matrix-sdk: Update ruma

Co-authored-by: Damir Jelić <poljar@termina.org.uk>
master
Jonas Platte 2021-06-20 01:35:28 +02:00 committed by Damir Jelić
parent bdd35206e8
commit 43e213fd67
30 changed files with 369 additions and 362 deletions

View File

@ -20,5 +20,5 @@ byteorder = "1.4.3"
image = { version = "0.23.14", optional = true } image = { version = "0.23.14", optional = true }
qrcode = { version = "0.12.0", default-features = false } qrcode = { version = "0.12.0", default-features = false }
rqrr = { version = "0.3.2" , optional = true } rqrr = { version = "0.3.2" , optional = true }
ruma-identifiers = "0.19.1" ruma-identifiers = { version = "0.19.3", git = "https://github.com/ruma/ruma", rev = "d73ab8add" }
thiserror = "1.0.24" thiserror = "1.0.24"

View File

@ -56,6 +56,8 @@ default_features = false
[dependencies.ruma] [dependencies.ruma]
version = "0.1.2" version = "0.1.2"
git = "https://github.com/ruma/ruma"
rev = "d73ab8add"
features = ["client-api-c", "compat", "unstable-pre-spec"] features = ["client-api-c", "compat", "unstable-pre-spec"]
[dependencies.tokio-stream] [dependencies.tokio-stream]

View File

@ -142,7 +142,7 @@ async fn login(
if let Some(Verification::SasV1(sas)) = client if let Some(Verification::SasV1(sas)) = client
.get_verification( .get_verification(
&e.sender, &e.sender,
e.content.relation.event_id.as_str(), e.content.relates_to.event_id.as_str(),
) )
.await .await
{ {
@ -153,7 +153,7 @@ async fn login(
if let Some(Verification::SasV1(sas)) = client if let Some(Verification::SasV1(sas)) = client
.get_verification( .get_verification(
&e.sender, &e.sender,
e.content.relation.event_id.as_str(), e.content.relates_to.event_id.as_str(),
) )
.await .await
{ {

View File

@ -1786,8 +1786,8 @@ impl Client {
request: &ToDeviceRequest, request: &ToDeviceRequest,
) -> Result<ToDeviceResponse> { ) -> Result<ToDeviceResponse> {
let txn_id_string = request.txn_id_string(); let txn_id_string = request.txn_id_string();
let request = RumaToDeviceRequest::new( let request = RumaToDeviceRequest::new_raw(
request.event_type.clone(), request.event_type.as_str(),
&txn_id_string, &txn_id_string,
request.messages.clone(), request.messages.clone(),
); );

View File

@ -47,6 +47,7 @@ use ruma::{
GlobalAccountDataEvent, RoomAccountDataEvent, StrippedStateEvent, SyncEphemeralRoomEvent, GlobalAccountDataEvent, RoomAccountDataEvent, StrippedStateEvent, SyncEphemeralRoomEvent,
SyncMessageEvent, SyncStateEvent, SyncMessageEvent, SyncStateEvent,
}, },
serde::Raw,
RoomId, RoomId,
}; };
use serde_json::value::RawValue as RawJsonValue; use serde_json::value::RawValue as RawJsonValue;
@ -89,14 +90,24 @@ impl Handler {
self.handle_room_account_data_event(room.clone(), &event).await; self.handle_room_account_data_event(room.clone(), &event).await;
} }
for event in room_info.state.events.iter().filter_map(|e| e.deserialize().ok()) { for (raw_event, event) in room_info.state.events.iter().filter_map(|e| {
self.handle_state_event(room.clone(), &event).await; if let Ok(d) = e.deserialize() {
Some((e, d))
} else {
None
}
}) {
self.handle_state_event(room.clone(), &event, raw_event).await;
} }
for event in for (raw_event, event) in room_info.timeline.events.iter().filter_map(|e| {
room_info.timeline.events.iter().filter_map(|e| e.event.deserialize().ok()) if let Ok(d) = e.event.deserialize() {
{ Some((&e.event, d))
self.handle_timeline_event(room.clone(), &event).await; } else {
None
}
}) {
self.handle_timeline_event(room.clone(), &event, raw_event).await;
} }
} }
} }
@ -109,14 +120,24 @@ impl Handler {
self.handle_room_account_data_event(room.clone(), &event).await; self.handle_room_account_data_event(room.clone(), &event).await;
} }
for event in room_info.state.events.iter().filter_map(|e| e.deserialize().ok()) { for (raw_event, event) in room_info.state.events.iter().filter_map(|e| {
self.handle_state_event(room.clone(), &event).await; if let Ok(d) = e.deserialize() {
Some((e, d))
} else {
None
}
}) {
self.handle_state_event(room.clone(), &event, raw_event).await;
} }
for event in for (raw_event, event) in room_info.timeline.events.iter().filter_map(|e| {
room_info.timeline.events.iter().filter_map(|e| e.event.deserialize().ok()) if let Ok(d) = e.event.deserialize() {
{ Some((&e.event, d))
self.handle_timeline_event(room.clone(), &event).await; } else {
None
}
}) {
self.handle_timeline_event(room.clone(), &event, raw_event).await;
} }
} }
} }
@ -144,7 +165,12 @@ impl Handler {
} }
} }
async fn handle_timeline_event(&self, room: Room, event: &AnySyncRoomEvent) { async fn handle_timeline_event(
&self,
room: Room,
event: &AnySyncRoomEvent,
raw_event: &Raw<AnySyncRoomEvent>,
) {
match event { match event {
AnySyncRoomEvent::State(event) => match event { AnySyncRoomEvent::State(event) => match event {
AnySyncStateEvent::RoomMember(e) => self.on_room_member(room, e).await, AnySyncStateEvent::RoomMember(e) => self.on_room_member(room, e).await,
@ -157,10 +183,25 @@ impl Handler {
AnySyncStateEvent::RoomPowerLevels(e) => self.on_room_power_levels(room, e).await, AnySyncStateEvent::RoomPowerLevels(e) => self.on_room_power_levels(room, e).await,
AnySyncStateEvent::RoomTombstone(e) => self.on_room_tombstone(room, e).await, AnySyncStateEvent::RoomTombstone(e) => self.on_room_tombstone(room, e).await,
AnySyncStateEvent::RoomJoinRules(e) => self.on_room_join_rules(room, e).await, AnySyncStateEvent::RoomJoinRules(e) => self.on_room_join_rules(room, e).await,
AnySyncStateEvent::Custom(e) => { AnySyncStateEvent::PolicyRuleRoom(_)
self.on_custom_event(room, &CustomEvent::State(e)).await | AnySyncStateEvent::PolicyRuleServer(_)
| AnySyncStateEvent::PolicyRuleUser(_)
| AnySyncStateEvent::RoomCreate(_)
| AnySyncStateEvent::RoomEncryption(_)
| AnySyncStateEvent::RoomGuestAccess(_)
| AnySyncStateEvent::RoomHistoryVisibility(_)
| AnySyncStateEvent::RoomPinnedEvents(_)
| AnySyncStateEvent::RoomServerAcl(_)
| AnySyncStateEvent::RoomThirdPartyInvite(_)
| AnySyncStateEvent::RoomTopic(_)
| AnySyncStateEvent::SpaceChild(_)
| AnySyncStateEvent::SpaceParent(_) => {}
_ => {
if let Ok(e) = raw_event.deserialize_as::<SyncStateEvent<CustomEventContent>>()
{
self.on_custom_event(room, &CustomEvent::State(&e)).await;
}
} }
_ => {}
}, },
AnySyncRoomEvent::Message(event) => match event { AnySyncRoomEvent::Message(event) => match event {
AnySyncMessageEvent::RoomMessage(e) => self.on_room_message(room, e).await, AnySyncMessageEvent::RoomMessage(e) => self.on_room_message(room, e).await,
@ -169,23 +210,40 @@ impl Handler {
} }
AnySyncMessageEvent::RoomRedaction(e) => self.on_room_redaction(room, e).await, AnySyncMessageEvent::RoomRedaction(e) => self.on_room_redaction(room, e).await,
AnySyncMessageEvent::Reaction(e) => self.on_room_reaction(room, e).await, AnySyncMessageEvent::Reaction(e) => self.on_room_reaction(room, e).await,
AnySyncMessageEvent::Custom(e) => {
self.on_custom_event(room, &CustomEvent::Message(e)).await
}
AnySyncMessageEvent::CallInvite(e) => self.on_room_call_invite(room, e).await, AnySyncMessageEvent::CallInvite(e) => self.on_room_call_invite(room, e).await,
AnySyncMessageEvent::CallAnswer(e) => self.on_room_call_answer(room, e).await, AnySyncMessageEvent::CallAnswer(e) => self.on_room_call_answer(room, e).await,
AnySyncMessageEvent::CallCandidates(e) => { AnySyncMessageEvent::CallCandidates(e) => {
self.on_room_call_candidates(room, e).await self.on_room_call_candidates(room, e).await
} }
AnySyncMessageEvent::CallHangup(e) => self.on_room_call_hangup(room, e).await, AnySyncMessageEvent::CallHangup(e) => self.on_room_call_hangup(room, e).await,
_ => {} AnySyncMessageEvent::KeyVerificationReady(_)
| AnySyncMessageEvent::KeyVerificationStart(_)
| AnySyncMessageEvent::KeyVerificationCancel(_)
| AnySyncMessageEvent::KeyVerificationAccept(_)
| AnySyncMessageEvent::KeyVerificationKey(_)
| AnySyncMessageEvent::KeyVerificationMac(_)
| AnySyncMessageEvent::KeyVerificationDone(_)
| AnySyncMessageEvent::RoomEncrypted(_)
| AnySyncMessageEvent::Sticker(_) => {}
_ => {
if let Ok(e) =
raw_event.deserialize_as::<SyncMessageEvent<CustomEventContent>>()
{
self.on_custom_event(room, &CustomEvent::Message(&e)).await;
}
}
}, },
AnySyncRoomEvent::RedactedState(_event) => {} AnySyncRoomEvent::RedactedState(_event) => {}
AnySyncRoomEvent::RedactedMessage(_event) => {} AnySyncRoomEvent::RedactedMessage(_event) => {}
} }
} }
async fn handle_state_event(&self, room: Room, event: &AnySyncStateEvent) { async fn handle_state_event(
&self,
room: Room,
event: &AnySyncStateEvent,
raw_event: &Raw<AnySyncStateEvent>,
) {
match event { match event {
AnySyncStateEvent::RoomMember(member) => self.on_state_member(room, member).await, AnySyncStateEvent::RoomMember(member) => self.on_state_member(room, member).await,
AnySyncStateEvent::RoomName(name) => self.on_state_name(room, name).await, AnySyncStateEvent::RoomName(name) => self.on_state_name(room, name).await,
@ -202,10 +260,24 @@ impl Handler {
// TODO make `on_state_tombstone` method // TODO make `on_state_tombstone` method
self.on_room_tombstone(room, tomb).await self.on_room_tombstone(room, tomb).await
} }
AnySyncStateEvent::Custom(custom) => { AnySyncStateEvent::PolicyRuleRoom(_)
self.on_custom_event(room, &CustomEvent::State(custom)).await | AnySyncStateEvent::PolicyRuleServer(_)
| AnySyncStateEvent::PolicyRuleUser(_)
| AnySyncStateEvent::RoomCreate(_)
| AnySyncStateEvent::RoomEncryption(_)
| AnySyncStateEvent::RoomGuestAccess(_)
| AnySyncStateEvent::RoomHistoryVisibility(_)
| AnySyncStateEvent::RoomPinnedEvents(_)
| AnySyncStateEvent::RoomServerAcl(_)
| AnySyncStateEvent::RoomThirdPartyInvite(_)
| AnySyncStateEvent::RoomTopic(_)
| AnySyncStateEvent::SpaceChild(_)
| AnySyncStateEvent::SpaceParent(_) => {}
_ => {
if let Ok(e) = raw_event.deserialize_as::<SyncStateEvent<CustomEventContent>>() {
self.on_custom_event(room, &CustomEvent::State(&e)).await;
}
} }
_ => {}
} }
} }

