From b2ccb61864421d7d8cd7783913dd6645308b2f35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Mon, 13 Jul 2020 14:32:59 +0200 Subject: [PATCH] crypto: Add the device id and identity keys to the megolm session. This way we don't need to pass in the account to encrypt events. --- matrix_sdk_crypto/src/machine.rs | 2 +- matrix_sdk_crypto/src/memory_stores.rs | 5 ++-- matrix_sdk_crypto/src/olm.rs | 32 ++++++++++++---------- matrix_sdk_crypto/src/store/memorystore.rs | 5 ++-- 4 files changed, 25 insertions(+), 19 deletions(-) diff --git a/matrix_sdk_crypto/src/machine.rs b/matrix_sdk_crypto/src/machine.rs index 6f50c1e8..c1dff76b 100644 --- a/matrix_sdk_crypto/src/machine.rs +++ b/matrix_sdk_crypto/src/machine.rs @@ -1006,7 +1006,7 @@ impl OlmMachine { panic!("Session is expired"); } - Ok(session.encrypt(self.account.clone(), content).await) + Ok(session.encrypt(content).await) } /// Encrypt some JSON content using the given Olm session. diff --git a/matrix_sdk_crypto/src/memory_stores.rs b/matrix_sdk_crypto/src/memory_stores.rs index 2172010c..be7aba34 100644 --- a/matrix_sdk_crypto/src/memory_stores.rs +++ b/matrix_sdk_crypto/src/memory_stores.rs @@ -214,7 +214,7 @@ mod test { use crate::device::test::get_device; use crate::memory_stores::{DeviceStore, GroupSessionStore, SessionStore}; use crate::olm::test::get_account_and_session; - use crate::olm::{InboundGroupSession, OutboundGroupSession}; + use crate::olm::InboundGroupSession; use matrix_sdk_common::identifiers::RoomId; #[tokio::test] @@ -251,9 +251,10 @@ mod test { #[tokio::test] async fn test_group_session_store() { + let (account, _) = get_account_and_session().await; let room_id = RoomId::try_from("!test:localhost").unwrap(); - let outbound = OutboundGroupSession::new(&room_id); + let (outbound, _) = account.create_group_session_pair(&room_id).await; assert_eq!(0, outbound.message_index().await); assert!(!outbound.shared()); diff --git a/matrix_sdk_crypto/src/olm.rs b/matrix_sdk_crypto/src/olm.rs index 5bc220b9..c8d31522 100644 --- a/matrix_sdk_crypto/src/olm.rs +++ b/matrix_sdk_crypto/src/olm.rs @@ -365,7 +365,8 @@ impl Account { &self, room_id: &RoomId, ) -> (OutboundGroupSession, InboundGroupSession) { - let outbound = OutboundGroupSession::new(room_id); + let outbound = + OutboundGroupSession::new(self.device_id.clone(), self.identity_keys.clone(), room_id); let identity_keys = self.identity_keys(); let sender_key = identity_keys.curve25519(); @@ -719,6 +720,8 @@ impl PartialEq for InboundGroupSession { #[derive(Clone)] pub struct OutboundGroupSession { inner: Arc>, + device_id: Arc, + account_identity_keys: Arc, session_id: Arc, room_id: Arc, creation_time: Arc, @@ -733,14 +736,21 @@ impl OutboundGroupSession { /// /// # Arguments /// + /// * `device_id` - The id of the device that created this session. + /// + /// * `identity_keys` - The identity keys of the account that created this + /// session. + /// /// * `room_id` - The id of the room that the session is used in. - pub fn new(room_id: &RoomId) -> Self { + fn new(device_id: Arc, identity_keys: Arc, room_id: &RoomId) -> Self { let session = OlmOutboundGroupSession::new(); let session_id = session.session_id(); OutboundGroupSession { inner: Arc::new(Mutex::new(session)), room_id: Arc::new(room_id.to_owned()), + device_id, + account_identity_keys: identity_keys, session_id: Arc::new(session_id), creation_time: Arc::new(Instant::now()), message_count: Arc::new(AtomicUsize::new(0)), @@ -772,20 +782,13 @@ impl OutboundGroupSession { /// /// # Arguments /// - /// * `account` - The account that owns created the outbound session. - /// encrypted. - /// /// * `content` - The plaintext content of the message that should be /// encrypted. /// /// # Panics /// /// Panics if the content can't be serialized. - pub async fn encrypt( - &self, - account: Account, - content: MessageEventContent, - ) -> EncryptedEventContent { + pub async fn encrypt(&self, content: MessageEventContent) -> EncryptedEventContent { let json_content = json!({ "content": content, "room_id": &*self.room_id, @@ -803,9 +806,9 @@ impl OutboundGroupSession { EncryptedEventContent::MegolmV1AesSha2(MegolmV1AesSha2Content { ciphertext, - sender_key: account.identity_keys().curve25519().to_owned(), + sender_key: self.account_identity_keys.curve25519().to_owned(), session_id: self.session_id().to_owned(), - device_id: (&*account.device_id).to_owned(), + device_id: (&*self.device_id).to_owned(), }) } @@ -868,7 +871,7 @@ impl std::fmt::Debug for OutboundGroupSession { #[cfg(test)] pub(crate) mod test { - use crate::olm::{Account, InboundGroupSession, OutboundGroupSession, Session}; + use crate::olm::{Account, InboundGroupSession, Session}; use matrix_sdk_common::api::r0::keys::SignedKey; use matrix_sdk_common::identifiers::{DeviceId, RoomId, UserId}; use olm_rs::session::OlmMessage; @@ -1021,9 +1024,10 @@ pub(crate) mod test { #[tokio::test] async fn group_session_creation() { + let alice = Account::new(&alice_id(), &alice_device_id()); let room_id = RoomId::try_from("!test:localhost").unwrap(); - let outbound = OutboundGroupSession::new(&room_id); + let (outbound, _) = alice.create_group_session_pair(&room_id).await; assert_eq!(0, outbound.message_index().await); assert!(!outbound.shared()); diff --git a/matrix_sdk_crypto/src/store/memorystore.rs b/matrix_sdk_crypto/src/store/memorystore.rs index 71a303ba..ccef2efd 100644 --- a/matrix_sdk_crypto/src/store/memorystore.rs +++ b/matrix_sdk_crypto/src/store/memorystore.rs @@ -128,7 +128,7 @@ mod test { use crate::device::test::get_device; use crate::olm::test::get_account_and_session; - use crate::olm::{InboundGroupSession, OutboundGroupSession}; + use crate::olm::InboundGroupSession; use crate::store::memorystore::MemoryStore; use crate::store::CryptoStore; use matrix_sdk_common::identifiers::RoomId; @@ -157,9 +157,10 @@ mod test { #[tokio::test] async fn test_group_session_store() { + let (account, _) = get_account_and_session().await; let room_id = RoomId::try_from("!test:localhost").unwrap(); - let outbound = OutboundGroupSession::new(&room_id); + let (outbound, _) = account.create_group_session_pair(&room_id).await; let inbound = InboundGroupSession::new( "test_key", "test_key",