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.master
parent
ac264918b8
commit
b2ccb61864
|
@ -1006,7 +1006,7 @@ impl OlmMachine {
|
||||||
panic!("Session is expired");
|
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.
|
/// Encrypt some JSON content using the given Olm session.
|
||||||
|
|
|
@ -214,7 +214,7 @@ mod test {
|
||||||
use crate::device::test::get_device;
|
use crate::device::test::get_device;
|
||||||
use crate::memory_stores::{DeviceStore, GroupSessionStore, SessionStore};
|
use crate::memory_stores::{DeviceStore, GroupSessionStore, SessionStore};
|
||||||
use crate::olm::test::get_account_and_session;
|
use crate::olm::test::get_account_and_session;
|
||||||
use crate::olm::{InboundGroupSession, OutboundGroupSession};
|
use crate::olm::InboundGroupSession;
|
||||||
use matrix_sdk_common::identifiers::RoomId;
|
use matrix_sdk_common::identifiers::RoomId;
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
|
@ -251,9 +251,10 @@ mod test {
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_group_session_store() {
|
async fn test_group_session_store() {
|
||||||
|
let (account, _) = get_account_and_session().await;
|
||||||
let room_id = RoomId::try_from("!test:localhost").unwrap();
|
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_eq!(0, outbound.message_index().await);
|
||||||
assert!(!outbound.shared());
|
assert!(!outbound.shared());
|
||||||
|
|
|
@ -365,7 +365,8 @@ impl Account {
|
||||||
&self,
|
&self,
|
||||||
room_id: &RoomId,
|
room_id: &RoomId,
|
||||||
) -> (OutboundGroupSession, InboundGroupSession) {
|
) -> (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 identity_keys = self.identity_keys();
|
||||||
|
|
||||||
let sender_key = identity_keys.curve25519();
|
let sender_key = identity_keys.curve25519();
|
||||||
|
@ -719,6 +720,8 @@ impl PartialEq for InboundGroupSession {
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct OutboundGroupSession {
|
pub struct OutboundGroupSession {
|
||||||
inner: Arc<Mutex<OlmOutboundGroupSession>>,
|
inner: Arc<Mutex<OlmOutboundGroupSession>>,
|
||||||
|
device_id: Arc<DeviceId>,
|
||||||
|
account_identity_keys: Arc<IdentityKeys>,
|
||||||
session_id: Arc<String>,
|
session_id: Arc<String>,
|
||||||
room_id: Arc<RoomId>,
|
room_id: Arc<RoomId>,
|
||||||
creation_time: Arc<Instant>,
|
creation_time: Arc<Instant>,
|
||||||
|
@ -733,14 +736,21 @@ impl OutboundGroupSession {
|
||||||
///
|
///
|
||||||
/// # Arguments
|
/// # 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.
|
/// * `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<DeviceId>, identity_keys: Arc<IdentityKeys>, room_id: &RoomId) -> Self {
|
||||||
let session = OlmOutboundGroupSession::new();
|
let session = OlmOutboundGroupSession::new();
|
||||||
let session_id = session.session_id();
|
let session_id = session.session_id();
|
||||||
|
|
||||||
OutboundGroupSession {
|
OutboundGroupSession {
|
||||||
inner: Arc::new(Mutex::new(session)),
|
inner: Arc::new(Mutex::new(session)),
|
||||||
room_id: Arc::new(room_id.to_owned()),
|
room_id: Arc::new(room_id.to_owned()),
|
||||||
|
device_id,
|
||||||
|
account_identity_keys: identity_keys,
|
||||||
session_id: Arc::new(session_id),
|
session_id: Arc::new(session_id),
|
||||||
creation_time: Arc::new(Instant::now()),
|
creation_time: Arc::new(Instant::now()),
|
||||||
message_count: Arc::new(AtomicUsize::new(0)),
|
message_count: Arc::new(AtomicUsize::new(0)),
|
||||||
|
@ -772,20 +782,13 @@ impl OutboundGroupSession {
|
||||||
///
|
///
|
||||||
/// # Arguments
|
/// # Arguments
|
||||||
///
|
///
|
||||||
/// * `account` - The account that owns created the outbound session.
|
|
||||||
/// encrypted.
|
|
||||||
///
|
|
||||||
/// * `content` - The plaintext content of the message that should be
|
/// * `content` - The plaintext content of the message that should be
|
||||||
/// encrypted.
|
/// encrypted.
|
||||||
///
|
///
|
||||||
/// # Panics
|
/// # Panics
|
||||||
///
|
///
|
||||||
/// Panics if the content can't be serialized.
|
/// Panics if the content can't be serialized.
|
||||||
pub async fn encrypt(
|
pub async fn encrypt(&self, content: MessageEventContent) -> EncryptedEventContent {
|
||||||
&self,
|
|
||||||
account: Account,
|
|
||||||
content: MessageEventContent,
|
|
||||||
) -> EncryptedEventContent {
|
|
||||||
let json_content = json!({
|
let json_content = json!({
|
||||||
"content": content,
|
"content": content,
|
||||||
"room_id": &*self.room_id,
|
"room_id": &*self.room_id,
|
||||||
|
@ -803,9 +806,9 @@ impl OutboundGroupSession {
|
||||||
|
|
||||||
EncryptedEventContent::MegolmV1AesSha2(MegolmV1AesSha2Content {
|
EncryptedEventContent::MegolmV1AesSha2(MegolmV1AesSha2Content {
|
||||||
ciphertext,
|
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(),
|
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)]
|
#[cfg(test)]
|
||||||
pub(crate) mod 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::api::r0::keys::SignedKey;
|
||||||
use matrix_sdk_common::identifiers::{DeviceId, RoomId, UserId};
|
use matrix_sdk_common::identifiers::{DeviceId, RoomId, UserId};
|
||||||
use olm_rs::session::OlmMessage;
|
use olm_rs::session::OlmMessage;
|
||||||
|
@ -1021,9 +1024,10 @@ pub(crate) mod test {
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn group_session_creation() {
|
async fn group_session_creation() {
|
||||||
|
let alice = Account::new(&alice_id(), &alice_device_id());
|
||||||
let room_id = RoomId::try_from("!test:localhost").unwrap();
|
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_eq!(0, outbound.message_index().await);
|
||||||
assert!(!outbound.shared());
|
assert!(!outbound.shared());
|
||||||
|
|
|
@ -128,7 +128,7 @@ mod test {
|
||||||
|
|
||||||
use crate::device::test::get_device;
|
use crate::device::test::get_device;
|
||||||
use crate::olm::test::get_account_and_session;
|
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::memorystore::MemoryStore;
|
||||||
use crate::store::CryptoStore;
|
use crate::store::CryptoStore;
|
||||||
use matrix_sdk_common::identifiers::RoomId;
|
use matrix_sdk_common::identifiers::RoomId;
|
||||||
|
@ -157,9 +157,10 @@ mod test {
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_group_session_store() {
|
async fn test_group_session_store() {
|
||||||
|
let (account, _) = get_account_and_session().await;
|
||||||
let room_id = RoomId::try_from("!test:localhost").unwrap();
|
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(
|
let inbound = InboundGroupSession::new(
|
||||||
"test_key",
|
"test_key",
|
||||||
"test_key",
|
"test_key",
|
||||||
|
|
Loading…
Reference in New Issue