I'm almost losing hope on this one. I'm trying to access the Firefox trust store from Java 7 using the NSS libraries that come with the Firefox installation, via PKCS#11.
Here is the code:
import java.security.KeyStore;
import java.security.Security;
import java.util.Enumeration;
import sun.security.pkcs11.SunPKCS11;
public class Test {
public static void main(String[] args) throws Exception {
String configName = "pkcs11.cfg";
SunPKCS11 p = new SunPKCS11(configName);
Security.addProvider(p);
KeyStore ks = KeyStore.getInstance("PKCS11", p);
ks.load(null, "apassword".toCharArray());
System.out.println("Size: " + ks.size());
Enumeration<String> aliases = ks.aliases();
while (aliases.hasMoreElements()) {
System.out.println(aliases.nextElement());
}
}
}
Here are the contents for the PKCS#11 config:
name = NSS
nssLibraryDirectory = /usr/lib/firefox/
nssSecmodDirectory = "/home/bogdan/.mozilla/firefox/x5d8wol9.default/"
nssModule =trustanchors
showInfo = true
When I run the application I also set the property -Djava.library.path=/usr/lib/firefox/
When I run the application I get the following:
NSS modules: [NSS Internal PKCS #11 Module (CRYPTO, /usr/lib/firefox/libsoftokn3.so, slot 0), NSS Internal PKCS #11 Module (KEYSTORE, /usr/lib/firefox/libsoftokn3.so, slot 1)]
Exception in thread "main" java.security.ProviderException: NSS module not available: trustanchors
at sun.security.pkcs11.SunPKCS11.<init>(SunPKCS11.java:271)
at sun.security.pkcs11.SunPKCS11.<init>(SunPKCS11.java:103)
at Test.main(Test.java:11)
You can actually see that the "trustanchors" module is not loaded at the initialisation step, but I have no idea why. The documentation here: http://docs.oracle.com/javase/6/docs/technotes/guides/security/p11guide.html#NSS says that
The trustanchors module enables access to NSS trust anchor certificates via the PKCS11 KeyStore, if secmod.db has been configured to include the trust anchor library.
but I have no idea what that means. It's worth noting that I get the same behaviour with both Windows XP 32 bit and Ubuntu 11.10 64-bit. It seems that the pkcs11.cfg is correct as if I change any of the paths the application will fail with other errors.
Any bright ideas?
modutil -list -dbdir /home/bogdan/.mozilla/firefox/x5d8wol9.default/
– BogdanException in thread "main" java.security.ProviderException: Library /usr/lib/firefox/home/bogdan/.mozilla/firefox/x5d8wol9.default/libnssckbi.so does not exist
(notice the weird path which is a concatenation with LD_LIBRARY_PATH) – Bogdan