From ca822480b146cb536045e724c0c2ad5d20b66dcf Mon Sep 17 00:00:00 2001 From: Niels Andriesse Date: Tue, 20 Aug 2019 13:54:37 +1000 Subject: [PATCH] Update for seed changes --- .../OnboardingKeyPairViewController.swift | 37 ++++++++++--------- .../AppSettings/AppSettingsViewController.m | 7 +++- .../src/Account/CreatePreKeysOperation.swift | 2 +- .../src/Messages/OWSIdentityManager.h | 3 +- .../src/Messages/OWSIdentityManager.m | 13 +++++-- .../Storage/TSStorageIdentityKeyStoreTests.m | 2 +- 6 files changed, 37 insertions(+), 27 deletions(-) diff --git a/Signal/src/Loki/OnboardingKeyPairViewController.swift b/Signal/src/Loki/OnboardingKeyPairViewController.swift index 7aa311baf..ebf9a5e68 100644 --- a/Signal/src/Loki/OnboardingKeyPairViewController.swift +++ b/Signal/src/Loki/OnboardingKeyPairViewController.swift @@ -1,7 +1,7 @@ final class OnboardingKeyPairViewController : OnboardingBaseViewController { private var mode: Mode = .register { didSet { if mode != oldValue { handleModeChanged() } } } - private var keyPair: ECKeyPair! { didSet { updateMnemonic() } } + private var seed: Data! { didSet { updateMnemonic() } } private var mnemonic: String! { didSet { handleMnemonicChanged() } } private var userName: String? @@ -104,7 +104,7 @@ final class OnboardingKeyPairViewController : OnboardingBaseViewController { super.loadView() setUpViewHierarchy() handleModeChanged() // Perform initial update - updateKeyPair() + updateSeed() } private func setUpViewHierarchy() { @@ -159,14 +159,13 @@ final class OnboardingKeyPairViewController : OnboardingBaseViewController { if mode == .register { mnemonicTextField.resignFirstResponder() } } - private func updateKeyPair() { - let identityManager = OWSIdentityManager.shared() - identityManager.generateNewIdentityKey() // Generate and store a new identity key pair - keyPair = identityManager.identityKeyPair()! + private func updateSeed() { + seed = Randomness.generateRandomBytes(16) } private func updateMnemonic() { - mnemonic = Mnemonic.encode(hexEncodedString: keyPair.hexEncodedPrivateKey) + let hexEncodedSeed = seed!.toHexString() + mnemonic = Mnemonic.encode(hexEncodedString: hexEncodedSeed) } private func handleMnemonicChanged() { @@ -191,25 +190,27 @@ final class OnboardingKeyPairViewController : OnboardingBaseViewController { } @objc private func registerOrRestore() { - let hexEncodedPublicKey: String + var seed: Data switch mode { - case .register: hexEncodedPublicKey = keyPair.hexEncodedPublicKey + case .register: seed = self.seed case .restore: let mnemonic = mnemonicTextField.text! do { - let hexEncodedPrivateKey = try Mnemonic.decode(mnemonic: mnemonic) - let keyPair = ECKeyPair.generate(withHexEncodedPrivateKey: hexEncodedPrivateKey) - // Use KVC to access dbConnection even though it's private - let databaseConnection = OWSIdentityManager.shared().value(forKey: "dbConnection") as! YapDatabaseConnection - // OWSPrimaryStorageIdentityKeyStoreIdentityKey is private so just use its value directly - databaseConnection.setObject(keyPair, forKey: "TSStorageManagerIdentityKeyStoreIdentityKey", inCollection: OWSPrimaryStorageIdentityKeyStoreCollection) - hexEncodedPublicKey = keyPair.hexEncodedPublicKey + let hexEncodedSeed = try Mnemonic.decode(mnemonic: mnemonic) + seed = Data(hex: hexEncodedSeed) } catch let error { let error = error as? Mnemonic.DecodingError ?? Mnemonic.DecodingError.generic - errorLabel.text = error.errorDescription - return + return errorLabel.text = error.errorDescription } } + // Use KVC to access dbConnection even though it's private + let databaseConnection = OWSIdentityManager.shared().value(forKey: "dbConnection") as! YapDatabaseConnection + databaseConnection.setObject(seed.toHexString(), forKey: "LKLokiSeed", inCollection: OWSPrimaryStorageIdentityKeyStoreCollection) + if seed.count == 16 { seed = seed + seed } + let identityManager = OWSIdentityManager.shared() + identityManager.generateNewIdentityKeyPair(fromSeed: seed) // This also stores it + let keyPair = identityManager.identityKeyPair()! + let hexEncodedPublicKey = keyPair.hexEncodedPublicKey let accountManager = TSAccountManager.sharedInstance() accountManager.phoneNumberAwaitingVerification = hexEncodedPublicKey accountManager.didRegister() diff --git a/Signal/src/ViewControllers/AppSettings/AppSettingsViewController.m b/Signal/src/ViewControllers/AppSettings/AppSettingsViewController.m index f583f3833..7ceab1e1c 100644 --- a/Signal/src/ViewControllers/AppSettings/AppSettingsViewController.m +++ b/Signal/src/ViewControllers/AppSettings/AppSettingsViewController.m @@ -520,8 +520,11 @@ - (void)showSeed { NSString *title = NSLocalizedString(@"Your Seed", @""); - ECKeyPair *keyPair = OWSIdentityManager.sharedManager.identityKeyPair; - NSString *mnemonic = [LKMnemonic encodeHexEncodedString:keyPair.hexEncodedPrivateKey]; + OWSIdentityManager *identityManager = OWSIdentityManager.sharedManager; + YapDatabaseConnection *databaseConnection = (YapDatabaseConnection *)[identityManager valueForKey:@"dbConnection"]; + NSString *hexEncodedSeed = [databaseConnection objectForKey:@"LKLokiSeed" inCollection:OWSPrimaryStorageIdentityKeyStoreCollection]; + if (hexEncodedSeed == nil) { hexEncodedSeed = identityManager.identityKeyPair.hexEncodedPrivateKey; } // Legacy account + NSString *mnemonic = [LKMnemonic encodeHexEncodedString:hexEncodedSeed]; UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:mnemonic preferredStyle:UIAlertControllerStyleAlert]; [alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { /* Do nothing */ }]]; [alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"Copy", @"") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { UIPasteboard.generalPasteboard.string = mnemonic; }]]; diff --git a/SignalServiceKit/src/Account/CreatePreKeysOperation.swift b/SignalServiceKit/src/Account/CreatePreKeysOperation.swift index ea1b091e6..b04e86f07 100644 --- a/SignalServiceKit/src/Account/CreatePreKeysOperation.swift +++ b/SignalServiceKit/src/Account/CreatePreKeysOperation.swift @@ -24,7 +24,7 @@ public class CreatePreKeysOperation: OWSOperation { Logger.debug("") if identityKeyManager.identityKeyPair() == nil { - identityKeyManager.generateNewIdentityKey() + identityKeyManager.generateNewIdentityKeyPair() } // Loki: We don't generate PreKeyRecords here. diff --git a/SignalServiceKit/src/Messages/OWSIdentityManager.h b/SignalServiceKit/src/Messages/OWSIdentityManager.h index 2e193192f..f8f14d41b 100644 --- a/SignalServiceKit/src/Messages/OWSIdentityManager.h +++ b/SignalServiceKit/src/Messages/OWSIdentityManager.h @@ -35,7 +35,8 @@ extern const NSUInteger kStoredIdentityKeyLength; + (instancetype)sharedManager; -- (void)generateNewIdentityKey; +- (void)generateNewIdentityKeyPair; +- (void)generateNewIdentityKeyPairFromSeed:(NSData *)seed; - (void)clearIdentityKey; - (void)setVerificationState:(OWSVerificationState)verificationState diff --git a/SignalServiceKit/src/Messages/OWSIdentityManager.m b/SignalServiceKit/src/Messages/OWSIdentityManager.m index b05cda119..d69b77e4f 100644 --- a/SignalServiceKit/src/Messages/OWSIdentityManager.m +++ b/SignalServiceKit/src/Messages/OWSIdentityManager.m @@ -117,11 +117,16 @@ NSString *const kNSNotificationName_IdentityStateDidChange = @"kNSNotificationNa object:nil]; } -- (void)generateNewIdentityKey +- (void)generateNewIdentityKeyPair { - [self.dbConnection setObject:[Curve25519 generateKeyPair] - forKey:OWSPrimaryStorageIdentityKeyStoreIdentityKey - inCollection:OWSPrimaryStorageIdentityKeyStoreCollection]; + ECKeyPair *keyPair = [Curve25519 generateKeyPair]; + [self.dbConnection setObject:keyPair forKey:OWSPrimaryStorageIdentityKeyStoreIdentityKey inCollection:OWSPrimaryStorageIdentityKeyStoreCollection]; +} + +- (void)generateNewIdentityKeyPairFromSeed:(NSData *)seed +{ + ECKeyPair *keyPair = [Curve25519 generateKeyPairFromSeed:seed]; + [self.dbConnection setObject:keyPair forKey:OWSPrimaryStorageIdentityKeyStoreIdentityKey inCollection:OWSPrimaryStorageIdentityKeyStoreCollection]; } - (void)clearIdentityKey diff --git a/SignalServiceKit/tests/Storage/TSStorageIdentityKeyStoreTests.m b/SignalServiceKit/tests/Storage/TSStorageIdentityKeyStoreTests.m index 2c19170c9..e92f62eaf 100644 --- a/SignalServiceKit/tests/Storage/TSStorageIdentityKeyStoreTests.m +++ b/SignalServiceKit/tests/Storage/TSStorageIdentityKeyStoreTests.m @@ -107,7 +107,7 @@ extern NSString *const OWSPrimaryStorageTrustedKeysCollection; - (void)testIdentityKey { - [[OWSIdentityManager sharedManager] generateNewIdentityKey]; + [[OWSIdentityManager sharedManager] generateNewIdentityKeyPair]; XCTAssert([[[OWSIdentityManager sharedManager] identityKeyPair].publicKey length] == 32); }