javaandroidkeytooljava-security

toDerInputStream rejects tag type 0 - Cannot read key from keystore


In my Android app, I have a keystore file named keystore.p12, which is located in /data/data/com.company.myapp/files. I pulled this particular file and now I want to retrieve the key stored inside it.

I tried it with the keytool like this:

>keytool.exe -list -keystore C:\Users\user\Desktop\keystore.p12 -storepass letmein -storetype PKCS12 -v

Unfortunately, I get the following error:

keytool error: java.io.IOException: toDerInputStream rejects tag type 0
java.io.IOException: toDerInputStream rejects tag type 0
        at sun.security.util.DerValue.toDerInputStream(DerValue.java:874)
        at sun.security.pkcs12.PKCS12KeyStore.engineLoad(PKCS12KeyStore.java:1915)
        at java.security.KeyStore.load(KeyStore.java:1445)
        at sun.security.tools.keytool.Main.doCommands(Main.java:795)
        at sun.security.tools.keytool.Main.run(Main.java:343)
        at sun.security.tools.keytool.Main.main(Main.java:336)

I tried the same thing with Java, but I experience the same exception:

public static void main(String[] args) {
        try {
            FileInputStream is = new FileInputStream("C:\\Users\\user\\Desktop\\keystore.p12");
            KeyStore keystore = KeyStore.getInstance("pkcs12");
            keystore.load(is, "letmein".toCharArray());
            is.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

Any idea how I can solve this issue?


Solution

  • It looks like your .p12 file is not a PKCS#12 file but a different key store type. I would try to open it with the Keystore Explorer which tries to open the file using every available key store type (PKCS12, BKS, JKS, ...).

    If it can load the file you will see in the file info at the bottom of the main widow for which file type loading succeeded.