From 26b79b928c37e349679291eb2fd15ced4fff678e Mon Sep 17 00:00:00 2001 From: Fi5t Date: Fri, 22 Jul 2022 22:32:16 +0300 Subject: [PATCH] Add workaround for issue #25 --- .../java/com/redmadrobot/pinkman/Pinkman.kt | 25 +++++++++++++------ .../exception/CorruptedStorageException.kt | 5 ++++ 2 files changed, 23 insertions(+), 7 deletions(-) create mode 100644 pinkman/src/main/java/com/redmadrobot/pinkman/exception/CorruptedStorageException.kt diff --git a/pinkman/src/main/java/com/redmadrobot/pinkman/Pinkman.kt b/pinkman/src/main/java/com/redmadrobot/pinkman/Pinkman.kt index 24377a2..7e56d2b 100644 --- a/pinkman/src/main/java/com/redmadrobot/pinkman/Pinkman.kt +++ b/pinkman/src/main/java/com/redmadrobot/pinkman/Pinkman.kt @@ -11,6 +11,7 @@ import androidx.security.crypto.EncryptedFile import androidx.security.crypto.MasterKeys import com.lambdapioneer.argon2kt.Argon2Exception import com.redmadrobot.pinkman.exception.BlacklistedPinException +import com.redmadrobot.pinkman.exception.CorruptedStorageException import com.redmadrobot.pinkman.internal.Salt import com.redmadrobot.pinkman.internal.argon2.Argon2 import com.redmadrobot.pinkman.internal.exception.BadHashException @@ -37,7 +38,12 @@ class Pinkman( .setKeySize(KEY_SIZE) .apply { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { - val isDeviceSecure = isDeviceSecure() + //FIXME: Dirty workaround. Waiting for a fix https://issuetracker.google.com/issues/191391068?pli=1 + val isDeviceSecure = if (Build.VERSION.SDK_INT == 31) { + false + } else { + isDeviceSecure() + } setUnlockedDeviceRequired(isDeviceSecure) @@ -52,12 +58,17 @@ class Pinkman( }.build() private val encryptedStorage by lazy { - EncryptedFile.Builder( - storageFile, - applicationContext, - MasterKeys.getOrCreate(keySpec), - EncryptedFile.FileEncryptionScheme.AES256_GCM_HKDF_4KB - ).setKeysetAlias(KEYSET_ALIAS).setKeysetPrefName(PREFERENCE_FILE).build() + try { + EncryptedFile.Builder( + storageFile, + applicationContext, + MasterKeys.getOrCreate(keySpec), + EncryptedFile.FileEncryptionScheme.AES256_GCM_HKDF_4KB + ).setKeysetAlias(KEYSET_ALIAS).setKeysetPrefName(PREFERENCE_FILE).build() + } catch (e: Exception) { + //FIXME: Dirty workaround. Waiting for a fix https://issuetracker.google.com/issues/191391068?pli=1 + throw CorruptedStorageException("Pinkman storage was corrupted. Please, remove existing PIN and create it again.") + } } private fun isDeviceSecure(): Boolean { diff --git a/pinkman/src/main/java/com/redmadrobot/pinkman/exception/CorruptedStorageException.kt b/pinkman/src/main/java/com/redmadrobot/pinkman/exception/CorruptedStorageException.kt new file mode 100644 index 0000000..875ce13 --- /dev/null +++ b/pinkman/src/main/java/com/redmadrobot/pinkman/exception/CorruptedStorageException.kt @@ -0,0 +1,5 @@ +package com.redmadrobot.pinkman.exception + +import java.lang.RuntimeException + +class CorruptedStorageException(message: String) : RuntimeException(message)