2020-05-11 16:19:26 +10:00
package org.thoughtcrime.securesms.loki.protocol
import android.content.Context
2020-08-07 13:47:40 +10:00
import android.util.Log
2020-08-11 15:05:13 +10:00
import com.google.protobuf.ByteString
2020-05-22 11:14:36 +10:00
import nl.komponents.kovenant.Promise
2020-05-13 10:24:20 +10:00
import org.thoughtcrime.securesms.ApplicationContext
2020-05-11 16:19:26 +10:00
import org.thoughtcrime.securesms.database.Address
import org.thoughtcrime.securesms.database.DatabaseFactory
2020-08-11 09:59:07 +10:00
import org.thoughtcrime.securesms.database.ThreadDatabase
2020-05-13 12:29:31 +10:00
import org.thoughtcrime.securesms.loki.utilities.recipient
2020-08-12 11:20:45 +10:00
import org.thoughtcrime.securesms.mms.OutgoingGroupMediaMessage
2020-08-11 09:59:07 +10:00
import org.thoughtcrime.securesms.mms.OutgoingMediaMessage
2020-05-11 16:19:26 +10:00
import org.thoughtcrime.securesms.recipients.Recipient
import org.thoughtcrime.securesms.sms.MessageSender
import org.thoughtcrime.securesms.util.GroupUtil
2020-08-07 10:17:35 +10:00
import org.thoughtcrime.securesms.util.Hex
2020-05-11 16:19:26 +10:00
import org.thoughtcrime.securesms.util.TextSecurePreferences
2020-08-07 10:17:35 +10:00
import org.whispersystems.libsignal.ecc.Curve
2020-08-07 16:30:41 +10:00
import org.whispersystems.signalservice.internal.push.SignalServiceProtos
2020-08-12 11:20:45 +10:00
import org.whispersystems.signalservice.internal.push.SignalServiceProtos.GroupContext
2020-08-07 16:30:41 +10:00
import org.whispersystems.signalservice.loki.protocol.closedgroups.ClosedGroupRatchet
2020-08-07 10:17:35 +10:00
import org.whispersystems.signalservice.loki.protocol.closedgroups.ClosedGroupSenderKey
import org.whispersystems.signalservice.loki.protocol.closedgroups.SharedSenderKeysImplementation
import org.whispersystems.signalservice.loki.utilities.hexEncodedPrivateKey
import org.whispersystems.signalservice.loki.utilities.hexEncodedPublicKey
2020-08-07 16:30:41 +10:00
import org.whispersystems.signalservice.loki.utilities.toHexString
2020-05-22 10:41:31 +10:00
import java.util.*
2020-05-11 16:19:26 +10:00
object ClosedGroupsProtocol {
2020-08-10 16:38:40 +10:00
val isSharedSenderKeysEnabled = true
2020-08-12 12:22:16 +10:00
public fun createClosedGroup ( context : Context , name : String , members : Collection < String > ) : String {
2020-08-07 10:17:35 +10:00
// Prepare
val userPublicKey = TextSecurePreferences . getLocalNumber ( context )
// Generate a key pair for the group
val groupKeyPair = Curve . generateKeyPair ( )
val groupPublicKey = groupKeyPair . hexEncodedPublicKey // Includes the "05" prefix
val membersAsData = members . map { Hex . fromStringCondensed ( it ) }
// Create ratchets for all members
val senderKeys : List < ClosedGroupSenderKey > = members . map { publicKey ->
val ratchet = SharedSenderKeysImplementation . shared . generateRatchet ( groupPublicKey , publicKey )
ClosedGroupSenderKey ( Hex . fromStringCondensed ( ratchet . chainKey ) , ratchet . keyIndex , Hex . fromStringCondensed ( publicKey ) )
}
// Create the group
2020-08-11 15:05:13 +10:00
val groupID = GroupUtil . getEncodedId ( GroupUtil . getEncodedId ( Hex . fromStringCondensed ( groupPublicKey ) , false ) . toByteArray ( ) , false ) // Signal double encodes the group ID
2020-08-10 09:23:51 +10:00
val admins = setOf ( userPublicKey )
2020-08-07 10:17:35 +10:00
DatabaseFactory . getGroupDatabase ( context ) . create ( groupID , name , LinkedList < Address > ( members . map { Address . fromSerialized ( it ) } ) ,
2020-08-10 09:23:51 +10:00
null , null , LinkedList < Address > ( admins . map { Address . fromSerialized ( it ) } ) )
2020-08-07 10:17:35 +10:00
DatabaseFactory . getRecipientDatabase ( context ) . setProfileSharing ( Recipient . from ( context , Address . fromSerialized ( groupID ) , false ) , true )
// Establish sessions if needed
establishSessionsWithMembersIfNeeded ( context , members )
// Send a closed group update message to all members using established channels
2020-08-10 09:23:51 +10:00
val adminsAsData = admins . map { Hex . fromStringCondensed ( it ) }
2020-08-07 13:47:40 +10:00
val closedGroupUpdateKind = ClosedGroupUpdateMessageSendJob . Kind . New ( Hex . fromStringCondensed ( groupPublicKey ) , name , groupKeyPair . privateKey . serialize ( ) ,
senderKeys , membersAsData , adminsAsData )
for ( member in members ) {
val job = ClosedGroupUpdateMessageSendJob ( member , closedGroupUpdateKind )
ApplicationContext . getInstance ( context ) . jobManager . add ( job )
}
// TODO: Wait for the messages to finish sending
2020-08-07 10:17:35 +10:00
// Add the group to the user's set of public keys to poll for
DatabaseFactory . getSSKDatabase ( context ) . setClosedGroupPrivateKey ( groupPublicKey , groupKeyPair . hexEncodedPrivateKey )
// Notify the user
2020-08-12 11:20:45 +10:00
val threadID = DatabaseFactory . getThreadDatabase ( context ) . getThreadIdFor ( Recipient . from ( context , Address . fromSerialized ( groupID ) , false ) )
insertInfoMessage ( context , groupID , GroupContext . Type . UPDATE , name , members , admins , threadID )
2020-08-07 10:17:35 +10:00
// Return
2020-08-12 12:22:16 +10:00
return groupID
2020-08-07 10:17:35 +10:00
}
2020-08-07 13:47:40 +10:00
public fun addMembers ( context : Context , newMembers : Collection < String > , groupPublicKey : String ) {
// Prepare
2020-08-10 09:23:51 +10:00
val sskDatabase = DatabaseFactory . getSSKDatabase ( context )
2020-08-07 13:47:40 +10:00
val groupDB = DatabaseFactory . getGroupDatabase ( context )
2020-08-12 11:20:45 +10:00
val groupID = GroupUtil . getEncodedId ( GroupUtil . getEncodedId ( Hex . fromStringCondensed ( groupPublicKey ) , false ) . toByteArray ( ) , false ) // Signal double encodes the group ID
2020-08-07 13:47:40 +10:00
val group = groupDB . getGroup ( groupID ) . orNull ( )
if ( group == null ) {
Log . d ( " Loki " , " Can't add users to nonexistent closed group. " )
return
}
val name = group . title
2020-08-12 11:20:45 +10:00
val admins = group . admins . map { it . serialize ( ) }
val adminsAsData = admins . map { Hex . fromStringCondensed ( it ) }
2020-08-10 09:23:51 +10:00
val groupPrivateKey = DatabaseFactory . getSSKDatabase ( context ) . getClosedGroupPrivateKey ( groupPublicKey )
if ( groupPrivateKey == null ) {
2020-08-07 13:47:40 +10:00
Log . d ( " Loki " , " Couldn't get private key for closed group. " )
return
}
// Add the members to the member list
val members = group . members . map { it . serialize ( ) } . toMutableSet ( )
members . addAll ( newMembers )
val membersAsData = members . map { Hex . fromStringCondensed ( it ) }
// Generate ratchets for the new members
val senderKeys : List < ClosedGroupSenderKey > = 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 = ClosedGroupUpdateMessageSendJob . Kind . Info ( Hex . fromStringCondensed ( groupPublicKey ) , name ,
2020-08-12 11:20:45 +10:00
senderKeys , membersAsData , adminsAsData )
2020-08-07 13:47:40 +10:00
val job = ClosedGroupUpdateMessageSendJob ( groupPublicKey , closedGroupUpdateKind )
ApplicationContext . getInstance ( context ) . jobManager . add ( job )
// Establish sessions if needed
establishSessionsWithMembersIfNeeded ( context , newMembers )
// Send closed group update messages to the new members using established channels
2020-08-10 09:23:51 +10:00
val allSenderKeys = sskDatabase . getAllClosedGroupSenderKeys ( groupPublicKey ) + senderKeys
2020-08-07 13:47:40 +10:00
for ( member in members ) {
@Suppress ( " NAME_SHADOWING " )
2020-08-10 09:23:51 +10:00
val closedGroupUpdateKind = ClosedGroupUpdateMessageSendJob . Kind . New ( Hex . fromStringCondensed ( groupPublicKey ) , name ,
2020-08-12 11:20:45 +10:00
Hex . fromStringCondensed ( groupPrivateKey ) , allSenderKeys , membersAsData , adminsAsData )
2020-08-07 13:47:40 +10:00
@Suppress ( " NAME_SHADOWING " )
val job = ClosedGroupUpdateMessageSendJob ( member , closedGroupUpdateKind )
ApplicationContext . getInstance ( context ) . jobManager . add ( job )
}
// Update the group
groupDB . updateMembers ( groupID , members . map { Address . fromSerialized ( it ) } )
// Notify the user
2020-08-12 11:20:45 +10:00
val threadID = DatabaseFactory . getThreadDatabase ( context ) . getThreadIdFor ( Recipient . from ( context , Address . fromSerialized ( groupID ) , false ) )
insertInfoMessage ( context , groupID , GroupContext . Type . UPDATE , name , members , admins , threadID )
2020-08-07 15:41:53 +10:00
}
2020-08-11 12:20:17 +10:00
@JvmStatic
2020-08-07 15:41:53 +10:00
public fun leave ( context : Context , groupPublicKey : String ) {
val userPublicKey = TextSecurePreferences . getLocalNumber ( context )
removeMembers ( context , setOf ( userPublicKey ) , groupPublicKey )
}
public fun removeMembers ( context : Context , membersToRemove : Collection < String > , groupPublicKey : String ) {
val userPublicKey = TextSecurePreferences . getLocalNumber ( context )
val sskDatabase = DatabaseFactory . getSSKDatabase ( context )
val isUserLeaving = membersToRemove . contains ( userPublicKey )
if ( isUserLeaving && membersToRemove . count ( ) != 1 ) {
Log . d ( " Loki " , " Can't remove self and others simultaneously. " )
2020-08-10 09:23:51 +10:00
return
2020-08-07 15:41:53 +10:00
}
val groupDB = DatabaseFactory . getGroupDatabase ( context )
2020-08-12 11:20:45 +10:00
val groupID = GroupUtil . getEncodedId ( GroupUtil . getEncodedId ( Hex . fromStringCondensed ( groupPublicKey ) , false ) . toByteArray ( ) , false ) // Signal double encodes the group ID
2020-08-07 15:41:53 +10:00
val group = groupDB . getGroup ( groupID ) . orNull ( )
if ( group == null ) {
Log . d ( " Loki " , " Can't add users to nonexistent closed group. " )
return
}
val name = group . title
2020-08-12 11:20:45 +10:00
val admins = group . admins . map { it . serialize ( ) }
val adminsAsData = admins . map { Hex . fromStringCondensed ( it ) }
2020-08-07 15:41:53 +10:00
// Remove the members from the member list
val members = group . members . map { it . serialize ( ) } . toSet ( ) . minus ( membersToRemove )
val membersAsData = members . map { Hex . fromStringCondensed ( it ) }
// Send the update to the group (don't include new ratchets as everyone should regenerate new ratchets individually)
val closedGroupUpdateKind = ClosedGroupUpdateMessageSendJob . Kind . Info ( Hex . fromStringCondensed ( groupPublicKey ) ,
2020-08-12 11:20:45 +10:00
name , setOf ( ) , membersAsData , adminsAsData )
2020-08-07 15:41:53 +10:00
val job = ClosedGroupUpdateMessageSendJob ( groupPublicKey , closedGroupUpdateKind )
2020-08-12 11:56:00 +10:00
job . setContext ( context )
job . onRun ( ) // Run the job immediately
2020-08-07 15:41:53 +10:00
// Delete all ratchets (it's important that this happens after sending out the update)
sskDatabase . removeAllClosedGroupRatchets ( groupPublicKey )
// 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 )
} else {
// Establish sessions if needed
establishSessionsWithMembersIfNeeded ( context , members )
// 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 ) {
@Suppress ( " NAME_SHADOWING " )
val closedGroupUpdateKind = ClosedGroupUpdateMessageSendJob . Kind . SenderKey ( Hex . fromStringCondensed ( groupPublicKey ) , userSenderKey )
@Suppress ( " NAME_SHADOWING " )
val job = ClosedGroupUpdateMessageSendJob ( groupPublicKey , closedGroupUpdateKind )
ApplicationContext . getInstance ( context ) . jobManager . add ( job )
}
}
// Update the group
groupDB . updateMembers ( groupID , members . map { Address . fromSerialized ( it ) } )
// Notify the user
2020-08-12 11:20:45 +10:00
val type = if ( isUserLeaving ) GroupContext . Type . QUIT else GroupContext . Type . UPDATE
val threadID = DatabaseFactory . getThreadDatabase ( context ) . getThreadIdFor ( Recipient . from ( context , Address . fromSerialized ( groupID ) , false ) )
insertInfoMessage ( context , groupID , type , name , members , admins , threadID )
2020-08-07 13:47:40 +10:00
}
2020-08-12 12:08:10 +10:00
@JvmStatic
2020-08-10 09:23:51 +10:00
public fun requestSenderKey ( context : Context , groupPublicKey : String , senderPublicKey : String ) {
// Establish session if needed
ApplicationContext . getInstance ( context ) . sendSessionRequestIfNeeded ( senderPublicKey )
// Send the request
val closedGroupUpdateKind = ClosedGroupUpdateMessageSendJob . Kind . SenderKeyRequest ( Hex . fromStringCondensed ( groupPublicKey ) )
val job = ClosedGroupUpdateMessageSendJob ( senderPublicKey , closedGroupUpdateKind )
ApplicationContext . getInstance ( context ) . jobManager . add ( job )
}
2020-08-11 11:51:51 +10:00
@JvmStatic
2020-08-10 09:23:51 +10:00
public fun handleSharedSenderKeysUpdate ( context : Context , closedGroupUpdate : SignalServiceProtos . ClosedGroupUpdate , senderPublicKey : String ) {
2020-08-11 15:05:13 +10:00
if ( !is Valid ( closedGroupUpdate ) ) { return ; }
2020-08-07 16:30:41 +10:00
when ( closedGroupUpdate . type ) {
SignalServiceProtos . ClosedGroupUpdate . Type . NEW -> handleNewClosedGroup ( context , closedGroupUpdate )
2020-08-10 09:23:51 +10:00
SignalServiceProtos . ClosedGroupUpdate . Type . INFO -> handleClosedGroupUpdate ( context , closedGroupUpdate , senderPublicKey )
SignalServiceProtos . ClosedGroupUpdate . Type . SENDER _KEY _REQUEST -> handleSenderKeyRequest ( context , closedGroupUpdate , senderPublicKey )
SignalServiceProtos . ClosedGroupUpdate . Type . SENDER _KEY -> handleSenderKey ( context , closedGroupUpdate , senderPublicKey )
2020-08-07 16:30:41 +10:00
else -> {
// Do nothing
}
}
}
2020-08-11 15:05:13 +10:00
private fun isValid ( closedGroupUpdate : SignalServiceProtos . ClosedGroupUpdate ) : Boolean {
if ( closedGroupUpdate . groupPublicKey . isEmpty ) { return false }
when ( closedGroupUpdate . type ) {
SignalServiceProtos . ClosedGroupUpdate . Type . NEW -> {
return ! closedGroupUpdate . name . isNullOrEmpty ( ) && ! ( closedGroupUpdate . groupPrivateKey ?: ByteString . copyFrom ( ByteArray ( 0 ) ) ) . isEmpty
&& closedGroupUpdate . senderKeysCount > 0 && closedGroupUpdate . membersCount > 0 && closedGroupUpdate . adminsCount > 0
}
SignalServiceProtos . ClosedGroupUpdate . Type . INFO -> {
return ! closedGroupUpdate . name . isNullOrEmpty ( ) && closedGroupUpdate . membersCount > 0 && closedGroupUpdate . adminsCount > 0 // senderKeys may be empty
}
SignalServiceProtos . ClosedGroupUpdate . Type . SENDER _KEY -> return true
SignalServiceProtos . ClosedGroupUpdate . Type . SENDER _KEY _REQUEST -> return closedGroupUpdate . senderKeysCount > 0
else -> return false
}
}
2020-08-07 16:30:41 +10:00
public fun handleNewClosedGroup ( context : Context , closedGroupUpdate : SignalServiceProtos . ClosedGroupUpdate ) {
// Prepare
val sskDatabase = DatabaseFactory . getSSKDatabase ( context )
// Unwrap the message
val groupPublicKey = closedGroupUpdate . groupPublicKey . toByteArray ( ) . toHexString ( )
val name = closedGroupUpdate . name
val groupPrivateKey = closedGroupUpdate . groupPrivateKey . toByteArray ( )
val senderKeys = closedGroupUpdate . senderKeysList . map {
ClosedGroupSenderKey ( it . chainKey . toByteArray ( ) , it . keyIndex , it . publicKey . toByteArray ( ) )
}
val members = closedGroupUpdate . membersList . map { it . toByteArray ( ) . toHexString ( ) }
val admins = closedGroupUpdate . adminsList . map { it . toByteArray ( ) . toHexString ( ) }
// Persist the ratchets
senderKeys . forEach { senderKey ->
2020-08-10 09:23:51 +10:00
if ( ! members . contains ( senderKey . publicKey . toHexString ( ) ) ) { return @forEach }
2020-08-07 16:30:41 +10:00
val ratchet = ClosedGroupRatchet ( senderKey . chainKey . toHexString ( ) , senderKey . keyIndex , listOf ( ) )
sskDatabase . setClosedGroupRatchet ( groupPublicKey , senderKey . publicKey . toHexString ( ) , ratchet )
}
// Create the group
2020-08-12 11:20:45 +10:00
val groupID = GroupUtil . getEncodedId ( GroupUtil . getEncodedId ( Hex . fromStringCondensed ( groupPublicKey ) , false ) . toByteArray ( ) , false ) // Signal double encodes the group ID
2020-08-07 16:30:41 +10:00
DatabaseFactory . getGroupDatabase ( context ) . create ( groupID , name , LinkedList < Address > ( members . map { Address . fromSerialized ( it ) } ) ,
null , null , LinkedList < Address > ( admins . map { Address . fromSerialized ( it ) } ) )
DatabaseFactory . getRecipientDatabase ( context ) . setProfileSharing ( Recipient . from ( context , Address . fromSerialized ( groupID ) , false ) , true )
// Add the group to the user's set of public keys to poll for
2020-08-11 13:31:21 +10:00
sskDatabase . setClosedGroupPrivateKey ( groupPublicKey , groupPrivateKey . toHexString ( ) )
2020-08-07 16:30:41 +10:00
// Notify the user
2020-08-12 11:20:45 +10:00
val threadID = DatabaseFactory . getThreadDatabase ( context ) . getThreadIdFor ( Recipient . from ( context , Address . fromSerialized ( groupID ) , false ) )
insertInfoMessage ( context , groupID , GroupContext . Type . UPDATE , name , members , admins , threadID )
2020-08-07 16:30:41 +10:00
// Establish sessions if needed
establishSessionsWithMembersIfNeeded ( context , members )
}
2020-08-10 09:23:51 +10:00
public fun handleClosedGroupUpdate ( context : Context , closedGroupUpdate : SignalServiceProtos . ClosedGroupUpdate , senderPublicKey : String ) {
2020-08-07 16:30:41 +10:00
// Prepare
val userPublicKey = TextSecurePreferences . getLocalNumber ( context )
val sskDatabase = DatabaseFactory . getSSKDatabase ( context )
// Unwrap the message
val groupPublicKey = closedGroupUpdate . groupPublicKey . toByteArray ( ) . toHexString ( )
val name = closedGroupUpdate . name
val senderKeys = closedGroupUpdate . senderKeysList . map {
ClosedGroupSenderKey ( it . chainKey . toByteArray ( ) , it . keyIndex , it . publicKey . toByteArray ( ) )
}
val members = closedGroupUpdate . membersList . map { it . toByteArray ( ) . toHexString ( ) }
2020-08-12 11:20:45 +10:00
val admins = closedGroupUpdate . adminsList . map { it . toByteArray ( ) . toHexString ( ) }
2020-08-07 16:30:41 +10:00
val groupDB = DatabaseFactory . getGroupDatabase ( context )
2020-08-12 11:20:45 +10:00
val groupID = GroupUtil . getEncodedId ( GroupUtil . getEncodedId ( Hex . fromStringCondensed ( groupPublicKey ) , false ) . toByteArray ( ) , false ) // Signal double encodes the group ID
2020-08-07 16:30:41 +10:00
val group = groupDB . getGroup ( groupID ) . orNull ( )
if ( group == null ) {
2020-08-10 09:23:51 +10:00
Log . d ( " Loki " , " Ignoring closed group info message for nonexistent group. " )
2020-08-07 16:30:41 +10:00
return
}
val oldMembers = group . members . map { it . serialize ( ) }
// Check that the sender is a member of the group (before the update)
if ( ! oldMembers . contains ( senderPublicKey ) ) {
Log . d ( " Loki " , " Ignoring closed group info message from non-member. " )
2020-08-10 09:23:51 +10:00
return
2020-08-07 16:30:41 +10:00
}
// Store the ratchets for any new members (it's important that this happens before the code below)
senderKeys . forEach { senderKey ->
2020-08-10 09:23:51 +10:00
if ( ! members . contains ( senderKey . publicKey . toHexString ( ) ) ) { return @forEach }
2020-08-07 16:30:41 +10:00
val ratchet = ClosedGroupRatchet ( senderKey . chainKey . toHexString ( ) , senderKey . keyIndex , listOf ( ) )
sskDatabase . setClosedGroupRatchet ( groupPublicKey , senderKey . publicKey . toHexString ( ) , ratchet )
}
// Delete all ratchets and either:
// • Send out the user's new ratchet using established channels if other members of the group left or were removed
// • Remove the group from the user's set of public keys to poll for if the current user was among the members that were removed
val wasUserRemoved = ! members . contains ( userPublicKey )
if ( members . toSet ( ) . intersect ( oldMembers ) != oldMembers . toSet ( ) ) {
sskDatabase . removeAllClosedGroupRatchets ( groupPublicKey )
if ( wasUserRemoved ) {
sskDatabase . removeClosedGroupPrivateKey ( groupPublicKey )
} else {
establishSessionsWithMembersIfNeeded ( context , members )
val userRatchet = SharedSenderKeysImplementation . shared . generateRatchet ( groupPublicKey , userPublicKey )
val userSenderKey = ClosedGroupSenderKey ( Hex . fromStringCondensed ( userRatchet . chainKey ) , userRatchet . keyIndex , Hex . fromStringCondensed ( userPublicKey ) )
for ( member in members ) {
val closedGroupUpdateKind = ClosedGroupUpdateMessageSendJob . Kind . SenderKey ( Hex . fromStringCondensed ( groupPublicKey ) , userSenderKey )
val job = ClosedGroupUpdateMessageSendJob ( groupPublicKey , closedGroupUpdateKind )
ApplicationContext . getInstance ( context ) . jobManager . add ( job )
}
}
}
// Update the group
groupDB . updateTitle ( groupID , name )
groupDB . updateMembers ( groupID , members . map { Address . fromSerialized ( it ) } )
// Notify the user
2020-08-12 11:20:45 +10:00
val type = if ( wasUserRemoved ) GroupContext . Type . QUIT else GroupContext . Type . UPDATE
val threadID = DatabaseFactory . getThreadDatabase ( context ) . getThreadIdFor ( Recipient . from ( context , Address . fromSerialized ( groupID ) , false ) )
insertInfoMessage ( context , groupID , type , name , members , admins , threadID )
2020-08-07 16:30:41 +10:00
}
2020-08-10 09:23:51 +10:00
public fun handleSenderKeyRequest ( context : Context , closedGroupUpdate : SignalServiceProtos . ClosedGroupUpdate , senderPublicKey : String ) {
2020-08-07 16:30:41 +10:00
// Prepare
val userPublicKey = TextSecurePreferences . getLocalNumber ( context )
val groupPublicKey = closedGroupUpdate . groupPublicKey . toByteArray ( ) . toHexString ( )
val groupDB = DatabaseFactory . getGroupDatabase ( context )
2020-08-12 11:20:45 +10:00
val groupID = GroupUtil . getEncodedId ( GroupUtil . getEncodedId ( Hex . fromStringCondensed ( groupPublicKey ) , false ) . toByteArray ( ) , false ) // Signal double encodes the group ID
2020-08-07 16:30:41 +10:00
val group = groupDB . getGroup ( groupID ) . orNull ( )
if ( group == null ) {
2020-08-10 09:23:51 +10:00
Log . d ( " Loki " , " Ignoring closed group sender key request for nonexistent group. " )
2020-08-07 16:30:41 +10:00
return
}
// Check that the requesting user is a member of the group
if ( ! group . members . map { it . serialize ( ) } . contains ( senderPublicKey ) ) {
2020-08-10 09:23:51 +10:00
Log . d ( " Loki " , " Ignoring closed group sender key request from non-member. " )
return
2020-08-07 16:30:41 +10:00
}
// Respond to the request
ApplicationContext . getInstance ( context ) . sendSessionRequestIfNeeded ( senderPublicKey )
val userRatchet = SharedSenderKeysImplementation . shared . generateRatchet ( groupPublicKey , userPublicKey )
val userSenderKey = ClosedGroupSenderKey ( Hex . fromStringCondensed ( userRatchet . chainKey ) , userRatchet . keyIndex , Hex . fromStringCondensed ( userPublicKey ) )
val closedGroupUpdateKind = ClosedGroupUpdateMessageSendJob . Kind . SenderKey ( Hex . fromStringCondensed ( groupPublicKey ) , userSenderKey )
val job = ClosedGroupUpdateMessageSendJob ( senderPublicKey , closedGroupUpdateKind )
ApplicationContext . getInstance ( context ) . jobManager . add ( job )
}
2020-08-10 09:23:51 +10:00
public fun handleSenderKey ( context : Context , closedGroupUpdate : SignalServiceProtos . ClosedGroupUpdate , senderPublicKey : String ) {
2020-08-07 16:30:41 +10:00
// Prepare
val sskDatabase = DatabaseFactory . getSSKDatabase ( context )
val groupPublicKey = closedGroupUpdate . groupPublicKey . toByteArray ( ) . toHexString ( )
val groupDB = DatabaseFactory . getGroupDatabase ( context )
2020-08-12 11:20:45 +10:00
val groupID = GroupUtil . getEncodedId ( GroupUtil . getEncodedId ( Hex . fromStringCondensed ( groupPublicKey ) , false ) . toByteArray ( ) , false ) // Signal double encodes the group ID
2020-08-07 16:30:41 +10:00
val group = groupDB . getGroup ( groupID ) . orNull ( )
if ( group == null ) {
Log . d ( " Loki " , " Ignoring closed group sender key for nonexistent group. " )
return
}
val senderKeyProto = closedGroupUpdate . senderKeysList . firstOrNull ( )
if ( senderKeyProto == null ) {
Log . d ( " Loki " , " Ignoring invalid closed group sender key. " )
return
}
val senderKey = ClosedGroupSenderKey ( senderKeyProto . chainKey . toByteArray ( ) , senderKeyProto . keyIndex , senderKeyProto . publicKey . toByteArray ( ) )
// Check that the sending user is a member of the group
if ( ! group . members . map { it . serialize ( ) } . contains ( senderPublicKey ) ) {
Log . d ( " Loki " , " Ignoring closed group sender key from non-member. " )
2020-08-10 09:23:51 +10:00
return
}
if ( senderKeyProto . publicKey . toByteArray ( ) . toHexString ( ) != senderPublicKey ) {
Log . d ( " Loki " , " Ignoring invalid closed group sender key. " )
return
2020-08-07 16:30:41 +10:00
}
// Store the sender key
val ratchet = ClosedGroupRatchet ( senderKey . chainKey . toHexString ( ) , senderKey . keyIndex , listOf ( ) )
sskDatabase . setClosedGroupRatchet ( groupPublicKey , senderPublicKey , ratchet )
}
2020-05-12 16:28:35 +10:00
@JvmStatic
2020-08-07 09:15:36 +10:00
fun shouldIgnoreContentMessage ( context : Context , address : Address , groupID : String ? , senderPublicKey : String ) : Boolean {
if ( ! address . isClosedGroup || groupID == null ) { return false }
/ *
2020-07-15 12:24:43 +10:00
FileServerAPI . shared . getDeviceLinks ( senderPublicKey ) . timeout ( 6000 ) . get ( )
2020-05-13 12:29:31 +10:00
val senderMasterPublicKey = MultiDeviceProtocol . shared . getMasterDevice ( senderPublicKey )
val publicKeyToCheckFor = senderMasterPublicKey ?: senderPublicKey
2020-08-07 09:15:36 +10:00
* /
2020-05-13 12:29:31 +10:00
val members = DatabaseFactory . getGroupDatabase ( context ) . getGroupMembers ( groupID , true )
2020-08-07 09:15:36 +10:00
return ! members . contains ( recipient ( context , senderPublicKey ) )
2020-05-13 12:29:31 +10:00
}
@JvmStatic
2020-08-07 09:15:36 +10:00
fun getMessageDestinations ( context : Context , groupID : String ) : List < Address > {
if ( GroupUtil . isRSSFeed ( groupID ) ) { return listOf ( ) }
2020-05-12 15:29:00 +10:00
if ( GroupUtil . isOpenGroup ( groupID ) ) {
2020-08-07 09:15:36 +10:00
return listOf ( Address . fromSerialized ( groupID ) )
2020-05-12 15:29:00 +10:00
} else {
2020-08-11 19:32:10 +10:00
val groupPublicKey = GroupUtil . getDecodedId ( GroupUtil . getDecodedStringId ( groupID ) ) . toHexString ( )
2020-08-11 09:59:07 +10:00
if ( DatabaseFactory . getSSKDatabase ( context ) . isSSKBasedClosedGroup ( groupPublicKey ) ) {
return listOf ( Address . fromSerialized ( groupPublicKey ) )
} else {
return DatabaseFactory . getGroupDatabase ( context ) . getGroupMembers ( groupID , false ) . map { it . address }
}
2020-08-07 09:15:36 +10:00
/ *
2020-07-15 12:24:43 +10:00
return FileServerAPI . shared . getDeviceLinks ( members . map { it . address . serialize ( ) } . toSet ( ) ) . map {
2020-05-22 11:14:36 +10:00
val result = members . flatMap { member ->
MultiDeviceProtocol . shared . getAllLinkedDevices ( member . address . serialize ( ) ) . map { Address . fromSerialized ( it ) }
} . toMutableSet ( )
val userMasterPublicKey = TextSecurePreferences . getMasterHexEncodedPublicKey ( context )
if ( userMasterPublicKey != null && result . contains ( Address . fromSerialized ( userMasterPublicKey ) ) ) {
result . remove ( Address . fromSerialized ( userMasterPublicKey ) )
}
val userPublicKey = TextSecurePreferences . getLocalNumber ( context )
if ( userPublicKey != null && result . contains ( Address . fromSerialized ( userPublicKey ) ) ) {
result . remove ( Address . fromSerialized ( userPublicKey ) )
}
result . toList ( )
2020-05-12 15:29:00 +10:00
}
2020-08-07 09:15:36 +10:00
* /
2020-05-12 15:29:00 +10:00
}
}
@JvmStatic
2020-08-07 09:15:36 +10:00
fun leaveLegacyGroup ( context : Context , recipient : Recipient ) : Boolean {
2020-05-11 16:54:31 +10:00
if ( ! recipient . address . isClosedGroup ) { return true }
2020-05-11 16:19:26 +10:00
val threadID = DatabaseFactory . getThreadDatabase ( context ) . getThreadIdFor ( recipient )
2020-08-07 09:15:36 +10:00
val message = GroupUtil . createGroupLeaveMessage ( context , recipient ) . orNull ( )
if ( threadID < 0 || message == null ) { return false }
MessageSender . send ( context , message , threadID , false , null )
/ *
2020-05-11 16:54:31 +10:00
val masterPublicKey = TextSecurePreferences . getMasterHexEncodedPublicKey ( context )
2020-05-13 12:29:31 +10:00
val publicKeyToRemove = masterPublicKey ?: TextSecurePreferences . getLocalNumber ( context )
2020-08-07 09:15:36 +10:00
* /
val userPublicKey = TextSecurePreferences . getLocalNumber ( context )
2020-05-11 16:19:26 +10:00
val groupDatabase = DatabaseFactory . getGroupDatabase ( context )
val groupID = recipient . address . toGroupString ( )
groupDatabase . setActive ( groupID , false )
2020-08-07 09:15:36 +10:00
groupDatabase . remove ( groupID , Address . fromSerialized ( userPublicKey ) )
2020-05-11 16:19:26 +10:00
return true
}
2020-05-12 16:28:35 +10:00
@JvmStatic
2020-08-07 10:17:35 +10:00
fun establishSessionsWithMembersIfNeeded ( context : Context , members : Collection < String > ) {
2020-08-07 09:15:36 +10:00
@Suppress ( " NAME_SHADOWING " ) val members = members . toMutableSet ( )
/ *
2020-05-12 16:28:35 +10:00
val allDevices = members . flatMap { member ->
MultiDeviceProtocol . shared . getAllLinkedDevices ( member )
} . toMutableSet ( )
2020-05-13 12:29:31 +10:00
val userMasterPublicKey = TextSecurePreferences . getMasterHexEncodedPublicKey ( context )
if ( userMasterPublicKey != null && allDevices . contains ( userMasterPublicKey ) ) {
allDevices . remove ( userMasterPublicKey )
2020-05-12 16:28:35 +10:00
}
2020-08-07 09:15:36 +10:00
* /
2020-05-12 16:28:35 +10:00
val userPublicKey = TextSecurePreferences . getLocalNumber ( context )
2020-08-07 09:15:36 +10:00
if ( userPublicKey != null && members . contains ( userPublicKey ) ) {
members . remove ( userPublicKey )
2020-05-12 16:28:35 +10:00
}
2020-08-07 09:15:36 +10:00
for ( member in members ) {
ApplicationContext . getInstance ( context ) . sendSessionRequestIfNeeded ( member )
2020-05-12 16:28:35 +10:00
}
}
2020-08-12 11:20:45 +10:00
private fun insertInfoMessage ( context : Context , groupID : String , type : GroupContext . Type , name : String ,
members : Collection < String > , admins : Collection < String > , threadID : Long ) {
val recipient = Recipient . from ( context , Address . fromSerialized ( groupID ) , false )
val groupContextBuilder = GroupContext . newBuilder ( )
. setId ( ByteString . copyFrom ( GroupUtil . getDecodedId ( GroupUtil . getDecodedStringId ( groupID ) ) ) )
. setType ( type )
. setName ( name )
. addAllMembers ( members )
. addAllAdmins ( admins )
val infoMessage = OutgoingGroupMediaMessage ( recipient , groupContextBuilder . build ( ) , null , System . currentTimeMillis ( ) , 0 , null , listOf ( ) , listOf ( ) )
val mmsDB = DatabaseFactory . getMmsDatabase ( context )
val infoMessageID = mmsDB . insertMessageOutbox ( infoMessage , threadID , false , null )
mmsDB . markAsSent ( infoMessageID , true )
}
2020-05-11 16:19:26 +10:00
}