From 262a61afc9b33fc4a4cbdcc886dc88a200db43ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Thu, 25 Jun 2020 13:31:30 +0200 Subject: [PATCH] crypto: Simplify the group session pair creation. --- matrix_sdk_crypto/src/machine.rs | 19 +++---------------- matrix_sdk_crypto/src/olm.rs | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 16 deletions(-) diff --git a/matrix_sdk_crypto/src/machine.rs b/matrix_sdk_crypto/src/machine.rs index 9ec72b04..b3d6d5ab 100644 --- a/matrix_sdk_crypto/src/machine.rs +++ b/matrix_sdk_crypto/src/machine.rs @@ -1058,26 +1058,13 @@ impl OlmMachine { /// This also creates a matching inbound group session and saves that one in /// the store. async fn create_outbound_group_session(&mut self, room_id: &RoomId) -> OlmResult<()> { - let session = OutboundGroupSession::new(room_id); - let identity_keys = self.account.identity_keys(); + let (outbound, inbound) = self.account.create_group_session_pair(room_id).await; - let sender_key = identity_keys.curve25519(); - let signing_key = identity_keys.ed25519(); - - let inbound_session = InboundGroupSession::new( - sender_key, - signing_key, - &room_id, - session.session_key().await, - )?; - let _ = self - .store - .save_inbound_group_session(inbound_session) - .await?; + let _ = self.store.save_inbound_group_session(inbound).await?; let _ = self .outbound_group_sessions - .insert(room_id.to_owned(), session); + .insert(room_id.to_owned(), outbound); Ok(()) } diff --git a/matrix_sdk_crypto/src/olm.rs b/matrix_sdk_crypto/src/olm.rs index ffb8abf9..f0129265 100644 --- a/matrix_sdk_crypto/src/olm.rs +++ b/matrix_sdk_crypto/src/olm.rs @@ -234,6 +234,38 @@ impl Account { last_use_time: Arc::new(now), }) } + + /// Create a group session pair. + /// + /// This session pair can be used to encrypt and decrypt messages meant for + /// a large group of participants. + /// + /// The outbound session is used to encrypt messages while the inbound one + /// is used to decrypt messages encrypted by the outbound one. + /// + /// # Arguments + /// + /// * `room_id` - The ID of the room where the group session will be used. + pub async fn create_group_session_pair( + &self, + room_id: &RoomId, + ) -> (OutboundGroupSession, InboundGroupSession) { + let outbound = OutboundGroupSession::new(room_id); + let identity_keys = self.identity_keys(); + + let sender_key = identity_keys.curve25519(); + let signing_key = identity_keys.ed25519(); + + let inbound = InboundGroupSession::new( + sender_key, + signing_key, + &room_id, + outbound.session_key().await, + ) + .expect("Can't create inbound group session from a newly created outbound group session"); + + (outbound, inbound) + } } impl PartialEq for Account {