Skip to content

Commit 5d9fdd8

Browse files
committed
feat: Add RoomKeyWithheldEntry to wrap to-device and bundle payloads.
1 parent 681b221 commit 5d9fdd8

File tree

9 files changed

+63
-29
lines changed

9 files changed

+63
-29
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -998,7 +998,7 @@ impl OlmMachine {
998998
Ok(())
999999
}
10001000

1001-
fn add_withheld_info(&self, changes: &mut Changes, event: &RoomKeyWithheldEvent) {
1001+
pub(crate) fn add_withheld_info(&self, changes: &mut Changes, event: &RoomKeyWithheldEvent) {
10021002
debug!(?event.content, "Processing `m.room_key.withheld` event");
10031003

10041004
if let RoomKeyWithheldContent::MegolmV1AesSha2(
@@ -1010,7 +1010,7 @@ impl OlmMachine {
10101010
.withheld_session_info
10111011
.entry(c.room_id.to_owned())
10121012
.or_default()
1013-
.insert(c.session_id.to_owned(), event.to_owned());
1013+
.insert(c.session_id.to_owned(), event.to_owned().into());
10141014
}
10151015
}
10161016

@@ -2029,7 +2029,7 @@ impl OlmMachine {
20292029
.store
20302030
.get_withheld_info(room_id, content.session_id())
20312031
.await?
2032-
.map(|e| e.content.withheld_code());
2032+
.map(|e| e.content().withheld_code());
20332033

20342034
if withheld_code.is_some() {
20352035
// Partially withheld, report with a withheld code if we have one.
@@ -2145,7 +2145,7 @@ impl OlmMachine {
21452145
.store
21462146
.get_withheld_info(room_id, session_id)
21472147
.await?
2148-
.map(|e| e.content.withheld_code());
2148+
.map(|e| e.content().withheld_code());
21492149
Err(MegolmError::MissingRoomKey(withheld_code))
21502150
}
21512151
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1065,7 +1065,7 @@ async fn test_withheld_unverified() {
10651065

10661066
assert_eq!(&withheld_received[0].room_id, room_id);
10671067
assert_matches!(
1068-
&withheld_received[0].withheld_event.content,
1068+
&withheld_received[0].withheld_event.content(),
10691069
RoomKeyWithheldContent::MegolmV1AesSha2(MegolmV1AesSha2WithheldContent::Unverified(
10701070
unverified_withheld_content
10711071
))

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ macro_rules! cryptostore_integration_tests {
6666
room_key_withheld::{
6767
CommonWithheldCodeContent, MegolmV1AesSha2WithheldContent,
6868
RoomKeyWithheldContent,
69+
RoomKeyWithheldEntry,
6970
},
7071
room_key_bundle::RoomKeyBundleContent,
7172
secret_send::SecretSendContent,
@@ -1138,7 +1139,7 @@ macro_rules! cryptostore_integration_tests {
11381139
async fn test_withheld_info_storage() {
11391140
let (account, store) = get_loaded_store("withheld_info_storage").await;
11401141

1141-
let mut info_list: BTreeMap<_, BTreeMap<_, _>> = BTreeMap::new();
1142+
let mut info_list: BTreeMap<_, BTreeMap<_, RoomKeyWithheldEntry>> = BTreeMap::new();
11421143

11431144
let user_id = account.user_id().to_owned();
11441145
let room_id = room_id!("!DwLygpkclUAfQNnfva:example.com");
@@ -1163,7 +1164,7 @@ macro_rules! cryptostore_integration_tests {
11631164
info_list
11641165
.entry(room_id.to_owned())
11651166
.or_default()
1166-
.insert(session_id_1.to_owned(), event);
1167+
.insert(session_id_1.to_owned(), event.into());
11671168

11681169
let content = RoomKeyWithheldContent::MegolmV1AesSha2(
11691170
MegolmV1AesSha2WithheldContent::BlackListed(
@@ -1183,7 +1184,7 @@ macro_rules! cryptostore_integration_tests {
11831184
info_list
11841185
.entry(room_id.to_owned())
11851186
.or_default()
1186-
.insert(session_id_2.to_owned(), event);
1187+
.insert(session_id_2.to_owned(), event.into());
11871188

11881189
let changes = Changes { withheld_session_info: info_list, ..Default::default() };
11891190
store.save_changes(changes).await.unwrap();
@@ -1192,16 +1193,16 @@ macro_rules! cryptostore_integration_tests {
11921193

11931194
assert_matches!(
11941195
is_withheld, Some(event)
1195-
if event.content.algorithm() == EventEncryptionAlgorithm::MegolmV1AesSha2 &&
1196-
event.content.withheld_code() == WithheldCode::Unverified
1196+
if event.content().algorithm() == EventEncryptionAlgorithm::MegolmV1AesSha2 &&
1197+
event.content().withheld_code() == WithheldCode::Unverified
11971198
);
11981199

11991200
let is_withheld = store.get_withheld_info(room_id, session_id_2).await.unwrap();
12001201

12011202
assert_matches!(
12021203
is_withheld, Some(event)
1203-
if event.content.algorithm() == EventEncryptionAlgorithm::MegolmV1AesSha2 &&
1204-
event.content.withheld_code() == WithheldCode::Blacklisted
1204+
if event.content().algorithm() == EventEncryptionAlgorithm::MegolmV1AesSha2 &&
1205+
event.content().withheld_code() == WithheldCode::Blacklisted
12051206
);
12061207

12071208
let other_room_id = room_id!("!nQRyiRFuyUhXeaQfiR:example.com");

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ use crate::{
4545
OutboundGroupSession, PickledAccount, PickledInboundGroupSession, PickledSession,
4646
PrivateCrossSigningIdentity, SenderDataType, StaticAccountData,
4747
},
48-
types::events::room_key_withheld::RoomKeyWithheldEvent,
48+
types::events::room_key_withheld::RoomKeyWithheldEntry,
4949
};
5050

5151
fn encode_key_info(info: &SecretInfo) -> String {
@@ -97,7 +97,7 @@ pub struct MemoryStore {
9797
identities: StdRwLock<HashMap<OwnedUserId, String>>,
9898
outgoing_key_requests: StdRwLock<HashMap<OwnedTransactionId, GossipRequest>>,
9999
key_requests_by_info: StdRwLock<HashMap<String, OwnedTransactionId>>,
100-
direct_withheld_info: StdRwLock<HashMap<OwnedRoomId, HashMap<String, RoomKeyWithheldEvent>>>,
100+
direct_withheld_info: StdRwLock<HashMap<OwnedRoomId, HashMap<String, RoomKeyWithheldEntry>>>,
101101
custom_values: StdRwLock<HashMap<String, Vec<u8>>>,
102102
leases: StdRwLock<HashMap<String, (String, Instant)>>,
103103
secret_inbox: StdRwLock<HashMap<String, Vec<GossippedSecret>>>,
@@ -420,7 +420,7 @@ impl CryptoStore for MemoryStore {
420420
&self,
421421
room_id: &RoomId,
422422
session_id: &str,
423-
) -> Result<Option<RoomKeyWithheldEvent>> {
423+
) -> Result<Option<RoomKeyWithheldEntry>> {
424424
Ok(self
425425
.direct_withheld_info
426426
.read()
@@ -1276,7 +1276,7 @@ mod integration_tests {
12761276
},
12771277
CryptoStore,
12781278
},
1279-
types::events::room_key_withheld::RoomKeyWithheldEvent,
1279+
types::events::room_key_withheld::RoomKeyWithheldEntry,
12801280
Account, DeviceData, GossipRequest, GossippedSecret, SecretInfo, Session, UserIdentityData,
12811281
};
12821282

@@ -1372,7 +1372,7 @@ mod integration_tests {
13721372
&self,
13731373
room_id: &RoomId,
13741374
session_id: &str,
1375-
) -> Result<Option<RoomKeyWithheldEvent>, Self::Error> {
1375+
) -> Result<Option<RoomKeyWithheldEntry>, Self::Error> {
13761376
self.0.get_withheld_info(room_id, session_id).await
13771377
}
13781378

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use crate::{
3535
InboundGroupSession, OlmMessageHash, OutboundGroupSession, PrivateCrossSigningIdentity,
3636
SenderDataType, Session,
3737
},
38-
types::events::room_key_withheld::RoomKeyWithheldEvent,
38+
types::events::room_key_withheld::RoomKeyWithheldEntry,
3939
Account, DeviceData, GossipRequest, GossippedSecret, SecretInfo, UserIdentityData,
4040
};
4141

@@ -116,7 +116,7 @@ pub trait CryptoStore: AsyncTraitDeps {
116116
&self,
117117
room_id: &RoomId,
118118
session_id: &str,
119-
) -> Result<Option<RoomKeyWithheldEvent>, Self::Error>;
119+
) -> Result<Option<RoomKeyWithheldEntry>, Self::Error>;
120120

121121
/// Get all the inbound group sessions we have stored.
122122
async fn get_inbound_group_sessions(&self) -> Result<Vec<InboundGroupSession>, Self::Error>;
@@ -588,7 +588,7 @@ impl<T: CryptoStore> CryptoStore for EraseCryptoStoreError<T> {
588588
&self,
589589
room_id: &RoomId,
590590
session_id: &str,
591-
) -> Result<Option<RoomKeyWithheldEvent>, Self::Error> {
591+
) -> Result<Option<RoomKeyWithheldEntry>, Self::Error> {
592592
self.0.get_withheld_info(room_id, session_id).await.map_err(Into::into)
593593
}
594594

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use crate::{
3434
SenderData,
3535
},
3636
types::{
37-
events::{room_key_bundle::RoomKeyBundleContent, room_key_withheld::RoomKeyWithheldEvent},
37+
events::{room_key_bundle::RoomKeyBundleContent, room_key_withheld::RoomKeyWithheldEntry},
3838
EventEncryptionAlgorithm,
3939
},
4040
Account, Device, DeviceData, GossippedSecret, Session, UserIdentity, UserIdentityData,
@@ -75,7 +75,7 @@ pub struct Changes {
7575
pub identities: IdentityChanges,
7676
pub devices: DeviceChanges,
7777
/// Stores when a `m.room_key.withheld` is received
78-
pub withheld_session_info: BTreeMap<OwnedRoomId, BTreeMap<String, RoomKeyWithheldEvent>>,
78+
pub withheld_session_info: BTreeMap<OwnedRoomId, BTreeMap<String, RoomKeyWithheldEntry>>,
7979
pub room_settings: HashMap<OwnedRoomId, RoomSettings>,
8080
pub secrets: Vec<GossippedSecret>,
8181
pub next_batch_token: Option<String>,
@@ -486,7 +486,7 @@ pub struct RoomKeyWithheldInfo {
486486

487487
/// The `m.room_key.withheld` event that notified us that the key is being
488488
/// withheld.
489-
pub withheld_event: RoomKeyWithheldEvent,
489+
pub withheld_event: RoomKeyWithheldEntry,
490490
}
491491

492492
/// Information about a received historic room key bundle.

crates/matrix-sdk-crypto/src/types/events/room_key_withheld.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,39 @@ use vodozemac::Curve25519PublicKey;
2525
use super::{EventType, ToDeviceEvent};
2626
use crate::types::{deserialize_curve_key, serialize_curve_key, EventEncryptionAlgorithm};
2727

28+
/// Represents an entry for a withheld room key event, which can be either a
29+
/// to-device event or a bundle entry.
30+
#[derive(Clone, Debug, Serialize, Deserialize)]
31+
#[serde(untagged)]
32+
pub enum RoomKeyWithheldEntry {
33+
/// A to-device event containing withheld room key information.
34+
ToDevice(RoomKeyWithheldEvent),
35+
/// Content held within a room key bundle.
36+
Bundle(RoomKeyWithheldContent),
37+
}
38+
39+
impl RoomKeyWithheldEntry {
40+
/// Returns a reference to the underlying `RoomKeyWithheldContent`.
41+
pub fn content(&self) -> &RoomKeyWithheldContent {
42+
match self {
43+
RoomKeyWithheldEntry::ToDevice(ev) => &ev.content,
44+
RoomKeyWithheldEntry::Bundle(content) => content,
45+
}
46+
}
47+
}
48+
49+
impl From<RoomKeyWithheldEvent> for RoomKeyWithheldEntry {
50+
fn from(value: RoomKeyWithheldEvent) -> Self {
51+
Self::ToDevice(value)
52+
}
53+
}
54+
55+
impl From<RoomKeyWithheldContent> for RoomKeyWithheldEntry {
56+
fn from(value: RoomKeyWithheldContent) -> Self {
57+
Self::Bundle(value)
58+
}
59+
}
60+
2861
/// The `m.room_key_request` to-device event.
2962
pub type RoomKeyWithheldEvent = ToDeviceEvent<RoomKeyWithheldContent>;
3063

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use matrix_sdk_crypto::{
3535
},
3636
CryptoStore, CryptoStoreError,
3737
},
38-
types::events::room_key_withheld::RoomKeyWithheldEvent,
38+
types::events::room_key_withheld::RoomKeyWithheldEntry,
3939
vodozemac::base64_encode,
4040
Account, DeviceData, GossipRequest, GossippedSecret, SecretInfo, TrackedUser, UserIdentityData,
4141
};
@@ -1386,7 +1386,7 @@ impl_crypto_store! {
13861386
&self,
13871387
room_id: &RoomId,
13881388
session_id: &str,
1389-
) -> Result<Option<RoomKeyWithheldEvent>> {
1389+
) -> Result<Option<RoomKeyWithheldEntry>> {
13901390
let key = self.serializer.encode_key(keys::DIRECT_WITHHELD_INFO, (session_id, room_id));
13911391
if let Some(pickle) = self
13921392
.inner

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use matrix_sdk_crypto::{
3333
},
3434
CryptoStore,
3535
},
36-
types::events::room_key_withheld::RoomKeyWithheldEvent,
36+
types::events::room_key_withheld::RoomKeyWithheldEntry,
3737
Account, DeviceData, GossipRequest, GossippedSecret, SecretInfo, TrackedUser, UserIdentityData,
3838
};
3939
use matrix_sdk_store_encryption::StoreCipher;
@@ -1379,7 +1379,7 @@ impl CryptoStore for SqliteCryptoStore {
13791379
&self,
13801380
room_id: &RoomId,
13811381
session_id: &str,
1382-
) -> Result<Option<RoomKeyWithheldEvent>> {
1382+
) -> Result<Option<RoomKeyWithheldEntry>> {
13831383
let room_id = self.encode_key("direct_withheld_info", room_id);
13841384
let session_id = self.encode_key("direct_withheld_info", session_id);
13851385

@@ -1388,7 +1388,7 @@ impl CryptoStore for SqliteCryptoStore {
13881388
.get_direct_withheld_info(session_id, room_id)
13891389
.await?
13901390
.map(|value| {
1391-
let info = self.deserialize_json::<RoomKeyWithheldEvent>(&value)?;
1391+
let info = self.deserialize_json::<RoomKeyWithheldEntry>(&value)?;
13921392
Ok(info)
13931393
})
13941394
.transpose()
@@ -1894,7 +1894,7 @@ mod tests {
18941894
.expect("This session should be withheld")
18951895
.unwrap();
18961896

1897-
assert_eq!(withheld_info.content.withheld_code(), WithheldCode::Unverified);
1897+
assert_eq!(withheld_info.content().withheld_code(), WithheldCode::Unverified);
18981898

18991899
let backup_keys = database.load_backup_keys().await.expect("backup key should be cached");
19001900
assert_eq!(backup_keys.backup_version.unwrap(), "6");

0 commit comments

Comments
 (0)