Skip to content

Commit 25bb607

Browse files
committed
feat(crypto): Allow fetching encryption info using session ID
1 parent c9a6ae9 commit 25bb607

File tree

2 files changed

+62
-14
lines changed

2 files changed

+62
-14
lines changed

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

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1681,17 +1681,6 @@ impl OlmMachine {
16811681
})
16821682
}
16831683

1684-
async fn get_megolm_encryption_info(
1685-
&self,
1686-
room_id: &RoomId,
1687-
event: &EncryptedEvent,
1688-
content: &SupportedEventEncryptionSchemes<'_>,
1689-
) -> MegolmResult<EncryptionInfo> {
1690-
let session =
1691-
self.get_inbound_group_session_or_error(room_id, content.session_id()).await?;
1692-
self.get_encryption_info(&session, &event.sender).await
1693-
}
1694-
16951684
async fn decrypt_megolm_events(
16961685
&self,
16971686
room_id: &RoomId,
@@ -2089,7 +2078,30 @@ impl OlmMachine {
20892078
}
20902079
};
20912080

2092-
self.get_megolm_encryption_info(room_id, &event, &content).await
2081+
self.get_session_encryption_info(room_id, content.session_id(), &event.sender).await
2082+
}
2083+
2084+
/// Get encryption info for a megolm session.
2085+
///
2086+
/// This recalculates the [`EncryptionInfo`] data that is returned by
2087+
/// [`OlmMachine::decrypt_room_event`], based on the current
2088+
/// verification status of the sender, etc.
2089+
///
2090+
/// Returns an error if the session can't be found.
2091+
///
2092+
/// # Arguments
2093+
///
2094+
/// * `room_id` - The ID of the room where the session is being used.
2095+
/// * `session_id` - The ID of the session to get information for.
2096+
/// * `sender` - The user ID of the sender who created this session.
2097+
pub async fn get_session_encryption_info(
2098+
&self,
2099+
room_id: &RoomId,
2100+
session_id: &str,
2101+
sender: &UserId,
2102+
) -> MegolmResult<EncryptionInfo> {
2103+
let session = self.get_inbound_group_session_or_error(room_id, session_id).await?;
2104+
self.get_encryption_info(&session, sender).await
20932105
}
20942106

20952107
/// Update the list of tracked users.

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

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ use assert_matches2::{assert_let, assert_matches};
1818
use futures_util::{pin_mut, FutureExt, StreamExt};
1919
use itertools::Itertools;
2020
use matrix_sdk_common::deserialized_responses::{
21-
UnableToDecryptInfo, UnableToDecryptReason, UnsignedDecryptionResult, UnsignedEventLocation,
22-
WithheldCode,
21+
AlgorithmInfo, UnableToDecryptInfo, UnableToDecryptReason, UnsignedDecryptionResult,
22+
UnsignedEventLocation, VerificationLevel, VerificationState, WithheldCode,
2323
};
2424
use matrix_sdk_test::{async_test, message_like_event_content, ruma_response_from_json, test_json};
2525
use ruma::{
@@ -418,6 +418,42 @@ async fn test_room_key_sharing() {
418418
assert_eq!(room_key_updates[0].session_id, alice_session.session_id());
419419
}
420420

421+
#[async_test]
422+
async fn test_session_encryption_info_can_be_fetched() {
423+
// Given a megolm session has been established
424+
let (alice, bob) = get_machine_pair_with_session(alice_id(), user_id(), false).await;
425+
let room_id = room_id!("!test:example.org");
426+
427+
send_room_key_to_device(&alice, &bob, room_id).await.unwrap();
428+
429+
let alice_session =
430+
alice.inner.group_session_manager.get_outbound_group_session(room_id).unwrap();
431+
432+
let session = bob
433+
.store()
434+
.get_inbound_group_session(room_id, alice_session.session_id())
435+
.await
436+
.unwrap()
437+
.unwrap();
438+
439+
// When I request the encryption info about this session
440+
let encryption_info =
441+
bob.get_session_encryption_info(room_id, session.session_id(), alice_id()).await.unwrap();
442+
443+
// Then the expected info is returned
444+
assert_eq!(encryption_info.sender, alice_id());
445+
assert_eq!(encryption_info.sender_device.unwrap(), alice_device_id());
446+
assert_matches!(
447+
encryption_info.algorithm_info,
448+
AlgorithmInfo::MegolmV1AesSha2 { curve25519_key, .. }
449+
);
450+
assert_eq!(curve25519_key, alice_session.sender_key().to_string());
451+
assert_eq!(
452+
encryption_info.verification_state,
453+
VerificationState::Unverified(VerificationLevel::UnsignedDevice)
454+
);
455+
}
456+
421457
#[async_test]
422458
async fn test_to_device_messages_from_dehydrated_devices_are_ignored() {
423459
// Given alice's device is dehydrated

0 commit comments

Comments
 (0)