From c97e38479089074e740ebe36138ae40d1b29ead2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Fri, 5 Mar 2021 16:12:24 +0100 Subject: [PATCH] crypto: Hook up the event encryption to use the rust-sdk --- .../sdk/internal/crypto/DefaultCryptoService.kt | 9 ++++++--- .../org/matrix/android/sdk/internal/newCrypto.kt | 8 ++++++++ rust-sdk/src/machine.rs | 15 ++++++++++++++- rust-sdk/src/olm.udl | 1 + 4 files changed, 29 insertions(+), 4 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 1ed7e39db2..b6f72e6d27 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 @@ -697,7 +697,7 @@ internal class DefaultCryptoService @Inject constructor( Timber.v("## CRYPTO | encryptEventContent() starts") runCatching { preshareGroupSession(roomId, userIds) - val content = safeAlgorithm.encryptEventContent(eventContent, eventType, userIds) + val content = encrypt(roomId, eventType, eventContent) Timber.v("## CRYPTO | encryptEventContent() : succeeds after ${System.currentTimeMillis() - t0} ms") MXEncryptEventContentResult(content, EventType.ENCRYPTED) }.foldToCallback(callback) @@ -982,6 +982,8 @@ internal class DefaultCryptoService @Inject constructor( // This request can only be a to-device request. when (toDeviceRequest) { is Request.ToDevice -> { + // TODO this produces floats for the Olm type fields, which + // are integers originally. val adapter = MoshiProvider.providesMoshi().adapter>>(Map::class.java) val body = adapter.fromJson(toDeviceRequest.body)!! @@ -996,8 +998,9 @@ internal class DefaultCryptoService @Inject constructor( } } - // private suspend fun encrypt(roomId: String, eventType: String, content: Content) { - // } + private suspend fun encrypt(roomId: String, eventType: String, content: Content): Content { + return olmMachine!!.encrypt(roomId, eventType, content) + } private suspend fun sendOutgoingRequests() { // TODO these requests should be sent out in parallel diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/newCrypto.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/newCrypto.kt index 8a3be1c74d..c3f123ac6d 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/newCrypto.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/newCrypto.kt @@ -27,6 +27,7 @@ import org.matrix.android.sdk.internal.di.MoshiProvider import org.matrix.android.sdk.internal.session.sync.model.DeviceListResponse import org.matrix.android.sdk.internal.session.sync.model.DeviceOneTimeKeysCountSyncResponse import org.matrix.android.sdk.internal.session.sync.model.ToDeviceSyncResponse +import org.matrix.android.sdk.api.session.events.model.Content import timber.log.Timber import uniffi.olm.Device as InnerDevice import uniffi.olm.DeviceLists @@ -87,6 +88,13 @@ internal class OlmMachine(user_id: String, device_id: String, path: File) { inner.outgoingRequests() } + suspend fun encrypt(roomId: String, eventType: String, content: Content): Content = withContext(Dispatchers.IO) { + val adapter = MoshiProvider.providesMoshi().adapter(Map::class.java) + val contentString = adapter.toJson(content) + val encrypted = inner.encrypt(roomId, eventType, contentString) + adapter.fromJson(encrypted)!! + } + suspend fun shareGroupSession(roomId: String, users: List): List = withContext(Dispatchers.IO) { inner.shareGroupSession(roomId, users) } diff --git a/rust-sdk/src/machine.rs b/rust-sdk/src/machine.rs index b4d3f475f1..3c8a041111 100644 --- a/rust-sdk/src/machine.rs +++ b/rust-sdk/src/machine.rs @@ -4,7 +4,7 @@ use std::{ }; use http::Response; -use serde_json::json; +use serde_json::{json, value::RawValue}; use tokio::runtime::Runtime; use matrix_sdk_common::{ @@ -407,6 +407,19 @@ impl OlmMachine { })) } + pub fn encrypt(&self, room_id: &str, event_type: &str, content: &str) -> String { + let room_id = RoomId::try_from(room_id).unwrap(); + let content: Box = serde_json::from_str(content).unwrap(); + + let content = AnyMessageEventContent::from_parts(event_type, content).unwrap(); + let encrypted_content = self + .runtime + .block_on(self.inner.encrypt(&room_id, content)) + .unwrap(); + + serde_json::to_string(&encrypted_content).unwrap() + } + pub fn decrypt_room_event( &self, event: &str, diff --git a/rust-sdk/src/olm.udl b/rust-sdk/src/olm.udl index c7faef71bc..36bda6c96f 100644 --- a/rust-sdk/src/olm.udl +++ b/rust-sdk/src/olm.udl @@ -76,6 +76,7 @@ interface OlmMachine { [Throws=DecryptionError] DecryptedEvent decrypt_room_event([ByRef] string event, [ByRef] string room_id); + string encrypt([ByRef] string room_id, [ByRef] string event_type, [ByRef] string content); record identity_keys();