36 #include <ldns/ldns.h> 38 #include <libxml/tree.h> 39 #include <libxml/parser.h> 40 #include <libxml/xpath.h> 41 #include <libxml/xpathInternals.h> 42 #include <libxml/relaxng.h> 53 #define HSM_TOKEN_LABEL_LENGTH 32 61 ldns_pkcs11_rv_str(CK_RV rv)
70 return "CKR_HOST_MEMORY";
72 return "CKR_GENERAL_ERROR";
74 return "CKR_FUNCTION_FAILED";
76 return "CKR_SLOT_ID_INVALID";
78 return "CKR_ATTRIBUTE_READ_ONLY";
80 return "CKR_ATTRIBUTE_SENSITIVE";
82 return "CKR_ATTRIBUTE_TYPE_INVALID";
84 return "CKR_ATTRIBUTE_VALUE_INVALID";
86 return "CKR_DATA_INVALID";
88 return "CKR_DATA_LEN_RANGE";
90 return "CKR_DEVICE_ERROR";
92 return "CKR_DEVICE_MEMORY";
94 return "CKR_DEVICE_REMOVED";
96 return "CKR_ENCRYPTED_DATA_INVALID";
98 return "CKR_ENCRYPTED_DATA_LEN_RANGE";
100 return "CKR_FUNCTION_CANCELED";
102 return "CKR_FUNCTION_NOT_PARALLEL";
104 return "CKR_FUNCTION_NOT_SUPPORTED";
106 return "CKR_KEY_HANDLE_INVALID";
108 return "CKR_KEY_SIZE_RANGE";
110 return "CKR_KEY_TYPE_INCONSISTENT";
112 return "CKR_MECHANISM_INVALID";
114 return "CKR_MECHANISM_PARAM_INVALID";
116 return "CKR_OBJECT_HANDLE_INVALID";
118 return "CKR_OPERATION_ACTIVE";
120 return "CKR_OPERATION_NOT_INITIALIZED";
122 return "CKR_PIN_INCORRECT";
124 return "CKR_PIN_INVALID";
126 return "CKR_PIN_LEN_RANGE";
128 return "CKR_SESSION_CLOSED";
130 return "CKR_SESSION_COUNT";
132 return "CKR_SESSION_HANDLE_INVALID";
134 return "CKR_SESSION_PARALLEL_NOT_SUPPORTED";
136 return "CKR_SESSION_READ_ONLY";
138 return "CKR_SESSION_EXISTS";
140 return "CKR_SIGNATURE_INVALID";
142 return "CKR_SIGNATURE_LEN_RANGE";
144 return "CKR_TEMPLATE_INCOMPLETE";
146 return "CKR_TEMPLATE_INCONSISTENT";
148 return "CKR_TOKEN_NOT_PRESENT";
150 return "CKR_TOKEN_NOT_RECOGNIZED";
152 return "CKR_TOKEN_WRITE_PROTECTED";
154 return "CKR_UNWRAPPING_KEY_HANDLE_INVALID";
156 return "CKR_UNWRAPPING_KEY_SIZE_RANGE";
158 return "CKR_UNWRAPPING_KEY_TYPE_INCONSISTENT";
160 return "CKR_USER_ALREADY_LOGGED_IN";
162 return "CKR_USER_NOT_LOGGED_IN";
164 return "CKR_USER_PIN_NOT_INITIALIZED";
166 return "CKR_USER_TYPE_INVALID";
168 return "CKR_WRAPPED_KEY_INVALID";
170 return "CKR_WRAPPED_KEY_LEN_RANGE";
172 return "CKR_WRAPPING_KEY_HANDLE_INVALID";
174 return "CKR_WRAPPING_KEY_SIZE_RANGE";
176 return "CKR_WRAPPING_KEY_TYPE_INCONSISTENT";
178 return "CKR_RANDOM_SEED_NOT_SUPPORTED";
180 return "CKR_VENDOR_DEFINED";
182 return "CKR_BUFFER_TOO_SMALL";
184 return "CKR_SAVED_STATE_INVALID";
186 return "CKR_INFORMATION_SENSITIVE";
188 return "CKR_STATE_UNSAVEABLE";
190 return "CKR_CRYPTOKI_NOT_INITIALIZED";
192 return "CKR_CRYPTOKI_ALREADY_INITIALIZED";
194 return "CKR_MUTEX_BAD";
196 return "CKR_MUTEX_NOT_LOCKED";
198 return "Unknown error";
204 const char *message, ...)
208 if (ctx && ctx->
error == 0) {
212 va_start(args, message);
231 hsm_pkcs11_check_error(
hsm_ctx_t *
ctx, CK_RV rv,
const char *action)
234 if (ctx && ctx->
error == 0) {
235 ctx->
error = (int) rv;
246 hsm_pkcs11_unload_functions(
void *handle)
249 #if defined(HAVE_LOADLIBRARY) 251 #elif defined(HAVE_DLOPEN) 252 (void) dlclose(handle);
261 CK_C_GetFunctionList pGetFunctionList = NULL;
263 if (module && module->
path) {
266 #if defined(HAVE_LOADLIBRARY) 268 HINSTANCE hDLL = LoadLibrary(_T(module->
path));
276 pGetFunctionList = (CK_C_GetFunctionList)
277 GetProcAddress(hDLL, _T(
"C_GetFunctionList"));
279 #elif defined(HAVE_DLOPEN) 281 void* pDynLib = dlopen(module->
path, RTLD_NOW | RTLD_LOCAL);
283 if (pDynLib == NULL) {
289 pGetFunctionList = (CK_C_GetFunctionList) dlsym(pDynLib,
"C_GetFunctionList");
298 #ifdef HAVE_PKCS11_MODULE 299 return C_GetFunctionList(pkcs11_functions);
305 if (pGetFunctionList == NULL) {
321 if (data == NULL || len == NULL)
return;
325 while ((
unsigned short int)(*p) == 0 && l > 1) {
337 hsm_pkcs11_check_token_name(
hsm_ctx_t *ctx,
340 const char *token_name)
349 if (hsm_pkcs11_check_error(ctx, rv,
"C_GetTokenInfo")) {
355 memcpy(token_name_bytes, token_name, strlen(token_name));
360 result = memcmp(token_info.
label,
369 uint8_t use_pubkey, uint8_t require_backup)
373 if (!name || !module || !tokenlabel)
return NULL;
380 r->
name = strdup(name);
381 r->
module = strdup(module);
388 r->
pin = strdup(pin);
415 const char *token_name, CK_SLOT_ID *slotId)
423 if (token_name == NULL || slotId == NULL)
return HSM_ERROR;
426 if (hsm_pkcs11_check_error(ctx, rv,
"get slot list")) {
432 "No slots found in HSM");
434 }
else if (slotCount > (SIZE_MAX /
sizeof(CK_SLOT_ID))) {
436 "Too many slots found in HSM");
440 slotIds = malloc(
sizeof(CK_SLOT_ID) * slotCount);
441 if(slotIds == NULL) {
443 "Could not allocate slot ID table");
448 if (hsm_pkcs11_check_error(ctx, rv,
"get slot list")) {
452 for (cur_slot = 0; cur_slot < slotCount; cur_slot++) {
453 if (hsm_pkcs11_check_token_name(ctx,
457 *slotId = slotIds[cur_slot];
465 "could not find token with the name %s", token_name);
474 hsm_module_new(
const char *repository,
475 const char *token_label,
481 if (!repository || !path)
return NULL;
485 if (!module)
return NULL;
499 module->
name = strdup(repository);
501 module->
path = strdup(path);
512 if (module->
name) free(module->
name);
514 if (module->
path) free(module->
path);
522 hsm_session_new(
hsm_module_t *module, CK_SESSION_HANDLE session_handle)
527 session->
session = session_handle;
550 const char *repository,
const char *token_label,
551 const char *module_path,
const char *pin,
558 CK_SESSION_HANDLE session_handle;
559 int first = 1, result;
566 module = hsm_module_new(repository, token_label, module_path, config);
568 rv = hsm_pkcs11_load_functions(module);
571 "hsm_session_init()",
572 "PKCS#11 module load failed: %s", module_path);
573 hsm_module_free(module);
580 if (hsm_pkcs11_check_error(ctx, rv,
"Initialization")) {
581 hsm_module_free(module);
587 result = hsm_get_slot_id(ctx, module->
sym, token_label, &slot_id);
589 hsm_module_free(module);
597 if (hsm_pkcs11_check_error(ctx, rv,
"Open first session")) {
598 hsm_module_free(module);
603 (
unsigned char *) pin,
604 strlen((
char *)pin));
607 *session = hsm_session_new(module, session_handle);
611 if (session_handle) {
613 C_CloseSession(session_handle);
614 if (hsm_pkcs11_check_error(ctx, rv,
615 "finalize after failed login")) {
616 hsm_module_free(module);
624 if (hsm_pkcs11_check_error(ctx, rv,
"finalize after failed login")) {
625 hsm_module_free(module);
629 hsm_module_free(module);
634 "hsm_session_init()",
635 "Incorrect PIN for repository %s", repository);
649 CK_SESSION_HANDLE session_handle;
653 result = hsm_get_slot_id(ctx,
657 if (result !=
HSM_OK)
return NULL;
664 if (hsm_pkcs11_check_error(ctx, rv,
"Clone session")) {
667 new_session = hsm_session_new(session->
module, session_handle);
694 hsm_session_free(ctx->
session[i]);
717 (void) hsm_pkcs11_check_error(ctx, rv,
"Logout");
722 (void) hsm_pkcs11_check_error(ctx, rv,
"Close session");
727 (void) hsm_pkcs11_check_error(ctx, rv,
"Finalize");
730 hsm_module_free(session->
module);
733 hsm_session_free(session);
743 hsm_ctx_close(
hsm_ctx_t *ctx,
int unload)
749 hsm_session_close(ctx, ctx->
session[i], unload);
764 if (!ctx || !session)
return -1;
780 new_ctx = hsm_ctx_new();
782 new_session = hsm_session_clone(ctx, ctx->
session[i]);
786 hsm_ctx_close(new_ctx, 0);
789 hsm_ctx_add_session(new_ctx, new_session);
827 CK_KEY_TYPE key_type;
838 if (hsm_pkcs11_check_error(ctx, rv,
839 "Get attr value algorithm type")) {
847 if ((
CK_LONG)
template[0].ulValueLen < 1) {
887 if (hsm_pkcs11_check_error(ctx, rv,
888 "Get attr value algorithm type")) {
892 if ((
CK_ULONG)
template[0].ulValueLen < 1) {
902 if (hsm_pkcs11_check_error(ctx, rv,
"Could not get the size of the modulus of the private key")) {
907 modulus = (
CK_BYTE_PTR)malloc(template2[0].ulValueLen);
908 template2[0].pValue = modulus;
909 if (modulus == NULL) {
911 "Error allocating memory for modulus");
921 if (hsm_pkcs11_check_error(ctx, rv,
"Could not get the modulus of the private key")) {
927 modulus_bits = template2[0].ulValueLen * 8;
929 for (
int i = 0; modulus_bits && (modulus[i] & mask) == 0; modulus_bits--) {
963 if (hsm_pkcs11_check_error(ctx, rv,
"Could not get the size of the prime of the private key")) {
967 return template2[0].ulValueLen * 8;
973 static unsigned char *
987 if (!session || !session->
module || !key || !data_len) {
996 if (hsm_pkcs11_check_error(ctx, rv,
"C_GetAttributeValue")) {
999 value_len =
template[0].ulValueLen;
1001 value =
template[0].pValue = malloc(value_len);
1004 "Error allocating memory for value");
1007 memset(value, 0, value_len);
1014 if (hsm_pkcs11_check_error(ctx, rv,
"get attribute value")) {
1019 if(value_len !=
template[0].ulValueLen) {
1021 "HSM returned two different length for a same CKA_EC_POINT. " \
1022 "Abnormal behaviour detected.");
1028 if (value_len < 2) {
1030 "The DER value is too short");
1036 if (value[0] != 0x04) {
1038 "Invalid identifier octet in the DER value");
1045 if (value[1] <= 0x7F) {
1047 }
else if (value[1] == 0x80) {
1049 "Indefinite length is not supported in DER values");
1054 header_len += value[1] & 0x80;
1058 if (value_len - header_len < 2) {
1060 "The value is too short");
1067 if (value[header_len] != 0x04) {
1069 "The value is not uncompressed");
1075 *data_len = value_len - header_len;
1076 data = malloc(*data_len);
1079 "Error allocating memory for data");
1084 memcpy(data, value + header_len, *data_len);
1099 unsigned char*
value = hsm_get_key_ecdsa_value(ctx, session, key, &value_len);
1102 if (value == NULL)
return 0;
1104 if( ((
CK_ULONG) - 1) / (8/2) < value_len) {
1110 bits = value_len * 8 / 2;
1121 switch (algorithm) {
1123 return hsm_get_key_size_rsa(ctx, session, key);
1126 return hsm_get_key_size_dsa(ctx, session, key);
1132 return hsm_get_key_size_ecdsa(ctx, session, key);
1138 static CK_OBJECT_HANDLE
1139 hsm_find_object_handle_for_id(
hsm_ctx_t *ctx,
1141 CK_OBJECT_CLASS key_class,
1146 CK_OBJECT_HANDLE object;
1150 {
CKA_CLASS, &key_class,
sizeof(key_class) },
1156 if (hsm_pkcs11_check_error(ctx, rv,
"Find objects init")) {
1164 if (hsm_pkcs11_check_error(ctx, rv,
"Find object")) {
1166 hsm_pkcs11_check_error(ctx, rv,
"Find objects cleanup");
1171 if (hsm_pkcs11_check_error(ctx, rv,
"Find object final")) {
1175 if (objectCount > 0) {
1187 static unsigned char *
1188 hsm_hex_parse(
const char *hex,
size_t *len)
1190 unsigned char *bytes;
1195 if (!len)
return NULL;
1198 if (!hex)
return NULL;
1199 hex_len = strlen(hex);
1200 if (hex_len % 2 != 0) {
1205 bytes = malloc(*len);
1206 for (i = 0; i < *len; i++) {
1207 bytes[i] = ldns_hexdigit_to_int(hex[2*i]) * 16 +
1208 ldns_hexdigit_to_int(hex[2*i+1]);
1218 hsm_hex_unparse(
char *dst,
const unsigned char *src,
size_t len)
1220 size_t dst_len = len*2 + 1;
1223 for (i = 0; i < len; i++) {
1224 snprintf(dst + (2*i), dst_len,
"%02x", src[i]);
1236 CK_OBJECT_HANDLE
object,
1252 if (hsm_pkcs11_check_error(ctx, rv,
"Get attr value")) {
1257 if ((
CK_LONG)
template[0].ulValueLen < 1) {
1263 template[0].pValue = malloc(
template[0].ulValueLen);
1269 if (hsm_pkcs11_check_error(ctx, rv,
"Get attr value 2")) {
1271 free(
template[0].pValue);
1275 *len =
template[0].ulValueLen;
1276 return template[0].pValue;
1284 libhsm_key_new_privkey_object_handle(
hsm_ctx_t *ctx,
1286 CK_OBJECT_HANDLE
object)
1292 id = hsm_get_id_for_object(ctx, session,
object, &len);
1294 if (!
id)
return NULL;
1296 key = libhsm_key_new();
1300 key->
public_key = hsm_find_object_handle_for_id(
1318 hsm_list_keys_session_internal(
hsm_ctx_t *ctx,
1328 {
CKA_CLASS, &key_class,
sizeof(key_class) },
1335 CK_OBJECT_HANDLE
object[max_object_count];
1336 CK_OBJECT_HANDLE *key_handles = NULL, *new_key_handles = NULL;
1341 if (hsm_pkcs11_check_error(ctx, rv,
"Find objects init")) {
1346 while (objectCount > 0) {
1351 if (hsm_pkcs11_check_error(ctx, rv,
"Find first object")) {
1353 hsm_pkcs11_check_error(ctx, rv,
"Find objects cleanup");
1357 total_count += objectCount;
1358 if (objectCount > 0 && store) {
1359 if (SIZE_MAX /
sizeof(CK_OBJECT_HANDLE) < total_count) {
1361 "Too much object handle returned by HSM to allocate key_handles");
1365 new_key_handles = realloc(key_handles, total_count *
sizeof(CK_OBJECT_HANDLE));
1366 if (new_key_handles != NULL) {
1367 key_handles = new_key_handles;
1370 "Error allocating memory for object handle (OOM)");
1374 for (i = 0; i < objectCount; i++) {
1375 key_handles[j] =
object[i];
1382 if (hsm_pkcs11_check_error(ctx, rv,
"Find objects final")) {
1389 "Too much object handle returned by HSM to allocate keys");
1396 "Error allocating memory for keys table (OOM)");
1400 for (i = 0; i < total_count; i++) {
1401 key = libhsm_key_new_privkey_object_handle(ctx, session,
1412 *count = total_count;
1433 return hsm_list_keys_session_internal(ctx, session, count, 1);
1442 const unsigned char *
id,
size_t len)
1445 CK_OBJECT_HANDLE private_key_handle;
1447 private_key_handle = hsm_find_object_handle_for_id(
1453 if (private_key_handle != 0) {
1454 key = libhsm_key_new_privkey_object_handle(ctx, session,
1455 private_key_handle);
1473 const unsigned char *
id,
1479 if (!
id)
return NULL;
1482 key = hsm_find_key_by_id_session(ctx, ctx->
session[i],
id, len);
1483 if (key)
return key;
1495 hsm_find_repository_session(
hsm_ctx_t *ctx,
const char *repository)
1515 "hsm_find_repository_session()",
1516 "Can't find repository: %s", repository);
1530 unsigned long hKey = 0;
1531 unsigned char *data = NULL;
1532 size_t data_size = 0;
1540 if (!session || !session->
module) {
1555 if (hsm_pkcs11_check_error(ctx, rv,
"C_GetAttributeValue")) {
1558 public_exponent_len =
template[0].ulValueLen;
1559 modulus_len =
template[1].ulValueLen;
1561 public_exponent =
template[0].pValue = malloc(public_exponent_len);
1562 if (!public_exponent) {
1564 "Error allocating memory for public exponent");
1568 modulus =
template[1].pValue = malloc(modulus_len);
1571 "Error allocating memory for modulus");
1572 free(public_exponent);
1581 if (hsm_pkcs11_check_error(ctx, rv,
"get attribute value")) {
1582 free(
template[0].pValue);
1583 free(
template[1].pValue);
1588 hsm_remove_leading_zeroes(public_exponent, &public_exponent_len);
1589 hsm_remove_leading_zeroes(modulus, &modulus_len);
1591 data_size = public_exponent_len + modulus_len + 1;
1592 if (public_exponent_len <= 255) {
1593 data = malloc(data_size);
1596 "Error allocating memory for pub key rr data");
1597 free(public_exponent);
1601 data[0] = public_exponent_len;
1602 memcpy(&data[1], public_exponent, public_exponent_len);
1603 memcpy(&data[1 + public_exponent_len], modulus, modulus_len);
1604 }
else if (public_exponent_len <= 65535) {
1606 data = malloc(data_size);
1609 "Error allocating memory for pub key rr data");
1610 free(public_exponent);
1615 ldns_write_uint16(&data[1], (uint16_t) public_exponent_len);
1616 memcpy(&data[3], public_exponent, public_exponent_len);
1617 memcpy(&data[3 + public_exponent_len], modulus, modulus_len);
1620 "Public exponent too big");
1621 free(public_exponent);
1625 rdf = ldns_rdf_new(LDNS_RDF_TYPE_B64, data_size, data);
1626 free(public_exponent);
1645 unsigned char *data = NULL;
1646 size_t data_size = 0;
1656 if (!session || !session->
module) {
1666 if (hsm_pkcs11_check_error(ctx, rv,
"C_GetAttributeValue")) {
1669 prime_len =
template[0].ulValueLen;
1670 subprime_len =
template[1].ulValueLen;
1671 base_len =
template[2].ulValueLen;
1672 value_len =
template[3].ulValueLen;
1674 prime =
template[0].pValue = malloc(prime_len);
1677 "Error allocating memory for prime");
1681 subprime =
template[1].pValue = malloc(subprime_len);
1684 "Error allocating memory for subprime");
1689 base =
template[2].pValue = malloc(base_len);
1692 "Error allocating memory for base");
1698 value =
template[3].pValue = malloc(value_len);
1701 "Error allocating memory for value");
1713 if (hsm_pkcs11_check_error(ctx, rv,
"get attribute value")) {
1721 data_size = prime_len + subprime_len + base_len + value_len + 1;
1722 data = malloc(data_size);
1725 "Error allocating memory for pub key rr data");
1732 data[0] = (prime_len - 64) / 8;
1733 memcpy(&data[1], subprime, subprime_len);
1734 memcpy(&data[1 + subprime_len], prime, prime_len);
1735 memcpy(&data[1 + subprime_len + prime_len], base, base_len);
1736 memcpy(&data[1 + subprime_len + prime_len + base_len], value, value_len);
1738 rdf = ldns_rdf_new(LDNS_RDF_TYPE_B64, data_size, data);
1760 if (!session || !session->
module) {
1770 if (hsm_pkcs11_check_error(ctx, rv,
"C_GetAttributeValue")) {
1773 value_len =
template[0].ulValueLen;
1775 value =
template[0].pValue = malloc(value_len);
1778 "Error allocating memory for value");
1787 if (hsm_pkcs11_check_error(ctx, rv,
"get attribute value")) {
1792 rdf = ldns_rdf_new(LDNS_RDF_TYPE_B64, value_len, value);
1801 unsigned char*
value = hsm_get_key_ecdsa_value(ctx, session, key, &value_len);
1803 if (value == NULL)
return NULL;
1805 ldns_rdf *rdf = ldns_rdf_new(LDNS_RDF_TYPE_B64, value_len, value);
1814 switch (hsm_get_key_algorithm(ctx, session, key)) {
1816 return hsm_get_key_rdata_rsa(ctx, session, key);
1819 return hsm_get_key_rdata_dsa(ctx, session, key);
1822 return hsm_get_key_rdata_gost(ctx, session, key);
1825 return hsm_get_key_rdata_ecdsa(ctx, session, key);
1836 hsm_create_prefix(
CK_ULONG digest_len,
1841 const CK_BYTE RSA_MD5_ID[] = { 0x30, 0x20, 0x30, 0x0C, 0x06, 0x08, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x02, 0x05, 0x05, 0x00, 0x04, 0x10 };
1842 const CK_BYTE RSA_SHA1_ID[] = { 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2B, 0x0E, 0x03, 0x02, 0x1A, 0x05, 0x00, 0x04, 0x14 };
1843 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 };
1844 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 };
1847 case LDNS_SIGN_RSAMD5:
1848 *data_size =
sizeof(RSA_MD5_ID) + digest_len;
1849 data = malloc(*data_size);
1850 memcpy(data, RSA_MD5_ID,
sizeof(RSA_MD5_ID));
1852 case LDNS_SIGN_RSASHA1:
1853 case LDNS_SIGN_RSASHA1_NSEC3:
1854 *data_size =
sizeof(RSA_SHA1_ID) + digest_len;
1855 data = malloc(*data_size);
1856 memcpy(data, RSA_SHA1_ID,
sizeof(RSA_SHA1_ID));
1858 case LDNS_SIGN_RSASHA256:
1859 *data_size =
sizeof(RSA_SHA256_ID) + digest_len;
1860 data = malloc(*data_size);
1861 memcpy(data, RSA_SHA256_ID,
sizeof(RSA_SHA256_ID));
1863 case LDNS_SIGN_RSASHA512:
1864 *data_size =
sizeof(RSA_SHA512_ID) + digest_len;
1865 data = malloc(*data_size);
1866 memcpy(data, RSA_SHA512_ID,
sizeof(RSA_SHA512_ID));
1869 case LDNS_SIGN_DSA_NSEC3:
1870 case LDNS_SIGN_ECC_GOST:
1872 #if !defined LDNS_BUILD_CONFIG_USE_ECDSA || LDNS_BUILD_CONFIG_USE_ECDSA 1873 case LDNS_SIGN_ECDSAP256SHA256:
1874 case LDNS_SIGN_ECDSAP384SHA384:
1876 *data_size = digest_len;
1877 data = malloc(*data_size);
1888 CK_MECHANISM_TYPE mechanism_type,
1890 ldns_buffer *sign_buf)
1896 digest_mechanism.pParameter = NULL;
1897 digest_mechanism.ulParameterLen = 0;
1898 digest_mechanism.
mechanism = mechanism_type;
1899 digest = malloc(digest_len);
1902 if (hsm_pkcs11_check_error(ctx, rv,
"HSM digest init")) {
1908 ldns_buffer_begin(sign_buf),
1909 ldns_buffer_position(sign_buf),
1912 if (hsm_pkcs11_check_error(ctx, rv,
"HSM digest")) {
1921 ldns_buffer *sign_buf,
1939 session = hsm_find_key_session(ctx, key);
1940 if (!session)
return NULL;
1945 switch (algorithm) {
1946 case LDNS_SIGN_RSAMD5:
1948 digest = hsm_digest_through_hsm(ctx, session,
1952 case LDNS_SIGN_RSASHA1:
1953 case LDNS_SIGN_RSASHA1_NSEC3:
1955 case LDNS_SIGN_DSA_NSEC3:
1956 digest_len = LDNS_SHA1_DIGEST_LENGTH;
1957 digest = malloc(digest_len);
1958 digest = ldns_sha1(ldns_buffer_begin(sign_buf),
1959 ldns_buffer_position(sign_buf),
1963 case LDNS_SIGN_RSASHA256:
1965 #if !defined LDNS_BUILD_CONFIG_USE_ECDSA || LDNS_BUILD_CONFIG_USE_ECDSA 1966 case LDNS_SIGN_ECDSAP256SHA256:
1968 digest_len = LDNS_SHA256_DIGEST_LENGTH;
1969 digest = malloc(digest_len);
1970 digest = ldns_sha256(ldns_buffer_begin(sign_buf),
1971 ldns_buffer_position(sign_buf),
1975 #if !defined LDNS_BUILD_CONFIG_USE_ECDSA || LDNS_BUILD_CONFIG_USE_ECDSA 1976 case LDNS_SIGN_ECDSAP384SHA384:
1977 digest_len = LDNS_SHA384_DIGEST_LENGTH;
1978 digest = malloc(digest_len);
1979 digest = ldns_sha384(ldns_buffer_begin(sign_buf),
1980 ldns_buffer_position(sign_buf),
1984 case LDNS_SIGN_RSASHA512:
1985 digest_len = LDNS_SHA512_DIGEST_LENGTH;
1986 digest = malloc(digest_len);
1987 digest = ldns_sha512(ldns_buffer_begin(sign_buf),
1988 ldns_buffer_position(sign_buf),
1991 case LDNS_SIGN_ECC_GOST:
1993 digest = hsm_digest_through_hsm(ctx, session,
2010 data = hsm_create_prefix(digest_len, algorithm, &data_len);
2011 memcpy(data + data_len - digest_len, digest, digest_len);
2013 sign_mechanism.pParameter = NULL;
2014 sign_mechanism.ulParameterLen = 0;
2016 case LDNS_SIGN_RSAMD5:
2017 case LDNS_SIGN_RSASHA1:
2018 case LDNS_SIGN_RSASHA1_NSEC3:
2019 case LDNS_SIGN_RSASHA256:
2020 case LDNS_SIGN_RSASHA512:
2024 case LDNS_SIGN_DSA_NSEC3:
2027 case LDNS_SIGN_ECC_GOST:
2031 #if !defined LDNS_BUILD_CONFIG_USE_ECDSA || LDNS_BUILD_CONFIG_USE_ECDSA 2032 case LDNS_SIGN_ECDSAP256SHA256:
2033 case LDNS_SIGN_ECDSAP384SHA384:
2049 if (hsm_pkcs11_check_error(ctx, rv,
"sign init")) {
2058 if (hsm_pkcs11_check_error(ctx, rv,
"sign final")) {
2064 sig_rdf = ldns_rdf_new_frm_data(LDNS_RDF_TYPE_B64,
2076 hsm_dname_is_wildcard(
const ldns_rdf* dname)
2078 return ( ldns_dname_label_count(dname) > 0 &&
2079 ldns_rdf_data(dname)[0] == 1 &&
2080 ldns_rdf_data(dname)[1] ==
'*');
2084 hsm_create_empty_rrsig(
const ldns_rr_list *rrset,
2089 uint32_t orig_class;
2091 uint8_t label_count;
2093 label_count = ldns_dname_label_count(
2094 ldns_rr_owner(ldns_rr_list_rr(rrset, 0)));
2096 if (hsm_dname_is_wildcard(ldns_rr_owner(ldns_rr_list_rr(rrset, 0)))) {
2100 rrsig = ldns_rr_new_frm_type(LDNS_RR_TYPE_RRSIG);
2103 orig_ttl = ldns_rr_ttl(ldns_rr_list_rr(rrset, 0));
2104 orig_class = ldns_rr_get_class(ldns_rr_list_rr(rrset, 0));
2106 ldns_rr_set_class(rrsig, orig_class);
2107 ldns_rr_set_ttl(rrsig, orig_ttl);
2108 ldns_rr_set_owner(rrsig,
2111 ldns_rr_list_rr(rrset,
2117 (void)ldns_rr_rrsig_set_origttl(
2119 ldns_native2rdf_int32(LDNS_RDF_TYPE_INT32,
2122 (void)ldns_rr_rrsig_set_signame(
2124 ldns_rdf_clone(sign_params->
owner));
2126 (void)ldns_rr_rrsig_set_labels(
2128 ldns_native2rdf_int8(LDNS_RDF_TYPE_INT8,
2133 (void)ldns_rr_rrsig_set_inception(
2135 ldns_native2rdf_int32(
2139 (void)ldns_rr_rrsig_set_inception(
2141 ldns_native2rdf_int32(LDNS_RDF_TYPE_TIME, now));
2144 (void)ldns_rr_rrsig_set_expiration(
2146 ldns_native2rdf_int32(
2150 (void)ldns_rr_rrsig_set_expiration(
2152 ldns_native2rdf_int32(
2154 now + LDNS_DEFAULT_EXP_TIME));
2157 (void)ldns_rr_rrsig_set_keytag(
2159 ldns_native2rdf_int16(LDNS_RDF_TYPE_INT16,
2162 (void)ldns_rr_rrsig_set_algorithm(
2164 ldns_native2rdf_int8(
2168 (void)ldns_rr_rrsig_set_typecovered(
2170 ldns_native2rdf_int16(
2172 ldns_rr_get_type(ldns_rr_list_rr(rrset,
2185 char *(pin_callback)(
unsigned int,
const char *,
unsigned int))
2189 char* module_pin = NULL;
2192 int repositories = 0;
2197 _hsm_ctx = hsm_ctx_new();
2201 hsm_config_default(&module_config);
2205 repo->
module, repo->
pin, &module_config);
2213 if (module_pin == NULL)
break;
2215 repo->
module, module_pin, &module_config);
2220 memset(module_pin, 0, strlen(module_pin));
2226 "No pin or callback function");
2237 if (result ==
HSM_OK && repositories == 0) {
2239 "No repositories found");
2250 hsm_ctx_close(_hsm_ctx, 1);
2260 newctx = hsm_ctx_clone(_hsm_ctx);
2272 CK_SESSION_HANDLE session_handle;
2280 if (session == NULL)
continue;
2286 if (hsm_pkcs11_check_error(ctx, rv,
"get session info")) {
2294 "Session not logged in");
2305 if (hsm_pkcs11_check_error(ctx, rv,
"test open session")) {
2310 if (hsm_pkcs11_check_error(ctx, rv,
"test close session")) {
2323 hsm_ctx_close(ctx, 0);
2338 params->
flags = LDNS_KEY_ZONE_KEY;
2342 params->
owner = NULL;
2350 if (params->
owner) ldns_rdf_deep_free(params->
owner);
2359 size_t key_count = 0;
2360 size_t cur_key_count;
2365 session_keys = hsm_list_keys_session(ctx, ctx->
session[i],
2367 keys = realloc(keys,
2369 for (j = 0; j < cur_key_count; j++) {
2370 keys[key_count + j] = session_keys[j];
2372 key_count += cur_key_count;
2384 const char *repository)
2388 if (!repository)
return NULL;
2390 session = hsm_find_repository_session(ctx, repository);
2395 return hsm_list_keys_session(ctx, session, count);
2401 unsigned char *id_bytes;
2405 id_bytes = hsm_hex_parse(
id, &len);
2407 if (!id_bytes)
return NULL;
2409 key = hsm_find_key_by_id_bin(ctx, id_bytes, len);
2415 generate_unique_id(
hsm_ctx_t *ctx,
unsigned char *buf,
size_t bufsize)
2420 while ((key = hsm_find_key_by_id_bin(ctx, buf, bufsize))) {
2429 const char *repository,
2430 unsigned long keysize)
2435 unsigned char id[16];
2439 CK_OBJECT_HANDLE publicKey, privateKey;
2440 CK_KEY_TYPE keyType =
CKK_RSA;
2444 CK_BYTE publicExponent[] = { 1, 0, 1 };
2449 session = hsm_find_repository_session(ctx, repository);
2450 if (!session)
return NULL;
2452 generate_unique_id(ctx,
id, 16);
2456 hsm_hex_unparse(id_str,
id, 16);
2468 {
CKA_WRAP, &cfalse,
sizeof(cfalse) },
2478 {
CKA_SIGN, &ctrue,
sizeof (ctrue) },
2489 publicKeyTemplate, 9,
2490 privateKeyTemplate, 10,
2493 if (hsm_pkcs11_check_error(ctx, rv,
"generate key pair")) {
2497 new_key = libhsm_key_new();
2515 const char *repository,
2516 unsigned long keysize)
2521 CK_OBJECT_HANDLE domainPar, publicKey, privateKey;
2526 unsigned char id[16];
2530 session = hsm_find_repository_session(ctx, repository);
2531 if (!session)
return NULL;
2533 generate_unique_id(ctx,
id, 16);
2537 hsm_hex_unparse(id_str,
id, 16);
2539 CK_KEY_TYPE keyType =
CKK_DSA;
2559 {
CKA_BASE, dsa_g,
sizeof(dsa_g) },
2565 {
CKA_WRAP, &cfalse,
sizeof(cfalse) },
2573 {
CKA_SIGN, &ctrue,
sizeof(ctrue) },
2588 if (hsm_pkcs11_check_error(ctx, rv,
"generate domain parameters")) {
2593 domainPar, publicKeyTemplate, 3);
2594 if (hsm_pkcs11_check_error(ctx, rv,
"get domain parameters")) {
2599 if (hsm_pkcs11_check_error(ctx, rv,
"destroy domain parameters")) {
2607 publicKeyTemplate, 10,
2608 privateKeyTemplate, 10,
2611 if (hsm_pkcs11_check_error(ctx, rv,
"generate key pair")) {
2615 new_key = libhsm_key_new();
2625 const char *repository)
2630 CK_OBJECT_HANDLE publicKey, privateKey;
2635 unsigned char id[16];
2639 session = hsm_find_repository_session(ctx, repository);
2640 if (!session)
return NULL;
2642 generate_unique_id(ctx,
id, 16);
2646 hsm_hex_unparse(id_str,
id, 16);
2653 CK_BYTE oid1[] = { 0x06, 0x07, 0x2A, 0x85, 0x03, 0x02, 0x02, 0x23, 0x01 };
2654 CK_BYTE oid2[] = { 0x06, 0x07, 0x2A, 0x85, 0x03, 0x02, 0x02, 0x1E, 0x01 };
2664 {
CKA_WRAP, &cfalse,
sizeof(cfalse) },
2672 {
CKA_SIGN, &ctrue,
sizeof(ctrue) },
2685 publicKeyTemplate, 9,
2686 privateKeyTemplate, 10,
2689 if (hsm_pkcs11_check_error(ctx, rv,
"generate key pair")) {
2693 new_key = libhsm_key_new();
2703 const char *repository,
2709 CK_OBJECT_HANDLE publicKey, privateKey;
2714 unsigned char id[16];
2718 session = hsm_find_repository_session(ctx, repository);
2719 if (!session)
return NULL;
2721 generate_unique_id(ctx,
id, 16);
2725 hsm_hex_unparse(id_str,
id, 16);
2727 CK_KEY_TYPE keyType =
CKK_EC;
2732 CK_BYTE oidP256[] = { 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07 };
2733 CK_BYTE oidP384[] = { 0x06, 0x05, 0x2B, 0x81, 0x04, 0x00, 0x22 };
2742 {
CKA_WRAP, &cfalse,
sizeof(cfalse) },
2750 {
CKA_SIGN, &ctrue,
sizeof(ctrue) },
2760 if (strcmp(curve,
"P-256") == 0)
2762 publicKeyTemplate[0].pValue = oidP256;
2763 publicKeyTemplate[0].ulValueLen =
sizeof(oidP256);
2765 else if (strcmp(curve,
"P-384") == 0)
2767 publicKeyTemplate[0].pValue = oidP384;
2768 publicKeyTemplate[0].ulValueLen =
sizeof(oidP384);
2779 publicKeyTemplate, 8,
2780 privateKeyTemplate, 10,
2783 if (hsm_pkcs11_check_error(ctx, rv,
"generate key pair")) {
2787 new_key = libhsm_key_new();
2800 if (!key)
return -1;
2802 session = hsm_find_key_session(ctx, key);
2803 if (!session)
return -2;
2807 if (hsm_pkcs11_check_error(ctx, rv,
"Destroy private key")) {
2815 if (hsm_pkcs11_check_error(ctx, rv,
"Destroy public key")) {
2828 for (i = 0; i < count; i++) {
2829 free((
void*)key_list[i]->modulename);
2843 if (!key)
return NULL;
2845 session = hsm_find_key_session(ctx, key);
2846 if (!session)
return NULL;
2848 id = hsm_get_id_for_object(ctx, session, key->
private_key, &len);
2849 if (!
id)
return NULL;
2852 id_str = malloc(len * 2 + 1);
2858 hsm_hex_unparse(id_str,
id, len);
2872 session = hsm_find_key_session(ctx, key);
2873 if (!session)
return NULL;
2878 if (key_info->
id == NULL) {
2879 key_info->
id = strdup(
"");
2882 key_info->
algorithm = (
unsigned long) hsm_get_key_algorithm(ctx,
2885 key_info->
keysize = (
unsigned long) hsm_get_key_size(ctx,
2929 const ldns_rr_list* rrset,
2934 ldns_buffer *sign_buf;
2938 if (!key)
return NULL;
2939 if (!sign_params)
return NULL;
2941 signature = hsm_create_empty_rrsig((ldns_rr_list *)rrset,
2947 sign_buf = ldns_buffer_new(LDNS_MAX_PACKETLEN);
2949 if (ldns_rrsig2buffer_wire(sign_buf, signature)
2950 != LDNS_STATUS_OK) {
2951 ldns_buffer_free(sign_buf);
2953 ldns_rr_free(signature);
2958 for(i = 0; i < ldns_rr_list_rr_count(rrset); i++) {
2959 ldns_rr2canonical(ldns_rr_list_rr(rrset, i));
2963 if (ldns_rr_list2buffer_wire(sign_buf, rrset)
2964 != LDNS_STATUS_OK) {
2965 ldns_buffer_free(sign_buf);
2966 ldns_rr_free(signature);
2970 b64_rdf = hsm_sign_buffer(ctx, sign_buf, key, sign_params->
algorithm);
2972 ldns_buffer_free(sign_buf);
2975 ldns_rr_free(signature);
2979 ldns_rr_rrsig_set_sig(signature, b64_rdf);
3007 sign_params->
owner = ldns_rdf_new_frm_str(LDNS_RDF_TYPE_DNAME,
"dummy");
3008 sign_params->
algorithm = (ldns_algorithm) alg;
3009 sign_params->
flags = LDNS_KEY_ZONE_KEY;
3011 sign_params->
flags |= LDNS_KEY_SEP_KEY;
3028 tag = ldns_calc_keytag(dnskey_rr);
3030 ldns_rr_free(dnskey_rr);
3058 session = hsm_find_key_session(ctx, key);
3059 if (!session)
return NULL;
3061 dnskey = ldns_rr_new();
3062 ldns_rr_set_type(dnskey, LDNS_RR_TYPE_DNSKEY);
3064 ldns_rr_set_owner(dnskey, ldns_rdf_clone(sign_params->
owner));
3066 ldns_rr_push_rdf(dnskey,
3067 ldns_native2rdf_int16(LDNS_RDF_TYPE_INT16,
3068 sign_params->
flags));
3069 ldns_rr_push_rdf(dnskey,
3070 ldns_native2rdf_int8(LDNS_RDF_TYPE_INT8,
3071 LDNS_DNSSEC_KEYPROTO));
3072 ldns_rr_push_rdf(dnskey,
3073 ldns_native2rdf_int8(LDNS_RDF_TYPE_ALG,
3076 rdata = hsm_get_key_rdata(ctx, session, key);
3077 if (rdata == NULL) {
3078 ldns_rr_free(dnskey);
3081 ldns_rr_push_rdf(dnskey, rdata);
3088 unsigned char *buffer,
3089 unsigned long length)
3094 if (!buffer)
return -1;
3118 unsigned char rnd_buf[4];
3121 memcpy(&rnd, rnd_buf, 4);
3133 unsigned char rnd_buf[8];
3136 memcpy(&rnd, rnd_buf, 8);
3149 const char *token_label,
3157 result = hsm_session_init(_hsm_ctx,
3165 result = hsm_ctx_add_session(_hsm_ctx, session);
3182 "hsm_token_attached()",
3183 "Can't find repository: %s", repository);
3204 if (message == NULL) {
3205 return strdup(
"libhsm memory allocation failed");
3221 printf(
"\t\tmodule at %p (sym %p)\n", (
void *) session->
module, (
void *) session->
module->
sym);
3222 printf(
"\t\tmodule path: %s\n", session->
module->
path);
3223 printf(
"\t\trepository name: %s\n", session->
module->
name);
3225 printf(
"\t\tsess handle: %u\n", (
unsigned int) session->
session);
3231 printf(
"CTX Sessions: %lu\n",
3234 printf(
"\tSession at %p\n", (
void *) ctx->
session[i]);
3246 printf(
"\tprivkey handle: %u\n", (
unsigned int) key->
private_key);
3248 printf(
"\tpubkey handle: %u\n", (
unsigned int) key->
public_key);
3250 printf(
"\tpubkey handle: %s\n",
"NULL");
3252 printf(
"\trepository: %s\n", key->
modulename);
3254 printf(
"\tsize: %lu\n", key_info->
keysize);
3255 printf(
"\tid: %s\n", key_info->
id);
3258 printf(
"key: hsm_get_key_info() returned NULL\n");
3261 printf(
"key: <void>\n");
3273 fprintf(stderr,
"%s\n", message);
3276 fprintf(stderr,
"Unknown error\n");
3293 result = hsm_get_slot_id(ctx,
3297 if (result !=
HSM_OK)
return;
3300 if (hsm_pkcs11_check_error(ctx, rv,
"C_GetTokenInfo")) {
3304 printf(
"Repository: %s\n",session->
module->
name);
3306 printf(
"\tModule: %s\n", session->
module->
path);
3307 printf(
"\tSlot: %lu\n", slot_id);
3308 printf(
"\tToken Label: %.*s\n",
3309 (
int)
sizeof(token_info.
label), token_info.
label);
3310 printf(
"\tManufacturer: %.*s\n",
3311 (
int)
sizeof(token_info.manufacturerID), token_info.manufacturerID);
3312 printf(
"\tModel: %.*s\n",
3313 (
int)
sizeof(token_info.
model), token_info.
model);
3314 printf(
"\tSerial: %.*s\n",
3315 (
int)
sizeof(token_info.serialNumber), token_info.serialNumber);
3323 keycache_cmpfunc(
const void* a,
const void* b)
3325 const char* x = (
const char*)a;
3326 const char* y = (
const char*)b;
3327 return strcmp(x, y);
3331 keycache_delfunc(ldns_rbnode_t* node,
void* cargo)
3334 free((
void*)node->key);
3335 free((
void*)node->data);
3342 ctx->
keycache = ldns_rbtree_create(keycache_cmpfunc);
3348 ldns_traverse_postorder(ctx->
keycache, keycache_delfunc, NULL);
3355 ldns_rbnode_t* node;
3357 node = ldns_rbtree_search(ctx->
keycache, locator);
3358 if (node == LDNS_RBTREE_NULL || node == NULL) {
3363 node = malloc(
sizeof(ldns_rbnode_t));
3364 node->key = strdup(locator);
3366 node = ldns_rbtree_insert(ctx->
keycache, node);
3370 if (node == LDNS_RBTREE_NULL || node == NULL)
#define CKR_SIGNATURE_INVALID
#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_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
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)
#define HSM_MAX_SIGNATURE_LENGTH
void hsm_sign_params_free(hsm_sign_params_t *params)
#define CKR_SESSION_READ_ONLY
#define CKA_GOSTR3410PARAMS
#define CKM_EC_KEY_PAIR_GEN
#define CKR_WRAPPING_KEY_HANDLE_INVALID
#define CKR_USER_NOT_LOGGED_IN
uint32_t hsm_random32(hsm_ctx_t *ctx)
#define CKR_ATTRIBUTE_SENSITIVE
#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
int hsm_keytag(const char *loc, int alg, int ksk, uint16_t *keytag)
#define CKR_INFORMATION_SENSITIVE
libhsm_key_t * hsm_find_key_by_id(hsm_ctx_t *ctx, const char *id)
#define CKR_GENERAL_ERROR
unsigned long int CK_ULONG
#define CKA_PUBLIC_EXPONENT
libhsm_key_t * hsm_generate_rsa_key(hsm_ctx_t *ctx, const char *repository, unsigned long keysize)
void keycache_destroy(hsm_ctx_t *ctx)
#define HSM_TOKEN_LABEL_LENGTH
hsm_repository_t * hsm_repository_new(char *name, char *module, char *tokenlabel, char *pin, uint8_t use_pubkey, uint8_t require_backup)
void hsm_destroy_context(hsm_ctx_t *ctx)
ck_mechanism_type_t mechanism
#define CKR_PIN_INCORRECT
void hsm_print_tokeninfo(hsm_ctx_t *ctx)
#define CKR_FUNCTION_FAILED
#define CKR_VENDOR_DEFINED
#define CKR_OPERATION_ACTIVE
#define CKR_SLOT_ID_INVALID
libhsm_key_t ** hsm_list_keys_repository(hsm_ctx_t *ctx, size_t *count, const char *repository)
ldns_rr * hsm_get_dnskey(hsm_ctx_t *ctx, const libhsm_key_t *key, const hsm_sign_params_t *sign_params)
#define CKA_GOSTR3411PARAMS
#define CKR_UNWRAPPING_KEY_HANDLE_INVALID
#define CKR_ENCRYPTED_DATA_LEN_RANGE
#define HSM_MODULE_NOT_FOUND
libhsm_key_t * hsm_generate_ecdsa_key(hsm_ctx_t *ctx, const char *repository, const char *curve)
#define CKR_MECHANISM_INVALID
#define CKR_FUNCTION_CANCELED
int hsm_open2(hsm_repository_t *rlist, char *(pin_callback)(unsigned int, const char *, unsigned int))
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
void libhsm_key_info_free(libhsm_key_info_t *key_info)
#define CKR_FUNCTION_NOT_PARALLEL
void keycache_create(hsm_ctx_t *ctx)
void hsm_print_key(hsm_ctx_t *ctx, libhsm_key_t *key)
#define CKR_SIGNATURE_LEN_RANGE
pthread_mutex_t _hsm_ctx_mutex
libhsm_key_t * hsm_generate_gost_key(hsm_ctx_t *ctx, const char *repository)
libhsm_key_t ** hsm_list_keys(hsm_ctx_t *ctx, size_t *count)
#define CKR_CRYPTOKI_NOT_INITIALIZED
const libhsm_key_t * keycache_lookup(hsm_ctx_t *ctx, const char *locator)
#define CKR_ENCRYPTED_DATA_INVALID
#define HSM_PIN_INCORRECT
#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
int hsm_remove_key(hsm_ctx_t *ctx, libhsm_key_t *key)
#define CKR_OBJECT_HANDLE_INVALID
char * hsm_get_key_id(hsm_ctx_t *ctx, const libhsm_key_t *key)
hsm_session_t * session[HSM_MAX_SESSIONS]
unsigned char CK_UTF8CHAR
void hsm_repository_free(hsm_repository_t *r)
uint64_t hsm_random64(hsm_ctx_t *ctx)
libhsm_key_t * hsm_generate_dsa_key(hsm_ctx_t *ctx, const char *repository, unsigned long keysize)
#define CKS_RW_USER_FUNCTIONS
#define CKR_OPERATION_NOT_INITIALIZED
#define CKR_BUFFER_TOO_SMALL
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
struct ck_function_list * CK_FUNCTION_LIST_PTR
void hsm_print_ctx(hsm_ctx_t *ctx)
#define CKR_UNWRAPPING_KEY_TYPE_INCONSISTENT
#define CKR_FUNCTION_NOT_SUPPORTED
#define CKR_USER_TYPE_INVALID
#define CKR_DEVICE_REMOVED
unsigned long private_key
void libhsm_key_list_free(libhsm_key_t **key_list, size_t count)
#define CKR_TOKEN_NOT_PRESENT
#define CKR_WRAPPED_KEY_INVALID
libhsm_key_info_t * hsm_get_key_info(hsm_ctx_t *ctx, const libhsm_key_t *key)
#define CKR_USER_ALREADY_LOGGED_IN
void hsm_print_error(hsm_ctx_t *gctx)
#define CKR_MECHANISM_PARAM_INVALID
#define CKR_DATA_LEN_RANGE
#define CKR_SESSION_HANDLE_INVALID