From 0388738e02c275dce8f205c989c5852fa0fcfe14 Mon Sep 17 00:00:00 2001 From: Matt Portune <59324545+mportune-bw@users.noreply.github.com> Date: Mon, 21 Sep 2020 11:59:56 -0400 Subject: [PATCH] fix issue with biometric validation (#1081) * fix issue with biometric validation * null check key & cipher --- src/Android/Services/BiometricService.cs | 31 +++++++++++++++++------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/src/Android/Services/BiometricService.cs b/src/Android/Services/BiometricService.cs index 69d4df2d8..83b19e5dc 100644 --- a/src/Android/Services/BiometricService.cs +++ b/src/Android/Services/BiometricService.cs @@ -51,6 +51,11 @@ namespace Bit.Droid.Services IKey key = _keystore.GetKey(KeyName, null); Cipher cipher = Cipher.GetInstance(Transformation); + if (key == null || cipher == null) + { + return Task.FromResult(true); + } + try { cipher.Init(CipherMode.EncryptMode, key); @@ -76,15 +81,23 @@ namespace Bit.Droid.Services private void CreateKey() { - KeyGenerator keyGen = KeyGenerator.GetInstance(KeyAlgorithm, KeyStoreName); - KeyGenParameterSpec keyGenSpec = - new KeyGenParameterSpec.Builder(KeyName, KeyStorePurpose.Encrypt | KeyStorePurpose.Decrypt) - .SetBlockModes(BlockMode) - .SetEncryptionPaddings(EncryptionPadding) - .SetUserAuthenticationRequired(true) - .Build(); - keyGen.Init(keyGenSpec); - keyGen.GenerateKey(); + try + { + var keyGen = KeyGenerator.GetInstance(KeyAlgorithm, KeyStoreName); + var keyGenSpec = + new KeyGenParameterSpec.Builder(KeyName, KeyStorePurpose.Encrypt | KeyStorePurpose.Decrypt) + .SetBlockModes(BlockMode) + .SetEncryptionPaddings(EncryptionPadding) + .SetUserAuthenticationRequired(true) + .Build(); + keyGen.Init(keyGenSpec); + keyGen.GenerateKey(); + } + catch + { + // Catch silently to allow biometrics to function on devices that are in a state where key generation + // is not functioning + } } } }