39#include <libxml/tree.h>
40#include <libxml/parser.h>
41#include <libxml/xpath.h>
42#include <libxml/xpathInternals.h>
43#include <libxml/relaxng.h>
56#define HSM_TOKEN_LABEL_LENGTH 32
64ldns_pkcs11_rv_str(CK_RV rv)
73 return "CKR_HOST_MEMORY";
75 return "CKR_GENERAL_ERROR";
77 return "CKR_FUNCTION_FAILED";
79 return "CKR_SLOT_ID_INVALID";
81 return "CKR_ATTRIBUTE_READ_ONLY";
83 return "CKR_ATTRIBUTE_SENSITIVE";
85 return "CKR_ATTRIBUTE_TYPE_INVALID";
87 return "CKR_ATTRIBUTE_VALUE_INVALID";
89 return "CKR_DATA_INVALID";
91 return "CKR_DATA_LEN_RANGE";
93 return "CKR_DEVICE_ERROR";
95 return "CKR_DEVICE_MEMORY";
97 return "CKR_DEVICE_REMOVED";
99 return "CKR_ENCRYPTED_DATA_INVALID";
101 return "CKR_ENCRYPTED_DATA_LEN_RANGE";
103 return "CKR_FUNCTION_CANCELED";
105 return "CKR_FUNCTION_NOT_PARALLEL";
107 return "CKR_FUNCTION_NOT_SUPPORTED";
109 return "CKR_KEY_HANDLE_INVALID";
111 return "CKR_KEY_SIZE_RANGE";
113 return "CKR_KEY_TYPE_INCONSISTENT";
115 return "CKR_MECHANISM_INVALID";
117 return "CKR_MECHANISM_PARAM_INVALID";
119 return "CKR_OBJECT_HANDLE_INVALID";
121 return "CKR_OPERATION_ACTIVE";
123 return "CKR_OPERATION_NOT_INITIALIZED";
125 return "CKR_PIN_INCORRECT";
127 return "CKR_PIN_INVALID";
129 return "CKR_PIN_LEN_RANGE";
131 return "CKR_SESSION_CLOSED";
133 return "CKR_SESSION_COUNT";
135 return "CKR_SESSION_HANDLE_INVALID";
137 return "CKR_SESSION_PARALLEL_NOT_SUPPORTED";
139 return "CKR_SESSION_READ_ONLY";
141 return "CKR_SESSION_EXISTS";
143 return "CKR_SIGNATURE_INVALID";
145 return "CKR_SIGNATURE_LEN_RANGE";
147 return "CKR_TEMPLATE_INCOMPLETE";
149 return "CKR_TEMPLATE_INCONSISTENT";
151 return "CKR_TOKEN_NOT_PRESENT";
153 return "CKR_TOKEN_NOT_RECOGNIZED";
155 return "CKR_TOKEN_WRITE_PROTECTED";
157 return "CKR_UNWRAPPING_KEY_HANDLE_INVALID";
159 return "CKR_UNWRAPPING_KEY_SIZE_RANGE";
161 return "CKR_UNWRAPPING_KEY_TYPE_INCONSISTENT";
163 return "CKR_USER_ALREADY_LOGGED_IN";
165 return "CKR_USER_NOT_LOGGED_IN";
167 return "CKR_USER_PIN_NOT_INITIALIZED";
169 return "CKR_USER_TYPE_INVALID";
171 return "CKR_WRAPPED_KEY_INVALID";
173 return "CKR_WRAPPED_KEY_LEN_RANGE";
175 return "CKR_WRAPPING_KEY_HANDLE_INVALID";
177 return "CKR_WRAPPING_KEY_SIZE_RANGE";
179 return "CKR_WRAPPING_KEY_TYPE_INCONSISTENT";
181 return "CKR_RANDOM_SEED_NOT_SUPPORTED";
186 return "CKR_VENDOR_DEFINED";
188 return "CKR_BUFFER_TOO_SMALL";
190 return "CKR_SAVED_STATE_INVALID";
192 return "CKR_INFORMATION_SENSITIVE";
194 return "CKR_STATE_UNSAVEABLE";
196 return "CKR_CRYPTOKI_NOT_INITIALIZED";
198 return "CKR_CRYPTOKI_ALREADY_INITIALIZED";
200 return "CKR_MUTEX_BAD";
202 return "CKR_MUTEX_NOT_LOCKED";
204 return "Unknown error";
210 const char *message, ...)
214 if (
ctx &&
ctx->error == 0) {
216 ctx->error_action = action;
218 va_start(args, message);
219 vsnprintf(
ctx->error_message,
sizeof(
ctx->error_message),
237hsm_pkcs11_check_error(
hsm_ctx_t *
ctx, CK_RV rv,
const char *action)
240 if (
ctx &&
ctx->error == 0) {
241 ctx->error = (int) rv;
242 ctx->error_action = action;
243 strlcpy(
ctx->error_message, ldns_pkcs11_rv_str(rv),
sizeof(
ctx->error_message));
252hsm_pkcs11_unload_functions(
void *handle)
255#if defined(HAVE_LOADLIBRARY)
257#elif defined(HAVE_DLOPEN)
258 (void) dlclose(handle);
267 CK_C_GetFunctionList pGetFunctionList = NULL;
269 if (module && module->
path) {
272#if defined(HAVE_LOADLIBRARY)
274 HINSTANCE hDLL = LoadLibrary(_T(module->
path));
282 pGetFunctionList = (CK_C_GetFunctionList)
283 GetProcAddress(hDLL, _T(
"C_GetFunctionList"));
285#elif defined(HAVE_DLOPEN)
287 void* pDynLib = dlopen(module->
path, RTLD_NOW | RTLD_LOCAL);
289 if (pDynLib == NULL) {
295 pGetFunctionList = (CK_C_GetFunctionList) functioncast(dlsym(pDynLib,
"C_GetFunctionList"));
297 module->handle = pDynLib;
304#ifdef HAVE_PKCS11_MODULE
305 return C_GetFunctionList(pkcs11_functions);
311 if (pGetFunctionList == NULL) {
327 if (data == NULL || len == NULL)
return;
331 while ((
unsigned short int)(*p) == 0 && l > 1) {
346 const char *token_name)
355 if (hsm_pkcs11_check_error(
ctx, rv,
"C_GetTokenInfo")) {
361 memcpy(token_name_bytes, token_name, strlen(token_name));
366 result = memcmp(token_info.
label,
375 uint8_t use_pubkey, uint8_t allowextract, uint8_t require_backup)
379 if (!name || !module || !tokenlabel)
return NULL;
386 r->
name = strdup(name);
387 r->
module = strdup(module);
394 r->
pin = strdup(pin);
422 const char *token_name, CK_SLOT_ID *slotId)
430 if (token_name == NULL || slotId == NULL)
return HSM_ERROR;
433 if (hsm_pkcs11_check_error(
ctx, rv,
"get slot list")) {
439 "No slots found in HSM");
441 }
else if (slotCount > (SIZE_MAX /
sizeof(CK_SLOT_ID))) {
443 "Too many slots found in HSM");
447 CHECKALLOC(slotIds = malloc(
sizeof(CK_SLOT_ID) * slotCount));
450 if (hsm_pkcs11_check_error(
ctx, rv,
"get slot list")) {
454 for (cur_slot = 0; cur_slot < slotCount; cur_slot++) {
455 if (hsm_pkcs11_check_token_name(
ctx,
459 *slotId = slotIds[cur_slot];
467 "could not find token with the name %s", token_name);
476hsm_module_new(
const char *repository,
477 const char *token_label,
483 if (!repository || !path)
return NULL;
492 module->config = NULL;
496 module->name = strdup(repository);
497 module->token_label = strdup(token_label);
498 module->path = dupstr(path);
499 module->handle = NULL;
509 if (module->
name) free(module->
name);
511 if (module->
path) free(module->
path);
519hsm_session_new(
hsm_module_t *module, CK_SESSION_HANDLE session_handle)
524 session->
session = session_handle;
548 const char *repository,
const char *token_label,
549 const char *module_path,
const char *pin,
556 CK_SESSION_HANDLE session_handle;
557 int first = 1, result;
564 module = hsm_module_new(repository, token_label, module_path, config);
566 rv = hsm_pkcs11_load_functions(module);
569 "hsm_session_init()",
570 "PKCS#11 module load failed: %s", module_path);
571 hsm_module_free(module);
578 if (hsm_pkcs11_check_error(
ctx, rv,
"Initialization")) {
579 hsm_module_free(module);
585 result = hsm_get_slot_id(
ctx, module->
sym, token_label, &
slot_id);
587 hsm_module_free(module);
591 CKF_SERIAL_SESSION | CKF_RW_SESSION,
595 if (hsm_pkcs11_check_error(
ctx, rv,
"Open first session")) {
596 hsm_module_free(module);
601 (unsigned char *) pin,
602 strlen((char *)pin));
605 *session = hsm_session_new(module, session_handle);
609 if (session_handle) {
611 C_CloseSession(session_handle);
612 if (hsm_pkcs11_check_error(
ctx, rv,
613 "finalize after failed login")) {
614 hsm_module_free(module);
622 if (hsm_pkcs11_check_error(
ctx, rv,
"finalize after failed login")) {
623 hsm_module_free(module);
627 hsm_module_free(module);
632 "hsm_session_init()",
633 "Incorrect PIN for repository %s", repository);
647 CK_SESSION_HANDLE session_handle;
651 result = hsm_get_slot_id(
ctx,
655 if (result !=
HSM_OK)
return NULL;
662 if (hsm_pkcs11_check_error(
ctx, rv,
"Clone session")) {
665 new_session = hsm_session_new(session->
module, session_handle);
676 ctx->session_count = 0;
688 for (i = 0; i <
ctx->session_count; i++) {
689 hsm_session_free(
ctx->session[i]);
712 (void) hsm_pkcs11_check_error(
ctx, rv,
"Logout");
717 (void) hsm_pkcs11_check_error(
ctx, rv,
"Close session");
722 (void) hsm_pkcs11_check_error(
ctx, rv,
"Finalize");
725 hsm_module_free(session->
module);
728 hsm_session_free(session);
743 for (i = 0; i <
ctx->session_count; i++) {
744 hsm_session_close(
ctx,
ctx->session[i], unload);
745 ctx->session[i] = NULL;
761 if (!
ctx || !session)
return -1;
763 ctx->session[
ctx->session_count] = session;
764 ctx->session_count++;
777 new_ctx = hsm_ctx_new();
778 for (i = 0; i <
ctx->session_count; i++) {
779 new_session = hsm_session_clone(
ctx,
ctx->session[i]);
783 hsm_ctx_close(new_ctx, 0);
786 hsm_ctx_add_session(new_ctx, new_session);
812 for (i = 0; i <
ctx->session_count; i++) {
813 if (
ctx->session[i] && !strcmp(
ctx->session[i]->module->name, key->
modulename)) {
814 return ctx->session[i];
826 CK_KEY_TYPE key_type;
837 if (hsm_pkcs11_check_error(
ctx, rv,
838 "Get attr value algorithm type")) {
846 if ((
CK_LONG)
template[0].ulValueLen < 1) {
886 if (hsm_pkcs11_check_error(
ctx, rv,
887 "Get attr value algorithm type")) {
891 if ((
CK_ULONG)
template[0].ulValueLen < 1) {
901 if (hsm_pkcs11_check_error(
ctx, rv,
"Could not get the size of the modulus of the private key")) {
906 modulus = (
CK_BYTE_PTR)malloc(template2[0].ulValueLen);
907 template2[0].pValue = modulus;
908 if (modulus == NULL) {
910 "Error allocating memory for modulus");
920 if (hsm_pkcs11_check_error(
ctx, rv,
"Could not get the modulus of the private key")) {
926 modulus_bits = template2[0].ulValueLen * 8;
928 for (
int i = 0; modulus_bits && (modulus[i] & mask) == 0; modulus_bits--) {
962 if (hsm_pkcs11_check_error(
ctx, rv,
"Could not get the size of the prime of the private key")) {
966 return template2[0].ulValueLen * 8;
972static unsigned char *
986 if (!session || !session->
module || !key || !data_len) {
995 if (hsm_pkcs11_check_error(
ctx, rv,
"C_GetAttributeValue")) {
1008 if (hsm_pkcs11_check_error(
ctx, rv,
"get attribute value")) {
1013 if(
value_len !=
template[0].ulValueLen) {
1015 "HSM returned two different length for a same CKA_EC_POINT. " \
1016 "Abnormal behaviour detected.");
1024 "The DER value is too short");
1030 if (
value[0] != 0x04) {
1032 "Invalid identifier octet in the DER value");
1039 if (
value[1] <= 0x7F) {
1041 }
else if (
value[1] == 0x80) {
1043 "Indefinite length is not supported in DER values");
1048 header_len +=
value[1] & 0x80;
1054 "The value is too short");
1061 if (
value[header_len] != 0x04) {
1063 "The value is not uncompressed");
1070 CHECKALLOC(data = malloc(*data_len));
1072 memcpy(data,
value + header_len, *data_len);
1090 if (
value == NULL)
return 0;
1107static unsigned char *
1121 if (!session || !session->
module || !key || !data_len) {
1130 if (hsm_pkcs11_check_error(
ctx, rv,
"C_GetAttributeValue")) {
1138 "Error allocating memory for value");
1148 if (hsm_pkcs11_check_error(
ctx, rv,
"get attribute value")) {
1153 if(
value_len !=
template[0].ulValueLen) {
1155 "HSM returned two different length for the same CKA_EC_POINT. " \
1156 "Abnormal behaviour detected.");
1164 "The DER value is too short");
1170 if (
value[0] != 0x04) {
1172 "Invalid identifier octet in the DER value");
1179 if (
value[1] <= 0x7F) {
1181 }
else if (
value[1] == 0x80) {
1183 "Indefinite length is not supported in DER values");
1188 header_len +=
value[1] & 0x80;
1194 "The value is too short");
1200 data = malloc(*data_len);
1203 "Error allocating memory for data");
1208 memcpy(data,
value + header_len, *data_len);
1227 if (
value == NULL)
return 0;
1261 return hsm_get_key_size_rsa(
ctx, session, key);
1264 return hsm_get_key_size_dsa(
ctx, session, key);
1270 return hsm_get_key_size_ecdsa(
ctx, session, key);
1272 return hsm_get_key_size_eddsa(
ctx, session, key);
1278static CK_OBJECT_HANDLE
1281 CK_OBJECT_CLASS key_class,
1286 CK_OBJECT_HANDLE object;
1290 {
CKA_CLASS, &key_class,
sizeof(key_class) },
1296 if (hsm_pkcs11_check_error(
ctx, rv,
"Find objects init")) {
1304 if (hsm_pkcs11_check_error(
ctx, rv,
"Find object")) {
1306 hsm_pkcs11_check_error(
ctx, rv,
"Find objects cleanup");
1311 if (hsm_pkcs11_check_error(
ctx, rv,
"Find object final")) {
1315 if (objectCount > 0) {
1327static unsigned char *
1328hsm_hex_parse(
const char *hex,
size_t *len)
1330 unsigned char *bytes;
1335 if (!len)
return NULL;
1338 if (!hex)
return NULL;
1339 hex_len = strlen(hex);
1340 if (hex_len % 2 != 0) {
1345 CHECKALLOC(bytes = malloc(*len));
1346 for (i = 0; i < *len; i++) {
1347 bytes[i] = ldns_hexdigit_to_int(hex[2*i]) * 16 +
1348 ldns_hexdigit_to_int(hex[2*i+1]);
1358hsm_hex_unparse(
char *dst,
const unsigned char *src,
size_t len)
1362 for (i = 0; i < len; i++) {
1363 snprintf(dst + (2*i), 3,
"%02x", src[i]);
1375 CK_OBJECT_HANDLE
object,
1391 if (hsm_pkcs11_check_error(
ctx, rv,
"Get attr value")) {
1396 if ((
CK_LONG)
template[0].ulValueLen < 1) {
1402 CHECKALLOC(
template[0].pValue = malloc(
template[0].ulValueLen));
1408 if (hsm_pkcs11_check_error(
ctx, rv,
"Get attr value 2")) {
1410 free(
template[0].pValue);
1414 *len =
template[0].ulValueLen;
1415 return template[0].pValue;
1425 CK_OBJECT_HANDLE
object)
1431 id = hsm_get_id_for_object(
ctx, session,
object, &len);
1433 if (!
id)
return NULL;
1435 key = libhsm_key_new();
1439 key->
public_key = hsm_find_object_handle_for_id(
1467 {
CKA_CLASS, &key_class,
sizeof(key_class) },
1474 CK_OBJECT_HANDLE
object[max_object_count];
1475 CK_OBJECT_HANDLE *key_handles = NULL, *new_key_handles = NULL;
1480 if (hsm_pkcs11_check_error(
ctx, rv,
"Find objects init")) {
1485 while (objectCount > 0) {
1490 if (hsm_pkcs11_check_error(
ctx, rv,
"Find first object")) {
1492 hsm_pkcs11_check_error(
ctx, rv,
"Find objects cleanup");
1496 total_count += objectCount;
1497 if (objectCount > 0 && store) {
1498 if (SIZE_MAX /
sizeof(CK_OBJECT_HANDLE) < total_count) {
1500 "Too much object handle returned by HSM to allocate key_handles");
1504 new_key_handles = realloc(key_handles, total_count *
sizeof(CK_OBJECT_HANDLE));
1505 if (new_key_handles != NULL) {
1506 key_handles = new_key_handles;
1509 "Error allocating memory for object handle (OOM)");
1513 for (i = 0; i < objectCount; i++) {
1514 key_handles[j] =
object[i];
1521 if (hsm_pkcs11_check_error(
ctx, rv,
"Find objects final")) {
1528 "Too much object handle returned by HSM to allocate keys");
1532 CHECKALLOC(keys = malloc(total_count *
sizeof(
libhsm_key_t *)));
1534 for (i = 0; i < total_count; i++) {
1535 key = libhsm_key_new_privkey_object_handle(
ctx, session,
1546 *count = total_count;
1567 return hsm_list_keys_session_internal(
ctx, session, count, 1);
1576 const unsigned char *
id,
size_t len)
1579 CK_OBJECT_HANDLE private_key_handle;
1581 private_key_handle = hsm_find_object_handle_for_id(
1587 if (private_key_handle != 0) {
1588 key = libhsm_key_new_privkey_object_handle(
ctx, session,
1589 private_key_handle);
1607 const unsigned char *
id,
1613 if (!
id)
return NULL;
1621 for (i = 0; i <
ctx->session_count; i++) {
1622 key = hsm_find_key_by_id_session(
ctx,
ctx->session[i],
id, len);
1639hsm_find_repository_session(
hsm_ctx_t *
ctx,
const char *repository)
1643 for (i = 0; i <
ctx->session_count; i++) {
1644 if (
ctx->session[i]) {
1645 return ctx->session[i];
1649 for (i = 0; i <
ctx->session_count; i++) {
1650 if (
ctx->session[i] &&
1651 strcmp(repository,
ctx->session[i]->module->name) == 0)
1653 return ctx->session[i];
1659 "hsm_find_repository_session()",
1660 "Can't find repository: %s", repository);
1674 unsigned long hKey = 0;
1675 unsigned char *data = NULL;
1676 size_t data_size = 0;
1684 if (!session || !session->
module) {
1699 if (hsm_pkcs11_check_error(
ctx, rv,
"C_GetAttributeValue")) {
1702 public_exponent_len =
template[0].ulValueLen;
1703 modulus_len =
template[1].ulValueLen;
1705 CHECKALLOC(public_exponent =
template[0].pValue = malloc(public_exponent_len));
1707 CHECKALLOC(modulus =
template[1].pValue = malloc(modulus_len));
1714 if (hsm_pkcs11_check_error(
ctx, rv,
"get attribute value")) {
1715 free(
template[0].pValue);
1716 free(
template[1].pValue);
1721 hsm_remove_leading_zeroes(public_exponent, &public_exponent_len);
1722 hsm_remove_leading_zeroes(modulus, &modulus_len);
1724 data_size = public_exponent_len + modulus_len + 1;
1725 if (public_exponent_len <= 255) {
1726 CHECKALLOC(data = malloc(data_size));
1727 data[0] = public_exponent_len;
1728 memcpy(&data[1], public_exponent, public_exponent_len);
1729 memcpy(&data[1 + public_exponent_len], modulus, modulus_len);
1730 }
else if (public_exponent_len <= 65535) {
1732 CHECKALLOC(data = malloc(data_size));
1734 ldns_write_uint16(&data[1], (uint16_t) public_exponent_len);
1735 memcpy(&data[3], public_exponent, public_exponent_len);
1736 memcpy(&data[3 + public_exponent_len], modulus, modulus_len);
1739 "Public exponent too big");
1740 free(public_exponent);
1744 rdf = ldns_rdf_new(LDNS_RDF_TYPE_B64, data_size, data);
1745 free(public_exponent);
1764 unsigned char *data = NULL;
1765 size_t data_size = 0;
1775 if (!session || !session->
module) {
1785 if (hsm_pkcs11_check_error(
ctx, rv,
"C_GetAttributeValue")) {
1788 prime_len =
template[0].ulValueLen;
1789 subprime_len =
template[1].ulValueLen;
1790 base_len =
template[2].ulValueLen;
1793 CHECKALLOC(prime =
template[0].pValue = malloc(prime_len));
1795 CHECKALLOC(subprime =
template[1].pValue = malloc(subprime_len));
1797 CHECKALLOC(base =
template[2].pValue = malloc(base_len));
1806 if (hsm_pkcs11_check_error(
ctx, rv,
"get attribute value")) {
1814 data_size = prime_len + subprime_len + base_len +
value_len + 1;
1815 CHECKALLOC(data = malloc(data_size));
1816 data[0] = (prime_len - 64) / 8;
1817 memcpy(&data[1], subprime, subprime_len);
1818 memcpy(&data[1 + subprime_len], prime, prime_len);
1819 memcpy(&data[1 + subprime_len + prime_len], base, base_len);
1820 memcpy(&data[1 + subprime_len + prime_len + base_len],
value,
value_len);
1822 rdf = ldns_rdf_new(LDNS_RDF_TYPE_B64, data_size, data);
1844 if (!session || !session->
module) {
1854 if (hsm_pkcs11_check_error(
ctx, rv,
"C_GetAttributeValue")) {
1866 if (hsm_pkcs11_check_error(
ctx, rv,
"get attribute value")) {
1882 if (
value == NULL)
return NULL;
1884 ldns_rdf *rdf = ldns_rdf_new(LDNS_RDF_TYPE_B64,
value_len,
value);
1896 if (
value == NULL)
return NULL;
1898 ldns_rdf *rdf = ldns_rdf_new(LDNS_RDF_TYPE_B64,
value_len,
value);
1907 switch (hsm_get_key_algorithm(
ctx, session, key)) {
1909 return hsm_get_key_rdata_rsa(
ctx, session, key);
1912 return hsm_get_key_rdata_dsa(
ctx, session, key);
1915 return hsm_get_key_rdata_gost(
ctx, session, key);
1918 return hsm_get_key_rdata_ecdsa(
ctx, session, key);
1920 return hsm_get_key_rdata_eddsa(
ctx, session, key);
1931hsm_create_prefix(
CK_ULONG digest_len,
1936 const CK_BYTE RSA_MD5_ID[] = { 0x30, 0x20, 0x30, 0x0C, 0x06, 0x08, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x02, 0x05, 0x05, 0x00, 0x04, 0x10 };
1937 const CK_BYTE RSA_SHA1_ID[] = { 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2B, 0x0E, 0x03, 0x02, 0x1A, 0x05, 0x00, 0x04, 0x14 };
1938 const CK_BYTE RSA_SHA256_ID[] = { 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20 };
1939 const CK_BYTE RSA_SHA512_ID[] = { 0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03, 0x05, 0x00, 0x04, 0x40 };
1941 switch((ldns_signing_algorithm)
algorithm) {
1942 case LDNS_SIGN_RSAMD5:
1943 *data_size =
sizeof(RSA_MD5_ID) + digest_len;
1944 CHECKALLOC(data = malloc(*data_size));
1945 memcpy(data, RSA_MD5_ID,
sizeof(RSA_MD5_ID));
1947 case LDNS_SIGN_RSASHA1:
1948 case LDNS_SIGN_RSASHA1_NSEC3:
1949 *data_size =
sizeof(RSA_SHA1_ID) + digest_len;
1950 CHECKALLOC(data = malloc(*data_size));
1951 memcpy(data, RSA_SHA1_ID,
sizeof(RSA_SHA1_ID));
1953 case LDNS_SIGN_RSASHA256:
1954 *data_size =
sizeof(RSA_SHA256_ID) + digest_len;
1955 CHECKALLOC(data = malloc(*data_size));
1956 memcpy(data, RSA_SHA256_ID,
sizeof(RSA_SHA256_ID));
1958 case LDNS_SIGN_RSASHA512:
1959 *data_size =
sizeof(RSA_SHA512_ID) + digest_len;
1960 CHECKALLOC(data = malloc(*data_size));
1961 memcpy(data, RSA_SHA512_ID,
sizeof(RSA_SHA512_ID));
1964 case LDNS_SIGN_DSA_NSEC3:
1965 case LDNS_SIGN_ECC_GOST:
1966 case LDNS_SIGN_ECDSAP256SHA256:
1967 case LDNS_SIGN_ECDSAP384SHA384:
1968 *data_size = digest_len;
1969 CHECKALLOC(data = malloc(*data_size));
1980 CK_MECHANISM_TYPE mechanism_type,
1982 ldns_buffer *sign_buf)
1988 digest_mechanism.pParameter = NULL;
1989 digest_mechanism.ulParameterLen = 0;
1990 digest_mechanism.
mechanism = mechanism_type;
1991 CHECKALLOC(digest = malloc(digest_len));
1994 if (hsm_pkcs11_check_error(
ctx, rv,
"HSM digest init")) {
2000 ldns_buffer_begin(sign_buf),
2001 ldns_buffer_position(sign_buf),
2004 if (hsm_pkcs11_check_error(
ctx, rv,
"HSM digest")) {
2013 ldns_buffer *sign_buf,
2022 int data_direct = 0;
2033 session = hsm_find_key_session(
ctx, key);
2034 if (!session)
return NULL;
2039 switch ((ldns_signing_algorithm)
algorithm) {
2040 case LDNS_SIGN_RSAMD5:
2042 digest = hsm_digest_through_hsm(
ctx, session,
2046 case LDNS_SIGN_RSASHA1:
2047 case LDNS_SIGN_RSASHA1_NSEC3:
2049 case LDNS_SIGN_DSA_NSEC3:
2050 digest_len = LDNS_SHA1_DIGEST_LENGTH;
2051 CHECKALLOC(digest = malloc(digest_len));
2052 digest = ldns_sha1(ldns_buffer_begin(sign_buf),
2053 ldns_buffer_position(sign_buf),
2057 case LDNS_SIGN_RSASHA256:
2058 case LDNS_SIGN_ECDSAP256SHA256:
2059 digest_len = LDNS_SHA256_DIGEST_LENGTH;
2060 CHECKALLOC(digest = malloc(digest_len));
2061 digest = ldns_sha256(ldns_buffer_begin(sign_buf),
2062 ldns_buffer_position(sign_buf),
2065 case LDNS_SIGN_ECDSAP384SHA384:
2066 digest_len = LDNS_SHA384_DIGEST_LENGTH;
2067 CHECKALLOC(digest = malloc(digest_len));
2068 digest = ldns_sha384(ldns_buffer_begin(sign_buf),
2069 ldns_buffer_position(sign_buf),
2072 case LDNS_SIGN_RSASHA512:
2073 digest_len = LDNS_SHA512_DIGEST_LENGTH;
2074 CHECKALLOC(digest = malloc(digest_len));
2075 digest = ldns_sha512(ldns_buffer_begin(sign_buf),
2076 ldns_buffer_position(sign_buf),
2079 case LDNS_SIGN_ECC_GOST:
2081 digest = hsm_digest_through_hsm(
ctx, session,
2085#if (LDNS_REVISION >= ((1<<16)|(7<<8)|(0)))
2086 case LDNS_SIGN_ED25519:
2089 case LDNS_SIGN_ED448:
2099 if (!data_direct && !digest) {
2104 data = ldns_buffer_begin(sign_buf);
2105 data_len = ldns_buffer_position(sign_buf);
2110 data = hsm_create_prefix(digest_len,
algorithm, &data_len);
2111 memcpy(data + data_len - digest_len, digest, digest_len);
2114 sign_mechanism.pParameter = NULL;
2115 sign_mechanism.ulParameterLen = 0;
2116 switch((ldns_signing_algorithm)
algorithm) {
2117 case LDNS_SIGN_RSAMD5:
2118 case LDNS_SIGN_RSASHA1:
2119 case LDNS_SIGN_RSASHA1_NSEC3:
2120 case LDNS_SIGN_RSASHA256:
2121 case LDNS_SIGN_RSASHA512:
2125 case LDNS_SIGN_DSA_NSEC3:
2128 case LDNS_SIGN_ECC_GOST:
2131 case LDNS_SIGN_ECDSAP256SHA256:
2132 case LDNS_SIGN_ECDSAP384SHA384:
2135#if (LDNS_REVISION >= ((1<<16)|(7<<8)|(0)))
2136 case LDNS_SIGN_ED25519:
2139 case LDNS_SIGN_ED448:
2155 if (hsm_pkcs11_check_error(
ctx, rv,
"sign init")) {
2166 if (hsm_pkcs11_check_error(
ctx, rv,
"sign final")) {
2174 sig_rdf = ldns_rdf_new_frm_data(LDNS_RDF_TYPE_B64,
2188hsm_dname_is_wildcard(
const ldns_rdf* dname)
2190 return ( ldns_dname_label_count(dname) > 0 &&
2191 ldns_rdf_data(dname)[0] == 1 &&
2192 ldns_rdf_data(dname)[1] ==
'*');
2196hsm_create_empty_rrsig(
const ldns_rr_list *rrset,
2201 uint32_t orig_class;
2203 uint8_t label_count;
2205 label_count = ldns_dname_label_count(
2206 ldns_rr_owner(ldns_rr_list_rr(rrset, 0)));
2208 if (hsm_dname_is_wildcard(ldns_rr_owner(ldns_rr_list_rr(rrset, 0)))) {
2212 rrsig = ldns_rr_new_frm_type(LDNS_RR_TYPE_RRSIG);
2215 orig_ttl = ldns_rr_ttl(ldns_rr_list_rr(rrset, 0));
2216 orig_class = ldns_rr_get_class(ldns_rr_list_rr(rrset, 0));
2218 ldns_rr_set_class(rrsig, orig_class);
2219 ldns_rr_set_ttl(rrsig, orig_ttl);
2220 ldns_rr_set_owner(rrsig,
2223 ldns_rr_list_rr(rrset,
2229 (void)ldns_rr_rrsig_set_origttl(
2231 ldns_native2rdf_int32(LDNS_RDF_TYPE_INT32,
2234 (void)ldns_rr_rrsig_set_signame(
2236 ldns_rdf_clone(sign_params->
owner));
2238 (void)ldns_rr_rrsig_set_labels(
2240 ldns_native2rdf_int8(LDNS_RDF_TYPE_INT8,
2245 (void)ldns_rr_rrsig_set_inception(
2247 ldns_native2rdf_int32(
2251 (void)ldns_rr_rrsig_set_inception(
2253 ldns_native2rdf_int32(LDNS_RDF_TYPE_TIME, now));
2256 (void)ldns_rr_rrsig_set_expiration(
2258 ldns_native2rdf_int32(
2262 (void)ldns_rr_rrsig_set_expiration(
2264 ldns_native2rdf_int32(
2266 now + LDNS_DEFAULT_EXP_TIME));
2269 (void)ldns_rr_rrsig_set_keytag(
2271 ldns_native2rdf_int16(LDNS_RDF_TYPE_INT16,
2274 (void)ldns_rr_rrsig_set_algorithm(
2276 ldns_native2rdf_int8(
2280 (void)ldns_rr_rrsig_set_typecovered(
2282 ldns_native2rdf_int16(
2284 ldns_rr_get_type(ldns_rr_list_rr(rrset,
2297 char *(pin_callback)(
unsigned int,
const char *,
unsigned int))
2301 char* module_pin = NULL;
2304 int repositories = 0;
2315 hsm_config_default(&module_config);
2321 repo->
module, repo->
pin, &module_config);
2327 module_pin = pin_callback(
_hsm_ctx->session_count,
2329 if (module_pin == NULL)
break;
2331 repo->
module, module_pin, &module_config);
2333 pin_callback(
_hsm_ctx->session_count - 1,
2336 memset(module_pin, 0, strlen(module_pin));
2342 "No pin or callback function");
2353 if (result ==
HSM_OK && repositories == 0) {
2355 "No repositories found");
2389 CK_SESSION_HANDLE session_handle;
2395 for (i = 0; i <
ctx->session_count; i++) {
2396 session =
ctx->session[i];
2397 if (session == NULL)
continue;
2403 if (hsm_pkcs11_check_error(
ctx, rv,
"get session info")) {
2411 "Session not logged in");
2422 if (hsm_pkcs11_check_error(
ctx, rv,
"test open session")) {
2427 if (hsm_pkcs11_check_error(
ctx, rv,
"test close session")) {
2440 hsm_ctx_close(
ctx, 0);
2452 params->
flags = LDNS_KEY_ZONE_KEY;
2456 params->
owner = NULL;
2464 if (params->
owner) ldns_rdf_deep_free(params->
owner);
2480 size_t key_count = 0;
2481 size_t cur_key_count;
2485 for (i = 0; i <
ctx->session_count; i++) {
2486 session_keys = hsm_list_keys_session(
ctx,
ctx->session[i],
2488 CHECKALLOC(keys = realloc(keys,
2489 (key_count + cur_key_count) *
sizeof(
libhsm_key_t *)));
2490 for (j = 0; j < cur_key_count; j++) {
2491 keys[key_count + j] = session_keys[j];
2493 key_count += cur_key_count;
2505 const char *repository)
2509 if (!repository)
return NULL;
2511 session = hsm_find_repository_session(
ctx, repository);
2516 return hsm_list_keys_session(
ctx, session, count);
2522 unsigned char *id_bytes;
2526 id_bytes = hsm_hex_parse(
id, &len);
2528 if (!id_bytes)
return NULL;
2530 key = hsm_find_key_by_id_bin(
ctx, id_bytes, len);
2536generate_unique_id(
hsm_ctx_t *
ctx,
unsigned char *buf,
size_t bufsize)
2541 while ((key = hsm_find_key_by_id_bin(
ctx, buf, bufsize))) {
2550 const char *repository,
2551 unsigned long keysize)
2556 unsigned char id[16];
2560 CK_OBJECT_HANDLE publicKey, privateKey;
2561 CK_KEY_TYPE keyType =
CKK_RSA;
2565 CK_BYTE publicExponent[] = { 1, 0, 1 };
2571 session = hsm_find_repository_session(
ctx, repository);
2572 if (!session)
return NULL;
2575 generate_unique_id(
ctx,
id, 16);
2579 hsm_hex_unparse(id_str,
id, 16);
2591 {
CKA_WRAP, &cfalse,
sizeof(cfalse) },
2601 {
CKA_SIGN, &ctrue,
sizeof (ctrue) },
2612 publicKeyTemplate, 9,
2613 privateKeyTemplate, 10,
2616 if (hsm_pkcs11_check_error(
ctx, rv,
"generate key pair")) {
2620 new_key = libhsm_key_new();
2638 const char *repository,
2639 unsigned long keysize)
2644 CK_OBJECT_HANDLE domainPar, publicKey, privateKey;
2650 unsigned char id[16];
2654 session = hsm_find_repository_session(
ctx, repository);
2655 if (!session)
return NULL;
2658 generate_unique_id(
ctx,
id, 16);
2662 hsm_hex_unparse(id_str,
id, 16);
2664 CK_KEY_TYPE keyType =
CKK_DSA;
2684 {
CKA_BASE, dsa_g,
sizeof(dsa_g) },
2690 {
CKA_WRAP, &cfalse,
sizeof(cfalse) },
2698 {
CKA_SIGN, &ctrue,
sizeof(ctrue) },
2715 if (hsm_pkcs11_check_error(
ctx, rv,
"generate domain parameters")) {
2720 domainPar, publicKeyTemplate, 3);
2721 if (hsm_pkcs11_check_error(
ctx, rv,
"get domain parameters")) {
2726 if (hsm_pkcs11_check_error(
ctx, rv,
"destroy domain parameters")) {
2734 publicKeyTemplate, 10,
2735 privateKeyTemplate, 10,
2738 if (hsm_pkcs11_check_error(
ctx, rv,
"generate key pair")) {
2742 new_key = libhsm_key_new();
2752 const char *repository)
2757 CK_OBJECT_HANDLE publicKey, privateKey;
2763 unsigned char id[16];
2767 session = hsm_find_repository_session(
ctx, repository);
2768 if (!session)
return NULL;
2771 generate_unique_id(
ctx,
id, 16);
2775 hsm_hex_unparse(id_str,
id, 16);
2782 CK_BYTE oid1[] = { 0x06, 0x07, 0x2A, 0x85, 0x03, 0x02, 0x02, 0x23, 0x01 };
2783 CK_BYTE oid2[] = { 0x06, 0x07, 0x2A, 0x85, 0x03, 0x02, 0x02, 0x1E, 0x01 };
2793 {
CKA_WRAP, &cfalse,
sizeof(cfalse) },
2801 {
CKA_SIGN, &ctrue,
sizeof(ctrue) },
2814 publicKeyTemplate, 9,
2815 privateKeyTemplate, 10,
2818 if (hsm_pkcs11_check_error(
ctx, rv,
"generate key pair")) {
2822 new_key = libhsm_key_new();
2832 const char *repository,
2838 CK_OBJECT_HANDLE publicKey, privateKey;
2844 unsigned char id[16];
2848 session = hsm_find_repository_session(
ctx, repository);
2849 if (!session)
return NULL;
2852 generate_unique_id(
ctx,
id, 16);
2856 hsm_hex_unparse(id_str,
id, 16);
2858 CK_KEY_TYPE keyType =
CKK_EC;
2863 CK_BYTE oidP256[] = { 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07 };
2864 CK_BYTE oidP384[] = { 0x06, 0x05, 0x2B, 0x81, 0x04, 0x00, 0x22 };
2873 {
CKA_WRAP, &cfalse,
sizeof(cfalse) },
2881 {
CKA_SIGN, &ctrue,
sizeof(ctrue) },
2891 if (strcmp(curve,
"P-256") == 0)
2893 publicKeyTemplate[0].pValue = oidP256;
2894 publicKeyTemplate[0].ulValueLen =
sizeof(oidP256);
2896 else if (strcmp(curve,
"P-384") == 0)
2898 publicKeyTemplate[0].pValue = oidP384;
2899 publicKeyTemplate[0].ulValueLen =
sizeof(oidP384);
2910 publicKeyTemplate, 8,
2911 privateKeyTemplate, 10,
2914 if (hsm_pkcs11_check_error(
ctx, rv,
"generate key pair")) {
2918 new_key = libhsm_key_new();
2928 const char *repository,
2934 CK_OBJECT_HANDLE publicKey, privateKey;
2940 unsigned char id[16];
2944 session = hsm_find_repository_session(
ctx, repository);
2945 if (!session)
return NULL;
2948 generate_unique_id(
ctx,
id, 16);
2952 hsm_hex_unparse(id_str,
id, 16);
2959 CK_BYTE oid25519[] = { 0x06, 0x03, 0x2B, 0x65, 0x70 };
2960 CK_BYTE oid448[] = { 0x06, 0x03, 0x2B, 0x65, 0x71 };
2969 {
CKA_WRAP, &cfalse,
sizeof(cfalse) },
2977 {
CKA_SIGN, &ctrue,
sizeof(ctrue) },
2987 if (strcmp(curve,
"edwards25519") == 0)
2989 publicKeyTemplate[0].pValue = oid25519;
2990 publicKeyTemplate[0].ulValueLen =
sizeof(oid25519);
2992 else if (strcmp(curve,
"edwards448") == 0)
2994 publicKeyTemplate[0].pValue = oid448;
2995 publicKeyTemplate[0].ulValueLen =
sizeof(oid448);
3006 publicKeyTemplate, 8,
3007 privateKeyTemplate, 10,
3010 if (hsm_pkcs11_check_error(
ctx, rv,
"generate key pair")) {
3014 new_key = libhsm_key_new();
3027 if (!key)
return -1;
3029 session = hsm_find_key_session(
ctx, key);
3030 if (!session)
return -2;
3034 if (hsm_pkcs11_check_error(
ctx, rv,
"Destroy private key")) {
3042 if (hsm_pkcs11_check_error(
ctx, rv,
"Destroy public key")) {
3055 for (i = 0; i < count; i++) {
3069 if (!key)
return NULL;
3071 session = hsm_find_key_session(
ctx, key);
3072 if (!session)
return NULL;
3074 id = hsm_get_id_for_object(
ctx, session, key->
private_key, &len);
3075 if (!
id)
return NULL;
3078 CHECKALLOC(id_str = malloc(len * 2 + 1));
3080 hsm_hex_unparse(id_str,
id, len);
3094 session = hsm_find_key_session(
ctx, key);
3095 if (!session)
return NULL;
3100 if (key_info->
id == NULL) {
3101 key_info->
id = strdup(
"");
3104 key_info->
algorithm = (
unsigned long) hsm_get_key_algorithm(
ctx,
3107 key_info->
keysize = (
unsigned long) hsm_get_key_size(
ctx,
3154 const ldns_rr_list* rrset,
3159 ldns_buffer *sign_buf;
3163 if (!key)
return NULL;
3164 if (!sign_params)
return NULL;
3166 signature = hsm_create_empty_rrsig((ldns_rr_list *)rrset,
3172 sign_buf = ldns_buffer_new(LDNS_MAX_PACKETLEN);
3174 if (ldns_rrsig2buffer_wire(sign_buf, signature)
3175 != LDNS_STATUS_OK) {
3176 ldns_buffer_free(sign_buf);
3178 ldns_rr_free(signature);
3183 for(i = 0; i < ldns_rr_list_rr_count(rrset); i++) {
3184 ldns_rr2canonical(ldns_rr_list_rr(rrset, i));
3188 if (ldns_rr_list2buffer_wire(sign_buf, rrset)
3189 != LDNS_STATUS_OK) {
3190 ldns_buffer_free(sign_buf);
3191 ldns_rr_free(signature);
3195 b64_rdf = hsm_sign_buffer(
ctx, sign_buf, key, sign_params->
algorithm);
3197 ldns_buffer_free(sign_buf);
3200 ldns_rr_free(signature);
3204 ldns_rr_rrsig_set_sig(signature, b64_rdf);
3232 sign_params->
owner = ldns_rdf_new_frm_str(LDNS_RDF_TYPE_DNAME,
"dummy");
3233 sign_params->
algorithm = (ldns_algorithm) alg;
3234 sign_params->
flags = LDNS_KEY_ZONE_KEY;
3236 sign_params->
flags |= LDNS_KEY_SEP_KEY;
3253 tag = ldns_calc_keytag(dnskey_rr);
3255 ldns_rr_free(dnskey_rr);
3283 session = hsm_find_key_session(
ctx, key);
3284 if (!session)
return NULL;
3286 dnskey = ldns_rr_new();
3287 ldns_rr_set_type(dnskey, LDNS_RR_TYPE_DNSKEY);
3289 ldns_rr_set_owner(dnskey, ldns_rdf_clone(sign_params->
owner));
3291 ldns_rr_push_rdf(dnskey,
3292 ldns_native2rdf_int16(LDNS_RDF_TYPE_INT16,
3293 sign_params->
flags));
3294 ldns_rr_push_rdf(dnskey,
3295 ldns_native2rdf_int8(LDNS_RDF_TYPE_INT8,
3296 LDNS_DNSSEC_KEYPROTO));
3297 ldns_rr_push_rdf(dnskey,
3298 ldns_native2rdf_int8(LDNS_RDF_TYPE_ALG,
3301 rdata = hsm_get_key_rdata(
ctx, session, key);
3302 if (rdata == NULL) {
3303 ldns_rr_free(dnskey);
3306 ldns_rr_push_rdf(dnskey, rdata);
3313 unsigned char *buffer,
3314 unsigned long length)
3319 if (!buffer)
return -1;
3323 for (i = 0; i <
ctx->session_count; i++) {
3324 session =
ctx->session[i];
3343 unsigned char rnd_buf[4];
3346 memcpy(&rnd, rnd_buf, 4);
3358 unsigned char rnd_buf[8];
3361 memcpy(&rnd, rnd_buf, 8);
3374 const char *token_label,
3382 result = hsm_session_init(
_hsm_ctx,
3390 result = hsm_ctx_add_session(
_hsm_ctx, session);
3399 for (i = 0; i <
ctx->session_count; i++) {
3400 if (
ctx->session[i] &&
3401 strcmp(
ctx->session[i]->module->name, repository) == 0) {
3407 "hsm_token_attached()",
3408 "Can't find repository: %s", repository);
3431 ctx->error_action ?
ctx->error_action :
"unknown()",
3432 ctx->error_message[0] ?
ctx->error_message :
"unknown error");
3447 printf(
"\t\tmodule at %p (sym %p)\n", (
void *) session->
module, (
void *) session->
module->
sym);
3448 printf(
"\t\tmodule path: %s\n", session->
module->
path);
3449 printf(
"\t\trepository name: %s\n", session->
module->
name);
3451 printf(
"\t\tsess handle: %u\n", (
unsigned int) session->
session);
3457 printf(
"CTX Sessions: %lu\n",
3458 (
long unsigned int)
ctx->session_count);
3459 for (i = 0; i <
ctx->session_count; i++) {
3460 printf(
"\tSession at %p\n", (
void *)
ctx->session[i]);
3472 printf(
"\tprivkey handle: %u\n", (
unsigned int) key->
private_key);
3474 printf(
"\tpubkey handle: %u\n", (
unsigned int) key->
public_key);
3476 printf(
"\tpubkey handle: %s\n",
"NULL");
3478 printf(
"\trepository: %s\n", key->
modulename);
3480 printf(
"\tsize: %lu\n", key_info->
keysize);
3481 printf(
"\tid: %s\n", key_info->
id);
3484 printf(
"key: hsm_get_key_info() returned NULL\n");
3487 printf(
"key: <void>\n");
3499 fprintf(stderr,
"%s\n", message);
3502 fprintf(stderr,
"Unknown error\n");
3516 for (i = 0; i <
ctx->session_count; i++) {
3517 session =
ctx->session[i];
3519 result = hsm_get_slot_id(
ctx,
3523 if (result !=
HSM_OK)
return;
3526 if (hsm_pkcs11_check_error(
ctx, rv,
"C_GetTokenInfo")) {
3530 printf(
"Repository: %s\n",session->
module->
name);
3532 printf(
"\tModule: %s\n", session->
module->
path);
3533 printf(
"\tSlot: %lu\n",
slot_id);
3534 printf(
"\tToken Label: %.*s\n",
3535 (
int)
sizeof(token_info.
label), token_info.
label);
3536 printf(
"\tManufacturer: %.*s\n",
3537 (
int)
sizeof(token_info.manufacturerID), token_info.manufacturerID);
3538 printf(
"\tModel: %.*s\n",
3539 (
int)
sizeof(token_info.
model), token_info.
model);
3540 printf(
"\tSerial: %.*s\n",
3541 (
int)
sizeof(token_info.serialNumber), token_info.serialNumber);
3543 if (i + 1 !=
ctx->session_count)
3549keycache_cmpfunc(
const void* a,
const void* b)
3551 const char* x = (
const char*)a;
3552 const char* y = (
const char*)b;
3553 return strcmp(x, y);
3557keycache_delfunc(ldns_rbnode_t* node,
void* cargo)
3560 free((
void*)node->key);
3562 free((
void*)node->data);
3569 ctx->keycache = ldns_rbtree_create(keycache_cmpfunc);
3570 CHECKALLOC(
_hsm_ctx->keycache_lock = malloc(
sizeof (pthread_mutex_t)));
3571 pthread_mutex_init(
_hsm_ctx->keycache_lock, NULL);
3577 ldns_traverse_postorder(
ctx->keycache, keycache_delfunc, NULL);
3578 ldns_rbtree_free(
ctx->keycache);
3579 pthread_mutex_destroy(
ctx->keycache_lock);
3580 free(
ctx->keycache_lock);
3581 ctx->keycache_lock = NULL;
3587 ldns_rbnode_t* node;
3589 pthread_mutex_lock(
ctx->keycache_lock);
3590 node = ldns_rbtree_search(
ctx->keycache, locator);
3591 pthread_mutex_unlock(
ctx->keycache_lock);
3592 if (node == LDNS_RBTREE_NULL || node == NULL) {
3597 CHECKALLOC(node = malloc(
sizeof(ldns_rbnode_t)));
3598 node->key = strdup(locator);
3600 pthread_mutex_lock(
ctx->keycache_lock);
3601 node = ldns_rbtree_insert(
ctx->keycache, node);
3602 pthread_mutex_unlock(
ctx->keycache_lock);
3606 if (node == LDNS_RBTREE_NULL || node == NULL)
hsm_repository_t * hsm_repository_new(char *name, char *module, char *tokenlabel, char *pin, uint8_t use_pubkey, uint8_t allowextract, uint8_t require_backup)
ldns_rr * hsm_sign_rrset(hsm_ctx_t *ctx, const ldns_rr_list *rrset, const libhsm_key_t *key, const hsm_sign_params_t *sign_params)
void libhsm_key_list_free(libhsm_key_t **key_list, size_t count)
void hsm_ctx_set_error(hsm_ctx_t *ctx, int error, const char *action, const char *message,...)
const libhsm_key_t * keycache_lookup(hsm_ctx_t *ctx, const char *locator)
pthread_mutex_t _hsm_ctx_mutex
uint64_t hsm_random64(hsm_ctx_t *ctx)
char * hsm_get_error(hsm_ctx_t *gctx)
uint32_t hsm_random32(hsm_ctx_t *ctx)
void hsm_print_error(hsm_ctx_t *gctx)
libhsm_key_t * hsm_find_key_by_id(hsm_ctx_t *ctx, const char *id)
int hsm_token_attached(hsm_ctx_t *ctx, const char *repository)
#define HSM_TOKEN_LABEL_LENGTH
libhsm_key_t * hsm_generate_rsa_key(hsm_ctx_t *ctx, const char *repository, unsigned long keysize)
libhsm_key_t * hsm_generate_gost_key(hsm_ctx_t *ctx, const char *repository)
void hsm_print_key(hsm_ctx_t *ctx, libhsm_key_t *key)
hsm_sign_params_t * hsm_sign_params_new()
void hsm_print_ctx(hsm_ctx_t *ctx)
void hsm_repository_free(hsm_repository_t *r)
int hsm_attach(const char *repository, const char *token_label, const char *path, const char *pin, const hsm_config_t *config)
char * hsm_get_key_id(hsm_ctx_t *ctx, const libhsm_key_t *key)
void libhsm_key_info_free(libhsm_key_info_t *key_info)
libhsm_key_t ** hsm_list_keys_repository(hsm_ctx_t *ctx, size_t *count, const char *repository)
int hsm_random_buffer(hsm_ctx_t *ctx, unsigned char *buffer, unsigned long length)
int hsm_open2(hsm_repository_t *rlist, char *(pin_callback)(unsigned int, const char *, unsigned int))
ldns_rr * hsm_get_dnskey(hsm_ctx_t *ctx, const libhsm_key_t *key, const hsm_sign_params_t *sign_params)
libhsm_key_t * hsm_generate_dsa_key(hsm_ctx_t *ctx, const char *repository, unsigned long keysize)
libhsm_key_t * hsm_generate_eddsa_key(hsm_ctx_t *ctx, const char *repository, const char *curve)
int hsm_keytag(const char *loc, int alg, int sep, uint16_t *keytag)
libhsm_key_t * hsm_generate_ecdsa_key(hsm_ctx_t *ctx, const char *repository, const char *curve)
void hsm_print_session(hsm_session_t *session)
void keycache_create(hsm_ctx_t *ctx)
int hsm_remove_key(hsm_ctx_t *ctx, libhsm_key_t *key)
void hsm_print_tokeninfo(hsm_ctx_t *ctx)
libhsm_key_info_t * hsm_get_key_info(hsm_ctx_t *ctx, const libhsm_key_t *key)
void hsm_destroy_context(hsm_ctx_t *ctx)
libhsm_key_t ** hsm_list_keys(hsm_ctx_t *ctx, size_t *count)
void keycache_destroy(hsm_ctx_t *ctx)
void libhsm_key_free(libhsm_key_t *key)
void hsm_sign_params_free(hsm_sign_params_t *params)
hsm_ctx_t * hsm_create_context()
#define HSM_NO_REPOSITORIES
struct hsm_repository_struct hsm_repository_t
#define HSM_ERROR_MSGSIZE
#define HSM_MODULE_NOT_FOUND
#define HSM_REPOSITORY_NOT_FOUND
#define HSM_MAX_SIGNATURE_LENGTH
#define HSM_PIN_INCORRECT
#define CKM_DSA_PARAMETER_GEN
#define CKR_SESSION_COUNT
#define CKR_DEVICE_MEMORY
#define CKR_GENERAL_ERROR
#define CKR_MECHANISM_INVALID
#define CKR_SLOT_ID_INVALID
#define CKR_ATTRIBUTE_TYPE_INVALID
#define CKR_FUNCTION_CANCELED
#define CKR_UNWRAPPING_KEY_HANDLE_INVALID
#define CKR_WRAPPING_KEY_SIZE_RANGE
#define CKR_MECHANISM_PARAM_INVALID
#define CKR_TOKEN_NOT_RECOGNIZED
#define CKR_ATTRIBUTE_SENSITIVE
struct ck_token_info CK_TOKEN_INFO
#define CKR_PIN_LEN_RANGE
#define CKR_RANDOM_SEED_NOT_SUPPORTED
#define CKR_SESSION_PARALLEL_NOT_SUPPORTED
#define CKR_ATTRIBUTE_READ_ONLY
#define CKR_WRAPPING_KEY_TYPE_INCONSISTENT
#define CKR_SESSION_EXISTS
unsigned long int CK_ULONG
#define CKA_PUBLIC_EXPONENT
#define CKM_RSA_PKCS_KEY_PAIR_GEN
#define CKR_SESSION_READ_ONLY
struct ck_session_info CK_SESSION_INFO
#define CKA_GOSTR3410PARAMS
#define CKR_ENCRYPTED_DATA_LEN_RANGE
#define CKR_SIGNATURE_INVALID
#define CKR_SESSION_HANDLE_INVALID
struct ck_function_list * CK_FUNCTION_LIST_PTR
#define CKR_TOKEN_NOT_PRESENT
#define CKR_CRYPTOKI_NOT_INITIALIZED
#define CKR_PIN_INCORRECT
#define CKR_WRAPPED_KEY_INVALID
#define CKR_WRAPPING_KEY_HANDLE_INVALID
#define CKR_MUTEX_NOT_LOCKED
#define CKR_SESSION_CLOSED
#define CKM_DSA_KEY_PAIR_GEN
#define CKF_OS_LOCKING_OK
#define CKM_EC_EDWARDS_KEY_PAIR_GEN
#define CKR_OBJECT_HANDLE_INVALID
#define CKR_UNWRAPPING_KEY_SIZE_RANGE
struct ck_mechanism CK_MECHANISM
#define CKR_ENCRYPTED_DATA_INVALID
#define CKR_CRYPTOKI_ALREADY_INITIALIZED
#define CKF_SERIAL_SESSION
#define CKR_FUNCTION_FAILED
#define CKR_OPERATION_NOT_INITIALIZED
#define CKM_EC_KEY_PAIR_GEN
#define CKR_KEY_SIZE_RANGE
#define CKR_TEMPLATE_INCONSISTENT
#define CKR_OPERATION_ACTIVE
#define CKR_DATA_LEN_RANGE
struct ck_function_list ** CK_FUNCTION_LIST_PTR_PTR
#define CKR_BUFFER_TOO_SMALL
#define CKS_RW_USER_FUNCTIONS
#define CKR_USER_PIN_NOT_INITIALIZED
#define CKR_USER_ALREADY_LOGGED_IN
#define CKR_STATE_UNSAVEABLE
#define CKR_INFORMATION_SENSITIVE
struct ck_attribute CK_ATTRIBUTE
#define CKR_ATTRIBUTE_VALUE_INVALID
#define CKR_FUNCTION_NOT_SUPPORTED
struct ck_c_initialize_args CK_C_INITIALIZE_ARGS
#define CKR_USER_NOT_LOGGED_IN
#define CKR_DEVICE_REMOVED
#define CKR_TOKEN_WRITE_PROTECTED
#define CKR_TEMPLATE_INCOMPLETE
#define CKM_GOSTR3410_KEY_PAIR_GEN
#define CKR_FUNCTION_NOT_PARALLEL
#define CKR_SIGNATURE_LEN_RANGE
#define CKR_USER_TYPE_INVALID
#define CKR_KEY_HANDLE_INVALID
#define CKA_GOSTR3411PARAMS
#define CKR_SAVED_STATE_INVALID
unsigned char CK_UTF8CHAR
#define CKR_WRAPPED_KEY_LEN_RANGE
#define CKR_KEY_TYPE_INCONSISTENT
#define CKR_UNWRAPPING_KEY_TYPE_INCONSISTENT
CK_C_GetSlotList C_GetSlotList
CK_C_GetTokenInfo C_GetTokenInfo
ck_mechanism_type_t mechanism
unsigned int allow_extract
pthread_mutex_t * keycache_lock
unsigned int allow_extract
unsigned long private_key