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 */
});

4

1 回答 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.

于 2015-10-03T21:36:07.057 回答