Error received while decrypting data when private key is retrieved from HSM.
I have added sunpkcs11 provider in java.security. Hence, NOT adding provide
How I resolved:
Root cause of this issue was that sunpkcs11 provider was getting loaded both statically and dynamically.
i.e. in java.security, provider entry along with cfg path was already added.
Also, in code, provider was initialized again with the cfg file.
This was causing the issue.
After changing:
SunPKCS11 provider = new SunPKCS11("/home/user/pkcs11.cfg");
TO:
SunPKCS11 sunPKCS11Provider = (SunPKCS11) Security.getProvider("SunPKCS11");
issue got resolved.
I have used following code and issue has been resolved
SunPKCS11 provider = new SunPKCS11("/home/user/pkcs11.cfg");
Security.addProvider(provider);
KeyStore keystore = KeyStore.getInstance("PKCS11");
keystore.load(null, passphrase.toCharArray());