Skip to content

Commit 59ddf1a

Browse files
authored
Merge pull request #8654 from vector-im/bca/fix_8653_qr_code
Fix QR code login support in rust
2 parents 1b3be24 + a015eda commit 59ddf1a

File tree

11 files changed

+96
-19
lines changed

11 files changed

+96
-19
lines changed

changelog.d/8653.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix Login with QR code not working with rust crypto.

matrix-sdk-android/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ dependencies {
216216

217217
implementation libs.google.phonenumber
218218

219-
rustCryptoImplementation("org.matrix.rustcomponents:crypto-android:0.3.14")
219+
rustCryptoImplementation("org.matrix.rustcomponents:crypto-android:0.3.15")
220220
// rustCryptoApi project(":library:rustCrypto")
221221

222222
testImplementation libs.tests.junit

matrix-sdk-android/src/androidTest/java/org/matrix/android/sdk/internal/crypto/E2eeShareKeysHistoryTest.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import org.junit.Test
2828
import org.junit.runner.RunWith
2929
import org.junit.runners.JUnit4
3030
import org.junit.runners.MethodSorters
31+
import org.matrix.android.sdk.BuildConfig
3132
import org.matrix.android.sdk.InstrumentedTest
3233
import org.matrix.android.sdk.api.query.QueryStringValue
3334
import org.matrix.android.sdk.api.session.Session
@@ -196,6 +197,7 @@ class E2eeShareKeysHistoryTest : InstrumentedTest {
196197

197198
@Test
198199
fun testNeedsRotationFromSharedToWorldReadable() {
200+
Assume.assumeTrue("Test is flacky on legacy crypto", BuildConfig.FLAVOR == "rustCrypto")
199201
testRotationDueToVisibilityChange(RoomHistoryVisibility.SHARED, RoomHistoryVisibilityContent("world_readable"))
200202
}
201203

matrix-sdk-android/src/kotlinCrypto/java/org/matrix/android/sdk/internal/crypto/SecretShareManager.kt

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,13 @@ internal class SecretShareManager @Inject constructor(
136136
.w("handleSecretRequest() : malformed request norequestingDeviceId ")
137137
}
138138

139+
if (deviceId == credentials.deviceId) {
140+
return Unit.also {
141+
Timber.tag(loggerTag.value)
142+
.v("handleSecretRequest() : Ignore request from self device")
143+
}
144+
}
145+
139146
val device = cryptoStore.getUserDevice(credentials.userId, deviceId)
140147
?: return Unit.also {
141148
Timber.tag(loggerTag.value)
@@ -254,6 +261,37 @@ internal class SecretShareManager @Inject constructor(
254261
}
255262
}
256263

264+
suspend fun requestMissingSecrets() {
265+
// quick implementation for backward compatibility with rust, will request all secrets to all own devices
266+
val secretNames = listOf(MASTER_KEY_SSSS_NAME, SELF_SIGNING_KEY_SSSS_NAME, USER_SIGNING_KEY_SSSS_NAME, KEYBACKUP_SECRET_SSSS_NAME)
267+
268+
secretNames.forEach { secretName ->
269+
val toDeviceContent = SecretShareRequest(
270+
requestingDeviceId = credentials.deviceId,
271+
secretName = secretName,
272+
requestId = createUniqueTxnId()
273+
)
274+
275+
val contentMap = MXUsersDevicesMap<Any>()
276+
contentMap.setObject(credentials.userId, "*", toDeviceContent)
277+
278+
val params = SendToDeviceTask.Params(
279+
eventType = EventType.REQUEST_SECRET,
280+
contentMap = contentMap
281+
)
282+
try {
283+
withContext(coroutineDispatchers.io) {
284+
sendToDeviceTask.execute(params)
285+
}
286+
Timber.tag(loggerTag.value)
287+
.d("Secret request sent for $secretName")
288+
} catch (failure: Throwable) {
289+
Timber.tag(loggerTag.value)
290+
.w("Failed to request secret $secretName")
291+
}
292+
}
293+
}
294+
257295
suspend fun onSecretSendReceived(toDevice: Event, handleGossip: ((name: String, value: String) -> Boolean)) {
258296
Timber.tag(loggerTag.value)
259297
.i("onSecretSend() from ${toDevice.senderId} : onSecretSendReceived ${toDevice.content?.get("sender_key")}")

matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/rendezvous/Rendezvous.kt

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,6 @@ import org.matrix.android.sdk.api.rendezvous.model.SecureRendezvousChannelAlgori
3434
import org.matrix.android.sdk.api.rendezvous.transports.SimpleHttpRendezvousTransport
3535
import org.matrix.android.sdk.api.session.Session
3636
import org.matrix.android.sdk.api.session.crypto.crosssigning.DeviceTrustLevel
37-
import org.matrix.android.sdk.api.session.crypto.crosssigning.KEYBACKUP_SECRET_SSSS_NAME
38-
import org.matrix.android.sdk.api.session.crypto.crosssigning.MASTER_KEY_SSSS_NAME
39-
import org.matrix.android.sdk.api.session.crypto.crosssigning.SELF_SIGNING_KEY_SSSS_NAME
40-
import org.matrix.android.sdk.api.session.crypto.crosssigning.USER_SIGNING_KEY_SSSS_NAME
4137
import org.matrix.android.sdk.api.util.MatrixJsonParser
4238
import timber.log.Timber
4339

@@ -222,15 +218,10 @@ class Rendezvous(
222218
Timber.tag(TAG).i("No master key given by verifying device")
223219
}
224220

225-
// request secrets from the verifying device
226-
Timber.tag(TAG).i("Requesting secrets from $verifyingDeviceId")
221+
// request secrets from other sessions.
222+
Timber.tag(TAG).i("Requesting secrets from other sessions")
227223

228-
session.sharedSecretStorageService().let {
229-
it.requestSecret(MASTER_KEY_SSSS_NAME, verifyingDeviceId)
230-
it.requestSecret(SELF_SIGNING_KEY_SSSS_NAME, verifyingDeviceId)
231-
it.requestSecret(USER_SIGNING_KEY_SSSS_NAME, verifyingDeviceId)
232-
it.requestSecret(KEYBACKUP_SECRET_SSSS_NAME, verifyingDeviceId)
233-
}
224+
session.sharedSecretStorageService().requestMissingSecrets()
234225
} else {
235226
Timber.tag(TAG).i("Not doing verification")
236227
}

matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/securestorage/SharedSecretStorageService.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,5 +135,11 @@ interface SharedSecretStorageService {
135135

136136
fun checkShouldBeAbleToAccessSecrets(secretNames: List<String>, keyId: String?): IntegrityResult
137137

138+
@Deprecated("Requesting custom secrets not yet support by rust stack, prefer requestMissingSecrets")
138139
suspend fun requestSecret(name: String, myOtherDeviceId: String)
140+
141+
/**
142+
* Request the missing local secrets to other sessions.
143+
*/
144+
suspend fun requestMissingSecrets()
139145
}

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/secrets/DefaultSharedSecretStorageService.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,12 @@ internal class DefaultSharedSecretStorageService @Inject constructor(
385385
return IntegrityResult.Success(keyInfo.content.passphrase != null)
386386
}
387387

388+
@Deprecated("Requesting custom secrets not yet support by rust stack, prefer requestMissingSecrets")
388389
override suspend fun requestSecret(name: String, myOtherDeviceId: String) {
389390
secretShareManager.requestSecretTo(myOtherDeviceId, name)
390391
}
392+
393+
override suspend fun requestMissingSecrets() {
394+
secretShareManager.requestMissingSecrets()
395+
}
391396
}

matrix-sdk-android/src/rustCrypto/java/org/matrix/android/sdk/internal/crypto/OlmMachine.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ import org.matrix.rustcomponents.sdk.crypto.DeviceLists
7676
import org.matrix.rustcomponents.sdk.crypto.EncryptionSettings
7777
import org.matrix.rustcomponents.sdk.crypto.KeyRequestPair
7878
import org.matrix.rustcomponents.sdk.crypto.KeysImportResult
79+
import org.matrix.rustcomponents.sdk.crypto.LocalTrust
7980
import org.matrix.rustcomponents.sdk.crypto.Logger
8081
import org.matrix.rustcomponents.sdk.crypto.MegolmV1BackupKey
8182
import org.matrix.rustcomponents.sdk.crypto.Request
@@ -869,6 +870,11 @@ internal class OlmMachine @Inject constructor(
869870
}
870871
}
871872

