Decrypt device names in linked devices views.

pull/1/head
Matthew Chen 6 years ago
parent c113c8e962
commit 0005a33d37

@ -13,6 +13,7 @@
#import "YapDatabaseTransaction.h" #import "YapDatabaseTransaction.h"
#import <Mantle/MTLValueTransformer.h> #import <Mantle/MTLValueTransformer.h>
#import <SignalCoreKit/NSDate+OWS.h> #import <SignalCoreKit/NSDate+OWS.h>
#import <SignalServiceKit/OWSIdentityManager.h>
#import <SignalServiceKit/SignalServiceKit-Swift.h> #import <SignalServiceKit/SignalServiceKit-Swift.h>
NS_ASSUME_NONNULL_BEGIN NS_ASSUME_NONNULL_BEGIN
@ -130,6 +131,13 @@ NSString *const kOWSPrimaryStorage_MayHaveLinkedDevices = @"kTSStorageManager_Ma
return TSAccountManager.sharedInstance; return TSAccountManager.sharedInstance;
} }
- (OWSIdentityManager *)identityManager
{
OWSAssertDebug(SSKEnvironment.shared.identityManager);
return SSKEnvironment.shared.identityManager;
}
#pragma mark - #pragma mark -
- (void)saveWithTransaction:(YapDatabaseReadWriteTransaction *)transaction - (void)saveWithTransaction:(YapDatabaseReadWriteTransaction *)transaction
@ -275,6 +283,20 @@ NSString *const kOWSPrimaryStorage_MayHaveLinkedDevices = @"kTSStorageManager_Ma
- (NSString *)displayName - (NSString *)displayName
{ {
if (self.name) { 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; return self.name;
} }

@ -32,7 +32,7 @@ public class DeviceNames: NSObject {
let masterSecret: Data let masterSecret: Data
do { do {
masterSecret = try Curve25519.generateSharedSecret(fromPublicKey: identityKeyPair.publicKey, masterSecret = try Curve25519.generateSharedSecret(fromPublicKey: identityKeyPair.publicKey,
privateKey: ephemeralKeyPair.privateKey) privateKey: ephemeralKeyPair.privateKey)
} catch { } catch {
Logger.error("Could not generate shared secret: \(error)") Logger.error("Could not generate shared secret: \(error)")
throw error throw error
@ -104,10 +104,22 @@ public class DeviceNames: NSObject {
} }
@objc @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 { 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. // Not necessarily an error; might be a legacy device name.
throw DeviceNameError.invalidInput throw DeviceNameError.invalidInput
} }

@ -31,8 +31,8 @@ class DeviceNamesTest: SSKBaseTestSwift {
} }
do { do {
_ = try DeviceNames.decryptDeviceName(input: plaintextData, _ = try DeviceNames.decryptDeviceName(inputData: plaintextData,
identityKeyPair: identityKeyPair) identityKeyPair: identityKeyPair)
XCTFail("Unexpectedly did not throw error.") XCTFail("Unexpectedly did not throw error.")
} catch { } catch {
// Failure is expected. // Failure is expected.
@ -47,7 +47,7 @@ class DeviceNamesTest: SSKBaseTestSwift {
let encrypted: Data let encrypted: Data
do { do {
encrypted = try DeviceNames.encryptDeviceName(plaintext: plaintext, encrypted = try DeviceNames.encryptDeviceName(plaintext: plaintext,
identityKeyPair: identityKeyPair) identityKeyPair: identityKeyPair)
} catch { } catch {
XCTFail("Failed with error: \(error)") XCTFail("Failed with error: \(error)")
return return
@ -55,8 +55,8 @@ class DeviceNamesTest: SSKBaseTestSwift {
let decrypted: String let decrypted: String
do { do {
decrypted = try DeviceNames.decryptDeviceName(input: encrypted, decrypted = try DeviceNames.decryptDeviceName(inputData: encrypted,
identityKeyPair: identityKeyPair) identityKeyPair: identityKeyPair)
} catch { } catch {
XCTFail("Failed with error: \(error)") XCTFail("Failed with error: \(error)")
return return

Loading…
Cancel
Save