Skip to content

Commit fc74526

Browse files
committed
fix(crypto): Add the ability to mark room keys as backed up when importing
1 parent 55973b5 commit fc74526

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

crates/matrix-sdk-crypto/src/file_encryption/key_export.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ pub enum KeyExportError {
8383
/// # block_on(async {
8484
/// # let export = Cursor::new("".to_owned());
8585
/// let exported_keys = decrypt_key_export(export, "1234").unwrap();
86-
/// machine.import_keys(exported_keys, |_, _| {}).await.unwrap();
86+
/// machine.import_keys(exported_keys, false, |_, _| {}).await.unwrap();
8787
/// # });
8888
/// ```
8989
pub fn decrypt_key_export(
@@ -312,7 +312,7 @@ mod test {
312312

313313
assert_eq!(export, decrypted);
314314
assert_eq!(
315-
machine.import_keys(decrypted, |_, _| {}).await.unwrap(),
315+
machine.import_keys(decrypted, false, |_, _| {}).await.unwrap(),
316316
RoomKeyImportResult::new(0, 1)
317317
);
318318
}
@@ -326,21 +326,27 @@ mod test {
326326
let export = vec![session.export_at_index(10).await];
327327

328328
assert_eq!(
329-
machine.import_keys(export.clone(), |_, _| {}).await?,
329+
machine.import_keys(export.clone(), false, |_, _| {}).await?,
330330
RoomKeyImportResult::new(1, 1)
331331
);
332-
assert_eq!(machine.import_keys(export, |_, _| {}).await?, RoomKeyImportResult::new(0, 1));
332+
assert_eq!(
333+
machine.import_keys(export, false, |_, _| {}).await?,
334+
RoomKeyImportResult::new(0, 1)
335+
);
333336

334337
let better_export = vec![session.export().await];
335338

336339
assert_eq!(
337-
machine.import_keys(better_export, |_, _| {}).await?,
340+
machine.import_keys(better_export, false, |_, _| {}).await?,
338341
RoomKeyImportResult::new(1, 1)
339342
);
340343

341344
let another_session = machine.create_inbound_session(&room_id).await?;
342345
let export = vec![another_session.export_at_index(10).await];
343-
assert_eq!(machine.import_keys(export, |_, _| {}).await?, RoomKeyImportResult::new(1, 1));
346+
assert_eq!(
347+
machine.import_keys(export, false, |_, _| {}).await?,
348+
RoomKeyImportResult::new(1, 1)
349+
);
344350

345351
Ok(())
346352
}

crates/matrix-sdk-crypto/src/machine.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1290,6 +1290,10 @@ impl OlmMachine {
12901290
/// imported into our store. If we already have a better version of a key
12911291
/// the key will *not* be imported.
12921292
///
1293+
/// * `from_backup` - Were the room keys imported from the backup, if true
1294+
/// will mark the room keys as already backed up. This will prevent backing
1295+
/// up keys that are already backed up.
1296+
///
12931297
/// Returns a tuple of numbers that represent the number of sessions that
12941298
/// were imported and the total number of sessions that were found in the
12951299
/// key export.
@@ -1305,12 +1309,13 @@ impl OlmMachine {
13051309
/// # block_on(async {
13061310
/// # let export = Cursor::new("".to_owned());
13071311
/// let exported_keys = decrypt_key_export(export, "1234").unwrap();
1308-
/// machine.import_keys(exported_keys, |_, _| {}).await.unwrap();
1312+
/// machine.import_keys(exported_keys, false, |_, _| {}).await.unwrap();
13091313
/// # });
13101314
/// ```
13111315
pub async fn import_keys(
13121316
&self,
13131317
exported_keys: Vec<ExportedRoomKey>,
1318+
#[allow(unused_variables)] from_backup: bool,
13141319
progress_listener: impl Fn(usize, usize),
13151320
) -> StoreResult<RoomKeyImportResult> {
13161321
type SessionIdToIndexMap = BTreeMap<Arc<str>, u32>;
@@ -1362,6 +1367,11 @@ impl OlmMachine {
13621367
// a better version of the same session, that is the first known
13631368
// index is lower.
13641369
if !existing_sessions.has_better_session(&session) {
1370+
#[cfg(feature = "backups_v1")]
1371+
if from_backup {
1372+
session.mark_as_backed_up()
1373+
}
1374+
13651375
sessions.push(session)
13661376
}
13671377

crates/matrix-sdk/src/encryption/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,7 @@ impl Client {
672672
let task = tokio::task::spawn_blocking(decrypt);
673673
let import = task.await.expect("Task join error")?;
674674

675-
Ok(olm.import_keys(import, |_, _| {}).await?)
675+
Ok(olm.import_keys(import, false, |_, _| {}).await?)
676676
}
677677

678678
/// Tries to decrypt a `AnyRoomEvent`. Returns unencrypted room event when

0 commit comments

Comments
 (0)