WIP closed group fix

This commit is contained in:
Ryan ZHAO 2021-02-11 16:57:43 +11:00
parent 5dadb89225
commit a5447e77e7
3 changed files with 31 additions and 158 deletions

View File

@ -93,20 +93,21 @@ object MessageReceiver {
} }
} }
groupPublicKey = envelope.source groupPublicKey = envelope.source
try { decrypt()
decrypt() // try {
} catch(error: Exception) { // decrypt()
val now = System.currentTimeMillis() // } catch(error: Exception) {
var shouldRequestEncryptionKeyPair = true // val now = System.currentTimeMillis()
lastEncryptionKeyPairRequest[groupPublicKey!!]?.let { // var shouldRequestEncryptionKeyPair = true
shouldRequestEncryptionKeyPair = now - it > 30 * 1000 // lastEncryptionKeyPairRequest[groupPublicKey!!]?.let {
} // shouldRequestEncryptionKeyPair = now - it > 30 * 1000
if (shouldRequestEncryptionKeyPair) { // }
MessageSender.requestEncryptionKeyPair(groupPublicKey) // if (shouldRequestEncryptionKeyPair) {
lastEncryptionKeyPairRequest[groupPublicKey] = now // MessageSender.requestEncryptionKeyPair(groupPublicKey)
} // lastEncryptionKeyPairRequest[groupPublicKey] = now
throw error // }
} // throw error
// }
} }
else -> throw Error.UnknownEnvelopeType else -> throw Error.UnknownEnvelopeType
} }

View File

@ -379,12 +379,16 @@ private fun MessageReceiver.handleClosedGroupMembersAdded(message: ClosedGroupCo
val members = group.members.map { it.serialize() } val members = group.members.map { it.serialize() }
val admins = group.admins.map { it.serialize() } val admins = group.admins.map { it.serialize() }
// Users that are part of this remove update
val updateMembers = kind.members.map { it.toByteArray().toHexString() } val updateMembers = kind.members.map { it.toByteArray().toHexString() }
// newMembers to save is old members minus removed members
val newMembers = members + updateMembers val newMembers = members + updateMembers
storage.updateMembers(groupID, newMembers.map { Address.fromSerialized(it) }) storage.updateMembers(groupID, newMembers.map { Address.fromSerialized(it) })
// Send the latest encryption key pair to the added members if the current user is the admin of the group
val isCurrentUserAdmin = admins.contains(storage.getUserPublicKey()!!)
if (isCurrentUserAdmin) {
for (member in updateMembers) {
MessageSender.sendLatestEncryptionKeyPair(member, groupPublicKey)
}
}
storage.insertIncomingInfoMessage(context, senderPublicKey, groupID, SignalServiceProtos.GroupContext.Type.UPDATE, SignalServiceGroup.Type.UPDATE, name, members, admins) storage.insertIncomingInfoMessage(context, senderPublicKey, groupID, SignalServiceProtos.GroupContext.Type.UPDATE, SignalServiceGroup.Type.UPDATE, name, members, admins)
} }

View File

