我正在尝试验证我自己生成的证书链,我似乎得到了正确的结果,但我不确定我是否以正确的方式进行验证。
假设我有一个 Intermediate -> Intermediate -> Leaf
链。我不关心根,我想验证链中的某个点。这可能是根,或者只是较长链的一小段,前提是它是完整的。
澄清;无论最终根是什么,我都想验证该链。 Root -> Intermediate 1 -> Intermediate 2 -> Leaf
应该是有效的,并且只有 Intermediate 1 -> Intermediate 2 -> Leaf
。这里唯一的区别可能(?)是什么被认为是 TrustAnchor。
为此,我关注了this Oracle documentation .
这是我的代码片段:
public boolean validateChain(final X509Certificate... certificates) {
try {
final CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
final X509Certificate intermediateCertificate = certificates[0];
final X509Certificate leaf = certificates[1];
final List<X509Certificate> path = Collections.singletonList(leaf);
final CertPath certPath = certificateFactory.generateCertPath(path);
Set<TrustAnchor> anchors = new HashSet<>();
anchors.add(new TrustAnchor(intermediateCertificate, null));
final X509Certificate root = this.certificate;
anchors.add(new TrustAnchor(root, null));
PKIXParameters params = new PKIXParameters(anchors);
params.setRevocationEnabled(false);
CertPathValidator validator = CertPathValidator.getInstance("PKIX");
validator.validate(certPath, params);
return true;
} catch (final GeneralSecurityException e) {
LOG.error("Could not validate certificate chain", e);
}
return false;
}
不要管基本的数组处理,我会对此进行改进。此实现假定已实现我的基类,并且始终是验证提供的证书所依据的根。
这是正确的做法吗?我真的不明白 TrustAnchor 和放置在 CertPath 中的证书之间的区别。
如果我将名为 intermediateCertificate
的证书添加到 CertPath 的构造中,执行将失败并显示:
Caused by: java.security.cert.CertPathValidatorException: subject/issuer name chaining check failed
如果我想验证一个有 10 个证书的链,是否应该只将叶证书放在 CertPath 中,而链上的所有其他证书都被视为 TrustAnchors?
如果有人能阐明这种区别,我将不胜感激。
补充信息编辑:
我知道我的链是有效的,因为我已经使用 OpenSSL 验证了我的整个链。这是我的整个链,其中每个文件只包含证书的公共(public)部分。
openssl verify -CAfile root.pem -untrusted intermediate1.pem -untrusted intermediate2.pem leaf.pem
我还验证了部分链:
openssl verify -no-CApath -partial_chain -trusted intermediate1.pem -trusted intermediate2.pem leaf.pem
这两个都以 leaf.pem: OK
响应。
最佳答案
我必须回答我的问题,因为我刚刚弄明白了,这是我这边的一个错误,但我也可以回答提出的问题。
不,不应将中间证书添加为信任 anchor 。信任 anchor 是您信任所有证书的共同点。
如果我正在阅读 RFC 5280是的,在处理 x509 证书时,信任根只是一个公钥,您可以通过它验证底层证书链是否到达该来源并遵循给定的约束。 IE。您的信任 anchor 是否为根并不重要。
我原来帖子中的片段是正确的,但我的类中有一个设置安全提供程序的静态初始化程序:
Security.addProvider(new BouncyCastleProvider());
为了让我的代码正常工作,我必须为证书工厂设置相同的提供者。这是完整的工作示例,以备不时之需:
public boolean validateChain(final X509Certificate... certificates) {
try {
final CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509", BouncyCastleProvider.PROVIDER_NAME); // <--- Here's the addition
final List<X509Certificate> path = Arrays.asList(certificates);
final CertPath certPath = certificateFactory.generateCertPath(path);
PKIXParameters params = new PKIXParameters(Set.of(new TrustAnchor(this.certificate, null)));
params.setRevocationEnabled(false);
CertPathValidator validator = CertPathValidator.getInstance("PKIX");
validator.validate(certPath, params);
return true;
} catch (final GeneralSecurityException e) {
LOG.error("Could not validate certificate chain", e);
}
return false;
}
在这种情况下,this.certificate
是根。我有一个类创建一个对象来处理支持 CA 的证书。
关于java - 验证证书链时是否应将中间证书添加为 TrustAnchors?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60633743/