From 5f848093b9a9a1c29bfb16e1659c7fe97e3d3f45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Fri, 5 Mar 2021 13:27:32 +0100 Subject: [PATCH] crypto: Send out to-device requests to share the room key --- .../internal/crypto/DefaultCryptoService.kt | 21 +++++++++++++++++- .../crypto/model/MXUsersDevicesMap.kt | 4 ++++ .../matrix/android/sdk/internal/newCrypto.kt | 4 ++++ rust-sdk/src/machine.rs | 22 ++++++++++++++++++- rust-sdk/src/olm.udl | 6 +++-- 5 files changed, 53 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 33b434ee15..1ed7e39db2 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 @@ -91,6 +91,7 @@ import org.matrix.android.sdk.internal.crypto.tasks.GetDeviceInfoTask import org.matrix.android.sdk.internal.crypto.tasks.GetDevicesTask import org.matrix.android.sdk.internal.crypto.tasks.NewUploadKeysTask import org.matrix.android.sdk.internal.crypto.tasks.SetDeviceNameTask +import org.matrix.android.sdk.internal.crypto.tasks.SendToDeviceTask import org.matrix.android.sdk.internal.crypto.tasks.UploadKeysTask import org.matrix.android.sdk.internal.crypto.tasks.ClaimOneTimeKeysForUsersDeviceTask import org.matrix.android.sdk.internal.crypto.verification.DefaultVerificationService @@ -174,6 +175,7 @@ internal class DefaultCryptoService @Inject constructor( // Tasks private val getDevicesTask: GetDevicesTask, private val oneTimeKeysForUsersDeviceTask: ClaimOneTimeKeysForUsersDeviceTask, + private val sendToDeviceTask: SendToDeviceTask, private val downloadKeysForUsersTask: DownloadKeysForUsersTask, private val getDeviceInfoTask: GetDeviceInfoTask, private val setDeviceNameTask: SetDeviceNameTask, @@ -962,9 +964,9 @@ internal class DefaultCryptoService @Inject constructor( private suspend fun preshareGroupSession(roomId: String, roomMembers: List) { val request = olmMachine!!.getMissingSessions(roomMembers) - roomId == "est" if (request != null) { + // This request can only be a keys claim request. when (request) { is Request.KeysClaim -> { val claimParams = ClaimOneTimeKeysForUsersDeviceTask.Params(request.oneTimeKeys) @@ -975,6 +977,23 @@ internal class DefaultCryptoService @Inject constructor( } } } + + for (toDeviceRequest in olmMachine!!.shareGroupSession(roomId, roomMembers)) { + // This request can only be a to-device request. + when (toDeviceRequest) { + is Request.ToDevice -> { + val adapter = MoshiProvider.providesMoshi().adapter>>(Map::class.java) + val body = adapter.fromJson(toDeviceRequest.body)!! + + val userMap = MXUsersDevicesMap() + userMap.join(body) + + val sendToDeviceParams = SendToDeviceTask.Params(toDeviceRequest.eventType, userMap) + sendToDeviceTask.execute(sendToDeviceParams) + olmMachine!!.markRequestAsSent(toDeviceRequest.requestId, RequestType.TO_DEVICE, "{}") + } + } + } } // private suspend fun encrypt(roomId: String, eventType: String, content: Content) { diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/model/MXUsersDevicesMap.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/model/MXUsersDevicesMap.kt index 9d7f2d9883..2c910c3bee 100755 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/model/MXUsersDevicesMap.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/model/MXUsersDevicesMap.kt @@ -115,6 +115,10 @@ class MXUsersDevicesMap { } } + fun join(other: Map>) { + map.putAll(other) + } + override fun toString(): String { return "MXUsersDevicesMap $map" } 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 21c463c08f..8a3be1c74d 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 @@ -87,6 +87,10 @@ internal class OlmMachine(user_id: String, device_id: String, path: File) { inner.outgoingRequests() } + suspend fun shareGroupSession(roomId: String, users: List): List = withContext(Dispatchers.IO) { + inner.shareGroupSession(roomId, users) + } + suspend fun getMissingSessions(users: List): Request? = withContext(Dispatchers.IO) { inner.getMissingSessions(users) } diff --git a/rust-sdk/src/machine.rs b/rust-sdk/src/machine.rs index 77c5cc064b..b4d3f475f1 100644 --- a/rust-sdk/src/machine.rs +++ b/rust-sdk/src/machine.rs @@ -26,7 +26,8 @@ use matrix_sdk_common::{ }; use matrix_sdk_crypto::{ - IncomingResponse, OlmMachine as InnerMachine, OutgoingRequest, ToDeviceRequest, + EncryptionSettings, IncomingResponse, OlmMachine as InnerMachine, OutgoingRequest, + ToDeviceRequest, }; use crate::error::{CryptoStoreError, DecryptionError, MachineCreationError}; @@ -358,6 +359,25 @@ impl OlmMachine { .block_on(self.inner.update_tracked_users(users.iter())); } + pub fn share_group_session(&self, room_id: &str, users: Vec) -> Vec { + let users: Vec = users + .into_iter() + .filter_map(|u| UserId::try_from(u).ok()) + .collect(); + + let room_id = RoomId::try_from(room_id).unwrap(); + let requests = self + .runtime + .block_on(self.inner.share_group_session( + &room_id, + users.iter(), + EncryptionSettings::default(), + )) + .unwrap(); + + requests.into_iter().map(|r| (&*r).into()).collect() + } + pub fn get_missing_sessions( &self, users: Vec, diff --git a/rust-sdk/src/olm.udl b/rust-sdk/src/olm.udl index d534a1cb36..c7faef71bc 100644 --- a/rust-sdk/src/olm.udl +++ b/rust-sdk/src/olm.udl @@ -84,11 +84,13 @@ interface OlmMachine { Device? get_device([ByRef] string user_id, [ByRef] string device_id); sequence get_user_devices([ByRef] string user_id); - sequence outgoing_requests(); - void update_tracked_users(sequence users); + sequence outgoing_requests(); + + void update_tracked_users(sequence users); [Throws=CryptoStoreError] Request? get_missing_sessions(sequence users); + sequence share_group_session([ByRef] string room_id, sequence users); [Throws=CryptoStoreError] void mark_request_as_sent(