我需要在Windows上构build一个证书链,从X.509智能卡证书通过一个或多个中间CA到根CA. 当CA证书位于JKS密钥库中时,这很容易,但我也需要使用Windows密钥库。
我可以从“Windows-ROOT”获得根CA证书,但我无法进入“中级证书颁发机构”密钥库。
有没有人做过这个?
谢谢!
SunMSCAPI加密提供程序只支持两个密钥存储: Windows-MY
(个人证书存储)和Windows-ROOT
(可信机构证书存储),因此我不认为可以直接访问其他Windows证书存储。 但是,它可能不是必需的,因为Windows-MY
密钥库似乎能够使用来自其他存储的证书构建证书链。
这里是我用来测试它的代码片段:
KeyStore ks = KeyStore.getInstance("Windows-MY"); ks.load(null, null) ; Enumeration en = ks.aliases() ; while (en.hasMoreElements()) { String aliasKey = (String)en.nextElement() ; Certificate c = ks.getCertificate(aliasKey) ; System.out.println("---> alias : " + aliasKey) ; if (ks.isKeyEntry(aliasKey)) { Certificate[] chain = ks.getCertificateChain(aliasKey); System.out.println("---> chain length: " + chain.length); for (Certificate cert: chain) { System.out.println(cert); } }
如果我在个人证书存储中添加一个带有私钥的证书,则链长度为1.在CA证书存储中添加CA后,我将再次启动该程序,链长度为2。
更新(四月,二月)可以以编程方式在Windows-MY
和Windows-ROOT
密钥库中添加证书,但有一些限制:
Windows-ROOT
添加证书时,会提示用户进行确认 Windows-MY
密钥库中添加的所有证书都是TrustedCertificateEntry
(从密钥存储的角度而不是Windows的角度)。 密钥库似乎用所有可用的证书构建了最长的链。 在密钥库中添加证书非常简单:
Certificate c = CertificateFactory.getInstance("X.509").generateCertificate(new FileInputStream("C:/Users/me/Downloads/myca.crt")); KeyStore.TrustedCertificateEntry entry = new KeyStore.TrustedCertificateEntry(c); ks.setEntry("CA1", entry , null);
Jcs有答案,但我想显示一些伪代码:
// load the Windows keystore KeyStore winKeystore = KeyStore.getInstance("Windows-MY", "SunMSCAPI"); winKeystore.load(null, null); // add the user's smart card cert to the keystore winKeystore.setCertificateEntry(myAlias, userCertificate); // build the cert chain! this will include intermediate CAs Certificate[] chain = winKeystore.getCertificateChain(myAlias);
Windows证书链在构建时未经过验证,但是现在您可以通过创建CertPath和PKIXParameters并使用它们来验证链。
CertPathValidator certPathValidator = CertPathValidator.getInstance(CertPathValidator.getDefaultType()); certPathValidator.validate(certPath, params);