获取证书链

use*_*934 6 java cryptography certificate digital-certificate x509certificate

我正在使用Java中的X509证书.给定证书是否可以在签名层次结构中找到所有其他证书,直到您获得根证书?

我有一个证书文件(带.cer扩展名),我想提取父签名证书.我想继续找到该证书的父级,直到我得到最终的根证书,这是自签名的.

我已检查过X509Certificate证书API和相关API,java.security.cert但找不到任何有用的内容.

Dir*_*lik 2

这并不难 - 假设您已经以某种方式/带外获得了一个或多个钥匙串中的所有中间证书和根证书。

看一下

http://codeautomate.org/blog/2012/02/certificate-validation-using-java/
Run Code Online (Sandbox Code Playgroud)

对于一个代码片段来说,它就是这样做的。密钥位位于 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
Run Code Online (Sandbox Code Playgroud)

至于如何获得中间/根证书 - 这是一个不同的问题。请注意,这段代码有点天真 - 并且不太理解交叉签名。不过,java pkix 会调用 - BouncyCastle 有一个示例。

您通常可以将根证书构建到密钥链中;但中间证书通常需要更动态地“收集”或发现。这通常需要在 TLS 或类似过程中查询 SSL 堆栈。