From 18e9de9b6de7a86e110975c0eb062c5073f87917 Mon Sep 17 00:00:00 2001 From: Brian Pellin Date: Thu, 13 Feb 2020 23:44:20 -0600 Subject: [PATCH] Handle failure to init biometrics crypto more gracefully --- .../com/keepassdroid/biometric/BiometricHelper.java | 11 ++++++++--- .../keepassdroid/fragments/PasswordFragment.java | 13 ++++++++----- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/com/keepassdroid/biometric/BiometricHelper.java b/app/src/main/java/com/keepassdroid/biometric/BiometricHelper.java index 2f020eb1d..0b98dc1ed 100644 --- a/app/src/main/java/com/keepassdroid/biometric/BiometricHelper.java +++ b/app/src/main/java/com/keepassdroid/biometric/BiometricHelper.java @@ -118,17 +118,18 @@ public boolean isFingerprintInitialized() { return initOk; } - public void initEncryptData() { + public boolean initEncryptData() { cryptoInitOk = false; if (!isFingerprintInitialized()) { if (biometricCallback != null) { biometricCallback.onException(); } - return; + return false; } try { initEncryptKey(false); + return true; } catch (final InvalidKeyException invalidKeyException) { try { biometricCallback.onKeyInvalidated(); @@ -142,6 +143,7 @@ public void initEncryptData() { biometricCallback.onException(); } + return false; } private void initEncryptKey( @@ -183,12 +185,13 @@ public Cipher getCipher() { return cipher; } - public void initDecryptData( + public boolean initDecryptData( final String ivSpecValue) { cryptoInitOk = false; try { initDecryptKey(ivSpecValue,false); + return true; } catch (final InvalidKeyException invalidKeyException) { // Key was invalidated (maybe all registered fingerprints were changed) // Retry with new key @@ -203,6 +206,8 @@ public void initDecryptData( } catch (final Exception e) { biometricCallback.onException(); } + + return false; } private void initDecryptKey( diff --git a/app/src/main/java/com/keepassdroid/fragments/PasswordFragment.java b/app/src/main/java/com/keepassdroid/fragments/PasswordFragment.java index 4d377433c..841a58d0a 100644 --- a/app/src/main/java/com/keepassdroid/fragments/PasswordFragment.java +++ b/app/src/main/java/com/keepassdroid/fragments/PasswordFragment.java @@ -152,9 +152,10 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat biometricOpen.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - initDecryptData(); + if (!initDecryptData()) { + return; + } Cipher cipher = biometricHelper.getCipher(); - // if cipher == null biometricOpenPrompt.authenticate(loadPrompt, new BiometricPrompt.CryptoObject(cipher)); } }); @@ -326,10 +327,10 @@ private boolean canceledBiometricAuth(int errorCode) { } - private void initDecryptData() { + private boolean initDecryptData() { final String ivSpecValue = prefsNoBackup.getString(getPreferenceKeyIvSpec(), null); - biometricHelper.initDecryptData(ivSpecValue); + return biometricHelper.initDecryptData(ivSpecValue); } @@ -742,7 +743,9 @@ public void onClick( }); } else if (mSuccess) { if (biometricCheck.isChecked()) { - biometricHelper.initEncryptData(); + if (!biometricHelper.initEncryptData()) { + return; + } Cipher cipher = biometricHelper.getCipher(); biometricSavePrompt.authenticate(savePrompt, new BiometricPrompt.CryptoObject(cipher));