From da503b0df1550fa61a387203bb63fad9bbc78c64 Mon Sep 17 00:00:00 2001 From: Niels Andriesse Date: Mon, 4 Jan 2021 15:30:13 +1100 Subject: [PATCH] Update Session protocol closed groups logic --- .../Database/Storage+ClosedGroups.swift | 44 +- .../ClosedGroupUpdateV2.swift | 210 ++++++++ .../Protos/Generated/SNProto.swift | 462 ++++++++++++++++++ .../Protos/Generated/SessionProtos.pb.swift | 364 +++++++++++++- .../Protos/SessionProtos.proto | 62 ++- .../MessageReceiver+Decryption.swift | 21 +- .../MessageReceiver+Handling.swift | 136 ++++++ .../Sending & Receiving/MessageReceiver.swift | 33 +- .../MessageSender+ClosedGroups.swift | 161 +++++- .../MessageSender+Encryption.swift | 4 +- .../Sending & Receiving/MessageSender.swift | 10 +- Signal.xcodeproj/project.pbxproj | 4 + 12 files changed, 1466 insertions(+), 45 deletions(-) create mode 100644 SessionMessagingKit/Messages/Control Messages/ClosedGroupUpdateV2.swift diff --git a/SessionMessagingKit/Database/Storage+ClosedGroups.swift b/SessionMessagingKit/Database/Storage+ClosedGroups.swift index ea75d9c54..96d76e8bf 100644 --- a/SessionMessagingKit/Database/Storage+ClosedGroups.swift +++ b/SessionMessagingKit/Database/Storage+ClosedGroups.swift @@ -2,6 +2,47 @@ import SessionProtocolKit extension Storage { + // MARK: - V2 + + private static func getClosedGroupEncryptionKeyPairCollection(for groupPublicKey: String) -> String { + return "SNClosedGroupEncryptionKeyPairCollection-\(groupPublicKey)" + } + + private static let closedGroupPublicKeyCollection = "SNClosedGroupPublicKeyCollection" + + public func getClosedGroupEncryptionKeyPairs(for groupPublicKey: String) -> [ECKeyPair] { + let collection = Storage.getClosedGroupEncryptionKeyPairCollection(for: groupPublicKey) + var timestampsAndKeyPairs: [(timestamp: Double, keyPair: ECKeyPair)] = [] + Storage.read { transaction in + transaction.enumerateKeysAndObjects(inCollection: collection) { key, object, _ in + guard let timestamp = Double(key), let keyPair = object as? ECKeyPair else { return } + timestampsAndKeyPairs.append((timestamp, keyPair)) + } + } + return timestampsAndKeyPairs.sorted { $0.timestamp < $1.timestamp }.map { $0.keyPair } + } + + public func getLatestClosedGroupEncryptionKeyPair(for groupPublicKey: String) -> ECKeyPair? { + return getClosedGroupEncryptionKeyPairs(for: groupPublicKey).last + } + + public func addClosedGroupEncryptionKeyPair(_ keyPair: ECKeyPair, for groupPublicKey: String, using transaction: Any) { + let collection = Storage.getClosedGroupEncryptionKeyPairCollection(for: groupPublicKey) + let timestamp = String(Date().timeIntervalSince1970) + (transaction as! YapDatabaseReadWriteTransaction).setObject(keyPair, forKey: timestamp, inCollection: collection) + } + + public func removeAllClosedGroupEncryptionKeyPairs(for groupPublicKey: String, using transaction: Any) { + let collection = Storage.getClosedGroupEncryptionKeyPairCollection(for: groupPublicKey) + (transaction as! YapDatabaseReadWriteTransaction).removeAllObjects(inCollection: collection) + } + + public func addClosedGroupPublicKey(_ groupPublicKey: String, using transaction: Any) { + (transaction as! YapDatabaseReadWriteTransaction).setObject(groupPublicKey, forKey: groupPublicKey, inCollection: Storage.closedGroupPublicKeyCollection) + } + + + // MARK: - Ratchets private static func getClosedGroupRatchetCollection(_ collection: ClosedGroupRatchetCollectionType, for groupPublicKey: String) -> String { @@ -76,7 +117,8 @@ extension Storage { public func getUserClosedGroupPublicKeys() -> Set { var result: Set = [] Storage.read { transaction in - result = Set(transaction.allKeys(inCollection: Storage.closedGroupPrivateKeyCollection)) + result = result.union(Set(transaction.allKeys(inCollection: Storage.closedGroupPublicKeyCollection))) + result = result.union(Set(transaction.allKeys(inCollection: Storage.closedGroupPrivateKeyCollection))) } return result } diff --git a/SessionMessagingKit/Messages/Control Messages/ClosedGroupUpdateV2.swift b/SessionMessagingKit/Messages/Control Messages/ClosedGroupUpdateV2.swift new file mode 100644 index 000000000..da0a845b2 --- /dev/null +++ b/SessionMessagingKit/Messages/Control Messages/ClosedGroupUpdateV2.swift @@ -0,0 +1,210 @@ +import SessionProtocolKit +import SessionUtilitiesKit + +public final class ClosedGroupUpdateV2 : ControlMessage { + public var kind: Kind? + + // MARK: Kind + public enum Kind : CustomStringConvertible { + case new(publicKey: Data, name: String, encryptionKeyPair: ECKeyPair, members: [Data], admins: [Data]) + case update(name: String, members: [Data]) + case encryptionKeyPair([KeyPairWrapper]) // The new encryption key pair encrypted for each member individually + + public var description: String { + switch self { + case .new: return "new" + case .update: return "update" + case .encryptionKeyPair: return "encryptionKeyPair" + } + } + } + + // MARK: Key Pair Wrapper + @objc(SNKeyPairWrapper) + public final class KeyPairWrapper : NSObject, NSCoding { // NSObject/NSCoding conformance is needed for YapDatabase compatibility + public var publicKey: String? + public var encryptedKeyPair: Data? + + public var isValid: Bool { publicKey != nil && encryptedKeyPair != nil } + + public init(publicKey: String, encryptedKeyPair: Data) { + self.publicKey = publicKey + self.encryptedKeyPair = encryptedKeyPair + } + + public required init?(coder: NSCoder) { + if let publicKey = coder.decodeObject(forKey: "publicKey") as! String? { self.publicKey = publicKey } + if let encryptedKeyPair = coder.decodeObject(forKey: "encryptedKeyPair") as! Data? { self.encryptedKeyPair = encryptedKeyPair } + } + + public func encode(with coder: NSCoder) { + coder.encode(publicKey, forKey: "publicKey") + coder.encode(encryptedKeyPair, forKey: "encryptedKeyPair") + } + + public static func fromProto(_ proto: SNProtoDataMessageClosedGroupUpdateV2KeyPairWrapper) -> KeyPairWrapper? { + return KeyPairWrapper(publicKey: proto.publicKey.toHexString(), encryptedKeyPair: proto.encryptedKeyPair) + } + + public func toProto() -> SNProtoDataMessageClosedGroupUpdateV2KeyPairWrapper? { + guard let publicKey = publicKey, let encryptedKeyPair = encryptedKeyPair else { return nil } + let result = SNProtoDataMessageClosedGroupUpdateV2KeyPairWrapper.builder(publicKey: Data(hex: publicKey), encryptedKeyPair: encryptedKeyPair) + do { + return try result.build() + } catch { + SNLog("Couldn't construct key pair wrapper proto from: \(self).") + return nil + } + } + } + + // MARK: Initialization + public override init() { super.init() } + + internal init(kind: Kind) { + super.init() + self.kind = kind + } + + // MARK: Validation + public override var isValid: Bool { + guard super.isValid, let kind = kind else { return false } + switch kind { + case .new(let publicKey, let name, let encryptionKeyPair, let members, let admins): + return !publicKey.isEmpty && !name.isEmpty && !encryptionKeyPair.publicKey.isEmpty + && !encryptionKeyPair.privateKey.isEmpty && !members.isEmpty && !admins.isEmpty + case .update(let name, let members): + return !name.isEmpty && !members.isEmpty + case .encryptionKeyPair: return true + } + } + + // MARK: Coding + public required init?(coder: NSCoder) { + super.init(coder: coder) + guard let rawKind = coder.decodeObject(forKey: "kind") as? String else { return nil } + switch rawKind { + case "new": + guard let publicKey = coder.decodeObject(forKey: "publicKey") as? Data, + let name = coder.decodeObject(forKey: "name") as? String, + let encryptionKeyPair = coder.decodeObject(forKey: "encryptionKeyPair") as? ECKeyPair, + let members = coder.decodeObject(forKey: "members") as? [Data], + let admins = coder.decodeObject(forKey: "admins") as? [Data] else { return nil } + self.kind = .new(publicKey: publicKey, name: name, encryptionKeyPair: encryptionKeyPair, members: members, admins: admins) + case "update": + guard let name = coder.decodeObject(forKey: "name") as? String, + let members = coder.decodeObject(forKey: "members") as? [Data] else { return nil } + self.kind = .update(name: name, members: members) + case "encryptionKeyPair": + guard let wrappers = coder.decodeObject(forKey: "wrappers") as? [KeyPairWrapper] else { return nil } + self.kind = .encryptionKeyPair(wrappers) + default: return nil + } + } + + public override func encode(with coder: NSCoder) { + super.encode(with: coder) + guard let kind = kind else { return } + switch kind { + case .new(let publicKey, let name, let encryptionKeyPair, let members, let admins): + coder.encode("new", forKey: "kind") + coder.encode(publicKey, forKey: "publicKey") + coder.encode(name, forKey: "name") + coder.encode(encryptionKeyPair, forKey: "encryptionKeyPair") + coder.encode(members, forKey: "members") + coder.encode(admins, forKey: "admins") + case .update(let name, let members): + coder.encode("update", forKey: "kind") + coder.encode(name, forKey: "name") + coder.encode(members, forKey: "members") + case .encryptionKeyPair(let wrappers): + coder.encode("encryptionKeyPair", forKey: "kind") + coder.encode(wrappers, forKey: "wrappers") + } + } + + // MARK: Proto Conversion + public override class func fromProto(_ proto: SNProtoContent) -> ClosedGroupUpdateV2? { + guard let closedGroupUpdateProto = proto.dataMessage?.closedGroupUpdateV2 else { return nil } + let kind: Kind + switch closedGroupUpdateProto.type { + case .new: + guard let publicKey = closedGroupUpdateProto.publicKey, let name = closedGroupUpdateProto.name, + let encryptionKeyPairAsProto = closedGroupUpdateProto.encryptionKeyPair else { return nil } + do { + let encryptionKeyPair = try ECKeyPair(publicKeyData: encryptionKeyPairAsProto.publicKey, privateKeyData: encryptionKeyPairAsProto.privateKey) + kind = .new(publicKey: publicKey, name: name, encryptionKeyPair: encryptionKeyPair, + members: closedGroupUpdateProto.members, admins: closedGroupUpdateProto.admins) + } catch { + SNLog("Couldn't parse key pair.") + return nil + } + case .update: + guard let name = closedGroupUpdateProto.name else { return nil } + kind = .update(name: name, members: closedGroupUpdateProto.members) + case .encryptionKeyPair: + let wrappers = closedGroupUpdateProto.wrappers.compactMap { KeyPairWrapper.fromProto($0) } + kind = .encryptionKeyPair(wrappers) + } + return ClosedGroupUpdateV2(kind: kind) + } + + public override func toProto(using transaction: YapDatabaseReadWriteTransaction) -> SNProtoContent? { + guard let kind = kind else { + SNLog("Couldn't construct closed group update proto from: \(self).") + return nil + } + do { + let closedGroupUpdate: SNProtoDataMessageClosedGroupUpdateV2.SNProtoDataMessageClosedGroupUpdateV2Builder + switch kind { + case .new(let publicKey, let name, let encryptionKeyPair, let members, let admins): + closedGroupUpdate = SNProtoDataMessageClosedGroupUpdateV2.builder(type: .new) + closedGroupUpdate.setPublicKey(publicKey) + closedGroupUpdate.setName(name) + let encryptionKeyPairAsProto = SNProtoDataMessageClosedGroupUpdateV2KeyPair.builder(publicKey: encryptionKeyPair.publicKey, privateKey: encryptionKeyPair.privateKey) + do { + closedGroupUpdate.setEncryptionKeyPair(try encryptionKeyPairAsProto.build()) + } catch { + SNLog("Couldn't construct closed group update proto from: \(self).") + return nil + } + closedGroupUpdate.setMembers(members) + closedGroupUpdate.setAdmins(admins) + case .update(let name, let members): + closedGroupUpdate = SNProtoDataMessageClosedGroupUpdateV2.builder(type: .update) + closedGroupUpdate.setName(name) + closedGroupUpdate.setMembers(members) + case .encryptionKeyPair(let wrappers): + closedGroupUpdate = SNProtoDataMessageClosedGroupUpdateV2.builder(type: .encryptionKeyPair) + closedGroupUpdate.setWrappers(wrappers.compactMap { $0.toProto() }) + } + let contentProto = SNProtoContent.builder() + let dataMessageProto = SNProtoDataMessage.builder() + dataMessageProto.setClosedGroupUpdateV2(try closedGroupUpdate.build()) + // Group context + try setGroupContextIfNeeded(on: dataMessageProto, using: transaction) + // Expiration timer + // TODO: We * want * expiration timer updates to be explicit. But currently Android will disable the expiration timer for a conversation + // if it receives a message without the current expiration timer value attached to it... + var expiration: UInt32 = 0 + if let disappearingMessagesConfiguration = OWSDisappearingMessagesConfiguration.fetch(uniqueId: threadID!, transaction: transaction) { + expiration = disappearingMessagesConfiguration.isEnabled ? disappearingMessagesConfiguration.durationSeconds : 0 + } + dataMessageProto.setExpireTimer(expiration) + contentProto.setDataMessage(try dataMessageProto.build()) + return try contentProto.build() + } catch { + SNLog("Couldn't construct closed group update proto from: \(self).") + return nil + } + } + + // MARK: Description + public override var description: String { + """ + ClosedGroupUpdate( + kind: \(kind?.description ?? "null") + ) + """ + } +} diff --git a/SessionMessagingKit/Protos/Generated/SNProto.swift b/SessionMessagingKit/Protos/Generated/SNProto.swift index 4eb8e8231..1ce70e31c 100644 --- a/SessionMessagingKit/Protos/Generated/SNProto.swift +++ b/SessionMessagingKit/Protos/Generated/SNProto.swift @@ -3399,6 +3399,451 @@ extension SNProtoDataMessageLokiProfile.SNProtoDataMessageLokiProfileBuilder { #endif +// MARK: - SNProtoDataMessageClosedGroupUpdateV2KeyPair + +@objc public class SNProtoDataMessageClosedGroupUpdateV2KeyPair: NSObject { + + // MARK: - SNProtoDataMessageClosedGroupUpdateV2KeyPairBuilder + + @objc public class func builder(publicKey: Data, privateKey: Data) -> SNProtoDataMessageClosedGroupUpdateV2KeyPairBuilder { + return SNProtoDataMessageClosedGroupUpdateV2KeyPairBuilder(publicKey: publicKey, privateKey: privateKey) + } + + // asBuilder() constructs a builder that reflects the proto's contents. + @objc public func asBuilder() -> SNProtoDataMessageClosedGroupUpdateV2KeyPairBuilder { + let builder = SNProtoDataMessageClosedGroupUpdateV2KeyPairBuilder(publicKey: publicKey, privateKey: privateKey) + return builder + } + + @objc public class SNProtoDataMessageClosedGroupUpdateV2KeyPairBuilder: NSObject { + + private var proto = SessionProtos_DataMessage.ClosedGroupUpdateV2.KeyPair() + + @objc fileprivate override init() {} + + @objc fileprivate init(publicKey: Data, privateKey: Data) { + super.init() + + setPublicKey(publicKey) + setPrivateKey(privateKey) + } + + @objc public func setPublicKey(_ valueParam: Data) { + proto.publicKey = valueParam + } + + @objc public func setPrivateKey(_ valueParam: Data) { + proto.privateKey = valueParam + } + + @objc public func build() throws -> SNProtoDataMessageClosedGroupUpdateV2KeyPair { + return try SNProtoDataMessageClosedGroupUpdateV2KeyPair.parseProto(proto) + } + + @objc public func buildSerializedData() throws -> Data { + return try SNProtoDataMessageClosedGroupUpdateV2KeyPair.parseProto(proto).serializedData() + } + } + + fileprivate let proto: SessionProtos_DataMessage.ClosedGroupUpdateV2.KeyPair + + @objc public let publicKey: Data + + @objc public let privateKey: Data + + private init(proto: SessionProtos_DataMessage.ClosedGroupUpdateV2.KeyPair, + publicKey: Data, + privateKey: Data) { + self.proto = proto + self.publicKey = publicKey + self.privateKey = privateKey + } + + @objc + public func serializedData() throws -> Data { + return try self.proto.serializedData() + } + + @objc public class func parseData(_ serializedData: Data) throws -> SNProtoDataMessageClosedGroupUpdateV2KeyPair { + let proto = try SessionProtos_DataMessage.ClosedGroupUpdateV2.KeyPair(serializedData: serializedData) + return try parseProto(proto) + } + + fileprivate class func parseProto(_ proto: SessionProtos_DataMessage.ClosedGroupUpdateV2.KeyPair) throws -> SNProtoDataMessageClosedGroupUpdateV2KeyPair { + guard proto.hasPublicKey else { + throw SNProtoError.invalidProtobuf(description: "\(logTag) missing required field: publicKey") + } + let publicKey = proto.publicKey + + guard proto.hasPrivateKey else { + throw SNProtoError.invalidProtobuf(description: "\(logTag) missing required field: privateKey") + } + let privateKey = proto.privateKey + + // MARK: - Begin Validation Logic for SNProtoDataMessageClosedGroupUpdateV2KeyPair - + + // MARK: - End Validation Logic for SNProtoDataMessageClosedGroupUpdateV2KeyPair - + + let result = SNProtoDataMessageClosedGroupUpdateV2KeyPair(proto: proto, + publicKey: publicKey, + privateKey: privateKey) + return result + } + + @objc public override var debugDescription: String { + return "\(proto)" + } +} + +#if DEBUG + +extension SNProtoDataMessageClosedGroupUpdateV2KeyPair { + @objc public func serializedDataIgnoringErrors() -> Data? { + return try! self.serializedData() + } +} + +extension SNProtoDataMessageClosedGroupUpdateV2KeyPair.SNProtoDataMessageClosedGroupUpdateV2KeyPairBuilder { + @objc public func buildIgnoringErrors() -> SNProtoDataMessageClosedGroupUpdateV2KeyPair? { + return try! self.build() + } +} + +#endif + +// MARK: - SNProtoDataMessageClosedGroupUpdateV2KeyPairWrapper + +@objc public class SNProtoDataMessageClosedGroupUpdateV2KeyPairWrapper: NSObject { + + // MARK: - SNProtoDataMessageClosedGroupUpdateV2KeyPairWrapperBuilder + + @objc public class func builder(publicKey: Data, encryptedKeyPair: Data) -> SNProtoDataMessageClosedGroupUpdateV2KeyPairWrapperBuilder { + return SNProtoDataMessageClosedGroupUpdateV2KeyPairWrapperBuilder(publicKey: publicKey, encryptedKeyPair: encryptedKeyPair) + } + + // asBuilder() constructs a builder that reflects the proto's contents. + @objc public func asBuilder() -> SNProtoDataMessageClosedGroupUpdateV2KeyPairWrapperBuilder { + let builder = SNProtoDataMessageClosedGroupUpdateV2KeyPairWrapperBuilder(publicKey: publicKey, encryptedKeyPair: encryptedKeyPair) + return builder + } + + @objc public class SNProtoDataMessageClosedGroupUpdateV2KeyPairWrapperBuilder: NSObject { + + private var proto = SessionProtos_DataMessage.ClosedGroupUpdateV2.KeyPairWrapper() + + @objc fileprivate override init() {} + + @objc fileprivate init(publicKey: Data, encryptedKeyPair: Data) { + super.init() + + setPublicKey(publicKey) + setEncryptedKeyPair(encryptedKeyPair) + } + + @objc public func setPublicKey(_ valueParam: Data) { + proto.publicKey = valueParam + } + + @objc public func setEncryptedKeyPair(_ valueParam: Data) { + proto.encryptedKeyPair = valueParam + } + + @objc public func build() throws -> SNProtoDataMessageClosedGroupUpdateV2KeyPairWrapper { + return try SNProtoDataMessageClosedGroupUpdateV2KeyPairWrapper.parseProto(proto) + } + + @objc public func buildSerializedData() throws -> Data { + return try SNProtoDataMessageClosedGroupUpdateV2KeyPairWrapper.parseProto(proto).serializedData() + } + } + + fileprivate let proto: SessionProtos_DataMessage.ClosedGroupUpdateV2.KeyPairWrapper + + @objc public let publicKey: Data + + @objc public let encryptedKeyPair: Data + + private init(proto: SessionProtos_DataMessage.ClosedGroupUpdateV2.KeyPairWrapper, + publicKey: Data, + encryptedKeyPair: Data) { + self.proto = proto + self.publicKey = publicKey + self.encryptedKeyPair = encryptedKeyPair + } + + @objc + public func serializedData() throws -> Data { + return try self.proto.serializedData() + } + + @objc public class func parseData(_ serializedData: Data) throws -> SNProtoDataMessageClosedGroupUpdateV2KeyPairWrapper { + let proto = try SessionProtos_DataMessage.ClosedGroupUpdateV2.KeyPairWrapper(serializedData: serializedData) + return try parseProto(proto) + } + + fileprivate class func parseProto(_ proto: SessionProtos_DataMessage.ClosedGroupUpdateV2.KeyPairWrapper) throws -> SNProtoDataMessageClosedGroupUpdateV2KeyPairWrapper { + guard proto.hasPublicKey else { + throw SNProtoError.invalidProtobuf(description: "\(logTag) missing required field: publicKey") + } + let publicKey = proto.publicKey + + guard proto.hasEncryptedKeyPair else { + throw SNProtoError.invalidProtobuf(description: "\(logTag) missing required field: encryptedKeyPair") + } + let encryptedKeyPair = proto.encryptedKeyPair + + // MARK: - Begin Validation Logic for SNProtoDataMessageClosedGroupUpdateV2KeyPairWrapper - + + // MARK: - End Validation Logic for SNProtoDataMessageClosedGroupUpdateV2KeyPairWrapper - + + let result = SNProtoDataMessageClosedGroupUpdateV2KeyPairWrapper(proto: proto, + publicKey: publicKey, + encryptedKeyPair: encryptedKeyPair) + return result + } + + @objc public override var debugDescription: String { + return "\(proto)" + } +} + +#if DEBUG + +extension SNProtoDataMessageClosedGroupUpdateV2KeyPairWrapper { + @objc public func serializedDataIgnoringErrors() -> Data? { + return try! self.serializedData() + } +} + +extension SNProtoDataMessageClosedGroupUpdateV2KeyPairWrapper.SNProtoDataMessageClosedGroupUpdateV2KeyPairWrapperBuilder { + @objc public func buildIgnoringErrors() -> SNProtoDataMessageClosedGroupUpdateV2KeyPairWrapper? { + return try! self.build() + } +} + +#endif + +// MARK: - SNProtoDataMessageClosedGroupUpdateV2 + +@objc public class SNProtoDataMessageClosedGroupUpdateV2: NSObject { + + // MARK: - SNProtoDataMessageClosedGroupUpdateV2Type + + @objc public enum SNProtoDataMessageClosedGroupUpdateV2Type: Int32 { + case new = 1 + case update = 2 + case encryptionKeyPair = 3 + } + + private class func SNProtoDataMessageClosedGroupUpdateV2TypeWrap(_ value: SessionProtos_DataMessage.ClosedGroupUpdateV2.TypeEnum) -> SNProtoDataMessageClosedGroupUpdateV2Type { + switch value { + case .new: return .new + case .update: return .update + case .encryptionKeyPair: return .encryptionKeyPair + } + } + + private class func SNProtoDataMessageClosedGroupUpdateV2TypeUnwrap(_ value: SNProtoDataMessageClosedGroupUpdateV2Type) -> SessionProtos_DataMessage.ClosedGroupUpdateV2.TypeEnum { + switch value { + case .new: return .new + case .update: return .update + case .encryptionKeyPair: return .encryptionKeyPair + } + } + + // MARK: - SNProtoDataMessageClosedGroupUpdateV2Builder + + @objc public class func builder(type: SNProtoDataMessageClosedGroupUpdateV2Type) -> SNProtoDataMessageClosedGroupUpdateV2Builder { + return SNProtoDataMessageClosedGroupUpdateV2Builder(type: type) + } + + // asBuilder() constructs a builder that reflects the proto's contents. + @objc public func asBuilder() -> SNProtoDataMessageClosedGroupUpdateV2Builder { + let builder = SNProtoDataMessageClosedGroupUpdateV2Builder(type: type) + if let _value = publicKey { + builder.setPublicKey(_value) + } + if let _value = name { + builder.setName(_value) + } + if let _value = encryptionKeyPair { + builder.setEncryptionKeyPair(_value) + } + builder.setMembers(members) + builder.setAdmins(admins) + builder.setWrappers(wrappers) + return builder + } + + @objc public class SNProtoDataMessageClosedGroupUpdateV2Builder: NSObject { + + private var proto = SessionProtos_DataMessage.ClosedGroupUpdateV2() + + @objc fileprivate override init() {} + + @objc fileprivate init(type: SNProtoDataMessageClosedGroupUpdateV2Type) { + super.init() + + setType(type) + } + + @objc public func setType(_ valueParam: SNProtoDataMessageClosedGroupUpdateV2Type) { + proto.type = SNProtoDataMessageClosedGroupUpdateV2TypeUnwrap(valueParam) + } + + @objc public func setPublicKey(_ valueParam: Data) { + proto.publicKey = valueParam + } + + @objc public func setName(_ valueParam: String) { + proto.name = valueParam + } + + @objc public func setEncryptionKeyPair(_ valueParam: SNProtoDataMessageClosedGroupUpdateV2KeyPair) { + proto.encryptionKeyPair = valueParam.proto + } + + @objc public func addMembers(_ valueParam: Data) { + var items = proto.members + items.append(valueParam) + proto.members = items + } + + @objc public func setMembers(_ wrappedItems: [Data]) { + proto.members = wrappedItems + } + + @objc public func addAdmins(_ valueParam: Data) { + var items = proto.admins + items.append(valueParam) + proto.admins = items + } + + @objc public func setAdmins(_ wrappedItems: [Data]) { + proto.admins = wrappedItems + } + + @objc public func addWrappers(_ valueParam: SNProtoDataMessageClosedGroupUpdateV2KeyPairWrapper) { + var items = proto.wrappers + items.append(valueParam.proto) + proto.wrappers = items + } + + @objc public func setWrappers(_ wrappedItems: [SNProtoDataMessageClosedGroupUpdateV2KeyPairWrapper]) { + proto.wrappers = wrappedItems.map { $0.proto } + } + + @objc public func build() throws -> SNProtoDataMessageClosedGroupUpdateV2 { + return try SNProtoDataMessageClosedGroupUpdateV2.parseProto(proto) + } + + @objc public func buildSerializedData() throws -> Data { + return try SNProtoDataMessageClosedGroupUpdateV2.parseProto(proto).serializedData() + } + } + + fileprivate let proto: SessionProtos_DataMessage.ClosedGroupUpdateV2 + + @objc public let type: SNProtoDataMessageClosedGroupUpdateV2Type + + @objc public let encryptionKeyPair: SNProtoDataMessageClosedGroupUpdateV2KeyPair? + + @objc public let wrappers: [SNProtoDataMessageClosedGroupUpdateV2KeyPairWrapper] + + @objc public var publicKey: Data? { + guard proto.hasPublicKey else { + return nil + } + return proto.publicKey + } + @objc public var hasPublicKey: Bool { + return proto.hasPublicKey + } + + @objc public var name: String? { + guard proto.hasName else { + return nil + } + return proto.name + } + @objc public var hasName: Bool { + return proto.hasName + } + + @objc public var members: [Data] { + return proto.members + } + + @objc public var admins: [Data] { + return proto.admins + } + + private init(proto: SessionProtos_DataMessage.ClosedGroupUpdateV2, + type: SNProtoDataMessageClosedGroupUpdateV2Type, + encryptionKeyPair: SNProtoDataMessageClosedGroupUpdateV2KeyPair?, + wrappers: [SNProtoDataMessageClosedGroupUpdateV2KeyPairWrapper]) { + self.proto = proto + self.type = type + self.encryptionKeyPair = encryptionKeyPair + self.wrappers = wrappers + } + + @objc + public func serializedData() throws -> Data { + return try self.proto.serializedData() + } + + @objc public class func parseData(_ serializedData: Data) throws -> SNProtoDataMessageClosedGroupUpdateV2 { + let proto = try SessionProtos_DataMessage.ClosedGroupUpdateV2(serializedData: serializedData) + return try parseProto(proto) + } + + fileprivate class func parseProto(_ proto: SessionProtos_DataMessage.ClosedGroupUpdateV2) throws -> SNProtoDataMessageClosedGroupUpdateV2 { + guard proto.hasType else { + throw SNProtoError.invalidProtobuf(description: "\(logTag) missing required field: type") + } + let type = SNProtoDataMessageClosedGroupUpdateV2TypeWrap(proto.type) + + var encryptionKeyPair: SNProtoDataMessageClosedGroupUpdateV2KeyPair? = nil + if proto.hasEncryptionKeyPair { + encryptionKeyPair = try SNProtoDataMessageClosedGroupUpdateV2KeyPair.parseProto(proto.encryptionKeyPair) + } + + var wrappers: [SNProtoDataMessageClosedGroupUpdateV2KeyPairWrapper] = [] + wrappers = try proto.wrappers.map { try SNProtoDataMessageClosedGroupUpdateV2KeyPairWrapper.parseProto($0) } + + // MARK: - Begin Validation Logic for SNProtoDataMessageClosedGroupUpdateV2 - + + // MARK: - End Validation Logic for SNProtoDataMessageClosedGroupUpdateV2 - + + let result = SNProtoDataMessageClosedGroupUpdateV2(proto: proto, + type: type, + encryptionKeyPair: encryptionKeyPair, + wrappers: wrappers) + return result + } + + @objc public override var debugDescription: String { + return "\(proto)" + } +} + +#if DEBUG + +extension SNProtoDataMessageClosedGroupUpdateV2 { + @objc public func serializedDataIgnoringErrors() -> Data? { + return try! self.serializedData() + } +} + +extension SNProtoDataMessageClosedGroupUpdateV2.SNProtoDataMessageClosedGroupUpdateV2Builder { + @objc public func buildIgnoringErrors() -> SNProtoDataMessageClosedGroupUpdateV2? { + return try! self.build() + } +} + +#endif + // MARK: - SNProtoDataMessageClosedGroupUpdateSenderKey @objc public class SNProtoDataMessageClosedGroupUpdateSenderKey: NSObject { @@ -3818,6 +4263,9 @@ extension SNProtoDataMessageClosedGroupUpdate.SNProtoDataMessageClosedGroupUpdat if let _value = closedGroupUpdate { builder.setClosedGroupUpdate(_value) } + if let _value = closedGroupUpdateV2 { + builder.setClosedGroupUpdateV2(_value) + } if let _value = publicChatInfo { builder.setPublicChatInfo(_value) } @@ -3896,6 +4344,10 @@ extension SNProtoDataMessageClosedGroupUpdate.SNProtoDataMessageClosedGroupUpdat proto.closedGroupUpdate = valueParam.proto } + @objc public func setClosedGroupUpdateV2(_ valueParam: SNProtoDataMessageClosedGroupUpdateV2) { + proto.closedGroupUpdateV2 = valueParam.proto + } + @objc public func setPublicChatInfo(_ valueParam: SNProtoPublicChatInfo) { proto.publicChatInfo = valueParam.proto } @@ -3925,6 +4377,8 @@ extension SNProtoDataMessageClosedGroupUpdate.SNProtoDataMessageClosedGroupUpdat @objc public let closedGroupUpdate: SNProtoDataMessageClosedGroupUpdate? + @objc public let closedGroupUpdateV2: SNProtoDataMessageClosedGroupUpdateV2? + @objc public let publicChatInfo: SNProtoPublicChatInfo? @objc public var body: String? { @@ -3976,6 +4430,7 @@ extension SNProtoDataMessageClosedGroupUpdate.SNProtoDataMessageClosedGroupUpdat preview: [SNProtoDataMessagePreview], profile: SNProtoDataMessageLokiProfile?, closedGroupUpdate: SNProtoDataMessageClosedGroupUpdate?, + closedGroupUpdateV2: SNProtoDataMessageClosedGroupUpdateV2?, publicChatInfo: SNProtoPublicChatInfo?) { self.proto = proto self.attachments = attachments @@ -3985,6 +4440,7 @@ extension SNProtoDataMessageClosedGroupUpdate.SNProtoDataMessageClosedGroupUpdat self.preview = preview self.profile = profile self.closedGroupUpdate = closedGroupUpdate + self.closedGroupUpdateV2 = closedGroupUpdateV2 self.publicChatInfo = publicChatInfo } @@ -4028,6 +4484,11 @@ extension SNProtoDataMessageClosedGroupUpdate.SNProtoDataMessageClosedGroupUpdat closedGroupUpdate = try SNProtoDataMessageClosedGroupUpdate.parseProto(proto.closedGroupUpdate) } + var closedGroupUpdateV2: SNProtoDataMessageClosedGroupUpdateV2? = nil + if proto.hasClosedGroupUpdateV2 { + closedGroupUpdateV2 = try SNProtoDataMessageClosedGroupUpdateV2.parseProto(proto.closedGroupUpdateV2) + } + var publicChatInfo: SNProtoPublicChatInfo? = nil if proto.hasPublicChatInfo { publicChatInfo = try SNProtoPublicChatInfo.parseProto(proto.publicChatInfo) @@ -4045,6 +4506,7 @@ extension SNProtoDataMessageClosedGroupUpdate.SNProtoDataMessageClosedGroupUpdat preview: preview, profile: profile, closedGroupUpdate: closedGroupUpdate, + closedGroupUpdateV2: closedGroupUpdateV2, publicChatInfo: publicChatInfo) return result } diff --git a/SessionMessagingKit/Protos/Generated/SessionProtos.pb.swift b/SessionMessagingKit/Protos/Generated/SessionProtos.pb.swift index e0f28ecc0..cd86791c9 100644 --- a/SessionMessagingKit/Protos/Generated/SessionProtos.pb.swift +++ b/SessionMessagingKit/Protos/Generated/SessionProtos.pb.swift @@ -856,6 +856,16 @@ struct SessionProtos_DataMessage { /// Clears the value of `closedGroupUpdate`. Subsequent reads from it will return its default value. mutating func clearClosedGroupUpdate() {self._closedGroupUpdate = nil} + /// Loki + var closedGroupUpdateV2: SessionProtos_DataMessage.ClosedGroupUpdateV2 { + get {return _closedGroupUpdateV2 ?? SessionProtos_DataMessage.ClosedGroupUpdateV2()} + set {_closedGroupUpdateV2 = newValue} + } + /// Returns true if `closedGroupUpdateV2` has been explicitly set. + var hasClosedGroupUpdateV2: Bool {return self._closedGroupUpdateV2 != nil} + /// Clears the value of `closedGroupUpdateV2`. Subsequent reads from it will return its default value. + mutating func clearClosedGroupUpdateV2() {self._closedGroupUpdateV2 = nil} + /// Loki: Internal public chat info var publicChatInfo: SessionProtos_PublicChatInfo { get {return _publicChatInfo ?? SessionProtos_PublicChatInfo()} @@ -1518,7 +1528,165 @@ struct SessionProtos_DataMessage { fileprivate var _profilePicture: String? = nil } - /// Loki + struct ClosedGroupUpdateV2 { + // SwiftProtobuf.Message conformance is added in an extension below. See the + // `Message` and `Message+*Additions` files in the SwiftProtobuf library for + // methods supported on all messages. + + /// @required + var type: SessionProtos_DataMessage.ClosedGroupUpdateV2.TypeEnum { + get {return _type ?? .new} + set {_type = newValue} + } + /// Returns true if `type` has been explicitly set. + var hasType: Bool {return self._type != nil} + /// Clears the value of `type`. Subsequent reads from it will return its default value. + mutating func clearType() {self._type = nil} + + var publicKey: Data { + get {return _publicKey ?? SwiftProtobuf.Internal.emptyData} + set {_publicKey = newValue} + } + /// Returns true if `publicKey` has been explicitly set. + var hasPublicKey: Bool {return self._publicKey != nil} + /// Clears the value of `publicKey`. Subsequent reads from it will return its default value. + mutating func clearPublicKey() {self._publicKey = nil} + + var name: String { + get {return _name ?? String()} + set {_name = newValue} + } + /// Returns true if `name` has been explicitly set. + var hasName: Bool {return self._name != nil} + /// Clears the value of `name`. Subsequent reads from it will return its default value. + mutating func clearName() {self._name = nil} + + var encryptionKeyPair: SessionProtos_DataMessage.ClosedGroupUpdateV2.KeyPair { + get {return _encryptionKeyPair ?? SessionProtos_DataMessage.ClosedGroupUpdateV2.KeyPair()} + set {_encryptionKeyPair = newValue} + } + /// Returns true if `encryptionKeyPair` has been explicitly set. + var hasEncryptionKeyPair: Bool {return self._encryptionKeyPair != nil} + /// Clears the value of `encryptionKeyPair`. Subsequent reads from it will return its default value. + mutating func clearEncryptionKeyPair() {self._encryptionKeyPair = nil} + + var members: [Data] = [] + + var admins: [Data] = [] + + var wrappers: [SessionProtos_DataMessage.ClosedGroupUpdateV2.KeyPairWrapper] = [] + + var unknownFields = SwiftProtobuf.UnknownStorage() + + enum TypeEnum: SwiftProtobuf.Enum { + typealias RawValue = Int + + /// publicKey, name, encryptionKeyPair, members, admins + case new // = 1 + + /// name, members + case update // = 2 + + /// wrappers + case encryptionKeyPair // = 3 + + init() { + self = .new + } + + init?(rawValue: Int) { + switch rawValue { + case 1: self = .new + case 2: self = .update + case 3: self = .encryptionKeyPair + default: return nil + } + } + + var rawValue: Int { + switch self { + case .new: return 1 + case .update: return 2 + case .encryptionKeyPair: return 3 + } + } + + } + + struct KeyPair { + // SwiftProtobuf.Message conformance is added in an extension below. See the + // `Message` and `Message+*Additions` files in the SwiftProtobuf library for + // methods supported on all messages. + + /// @required + var publicKey: Data { + get {return _publicKey ?? SwiftProtobuf.Internal.emptyData} + set {_publicKey = newValue} + } + /// Returns true if `publicKey` has been explicitly set. + var hasPublicKey: Bool {return self._publicKey != nil} + /// Clears the value of `publicKey`. Subsequent reads from it will return its default value. + mutating func clearPublicKey() {self._publicKey = nil} + + /// @required + var privateKey: Data { + get {return _privateKey ?? SwiftProtobuf.Internal.emptyData} + set {_privateKey = newValue} + } + /// Returns true if `privateKey` has been explicitly set. + var hasPrivateKey: Bool {return self._privateKey != nil} + /// Clears the value of `privateKey`. Subsequent reads from it will return its default value. + mutating func clearPrivateKey() {self._privateKey = nil} + + var unknownFields = SwiftProtobuf.UnknownStorage() + + init() {} + + fileprivate var _publicKey: Data? = nil + fileprivate var _privateKey: Data? = nil + } + + struct KeyPairWrapper { + // SwiftProtobuf.Message conformance is added in an extension below. See the + // `Message` and `Message+*Additions` files in the SwiftProtobuf library for + // methods supported on all messages. + + /// @required + var publicKey: Data { + get {return _publicKey ?? SwiftProtobuf.Internal.emptyData} + set {_publicKey = newValue} + } + /// Returns true if `publicKey` has been explicitly set. + var hasPublicKey: Bool {return self._publicKey != nil} + /// Clears the value of `publicKey`. Subsequent reads from it will return its default value. + mutating func clearPublicKey() {self._publicKey = nil} + + /// @required + var encryptedKeyPair: Data { + get {return _encryptedKeyPair ?? SwiftProtobuf.Internal.emptyData} + set {_encryptedKeyPair = newValue} + } + /// Returns true if `encryptedKeyPair` has been explicitly set. + var hasEncryptedKeyPair: Bool {return self._encryptedKeyPair != nil} + /// Clears the value of `encryptedKeyPair`. Subsequent reads from it will return its default value. + mutating func clearEncryptedKeyPair() {self._encryptedKeyPair = nil} + + var unknownFields = SwiftProtobuf.UnknownStorage() + + init() {} + + fileprivate var _publicKey: Data? = nil + fileprivate var _encryptedKeyPair: Data? = nil + } + + init() {} + + fileprivate var _type: SessionProtos_DataMessage.ClosedGroupUpdateV2.TypeEnum? = nil + fileprivate var _publicKey: Data? = nil + fileprivate var _name: String? = nil + fileprivate var _encryptionKeyPair: SessionProtos_DataMessage.ClosedGroupUpdateV2.KeyPair? = nil + } + struct ClosedGroupUpdate { // SwiftProtobuf.Message conformance is added in an extension below. See the // `Message` and `Message+*Additions` files in the SwiftProtobuf library for @@ -1673,6 +1841,7 @@ struct SessionProtos_DataMessage { fileprivate var _quote: SessionProtos_DataMessage.Quote? = nil fileprivate var _profile: SessionProtos_DataMessage.LokiProfile? = nil fileprivate var _closedGroupUpdate: SessionProtos_DataMessage.ClosedGroupUpdate? = nil + fileprivate var _closedGroupUpdateV2: SessionProtos_DataMessage.ClosedGroupUpdateV2? = nil fileprivate var _publicChatInfo: SessionProtos_PublicChatInfo? = nil } @@ -1698,6 +1867,10 @@ extension SessionProtos_DataMessage.Contact.PostalAddress.TypeEnum: CaseIterable // Support synthesized by the compiler. } +extension SessionProtos_DataMessage.ClosedGroupUpdateV2.TypeEnum: CaseIterable { + // Support synthesized by the compiler. +} + extension SessionProtos_DataMessage.ClosedGroupUpdate.TypeEnum: CaseIterable { // Support synthesized by the compiler. } @@ -3027,6 +3200,12 @@ extension SessionProtos_Content: SwiftProtobuf.Message, SwiftProtobuf._MessageIm 103: .same(proto: "lokiDeviceLinkMessage"), ] + public var isInitialized: Bool { + if let v = self._dataMessage, !v.isInitialized {return false} + if let v = self._syncMessage, !v.isInitialized {return false} + return true + } + mutating func decodeMessage(decoder: inout D) throws { while let fieldNumber = try decoder.nextFieldNumber() { switch fieldNumber { @@ -3481,9 +3660,15 @@ extension SessionProtos_DataMessage: SwiftProtobuf.Message, SwiftProtobuf._Messa 10: .same(proto: "preview"), 101: .same(proto: "profile"), 103: .same(proto: "closedGroupUpdate"), + 104: .same(proto: "closedGroupUpdateV2"), 999: .same(proto: "publicChatInfo"), ] + public var isInitialized: Bool { + if let v = self._closedGroupUpdateV2, !v.isInitialized {return false} + return true + } + mutating func decodeMessage(decoder: inout D) throws { while let fieldNumber = try decoder.nextFieldNumber() { switch fieldNumber { @@ -3499,6 +3684,7 @@ extension SessionProtos_DataMessage: SwiftProtobuf.Message, SwiftProtobuf._Messa case 10: try decoder.decodeRepeatedMessageField(value: &self.preview) case 101: try decoder.decodeSingularMessageField(value: &self._profile) case 103: try decoder.decodeSingularMessageField(value: &self._closedGroupUpdate) + case 104: try decoder.decodeSingularMessageField(value: &self._closedGroupUpdateV2) case 999: try decoder.decodeSingularMessageField(value: &self._publicChatInfo) default: break } @@ -3542,6 +3728,9 @@ extension SessionProtos_DataMessage: SwiftProtobuf.Message, SwiftProtobuf._Messa if let v = self._closedGroupUpdate { try visitor.visitSingularMessageField(value: v, fieldNumber: 103) } + if let v = self._closedGroupUpdateV2 { + try visitor.visitSingularMessageField(value: v, fieldNumber: 104) + } if let v = self._publicChatInfo { try visitor.visitSingularMessageField(value: v, fieldNumber: 999) } @@ -3561,6 +3750,7 @@ extension SessionProtos_DataMessage: SwiftProtobuf.Message, SwiftProtobuf._Messa if lhs.preview != rhs.preview {return false} if lhs._profile != rhs._profile {return false} if lhs._closedGroupUpdate != rhs._closedGroupUpdate {return false} + if lhs._closedGroupUpdateV2 != rhs._closedGroupUpdateV2 {return false} if lhs._publicChatInfo != rhs._publicChatInfo {return false} if lhs.unknownFields != rhs.unknownFields {return false} return true @@ -4090,6 +4280,168 @@ extension SessionProtos_DataMessage.LokiProfile: SwiftProtobuf.Message, SwiftPro } } +extension SessionProtos_DataMessage.ClosedGroupUpdateV2: SwiftProtobuf.Message, SwiftProtobuf._MessageImplementationBase, SwiftProtobuf._ProtoNameProviding { + static let protoMessageName: String = SessionProtos_DataMessage.protoMessageName + ".ClosedGroupUpdateV2" + static let _protobuf_nameMap: SwiftProtobuf._NameMap = [ + 1: .same(proto: "type"), + 2: .same(proto: "publicKey"), + 3: .same(proto: "name"), + 4: .same(proto: "encryptionKeyPair"), + 5: .same(proto: "members"), + 6: .same(proto: "admins"), + 7: .same(proto: "wrappers"), + ] + + public var isInitialized: Bool { + if self._type == nil {return false} + if let v = self._encryptionKeyPair, !v.isInitialized {return false} + if !SwiftProtobuf.Internal.areAllInitialized(self.wrappers) {return false} + return true + } + + mutating func decodeMessage(decoder: inout D) throws { + while let fieldNumber = try decoder.nextFieldNumber() { + switch fieldNumber { + case 1: try decoder.decodeSingularEnumField(value: &self._type) + case 2: try decoder.decodeSingularBytesField(value: &self._publicKey) + case 3: try decoder.decodeSingularStringField(value: &self._name) + case 4: try decoder.decodeSingularMessageField(value: &self._encryptionKeyPair) + case 5: try decoder.decodeRepeatedBytesField(value: &self.members) + case 6: try decoder.decodeRepeatedBytesField(value: &self.admins) + case 7: try decoder.decodeRepeatedMessageField(value: &self.wrappers) + default: break + } + } + } + + func traverse(visitor: inout V) throws { + if let v = self._type { + try visitor.visitSingularEnumField(value: v, fieldNumber: 1) + } + if let v = self._publicKey { + try visitor.visitSingularBytesField(value: v, fieldNumber: 2) + } + if let v = self._name { + try visitor.visitSingularStringField(value: v, fieldNumber: 3) + } + if let v = self._encryptionKeyPair { + try visitor.visitSingularMessageField(value: v, fieldNumber: 4) + } + if !self.members.isEmpty { + try visitor.visitRepeatedBytesField(value: self.members, fieldNumber: 5) + } + if !self.admins.isEmpty { + try visitor.visitRepeatedBytesField(value: self.admins, fieldNumber: 6) + } + if !self.wrappers.isEmpty { + try visitor.visitRepeatedMessageField(value: self.wrappers, fieldNumber: 7) + } + try unknownFields.traverse(visitor: &visitor) + } + + static func ==(lhs: SessionProtos_DataMessage.ClosedGroupUpdateV2, rhs: SessionProtos_DataMessage.ClosedGroupUpdateV2) -> Bool { + if lhs._type != rhs._type {return false} + if lhs._publicKey != rhs._publicKey {return false} + if lhs._name != rhs._name {return false} + if lhs._encryptionKeyPair != rhs._encryptionKeyPair {return false} + if lhs.members != rhs.members {return false} + if lhs.admins != rhs.admins {return false} + if lhs.wrappers != rhs.wrappers {return false} + if lhs.unknownFields != rhs.unknownFields {return false} + return true + } +} + +extension SessionProtos_DataMessage.ClosedGroupUpdateV2.TypeEnum: SwiftProtobuf._ProtoNameProviding { + static let _protobuf_nameMap: SwiftProtobuf._NameMap = [ + 1: .same(proto: "NEW"), + 2: .same(proto: "UPDATE"), + 3: .same(proto: "ENCRYPTION_KEY_PAIR"), + ] +} + +extension SessionProtos_DataMessage.ClosedGroupUpdateV2.KeyPair: SwiftProtobuf.Message, SwiftProtobuf._MessageImplementationBase, SwiftProtobuf._ProtoNameProviding { + static let protoMessageName: String = SessionProtos_DataMessage.ClosedGroupUpdateV2.protoMessageName + ".KeyPair" + static let _protobuf_nameMap: SwiftProtobuf._NameMap = [ + 1: .same(proto: "publicKey"), + 2: .same(proto: "privateKey"), + ] + + public var isInitialized: Bool { + if self._publicKey == nil {return false} + if self._privateKey == nil {return false} + return true + } + + mutating func decodeMessage(decoder: inout D) throws { + while let fieldNumber = try decoder.nextFieldNumber() { + switch fieldNumber { + case 1: try decoder.decodeSingularBytesField(value: &self._publicKey) + case 2: try decoder.decodeSingularBytesField(value: &self._privateKey) + default: break + } + } + } + + func traverse(visitor: inout V) throws { + if let v = self._publicKey { + try visitor.visitSingularBytesField(value: v, fieldNumber: 1) + } + if let v = self._privateKey { + try visitor.visitSingularBytesField(value: v, fieldNumber: 2) + } + try unknownFields.traverse(visitor: &visitor) + } + + static func ==(lhs: SessionProtos_DataMessage.ClosedGroupUpdateV2.KeyPair, rhs: SessionProtos_DataMessage.ClosedGroupUpdateV2.KeyPair) -> Bool { + if lhs._publicKey != rhs._publicKey {return false} + if lhs._privateKey != rhs._privateKey {return false} + if lhs.unknownFields != rhs.unknownFields {return false} + return true + } +} + +extension SessionProtos_DataMessage.ClosedGroupUpdateV2.KeyPairWrapper: SwiftProtobuf.Message, SwiftProtobuf._MessageImplementationBase, SwiftProtobuf._ProtoNameProviding { + static let protoMessageName: String = SessionProtos_DataMessage.ClosedGroupUpdateV2.protoMessageName + ".KeyPairWrapper" + static let _protobuf_nameMap: SwiftProtobuf._NameMap = [ + 1: .same(proto: "publicKey"), + 2: .same(proto: "encryptedKeyPair"), + ] + + public var isInitialized: Bool { + if self._publicKey == nil {return false} + if self._encryptedKeyPair == nil {return false} + return true + } + + mutating func decodeMessage(decoder: inout D) throws { + while let fieldNumber = try decoder.nextFieldNumber() { + switch fieldNumber { + case 1: try decoder.decodeSingularBytesField(value: &self._publicKey) + case 2: try decoder.decodeSingularBytesField(value: &self._encryptedKeyPair) + default: break + } + } + } + + func traverse(visitor: inout V) throws { + if let v = self._publicKey { + try visitor.visitSingularBytesField(value: v, fieldNumber: 1) + } + if let v = self._encryptedKeyPair { + try visitor.visitSingularBytesField(value: v, fieldNumber: 2) + } + try unknownFields.traverse(visitor: &visitor) + } + + static func ==(lhs: SessionProtos_DataMessage.ClosedGroupUpdateV2.KeyPairWrapper, rhs: SessionProtos_DataMessage.ClosedGroupUpdateV2.KeyPairWrapper) -> Bool { + if lhs._publicKey != rhs._publicKey {return false} + if lhs._encryptedKeyPair != rhs._encryptedKeyPair {return false} + if lhs.unknownFields != rhs.unknownFields {return false} + return true + } +} + extension SessionProtos_DataMessage.ClosedGroupUpdate: SwiftProtobuf.Message, SwiftProtobuf._MessageImplementationBase, SwiftProtobuf._ProtoNameProviding { static let protoMessageName: String = SessionProtos_DataMessage.protoMessageName + ".ClosedGroupUpdate" static let _protobuf_nameMap: SwiftProtobuf._NameMap = [ @@ -4346,6 +4698,11 @@ extension SessionProtos_SyncMessage: SwiftProtobuf.Message, SwiftProtobuf._Messa 100: .same(proto: "openGroups"), ] + public var isInitialized: Bool { + if let v = self._sent, !v.isInitialized {return false} + return true + } + mutating func decodeMessage(decoder: inout D) throws { while let fieldNumber = try decoder.nextFieldNumber() { switch fieldNumber { @@ -4425,6 +4782,11 @@ extension SessionProtos_SyncMessage.Sent: SwiftProtobuf.Message, SwiftProtobuf._ 6: .same(proto: "isRecipientUpdate"), ] + public var isInitialized: Bool { + if let v = self._message, !v.isInitialized {return false} + return true + } + mutating func decodeMessage(decoder: inout D) throws { while let fieldNumber = try decoder.nextFieldNumber() { switch fieldNumber { diff --git a/SessionMessagingKit/Protos/SessionProtos.proto b/SessionMessagingKit/Protos/SessionProtos.proto index f16efa267..49d2eb7a2 100644 --- a/SessionMessagingKit/Protos/SessionProtos.proto +++ b/SessionMessagingKit/Protos/SessionProtos.proto @@ -246,7 +246,40 @@ message DataMessage { optional string profilePicture = 2; } - message ClosedGroupUpdate { // Loki + message ClosedGroupUpdateV2 { + + enum Type { + NEW = 1; // publicKey, name, encryptionKeyPair, members, admins + UPDATE = 2; // name, members + ENCRYPTION_KEY_PAIR = 3; // wrappers + } + + message KeyPair { + // @required + required bytes publicKey = 1; + // @required + required bytes privateKey = 2; + } + + message KeyPairWrapper { + // @required + required bytes publicKey = 1; // The public key of the user the key pair is meant for + // @required + required bytes encryptedKeyPair = 2; // The encrypted key pair + } + + // @required + required Type type = 1; + optional bytes publicKey = 2; + optional string name = 3; + optional KeyPair encryptionKeyPair = 4; + repeated bytes members = 5; + repeated bytes admins = 6; + repeated KeyPairWrapper wrappers = 7; + } + + message ClosedGroupUpdate { + enum Type { NEW = 0; // groupPublicKey, name, groupPrivateKey, senderKeys, members, admins INFO = 1; // groupPublicKey, name, senderKeys, members, admins @@ -274,19 +307,20 @@ message DataMessage { optional Type type = 7; } - optional string body = 1; - repeated AttachmentPointer attachments = 2; - optional GroupContext group = 3; - optional uint32 flags = 4; - optional uint32 expireTimer = 5; - optional bytes profileKey = 6; - optional uint64 timestamp = 7; - optional Quote quote = 8; - repeated Contact contact = 9; - repeated Preview preview = 10; - optional LokiProfile profile = 101; // Loki: The current user's profile - optional ClosedGroupUpdate closedGroupUpdate = 103; // Loki - optional PublicChatInfo publicChatInfo = 999; // Loki: Internal public chat info + optional string body = 1; + repeated AttachmentPointer attachments = 2; + optional GroupContext group = 3; + optional uint32 flags = 4; + optional uint32 expireTimer = 5; + optional bytes profileKey = 6; + optional uint64 timestamp = 7; + optional Quote quote = 8; + repeated Contact contact = 9; + repeated Preview preview = 10; + optional LokiProfile profile = 101; // Loki: The current user's profile + optional ClosedGroupUpdate closedGroupUpdate = 103; // Loki + optional ClosedGroupUpdateV2 closedGroupUpdateV2 = 104; // Loki + optional PublicChatInfo publicChatInfo = 999; // Loki: Internal public chat info } message NullMessage { diff --git a/SessionMessagingKit/Sending & Receiving/MessageReceiver+Decryption.swift b/SessionMessagingKit/Sending & Receiving/MessageReceiver+Decryption.swift index 13bc05b60..2c254c0ae 100644 --- a/SessionMessagingKit/Sending & Receiving/MessageReceiver+Decryption.swift +++ b/SessionMessagingKit/Sending & Receiving/MessageReceiver+Decryption.swift @@ -3,24 +3,11 @@ import SessionProtocolKit import SessionUtilitiesKit import Sodium -internal extension MessageReceiver { +extension MessageReceiver { - static func decryptWithSessionProtocol(envelope: SNProtoEnvelope) throws -> (plaintext: Data, senderX25519PublicKey: String) { - guard let ciphertext = envelope.content else { throw Error.noData } - let recipientX25519PrivateKey: Data - let recipientX25519PublicKey: Data - switch envelope.type { - case .unidentifiedSender: - guard let userX25519KeyPair = SNMessagingKitConfiguration.shared.storage.getUserKeyPair() else { throw Error.noUserX25519KeyPair } - recipientX25519PrivateKey = userX25519KeyPair.privateKey - recipientX25519PublicKey = Data(hex: userX25519KeyPair.hexEncodedPublicKey.removing05PrefixIfNeeded()) - case .closedGroupCiphertext: - guard let hexEncodedGroupPublicKey = envelope.source, SNMessagingKitConfiguration.shared.storage.isClosedGroup(hexEncodedGroupPublicKey) else { throw Error.invalidGroupPublicKey } - guard let hexEncodedGroupPrivateKey = SNMessagingKitConfiguration.shared.storage.getClosedGroupPrivateKey(for: hexEncodedGroupPublicKey) else { throw Error.noGroupPrivateKey } - recipientX25519PrivateKey = Data(hex: hexEncodedGroupPrivateKey) - recipientX25519PublicKey = Data(hex: hexEncodedGroupPublicKey.removing05PrefixIfNeeded()) - default: preconditionFailure() - } + internal static func decryptWithSessionProtocol(ciphertext: Data, using x25519KeyPair: ECKeyPair) throws -> (plaintext: Data, senderX25519PublicKey: String) { + let recipientX25519PrivateKey = x25519KeyPair.privateKey + let recipientX25519PublicKey = Data(hex: x25519KeyPair.hexEncodedPublicKey.removing05PrefixIfNeeded()) let sodium = Sodium() let signatureSize = sodium.sign.Bytes let ed25519PublicKeySize = sodium.sign.PublicKeyBytes diff --git a/SessionMessagingKit/Sending & Receiving/MessageReceiver+Handling.swift b/SessionMessagingKit/Sending & Receiving/MessageReceiver+Handling.swift index c0a4b828e..045456410 100644 --- a/SessionMessagingKit/Sending & Receiving/MessageReceiver+Handling.swift +++ b/SessionMessagingKit/Sending & Receiving/MessageReceiver+Handling.swift @@ -11,6 +11,7 @@ extension MessageReceiver { switch message { case let message as ReadReceipt: handleReadReceipt(message, using: transaction) case let message as TypingIndicator: handleTypingIndicator(message, using: transaction) + case let message as ClosedGroupUpdateV2: handleClosedGroupUpdateV2(message, using: transaction) case let message as ClosedGroupUpdate: handleClosedGroupUpdate(message, using: transaction) case let message as ExpirationTimerUpdate: handleExpirationTimerUpdate(message, using: transaction) case let message as VisibleMessage: try handleVisibleMessage(message, associatedWithProto: proto, openGroupID: openGroupID, isBackgroundPoll: isBackgroundPoll, using: transaction) @@ -216,6 +217,14 @@ extension MessageReceiver { return tsIncomingMessageID } + private static func handleClosedGroupUpdateV2(_ message: ClosedGroupUpdateV2, using transaction: Any) { + switch message.kind! { + case .new: handleNewGroupV2(message, using: transaction) + case .update: handleGroupUpdateV2(message, using: transaction) + case .encryptionKeyPair: handleGroupEncryptionKeyPair(message, using: transaction) + } + } + private static func handleClosedGroupUpdate(_ message: ClosedGroupUpdate, using transaction: Any) { switch message.kind! { case .new: handleNewGroup(message, using: transaction) @@ -225,6 +234,133 @@ extension MessageReceiver { } } + // MARK: - V2 + + private static func handleNewGroupV2(_ message: ClosedGroupUpdateV2, using transaction: Any) { + // Prepare + guard case let .new(publicKeyAsData, name, encryptionKeyPair, membersAsData, adminsAsData) = message.kind else { return } + let transaction = transaction as! YapDatabaseReadWriteTransaction + // Unwrap the message + let groupPublicKey = publicKeyAsData.toHexString() + let members = membersAsData.map { $0.toHexString() } + let admins = adminsAsData.map { $0.toHexString() } + // Create the group + let groupID = LKGroupUtilities.getEncodedClosedGroupIDAsData(groupPublicKey) + let group = TSGroupModel(title: name, memberIds: members, image: nil, groupId: groupID, groupType: .closedGroup, adminIds: admins) + let thread: TSGroupThread + if let t = TSGroupThread.fetch(uniqueId: TSGroupThread.threadId(fromGroupId: groupID), transaction: transaction) { + thread = t + thread.setGroupModel(group, with: transaction) + } else { + thread = TSGroupThread.getOrCreateThread(with: group, transaction: transaction) + thread.usesSharedSenderKeys = true + thread.save(with: transaction) + } + // Add the group to the user's set of public keys to poll for + Storage.shared.addClosedGroupPublicKey(groupPublicKey, using: transaction) + Storage.shared.addClosedGroupEncryptionKeyPair(encryptionKeyPair, for: groupPublicKey, using: transaction) + // Notify the PN server + let _ = PushNotificationAPI.performOperation(.subscribe, for: groupPublicKey, publicKey: getUserHexEncodedPublicKey()) + // Notify the user + let infoMessage = TSInfoMessage(timestamp: NSDate.ows_millisecondTimeStamp(), in: thread, messageType: .typeGroupUpdate) + infoMessage.save(with: transaction) + } + + private static func handleGroupUpdateV2(_ message: ClosedGroupUpdateV2, using transaction: Any) { + // Prepare + guard case let .update(name, membersAsData) = message.kind else { return } + let transaction = transaction as! YapDatabaseReadWriteTransaction + // Unwrap the message + guard let groupPublicKey = message.groupPublicKey else { return } + let members = membersAsData.map { $0.toHexString() } + // Get the group + let groupID = LKGroupUtilities.getEncodedClosedGroupIDAsData(groupPublicKey) + let threadID = TSGroupThread.threadId(fromGroupId: groupID) + guard let thread = TSGroupThread.fetch(uniqueId: threadID, transaction: transaction) else { + return SNLog("Ignoring closed group update message for nonexistent group.") + } + let group = thread.groupModel + let oldMembers = group.groupMemberIds + // Check that the sender is a member of the group (before the update) + guard Set(group.groupMemberIds).contains(message.sender!) else { + return SNLog("Ignoring closed group update message from non-member.") + } + // Remove the group from the user's set of public keys to poll for if the current user was removed + let userPublicKey = getUserHexEncodedPublicKey() + let wasCurrentUserRemoved = !members.contains(userPublicKey) + if wasCurrentUserRemoved { + Storage.shared.removeAllClosedGroupEncryptionKeyPairs(for: groupPublicKey, using: transaction) + // Notify the PN server + let _ = PushNotificationAPI.performOperation(.unsubscribe, for: groupPublicKey, publicKey: userPublicKey) + } + // Generate and distribute a new encryption key pair if needed + let wasAnyUserRemoved = (Set(members).intersection(oldMembers) != Set(oldMembers)) + let isCurrentUserAdmin = group.groupAdminIds.contains(getUserHexEncodedPublicKey()) + if wasAnyUserRemoved && isCurrentUserAdmin { + do { + try MessageSender.generateAndSendNewEncryptionKeyPair(for: groupPublicKey, to: Set(members), using: transaction) + } catch { + SNLog("Couldn't distribute new encryption key pair.") + } + } + // Update the group + let newGroupModel = TSGroupModel(title: name, memberIds: members, image: nil, groupId: groupID, groupType: .closedGroup, adminIds: group.groupAdminIds) + thread.setGroupModel(newGroupModel, with: transaction) + // Notify the user if needed + if Set(members) != Set(oldMembers) || name != group.groupName { + let infoMessageType: TSInfoMessageType = wasCurrentUserRemoved ? .typeGroupQuit : .typeGroupUpdate + let updateInfo = group.getInfoStringAboutUpdate(to: newGroupModel) + let infoMessage = TSInfoMessage(timestamp: NSDate.ows_millisecondTimeStamp(), in: thread, messageType: infoMessageType, customMessage: updateInfo) + infoMessage.save(with: transaction) + } + } + + private static func handleGroupEncryptionKeyPair(_ message: ClosedGroupUpdateV2, using transaction: Any) { + // Prepare + guard case let .encryptionKeyPair(wrappers) = message.kind, let groupPublicKey = message.groupPublicKey else { return } + let transaction = transaction as! YapDatabaseReadWriteTransaction + let userPublicKey = getUserHexEncodedPublicKey() + guard let userKeyPair = SNMessagingKitConfiguration.shared.storage.getUserKeyPair() else { + return SNLog("Couldn't find user X25519 key pair.") + } + let groupID = LKGroupUtilities.getEncodedClosedGroupIDAsData(groupPublicKey) + let threadID = TSGroupThread.threadId(fromGroupId: groupID) + guard let thread = TSGroupThread.fetch(uniqueId: threadID, transaction: transaction) else { + return SNLog("Ignoring closed group encryption key pair for nonexistent group.") + } + guard thread.groupModel.groupAdminIds.contains(message.sender!) else { + return SNLog("Ignoring closed group encryption key pair from non-admin.") + } + // Find our wrapper and decrypt it if possible + guard let wrapper = wrappers.first(where: { $0.publicKey == userPublicKey }), let encryptedKeyPair = wrapper.encryptedKeyPair else { return } + let plaintext: Data + do { + plaintext = try MessageReceiver.decryptWithSessionProtocol(ciphertext: encryptedKeyPair, using: userKeyPair).plaintext + } catch { + return SNLog("Couldn't decrypt closed group encryption key pair.") + } + // Parse it + let proto: SNProtoDataMessageClosedGroupUpdateV2KeyPair + do { + proto = try SNProtoDataMessageClosedGroupUpdateV2KeyPair.parseData(plaintext) + } catch { + return SNLog("Couldn't parse closed group encryption key pair.") + } + let keyPair: ECKeyPair + do { + keyPair = try ECKeyPair(publicKeyData: proto.publicKey, privateKeyData: proto.privateKey) + } catch { + return SNLog("Couldn't parse closed group encryption key pair.") + } + // Store it + Storage.shared.addClosedGroupEncryptionKeyPair(keyPair, for: groupPublicKey, using: transaction) + SNLog("Received a new closed group encryption key pair.") + } + + + + // MARK: - V1 + private static func handleNewGroup(_ message: ClosedGroupUpdate, using transaction: Any) { guard case let .new(groupPublicKeyAsData, name, groupPrivateKey, senderKeys, membersAsData, adminsAsData) = message.kind else { return } let transaction = transaction as! YapDatabaseReadWriteTransaction diff --git a/SessionMessagingKit/Sending & Receiving/MessageReceiver.swift b/SessionMessagingKit/Sending & Receiving/MessageReceiver.swift index 9f76f3ab0..92e54db01 100644 --- a/SessionMessagingKit/Sending & Receiving/MessageReceiver.swift +++ b/SessionMessagingKit/Sending & Receiving/MessageReceiver.swift @@ -17,7 +17,7 @@ public enum MessageReceiver { case decryptionFailed // Shared sender keys case invalidGroupPublicKey - case noGroupPrivateKey + case noGroupKeyPair case sharedSecretGenerationFailed public var isRetryable: Bool { @@ -43,7 +43,7 @@ public enum MessageReceiver { case .decryptionFailed: return "Decryption failed." // Shared sender keys case .invalidGroupPublicKey: return "Invalid group public key." - case .noGroupPrivateKey: return "Missing group private key." + case .noGroupKeyPair: return "Missing group key pair." case .sharedSecretGenerationFailed: return "Couldn't generate a shared secret." } } @@ -58,17 +58,37 @@ public enum MessageReceiver { guard !Set(storage.getReceivedMessageTimestamps(using: transaction)).contains(envelope.timestamp) else { throw Error.duplicateMessage } storage.addReceivedMessageTimestamp(envelope.timestamp, using: transaction) // Decrypt the contents - let plaintext: Data - let sender: String + guard let ciphertext = envelope.content else { throw Error.noData } + var plaintext: Data! + var sender: String! var groupPublicKey: String? = nil if isOpenGroupMessage { (plaintext, sender) = (envelope.content!, envelope.source!) } else { switch envelope.type { case .unidentifiedSender: - (plaintext, sender) = try decryptWithSessionProtocol(envelope: envelope) + guard let userX25519KeyPair = SNMessagingKitConfiguration.shared.storage.getUserKeyPair() else { throw Error.noUserX25519KeyPair } + (plaintext, sender) = try decryptWithSessionProtocol(ciphertext: ciphertext, using: userX25519KeyPair) case .closedGroupCiphertext: - (plaintext, sender) = try decryptWithSessionProtocol(envelope: envelope) + 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 + } + } + } + try decrypt() groupPublicKey = envelope.source default: throw Error.unknownEnvelopeType } @@ -89,6 +109,7 @@ public enum MessageReceiver { let message: Message? = { if let readReceipt = ReadReceipt.fromProto(proto) { return readReceipt } if let typingIndicator = TypingIndicator.fromProto(proto) { return typingIndicator } + if let closedGroupUpdate = ClosedGroupUpdateV2.fromProto(proto) { return closedGroupUpdate } if let closedGroupUpdate = ClosedGroupUpdate.fromProto(proto) { return closedGroupUpdate } if let expirationTimerUpdate = ExpirationTimerUpdate.fromProto(proto) { return expirationTimerUpdate } if let visibleMessage = VisibleMessage.fromProto(proto) { return visibleMessage } diff --git a/SessionMessagingKit/Sending & Receiving/MessageSender+ClosedGroups.swift b/SessionMessagingKit/Sending & Receiving/MessageSender+ClosedGroups.swift index ccaae98b9..5ac992960 100644 --- a/SessionMessagingKit/Sending & Receiving/MessageSender+ClosedGroups.swift +++ b/SessionMessagingKit/Sending & Receiving/MessageSender+ClosedGroups.swift @@ -3,6 +3,165 @@ import SessionProtocolKit extension MessageSender : SharedSenderKeysDelegate { + // MARK: - V2 + + public static func createV2ClosedGroup(name: String, members: Set, transaction: YapDatabaseReadWriteTransaction) -> Promise { + // Prepare + var members = members + let userPublicKey = getUserHexEncodedPublicKey() + // Generate the group's public key + let groupPublicKey = Curve25519.generateKeyPair().hexEncodedPublicKey // Includes the "05" prefix + // Generate the key pair that'll be used for encryption and decryption + let encryptionKeyPair = Curve25519.generateKeyPair() + // Ensure the current user is included in the member list + members.insert(userPublicKey) + let membersAsData = members.map { Data(hex: $0) } + // Create the group + let admins = [ userPublicKey ] + let adminsAsData = admins.map { Data(hex: $0) } + let groupID = LKGroupUtilities.getEncodedClosedGroupIDAsData(groupPublicKey) + let group = TSGroupModel(title: name, memberIds: [String](members), image: nil, groupId: groupID, groupType: .closedGroup, adminIds: admins) + let thread = TSGroupThread.getOrCreateThread(with: group, transaction: transaction) + thread.usesSharedSenderKeys = true // TODO: We should be able to safely deprecate this + thread.save(with: transaction) + // Send a closed group update message to all members individually + var promises: [Promise] = [] + for member in members { + guard member != userPublicKey else { continue } + let thread = TSContactThread.getOrCreateThread(withContactId: member, transaction: transaction) + thread.save(with: transaction) + let closedGroupUpdateKind = ClosedGroupUpdateV2.Kind.new(publicKey: Data(hex: groupPublicKey), name: name, + encryptionKeyPair: encryptionKeyPair, members: membersAsData, admins: adminsAsData) + let closedGroupUpdate = ClosedGroupUpdateV2(kind: closedGroupUpdateKind) + let promise = MessageSender.sendNonDurably(closedGroupUpdate, in: thread, using: transaction) + promises.append(promise) + } + // Add the group to the user's set of public keys to poll for + Storage.shared.addClosedGroupPublicKey(groupPublicKey, using: transaction) + Storage.shared.addClosedGroupEncryptionKeyPair(encryptionKeyPair, for: groupPublicKey, using: transaction) + // Notify the PN server + promises.append(PushNotificationAPI.performOperation(.subscribe, for: groupPublicKey, publicKey: userPublicKey)) + // Notify the user + let infoMessage = TSInfoMessage(timestamp: NSDate.ows_millisecondTimeStamp(), in: thread, messageType: .typeGroupUpdate) + infoMessage.save(with: transaction) + // Return + return when(fulfilled: promises).map2 { thread } + } + + public static func updateV2(_ groupPublicKey: String, with members: Set, name: String, transaction: YapDatabaseReadWriteTransaction) throws { + // Prepare + let userPublicKey = getUserHexEncodedPublicKey() + let groupID = LKGroupUtilities.getEncodedClosedGroupIDAsData(groupPublicKey) + let threadID = TSGroupThread.threadId(fromGroupId: groupID) + guard let thread = TSGroupThread.fetch(uniqueId: threadID, transaction: transaction) else { + SNLog("Can't update nonexistent closed group.") + throw Error.noThread + } + let group = thread.groupModel + let oldMembers = Set(group.groupMemberIds) + let newMembers = members.subtracting(oldMembers) + let membersAsData = members.map { Data(hex: $0) } + let admins = group.groupAdminIds + let adminsAsData = admins.map { Data(hex: $0) } + guard let encryptionKeyPair = Storage.shared.getLatestClosedGroupEncryptionKeyPair(for: groupPublicKey) else { + SNLog("Couldn't get key pair for closed group.") + throw Error.noKeyPair + } + let removedMembers = oldMembers.subtracting(members) + guard !removedMembers.contains(admins.first!) else { + SNLog("Can't remove admin from closed group.") + throw Error.invalidClosedGroupUpdate + } + let isUserLeaving = removedMembers.contains(userPublicKey) + if isUserLeaving && (removedMembers.count != 1 || !newMembers.isEmpty) { + SNLog("Can't remove self and add or remove others simultaneously.") + throw Error.invalidClosedGroupUpdate + } + // Send the update to the group + let mainClosedGroupUpdate = ClosedGroupUpdateV2(kind: .update(name: name, members: membersAsData)) + if isUserLeaving { + let _ = MessageSender.sendNonDurably(mainClosedGroupUpdate, in: thread, using: transaction).done { + SNMessagingKitConfiguration.shared.storage.write { transaction in + // Remove the group private key and unsubscribe from PNs + Storage.shared.removeAllClosedGroupEncryptionKeyPairs(for: groupPublicKey, using: transaction) + let _ = PushNotificationAPI.performOperation(.unsubscribe, for: groupPublicKey, publicKey: userPublicKey) + } + } + } else { + MessageSender.send(mainClosedGroupUpdate, in: thread, using: transaction) + // Generate and distribute a new encryption key pair if needed + let wasAnyUserRemoved = !removedMembers.isEmpty + let isCurrentUserAdmin = group.groupAdminIds.contains(getUserHexEncodedPublicKey()) + if wasAnyUserRemoved && isCurrentUserAdmin { + try generateAndSendNewEncryptionKeyPair(for: groupPublicKey, to: members.subtracting(newMembers), using: transaction) + } + // Send closed group update messages to any new members individually + if !newMembers.isEmpty { + for member in newMembers { + let thread = TSContactThread.getOrCreateThread(withContactId: member, transaction: transaction) + thread.save(with: transaction) + let closedGroupUpdateKind = ClosedGroupUpdateV2.Kind.new(publicKey: Data(hex: groupPublicKey), name: name, + encryptionKeyPair: encryptionKeyPair, members: membersAsData, admins: adminsAsData) + let closedGroupUpdate = ClosedGroupUpdateV2(kind: closedGroupUpdateKind) + MessageSender.send(closedGroupUpdate, in: thread, using: transaction) + } + } + } + // Update the group + let newGroupModel = TSGroupModel(title: name, memberIds: [String](members), image: nil, groupId: groupID, groupType: .closedGroup, adminIds: admins) + thread.setGroupModel(newGroupModel, with: transaction) + // Notify the user + let updateInfo = group.getInfoStringAboutUpdate(to: newGroupModel) + let infoMessage = TSInfoMessage(timestamp: NSDate.ows_millisecondTimeStamp(), in: thread, messageType: .typeGroupUpdate, customMessage: updateInfo) + infoMessage.save(with: transaction) + } + + public static func leaveV2(_ groupPublicKey: String, using transaction: YapDatabaseReadWriteTransaction) throws { + let groupID = LKGroupUtilities.getEncodedClosedGroupIDAsData(groupPublicKey) + let threadID = TSGroupThread.threadId(fromGroupId: groupID) + guard let thread = TSGroupThread.fetch(uniqueId: threadID, transaction: transaction) else { + SNLog("Can't leave nonexistent closed group.") + throw Error.noThread + } + let group = thread.groupModel + var newMembers = Set(group.groupMemberIds) + newMembers.remove(getUserHexEncodedPublicKey()) + return try updateV2(groupPublicKey, with: newMembers, name: group.groupName!, transaction: transaction) + } + + public static func generateAndSendNewEncryptionKeyPair(for groupPublicKey: String, to targetMembers: Set, using transaction: Any) throws { + // Prepare + let transaction = transaction as! YapDatabaseReadWriteTransaction + let groupID = LKGroupUtilities.getEncodedClosedGroupIDAsData(groupPublicKey) + let threadID = TSGroupThread.threadId(fromGroupId: groupID) + guard let thread = TSGroupThread.fetch(uniqueId: threadID, transaction: transaction) else { + SNLog("Can't distribute new encryption key pair for nonexistent closed group.") + throw Error.noThread + } + guard thread.groupModel.groupAdminIds.contains(getUserHexEncodedPublicKey()) else { + SNLog("Can't distribute new encryption key pair as a non-admin.") + throw Error.invalidClosedGroupUpdate + } + // Generate the new encryption key pair + let newKeyPair = Curve25519.generateKeyPair() + // Store it + Storage.shared.addClosedGroupEncryptionKeyPair(newKeyPair, for: groupPublicKey, using: transaction) + // Distribute it + let proto = try SNProtoDataMessageClosedGroupUpdateV2KeyPair.builder(publicKey: newKeyPair.publicKey, + privateKey: newKeyPair.privateKey).build() + let plaintext = try proto.serializedData() + let wrappers = try targetMembers.compactMap { publicKey -> ClosedGroupUpdateV2.KeyPairWrapper in + let ciphertext = try MessageSender.encryptWithSessionProtocol(plaintext, for: publicKey) + return ClosedGroupUpdateV2.KeyPairWrapper(publicKey: publicKey, encryptedKeyPair: ciphertext) + } + let closedGroupUpdate = ClosedGroupUpdateV2(kind: .encryptionKeyPair(wrappers)) + MessageSender.send(closedGroupUpdate, in: thread, using: transaction) + } + + + + // MARK: - V1 + public static func createClosedGroup(name: String, members: Set, transaction: YapDatabaseReadWriteTransaction) -> Promise { // Prepare var members = members @@ -67,7 +226,7 @@ extension MessageSender : SharedSenderKeysDelegate { let adminsAsData = admins.map { Data(hex: $0) } guard let groupPrivateKey = Storage.shared.getClosedGroupPrivateKey(for: groupPublicKey) else { SNLog("Couldn't get private key for closed group.") - return Promise(error: Error.noPrivateKey) + return Promise(error: Error.noKeyPair) } let wasAnyUserRemoved = Set(members).intersection(oldMembers) != oldMembers let removedMembers = oldMembers.subtracting(members) diff --git a/SessionMessagingKit/Sending & Receiving/MessageSender+Encryption.swift b/SessionMessagingKit/Sending & Receiving/MessageSender+Encryption.swift index 323f76c48..ed61dbd41 100644 --- a/SessionMessagingKit/Sending & Receiving/MessageSender+Encryption.swift +++ b/SessionMessagingKit/Sending & Receiving/MessageSender+Encryption.swift @@ -2,9 +2,9 @@ import SessionProtocolKit import SessionUtilitiesKit import Sodium -internal extension MessageSender { +extension MessageSender { - static func encryptWithSessionProtocol(_ plaintext: Data, for recipientHexEncodedX25519PublicKey: String) throws -> Data { + internal static func encryptWithSessionProtocol(_ plaintext: Data, for recipientHexEncodedX25519PublicKey: String) throws -> Data { guard let userED25519KeyPair = SNMessagingKitConfiguration.shared.storage.getUserED25519KeyPair() else { throw Error.noUserED25519KeyPair } let recipientX25519PublicKey = Data(hex: recipientHexEncodedX25519PublicKey.removing05PrefixIfNeeded()) let sodium = Sodium() diff --git a/SessionMessagingKit/Sending & Receiving/MessageSender.swift b/SessionMessagingKit/Sending & Receiving/MessageSender.swift index b6a517776..15248ca2f 100644 --- a/SessionMessagingKit/Sending & Receiving/MessageSender.swift +++ b/SessionMessagingKit/Sending & Receiving/MessageSender.swift @@ -16,7 +16,7 @@ public final class MessageSender : NSObject { case encryptionFailed // Closed groups case noThread - case noPrivateKey + case noKeyPair case invalidClosedGroupUpdate internal var isRetryable: Bool { @@ -37,7 +37,7 @@ public final class MessageSender : NSObject { case .encryptionFailed: return "Couldn't encrypt message." // Closed groups case .noThread: return "Couldn't find a thread associated with the given group public key." - case .noPrivateKey: return "Couldn't find a private key associated with the given group public key." + case .noKeyPair: return "Couldn't find a private key associated with the given group public key." case .invalidClosedGroupUpdate: return "Invalid group update." } } @@ -212,7 +212,11 @@ public final class MessageSender : NSObject { } let recipient = message.recipient! let base64EncodedData = wrappedMessage.base64EncodedString() - guard let (timestamp, nonce) = ProofOfWork.calculate(ttl: type(of: message).ttl, publicKey: recipient, data: base64EncodedData) else { + var ttl = type(of: message).ttl + if let closedGroupUpdate = message as? ClosedGroupUpdateV2, case .encryptionKeyPair = closedGroupUpdate.kind! { + ttl = 30 * 24 * 60 * 60 * 1000 + } + guard let (timestamp, nonce) = ProofOfWork.calculate(ttl: ttl, publicKey: recipient, data: base64EncodedData) else { SNLog("Proof of work calculation failed.") handleFailure(with: Error.proofOfWorkCalculationFailed, using: transaction) return promise diff --git a/Signal.xcodeproj/project.pbxproj b/Signal.xcodeproj/project.pbxproj index b8c28819c..d68763535 100644 --- a/Signal.xcodeproj/project.pbxproj +++ b/Signal.xcodeproj/project.pbxproj @@ -516,6 +516,7 @@ C3471ED42555386B00297E91 /* AESGCM.swift in Sources */ = {isa = PBXBuildFile; fileRef = C3C2A5D72553860B00C340D1 /* AESGCM.swift */; }; C3471F4C25553AB000297E91 /* MessageReceiver+Decryption.swift in Sources */ = {isa = PBXBuildFile; fileRef = C3471F4B25553AB000297E91 /* MessageReceiver+Decryption.swift */; }; C3471FA42555439E00297E91 /* Notification+MessageSender.swift in Sources */ = {isa = PBXBuildFile; fileRef = C3471FA32555439E00297E91 /* Notification+MessageSender.swift */; }; + C34A977425A3E34A00852C71 /* ClosedGroupUpdateV2.swift in Sources */ = {isa = PBXBuildFile; fileRef = C34A977325A3E34A00852C71 /* ClosedGroupUpdateV2.swift */; }; C34C8F7423A7830B00D82669 /* SpaceMono-Bold.ttf in Resources */ = {isa = PBXBuildFile; fileRef = C34C8F7323A7830A00D82669 /* SpaceMono-Bold.ttf */; }; C352A2F525574B4700338F3E /* Job.swift in Sources */ = {isa = PBXBuildFile; fileRef = C352A2F425574B4700338F3E /* Job.swift */; }; C352A2FF25574B6300338F3E /* MessageSendJob.swift in Sources */ = {isa = PBXBuildFile; fileRef = C352A2FE25574B6300338F3E /* MessageSendJob.swift */; }; @@ -1524,6 +1525,7 @@ C3471ECA2555356A00297E91 /* MessageSender+Encryption.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "MessageSender+Encryption.swift"; sourceTree = ""; }; C3471F4B25553AB000297E91 /* MessageReceiver+Decryption.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "MessageReceiver+Decryption.swift"; sourceTree = ""; }; C3471FA32555439E00297E91 /* Notification+MessageSender.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Notification+MessageSender.swift"; sourceTree = ""; }; + C34A977325A3E34A00852C71 /* ClosedGroupUpdateV2.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ClosedGroupUpdateV2.swift; sourceTree = ""; }; C34C8F7323A7830A00D82669 /* SpaceMono-Bold.ttf */ = {isa = PBXFileReference; lastKnownFileType = file; path = "SpaceMono-Bold.ttf"; sourceTree = ""; }; C352A2F425574B4700338F3E /* Job.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Job.swift; sourceTree = ""; }; C352A2FE25574B6300338F3E /* MessageSendJob.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessageSendJob.swift; sourceTree = ""; }; @@ -2548,6 +2550,7 @@ C300A5BC2554B00D00555489 /* ReadReceipt.swift */, C300A5D22554B05A00555489 /* TypingIndicator.swift */, C300A5DC2554B06600555489 /* ClosedGroupUpdate.swift */, + C34A977325A3E34A00852C71 /* ClosedGroupUpdateV2.swift */, C300A5E62554B07300555489 /* ExpirationTimerUpdate.swift */, ); path = "Control Messages"; @@ -4801,6 +4804,7 @@ C3471FA42555439E00297E91 /* Notification+MessageSender.swift in Sources */, C32C5BEF256DC8EE003C73A2 /* OWSDisappearingMessagesJob.m in Sources */, C3A7222A2558C1E40043A11F /* DotNetAPI.swift in Sources */, + C34A977425A3E34A00852C71 /* ClosedGroupUpdateV2.swift in Sources */, C32C5E97256DE0CB003C73A2 /* OWSPrimaryStorage.m in Sources */, C32C5EB9256DE130003C73A2 /* OWSQuotedReplyModel+Conversion.swift in Sources */, C3A71D1F25589AC30043A11F /* WebSocketResources.pb.swift in Sources */,