Uncaught exception thrown by finalizer: Binder has been finalized, android keystore

Viewed 1081

I implemented fingerprint unlock in my app, now i added pin unlock but the app doesn't start at all. I tried to change KeyStore Instance name and used the same KeyStore Instance name.

And sometimes the app throws this error:

E/System: Uncaught exception thrown by finalizer
E/System: java.lang.IllegalStateException: Binder has been finalized!
        at android.os.BinderProxy.transactNative(Native Method)
        at android.os.BinderProxy.transact(Binder.java:751)
        at android.security.IKeystoreService$Stub$Proxy.abort(IKeystoreService.java:1373)
        at android.security.KeyStore.abort(KeyStore.java:541)
        at android.security.keystore.AndroidKeyStoreCipherSpiBase.finalize(AndroidKeyStoreCipherSpiBase.java:744)
        at android.security.keystore.AndroidKeyStoreUnauthenticatedAESCipherSpi$CBC$PKCS7Padding.finalize(Unknown Source:0)
        at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:253)
        at java.lang.Daemons$FinalizerDaemon.runInternal(Daemons.java:240)
        at java.lang.Daemons$Daemon.run(Daemons.java:103)

When i try to start the app it just closes without a crash in debug logs or logcat logs, Any suggestions for this problem?

Code:

@Override

    protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.auth);

    keyguardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
    fingerprintManager = (FingerprintManager) getSystemService(FINGERPRINT_SERVICE);
    msg = (TextView) findViewById(R.id.message);


        try {
            generateKey();
        } catch (FingerprintException e) {
            e.printStackTrace();
        }

        if (initCipher()) {
            cryptoObject = new FingerprintManager.CryptoObject(cipher);
            FingerprintHandler helper = new FingerprintHandler(this);
            helper.startAuth(fingerprintManager, cryptoObject);
        }

    if (!getIntent().hasExtra(EXTRA_OPER)) {
        sendErr("operation is not defined");
        return;
    }
    oper = getIntent().getStringExtra(EXTRA_OPER);
    if (OPER_CREATE.equals(oper)) {
        msg.setText(R.string.create_code);
        if (!getIntent().hasExtra(EXTRA_SESSION)) {
            sendErr("session not found");
            return;
        }
        session = getIntent().getStringExtra(EXTRA_SESSION);
    } else if (OPER_VERIFY.equals(oper)) {
        msg.setText(R.string.type_code);
    }
    pin = new StringBuilder();
}

private void generateKey() throws FingerprintException {
    try {
        // Obtain a reference to the Keystore using the standard Android keystore container identifier (“AndroidKeystore”)//
        keyStore = KeyStore.getInstance("AndroidKeyStore");

        //Generate the key//
        keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");

        //Initialize an empty KeyStore//
        keyStore.load(null);

        //Initialize the KeyGenerator//
        keyGenerator.init(new

                //Specify the operation(s) this key can be used for//
                KeyGenParameterSpec.Builder(KEY_NAME,
                KeyProperties.PURPOSE_ENCRYPT |
                        KeyProperties.PURPOSE_DECRYPT)
                .setBlockModes(KeyProperties.BLOCK_MODE_CBC)

                //Configure this key so that the user has to confirm their identity with a fingerprint each time they want to use it//
                .setUserAuthenticationRequired(true)
                .setEncryptionPaddings(
                        KeyProperties.ENCRYPTION_PADDING_PKCS7)
                .build());

        //Generate the key//
        keyGenerator.generateKey();

    } catch (KeyStoreException
            | NoSuchAlgorithmException
            | NoSuchProviderException
            | InvalidAlgorithmParameterException
            | CertificateException
            | IOException exc) {
        exc.printStackTrace();
        throw new FingerprintException(exc);
    }
}

//Create a new method that we’ll use to initialize our cipher//
public boolean initCipher() {
    try {
        //Obtain a cipher instance and configure it with the properties required for fingerprint authentication//
        cipher = Cipher.getInstance(
                KeyProperties.KEY_ALGORITHM_AES + "/"
                        + KeyProperties.BLOCK_MODE_CBC + "/"
                        + KeyProperties.ENCRYPTION_PADDING_PKCS7);
    } catch (NoSuchAlgorithmException |
            NoSuchPaddingException e) {
        throw new RuntimeException("Failed to get Cipher", e);
    }

    try {
        keyStore.load(null);
        SecretKey key = (SecretKey) keyStore.getKey(KEY_NAME,
                null);
        cipher.init(Cipher.ENCRYPT_MODE, key);
        //Return true if the cipher has been initialized successfully//
        return true;
    } catch (KeyPermanentlyInvalidatedException e) {

        //Return false if cipher initialization failed//
        return false;
    } catch (KeyStoreException | CertificateException
            | UnrecoverableKeyException | IOException
            | NoSuchAlgorithmException | InvalidKeyException e) {
        throw new RuntimeException("Failed to init Cipher", e);
    }
}

private class FingerprintException extends Exception {
    public FingerprintException(Exception e) {
        super(e);
    }
}

private void next() {
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    if (OPER_CREATE.equals(oper)) {
        if (pin0 == null) {
            pin0 = pin.toString();
            pin = new StringBuilder();
            showPin();
            msg.setText(R.string.retype_code);
            return;
        }
        if (!pin0.equals(pin.toString())) {
            Toast.makeText(this, R.string.not_equal, Toast.LENGTH_LONG).show();
            pin0 = null;
            pin = new StringBuilder();
            showPin();
            msg.setText(R.string.create_code);
            return;
        }
        boolean b = new PinFacade(this).setupPin(pin.toString(), session);
        if (b) sendOk();
        else sendErr("can't setup pin");
    } else if (OPER_VERIFY.equals(oper)){
        try {
            session = new PinFacade(this).verifyPin(pin.toString());
            sendOk();
        } catch (Exception e) {
            e.printStackTrace();
            sendErr(e.getMessage());
        }
    }
}

private void sendOk() {
    setResult(RESULT_OK, new Intent().putExtra(EXTRA_SESSION, session));
    finish();
}

private void sendErr(String err) {
    setResult(RESULT_CANCELED, new Intent().putExtra(EXTRA_ERROR, err));
    finish();
}

}
1 Answers

I fixed my issue by checking if the phone has Fingerprint hardware, if the phone has fingerprints configured, if the app has fingerprint permissions and if the phone is locked.

    if (!fingerprintManager.isHardwareDetected()) {
        msg.setText("Your device doesn't support fingerprint authentication");
    }
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED) {
        msg.setText("Please enable the fingerprint permission");
    }
    if (!fingerprintManager.hasEnrolledFingerprints()) {
        msg.setText("No fingerprint configured. Please register at least one fingerprint in your device's Settings");
    }

    if (!keyguardManager.isKeyguardSecure()) {
        msg.setText("Please enable lockscreen security in your device's Settings");
    } else {
        try {
            generateKey();
        } catch (FingerprintException e) {
            e.printStackTrace();
        }

        if (initCipher()) {
            cryptoObject = new FingerprintManager.CryptoObject(cipher);
            helper = new FingerprintHandler(this);
            helper.startAuth(fingerprintManager, cryptoObject);
        }
    }
Related