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)
256 #if defined(HAVE_LOADLIBRARY) 258 #elif defined(HAVE_DLOPEN) 259 (void) dlclose(handle);
268 CK_C_GetFunctionList pGetFunctionList = NULL;
270 if (module && module->
path) {
273 #if defined(HAVE_LOADLIBRARY) 275 HINSTANCE hDLL = LoadLibrary(_T(module->
path));
283 pGetFunctionList = (CK_C_GetFunctionList)
284 GetProcAddress(hDLL, _T(
"C_GetFunctionList"));
286 #elif defined(HAVE_DLOPEN) 288 void* pDynLib = dlopen(module->
path, RTLD_NOW | RTLD_LOCAL);
290 if (pDynLib == NULL) {
296 pGetFunctionList = (CK_C_GetFunctionList) dlsym(pDynLib,
"C_GetFunctionList");
305 #ifdef HAVE_PKCS11_MODULE 306 return C_GetFunctionList(pkcs11_functions);
312 if (pGetFunctionList == NULL) {
328 if (data == NULL || len == NULL)
return;
332 while ((
unsigned short int)(*p) == 0 && l > 1) {
344 hsm_pkcs11_check_token_name(
hsm_ctx_t *ctx,
347 const char *token_name)
356 if (hsm_pkcs11_check_error(ctx, rv,
"C_GetTokenInfo")) {
362 memcpy(token_name_bytes, token_name, strlen(token_name));
367 result = memcmp(token_info.
label,
378 const char *token_name, CK_SLOT_ID *slotId)
386 if (token_name == NULL || slotId == NULL)
return HSM_ERROR;
389 if (hsm_pkcs11_check_error(ctx, rv,
"get slot list")) {
395 "No slots found in HSM");
397 }
else if (slotCount > (SIZE_MAX /
sizeof(CK_SLOT_ID))) {
399 "Too many slots found in HSM");
403 slotIds = malloc(
sizeof(CK_SLOT_ID) * slotCount);
404 if(slotIds == NULL) {
406 "Could not allocate slot ID table");
411 if (hsm_pkcs11_check_error(ctx, rv,
"get slot list")) {
415 for (cur_slot = 0; cur_slot < slotCount; cur_slot++) {
416 if (hsm_pkcs11_check_token_name(ctx,
420 *slotId = slotIds[cur_slot];
428 "could not find token with the name %s", token_name);
437 hsm_module_new(
const char *repository,
438 const char *token_label,
444 if (!repository || !path)
return NULL;
448 if (!module)
return NULL;
462 module->
name = strdup(repository);
464 module->
path = strdup(path);
475 if (module->
name) free(module->
name);
477 if (module->
path) free(module->
path);
485 hsm_session_new(
hsm_module_t *module, CK_SESSION_HANDLE session_handle)
490 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 (void)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, *key_handles_prev;
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 (void)hsm_pkcs11_check_error(ctx, rv,
"Find objects cleanup");
1189 total_count += objectCount;
1190 if (objectCount > 0 && store) {
1191 key_handles_prev = key_handles;
1192 if (!(key_handles = realloc(key_handles_prev, total_count *
sizeof(CK_OBJECT_HANDLE)))) {
1193 free(key_handles_prev);
1197 for (i = 0; i < objectCount; i++) {
1198 key_handles[j] =
object[i];
1205 if (hsm_pkcs11_check_error(ctx, rv,
"Find objects final")) {
1213 if (!(keys = realloc(keys_prev, total_count *
sizeof(
hsm_key_t *)))) {
1219 for (i = 0; i < total_count; i++) {
1220 key = hsm_key_new_privkey_object_handle(ctx, session,
1228 *count = total_count;
1244 return hsm_list_keys_session_internal(ctx, session, count, 1);
1257 (void) hsm_list_keys_session_internal(ctx, session, &count, 0);
1267 const unsigned char *
id,
size_t len)
1270 CK_OBJECT_HANDLE private_key_handle;
1272 private_key_handle = hsm_find_object_handle_for_id(
1278 if (private_key_handle != 0) {
1279 key = hsm_key_new_privkey_object_handle(ctx, session,
1280 private_key_handle);
1298 const unsigned char *
id,
1305 if (!
id)
return NULL;
1308 key = hsm_find_key_by_id_session(ctx, ctx->
session[i],
id, len);
1309 if (key)
return key;
1321 hsm_find_repository_session(
hsm_ctx_t *ctx,
const char *repository)
1341 "hsm_find_repository_session()",
1342 "Can't find repository: %s", repository);
1356 unsigned long hKey = 0;
1357 unsigned char *data = NULL;
1358 size_t data_size = 0;
1366 if (!session || !session->
module) {
1381 if (hsm_pkcs11_check_error(ctx, rv,
"C_GetAttributeValue")) {
1384 public_exponent_len =
template[0].ulValueLen;
1385 modulus_len =
template[1].ulValueLen;
1387 public_exponent =
template[0].pValue = malloc(public_exponent_len);
1388 if (!public_exponent) {
1390 "Error allocating memory for public exponent");
1394 modulus =
template[1].pValue = malloc(modulus_len);
1397 "Error allocating memory for modulus");
1398 free(public_exponent);
1407 if (hsm_pkcs11_check_error(ctx, rv,
"get attribute value")) {
1408 free(
template[0].pValue);
1409 free(
template[1].pValue);
1414 hsm_remove_leading_zeroes(public_exponent, &public_exponent_len);
1415 hsm_remove_leading_zeroes(modulus, &modulus_len);
1417 data_size = public_exponent_len + modulus_len + 1;
1418 if (public_exponent_len <= 255) {
1419 data = malloc(data_size);
1422 "Error allocating memory for pub key rr data");
1423 free(public_exponent);
1427 data[0] = public_exponent_len;
1428 memcpy(&data[1], public_exponent, public_exponent_len);
1429 memcpy(&data[1 + public_exponent_len], modulus, modulus_len);
1430 }
else if (public_exponent_len <= 65535) {
1432 data = malloc(data_size);
1435 "Error allocating memory for pub key rr data");
1436 free(public_exponent);
1441 ldns_write_uint16(&data[1], (uint16_t) public_exponent_len);
1442 memcpy(&data[3], public_exponent, public_exponent_len);
1443 memcpy(&data[3 + public_exponent_len], modulus, modulus_len);
1446 "Public exponent too big");
1447 free(public_exponent);
1451 rdf = ldns_rdf_new(LDNS_RDF_TYPE_B64, data_size, data);
1452 free(public_exponent);
1471 unsigned char *data = NULL;
1472 size_t data_size = 0;
1482 if (!session || !session->
module) {
1492 if (hsm_pkcs11_check_error(ctx, rv,
"C_GetAttributeValue")) {
1495 prime_len =
template[0].ulValueLen;
1496 subprime_len =
template[1].ulValueLen;
1497 base_len =
template[2].ulValueLen;
1498 value_len =
template[3].ulValueLen;
1500 prime =
template[0].pValue = malloc(prime_len);
1503 "Error allocating memory for prime");
1507 subprime =
template[1].pValue = malloc(subprime_len);
1510 "Error allocating memory for subprime");
1515 base =
template[2].pValue = malloc(base_len);
1518 "Error allocating memory for base");
1524 value =
template[3].pValue = malloc(value_len);
1527 "Error allocating memory for value");
1539 if (hsm_pkcs11_check_error(ctx, rv,
"get attribute value")) {
1547 data_size = prime_len + subprime_len + base_len + value_len + 1;
1548 data = malloc(data_size);
1551 "Error allocating memory for pub key rr data");
1558 data[0] = (prime_len - 64) / 8;
1559 memcpy(&data[1], subprime, subprime_len);
1560 memcpy(&data[1 + subprime_len], prime, prime_len);
1561 memcpy(&data[1 + subprime_len + prime_len], base, base_len);
1562 memcpy(&data[1 + subprime_len + prime_len + base_len], value, value_len);
1564 rdf = ldns_rdf_new(LDNS_RDF_TYPE_B64, data_size, data);
1586 if (!session || !session->
module) {
1596 if (hsm_pkcs11_check_error(ctx, rv,
"C_GetAttributeValue")) {
1599 value_len =
template[0].ulValueLen;
1601 value =
template[0].pValue = malloc(value_len);
1604 "Error allocating memory for value");
1613 if (hsm_pkcs11_check_error(ctx, rv,
"get attribute value")) {
1618 rdf = ldns_rdf_new(LDNS_RDF_TYPE_B64, value_len, value);
1627 switch (hsm_get_key_algorithm(ctx, session, key)) {
1629 return hsm_get_key_rdata_rsa(ctx, session, key);
1632 return hsm_get_key_rdata_dsa(ctx, session, key);
1635 return hsm_get_key_rdata_gost(ctx, session, key);
1647 hsm_create_prefix(
CK_ULONG digest_len,
1652 const CK_BYTE RSA_MD5_ID[] = { 0x30, 0x20, 0x30, 0x0C, 0x06, 0x08, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x02, 0x05, 0x05, 0x00, 0x04, 0x10 };
1653 const CK_BYTE RSA_SHA1_ID[] = { 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2B, 0x0E, 0x03, 0x02, 0x1A, 0x05, 0x00, 0x04, 0x14 };
1654 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 };
1655 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 };
1658 case LDNS_SIGN_RSAMD5:
1659 *data_size =
sizeof(RSA_MD5_ID) + digest_len;
1660 data = malloc(*data_size);
1661 memcpy(data, RSA_MD5_ID,
sizeof(RSA_MD5_ID));
1663 case LDNS_SIGN_RSASHA1:
1664 case LDNS_SIGN_RSASHA1_NSEC3:
1665 *data_size =
sizeof(RSA_SHA1_ID) + digest_len;
1666 data = malloc(*data_size);
1667 memcpy(data, RSA_SHA1_ID,
sizeof(RSA_SHA1_ID));
1669 case LDNS_SIGN_RSASHA256:
1670 *data_size =
sizeof(RSA_SHA256_ID) + digest_len;
1671 data = malloc(*data_size);
1672 memcpy(data, RSA_SHA256_ID,
sizeof(RSA_SHA256_ID));
1674 case LDNS_SIGN_RSASHA512:
1675 *data_size =
sizeof(RSA_SHA512_ID) + digest_len;
1676 data = malloc(*data_size);
1677 memcpy(data, RSA_SHA512_ID,
sizeof(RSA_SHA512_ID));
1680 case LDNS_SIGN_DSA_NSEC3:
1681 case LDNS_SIGN_ECC_GOST:
1682 #if LDNS_BUILD_CONFIG_USE_ECDSA 1683 case LDNS_SIGN_ECDSAP256SHA256:
1684 case LDNS_SIGN_ECDSAP384SHA384:
1686 *data_size = digest_len;
1687 data = malloc(*data_size);
1698 CK_MECHANISM_TYPE mechanism_type,
1700 ldns_buffer *sign_buf)
1706 digest_mechanism.pParameter = NULL;
1707 digest_mechanism.ulParameterLen = 0;
1708 digest_mechanism.
mechanism = mechanism_type;
1709 digest = malloc(digest_len);
1712 if (hsm_pkcs11_check_error(ctx, rv,
"HSM digest init")) {
1718 ldns_buffer_begin(sign_buf),
1719 ldns_buffer_position(sign_buf),
1722 if (hsm_pkcs11_check_error(ctx, rv,
"HSM digest")) {
1731 ldns_buffer *sign_buf,
1749 session = hsm_find_key_session(ctx, key);
1750 if (!session)
return NULL;
1755 switch (algorithm) {
1756 case LDNS_SIGN_RSAMD5:
1758 digest = hsm_digest_through_hsm(ctx, session,
1762 case LDNS_SIGN_RSASHA1:
1763 case LDNS_SIGN_RSASHA1_NSEC3:
1765 case LDNS_SIGN_DSA_NSEC3:
1766 digest_len = LDNS_SHA1_DIGEST_LENGTH;
1767 digest = malloc(digest_len);
1768 digest = ldns_sha1(ldns_buffer_begin(sign_buf),
1769 ldns_buffer_position(sign_buf),
1773 case LDNS_SIGN_RSASHA256:
1774 #if LDNS_BUILD_CONFIG_USE_ECDSA 1775 case LDNS_SIGN_ECDSAP256SHA256:
1777 digest_len = LDNS_SHA256_DIGEST_LENGTH;
1778 digest = malloc(digest_len);
1779 digest = ldns_sha256(ldns_buffer_begin(sign_buf),
1780 ldns_buffer_position(sign_buf),
1783 #if LDNS_BUILD_CONFIG_USE_ECDSA 1784 case LDNS_SIGN_ECDSAP384SHA384:
1785 digest_len = LDNS_SHA384_DIGEST_LENGTH;
1786 digest = malloc(digest_len);
1787 digest = ldns_sha384(ldns_buffer_begin(sign_buf),
1788 ldns_buffer_position(sign_buf),
1792 case LDNS_SIGN_RSASHA512:
1793 digest_len = LDNS_SHA512_DIGEST_LENGTH;
1794 digest = malloc(digest_len);
1795 digest = ldns_sha512(ldns_buffer_begin(sign_buf),
1796 ldns_buffer_position(sign_buf),
1799 case LDNS_SIGN_ECC_GOST:
1801 digest = hsm_digest_through_hsm(ctx, session,
1818 data = hsm_create_prefix(digest_len, algorithm, &data_len);
1819 memcpy(data + data_len - digest_len, digest, digest_len);
1821 sign_mechanism.pParameter = NULL;
1822 sign_mechanism.ulParameterLen = 0;
1824 case LDNS_SIGN_RSAMD5:
1825 case LDNS_SIGN_RSASHA1:
1826 case LDNS_SIGN_RSASHA1_NSEC3:
1827 case LDNS_SIGN_RSASHA256:
1828 case LDNS_SIGN_RSASHA512:
1832 case LDNS_SIGN_DSA_NSEC3:
1835 case LDNS_SIGN_ECC_GOST:
1838 #if LDNS_BUILD_CONFIG_USE_ECDSA 1840 case LDNS_SIGN_ECDSAP256SHA256:
1841 case LDNS_SIGN_ECDSAP384SHA384:
1855 if (hsm_pkcs11_check_error(ctx, rv,
"sign init")) {
1864 if (hsm_pkcs11_check_error(ctx, rv,
"sign final")) {
1870 sig_rdf = ldns_rdf_new_frm_data(LDNS_RDF_TYPE_B64,
1882 hsm_dname_is_wildcard(
const ldns_rdf* dname)
1884 return ( ldns_dname_label_count(dname) > 0 &&
1885 ldns_rdf_data(dname)[0] == 1 &&
1886 ldns_rdf_data(dname)[1] ==
'*');
1890 hsm_create_empty_rrsig(
const ldns_rr_list *rrset,
1895 uint32_t orig_class;
1897 uint8_t label_count;
1899 label_count = ldns_dname_label_count(
1900 ldns_rr_owner(ldns_rr_list_rr(rrset, 0)));
1902 if (hsm_dname_is_wildcard(ldns_rr_owner(ldns_rr_list_rr(rrset, 0)))) {
1906 rrsig = ldns_rr_new_frm_type(LDNS_RR_TYPE_RRSIG);
1909 orig_ttl = ldns_rr_ttl(ldns_rr_list_rr(rrset, 0));
1910 orig_class = ldns_rr_get_class(ldns_rr_list_rr(rrset, 0));
1912 ldns_rr_set_class(rrsig, orig_class);
1913 ldns_rr_set_ttl(rrsig, orig_ttl);
1914 ldns_rr_set_owner(rrsig,
1917 ldns_rr_list_rr(rrset,
1923 (void)ldns_rr_rrsig_set_origttl(
1925 ldns_native2rdf_int32(LDNS_RDF_TYPE_INT32,
1928 (void)ldns_rr_rrsig_set_signame(
1930 ldns_rdf_clone(sign_params->
owner));
1932 (void)ldns_rr_rrsig_set_labels(
1934 ldns_native2rdf_int8(LDNS_RDF_TYPE_INT8,
1939 (void)ldns_rr_rrsig_set_inception(
1941 ldns_native2rdf_int32(
1945 (void)ldns_rr_rrsig_set_inception(
1947 ldns_native2rdf_int32(LDNS_RDF_TYPE_TIME, now));
1950 (void)ldns_rr_rrsig_set_expiration(
1952 ldns_native2rdf_int32(
1956 (void)ldns_rr_rrsig_set_expiration(
1958 ldns_native2rdf_int32(
1960 now + LDNS_DEFAULT_EXP_TIME));
1963 (void)ldns_rr_rrsig_set_keytag(
1965 ldns_native2rdf_int16(LDNS_RDF_TYPE_INT16,
1968 (void)ldns_rr_rrsig_set_algorithm(
1970 ldns_native2rdf_int8(
1974 (void)ldns_rr_rrsig_set_typecovered(
1976 ldns_native2rdf_int16(
1978 ldns_rr_get_type(ldns_rr_list_rr(rrset,
1991 char *(pin_callback)(
unsigned int,
const char *,
unsigned int))
1994 xmlXPathContextPtr xpath_ctx;
1995 xmlXPathObjectPtr xpath_obj;
2008 int repositories = 0;
2012 _hsm_ctx = hsm_ctx_new();
2015 config_file = strdup(config);
2017 config_file = strdup(HSM_DEFAULT_CONFIG);
2021 doc = xmlParseFile(config_file);
2028 xpath_ctx = xmlXPathNewContext(doc);
2029 if(xpath_ctx == NULL) {
2031 hsm_ctx_free(_hsm_ctx);
2037 xexpr = (xmlChar *)
"//Configuration/RepositoryList/Repository";
2038 xpath_obj = xmlXPathEvalExpression(xexpr, xpath_ctx);
2039 if(xpath_obj == NULL) {
2040 xmlXPathFreeContext(xpath_ctx);
2042 hsm_ctx_free(_hsm_ctx);
2047 if (xpath_obj->nodesetval) {
2048 for (i = 0; i < xpath_obj->nodesetval->nodeNr; i++) {
2053 hsm_config_default(&module_config);
2055 curNode = xpath_obj->nodesetval->nodeTab[i]->xmlChildrenNode;
2056 repository = (
char *) xmlGetProp(xpath_obj->nodesetval->nodeTab[i],
2057 (
const xmlChar *)
"name");
2060 if (xmlStrEqual(curNode->name, (
const xmlChar *)
"TokenLabel"))
2061 token_label = (
char *) xmlNodeGetContent(curNode);
2062 if (xmlStrEqual(curNode->name, (
const xmlChar *)
"Module"))
2063 module_path = (
char *) xmlNodeGetContent(curNode);
2064 if (xmlStrEqual(curNode->name, (
const xmlChar *)
"PIN"))
2065 module_pin = (
char *) xmlNodeGetContent(curNode);
2066 if (xmlStrEqual(curNode->name, (
const xmlChar *)
"SkipPublicKey"))
2068 if (xmlStrEqual(curNode->name, (
const xmlChar *)
"AllowExtraction"))
2070 curNode = curNode->next;
2073 if (repository && token_label && module_path) {
2097 if (module_pin == NULL)
break;
2109 memset(module_pin, 0, strlen(module_pin));
2115 "No pin or callback function");
2132 xmlXPathFreeObject(xpath_obj);
2133 xmlXPathFreeContext(xpath_ctx);
2136 if (result ==
HSM_OK && repositories == 0) {
2138 "No repositories found");
2148 hsm_ctx_close(_hsm_ctx, 1);
2155 return hsm_ctx_clone(_hsm_ctx);
2165 CK_SESSION_HANDLE session_handle;
2173 if (session == NULL)
continue;
2179 if (hsm_pkcs11_check_error(ctx, rv,
"get session info")) {
2186 "Session not logged in");
2196 if (hsm_pkcs11_check_error(ctx, rv,
"test open session")) {
2200 if (hsm_pkcs11_check_error(ctx, rv,
"test close session")) {
2211 hsm_ctx_close(ctx, 0);
2226 params->
flags = LDNS_KEY_ZONE_KEY;
2230 params->
owner = NULL;
2238 if (params->
owner) ldns_rdf_deep_free(params->
owner);
2247 size_t key_count = 0;
2248 size_t cur_key_count;
2260 keys = realloc(keys_prev,
2261 (key_count + cur_key_count) *
sizeof(
hsm_key_t *));
2267 for (j = 0; j < cur_key_count; j++) {
2268 keys[key_count + j] = session_keys[j];
2270 key_count += cur_key_count;
2282 const char *repository)
2286 if (!repository)
return NULL;
2289 session = hsm_find_repository_session(ctx, repository);
2312 const char *repository)
2316 if (!repository)
return 0;
2319 session = hsm_find_repository_session(ctx, repository);
2329 unsigned char *id_bytes;
2333 id_bytes = hsm_hex_parse(
id, &len);
2335 if (!id_bytes)
return NULL;
2337 key = hsm_find_key_by_id_bin(ctx, id_bytes, len);
2344 const char *repository,
2345 unsigned long keysize)
2350 unsigned char id[16];
2354 CK_OBJECT_HANDLE publicKey, privateKey;
2355 CK_KEY_TYPE keyType =
CKK_RSA;
2359 CK_BYTE publicExponent[] = { 1, 0, 1 };
2366 session = hsm_find_repository_session(ctx, repository);
2367 if (!session)
return NULL;
2375 }
while ((key = hsm_find_key_by_id_bin(ctx,
id, 16)));
2378 hsm_hex_unparse(id_str,
id, 16);
2390 {
CKA_WRAP, &cfalse,
sizeof(cfalse) },
2400 {
CKA_SIGN, &ctrue,
sizeof (ctrue) },
2411 publicKeyTemplate, 9,
2412 privateKeyTemplate, 10,
2415 if (hsm_pkcs11_check_error(ctx, rv,
"generate key pair")) {
2419 new_key = hsm_key_new();
2437 const char *repository,
2438 unsigned long keysize)
2443 CK_OBJECT_HANDLE domainPar, publicKey, privateKey;
2449 unsigned char id[16];
2453 CK_KEY_TYPE keyType =
CKK_DSA;
2473 {
CKA_BASE, dsa_g,
sizeof(dsa_g) },
2479 {
CKA_WRAP, &cfalse,
sizeof(cfalse) },
2487 {
CKA_SIGN, &ctrue,
sizeof(ctrue) },
2497 session = hsm_find_repository_session(ctx, repository);
2498 if (!session)
return NULL;
2507 }
while ((key = hsm_find_key_by_id_bin(ctx,
id, 16)));
2510 hsm_hex_unparse(id_str,
id, 16);
2518 if (hsm_pkcs11_check_error(ctx, rv,
"generate domain parameters")) {
2523 domainPar, publicKeyTemplate, 3);
2524 if (hsm_pkcs11_check_error(ctx, rv,
"get domain parameters")) {
2529 if (hsm_pkcs11_check_error(ctx, rv,
"destroy domain parameters")) {
2537 publicKeyTemplate, 10,
2538 privateKeyTemplate, 10,
2541 if (hsm_pkcs11_check_error(ctx, rv,
"generate key pair")) {
2545 new_key = hsm_key_new();
2555 const char *repository)
2560 CK_OBJECT_HANDLE publicKey, privateKey;
2566 unsigned char id[16];
2575 CK_BYTE oid1[] = { 0x06, 0x07, 0x2A, 0x85, 0x03, 0x02, 0x02, 0x23, 0x01 };
2576 CK_BYTE oid2[] = { 0x06, 0x07, 0x2A, 0x85, 0x03, 0x02, 0x02, 0x1E, 0x01 };
2586 {
CKA_WRAP, &cfalse,
sizeof(cfalse) },
2594 {
CKA_SIGN, &ctrue,
sizeof(ctrue) },
2604 session = hsm_find_repository_session(ctx, repository);
2605 if (!session)
return NULL;
2614 }
while ((key = hsm_find_key_by_id_bin(ctx,
id, 16)));
2617 hsm_hex_unparse(id_str,
id, 16);
2623 publicKeyTemplate, 9,
2624 privateKeyTemplate, 10,
2627 if (hsm_pkcs11_check_error(ctx, rv,
"generate key pair")) {
2631 new_key = hsm_key_new();
2645 if (!key)
return -1;
2647 session = hsm_find_key_session(ctx, key);
2648 if (!session)
return -2;
2652 if (hsm_pkcs11_check_error(ctx, rv,
"Destroy private key")) {
2660 if (hsm_pkcs11_check_error(ctx, rv,
"Destroy public key")) {
2681 for (i = 0; i < count; i++) {
2696 if (!key)
return NULL;
2698 session = hsm_find_key_session(ctx, key);
2699 if (!session)
return NULL;
2701 id = hsm_get_id_for_object(ctx, session, key->
private_key, &len);
2702 if (!
id)
return NULL;
2705 id_str = malloc(len * 2 + 1);
2711 hsm_hex_unparse(id_str,
id, len);
2726 session = hsm_find_key_session(ctx, key);
2727 if (!session)
return NULL;
2732 if (key_info->
id == NULL) {
2733 key_info->
id = strdup(
"");
2736 key_info->
algorithm = (
unsigned long) hsm_get_key_algorithm(ctx,
2739 key_info->
keysize = (
unsigned long) hsm_get_key_size(ctx,
2781 const ldns_rr_list* rrset,
2786 ldns_buffer *sign_buf;
2790 if (!key)
return NULL;
2791 if (!sign_params)
return NULL;
2794 signature = hsm_create_empty_rrsig((ldns_rr_list *)rrset,
2800 sign_buf = ldns_buffer_new(LDNS_MAX_PACKETLEN);
2802 if (ldns_rrsig2buffer_wire(sign_buf, signature)
2803 != LDNS_STATUS_OK) {
2804 ldns_buffer_free(sign_buf);
2806 ldns_rr_free(signature);
2811 for(i = 0; i < ldns_rr_list_rr_count(rrset); i++) {
2812 ldns_rr2canonical(ldns_rr_list_rr(rrset, i));
2816 if (ldns_rr_list2buffer_wire(sign_buf, rrset)
2817 != LDNS_STATUS_OK) {
2818 ldns_buffer_free(sign_buf);
2819 ldns_rr_free(signature);
2823 b64_rdf = hsm_sign_buffer(ctx, sign_buf, key, sign_params->
algorithm);
2825 ldns_buffer_free(sign_buf);
2828 ldns_rr_free(signature);
2832 ldns_rr_rrsig_set_sig(signature, b64_rdf);
2849 size_t *digest_length)
2857 if (hsm_pkcs11_check_error(ctx, rv,
"digest init")) {
2868 if (hsm_pkcs11_check_error(ctx, rv,
"digest to determine result size")) {
2878 if (hsm_pkcs11_check_error(ctx, rv,
"digest")) {
2892 uint16_t iterations,
2893 uint8_t salt_length,
2896 char *orig_owner_str;
2897 size_t hashed_owner_str_len;
2898 ldns_rdf *hashed_owner;
2899 char *hashed_owner_str;
2900 char *hashed_owner_b32;
2901 int hashed_owner_b32_len;
2904 size_t hash_length = 0;
2914 mechanism.pParameter = NULL;
2915 mechanism.ulParameterLen = 0;
2918 printf(
"unknown algo: %u\n", (
unsigned int)algorithm);
2933 orig_owner_str = ldns_rdf2str(name);
2935 hashed_owner_str_len = salt_length + ldns_rdf_size(name);
2936 hashed_owner_str = LDNS_XMALLOC(
char, hashed_owner_str_len);
2937 memcpy(hashed_owner_str, ldns_rdf_data(name), ldns_rdf_size(name));
2938 memcpy(hashed_owner_str + ldns_rdf_size(name), salt, salt_length);
2940 for (cur_it = iterations + 1; cur_it > 0; cur_it--) {
2941 if (hash != NULL) free(hash);
2942 hash = (
char *) hsm_digest(ctx,
2946 hashed_owner_str_len,
2949 LDNS_FREE(hashed_owner_str);
2950 hashed_owner_str_len = salt_length + hash_length;
2951 hashed_owner_str = LDNS_XMALLOC(
char, hashed_owner_str_len);
2952 if (!hashed_owner_str) {
2957 memcpy(hashed_owner_str, hash, hash_length);
2958 memcpy(hashed_owner_str + hash_length, salt, salt_length);
2961 LDNS_FREE(hashed_owner_str);
2962 hashed_owner_str = hash;
2963 hashed_owner_str_len = hash_length;
2964 hashed_owner_b32 = LDNS_XMALLOC(
char,
2965 ldns_b32_ntop_calculate_size(
2966 hashed_owner_str_len) + 1);
2967 LDNS_FREE(orig_owner_str);
2968 hashed_owner_b32_len =
2969 (size_t) ldns_b32_ntop_extended_hex((uint8_t *) hashed_owner_str,
2970 hashed_owner_str_len,
2972 ldns_b32_ntop_calculate_size(
2973 hashed_owner_str_len));
2974 if (hashed_owner_b32_len < 1) {
2975 error_name = ldns_rdf2str(name);
2977 "Error in base32 extended hex encoding " 2978 "of hashed owner name (name: %s, return code: %d)",
2979 error_name, hashed_owner_b32_len);
2980 LDNS_FREE(error_name);
2981 LDNS_FREE(hashed_owner_b32);
2984 hashed_owner_str_len = hashed_owner_b32_len;
2985 hashed_owner_b32[hashed_owner_b32_len] =
'\0';
2987 status = ldns_str2rdf_dname(&hashed_owner, hashed_owner_b32);
2988 if (status != LDNS_STATUS_OK) {
2990 "Error creating rdf from %s", hashed_owner_b32);
2991 LDNS_FREE(hashed_owner_b32);
2996 LDNS_FREE(hashed_owner_b32);
2997 return hashed_owner;
3019 session = hsm_find_key_session(ctx, key);
3020 if (!session)
return NULL;
3022 dnskey = ldns_rr_new();
3023 ldns_rr_set_type(dnskey, LDNS_RR_TYPE_DNSKEY);
3025 ldns_rr_set_owner(dnskey, ldns_rdf_clone(sign_params->
owner));
3027 ldns_rr_push_rdf(dnskey,
3028 ldns_native2rdf_int16(LDNS_RDF_TYPE_INT16,
3029 sign_params->
flags));
3030 ldns_rr_push_rdf(dnskey,
3031 ldns_native2rdf_int8(LDNS_RDF_TYPE_INT8,
3032 LDNS_DNSSEC_KEYPROTO));
3033 ldns_rr_push_rdf(dnskey,
3034 ldns_native2rdf_int8(LDNS_RDF_TYPE_ALG,
3037 rdata = hsm_get_key_rdata(ctx, session, key);
3038 if (rdata == NULL) {
3039 ldns_rr_free(dnskey);
3042 ldns_rr_push_rdf(dnskey, rdata);
3049 unsigned char *buffer,
3050 unsigned long length)
3055 if (!buffer)
return -1;
3080 unsigned char rnd_buf[4];
3083 memcpy(&rnd, rnd_buf, 4);
3095 unsigned char rnd_buf[8];
3098 memcpy(&rnd, rnd_buf, 8);
3111 const char *token_label,
3119 result = hsm_session_init(_hsm_ctx,
3127 return hsm_ctx_add_session(_hsm_ctx, session);
3141 hsm_session_close(_hsm_ctx, _hsm_ctx->
session[i], 1);
3170 "hsm_token_attached()",
3171 "Can't find repository: %s", repository);
3179 case LDNS_SIGN_RSAMD5:
3180 case LDNS_SIGN_RSASHA1:
3181 case LDNS_SIGN_RSASHA1_NSEC3:
3182 case LDNS_SIGN_RSASHA256:
3183 case LDNS_SIGN_RSASHA512:
3185 case LDNS_SIGN_DSA_NSEC3:
3186 case LDNS_SIGN_ECC_GOST:
3189 #if LDNS_BUILD_CONFIG_USE_ECDSA 3190 case LDNS_SIGN_ECDSAP256SHA256:
3191 case LDNS_SIGN_ECDSAP384SHA384:
3215 if (message == NULL) {
3216 return strdup(
"libhsm memory allocation failed");
3232 printf(
"\t\tmodule at %p (sym %p)\n", (
void *) session->
module, (
void *) session->
module->
sym);
3233 printf(
"\t\tmodule path: %s\n", session->
module->
path);
3234 printf(
"\t\trepository name: %s\n", session->
module->
name);
3236 printf(
"\t\tsess handle: %u\n", (
unsigned int) session->
session);
3248 printf(
"CTX Sessions: %lu\n",
3251 printf(
"\tSession at %p\n", (
void *) ctx->
session[i]);
3263 printf(
"\tmodule: %p\n", (
void *) key->
module);
3264 printf(
"\tprivkey handle: %u\n", (
unsigned int) key->
private_key);
3266 printf(
"\tpubkey handle: %u\n", (
unsigned int) key->
public_key);
3268 printf(
"\tpubkey handle: %s\n",
"NULL");
3270 printf(
"\trepository: %s\n", key->
module->
name);
3272 printf(
"\tsize: %lu\n", key_info->
keysize);
3273 printf(
"\tid: %s\n", key_info->
id);
3276 printf(
"key: hsm_get_key_info() returned NULL\n");
3279 printf(
"key: <void>\n");
3291 fprintf(stderr,
"%s\n", message);
3294 fprintf(stderr,
"Unknown error\n");
3322 if (result !=
HSM_OK)
return;
3325 if (hsm_pkcs11_check_error(ctx, rv,
"C_GetTokenInfo")) {
3329 printf(
"Repository: %s\n",session->
module->
name);
3331 printf(
"\tModule: %s\n", session->
module->
path);
3332 printf(
"\tSlot: %lu\n", slot_id);
3333 printf(
"\tToken Label: %.*s\n",
3334 (
int)
sizeof(token_info.
label), token_info.
label);
3335 printf(
"\tManufacturer: %.*s\n",
3336 (
int)
sizeof(token_info.manufacturerID), token_info.manufacturerID);
3337 printf(
"\tModel: %.*s\n",
3338 (
int)
sizeof(token_info.
model), token_info.
model);
3339 printf(
"\tSerial: %.*s\n",
3340 (
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
unsigned int allow_extract
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)