Skip to content

Commit cd6fe27

Browse files
zecakehpoljar
authored andcommitted
refactor(crypto): Make deprecated sender_key and device_id optional in RoomEncryptedEventContent and RoomKeyRequestContent
They were deprecated in Matrix 1.3 and are now optional. Signed-off-by: Kévin Commaille <[email protected]>
1 parent 5f447bb commit cd6fe27

File tree

6 files changed

+109
-13
lines changed

6 files changed

+109
-13
lines changed

crates/matrix-sdk-crypto/src/olm/group_sessions/outbound.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -499,9 +499,9 @@ impl OutboundGroupSession {
499499
let scheme: RoomEventEncryptionScheme = match self.settings.algorithm {
500500
EventEncryptionAlgorithm::MegolmV1AesSha2 => MegolmV1AesSha2Content {
501501
ciphertext,
502-
sender_key: self.account_identity_keys.curve25519,
502+
sender_key: Some(self.account_identity_keys.curve25519),
503503
session_id: self.session_id().to_owned(),
504-
device_id: (*self.device_id).to_owned(),
504+
device_id: Some(self.device_id.clone()),
505505
}
506506
.into(),
507507
#[cfg(feature = "experimental-algorithms")]

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -949,7 +949,7 @@ macro_rules! cryptostore_integration_tests {
949949
let id = TransactionId::new();
950950
let info: SecretInfo = MegolmV1AesSha2Content {
951951
room_id: room_id!("!test:localhost").to_owned(),
952-
sender_key,
952+
sender_key: Some(sender_key),
953953
session_id: "test_session_id".to_owned(),
954954
}
955955
.into();
@@ -1010,7 +1010,7 @@ macro_rules! cryptostore_integration_tests {
10101010
let id = TransactionId::new();
10111011
let info: SecretInfo = MegolmV1AesSha2Content {
10121012
room_id: room_id!("!test:localhost").to_owned(),
1013-
sender_key: account.identity_keys().curve25519,
1013+
sender_key: Some(account.identity_keys().curve25519),
10141014
session_id: "test_session_id".to_owned(),
10151015
}
10161016
.into();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ impl DecryptedForwardedRoomKeyEvent {
5959
ForwardedRoomKeyContent::MegolmV1AesSha2(c) => Some(
6060
room_key_request::MegolmV1AesSha2Content {
6161
room_id: c.room_id.to_owned(),
62-
sender_key: c.claimed_sender_key,
62+
sender_key: Some(c.claimed_sender_key),
6363
session_id: c.session_id.to_owned(),
6464
}
6565
.into(),

crates/matrix-sdk-crypto/src/types/events/room/encrypted.rs

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ use vodozemac::{megolm::MegolmMessage, olm::OlmMessage, Curve25519PublicKey};
2323

2424
use super::Event;
2525
use crate::types::{
26-
deserialize_curve_key,
26+
deserialize_curve_key, deserialize_curve_key_option,
2727
events::{
2828
room_key_request::{self, SupportedKeyInfo},
2929
EventType, ToDeviceEvent,
3030
},
31-
serialize_curve_key, EventEncryptionAlgorithm,
31+
serialize_curve_key, serialize_curve_key_option, EventEncryptionAlgorithm,
3232
};
3333

3434
/// An m.room.encrypted room event.
@@ -309,11 +309,17 @@ pub struct MegolmV1AesSha2Content {
309309
pub ciphertext: MegolmMessage,
310310

311311
/// The Curve25519 key of the sender.
312-
#[serde(deserialize_with = "deserialize_curve_key", serialize_with = "serialize_curve_key")]
313-
pub sender_key: Curve25519PublicKey,
312+
#[serde(
313+
default,
314+
deserialize_with = "deserialize_curve_key_option",
315+
serialize_with = "serialize_curve_key_option",
316+
skip_serializing_if = "Option::is_none"
317+
)]
318+
pub sender_key: Option<Curve25519PublicKey>,
314319

315320
/// The ID of the sending device.
316-
pub device_id: OwnedDeviceId,
321+
#[serde(skip_serializing_if = "Option::is_none")]
322+
pub device_id: Option<OwnedDeviceId>,
317323

318324
/// The ID of the session used to encrypt the message.
319325
pub session_id: String,
@@ -528,4 +534,37 @@ pub(crate) mod tests {
528534

529535
Ok(())
530536
}
537+
538+
#[test]
539+
fn deserialization_missing_sender_key_device_id() -> Result<(), serde_json::Error> {
540+
let json = json!({
541+
"sender": "@alice:example.org",
542+
"event_id": "$Nhl3rsgHMjk-DjMJANawr9HHAhLg4GcoTYrSiYYGqEE",
543+
"content": {
544+
"m.custom": "something custom",
545+
"algorithm": "m.megolm.v1.aes-sha2",
546+
"session_id": "ZFD6+OmV7fVCsJ7Gap8UnORH8EnmiAkes8FAvQuCw/I",
547+
"ciphertext":
548+
"AwgAEiBQs2LgBD2CcB+RLH2bsgp9VadFUJhBXOtCmcJuttBDOeDNjL21d9\
549+
z0AcVSfQFAh9huh4or7sWuNrHcvu9/sMbweTgc0UtdA5xFLheubHouXy4a\
550+
ewze+ShndWAaTbjWJMLsPSQDUMQHBA",
551+
"m.relates_to": {
552+
"rel_type": "m.reference",
553+
"event_id": "$WUreEJERkFzO8i2dk6CmTex01cP1dZ4GWKhKCwkWHrQ"
554+
},
555+
},
556+
"type": "m.room.encrypted",
557+
"origin_server_ts": 1632491098485u64,
558+
"m.custom.top": "something custom in the top",
559+
});
560+
561+
let event: EncryptedEvent = serde_json::from_value(json.clone())?;
562+
563+
assert_matches!(event.content.scheme, RoomEventEncryptionScheme::MegolmV1AesSha2(_));
564+
assert!(event.content.relates_to.is_some());
565+
let serialized = serde_json::to_value(event)?;
566+
assert_eq!(json, serialized);
567+
568+
Ok(())
569+
}
531570
}

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

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ use serde_json::Value;
2525
use vodozemac::Curve25519PublicKey;
2626

2727
use super::{EventType, ToDeviceEvent};
28-
use crate::types::{deserialize_curve_key, serialize_curve_key, EventEncryptionAlgorithm};
28+
use crate::types::{
29+
deserialize_curve_key_option, serialize_curve_key_option, EventEncryptionAlgorithm,
30+
};
2931

3032
/// The `m.room_key_request` to-device event.
3133
pub type RoomKeyRequestEvent = ToDeviceEvent<RoomKeyRequestContent>;
@@ -209,8 +211,13 @@ pub struct MegolmV1AesSha2Content {
209211
pub room_id: OwnedRoomId,
210212

211213
/// The Curve25519 key of the device which initiated the session originally.
212-
#[serde(deserialize_with = "deserialize_curve_key", serialize_with = "serialize_curve_key")]
213-
pub sender_key: Curve25519PublicKey,
214+
#[serde(
215+
default,
216+
deserialize_with = "deserialize_curve_key_option",
217+
serialize_with = "serialize_curve_key_option",
218+
skip_serializing_if = "Option::is_none"
219+
)]
220+
pub sender_key: Option<Curve25519PublicKey>,
214221

215222
/// The ID of the session that the key is for.
216223
pub session_id: String,
@@ -382,4 +389,33 @@ mod tests {
382389

383390
Ok(())
384391
}
392+
393+
#[test]
394+
fn deserialization_missing_sender_key() -> Result<(), serde_json::Error> {
395+
let json = json!({
396+
"sender": "@alice:example.org",
397+
"content": {
398+
"action": "request",
399+
"body": {
400+
"algorithm": "m.megolm.v1.aes-sha2",
401+
"room_id": "!Cuyf34gef24t:localhost",
402+
"session_id": "X3lUlvLELLYxeTx4yOVu6UDpasGEVO0Jbu+QFnm0cKQ"
403+
},
404+
"request_id": "1495474790150.19",
405+
"requesting_device_id": "RJYKSTBOIE"
406+
},
407+
"type": "m.room_key_request"
408+
});
409+
410+
let event: RoomKeyRequestEvent = serde_json::from_value(json.clone())?;
411+
412+
assert_matches!(
413+
event.content.action,
414+
Action::Request(RequestedKeyInfo::MegolmV1AesSha2(_))
415+
);
416+
let serialized = serde_json::to_value(event)?;
417+
assert_eq!(json, serialized);
418+
419+
Ok(())
420+
}
385421
}

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,27 @@ where
519519
keys.serialize(s)
520520
}
521521

522+
pub(crate) fn deserialize_curve_key_option<'de, D>(
523+
de: D,
524+
) -> Result<Option<Curve25519PublicKey>, D::Error>
525+
where
526+
D: Deserializer<'de>,
527+
{
528+
let key: Option<String> = Deserialize::deserialize(de)?;
529+
key.map(|k| Curve25519PublicKey::from_base64(&k)).transpose().map_err(serde::de::Error::custom)
530+
}
531+
532+
pub(crate) fn serialize_curve_key_option<S>(
533+
key: &Option<Curve25519PublicKey>,
534+
s: S,
535+
) -> Result<S::Ok, S::Error>
536+
where
537+
S: Serializer,
538+
{
539+
let key = key.as_ref().map(|k| k.to_base64());
540+
key.serialize(s)
541+
}
542+
522543
/// Trait to express the various room key export formats we have in a unified
523544
/// manner.
524545
pub trait RoomKeyExport {

0 commit comments

Comments
 (0)