【发布时间】:2021-03-31 17:00:18
【问题描述】:
我是 PKI 世界的新手,一般是证书。我正在编写一个需要验证证书链的服务。
采取的一般做法如下
a) 根据发送的数据生成证书列表
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(data);
CertPathValidatorResult certPathValidatorResult = null;
try {
CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
List<X509Certificate> x509Certificates =
(List<X509Certificate>) certificateFactory.generateCertificates(byteArrayInputStream);
CertPath certPath = certificateFactory.generateCertPath(x509Certificates);
-
加载 JDK 密钥库,像这样 //加载JDK的cacerts keystore文件 字符串文件名 = System.getProperty("java.home") + "/lib/security/cacerts".replace('/', File.separatorChar); FileInputStream is = new FileInputStream(filename); KeyStore 密钥库 = KeyStore.getInstance(KeyStore.getDefaultType()); 字符串密码 = "changeit"; keystore.load(is, password.toCharArray());
-
CertPathValidator certPathValidator = CertPathValidator.getInstance("PKIX"); PKIXParameters pkixParameters = new PKIXParameters(keystore); //pkixParameters.setRevocationEnabled(false); PKIXParameters certPathValidatorResult = certPathValidator.validate(certPath, pkixParameters); -
我假设如果这不是一个有效的链,它会抛出一个异常。此验证检查是否会过期证书、有效公钥?
-
我还需要能够找到证书的 OCSP 状态或检查它是否被撤销>?如何使用 Cryptography API 完成此操作
-
是否推荐使用充气城堡而不是 API? Bouncy castle 有办法检查证书的 CRL 和 OCSP 状态吗?
提前感谢所有的指点和帮助。欣赏它。
最好的问候
【问题讨论】:
-
PKIX 验证器默认检查到期和撤销。没有通用的方法来检查公钥是否有效生成,即使对于特定的算法,它通常也不能仅在公钥上仅对私钥进行。 PKIX 验证器可以做 OCSP,见docs.oracle.com/en/java/javase/11/security/…。
标签: java certificate x509certificate ocsp