mirror of
https://github.com/oxen-io/session-android.git
synced 2024-12-25 01:07:47 +00:00
Merge pull request #450 from hjubb/closed_group_kp_distribution
Closed group kp distribution
This commit is contained in:
commit
d3b8642b18
@ -280,18 +280,15 @@ class EditClosedGroupActivity : PassphraseRequiredActionBarActivity() {
|
|||||||
ClosedGroupsProtocolV2.explicitLeave(this, groupPublicKey!!)
|
ClosedGroupsProtocolV2.explicitLeave(this, groupPublicKey!!)
|
||||||
} else {
|
} else {
|
||||||
task {
|
task {
|
||||||
val name =
|
if (hasNameChanged) {
|
||||||
if (hasNameChanged) ClosedGroupsProtocolV2.explicitNameChange(this@EditClosedGroupActivity,groupPublicKey!!,name)
|
ClosedGroupsProtocolV2.explicitNameChange(this@EditClosedGroupActivity, groupPublicKey!!, name)
|
||||||
else Promise.of(Unit)
|
}
|
||||||
name.get()
|
|
||||||
members.filterNot { it in originalMembers }.let { adds ->
|
members.filterNot { it in originalMembers }.let { adds ->
|
||||||
if (adds.isNotEmpty()) ClosedGroupsProtocolV2.explicitAddMembers(this@EditClosedGroupActivity, groupPublicKey!!, adds.map { it.address.serialize() })
|
if (adds.isNotEmpty()) ClosedGroupsProtocolV2.explicitAddMembers(this@EditClosedGroupActivity, groupPublicKey!!, adds.map { it.address.serialize() })
|
||||||
else Promise.of(Unit)
|
}
|
||||||
}.get()
|
|
||||||
originalMembers.filterNot { it in members }.let { removes ->
|
originalMembers.filterNot { it in members }.let { removes ->
|
||||||
if (removes.isNotEmpty()) ClosedGroupsProtocolV2.explicitRemoveMembers(this@EditClosedGroupActivity, groupPublicKey!!, removes.map { it.address.serialize() })
|
if (removes.isNotEmpty()) ClosedGroupsProtocolV2.explicitRemoveMembers(this@EditClosedGroupActivity, groupPublicKey!!, removes.map { it.address.serialize() })
|
||||||
else Promise.of(Unit)
|
}
|
||||||
}.get()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
promise.successUi {
|
promise.successUi {
|
||||||
|
@ -27,7 +27,10 @@ import org.session.libsignal.utilities.Hex
|
|||||||
import java.util.*
|
import java.util.*
|
||||||
import java.util.concurrent.TimeUnit
|
import java.util.concurrent.TimeUnit
|
||||||
|
|
||||||
class ClosedGroupUpdateMessageSendJobV2 private constructor(parameters: Parameters, private val destination: String, private val kind: Kind, private val sentTime: Long) : BaseJob(parameters) {
|
class ClosedGroupUpdateMessageSendJobV2 private constructor(parameters: Parameters,
|
||||||
|
private val destination: String,
|
||||||
|
private val kind: Kind,
|
||||||
|
private val sentTime: Long) : BaseJob(parameters) {
|
||||||
|
|
||||||
sealed class Kind {
|
sealed class Kind {
|
||||||
class New(val publicKey: ByteArray, val name: String, val encryptionKeyPair: ECKeyPair, val members: Collection<ByteArray>, val admins: Collection<ByteArray>) : Kind()
|
class New(val publicKey: ByteArray, val name: String, val encryptionKeyPair: ECKeyPair, val members: Collection<ByteArray>, val admins: Collection<ByteArray>) : Kind()
|
||||||
@ -36,7 +39,7 @@ class ClosedGroupUpdateMessageSendJobV2 private constructor(parameters: Paramete
|
|||||||
class RemoveMembers(val members: Collection<ByteArray>) : Kind()
|
class RemoveMembers(val members: Collection<ByteArray>) : Kind()
|
||||||
class AddMembers(val members: Collection<ByteArray>) : Kind()
|
class AddMembers(val members: Collection<ByteArray>) : Kind()
|
||||||
class NameChange(val name: String) : Kind()
|
class NameChange(val name: String) : Kind()
|
||||||
class EncryptionKeyPair(val wrappers: Collection<KeyPairWrapper>) : Kind() // The new encryption key pair encrypted for each member individually
|
class EncryptionKeyPair(val wrappers: Collection<KeyPairWrapper>, val targetUser: String?) : Kind() // The new encryption key pair encrypted for each member individually
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
@ -116,6 +119,7 @@ class ClosedGroupUpdateMessageSendJobV2 private constructor(parameters: Paramete
|
|||||||
builder.putString("kind", "EncryptionKeyPair")
|
builder.putString("kind", "EncryptionKeyPair")
|
||||||
val wrappers = kind.wrappers.joinToString(" - ") { Json.encodeToString(it) }
|
val wrappers = kind.wrappers.joinToString(" - ") { Json.encodeToString(it) }
|
||||||
builder.putString("wrappers", wrappers)
|
builder.putString("wrappers", wrappers)
|
||||||
|
builder.putString("targetUser", kind.targetUser)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return builder.build()
|
return builder.build()
|
||||||
@ -146,7 +150,8 @@ class ClosedGroupUpdateMessageSendJobV2 private constructor(parameters: Paramete
|
|||||||
}
|
}
|
||||||
"EncryptionKeyPair" -> {
|
"EncryptionKeyPair" -> {
|
||||||
val wrappers: Collection<KeyPairWrapper> = data.getString("wrappers").split(" - ").map { Json.decodeFromString(it) }
|
val wrappers: Collection<KeyPairWrapper> = data.getString("wrappers").split(" - ").map { Json.decodeFromString(it) }
|
||||||
kind = Kind.EncryptionKeyPair(wrappers)
|
val targetUser = data.getString("targetUser")
|
||||||
|
kind = Kind.EncryptionKeyPair(wrappers, targetUser)
|
||||||
}
|
}
|
||||||
"RemoveMembers" -> {
|
"RemoveMembers" -> {
|
||||||
val members = data.getString("members").split(" - ").map { Hex.fromStringCondensed(it) }
|
val members = data.getString("members").split(" - ").map { Hex.fromStringCondensed(it) }
|
||||||
@ -170,6 +175,11 @@ class ClosedGroupUpdateMessageSendJobV2 private constructor(parameters: Paramete
|
|||||||
}
|
}
|
||||||
|
|
||||||
public override fun onRun() {
|
public override fun onRun() {
|
||||||
|
val sendDestination = if (kind is Kind.EncryptionKeyPair && kind.targetUser != null) {
|
||||||
|
kind.targetUser
|
||||||
|
} else {
|
||||||
|
destination
|
||||||
|
}
|
||||||
val contentMessage = SignalServiceProtos.Content.newBuilder()
|
val contentMessage = SignalServiceProtos.Content.newBuilder()
|
||||||
val dataMessage = SignalServiceProtos.DataMessage.newBuilder()
|
val dataMessage = SignalServiceProtos.DataMessage.newBuilder()
|
||||||
val closedGroupUpdate = SignalServiceProtos.ClosedGroupUpdateV2.newBuilder()
|
val closedGroupUpdate = SignalServiceProtos.ClosedGroupUpdateV2.newBuilder()
|
||||||
@ -193,6 +203,9 @@ class ClosedGroupUpdateMessageSendJobV2 private constructor(parameters: Paramete
|
|||||||
is Kind.EncryptionKeyPair -> {
|
is Kind.EncryptionKeyPair -> {
|
||||||
closedGroupUpdate.type = SignalServiceProtos.ClosedGroupUpdateV2.Type.ENCRYPTION_KEY_PAIR
|
closedGroupUpdate.type = SignalServiceProtos.ClosedGroupUpdateV2.Type.ENCRYPTION_KEY_PAIR
|
||||||
closedGroupUpdate.addAllWrappers(kind.wrappers.map { it.toProto() })
|
closedGroupUpdate.addAllWrappers(kind.wrappers.map { it.toProto() })
|
||||||
|
if (kind.targetUser != null) {
|
||||||
|
closedGroupUpdate.publicKey = ByteString.copyFrom(Hex.fromStringCondensed(destination))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Kind.Leave -> {
|
Kind.Leave -> {
|
||||||
closedGroupUpdate.type = SignalServiceProtos.ClosedGroupUpdateV2.Type.MEMBER_LEFT
|
closedGroupUpdate.type = SignalServiceProtos.ClosedGroupUpdateV2.Type.MEMBER_LEFT
|
||||||
@ -214,8 +227,8 @@ class ClosedGroupUpdateMessageSendJobV2 private constructor(parameters: Paramete
|
|||||||
contentMessage.dataMessage = dataMessage.build()
|
contentMessage.dataMessage = dataMessage.build()
|
||||||
val serializedContentMessage = contentMessage.build().toByteArray()
|
val serializedContentMessage = contentMessage.build().toByteArray()
|
||||||
val messageSender = ApplicationContext.getInstance(context).communicationModule.provideSignalMessageSender()
|
val messageSender = ApplicationContext.getInstance(context).communicationModule.provideSignalMessageSender()
|
||||||
val address = SignalServiceAddress(destination)
|
val address = SignalServiceAddress(sendDestination)
|
||||||
val recipient = recipient(context, destination)
|
val recipient = recipient(context, sendDestination)
|
||||||
val udAccess = UnidentifiedAccessUtil.getAccessFor(context, recipient)
|
val udAccess = UnidentifiedAccessUtil.getAccessFor(context, recipient)
|
||||||
val ttl = when (kind) {
|
val ttl = when (kind) {
|
||||||
is Kind.EncryptionKeyPair -> 4 * 24 * 60 * 60 * 1000
|
is Kind.EncryptionKeyPair -> 4 * 24 * 60 * 60 * 1000
|
||||||
@ -227,7 +240,7 @@ class ClosedGroupUpdateMessageSendJobV2 private constructor(parameters: Paramete
|
|||||||
sentTime, serializedContentMessage, false, ttl, false,
|
sentTime, serializedContentMessage, false, ttl, false,
|
||||||
true, false, false, Optional.absent())
|
true, false, false, Optional.absent())
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
Log.d("Loki", "Failed to send closed group update message to: $destination due to error: $e.")
|
Log.d("Loki", "Failed to send closed group update message to: $sendDestination due to error: $e.")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,7 +5,6 @@ import android.util.Log
|
|||||||
import com.google.protobuf.ByteString
|
import com.google.protobuf.ByteString
|
||||||
import nl.komponents.kovenant.Promise
|
import nl.komponents.kovenant.Promise
|
||||||
import nl.komponents.kovenant.deferred
|
import nl.komponents.kovenant.deferred
|
||||||
import nl.komponents.kovenant.task
|
|
||||||
import org.session.libsignal.libsignal.ecc.Curve
|
import org.session.libsignal.libsignal.ecc.Curve
|
||||||
import org.session.libsignal.libsignal.ecc.DjbECPrivateKey
|
import org.session.libsignal.libsignal.ecc.DjbECPrivateKey
|
||||||
import org.session.libsignal.libsignal.ecc.DjbECPublicKey
|
import org.session.libsignal.libsignal.ecc.DjbECPublicKey
|
||||||
@ -129,19 +128,16 @@ object ClosedGroupsProtocolV2 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun explicitAddMembers(context: Context, groupPublicKey: String, membersToAdd: List<String>): Promise<Any, java.lang.Exception> {
|
fun explicitAddMembers(context: Context, groupPublicKey: String, membersToAdd: List<String>) {
|
||||||
return task {
|
|
||||||
val apiDB = DatabaseFactory.getLokiAPIDatabase(context)
|
val apiDB = DatabaseFactory.getLokiAPIDatabase(context)
|
||||||
val groupDB = DatabaseFactory.getGroupDatabase(context)
|
val groupDB = DatabaseFactory.getGroupDatabase(context)
|
||||||
val groupID = doubleEncodeGroupID(groupPublicKey)
|
val groupID = doubleEncodeGroupID(groupPublicKey)
|
||||||
val group = groupDB.getGroup(groupID).orNull()
|
val group = groupDB.getGroup(groupID).orNull()
|
||||||
if (group == null) {
|
if (group == null) {
|
||||||
Log.d("Loki", "Can't leave nonexistent closed group.")
|
Log.d("Loki", "Can't leave nonexistent closed group.")
|
||||||
return@task Error.NoThread
|
throw Error.NoThread
|
||||||
}
|
}
|
||||||
val updatedMembers = group.members.map { it.serialize() }.toSet() + membersToAdd
|
val updatedMembers = group.members.map { it.serialize() }.toSet() + membersToAdd
|
||||||
// Save the new group members
|
|
||||||
groupDB.updateMembers(groupID, updatedMembers.map { Address.fromSerialized(it) })
|
|
||||||
val membersAsData = updatedMembers.map { Hex.fromStringCondensed(it) }
|
val membersAsData = updatedMembers.map { Hex.fromStringCondensed(it) }
|
||||||
val newMembersAsData = membersToAdd.map { Hex.fromStringCondensed(it) }
|
val newMembersAsData = membersToAdd.map { Hex.fromStringCondensed(it) }
|
||||||
val admins = group.admins.map { it.serialize() }
|
val admins = group.admins.map { it.serialize() }
|
||||||
@ -152,7 +148,7 @@ object ClosedGroupsProtocolV2 {
|
|||||||
}.orNull()
|
}.orNull()
|
||||||
if (encryptionKeyPair == null) {
|
if (encryptionKeyPair == null) {
|
||||||
Log.d("Loki", "Couldn't get encryption key pair for closed group.")
|
Log.d("Loki", "Couldn't get encryption key pair for closed group.")
|
||||||
return@task Error.NoKeyPair
|
throw Error.NoKeyPair
|
||||||
}
|
}
|
||||||
val name = group.title
|
val name = group.title
|
||||||
// Send the update to the group
|
// Send the update to the group
|
||||||
@ -160,6 +156,8 @@ object ClosedGroupsProtocolV2 {
|
|||||||
val job = ClosedGroupUpdateMessageSendJobV2(groupPublicKey, memberUpdateKind, sentTime)
|
val job = ClosedGroupUpdateMessageSendJobV2(groupPublicKey, memberUpdateKind, sentTime)
|
||||||
job.setContext(context)
|
job.setContext(context)
|
||||||
job.onRun() // Run the job immediately
|
job.onRun() // Run the job immediately
|
||||||
|
// Save the new group members
|
||||||
|
groupDB.updateMembers(groupID, updatedMembers.map { Address.fromSerialized(it) })
|
||||||
// Notify the user
|
// Notify the user
|
||||||
val infoType = GroupContext.Type.UPDATE
|
val infoType = GroupContext.Type.UPDATE
|
||||||
val threadID = DatabaseFactory.getThreadDatabase(context).getOrCreateThreadIdFor(Recipient.from(context, Address.fromSerialized(groupID), false))
|
val threadID = DatabaseFactory.getThreadDatabase(context).getOrCreateThreadIdFor(Recipient.from(context, Address.fromSerialized(groupID), false))
|
||||||
@ -173,11 +171,9 @@ object ClosedGroupsProtocolV2 {
|
|||||||
ApplicationContext.getInstance(context).jobManager.add(newMemberJob)
|
ApplicationContext.getInstance(context).jobManager.add(newMemberJob)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun explicitRemoveMembers(context: Context, groupPublicKey: String, membersToRemove: List<String>): Promise<Any, Exception> {
|
fun explicitRemoveMembers(context: Context, groupPublicKey: String, membersToRemove: List<String>) {
|
||||||
return task {
|
|
||||||
val userPublicKey = TextSecurePreferences.getLocalNumber(context)
|
val userPublicKey = TextSecurePreferences.getLocalNumber(context)
|
||||||
val apiDB = DatabaseFactory.getLokiAPIDatabase(context)
|
val apiDB = DatabaseFactory.getLokiAPIDatabase(context)
|
||||||
val groupDB = DatabaseFactory.getGroupDatabase(context)
|
val groupDB = DatabaseFactory.getGroupDatabase(context)
|
||||||
@ -185,22 +181,20 @@ object ClosedGroupsProtocolV2 {
|
|||||||
val group = groupDB.getGroup(groupID).orNull()
|
val group = groupDB.getGroup(groupID).orNull()
|
||||||
if (group == null) {
|
if (group == null) {
|
||||||
Log.d("Loki", "Can't leave nonexistent closed group.")
|
Log.d("Loki", "Can't leave nonexistent closed group.")
|
||||||
return@task Error.NoThread
|
throw Error.NoThread
|
||||||
}
|
}
|
||||||
val updatedMembers = group.members.map { it.serialize() }.toSet() - membersToRemove
|
val updatedMembers = group.members.map { it.serialize() }.toSet() - membersToRemove
|
||||||
// Save the new group members
|
|
||||||
groupDB.updateMembers(groupID, updatedMembers.map { Address.fromSerialized(it) })
|
|
||||||
val removeMembersAsData = membersToRemove.map { Hex.fromStringCondensed(it) }
|
val removeMembersAsData = membersToRemove.map { Hex.fromStringCondensed(it) }
|
||||||
val admins = group.admins.map { it.serialize() }
|
val admins = group.admins.map { it.serialize() }
|
||||||
val sentTime = System.currentTimeMillis()
|
val sentTime = System.currentTimeMillis()
|
||||||
val encryptionKeyPair = apiDB.getLatestClosedGroupEncryptionKeyPair(groupPublicKey)
|
val encryptionKeyPair = apiDB.getLatestClosedGroupEncryptionKeyPair(groupPublicKey)
|
||||||
if (encryptionKeyPair == null) {
|
if (encryptionKeyPair == null) {
|
||||||
Log.d("Loki", "Couldn't get encryption key pair for closed group.")
|
Log.d("Loki", "Couldn't get encryption key pair for closed group.")
|
||||||
return@task Error.NoKeyPair
|
throw Error.NoKeyPair
|
||||||
}
|
}
|
||||||
if (membersToRemove.any { it in admins } && updatedMembers.isNotEmpty()) {
|
if (membersToRemove.any { it in admins } && updatedMembers.isNotEmpty()) {
|
||||||
Log.d("Loki", "Can't remove admin from closed group unless the group is destroyed entirely.")
|
Log.d("Loki", "Can't remove admin from closed group unless the group is destroyed entirely.")
|
||||||
return@task Error.InvalidUpdate
|
throw Error.InvalidUpdate
|
||||||
}
|
}
|
||||||
val name = group.title
|
val name = group.title
|
||||||
// Send the update to the group
|
// Send the update to the group
|
||||||
@ -208,6 +202,8 @@ object ClosedGroupsProtocolV2 {
|
|||||||
val job = ClosedGroupUpdateMessageSendJobV2(groupPublicKey, memberUpdateKind, sentTime)
|
val job = ClosedGroupUpdateMessageSendJobV2(groupPublicKey, memberUpdateKind, sentTime)
|
||||||
job.setContext(context)
|
job.setContext(context)
|
||||||
job.onRun() // Run the job immediately
|
job.onRun() // Run the job immediately
|
||||||
|
// Save the new group members
|
||||||
|
groupDB.updateMembers(groupID, updatedMembers.map { Address.fromSerialized(it) })
|
||||||
// Notify the user
|
// Notify the user
|
||||||
val infoType = GroupContext.Type.UPDATE
|
val infoType = GroupContext.Type.UPDATE
|
||||||
val threadID = DatabaseFactory.getThreadDatabase(context).getOrCreateThreadIdFor(Recipient.from(context, Address.fromSerialized(groupID), false))
|
val threadID = DatabaseFactory.getThreadDatabase(context).getOrCreateThreadIdFor(Recipient.from(context, Address.fromSerialized(groupID), false))
|
||||||
@ -216,14 +212,10 @@ object ClosedGroupsProtocolV2 {
|
|||||||
if (isCurrentUserAdmin) {
|
if (isCurrentUserAdmin) {
|
||||||
generateAndSendNewEncryptionKeyPair(context, groupPublicKey, updatedMembers)
|
generateAndSendNewEncryptionKeyPair(context, groupPublicKey, updatedMembers)
|
||||||
}
|
}
|
||||||
return@task Unit
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun explicitNameChange(context: Context, groupPublicKey: String, newName: String): Promise<Unit, Exception> {
|
fun explicitNameChange(context: Context, groupPublicKey: String, newName: String) {
|
||||||
val deferred = deferred<Unit, Exception>()
|
|
||||||
ThreadUtils.queue {
|
|
||||||
val groupDB = DatabaseFactory.getGroupDatabase(context)
|
val groupDB = DatabaseFactory.getGroupDatabase(context)
|
||||||
val groupID = doubleEncodeGroupID(groupPublicKey)
|
val groupID = doubleEncodeGroupID(groupPublicKey)
|
||||||
val group = groupDB.getGroup(groupID).orNull()
|
val group = groupDB.getGroup(groupID).orNull()
|
||||||
@ -232,7 +224,7 @@ object ClosedGroupsProtocolV2 {
|
|||||||
val sentTime = System.currentTimeMillis()
|
val sentTime = System.currentTimeMillis()
|
||||||
if (group == null) {
|
if (group == null) {
|
||||||
Log.d("Loki", "Can't leave nonexistent closed group.")
|
Log.d("Loki", "Can't leave nonexistent closed group.")
|
||||||
return@queue deferred.reject(Error.NoThread)
|
throw Error.NoThread
|
||||||
}
|
}
|
||||||
// Send the update to the group
|
// Send the update to the group
|
||||||
val kind = ClosedGroupUpdateMessageSendJobV2.Kind.NameChange(newName)
|
val kind = ClosedGroupUpdateMessageSendJobV2.Kind.NameChange(newName)
|
||||||
@ -245,116 +237,9 @@ object ClosedGroupsProtocolV2 {
|
|||||||
insertOutgoingInfoMessage(context, groupID, infoType, newName, members, admins, threadID, sentTime)
|
insertOutgoingInfoMessage(context, groupID, infoType, newName, members, admins, threadID, sentTime)
|
||||||
// Update the group
|
// Update the group
|
||||||
groupDB.updateTitle(groupID, newName)
|
groupDB.updateTitle(groupID, newName)
|
||||||
deferred.resolve(Unit)
|
|
||||||
}
|
|
||||||
return deferred.promise
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@JvmStatic
|
private fun generateAndSendNewEncryptionKeyPair(context: Context, groupPublicKey: String, targetMembers: Collection<String>) {
|
||||||
fun leave(context: Context, groupPublicKey: String): Promise<Unit, Exception> {
|
|
||||||
val userPublicKey = TextSecurePreferences.getLocalNumber(context)!!
|
|
||||||
val groupDB = DatabaseFactory.getGroupDatabase(context)
|
|
||||||
val groupID = doubleEncodeGroupID(groupPublicKey)
|
|
||||||
val group = groupDB.getGroup(groupID).orNull()
|
|
||||||
if (group == null) {
|
|
||||||
Log.d("Loki", "Can't leave nonexistent closed group.")
|
|
||||||
return Promise.ofFail(Error.NoThread)
|
|
||||||
}
|
|
||||||
val name = group.title
|
|
||||||
val oldMembers = group.members.map { it.serialize() }.toSet()
|
|
||||||
val newMembers: Set<String>
|
|
||||||
val isCurrentUserAdmin = group.admins.map { it.toString() }.contains(userPublicKey)
|
|
||||||
if (!isCurrentUserAdmin) {
|
|
||||||
newMembers = oldMembers.minus(userPublicKey)
|
|
||||||
} else {
|
|
||||||
newMembers = setOf() // If the admin leaves the group is destroyed
|
|
||||||
}
|
|
||||||
return update(context, groupPublicKey, newMembers, name)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun update(context: Context, groupPublicKey: String, members: Collection<String>, name: String): Promise<Unit, Exception> {
|
|
||||||
val deferred = deferred<Unit, Exception>()
|
|
||||||
ThreadUtils.queue {
|
|
||||||
val userPublicKey = TextSecurePreferences.getLocalNumber(context)!!
|
|
||||||
val apiDB = DatabaseFactory.getLokiAPIDatabase(context)
|
|
||||||
val groupDB = DatabaseFactory.getGroupDatabase(context)
|
|
||||||
val groupID = doubleEncodeGroupID(groupPublicKey)
|
|
||||||
val group = groupDB.getGroup(groupID).orNull()
|
|
||||||
if (group == null) {
|
|
||||||
Log.d("Loki", "Can't update nonexistent closed group.")
|
|
||||||
return@queue deferred.reject(Error.NoThread)
|
|
||||||
}
|
|
||||||
val sentTime = System.currentTimeMillis()
|
|
||||||
val oldMembers = group.members.map { it.serialize() }.toSet()
|
|
||||||
val newMembers = members.minus(oldMembers)
|
|
||||||
val membersAsData = members.map { Hex.fromStringCondensed(it) }
|
|
||||||
val admins = group.admins.map { it.serialize() }
|
|
||||||
val adminsAsData = admins.map { Hex.fromStringCondensed(it) }
|
|
||||||
val encryptionKeyPair = apiDB.getLatestClosedGroupEncryptionKeyPair(groupPublicKey)
|
|
||||||
if (encryptionKeyPair == null) {
|
|
||||||
Log.d("Loki", "Couldn't get encryption key pair for closed group.")
|
|
||||||
return@queue deferred.reject(Error.NoKeyPair)
|
|
||||||
}
|
|
||||||
val removedMembers = oldMembers.minus(members)
|
|
||||||
if (removedMembers.contains(admins.first()) && members.isNotEmpty()) {
|
|
||||||
Log.d("Loki", "Can't remove admin from closed group unless the group is destroyed entirely.")
|
|
||||||
return@queue deferred.reject(Error.InvalidUpdate)
|
|
||||||
}
|
|
||||||
val isUserLeaving = removedMembers.contains(userPublicKey)
|
|
||||||
if (isUserLeaving && members.isNotEmpty()) {
|
|
||||||
if (removedMembers.count() != 1 || newMembers.isNotEmpty()) {
|
|
||||||
Log.d("Loki", "Can't remove self and add or remove others simultaneously.")
|
|
||||||
return@queue deferred.reject(Error.InvalidUpdate)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Send the update to the group
|
|
||||||
@Suppress("NAME_SHADOWING")
|
|
||||||
val closedGroupUpdateKind = ClosedGroupUpdateMessageSendJobV2.Kind.Update(name, membersAsData)
|
|
||||||
@Suppress("NAME_SHADOWING")
|
|
||||||
val job = ClosedGroupUpdateMessageSendJobV2(groupPublicKey, closedGroupUpdateKind, sentTime)
|
|
||||||
job.setContext(context)
|
|
||||||
job.onRun() // Run the job immediately
|
|
||||||
if (isUserLeaving) {
|
|
||||||
// Remove the group private key and unsubscribe from PNs
|
|
||||||
apiDB.removeAllClosedGroupEncryptionKeyPairs(groupPublicKey)
|
|
||||||
apiDB.removeClosedGroupPublicKey(groupPublicKey)
|
|
||||||
// Mark the group as inactive
|
|
||||||
groupDB.setActive(groupID, false)
|
|
||||||
groupDB.removeMember(groupID, Address.fromSerialized(userPublicKey))
|
|
||||||
// Notify the PN server
|
|
||||||
LokiPushNotificationManager.performOperation(context, ClosedGroupOperation.Unsubscribe, groupPublicKey, userPublicKey)
|
|
||||||
} else {
|
|
||||||
// Generate and distribute a new encryption key pair if needed
|
|
||||||
val wasAnyUserRemoved = removedMembers.isNotEmpty()
|
|
||||||
val isCurrentUserAdmin = admins.contains(userPublicKey)
|
|
||||||
if (wasAnyUserRemoved && isCurrentUserAdmin) {
|
|
||||||
generateAndSendNewEncryptionKeyPair(context, groupPublicKey, members.minus(newMembers))
|
|
||||||
}
|
|
||||||
// Send closed group update messages to any new members individually
|
|
||||||
for (member in newMembers) {
|
|
||||||
@Suppress("NAME_SHADOWING")
|
|
||||||
val closedGroupUpdateKind = ClosedGroupUpdateMessageSendJobV2.Kind.New(Hex.fromStringCondensed(groupPublicKey), name, encryptionKeyPair, membersAsData, adminsAsData)
|
|
||||||
@Suppress("NAME_SHADOWING")
|
|
||||||
val job = ClosedGroupUpdateMessageSendJobV2(member, closedGroupUpdateKind, sentTime)
|
|
||||||
ApplicationContext.getInstance(context).jobManager.add(job)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Update the group
|
|
||||||
groupDB.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
|
|
||||||
groupDB.updateMembers(groupID, members.map { Address.fromSerialized(it) })
|
|
||||||
}
|
|
||||||
// Notify the user
|
|
||||||
val infoType = if (isUserLeaving) GroupContext.Type.QUIT else GroupContext.Type.UPDATE
|
|
||||||
val threadID = DatabaseFactory.getThreadDatabase(context).getOrCreateThreadIdFor(Recipient.from(context, Address.fromSerialized(groupID), false))
|
|
||||||
insertOutgoingInfoMessage(context, groupID, infoType, name, members, admins, threadID, sentTime)
|
|
||||||
deferred.resolve(Unit)
|
|
||||||
}
|
|
||||||
return deferred.promise
|
|
||||||
}
|
|
||||||
|
|
||||||
fun generateAndSendNewEncryptionKeyPair(context: Context, groupPublicKey: String, targetMembers: Collection<String>) {
|
|
||||||
// Prepare
|
// Prepare
|
||||||
val userPublicKey = TextSecurePreferences.getLocalNumber(context)
|
val userPublicKey = TextSecurePreferences.getLocalNumber(context)
|
||||||
val apiDB = DatabaseFactory.getLokiAPIDatabase(context)
|
val apiDB = DatabaseFactory.getLokiAPIDatabase(context)
|
||||||
@ -383,7 +268,7 @@ object ClosedGroupsProtocolV2 {
|
|||||||
pendingKeyPair[groupPublicKey] = Optional.absent()
|
pendingKeyPair[groupPublicKey] = Optional.absent()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun sendEncryptionKeyPair(context: Context, groupPublicKey: String, newKeyPair: ECKeyPair, targetMembers: Collection<String>, force: Boolean = true) {
|
private fun sendEncryptionKeyPair(context: Context, groupPublicKey: String, newKeyPair: ECKeyPair, targetMembers: Collection<String>, targetUser: String? = null, force: Boolean = true) {
|
||||||
val proto = SignalServiceProtos.KeyPair.newBuilder()
|
val proto = SignalServiceProtos.KeyPair.newBuilder()
|
||||||
proto.publicKey = ByteString.copyFrom(newKeyPair.publicKey.serialize().removing05PrefixIfNeeded())
|
proto.publicKey = ByteString.copyFrom(newKeyPair.publicKey.serialize().removing05PrefixIfNeeded())
|
||||||
proto.privateKey = ByteString.copyFrom(newKeyPair.privateKey.serialize())
|
proto.privateKey = ByteString.copyFrom(newKeyPair.privateKey.serialize())
|
||||||
@ -392,7 +277,7 @@ object ClosedGroupsProtocolV2 {
|
|||||||
val ciphertext = SessionProtocolImpl(context).encrypt(plaintext, publicKey)
|
val ciphertext = SessionProtocolImpl(context).encrypt(plaintext, publicKey)
|
||||||
ClosedGroupUpdateMessageSendJobV2.KeyPairWrapper(publicKey, ciphertext)
|
ClosedGroupUpdateMessageSendJobV2.KeyPairWrapper(publicKey, ciphertext)
|
||||||
}
|
}
|
||||||
val job = ClosedGroupUpdateMessageSendJobV2(groupPublicKey, ClosedGroupUpdateMessageSendJobV2.Kind.EncryptionKeyPair(wrappers), System.currentTimeMillis())
|
val job = ClosedGroupUpdateMessageSendJobV2(groupPublicKey, ClosedGroupUpdateMessageSendJobV2.Kind.EncryptionKeyPair(wrappers, targetUser), System.currentTimeMillis())
|
||||||
if (force) {
|
if (force) {
|
||||||
job.setContext(context)
|
job.setContext(context)
|
||||||
job.onRun() // Run the job immediately
|
job.onRun() // Run the job immediately
|
||||||
@ -546,17 +431,17 @@ object ClosedGroupsProtocolV2 {
|
|||||||
Log.d("Loki", "Ignoring closed group info message for nonexistent or inactive group.")
|
Log.d("Loki", "Ignoring closed group info message for nonexistent or inactive group.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
// Check common group update logic
|
||||||
if (!isValidGroupUpdate(group, sentTimestamp, senderPublicKey)) {
|
if (!isValidGroupUpdate(group, sentTimestamp, senderPublicKey)) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
val name = group.title
|
val name = group.title
|
||||||
// Check common group update logic
|
|
||||||
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
|
// Users that are part of this add update
|
||||||
val updateMembers = closedGroupUpdate.membersList.map { it.toByteArray().toHexString() }
|
val updateMembers = closedGroupUpdate.membersList.map { it.toByteArray().toHexString() }
|
||||||
// newMembers to save is old members minus removed members
|
// newMembers to save is old members plus members included in this update
|
||||||
val newMembers = members + updateMembers
|
val newMembers = members + updateMembers
|
||||||
groupDB.updateMembers(groupID, newMembers.map { Address.fromSerialized(it) })
|
groupDB.updateMembers(groupID, newMembers.map { Address.fromSerialized(it) })
|
||||||
|
|
||||||
@ -573,7 +458,9 @@ object ClosedGroupsProtocolV2 {
|
|||||||
if (encryptionKeyPair == null) {
|
if (encryptionKeyPair == null) {
|
||||||
Log.d("Loki", "Couldn't get encryption key pair for closed group.")
|
Log.d("Loki", "Couldn't get encryption key pair for closed group.")
|
||||||
} else {
|
} else {
|
||||||
sendEncryptionKeyPair(context, groupPublicKey, encryptionKeyPair, newMembers, false)
|
for (user in updateMembers) {
|
||||||
|
sendEncryptionKeyPair(context, groupPublicKey, encryptionKeyPair, setOf(user), targetUser = user, force = false)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -628,7 +515,8 @@ object ClosedGroupsProtocolV2 {
|
|||||||
val updatedMemberList = members - senderPublicKey
|
val updatedMemberList = members - senderPublicKey
|
||||||
val userLeft = userPublicKey == senderPublicKey
|
val userLeft = userPublicKey == senderPublicKey
|
||||||
|
|
||||||
if (didAdminLeave || userLeft) {
|
// if the admin left, we left, or we are the only remaining member: remove the group
|
||||||
|
if (didAdminLeave || userLeft || updatedMemberList.size == 1) {
|
||||||
disableLocalGroupAndUnsubscribe(context, apiDB, groupPublicKey, groupDB, groupID, userPublicKey)
|
disableLocalGroupAndUnsubscribe(context, apiDB, groupPublicKey, groupDB, groupID, userPublicKey)
|
||||||
} else {
|
} else {
|
||||||
val isCurrentUserAdmin = admins.contains(userPublicKey)
|
val isCurrentUserAdmin = admins.contains(userPublicKey)
|
||||||
@ -735,7 +623,12 @@ object ClosedGroupsProtocolV2 {
|
|||||||
val userKeyPair = apiDB.getUserX25519KeyPair()
|
val userKeyPair = apiDB.getUserX25519KeyPair()
|
||||||
// Unwrap the message
|
// Unwrap the message
|
||||||
val groupDB = DatabaseFactory.getGroupDatabase(context)
|
val groupDB = DatabaseFactory.getGroupDatabase(context)
|
||||||
val groupID = doubleEncodeGroupID(groupPublicKey)
|
val correctGroupPublicKey = when {
|
||||||
|
groupPublicKey.isNotEmpty() -> groupPublicKey
|
||||||
|
!closedGroupUpdate.publicKey.isEmpty -> closedGroupUpdate.publicKey.toByteArray().toHexString()
|
||||||
|
else -> ""
|
||||||
|
}
|
||||||
|
val groupID = doubleEncodeGroupID(correctGroupPublicKey)
|
||||||
val group = groupDB.getGroup(groupID).orNull()
|
val group = groupDB.getGroup(groupID).orNull()
|
||||||
if (group == null) {
|
if (group == null) {
|
||||||
Log.d("Loki", "Ignoring closed group encryption key pair message for nonexistent group.")
|
Log.d("Loki", "Ignoring closed group encryption key pair message for nonexistent group.")
|
||||||
@ -753,7 +646,7 @@ object ClosedGroupsProtocolV2 {
|
|||||||
val proto = SignalServiceProtos.KeyPair.parseFrom(plaintext)
|
val proto = SignalServiceProtos.KeyPair.parseFrom(plaintext)
|
||||||
val keyPair = ECKeyPair(DjbECPublicKey(proto.publicKey.toByteArray().removing05PrefixIfNeeded()), DjbECPrivateKey(proto.privateKey.toByteArray()))
|
val keyPair = ECKeyPair(DjbECPublicKey(proto.publicKey.toByteArray().removing05PrefixIfNeeded()), DjbECPrivateKey(proto.privateKey.toByteArray()))
|
||||||
// Store it
|
// Store it
|
||||||
apiDB.addClosedGroupEncryptionKeyPair(keyPair, groupPublicKey)
|
apiDB.addClosedGroupEncryptionKeyPair(keyPair, correctGroupPublicKey)
|
||||||
Log.d("Loki", "Received a new closed group encryption key pair")
|
Log.d("Loki", "Received a new closed group encryption key pair")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user