java.security.KeyStoreException: Uninitialized keystore
at java.security.KeyStore.entryInstanceOf(KeyStore.java:1690)
at com.my.package.MyKeyManager.getSecretKey(MyKeyManager.java:137)
Why am I getting the above uninitialized keystore exception when executing the following code (Samsung S9 running Android 9):
import java.security.KeyStore;
...
SecretKey getSecretKey(String alias) {
mKeyStore = KeyStore.getInstance("AndroidKeyStore");
mKeyStore.load(null);
if (mKeyStore.containsAlias(alias)
&& mKeyStore.entryInstanceOf(alias, KeyStore.SecretKeyEntry.class)) {
}
}
Here's the really weird part - If you look at the Android source code for containsAlias which I call right before entryInstanceOf, it also has the same check for !initialized. Yet somehow the check in containsAlias passes and the check in entryInstanceOf fails.
Also note that the exception is being thrown on line 1690 of the java.security.KeyStore file, but that line has never thrown that exception in the entire history of the AOSP (I checked the commit file history for that file). This leads me to believe that Samsung must be modifying this file for their flavor of Android.
Does anyone know what's going on here or if there is a way to view the Samsung Android java source files? I found opensource.samsung.com, but none of the source downloads include any java files (only c files).