这并不难 – 假设你以某种方式/带外获得了一个或多个钥匙串中的所有中间证书和根证书.
看一下
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堆栈.