matrix_sdk_crypto/store/
mod.rs

1// Copyright 2020 The Matrix.org Foundation C.I.C.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Types and traits to implement the storage layer for the [`OlmMachine`]
16//!
17//! The storage layer for the [`OlmMachine`] can be customized using a trait.
18//! Implementing your own [`CryptoStore`]
19//!
20//! An in-memory only store is provided as well as an SQLite-based one,
21//! depending on your needs and targets a custom store may be implemented, e.g.
22//! for `wasm-unknown-unknown` an indexeddb store would be needed
23//!
24//! ```
25//! # use std::sync::Arc;
26//! # use matrix_sdk_crypto::{
27//! #     OlmMachine,
28//! #     store::MemoryStore,
29//! # };
30//! # use ruma::{device_id, user_id};
31//! # let user_id = user_id!("@example:localhost");
32//! # let device_id = device_id!("TEST");
33//! let store = Arc::new(MemoryStore::new());
34//!
35//! let machine = OlmMachine::with_store(user_id, device_id, store, None);
36//! ```
37//!
38//! [`OlmMachine`]: /matrix_sdk_crypto/struct.OlmMachine.html
39//! [`CryptoStore`]: trait.Cryptostore.html
40
41use std::{
42    collections::{BTreeMap, BTreeSet, HashMap, HashSet},
43    fmt::Debug,
44    ops::Deref,
45    pin::pin,
46    sync::{Arc, atomic::Ordering},
47    time::Duration,
48};
49
50use as_variant::as_variant;
51use futures_core::Stream;
52use futures_util::StreamExt;
53use itertools::{Either, Itertools};
54use ruma::{
55    DeviceId, OwnedDeviceId, OwnedUserId, RoomId, UserId, encryption::KeyUsage,
56    events::secret::request::SecretName,
57};
58use serde::{Serialize, de::DeserializeOwned};
59use thiserror::Error;
60use tokio::sync::{Mutex, Notify, OwnedRwLockWriteGuard, RwLock};
61use tokio_stream::wrappers::errors::BroadcastStreamRecvError;
62use tracing::{info, instrument, trace, warn};
63use types::{RoomKeyBundleInfo, StoredRoomKeyBundleData};
64use vodozemac::{Curve25519PublicKey, megolm::SessionOrdering};
65
66use self::types::{
67    Changes, CrossSigningKeyExport, DeviceChanges, DeviceUpdates, IdentityChanges, IdentityUpdates,
68    PendingChanges, RoomKeyInfo, RoomKeyWithheldInfo, UserKeyQueryResult,
69};
70use crate::{
71    CrossSigningStatus, OwnUserIdentityData, RoomKeyImportResult,
72    gossiping::GossippedSecret,
73    identities::{Device, DeviceData, UserDevices, UserIdentityData, user::UserIdentity},
74    olm::{
75        Account, ExportedRoomKey, InboundGroupSession, PrivateCrossSigningIdentity, SenderData,
76        Session, StaticAccountData,
77    },
78    store::types::RoomKeyWithheldEntry,
79    types::{
80        BackupSecrets, CrossSigningSecrets, MegolmBackupV1Curve25519AesSha2Secrets, RoomKeyExport,
81        SecretsBundle,
82    },
83    verification::VerificationMachine,
84};
85#[cfg(doc)]
86use crate::{backups::BackupMachine, identities::OwnUserIdentity};
87
88pub mod caches;
89mod crypto_store_wrapper;
90mod error;
91mod memorystore;
92mod traits;
93pub mod types;
94
95#[cfg(any(test, feature = "testing"))]
96#[macro_use]
97#[allow(missing_docs)]
98pub mod integration_tests;
99
100pub(crate) use crypto_store_wrapper::CryptoStoreWrapper;
101pub use error::{CryptoStoreError, Result};
102use matrix_sdk_common::{
103    cross_process_lock::{CrossProcessLock, CrossProcessLockGeneration},
104    deserialized_responses::WithheldCode,
105    timeout::timeout,
106};
107pub use memorystore::MemoryStore;
108pub use traits::{CryptoStore, DynCryptoStore, IntoCryptoStore};
109
110use self::caches::{SequenceNumber, StoreCache, StoreCacheGuard, UsersForKeyQuery};
111use crate::types::{
112    events::room_key_withheld::RoomKeyWithheldContent, room_history::RoomKeyBundle,
113};
114pub use crate::{
115    dehydrated_devices::DehydrationError,
116    gossiping::{GossipRequest, SecretInfo},
117};
118
119/// A wrapper for our CryptoStore trait object.
120///
121/// This is needed because we want to have a generic interface so we can
122/// store/restore objects that we can serialize. Since trait objects and
123/// generics don't mix let the CryptoStore store strings and this wrapper
124/// adds the generic interface on top.
125#[derive(Debug, Clone)]
126pub struct Store {
127    inner: Arc<StoreInner>,
128}
129
130#[derive(Debug, Default)]
131pub(crate) struct KeyQueryManager {
132    /// Record of the users that are waiting for a /keys/query.
133    users_for_key_query: Mutex<UsersForKeyQuery>,
134
135    /// Notifier that is triggered each time an update is received for a user.
136    users_for_key_query_notify: Notify,
137}
138
139impl KeyQueryManager {
140    pub async fn synced<'a>(&'a self, cache: &'a StoreCache) -> Result<SyncedKeyQueryManager<'a>> {
141        self.ensure_sync_tracked_users(cache).await?;
142        Ok(SyncedKeyQueryManager { cache, manager: self })
143    }
144
145    /// Load the list of users for whom we are tracking their device lists and
146    /// fill out our caches.
147    ///
148    /// This method ensures that we're only going to load the users from the
149    /// actual [`CryptoStore`] once, it will also make sure that any
150    /// concurrent calls to this method get deduplicated.
151    async fn ensure_sync_tracked_users(&self, cache: &StoreCache) -> Result<()> {
152        // Check if the users are loaded, and in that case do nothing.
153        let loaded = cache.loaded_tracked_users.read().await;
154        if *loaded {
155            return Ok(());
156        }
157
158        // Otherwise, we may load the users.
159        drop(loaded);
160        let mut loaded = cache.loaded_tracked_users.write().await;
161
162        // Check again if the users have been loaded, in case another call to this
163        // method loaded the tracked users between the time we tried to
164        // acquire the lock and the time we actually acquired the lock.
165        if *loaded {
166            return Ok(());
167        }
168
169        let tracked_users = cache.store.load_tracked_users().await?;
170
171        let mut query_users_lock = self.users_for_key_query.lock().await;
172        let mut tracked_users_cache = cache.tracked_users.write();
173        for user in tracked_users {
174            tracked_users_cache.insert(user.user_id.to_owned());
175
176            if user.dirty {
177                query_users_lock.insert_user(&user.user_id);
178            }
179        }
180
181        *loaded = true;
182
183        Ok(())
184    }
185
186    /// Wait for a `/keys/query` response to be received if one is expected for
187    /// the given user.
188    ///
189    /// If the given timeout elapses, the method will stop waiting and return
190    /// [`UserKeyQueryResult::TimeoutExpired`].
191    ///
192    /// Requires a [`StoreCacheGuard`] to make sure the users for which a key
193    /// query is pending are up to date, but doesn't hold on to it
194    /// thereafter: the lock is short-lived in this case.
195    pub async fn wait_if_user_key_query_pending(
196        &self,
197        cache: StoreCacheGuard,
198        timeout_duration: Duration,
199        user: &UserId,
200    ) -> Result<UserKeyQueryResult> {
201        {
202            // Drop the cache early, so we don't keep it while waiting (since writing the
203            // results requires to write in the cache, thus take another lock).
204            self.ensure_sync_tracked_users(&cache).await?;
205            drop(cache);
206        }
207
208        let mut users_for_key_query = self.users_for_key_query.lock().await;
209        let Some(waiter) = users_for_key_query.maybe_register_waiting_task(user) else {
210            return Ok(UserKeyQueryResult::WasNotPending);
211        };
212
213        let wait_for_completion = async {
214            while !waiter.completed.load(Ordering::Relaxed) {
215                // Register for being notified before releasing the mutex, so
216                // it's impossible to miss a wakeup between the last check for
217                // whether we should wait, and starting to wait.
218                let mut notified = pin!(self.users_for_key_query_notify.notified());
219                notified.as_mut().enable();
220                drop(users_for_key_query);
221
222                // Wait for a notification
223                notified.await;
224
225                // Reclaim the lock before checking the flag to avoid races
226                // when two notifications happen right after each other and the
227                // second one sets the flag we want to wait for.
228                users_for_key_query = self.users_for_key_query.lock().await;
229            }
230        };
231
232        match timeout(Box::pin(wait_for_completion), timeout_duration).await {
233            Err(_) => {
234                warn!(
235                    user_id = ?user,
236                    "The user has a pending `/keys/query` request which did \
237                    not finish yet, some devices might be missing."
238                );
239
240                Ok(UserKeyQueryResult::TimeoutExpired)
241            }
242            _ => Ok(UserKeyQueryResult::WasPending),
243        }
244    }
245}
246
247pub(crate) struct SyncedKeyQueryManager<'a> {
248    cache: &'a StoreCache,
249    manager: &'a KeyQueryManager,
250}
251
252impl SyncedKeyQueryManager<'_> {
253    /// Add entries to the list of users being tracked for device changes
254    ///
255    /// Any users not already on the list are flagged as awaiting a key query.
256    /// Users that were already in the list are unaffected.
257    pub async fn update_tracked_users(&self, users: impl Iterator<Item = &UserId>) -> Result<()> {
258        let mut store_updates = Vec::new();
259        let mut key_query_lock = self.manager.users_for_key_query.lock().await;
260
261        {
262            let mut tracked_users = self.cache.tracked_users.write();
263            for user_id in users {
264                if tracked_users.insert(user_id.to_owned()) {
265                    key_query_lock.insert_user(user_id);
266                    store_updates.push((user_id, true))
267                }
268            }
269        }
270
271        self.cache.store.save_tracked_users(&store_updates).await
272    }
273
274    /// Process notifications that users have changed devices.
275    ///
276    /// This is used to handle the list of device-list updates that is received
277    /// from the `/sync` response. Any users *whose device lists we are
278    /// tracking* are flagged as needing a key query. Users whose devices we
279    /// are not tracking are ignored.
280    pub async fn mark_tracked_users_as_changed(
281        &self,
282        users: impl Iterator<Item = &UserId>,
283    ) -> Result<()> {
284        let mut store_updates: Vec<(&UserId, bool)> = Vec::new();
285        let mut key_query_lock = self.manager.users_for_key_query.lock().await;
286
287        {
288            let tracked_users = &self.cache.tracked_users.read();
289            for user_id in users {
290                if tracked_users.contains(user_id) {
291                    key_query_lock.insert_user(user_id);
292                    store_updates.push((user_id, true));
293                }
294            }
295        }
296
297        self.cache.store.save_tracked_users(&store_updates).await
298    }
299
300    /// Flag that the given users devices are now up-to-date.
301    ///
302    /// This is called after processing the response to a /keys/query request.
303    /// Any users whose device lists we are tracking are removed from the
304    /// list of those pending a /keys/query.
305    pub async fn mark_tracked_users_as_up_to_date(
306        &self,
307        users: impl Iterator<Item = &UserId>,
308        sequence_number: SequenceNumber,
309    ) -> Result<()> {
310        let mut store_updates: Vec<(&UserId, bool)> = Vec::new();
311        let mut key_query_lock = self.manager.users_for_key_query.lock().await;
312
313        {
314            let tracked_users = self.cache.tracked_users.read();
315            for user_id in users {
316                if tracked_users.contains(user_id) {
317                    let clean = key_query_lock.maybe_remove_user(user_id, sequence_number);
318                    store_updates.push((user_id, !clean));
319                }
320            }
321        }
322
323        self.cache.store.save_tracked_users(&store_updates).await?;
324        // wake up any tasks that may have been waiting for updates
325        self.manager.users_for_key_query_notify.notify_waiters();
326
327        Ok(())
328    }
329
330    /// Get the set of users that has the outdate/dirty flag set for their list
331    /// of devices.
332    ///
333    /// This set should be included in a `/keys/query` request which will update
334    /// the device list.
335    ///
336    /// # Returns
337    ///
338    /// A pair `(users, sequence_number)`, where `users` is the list of users to
339    /// be queried, and `sequence_number` is the current sequence number,
340    /// which should be returned in `mark_tracked_users_as_up_to_date`.
341    pub async fn users_for_key_query(&self) -> (HashSet<OwnedUserId>, SequenceNumber) {
342        self.manager.users_for_key_query.lock().await.users_for_key_query()
343    }
344
345    /// See the docs for [`crate::OlmMachine::tracked_users()`].
346    pub fn tracked_users(&self) -> HashSet<OwnedUserId> {
347        self.cache.tracked_users.read().iter().cloned().collect()
348    }
349
350    /// Mark the given user as being tracked for device lists, and mark that it
351    /// has an outdated device list.
352    ///
353    /// This means that the user will be considered for a `/keys/query` request
354    /// next time [`Store::users_for_key_query()`] is called.
355    pub async fn mark_user_as_changed(&self, user: &UserId) -> Result<()> {
356        self.manager.users_for_key_query.lock().await.insert_user(user);
357        self.cache.tracked_users.write().insert(user.to_owned());
358
359        self.cache.store.save_tracked_users(&[(user, true)]).await
360    }
361}
362
363/// Convert the devices and vectors contained in the [`DeviceChanges`] into
364/// a [`DeviceUpdates`] struct.
365///
366/// The [`DeviceChanges`] will contain vectors of [`DeviceData`]s which
367/// we want to convert to a [`Device`].
368fn collect_device_updates(
369    verification_machine: VerificationMachine,
370    own_identity: Option<OwnUserIdentityData>,
371    identities: IdentityChanges,
372    devices: DeviceChanges,
373) -> DeviceUpdates {
374    let mut new: BTreeMap<_, BTreeMap<_, _>> = BTreeMap::new();
375    let mut changed: BTreeMap<_, BTreeMap<_, _>> = BTreeMap::new();
376
377    let (new_identities, changed_identities, unchanged_identities) = identities.into_maps();
378
379    let map_device = |device: DeviceData| {
380        let device_owner_identity = new_identities
381            .get(device.user_id())
382            .or_else(|| changed_identities.get(device.user_id()))
383            .or_else(|| unchanged_identities.get(device.user_id()))
384            .cloned();
385
386        Device {
387            inner: device,
388            verification_machine: verification_machine.to_owned(),
389            own_identity: own_identity.to_owned(),
390            device_owner_identity,
391        }
392    };
393
394    for device in devices.new {
395        let device = map_device(device);
396
397        new.entry(device.user_id().to_owned())
398            .or_default()
399            .insert(device.device_id().to_owned(), device);
400    }
401
402    for device in devices.changed {
403        let device = map_device(device);
404
405        changed
406            .entry(device.user_id().to_owned())
407            .or_default()
408            .insert(device.device_id().to_owned(), device.to_owned());
409    }
410
411    DeviceUpdates { new, changed }
412}
413
414/// A temporary transaction (that implies a write) to the underlying store.
415#[allow(missing_debug_implementations)]
416pub struct StoreTransaction {
417    store: Store,
418    changes: PendingChanges,
419    // TODO hold onto the cross-process crypto store lock + cache.
420    cache: OwnedRwLockWriteGuard<StoreCache>,
421}
422
423impl StoreTransaction {
424    /// Starts a new `StoreTransaction`.
425    async fn new(store: Store) -> Self {
426        let cache = store.inner.cache.clone();
427
428        Self { store, changes: PendingChanges::default(), cache: cache.clone().write_owned().await }
429    }
430
431    pub(crate) fn cache(&self) -> &StoreCache {
432        &self.cache
433    }
434
435    /// Returns a reference to the current `Store`.
436    pub fn store(&self) -> &Store {
437        &self.store
438    }
439
440    /// Gets a `Account` for update.
441    ///
442    /// Note: since it's guaranteed that one can't have both a
443    /// `StoreTransaction` and a `StoreCacheGuard` at runtime (since the
444    /// underlying `StoreCache` is guarded by a `RwLock` mutex), this ensures
445    /// that we can't have two copies of an `Account` alive at the same time.
446    pub async fn account(&mut self) -> Result<&mut Account> {
447        if self.changes.account.is_none() {
448            // Make sure the cache loaded the account.
449            let _ = self.cache.account().await?;
450            self.changes.account = self.cache.account.lock().await.take();
451        }
452        Ok(self.changes.account.as_mut().unwrap())
453    }
454
455    /// Commits all dirty fields to the store, and maintains the cache so it
456    /// reflects the current state of the database.
457    pub async fn commit(self) -> Result<()> {
458        if self.changes.is_empty() {
459            return Ok(());
460        }
461
462        // Save changes in the database.
463        let account = self.changes.account.as_ref().map(|acc| acc.deep_clone());
464
465        self.store.save_pending_changes(self.changes).await?;
466
467        // Make the cache coherent with the database.
468        if let Some(account) = account {
469            *self.cache.account.lock().await = Some(account);
470        }
471
472        Ok(())
473    }
474}
475
476#[derive(Debug)]
477struct StoreInner {
478    identity: Arc<Mutex<PrivateCrossSigningIdentity>>,
479    store: Arc<CryptoStoreWrapper>,
480
481    /// In-memory cache for the current crypto store.
482    ///
483    /// ⚠ Must remain private.
484    cache: Arc<RwLock<StoreCache>>,
485
486    verification_machine: VerificationMachine,
487
488    /// Static account data that never changes (and thus can be loaded once and
489    /// for all when creating the store).
490    static_account: StaticAccountData,
491}
492
493/// Error describing what went wrong when importing private cross signing keys
494/// or the key backup key.
495#[derive(Debug, Error)]
496pub enum SecretImportError {
497    /// The key that we tried to import was invalid.
498    #[error("Error while importing {name}: {error}")]
499    Key {
500        /// The name of the secret that was being imported.
501        name: SecretName,
502        /// The key error that occurred.
503        error: vodozemac::KeyError,
504    },
505    /// The public key of the imported private key doesn't match the public
506    /// key that was uploaded to the server.
507    #[error(
508        "Error while importing {name}: The public key of the imported private \
509            key doesn't match the public key that was uploaded to the server"
510    )]
511    MismatchedPublicKeys {
512        /// The name of the secret that was being imported.
513        name: SecretName,
514    },
515    /// The new version of the identity couldn't be stored.
516    #[error(transparent)]
517    Store(#[from] CryptoStoreError),
518}
519
520/// Error describing what went wrong when exporting a [`SecretsBundle`].
521///
522/// The [`SecretsBundle`] can only be exported if we have all cross-signing
523/// private keys in the store.
524#[derive(Debug, Error)]
525pub enum SecretsBundleExportError {
526    /// The store itself had an error.
527    #[error(transparent)]
528    Store(#[from] CryptoStoreError),
529    /// We're missing one or multiple cross-signing keys.
530    #[error("The store is missing one or multiple cross-signing keys")]
531    MissingCrossSigningKey(KeyUsage),
532    /// We're missing all cross-signing keys.
533    #[error("The store doesn't contain any cross-signing keys")]
534    MissingCrossSigningKeys,
535    /// We have a backup key stored, but we don't know the version of the
536    /// backup.
537    #[error("The store contains a backup key, but no backup version")]
538    MissingBackupVersion,
539}
540
541impl Store {
542    /// Create a new Store.
543    pub(crate) fn new(
544        account: StaticAccountData,
545        identity: Arc<Mutex<PrivateCrossSigningIdentity>>,
546        store: Arc<CryptoStoreWrapper>,
547        verification_machine: VerificationMachine,
548    ) -> Self {
549        Self {
550            inner: Arc::new(StoreInner {
551                static_account: account,
552                identity,
553                store: store.clone(),
554                verification_machine,
555                cache: Arc::new(RwLock::new(StoreCache {
556                    store,
557                    tracked_users: Default::default(),
558                    loaded_tracked_users: Default::default(),
559                    account: Default::default(),
560                })),
561            }),
562        }
563    }
564
565    /// UserId associated with this store
566    pub(crate) fn user_id(&self) -> &UserId {
567        &self.inner.static_account.user_id
568    }
569
570    /// DeviceId associated with this store
571    pub(crate) fn device_id(&self) -> &DeviceId {
572        self.inner.verification_machine.own_device_id()
573    }
574
575    /// The static data for the account associated with this store.
576    pub(crate) fn static_account(&self) -> &StaticAccountData {
577        &self.inner.static_account
578    }
579
580    pub(crate) async fn cache(&self) -> Result<StoreCacheGuard> {
581        // TODO: (bnjbvr, #2624) If configured with a cross-process lock:
582        // - try to take the lock,
583        // - if acquired, look if another process touched the underlying storage,
584        // - if yes, reload everything; if no, return current cache
585        Ok(StoreCacheGuard { cache: self.inner.cache.clone().read_owned().await })
586    }
587
588    pub(crate) async fn transaction(&self) -> StoreTransaction {
589        StoreTransaction::new(self.clone()).await
590    }
591
592    // Note: bnjbvr lost against borrowck here. Ideally, the `F` parameter would
593    // take a `&StoreTransaction`, but callers didn't quite like that.
594    pub(crate) async fn with_transaction<
595        T,
596        Fut: Future<Output = Result<(StoreTransaction, T), crate::OlmError>>,
597        F: FnOnce(StoreTransaction) -> Fut,
598    >(
599        &self,
600        func: F,
601    ) -> Result<T, crate::OlmError> {
602        let tr = self.transaction().await;
603        let (tr, res) = func(tr).await?;
604        tr.commit().await?;
605        Ok(res)
606    }
607
608    #[cfg(test)]
609    /// test helper to reset the cross signing identity
610    pub(crate) async fn reset_cross_signing_identity(&self) {
611        self.inner.identity.lock().await.reset();
612    }
613
614    /// PrivateCrossSigningIdentity associated with this store
615    pub(crate) fn private_identity(&self) -> Arc<Mutex<PrivateCrossSigningIdentity>> {
616        self.inner.identity.clone()
617    }
618
619    /// Save the given Sessions to the store
620    pub(crate) async fn save_sessions(&self, sessions: &[Session]) -> Result<()> {
621        let changes = Changes { sessions: sessions.to_vec(), ..Default::default() };
622
623        self.save_changes(changes).await
624    }
625
626    pub(crate) async fn get_sessions(
627        &self,
628        sender_key: &str,
629    ) -> Result<Option<Arc<Mutex<Vec<Session>>>>> {
630        self.inner.store.get_sessions(sender_key).await
631    }
632
633    pub(crate) async fn save_changes(&self, changes: Changes) -> Result<()> {
634        self.inner.store.save_changes(changes).await
635    }
636
637    /// Given an `InboundGroupSession` which we have just received, see if we
638    /// have a matching session already in the store, and determine how to
639    /// handle it.
640    ///
641    /// If the store already has everything we can gather from the new session,
642    /// returns `None`. Otherwise, returns a merged session which should be
643    /// persisted to the store.
644    pub(crate) async fn merge_received_group_session(
645        &self,
646        session: InboundGroupSession,
647    ) -> Result<Option<InboundGroupSession>> {
648        let old_session = self
649            .inner
650            .store
651            .get_inbound_group_session(session.room_id(), session.session_id())
652            .await?;
653
654        // If there is no old session, just use the new session.
655        let Some(old_session) = old_session else {
656            info!("Received a new megolm room key");
657            return Ok(Some(session));
658        };
659
660        let index_comparison = session.compare_ratchet(&old_session).await;
661        let trust_level_comparison =
662            session.sender_data.compare_trust_level(&old_session.sender_data);
663
664        let result = match (index_comparison, trust_level_comparison) {
665            (SessionOrdering::Unconnected, _) => {
666                // If this happens, it means that we have two sessions purporting to have the
667                // same session id, but where the ratchets do not match up.
668                // In other words, someone is playing silly buggers.
669                warn!(
670                    "Received a group session with an ratchet that does not connect to the one in the store, discarding"
671                );
672                None
673            }
674
675            (SessionOrdering::Better, std::cmp::Ordering::Greater)
676            | (SessionOrdering::Better, std::cmp::Ordering::Equal)
677            | (SessionOrdering::Equal, std::cmp::Ordering::Greater) => {
678                // The new session is unambiguously better than what we have in the store.
679                info!(
680                    ?index_comparison,
681                    ?trust_level_comparison,
682                    "Received a megolm room key that we have a worse version of, merging"
683                );
684                Some(session)
685            }
686
687            (SessionOrdering::Worse, std::cmp::Ordering::Less)
688            | (SessionOrdering::Worse, std::cmp::Ordering::Equal)
689            | (SessionOrdering::Equal, std::cmp::Ordering::Less) => {
690                // The new session is unambiguously worse than the one we have in the store.
691                warn!(
692                    ?index_comparison,
693                    ?trust_level_comparison,
694                    "Received a megolm room key that we already have a better version \
695                     of, discarding"
696                );
697                None
698            }
699
700            (SessionOrdering::Equal, std::cmp::Ordering::Equal) => {
701                // The new session is the same as what we have.
702                info!("Received a megolm room key that we already have, discarding");
703                None
704            }
705
706            (SessionOrdering::Better, std::cmp::Ordering::Less) => {
707                // We need to take the ratchet from the new session, and the
708                // sender data from the old session.
709                info!("Upgrading a previously-received megolm session with new ratchet");
710                let result = old_session.with_ratchet(&session);
711                // We'll need to back it up again.
712                result.reset_backup_state();
713                Some(result)
714            }
715
716            (SessionOrdering::Worse, std::cmp::Ordering::Greater) => {
717                // We need to take the ratchet from the old session, and the
718                // sender data from the new session.
719                info!("Upgrading a previously-received megolm session with new sender data");
720                Some(session.with_ratchet(&old_session))
721            }
722        };
723
724        Ok(result)
725    }
726
727    #[cfg(test)]
728    /// Testing helper to allow to save only a set of devices
729    pub(crate) async fn save_device_data(&self, devices: &[DeviceData]) -> Result<()> {
730        use types::DeviceChanges;
731
732        let changes = Changes {
733            devices: DeviceChanges { changed: devices.to_vec(), ..Default::default() },
734            ..Default::default()
735        };
736
737        self.save_changes(changes).await
738    }
739
740    /// Convenience helper to persist an array of [`InboundGroupSession`]s.
741    pub(crate) async fn save_inbound_group_sessions(
742        &self,
743        sessions: &[InboundGroupSession],
744    ) -> Result<()> {
745        let changes = Changes { inbound_group_sessions: sessions.to_vec(), ..Default::default() };
746
747        self.save_changes(changes).await
748    }
749
750    /// Get the display name of our own device.
751    pub(crate) async fn device_display_name(&self) -> Result<Option<String>, CryptoStoreError> {
752        Ok(self
753            .inner
754            .store
755            .get_device(self.user_id(), self.device_id())
756            .await?
757            .and_then(|d| d.display_name().map(|d| d.to_owned())))
758    }
759
760    /// Get the device data for the given [`UserId`] and [`DeviceId`].
761    ///
762    /// *Note*: This method will include our own device which is always present
763    /// in the store.
764    pub(crate) async fn get_device_data(
765        &self,
766        user_id: &UserId,
767        device_id: &DeviceId,
768    ) -> Result<Option<DeviceData>> {
769        self.inner.store.get_device(user_id, device_id).await
770    }
771
772    /// Get the device data for the given [`UserId`] and [`DeviceId`].
773    ///
774    /// *Note*: This method will **not** include our own device.
775    ///
776    /// Use this method if you need a list of recipients for a given user, since
777    /// we don't want to encrypt for our own device, otherwise take a look at
778    /// the [`Store::get_device_data_for_user`] method.
779    pub(crate) async fn get_device_data_for_user_filtered(
780        &self,
781        user_id: &UserId,
782    ) -> Result<HashMap<OwnedDeviceId, DeviceData>> {
783        self.inner.store.get_user_devices(user_id).await.map(|mut d| {
784            if user_id == self.user_id() {
785                d.remove(self.device_id());
786            }
787            d
788        })
789    }
790
791    /// Get the [`DeviceData`] for all the devices a user has.
792    ///
793    /// *Note*: This method will include our own device which is always present
794    /// in the store.
795    ///
796    /// Use this method if you need to operate on or update all devices of a
797    /// user, otherwise take a look at the
798    /// [`Store::get_device_data_for_user_filtered`] method.
799    pub(crate) async fn get_device_data_for_user(
800        &self,
801        user_id: &UserId,
802    ) -> Result<HashMap<OwnedDeviceId, DeviceData>> {
803        self.inner.store.get_user_devices(user_id).await
804    }
805
806    /// Get a [`Device`] for the given user with the given
807    /// [`Curve25519PublicKey`] key.
808    ///
809    /// *Note*: This method will include our own device which is always present
810    /// in the store.
811    pub(crate) async fn get_device_from_curve_key(
812        &self,
813        user_id: &UserId,
814        curve_key: Curve25519PublicKey,
815    ) -> Result<Option<Device>> {
816        self.get_user_devices(user_id)
817            .await
818            .map(|d| d.devices().find(|d| d.curve25519_key() == Some(curve_key)))
819    }
820
821    /// Get all devices associated with the given [`UserId`].
822    ///
823    /// This method is more expensive than the
824    /// [`Store::get_device_data_for_user`] method, since a [`Device`]
825    /// requires the [`OwnUserIdentityData`] and the [`UserIdentityData`] of the
826    /// device owner to be fetched from the store as well.
827    ///
828    /// *Note*: This method will include our own device which is always present
829    /// in the store.
830    pub(crate) async fn get_user_devices(&self, user_id: &UserId) -> Result<UserDevices> {
831        let devices = self.get_device_data_for_user(user_id).await?;
832
833        let own_identity = self
834            .inner
835            .store
836            .get_user_identity(self.user_id())
837            .await?
838            .and_then(|i| i.own().cloned());
839        let device_owner_identity = self.inner.store.get_user_identity(user_id).await?;
840
841        Ok(UserDevices {
842            inner: devices,
843            verification_machine: self.inner.verification_machine.clone(),
844            own_identity,
845            device_owner_identity,
846        })
847    }
848
849    /// Get a [`Device`] for the given user with the given [`DeviceId`].
850    ///
851    /// This method is more expensive than the [`Store::get_device_data`] method
852    /// since a [`Device`] requires the [`OwnUserIdentityData`] and the
853    /// [`UserIdentityData`] of the device owner to be fetched from the
854    /// store as well.
855    ///
856    /// *Note*: This method will include our own device which is always present
857    /// in the store.
858    pub(crate) async fn get_device(
859        &self,
860        user_id: &UserId,
861        device_id: &DeviceId,
862    ) -> Result<Option<Device>> {
863        if let Some(device_data) = self.inner.store.get_device(user_id, device_id).await? {
864            Ok(Some(self.wrap_device_data(device_data).await?))
865        } else {
866            Ok(None)
867        }
868    }
869
870    /// Create a new device using the supplied [`DeviceData`]. Normally we would
871    /// call [`Self::get_device`] to find an existing device inside this
872    /// store. Only call this if you have some existing DeviceData and want
873    /// to wrap it with the extra information provided by a [`Device`].
874    pub(crate) async fn wrap_device_data(&self, device_data: DeviceData) -> Result<Device> {
875        let own_identity = self
876            .inner
877            .store
878            .get_user_identity(self.user_id())
879            .await?
880            .and_then(|i| i.own().cloned());
881
882        let device_owner_identity =
883            self.inner.store.get_user_identity(device_data.user_id()).await?;
884
885        Ok(Device {
886            inner: device_data,
887            verification_machine: self.inner.verification_machine.clone(),
888            own_identity,
889            device_owner_identity,
890        })
891    }
892
893    ///  Get the Identity of `user_id`
894    pub(crate) async fn get_identity(&self, user_id: &UserId) -> Result<Option<UserIdentity>> {
895        let own_identity = self
896            .inner
897            .store
898            .get_user_identity(self.user_id())
899            .await?
900            .and_then(as_variant!(UserIdentityData::Own));
901
902        Ok(self.inner.store.get_user_identity(user_id).await?.map(|i| {
903            UserIdentity::new(
904                self.clone(),
905                i,
906                self.inner.verification_machine.to_owned(),
907                own_identity,
908            )
909        }))
910    }
911
912    /// Try to export the secret with the given secret name.
913    ///
914    /// The exported secret will be encoded as unpadded base64. Returns `Null`
915    /// if the secret can't be found.
916    ///
917    /// # Arguments
918    ///
919    /// * `secret_name` - The name of the secret that should be exported.
920    pub async fn export_secret(
921        &self,
922        secret_name: &SecretName,
923    ) -> Result<Option<String>, CryptoStoreError> {
924        Ok(match secret_name {
925            SecretName::CrossSigningMasterKey
926            | SecretName::CrossSigningUserSigningKey
927            | SecretName::CrossSigningSelfSigningKey => {
928                self.inner.identity.lock().await.export_secret(secret_name).await
929            }
930            SecretName::RecoveryKey => {
931                if let Some(key) = self.load_backup_keys().await?.decryption_key {
932                    let exported = key.to_base64();
933                    Some(exported)
934                } else {
935                    None
936                }
937            }
938            name => {
939                warn!(secret = ?name, "Unknown secret was requested");
940                None
941            }
942        })
943    }
944
945    /// Export all the private cross signing keys we have.
946    ///
947    /// The export will contain the seed for the ed25519 keys as a unpadded
948    /// base64 encoded string.
949    ///
950    /// This method returns `None` if we don't have any private cross signing
951    /// keys.
952    pub async fn export_cross_signing_keys(
953        &self,
954    ) -> Result<Option<CrossSigningKeyExport>, CryptoStoreError> {
955        let master_key = self.export_secret(&SecretName::CrossSigningMasterKey).await?;
956        let self_signing_key = self.export_secret(&SecretName::CrossSigningSelfSigningKey).await?;
957        let user_signing_key = self.export_secret(&SecretName::CrossSigningUserSigningKey).await?;
958
959        Ok(if master_key.is_none() && self_signing_key.is_none() && user_signing_key.is_none() {
960            None
961        } else {
962            Some(CrossSigningKeyExport { master_key, self_signing_key, user_signing_key })
963        })
964    }
965
966    /// Import our private cross signing keys.
967    ///
968    /// The export needs to contain the seed for the Ed25519 keys as an unpadded
969    /// base64 encoded string.
970    pub async fn import_cross_signing_keys(
971        &self,
972        export: CrossSigningKeyExport,
973    ) -> Result<CrossSigningStatus, SecretImportError> {
974        if let Some(public_identity) =
975            self.get_identity(self.user_id()).await?.and_then(|i| i.own())
976        {
977            let identity = self.inner.identity.lock().await;
978
979            identity
980                .import_secrets(
981                    public_identity.to_owned(),
982                    export.master_key.as_deref(),
983                    export.self_signing_key.as_deref(),
984                    export.user_signing_key.as_deref(),
985                )
986                .await?;
987
988            let status = identity.status().await;
989
990            let diff = identity.get_public_identity_diff(&public_identity.inner).await;
991
992            let mut changes =
993                Changes { private_identity: Some(identity.clone()), ..Default::default() };
994
995            if diff.none_differ() {
996                public_identity.mark_as_verified();
997                changes.identities.changed.push(UserIdentityData::Own(public_identity.inner));
998            }
999
1000            info!(?status, "Successfully imported the private cross-signing keys");
1001
1002            self.save_changes(changes).await?;
1003        } else {
1004            warn!(
1005                "No public identity found while importing cross-signing keys, \
1006                 a /keys/query needs to be done"
1007            );
1008        }
1009
1010        Ok(self.inner.identity.lock().await.status().await)
1011    }
1012
1013    /// Export all the secrets we have in the store into a [`SecretsBundle`].
1014    ///
1015    /// This method will export all the private cross-signing keys and, if
1016    /// available, the private part of a backup key and its accompanying
1017    /// version.
1018    ///
1019    /// The method will fail if we don't have all three private cross-signing
1020    /// keys available.
1021    ///
1022    /// **Warning**: Only export this and share it with a trusted recipient,
1023    /// i.e. if an existing device is sharing this with a new device.
1024    pub async fn export_secrets_bundle(&self) -> Result<SecretsBundle, SecretsBundleExportError> {
1025        let Some(cross_signing) = self.export_cross_signing_keys().await? else {
1026            return Err(SecretsBundleExportError::MissingCrossSigningKeys);
1027        };
1028
1029        let Some(master_key) = cross_signing.master_key.clone() else {
1030            return Err(SecretsBundleExportError::MissingCrossSigningKey(KeyUsage::Master));
1031        };
1032
1033        let Some(user_signing_key) = cross_signing.user_signing_key.clone() else {
1034            return Err(SecretsBundleExportError::MissingCrossSigningKey(KeyUsage::UserSigning));
1035        };
1036
1037        let Some(self_signing_key) = cross_signing.self_signing_key.clone() else {
1038            return Err(SecretsBundleExportError::MissingCrossSigningKey(KeyUsage::SelfSigning));
1039        };
1040
1041        let backup_keys = self.load_backup_keys().await?;
1042
1043        let backup = if let Some(key) = backup_keys.decryption_key {
1044            if let Some(backup_version) = backup_keys.backup_version {
1045                Some(BackupSecrets::MegolmBackupV1Curve25519AesSha2(
1046                    MegolmBackupV1Curve25519AesSha2Secrets { key, backup_version },
1047                ))
1048            } else {
1049                return Err(SecretsBundleExportError::MissingBackupVersion);
1050            }
1051        } else {
1052            None
1053        };
1054
1055        Ok(SecretsBundle {
1056            cross_signing: CrossSigningSecrets { master_key, user_signing_key, self_signing_key },
1057            backup,
1058        })
1059    }
1060
1061    /// Import and persists secrets from a [`SecretsBundle`].
1062    ///
1063    /// This method will import all the private cross-signing keys and, if
1064    /// available, the private part of a backup key and its accompanying
1065    /// version into the store.
1066    ///
1067    /// **Warning**: Only import this from a trusted source, i.e. if an existing
1068    /// device is sharing this with a new device. The imported cross-signing
1069    /// keys will create a [`OwnUserIdentity`] and mark it as verified.
1070    ///
1071    /// The backup key will be persisted in the store and can be enabled using
1072    /// the [`BackupMachine`].
1073    pub async fn import_secrets_bundle(
1074        &self,
1075        bundle: &SecretsBundle,
1076    ) -> Result<(), SecretImportError> {
1077        let mut changes = Changes::default();
1078
1079        if let Some(backup_bundle) = &bundle.backup {
1080            match backup_bundle {
1081                BackupSecrets::MegolmBackupV1Curve25519AesSha2(bundle) => {
1082                    changes.backup_decryption_key = Some(bundle.key.clone());
1083                    changes.backup_version = Some(bundle.backup_version.clone());
1084                }
1085            }
1086        }
1087
1088        let identity = self.inner.identity.lock().await;
1089
1090        identity
1091            .import_secrets_unchecked(
1092                Some(&bundle.cross_signing.master_key),
1093                Some(&bundle.cross_signing.self_signing_key),
1094                Some(&bundle.cross_signing.user_signing_key),
1095            )
1096            .await?;
1097
1098        let public_identity = identity.to_public_identity().await.expect(
1099            "We should be able to create a new public identity since we just imported \
1100             all the private cross-signing keys",
1101        );
1102
1103        changes.private_identity = Some(identity.clone());
1104        changes.identities.new.push(UserIdentityData::Own(public_identity));
1105
1106        Ok(self.save_changes(changes).await?)
1107    }
1108
1109    /// Import the given `secret` named `secret_name` into the keystore.
1110    pub async fn import_secret(&self, secret: &GossippedSecret) -> Result<(), SecretImportError> {
1111        match &secret.secret_name {
1112            SecretName::CrossSigningMasterKey
1113            | SecretName::CrossSigningUserSigningKey
1114            | SecretName::CrossSigningSelfSigningKey => {
1115                if let Some(public_identity) =
1116                    self.get_identity(self.user_id()).await?.and_then(|i| i.own())
1117                {
1118                    let identity = self.inner.identity.lock().await;
1119
1120                    identity
1121                        .import_secret(
1122                            public_identity,
1123                            &secret.secret_name,
1124                            &secret.event.content.secret,
1125                        )
1126                        .await?;
1127                    info!(
1128                        secret_name = ?secret.secret_name,
1129                        "Successfully imported a private cross signing key"
1130                    );
1131
1132                    let changes =
1133                        Changes { private_identity: Some(identity.clone()), ..Default::default() };
1134
1135                    self.save_changes(changes).await?;
1136                }
1137            }
1138            SecretName::RecoveryKey => {
1139                // We don't import the decryption key here since we'll want to
1140                // check if the public key matches to the latest version on the
1141                // server. We instead put the secret into a secret inbox where
1142                // it will stay until it either gets overwritten
1143                // or the user accepts the secret.
1144            }
1145            name => {
1146                warn!(secret = ?name, "Tried to import an unknown secret");
1147            }
1148        }
1149
1150        Ok(())
1151    }
1152
1153    /// Check whether there is a global flag to only encrypt messages for
1154    /// trusted devices or for everyone.
1155    pub async fn get_only_allow_trusted_devices(&self) -> Result<bool> {
1156        let value = self.get_value("only_allow_trusted_devices").await?.unwrap_or_default();
1157        Ok(value)
1158    }
1159
1160    /// Set global flag whether to encrypt messages for untrusted devices, or
1161    /// whether they should be excluded from the conversation.
1162    pub async fn set_only_allow_trusted_devices(
1163        &self,
1164        block_untrusted_devices: bool,
1165    ) -> Result<()> {
1166        self.set_value("only_allow_trusted_devices", &block_untrusted_devices).await
1167    }
1168
1169    /// Get custom stored value associated with a key
1170    pub async fn get_value<T: DeserializeOwned>(&self, key: &str) -> Result<Option<T>> {
1171        let Some(value) = self.get_custom_value(key).await? else {
1172            return Ok(None);
1173        };
1174        let deserialized = self.deserialize_value(&value)?;
1175        Ok(Some(deserialized))
1176    }
1177
1178    /// Store custom value associated with a key
1179    pub async fn set_value(&self, key: &str, value: &impl Serialize) -> Result<()> {
1180        let serialized = self.serialize_value(value)?;
1181        self.set_custom_value(key, serialized).await?;
1182        Ok(())
1183    }
1184
1185    fn serialize_value(&self, value: &impl Serialize) -> Result<Vec<u8>> {
1186        let serialized =
1187            rmp_serde::to_vec_named(value).map_err(|x| CryptoStoreError::Backend(x.into()))?;
1188        Ok(serialized)
1189    }
1190
1191    fn deserialize_value<T: DeserializeOwned>(&self, value: &[u8]) -> Result<T> {
1192        let deserialized =
1193            rmp_serde::from_slice(value).map_err(|e| CryptoStoreError::Backend(e.into()))?;
1194        Ok(deserialized)
1195    }
1196
1197    /// Receive notifications of room keys being received as a [`Stream`].
1198    ///
1199    /// Each time a room key is updated in any way, an update will be sent to
1200    /// the stream. Updates that happen at the same time are batched into a
1201    /// [`Vec`].
1202    ///
1203    /// If the reader of the stream lags too far behind an error will be sent to
1204    /// the reader.
1205    ///
1206    /// The stream will terminate once all references to the underlying
1207    /// `CryptoStoreWrapper` are dropped.
1208    pub fn room_keys_received_stream(
1209        &self,
1210    ) -> impl Stream<Item = Result<Vec<RoomKeyInfo>, BroadcastStreamRecvError>> + use<> {
1211        self.inner.store.room_keys_received_stream()
1212    }
1213
1214    /// Receive notifications of received `m.room_key.withheld` messages.
1215    ///
1216    /// Each time an `m.room_key.withheld` is received and stored, an update
1217    /// will be sent to the stream. Updates that happen at the same time are
1218    /// batched into a [`Vec`].
1219    ///
1220    /// If the reader of the stream lags too far behind, a warning will be
1221    /// logged and items will be dropped.
1222    pub fn room_keys_withheld_received_stream(
1223        &self,
1224    ) -> impl Stream<Item = Vec<RoomKeyWithheldInfo>> + use<> {
1225        self.inner.store.room_keys_withheld_received_stream()
1226    }
1227
1228    /// Returns a stream of user identity updates, allowing users to listen for
1229    /// notifications about new or changed user identities.
1230    ///
1231    /// The stream produced by this method emits updates whenever a new user
1232    /// identity is discovered or when an existing identities information is
1233    /// changed. Users can subscribe to this stream and receive updates in
1234    /// real-time.
1235    ///
1236    /// Caution: the returned stream will never terminate, and it holds a
1237    /// reference to the [`CryptoStore`]. Listeners should be careful to avoid
1238    /// resource leaks.
1239    ///
1240    /// # Examples
1241    ///
1242    /// ```no_run
1243    /// # use matrix_sdk_crypto::OlmMachine;
1244    /// # use ruma::{device_id, user_id};
1245    /// # use futures_util::{pin_mut, StreamExt};
1246    /// # let machine: OlmMachine = unimplemented!();
1247    /// # futures_executor::block_on(async {
1248    /// let identities_stream = machine.store().user_identities_stream();
1249    /// pin_mut!(identities_stream);
1250    ///
1251    /// for identity_updates in identities_stream.next().await {
1252    ///     for (_, identity) in identity_updates.new {
1253    ///         println!("A new identity has been added {}", identity.user_id());
1254    ///     }
1255    /// }
1256    /// # });
1257    /// ```
1258    pub fn user_identities_stream(&self) -> impl Stream<Item = IdentityUpdates> + use<> {
1259        let verification_machine = self.inner.verification_machine.to_owned();
1260
1261        let this = self.clone();
1262        self.inner.store.identities_stream().map(move |(own_identity, identities, _)| {
1263            let (new_identities, changed_identities, unchanged_identities) = identities.into_maps();
1264
1265            let map_identity = |(user_id, identity)| {
1266                (
1267                    user_id,
1268                    UserIdentity::new(
1269                        this.clone(),
1270                        identity,
1271                        verification_machine.to_owned(),
1272                        own_identity.to_owned(),
1273                    ),
1274                )
1275            };
1276
1277            let new = new_identities.into_iter().map(map_identity).collect();
1278            let changed = changed_identities.into_iter().map(map_identity).collect();
1279            let unchanged = unchanged_identities.into_iter().map(map_identity).collect();
1280
1281            IdentityUpdates { new, changed, unchanged }
1282        })
1283    }
1284
1285    /// Returns a stream of device updates, allowing users to listen for
1286    /// notifications about new or changed devices.
1287    ///
1288    /// The stream produced by this method emits updates whenever a new device
1289    /// is discovered or when an existing device's information is changed. Users
1290    /// can subscribe to this stream and receive updates in real-time.
1291    ///
1292    /// Caution: the returned stream will never terminate, and it holds a
1293    /// reference to the [`CryptoStore`]. Listeners should be careful to avoid
1294    /// resource leaks.
1295    ///
1296    /// # Examples
1297    ///
1298    /// ```no_run
1299    /// # use matrix_sdk_crypto::OlmMachine;
1300    /// # use ruma::{device_id, user_id};
1301    /// # use futures_util::{pin_mut, StreamExt};
1302    /// # let machine: OlmMachine = unimplemented!();
1303    /// # futures_executor::block_on(async {
1304    /// let devices_stream = machine.store().devices_stream();
1305    /// pin_mut!(devices_stream);
1306    ///
1307    /// for device_updates in devices_stream.next().await {
1308    ///     if let Some(user_devices) = device_updates.new.get(machine.user_id()) {
1309    ///         for device in user_devices.values() {
1310    ///             println!("A new device has been added {}", device.device_id());
1311    ///         }
1312    ///     }
1313    /// }
1314    /// # });
1315    /// ```
1316    pub fn devices_stream(&self) -> impl Stream<Item = DeviceUpdates> + use<> {
1317        let verification_machine = self.inner.verification_machine.to_owned();
1318
1319        self.inner.store.identities_stream().map(move |(own_identity, identities, devices)| {
1320            collect_device_updates(
1321                verification_machine.to_owned(),
1322                own_identity,
1323                identities,
1324                devices,
1325            )
1326        })
1327    }
1328
1329    /// Returns a [`Stream`] of user identity and device updates
1330    ///
1331    /// The stream returned by this method returns the same data as
1332    /// [`Store::user_identities_stream`] and [`Store::devices_stream`] but does
1333    /// not include references to the `VerificationMachine`. It is therefore a
1334    /// lower-level view on that data.
1335    ///
1336    /// The stream will terminate once all references to the underlying
1337    /// `CryptoStoreWrapper` are dropped.
1338    pub fn identities_stream_raw(
1339        &self,
1340    ) -> impl Stream<Item = (IdentityChanges, DeviceChanges)> + use<> {
1341        self.inner.store.identities_stream().map(|(_, identities, devices)| (identities, devices))
1342    }
1343
1344    /// Creates a [`CrossProcessLock`] for this store, that will contain the
1345    /// given key and value when hold.
1346    pub fn create_store_lock(
1347        &self,
1348        lock_key: String,
1349        lock_value: String,
1350    ) -> CrossProcessLock<LockableCryptoStore> {
1351        self.inner.store.create_store_lock(lock_key, lock_value)
1352    }
1353
1354    /// Receive notifications of gossipped secrets being received and stored in
1355    /// the secret inbox as a [`Stream`].
1356    ///
1357    /// The gossipped secrets are received using the `m.secret.send` event type
1358    /// and are guaranteed to have been received over a 1-to-1 Olm
1359    /// [`Session`] from a verified [`Device`].
1360    ///
1361    /// The [`GossippedSecret`] can also be later found in the secret inbox and
1362    /// retrieved using the [`CryptoStore::get_secrets_from_inbox()`] method.
1363    ///
1364    /// After a suitable secret of a certain type has been found it can be
1365    /// removed from the store
1366    /// using the [`CryptoStore::delete_secrets_from_inbox()`] method.
1367    ///
1368    /// The only secret this will currently broadcast is the
1369    /// `m.megolm_backup.v1`.
1370    ///
1371    /// If the reader of the stream lags too far behind, a warning will be
1372    /// logged and items will be dropped.
1373    ///
1374    /// # Examples
1375    ///
1376    /// ```no_run
1377    /// # use matrix_sdk_crypto::OlmMachine;
1378    /// # use ruma::{device_id, user_id};
1379    /// # use futures_util::{pin_mut, StreamExt};
1380    /// # let alice = user_id!("@alice:example.org").to_owned();
1381    /// # futures_executor::block_on(async {
1382    /// # let machine = OlmMachine::new(&alice, device_id!("DEVICEID")).await;
1383    ///
1384    /// let secret_stream = machine.store().secrets_stream();
1385    /// pin_mut!(secret_stream);
1386    ///
1387    /// for secret in secret_stream.next().await {
1388    ///     // Accept the secret if it's valid, then delete all the secrets of this type.
1389    ///     machine.store().delete_secrets_from_inbox(&secret.secret_name);
1390    /// }
1391    /// # });
1392    /// ```
1393    pub fn secrets_stream(&self) -> impl Stream<Item = GossippedSecret> + use<> {
1394        self.inner.store.secrets_stream()
1395    }
1396
1397    /// Receive notifications of historic room key bundles as a [`Stream`].
1398    ///
1399    /// Historic room key bundles are defined in [MSC4268](https://github.com/matrix-org/matrix-spec-proposals/pull/4268).
1400    ///
1401    /// Each time a historic room key bundle was received, an update will be
1402    /// sent to the stream. This stream can be used to accept historic room key
1403    /// bundles that arrive out of order, i.e. the bundle arrives after the
1404    /// user has already accepted a room invitation.
1405    ///
1406    /// # Examples
1407    ///
1408    /// ```no_run
1409    /// # use matrix_sdk_crypto::{
1410    /// #    OlmMachine,
1411    /// #    store::types::StoredRoomKeyBundleData,
1412    /// #    types::room_history::RoomKeyBundle
1413    /// # };
1414    /// # use ruma::{device_id, user_id};
1415    /// # use futures_util::{pin_mut, StreamExt};
1416    /// # let alice = user_id!("@alice:example.org").to_owned();
1417    /// # async {
1418    /// # let machine = OlmMachine::new(&alice, device_id!("DEVICEID")).await;
1419    /// let bundle_stream = machine.store().historic_room_key_stream();
1420    /// pin_mut!(bundle_stream);
1421    ///
1422    /// while let Some(bundle_info) = bundle_stream.next().await {
1423    ///     // Try to find the bundle content in the store and if it's valid accept it.
1424    ///     if let Some(bundle_data) = machine.store().get_received_room_key_bundle_data(&bundle_info.room_id, &bundle_info.sender).await? {
1425    ///         // Download the bundle now and import it.
1426    ///         let bundle: RoomKeyBundle = todo!("Download the bundle");
1427    ///         machine.store().receive_room_key_bundle(
1428    ///             &bundle_data,
1429    ///             bundle,
1430    ///             |_, _| {},
1431    ///         ).await?;
1432    ///     }
1433    /// }
1434    /// # anyhow::Ok(()) };
1435    /// ```
1436    pub fn historic_room_key_stream(&self) -> impl Stream<Item = RoomKeyBundleInfo> + use<> {
1437        self.inner.store.historic_room_key_stream()
1438    }
1439
1440    /// Import the given room keys into the store.
1441    ///
1442    /// # Arguments
1443    ///
1444    /// * `exported_keys` - The keys to be imported.
1445    /// * `from_backup_version` - If the keys came from key backup, the key
1446    ///   backup version. This will cause the keys to be marked as already
1447    ///   backed up, and therefore not requiring another backup.
1448    /// * `progress_listener` - Callback which will be called after each key is
1449    ///   processed. Called with arguments `(processed, total)` where
1450    ///   `processed` is the number of keys processed so far, and `total` is the
1451    ///   total number of keys (i.e., `exported_keys.len()`).
1452    pub async fn import_room_keys(
1453        &self,
1454        exported_keys: Vec<ExportedRoomKey>,
1455        from_backup_version: Option<&str>,
1456        progress_listener: impl Fn(usize, usize),
1457    ) -> Result<RoomKeyImportResult> {
1458        let exported_keys: Vec<&ExportedRoomKey> = exported_keys.iter().collect();
1459        self.import_sessions_impl(exported_keys, from_backup_version, progress_listener).await
1460    }
1461
1462    /// Import the given room keys into our store.
1463    ///
1464    /// # Arguments
1465    ///
1466    /// * `exported_keys` - A list of previously exported keys that should be
1467    ///   imported into our store. If we already have a better version of a key
1468    ///   the key will *not* be imported.
1469    ///
1470    /// Returns a tuple of numbers that represent the number of sessions that
1471    /// were imported and the total number of sessions that were found in the
1472    /// key export.
1473    ///
1474    /// # Examples
1475    ///
1476    /// ```no_run
1477    /// # use std::io::Cursor;
1478    /// # use matrix_sdk_crypto::{OlmMachine, decrypt_room_key_export};
1479    /// # use ruma::{device_id, user_id};
1480    /// # let alice = user_id!("@alice:example.org");
1481    /// # async {
1482    /// # let machine = OlmMachine::new(&alice, device_id!("DEVICEID")).await;
1483    /// # let export = Cursor::new("".to_owned());
1484    /// let exported_keys = decrypt_room_key_export(export, "1234").unwrap();
1485    /// machine.store().import_exported_room_keys(exported_keys, |_, _| {}).await.unwrap();
1486    /// # };
1487    /// ```
1488    pub async fn import_exported_room_keys(
1489        &self,
1490        exported_keys: Vec<ExportedRoomKey>,
1491        progress_listener: impl Fn(usize, usize),
1492    ) -> Result<RoomKeyImportResult> {
1493        self.import_room_keys(exported_keys, None, progress_listener).await
1494    }
1495
1496    async fn import_sessions_impl<T>(
1497        &self,
1498        room_keys: Vec<T>,
1499        from_backup_version: Option<&str>,
1500        progress_listener: impl Fn(usize, usize),
1501    ) -> Result<RoomKeyImportResult>
1502    where
1503        T: TryInto<InboundGroupSession> + RoomKeyExport + Copy,
1504        T::Error: Debug,
1505    {
1506        let mut sessions = Vec::new();
1507
1508        let total_count = room_keys.len();
1509        let mut keys = BTreeMap::new();
1510
1511        for (i, key) in room_keys.into_iter().enumerate() {
1512            match key.try_into() {
1513                Ok(session) => {
1514                    // Only import the session if we didn't have this session or
1515                    // if it's a better version of the same session.
1516                    if let Some(merged) = self.merge_received_group_session(session).await? {
1517                        if from_backup_version.is_some() {
1518                            merged.mark_as_backed_up();
1519                        }
1520
1521                        keys.entry(merged.room_id().to_owned())
1522                            .or_insert_with(BTreeMap::new)
1523                            .entry(merged.sender_key().to_base64())
1524                            .or_insert_with(BTreeSet::new)
1525                            .insert(merged.session_id().to_owned());
1526
1527                        sessions.push(merged);
1528                    }
1529                }
1530                Err(e) => {
1531                    warn!(
1532                        sender_key = key.sender_key().to_base64(),
1533                        room_id = ?key.room_id(),
1534                        session_id = key.session_id(),
1535                        error = ?e,
1536                        "Couldn't import a room key from a file export."
1537                    );
1538                }
1539            }
1540
1541            progress_listener(i, total_count);
1542        }
1543
1544        let imported_count = sessions.len();
1545
1546        self.inner.store.save_inbound_group_sessions(sessions, from_backup_version).await?;
1547
1548        info!(total_count, imported_count, room_keys = ?keys, "Successfully imported room keys");
1549
1550        Ok(RoomKeyImportResult::new(imported_count, total_count, keys))
1551    }
1552
1553    pub(crate) fn crypto_store(&self) -> Arc<CryptoStoreWrapper> {
1554        self.inner.store.clone()
1555    }
1556
1557    /// Export the keys that match the given predicate.
1558    ///
1559    /// # Arguments
1560    ///
1561    /// * `predicate` - A closure that will be called for every known
1562    ///   `InboundGroupSession`, which represents a room key. If the closure
1563    ///   returns `true` the `InboundGroupSession` will be included in the
1564    ///   export, if the closure returns `false` it will not be included.
1565    ///
1566    /// # Examples
1567    ///
1568    /// ```no_run
1569    /// # use matrix_sdk_crypto::{OlmMachine, encrypt_room_key_export};
1570    /// # use ruma::{device_id, user_id, room_id};
1571    /// # let alice = user_id!("@alice:example.org");
1572    /// # async {
1573    /// # let machine = OlmMachine::new(&alice, device_id!("DEVICEID")).await;
1574    /// let room_id = room_id!("!test:localhost");
1575    /// let exported_keys = machine.store().export_room_keys(|s| s.room_id() == room_id).await.unwrap();
1576    /// let encrypted_export = encrypt_room_key_export(&exported_keys, "1234", 1);
1577    /// # };
1578    /// ```
1579    pub async fn export_room_keys(
1580        &self,
1581        predicate: impl FnMut(&InboundGroupSession) -> bool,
1582    ) -> Result<Vec<ExportedRoomKey>> {
1583        let mut exported = Vec::new();
1584
1585        let mut sessions = self.get_inbound_group_sessions().await?;
1586        sessions.retain(predicate);
1587
1588        for session in sessions {
1589            let export = session.export().await;
1590            exported.push(export);
1591        }
1592
1593        Ok(exported)
1594    }
1595
1596    /// Export room keys matching a predicate, providing them as an async
1597    /// `Stream`.
1598    ///
1599    /// # Arguments
1600    ///
1601    /// * `predicate` - A closure that will be called for every known
1602    ///   `InboundGroupSession`, which represents a room key. If the closure
1603    ///   returns `true` the `InboundGroupSession` will be included in the
1604    ///   export, if the closure returns `false` it will not be included.
1605    ///
1606    /// # Examples
1607    ///
1608    /// ```no_run
1609    /// use std::pin::pin;
1610    ///
1611    /// use matrix_sdk_crypto::{OlmMachine, olm::ExportedRoomKey};
1612    /// use ruma::{device_id, room_id, user_id};
1613    /// use tokio_stream::StreamExt;
1614    /// # async {
1615    /// let alice = user_id!("@alice:example.org");
1616    /// let machine = OlmMachine::new(&alice, device_id!("DEVICEID")).await;
1617    /// let room_id = room_id!("!test:localhost");
1618    /// let mut keys = pin!(
1619    ///     machine
1620    ///         .store()
1621    ///         .export_room_keys_stream(|s| s.room_id() == room_id)
1622    ///         .await
1623    ///         .unwrap()
1624    /// );
1625    /// while let Some(key) = keys.next().await {
1626    ///     println!("{}", key.room_id);
1627    /// }
1628    /// # };
1629    /// ```
1630    pub async fn export_room_keys_stream(
1631        &self,
1632        predicate: impl FnMut(&InboundGroupSession) -> bool,
1633    ) -> Result<impl Stream<Item = ExportedRoomKey>> {
1634        // TODO: if/when there is a get_inbound_group_sessions_stream, use that here.
1635        let sessions = self.get_inbound_group_sessions().await?;
1636        Ok(futures_util::stream::iter(sessions.into_iter().filter(predicate))
1637            .then(|session| async move { session.export().await }))
1638    }
1639
1640    /// Assemble a room key bundle for sharing encrypted history, as per
1641    /// [MSC4268].
1642    ///
1643    /// [MSC4268]: https://github.com/matrix-org/matrix-spec-proposals/pull/4268
1644    pub async fn build_room_key_bundle(
1645        &self,
1646        room_id: &RoomId,
1647    ) -> std::result::Result<RoomKeyBundle, CryptoStoreError> {
1648        let sessions = self.get_inbound_group_sessions_by_room_id(room_id).await?;
1649
1650        let mut bundle = RoomKeyBundle::default();
1651        for session in sessions {
1652            if session.shared_history() {
1653                bundle.room_keys.push(session.export().await.into());
1654            } else {
1655                bundle.withheld.push(RoomKeyWithheldContent::new(
1656                    session.algorithm().to_owned(),
1657                    WithheldCode::HistoryNotShared,
1658                    session.room_id().to_owned(),
1659                    session.session_id().to_owned(),
1660                    session.sender_key().to_owned(),
1661                    self.device_id().to_owned(),
1662                ));
1663            }
1664        }
1665
1666        // If we received a key bundle ourselves, in which one or more sessions was
1667        // marked as "history not shared", pass that on to the new user.
1668        let withhelds = self.get_withheld_sessions_by_room_id(room_id).await?;
1669        for withheld in withhelds {
1670            if withheld.content.withheld_code() == WithheldCode::HistoryNotShared {
1671                bundle.withheld.push(withheld.content);
1672            }
1673        }
1674
1675        Ok(bundle)
1676    }
1677
1678    /// Import the contents of a downloaded and decrypted [MSC4268] key bundle.
1679    ///
1680    /// # Arguments
1681    ///
1682    /// * `bundle_info` - The [`StoredRoomKeyBundleData`] of the bundle that is
1683    ///   being received.
1684    /// * `bundle` - The decrypted and deserialized bundle itself.
1685    ///
1686    /// [MSC4268]: https://github.com/matrix-org/matrix-spec-proposals/pull/4268
1687    #[instrument(skip(self, bundle, progress_listener), fields(bundle_size = bundle.room_keys.len(), sender_data))]
1688    pub async fn receive_room_key_bundle(
1689        &self,
1690        bundle_info: &StoredRoomKeyBundleData,
1691        bundle: RoomKeyBundle,
1692        progress_listener: impl Fn(usize, usize),
1693    ) -> Result<(), CryptoStoreError> {
1694        let sender_data = if bundle_info.sender_data.should_recalculate() {
1695            let device = self
1696                .get_device_from_curve_key(&bundle_info.sender_user, bundle_info.sender_key)
1697                .await?;
1698
1699            device
1700                .as_ref()
1701                .map(SenderData::from_device)
1702                .unwrap_or_else(|| bundle_info.sender_data.clone())
1703        } else {
1704            bundle_info.sender_data.clone()
1705        };
1706
1707        tracing::Span::current().record("sender_data", tracing::field::debug(&sender_data));
1708
1709        if matches!(
1710            &sender_data,
1711            SenderData::UnknownDevice { .. }
1712                | SenderData::VerificationViolation(_)
1713                | SenderData::DeviceInfo { .. }
1714        ) {
1715            warn!(
1716                "Not accepting a historic room key bundle due to insufficient trust in the sender"
1717            );
1718            return Ok(());
1719        }
1720
1721        self.import_room_key_bundle_sessions(bundle_info, &bundle, progress_listener).await?;
1722        self.import_room_key_bundle_withheld_info(bundle_info, &bundle).await?;
1723
1724        Ok(())
1725    }
1726
1727    async fn import_room_key_bundle_sessions(
1728        &self,
1729        bundle_info: &StoredRoomKeyBundleData,
1730        bundle: &RoomKeyBundle,
1731        progress_listener: impl Fn(usize, usize),
1732    ) -> Result<(), CryptoStoreError> {
1733        let (good, bad): (Vec<_>, Vec<_>) = bundle.room_keys.iter().partition_map(|key| {
1734            if key.room_id != bundle_info.bundle_data.room_id {
1735                trace!("Ignoring key for incorrect room {} in bundle", key.room_id);
1736                Either::Right(key)
1737            } else {
1738                Either::Left(key)
1739            }
1740        });
1741
1742        match (bad.is_empty(), good.is_empty()) {
1743            // Case 1: Completely empty bundle.
1744            (true, true) => {
1745                warn!("Received a completely empty room key bundle");
1746            }
1747
1748            // Case 2: A bundle for the wrong room.
1749            (false, true) => {
1750                let bad_keys: Vec<_> =
1751                    bad.iter().map(|&key| (&key.room_id, &key.session_id)).collect();
1752
1753                warn!(
1754                    ?bad_keys,
1755                    "Received a room key bundle for the wrong room, ignoring all room keys from the bundle"
1756                );
1757            }
1758
1759            // Case 3: A bundle containing useful room keys.
1760            (_, false) => {
1761                // We have at least some good keys, if we also have some bad ones let's
1762                // mention that here.
1763                if !bad.is_empty() {
1764                    warn!(
1765                        bad_key_count = bad.len(),
1766                        "The room key bundle contained some room keys \
1767                         that were meant for a different room"
1768                    );
1769                }
1770
1771                self.import_sessions_impl(good, None, progress_listener).await?;
1772            }
1773        }
1774
1775        Ok(())
1776    }
1777
1778    async fn import_room_key_bundle_withheld_info(
1779        &self,
1780        bundle_info: &StoredRoomKeyBundleData,
1781        bundle: &RoomKeyBundle,
1782    ) -> Result<(), CryptoStoreError> {
1783        let mut session_id_to_withheld_code_map = BTreeMap::new();
1784
1785        let mut changes = Changes::default();
1786        for withheld in &bundle.withheld {
1787            let (room_id, session_id) = match withheld {
1788                RoomKeyWithheldContent::MegolmV1AesSha2(c) => match (c.room_id(), c.session_id()) {
1789                    (Some(room_id), Some(session_id)) => (room_id, session_id),
1790                    _ => continue,
1791                },
1792                #[cfg(feature = "experimental-algorithms")]
1793                RoomKeyWithheldContent::MegolmV2AesSha2(c) => match (c.room_id(), c.session_id()) {
1794                    (Some(room_id), Some(session_id)) => (room_id, session_id),
1795                    _ => continue,
1796                },
1797                RoomKeyWithheldContent::Unknown(_) => continue,
1798            };
1799
1800            if room_id != bundle_info.bundle_data.room_id {
1801                trace!("Ignoring withheld info for incorrect room {} in bundle", room_id);
1802                continue;
1803            }
1804
1805            changes.withheld_session_info.entry(room_id.to_owned()).or_default().insert(
1806                session_id.to_owned(),
1807                RoomKeyWithheldEntry {
1808                    sender: bundle_info.sender_user.clone(),
1809                    content: withheld.to_owned(),
1810                },
1811            );
1812            session_id_to_withheld_code_map.insert(session_id, withheld.withheld_code());
1813        }
1814
1815        self.save_changes(changes).await?;
1816
1817        info!(
1818            room_id = ?bundle_info.bundle_data.room_id,
1819            ?session_id_to_withheld_code_map,
1820            "Successfully imported withheld info from room key bundle",
1821        );
1822
1823        Ok(())
1824    }
1825}
1826
1827impl Deref for Store {
1828    type Target = DynCryptoStore;
1829
1830    fn deref(&self) -> &Self::Target {
1831        self.inner.store.deref().deref()
1832    }
1833}
1834
1835/// A crypto store that implements primitives for cross-process locking.
1836#[derive(Clone, Debug)]
1837pub struct LockableCryptoStore(Arc<dyn CryptoStore<Error = CryptoStoreError>>);
1838
1839impl matrix_sdk_common::cross_process_lock::TryLock for LockableCryptoStore {
1840    type LockError = CryptoStoreError;
1841
1842    async fn try_lock(
1843        &self,
1844        lease_duration_ms: u32,
1845        key: &str,
1846        holder: &str,
1847    ) -> std::result::Result<Option<CrossProcessLockGeneration>, Self::LockError> {
1848        self.0.try_take_leased_lock(lease_duration_ms, key, holder).await
1849    }
1850}
1851
1852#[cfg(test)]
1853mod tests {
1854    use std::{collections::BTreeMap, pin::pin};
1855
1856    use assert_matches2::{assert_let, assert_matches};
1857    use futures_util::StreamExt;
1858    use insta::{_macro_support::Content, assert_json_snapshot, internals::ContentPath};
1859    use matrix_sdk_test::async_test;
1860    use ruma::{
1861        RoomId, device_id,
1862        events::room::{EncryptedFileInit, JsonWebKeyInit},
1863        owned_device_id, owned_mxc_uri, room_id,
1864        serde::Base64,
1865        user_id,
1866    };
1867    use serde_json::json;
1868    use vodozemac::{Ed25519Keypair, megolm::SessionKey};
1869
1870    use crate::{
1871        Account, OlmMachine,
1872        machine::test_helpers::get_machine_pair,
1873        olm::{InboundGroupSession, SenderData},
1874        store::types::{DehydratedDeviceKey, RoomKeyWithheldEntry, StoredRoomKeyBundleData},
1875        types::{
1876            EventEncryptionAlgorithm,
1877            events::{
1878                room_key_bundle::RoomKeyBundleContent,
1879                room_key_withheld::{MegolmV1AesSha2WithheldContent, RoomKeyWithheldContent},
1880            },
1881        },
1882    };
1883
1884    #[async_test]
1885    async fn test_merge_received_group_session() {
1886        let alice_account = Account::with_device_id(user_id!("@a:s.co"), device_id!("ABC"));
1887        let bob = OlmMachine::new(user_id!("@b:s.co"), device_id!("DEF")).await;
1888
1889        let room_id = room_id!("!test:localhost");
1890
1891        let megolm_signing_key = Ed25519Keypair::new();
1892        let inbound = make_inbound_group_session(&alice_account, &megolm_signing_key, room_id);
1893
1894        // Bob already knows about the session, at index 5, with the device keys.
1895        let mut inbound_at_index_5 =
1896            InboundGroupSession::from_export(&inbound.export_at_index(5).await).unwrap();
1897        inbound_at_index_5.sender_data = inbound.sender_data.clone();
1898        bob.store().save_inbound_group_sessions(&[inbound_at_index_5.clone()]).await.unwrap();
1899
1900        // No changes if we get a disconnected session.
1901        let disconnected = make_inbound_group_session(&alice_account, &megolm_signing_key, room_id);
1902        assert_eq!(bob.store().merge_received_group_session(disconnected).await.unwrap(), None);
1903
1904        // No changes needed when we receive a worse copy of the session
1905        let mut worse =
1906            InboundGroupSession::from_export(&inbound.export_at_index(10).await).unwrap();
1907        worse.sender_data = inbound.sender_data.clone();
1908        assert_eq!(bob.store().merge_received_group_session(worse).await.unwrap(), None);
1909
1910        // Nor when we receive an exact copy of what we already have
1911        let mut copy = InboundGroupSession::from_pickle(inbound_at_index_5.pickle().await).unwrap();
1912        copy.sender_data = inbound.sender_data.clone();
1913        assert_eq!(bob.store().merge_received_group_session(copy).await.unwrap(), None);
1914
1915        // But when we receive a better copy of the session, we should get it back
1916        let mut better =
1917            InboundGroupSession::from_export(&inbound.export_at_index(0).await).unwrap();
1918        better.sender_data = inbound.sender_data.clone();
1919        assert_let!(Some(update) = bob.store().merge_received_group_session(better).await.unwrap());
1920        assert_eq!(update.first_known_index(), 0);
1921
1922        // A worse copy of the ratchet, but better trust data
1923        {
1924            let mut worse_ratchet_better_trust =
1925                InboundGroupSession::from_export(&inbound.export_at_index(10).await).unwrap();
1926            let updated_sender_data = SenderData::sender_verified(
1927                alice_account.user_id(),
1928                alice_account.device_id(),
1929                Ed25519Keypair::new().public_key(),
1930            );
1931            worse_ratchet_better_trust.sender_data = updated_sender_data.clone();
1932            assert_let!(
1933                Some(update) = bob
1934                    .store()
1935                    .merge_received_group_session(worse_ratchet_better_trust)
1936                    .await
1937                    .unwrap()
1938            );
1939            assert_eq!(update.sender_data, updated_sender_data);
1940            assert_eq!(update.first_known_index(), 5);
1941            assert_eq!(
1942                update.export_at_index(0).await.session_key.to_bytes(),
1943                inbound.export_at_index(5).await.session_key.to_bytes()
1944            );
1945        }
1946
1947        // A better copy of the ratchet, but worse trust data
1948        {
1949            let mut better_ratchet_worse_trust =
1950                InboundGroupSession::from_export(&inbound.export_at_index(0).await).unwrap();
1951            let updated_sender_data = SenderData::unknown();
1952            better_ratchet_worse_trust.sender_data = updated_sender_data.clone();
1953            assert_let!(
1954                Some(update) = bob
1955                    .store()
1956                    .merge_received_group_session(better_ratchet_worse_trust)
1957                    .await
1958                    .unwrap()
1959            );
1960            assert_eq!(update.sender_data, inbound.sender_data);
1961            assert_eq!(update.first_known_index(), 0);
1962            assert_eq!(
1963                update.export_at_index(0).await.session_key.to_bytes(),
1964                inbound.export_at_index(0).await.session_key.to_bytes()
1965            );
1966        }
1967    }
1968
1969    /// Create an [`InboundGroupSession`] for the given room, using the given
1970    /// Ed25519 key as the signing key/session ID.
1971    fn make_inbound_group_session(
1972        sender_account: &Account,
1973        signing_key: &Ed25519Keypair,
1974        room_id: &RoomId,
1975    ) -> InboundGroupSession {
1976        InboundGroupSession::new(
1977            sender_account.identity_keys.curve25519,
1978            sender_account.identity_keys.ed25519,
1979            room_id,
1980            &make_session_key(signing_key),
1981            SenderData::device_info(crate::types::DeviceKeys::new(
1982                sender_account.user_id().to_owned(),
1983                sender_account.device_id().to_owned(),
1984                vec![],
1985                BTreeMap::new(),
1986                crate::types::Signatures::new(),
1987            )),
1988            EventEncryptionAlgorithm::MegolmV1AesSha2,
1989            Some(ruma::events::room::history_visibility::HistoryVisibility::Shared),
1990            true,
1991        )
1992        .unwrap()
1993    }
1994
1995    /// Make a Megolm [`SessionKey`] using the given Ed25519 key as a signing
1996    /// key/session ID.
1997    fn make_session_key(signing_key: &Ed25519Keypair) -> SessionKey {
1998        use rand::Rng;
1999
2000        // `SessionKey::new` is not public, so the easiest way to construct a Megolm
2001        // session using a known Ed25519 key is to build a byte array in the export
2002        // format.
2003
2004        let mut session_key_bytes = vec![0u8; 229];
2005        // 0: version
2006        session_key_bytes[0] = 2;
2007        // 1..5: index
2008        // 5..133: ratchet key
2009        rand::thread_rng().fill(&mut session_key_bytes[5..133]);
2010        // 133..165: public ed25519 key
2011        session_key_bytes[133..165].copy_from_slice(signing_key.public_key().as_bytes());
2012        // 165..229: signature
2013        let sig = signing_key.sign(&session_key_bytes[0..165]);
2014        session_key_bytes[165..229].copy_from_slice(&sig.to_bytes());
2015
2016        SessionKey::from_bytes(&session_key_bytes).unwrap()
2017    }
2018
2019    #[async_test]
2020    async fn test_import_room_keys_notifies_stream() {
2021        use futures_util::FutureExt;
2022
2023        let (alice, bob, _) =
2024            get_machine_pair(user_id!("@a:s.co"), user_id!("@b:s.co"), false).await;
2025
2026        let room1_id = room_id!("!room1:localhost");
2027        alice.create_outbound_group_session_with_defaults_test_helper(room1_id).await.unwrap();
2028        let exported_sessions = alice.store().export_room_keys(|_| true).await.unwrap();
2029
2030        let mut room_keys_received_stream = Box::pin(bob.store().room_keys_received_stream());
2031        bob.store().import_room_keys(exported_sessions, None, |_, _| {}).await.unwrap();
2032
2033        let room_keys = room_keys_received_stream
2034            .next()
2035            .now_or_never()
2036            .flatten()
2037            .expect("We should have received an update of room key infos")
2038            .unwrap();
2039        assert_eq!(room_keys.len(), 1);
2040        assert_eq!(room_keys[0].room_id, "!room1:localhost");
2041    }
2042
2043    #[async_test]
2044    async fn test_export_room_keys_provides_selected_keys() {
2045        // Given an OlmMachine with room keys in it
2046        let (alice, _, _) = get_machine_pair(user_id!("@a:s.co"), user_id!("@b:s.co"), false).await;
2047        let room1_id = room_id!("!room1:localhost");
2048        let room2_id = room_id!("!room2:localhost");
2049        let room3_id = room_id!("!room3:localhost");
2050        alice.create_outbound_group_session_with_defaults_test_helper(room1_id).await.unwrap();
2051        alice.create_outbound_group_session_with_defaults_test_helper(room2_id).await.unwrap();
2052        alice.create_outbound_group_session_with_defaults_test_helper(room3_id).await.unwrap();
2053
2054        // When I export some of the keys
2055        let keys = alice
2056            .store()
2057            .export_room_keys(|s| s.room_id() == room2_id || s.room_id() == room3_id)
2058            .await
2059            .unwrap();
2060
2061        // Then the requested keys were provided
2062        assert_eq!(keys.len(), 2);
2063        assert_eq!(keys[0].algorithm, EventEncryptionAlgorithm::MegolmV1AesSha2);
2064        assert_eq!(keys[1].algorithm, EventEncryptionAlgorithm::MegolmV1AesSha2);
2065        assert_eq!(keys[0].room_id, "!room2:localhost");
2066        assert_eq!(keys[1].room_id, "!room3:localhost");
2067        assert_eq!(keys[0].session_key.to_base64().len(), 220);
2068        assert_eq!(keys[1].session_key.to_base64().len(), 220);
2069    }
2070
2071    #[async_test]
2072    async fn test_export_room_keys_stream_can_provide_all_keys() {
2073        // Given an OlmMachine with room keys in it
2074        let (alice, _, _) = get_machine_pair(user_id!("@a:s.co"), user_id!("@b:s.co"), false).await;
2075        let room1_id = room_id!("!room1:localhost");
2076        let room2_id = room_id!("!room2:localhost");
2077        alice.create_outbound_group_session_with_defaults_test_helper(room1_id).await.unwrap();
2078        alice.create_outbound_group_session_with_defaults_test_helper(room2_id).await.unwrap();
2079
2080        // When I export the keys as a stream
2081        let mut keys = pin!(alice.store().export_room_keys_stream(|_| true).await.unwrap());
2082
2083        // And collect them
2084        let mut collected = vec![];
2085        while let Some(key) = keys.next().await {
2086            collected.push(key);
2087        }
2088
2089        // Then all the keys were provided
2090        assert_eq!(collected.len(), 2);
2091        assert_eq!(collected[0].algorithm, EventEncryptionAlgorithm::MegolmV1AesSha2);
2092        assert_eq!(collected[1].algorithm, EventEncryptionAlgorithm::MegolmV1AesSha2);
2093        assert_eq!(collected[0].room_id, "!room1:localhost");
2094        assert_eq!(collected[1].room_id, "!room2:localhost");
2095        assert_eq!(collected[0].session_key.to_base64().len(), 220);
2096        assert_eq!(collected[1].session_key.to_base64().len(), 220);
2097    }
2098
2099    #[async_test]
2100    async fn test_export_room_keys_stream_can_provide_a_subset_of_keys() {
2101        // Given an OlmMachine with room keys in it
2102        let (alice, _, _) = get_machine_pair(user_id!("@a:s.co"), user_id!("@b:s.co"), false).await;
2103        let room1_id = room_id!("!room1:localhost");
2104        let room2_id = room_id!("!room2:localhost");
2105        alice.create_outbound_group_session_with_defaults_test_helper(room1_id).await.unwrap();
2106        alice.create_outbound_group_session_with_defaults_test_helper(room2_id).await.unwrap();
2107
2108        // When I export the keys as a stream
2109        let mut keys =
2110            pin!(alice.store().export_room_keys_stream(|s| s.room_id() == room1_id).await.unwrap());
2111
2112        // And collect them
2113        let mut collected = vec![];
2114        while let Some(key) = keys.next().await {
2115            collected.push(key);
2116        }
2117
2118        // Then all the keys matching our predicate were provided, and no others
2119        assert_eq!(collected.len(), 1);
2120        assert_eq!(collected[0].algorithm, EventEncryptionAlgorithm::MegolmV1AesSha2);
2121        assert_eq!(collected[0].room_id, "!room1:localhost");
2122        assert_eq!(collected[0].session_key.to_base64().len(), 220);
2123    }
2124
2125    #[async_test]
2126    async fn test_export_secrets_bundle() {
2127        let user_id = user_id!("@alice:example.com");
2128        let (first, second, _) = get_machine_pair(user_id, user_id, false).await;
2129
2130        let _ = first
2131            .bootstrap_cross_signing(false)
2132            .await
2133            .expect("We should be able to bootstrap cross-signing");
2134
2135        let bundle = first.store().export_secrets_bundle().await.expect(
2136            "We should be able to export the secrets bundle, now that we \
2137             have the cross-signing keys",
2138        );
2139
2140        assert!(bundle.backup.is_none(), "The bundle should not contain a backup key");
2141
2142        second
2143            .store()
2144            .import_secrets_bundle(&bundle)
2145            .await
2146            .expect("We should be able to import the secrets bundle");
2147
2148        let status = second.cross_signing_status().await;
2149        let identity = second.get_identity(user_id, None).await.unwrap().unwrap().own().unwrap();
2150
2151        assert!(identity.is_verified(), "The public identity should be marked as verified.");
2152
2153        assert!(status.is_complete(), "We should have imported all the cross-signing keys");
2154    }
2155
2156    #[async_test]
2157    async fn test_create_dehydrated_device_key() {
2158        let pickle_key = DehydratedDeviceKey::new()
2159            .expect("Should be able to create a random dehydrated device key");
2160
2161        let to_vec = pickle_key.inner.to_vec();
2162        let pickle_key_from_slice = DehydratedDeviceKey::from_slice(to_vec.as_slice())
2163            .expect("Should be able to create a dehydrated device key from slice");
2164
2165        assert_eq!(pickle_key_from_slice.to_base64(), pickle_key.to_base64());
2166    }
2167
2168    #[async_test]
2169    async fn test_create_dehydrated_errors() {
2170        let too_small = [0u8; 22];
2171        let pickle_key = DehydratedDeviceKey::from_slice(&too_small);
2172
2173        assert!(pickle_key.is_err());
2174
2175        let too_big = [0u8; 40];
2176        let pickle_key = DehydratedDeviceKey::from_slice(&too_big);
2177
2178        assert!(pickle_key.is_err());
2179    }
2180
2181    #[async_test]
2182    async fn test_build_room_key_bundle() {
2183        // Given: Alice has sent a number of room keys to Bob, including some in the
2184        // wrong room, and some that are not marked as shared...
2185        let alice = OlmMachine::new(user_id!("@a:s.co"), device_id!("ALICE")).await;
2186        let bob = OlmMachine::new(user_id!("@b:s.co"), device_id!("BOB")).await;
2187
2188        let room1_id = room_id!("!room1:localhost");
2189        let room2_id = room_id!("!room2:localhost");
2190
2191        /* We use hardcoded megolm session data, to get a stable output snapshot. These were all created with:
2192
2193           println!("{}", vodozemac::megolm::GroupSession::new(Default::default()).session_key().to_base64());
2194        */
2195        let session_key1 = "AgAAAAC2XHVzsMBKs4QCRElJ92CJKyGtknCSC8HY7cQ7UYwndMKLQAejXLh5UA0l6s736mgctcUMNvELScUWrObdflrHo+vth/gWreXOaCnaSxmyjjKErQwyIYTkUfqbHy40RJfEesLwnN23on9XAkch/iy8R2+Jz7B8zfG01f2Ow2SxPQFnAndcO1ZSD2GmXgedy6n4B20MWI1jGP2wiexOWbFSya8DO/VxC9m5+/mF+WwYqdpKn9g4Y05Yw4uz7cdjTc3rXm7xK+8E7hI//5QD1nHPvuKYbjjM9u2JSL+Bzp61Cw";
2196        let session_key2 = "AgAAAAC1BXreFTUQQSBGekTEuYxhdytRKyv4JgDGcG+VOBYdPNGgs807SdibCGJky4lJ3I+7ZDGHoUzZPZP/4ogGu4kxni0PWdtWuN7+5zsuamgoFF/BkaGeUUGv6kgIkx8pyPpM5SASTUEP9bN2loDSpUPYwfiIqz74DgC4WQ4435sTBctYvKz8n+TDJwdLXpyT6zKljuqADAioud+s/iqx9LYn9HpbBfezZcvbg67GtE113pLrvde3IcPI5s6dNHK2onGO2B2eoaobcen18bbEDnlUGPeIivArLya7Da6us14jBQ";
2197        let session_key3 = "AgAAAAAM9KFsliaUUhGSXgwOzM5UemjkNH4n8NHgvC/y8hhw13zTF+ooGD4uIYEXYX630oNvQm/EvgZo+dkoc0re+vsqsx4sQeNODdSjcBsWOa0oDF+irQn9oYoLUDPI1IBtY1rX+FV99Zm/xnG7uFOX7aTVlko2GSdejy1w9mfobmfxu5aUc04A9zaKJP1pOthZvRAlhpymGYHgsDtWPrrjyc/yypMflE4kIUEEEtu1kT6mrAmcl615XYRAHYK9G2+fZsGvokwzbkl4nulGwcZMpQEoM0nD2o3GWgX81HW3nGfKBg";
2198        let session_key4 = "AgAAAAA4Kkesxq2h4v9PLD6Sm3Smxspz1PXTqytQPCMQMkkrHNmzV2bHlJ+6/Al9cu8vh1Oj69AK0WUAeJOJuaiskEeg/PI3P03+UYLeC379RzgqwSHdBgdQ41G2vD6zpgmE/8vYToe+qpCZACtPOswZxyqxHH+T/Iq0nv13JmlFGIeA6fEPfr5Y28B49viG74Fs9rxV9EH5PfjbuPM/p+Sz5obShuaBPKQBX1jT913nEXPoIJ06exNZGr0285nw/LgVvNlmWmbqNnbzO2cNZjQWA+xZYz5FSfyCxwqEBbEdUCuRCQ";
2199
2200        let sessions = [
2201            create_inbound_group_session_with_visibility(
2202                &alice,
2203                room1_id,
2204                &SessionKey::from_base64(session_key1).unwrap(),
2205                true,
2206            ),
2207            create_inbound_group_session_with_visibility(
2208                &alice,
2209                room1_id,
2210                &SessionKey::from_base64(session_key2).unwrap(),
2211                true,
2212            ),
2213            create_inbound_group_session_with_visibility(
2214                &alice,
2215                room1_id,
2216                &SessionKey::from_base64(session_key3).unwrap(),
2217                false,
2218            ),
2219            create_inbound_group_session_with_visibility(
2220                &alice,
2221                room2_id,
2222                &SessionKey::from_base64(session_key4).unwrap(),
2223                true,
2224            ),
2225        ];
2226        bob.store().save_inbound_group_sessions(&sessions).await.unwrap();
2227
2228        // When I build the bundle
2229        let mut bundle = bob.store().build_room_key_bundle(room1_id).await.unwrap();
2230
2231        // Then the bundle matches the snapshot.
2232
2233        // We sort the sessions in the bundle, so that the snapshot is stable.
2234        bundle.room_keys.sort_by_key(|session| session.session_id.clone());
2235
2236        // We substitute the algorithm, since this changes based on feature flags.
2237        let algorithm = if cfg!(feature = "experimental-algorithms") {
2238            "m.megolm.v2.aes-sha2"
2239        } else {
2240            "m.megolm.v1.aes-sha2"
2241        };
2242        let map_algorithm = move |value: Content, _path: ContentPath<'_>| {
2243            assert_eq!(value.as_str().unwrap(), algorithm);
2244            "[algorithm]"
2245        };
2246
2247        // We also substitute alice's keys in the snapshot with placeholders
2248        let alice_curve_key = alice.identity_keys().curve25519.to_base64();
2249        let map_alice_curve_key = move |value: Content, _path: ContentPath<'_>| {
2250            assert_eq!(value.as_str().unwrap(), alice_curve_key);
2251            "[alice curve key]"
2252        };
2253        let alice_ed25519_key = alice.identity_keys().ed25519.to_base64();
2254        let map_alice_ed25519_key = move |value: Content, _path: ContentPath<'_>| {
2255            assert_eq!(value.as_str().unwrap(), alice_ed25519_key);
2256            "[alice ed25519 key]"
2257        };
2258
2259        insta::with_settings!({ sort_maps => true }, {
2260            assert_json_snapshot!(bundle, {
2261                ".withheld[].algorithm" => insta::dynamic_redaction(map_algorithm),
2262                ".room_keys[].algorithm" => insta::dynamic_redaction(map_algorithm),
2263                ".room_keys[].sender_key" => insta::dynamic_redaction(map_alice_curve_key.clone()),
2264                ".withheld[].sender_key" => insta::dynamic_redaction(map_alice_curve_key),
2265                ".room_keys[].sender_claimed_keys.ed25519" => insta::dynamic_redaction(map_alice_ed25519_key),
2266            });
2267        });
2268    }
2269
2270    #[async_test]
2271    async fn test_receive_room_key_bundle() {
2272        let alice = OlmMachine::new(user_id!("@a:s.co"), device_id!("ALICE")).await;
2273        let alice_key = alice.identity_keys().curve25519;
2274        let bob = OlmMachine::new(user_id!("@b:s.co"), device_id!("BOB")).await;
2275
2276        let room_id = room_id!("!room1:localhost");
2277
2278        let session_key1 = "AgAAAAC2XHVzsMBKs4QCRElJ92CJKyGtknCSC8HY7cQ7UYwndMKLQAejXLh5UA0l6s736mgctcUMNvELScUWrObdflrHo+vth/gWreXOaCnaSxmyjjKErQwyIYTkUfqbHy40RJfEesLwnN23on9XAkch/iy8R2+Jz7B8zfG01f2Ow2SxPQFnAndcO1ZSD2GmXgedy6n4B20MWI1jGP2wiexOWbFSya8DO/VxC9m5+/mF+WwYqdpKn9g4Y05Yw4uz7cdjTc3rXm7xK+8E7hI//5QD1nHPvuKYbjjM9u2JSL+Bzp61Cw";
2279        let session_key2 = "AgAAAAC1BXreFTUQQSBGekTEuYxhdytRKyv4JgDGcG+VOBYdPNGgs807SdibCGJky4lJ3I+7ZDGHoUzZPZP/4ogGu4kxni0PWdtWuN7+5zsuamgoFF/BkaGeUUGv6kgIkx8pyPpM5SASTUEP9bN2loDSpUPYwfiIqz74DgC4WQ4435sTBctYvKz8n+TDJwdLXpyT6zKljuqADAioud+s/iqx9LYn9HpbBfezZcvbg67GtE113pLrvde3IcPI5s6dNHK2onGO2B2eoaobcen18bbEDnlUGPeIivArLya7Da6us14jBQ";
2280
2281        let sessions = [
2282            create_inbound_group_session_with_visibility(
2283                &alice,
2284                room_id,
2285                &SessionKey::from_base64(session_key1).unwrap(),
2286                true,
2287            ),
2288            create_inbound_group_session_with_visibility(
2289                &alice,
2290                room_id,
2291                &SessionKey::from_base64(session_key2).unwrap(),
2292                false,
2293            ),
2294        ];
2295
2296        alice.store().save_inbound_group_sessions(&sessions).await.unwrap();
2297        let bundle = alice.store().build_room_key_bundle(room_id).await.unwrap();
2298
2299        bob.store()
2300            .receive_room_key_bundle(
2301                &StoredRoomKeyBundleData {
2302                    sender_user: alice.user_id().to_owned(),
2303                    sender_key: alice_key,
2304                    sender_data: SenderData::sender_verified(
2305                        alice.user_id(),
2306                        device_id!("ALICE"),
2307                        alice.identity_keys().ed25519,
2308                    ),
2309
2310                    bundle_data: RoomKeyBundleContent {
2311                        room_id: room_id.to_owned(),
2312                        // This isn't used at all in the method call, so we can fill it with
2313                        // garbage.
2314                        file: EncryptedFileInit {
2315                            url: owned_mxc_uri!("mxc://example.com/0"),
2316                            key: JsonWebKeyInit {
2317                                kty: "oct".to_owned(),
2318                                key_ops: vec!["encrypt".to_owned(), "decrypt".to_owned()],
2319                                alg: "A256CTR.".to_owned(),
2320                                k: Base64::new(vec![0u8; 128]),
2321                                ext: true,
2322                            }
2323                            .into(),
2324                            iv: Base64::new(vec![0u8; 128]),
2325                            hashes: vec![("sha256".to_owned(), Base64::new(vec![0u8; 128]))]
2326                                .into_iter()
2327                                .collect(),
2328                            v: "v2".to_owned(),
2329                        }
2330                        .into(),
2331                    },
2332                },
2333                bundle,
2334                |_, _| {},
2335            )
2336            .await
2337            .unwrap();
2338
2339        // The room key should be imported successfully
2340        let imported_sessions =
2341            bob.store().get_inbound_group_sessions_by_room_id(room_id).await.unwrap();
2342
2343        assert_eq!(imported_sessions.len(), 1);
2344        assert_eq!(imported_sessions[0].room_id(), room_id);
2345
2346        assert_matches!(
2347            bob.store()
2348                .get_withheld_info(room_id, sessions[1].session_id())
2349                .await
2350                .unwrap()
2351                .expect("Withheld info should be present in the store."),
2352            RoomKeyWithheldEntry {
2353                #[cfg(not(feature = "experimental-algorithms"))]
2354                content: RoomKeyWithheldContent::MegolmV1AesSha2(
2355                    MegolmV1AesSha2WithheldContent::HistoryNotShared(_)
2356                ),
2357                #[cfg(feature = "experimental-algorithms")]
2358                content: RoomKeyWithheldContent::MegolmV2AesSha2(
2359                    MegolmV1AesSha2WithheldContent::HistoryNotShared(_)
2360                ),
2361                ..
2362            }
2363        );
2364    }
2365
2366    /// Tests that the new store format introduced in [#5737][#5737] does not
2367    /// conflict with items already in the store that were serialised with the
2368    /// older format.
2369    ///
2370    /// [#5737]: https://github.com/matrix-org/matrix-rust-sdk/pull/5737
2371    #[async_test]
2372    async fn test_deserialize_room_key_withheld_entry_from_to_device_event() {
2373        let entry: RoomKeyWithheldEntry = serde_json::from_value(json!(
2374            {
2375              "content": {
2376                "algorithm": "m.megolm.v1.aes-sha2",
2377                "code": "m.unauthorised",
2378                "from_device": "ALICE",
2379                "reason": "You are not authorised to read the message.",
2380                "room_id": "!roomid:s.co",
2381                "sender_key": "7hIcOrEroXYdzjtCBvBjUiqvT0Me7g+ymeXqoc65RS0",
2382                "session_id": "session123"
2383              },
2384              "sender": "@alice:s.co",
2385              "type": "m.room_key.withheld"
2386            }
2387        ))
2388        .unwrap();
2389
2390        assert_matches!(
2391            entry,
2392            RoomKeyWithheldEntry {
2393                sender,
2394                content: RoomKeyWithheldContent::MegolmV1AesSha2(
2395                    MegolmV1AesSha2WithheldContent::Unauthorised(withheld_content,)
2396                ),
2397            }
2398        );
2399
2400        assert_eq!(sender, "@alice:s.co");
2401        assert_eq!(withheld_content.room_id, "!roomid:s.co");
2402        assert_eq!(withheld_content.session_id, "session123");
2403        assert_eq!(
2404            withheld_content.sender_key.to_base64(),
2405            "7hIcOrEroXYdzjtCBvBjUiqvT0Me7g+ymeXqoc65RS0"
2406        );
2407        assert_eq!(withheld_content.from_device, Some(owned_device_id!("ALICE")));
2408    }
2409
2410    /// Create an inbound Megolm session for the given room.
2411    ///
2412    /// `olm_machine` is used to set the `sender_key` and `signing_key`
2413    /// fields of the resultant session.
2414    ///
2415    /// The encryption algorithm used for the session depends on the
2416    /// `experimental-algorithms` feature flag:
2417    ///
2418    /// - When not set, the session uses `m.megolm.v1.aes-sha2`.
2419    /// - When set, the session uses `m.megolm.v2.aes-sha2`.
2420    fn create_inbound_group_session_with_visibility(
2421        olm_machine: &OlmMachine,
2422        room_id: &RoomId,
2423        session_key: &SessionKey,
2424        shared_history: bool,
2425    ) -> InboundGroupSession {
2426        let identity_keys = &olm_machine.store().static_account().identity_keys;
2427        InboundGroupSession::new(
2428            identity_keys.curve25519,
2429            identity_keys.ed25519,
2430            room_id,
2431            session_key,
2432            SenderData::unknown(),
2433            #[cfg(not(feature = "experimental-algorithms"))]
2434            EventEncryptionAlgorithm::MegolmV1AesSha2,
2435            #[cfg(feature = "experimental-algorithms")]
2436            EventEncryptionAlgorithm::MegolmV2AesSha2,
2437            None,
2438            shared_history,
2439        )
2440        .unwrap()
2441    }
2442}