From b1d9cb0cf9e617f0de31cfcce33b00330351671e Mon Sep 17 00:00:00 2001 From: nielsandriesse Date: Thu, 1 Oct 2020 13:46:13 +1000 Subject: [PATCH] Debug SSKs further --- .../Closed Groups/ClosedGroupsProtocol.swift | 15 +++------------ .../SharedSenderKeysImplementation.swift | 7 ++++++- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/SignalServiceKit/src/Loki/Protocol/Closed Groups/ClosedGroupsProtocol.swift b/SignalServiceKit/src/Loki/Protocol/Closed Groups/ClosedGroupsProtocol.swift index cd0e075c8..8d6275da6 100644 --- a/SignalServiceKit/src/Loki/Protocol/Closed Groups/ClosedGroupsProtocol.swift +++ b/SignalServiceKit/src/Loki/Protocol/Closed Groups/ClosedGroupsProtocol.swift @@ -411,7 +411,9 @@ public final class ClosedGroupsProtocol : NSObject { // Respond to the request print("[Loki] Responding to sender key request from: \(senderPublicKey).") SessionManagementProtocol.sendSessionRequestIfNeeded(to: senderPublicKey, using: transaction) // This internally takes care of multi device - let userRatchet = SharedSenderKeysImplementation.shared.generateRatchet(for: groupPublicKey, senderPublicKey: userPublicKey, using: transaction) + guard let userRatchet = Storage.getClosedGroupRatchet(for: groupPublicKey, senderPublicKey: userPublicKey) else { + return print("[Loki] Missing own ratchet.") + } let userSenderKey = ClosedGroupSenderKey(chainKey: Data(hex: userRatchet.chainKey), keyIndex: userRatchet.keyIndex, publicKey: Data(hex: userPublicKey)) let thread = TSContactThread.getOrCreateThread(withContactId: senderPublicKey, transaction: transaction) thread.save(with: transaction) @@ -424,20 +426,9 @@ public final class ClosedGroupsProtocol : NSObject { private static func handleSenderKeyMessage(_ closedGroupUpdate: SSKProtoDataMessageClosedGroupUpdate, from senderPublicKey: String, using transaction: YapDatabaseReadWriteTransaction) { // Prepare let groupPublicKey = closedGroupUpdate.groupPublicKey.toHexString() - let groupID = LKGroupUtilities.getEncodedClosedGroupIDAsData(groupPublicKey) - guard let thread = TSGroupThread.fetch(uniqueId: TSGroupThread.threadId(fromGroupId: groupID), transaction: transaction) else { - return print("[Loki] Ignoring closed group sender key for nonexistent group.") - } - let group = thread.groupModel guard let senderKey = closedGroupUpdate.senderKeys.first else { return print("[Loki] Ignoring invalid closed group sender key.") } - // Check that the requesting user is a member of the group - var membersAndLinkedDevices: Set = Set(group.groupMemberIds) - for member in group.groupMemberIds { - let deviceLinks = OWSPrimaryStorage.shared().getDeviceLinks(for: member, in: transaction) - membersAndLinkedDevices.formUnion(deviceLinks.flatMap { [ $0.master.publicKey, $0.slave.publicKey ] }) - } guard senderKey.publicKey.toHexString() == senderPublicKey else { return print("[Loki] Ignoring invalid closed group sender key.") } diff --git a/SignalServiceKit/src/Loki/Protocol/Closed Groups/SharedSenderKeysImplementation.swift b/SignalServiceKit/src/Loki/Protocol/Closed Groups/SharedSenderKeysImplementation.swift index 170158c6f..59e9d8cb5 100644 --- a/SignalServiceKit/src/Loki/Protocol/Closed Groups/SharedSenderKeysImplementation.swift +++ b/SignalServiceKit/src/Loki/Protocol/Closed Groups/SharedSenderKeysImplementation.swift @@ -177,7 +177,12 @@ public final class SharedSenderKeysImplementation : NSObject { throw RatchetingError.messageKeyMissing(targetKeyIndex: keyIndex, groupPublicKey: groupPublicKey, senderPublicKey: senderPublicKey) } let aes = try AES(key: Data(hex: messageKey).bytes, blockMode: gcm, padding: .noPadding) - return Data(try aes.decrypt(ciphertext.bytes)) + do { + return Data(try aes.decrypt(ciphertext.bytes)) + } catch { + ClosedGroupsProtocol.requestSenderKey(for: groupPublicKey, senderPublicKey: senderPublicKey, using: transaction) + throw error + } } @objc public func isClosedGroup(_ publicKey: String) -> Bool {