PKCS#12:DerInputStream.getLength() 异常

发布于 2024-12-04 02:16:17 字数 474 浏览 0 评论 0原文

我使用 keytool 命令生成证书:

keytool -genkeypair -alias myRSAKey -keyalg RSA -keysize 1024 -keystore test.p12 -storepass test -storetype pkcs12

然后,如果我尝试使用 java security API 加载它,在将文件获取为 byte[] 后:

KeyStore ks = KeyStore.getInstance("PKCS12");
try{
   ks.load(new ByteArrayInputStream(data), "test".toCharArray())
} catch (Exception e){
   ...
}

我得到 DerInputStream.getLength(): lengthTag=127,太大的异常。

怎么了?

I generate a certificate using the keytool command:

keytool -genkeypair -alias myRSAKey -keyalg RSA -keysize 1024 -keystore test.p12 -storepass test -storetype pkcs12

Then if I try to load it using java security API, after getting the file as a byte[] :

KeyStore ks = KeyStore.getInstance("PKCS12");
try{
   ks.load(new ByteArrayInputStream(data), "test".toCharArray())
} catch (Exception e){
   ...
}

I get a DerInputStream.getLength(): lengthTag=127, too big exception.

What is wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

孤单情人 2024-12-11 02:16:17

我遇到了这个问题,我已经搜索了谷歌的深处,但仍然找不到答案。经过几天与质量糟糕的遗留代码的斗争后,我找到了导致此错误的原因。

KeyStore.load(InputStream is, String pass);

这个方法接受一个I​​nputStream,如果这样的InputStream有任何问题,就会抛出这个异常,我遇到过一些问题:

  • InputStream指向错误/空白/刚刚创建的文件
  • InputStream已经打开或其他东西持有资源
  • InputStream 已被使用并读取,因此 InputStream 的下一个字节的位置是它的末尾

最后一个是造成我的问题的原因。该代码从证书创建一个 InputStream,并继续在两个 KeyStore.load() 调用中使用它,第一个成功,第二个总是给我带来这个错误。

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.

枉心 2024-12-11 02:16:17

对于其他有类似问题的人:

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

对我来说,解决方案是删除参数:-storetype pkcs12
因为标准类型是 jks

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

浅唱ヾ落雨殇 2024-12-11 02:16:17

您创建的证书可能在末尾有一个额外的字符,该字符被误解为另一个证书。
在末尾使用一个或多个空行。

参考:Java证书解析

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

吃素的狼 2024-12-11 02:16:17

我的问题(lengthTag=109,太大)是 .p12 文件实际上是 JKS 格式,而不是 PKCS # 12 格式。有人重命名了文件扩展名。通过以正确的 PKCS 格式重新生成解决了该问题。

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)

要检查安全文件的格式,可以使用 KeyStore Explorer 打开该文件。左下栏显示实际格式。

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.

牛↙奶布丁 2024-12-11 02:16:17

在代码中指定证书类型
例如:

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

Specify the type of certificate in the code
for eg:

System.setProperty("javax.net.ssl.trustStoreType", "jks");
System.setProperty("javax.net.ssl.keyStoreType", "pkcs12"); 
甩你一脸翔 2024-12-11 02:16:17

在 AndroidX 迁移并使用新的测试框架后,我在 Android Studio 中发生了这种情况。即使删除现有的 ~/.android/debug.keystore 对我来说也失败了

解决方案是手动重新生成它(接受所有问题为空并在最后一个问题上说“是”)

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

并复制它

$ rm ~/.android/debug.keystore
$ cp debug.keystore ~/.android/debug.keystore

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
财迷小姐 2024-12-11 02:16:17

这发生在我身上,因为我已在 Windows 10 计算机上本地复制并粘贴了 .p12 文件。不知道如何/为什么这是一个问题,但是当我克隆一个具有 .p12 文件的项目并将我的代码指向它们时,这些文件可以工作。但是,将 Windows 文件资源管理器中的文件复制并粘贴到硬盘驱动器上的其他位置会导致此错误!!!!

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!!!!

野生奥特曼 2024-12-11 02:16:17

我有同样的问题。

我的解决方案是在下面的行中将 PKCS12 替换为 jceks,因为我显然使用了错误的类型。

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

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");
酒解孤独 2024-12-11 02:16:17

你做错了什么。
我尝试了你的命令,然后加载了 p12 就好了。
以下代码有效:

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

我想知道如果您按原样输入命令,您会从 keytool 收到错误消息,密码必须至少为 6 个字符。
您没有收到该错误吗?您使用什么版本的java?
注意:如果您需要创建证书,也可以查看此工具。
http://sourceforge.net/projects/certhelper/

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/

梨涡 2024-12-11 02:16:17

确保 inputstream 变量的范围仅限于声明它的方法,而不是静态/类变量。这样可以避免此异常。
原因:当输入流被声明为类变量时,在第一次加载证书或数据后,输入流不会关闭。因此,使其仅可用于方法。

Make sure the scope of the inputstream variable is only to the method where you’re declaring it but not as static/class variable.This way this exception can be avoided.
Reason : Inputstream is not getting closed after the first time of loading certificate or data in it while it is declared as class variable.so make it available only to method.

蓝海 2024-12-11 02:16:17

这发生在我身上,因为以下命令:

openssl pkcs12 -export -in import.pem -inkey myhost.key.pem -name shared > server.p12 (来自 https://docs.oracle.com/en/database/other-databases/nosql-database/12.2.4.5/security/import-key-pair-java-keystore.html

生成了错误的格式化的 pkcs12 文件。使用以下命令纠正了问题:

openssl pkcs12 -export -in import.pem -inkey myhost.key.pem -name shared -out server.p12

This happened to me because the following command:

openssl pkcs12 -export -in import.pem -inkey myhost.key.pem -name shared > server.p12 (from https://docs.oracle.com/en/database/other-databases/nosql-database/12.2.4.5/security/import-key-pair-java-keystore.html)

generated a wrongly formatted pkcs12 file. Using the following corrected the problem:

openssl pkcs12 -export -in import.pem -inkey myhost.key.pem -name shared -out server.p12

或十年 2024-12-11 02:16:17

这个错误有多种原因......日志可能真的很混乱。

主要原因之一可能是 Maven 过滤。
根据 maven 官方文档
警告:不要过滤包含图像等二进制内容的文件!这很可能会导致输出损坏。
我们的 .jks 在打包阶段被 Maven 损坏了。

这个帖子帮助我弄清楚了。

我们可以直接在相关的 pom.xml 中从过滤中排除一些目录或文件扩展名:

       <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>${maven-resources-plugin.version}</version>
            <configuration>
                <nonFilteredFileExtensions>
                    <nonFilteredFileExtension>jks</nonFilteredFileExtension>
                    <nonFilteredFileExtension>p12</nonFilteredFileExtension>
                </nonFilteredFileExtensions>
            </configuration>
        </plugin>

This error has multpile causes... The log can be realy confusing.

One main cause can be maven filtering.
According to maven official documentation
Warning: Do not filter files with binary content like images! This will most likely result in corrupt output.
Our .jks was corrupted by maven during packaging stage.

This thread helped me to figure it out.

We can exclude some directories or file extensions from filtering directly in concerned pom.xml :

       <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>${maven-resources-plugin.version}</version>
            <configuration>
                <nonFilteredFileExtensions>
                    <nonFilteredFileExtension>jks</nonFilteredFileExtension>
                    <nonFilteredFileExtension>p12</nonFilteredFileExtension>
                </nonFilteredFileExtensions>
            </configuration>
        </plugin>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文