37 #include <libxml/tree.h>
38 #include <libxml/parser.h>
39 #include <libxml/xpath.h>
40 #include <libxml/xpathInternals.h>
41 #include <libxml/relaxng.h>
50 #define HSM_TOKEN_LABEL_LENGTH 32
57 ldns_pkcs11_rv_str(CK_RV rv)
66 return "CKR_HOST_MEMORY";
68 return "CKR_GENERAL_ERROR";
70 return "CKR_FUNCTION_FAILED";
72 return "CKR_SLOT_ID_INVALID";
74 return "CKR_ATTRIBUTE_READ_ONLY";
76 return "CKR_ATTRIBUTE_SENSITIVE";
78 return "CKR_ATTRIBUTE_TYPE_INVALID";
80 return "CKR_ATTRIBUTE_VALUE_INVALID";
82 return "CKR_DATA_INVALID";
84 return "CKR_DATA_LEN_RANGE";
86 return "CKR_DEVICE_ERROR";
88 return "CKR_DEVICE_MEMORY";
90 return "CKR_DEVICE_REMOVED";
92 return "CKR_ENCRYPTED_DATA_INVALID";
94 return "CKR_ENCRYPTED_DATA_LEN_RANGE";
96 return "CKR_FUNCTION_CANCELED";
98 return "CKR_FUNCTION_NOT_PARALLEL";
100 return "CKR_FUNCTION_NOT_SUPPORTED";
102 return "CKR_KEY_HANDLE_INVALID";
104 return "CKR_KEY_SIZE_RANGE";
106 return "CKR_KEY_TYPE_INCONSISTENT";
108 return "CKR_MECHANISM_INVALID";
110 return "CKR_MECHANISM_PARAM_INVALID";
112 return "CKR_OBJECT_HANDLE_INVALID";
114 return "CKR_OPERATION_ACTIVE";
116 return "CKR_OPERATION_NOT_INITIALIZED";
118 return "CKR_PIN_INCORRECT";
120 return "CKR_PIN_INVALID";
122 return "CKR_PIN_LEN_RANGE";
124 return "CKR_SESSION_CLOSED";
126 return "CKR_SESSION_COUNT";
128 return "CKR_SESSION_HANDLE_INVALID";
130 return "CKR_SESSION_PARALLEL_NOT_SUPPORTED";
132 return "CKR_SESSION_READ_ONLY";
134 return "CKR_SESSION_EXISTS";
136 return "CKR_SIGNATURE_INVALID";
138 return "CKR_SIGNATURE_LEN_RANGE";
140 return "CKR_TEMPLATE_INCOMPLETE";
142 return "CKR_TEMPLATE_INCONSISTENT";
144 return "CKR_TOKEN_NOT_PRESENT";
146 return "CKR_TOKEN_NOT_RECOGNIZED";
148 return "CKR_TOKEN_WRITE_PROTECTED";
150 return "CKR_UNWRAPPING_KEY_HANDLE_INVALID";
152 return "CKR_UNWRAPPING_KEY_SIZE_RANGE";
154 return "CKR_UNWRAPPING_KEY_TYPE_INCONSISTENT";
156 return "CKR_USER_ALREADY_LOGGED_IN";
158 return "CKR_USER_NOT_LOGGED_IN";
160 return "CKR_USER_PIN_NOT_INITIALIZED";
162 return "CKR_USER_TYPE_INVALID";
164 return "CKR_WRAPPED_KEY_INVALID";
166 return "CKR_WRAPPED_KEY_LEN_RANGE";
168 return "CKR_WRAPPING_KEY_HANDLE_INVALID";
170 return "CKR_WRAPPING_KEY_SIZE_RANGE";
172 return "CKR_WRAPPING_KEY_TYPE_INCONSISTENT";
174 return "CKR_RANDOM_SEED_NOT_SUPPORTED";
176 return "CKR_VENDOR_DEFINED";
178 return "CKR_BUFFER_TOO_SMALL";
180 return "CKR_SAVED_STATE_INVALID";
182 return "CKR_INFORMATION_SENSITIVE";
184 return "CKR_STATE_UNSAVEABLE";
186 return "CKR_CRYPTOKI_NOT_INITIALIZED";
188 return "CKR_CRYPTOKI_ALREADY_INITIALIZED";
190 return "CKR_MUTEX_BAD";
192 return "CKR_MUTEX_NOT_LOCKED";
194 return "Unknown error";
211 const char *message, ...)
215 if (ctx && ctx->
error == 0) {
219 va_start(args, message);
238 hsm_pkcs11_check_error(
hsm_ctx_t *ctx, CK_RV rv,
const char *action)
241 if (ctx && ctx->
error == 0) {
242 ctx->
error = (int) rv;
253 hsm_pkcs11_unload_functions(
void *handle)
257 #if defined(HAVE_LOADLIBRARY)
259 #elif defined(HAVE_DLOPEN)
260 result = dlclose(handle);
269 CK_C_GetFunctionList pGetFunctionList = NULL;
271 if (module && module->
path) {
274 #if defined(HAVE_LOADLIBRARY)
276 HINSTANCE hDLL = LoadLibrary(_T(module->
path));
284 pGetFunctionList = (CK_C_GetFunctionList)
285 GetProcAddress(hDLL, _T(
"C_GetFunctionList"));
287 #elif defined(HAVE_DLOPEN)
289 void* pDynLib = dlopen(module->
path, RTLD_NOW | RTLD_LOCAL);
291 if (pDynLib == NULL) {
297 pGetFunctionList = (CK_C_GetFunctionList) dlsym(pDynLib,
"C_GetFunctionList");
306 #ifdef HAVE_PKCS11_MODULE
307 return C_GetFunctionList(pkcs11_functions);
313 if (pGetFunctionList == NULL) {
329 if (data == NULL || len == NULL)
return;
333 while ((
unsigned short int)(*p) == 0 && l > 1) {
345 hsm_pkcs11_check_token_name(
hsm_ctx_t *ctx,
348 const char *token_name)
357 if (hsm_pkcs11_check_error(ctx, rv,
"C_GetTokenInfo")) {
363 memcpy(token_name_bytes, token_name, strlen(token_name));
368 result = memcmp(token_info.
label,
379 const char *token_name, CK_SLOT_ID *slotId)
387 if (token_name == NULL || slotId == NULL)
return HSM_ERROR;
390 if (hsm_pkcs11_check_error(ctx, rv,
"get slot list")) {
396 "No slots found in HSM");
398 }
else if (slotCount > (SIZE_MAX /
sizeof(CK_SLOT_ID))) {
400 "Too many slots found in HSM");
404 slotIds = malloc(
sizeof(CK_SLOT_ID) * slotCount);
405 if(slotIds == NULL) {
407 "Could not allocate slot ID table");
412 if (hsm_pkcs11_check_error(ctx, rv,
"get slot list")) {
416 for (cur_slot = 0; cur_slot < slotCount; cur_slot++) {
417 if (hsm_pkcs11_check_token_name(ctx,
421 *slotId = slotIds[cur_slot];
429 "could not find token with the name %s", token_name);
438 hsm_module_new(
const char *repository,
439 const char *token_label,
445 if (!repository || !path)
return NULL;
449 if (!module)
return NULL;
463 module->
name = strdup(repository);
465 module->
path = strdup(path);
476 if (module->
name) free(module->
name);
478 if (module->
path) free(module->
path);
486 hsm_session_new(
hsm_module_t *module, CK_SESSION_HANDLE session_handle)
491 session->
session = session_handle;
514 const char *repository,
const char *token_label,
515 const char *module_path,
const char *pin,
522 CK_SESSION_HANDLE session_handle;
523 int first = 1, result;
530 module = hsm_module_new(repository, token_label, module_path, config);
532 rv = hsm_pkcs11_load_functions(module);
535 "hsm_session_init()",
536 "PKCS#11 module load failed: %s", module_path);
537 hsm_module_free(module);
544 if (hsm_pkcs11_check_error(ctx, rv,
"Initialization")) {
545 hsm_module_free(module);
553 hsm_module_free(module);
561 if (hsm_pkcs11_check_error(ctx, rv,
"Open first session")) {
562 hsm_module_free(module);
567 (
unsigned char *) pin,
568 strlen((
char *)pin));
571 *session = hsm_session_new(module, session_handle);
575 if (session_handle) {
577 C_CloseSession(session_handle);
578 if (hsm_pkcs11_check_error(ctx, rv,
579 "finalize after failed login")) {
580 hsm_module_free(module);
588 if (hsm_pkcs11_check_error(ctx, rv,
"finalize after failed login")) {
589 hsm_module_free(module);
593 hsm_module_free(module);
598 "hsm_session_init()",
599 "Incorrect PIN for repository %s", repository);
613 CK_SESSION_HANDLE session_handle;
621 if (result !=
HSM_OK)
return NULL;
628 if (hsm_pkcs11_check_error(ctx, rv,
"Clone session")) {
631 new_session = hsm_session_new(session->
module, session_handle);
654 hsm_session_free(ctx->
session[i]);
677 (void) hsm_pkcs11_check_error(ctx, rv,
"Logout");
682 (void) hsm_pkcs11_check_error(ctx, rv,
"Close session");
687 (void) hsm_pkcs11_check_error(ctx, rv,
"Finalize");
690 hsm_module_free(session->
module);
693 hsm_session_free(session);
703 hsm_ctx_close(
hsm_ctx_t *ctx,
int unload)
712 hsm_session_close(ctx, ctx->
session[i], unload);
736 if (!ctx || !session)
return -1;
752 new_ctx = hsm_ctx_new();
754 new_session = hsm_session_clone(ctx, ctx->
session[i]);
758 hsm_ctx_close(new_ctx, 0);
761 hsm_ctx_add_session(new_ctx, new_session);
784 if (!key || !key->
module)
return NULL;
800 CK_KEY_TYPE key_type;
811 if (hsm_pkcs11_check_error(ctx, rv,
812 "Get attr value algorithm type")) {
820 if ((
CK_LONG)
template[0].ulValueLen < 1) {
860 if (hsm_pkcs11_check_error(ctx, rv,
861 "Get attr value algorithm type")) {
865 if ((
CK_ULONG)
template[0].ulValueLen < 1) {
875 if (hsm_pkcs11_check_error(ctx, rv,
"Could not get the size of the modulus of the private key")) {
880 modulus = (
CK_BYTE_PTR)malloc(template2[0].ulValueLen);
881 template2[0].pValue = modulus;
882 if (modulus == NULL) {
884 "Error allocating memory for modulus");
894 if (hsm_pkcs11_check_error(ctx, rv,
"Could not get the modulus of the private key")) {
900 modulus_bits = template2[0].ulValueLen * 8;
902 for (
int i = 0; modulus_bits && (modulus[i] & mask) == 0; modulus_bits--) {
936 if (hsm_pkcs11_check_error(ctx, rv,
"Could not get the size of the prime of the private key")) {
940 return template2[0].ulValueLen * 8;
951 return hsm_get_key_size_rsa(ctx, session, key);
954 return hsm_get_key_size_dsa(ctx, session, key);
965 static CK_OBJECT_HANDLE
966 hsm_find_object_handle_for_id(
hsm_ctx_t *ctx,
968 CK_OBJECT_CLASS key_class,
973 CK_OBJECT_HANDLE object;
977 {
CKA_CLASS, &key_class,
sizeof(key_class) },
983 if (hsm_pkcs11_check_error(ctx, rv,
"Find objects init")) {
991 if (hsm_pkcs11_check_error(ctx, rv,
"Find object")) {
993 hsm_pkcs11_check_error(ctx, rv,
"Find objects cleanup");
998 if (hsm_pkcs11_check_error(ctx, rv,
"Find object final")) {
1002 if (objectCount > 0) {
1014 static unsigned char *
1015 hsm_hex_parse(
const char *hex,
size_t *len)
1017 unsigned char *bytes;
1022 if (!len)
return NULL;
1025 if (!hex)
return NULL;
1026 hex_len = strlen(hex);
1027 if (hex_len % 2 != 0) {
1032 bytes = malloc(*len);
1033 for (i = 0; i < *len; i++) {
1034 bytes[i] = ldns_hexdigit_to_int(hex[2*i]) * 16 +
1035 ldns_hexdigit_to_int(hex[2*i+1]);
1045 hsm_hex_unparse(
char *dst,
const unsigned char *src,
size_t len)
1047 size_t dst_len = len*2 + 1;
1050 for (i = 0; i < len; i++) {
1051 snprintf(dst + (2*i), dst_len,
"%02x", src[i]);
1063 CK_OBJECT_HANDLE
object,
1079 if (hsm_pkcs11_check_error(ctx, rv,
"Get attr value")) {
1084 if ((
CK_LONG)
template[0].ulValueLen < 1) {
1090 template[0].pValue = malloc(
template[0].ulValueLen);
1096 if (hsm_pkcs11_check_error(ctx, rv,
"Get attr value 2")) {
1098 free(
template[0].pValue);
1102 *len =
template[0].ulValueLen;
1103 return template[0].pValue;
1111 hsm_key_new_privkey_object_handle(
hsm_ctx_t *ctx,
1113 CK_OBJECT_HANDLE
object)
1119 id = hsm_get_id_for_object(ctx, session,
object, &len);
1121 if (!
id)
return NULL;
1123 key = hsm_key_new();
1128 key->
public_key = hsm_find_object_handle_for_id(
1149 hsm_list_keys_session_internal(
hsm_ctx_t *ctx,
1159 {
CKA_CLASS, &key_class,
sizeof(key_class) },
1166 CK_OBJECT_HANDLE
object[max_object_count];
1167 CK_OBJECT_HANDLE *key_handles = NULL;
1171 if (hsm_pkcs11_check_error(ctx, rv,
"Find objects init")) {
1176 while (objectCount > 0) {
1181 if (hsm_pkcs11_check_error(ctx, rv,
"Find first object")) {
1185 hsm_pkcs11_check_error(ctx, rv,
"Find objects cleanup");
1189 total_count += objectCount;
1190 if (objectCount > 0 && store) {
1191 key_handles = realloc(key_handles, total_count *
sizeof(CK_OBJECT_HANDLE));
1192 for (i = 0; i < objectCount; i++) {
1193 key_handles[j] =
object[i];
1200 if (hsm_pkcs11_check_error(ctx, rv,
"Find objects final")) {
1207 keys = realloc(keys, total_count *
sizeof(
hsm_key_t *));
1208 for (i = 0; i < total_count; i++) {
1209 key = hsm_key_new_privkey_object_handle(ctx, session,
1217 *count = total_count;
1233 return hsm_list_keys_session_internal(ctx, session, count, 1);
1246 (void) hsm_list_keys_session_internal(ctx, session, &count, 0);
1256 const unsigned char *
id,
size_t len)
1259 CK_OBJECT_HANDLE private_key_handle;
1261 private_key_handle = hsm_find_object_handle_for_id(
1267 if (private_key_handle != 0) {
1268 key = hsm_key_new_privkey_object_handle(ctx, session,
1269 private_key_handle);
1287 const unsigned char *
id,
1294 if (!
id)
return NULL;
1297 key = hsm_find_key_by_id_session(ctx, ctx->
session[i],
id, len);
1298 if (key)
return key;
1310 hsm_find_repository_session(
hsm_ctx_t *ctx,
const char *repository)
1330 "hsm_find_repository_session()",
1331 "Can't find repository: %s", repository);
1345 unsigned long hKey = 0;
1346 unsigned char *data = NULL;
1347 size_t data_size = 0;
1355 if (!session || !session->
module) {
1370 if (hsm_pkcs11_check_error(ctx, rv,
"C_GetAttributeValue")) {
1373 public_exponent_len =
template[0].ulValueLen;
1374 modulus_len =
template[1].ulValueLen;
1376 public_exponent =
template[0].pValue = malloc(public_exponent_len);
1377 if (!public_exponent) {
1379 "Error allocating memory for public exponent");
1383 modulus =
template[1].pValue = malloc(modulus_len);
1386 "Error allocating memory for modulus");
1387 free(public_exponent);
1396 if (hsm_pkcs11_check_error(ctx, rv,
"get attribute value")) {
1397 free(
template[0].pValue);
1398 free(
template[1].pValue);
1403 hsm_remove_leading_zeroes(public_exponent, &public_exponent_len);
1404 hsm_remove_leading_zeroes(modulus, &modulus_len);
1406 data_size = public_exponent_len + modulus_len + 1;
1407 if (public_exponent_len <= 255) {
1408 data = malloc(data_size);
1411 "Error allocating memory for pub key rr data");
1412 free(public_exponent);
1416 data[0] = public_exponent_len;
1417 memcpy(&data[1], public_exponent, public_exponent_len);
1418 memcpy(&data[1 + public_exponent_len], modulus, modulus_len);
1419 }
else if (public_exponent_len <= 65535) {
1421 data = malloc(data_size);
1424 "Error allocating memory for pub key rr data");
1425 free(public_exponent);
1430 ldns_write_uint16(&data[1], (uint16_t) public_exponent_len);
1431 memcpy(&data[3], public_exponent, public_exponent_len);
1432 memcpy(&data[3 + public_exponent_len], modulus, modulus_len);
1435 "Public exponent too big");
1436 free(public_exponent);
1440 rdf = ldns_rdf_new(LDNS_RDF_TYPE_B64, data_size, data);
1441 free(public_exponent);
1460 unsigned char *data = NULL;
1461 size_t data_size = 0;
1471 if (!session || !session->
module) {
1481 if (hsm_pkcs11_check_error(ctx, rv,
"C_GetAttributeValue")) {
1484 prime_len =
template[0].ulValueLen;
1485 subprime_len =
template[1].ulValueLen;
1486 base_len =
template[2].ulValueLen;
1487 value_len =
template[3].ulValueLen;
1489 prime =
template[0].pValue = malloc(prime_len);
1492 "Error allocating memory for prime");
1496 subprime =
template[1].pValue = malloc(subprime_len);
1499 "Error allocating memory for subprime");
1504 base =
template[2].pValue = malloc(base_len);
1507 "Error allocating memory for base");
1513 value =
template[3].pValue = malloc(value_len);
1516 "Error allocating memory for value");
1528 if (hsm_pkcs11_check_error(ctx, rv,
"get attribute value")) {
1536 data_size = prime_len + subprime_len + base_len + value_len + 1;
1537 data = malloc(data_size);
1540 "Error allocating memory for pub key rr data");
1547 data[0] = (prime_len - 64) / 8;
1548 memcpy(&data[1], subprime, subprime_len);
1549 memcpy(&data[1 + subprime_len], prime, prime_len);
1550 memcpy(&data[1 + subprime_len + prime_len], base, base_len);
1551 memcpy(&data[1 + subprime_len + prime_len + base_len], value, value_len);
1553 rdf = ldns_rdf_new(LDNS_RDF_TYPE_B64, data_size, data);
1575 if (!session || !session->
module) {
1585 if (hsm_pkcs11_check_error(ctx, rv,
"C_GetAttributeValue")) {
1588 value_len =
template[0].ulValueLen;
1590 value =
template[0].pValue = malloc(value_len);
1593 "Error allocating memory for value");
1602 if (hsm_pkcs11_check_error(ctx, rv,
"get attribute value")) {
1607 rdf = ldns_rdf_new(LDNS_RDF_TYPE_B64, value_len, value);
1616 switch (hsm_get_key_algorithm(ctx, session, key)) {
1618 return hsm_get_key_rdata_rsa(ctx, session, key);
1621 return hsm_get_key_rdata_dsa(ctx, session, key);
1624 return hsm_get_key_rdata_gost(ctx, session, key);
1636 hsm_create_prefix(
CK_ULONG digest_len,
1637 ldns_algorithm algorithm,
1641 const CK_BYTE RSA_MD5_ID[] = { 0x30, 0x20, 0x30, 0x0C, 0x06, 0x08, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x02, 0x05, 0x05, 0x00, 0x04, 0x10 };
1642 const CK_BYTE RSA_SHA1_ID[] = { 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2B, 0x0E, 0x03, 0x02, 0x1A, 0x05, 0x00, 0x04, 0x14 };
1643 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 };
1644 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 };
1647 case LDNS_SIGN_RSAMD5:
1648 *data_size =
sizeof(RSA_MD5_ID) + digest_len;
1649 data = malloc(*data_size);
1650 memcpy(data, RSA_MD5_ID,
sizeof(RSA_MD5_ID));
1652 case LDNS_SIGN_RSASHA1:
1653 case LDNS_SIGN_RSASHA1_NSEC3:
1654 *data_size =
sizeof(RSA_SHA1_ID) + digest_len;
1655 data = malloc(*data_size);
1656 memcpy(data, RSA_SHA1_ID,
sizeof(RSA_SHA1_ID));
1658 case LDNS_SIGN_RSASHA256:
1659 *data_size =
sizeof(RSA_SHA256_ID) + digest_len;
1660 data = malloc(*data_size);
1661 memcpy(data, RSA_SHA256_ID,
sizeof(RSA_SHA256_ID));
1663 case LDNS_SIGN_RSASHA512:
1664 *data_size =
sizeof(RSA_SHA512_ID) + digest_len;
1665 data = malloc(*data_size);
1666 memcpy(data, RSA_SHA512_ID,
sizeof(RSA_SHA512_ID));
1669 case LDNS_SIGN_DSA_NSEC3:
1670 case LDNS_SIGN_ECC_GOST:
1671 #if LDNS_BUILD_CONFIG_USE_ECDSA
1672 case LDNS_SIGN_ECDSAP256SHA256:
1673 case LDNS_SIGN_ECDSAP384SHA384:
1675 *data_size = digest_len;
1676 data = malloc(*data_size);
1687 CK_MECHANISM_TYPE mechanism_type,
1689 ldns_buffer *sign_buf)
1695 digest_mechanism.pParameter = NULL;
1696 digest_mechanism.ulParameterLen = 0;
1697 digest_mechanism.
mechanism = mechanism_type;
1698 digest = malloc(digest_len);
1701 if (hsm_pkcs11_check_error(ctx, rv,
"HSM digest init")) {
1707 ldns_buffer_begin(sign_buf),
1708 ldns_buffer_position(sign_buf),
1711 if (hsm_pkcs11_check_error(ctx, rv,
"HSM digest")) {
1720 ldns_buffer *sign_buf,
1722 ldns_algorithm algorithm)
1738 session = hsm_find_key_session(ctx, key);
1739 if (!session)
return NULL;
1744 switch (algorithm) {
1745 case LDNS_SIGN_RSAMD5:
1747 digest = hsm_digest_through_hsm(ctx, session,
1751 case LDNS_SIGN_RSASHA1:
1752 case LDNS_SIGN_RSASHA1_NSEC3:
1754 case LDNS_SIGN_DSA_NSEC3:
1755 digest_len = LDNS_SHA1_DIGEST_LENGTH;
1756 digest = malloc(digest_len);
1757 digest = ldns_sha1(ldns_buffer_begin(sign_buf),
1758 ldns_buffer_position(sign_buf),
1762 case LDNS_SIGN_RSASHA256:
1763 #if LDNS_BUILD_CONFIG_USE_ECDSA
1764 case LDNS_SIGN_ECDSAP256SHA256:
1766 digest_len = LDNS_SHA256_DIGEST_LENGTH;
1767 digest = malloc(digest_len);
1768 digest = ldns_sha256(ldns_buffer_begin(sign_buf),
1769 ldns_buffer_position(sign_buf),
1772 #if LDNS_BUILD_CONFIG_USE_ECDSA
1773 case LDNS_SIGN_ECDSAP384SHA384:
1774 digest_len = LDNS_SHA384_DIGEST_LENGTH;
1775 digest = malloc(digest_len);
1776 digest = ldns_sha384(ldns_buffer_begin(sign_buf),
1777 ldns_buffer_position(sign_buf),
1781 case LDNS_SIGN_RSASHA512:
1782 digest_len = LDNS_SHA512_DIGEST_LENGTH;
1783 digest = malloc(digest_len);
1784 digest = ldns_sha512(ldns_buffer_begin(sign_buf),
1785 ldns_buffer_position(sign_buf),
1788 case LDNS_SIGN_ECC_GOST:
1790 digest = hsm_digest_through_hsm(ctx, session,
1807 data = hsm_create_prefix(digest_len, algorithm, &data_len);
1808 memcpy(data + data_len - digest_len, digest, digest_len);
1810 sign_mechanism.pParameter = NULL;
1811 sign_mechanism.ulParameterLen = 0;
1813 case LDNS_SIGN_RSAMD5:
1814 case LDNS_SIGN_RSASHA1:
1815 case LDNS_SIGN_RSASHA1_NSEC3:
1816 case LDNS_SIGN_RSASHA256:
1817 case LDNS_SIGN_RSASHA512:
1821 case LDNS_SIGN_DSA_NSEC3:
1824 case LDNS_SIGN_ECC_GOST:
1827 #if LDNS_BUILD_CONFIG_USE_ECDSA
1829 case LDNS_SIGN_ECDSAP256SHA256:
1830 case LDNS_SIGN_ECDSAP384SHA384:
1844 if (hsm_pkcs11_check_error(ctx, rv,
"sign init")) {
1853 if (hsm_pkcs11_check_error(ctx, rv,
"sign final")) {
1859 sig_rdf = ldns_rdf_new_frm_data(LDNS_RDF_TYPE_B64,
1871 hsm_dname_is_wildcard(
const ldns_rdf* dname)
1873 return ( ldns_dname_label_count(dname) > 0 &&
1874 ldns_rdf_data(dname)[0] == 1 &&
1875 ldns_rdf_data(dname)[1] ==
'*');
1879 hsm_create_empty_rrsig(
const ldns_rr_list *rrset,
1884 uint32_t orig_class;
1886 uint8_t label_count;
1888 label_count = ldns_dname_label_count(
1889 ldns_rr_owner(ldns_rr_list_rr(rrset, 0)));
1891 if (hsm_dname_is_wildcard(ldns_rr_owner(ldns_rr_list_rr(rrset, 0)))) {
1895 rrsig = ldns_rr_new_frm_type(LDNS_RR_TYPE_RRSIG);
1898 orig_ttl = ldns_rr_ttl(ldns_rr_list_rr(rrset, 0));
1899 orig_class = ldns_rr_get_class(ldns_rr_list_rr(rrset, 0));
1901 ldns_rr_set_class(rrsig, orig_class);
1902 ldns_rr_set_ttl(rrsig, orig_ttl);
1903 ldns_rr_set_owner(rrsig,
1906 ldns_rr_list_rr(rrset,
1912 (void)ldns_rr_rrsig_set_origttl(
1914 ldns_native2rdf_int32(LDNS_RDF_TYPE_INT32,
1917 (void)ldns_rr_rrsig_set_signame(
1919 ldns_rdf_clone(sign_params->
owner));
1921 (void)ldns_rr_rrsig_set_labels(
1923 ldns_native2rdf_int8(LDNS_RDF_TYPE_INT8,
1928 (void)ldns_rr_rrsig_set_inception(
1930 ldns_native2rdf_int32(
1934 (void)ldns_rr_rrsig_set_inception(
1936 ldns_native2rdf_int32(LDNS_RDF_TYPE_TIME, now));
1939 (void)ldns_rr_rrsig_set_expiration(
1941 ldns_native2rdf_int32(
1945 (void)ldns_rr_rrsig_set_expiration(
1947 ldns_native2rdf_int32(
1949 now + LDNS_DEFAULT_EXP_TIME));
1952 (void)ldns_rr_rrsig_set_keytag(
1954 ldns_native2rdf_int16(LDNS_RDF_TYPE_INT16,
1957 (void)ldns_rr_rrsig_set_algorithm(
1959 ldns_native2rdf_int8(
1963 (void)ldns_rr_rrsig_set_typecovered(
1965 ldns_native2rdf_int16(
1967 ldns_rr_get_type(ldns_rr_list_rr(rrset,
1980 char *(pin_callback)(
unsigned int,
const char *,
unsigned int))
1983 xmlXPathContextPtr xpath_ctx;
1984 xmlXPathObjectPtr xpath_obj;
1997 int repositories = 0;
2001 _hsm_ctx = hsm_ctx_new();
2004 config_file = strdup(config);
2006 config_file = strdup(HSM_DEFAULT_CONFIG);
2010 doc = xmlParseFile(config_file);
2017 xpath_ctx = xmlXPathNewContext(doc);
2018 if(xpath_ctx == NULL) {
2020 hsm_ctx_free(_hsm_ctx);
2026 xexpr = (xmlChar *)
"//Configuration/RepositoryList/Repository";
2027 xpath_obj = xmlXPathEvalExpression(xexpr, xpath_ctx);
2028 if(xpath_obj == NULL) {
2029 xmlXPathFreeContext(xpath_ctx);
2031 hsm_ctx_free(_hsm_ctx);
2036 if (xpath_obj->nodesetval) {
2037 for (i = 0; i < xpath_obj->nodesetval->nodeNr; i++) {
2042 hsm_config_default(&module_config);
2044 curNode = xpath_obj->nodesetval->nodeTab[i]->xmlChildrenNode;
2045 repository = (
char *) xmlGetProp(xpath_obj->nodesetval->nodeTab[i],
2046 (
const xmlChar *)
"name");
2049 if (xmlStrEqual(curNode->name, (
const xmlChar *)
"TokenLabel"))
2050 token_label = (
char *) xmlNodeGetContent(curNode);
2051 if (xmlStrEqual(curNode->name, (
const xmlChar *)
"Module"))
2052 module_path = (
char *) xmlNodeGetContent(curNode);
2053 if (xmlStrEqual(curNode->name, (
const xmlChar *)
"PIN"))
2054 module_pin = (
char *) xmlNodeGetContent(curNode);
2055 if (xmlStrEqual(curNode->name, (
const xmlChar *)
"SkipPublicKey"))
2057 curNode = curNode->next;
2060 if (repository && token_label && module_path) {
2084 if (module_pin == NULL)
break;
2096 memset(module_pin, 0, strlen(module_pin));
2102 "No pin or callback function");
2119 xmlXPathFreeObject(xpath_obj);
2120 xmlXPathFreeContext(xpath_ctx);
2123 if (result ==
HSM_OK && repositories == 0) {
2125 "No repositories found");
2135 hsm_ctx_close(_hsm_ctx, 1);
2142 return hsm_ctx_clone(_hsm_ctx);
2152 CK_SESSION_HANDLE session_handle;
2160 if (session == NULL)
continue;
2166 if (hsm_pkcs11_check_error(ctx, rv,
"get session info")) {
2173 "Session not logged in");
2183 if (hsm_pkcs11_check_error(ctx, rv,
"test open session")) {
2187 if (hsm_pkcs11_check_error(ctx, rv,
"test close session")) {
2198 hsm_ctx_close(ctx, 0);
2212 params->
algorithm = LDNS_SIGN_RSASHA256;
2213 params->
flags = LDNS_KEY_ZONE_KEY;
2217 params->
owner = NULL;
2225 if (params->
owner) ldns_rdf_deep_free(params->
owner);
2234 size_t key_count = 0;
2235 size_t cur_key_count;
2246 keys = realloc(keys,
2247 (key_count + cur_key_count) *
sizeof(
hsm_key_t *));
2248 for (j = 0; j < cur_key_count; j++) {
2249 keys[key_count + j] = session_keys[j];
2251 key_count += cur_key_count;
2263 const char *repository)
2267 if (!repository)
return NULL;
2270 session = hsm_find_repository_session(ctx, repository);
2293 const char *repository)
2297 if (!repository)
return 0;
2300 session = hsm_find_repository_session(ctx, repository);
2310 unsigned char *id_bytes;
2314 id_bytes = hsm_hex_parse(
id, &len);
2316 if (!id_bytes)
return NULL;
2318 key = hsm_find_key_by_id_bin(ctx, id_bytes, len);
2325 const char *repository,
2326 unsigned long keysize)
2331 unsigned char id[16];
2335 CK_OBJECT_HANDLE publicKey, privateKey;
2336 CK_KEY_TYPE keyType =
CKK_RSA;
2340 CK_BYTE publicExponent[] = { 1, 0, 1 };
2346 session = hsm_find_repository_session(ctx, repository);
2347 if (!session)
return NULL;
2352 }
while (hsm_find_key_by_id_bin(ctx,
id, 16));
2355 hsm_hex_unparse(id_str,
id, 16);
2367 {
CKA_WRAP, &cfalse,
sizeof(cfalse) },
2377 {
CKA_SIGN, &ctrue,
sizeof (ctrue) },
2388 publicKeyTemplate, 9,
2389 privateKeyTemplate, 10,
2392 if (hsm_pkcs11_check_error(ctx, rv,
"generate key pair")) {
2396 new_key = hsm_key_new();
2414 const char *repository,
2415 unsigned long keysize)
2420 CK_OBJECT_HANDLE domainPar, publicKey, privateKey;
2425 unsigned char id[16];
2429 CK_KEY_TYPE keyType =
CKK_DSA;
2449 {
CKA_BASE, dsa_g,
sizeof(dsa_g) },
2455 {
CKA_WRAP, &cfalse,
sizeof(cfalse) },
2463 {
CKA_SIGN, &ctrue,
sizeof(ctrue) },
2473 session = hsm_find_repository_session(ctx, repository);
2474 if (!session)
return NULL;
2480 }
while (hsm_find_key_by_id_bin(ctx,
id, 16));
2483 hsm_hex_unparse(id_str,
id, 16);
2491 if (hsm_pkcs11_check_error(ctx, rv,
"generate domain parameters")) {
2496 domainPar, publicKeyTemplate, 3);
2497 if (hsm_pkcs11_check_error(ctx, rv,
"get domain parameters")) {
2502 if (hsm_pkcs11_check_error(ctx, rv,
"destroy domain parameters")) {
2510 publicKeyTemplate, 10,
2511 privateKeyTemplate, 10,
2514 if (hsm_pkcs11_check_error(ctx, rv,
"generate key pair")) {
2518 new_key = hsm_key_new();
2528 const char *repository)
2533 CK_OBJECT_HANDLE publicKey, privateKey;
2538 unsigned char id[16];
2547 CK_BYTE oid1[] = { 0x06, 0x07, 0x2A, 0x85, 0x03, 0x02, 0x02, 0x23, 0x01 };
2548 CK_BYTE oid2[] = { 0x06, 0x07, 0x2A, 0x85, 0x03, 0x02, 0x02, 0x1E, 0x01 };
2558 {
CKA_WRAP, &cfalse,
sizeof(cfalse) },
2566 {
CKA_SIGN, &ctrue,
sizeof(ctrue) },
2576 session = hsm_find_repository_session(ctx, repository);
2577 if (!session)
return NULL;
2583 }
while (hsm_find_key_by_id_bin(ctx,
id, 16));
2586 hsm_hex_unparse(id_str,
id, 16);
2592 publicKeyTemplate, 9,
2593 privateKeyTemplate, 10,
2596 if (hsm_pkcs11_check_error(ctx, rv,
"generate key pair")) {
2600 new_key = hsm_key_new();
2614 if (!key)
return -1;
2616 session = hsm_find_key_session(ctx, key);
2617 if (!session)
return -2;
2621 if (hsm_pkcs11_check_error(ctx, rv,
"Destroy private key")) {
2629 if (hsm_pkcs11_check_error(ctx, rv,
"Destroy public key")) {
2650 for (i = 0; i < count; i++) {
2665 if (!key)
return NULL;
2667 session = hsm_find_key_session(ctx, key);
2668 if (!session)
return NULL;
2670 id = hsm_get_id_for_object(ctx, session, key->
private_key, &len);
2671 if (!
id)
return NULL;
2674 id_str = malloc(len * 2 + 1);
2680 hsm_hex_unparse(id_str,
id, len);
2695 session = hsm_find_key_session(ctx, key);
2696 if (!session)
return NULL;
2701 if (key_info->
id == NULL) {
2702 key_info->
id = strdup(
"");
2705 key_info->
algorithm = (
unsigned long) hsm_get_key_algorithm(ctx,
2708 key_info->
keysize = (
unsigned long) hsm_get_key_size(ctx,
2750 const ldns_rr_list* rrset,
2755 ldns_buffer *sign_buf;
2759 if (!key)
return NULL;
2760 if (!sign_params)
return NULL;
2763 signature = hsm_create_empty_rrsig((ldns_rr_list *)rrset,
2769 sign_buf = ldns_buffer_new(LDNS_MAX_PACKETLEN);
2771 if (ldns_rrsig2buffer_wire(sign_buf, signature)
2772 != LDNS_STATUS_OK) {
2773 ldns_buffer_free(sign_buf);
2775 ldns_rr_free(signature);
2780 for(i = 0; i < ldns_rr_list_rr_count(rrset); i++) {
2781 ldns_rr2canonical(ldns_rr_list_rr(rrset, i));
2785 if (ldns_rr_list2buffer_wire(sign_buf, rrset)
2786 != LDNS_STATUS_OK) {
2787 ldns_buffer_free(sign_buf);
2788 ldns_rr_free(signature);
2792 b64_rdf = hsm_sign_buffer(ctx, sign_buf, key, sign_params->
algorithm);
2794 ldns_buffer_free(sign_buf);
2797 ldns_rr_free(signature);
2801 ldns_rr_rrsig_set_sig(signature, b64_rdf);
2818 size_t *digest_length)
2826 if (hsm_pkcs11_check_error(ctx, rv,
"digest init")) {
2837 if (hsm_pkcs11_check_error(ctx, rv,
"digest to determine result size")) {
2847 if (hsm_pkcs11_check_error(ctx, rv,
"digest")) {
2861 uint16_t iterations,
2862 uint8_t salt_length,
2865 char *orig_owner_str;
2866 size_t hashed_owner_str_len;
2867 ldns_rdf *hashed_owner;
2868 char *hashed_owner_str;
2869 char *hashed_owner_b32;
2870 int hashed_owner_b32_len;
2873 size_t hash_length = 0;
2883 mechanism.pParameter = NULL;
2884 mechanism.ulParameterLen = 0;
2887 printf(
"unknown algo: %u\n", (
unsigned int)algorithm);
2902 orig_owner_str = ldns_rdf2str(name);
2904 hashed_owner_str_len = salt_length + ldns_rdf_size(name);
2905 hashed_owner_str = LDNS_XMALLOC(
char, hashed_owner_str_len);
2906 memcpy(hashed_owner_str, ldns_rdf_data(name), ldns_rdf_size(name));
2907 memcpy(hashed_owner_str + ldns_rdf_size(name), salt, salt_length);
2909 for (cur_it = iterations + 1; cur_it > 0; cur_it--) {
2910 if (hash != NULL) free(hash);
2911 hash = (
char *) hsm_digest(ctx,
2915 hashed_owner_str_len,
2918 LDNS_FREE(hashed_owner_str);
2919 hashed_owner_str_len = salt_length + hash_length;
2920 hashed_owner_str = LDNS_XMALLOC(
char, hashed_owner_str_len);
2921 if (!hashed_owner_str) {
2926 memcpy(hashed_owner_str, hash, hash_length);
2927 memcpy(hashed_owner_str + hash_length, salt, salt_length);
2930 LDNS_FREE(hashed_owner_str);
2931 hashed_owner_str = hash;
2932 hashed_owner_str_len = hash_length;
2933 hashed_owner_b32 = LDNS_XMALLOC(
char,
2934 ldns_b32_ntop_calculate_size(
2935 hashed_owner_str_len) + 1);
2936 LDNS_FREE(orig_owner_str);
2937 hashed_owner_b32_len =
2938 (size_t) ldns_b32_ntop_extended_hex((uint8_t *) hashed_owner_str,
2939 hashed_owner_str_len,
2941 ldns_b32_ntop_calculate_size(
2942 hashed_owner_str_len));
2943 if (hashed_owner_b32_len < 1) {
2944 error_name = ldns_rdf2str(name);
2946 "Error in base32 extended hex encoding "
2947 "of hashed owner name (name: %s, return code: %d)",
2948 error_name, hashed_owner_b32_len);
2949 LDNS_FREE(error_name);
2950 LDNS_FREE(hashed_owner_b32);
2953 hashed_owner_str_len = hashed_owner_b32_len;
2954 hashed_owner_b32[hashed_owner_b32_len] =
'\0';
2956 status = ldns_str2rdf_dname(&hashed_owner, hashed_owner_b32);
2957 if (status != LDNS_STATUS_OK) {
2959 "Error creating rdf from %s", hashed_owner_b32);
2960 LDNS_FREE(hashed_owner_b32);
2965 LDNS_FREE(hashed_owner_b32);
2966 return hashed_owner;
2988 session = hsm_find_key_session(ctx, key);
2989 if (!session)
return NULL;
2991 dnskey = ldns_rr_new();
2992 ldns_rr_set_type(dnskey, LDNS_RR_TYPE_DNSKEY);
2994 ldns_rr_set_owner(dnskey, ldns_rdf_clone(sign_params->
owner));
2996 ldns_rr_push_rdf(dnskey,
2997 ldns_native2rdf_int16(LDNS_RDF_TYPE_INT16,
2998 sign_params->
flags));
2999 ldns_rr_push_rdf(dnskey,
3000 ldns_native2rdf_int8(LDNS_RDF_TYPE_INT8,
3001 LDNS_DNSSEC_KEYPROTO));
3002 ldns_rr_push_rdf(dnskey,
3003 ldns_native2rdf_int8(LDNS_RDF_TYPE_ALG,
3006 rdata = hsm_get_key_rdata(ctx, session, key);
3007 if (rdata == NULL) {
3008 ldns_rr_free(dnskey);
3011 ldns_rr_push_rdf(dnskey, rdata);
3018 unsigned char *buffer,
3019 unsigned long length)
3024 if (!buffer)
return -1;
3049 unsigned char rnd_buf[4];
3052 memcpy(&rnd, rnd_buf, 4);
3064 unsigned char rnd_buf[8];
3067 memcpy(&rnd, rnd_buf, 8);
3080 const char *token_label,
3088 result = hsm_session_init(_hsm_ctx,
3096 return hsm_ctx_add_session(_hsm_ctx, session);
3110 hsm_session_close(_hsm_ctx, _hsm_ctx->
session[i], 1);
3139 "hsm_token_attached()",
3140 "Can't find repository: %s", repository);
3148 case LDNS_SIGN_RSAMD5:
3149 case LDNS_SIGN_RSASHA1:
3150 case LDNS_SIGN_RSASHA1_NSEC3:
3151 case LDNS_SIGN_RSASHA256:
3152 case LDNS_SIGN_RSASHA512:
3154 case LDNS_SIGN_DSA_NSEC3:
3155 case LDNS_SIGN_ECC_GOST:
3158 #if LDNS_BUILD_CONFIG_USE_ECDSA
3159 case LDNS_SIGN_ECDSAP256SHA256:
3160 case LDNS_SIGN_ECDSAP384SHA384:
3184 if (message == NULL) {
3185 return strdup(
"libhsm memory allocation failed");
3201 printf(
"\t\tmodule at %p (sym %p)\n", (
void *) session->
module, (
void *) session->
module->
sym);
3202 printf(
"\t\tmodule path: %s\n", session->
module->
path);
3203 printf(
"\t\trepository name: %s\n", session->
module->
name);
3205 printf(
"\t\tsess handle: %u\n", (
unsigned int) session->
session);
3217 printf(
"CTX Sessions: %lu\n",
3220 printf(
"\tSession at %p\n", (
void *) ctx->
session[i]);
3232 printf(
"\tmodule: %p\n", (
void *) key->
module);
3233 printf(
"\tprivkey handle: %u\n", (
unsigned int) key->
private_key);
3235 printf(
"\tpubkey handle: %u\n", (
unsigned int) key->
public_key);
3237 printf(
"\tpubkey handle: %s\n",
"NULL");
3239 printf(
"\trepository: %s\n", key->
module->
name);
3241 printf(
"\tsize: %lu\n", key_info->
keysize);
3242 printf(
"\tid: %s\n", key_info->
id);
3245 printf(
"key: hsm_get_key_info() returned NULL\n");
3248 printf(
"key: <void>\n");
3260 fprintf(stderr,
"%s\n", message);
3263 fprintf(stderr,
"Unknown error\n");
3291 if (result !=
HSM_OK)
return;
3294 if (hsm_pkcs11_check_error(ctx, rv,
"C_GetTokenInfo")) {
3298 printf(
"Repository: %s\n",session->
module->
name);
3300 printf(
"\tModule: %s\n", session->
module->
path);
3301 printf(
"\tSlot: %lu\n", slot_id);
3302 printf(
"\tToken Label: %.*s\n",
3303 (
int)
sizeof(token_info.
label), token_info.
label);
3304 printf(
"\tManufacturer: %.*s\n",
3305 (
int)
sizeof(token_info.manufacturerID), token_info.manufacturerID);
3306 printf(
"\tModel: %.*s\n",
3307 (
int)
sizeof(token_info.
model), token_info.
model);
3308 printf(
"\tSerial: %.*s\n",
3309 (
int)
sizeof(token_info.serialNumber), token_info.serialNumber);
char * hsm_get_key_id(hsm_ctx_t *ctx, const hsm_key_t *key)
#define CKR_SIGNATURE_INVALID
ldns_rdf * hsm_nsec3_hash_name(hsm_ctx_t *ctx, ldns_rdf *name, uint8_t algorithm, uint16_t iterations, uint8_t salt_length, uint8_t *salt)
#define CKR_SAVED_STATE_INVALID
#define CKR_SESSION_EXISTS
#define CKR_RANDOM_SEED_NOT_SUPPORTED
#define CKR_SESSION_COUNT
int hsm_attach(const char *repository, const char *token_label, const char *path, const char *pin, const hsm_config_t *config)
#define CKR_KEY_SIZE_RANGE
#define CKF_OS_LOCKING_OK
#define CKR_KEY_TYPE_INCONSISTENT
void hsm_key_free(hsm_key_t *key)
void hsm_print_session(hsm_session_t *session)
#define CKF_SERIAL_SESSION
CK_C_GetSlotList C_GetSlotList
#define CKR_SESSION_PARALLEL_NOT_SUPPORTED
#define CKM_GOSTR3410_KEY_PAIR_GEN
#define CKR_STATE_UNSAVEABLE
#define CKR_MUTEX_NOT_LOCKED
char * hsm_get_error(hsm_ctx_t *gctx)
const char * error_action
hsm_key_t * hsm_find_key_by_id(hsm_ctx_t *ctx, const char *id)
int hsm_detach(const char *repository)
#define HSM_MAX_SIGNATURE_LENGTH
hsm_key_info_t * hsm_get_key_info(hsm_ctx_t *ctx, const hsm_key_t *key)
void hsm_sign_params_free(hsm_sign_params_t *params)
#define CKR_SESSION_READ_ONLY
#define CKA_GOSTR3410PARAMS
#define CKR_WRAPPING_KEY_HANDLE_INVALID
#define CKR_USER_NOT_LOGGED_IN
unsigned long private_key
uint32_t hsm_random32(hsm_ctx_t *ctx)
#define CKR_ATTRIBUTE_SENSITIVE
void hsm_print_ctx(hsm_ctx_t *gctx)
#define CKR_TEMPLATE_INCOMPLETE
void hsm_ctx_set_error(hsm_ctx_t *ctx, int error, const char *action, const char *message,...)
#define CKM_DSA_PARAMETER_GEN
#define CKR_PIN_LEN_RANGE
#define CKR_INFORMATION_SENSITIVE
#define CKR_GENERAL_ERROR
unsigned long int CK_ULONG
void hsm_key_list_free(hsm_key_t **key_list, size_t count)
#define CKA_PUBLIC_EXPONENT
hsm_key_t * hsm_generate_dsa_key(hsm_ctx_t *ctx, const char *repository, unsigned long keysize)
ldns_rr * hsm_get_dnskey(hsm_ctx_t *ctx, const hsm_key_t *key, const hsm_sign_params_t *sign_params)
#define HSM_TOKEN_LABEL_LENGTH
void hsm_destroy_context(hsm_ctx_t *ctx)
ck_mechanism_type_t mechanism
#define CKR_PIN_INCORRECT
#define CKR_FUNCTION_FAILED
#define CKR_VENDOR_DEFINED
#define CKR_OPERATION_ACTIVE
#define CKR_SLOT_ID_INVALID
#define CKA_GOSTR3411PARAMS
#define CKR_UNWRAPPING_KEY_HANDLE_INVALID
int hsm_open(const char *config, char *(pin_callback)(unsigned int, const char *, unsigned int))
#define CKR_ENCRYPTED_DATA_LEN_RANGE
size_t hsm_count_keys_session(hsm_ctx_t *ctx, const hsm_session_t *session)
hsm_key_t ** hsm_list_keys(hsm_ctx_t *ctx, size_t *count)
#define HSM_MODULE_NOT_FOUND
#define CKR_MECHANISM_INVALID
void hsm_key_info_free(hsm_key_info_t *key_info)
void hsm_print_key(hsm_key_t *key)
#define CKR_FUNCTION_CANCELED
int hsm_token_attached(hsm_ctx_t *ctx, const char *repository)
#define CKR_WRAPPED_KEY_LEN_RANGE
#define CKR_KEY_HANDLE_INVALID
#define HSM_ERROR_MSGSIZE
CK_C_GetTokenInfo C_GetTokenInfo
#define CKR_CRYPTOKI_ALREADY_INITIALIZED
#define CKM_DSA_KEY_PAIR_GEN
#define HSM_REPOSITORY_NOT_FOUND
#define HSM_NO_REPOSITORIES
hsm_sign_params_t * hsm_sign_params_new()
#define CKM_RSA_PKCS_KEY_PAIR_GEN
#define CKR_ATTRIBUTE_VALUE_INVALID
#define CKR_FUNCTION_NOT_PARALLEL
#define CKR_SIGNATURE_LEN_RANGE
#define HSM_CONFIG_FILE_ERROR
#define CKR_CRYPTOKI_NOT_INITIALIZED
#define CKR_ENCRYPTED_DATA_INVALID
#define HSM_PIN_INCORRECT
hsm_key_t * hsm_generate_gost_key(hsm_ctx_t *ctx, const char *repository)
#define CKR_ATTRIBUTE_TYPE_INVALID
#define CKR_UNWRAPPING_KEY_SIZE_RANGE
#define CKR_USER_PIN_NOT_INITIALIZED
char error_message[HSM_ERROR_MSGSIZE]
#define CKR_WRAPPING_KEY_TYPE_INCONSISTENT
#define CKR_TEMPLATE_INCONSISTENT
#define CKR_ATTRIBUTE_READ_ONLY
#define CKR_TOKEN_NOT_RECOGNIZED
#define CKR_WRAPPING_KEY_SIZE_RANGE
#define CKR_TOKEN_WRITE_PROTECTED
#define CKR_OBJECT_HANDLE_INVALID
hsm_session_t * session[HSM_MAX_SESSIONS]
unsigned char CK_UTF8CHAR
const hsm_module_t * module
int hsm_supported_algorithm(ldns_algorithm algorithm)
uint64_t hsm_random64(hsm_ctx_t *ctx)
#define CKS_RW_USER_FUNCTIONS
#define CKR_OPERATION_NOT_INITIALIZED
#define CKR_BUFFER_TOO_SMALL
int hsm_remove_key(hsm_ctx_t *ctx, hsm_key_t *key)
hsm_ctx_t * hsm_create_context()
int hsm_random_buffer(hsm_ctx_t *ctx, unsigned char *buffer, unsigned long length)
#define CKR_SESSION_CLOSED
#define CKR_DEVICE_MEMORY
hsm_key_t ** hsm_list_keys_repository(hsm_ctx_t *ctx, size_t *count, const char *repository)
struct ck_function_list * CK_FUNCTION_LIST_PTR
#define CKR_UNWRAPPING_KEY_TYPE_INCONSISTENT
#define CKR_FUNCTION_NOT_SUPPORTED
hsm_key_t ** hsm_list_keys_session(hsm_ctx_t *ctx, const hsm_session_t *session, size_t *count)
#define CKR_USER_TYPE_INVALID
#define CKR_DEVICE_REMOVED
hsm_key_t * hsm_generate_rsa_key(hsm_ctx_t *ctx, const char *repository, unsigned long keysize)
#define CKR_TOKEN_NOT_PRESENT
#define CKR_WRAPPED_KEY_INVALID
#define CKR_USER_ALREADY_LOGGED_IN
int hsm_check_context(hsm_ctx_t *ctx)
int hsm_get_slot_id(hsm_ctx_t *ctx, CK_FUNCTION_LIST_PTR pkcs11_functions, const char *token_name, CK_SLOT_ID *slotId)
void hsm_print_error(hsm_ctx_t *gctx)
#define CKR_MECHANISM_PARAM_INVALID
void hsm_print_tokeninfo(hsm_ctx_t *gctx)
size_t hsm_count_keys(hsm_ctx_t *ctx)
size_t hsm_count_keys_repository(hsm_ctx_t *ctx, const char *repository)
#define CKR_DATA_LEN_RANGE
#define CKR_SESSION_HANDLE_INVALID
ldns_rr * hsm_sign_rrset(hsm_ctx_t *ctx, const ldns_rr_list *rrset, const hsm_key_t *key, const hsm_sign_params_t *sign_params)