Skip to content

Commit 61fa339

Browse files
committed
refactor(crypto): Add a constructor to create an InboundGroupSession from a m.room_key event
1 parent 3f5efc1 commit 61fa339

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

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

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -877,15 +877,8 @@ impl OlmMachine {
877877
event: &DecryptedRoomKeyEvent,
878878
content: &MegolmV1AesSha2Content,
879879
) -> OlmResult<Option<InboundGroupSession>> {
880-
let session = InboundGroupSession::new(
881-
sender_key,
882-
event.keys.ed25519,
883-
&content.room_id,
884-
&content.session_key,
885-
SenderData::unknown(),
886-
event.content.algorithm(),
887-
None,
888-
);
880+
let session =
881+
InboundGroupSession::from_room_key_content(sender_key, event.keys.ed25519, content);
889882

890883
match session {
891884
Ok(mut session) => {

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ use crate::{
5151
},
5252
olm_v1::DecryptedForwardedRoomKeyEvent,
5353
room::encrypted::{EncryptedEvent, RoomEventEncryptionScheme},
54+
room_key,
5455
},
5556
serialize_curve_key, EventEncryptionAlgorithm, SigningKeys,
5657
},
@@ -210,6 +211,36 @@ impl InboundGroupSession {
210211
})
211212
}
212213

214+
/// Create a new [`InboundGroupSession`] from a `m.room_key` event with an
215+
/// `m.megolm.v1.aes-sha2` content.
216+
///
217+
/// The `m.room_key` event **must** have been encrypted using the
218+
/// `m.olm.v1.curve25519-aes-sha2` algorithm and the `sender_key` **must**
219+
/// be the long-term [`Curve25519PublicKey`] that was used to establish
220+
/// the 1-to-1 Olm session.
221+
///
222+
/// The `signing_key` **must** be the [`Ed25519PublicKey`] contained in the
223+
/// `keys` field of the [decrypted payload].
224+
///
225+
/// [decrypted payload]: https://spec.matrix.org/unstable/client-server-api/#molmv1curve25519-aes-sha2
226+
pub fn from_room_key_content(
227+
sender_key: Curve25519PublicKey,
228+
signing_key: Ed25519PublicKey,
229+
content: &room_key::MegolmV1AesSha2Content,
230+
) -> Result<Self, SessionCreationError> {
231+
let room_key::MegolmV1AesSha2Content { room_id, session_id: _, session_key, .. } = content;
232+
233+
Self::new(
234+
sender_key,
235+
signing_key,
236+
room_id,
237+
session_key,
238+
SenderData::unknown(),
239+
EventEncryptionAlgorithm::MegolmV1AesSha2,
240+
None,
241+
)
242+
}
243+
213244
/// Create a new [`InboundGroupSession`] from an exported version of the
214245
/// group session.
215246
///

0 commit comments

Comments
 (0)