diff --git a/src/Messages/OWSIdentityManager.h b/src/Messages/OWSIdentityManager.h index 568b8a6c5..c1255157b 100644 --- a/src/Messages/OWSIdentityManager.h +++ b/src/Messages/OWSIdentityManager.h @@ -2,22 +2,19 @@ // Copyright (c) 2017 Open Whisper Systems. All rights reserved. // +#import "OWSRecipientIdentity.h" #import NS_ASSUME_NONNULL_BEGIN extern NSString *const TSStorageManagerTrustedKeysCollection; +// This notification will be fired whenever identities are created +// or their verification state changes. extern NSString *const kNSNotificationName_IdentityStateDidChange; @class OWSRecipientIdentity; -// typedef NS_ENUM(NSUInteger, OWSVerificationState) { -// OWSVerificationStateDefault, -// OWSVerificationStateVerified, -// OWSVerificationStateNoLongerVerified, -//}; - // This class can be safely accessed and used from any thread. @interface OWSIdentityManager : NSObject @@ -25,47 +22,6 @@ extern NSString *const kNSNotificationName_IdentityStateDidChange; + (instancetype)sharedManager; -//- (void)setVerificationState:(OWSVerificationState)verificationState -// forPhoneNumber:(NSString *)phoneNumber -// isUserInitiatedChange:(BOOL)isUserInitiatedChange; -// -//- (OWSVerificationState)verificationStateForPhoneNumber:(NSString *)phoneNumber; - -/** - * Explicitly mark an identity as approved for blocking/nonblocking use - * e.g. in response to a user confirmation action. - * - * @param identityKey key data used to identify the recipient - * @param recipientId unique stable identifier for the recipient, e.g. e164 phone number - * @param approvedForBlockingUse if the user wants explicit confirmation before sending to changed numbers, whether - * that confirmation has occurred. - * @param approvedForNonBlockingUse YES to override the duration during which we consider an SN "too soon" to send. - * - * @returns YES if we are replacing an existing known identity key for recipientId. - * NO if there was no previously stored identity key for the recipient. - */ -//- (BOOL)saveRemoteIdentity:(NSData *)identityKey -// recipientId:(NSString *)recipientId -// approvedForBlockingUse:(BOOL)approvedForBlockingUse -// approvedForNonBlockingUse:(BOOL)approvedForNonBlockingUse; - -/** - * Check if a recipient identity corresponds to an untrusted identity - * - * @param recipientId unique stable identifier for the recipient, e.g. e164 phone number - * @returns nil if the identity doesn't exist or if it's trusted - * else returns the untrusted identity - */ -//- (nullable OWSRecipientIdentity *)unconfirmedIdentityThatShouldBlockSendingForRecipientId:(NSString *)recipientId; - -/** - * @param recipientId unique stable identifier for the recipient, e.g. e164 phone number - * @returns nil if the recipient's current id has been seen, or if it's the users first key - * else returns the unseen identity - * - */ -//- (nullable OWSRecipientIdentity *)unseenIdentityChangeForRecipientId:(NSString *)recipientId; - /** * @param recipientId unique stable identifier for the recipient, e.g. e164 phone number * @returns if the recipient's current identity is trusted. @@ -74,7 +30,13 @@ extern NSString *const kNSNotificationName_IdentityStateDidChange; - (void)generateNewIdentityKey; - (nullable NSData *)identityKeyForRecipientId:(NSString *)recipientId; -//- (void)removeIdentityKeyForRecipient:(NSString *)receipientId; + +- (void)setVerificationState:(OWSVerificationState)verificationState + identityKey:(NSData *)identityKey + recipientId:(NSString *)recipientId + sendSyncMessage:(BOOL)sendSyncMessage; + +- (OWSVerificationState)verificationStateForRecipientId:(NSString *)recipientId; @end diff --git a/src/Messages/OWSIdentityManager.m b/src/Messages/OWSIdentityManager.m index f5555bef6..6fedaffca 100644 --- a/src/Messages/OWSIdentityManager.m +++ b/src/Messages/OWSIdentityManager.m @@ -3,22 +3,15 @@ // #import "OWSIdentityManager.h" -#import "OWSMessageSender.h" -#import "TSStorageManager.h" -#import "TextSecureKitEnv.h" - -// TODO: Review -#import "NSDate+millisecondTimeStamp.h" #import "NotificationsProtocol.h" -#import "OWSIdentityManager.h" +#import "OWSMessageSender.h" #import "OWSRecipientIdentity.h" +#import "TextSecureKitEnv.h" #import "TSAccountManager.h" #import "TSContactThread.h" #import "TSErrorMessage.h" #import "TSGroupThread.h" -#import "TSPreferences.h" -#import "TSStorageManager+SessionStore.h" -#import "TextSecureKitEnv.h" +#import "TSStorageManager.h" #import <25519/Curve25519.h> NS_ASSUME_NONNULL_BEGIN @@ -35,20 +28,11 @@ const NSTimeInterval kIdentityKeyStoreNonBlockingSecondsThreshold = 5.0; NSString *const kNSNotificationName_IdentityStateDidChange = @"kNSNotificationName_IdentityStateDidChange"; -// NSString *const kOWSIdentityManager_Collection = @"kOWSIdentityManager_Collection"; -//// This key is used to persist the current "verification map" state. -// NSString *const kOWSIdentityManager_VerificationMapKey = @"kOWSIdentityManager_VerificationMapKey"; - @interface OWSIdentityManager () @property (nonatomic, readonly) TSStorageManager *storageManager; @property (nonatomic, readonly) OWSMessageSender *messageSender; -//// We don't store the phone numbers as instances of PhoneNumber to avoid -//// consistency issues between clients, but these should all be valid e164 -//// phone numbers. -//@property (nonatomic, readonly) NSMutableDictionary *verificationMap; - @end #pragma mark - @@ -93,53 +77,6 @@ NSString *const kNSNotificationName_IdentityStateDidChange = @"kNSNotificationNa return self; } -//- (OWSVerificationState)verificationStateForPhoneNumber:(NSString *)phoneNumber -//{ -// OWSAssert(phoneNumber.length > 0); -// -// @synchronized(self) -// { -// [self lazyLoadStateIfNecessary]; -// OWSAssert(self.verificationMap); -// -// NSNumber * _Nullable existingValue = self.verificationMap[phoneNumber]; -// -// return (existingValue -// ? (OWSVerificationState) existingValue.intValue -// : OWSVerificationStateDefault); -// } -//} -// -//- (void)handleUpdate:(NSDictionary *)verificationMap -// sendSyncMessage:(BOOL)sendSyncMessage -//{ -// OWSAssert(verificationMap); -// -// [_storageManager setObject:verificationMap -// forKey:kOWSIdentityManager_VerificationMapKey -// inCollection:kOWSIdentityManager_Collection]; -// -// dispatch_async(dispatch_get_main_queue(), ^{ -// [[NSNotificationCenter defaultCenter] postNotificationName:kNSNotificationName_VerificationStateDidChange -// object:nil -// userInfo:nil]; -// }); -//} -// -//// This method should only be called from within a synchronized block. -//- (void)lazyLoadStateIfNecessary -//{ -// if (self.verificationMap) { -// // verificationMap has already been loaded, abort. -// return; -// } -// -// NSDictionary *verificationMap = -// [_storageManager objectForKey:kOWSIdentityManager_VerificationMapKey -// inCollection:kOWSIdentityManager_Collection]; -// _verificationMap = (verificationMap ? [verificationMap mutableCopy] : [NSMutableDictionary new]); -//} - - (BOOL)isCurrentIdentityTrustedForSendingWithRecipientId:(NSString *)recipientId { OWSAssert(recipientId.length > 0); @@ -252,11 +189,10 @@ NSString *const kNSNotificationName_IdentityStateDidChange = @"kNSNotificationNa OWSAssert(identityKey.length > 0); OWSAssert(recipientId.length > 0); - // NSDictionary *verificationMapCopy = nil; - @synchronized(self) { - + // Ensure a remote identity exists for this key. We may be learning about + // it for the first time. [self saveRemoteIdentity:identityKey recipientId:recipientId]; OWSRecipientIdentity *identity = [OWSRecipientIdentity fetchObjectWithUniqueID:recipientId]; @@ -282,6 +218,23 @@ NSString *const kNSNotificationName_IdentityStateDidChange = @"kNSNotificationNa [self fireIdentityStateChangeNotification]; } +- (OWSVerificationState)verificationStateForRecipientId:(NSString *)recipientId +{ + OWSAssert(recipientId.length > 0); + + @synchronized(self) + { + OWSRecipientIdentity *_Nullable currentIdentity = [OWSRecipientIdentity fetchObjectWithUniqueID:recipientId]; + + if (!currentIdentity) { + // We might not know the identity for this recipient yet. + return OWSVerificationStateDefault; + } + + return currentIdentity.verificationState; + } +} + - (void)fireIdentityStateChangeNotification { dispatch_async(dispatch_get_main_queue(), ^{ @@ -331,56 +284,6 @@ NSString *const kNSNotificationName_IdentityStateDidChange = @"kNSNotificationNa } } -// TODO: Cull unused methods within this class. - -//- (nullable OWSRecipientIdentity *)unconfirmedIdentityThatShouldBlockSendingForRecipientId:(NSString *)recipientId; -//{ -// OWSAssert(recipientId != nil); -// -// @synchronized(self) -// { -// OWSRecipientIdentity *currentIdentity = [OWSRecipientIdentity fetchObjectWithUniqueID:recipientId]; -// if (currentIdentity == nil) { -// // No preexisting key, Trust On First Use -// return nil; -// } -// -// if ([self isTrustedIdentityKey:currentIdentity.identityKey -// recipientId:currentIdentity.recipientId -// direction:TSMessageDirectionOutgoing]) { -// return nil; -// } -// -// // identity not yet trusted for sending -// return currentIdentity; -// } -//} - -//- (nullable OWSRecipientIdentity *)unseenIdentityChangeForRecipientId:(NSString *)recipientId -//{ -// OWSAssert(recipientId != nil); -// -// @synchronized(self) -// { -// 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);