From bbd689bfd0d81e320ea635c68771db33bfa8e281 Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Tue, 30 Jan 2018 16:41:25 -0500 Subject: [PATCH] Add protocol context to protocol kit. --- .../ConversationViewController.m | 4 +-- .../FingerprintViewController.m | 3 +-- .../OWSConversationSettingsViewController.m | 3 +-- .../OWSLinkDeviceViewController.m | 2 +- .../ShowGroupMembersViewController.m | 9 +++---- Signal/src/environment/NotificationsManager.m | 3 +-- .../src/Account/TSPreKeyManager.m | 4 +-- .../src/Contacts/Threads/TSContactThread.m | 2 +- .../src/Messages/OWSIdentityManager.h | 8 +++--- .../src/Messages/OWSIdentityManager.m | 6 ++--- .../src/Security/OWSFingerprintBuilder.m | 5 ++-- .../src/Security/OWSRecipientIdentity.m | 27 +++++-------------- .../TSStorageManager+SessionStore.m | 3 --- .../TSStorageManager+SignedPreKeyStore.m | 2 +- 14 files changed, 28 insertions(+), 53 deletions(-) diff --git a/Signal/src/ViewControllers/ConversationView/ConversationViewController.m b/Signal/src/ViewControllers/ConversationView/ConversationViewController.m index 3ac6a52c9..911dee6b4 100644 --- a/Signal/src/ViewControllers/ConversationView/ConversationViewController.m +++ b/Signal/src/ViewControllers/ConversationView/ConversationViewController.m @@ -699,7 +699,7 @@ typedef NS_ENUM(NSInteger, MessagesRangeSizeMode) { { NSMutableArray *result = [NSMutableArray new]; for (NSString *recipientId in self.thread.recipientIdentifiers) { - if ([[OWSIdentityManager sharedManager] verificationStateForRecipientIdWithoutTransaction:recipientId] + if ([[OWSIdentityManager sharedManager] verificationStateForRecipientId:recipientId] == OWSVerificationStateNoLongerVerified) { [result addObject:recipientId]; } @@ -1303,7 +1303,7 @@ typedef NS_ENUM(NSInteger, MessagesRangeSizeMode) { BOOL isVerified = YES; for (NSString *recipientId in self.thread.recipientIdentifiers) { - if ([[OWSIdentityManager sharedManager] verificationStateForRecipientIdWithoutTransaction:recipientId] + if ([[OWSIdentityManager sharedManager] verificationStateForRecipientId:recipientId] != OWSVerificationStateVerified) { isVerified = NO; break; diff --git a/Signal/src/ViewControllers/FingerprintViewController.m b/Signal/src/ViewControllers/FingerprintViewController.m index 3af6a0a8c..bb0d97b14 100644 --- a/Signal/src/ViewControllers/FingerprintViewController.m +++ b/Signal/src/ViewControllers/FingerprintViewController.m @@ -338,8 +338,7 @@ typedef void (^CustomLayoutBlock)(void); { OWSAssert(self.recipientId.length > 0); - BOOL isVerified = - [[OWSIdentityManager sharedManager] verificationStateForRecipientIdWithoutTransaction:self.recipientId] + BOOL isVerified = [[OWSIdentityManager sharedManager] verificationStateForRecipientId:self.recipientId] == OWSVerificationStateVerified; if (isVerified) { diff --git a/Signal/src/ViewControllers/OWSConversationSettingsViewController.m b/Signal/src/ViewControllers/OWSConversationSettingsViewController.m index 0440e84bd..0f493de33 100644 --- a/Signal/src/ViewControllers/OWSConversationSettingsViewController.m +++ b/Signal/src/ViewControllers/OWSConversationSettingsViewController.m @@ -682,8 +682,7 @@ NS_ASSUME_NONNULL_BEGIN } } - BOOL isVerified = - [[OWSIdentityManager sharedManager] verificationStateForRecipientIdWithoutTransaction:recipientId] + BOOL isVerified = [[OWSIdentityManager sharedManager] verificationStateForRecipientId:recipientId] == OWSVerificationStateVerified; if (isVerified) { NSMutableAttributedString *subtitle = [NSMutableAttributedString new]; diff --git a/Signal/src/ViewControllers/OWSLinkDeviceViewController.m b/Signal/src/ViewControllers/OWSLinkDeviceViewController.m index 81313d237..2c178d215 100644 --- a/Signal/src/ViewControllers/OWSLinkDeviceViewController.m +++ b/Signal/src/ViewControllers/OWSLinkDeviceViewController.m @@ -149,7 +149,7 @@ NS_ASSUME_NONNULL_BEGIN // Optimistically set this flag. [OWSDeviceManager.sharedManager setMayHaveLinkedDevices]; - ECKeyPair *_Nullable identityKeyPair = [[OWSIdentityManager sharedManager] identityKeyPairWithoutProtocolContext]; + ECKeyPair *_Nullable identityKeyPair = [[OWSIdentityManager sharedManager] identityKeyPair]; OWSAssert(identityKeyPair); NSData *myPublicKey = identityKeyPair.publicKey; NSData *myPrivateKey = identityKeyPair.ows_privateKey; diff --git a/Signal/src/ViewControllers/ShowGroupMembersViewController.m b/Signal/src/ViewControllers/ShowGroupMembersViewController.m index b0f17d2dd..23e616614 100644 --- a/Signal/src/ViewControllers/ShowGroupMembersViewController.m +++ b/Signal/src/ViewControllers/ShowGroupMembersViewController.m @@ -177,7 +177,7 @@ NS_ASSUME_NONNULL_BEGIN ContactTableViewCell *cell = [ContactTableViewCell new]; SignalAccount *signalAccount = [helper signalAccountForRecipientId:recipientId]; OWSVerificationState verificationState = - [[OWSIdentityManager sharedManager] verificationStateForRecipientIdWithoutTransaction:recipientId]; + [[OWSIdentityManager sharedManager] verificationStateForRecipientId:recipientId]; BOOL isVerified = verificationState == OWSVerificationStateVerified; BOOL isNoLongerVerified = verificationState == OWSVerificationStateNoLongerVerified; BOOL isBlocked = [helper isRecipientIdBlocked:recipientId]; @@ -244,10 +244,9 @@ NS_ASSUME_NONNULL_BEGIN OWSIdentityManager *identityManger = [OWSIdentityManager sharedManager]; NSArray *recipientIds = [self noLongerVerifiedRecipientIds]; for (NSString *recipientId in recipientIds) { - OWSVerificationState verificationState = - [identityManger verificationStateForRecipientIdWithoutTransaction:recipientId]; + OWSVerificationState verificationState = [identityManger verificationStateForRecipientId:recipientId]; if (verificationState == OWSVerificationStateNoLongerVerified) { - NSData *identityKey = [identityManger identityKeyForRecipientIdWOT:recipientId]; + NSData *identityKey = [identityManger identityKeyForRecipientId:recipientId]; if (identityKey.length < 1) { OWSFail(@"Missing identity key for: %@", recipientId); continue; @@ -271,7 +270,7 @@ NS_ASSUME_NONNULL_BEGIN { NSMutableArray *result = [NSMutableArray new]; for (NSString *recipientId in self.thread.recipientIdentifiers) { - if ([[OWSIdentityManager sharedManager] verificationStateForRecipientIdWithoutTransaction:recipientId] + if ([[OWSIdentityManager sharedManager] verificationStateForRecipientId:recipientId] == OWSVerificationStateNoLongerVerified) { [result addObject:recipientId]; } diff --git a/Signal/src/environment/NotificationsManager.m b/Signal/src/environment/NotificationsManager.m index 19d343a21..90f76ab31 100644 --- a/Signal/src/environment/NotificationsManager.m +++ b/Signal/src/environment/NotificationsManager.m @@ -291,8 +291,7 @@ NSString *const kNotificationsManagerNewMesssageSoundName = @"NewMessage.aifc"; // "no longer verified". BOOL isNoLongerVerified = NO; for (NSString *recipientId in thread.recipientIdentifiers) { - if ([OWSIdentityManager.sharedManager - verificationStateForRecipientIdWithoutTransaction:recipientId] + if ([OWSIdentityManager.sharedManager verificationStateForRecipientId:recipientId] == OWSVerificationStateNoLongerVerified) { isNoLongerVerified = YES; break; diff --git a/SignalServiceKit/src/Account/TSPreKeyManager.m b/SignalServiceKit/src/Account/TSPreKeyManager.m index b9afdbbe7..9078842f3 100644 --- a/SignalServiceKit/src/Account/TSPreKeyManager.m +++ b/SignalServiceKit/src/Account/TSPreKeyManager.m @@ -134,11 +134,11 @@ static const NSTimeInterval kSignedPreKeyUpdateFailureMaxFailureDuration = 10 * RefreshPreKeysMode modeCopy = mode; TSStorageManager *storageManager = [TSStorageManager sharedManager]; - ECKeyPair *identityKeyPair = [[OWSIdentityManager sharedManager] identityKeyPairWithoutProtocolContext]; + ECKeyPair *identityKeyPair = [[OWSIdentityManager sharedManager] identityKeyPair]; if (!identityKeyPair) { [[OWSIdentityManager sharedManager] generateNewIdentityKey]; - identityKeyPair = [[OWSIdentityManager sharedManager] identityKeyPairWithoutProtocolContext]; + identityKeyPair = [[OWSIdentityManager sharedManager] identityKeyPair]; // Switch modes if necessary. modeCopy = RefreshPreKeysMode_SignedAndOneTime; diff --git a/SignalServiceKit/src/Contacts/Threads/TSContactThread.m b/SignalServiceKit/src/Contacts/Threads/TSContactThread.m index f59d407b5..599ea9202 100644 --- a/SignalServiceKit/src/Contacts/Threads/TSContactThread.m +++ b/SignalServiceKit/src/Contacts/Threads/TSContactThread.m @@ -111,7 +111,7 @@ NS_ASSUME_NONNULL_BEGIN - (BOOL)hasSafetyNumbers { - return !![[OWSIdentityManager sharedManager] identityKeyForRecipientIdWOT:self.contactIdentifier]; + return !![[OWSIdentityManager sharedManager] identityKeyForRecipientId:self.contactIdentifier]; } - (NSString *)name diff --git a/SignalServiceKit/src/Messages/OWSIdentityManager.h b/SignalServiceKit/src/Messages/OWSIdentityManager.h index 5e039d692..a3f887c4d 100644 --- a/SignalServiceKit/src/Messages/OWSIdentityManager.h +++ b/SignalServiceKit/src/Messages/OWSIdentityManager.h @@ -30,8 +30,7 @@ extern const NSUInteger kIdentityKeyLength; - (void)generateNewIdentityKey; -// TODO: Rename to identityKeyForRecipientId. -- (nullable NSData *)identityKeyForRecipientIdWOT:(NSString *)recipientId; +- (nullable NSData *)identityKeyForRecipientId:(NSString *)recipientId; - (nullable NSData *)identityKeyForRecipientId:(NSString *)recipientId protocolContext:(nullable id)protocolContext; @@ -41,7 +40,7 @@ extern const NSUInteger kIdentityKeyLength; isUserInitiatedChange:(BOOL)isUserInitiatedChange protocolContext:(nullable id)protocolContext; -- (OWSVerificationState)verificationStateForRecipientIdWithoutTransaction:(NSString *)recipientId; +- (OWSVerificationState)verificationStateForRecipientId:(NSString *)recipientId; - (OWSVerificationState)verificationStateForRecipientId:(NSString *)recipientId transaction:(YapDatabaseReadWriteTransaction *)transaction; @@ -59,8 +58,7 @@ extern const NSUInteger kIdentityKeyLength; #pragma mark - Debug -// TODO: -- (nullable ECKeyPair *)identityKeyPairWithoutProtocolContext; +- (nullable ECKeyPair *)identityKeyPair; #if DEBUG // Clears everything except the local identity key. diff --git a/SignalServiceKit/src/Messages/OWSIdentityManager.m b/SignalServiceKit/src/Messages/OWSIdentityManager.m index a189d3365..e43ff3b8f 100644 --- a/SignalServiceKit/src/Messages/OWSIdentityManager.m +++ b/SignalServiceKit/src/Messages/OWSIdentityManager.m @@ -132,7 +132,7 @@ NSString *const kNSNotificationName_IdentityStateDidChange = @"kNSNotificationNa inCollection:TSStorageManagerIdentityKeyStoreCollection]; } -- (nullable NSData *)identityKeyForRecipientIdWOT:(NSString *)recipientId +- (nullable NSData *)identityKeyForRecipientId:(NSString *)recipientId { __block NSData *_Nullable result = nil; [self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *_Nonnull transaction) { @@ -151,7 +151,7 @@ NSString *const kNSNotificationName_IdentityStateDidChange = @"kNSNotificationNa return [OWSRecipientIdentity fetchObjectWithUniqueID:recipientId transaction:transaction].identityKey; } -- (nullable ECKeyPair *)identityKeyPairWithoutProtocolContext +- (nullable ECKeyPair *)identityKeyPair { __block ECKeyPair *_Nullable identityKeyPair = nil; [self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *_Nonnull transaction) { @@ -305,7 +305,7 @@ NSString *const kNSNotificationName_IdentityStateDidChange = @"kNSNotificationNa [self fireIdentityStateChangeNotification]; } -- (OWSVerificationState)verificationStateForRecipientIdWithoutTransaction:(NSString *)recipientId +- (OWSVerificationState)verificationStateForRecipientId:(NSString *)recipientId { __block OWSVerificationState result; // Use a read/write transaction to block on latest. diff --git a/SignalServiceKit/src/Security/OWSFingerprintBuilder.m b/SignalServiceKit/src/Security/OWSFingerprintBuilder.m index 303ed13ad..afd2fe5e4 100644 --- a/SignalServiceKit/src/Security/OWSFingerprintBuilder.m +++ b/SignalServiceKit/src/Security/OWSFingerprintBuilder.m @@ -36,8 +36,7 @@ NS_ASSUME_NONNULL_BEGIN - (nullable OWSFingerprint *)fingerprintWithTheirSignalId:(NSString *)theirSignalId { - NSData *_Nullable theirIdentityKey = - [[OWSIdentityManager sharedManager] identityKeyForRecipientIdWOT:theirSignalId]; + NSData *_Nullable theirIdentityKey = [[OWSIdentityManager sharedManager] identityKeyForRecipientId:theirSignalId]; if (theirIdentityKey == nil) { OWSFail(@"%@ Missing their identity key", self.logTag); @@ -52,7 +51,7 @@ NS_ASSUME_NONNULL_BEGIN NSString *theirName = [self.contactsManager displayNameForPhoneIdentifier:theirSignalId]; NSString *mySignalId = [self.accountManager localNumber]; - NSData *myIdentityKey = [[OWSIdentityManager sharedManager] identityKeyPairWithoutProtocolContext].publicKey; + NSData *myIdentityKey = [[OWSIdentityManager sharedManager] identityKeyPair].publicKey; return [OWSFingerprint fingerprintWithMyStableId:mySignalId myIdentityKey:myIdentityKey diff --git a/SignalServiceKit/src/Security/OWSRecipientIdentity.m b/SignalServiceKit/src/Security/OWSRecipientIdentity.m index dbf258589..997a16576 100644 --- a/SignalServiceKit/src/Security/OWSRecipientIdentity.m +++ b/SignalServiceKit/src/Security/OWSRecipientIdentity.m @@ -3,6 +3,7 @@ // #import "OWSRecipientIdentity.h" +#import "TSStorageManager+SessionStore.h" #import "TSStorageManager.h" #import @@ -127,21 +128,21 @@ OWSSignalServiceProtosVerifiedState OWSVerificationStateToProtoState(OWSVerifica - (void)saveWithTransaction:(YapDatabaseReadWriteTransaction *)transaction { - OWSAssert(transaction.connection == [OWSRecipientIdentity dbReadWriteConnection]); + OWSAssert(!transaction.connection.objectCacheEnabled); [super saveWithTransaction:transaction]; } - (void)removeWithTransaction:(YapDatabaseReadWriteTransaction *)transaction { - OWSAssert(transaction.connection == [OWSRecipientIdentity dbReadWriteConnection]); + OWSAssert(!transaction.connection.objectCacheEnabled); [super removeWithTransaction:transaction]; } - (void)touchWithTransaction:(YapDatabaseReadWriteTransaction *)transaction { - OWSAssert(transaction.connection == [OWSRecipientIdentity dbReadWriteConnection]); + OWSAssert(!transaction.connection.objectCacheEnabled); [super touchWithTransaction:transaction]; } @@ -149,7 +150,7 @@ OWSSignalServiceProtosVerifiedState OWSVerificationStateToProtoState(OWSVerifica + (nullable instancetype)fetchObjectWithUniqueID:(NSString *)uniqueID transaction:(YapDatabaseReadTransaction *)transaction { - OWSAssert(transaction.connection == [OWSRecipientIdentity dbReadConnection]); + OWSAssert(!transaction.connection.objectCacheEnabled); return [super fetchObjectWithUniqueID:uniqueID transaction:transaction]; } @@ -161,25 +162,9 @@ OWSSignalServiceProtosVerifiedState OWSVerificationStateToProtoState(OWSVerifica return self.dbReadWriteConnection; } -// TODO: Replace with protocol connection? -/** - * Override to disable the object cache to better enforce transaction semantics on the store. - * Note that it's still technically possible to access this collection from a different collection, - * but that should be considered a bug. - */ + (YapDatabaseConnection *)dbReadWriteConnection { - static dispatch_once_t onceToken; - static YapDatabaseConnection *sharedDBConnection; - dispatch_once(&onceToken, ^{ - sharedDBConnection = [TSStorageManager sharedManager].newDatabaseConnection; - sharedDBConnection.objectCacheEnabled = NO; -#if DEBUG - sharedDBConnection.permittedTransactions = YDB_AnySyncTransaction; -#endif - }); - - return sharedDBConnection; + return TSStorageManager.protocolStoreDBConnection; } - (YapDatabaseConnection *)dbReadConnection diff --git a/SignalServiceKit/src/Storage/AxolotlStore/TSStorageManager+SessionStore.m b/SignalServiceKit/src/Storage/AxolotlStore/TSStorageManager+SessionStore.m index 180bc225c..f40711c6c 100644 --- a/SignalServiceKit/src/Storage/AxolotlStore/TSStorageManager+SessionStore.m +++ b/SignalServiceKit/src/Storage/AxolotlStore/TSStorageManager+SessionStore.m @@ -27,9 +27,6 @@ NSString *const kSessionStoreDBConnectionKey = @"kSessionStoreDBConnectionKey"; dispatch_once(&onceToken, ^{ protocolStoreDBConnection = [TSStorageManager sharedManager].newDatabaseConnection; protocolStoreDBConnection.objectCacheEnabled = NO; -#if DEBUG - protocolStoreDBConnection.permittedTransactions = YDB_AnySyncTransaction; -#endif }); return protocolStoreDBConnection; diff --git a/SignalServiceKit/src/Storage/AxolotlStore/TSStorageManager+SignedPreKeyStore.m b/SignalServiceKit/src/Storage/AxolotlStore/TSStorageManager+SignedPreKeyStore.m index 5e85643fe..bbd80e11f 100644 --- a/SignalServiceKit/src/Storage/AxolotlStore/TSStorageManager+SignedPreKeyStore.m +++ b/SignalServiceKit/src/Storage/AxolotlStore/TSStorageManager+SignedPreKeyStore.m @@ -27,7 +27,7 @@ NSString *const TSStorageManagerKeyPrekeyCurrentSignedPrekeyId = @"currentSigned // Signed prekey ids must be > 0. int preKeyId = 1 + arc4random_uniform(INT32_MAX - 1); - ECKeyPair *_Nullable identityKeyPair = [[OWSIdentityManager sharedManager] identityKeyPairWithoutProtocolContext]; + ECKeyPair *_Nullable identityKeyPair = [[OWSIdentityManager sharedManager] identityKeyPair]; return [[SignedPreKeyRecord alloc] initWithId:preKeyId keyPair:keyPair