1

我用 Java 实现了一个 SAML SP。
为了验证 SAML 响应的证书,
我从 SAML 响应中提取 X509Certificate 元素,并根据我预先上传 IDP 证书的 Java 密钥库文件对其进行验证。
我使用以下代码来验证证书:

 X509Certificate certFromResponse = //extract from SAML response 
 KeyStore keyStore = getKS();
 PKIXParameters params = new PKIXParameters(keyStore);
 params.setRevocationEnabled(false);
 CertPath certPath = 
 certificateFactory.generateCertPath(Arrays.asList(certFromResponse));
 CertPathValidator certPathValidator = CertPathValidator.getInstance(CertPathValidator.getDefaultType());
 CertPathValidatorResult result = certPathValidator.validate(certPath, params);

这适用于根 CA 的证书。
当证书有证书路径时,验证失败。
一种可能的处理方法是将路径中的所有证书手动上传到
具有不同别名的 JKS 文件中,然后将它们提取到如下列表中:

List<Certificate> certs = new ArrayList<Certificate>();
certs.add(certFromResponse);
if (keyStore.getCertificate("ALIAS_CA_1") != null) {
    certs.add(keyStore.getCertificate("ALIAS_CA_1"));
}
if (keyStore.getCertificate("ALIAS_CA_2") != null) {
    certs.add(keyStore.getCertificate("ALIAS_CA_2");
}
...
CertPath certPath = certificateFactory.generateCertPath(certs);

有没有更直接的方法来做到这一点?
是否可以从证书本身中提取证书路径?

谢谢!

4

1 回答 1

1

PKIXParameters 似乎自动提取了证书路径,因此无需手动进行。
我们所要做的就是将所有证书上传到密钥库。

于 2013-07-08T08:17:51.543 回答