使用Java PKCS#11读取DoD公共访问卡

时间:2013-05-14 18:40:28

标签: java pkcs#11 cac

我研究过如何使用Sun PKCS#11 api访问DoD CAC,并可能使用CAC访问(只读)Active Directory。我的问题是,我在大多数情况下发现的将引用一些代码,但从不显示引用的代码。我找到了以下代码,但是给出了错误。有没有人知道任何代码示例或使用PKCS11进行CAC的明确文档?或者可能有效的API解决方案?

import java.io.*;
import java.util.*;
import java.security.cert.CertificateException;
import java.security.KeyStoreException;
import java.security.cert.X509Certificate;
import java.security.KeyStore;
import java.security.Provider;
import java.security.Security;


public class SmartCard {
    public static void main(String[] args) throws Exception {
        try {
            String configName = "pkcs11.properties";
            Provider p = new sun.security.pkcs11.SunPKCS11(configName);
            Security.addProvider(p);
            Console c = System.console();
            char[] pin = c.readPassword("Enter your PIN: ");
            KeyStore cac = null;
            cac = KeyStore.getInstance("PKCS11");
            cac.load(null, pin);
            showInfoAboutCAC(cac);
        }
        catch(Exception ex) {
            ex.printStackTrace();
            System.exit(0);
        }
    }
    public static void showInfoAboutCAC(KeyStore ks) throws KeyStoreException, CertificateException {
        Enumeration<String> aliases = ks.aliases();
        while(aliases.hasMoreElements()) {
            String alias = aliases.nextElement();
            X509Certificate[] cchain = (X509Certificate[]) ks.getCertificateChain(alias);
            System.out.println("Certificate Chain for " + alias);
            for(int i = 0; i < cchain.length; i++) {
                System.out.println(" -SubjectDN: " + cchain[i].getSubjectDN());
                System.out.println(" -IssuerDN: " + cchain[i].getIssuerDN());
            }
        }
    }
}

java.security.ProviderException: Initialization failed
        at sun.security.pkcs11.SunPKCS11.<init>(SunPKCS11.java:374)
        at sun.security.pkcs11.SunPKCS11.<init>(SunPKCS11.java:103)
        at smartcard.SmartCard.main(SmartCard.java:21)
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:137)
        at sun.security.pkcs11.wrapper.PKCS11.getInstance(PKCS11.java:150)
        at sun.security.pkcs11.SunPKCS11.<init>(SunPKCS11.java:312)
        ... 2 more

2 个答案:

答案 0 :(得分:2)

在以下代码中,configName应该是某个配置文件的文件路径。它不存在,或者java无法读取它,因此抛出IOException。弄清楚该文件应该是什么并创建它,或者确保java有权访问该文件,并且代码将会运行。

String configName = "pkcs11.properties";
Provider p = new sun.security.pkcs11.SunPKCS11(configName);

阅读手册可能会有所帮助: http://docs.oracle.com/javase/8/docs/technotes/guides/security/p11guide.html#Config

答案 1 :(得分:1)

属性文件确实存在,并且与应用程序位于同一目录中。据我所知,Java可以访问该文件。

如果有帮助,这是文件的内容:

name=SmartCard
library=C:/Program Files/Java/jre7/bin/j2pkcs11.dll