873+
suspend fun requestMissingSecretsFromOtherSessions(): Boolean {
874+
return withContext(coroutineDispatchers.io) {
875+
inner.queryMissingSecretsFromOtherSessions()
876+
}
877+
}
872878
@Throws(CryptoStoreException::class)
873879
suspend fun enableBackupV1(key: String, version: String) {
874880
return withContext(coroutineDispatchers.computation) {
@@ -934,4 +940,11 @@ internal class OlmMachine @Inject constructor(
934940
inner.verifyBackup(serializedAuthData)
935941
}
936942
}
943+
944+
@Throws(CryptoStoreException::class)
945+
suspend fun setDeviceLocalTrust(userId: String, deviceId: String, trusted: Boolean) {
946+
withContext(coroutineDispatchers.io) {
947+
inner.setLocalTrust(userId, deviceId, if (trusted) LocalTrust.VERIFIED else LocalTrust.UNSET)
948+
}
949+
}
937950
}

matrix-sdk-android/src/rustCrypto/java/org/matrix/android/sdk/internal/crypto/RustCryptoService.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import org.matrix.android.sdk.api.MatrixCoroutineDispatchers
3131
import org.matrix.android.sdk.api.auth.UserInteractiveAuthInterceptor
3232
import org.matrix.android.sdk.api.crypto.MXCRYPTO_ALGORITHM_MEGOLM
3333
import org.matrix.android.sdk.api.crypto.MXCryptoConfig
34+
import org.matrix.android.sdk.api.extensions.orFalse
3435
import org.matrix.android.sdk.api.extensions.tryOrNull
3536
import org.matrix.android.sdk.api.listeners.ProgressListener
3637
import org.matrix.android.sdk.api.logger.LoggerTag
@@ -536,7 +537,8 @@ internal class RustCryptoService @Inject constructor(
536537
}
537538

