无法访问 USB 令牌上的所有 KeyStore 别名

Can't access all KeyStore aliases on USB Token

我曾经使用以下代码访问证书链以在 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 只会读取令牌上的两个证书之一,这不是我应该用于签名的证书。

我想出的最佳解决方案是在 Keystore.getInstance() 调用中使用 "Windows-MY",它可以访问所有证书(甚至那些不是来自令牌的证书,但让我们交叉手指)。 这样做的最大缺点是这样做会为整个签名过程创建两个 PIN 提示:第一次是我编码的提示,要求用户输入 PIN;第二次是我尝试签名的时候——这次是 Windows 风格的提示。

有没有办法使用 PKCS11 密钥库实例访问所有证书,或者避免出现 Windows 提示?

我考虑过删除 post,因为我的代码中的其他地方有错误,但如果有人需要它,我会把它留在这里:

问题是我用来读取证书的 dll (incryptoki2.dll) 不再适用于来自同一制造商的新密钥。切换到 bit4ipki.dll,现在我可以读取证书了。