Skip to main content

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    ) -> Result<
560        (
561            BTreeMap<OwnedRoomId, RoomKeyBackup>,
562            BTreeMap<OwnedRoomId, BTreeMap<SenderKey, BTreeSet<SessionId>>>,
563        ),
564        vodozemac::pk_encryption::Error,
565    > {
566        let mut backup: BTreeMap<OwnedRoomId, RoomKeyBackup> = BTreeMap::new();
567        let mut session_record: BTreeMap<OwnedRoomId, BTreeMap<SenderKey, BTreeSet<SessionId>>> =
568            BTreeMap::new();
569
570        for session in sessions {
571            let room_id = session.room_id().to_owned();
572            let session_id = session.session_id().to_owned();
573            let sender_key = session.sender_key().to_owned();
574            let session = backup_key.encrypt(session).await?;
575
576            session_record
577                .entry(room_id.to_owned())
578                .or_default()
579                .entry(sender_key.to_base64())
580                .or_default()
581                .insert(session_id.clone());
582
583            let session = Raw::new(&session).expect("Can't serialize a backed up room key");
584
585            backup
586                .entry(room_id)
587                .or_insert_with(|| RoomKeyBackup::new(BTreeMap::new()))
588                .sessions
589                .insert(session_id, session);
590        }
591
592        Ok((backup, session_record))
593    }
594
595    /// Import the given room keys into our store.
596    ///
597    /// # Arguments
598    ///
599    /// * `room_keys` - A list of previously exported keys that should be
600    ///   imported into our store. If we already have a better version of a key
601    ///   the key will *not* be imported.
602    ///
603    /// Returns a [`RoomKeyImportResult`] containing information about room keys
604    /// which were imported.
605    #[deprecated(note = "Use the OlmMachine::store::import_room_keys method instead")]
606    pub async fn import_backed_up_room_keys(
607        &self,
608        room_keys: BTreeMap<OwnedRoomId, BTreeMap<String, BackedUpRoomKey>>,
609        progress_listener: impl Fn(usize, usize),
610    ) -> Result<RoomKeyImportResult, CryptoStoreError> {
611        let mut decrypted_room_keys = vec![];
612
613        for (room_id, room_keys) in room_keys {
614            for (session_id, room_key) in room_keys {
615                let room_key = ExportedRoomKey::from_backed_up_room_key(
616                    room_id.to_owned(),
617                    session_id,
618                    room_key,
619                );
620
621                decrypted_room_keys.push(room_key);
622            }
623        }
624
625        // FIXME: This method is a bit flawed: we have no real idea which backup version
626        //   these keys came from. For example, we might have reset the backup
627        //   since the keys were downloaded. For now, let's assume they came from
628        //   the "current" backup version.
629        let backup_version = self.backup_version().await;
630
631        self.store
632            .import_room_keys(decrypted_room_keys, backup_version.as_deref(), progress_listener)
633            .await
634    }
635}
636
637#[cfg(test)]
638mod tests {
639    use std::collections::BTreeMap;
640
641    use assert_matches2::assert_let;
642    use matrix_sdk_test::async_test;
643    use ruma::{CanonicalJsonValue, DeviceId, RoomId, UserId, device_id, room_id, user_id};
644    use serde_json::json;
645
646    use super::BackupMachine;
647    use crate::{
648        OlmError, OlmMachine,
649        olm::BackedUpRoomKey,
650        store::{
651            CryptoStore, MemoryStore,
652            types::{BackupDecryptionKey, Changes},
653        },
654        types::RoomKeyBackupInfo,
655    };
656
657    fn room_key() -> BackedUpRoomKey {
658        let json = json!({
659            "algorithm": "m.megolm.v1.aes-sha2",
660            "sender_key": "DeHIg4gwhClxzFYcmNntPNF9YtsdZbmMy8+3kzCMXHA",
661            "session_key": "AQAAAABvWMNZjKFtebYIePKieQguozuoLgzeY6wKcyJjLJcJtQgy1dPqTBD12U+XrYLrRHn\
662                            lKmxoozlhFqJl456+9hlHCL+yq+6ScFuBHtJepnY1l2bdLb4T0JMDkNsNErkiLiLnD6yp3J\
663                            DSjIhkdHxmup/huygrmroq6/L5TaThEoqvW4DPIuO14btKudsS34FF82pwjKS4p6Mlch+0e\
664                            fHAblQV",
665            "sender_claimed_keys":{},
666            "forwarding_curve25519_key_chain":[]
667        });
668
669        serde_json::from_value(json)
670            .expect("We should be able to deserialize our backed up room key")
671    }
672
673    fn alice_id() -> &'static UserId {
674        user_id!("@alice:example.org")
675    }
676
677    fn alice_device_id() -> &'static DeviceId {
678        device_id!("JLAFKJWSCS")
679    }
680
681    fn room_id() -> &'static RoomId {
682        room_id!("!test:localhost")
683    }
684
685    fn room_id2() -> &'static RoomId {
686        room_id!("!test2:localhost")
687    }
688
689    async fn backup_flow(machine: OlmMachine) -> Result<(), OlmError> {
690        let backup_machine = machine.backup_machine();
691        let backup_version = current_backup_version(backup_machine).await;
692
693        let counts =
694            backup_machine.store.inbound_group_session_counts(backup_version.as_deref()).await?;
695
696        assert_eq!(counts.total, 0, "Initially no keys exist");
697        assert_eq!(counts.backed_up, 0, "Initially no backed up keys exist");
698
699        machine.create_outbound_group_session_with_defaults_test_helper(room_id()).await?;
700        machine.create_outbound_group_session_with_defaults_test_helper(room_id2()).await?;
701
702        let counts =
703            backup_machine.store.inbound_group_session_counts(backup_version.as_deref()).await?;
704        assert_eq!(counts.total, 2, "Two room keys need to exist in the store");
705        assert_eq!(counts.backed_up, 0, "No room keys have been backed up yet");
706
707        let decryption_key = BackupDecryptionKey::new();
708        let backup_key = decryption_key.megolm_v1_public_key();
709        backup_key.set_version("1".to_owned());
710
711        backup_machine.enable_backup_v1(backup_key).await?;
712
713        let (request_id, _) =
714            backup_machine.backup().await?.expect("Created a backup request successfully");
715        assert_eq!(
716            Some(&request_id),
717            backup_machine.backup().await?.as_ref().map(|(request_id, _)| request_id),
718            "Calling backup again without uploading creates the same backup request"
719        );
720
721        backup_machine.mark_request_as_sent(&request_id).await?;
722        let backup_version = current_backup_version(backup_machine).await;
723
724        let counts =
725            backup_machine.store.inbound_group_session_counts(backup_version.as_deref()).await?;
726        assert_eq!(counts.total, 2);
727        assert_eq!(counts.backed_up, 2, "All room keys have been backed up");
728
729        assert!(
730            backup_machine.backup().await?.is_none(),
731            "No room keys need to be backed up, no request needs to be created"
732        );
733
734        backup_machine.disable_backup().await?;
735        let backup_version = current_backup_version(backup_machine).await;
736
737        let counts =
738            backup_machine.store.inbound_group_session_counts(backup_version.as_deref()).await?;
739        assert_eq!(counts.total, 2);
740        assert_eq!(
741            counts.backed_up, 0,
742            "Disabling the backup resets the backup flag on the room keys"
743        );
744
745        Ok(())
746    }
747
748    async fn current_backup_version(backup_machine: &BackupMachine) -> Option<String> {
749        backup_machine.backup_key.read().await.as_ref().and_then(|k| k.backup_version())
750    }
751
752    #[async_test]
753    async fn test_memory_store_backups() -> Result<(), OlmError> {
754        let machine = OlmMachine::new(alice_id(), alice_device_id()).await;
755
756        backup_flow(machine).await
757    }
758
759    #[async_test]
760    async fn test_verify_auth_data() -> Result<(), OlmError> {
761        let machine = OlmMachine::new(alice_id(), alice_device_id()).await;
762        let backup_machine = machine.backup_machine();
763
764        let auth_data = json!({
765            "public_key":"XjhWTCjW7l59pbfx9tlCBQolfnIQWARoKOzjTOPSlWM",
766        });
767
768        let backup_version = json!({
769            "algorithm": "m.megolm_backup.v1.curve25519-aes-sha2",
770            "auth_data": auth_data,
771        });
772
773        let canonical_json: CanonicalJsonValue =
774            auth_data.clone().try_into().expect("Canonicalizing should always work");
775        let serialized = canonical_json.to_string();
776
777        let backup_version: RoomKeyBackupInfo = serde_json::from_value(backup_version).unwrap();
778
779        let state = backup_machine
780            .verify_backup(backup_version, false)
781            .await
782            .expect("Verifying should work");
783        assert!(!state.trusted());
784        assert!(!state.device_signature.trusted());
785        assert!(!state.user_identity_signature.trusted());
786        assert!(!state.other_signatures.values().any(|s| s.trusted()));
787
788        let signatures = machine.sign(&serialized).await?;
789
790        let backup_version = json!({
791            "algorithm": "m.megolm_backup.v1.curve25519-aes-sha2",
792            "auth_data": {
793                "public_key":"XjhWTCjW7l59pbfx9tlCBQolfnIQWARoKOzjTOPSlWM",
794                "signatures": signatures,
795            }
796        });
797        let backup_version: RoomKeyBackupInfo = serde_json::from_value(backup_version).unwrap();
798
799        let state = backup_machine
800            .verify_backup(backup_version, false)
801            .await
802            .expect("Verifying should work");
803
804        assert!(state.trusted());
805        assert!(state.device_signature.trusted());
806        assert!(!state.user_identity_signature.trusted());
807        assert!(!state.other_signatures.values().any(|s| s.trusted()));
808
809        machine
810            .bootstrap_cross_signing(true)
811            .await
812            .expect("Bootstrapping a new identity always works");
813
814        let signatures = machine.sign(&serialized).await?;
815
816        let backup_version = json!({
817            "algorithm": "m.megolm_backup.v1.curve25519-aes-sha2",
818            "auth_data": {
819                "public_key":"XjhWTCjW7l59pbfx9tlCBQolfnIQWARoKOzjTOPSlWM",
820                "signatures": signatures,
821            }
822        });
823        let backup_version: RoomKeyBackupInfo = serde_json::from_value(backup_version).unwrap();
824
825        let state = backup_machine
826            .verify_backup(backup_version, false)
827            .await
828            .expect("Verifying should work");
829
830        assert!(state.trusted());
831        assert!(state.device_signature.trusted());
832        assert!(state.user_identity_signature.trusted());
833        assert!(!state.other_signatures.values().any(|s| s.trusted()));
834
835        Ok(())
836    }
837
838    #[async_test]
839    async fn test_import_backed_up_room_keys() {
840        let machine = OlmMachine::new(alice_id(), alice_device_id()).await;
841        let backup_machine = machine.backup_machine();
842
843        // We set up a backup key, so that we can test `backup_machine.backup()` later.
844        let decryption_key = BackupDecryptionKey::new();
845        let backup_key = decryption_key.megolm_v1_public_key();
846        backup_key.set_version("1".to_owned());
847        backup_machine.enable_backup_v1(backup_key).await.expect("Couldn't enable backup");
848
849        let room_id = room_id!("!DovneieKSTkdHKpIXy:morpheus.localhost");
850        let session_id = "gM8i47Xhu0q52xLfgUXzanCMpLinoyVyH7R58cBuVBU";
851        let room_key = room_key();
852
853        let room_keys: BTreeMap<_, BTreeMap<_, _>> = BTreeMap::from([(
854            room_id.to_owned(),
855            BTreeMap::from([(session_id.to_owned(), room_key)]),
856        )]);
857
858        let session = machine.store().get_inbound_group_session(room_id, session_id).await.unwrap();
859
860        assert!(session.is_none(), "Initially we should not have the session in the store");
861
862        #[allow(deprecated)]
863        backup_machine
864            .import_backed_up_room_keys(room_keys, |_, _| {})
865            .await
866            .expect("We should be able to import a room key");
867
868        // Now check that the session was correctly imported, and that it is marked as
869        // backed up
870        let session = machine.store().get_inbound_group_session(room_id, session_id).await.unwrap();
871        assert_let!(Some(session) = session);
872        assert!(
873            session.backed_up(),
874            "If a session was imported from a backup, it should be considered to be backed up"
875        );
876        assert!(session.has_been_imported());
877
878        // Also check that it is not returned by a backup request.
879        let backup_request =
880            backup_machine.backup().await.expect("We should be able to create a backup request");
881        assert!(
882            backup_request.is_none(),
883            "If a session was imported from backup, it should not be backed up again."
884        );
885    }
886
887    #[async_test]
888    async fn test_sign_backup_info() {
889        let machine = OlmMachine::new(alice_id(), alice_device_id()).await;
890        let backup_machine = machine.backup_machine();
891
892        let decryption_key = BackupDecryptionKey::new();
893        let mut backup_info = decryption_key.to_backup_info();
894
895        let result = backup_machine.verify_backup(backup_info.to_owned(), false).await.unwrap();
896
897        assert!(!result.trusted());
898
899        backup_machine.sign_backup(&mut backup_info).await.unwrap();
900
901        let result = backup_machine.verify_backup(backup_info, false).await.unwrap();
902
903        assert!(result.trusted());
904    }
905
906    #[async_test]
907    async fn test_fix_backup_key_mismatch() {
908        let store = MemoryStore::new();
909
910        let backup_decryption_key = BackupDecryptionKey::new();
911
912        store
913            .save_changes(Changes {
914                backup_decryption_key: Some(backup_decryption_key.clone()),
915                backup_version: Some("1".to_owned()),
916                ..Default::default()
917            })
918            .await
919            .unwrap();
920
921        // Create the machine using `with_store` and without a call to enable_backup_v1,
922        // like regenerate_olm would do
923        let alice =
924            OlmMachine::with_store(alice_id(), alice_device_id(), store, None).await.unwrap();
925
926        let binding = alice.backup_machine().backup_key.read().await;
927        let machine_backup_key = binding.as_ref().unwrap();
928
929        assert_eq!(
930            machine_backup_key.to_base64(),
931            backup_decryption_key.megolm_v1_public_key().to_base64(),
932            "The OlmMachine loaded the wrong backup key."
933        );
934    }
935}