mirror of
https://github.com/oxen-io/session-android.git
synced 2025-01-12 09:33:39 +00:00
Fix various SSK race conditions
This commit is contained in:
parent
0cd24905b7
commit
b192542427
@ -91,8 +91,9 @@ public class SQLCipherOpenHelper extends SQLiteOpenHelper {
|
|||||||
private static final int lokiV12 = 33;
|
private static final int lokiV12 = 33;
|
||||||
private static final int lokiV13 = 34;
|
private static final int lokiV13 = 34;
|
||||||
private static final int lokiV14_BACKUP_FILES = 35;
|
private static final int lokiV14_BACKUP_FILES = 35;
|
||||||
|
private static final int lokiV15 = 36;
|
||||||
|
|
||||||
private static final int DATABASE_VERSION = lokiV14_BACKUP_FILES; // Loki - onUpgrade(...) must be updated to use Loki version numbers if Signal makes any database changes
|
private static final int DATABASE_VERSION = lokiV15;
|
||||||
private static final String DATABASE_NAME = "signal.db";
|
private static final String DATABASE_NAME = "signal.db";
|
||||||
|
|
||||||
private final Context context;
|
private final Context context;
|
||||||
@ -164,7 +165,8 @@ public class SQLCipherOpenHelper extends SQLiteOpenHelper {
|
|||||||
db.execSQL(LokiUserDatabase.getCreateDisplayNameTableCommand());
|
db.execSQL(LokiUserDatabase.getCreateDisplayNameTableCommand());
|
||||||
db.execSQL(LokiUserDatabase.getCreateServerDisplayNameTableCommand());
|
db.execSQL(LokiUserDatabase.getCreateServerDisplayNameTableCommand());
|
||||||
db.execSQL(LokiBackupFilesDatabase.getCreateTableCommand());
|
db.execSQL(LokiBackupFilesDatabase.getCreateTableCommand());
|
||||||
db.execSQL(SharedSenderKeysDatabase.getCreateClosedGroupRatchetTableCommand());
|
db.execSQL(SharedSenderKeysDatabase.getCreateOldClosedGroupRatchetTableCommand());
|
||||||
|
db.execSQL(SharedSenderKeysDatabase.getCreateCurrentClosedGroupRatchetTableCommand());
|
||||||
db.execSQL(SharedSenderKeysDatabase.getCreateClosedGroupPrivateKeyTableCommand());
|
db.execSQL(SharedSenderKeysDatabase.getCreateClosedGroupPrivateKeyTableCommand());
|
||||||
|
|
||||||
executeStatements(db, SmsDatabase.CREATE_INDEXS);
|
executeStatements(db, SmsDatabase.CREATE_INDEXS);
|
||||||
@ -614,7 +616,7 @@ public class SQLCipherOpenHelper extends SQLiteOpenHelper {
|
|||||||
|
|
||||||
if (oldVersion < lokiV12) {
|
if (oldVersion < lokiV12) {
|
||||||
db.execSQL(LokiAPIDatabase.getCreateLastMessageHashValueTable2Command());
|
db.execSQL(LokiAPIDatabase.getCreateLastMessageHashValueTable2Command());
|
||||||
db.execSQL(SharedSenderKeysDatabase.getCreateClosedGroupRatchetTableCommand());
|
db.execSQL(SharedSenderKeysDatabase.getCreateCurrentClosedGroupRatchetTableCommand());
|
||||||
db.execSQL(SharedSenderKeysDatabase.getCreateClosedGroupPrivateKeyTableCommand());
|
db.execSQL(SharedSenderKeysDatabase.getCreateClosedGroupPrivateKeyTableCommand());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -626,6 +628,10 @@ public class SQLCipherOpenHelper extends SQLiteOpenHelper {
|
|||||||
db.execSQL(LokiBackupFilesDatabase.getCreateTableCommand());
|
db.execSQL(LokiBackupFilesDatabase.getCreateTableCommand());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (oldVersion < lokiV15) {
|
||||||
|
db.execSQL(SharedSenderKeysDatabase.getCreateOldClosedGroupRatchetTableCommand());
|
||||||
|
}
|
||||||
|
|
||||||
db.setTransactionSuccessful();
|
db.setTransactionSuccessful();
|
||||||
} finally {
|
} finally {
|
||||||
db.endTransaction();
|
db.endTransaction();
|
||||||
|
@ -7,6 +7,7 @@ import org.thoughtcrime.securesms.database.helpers.SQLCipherOpenHelper
|
|||||||
import org.thoughtcrime.securesms.loki.utilities.*
|
import org.thoughtcrime.securesms.loki.utilities.*
|
||||||
import org.thoughtcrime.securesms.util.Hex
|
import org.thoughtcrime.securesms.util.Hex
|
||||||
import org.whispersystems.signalservice.loki.protocol.closedgroups.ClosedGroupRatchet
|
import org.whispersystems.signalservice.loki.protocol.closedgroups.ClosedGroupRatchet
|
||||||
|
import org.whispersystems.signalservice.loki.protocol.closedgroups.ClosedGroupRatchetCollectionType
|
||||||
import org.whispersystems.signalservice.loki.protocol.closedgroups.ClosedGroupSenderKey
|
import org.whispersystems.signalservice.loki.protocol.closedgroups.ClosedGroupSenderKey
|
||||||
import org.whispersystems.signalservice.loki.protocol.closedgroups.SharedSenderKeysDatabaseProtocol
|
import org.whispersystems.signalservice.loki.protocol.closedgroups.SharedSenderKeysDatabaseProtocol
|
||||||
import org.whispersystems.signalservice.loki.utilities.PublicKeyValidation
|
import org.whispersystems.signalservice.loki.utilities.PublicKeyValidation
|
||||||
@ -17,13 +18,18 @@ class SharedSenderKeysDatabase(context: Context, helper: SQLCipherOpenHelper) :
|
|||||||
// Shared
|
// Shared
|
||||||
private val closedGroupPublicKey = "closed_group_public_key"
|
private val closedGroupPublicKey = "closed_group_public_key"
|
||||||
// Ratchets
|
// Ratchets
|
||||||
private val closedGroupRatchetTable = "closed_group_ratchet_table"
|
private val oldClosedGroupRatchetTable = "old_closed_group_ratchet_table"
|
||||||
|
private val currentClosedGroupRatchetTable = "closed_group_ratchet_table"
|
||||||
private val senderPublicKey = "sender_public_key"
|
private val senderPublicKey = "sender_public_key"
|
||||||
private val chainKey = "chain_key"
|
private val chainKey = "chain_key"
|
||||||
private val keyIndex = "key_index"
|
private val keyIndex = "key_index"
|
||||||
private val messageKeys = "message_keys"
|
private val messageKeys = "message_keys"
|
||||||
@JvmStatic val createClosedGroupRatchetTableCommand
|
@JvmStatic val createOldClosedGroupRatchetTableCommand
|
||||||
= "CREATE TABLE $closedGroupRatchetTable ($closedGroupPublicKey STRING, $senderPublicKey STRING, $chainKey STRING, " +
|
= "CREATE TABLE $currentClosedGroupRatchetTable ($closedGroupPublicKey STRING, $senderPublicKey STRING, $chainKey STRING, " +
|
||||||
|
"$keyIndex INTEGER DEFAULT 0, $messageKeys TEXT, PRIMARY KEY ($closedGroupPublicKey, $senderPublicKey));"
|
||||||
|
// Private keys
|
||||||
|
@JvmStatic val createCurrentClosedGroupRatchetTableCommand
|
||||||
|
= "CREATE TABLE $currentClosedGroupRatchetTable ($closedGroupPublicKey STRING, $senderPublicKey STRING, $chainKey STRING, " +
|
||||||
"$keyIndex INTEGER DEFAULT 0, $messageKeys TEXT, PRIMARY KEY ($closedGroupPublicKey, $senderPublicKey));"
|
"$keyIndex INTEGER DEFAULT 0, $messageKeys TEXT, PRIMARY KEY ($closedGroupPublicKey, $senderPublicKey));"
|
||||||
// Private keys
|
// Private keys
|
||||||
private val closedGroupPrivateKeyTable = "closed_group_private_key_table"
|
private val closedGroupPrivateKeyTable = "closed_group_private_key_table"
|
||||||
@ -32,11 +38,18 @@ class SharedSenderKeysDatabase(context: Context, helper: SQLCipherOpenHelper) :
|
|||||||
= "CREATE TABLE $closedGroupPrivateKeyTable ($closedGroupPublicKey STRING PRIMARY KEY, $closedGroupPrivateKey STRING);"
|
= "CREATE TABLE $closedGroupPrivateKeyTable ($closedGroupPublicKey STRING PRIMARY KEY, $closedGroupPrivateKey STRING);"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun getTable(collection: ClosedGroupRatchetCollectionType): String {
|
||||||
|
return when (collection) {
|
||||||
|
ClosedGroupRatchetCollectionType.Old -> oldClosedGroupRatchetTable
|
||||||
|
ClosedGroupRatchetCollectionType.Current -> currentClosedGroupRatchetTable
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// region Ratchets & Sender Keys
|
// region Ratchets & Sender Keys
|
||||||
override fun getClosedGroupRatchet(groupPublicKey: String, senderPublicKey: String): ClosedGroupRatchet? {
|
override fun getClosedGroupRatchet(groupPublicKey: String, senderPublicKey: String, collection: ClosedGroupRatchetCollectionType): ClosedGroupRatchet? {
|
||||||
val database = databaseHelper.readableDatabase
|
val database = databaseHelper.readableDatabase
|
||||||
val query = "${Companion.closedGroupPublicKey} = ? AND ${Companion.senderPublicKey} = ?"
|
val query = "${Companion.closedGroupPublicKey} = ? AND ${Companion.senderPublicKey} = ?"
|
||||||
return database.get(closedGroupRatchetTable, query, arrayOf( groupPublicKey, senderPublicKey )) { cursor ->
|
return database.get(getTable(collection), query, arrayOf( groupPublicKey, senderPublicKey )) { cursor ->
|
||||||
val chainKey = cursor.getString(Companion.chainKey)
|
val chainKey = cursor.getString(Companion.chainKey)
|
||||||
val keyIndex = cursor.getInt(Companion.keyIndex)
|
val keyIndex = cursor.getInt(Companion.keyIndex)
|
||||||
val messageKeys = cursor.getString(Companion.messageKeys).split("-")
|
val messageKeys = cursor.getString(Companion.messageKeys).split("-")
|
||||||
@ -44,7 +57,7 @@ class SharedSenderKeysDatabase(context: Context, helper: SQLCipherOpenHelper) :
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun setClosedGroupRatchet(groupPublicKey: String, senderPublicKey: String, ratchet: ClosedGroupRatchet) {
|
override fun setClosedGroupRatchet(groupPublicKey: String, senderPublicKey: String, ratchet: ClosedGroupRatchet, collection: ClosedGroupRatchetCollectionType) {
|
||||||
val database = databaseHelper.writableDatabase
|
val database = databaseHelper.writableDatabase
|
||||||
val values = ContentValues()
|
val values = ContentValues()
|
||||||
values.put(Companion.closedGroupPublicKey, groupPublicKey)
|
values.put(Companion.closedGroupPublicKey, groupPublicKey)
|
||||||
@ -53,23 +66,33 @@ class SharedSenderKeysDatabase(context: Context, helper: SQLCipherOpenHelper) :
|
|||||||
values.put(Companion.keyIndex, ratchet.keyIndex)
|
values.put(Companion.keyIndex, ratchet.keyIndex)
|
||||||
values.put(Companion.messageKeys, ratchet.messageKeys.joinToString("-"))
|
values.put(Companion.messageKeys, ratchet.messageKeys.joinToString("-"))
|
||||||
val query = "${Companion.closedGroupPublicKey} = ? AND ${Companion.senderPublicKey} = ?"
|
val query = "${Companion.closedGroupPublicKey} = ? AND ${Companion.senderPublicKey} = ?"
|
||||||
database.insertOrUpdate(closedGroupRatchetTable, values, query, arrayOf( groupPublicKey, senderPublicKey ))
|
database.insertOrUpdate(getTable(collection), values, query, arrayOf( groupPublicKey, senderPublicKey ))
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun removeAllClosedGroupRatchets(groupPublicKey: String) {
|
override fun removeAllClosedGroupRatchets(groupPublicKey: String, collection: ClosedGroupRatchetCollectionType) {
|
||||||
val database = databaseHelper.writableDatabase
|
val database = databaseHelper.writableDatabase
|
||||||
val query = "${Companion.closedGroupPublicKey} = ?"
|
val query = "${Companion.closedGroupPublicKey} = ?"
|
||||||
database.delete(closedGroupRatchetTable, query, arrayOf( groupPublicKey ))
|
database.delete(getTable(collection), query, arrayOf( groupPublicKey ))
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getAllClosedGroupSenderKeys(groupPublicKey: String): Set<ClosedGroupSenderKey> {
|
override fun getAllClosedGroupRatchets(groupPublicKey: String, collection: ClosedGroupRatchetCollectionType): Set<Pair<String, ClosedGroupRatchet>> {
|
||||||
val database = databaseHelper.readableDatabase
|
val database = databaseHelper.readableDatabase
|
||||||
val query = "${Companion.closedGroupPublicKey} = ?"
|
val query = "${Companion.closedGroupPublicKey} = ?"
|
||||||
return database.getAll(closedGroupRatchetTable, query, arrayOf( groupPublicKey )) { cursor ->
|
return database.getAll(getTable(collection), query, arrayOf( groupPublicKey )) { cursor ->
|
||||||
val chainKey = cursor.getString(Companion.chainKey)
|
val chainKey = cursor.getString(Companion.chainKey)
|
||||||
val keyIndex = cursor.getInt(Companion.keyIndex)
|
val keyIndex = cursor.getInt(Companion.keyIndex)
|
||||||
|
val messageKeys = cursor.getString(Companion.messageKeys).split("-")
|
||||||
val senderPublicKey = cursor.getString(Companion.senderPublicKey)
|
val senderPublicKey = cursor.getString(Companion.senderPublicKey)
|
||||||
ClosedGroupSenderKey(Hex.fromStringCondensed(chainKey), keyIndex, Hex.fromStringCondensed(senderPublicKey))
|
val ratchet = ClosedGroupRatchet(chainKey, keyIndex, messageKeys)
|
||||||
|
Pair(senderPublicKey, ratchet)
|
||||||
|
}.toSet()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getAllClosedGroupSenderKeys(groupPublicKey: String, collection: ClosedGroupRatchetCollectionType): Set<ClosedGroupSenderKey> {
|
||||||
|
return getAllClosedGroupRatchets(groupPublicKey, collection).map { pair ->
|
||||||
|
val senderPublicKey = pair.first
|
||||||
|
val ratchet = pair.second
|
||||||
|
ClosedGroupSenderKey(Hex.fromStringCondensed(ratchet.chainKey), ratchet.keyIndex, Hex.fromStringCondensed(senderPublicKey))
|
||||||
}.toSet()
|
}.toSet()
|
||||||
}
|
}
|
||||||
// endregion
|
// endregion
|
||||||
|
@ -27,6 +27,7 @@ import org.whispersystems.signalservice.internal.push.SignalServiceProtos
|
|||||||
import org.whispersystems.signalservice.internal.push.SignalServiceProtos.GroupContext
|
import org.whispersystems.signalservice.internal.push.SignalServiceProtos.GroupContext
|
||||||
import org.whispersystems.signalservice.loki.api.SnodeAPI
|
import org.whispersystems.signalservice.loki.api.SnodeAPI
|
||||||
import org.whispersystems.signalservice.loki.protocol.closedgroups.ClosedGroupRatchet
|
import org.whispersystems.signalservice.loki.protocol.closedgroups.ClosedGroupRatchet
|
||||||
|
import org.whispersystems.signalservice.loki.protocol.closedgroups.ClosedGroupRatchetCollectionType
|
||||||
import org.whispersystems.signalservice.loki.protocol.closedgroups.ClosedGroupSenderKey
|
import org.whispersystems.signalservice.loki.protocol.closedgroups.ClosedGroupSenderKey
|
||||||
import org.whispersystems.signalservice.loki.protocol.closedgroups.SharedSenderKeysImplementation
|
import org.whispersystems.signalservice.loki.protocol.closedgroups.SharedSenderKeysImplementation
|
||||||
import org.whispersystems.signalservice.loki.utilities.hexEncodedPrivateKey
|
import org.whispersystems.signalservice.loki.utilities.hexEncodedPrivateKey
|
||||||
@ -151,6 +152,13 @@ object ClosedGroupsProtocol {
|
|||||||
job.setContext(context)
|
job.setContext(context)
|
||||||
job.onRun() // Run the job immediately
|
job.onRun() // Run the job immediately
|
||||||
}
|
}
|
||||||
|
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)
|
// Delete all ratchets (it's important that this happens * after * sending out the update)
|
||||||
sskDatabase.removeAllClosedGroupRatchets(groupPublicKey)
|
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
|
// 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
|
||||||
@ -366,6 +374,13 @@ object ClosedGroupsProtocol {
|
|||||||
val wasAnyUserRemoved = members.toSet().intersect(oldMembers) != oldMembers.toSet()
|
val wasAnyUserRemoved = members.toSet().intersect(oldMembers) != oldMembers.toSet()
|
||||||
val wasSenderRemoved = !members.contains(senderPublicKey)
|
val wasSenderRemoved = !members.contains(senderPublicKey)
|
||||||
if (wasAnyUserRemoved) {
|
if (wasAnyUserRemoved) {
|
||||||
|
val allOldRatchets = sskDatabase.getAllClosedGroupRatchets(groupPublicKey, ClosedGroupRatchetCollectionType.Current)
|
||||||
|
for (pair in allOldRatchets) {
|
||||||
|
@Suppress("NAME_SHADOWING") val senderPublicKey = pair.first
|
||||||
|
val ratchet = pair.second
|
||||||
|
val collection = ClosedGroupRatchetCollectionType.Old
|
||||||
|
sskDatabase.setClosedGroupRatchet(groupPublicKey, senderPublicKey, ratchet, collection)
|
||||||
|
}
|
||||||
sskDatabase.removeAllClosedGroupRatchets(groupPublicKey)
|
sskDatabase.removeAllClosedGroupRatchets(groupPublicKey)
|
||||||
if (wasCurrentUserRemoved) {
|
if (wasCurrentUserRemoved) {
|
||||||
sskDatabase.removeClosedGroupPrivateKey(groupPublicKey)
|
sskDatabase.removeClosedGroupPrivateKey(groupPublicKey)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user