Here’s the translation of your question into English:
Hello everyone, I’m having trouble creating symmetric keys using pkcs11js.
What I think might be happening is that I’m missing some attribute in the TEMPLATE that I pass to the C_GenerateKeyAsync function, or that the mechanism I’m passing to the function is not entirely correct.
I’ve been searching through the pkcs11js documentation and couldn’t find anything that helps solve the problem, and the same goes for the YubiHSM2 documentation.
The issue occurs when I try to create a symmetric key using the C_GenerateKeyAsync method. The error I’m getting is as follows:
errorData: {
name: 'Pkcs11Error',
message: 'CKR_FUNCTION_FAILED',
stackTrace: 'Pkcs11Error: CKR_FUNCTION_FAILEDn' +
' at prepareError (/opt/c-cipher/node_modules/pkcs11js/index.js:58:12)n' +
' at /opt/c-cipher/node_modules/pkcs11js/index.js:94:19'
},
And the YubiHSM2 logs are as follows:
[LIB - ERR 12:10:14.736362] yubihsm.c:2248 (generate_key): Failed to send GENERATE SYMMETRIC KEY command: Invalid command
[P11 - ERR 12:10:14.736387] yubihsm_pkcs11.c:5142 (C_GenerateKey): Failed generating symmetric key
This is my code:
async generateSymmetricKeyService(_symmetric) {
let SESSION;
let SESSION_ACTIVE = false;
let SLOT;
try {
pkcs11Lib.C_Initialize({
library: CC_OWN_CONST.LIB_PATH,
});
//? Get the list of slots and select one
const SLOTS = pkcs11Lib.C_GetSlotList(true);
SLOT = SLOTS[0];
if (!SLOT) throw new ErrorUtilClass(__filename, 'TCG_DYT', 'Slot not available').server();
//? Open a session
SESSION = pkcs11Lib.C_OpenSession(SLOT, pkcs11js.CKF_SERIAL_SESSION | pkcs11js.CKF_RW_SESSION);
pkcs11Lib.C_Login(SESSION, pkcs11js.CKU_USER, CC_OWN_CONST.CREDENTIAL.PASSWORD);
SESSION_ACTIVE = true;
//? Define the template for the symmetric key
const TEMPLATE = [
{ type: pkcs11js.CKA_CLASS, value: pkcs11js.CKO_SECRET_KEY },
{ type: pkcs11js.CKA_TOKEN, value: true },
{ type: pkcs11js.CKA_KEY_TYPE, value: pkcs11js.CKK_AES },
{ type: pkcs11js.CKA_LABEL, value: _symmetric.labelKey },
{ type: pkcs11js.CKA_VALUE_LEN, value: _symmetric.sizeKey },
{ type: pkcs11js.CKA_ENCRYPT, value: true },
{ type: pkcs11js.CKA_EXTRACTABLE, value: true }
];
let KEY;
KEY = await pkcs11Lib.C_GenerateKeyAsync(SESSION,{ mechanism: pkcs11js.CKM_AES_KEY_GEN }, TEMPLATE).catch((_error) => {
throw new ErrorUtilClass(__filename, 'TCG_DYT', _error).server();
});
return KEY;
} catch (_error) {
throw !_error.errorType ? new ErrorUtilClass(__filename, 'TCG_DYT', _error).server() : _error;
} finally {
if (SESSION_ACTIVE) {
pkcs11Lib.C_Logout(SESSION);
pkcs11Lib.C_CloseSession(SESSION);
}
pkcs11Lib.C_Finalize();
}
},
I’ve tried changing the template attributes and also adding parameters to the mechanism, but I haven’t been able to find the solution.
Camilo Torres is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.