根据 Java 中的自定义信任列表检查证书有效性
Check a certificate validity against a custom trust list in Java
我有一份使用 CAdES 进行数字签名的文档。我使用 BouncyCastle API 获取签名者的 X509Certificate[]
个实例,但我们假设该列表仅包含一个元素。
我需要在今天验证此证书是否受信任,并且我不想使用通常用于信任 SSL 证书的系统标准信任库。不,我想在我的类路径中使用 .cer
文件列表构建我自己的信任列表。目前,单个 CA 是受信任的,但显然将来可能会添加更多证书。
到目前为止,我已经阅读了 this 并尝试在我的代码中实现。我不需要 SSLContext
,我需要检查数字签名文档的有效性。我现在很困惑。
X509TrustManager
API 只提供验证 client/server 证书的方法,但我的只有数字 signature/non-repudiation 使用标志。
问题可以用两种方式来表述:
- Java 如何根据可加载到内存中的自定义根 CA 列表检查
X509Certificate
实例的有效性?
- 如何检查数字签名文档是否使用从自定义列表的已知 CA 派生的证书签名?
从每个签名者的 CAdES 签名中提取签名者的证书以及作为 X509Certificate
列表的中间证书。还构建一个包含所有根 CA 证书的集合
然后您可以使用这个(稍微改编)example code 来验证并使用 Java 和 BouncyCastle 构建认证链。如果验证成功,它将return认证链
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开源项目
我有一份使用 CAdES 进行数字签名的文档。我使用 BouncyCastle API 获取签名者的 X509Certificate[]
个实例,但我们假设该列表仅包含一个元素。
我需要在今天验证此证书是否受信任,并且我不想使用通常用于信任 SSL 证书的系统标准信任库。不,我想在我的类路径中使用 .cer
文件列表构建我自己的信任列表。目前,单个 CA 是受信任的,但显然将来可能会添加更多证书。
到目前为止,我已经阅读了 this 并尝试在我的代码中实现。我不需要 SSLContext
,我需要检查数字签名文档的有效性。我现在很困惑。
X509TrustManager
API 只提供验证 client/server 证书的方法,但我的只有数字 signature/non-repudiation 使用标志。
问题可以用两种方式来表述:
- Java 如何根据可加载到内存中的自定义根 CA 列表检查
X509Certificate
实例的有效性? - 如何检查数字签名文档是否使用从自定义列表的已知 CA 派生的证书签名?
从每个签名者的 CAdES 签名中提取签名者的证书以及作为 X509Certificate
列表的中间证书。还构建一个包含所有根 CA 证书的集合
然后您可以使用这个(稍微改编)example code 来验证并使用 Java 和 BouncyCastle 构建认证链。如果验证成功,它将return认证链
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开源项目