I am seeing an null pointer exception when trying to get the private key from java pkcs11 keystore, when the key is generated by pkcs11-tool. This works fine if the key is generate using keytool. I can list the keys from pkcs11-tool as well but not from keytool. What is the correct way to import or generate keys other than keytool so that they are visible to java pkcs11 keystore?
Generate keys:
pkcs11-tool --module /usr/lib/x86_64-linux-gnu/softhsm/libsofthsm2.so -l --pin <pin> --keypairgen --key-type rsa:2048 --label jtest
From java load and access the key(code snippet):
String configName = "/tmp/pkcs11.cfg";
Provider p = new SunPKCS11(configName);
Security.addProvider(p);
char[] pin = "<pin>".toCharArray();
KeyStore keyStore = KeyStore.getInstance("PKCS11", p);
keyStore.load(null, pin);
PrivateKeyEntry privateKeyEntry =
(PrivateKeyEntry)keyStore.getEntry("jtest", null);
PrivateKey privateKey = privateKeyEntry.getPrivateKey();
See the exception when trying to get the private key above.