yakivmospan / scytale

One tool to manage key generation, key storing and encryption on different APIs of Android.
432 stars 45 forks source link

E/Scytale: java.security.KeyStoreException: KeyStore was not initialized #10

Closed sergiobr closed 6 years ago

sergiobr commented 7 years ago

Hi, I'm getting this error on my app using scytale. The app continues working fine, but I would like to know how to fix this error?

yakivmospan commented 7 years ago

Hey, can you please provide some more details on this? Like what version of Scytale are you using, what yours Android API version is?

Also can you please try to set an ErrorListener and see more details about your error ?

To setup an error listener use :

crypto.setErrorListener(new ErrorListener() {
     @Override
      public void onError(Exception e) {
         // handle error
      }
});

store.setErrorListener(new ErrorListener() {
    @Override
     public void onError(Exception e) {
         // handle error
     }
});

Thanks, Yakiv Mospan

yakivmospan commented 7 years ago

And if possible, can you please provide a code snippet you are using ? Probably you are trying to get a key that is not saved to Storeyet.

sergiobr commented 7 years ago

Hello, The Scytale version is 1.0.1 and Android APIs where 22 and 23. The code snippet:

store = new Store(context);
if (!store.hasKey(KEY_ALIAS)) {
    SecretKey key = store.generateSymmetricKey(KEY_ALIAS, null);
 }

is called when the key is needed. The error listener result:

07-10 16:41:49.341 27858-27858/ V/createKeys: +++ error store: KeyStore was not initialized
07-10 16:41:49.342 27858-27858/ W/System.err: java.security.KeyStoreException: KeyStore was not initialized
07-10 16:41:49.346 27858-27858/ W/System.err:     at java.security.KeyStore.throwNotInitialized(KeyStore.java:96)
07-10 16:41:49.346 27858-27858/ W/System.err:     at java.security.KeyStore.isKeyEntry(KeyStore.java:471)
07-10 16:41:49.346 27858-27858/ W/System.err:     at com.yakivmospan.scytale.Store.isKeyEntry(Store.java:254)
07-10 16:41:49.346 27858-27858/ W/System.err:     at com.yakivmospan.scytale.Store.hasKey(Store.java:215)

After that I receive the error "E/Scytale: java.io.IOException" when I try to decrypt encrypted values using:

SecretKey key = store.getSymmetricKey(KEY_ALIAS, null);
com.yakivmospan.scytale.Crypto crypto = new com.yakivmospan.scytale.Crypto(Options.TRANSFORMATION_SYMMETRIC);
String decryptedData = crypto.decrypt(input, key);

Error stack:

07-10 17:27:02.524 12652-12652/ E/Scytale: java.io.IOException
07-10 17:27:02.525 12652-12652/ V/createKeys: +++ error store: null
07-10 17:27:02.530 12652-12652/W/System.err: java.io.IOException
07-10 17:27:02.547 12652-12652/ W/System.err:     at javax.crypto.CipherOutputStream.close(CipherOutputStream.java:136)
07-10 17:27:02.560 12652-12652/ W/System.err:     at com.yakivmospan.scytale.Crypto.decode(Crypto.java:188)
07-10 17:27:02.572 12652-12652/ W/System.err:     at com.yakivmospan.scytale.Crypto.decrypt(Crypto.java:172)
07-10 17:27:02.585 12652-12652/ W/System.err:     at com.yakivmospan.scytale.Crypto.decrypt(Crypto.java:139)
07-10 17:27:02.598 12652-12652/ W/System.err:     at .u.Class3.decrypt(Class3.java:194)
07-10 17:27:02.612 12652-12652/ W/System.err:     at .h.Helper.getEncrypted(Helper.java:57)
07-10 17:27:02.625 12652-12652/ W/System.err:     at .a.Activity1.method1(BaseActivity.java:26)
07-10 17:27:02.639 12652-12652/ W/System.err:     at .a.Activity2.onCreate(UseActivity.java:172)
07-10 17:27:02.652 12652-12652/ W/System.err:     at android.app.Activity.performCreate(Activity.java:6259)
07-10 17:27:02.666 12652-12652/ W/System.err:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130)
07-10 17:27:02.681 12652-12652/ W/System.err:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
07-10 17:27:02.695 12652-12652/ W/System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
07-10 17:27:02.706 12652-12652/ W/System.err:     at android.app.ActivityThread.-wrap11(ActivityThread.java)
07-10 17:27:02.720 12652-12652/ W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
07-10 17:27:02.732 12652-12652/ W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
07-10 17:27:02.743 12652-12652/ W/System.err:     at android.os.Looper.loop(Looper.java:148)
07-10 17:27:02.756 12652-12652/ W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5443)
07-10 17:27:02.766 12652-12652/ W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
07-10 17:27:02.781 12652-12652/ W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
07-10 17:27:02.794 12652-12652/ W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
yakivmospan commented 7 years ago

