从DNCSe上没有PIN /密码的PKCS11智能卡获取证书

时间:2017-10-26 23:00:24

标签: java cryptography

我想找到一些适用于Windows的库,因为我试图提取smartcrd的公共证书(DNIe,西班牙国家文件)而没有用于学校目的的针脚。 有我的代码,但我想问题在于库:

String pkcs11Config = "name = SmartCard\nlibrary = C:\\SCMNIGHT\\opensc-pkcs11.dll";
ByteArrayInputStream confStream = new ByteArrayInputStream(pkcs11Config.getBytes());
Provider prov = new sun.security.pkcs11.SunPKCS11(confStream);
Security.addProvider(prov);
KeyStore cc = null;
String pin = "";
try {
    cc = KeyStore.getInstance("PKCS11",prov);
    KeyStore.PasswordProtection pp = new KeyStore.PasswordProtection(pin.toCharArray());
    cc.load(null ,  pp.getPassword() );
    Enumeration aliases = cc.aliases();
    while (aliases.hasMoreElements()) {
        Object alias = aliases.nextElement();
        try {
            X509Certificate cert0 = (X509Certificate) cc.getCertificate(alias.toString());
            System.out.println("I am: " + cert0.getSubjectDN().getName());
        } catch (Exception e) {
            continue;
        }
    }
} catch (Exception e) {
    e.printStackTrace();
}

因为我得到了;

java.io.IOException: load failed
at sun.security.pkcs11.P11KeyStore.engineLoad(P11KeyStore.java:766)
at java.security.KeyStore.load(KeyStore.java:1445)
at com.mycompany.tests.Example.main(Example.java:38)
Caused by: javax.security.auth.login.LoginException
    at sun.security.pkcs11.SunPKCS11.login(SunPKCS11.java:1238)
    at sun.security.pkcs11.P11KeyStore.login(P11KeyStore.java:864)
    at sun.security.pkcs11.P11KeyStore.engineLoad(P11KeyStore.java:753)
    ... 2 more
Caused by: sun.security.pkcs11.wrapper.PKCS11Exception: CKR_PIN_LEN_RANGE
    at sun.security.pkcs11.wrapper.PKCS11.C_Login(Native Method)
    at sun.security.pkcs11.SunPKCS11.login(SunPKCS11.java:1222)
    ... 4 more

我正在使用open sc pkcs reader和Java PKCS11。 任何想法,建议或评论?

1 个答案:

答案 0 :(得分:0)

您不应该只是为了获得公共证书而需要PIN。在任何情况下,""都是空PIN,而不是缺少PIN。请尝试null代替pin.toCharArray()pp.getPassword()。事实上,我并没有在这里看到使用KeyStore.PasswordProtection的重点。

但显然你确实需要PIN,而且还有一个,所以你需要找出它的内容,可能来自用户,并将其提供给load()

相关问题