crypto: Get rid of the roomEncryptorsStore

This commit is contained in:
Damir Jelić 2021-03-25 15:53:18 +01:00
parent 3812162f4f
commit dace959d69

View file

@ -59,7 +59,6 @@ import org.matrix.android.sdk.api.util.JsonDict
import org.matrix.android.sdk.internal.OlmMachine import org.matrix.android.sdk.internal.OlmMachine
import org.matrix.android.sdk.internal.setRustLogger import org.matrix.android.sdk.internal.setRustLogger
import org.matrix.android.sdk.internal.crypto.actions.MegolmSessionDataImporter import org.matrix.android.sdk.internal.crypto.actions.MegolmSessionDataImporter
import org.matrix.android.sdk.internal.crypto.actions.SetDeviceVerificationAction
import org.matrix.android.sdk.internal.crypto.algorithms.IMXEncrypting import org.matrix.android.sdk.internal.crypto.algorithms.IMXEncrypting
import org.matrix.android.sdk.internal.crypto.algorithms.IMXWithHeldExtension import org.matrix.android.sdk.internal.crypto.algorithms.IMXWithHeldExtension
import org.matrix.android.sdk.internal.crypto.algorithms.megolm.MXMegolmEncryptionFactory import org.matrix.android.sdk.internal.crypto.algorithms.megolm.MXMegolmEncryptionFactory
@ -137,8 +136,6 @@ internal class DefaultCryptoService @Inject constructor(
private val myDeviceInfoHolder: Lazy<MyDeviceInfoHolder>, private val myDeviceInfoHolder: Lazy<MyDeviceInfoHolder>,
// the crypto store // the crypto store
private val cryptoStore: IMXCryptoStore, private val cryptoStore: IMXCryptoStore,
// Room encryptors store
private val roomEncryptorsStore: RoomEncryptorsStore,
// Set of parameters used to configure/customize the end-to-end crypto. // Set of parameters used to configure/customize the end-to-end crypto.
private val mxCryptoConfig: MXCryptoConfig, private val mxCryptoConfig: MXCryptoConfig,
// Device list manager // Device list manager
@ -152,14 +149,10 @@ internal class DefaultCryptoService @Inject constructor(
private val crossSigningService: DefaultCrossSigningService, private val crossSigningService: DefaultCrossSigningService,
// Actions // Actions
private val setDeviceVerificationAction: SetDeviceVerificationAction,
private val megolmSessionDataImporter: MegolmSessionDataImporter, private val megolmSessionDataImporter: MegolmSessionDataImporter,
private val warnOnUnknownDevicesRepository: WarnOnUnknownDeviceRepository, private val warnOnUnknownDevicesRepository: WarnOnUnknownDeviceRepository,
// Repository
private val megolmEncryptionFactory: MXMegolmEncryptionFactory,
private val olmEncryptionFactory: MXOlmEncryptionFactory,
private val deleteDeviceTask: DeleteDeviceTask,
// Tasks // Tasks
private val deleteDeviceTask: DeleteDeviceTask,
private val getDevicesTask: GetDevicesTask, private val getDevicesTask: GetDevicesTask,
private val oneTimeKeysForUsersDeviceTask: ClaimOneTimeKeysForUsersDeviceTask, private val oneTimeKeysForUsersDeviceTask: ClaimOneTimeKeysForUsersDeviceTask,
private val sendToDeviceTask: SendToDeviceTask, private val sendToDeviceTask: SendToDeviceTask,
@ -501,7 +494,7 @@ internal class DefaultCryptoService @Inject constructor(
* @param deviceId the unique identifier for the device. * @param deviceId the unique identifier for the device.
*/ */
override fun setDeviceVerification(trustLevel: DeviceTrustLevel, userId: String, deviceId: String) { override fun setDeviceVerification(trustLevel: DeviceTrustLevel, userId: String, deviceId: String) {
setDeviceVerificationAction.handle(trustLevel, userId, deviceId) // TODO
} }
/** /**
@ -524,21 +517,12 @@ internal class DefaultCryptoService @Inject constructor(
return false return false
} }
val encryptingClass = MXCryptoAlgorithms.hasEncryptorClassForAlgorithm(algorithm) if (algorithm != MXCRYPTO_ALGORITHM_MEGOLM) {
if (!encryptingClass) {
Timber.e("## CRYPTO | setEncryptionInRoom() : Unable to encrypt room $roomId with $algorithm") Timber.e("## CRYPTO | setEncryptionInRoom() : Unable to encrypt room $roomId with $algorithm")
return false return false
} }
cryptoStore.storeRoomAlgorithm(roomId, algorithm!!) cryptoStore.storeRoomAlgorithm(roomId, algorithm)
val alg: IMXEncrypting = when (algorithm) {
MXCRYPTO_ALGORITHM_MEGOLM -> megolmEncryptionFactory.create(roomId)
else -> olmEncryptionFactory.create(roomId)
}
roomEncryptorsStore.put(roomId, alg)
// if encryption was not previously enabled in this room, we will have been // if encryption was not previously enabled in this room, we will have been
// ignoring new device events for these users so far. We may well have // ignoring new device events for these users so far. We may well have
@ -606,18 +590,10 @@ internal class DefaultCryptoService @Inject constructor(
callback: MatrixCallback<MXEncryptEventContentResult>) { callback: MatrixCallback<MXEncryptEventContentResult>) {
// moved to crypto scope to have uptodate values // moved to crypto scope to have uptodate values
cryptoCoroutineScope.launch(coroutineDispatchers.crypto) { cryptoCoroutineScope.launch(coroutineDispatchers.crypto) {
val userIds = getRoomUserIds(roomId) val algorithm = getEncryptionAlgorithm(roomId)
var alg = roomEncryptorsStore.get(roomId)
if (alg == null) { if (algorithm != null) {
val algorithm = getEncryptionAlgorithm(roomId) val userIds = getRoomUserIds(roomId)
if (algorithm != null) {
if (setEncryptionInRoom(roomId, algorithm, userIds)) {
alg = roomEncryptorsStore.get(roomId)
}
}
}
val safeAlgorithm = alg
if (safeAlgorithm != null) {
val t0 = System.currentTimeMillis() val t0 = System.currentTimeMillis()
Timber.v("## CRYPTO | encryptEventContent() starts") Timber.v("## CRYPTO | encryptEventContent() starts")
runCatching { runCatching {
@ -627,9 +603,7 @@ internal class DefaultCryptoService @Inject constructor(
MXEncryptEventContentResult(content, EventType.ENCRYPTED) MXEncryptEventContentResult(content, EventType.ENCRYPTED)
}.foldToCallback(callback) }.foldToCallback(callback)
} else { } else {
val algorithm = getEncryptionAlgorithm(roomId) val reason = String.format(MXCryptoError.UNABLE_TO_ENCRYPT_REASON, MXCryptoError.NO_MORE_ALGORITHM_REASON)
val reason = String.format(MXCryptoError.UNABLE_TO_ENCRYPT_REASON,
algorithm ?: MXCryptoError.NO_MORE_ALGORITHM_REASON)
Timber.e("## CRYPTO | encryptEventContent() : $reason") Timber.e("## CRYPTO | encryptEventContent() : $reason")
callback.onFailure(Failure.CryptoError(MXCryptoError.Base(MXCryptoError.ErrorType.UNABLE_TO_ENCRYPT, reason))) callback.onFailure(Failure.CryptoError(MXCryptoError.Base(MXCryptoError.ErrorType.UNABLE_TO_ENCRYPT, reason)))
} }
@ -637,9 +611,7 @@ internal class DefaultCryptoService @Inject constructor(
} }
override fun discardOutboundSession(roomId: String) { override fun discardOutboundSession(roomId: String) {
cryptoCoroutineScope.launch(coroutineDispatchers.crypto) { // TODO
roomEncryptorsStore.get(roomId)?.discardSessionKey()
}
} }
/** /**
@ -706,7 +678,10 @@ internal class DefaultCryptoService @Inject constructor(
* @param event the membership event causing the change * @param event the membership event causing the change
*/ */
private suspend fun onRoomMembershipEvent(roomId: String, event: Event) { private suspend fun onRoomMembershipEvent(roomId: String, event: Event) {
roomEncryptorsStore.get(roomId) ?: /* No encrypting in this room */ return // We only care about the memberships if this room is encrypted
if (isRoomEncrypted(roomId)) {
return
}
event.stateKey?.let { userId -> event.stateKey?.let { userId ->
val roomMember: RoomMemberContent? = event.content.toModel() val roomMember: RoomMemberContent? = event.content.toModel()