1

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!

N00b Pr0grammer
  • 4,503
  • 5
  • 32
  • 46
dexter
  • 41
  • 1
  • 1
  • 7
  • I believe the problem is with your .dll, check if it the correct dll file you are referrring – mhasan Oct 03 '16 at 06:50
  • Why dont you use KeyStore keyStore = KeyStore.getInstance("PKCS11",p); to get the keystore, might help you in getting close to your issue – mhasan Oct 03 '16 at 06:52
  • @mhasan, that did not work either. Can I download the dll from the internet? I am using a CAC card – dexter Oct 03 '16 at 13:36

1 Answers1

2

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);
N00b Pr0grammer
  • 4,503
  • 5
  • 32
  • 46
ARAVIND
  • 51
  • 1
  • 8
  • I noticed a recent edit to this post, which dramatically change your intention, Please check if it's your intention or not – Danh Dec 05 '16 at 08:23
  • `slot` instead of the documented `slotListindex` fixed it for me, worth mentioning – PatricNox Sep 23 '21 at 09:09