Android KeyGenerator returns ProviderException

575 views Asked by At
    Seen the below Exception when accessing to the device fingerprint sensor.
Noticed this issue in only one device
Model : vivo 1907
Product : 1907
Device : 1907
SDK Version : 31
Android Version : 12

Code Snippet :
 private boolean generateKey() {
        try {
            keyStore = KeyStore.getInstance("AndroidKeyStore");
        } catch (Exception e) {
            e.printStackTrace();
            LogManager.error(TAG + " :: " + " generateKey(): "
                    + " Exception - " + e.getMessage()
            );
            return  false;
        }


        KeyGenerator keyGenerator;
        try {
            keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
        }
        catch (NoSuchAlgorithmException | NoSuchProviderException e) {
            e.printStackTrace();
            LogManager.error(TAG + " :: " + " generateKey(): "
                    + " Error."
                    + " Setting key algorithm failed."
                    + " Exception - " + e.getMessage()
            );
            return false;

        }


        try {
            keyStore.load(null);
            keyGenerator.init(new
                    KeyGenParameterSpec.Builder(KEY_NAME,
                    KeyProperties.PURPOSE_ENCRYPT |
                            KeyProperties.PURPOSE_DECRYPT)
                    .setBlockModes(KeyProperties.BLOCK_MODE_CBC)
                    // Require the user to authenticate with a fingerprint to authorize every use
                    // of the key
                    .setUserAuthenticationRequired(true)
                    .setEncryptionPaddings(
                            KeyProperties.ENCRYPTION_PADDING_PKCS7)
                    .build());
            keyGenerator.generateKey();
        }
        catch (NoSuchAlgorithmException |
                InvalidAlgorithmParameterException
                | CertificateException | IOException e) {
            LogManager.error(TAG + " :: " + " generateKey(): "
                    + " Error."
                    + " Generating key failed."
                    + " Exception - " + e.getMessage()
            );

            return false;
        }

        return true;
    }

Exception - java.security.ProviderException: Keystore key generation failed at android.security.keystore2.AndroidKeyStoreKeyGeneratorSpi.engineGenerateKey(AndroidKeyStoreKeyGeneratorSpi.java:413) at javax.crypto.KeyGenerator.generateKey(KeyGenerator.java:612) at com.qbrik.fingerPrintManager.FingerprintService.generateKey(FingerprintService.java:442) at com.qbrik.fingerPrintManager.FingerprintService.initService(FingerprintService.java:296) at com.qbrik.shaktiapp.LoginPasscodeActivity.triggerFingerprintService(LoginPasscodeActivity.java:442) at com.qbrik.shaktiapp.LoginPasscodeActivity.onShaktiCreate(LoginPasscodeActivity.java:375) at com.qbrik.ShaktiBaseActivity.onCreate(ShaktiBaseActivity.java:338) at android.app.Activity.performCreate(Activity.java:8365) at android.app.Activity.performCreate(Activity.java:8343) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1379) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3912) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4096) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2441) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loopOnce(Looper.java:233) at android.os.Looper.loop(Looper.java:334) at android.app.ActivityThread.main(ActivityThread.java:8396) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:582) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1068) Caused by: android.security.KeyStoreException: Keystore not initialized at android.security.KeyStore2.getKeyStoreException(KeyStore2.java:332) at android.security.KeyStoreSecurityLevel.handleExceptions(KeyStoreSecurityLevel.java:57) at android.security.KeyStoreSecurityLevel.generateKey(KeyStoreSecurityLevel.java:145) at android.security.keystore2.AndroidKeyStoreKeyGeneratorSpi.engineGenerateKey(AndroidKeyStoreKeyGeneratorSpi.java:400) ... 22 more

0

There are 0 answers