From 71136e44c03c79f80d6d1a2446673bc4d53a2067 Mon Sep 17 00:00:00 2001 From: Valere Date: Wed, 20 Dec 2023 16:38:54 +0100 Subject: [PATCH] Fix regenerate_olm_machine loosing backup state --- crates/matrix-sdk/src/encryption/mod.rs | 27 +++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/crates/matrix-sdk/src/encryption/mod.rs b/crates/matrix-sdk/src/encryption/mod.rs index bf2216a1bf4..386268845f5 100644 --- a/crates/matrix-sdk/src/encryption/mod.rs +++ b/crates/matrix-sdk/src/encryption/mod.rs @@ -1180,6 +1180,10 @@ impl Encryption { drop(olm_machine_guard); // Recreate the OlmMachine. self.client.base_client().regenerate_olm().await?; + // we need to trigger that so it gets back the cached key if known + // otherwise the new olm machine `BackupMachine#backup_key` will be out of sync + // and say backup is disabled. + self.client.encryption().backups().setup_and_resume().await?; } } Ok(()) @@ -1452,6 +1456,21 @@ mod tests { let initial_olm_machine = client1.olm_machine().await.clone().expect("must have an olm machine"); + // Also enable backup to check that new machine has the same backup keys. + let decryption_key = matrix_sdk_base::crypto::store::BackupDecryptionKey::new() + .expect("Can't create new recovery key"); + let backup_key = decryption_key.megolm_v1_public_key(); + backup_key.set_version("1".to_owned()); + initial_olm_machine + .backup_machine() + .save_decryption_key(Some(decryption_key.to_owned()), Some("1".to_owned())) + .await + .expect("Should save"); + + initial_olm_machine.backup_machine().enable_backup_v1(backup_key.clone()).await.unwrap(); + + assert!(client1.encryption().backups().are_enabled().await); + // The other client can't take the lock too. let acquired2 = client2.encryption().try_lock_store_once().await.unwrap(); assert!(acquired2.is_none()); @@ -1486,7 +1505,15 @@ mod tests { // But now its olm machine has been invalidated and thus regenerated! let olm_machine = client1.olm_machine().await.clone().expect("must have an olm machine"); + + let backup_key_new = olm_machine.backup_machine().get_backup_keys().await.unwrap(); assert!(!initial_olm_machine.same_as(&olm_machine)); + assert!(backup_key_new.decryption_key.is_some()); + assert_eq!( + backup_key_new.decryption_key.unwrap().megolm_v1_public_key().to_base64(), + backup_key.to_base64() + ); + assert!(client1.encryption().backups().are_enabled().await); } #[cfg(feature = "sqlite")]