|
|
@ -136,6 +136,15 @@ public final class ClosedGroupsProtocol : NSObject {
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
// Establish sessions if needed
|
|
|
|
// Establish sessions if needed
|
|
|
|
establishSessionsIfNeeded(with: [String](members), using: transaction)
|
|
|
|
establishSessionsIfNeeded(with: [String](members), using: transaction)
|
|
|
|
|
|
|
|
// Send closed group update messages to any new members using established channels
|
|
|
|
|
|
|
|
for member in newMembers {
|
|
|
|
|
|
|
|
let thread = TSContactThread.getOrCreateThread(withContactId: member, transaction: transaction)
|
|
|
|
|
|
|
|
thread.save(with: transaction)
|
|
|
|
|
|
|
|
let closedGroupUpdateMessageKind = ClosedGroupUpdateMessage.Kind.new(groupPublicKey: Data(hex: groupPublicKey), name: name,
|
|
|
|
|
|
|
|
groupPrivateKey: Data(hex: groupPrivateKey), senderKeys: [], members: membersAsData, admins: adminsAsData)
|
|
|
|
|
|
|
|
let closedGroupUpdateMessage = ClosedGroupUpdateMessage(thread: thread, kind: closedGroupUpdateMessageKind)
|
|
|
|
|
|
|
|
messageSenderJobQueue.add(message: closedGroupUpdateMessage, transaction: transaction)
|
|
|
|
|
|
|
|
}
|
|
|
|
// Send out the user's new ratchet to all members (minus the removed ones) using established channels
|
|
|
|
// Send out the user's new ratchet to all members (minus the removed ones) using established channels
|
|
|
|
let userRatchet = SharedSenderKeysImplementation.shared.generateRatchet(for: groupPublicKey, senderPublicKey: userPublicKey, using: transaction)
|
|
|
|
let userRatchet = SharedSenderKeysImplementation.shared.generateRatchet(for: groupPublicKey, senderPublicKey: userPublicKey, using: transaction)
|
|
|
|
let userSenderKey = ClosedGroupSenderKey(chainKey: Data(hex: userRatchet.chainKey), keyIndex: userRatchet.keyIndex, publicKey: Data(hex: userPublicKey))
|
|
|
|
let userSenderKey = ClosedGroupSenderKey(chainKey: Data(hex: userRatchet.chainKey), keyIndex: userRatchet.keyIndex, publicKey: Data(hex: userPublicKey))
|
|
|
@ -150,7 +159,7 @@ public final class ClosedGroupsProtocol : NSObject {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
} else if !newMembers.isEmpty {
|
|
|
|
seal.fulfill(())
|
|
|
|
seal.fulfill(())
|
|
|
|
// Generate ratchets for any new members
|
|
|
|
// Generate ratchets for any new members
|
|
|
|
newSenderKeys = newMembers.map { publicKey in
|
|
|
|
newSenderKeys = newMembers.map { publicKey in
|
|
|
@ -162,7 +171,6 @@ public final class ClosedGroupsProtocol : NSObject {
|
|
|
|
members: membersAsData, admins: adminsAsData)
|
|
|
|
members: membersAsData, admins: adminsAsData)
|
|
|
|
let closedGroupUpdateMessage = ClosedGroupUpdateMessage(thread: thread, kind: closedGroupUpdateMessageKind)
|
|
|
|
let closedGroupUpdateMessage = ClosedGroupUpdateMessage(thread: thread, kind: closedGroupUpdateMessageKind)
|
|
|
|
messageSenderJobQueue.add(message: closedGroupUpdateMessage, transaction: transaction)
|
|
|
|
messageSenderJobQueue.add(message: closedGroupUpdateMessage, transaction: transaction)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Establish sessions if needed
|
|
|
|
// Establish sessions if needed
|
|
|
|
establishSessionsIfNeeded(with: [String](newMembers), using: transaction)
|
|
|
|
establishSessionsIfNeeded(with: [String](newMembers), using: transaction)
|
|
|
|
// Send closed group update messages to the new members using established channels
|
|
|
|
// Send closed group update messages to the new members using established channels
|
|
|
@ -176,6 +184,14 @@ public final class ClosedGroupsProtocol : NSObject {
|
|
|
|
let closedGroupUpdateMessage = ClosedGroupUpdateMessage(thread: thread, kind: closedGroupUpdateMessageKind)
|
|
|
|
let closedGroupUpdateMessage = ClosedGroupUpdateMessage(thread: thread, kind: closedGroupUpdateMessageKind)
|
|
|
|
messageSenderJobQueue.add(message: closedGroupUpdateMessage, transaction: transaction)
|
|
|
|
messageSenderJobQueue.add(message: closedGroupUpdateMessage, transaction: transaction)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
seal.fulfill(())
|
|
|
|
|
|
|
|
let allSenderKeys = Storage.getAllClosedGroupSenderKeys(for: groupPublicKey)
|
|
|
|
|
|
|
|
let closedGroupUpdateMessageKind = ClosedGroupUpdateMessage.Kind.info(groupPublicKey: Data(hex: groupPublicKey), name: name,
|
|
|
|
|
|
|
|
senderKeys: [ClosedGroupSenderKey](allSenderKeys), members: membersAsData, admins: adminsAsData)
|
|
|
|
|
|
|
|
let closedGroupUpdateMessage = ClosedGroupUpdateMessage(thread: thread, kind: closedGroupUpdateMessageKind)
|
|
|
|
|
|
|
|
messageSenderJobQueue.add(message: closedGroupUpdateMessage, transaction: transaction)
|
|
|
|
|
|
|
|
}
|
|
|
|
// Update the group
|
|
|
|
// Update the group
|
|
|
|
let newGroupModel = TSGroupModel(title: name, memberIds: [String](members), image: nil, groupId: groupID, groupType: .closedGroup, adminIds: admins)
|
|
|
|
let newGroupModel = TSGroupModel(title: name, memberIds: [String](members), image: nil, groupId: groupID, groupType: .closedGroup, adminIds: admins)
|
|
|
|
thread.setGroupModel(newGroupModel, with: transaction)
|
|
|
|
thread.setGroupModel(newGroupModel, with: transaction)
|
|
|
@ -265,7 +281,7 @@ public final class ClosedGroupsProtocol : NSObject {
|
|
|
|
let missingSenderKeys = Set(members).subtracting(senderKeys.map { $0.publicKey.toHexString() })
|
|
|
|
let missingSenderKeys = Set(members).subtracting(senderKeys.map { $0.publicKey.toHexString() })
|
|
|
|
let userPublicKey = getUserHexEncodedPublicKey()
|
|
|
|
let userPublicKey = getUserHexEncodedPublicKey()
|
|
|
|
if missingSenderKeys.contains(userPublicKey) {
|
|
|
|
if missingSenderKeys.contains(userPublicKey) {
|
|
|
|
establishSessionsIfNeeded(with: [String](missingSenderKeys), using: transaction)
|
|
|
|
establishSessionsIfNeeded(with: [String](members), using: transaction)
|
|
|
|
let userRatchet = SharedSenderKeysImplementation.shared.generateRatchet(for: groupPublicKey, senderPublicKey: userPublicKey, using: transaction)
|
|
|
|
let userRatchet = SharedSenderKeysImplementation.shared.generateRatchet(for: groupPublicKey, senderPublicKey: userPublicKey, using: transaction)
|
|
|
|
let userSenderKey = ClosedGroupSenderKey(chainKey: Data(hex: userRatchet.chainKey), keyIndex: userRatchet.keyIndex, publicKey: Data(hex: userPublicKey))
|
|
|
|
let userSenderKey = ClosedGroupSenderKey(chainKey: Data(hex: userRatchet.chainKey), keyIndex: userRatchet.keyIndex, publicKey: Data(hex: userPublicKey))
|
|
|
|
for member in members {
|
|
|
|
for member in members {
|
|
|
@ -283,9 +299,15 @@ public final class ClosedGroupsProtocol : NSObject {
|
|
|
|
// Create the group
|
|
|
|
// Create the group
|
|
|
|
let groupID = LKGroupUtilities.getEncodedClosedGroupIDAsData(groupPublicKey)
|
|
|
|
let groupID = LKGroupUtilities.getEncodedClosedGroupIDAsData(groupPublicKey)
|
|
|
|
let group = TSGroupModel(title: name, memberIds: members, image: nil, groupId: groupID, groupType: .closedGroup, adminIds: admins)
|
|
|
|
let group = TSGroupModel(title: name, memberIds: members, image: nil, groupId: groupID, groupType: .closedGroup, adminIds: admins)
|
|
|
|
let thread = TSGroupThread.getOrCreateThread(with: group, transaction: transaction)
|
|
|
|
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.usesSharedSenderKeys = true
|
|
|
|
thread.save(with: transaction)
|
|
|
|
thread.save(with: transaction)
|
|
|
|
|
|
|
|
}
|
|
|
|
SSKEnvironment.shared.profileManager.addThread(toProfileWhitelist: thread)
|
|
|
|
SSKEnvironment.shared.profileManager.addThread(toProfileWhitelist: thread)
|
|
|
|
// Add the group to the user's set of public keys to poll for
|
|
|
|
// Add the group to the user's set of public keys to poll for
|
|
|
|
Storage.setClosedGroupPrivateKey(groupPrivateKey.toHexString(), for: groupPublicKey, using: transaction)
|
|
|
|
Storage.setClosedGroupPrivateKey(groupPrivateKey.toHexString(), for: groupPublicKey, using: transaction)
|
|
|
@ -389,7 +411,9 @@ public final class ClosedGroupsProtocol : NSObject {
|
|
|
|
// Respond to the request
|
|
|
|
// Respond to the request
|
|
|
|
print("[Loki] Responding to sender key request from: \(senderPublicKey).")
|
|
|
|
print("[Loki] Responding to sender key request from: \(senderPublicKey).")
|
|
|
|
SessionManagementProtocol.sendSessionRequestIfNeeded(to: senderPublicKey, using: transaction) // This internally takes care of multi device
|
|
|
|
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 userSenderKey = ClosedGroupSenderKey(chainKey: Data(hex: userRatchet.chainKey), keyIndex: userRatchet.keyIndex, publicKey: Data(hex: userPublicKey))
|
|
|
|
let thread = TSContactThread.getOrCreateThread(withContactId: senderPublicKey, transaction: transaction)
|
|
|
|
let thread = TSContactThread.getOrCreateThread(withContactId: senderPublicKey, transaction: transaction)
|
|
|
|
thread.save(with: transaction)
|
|
|
|
thread.save(with: transaction)
|
|
|
@ -402,23 +426,9 @@ public final class ClosedGroupsProtocol : NSObject {
|
|
|
|
private static func handleSenderKeyMessage(_ closedGroupUpdate: SSKProtoDataMessageClosedGroupUpdate, from senderPublicKey: String, using transaction: YapDatabaseReadWriteTransaction) {
|
|
|
|
private static func handleSenderKeyMessage(_ closedGroupUpdate: SSKProtoDataMessageClosedGroupUpdate, from senderPublicKey: String, using transaction: YapDatabaseReadWriteTransaction) {
|
|
|
|
// Prepare
|
|
|
|
// Prepare
|
|
|
|
let groupPublicKey = closedGroupUpdate.groupPublicKey.toHexString()
|
|
|
|
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 {
|
|
|
|
guard let senderKey = closedGroupUpdate.senderKeys.first else {
|
|
|
|
return print("[Loki] Ignoring invalid closed group sender key.")
|
|
|
|
return print("[Loki] Ignoring invalid closed group sender key.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Check that the requesting user is a member of the group
|
|
|
|
|
|
|
|
var membersAndLinkedDevices: Set<String> = 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 membersAndLinkedDevices.contains(senderPublicKey) else {
|
|
|
|
|
|
|
|
return print("[Loki] Ignoring closed group sender key from non-member.")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
guard senderKey.publicKey.toHexString() == senderPublicKey else {
|
|
|
|
guard senderKey.publicKey.toHexString() == senderPublicKey else {
|
|
|
|
return print("[Loki] Ignoring invalid closed group sender key.")
|
|
|
|
return print("[Loki] Ignoring invalid closed group sender key.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|