java.security.KeyStoreException: PKCS11 not found

dexter picture dexter · Oct 3, 2016 · Viewed 8.5k times · Source

My goal is to read information from a CAC card and use pkcs11 to extract information from it and sign my document. I couldn't find the right dll for my hardware, so I installed openSC on my machine and used opensc-pkcs11.dll in the following code:

String configName = "pkcs.cnf"; //my config file that points to opensc-pkcs11.dll
String PIN = "123456";
Provider p = new sun.security.pkcs11.SunPKCS11(configName);
Security.addProvider(p);
KeyStore keyStore = KeyStore.getInstance("PKCS11");
char[] pin = PIN.toCharArray();
keyStore.load(null, pin);

But I'm getting an error at the following location

 KeyStore keyStore = KeyStore.getInstance("PKCS11");

with the Error stacktrace:

java.security.KeyStoreException: PKCS11 not found
    at java.security.KeyStore.getInstance(Unknown Source)
    Caused by: java.security.NoSuchAlgorithmException: no such algorithm: PKCS11 for provider SunPKCS11-FooAccelerator
    at sun.security.jca.GetInstance.getService(Unknown Source)
    at sun.security.jca.GetInstance.getInstance(Unknown Source)
    at java.security.Security.getImpl(Unknown Source)
    ... 2 more

Help!

Answer

ARAVIND picture ARAVIND · Dec 4, 2016

First create a config.cfg as shown below:

name=name of your CAC card
slot=1
library=C:\Windows\System32\eps2003csp11.dll 
\\This is the dll file for etoken like this when you are installing driver, a separate dll file would be generated for your CAC card.

Then provide the config file path in the program, as shown below:

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