Skip to content

Commit 923d457

Browse files
committed
crypto: add methods for room key bundles to store traits
1 parent 5ccbcfb commit 923d457

File tree

5 files changed

+102
-5
lines changed

5 files changed

+102
-5
lines changed

crates/matrix-sdk-crypto/src/store/integration_tests.rs

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ macro_rules! cryptostore_integration_tests {
5252
},
5353
store::{
5454
BackupDecryptionKey, Changes, CryptoStore, DehydratedDeviceKey, DeviceChanges, GossipRequest,
55-
IdentityChanges, PendingChanges, RoomSettings,
55+
IdentityChanges, PendingChanges, RoomSettings, StoredRoomKeyBundleData,
5656
},
5757
testing::{get_device, get_other_identity, get_own_identity},
5858
types::{
@@ -64,6 +64,7 @@ macro_rules! cryptostore_integration_tests {
6464
CommonWithheldCodeContent, MegolmV1AesSha2WithheldContent,
6565
RoomKeyWithheldContent,
6666
},
67+
room_key_bundle::RoomKeyBundleContent,
6768
secret_send::SecretSendContent,
6869
ToDeviceEvent,
6970
},
@@ -1276,6 +1277,57 @@ macro_rules! cryptostore_integration_tests {
12761277
assert_eq!(None, loaded_2);
12771278
}
12781279

1280+
#[async_test]
1281+
#[ignore] // not yet implemented for all stores
1282+
async fn test_received_room_key_bundle() {
1283+
let store = get_store("received_room_key_bundle", None, true).await;
1284+
let test_room = room_id!("!room:example.org");
1285+
1286+
fn make_bundle_data(sender_user: &UserId, bundle_uri: &str) -> StoredRoomKeyBundleData {
1287+
let jwk = ruma::events::room::JsonWebKeyInit {
1288+
kty: "oct".to_owned(),
1289+
key_ops: vec!["encrypt".to_owned(), "decrypt".to_owned()],
1290+
alg: "A256CTR".to_owned(),
1291+
k: ruma::serde::Base64::new(vec![0u8; 0]),
1292+
ext: true,
1293+
}.into();
1294+
1295+
let file = ruma::events::room::EncryptedFileInit {
1296+
url: ruma::OwnedMxcUri::from(bundle_uri),
1297+
key: jwk,
1298+
iv: ruma::serde::Base64::new(vec![0u8; 0]),
1299+
hashes: Default::default(),
1300+
v: "".to_owned(),
1301+
}.into();
1302+
1303+
StoredRoomKeyBundleData {
1304+
sender_user: sender_user.to_owned(),
1305+
sender_data: SenderData::unknown(),
1306+
bundle_data: RoomKeyBundleContent {
1307+
room_id: room_id!("!room:example.org").to_owned(),
1308+
file,
1309+
},
1310+
}
1311+
}
1312+
1313+
// Add three entries
1314+
let changes = Changes {
1315+
received_room_key_bundles: vec![
1316+
make_bundle_data(user_id!("@alice:example.com"), "alice1"),
1317+
make_bundle_data(user_id!("@bob:example.com"), "bob1"),
1318+
make_bundle_data(user_id!("@alice:example.com"), "alice2"),
1319+
],
1320+
..Default::default()
1321+
};
1322+
store.save_changes(changes).await.unwrap();
1323+
1324+
// Check we get the right one
1325+
let bundle = store.get_received_room_key_bundle_data(
1326+
test_room, user_id!("@alice:example.com")
1327+
).await.unwrap().expect("Did not get any bundle data");
1328+
assert_eq!(bundle.bundle_data.file.url.to_string(), "alice2");
1329+
}
1330+
12791331
fn session_info(session: &InboundGroupSession) -> (&RoomId, &str) {
12801332
(&session.room_id(), &session.session_id())
12811333
}

