From 9d41abed444ac7befd602a6e3d219a407919d7ab Mon Sep 17 00:00:00 2001 From: nielsandriesse Date: Fri, 25 Sep 2020 11:57:54 +1000 Subject: [PATCH] Store the ED25519 key pair --- .../Loki/View Controllers/RegisterVC.swift | 23 +++++++++++-------- .../src/Messages/OWSIdentityManager.h | 5 ++++ .../src/Messages/OWSIdentityManager.m | 4 +++- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/Signal/src/Loki/View Controllers/RegisterVC.swift b/Signal/src/Loki/View Controllers/RegisterVC.swift index 83765a8e4..5823950a1 100644 --- a/Signal/src/Loki/View Controllers/RegisterVC.swift +++ b/Signal/src/Loki/View Controllers/RegisterVC.swift @@ -2,7 +2,8 @@ import Sodium final class RegisterVC : BaseVC { private var seed: Data! { didSet { updateKeyPair() } } - private var keyPair: ECKeyPair! { didSet { updatePublicKeyLabel() } } + private var ed25519KeyPair: Sign.KeyPair! + private var x25519KeyPair: ECKeyPair! { didSet { updatePublicKeyLabel() } } // MARK: Components private lazy var publicKeyLabel: UILabel = { @@ -135,14 +136,14 @@ final class RegisterVC : BaseVC { private func updateKeyPair() { let padding = Data(repeating: 0, count: 16) - let ed25519KeyPair = Sodium().sign.keyPair(seed: (seed + padding).bytes)! + ed25519KeyPair = Sodium().sign.keyPair(seed: (seed + padding).bytes)! let x25519PublicKey = Sodium().sign.toX25519(ed25519PublicKey: ed25519KeyPair.publicKey)! let x25519SecretKey = Sodium().sign.toX25519(ed25519SecretKey: ed25519KeyPair.secretKey)! - keyPair = ECKeyPair(publicKey: Data(x25519PublicKey), privateKey: Data(x25519SecretKey)) + x25519KeyPair = ECKeyPair(publicKey: Data(x25519PublicKey), privateKey: Data(x25519SecretKey)) } private func updatePublicKeyLabel() { - let hexEncodedPublicKey = keyPair.hexEncodedPublicKey + let hexEncodedPublicKey = x25519KeyPair.hexEncodedPublicKey let characterCount = hexEncodedPublicKey.count var count = 0 let limit = 32 @@ -170,11 +171,13 @@ final class RegisterVC : BaseVC { // MARK: Interaction @objc private func register() { - let identityManager = OWSIdentityManager.shared() - let databaseConnection = identityManager.value(forKey: "dbConnection") as! YapDatabaseConnection - databaseConnection.setObject(seed.toHexString(), forKey: "LKLokiSeed", inCollection: OWSPrimaryStorageIdentityKeyStoreCollection) - databaseConnection.setObject(keyPair!, forKey: OWSPrimaryStorageIdentityKeyStoreIdentityKey, inCollection: OWSPrimaryStorageIdentityKeyStoreCollection) - TSAccountManager.sharedInstance().phoneNumberAwaitingVerification = keyPair!.hexEncodedPublicKey + let dbConnection = OWSIdentityManager.shared().dbConnection + let collection = OWSPrimaryStorageIdentityKeyStoreCollection + dbConnection.setObject(seed.toHexString(), forKey: LKSeedKey, inCollection: collection) + dbConnection.setObject(ed25519KeyPair.secretKey.toHexString(), forKey: LKED25519SecretKey, inCollection: collection) + dbConnection.setObject(ed25519KeyPair.publicKey.toHexString(), forKey: LKED25519PublicKey, inCollection: collection) + dbConnection.setObject(x25519KeyPair!, forKey: OWSPrimaryStorageIdentityKeyStoreIdentityKey, inCollection: collection) + TSAccountManager.sharedInstance().phoneNumberAwaitingVerification = x25519KeyPair!.hexEncodedPublicKey OWSPrimaryStorage.shared().setRestorationTime(0) UserDefaults.standard[.hasViewedSeed] = false let displayNameVC = DisplayNameVC() @@ -182,7 +185,7 @@ final class RegisterVC : BaseVC { } @objc private func copyPublicKey() { - UIPasteboard.general.string = keyPair.hexEncodedPublicKey + UIPasteboard.general.string = x25519KeyPair.hexEncodedPublicKey copyPublicKeyButton.isUserInteractionEnabled = false UIView.transition(with: copyPublicKeyButton, duration: 0.25, options: .transitionCrossDissolve, animations: { self.copyPublicKeyButton.setTitle("Copied", for: UIControl.State.normal) diff --git a/SignalServiceKit/src/Messages/OWSIdentityManager.h b/SignalServiceKit/src/Messages/OWSIdentityManager.h index 4eb13b210..2c4631c4c 100644 --- a/SignalServiceKit/src/Messages/OWSIdentityManager.h +++ b/SignalServiceKit/src/Messages/OWSIdentityManager.h @@ -8,6 +8,9 @@ NS_ASSUME_NONNULL_BEGIN extern NSString *const OWSPrimaryStorageIdentityKeyStoreIdentityKey; +extern NSString *const LKSeedKey; +extern NSString *const LKED25519SecretKey; +extern NSString *const LKED25519PublicKey; extern NSString *const OWSPrimaryStorageIdentityKeyStoreCollection; extern NSString *const OWSPrimaryStorageTrustedKeysCollection; @@ -31,6 +34,8 @@ extern const NSUInteger kStoredIdentityKeyLength; // This class can be safely accessed and used from any thread. @interface OWSIdentityManager : NSObject +@property (nonatomic, readonly) YapDatabaseConnection *dbConnection; + - (instancetype)init NS_UNAVAILABLE; - (instancetype)initWithPrimaryStorage:(OWSPrimaryStorage *)primaryStorage NS_DESIGNATED_INITIALIZER; diff --git a/SignalServiceKit/src/Messages/OWSIdentityManager.m b/SignalServiceKit/src/Messages/OWSIdentityManager.m index 910f8c874..463ae79a9 100644 --- a/SignalServiceKit/src/Messages/OWSIdentityManager.m +++ b/SignalServiceKit/src/Messages/OWSIdentityManager.m @@ -33,6 +33,9 @@ NS_ASSUME_NONNULL_BEGIN // Storing our own identity key NSString *const OWSPrimaryStorageIdentityKeyStoreIdentityKey = @"TSStorageManagerIdentityKeyStoreIdentityKey"; +NSString *const LKSeedKey = @"LKLokiSeed"; +NSString *const LKED25519SecretKey = @"LKED25519SecretKey"; +NSString *const LKED25519PublicKey = @"LKED25519PublicKey"; NSString *const OWSPrimaryStorageIdentityKeyStoreCollection = @"TSStorageManagerIdentityKeyStoreCollection"; // Storing recipients identity keys @@ -57,7 +60,6 @@ NSString *const kNSNotificationName_IdentityStateDidChange = @"kNSNotificationNa @interface OWSIdentityManager () @property (nonatomic, readonly) OWSPrimaryStorage *primaryStorage; -@property (nonatomic, readonly) YapDatabaseConnection *dbConnection; @end