diff --git a/crates/matrix-sdk-crypto/src/machine.rs b/crates/matrix-sdk-crypto/src/machine.rs
index 3cb1bad0c..0ee514e62 100644
--- a/crates/matrix-sdk-crypto/src/machine.rs
+++ b/crates/matrix-sdk-crypto/src/machine.rs
@@ -1289,15 +1289,15 @@ impl OlmMachine {
if let MegolmError::Decryption(DecryptionError::UnknownMessageIndex(_, _)) =
error
{
- let withheld_info = self
+ let withheld_code = self
.store
.get_withheld_info(room_id, content.session_id())
.await?
- .map(|i| i.withheld_code);
+ .map(|i| i.withheld_code());
- if withheld_info.is_some() {
+ if withheld_code.is_some() {
// Partially withheld, report with a withheld code if we have one.
- MegolmError::MissingRoomKey(withheld_info)
+ MegolmError::MissingRoomKey(withheld_code)
} else {
error
}
@@ -1307,12 +1307,13 @@ impl OlmMachine {
),
}
} else {
- let withheld_info = self
+ let withheld_code = self
.store
.get_withheld_info(room_id, content.session_id())
.await?
- .map(|i| i.withheld_code);
- Err(MegolmError::MissingRoomKey(withheld_info))
+ .map(|i| i.withheld_code());
+
+ Err(MegolmError::MissingRoomKey(withheld_code))
}
}
diff --git a/crates/matrix-sdk-crypto/src/olm/group_sessions/outbound.rs b/crates/matrix-sdk-crypto/src/olm/group_sessions/outbound.rs
index e6f7f23c3..8a64d8b9f 100644
--- a/crates/matrix-sdk-crypto/src/olm/group_sessions/outbound.rs
+++ b/crates/matrix-sdk-crypto/src/olm/group_sessions/outbound.rs
@@ -51,7 +51,7 @@ use crate::{
MegolmV1AesSha2Content, RoomEncryptedEventContent, RoomEventEncryptionScheme,
},
room_key::{MegolmV1AesSha2Content as MegolmV1AesSha2RoomKeyContent, RoomKeyContent},
- room_key_withheld::WithheldCode,
+ room_key_withheld::{RoomKeyWithheldContent, WithheldCode},
},
EventEncryptionAlgorithm,
},
@@ -242,6 +242,19 @@ impl OutboundGroupSession {
self.to_share_with_set.insert(request_id, (request, share_infos));
}
+ /// Create a new `m.room_key.withheld` event content with the given code for
+ /// this outbound group session.
+ pub fn withheld_code(&self, code: WithheldCode) -> RoomKeyWithheldContent {
+ RoomKeyWithheldContent::new(
+ self.settings().algorithm.to_owned(),
+ code,
+ self.room_id().to_owned(),
+ self.session_id().to_owned(),
+ self.sender_key().to_owned(),
+ (*self.device_id).to_owned(),
+ )
+ }
+
/// This should be called if an the user wishes to rotate this session.
pub fn invalidate_session(&self) {
self.invalidated.store(true, Ordering::Relaxed)
diff --git a/crates/matrix-sdk-crypto/src/session_manager/group_sessions.rs b/crates/matrix-sdk-crypto/src/session_manager/group_sessions.rs
index 49fa1a6e6..55e726595 100644
--- a/crates/matrix-sdk-crypto/src/session_manager/group_sessions.rs
+++ b/crates/matrix-sdk-crypto/src/session_manager/group_sessions.rs
@@ -586,15 +586,7 @@ impl GroupSessionManager {
) -> OlmResult<()> {
// Convert a withheld code for the group session into a to-device event content.
let to_content = |code| {
- let content = RoomKeyWithheldContent::create(
- group_session.settings().algorithm.to_owned(),
- code,
- group_session.room_id().to_owned(),
- group_session.session_id().to_owned(),
- group_session.sender_key(),
- Some(self.account.device_id().to_owned()),
- );
-
+ let content = group_session.withheld_code(code);
Raw::new(&content).expect("We can always serialize a withheld content info").cast()
};
diff --git a/crates/matrix-sdk-crypto/src/store/integration_tests.rs b/crates/matrix-sdk-crypto/src/store/integration_tests.rs
index d983a70ba..2dd3a9770 100644
--- a/crates/matrix-sdk-crypto/src/store/integration_tests.rs
+++ b/crates/matrix-sdk-crypto/src/store/integration_tests.rs
@@ -8,7 +8,7 @@ macro_rules! cryptostore_integration_tests {
use matrix_sdk_test::async_test;
use ruma::{
device_id, encryption::SignedKey, room_id, serde::Base64, user_id, DeviceId,
- OwnedDeviceId, OwnedUserId, TransactionId, UserId,
+ JsOption, OwnedDeviceId, OwnedUserId, TransactionId, UserId,
};
use $crate::{
olm::{
@@ -22,7 +22,8 @@ macro_rules! cryptostore_integration_tests {
testing::{get_device, get_other_identity, get_own_identity},
types::{
events::{
- room_key_request::MegolmV1AesSha2Content, room_key_withheld::WithheldCode,
+ room_key_request::MegolmV1AesSha2Content,
+ room_key_withheld::{CommonWithheldCodeContent, WithheldCode},
},
EventEncryptionAlgorithm,
},
@@ -629,30 +630,28 @@ macro_rules! cryptostore_integration_tests {
let session_id_1 = "GBnDxGP9i3IkPsz3/ihNr6P7qjIXxSRVWZ1MYmSn09w";
let session_id_2 = "IDLtnNCH2kIr3xIf1B7JFkGpQmTjyMca2jww+X6zeOE";
- let info = DirectWithheldInfo {
+ let content = CommonWithheldCodeContent {
room_id: room_id.to_owned(),
- algorithm: EventEncryptionAlgorithm::MegolmV1AesSha2,
session_id: session_id_1.into(),
- claimed_sender_key: Curve25519PublicKey::from_base64(
+ from_device: JsOption::Undefined,
+ other: Default::default(),
+ sender_key: Curve25519PublicKey::from_base64(
"9n7mdWKOjr9c4NTlG6zV8dbFtNK79q9vZADoh7nMUwA",
)
.unwrap(),
- withheld_code: WithheldCode::Unverified,
};
+ let info = DirectWithheldInfo::new(
+ EventEncryptionAlgorithm::MegolmV1AesSha2,
+ WithheldCode::Unverified,
+ content.to_owned(),
+ );
info_list.push(info);
-
- let info = DirectWithheldInfo {
- room_id: room_id.to_owned(),
- algorithm: EventEncryptionAlgorithm::MegolmV1AesSha2,
- session_id: session_id_2.into(),
- claimed_sender_key: Curve25519PublicKey::from_base64(
- "9n7mdWKOjr9c4NTlG6zV8dbFtNK79q9vZADoh7nMUwA",
- )
- .unwrap(),
- withheld_code: WithheldCode::Blacklisted,
- };
-
+ let info = DirectWithheldInfo::new(
+ EventEncryptionAlgorithm::MegolmV1AesSha2,
+ WithheldCode::Blacklisted,
+ content,
+ );
info_list.push(info);
let changes = Changes { withheld_session_info: info_list, ..Default::default() };
@@ -662,9 +661,9 @@ macro_rules! cryptostore_integration_tests {
let is_withheld = store.get_withheld_info(room_id, session_id_1).await.unwrap();
if let Some(info) = is_withheld {
- let actual_code = info.withheld_code;
- assert_eq!(EventEncryptionAlgorithm::MegolmV1AesSha2, info.algorithm);
- assert_eq!(room_id, info.room_id);
+ let actual_code = info.withheld_code();
+ assert_eq!(EventEncryptionAlgorithm::MegolmV1AesSha2, info.algorithm());
+ assert_eq!(room_id, info.room_id());
assert_eq!(WithheldCode::Unverified, actual_code);
} else {
panic!();
@@ -673,7 +672,7 @@ macro_rules! cryptostore_integration_tests {
let is_withheld = store.get_withheld_info(room_id, session_id_2).await.unwrap();
if let Some(info) = is_withheld {
- let actual_code = info.withheld_code;
+ let actual_code = info.withheld_code();
assert_eq!(WithheldCode::Blacklisted, actual_code);
} else {
panic!();
diff --git a/crates/matrix-sdk-crypto/src/store/memorystore.rs b/crates/matrix-sdk-crypto/src/store/memorystore.rs
index 40f07b0a3..b4b56dd3d 100644
--- a/crates/matrix-sdk-crypto/src/store/memorystore.rs
+++ b/crates/matrix-sdk-crypto/src/store/memorystore.rs
@@ -150,7 +150,7 @@ impl CryptoStore for MemoryStore {
}
for info in changes.withheld_session_info {
- self.direct_withheld_info.insert(info.session_id.to_owned(), info.to_owned());
+ self.direct_withheld_info.insert(info.session_id().to_owned(), info);
}
Ok(())
@@ -285,19 +285,11 @@ impl CryptoStore for MemoryStore {
room_id: &RoomId,
session_id: &str,
) -> Result