Skip to content

Commit 8845550

Browse files
andybalaampoljar
andauthored
crypto: Calculate sender data for incoming sessions
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]>
1 parent 84c9280 commit 8845550

File tree

10 files changed

+985
-27
lines changed

10 files changed

+985
-27
lines changed

crates/matrix-sdk-crypto/src/error.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,10 @@ pub enum SessionCreationError {
297297
/// Error when creating an Olm Session from an incoming Olm message.
298298
#[error(transparent)]
299299
InboundCreation(#[from] vodozemac::olm::SessionCreationError),
300+
301+
/// The given device keys are invalid.
302+
#[error("The given device keys are invalid")]
303+
InvalidDeviceKeys(#[from] SignatureError),
300304
}
301305

302306
/// Errors that can be returned by

crates/matrix-sdk-crypto/src/gossiping/machine.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1218,6 +1218,8 @@ mod tests {
12181218
create_sessions: bool,
12191219
algorithm: EventEncryptionAlgorithm,
12201220
) -> (GossipMachine, OutboundGroupSession, GossipMachine) {
1221+
use crate::olm::SenderData;
1222+
12211223
let alice_machine = get_machine_test_helper().await;
12221224
let alice_device = DeviceData::from_account(
12231225
&alice_machine.inner.store.cache().await.unwrap().account().await.unwrap(),
@@ -1270,7 +1272,7 @@ mod tests {
12701272
.inner
12711273
.store
12721274
.static_account()
1273-
.create_group_session_pair(room_id(), settings)
1275+
.create_group_session_pair(room_id(), settings, SenderData::unknown())
12741276
.await
12751277
.unwrap();
12761278

crates/matrix-sdk-crypto/src/machine.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ use crate::{
6666
identities::{user::UserIdentities, Device, IdentityManager, UserDevices},
6767
olm::{
6868
Account, CrossSigningStatus, EncryptionSettings, IdentityKeys, InboundGroupSession,
69-
OlmDecryptionInfo, PrivateCrossSigningIdentity, SenderData, SessionType, StaticAccountData,
69+
OlmDecryptionInfo, PrivateCrossSigningIdentity, SenderDataFinder, SessionType,
70+
StaticAccountData,
7071
},
7172
requests::{IncomingResponse, OutgoingRequest, UploadSigningKeysRequest},
7273
session_manager::{GroupSessionManager, SessionManager},
@@ -816,7 +817,8 @@ impl OlmMachine {
816817
event: &DecryptedRoomKeyEvent,
817818
content: &MegolmV1AesSha2Content,
818819
) -> OlmResult<Option<InboundGroupSession>> {
819-
let sender_data = SenderData::unknown();
820+
let sender_data =
821+
SenderDataFinder::find_using_event(self.store(), sender_key, event).await?;
820822

821823
let session = InboundGroupSession::new(
822824
sender_key,
@@ -897,10 +899,16 @@ impl OlmMachine {
897899
&self,
898900
room_id: &RoomId,
899901
) -> OlmResult<()> {
902+
use crate::olm::SenderData;
903+
900904
let (_, session) = self
901905
.inner
902906
.group_session_manager
903-
.create_outbound_group_session(room_id, EncryptionSettings::default())
907+
.create_outbound_group_session(
908+
room_id,
909+
EncryptionSettings::default(),
910+
SenderData::unknown(),
911+
)
904912
.await?;
905913

906914
self.store().save_inbound_group_sessions(&[session]).await?;
@@ -914,10 +922,16 @@ impl OlmMachine {
914922
&self,
915923
room_id: &RoomId,
916924
) -> OlmResult<InboundGroupSession> {
925+
use crate::olm::SenderData;
926+
917927
let (_, session) = self
918928
.inner
919929
.group_session_manager
920-
.create_outbound_group_session(room_id, EncryptionSettings::default())
930+
.create_outbound_group_session(
931+
room_id,
932+
EncryptionSettings::default(),
933+
SenderData::unknown(),
934+
)
921935
.await?;
922936

923937
Ok(session)
@@ -4191,7 +4205,7 @@ pub(crate) mod tests {
41914205
let (outbound, mut inbound) = alice
41924206
.store()
41934207
.static_account()
4194-
.create_group_session_pair(room_id, Default::default())
4208+
.create_group_session_pair(room_id, Default::default(), SenderData::unknown())
41954209
.await
41964210
.unwrap();
41974211

crates/matrix-sdk-crypto/src/olm/account.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ impl StaticAccountData {
198198
&self,
199199
room_id: &RoomId,
200200
settings: EncryptionSettings,
201+
own_sender_data: SenderData,
201202
) -> Result<(OutboundGroupSession, InboundGroupSession), MegolmSessionCreationError> {
202203
trace!(?room_id, algorithm = settings.algorithm.as_str(), "Creating a new room key");
203204

@@ -221,7 +222,7 @@ impl StaticAccountData {
221222
signing_key,
222223
room_id,
223224
&outbound.session_key().await,
224-
SenderData::unknown(),
225+
own_sender_data,
225226
algorithm,
226227
Some(visibility),
227228
)?;
@@ -237,9 +238,13 @@ impl StaticAccountData {
237238
&self,
238239
room_id: &RoomId,
239240
) -> (OutboundGroupSession, InboundGroupSession) {
240-
self.create_group_session_pair(room_id, EncryptionSettings::default())
241-
.await
242-
.expect("Can't create default group session pair")
241+
self.create_group_session_pair(
242+
room_id,
243+
EncryptionSettings::default(),
244+
SenderData::unknown(),
245+
)
246+
.await
247+
.expect("Can't create default group session pair")
243248
}
244249

245250
/// Get the key ID of our Ed25519 signing key.

crates/matrix-sdk-crypto/src/olm/group_sessions/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@ use serde::{Deserialize, Serialize};
1818
mod inbound;
1919
mod outbound;
2020
mod sender_data;
21+
mod sender_data_finder;
2122

2223
pub use inbound::{InboundGroupSession, PickledInboundGroupSession};
2324
pub(crate) use outbound::ShareState;
2425
pub use outbound::{
2526
EncryptionSettings, OutboundGroupSession, PickledOutboundGroupSession, ShareInfo,
2627
};
2728
pub use sender_data::{SenderData, SenderDataRetryDetails};
29+
pub(crate) use sender_data_finder::SenderDataFinder;
2830
use thiserror::Error;
2931
pub use vodozemac::megolm::{ExportedSessionKey, SessionKey};
3032
use vodozemac::{megolm::SessionKeyDecodeError, Curve25519PublicKey};

crates/matrix-sdk-crypto/src/olm/group_sessions/outbound.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -811,7 +811,10 @@ mod tests {
811811
user_id, SecondsSinceUnixEpoch,
812812
};
813813

814-
use crate::{olm::OutboundGroupSession, Account, EncryptionSettings, MegolmError};
814+
use crate::{
815+
olm::{OutboundGroupSession, SenderData},
816+
Account, EncryptionSettings, MegolmError,
817+
};
815818

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

@@ -999,7 +1002,11 @@ mod tests {
9991002
Account::with_device_id(user_id!("@alice:example.org"), device_id!("DEVICEID"))
10001003
.static_data;
10011004
let (session, _) = account
1002-
.create_group_session_pair(room_id!("!test_room:example.org"), settings)
1005+
.create_group_session_pair(
1006+
room_id!("!test_room:example.org"),
1007+
settings,
1008+
SenderData::unknown(),
1009+
)
10031010
.await
10041011
.unwrap();
10051012
session

0 commit comments

Comments
 (0)