From 31a6ef47698a7e7c4c2f37bada1a18eb1cf2e09d Mon Sep 17 00:00:00 2001 From: Mikunj Date: Thu, 22 Aug 2019 10:58:05 +1000 Subject: [PATCH 01/10] Make SignalServiceKit use 10.0 --- Podfile.lock | 4 ++-- Pods | 2 +- SignalServiceKit.podspec | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Podfile.lock b/Podfile.lock index 2f672862a..79a4f123f 100644 --- a/Podfile.lock +++ b/Podfile.lock @@ -309,7 +309,7 @@ SPEC CHECKSUMS: SAMKeychain: 483e1c9f32984d50ca961e26818a534283b4cd5c SignalCoreKit: c2d8132cdedb95d35eb2f8ae7eac0957695d0a8b SignalMetadataKit: 6fa5e9a53c7f104568662521a2f3874672ff7a02 - SignalServiceKit: d705b3264177c5d4162af9d266c9ca6af7181191 + SignalServiceKit: 5c5b63a39d5054201ab59ef6daf0fa0a1a0c7887 SQLCipher: efbdb52cdbe340bcd892b1b14297df4e07241b7f SSZipArchive: 8e859da2520142e09166bc9161967db296e9d02f Starscream: ef3ece99d765eeccb67de105bfa143f929026cf5 @@ -317,6 +317,6 @@ SPEC CHECKSUMS: YapDatabase: b418a4baa6906e8028748938f9159807fd039af4 YYImage: 1e1b62a9997399593e4b9c4ecfbbabbf1d3f3b54 -PODFILE CHECKSUM: d7c222f400892f5c056cb2ec3517da1ac1e10238 +PODFILE CHECKSUM: 10152a1fffafd51206b62fdd8cac86a5de8cf083 COCOAPODS: 1.7.2 diff --git a/Pods b/Pods index c30c8edc5..40bc969de 160000 --- a/Pods +++ b/Pods @@ -1 +1 @@ -Subproject commit c30c8edc5df09744e7091da12c2839762cb580ae +Subproject commit 40bc969de75f70d546256db090dec25fe4661120 diff --git a/SignalServiceKit.podspec b/SignalServiceKit.podspec index 7e4126d02..e4b5c150d 100644 --- a/SignalServiceKit.podspec +++ b/SignalServiceKit.podspec @@ -21,7 +21,7 @@ An Objective-C library for communicating with the Signal messaging service. s.source = { :git => "https://github.com/signalapp/SignalServiceKit.git", :tag => s.version.to_s } s.social_media_url = 'https://twitter.com/FredericJacobs' - s.platform = :ios, '9.0' + s.platform = :ios, '10.0' #s.ios.deployment_target = '9.0' #s.osx.deployment_target = '10.9' s.requires_arc = true From c5b9d8c7e252f05838b9a563eb012b924cc462c0 Mon Sep 17 00:00:00 2001 From: Mikunj Date: Thu, 22 Aug 2019 11:09:51 +1000 Subject: [PATCH 02/10] Added DiffieHellman class --- Pods | 2 +- Signal/Signal-Info.plist | 6 +-- .../src/Loki/Crypto/DiffieHellman.swift | 43 +++++++++++++++++++ 3 files changed, 45 insertions(+), 6 deletions(-) create mode 100644 SignalServiceKit/src/Loki/Crypto/DiffieHellman.swift diff --git a/Pods b/Pods index 40bc969de..d2cc7e97d 160000 --- a/Pods +++ b/Pods @@ -1 +1 @@ -Subproject commit 40bc969de75f70d546256db090dec25fe4661120 +Subproject commit d2cc7e97d653ab30ffadedd8034f6d8d27c27775 diff --git a/Signal/Signal-Info.plist b/Signal/Signal-Info.plist index ea98f085f..4de2cfbc6 100644 --- a/Signal/Signal-Info.plist +++ b/Signal/Signal-Info.plist @@ -6,14 +6,10 @@ CarthageVersion 0.33.0 - DateTime - Thu Jul 18 04:53:39 UTC 2019 OSXVersion - 10.14.5 + 10.14.6 WebRTCCommit 1445d719bf05280270e9f77576f80f973fd847f8 M73 - XCodeVersion - 1000.1020 CFBundleDevelopmentRegion en diff --git a/SignalServiceKit/src/Loki/Crypto/DiffieHellman.swift b/SignalServiceKit/src/Loki/Crypto/DiffieHellman.swift new file mode 100644 index 000000000..4c6bbbb50 --- /dev/null +++ b/SignalServiceKit/src/Loki/Crypto/DiffieHellman.swift @@ -0,0 +1,43 @@ +import CryptoSwift +import Curve25519Kit + +public enum DiffieHellman { + // The length of the iv + public static let ivLength: Int32 = 16; + + public static func encrypt(plainText: Data, symmetricKey: Data) throws -> Data { + let iv = Randomness.generateRandomBytes(ivLength)! + let ivBytes = [UInt8](iv) + + let symmetricKeyBytes = [UInt8](symmetricKey) + let messageBytes = [UInt8](plainText) + + let blockMode = CBC(iv: ivBytes) + let aes = try AES(key: symmetricKeyBytes, blockMode: blockMode) + let cipherText = try aes.encrypt(messageBytes) + let ivAndCipher = ivBytes + cipherText + return Data(bytes: ivAndCipher, count: ivAndCipher.count) + } + + public static func encrypt(plainText: Data, publicKey: Data, privateKey: Data) throws -> Data { + let symmetricKey = try Curve25519.generateSharedSecret(fromPublicKey: publicKey, privateKey: privateKey) + return try encrypt(plainText: plainText, symmetricKey: symmetricKey) + } + + public static func decrypt(cipherText: Data, symmetricKey: Data) throws -> Data { + let symmetricKeyBytes = [UInt8](symmetricKey) + let ivBytes = [UInt8](cipherText[.. Data { + let symmetricKey = try Curve25519.generateSharedSecret(fromPublicKey: publicKey, privateKey: privateKey) + return try decrypt(cipherText: cipherText, symmetricKey: symmetricKey) + } +} From 130ed6c371a961ad46d957995fbefcf4b9a54ac4 Mon Sep 17 00:00:00 2001 From: Mikunj Date: Thu, 22 Aug 2019 12:34:24 +1000 Subject: [PATCH 03/10] Added function for submitting tokens. --- .../src/Loki/API/LokiGroupChatAPI.swift | 32 ++++++++++++++----- .../Loki/Crypto/FallbackSessionCipher.swift | 32 ++----------------- 2 files changed, 26 insertions(+), 38 deletions(-) diff --git a/SignalServiceKit/src/Loki/API/LokiGroupChatAPI.swift b/SignalServiceKit/src/Loki/API/LokiGroupChatAPI.swift index 5309235d9..b2b73a98f 100644 --- a/SignalServiceKit/src/Loki/API/LokiGroupChatAPI.swift +++ b/SignalServiceKit/src/Loki/API/LokiGroupChatAPI.swift @@ -13,25 +13,41 @@ public final class LokiGroupChatAPI : NSObject { return SSKEnvironment.shared.contactsManager.displayName(forPhoneIdentifier: userHexEncodedPublicKey) ?? "Anonymous" } + internal static var identityKeyPair: ECKeyPair { + return OWSIdentityManager.shared().identityKeyPair()! + } + private static var userHexEncodedPublicKey: String { - return OWSIdentityManager.shared().identityKeyPair()!.hexEncodedPublicKey + return identityKeyPair.hexEncodedPublicKey } + public enum Error : Swift.Error { - case tokenParsingFailed, messageParsingFailed + case tokenParsingFailed, tokenDecryptionFailed, messageParsingFailed } - public static func getEncryptedToken() -> Promise { + public static func fetchToken() -> Promise { print("[Loki] Getting group chat auth token.") - let url = URL(string: "\(serverURL)/loki/v1/getToken")! - let parameters = [ "pubKey" : userHexEncodedPublicKey ] - let request = TSRequest(url: url, method: "POST", parameters: parameters) + 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 encryptedToken = json["cipherText64"] as? String else { throw Error.tokenParsingFailed } - return encryptedToken + 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 } + + let ivAndCipher = nonce + cipherText + guard let tokenData = try? DiffieHellman.decrypt(cipherText: ivAndCipher, publicKey: serverPubKey, privateKey: identityKeyPair.privateKey) else { throw Error.tokenDecryptionFailed } + + return tokenData.base64EncodedString() } } + public static func submitToken(_ token: String) -> Promise { + print("[Loki] Submitting group chat auth token.") + let url = URL(string: "\(serverURL)/loki/v1/submit_challenge")! + let parameters = [ "pubKey" : userHexEncodedPublicKey, "token" : token ] + let request = TSRequest(url: url, method: "POST", parameters: parameters) + return TSNetworkManager.shared().makePromise(request: request).asVoid() + } + public static func getMessages(for group: UInt) -> Promise<[LokiGroupMessage]> { print("[Loki] Getting messages for group chat with ID: \(group).") let queryParameters = "include_annotations=1&count=-\(batchCount)" diff --git a/SignalServiceKit/src/Loki/Crypto/FallbackSessionCipher.swift b/SignalServiceKit/src/Loki/Crypto/FallbackSessionCipher.swift index 336bdc4a6..e1d92c587 100644 --- a/SignalServiceKit/src/Loki/Crypto/FallbackSessionCipher.swift +++ b/SignalServiceKit/src/Loki/Crypto/FallbackSessionCipher.swift @@ -72,7 +72,7 @@ private extension String { @objc public func encrypt(message: Data) -> Data? { guard let symmetricKey = symmetricKey else { return nil } do { - return try diffieHellmanEncrypt(plainText: message, symmetricKey: symmetricKey) + return try DiffieHellman.encrypt(plainText: message, symmetricKey: symmetricKey) } catch { Logger.warn("FallBackSessionCipher: Failed to encrypt message") return nil @@ -86,38 +86,10 @@ private extension String { @objc public func decrypt(message: Data) -> Data? { guard let symmetricKey = symmetricKey else { return nil } do { - return try diffieHellmanDecrypt(cipherText: message, symmetricKey: symmetricKey) + return try DiffieHellman.decrypt(cipherText: message, symmetricKey: symmetricKey) } catch { Logger.warn("FallBackSessionCipher: Failed to decrypt message") return nil } } - - // Encypt the message with the symmetric key and a 16 bit iv - private func diffieHellmanEncrypt(plainText: Data, symmetricKey: Data) throws -> Data { - let iv = Randomness.generateRandomBytes(ivLength)! - let ivBytes = [UInt8](iv) - - let symmetricKeyBytes = [UInt8](symmetricKey) - let messageBytes = [UInt8](plainText) - - let blockMode = CBC(iv: ivBytes) - let aes = try AES(key: symmetricKeyBytes, blockMode: blockMode) - let cipherText = try aes.encrypt(messageBytes) - let ivAndCipher = ivBytes + cipherText - return Data(bytes: ivAndCipher, count: ivAndCipher.count) - } - - // Decrypt the message with the symmetric key - private func diffieHellmanDecrypt(cipherText: Data, symmetricKey: Data) throws -> Data { - let symmetricKeyBytes = [UInt8](symmetricKey) - let ivBytes = [UInt8](cipherText[.. Date: Thu, 22 Aug 2019 13:14:35 +1000 Subject: [PATCH 04/10] Added group chat authentication. --- .../src/Loki/API/LokiGroupChatAPI.swift | 44 ++++++++++++------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/SignalServiceKit/src/Loki/API/LokiGroupChatAPI.swift b/SignalServiceKit/src/Loki/API/LokiGroupChatAPI.swift index b2b73a98f..208ce953a 100644 --- a/SignalServiceKit/src/Loki/API/LokiGroupChatAPI.swift +++ b/SignalServiceKit/src/Loki/API/LokiGroupChatAPI.swift @@ -9,6 +9,8 @@ public final class LokiGroupChatAPI : NSObject { @objc public static let publicChatMessageType = "network.loki.messenger.publicChat" @objc public static let publicChatID = 1 + private static let tokenCollection = "LokiGroupChatTokenCollection" + internal static var userDisplayName: String { return SSKEnvironment.shared.contactsManager.displayName(forPhoneIdentifier: userHexEncodedPublicKey) ?? "Anonymous" } @@ -26,7 +28,7 @@ public final class LokiGroupChatAPI : NSObject { case tokenParsingFailed, tokenDecryptionFailed, messageParsingFailed } - public static func fetchToken() -> Promise { + internal static func fetchToken() -> Promise { print("[Loki] Getting group chat auth token.") let url = URL(string: "\(serverURL)/loki/v1/get_challenge?pubKey=\(userHexEncodedPublicKey)")! let request = TSRequest(url: url) @@ -40,12 +42,22 @@ public final class LokiGroupChatAPI : NSObject { } } - public static func submitToken(_ token: String) -> Promise { + internal static func submitToken(_ token: String) -> Promise { print("[Loki] Submitting group chat auth token.") let url = URL(string: "\(serverURL)/loki/v1/submit_challenge")! let parameters = [ "pubKey" : userHexEncodedPublicKey, "token" : token ] let request = TSRequest(url: url, method: "POST", parameters: parameters) - return TSNetworkManager.shared().makePromise(request: request).asVoid() + return TSNetworkManager.shared().makePromise(request: request).map { _ in token } + } + + internal static func getToken() -> Promise { + guard let token = storage.dbReadConnection.string(forKey: serverURL, inCollection: tokenCollection) else { + return fetchToken().then { submitToken($0) }.map { token -> String in + storage.dbReadWriteConnection.setObject(token, forKey: serverURL, inCollection: tokenCollection) + return token + } + } + return Promise.value(token) } public static func getMessages(for group: UInt) -> Promise<[LokiGroupMessage]> { @@ -71,19 +83,21 @@ public final class LokiGroupChatAPI : NSObject { } public static func sendMessage(_ message: LokiGroupMessage, to group: UInt) -> Promise { - print("[Loki] Sending message to group chat with ID: \(group).") - let url = URL(string: "\(serverURL)/channels/\(group)/messages")! - let parameters = message.toJSON() - let request = TSRequest(url: url, method: "POST", parameters: parameters) - request.allHTTPHeaderFields = [ "Content-Type" : "application/json", "Authorization" : "Bearer loki" ] - let displayName = userDisplayName - return TSNetworkManager.shared().makePromise(request: request).map { $0.responseObject }.map { rawResponse in - guard let json = rawResponse as? JSON, let message = json["data"] as? JSON, let serverID = message["id"] as? UInt, let body = message["text"] as? String, let dateAsString = message["created_at"] as? String, let date = ISO8601DateFormatter().date(from: dateAsString) else { - print("[Loki] Couldn't parse messages for group chat with ID: \(group) from: \(rawResponse).") - throw Error.messageParsingFailed + return getToken().then { token -> Promise in + print("[Loki] Sending message to group chat with ID: \(group).") + let url = URL(string: "\(serverURL)/channels/\(group)/messages")! + let parameters = message.toJSON() + let request = TSRequest(url: url, method: "POST", parameters: parameters) + request.allHTTPHeaderFields = [ "Content-Type" : "application/json", "Authorization" : "Bearer \(token)" ] + let displayName = userDisplayName + return TSNetworkManager.shared().makePromise(request: request).map { $0.responseObject }.map { rawResponse in + guard let json = rawResponse as? JSON, let message = json["data"] as? JSON, let serverID = message["id"] as? UInt, let body = message["text"] as? String, let dateAsString = message["created_at"] as? String, let date = ISO8601DateFormatter().date(from: dateAsString) else { + print("[Loki] Couldn't parse messages for group chat with ID: \(group) from: \(rawResponse).") + throw Error.messageParsingFailed + } + let timestamp = UInt64(date.timeIntervalSince1970) * 1000 + return LokiGroupMessage(serverID: serverID, hexEncodedPublicKey: userHexEncodedPublicKey, displayName: displayName, body: body, type: publicChatMessageType, timestamp: timestamp) } - let timestamp = UInt64(date.timeIntervalSince1970) * 1000 - return LokiGroupMessage(serverID: serverID, hexEncodedPublicKey: userHexEncodedPublicKey, displayName: displayName, body: body, type: publicChatMessageType, timestamp: timestamp) } } From ffd95f94d01167f7fc9a8a00452606cf5fdbadf6 Mon Sep 17 00:00:00 2001 From: Mikunj Date: Thu, 22 Aug 2019 15:33:19 +1000 Subject: [PATCH 05/10] 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 } } From 39177fe448fddbe7d7eac48f3b9d8e55097b7f9d Mon Sep 17 00:00:00 2001 From: Mikunj Date: Thu, 22 Aug 2019 15:54:38 +1000 Subject: [PATCH 06/10] Ensure we have a valid token length. Disable typing indicator messages on group threads. --- SignalServiceKit/src/Loki/API/LokiGroupChatAPI.swift | 2 +- SignalServiceKit/src/Util/TypingIndicators.swift | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/SignalServiceKit/src/Loki/API/LokiGroupChatAPI.swift b/SignalServiceKit/src/Loki/API/LokiGroupChatAPI.swift index 9c2acd0e3..5de157785 100644 --- a/SignalServiceKit/src/Loki/API/LokiGroupChatAPI.swift +++ b/SignalServiceKit/src/Loki/API/LokiGroupChatAPI.swift @@ -55,7 +55,7 @@ public final class LokiGroupChatAPI : NSObject { } internal static func getToken() -> Promise { - guard let token = storage.dbReadConnection.string(forKey: serverURL, inCollection: tokenCollection) else { + guard let token = storage.dbReadConnection.string(forKey: serverURL, inCollection: tokenCollection), token.count > 0 else { return fetchToken().then { submitToken($0) }.map { token -> String in storage.dbReadWriteConnection.setObject(token, forKey: serverURL, inCollection: tokenCollection) return token diff --git a/SignalServiceKit/src/Util/TypingIndicators.swift b/SignalServiceKit/src/Util/TypingIndicators.swift index f148a7555..67f49cc7f 100644 --- a/SignalServiceKit/src/Util/TypingIndicators.swift +++ b/SignalServiceKit/src/Util/TypingIndicators.swift @@ -322,6 +322,11 @@ public class TypingIndicatorsImpl: NSObject, TypingIndicators { guard delegate.areTypingIndicatorsEnabled() else { return } + + // Disable typing indicators on public group chats + guard !thread.isGroupThread() else { + return + } let message = TypingIndicatorMessage(thread: thread, action: action) messageSender.sendPromise(message: message).retainUntilComplete() From 6463187c0dd291eeaec00b9c1327366994822c0e Mon Sep 17 00:00:00 2001 From: Mikunj Date: Thu, 22 Aug 2019 16:00:35 +1000 Subject: [PATCH 07/10] Disable read receipts for groups. --- SignalServiceKit/src/Messages/OWSOutgoingReceiptManager.m | 3 +++ 1 file changed, 3 insertions(+) diff --git a/SignalServiceKit/src/Messages/OWSOutgoingReceiptManager.m b/SignalServiceKit/src/Messages/OWSOutgoingReceiptManager.m index 1eeab9089..35b330d3b 100644 --- a/SignalServiceKit/src/Messages/OWSOutgoingReceiptManager.m +++ b/SignalServiceKit/src/Messages/OWSOutgoingReceiptManager.m @@ -177,6 +177,9 @@ NSString *const kOutgoingReadReceiptManagerCollection = @"kOutgoingReadReceiptMa // If we aren't friends with the user then don't send out any receipts if (thread.friendRequestStatus != LKThreadFriendRequestStatusFriends) { continue; } + // Don't send any receipts for groups + if (thread.isGroupThread) { continue; } + OWSReceiptsForSenderMessage *message; NSString *receiptName; switch (receiptType) { From d7ada7566037b2695da06f8fcfc99b00955c81e2 Mon Sep 17 00:00:00 2001 From: Mikunj Date: Fri, 23 Aug 2019 10:27:22 +1000 Subject: [PATCH 08/10] Fix date not being parsed properly. Fix message indicators on groups. Fix receipts being sent in groups. --- Signal/src/AppDelegate.m | 2 +- .../src/Loki/API/LokiGroupChatAPI.swift | 16 +++++++++++++--- .../src/Messages/OWSReadReceiptManager.m | 5 ++++- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/Signal/src/AppDelegate.m b/Signal/src/AppDelegate.m index f29d76ebb..29ba4f2d5 100644 --- a/Signal/src/AppDelegate.m +++ b/Signal/src/AppDelegate.m @@ -1493,7 +1493,7 @@ static NSTimeInterval launchStartedAt; if (isPublicChatSetUp) { return; } NSString *title = NSLocalizedString(@"Loki Public Chat", @""); NSData *groupID = [[[LKGroupChatAPI.serverURL stringByAppendingString:@"."] stringByAppendingString:@(LKGroupChatAPI.publicChatID).stringValue] dataUsingEncoding:NSUTF8StringEncoding]; - TSGroupModel *group = [[TSGroupModel alloc] initWithTitle:title memberIds:@[ OWSIdentityManager.sharedManager.identityKeyPair.hexEncodedPublicKey ] image:nil groupId:groupID]; + TSGroupModel *group = [[TSGroupModel alloc] initWithTitle:title memberIds:@[ OWSIdentityManager.sharedManager.identityKeyPair.hexEncodedPublicKey, LKGroupChatAPI.serverURL ] image:nil groupId:groupID]; __block TSGroupThread *thread; [OWSPrimaryStorage.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { thread = [TSGroupThread getOrCreateThreadWithGroupModel:group transaction:transaction]; diff --git a/SignalServiceKit/src/Loki/API/LokiGroupChatAPI.swift b/SignalServiceKit/src/Loki/API/LokiGroupChatAPI.swift index 5de157785..e99e69eae 100644 --- a/SignalServiceKit/src/Loki/API/LokiGroupChatAPI.swift +++ b/SignalServiceKit/src/Loki/API/LokiGroupChatAPI.swift @@ -33,12 +33,18 @@ 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 serverPubKey64 = json["serverPubKey64"] as? String, let cipherText = Data(base64Encoded: cipherText64), let serverPubKey = Data(base64Encoded: serverPubKey64) else { + guard let json = rawResponse as? JSON, let cipherText64 = json["cipherText64"] as? String, let serverPubKey64 = json["serverPubKey64"] as? String, let cipherText = Data(base64Encoded: cipherText64), var serverPubKey = Data(base64Encoded: serverPubKey64) else { throw Error.tokenParsingFailed } + // If we have length 33 then the pubkey is prefixed with 05, so we need to remove a byte + if (serverPubKey.count == 33) { + let hex = serverPubKey.hexadecimalString + serverPubKey = Data.data(fromHex: hex.substring(from: 2))! + } + // 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 { + guard let tokenData = try? DiffieHellman.decrypt(cipherText: cipherText, publicKey: serverPubKey, privateKey: identityKeyPair.privateKey), let token = String(bytes: tokenData, encoding: .utf8), token.count > 0 else { throw Error.tokenDecryptionFailed } @@ -95,7 +101,11 @@ public final class LokiGroupChatAPI : NSObject { request.allHTTPHeaderFields = [ "Content-Type" : "application/json", "Authorization" : "Bearer \(token)" ] let displayName = userDisplayName return TSNetworkManager.shared().makePromise(request: request).map { $0.responseObject }.map { rawResponse in - guard let json = rawResponse as? JSON, let message = json["data"] as? JSON, let serverID = message["id"] as? UInt, let body = message["text"] as? String, let dateAsString = message["created_at"] as? String, let date = ISO8601DateFormatter().date(from: dateAsString) else { + // ISO8601DateFormatter doesn't support milliseconds for iOS 10 and below + let dateFormatter = DateFormatter() + dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ" + + guard let json = rawResponse as? JSON, let message = json["data"] as? JSON, let serverID = message["id"] as? UInt, let body = message["text"] as? String, let dateAsString = message["created_at"] as? String, let date = dateFormatter.date(from: dateAsString) else { print("[Loki] Couldn't parse messages for group chat with ID: \(group) from: \(rawResponse).") throw Error.messageParsingFailed } diff --git a/SignalServiceKit/src/Messages/OWSReadReceiptManager.m b/SignalServiceKit/src/Messages/OWSReadReceiptManager.m index e08fe371f..779d319fe 100644 --- a/SignalServiceKit/src/Messages/OWSReadReceiptManager.m +++ b/SignalServiceKit/src/Messages/OWSReadReceiptManager.m @@ -290,7 +290,10 @@ NSString *const OWSReadReceiptManagerAreReadReceiptsEnabled = @"areReadReceiptsE OWSLogVerbose(@"Ignoring read receipt for self-sender."); return; } - + + // Don't enqueue message for group threads + if (message.thread.isGroupThread) { return; } + if ([self areReadReceiptsEnabled]) { OWSLogVerbose(@"Enqueuing read receipt for sender."); [self.outgoingReceiptManager enqueueReadReceiptForEnvelope:messageAuthorId timestamp:message.timestamp]; From d32b45f7925c5e6b4d8f3cb033d98d6481731f5c Mon Sep 17 00:00:00 2001 From: Niels Andriesse Date: Fri, 23 Aug 2019 16:14:19 +1000 Subject: [PATCH 09/10] Clean --- .../src/Loki/API/LokiGroupChatAPI.swift | 53 ++++++++----------- .../src/Loki/Crypto/DiffieHellman.swift | 24 ++++----- .../Loki/Crypto/FallbackSessionCipher.swift | 4 +- .../src/Messages/OWSReadReceiptManager.m | 2 +- .../src/Util/TypingIndicators.swift | 6 +-- 5 files changed, 37 insertions(+), 52 deletions(-) diff --git a/SignalServiceKit/src/Loki/API/LokiGroupChatAPI.swift b/SignalServiceKit/src/Loki/API/LokiGroupChatAPI.swift index e99e69eae..92a7e4cd8 100644 --- a/SignalServiceKit/src/Loki/API/LokiGroupChatAPI.swift +++ b/SignalServiceKit/src/Loki/API/LokiGroupChatAPI.swift @@ -8,46 +8,35 @@ public final class LokiGroupChatAPI : NSObject { private static let batchCount = 8 @objc public static let publicChatMessageType = "network.loki.messenger.publicChat" @objc public static let publicChatID = 1 - private static let tokenCollection = "LokiGroupChatTokenCollection" - internal static var userDisplayName: String { - return SSKEnvironment.shared.contactsManager.displayName(forPhoneIdentifier: userHexEncodedPublicKey) ?? "Anonymous" - } - - internal static var identityKeyPair: ECKeyPair { - return OWSIdentityManager.shared().identityKeyPair()! - } - - private static var userHexEncodedPublicKey: String { - return identityKeyPair.hexEncodedPublicKey - } - + internal static var userDisplayName: String { return SSKEnvironment.shared.contactsManager.displayName(forPhoneIdentifier: userHexEncodedPublicKey) ?? "Anonymous" } + private static var userKeyPair: ECKeyPair { return OWSIdentityManager.shared().identityKeyPair()! } + private static var userHexEncodedPublicKey: String { return userKeyPair.hexEncodedPublicKey } public enum Error : Swift.Error { case tokenParsingFailed, tokenDecryptionFailed, messageParsingFailed } - internal static func fetchToken() -> Promise { + private static func getTokenFromServer() -> Promise { print("[Loki] Getting group chat auth token.") 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 serverPubKey64 = json["serverPubKey64"] as? String, let cipherText = Data(base64Encoded: cipherText64), var serverPubKey = Data(base64Encoded: serverPubKey64) else { + guard let json = rawResponse as? JSON, let base64EncodedChallenge = json["cipherText64"] as? String, let base64EncodedServerPublicKey = json["serverPubKey64"] as? String, + let challenge = Data(base64Encoded: base64EncodedChallenge), var serverPublicKey = Data(base64Encoded: base64EncodedServerPublicKey) else { throw Error.tokenParsingFailed } - - // If we have length 33 then the pubkey is prefixed with 05, so we need to remove a byte - if (serverPubKey.count == 33) { - let hex = serverPubKey.hexadecimalString - serverPubKey = Data.data(fromHex: hex.substring(from: 2))! + // Discard the "05" prefix if needed + if (serverPublicKey.count == 33) { + let hexEncodedServerPublicKey = serverPublicKey.hexadecimalString + serverPublicKey = Data.data(fromHex: hexEncodedServerPublicKey.substring(from: 2))! } - - // 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), token.count > 0 else { + // The challenge is prefixed by the 16 bit IV + guard let tokenAsData = try? DiffieHellman.decrypt(challenge, publicKey: serverPublicKey, privateKey: userKeyPair.privateKey), + let token = String(bytes: tokenAsData, encoding: .utf8), token.count > 0 else { throw Error.tokenDecryptionFailed } - return token } } @@ -61,13 +50,14 @@ public final class LokiGroupChatAPI : NSObject { } internal static func getToken() -> Promise { - guard let token = storage.dbReadConnection.string(forKey: serverURL, inCollection: tokenCollection), token.count > 0 else { - return fetchToken().then { submitToken($0) }.map { token -> String in + if let token = storage.dbReadConnection.string(forKey: serverURL, inCollection: tokenCollection), token.count > 0 { + return Promise.value(token) + } else { + return getTokenFromServer().then { submitToken($0) }.map { token -> String in storage.dbReadWriteConnection.setObject(token, forKey: serverURL, inCollection: tokenCollection) return token } } - return Promise.value(token) } public static func getMessages(for group: UInt) -> Promise<[LokiGroupMessage]> { @@ -82,7 +72,8 @@ public final class LokiGroupChatAPI : NSObject { } return rawMessages.flatMap { message in guard let annotations = message["annotations"] as? [JSON], let annotation = annotations.first, let value = annotation["value"] as? JSON, - let serverID = message["id"] as? UInt, let body = message["text"] as? String, let hexEncodedPublicKey = value["source"] as? String, let displayName = value["from"] as? String, let timestamp = value["timestamp"] as? UInt64 else { + let serverID = message["id"] as? UInt, let body = message["text"] as? String, let hexEncodedPublicKey = value["source"] as? String, let displayName = value["from"] as? String, + let timestamp = value["timestamp"] as? UInt64 else { print("[Loki] Couldn't parse message for group chat with ID: \(group) from: \(message).") return nil } @@ -101,11 +92,11 @@ public final class LokiGroupChatAPI : NSObject { request.allHTTPHeaderFields = [ "Content-Type" : "application/json", "Authorization" : "Bearer \(token)" ] let displayName = userDisplayName return TSNetworkManager.shared().makePromise(request: request).map { $0.responseObject }.map { rawResponse in - // ISO8601DateFormatter doesn't support milliseconds for iOS 10 and below + // ISO8601DateFormatter doesn't support milliseconds before iOS 11 let dateFormatter = DateFormatter() dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ" - - guard let json = rawResponse as? JSON, let message = json["data"] as? JSON, let serverID = message["id"] as? UInt, let body = message["text"] as? String, let dateAsString = message["created_at"] as? String, let date = dateFormatter.date(from: dateAsString) else { + guard let json = rawResponse as? JSON, let message = json["data"] as? JSON, let serverID = message["id"] as? UInt, let body = message["text"] as? String, + let dateAsString = message["created_at"] as? String, let date = dateFormatter.date(from: dateAsString) else { print("[Loki] Couldn't parse messages for group chat with ID: \(group) from: \(rawResponse).") throw Error.messageParsingFailed } diff --git a/SignalServiceKit/src/Loki/Crypto/DiffieHellman.swift b/SignalServiceKit/src/Loki/Crypto/DiffieHellman.swift index 4c6bbbb50..874a698e3 100644 --- a/SignalServiceKit/src/Loki/Crypto/DiffieHellman.swift +++ b/SignalServiceKit/src/Loki/Crypto/DiffieHellman.swift @@ -2,16 +2,14 @@ import CryptoSwift import Curve25519Kit public enum DiffieHellman { - // The length of the iv + public static let ivLength: Int32 = 16; - public static func encrypt(plainText: Data, symmetricKey: Data) throws -> Data { + public static func encrypt(_ plainTextData: Data, using symmetricKey: Data) throws -> Data { let iv = Randomness.generateRandomBytes(ivLength)! let ivBytes = [UInt8](iv) - let symmetricKeyBytes = [UInt8](symmetricKey) - let messageBytes = [UInt8](plainText) - + let messageBytes = [UInt8](plainTextData) let blockMode = CBC(iv: ivBytes) let aes = try AES(key: symmetricKeyBytes, blockMode: blockMode) let cipherText = try aes.encrypt(messageBytes) @@ -19,25 +17,23 @@ public enum DiffieHellman { return Data(bytes: ivAndCipher, count: ivAndCipher.count) } - public static func encrypt(plainText: Data, publicKey: Data, privateKey: Data) throws -> Data { + public static func encrypt(_ plainTextData: Data, publicKey: Data, privateKey: Data) throws -> Data { let symmetricKey = try Curve25519.generateSharedSecret(fromPublicKey: publicKey, privateKey: privateKey) - return try encrypt(plainText: plainText, symmetricKey: symmetricKey) + return try encrypt(plainTextData, using: symmetricKey) } - public static func decrypt(cipherText: Data, symmetricKey: Data) throws -> Data { + public static func decrypt(_ encryptedData: Data, using symmetricKey: Data) throws -> Data { let symmetricKeyBytes = [UInt8](symmetricKey) - let ivBytes = [UInt8](cipherText[.. Data { + public static func decrypt(_ encryptedData: Data, publicKey: Data, privateKey: Data) throws -> Data { let symmetricKey = try Curve25519.generateSharedSecret(fromPublicKey: publicKey, privateKey: privateKey) - return try decrypt(cipherText: cipherText, symmetricKey: symmetricKey) + return try decrypt(encryptedData, using: symmetricKey) } } diff --git a/SignalServiceKit/src/Loki/Crypto/FallbackSessionCipher.swift b/SignalServiceKit/src/Loki/Crypto/FallbackSessionCipher.swift index e1d92c587..bb3f77dff 100644 --- a/SignalServiceKit/src/Loki/Crypto/FallbackSessionCipher.swift +++ b/SignalServiceKit/src/Loki/Crypto/FallbackSessionCipher.swift @@ -72,7 +72,7 @@ private extension String { @objc public func encrypt(message: Data) -> Data? { guard let symmetricKey = symmetricKey else { return nil } do { - return try DiffieHellman.encrypt(plainText: message, symmetricKey: symmetricKey) + return try DiffieHellman.encrypt(message, using: symmetricKey) } catch { Logger.warn("FallBackSessionCipher: Failed to encrypt message") return nil @@ -86,7 +86,7 @@ private extension String { @objc public func decrypt(message: Data) -> Data? { guard let symmetricKey = symmetricKey else { return nil } do { - return try DiffieHellman.decrypt(cipherText: message, symmetricKey: symmetricKey) + return try DiffieHellman.decrypt(message, using: symmetricKey) } catch { Logger.warn("FallBackSessionCipher: Failed to decrypt message") return nil diff --git a/SignalServiceKit/src/Messages/OWSReadReceiptManager.m b/SignalServiceKit/src/Messages/OWSReadReceiptManager.m index 779d319fe..b6a66f261 100644 --- a/SignalServiceKit/src/Messages/OWSReadReceiptManager.m +++ b/SignalServiceKit/src/Messages/OWSReadReceiptManager.m @@ -291,7 +291,7 @@ NSString *const OWSReadReceiptManagerAreReadReceiptsEnabled = @"areReadReceiptsE return; } - // Don't enqueue message for group threads + // Don't send any receipts for groups if (message.thread.isGroupThread) { return; } if ([self areReadReceiptsEnabled]) { diff --git a/SignalServiceKit/src/Util/TypingIndicators.swift b/SignalServiceKit/src/Util/TypingIndicators.swift index 67f49cc7f..fe9f53fc8 100644 --- a/SignalServiceKit/src/Util/TypingIndicators.swift +++ b/SignalServiceKit/src/Util/TypingIndicators.swift @@ -323,10 +323,8 @@ public class TypingIndicatorsImpl: NSObject, TypingIndicators { return } - // Disable typing indicators on public group chats - guard !thread.isGroupThread() else { - return - } + // Don't send any typing indicators for groups + guard !thread.isGroupThread() else { return } let message = TypingIndicatorMessage(thread: thread, action: action) messageSender.sendPromise(message: message).retainUntilComplete() From 019265f6254fd018ee7b9db8a38483e337ee804e Mon Sep 17 00:00:00 2001 From: Niels Andriesse Date: Fri, 23 Aug 2019 16:14:46 +1000 Subject: [PATCH 10/10] Update Pods --- Pods | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Pods b/Pods index d2cc7e97d..d9ab8b130 160000 --- a/Pods +++ b/Pods @@ -1 +1 @@ -Subproject commit d2cc7e97d653ab30ffadedd8034f6d8d27c27775 +Subproject commit d9ab8b13002bf6ebc932ca4f45df56b577b6a188