java - Java验证证书与颁发者证书
问题描述
假设我有一个根 CA -> 中间 CA -> 叶证书。我需要通过以下代码狙击验证叶子证书:
/**
* Attempts to build a certification chain for given certificate and to
* verify it. Relies on a set of root CA certificates (trust anchors) and a
* set of intermediate certificates (to be used as part of the chain).
*
* @param cert - certificate for validation
* @param trustAnchors - set of trust anchors
* @param intermediateCerts - set of intermediate certificates
* @param signDate the date when the signing took place
* @return the certification chain (if verification is successful)
* @throws GeneralSecurityException - if the verification is not successful
* (e.g. certification path cannot be built or some certificate in the chain
* is expired)
*/
private static PKIXCertPathBuilderResult verifyCertificate(X509Certificate cert, Set<TrustAnchor> trustAnchors,
Set<X509Certificate> intermediateCerts, Date signDate) throws GeneralSecurityException {
X509CertSelector selector = new X509CertSelector();
selector.setCertificate(cert);
PKIXBuilderParameters pkixParams = new PKIXBuilderParameters(trustAnchors, selector);
// Disable CRL checks (this is done manually as additional step)
pkixParams.setRevocationEnabled(false);
pkixParams.setPolicyQualifiersRejected(false);
pkixParams.setDate(signDate);
// Specify a list of intermediate certificates
CertStore intermediateCertStore = CertStore.getInstance("Collection", new CollectionCertStoreParameters(intermediateCerts));
pkixParams.addCertStore(intermediateCertStore);
// Build and verify the certification chain
CertPathBuilder builder = CertPathBuilder.getInstance("PKIX");
return (PKIXCertPathBuilderResult) builder.build(pkixParams);
}
我可以理解参数 trustAnchors 是我的根 CA,参数 intermediateCerts 是我的中间 CA。但是由于某些原因,根 CA 是私有的(我的客户将其保密)并且不能作为 trustAnchors 传递(意味着 trustAnchors 为空/空)=> 发生异常。可以通过将中间 CA 作为 trustAnchors 来修复它(现在 middleCerts 将为空),我可以获得结果。但我不知道这种方式是否正确。有人可以帮我解决这个问题吗?
解决方案
正如@Robert 所说,我通过“然后使用中间 CA 作为根 CA(trustAnchors)来解决。然后证书验证在中间证书处停止”。
推荐阅读
- xml - 使用 GPath 查找具有特定子节点的 XML 节点
- javascript - 如何在导出为 web 组件的 aurelia 组件中启用渲染路由器视图
- ios - 如何在单独的 xib 文件和单独的类中创建工具栏并在多个视图控制器中使用它?
- mapbox - 地图缩放时如何不加载cartoDB的隐藏层(Mapbox是底图)
- html - 简单导航栏不起作用:只显示空白 div
- r - 每个参数每小时数据的每日平均值
- angular - Angular 类型脚本依赖项未定义或类未定义
- php - 将多行内的值保存到数组
- c++ - c++ 激光检测使用逐帧分析
- python - 使用 numpy 将带通滤波器应用于 .WAV 文件