I'm trying to use Softhsm2 in my project. Unfortunately, it keeps showing
java.security.ProviderException: Error parsing configuration
I try reinstall the app but problem still remain. May I know what I missed? Please check below source code, config file, tokens, java version & environment variable.
public static void main(String[] args) {
try {
String configName = "softhsm2.conf";
Provider p = new SunPKCS11(configName);
if (-1 == Security.addProvider(p)) {
throw new RuntimeException("could not add security provider");
}
// Load the key store
char[] pin = "mypin".toCharArray();
KeyStore keyStore = KeyStore.getInstance("PKCS11", p);
keyStore.load(null, pin);
// AES key
SecretKeySpec secretKeySpec = new SecretKeySpec("0123456789ABCDEF".getBytes(), "AES");
Key key = new SecretKeySpec(secretKeySpec.getEncoded(), "AES");
keyStore.setKeyEntry("AA", key, "1234".toCharArray(), null);
keyStore.store(null); //this gives me the exception.
} catch(Exception e) {
System.out.println("error: " + e.toString());
}
}
Token generated:
Microsoft Windows [Version 10.0.17134.1304]
(c) 2018 Microsoft Corporation. All rights reserved.
C:\WINDOWS\system32>cd/
C:\>cd softhsm2
C:\SoftHSM2>cd bin
C:\SoftHSM2\bin>softhsm2-util.exe --show-slots
Available slots:
Slot 1759353984
Slot info:
Description: SoftHSM slot ID 0x68dd9c80
Manufacturer ID: SoftHSM project
Hardware version: 2.5
Firmware version: 2.5
Token present: yes
Token info:
Manufacturer ID: SoftHSM project
Model: SoftHSM v2
Hardware version: 2.5
Firmware version: 2.5
Serial number: 8414edde68dd9c80
Initialized: yes
User PIN init.: yes
Label: My token 2
Slot 2066642166
Slot info:
Description: SoftHSM slot ID 0x7b2e74f6
Manufacturer ID: SoftHSM project
Hardware version: 2.5
Firmware version: 2.5
Token present: yes
Token info:
Manufacturer ID: SoftHSM project
Model: SoftHSM v2
Hardware version: 2.5
Firmware version: 2.5
Serial number: 9176dec57b2e74f6
Initialized: yes
User PIN init.: yes
Label: My token 1
Slot 2
Slot info:
Description: SoftHSM slot ID 0x2
Manufacturer ID: SoftHSM project
Hardware version: 2.5
Firmware version: 2.5
Token present: yes
Token info:
Manufacturer ID: SoftHSM project
Model: SoftHSM v2
Hardware version: 2.5
Firmware version: 2.5
Serial number:
Initialized: no
User PIN init.: no
Label:
C:\SoftHSM2\bin>
Config: C:\SoftHSM2\etc\softhsm2.conf
# SoftHSM v2 configuration file
directories.tokendir = C:\SoftHSM2\var\softhsm2\tokens\
objectstore.backend = file
log.level = INFO
slots.removable = false
java version "1.8.0_152"
Java(TM) SE Runtime Environment (build 1.8.0_152-b16)
Java HotSpot(TM) 64-Bit Server VM (build 25.152-b16, mixed mode)
Environment variable SOFTHSM2_CONF C:\SoftHSM2\etc\softhsm2.conf
Path: C:\SoftHSM2\lib\
I'm no JAVA expert but you seem to be passing incorrect configuration to
SunPKCS11
provider.There seem to be two different configurations in your scenario:
SunPKCS11
configuration that instructsSunPKCS11
provider which PKCS#11 library to load.SoftHSM2
configuration that instructsSoftHSM2
library where to store data etc.SunPKCS11
documentation suggests that in your caseSunPKCS11
configuration should look similar to this: