PKCS#12 : DerInputStream.getLength() exception

纵饮孤独 提交于 2019-11-30 03:52:55

I had this problem and I've searched the depths of google and still couldn't find the answer. After some days battling with a terrible quality legacy code, I found what was causing this error.

KeyStore.load(InputStream is, String pass);

this method takes an InputStream and if there's any problem with such InputStream, this exception is thrown, some problems that I've encountered:

  • The InputStream points to the wrong / blank / just created file
  • The InputStream is already open or something else is holding the resource
  • The InputStream was already used and read, thus the position of the next byte of InputStream is it's end

The last one was the responsible for my problem. The code was creating an InputStream from a certificate, and proceeding to use it in two KeyStore.load() calls, the first one was successful, the second one always got me this error.

Probably the certificate you create has an extra character at the end which is misinterpreted to be another certificate. Use one or more blank lines at the end.

Refer: Java Certificate Parsing

Jan vO

For others with a similar problem:

"keystore load: DerInputStream.getLength(): lengthTag=109, too big."

For me solution was to remove the param: -storetype pkcs12 since the standard type is jks

Harikrishnan P.R

Specify the type of certificate in the code for eg:

System.setProperty("javax.net.ssl.trustStoreType", "jks");
System.setProperty("javax.net.ssl.keyStoreType", "pkcs12"); 

My issue (lengthTag=109, too big) was the .p12 file actually is JKS format and not PKCS # 12 format. Someone renamed the file extension. By regenerating in proper PKCS format resolved the issue.

java.io.IOException: DerInputStream.getLength(): lengthTag=109, too big.
    at sun.security.util.DerInputStream.getLength(DerInputStream.java:599)
    at sun.security.util.DerValue.init(DerValue.java:365)
    at sun.security.util.DerValue.<init>(DerValue.java:320)
    at sun.security.pkcs12.PKCS12KeyStore.engineLoad(PKCS12KeyStore.java:1914)
    at java.security.KeyStore.load(KeyStore.java:1445)

To check the format of a security file, may use KeyStore Explorer to open the file. The left bottom bar shows the actual format.

You are doing something wrong.
I tried your command and then loaded the p12 just fine.
The following code works:

 FileInputStream fin = new FileInputStream("..\\test.p12");
 KeyStore ks = KeyStore.getInstance("PKCS12");
 ks.load(fin, "123456".toCharArray());
 System.out.println(ks.getCertificate("myrsakey"));

I was wondering if you put the command as is you get an error from keytool that the password must be at least 6 characters.
You did not get that error? What version of java are you using?
Note:if you need to create certificates you can also look into this tool.
http://sourceforge.net/projects/certhelper/

sindacco

I had the same issue.

My solution is to replace PKCS12 with jceks in the line below because I was apparently using the wrong type.

KeyStore clientStore = KeyStore.getInstance("PKCS12");

This happened to me because I had copy and pasted the .p12 file locally on my windows 10 machine. No clue how/why this is a problem, but when I clone a project that has .p12 files and point my code to them, the files work. However, copy and pasting the files in windows file explorer to somewhere else on the harddrive causes this error!!!!

This happened to me in Android Studio after AndroidX migration and using the new testing framework. Even deleting the existing ~/.android/debug.keystore was failing for me

The solution was regenerate it manually (accept all questions as empty and say yes at the last one)

$ keytool -genkey -v -keystore debug.keystore -storepass android -alias androiddebugkey -keypass android -keyalg RSA -keysize 2048 -validity 10000

And copy it

$ rm ~/.android/debug.keystore
$ cp debug.keystore ~/.android/debug.keystore
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!