【问题标题】:Java verify certificate againt issuer certificateJava验证证书与颁发者证书
【发布时间】:2021-04-13 00:24:09
【问题描述】:

假设我有一个根 CA -> 中间 CA -> 叶证书。我需要通过以下代码狙击验证叶子证书:

    /**
     * Attempts to build a certification chain for given certificate and to
     * verify it. Relies on a set of root CA certificates (trust anchors) and a
     * set of intermediate certificates (to be used as part of the chain).
     *
     * @param cert              - certificate for validation
     * @param trustAnchors      - set of trust anchors
     * @param intermediateCerts - set of intermediate certificates
     * @param signDate          the date when the signing took place
     * @return the certification chain (if verification is successful)
     * @throws GeneralSecurityException - if the verification is not successful
     *                                  (e.g. certification path cannot be built or some certificate in the chain
     *                                  is expired)
     */
    private static PKIXCertPathBuilderResult verifyCertificate(X509Certificate cert, Set<TrustAnchor> trustAnchors,
                                                               Set<X509Certificate> intermediateCerts, Date signDate) throws GeneralSecurityException {
        X509CertSelector selector = new X509CertSelector();
        selector.setCertificate(cert);
        PKIXBuilderParameters pkixParams = new PKIXBuilderParameters(trustAnchors, selector);
        // Disable CRL checks (this is done manually as additional step)
        pkixParams.setRevocationEnabled(false);
        pkixParams.setPolicyQualifiersRejected(false);
        pkixParams.setDate(signDate);
        // Specify a list of intermediate certificates
        CertStore intermediateCertStore = CertStore.getInstance("Collection", new CollectionCertStoreParameters(intermediateCerts));
        pkixParams.addCertStore(intermediateCertStore);
        // Build and verify the certification chain
        CertPathBuilder builder = CertPathBuilder.getInstance("PKIX");
        return (PKIXCertPathBuilderResult) builder.build(pkixParams);
    }

我可以理解参数 trustAnchors 是我的根 CA,参数 middleCerts 是我的中间 CA。但是由于某些原因,根 CA 是私有的(我的客户将其保密)并且不能作为 trustAnchors 传递(意味着 trustAnchors 为空/空)=> 发生异常。可以通过将中间 CA 作为 trustAnchors 来修复它(现在 intermediateCerts 将为空),我可以获得结果。但我不知道这种方式是否正确。有人可以帮我解决这个问题吗?

【问题讨论】:

  • “根 CA 是私有的”是什么意思?包括这个 CA 的证书应该不是问题。
  • 我的意思是我的客户不想公开他们的根 CA,因为有一些敏感信息。所以我只有中级 CA -> 叶证书
  • 好的,只是客户的愚蠢行为——在公共场合使用证书以及到非公共 CA 的信任路径。这就是我所说的糟糕设计......然后使用中间 CA 作为根 CA(用于您的代码)。然后证书验证在中间证书处停止。
  • 你的意思是我可以通过中间CA作为trustAnchors,一切都会好起来的?
  • 证书链验证运行,直到找到有效的根。据我所知,这个证书是否真的是一个自签名的根 CA 证书并不重要。

标签: java ssl certificate x509certificate bouncycastle


【解决方案1】:

正如@Robert 所说,我通过“然后使用中间 CA 作为根 CA(trustAnchors)来解决。然后证书验证在中间证书处停止”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-09
    • 1970-01-01
    • 2018-10-03
    • 2019-03-19
    相关资源
    最近更新 更多