View File

@ -33,6 +33,8 @@ matrix-sdk = { version = "0.2", path = "../matrix_sdk", default-features = false
[dependencies.ruma] [dependencies.ruma]
version = "0.1.2" version = "0.1.2"
git = "https://github.com/ruma/ruma"
rev = "d73ab8add"
features = ["client-api-c", "appservice-api-s", "unstable-pre-spec"] features = ["client-api-c", "appservice-api-s", "unstable-pre-spec"]
[dev-dependencies] [dev-dependencies]

View File

@ -26,7 +26,7 @@ docs = ["encryption", "sled_cryptostore"]
[dependencies] [dependencies]
dashmap = "4.0.2" dashmap = "4.0.2"
lru = "0.6.5" lru = "0.6.5"
ruma = { version = "0.1.2", features = ["client-api-c", "unstable-pre-spec"] } ruma = { version = "0.1.2", features = ["client-api-c", "unstable-pre-spec"], git = "https://github.com/ruma/ruma", rev = "d73ab8add" }
serde = { version = "1.0.122", features = ["rc"] } serde = { version = "1.0.122", features = ["rc"] }
serde_json = "1.0.61" serde_json = "1.0.61"
tracing = "0.1.22" tracing = "0.1.22"

View File

@ -13,7 +13,7 @@ version = "0.2.0"
[dependencies] [dependencies]
async-trait = "0.1.42" async-trait = "0.1.42"
instant = { version = "0.1.9", features = ["wasm-bindgen", "now"] } instant = { version = "0.1.9", features = ["wasm-bindgen", "now"] }
ruma = { version = "0.1.2", features = ["client-api-c"] } ruma = { version = "0.1.2", features = ["client-api-c"], git = "https://github.com/ruma/ruma", rev = "d73ab8add" }
serde = "1.0.122" serde = "1.0.122"
[target.'cfg(not(target_arch = "wasm32"))'.dependencies] [target.'cfg(not(target_arch = "wasm32"))'.dependencies]

View File

@ -22,7 +22,7 @@ docs = ["sled_cryptostore"]
[dependencies] [dependencies]
matrix-qrcode = { version = "0.1.0", path = "../matrix_qrcode" } matrix-qrcode = { version = "0.1.0", path = "../matrix_qrcode" }
matrix-sdk-common = { version = "0.2.0", path = "../matrix_sdk_common" } matrix-sdk-common = { version = "0.2.0", path = "../matrix_sdk_common" }
ruma = { version = "0.1.2", features = ["client-api-c", "unstable-pre-spec"] } ruma = { version = "0.1.2", features = ["client-api-c", "unstable-pre-spec"], git = "https://github.com/ruma/ruma", rev = "d73ab8add" }
olm-rs = { version = "1.0.0", features = ["serde"] } olm-rs = { version = "1.0.0", features = ["serde"] }
getrandom = "0.2.2" getrandom = "0.2.2"

View File

@ -25,11 +25,10 @@ use std::{
use atomic::Atomic; use atomic::Atomic;
use matrix_sdk_common::locks::Mutex; use matrix_sdk_common::locks::Mutex;
use ruma::{ use ruma::{
api::client::r0::keys::SignedKey, encryption::{DeviceKeys, SignedKey},
encryption::DeviceKeys,
events::{ events::{
forwarded_room_key::ForwardedRoomKeyToDeviceEventContent, forwarded_room_key::ForwardedRoomKeyToDeviceEventContent,
room::encrypted::EncryptedEventContent, EventType, room::encrypted::EncryptedEventContent, AnyToDeviceEventContent,
}, },
identifiers::{ identifiers::{
DeviceId, DeviceIdBox, DeviceKeyAlgorithm, DeviceKeyId, EventEncryptionAlgorithm, UserId, DeviceId, DeviceIdBox, DeviceKeyAlgorithm, DeviceKeyId, EventEncryptionAlgorithm, UserId,
@ -176,15 +175,12 @@ impl Device {
/// ///
/// # Arguments /// # Arguments
/// ///
/// * `event_type` - The type of the event.
///
/// * `content` - The content of the event that should be encrypted. /// * `content` - The content of the event that should be encrypted.
pub(crate) async fn encrypt( pub(crate) async fn encrypt(
&self, &self,
event_type: EventType, content: AnyToDeviceEventContent,
content: Value,
) -> OlmResult<(Session, EncryptedEventContent)> { ) -> OlmResult<(Session, EncryptedEventContent)> {
self.inner.encrypt(&*self.verification_machine.store, event_type, content).await self.inner.encrypt(&*self.verification_machine.store, content).await
} }
/// Encrypt the given inbound group session as a forwarded room key for this /// Encrypt the given inbound group session as a forwarded room key for this
@ -213,8 +209,7 @@ impl Device {
); );
}; };
let content = serde_json::to_value(content)?; self.encrypt(AnyToDeviceEventContent::ForwardedRoomKey(content)).await
self.encrypt(EventType::ForwardedRoomKey, content).await
} }
} }
@ -421,8 +416,7 @@ impl ReadOnlyDevice {
pub(crate) async fn encrypt( pub(crate) async fn encrypt(
&self, &self,
store: &dyn CryptoStore, store: &dyn CryptoStore,
event_type: EventType, content: AnyToDeviceEventContent,
content: Value,
) -> OlmResult<(Session, EncryptedEventContent)> { ) -> OlmResult<(Session, EncryptedEventContent)> {
let sender_key = if let Some(k) = self.get_key(DeviceKeyAlgorithm::Curve25519) { let sender_key = if let Some(k) = self.get_key(DeviceKeyAlgorithm::Curve25519) {
k k
@ -455,7 +449,7 @@ impl ReadOnlyDevice {
return Err(OlmError::MissingSession); return Err(OlmError::MissingSession);
}; };
let message = session.encrypt(self, event_type, content).await?; let message = session.encrypt(self, content).await?;
Ok((session, message)) Ok((session, message))
} }

View File

@ -22,7 +22,7 @@ use std::{
}; };
use ruma::{ use ruma::{
api::client::r0::keys::{CrossSigningKey, KeyUsage}, encryption::{CrossSigningKey, KeyUsage},
DeviceKeyId, UserId, DeviceKeyId, UserId,
}; };
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};

View File

