コード例 #1
0
        public override bool Init()
        {
            Kp2aLog.Log("FP: Init for Dec");
            try
            {
                _keystore.Load(null);
                var key      = _keystore.GetKey(GetAlias(_keyId), null);
                var ivParams = new IvParameterSpec(_iv);
                _cipher.Init(CipherMode.DecryptMode, key, ivParams);

                _cryptoObject = new BiometricPrompt.CryptoObject(_cipher);
                return(true);
            }
            catch (KeyPermanentlyInvalidatedException e)
            {
                Kp2aLog.Log("FP: KeyPermanentlyInvalidatedException." + e.ToString());
                return(false);
            }
            catch (KeyStoreException e)
            {
                throw new RuntimeException(FailedToInitCipher, e);
            }
            catch (CertificateException e)
            {
                throw new RuntimeException(FailedToInitCipher, e);
            }
            catch (UnrecoverableKeyException e)
            {
                throw new RuntimeException(FailedToInitCipher, e);
            }
            catch (IOException e)
            {
                throw new RuntimeException(FailedToInitCipher, e);
            }
            catch (NoSuchAlgorithmException e)
            {
                throw new RuntimeException(FailedToInitCipher, e);
            }
            catch (InvalidKeyException e)
            {
                throw new RuntimeException(FailedToInitCipher, e);
            }
        }
コード例 #2
0
        public override bool Init()
        {
            Kp2aLog.Log("FP: Init for Dec");
            try
            {
                _keystore.Load(null);
                var aliases = _keystore.Aliases();
                if (aliases == null)
                {
                    Kp2aLog.Log("KS: no aliases");
                }
                else
                {
                    while (aliases.HasMoreElements)
                    {
                        var o = aliases.NextElement();
                        Kp2aLog.Log("alias: " + o?.ToString());
                    }
                    Kp2aLog.Log("KS: end aliases");
                }
                var key = _keystore.GetKey(GetAlias(_keyId), null);
                if (key == null)
                {
                    throw new Exception("Failed to init cipher for fingerprint Init: key is null");
                }
                var ivParams = new IvParameterSpec(_iv);
                _cipher.Init(CipherMode.DecryptMode, key, ivParams);

                _cryptoObject = new BiometricPrompt.CryptoObject(_cipher);
                return(true);
            }
            catch (KeyPermanentlyInvalidatedException e)
            {
                Kp2aLog.Log("FP: KeyPermanentlyInvalidatedException." + e.ToString());
                return(false);
            }
            catch (KeyStoreException e)
            {
                throw new RuntimeException(FailedToInitCipher + " (keystore)", e);
            }
            catch (CertificateException e)
            {
                throw new RuntimeException(FailedToInitCipher + " (CertificateException)", e);
            }
            catch (UnrecoverableKeyException e)
            {
                throw new RuntimeException(FailedToInitCipher + " (UnrecoverableKeyException)", e);
            }
            catch (IOException e)
            {
                throw new RuntimeException(FailedToInitCipher + " (IOException)", e);
            }
            catch (NoSuchAlgorithmException e)
            {
                throw new RuntimeException(FailedToInitCipher + " (NoSuchAlgorithmException)", e);
            }
            catch (InvalidKeyException e)
            {
                throw new RuntimeException(FailedToInitCipher + " (InvalidKeyException)" + e.ToString(), e);
            }
        }
コード例 #3
0
        private async Task <BiometricPrompt.AuthenticationResult> AuthenticateAndProcess(CancellationToken ct, string keyName, BiometricPrompt.CryptoObject crypto = null)
        {
            if (this.Log().IsEnabled(LogLevel.Debug))
            {
                this.Log().Debug($"Authenticating and processing the fingerprint (key name: '{keyName}').");
            }

            int result = 0;

            if (Android.OS.Build.VERSION.SdkInt <= Android.OS.BuildVersionCodes.Q)
            {
                result = _biometricManager.CanAuthenticate();
            }
            else
            {
                result = _biometricManager.CanAuthenticate(BiometricManager.Authenticators.BiometricStrong);
            }

            if (result == BiometricManager.BiometricSuccess)
            {
                _authenticationCompletionSource = new TaskCompletionSource <BiometricPrompt.AuthenticationResult>();

                // Prepare and show UI
                var prompt = await _promptInfoBuilder(ct);

                await _dispatcher.RunAsync(CoreDispatcherPriority.High, () =>
                {
                    try
                    {
                        if (crypto == null)
                        {
                            _biometricPrompt.Authenticate(prompt);
                        }
                        else
                        {
                            _biometricPrompt.Authenticate(prompt, crypto);
                        }
                    }
                    catch (System.Exception e)
                    {
                        _authenticationCompletionSource.TrySetException(e);
                    }
                });

                var authenticationTask = _authenticationCompletionSource.Task;
                await Task.WhenAny(authenticationTask);

                if (authenticationTask.IsCompletedSuccessfully && this.Log().IsEnabled(LogLevel.Information))
                {
                    this.Log().Info($"Successfully authenticated and processed the fingerprint (key name: '{keyName}').");
                }

                if (authenticationTask.IsCanceled)
                {
                    throw new OperationCanceledException();
                }
                return(authenticationTask.Result);
            }
            else
            {
                if (result == BiometricManager.BiometricErrorNoneEnrolled)
                {
                    throw new InvalidOperationException("No fingerprint(s) registered.");
                }
                else
                {
                    if (this.Log().IsEnabled(LogLevel.Warning))
                    {
                        this.Log().Warn($"Fingerprint authentication is not available.");
                    }

                    throw new NotSupportedException("Fingerprint authentication is not available.");
                }
            }
        }