crypto: Simplify the group session pair creation.

master
Damir Jelić 2020-06-25 13:31:30 +02:00
parent 676d547161
commit 262a61afc9
2 changed files with 35 additions and 16 deletions

View File

@ -1058,26 +1058,13 @@ impl OlmMachine {
/// This also creates a matching inbound group session and saves that one in /// This also creates a matching inbound group session and saves that one in
/// the store. /// the store.
async fn create_outbound_group_session(&mut self, room_id: &RoomId) -> OlmResult<()> { async fn create_outbound_group_session(&mut self, room_id: &RoomId) -> OlmResult<()> {
let session = OutboundGroupSession::new(room_id); let (outbound, inbound) = self.account.create_group_session_pair(room_id).await;
let identity_keys = self.account.identity_keys();
let sender_key = identity_keys.curve25519(); let _ = self.store.save_inbound_group_session(inbound).await?;
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 let _ = self
.outbound_group_sessions .outbound_group_sessions
.insert(room_id.to_owned(), session); .insert(room_id.to_owned(), outbound);
Ok(()) Ok(())
} }

View File

@ -234,6 +234,38 @@ impl Account {
last_use_time: Arc::new(now), 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 { impl PartialEq for Account {