From 61fa339163662cb7acd6ae1de0e8e522bf8bb61c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Wed, 19 Feb 2025 12:57:32 +0100 Subject: [PATCH] refactor(crypto): Add a constructor to create an InboundGroupSession from a m.room_key event --- crates/matrix-sdk-crypto/src/machine/mod.rs | 11 ++----- .../src/olm/group_sessions/inbound.rs | 31 +++++++++++++++++++ 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/crates/matrix-sdk-crypto/src/machine/mod.rs b/crates/matrix-sdk-crypto/src/machine/mod.rs index a497057d2..5567b9a8d 100644 --- a/crates/matrix-sdk-crypto/src/machine/mod.rs +++ b/crates/matrix-sdk-crypto/src/machine/mod.rs @@ -877,15 +877,8 @@ impl OlmMachine { event: &DecryptedRoomKeyEvent, content: &MegolmV1AesSha2Content, ) -> OlmResult> { - let session = InboundGroupSession::new( - sender_key, - event.keys.ed25519, - &content.room_id, - &content.session_key, - SenderData::unknown(), - event.content.algorithm(), - None, - ); + let session = + InboundGroupSession::from_room_key_content(sender_key, event.keys.ed25519, content); match session { Ok(mut session) => { diff --git a/crates/matrix-sdk-crypto/src/olm/group_sessions/inbound.rs b/crates/matrix-sdk-crypto/src/olm/group_sessions/inbound.rs index f86ee2823..964bf9b92 100644 --- a/crates/matrix-sdk-crypto/src/olm/group_sessions/inbound.rs +++ b/crates/matrix-sdk-crypto/src/olm/group_sessions/inbound.rs @@ -51,6 +51,7 @@ use crate::{ }, olm_v1::DecryptedForwardedRoomKeyEvent, room::encrypted::{EncryptedEvent, RoomEventEncryptionScheme}, + room_key, }, serialize_curve_key, EventEncryptionAlgorithm, SigningKeys, }, @@ -210,6 +211,36 @@ impl InboundGroupSession { }) } + /// Create a new [`InboundGroupSession`] from a `m.room_key` event with an + /// `m.megolm.v1.aes-sha2` content. + /// + /// The `m.room_key` event **must** have been encrypted using the + /// `m.olm.v1.curve25519-aes-sha2` algorithm and the `sender_key` **must** + /// be the long-term [`Curve25519PublicKey`] that was used to establish + /// the 1-to-1 Olm session. + /// + /// The `signing_key` **must** be the [`Ed25519PublicKey`] contained in the + /// `keys` field of the [decrypted payload]. + /// + /// [decrypted payload]: https://spec.matrix.org/unstable/client-server-api/#molmv1curve25519-aes-sha2 + pub fn from_room_key_content( + sender_key: Curve25519PublicKey, + signing_key: Ed25519PublicKey, + content: &room_key::MegolmV1AesSha2Content, + ) -> Result { + let room_key::MegolmV1AesSha2Content { room_id, session_id: _, session_key, .. } = content; + + Self::new( + sender_key, + signing_key, + room_id, + session_key, + SenderData::unknown(), + EventEncryptionAlgorithm::MegolmV1AesSha2, + None, + ) + } + /// Create a new [`InboundGroupSession`] from an exported version of the /// group session. ///