@ -243,6 +243,7 @@ fun MessageSender.generateAndSendNewEncryptionKeyPair(groupPublicKey: String, ta
} }
} }
/// Note: Shouldn't currently be in use.
fun MessageSender.requestEncryptionKeyPair(groupPublicKey: String) { fun MessageSender.requestEncryptionKeyPair(groupPublicKey: String) {
val storage = MessagingConfiguration.shared.storage val storage = MessagingConfiguration.shared.storage
val groupID = GroupUtil.doubleEncodeGroupID(groupPublicKey) val groupID = GroupUtil.doubleEncodeGroupID(groupPublicKey)
@ -257,149 +258,16 @@ fun MessageSender.requestEncryptionKeyPair(groupPublicKey: String) {
send(closedGroupControlMessage, Address.fromSerialized(groupID)) send(closedGroupControlMessage, Address.fromSerialized(groupID))
} }
/// - Note: Deprecated. fun MessageSender.sendLatestEncryptionKeyPair(publicKey: String, groupPublicKey: String) {
fun MessageSender.update(groupPublicKey: String, members: Collection<String>, name: String): Promise<Unit, Exception> {
val deferred = deferred<Unit, Exception>()
val context = MessagingConfiguration.shared.context
val storage = MessagingConfiguration.shared.storage val storage = MessagingConfiguration.shared.storage
val userPublicKey = storage.getUserPublicKey()!! val groupID = GroupUtil.doubleEncodeGroupID(groupPublicKey)
val sskDatabase = MessagingConfiguration.shared.sskDatabase val group = storage.getGroup(groupID) ?: run {
val groupID = GroupUtil.getEncodedClosedGroupID(GroupUtil.getEncodedClosedGroupID(Hex.fromStringCondensed(groupPublicKey)).toByteArray()) // double encoded Log.d("Loki", "Can't send encryption key pair for nonexistent closed group.")
val group = storage.getGroup(groupID) throw Error.NoThread
if (group == null) {
Log.d("Loki", "Can't update nonexistent closed group.")
deferred.reject(Error.NoThread)
return deferred.promise
} }
val oldMembers = group.members.map { it.serialize() }.toSet() val members = group.members.map { it.serialize() }
val newMembers = members.minus(oldMembers) if (!members.contains(publicKey)) {
val membersAsData = members.map { Hex.fromStringCondensed(it) } Log.d("Loki", "Refusing to send latest encryption key pair to non-member.")
val admins = group.admins.map { it.serialize() }
val adminsAsData = admins.map { Hex.fromStringCondensed(it) }
val groupPrivateKey = sskDatabase.getClosedGroupPrivateKey(groupPublicKey)
if (groupPrivateKey == null) {
Log.d("Loki", "Couldn't get private key for closed group.")
deferred.reject(Error.NoPrivateKey)
return deferred.promise
}
val wasAnyUserRemoved = members.toSet().intersect(oldMembers) != oldMembers.toSet()
val removedMembers = oldMembers.minus(members)
val isUserLeaving = removedMembers.contains(userPublicKey)
val newSenderKeys: List<ClosedGroupSenderKey>
if (wasAnyUserRemoved) {
if (isUserLeaving && removedMembers.count() != 1) {
Log.d("Loki", "Can't remove self and others simultaneously.")
deferred.reject(Error.InvalidClosedGroupUpdate)
return deferred.promise
}
// Send the update to the existing members using established channels (don't include new ratchets as everyone should regenerate new ratchets individually)
val promises = oldMembers.map { member ->
val closedGroupUpdateKind = ClosedGroupUpdate.Kind.Info(Hex.fromStringCondensed(groupPublicKey),
name, setOf(), membersAsData, adminsAsData)
val closedGroupUpdate = ClosedGroupUpdate()
closedGroupUpdate.kind = closedGroupUpdateKind
val address = Address.fromSerialized(member)
MessageSender.sendNonDurably(closedGroupUpdate, address).get()
}
val allOldRatchets = sskDatabase.getAllClosedGroupRatchets(groupPublicKey, ClosedGroupRatchetCollectionType.Current)
for (pair in allOldRatchets) {
val senderPublicKey = pair.first
val ratchet = pair.second
val collection = ClosedGroupRatchetCollectionType.Old
sskDatabase.setClosedGroupRatchet(groupPublicKey, senderPublicKey, ratchet, collection)
}
// Delete all ratchets (it's important that this happens * after * sending out the update)
sskDatabase.removeAllClosedGroupRatchets(groupPublicKey, ClosedGroupRatchetCollectionType.Current)
// Remove the group from the user's set of public keys to poll for if the user is leaving. Otherwise generate a new ratchet and
// send it out to all members (minus the removed ones) using established channels.
if (isUserLeaving) {
sskDatabase.removeClosedGroupPrivateKey(groupPublicKey)
storage.setActive(groupID, false)
storage.removeMember(groupID, Address.fromSerialized(userPublicKey))
// Notify the PN server
PushNotificationAPI.performOperation(PushNotificationAPI.ClosedGroupOperation.Unsubscribe, groupPublicKey, userPublicKey)
} else {
// Send closed group update messages to any new members using established channels
for (member in newMembers) {
val closedGroupUpdateKind = ClosedGroupUpdate.Kind.New(Hex.fromStringCondensed(groupPublicKey), name,
Hex.fromStringCondensed(groupPrivateKey), listOf(), membersAsData, adminsAsData)
val closedGroupUpdate = ClosedGroupUpdate()
closedGroupUpdate.kind = closedGroupUpdateKind
val address = Address.fromSerialized(member)
MessageSender.sendNonDurably(closedGroupUpdate, address)
}
// Send out the user's new ratchet to all members (minus the removed ones) using established channels
val userRatchet = SharedSenderKeysImplementation.shared.generateRatchet(groupPublicKey, userPublicKey)
val userSenderKey = ClosedGroupSenderKey(Hex.fromStringCondensed(userRatchet.chainKey), userRatchet.keyIndex, Hex.fromStringCondensed(userPublicKey))
for (member in members) {
if (member == userPublicKey) { continue }
val closedGroupUpdateKind = ClosedGroupUpdate.Kind.SenderKey(Hex.fromStringCondensed(groupPublicKey), userSenderKey)
val closedGroupUpdate = ClosedGroupUpdate()
closedGroupUpdate.kind = closedGroupUpdateKind
val address = Address.fromSerialized(member)
MessageSender.sendNonDurably(closedGroupUpdate, address)
}
}
} else if (newMembers.isNotEmpty()) {
// Generate ratchets for any new members
newSenderKeys = newMembers.map { publicKey ->
val ratchet = SharedSenderKeysImplementation.shared.generateRatchet(groupPublicKey, publicKey)
ClosedGroupSenderKey(Hex.fromStringCondensed(ratchet.chainKey), ratchet.keyIndex, Hex.fromStringCondensed(publicKey))
}
// Send a closed group update message to the existing members with the new members' ratchets (this message is aimed at the group)
val closedGroupUpdateKind = ClosedGroupUpdate.Kind.Info(Hex.fromStringCondensed(groupPublicKey), name,
newSenderKeys, membersAsData, adminsAsData)
val closedGroupUpdate = ClosedGroupUpdate()
closedGroupUpdate.kind = closedGroupUpdateKind
val address = Address.fromSerialized(groupID)
MessageSender.send(closedGroupUpdate, address)
// Send closed group update messages to the new members using established channels
var allSenderKeys = sskDatabase.getAllClosedGroupSenderKeys(groupPublicKey, ClosedGroupRatchetCollectionType.Current)
allSenderKeys = allSenderKeys.union(newSenderKeys)
for (member in newMembers) {
val closedGroupUpdateKind = ClosedGroupUpdate.Kind.New(Hex.fromStringCondensed(groupPublicKey), name,
Hex.fromStringCondensed(groupPrivateKey), allSenderKeys, membersAsData, adminsAsData)
val closedGroupUpdate = ClosedGroupUpdate()
closedGroupUpdate.kind = closedGroupUpdateKind
val address = Address.fromSerialized(member)
MessageSender.send(closedGroupUpdate, address)
}
} else {
val allSenderKeys = sskDatabase.getAllClosedGroupSenderKeys(groupPublicKey, ClosedGroupRatchetCollectionType.Current)
val closedGroupUpdateKind = ClosedGroupUpdate.Kind.Info(Hex.fromStringCondensed(groupPublicKey), name,
allSenderKeys, membersAsData, adminsAsData)
val closedGroupUpdate = ClosedGroupUpdate()
closedGroupUpdate.kind = closedGroupUpdateKind
val address = Address.fromSerialized(groupID)
MessageSender.send(closedGroupUpdate, address)
}
// Update the group
storage.updateTitle(groupID, name)
if (!isUserLeaving) {
// The call below sets isActive to true, so if the user is leaving we have to use groupDB.remove(...) instead
storage.updateMembers(groupID, members.map { Address.fromSerialized(it) })
}
// Notify the user
val infoType = if (isUserLeaving) SignalServiceProtos.GroupContext.Type.QUIT else SignalServiceProtos.GroupContext.Type.UPDATE
val threadID = storage.getOrCreateThreadIdFor(Address.fromSerialized(groupID))
storage.insertOutgoingInfoMessage(context, groupID, infoType, name, members, admins, threadID)
deferred.resolve(Unit)
return deferred.promise
}
/// - Note: Deprecated.
fun MessageSender.leave(groupPublicKey: String) {
val storage = MessagingConfiguration.shared.storage
val userPublicKey = storage.getUserPublicKey()!!
val groupID = GroupUtil.getEncodedClosedGroupID(GroupUtil.getEncodedClosedGroupID(Hex.fromStringCondensed(groupPublicKey)).toByteArray()) // double encoded
val group = storage.getGroup(groupID)
if (group == null) {
Log.d("Loki", "Can't leave nonexistent closed group.")
return return
} }
val name = group.title
val oldMembers = group.members.map { it.serialize() }.toSet()
val newMembers = oldMembers.minus(userPublicKey)
return update(groupPublicKey, newMembers, name).get()
} }