gpt4 book ai didi

java - Java 中的 CRL 检查

转载 作者:太空宇宙 更新时间:2023-11-03 13:22:31 27 4
gpt4 key购买 nike

我已将 CertStore 配置为使用本地存储的 CRL。我只想使用这些本地存储的 CRL 来执行证书验证。如果传入连接的证书与这些 CRL 中的任何一个都不匹配,则不应尝试从 CDP 点获取 CRL,而只是软失败。有什么办法可以实现吗?

        System.setProperty("com.sun.security.enableCRLDP", "false");
KeyManagerFactory keyManagerFactory = null;
KeyStore keyStore = null;
keyManagerFactory = KeyManagerFactory.getInstance(keyAlgorithm);
keyStore = KeyStore.getInstance(keyStoreType);
ksFile = new FileInputStream(keyStoreFile);
keyStore.load(ksFile,password);
keyManagerFactory.init (keyStore,password);

TrustManagerFactory tmf = TrustManagerFactory.getInstance("PKIX", "SunJSSE");
CertPathBuilder cpb = CertPathBuilder.getInstance("PKIX");
List<CertStore> certStores = new ArrayList<>();
Collection<CRL> crls = new HashSet<>();
crls.add(CertificateFactory.getInstance("X.509").generateCRL( new java.io.FileInputStream("crl path")));
crls.add(CertificateFactory.getInstance("X.509").generateCRL( new java.io.FileInputStream("crl path2")));
certStores.add(CertStore.getInstance("Collection", new CollectionCertStoreParameters(crls)));
PKIXRevocationChecker rc = (PKIXRevocationChecker)cpb.getRevocationChecker();
rc.setOptions(EnumSet.of(
PKIXRevocationChecker.Option.PREFER_CRLS, // prefer CLR over OCSP
// handshake should not fail when CRL is not available
PKIXRevocationChecker.Option.NO_FALLBACK));

CertPathParameters pkixParams = new PKIXBuilderParameters(keyStore, new X509CertSelector());
// PKIXBuilderParameters pkixParams = new PKIXBuilderParameters(keyStore, new X509CertSelector());
((PKIXParameters) pkixParams).setRevocationEnabled(true);
((PKIXParameters) pkixParams).setCertStores(certStores);
((PKIXParameters) pkixParams).addCertPathChecker(rc);

tmf.init( new CertPathTrustManagerParameters(pkixParams) );

SSLContext context = SSLContext.getInstance(protocol);
context.init (keyManagerFactory.getKeyManagers (), tmf.getTrustManagers(), null);

最佳答案

审查 PKIXRevocationChecker文档和 checkCRLs代码,我可以看到 com.sun.security.enableCRLDP 除了在传统模式下没有被使用,并且 如果提供的 CRL 不适合验证证书,则下载 CRL

您可以查看/调试在 DistributionPointFetcher.verifyCRLs 中应用的标准看看为什么你的 CRL 没有被使用

Verifies a CRL for the given certificate's Distribution Point to ensure it is appropriate for checking the revocation status.

static boolean verifyCRL(X509CertImpl certImpl, DistributionPoint point,X509CRL crl, boolean[] reasonsMask, boolean signFlag,PublicKey prevKey, X509Certificate prevCert, String provider,Set<TrustAnchor> trustAnchors, List<CertStore> certStores,Date validity) throws CRLException, IOException {

备选方案:自行检查撤销

您需要禁用默认撤销检查才能使用您自己的 CRL 列表。我认为您可以包装默认的 TrustManager 以在服务器证书链的信任验证期间包含吊销检查

首先删除 ((PKIXParameters) pkixParams).setRevocationEnabled(true); 然后添加这段代码

//tmf.init(...);

TrustManager[] trustManagers = tmf.getTrustManagers();
final X509TrustManager origTrustmanager = (X509TrustManager)trustManagers[0];

TrustManager[] wrappedTrustManagers = new TrustManager[]{
new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return origTrustmanager.getAcceptedIssuers();
}

public void checkClientTrusted(X509Certificate[] certs, String authType) {
origTrustmanager.checkClientTrusted(certs, authType);
}

public void checkServerTrusted(X509Certificate[] certs, String authType) {
//Original trust checking
origTrustmanager.checkServerTrusted(certs, authType);

//Check revocation with each CRL
//from docs: CertificateException - if the certificate chain is not trusted by this TrustManager.
for (CRL crl: crls){
if (crl.isRevoked(certs[0]){
throw new CertificateException (e);
}
}
}
}
};

免责声明:我没有测试它,但它应该基于类似的 examples 工作

关于java - Java 中的 CRL 检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45460015/

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