Skip to main content

matrix_sdk_crypto/machine/
mod.rs

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