are*_*res 7 java bouncycastle jce x509certificate
我正在使用以下代码根据此处X509Certificate的参考资料验证 a 。
static void verifyCertTrust(X509Certificate certificate, Set<X509Certificate> additionalCerts) throws CertificateException, NoSuchAlgorithmException, NoSuchProviderException, CertPathValidatorException, InvalidAlgorithmParameterException, CertPathBuilderException{
Set<X509Certificate> trustedRoots = new HashSet<X509Certificate>();
Set<X509Certificate> intermediateCerts = new HashSet<X509Certificate>();
for (X509Certificate cert : additionalCerts) {
if(isSelfSigned(cert)){
trustedRoots.add(cert);
}
else{
intermediateCerts.add(cert);
}
}
Set<TrustAnchor> trustAnchors = new HashSet<TrustAnchor>();
for (X509Certificate root : trustedRoots) {
trustAnchors.add(new TrustAnchor(root, null));
}
X509CertSelector selector = new X509CertSelector();
selector.setCertificate(certificate);
PKIXParameters parameters = new PKIXBuilderParameters(trustAnchors, selector);
parameters.setRevocationEnabled(false);
CertStore intermediateCertStore = CertStore.getInstance("Collection", new CollectionCertStoreParameters(intermediateCerts), "BC");
parameters.addCertStore(intermediateCertStore);
CertPathBuilder cpb = CertPathBuilder.getInstance("PKIX", "BC");
cpb.build(parameters);
}
Run Code Online (Sandbox Code Playgroud)
如果我BC在获取实例时删除提供程序CertPathBuilder并让 JVM 使用默认SUN提供程序,则此方法有效。但是,对于BC提供程序,我得到以下异常。
Exception in thread "main" java.security.cert.CertPathBuilderException: No certificate found matching targetContraints.
at org.bouncycastle.jce.provider.PKIXCertPathBuilderSpi.engineBuild(Unknown Source)
at java.security.cert.CertPathBuilder.build(Unknown Source)
at signer.GetPkcs11Key.verifyCertTrust(GetPkcs11Key.java:105)
at signer.GetPkcs11Key.main(GetPkcs11Key.java:71)
Run Code Online (Sandbox Code Playgroud)
任何想法如何与 BouncyCastle 提供商一起使用?
小智 6
要验证的证书必须在您的示例中的 CertStore 中,因此添加以下内容:
parameters.setRevocationEnabled...;
//Add the certitificate to the cert store
intermediateCerts.add(certificate);
CertStore intermediateCertStore....
Run Code Online (Sandbox Code Playgroud)