@ -20,21 +20,20 @@
// If we don't trust the device store an object that remembers the request and // If we don't trust the device store an object that remembers the request and
// let the users introspect that object. // let the users introspect that object.
use std::{collections::BTreeMap, sync::Arc}; use std::sync::Arc;
use dashmap::{mapref::entry::Entry, DashMap, DashSet}; use dashmap::{mapref::entry::Entry, DashMap, DashSet};
use matrix_sdk_common::uuid::Uuid; use matrix_sdk_common::uuid::Uuid;
use ruma::{ use ruma::{
api::client::r0::to_device::DeviceIdOrAllDevices,
events::{ events::{
forwarded_room_key::ForwardedRoomKeyToDeviceEventContent, forwarded_room_key::ForwardedRoomKeyToDeviceEventContent,
room_key_request::{Action, RequestedKeyInfo, RoomKeyRequestToDeviceEventContent}, room_key_request::{Action, RequestedKeyInfo, RoomKeyRequestToDeviceEventContent},
AnyToDeviceEvent, EventType, ToDeviceEvent, AnyToDeviceEvent, AnyToDeviceEventContent, ToDeviceEvent,
}, },
identifiers::{DeviceId, DeviceIdBox, EventEncryptionAlgorithm, RoomId, UserId}, identifiers::{DeviceId, DeviceIdBox, EventEncryptionAlgorithm, RoomId, UserId},
to_device::DeviceIdOrAllDevices,
}; };
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json::value::to_raw_value;
use thiserror::Error; use thiserror::Error;
use tracing::{error, info, trace, warn}; use tracing::{error, info, trace, warn};
@ -150,7 +149,7 @@ pub struct OutgoingKeyRequest {
} }
impl OutgoingKeyRequest { impl OutgoingKeyRequest {
fn to_request(&self, own_device_id: &DeviceId) -> Result<OutgoingRequest, serde_json::Error> { fn to_request(&self, own_device_id: &DeviceId) -> OutgoingRequest {
let content = RoomKeyRequestToDeviceEventContent::new( let content = RoomKeyRequestToDeviceEventContent::new(
Action::Request, Action::Request,
Some(self.info.clone()), Some(self.info.clone()),
@ -158,13 +157,17 @@ impl OutgoingKeyRequest {
self.request_id.to_string(), self.request_id.to_string(),
); );
wrap_key_request_content(self.request_recipient.clone(), self.request_id, &content) let request = ToDeviceRequest::new_with_id(
&self.request_recipient,
DeviceIdOrAllDevices::AllDevices,
AnyToDeviceEventContent::RoomKeyRequest(content),
self.request_id,
);
OutgoingRequest { request_id: request.txn_id, request: Arc::new(request.into()) }
} }
fn to_cancellation( fn to_cancellation(&self, own_device_id: &DeviceId) -> OutgoingRequest {
&self,
own_device_id: &DeviceId,
) -> Result<OutgoingRequest, serde_json::Error> {
let content = RoomKeyRequestToDeviceEventContent::new( let content = RoomKeyRequestToDeviceEventContent::new(
Action::CancelRequest, Action::CancelRequest,
None, None,
@ -172,8 +175,13 @@ impl OutgoingKeyRequest {
self.request_id.to_string(), self.request_id.to_string(),
); );
let id = Uuid::new_v4(); let request = ToDeviceRequest::new(
wrap_key_request_content(self.request_recipient.clone(), id, &content) &self.request_recipient,
DeviceIdOrAllDevices::AllDevices,
AnyToDeviceEventContent::RoomKeyRequest(content),
);
OutgoingRequest { request_id: request.txn_id, request: Arc::new(request.into()) }
} }
} }
@ -187,26 +195,6 @@ impl PartialEq for OutgoingKeyRequest {
} }
} }
fn wrap_key_request_content(
recipient: UserId,
id: Uuid,
content: &RoomKeyRequestToDeviceEventContent,
) -> Result<OutgoingRequest, serde_json::Error> {
let mut messages = BTreeMap::new();
messages
.entry(recipient)
.or_insert_with(BTreeMap::new)
.insert(DeviceIdOrAllDevices::AllDevices, to_raw_value(content)?);
Ok(OutgoingRequest {
request_id: id,
request: Arc::new(
ToDeviceRequest { event_type: EventType::RoomKeyRequest, txn_id: id, messages }.into(),
),
})
}
impl KeyRequestMachine { impl KeyRequestMachine {
pub fn new( pub fn new(
user_id: Arc<UserId>, user_id: Arc<UserId>,
@ -229,13 +217,14 @@ impl KeyRequestMachine {
/// Load stored outgoing requests that were not yet sent out. /// Load stored outgoing requests that were not yet sent out.
async fn load_outgoing_requests(&self) -> Result<Vec<OutgoingRequest>, CryptoStoreError> { async fn load_outgoing_requests(&self) -> Result<Vec<OutgoingRequest>, CryptoStoreError> {
self.store Ok(self
.store
.get_unsent_key_requests() .get_unsent_key_requests()
.await? .await?
.into_iter() .into_iter()
.filter(|i| !i.sent_out) .filter(|i| !i.sent_out)
.map(|info| info.to_request(self.device_id()).map_err(CryptoStoreError::from)) .map(|info| info.to_request(self.device_id()))
.collect() .collect())
} }
/// Our own user id. /// Our own user id.
@ -448,23 +437,15 @@ impl KeyRequestMachine {
let (used_session, content) = let (used_session, content) =
device.encrypt_session(session.clone(), message_index).await?; device.encrypt_session(session.clone(), message_index).await?;
let id = Uuid::new_v4(); let request = ToDeviceRequest::new(
let mut messages = BTreeMap::new(); device.user_id(),
device.device_id().to_owned(),
messages.entry(device.user_id().to_owned()).or_insert_with(BTreeMap::new).insert( AnyToDeviceEventContent::RoomEncrypted(content),
DeviceIdOrAllDevices::DeviceId(device.device_id().into()),
to_raw_value(&content)?,
); );
let request = OutgoingRequest { let request =
request_id: id, OutgoingRequest { request_id: request.txn_id, request: Arc::new(request.into()) };
request: Arc::new( self.outgoing_to_device_requests.insert(request.request_id, request);
ToDeviceRequest { event_type: EventType::RoomEncrypted, txn_id: id, messages }
.into(),
),
};
self.outgoing_to_device_requests.insert(id, request);
Ok(used_session) Ok(used_session)
} }
@ -594,8 +575,8 @@ impl KeyRequestMachine {
let request = self.store.get_key_request_by_info(&key_info).await?; let request = self.store.get_key_request_by_info(&key_info).await?;
if let Some(request) = request { if let Some(request) = request {
let cancel = request.to_cancellation(self.device_id())?; let cancel = request.to_cancellation(self.device_id());
let request = request.to_request(self.device_id())?; let request = request.to_request(self.device_id());
Ok((Some(cancel), request)) Ok((Some(cancel), request))
} else { } else {
@ -618,7 +599,7 @@ impl KeyRequestMachine {
sent_out: false, sent_out: false,
}; };
let outgoing_request = request.to_request(self.device_id())?; let outgoing_request = request.to_request(self.device_id());
self.save_outgoing_key_info(request).await?; self.save_outgoing_key_info(request).await?;
Ok(outgoing_request) Ok(outgoing_request)
@ -717,7 +698,7 @@ impl KeyRequestMachine {
// can delete it in one transaction. // can delete it in one transaction.
self.delete_key_info(&key_info).await?; self.delete_key_info(&key_info).await?;
let request = key_info.to_cancellation(self.device_id())?; let request = key_info.to_cancellation(self.device_id());
self.outgoing_to_device_requests.insert(request.request_id, request); self.outgoing_to_device_requests.insert(request.request_id, request);
Ok(()) Ok(())
@ -789,13 +770,14 @@ mod test {
use matrix_sdk_common::locks::Mutex; use matrix_sdk_common::locks::Mutex;
use matrix_sdk_test::async_test; use matrix_sdk_test::async_test;
use ruma::{ use ruma::{
api::client::r0::to_device::DeviceIdOrAllDevices,
events::{ events::{
forwarded_room_key::ForwardedRoomKeyToDeviceEventContent, forwarded_room_key::ForwardedRoomKeyToDeviceEventContent,
room::encrypted::EncryptedEventContent, room::encrypted::EncryptedEventContent,
room_key_request::RoomKeyRequestToDeviceEventContent, AnyToDeviceEvent, ToDeviceEvent, room_key_request::RoomKeyRequestToDeviceEventContent, AnyToDeviceEvent, ToDeviceEvent,
}, },
room_id, user_id, DeviceIdBox, RoomId, UserId, room_id,
to_device::DeviceIdOrAllDevices,
user_id, DeviceIdBox, RoomId, UserId,
}; };
use super::{KeyRequestMachine, KeyshareDecision}; use super::{KeyRequestMachine, KeyshareDecision};
@ -1203,8 +1185,7 @@ mod test {
.unwrap() .unwrap()
.get(&DeviceIdOrAllDevices::AllDevices) .get(&DeviceIdOrAllDevices::AllDevices)
.unwrap(); .unwrap();
let content: RoomKeyRequestToDeviceEventContent = let content: RoomKeyRequestToDeviceEventContent = content.deserialize_as().unwrap();
serde_json::from_str(content.get()).unwrap();
alice_machine.mark_outgoing_request_as_sent(id).await.unwrap(); alice_machine.mark_outgoing_request_as_sent(id).await.unwrap();
@ -1233,7 +1214,7 @@ mod test {
.unwrap() .unwrap()
.get(&DeviceIdOrAllDevices::DeviceId(alice_device_id())) .get(&DeviceIdOrAllDevices::DeviceId(alice_device_id()))
.unwrap(); .unwrap();
let content: EncryptedEventContent = serde_json::from_str(content.get()).unwrap(); let content: EncryptedEventContent = content.deserialize_as().unwrap();
bob_machine.mark_outgoing_request_as_sent(id).await.unwrap(); bob_machine.mark_outgoing_request_as_sent(id).await.unwrap();
@ -1336,8 +1317,7 @@ mod test {
.unwrap() .unwrap()
.get(&DeviceIdOrAllDevices::AllDevices) .get(&DeviceIdOrAllDevices::AllDevices)
.unwrap(); .unwrap();
let content: RoomKeyRequestToDeviceEventContent = let content: RoomKeyRequestToDeviceEventContent = content.deserialize_as().unwrap();
serde_json::from_str(content.get()).unwrap();
alice_machine.mark_outgoing_request_as_sent(id).await.unwrap(); alice_machine.mark_outgoing_request_as_sent(id).await.unwrap();
@ -1382,7 +1362,7 @@ mod test {
.unwrap() .unwrap()
.get(&DeviceIdOrAllDevices::DeviceId(alice_device_id())) .get(&DeviceIdOrAllDevices::DeviceId(alice_device_id()))
.unwrap(); .unwrap();
let content: EncryptedEventContent = serde_json::from_str(content.get()).unwrap(); let content: EncryptedEventContent = content.deserialize_as().unwrap();
bob_machine.mark_outgoing_request_as_sent(id).await.unwrap(); bob_machine.mark_outgoing_request_as_sent(id).await.unwrap();

View File

@ -755,11 +755,11 @@ impl OlmMachine {
| AnyToDeviceEvent::KeyVerificationStart(..) => { | AnyToDeviceEvent::KeyVerificationStart(..) => {
self.handle_verification_event(event).await; self.handle_verification_event(event).await;
} }
AnyToDeviceEvent::Dummy(_) => {} AnyToDeviceEvent::Dummy(_)
AnyToDeviceEvent::RoomKey(_) => {} | AnyToDeviceEvent::RoomKey(_)
AnyToDeviceEvent::ForwardedRoomKey(_) => {} | AnyToDeviceEvent::ForwardedRoomKey(_)
AnyToDeviceEvent::RoomEncrypted(_) => {} | AnyToDeviceEvent::RoomEncrypted(_) => {}
AnyToDeviceEvent::Custom(_) => {} _ => {}
} }
} }
@ -1231,21 +1231,22 @@ pub(crate) mod test {
use matrix_sdk_test::test_json; use matrix_sdk_test::test_json;
use ruma::{ use ruma::{
api::{ api::{
client::r0::keys::{claim_keys, get_keys, upload_keys, OneTimeKey}, client::r0::keys::{claim_keys, get_keys, upload_keys},
IncomingResponse, IncomingResponse,
}, },
encryption::OneTimeKey,
events::{ events::{
dummy::DummyToDeviceEventContent,
room::{ room::{
encrypted::EncryptedEventContent, encrypted::EncryptedEventContent,
message::{MessageEventContent, MessageType}, message::{MessageEventContent, MessageType},
}, },
AnyMessageEventContent, AnySyncMessageEvent, AnySyncRoomEvent, AnyToDeviceEvent, AnyMessageEventContent, AnySyncMessageEvent, AnySyncRoomEvent, AnyToDeviceEvent,
EventType, SyncMessageEvent, ToDeviceEvent, Unsigned, AnyToDeviceEventContent, SyncMessageEvent, ToDeviceEvent, Unsigned,
}, },
identifiers::{ identifiers::{
event_id, room_id, user_id, DeviceId, DeviceKeyAlgorithm, DeviceKeyId, UserId, event_id, room_id, user_id, DeviceId, DeviceKeyAlgorithm, DeviceKeyId, UserId,
}, },
serde::Raw,
uint, MilliSecondsSinceUnixEpoch, uint, MilliSecondsSinceUnixEpoch,
}; };
use serde_json::json; use serde_json::json;
@ -1291,12 +1292,16 @@ pub(crate) mod test {
fn to_device_requests_to_content(requests: Vec<Arc<ToDeviceRequest>>) -> EncryptedEventContent { fn to_device_requests_to_content(requests: Vec<Arc<ToDeviceRequest>>) -> EncryptedEventContent {
let to_device_request = &requests[0]; let to_device_request = &requests[0];
let content: Raw<EncryptedEventContent> = serde_json::from_str( to_device_request
to_device_request.messages.values().next().unwrap().values().next().unwrap().get(), .messages
) .values()
.unwrap(); .next()
.unwrap()
content.deserialize().unwrap() .values()
.next()
.unwrap()
.deserialize_as()
.unwrap()
} }
pub(crate) async fn get_prepared_machine() -> (OlmMachine, OneTimeKeys) { pub(crate) async fn get_prepared_machine() -> (OlmMachine, OneTimeKeys) {
@ -1358,7 +1363,10 @@ pub(crate) mod test {
let bob_device = alice.get_device(&bob.user_id, &bob.device_id).await.unwrap().unwrap(); let bob_device = alice.get_device(&bob.user_id, &bob.device_id).await.unwrap().unwrap();
let (session, content) = bob_device.encrypt(EventType::Dummy, json!({})).await.unwrap(); let (session, content) = bob_device
.encrypt(AnyToDeviceEventContent::Dummy(DummyToDeviceEventContent::new()))
.await
.unwrap();
alice.store.save_sessions(&[session]).await.unwrap(); alice.store.save_sessions(&[session]).await.unwrap();
let event = ToDeviceEvent { sender: alice.user_id().clone(), content }; let event = ToDeviceEvent { sender: alice.user_id().clone(), content };
@ -1593,7 +1601,11 @@ pub(crate) mod test {
let event = ToDeviceEvent { let event = ToDeviceEvent {
sender: alice.user_id().clone(), sender: alice.user_id().clone(),
content: bob_device.encrypt(EventType::Dummy, json!({})).await.unwrap().1, content: bob_device
.encrypt(AnyToDeviceEventContent::Dummy(DummyToDeviceEventContent::new()))
.await
.unwrap()
.1,
}; };
let event = bob.decrypt_to_device_event(&event).await.unwrap().event.deserialize().unwrap(); let event = bob.decrypt_to_device_event(&event).await.unwrap().event.deserialize().unwrap();

View File

@ -30,13 +30,9 @@ use olm_rs::{
session::{OlmMessage, PreKeyMessage}, session::{OlmMessage, PreKeyMessage},
PicklingMode, PicklingMode,
}; };
#[cfg(test)]
use ruma::events::EventType;
use ruma::{ use ruma::{
api::client::r0::keys::{ api::client::r0::keys::{upload_keys, upload_signatures::Request as SignatureUploadRequest},
upload_keys, upload_signatures::Request as SignatureUploadRequest, OneTimeKey, SignedKey, encryption::{DeviceKeys, OneTimeKey, SignedKey},
},
encryption::DeviceKeys,
events::{ events::{
room::encrypted::{EncryptedEventContent, EncryptedEventScheme}, room::encrypted::{EncryptedEventContent, EncryptedEventScheme},
AnyToDeviceEvent, ToDeviceEvent, AnyToDeviceEvent, ToDeviceEvent,
@ -993,6 +989,8 @@ impl ReadOnlyAccount {
#[cfg(test)] #[cfg(test)]
pub(crate) async fn create_session_for(&self, other: &ReadOnlyAccount) -> (Session, Session) { pub(crate) async fn create_session_for(&self, other: &ReadOnlyAccount) -> (Session, Session) {
use ruma::events::{dummy::DummyToDeviceEventContent, AnyToDeviceEventContent};
other.generate_one_time_keys_helper(1).await; other.generate_one_time_keys_helper(1).await;
let one_time = other.signed_one_time_keys().await.unwrap(); let one_time = other.signed_one_time_keys().await.unwrap();
@ -1003,7 +1001,10 @@ impl ReadOnlyAccount {
other.mark_keys_as_published().await; other.mark_keys_as_published().await;
let message = our_session.encrypt(&device, EventType::Dummy, json!({})).await.unwrap(); let message = our_session
.encrypt(&device, AnyToDeviceEventContent::Dummy(DummyToDeviceEventContent::new()))
.await
.unwrap();
let content = if let EncryptedEventScheme::OlmV1Curve25519AesSha2(c) = message.scheme { let content = if let EncryptedEventScheme::OlmV1Curve25519AesSha2(c) = message.scheme {
c c
} else { } else {

View File

@ -320,11 +320,9 @@ impl InboundGroupSession {
decrypted_object.get_mut("content").map(|c| c.as_object_mut()).flatten() decrypted_object.get_mut("content").map(|c| c.as_object_mut()).flatten()
{ {
if !decrypted_content.contains_key("m.relates_to") { if !decrypted_content.contains_key("m.relates_to") {
if let Some(relation) = &event.content.relates_to { let content = serde_json::to_value(&event.content)?;
decrypted_content.insert( if let Some(relation) = content.as_object().and_then(|o| o.get("m.relates_to")) {
"m.relates_to".to_owned(), decrypted_content.insert("m.relates_to".to_owned(), relation.to_owned());
serde_json::to_value(relation).unwrap_or_default(),
);
} }
} }
} }

View File

@ -34,20 +34,20 @@ use olm_rs::{
errors::OlmGroupSessionError, outbound_group_session::OlmOutboundGroupSession, PicklingMode, errors::OlmGroupSessionError, outbound_group_session::OlmOutboundGroupSession, PicklingMode,
}; };
use ruma::{ use ruma::{
api::client::r0::to_device::DeviceIdOrAllDevices,
events::{ events::{
room::{ room::{
encrypted::{EncryptedEventContent, EncryptedEventScheme, MegolmV1AesSha2ContentInit}, encrypted::{EncryptedEventContent, EncryptedEventScheme, MegolmV1AesSha2ContentInit},
encryption::EncryptionEventContent, encryption::EncryptionEventContent,
history_visibility::HistoryVisibility, history_visibility::HistoryVisibility,
message::Relation,
}, },
AnyMessageEventContent, EventContent, room_key::RoomKeyToDeviceEventContent,
AnyMessageEventContent, AnyToDeviceEventContent, EventContent,
}, },
to_device::DeviceIdOrAllDevices,
DeviceId, DeviceIdBox, EventEncryptionAlgorithm, RoomId, UserId, DeviceId, DeviceIdBox, EventEncryptionAlgorithm, RoomId, UserId,
}; };
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json::{json, Value}; use serde_json::json;
use tracing::{debug, error, trace}; use tracing::{debug, error, trace};
use super::{ use super::{
@ -277,13 +277,8 @@ impl OutboundGroupSession {
"type": content.event_type(), "type": content.event_type(),
}); });
let relates_to: Option<Relation> = json_content
.get("content")
.map(|c| c.get("m.relates_to").cloned().map(|r| serde_json::from_value(r).ok()))
.flatten()
.flatten();
let plaintext = json_content.to_string(); let plaintext = json_content.to_string();
let relation = content.relation();
let ciphertext = self.encrypt_helper(plaintext).await; let ciphertext = self.encrypt_helper(plaintext).await;
@ -297,7 +292,7 @@ impl OutboundGroupSession {
EncryptedEventContent::new( EncryptedEventContent::new(
EncryptedEventScheme::MegolmV1AesSha2(encrypted_content), EncryptedEventScheme::MegolmV1AesSha2(encrypted_content),
relates_to, relation,
) )
} }
@ -361,16 +356,15 @@ impl OutboundGroupSession {
session.session_message_index() session.session_message_index()
} }
/// Get the outbound group session key as a json value that can be sent as a pub(crate) async fn as_content(&self) -> AnyToDeviceEventContent {
/// m.room_key. let session_key = self.session_key().await;
pub async fn as_json(&self) -> Value {
json!({ AnyToDeviceEventContent::RoomKey(RoomKeyToDeviceEventContent::new(
"algorithm": EventEncryptionAlgorithm::MegolmV1AesSha2, EventEncryptionAlgorithm::MegolmV1AesSha2,
"room_id": &*self.room_id, self.room_id().to_owned(),
"session_id": &*self.session_id, self.session_id().to_owned(),
"session_key": self.session_key().await, session_key.0.clone(),
"chain_index": self.message_index().await, ))
})
} }
/// Has or will the session be shared with the given user/device pair. /// Has or will the session be shared with the given user/device pair.

View File

@ -63,9 +63,8 @@ pub(crate) mod test {
use olm_rs::session::OlmMessage; use olm_rs::session::OlmMessage;
use ruma::{ use ruma::{
api::client::r0::keys::SignedKey, encryption::SignedKey, events::forwarded_room_key::ForwardedRoomKeyToDeviceEventContent,
events::forwarded_room_key::ForwardedRoomKeyToDeviceEventContent, room_id, user_id, room_id, user_id, DeviceId, UserId,
DeviceId, UserId,
}; };
use crate::olm::{InboundGroupSession, ReadOnlyAccount, Session}; use crate::olm::{InboundGroupSession, ReadOnlyAccount, Session};

View File

@ -26,12 +26,12 @@ use ruma::{
CiphertextInfo, EncryptedEventContent, EncryptedEventScheme, CiphertextInfo, EncryptedEventContent, EncryptedEventScheme,
OlmV1Curve25519AesSha2Content, OlmV1Curve25519AesSha2Content,
}, },
EventType, AnyToDeviceEventContent, EventContent,
}, },
identifiers::{DeviceId, DeviceKeyAlgorithm, UserId}, identifiers::{DeviceId, DeviceKeyAlgorithm, UserId},
}; };
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json::{json, Value}; use serde_json::json;
use super::{deserialize_instant, serialize_instant, IdentityKeys}; use super::{deserialize_instant, serialize_instant, IdentityKeys};
use crate::{ use crate::{
@ -105,21 +105,17 @@ impl Session {
/// encrypted, this needs to be the device that was used to create this /// encrypted, this needs to be the device that was used to create this
/// session with. /// session with.
/// ///
/// * `event_type` - The type of the event.
///
/// * `content` - The content of the event. /// * `content` - The content of the event.
pub async fn encrypt( pub async fn encrypt(
&mut self, &mut self,
recipient_device: &ReadOnlyDevice, recipient_device: &ReadOnlyDevice,
event_type: EventType, content: AnyToDeviceEventContent,
content: Value,
) -> OlmResult<EncryptedEventContent> { ) -> OlmResult<EncryptedEventContent> {
let recipient_signing_key = recipient_device let recipient_signing_key = recipient_device
.get_key(DeviceKeyAlgorithm::Ed25519) .get_key(DeviceKeyAlgorithm::Ed25519)
.ok_or(EventError::MissingSigningKey)?; .ok_or(EventError::MissingSigningKey)?;
let relates_to = let event_type = content.event_type();
content.get("m.relates_to").cloned().and_then(|v| serde_json::from_value(v).ok());
let payload = json!({ let payload = json!({
"sender": self.user_id.as_str(), "sender": self.user_id.as_str(),
@ -149,7 +145,7 @@ impl Session {
content, content,
self.our_identity_keys.curve25519().to_string(), self.our_identity_keys.curve25519().to_string(),
)), )),
relates_to, None,
)) ))
} }

View File

@ -25,8 +25,8 @@ use std::{
use matrix_sdk_common::locks::Mutex; use matrix_sdk_common::locks::Mutex;
use pk_signing::{MasterSigning, PickledSignings, SelfSigning, Signing, SigningError, UserSigning}; use pk_signing::{MasterSigning, PickledSignings, SelfSigning, Signing, SigningError, UserSigning};
use ruma::{ use ruma::{
api::client::r0::keys::{upload_signatures::Request as SignatureUploadRequest, KeyUsage}, api::client::r0::keys::upload_signatures::Request as SignatureUploadRequest,
encryption::DeviceKeys, encryption::{DeviceKeys, KeyUsage},
DeviceKeyAlgorithm, DeviceKeyId, UserId, DeviceKeyAlgorithm, DeviceKeyId, UserId,
}; };
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -403,7 +403,7 @@ mod test {
use std::{collections::BTreeMap, sync::Arc}; use std::{collections::BTreeMap, sync::Arc};
use matrix_sdk_test::async_test; use matrix_sdk_test::async_test;
use ruma::{api::client::r0::keys::CrossSigningKey, user_id, UserId}; use ruma::{encryption::CrossSigningKey, user_id, UserId};
use super::{PrivateCrossSigningIdentity, Signing}; use super::{PrivateCrossSigningIdentity, Signing};
use crate::{ use crate::{

View File

@ -24,8 +24,7 @@ use olm_rs::pk::OlmPkSigning;
#[cfg(test)] #[cfg(test)]
use olm_rs::{errors::OlmUtilityError, utility::OlmUtility}; use olm_rs::{errors::OlmUtilityError, utility::OlmUtility};
use ruma::{ use ruma::{
api::client::r0::keys::{CrossSigningKey, KeyUsage}, encryption::{CrossSigningKey, DeviceKeys, KeyUsage},
encryption::DeviceKeys,
serde::CanonicalJsonValue, serde::CanonicalJsonValue,
DeviceKeyAlgorithm, DeviceKeyId, UserId, DeviceKeyAlgorithm, DeviceKeyId, UserId,
}; };

View File

@ -25,16 +25,17 @@ use ruma::{
Request as SignatureUploadRequest, Response as SignatureUploadResponse, Request as SignatureUploadRequest, Response as SignatureUploadResponse,
}, },
upload_signing_keys::Response as SigningKeysUploadResponse, upload_signing_keys::Response as SigningKeysUploadResponse,
CrossSigningKey,
}, },
message::send_message_event::Response as RoomMessageResponse, message::send_message_event::Response as RoomMessageResponse,
to_device::{send_event_to_device::Response as ToDeviceResponse, DeviceIdOrAllDevices}, to_device::send_event_to_device::Response as ToDeviceResponse,
}, },
encryption::CrossSigningKey,
events::{AnyMessageEventContent, AnyToDeviceEventContent, EventContent, EventType}, events::{AnyMessageEventContent, AnyToDeviceEventContent, EventContent, EventType},
serde::Raw,
to_device::DeviceIdOrAllDevices,
DeviceIdBox, RoomId, UserId, DeviceIdBox, RoomId, UserId,
}; };
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json::value::RawValue as RawJsonValue;
/// Customized version of /// Customized version of
/// `ruma_client_api::r0::to_device::send_event_to_device::Request`, /// `ruma_client_api::r0::to_device::send_event_to_device::Request`,
@ -54,7 +55,7 @@ pub struct ToDeviceRequest {
/// The content's type for this field will be updated in a future /// The content's type for this field will be updated in a future
/// release, until then you can create a value using /// release, until then you can create a value using
/// `serde_json::value::to_raw_value`. /// `serde_json::value::to_raw_value`.
pub messages: BTreeMap<UserId, BTreeMap<DeviceIdOrAllDevices, Box<RawJsonValue>>>, pub messages: BTreeMap<UserId, BTreeMap<DeviceIdOrAllDevices, Raw<AnyToDeviceEventContent>>>,
} }
impl ToDeviceRequest { impl ToDeviceRequest {
@ -73,18 +74,24 @@ impl ToDeviceRequest {
recipient: &UserId, recipient: &UserId,
recipient_device: impl Into<DeviceIdOrAllDevices>, recipient_device: impl Into<DeviceIdOrAllDevices>,
content: AnyToDeviceEventContent, content: AnyToDeviceEventContent,
) -> Self {
Self::new_with_id(recipient, recipient_device, content, Uuid::new_v4())
}
pub(crate) fn new_with_id(
recipient: &UserId,
recipient_device: impl Into<DeviceIdOrAllDevices>,
content: AnyToDeviceEventContent,
txn_id: Uuid,
) -> Self { ) -> Self {
let mut messages = BTreeMap::new(); let mut messages = BTreeMap::new();
let mut user_messages = BTreeMap::new(); let mut user_messages = BTreeMap::new();
user_messages.insert(
recipient_device.into(),
serde_json::value::to_raw_value(&content).expect("Can't serialize to-device content"),
);
messages.insert(recipient.clone(), user_messages);
let event_type = EventType::from(content.event_type()); let event_type = EventType::from(content.event_type());
ToDeviceRequest { txn_id: Uuid::new_v4(), event_type, messages } user_messages.insert(recipient_device.into(), Raw::from(content));
messages.insert(recipient.clone(), user_messages);
ToDeviceRequest { event_type, txn_id, messages }
} }
/// Gets the transaction ID as a string. /// Gets the transaction ID as a string.

View File

@ -21,14 +21,14 @@ use dashmap::DashMap;
use futures::future::join_all; use futures::future::join_all;
use matrix_sdk_common::{executor::spawn, uuid::Uuid}; use matrix_sdk_common::{executor::spawn, uuid::Uuid};
use ruma::{ use ruma::{
api::client::r0::to_device::DeviceIdOrAllDevices,
events::{ events::{
room::{encrypted::EncryptedEventContent, history_visibility::HistoryVisibility}, room::{encrypted::EncryptedEventContent, history_visibility::HistoryVisibility},
AnyMessageEventContent, EventType, AnyMessageEventContent, AnyToDeviceEventContent, EventType,
}, },
serde::Raw,
to_device::DeviceIdOrAllDevices,
DeviceId, DeviceIdBox, RoomId, UserId, DeviceId, DeviceIdBox, RoomId, UserId,
}; };
use serde_json::Value;
use tracing::{debug, info, trace}; use tracing::{debug, info, trace};
use crate::{ use crate::{
@ -224,22 +224,22 @@ impl GroupSessionManager {
/// Encrypt the given content for the given devices and create a to-device /// Encrypt the given content for the given devices and create a to-device
/// requests that sends the encrypted content to them. /// requests that sends the encrypted content to them.
async fn encrypt_session_for( async fn encrypt_session_for(
content: Value, content: AnyToDeviceEventContent,
devices: Vec<Device>, devices: Vec<Device>,
) -> OlmResult<(Uuid, ToDeviceRequest, Vec<Session>)> { ) -> OlmResult<(Uuid, ToDeviceRequest, Vec<Session>)> {
let mut messages = BTreeMap::new(); let mut messages = BTreeMap::new();
let mut changed_sessions = Vec::new(); let mut changed_sessions = Vec::new();
let encrypt = |device: Device, content: Value| async move { let encrypt = |device: Device, content: AnyToDeviceEventContent| async move {
let mut message = BTreeMap::new(); let mut message = BTreeMap::new();
let encrypted = device.encrypt(EventType::RoomKey, content.clone()).await; let encrypted = device.encrypt(content.clone()).await;
let used_session = match encrypted { let used_session = match encrypted {
Ok((session, encrypted)) => { Ok((session, encrypted)) => {
message.entry(device.user_id().clone()).or_insert_with(BTreeMap::new).insert( message.entry(device.user_id().clone()).or_insert_with(BTreeMap::new).insert(
DeviceIdOrAllDevices::DeviceId(device.device_id().into()), DeviceIdOrAllDevices::DeviceId(device.device_id().into()),
serde_json::value::to_raw_value(&encrypted)?, Raw::from(AnyToDeviceEventContent::RoomEncrypted(encrypted)),
); );
Some(session) Some(session)
} }
@ -375,7 +375,7 @@ impl GroupSessionManager {
pub async fn encrypt_request( pub async fn encrypt_request(
chunk: Vec<Device>, chunk: Vec<Device>,
content: Value, content: AnyToDeviceEventContent,
outbound: OutboundGroupSession, outbound: OutboundGroupSession,
message_index: u32, message_index: u32,
being_shared: Arc<DashMap<Uuid, OutboundGroupSession>>, being_shared: Arc<DashMap<Uuid, OutboundGroupSession>>,
@ -466,7 +466,7 @@ impl GroupSessionManager {
.flatten() .flatten()
.collect(); .collect();
let key_content = outbound.as_json().await; let key_content = outbound.as_content().await;
let message_index = outbound.message_index().await; let message_index = outbound.message_index().await;
if !devices.is_empty() { if !devices.is_empty() {

View File

@ -17,15 +17,13 @@ use std::{collections::BTreeMap, sync::Arc, time::Duration};
use dashmap::{DashMap, DashSet}; use dashmap::{DashMap, DashSet};
use matrix_sdk_common::uuid::Uuid; use matrix_sdk_common::uuid::Uuid;
use ruma::{ use ruma::{
api::client::r0::{ api::client::r0::keys::claim_keys::{
keys::claim_keys::{Request as KeysClaimRequest, Response as KeysClaimResponse}, Request as KeysClaimRequest, Response as KeysClaimResponse,
to_device::DeviceIdOrAllDevices,
}, },
assign, assign,
events::EventType, events::{dummy::DummyToDeviceEventContent, AnyToDeviceEventContent},
DeviceId, DeviceIdBox, DeviceKeyAlgorithm, UserId, DeviceId, DeviceIdBox, DeviceKeyAlgorithm, UserId,
}; };
use serde_json::{json, value::to_raw_value};
use tracing::{error, info, warn}; use tracing::{error, info, warn};
use crate::{ use crate::{
@ -118,28 +116,21 @@ impl SessionManager {
async fn check_if_unwedged(&self, user_id: &UserId, device_id: &DeviceId) -> OlmResult<()> { async fn check_if_unwedged(&self, user_id: &UserId, device_id: &DeviceId) -> OlmResult<()> {
if self.wedged_devices.get(user_id).map(|d| d.remove(device_id)).flatten().is_some() { if self.wedged_devices.get(user_id).map(|d| d.remove(device_id)).flatten().is_some() {
if let Some(device) = self.store.get_device(user_id, device_id).await? { if let Some(device) = self.store.get_device(user_id, device_id).await? {
let (_, content) = device.encrypt(EventType::Dummy, json!({})).await?; let content = AnyToDeviceEventContent::Dummy(DummyToDeviceEventContent::new());
let id = Uuid::new_v4(); let (_, content) = device.encrypt(content).await?;
let mut messages = BTreeMap::new();
messages.entry(device.user_id().to_owned()).or_insert_with(BTreeMap::new).insert( let request = ToDeviceRequest::new(
DeviceIdOrAllDevices::DeviceId(device.device_id().into()), device.user_id(),
to_raw_value(&content)?, device.device_id().to_owned(),
AnyToDeviceEventContent::RoomEncrypted(content),
); );
let request = OutgoingRequest { let request = OutgoingRequest {
request_id: id, request_id: request.txn_id,
request: Arc::new( request: Arc::new(request.into()),
ToDeviceRequest {
event_type: EventType::RoomEncrypted,
txn_id: id,
messages,
}
.into(),
),
}; };
self.outgoing_to_device_requests.insert(id, request); self.outgoing_to_device_requests.insert(request.request_id, request);
} }
} }

View File

@ -757,7 +757,7 @@ mod test {
use matrix_sdk_test::async_test; use matrix_sdk_test::async_test;
use olm_rs::outbound_group_session::OlmOutboundGroupSession; use olm_rs::outbound_group_session::OlmOutboundGroupSession;
use ruma::{ use ruma::{
api::client::r0::keys::SignedKey, encryption::SignedKey,
events::room_key_request::RequestedKeyInfo, events::room_key_request::RequestedKeyInfo,
identifiers::{room_id, user_id, DeviceId, EventEncryptionAlgorithm, UserId}, identifiers::{room_id, user_id, DeviceId, EventEncryptionAlgorithm, UserId},
}; };

View File

@ -79,8 +79,7 @@ impl AnyEvent<'_> {
| AnyMessageEvent::RoomEncrypted(_) | AnyMessageEvent::RoomEncrypted(_)
| AnyMessageEvent::RoomMessageFeedback(_) | AnyMessageEvent::RoomMessageFeedback(_)
| AnyMessageEvent::RoomRedaction(_) | AnyMessageEvent::RoomRedaction(_)
| AnyMessageEvent::Sticker(_) | AnyMessageEvent::Sticker(_) => None,
| AnyMessageEvent::Custom(_) => None,
AnyMessageEvent::RoomMessage(m) => { AnyMessageEvent::RoomMessage(m) => {
if let MessageType::VerificationRequest(v) = &m.content.msgtype { if let MessageType::VerificationRequest(v) = &m.content.msgtype {
Some(RequestContent::from(v).into()) Some(RequestContent::from(v).into())
@ -105,14 +104,14 @@ impl AnyEvent<'_> {
AnyMessageEvent::KeyVerificationDone(e) => { AnyMessageEvent::KeyVerificationDone(e) => {
Some(DoneContent::from(&e.content).into()) Some(DoneContent::from(&e.content).into())
} }
_ => None,
}, },
AnyEvent::ToDevice(e) => match e { AnyEvent::ToDevice(e) => match e {
AnyToDeviceEvent::Dummy(_) AnyToDeviceEvent::Dummy(_)
| AnyToDeviceEvent::RoomKey(_) | AnyToDeviceEvent::RoomKey(_)
| AnyToDeviceEvent::RoomKeyRequest(_) | AnyToDeviceEvent::RoomKeyRequest(_)
| AnyToDeviceEvent::ForwardedRoomKey(_) | AnyToDeviceEvent::ForwardedRoomKey(_)
| AnyToDeviceEvent::RoomEncrypted(_) | AnyToDeviceEvent::RoomEncrypted(_) => None,
| AnyToDeviceEvent::Custom(_) => None,
AnyToDeviceEvent::KeyVerificationRequest(e) => { AnyToDeviceEvent::KeyVerificationRequest(e) => {
Some(RequestContent::from(&e.content).into()) Some(RequestContent::from(&e.content).into())
} }
@ -137,6 +136,7 @@ impl AnyEvent<'_> {
AnyToDeviceEvent::KeyVerificationDone(e) => { AnyToDeviceEvent::KeyVerificationDone(e) => {
Some(DoneContent::from(&e.content).into()) Some(DoneContent::from(&e.content).into())
} }
_ => None,
}, },
} }
} }
@ -178,30 +178,30 @@ impl TryFrom<&AnyMessageEvent> for FlowId {
| AnyMessageEvent::RoomEncrypted(_) | AnyMessageEvent::RoomEncrypted(_)
| AnyMessageEvent::RoomMessageFeedback(_) | AnyMessageEvent::RoomMessageFeedback(_)
| AnyMessageEvent::RoomRedaction(_) | AnyMessageEvent::RoomRedaction(_)
| AnyMessageEvent::Sticker(_) | AnyMessageEvent::Sticker(_) => Err(()),
| AnyMessageEvent::Custom(_) => Err(()),
AnyMessageEvent::KeyVerificationReady(e) => { AnyMessageEvent::KeyVerificationReady(e) => {
Ok(FlowId::from((&e.room_id, &e.content.relation.event_id))) Ok(FlowId::from((&e.room_id, &e.content.relates_to.event_id)))
} }
AnyMessageEvent::RoomMessage(e) => Ok(FlowId::from((&e.room_id, &e.event_id))), AnyMessageEvent::RoomMessage(e) => Ok(FlowId::from((&e.room_id, &e.event_id))),
AnyMessageEvent::KeyVerificationStart(e) => { AnyMessageEvent::KeyVerificationStart(e) => {
Ok(FlowId::from((&e.room_id, &e.content.relation.event_id))) Ok(FlowId::from((&e.room_id, &e.content.relates_to.event_id)))
} }
AnyMessageEvent::KeyVerificationCancel(e) => { AnyMessageEvent::KeyVerificationCancel(e) => {
Ok(FlowId::from((&e.room_id, &e.content.relation.event_id))) Ok(FlowId::from((&e.room_id, &e.content.relates_to.event_id)))
} }
AnyMessageEvent::KeyVerificationAccept(e) => { AnyMessageEvent::KeyVerificationAccept(e) => {
Ok(FlowId::from((&e.room_id, &e.content.relation.event_id))) Ok(FlowId::from((&e.room_id, &e.content.relates_to.event_id)))
} }
AnyMessageEvent::KeyVerificationKey(e) => { AnyMessageEvent::KeyVerificationKey(e) => {
Ok(FlowId::from((&e.room_id, &e.content.relation.event_id))) Ok(FlowId::from((&e.room_id, &e.content.relates_to.event_id)))
} }
AnyMessageEvent::KeyVerificationMac(e) => { AnyMessageEvent::KeyVerificationMac(e) => {
Ok(FlowId::from((&e.room_id, &e.content.relation.event_id))) Ok(FlowId::from((&e.room_id, &e.content.relates_to.event_id)))
} }
AnyMessageEvent::KeyVerificationDone(e) => { AnyMessageEvent::KeyVerificationDone(e) => {
Ok(FlowId::from((&e.room_id, &e.content.relation.event_id))) Ok(FlowId::from((&e.room_id, &e.content.relates_to.event_id)))
} }
_ => Err(()),
} }
} }
} }
@ -215,8 +215,7 @@ impl TryFrom<&AnyToDeviceEvent> for FlowId {
| AnyToDeviceEvent::RoomKey(_) | AnyToDeviceEvent::RoomKey(_)
| AnyToDeviceEvent::RoomKeyRequest(_) | AnyToDeviceEvent::RoomKeyRequest(_)
| AnyToDeviceEvent::ForwardedRoomKey(_) | AnyToDeviceEvent::ForwardedRoomKey(_)
| AnyToDeviceEvent::RoomEncrypted(_) | AnyToDeviceEvent::RoomEncrypted(_) => Err(()),
| AnyToDeviceEvent::Custom(_) => Err(()),
AnyToDeviceEvent::KeyVerificationRequest(e) => { AnyToDeviceEvent::KeyVerificationRequest(e) => {
Ok(FlowId::from(e.content.transaction_id.to_owned())) Ok(FlowId::from(e.content.transaction_id.to_owned()))
} }
@ -241,6 +240,7 @@ impl TryFrom<&AnyToDeviceEvent> for FlowId {
AnyToDeviceEvent::KeyVerificationDone(e) => { AnyToDeviceEvent::KeyVerificationDone(e) => {
Ok(FlowId::from(e.content.transaction_id.to_owned())) Ok(FlowId::from(e.content.transaction_id.to_owned()))
} }
_ => Err(()),
} }
} }
} }
@ -397,7 +397,7 @@ impl<'a> StartContent<'a> {
pub fn flow_id(&self) -> &str { pub fn flow_id(&self) -> &str {
match self { match self {
Self::ToDevice(c) => &c.transaction_id, Self::ToDevice(c) => &c.transaction_id,
Self::Room(c) => c.relation.event_id.as_str(), Self::Room(c) => c.relates_to.event_id.as_str(),
} }
} }
@ -449,7 +449,7 @@ impl<'a> DoneContent<'a> {
pub fn flow_id(&self) -> &str { pub fn flow_id(&self) -> &str {
match self { match self {
Self::ToDevice(c) => &c.transaction_id, Self::ToDevice(c) => &c.transaction_id,
Self::Room(c) => c.relation.event_id.as_str(), Self::Room(c) => c.relates_to.event_id.as_str(),
} }
} }
} }
@ -464,7 +464,7 @@ impl AcceptContent<'_> {
pub fn flow_id(&self) -> &str { pub fn flow_id(&self) -> &str {
match self { match self {
Self::ToDevice(c) => &c.transaction_id, Self::ToDevice(c) => &c.transaction_id,
Self::Room(c) => c.relation.event_id.as_str(), Self::Room(c) => c.relates_to.event_id.as_str(),
} }
} }
@ -495,7 +495,7 @@ impl KeyContent<'_> {
pub fn flow_id(&self) -> &str { pub fn flow_id(&self) -> &str {
match self { match self {
Self::ToDevice(c) => &c.transaction_id, Self::ToDevice(c) => &c.transaction_id,
Self::Room(c) => c.relation.event_id.as_str(), Self::Room(c) => c.relates_to.event_id.as_str(),
} }
} }
@ -517,7 +517,7 @@ impl MacContent<'_> {
pub fn flow_id(&self) -> &str { pub fn flow_id(&self) -> &str {
match self { match self {
Self::ToDevice(c) => &c.transaction_id, Self::ToDevice(c) => &c.transaction_id,
Self::Room(c) => c.relation.event_id.as_str(), Self::Room(c) => c.relates_to.event_id.as_str(),
} }
} }
@ -582,7 +582,7 @@ impl OwnedStartContent {
pub fn flow_id(&self) -> FlowId { pub fn flow_id(&self) -> FlowId {
match self { match self {
Self::ToDevice(c) => FlowId::ToDevice(c.transaction_id.clone()), Self::ToDevice(c) => FlowId::ToDevice(c.transaction_id.clone()),
Self::Room(r, c) => FlowId::InRoom(r.clone(), c.relation.event_id.clone()), Self::Room(r, c) => FlowId::InRoom(r.clone(), c.relates_to.event_id.clone()),
} }
} }
@ -688,72 +688,65 @@ impl From<RoomMessageRequest> for OutgoingContent {
#[cfg(test)] #[cfg(test)]
impl TryFrom<ToDeviceRequest> for OutgoingContent { impl TryFrom<ToDeviceRequest> for OutgoingContent {
type Error = (); type Error = String;
fn try_from(value: ToDeviceRequest) -> Result<Self, Self::Error> { fn try_from(request: ToDeviceRequest) -> Result<Self, Self::Error> {
use ruma::events::EventType; use ruma::events::EventType;
use serde_json::Value; use serde_json::Value;
let json: Value = serde_json::from_str( let json: Value = serde_json::from_str(
value request
.messages .messages
.values() .values()
.next() .next()
.and_then(|m| m.values().next().map(|j| j.get())) .and_then(|m| m.values().next())
.ok_or(())?, .map(|c| c.json().get())
.ok_or_else(|| "Content is missing from the request".to_owned())?,
) )
.map_err(|_| ())?; .map_err(|e| e.to_string())?;
match value.event_type { let content = match request.event_type {
EventType::KeyVerificationRequest => { EventType::KeyVerificationStart => AnyToDeviceEventContent::KeyVerificationStart(
Ok(AnyToDeviceEventContent::KeyVerificationRequest( serde_json::from_value(json).map_err(|e| e.to_string())?,
serde_json::from_value(json).map_err(|_| ())?, ),
) EventType::KeyVerificationKey => AnyToDeviceEventContent::KeyVerificationKey(
.into()) serde_json::from_value(json).map_err(|e| e.to_string())?,
} ),
EventType::KeyVerificationReady => Ok(AnyToDeviceEventContent::KeyVerificationReady( EventType::KeyVerificationAccept => AnyToDeviceEventContent::KeyVerificationAccept(
serde_json::from_value(json).map_err(|_| ())?, serde_json::from_value(json).map_err(|e| e.to_string())?,
) ),
.into()), EventType::KeyVerificationMac => AnyToDeviceEventContent::KeyVerificationMac(
EventType::KeyVerificationDone => Ok(AnyToDeviceEventContent::KeyVerificationDone( serde_json::from_value(json).map_err(|e| e.to_string())?,
serde_json::from_value(json).map_err(|_| ())?, ),
) EventType::KeyVerificationCancel => AnyToDeviceEventContent::KeyVerificationCancel(
.into()), serde_json::from_value(json).map_err(|e| e.to_string())?,
EventType::KeyVerificationStart => Ok(AnyToDeviceEventContent::KeyVerificationStart( ),
serde_json::from_value(json).map_err(|_| ())?, EventType::KeyVerificationReady => AnyToDeviceEventContent::KeyVerificationReady(
) serde_json::from_value(json).map_err(|e| e.to_string())?,
.into()), ),
EventType::KeyVerificationKey => Ok(AnyToDeviceEventContent::KeyVerificationKey( EventType::KeyVerificationDone => AnyToDeviceEventContent::KeyVerificationDone(
serde_json::from_value(json).map_err(|_| ())?, serde_json::from_value(json).map_err(|e| e.to_string())?,
) ),
.into()), EventType::KeyVerificationRequest => AnyToDeviceEventContent::KeyVerificationRequest(
EventType::KeyVerificationAccept => Ok(AnyToDeviceEventContent::KeyVerificationAccept( serde_json::from_value(json).map_err(|e| e.to_string())?,
serde_json::from_value(json).map_err(|_| ())?, ),
) e => return Err(format!("Unsupported event type {}", e)),
.into()), };
EventType::KeyVerificationMac => Ok(AnyToDeviceEventContent::KeyVerificationMac(
serde_json::from_value(json).map_err(|_| ())?, Ok(content.into())
)
.into()),
EventType::KeyVerificationCancel => Ok(AnyToDeviceEventContent::KeyVerificationCancel(
serde_json::from_value(json).map_err(|_| ())?,
)
.into()),
_ => Err(()),
}
} }
} }
#[cfg(test)] #[cfg(test)]
impl TryFrom<OutgoingRequest> for OutgoingContent { impl TryFrom<OutgoingRequest> for OutgoingContent {
type Error = (); type Error = String;
fn try_from(value: OutgoingRequest) -> Result<Self, ()> { fn try_from(value: OutgoingRequest) -> Result<Self, Self::Error> {
match value.request() { match value.request() {
crate::OutgoingRequests::KeysUpload(_) => Err(()), crate::OutgoingRequests::KeysUpload(_) => Err("Invalid request type".to_owned()),
crate::OutgoingRequests::KeysQuery(_) => Err(()), crate::OutgoingRequests::KeysQuery(_) => Err("Invalid request type".to_owned()),
crate::OutgoingRequests::ToDeviceRequest(r) => Self::try_from(r.clone()), crate::OutgoingRequests::ToDeviceRequest(r) => Self::try_from(r.clone()),
crate::OutgoingRequests::SignatureUpload(_) => Err(()), crate::OutgoingRequests::SignatureUpload(_) => Err("Invalid request type".to_owned()),
crate::OutgoingRequests::RoomMessage(r) => Ok(Self::from(r.clone())), crate::OutgoingRequests::RoomMessage(r) => Ok(Self::from(r.clone())),
} }
} }

View File

@ -525,10 +525,9 @@ impl IdentitiesBeingVerified {
#[cfg(test)] #[cfg(test)]
pub(crate) mod test { pub(crate) mod test {
use ruma::{ use ruma::{
events::{AnyToDeviceEvent, AnyToDeviceEventContent, EventType, ToDeviceEvent}, events::{AnyToDeviceEvent, AnyToDeviceEventContent, ToDeviceEvent},
UserId, UserId,
}; };
use serde_json::Value;
use super::event_enums::OutgoingContent; use super::event_enums::OutgoingContent;
use crate::{ use crate::{
@ -540,7 +539,7 @@ pub(crate) mod test {
sender: &UserId, sender: &UserId,
request: &OutgoingVerificationRequest, request: &OutgoingVerificationRequest,
) -> AnyToDeviceEvent { ) -> AnyToDeviceEvent {
let content = get_content_from_request(request); let content = request.to_owned().into();
wrap_any_to_device_content(sender, content) wrap_any_to_device_content(sender, content)
} }
@ -589,36 +588,4 @@ pub(crate) mod test {
_ => unreachable!(), _ => unreachable!(),
} }
} }
pub(crate) fn get_content_from_request(
request: &OutgoingVerificationRequest,
) -> OutgoingContent {
let request =
if let OutgoingVerificationRequest::ToDevice(r) = request { r } else { unreachable!() };
let json: Value = serde_json::from_str(
request.messages.values().next().unwrap().values().next().unwrap().get(),
)
.unwrap();
match request.event_type {
EventType::KeyVerificationStart => {
AnyToDeviceEventContent::KeyVerificationStart(serde_json::from_value(json).unwrap())
}
EventType::KeyVerificationKey => {
AnyToDeviceEventContent::KeyVerificationKey(serde_json::from_value(json).unwrap())
}
EventType::KeyVerificationAccept => AnyToDeviceEventContent::KeyVerificationAccept(
serde_json::from_value(json).unwrap(),
),
EventType::KeyVerificationMac => {
AnyToDeviceEventContent::KeyVerificationMac(serde_json::from_value(json).unwrap())
}
EventType::KeyVerificationCancel => AnyToDeviceEventContent::KeyVerificationCancel(
serde_json::from_value(json).unwrap(),
),
_ => unreachable!(),
}
.into()
}
} }

View File

@ -19,7 +19,6 @@ use std::sync::{Arc, Mutex};
use matrix_qrcode::QrVerificationData; use matrix_qrcode::QrVerificationData;
use matrix_sdk_common::uuid::Uuid; use matrix_sdk_common::uuid::Uuid;
use ruma::{ use ruma::{
api::client::r0::to_device::DeviceIdOrAllDevices,
events::{ events::{
key::verification::{ key::verification::{
cancel::CancelCode, cancel::CancelCode,
@ -31,6 +30,7 @@ use ruma::{
room::message::KeyVerificationRequestEventContent, room::message::KeyVerificationRequestEventContent,
AnyMessageEventContent, AnyToDeviceEventContent, AnyMessageEventContent, AnyToDeviceEventContent,
}, },
to_device::DeviceIdOrAllDevices,
DeviceId, DeviceIdBox, DeviceKeyAlgorithm, EventId, MilliSecondsSinceUnixEpoch, RoomId, UserId, DeviceId, DeviceIdBox, DeviceKeyAlgorithm, EventId, MilliSecondsSinceUnixEpoch, RoomId, UserId,
}; };
use tracing::{info, trace, warn}; use tracing::{info, trace, warn};
@ -51,9 +51,9 @@ use crate::{
}; };
const SUPPORTED_METHODS: &[VerificationMethod] = &[ const SUPPORTED_METHODS: &[VerificationMethod] = &[
VerificationMethod::MSasV1, VerificationMethod::SasV1,
VerificationMethod::MQrCodeShowV1, VerificationMethod::QrCodeShowV1,
VerificationMethod::MReciprocateV1, VerificationMethod::ReciprocateV1,
]; ];
/// An object controlling key verification requests. /// An object controlling key verification requests.
@ -780,8 +780,8 @@ impl RequestState<Ready> {
// If we didn't state that we support showing QR codes or if the other // If we didn't state that we support showing QR codes or if the other
// side doesn't support scanning QR codes bail early. // side doesn't support scanning QR codes bail early.
if !self.state.our_methods.contains(&VerificationMethod::MQrCodeShowV1) if !self.state.our_methods.contains(&VerificationMethod::QrCodeShowV1)
|| !self.state.their_methods.contains(&VerificationMethod::MQrScanShowV1) || !self.state.their_methods.contains(&VerificationMethod::QrCodeScanV1)
{ {
return Ok(None); return Ok(None);
} }
@ -942,7 +942,7 @@ impl RequestState<Ready> {
account: ReadOnlyAccount, account: ReadOnlyAccount,
private_identity: PrivateCrossSigningIdentity, private_identity: PrivateCrossSigningIdentity,
) -> Result<Option<(Sas, OutgoingContent)>, CryptoStoreError> { ) -> Result<Option<(Sas, OutgoingContent)>, CryptoStoreError> {
if !self.state.their_methods.contains(&VerificationMethod::MSasV1) { if !self.state.their_methods.contains(&VerificationMethod::SasV1) {
return Ok(None); return Ok(None);
} }

View File

@ -476,12 +476,12 @@ impl AcceptSettings {
fn apply(self, mut content: OwnedAcceptContent) -> OwnedAcceptContent { fn apply(self, mut content: OwnedAcceptContent) -> OwnedAcceptContent {
match &mut content { match &mut content {
OwnedAcceptContent::ToDevice(AcceptToDeviceEventContent { OwnedAcceptContent::ToDevice(AcceptToDeviceEventContent {
method: AcceptMethod::MSasV1(c), method: AcceptMethod::SasV1(c),
.. ..
}) })
| OwnedAcceptContent::Room( | OwnedAcceptContent::Room(
_, _,
AcceptEventContent { method: AcceptMethod::MSasV1(c), .. }, AcceptEventContent { method: AcceptMethod::SasV1(c), .. },
) => { ) => {
c.short_authentication_string.retain(|sas| self.allowed_methods.contains(sas)); c.short_authentication_string.retain(|sas| self.allowed_methods.contains(sas));
content content

View File

@ -102,7 +102,7 @@ impl TryFrom<AcceptV1Content> for AcceptedProtocols {
Err(CancelCode::UnknownMethod) Err(CancelCode::UnknownMethod)
} else { } else {
Ok(Self { Ok(Self {
method: VerificationMethod::MSasV1, method: VerificationMethod::SasV1,
hash: content.hash, hash: content.hash,
key_agreement_protocol: content.key_agreement_protocol, key_agreement_protocol: content.key_agreement_protocol,
message_auth_code: content.message_authentication_code, message_auth_code: content.message_authentication_code,
@ -149,7 +149,7 @@ impl TryFrom<&SasV1Content> for AcceptedProtocols {
} }
Ok(Self { Ok(Self {
method: VerificationMethod::MSasV1, method: VerificationMethod::SasV1,
hash: HashAlgorithm::Sha256, hash: HashAlgorithm::Sha256,
key_agreement_protocol: KeyAgreementProtocol::Curve25519HkdfSha256, key_agreement_protocol: KeyAgreementProtocol::Curve25519HkdfSha256,
message_auth_code: MessageAuthenticationCode::HkdfHmacSha256, message_auth_code: MessageAuthenticationCode::HkdfHmacSha256,
@ -163,7 +163,7 @@ impl TryFrom<&SasV1Content> for AcceptedProtocols {
impl Default for AcceptedProtocols { impl Default for AcceptedProtocols {
fn default() -> Self { fn default() -> Self {
AcceptedProtocols { AcceptedProtocols {
method: VerificationMethod::MSasV1, method: VerificationMethod::SasV1,
hash: HashAlgorithm::Sha256, hash: HashAlgorithm::Sha256,
key_agreement_protocol: KeyAgreementProtocol::Curve25519HkdfSha256, key_agreement_protocol: KeyAgreementProtocol::Curve25519HkdfSha256,
message_auth_code: MessageAuthenticationCode::HkdfHmacSha256, message_auth_code: MessageAuthenticationCode::HkdfHmacSha256,
@ -450,7 +450,7 @@ impl SasState<Created> {
) -> Result<SasState<Accepted>, SasState<Cancelled>> { ) -> Result<SasState<Accepted>, SasState<Cancelled>> {
self.check_event(sender, content.flow_id()).map_err(|c| self.clone().cancel(c))?; self.check_event(sender, content.flow_id()).map_err(|c| self.clone().cancel(c))?;
if let AcceptMethod::MSasV1(content) = content.method() { if let AcceptMethod::SasV1(content) = content.method() {
let accepted_protocols = let accepted_protocols =
AcceptedProtocols::try_from(content.clone()).map_err(|c| self.clone().cancel(c))?; AcceptedProtocols::try_from(content.clone()).map_err(|c| self.clone().cancel(c))?;
@ -560,7 +560,7 @@ impl SasState<Started> {
/// been started because of a /// been started because of a
/// m.key.verification.request -> m.key.verification.ready flow. /// m.key.verification.request -> m.key.verification.ready flow.
pub fn as_content(&self) -> OwnedAcceptContent { pub fn as_content(&self) -> OwnedAcceptContent {
let method = AcceptMethod::MSasV1( let method = AcceptMethod::SasV1(
AcceptV1ContentInit { AcceptV1ContentInit {
commitment: self.state.commitment.clone(), commitment: self.state.commitment.clone(),
hash: self.state.accepted_protocols.hash.clone(), hash: self.state.accepted_protocols.hash.clone(),
@ -684,10 +684,10 @@ impl SasState<Accepted> {
pub fn as_content(&self) -> OutgoingContent { pub fn as_content(&self) -> OutgoingContent {
match &*self.verification_flow_id { match &*self.verification_flow_id {
FlowId::ToDevice(s) => { FlowId::ToDevice(s) => {
AnyToDeviceEventContent::KeyVerificationKey(KeyToDeviceEventContent { AnyToDeviceEventContent::KeyVerificationKey(KeyToDeviceEventContent::new(
transaction_id: s.to_string(), s.to_string(),
key: self.inner.lock().unwrap().public_key(), self.inner.lock().unwrap().public_key(),
}) ))
.into() .into()
} }
FlowId::InRoom(r, e) => ( FlowId::InRoom(r, e) => (
@ -710,10 +710,10 @@ impl SasState<KeyReceived> {
pub fn as_content(&self) -> OutgoingContent { pub fn as_content(&self) -> OutgoingContent {
match &*self.verification_flow_id { match &*self.verification_flow_id {
FlowId::ToDevice(s) => { FlowId::ToDevice(s) => {
AnyToDeviceEventContent::KeyVerificationKey(KeyToDeviceEventContent { AnyToDeviceEventContent::KeyVerificationKey(KeyToDeviceEventContent::new(
transaction_id: s.to_string(), s.to_string(),
key: self.inner.lock().unwrap().public_key(), self.inner.lock().unwrap().public_key(),
}) ))
.into() .into()
} }
FlowId::InRoom(r, e) => ( FlowId::InRoom(r, e) => (
@ -1090,11 +1090,12 @@ mod test {
use ruma::{ use ruma::{
events::key::verification::{ events::key::verification::{
accept::{AcceptMethod, CustomContent}, accept::{AcceptMethod, AcceptToDeviceEventContent},
start::{CustomContent as CustomStartContent, StartMethod}, start::{StartMethod, StartToDeviceEventContent},
}, },
DeviceId, UserId, DeviceId, UserId,
}; };
use serde_json::json;
use super::{Accepted, Created, SasState, Started}; use super::{Accepted, Created, SasState, Started};
use crate::{ use crate::{
@ -1227,7 +1228,7 @@ mod test {
let mut method = content.method_mut(); let mut method = content.method_mut();
match &mut method { match &mut method {
AcceptMethod::MSasV1(ref mut c) => { AcceptMethod::SasV1(ref mut c) => {
c.commitment = "".to_string(); c.commitment = "".to_string();
} }
_ => panic!("Unknown accept event content"), _ => panic!("Unknown accept event content"),
@ -1266,7 +1267,7 @@ mod test {
let mut method = content.method_mut(); let mut method = content.method_mut();
match &mut method { match &mut method {
AcceptMethod::MSasV1(ref mut c) => { AcceptMethod::SasV1(ref mut c) => {
c.short_authentication_string = vec![]; c.short_authentication_string = vec![];
} }
_ => panic!("Unknown accept event content"), _ => panic!("Unknown accept event content"),
@ -1283,14 +1284,13 @@ mod test {
async fn sas_unknown_method() { async fn sas_unknown_method() {
let (alice, bob) = get_sas_pair().await; let (alice, bob) = get_sas_pair().await;
let mut content = bob.as_content(); let content = json!({
let method = content.method_mut(); "method": "m.sas.custom",
"method_data": "something",
*method = AcceptMethod::Custom(CustomContent { "transaction_id": "some_id",
method: "m.sas.custom".to_string(),
data: Default::default(),
}); });
let content: AcceptToDeviceEventContent = serde_json::from_value(content).unwrap();
let content = AcceptContent::from(&content); let content = AcceptContent::from(&content);
alice alice
@ -1331,22 +1331,22 @@ mod test {
) )
.expect_err("Didn't cancel on invalid MAC method"); .expect_err("Didn't cancel on invalid MAC method");
let mut start_content = alice_sas.as_content(); let content = json!({
let method = start_content.method_mut(); "method": "m.sas.custom",
"from_device": "DEVICEID",
*method = StartMethod::Custom(CustomStartContent { "method_data": "something",
method: "m.sas.custom".to_string(), "transaction_id": "some_id",
data: Default::default(),
}); });
let flow_id = start_content.flow_id(); let content: StartToDeviceEventContent = serde_json::from_value(content).unwrap();
let content = StartContent::from(&start_content); let content = StartContent::from(&content);
let flow_id = content.flow_id().to_owned();
SasState::<Started>::from_start_event( SasState::<Started>::from_start_event(
bob.clone(), bob.clone(),
alice_device, alice_device,
None, None,
flow_id, flow_id.into(),
&content, &content,
false, false,
) )

View File

@ -18,6 +18,6 @@ http = "0.2.3"
lazy_static = "1.4.0" lazy_static = "1.4.0"
matrix-sdk-common = { version = "0.2.0", path = "../matrix_sdk_common" } matrix-sdk-common = { version = "0.2.0", path = "../matrix_sdk_common" }
matrix-sdk-test-macros = { version = "0.1.0", path = "../matrix_sdk_test_macros" } matrix-sdk-test-macros = { version = "0.1.0", path = "../matrix_sdk_test_macros" }
ruma = { version = "0.1.2", features = ["client-api-c"] } ruma = { version = "0.1.2", features = ["client-api-c"], git = "https://github.com/ruma/ruma", rev = "d73ab8add" }
serde = "1.0.122" serde = "1.0.122"
serde_json = "1.0.61" serde_json = "1.0.61"