diff --git a/SignalServiceKit/src/Devices/OWSDevice.m b/SignalServiceKit/src/Devices/OWSDevice.m index 00687cd73..b81dca1db 100644 --- a/SignalServiceKit/src/Devices/OWSDevice.m +++ b/SignalServiceKit/src/Devices/OWSDevice.m @@ -13,6 +13,7 @@ #import "YapDatabaseTransaction.h" #import #import +#import #import NS_ASSUME_NONNULL_BEGIN @@ -130,6 +131,13 @@ NSString *const kOWSPrimaryStorage_MayHaveLinkedDevices = @"kTSStorageManager_Ma return TSAccountManager.sharedInstance; } +- (OWSIdentityManager *)identityManager +{ + OWSAssertDebug(SSKEnvironment.shared.identityManager); + + return SSKEnvironment.shared.identityManager; +} + #pragma mark - - (void)saveWithTransaction:(YapDatabaseReadWriteTransaction *)transaction @@ -275,6 +283,20 @@ NSString *const kOWSPrimaryStorage_MayHaveLinkedDevices = @"kTSStorageManager_Ma - (NSString *)displayName { if (self.name) { + ECKeyPair *_Nullable identityKeyPair = self.identityManager.identityKeyPair; + OWSAssertDebug(identityKeyPair); + if (identityKeyPair) { + NSError *error; + NSString *_Nullable decryptedName = + [DeviceNames decryptDeviceNameWithInputString:self.name identityKeyPair:identityKeyPair error:&error]; + if (error) { + // Not necessarily an error; might be a legacy device name. + OWSLogError(@"Could not decrypt device name: %@", error); + } else if (decryptedName) { + return decryptedName; + } + } + return self.name; } diff --git a/SignalServiceKit/src/Util/DeviceNames.swift b/SignalServiceKit/src/Util/DeviceNames.swift index 4a111ce6e..f02307191 100644 --- a/SignalServiceKit/src/Util/DeviceNames.swift +++ b/SignalServiceKit/src/Util/DeviceNames.swift @@ -32,7 +32,7 @@ public class DeviceNames: NSObject { let masterSecret: Data do { masterSecret = try Curve25519.generateSharedSecret(fromPublicKey: identityKeyPair.publicKey, - privateKey: ephemeralKeyPair.privateKey) + privateKey: ephemeralKeyPair.privateKey) } catch { Logger.error("Could not generate shared secret: \(error)") throw error @@ -104,10 +104,22 @@ public class DeviceNames: NSObject { } @objc - public class func decryptDeviceName(input: Data, + public class func decryptDeviceName(inputString: String, + identityKeyPair: ECKeyPair) throws -> String { + guard let inputData = Data(base64Encoded: inputString) else { + // Not necessarily an error; might be a legacy device name. + throw DeviceNameError.invalidInput + } + + return try decryptDeviceName(inputData: inputData, + identityKeyPair: identityKeyPair) + } + + @objc + public class func decryptDeviceName(inputData: Data, identityKeyPair: ECKeyPair) throws -> String { - guard let protoData = Data(base64Encoded: input) else { + guard let protoData = Data(base64Encoded: inputData) else { // Not necessarily an error; might be a legacy device name. throw DeviceNameError.invalidInput } diff --git a/SignalServiceKit/tests/Util/DeviceNamesTest.swift b/SignalServiceKit/tests/Util/DeviceNamesTest.swift index 2e8f8bf73..c62d39a55 100644 --- a/SignalServiceKit/tests/Util/DeviceNamesTest.swift +++ b/SignalServiceKit/tests/Util/DeviceNamesTest.swift @@ -31,8 +31,8 @@ class DeviceNamesTest: SSKBaseTestSwift { } do { - _ = try DeviceNames.decryptDeviceName(input: plaintextData, - identityKeyPair: identityKeyPair) + _ = try DeviceNames.decryptDeviceName(inputData: plaintextData, + identityKeyPair: identityKeyPair) XCTFail("Unexpectedly did not throw error.") } catch { // Failure is expected. @@ -47,7 +47,7 @@ class DeviceNamesTest: SSKBaseTestSwift { let encrypted: Data do { encrypted = try DeviceNames.encryptDeviceName(plaintext: plaintext, - identityKeyPair: identityKeyPair) + identityKeyPair: identityKeyPair) } catch { XCTFail("Failed with error: \(error)") return @@ -55,8 +55,8 @@ class DeviceNamesTest: SSKBaseTestSwift { let decrypted: String do { - decrypted = try DeviceNames.decryptDeviceName(input: encrypted, - identityKeyPair: identityKeyPair) + decrypted = try DeviceNames.decryptDeviceName(inputData: encrypted, + identityKeyPair: identityKeyPair) } catch { XCTFail("Failed with error: \(error)") return