根据Java

时间:2017-03-01 08:55:38

标签: java security x509certificate

我有一个使用CAdES进行数字签名的文档。我使用BouncyCastle API来获取签名者的X509Certificate[]个实例,但我们假设该列表包含一个和一个唯一的元素。

我需要在今天的日期验证此证书是否可信,我不想使用通常用于信任SSL证书的系统标准信任存储。不,我想在我的类路径中使用.cer文件列表构建自己的信任列表。目前,单个CA是可信的,但显然将来可能会添加更多证书。

到目前为止,我已阅读 this 并尝试在我的代码中实现。我不需要SSLContext,我需要检查数字签名文档的有效性。我现在很困惑。

然而,

X509TrustManager API只提供验证客户端/服务器证书的方法,但我的只有数字签名/不可否认使用标志。

问题可以用两种方式表达:

  1. Java中的一个如何针对可以加载到内存中的自定义根CA列表检查X509Certificate实例的有效性?
  2. 如何检查数字签名文档是否使用来自自定义列表的已知CA的证书进行签名?

1 个答案:

答案 0 :(得分:2)

从每个签名者的CAdES签名中提取签名者的证书,并将中间证书从X509Certificate列表中提取出来。还构建包含所有根CA证书的集合

然后您可以使用此(略微调整)example code来验证和构建使用Java和BouncyCastle的认证链。如果验证成功,它将返回认证链

public PKIXCertPathBuilderResult verifyCertificateChain(
     X509Certificate cert, 
     Set<X509Certificate> trustedRootCerts,
     Set<X509Certificate> intermediateCerts) throws GeneralSecurityException {

    // Create the selector that specifies the starting certificate
    X509CertSelector selector = new X509CertSelector(); 
    selector.setCertificate(cert);

    // Create the trust anchors (set of root CA certificates)
    Set<TrustAnchor> trustAnchors = new HashSet<TrustAnchor>();
    for (X509Certificate trustedRootCert : trustedRootCerts) {
        trustAnchors.add(new TrustAnchor(trustedRootCert, null));
    }

    // Configure the PKIX certificate builder algorithm parameters
    PKIXBuilderParameters pkixParams = 
        new PKIXBuilderParameters(trustAnchors, selector);

    // Disable CRL checks (this is done manually as additional step)
    pkixParams.setRevocationEnabled(false);

    // Specify a list of intermediate certificates
    // certificate itself has to be added to the list 
    intermediateCerts.add(cert); 
    CertStore intermediateCertStore = CertStore.getInstance("Collection",
        new CollectionCertStoreParameters(intermediateCerts), "BC");
    pkixParams.addCertStore(intermediateCertStore);

    // Build and verify the certification chain
    CertPathBuilder builder = CertPathBuilder.getInstance("PKIX", "BC");
    PKIXCertPathBuilderResult result = 
        (PKIXCertPathBuilderResult) builder.build(pkixParams);
    return result;
}

如果您不想处理CAdES的复杂性,我建议使用SD-DSS开源项目