diff --git a/src/Android/MainApplication.cs b/src/Android/MainApplication.cs index be1cf8174..d1a3780c8 100644 --- a/src/Android/MainApplication.cs +++ b/src/Android/MainApplication.cs @@ -156,9 +156,9 @@ namespace Bit.Droid messagingService, broadcasterService); var autofillHandler = new AutofillHandler(stateService, messagingService, clipboardService, platformUtilsService, new LazyResolve()); - var biometricService = new BiometricService(stateService); var cryptoFunctionService = new PclCryptoFunctionService(cryptoPrimitiveService); var cryptoService = new CryptoService(stateService, cryptoFunctionService); + var biometricService = new BiometricService(stateService, cryptoService); var passwordRepromptService = new MobilePasswordRepromptService(platformUtilsService, cryptoService); ServiceContainer.Register(preferencesStorage); diff --git a/src/Android/Services/BiometricService.cs b/src/Android/Services/BiometricService.cs index 7a41b0319..fbca61cc0 100644 --- a/src/Android/Services/BiometricService.cs +++ b/src/Android/Services/BiometricService.cs @@ -2,6 +2,7 @@ using System.Threading.Tasks; using Android.OS; using Android.Security.Keystore; +using Bit.App.Services; using Bit.Core.Abstractions; using Bit.Core.Services; using Java.Security; @@ -9,10 +10,8 @@ using Javax.Crypto; namespace Bit.Droid.Services { - public class BiometricService : IBiometricService + public class BiometricService : BaseBiometricService { - private readonly IStateService _stateService; - private const string KeyName = "com.8bit.bitwarden.biometric_integrity"; private const string KeyStoreName = "AndroidKeyStore"; @@ -24,14 +23,14 @@ namespace Bit.Droid.Services private readonly KeyStore _keystore; - public BiometricService(IStateService stateService) + public BiometricService(IStateService stateService, ICryptoService cryptoService) + : base(stateService, cryptoService) { - _stateService = stateService; _keystore = KeyStore.GetInstance(KeyStoreName); _keystore.Load(null); } - public async Task SetupBiometricAsync(string bioIntegritySrcKey = null) + public override async Task SetupBiometricAsync(string bioIntegritySrcKey = null) { if (Build.VERSION.SdkInt >= BuildVersionCodes.M) { @@ -41,7 +40,7 @@ namespace Bit.Droid.Services return true; } - public async Task IsSystemBiometricIntegrityValidAsync(string bioIntegritySrcKey = null) + public override async Task IsSystemBiometricIntegrityValidAsync(string bioIntegritySrcKey = null) { if (Build.VERSION.SdkInt < BuildVersionCodes.M) { diff --git a/src/App/Pages/Accounts/LockPageViewModel.cs b/src/App/Pages/Accounts/LockPageViewModel.cs index 3ef580c9a..1faa9e6e2 100644 --- a/src/App/Pages/Accounts/LockPageViewModel.cs +++ b/src/App/Pages/Accounts/LockPageViewModel.cs @@ -168,7 +168,7 @@ namespace Bit.App.Pages PinEnabled = (_pinStatus == PinLockType.Transient && ephemeralPinSet != null) || _pinStatus == PinLockType.Persistent; - BiometricEnabled = await _vaultTimeoutService.IsBiometricLockSetAsync() && await _cryptoService.HasEncryptedUserKeyAsync(); + BiometricEnabled = await _vaultTimeoutService.IsBiometricLockSetAsync() && await _biometricService.CanUseBiometricsUnlockAsync(); // Users with key connector and without biometric or pin has no MP to unlock with _usingKeyConnector = await _keyConnectorService.GetUsesKeyConnectorAsync(); diff --git a/src/App/Services/BaseBiometricService.cs b/src/App/Services/BaseBiometricService.cs new file mode 100644 index 000000000..3d9ea9cc2 --- /dev/null +++ b/src/App/Services/BaseBiometricService.cs @@ -0,0 +1,25 @@ +using System.Threading.Tasks; +using Bit.Core.Abstractions; + +namespace Bit.App.Services +{ + public abstract class BaseBiometricService : IBiometricService + { + protected readonly IStateService _stateService; + protected readonly ICryptoService _cryptoService; + + protected BaseBiometricService(IStateService stateService, ICryptoService cryptoService) + { + _stateService = stateService; + _cryptoService = cryptoService; + } + + public async Task CanUseBiometricsUnlockAsync() + { + return await _cryptoService.HasEncryptedUserKeyAsync() || await _stateService.GetKeyEncryptedAsync() != null; + } + + public abstract Task IsSystemBiometricIntegrityValidAsync(string bioIntegritySrcKey = null); + public abstract Task SetupBiometricAsync(string bioIntegritySrcKey = null); + } +} diff --git a/src/Core/Abstractions/IBiometricService.cs b/src/Core/Abstractions/IBiometricService.cs index 232b301ca..3ce26c058 100644 --- a/src/Core/Abstractions/IBiometricService.cs +++ b/src/Core/Abstractions/IBiometricService.cs @@ -4,6 +4,7 @@ namespace Bit.Core.Abstractions { public interface IBiometricService { + Task CanUseBiometricsUnlockAsync(); Task SetupBiometricAsync(string bioIntegritySrcKey = null); Task IsSystemBiometricIntegrityValidAsync(string bioIntegritySrcKey = null); } diff --git a/src/iOS.Core/Services/BiometricService.cs b/src/iOS.Core/Services/BiometricService.cs index d34a4adff..3e68fdf66 100644 --- a/src/iOS.Core/Services/BiometricService.cs +++ b/src/iOS.Core/Services/BiometricService.cs @@ -1,20 +1,19 @@ using System.Threading.Tasks; +using Bit.App.Services; using Bit.Core.Abstractions; using Foundation; using LocalAuthentication; namespace Bit.iOS.Core.Services { - public class BiometricService : IBiometricService + public class BiometricService : BaseBiometricService { - private IStateService _stateService; - - public BiometricService(IStateService stateService) + public BiometricService(IStateService stateService, ICryptoService cryptoService) + : base(stateService, cryptoService) { - _stateService = stateService; } - public async Task SetupBiometricAsync(string bioIntegritySrcKey = null) + public override async Task SetupBiometricAsync(string bioIntegritySrcKey = null) { if (bioIntegritySrcKey == null) { @@ -30,7 +29,7 @@ namespace Bit.iOS.Core.Services return true; } - public async Task IsSystemBiometricIntegrityValidAsync(string bioIntegritySrcKey = null) + public override async Task IsSystemBiometricIntegrityValidAsync(string bioIntegritySrcKey = null) { var state = GetState(); if (state == null) diff --git a/src/iOS.Core/Utilities/iOSCoreHelpers.cs b/src/iOS.Core/Utilities/iOSCoreHelpers.cs index 09c558134..45b3ebd34 100644 --- a/src/iOS.Core/Utilities/iOSCoreHelpers.cs +++ b/src/iOS.Core/Utilities/iOSCoreHelpers.cs @@ -112,9 +112,9 @@ namespace Bit.iOS.Core.Utilities var clipboardService = new ClipboardService(stateService); var platformUtilsService = new MobilePlatformUtilsService(deviceActionService, clipboardService, messagingService, broadcasterService); - var biometricService = new BiometricService(stateService); var cryptoFunctionService = new PclCryptoFunctionService(cryptoPrimitiveService); var cryptoService = new CryptoService(stateService, cryptoFunctionService); + var biometricService = new BiometricService(stateService, cryptoService); var passwordRepromptService = new MobilePasswordRepromptService(platformUtilsService, cryptoService); ServiceContainer.Register(preferencesStorage);