Java从Windows密钥库访问中间CA?
我需要在Windows上构建一个证书链,从X.509智能卡证书到一个或多个中间CA再到根CA。当CA证书位于JKS密钥库中时,这很容易,但我也需要使用Windows密钥库 我可以从“Windows根目录”获取根CA证书,但无法获取“中间证书颁发机构”密钥库 有人这样做过吗Java从Windows密钥库访问中间CA?,java,windows,x509certificate,Java,Windows,X509certificate,我需要在Windows上构建一个证书链,从X.509智能卡证书到一个或多个中间CA再到根CA。当CA证书位于JKS密钥库中时,这很容易,但我也需要使用Windows密钥库 我可以从“Windows根目录”获取根CA证书,但无法获取“中间证书颁发机构”密钥库 有人这样做过吗 谢谢 SunMSCAPI加密提供程序只支持两个密钥库:Windows MY(个人证书存储)和Windows ROOT(可信机构证书存储),因此我认为无法直接访问其他Windows证书存储。但是,这可能不是必需的,因为Windo
谢谢 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
更新(4月2日)
可以通过编程方式在Windows MY
和Windows ROOT
密钥库中添加证书,但有一些限制:
- 在
Windows根目录中添加证书时,系统将提示用户确认
- 在
密钥库中添加的所有证书都是Windows MY
(从密钥库的角度来看,而不是从Windows的角度来看)。密钥库似乎用所有可用的证书构建了尽可能长的链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证书链在构建时不会进行验证,但现在您可以执行创建证书路径和PKIXParameters并使用它们验证链的常规操作
CertPathValidator certPathValidator = CertPathValidator.getInstance(CertPathValidator.getDefaultType());
certPathValidator.validate(certPath, params);
是的,如果用户的证书在个人存储中,则没有问题。我想知道我是否可以通过编程方式添加它?这很有效!我从智能卡检索用户证书,以编程方式将其添加到Windows my store,然后使用Windows密钥库构建链。