我正在使用Java中的X509证书。 鉴于证书是否可以在签名层次结构中找到所有其他证书,直到您获得根证书?
我有一个证书文件(扩展名为.cer
),我想提取父签名证书。我想继续找到该证书的父级,直到我得到最终的根证书,这是自签名的。
我已在java.security.cert
中检查了X509Certificate证书API和相关API,但找不到任何有用的内容。
答案 0 :(得分:2)
这并不难 - 假设你以某种方式/带外获得了一个或多个钥匙串中的所有中间证书和根证书。
看看
http://codeautomate.org/blog/2012/02/certificate-validation-using-java/
代码剪切,就是这样。关键位在validateKeyChain()中,基本上由
组成 cert = cert-to-validate
while(not self signed) {
extract issuer from cert
scan keychain(s) to find cert with a subject equal to the issuer
if none found - error
check if the signature is correct.
cert = issuers_cert
}
if not at the top/root - error
至于如何获得中间/根证书 - 这是一个不同的问题。请注意,此代码有点天真 - 并且不太了解交叉签名。尽管如此,java pkix会调用--BouncyCastle有一个例子。
您通常可以将根证书构建到密钥链中;但是中间证书通常需要“收集”或更加动态地发现。这通常需要在TLS或类似过程中查询SSL堆栈。