Android 10 android.security.keymaster.ExportResult.resultCode NullPointerException crash
Asked Answered
S

1

13

My app is getting single crashes reports from Android 10 users only. This is the stacktrace:

Fatal Exception: java.lang.NullPointerException: Attempt to read from field 'int android.security.keymaster.ExportResult.resultCode' on a null object reference

       at android.security.keystore.AndroidKeyStoreProvider.loadAndroidKeyStorePublicKeyFromKeystore(AndroidKeyStoreProvider.java:256)
       at android.security.keystore.AndroidKeyStoreProvider.loadAndroidKeyStoreKeyPairFromKeystore(AndroidKeyStoreProvider.java:296)
       at android.security.keystore.AndroidKeyStoreProvider.loadAndroidKeyStorePrivateKeyFromKeystore(AndroidKeyStoreProvider.java:316)
       at android.security.keystore.AndroidKeyStoreProvider.loadAndroidKeyStoreKeyFromKeystore(AndroidKeyStoreProvider.java:378)
       at android.security.keystore.AndroidKeyStoreSpi.engineGetKey(AndroidKeyStoreSpi.java:105)
       at java.security.KeyStore.getKey(KeyStore.java:1062)
       at we.lmk.iqQ(we.lmk:19)
       at we.Vmk.apply(we.Vmk:51)
       at io.reactivex.internal.operators.single.SingleMap$MapSingleObserver.onSuccess(SingleMap.java:57)
       at io.reactivex.internal.operators.single.SingleFromCallable.subscribeActual(SingleFromCallable.java:56)
       at io.reactivex.Single.subscribe(Single.java:3603)
       at io.reactivex.internal.operators.single.SingleMap.subscribeActual(SingleMap.java:34)
       at io.reactivex.Single.subscribe(Single.java:3603)
       at io.reactivex.internal.operators.single.SingleZipArray.subscribeActual(SingleZipArray.java:63)
       at io.reactivex.Single.subscribe(Single.java:3603)
       at io.reactivex.internal.operators.maybe.MaybeFilterSingle.subscribeActual(MaybeFilterSingle.java:40)
       at io.reactivex.Maybe.subscribe(Maybe.java:4290)
       at io.reactivex.internal.operators.maybe.MaybeMap.subscribeActual(MaybeMap.java:40)
       at io.reactivex.Maybe.subscribe(Maybe.java:4290)
       at io.reactivex.internal.operators.maybe.MaybeMap.subscribeActual(MaybeMap.java:40)
       at io.reactivex.Maybe.subscribe(Maybe.java:4290)
       at io.reactivex.internal.operators.maybe.MaybeFlatMapSingle.subscribeActual(MaybeFlatMapSingle.java:47)
       at io.reactivex.Single.subscribe(Single.java:3603)
       at io.reactivex.internal.operators.single.SingleFlatMap.subscribeActual(SingleFlatMap.java:36)
       at io.reactivex.Single.subscribe(Single.java:3603)
       at io.reactivex.internal.operators.single.SingleFlatMap.subscribeActual(SingleFlatMap.java:36)
       at io.reactivex.Single.subscribe(Single.java:3603)
       at io.reactivex.internal.operators.single.SingleSubscribeOn$SubscribeOnObserver.run(SingleSubscribeOn.java:89)
       at io.reactivex.Scheduler$DisposeTask.run(Scheduler.java:578)
       at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:66)
       at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:57)
       at java.util.concurrent.FutureTask.run(FutureTask.java:266)
       at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301)
       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
       at java.lang.Thread.run(Thread.java:919)

And this is my code:

@NonNull
private static KeyStore getKeyStoreInstance() throws SecureStorageException {
    try {
        // Get the AndroidKeyStore instance
        KeyStore keyStore = KeyStore.getInstance(KEY_KEYSTORE_NAME);

        // Relict of the JCA API - you have to call load even
        // if you do not have an input stream you want to load or it'll crash
        keyStore.load(null);

        return keyStore;
    } catch (Exception e) {
        throw new SecureStorageException(e.getMessage(), e, KEYSTORE_EXCEPTION);
    }
}

This code is provided by this library: https://github.com/adorsys/secure-storage-android

I didn't find any solution yet, is it possible that this is internal Android 10 bug?

Sledgehammer answered 19/12, 2019 at 6:26 Comment(0)
J
0

I had gotten the same error and had solved it with surrounding code with try/catch and recursive call method after sleep timeout:

private int getDataAttemptNumber = 0;
public byte[] getData(String key) {
    KeyStore ks = null;
    try {
        ks = KeyStore.getInstance(KEYSTORE_PROVIDER_ANDROID_KEYSTORE);
        ks.load(null);
        PrivateKey privateKey = (PrivateKey) ks.getKey(alias, null);
        return decrypt(privateKey, preferences.getString(key, null));
    } catch (NullPointerException e) {
        getDataAttemptNumber++;
        if (getDataAttemptNumber > 10) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException ex) {
                // nothing to do
            }
            getDataAttemptNumber = 0;
            return null;
        }
        return getData(key);
    } catch (Exception e) {
        try {
            if (ks != null)
                ks.deleteEntry(alias);
        } catch (Exception e1) {
            // Just ignore any errors here
        }
    }
    return null;
}
Jasperjaspers answered 21/3, 2023 at 6:32 Comment(1)
It is not working some times...Jasperjaspers

© 2022 - 2024 — McMap. All rights reserved.