Getting Wrong version of key store. in android

732 Views Asked by At

I am making a keystore get registered using a jks file but getting "Wrong version of key store." with the below code

TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());

        InputStream keyStoreStream=mContext.getResources().openRawResource(R.raw.ssokeystore);
        keyStore.load(keyStoreStream, "<<Password>>".toCharArray());//Getting the error here even giving the right password

        trustManagerFactory.init(keyStore);
        TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, trustManagers, null);
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

What could be the reason for it, My main use case is to allow any https calls made from the application by registering the certificate

Here is the stacktrace

W/System.err: java.io.IOException: Wrong version of key store.
W/System.err:     at com.android.org.bouncycastle.jcajce.provider.keystore.bc.BcKeyStoreSpi.engineLoad(BcKeyStoreSpi.java:815)
W/System.err:     at java.security.KeyStore.load(KeyStore.java:1484)
W/System.err:     at net.openid.appauthdemo.TrustStoreConfiguration.ssoTtrustStore(TrustStoreConfiguration.java:32)
W/System.err:     at net.openid.appauthdemo.LoginActivity.onCreate(LoginActivity.java:117)
W/System.err:     at android.app.Activity.performCreate(Activity.java:7224)
W/System.err:     at android.app.Activity.performCreate(Activity.java:7213)
W/System.err:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1272)
W/System.err:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2926)
W/System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3081)
W/System.err:     at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
W/System.err:     at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
W/System.err:     at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1831)
W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:106)
W/System.err:     at android.os.Looper.loop(Looper.java:201)
W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6810)
W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
W/System.err:     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)

2

There are 2 best solutions below

0
On

What type is your KeyStore, and what OS version is it crashing on? According to the KeyStore documentation, the following types are supported on the Android platform:

KeyStore type support

0
On

Easiest way to solve (guaranteed to work on Android, as of Oct 2021) is to switch from your existing Java KeyStore to BouncyCastle KeyStore (BKS).

Open your KeyStore file, and then use KeyStore Explorer (downloadable executable for Mac and Windows available online) to switch to BKS.