PKCS#11 PKCS11 dll

时间:2012-01-16 14:40:52

标签: java smartcard pki pkcs#11

我们正在尝试实现一个java独立应用程序,它可以连接到https网站并使用PKI智能卡进行身份验证,我们正在解决各种问题。

我必须提到,如果我们使用applet(然后将使用浏览器的密钥库和trustore),我们就可以运行这种应用程序,一切正常,我们输入卡片编号,我们就可以访问网页。

我有两个问题。首先关于我的代码,有人看到它的错误。我在运行时包含了运行时错误:

public class TestPKCS11 {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {

    // Set keyStore and trustStore
    System.setProperty("javax.net.ssl.trustStoreType", "PKCS11");
    System.setProperty("javax.net.ssl.trustStore", "NONE");
    System.setProperty("javax.net.ssl.trustStorePassword", "changeit");
    System.setProperty("javax.net.ssl.trustStoreProvider", "SunPKCS11-mycard");
    String trustStore = System.getProperty("javax.net.ssl.trustStore");
    if (trustStore == null) {
        System.out.println("javax.net.ssl.trustStore is not defined");
    } else {
        System.out.println("javax.net.ssl.trustStore = " + trustStore);
    }

    System.setProperty("javax.net.ssl.keyStoreType", "PKCS11");
    System.setProperty("javax.net.ssl.keyStore", "NONE");
    System.setProperty("javax.net.ssl.keyStorePassword", "changeit");
    System.setProperty("javax.net.ssl.keyStoreProvider", "SunPKCS11-mycard");
    String keyStore = System.getProperty("javax.net.ssl.keyStore");
    if (keyStore == null) {
        System.out.println("javax.net.ssl.keyStore is not defined");
    } else {
        System.out.println("javax.net.ssl.keyStore = " + keyStore);
    }

    System.setProperty("javax.net.debug", "ssl"); // dynamic conf of PKCS#11

    String configName = "C:\\confDirectory\\pkcs11.cfg";

    sun.security.pkcs11.SunPKCS11 sunPKCS11 = new sun.security.pkcs11.SunPKCS11(configName);
    Provider p = sunPKCS11;
    Security.addProvider(p);


    SSLSocketFactory sslFact = (SSLSocketFactory) SSLSocketFactory.getDefault();

    try{
        SSLSocket sock = (SSLSocket)sslFact.createSocket("myserver", 8081);

        sock.startHandshake();

    } catch (SSLHandshakeException ex) {
        Logger.getLogger(TestPKCS11.class.getName()).log(Level.SEVERE, null, ex);
        System.out.println(ex.getMessage());
    } 
    catch (IOException ex) {
        Logger.getLogger(TestPKCS11.class.getName()).log(Level.SEVERE, null, ex);
    }
}

执行错误:

Exception in thread "main" java.security.ProviderException: Initialization failed
    at sun.security.pkcs11.SunPKCS11.<init>(SunPKCS11.java:340)
    at sun.security.pkcs11.SunPKCS11.<init>(SunPKCS11.java:86)
    at TestPKCS11.main(TestPKCS11.java:95)
Caused by: java.io.IOException: The specified procedure could not be found.
    at sun.security.pkcs11.wrapper.PKCS11.connect(Native Method)
    at sun.security.pkcs11.wrapper.PKCS11.<init>(PKCS11.java:141)
    at sun.security.pkcs11.wrapper.PKCS11.getInstance(PKCS11.java:154)
    at sun.security.pkcs11.SunPKCS11.<init>(SunPKCS11.java:281)
    ... 2 more

我的第二个问题是关于用于pkcs11的dll。目前我正在使用它包含在IBM的Rational安装(jpkcs11.dll)中,但我真的不确定它是好的。我看过OpenSC但找不到OpenSC-pkcs11.dll文件。我只能看到opensc.dll。

我在使用Java 1.6 27的Windows7上运行

谢谢

2 个答案:

答案 0 :(得分:2)

OpenSC PKCS#11被命名为“opensc-pkcs11.dll”,它被放到system32。但您需要确保OpenSC支持您的智能卡。作为一般规则:您需要使用您的卡附带的PKCS#11提供商(通常是封闭源)或支持您的卡(如OpenSC)

答案 1 :(得分:0)

您可以从用户主目录动态加载DLL,而不是将DLL放入system32。但这取决于您的应用程序,您将如何安装它。