From 38ce3ebed72d9337cd3b38749f9f342143a3ea57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Wed, 21 Jul 2021 14:58:41 +0200 Subject: [PATCH] crypto: Move the Device class into a separate file --- .../android/sdk/internal/crypto/Device.kt | 71 +++++++++++++++++++ .../android/sdk/internal/crypto/OlmMachine.kt | 31 -------- 2 files changed, 71 insertions(+), 31 deletions(-) create mode 100644 matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/Device.kt diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/Device.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/Device.kt new file mode 100644 index 0000000000..fa09f30f8e --- /dev/null +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/Device.kt @@ -0,0 +1,71 @@ +/* + * 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.VerificationService +import uniffi.olm.CryptoStoreErrorException +import uniffi.olm.Device as InnerDevice +import uniffi.olm.OlmMachine + +/** Class representing a device that supports E2EE in the Matrix world + * + * This class can be used to directly start a verification flow with the device + * or to manually verify the device. + */ +internal class Device( + private val machine: OlmMachine, + private var inner: InnerDevice, + private val sender: RequestSender, + private val listeners: ArrayList, +) { + /** Start an interactive verification with this device + * + * This sends out a m.key.verification.start event with the method set to + * m.sas.v1 to this device using to-device messaging. + */ + // TODO this has been deprecated in the spec, add a requestVerification() method + // to this class and use that one instead + @Throws(CryptoStoreErrorException::class) + suspend fun startVerification(): SasVerification? { + val result = withContext(Dispatchers.IO) { + machine.startSasWithDevice(inner.userId, inner.deviceId) + } + + return if (result != null) { + this.sender.sendVerificationRequest(result.request) + SasVerification( + this.machine, result.sas, this.sender, this.listeners, + ) + } else { + null + } + } + + /** Mark this device as locally trusted + * + * This won't upload any signatures, it will only mark the device as trusted + * in the local database. + */ + @Throws(CryptoStoreErrorException::class) + suspend fun markAsTrusted() { + withContext(Dispatchers.IO) { + machine.markDeviceAsTrusted(inner.userId, inner.deviceId) + } + } +} 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 3a9d12ce12..4ae46fd2d5 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,7 +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.VerificationService 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 @@ -119,36 +118,6 @@ internal class DeviceUpdateObserver { } } -internal class Device( - private val machine: uniffi.olm.OlmMachine, - private var inner: InnerDevice, - private val sender: RequestSender, - private val listeners: ArrayList, -) { - @Throws(CryptoStoreErrorException::class) - suspend fun startVerification(): SasVerification? { - val result = withContext(Dispatchers.IO) { - machine.startSasWithDevice(inner.userId, inner.deviceId) - } - - return if (result != null) { - this.sender.sendVerificationRequest(result.request) - SasVerification( - this.machine, result.sas, this.sender, this.listeners, - ) - } else { - null - } - } - - @Throws(CryptoStoreErrorException::class) - suspend fun markAsTrusted() { - withContext(Dispatchers.IO) { - machine.markDeviceAsTrusted(inner.userId, inner.deviceId) - } - } -} - internal class OlmMachine( user_id: String, device_id: String,