Java X509证书解析和验证

Dri*_*mri 12 java validation jce pki x509certificate

我试图通过几个步骤处理X509证书并遇到几个问题.我是JCE的新手,所以我还没有完全了解所有内容.

我们希望能够根据不同的编码(PEM,DER和PCKS7)解析几个不同的X509证书.我使用FireFox(证书包括链)以PEM和PCKS7格式从https://belgium.be导出相同的证书.我已经离开了一些问题所不需要的线路

public List<X509Certificate> parse(FileInputStream fis) {  
    /*
     * Generate a X509 Certificate initialized with the data read from the inputstream. 
     * NOTE: Generation fails when using BufferedInputStream on PKCS7 certificates.
     */
    List<X509Certificate> certificates = null;
      log.debug("Parsing new certificate.");
      certificates = (List<X509Certificate>) cf.generateCertificates(fis);
    return certificates;
  }
Run Code Online (Sandbox Code Playgroud)

只要我使用FileInputStream而不是PCKS7的BufferedInputStream,这段代码工作正常,我觉得这很奇怪?但我可以忍受它.

下一步是验证这些证书链.1)检查所有证书是否都有有效日期(简单)2)使用OCSP验证证书链(如果证书中未找到OCSP URL,则回退到CRL).这是我不完全确定如何处理这个问题的地方.

我正在使用Sun JCE,但似乎没有那么多文档可用(在示例中)?

我首先做了一个简单的实现,只检查链而不经过OCSP/CRL检查.

private Boolean validateChain(List<X509Certificate> certificates) {
    PKIXParameters params;
    CertPath certPath;
    CertPathValidator certPathValidator;
    Boolean valid = Boolean.FALSE;

    params = new PKIXParameters(keyStore);
    params.setRevocationEnabled(false);

    certPath = cf.generateCertPath(certificates);
    certPathValidator = CertPathValidator.getInstance("PKIX");

    PKIXCertPathValidatorResult result = (PKIXCertPathValidatorResult)  
    certPathValidator.validate(certPath, params);

      if(null != result) {
        valid = Boolean.TRUE;
      }
    return valid;
 }
Run Code Online (Sandbox Code Playgroud)

这适用于我的PEM证书,但不适用于PCKS7证书(相同的证书,仅以其他格式导出). java.security.cert.CertPathValidatorException:Path不与任何信任锚链接.

我能看到的唯一区别是CertPath的形成顺序是不一样的?我无法弄清楚出了什么问题所以我现在离开了这个并继续使用PEM证书,但我们可以打电话给这个问题1;)

之后我想要实现的是OCSP检查.显然,如果我使用以下命令启用OCSP: Security.setProperty("ocsp.enable","true"); 并设置 params.setRevocationEnabled(true); 它应该能够自己找到OCSP URL,但似乎并非如此.应该做的标准实现是什么(问题2)? java.security.cert.CertPathValidatorException:必须指定OCSP Responder的位置

通过这个,我发现了一种使用AuthorityInfoAccessExtension等从证书中检索OCSP URL的方法.

但是在ocsp.url属性中手动设置OCSP url之后,我收到了java.security.cert.CertPathValidatorException:OCSP响应错误:UNAUTHORIZED

看起来我错过了很多必要的步骤,而很多在线参考说明设置ocsp.enable属性应该是你需要做的所有事情吗?

也许你们中的任何一个人都不能指导我完成这个过程吗?告诉我我完全错了:)

如果没有找到OCSP,下一步将是实施CRL检查,如果有人可以指出任何示例或向我展示一些文档,也将非常感谢!

谢谢!

编辑: 由于它没有自己拾取属性,我一直在尝试使用以下方法自行设置所有属性:

    // Activate OCSP
        Security.setProperty("ocsp.enable", "true");
        // Activate CRLDP -- no idea what this is
        Security.setProperty("com.sun.security.enableCRLDP", "true");

        X509Certificate target = (X509Certificate) certPath.getCertificates().get(0);
        Security.setProperty("ocsp.responderURL","http://ocsp.pki.belgium.be/");
        Security.setProperty("ocsp.responderCertIssuerName", target.getIssuerX500Principal().getName());
        Security.setProperty("ocsp.responderCertSubjectName", target.getSubjectX500Principal().getName());
        Security.setProperty("ocsp.responderCertSerialNumber", target.getSerialNumber().toString(16));
Run Code Online (Sandbox Code Playgroud)

这给出了一个例外:java.security.cert.CertPathValidatorException:找不到响应者的证书(使用OCSP安全属性设置).

Dri*_*mri 15

为了将来的参考,我会将答案发布到我自己的问题上(部分至少)

OCSP和CRL检查已在标准Java实现中实现,不需要自定义代码或其他提供程序(BC,..).它们默认是禁用的.

要启用此功能,您必须至少设置两个参数:

(PKIXParameters or PKIXParameterBuilder) params.setRevocationEnabled(true);
Security.setProperty("ocsp.enable", "true");
Run Code Online (Sandbox Code Playgroud)

当您尝试验证证书路径时,这将激活OCSP检查(PKIXCertPathValidatorResult.validate()).

如果要在没有OCSP可用的情况下添加CRL的回退检查,请添加aditional属性:

System.setProperty("com.sun.security.enableCRLDP", "true");
Run Code Online (Sandbox Code Playgroud)

我的很多问题都发生了,因为我必须支持不同的证书格式(PKCS7,PEM).我的实现对于PEM工作正常,但由于PKCS7不保存链中证书的顺序,所以它有点难度(http://bugs.sun.com/view_bug.do?bug_id=6238093)

X509CertSelector targetConstraints = new X509CertSelector();

targetConstraints.setCertificate(certificates.get(0));
// Here's the issue for PKCS7 certificates since they are not ordered,
// but I havent figured out how I can see what the target certificate
// (lowest level) is in the incoming certificates..

PKIXBuilderParameters params = new PKIXBuilderParameters(anchors, targetConstraints);   
Run Code Online (Sandbox Code Playgroud)

希望这对其他人也是有用的评论,也许有人可以阐明如何在无序的PKCS7列表中找到目标证书?