From d04c7e019029ab05fb6eaea2cb9e0bab2fa44b3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Fri, 10 Apr 2020 14:00:03 +0200 Subject: [PATCH] crypto: Add a GroupSessionKey type. --- src/crypto/machine.rs | 8 +++++--- src/crypto/olm.rs | 14 ++++++++++---- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/crypto/machine.rs b/src/crypto/machine.rs index ab3e0fe6..431a0890 100644 --- a/src/crypto/machine.rs +++ b/src/crypto/machine.rs @@ -21,7 +21,7 @@ use std::sync::Arc; use uuid::Uuid; use super::error::{OlmError, Result, SignatureError, VerificationResult}; -use super::olm::{Account, InboundGroupSession, OutboundGroupSession, Session}; +use super::olm::{Account, GroupSessionKey, InboundGroupSession, OutboundGroupSession, Session}; use super::store::memorystore::MemoryStore; #[cfg(feature = "sqlite-cryptostore")] use super::store::sqlite::SqliteStore; @@ -776,11 +776,13 @@ impl OlmMachine { .get("ed25519") .ok_or(OlmError::MissingSigningKey)?; + let session_key = GroupSessionKey(event.content.session_key.to_owned()); + let session = InboundGroupSession::new( sender_key, signing_key, &event.content.room_id, - &event.content.session_key, + session_key, )?; self.store.save_inbound_group_session(session).await?; Ok(()) @@ -807,7 +809,7 @@ impl OlmMachine { sender_key, signing_key, &room_id, - &session.session_key().await, + session.session_key().await, )?; self.store .save_inbound_group_session(inbound_session) diff --git a/src/crypto/olm.rs b/src/crypto/olm.rs index 2653f8bc..819ad4ed 100644 --- a/src/crypto/olm.rs +++ b/src/crypto/olm.rs @@ -17,6 +17,7 @@ use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; use std::sync::Arc; use std::time::Instant; +use serde::Serialize; use tokio::sync::Mutex; use olm_rs::account::{IdentityKeys, OlmAccount, OneTimeKeys}; @@ -309,6 +310,11 @@ impl PartialEq for Session { } } +/// The private session key of a group session. +/// Can be used to create a new inbound group session. +#[derive(Clone, Serialize)] +pub struct GroupSessionKey(pub String); + /// Inbound group session. /// /// Inbound group sessions are used to exchange room messages between a group of @@ -342,10 +348,10 @@ impl InboundGroupSession { sender_key: &str, signing_key: &str, room_id: &RoomId, - session_key: &str, + session_key: GroupSessionKey, ) -> Result { Ok(InboundGroupSession { - inner: OlmInboundGroupSession::new(session_key)?, + inner: OlmInboundGroupSession::new(&session_key.0)?, sender_key: sender_key.to_owned(), signing_key: signing_key.to_owned(), room_id: room_id.clone(), @@ -504,9 +510,9 @@ impl OutboundGroupSession { /// Get the session key of this session. /// /// A session key can be used to to create an `InboundGroupSession`. - pub async fn session_key(&self) -> String { + pub async fn session_key(&self) -> GroupSessionKey { let session = self.inner.lock().await; - session.session_key() + GroupSessionKey(session.session_key()) } /// Returns the unique identifier for this session.