gpt4 book ai didi

java - PKIXCertPathBuilder 使用 Bouncy CaSTLe 提供程序失败但使用默认 (SUN) 提供程序

转载 作者:搜寻专家 更新时间:2023-11-01 02:38:47 26 4
gpt4 key购买 nike

我正在使用以下代码根据引用文献 here 验证 X509Certificate .

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);

}

如果我在获取 CertPathBuilder 实例时删除提供程序 BC 并让 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)

关于如何与 BouncyCaSTLe 提供程序一起使用的任何想法?

最佳答案

要验证的证书必须在您的示例中的 CertStore 中,因此请添加:

 parameters.setRevocationEnabled...;
//Add the certitificate to the cert store
intermediateCerts.add(certificate);
CertStore intermediateCertStore....

关于java - PKIXCertPathBuilder 使用 Bouncy CaSTLe 提供程序失败但使用默认 (SUN) 提供程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39438741/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com