diff --git a/cryptography/src/commonJvmAndroid/kotlin/com.wire.kalium.cryptography/CoreCryptoCentral.kt b/cryptography/src/commonJvmAndroid/kotlin/com.wire.kalium.cryptography/CoreCryptoCentral.kt index 89c70247f9e..72faee95ae6 100644 --- a/cryptography/src/commonJvmAndroid/kotlin/com.wire.kalium.cryptography/CoreCryptoCentral.kt +++ b/cryptography/src/commonJvmAndroid/kotlin/com.wire.kalium.cryptography/CoreCryptoCentral.kt @@ -20,7 +20,10 @@ package com.wire.kalium.cryptography import com.wire.crypto.ClientId import com.wire.crypto.CoreCrypto import com.wire.crypto.CoreCryptoCallbacks +import com.wire.crypto.CoreCryptoLogLevel +import com.wire.crypto.CoreCryptoLogger import com.wire.crypto.coreCryptoDeferredInit +import com.wire.crypto.setLogger import com.wire.kalium.cryptography.MLSClientImpl.Companion.toCrlRegistration import com.wire.kalium.cryptography.exceptions.CryptographyException import java.io.File @@ -36,12 +39,28 @@ actual suspend fun coreCryptoCentral( key = databaseKey ) coreCrypto.setCallbacks(Callbacks()) + setLogger(CoreCryptoLoggerImpl(), CoreCryptoLogLevel.INFO) return CoreCryptoCentralImpl( cc = coreCrypto, rootDir = rootDir ) } +private class CoreCryptoLoggerImpl : CoreCryptoLogger { + override fun log(level: CoreCryptoLogLevel, message: String, context: String?) { + when (level) { + CoreCryptoLogLevel.TRACE -> kaliumLogger.v("$message. $context") + CoreCryptoLogLevel.DEBUG -> kaliumLogger.d("$message. $context") + CoreCryptoLogLevel.INFO -> kaliumLogger.i("$message. $context") + CoreCryptoLogLevel.WARN -> kaliumLogger.w("$message. $context") + CoreCryptoLogLevel.ERROR -> kaliumLogger.e("$message. $context") + CoreCryptoLogLevel.OFF -> { + // nop + } + } + } +} + private class Callbacks : CoreCryptoCallbacks { override suspend fun authorize(conversationId: ByteArray, clientId: ClientId): Boolean { // We always return true because our BE is currently enforcing that this constraint is always true diff --git a/cryptography/src/commonJvmAndroid/kotlin/com.wire.kalium.cryptography/ProteusClientCoreCryptoImpl.kt b/cryptography/src/commonJvmAndroid/kotlin/com.wire.kalium.cryptography/ProteusClientCoreCryptoImpl.kt index fdc9bc2b69c..f972b6ea165 100644 --- a/cryptography/src/commonJvmAndroid/kotlin/com.wire.kalium.cryptography/ProteusClientCoreCryptoImpl.kt +++ b/cryptography/src/commonJvmAndroid/kotlin/com.wire.kalium.cryptography/ProteusClientCoreCryptoImpl.kt @@ -28,6 +28,7 @@ import io.ktor.util.encodeBase64 import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.withLock import java.io.File +import com.wire.crypto.ProteusException as ProteusExceptionNative @Suppress("TooManyFunctions") class ProteusClientCoreCryptoImpl private constructor( @@ -145,13 +146,12 @@ class ProteusClientCoreCryptoImpl private constructor( private inline fun wrapException(b: () -> T): T { try { return b() - } catch (e: CoreCryptoException) { - val proteusLastErrorCode = coreCrypto.proteusLastErrorCode() + } catch (e: CoreCryptoException.Proteus) { throw ProteusException( - e.message, - ProteusException.fromProteusCode(proteusLastErrorCode.toInt()), - proteusLastErrorCode.toInt(), - e + message = e.message, + code = mapProteusExceptionToErrorCode(e.v1), + intCode = mapProteusExceptionToRawIntErrorCode(e.v1), + cause = e ) } catch (e: Exception) { throw ProteusException(e.message, ProteusException.Code.UNKNOWN_ERROR, null, e) @@ -187,11 +187,11 @@ class ProteusClientCoreCryptoImpl private constructor( return ProteusClientCoreCryptoImpl(coreCrypto) } catch (exception: ProteusStorageMigrationException) { throw exception - } catch (e: CoreCryptoException) { + } catch (e: CoreCryptoException.Proteus) { throw ProteusException( message = e.message, - code = ProteusException.fromProteusCode(coreCrypto.proteusLastErrorCode().toInt()), - intCode = coreCrypto.proteusLastErrorCode().toInt(), + code = mapProteusExceptionToErrorCode(e.v1), + intCode = mapProteusExceptionToRawIntErrorCode(e.v1), cause = e.cause ) } catch (e: Exception) { @@ -218,5 +218,24 @@ class ProteusClientCoreCryptoImpl private constructor( throw ProteusStorageMigrationException("Failed to migrate from crypto box at $rootDir", exception) } } + + private fun mapProteusExceptionToErrorCode(proteusException: ProteusExceptionNative): ProteusException.Code { + return when (proteusException) { + is ProteusExceptionNative.SessionNotFound -> ProteusException.Code.SESSION_NOT_FOUND + is ProteusExceptionNative.DuplicateMessage -> ProteusException.Code.DUPLICATE_MESSAGE + is ProteusExceptionNative.RemoteIdentityChanged -> ProteusException.Code.REMOTE_IDENTITY_CHANGED + is ProteusExceptionNative.Other -> ProteusException.fromProteusCode(proteusException.v1.toInt()) + } + } + + @Suppress("MagicNumber") + private fun mapProteusExceptionToRawIntErrorCode(proteusException: ProteusExceptionNative): Int { + return when (proteusException) { + is ProteusExceptionNative.SessionNotFound -> 102 + is ProteusExceptionNative.DuplicateMessage -> 209 + is ProteusExceptionNative.RemoteIdentityChanged -> 204 + is ProteusExceptionNative.Other -> proteusException.v1.toInt() + } + } } } diff --git a/cryptography/src/commonMain/kotlin/com/wire/kalium/cryptography/exceptions/ProteusException.kt b/cryptography/src/commonMain/kotlin/com/wire/kalium/cryptography/exceptions/ProteusException.kt index 547d0deefa9..18750c29f92 100644 --- a/cryptography/src/commonMain/kotlin/com/wire/kalium/cryptography/exceptions/ProteusException.kt +++ b/cryptography/src/commonMain/kotlin/com/wire/kalium/cryptography/exceptions/ProteusException.kt @@ -176,19 +176,25 @@ open class ProteusException(message: String?, val code: Code, val intCode: Int?, } } - // Mapping source: - // https://github.com/wireapp/proteus/blob/2.x/crates/proteus-traits/src/lib.rs - // https://github.com/wireapp/wire-web-core/blob/7383e108f5e9d15d0b82c41ed504964667463cfc/packages/proteus/README.md + /** + * Those error codes are mapped directly from [com.wire.crypto.ProteusException]: + * - [Code.SESSION_NOT_FOUND] + * - [Code.REMOTE_IDENTITY_CHANGED] + * - [Code.DUPLICATE_MESSAGE] + * + * See the mapping: [com.wire.kalium.cryptography.ProteusClientCoreCryptoImpl.Companion.mapProteusExceptionToErrorCode] + * + * [Mapping sources](https://github.com/wireapp/proteus/blob/2.x/crates/proteus-traits/src/lib.rs) + * + * [Mapping source README](https://github.com/wireapp/wire-web-core/blob/7383e108f5e9d15d0b82c41ed504964667463cfc/packages/proteus/README.md) + */ fun fromProteusCode(code: Int): Code { @Suppress("MagicNumber") return when (code) { 501 -> Code.STORAGE_ERROR - 102 -> Code.SESSION_NOT_FOUND 3, 301, 302, 303 -> Code.DECODE_ERROR - 204 -> Code.REMOTE_IDENTITY_CHANGED 206, 207, 210 -> Code.INVALID_SIGNATURE 200, 201, 202, 205, 213 -> Code.INVALID_MESSAGE - 209 -> Code.DUPLICATE_MESSAGE 211, 212 -> Code.TOO_DISTANT_FUTURE 208 -> Code.OUTDATED_MESSAGE 300 -> Code.IDENTITY_ERROR diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 8fe82ae3248..6c3b94a0f59 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -39,7 +39,7 @@ pbandk = "0.14.2" turbine = "1.1.0" avs = "10.0.1" jna = "5.14.0" -core-crypto = "1.0.2" +core-crypto = "2.0.0" core-crypto-multiplatform = "0.6.0-rc.3-multiplatform-pre1" completeKotlin = "1.1.0" desugar-jdk = "2.1.3" diff --git a/logic/src/commonJvmAndroid/kotlin/com/wire/kalium/logic/CoreCryptoExceptionMapper.kt b/logic/src/commonJvmAndroid/kotlin/com/wire/kalium/logic/CoreCryptoExceptionMapper.kt index a41535a1dc3..d2b7342a6d1 100644 --- a/logic/src/commonJvmAndroid/kotlin/com/wire/kalium/logic/CoreCryptoExceptionMapper.kt +++ b/logic/src/commonJvmAndroid/kotlin/com/wire/kalium/logic/CoreCryptoExceptionMapper.kt @@ -18,21 +18,20 @@ package com.wire.kalium.logic import com.wire.crypto.CoreCryptoException -import uniffi.core_crypto.CryptoError +import com.wire.crypto.MlsException actual fun mapMLSException(exception: Exception): MLSFailure = - if (exception is CoreCryptoException.CryptoException) { - when (exception.error) { - is CryptoError.WrongEpoch -> MLSFailure.WrongEpoch - is CryptoError.DuplicateMessage -> MLSFailure.DuplicateMessage - is CryptoError.BufferedFutureMessage -> MLSFailure.BufferedFutureMessage - is CryptoError.SelfCommitIgnored -> MLSFailure.SelfCommitIgnored - is CryptoError.UnmergedPendingGroup -> MLSFailure.UnmergedPendingGroup - is CryptoError.StaleProposal -> MLSFailure.StaleProposal - is CryptoError.StaleCommit -> MLSFailure.StaleCommit - is CryptoError.ConversationAlreadyExists -> MLSFailure.ConversationAlreadyExists - is CryptoError.MessageEpochTooOld -> MLSFailure.MessageEpochTooOld - is CryptoError.MlsException -> MLSFailure.InternalErrors + if (exception is CoreCryptoException.Mls) { + when (exception.v1) { + is MlsException.WrongEpoch -> MLSFailure.WrongEpoch + is MlsException.DuplicateMessage -> MLSFailure.DuplicateMessage + is MlsException.BufferedFutureMessage -> MLSFailure.BufferedFutureMessage + is MlsException.SelfCommitIgnored -> MLSFailure.SelfCommitIgnored + is MlsException.UnmergedPendingGroup -> MLSFailure.UnmergedPendingGroup + is MlsException.StaleProposal -> MLSFailure.StaleProposal + is MlsException.StaleCommit -> MLSFailure.StaleCommit + is MlsException.ConversationAlreadyExists -> MLSFailure.ConversationAlreadyExists + is MlsException.MessageEpochTooOld -> MLSFailure.MessageEpochTooOld else -> MLSFailure.Generic(exception) } } else {