From 51dd09a94cef15dcb4c076769278d3e8cbc52c0a Mon Sep 17 00:00:00 2001 From: Artem Chepurnoy Date: Mon, 8 Jan 2024 19:49:23 +0200 Subject: [PATCH] Show proper error message if generating KDF hash fails with OOM --- .../exception/OutOfMemoryKdfException.kt | 12 ++++++++++ .../keyguard/provider/bitwarden/api/login.kt | 23 +++++++++++++++---- .../commonMain/resources/MR/base/strings.xml | 1 + 3 files changed, 31 insertions(+), 5 deletions(-) create mode 100644 common/src/commonMain/kotlin/com/artemchep/keyguard/common/exception/OutOfMemoryKdfException.kt diff --git a/common/src/commonMain/kotlin/com/artemchep/keyguard/common/exception/OutOfMemoryKdfException.kt b/common/src/commonMain/kotlin/com/artemchep/keyguard/common/exception/OutOfMemoryKdfException.kt new file mode 100644 index 00000000..bdfadca7 --- /dev/null +++ b/common/src/commonMain/kotlin/com/artemchep/keyguard/common/exception/OutOfMemoryKdfException.kt @@ -0,0 +1,12 @@ +package com.artemchep.keyguard.common.exception + +import com.artemchep.keyguard.res.Res +import dev.icerock.moko.resources.StringResource + +open class OutOfMemoryKdfException( + m: String?, + e: Throwable?, +) : Exception(m, e), Readable { + override val title: StringResource + get() = Res.strings.error_failed_generate_kdf_hash_oom +} diff --git a/common/src/commonMain/kotlin/com/artemchep/keyguard/provider/bitwarden/api/login.kt b/common/src/commonMain/kotlin/com/artemchep/keyguard/provider/bitwarden/api/login.kt index dca94a38..c3654aa0 100644 --- a/common/src/commonMain/kotlin/com/artemchep/keyguard/provider/bitwarden/api/login.kt +++ b/common/src/commonMain/kotlin/com/artemchep/keyguard/provider/bitwarden/api/login.kt @@ -1,5 +1,6 @@ package com.artemchep.keyguard.provider.bitwarden.api +import com.artemchep.keyguard.common.exception.OutOfMemoryKdfException import com.artemchep.keyguard.common.io.bind import com.artemchep.keyguard.common.model.Argon2Mode import com.artemchep.keyguard.common.service.crypto.CryptoGenerator @@ -234,11 +235,23 @@ private fun generateSecrets( .lowercase(Locale.ENGLISH) .toByteArray() - val masterKey = cryptoGenerator.masterKeyHash( - seed = passwordBytes, - salt = emailBytes, - config = hashConfig, - ) + val masterKey = runCatching { + cryptoGenerator.masterKeyHash( + seed = passwordBytes, + salt = emailBytes, + config = hashConfig, + ) + }.getOrElse { e -> + if (e is OutOfMemoryError) { + val newError = OutOfMemoryKdfException( + m = e.localizedMessage ?: e.message, + e = e, + ) + throw newError + } + + throw e + } val passwordKey = cryptoGenerator.pbkdf2( seed = masterKey, salt = passwordBytes, diff --git a/common/src/commonMain/resources/MR/base/strings.xml b/common/src/commonMain/resources/MR/base/strings.xml index 3acbfcac..1f9ddba9 100644 --- a/common/src/commonMain/resources/MR/base/strings.xml +++ b/common/src/commonMain/resources/MR/base/strings.xml @@ -399,6 +399,7 @@ Invalid URI Invalid card number Incorrect password + Failed to create a key, out of memory. Please check your KDF server settings. Failed to generate OTP code Failed to create a passkey Failed to authorize a request