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")
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<String, HashMap<String, Any>>>(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

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.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<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) {
inner.shareGroupSession(roomId, users)
}

View File

@ -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<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(
&self,
event: &str,

View File

@ -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<DOMString, string> identity_keys();