diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/crypto/keysbackup/BackupRecoveryKey.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/crypto/keysbackup/BackupRecoveryKey.kt index a3ab09d3d6..48db9c5ebf 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/crypto/keysbackup/BackupRecoveryKey.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/crypto/keysbackup/BackupRecoveryKey.kt @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 The Matrix.org Foundation C.I.C. + * Copyright (c) 2023 New Vector Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -50,6 +50,10 @@ class BackupRecoveryKey internal constructor(internal val inner: InnerBackupReco return this.toBase58() == other.toBase58() } + override fun hashCode(): Int { + return toBase58().hashCode() + } + override fun toBase58() = inner.toBase58() override fun toBase64() = inner.toBase64() diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/Device.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/Device.kt index 4cb329175b..0bd6ed06d1 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/Device.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/Device.kt @@ -68,7 +68,7 @@ internal class Device @AssistedInject constructor( } /** - * Request an interactive verification to begin + * Request an interactive verification to begin. * * This sends out a m.key.verification.request event over to-device messaging to * to this device. @@ -97,7 +97,8 @@ internal class Device @AssistedInject constructor( } } - /** Start an interactive verification with this device + /** + * Start an interactive verification with this device. * * This sends out a m.key.verification.start event with the method set to * m.sas.v1 to this device using to-device messaging. @@ -126,7 +127,7 @@ internal class Device @AssistedInject constructor( } /** - * Mark this device as locally trusted + * Mark this device as locally trusted. * * This won't upload any signatures, it will only mark the device as trusted * in the local database. @@ -139,7 +140,7 @@ internal class Device @AssistedInject constructor( } /** - * Manually verify this device + * Manually verify this device. * * This will sign the device with our self-signing key and upload the signatures * to the server. @@ -157,7 +158,7 @@ internal class Device @AssistedInject constructor( } /** - * Get the DeviceTrustLevel of this device + * Get the DeviceTrustLevel of this device. */ @Throws(CryptoStoreException::class) suspend fun trustLevel(): DeviceTrustLevel { diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/OlmMachine.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/OlmMachine.kt index 4646d74c9a..998c6ef753 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/OlmMachine.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/OlmMachine.kt @@ -251,6 +251,10 @@ internal class OlmMachine @Inject constructor( * sync. * * @param keyCounts The map of uploaded one-time key types and counts. + * + * @param deviceUnusedFallbackKeyTypes The key algorithms for which the server has an unused fallback key for the device. + * + * @return The handled events, decrypted if needed (secrets are zeroised). */ @Throws(CryptoStoreException::class) suspend fun receiveSyncChanges( @@ -312,7 +316,7 @@ internal class OlmMachine @Inject constructor( } /** - * Used for lazy migration of inboundGroupSession from EA to ER + * Used for lazy migration of inboundGroupSession from EA to ER. */ suspend fun importRoomKey(inbound: MXInboundMegolmSessionWrapper): Result { Timber.v("Migration:: Tentative lazy migration") @@ -380,6 +384,8 @@ internal class OlmMachine @Inject constructor( * @param users The list of users which are considered to be members of the room and should * receive the room key. * + * @param settings The encryption settings for that room. + * * @return The list of [Request.ToDevice] that need to be sent out. */ @Throws(CryptoStoreException::class) @@ -723,7 +729,7 @@ internal class OlmMachine @Inject constructor( ensureUsersKeys.invoke(userIds, forceDownload) } - fun getUserIdentityFlow(userId: String): Flow> { + private fun getUserIdentityFlow(userId: String): Flow> { return channelFlow { val userIdentityCollector = UserIdentityCollector(userId, this) val onClose = safeInvokeOnClose { @@ -789,7 +795,8 @@ internal class OlmMachine @Inject constructor( runBlocking { inner.discardRoomKey(roomId) } } - /** Get all the verification requests we have with the given user + /** + * Get all the verification requests we have with the given user. * * @param userId The ID of the user for which we would like to fetch the * verification requests @@ -800,7 +807,7 @@ internal class OlmMachine @Inject constructor( return verificationsProvider.getVerificationRequests(userId) } - /** Get a verification request for the given user with the given flow ID */ + /** Get a verification request for the given user with the given flow ID. */ fun getVerificationRequest(userId: String, flowId: String): VerificationRequest? { return verificationsProvider.getVerificationRequest(userId, flowId) } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/PrepareToEncryptUseCase.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/PrepareToEncryptUseCase.kt index 891e1fe3c0..8765d99bca 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/PrepareToEncryptUseCase.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/PrepareToEncryptUseCase.kt @@ -24,6 +24,7 @@ import kotlinx.coroutines.sync.withLock import kotlinx.coroutines.withContext import org.matrix.android.sdk.api.MatrixCoroutineDispatchers import org.matrix.android.sdk.api.crypto.MXCRYPTO_ALGORITHM_MEGOLM +import org.matrix.android.sdk.api.failure.Failure import org.matrix.android.sdk.api.logger.LoggerTag import org.matrix.android.sdk.api.session.crypto.MXCryptoError import org.matrix.android.sdk.internal.crypto.keysbackup.RustKeyBackupService @@ -79,7 +80,7 @@ internal class PrepareToEncryptUseCase @Inject constructor( if (algorithm == null) { val reason = String.format(MXCryptoError.UNABLE_TO_ENCRYPT_REASON, MXCryptoError.NO_MORE_ALGORITHM_REASON) Timber.tag(loggerTag.value).e("prepareToEncrypt() : $reason") - throw IllegalArgumentException("Missing algorithm") + throw Failure.CryptoError(MXCryptoError.Base(MXCryptoError.ErrorType.UNABLE_TO_ENCRYPT, reason)) } preshareRoomKey(roomId, userIds, forceDistributeToUnverified) } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/RustCrossSigningService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/RustCrossSigningService.kt index 5a200a59ff..c4fd06bcd0 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/RustCrossSigningService.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/RustCrossSigningService.kt @@ -39,7 +39,7 @@ internal class RustCrossSigningService @Inject constructor( ) : CrossSigningService { /** - * Is our own identity trusted + * Is our own identity trusted. */ override suspend fun isCrossSigningVerified(): Boolean { return when (val identity = olmMachine.getIdentity(olmMachine.userId())) { @@ -104,7 +104,7 @@ internal class RustCrossSigningService @Inject constructor( } /** - * Get the public cross signing keys for the given user + * Get the public cross signing keys for the given user. * * @param otherUserId The ID of the user for which we would like to fetch the cross signing keys. */ @@ -131,7 +131,7 @@ internal class RustCrossSigningService @Inject constructor( } /** - * Can we sign our other devices or other users? + * Can we sign our other devices or other users. * * Returning true means that we have the private self-signing and user-signing keys at hand. */ @@ -165,7 +165,7 @@ internal class RustCrossSigningService @Inject constructor( } /** - * Sign one of your devices and upload the signature + * Sign one of your devices and upload the signature. */ override suspend fun trustDevice(deviceId: String) { val device = olmMachine.getDevice(olmMachine.userId(), deviceId) @@ -174,15 +174,15 @@ internal class RustCrossSigningService @Inject constructor( if (verified) { return } else { - throw IllegalArgumentException("This device [$deviceId] is not known, or not yours") + require(false) { "This device [$deviceId] is not known, or not yours" } } } else { - throw IllegalArgumentException("This device [$deviceId] is not known") + require(false) { "This device [$deviceId] is not known" } } } /** - * Check if a device is trusted + * Check if a device is trusted. * * This will check that we have a valid trust chain from our own master key to a device, either * using the self-signing key for our own devices or using the user-signing key and the master diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/keysbackup/RustKeyBackupService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/keysbackup/RustKeyBackupService.kt index c3d172835a..cffee25bc0 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/keysbackup/RustKeyBackupService.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/keysbackup/RustKeyBackupService.kt @@ -855,7 +855,7 @@ internal class RustKeyBackupService @Inject constructor( } /** - * Do a backup if there are new keys, with a delay + * Do a backup if there are new keys, with a delay. */ suspend fun maybeBackupKeys() { withContext(coroutineDispatchers.crypto) { @@ -886,7 +886,7 @@ internal class RustKeyBackupService @Inject constructor( } /** - * Send a chunk of keys to backup + * Send a chunk of keys to backup. */ private suspend fun backupKeys(forceRecheck: Boolean = false) { Timber.v("backupKeys") diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/RustCryptoStore.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/RustCryptoStore.kt index b242a3ed34..bf9a5ebf13 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/RustCryptoStore.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/RustCryptoStore.kt @@ -70,7 +70,7 @@ private val loggerTag = LoggerTag("RealmCryptoStore", LoggerTag.CRYPTO) /** * In the transition phase, the rust SDK is still using parts to the realm crypto store, - * this should be removed after full migration + * this should be removed after full migration. */ @SessionScope internal class RustCryptoStore @Inject constructor( @@ -118,6 +118,7 @@ internal class RustCryptoStore @Inject constructor( /** * Retrieve a device by its identity key. * + * @param userId The device owner userId. * @param identityKey the device identity key (`MXDeviceInfo.identityKey`) * @return the device or null if not found */ @@ -134,7 +135,7 @@ internal class RustCryptoStore @Inject constructor( } /** - * Needed for lazy migration of sessions from the legacy store + * Needed for lazy migration of sessions from the legacy store. */ override fun getInboundGroupSession(sessionId: String, senderKey: String): MXInboundMegolmSessionWrapper? { val key = OlmInboundGroupSessionEntity.createPrimaryKey(sessionId, senderKey) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/migration/rust/ExtractUtils.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/migration/rust/RealmToMigrate.kt similarity index 100% rename from matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/migration/rust/ExtractUtils.kt rename to matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/migration/rust/RealmToMigrate.kt diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/verification/RustVerificationService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/verification/RustVerificationService.kt index 35965d6f2e..5a66f93b7c 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/verification/RustVerificationService.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/verification/RustVerificationService.kt @@ -40,14 +40,16 @@ import org.matrix.rustcomponents.sdk.crypto.VerificationRequestState import timber.log.Timber import javax.inject.Inject -/** A helper class to deserialize to-device `m.key.verification.*` events to fetch the transaction id out */ +/** + * A helper class to deserialize to-device `m.key.verification.*` events to fetch the transaction id out. + */ @JsonClass(generateAdapter = true) internal data class ToDeviceVerificationEvent( @Json(name = "sender") val sender: String?, @Json(name = "transaction_id") val transactionId: String ) -/** Helper method to fetch the unique ID of the verification event */ +/** Helper method to fetch the unique ID of the verification event. */ private fun getFlowId(event: Event): String? { return if (event.eventId != null) { event.getRelationContent()?.eventId @@ -57,7 +59,7 @@ private fun getFlowId(event: Event): String? { } } -/** Convert a list of VerificationMethod into a list of strings that can be passed to the Rust side */ +/** Convert a list of VerificationMethod into a list of strings that can be passed to the Rust side. */ internal fun prepareMethods(methods: List): List { val stringMethods: MutableList = methods.map { it.toValue() }.toMutableList() @@ -147,7 +149,7 @@ internal class RustVerificationService @Inject constructor( } } - /** Check if the start event created new verification objects and dispatch updates */ + /** Check if the start event created new verification objects and dispatch updates. */ private suspend fun onStart(event: Event) { if (event.unsignedData?.transactionId != null) return // remote echo val sender = event.senderId ?: return @@ -186,7 +188,7 @@ internal class RustVerificationService @Inject constructor( } } - /** Check if the request event created a nev verification request object and dispatch that it dis so */ + /** Check if the request event created a nev verification request object and dispatch that it dis so. */ private suspend fun onRequest(event: Event, fromRoomMessage: Boolean) { val flowId = if (fromRoomMessage) { event.eventId diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/verification/SasVerification.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/verification/SasVerification.kt index 12ca5ae6e5..13afdd7b5b 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/verification/SasVerification.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/verification/SasVerification.kt @@ -35,7 +35,7 @@ import org.matrix.rustcomponents.sdk.crypto.Sas import org.matrix.rustcomponents.sdk.crypto.SasListener import org.matrix.rustcomponents.sdk.crypto.SasState -/** Class representing a short auth string verification flow */ +/** Class representing a short auth string verification flow. */ internal class SasVerification @AssistedInject constructor( @Assisted private var inner: Sas, // private val olmMachine: OlmMachine, @@ -56,14 +56,14 @@ internal class SasVerification @AssistedInject constructor( fun create(inner: Sas): SasVerification } - /** The user ID of the other user that is participating in this verification flow */ + /** The user ID of the other user that is participating in this verification flow. */ override val otherUserId: String = inner.otherUserId() - /** Get the device id of the other user's device participating in this verification flow */ + /** Get the device id of the other user's device participating in this verification flow. */ override val otherDeviceId: String get() = inner.otherDeviceId() - /** Did the other side initiate this verification flow */ + /** Did the other side initiate this verification flow. */ override val isIncoming: Boolean get() = !inner.weStarted() @@ -85,11 +85,11 @@ internal class SasVerification @AssistedInject constructor( } } - /** Get the unique id of this verification */ + /** Get the unique id of this verification. */ override val transactionId: String get() = inner.flowId() - /** Cancel the verification flow + /** Cancel the verification flow. * * This will send out a m.key.verification.cancel event with the cancel * code set to m.user. @@ -102,7 +102,7 @@ internal class SasVerification @AssistedInject constructor( cancelHelper(CancelCode.User) } - /** Cancel the verification flow + /** Cancel the verification flow. * * This will send out a m.key.verification.cancel event with the cancel * code set to the given CancelCode. @@ -117,7 +117,7 @@ internal class SasVerification @AssistedInject constructor( cancelHelper(code) } - /** Cancel the verification flow + /** Cancel the verification flow. * * This will send out a m.key.verification.cancel event with the cancel * code set to the m.mismatched_sas cancel code. @@ -141,7 +141,7 @@ internal class SasVerification @AssistedInject constructor( // return inner.supportsEmoji() // } - /** Confirm that the short authentication code matches on both sides + /** Confirm that the short authentication code matches on both sides. * * This sends a m.key.verification.mac event out, the verification isn't yet * done, we still need to receive such an event from the other side if we haven't @@ -154,7 +154,7 @@ internal class SasVerification @AssistedInject constructor( confirm() } - /** Accept the verification flow, signaling the other side that we do want to verify + /** Accept the verification flow, signaling the other side that we do want to verify. * * This sends a m.key.verification.accept event out that is a response to a * m.key.verification.start event from the other side. @@ -166,7 +166,7 @@ internal class SasVerification @AssistedInject constructor( accept() } - /** Get the decimal representation of the short auth string + /** Get the decimal representation of the short auth string. * * @return A string of three space delimited numbers that * represent the short auth string or an empty string if we're not yet @@ -176,7 +176,7 @@ internal class SasVerification @AssistedInject constructor( return decimals?.joinToString(" ") ?: "" } - /** Get the emoji representation of the short auth string + /** Get the emoji representation of the short auth string. * * @return A list of 7 EmojiRepresentation objects that represent the * short auth string or an empty list if we're not yet in a presentable diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/verification/qrcode/QrCodeVerification.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/verification/qrcode/QrCodeVerification.kt index dcf4c4013d..03df266108 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/verification/qrcode/QrCodeVerification.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/verification/qrcode/QrCodeVerification.kt @@ -36,7 +36,7 @@ import org.matrix.rustcomponents.sdk.crypto.QrCode import org.matrix.rustcomponents.sdk.crypto.QrCodeState import timber.log.Timber -/** Class representing a QR code based verification flow */ +/** Class representing a QR code based verification flow. */ internal class QrCodeVerification @AssistedInject constructor( @Assisted private var inner: QrCode, private val olmMachine: OlmMachine, @@ -86,12 +86,12 @@ internal class QrCodeVerification @AssistedInject constructor( // dispatchTxUpdated() // } - /** Confirm that the other side has indeed scanned the QR code we presented */ + /** Confirm that the other side has indeed scanned the QR code we presented. */ override suspend fun otherUserScannedMyQrCode() { confirm() } - /** Cancel the QR code verification, denying that the other side has scanned the QR code */ + /** Cancel the QR code verification, denying that the other side has scanned the QR code. */ override suspend fun otherUserDidNotScannedMyQrCode() { // TODO Is this code correct here? The old code seems to do this cancelHelper(CancelCode.MismatchedKeys) @@ -115,26 +115,26 @@ internal class QrCodeVerification @AssistedInject constructor( } } - /** Get the unique id of this verification */ + /** Get the unique id of this verification. */ override val transactionId: String get() = inner.flowId() - /** Get the user id of the other user participating in this verification flow */ + /** Get the user id of the other user participating in this verification flow. */ override val otherUserId: String get() = inner.otherUserId() - /** Get the device id of the other user's device participating in this verification flow */ + /** Get the device id of the other user's device participating in this verification flow. */ override var otherDeviceId: String? get() = inner.otherDeviceId() @Suppress("UNUSED_PARAMETER") set(value) { } - /** Did the other side initiate this verification flow */ + /** Did the other side initiate this verification flow. */ override val isIncoming: Boolean get() = !inner.weStarted() - /** Cancel the verification flow + /** Cancel the verification flow. * * This will send out a m.key.verification.cancel event with the cancel * code set to m.user. @@ -147,7 +147,7 @@ internal class QrCodeVerification @AssistedInject constructor( cancelHelper(CancelCode.User) } - /** Cancel the verification flow + /** Cancel the verification flow. * * This will send out a m.key.verification.cancel event with the cancel * code set to the given CancelCode. @@ -162,12 +162,12 @@ internal class QrCodeVerification @AssistedInject constructor( cancelHelper(code) } - /** Is this verification happening over to-device messages */ + /** Is this verification happening over to-device messages. */ override fun isToDeviceTransport(): Boolean { return inner.roomId() == null } - /** Confirm the QR code verification + /** Confirm the QR code verification. * * This confirms that the other side has scanned our QR code and sends * out a m.key.verification.done event to the other side. @@ -202,7 +202,7 @@ internal class QrCodeVerification @AssistedInject constructor( } } - /** Fetch fresh data from the Rust side for our verification flow */ + /** Fetch fresh data from the Rust side for our verification flow. */ private fun refreshData() { innerMachine.getVerification(inner.otherUserId(), inner.flowId()) ?.asQr()?.let {