【问题标题】:Bouncycastle: CertificateFactory.generateCertificate gives "sequence wrong size for a certificate" on one machine but not on anotherBouncycastle:CertificateFactory.generateCertificate 在一台机器上给出“证书的序列错误大小”,但在另一台机器上没有
【发布时间】:2011-02-23 15:20:45
【问题描述】:

我正在尝试打开一个 p7b 文件并从中读取 CA 证书。下面是我的代码。它在一台机器上工作正常,但在另一台机器上调用 certFactory.generateCertificate 抛出异常

Error Message:java.lang.IllegalArgumentException: sequence wrong size for a certificate

在两台机器上我都有相同的 p7b 文件和相同的 bouncycastle jars。可以工作的机器是 Windows Xp,不工作的机器是 Windows 2007 服务器机器。它是 64 位机器,但我只使用 32 位 jvm。

    CertificateFactory certFactory = CertificateFactory.getInstance("X.509",
            new BouncyCastleProvider());
    java.security.cert.Certificate cert = null;
    FileInputStream inStream = new FileInputStream("");
    ArrayList<java.security.cert.Certificate> certificates = new ArrayList<java.security.cert.Certificate>();
    CAService caService = null;
    caService.getCertificateAuthority().setCaCerts(new ArrayList<String>());

    while ((cert = certFactory.generateCertificate(inStream)) != null)
    {
        certificates.add(cert);
        StringWriter swrtr = new StringWriter();
        PEMWriter writer = new PEMWriter(swrtr);
        writer.writeObject(cert);
        writer.flush();
        caService.getCertificateAuthority().getCaCerts().add(swrtr.toString());
    }

我什至编写了一个独立的程序,甚至在运行时明确指定要使用的 java.exe,但仅在那台机器上我就面临同样的异常。

c:\jdk1.5.0_14\jre\bin\java.exe -classpath .;bcprov-jdk15-143.jar MSCAConfigurator

Exception in thread "main" java.security.cert.CertificateException: java.lang.IllegalArgumentException: sequence wrong size for a certificate
        at org.bouncycastle.jce.provider.JDKX509CertificateFactory.engineGenerateCertificate(Unknown Source)
        at java.security.cert.CertificateFactory.generateCertificate(CertificateFactory.java:271)
        at MSCAConfigurator.main(MSCAConfigurator.java:31)
Caused by: java.lang.IllegalArgumentException: sequence wrong size for a certificate
        at org.bouncycastle.asn1.x509.X509CertificateStructure.<init>(Unknown Source)
        at org.bouncycastle.asn1.x509.X509CertificateStructure.getInstance(Unknown Source)
        at org.bouncycastle.jce.provider.JDKX509CertificateFactory.readPEMCertificate(Unknown Source)

我有无限强度的政策罐子。

C:\jdk1.5.0_14\jre\lib\security>dir *.jar

驱动器 C 中的卷没有标签。

卷序列号为 D214-CB94

C目录:\jdk1.5.0_14\jre\lib\security

09/13/2004 04:12 PM 2,486 local_policy.jar

09/13/2004 04:12 PM 2,472 US_export_policy.jar

这台机器有什么问题?在我开枪之前请有人帮忙。

【问题讨论】:

    标签: certificate bouncycastle


    【解决方案1】:
    【解决方案2】:

    根据 javadocs,CertificateFactory.generateCertificates() 支持 PKCS#7 格式,但 CertificateFactory.generateCertificate() 不支持。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-06
      • 2016-02-19
      • 2018-09-18
      • 2016-09-28
      • 2015-11-08
      • 1970-01-01
      相关资源
      最近更新 更多