From 05119bcf90de217b6302e2c51f3ce424317687e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Mon, 28 Jun 2021 14:08:08 +0200 Subject: [PATCH] crypto: Allow devices to be marked manually as verified --- .../android/sdk/internal/crypto/OlmMachine.kt | 6 ++++ .../verification/RustVerificationService.kt | 13 +------- rust-sdk/src/lib.rs | 4 ++- rust-sdk/src/machine.rs | 32 ++++++++++++++++--- rust-sdk/src/olm.udl | 2 ++ 5 files changed, 39 insertions(+), 18 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 04a51c723c..ddd7cc4134 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 @@ -632,6 +632,12 @@ internal class OlmMachine( return plainDevices } + @Throws(CryptoStoreErrorException::class) + internal suspend fun markDeviceAsTrusted(userId: String, deviceId: String) = + withContext(Dispatchers.IO) { + inner.markDeviceAsTrusted(userId, deviceId) + } + /** Update all of our live device listeners. */ private suspend fun updateLiveDevices() { for ((liveDevice, users) in deviceUpdateObserver.listeners) { 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 b326639de7..26d9fd1ef6 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 @@ -102,18 +102,7 @@ constructor( } override fun markedLocallyAsManuallyVerified(userId: String, deviceID: String) { - TODO() - // setDeviceVerificationAction.handle(DeviceTrustLevel(false, true), - // userId, - // deviceID) - - // listeners.forEach { - // try { - // it.markedAsManuallyVerified(userId, deviceID) - // } catch (e: Throwable) { - // Timber.e(e, "## Error while notifying listeners") - // } - // } + runBlocking { olmMachine.markDeviceAsTrusted(userId, deviceID) } } fun onEvent(event: Event) = when (event.getClearType()) { diff --git a/rust-sdk/src/lib.rs b/rust-sdk/src/lib.rs index e21591d976..331c6336b2 100644 --- a/rust-sdk/src/lib.rs +++ b/rust-sdk/src/lib.rs @@ -7,7 +7,9 @@ mod responses; pub use device::Device; pub use error::{CryptoStoreError, DecryptionError, KeyImportError, MachineCreationError}; pub use logger::{set_logger, Logger}; -pub use machine::{KeyRequestPair, OlmMachine, Sas, StartSasResult, VerificationRequest, QrCode, Verification}; +pub use machine::{ + KeyRequestPair, OlmMachine, QrCode, Sas, StartSasResult, Verification, VerificationRequest, +}; pub use responses::{ DeviceLists, KeysImportResult, OutgoingVerificationRequest, Request, RequestType, }; diff --git a/rust-sdk/src/machine.rs b/rust-sdk/src/machine.rs index aa2b581e07..bbce67d9a7 100644 --- a/rust-sdk/src/machine.rs +++ b/rust-sdk/src/machine.rs @@ -4,8 +4,8 @@ use std::{ io::Cursor, }; -use js_int::UInt; use base64::encode; +use js_int::UInt; use ruma::{ api::{ client::r0::{ @@ -30,9 +30,9 @@ use tokio::runtime::Runtime; use matrix_sdk_common::{deserialized_responses::AlgorithmInfo, uuid::Uuid}; use matrix_sdk_crypto::{ - decrypt_key_export, encrypt_key_export, EncryptionSettings, OlmMachine as InnerMachine, - QrVerification as InnerQr, Sas as InnerSas, Verification as RustVerification, - VerificationRequest as InnerVerificationRequest, + decrypt_key_export, encrypt_key_export, EncryptionSettings, LocalTrust, + OlmMachine as InnerMachine, QrVerification as InnerQr, Sas as InnerSas, + Verification as RustVerification, VerificationRequest as InnerVerificationRequest, }; use crate::{ @@ -227,6 +227,25 @@ impl OlmMachine { .map(|d| d.into())) } + pub fn mark_device_as_trusted( + &self, + user_id: &str, + device_id: &str, + ) -> Result<(), CryptoStoreError> { + let user_id = UserId::try_from(user_id)?; + + let device = self + .runtime + .block_on(self.inner.get_device(&user_id, device_id.into()))?; + + if let Some(device) = device { + self.runtime + .block_on(device.set_local_trust(LocalTrust::Verified))?; + } + + Ok(()) + } + /// Get all devices of an user. /// /// # Arguments @@ -731,7 +750,10 @@ impl OlmMachine { let user_id = UserId::try_from(user_id).ok()?; self.inner .get_verification(&user_id, flow_id) - .and_then(|v| v.qr_v1().and_then(|qr| qr.to_bytes().map(|b| encode(b)).ok())) + .and_then(|v| { + v.qr_v1() + .and_then(|qr| qr.to_bytes().map(|b| encode(b)).ok()) + }) } pub fn start_sas_verification( diff --git a/rust-sdk/src/olm.udl b/rust-sdk/src/olm.udl index 9b488d6962..2acb35bb23 100644 --- a/rust-sdk/src/olm.udl +++ b/rust-sdk/src/olm.udl @@ -177,6 +177,8 @@ interface OlmMachine { [Throws=CryptoStoreError] Device? get_device([ByRef] string user_id, [ByRef] string device_id); [Throws=CryptoStoreError] + void mark_device_as_trusted([ByRef] string user_id, [ByRef] string device_id); + [Throws=CryptoStoreError] sequence get_user_devices([ByRef] string user_id); void update_tracked_users(sequence users);