538539
override suspend fun setDeviceVerification(trustLevel: DeviceTrustLevel, userId: String, deviceId: String) {
539-
TODO("Not yet implemented")
540+
Timber.w("Rust stack only support API to set local trust")
541+
olmMachine.setDeviceLocalTrust(userId, deviceId, trustLevel.isLocallyVerified().orFalse())
540542
}
541543

542544
/**

matrix-sdk-android/src/rustCrypto/java/org/matrix/android/sdk/internal/crypto/SecretShareManager.kt

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,29 @@
1616

1717
package org.matrix.android.sdk.internal.crypto
1818

19-
import org.matrix.android.sdk.BuildConfig
19+
import org.matrix.android.sdk.api.session.events.model.EventType
20+
import org.matrix.android.sdk.internal.crypto.network.OutgoingRequestsProcessor
21+
import org.matrix.rustcomponents.sdk.crypto.Request
2022
import timber.log.Timber
2123
import javax.inject.Inject
24+
import javax.inject.Provider
2225

23-
internal class SecretShareManager @Inject constructor() {
26+
internal class SecretShareManager @Inject constructor(
27+
private val olmMachine: Provider<OlmMachine>,
28+
private val outgoingRequestsProcessor: OutgoingRequestsProcessor) {
2429

2530
suspend fun requestSecretTo(deviceId: String, secretName: String) {
26-
// nop in rust?
27-
if (BuildConfig.DEBUG) TODO("requestSecretTo Not implemented in Rust")
28-
Timber.e("SecretShareManager Not supported in rust $deviceId, $secretName")
31+
Timber.w("SecretShareManager requesting custom secrets not supported $deviceId, $secretName")
32+
// rust stack only support requesting secrets defined in the spec (not custom secret yet)
33+
requestMissingSecrets()
34+
}
35+
36+
suspend fun requestMissingSecrets() {
37+
this.olmMachine.get().requestMissingSecretsFromOtherSessions()
38+
39+
// immediately send the requests
40+
outgoingRequestsProcessor.processOutgoingRequests(this.olmMachine.get()) {
41+
it is Request.ToDevice && it.eventType == EventType.REQUEST_SECRET
42+
}
2943
}
3044
}

0 commit comments

Comments
 (0)