From fb23cbff97ba8bf534fc52d7d14a21f204d8d01e Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Tue, 7 Mar 2023 16:53:34 +0100 Subject: [PATCH] crypto-ffi: Use UniFFI proc-macros for more OlmMachine methods --- bindings/matrix-sdk-crypto-ffi/src/lib.rs | 12 ++- bindings/matrix-sdk-crypto-ffi/src/machine.rs | 99 ++++++++++--------- bindings/matrix-sdk-crypto-ffi/src/olm.udl | 67 ------------- 3 files changed, 61 insertions(+), 117 deletions(-) diff --git a/bindings/matrix-sdk-crypto-ffi/src/lib.rs b/bindings/matrix-sdk-crypto-ffi/src/lib.rs index 187e04d68..29ff39c60 100644 --- a/bindings/matrix-sdk-crypto-ffi/src/lib.rs +++ b/bindings/matrix-sdk-crypto-ffi/src/lib.rs @@ -717,10 +717,14 @@ mod uniffi_types { backup_recovery_key::{ BackupRecoveryKey, DecodeError, MegolmV1BackupKey, PassphraseInfo, PkDecryptionError, }, - error::CryptoStoreError, - machine::OlmMachine, - responses::Request, - BackupKeys, CrossSigningStatus, RoomKeyCounts, + error::{CryptoStoreError, DecryptionError, SecretImportError}, + machine::{KeyRequestPair, OlmMachine}, + responses::{BootstrapCrossSigningResult, DeviceLists, Request}, + verification::{ + RequestVerificationResult, StartSasResult, Verification, VerificationRequest, + }, + BackupKeys, CrossSigningKeyExport, CrossSigningStatus, DecryptedEvent, EncryptionSettings, + RoomKeyCounts, }; } diff --git a/bindings/matrix-sdk-crypto-ffi/src/machine.rs b/bindings/matrix-sdk-crypto-ffi/src/machine.rs index b80d906ed..814bf021a 100644 --- a/bindings/matrix-sdk-crypto-ffi/src/machine.rs +++ b/bindings/matrix-sdk-crypto-ffi/src/machine.rs @@ -450,7 +450,10 @@ impl OlmMachine { Ok(()) } +} +#[uniffi::export] +impl OlmMachine { /// Let the state machine know about E2EE related sync changes that we /// received from the server. /// @@ -468,12 +471,12 @@ impl OlmMachine { /// * `key_counts` - The map of uploaded one-time key types and counts. pub fn receive_sync_changes( &self, - events: &str, + events: String, device_changes: DeviceLists, key_counts: HashMap, unused_fallback_keys: Option>, ) -> Result { - let to_device: ToDevice = serde_json::from_str(events)?; + let to_device: ToDevice = serde_json::from_str(&events)?; let device_changes: RumaDeviceLists = device_changes.into(); let key_counts: BTreeMap = key_counts .into_iter() @@ -528,8 +531,8 @@ impl OlmMachine { /// /// A user can be marked for tracking using the /// [`OlmMachine::update_tracked_users()`] method. - pub fn is_user_tracked(&self, user_id: &str) -> Result { - let user_id = parse_user_id(user_id)?; + pub fn is_user_tracked(&self, user_id: String) -> Result { + let user_id = parse_user_id(&user_id)?; Ok(self.runtime.block_on(self.inner.tracked_users())?.contains(&user_id)) } @@ -581,7 +584,7 @@ impl OlmMachine { /// * `settings` - The settings that should be used for the room key. pub fn share_room_key( &self, - room_id: &str, + room_id: String, users: Vec, settings: EncryptionSettings, ) -> Result, CryptoStoreError> { @@ -633,16 +636,16 @@ impl OlmMachine { /// * `content` - The serialized content of the event. pub fn encrypt( &self, - room_id: &str, - event_type: &str, - content: &str, + room_id: String, + event_type: String, + content: String, ) -> Result { let room_id = RoomId::parse(room_id)?; - let content: Value = serde_json::from_str(content)?; + let content: Value = serde_json::from_str(&content)?; let encrypted_content = self .runtime - .block_on(self.inner.encrypt_room_event_raw(&room_id, content, event_type)) + .block_on(self.inner.encrypt_room_event_raw(&room_id, content, &event_type)) .expect("Encrypting an event produced an error"); Ok(serde_json::to_string(&encrypted_content)?) @@ -657,8 +660,8 @@ impl OlmMachine { /// * `room_id` - The unique id of the room where the event was sent to. pub fn decrypt_room_event( &self, - event: &str, - room_id: &str, + event: String, + room_id: String, handle_verification_events: bool, ) -> Result { // Element Android wants only the content and the type and will create a @@ -672,7 +675,7 @@ impl OlmMachine { content: &'a RawValue, } - let event: Raw<_> = serde_json::from_str(event)?; + let event: Raw<_> = serde_json::from_str(&event)?; let room_id = RoomId::parse(room_id)?; let decrypted = self.runtime.block_on(self.inner.decrypt_room_event(&event, &room_id))?; @@ -727,10 +730,10 @@ impl OlmMachine { /// * `room_id` - The id of the room the event was sent to. pub fn request_room_key( &self, - event: &str, - room_id: &str, + event: String, + room_id: String, ) -> Result { - let event: Raw<_> = serde_json::from_str(event)?; + let event: Raw<_> = serde_json::from_str(&event)?; let room_id = RoomId::parse(room_id)?; let (cancel, request) = @@ -753,17 +756,19 @@ impl OlmMachine { /// passphrase into an key. pub fn export_room_keys( &self, - passphrase: &str, + passphrase: String, rounds: i32, ) -> Result { let keys = self.runtime.block_on(self.inner.export_room_keys(|_| true))?; - let encrypted = encrypt_room_key_export(&keys, passphrase, rounds as u32) + let encrypted = encrypt_room_key_export(&keys, &passphrase, rounds as u32) .map_err(CryptoStoreError::Serialization)?; Ok(encrypted) } +} +impl OlmMachine { fn import_room_keys_helper( &self, keys: Vec, @@ -838,10 +843,13 @@ impl OlmMachine { self.import_room_keys_helper(keys, true, progress_listener) } +} +#[uniffi::export] +impl OlmMachine { /// Discard the currently active room key for the given room if there is /// one. - pub fn discard_room_key(&self, room_id: &str) -> Result<(), CryptoStoreError> { + pub fn discard_room_key(&self, room_id: String) -> Result<(), CryptoStoreError> { let room_id = RoomId::parse(room_id)?; self.runtime.block_on(self.inner.invalidate_group_session(&room_id))?; @@ -857,8 +865,8 @@ impl OlmMachine { /// **Note**: This has been deprecated. pub fn receive_unencrypted_verification_event( &self, - event: &str, - room_id: &str, + event: String, + room_id: String, ) -> Result<(), CryptoStoreError> { self.receive_verification_event(event, room_id) } @@ -869,11 +877,11 @@ impl OlmMachine { /// in rooms to the `OlmMachine`. The event should be in the decrypted form. pub fn receive_verification_event( &self, - event: &str, - room_id: &str, + event: String, + room_id: String, ) -> Result<(), CryptoStoreError> { let room_id = RoomId::parse(room_id)?; - let event: AnySyncMessageLikeEvent = serde_json::from_str(event)?; + let event: AnySyncMessageLikeEvent = serde_json::from_str(&event)?; let event = event.into_full_event(room_id); @@ -888,7 +896,7 @@ impl OlmMachine { /// /// * `user_id` - The ID of the user for which we would like to fetch the /// verification requests. - pub fn get_verification_requests(&self, user_id: &str) -> Vec> { + pub fn get_verification_requests(&self, user_id: String) -> Vec> { let Ok(user_id) = UserId::parse(user_id) else { return vec![]; }; @@ -913,8 +921,8 @@ impl OlmMachine { /// * `flow_id` - The ID that uniquely identifies the verification flow. pub fn get_verification_request( &self, - user_id: &str, - flow_id: &str, + user_id: String, + flow_id: String, ) -> Option> { let user_id = UserId::parse(user_id).ok()?; @@ -934,10 +942,10 @@ impl OlmMachine { /// support. pub fn verification_request_content( &self, - user_id: &str, + user_id: String, methods: Vec, ) -> Result, CryptoStoreError> { - let user_id = parse_user_id(user_id)?; + let user_id = parse_user_id(&user_id)?; let identity = self.runtime.block_on(self.inner.get_identity(&user_id, None))?; @@ -974,12 +982,12 @@ impl OlmMachine { /// [verification_request_content()]: #method.verification_request_content pub fn request_verification( &self, - user_id: &str, - room_id: &str, - event_id: &str, + user_id: String, + room_id: String, + event_id: String, methods: Vec, ) -> Result>, CryptoStoreError> { - let user_id = parse_user_id(user_id)?; + let user_id = parse_user_id(&user_id)?; let event_id = EventId::parse(event_id)?; let room_id = RoomId::parse(room_id)?; @@ -1016,17 +1024,18 @@ impl OlmMachine { /// supported in the `m.key.verification.request` event. pub fn request_verification_with_device( &self, - user_id: &str, - device_id: &str, + user_id: String, + device_id: String, methods: Vec, ) -> Result, CryptoStoreError> { - let user_id = parse_user_id(user_id)?; + let user_id = parse_user_id(&user_id)?; + let device_id = device_id.as_str().into(); let methods = methods.into_iter().map(VerificationMethod::from).collect(); Ok( if let Some(device) = - self.runtime.block_on(self.inner.get_device(&user_id, device_id.into(), None))? + self.runtime.block_on(self.inner.get_device(&user_id, device_id, None))? { let (verification, request) = self.runtime.block_on(device.request_verification_with_methods(methods)); @@ -1085,11 +1094,11 @@ impl OlmMachine { /// verification. /// /// * `flow_id` - The ID that uniquely identifies the verification flow. - pub fn get_verification(&self, user_id: &str, flow_id: &str) -> Option> { + pub fn get_verification(&self, user_id: String, flow_id: String) -> Option> { let user_id = UserId::parse(user_id).ok()?; self.inner - .get_verification(&user_id, flow_id) + .get_verification(&user_id, &flow_id) .map(|v| Verification { inner: v, runtime: self.runtime.handle().to_owned() }.into()) } @@ -1109,14 +1118,15 @@ impl OlmMachine { /// [request_verification_with_device()]: #method.request_verification_with_device pub fn start_sas_with_device( &self, - user_id: &str, - device_id: &str, + user_id: String, + device_id: String, ) -> Result, CryptoStoreError> { - let user_id = parse_user_id(user_id)?; + let user_id = parse_user_id(&user_id)?; + let device_id = device_id.as_str().into(); Ok( if let Some(device) = - self.runtime.block_on(self.inner.get_device(&user_id, device_id.into(), None))? + self.runtime.block_on(self.inner.get_device(&user_id, device_id, None))? { let (sas, request) = self.runtime.block_on(device.start_verification())?; @@ -1159,10 +1169,7 @@ impl OlmMachine { Ok(()) } -} -#[uniffi::export] -impl OlmMachine { /// Activate the given backup key to be used with the given backup version. /// /// **Warning**: The caller needs to make sure that the given `BackupKey` is diff --git a/bindings/matrix-sdk-crypto-ffi/src/olm.udl b/bindings/matrix-sdk-crypto-ffi/src/olm.udl index 5d16670ef..48d1d6bf2 100644 --- a/bindings/matrix-sdk-crypto-ffi/src/olm.udl +++ b/bindings/matrix-sdk-crypto-ffi/src/olm.udl @@ -354,11 +354,6 @@ interface OlmMachine { string? passphrase ); - [Throws=CryptoStoreError] - string receive_sync_changes([ByRef] string events, - DeviceLists device_changes, - record key_counts, - sequence? unused_fallback_keys); [Throws=CryptoStoreError] sequence outgoing_requests(); [Throws=CryptoStoreError] @@ -368,11 +363,6 @@ interface OlmMachine { [ByRef] string response ); - [Throws=DecryptionError] - DecryptedEvent decrypt_room_event([ByRef] string event, [ByRef] string room_id, boolean handle_verificaton_events); - [Throws=CryptoStoreError] - string encrypt([ByRef] string room_id, [ByRef] string event_type, [ByRef] string content); - [Throws=CryptoStoreError] UserIdentity? get_identity([ByRef] string user_id, u32 timeout); [Throws=SignatureError] @@ -386,56 +376,6 @@ interface OlmMachine { [Throws=CryptoStoreError] sequence get_user_devices([ByRef] string user_id, u32 timeout); - [Throws=CryptoStoreError] - boolean is_user_tracked([ByRef] string user_id); - [Throws=CryptoStoreError] - void update_tracked_users(sequence users); - [Throws=CryptoStoreError] - Request? get_missing_sessions(sequence users); - [Throws=CryptoStoreError] - sequence share_room_key( - [ByRef] string room_id, - sequence users, - EncryptionSettings settings - ); - - [Throws=CryptoStoreError] - void receive_unencrypted_verification_event([ByRef] string event, [ByRef] string room_id); - [Throws=CryptoStoreError] - void receive_verification_event([ByRef] string event, [ByRef] string room_id); - sequence get_verification_requests([ByRef] string user_id); - VerificationRequest? get_verification_request([ByRef] string user_id, [ByRef] string flow_id); - Verification? get_verification([ByRef] string user_id, [ByRef] string flow_id); - - [Throws=CryptoStoreError] - VerificationRequest? request_verification( - [ByRef] string user_id, - [ByRef] string room_id, - [ByRef] string event_id, - sequence methods - ); - [Throws=CryptoStoreError] - string? verification_request_content( - [ByRef] string user_id, - sequence methods - ); - [Throws=CryptoStoreError] - RequestVerificationResult? request_self_verification(sequence methods); - [Throws=CryptoStoreError] - RequestVerificationResult? request_verification_with_device( - [ByRef] string user_id, - [ByRef] string device_id, - sequence methods - ); - - [Throws=CryptoStoreError] - StartSasResult? start_sas_with_device([ByRef] string user_id, [ByRef] string device_id); - - [Throws=DecryptionError] - KeyRequestPair request_room_key([ByRef] string event, [ByRef] string room_id); - - [Throws=CryptoStoreError] - string export_room_keys([ByRef] string passphrase, i32 rounds); [Throws=KeyImportError] KeysImportResult import_room_keys( [ByRef] string keys, @@ -447,14 +387,7 @@ interface OlmMachine { [ByRef] string keys, ProgressListener progress_listener ); - [Throws=CryptoStoreError] - void discard_room_key([ByRef] string room_id); - [Throws=CryptoStoreError] - BootstrapCrossSigningResult bootstrap_cross_signing(); - CrossSigningKeyExport? export_cross_signing_keys(); - [Throws=SecretImportError] - void import_cross_signing_keys(CrossSigningKeyExport export); [Throws=CryptoStoreError] boolean is_identity_verified([ByRef] string user_id);