diff --git a/rust-sdk/Cargo.toml b/rust-sdk/Cargo.toml index 138c937c0c..d70e7e4418 100644 --- a/rust-sdk/Cargo.toml +++ b/rust-sdk/Cargo.toml @@ -24,11 +24,11 @@ features = ["lax_deserialize"] [dependencies.matrix-sdk-common] git = "https://github.com/matrix-org/matrix-rust-sdk/" -branch = "verification-qr" +rev = "0fb3dedd1cd3b0766fa7378754480d52d38e8ef2" [dependencies.matrix-sdk-crypto] git = "https://github.com/matrix-org/matrix-rust-sdk/" -branch = "verification-qr" +rev = "0fb3dedd1cd3b0766fa7378754480d52d38e8ef2" features = ["sled_cryptostore"] [dependencies.tokio] diff --git a/rust-sdk/src/lib.rs b/rust-sdk/src/lib.rs index e7ac395fd9..c1c37f3455 100644 --- a/rust-sdk/src/lib.rs +++ b/rust-sdk/src/lib.rs @@ -7,7 +7,7 @@ mod responses; pub use device::Device; pub use error::{CryptoStoreError, DecryptionError, KeyImportError, MachineCreationError}; pub use logger::{set_logger, Logger}; -pub use machine::{KeyRequestPair, OlmMachine, Sas}; +pub use machine::{KeyRequestPair, OlmMachine, Sas, VerificationRequest}; pub use responses::{ DeviceLists, KeysImportResult, OutgoingVerificationRequest, Request, RequestType, }; @@ -30,4 +30,35 @@ pub struct DecryptedEvent { pub forwarding_curve25519_chain: Vec, } +pub enum CancelCode { + User, + Timeout, + UnknownTransaction, + UnknownMethod, + UnexpectedMessage, + KeyMismatch, + UserMismatch, + InvalidMessage, + Accepted, +} + +impl From for CancelCode { + fn from(c: ruma::events::key::verification::cancel::CancelCode) -> Self { + use ruma::events::key::verification::cancel::CancelCode as RumaCancelCode; + + match c { + RumaCancelCode::User => Self::User, + RumaCancelCode::Timeout => Self::Timeout, + RumaCancelCode::UnknownTransaction => Self::UnknownTransaction, + RumaCancelCode::UnknownMethod => Self::UnknownMethod, + RumaCancelCode::UnexpectedMessage => Self::UnexpectedMessage, + RumaCancelCode::KeyMismatch => Self::KeyMismatch, + RumaCancelCode::UserMismatch => Self::UserMismatch, + RumaCancelCode::InvalidMessage => Self::InvalidMessage, + RumaCancelCode::Accepted => Self::Accepted, + RumaCancelCode::_Custom(_) => Self::User, + } + } +} + include!(concat!(env!("OUT_DIR"), "/olm.uniffi.rs")); diff --git a/rust-sdk/src/machine.rs b/rust-sdk/src/machine.rs index 028169bff1..4cd216977a 100644 --- a/rust-sdk/src/machine.rs +++ b/rust-sdk/src/machine.rs @@ -18,8 +18,8 @@ use ruma::{ IncomingResponse, }, events::{ - room::encrypted::EncryptedEventContent, AnyMessageEventContent, EventContent, - SyncMessageEvent, + key::verification::VerificationMethod, room::encrypted::EncryptedEventContent, + AnyMessageEventContent, EventContent, SyncMessageEvent, }, DeviceKeyAlgorithm, RoomId, UserId, }; @@ -30,14 +30,14 @@ use tokio::runtime::Runtime; use matrix_sdk_common::{deserialized_responses::AlgorithmInfo, uuid::Uuid}; use matrix_sdk_crypto::{ decrypt_key_export, encrypt_key_export, EncryptionSettings, OlmMachine as InnerMachine, - Sas as InnerSas, + Sas as InnerSas, VerificationRequest as InnerVerificationRequest, }; use crate::{ error::{CryptoStoreError, DecryptionError, MachineCreationError}, responses::{response_from_string, OutgoingVerificationRequest, OwnedResponse}, - DecryptedEvent, Device, DeviceLists, KeyImportError, KeysImportResult, ProgressListener, - Request, RequestType, + CancelCode, DecryptedEvent, Device, DeviceLists, KeyImportError, KeysImportResult, + ProgressListener, Request, RequestType, }; /// A high level state machine that handles E2EE for Matrix. @@ -70,6 +70,44 @@ impl From for Sas { } } +pub struct VerificationRequest { + pub other_user_id: String, + pub other_device_id: Option, + pub flow_id: String, + pub is_cancelled: bool, + pub is_done: bool, + pub is_ready: bool, + pub room_id: Option, + pub cancel_code: Option, + pub we_started: bool, + pub is_passive: bool, + pub their_methods: Option>, + pub our_methods: Option>, +} + +impl From for VerificationRequest { + fn from(v: InnerVerificationRequest) -> Self { + Self { + other_user_id: v.other_user().to_string(), + other_device_id: v.other_device_id().map(|d| d.to_string()), + flow_id: v.flow_id().as_str().to_owned(), + is_cancelled: v.is_cancelled(), + is_done: v.is_done(), + is_ready: v.is_ready(), + room_id: v.room_id().map(|r| r.to_string()), + cancel_code: v.cancel_code().map(|c| c.clone().into()), + we_started: v.we_started(), + is_passive: v.is_passive(), + their_methods: v + .their_supported_methods() + .map(|v| v.into_iter().map(|m| m.to_string()).collect()), + our_methods: v + .our_supported_methods() + .map(|v| v.into_iter().map(|m| m.to_string()).collect()), + } + } +} + /// A pair of outgoing room key requests, both of those are sendToDevice /// requests. pub struct KeyRequestPair { @@ -556,6 +594,51 @@ impl OlmMachine { Ok(()) } + pub fn get_verification_requests(&self, user_id: &str) -> Vec { + let user_id = if let Ok(user_id) = UserId::try_from(user_id) { + user_id + } else { + return vec![]; + }; + + self.inner + .get_verification_requests(&user_id) + .into_iter() + .map(|v| v.into()) + .collect() + } + + pub fn get_verification_request( + &self, + user_id: &str, + flow_id: &str, + ) -> Option { + let user_id = UserId::try_from(user_id).ok()?; + + self.inner + .get_verification_request(&user_id, flow_id) + .map(|v| v.into()) + } + + pub fn accept_verification_request( + &self, + user_id: &str, + flow_id: &str, + methods: Vec, + ) -> Option { + let user_id = UserId::try_from(user_id).ok()?; + let methods = methods + .into_iter() + .map(|m| VerificationMethod::from(m)) + .collect(); + + if let Some(verification) = self.inner.get_verification_request(&user_id, flow_id) { + verification.accept_with_methods(methods).map(|r| r.into()) + } else { + None + } + } + pub fn get_verification(&self, flow_id: &str) -> Option { todo!() // self.inner.get_verification(flow_id).map(|s| s.into()) diff --git a/rust-sdk/src/olm.udl b/rust-sdk/src/olm.udl index cdf5c2d7c4..01b0b84135 100644 --- a/rust-sdk/src/olm.udl +++ b/rust-sdk/src/olm.udl @@ -73,6 +73,34 @@ dictionary Sas { boolean timed_out; }; +dictionary VerificationRequest { + string other_user_id; + string? other_device_id; + string flow_id; + boolean is_cancelled; + boolean is_done; + boolean is_ready; + boolean we_started; + boolean is_passive; + string? room_id; + CancelCode? cancel_code; + sequence? their_methods; + sequence? our_methods; + +}; + +enum CancelCode { + "User", + "Timeout", + "UnknownTransaction", + "UnknownMethod", + "UnexpectedMessage", + "KeyMismatch", + "UserMismatch", + "InvalidMessage", + "Accepted", +}; + dictionary KeyRequestPair { Request? cancellation; Request key_request; @@ -136,8 +164,16 @@ interface OlmMachine { [Throws=CryptoStoreError] sequence share_room_key([ByRef] string room_id, sequence users); + sequence get_verification_requests([ByRef] string user_id); + VerificationRequest? get_verification_request([ByRef] string user_id, [ByRef] string flow_id); Sas? get_verification([ByRef] string flow_id); + OutgoingVerificationRequest? accept_verification_request( + [ByRef] string user_id, + [ByRef] string flow_id, + sequence methods + ); + [Throws=CryptoStoreError] Sas start_verification([ByRef] Device device); [Throws=CryptoStoreError]