matrix_sdk_crypto/backups/
mod.rs

1// Copyright 2021, 2022 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//! Server-side backup support for room keys
16//!
17//! This module largely implements support for server-side backups using the
18//! `m.megolm_backup.v1.curve25519-aes-sha2` backup algorithm.
19//!
20//! Due to various flaws in this backup algorithm it is **not** recommended to
21//! use this module or any of its functionality. The module is only provided for
22//! backwards compatibility.
23//!
24//! [spec]: https://spec.matrix.org/unstable/client-server-api/#server-side-key-backups
25
26use std::{
27    collections::{BTreeMap, BTreeSet},
28    sync::Arc,
29};
30
31use ruma::{
32    DeviceId, DeviceKeyAlgorithm, OwnedDeviceId, OwnedRoomId, OwnedTransactionId, RoomId,
33    TransactionId, api::client::backup::RoomKeyBackup, serde::Raw,
34};
35use tokio::sync::RwLock;
36use tracing::{debug, info, instrument, trace, warn};
37
38use crate::{
39    CryptoStoreError, Device, RoomKeyImportResult, SignatureError,
40    olm::{BackedUpRoomKey, ExportedRoomKey, InboundGroupSession, SignedJsonObject},
41    store::{
42        Store,
43        types::{BackupDecryptionKey, BackupKeys, Changes, RoomKeyCounts},
44    },
45    types::{MegolmV1AuthData, RoomKeyBackupInfo, Signatures, requests::KeysBackupRequest},
46};
47
48mod keys;
49
50pub use keys::{DecodeError, DecryptionError, MegolmV1BackupKey};
51
52/// A state machine that handles backing up room keys.
53///
54/// The state machine can be activated using the
55/// [`BackupMachine::enable_backup_v1`] method. After the state machine has been
56/// enabled a request that will upload encrypted room keys can be generated
57/// using the [`BackupMachine::backup`] method.
58#[derive(Debug, Clone)]
59pub struct BackupMachine {
60    store: Store,
61    backup_key: Arc<RwLock<Option<MegolmV1BackupKey>>>,
62    pending_backup: Arc<RwLock<Option<PendingBackup>>>,
63}
64
65type SenderKey = String;
66type SessionId = String;
67
68#[derive(Debug, Clone)]
69struct PendingBackup {
70    request_id: OwnedTransactionId,
71    request: KeysBackupRequest,
72    sessions: BTreeMap<OwnedRoomId, BTreeMap<SenderKey, BTreeSet<SessionId>>>,
73}
74
75/// The result of a signature verification of a signed JSON object.
76#[derive(Clone, Debug, Default, PartialEq, Eq)]
77pub struct SignatureVerification {
78    /// The result of the signature verification using the public key of our own
79    /// device.
80    pub device_signature: SignatureState,
81    /// The result of the signature verification using the public key of our own
82    /// user identity.
83    pub user_identity_signature: SignatureState,
84    /// The result of the signature verification using public keys of other
85    /// devices we own.
86    pub other_signatures: BTreeMap<OwnedDeviceId, SignatureState>,
87}
88
89impl SignatureVerification {
90    /// Is the result considered to be trusted?
91    ///
92    /// This tells us if the result has a valid signature from any of the
93    /// following:
94    ///
95    /// * Our own device
96    /// * Our own user identity, provided the identity is trusted as well
97    /// * Any of our own devices, provided the device is trusted as well
98    pub fn trusted(&self) -> bool {
99        self.device_signature.trusted()
100            || self.user_identity_signature.trusted()
101            || self.other_signatures.values().any(|s| s.trusted())
102    }
103}
104
105/// The result of a signature check.
106#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
107#[cfg_attr(feature = "uniffi", derive(uniffi::Enum))]
108pub enum SignatureState {
109    /// The signature is missing.
110    #[default]
111    Missing,
112    /// The signature is invalid.
113    Invalid,
114    /// The signature is valid but the device or user identity that created the
115    /// signature is not trusted.
116    ValidButNotTrusted,
117    /// The signature is valid and the device or user identity that created the
118    /// signature is trusted.
119    ValidAndTrusted,
120}
121
122impl SignatureState {
123    /// Is the state considered to be trusted?
124    pub fn trusted(self) -> bool {
125        self == SignatureState::ValidAndTrusted
126    }
127
128    /// Did we find a valid signature?
129    pub fn signed(self) -> bool {
130        self == SignatureState::ValidButNotTrusted && self == SignatureState::ValidAndTrusted
131    }
132}
133
134impl BackupMachine {
135    const BACKUP_BATCH_SIZE: usize = 100;
136
137    pub(crate) fn new(store: Store, backup_key: Option<MegolmV1BackupKey>) -> Self {
138        Self {
139            store,
140            backup_key: RwLock::new(backup_key).into(),
141            pending_backup: RwLock::new(None).into(),
142        }
143    }
144
145    /// Are we able to back up room keys to the server?
146    pub async fn enabled(&self) -> bool {
147        self.backup_key.read().await.as_ref().is_some_and(|b| b.backup_version().is_some())
148    }
149
150    /// Check if our own device has signed the given signed JSON payload.
151    fn check_own_device_signature(
152        &self,
153        signatures: &Signatures,
154        auth_data: &str,
155    ) -> SignatureState {
156        match self.store.static_account().has_signed_raw(signatures, auth_data) {
157            Ok(_) => SignatureState::ValidAndTrusted,
158            Err(e) => match e {
159                SignatureError::NoSignatureFound => SignatureState::Missing,
160                _ => SignatureState::Invalid,
161            },
162        }
163    }
164
165    /// Check if our own cross-signing user identity has signed the given signed
166    /// JSON payload.
167    async fn check_own_identity_signature(
168        &self,
169        signatures: &Signatures,
170        auth_data: &str,
171    ) -> Result<SignatureState, CryptoStoreError> {
172        let user_id = &self.store.static_account().user_id;
173        let identity = self.store.get_identity(user_id).await?;
174
175        let ret = if let Some(identity) = identity.and_then(|i| i.own()) {
176            match identity.master_key().has_signed_raw(signatures, auth_data) {
177                Ok(_) => {
178                    if identity.is_verified() {
179                        SignatureState::ValidAndTrusted
180                    } else {
181                        SignatureState::ValidButNotTrusted
182                    }
183                }
184                Err(e) => match e {
185                    SignatureError::NoSignatureFound => SignatureState::Missing,
186                    _ => SignatureState::Invalid,
187                },
188            }
189        } else {
190            SignatureState::Missing
191        };
192
193        Ok(ret)
194    }
195
196    /// Check if the signed JSON payload `auth_data` has been signed by the
197    /// `device`.
198    fn backup_signed_by_device(
199        &self,
200        device: Device,
201        signatures: &Signatures,
202        auth_data: &str,
203    ) -> SignatureState {
204        if device.has_signed_raw(signatures, auth_data).is_ok() {
205            if device.is_verified() {
206                SignatureState::ValidAndTrusted
207            } else {
208                SignatureState::ValidButNotTrusted
209            }
210        } else {
211            SignatureState::Invalid
212        }
213    }
214
215    /// Check if the signed JSON payload `auth_data` has been signed by any of
216    /// our devices.
217    async fn test_device_signatures(
218        &self,
219        signatures: &Signatures,
220        auth_data: &str,
221        compute_all_signatures: bool,
222    ) -> Result<BTreeMap<OwnedDeviceId, SignatureState>, CryptoStoreError> {
223        let mut result = BTreeMap::new();
224
225        if let Some(user_signatures) = signatures.get(&self.store.static_account().user_id) {
226            for device_key_id in user_signatures.keys() {
227                if device_key_id.algorithm() == DeviceKeyAlgorithm::Ed25519 {
228                    // No need to check our own device here, we're doing that using
229                    // the check_own_device_signature().
230                    if device_key_id.key_name() == self.store.static_account().device_id {
231                        continue;
232                    }
233
234                    let state = self
235                        .test_ed25519_device_signature(
236                            device_key_id.key_name(),
237                            signatures,
238                            auth_data,
239                        )
240                        .await?;
241
242                    result.insert(device_key_id.key_name().to_owned(), state);
243
244                    // Abort the loop if we found a trusted and valid signature,
245                    // unless we should check all of them.
246                    if state.trusted() && !compute_all_signatures {
247                        break;
248                    }
249                }
250            }
251        }
252
253        Ok(result)
254    }
255
256    async fn test_ed25519_device_signature(
257        &self,
258        device_id: &DeviceId,
259        signatures: &Signatures,
260        auth_data: &str,
261    ) -> Result<SignatureState, CryptoStoreError> {
262        // We might iterate over some non-device signatures as well, but in this
263        // case there's no corresponding device and we get `Ok(None)` here, so
264        // things still work out.
265        let device = self.store.get_device(self.store.user_id(), device_id).await?;
266        trace!(?device_id, "Checking backup auth data for device");
267
268        if let Some(device) = device {
269            Ok(self.backup_signed_by_device(device, signatures, auth_data))
270        } else {
271            trace!(?device_id, "Device not found, can't check signature");
272            Ok(SignatureState::Missing)
273        }
274    }
275
276    async fn verify_auth_data_v1(
277        &self,
278        auth_data: MegolmV1AuthData,
279        compute_all_signatures: bool,
280    ) -> Result<SignatureVerification, CryptoStoreError> {
281        let serialized_auth_data = match auth_data.to_canonical_json() {
282            Ok(s) => s,
283            Err(e) => {
284                warn!(error =? e, "Error while verifying backup, can't canonicalize auth data");
285                return Ok(Default::default());
286            }
287        };
288
289        // Check if there's a signature from our own device.
290        let device_signature =
291            self.check_own_device_signature(&auth_data.signatures, &serialized_auth_data);
292        // Check if there's a signature from our own user identity.
293        let user_identity_signature =
294            self.check_own_identity_signature(&auth_data.signatures, &serialized_auth_data).await?;
295
296        // Collect all the other signatures if there isn't already a valid one,
297        // or if we're told to collect all of them anyways.
298        let other_signatures = if !(device_signature.trusted() || user_identity_signature.trusted())
299            || compute_all_signatures
300        {
301            self.test_device_signatures(
302                &auth_data.signatures,
303                &serialized_auth_data,
304                compute_all_signatures,
305            )
306            .await?
307        } else {
308            Default::default()
309        };
310
311        Ok(SignatureVerification { device_signature, user_identity_signature, other_signatures })
312    }
313
314    /// Verify some backup info that we downloaded from the server.
315    ///
316    /// # Arguments
317    ///
318    /// * `backup_info`: The backup info that should be verified. Should be
319    ///   fetched from the server using the [`/room_keys/version`] endpoint.
320    ///
321    /// * `compute_all_signatures`: *Useful for debugging only*. If this
322    ///   parameter is `true`, the internal machinery will compute the trust
323    ///   state for all signatures before returning, instead of short-circuiting
324    ///   on the first trusted signature. Has no impact on whether the backup
325    ///   will be considered verified.
326    ///
327    /// [`/room_keys/version`]: https://spec.matrix.org/unstable/client-server-api/#get_matrixclientv3room_keysversion
328    pub async fn verify_backup(
329        &self,
330        backup_info: RoomKeyBackupInfo,
331        compute_all_signatures: bool,
332    ) -> Result<SignatureVerification, CryptoStoreError> {
333        trace!(?backup_info, "Verifying backup auth data");
334
335        if let RoomKeyBackupInfo::MegolmBackupV1Curve25519AesSha2(data) = backup_info {
336            self.verify_auth_data_v1(data, compute_all_signatures).await
337        } else {
338            Ok(Default::default())
339        }
340    }
341
342    /// Sign a [`RoomKeyBackupInfo`] using the device's identity key and, if
343    /// available, the cross-signing master key.
344    ///
345    /// # Arguments
346    ///
347    /// * `backup_info`: The backup version that should be verified. Should be
348    ///   created from the [`BackupDecryptionKey`] using the
349    ///   [`BackupDecryptionKey::to_backup_info()`] method.
350    pub async fn sign_backup(
351        &self,
352        backup_info: &mut RoomKeyBackupInfo,
353    ) -> Result<(), SignatureError> {
354        if let RoomKeyBackupInfo::MegolmBackupV1Curve25519AesSha2(data) = backup_info {
355            let canonical_json = data.to_canonical_json()?;
356
357            let private_identity = self.store.private_identity();
358            let identity = private_identity.lock().await;
359
360            if let Some(key_id) = identity.master_key_id().await
361                && let Ok(signature) = identity.sign(&canonical_json).await
362            {
363                data.signatures.add_signature(self.store.user_id().to_owned(), key_id, signature);
364            }
365
366            let cache = self.store.cache().await?;
367            let account = cache.account().await?;
368            let key_id = account.signing_key_id();
369            let signature = account.sign(&canonical_json);
370            data.signatures.add_signature(self.store.user_id().to_owned(), key_id, signature);
371
372            Ok(())
373        } else {
374            Err(SignatureError::UnsupportedAlgorithm)
375        }
376    }
377
378    /// Activate the given backup key to be used to encrypt and backup room
379    /// keys.
380    ///
381    /// This will use the [`m.megolm_backup.v1.curve25519-aes-sha2`] algorithm
382    /// to encrypt the room keys.
383    ///
384    /// [`m.megolm_backup.v1.curve25519-aes-sha2`]:
385    /// https://spec.matrix.org/unstable/client-server-api/#backup-algorithm-mmegolm_backupv1curve25519-aes-sha2
386    pub async fn enable_backup_v1(&self, key: MegolmV1BackupKey) -> Result<(), CryptoStoreError> {
387        if key.backup_version().is_some() {
388            *self.backup_key.write().await = Some(key.clone());
389            info!(backup_key = ?key, "Activated a backup");
390        } else {
391            warn!(backup_key = ?key, "Tried to activate a backup without having the backup key uploaded");
392        }
393
394        Ok(())
395    }
396
397    /// Get the number of backed up room keys and the total number of room keys.
398    pub async fn room_key_counts(&self) -> Result<RoomKeyCounts, CryptoStoreError> {
399        let backup_version = self.backup_key.read().await.as_ref().and_then(|k| k.backup_version());
400        self.store.inbound_group_session_counts(backup_version.as_deref()).await
401    }
402
403    /// Disable and reset our backup state.
404    ///
405    /// This will remove any pending backup request, remove the backup key and
406    /// reset the backup state of each room key we have.
407    #[instrument(skip(self))]
408    pub async fn disable_backup(&self) -> Result<(), CryptoStoreError> {
409        debug!("Disabling key backup and resetting backup state for room keys");
410
411        self.backup_key.write().await.take();
412        self.pending_backup.write().await.take();
413
414        self.store.reset_backup_state().await?;
415
416        debug!("Done disabling backup");
417
418        Ok(())
419    }
420
421    /// Provide the `backup_version` of the current `backup_key`, or None if
422    /// there is no current key, or the key is not used with any backup
423    /// version.
424    pub async fn backup_version(&self) -> Option<String> {
425        self.backup_key.read().await.as_ref().and_then(|k| k.backup_version())
426    }
427
428    /// Store the backup decryption key in the crypto store.
429    ///
430    /// This is useful if the client wants to support gossiping of the backup
431    /// key.
432    pub async fn save_decryption_key(
433        &self,
434        backup_decryption_key: Option<BackupDecryptionKey>,
435        version: Option<String>,
436    ) -> Result<(), CryptoStoreError> {
437        let changes =
438            Changes { backup_decryption_key, backup_version: version, ..Default::default() };
439        self.store.save_changes(changes).await
440    }
441
442    /// Get the backup keys we have saved in our crypto store.
443    pub async fn get_backup_keys(&self) -> Result<BackupKeys, CryptoStoreError> {
444        self.store.load_backup_keys().await
445    }
446
447    /// Encrypt a batch of room keys and return a request that needs to be sent
448    /// out to backup the room keys.
449    pub async fn backup(
450        &self,
451    ) -> Result<Option<(OwnedTransactionId, KeysBackupRequest)>, CryptoStoreError> {
452        let mut request = self.pending_backup.write().await;
453
454        if let Some(request) = &*request {
455            trace!("Backing up, returning an existing request");
456
457            Ok(Some((request.request_id.clone(), request.request.clone())))
458        } else {
459            trace!("Backing up, creating a new request");
460
461            let new_request = self.backup_helper().await?;
462            *request = new_request.clone();
463
464            Ok(new_request.map(|r| (r.request_id, r.request)))
465        }
466    }
467
468    pub(crate) async fn mark_request_as_sent(
469        &self,
470        request_id: &TransactionId,
471    ) -> Result<(), CryptoStoreError> {
472        let mut request = self.pending_backup.write().await;
473        if let Some(r) = &*request {
474            if r.request_id == request_id {
475                let room_and_session_ids: Vec<(&RoomId, &str)> = r
476                    .sessions
477                    .iter()
478                    .flat_map(|(room_id, sender_key_to_session_ids)| {
479                        std::iter::repeat(room_id).zip(sender_key_to_session_ids.values().flatten())
480                    })
481                    .map(|(room_id, session_id)| (room_id.as_ref(), session_id.as_str()))
482                    .collect();
483
484                trace!(request_id = ?r.request_id, keys = ?r.sessions, "Marking room keys as backed up");
485
486                self.store
487                    .mark_inbound_group_sessions_as_backed_up(
488                        &r.request.version,
489                        &room_and_session_ids,
490                    )
491                    .await?;
492
493                trace!(
494                    request_id = ?r.request_id,
495                    keys = ?r.sessions,
496                    "Marked room keys as backed up"
497                );
498
499                *request = None;
500            } else {
501                warn!(
502                    expected = ?r.request_id,
503                    got = ?request_id,
504                    "Tried to mark a pending backup as sent but the request id didn't match"
505                );
506            }
507        } else {
508            warn!(
509                ?request_id,
510                "Tried to mark a pending backup as sent but there isn't a backup pending"
511            );
512        }
513
514        Ok(())
515    }
516
517    async fn backup_helper(&self) -> Result<Option<PendingBackup>, CryptoStoreError> {
518        let Some(backup_key) = &*self.backup_key.read().await else {
519            warn!("Trying to backup room keys but no backup key was found");
520            return Ok(None);
521        };
522
523        let Some(version) = backup_key.backup_version() else {
524            warn!("Trying to backup room keys but the backup key wasn't uploaded");
525            return Ok(None);
526        };
527
528        let sessions =
529            self.store.inbound_group_sessions_for_backup(&version, Self::BACKUP_BATCH_SIZE).await?;
530
531        if sessions.is_empty() {
532            trace!(?backup_key, "No room keys need to be backed up");
533            return Ok(None);
534        }
535
536        let key_count = sessions.len();
537        let (backup, session_record) = Self::backup_keys(sessions, backup_key).await;
538
539        info!(
540            key_count = key_count,
541            keys = ?session_record,
542            ?backup_key,
543            "Successfully created a room keys backup request"
544        );
545
546        let request = PendingBackup {
547            request_id: TransactionId::new(),
548            request: KeysBackupRequest { version, rooms: backup },
549            sessions: session_record,
550        };
551
552        Ok(Some(request))
553    }
554
555    /// Backup all the non-backed up room keys we know about
556    async fn backup_keys(
557        sessions: Vec<InboundGroupSession>,
558        backup_key: &MegolmV1BackupKey,
559    ) -> (
560        BTreeMap<OwnedRoomId, RoomKeyBackup>,
561        BTreeMap<OwnedRoomId, BTreeMap<SenderKey, BTreeSet<SessionId>>>,
562    ) {
563        let mut backup: BTreeMap<OwnedRoomId, RoomKeyBackup> = BTreeMap::new();
564        let mut session_record: BTreeMap<OwnedRoomId, BTreeMap<SenderKey, BTreeSet<SessionId>>> =
565            BTreeMap::new();
566
567        for session in sessions {
568            let room_id = session.room_id().to_owned();
569            let session_id = session.session_id().to_owned();
570            let sender_key = session.sender_key().to_owned();
571            let session = backup_key.encrypt(session).await;
572
573            session_record
574                .entry(room_id.to_owned())
575                .or_default()
576                .entry(sender_key.to_base64())
577                .or_default()
578                .insert(session_id.clone());
579
580            let session = Raw::new(&session).expect("Can't serialize a backed up room key");
581
582            backup
583                .entry(room_id)
584                .or_insert_with(|| RoomKeyBackup::new(BTreeMap::new()))
585                .sessions
586                .insert(session_id, session);
587        }
588
589        (backup, session_record)
590    }
591
592    /// Import the given room keys into our store.
593    ///
594    /// # Arguments
595    ///
596    /// * `room_keys` - A list of previously exported keys that should be
597    ///   imported into our store. If we already have a better version of a key
598    ///   the key will *not* be imported.
599    ///
600    /// Returns a [`RoomKeyImportResult`] containing information about room keys
601    /// which were imported.
602    #[deprecated(note = "Use the OlmMachine::store::import_room_keys method instead")]
603    pub async fn import_backed_up_room_keys(
604        &self,
605        room_keys: BTreeMap<OwnedRoomId, BTreeMap<String, BackedUpRoomKey>>,
606        progress_listener: impl Fn(usize, usize),
607    ) -> Result<RoomKeyImportResult, CryptoStoreError> {
608        let mut decrypted_room_keys = vec![];
609
610        for (room_id, room_keys) in room_keys {
611            for (session_id, room_key) in room_keys {
612                let room_key = ExportedRoomKey::from_backed_up_room_key(
613                    room_id.to_owned(),
614                    session_id,
615                    room_key,
616                );
617
618                decrypted_room_keys.push(room_key);
619            }
620        }
621
622        // FIXME: This method is a bit flawed: we have no real idea which backup version
623        //   these keys came from. For example, we might have reset the backup
624        //   since the keys were downloaded. For now, let's assume they came from
625        //   the "current" backup version.
626        let backup_version = self.backup_version().await;
627
628        self.store
629            .import_room_keys(decrypted_room_keys, backup_version.as_deref(), progress_listener)
630            .await
631    }
632}
633
634#[cfg(test)]
635mod tests {
636    use std::collections::BTreeMap;
637
638    use assert_matches2::assert_let;
639    use matrix_sdk_test::async_test;
640    use ruma::{CanonicalJsonValue, DeviceId, RoomId, UserId, device_id, room_id, user_id};
641    use serde_json::json;
642
643    use super::BackupMachine;
644    use crate::{
645        OlmError, OlmMachine,
646        olm::BackedUpRoomKey,
647        store::{
648            CryptoStore, MemoryStore,
649            types::{BackupDecryptionKey, Changes},
650        },
651        types::RoomKeyBackupInfo,
652    };
653
654    fn room_key() -> BackedUpRoomKey {
655        let json = json!({
656            "algorithm": "m.megolm.v1.aes-sha2",
657            "sender_key": "DeHIg4gwhClxzFYcmNntPNF9YtsdZbmMy8+3kzCMXHA",
658            "session_key": "AQAAAABvWMNZjKFtebYIePKieQguozuoLgzeY6wKcyJjLJcJtQgy1dPqTBD12U+XrYLrRHn\
659                            lKmxoozlhFqJl456+9hlHCL+yq+6ScFuBHtJepnY1l2bdLb4T0JMDkNsNErkiLiLnD6yp3J\
660                            DSjIhkdHxmup/huygrmroq6/L5TaThEoqvW4DPIuO14btKudsS34FF82pwjKS4p6Mlch+0e\
661                            fHAblQV",
662            "sender_claimed_keys":{},
663            "forwarding_curve25519_key_chain":[]
664        });
665
666        serde_json::from_value(json)
667            .expect("We should be able to deserialize our backed up room key")
668    }
669
670    fn alice_id() -> &'static UserId {
671        user_id!("@alice:example.org")
672    }
673
674    fn alice_device_id() -> &'static DeviceId {
675        device_id!("JLAFKJWSCS")
676    }
677
678    fn room_id() -> &'static RoomId {
679        room_id!("!test:localhost")
680    }
681
682    fn room_id2() -> &'static RoomId {
683        room_id!("!test2:localhost")
684    }
685
686    async fn backup_flow(machine: OlmMachine) -> Result<(), OlmError> {
687        let backup_machine = machine.backup_machine();
688        let backup_version = current_backup_version(backup_machine).await;
689
690        let counts =
691            backup_machine.store.inbound_group_session_counts(backup_version.as_deref()).await?;
692
693        assert_eq!(counts.total, 0, "Initially no keys exist");
694        assert_eq!(counts.backed_up, 0, "Initially no backed up keys exist");
695
696        machine.create_outbound_group_session_with_defaults_test_helper(room_id()).await?;
697        machine.create_outbound_group_session_with_defaults_test_helper(room_id2()).await?;
698
699        let counts =
700            backup_machine.store.inbound_group_session_counts(backup_version.as_deref()).await?;
701        assert_eq!(counts.total, 2, "Two room keys need to exist in the store");
702        assert_eq!(counts.backed_up, 0, "No room keys have been backed up yet");
703
704        let decryption_key = BackupDecryptionKey::new().expect("Can't create new recovery key");
705        let backup_key = decryption_key.megolm_v1_public_key();
706        backup_key.set_version("1".to_owned());
707
708        backup_machine.enable_backup_v1(backup_key).await?;
709
710        let (request_id, _) =
711            backup_machine.backup().await?.expect("Created a backup request successfully");
712        assert_eq!(
713            Some(&request_id),
714            backup_machine.backup().await?.as_ref().map(|(request_id, _)| request_id),
715            "Calling backup again without uploading creates the same backup request"
716        );
717
718        backup_machine.mark_request_as_sent(&request_id).await?;
719        let backup_version = current_backup_version(backup_machine).await;
720
721        let counts =
722            backup_machine.store.inbound_group_session_counts(backup_version.as_deref()).await?;
723        assert_eq!(counts.total, 2);
724        assert_eq!(counts.backed_up, 2, "All room keys have been backed up");
725
726        assert!(
727            backup_machine.backup().await?.is_none(),
728            "No room keys need to be backed up, no request needs to be created"
729        );
730
731        backup_machine.disable_backup().await?;
732        let backup_version = current_backup_version(backup_machine).await;
733
734        let counts =
735            backup_machine.store.inbound_group_session_counts(backup_version.as_deref()).await?;
736        assert_eq!(counts.total, 2);
737        assert_eq!(
738            counts.backed_up, 0,
739            "Disabling the backup resets the backup flag on the room keys"
740        );
741
742        Ok(())
743    }
744
745    async fn current_backup_version(backup_machine: &BackupMachine) -> Option<String> {
746        backup_machine.backup_key.read().await.as_ref().and_then(|k| k.backup_version())
747    }
748
749    #[async_test]
750    async fn test_memory_store_backups() -> Result<(), OlmError> {
751        let machine = OlmMachine::new(alice_id(), alice_device_id()).await;
752
753        backup_flow(machine).await
754    }
755
756    #[async_test]
757    async fn test_verify_auth_data() -> Result<(), OlmError> {
758        let machine = OlmMachine::new(alice_id(), alice_device_id()).await;
759        let backup_machine = machine.backup_machine();
760
761        let auth_data = json!({
762            "public_key":"XjhWTCjW7l59pbfx9tlCBQolfnIQWARoKOzjTOPSlWM",
763        });
764
765        let backup_version = json!({
766            "algorithm": "m.megolm_backup.v1.curve25519-aes-sha2",
767            "auth_data": auth_data,
768        });
769
770        let canonical_json: CanonicalJsonValue =
771            auth_data.clone().try_into().expect("Canonicalizing should always work");
772        let serialized = canonical_json.to_string();
773
774        let backup_version: RoomKeyBackupInfo = serde_json::from_value(backup_version).unwrap();
775
776        let state = backup_machine
777            .verify_backup(backup_version, false)
778            .await
779            .expect("Verifying should work");
780        assert!(!state.trusted());
781        assert!(!state.device_signature.trusted());
782        assert!(!state.user_identity_signature.trusted());
783        assert!(!state.other_signatures.values().any(|s| s.trusted()));
784
785        let signatures = machine.sign(&serialized).await?;
786
787        let backup_version = json!({
788            "algorithm": "m.megolm_backup.v1.curve25519-aes-sha2",
789            "auth_data": {
790                "public_key":"XjhWTCjW7l59pbfx9tlCBQolfnIQWARoKOzjTOPSlWM",
791                "signatures": signatures,
792            }
793        });
794        let backup_version: RoomKeyBackupInfo = serde_json::from_value(backup_version).unwrap();
795
796        let state = backup_machine
797            .verify_backup(backup_version, false)
798            .await
799            .expect("Verifying should work");
800
801        assert!(state.trusted());
802        assert!(state.device_signature.trusted());
803        assert!(!state.user_identity_signature.trusted());
804        assert!(!state.other_signatures.values().any(|s| s.trusted()));
805
806        machine
807            .bootstrap_cross_signing(true)
808            .await
809            .expect("Bootstrapping a new identity always works");
810
811        let signatures = machine.sign(&serialized).await?;
812
813        let backup_version = json!({
814            "algorithm": "m.megolm_backup.v1.curve25519-aes-sha2",
815            "auth_data": {
816                "public_key":"XjhWTCjW7l59pbfx9tlCBQolfnIQWARoKOzjTOPSlWM",
817                "signatures": signatures,
818            }
819        });
820        let backup_version: RoomKeyBackupInfo = serde_json::from_value(backup_version).unwrap();
821
822        let state = backup_machine
823            .verify_backup(backup_version, false)
824            .await
825            .expect("Verifying should work");
826
827        assert!(state.trusted());
828        assert!(state.device_signature.trusted());
829        assert!(state.user_identity_signature.trusted());
830        assert!(!state.other_signatures.values().any(|s| s.trusted()));
831
832        Ok(())
833    }
834
835    #[async_test]
836    async fn test_import_backed_up_room_keys() {
837        let machine = OlmMachine::new(alice_id(), alice_device_id()).await;
838        let backup_machine = machine.backup_machine();
839
840        // We set up a backup key, so that we can test `backup_machine.backup()` later.
841        let decryption_key = BackupDecryptionKey::new().expect("Couldn't create new recovery key");
842        let backup_key = decryption_key.megolm_v1_public_key();
843        backup_key.set_version("1".to_owned());
844        backup_machine.enable_backup_v1(backup_key).await.expect("Couldn't enable backup");
845
846        let room_id = room_id!("!DovneieKSTkdHKpIXy:morpheus.localhost");
847        let session_id = "gM8i47Xhu0q52xLfgUXzanCMpLinoyVyH7R58cBuVBU";
848        let room_key = room_key();
849
850        let room_keys: BTreeMap<_, BTreeMap<_, _>> = BTreeMap::from([(
851            room_id.to_owned(),
852            BTreeMap::from([(session_id.to_owned(), room_key)]),
853        )]);
854
855        let session = machine.store().get_inbound_group_session(room_id, session_id).await.unwrap();
856
857        assert!(session.is_none(), "Initially we should not have the session in the store");
858
859        #[allow(deprecated)]
860        backup_machine
861            .import_backed_up_room_keys(room_keys, |_, _| {})
862            .await
863            .expect("We should be able to import a room key");
864
865        // Now check that the session was correctly imported, and that it is marked as
866        // backed up
867        let session = machine.store().get_inbound_group_session(room_id, session_id).await.unwrap();
868        assert_let!(Some(session) = session);
869        assert!(
870            session.backed_up(),
871            "If a session was imported from a backup, it should be considered to be backed up"
872        );
873        assert!(session.has_been_imported());
874
875        // Also check that it is not returned by a backup request.
876        let backup_request =
877            backup_machine.backup().await.expect("We should be able to create a backup request");
878        assert!(
879            backup_request.is_none(),
880            "If a session was imported from backup, it should not be backed up again."
881        );
882    }
883
884    #[async_test]
885    async fn test_sign_backup_info() {
886        let machine = OlmMachine::new(alice_id(), alice_device_id()).await;
887        let backup_machine = machine.backup_machine();
888
889        let decryption_key = BackupDecryptionKey::new().unwrap();
890        let mut backup_info = decryption_key.to_backup_info();
891
892        let result = backup_machine.verify_backup(backup_info.to_owned(), false).await.unwrap();
893
894        assert!(!result.trusted());
895
896        backup_machine.sign_backup(&mut backup_info).await.unwrap();
897
898        let result = backup_machine.verify_backup(backup_info, false).await.unwrap();
899
900        assert!(result.trusted());
901    }
902
903    #[async_test]
904    async fn test_fix_backup_key_mismatch() {
905        let store = MemoryStore::new();
906
907        let backup_decryption_key = BackupDecryptionKey::new().unwrap();
908
909        store
910            .save_changes(Changes {
911                backup_decryption_key: Some(backup_decryption_key.clone()),
912                backup_version: Some("1".to_owned()),
913                ..Default::default()
914            })
915            .await
916            .unwrap();
917
918        // Create the machine using `with_store` and without a call to enable_backup_v1,
919        // like regenerate_olm would do
920        let alice =
921            OlmMachine::with_store(alice_id(), alice_device_id(), store, None).await.unwrap();
922
923        let binding = alice.backup_machine().backup_key.read().await;
924        let machine_backup_key = binding.as_ref().unwrap();
925
926        assert_eq!(
927            machine_backup_key.to_base64(),
928            backup_decryption_key.megolm_v1_public_key().to_base64(),
929            "The OlmMachine loaded the wrong backup key."
930        );
931    }
932}