【发布时间】:2023-04-08 09:49:01
【问题描述】:
我曾经使用此代码访问用于在 USB 令牌上进行 PDF 签名的证书链:
this._keyStore = KeyStore.getInstance("PKCS11");
this._keyStore.load(null, myPassword);
Enumeration<String> aliases = this._keyStore.aliases();
while (aliases.hasMoreElements()) {
String nextElement = (String) aliases.nextElement();
System.out.println("Enumeration element : "+nextElement);
try
{
this._privateKey = (PrivateKey) this._keyStore.getKey(nextElement, pass);
this._certificatesChain = (X509Certificate[]) this._keyStore.getCertificateChain(nextElement);
if (this._certificatesChain.length == 0)
{
//Let's try another
continue;
}
if (this._certificatesChain[0].getKeyUsage()[1])
{
//I want to use this
break;
}
}
catch (Exception e){continue;}
我收到了一个新的 USB 令牌,它不能用于此。似乎使用 PKCS11 只会读取 Token 上的两个证书之一,这不是我应该用于签名的那个。
我想出的最佳解决方案是在 Keystore.getInstance() 调用中使用“Windows-MY”,它可以访问所有证书(即使是那些不是来自 Token 的证书,但让我们交叉手指)。 这样做的最大缺点是,这样做会为整个签名过程创建两个 PIN 提示:第一次是我编写的提示用户输入 PIN 的提示;第二次是我尝试签名时——这次是使用 Windows 风格的提示符。
有没有办法使用 PKCS11 密钥库实例访问所有证书,或者避免 Windows 提示?
【问题讨论】:
-
它是标准的 USB 存储密钥,还是加密设备?在第二种情况下,Pin 对于安全问题是正常的。
-
这是一个 USB 签名令牌。
标签: java digital-signature keystore signing pkcs#11