Skip to content

Commit a60e336

Browse files
committed
feat(crypto): Start using the stable identifier for the sender device keys
This patch updates the sending side of the `sender_device_keys` field introduced in MSC4147. Since the MSC got merged, we're switching from the unstable identifier to the stable one. A couple of snapshot tests were added modified to make this happen.
1 parent 426a4ff commit a60e336

File tree

7 files changed

+98
-25
lines changed

7 files changed

+98
-25
lines changed

crates/matrix-sdk-crypto/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ All notable changes to this project will be documented in this file.
88

99
### Features
1010

11+
- Send stable identifier `sender_device_keys` for MSC4147 (Including device
12+
keys with Olm-encrypted events).
13+
([#4964](https://github.com/matrix-org/matrix-rust-sdk/pull/4964))
14+
1115
- Add experimental APIs for sharing encrypted room key history with new members, `Store::build_room_key_bundle` and `OlmMachine::share_room_key_bundle_data`.
1216
([#4775](https://github.com/matrix-org/matrix-rust-sdk/pull/4775), [#4864](https://github.com/matrix-org/matrix-rust-sdk/pull/4864))
1317

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -222,11 +222,11 @@ async fn test_processed_to_device_variants() {
222222
processed_event.to_raw().deserialize_as::<Value>().unwrap(),
223223
{
224224
".keys.ed25519" => "[sender_ed25519_key]",
225-
r#"["org.matrix.msc4147.device_keys"].device_id"# => "[ABCDEFGH]",
226-
r#"["org.matrix.msc4147.device_keys"].keys"# => "++REDACTED++",
227-
r#"["org.matrix.msc4147.device_keys"].signatures"# => "++REDACTED++",
225+
r#"["sender_device_keys"].device_id"# => "[ABCDEFGH]",
226+
r#"["sender_device_keys"].keys"# => "++REDACTED++",
227+
r#"["sender_device_keys"].signatures"# => "++REDACTED++",
228228
// Redacted because depending on feature flags
229-
r#"["org.matrix.msc4147.device_keys"].algorithms"# => "++REDACTED++",
229+
r#"["sender_device_keys"].algorithms"# => "++REDACTED++",
230230
".recipient_keys.ed25519" => "[recipient_sender_key]",
231231
}
232232
);

crates/matrix-sdk-crypto/src/machine/tests/snapshots/processed_to_device_variants.snap

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
source: crates/matrix-sdk-crypto/src/machine/tests/send_encrypted_to_device.rs
3-
expression: "serde_json::from_str::<Value>(processed_event.to_raw().json().get()).unwrap()"
3+
expression: "processed_event.to_raw().deserialize_as::<Value>().unwrap()"
44
---
55
{
66
"content": {
@@ -16,17 +16,17 @@ expression: "serde_json::from_str::<Value>(processed_event.to_raw().json().get()
1616
"keys": {
1717
"ed25519": "[sender_ed25519_key]"
1818
},
19-
"org.matrix.msc4147.device_keys": {
19+
"recipient": "@bob:example.com",
20+
"recipient_keys": {
21+
"ed25519": "[recipient_sender_key]"
22+
},
23+
"sender": "@alice:example.org",
24+
"sender_device_keys": {
2025
"algorithms": "++REDACTED++",
2126
"device_id": "[ABCDEFGH]",
2227
"keys": "++REDACTED++",
2328
"signatures": "++REDACTED++",
2429
"user_id": "@alice:example.org"
2530
},
26-
"recipient": "@bob:example.com",
27-
"recipient_keys": {
28-
"ed25519": "[recipient_sender_key]"
29-
},
30-
"sender": "@alice:example.org",
3131
"type": "rtc.call.encryption_keys"
3232
}

crates/matrix-sdk-crypto/src/olm/session.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use crate::{
3030
error::{EventError, OlmResult, SessionUnpickleError},
3131
types::{
3232
events::{
33-
olm_v1::DecryptedOlmV1Event,
33+
olm_v1::{DecryptedOlmV1Event, OlmV1Keys},
3434
room::encrypted::{OlmV1Curve25519AesSha2Content, ToDeviceEncryptedEventContent},
3535
EventType,
3636
},
@@ -192,15 +192,13 @@ impl Session {
192192
let content = DecryptedOlmV1Event {
193193
sender: self.our_device_keys.user_id.clone(),
194194
recipient: recipient_device.user_id().into(),
195-
keys: crate::types::events::olm_v1::OlmV1Keys {
195+
keys: OlmV1Keys {
196196
ed25519: self
197197
.our_device_keys
198198
.ed25519_key()
199199
.expect("Our own device should have an Ed25519 public key"),
200200
},
201-
recipient_keys: crate::types::events::olm_v1::OlmV1Keys {
202-
ed25519: recipient_signing_key,
203-
},
201+
recipient_keys: OlmV1Keys { ed25519: recipient_signing_key },
204202
sender_device_keys: Some(self.our_device_keys.clone()),
205203
content,
206204
};
@@ -408,13 +406,10 @@ mod tests {
408406
)
409407
.unwrap();
410408

411-
// Also ensure that the encrypted payload has the device keys under the unstable
409+
// Also ensure that the encrypted payload has the device keys under the stable
412410
// prefix
413411
let plaintext: Value = serde_json::from_str(&bob_session_result.plaintext).unwrap();
414-
assert_eq!(
415-
plaintext["org.matrix.msc4147.device_keys"]["user_id"].as_str(),
416-
Some("@alice:localhost")
417-
);
412+
assert_eq!(plaintext["sender_device_keys"]["user_id"].as_str(), Some("@alice:localhost"));
418413

419414
// And the serialized object matches the format as specified in
420415
// DecryptedOlmV1Event

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,7 @@ impl<C: EventType + Debug + Sized + Serialize> Serialize for DecryptedOlmV1Event
203203
recipient: &'a UserId,
204204
keys: &'a OlmV1Keys,
205205
recipient_keys: &'a OlmV1Keys,
206-
#[serde(
207-
rename = "org.matrix.msc4147.device_keys",
208-
skip_serializing_if = "Option::is_none"
209-
)]
206+
#[serde(skip_serializing_if = "Option::is_none")]
210207
sender_device_keys: Option<&'a DeviceKeys>,
211208
content: &'a C,
212209
#[serde(rename = "type")]
@@ -326,6 +323,7 @@ mod tests {
326323
use std::collections::BTreeMap;
327324

328325
use assert_matches::assert_matches;
326+
use insta::{assert_json_snapshot, with_settings};
329327
use ruma::{device_id, owned_user_id, KeyId};
330328
use serde_json::{json, Value};
331329
use similar_asserts::assert_eq;
@@ -504,6 +502,17 @@ mod tests {
504502
(sender_device_keys_json, sender_device_keys)
505503
}
506504

505+
#[test]
506+
fn decrypted_to_device_event_snapshot() {
507+
let event_json = room_key_event();
508+
let event: DecryptedRoomKeyEvent = serde_json::from_value(event_json)
509+
.expect("JSON should deserialize to the right event type");
510+
511+
with_settings!({ sort_maps => true, prepend_module_to_snapshot => false }, {
512+
assert_json_snapshot!(event);
513+
})
514+
}
515+
507516
#[test]
508517
fn deserialization() -> Result<(), serde_json::Error> {
509518
macro_rules! assert_deserialization_result {
@@ -571,6 +580,10 @@ mod tests {
571580

572581
// Then it contains the sender_device_keys
573582
assert_eq!(event.sender_device_keys, Some(sender_device_keys));
583+
584+
with_settings!({ sort_maps => true, prepend_module_to_snapshot => false }, {
585+
assert_json_snapshot!(event);
586+
});
574587
}
575588

576589
#[test]
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
source: crates/matrix-sdk-crypto/src/types/events/olm_v1.rs
3+
expression: event
4+
---
5+
{
6+
"sender": "@alice:example.org",
7+
"recipient": "@bob:example.org",
8+
"keys": {
9+
"ed25519": "aOfOnlaeMb5GW1TxkZ8pXnblkGMgAvps+lAukrdYaZk"
10+
},
11+
"recipient_keys": {
12+
"ed25519": "aOfOnlaeMb5GW1TxkZ8pXnblkGMgAvps+lAukrdYaZk"
13+
},
14+
"content": {
15+
"algorithm": "m.megolm.v1.aes-sha2",
16+
"org.matrix.msc3061.shared_history": false,
17+
"room_id": "!Cuyf34gef24t:localhost",
18+
"session_id": "ZFD6+OmV7fVCsJ7Gap8UnORH8EnmiAkes8FAvQuCw/I",
19+
"session_key": "AgAAAADNp1EbxXYOGmJtyX4AkD1bvJvAUyPkbIaKxtnGKjvSQ3E/4mnuqdM4vsmNzpO1EeWzz1rDkUpYhYE9kP7sJhgLXijVv80fMPHfGc49hPdu8A+xnwD4SQiYdFmSWJOIqsxeo/fiHtino//CDQENtcKuEt0I9s0+Kk4YSH310Szse2RQ+vjple31QrCexmqfFJzkR/BJ5ogJHrPBQL0LgsPyglIbMTLg7qygIaYU5Fe2QdKMH7nTZPNIRHh1RaMfHVETAUJBax88EWZBoifk80gdHUwHSgMk77vCc2a5KHKLDA"
20+
},
21+
"type": "m.room_key"
22+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
---
2+
source: crates/matrix-sdk-crypto/src/types/events/olm_v1.rs
3+
expression: event
4+
---
5+
{
6+
"sender": "@alice:example.org",
7+
"recipient": "@bob:example.org",
8+
"keys": {
9+
"ed25519": "aOfOnlaeMb5GW1TxkZ8pXnblkGMgAvps+lAukrdYaZk"
10+
},
11+
"recipient_keys": {
12+
"ed25519": "aOfOnlaeMb5GW1TxkZ8pXnblkGMgAvps+lAukrdYaZk"
13+
},
14+
"sender_device_keys": {
15+
"algorithms": [
16+
"m.olm.v1.curve25519-aes-sha2"
17+
],
18+
"device_id": "DEV",
19+
"keys": {
20+
"curve25519:DEV": "c29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb28",
21+
"ed25519:DEV": "b29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb28"
22+
},
23+
"signatures": {
24+
"@u:s.co": {
25+
"ed25519:DEV": "mia28GKixFzOWKJ0h7Bdrdy2fjxiHCsst1qpe467FbW85H61UlshtKBoAXfTLlVfi0FX+/noJ8B3noQPnY+9Cg",
26+
"ed25519:ssk": "mia28GKixFzOWKJ0h7Bdrdy2fjxiHCsst1qpe467FbW85H61UlshtKBoAXfTLlVfi0FX+/noJ8B3noQPnY+9Cg"
27+
}
28+
},
29+
"user_id": "@u:s.co"
30+
},
31+
"content": {
32+
"algorithm": "m.megolm.v1.aes-sha2",
33+
"org.matrix.msc3061.shared_history": false,
34+
"room_id": "!Cuyf34gef24t:localhost",
35+
"session_id": "ZFD6+OmV7fVCsJ7Gap8UnORH8EnmiAkes8FAvQuCw/I",
36+
"session_key": "AgAAAADNp1EbxXYOGmJtyX4AkD1bvJvAUyPkbIaKxtnGKjvSQ3E/4mnuqdM4vsmNzpO1EeWzz1rDkUpYhYE9kP7sJhgLXijVv80fMPHfGc49hPdu8A+xnwD4SQiYdFmSWJOIqsxeo/fiHtino//CDQENtcKuEt0I9s0+Kk4YSH310Szse2RQ+vjple31QrCexmqfFJzkR/BJ5ogJHrPBQL0LgsPyglIbMTLg7qygIaYU5Fe2QdKMH7nTZPNIRHh1RaMfHVETAUJBax88EWZBoifk80gdHUwHSgMk77vCc2a5KHKLDA"
37+
},
38+
"type": "m.room_key"
39+
}

0 commit comments

Comments
 (0)