Node.js NodeJS FFI-创建结构的数组 我有C++代码。如何使用nodejs模块ffi,ref,ref-struct,ref-array CK_BBOOL yes = CK_TRUE; CK_BBOOL no = CK_FALSE; // encryption/decryption sensitive key CK_ATTRIBUTE key_template[] = { {CKA_SENSITIVE, &yes, sizeof(CK_BBOOL)}, // key is sensitive: should not be readable {CKA_ENCRYPT, &yes, sizeof(CK_BBOOL)} , // key can encrypt data {CKA_DECRYPT, &yes, sizeof(CK_BBOOL)} , // key can decrypt data }; CK_OBJECT_HANDLE key; // key handle for the new key CK_MECHANISM gen_mec = {CKM_DES_KEY_GEN, NULL_PTR, 0}; // DES key C_GenerateKey(session, &gen_mec, key_template, 3, &key); // generates the key if (rv != CKR_OK) { printf("Something went wrong while generating the key: %lu\n",rv); exit(1); } else printf("Key generated!\n"); // now key 'points' to the freshly generated key
有关更多详细信息,请参阅PKCS11函数 我的JavaScript代码是Node.js NodeJS FFI-创建结构的数组 我有C++代码。如何使用nodejs模块ffi,ref,ref-struct,ref-array CK_BBOOL yes = CK_TRUE; CK_BBOOL no = CK_FALSE; // encryption/decryption sensitive key CK_ATTRIBUTE key_template[] = { {CKA_SENSITIVE, &yes, sizeof(CK_BBOOL)}, // key is sensitive: should not be readable {CKA_ENCRYPT, &yes, sizeof(CK_BBOOL)} , // key can encrypt data {CKA_DECRYPT, &yes, sizeof(CK_BBOOL)} , // key can decrypt data }; CK_OBJECT_HANDLE key; // key handle for the new key CK_MECHANISM gen_mec = {CKM_DES_KEY_GEN, NULL_PTR, 0}; // DES key C_GenerateKey(session, &gen_mec, key_template, 3, &key); // generates the key if (rv != CKR_OK) { printf("Something went wrong while generating the key: %lu\n",rv); exit(1); } else printf("Key generated!\n"); // now key 'points' to the freshly generated key,node.js,ref,node-ffi,Node.js,Ref,Node Ffi,有关更多详细信息,请参阅PKCS11函数 我的JavaScript代码是 var Templates = RefArray(CKI.CK_ATTRIBUTE); var valueLen = new Buffer(4); valueLen.writeUInt32LE(32, 0); valueLen.type = CKI.CK_ULONG; //ulong var bTrue = new Buffer(1); bTrue.writeUInt8(1, 0); bTrue.type = CKI.
var Templates = RefArray(CKI.CK_ATTRIBUTE);
var valueLen = new Buffer(4);
valueLen.writeUInt32LE(32, 0);
valueLen.type = CKI.CK_ULONG; //ulong
var bTrue = new Buffer(1);
bTrue.writeUInt8(1, 0);
bTrue.type = CKI.CK_BYTE; //uchar
var template0 = new CKI.CK_ATTRIBUTE({type: CKI.CKA_SENSITIVE, pValue: bTrue.ref(), ulValueLen: 1});
var template1 = new CKI.CK_ATTRIBUTE({type: CKI.CKA_VALUE_LEN, pValue: valueLen.ref(), ulValueLen: 4});
var template2 = new CKI.CK_ATTRIBUTE({type: CKI.CKA_ENCRYPT, pValue: bTrue.ref(), ulValueLen: 1});
var template3 = new CKI.CK_ATTRIBUTE({type: CKI.CKA_DECRYPT, pValue: bTrue.ref(), ulValueLen: 1});
var templates = new Templates(4);
templates[0] = template0;
templates[1] = template2;
templates[2] = template3;
templates[3] = template1;
var $hObject = Ref.alloc(CKI.CK_ULONG);
Debug('C_GenerateKey');
var res = this.cki.C_GenerateKey(this.handle, mech.ref(), templates.ref(), 4, $hObject);
Utils.check_cki_res(res, 'C_GenerateKey');
为CKK_AES_KEY_GEN
mechanismC_GenerateKey
运行此代码后返回错误
Cryptoki函数C_GenerateKey上的错误。错误模板不完整(208)
我解决了我的问题。我的错误是错误地使用了
ref数组
(或者是ref数组
问题)。下面是我创建数组的示例
var buf = [];
function attr(t, v, l){
return (new CKI.CK_ATTRIBUTE({ type: t, pValue: v, ulValueLen: l })).ref()
}
buf.push(attr(CKI.CKA_CLASS, secretKey.ref(), 8));
buf.push(attr(CKI.CKA_TOKEN, bTrue.ref(), 1));
buf.push(attr(CKI.CKA_SENSITIVE, bTrue.ref(), 1));
buf.push(attr(CKI.CKA_VALUE_LEN, valueLen.ref(), 8));
buf.push(attr(CKI.CKA_KEY_TYPE, secretKey.ref(), 8));
buf.push(attr(CKI.CKA_LABEL, new Buffer("testAES"), 7));
buf.push(attr(CKI.CKA_PRIVATE, bTrue.ref(), 1));
var buf = Buffer.concat(buf);
var res = this.cki.C_GenerateKey(this.handle, mech.ref(), buf, 7, $hObject);
很好。我将日志打印到文件以进行检查。对于pkcs11
登录,我使用了它(它非常有用)。这是来自C_GenerateKey
函数的日志
0x000006e5 : 0x0708e740 : *** Begin attribute template ***
0x000006e5 : 0x0708e740 : Attribute 0
0x000006e5 : 0x0708e740 : Attribute: 0 (CKA_CLASS)
0x000006e5 : 0x0708e740 : pValue: 0x1bc2768
0x000006e5 : 0x0708e740 : ulValueLen: 8
0x000006e5 : 0x0708e740 : *pValue: HEX(A826BC0100000000)
0x000006e5 : 0x0708e740 : Attribute 1
0x000006e5 : 0x0708e740 : Attribute: 1 (CKA_TOKEN)
0x000006e5 : 0x0708e740 : pValue: 0x1bc2788
0x000006e5 : 0x0708e740 : ulValueLen: 1
0x000006e5 : 0x0708e740 : *pValue: HEX(B8)
0x000006e5 : 0x0708e740 : Attribute 2
0x000006e5 : 0x0708e740 : Attribute: 259 (CKA_SENSITIVE)
0x000006e5 : 0x0708e740 : pValue: 0x1bc27a8
0x000006e5 : 0x0708e740 : ulValueLen: 1
0x000006e5 : 0x0708e740 : *pValue: HEX(B8)
0x000006e5 : 0x0708e740 : Attribute 3
0x000006e5 : 0x0708e740 : Attribute: 353 (CKA_VALUE_LEN)
0x000006e5 : 0x0708e740 : pValue: 0x1bc27c8
0x000006e5 : 0x0708e740 : ulValueLen: 8
0x000006e5 : 0x0708e740 : *pValue: HEX(A026BC0100000000)
0x000006e5 : 0x0708e740 : Attribute 4
0x000006e5 : 0x0708e740 : Attribute: 256 (CKA_KEY_TYPE)
0x000006e5 : 0x0708e740 : pValue: 0x1bc27e8
0x000006e5 : 0x0708e740 : ulValueLen: 8
0x000006e5 : 0x0708e740 : *pValue: HEX(A826BC0100000000)
0x000006e5 : 0x0708e740 : Attribute 5
0x000006e5 : 0x0708e740 : Attribute: 3 (CKA_LABEL)
0x000006e5 : 0x0708e740 : pValue: 0x1bc2808
0x000006e5 : 0x0708e740 : ulValueLen: 7
0x000006e5 : 0x0708e740 : *pValue: HEX(74657374414553)
0x000006e5 : 0x0708e740 : Attribute 6
0x000006e5 : 0x0708e740 : Attribute: 2 (CKA_PRIVATE)
0x000006e5 : 0x0708e740 : pValue: 0x1bc2828
0x000006e5 : 0x0708e740 : ulValueLen: 1
0x000006e5 : 0x0708e740 : *pValue: HEX(B8)
0x000006e5 : 0x0708e740 : *** End attribute template ***
我解决了我的问题。我的错误是错误地使用了
ref数组
(或者是ref数组
问题)。下面是我创建数组的示例
var buf = [];
function attr(t, v, l){
return (new CKI.CK_ATTRIBUTE({ type: t, pValue: v, ulValueLen: l })).ref()
}
buf.push(attr(CKI.CKA_CLASS, secretKey.ref(), 8));
buf.push(attr(CKI.CKA_TOKEN, bTrue.ref(), 1));
buf.push(attr(CKI.CKA_SENSITIVE, bTrue.ref(), 1));
buf.push(attr(CKI.CKA_VALUE_LEN, valueLen.ref(), 8));
buf.push(attr(CKI.CKA_KEY_TYPE, secretKey.ref(), 8));
buf.push(attr(CKI.CKA_LABEL, new Buffer("testAES"), 7));
buf.push(attr(CKI.CKA_PRIVATE, bTrue.ref(), 1));
var buf = Buffer.concat(buf);
var res = this.cki.C_GenerateKey(this.handle, mech.ref(), buf, 7, $hObject);
很好。我将日志打印到文件以进行检查。对于pkcs11
登录,我使用了它(它非常有用)。这是来自C_GenerateKey
函数的日志
0x000006e5 : 0x0708e740 : *** Begin attribute template ***
0x000006e5 : 0x0708e740 : Attribute 0
0x000006e5 : 0x0708e740 : Attribute: 0 (CKA_CLASS)
0x000006e5 : 0x0708e740 : pValue: 0x1bc2768
0x000006e5 : 0x0708e740 : ulValueLen: 8
0x000006e5 : 0x0708e740 : *pValue: HEX(A826BC0100000000)
0x000006e5 : 0x0708e740 : Attribute 1
0x000006e5 : 0x0708e740 : Attribute: 1 (CKA_TOKEN)
0x000006e5 : 0x0708e740 : pValue: 0x1bc2788
0x000006e5 : 0x0708e740 : ulValueLen: 1
0x000006e5 : 0x0708e740 : *pValue: HEX(B8)
0x000006e5 : 0x0708e740 : Attribute 2
0x000006e5 : 0x0708e740 : Attribute: 259 (CKA_SENSITIVE)
0x000006e5 : 0x0708e740 : pValue: 0x1bc27a8
0x000006e5 : 0x0708e740 : ulValueLen: 1
0x000006e5 : 0x0708e740 : *pValue: HEX(B8)
0x000006e5 : 0x0708e740 : Attribute 3
0x000006e5 : 0x0708e740 : Attribute: 353 (CKA_VALUE_LEN)
0x000006e5 : 0x0708e740 : pValue: 0x1bc27c8
0x000006e5 : 0x0708e740 : ulValueLen: 8
0x000006e5 : 0x0708e740 : *pValue: HEX(A026BC0100000000)
0x000006e5 : 0x0708e740 : Attribute 4
0x000006e5 : 0x0708e740 : Attribute: 256 (CKA_KEY_TYPE)
0x000006e5 : 0x0708e740 : pValue: 0x1bc27e8
0x000006e5 : 0x0708e740 : ulValueLen: 8
0x000006e5 : 0x0708e740 : *pValue: HEX(A826BC0100000000)
0x000006e5 : 0x0708e740 : Attribute 5
0x000006e5 : 0x0708e740 : Attribute: 3 (CKA_LABEL)
0x000006e5 : 0x0708e740 : pValue: 0x1bc2808
0x000006e5 : 0x0708e740 : ulValueLen: 7
0x000006e5 : 0x0708e740 : *pValue: HEX(74657374414553)
0x000006e5 : 0x0708e740 : Attribute 6
0x000006e5 : 0x0708e740 : Attribute: 2 (CKA_PRIVATE)
0x000006e5 : 0x0708e740 : pValue: 0x1bc2828
0x000006e5 : 0x0708e740 : ulValueLen: 1
0x000006e5 : 0x0708e740 : *pValue: HEX(B8)
0x000006e5 : 0x0708e740 : *** End attribute template ***