matrix_sdk_crypto/machine/
mod.rs

1// Copyright 2020 The Matrix.org Foundation C.I.C.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::{
16    collections::{BTreeMap, HashMap, HashSet},
17    sync::Arc,
18    time::Duration,
19};
20
21use itertools::Itertools;
22#[cfg(feature = "experimental-send-custom-to-device")]
23use matrix_sdk_common::deserialized_responses::WithheldCode;
24use matrix_sdk_common::{
25    deserialized_responses::{
26        AlgorithmInfo, DecryptedRoomEvent, DeviceLinkProblem, EncryptionInfo, UnableToDecryptInfo,
27        UnableToDecryptReason, UnsignedDecryptionResult, UnsignedEventLocation, VerificationLevel,
28        VerificationState,
29    },
30    locks::RwLock as StdRwLock,
31    BoxFuture,
32};
33use ruma::{
34    api::client::{
35        dehydrated_device::DehydratedDeviceData,
36        keys::{
37            claim_keys::v3::Request as KeysClaimRequest,
38            get_keys::v3::Response as KeysQueryResponse,
39            upload_keys::v3::{Request as UploadKeysRequest, Response as UploadKeysResponse},
40            upload_signatures::v3::Request as UploadSignaturesRequest,
41        },
42        sync::sync_events::DeviceLists,
43    },
44    assign,
45    events::{
46        secret::request::SecretName, AnyMessageLikeEvent, AnyMessageLikeEventContent,
47        AnyToDeviceEvent, MessageLikeEventContent,
48    },
49    serde::{JsonObject, Raw},
50    DeviceId, MilliSecondsSinceUnixEpoch, OneTimeKeyAlgorithm, OwnedDeviceId, OwnedDeviceKeyId,
51    OwnedTransactionId, OwnedUserId, RoomId, TransactionId, UInt, UserId,
52};
53use serde_json::{value::to_raw_value, Value};
54use tokio::sync::Mutex;
55#[cfg(feature = "experimental-send-custom-to-device")]
56use tracing::trace;
57use tracing::{
58    debug, error,
59    field::{debug, display},
60    info, instrument, trace, warn, Span,
61};
62use vodozemac::{
63    megolm::{DecryptionError, SessionOrdering},
64    Curve25519PublicKey, Ed25519Signature,
65};
66
67use crate::{
68    backups::{BackupMachine, MegolmV1BackupKey},
69    dehydrated_devices::{DehydratedDevices, DehydrationError},
70    error::{EventError, MegolmError, MegolmResult, OlmError, OlmResult, SetRoomSettingsError},
71    gossiping::GossipMachine,
72    identities::{user::UserIdentity, Device, IdentityManager, UserDevices},
73    olm::{
74        Account, CrossSigningStatus, EncryptionSettings, IdentityKeys, InboundGroupSession,
75        KnownSenderData, OlmDecryptionInfo, PrivateCrossSigningIdentity, SenderData,
76        SenderDataFinder, SessionType, StaticAccountData,
77    },
78    session_manager::{GroupSessionManager, SessionManager},
79    store::{
80        Changes, CryptoStoreWrapper, DeviceChanges, IdentityChanges, IntoCryptoStore, MemoryStore,
81        PendingChanges, Result as StoreResult, RoomKeyInfo, RoomSettings, SecretImportError, Store,
82        StoreCache, StoreTransaction, StoredRoomKeyBundleData,
83    },
84    types::{
85        events::{
86            olm_v1::{AnyDecryptedOlmEvent, DecryptedRoomKeyBundleEvent, DecryptedRoomKeyEvent},
87            room::encrypted::{
88                EncryptedEvent, EncryptedToDeviceEvent, RoomEncryptedEventContent,
89                RoomEventEncryptionScheme, SupportedEventEncryptionSchemes,
90            },
91            room_key::{MegolmV1AesSha2Content, RoomKeyContent},
92            room_key_bundle::RoomKeyBundleContent,
93            room_key_withheld::{
94                MegolmV1AesSha2WithheldContent, RoomKeyWithheldContent, RoomKeyWithheldEvent,
95            },
96            ToDeviceEvents,
97        },
98        requests::{
99            AnyIncomingResponse, KeysQueryRequest, OutgoingRequest, ToDeviceRequest,
100            UploadSigningKeysRequest,
101        },
102        EventEncryptionAlgorithm, ProcessedToDeviceEvent, Signatures,
103    },
104    utilities::timestamp_to_iso8601,
105    verification::{Verification, VerificationMachine, VerificationRequest},
106    CollectStrategy, CrossSigningKeyExport, CryptoStoreError, DecryptionSettings, DeviceData,
107    LocalTrust, RoomEventDecryptionResult, SignatureError, TrustRequirement,
108};
109
110/// State machine implementation of the Olm/Megolm encryption protocol used for
111/// Matrix end to end encryption.
112#[derive(Clone)]
113pub struct OlmMachine {
114    pub(crate) inner: Arc<OlmMachineInner>,
115}
116
117pub struct OlmMachineInner {
118    /// The unique user id that owns this account.
119    user_id: OwnedUserId,
120    /// The unique device ID of the device that holds this account.
121    device_id: OwnedDeviceId,
122    /// The private part of our cross signing identity.
123    /// Used to sign devices and other users, might be missing if some other
124    /// device bootstrapped cross signing or cross signing isn't bootstrapped at
125    /// all.
126    user_identity: Arc<Mutex<PrivateCrossSigningIdentity>>,
127    /// Store for the encryption keys.
128    /// Persists all the encryption keys so a client can resume the session
129    /// without the need to create new keys.
130    store: Store,
131    /// A state machine that handles Olm sessions creation.
132    session_manager: SessionManager,
133    /// A state machine that keeps track of our outbound group sessions.
134    pub(crate) group_session_manager: GroupSessionManager,
135    /// A state machine that is responsible to handle and keep track of SAS
136    /// verification flows.
137    verification_machine: VerificationMachine,
138    /// The state machine that is responsible to handle outgoing and incoming
139    /// key requests.
140    pub(crate) key_request_machine: GossipMachine,
141    /// State machine handling public user identities and devices, keeping track
142    /// of when a key query needs to be done and handling one.
143    identity_manager: IdentityManager,
144    /// A state machine that handles creating room key backups.
145    backup_machine: BackupMachine,
146}
147
148#[cfg(not(tarpaulin_include))]
149impl std::fmt::Debug for OlmMachine {
150    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
151        f.debug_struct("OlmMachine")
152            .field("user_id", &self.user_id())
153            .field("device_id", &self.device_id())
154            .finish()
155    }
156}
157
158impl OlmMachine {
159    const CURRENT_GENERATION_STORE_KEY: &'static str = "generation-counter";
160    const HAS_MIGRATED_VERIFICATION_LATCH: &'static str = "HAS_MIGRATED_VERIFICATION_LATCH";
161
162    /// Create a new memory based OlmMachine.
163    ///
164    /// The created machine will keep the encryption keys only in memory and
165    /// once the object is dropped the keys will be lost.
166    ///
167    /// # Arguments
168    ///
169    /// * `user_id` - The unique id of the user that owns this machine.
170    ///
171    /// * `device_id` - The unique id of the device that owns this machine.
172    pub async fn new(user_id: &UserId, device_id: &DeviceId) -> Self {
173        OlmMachine::with_store(user_id, device_id, MemoryStore::new(), None)
174            .await
175            .expect("Reading and writing to the memory store always succeeds")
176    }
177
178    pub(crate) async fn rehydrate(
179        &self,
180        pickle_key: &[u8; 32],
181        device_id: &DeviceId,
182        device_data: Raw<DehydratedDeviceData>,
183    ) -> Result<OlmMachine, DehydrationError> {
184        let account = Account::rehydrate(pickle_key, self.user_id(), device_id, device_data)?;
185        let static_account = account.static_data().clone();
186
187        let store =
188            Arc::new(CryptoStoreWrapper::new(self.user_id(), device_id, MemoryStore::new()));
189        let device = DeviceData::from_account(&account);
190        store.save_pending_changes(PendingChanges { account: Some(account) }).await?;
191        store
192            .save_changes(Changes {
193                devices: DeviceChanges { new: vec![device], ..Default::default() },
194                ..Default::default()
195            })
196            .await?;
197
198        let (verification_machine, store, identity_manager) =
199            Self::new_helper_prelude(store, static_account, self.store().private_identity());
200
201        Ok(Self::new_helper(
202            device_id,
203            store,
204            verification_machine,
205            identity_manager,
206            self.store().private_identity(),
207            None,
208        ))
209    }
210
211    fn new_helper_prelude(
212        store_wrapper: Arc<CryptoStoreWrapper>,
213        account: StaticAccountData,
214        user_identity: Arc<Mutex<PrivateCrossSigningIdentity>>,
215    ) -> (VerificationMachine, Store, IdentityManager) {
216        let verification_machine =
217            VerificationMachine::new(account.clone(), user_identity.clone(), store_wrapper.clone());
218        let store = Store::new(account, user_identity, store_wrapper, verification_machine.clone());
219
220        let identity_manager = IdentityManager::new(store.clone());
221
222        (verification_machine, store, identity_manager)
223    }
224
225    fn new_helper(
226        device_id: &DeviceId,
227        store: Store,
228        verification_machine: VerificationMachine,
229        identity_manager: IdentityManager,
230        user_identity: Arc<Mutex<PrivateCrossSigningIdentity>>,
231        maybe_backup_key: Option<MegolmV1BackupKey>,
232    ) -> Self {
233        let group_session_manager = GroupSessionManager::new(store.clone());
234
235        let users_for_key_claim = Arc::new(StdRwLock::new(BTreeMap::new()));
236        let key_request_machine = GossipMachine::new(
237            store.clone(),
238            identity_manager.clone(),
239            group_session_manager.session_cache(),
240            users_for_key_claim.clone(),
241        );
242
243        let session_manager =
244            SessionManager::new(users_for_key_claim, key_request_machine.clone(), store.clone());
245
246        let backup_machine = BackupMachine::new(store.clone(), maybe_backup_key);
247
248        let inner = Arc::new(OlmMachineInner {
249            user_id: store.user_id().to_owned(),
250            device_id: device_id.to_owned(),
251            user_identity,
252            store,
253            session_manager,
254            group_session_manager,
255            verification_machine,
256            key_request_machine,
257            identity_manager,
258            backup_machine,
259        });
260
261        Self { inner }
262    }
263
264    /// Create a new OlmMachine with the given [`CryptoStore`].
265    ///
266    /// The created machine will keep the encryption keys only in memory and
267    /// once the object is dropped the keys will be lost.
268    ///
269    /// If the store already contains encryption keys for the given user/device
270    /// pair those will be re-used. Otherwise new ones will be created and
271    /// stored.
272    ///
273    /// # Arguments
274    ///
275    /// * `user_id` - The unique id of the user that owns this machine.
276    ///
277    /// * `device_id` - The unique id of the device that owns this machine.
278    ///
279    /// * `store` - A `CryptoStore` implementation that will be used to store
280    /// the encryption keys.
281    ///
282    /// * `custom_account` - A custom [`vodozemac::olm::Account`] to be used for
283    ///   the identity and one-time keys of this [`OlmMachine`]. If no account
284    ///   is provided, a new default one or one from the store will be used. If
285    ///   an account is provided and one already exists in the store for this
286    ///   [`UserId`]/[`DeviceId`] combination, an error will be raised. This is
287    ///   useful if one wishes to create identity keys before knowing the
288    ///   user/device IDs, e.g., to use the identity key as the device ID.
289    ///
290    /// [`CryptoStore`]: crate::store::CryptoStore
291    #[instrument(skip(store, custom_account), fields(ed25519_key, curve25519_key))]
292    pub async fn with_store(
293        user_id: &UserId,
294        device_id: &DeviceId,
295        store: impl IntoCryptoStore,
296        custom_account: Option<vodozemac::olm::Account>,
297    ) -> StoreResult<Self> {
298        let store = store.into_crypto_store();
299
300        let static_account = match store.load_account().await? {
301            Some(account) => {
302                if user_id != account.user_id()
303                    || device_id != account.device_id()
304                    || custom_account.is_some()
305                {
306                    return Err(CryptoStoreError::MismatchedAccount {
307                        expected: (account.user_id().to_owned(), account.device_id().to_owned()),
308                        got: (user_id.to_owned(), device_id.to_owned()),
309                    });
310                }
311
312                Span::current()
313                    .record("ed25519_key", display(account.identity_keys().ed25519))
314                    .record("curve25519_key", display(account.identity_keys().curve25519));
315                debug!("Restored an Olm account");
316
317                account.static_data().clone()
318            }
319
320            None => {
321                let account = if let Some(account) = custom_account {
322                    Account::new_helper(account, user_id, device_id)
323                } else {
324                    Account::with_device_id(user_id, device_id)
325                };
326
327                let static_account = account.static_data().clone();
328
329                Span::current()
330                    .record("ed25519_key", display(account.identity_keys().ed25519))
331                    .record("curve25519_key", display(account.identity_keys().curve25519));
332
333                let device = DeviceData::from_account(&account);
334
335                // We just created this device from our own Olm `Account`. Since we are the
336                // owners of the private keys of this device we can safely mark
337                // the device as verified.
338                device.set_trust_state(LocalTrust::Verified);
339
340                let changes = Changes {
341                    devices: DeviceChanges { new: vec![device], ..Default::default() },
342                    ..Default::default()
343                };
344                store.save_changes(changes).await?;
345                store.save_pending_changes(PendingChanges { account: Some(account) }).await?;
346
347                debug!("Created a new Olm account");
348
349                static_account
350            }
351        };
352
353        let identity = match store.load_identity().await? {
354            Some(i) => {
355                let master_key = i
356                    .master_public_key()
357                    .await
358                    .and_then(|m| m.get_first_key().map(|m| m.to_owned()));
359                debug!(?master_key, "Restored the cross signing identity");
360                i
361            }
362            None => {
363                debug!("Creating an empty cross signing identity stub");
364                PrivateCrossSigningIdentity::empty(user_id)
365            }
366        };
367
368        // FIXME: This is a workaround for `regenerate_olm` clearing the backup
369        // state. Ideally, backups should not get automatically enabled since
370        // the `OlmMachine` doesn't get enough info from the homeserver for this
371        // to work reliably.
372        let saved_keys = store.load_backup_keys().await?;
373        let maybe_backup_key = saved_keys.decryption_key.and_then(|k| {
374            if let Some(version) = saved_keys.backup_version {
375                let megolm_v1_backup_key = k.megolm_v1_public_key();
376                megolm_v1_backup_key.set_version(version);
377                Some(megolm_v1_backup_key)
378            } else {
379                None
380            }
381        });
382
383        let identity = Arc::new(Mutex::new(identity));
384        let store = Arc::new(CryptoStoreWrapper::new(user_id, device_id, store));
385
386        let (verification_machine, store, identity_manager) =
387            Self::new_helper_prelude(store, static_account, identity.clone());
388
389        // FIXME: We might want in the future a more generic high-level data migration
390        // mechanism (at the store wrapper layer).
391        Self::migration_post_verified_latch_support(&store, &identity_manager).await?;
392
393        Ok(Self::new_helper(
394            device_id,
395            store,
396            verification_machine,
397            identity_manager,
398            identity,
399            maybe_backup_key,
400        ))
401    }
402
403    // The sdk now support verified identity change detection.
404    // This introduces a new local flag (`verified_latch` on
405    // `OtherUserIdentityData`). In order to ensure that this flag is up-to-date and
406    // for the sake of simplicity we force a re-download of tracked users by marking
407    // them as dirty.
408    //
409    // pub(crate) visibility for testing.
410    pub(crate) async fn migration_post_verified_latch_support(
411        store: &Store,
412        identity_manager: &IdentityManager,
413    ) -> Result<(), CryptoStoreError> {
414        let maybe_migrate_for_identity_verified_latch =
415            store.get_custom_value(Self::HAS_MIGRATED_VERIFICATION_LATCH).await?.is_none();
416
417        if maybe_migrate_for_identity_verified_latch {
418            identity_manager.mark_all_tracked_users_as_dirty(store.cache().await?).await?;
419
420            store.set_custom_value(Self::HAS_MIGRATED_VERIFICATION_LATCH, vec![0]).await?
421        }
422        Ok(())
423    }
424
425    /// Get the crypto store associated with this `OlmMachine` instance.
426    pub fn store(&self) -> &Store {
427        &self.inner.store
428    }
429
430    /// The unique user id that owns this `OlmMachine` instance.
431    pub fn user_id(&self) -> &UserId {
432        &self.inner.user_id
433    }
434
435    /// The unique device ID that identifies this `OlmMachine`.
436    pub fn device_id(&self) -> &DeviceId {
437        &self.inner.device_id
438    }
439
440    /// The time at which the `Account` backing this `OlmMachine` was created.
441    ///
442    /// An [`Account`] is created when an `OlmMachine` is first instantiated
443    /// against a given [`Store`], at which point it creates identity keys etc.
444    /// This method returns the timestamp, according to the local clock, at
445    /// which that happened.
446    pub fn device_creation_time(&self) -> MilliSecondsSinceUnixEpoch {
447        self.inner.store.static_account().creation_local_time()
448    }
449
450    /// Get the public parts of our Olm identity keys.
451    pub fn identity_keys(&self) -> IdentityKeys {
452        let account = self.inner.store.static_account();
453        account.identity_keys()
454    }
455
456    /// Get the display name of our own device
457    pub async fn display_name(&self) -> StoreResult<Option<String>> {
458        self.store().device_display_name().await
459    }
460
461    /// Get the list of "tracked users".
462    ///
463    /// See [`update_tracked_users`](#method.update_tracked_users) for more
464    /// information.
465    pub async fn tracked_users(&self) -> StoreResult<HashSet<OwnedUserId>> {
466        let cache = self.store().cache().await?;
467        Ok(self.inner.identity_manager.key_query_manager.synced(&cache).await?.tracked_users())
468    }
469
470    /// Enable or disable room key requests.
471    ///
472    /// Room key requests allow the device to request room keys that it might
473    /// have missed in the original share using `m.room_key_request`
474    /// events.
475    ///
476    /// See also [`OlmMachine::set_room_key_forwarding_enabled`] and
477    /// [`OlmMachine::are_room_key_requests_enabled`].
478    #[cfg(feature = "automatic-room-key-forwarding")]
479    pub fn set_room_key_requests_enabled(&self, enable: bool) {
480        self.inner.key_request_machine.set_room_key_requests_enabled(enable)
481    }
482
483    /// Query whether we should send outgoing `m.room_key_request`s on
484    /// decryption failure.
485    ///
486    /// See also [`OlmMachine::set_room_key_requests_enabled`].
487    pub fn are_room_key_requests_enabled(&self) -> bool {
488        self.inner.key_request_machine.are_room_key_requests_enabled()
489    }
490
491    /// Enable or disable room key forwarding.
492    ///
493    /// If room key forwarding is enabled, we will automatically reply to
494    /// incoming `m.room_key_request` messages from verified devices by
495    /// forwarding the requested key (if we have it).
496    ///
497    /// See also [`OlmMachine::set_room_key_requests_enabled`] and
498    /// [`OlmMachine::is_room_key_forwarding_enabled`].
499    #[cfg(feature = "automatic-room-key-forwarding")]
500    pub fn set_room_key_forwarding_enabled(&self, enable: bool) {
501        self.inner.key_request_machine.set_room_key_forwarding_enabled(enable)
502    }
503
504    /// Is room key forwarding enabled?
505    ///
506    /// See also [`OlmMachine::set_room_key_forwarding_enabled`].
507    pub fn is_room_key_forwarding_enabled(&self) -> bool {
508        self.inner.key_request_machine.is_room_key_forwarding_enabled()
509    }
510
511    /// Get the outgoing requests that need to be sent out.
512    ///
513    /// This returns a list of [`OutgoingRequest`]. Those requests need to be
514    /// sent out to the server and the responses need to be passed back to
515    /// the state machine using [`mark_request_as_sent`].
516    ///
517    /// [`mark_request_as_sent`]: #method.mark_request_as_sent
518    pub async fn outgoing_requests(&self) -> StoreResult<Vec<OutgoingRequest>> {
519        let mut requests = Vec::new();
520
521        {
522            let store_cache = self.inner.store.cache().await?;
523            let account = store_cache.account().await?;
524            if let Some(r) = self.keys_for_upload(&account).await.map(|r| OutgoingRequest {
525                request_id: TransactionId::new(),
526                request: Arc::new(r.into()),
527            }) {
528                requests.push(r);
529            }
530        }
531
532        for request in self
533            .inner
534            .identity_manager
535            .users_for_key_query()
536            .await?
537            .into_iter()
538            .map(|(request_id, r)| OutgoingRequest { request_id, request: Arc::new(r.into()) })
539        {
540            requests.push(request);
541        }
542
543        requests.append(&mut self.inner.verification_machine.outgoing_messages());
544        requests.append(&mut self.inner.key_request_machine.outgoing_to_device_requests().await?);
545
546        Ok(requests)
547    }
548
549    /// Generate an "out-of-band" key query request for the given set of users.
550    ///
551    /// This can be useful if we need the results from [`get_identity`] or
552    /// [`get_user_devices`] to be as up-to-date as possible.
553    ///
554    /// Note that this request won't be awaited by other calls waiting for a
555    /// user's or device's keys, since this is an out-of-band query.
556    ///
557    /// # Arguments
558    ///
559    /// * `users` - list of users whose keys should be queried
560    ///
561    /// # Returns
562    ///
563    /// A request to be sent out to the server. Once sent, the response should
564    /// be passed back to the state machine using [`mark_request_as_sent`].
565    ///
566    /// [`mark_request_as_sent`]: OlmMachine::mark_request_as_sent
567    /// [`get_identity`]: OlmMachine::get_identity
568    /// [`get_user_devices`]: OlmMachine::get_user_devices
569    pub fn query_keys_for_users<'a>(
570        &self,
571        users: impl IntoIterator<Item = &'a UserId>,
572    ) -> (OwnedTransactionId, KeysQueryRequest) {
573        self.inner.identity_manager.build_key_query_for_users(users)
574    }
575
576    /// Mark the request with the given request id as sent.
577    ///
578    /// # Arguments
579    ///
580    /// * `request_id` - The unique id of the request that was sent out. This is
581    ///   needed to couple the response with the now sent out request.
582    ///
583    /// * `response` - The response that was received from the server after the
584    ///   outgoing request was sent out.
585    pub async fn mark_request_as_sent<'a>(
586        &self,
587        request_id: &TransactionId,
588        response: impl Into<AnyIncomingResponse<'a>>,
589    ) -> OlmResult<()> {
590        match response.into() {
591            AnyIncomingResponse::KeysUpload(response) => {
592                Box::pin(self.receive_keys_upload_response(response)).await?;
593            }
594            AnyIncomingResponse::KeysQuery(response) => {
595                Box::pin(self.receive_keys_query_response(request_id, response)).await?;
596            }
597            AnyIncomingResponse::KeysClaim(response) => {
598                Box::pin(
599                    self.inner.session_manager.receive_keys_claim_response(request_id, response),
600                )
601                .await?;
602            }
603            AnyIncomingResponse::ToDevice(_) => {
604                Box::pin(self.mark_to_device_request_as_sent(request_id)).await?;
605            }
606            AnyIncomingResponse::SigningKeysUpload(_) => {
607                Box::pin(self.receive_cross_signing_upload_response()).await?;
608            }
609            AnyIncomingResponse::SignatureUpload(_) => {
610                self.inner.verification_machine.mark_request_as_sent(request_id);
611            }
612            AnyIncomingResponse::RoomMessage(_) => {
613                self.inner.verification_machine.mark_request_as_sent(request_id);
614            }
615            AnyIncomingResponse::KeysBackup(_) => {
616                Box::pin(self.inner.backup_machine.mark_request_as_sent(request_id)).await?;
617            }
618        };
619
620        Ok(())
621    }
622
623    /// Mark the cross signing identity as shared.
624    async fn receive_cross_signing_upload_response(&self) -> StoreResult<()> {
625        let identity = self.inner.user_identity.lock().await;
626        identity.mark_as_shared();
627
628        let changes = Changes { private_identity: Some(identity.clone()), ..Default::default() };
629
630        self.store().save_changes(changes).await
631    }
632
633    /// Create a new cross signing identity and get the upload request to push
634    /// the new public keys to the server.
635    ///
636    /// **Warning**: if called with `reset`, this will delete any existing cross
637    /// signing keys that might exist on the server and thus will reset the
638    /// trust between all the devices.
639    ///
640    /// # Returns
641    ///
642    /// A triple of requests which should be sent out to the server, in the
643    /// order they appear in the return tuple.
644    ///
645    /// The first request's response, if present, should be passed back to the
646    /// state machine using [`mark_request_as_sent`].
647    ///
648    /// These requests may require user interactive auth.
649    ///
650    /// [`mark_request_as_sent`]: #method.mark_request_as_sent
651    pub async fn bootstrap_cross_signing(
652        &self,
653        reset: bool,
654    ) -> StoreResult<CrossSigningBootstrapRequests> {
655        // Don't hold the lock, otherwise we might deadlock in
656        // `bootstrap_cross_signing()` on `account` if a sync task is already
657        // running (which locks `account`), or we will deadlock
658        // in `upload_device_keys()` which locks private identity again.
659        let identity = self.inner.user_identity.lock().await.clone();
660
661        let (upload_signing_keys_req, upload_signatures_req) = if reset || identity.is_empty().await
662        {
663            info!("Creating new cross signing identity");
664
665            let (identity, upload_signing_keys_req, upload_signatures_req) = {
666                let cache = self.inner.store.cache().await?;
667                let account = cache.account().await?;
668                account.bootstrap_cross_signing().await
669            };
670
671            let public = identity.to_public_identity().await.expect(
672                "Couldn't create a public version of the identity from a new private identity",
673            );
674
675            *self.inner.user_identity.lock().await = identity.clone();
676
677            self.store()
678                .save_changes(Changes {
679                    identities: IdentityChanges { new: vec![public.into()], ..Default::default() },
680                    private_identity: Some(identity),
681                    ..Default::default()
682                })
683                .await?;
684
685            (upload_signing_keys_req, upload_signatures_req)
686        } else {
687            info!("Trying to upload the existing cross signing identity");
688            let upload_signing_keys_req = identity.as_upload_request().await;
689
690            // TODO remove this expect.
691            let upload_signatures_req = identity
692                .sign_account(self.inner.store.static_account())
693                .await
694                .expect("Can't sign device keys");
695
696            (upload_signing_keys_req, upload_signatures_req)
697        };
698
699        // If there are any *device* keys to upload (i.e. the account isn't shared),
700        // upload them before we upload the signatures, since the signatures may
701        // reference keys to be uploaded.
702        let upload_keys_req =
703            self.upload_device_keys().await?.map(|(_, request)| OutgoingRequest::from(request));
704
705        Ok(CrossSigningBootstrapRequests {
706            upload_signing_keys_req,
707            upload_keys_req,
708            upload_signatures_req,
709        })
710    }
711
712    /// Upload the device keys for this [`OlmMachine`].
713    ///
714    /// **Warning**: Do not use this method if
715    /// [`OlmMachine::outgoing_requests()`] is already in use. This method
716    /// is intended for explicitly uploading the device keys before starting
717    /// a sync and before using [`OlmMachine::outgoing_requests()`].
718    ///
719    /// # Returns
720    ///
721    /// A tuple containing a transaction ID and a request if the device keys
722    /// need to be uploaded. Otherwise, returns `None`.
723    pub async fn upload_device_keys(
724        &self,
725    ) -> StoreResult<Option<(OwnedTransactionId, UploadKeysRequest)>> {
726        let cache = self.store().cache().await?;
727        let account = cache.account().await?;
728
729        Ok(self.keys_for_upload(&account).await.map(|request| (TransactionId::new(), request)))
730    }
731
732    /// Receive a successful `/keys/upload` response.
733    ///
734    /// # Arguments
735    ///
736    /// * `response` - The response of the `/keys/upload` request that the
737    ///   client performed.
738    async fn receive_keys_upload_response(&self, response: &UploadKeysResponse) -> OlmResult<()> {
739        self.inner
740            .store
741            .with_transaction(|mut tr| async {
742                let account = tr.account().await?;
743                account.receive_keys_upload_response(response)?;
744                Ok((tr, ()))
745            })
746            .await
747    }
748
749    /// Get a key claiming request for the user/device pairs that we are
750    /// missing Olm sessions for.
751    ///
752    /// Returns None if no key claiming request needs to be sent out.
753    ///
754    /// Sessions need to be established between devices so group sessions for a
755    /// room can be shared with them.
756    ///
757    /// This should be called every time a group session needs to be shared as
758    /// well as between sync calls. After a sync some devices may request room
759    /// keys without us having a valid Olm session with them, making it
760    /// impossible to server the room key request, thus it's necessary to check
761    /// for missing sessions between sync as well.
762    ///
763    /// **Note**: Care should be taken that only one such request at a time is
764    /// in flight, e.g. using a lock.
765    ///
766    /// The response of a successful key claiming requests needs to be passed to
767    /// the `OlmMachine` with the [`mark_request_as_sent`].
768    ///
769    /// # Arguments
770    ///
771    /// `users` - The list of users that we should check if we lack a session
772    /// with one of their devices. This can be an empty iterator when calling
773    /// this method between sync requests.
774    ///
775    /// [`mark_request_as_sent`]: #method.mark_request_as_sent
776    #[instrument(skip_all)]
777    pub async fn get_missing_sessions(
778        &self,
779        users: impl Iterator<Item = &UserId>,
780    ) -> StoreResult<Option<(OwnedTransactionId, KeysClaimRequest)>> {
781        self.inner.session_manager.get_missing_sessions(users).await
782    }
783
784    /// Receive a successful `/keys/query` response.
785    ///
786    /// Returns a list of newly discovered devices and devices that changed.
787    ///
788    /// # Arguments
789    ///
790    /// * `response` - The response of the `/keys/query` request that the client
791    ///   performed.
792    async fn receive_keys_query_response(
793        &self,
794        request_id: &TransactionId,
795        response: &KeysQueryResponse,
796    ) -> OlmResult<(DeviceChanges, IdentityChanges)> {
797        self.inner.identity_manager.receive_keys_query_response(request_id, response).await
798    }
799
800    /// Get a request to upload E2EE keys to the server.
801    ///
802    /// Returns None if no keys need to be uploaded.
803    ///
804    /// The response of a successful key upload requests needs to be passed to
805    /// the [`OlmMachine`] with the [`receive_keys_upload_response`].
806    ///
807    /// [`receive_keys_upload_response`]: #method.receive_keys_upload_response
808    async fn keys_for_upload(&self, account: &Account) -> Option<UploadKeysRequest> {
809        let (mut device_keys, one_time_keys, fallback_keys) = account.keys_for_upload();
810
811        // When uploading the device keys, if all private cross-signing keys are
812        // available locally, sign the device using these cross-signing keys.
813        // This will mark the device as verified if the user identity (i.e., the
814        // cross-signing keys) is also marked as verified.
815        //
816        // This approach eliminates the need to upload signatures in a separate request,
817        // ensuring that other users/devices will never encounter this device
818        // without a signature from their user identity. Consequently, they will
819        // never see the device as unverified.
820        if let Some(device_keys) = &mut device_keys {
821            let private_identity = self.store().private_identity();
822            let guard = private_identity.lock().await;
823
824            if guard.status().await.is_complete() {
825                guard.sign_device_keys(device_keys).await.expect(
826                    "We should be able to sign our device keys since we confirmed that we \
827                     have a complete set of private cross-signing keys",
828                );
829            }
830        }
831
832        if device_keys.is_none() && one_time_keys.is_empty() && fallback_keys.is_empty() {
833            None
834        } else {
835            let device_keys = device_keys.map(|d| d.to_raw());
836
837            Some(assign!(UploadKeysRequest::new(), {
838                device_keys, one_time_keys, fallback_keys
839            }))
840        }
841    }
842
843    /// Decrypt a to-device event.
844    ///
845    /// Returns a decrypted `ToDeviceEvent` if the decryption was successful,
846    /// an error indicating why decryption failed otherwise.
847    ///
848    /// # Arguments
849    ///
850    /// * `event` - The to-device event that should be decrypted.
851    async fn decrypt_to_device_event(
852        &self,
853        transaction: &mut StoreTransaction,
854        event: &EncryptedToDeviceEvent,
855        changes: &mut Changes,
856    ) -> OlmResult<OlmDecryptionInfo> {
857        let mut decrypted =
858            transaction.account().await?.decrypt_to_device_event(&self.inner.store, event).await?;
859
860        // We ignore all to-device events from dehydrated devices - we should not
861        // receive any
862        if !self.to_device_event_is_from_dehydrated_device(&decrypted, &event.sender).await? {
863            // Handle the decrypted event, e.g. fetch out Megolm sessions out of
864            // the event.
865            self.handle_decrypted_to_device_event(transaction.cache(), &mut decrypted, changes)
866                .await?;
867        }
868
869        Ok(decrypted)
870    }
871
872    #[instrument(
873        skip_all,
874        // This function is only ever called by add_room_key via
875        // handle_decrypted_to_device_event, so sender, sender_key, and algorithm are
876        // already recorded.
877        fields(room_id = ? content.room_id, session_id)
878    )]
879    async fn handle_key(
880        &self,
881        sender_key: Curve25519PublicKey,
882        event: &DecryptedRoomKeyEvent,
883        content: &MegolmV1AesSha2Content,
884    ) -> OlmResult<Option<InboundGroupSession>> {
885        let session =
886            InboundGroupSession::from_room_key_content(sender_key, event.keys.ed25519, content);
887
888        match session {
889            Ok(mut session) => {
890                Span::current().record("session_id", session.session_id());
891
892                let sender_data =
893                    SenderDataFinder::find_using_event(self.store(), sender_key, event, &session)
894                        .await?;
895
896                session.sender_data = sender_data;
897
898                match self.store().compare_group_session(&session).await? {
899                    SessionOrdering::Better => {
900                        info!("Received a new megolm room key");
901
902                        Ok(Some(session))
903                    }
904                    comparison_result => {
905                        warn!(
906                            ?comparison_result,
907                            "Received a megolm room key that we already have a better version \
908                             of, discarding"
909                        );
910
911                        Ok(None)
912                    }
913                }
914            }
915            Err(e) => {
916                Span::current().record("session_id", &content.session_id);
917                warn!("Received a room key event which contained an invalid session key: {e}");
918
919                Ok(None)
920            }
921        }
922    }
923
924    /// Create a group session from a room key and add it to our crypto store.
925    #[instrument(skip_all, fields(algorithm = ?event.content.algorithm()))]
926    async fn add_room_key(
927        &self,
928        sender_key: Curve25519PublicKey,
929        event: &DecryptedRoomKeyEvent,
930    ) -> OlmResult<Option<InboundGroupSession>> {
931        match &event.content {
932            RoomKeyContent::MegolmV1AesSha2(content) => {
933                self.handle_key(sender_key, event, content).await
934            }
935            #[cfg(feature = "experimental-algorithms")]
936            RoomKeyContent::MegolmV2AesSha2(content) => {
937                self.handle_key(sender_key, event, content).await
938            }
939            RoomKeyContent::Unknown(_) => {
940                warn!("Received a room key with an unsupported algorithm");
941                Ok(None)
942            }
943        }
944    }
945
946    /// Handle a received, decrypted, `io.element.msc4268.room_key_bundle`
947    /// to-device event.
948    #[instrument()]
949    async fn receive_room_key_bundle_data(
950        &self,
951        event: &DecryptedRoomKeyBundleEvent,
952        changes: &mut Changes,
953    ) -> OlmResult<()> {
954        let Some(sender_device_keys) = &event.sender_device_keys else {
955            warn!("Received a room key bundle with no sender device keys: ignoring");
956            return Ok(());
957        };
958
959        // We already checked that `sender_device_keys` matches the actual sender of the
960        // message when we decrypted the message, which included doing
961        // `DeviceData::try_from` on it, so it can't fail.
962
963        let sender_device_data =
964            DeviceData::try_from(sender_device_keys).expect("failed to verify sender device keys");
965        let sender_device = self.store().wrap_device_data(sender_device_data).await?;
966
967        changes.received_room_key_bundles.push(StoredRoomKeyBundleData {
968            sender_user: event.sender.clone(),
969            sender_data: SenderData::from_device(&sender_device),
970            bundle_data: event.content.clone(),
971        });
972        Ok(())
973    }
974
975    fn add_withheld_info(&self, changes: &mut Changes, event: &RoomKeyWithheldEvent) {
976        debug!(?event.content, "Processing `m.room_key.withheld` event");
977
978        if let RoomKeyWithheldContent::MegolmV1AesSha2(
979            MegolmV1AesSha2WithheldContent::BlackListed(c)
980            | MegolmV1AesSha2WithheldContent::Unverified(c),
981        ) = &event.content
982        {
983            changes
984                .withheld_session_info
985                .entry(c.room_id.to_owned())
986                .or_default()
987                .insert(c.session_id.to_owned(), event.to_owned());
988        }
989    }
990
991    #[cfg(test)]
992    pub(crate) async fn create_outbound_group_session_with_defaults_test_helper(
993        &self,
994        room_id: &RoomId,
995    ) -> OlmResult<()> {
996        let (_, session) = self
997            .inner
998            .group_session_manager
999            .create_outbound_group_session(
1000                room_id,
1001                EncryptionSettings::default(),
1002                SenderData::unknown(),
1003            )
1004            .await?;
1005
1006        self.store().save_inbound_group_sessions(&[session]).await?;
1007
1008        Ok(())
1009    }
1010
1011    #[cfg(test)]
1012    #[allow(dead_code)]
1013    pub(crate) async fn create_inbound_session_test_helper(
1014        &self,
1015        room_id: &RoomId,
1016    ) -> OlmResult<InboundGroupSession> {
1017        let (_, session) = self
1018            .inner
1019            .group_session_manager
1020            .create_outbound_group_session(
1021                room_id,
1022                EncryptionSettings::default(),
1023                SenderData::unknown(),
1024            )
1025            .await?;
1026
1027        Ok(session)
1028    }
1029
1030    /// Encrypt a room message for the given room.
1031    ///
1032    /// Beware that a room key needs to be shared before this method
1033    /// can be called using the [`OlmMachine::share_room_key`] method.
1034    ///
1035    /// # Arguments
1036    ///
1037    /// * `room_id` - The id of the room for which the message should be
1038    ///   encrypted.
1039    ///
1040    /// * `content` - The plaintext content of the message that should be
1041    ///   encrypted.
1042    ///
1043    /// # Panics
1044    ///
1045    /// Panics if a room key for the given room wasn't shared beforehand.
1046    pub async fn encrypt_room_event(
1047        &self,
1048        room_id: &RoomId,
1049        content: impl MessageLikeEventContent,
1050    ) -> MegolmResult<Raw<RoomEncryptedEventContent>> {
1051        let event_type = content.event_type().to_string();
1052        let content = Raw::new(&content)?.cast();
1053        self.encrypt_room_event_raw(room_id, &event_type, &content).await
1054    }
1055
1056    /// Encrypt a raw JSON content for the given room.
1057    ///
1058    /// This method is equivalent to the [`OlmMachine::encrypt_room_event()`]
1059    /// method but operates on an arbitrary JSON value instead of strongly-typed
1060    /// event content struct.
1061    ///
1062    /// # Arguments
1063    ///
1064    /// * `room_id` - The id of the room for which the message should be
1065    ///   encrypted.
1066    ///
1067    /// * `content` - The plaintext content of the message that should be
1068    ///   encrypted as a raw JSON value.
1069    ///
1070    /// * `event_type` - The plaintext type of the event.
1071    ///
1072    /// # Panics
1073    ///
1074    /// Panics if a group session for the given room wasn't shared beforehand.
1075    pub async fn encrypt_room_event_raw(
1076        &self,
1077        room_id: &RoomId,
1078        event_type: &str,
1079        content: &Raw<AnyMessageLikeEventContent>,
1080    ) -> MegolmResult<Raw<RoomEncryptedEventContent>> {
1081        self.inner.group_session_manager.encrypt(room_id, event_type, content).await
1082    }
1083
1084    /// Forces the currently active room key, which is used to encrypt messages,
1085    /// to be rotated.
1086    ///
1087    /// A new room key will be crated and shared with all the room members the
1088    /// next time a message will be sent. You don't have to call this method,
1089    /// room keys will be rotated automatically when necessary. This method is
1090    /// still useful for debugging purposes.
1091    ///
1092    /// Returns true if a session was invalidated, false if there was no session
1093    /// to invalidate.
1094    pub async fn discard_room_key(&self, room_id: &RoomId) -> StoreResult<bool> {
1095        self.inner.group_session_manager.invalidate_group_session(room_id).await
1096    }
1097
1098    /// Get to-device requests to share a room key with users in a room.
1099    ///
1100    /// # Arguments
1101    ///
1102    /// `room_id` - The room id of the room where the room key will be
1103    /// used.
1104    ///
1105    /// `users` - The list of users that should receive the room key.
1106    ///
1107    /// `settings` - Encryption settings that affect when are room keys rotated
1108    /// and who are they shared with.
1109    ///
1110    /// # Returns
1111    ///
1112    /// List of the to-device requests that need to be sent out to the server
1113    /// and the responses need to be passed back to the state machine with
1114    /// [`mark_request_as_sent`], using the to-device `txn_id` as `request_id`.
1115    ///
1116    /// [`mark_request_as_sent`]: #method.mark_request_as_sent
1117    pub async fn share_room_key(
1118        &self,
1119        room_id: &RoomId,
1120        users: impl Iterator<Item = &UserId>,
1121        encryption_settings: impl Into<EncryptionSettings>,
1122    ) -> OlmResult<Vec<Arc<ToDeviceRequest>>> {
1123        self.inner.group_session_manager.share_room_key(room_id, users, encryption_settings).await
1124    }
1125
1126    /// Encrypts the given content using Olm for each of the given devices.
1127    ///
1128    /// The 1-to-1 session must be established prior to this
1129    /// call by using the [`OlmMachine::get_missing_sessions`] method or the
1130    /// encryption will fail.
1131    ///
1132    /// The caller is responsible for sending the encrypted
1133    /// event to the target device, and should do it ASAP to avoid out-of-order
1134    /// messages.
1135    ///
1136    /// # Returns
1137    /// A list of `ToDeviceRequest` to send out the event, and the list of
1138    /// devices where encryption did not succeed (device excluded or no olm)
1139    #[cfg(feature = "experimental-send-custom-to-device")]
1140    pub async fn encrypt_content_for_devices(
1141        &self,
1142        devices: Vec<DeviceData>,
1143        event_type: &str,
1144        content: &Value,
1145    ) -> OlmResult<(Vec<ToDeviceRequest>, Vec<(DeviceData, WithheldCode)>)> {
1146        // TODO: Use a `CollectStrategy` arguments to filter our devices depending on
1147        // safety settings (like not sending to insecure devices).
1148        let mut changes = Changes::default();
1149
1150        let result = self
1151            .inner
1152            .group_session_manager
1153            .encrypt_content_for_devices(devices, event_type, content.clone(), &mut changes)
1154            .await;
1155
1156        // Persist any changes we might have collected.
1157        if !changes.is_empty() {
1158            let session_count = changes.sessions.len();
1159
1160            self.inner.store.save_changes(changes).await?;
1161
1162            trace!(
1163                session_count = session_count,
1164                "Stored the changed sessions after encrypting a custom to-device event"
1165            );
1166        }
1167
1168        result
1169    }
1170    /// Collect the devices belonging to the given user, and send the details of
1171    /// a room key bundle to those devices.
1172    ///
1173    /// Returns a list of to-device requests which must be sent.
1174    pub async fn share_room_key_bundle_data(
1175        &self,
1176        user_id: &UserId,
1177        collect_strategy: &CollectStrategy,
1178        bundle_data: RoomKeyBundleContent,
1179    ) -> OlmResult<Vec<ToDeviceRequest>> {
1180        self.inner
1181            .group_session_manager
1182            .share_room_key_bundle_data(user_id, collect_strategy, bundle_data)
1183            .await
1184    }
1185
1186    /// Receive an unencrypted verification event.
1187    ///
1188    /// This method can be used to pass verification events that are happening
1189    /// in unencrypted rooms to the `OlmMachine`.
1190    ///
1191    /// **Note**: This does not need to be called for encrypted events since
1192    /// those will get passed to the `OlmMachine` during decryption.
1193    #[deprecated(note = "Use OlmMachine::receive_verification_event instead", since = "0.7.0")]
1194    pub async fn receive_unencrypted_verification_event(
1195        &self,
1196        event: &AnyMessageLikeEvent,
1197    ) -> StoreResult<()> {
1198        self.inner.verification_machine.receive_any_event(event).await
1199    }
1200
1201    /// Receive a verification event.
1202    ///
1203    /// The event should be in the decrypted form.
1204    pub async fn receive_verification_event(&self, event: &AnyMessageLikeEvent) -> StoreResult<()> {
1205        self.inner.verification_machine.receive_any_event(event).await
1206    }
1207
1208    /// Receive and properly handle a decrypted to-device event.
1209    ///
1210    /// # Arguments
1211    ///
1212    /// * `decrypted` - The decrypted event and some associated metadata.
1213    #[instrument(
1214        skip_all,
1215        fields(
1216            sender_key = ?decrypted.result.sender_key,
1217            event_type = decrypted.result.event.event_type(),
1218        ),
1219    )]
1220    async fn handle_decrypted_to_device_event(
1221        &self,
1222        cache: &StoreCache,
1223        decrypted: &mut OlmDecryptionInfo,
1224        changes: &mut Changes,
1225    ) -> OlmResult<()> {
1226        debug!(
1227            sender_device_keys =
1228                ?decrypted.result.event.sender_device_keys().map(|k| (k.curve25519_key(), k.ed25519_key())).unwrap_or((None, None)),
1229            "Received a decrypted to-device event",
1230        );
1231
1232        match &*decrypted.result.event {
1233            AnyDecryptedOlmEvent::RoomKey(e) => {
1234                let session = self.add_room_key(decrypted.result.sender_key, e).await?;
1235                decrypted.inbound_group_session = session;
1236            }
1237            AnyDecryptedOlmEvent::ForwardedRoomKey(e) => {
1238                let session = self
1239                    .inner
1240                    .key_request_machine
1241                    .receive_forwarded_room_key(decrypted.result.sender_key, e)
1242                    .await?;
1243                decrypted.inbound_group_session = session;
1244            }
1245            AnyDecryptedOlmEvent::SecretSend(e) => {
1246                let name = self
1247                    .inner
1248                    .key_request_machine
1249                    .receive_secret_event(cache, decrypted.result.sender_key, e, changes)
1250                    .await?;
1251
1252                // Set the secret name so other consumers of the event know
1253                // what this event is about.
1254                if let Ok(ToDeviceEvents::SecretSend(mut e)) =
1255                    decrypted.result.raw_event.deserialize_as()
1256                {
1257                    e.content.secret_name = name;
1258                    decrypted.result.raw_event = Raw::from_json(to_raw_value(&e)?);
1259                }
1260            }
1261            AnyDecryptedOlmEvent::Dummy(_) => {
1262                debug!("Received an `m.dummy` event");
1263            }
1264            AnyDecryptedOlmEvent::RoomKeyBundle(e) => {
1265                debug!("Received a room key bundle event {:?}", e);
1266                self.receive_room_key_bundle_data(e, changes).await?;
1267            }
1268            AnyDecryptedOlmEvent::Custom(_) => {
1269                warn!("Received an unexpected encrypted to-device event");
1270            }
1271        }
1272
1273        Ok(())
1274    }
1275
1276    async fn handle_verification_event(&self, event: &ToDeviceEvents) {
1277        if let Err(e) = self.inner.verification_machine.receive_any_event(event).await {
1278            error!("Error handling a verification event: {e:?}");
1279        }
1280    }
1281
1282    /// Mark an outgoing to-device requests as sent.
1283    async fn mark_to_device_request_as_sent(&self, request_id: &TransactionId) -> StoreResult<()> {
1284        self.inner.verification_machine.mark_request_as_sent(request_id);
1285        self.inner.key_request_machine.mark_outgoing_request_as_sent(request_id).await?;
1286        self.inner.group_session_manager.mark_request_as_sent(request_id).await?;
1287        self.inner.session_manager.mark_outgoing_request_as_sent(request_id);
1288        Ok(())
1289    }
1290
1291    /// Get a verification object for the given user id with the given flow id.
1292    pub fn get_verification(&self, user_id: &UserId, flow_id: &str) -> Option<Verification> {
1293        self.inner.verification_machine.get_verification(user_id, flow_id)
1294    }
1295
1296    /// Get a verification request object with the given flow id.
1297    pub fn get_verification_request(
1298        &self,
1299        user_id: &UserId,
1300        flow_id: impl AsRef<str>,
1301    ) -> Option<VerificationRequest> {
1302        self.inner.verification_machine.get_request(user_id, flow_id)
1303    }
1304
1305    /// Get all the verification requests of a given user.
1306    pub fn get_verification_requests(&self, user_id: &UserId) -> Vec<VerificationRequest> {
1307        self.inner.verification_machine.get_requests(user_id)
1308    }
1309
1310    async fn handle_to_device_event(&self, changes: &mut Changes, event: &ToDeviceEvents) {
1311        use crate::types::events::ToDeviceEvents::*;
1312
1313        match event {
1314            RoomKeyRequest(e) => self.inner.key_request_machine.receive_incoming_key_request(e),
1315            SecretRequest(e) => self.inner.key_request_machine.receive_incoming_secret_request(e),
1316            RoomKeyWithheld(e) => self.add_withheld_info(changes, e),
1317            KeyVerificationAccept(..)
1318            | KeyVerificationCancel(..)
1319            | KeyVerificationKey(..)
1320            | KeyVerificationMac(..)
1321            | KeyVerificationRequest(..)
1322            | KeyVerificationReady(..)
1323            | KeyVerificationDone(..)
1324            | KeyVerificationStart(..) => {
1325                self.handle_verification_event(event).await;
1326            }
1327            Dummy(_) | RoomKey(_) | ForwardedRoomKey(_) | RoomEncrypted(_) => {}
1328            _ => {}
1329        }
1330    }
1331
1332    fn record_message_id(event: &Raw<AnyToDeviceEvent>) {
1333        use serde::Deserialize;
1334
1335        #[derive(Deserialize)]
1336        struct ContentStub<'a> {
1337            #[serde(borrow, rename = "org.matrix.msgid")]
1338            message_id: Option<&'a str>,
1339        }
1340
1341        #[derive(Deserialize)]
1342        struct ToDeviceStub<'a> {
1343            sender: &'a str,
1344            #[serde(rename = "type")]
1345            event_type: &'a str,
1346            #[serde(borrow)]
1347            content: ContentStub<'a>,
1348        }
1349
1350        if let Ok(event) = event.deserialize_as::<ToDeviceStub<'_>>() {
1351            Span::current().record("sender", event.sender);
1352            Span::current().record("event_type", event.event_type);
1353            Span::current().record("message_id", event.content.message_id);
1354        }
1355    }
1356
1357    /// Decrypt the supplied to-device event (if needed, and if we can) and
1358    /// handle it.
1359    ///
1360    /// Return the same event, decrypted if possible and needed.
1361    ///
1362    /// If we can identify that this to-device event came from a dehydrated
1363    /// device, this method does not process it, and returns `None`.
1364    #[instrument(skip_all, fields(sender, event_type, message_id))]
1365    async fn receive_to_device_event(
1366        &self,
1367        transaction: &mut StoreTransaction,
1368        changes: &mut Changes,
1369        mut raw_event: Raw<AnyToDeviceEvent>,
1370    ) -> Option<ProcessedToDeviceEvent> {
1371        Self::record_message_id(&raw_event);
1372
1373        let event: ToDeviceEvents = match raw_event.deserialize_as() {
1374            Ok(e) => e,
1375            Err(e) => {
1376                // Skip invalid events.
1377                warn!("Received an invalid to-device event: {e}");
1378                return Some(ProcessedToDeviceEvent::Invalid(raw_event));
1379            }
1380        };
1381
1382        debug!("Received a to-device event");
1383
1384        match event {
1385            ToDeviceEvents::RoomEncrypted(e) => {
1386                let decrypted = match self.decrypt_to_device_event(transaction, &e, changes).await {
1387                    Ok(e) => e,
1388                    Err(err) => {
1389                        if let OlmError::SessionWedged(sender, curve_key) = err {
1390                            if let Err(e) = self
1391                                .inner
1392                                .session_manager
1393                                .mark_device_as_wedged(&sender, curve_key)
1394                                .await
1395                            {
1396                                error!(
1397                                    error = ?e,
1398                                    "Couldn't mark device from to be unwedged",
1399                                );
1400                            }
1401                        }
1402
1403                        return Some(ProcessedToDeviceEvent::UnableToDecrypt(raw_event));
1404                    }
1405                };
1406
1407                // We ignore all to-device events from dehydrated devices - we should not
1408                // receive any
1409                match self.to_device_event_is_from_dehydrated_device(&decrypted, &e.sender).await {
1410                    Ok(true) => {
1411                        warn!(
1412                            sender = ?e.sender,
1413                            session = ?decrypted.session,
1414                            "Received a to-device event from a dehydrated device. This is unexpected: ignoring event"
1415                        );
1416                        return None;
1417                    }
1418                    Ok(false) => {}
1419                    Err(err) => {
1420                        error!(
1421                            error = ?err,
1422                            "Couldn't check whether event is from dehydrated device",
1423                        );
1424                    }
1425                }
1426
1427                // New sessions modify the account so we need to save that
1428                // one as well.
1429                match decrypted.session {
1430                    SessionType::New(s) | SessionType::Existing(s) => {
1431                        changes.sessions.push(s);
1432                    }
1433                }
1434
1435                changes.message_hashes.push(decrypted.message_hash);
1436
1437                if let Some(group_session) = decrypted.inbound_group_session {
1438                    changes.inbound_group_sessions.push(group_session);
1439                }
1440
1441                match decrypted.result.raw_event.deserialize_as() {
1442                    Ok(event) => {
1443                        self.handle_to_device_event(changes, &event).await;
1444
1445                        raw_event = event
1446                            .serialize_zeroized()
1447                            .expect("Zeroizing and reserializing our events should always work")
1448                            .cast();
1449                    }
1450                    Err(e) => {
1451                        warn!("Received an invalid encrypted to-device event: {e}");
1452                        raw_event = decrypted.result.raw_event;
1453                    }
1454                }
1455
1456                Some(ProcessedToDeviceEvent::Decrypted(raw_event))
1457            }
1458
1459            e => {
1460                self.handle_to_device_event(changes, &e).await;
1461                Some(ProcessedToDeviceEvent::PlainText(raw_event))
1462            }
1463        }
1464    }
1465
1466    /// Decide whether a decrypted to-device event was sent from a dehydrated
1467    /// device.
1468    ///
1469    /// This accepts an [`OlmDecryptionInfo`] because it deals with a decrypted
1470    /// event.
1471    async fn to_device_event_is_from_dehydrated_device(
1472        &self,
1473        decrypted: &OlmDecryptionInfo,
1474        sender_user_id: &UserId,
1475    ) -> OlmResult<bool> {
1476        // Does the to-device message include device info?
1477        if let Some(device_keys) = decrypted.result.event.sender_device_keys() {
1478            // There is no need to check whether the device keys are signed correctly - any
1479            // to-device message that claims to be from a dehydrated device is weird, so we
1480            // will drop it.
1481
1482            // Does the included device info say the device is dehydrated?
1483            if device_keys.dehydrated.unwrap_or(false) {
1484                return Ok(true);
1485            }
1486            // If not, fall through and check our existing list of devices
1487            // below, just in case the sender is sending us incorrect
1488            // information embedded in the to-device message, but we know
1489            // better.
1490        }
1491
1492        // Do we already know about this device?
1493        Ok(self
1494            .store()
1495            .get_device_from_curve_key(sender_user_id, decrypted.result.sender_key)
1496            .await?
1497            .is_some_and(|d| d.is_dehydrated()))
1498    }
1499
1500    /// Handle a to-device and one-time key counts from a sync response.
1501    ///
1502    /// This will decrypt and handle to-device events returning the decrypted
1503    /// versions of them.
1504    ///
1505    /// To decrypt an event from the room timeline, call [`decrypt_room_event`].
1506    ///
1507    /// # Arguments
1508    ///
1509    /// * `sync_changes` - an [`EncryptionSyncChanges`] value, constructed from
1510    ///   a sync response.
1511    ///
1512    /// [`decrypt_room_event`]: #method.decrypt_room_event
1513    ///
1514    /// # Returns
1515    ///
1516    /// A tuple of (decrypted to-device events, updated room keys).
1517    #[instrument(skip_all)]
1518    pub async fn receive_sync_changes(
1519        &self,
1520        sync_changes: EncryptionSyncChanges<'_>,
1521    ) -> OlmResult<(Vec<ProcessedToDeviceEvent>, Vec<RoomKeyInfo>)> {
1522        let mut store_transaction = self.inner.store.transaction().await;
1523
1524        let (events, changes) =
1525            self.preprocess_sync_changes(&mut store_transaction, sync_changes).await?;
1526
1527        // Technically save_changes also does the same work, so if it's slow we could
1528        // refactor this to do it only once.
1529        let room_key_updates: Vec<_> =
1530            changes.inbound_group_sessions.iter().map(RoomKeyInfo::from).collect();
1531
1532        self.store().save_changes(changes).await?;
1533        store_transaction.commit().await?;
1534
1535        Ok((events, room_key_updates))
1536    }
1537
1538    /// Initial processing of the changes specified within a sync response.
1539    ///
1540    /// Returns the to-device events (decrypted where needed and where possible)
1541    /// and the processed set of changes.
1542    ///
1543    /// If any of the to-device events in the supplied changes were sent from
1544    /// dehydrated devices, these are not processed, and are omitted from
1545    /// the returned list, as per MSC3814.
1546    pub(crate) async fn preprocess_sync_changes(
1547        &self,
1548        transaction: &mut StoreTransaction,
1549        sync_changes: EncryptionSyncChanges<'_>,
1550    ) -> OlmResult<(Vec<ProcessedToDeviceEvent>, Changes)> {
1551        // Remove verification objects that have expired or are done.
1552        let mut events: Vec<ProcessedToDeviceEvent> = self
1553            .inner
1554            .verification_machine
1555            .garbage_collect()
1556            .iter()
1557            // These are `fake` to device events just serving as local echo
1558            // in order that our own client can react quickly to cancelled transaction.
1559            // Just use PlainText for that.
1560            .map(|e| ProcessedToDeviceEvent::PlainText(e.clone()))
1561            .collect();
1562        // The account is automatically saved by the store transaction created by the
1563        // caller.
1564        let mut changes = Default::default();
1565
1566        {
1567            let account = transaction.account().await?;
1568            account.update_key_counts(
1569                sync_changes.one_time_keys_counts,
1570                sync_changes.unused_fallback_keys,
1571            )
1572        }
1573
1574        if let Err(e) = self
1575            .inner
1576            .identity_manager
1577            .receive_device_changes(
1578                transaction.cache(),
1579                sync_changes.changed_devices.changed.iter().map(|u| u.as_ref()),
1580            )
1581            .await
1582        {
1583            error!(error = ?e, "Error marking a tracked user as changed");
1584        }
1585
1586        for raw_event in sync_changes.to_device_events {
1587            let raw_event =
1588                Box::pin(self.receive_to_device_event(transaction, &mut changes, raw_event)).await;
1589
1590            if let Some(raw_event) = raw_event {
1591                events.push(raw_event);
1592            }
1593        }
1594
1595        let changed_sessions = self
1596            .inner
1597            .key_request_machine
1598            .collect_incoming_key_requests(transaction.cache())
1599            .await?;
1600
1601        changes.sessions.extend(changed_sessions);
1602        changes.next_batch_token = sync_changes.next_batch_token;
1603
1604        Ok((events, changes))
1605    }
1606
1607    /// Request a room key from our devices.
1608    ///
1609    /// This method will return a request cancellation and a new key request if
1610    /// the key was already requested, otherwise it will return just the key
1611    /// request.
1612    ///
1613    /// The request cancellation *must* be sent out before the request is sent
1614    /// out, otherwise devices will ignore the key request.
1615    ///
1616    /// # Arguments
1617    ///
1618    /// * `room_id` - The id of the room where the key is used in.
1619    ///
1620    /// * `sender_key` - The curve25519 key of the sender that owns the key.
1621    ///
1622    /// * `session_id` - The id that uniquely identifies the session.
1623    pub async fn request_room_key(
1624        &self,
1625        event: &Raw<EncryptedEvent>,
1626        room_id: &RoomId,
1627    ) -> MegolmResult<(Option<OutgoingRequest>, OutgoingRequest)> {
1628        let event = event.deserialize()?;
1629        self.inner.key_request_machine.request_key(room_id, &event).await
1630    }
1631
1632    /// Find whether the supplied session is verified, and provide
1633    /// explanation of what is missing/wrong if not.
1634    ///
1635    /// Checks both the stored verification state of the session and a
1636    /// recalculated verification state based on our current knowledge, and
1637    /// returns the more trusted of the two.
1638    ///
1639    /// Store the updated [`SenderData`] for this session in the store
1640    /// if we find an updated value for it.
1641    async fn get_or_update_verification_state(
1642        &self,
1643        session: &InboundGroupSession,
1644        sender: &UserId,
1645    ) -> MegolmResult<(VerificationState, Option<OwnedDeviceId>)> {
1646        /// Whether we should recalculate the Megolm sender's data, given the
1647        /// current sender data. We only want to recalculate if it might
1648        /// increase trust and allow us to decrypt messages that we
1649        /// otherwise might refuse to decrypt.
1650        ///
1651        /// We recalculate for all states except:
1652        ///
1653        /// - SenderUnverified: the sender is trusted enough that we will
1654        ///   decrypt their messages in all cases, or
1655        /// - SenderVerified: the sender is the most trusted they can be.
1656        fn should_recalculate_sender_data(sender_data: &SenderData) -> bool {
1657            matches!(
1658                sender_data,
1659                SenderData::UnknownDevice { .. }
1660                    | SenderData::DeviceInfo { .. }
1661                    | SenderData::VerificationViolation { .. }
1662            )
1663        }
1664
1665        let sender_data = if should_recalculate_sender_data(&session.sender_data) {
1666            // The session is not sure of the sender yet. Calculate it.
1667            let calculated_sender_data = SenderDataFinder::find_using_curve_key(
1668                self.store(),
1669                session.sender_key(),
1670                sender,
1671                session,
1672            )
1673            .await?;
1674
1675            // Is the newly-calculated sender data more trusted?
1676            if calculated_sender_data.compare_trust_level(&session.sender_data).is_gt() {
1677                // Yes - save it to the store
1678                let mut new_session = session.clone();
1679                new_session.sender_data = calculated_sender_data.clone();
1680                self.store().save_inbound_group_sessions(&[new_session]).await?;
1681
1682                // and use it now.
1683                calculated_sender_data
1684            } else {
1685                // No - use the existing data.
1686                session.sender_data.clone()
1687            }
1688        } else {
1689            session.sender_data.clone()
1690        };
1691
1692        Ok(sender_data_to_verification_state(sender_data, session.has_been_imported()))
1693    }
1694
1695    /// Request missing local secrets from our devices (cross signing private
1696    /// keys, megolm backup). This will ask the sdk to create outgoing
1697    /// request to get the missing secrets.
1698    ///
1699    /// The requests will be processed as soon as `outgoing_requests()` is
1700    /// called to process them.
1701    ///
1702    /// # Returns
1703    ///
1704    /// A bool result saying if actual secrets were missing and have been
1705    /// requested
1706    ///
1707    /// # Examples
1708    //
1709    /// ```
1710    /// # async {
1711    /// # use matrix_sdk_crypto::OlmMachine;
1712    /// # let machine: OlmMachine = unimplemented!();
1713    /// if machine.query_missing_secrets_from_other_sessions().await.unwrap() {
1714    ///     let to_send = machine.outgoing_requests().await.unwrap();
1715    ///     // send the to device requests
1716    /// };
1717    /// # anyhow::Ok(()) };
1718    /// ```
1719    pub async fn query_missing_secrets_from_other_sessions(&self) -> StoreResult<bool> {
1720        let identity = self.inner.user_identity.lock().await;
1721        let mut secrets = identity.get_missing_secrets().await;
1722
1723        if self.store().load_backup_keys().await?.decryption_key.is_none() {
1724            secrets.push(SecretName::RecoveryKey);
1725        }
1726
1727        if secrets.is_empty() {
1728            debug!("No missing requests to query");
1729            return Ok(false);
1730        }
1731
1732        let secret_requests = GossipMachine::request_missing_secrets(self.user_id(), secrets);
1733
1734        // Check if there are already in-flight requests for these secrets?
1735        let unsent_request = self.store().get_unsent_secret_requests().await?;
1736        let not_yet_requested = secret_requests
1737            .into_iter()
1738            .filter(|request| !unsent_request.iter().any(|unsent| unsent.info == request.info))
1739            .collect_vec();
1740
1741        if not_yet_requested.is_empty() {
1742            debug!("The missing secrets have already been requested");
1743            Ok(false)
1744        } else {
1745            debug!("Requesting missing secrets");
1746
1747            let changes = Changes { key_requests: not_yet_requested, ..Default::default() };
1748
1749            self.store().save_changes(changes).await?;
1750            Ok(true)
1751        }
1752    }
1753
1754    /// Get some metadata pertaining to a given group session.
1755    ///
1756    /// This includes the session owner's Matrix user ID, their device ID, info
1757    /// regarding the cryptographic algorithm and whether the session, and by
1758    /// extension the events decrypted by the session, are trusted.
1759    async fn get_encryption_info(
1760        &self,
1761        session: &InboundGroupSession,
1762        sender: &UserId,
1763    ) -> MegolmResult<EncryptionInfo> {
1764        let (verification_state, device_id) =
1765            self.get_or_update_verification_state(session, sender).await?;
1766
1767        let sender = sender.to_owned();
1768
1769        Ok(EncryptionInfo {
1770            sender,
1771            sender_device: device_id,
1772            algorithm_info: AlgorithmInfo::MegolmV1AesSha2 {
1773                curve25519_key: session.sender_key().to_base64(),
1774                sender_claimed_keys: session
1775                    .signing_keys()
1776                    .iter()
1777                    .map(|(k, v)| (k.to_owned(), v.to_base64()))
1778                    .collect(),
1779                session_id: Some(session.session_id().to_owned()),
1780            },
1781            verification_state,
1782        })
1783    }
1784
1785    async fn decrypt_megolm_events(
1786        &self,
1787        room_id: &RoomId,
1788        event: &EncryptedEvent,
1789        content: &SupportedEventEncryptionSchemes<'_>,
1790        decryption_settings: &DecryptionSettings,
1791    ) -> MegolmResult<(JsonObject, EncryptionInfo)> {
1792        let session =
1793            self.get_inbound_group_session_or_error(room_id, content.session_id()).await?;
1794
1795        // This function is only ever called by decrypt_room_event, so
1796        // room_id, sender, algorithm and session_id are recorded already
1797        //
1798        // While we already record the sender key in some cases from the event, the
1799        // sender key in the event is deprecated, so let's record it now.
1800        Span::current().record("sender_key", debug(session.sender_key()));
1801
1802        let result = session.decrypt(event).await;
1803        match result {
1804            Ok((decrypted_event, _)) => {
1805                let encryption_info = self.get_encryption_info(&session, &event.sender).await?;
1806
1807                self.check_sender_trust_requirement(
1808                    &session,
1809                    &encryption_info,
1810                    &decryption_settings.sender_device_trust_requirement,
1811                )?;
1812
1813                Ok((decrypted_event, encryption_info))
1814            }
1815            Err(error) => Err(
1816                if let MegolmError::Decryption(DecryptionError::UnknownMessageIndex(_, _)) = error {
1817                    let withheld_code = self
1818                        .inner
1819                        .store
1820                        .get_withheld_info(room_id, content.session_id())
1821                        .await?
1822                        .map(|e| e.content.withheld_code());
1823
1824                    if withheld_code.is_some() {
1825                        // Partially withheld, report with a withheld code if we have one.
1826                        MegolmError::MissingRoomKey(withheld_code)
1827                    } else {
1828                        error
1829                    }
1830                } else {
1831                    error
1832                },
1833            ),
1834        }
1835    }
1836
1837    /// Check that a Megolm event satisfies the sender trust
1838    /// requirement from the decryption settings.
1839    ///
1840    /// If the requirement is not satisfied, returns
1841    /// [`MegolmError::SenderIdentityNotTrusted`].
1842    fn check_sender_trust_requirement(
1843        &self,
1844        session: &InboundGroupSession,
1845        encryption_info: &EncryptionInfo,
1846        trust_requirement: &TrustRequirement,
1847    ) -> MegolmResult<()> {
1848        trace!(
1849            verification_state = ?encryption_info.verification_state,
1850            ?trust_requirement, "check_sender_trust_requirement",
1851        );
1852
1853        // VerificationState::Verified is acceptable for all TrustRequirement levels, so
1854        // let's get that out of the way
1855        let verification_level = match &encryption_info.verification_state {
1856            VerificationState::Verified => return Ok(()),
1857            VerificationState::Unverified(verification_level) => verification_level,
1858        };
1859
1860        let ok = match trust_requirement {
1861            TrustRequirement::Untrusted => true,
1862
1863            TrustRequirement::CrossSignedOrLegacy => {
1864                // `VerificationLevel::UnsignedDevice` and `VerificationLevel::None` correspond
1865                // to `SenderData::DeviceInfo` and `SenderData::UnknownDevice`
1866                // respectively, and those cases may be acceptable if the reason
1867                // for the lack of data is that the sessions were established
1868                // before we started collecting SenderData.
1869                let legacy_session = match session.sender_data {
1870                    SenderData::DeviceInfo { legacy_session, .. } => legacy_session,
1871                    SenderData::UnknownDevice { legacy_session, .. } => legacy_session,
1872                    _ => false,
1873                };
1874
1875                // In the CrossSignedOrLegacy case the following rules apply:
1876                //
1877                // 1. Identities we have not yet verified can be decrypted regardless of the
1878                //    legacy state of the session.
1879                // 2. Devices that aren't signed by the owning identity of the device can only
1880                //    be decrypted if it's a legacy session.
1881                // 3. If we have no information about the device, we should only decrypt if it's
1882                //    a legacy session.
1883                // 4. Anything else, should throw an error.
1884                match (verification_level, legacy_session) {
1885                    // Case 1
1886                    (VerificationLevel::UnverifiedIdentity, _) => true,
1887
1888                    // Case 2
1889                    (VerificationLevel::UnsignedDevice, true) => true,
1890
1891                    // Case 3
1892                    (VerificationLevel::None(_), true) => true,
1893
1894                    // Case 4
1895                    (VerificationLevel::VerificationViolation, _)
1896                    | (VerificationLevel::UnsignedDevice, false)
1897                    | (VerificationLevel::None(_), false) => false,
1898                }
1899            }
1900
1901            // If cross-signing of identities is required, the only acceptable unverified case
1902            // is when the identity is signed but not yet verified by us.
1903            TrustRequirement::CrossSigned => match verification_level {
1904                VerificationLevel::UnverifiedIdentity => true,
1905
1906                VerificationLevel::VerificationViolation
1907                | VerificationLevel::UnsignedDevice
1908                | VerificationLevel::None(_) => false,
1909            },
1910        };
1911
1912        if ok {
1913            Ok(())
1914        } else {
1915            Err(MegolmError::SenderIdentityNotTrusted(verification_level.clone()))
1916        }
1917    }
1918
1919    /// Attempt to retrieve an inbound group session from the store.
1920    ///
1921    /// If the session is not found, checks for withheld reports, and returns a
1922    /// [`MegolmError::MissingRoomKey`] error.
1923    async fn get_inbound_group_session_or_error(
1924        &self,
1925        room_id: &RoomId,
1926        session_id: &str,
1927    ) -> MegolmResult<InboundGroupSession> {
1928        match self.store().get_inbound_group_session(room_id, session_id).await? {
1929            Some(session) => Ok(session),
1930            None => {
1931                let withheld_code = self
1932                    .inner
1933                    .store
1934                    .get_withheld_info(room_id, session_id)
1935                    .await?
1936                    .map(|e| e.content.withheld_code());
1937                Err(MegolmError::MissingRoomKey(withheld_code))
1938            }
1939        }
1940    }
1941
1942    /// Attempt to decrypt an event from a room timeline, returning information
1943    /// on the failure if it fails.
1944    ///
1945    /// # Arguments
1946    ///
1947    /// * `event` - The event that should be decrypted.
1948    ///
1949    /// * `room_id` - The ID of the room where the event was sent to.
1950    ///
1951    /// # Returns
1952    ///
1953    /// The decrypted event, if it was successfully decrypted. Otherwise,
1954    /// information on the failure, unless the failure was due to an
1955    /// internal error, in which case, an `Err` result.
1956    pub async fn try_decrypt_room_event(
1957        &self,
1958        raw_event: &Raw<EncryptedEvent>,
1959        room_id: &RoomId,
1960        decryption_settings: &DecryptionSettings,
1961    ) -> Result<RoomEventDecryptionResult, CryptoStoreError> {
1962        match self.decrypt_room_event_inner(raw_event, room_id, true, decryption_settings).await {
1963            Ok(decrypted) => Ok(RoomEventDecryptionResult::Decrypted(decrypted)),
1964            Err(err) => Ok(RoomEventDecryptionResult::UnableToDecrypt(megolm_error_to_utd_info(
1965                raw_event, err,
1966            )?)),
1967        }
1968    }
1969
1970    /// Decrypt an event from a room timeline.
1971    ///
1972    /// # Arguments
1973    ///
1974    /// * `event` - The event that should be decrypted.
1975    ///
1976    /// * `room_id` - The ID of the room where the event was sent to.
1977    pub async fn decrypt_room_event(
1978        &self,
1979        event: &Raw<EncryptedEvent>,
1980        room_id: &RoomId,
1981        decryption_settings: &DecryptionSettings,
1982    ) -> MegolmResult<DecryptedRoomEvent> {
1983        self.decrypt_room_event_inner(event, room_id, true, decryption_settings).await
1984    }
1985
1986    #[instrument(name = "decrypt_room_event", skip_all, fields(?room_id, event_id, origin_server_ts, sender, algorithm, session_id, message_index, sender_key))]
1987    async fn decrypt_room_event_inner(
1988        &self,
1989        event: &Raw<EncryptedEvent>,
1990        room_id: &RoomId,
1991        decrypt_unsigned: bool,
1992        decryption_settings: &DecryptionSettings,
1993    ) -> MegolmResult<DecryptedRoomEvent> {
1994        let event = event.deserialize()?;
1995
1996        Span::current()
1997            .record("sender", debug(&event.sender))
1998            .record("event_id", debug(&event.event_id))
1999            .record(
2000                "origin_server_ts",
2001                timestamp_to_iso8601(event.origin_server_ts)
2002                    .unwrap_or_else(|| "<out of range>".to_owned()),
2003            )
2004            .record("algorithm", debug(event.content.algorithm()));
2005
2006        let content: SupportedEventEncryptionSchemes<'_> = match &event.content.scheme {
2007            RoomEventEncryptionScheme::MegolmV1AesSha2(c) => {
2008                Span::current().record("sender_key", debug(c.sender_key));
2009                c.into()
2010            }
2011            #[cfg(feature = "experimental-algorithms")]
2012            RoomEventEncryptionScheme::MegolmV2AesSha2(c) => c.into(),
2013            RoomEventEncryptionScheme::Unknown(_) => {
2014                warn!("Received an encrypted room event with an unsupported algorithm");
2015                return Err(EventError::UnsupportedAlgorithm.into());
2016            }
2017        };
2018
2019        Span::current().record("session_id", content.session_id());
2020        Span::current().record("message_index", content.message_index());
2021
2022        let result =
2023            self.decrypt_megolm_events(room_id, &event, &content, decryption_settings).await;
2024
2025        if let Err(e) = &result {
2026            #[cfg(feature = "automatic-room-key-forwarding")]
2027            match e {
2028                // Optimisation should we request if we received a withheld code?
2029                // Maybe for some code there is no point
2030                MegolmError::MissingRoomKey(_)
2031                | MegolmError::Decryption(DecryptionError::UnknownMessageIndex(_, _)) => {
2032                    self.inner
2033                        .key_request_machine
2034                        .create_outgoing_key_request(room_id, &event)
2035                        .await?;
2036                }
2037                _ => {}
2038            }
2039
2040            warn!("Failed to decrypt a room event: {e}");
2041        }
2042
2043        let (mut decrypted_event, encryption_info) = result?;
2044
2045        let mut unsigned_encryption_info = None;
2046        if decrypt_unsigned {
2047            // Try to decrypt encrypted unsigned events.
2048            unsigned_encryption_info = self
2049                .decrypt_unsigned_events(&mut decrypted_event, room_id, decryption_settings)
2050                .await;
2051        }
2052
2053        let event = serde_json::from_value::<Raw<AnyMessageLikeEvent>>(decrypted_event.into())?;
2054
2055        Ok(DecryptedRoomEvent { event, encryption_info, unsigned_encryption_info })
2056    }
2057
2058    /// Try to decrypt the events bundled in the `unsigned` object of the given
2059    /// event.
2060    ///
2061    /// # Arguments
2062    ///
2063    /// * `main_event` - The event that may contain bundled encrypted events in
2064    ///   its `unsigned` object.
2065    ///
2066    /// * `room_id` - The ID of the room where the event was sent to.
2067    async fn decrypt_unsigned_events(
2068        &self,
2069        main_event: &mut JsonObject,
2070        room_id: &RoomId,
2071        decryption_settings: &DecryptionSettings,
2072    ) -> Option<BTreeMap<UnsignedEventLocation, UnsignedDecryptionResult>> {
2073        let unsigned = main_event.get_mut("unsigned")?.as_object_mut()?;
2074        let mut unsigned_encryption_info: Option<
2075            BTreeMap<UnsignedEventLocation, UnsignedDecryptionResult>,
2076        > = None;
2077
2078        // Search for an encrypted event in `m.replace`, an edit.
2079        let location = UnsignedEventLocation::RelationsReplace;
2080        let replace = location.find_mut(unsigned);
2081        if let Some(decryption_result) =
2082            self.decrypt_unsigned_event(replace, room_id, decryption_settings).await
2083        {
2084            unsigned_encryption_info
2085                .get_or_insert_with(Default::default)
2086                .insert(location, decryption_result);
2087        }
2088
2089        // Search for an encrypted event in `latest_event` in `m.thread`, the
2090        // latest event of a thread.
2091        let location = UnsignedEventLocation::RelationsThreadLatestEvent;
2092        let thread_latest_event = location.find_mut(unsigned);
2093        if let Some(decryption_result) =
2094            self.decrypt_unsigned_event(thread_latest_event, room_id, decryption_settings).await
2095        {
2096            unsigned_encryption_info
2097                .get_or_insert_with(Default::default)
2098                .insert(location, decryption_result);
2099        }
2100
2101        unsigned_encryption_info
2102    }
2103
2104    /// Try to decrypt the given bundled event.
2105    ///
2106    /// # Arguments
2107    ///
2108    /// * `event` - The bundled event that may be encrypted
2109    ///
2110    /// * `room_id` - The ID of the room where the event was sent to.
2111    fn decrypt_unsigned_event<'a>(
2112        &'a self,
2113        event: Option<&'a mut Value>,
2114        room_id: &'a RoomId,
2115        decryption_settings: &'a DecryptionSettings,
2116    ) -> BoxFuture<'a, Option<UnsignedDecryptionResult>> {
2117        Box::pin(async move {
2118            let event = event?;
2119
2120            let is_encrypted = event
2121                .get("type")
2122                .and_then(|type_| type_.as_str())
2123                .is_some_and(|s| s == "m.room.encrypted");
2124            if !is_encrypted {
2125                return None;
2126            }
2127
2128            let raw_event = serde_json::from_value(event.clone()).ok()?;
2129            match self
2130                .decrypt_room_event_inner(&raw_event, room_id, false, decryption_settings)
2131                .await
2132            {
2133                Ok(decrypted_event) => {
2134                    // Replace the encrypted event.
2135                    *event = serde_json::to_value(decrypted_event.event).ok()?;
2136                    Some(UnsignedDecryptionResult::Decrypted(decrypted_event.encryption_info))
2137                }
2138                Err(err) => {
2139                    // For now, we throw away crypto store errors and just treat the unsigned event
2140                    // as unencrypted. Crypto store errors represent problems with the application
2141                    // rather than normal UTD errors, so they should probably be propagated
2142                    // rather than swallowed.
2143                    let utd_info = megolm_error_to_utd_info(&raw_event, err).ok()?;
2144                    Some(UnsignedDecryptionResult::UnableToDecrypt(utd_info))
2145                }
2146            }
2147        })
2148    }
2149
2150    /// Check if we have the room key for the given event in the store.
2151    ///
2152    /// # Arguments
2153    ///
2154    /// * `event` - The event to get information for.
2155    /// * `room_id` - The ID of the room where the event was sent to.
2156    pub async fn is_room_key_available(
2157        &self,
2158        event: &Raw<EncryptedEvent>,
2159        room_id: &RoomId,
2160    ) -> Result<bool, CryptoStoreError> {
2161        let event = event.deserialize()?;
2162
2163        let (session_id, message_index) = match &event.content.scheme {
2164            RoomEventEncryptionScheme::MegolmV1AesSha2(c) => {
2165                (&c.session_id, c.ciphertext.message_index())
2166            }
2167            #[cfg(feature = "experimental-algorithms")]
2168            RoomEventEncryptionScheme::MegolmV2AesSha2(c) => {
2169                (&c.session_id, c.ciphertext.message_index())
2170            }
2171            RoomEventEncryptionScheme::Unknown(_) => {
2172                // We don't support this encryption algorithm, so clearly don't have its key.
2173                return Ok(false);
2174            }
2175        };
2176
2177        // Check that we have the session in the store, and that its first known index
2178        // predates the index of our message.
2179        Ok(self
2180            .store()
2181            .get_inbound_group_session(room_id, session_id)
2182            .await?
2183            .filter(|s| s.first_known_index() <= message_index)
2184            .is_some())
2185    }
2186
2187    /// Get encryption info for a decrypted timeline event.
2188    ///
2189    /// This recalculates the [`EncryptionInfo`] data that is returned by
2190    /// [`OlmMachine::decrypt_room_event`], based on the current
2191    /// verification status of the sender, etc.
2192    ///
2193    /// Returns an error for an unencrypted event.
2194    ///
2195    /// # Arguments
2196    ///
2197    /// * `event` - The event to get information for.
2198    /// * `room_id` - The ID of the room where the event was sent to.
2199    pub async fn get_room_event_encryption_info(
2200        &self,
2201        event: &Raw<EncryptedEvent>,
2202        room_id: &RoomId,
2203    ) -> MegolmResult<EncryptionInfo> {
2204        let event = event.deserialize()?;
2205
2206        let content: SupportedEventEncryptionSchemes<'_> = match &event.content.scheme {
2207            RoomEventEncryptionScheme::MegolmV1AesSha2(c) => c.into(),
2208            #[cfg(feature = "experimental-algorithms")]
2209            RoomEventEncryptionScheme::MegolmV2AesSha2(c) => c.into(),
2210            RoomEventEncryptionScheme::Unknown(_) => {
2211                return Err(EventError::UnsupportedAlgorithm.into());
2212            }
2213        };
2214
2215        self.get_session_encryption_info(room_id, content.session_id(), &event.sender).await
2216    }
2217
2218    /// Get encryption info for a megolm session.
2219    ///
2220    /// This recalculates the [`EncryptionInfo`] data that is returned by
2221    /// [`OlmMachine::decrypt_room_event`], based on the current
2222    /// verification status of the sender, etc.
2223    ///
2224    /// Returns an error if the session can't be found.
2225    ///
2226    /// # Arguments
2227    ///
2228    /// * `room_id` - The ID of the room where the session is being used.
2229    /// * `session_id` - The ID of the session to get information for.
2230    /// * `sender` - The user ID of the sender who created this session.
2231    pub async fn get_session_encryption_info(
2232        &self,
2233        room_id: &RoomId,
2234        session_id: &str,
2235        sender: &UserId,
2236    ) -> MegolmResult<EncryptionInfo> {
2237        let session = self.get_inbound_group_session_or_error(room_id, session_id).await?;
2238        self.get_encryption_info(&session, sender).await
2239    }
2240
2241    /// Update the list of tracked users.
2242    ///
2243    /// The OlmMachine maintains a list of users whose devices we are keeping
2244    /// track of: these are known as "tracked users". These must be users
2245    /// that we share a room with, so that the server sends us updates for
2246    /// their device lists.
2247    ///
2248    /// # Arguments
2249    ///
2250    /// * `users` - An iterator over user ids that should be added to the list
2251    ///   of tracked users
2252    ///
2253    /// Any users that hadn't been seen before will be flagged for a key query
2254    /// immediately, and whenever [`OlmMachine::receive_sync_changes()`]
2255    /// receives a "changed" notification for that user in the future.
2256    ///
2257    /// Users that were already in the list are unaffected.
2258    pub async fn update_tracked_users(
2259        &self,
2260        users: impl IntoIterator<Item = &UserId>,
2261    ) -> StoreResult<()> {
2262        self.inner.identity_manager.update_tracked_users(users).await
2263    }
2264
2265    /// Mark all tracked users as dirty.
2266    ///
2267    /// All users *whose device lists we are tracking* are flagged as needing a
2268    /// key query. Users whose devices we are not tracking are ignored.
2269    pub async fn mark_all_tracked_users_as_dirty(&self) -> StoreResult<()> {
2270        self.inner
2271            .identity_manager
2272            .mark_all_tracked_users_as_dirty(self.inner.store.cache().await?)
2273            .await
2274    }
2275
2276    async fn wait_if_user_pending(
2277        &self,
2278        user_id: &UserId,
2279        timeout: Option<Duration>,
2280    ) -> StoreResult<()> {
2281        if let Some(timeout) = timeout {
2282            let cache = self.store().cache().await?;
2283            self.inner
2284                .identity_manager
2285                .key_query_manager
2286                .wait_if_user_key_query_pending(cache, timeout, user_id)
2287                .await?;
2288        }
2289        Ok(())
2290    }
2291
2292    /// Get a specific device of a user.
2293    ///
2294    /// # Arguments
2295    ///
2296    /// * `user_id` - The unique id of the user that the device belongs to.
2297    ///
2298    /// * `device_id` - The unique id of the device.
2299    ///
2300    /// * `timeout` - The amount of time we should wait before returning if the
2301    /// user's device list has been marked as stale. **Note**, this assumes that
2302    /// the requests from [`OlmMachine::outgoing_requests`] are being
2303    /// processed and sent out.
2304    ///
2305    /// Returns a `Device` if one is found and the crypto store didn't throw an
2306    /// error.
2307    ///
2308    /// # Examples
2309    ///
2310    /// ```
2311    /// # use matrix_sdk_crypto::OlmMachine;
2312    /// # use ruma::{device_id, user_id};
2313    /// # let alice = user_id!("@alice:example.org").to_owned();
2314    /// # futures_executor::block_on(async {
2315    /// # let machine = OlmMachine::new(&alice, device_id!("DEVICEID")).await;
2316    /// let device = machine.get_device(&alice, device_id!("DEVICEID"), None).await;
2317    ///
2318    /// println!("{:?}", device);
2319    /// # });
2320    /// ```
2321    #[instrument(skip(self))]
2322    pub async fn get_device(
2323        &self,
2324        user_id: &UserId,
2325        device_id: &DeviceId,
2326        timeout: Option<Duration>,
2327    ) -> StoreResult<Option<Device>> {
2328        self.wait_if_user_pending(user_id, timeout).await?;
2329        self.store().get_device(user_id, device_id).await
2330    }
2331
2332    /// Get the cross signing user identity of a user.
2333    ///
2334    /// # Arguments
2335    ///
2336    /// * `user_id` - The unique id of the user that the identity belongs to
2337    ///
2338    /// * `timeout` - The amount of time we should wait before returning if the
2339    /// user's device list has been marked as stale. **Note**, this assumes that
2340    /// the requests from [`OlmMachine::outgoing_requests`] are being
2341    /// processed and sent out.
2342    ///
2343    /// Returns a [`UserIdentity`] enum if one is found and the crypto store
2344    /// didn't throw an error.
2345    #[instrument(skip(self))]
2346    pub async fn get_identity(
2347        &self,
2348        user_id: &UserId,
2349        timeout: Option<Duration>,
2350    ) -> StoreResult<Option<UserIdentity>> {
2351        self.wait_if_user_pending(user_id, timeout).await?;
2352        self.store().get_identity(user_id).await
2353    }
2354
2355    /// Get a map holding all the devices of an user.
2356    ///
2357    /// # Arguments
2358    ///
2359    /// * `user_id` - The unique id of the user that the devices belong to.
2360    ///
2361    /// * `timeout` - The amount of time we should wait before returning if the
2362    /// user's device list has been marked as stale. **Note**, this assumes that
2363    /// the requests from [`OlmMachine::outgoing_requests`] are being
2364    /// processed and sent out.
2365    ///
2366    /// # Examples
2367    ///
2368    /// ```
2369    /// # use matrix_sdk_crypto::OlmMachine;
2370    /// # use ruma::{device_id, user_id};
2371    /// # let alice = user_id!("@alice:example.org").to_owned();
2372    /// # futures_executor::block_on(async {
2373    /// # let machine = OlmMachine::new(&alice, device_id!("DEVICEID")).await;
2374    /// let devices = machine.get_user_devices(&alice, None).await.unwrap();
2375    ///
2376    /// for device in devices.devices() {
2377    ///     println!("{:?}", device);
2378    /// }
2379    /// # });
2380    /// ```
2381    #[instrument(skip(self))]
2382    pub async fn get_user_devices(
2383        &self,
2384        user_id: &UserId,
2385        timeout: Option<Duration>,
2386    ) -> StoreResult<UserDevices> {
2387        self.wait_if_user_pending(user_id, timeout).await?;
2388        self.store().get_user_devices(user_id).await
2389    }
2390
2391    /// Get the status of the private cross signing keys.
2392    ///
2393    /// This can be used to check which private cross signing keys we have
2394    /// stored locally.
2395    pub async fn cross_signing_status(&self) -> CrossSigningStatus {
2396        self.inner.user_identity.lock().await.status().await
2397    }
2398
2399    /// Export all the private cross signing keys we have.
2400    ///
2401    /// The export will contain the seed for the ed25519 keys as a unpadded
2402    /// base64 encoded string.
2403    ///
2404    /// This method returns `None` if we don't have any private cross signing
2405    /// keys.
2406    pub async fn export_cross_signing_keys(&self) -> StoreResult<Option<CrossSigningKeyExport>> {
2407        let master_key = self.store().export_secret(&SecretName::CrossSigningMasterKey).await?;
2408        let self_signing_key =
2409            self.store().export_secret(&SecretName::CrossSigningSelfSigningKey).await?;
2410        let user_signing_key =
2411            self.store().export_secret(&SecretName::CrossSigningUserSigningKey).await?;
2412
2413        Ok(if master_key.is_none() && self_signing_key.is_none() && user_signing_key.is_none() {
2414            None
2415        } else {
2416            Some(CrossSigningKeyExport { master_key, self_signing_key, user_signing_key })
2417        })
2418    }
2419
2420    /// Import our private cross signing keys.
2421    ///
2422    /// The export needs to contain the seed for the ed25519 keys as an unpadded
2423    /// base64 encoded string.
2424    pub async fn import_cross_signing_keys(
2425        &self,
2426        export: CrossSigningKeyExport,
2427    ) -> Result<CrossSigningStatus, SecretImportError> {
2428        self.store().import_cross_signing_keys(export).await
2429    }
2430
2431    async fn sign_with_master_key(
2432        &self,
2433        message: &str,
2434    ) -> Result<(OwnedDeviceKeyId, Ed25519Signature), SignatureError> {
2435        let identity = &*self.inner.user_identity.lock().await;
2436        let key_id = identity.master_key_id().await.ok_or(SignatureError::MissingSigningKey)?;
2437
2438        let signature = identity.sign(message).await?;
2439
2440        Ok((key_id, signature))
2441    }
2442
2443    /// Sign the given message using our device key and if available cross
2444    /// signing master key.
2445    ///
2446    /// Presently, this should only be used for signing the server-side room
2447    /// key backups.
2448    pub async fn sign(&self, message: &str) -> Result<Signatures, CryptoStoreError> {
2449        let mut signatures = Signatures::new();
2450
2451        {
2452            let cache = self.inner.store.cache().await?;
2453            let account = cache.account().await?;
2454            let key_id = account.signing_key_id();
2455            let signature = account.sign(message);
2456            signatures.add_signature(self.user_id().to_owned(), key_id, signature);
2457        }
2458
2459        match self.sign_with_master_key(message).await {
2460            Ok((key_id, signature)) => {
2461                signatures.add_signature(self.user_id().to_owned(), key_id, signature);
2462            }
2463            Err(e) => {
2464                warn!(error = ?e, "Couldn't sign the message using the cross signing master key")
2465            }
2466        }
2467
2468        Ok(signatures)
2469    }
2470
2471    /// Get a reference to the backup related state machine.
2472    ///
2473    /// This state machine can be used to incrementally backup all room keys to
2474    /// the server.
2475    pub fn backup_machine(&self) -> &BackupMachine {
2476        &self.inner.backup_machine
2477    }
2478
2479    /// Syncs the database and in-memory generation counter.
2480    ///
2481    /// This requires that the crypto store lock has been acquired already.
2482    pub async fn initialize_crypto_store_generation(
2483        &self,
2484        generation: &Mutex<Option<u64>>,
2485    ) -> StoreResult<()> {
2486        // Avoid reentrant initialization by taking the lock for the entire's function
2487        // scope.
2488        let mut gen_guard = generation.lock().await;
2489
2490        let prev_generation =
2491            self.inner.store.get_custom_value(Self::CURRENT_GENERATION_STORE_KEY).await?;
2492
2493        let gen = match prev_generation {
2494            Some(val) => {
2495                // There was a value in the store. We need to signal that we're a different
2496                // process, so we don't just reuse the value but increment it.
2497                u64::from_le_bytes(val.try_into().map_err(|_| {
2498                    CryptoStoreError::InvalidLockGeneration("invalid format".to_owned())
2499                })?)
2500                .wrapping_add(1)
2501            }
2502            None => 0,
2503        };
2504
2505        tracing::debug!("Initialising crypto store generation at {}", gen);
2506
2507        self.inner
2508            .store
2509            .set_custom_value(Self::CURRENT_GENERATION_STORE_KEY, gen.to_le_bytes().to_vec())
2510            .await?;
2511
2512        *gen_guard = Some(gen);
2513
2514        Ok(())
2515    }
2516
2517    /// If needs be, update the local and on-disk crypto store generation.
2518    ///
2519    /// ## Requirements
2520    ///
2521    /// - This assumes that `initialize_crypto_store_generation` has been called
2522    ///   beforehand.
2523    /// - This requires that the crypto store lock has been acquired.
2524    ///
2525    /// # Arguments
2526    ///
2527    /// * `generation` - The in-memory generation counter (or rather, the
2528    ///   `Mutex` wrapping it). This defines the "expected" generation on entry,
2529    ///   and, if we determine an update is needed, is updated to hold the "new"
2530    ///   generation.
2531    ///
2532    /// # Returns
2533    ///
2534    /// A tuple containing:
2535    ///
2536    /// * A `bool`, set to `true` if another process has updated the generation
2537    ///   number in the `Store` since our expected value, and as such we've
2538    ///   incremented and updated it in the database. Otherwise, `false`.
2539    ///
2540    /// * The (possibly updated) generation counter.
2541    pub async fn maintain_crypto_store_generation(
2542        &'_ self,
2543        generation: &Mutex<Option<u64>>,
2544    ) -> StoreResult<(bool, u64)> {
2545        let mut gen_guard = generation.lock().await;
2546
2547        // The database value must be there:
2548        // - either we could initialize beforehand, thus write into the database,
2549        // - or we couldn't, and then another process was holding onto the database's
2550        //   lock, thus
2551        // has written a generation counter in there.
2552        let actual_gen = self
2553            .inner
2554            .store
2555            .get_custom_value(Self::CURRENT_GENERATION_STORE_KEY)
2556            .await?
2557            .ok_or_else(|| {
2558                CryptoStoreError::InvalidLockGeneration("counter missing in store".to_owned())
2559            })?;
2560
2561        let actual_gen =
2562            u64::from_le_bytes(actual_gen.try_into().map_err(|_| {
2563                CryptoStoreError::InvalidLockGeneration("invalid format".to_owned())
2564            })?);
2565
2566        let new_gen = match gen_guard.as_ref() {
2567            Some(expected_gen) => {
2568                if actual_gen == *expected_gen {
2569                    return Ok((false, actual_gen));
2570                }
2571                // Increment the biggest, and store it everywhere.
2572                actual_gen.max(*expected_gen).wrapping_add(1)
2573            }
2574            None => {
2575                // Some other process hold onto the lock when initializing, so we must reload.
2576                // Increment database value, and store it everywhere.
2577                actual_gen.wrapping_add(1)
2578            }
2579        };
2580
2581        tracing::debug!(
2582            "Crypto store generation mismatch: previously known was {:?}, actual is {:?}, next is {}",
2583            *gen_guard,
2584            actual_gen,
2585            new_gen
2586        );
2587
2588        // Update known value.
2589        *gen_guard = Some(new_gen);
2590
2591        // Update value in database.
2592        self.inner
2593            .store
2594            .set_custom_value(Self::CURRENT_GENERATION_STORE_KEY, new_gen.to_le_bytes().to_vec())
2595            .await?;
2596
2597        Ok((true, new_gen))
2598    }
2599
2600    /// Manage dehydrated devices.
2601    pub fn dehydrated_devices(&self) -> DehydratedDevices {
2602        DehydratedDevices { inner: self.to_owned() }
2603    }
2604
2605    /// Get the stored encryption settings for the given room, such as the
2606    /// encryption algorithm or whether to encrypt only for trusted devices.
2607    ///
2608    /// These settings can be modified via [`OlmMachine::set_room_settings`].
2609    pub async fn room_settings(&self, room_id: &RoomId) -> StoreResult<Option<RoomSettings>> {
2610        // There's not much to do here: it's just exposed for symmetry with
2611        // `set_room_settings`.
2612        self.inner.store.get_room_settings(room_id).await
2613    }
2614
2615    /// Store encryption settings for the given room.
2616    ///
2617    /// This method checks if the new settings are "safe" -- ie, that they do
2618    /// not represent a downgrade in encryption security from any previous
2619    /// settings. Attempts to downgrade security will result in a
2620    /// [`SetRoomSettingsError::EncryptionDowngrade`].
2621    ///
2622    /// If the settings are valid, they will be persisted to the crypto store.
2623    /// These settings are not used directly by this library, but the saved
2624    /// settings can be retrieved via [`OlmMachine::room_settings`].
2625    pub async fn set_room_settings(
2626        &self,
2627        room_id: &RoomId,
2628        new_settings: &RoomSettings,
2629    ) -> Result<(), SetRoomSettingsError> {
2630        let store = &self.inner.store;
2631
2632        // We want to make sure that we do not race against a second concurrent call to
2633        // `set_room_settings`. By way of an easy way to do so, we start a
2634        // StoreTransaction. There's no need to commit() it: we're just using it as a
2635        // lock guard.
2636        let _store_transaction = store.transaction().await;
2637
2638        let old_settings = store.get_room_settings(room_id).await?;
2639
2640        // We want to make sure that the change to the room settings does not represent
2641        // a downgrade in security. The [E2EE implementation guide] recommends:
2642        //
2643        //  > This flag should **not** be cleared if a later `m.room.encryption` event
2644        //  > changes the configuration.
2645        //
2646        // (However, it doesn't really address how to handle changes to the rotation
2647        // parameters, etc.) For now at least, we are very conservative here:
2648        // any new settings are rejected if they differ from the existing settings.
2649        // merit improvement (cf https://github.com/element-hq/element-meta/issues/69).
2650        //
2651        // [E2EE implementation guide]: https://matrix.org/docs/matrix-concepts/end-to-end-encryption/#handling-an-m-room-encryption-state-event
2652        if let Some(old_settings) = old_settings {
2653            if old_settings != *new_settings {
2654                return Err(SetRoomSettingsError::EncryptionDowngrade);
2655            } else {
2656                // nothing to do here
2657                return Ok(());
2658            }
2659        }
2660
2661        // Make sure that the new settings are valid
2662        match new_settings.algorithm {
2663            EventEncryptionAlgorithm::MegolmV1AesSha2 => (),
2664
2665            #[cfg(feature = "experimental-algorithms")]
2666            EventEncryptionAlgorithm::MegolmV2AesSha2 => (),
2667
2668            _ => {
2669                warn!(
2670                    ?room_id,
2671                    "Rejecting invalid encryption algorithm {}", new_settings.algorithm
2672                );
2673                return Err(SetRoomSettingsError::InvalidSettings);
2674            }
2675        }
2676
2677        // The new settings are acceptable, so let's save them.
2678        store
2679            .save_changes(Changes {
2680                room_settings: HashMap::from([(room_id.to_owned(), new_settings.clone())]),
2681                ..Default::default()
2682            })
2683            .await?;
2684
2685        Ok(())
2686    }
2687
2688    /// Returns whether this `OlmMachine` is the same another one.
2689    ///
2690    /// Useful for testing purposes only.
2691    #[cfg(any(feature = "testing", test))]
2692    pub fn same_as(&self, other: &OlmMachine) -> bool {
2693        Arc::ptr_eq(&self.inner, &other.inner)
2694    }
2695
2696    /// Testing purposes only.
2697    #[cfg(any(feature = "testing", test))]
2698    pub async fn uploaded_key_count(&self) -> Result<u64, CryptoStoreError> {
2699        let cache = self.inner.store.cache().await?;
2700        let account = cache.account().await?;
2701        Ok(account.uploaded_key_count())
2702    }
2703
2704    /// Returns the identity manager.
2705    #[cfg(test)]
2706    pub(crate) fn identity_manager(&self) -> &IdentityManager {
2707        &self.inner.identity_manager
2708    }
2709
2710    /// Returns a store key, only useful for testing purposes.
2711    #[cfg(test)]
2712    pub(crate) fn key_for_has_migrated_verification_latch() -> &'static str {
2713        Self::HAS_MIGRATED_VERIFICATION_LATCH
2714    }
2715}
2716
2717fn sender_data_to_verification_state(
2718    sender_data: SenderData,
2719    session_has_been_imported: bool,
2720) -> (VerificationState, Option<OwnedDeviceId>) {
2721    match sender_data {
2722        SenderData::UnknownDevice { owner_check_failed: false, .. } => {
2723            let device_link_problem = if session_has_been_imported {
2724                DeviceLinkProblem::InsecureSource
2725            } else {
2726                DeviceLinkProblem::MissingDevice
2727            };
2728
2729            (VerificationState::Unverified(VerificationLevel::None(device_link_problem)), None)
2730        }
2731        SenderData::UnknownDevice { owner_check_failed: true, .. } => (
2732            VerificationState::Unverified(VerificationLevel::None(
2733                DeviceLinkProblem::InsecureSource,
2734            )),
2735            None,
2736        ),
2737        SenderData::DeviceInfo { device_keys, .. } => (
2738            VerificationState::Unverified(VerificationLevel::UnsignedDevice),
2739            Some(device_keys.device_id),
2740        ),
2741        SenderData::VerificationViolation(KnownSenderData { device_id, .. }) => {
2742            (VerificationState::Unverified(VerificationLevel::VerificationViolation), device_id)
2743        }
2744        SenderData::SenderUnverified(KnownSenderData { device_id, .. }) => {
2745            (VerificationState::Unverified(VerificationLevel::UnverifiedIdentity), device_id)
2746        }
2747        SenderData::SenderVerified(KnownSenderData { device_id, .. }) => {
2748            (VerificationState::Verified, device_id)
2749        }
2750    }
2751}
2752
2753/// A set of requests to be executed when bootstrapping cross-signing using
2754/// [`OlmMachine::bootstrap_cross_signing`].
2755#[derive(Debug, Clone)]
2756pub struct CrossSigningBootstrapRequests {
2757    /// An optional request to upload a device key.
2758    ///
2759    /// Should be sent first, if present.
2760    ///
2761    /// If present, its result must be processed back with
2762    /// `OlmMachine::mark_request_as_sent`.
2763    pub upload_keys_req: Option<OutgoingRequest>,
2764
2765    /// Request to upload the cross-signing keys.
2766    ///
2767    /// Should be sent second.
2768    pub upload_signing_keys_req: UploadSigningKeysRequest,
2769
2770    /// Request to upload key signatures, including those for the cross-signing
2771    /// keys, and maybe some for the optional uploaded key too.
2772    ///
2773    /// Should be sent last.
2774    pub upload_signatures_req: UploadSignaturesRequest,
2775}
2776
2777/// Data contained from a sync response and that needs to be processed by the
2778/// OlmMachine.
2779#[derive(Debug)]
2780pub struct EncryptionSyncChanges<'a> {
2781    /// The list of to-device events received in the sync.
2782    pub to_device_events: Vec<Raw<AnyToDeviceEvent>>,
2783    /// The mapping of changed and left devices, per user, as returned in the
2784    /// sync response.
2785    pub changed_devices: &'a DeviceLists,
2786    /// The number of one time keys, as returned in the sync response.
2787    pub one_time_keys_counts: &'a BTreeMap<OneTimeKeyAlgorithm, UInt>,
2788    /// An optional list of fallback keys.
2789    pub unused_fallback_keys: Option<&'a [OneTimeKeyAlgorithm]>,
2790    /// A next-batch token obtained from a to-device sync query.
2791    pub next_batch_token: Option<String>,
2792}
2793
2794/// Convert a [`MegolmError`] into an [`UnableToDecryptInfo`] or a
2795/// [`CryptoStoreError`].
2796///
2797/// Most `MegolmError` codes are converted into a suitable
2798/// `UnableToDecryptInfo`. The exception is [`MegolmError::Store`], which
2799/// represents a problem with our datastore rather than with the message itself,
2800/// and is therefore returned as a `CryptoStoreError`.
2801fn megolm_error_to_utd_info(
2802    raw_event: &Raw<EncryptedEvent>,
2803    error: MegolmError,
2804) -> Result<UnableToDecryptInfo, CryptoStoreError> {
2805    use MegolmError::*;
2806    let reason = match error {
2807        EventError(_) => UnableToDecryptReason::MalformedEncryptedEvent,
2808        Decode(_) => UnableToDecryptReason::MalformedEncryptedEvent,
2809        MissingRoomKey(maybe_withheld) => {
2810            UnableToDecryptReason::MissingMegolmSession { withheld_code: maybe_withheld }
2811        }
2812        Decryption(DecryptionError::UnknownMessageIndex(_, _)) => {
2813            UnableToDecryptReason::UnknownMegolmMessageIndex
2814        }
2815        Decryption(_) => UnableToDecryptReason::MegolmDecryptionFailure,
2816        JsonError(_) => UnableToDecryptReason::PayloadDeserializationFailure,
2817        MismatchedIdentityKeys(_) => UnableToDecryptReason::MismatchedIdentityKeys,
2818        SenderIdentityNotTrusted(level) => UnableToDecryptReason::SenderIdentityNotTrusted(level),
2819
2820        // Pass through crypto store errors, which indicate a problem with our
2821        // application, rather than a UTD.
2822        Store(error) => Err(error)?,
2823    };
2824
2825    let session_id = raw_event.deserialize().ok().and_then(|ev| match ev.content.scheme {
2826        RoomEventEncryptionScheme::MegolmV1AesSha2(s) => Some(s.session_id),
2827        #[cfg(feature = "experimental-algorithms")]
2828        RoomEventEncryptionScheme::MegolmV2AesSha2(s) => Some(s.session_id),
2829        RoomEventEncryptionScheme::Unknown(_) => None,
2830    });
2831
2832    Ok(UnableToDecryptInfo { session_id, reason })
2833}
2834
2835#[cfg(test)]
2836pub(crate) mod test_helpers;
2837
2838#[cfg(test)]
2839pub(crate) mod tests;