crates/matrix-sdk-crypto/src/store/memorystore.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ use vodozemac::Curve25519PublicKey;
3333
use super::{
3434
caches::DeviceStore, Account, BackupKeys, Changes, CryptoStore, DehydratedDeviceKey,
3535
InboundGroupSession, PendingChanges, RoomKeyCounts, RoomSettings, Session,
36+
StoredRoomKeyBundleData,
3637
};
3738
use crate::{
3839
gossiping::{GossipRequest, GossippedSecret, SecretInfo},
@@ -719,6 +720,14 @@ impl CryptoStore for MemoryStore {
719720
Ok(self.room_settings.read().get(room_id).cloned())
720721
}
721722

723+
async fn get_received_room_key_bundle_data(
724+
&self,
725+
_room_id: &RoomId,
726+
_user_id: &UserId,
727+
) -> Result<Option<StoredRoomKeyBundleData>> {
728+
todo!()
729+
}
730+
722731
async fn get_custom_value(&self, key: &str) -> Result<Option<Vec<u8>>> {
723732
Ok(self.custom_values.read().get(key).cloned())
724733
}
@@ -1249,7 +1258,7 @@ mod integration_tests {
12491258
},
12501259
store::{
12511260
BackupKeys, Changes, CryptoStore, DehydratedDeviceKey, PendingChanges, RoomKeyCounts,
1252-
RoomSettings,
1261+
RoomSettings, StoredRoomKeyBundleData,
12531262
},
12541263
types::events::room_key_withheld::RoomKeyWithheldEvent,
12551264
Account, DeviceData, GossipRequest, GossippedSecret, SecretInfo, Session, TrackedUser,
@@ -1511,6 +1520,14 @@ mod integration_tests {
15111520
self.0.get_room_settings(room_id).await
15121521
}
15131522

1523+
async fn get_received_room_key_bundle_data(
1524+
&self,
1525+
room_id: &RoomId,
1526+
user_id: &UserId,
1527+
) -> crate::store::Result<Option<StoredRoomKeyBundleData>, Self::Error> {
1528+
self.0.get_received_room_key_bundle_data(room_id, user_id).await
1529+
}
1530+
15141531
async fn get_custom_value(&self, key: &str) -> Result<Option<Vec<u8>>, Self::Error> {
15151532
self.0.get_custom_value(key).await
15161533
}

crates/matrix-sdk-crypto/src/store/traits.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use vodozemac::Curve25519PublicKey;
2323

2424
use super::{
2525
BackupKeys, Changes, CryptoStoreError, DehydratedDeviceKey, PendingChanges, Result,
26-
RoomKeyCounts, RoomSettings,
26+
RoomKeyCounts, RoomSettings, StoredRoomKeyBundleData,
2727
};
2828
#[cfg(doc)]
2929
use crate::olm::SenderData;
@@ -323,6 +323,14 @@ pub trait CryptoStore: AsyncTraitDeps {
323323
room_id: &RoomId,
324324
) -> Result<Option<RoomSettings>, Self::Error>;
325325

326+
/// Get the details about the room key bundle data received from the given
327+
/// user for the given room.
328+
async fn get_received_room_key_bundle_data(
329+
&self,
330+
room_id: &RoomId,
331+
user_id: &UserId,
332+
) -> Result<Option<StoredRoomKeyBundleData>, Self::Error>;
333+
326334
/// Get arbitrary data from the store
327335
///
328336
/// # Arguments
@@ -569,6 +577,14 @@ impl<T: CryptoStore> CryptoStore for EraseCryptoStoreError<T> {
569577
self.0.get_room_settings(room_id).await.map_err(Into::into)
570578
}
571579

580+
async fn get_received_room_key_bundle_data(
581+
&self,
582+
room_id: &RoomId,
583+
user_id: &UserId,
584+
) -> Result<Option<StoredRoomKeyBundleData>> {
585+
self.0.get_received_room_key_bundle_data(room_id, user_id).await.map_err(Into::into)
586+
}
587+
572588
async fn get_custom_value(&self, key: &str) -> Result<Option<Vec<u8>>, Self::Error> {
573589
self.0.get_custom_value(key).await.map_err(Into::into)
574590
}

crates/matrix-sdk-indexeddb/src/crypto_store/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use matrix_sdk_crypto::{
3030
},
3131
store::{
3232
BackupKeys, Changes, CryptoStore, CryptoStoreError, DehydratedDeviceKey, PendingChanges,
33-
RoomKeyCounts, RoomSettings,
33+
RoomKeyCounts, RoomSettings, StoredRoomKeyBundleData,
3434
},
3535
types::events::room_key_withheld::RoomKeyWithheldEvent,
3636
vodozemac::base64_encode,
@@ -1359,6 +1359,10 @@ impl_crypto_store! {
13591359
.transpose()
13601360
}
13611361

1362+
async fn get_received_room_key_bundle_data(&self, _room_id: &RoomId, _user_id: &UserId) -> Result<Option<StoredRoomKeyBundleData>> {
1363+
todo!()
1364+
}
1365+
13621366
async fn get_custom_value(&self, key: &str) -> Result<Option<Vec<u8>>> {
13631367
self
13641368
.inner

crates/matrix-sdk-sqlite/src/crypto_store.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use matrix_sdk_crypto::{
2929
},
3030
store::{
3131
BackupKeys, Changes, CryptoStore, DehydratedDeviceKey, PendingChanges, RoomKeyCounts,
32-
RoomSettings,
32+
RoomSettings, StoredRoomKeyBundleData,
3333
},
3434
types::events::room_key_withheld::RoomKeyWithheldEvent,
3535
Account, DeviceData, GossipRequest, GossippedSecret, SecretInfo, TrackedUser, UserIdentityData,
@@ -1337,6 +1337,14 @@ impl CryptoStore for SqliteCryptoStore {
13371337
return Ok(Some(settings));
13381338
}
13391339

1340+
async fn get_received_room_key_bundle_data(
1341+
&self,
1342+
_room_id: &RoomId,
1343+
_user_id: &UserId,
1344+
) -> Result<Option<StoredRoomKeyBundleData>> {
1345+
todo!()
1346+
}
1347+
13401348
async fn get_custom_value(&self, key: &str) -> Result<Option<Vec<u8>>> {
13411349
let Some(serialized) = self.acquire().await?.get_kv(key).await? else {
13421350
return Ok(None);

0 commit comments

Comments
 (0)