Skip to content

Commit 289f653

Browse files
committed
crypto: Allow finding SenderData based on known DeviceKeys
1 parent 7152714 commit 289f653

File tree

6 files changed

+192
-80
lines changed

6 files changed

+192
-80
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1216,6 +1216,8 @@ mod tests {
12161216
create_sessions: bool,
12171217
algorithm: EventEncryptionAlgorithm,
12181218
) -> (GossipMachine, OutboundGroupSession, GossipMachine) {
1219+
use crate::olm::SenderData;
1220+
12191221
let alice_machine = get_machine_test_helper().await;
12201222
let alice_device = ReadOnlyDevice::from_account(
12211223
&alice_machine.inner.store.cache().await.unwrap().account().await.unwrap(),
@@ -1268,7 +1270,7 @@ mod tests {
12681270
.inner
12691271
.store
12701272
.static_account()
1271-
.create_group_session_pair(room_id(), settings)
1273+
.create_group_session_pair(room_id(), settings, SenderData::unknown())
12721274
.await
12731275
.unwrap();
12741276

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

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ use crate::{
6666
identities::{user::UserIdentities, Device, IdentityManager, UserDevices},
6767
olm::{
6868
Account, CrossSigningStatus, EncryptionSettings, IdentityKeys, InboundGroupSession,
69-
OlmDecryptionInfo, PrivateCrossSigningIdentity, SenderDataFinder, SessionType,
69+
OlmDecryptionInfo, PrivateCrossSigningIdentity, SenderData, SenderDataFinder, SessionType,
7070
StaticAccountData,
7171
},
7272
requests::{IncomingResponse, OutgoingRequest, UploadSigningKeysRequest},
@@ -810,7 +810,8 @@ impl OlmMachine {
810810
event: &DecryptedRoomKeyEvent,
811811
content: &MegolmV1AesSha2Content,
812812
) -> OlmResult<Option<InboundGroupSession>> {
813-
let sender_data = SenderDataFinder::find(self, sender_key, event, content).await?;
813+
let sender_data =
814+
SenderDataFinder::find_using_event(self, sender_key, event, content).await?;
814815

815816
let session = InboundGroupSession::new(
816817
sender_key,
@@ -894,7 +895,11 @@ impl OlmMachine {
894895
let (_, session) = self
895896
.inner
896897
.group_session_manager
897-
.create_outbound_group_session(room_id, EncryptionSettings::default())
898+
.create_outbound_group_session(
899+
room_id,
900+
EncryptionSettings::default(),
901+
SenderData::unknown(),
902+
)
898903
.await?;
899904

900905
self.store().save_inbound_group_sessions(&[session]).await?;
@@ -911,7 +916,11 @@ impl OlmMachine {
911916
let (_, session) = self
912917
.inner
913918
.group_session_manager
914-
.create_outbound_group_session(room_id, EncryptionSettings::default())
919+
.create_outbound_group_session(
920+
room_id,
921+
EncryptionSettings::default(),
922+
SenderData::unknown(),
923+
)
915924
.await?;
916925

917926
Ok(session)
@@ -1010,7 +1019,26 @@ impl OlmMachine {
10101019
users: impl Iterator<Item = &UserId>,
10111020
encryption_settings: impl Into<EncryptionSettings>,
10121021
) -> OlmResult<Vec<Arc<ToDeviceRequest>>> {
1013-
self.inner.group_session_manager.share_room_key(room_id, users, encryption_settings).await
1022+
// Use our own device info to populate the SenderData that validates the
1023+
// InboundGroupSession that we create as a pair to the OutboundGroupSession we
1024+
// are sending out.
1025+
let account = self.store().static_account();
1026+
let device = self.store().get_device(account.user_id(), account.device_id()).await;
1027+
let own_sender_data = match device {
1028+
Ok(Some(device)) => {
1029+
SenderDataFinder::find_using_device_keys(self, device.as_device_keys().clone())
1030+
.await?
1031+
}
1032+
_ => {
1033+
error!("Unable to find our own device!");
1034+
SenderData::unknown()
1035+
}
1036+
};
1037+
1038+
self.inner
1039+
.group_session_manager
1040+
.share_room_key(room_id, users, encryption_settings, own_sender_data)
1041+
.await
10141042
}
10151043

10161044
/// Receive an unencrypted verification event.
@@ -4163,7 +4191,7 @@ pub(crate) mod tests {
41634191
let (outbound, mut inbound) = alice
41644192
.store()
41654193
.static_account()
4166-
.create_group_session_pair(room_id, Default::default())
4194+
.create_group_session_pair(room_id, Default::default(), SenderData::unknown())
41674195
.await
41684196
.unwrap();
41694197

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/outbound.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,10 @@ mod tests {
790790
user_id, SecondsSinceUnixEpoch,
791791
};
792792

793-
use crate::{olm::OutboundGroupSession, Account, EncryptionSettings, MegolmError};
793+
use crate::{
794+
olm::{OutboundGroupSession, SenderData},
795+
Account, EncryptionSettings, MegolmError,
796+
};
794797

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

@@ -978,7 +981,11 @@ mod tests {
978981
Account::with_device_id(user_id!("@alice:example.org"), device_id!("DEVICEID"))
979982
.static_data;
980983
let (session, _) = account
981-
.create_group_session_pair(room_id!("!test_room:example.org"), settings)
984+
.create_group_session_pair(
985+
room_id!("!test_room:example.org"),
986+
settings,
987+
SenderData::unknown(),
988+
)
982989
.await
983990
.unwrap();
984991
session

0 commit comments

Comments
 (0)