I am trying to sign Pdf documents using Pkcs11Interop .net library. I need to use ECDSA encryption algorithm with SHA256 hash algorithm. And I am using SoftHSM 2.2.0 for storing private keys.
I found an CKM enum, CKM_ECDSA_SHA256, which I am passing while creating an object of class mechanism for calling the Sign method of Session.
I am getting the response from the "Signdata" method, however, on opening the Pdf files generated after signing give an error "Signature Invalid". Here is the code snippet for Signdata method call. I don't get any error or exception in the code, however, the pdf as I have mentioned are showing signature invalid.
private Pkcs11 _pkcs11;
private Slot _slot;
private Session _session;
try
{
_pkcs11 = new Pkcs11(hsmCryptoApi, true);
}
catch (Pkcs11Exception ex)
{
if (ex.RV == CKR.CKR_CANT_LOCK)
_pkcs11 = new Pkcs11(hsmCryptoApi, false);
else
throw ex;
}
_slot = FindSlot(_pkcs11, _certificateInformation.TokenLabel);
_session = _slot.OpenSession(true);
using (Mechanism mechanism = new Mechanism(CKM.CKM_ECDSA_SHA256))
{
_session.Login(CKU.CKU_USER, passowrd);
byte[] signedHash = _session.Sign(mechanism, GetPrivateKeyHandle(), message);
_session.Logout();
return signedHash;
}
private ObjectHandle GetPrivateKeyHandle()
{
string keyLabel = _certificateInformation.KeyLabel;
List<ObjectAttribute> searchTemplate = new List<ObjectAttribute>();
searchTemplate.Add(new ObjectAttribute(CKA.CKA_CLASS, CKO.CKO_PRIVATE_KEY));
searchTemplate.Add(new ObjectAttribute(CKA.CKA_LABEL, keyLabel));
List<ObjectHandle> foundObjects = _session.FindAllObjects(searchTemplate);
return foundObjects[0];
}
- Please tell me if SoftHSM 2.2.0 supports ECDSA_P256 with SHA256 or not ??
- If not, then is there any way to enable the support ??
- If it does support, please help me how to fix this ??
- It looks like it want me to pass ECDSA_Param, does anybody have any code snippet for passing the ECDSA_Param
I think you need to construct
ECDSA-Sig-Value
structure and fill it with the data from yoursignedHash
variable.PKCS#11 v2.20 chapter 12.3.1:
RFC5753 chapter 7.2:
Following method uses BouncyCastle library to constructs DER-encoded
ECDSA-Sig-Value
structure: