From 2f4725cfe96172827c8d62a5b4aaa76e74ae25f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Mart=C3=ADn?= Date: Mon, 8 Aug 2022 12:22:05 +0200 Subject: [PATCH] Prevent crash while validating biometric key. --- changelog.d/6768.bugfix | 1 + .../features/pin/lockscreen/crypto/KeyStoreCrypto.kt | 12 +++--------- 2 files changed, 4 insertions(+), 9 deletions(-) create mode 100644 changelog.d/6768.bugfix diff --git a/changelog.d/6768.bugfix b/changelog.d/6768.bugfix new file mode 100644 index 0000000000..764386132b --- /dev/null +++ b/changelog.d/6768.bugfix @@ -0,0 +1 @@ +Fix crash when biometric key is used when coming back to foreground and KeyStore reports that the device is still locked. diff --git a/vector/src/main/java/im/vector/app/features/pin/lockscreen/crypto/KeyStoreCrypto.kt b/vector/src/main/java/im/vector/app/features/pin/lockscreen/crypto/KeyStoreCrypto.kt index d37c11ed69..a42ce3a9b7 100644 --- a/vector/src/main/java/im/vector/app/features/pin/lockscreen/crypto/KeyStoreCrypto.kt +++ b/vector/src/main/java/im/vector/app/features/pin/lockscreen/crypto/KeyStoreCrypto.kt @@ -20,13 +20,13 @@ import android.annotation.SuppressLint import android.content.Context import android.os.Build import android.security.keystore.KeyPermanentlyInvalidatedException -import android.security.keystore.UserNotAuthenticatedException import android.util.Base64 import androidx.annotation.VisibleForTesting import androidx.biometric.BiometricPrompt import dagger.assisted.Assisted import dagger.assisted.AssistedFactory import dagger.assisted.AssistedInject +import org.matrix.android.sdk.api.extensions.tryOrNull import org.matrix.android.sdk.api.securestorage.SecretStoringUtils import org.matrix.android.sdk.api.util.BuildVersionSdkIntProvider import java.security.Key @@ -113,14 +113,8 @@ class KeyStoreCrypto @AssistedInject constructor( fun hasValidKey(): Boolean { val keyExists = hasKey() return if (buildVersionSdkIntProvider.get() >= Build.VERSION_CODES.M && keyExists) { - try { - ensureKey() - true - } catch (e: KeyPermanentlyInvalidatedException) { - false - } catch (e: UserNotAuthenticatedException) { - false - } + val initializedKey = tryOrNull("Error validating lockscreen system key.") { ensureKey() } + initializedKey != null } else { keyExists }