0

I'm writing PKCS11 cryptoki wrapper using nodejs plugins ffi, ref, ref-struct and ref-array. I have this code.

var hSession = this.session.handle;
var hObject = this.handle;
var $label = new (arrayType(cki.CK_UTF8CHAR))(80);

var template = new (arrayType(cki.CK_ATTRIBUTE))(1);
template[0] = new cki.CK_ATTRIBUTE({
    type:cki.CKA_LABEL, 
    pValue: $label.ref(), 
    ulValueLen: 80}) 
var res = this.cki.C_GetAttributeValue(hSession, hObject, template.ref(), 1);
if (res == cki.CKR_OK) {
    console.log("Ok");
}
else{
    console.log("Wrong "+res);
}

When I call this function I have wrong results (CKR_ARGUMENTS_BAD, CKR_ATTRIBUTE_TYPE_INVALID). Please, help me to find error.

FFI function

"C_GetAttributeValue":[t.CK_RV, [t.CK_SESSION_HANDLE, t.CK_OBJECT_HANDLE, t.CK_ATTRIBUTE_PTR, t.CK_ULONG]],

Types

/* CK_ATTRIBUTE is a structure that includes the type, length
 * and value of an attribute */
t.CK_ATTRIBUTE = struct({
  type: t.CK_ATTRIBUTE_TYPE,
  pValue: t.CK_VOID_PTR,

  /* ulValueLen went from CK_USHORT to CK_ULONG for v2.0 */
  ulValueLen: t.CK_ULONG  /* in bytes */
});

11
  • Could you try to pass a NULL pointer as pValue in the struct. This way you can confirm, that the attribute type (and the structure as a whole) is understood correctly (giving NULL is used to query the actual attribute length). Commented Sep 30, 2015 at 10:02
  • Additionally you might consider using some logging pkcs#11 library to see how your function call is understood/processed. For example this -- the first one I googled, didn't test (can't help here as I have some proprietary logging library which I cannot share) Commented Sep 30, 2015 at 10:05
  • Do you have any progress (I am just curious and saw your new question)? Commented Oct 3, 2015 at 20:58
  • 1
    What if you change var $label = new (arrayType(cki.CK_UTF8CHAR))(80); into var $label = new Buffer(80); and use it as pValue: $label,? Commented Oct 3, 2015 at 21:10
  • 1
    $label.toString('ascii',0,18)? Commented Oct 3, 2015 at 21:20

1 Answer 1

1

(Transcript of discussion in comments)

Use pure Buffer to provide the buffer to store the attribute value:

var $label = new Buffer(80);

Pass it in the structure as follows:

template[0] = new cki.CK_ATTRIBUTE({
    type:cki.CKA_LABEL, 
    pValue: $label, 
    ulValueLen: $label.length}) 

Then use $label.toString('utf8',0,<ulValueLen>) to get the actual string.

Note: I am not proficient with Node FFI, but this approach just seem to work.

Sign up to request clarification or add additional context in comments.

1 Comment

To convert Buffer for attribute CKA_CLASS use $label.readInt32LE()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.