crypto: Hook up the event encryption to use the rust-sdk

This commit is contained in:
Damir Jelić 2021-03-05 16:12:24 +01:00
parent 5f848093b9
commit c97e384790
4 changed files with 29 additions and 4 deletions

View file

@ -697,7 +697,7 @@ internal class DefaultCryptoService @Inject constructor(
Timber.v("## CRYPTO | encryptEventContent() starts") Timber.v("## CRYPTO | encryptEventContent() starts")
runCatching { runCatching {
preshareGroupSession(roomId, userIds) 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") Timber.v("## CRYPTO | encryptEventContent() : succeeds after ${System.currentTimeMillis() - t0} ms")
MXEncryptEventContentResult(content, EventType.ENCRYPTED) MXEncryptEventContentResult(content, EventType.ENCRYPTED)
}.foldToCallback(callback) }.foldToCallback(callback)
@ -982,6 +982,8 @@ internal class DefaultCryptoService @Inject constructor(
// This request can only be a to-device request. // This request can only be a to-device request.
when (toDeviceRequest) { when (toDeviceRequest) {
is Request.ToDevice -> { is Request.ToDevice -> {
// TODO this produces floats for the Olm type fields, which
// are integers originally.
val adapter = MoshiProvider.providesMoshi().adapter<Map<String, HashMap<String, Any>>>(Map::class.java) val adapter = MoshiProvider.providesMoshi().adapter<Map<String, HashMap<String, Any>>>(Map::class.java)
val body = adapter.fromJson(toDeviceRequest.body)!! 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() { private suspend fun sendOutgoingRequests() {
// TODO these requests should be sent out in parallel // TODO these requests should be sent out in parallel

View file

@ -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.DeviceListResponse
import org.matrix.android.sdk.internal.session.sync.model.DeviceOneTimeKeysCountSyncResponse 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.internal.session.sync.model.ToDeviceSyncResponse
import org.matrix.android.sdk.api.session.events.model.Content
import timber.log.Timber import timber.log.Timber
import uniffi.olm.Device as InnerDevice import uniffi.olm.Device as InnerDevice
import uniffi.olm.DeviceLists import uniffi.olm.DeviceLists
@ -87,6 +88,13 @@ internal class OlmMachine(user_id: String, device_id: String, path: File) {
inner.outgoingRequests() inner.outgoingRequests()
} }
suspend fun encrypt(roomId: String, eventType: String, content: Content): Content = withContext(Dispatchers.IO) {
val adapter = MoshiProvider.providesMoshi().adapter<Content>(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<String>): List<Request> = withContext(Dispatchers.IO) { suspend fun shareGroupSession(roomId: String, users: List<String>): List<Request> = withContext(Dispatchers.IO) {
inner.shareGroupSession(roomId, users) inner.shareGroupSession(roomId, users)
} }

View file

@ -4,7 +4,7 @@ use std::{
}; };
use http::Response; use http::Response;
use serde_json::json; use serde_json::{json, value::RawValue};
use tokio::runtime::Runtime; use tokio::runtime::Runtime;
use matrix_sdk_common::{ 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<RawValue> = 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( pub fn decrypt_room_event(
&self, &self,
event: &str, event: &str,

View file

@ -76,6 +76,7 @@ interface OlmMachine {
[Throws=DecryptionError] [Throws=DecryptionError]
DecryptedEvent decrypt_room_event([ByRef] string event, [ByRef] string room_id); 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<DOMString, string> identity_keys(); record<DOMString, string> identity_keys();