From ffd95f94d01167f7fc9a8a00452606cf5fdbadf6 Mon Sep 17 00:00:00 2001 From: Mikunj Date: Thu, 22 Aug 2019 15:33:19 +1000 Subject: [PATCH] Decode token as utf8 string. --- SignalServiceKit/src/Loki/API/LokiGroupChatAPI.swift | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/SignalServiceKit/src/Loki/API/LokiGroupChatAPI.swift b/SignalServiceKit/src/Loki/API/LokiGroupChatAPI.swift index 208ce953a..9c2acd0e3 100644 --- a/SignalServiceKit/src/Loki/API/LokiGroupChatAPI.swift +++ b/SignalServiceKit/src/Loki/API/LokiGroupChatAPI.swift @@ -33,12 +33,16 @@ public final class LokiGroupChatAPI : NSObject { let url = URL(string: "\(serverURL)/loki/v1/get_challenge?pubKey=\(userHexEncodedPublicKey)")! let request = TSRequest(url: url) return TSNetworkManager.shared().makePromise(request: request).map { $0.responseObject }.map { rawResponse in - guard let json = rawResponse as? JSON, let cipherText64 = json["cipherText64"] as? String, let nonce64 = json["nonce64"] as? String, let serverPubKey64 = json["serverPubKey64"] as? String, let cipherText = Data(base64Encoded: cipherText64), let nonce = Data(base64Encoded: nonce64), let serverPubKey = Data(base64Encoded: serverPubKey64) else { throw Error.tokenParsingFailed } + guard let json = rawResponse as? JSON, let cipherText64 = json["cipherText64"] as? String, let serverPubKey64 = json["serverPubKey64"] as? String, let cipherText = Data(base64Encoded: cipherText64), let serverPubKey = Data(base64Encoded: serverPubKey64) else { + throw Error.tokenParsingFailed + } - let ivAndCipher = nonce + cipherText - guard let tokenData = try? DiffieHellman.decrypt(cipherText: ivAndCipher, publicKey: serverPubKey, privateKey: identityKeyPair.privateKey) else { throw Error.tokenDecryptionFailed } + // Cipher text has the 16 bit iv prepended to it + guard let tokenData = try? DiffieHellman.decrypt(cipherText: cipherText, publicKey: serverPubKey, privateKey: identityKeyPair.privateKey), let token = String(bytes: tokenData, encoding: .utf8) else { + throw Error.tokenDecryptionFailed + } - return tokenData.base64EncodedString() + return token } }