From d83fc971ce3dc2635109184e29ce1b9374dd3fb9 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Mon, 10 Aug 2020 12:39:00 +0200 Subject: [PATCH] Update ruma --- matrix_sdk/src/client.rs | 81 ++++++++++--------- matrix_sdk/src/http_client.rs | 29 ++----- matrix_sdk/src/request_builder.rs | 22 ++--- matrix_sdk/src/sas.rs | 28 ++++++- matrix_sdk_base/src/client.rs | 12 +-- matrix_sdk_common/Cargo.toml | 2 +- matrix_sdk_common/src/lib.rs | 2 +- matrix_sdk_crypto/src/device.rs | 34 ++++---- matrix_sdk_crypto/src/lib.rs | 7 +- matrix_sdk_crypto/src/machine.rs | 63 ++++++++------- matrix_sdk_crypto/src/olm/account.rs | 37 +++++---- matrix_sdk_crypto/src/olm/session.rs | 5 +- matrix_sdk_crypto/src/store/sqlite.rs | 22 +++-- matrix_sdk_crypto/src/verification/machine.rs | 12 ++- matrix_sdk_crypto/src/verification/mod.rs | 6 +- .../src/verification/sas/helpers.rs | 29 +++---- matrix_sdk_crypto/src/verification/sas/mod.rs | 13 +-- 17 files changed, 205 insertions(+), 199 deletions(-) diff --git a/matrix_sdk/src/client.rs b/matrix_sdk/src/client.rs index 0298b1df..6c481318 100644 --- a/matrix_sdk/src/client.rs +++ b/matrix_sdk/src/client.rs @@ -24,6 +24,8 @@ use std::{ sync::Arc, }; +#[cfg(feature = "encryption")] +use matrix_sdk_common::api::r0::to_device::send_event_to_device::Request as ToDeviceRequest; use matrix_sdk_common::{ identifiers::ServerName, instant::{Duration, Instant}, @@ -31,7 +33,7 @@ use matrix_sdk_common::{ locks::RwLock, presence::PresenceState, uuid::Uuid, - FromHttpRequestError, FromHttpResponseError, Outgoing, + FromHttpResponseError, }; use futures_timer::Delay as sleep; @@ -272,10 +274,6 @@ impl SyncSettings { } } -#[cfg(feature = "encryption")] -use api::r0::keys::{claim_keys, get_keys, upload_keys, KeyAlgorithm}; -#[cfg(feature = "encryption")] -use api::r0::to_device::send_event_to_device; use api::r0::{ account::register, directory::{get_public_rooms, get_public_rooms_filtered}, @@ -284,7 +282,7 @@ use api::r0::{ invite_user::{self, InvitationRecipient}, join_room_by_id, join_room_by_id_or_alias, kick_user, leave_room, Invite3pid, }, - message::{create_message_event, get_message_events}, + message::{get_message_events, send_message_event}, read_marker::set_read_marker, receipt::create_receipt, room::create_room, @@ -292,6 +290,14 @@ use api::r0::{ sync::sync_events, typing::create_typing_event, }; +#[cfg(feature = "encryption")] +use matrix_sdk_common::{ + api::r0::{ + keys::{claim_keys, get_keys, upload_keys}, + to_device::send_event_to_device, + }, + identifiers::DeviceKeyAlgorithm, +}; impl Client { /// Creates a new client for making HTTP requests to the given homeserver. @@ -739,8 +745,6 @@ impl Client { server: Option<&str>, ) -> Result { let limit = limit.map(|n| UInt::try_from(n).ok()).flatten(); - let since = since.map(ToString::to_string); - let server = server.map(ToString::to_string); let request = get_public_rooms::Request { limit, @@ -775,13 +779,13 @@ impl Client { /// let mut builder = RoomListFilterBuilder::new(); /// builder /// .filter(Filter { generic_search_term, }) - /// .since(last_sync_token) + /// .since(&last_sync_token) /// .room_network(RoomNetwork::Matrix); /// /// client.public_rooms_filtered(builder).await; /// # }) /// ``` - pub async fn public_rooms_filtered>( + pub async fn public_rooms_filtered<'a, R: Into>>( &self, room_search: R, ) -> Result { @@ -830,7 +834,7 @@ impl Client { /// `MessagesRequestBuilder`s `from` field as a starting point. /// /// Sends a request to `/_matrix/client/r0/rooms/{room_id}/messages` and - /// returns a `get_message_events::IncomingResponse` that contains chunks + /// returns a `get_message_events::Response` that contains chunks /// of `RoomEvents`. /// /// # Arguments @@ -991,7 +995,7 @@ impl Client { room_id: &RoomId, content: MessageEventContent, txn_id: Option, - ) -> Result { + ) -> Result { #[allow(unused_mut)] let mut event_type = EventType::RoomMessage; #[allow(unused_mut)] @@ -1044,10 +1048,10 @@ impl Client { } } - let request = create_message_event::Request { - room_id: room_id.clone(), + let request = send_message_event::Request { + room_id: &room_id, event_type, - txn_id: txn_id.unwrap_or_else(Uuid::new_v4).to_string(), + txn_id: &txn_id.unwrap_or_else(Uuid::new_v4).to_string(), data: raw_content, }; @@ -1093,19 +1097,10 @@ impl Client { /// // returned /// # }) /// ``` - pub async fn send( - &self, - request: Request, - ) -> Result<::Incoming> + pub async fn send(&self, request: Request) -> Result where Request: Endpoint + Debug, - ::Incoming: - TryFrom>, Error = FromHttpRequestError>, - ::Incoming: TryFrom< - http::Response>, - Error = FromHttpResponseError<::ResponseError>, - >, - crate::Error: From::ResponseError>>, + crate::Error: From>, { self.http_client .send(request, self.base_client.session().clone()) @@ -1240,12 +1235,16 @@ impl Client { } } - for request in self.base_client.outgoing_to_device_requests().await { - let transaction_id = request.txn_id.clone(); + for req in self.base_client.outgoing_to_device_requests().await { + let request = ToDeviceRequest { + event_type: req.event_type, + txn_id: &req.txn_id, + messages: req.messages, + }; if self.send(request).await.is_ok() { self.base_client - .mark_to_device_request_as_sent(&transaction_id) + .mark_to_device_request_as_sent(&req.txn_id) .await; } } @@ -1292,7 +1291,7 @@ impl Client { #[instrument] async fn claim_one_time_keys( &self, - one_time_keys: BTreeMap, KeyAlgorithm>>, + one_time_keys: BTreeMap, DeviceKeyAlgorithm>>, ) -> Result { let request = claim_keys::Request { timeout: None, @@ -1326,7 +1325,13 @@ impl Client { .await .expect("Keys don't need to be uploaded"); - for request in requests.drain(..) { + for req in requests.drain(..) { + let request = ToDeviceRequest { + event_type: req.event_type, + txn_id: &req.txn_id, + messages: req.messages, + }; + let _response: send_event_to_device::Response = self.send(request).await?; } @@ -1444,14 +1449,14 @@ mod test { set_read_marker, Client, ClientConfig, Invite3pid, MessageEventContent, Session, SyncSettings, Url, }; - use crate::events::room::message::TextMessageEventContent; - - use crate::{ - identifiers::{event_id, room_id, user_id}, - RegistrationBuilder, RoomListFilterBuilder, - }; + use crate::{RegistrationBuilder, RoomListFilterBuilder}; use matrix_sdk_base::JsonStore; + use matrix_sdk_common::{ + events::room::message::TextMessageEventContent, + identifiers::{event_id, room_id, user_id}, + thirdparty, + }; use matrix_sdk_test::{test_json, EventBuilder, EventsJson}; use mockito::{mock, Matcher}; use tempfile::tempdir; @@ -1785,7 +1790,7 @@ mod test { &Invite3pid { id_server: "example.org".to_string(), id_access_token: "IdToken".to_string(), - medium: crate::api::r0::thirdparty::Medium::Email, + medium: thirdparty::Medium::Email, address: "address".to_string(), }, ) diff --git a/matrix_sdk/src/http_client.rs b/matrix_sdk/src/http_client.rs index 6660f631..3e2367a1 100644 --- a/matrix_sdk/src/http_client.rs +++ b/matrix_sdk/src/http_client.rs @@ -22,7 +22,7 @@ use reqwest::{Client, Response}; use tracing::trace; use url::Url; -use matrix_sdk_common::{locks::RwLock, FromHttpRequestError, FromHttpResponseError, Outgoing}; +use matrix_sdk_common::{locks::RwLock, FromHttpResponseError}; use crate::{Endpoint, Error, Result, Session}; @@ -33,20 +33,11 @@ pub(crate) struct HttpClient { } impl HttpClient { - async fn send_request( + async fn send_request( &self, request: Request, session: Arc>>, - ) -> Result - where - Request: Endpoint, - ::Incoming: - TryFrom>, Error = FromHttpRequestError>, - ::Incoming: TryFrom< - http::Response>, - Error = FromHttpResponseError<::ResponseError>, - >, - { + ) -> Result { let mut request = { let read_guard; let access_token = if Request::METADATA.requires_authentication { @@ -95,16 +86,10 @@ impl HttpClient { &self, request: Request, session: Arc>>, - ) -> Result<::Incoming> + ) -> Result where Request: Endpoint, - ::Incoming: - TryFrom>, Error = FromHttpRequestError>, - ::Incoming: TryFrom< - http::Response>, - Error = FromHttpResponseError<::ResponseError>, - >, - Error: From::ResponseError>>, + Error: From>, { let response = self.send_request(request, session).await?; @@ -112,8 +97,6 @@ impl HttpClient { let response = self.response_to_http_response(response).await?; - Ok(::Incoming::try_from( - response, - )?) + Ok(Request::IncomingResponse::try_from(response)?) } } diff --git a/matrix_sdk/src/request_builder.rs b/matrix_sdk/src/request_builder.rs index 16565816..5793f64c 100644 --- a/matrix_sdk/src/request_builder.rs +++ b/matrix_sdk/src/request_builder.rs @@ -381,22 +381,22 @@ impl Into for RegistrationBuilder { /// let mut builder = RoomListFilterBuilder::new(); /// builder /// .filter(Filter { generic_search_term, }) -/// .since(last_sync_token) +/// .since(&last_sync_token) /// .room_network(RoomNetwork::Matrix); /// /// client.public_rooms_filtered(builder).await.is_err(); /// # }) /// ``` #[derive(Clone, Debug, Default)] -pub struct RoomListFilterBuilder { - server: Option, +pub struct RoomListFilterBuilder<'a> { + server: Option<&'a str>, limit: Option, - since: Option, + since: Option<&'a str>, filter: Option, room_network: Option, } -impl RoomListFilterBuilder { +impl<'a> RoomListFilterBuilder<'a> { /// Create a `RoomListFilterBuilder` builder to make a `get_message_events::Request`. pub fn new() -> Self { Self::default() @@ -405,8 +405,8 @@ impl RoomListFilterBuilder { /// The server to fetch the public room lists from. /// /// `None` means the server this request is sent to. - pub fn server>(&mut self, server: S) -> &mut Self { - self.server = Some(server.into()); + pub fn server(&mut self, server: &'a str) -> &mut Self { + self.server = Some(server); self } @@ -417,8 +417,8 @@ impl RoomListFilterBuilder { } /// Pagination token from a previous request. - pub fn since>(&mut self, since: S) -> &mut Self { - self.since = Some(since.into()); + pub fn since(&mut self, since: &'a str) -> &mut Self { + self.since = Some(since); self } @@ -437,8 +437,8 @@ impl RoomListFilterBuilder { } } -impl Into for RoomListFilterBuilder { - fn into(self) -> get_public_rooms_filtered::Request { +impl<'a> Into> for RoomListFilterBuilder<'a> { + fn into(self) -> get_public_rooms_filtered::Request<'a> { get_public_rooms_filtered::Request { room_network: self.room_network.unwrap_or_default(), limit: self.limit.map(UInt::from), diff --git a/matrix_sdk/src/sas.rs b/matrix_sdk/src/sas.rs index 3cff3faa..6db56274 100644 --- a/matrix_sdk/src/sas.rs +++ b/matrix_sdk/src/sas.rs @@ -17,7 +17,9 @@ use std::sync::Arc; use url::Url; use matrix_sdk_base::{Device, Sas as BaseSas, Session}; -use matrix_sdk_common::locks::RwLock; +use matrix_sdk_common::{ + api::r0::to_device::send_event_to_device::Request as ToDeviceRequest, locks::RwLock, +}; use crate::{error::Result, http_client::HttpClient}; @@ -34,7 +36,13 @@ pub struct Sas { impl Sas { /// Accept the interactive verification flow. pub async fn accept(&self) -> Result<()> { - if let Some(request) = self.inner.accept() { + if let Some(req) = self.inner.accept() { + let request = ToDeviceRequest { + event_type: req.event_type, + txn_id: &req.txn_id, + messages: req.messages, + }; + self.http_client.send(request, self.session.clone()).await?; } Ok(()) @@ -42,7 +50,13 @@ impl Sas { /// Confirm that the short auth strings match on both sides. pub async fn confirm(&self) -> Result<()> { - if let Some(request) = self.inner.confirm().await? { + if let Some(req) = self.inner.confirm().await? { + let request = ToDeviceRequest { + event_type: req.event_type, + txn_id: &req.txn_id, + messages: req.messages, + }; + self.http_client.send(request, self.session.clone()).await?; } @@ -51,7 +65,13 @@ impl Sas { /// Cancel the interactive verification flow. pub async fn cancel(&self) -> Result<()> { - if let Some(request) = self.inner.cancel() { + if let Some(req) = self.inner.cancel() { + let request = ToDeviceRequest { + event_type: req.event_type, + txn_id: &req.txn_id, + messages: req.messages, + }; + self.http_client.send(request, self.session.clone()).await?; } Ok(()) diff --git a/matrix_sdk_base/src/client.rs b/matrix_sdk_base/src/client.rs index 0719e593..a8e09855 100644 --- a/matrix_sdk_base/src/client.rs +++ b/matrix_sdk_base/src/client.rs @@ -42,13 +42,13 @@ use matrix_sdk_common::{ use matrix_sdk_common::{ api::r0::keys::{ claim_keys::Response as KeysClaimResponse, get_keys::Response as KeysQueryResponse, - upload_keys::Response as KeysUploadResponse, DeviceKeys, KeyAlgorithm, + upload_keys::Response as KeysUploadResponse, DeviceKeys, }, - api::r0::to_device::send_event_to_device::Request as ToDeviceRequest, + api::r0::to_device::send_event_to_device::IncomingRequest as OwnedToDeviceRequest, events::room::{ encrypted::EncryptedEventContent, message::MessageEventContent as MsgEventContent, }, - identifiers::DeviceId, + identifiers::{DeviceId, DeviceKeyAlgorithm}, }; #[cfg(feature = "encryption")] use matrix_sdk_crypto::{CryptoStore, OlmError, OlmMachine, OneTimeKeys, Sas}; @@ -1274,7 +1274,7 @@ impl BaseClient { pub async fn get_missing_sessions( &self, users: impl Iterator, - ) -> Result, KeyAlgorithm>>> { + ) -> Result, DeviceKeyAlgorithm>>> { let mut olm = self.olm.lock().await; match &mut *olm { @@ -1286,7 +1286,7 @@ impl BaseClient { /// Get a to-device request that will share a group session for a room. #[cfg(feature = "encryption")] #[cfg_attr(docsrs, doc(cfg(feature = "encryption")))] - pub async fn share_group_session(&self, room_id: &RoomId) -> Result> { + pub async fn share_group_session(&self, room_id: &RoomId) -> Result> { let room = self.get_joined_room(room_id).await.expect("No room found"); let mut olm = self.olm.lock().await; @@ -1828,7 +1828,7 @@ impl BaseClient { /// Get the to-device requests that need to be sent out. #[cfg(feature = "encryption")] #[cfg_attr(docsrs, doc(cfg(feature = "encryption")))] - pub async fn outgoing_to_device_requests(&self) -> Vec { + pub async fn outgoing_to_device_requests(&self) -> Vec { self.olm .lock() .await diff --git a/matrix_sdk_common/Cargo.toml b/matrix_sdk_common/Cargo.toml index e4cc1714..71f5ac16 100644 --- a/matrix_sdk_common/Cargo.toml +++ b/matrix_sdk_common/Cargo.toml @@ -17,7 +17,7 @@ js_int = "0.1.8" [dependencies.ruma] version = "0.0.1" git = "https://github.com/ruma/ruma" -rev = "a589e92144e1af635ec3fd224265193e45dcdc95" +rev = "9cf552f36186eedff44ebe0c6a32d598315f5860" features = ["client-api"] [target.'cfg(not(target_arch = "wasm32"))'.dependencies] diff --git a/matrix_sdk_common/src/lib.rs b/matrix_sdk_common/src/lib.rs index 2e4898c7..fd8fff5f 100644 --- a/matrix_sdk_common/src/lib.rs +++ b/matrix_sdk_common/src/lib.rs @@ -6,7 +6,7 @@ pub use ruma::{ error::{FromHttpRequestError, FromHttpResponseError, IntoHttpError, ServerError}, Endpoint, EndpointError, Outgoing, }, - events, identifiers, presence, push, Raw, + events, identifiers, presence, push, thirdparty, Raw, }; pub use uuid; diff --git a/matrix_sdk_crypto/src/device.rs b/matrix_sdk_crypto/src/device.rs index cc459523..62a54bb8 100644 --- a/matrix_sdk_crypto/src/device.rs +++ b/matrix_sdk_crypto/src/device.rs @@ -23,9 +23,9 @@ use std::{ use atomic::Atomic; use matrix_sdk_common::{ - api::r0::keys::{AlgorithmAndDeviceId, DeviceKeys, KeyAlgorithm, SignedKey}, + api::r0::keys::{DeviceKeys, SignedKey}, events::Algorithm, - identifiers::{DeviceId, UserId}, + identifiers::{DeviceId, DeviceKeyAlgorithm, DeviceKeyId, UserId}, }; use serde_json::{json, Value}; @@ -40,8 +40,8 @@ pub struct Device { user_id: Arc, device_id: Arc>, algorithms: Arc>, - keys: Arc>, - signatures: Arc>>, + keys: Arc>, + signatures: Arc>>, display_name: Arc>, deleted: Arc, trust_state: Arc>, @@ -80,8 +80,8 @@ impl Device { display_name: Option, trust_state: TrustState, algorithms: Vec, - keys: BTreeMap, - signatures: BTreeMap>, + keys: BTreeMap, + signatures: BTreeMap>, ) -> Self { Device { user_id: Arc::new(user_id), @@ -111,20 +111,18 @@ impl Device { } /// Get the key of the given key algorithm belonging to this device. - pub fn get_key(&self, algorithm: KeyAlgorithm) -> Option<&String> { - self.keys.get(&AlgorithmAndDeviceId( - algorithm, - self.device_id.as_ref().clone(), - )) + pub fn get_key(&self, algorithm: DeviceKeyAlgorithm) -> Option<&String> { + self.keys + .get(&DeviceKeyId::from_parts(algorithm, &self.device_id)) } /// Get a map containing all the device keys. - pub fn keys(&self) -> &BTreeMap { + pub fn keys(&self) -> &BTreeMap { &self.keys } /// Get a map containing all the device signatures. - pub fn signatures(&self) -> &BTreeMap> { + pub fn signatures(&self) -> &BTreeMap> { &self.signatures } @@ -173,7 +171,7 @@ impl Device { fn is_signed_by_device(&self, json: &mut Value) -> Result<(), SignatureError> { let signing_key = self - .get_key(KeyAlgorithm::Ed25519) + .get_key(DeviceKeyAlgorithm::Ed25519) .ok_or(SignatureError::MissingSigningKey)?; verify_json(&self.user_id, &self.device_id.as_str(), signing_key, json) @@ -249,8 +247,8 @@ pub(crate) mod test { use crate::device::{Device, TrustState}; use matrix_sdk_common::{ - api::r0::keys::{DeviceKeys, KeyAlgorithm}, - identifiers::user_id, + api::r0::keys::DeviceKeys, + identifiers::{user_id, DeviceKeyAlgorithm}, }; fn device_keys() -> DeviceKeys { @@ -299,11 +297,11 @@ pub(crate) mod test { device.display_name().as_ref().unwrap() ); assert_eq!( - device.get_key(KeyAlgorithm::Curve25519).unwrap(), + device.get_key(DeviceKeyAlgorithm::Curve25519).unwrap(), "xfgbLIC5WAl1OIkpOzoxpCe8FsRDT6nch7NQsOb15nc" ); assert_eq!( - device.get_key(KeyAlgorithm::Ed25519).unwrap(), + device.get_key(DeviceKeyAlgorithm::Ed25519).unwrap(), "2/5LWJMow5zhJqakV88SIc7q/1pa8fmkfgAzx72w9G4" ); } diff --git a/matrix_sdk_crypto/src/lib.rs b/matrix_sdk_crypto/src/lib.rs index 6b2c3d2c..69e0b518 100644 --- a/matrix_sdk_crypto/src/lib.rs +++ b/matrix_sdk_crypto/src/lib.rs @@ -45,10 +45,7 @@ pub use store::{CryptoStore, CryptoStoreError}; pub use verification::Sas; use error::SignatureError; -use matrix_sdk_common::{ - api::r0::keys::{AlgorithmAndDeviceId, KeyAlgorithm}, - identifiers::UserId, -}; +use matrix_sdk_common::identifiers::{DeviceKeyAlgorithm, DeviceKeyId, UserId}; use olm_rs::utility::OlmUtility; use serde_json::Value; @@ -86,7 +83,7 @@ pub(crate) fn verify_json( json_object.insert("unsigned".to_string(), u); } - let key_id = AlgorithmAndDeviceId(KeyAlgorithm::Ed25519, key_id.into()); + let key_id = DeviceKeyId::from_parts(DeviceKeyAlgorithm::Ed25519, key_id.into()); let signatures = signatures.ok_or(SignatureError::NoSignatureFound)?; let signature_object = signatures diff --git a/matrix_sdk_crypto/src/machine.rs b/matrix_sdk_crypto/src/machine.rs index 3e8667d9..4d39a5f2 100644 --- a/matrix_sdk_crypto/src/machine.rs +++ b/matrix_sdk_crypto/src/machine.rs @@ -23,10 +23,11 @@ use std::{ }; use api::r0::{ - keys, - keys::{AlgorithmAndDeviceId, DeviceKeys, KeyAlgorithm, OneTimeKey}, + keys::{claim_keys, get_keys, upload_keys, DeviceKeys, OneTimeKey}, sync::sync_events::Response as SyncResponse, - to_device::{send_event_to_device::Request as ToDeviceRequest, DeviceIdOrAllDevices}, + to_device::{ + send_event_to_device::IncomingRequest as OwnedToDeviceRequest, DeviceIdOrAllDevices, + }, }; use matrix_sdk_common::{ api, @@ -37,7 +38,7 @@ use matrix_sdk_common::{ room_key_request::RoomKeyRequestEventContent, Algorithm, AnySyncRoomEvent, AnyToDeviceEvent, EventType, SyncMessageEvent, ToDeviceEvent, }, - identifiers::{DeviceId, RoomId, UserId}, + identifiers::{DeviceId, DeviceKeyAlgorithm, DeviceKeyId, RoomId, UserId}, locks::RwLock, uuid::Uuid, Raw, @@ -62,7 +63,7 @@ use super::{ /// A map from the algorithm and device id to a one-time key. /// /// These keys need to be periodically uploaded to the server. -pub type OneTimeKeys = BTreeMap; +pub type OneTimeKeys = BTreeMap; /// State machine implementation of the Olm/Megolm encryption protocol used for /// Matrix end to end encryption. @@ -226,7 +227,7 @@ impl OlmMachine { #[instrument] pub async fn receive_keys_upload_response( &mut self, - response: &keys::upload_keys::Response, + response: &upload_keys::Response, ) -> OlmResult<()> { if !self.account.shared() { debug!("Marking account as shared"); @@ -235,7 +236,7 @@ impl OlmMachine { let one_time_key_count = response .one_time_key_counts - .get(&keys::KeyAlgorithm::SignedCurve25519); + .get(&DeviceKeyAlgorithm::SignedCurve25519); let count: u64 = one_time_key_count.map_or(0, |c| (*c).into()); debug!( @@ -277,14 +278,14 @@ impl OlmMachine { pub async fn get_missing_sessions( &mut self, users: impl Iterator, - ) -> OlmResult, KeyAlgorithm>>> { + ) -> OlmResult, DeviceKeyAlgorithm>>> { let mut missing = BTreeMap::new(); for user_id in users { let user_devices = self.store.read().await.get_user_devices(user_id).await?; for device in user_devices.devices() { - let sender_key = if let Some(k) = device.get_key(KeyAlgorithm::Curve25519) { + let sender_key = if let Some(k) = device.get_key(DeviceKeyAlgorithm::Curve25519) { k } else { continue; @@ -304,8 +305,10 @@ impl OlmMachine { } let user_map = missing.get_mut(user_id).unwrap(); - let _ = - user_map.insert(device.device_id().into(), KeyAlgorithm::SignedCurve25519); + let _ = user_map.insert( + device.device_id().into(), + DeviceKeyAlgorithm::SignedCurve25519, + ); } } } @@ -321,7 +324,7 @@ impl OlmMachine { /// * `response` - The response containing the claimed one-time keys. pub async fn receive_keys_claim_response( &mut self, - response: &keys::claim_keys::Response, + response: &claim_keys::Response, ) -> OlmResult<()> { // TODO log the failures here @@ -472,7 +475,7 @@ impl OlmMachine { /// performed. pub async fn receive_keys_query_response( &mut self, - response: &keys::get_keys::Response, + response: &get_keys::Response, ) -> OlmResult> { let changed_devices = self .handle_devices_from_key_query(&response.device_keys) @@ -654,13 +657,13 @@ impl OlmMachine { .ok_or_else(|| EventError::MissingField("recipient".to_string()))?; let recipient: UserId = serde_json::from_value(recipient)?; - let recipient_keys: BTreeMap = serde_json::from_value( + let recipient_keys: BTreeMap = serde_json::from_value( decrypted_json .get("recipient_keys") .cloned() .ok_or_else(|| EventError::MissingField("recipient_keys".to_string()))?, )?; - let keys: BTreeMap = serde_json::from_value( + let keys: BTreeMap = serde_json::from_value( decrypted_json .get("keys") .cloned() @@ -673,14 +676,14 @@ impl OlmMachine { if self.account.identity_keys().ed25519() != recipient_keys - .get(&KeyAlgorithm::Ed25519) + .get(&DeviceKeyAlgorithm::Ed25519) .ok_or(EventError::MissingSigningKey)? { return Err(EventError::MissmatchedKeys.into()); } let signing_key = keys - .get(&KeyAlgorithm::Ed25519) + .get(&DeviceKeyAlgorithm::Ed25519) .ok_or(EventError::MissingSigningKey)?; Ok(( @@ -872,7 +875,7 @@ impl OlmMachine { event_type: EventType, content: Value, ) -> OlmResult { - let sender_key = if let Some(k) = recipient_device.get_key(KeyAlgorithm::Curve25519) { + let sender_key = if let Some(k) = recipient_device.get_key(DeviceKeyAlgorithm::Curve25519) { k } else { warn!( @@ -943,7 +946,7 @@ impl OlmMachine { &mut self, room_id: &RoomId, users: I, - ) -> OlmResult> + ) -> OlmResult> where I: IntoIterator, { @@ -1008,7 +1011,7 @@ impl OlmMachine { ); } - requests.push(ToDeviceRequest { + requests.push(OwnedToDeviceRequest { event_type: EventType::RoomEncrypted, txn_id: Uuid::new_v4().to_string(), messages, @@ -1076,7 +1079,7 @@ impl OlmMachine { } /// Get the to-device requests that need to be sent out. - pub fn outgoing_to_device_requests(&self) -> Vec { + pub fn outgoing_to_device_requests(&self) -> Vec { self.verification_machine.outgoing_to_device_requests() } @@ -1106,7 +1109,7 @@ impl OlmMachine { let one_time_key_count = response .device_one_time_keys_count - .get(&keys::KeyAlgorithm::SignedCurve25519); + .get(&DeviceKeyAlgorithm::SignedCurve25519); let count: u64 = one_time_key_count.map_or(0, |c| (*c).into()); self.update_key_count(count); @@ -1285,7 +1288,7 @@ mod test { }; use matrix_sdk_common::{ - api::r0::{keys, to_device::send_event_to_device::Request as ToDeviceRequest}, + api::r0::{keys, to_device::send_event_to_device::IncomingRequest as OwnedToDeviceRequest}, events::{ room::{ encrypted::EncryptedEventContent, @@ -1294,7 +1297,7 @@ mod test { AnySyncMessageEvent, AnySyncRoomEvent, AnyToDeviceEvent, EventType, SyncMessageEvent, ToDeviceEvent, Unsigned, }, - identifiers::{event_id, room_id, user_id, DeviceId, UserId}, + identifiers::{event_id, room_id, user_id, DeviceId, DeviceKeyAlgorithm, UserId}, Raw, }; use matrix_sdk_test::test_json; @@ -1328,7 +1331,7 @@ mod test { keys::get_keys::Response::try_from(data).expect("Can't parse the keys upload response") } - fn to_device_requests_to_content(requests: Vec) -> EncryptedEventContent { + fn to_device_requests_to_content(requests: Vec) -> EncryptedEventContent { let to_device_request = &requests[0]; let content: Raw = serde_json::from_str( @@ -1462,7 +1465,7 @@ mod test { response .one_time_key_counts - .remove(&keys::KeyAlgorithm::SignedCurve25519) + .remove(&DeviceKeyAlgorithm::SignedCurve25519) .unwrap(); assert!(machine.should_upload_keys().await); @@ -1474,7 +1477,7 @@ mod test { response .one_time_key_counts - .insert(keys::KeyAlgorithm::SignedCurve25519, uint!(10)); + .insert(DeviceKeyAlgorithm::SignedCurve25519, uint!(10)); machine .receive_keys_upload_response(&response) .await @@ -1483,7 +1486,7 @@ mod test { response .one_time_key_counts - .insert(keys::KeyAlgorithm::SignedCurve25519, uint!(50)); + .insert(DeviceKeyAlgorithm::SignedCurve25519, uint!(50)); machine .receive_keys_upload_response(&response) .await @@ -1508,7 +1511,7 @@ mod test { response .one_time_key_counts - .insert(keys::KeyAlgorithm::SignedCurve25519, uint!(50)); + .insert(DeviceKeyAlgorithm::SignedCurve25519, uint!(50)); machine .receive_keys_upload_response(&response) .await @@ -1615,7 +1618,7 @@ mod test { let mut response = keys_upload_response(); response.one_time_key_counts.insert( - keys::KeyAlgorithm::SignedCurve25519, + DeviceKeyAlgorithm::SignedCurve25519, (one_time_keys.unwrap().len() as u64).try_into().unwrap(), ); diff --git a/matrix_sdk_crypto/src/olm/account.rs b/matrix_sdk_crypto/src/olm/account.rs index 6fde117c..a8cd18ee 100644 --- a/matrix_sdk_crypto/src/olm/account.rs +++ b/matrix_sdk_crypto/src/olm/account.rs @@ -23,9 +23,9 @@ use std::{ }; use matrix_sdk_common::{ - api::r0::keys::{AlgorithmAndDeviceId, DeviceKeys, KeyAlgorithm, OneTimeKey, SignedKey}, + api::r0::keys::{DeviceKeys, OneTimeKey, SignedKey}, events::Algorithm, - identifiers::{DeviceId, RoomId, UserId}, + identifiers::{DeviceId, DeviceKeyAlgorithm, DeviceKeyId, RoomId, UserId}, instant::Instant, locks::Mutex, }; @@ -204,7 +204,7 @@ impl Account { ) -> Result< ( Option, - Option>, + Option>, ), (), > { @@ -286,11 +286,11 @@ impl Account { let mut keys = BTreeMap::new(); keys.insert( - AlgorithmAndDeviceId(KeyAlgorithm::Curve25519, (*self.device_id).clone()), + DeviceKeyId::from_parts(DeviceKeyAlgorithm::Curve25519, &self.device_id), identity_keys.curve25519().to_owned(), ); keys.insert( - AlgorithmAndDeviceId(KeyAlgorithm::Ed25519, (*self.device_id).clone()), + DeviceKeyId::from_parts(DeviceKeyAlgorithm::Ed25519, &self.device_id), identity_keys.ed25519().to_owned(), ); @@ -305,7 +305,7 @@ impl Account { let mut signature = BTreeMap::new(); signature.insert( - AlgorithmAndDeviceId(KeyAlgorithm::Ed25519, (*self.device_id).clone()), + DeviceKeyId::from_parts(DeviceKeyAlgorithm::Ed25519, &self.device_id), self.sign_json(&device_keys).await, ); signatures.insert((*self.user_id).clone(), signature); @@ -345,7 +345,7 @@ impl Account { /// If no one-time keys need to be uploaded returns an empty error. pub(crate) async fn signed_one_time_keys( &self, - ) -> Result, ()> { + ) -> Result, ()> { let _ = self.generate_one_time_keys().await?; let one_time_keys = self.one_time_keys().await; @@ -361,7 +361,7 @@ impl Account { let mut signature_map = BTreeMap::new(); signature_map.insert( - AlgorithmAndDeviceId(KeyAlgorithm::Ed25519, (*self.device_id).clone()), + DeviceKeyId::from_parts(DeviceKeyAlgorithm::Ed25519, &self.device_id), signature, ); @@ -374,7 +374,10 @@ impl Account { }; one_time_key_map.insert( - AlgorithmAndDeviceId(KeyAlgorithm::SignedCurve25519, key_id.as_str().into()), + DeviceKeyId::from_parts( + DeviceKeyAlgorithm::SignedCurve25519, + key_id.as_str().into(), + ), OneTimeKey::SignedKey(signed_key), ); } @@ -432,7 +435,7 @@ impl Account { pub(crate) async fn create_outbound_session( &self, device: Device, - key_map: &BTreeMap, + key_map: &BTreeMap, ) -> Result { let one_time_key = key_map.values().next().ok_or_else(|| { SessionCreationError::OneTimeKeyMissing( @@ -459,12 +462,14 @@ impl Account { ) })?; - let curve_key = device.get_key(KeyAlgorithm::Curve25519).ok_or_else(|| { - SessionCreationError::DeviceMissingCurveKey( - device.user_id().to_owned(), - device.device_id().into(), - ) - })?; + let curve_key = device + .get_key(DeviceKeyAlgorithm::Curve25519) + .ok_or_else(|| { + SessionCreationError::DeviceMissingCurveKey( + device.user_id().to_owned(), + device.device_id().into(), + ) + })?; self.create_outbound_session_helper(curve_key, &one_time_key) .await diff --git a/matrix_sdk_crypto/src/olm/session.rs b/matrix_sdk_crypto/src/olm/session.rs index a1ca78b9..07a3339a 100644 --- a/matrix_sdk_crypto/src/olm/session.rs +++ b/matrix_sdk_crypto/src/olm/session.rs @@ -15,12 +15,11 @@ use std::{collections::BTreeMap, fmt, sync::Arc}; use matrix_sdk_common::{ - api::r0::keys::KeyAlgorithm, events::{ room::encrypted::{CiphertextInfo, EncryptedEventContent, OlmV1Curve25519AesSha2Content}, EventType, }, - identifiers::{DeviceId, UserId}, + identifiers::{DeviceId, DeviceKeyAlgorithm, UserId}, instant::Instant, locks::Mutex, }; @@ -109,7 +108,7 @@ impl Session { content: Value, ) -> OlmResult { let recipient_signing_key = recipient_device - .get_key(KeyAlgorithm::Ed25519) + .get_key(DeviceKeyAlgorithm::Ed25519) .ok_or(EventError::MissingSigningKey)?; let payload = json!({ diff --git a/matrix_sdk_crypto/src/store/sqlite.rs b/matrix_sdk_crypto/src/store/sqlite.rs index c773b002..b0cbdd0f 100644 --- a/matrix_sdk_crypto/src/store/sqlite.rs +++ b/matrix_sdk_crypto/src/store/sqlite.rs @@ -22,9 +22,8 @@ use std::{ use async_trait::async_trait; use matrix_sdk_common::{ - api::r0::keys::{AlgorithmAndDeviceId, KeyAlgorithm}, events::Algorithm, - identifiers::{DeviceId, RoomId, UserId}, + identifiers::{DeviceId, DeviceKeyAlgorithm, DeviceKeyId, RoomId, UserId}, instant::{Duration, Instant}, locks::Mutex, }; @@ -497,14 +496,14 @@ impl SqliteStore { .fetch_all(&mut *connection) .await?; - let keys: BTreeMap = key_rows + let keys: BTreeMap = key_rows .into_iter() .filter_map(|row| { - let algorithm = KeyAlgorithm::try_from(&*row.0).ok()?; + let algorithm = row.0.parse::().ok()?; let key = row.1; Some(( - AlgorithmAndDeviceId(algorithm, device_id.as_str().into()), + DeviceKeyId::from_parts(algorithm, device_id.as_str().into()), key, )) }) @@ -518,8 +517,7 @@ impl SqliteStore { .fetch_all(&mut *connection) .await?; - let mut signatures: BTreeMap> = - BTreeMap::new(); + let mut signatures: BTreeMap> = BTreeMap::new(); for row in signature_rows { let user_id = if let Ok(u) = UserId::try_from(&*row.0) { @@ -528,7 +526,7 @@ impl SqliteStore { continue; }; - let key_algorithm = if let Ok(k) = KeyAlgorithm::try_from(&*row.1) { + let key_algorithm = if let Ok(k) = row.1.parse::() { k } else { continue; @@ -542,7 +540,7 @@ impl SqliteStore { let user_map = signatures.get_mut(&user_id).unwrap(); user_map.insert( - AlgorithmAndDeviceId(key_algorithm, device_id.as_str().into()), + DeviceKeyId::from_parts(key_algorithm, device_id.as_str().into()), signature.to_owned(), ); } @@ -610,7 +608,7 @@ impl SqliteStore { .await?; } - for (key_algorithm, key) in device.keys() { + for (key_id, key) in device.keys() { query( "INSERT OR IGNORE INTO device_keys ( device_id, algorithm, key @@ -618,7 +616,7 @@ impl SqliteStore { ", ) .bind(device_row_id) - .bind(key_algorithm.0.to_string()) + .bind(key_id.algorithm().to_string()) .bind(key) .execute(&mut *connection) .await?; @@ -634,7 +632,7 @@ impl SqliteStore { ) .bind(device_row_id) .bind(user_id.as_str()) - .bind(key_id.0.to_string()) + .bind(key_id.algorithm().to_string()) .bind(signature) .execute(&mut *connection) .await?; diff --git a/matrix_sdk_crypto/src/verification/machine.rs b/matrix_sdk_crypto/src/verification/machine.rs index a19060cf..7ef9e745 100644 --- a/matrix_sdk_crypto/src/verification/machine.rs +++ b/matrix_sdk_crypto/src/verification/machine.rs @@ -19,7 +19,7 @@ use dashmap::DashMap; use tracing::{trace, warn}; use matrix_sdk_common::{ - api::r0::to_device::send_event_to_device::Request as ToDeviceRequest, + api::r0::to_device::send_event_to_device::IncomingRequest as OwnedToDeviceRequest, events::{AnyToDeviceEvent, AnyToDeviceEventContent}, identifiers::{DeviceId, UserId}, locks::RwLock, @@ -33,7 +33,7 @@ pub struct VerificationMachine { account: Account, store: Arc>>, verifications: Arc>, - outgoing_to_device_messages: Arc>, + outgoing_to_device_messages: Arc>, } impl VerificationMachine { @@ -73,11 +73,15 @@ impl VerificationMachine { self.outgoing_to_device_messages.remove(uuid); } - pub fn outgoing_to_device_requests(&self) -> Vec { + pub fn outgoing_to_device_requests(&self) -> Vec { #[allow(clippy::map_clone)] self.outgoing_to_device_messages .iter() - .map(|r| r.clone()) + .map(|r| OwnedToDeviceRequest { + event_type: r.event_type.clone(), + txn_id: r.txn_id.clone(), + messages: r.messages.clone(), + }) .collect() } diff --git a/matrix_sdk_crypto/src/verification/mod.rs b/matrix_sdk_crypto/src/verification/mod.rs index e42e5a50..2f2f23a2 100644 --- a/matrix_sdk_crypto/src/verification/mod.rs +++ b/matrix_sdk_crypto/src/verification/mod.rs @@ -25,7 +25,7 @@ pub(crate) mod test { use serde_json::Value; use matrix_sdk_common::{ - api::r0::to_device::send_event_to_device::Request as ToDeviceRequest, + api::r0::to_device::send_event_to_device::IncomingRequest as OwnedToDeviceRequest, events::{AnyToDeviceEvent, AnyToDeviceEventContent, EventType, ToDeviceEvent}, identifiers::UserId, }; @@ -64,7 +64,9 @@ pub(crate) mod test { } } - pub(crate) fn get_content_from_request(request: &ToDeviceRequest) -> AnyToDeviceEventContent { + pub(crate) fn get_content_from_request( + request: &OwnedToDeviceRequest, + ) -> AnyToDeviceEventContent { let json: Value = serde_json::from_str( request .messages diff --git a/matrix_sdk_crypto/src/verification/sas/helpers.rs b/matrix_sdk_crypto/src/verification/sas/helpers.rs index 9c6e36fc..1715db9b 100644 --- a/matrix_sdk_crypto/src/verification/sas/helpers.rs +++ b/matrix_sdk_crypto/src/verification/sas/helpers.rs @@ -5,15 +5,14 @@ use tracing::trace; use olm_rs::sas::OlmSas; use matrix_sdk_common::{ - api::r0::{ - keys::{AlgorithmAndDeviceId, KeyAlgorithm}, - to_device::{send_event_to_device::Request as ToDeviceRequest, DeviceIdOrAllDevices}, + api::r0::to_device::{ + send_event_to_device::IncomingRequest as OwnedToDeviceRequest, DeviceIdOrAllDevices, }, events::{ key::verification::{cancel::CancelCode, mac::MacEventContent}, AnyToDeviceEventContent, EventType, ToDeviceEvent, }, - identifiers::{DeviceId, UserId}, + identifiers::{DeviceId, DeviceKeyAlgorithm, DeviceKeyId, UserId}, uuid::Uuid, }; @@ -161,20 +160,10 @@ pub fn receive_mac_event( } for (key_id, key_mac) in &event.content.mac { - let split: Vec<&str> = key_id.splitn(2, ':').collect(); - - if split.len() != 2 { - continue; - } - - let algorithm: KeyAlgorithm = if let Ok(a) = split[0].try_into() { - a - } else { - continue; + let device_key_id: DeviceKeyId = match key_id.as_str().try_into() { + Ok(id) => id, + Err(_) => continue, }; - let id = split[1]; - - let device_key_id = AlgorithmAndDeviceId(algorithm, id.into()); if let Some(key) = ids.other_device.keys().get(&device_key_id) { if key_mac @@ -231,7 +220,7 @@ fn extra_mac_info_send(ids: &SasIds, flow_id: &str) -> String { pub fn get_mac_content(sas: &OlmSas, ids: &SasIds, flow_id: &str) -> MacEventContent { let mut mac: BTreeMap = BTreeMap::new(); - let key_id = AlgorithmAndDeviceId(KeyAlgorithm::Ed25519, ids.account.device_id().into()); + let key_id = DeviceKeyId::from_parts(DeviceKeyAlgorithm::Ed25519, ids.account.device_id()); let key = ids.account.identity_keys().ed25519(); let info = extra_mac_info_send(ids, flow_id); @@ -423,7 +412,7 @@ pub fn content_to_request( recipient: &UserId, recipient_device: &DeviceId, content: AnyToDeviceEventContent, -) -> ToDeviceRequest { +) -> OwnedToDeviceRequest { let mut messages = BTreeMap::new(); let mut user_messages = BTreeMap::new(); @@ -442,7 +431,7 @@ pub fn content_to_request( _ => unreachable!(), }; - ToDeviceRequest { + OwnedToDeviceRequest { txn_id: Uuid::new_v4().to_string(), event_type, messages, diff --git a/matrix_sdk_crypto/src/verification/sas/mod.rs b/matrix_sdk_crypto/src/verification/sas/mod.rs index eba8a800..1907d438 100644 --- a/matrix_sdk_crypto/src/verification/sas/mod.rs +++ b/matrix_sdk_crypto/src/verification/sas/mod.rs @@ -19,7 +19,7 @@ use std::sync::{Arc, Mutex}; use tracing::{info, trace, warn}; use matrix_sdk_common::{ - api::r0::to_device::send_event_to_device::Request as ToDeviceRequest, + api::r0::to_device::send_event_to_device::IncomingRequest as OwnedToDeviceRequest, events::{ key::verification::{ accept::AcceptEventContent, cancel::CancelCode, mac::MacEventContent, @@ -138,7 +138,7 @@ impl Sas { /// /// This does nothing if the verification was already accepted, otherwise it /// returns an `AcceptEventContent` that needs to be sent out. - pub fn accept(&self) -> Option { + pub fn accept(&self) -> Option { self.inner.lock().unwrap().accept().map(|c| { let content = AnyToDeviceEventContent::KeyVerificationAccept(c); self.content_to_request(content) @@ -152,7 +152,7 @@ impl Sas { /// Does nothing if we're not in a state where we can confirm the short auth /// string, otherwise returns a `MacEventContent` that needs to be sent to /// the server. - pub async fn confirm(&self) -> Result, CryptoStoreError> { + pub async fn confirm(&self) -> Result, CryptoStoreError> { let (content, done) = { let mut guard = self.inner.lock().unwrap(); let sas: InnerSas = (*guard).clone(); @@ -234,7 +234,7 @@ impl Sas { /// /// Returns None if the `Sas` object is already in a canceled state, /// otherwise it returns a request that needs to be sent out. - pub fn cancel(&self) -> Option { + pub fn cancel(&self) -> Option { let mut guard = self.inner.lock().unwrap(); let sas: InnerSas = (*guard).clone(); let (sas, content) = sas.cancel(); @@ -291,7 +291,10 @@ impl Sas { self.inner.lock().unwrap().verified_devices() } - pub(crate) fn content_to_request(&self, content: AnyToDeviceEventContent) -> ToDeviceRequest { + pub(crate) fn content_to_request( + &self, + content: AnyToDeviceEventContent, + ) -> OwnedToDeviceRequest { content_to_request(self.other_user_id(), self.other_device_id(), content) } }