Ensure legacy closed groups still work

pull/325/head
nielsandriesse 4 years ago
parent da503b0df1
commit de384fd4d9

@ -71,24 +71,31 @@ public enum MessageReceiver {
(plaintext, sender) = try decryptWithSessionProtocol(ciphertext: ciphertext, using: userX25519KeyPair) (plaintext, sender) = try decryptWithSessionProtocol(ciphertext: ciphertext, using: userX25519KeyPair)
case .closedGroupCiphertext: case .closedGroupCiphertext:
guard let hexEncodedGroupPublicKey = envelope.source, SNMessagingKitConfiguration.shared.storage.isClosedGroup(hexEncodedGroupPublicKey) else { throw Error.invalidGroupPublicKey } guard let hexEncodedGroupPublicKey = envelope.source, SNMessagingKitConfiguration.shared.storage.isClosedGroup(hexEncodedGroupPublicKey) else { throw Error.invalidGroupPublicKey }
var keyPairs = Storage.shared.getClosedGroupEncryptionKeyPairs(for: hexEncodedGroupPublicKey) do {
guard !keyPairs.isEmpty else { throw Error.noGroupKeyPair } var keyPairs = Storage.shared.getClosedGroupEncryptionKeyPairs(for: hexEncodedGroupPublicKey)
// Loop through all known group key pairs in reverse order (i.e. try the latest key pair first (which'll more than guard !keyPairs.isEmpty else { throw Error.noGroupKeyPair }
// likely be the one we want) but try older ones in case that didn't work) // Loop through all known group key pairs in reverse order (i.e. try the latest key pair first (which'll more than
var keyPair = keyPairs.removeLast() // likely be the one we want) but try older ones in case that didn't work)
func decrypt() throws { var keyPair = keyPairs.removeLast()
do { func decrypt() throws {
(plaintext, sender) = try decryptWithSessionProtocol(ciphertext: ciphertext, using: keyPair) do {
} catch { (plaintext, sender) = try decryptWithSessionProtocol(ciphertext: ciphertext, using: keyPair)
if !keyPairs.isEmpty { } catch {
keyPair = keyPairs.removeLast() if !keyPairs.isEmpty {
try decrypt() keyPair = keyPairs.removeLast()
} else { try decrypt()
throw error } 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 groupPublicKey = envelope.source
default: throw Error.unknownEnvelopeType default: throw Error.unknownEnvelopeType
} }

@ -10,6 +10,7 @@ public final class Configuration : NSObject {
@objc public static func performMainSetup() { @objc public static func performMainSetup() {
SNMessagingKit.configure(storage: Storage.shared) SNMessagingKit.configure(storage: Storage.shared)
SNSnodeKit.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)) SNUtilitiesKit.configure(owsPrimaryStorage: OWSPrimaryStorage.shared(), maxFileSize: UInt(Double(FileServerAPI.maxFileSize) / FileServerAPI.fileSizeORMultiplier))
} }
} }

Loading…
Cancel
Save