Skip to content

Commit

Permalink
crypto: Calculate sender data for incoming sessions
Browse files Browse the repository at this point in the history
Part of #3543.
Builds on top of #3556

Implements the "fast lane" as described in
#3544

This will begin to populate `InboundGroupSession`s with the new
`SenderData` struct introduced in
#3556 but it will only
do it when the information is already available in the store. Future PRs
for this issue will query Matrix APIs using spawned async tasks.

Future issues will do retries and migration of old sessions.

---------

Signed-off-by: Andy Balaam <[email protected]>
Co-authored-by: Damir Jelić <[email protected]>
  • Loading branch information
andybalaam and poljar authored Jul 16, 2024
1 parent 84c9280 commit 8845550
Show file tree
Hide file tree
Showing 10 changed files with 985 additions and 27 deletions.
4 changes: 4 additions & 0 deletions crates/matrix-sdk-crypto/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,10 @@ pub enum SessionCreationError {
/// Error when creating an Olm Session from an incoming Olm message.
#[error(transparent)]
InboundCreation(#[from] vodozemac::olm::SessionCreationError),

/// The given device keys are invalid.
#[error("The given device keys are invalid")]
InvalidDeviceKeys(#[from] SignatureError),
}

/// Errors that can be returned by
Expand Down
4 changes: 3 additions & 1 deletion crates/matrix-sdk-crypto/src/gossiping/machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1218,6 +1218,8 @@ mod tests {
create_sessions: bool,
algorithm: EventEncryptionAlgorithm,
) -> (GossipMachine, OutboundGroupSession, GossipMachine) {
use crate::olm::SenderData;

let alice_machine = get_machine_test_helper().await;
let alice_device = DeviceData::from_account(
&alice_machine.inner.store.cache().await.unwrap().account().await.unwrap(),
Expand Down Expand Up @@ -1270,7 +1272,7 @@ mod tests {
.inner
.store
.static_account()
.create_group_session_pair(room_id(), settings)
.create_group_session_pair(room_id(), settings, SenderData::unknown())
.await
.unwrap();

Expand Down
24 changes: 19 additions & 5 deletions crates/matrix-sdk-crypto/src/machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ use crate::{
identities::{user::UserIdentities, Device, IdentityManager, UserDevices},
olm::{
Account, CrossSigningStatus, EncryptionSettings, IdentityKeys, InboundGroupSession,
OlmDecryptionInfo, PrivateCrossSigningIdentity, SenderData, SessionType, StaticAccountData,
OlmDecryptionInfo, PrivateCrossSigningIdentity, SenderDataFinder, SessionType,
StaticAccountData,
},
requests::{IncomingResponse, OutgoingRequest, UploadSigningKeysRequest},
session_manager::{GroupSessionManager, SessionManager},
Expand Down Expand Up @@ -816,7 +817,8 @@ impl OlmMachine {
event: &DecryptedRoomKeyEvent,
content: &MegolmV1AesSha2Content,
) -> OlmResult<Option<InboundGroupSession>> {
let sender_data = SenderData::unknown();
let sender_data =
SenderDataFinder::find_using_event(self.store(), sender_key, event).await?;

let session = InboundGroupSession::new(
sender_key,
Expand Down Expand Up @@ -897,10 +899,16 @@ impl OlmMachine {
&self,
room_id: &RoomId,
) -> OlmResult<()> {
use crate::olm::SenderData;

let (_, session) = self
.inner
.group_session_manager
.create_outbound_group_session(room_id, EncryptionSettings::default())
.create_outbound_group_session(
room_id,
EncryptionSettings::default(),
SenderData::unknown(),
)
.await?;

self.store().save_inbound_group_sessions(&[session]).await?;
Expand All @@ -914,10 +922,16 @@ impl OlmMachine {
&self,
room_id: &RoomId,
) -> OlmResult<InboundGroupSession> {
use crate::olm::SenderData;

let (_, session) = self
.inner
.group_session_manager
.create_outbound_group_session(room_id, EncryptionSettings::default())
.create_outbound_group_session(
room_id,
EncryptionSettings::default(),
SenderData::unknown(),
)
.await?;

Ok(session)
Expand Down Expand Up @@ -4191,7 +4205,7 @@ pub(crate) mod tests {
let (outbound, mut inbound) = alice
.store()
.static_account()
.create_group_session_pair(room_id, Default::default())
.create_group_session_pair(room_id, Default::default(), SenderData::unknown())
.await
.unwrap();

Expand Down
13 changes: 9 additions & 4 deletions crates/matrix-sdk-crypto/src/olm/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ impl StaticAccountData {
&self,
room_id: &RoomId,
settings: EncryptionSettings,
own_sender_data: SenderData,
) -> Result<(OutboundGroupSession, InboundGroupSession), MegolmSessionCreationError> {
trace!(?room_id, algorithm = settings.algorithm.as_str(), "Creating a new room key");

Expand All @@ -221,7 +222,7 @@ impl StaticAccountData {
signing_key,
room_id,
&outbound.session_key().await,
SenderData::unknown(),
own_sender_data,
algorithm,
Some(visibility),
)?;
Expand All @@ -237,9 +238,13 @@ impl StaticAccountData {
&self,
room_id: &RoomId,
) -> (OutboundGroupSession, InboundGroupSession) {
self.create_group_session_pair(room_id, EncryptionSettings::default())
.await
.expect("Can't create default group session pair")
self.create_group_session_pair(
room_id,
EncryptionSettings::default(),
SenderData::unknown(),
)
.await
.expect("Can't create default group session pair")
}

/// Get the key ID of our Ed25519 signing key.
Expand Down
2 changes: 2 additions & 0 deletions crates/matrix-sdk-crypto/src/olm/group_sessions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ use serde::{Deserialize, Serialize};
mod inbound;
mod outbound;
mod sender_data;
mod sender_data_finder;

pub use inbound::{InboundGroupSession, PickledInboundGroupSession};
pub(crate) use outbound::ShareState;
pub use outbound::{
EncryptionSettings, OutboundGroupSession, PickledOutboundGroupSession, ShareInfo,
};
pub use sender_data::{SenderData, SenderDataRetryDetails};
pub(crate) use sender_data_finder::SenderDataFinder;
use thiserror::Error;
pub use vodozemac::megolm::{ExportedSessionKey, SessionKey};
use vodozemac::{megolm::SessionKeyDecodeError, Curve25519PublicKey};
Expand Down
11 changes: 9 additions & 2 deletions crates/matrix-sdk-crypto/src/olm/group_sessions/outbound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,10 @@ mod tests {
user_id, SecondsSinceUnixEpoch,
};

use crate::{olm::OutboundGroupSession, Account, EncryptionSettings, MegolmError};
use crate::{
olm::{OutboundGroupSession, SenderData},
Account, EncryptionSettings, MegolmError,
};

const TWO_HOURS: Duration = Duration::from_secs(60 * 60 * 2);

Expand Down Expand Up @@ -999,7 +1002,11 @@ mod tests {
Account::with_device_id(user_id!("@alice:example.org"), device_id!("DEVICEID"))
.static_data;
let (session, _) = account
.create_group_session_pair(room_id!("!test_room:example.org"), settings)
.create_group_session_pair(
room_id!("!test_room:example.org"),
settings,
SenderData::unknown(),
)
.await
.unwrap();
session
Expand Down
Loading

0 comments on commit 8845550

Please sign in to comment.