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

时间:2015-12-28 10:07:52

标签: java digital-signature keystore signing pkcs#11

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

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

是否有办法使用PKCS11 Keystore实例访问所有证书,或者避免使用Windows提示符?

1 个答案:

答案 0 :(得分:0)

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

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