From d00b54929faeb7a09230b874c85ea802103bdc34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Sun, 20 Jun 2021 22:34:54 +0200 Subject: [PATCH] crypto: Add the scaffolding to connect the SAS verification to the rust side --- .../android/sdk/internal/crypto/OlmMachine.kt | 126 +++++++++++++++--- .../verification/RustVerificationService.kt | 3 + 2 files changed, 109 insertions(+), 20 deletions(-) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/OlmMachine.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/OlmMachine.kt index f1e21e0b43..19d8233462 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/OlmMachine.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/OlmMachine.kt @@ -29,9 +29,11 @@ import org.matrix.android.sdk.api.session.crypto.MXCryptoError import org.matrix.android.sdk.api.session.crypto.verification.CancelCode import org.matrix.android.sdk.api.session.crypto.verification.EmojiRepresentation import org.matrix.android.sdk.api.session.crypto.verification.PendingVerificationRequest +import org.matrix.android.sdk.api.session.crypto.verification.SasVerificationTransaction import org.matrix.android.sdk.api.session.crypto.verification.ValidVerificationInfoReady import org.matrix.android.sdk.api.session.crypto.verification.ValidVerificationInfoRequest import org.matrix.android.sdk.api.session.crypto.verification.VerificationMethod +import org.matrix.android.sdk.api.session.crypto.verification.VerificationTxState import org.matrix.android.sdk.api.session.events.model.Content import org.matrix.android.sdk.api.session.events.model.Event import org.matrix.android.sdk.api.util.JsonDict @@ -63,6 +65,7 @@ import uniffi.olm.ProgressListener as RustProgressListener import uniffi.olm.Request import uniffi.olm.RequestType import uniffi.olm.Sas as InnerSas +import uniffi.olm.StartSasResult import uniffi.olm.VerificationRequest as InnerRequest import uniffi.olm.setLogger @@ -148,13 +151,15 @@ internal class VerificationRequest( fun accept_with_methods(methods: List): OutgoingVerificationRequest? { val stringMethods: MutableList = - methods.map { - when (it) { - VerificationMethod.QR_CODE_SCAN -> VERIFICATION_METHOD_QR_CODE_SCAN - VerificationMethod.QR_CODE_SHOW -> VERIFICATION_METHOD_QR_CODE_SHOW - VerificationMethod.SAS -> VERIFICATION_METHOD_SAS - } - }.toMutableList() + methods + .map { + when (it) { + VerificationMethod.QR_CODE_SCAN -> VERIFICATION_METHOD_QR_CODE_SCAN + VerificationMethod.QR_CODE_SHOW -> VERIFICATION_METHOD_QR_CODE_SHOW + VerificationMethod.SAS -> VERIFICATION_METHOD_SAS + } + } + .toMutableList() if (stringMethods.contains(VERIFICATION_METHOD_QR_CODE_SHOW) || stringMethods.contains(VERIFICATION_METHOD_QR_CODE_SCAN)) { @@ -180,6 +185,14 @@ internal class VerificationRequest( return this.inner.isReady } + suspend fun startSasVerification(): StartSasResult? { + refreshData() + + return withContext(Dispatchers.IO) { + machine.startSasVerification(inner.otherUserId, inner.flowId) + } + } + fun toPendingVerificationRequest(): PendingVerificationRequest { refreshData() val code = this.inner.cancelCode @@ -276,9 +289,12 @@ private fun toCancelCode(cancelCode: RustCancelCode): CancelCode { } } -internal class Sas(private val machine: InnerMachine, private var inner: InnerSas) { +public class SasVerification(private val machine: InnerMachine, private var inner: InnerSas) : + SasVerificationTransaction { + private var stateField: VerificationTxState = VerificationTxState.OnStarted + private fun refreshData() { - val sas = this.machine.getVerification(this.inner.flowId) + val sas = this.machine.getVerification(this.inner.otherUserId, this.inner.flowId) if (sas != null) { this.inner = sas @@ -287,6 +303,68 @@ internal class Sas(private val machine: InnerMachine, private var inner: InnerSa return } + override val isIncoming: Boolean + get() { + return false + } + + override var otherDeviceId: String? + get() { + return this.inner.otherDeviceId + } + set(value) { + if (value != null) { + this.inner.otherDeviceId = value + } + } + + override val otherUserId: String + get() { + return this.inner.otherUserId + } + + override var state: VerificationTxState + get() { + TODO() + } + set(v) { + this.stateField = v + } + + override val transactionId: String + get() { + return this.inner.flowId + } + + override fun cancel() { + TODO() + } + + override fun cancel(code: CancelCode) { + TODO() + } + + override fun shortCodeDoesNotMatch() { + TODO() + } + + override fun isToDeviceTransport(): Boolean { + return false + } + + override fun supportsDecimal(): Boolean { + return false + } + + override fun supportsEmoji(): Boolean { + return false + } + + override fun userHasVerifiedShortCode() { + // This is confirm + TODO() + } + fun isCanceled(): Boolean { refreshData() return this.inner.isCancelled @@ -308,19 +386,21 @@ internal class Sas(private val machine: InnerMachine, private var inner: InnerSa } fun accept(): OutgoingVerificationRequest? { - return this.machine.acceptVerification(inner.flowId) + return this.machine.acceptSasVerification(this.inner.otherUserId, inner.flowId) } @Throws(CryptoStoreErrorException::class) suspend fun confirm(): OutgoingVerificationRequest? = - withContext(Dispatchers.IO) { machine.confirmVerification(inner.flowId) } + withContext(Dispatchers.IO) { + machine.confirmVerification(inner.otherUserId, inner.flowId) + } - fun cancel(): OutgoingVerificationRequest? { - return this.machine.cancelVerification(inner.flowId) + fun cancelHelper(): OutgoingVerificationRequest? { + return this.machine.cancelVerification(this.inner.otherUserId, inner.flowId) } - fun emoji(): List { - val emojiIndex = this.machine.getEmojiIndex(this.inner.flowId) + override fun getEmojiCodeRepresentation(): List { + val emojiIndex = this.machine.getEmojiIndex(this.inner.otherUserId, this.inner.flowId) return if (emojiIndex != null) { emojiIndex.map { getEmojiForCode(it) } @@ -329,8 +409,14 @@ internal class Sas(private val machine: InnerMachine, private var inner: InnerSa } } - fun decimals(): List? { - return this.machine.getDecimals(this.inner.flowId) + override fun getDecimalCodeRepresentation(): String { + val decimals = this.machine.getDecimals(this.inner.otherUserId, this.inner.flowId) + + return if (decimals != null) { + decimals.joinToString(" ") + } else { + "" + } } } @@ -753,13 +839,13 @@ internal class OlmMachine( } /** Get an active verification */ - fun getVerification(flowId: String): Sas? { - val sas = this.inner.getVerification(flowId) + fun getVerification(userId: String, flowId: String): SasVerification? { + val sas = this.inner.getVerification(userId, flowId) return if (sas == null) { null } else { - Sas(this.inner, sas) + SasVerification(this.inner, sas) } } } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/verification/RustVerificationService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/verification/RustVerificationService.kt index 2acad69ebf..b561e90d5f 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/verification/RustVerificationService.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/verification/RustVerificationService.kt @@ -300,6 +300,9 @@ constructor( } } + // TODO create a class that handles this, the DefaultCryptoService has + // similar needs so we could share code there, beware that local echo seems + // to be handled here suspend fun sendRequest(request: OutgoingVerificationRequest) { when (request) { is OutgoingVerificationRequest.ToDevice -> {