From 3fd2f5794e2cb7f7272de0597fa3df7b97c59055 Mon Sep 17 00:00:00 2001 From: Valere Date: Mon, 30 Sep 2024 09:13:07 +0200 Subject: [PATCH] crypto: Expose `with_decryption_trust_requirement` for ClientBuilder --- crates/matrix-sdk-base/src/client.rs | 12 ++++++++++-- crates/matrix-sdk/CHANGELOG.md | 1 + crates/matrix-sdk/src/client/builder/mod.rs | 17 ++++++++++++++++- crates/matrix-sdk/src/room/mod.rs | 7 ++++--- 4 files changed, 31 insertions(+), 6 deletions(-) diff --git a/crates/matrix-sdk-base/src/client.rs b/crates/matrix-sdk-base/src/client.rs index 8be5a1f1c..67e583248 100644 --- a/crates/matrix-sdk-base/src/client.rs +++ b/crates/matrix-sdk-base/src/client.rs @@ -117,6 +117,10 @@ pub struct BaseClient { /// encrypted message. #[cfg(feature = "e2e-encryption")] pub room_key_recipient_strategy: CollectStrategy, + + /// The trust requirement to use for decrypting events. + #[cfg(feature = "e2e-encryption")] + pub decryption_trust_requirement: TrustRequirement, } #[cfg(not(tarpaulin_include))] @@ -156,6 +160,8 @@ impl BaseClient { room_info_notable_update_sender, #[cfg(feature = "e2e-encryption")] room_key_recipient_strategy: Default::default(), + #[cfg(feature = "e2e-encryption")] + decryption_trust_requirement: TrustRequirement::Untrusted, } } @@ -180,6 +186,7 @@ impl BaseClient { ignore_user_list_changes: Default::default(), room_info_notable_update_sender: self.room_info_notable_update_sender.clone(), room_key_recipient_strategy: self.room_key_recipient_strategy.clone(), + decryption_trust_requirement: self.decryption_trust_requirement, }; if let Some(session_meta) = self.session_meta().cloned() { @@ -345,8 +352,9 @@ impl BaseClient { let olm = self.olm_machine().await; let Some(olm) = olm.as_ref() else { return Ok(None) }; - let decryption_settings = - DecryptionSettings { sender_device_trust_requirement: TrustRequirement::Untrusted }; + let decryption_settings = DecryptionSettings { + sender_device_trust_requirement: self.decryption_trust_requirement, + }; let event: SyncTimelineEvent = olm.decrypt_room_event(event.cast_ref(), room_id, &decryption_settings).await?.into(); diff --git a/crates/matrix-sdk/CHANGELOG.md b/crates/matrix-sdk/CHANGELOG.md index 3705158a8..95c6e781d 100644 --- a/crates/matrix-sdk/CHANGELOG.md +++ b/crates/matrix-sdk/CHANGELOG.md @@ -27,6 +27,7 @@ Breaking changes: Additions: +- new `ClientBuilder::with_decryption_trust_requirement` method. - new `ClientBuilder::with_room_key_recipient_strategy` method - new `Room.set_account_data` and `Room.set_account_data_raw` RoomAccountData setters, analogous to the GlobalAccountData - new `RequestConfig.max_concurrent_requests` which allows to limit the maximum number of concurrent requests the internal HTTP client issues (all others have to wait until the number drops below that threshold again) diff --git a/crates/matrix-sdk/src/client/builder/mod.rs b/crates/matrix-sdk/src/client/builder/mod.rs index 0dfe93811..01125de91 100644 --- a/crates/matrix-sdk/src/client/builder/mod.rs +++ b/crates/matrix-sdk/src/client/builder/mod.rs @@ -29,7 +29,7 @@ use tracing::{debug, field::debug, instrument, Span}; use super::{Client, ClientInner}; #[cfg(feature = "e2e-encryption")] -use crate::crypto::CollectStrategy; +use crate::crypto::{CollectStrategy, TrustRequirement}; #[cfg(feature = "e2e-encryption")] use crate::encryption::EncryptionSettings; #[cfg(not(target_arch = "wasm32"))] @@ -99,6 +99,8 @@ pub struct ClientBuilder { encryption_settings: EncryptionSettings, #[cfg(feature = "e2e-encryption")] room_key_recipient_strategy: CollectStrategy, + #[cfg(feature = "e2e-encryption")] + decryption_trust_requirement: TrustRequirement, } impl ClientBuilder { @@ -118,6 +120,8 @@ impl ClientBuilder { encryption_settings: Default::default(), #[cfg(feature = "e2e-encryption")] room_key_recipient_strategy: Default::default(), + #[cfg(feature = "e2e-encryption")] + decryption_trust_requirement: TrustRequirement::Untrusted, } } @@ -407,6 +411,16 @@ impl ClientBuilder { self } + /// Set the trust requirement to be used when decrypting events. + #[cfg(feature = "e2e-encryption")] + pub fn with_decryption_trust_requirement( + mut self, + trust_requirement: TrustRequirement, + ) -> Self { + self.decryption_trust_requirement = trust_requirement; + self + } + /// Create a [`Client`] with the options set on this builder. /// /// # Errors @@ -445,6 +459,7 @@ impl ClientBuilder { #[cfg(feature = "e2e-encryption")] { client.room_key_recipient_strategy = self.room_key_recipient_strategy; + client.decryption_trust_requirement = self.decryption_trust_requirement; } client }; diff --git a/crates/matrix-sdk/src/room/mod.rs b/crates/matrix-sdk/src/room/mod.rs index 1c7f6050d..20ad04d6a 100644 --- a/crates/matrix-sdk/src/room/mod.rs +++ b/crates/matrix-sdk/src/room/mod.rs @@ -15,7 +15,7 @@ use futures_util::{ stream::FuturesUnordered, }; #[cfg(feature = "e2e-encryption")] -use matrix_sdk_base::crypto::{DecryptionSettings, TrustRequirement}; +use matrix_sdk_base::crypto::DecryptionSettings; use matrix_sdk_base::{ deserialized_responses::{ RawAnySyncOrStrippedState, RawSyncOrStrippedState, SyncOrStrippedState, TimelineEvent, @@ -1183,8 +1183,9 @@ impl Room { let machine = self.client.olm_machine().await; let machine = machine.as_ref().ok_or(Error::NoOlmMachine)?; - let decryption_settings = - DecryptionSettings { sender_device_trust_requirement: TrustRequirement::Untrusted }; + let decryption_settings = DecryptionSettings { + sender_device_trust_requirement: self.client.base_client().decryption_trust_requirement, + }; let mut event = match machine .decrypt_room_event(event.cast_ref(), self.inner.room_id(), &decryption_settings) .await