您需要创建一个名为的文件,并在其中包含以下行:card.config
name = myConfig
library = /path/to/library/that/implements/cac/card/reader
然后试试这个:
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.SecureRandom;
import java.security.Security;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManagerFactory;
public class Test
{
public static void main(String arg[]) throws Exception
{
try
{
//Create our certificates from our CAC Card
String configName = "card.config";
Provider p = new sun.security.pkcs11.SunPKCS11(configName);
Security.addProvider(p);
//Get the pin from user entered data
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)
{
//System.out.println("*" + ex.getMessage());
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(i + " SubjectDN: " + cchain[i].getSubjectDN());
System.out.println(i + " IssuerDN: " + cchain[i].getIssuerDN());
}
}
}
}
此时,您有一个密钥库,可用于创建 ssl 套接字以与 https Web 服务器通信。