我有一个使用CAdES进行数字签名的文档。我使用BouncyCastle API来获取签名者的X509Certificate[]
个实例,但我们假设该列表包含一个和一个唯一的元素。
我需要在今天的日期验证此证书是否可信,我不想使用通常用于信任SSL证书的系统标准信任存储。不,我想在我的类路径中使用.cer
文件列表构建自己的信任列表。目前,单个CA是可信的,但显然将来可能会添加更多证书。
到目前为止,我已阅读 this 并尝试在我的代码中实现。我不需要SSLContext
,我需要检查数字签名文档的有效性。我现在很困惑。
X509TrustManager
API只提供验证客户端/服务器证书的方法,但我的只有数字签名/不可否认使用标志。
问题可以用两种方式表达:
X509Certificate
实例的有效性?答案 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开源项目