Skip to content

Commit 5d3f9ed

Browse files
committed
crypto: Calculate sender data for incoming sessions
1 parent 03d4a30 commit 5d3f9ed

File tree

8 files changed

+976
-24
lines changed

8 files changed

+976
-24
lines changed

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 = ReadOnlyDevice::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: 34 additions & 6 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, SenderData, SenderDataFinder, SessionType,
70+
StaticAccountData,
7071
},
7172
requests::{IncomingResponse, OutgoingRequest, UploadSigningKeysRequest},
7273
session_manager::{GroupSessionManager, SessionManager},
@@ -816,7 +817,7 @@ impl OlmMachine {
816817
event: &DecryptedRoomKeyEvent,
817818
content: &MegolmV1AesSha2Content,
818819
) -> OlmResult<Option<InboundGroupSession>> {
819-
let sender_data = SenderData::unknown();
820+
let sender_data = SenderDataFinder::find_using_event(self, sender_key, event).await?;
820821

821822
let session = InboundGroupSession::new(
822823
sender_key,
@@ -900,7 +901,11 @@ impl OlmMachine {
900901
let (_, session) = self
901902
.inner
902903
.group_session_manager
903-
.create_outbound_group_session(room_id, EncryptionSettings::default())
904+
.create_outbound_group_session(
905+
room_id,
906+
EncryptionSettings::default(),
907+
SenderData::unknown(),
908+
)
904909
.await?;
905910

906911
self.store().save_inbound_group_sessions(&[session]).await?;
@@ -917,7 +922,11 @@ impl OlmMachine {
917922
let (_, session) = self
918923
.inner
919924
.group_session_manager
920-
.create_outbound_group_session(room_id, EncryptionSettings::default())
925+
.create_outbound_group_session(
926+
room_id,
927+
EncryptionSettings::default(),
928+
SenderData::unknown(),
929+
)
921930
.await?;
922931

923932
Ok(session)
@@ -1016,7 +1025,26 @@ impl OlmMachine {
10161025
users: impl Iterator<Item = &UserId>,
10171026
encryption_settings: impl Into<EncryptionSettings>,
10181027
) -> OlmResult<Vec<Arc<ToDeviceRequest>>> {
1019-
self.inner.group_session_manager.share_room_key(room_id, users, encryption_settings).await
1028+
// Use our own device info to populate the SenderData that validates the
1029+
// InboundGroupSession that we create as a pair to the OutboundGroupSession we
1030+
// are sending out.
1031+
let account = self.store().static_account();
1032+
let device = self.store().get_device(account.user_id(), account.device_id()).await;
1033+
let own_sender_data = match device {
1034+
Ok(Some(device)) => {
1035+
SenderDataFinder::find_using_device_keys(self, device.as_device_keys().clone())
1036+
.await?
1037+
}
1038+
_ => {
1039+
error!("Unable to find our own device!");
1040+
SenderData::unknown()
1041+
}
1042+
};
1043+
1044+
self.inner
1045+
.group_session_manager
1046+
.share_room_key(room_id, users, encryption_settings, own_sender_data)
1047+
.await
10201048
}
10211049

10221050
/// Receive an unencrypted verification event.
@@ -4169,7 +4197,7 @@ pub(crate) mod tests {
41694197
let (outbound, mut inbound) = alice
41704198
.store()
41714199
.static_account()
4172-
.create_group_session_pair(room_id, Default::default())
4200+
.create_group_session_pair(room_id, Default::default(), SenderData::unknown())
41734201
.await
41744202
.unwrap();
41754203

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)