From 846242217b9d441cb8f44379f0018d792241e71e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Fri, 25 Jun 2021 14:06:48 +0200 Subject: [PATCH] crypto: Move the VerificationRequest into a separate file --- .../android/sdk/internal/crypto/OlmMachine.kt | 152 ---------------- .../internal/crypto/VerificationRequest.kt | 172 ++++++++++++++++++ .../verification/RustVerificationService.kt | 2 - 3 files changed, 172 insertions(+), 154 deletions(-) create mode 100644 matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/VerificationRequest.kt 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 7d93ee715e..3bbd7b4524 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 @@ -26,12 +26,6 @@ import kotlinx.coroutines.runBlocking import kotlinx.coroutines.withContext import org.matrix.android.sdk.api.listeners.ProgressListener 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.PendingVerificationRequest -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.safeValueOf 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 @@ -40,10 +34,6 @@ import org.matrix.android.sdk.internal.crypto.model.CryptoDeviceInfo import org.matrix.android.sdk.internal.crypto.model.ImportRoomKeysResult import org.matrix.android.sdk.internal.crypto.model.MXUsersDevicesMap import org.matrix.android.sdk.internal.crypto.model.rest.UnsignedDeviceInfo -import org.matrix.android.sdk.internal.crypto.model.rest.VERIFICATION_METHOD_QR_CODE_SCAN -import org.matrix.android.sdk.internal.crypto.model.rest.VERIFICATION_METHOD_QR_CODE_SHOW -import org.matrix.android.sdk.internal.crypto.model.rest.VERIFICATION_METHOD_RECIPROCATE -import org.matrix.android.sdk.internal.crypto.model.rest.VERIFICATION_METHOD_SAS import org.matrix.android.sdk.internal.di.MoshiProvider import org.matrix.android.sdk.internal.session.sync.model.DeviceListResponse import org.matrix.android.sdk.internal.session.sync.model.DeviceOneTimeKeysCountSyncResponse @@ -56,13 +46,10 @@ import uniffi.olm.DeviceLists import uniffi.olm.KeyRequestPair import uniffi.olm.Logger import uniffi.olm.OlmMachine as InnerMachine -import uniffi.olm.OutgoingVerificationRequest import uniffi.olm.ProgressListener as RustProgressListener import uniffi.olm.Request import uniffi.olm.RequestType import uniffi.olm.Sas -import uniffi.olm.StartSasResult -import uniffi.olm.VerificationRequest as InnerRequest import uniffi.olm.setLogger class CryptoLogger : Logger { @@ -130,145 +117,6 @@ internal class DeviceUpdateObserver { } } -internal class VerificationRequest( - private val machine: InnerMachine, - private var inner: InnerRequest -) { - private fun refreshData() { - val request = this.machine.getVerificationRequest(this.inner.otherUserId, this.inner.flowId) - - if (request != null) { - this.inner = request - } - - return - } - - fun acceptWithMethods(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() - - if (stringMethods.contains(VERIFICATION_METHOD_QR_CODE_SHOW) || - stringMethods.contains(VERIFICATION_METHOD_QR_CODE_SCAN)) { - stringMethods.add(VERIFICATION_METHOD_RECIPROCATE) - } - - return this.machine.acceptVerificationRequest( - this.inner.otherUserId, this.inner.flowId, stringMethods) - } - - fun isCanceled(): Boolean { - refreshData() - return this.inner.isCancelled - } - - fun isDone(): Boolean { - refreshData() - return this.inner.isDone - } - - fun isReady(): Boolean { - refreshData() - 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 - - val cancelCode = - if (code != null) { - safeValueOf(code) - } else { - null - } - - val ourMethods = this.inner.ourMethods - val theirMethods = this.inner.theirMethods - val otherDeviceId = this.inner.otherDeviceId - - var requestInfo: ValidVerificationInfoRequest? = null - var readyInfo: ValidVerificationInfoReady? = null - - if (this.inner.weStarted && ourMethods != null) { - requestInfo = - ValidVerificationInfoRequest( - this.inner.flowId, - this.machine.deviceId(), - ourMethods, - null, - ) - } else if (!this.inner.weStarted && ourMethods != null) { - readyInfo = - ValidVerificationInfoReady( - this.inner.flowId, - this.machine.deviceId(), - ourMethods, - ) - } - - if (this.inner.weStarted && theirMethods != null && otherDeviceId != null) { - readyInfo = - ValidVerificationInfoReady( - this.inner.flowId, - otherDeviceId, - theirMethods, - ) - } else if (!this.inner.weStarted && theirMethods != null && otherDeviceId != null) { - requestInfo = - ValidVerificationInfoRequest( - this.inner.flowId, - otherDeviceId, - theirMethods, - System.currentTimeMillis(), - ) - } - - return PendingVerificationRequest( - // Creation time - System.currentTimeMillis(), - // Who initiated the request - !this.inner.weStarted, - // Local echo id, what to do here? - this.inner.flowId, - // other user - this.inner.otherUserId, - // room id - this.inner.roomId, - // transaction id - this.inner.flowId, - // val requestInfo: ValidVerificationInfoRequest? = null, - requestInfo, - // val readyInfo: ValidVerificationInfoReady? = null, - readyInfo, - // cancel code if there is one - cancelCode, - // are we done/successful - this.inner.isDone, - // did another device answer the request - this.inner.isPassive, - // devices that should receive the events we send out - null, - ) - } -} - internal class OlmMachine( user_id: String, device_id: String, diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/VerificationRequest.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/VerificationRequest.kt new file mode 100644 index 0000000000..10b23ff0ad --- /dev/null +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/VerificationRequest.kt @@ -0,0 +1,172 @@ +/* + * Copyright (c) 2021 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.matrix.android.sdk.internal.crypto + +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext +import org.matrix.android.sdk.api.session.crypto.verification.PendingVerificationRequest +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.safeValueOf +import org.matrix.android.sdk.internal.crypto.model.rest.VERIFICATION_METHOD_QR_CODE_SCAN +import org.matrix.android.sdk.internal.crypto.model.rest.VERIFICATION_METHOD_QR_CODE_SHOW +import org.matrix.android.sdk.internal.crypto.model.rest.VERIFICATION_METHOD_RECIPROCATE +import org.matrix.android.sdk.internal.crypto.model.rest.VERIFICATION_METHOD_SAS +import uniffi.olm.OlmMachine +import uniffi.olm.OutgoingVerificationRequest +import uniffi.olm.StartSasResult +import uniffi.olm.VerificationRequest + +internal class VerificationRequest( + private val machine: OlmMachine, + private var inner: VerificationRequest +) { + private fun refreshData() { + val request = this.machine.getVerificationRequest(this.inner.otherUserId, this.inner.flowId) + + if (request != null) { + this.inner = request + } + + return + } + + fun acceptWithMethods(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() + + if (stringMethods.contains(VERIFICATION_METHOD_QR_CODE_SHOW) || + stringMethods.contains(VERIFICATION_METHOD_QR_CODE_SCAN)) { + stringMethods.add(VERIFICATION_METHOD_RECIPROCATE) + } + + return this.machine.acceptVerificationRequest( + this.inner.otherUserId, this.inner.flowId, stringMethods) + } + + fun isCanceled(): Boolean { + refreshData() + return this.inner.isCancelled + } + + fun isDone(): Boolean { + refreshData() + return this.inner.isDone + } + + fun isReady(): Boolean { + refreshData() + 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 + + val cancelCode = + if (code != null) { + safeValueOf(code) + } else { + null + } + + val ourMethods = this.inner.ourMethods + val theirMethods = this.inner.theirMethods + val otherDeviceId = this.inner.otherDeviceId + + var requestInfo: ValidVerificationInfoRequest? = null + var readyInfo: ValidVerificationInfoReady? = null + + if (this.inner.weStarted && ourMethods != null) { + requestInfo = + ValidVerificationInfoRequest( + this.inner.flowId, + this.machine.deviceId(), + ourMethods, + null, + ) + } else if (!this.inner.weStarted && ourMethods != null) { + readyInfo = + ValidVerificationInfoReady( + this.inner.flowId, + this.machine.deviceId(), + ourMethods, + ) + } + + if (this.inner.weStarted && theirMethods != null && otherDeviceId != null) { + readyInfo = + ValidVerificationInfoReady( + this.inner.flowId, + otherDeviceId, + theirMethods, + ) + } else if (!this.inner.weStarted && theirMethods != null && otherDeviceId != null) { + requestInfo = + ValidVerificationInfoRequest( + this.inner.flowId, + otherDeviceId, + theirMethods, + System.currentTimeMillis(), + ) + } + + return PendingVerificationRequest( + // Creation time + System.currentTimeMillis(), + // Who initiated the request + !this.inner.weStarted, + // Local echo id, what to do here? + this.inner.flowId, + // other user + this.inner.otherUserId, + // room id + this.inner.roomId, + // transaction id + this.inner.flowId, + // val requestInfo: ValidVerificationInfoRequest? = null, + requestInfo, + // val readyInfo: ValidVerificationInfoReady? = null, + readyInfo, + // cancel code if there is one + cancelCode, + // are we done/successful + this.inner.isDone, + // did another device answer the request + this.inner.isPassive, + // devices that should receive the events we send out + null, + ) + } +} 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 0630830ce0..b103607c78 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 @@ -178,13 +178,11 @@ constructor( // TODO This should be handled inside the rust-sdk decryption method } - // TODO All this methods should be delegated to a TransactionStore override fun getExistingTransaction( otherUserId: String, tid: String, ): VerificationTransaction? { val verification = this.olmMachine.getVerification(otherUserId, tid) ?: return null - return SasVerification(this.olmMachine.inner(), verification, this.requestSender, this.listeners) }