From e10cc0c1803c598a7518b6c8b28195e20f0eb12e Mon Sep 17 00:00:00 2001 From: Michael Kirk Date: Wed, 31 May 2017 11:30:05 -0700 Subject: [PATCH 1/2] determine if recipient identity change is unseen // FREEBIE --- .../TSStorageManager+IdentityKeyStore.h | 7 +++++++ .../TSStorageManager+IdentityKeyStore.m | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/Storage/AxolotlStore/TSStorageManager+IdentityKeyStore.h b/src/Storage/AxolotlStore/TSStorageManager+IdentityKeyStore.h index 841d7997a..0244f7b03 100644 --- a/src/Storage/AxolotlStore/TSStorageManager+IdentityKeyStore.h +++ b/src/Storage/AxolotlStore/TSStorageManager+IdentityKeyStore.h @@ -40,6 +40,13 @@ extern NSString *const TSStorageManagerTrustedKeysCollection; */ - (nullable OWSRecipientIdentity *)unconfirmedIdentityThatShouldBlockSendingForRecipientId:(NSString *)recipientId; +/** + * @param recipientId unique stable identifier for the recipient, e.g. e164 phone number + * @returns YES if the recipient's id has not been marked as seen (and it's not a TOFU situation) + * NO if the recipient's current id has been seen, or if it's the users first key + */ +- (BOOL)hasUnseenIdentityChangeForRecipientId:(NSString *)recipientId; + - (void)generateNewIdentityKey; - (nullable NSData *)identityKeyForRecipientId:(NSString *)recipientId; - (void)removeIdentityKeyForRecipient:(NSString *)receipientId; diff --git a/src/Storage/AxolotlStore/TSStorageManager+IdentityKeyStore.m b/src/Storage/AxolotlStore/TSStorageManager+IdentityKeyStore.m index c6ee65256..925e09567 100644 --- a/src/Storage/AxolotlStore/TSStorageManager+IdentityKeyStore.m +++ b/src/Storage/AxolotlStore/TSStorageManager+IdentityKeyStore.m @@ -278,6 +278,24 @@ const NSTimeInterval kIdentityKeyStoreNonBlockingSecondsThreshold = 5.0; } } + +- (BOOL)hasUnseenIdentityChangeForRecipientId:(NSString *)recipientId +{ + OWSAssert(recipientId != nil); + + OWSRecipientIdentity *recipientIdentity = [OWSRecipientIdentity fetchObjectWithUniqueID:recipientId]; + + if (!recipientIdentity) { + return NO; + } + + if (recipientIdentity.isFirstKnownKey) { + return NO; + } + + return !recipientIdentity.wasSeen; +} + @end NS_ASSUME_NONNULL_END From ebd4800e21a1dd3410bfa498f986706c5c71aa4c Mon Sep 17 00:00:00 2001 From: Michael Kirk Date: Wed, 31 May 2017 17:40:59 -0700 Subject: [PATCH 2/2] return unseen identity rather than bool This turns out to be more versitile for the client app // FREEBIE --- .../TSStorageManager+IdentityKeyStore.h | 7 +-- .../TSStorageManager+IdentityKeyStore.m | 43 +++++++++++-------- 2 files changed, 29 insertions(+), 21 deletions(-) diff --git a/src/Storage/AxolotlStore/TSStorageManager+IdentityKeyStore.h b/src/Storage/AxolotlStore/TSStorageManager+IdentityKeyStore.h index 0244f7b03..e178b7f3a 100644 --- a/src/Storage/AxolotlStore/TSStorageManager+IdentityKeyStore.h +++ b/src/Storage/AxolotlStore/TSStorageManager+IdentityKeyStore.h @@ -42,10 +42,11 @@ extern NSString *const TSStorageManagerTrustedKeysCollection; /** * @param recipientId unique stable identifier for the recipient, e.g. e164 phone number - * @returns YES if the recipient's id has not been marked as seen (and it's not a TOFU situation) - * NO if the recipient's current id has been seen, or if it's the users first key + * @returns nil if the recipient's current id has been seen, or if it's the users first key + * else returns the unseen identity + * */ -- (BOOL)hasUnseenIdentityChangeForRecipientId:(NSString *)recipientId; +- (nullable OWSRecipientIdentity *)unseenIdentityChangeForRecipientId:(NSString *)recipientId; - (void)generateNewIdentityKey; - (nullable NSData *)identityKeyForRecipientId:(NSString *)recipientId; diff --git a/src/Storage/AxolotlStore/TSStorageManager+IdentityKeyStore.m b/src/Storage/AxolotlStore/TSStorageManager+IdentityKeyStore.m index 925e09567..2c78f6f48 100644 --- a/src/Storage/AxolotlStore/TSStorageManager+IdentityKeyStore.m +++ b/src/Storage/AxolotlStore/TSStorageManager+IdentityKeyStore.m @@ -199,6 +199,31 @@ const NSTimeInterval kIdentityKeyStoreNonBlockingSecondsThreshold = 5.0; } } +- (nullable OWSRecipientIdentity *)unseenIdentityChangeForRecipientId:(NSString *)recipientId +{ + OWSAssert(recipientId != nil); + + @synchronized([[self class] sharedIdentityKeyLock]) + { + OWSRecipientIdentity *currentIdentity = [OWSRecipientIdentity fetchObjectWithUniqueID:recipientId]; + if (currentIdentity == nil) { + // No preexisting key, Trust On First Use + return nil; + } + + if (currentIdentity.isFirstKnownKey) { + return nil; + } + + if (currentIdentity.wasSeen) { + return nil; + } + + // identity not yet seen + return currentIdentity; + } +} + - (BOOL)isTrustedKey:(NSData *)identityKey forSendingToIdentity:(nullable OWSRecipientIdentity *)recipientIdentity { OWSAssert(identityKey != nil); @@ -278,24 +303,6 @@ const NSTimeInterval kIdentityKeyStoreNonBlockingSecondsThreshold = 5.0; } } - -- (BOOL)hasUnseenIdentityChangeForRecipientId:(NSString *)recipientId -{ - OWSAssert(recipientId != nil); - - OWSRecipientIdentity *recipientIdentity = [OWSRecipientIdentity fetchObjectWithUniqueID:recipientId]; - - if (!recipientIdentity) { - return NO; - } - - if (recipientIdentity.isFirstKnownKey) { - return NO; - } - - return !recipientIdentity.wasSeen; -} - @end NS_ASSUME_NONNULL_END