Skip to content

Commit 7b6132b

Browse files
committed
feat(crypto): Return the list of room keys we imported
Clients might want to retry decryption after they import room keys, for this to be efficient they'll need to know which room keys got imported. This patch extends the return value of the room key import result to include the map of room keys that got imported.
1 parent 10173b9 commit 7b6132b

File tree

3 files changed

+57
-18
lines changed

3 files changed

+57
-18
lines changed

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

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,10 @@ fn decrypt_helper(ciphertext: &str, passphrase: &str) -> Result<String, KeyExpor
235235

236236
#[cfg(test)]
237237
mod test {
238-
use std::io::Cursor;
238+
use std::{
239+
collections::{BTreeMap, BTreeSet},
240+
io::Cursor,
241+
};
239242

240243
use indoc::indoc;
241244
use matrix_sdk_test::async_test;
@@ -313,7 +316,7 @@ mod test {
313316
assert_eq!(export, decrypted);
314317
assert_eq!(
315318
machine.import_keys(decrypted, false, |_, _| {}).await.unwrap(),
316-
RoomKeyImportResult::new(0, 1)
319+
RoomKeyImportResult::new(0, 1, BTreeMap::new())
317320
);
318321
}
319322

@@ -325,29 +328,45 @@ mod test {
325328

326329
let export = vec![session.export_at_index(10).await];
327330

328-
assert_eq!(
329-
machine.import_keys(export.clone(), false, |_, _| {}).await?,
330-
RoomKeyImportResult::new(1, 1)
331+
let keys = RoomKeyImportResult::new(
332+
1,
333+
1,
334+
BTreeMap::from([(
335+
session.room_id().to_owned(),
336+
BTreeMap::from([(
337+
session.sender_key().to_owned(),
338+
BTreeSet::from([session.session_id().to_owned()]),
339+
)]),
340+
)]),
331341
);
342+
343+
assert_eq!(machine.import_keys(export.clone(), false, |_, _| {}).await?, keys,);
332344
assert_eq!(
333345
machine.import_keys(export, false, |_, _| {}).await?,
334-
RoomKeyImportResult::new(0, 1)
346+
RoomKeyImportResult::new(0, 1, BTreeMap::new())
335347
);
336348

337349
let better_export = vec![session.export().await];
338350

339-
assert_eq!(
340-
machine.import_keys(better_export, false, |_, _| {}).await?,
341-
RoomKeyImportResult::new(1, 1)
342-
);
351+
assert_eq!(machine.import_keys(better_export, false, |_, _| {}).await?, keys,);
343352

344353
let another_session = machine.create_inbound_session(&room_id).await?;
345354
let export = vec![another_session.export_at_index(10).await];
346-
assert_eq!(
347-
machine.import_keys(export, false, |_, _| {}).await?,
348-
RoomKeyImportResult::new(1, 1)
355+
356+
let keys = RoomKeyImportResult::new(
357+
1,
358+
1,
359+
BTreeMap::from([(
360+
another_session.room_id().to_owned(),
361+
BTreeMap::from([(
362+
another_session.sender_key().to_owned(),
363+
BTreeSet::from([another_session.session_id().to_owned()]),
364+
)]),
365+
)]),
349366
);
350367

368+
assert_eq!(machine.import_keys(export, false, |_, _| {}).await?, keys,);
369+
351370
Ok(())
352371
}
353372

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,31 @@ pub mod store;
4040
mod utilities;
4141
mod verification;
4242

43+
use std::collections::{BTreeMap, BTreeSet};
44+
45+
use ruma::RoomId;
46+
4347
/// Return type for the room key importing.
4448
#[derive(Debug, Clone, PartialEq)]
4549
pub struct RoomKeyImportResult {
4650
/// The number of room keys that were imported.
4751
pub imported_count: usize,
4852
/// The total number of room keys that were found in the export.
4953
pub total_count: usize,
54+
/// The map of keys that were imported.
55+
///
56+
/// It's a map from room id to a map of the sender key to a set of session
57+
/// ids.
58+
pub keys: BTreeMap<RoomId, BTreeMap<String, BTreeSet<String>>>,
5059
}
5160

5261
impl RoomKeyImportResult {
53-
pub(crate) fn new(imported_count: usize, total_count: usize) -> Self {
54-
Self { imported_count, total_count }
62+
pub(crate) fn new(
63+
imported_count: usize,
64+
total_count: usize,
65+
keys: BTreeMap<RoomId, BTreeMap<String, BTreeSet<String>>>,
66+
) -> Self {
67+
Self { imported_count, total_count, keys }
5568
}
5669
}
5770

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#[cfg(feature = "sled_cryptostore")]
1616
use std::path::Path;
1717
use std::{
18-
collections::{BTreeMap, HashSet},
18+
collections::{BTreeMap, BTreeSet, HashSet},
1919
mem,
2020
sync::Arc,
2121
};
@@ -1359,6 +1359,7 @@ impl OlmMachine {
13591359
};
13601360

13611361
let total_count = exported_keys.len();
1362+
let mut keys = BTreeMap::new();
13621363

13631364
for (i, key) in exported_keys.into_iter().enumerate() {
13641365
let session = InboundGroupSession::from_export(key)?;
@@ -1372,6 +1373,12 @@ impl OlmMachine {
13721373
session.mark_as_backed_up()
13731374
}
13741375

1376+
keys.entry(session.room_id().to_owned())
1377+
.or_insert_with(BTreeMap::new)
1378+
.entry(session.sender_key().to_owned())
1379+
.or_insert_with(BTreeSet::new)
1380+
.insert(session.session_id().to_owned());
1381+
13751382
sessions.push(session)
13761383
}
13771384

@@ -1384,9 +1391,9 @@ impl OlmMachine {
13841391

13851392
self.store.save_changes(changes).await?;
13861393

1387-
info!(total_count, imported_count, "Successfully imported room keys");
1394+
info!(total_count, imported_count, room_keys =? keys, "Successfully imported room keys");
13881395

1389-
Ok(RoomKeyImportResult::new(imported_count, total_count))
1396+
Ok(RoomKeyImportResult::new(imported_count, total_count, keys))
13901397
}
13911398

13921399
/// Export the keys that match the given predicate.

0 commit comments

Comments
 (0)