Thats so strange, I'm using the same code snippet on API 23 and all is working. Also was running different tests on different API's including hasKey test, and they are passing.

Was trying to pass null as KEY_ALIAS, and got crash, but not the same as you. This is a snippet i was trying to run:

    @Test
    public void crashTest() {
        Store store = new Store(context);
        store.setErrorListener(new ErrorListener() {
            @Override
            public void onError(Exception e) {
                e.printStackTrace();
            }
        });

        if (!store.hasKey(KEY_ALIAS_SYMMETRIC)) {
            SecretKey key = store.generateSymmetricKey(KEY_ALIAS_SYMMETRIC, null);
            assertThat(key != null, is(true));
        }
    }

Are you running this on device or emulator? Can this be specific issue for some vendor ?

Thanks, Yakiv

yakivmospan commented 7 years ago

I've created the case, this can happen if we forgot to load() Keystore before usage. The problem is that I'm loading it - https://github.com/yakivmospan/scytale/blob/develop/library/src/main/java/com/yakivmospan/scytale/Store.java#L551, after removing this line, I'm getting the same crash as you do, with my code snippet from above.

07-19 16:28:10.846 16984-16997/? E/Scytale: java.security.KeyStoreException: KeyStore was not initialized
                                                at java.security.KeyStore.throwNotInitialized(KeyStore.java:96)
                                                at java.security.KeyStore.isKeyEntry(KeyStore.java:471)
                                                at com.yakivmospan.scytale.Store.isKeyEntry(Store.java:254)
                                                at com.yakivmospan.scytale.Store.hasKey(Store.java:215)
                                                at com.yakivmospan.scytale.SymmetricKeyTest.crashTest(SymmetricKeyTest.java:146)
                                                at java.lang.reflect.Method.invoke(Native Method)
                                                at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
                                                at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
                                                at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
                                                at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
                                                at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
                                                at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
                                                at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
                                                at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
                                                at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
                                                at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
                                                at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
                                                at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
                                                at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
                                                at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
                                                at org.junit.runners.Suite.runChild(Suite.java:128)
                                                at org.junit.runners.Suite.runChild(Suite.java:27)
                                                at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
                                                at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
                                                at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
                                                at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
                                                at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
                                                at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
                                                at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
                                                at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
                                                at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:59)
                                                at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:262)
                                                at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1879)

Again, maybe this is not working for some specific device.

yakivmospan commented 7 years ago

Can you please just try to run this simple sample from android developer docs :

/*
 * Load the Android KeyStore instance using the the
 * "AndroidKeyStore" provider to list out what entries are
 * currently stored.
 */
KeyStore ks = KeyStore.getInstance("AndroidKeyStore");
ks.load(null);
Enumeration<String> aliases = ks.aliases();

If this is something with device, you shell get keystore not initialized error on last line.

drawers commented 7 years ago

This sounds like an issue with the vendor implementation of the keystore on the device itself rather than a problem with the library. It would be good to report this if we know which phone it affects. Please note I found the following issue with the Nexus 5X implementation reported here:

https://issuetracker.google.com/issues/62500127

yakivmospan commented 7 years ago

@drawers Thank you for your contribution 👍