fix(crypto): Add the ability to mark room keys as backed up when importing

This commit is contained in:
Damir Jelić
2021-11-25 10:23:12 +01:00
parent 55973b5bf1
commit fc74526699
3 changed files with 24 additions and 8 deletions

View File

@@ -83,7 +83,7 @@ pub enum KeyExportError {
/// # block_on(async {
/// # let export = Cursor::new("".to_owned());
/// let exported_keys = decrypt_key_export(export, "1234").unwrap();
/// machine.import_keys(exported_keys, |_, _| {}).await.unwrap();
/// machine.import_keys(exported_keys, false, |_, _| {}).await.unwrap();
/// # });
/// ```
pub fn decrypt_key_export(
@@ -312,7 +312,7 @@ mod test {
assert_eq!(export, decrypted);
assert_eq!(
machine.import_keys(decrypted, |_, _| {}).await.unwrap(),
machine.import_keys(decrypted, false, |_, _| {}).await.unwrap(),
RoomKeyImportResult::new(0, 1)
);
}
@@ -326,21 +326,27 @@ mod test {
let export = vec![session.export_at_index(10).await];
assert_eq!(
machine.import_keys(export.clone(), |_, _| {}).await?,
machine.import_keys(export.clone(), false, |_, _| {}).await?,
RoomKeyImportResult::new(1, 1)
);
assert_eq!(machine.import_keys(export, |_, _| {}).await?, RoomKeyImportResult::new(0, 1));
assert_eq!(
machine.import_keys(export, false, |_, _| {}).await?,
RoomKeyImportResult::new(0, 1)
);
let better_export = vec![session.export().await];
assert_eq!(
machine.import_keys(better_export, |_, _| {}).await?,
machine.import_keys(better_export, false, |_, _| {}).await?,
RoomKeyImportResult::new(1, 1)
);
let another_session = machine.create_inbound_session(&room_id).await?;
let export = vec![another_session.export_at_index(10).await];
assert_eq!(machine.import_keys(export, |_, _| {}).await?, RoomKeyImportResult::new(1, 1));
assert_eq!(
machine.import_keys(export, false, |_, _| {}).await?,
RoomKeyImportResult::new(1, 1)
);
Ok(())
}

View File

@@ -1290,6 +1290,10 @@ impl OlmMachine {
/// imported into our store. If we already have a better version of a key
/// the key will *not* be imported.
///
/// * `from_backup` - Were the room keys imported from the backup, if true
/// will mark the room keys as already backed up. This will prevent backing
/// up keys that are already backed up.
///
/// Returns a tuple of numbers that represent the number of sessions that
/// were imported and the total number of sessions that were found in the
/// key export.
@@ -1305,12 +1309,13 @@ impl OlmMachine {
/// # block_on(async {
/// # let export = Cursor::new("".to_owned());
/// let exported_keys = decrypt_key_export(export, "1234").unwrap();
/// machine.import_keys(exported_keys, |_, _| {}).await.unwrap();
/// machine.import_keys(exported_keys, false, |_, _| {}).await.unwrap();
/// # });
/// ```
pub async fn import_keys(
&self,
exported_keys: Vec<ExportedRoomKey>,
#[allow(unused_variables)] from_backup: bool,
progress_listener: impl Fn(usize, usize),
) -> StoreResult<RoomKeyImportResult> {
type SessionIdToIndexMap = BTreeMap<Arc<str>, u32>;
@@ -1362,6 +1367,11 @@ impl OlmMachine {
// a better version of the same session, that is the first known
// index is lower.
if !existing_sessions.has_better_session(&session) {
#[cfg(feature = "backups_v1")]
if from_backup {
session.mark_as_backed_up()
}
sessions.push(session)
}

View File

@@ -672,7 +672,7 @@ impl Client {
let task = tokio::task::spawn_blocking(decrypt);
let import = task.await.expect("Task join error")?;
Ok(olm.import_keys(import, |_, _| {}).await?)
Ok(olm.import_keys(import, false, |_, _| {}).await?)
}
/// Tries to decrypt a `AnyRoomEvent`. Returns unencrypted room event when