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