From de384fd4d98616e426e1039bcbc03a0ad9426463 Mon Sep 17 00:00:00 2001 From: nielsandriesse Date: Tue, 5 Jan 2021 16:13:02 +1100 Subject: [PATCH] Ensure legacy closed groups still work --- .../Sending & Receiving/MessageReceiver.swift | 37 +++++++++++-------- SignalUtilitiesKit/Configuration.swift | 1 + 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/SessionMessagingKit/Sending & Receiving/MessageReceiver.swift b/SessionMessagingKit/Sending & Receiving/MessageReceiver.swift index 92e54db01..66409971b 100644 --- a/SessionMessagingKit/Sending & Receiving/MessageReceiver.swift +++ b/SessionMessagingKit/Sending & Receiving/MessageReceiver.swift @@ -71,24 +71,31 @@ public enum MessageReceiver { (plaintext, sender) = try decryptWithSessionProtocol(ciphertext: ciphertext, using: userX25519KeyPair) case .closedGroupCiphertext: guard let hexEncodedGroupPublicKey = envelope.source, SNMessagingKitConfiguration.shared.storage.isClosedGroup(hexEncodedGroupPublicKey) else { throw Error.invalidGroupPublicKey } - var keyPairs = Storage.shared.getClosedGroupEncryptionKeyPairs(for: hexEncodedGroupPublicKey) - guard !keyPairs.isEmpty else { throw Error.noGroupKeyPair } - // Loop through all known group key pairs in reverse order (i.e. try the latest key pair first (which'll more than - // likely be the one we want) but try older ones in case that didn't work) - var keyPair = keyPairs.removeLast() - func decrypt() throws { - do { - (plaintext, sender) = try decryptWithSessionProtocol(ciphertext: ciphertext, using: keyPair) - } catch { - if !keyPairs.isEmpty { - keyPair = keyPairs.removeLast() - try decrypt() - } else { - throw error + do { + var keyPairs = Storage.shared.getClosedGroupEncryptionKeyPairs(for: hexEncodedGroupPublicKey) + guard !keyPairs.isEmpty else { throw Error.noGroupKeyPair } + // Loop through all known group key pairs in reverse order (i.e. try the latest key pair first (which'll more than + // likely be the one we want) but try older ones in case that didn't work) + var keyPair = keyPairs.removeLast() + func decrypt() throws { + do { + (plaintext, sender) = try decryptWithSessionProtocol(ciphertext: ciphertext, using: keyPair) + } catch { + if !keyPairs.isEmpty { + keyPair = keyPairs.removeLast() + try decrypt() + } else { + throw error + } } } + try decrypt() + } catch { + // Fall back on the V1 method + guard let privateKey = SNMessagingKitConfiguration.shared.storage.getClosedGroupPrivateKey(for: hexEncodedGroupPublicKey) else { throw Error.noGroupKeyPair } + let keyPair = try ECKeyPair(publicKeyData: Data(hex: hexEncodedGroupPublicKey.removing05PrefixIfNeeded()), privateKeyData: Data(hex: privateKey)) + (plaintext, sender) = try decryptWithSessionProtocol(ciphertext: ciphertext, using: keyPair) } - try decrypt() groupPublicKey = envelope.source default: throw Error.unknownEnvelopeType } diff --git a/SignalUtilitiesKit/Configuration.swift b/SignalUtilitiesKit/Configuration.swift index 30384df3b..540664d79 100644 --- a/SignalUtilitiesKit/Configuration.swift +++ b/SignalUtilitiesKit/Configuration.swift @@ -10,6 +10,7 @@ public final class Configuration : NSObject { @objc public static func performMainSetup() { SNMessagingKit.configure(storage: Storage.shared) SNSnodeKit.configure(storage: Storage.shared) + SNProtocolKit.configure(storage: Storage.shared, sharedSenderKeysDelegate: MessageSender.shared) SNUtilitiesKit.configure(owsPrimaryStorage: OWSPrimaryStorage.shared(), maxFileSize: UInt(Double(FileServerAPI.maxFileSize) / FileServerAPI.fileSizeORMultiplier)) } }