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

我不得不提到的是,如果我们使用小程序,则能够运行这种应用程序(然后将使用浏览器的密钥存储区和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。

我在Windows7和Java 1.6上运行27

谢谢

最佳答案

OpenSC PKCS#11的名称为“ opensc-pkcs11.dll”,并将其放置到system32中。但是,您需要确保OpenSC支持您的智能卡。作为一般规则:您需要使用卡随附的PKCS#11提供程序(通常是封闭源)或支持您的卡(如OpenSC)

09-08 01:35