I am using softhsm
as pkcs11 provider, and my platform is ubuntu 64
I want to encrypt using softhsm
, so I imported a key into slot 0, it told that the key is imported and when I try to import another key with that ID it says the ID is used, so the provider has imported the key. But when I try to find the key by code, the wrapper can not find any key. Is there any problem with my code? or what??
Module pkcs11Module = Module.getInstance("libsofthsm.so");
pkcs11Module.initialize(null );
Info info = pkcs11Module.getInfo();
System.out.println(info);
Slot[] slotsWithToken = pkcs11Module.getSlotList(Module.SlotRequirement.TOKEN_PRESENT);
Token token = slotsWithToken[0].getToken();
Session session = token.openSession(Token.SessionType.SERIAL_SESSION,
Token.SessionReadWriteBehavior.RO_SESSION,
null,
null);
RSAPrivateKey searchTemplate = new RSAPrivateKey();
searchTemplate.getSign().setBooleanValue(Boolean.TRUE);
session.findObjectsInit(searchTemplate);
Object[] matchingKeys;
RSAPrivateKey signatureKey;
if ((matchingKeys = session.findObjects(1)).length > 0) {
signatureKey = (RSAPrivateKey) matchingKeys[0];
} else {
signatureKey = null; //It goes here so no key was found.
}