communicate: RCS MLS group invitations could never be joined
- Commit
b1d9b4b0ee93aa70946fbb974b5d585fdb70ac69- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-07 11:28 UTC (29 days ago)
- Parents
058dd73
freshKeyPackage keeps the key package's private keys in the storage of our 1:1 pseudo-thread row (`e2e:<e164>`), but joinEncryptedGroup (called from handleWelcome with the sender's conversation id) only tried that conversation's storage, which never holds them, so OpenMLS could not open any Welcome. Hidden until now because native MLS never ran in JVM tests. Try the conversation's storage, then each `e2e:` key-package row; OpenMLS accepts only the one with the matching private key. A rejected candidate throws from JNI, so each attempt is caught on its own. RcsSlice3Pass2E2ETest's round trip now runs natively and models two devices (MLS state is per device, keyed by group id, so creator and joiner can't share one DB). All 227 rcs tests pass.
Diff
communicate/src/main/java/com/vayunmathur/communicate/data/rcs/RcsDatabase.kt
diff --git a/communicate/src/main/java/com/vayunmathur/communicate/data/rcs/RcsDatabase.kt b/communicate/src/main/java/com/vayunmathur/communicate/data/rcs/RcsDatabase.ktindex 847eead5d1..bc5ba6e86e 100644--- a/communicate/src/main/java/com/vayunmathur/communicate/data/rcs/RcsDatabase.kt+++ b/communicate/src/main/java/com/vayunmathur/communicate/data/rcs/RcsDatabase.kt@@ -282,6 +282,10 @@ interface RcsMlsGroupDao { @Query("SELECT * FROM rcs_mls_group WHERE conversationId = :conversationId LIMIT 1") suspend fun getByConversation(conversationId: String): RcsMlsGroup? + /** Rows of the 1:1 pseudo-threads (`e2e:<e164>`), where freshKeyPackage keeps key-package state. */+ @Query("SELECT * FROM rcs_mls_group WHERE conversationId LIKE 'e2e:%'")+ suspend fun getKeyPackageSnapshots(): List<RcsMlsGroup>+ @Insert(onConflict = OnConflictStrategy.REPLACE) suspend fun upsert(group: RcsMlsGroup)
communicate/src/main/java/com/vayunmathur/communicate/data/rcs/e2e/RcsE2E.kt
diff --git a/communicate/src/main/java/com/vayunmathur/communicate/data/rcs/e2e/RcsE2E.kt b/communicate/src/main/java/com/vayunmathur/communicate/data/rcs/e2e/RcsE2E.ktindex 87adac91ca..369faaa18f 100644--- a/communicate/src/main/java/com/vayunmathur/communicate/data/rcs/e2e/RcsE2E.kt+++ b/communicate/src/main/java/com/vayunmathur/communicate/data/rcs/e2e/RcsE2E.kt@@ -364,9 +364,18 @@ object RcsE2E { ): ByteArray? = withContext(Dispatchers.IO) { if (!RcsFeature.enabled || !RustMlsCrypto.isAvailable) return@withContext null val db = RcsDatabase.getDatabase(context)- val storage = db.mlsGroupDao().getByConversation(conversationId)?.storage ?: ByteArray(0)+ val dao = db.mlsGroupDao()+ // The Welcome is encrypted to one of OUR key packages, whose private keys live in the+ // storage freshKeyPackage keeps under the 1:1 pseudo-thread row (`e2e:<e164>`), not under+ // [conversationId] (the sender's thread). Try this conversation's storage first, then each+ // of those; OpenMLS only accepts the one holding the matching private key. A rejected+ // candidate throws from JNI, so each attempt is caught on its own.+ val candidates = listOf(dao.getByConversation(conversationId)?.storage ?: ByteArray(0)) ++ dao.getKeyPackageSnapshots().map { it.storage } runCatching {- val out = RustMlsCrypto.joinGroup(storage, welcome) ?: return@runCatching null+ val (storage, out) = candidates.firstNotNullOfOrNull { candidate ->+ runCatching { RustMlsCrypto.joinGroup(candidate, welcome) }.getOrNull()?.let { candidate to it }+ } ?: return@runCatching null val storageOut = out.getOrNull(0) ?: storage val groupId = out.getOrNull(1) ?: return@runCatching null db.mlsGroupDao().upsert(
communicate/src/test/java/com/vayunmathur/communicate/rcs/RcsSlice3Pass2E2ETest.kt
diff --git a/communicate/src/test/java/com/vayunmathur/communicate/rcs/RcsSlice3Pass2E2ETest.kt b/communicate/src/test/java/com/vayunmathur/communicate/rcs/RcsSlice3Pass2E2ETest.ktindex c6063f5f2a..dd6227cf8b 100644--- a/communicate/src/test/java/com/vayunmathur/communicate/rcs/RcsSlice3Pass2E2ETest.kt+++ b/communicate/src/test/java/com/vayunmathur/communicate/rcs/RcsSlice3Pass2E2ETest.kt@@ -40,13 +40,28 @@ class RcsSlice3Pass2E2ETest { private val context: Context get() = ApplicationProvider.getApplicationContext() private lateinit var testDb: RcsDatabase+ /** The peer's own device database: MLS state is per device, keyed by group id. */+ private lateinit var peerDb: RcsDatabase - @BeforeTest- fun setUp() {- testDb = androidx.room3.Room.inMemoryDatabaseBuilder(context, RcsDatabase::class.java)+ private fun newDb(): RcsDatabase =+ androidx.room3.Room.inMemoryDatabaseBuilder(context, RcsDatabase::class.java) .setDriver(androidx.sqlite.driver.bundled.BundledSQLiteDriver()) .allowMainThreadQueries() .build()++ private inline fun <T> onDevice(db: RcsDatabase, block: () -> T): T {+ RcsDatabase.overrideForTest = db+ try {+ return block()+ } finally {+ RcsDatabase.overrideForTest = testDb+ }+ }++ @BeforeTest+ fun setUp() {+ testDb = newDb()+ peerDb = newDb() RcsDatabase.overrideForTest = testDb RcsPeerKeys.clear() }@@ -55,6 +70,7 @@ class RcsSlice3Pass2E2ETest { fun tearDown() { RcsDatabase.overrideForTest = null testDb.close()+ peerDb.close() RcsPeerKeys.clear() } @@ -201,28 +217,31 @@ class RcsSlice3Pass2E2ETest { assertNotNull(row) assertEquals(identity.toList(), row.identity.toList()) - // Key packages for both sides.+ // Key packages for both sides, each on its own device. val myKp = RcsE2E.freshKeyPackage(context, me) assertNotNull(myKp)- val peerIdentity = RcsE2E.identityFor(context, peer)- assertNotNull(peerIdentity)- val peerKp = RcsE2E.freshKeyPackage(context, peer)+ val peerKp = onDevice(peerDb) {+ assertNotNull(RcsE2E.identityFor(context, peer))+ RcsE2E.freshKeyPackage(context, peer)+ } assertNotNull(peerKp) - // Create a group adding the peer, then join from the Welcome.+ // Create a group adding the peer; the peer joins from the Welcome on its device. The+ // Welcome is sealed to the peer's key package, whose private keys sit in the peer's+ // 1:1 pseudo-thread storage, not in the conversation it arrives on. val created = RcsE2E.createEncryptedGroup(context, me, "conv-mls-1", listOf(peerKp)) assertNotNull(created) val (commit, welcome) = created assertTrue(commit.isNotEmpty() && welcome.isNotEmpty())- val groupId = RcsE2E.joinEncryptedGroup(context, "conv-mls-2", welcome)+ val groupId = onDevice(peerDb) { RcsE2E.joinEncryptedGroup(context, me, welcome) } assertNotNull(groupId) assertEquals(groupId.toList(), RcsE2E.groupIdFor(context, "conv-mls-1")?.toList())- assertEquals(groupId.toList(), RcsE2E.groupIdFor(context, "conv-mls-2")?.toList())+ assertEquals(groupId.toList(), onDevice(peerDb) { RcsE2E.groupIdFor(context, me) }?.toList()) // Encrypt on one side, decrypt on the joined side. val payload = RcsE2E.encryptTo(context, me, "conv-mls-1", "secret hello".toByteArray(Charsets.UTF_8)) assertNotNull(payload)- val decrypted = RcsE2E.decryptFor(context, "conv-mls-2", payload)+ val decrypted = onDevice(peerDb) { RcsE2E.decryptFor(context, me, payload) } assertNotNull(decrypted) assertEquals("secret hello", decrypted.first.toString(Charsets.UTF_8)) assertTrue(decrypted.second)