java.lang.IllegalArgumentException:错误的序列大小

时间:2014-12-12 05:12:48

标签: java ssl x509certificate cer

我是新的证书,我有一个场景,需要阅读SSL证书,解压缩并验证证书中指定的电子邮件。因为我写了下面的代码,但我得到java.lang.IllegalArgumentException

public GenericFormResponse execute(WebRequest wreq, String epName, String ipAddr, boolean useDefault, MultipartFile certFile)throws Exception {
.......//some code
byte[] certBytes = certFile.getBytes();
CertificateFactory cf = CertificateFactory.getInstance("X509");
X509Certificate cert = (X509Certificate) cf.generateCertificate(new ByteArrayInputStream(certBytes));
NameAdapter subject = CertificateVerifier.getSubject(cert);
if(StringUtils.equalsIgnoreCase(subject.getEmailAddress(), email)){
    ep.setCertData(cert.getSignature());
}else{
    LOGGER.debug("invalid certificates found.");
    response.setSuccess(false);
    response.setGlobalErrorCode("sa_endpoint_invalid_cert");
    return response;
}
......//some code.
}

CertificateVerifier.getSubject(cert);是在另一个场景中正常运行的自定义代码。 异常堆栈跟踪:

Caused by: java.lang.IllegalArgumentException: Bad sequence size: 6
        at org.bouncycastle.asn1.x509.AlgorithmIdentifier.<init>(Unknown Source) ~[bcprov-jdk15on-1.51.jar:1.51.0]
        at org.bouncycastle.asn1.x509.AlgorithmIdentifier.getInstance(Unknown Source) ~[bcprov-jdk15on-1.51.jar:1.51.0]
        at org.bouncycastle.asn1.x509.TBSCertificate.<init>(Unknown Source) ~[bcprov-jdk15on-1.51.jar:1.51.0]
        at org.bouncycastle.asn1.x509.TBSCertificate.getInstance(Unknown Source) ~[bcprov-jdk15on-1.51.jar:1.51.0]
        at org.bouncycastle.asn1.x509.Certificate.<init>(Unknown Source) ~[bcprov-jdk15on-1.51.jar:1.51.0]
        at org.bouncycastle.asn1.x509.Certificate.getInstance(Unknown Source) ~[bcprov-jdk15on-1.51.jar:1.51.0]
        at org.bouncycastle.jcajce.provider.asymmetric.x509.CertificateFactory.readPEMCertificate(Unknown Source) ~[bcprov-jdk15on-1.51.jar:1.51.0]
        ... 43 common frames omitted

请帮助任何人,如何阅读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ICATE-----

提前致谢。

2 个答案:

答案 0 :(得分:0)

X509缺少点使用 X.509

CertificateFactory cf = CertificateFactory.getInstance("X.509")

答案 1 :(得分:0)

首先,我很困惑。你说你需要阅读SSL证书。但是您正在读取包含“证书签名请求”的.csr文件。这不是证书。从这个请求中,可以读取公钥,有效期,密钥用法并使用所有这些来创建证书。为此,请按照步骤进行操作。

  1. 将文件数据(字节数组)转换为字符串。
  2. 使用String的replaceAll方法删除以下行(例如csrString.replaceAll(“....”))  
    第1行:-----开始证书-----  
    第2行:----- END CERTIFICATE -----
  3. 将数据转换为十六进制。 Java 7内置了库
  4. javax.xml.bind.DatatypeConverter.parseBase64Binary( “...”)

    1. 使用以下方法获取PKCS10格式数据
    2. PKCS10CertificationRequest pkcs10CertificationRequest = new PKCS10CertificationRequest(csrData);

      1. 现在从pkcs10CertificationRequest读取数据并创建X509Certificate
      2. 有关更多信息,请阅读RFC。 https://tools.ietf.org/html/rfc2986