From a9a7400fefcbfcf905b8c7a09e644812bd264cee Mon Sep 17 00:00:00 2001 From: ariskotsomitopoulos Date: Fri, 20 May 2022 17:46:31 +0300 Subject: [PATCH] Add MXCryptoConfig flag for key history sharing Add shared_history flag to sessionBackupData --- .../matrix/android/sdk/api/crypto/MXCryptoConfig.kt | 6 ++++++ .../crypto/keysbackup/DefaultKeysBackupService.kt | 5 +++-- .../crypto/keysbackup/model/rest/KeyBackupData.kt | 9 ++++++++- .../sdk/internal/crypto/store/db/RealmCryptoStore.kt | 3 +++ .../room/membership/DefaultMembershipService.kt | 11 +++++++++-- 5 files changed, 29 insertions(+), 5 deletions(-) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/crypto/MXCryptoConfig.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/crypto/MXCryptoConfig.kt index 9507ddda65..ea0c413546 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/crypto/MXCryptoConfig.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/crypto/MXCryptoConfig.kt @@ -38,4 +38,10 @@ data class MXCryptoConfig constructor( * You can limit request only to your sessions by turning this setting to `true` */ val limitRoomKeyRequestsToMyDevices: Boolean = false, + + /** + * Flag that indicates whether or not key history will be shared to invited + * users with respect to room visibility + */ + val shouldShareKeyHistory: Boolean = true, ) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/keysbackup/DefaultKeysBackupService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/keysbackup/DefaultKeysBackupService.kt index 08765f0c39..3514dae3f2 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/keysbackup/DefaultKeysBackupService.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/keysbackup/DefaultKeysBackupService.kt @@ -1429,7 +1429,8 @@ internal class DefaultKeysBackupService @Inject constructor( "sender_key" to sessionData.senderKey, "sender_claimed_keys" to sessionData.senderClaimedKeys, "forwarding_curve25519_key_chain" to (sessionData.forwardingCurve25519KeyChain.orEmpty()), - "session_key" to sessionData.sessionKey + "session_key" to sessionData.sessionKey, + "org.matrix.msc3061.shared_history" to sessionData.sharedHistory ) val json = MoshiProvider.providesMoshi() @@ -1456,7 +1457,7 @@ internal class DefaultKeysBackupService @Inject constructor( }, forwardedCount = olmInboundGroupSessionWrapper.sessionData.forwardingCurve25519KeyChain.orEmpty().size, isVerified = device?.isVerified == true, - + sharedHistory = olmInboundGroupSessionWrapper.sessionData.sharedHistory, sessionData = mapOf( "ciphertext" to encryptedSessionBackupData.mCipherText, "mac" to encryptedSessionBackupData.mMac, diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/keysbackup/model/rest/KeyBackupData.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/keysbackup/model/rest/KeyBackupData.kt index 5c3d0c12b0..675bf12248 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/keysbackup/model/rest/KeyBackupData.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/keysbackup/model/rest/KeyBackupData.kt @@ -50,5 +50,12 @@ internal data class KeyBackupData( * Algorithm-dependent data. */ @Json(name = "session_data") - val sessionData: JsonDict + val sessionData: JsonDict, + + /** + * Flag that indicates whether or not the current inboundSession will be shared to + * invited users to decrypt past messages + */ + @Json(name = "org.matrix.msc3061.shared_history") + val sharedHistory: Boolean = false ) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/RealmCryptoStore.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/RealmCryptoStore.kt index 74d547b9fb..15f95f01cb 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/RealmCryptoStore.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/RealmCryptoStore.kt @@ -26,6 +26,7 @@ import io.realm.RealmConfiguration import io.realm.Sort import io.realm.kotlin.createObject import io.realm.kotlin.where +import org.matrix.android.sdk.api.MatrixConfiguration import org.matrix.android.sdk.api.crypto.MXCRYPTO_ALGORITHM_MEGOLM import org.matrix.android.sdk.api.extensions.tryOrNull import org.matrix.android.sdk.api.logger.LoggerTag @@ -111,6 +112,7 @@ internal class RealmCryptoStore @Inject constructor( private val crossSigningKeysMapper: CrossSigningKeysMapper, @UserId private val userId: String, @DeviceId private val deviceId: String?, + private val matrixConfiguration: MatrixConfiguration, private val clock: Clock, ) : IMXCryptoStore { @@ -658,6 +660,7 @@ internal class RealmCryptoStore @Inject constructor( } override fun shouldShareHistory(roomId: String): Boolean { + if (!matrixConfiguration.cryptoConfig.shouldShareKeyHistory) return false return doWithRealm(realmConfiguration) { CryptoRoomEntity.getById(it, roomId)?.shouldShareHistory } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/membership/DefaultMembershipService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/membership/DefaultMembershipService.kt index 54c1a6b57e..331119530e 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/membership/DefaultMembershipService.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/membership/DefaultMembershipService.kt @@ -24,6 +24,7 @@ import dagger.assisted.AssistedFactory import dagger.assisted.AssistedInject import io.realm.Realm import io.realm.RealmQuery +import org.matrix.android.sdk.api.MatrixConfiguration import org.matrix.android.sdk.api.session.crypto.CryptoService import org.matrix.android.sdk.api.session.identity.ThreePid import org.matrix.android.sdk.api.session.room.members.MembershipService @@ -57,6 +58,7 @@ internal class DefaultMembershipService @AssistedInject constructor( private val cryptoService: CryptoService, @UserId private val userId: String, + private val matrixConfiguration: MatrixConfiguration, private val queryStringValueProcessor: QueryStringValueProcessor ) : MembershipService { @@ -144,13 +146,18 @@ internal class DefaultMembershipService @AssistedInject constructor( } override suspend fun invite(userId: String, reason: String?) { + sendShareHistoryKeysIfNeeded(userId) + val params = InviteTask.Params(roomId, userId, reason) + inviteTask.execute(params) + } + + private suspend fun sendShareHistoryKeysIfNeeded(userId: String) { + if (!matrixConfiguration.cryptoConfig.shouldShareKeyHistory) return // TODO not sure it's the right way to get the latest messages in a room val sessionInfo = Realm.getInstance(monarchy.realmConfiguration).use { ChunkEntity.findLatestSessionInfo(it, roomId) } cryptoService.sendSharedHistoryKeys(roomId, userId, sessionInfo) - val params = InviteTask.Params(roomId, userId, reason) - inviteTask.execute(params) } override suspend fun invite3pid(threePid: ThreePid) {