From 8bfb7a6e0c4f5d1deb9f7b414929faf3a986984b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Tue, 20 Apr 2021 13:29:52 +0200 Subject: [PATCH] crypto: Connect the room key requesting to the rust side --- .../internal/crypto/DefaultCryptoService.kt | 18 +++++++++- .../android/sdk/internal/crypto/OlmMachine.kt | 10 ++++++ rust-sdk/src/lib.rs | 2 +- rust-sdk/src/machine.rs | 35 ++++++++++++++++--- rust-sdk/src/olm.udl | 9 +++++ 5 files changed, 68 insertions(+), 6 deletions(-) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/DefaultCryptoService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/DefaultCryptoService.kt index 61c2dcf501..9bae0f9a48 100755 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/DefaultCryptoService.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/DefaultCryptoService.kt @@ -952,7 +952,23 @@ internal class DefaultCryptoService @Inject constructor( * @param event the event to decrypt again. */ override fun reRequestRoomKeyForEvent(event: Event) { - // TODO + cryptoCoroutineScope.launch(coroutineDispatchers.crypto) { + val requestPair = olmMachine!!.requestRoomKey(event) + + if (requestPair.cancellation != null) { + when (requestPair.cancellation) { + is Request.ToDevice -> { + sendToDevice(requestPair.cancellation) + } + } + } + + when(requestPair.keyRequest) { + is Request.ToDevice -> { + sendToDevice(requestPair.keyRequest) + } + } + } } /** diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/OlmMachine.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/OlmMachine.kt index bbe10ce068..0a96122eee 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/OlmMachine.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/OlmMachine.kt @@ -40,8 +40,10 @@ import org.matrix.android.sdk.internal.session.sync.model.DeviceOneTimeKeysCount import org.matrix.android.sdk.internal.session.sync.model.ToDeviceSyncResponse import timber.log.Timber import uniffi.olm.CryptoStoreErrorException +import uniffi.olm.DecryptionErrorException import uniffi.olm.Device import uniffi.olm.DeviceLists +import uniffi.olm.KeyRequestPair import uniffi.olm.Logger import uniffi.olm.OlmMachine as InnerMachine import uniffi.olm.ProgressListener as RustProgressListener @@ -370,6 +372,14 @@ internal class OlmMachine(user_id: String, device_id: String, path: File, device } } + @Throws(DecryptionErrorException::class) + suspend fun requestRoomKey(event: Event): KeyRequestPair = withContext(Dispatchers.IO) { + val adapter = MoshiProvider.providesMoshi().adapter(Event::class.java) + val serializedEvent = adapter.toJson(event) + + inner.requestRoomKey(serializedEvent, event.roomId!!) + } + /** * Export all of our room keys. * diff --git a/rust-sdk/src/lib.rs b/rust-sdk/src/lib.rs index 998182d793..99f01ad7f3 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::{OlmMachine, Sas}; +pub use machine::{OlmMachine, Sas, KeyRequestPair}; pub use responses::{DeviceLists, KeysImportResult, Request, RequestType}; pub trait ProgressListener { diff --git a/rust-sdk/src/machine.rs b/rust-sdk/src/machine.rs index 1344f1a0f8..7478000f44 100644 --- a/rust-sdk/src/machine.rs +++ b/rust-sdk/src/machine.rs @@ -47,6 +47,11 @@ pub struct Sas { pub request: Request, } +pub struct KeyRequestPair { + pub cancellation: Option, + pub key_request: Request, +} + impl OlmMachine { /// Create a new `OlmMachine` /// @@ -133,12 +138,13 @@ impl OlmMachine { /// [mark_request_as_sent()](#method.mark_request_as_sent) method. /// /// **Note**: This method call should be locked per call. - pub fn outgoing_requests(&self) -> Vec { - self.runtime - .block_on(self.inner.outgoing_requests()) + pub fn outgoing_requests(&self) -> Result, CryptoStoreError> { + Ok(self + .runtime + .block_on(self.inner.outgoing_requests())? .into_iter() .map(|r| r.into()) - .collect() + .collect()) } /// Mark a request that was sent to the server as sent. @@ -418,6 +424,27 @@ impl OlmMachine { }) } + pub fn request_room_key( + &self, + event: &str, + room_id: &str, + ) -> Result { + let event: SyncMessageEvent = serde_json::from_str(event)?; + let room_id = RoomId::try_from(room_id)?; + + let (cancel, request) = self + .runtime + .block_on(self.inner.request_room_key(&event, &room_id))?; + + let cancellation = cancel.map(|r| r.into()); + let key_request = request.into(); + + Ok(KeyRequestPair { + cancellation, + key_request, + }) + } + /// Export all of our room keys. /// /// # Arguments diff --git a/rust-sdk/src/olm.udl b/rust-sdk/src/olm.udl index 6a711dfb21..615ad8c8e7 100644 --- a/rust-sdk/src/olm.udl +++ b/rust-sdk/src/olm.udl @@ -70,6 +70,11 @@ dictionary Sas { Request request; }; +dictionary KeyRequestPair { + Request? cancellation; + Request key_request; +}; + [Enum] interface Request { ToDevice(string request_id, string event_type, string body); @@ -98,6 +103,7 @@ interface OlmMachine { string receive_sync_changes([ByRef] string events, DeviceLists device_changes, record key_counts); + [Throws=CryptoStoreError] sequence outgoing_requests(); [Throws=CryptoStoreError] void mark_request_as_sent( @@ -125,6 +131,9 @@ interface OlmMachine { [Throws=CryptoStoreError] Sas start_verification([ByRef] Device device); + [Throws=DecryptionError] + KeyRequestPair request_room_key([ByRef] string event, [ByRef] string room_id); + [Throws=CryptoStoreError] string export_keys([ByRef] string passphrase, i32 rounds); [Throws=KeyImportError]