diff --git a/index.d.ts b/index.d.ts index 91f71dd..5237a12 100644 --- a/index.d.ts +++ b/index.d.ts @@ -410,6 +410,37 @@ declare module "pkcs11js" { iteration: number; } + interface KeyWrapSetOaepParams extends IParams { + bc: number; + x: Buffer; + } + + interface GcmParams extends IParams { + iv: Buffer; + ivBits: number; + aad?: Buffer; + tagBits?: number; + } + + interface CcmParams extends IParams { + dataLen: number; + nonce?: Buffer; + aad?: Buffer; + macLen?: number; + } + + interface GostR3410DeriveParams extends IParams { + kdf: number; + publicData: Buffer; + ukm?: Buffer; + } + + interface GostR3410KeyWrapParams extends IParams { + wrapOID: Buffer; + ukm?: Buffer; + key: Handle; + } + //#endregion interface KeyPair { @@ -2036,6 +2067,11 @@ declare module "pkcs11js" { const CK_PARAMS_SKIPJACK_PRIVATE_WRAP: number; const CK_PARAMS_SKIPJACK_RELAYX: number; const CK_PARAMS_PBE: number; + const CK_PARAMS_KEY_WRAP_SET_OAEP: number; + const CK_PARAMS_GCM: number; + const CK_PARAMS_CCM: number; + const CK_PARAM_GOSTR3410_DERIVE: number; + const CK_PARAM_GOSTR3410_KEY_WRAP: number; //#endregion //#region User types diff --git a/src/common.h b/src/common.h index d7675f6..5c3f9bb 100644 --- a/src/common.h +++ b/src/common.h @@ -7,6 +7,7 @@ #include #include #include +#include #ifdef _WIN32 // Use Windows-specific definitions @@ -66,6 +67,11 @@ typedef CK_NSS_C_INITIALIZE_ARGS *CK_NSS_C_INITIALIZE_ARGS_PTR; #define CK_PARAMS_SKIPJACK_PRIVATE_WRAP 21 #define CK_PARAMS_SKIPJACK_RELAYX 22 #define CK_PARAMS_PBE 23 +#define CK_PARAMS_KEY_WRAP_SET_OAEP 24 +#define CK_PARAMS_GCM 25 +#define CK_PARAMS_CCM 26 +#define CK_PARAM_GOSTR3410_DERIVE 27 +#define CK_PARAM_GOSTR3410_KEY_WRAP 28 /** * @brief Retrieves the name of the error code. diff --git a/src/const.cpp b/src/const.cpp index 355615d..1e00bd5 100644 --- a/src/const.cpp +++ b/src/const.cpp @@ -168,6 +168,11 @@ void set_ckp(napi_env env, napi_value exports) SET_CONST(CK_PARAMS_SKIPJACK_PRIVATE_WRAP); SET_CONST(CK_PARAMS_SKIPJACK_RELAYX); SET_CONST(CK_PARAMS_PBE); + SET_CONST(CK_PARAMS_KEY_WRAP_SET_OAEP); + SET_CONST(CK_PARAMS_GCM); + SET_CONST(CK_PARAMS_CCM); + SET_CONST(CK_PARAM_GOSTR3410_DERIVE); + SET_CONST(CK_PARAM_GOSTR3410_KEY_WRAP); } /** diff --git a/src/params.cpp b/src/params.cpp index 53bf4d9..24421af 100644 --- a/src/params.cpp +++ b/src/params.cpp @@ -163,6 +163,76 @@ bool read_property_ulong(napi_env env, napi_value object, const char *property, } \ } +/** + * @brief Reads a byte property from a given JavaScript object. + * + * @param env The N-API environment. + * @param object The JavaScript object to read the property from. + * @param property The name of the property to read. + * @param value A pointer to store the read value. + * @return Returns true if the property was successfully read, false otherwise. + */ +bool read_property_byte(napi_env env, napi_value object, const char *property, CK_BYTE_PTR value) +{ + // Get the property value + napi_value propertyValue; + napi_get_named_property(env, object, property, &propertyValue); + + // Check that the property value is a Number + napi_valuetype propertyValueType; + napi_typeof(env, propertyValue, &propertyValueType); + if (propertyValueType != napi_number) + { + return false; + } + + // Read the property value and write it to the single byte + uint32_t uintValue; + napi_get_value_uint32(env, propertyValue, (uint32_t *)&uintValue); + if (uintValue > 255) + { + return false; + } + *value = (CK_BYTE)uintValue; + + return true; +} + +/** + * @brief Macro for reading a required byte property from a given JavaScript object. + * + * Creates a variable with the name of the property and reads the property value into it. + * + * @param property The name of the property to read. + * @param target The JavaScript object to read the property from. + * @param paramType The type of the mechanism parameter. + */ +#define READ_BYTE_REQUIRED(property, target, paramType) \ + CK_BYTE property; \ + if (!read_property_byte(env, target, #property, &property)) \ + { \ + THROW_PROPERTY_TYPE(#property, #paramType, "Number"); \ + } + +/** + * @brief Macro for reading an optional byte property from a given JavaScript object. + * + * Creates a variable with the name of the property and reads the property value into it + * + * @param property The name of the property to read. + * @param target The JavaScript object to read the property from. + * @param paramType The type of the mechanism parameter. + */ +#define READ_BYTE_OPTIONAL(property, target, paramType) \ + CK_BYTE property = 0; \ + if (has_property(env, target, #property)) \ + { \ + if (!read_property_byte(env, target, #property, &property)) \ + { \ + THROW_PROPERTY_TYPE(#property, #paramType, "Number"); \ + } \ + } + /** * @brief Reads a byte array property from a given JavaScript object. * @@ -281,6 +351,48 @@ bool read_property_handle(napi_env env, napi_value object, const char *property, } \ } +bool read_property_string(napi_env env, napi_value object, const char *property, std::string *value) +{ + // Get the property value + napi_value propertyValue; + napi_get_named_property(env, object, property, &propertyValue); + + // Check that the property value is a String + napi_valuetype propertyValueType; + napi_typeof(env, propertyValue, &propertyValueType); + if (propertyValueType != napi_string) + { + return false; + } + + // Read the property value + size_t stringLength; + napi_get_value_string_utf8(env, propertyValue, nullptr, 0, &stringLength); + char *stringValue = (char *)malloc(stringLength + 1); + napi_get_value_string_utf8(env, propertyValue, stringValue, stringLength + 1, &stringLength); + *value = std::string(stringValue); + free(stringValue); + + return true; +} + +#define READ_STRING_REQUIRED(property, target, paramType) \ + std::string property; \ + if (!read_property_string(env, target, #property, &property)) \ + { \ + THROW_PROPERTY_TYPE(#property, #paramType, "String"); \ + } + +#define READ_STRING_OPTIONAL(property, target, paramType) \ + std::string property; \ + if (has_property(env, target, #property)) \ + { \ + if (!read_property_string(env, target, #property, &property)) \ + { \ + THROW_PROPERTY_TYPE(#property, #paramType, "String"); \ + } \ + } + /** * Reads CK_RSA_PKCS_OAEP_PARAMS from a given JavaScript object. * @@ -962,4 +1074,140 @@ bool get_params_pbe( mechanism->ulParameterLen = sizeof(CK_PBE_PARAMS); return true; -} \ No newline at end of file +} + +// Reads CK_KEY_WRAP_SET_OAEP_PARAMS from a given JavaScript object. +bool get_params_key_wrap_set_oaep( + napi_env env, + napi_value mechanismParameter, + CK_MECHANISM_PTR mechanism) +{ + CK_KEY_WRAP_SET_OAEP_PARAMS_PTR params = CK_KEY_WRAP_SET_OAEP_PARAMS_PTR(malloc(sizeof(CK_KEY_WRAP_SET_OAEP_PARAMS))); + + // Read the mechanism parameters + READ_BYTE_OPTIONAL(bc, mechanismParameter, CK_KEY_WRAP_SET_OAEP_PARAMS); + READ_BYTES_OPTIONAL(x, mechanismParameter, CK_KEY_WRAP_SET_OAEP_PARAMS); + + // Create the mechanism parameters structure + params->bBC = bc; + params->pX = x; + params->ulXLen = xLength; + + // Set the mechanism parameters + mechanism->pParameter = params; + mechanism->ulParameterLen = sizeof(CK_KEY_WRAP_SET_OAEP_PARAMS); + + return true; +} + +// Reads CK_GCM_PARAMS from a given JavaScript object. +bool get_params_gcm( + napi_env env, + napi_value mechanismParameter, + CK_MECHANISM_PTR mechanism) +{ + CK_GCM_PARAMS_PTR params = CK_GCM_PARAMS_PTR(malloc(sizeof(CK_GCM_PARAMS))); + + // Read the mechanism parameters + READ_BYTES_REQUIRED(iv, mechanismParameter, CK_GCM_PARAMS); + READ_ULONG_REQUIRED(ivBits, mechanismParameter, CK_GCM_PARAMS); + READ_BYTES_OPTIONAL(aad, mechanismParameter, CK_GCM_PARAMS); + READ_ULONG_OPTIONAL(tagBits, mechanismParameter, CK_GCM_PARAMS); + + // Create the mechanism parameters structure + params->pIv = iv; + params->ulIvLen = ivLength; + params->ulIvBits = ivBits; + params->pAAD = aad; + params->ulAADLen = aadLength; + params->ulTagBits = tagBits; + + // Set the mechanism parameters + mechanism->pParameter = params; + mechanism->ulParameterLen = sizeof(CK_GCM_PARAMS); + + return true; +} + +// Reads CK_CCM_PARAMS from a given JavaScript object. +bool get_params_ccm( + napi_env env, + napi_value mechanismParameter, + CK_MECHANISM_PTR mechanism) +{ + CK_CCM_PARAMS_PTR params = CK_CCM_PARAMS_PTR(malloc(sizeof(CK_CCM_PARAMS))); + + // Read the mechanism parameters + READ_ULONG_REQUIRED(dataLength, mechanismParameter, CK_CCM_PARAMS); + READ_BYTES_OPTIONAL(nonce, mechanismParameter, CK_CCM_PARAMS); + READ_BYTES_OPTIONAL(aad, mechanismParameter, CK_CCM_PARAMS); + READ_ULONG_OPTIONAL(macLength, mechanismParameter, CK_CCM_PARAMS); + + // Create the mechanism parameters structure + params->ulDataLen = dataLength; + params->pNonce = nonce; + params->ulNonceLen = nonceLength; + params->pAAD = aad; + params->ulAADLen = aadLength; + params->ulMACLen = macLength; + + // Set the mechanism parameters + mechanism->pParameter = params; + mechanism->ulParameterLen = sizeof(CK_CCM_PARAMS); + + return true; +} + +// Reads CK_GOSTR3410_DERIVE_PARAMS from a given JavaScript object. +bool get_params_gost_r3410_derive( + napi_env env, + napi_value mechanismParameter, + CK_MECHANISM_PTR mechanism) +{ + CK_GOSTR3410_DERIVE_PARAMS_PTR params = CK_GOSTR3410_DERIVE_PARAMS_PTR(malloc(sizeof(CK_GOSTR3410_DERIVE_PARAMS))); + + // Read the mechanism parameters + READ_ULONG_REQUIRED(kdf, mechanismParameter, CK_GOSTR3410_DERIVE_PARAMS); + READ_BYTES_REQUIRED(publicData, mechanismParameter, CK_GOSTR3410_DERIVE_PARAMS); + READ_BYTES_OPTIONAL(ukm, mechanismParameter, CK_GOSTR3410_DERIVE_PARAMS); + + // Create the mechanism parameters structure + params->kdf = kdf; + params->pPublicData = publicData; + params->ulPublicDataLen = publicDataLength; + params->pUKM = ukm; + params->ulUKMLen = ukmLength; + + // Set the mechanism parameters + mechanism->pParameter = params; + mechanism->ulParameterLen = sizeof(CK_GOSTR3410_DERIVE_PARAMS); + + return true; +} + +// Reads CK_GOSTR3410_KEY_WRAP_PARAMS from a given JavaScript object. +bool get_params_gost_r3410_key_wrap( + napi_env env, + napi_value mechanismParameter, + CK_MECHANISM_PTR mechanism) +{ + CK_GOSTR3410_KEY_WRAP_PARAMS_PTR params = CK_GOSTR3410_KEY_WRAP_PARAMS_PTR(malloc(sizeof(CK_GOSTR3410_KEY_WRAP_PARAMS))); + + // Read the mechanism parameters + READ_BYTES_REQUIRED(wrapOID, mechanismParameter, CK_GOSTR3410_KEY_WRAP_PARAMS); + READ_BYTES_OPTIONAL(ukm, mechanismParameter, CK_GOSTR3410_KEY_WRAP_PARAMS); + READ_HANDLE_REQUIRED(key, mechanismParameter, CK_GOSTR3410_KEY_WRAP_PARAMS); + + // Create the mechanism parameters structure + params->pWrapOID = wrapOID; + params->ulWrapOIDLen = wrapOIDLength; + params->pUKM = ukm; + params->ulUKMLen = ukmLength; + params->hKey = key; + + // Set the mechanism parameters + mechanism->pParameter = params; + mechanism->ulParameterLen = sizeof(CK_GOSTR3410_KEY_WRAP_PARAMS); + + return true; +} diff --git a/src/pkcs11.cpp b/src/pkcs11.cpp index f510726..bc0ca59 100644 --- a/src/pkcs11.cpp +++ b/src/pkcs11.cpp @@ -543,16 +543,35 @@ bool get_args_mechanism(napi_env env, napi_value *arg, size_t argc, size_t index { return get_params_pbe(env, mechanismParameter, mechanism); } + case CK_PARAMS_KEY_WRAP_SET_OAEP: + { + return get_params_key_wrap_set_oaep(env, mechanismParameter, mechanism); + } + case CK_PARAMS_GCM: + { + return get_params_gcm(env, mechanismParameter, mechanism); + } + case CK_PARAMS_CCM: + { + return get_params_ccm(env, mechanismParameter, mechanism); + } + case CK_PARAM_GOSTR3410_DERIVE: + { + return get_params_gost_r3410_derive(env, mechanismParameter, mechanism); + } + case CK_PARAM_GOSTR3410_KEY_WRAP: + { + return get_params_gost_r3410_key_wrap(env, mechanismParameter, mechanism); + } + } + else + { + mechanism->pParameter = nullptr; + mechanism->ulParameterLen = 0; } - } - else - { - mechanism->pParameter = nullptr; - mechanism->ulParameterLen = 0; - } - return true; -} + return true; + } /** * @brief A macro that retrieves a mechanism from the argument list at a specified index. @@ -567,127 +586,127 @@ bool get_args_mechanism(napi_env env, napi_value *arg, size_t argc, size_t index return nullptr; \ } -/** - * @brief Get the attributes object from the argument list at a specified index. - * - * @param env The N-API environment. - * @param arg The array of N-API values representing the arguments. - * @param argc The number of arguments in the array. - * @param index The index of the argument to retrieve. - * @param attrs A pointer to store the retrieved attributes. If nullptr, only the length is retrieved. - * @param length A pointer to store the length of the attributes. - * @return true if the attributes were successfully retrieved, false otherwise. - */ -bool get_args_attributes(napi_env env, napi_value *arg, size_t argc, size_t index, AttributesWrapper *attrs, CK_ULONG *length) -{ - ASSERT_ARGS_INDEX(index); - - // check type - // { type: number, value?: number | boolean | string | Buffer }[] - bool isArray; - napi_is_array(env, arg[index], &isArray); - if (!isArray) - { - THROW_TYPE_ERRORF(false, "Argument %lu has wrong type. Should be an Array", index); - } - - // get length - napi_value array = arg[index]; - uint32_t arrayLength; - napi_get_array_length(env, array, &arrayLength); - if (attrs != nullptr && arrayLength != attrs->length) - { - THROW_TYPE_ERRORF(false, "Parameter 'attrs' has wrong length. Should be %lu.", attrs->length); - } - *length = arrayLength; - - if (attrs == nullptr) - { - // only length is required - return true; - } - - // get attributes - for (int i = 0; i < int(arrayLength); i++) + /** + * @brief Get the attributes object from the argument list at a specified index. + * + * @param env The N-API environment. + * @param arg The array of N-API values representing the arguments. + * @param argc The number of arguments in the array. + * @param index The index of the argument to retrieve. + * @param attrs A pointer to store the retrieved attributes. If nullptr, only the length is retrieved. + * @param length A pointer to store the length of the attributes. + * @return true if the attributes were successfully retrieved, false otherwise. + */ + bool get_args_attributes(napi_env env, napi_value * arg, size_t argc, size_t index, AttributesWrapper * attrs, CK_ULONG * length) { - napi_value element; - napi_get_element(env, array, i, &element); + ASSERT_ARGS_INDEX(index); - // check element type - if (!is_object(env, element)) + // check type + // { type: number, value?: number | boolean | string | Buffer }[] + bool isArray; + napi_is_array(env, arg[index], &isArray); + if (!isArray) { - THROW_TYPE_ERRORF(false, "Element %d has wrong type. Should be an Object", i); + THROW_TYPE_ERRORF(false, "Argument %lu has wrong type. Should be an Array", index); } - // type - napi_value typeValue; - napi_get_named_property(env, element, "type", &typeValue); - if (!is_number(env, typeValue)) + // get length + napi_value array = arg[index]; + uint32_t arrayLength; + napi_get_array_length(env, array, &arrayLength); + if (attrs != nullptr && arrayLength != attrs->length) { - THROW_TYPE_ERRORF(false, "Element %d has wrong type. Property 'type' should be a Number", i); + THROW_TYPE_ERRORF(false, "Parameter 'attrs' has wrong length. Should be %lu.", attrs->length); } + *length = arrayLength; - // value - napi_value valueValue; - napi_get_named_property(env, element, "value", &valueValue); - napi_valuetype valueValueType; - napi_typeof(env, valueValue, &valueValueType); - bool valueIsBuffer = false; - napi_is_buffer(env, valueValue, &valueIsBuffer); - if (valueValueType != napi_undefined && // undefined - valueValueType != napi_null && // null - valueValueType != napi_number && // Number - valueValueType != napi_boolean && // Boolean - valueValueType != napi_string && // String - !valueIsBuffer) // Buffer + if (attrs == nullptr) { - THROW_TYPE_ERRORF(false, "Element %d has wrong type. Property 'value' should be a Number, Boolean, String or Buffer", i); + // only length is required + return true; } - CK_ATTRIBUTE_PTR attr = &attrs->attributes[i]; + // get attributes + for (int i = 0; i < int(arrayLength); i++) + { + napi_value element; + napi_get_element(env, array, i, &element); - uint32_t type; - napi_get_value_uint32(env, typeValue, &type); - attr->type = (CK_ATTRIBUTE_TYPE)type; + // check element type + if (!is_object(env, element)) + { + THROW_TYPE_ERRORF(false, "Element %d has wrong type. Should be an Object", i); + } - if (valueValueType == napi_undefined || valueValueType == napi_null) - { - attrs->allocValue(i, 0); - } - else if (valueValueType == napi_number) - { - attrs->allocValue(i, sizeof(CK_ULONG)); - uint32_t value; - napi_get_value_uint32(env, valueValue, &value); - *(CK_ULONG *)attr->pValue = value; - } - else if (valueValueType == napi_boolean) - { - attrs->allocValue(i, sizeof(CK_BBOOL)); - bool value; - napi_get_value_bool(env, valueValue, &value); - *(CK_BBOOL *)attr->pValue = value ? CK_TRUE : CK_FALSE; - } - else if (valueValueType == napi_string) - { - size_t length; - napi_get_value_string_utf8(env, valueValue, nullptr, 0, &length); - attrs->allocValue(i, length + 1); - attrs->attributes[i].ulValueLen = length; // length without null terminator - napi_get_value_string_utf8(env, valueValue, (char *)attr->pValue, length + 1, &length); - } - else if (valueIsBuffer) - { - void *data; - size_t length; - napi_get_buffer_info(env, valueValue, &data, &length); - attrs->allocValue(i, length); - memcpy(attr->pValue, data, length); + // type + napi_value typeValue; + napi_get_named_property(env, element, "type", &typeValue); + if (!is_number(env, typeValue)) + { + THROW_TYPE_ERRORF(false, "Element %d has wrong type. Property 'type' should be a Number", i); + } + + // value + napi_value valueValue; + napi_get_named_property(env, element, "value", &valueValue); + napi_valuetype valueValueType; + napi_typeof(env, valueValue, &valueValueType); + bool valueIsBuffer = false; + napi_is_buffer(env, valueValue, &valueIsBuffer); + if (valueValueType != napi_undefined && // undefined + valueValueType != napi_null && // null + valueValueType != napi_number && // Number + valueValueType != napi_boolean && // Boolean + valueValueType != napi_string && // String + !valueIsBuffer) // Buffer + { + THROW_TYPE_ERRORF(false, "Element %d has wrong type. Property 'value' should be a Number, Boolean, String or Buffer", i); + } + + CK_ATTRIBUTE_PTR attr = &attrs->attributes[i]; + + uint32_t type; + napi_get_value_uint32(env, typeValue, &type); + attr->type = (CK_ATTRIBUTE_TYPE)type; + + if (valueValueType == napi_undefined || valueValueType == napi_null) + { + attrs->allocValue(i, 0); + } + else if (valueValueType == napi_number) + { + attrs->allocValue(i, sizeof(CK_ULONG)); + uint32_t value; + napi_get_value_uint32(env, valueValue, &value); + *(CK_ULONG *)attr->pValue = value; + } + else if (valueValueType == napi_boolean) + { + attrs->allocValue(i, sizeof(CK_BBOOL)); + bool value; + napi_get_value_bool(env, valueValue, &value); + *(CK_BBOOL *)attr->pValue = value ? CK_TRUE : CK_FALSE; + } + else if (valueValueType == napi_string) + { + size_t length; + napi_get_value_string_utf8(env, valueValue, nullptr, 0, &length); + attrs->allocValue(i, length + 1); + attrs->attributes[i].ulValueLen = length; // length without null terminator + napi_get_value_string_utf8(env, valueValue, (char *)attr->pValue, length + 1, &length); + } + else if (valueIsBuffer) + { + void *data; + size_t length; + napi_get_buffer_info(env, valueValue, &data, &length); + attrs->allocValue(i, length); + memcpy(attr->pValue, data, length); + } } - } - return true; -} + return true; + } /** * @brief A macro that retrieves attributes from the argument list at a specified index. @@ -717,29 +736,29 @@ bool get_args_attributes(napi_env env, napi_value *arg, size_t argc, size_t inde THROW_TYPE_ERRORF(nullptr, "Argument %lu has wrong type. Should be a Function", index); \ } -/** - * @brief Get a list of arguments from the function call. - * - * @param env The N-API environment. - * @param info The N-API callback info. - * @param argc The number of arguments to retrieve. - * @param arg A pointer to store the retrieved arguments. - * @return true if the arguments were successfully retrieved, false otherwise. - */ -bool get_args(napi_env env, napi_callback_info info, size_t argc, napi_value *arg) -{ - napi_value jsthis; - size_t length = 0; - napi_get_cb_info(env, info, &length, nullptr, &jsthis, nullptr); - if (length != argc) + /** + * @brief Get a list of arguments from the function call. + * + * @param env The N-API environment. + * @param info The N-API callback info. + * @param argc The number of arguments to retrieve. + * @param arg A pointer to store the retrieved arguments. + * @return true if the arguments were successfully retrieved, false otherwise. + */ + bool get_args(napi_env env, napi_callback_info info, size_t argc, napi_value * arg) { - THROW_TYPE_ERRORF(false, "Parameters are required. Expected %lu arguments, but received %lu.", argc, length); - } + napi_value jsthis; + size_t length = 0; + napi_get_cb_info(env, info, &length, nullptr, &jsthis, nullptr); + if (length != argc) + { + THROW_TYPE_ERRORF(false, "Parameters are required. Expected %lu arguments, but received %lu.", argc, length); + } - napi_get_cb_info(env, info, &length, arg, &jsthis, nullptr); + napi_get_cb_info(env, info, &length, arg, &jsthis, nullptr); - return true; -} + return true; + } /** * @brief A macro that retrieves a list of arguments from the function call. @@ -781,1927 +800,1927 @@ bool get_args(napi_env env, napi_callback_info info, size_t argc, napi_value *ar return nullptr; \ } -class Pkcs11 -{ -public: - void *handle; - CK_FUNCTION_LIST_PTR functionList; - - Pkcs11() : handle(nullptr), functionList(nullptr) {} - - ~Pkcs11() - { - if (handle != nullptr) - { - dlclose(handle); - handle = nullptr; - } - } - - static napi_value Constructor(napi_env env, napi_callback_info info) + class Pkcs11 { - napi_value target; - napi_get_new_target(env, info, &target); + public: + void *handle; + CK_FUNCTION_LIST_PTR functionList; - bool isConstructor = target != nullptr; + Pkcs11() : handle(nullptr), functionList(nullptr) {} - if (isConstructor) + ~Pkcs11() { - napi_value jsthis; - napi_get_cb_info(env, info, nullptr, nullptr, &jsthis, nullptr); - - Pkcs11 *pkcs11 = new Pkcs11(); - napi_wrap(env, jsthis, pkcs11, Pkcs11::Destructor, nullptr, nullptr); - - return jsthis; + if (handle != nullptr) + { + dlclose(handle); + handle = nullptr; + } } - else - { - napi_value cons; - napi_get_reference_value(env, constructorRef, &cons); - - napi_value instance; - napi_new_instance(env, cons, 0, nullptr, &instance); - return instance; - } - } + static napi_value Constructor(napi_env env, napi_callback_info info) + { + napi_value target; + napi_get_new_target(env, info, &target); - static void Destructor(napi_env env, void *nativeObject, void *finalize_hint) - { - Pkcs11 *pkcs11 = static_cast(nativeObject); - pkcs11->~Pkcs11(); - } + bool isConstructor = target != nullptr; - /** - * @brief Loads the PKCS11 module. - * - * @param env The N-API environment. - * @param info The N-API callback info. - * @return The loaded PKCS11 module. - */ - static napi_value Load(napi_env env, napi_callback_info info) - { - napi_value jsthis; - napi_get_cb_info(env, info, nullptr, nullptr, &jsthis, nullptr); + if (isConstructor) + { + napi_value jsthis; + napi_get_cb_info(env, info, nullptr, nullptr, &jsthis, nullptr); - Pkcs11 *pkcs11; - napi_unwrap(env, jsthis, (void **)&pkcs11); + Pkcs11 *pkcs11 = new Pkcs11(); + napi_wrap(env, jsthis, pkcs11, Pkcs11::Destructor, nullptr, nullptr); - size_t argc = 1; - napi_value arg[1]; - napi_get_cb_info(env, info, &argc, arg, nullptr, nullptr); + return jsthis; + } + else + { + napi_value cons; + napi_get_reference_value(env, constructorRef, &cons); - size_t length; - napi_get_value_string_utf8(env, arg[0], nullptr, 0, &length); + napi_value instance; + napi_new_instance(env, cons, 0, nullptr, &instance); - std::vector path(length + 1); - char *pPath = path.data(); - napi_get_value_string_utf8(env, arg[0], pPath, length + 1, &length); + return instance; + } + } - pkcs11->handle = dlopen(pPath, RTLD_LAZY); - if (pkcs11->handle == nullptr) + static void Destructor(napi_env env, void *nativeObject, void *finalize_hint) { - napi_throw_error(env, nullptr, dlerror()); - return nullptr; + Pkcs11 *pkcs11 = static_cast(nativeObject); + pkcs11->~Pkcs11(); } - CK_C_GetFunctionList pC_GetFunctionList = (CK_C_GetFunctionList)dlsym(pkcs11->handle, "C_GetFunctionList"); - if (pC_GetFunctionList == nullptr) + /** + * @brief Loads the PKCS11 module. + * + * @param env The N-API environment. + * @param info The N-API callback info. + * @return The loaded PKCS11 module. + */ + static napi_value Load(napi_env env, napi_callback_info info) { - napi_throw_error(env, nullptr, dlerror()); - return nullptr; - } + napi_value jsthis; + napi_get_cb_info(env, info, nullptr, nullptr, &jsthis, nullptr); - CK_RV rv = pC_GetFunctionList(&pkcs11->functionList); - ASSERT_RV(rv); + Pkcs11 *pkcs11; + napi_unwrap(env, jsthis, (void **)&pkcs11); - return nullptr; - } + size_t argc = 1; + napi_value arg[1]; + napi_get_cb_info(env, info, &argc, arg, nullptr, nullptr); - /** - * @brief Closes the PKCS11 module. - * - * @param env The N-API environment. - * @param info The N-API callback info. - * @return Nothing. - */ - static napi_value Close(napi_env env, napi_callback_info info) - { - napi_value jsthis; - napi_get_cb_info(env, info, nullptr, nullptr, &jsthis, nullptr); + size_t length; + napi_get_value_string_utf8(env, arg[0], nullptr, 0, &length); - Pkcs11 *pkcs11; - napi_unwrap(env, jsthis, (void **)&pkcs11); + std::vector path(length + 1); + char *pPath = path.data(); + napi_get_value_string_utf8(env, arg[0], pPath, length + 1, &length); - if (pkcs11->handle != nullptr) - { - dlclose(pkcs11->handle); - pkcs11->handle = nullptr; - } + pkcs11->handle = dlopen(pPath, RTLD_LAZY); + if (pkcs11->handle == nullptr) + { + napi_throw_error(env, nullptr, dlerror()); + return nullptr; + } - return nullptr; - } + CK_C_GetFunctionList pC_GetFunctionList = (CK_C_GetFunctionList)dlsym(pkcs11->handle, "C_GetFunctionList"); + if (pC_GetFunctionList == nullptr) + { + napi_throw_error(env, nullptr, dlerror()); + return nullptr; + } - static napi_value C_Initialize(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); + CK_RV rv = pC_GetFunctionList(&pkcs11->functionList); + ASSERT_RV(rv); - // Read arguments - size_t argc = 1; - napi_value arg[1]; - napi_get_cb_info(env, info, &argc, arg, nullptr, nullptr); + return nullptr; + } - CK_VOID_PTR pInitArgs = nullptr; - CK_NSS_C_INITIALIZE_ARGS nssInitArgs = {nullptr, nullptr, nullptr, nullptr, 0, nullptr, nullptr}; - CK_C_INITIALIZE_ARGS initArgs = {nullptr, nullptr, nullptr, nullptr, 0, nullptr}; - char *path = nullptr; - if (argc > 0 && !is_empty(env, arg[0])) + /** + * @brief Closes the PKCS11 module. + * + * @param env The N-API environment. + * @param info The N-API callback info. + * @return Nothing. + */ + static napi_value Close(napi_env env, napi_callback_info info) { - napi_valuetype type; - napi_typeof(env, arg[0], &type); + napi_value jsthis; + napi_get_cb_info(env, info, nullptr, nullptr, &jsthis, nullptr); - if (type != napi_object) + Pkcs11 *pkcs11; + napi_unwrap(env, jsthis, (void **)&pkcs11); + + if (pkcs11->handle != nullptr) { - THROW_TYPE_ERRORF(nullptr, "Argument %lu has wrong type. Should be an Object", 0); + dlclose(pkcs11->handle); + pkcs11->handle = nullptr; } - // Read common C_Initialize args - napi_value flags; - napi_get_named_property(env, arg[0], "flags", &flags); - uint32_t ckFlags; - napi_get_value_uint32(env, flags, &ckFlags); + return nullptr; + } - bool hasLibraryParameters; - napi_has_named_property(env, arg[0], "libraryParameters", &hasLibraryParameters); - if (hasLibraryParameters) + static napi_value C_Initialize(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); + + // Read arguments + size_t argc = 1; + napi_value arg[1]; + napi_get_cb_info(env, info, &argc, arg, nullptr, nullptr); + + CK_VOID_PTR pInitArgs = nullptr; + CK_NSS_C_INITIALIZE_ARGS nssInitArgs = {nullptr, nullptr, nullptr, nullptr, 0, nullptr, nullptr}; + CK_C_INITIALIZE_ARGS initArgs = {nullptr, nullptr, nullptr, nullptr, 0, nullptr}; + char *path = nullptr; + if (argc > 0 && !is_empty(env, arg[0])) { - // Read NSS C_Initialize args - napi_value libraryParameters; - napi_get_named_property(env, arg[0], "libraryParameters", &libraryParameters); napi_valuetype type; - napi_typeof(env, libraryParameters, &type); + napi_typeof(env, arg[0], &type); - if (type != napi_string) + if (type != napi_object) { - THROW_TYPE_ERRORF(nullptr, "Argument %lu has wrong type. Property 'libraryParameters' should be a String", 0); + THROW_TYPE_ERRORF(nullptr, "Argument %lu has wrong type. Should be an Object", 0); } - size_t length; - napi_get_value_string_utf8(env, libraryParameters, nullptr, 0, &length); + // Read common C_Initialize args + napi_value flags; + napi_get_named_property(env, arg[0], "flags", &flags); + uint32_t ckFlags; + napi_get_value_uint32(env, flags, &ckFlags); + + bool hasLibraryParameters; + napi_has_named_property(env, arg[0], "libraryParameters", &hasLibraryParameters); + if (hasLibraryParameters) + { + // Read NSS C_Initialize args + napi_value libraryParameters; + napi_get_named_property(env, arg[0], "libraryParameters", &libraryParameters); + napi_valuetype type; + napi_typeof(env, libraryParameters, &type); + + if (type != napi_string) + { + THROW_TYPE_ERRORF(nullptr, "Argument %lu has wrong type. Property 'libraryParameters' should be a String", 0); + } + + size_t length; + napi_get_value_string_utf8(env, libraryParameters, nullptr, 0, &length); - path = new char[length + 1]; - napi_get_value_string_utf8(env, libraryParameters, path, length + 1, &length); + path = new char[length + 1]; + napi_get_value_string_utf8(env, libraryParameters, path, length + 1, &length); - nssInitArgs.LibraryParameters = (CK_CHAR_PTR)path; - nssInitArgs.flags = (CK_FLAGS)ckFlags; + nssInitArgs.LibraryParameters = (CK_CHAR_PTR)path; + nssInitArgs.flags = (CK_FLAGS)ckFlags; - pInitArgs = &nssInitArgs; + pInitArgs = &nssInitArgs; + } + else + { + // Read common C_Initialize args + initArgs.flags = (CK_FLAGS)ckFlags; + + pInitArgs = &initArgs; + } } - else - { - // Read common C_Initialize args - initArgs.flags = (CK_FLAGS)ckFlags; + if (pInitArgs == nullptr) + { pInitArgs = &initArgs; } - } - if (pInitArgs == nullptr) - { - pInitArgs = &initArgs; + // Call PKCS11 function + CK_RV rv = pkcs11->functionList->C_Initialize(pInitArgs); + if (path != nullptr) + { + delete[] path; + } + ASSERT_RV(rv); + + return nullptr; } - // Call PKCS11 function - CK_RV rv = pkcs11->functionList->C_Initialize(pInitArgs); - if (path != nullptr) + static napi_value C_Finalize(napi_env env, napi_callback_info info) { - delete[] path; - } - ASSERT_RV(rv); + UNWRAP_PKCS11(); - return nullptr; - } + // Call PKCS11 function + CK_RV rv = pkcs11->functionList->C_Finalize(nullptr); + ASSERT_RV(rv); - static napi_value C_Finalize(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); + return nullptr; + } - // Call PKCS11 function - CK_RV rv = pkcs11->functionList->C_Finalize(nullptr); - ASSERT_RV(rv); + static napi_value C_GetInfo(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); - return nullptr; - } + // Call PKCS11 function + CK_INFO ckInfo; + CK_RV rv = pkcs11->functionList->C_GetInfo(&ckInfo); + ASSERT_RV(rv); - static napi_value C_GetInfo(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); + // Create result object + napi_value result; + napi_create_object(env, &result); + + napi_value cryptokiVersion = create_version(env, ckInfo.cryptokiVersion); + napi_set_named_property(env, result, "cryptokiVersion", cryptokiVersion); - // Call PKCS11 function - CK_INFO ckInfo; - CK_RV rv = pkcs11->functionList->C_GetInfo(&ckInfo); - ASSERT_RV(rv); + napi_value manufacturerID; + napi_create_string_utf8(env, (char *)&ckInfo.manufacturerID[0], sizeof(ckInfo.manufacturerID), &manufacturerID); + napi_set_named_property(env, result, "manufacturerID", manufacturerID); - // Create result object - napi_value result; - napi_create_object(env, &result); + napi_value flags; + napi_create_uint32(env, ckInfo.flags, &flags); + napi_set_named_property(env, result, "flags", flags); - napi_value cryptokiVersion = create_version(env, ckInfo.cryptokiVersion); - napi_set_named_property(env, result, "cryptokiVersion", cryptokiVersion); + napi_value libraryDescription; + napi_create_string_utf8(env, (char *)&ckInfo.libraryDescription[0], sizeof(ckInfo.libraryDescription), &libraryDescription); + napi_set_named_property(env, result, "libraryDescription", libraryDescription); - napi_value manufacturerID; - napi_create_string_utf8(env, (char *)&ckInfo.manufacturerID[0], sizeof(ckInfo.manufacturerID), &manufacturerID); - napi_set_named_property(env, result, "manufacturerID", manufacturerID); + napi_value libraryVersion = create_version(env, ckInfo.libraryVersion); + napi_set_named_property(env, result, "libraryVersion", libraryVersion); - napi_value flags; - napi_create_uint32(env, ckInfo.flags, &flags); - napi_set_named_property(env, result, "flags", flags); + return result; + } - napi_value libraryDescription; - napi_create_string_utf8(env, (char *)&ckInfo.libraryDescription[0], sizeof(ckInfo.libraryDescription), &libraryDescription); - napi_set_named_property(env, result, "libraryDescription", libraryDescription); + static napi_value C_GetSlotList(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); + + // Read arguments + CK_BBOOL ckTokenPresent = CK_FALSE; + size_t argc = 1; + napi_value arg[1]; + napi_get_cb_info(env, info, &argc, arg, nullptr, nullptr); + if (argc > 0) + { + napi_valuetype type; + napi_typeof(env, arg[0], &type); - napi_value libraryVersion = create_version(env, ckInfo.libraryVersion); - napi_set_named_property(env, result, "libraryVersion", libraryVersion); + if (type != napi_boolean) + { + THROW_TYPE_ERRORF(nullptr, "Argument %lu has wrong type. Should be a Boolean", 0); + } - return result; - } + bool temp; + napi_get_value_bool(env, arg[0], &temp); + ckTokenPresent = temp; + } - static napi_value C_GetSlotList(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); + // Call PKCS11 function + CK_ULONG slotCount; + CK_RV rv = pkcs11->functionList->C_GetSlotList(ckTokenPresent, nullptr, &slotCount); // get slot count + ASSERT_RV(rv); - // Read arguments - CK_BBOOL ckTokenPresent = CK_FALSE; - size_t argc = 1; - napi_value arg[1]; - napi_get_cb_info(env, info, &argc, arg, nullptr, nullptr); - if (argc > 0) - { - napi_valuetype type; - napi_typeof(env, arg[0], &type); + std::vector slotList(slotCount); + CK_SLOT_ID_PTR pSlotList = slotList.data(); + rv = pkcs11->functionList->C_GetSlotList(ckTokenPresent, pSlotList, &slotCount); + ASSERT_RV(rv); - if (type != napi_boolean) + // Create result array + napi_value result; + napi_create_array(env, &result); + for (int i = 0; i < int(slotCount); i++) { - THROW_TYPE_ERRORF(nullptr, "Argument %lu has wrong type. Should be a Boolean", 0); + napi_value slotId; + napi_create_buffer_copy(env, sizeof(CK_SLOT_ID), &pSlotList[i], nullptr, &slotId); + napi_set_element(env, result, i, slotId); } - bool temp; - napi_get_value_bool(env, arg[0], &temp); - ckTokenPresent = temp; + return result; } - // Call PKCS11 function - CK_ULONG slotCount; - CK_RV rv = pkcs11->functionList->C_GetSlotList(ckTokenPresent, nullptr, &slotCount); // get slot count - ASSERT_RV(rv); + static napi_value C_GetSlotInfo(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); + GET_ARGS(1, arg) - std::vector slotList(slotCount); - CK_SLOT_ID_PTR pSlotList = slotList.data(); - rv = pkcs11->functionList->C_GetSlotList(ckTokenPresent, pSlotList, &slotCount); - ASSERT_RV(rv); + // Read arguments + GET_ARGS_SLOT_ID(0, slotId) - // Create result array - napi_value result; - napi_create_array(env, &result); - for (int i = 0; i < int(slotCount); i++) - { - napi_value slotId; - napi_create_buffer_copy(env, sizeof(CK_SLOT_ID), &pSlotList[i], nullptr, &slotId); - napi_set_element(env, result, i, slotId); - } + // Call PKCS11 function + CK_SLOT_INFO ckSlotInfo; + CK_RV rv = pkcs11->functionList->C_GetSlotInfo(slotId, &ckSlotInfo); + ASSERT_RV(rv); - return result; - } + // Create result object + napi_value result; + napi_create_object(env, &result); - static napi_value C_GetSlotInfo(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); - GET_ARGS(1, arg) + napi_value slotDescription; + napi_create_string_utf8(env, (char *)&ckSlotInfo.slotDescription[0], sizeof(ckSlotInfo.slotDescription), &slotDescription); + napi_set_named_property(env, result, "slotDescription", slotDescription); - // Read arguments - GET_ARGS_SLOT_ID(0, slotId) + napi_value manufacturerID; + napi_create_string_utf8(env, (char *)&ckSlotInfo.manufacturerID[0], sizeof(ckSlotInfo.manufacturerID), &manufacturerID); + napi_set_named_property(env, result, "manufacturerID", manufacturerID); - // Call PKCS11 function - CK_SLOT_INFO ckSlotInfo; - CK_RV rv = pkcs11->functionList->C_GetSlotInfo(slotId, &ckSlotInfo); - ASSERT_RV(rv); + napi_value flags; + napi_create_uint32(env, ckSlotInfo.flags, &flags); + napi_set_named_property(env, result, "flags", flags); - // Create result object - napi_value result; - napi_create_object(env, &result); + napi_value hardwareVersion = create_version(env, ckSlotInfo.hardwareVersion); + napi_set_named_property(env, result, "hardwareVersion", hardwareVersion); - napi_value slotDescription; - napi_create_string_utf8(env, (char *)&ckSlotInfo.slotDescription[0], sizeof(ckSlotInfo.slotDescription), &slotDescription); - napi_set_named_property(env, result, "slotDescription", slotDescription); + napi_value firmwareVersion = create_version(env, ckSlotInfo.firmwareVersion); + napi_set_named_property(env, result, "firmwareVersion", firmwareVersion); - napi_value manufacturerID; - napi_create_string_utf8(env, (char *)&ckSlotInfo.manufacturerID[0], sizeof(ckSlotInfo.manufacturerID), &manufacturerID); - napi_set_named_property(env, result, "manufacturerID", manufacturerID); + return result; + } - napi_value flags; - napi_create_uint32(env, ckSlotInfo.flags, &flags); - napi_set_named_property(env, result, "flags", flags); + static napi_value C_GetTokenInfo(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); + GET_ARGS(1, arg) - napi_value hardwareVersion = create_version(env, ckSlotInfo.hardwareVersion); - napi_set_named_property(env, result, "hardwareVersion", hardwareVersion); + // Read arguments + GET_ARGS_SLOT_ID(0, slotId) - napi_value firmwareVersion = create_version(env, ckSlotInfo.firmwareVersion); - napi_set_named_property(env, result, "firmwareVersion", firmwareVersion); + // Call PKCS11 function + CK_TOKEN_INFO ckTokenInfo; + CK_RV rv = pkcs11->functionList->C_GetTokenInfo(slotId, &ckTokenInfo); + ASSERT_RV(rv); - return result; - } + // Create result object + napi_value result; + napi_create_object(env, &result); - static napi_value C_GetTokenInfo(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); - GET_ARGS(1, arg) - - // Read arguments - GET_ARGS_SLOT_ID(0, slotId) - - // Call PKCS11 function - CK_TOKEN_INFO ckTokenInfo; - CK_RV rv = pkcs11->functionList->C_GetTokenInfo(slotId, &ckTokenInfo); - ASSERT_RV(rv); - - // Create result object - napi_value result; - napi_create_object(env, &result); - - // label - napi_value label; - napi_create_string_utf8(env, (char *)&ckTokenInfo.label[0], sizeof(ckTokenInfo.label), &label); - napi_set_named_property(env, result, "label", label); - - // manufacturerID - napi_value manufacturerID; - napi_create_string_utf8(env, (char *)&ckTokenInfo.manufacturerID[0], sizeof(ckTokenInfo.manufacturerID), &manufacturerID); - napi_set_named_property(env, result, "manufacturerID", manufacturerID); - - // model - napi_value model; - napi_create_string_utf8(env, (char *)&ckTokenInfo.model[0], sizeof(ckTokenInfo.model), &model); - napi_set_named_property(env, result, "model", model); - - // serialNumber - napi_value serialNumber; - napi_create_string_utf8(env, (char *)&ckTokenInfo.serialNumber[0], sizeof(ckTokenInfo.serialNumber), &serialNumber); - napi_set_named_property(env, result, "serialNumber", serialNumber); - - // flags - napi_value flags; - napi_create_uint32(env, ckTokenInfo.flags, &flags); - napi_set_named_property(env, result, "flags", flags); - - // maxSessionCount - napi_value ulMaxSessionCount; - napi_create_uint32(env, ckTokenInfo.ulMaxSessionCount, &ulMaxSessionCount); - napi_set_named_property(env, result, "maxSessionCount", ulMaxSessionCount); - - // sessionCount - napi_value ulSessionCount; - napi_create_uint32(env, ckTokenInfo.ulSessionCount, &ulSessionCount); - napi_set_named_property(env, result, "sessionCount", ulSessionCount); - - // maxRwSessionCount - napi_value ulMaxRwSessionCount; - napi_create_uint32(env, ckTokenInfo.ulMaxRwSessionCount, &ulMaxRwSessionCount); - napi_set_named_property(env, result, "maxRwSessionCount", ulMaxRwSessionCount); - - // rwSessionCount - napi_value ulRwSessionCount; - napi_create_uint32(env, ckTokenInfo.ulRwSessionCount, &ulRwSessionCount); - napi_set_named_property(env, result, "rwSessionCount", ulRwSessionCount); - - // maxPinLen - napi_value ulMaxPinLen; - napi_create_uint32(env, ckTokenInfo.ulMaxPinLen, &ulMaxPinLen); - napi_set_named_property(env, result, "maxPinLen", ulMaxPinLen); - - // minPinLen - napi_value ulMinPinLen; - napi_create_uint32(env, ckTokenInfo.ulMinPinLen, &ulMinPinLen); - napi_set_named_property(env, result, "minPinLen", ulMinPinLen); - - // hardwareVersion - napi_value hardwareVersion = create_version(env, ckTokenInfo.hardwareVersion); - napi_set_named_property(env, result, "hardwareVersion", hardwareVersion); - - // firmwareVersion - napi_value firmwareVersion = create_version(env, ckTokenInfo.firmwareVersion); - napi_set_named_property(env, result, "firmwareVersion", firmwareVersion); - - // utcTime - napi_value utcTime = create_date_utc_property(env, ckTokenInfo.utcTime); - napi_set_named_property(env, result, "utcTime", utcTime); - - // totalPublicMemory - napi_value totalPublicMemory; - napi_create_bigint_uint64(env, ckTokenInfo.ulTotalPublicMemory, &totalPublicMemory); - napi_set_named_property(env, result, "totalPublicMemory", totalPublicMemory); - - // freePublicMemory - napi_value freePublicMemory; - napi_create_bigint_uint64(env, ckTokenInfo.ulFreePublicMemory, &freePublicMemory); - napi_set_named_property(env, result, "freePublicMemory", freePublicMemory); - - // totalPrivateMemory - napi_value totalPrivateMemory; - napi_create_bigint_uint64(env, ckTokenInfo.ulTotalPrivateMemory, &totalPrivateMemory); - napi_set_named_property(env, result, "totalPrivateMemory", totalPrivateMemory); - - // freePrivateMemory - napi_value freePrivateMemory; - napi_create_bigint_uint64(env, ckTokenInfo.ulFreePrivateMemory, &freePrivateMemory); - napi_set_named_property(env, result, "freePrivateMemory", freePrivateMemory); - - return result; - } + // label + napi_value label; + napi_create_string_utf8(env, (char *)&ckTokenInfo.label[0], sizeof(ckTokenInfo.label), &label); + napi_set_named_property(env, result, "label", label); - static napi_value C_GetMechanismList(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); - GET_ARGS(1, arg) + // manufacturerID + napi_value manufacturerID; + napi_create_string_utf8(env, (char *)&ckTokenInfo.manufacturerID[0], sizeof(ckTokenInfo.manufacturerID), &manufacturerID); + napi_set_named_property(env, result, "manufacturerID", manufacturerID); - // Read arguments - GET_ARGS_SLOT_ID(0, slotId) + // model + napi_value model; + napi_create_string_utf8(env, (char *)&ckTokenInfo.model[0], sizeof(ckTokenInfo.model), &model); + napi_set_named_property(env, result, "model", model); - // Call PKCS11 function - CK_ULONG mechanismCount; - CK_RV rv = pkcs11->functionList->C_GetMechanismList(slotId, nullptr, &mechanismCount); // get mechanism count - ASSERT_RV(rv); + // serialNumber + napi_value serialNumber; + napi_create_string_utf8(env, (char *)&ckTokenInfo.serialNumber[0], sizeof(ckTokenInfo.serialNumber), &serialNumber); + napi_set_named_property(env, result, "serialNumber", serialNumber); - std::vector mechanismList(mechanismCount); - CK_MECHANISM_TYPE_PTR pMechanismList = mechanismList.data(); - rv = pkcs11->functionList->C_GetMechanismList(slotId, pMechanismList, &mechanismCount); - ASSERT_RV(rv); + // flags + napi_value flags; + napi_create_uint32(env, ckTokenInfo.flags, &flags); + napi_set_named_property(env, result, "flags", flags); + + // maxSessionCount + napi_value ulMaxSessionCount; + napi_create_uint32(env, ckTokenInfo.ulMaxSessionCount, &ulMaxSessionCount); + napi_set_named_property(env, result, "maxSessionCount", ulMaxSessionCount); + + // sessionCount + napi_value ulSessionCount; + napi_create_uint32(env, ckTokenInfo.ulSessionCount, &ulSessionCount); + napi_set_named_property(env, result, "sessionCount", ulSessionCount); + + // maxRwSessionCount + napi_value ulMaxRwSessionCount; + napi_create_uint32(env, ckTokenInfo.ulMaxRwSessionCount, &ulMaxRwSessionCount); + napi_set_named_property(env, result, "maxRwSessionCount", ulMaxRwSessionCount); + + // rwSessionCount + napi_value ulRwSessionCount; + napi_create_uint32(env, ckTokenInfo.ulRwSessionCount, &ulRwSessionCount); + napi_set_named_property(env, result, "rwSessionCount", ulRwSessionCount); + + // maxPinLen + napi_value ulMaxPinLen; + napi_create_uint32(env, ckTokenInfo.ulMaxPinLen, &ulMaxPinLen); + napi_set_named_property(env, result, "maxPinLen", ulMaxPinLen); + + // minPinLen + napi_value ulMinPinLen; + napi_create_uint32(env, ckTokenInfo.ulMinPinLen, &ulMinPinLen); + napi_set_named_property(env, result, "minPinLen", ulMinPinLen); + + // hardwareVersion + napi_value hardwareVersion = create_version(env, ckTokenInfo.hardwareVersion); + napi_set_named_property(env, result, "hardwareVersion", hardwareVersion); + + // firmwareVersion + napi_value firmwareVersion = create_version(env, ckTokenInfo.firmwareVersion); + napi_set_named_property(env, result, "firmwareVersion", firmwareVersion); + + // utcTime + napi_value utcTime = create_date_utc_property(env, ckTokenInfo.utcTime); + napi_set_named_property(env, result, "utcTime", utcTime); + + // totalPublicMemory + napi_value totalPublicMemory; + napi_create_bigint_uint64(env, ckTokenInfo.ulTotalPublicMemory, &totalPublicMemory); + napi_set_named_property(env, result, "totalPublicMemory", totalPublicMemory); + + // freePublicMemory + napi_value freePublicMemory; + napi_create_bigint_uint64(env, ckTokenInfo.ulFreePublicMemory, &freePublicMemory); + napi_set_named_property(env, result, "freePublicMemory", freePublicMemory); + + // totalPrivateMemory + napi_value totalPrivateMemory; + napi_create_bigint_uint64(env, ckTokenInfo.ulTotalPrivateMemory, &totalPrivateMemory); + napi_set_named_property(env, result, "totalPrivateMemory", totalPrivateMemory); + + // freePrivateMemory + napi_value freePrivateMemory; + napi_create_bigint_uint64(env, ckTokenInfo.ulFreePrivateMemory, &freePrivateMemory); + napi_set_named_property(env, result, "freePrivateMemory", freePrivateMemory); + + return result; + } - // Create result array - napi_value result; - napi_create_array(env, &result); - for (int i = 0; i < int(mechanismCount); i++) + static napi_value C_GetMechanismList(napi_env env, napi_callback_info info) { - napi_value mechanism; - napi_create_uint32(env, pMechanismList[i], &mechanism); - napi_set_element(env, result, i, mechanism); - } + UNWRAP_PKCS11(); + GET_ARGS(1, arg) - return result; - } + // Read arguments + GET_ARGS_SLOT_ID(0, slotId) - static napi_value C_GetMechanismInfo(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); - GET_ARGS(2, arg) + // Call PKCS11 function + CK_ULONG mechanismCount; + CK_RV rv = pkcs11->functionList->C_GetMechanismList(slotId, nullptr, &mechanismCount); // get mechanism count + ASSERT_RV(rv); - // Read arguments - GET_ARGS_SLOT_ID(0, slotId) - GET_ARGS_MECHANISM_TYPE(1, mechanismType) + std::vector mechanismList(mechanismCount); + CK_MECHANISM_TYPE_PTR pMechanismList = mechanismList.data(); + rv = pkcs11->functionList->C_GetMechanismList(slotId, pMechanismList, &mechanismCount); + ASSERT_RV(rv); - // Call PKCS11 function - CK_MECHANISM_INFO ckMechanismInfo; - CK_RV rv = pkcs11->functionList->C_GetMechanismInfo(slotId, mechanismType, &ckMechanismInfo); - ASSERT_RV(rv); + // Create result array + napi_value result; + napi_create_array(env, &result); + for (int i = 0; i < int(mechanismCount); i++) + { + napi_value mechanism; + napi_create_uint32(env, pMechanismList[i], &mechanism); + napi_set_element(env, result, i, mechanism); + } - // Create result object - napi_value result; - napi_create_object(env, &result); + return result; + } - napi_value minKeySize; - napi_create_uint32(env, ckMechanismInfo.ulMinKeySize, &minKeySize); - napi_set_named_property(env, result, "minKeySize", minKeySize); + static napi_value C_GetMechanismInfo(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); + GET_ARGS(2, arg) - napi_value maxKeySize; - napi_create_uint32(env, ckMechanismInfo.ulMaxKeySize, &maxKeySize); - napi_set_named_property(env, result, "maxKeySize", maxKeySize); + // Read arguments + GET_ARGS_SLOT_ID(0, slotId) + GET_ARGS_MECHANISM_TYPE(1, mechanismType) - napi_value flags; - napi_create_uint32(env, ckMechanismInfo.flags, &flags); - napi_set_named_property(env, result, "flags", flags); + // Call PKCS11 function + CK_MECHANISM_INFO ckMechanismInfo; + CK_RV rv = pkcs11->functionList->C_GetMechanismInfo(slotId, mechanismType, &ckMechanismInfo); + ASSERT_RV(rv); - return result; - } + // Create result object + napi_value result; + napi_create_object(env, &result); - static napi_value C_InitToken(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); - GET_ARGS(3, arg) + napi_value minKeySize; + napi_create_uint32(env, ckMechanismInfo.ulMinKeySize, &minKeySize); + napi_set_named_property(env, result, "minKeySize", minKeySize); - // Read arguments - GET_ARGS_SLOT_ID(0, slotId) - GET_ARGS_STRING(1, pin) - GET_ARGS_STRING(2, label) + napi_value maxKeySize; + napi_create_uint32(env, ckMechanismInfo.ulMaxKeySize, &maxKeySize); + napi_set_named_property(env, result, "maxKeySize", maxKeySize); - // Call PKCS11 function - CK_RV rv = pkcs11->functionList->C_InitToken(slotId, (CK_UTF8CHAR_PTR)pin, (CK_ULONG)pinLength, (CK_UTF8CHAR_PTR)label); - ASSERT_RV(rv); + napi_value flags; + napi_create_uint32(env, ckMechanismInfo.flags, &flags); + napi_set_named_property(env, result, "flags", flags); - return nullptr; - } + return result; + } - static napi_value C_InitPIN(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); - GET_ARGS(2, arg) + static napi_value C_InitToken(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); + GET_ARGS(3, arg) - // Read arguments - GET_ARGS_SESSION_HANDLE(0, sessionHandle) - GET_ARGS_STRING(1, pin) + // Read arguments + GET_ARGS_SLOT_ID(0, slotId) + GET_ARGS_STRING(1, pin) + GET_ARGS_STRING(2, label) - // Call PKCS11 function - CK_RV rv = pkcs11->functionList->C_InitPIN(sessionHandle, (CK_UTF8CHAR_PTR)pin, (CK_ULONG)pinLength); - ASSERT_RV(rv); + // Call PKCS11 function + CK_RV rv = pkcs11->functionList->C_InitToken(slotId, (CK_UTF8CHAR_PTR)pin, (CK_ULONG)pinLength, (CK_UTF8CHAR_PTR)label); + ASSERT_RV(rv); - return nullptr; - } + return nullptr; + } - static napi_value C_SetPIN(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); - GET_ARGS(3, arg) - - // Read arguments - GET_ARGS_SESSION_HANDLE(0, sessionHandle) - GET_ARGS_STRING(1, oldPin) - GET_ARGS_STRING(2, newPin) - - // Call PKCS11 function - CK_RV rv = pkcs11->functionList->C_SetPIN( - sessionHandle, - (CK_UTF8CHAR_PTR)oldPin, (CK_ULONG)oldPinLength, - (CK_UTF8CHAR_PTR)newPin, (CK_ULONG)newPinLength); - ASSERT_RV(rv); - - return nullptr; - } + static napi_value C_InitPIN(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); + GET_ARGS(2, arg) - static napi_value C_OpenSession(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); - GET_ARGS(2, arg) + // Read arguments + GET_ARGS_SESSION_HANDLE(0, sessionHandle) + GET_ARGS_STRING(1, pin) - // Read arguments - GET_ARGS_SLOT_ID(0, slotId) - GET_ARGS_ULONG(1, flags) - // GET_FUNCTION_FROM_ARG(2, callback) + // Call PKCS11 function + CK_RV rv = pkcs11->functionList->C_InitPIN(sessionHandle, (CK_UTF8CHAR_PTR)pin, (CK_ULONG)pinLength); + ASSERT_RV(rv); - // Call PKCS11 function - CK_SESSION_HANDLE sessionHandle; - CK_RV rv = pkcs11->functionList->C_OpenSession(slotId, (CK_FLAGS)flags, nullptr, nullptr, &sessionHandle); - ASSERT_RV(rv); + return nullptr; + } - // Create result - napi_value result; - napi_create_buffer_copy(env, sizeof(sessionHandle), &sessionHandle, nullptr, &result); + static napi_value C_SetPIN(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); + GET_ARGS(3, arg) + + // Read arguments + GET_ARGS_SESSION_HANDLE(0, sessionHandle) + GET_ARGS_STRING(1, oldPin) + GET_ARGS_STRING(2, newPin) + + // Call PKCS11 function + CK_RV rv = pkcs11->functionList->C_SetPIN( + sessionHandle, + (CK_UTF8CHAR_PTR)oldPin, (CK_ULONG)oldPinLength, + (CK_UTF8CHAR_PTR)newPin, (CK_ULONG)newPinLength); + ASSERT_RV(rv); - return result; - } + return nullptr; + } - static napi_value C_CloseSession(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); - GET_ARGS(1, arg) + static napi_value C_OpenSession(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); + GET_ARGS(2, arg) - // Read arguments - GET_ARGS_SESSION_HANDLE(0, sessionHandle) + // Read arguments + GET_ARGS_SLOT_ID(0, slotId) + GET_ARGS_ULONG(1, flags) + // GET_FUNCTION_FROM_ARG(2, callback) - // Call PKCS11 function - CK_RV rv = pkcs11->functionList->C_CloseSession(sessionHandle); - ASSERT_RV(rv); + // Call PKCS11 function + CK_SESSION_HANDLE sessionHandle; + CK_RV rv = pkcs11->functionList->C_OpenSession(slotId, (CK_FLAGS)flags, nullptr, nullptr, &sessionHandle); + ASSERT_RV(rv); - return nullptr; - } + // Create result + napi_value result; + napi_create_buffer_copy(env, sizeof(sessionHandle), &sessionHandle, nullptr, &result); - static napi_value C_CloseAllSessions(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); - GET_ARGS(1, arg) + return result; + } - // Read arguments - GET_ARGS_SLOT_ID(0, slotId) + static napi_value C_CloseSession(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); + GET_ARGS(1, arg) - // Call PKCS11 function - CK_RV rv = pkcs11->functionList->C_CloseAllSessions(slotId); - ASSERT_RV(rv); + // Read arguments + GET_ARGS_SESSION_HANDLE(0, sessionHandle) - return nullptr; - } + // Call PKCS11 function + CK_RV rv = pkcs11->functionList->C_CloseSession(sessionHandle); + ASSERT_RV(rv); - static napi_value C_GetSessionInfo(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); - GET_ARGS(1, arg) + return nullptr; + } - // Read arguments - GET_ARGS_SESSION_HANDLE(0, sessionHandle) + static napi_value C_CloseAllSessions(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); + GET_ARGS(1, arg) - // Call PKCS11 function - CK_SESSION_INFO ckSessionInfo; - CK_RV rv = pkcs11->functionList->C_GetSessionInfo(sessionHandle, &ckSessionInfo); - ASSERT_RV(rv); + // Read arguments + GET_ARGS_SLOT_ID(0, slotId) - // Create result - napi_value result; - napi_create_object(env, &result); + // Call PKCS11 function + CK_RV rv = pkcs11->functionList->C_CloseAllSessions(slotId); + ASSERT_RV(rv); - napi_value slotID; - napi_create_buffer_copy(env, sizeof(ckSessionInfo.slotID), &ckSessionInfo.slotID, nullptr, &slotID); - napi_set_named_property(env, result, "slotID", slotID); + return nullptr; + } - napi_value state; - napi_create_uint32(env, ckSessionInfo.state, &state); - napi_set_named_property(env, result, "state", state); + static napi_value C_GetSessionInfo(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); + GET_ARGS(1, arg) - napi_value flags; - napi_create_uint32(env, ckSessionInfo.flags, &flags); - napi_set_named_property(env, result, "flags", flags); + // Read arguments + GET_ARGS_SESSION_HANDLE(0, sessionHandle) - napi_value ulDeviceError; - napi_create_uint32(env, ckSessionInfo.ulDeviceError, &ulDeviceError); - napi_set_named_property(env, result, "deviceError", ulDeviceError); + // Call PKCS11 function + CK_SESSION_INFO ckSessionInfo; + CK_RV rv = pkcs11->functionList->C_GetSessionInfo(sessionHandle, &ckSessionInfo); + ASSERT_RV(rv); - return result; - } + // Create result + napi_value result; + napi_create_object(env, &result); - static napi_value C_GetOperationState(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); - GET_ARGS(1, arg) + napi_value slotID; + napi_create_buffer_copy(env, sizeof(ckSessionInfo.slotID), &ckSessionInfo.slotID, nullptr, &slotID); + napi_set_named_property(env, result, "slotID", slotID); - // Read arguments - GET_ARGS_SESSION_HANDLE(0, sessionHandle) + napi_value state; + napi_create_uint32(env, ckSessionInfo.state, &state); + napi_set_named_property(env, result, "state", state); - // Call PKCS11 function - CK_ULONG stateLength; - CK_RV rv = pkcs11->functionList->C_GetOperationState(sessionHandle, nullptr, &stateLength); // get state length - ASSERT_RV(rv); + napi_value flags; + napi_create_uint32(env, ckSessionInfo.flags, &flags); + napi_set_named_property(env, result, "flags", flags); - std::vector stateVector(stateLength); - CK_BYTE_PTR state = stateVector.data(); - rv = pkcs11->functionList->C_GetOperationState(sessionHandle, state, &stateLength); - ASSERT_RV(rv); + napi_value ulDeviceError; + napi_create_uint32(env, ckSessionInfo.ulDeviceError, &ulDeviceError); + napi_set_named_property(env, result, "deviceError", ulDeviceError); - // Create result - napi_value result; - napi_create_buffer_copy(env, stateLength, state, nullptr, &result); + return result; + } - return result; - } + static napi_value C_GetOperationState(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); + GET_ARGS(1, arg) - static napi_value C_SetOperationState(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); - GET_ARGS(4, arg) - - // Read arguments - GET_ARGS_SESSION_HANDLE(0, sessionHandle) - GET_ARGS_BUFFER(1, state) - GET_ARGS_HANDLE(2, encryptionKeyHandle) - GET_ARGS_HANDLE(3, authenticationKeyHandle) - - // Call PKCS11 function - CK_RV rv = pkcs11->functionList->C_SetOperationState( - sessionHandle, - (CK_BYTE_PTR)state, (CK_ULONG)stateLength, - (CK_OBJECT_HANDLE)encryptionKeyHandle, - (CK_OBJECT_HANDLE)authenticationKeyHandle); - ASSERT_RV(rv); - - return nullptr; - } + // Read arguments + GET_ARGS_SESSION_HANDLE(0, sessionHandle) - static napi_value C_Login(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); - GET_ARGS(3, arg) + // Call PKCS11 function + CK_ULONG stateLength; + CK_RV rv = pkcs11->functionList->C_GetOperationState(sessionHandle, nullptr, &stateLength); // get state length + ASSERT_RV(rv); - // Read arguments - GET_ARGS_SESSION_HANDLE(0, sessionHandle) - GET_ARGS_ULONG(1, userType) - GET_ARGS_STRING(2, pin) + std::vector stateVector(stateLength); + CK_BYTE_PTR state = stateVector.data(); + rv = pkcs11->functionList->C_GetOperationState(sessionHandle, state, &stateLength); + ASSERT_RV(rv); - // Call PKCS11 function - CK_RV rv = pkcs11->functionList->C_Login(sessionHandle, (CK_USER_TYPE)userType, (CK_UTF8CHAR_PTR)pin, (CK_ULONG)pinLength); - ASSERT_RV(rv); + // Create result + napi_value result; + napi_create_buffer_copy(env, stateLength, state, nullptr, &result); - return nullptr; - } + return result; + } - static napi_value C_Logout(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); - GET_ARGS(1, arg) + static napi_value C_SetOperationState(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); + GET_ARGS(4, arg) + + // Read arguments + GET_ARGS_SESSION_HANDLE(0, sessionHandle) + GET_ARGS_BUFFER(1, state) + GET_ARGS_HANDLE(2, encryptionKeyHandle) + GET_ARGS_HANDLE(3, authenticationKeyHandle) + + // Call PKCS11 function + CK_RV rv = pkcs11->functionList->C_SetOperationState( + sessionHandle, + (CK_BYTE_PTR)state, (CK_ULONG)stateLength, + (CK_OBJECT_HANDLE)encryptionKeyHandle, + (CK_OBJECT_HANDLE)authenticationKeyHandle); + ASSERT_RV(rv); - // Read arguments - GET_ARGS_SESSION_HANDLE(0, sessionHandle) + return nullptr; + } - // Call PKCS11 function - CK_RV rv = pkcs11->functionList->C_Logout(sessionHandle); - ASSERT_RV(rv); + static napi_value C_Login(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); + GET_ARGS(3, arg) - return nullptr; - } + // Read arguments + GET_ARGS_SESSION_HANDLE(0, sessionHandle) + GET_ARGS_ULONG(1, userType) + GET_ARGS_STRING(2, pin) - static napi_value C_SeedRandom(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); - GET_ARGS(2, arg) + // Call PKCS11 function + CK_RV rv = pkcs11->functionList->C_Login(sessionHandle, (CK_USER_TYPE)userType, (CK_UTF8CHAR_PTR)pin, (CK_ULONG)pinLength); + ASSERT_RV(rv); + + return nullptr; + } - // Read arguments - GET_ARGS_SESSION_HANDLE(0, sessionHandle) - GET_ARGS_BUFFER(1, seed) + static napi_value C_Logout(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); + GET_ARGS(1, arg) - // Call PKCS11 function - CK_RV rv = pkcs11->functionList->C_SeedRandom(sessionHandle, (CK_BYTE_PTR)seed, (CK_ULONG)seedLength); - ASSERT_RV(rv); + // Read arguments + GET_ARGS_SESSION_HANDLE(0, sessionHandle) - return nullptr; - } + // Call PKCS11 function + CK_RV rv = pkcs11->functionList->C_Logout(sessionHandle); + ASSERT_RV(rv); - static napi_value C_GenerateRandom(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); - GET_ARGS(2, arg) + return nullptr; + } - // Read arguments - GET_ARGS_SESSION_HANDLE(0, sessionHandle) - GET_ARGS_BUFFER(1, randomData) + static napi_value C_SeedRandom(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); + GET_ARGS(2, arg) - // Call PKCS11 function - CK_RV rv = pkcs11->functionList->C_GenerateRandom(sessionHandle, (CK_BYTE_PTR)randomData, (CK_ULONG)randomDataLength); - ASSERT_RV(rv); + // Read arguments + GET_ARGS_SESSION_HANDLE(0, sessionHandle) + GET_ARGS_BUFFER(1, seed) - return arg[1]; - } + // Call PKCS11 function + CK_RV rv = pkcs11->functionList->C_SeedRandom(sessionHandle, (CK_BYTE_PTR)seed, (CK_ULONG)seedLength); + ASSERT_RV(rv); - static napi_value C_CreateObject(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); - GET_ARGS(2, arg) + return nullptr; + } - // Read arguments - GET_ARGS_SESSION_HANDLE(0, sessionHandle) - GET_ARGS_ATTRIBUTES(1, attrs) + static napi_value C_GenerateRandom(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); + GET_ARGS(2, arg) - // Call PKCS11 function - CK_OBJECT_HANDLE objectHandle; - CK_RV rv = pkcs11->functionList->C_CreateObject(sessionHandle, attrs.attributes, attrsLength, &objectHandle); - ASSERT_RV(rv); + // Read arguments + GET_ARGS_SESSION_HANDLE(0, sessionHandle) + GET_ARGS_BUFFER(1, randomData) - // Create result - napi_value result; - napi_create_buffer_copy(env, sizeof(objectHandle), &objectHandle, nullptr, &result); + // Call PKCS11 function + CK_RV rv = pkcs11->functionList->C_GenerateRandom(sessionHandle, (CK_BYTE_PTR)randomData, (CK_ULONG)randomDataLength); + ASSERT_RV(rv); - return result; - } + return arg[1]; + } - static napi_value C_FindObjectsInit(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); - GET_ARGS(2, arg) + static napi_value C_CreateObject(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); + GET_ARGS(2, arg) - // Read arguments - GET_ARGS_SESSION_HANDLE(0, sessionHandle) - GET_ARGS_ATTRIBUTES(1, attrs) + // Read arguments + GET_ARGS_SESSION_HANDLE(0, sessionHandle) + GET_ARGS_ATTRIBUTES(1, attrs) - // Call PKCS11 function - CK_RV rv = pkcs11->functionList->C_FindObjectsInit(sessionHandle, attrs.attributes, attrsLength); - ASSERT_RV(rv); + // Call PKCS11 function + CK_OBJECT_HANDLE objectHandle; + CK_RV rv = pkcs11->functionList->C_CreateObject(sessionHandle, attrs.attributes, attrsLength, &objectHandle); + ASSERT_RV(rv); - return nullptr; - } + // Create result + napi_value result; + napi_create_buffer_copy(env, sizeof(objectHandle), &objectHandle, nullptr, &result); - static napi_value C_FindObjects(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); - GET_ARGS(2, arg) + return result; + } - // Read arguments - GET_ARGS_SESSION_HANDLE(0, sessionHandle) - GET_ARGS_ULONG(1, maxObjectCount) + static napi_value C_FindObjectsInit(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); + GET_ARGS(2, arg) + + // Read arguments + GET_ARGS_SESSION_HANDLE(0, sessionHandle) + GET_ARGS_ATTRIBUTES(1, attrs) - // Call PKCS11 function - CK_ULONG objectCount; - std::vector objectVector(maxObjectCount); - CK_OBJECT_HANDLE_PTR objectHandles = objectVector.data(); - CK_RV rv = pkcs11->functionList->C_FindObjects(sessionHandle, objectHandles, maxObjectCount, &objectCount); - ASSERT_RV(rv); + // Call PKCS11 function + CK_RV rv = pkcs11->functionList->C_FindObjectsInit(sessionHandle, attrs.attributes, attrsLength); + ASSERT_RV(rv); - // Create result array - napi_value result; - napi_create_array(env, &result); - for (int i = 0; i < int(objectCount); i++) + return nullptr; + } + + static napi_value C_FindObjects(napi_env env, napi_callback_info info) { - napi_value objectHandle; - napi_create_buffer_copy(env, sizeof(objectHandles[i]), &objectHandles[i], nullptr, &objectHandle); - napi_set_element(env, result, i, objectHandle); + UNWRAP_PKCS11(); + GET_ARGS(2, arg) + + // Read arguments + GET_ARGS_SESSION_HANDLE(0, sessionHandle) + GET_ARGS_ULONG(1, maxObjectCount) + + // Call PKCS11 function + CK_ULONG objectCount; + std::vector objectVector(maxObjectCount); + CK_OBJECT_HANDLE_PTR objectHandles = objectVector.data(); + CK_RV rv = pkcs11->functionList->C_FindObjects(sessionHandle, objectHandles, maxObjectCount, &objectCount); + ASSERT_RV(rv); + + // Create result array + napi_value result; + napi_create_array(env, &result); + for (int i = 0; i < int(objectCount); i++) + { + napi_value objectHandle; + napi_create_buffer_copy(env, sizeof(objectHandles[i]), &objectHandles[i], nullptr, &objectHandle); + napi_set_element(env, result, i, objectHandle); + } + + return result; } - return result; - } + static napi_value C_FindObjectsFinal(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); + GET_ARGS(1, arg) - static napi_value C_FindObjectsFinal(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); - GET_ARGS(1, arg) + // Read arguments + GET_ARGS_SESSION_HANDLE(0, sessionHandle) - // Read arguments - GET_ARGS_SESSION_HANDLE(0, sessionHandle) + // Call PKCS11 function + CK_RV rv = pkcs11->functionList->C_FindObjectsFinal(sessionHandle); + ASSERT_RV(rv); - // Call PKCS11 function - CK_RV rv = pkcs11->functionList->C_FindObjectsFinal(sessionHandle); - ASSERT_RV(rv); + return nullptr; + } - return nullptr; - } + static napi_value C_CopyObject(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); + GET_ARGS(3, arg) + + // Read arguments + GET_ARGS_SESSION_HANDLE(0, sessionHandle) + GET_ARGS_HANDLE(1, objectHandle) + GET_ARGS_ATTRIBUTES(2, attrs) + + // Call PKCS11 function + CK_OBJECT_HANDLE newObjectHandle; + CK_RV rv = pkcs11->functionList->C_CopyObject( + sessionHandle, + objectHandle, + attrs.attributes, attrsLength, + &newObjectHandle); + ASSERT_RV(rv); - static napi_value C_CopyObject(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); - GET_ARGS(3, arg) - - // Read arguments - GET_ARGS_SESSION_HANDLE(0, sessionHandle) - GET_ARGS_HANDLE(1, objectHandle) - GET_ARGS_ATTRIBUTES(2, attrs) - - // Call PKCS11 function - CK_OBJECT_HANDLE newObjectHandle; - CK_RV rv = pkcs11->functionList->C_CopyObject( - sessionHandle, - objectHandle, - attrs.attributes, attrsLength, - &newObjectHandle); - ASSERT_RV(rv); - - // Create result - napi_value result; - napi_create_buffer_copy(env, sizeof(newObjectHandle), &newObjectHandle, nullptr, &result); - - return result; - } + // Create result + napi_value result; + napi_create_buffer_copy(env, sizeof(newObjectHandle), &newObjectHandle, nullptr, &result); - static napi_value C_DestroyObject(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); - GET_ARGS(2, arg) + return result; + } - // Read arguments - GET_ARGS_SESSION_HANDLE(0, sessionHandle) - GET_ARGS_HANDLE(1, objectHandle) + static napi_value C_DestroyObject(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); + GET_ARGS(2, arg) - // Call PKCS11 function - CK_RV rv = pkcs11->functionList->C_DestroyObject(sessionHandle, objectHandle); - ASSERT_RV(rv); + // Read arguments + GET_ARGS_SESSION_HANDLE(0, sessionHandle) + GET_ARGS_HANDLE(1, objectHandle) - return nullptr; - } + // Call PKCS11 function + CK_RV rv = pkcs11->functionList->C_DestroyObject(sessionHandle, objectHandle); + ASSERT_RV(rv); - static napi_value C_GetAttributeValue(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); - GET_ARGS(3, arg) + return nullptr; + } - // Read arguments - GET_ARGS_SESSION_HANDLE(0, sessionHandle) - GET_ARGS_HANDLE(1, objectHandle) - GET_ARGS_ATTRIBUTES(2, attrs) + static napi_value C_GetAttributeValue(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); + GET_ARGS(3, arg) - // Call PKCS11 function - CK_RV rv = pkcs11->functionList->C_GetAttributeValue(sessionHandle, objectHandle, attrs.attributes, attrsLength); - ASSERT_RV(rv); + // Read arguments + GET_ARGS_SESSION_HANDLE(0, sessionHandle) + GET_ARGS_HANDLE(1, objectHandle) + GET_ARGS_ATTRIBUTES(2, attrs) - attrs.allocAllValues(); + // Call PKCS11 function + CK_RV rv = pkcs11->functionList->C_GetAttributeValue(sessionHandle, objectHandle, attrs.attributes, attrsLength); + ASSERT_RV(rv); - rv = pkcs11->functionList->C_GetAttributeValue(sessionHandle, objectHandle, attrs.attributes, attrsLength); - ASSERT_RV(rv); + attrs.allocAllValues(); - // Create result array - napi_value result = arg[2]; - for (int i = 0; i < int(attrsLength); i++) - { - // Get element - napi_value element; - napi_get_element(env, result, i, &element); + rv = pkcs11->functionList->C_GetAttributeValue(sessionHandle, objectHandle, attrs.attributes, attrsLength); + ASSERT_RV(rv); - // create Buffer for value - napi_value value; - CK_ATTRIBUTE_PTR attr = &attrs.attributes[i]; - napi_create_buffer_copy(env, attr->ulValueLen, attr->pValue, nullptr, &value); + // Create result array + napi_value result = arg[2]; + for (int i = 0; i < int(attrsLength); i++) + { + // Get element + napi_value element; + napi_get_element(env, result, i, &element); - // set value property on element - napi_set_named_property(env, element, "value", value); - } + // create Buffer for value + napi_value value; + CK_ATTRIBUTE_PTR attr = &attrs.attributes[i]; + napi_create_buffer_copy(env, attr->ulValueLen, attr->pValue, nullptr, &value); - return result; - } + // set value property on element + napi_set_named_property(env, element, "value", value); + } - static napi_value C_SetAttributeValue(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); - GET_ARGS(3, arg) + return result; + } - // Read arguments - GET_ARGS_SESSION_HANDLE(0, sessionHandle) - GET_ARGS_HANDLE(1, objectHandle) - GET_ARGS_ATTRIBUTES(2, attrs) + static napi_value C_SetAttributeValue(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); + GET_ARGS(3, arg) - // Call PKCS11 function - CK_RV rv = pkcs11->functionList->C_SetAttributeValue(sessionHandle, objectHandle, attrs.attributes, attrsLength); - ASSERT_RV(rv); + // Read arguments + GET_ARGS_SESSION_HANDLE(0, sessionHandle) + GET_ARGS_HANDLE(1, objectHandle) + GET_ARGS_ATTRIBUTES(2, attrs) - return nullptr; - } + // Call PKCS11 function + CK_RV rv = pkcs11->functionList->C_SetAttributeValue(sessionHandle, objectHandle, attrs.attributes, attrsLength); + ASSERT_RV(rv); - static napi_value C_GetObjectSize(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); - GET_ARGS(2, arg) + return nullptr; + } - // Read arguments - GET_ARGS_SESSION_HANDLE(0, sessionHandle) - GET_ARGS_HANDLE(1, objectHandle) + static napi_value C_GetObjectSize(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); + GET_ARGS(2, arg) - // Call PKCS11 function - CK_ULONG objectSize; - CK_RV rv = pkcs11->functionList->C_GetObjectSize(sessionHandle, objectHandle, &objectSize); - ASSERT_RV(rv); + // Read arguments + GET_ARGS_SESSION_HANDLE(0, sessionHandle) + GET_ARGS_HANDLE(1, objectHandle) - // Create result - napi_value result; - napi_create_uint32(env, objectSize, &result); + // Call PKCS11 function + CK_ULONG objectSize; + CK_RV rv = pkcs11->functionList->C_GetObjectSize(sessionHandle, objectHandle, &objectSize); + ASSERT_RV(rv); - return result; - } + // Create result + napi_value result; + napi_create_uint32(env, objectSize, &result); - static napi_value C_DigestInit(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); - GET_ARGS(2, arg) + return result; + } - // Read arguments - GET_ARGS_SESSION_HANDLE(0, sessionHandle) - GET_ARGS_MECHANISM(1, mechanism) + static napi_value C_DigestInit(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); + GET_ARGS(2, arg) - // Call PKCS11 function - CK_RV rv = pkcs11->functionList->C_DigestInit(sessionHandle, mechanism.value); - ASSERT_RV(rv); + // Read arguments + GET_ARGS_SESSION_HANDLE(0, sessionHandle) + GET_ARGS_MECHANISM(1, mechanism) - return nullptr; - } + // Call PKCS11 function + CK_RV rv = pkcs11->functionList->C_DigestInit(sessionHandle, mechanism.value); + ASSERT_RV(rv); - static napi_value C_Digest(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); - GET_ARGS(3, arg) + return nullptr; + } - // Read arguments - GET_ARGS_SESSION_HANDLE(0, sessionHandle) - GET_ARGS_BUFFER(1, data) - GET_ARGS_BUFFER(2, digest) + static napi_value C_Digest(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); + GET_ARGS(3, arg) - // Call PKCS11 function - CK_RV rv = pkcs11->functionList->C_Digest(sessionHandle, (CK_BYTE_PTR)data, (CK_ULONG)dataLength, (CK_BYTE_PTR)digest, (CK_ULONG_PTR)&digestLength); - ASSERT_RV(rv); + // Read arguments + GET_ARGS_SESSION_HANDLE(0, sessionHandle) + GET_ARGS_BUFFER(1, data) + GET_ARGS_BUFFER(2, digest) - // Create result - napi_value result; - napi_create_uint32(env, digestLength, &result); + // Call PKCS11 function + CK_RV rv = pkcs11->functionList->C_Digest(sessionHandle, (CK_BYTE_PTR)data, (CK_ULONG)dataLength, (CK_BYTE_PTR)digest, (CK_ULONG_PTR)&digestLength); + ASSERT_RV(rv); - return result; - } + // Create result + napi_value result; + napi_create_uint32(env, digestLength, &result); - static napi_value C_DigestCallback(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); - GET_ARGS(4, arg) - - // Read arguments - GET_ARGS_SESSION_HANDLE(0, sessionHandle) - GET_ARGS_BUFFER(1, data) - GET_ARGS_BUFFER(2, digest) - GET_ARGS_CALLBACK(3, callback) - - // Create worker - new Worker(env, callback, pkcs11->functionList->C_Digest, sessionHandle, (CK_BYTE_PTR)data, (CK_ULONG)dataLength, (CK_BYTE_PTR)digest, (CK_ULONG)digestLength); - return nullptr; - } + return result; + } - static napi_value C_DigestUpdate(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); - GET_ARGS(2, arg) + static napi_value C_DigestCallback(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); + GET_ARGS(4, arg) - // Read arguments - GET_ARGS_SESSION_HANDLE(0, sessionHandle) - GET_ARGS_BUFFER(1, data) + // Read arguments + GET_ARGS_SESSION_HANDLE(0, sessionHandle) + GET_ARGS_BUFFER(1, data) + GET_ARGS_BUFFER(2, digest) + GET_ARGS_CALLBACK(3, callback) - // Call PKCS11 function - CK_RV rv = pkcs11->functionList->C_DigestUpdate(sessionHandle, (CK_BYTE_PTR)data, (CK_ULONG)dataLength); - ASSERT_RV(rv); + // Create worker + new Worker(env, callback, pkcs11->functionList->C_Digest, sessionHandle, (CK_BYTE_PTR)data, (CK_ULONG)dataLength, (CK_BYTE_PTR)digest, (CK_ULONG)digestLength); + return nullptr; + } - return nullptr; - } + static napi_value C_DigestUpdate(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); + GET_ARGS(2, arg) - static napi_value C_DigestKey(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); - GET_ARGS(2, arg) + // Read arguments + GET_ARGS_SESSION_HANDLE(0, sessionHandle) + GET_ARGS_BUFFER(1, data) - // Read arguments - GET_ARGS_SESSION_HANDLE(0, sessionHandle) - GET_ARGS_HANDLE(1, keyHandle) + // Call PKCS11 function + CK_RV rv = pkcs11->functionList->C_DigestUpdate(sessionHandle, (CK_BYTE_PTR)data, (CK_ULONG)dataLength); + ASSERT_RV(rv); - // Call PKCS11 function - CK_RV rv = pkcs11->functionList->C_DigestKey(sessionHandle, keyHandle); - ASSERT_RV(rv); + return nullptr; + } - return nullptr; - } + static napi_value C_DigestKey(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); + GET_ARGS(2, arg) - static napi_value C_DigestFinal(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); - GET_ARGS(2, arg) + // Read arguments + GET_ARGS_SESSION_HANDLE(0, sessionHandle) + GET_ARGS_HANDLE(1, keyHandle) - // Read arguments - GET_ARGS_SESSION_HANDLE(0, sessionHandle) - GET_ARGS_BUFFER(1, digest) + // Call PKCS11 function + CK_RV rv = pkcs11->functionList->C_DigestKey(sessionHandle, keyHandle); + ASSERT_RV(rv); - // Call PKCS11 function - CK_RV rv = pkcs11->functionList->C_DigestFinal(sessionHandle, (CK_BYTE_PTR)digest, (CK_ULONG_PTR)&digestLength); - ASSERT_RV(rv); + return nullptr; + } - // Create result - napi_value result; - napi_create_uint32(env, digestLength, &result); + static napi_value C_DigestFinal(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); + GET_ARGS(2, arg) - return result; - } + // Read arguments + GET_ARGS_SESSION_HANDLE(0, sessionHandle) + GET_ARGS_BUFFER(1, digest) - static napi_value C_DigestFinalCallback(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); - GET_ARGS(3, arg) + // Call PKCS11 function + CK_RV rv = pkcs11->functionList->C_DigestFinal(sessionHandle, (CK_BYTE_PTR)digest, (CK_ULONG_PTR)&digestLength); + ASSERT_RV(rv); - // Read arguments - GET_ARGS_SESSION_HANDLE(0, sessionHandle) - GET_ARGS_BUFFER(1, digest) - GET_ARGS_CALLBACK(2, callback) + // Create result + napi_value result; + napi_create_uint32(env, digestLength, &result); - // Create worker - new Worker2(env, callback, pkcs11->functionList->C_DigestFinal, sessionHandle, (CK_BYTE_PTR)digest, (CK_ULONG)digestLength); - return nullptr; - } + return result; + } - static napi_value C_GenerateKey(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); - GET_ARGS(3, arg) + static napi_value C_DigestFinalCallback(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); + GET_ARGS(3, arg) - // Read arguments - GET_ARGS_SESSION_HANDLE(0, sessionHandle) - GET_ARGS_MECHANISM(1, mechanism) - GET_ARGS_ATTRIBUTES(2, attrs) + // Read arguments + GET_ARGS_SESSION_HANDLE(0, sessionHandle) + GET_ARGS_BUFFER(1, digest) + GET_ARGS_CALLBACK(2, callback) - // Call PKCS11 function - CK_OBJECT_HANDLE keyHandle; - CK_RV rv = pkcs11->functionList->C_GenerateKey(sessionHandle, mechanism.value, attrs.attributes, attrsLength, &keyHandle); - ASSERT_RV(rv); + // Create worker + new Worker2(env, callback, pkcs11->functionList->C_DigestFinal, sessionHandle, (CK_BYTE_PTR)digest, (CK_ULONG)digestLength); + return nullptr; + } - // Create result - napi_value result; - napi_create_buffer_copy(env, sizeof(keyHandle), &keyHandle, nullptr, &result); + static napi_value C_GenerateKey(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); + GET_ARGS(3, arg) - return result; - } + // Read arguments + GET_ARGS_SESSION_HANDLE(0, sessionHandle) + GET_ARGS_MECHANISM(1, mechanism) + GET_ARGS_ATTRIBUTES(2, attrs) - static napi_value C_GenerateKeyCallback(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); - GET_ARGS(4, arg) - - // Read arguments - GET_ARGS_SESSION_HANDLE(0, sessionHandle) - GET_ARGS_MECHANISM(1, mechanism) - mechanism.dispose = false; - GET_ARGS_ATTRIBUTES(2, attrs) - attrs.dispose = false; - GET_ARGS_CALLBACK(3, callback) - - // Create worker - new WorkerGenerateKey(env, callback, pkcs11->functionList->C_GenerateKey, sessionHandle, mechanism.value, attrs.attributes, attrsLength); - return nullptr; - } + // Call PKCS11 function + CK_OBJECT_HANDLE keyHandle; + CK_RV rv = pkcs11->functionList->C_GenerateKey(sessionHandle, mechanism.value, attrs.attributes, attrsLength, &keyHandle); + ASSERT_RV(rv); - static napi_value C_GenerateKeyPair(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); - GET_ARGS(4, arg) - - // Read arguments - GET_ARGS_SESSION_HANDLE(0, sessionHandle) - GET_ARGS_MECHANISM(1, mechanism) - GET_ARGS_ATTRIBUTES(2, publicKeyAttrs) - GET_ARGS_ATTRIBUTES(3, privateKeyAttrs) - - // Call PKCS11 function - CK_OBJECT_HANDLE publicKeyHandle; - CK_OBJECT_HANDLE privateKeyHandle; - CK_RV rv = pkcs11->functionList->C_GenerateKeyPair( - sessionHandle, - mechanism.value, - publicKeyAttrs.attributes, publicKeyAttrsLength, - privateKeyAttrs.attributes, privateKeyAttrsLength, - &publicKeyHandle, &privateKeyHandle); - ASSERT_RV(rv); - - // Create result - napi_value result; - napi_create_object(env, &result); - - napi_value privateKey; - napi_create_buffer_copy(env, sizeof(privateKeyHandle), &privateKeyHandle, nullptr, &privateKey); - napi_set_named_property(env, result, "privateKey", privateKey); - - napi_value publicKey; - napi_create_buffer_copy(env, sizeof(publicKeyHandle), &publicKeyHandle, nullptr, &publicKey); - napi_set_named_property(env, result, "publicKey", publicKey); - - return result; - } + // Create result + napi_value result; + napi_create_buffer_copy(env, sizeof(keyHandle), &keyHandle, nullptr, &result); - static napi_value C_GenerateKeyPairCallback(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); - GET_ARGS(5, arg) - - // Read arguments - GET_ARGS_SESSION_HANDLE(0, sessionHandle) - GET_ARGS_MECHANISM(1, mechanism) - mechanism.dispose = false; - GET_ARGS_ATTRIBUTES(2, publicKeyAttrs) - publicKeyAttrs.dispose = false; - GET_ARGS_ATTRIBUTES(3, privateKeyAttrs) - privateKeyAttrs.dispose = false; - GET_ARGS_CALLBACK(4, callback) - - new WorkerGenerateKeyPair(env, callback, pkcs11->functionList->C_GenerateKeyPair, sessionHandle, mechanism.value, publicKeyAttrs.attributes, publicKeyAttrsLength, privateKeyAttrs.attributes, privateKeyAttrsLength); - return nullptr; - } + return result; + } - static napi_value C_SignInit(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); - GET_ARGS(3, arg) + static napi_value C_GenerateKeyCallback(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); + GET_ARGS(4, arg) + + // Read arguments + GET_ARGS_SESSION_HANDLE(0, sessionHandle) + GET_ARGS_MECHANISM(1, mechanism) + mechanism.dispose = false; + GET_ARGS_ATTRIBUTES(2, attrs) + attrs.dispose = false; + GET_ARGS_CALLBACK(3, callback) + + // Create worker + new WorkerGenerateKey(env, callback, pkcs11->functionList->C_GenerateKey, sessionHandle, mechanism.value, attrs.attributes, attrsLength); + return nullptr; + } - // Read arguments - GET_ARGS_SESSION_HANDLE(0, sessionHandle) - GET_ARGS_MECHANISM(1, mechanism) - GET_ARGS_HANDLE(2, keyHandle) + static napi_value C_GenerateKeyPair(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); + GET_ARGS(4, arg) + + // Read arguments + GET_ARGS_SESSION_HANDLE(0, sessionHandle) + GET_ARGS_MECHANISM(1, mechanism) + GET_ARGS_ATTRIBUTES(2, publicKeyAttrs) + GET_ARGS_ATTRIBUTES(3, privateKeyAttrs) + + // Call PKCS11 function + CK_OBJECT_HANDLE publicKeyHandle; + CK_OBJECT_HANDLE privateKeyHandle; + CK_RV rv = pkcs11->functionList->C_GenerateKeyPair( + sessionHandle, + mechanism.value, + publicKeyAttrs.attributes, publicKeyAttrsLength, + privateKeyAttrs.attributes, privateKeyAttrsLength, + &publicKeyHandle, &privateKeyHandle); + ASSERT_RV(rv); - // Call PKCS11 function - CK_RV rv = pkcs11->functionList->C_SignInit(sessionHandle, mechanism.value, keyHandle); - ASSERT_RV(rv); + // Create result + napi_value result; + napi_create_object(env, &result); - return nullptr; - } + napi_value privateKey; + napi_create_buffer_copy(env, sizeof(privateKeyHandle), &privateKeyHandle, nullptr, &privateKey); + napi_set_named_property(env, result, "privateKey", privateKey); - static napi_value C_Sign(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); - GET_ARGS(3, arg) + napi_value publicKey; + napi_create_buffer_copy(env, sizeof(publicKeyHandle), &publicKeyHandle, nullptr, &publicKey); + napi_set_named_property(env, result, "publicKey", publicKey); - // Read arguments - GET_ARGS_SESSION_HANDLE(0, sessionHandle) - GET_ARGS_BUFFER(1, data) - GET_ARGS_BUFFER(2, signature) + return result; + } - // Call PKCS11 function - CK_RV rv = pkcs11->functionList->C_Sign(sessionHandle, (CK_BYTE_PTR)data, (CK_ULONG)dataLength, (CK_BYTE_PTR)signature, (CK_ULONG_PTR)&signatureLength); - ASSERT_RV(rv); + static napi_value C_GenerateKeyPairCallback(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); + GET_ARGS(5, arg) + + // Read arguments + GET_ARGS_SESSION_HANDLE(0, sessionHandle) + GET_ARGS_MECHANISM(1, mechanism) + mechanism.dispose = false; + GET_ARGS_ATTRIBUTES(2, publicKeyAttrs) + publicKeyAttrs.dispose = false; + GET_ARGS_ATTRIBUTES(3, privateKeyAttrs) + privateKeyAttrs.dispose = false; + GET_ARGS_CALLBACK(4, callback) + + new WorkerGenerateKeyPair(env, callback, pkcs11->functionList->C_GenerateKeyPair, sessionHandle, mechanism.value, publicKeyAttrs.attributes, publicKeyAttrsLength, privateKeyAttrs.attributes, privateKeyAttrsLength); + return nullptr; + } - // Create result - napi_value result; - napi_create_uint32(env, signatureLength, &result); + static napi_value C_SignInit(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); + GET_ARGS(3, arg) - return result; - } + // Read arguments + GET_ARGS_SESSION_HANDLE(0, sessionHandle) + GET_ARGS_MECHANISM(1, mechanism) + GET_ARGS_HANDLE(2, keyHandle) - static napi_value C_SignCallback(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); - GET_ARGS(4, arg) - - // Read arguments - GET_ARGS_SESSION_HANDLE(0, sessionHandle) - GET_ARGS_BUFFER(1, data) - GET_ARGS_BUFFER(2, signature) - GET_ARGS_CALLBACK(3, callback) - - // Create worker - new Worker(env, callback, pkcs11->functionList->C_Sign, sessionHandle, (CK_BYTE_PTR)data, (CK_ULONG)dataLength, (CK_BYTE_PTR)signature, (CK_ULONG)signatureLength); - return nullptr; - } + // Call PKCS11 function + CK_RV rv = pkcs11->functionList->C_SignInit(sessionHandle, mechanism.value, keyHandle); + ASSERT_RV(rv); - static napi_value C_SignUpdate(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); - GET_ARGS(2, arg) + return nullptr; + } - // Read arguments - GET_ARGS_SESSION_HANDLE(0, sessionHandle) - GET_ARGS_BUFFER(1, data) + static napi_value C_Sign(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); + GET_ARGS(3, arg) - // Call PKCS11 function - CK_RV rv = pkcs11->functionList->C_SignUpdate(sessionHandle, (CK_BYTE_PTR)data, (CK_ULONG)dataLength); - ASSERT_RV(rv); + // Read arguments + GET_ARGS_SESSION_HANDLE(0, sessionHandle) + GET_ARGS_BUFFER(1, data) + GET_ARGS_BUFFER(2, signature) - return nullptr; - } + // Call PKCS11 function + CK_RV rv = pkcs11->functionList->C_Sign(sessionHandle, (CK_BYTE_PTR)data, (CK_ULONG)dataLength, (CK_BYTE_PTR)signature, (CK_ULONG_PTR)&signatureLength); + ASSERT_RV(rv); - static napi_value C_SignFinal(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); - GET_ARGS(2, arg) + // Create result + napi_value result; + napi_create_uint32(env, signatureLength, &result); - // Read arguments - GET_ARGS_SESSION_HANDLE(0, sessionHandle) - GET_ARGS_BUFFER(1, signature) + return result; + } - // Call PKCS11 function - CK_RV rv = pkcs11->functionList->C_SignFinal(sessionHandle, (CK_BYTE_PTR)signature, (CK_ULONG_PTR)&signatureLength); - ASSERT_RV(rv); + static napi_value C_SignCallback(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); + GET_ARGS(4, arg) - // Create result - napi_value result; - napi_create_uint32(env, signatureLength, &result); + // Read arguments + GET_ARGS_SESSION_HANDLE(0, sessionHandle) + GET_ARGS_BUFFER(1, data) + GET_ARGS_BUFFER(2, signature) + GET_ARGS_CALLBACK(3, callback) - return result; - } + // Create worker + new Worker(env, callback, pkcs11->functionList->C_Sign, sessionHandle, (CK_BYTE_PTR)data, (CK_ULONG)dataLength, (CK_BYTE_PTR)signature, (CK_ULONG)signatureLength); + return nullptr; + } - static napi_value C_SignFinalCallback(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); - GET_ARGS(3, arg) + static napi_value C_SignUpdate(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); + GET_ARGS(2, arg) - // Read arguments - GET_ARGS_SESSION_HANDLE(0, sessionHandle) - GET_ARGS_BUFFER(1, signature) - GET_ARGS_CALLBACK(2, callback) + // Read arguments + GET_ARGS_SESSION_HANDLE(0, sessionHandle) + GET_ARGS_BUFFER(1, data) - // Create worker - new Worker2(env, callback, pkcs11->functionList->C_SignFinal, sessionHandle, (CK_BYTE_PTR)signature, (CK_ULONG)signatureLength); - return nullptr; - } + // Call PKCS11 function + CK_RV rv = pkcs11->functionList->C_SignUpdate(sessionHandle, (CK_BYTE_PTR)data, (CK_ULONG)dataLength); + ASSERT_RV(rv); - static napi_value C_VerifyInit(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); - GET_ARGS(3, arg) + return nullptr; + } - // Read arguments - GET_ARGS_SESSION_HANDLE(0, sessionHandle) - GET_ARGS_MECHANISM(1, mechanism) - GET_ARGS_HANDLE(2, keyHandle) + static napi_value C_SignFinal(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); + GET_ARGS(2, arg) - // Call PKCS11 function - CK_RV rv = pkcs11->functionList->C_VerifyInit(sessionHandle, mechanism.value, keyHandle); - ASSERT_RV(rv); + // Read arguments + GET_ARGS_SESSION_HANDLE(0, sessionHandle) + GET_ARGS_BUFFER(1, signature) - return nullptr; - } + // Call PKCS11 function + CK_RV rv = pkcs11->functionList->C_SignFinal(sessionHandle, (CK_BYTE_PTR)signature, (CK_ULONG_PTR)&signatureLength); + ASSERT_RV(rv); - static napi_value C_Verify(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); - GET_ARGS(3, arg) + // Create result + napi_value result; + napi_create_uint32(env, signatureLength, &result); - // Read arguments - GET_ARGS_SESSION_HANDLE(0, sessionHandle) - GET_ARGS_BUFFER(1, data) - GET_ARGS_BUFFER(2, signature) + return result; + } - // Call PKCS11 function - CK_RV rv = pkcs11->functionList->C_Verify(sessionHandle, (CK_BYTE_PTR)data, (CK_ULONG)dataLength, (CK_BYTE_PTR)signature, (CK_ULONG)signatureLength); - ASSERT_RV(rv); + static napi_value C_SignFinalCallback(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); + GET_ARGS(3, arg) - // Create result - napi_value result; - napi_get_boolean(env, true, &result); + // Read arguments + GET_ARGS_SESSION_HANDLE(0, sessionHandle) + GET_ARGS_BUFFER(1, signature) + GET_ARGS_CALLBACK(2, callback) - return result; - } + // Create worker + new Worker2(env, callback, pkcs11->functionList->C_SignFinal, sessionHandle, (CK_BYTE_PTR)signature, (CK_ULONG)signatureLength); + return nullptr; + } - static napi_value C_VerifyCallback(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); - GET_ARGS(4, arg) - - // Read arguments - GET_ARGS_SESSION_HANDLE(0, sessionHandle) - GET_ARGS_BUFFER(1, data) - GET_ARGS_BUFFER(2, signature) - GET_ARGS_CALLBACK(3, callback) - - // Create worker - new WorkerVerify(env, callback, pkcs11->functionList->C_Verify, sessionHandle, (CK_BYTE_PTR)data, (CK_ULONG)dataLength, (CK_BYTE_PTR)signature, (CK_ULONG)signatureLength); - return nullptr; - } + static napi_value C_VerifyInit(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); + GET_ARGS(3, arg) - static napi_value C_VerifyUpdate(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); - GET_ARGS(2, arg) + // Read arguments + GET_ARGS_SESSION_HANDLE(0, sessionHandle) + GET_ARGS_MECHANISM(1, mechanism) + GET_ARGS_HANDLE(2, keyHandle) - // Read arguments - GET_ARGS_SESSION_HANDLE(0, sessionHandle) - GET_ARGS_BUFFER(1, data) + // Call PKCS11 function + CK_RV rv = pkcs11->functionList->C_VerifyInit(sessionHandle, mechanism.value, keyHandle); + ASSERT_RV(rv); - // Call PKCS11 function - CK_RV rv = pkcs11->functionList->C_VerifyUpdate(sessionHandle, (CK_BYTE_PTR)data, (CK_ULONG)dataLength); - ASSERT_RV(rv); + return nullptr; + } - return nullptr; - } + static napi_value C_Verify(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); + GET_ARGS(3, arg) - static napi_value C_VerifyFinal(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); - GET_ARGS(2, arg) + // Read arguments + GET_ARGS_SESSION_HANDLE(0, sessionHandle) + GET_ARGS_BUFFER(1, data) + GET_ARGS_BUFFER(2, signature) - // Read arguments - GET_ARGS_SESSION_HANDLE(0, sessionHandle) - GET_ARGS_BUFFER(1, signature) + // Call PKCS11 function + CK_RV rv = pkcs11->functionList->C_Verify(sessionHandle, (CK_BYTE_PTR)data, (CK_ULONG)dataLength, (CK_BYTE_PTR)signature, (CK_ULONG)signatureLength); + ASSERT_RV(rv); - // Call PKCS11 function - CK_RV rv = pkcs11->functionList->C_VerifyFinal(sessionHandle, (CK_BYTE_PTR)signature, (CK_ULONG)signatureLength); - ASSERT_RV(rv); + // Create result + napi_value result; + napi_get_boolean(env, true, &result); - // Create result - napi_value result; - napi_get_boolean(env, true, &result); + return result; + } - return result; - } + static napi_value C_VerifyCallback(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); + GET_ARGS(4, arg) - static napi_value C_VerifyFinalCallback(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); - GET_ARGS(3, arg) + // Read arguments + GET_ARGS_SESSION_HANDLE(0, sessionHandle) + GET_ARGS_BUFFER(1, data) + GET_ARGS_BUFFER(2, signature) + GET_ARGS_CALLBACK(3, callback) - // Read arguments - GET_ARGS_SESSION_HANDLE(0, sessionHandle) - GET_ARGS_BUFFER(1, signature) - GET_ARGS_CALLBACK(2, callback) + // Create worker + new WorkerVerify(env, callback, pkcs11->functionList->C_Verify, sessionHandle, (CK_BYTE_PTR)data, (CK_ULONG)dataLength, (CK_BYTE_PTR)signature, (CK_ULONG)signatureLength); + return nullptr; + } - // Create worker - new WorkerVerifyFinal(env, callback, pkcs11->functionList->C_VerifyFinal, sessionHandle, (CK_BYTE_PTR)signature, (CK_ULONG)signatureLength); - return nullptr; - } + static napi_value C_VerifyUpdate(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); + GET_ARGS(2, arg) - static napi_value C_EncryptInit(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); - GET_ARGS(3, arg) + // Read arguments + GET_ARGS_SESSION_HANDLE(0, sessionHandle) + GET_ARGS_BUFFER(1, data) - // Read arguments - GET_ARGS_SESSION_HANDLE(0, sessionHandle) - GET_ARGS_MECHANISM(1, mechanism) - GET_ARGS_HANDLE(2, keyHandle) + // Call PKCS11 function + CK_RV rv = pkcs11->functionList->C_VerifyUpdate(sessionHandle, (CK_BYTE_PTR)data, (CK_ULONG)dataLength); + ASSERT_RV(rv); - // Call PKCS11 function - CK_RV rv = pkcs11->functionList->C_EncryptInit(sessionHandle, mechanism.value, keyHandle); - ASSERT_RV(rv); + return nullptr; + } - return nullptr; - } + static napi_value C_VerifyFinal(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); + GET_ARGS(2, arg) - static napi_value C_Encrypt(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); - GET_ARGS(3, arg) + // Read arguments + GET_ARGS_SESSION_HANDLE(0, sessionHandle) + GET_ARGS_BUFFER(1, signature) - // Read arguments - GET_ARGS_SESSION_HANDLE(0, sessionHandle) - GET_ARGS_BUFFER(1, data) - GET_ARGS_BUFFER(2, encryptedData) + // Call PKCS11 function + CK_RV rv = pkcs11->functionList->C_VerifyFinal(sessionHandle, (CK_BYTE_PTR)signature, (CK_ULONG)signatureLength); + ASSERT_RV(rv); - // Call PKCS11 function - CK_RV rv = pkcs11->functionList->C_Encrypt(sessionHandle, (CK_BYTE_PTR)data, (CK_ULONG)dataLength, (CK_BYTE_PTR)encryptedData, (CK_ULONG_PTR)&encryptedDataLength); - ASSERT_RV(rv); + // Create result + napi_value result; + napi_get_boolean(env, true, &result); - // Create result (size of encryptedData) - napi_value result; - napi_create_uint32(env, encryptedDataLength, &result); + return result; + } - return result; - } + static napi_value C_VerifyFinalCallback(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); + GET_ARGS(3, arg) - static napi_value C_EncryptCallback(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); - GET_ARGS(4, arg) + // Read arguments + GET_ARGS_SESSION_HANDLE(0, sessionHandle) + GET_ARGS_BUFFER(1, signature) + GET_ARGS_CALLBACK(2, callback) - // Read arguments - GET_ARGS_SESSION_HANDLE(0, sessionHandle) - GET_ARGS_BUFFER(1, data) - GET_ARGS_BUFFER(2, encryptedData) - GET_ARGS_CALLBACK(3, callback) + // Create worker + new WorkerVerifyFinal(env, callback, pkcs11->functionList->C_VerifyFinal, sessionHandle, (CK_BYTE_PTR)signature, (CK_ULONG)signatureLength); + return nullptr; + } - new Worker(env, callback, pkcs11->functionList->C_Encrypt, sessionHandle, (CK_BYTE_PTR)data, (CK_ULONG)dataLength, (CK_BYTE_PTR)encryptedData, (CK_ULONG)encryptedDataLength); - return nullptr; - } + static napi_value C_EncryptInit(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); + GET_ARGS(3, arg) - static napi_value C_EncryptUpdate(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); - GET_ARGS(3, arg) + // Read arguments + GET_ARGS_SESSION_HANDLE(0, sessionHandle) + GET_ARGS_MECHANISM(1, mechanism) + GET_ARGS_HANDLE(2, keyHandle) - // Read arguments - GET_ARGS_SESSION_HANDLE(0, sessionHandle) - GET_ARGS_BUFFER(1, data) - GET_ARGS_BUFFER(2, encryptedData) + // Call PKCS11 function + CK_RV rv = pkcs11->functionList->C_EncryptInit(sessionHandle, mechanism.value, keyHandle); + ASSERT_RV(rv); - // Call PKCS11 function - CK_RV rv = pkcs11->functionList->C_EncryptUpdate(sessionHandle, (CK_BYTE_PTR)data, (CK_ULONG)dataLength, (CK_BYTE_PTR)encryptedData, (CK_ULONG_PTR)&encryptedDataLength); - ASSERT_RV(rv); + return nullptr; + } - // Create result (size of encryptedData) - napi_value result; - napi_create_uint32(env, encryptedDataLength, &result); + static napi_value C_Encrypt(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); + GET_ARGS(3, arg) - return result; - } + // Read arguments + GET_ARGS_SESSION_HANDLE(0, sessionHandle) + GET_ARGS_BUFFER(1, data) + GET_ARGS_BUFFER(2, encryptedData) - static napi_value C_EncryptFinal(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); - GET_ARGS(2, arg) + // Call PKCS11 function + CK_RV rv = pkcs11->functionList->C_Encrypt(sessionHandle, (CK_BYTE_PTR)data, (CK_ULONG)dataLength, (CK_BYTE_PTR)encryptedData, (CK_ULONG_PTR)&encryptedDataLength); + ASSERT_RV(rv); - // Read arguments - GET_ARGS_SESSION_HANDLE(0, sessionHandle) - GET_ARGS_BUFFER(1, encryptedData) + // Create result (size of encryptedData) + napi_value result; + napi_create_uint32(env, encryptedDataLength, &result); - // Call PKCS11 function - CK_RV rv = pkcs11->functionList->C_EncryptFinal(sessionHandle, (CK_BYTE_PTR)encryptedData, (CK_ULONG_PTR)&encryptedDataLength); - ASSERT_RV(rv); + return result; + } - // Create result (size of encryptedData) - napi_value result; - napi_create_uint32(env, encryptedDataLength, &result); + static napi_value C_EncryptCallback(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); + GET_ARGS(4, arg) - return result; - } + // Read arguments + GET_ARGS_SESSION_HANDLE(0, sessionHandle) + GET_ARGS_BUFFER(1, data) + GET_ARGS_BUFFER(2, encryptedData) + GET_ARGS_CALLBACK(3, callback) - static napi_value C_EncryptFinalCallback(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); - GET_ARGS(3, arg) + new Worker(env, callback, pkcs11->functionList->C_Encrypt, sessionHandle, (CK_BYTE_PTR)data, (CK_ULONG)dataLength, (CK_BYTE_PTR)encryptedData, (CK_ULONG)encryptedDataLength); + return nullptr; + } - // Read arguments - GET_ARGS_SESSION_HANDLE(0, sessionHandle) - GET_ARGS_BUFFER(1, encryptedData) - GET_ARGS_CALLBACK(2, callback) + static napi_value C_EncryptUpdate(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); + GET_ARGS(3, arg) - // Create worker - new Worker2(env, callback, pkcs11->functionList->C_EncryptFinal, sessionHandle, (CK_BYTE_PTR)encryptedData, (CK_ULONG)encryptedDataLength); - return nullptr; - } + // Read arguments + GET_ARGS_SESSION_HANDLE(0, sessionHandle) + GET_ARGS_BUFFER(1, data) + GET_ARGS_BUFFER(2, encryptedData) - static napi_value C_DecryptInit(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); - GET_ARGS(3, arg) + // Call PKCS11 function + CK_RV rv = pkcs11->functionList->C_EncryptUpdate(sessionHandle, (CK_BYTE_PTR)data, (CK_ULONG)dataLength, (CK_BYTE_PTR)encryptedData, (CK_ULONG_PTR)&encryptedDataLength); + ASSERT_RV(rv); - // Read arguments - GET_ARGS_SESSION_HANDLE(0, sessionHandle) - GET_ARGS_MECHANISM(1, mechanism) - GET_ARGS_HANDLE(2, keyHandle) + // Create result (size of encryptedData) + napi_value result; + napi_create_uint32(env, encryptedDataLength, &result); - // Call PKCS11 function - CK_RV rv = pkcs11->functionList->C_DecryptInit(sessionHandle, mechanism.value, keyHandle); - ASSERT_RV(rv); + return result; + } - return nullptr; - } + static napi_value C_EncryptFinal(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); + GET_ARGS(2, arg) - static napi_value C_Decrypt(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); - GET_ARGS(3, arg) + // Read arguments + GET_ARGS_SESSION_HANDLE(0, sessionHandle) + GET_ARGS_BUFFER(1, encryptedData) - // Read arguments - GET_ARGS_SESSION_HANDLE(0, sessionHandle) - GET_ARGS_BUFFER(1, encryptedData) - GET_ARGS_BUFFER(2, data) + // Call PKCS11 function + CK_RV rv = pkcs11->functionList->C_EncryptFinal(sessionHandle, (CK_BYTE_PTR)encryptedData, (CK_ULONG_PTR)&encryptedDataLength); + ASSERT_RV(rv); - // Call PKCS11 function - CK_RV rv = pkcs11->functionList->C_Decrypt(sessionHandle, (CK_BYTE_PTR)encryptedData, (CK_ULONG)encryptedDataLength, (CK_BYTE_PTR)data, (CK_ULONG_PTR)&dataLength); - ASSERT_RV(rv); + // Create result (size of encryptedData) + napi_value result; + napi_create_uint32(env, encryptedDataLength, &result); - // Create result (size of data) - napi_value result; - napi_create_uint32(env, dataLength, &result); + return result; + } - return result; - } + static napi_value C_EncryptFinalCallback(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); + GET_ARGS(3, arg) - static napi_value C_DecryptCallback(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); - GET_ARGS(4, arg) - - // Read arguments - GET_ARGS_SESSION_HANDLE(0, sessionHandle) - GET_ARGS_BUFFER(1, encryptedData) - GET_ARGS_BUFFER(2, data) - GET_ARGS_CALLBACK(3, callback) - - // Create worker - new Worker(env, callback, pkcs11->functionList->C_Decrypt, sessionHandle, (CK_BYTE_PTR)encryptedData, (CK_ULONG)encryptedDataLength, (CK_BYTE_PTR)data, (CK_ULONG)dataLength); - return nullptr; - } + // Read arguments + GET_ARGS_SESSION_HANDLE(0, sessionHandle) + GET_ARGS_BUFFER(1, encryptedData) + GET_ARGS_CALLBACK(2, callback) - static napi_value C_DecryptUpdate(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); - GET_ARGS(3, arg) + // Create worker + new Worker2(env, callback, pkcs11->functionList->C_EncryptFinal, sessionHandle, (CK_BYTE_PTR)encryptedData, (CK_ULONG)encryptedDataLength); + return nullptr; + } - // Read arguments - GET_ARGS_SESSION_HANDLE(0, sessionHandle) - GET_ARGS_BUFFER(1, encryptedData) - GET_ARGS_BUFFER(2, data) + static napi_value C_DecryptInit(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); + GET_ARGS(3, arg) - // Call PKCS11 function - CK_RV rv = pkcs11->functionList->C_DecryptUpdate(sessionHandle, (CK_BYTE_PTR)encryptedData, (CK_ULONG)encryptedDataLength, (CK_BYTE_PTR)data, (CK_ULONG_PTR)&dataLength); - ASSERT_RV(rv); + // Read arguments + GET_ARGS_SESSION_HANDLE(0, sessionHandle) + GET_ARGS_MECHANISM(1, mechanism) + GET_ARGS_HANDLE(2, keyHandle) - // Create result (size of data) - napi_value result; - napi_create_uint32(env, dataLength, &result); + // Call PKCS11 function + CK_RV rv = pkcs11->functionList->C_DecryptInit(sessionHandle, mechanism.value, keyHandle); + ASSERT_RV(rv); - return result; - } + return nullptr; + } - static napi_value C_DecryptFinal(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); - GET_ARGS(2, arg) + static napi_value C_Decrypt(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); + GET_ARGS(3, arg) - // Read arguments - GET_ARGS_SESSION_HANDLE(0, sessionHandle) - GET_ARGS_BUFFER(1, data) + // Read arguments + GET_ARGS_SESSION_HANDLE(0, sessionHandle) + GET_ARGS_BUFFER(1, encryptedData) + GET_ARGS_BUFFER(2, data) - // Call PKCS11 function - CK_RV rv = pkcs11->functionList->C_DecryptFinal(sessionHandle, (CK_BYTE_PTR)data, (CK_ULONG_PTR)&dataLength); - ASSERT_RV(rv); + // Call PKCS11 function + CK_RV rv = pkcs11->functionList->C_Decrypt(sessionHandle, (CK_BYTE_PTR)encryptedData, (CK_ULONG)encryptedDataLength, (CK_BYTE_PTR)data, (CK_ULONG_PTR)&dataLength); + ASSERT_RV(rv); - // Create result (size of data) - napi_value result; - napi_create_uint32(env, dataLength, &result); + // Create result (size of data) + napi_value result; + napi_create_uint32(env, dataLength, &result); - return result; - } + return result; + } - static napi_value C_DecryptFinalCallback(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); - GET_ARGS(3, arg) + static napi_value C_DecryptCallback(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); + GET_ARGS(4, arg) - // Read arguments - GET_ARGS_SESSION_HANDLE(0, sessionHandle) - GET_ARGS_BUFFER(1, data) - GET_ARGS_CALLBACK(2, callback) + // Read arguments + GET_ARGS_SESSION_HANDLE(0, sessionHandle) + GET_ARGS_BUFFER(1, encryptedData) + GET_ARGS_BUFFER(2, data) + GET_ARGS_CALLBACK(3, callback) - // Create worker - new Worker2(env, arg[2], pkcs11->functionList->C_DecryptFinal, sessionHandle, (CK_BYTE_PTR)data, (CK_ULONG)dataLength); - return nullptr; - } + // Create worker + new Worker(env, callback, pkcs11->functionList->C_Decrypt, sessionHandle, (CK_BYTE_PTR)encryptedData, (CK_ULONG)encryptedDataLength, (CK_BYTE_PTR)data, (CK_ULONG)dataLength); + return nullptr; + } - static napi_value C_DeriveKey(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); - GET_ARGS(4, arg) + static napi_value C_DecryptUpdate(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); + GET_ARGS(3, arg) - // Read arguments - GET_ARGS_SESSION_HANDLE(0, sessionHandle) - GET_ARGS_MECHANISM(1, mechanism) - GET_ARGS_HANDLE(2, baseKeyHandle) - GET_ARGS_ATTRIBUTES(3, attrs) + // Read arguments + GET_ARGS_SESSION_HANDLE(0, sessionHandle) + GET_ARGS_BUFFER(1, encryptedData) + GET_ARGS_BUFFER(2, data) - // Call PKCS11 function - CK_OBJECT_HANDLE keyHandle; - CK_RV rv = pkcs11->functionList->C_DeriveKey(sessionHandle, mechanism.value, baseKeyHandle, attrs.attributes, attrsLength, &keyHandle); - ASSERT_RV(rv); + // Call PKCS11 function + CK_RV rv = pkcs11->functionList->C_DecryptUpdate(sessionHandle, (CK_BYTE_PTR)encryptedData, (CK_ULONG)encryptedDataLength, (CK_BYTE_PTR)data, (CK_ULONG_PTR)&dataLength); + ASSERT_RV(rv); - // Create result - napi_value result; - napi_create_buffer_copy(env, sizeof(keyHandle), &keyHandle, nullptr, &result); + // Create result (size of data) + napi_value result; + napi_create_uint32(env, dataLength, &result); - return result; - } + return result; + } - static napi_value C_DeriveKeyCallback(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); - GET_ARGS(5, arg) - - // Read arguments - GET_ARGS_SESSION_HANDLE(0, sessionHandle) - GET_ARGS_MECHANISM(1, mechanism) - mechanism.dispose = false; - GET_ARGS_HANDLE(2, baseKeyHandle) - GET_ARGS_ATTRIBUTES(3, attrs) - attrs.dispose = false; - GET_ARGS_CALLBACK(4, callback) - - // Create worker - new WorkerDeriveKey(env, callback, pkcs11->functionList->C_DeriveKey, sessionHandle, mechanism.value, baseKeyHandle, attrs.attributes, attrsLength); - return nullptr; - } + static napi_value C_DecryptFinal(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); + GET_ARGS(2, arg) - static napi_value C_WrapKey(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); - GET_ARGS(5, arg) + // Read arguments + GET_ARGS_SESSION_HANDLE(0, sessionHandle) + GET_ARGS_BUFFER(1, data) - // Read arguments - GET_ARGS_SESSION_HANDLE(0, sessionHandle) - GET_ARGS_MECHANISM(1, mechanism) - GET_ARGS_HANDLE(2, wrappingKeyHandle) - GET_ARGS_HANDLE(3, keyHandle) - GET_ARGS_BUFFER(4, wrappedKey) + // Call PKCS11 function + CK_RV rv = pkcs11->functionList->C_DecryptFinal(sessionHandle, (CK_BYTE_PTR)data, (CK_ULONG_PTR)&dataLength); + ASSERT_RV(rv); - // Call PKCS11 function - CK_RV rv = pkcs11->functionList->C_WrapKey(sessionHandle, mechanism.value, wrappingKeyHandle, keyHandle, (CK_BYTE_PTR)wrappedKey, (CK_ULONG_PTR)&wrappedKeyLength); - ASSERT_RV(rv); + // Create result (size of data) + napi_value result; + napi_create_uint32(env, dataLength, &result); - // Create result (size of wrappedKey) - napi_value result; - napi_create_uint32(env, wrappedKeyLength, &result); + return result; + } - return result; - } + static napi_value C_DecryptFinalCallback(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); + GET_ARGS(3, arg) - static napi_value C_WrapKeyCallback(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); - GET_ARGS(6, arg) - - // Read arguments - GET_ARGS_SESSION_HANDLE(0, sessionHandle) - GET_ARGS_MECHANISM(1, mechanism) - mechanism.dispose = false; - GET_ARGS_HANDLE(2, wrappingKeyHandle) - GET_ARGS_HANDLE(3, keyHandle) - GET_ARGS_BUFFER(4, wrappedKey) - GET_ARGS_CALLBACK(5, callback) - - // Create worker - new WorkerWrapKey(env, callback, pkcs11->functionList->C_WrapKey, sessionHandle, mechanism.value, wrappingKeyHandle, keyHandle, (CK_BYTE_PTR)wrappedKey, (CK_ULONG)wrappedKeyLength); - return nullptr; - } + // Read arguments + GET_ARGS_SESSION_HANDLE(0, sessionHandle) + GET_ARGS_BUFFER(1, data) + GET_ARGS_CALLBACK(2, callback) - static napi_value C_UnwrapKey(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); - GET_ARGS(5, arg) - - // Read arguments - GET_ARGS_SESSION_HANDLE(0, sessionHandle) - GET_ARGS_MECHANISM(1, mechanism) - GET_ARGS_HANDLE(2, unwrappingKeyHandle) - GET_ARGS_BUFFER(3, wrappedKey) - GET_ARGS_ATTRIBUTES(4, attrs) - - // Call PKCS11 function - CK_OBJECT_HANDLE keyHandle; - CK_RV rv = pkcs11->functionList->C_UnwrapKey(sessionHandle, mechanism.value, unwrappingKeyHandle, (CK_BYTE_PTR)wrappedKey, (CK_ULONG)wrappedKeyLength, attrs.attributes, attrsLength, &keyHandle); - ASSERT_RV(rv); - - // Create result - napi_value result; - napi_create_buffer_copy(env, sizeof(keyHandle), &keyHandle, nullptr, &result); - - return result; - } + // Create worker + new Worker2(env, arg[2], pkcs11->functionList->C_DecryptFinal, sessionHandle, (CK_BYTE_PTR)data, (CK_ULONG)dataLength); + return nullptr; + } - static napi_value C_UnwrapKeyCallback(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); - GET_ARGS(6, arg) - - // Read arguments - GET_ARGS_SESSION_HANDLE(0, sessionHandle) - GET_ARGS_MECHANISM(1, mechanism) - mechanism.dispose = false; - GET_ARGS_HANDLE(2, unwrappingKeyHandle) - GET_ARGS_BUFFER(3, wrappedKey) - GET_ARGS_ATTRIBUTES(4, attrs) - attrs.dispose = false; - GET_ARGS_CALLBACK(5, callback) - - // Create worker - new WorkerUnwrapKey(env, callback, pkcs11->functionList->C_UnwrapKey, sessionHandle, mechanism.value, unwrappingKeyHandle, (CK_BYTE_PTR)wrappedKey, (CK_ULONG)wrappedKeyLength, attrs.attributes, attrsLength); - return nullptr; - } + static napi_value C_DeriveKey(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); + GET_ARGS(4, arg) + + // Read arguments + GET_ARGS_SESSION_HANDLE(0, sessionHandle) + GET_ARGS_MECHANISM(1, mechanism) + GET_ARGS_HANDLE(2, baseKeyHandle) + GET_ARGS_ATTRIBUTES(3, attrs) + + // Call PKCS11 function + CK_OBJECT_HANDLE keyHandle; + CK_RV rv = pkcs11->functionList->C_DeriveKey(sessionHandle, mechanism.value, baseKeyHandle, attrs.attributes, attrsLength, &keyHandle); + ASSERT_RV(rv); - static napi_value C_SignRecoverInit(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); - GET_ARGS(3, arg) + // Create result + napi_value result; + napi_create_buffer_copy(env, sizeof(keyHandle), &keyHandle, nullptr, &result); - // Read arguments - GET_ARGS_SESSION_HANDLE(0, sessionHandle) - GET_ARGS_MECHANISM(1, mechanism) - GET_ARGS_HANDLE(2, keyHandle) + return result; + } - // Call PKCS11 function - CK_RV rv = pkcs11->functionList->C_SignRecoverInit(sessionHandle, mechanism.value, keyHandle); - ASSERT_RV(rv); + static napi_value C_DeriveKeyCallback(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); + GET_ARGS(5, arg) + + // Read arguments + GET_ARGS_SESSION_HANDLE(0, sessionHandle) + GET_ARGS_MECHANISM(1, mechanism) + mechanism.dispose = false; + GET_ARGS_HANDLE(2, baseKeyHandle) + GET_ARGS_ATTRIBUTES(3, attrs) + attrs.dispose = false; + GET_ARGS_CALLBACK(4, callback) + + // Create worker + new WorkerDeriveKey(env, callback, pkcs11->functionList->C_DeriveKey, sessionHandle, mechanism.value, baseKeyHandle, attrs.attributes, attrsLength); + return nullptr; + } - return nullptr; - } + static napi_value C_WrapKey(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); + GET_ARGS(5, arg) + + // Read arguments + GET_ARGS_SESSION_HANDLE(0, sessionHandle) + GET_ARGS_MECHANISM(1, mechanism) + GET_ARGS_HANDLE(2, wrappingKeyHandle) + GET_ARGS_HANDLE(3, keyHandle) + GET_ARGS_BUFFER(4, wrappedKey) + + // Call PKCS11 function + CK_RV rv = pkcs11->functionList->C_WrapKey(sessionHandle, mechanism.value, wrappingKeyHandle, keyHandle, (CK_BYTE_PTR)wrappedKey, (CK_ULONG_PTR)&wrappedKeyLength); + ASSERT_RV(rv); - static napi_value C_SignRecover(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); - GET_ARGS(3, arg) + // Create result (size of wrappedKey) + napi_value result; + napi_create_uint32(env, wrappedKeyLength, &result); - // Read arguments - GET_ARGS_SESSION_HANDLE(0, sessionHandle) - GET_ARGS_BUFFER(1, data) - GET_ARGS_BUFFER(2, signature) + return result; + } - // Call PKCS11 function - CK_RV rv = pkcs11->functionList->C_SignRecover(sessionHandle, (CK_BYTE_PTR)data, (CK_ULONG)dataLength, (CK_BYTE_PTR)signature, (CK_ULONG_PTR)&signatureLength); - ASSERT_RV(rv); + static napi_value C_WrapKeyCallback(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); + GET_ARGS(6, arg) + + // Read arguments + GET_ARGS_SESSION_HANDLE(0, sessionHandle) + GET_ARGS_MECHANISM(1, mechanism) + mechanism.dispose = false; + GET_ARGS_HANDLE(2, wrappingKeyHandle) + GET_ARGS_HANDLE(3, keyHandle) + GET_ARGS_BUFFER(4, wrappedKey) + GET_ARGS_CALLBACK(5, callback) + + // Create worker + new WorkerWrapKey(env, callback, pkcs11->functionList->C_WrapKey, sessionHandle, mechanism.value, wrappingKeyHandle, keyHandle, (CK_BYTE_PTR)wrappedKey, (CK_ULONG)wrappedKeyLength); + return nullptr; + } - // Create result - napi_value result; - napi_create_uint32(env, signatureLength, &result); + static napi_value C_UnwrapKey(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); + GET_ARGS(5, arg) + + // Read arguments + GET_ARGS_SESSION_HANDLE(0, sessionHandle) + GET_ARGS_MECHANISM(1, mechanism) + GET_ARGS_HANDLE(2, unwrappingKeyHandle) + GET_ARGS_BUFFER(3, wrappedKey) + GET_ARGS_ATTRIBUTES(4, attrs) + + // Call PKCS11 function + CK_OBJECT_HANDLE keyHandle; + CK_RV rv = pkcs11->functionList->C_UnwrapKey(sessionHandle, mechanism.value, unwrappingKeyHandle, (CK_BYTE_PTR)wrappedKey, (CK_ULONG)wrappedKeyLength, attrs.attributes, attrsLength, &keyHandle); + ASSERT_RV(rv); - return result; - } + // Create result + napi_value result; + napi_create_buffer_copy(env, sizeof(keyHandle), &keyHandle, nullptr, &result); - static napi_value C_VerifyRecoverInit(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); - GET_ARGS(3, arg) + return result; + } - GET_ARGS_SESSION_HANDLE(0, sessionHandle) - GET_ARGS_MECHANISM(1, mechanism) - GET_ARGS_HANDLE(2, keyHandle) + static napi_value C_UnwrapKeyCallback(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); + GET_ARGS(6, arg) + + // Read arguments + GET_ARGS_SESSION_HANDLE(0, sessionHandle) + GET_ARGS_MECHANISM(1, mechanism) + mechanism.dispose = false; + GET_ARGS_HANDLE(2, unwrappingKeyHandle) + GET_ARGS_BUFFER(3, wrappedKey) + GET_ARGS_ATTRIBUTES(4, attrs) + attrs.dispose = false; + GET_ARGS_CALLBACK(5, callback) + + // Create worker + new WorkerUnwrapKey(env, callback, pkcs11->functionList->C_UnwrapKey, sessionHandle, mechanism.value, unwrappingKeyHandle, (CK_BYTE_PTR)wrappedKey, (CK_ULONG)wrappedKeyLength, attrs.attributes, attrsLength); + return nullptr; + } - CK_RV rv = pkcs11->functionList->C_VerifyRecoverInit(sessionHandle, mechanism.value, keyHandle); - ASSERT_RV(rv); + static napi_value C_SignRecoverInit(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); + GET_ARGS(3, arg) - return nullptr; - } + // Read arguments + GET_ARGS_SESSION_HANDLE(0, sessionHandle) + GET_ARGS_MECHANISM(1, mechanism) + GET_ARGS_HANDLE(2, keyHandle) - static napi_value C_VerifyRecover(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); - GET_ARGS(3, arg) + // Call PKCS11 function + CK_RV rv = pkcs11->functionList->C_SignRecoverInit(sessionHandle, mechanism.value, keyHandle); + ASSERT_RV(rv); - // Read arguments - GET_ARGS_SESSION_HANDLE(0, sessionHandle) - GET_ARGS_BUFFER(1, signature) - GET_ARGS_BUFFER(2, data) + return nullptr; + } - // Call PKCS11 function - CK_RV rv = pkcs11->functionList->C_VerifyRecover(sessionHandle, (CK_BYTE_PTR)signature, (CK_ULONG)signatureLength, (CK_BYTE_PTR)data, (CK_ULONG_PTR)&dataLength); - ASSERT_RV(rv); + static napi_value C_SignRecover(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); + GET_ARGS(3, arg) - // Create result - napi_value result; - napi_create_uint32(env, dataLength, &result); + // Read arguments + GET_ARGS_SESSION_HANDLE(0, sessionHandle) + GET_ARGS_BUFFER(1, data) + GET_ARGS_BUFFER(2, signature) - return result; - } + // Call PKCS11 function + CK_RV rv = pkcs11->functionList->C_SignRecover(sessionHandle, (CK_BYTE_PTR)data, (CK_ULONG)dataLength, (CK_BYTE_PTR)signature, (CK_ULONG_PTR)&signatureLength); + ASSERT_RV(rv); - static napi_value C_WaitForSlotEvent(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); - GET_ARGS(1, arg) + // Create result + napi_value result; + napi_create_uint32(env, signatureLength, &result); - // Read arguments - GET_ARGS_ULONG(0, flags) + return result; + } - // Call PKCS11 function - CK_SLOT_ID slotId; - CK_RV rv = pkcs11->functionList->C_WaitForSlotEvent(flags, &slotId, nullptr); - if (rv != CKR_NO_EVENT) + static napi_value C_VerifyRecoverInit(napi_env env, napi_callback_info info) { + UNWRAP_PKCS11(); + GET_ARGS(3, arg) + + GET_ARGS_SESSION_HANDLE(0, sessionHandle) + GET_ARGS_MECHANISM(1, mechanism) + GET_ARGS_HANDLE(2, keyHandle) + + CK_RV rv = pkcs11->functionList->C_VerifyRecoverInit(sessionHandle, mechanism.value, keyHandle); ASSERT_RV(rv); + + return nullptr; } - // Create result - napi_value result; - if (rv == CKR_NO_EVENT) + static napi_value C_VerifyRecover(napi_env env, napi_callback_info info) { - napi_get_null(env, &result); + UNWRAP_PKCS11(); + GET_ARGS(3, arg) + + // Read arguments + GET_ARGS_SESSION_HANDLE(0, sessionHandle) + GET_ARGS_BUFFER(1, signature) + GET_ARGS_BUFFER(2, data) + + // Call PKCS11 function + CK_RV rv = pkcs11->functionList->C_VerifyRecover(sessionHandle, (CK_BYTE_PTR)signature, (CK_ULONG)signatureLength, (CK_BYTE_PTR)data, (CK_ULONG_PTR)&dataLength); + ASSERT_RV(rv); + + // Create result + napi_value result; + napi_create_uint32(env, dataLength, &result); + + return result; } - else + + static napi_value C_WaitForSlotEvent(napi_env env, napi_callback_info info) { - napi_create_buffer_copy(env, sizeof(CK_SLOT_ID), &slotId, nullptr, &result); + UNWRAP_PKCS11(); + GET_ARGS(1, arg) + + // Read arguments + GET_ARGS_ULONG(0, flags) + + // Call PKCS11 function + CK_SLOT_ID slotId; + CK_RV rv = pkcs11->functionList->C_WaitForSlotEvent(flags, &slotId, nullptr); + if (rv != CKR_NO_EVENT) + { + ASSERT_RV(rv); + } + + // Create result + napi_value result; + if (rv == CKR_NO_EVENT) + { + napi_get_null(env, &result); + } + else + { + napi_create_buffer_copy(env, sizeof(CK_SLOT_ID), &slotId, nullptr, &result); + } + return result; } - return result; - } - static napi_value dualOperation(napi_env env, napi_callback_info info, CK_C_DigestEncryptUpdate operation) - { - UNWRAP_PKCS11(); - GET_ARGS(3, arg) + static napi_value dualOperation(napi_env env, napi_callback_info info, CK_C_DigestEncryptUpdate operation) + { + UNWRAP_PKCS11(); + GET_ARGS(3, arg) - // Read arguments - GET_ARGS_SESSION_HANDLE(0, sessionHandle) - GET_ARGS_BUFFER(1, inData) - GET_ARGS_BUFFER(2, outData) + // Read arguments + GET_ARGS_SESSION_HANDLE(0, sessionHandle) + GET_ARGS_BUFFER(1, inData) + GET_ARGS_BUFFER(2, outData) - // Call PKCS11 function - CK_RV rv = operation(sessionHandle, (CK_BYTE_PTR)inData, (CK_ULONG)inDataLength, (CK_BYTE_PTR)outData, (CK_ULONG_PTR)&outDataLength); - ASSERT_RV(rv); + // Call PKCS11 function + CK_RV rv = operation(sessionHandle, (CK_BYTE_PTR)inData, (CK_ULONG)inDataLength, (CK_BYTE_PTR)outData, (CK_ULONG_PTR)&outDataLength); + ASSERT_RV(rv); - // Create result - napi_value result; - napi_create_uint32(env, outDataLength, &result); + // Create result + napi_value result; + napi_create_uint32(env, outDataLength, &result); - return result; - } + return result; + } - static napi_value dualOperationCallback(napi_env env, napi_callback_info info, CK_C_DigestEncryptUpdate operation) - { - UNWRAP_PKCS11(); - GET_ARGS(4, arg) - - // Read arguments - GET_ARGS_SESSION_HANDLE(0, sessionHandle) - GET_ARGS_BUFFER(1, inData) - GET_ARGS_BUFFER(2, outData) - GET_ARGS_CALLBACK(3, callback) - - // Create worker - new WorkerDualOperation(env, callback, operation, sessionHandle, (CK_BYTE_PTR)inData, (CK_ULONG)inDataLength, (CK_BYTE_PTR)outData, (CK_ULONG)outDataLength); - return nullptr; - } + static napi_value dualOperationCallback(napi_env env, napi_callback_info info, CK_C_DigestEncryptUpdate operation) + { + UNWRAP_PKCS11(); + GET_ARGS(4, arg) - static napi_value C_DigestEncryptUpdate(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); + // Read arguments + GET_ARGS_SESSION_HANDLE(0, sessionHandle) + GET_ARGS_BUFFER(1, inData) + GET_ARGS_BUFFER(2, outData) + GET_ARGS_CALLBACK(3, callback) - return dualOperation(env, info, pkcs11->functionList->C_DigestEncryptUpdate); - } + // Create worker + new WorkerDualOperation(env, callback, operation, sessionHandle, (CK_BYTE_PTR)inData, (CK_ULONG)inDataLength, (CK_BYTE_PTR)outData, (CK_ULONG)outDataLength); + return nullptr; + } - static napi_value C_DigestEncryptUpdateCallback(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); + static napi_value C_DigestEncryptUpdate(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); - return dualOperationCallback(env, info, pkcs11->functionList->C_DigestEncryptUpdate); - } + return dualOperation(env, info, pkcs11->functionList->C_DigestEncryptUpdate); + } - static napi_value C_DecryptDigestUpdate(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); + static napi_value C_DigestEncryptUpdateCallback(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); - return dualOperation(env, info, pkcs11->functionList->C_DecryptDigestUpdate); - } + return dualOperationCallback(env, info, pkcs11->functionList->C_DigestEncryptUpdate); + } - static napi_value C_DecryptDigestUpdateCallback(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); + static napi_value C_DecryptDigestUpdate(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); - return dualOperationCallback(env, info, pkcs11->functionList->C_DecryptDigestUpdate); - } + return dualOperation(env, info, pkcs11->functionList->C_DecryptDigestUpdate); + } - static napi_value C_SignEncryptUpdate(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); + static napi_value C_DecryptDigestUpdateCallback(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); - return dualOperation(env, info, pkcs11->functionList->C_SignEncryptUpdate); - } + return dualOperationCallback(env, info, pkcs11->functionList->C_DecryptDigestUpdate); + } - static napi_value C_SignEncryptUpdateCallback(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); + static napi_value C_SignEncryptUpdate(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); - return dualOperationCallback(env, info, pkcs11->functionList->C_SignEncryptUpdate); - } + return dualOperation(env, info, pkcs11->functionList->C_SignEncryptUpdate); + } - static napi_value C_DecryptVerifyUpdate(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); + static napi_value C_SignEncryptUpdateCallback(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); - return dualOperation(env, info, pkcs11->functionList->C_DecryptVerifyUpdate); - } + return dualOperationCallback(env, info, pkcs11->functionList->C_SignEncryptUpdate); + } - static napi_value C_DecryptVerifyUpdateCallback(napi_env env, napi_callback_info info) - { - UNWRAP_PKCS11(); + static napi_value C_DecryptVerifyUpdate(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); - return dualOperationCallback(env, info, pkcs11->functionList->C_DecryptVerifyUpdate); - } -}; + return dualOperation(env, info, pkcs11->functionList->C_DecryptVerifyUpdate); + } + + static napi_value C_DecryptVerifyUpdateCallback(napi_env env, napi_callback_info info) + { + UNWRAP_PKCS11(); + + return dualOperationCallback(env, info, pkcs11->functionList->C_DecryptVerifyUpdate); + } + };