Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
util_pkcs11.h File Reference
#include <yubihsm.h>
#include <pkcs11y.h>
#include "yubihsm_pkcs11.h"
Include dependency graph for util_pkcs11.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

CK_RV get_mechanism_list (yubihsm_pkcs11_slot *slot, CK_MECHANISM_TYPE_PTR pMechanismList, CK_ULONG_PTR count)
 
bool get_mechanism_info (yubihsm_pkcs11_slot *slot, CK_MECHANISM_TYPE type, CK_MECHANISM_INFO_PTR pInfo)
 
bool parse_hex (CK_UTF8CHAR_PTR hex, CK_ULONG hex_len, uint8_t *parsed)
 
void destroy_session (yubihsm_pkcs11_context *ctx, CK_SESSION_HANDLE hSession)
 
CK_RV get_attribute (CK_ATTRIBUTE_TYPE type, yh_object_descriptor *object, CK_VOID_PTR value, CK_ULONG_PTR length, yh_session *session)
 
CK_RV get_attribute_ecsession_key (CK_ATTRIBUTE_TYPE type, ecdh_session_key *key, CK_VOID_PTR value, CK_ULONG_PTR length)
 
yubihsm_pkcs11_object_descget_object_desc (yh_session *session, yubihsm_pkcs11_object_desc *objects, CK_OBJECT_HANDLE objectHandle)
 
void delete_object_from_cache (yubihsm_pkcs11_object_desc *objects, CK_OBJECT_HANDLE objHandle)
 
bool check_sign_mechanism (yubihsm_pkcs11_slot *slot, CK_MECHANISM_PTR pMechanism)
 
CK_RV apply_sign_mechanism_init (yubihsm_pkcs11_op_info *op_info)
 
CK_RV apply_sign_mechanism_update (yubihsm_pkcs11_op_info *op_info, CK_BYTE_PTR in, CK_ULONG in_len)
 
CK_RV apply_sign_mechanism_finalize (yubihsm_pkcs11_op_info *op_info)
 
CK_RV perform_signature (yh_session *session, yubihsm_pkcs11_op_info *op_info, uint8_t *signature, uint16_t *signature_len)
 
bool sign_mechanism_cleanup (yubihsm_pkcs11_op_info *op_info)
 
bool check_verify_mechanism (yubihsm_pkcs11_slot *slot, CK_MECHANISM_PTR pMechanism)
 
CK_RV apply_verify_mechanism_init (yubihsm_pkcs11_op_info *op_info)
 
CK_RV apply_verify_mechanism_update (yubihsm_pkcs11_op_info *op_info, CK_BYTE_PTR in, CK_ULONG in_len)
 
CK_RV apply_verify_mechanism_finalize (yubihsm_pkcs11_op_info *op_info)
 
CK_RV perform_verify (yh_session *session, yubihsm_pkcs11_op_info *op_info, uint8_t *signature, uint16_t signature_len)
 
bool verify_mechanism_cleanup (yubihsm_pkcs11_op_info *op_info)
 
bool check_decrypt_mechanism (yubihsm_pkcs11_slot *slot, CK_MECHANISM_PTR pMechanism)
 
CK_RV apply_decrypt_mechanism_init (yubihsm_pkcs11_op_info *op_info)
 
CK_RV apply_decrypt_mechanism_update (yubihsm_pkcs11_op_info *op_info, CK_BYTE_PTR in, CK_ULONG in_len)
 
CK_RV apply_decrypt_mechanism_finalize (yubihsm_pkcs11_op_info *op_info)
 
CK_RV perform_encrypt (yh_session *session, yubihsm_pkcs11_op_info *op_info, uint8_t *plaintext, uint16_t *plaintext_len)
 
bool decrypt_mechanism_cleanup (yubihsm_pkcs11_op_info *op_info)
 
bool check_encrypt_mechanism (yubihsm_pkcs11_slot *slot, CK_MECHANISM_PTR pMechanism)
 
CK_RV apply_encrypt_mechanism_update (yubihsm_pkcs11_op_info *op_info, CK_BYTE_PTR in, CK_ULONG in_len)
 
CK_RV perform_decrypt (yh_session *session, yubihsm_pkcs11_op_info *op_info, uint8_t *ciphertext, uint16_t *ciphertext_len)
 
bool check_digest_mechanism (CK_MECHANISM_PTR pMechanism)
 
CK_RV apply_digest_mechanism_init (yubihsm_pkcs11_op_info *op_info)
 
CK_RV apply_digest_mechanism_update (yubihsm_pkcs11_op_info *op_info, CK_BYTE_PTR in, CK_ULONG in_len)
 
CK_RV apply_digest_mechanism_finalize (yubihsm_pkcs11_op_info *op_info)
 
CK_RV perform_digest (yubihsm_pkcs11_op_info *op_info, uint8_t *digest, uint16_t *digest_len)
 
bool digest_mechanism_cleanup (yubihsm_pkcs11_op_info *op_info)
 
CK_ULONG get_digest_bytelength (CK_MECHANISM_TYPE m)
 
bool check_wrap_mechanism (yubihsm_pkcs11_slot *slot, CK_MECHANISM_PTR pMechanism)
 
bool is_RSA_sign_mechanism (CK_MECHANISM_TYPE m)
 
bool is_RSA_decrypt_mechanism (CK_MECHANISM_TYPE m)
 
bool is_hashed_mechanism (CK_MECHANISM_TYPE m)
 
bool is_PKCS1v1_5_sign_mechanism (CK_MECHANISM_TYPE m)
 
bool is_ECDSA_sign_mechanism (CK_MECHANISM_TYPE m)
 
bool is_PSS_sign_mechanism (CK_MECHANISM_TYPE m)
 
bool is_HMAC_sign_mechanism (CK_MECHANISM_TYPE m)
 
void set_native_locking (yubihsm_pkcs11_context *ctx)
 
bool add_connectors (yubihsm_pkcs11_context *ctx, int n_connectors, char **connector_names, yh_connector **connectors)
 
bool delete_session (yubihsm_pkcs11_context *ctx, CK_SESSION_HANDLE_PTR phSession)
 
CK_RV get_session (yubihsm_pkcs11_context *ctx, CK_SESSION_HANDLE hSession, yubihsm_pkcs11_session **session, int session_state)
 
yubihsm_pkcs11_slotget_slot (yubihsm_pkcs11_context *ctx, CK_ULONG id)
 
void release_slot (yubihsm_pkcs11_context *ctx, yubihsm_pkcs11_slot *slot)
 
bool create_session (yubihsm_pkcs11_slot *slot, CK_FLAGS flags, CK_SESSION_HANDLE_PTR phSession)
 
void release_session (yubihsm_pkcs11_context *ctx, yubihsm_pkcs11_session *session)
 
CK_RV set_template_attribute (yubihsm_pkcs11_attribute *attribute, void *value)
 
CK_RV parse_rsa_template (CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount, yubihsm_pkcs11_object_template *template)
 
CK_RV parse_ec_template (CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount, yubihsm_pkcs11_object_template *template)
 
CK_RV parse_hmac_template (CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount, yubihsm_pkcs11_object_template *template, bool generate)
 
CK_RV parse_wrap_template (CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount, yubihsm_pkcs11_object_template *template, bool generate)
 
CK_RV parse_rsa_generate_template (CK_ATTRIBUTE_PTR pPublicKeyTemplate, CK_ULONG ulPublicKeyAttributeCount, CK_ATTRIBUTE_PTR pPrivateKeyTemplate, CK_ULONG ulPrivateKeyAttributeCount, yubihsm_pkcs11_object_template *template)
 
CK_RV parse_ec_generate_template (CK_ATTRIBUTE_PTR pPublicKeyTemplate, CK_ULONG ulPublicKeyAttributeCount, CK_ATTRIBUTE_PTR pPrivateKeyTemplate, CK_ULONG ulPrivateKeyAttributeCount, yubihsm_pkcs11_object_template *template)
 
int parse_id_value (void *value, CK_ULONG len)
 
CK_RV populate_template (int type, void *object, CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount, yh_session *session)
 
CK_RV validate_derive_key_attribute (CK_ATTRIBUTE_TYPE type, void *value)
 

Function Documentation

◆ add_connectors()

bool add_connectors ( yubihsm_pkcs11_context * ctx,
int n_connectors,
char ** connector_names,
yh_connector ** connectors )

Definition at line 2924 of file util_pkcs11.c.

2925 {
2926 if (ctx->slots.head == NULL) {
2927 list_create(&ctx->slots, sizeof(yubihsm_pkcs11_slot), free_pkcs11_slot);
2928 }
2929
2930 for (int i = 0; i < n_connectors; i++) {
2932 memset(&slot, 0, sizeof(yubihsm_pkcs11_slot));
2933 slot.id = i;
2934 slot.connector_name = strdup(connector_names[i]);
2935 slot.max_session_id = 1;
2936 if (!slot.connector_name) {
2937 return false;
2938 }
2939 slot.connector = connectors[i];
2940 if (ctx->create_mutex != NULL) {
2941 if (ctx->create_mutex(&slot.mutex) != CKR_OK) {
2942 return false;
2943 }
2944 }
2946 if (list_append(&ctx->slots, (void *) &slot) != true) {
2947 return false;
2948 }
2949 }
2950 return true;
2951}
bool list_append(List *list, void *item)
Definition list.c:78
void list_create(List *list, int item_size, FreeItemFn free_item_fn)
Definition list.c:24
#define CKR_OK
Definition pkcs11.h:1092
ListItem * head
Definition list.h:38
CK_CREATEMUTEX create_mutex
yh_connector * connector
yubihsm_pkcs11_slot * slot
memset(pInfo->slotDescription, ' ', 64)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ apply_decrypt_mechanism_finalize()

CK_RV apply_decrypt_mechanism_finalize ( yubihsm_pkcs11_op_info * op_info)

◆ apply_decrypt_mechanism_init()

CK_RV apply_decrypt_mechanism_init ( yubihsm_pkcs11_op_info * op_info)

Definition at line 1884 of file util_pkcs11.c.

1884 {
1885
1886 op_info->buffer_length = 0;
1887 op_info->op.decrypt.finalized = false;
1888
1889 switch (op_info->mechanism.mechanism) {
1890 case CKM_RSA_PKCS:
1891 case CKM_RSA_PKCS_OAEP:
1893 return CKR_OK;
1894 default:
1895 DBG_ERR("Mechanism %lu not supported", op_info->mechanism.mechanism);
1896 return CKR_MECHANISM_INVALID;
1897 }
1898
1899 return CKR_OK;
1900}
#define DBG_ERR(...)
Definition debug_lib.h:76
#define CKR_MECHANISM_INVALID
Definition pkcs11.h:1126
#define CKM_RSA_PKCS
Definition pkcs11.h:470
#define CKM_RSA_PKCS_OAEP
Definition pkcs11.h:478
#define CKM_YUBICO_AES_CCM_WRAP
Definition pkcs11y.h:33
CK_MECHANISM_TYPE mechanism
decrypt_info decrypt
Here is the call graph for this function:

◆ apply_decrypt_mechanism_update()

CK_RV apply_decrypt_mechanism_update ( yubihsm_pkcs11_op_info * op_info,
CK_BYTE_PTR in,
CK_ULONG in_len )

Definition at line 2053 of file util_pkcs11.c.

2054 {
2055
2056 switch (op_info->mechanism.mechanism) {
2057 case CKM_RSA_PKCS:
2058 case CKM_RSA_PKCS_OAEP:
2060 if (op_info->buffer_length + in_len > sizeof(op_info->buffer)) {
2061 return CKR_DATA_LEN_RANGE;
2062 }
2063
2064 memcpy(op_info->buffer + op_info->buffer_length, in, in_len);
2065 op_info->buffer_length += in_len;
2066 break;
2067
2068 default:
2069 return CKR_FUNCTION_FAILED;
2070 }
2071
2072 return CKR_OK;
2073}
#define CKR_FUNCTION_FAILED
Definition pkcs11.h:1097
#define CKR_DATA_LEN_RANGE
Definition pkcs11.h:1107
uint8_t buffer[YUBIHSM_PKCS11_OP_BUFSIZE]
memcpy((char *) pInfo->slotDescription, s, l)
size_t in_len
Here is the call graph for this function:
Here is the caller graph for this function:

◆ apply_digest_mechanism_finalize()

CK_RV apply_digest_mechanism_finalize ( yubihsm_pkcs11_op_info * op_info)

Definition at line 2163 of file util_pkcs11.c.

2163 {
2164
2165 int ret;
2166 ret = EVP_DigestFinal_ex(op_info->op.digest.md_ctx, op_info->buffer,
2167 &op_info->buffer_length);
2168
2169 EVP_MD_CTX_destroy(op_info->op.digest.md_ctx);
2170 op_info->op.digest.md_ctx = NULL;
2171
2172 if (ret != 1) {
2173 return CKR_FUNCTION_FAILED;
2174 }
2175
2176 return CKR_OK;
2177}
EVP_MD_CTX * md_ctx
digest_info digest
CK_RV ret

◆ apply_digest_mechanism_init()

CK_RV apply_digest_mechanism_init ( yubihsm_pkcs11_op_info * op_info)

Definition at line 1902 of file util_pkcs11.c.

1902 {
1903
1904 const EVP_MD *md = NULL;
1905
1906 op_info->buffer_length = 0;
1907
1908 switch (op_info->mechanism.mechanism) {
1909 case CKM_SHA_1:
1910 md = EVP_sha1();
1911 break;
1912
1913 case CKM_SHA256:
1914 md = EVP_sha256();
1915 break;
1916
1917 case CKM_SHA384:
1918 md = EVP_sha384();
1919 break;
1920
1921 case CKM_SHA512:
1922 md = EVP_sha512();
1923 break;
1924
1925 default:
1926 DBG_ERR("Mechanism %lu not supported", op_info->mechanism.mechanism);
1927 return CKR_MECHANISM_INVALID;
1928 }
1929
1930 op_info->op.digest.md_ctx = EVP_MD_CTX_create();
1931
1932 op_info->op.digest.is_multipart = false;
1933
1934 if (EVP_DigestInit_ex(op_info->op.digest.md_ctx, md, NULL) == 0) {
1935 EVP_MD_CTX_destroy(op_info->op.digest.md_ctx);
1936 op_info->op.digest.md_ctx = NULL;
1937 return CKR_FUNCTION_FAILED;
1938 }
1939
1940 return CKR_OK;
1941}
#define CKM_SHA256
Definition pkcs11.h:541
#define CKM_SHA384
Definition pkcs11.h:544
#define CKM_SHA512
Definition pkcs11.h:547
#define CKM_SHA_1
Definition pkcs11.h:532
Here is the call graph for this function:

◆ apply_digest_mechanism_update()

CK_RV apply_digest_mechanism_update ( yubihsm_pkcs11_op_info * op_info,
CK_BYTE_PTR in,
CK_ULONG in_len )

Definition at line 2095 of file util_pkcs11.c.

2096 {
2097
2098 switch (op_info->mechanism.mechanism) {
2099 case CKM_SHA_1:
2100 case CKM_SHA256:
2101 case CKM_SHA384:
2102 case CKM_SHA512:
2103 if (EVP_DigestUpdate(op_info->op.digest.md_ctx, in, in_len) != 1) {
2104 EVP_MD_CTX_destroy(op_info->op.digest.md_ctx);
2105 op_info->op.digest.md_ctx = NULL;
2106 return CKR_FUNCTION_FAILED;
2107 }
2108 break;
2109
2110 default:
2111 return CKR_FUNCTION_FAILED;
2112 }
2113
2114 return CKR_OK;
2115}
Here is the call graph for this function:

◆ apply_encrypt_mechanism_update()

CK_RV apply_encrypt_mechanism_update ( yubihsm_pkcs11_op_info * op_info,
CK_BYTE_PTR in,
CK_ULONG in_len )

Definition at line 2075 of file util_pkcs11.c.

2076 {
2077
2078 switch (op_info->mechanism.mechanism) {
2080 if (op_info->buffer_length + in_len > sizeof(op_info->buffer)) {
2081 return CKR_DATA_LEN_RANGE;
2082 }
2083
2084 memcpy(op_info->buffer + op_info->buffer_length, in, in_len);
2085 op_info->buffer_length += in_len;
2086 break;
2087
2088 default:
2089 return CKR_FUNCTION_FAILED;
2090 }
2091
2092 return CKR_OK;
2093}
Here is the call graph for this function:

◆ apply_sign_mechanism_finalize()

CK_RV apply_sign_mechanism_finalize ( yubihsm_pkcs11_op_info * op_info)

Definition at line 2117 of file util_pkcs11.c.

2117 {
2118
2119 if (is_hashed_mechanism(op_info->mechanism.mechanism)) {
2120 int ret;
2121 ret = EVP_DigestFinal_ex(op_info->op.sign.md_ctx, op_info->buffer,
2122 &op_info->buffer_length);
2123
2124 EVP_MD_CTX_destroy(op_info->op.sign.md_ctx);
2125 op_info->op.sign.md_ctx = NULL;
2126
2127 if (ret != 1) {
2128 return CKR_FUNCTION_FAILED;
2129 }
2130 }
2131
2133 if (op_info->buffer_length < op_info->op.sign.sig_len / 2) {
2134 uint16_t padding =
2135 (op_info->op.sign.sig_len / 2) - op_info->buffer_length;
2136 memmove(op_info->buffer + padding, op_info->buffer,
2137 op_info->buffer_length);
2138 memset(op_info->buffer, 0, padding);
2139 op_info->buffer_length += padding;
2140 } else if (op_info->buffer_length > op_info->op.sign.sig_len / 2) {
2141 op_info->buffer_length = op_info->op.sign.sig_len / 2;
2142 }
2143 }
2144
2145 // TODO(adma): check if more steps are need for PSS or ECDSA
2146
2147 return CKR_OK;
2148}
unsigned short uint16_t
Definition stdint.h:125
EVP_MD_CTX * md_ctx
uint16_t sig_len
sign_info sign
bool is_ECDSA_sign_mechanism(CK_MECHANISM_TYPE m)
bool is_hashed_mechanism(CK_MECHANISM_TYPE m)
Here is the call graph for this function:

◆ apply_sign_mechanism_init()

CK_RV apply_sign_mechanism_init ( yubihsm_pkcs11_op_info * op_info)

Definition at line 1763 of file util_pkcs11.c.

1763 {
1764
1765 const EVP_MD *md = NULL;
1766
1767 op_info->buffer_length = 0;
1768
1769 switch (op_info->mechanism.mechanism) {
1770 case CKM_RSA_PKCS:
1771 case CKM_RSA_PKCS_PSS:
1772 case CKM_ECDSA:
1773 case CKM_SHA_1_HMAC:
1774 case CKM_SHA256_HMAC:
1775 case CKM_SHA384_HMAC:
1776 case CKM_SHA512_HMAC:
1777 // NOTE(adma): no hash required for these mechanisms
1778 op_info->op.sign.md_ctx = NULL;
1779 return CKR_OK;
1780
1781 case CKM_SHA1_RSA_PKCS:
1783 case CKM_ECDSA_SHA1:
1784 md = EVP_sha1();
1785 break;
1786
1789 case CKM_ECDSA_SHA256:
1790 md = EVP_sha256();
1791 break;
1792
1795 case CKM_ECDSA_SHA384:
1796 md = EVP_sha384();
1797 break;
1798
1801 case CKM_ECDSA_SHA512:
1802 md = EVP_sha512();
1803 break;
1804
1805 default:
1806 DBG_ERR("Mechanism %lu not supported", op_info->mechanism.mechanism);
1807 return CKR_MECHANISM_INVALID;
1808 }
1809
1810 op_info->op.sign.md_ctx = EVP_MD_CTX_create();
1811
1812 if (EVP_DigestInit_ex(op_info->op.sign.md_ctx, md, NULL) == 0) {
1813 EVP_MD_CTX_destroy(op_info->op.sign.md_ctx);
1814 op_info->op.sign.md_ctx = NULL;
1815 return CKR_FUNCTION_FAILED;
1816 }
1817
1818 return CKR_OK;
1819}
#define CKM_ECDSA_SHA1
Definition pkcs11.h:647
#define CKM_SHA_1_HMAC
Definition pkcs11.h:533
#define CKM_SHA512_RSA_PKCS_PSS
Definition pkcs11.h:498
#define CKM_ECDSA_SHA512
Definition pkcs11.h:651
#define CKM_SHA512_RSA_PKCS
Definition pkcs11.h:495
#define CKM_SHA256_HMAC
Definition pkcs11.h:542
#define CKM_SHA384_HMAC
Definition pkcs11.h:545
#define CKM_SHA1_RSA_PKCS
Definition pkcs11.h:475
#define CKM_ECDSA
Definition pkcs11.h:646
#define CKM_ECDSA_SHA384
Definition pkcs11.h:650
#define CKM_ECDSA_SHA256
Definition pkcs11.h:649
#define CKM_SHA256_RSA_PKCS_PSS
Definition pkcs11.h:496
#define CKM_RSA_PKCS_PSS
Definition pkcs11.h:482
#define CKM_SHA1_RSA_PKCS_PSS
Definition pkcs11.h:483
#define CKM_SHA384_RSA_PKCS
Definition pkcs11.h:494
#define CKM_SHA512_HMAC
Definition pkcs11.h:548
#define CKM_SHA256_RSA_PKCS
Definition pkcs11.h:493
#define CKM_SHA384_RSA_PKCS_PSS
Definition pkcs11.h:497
Here is the call graph for this function:

◆ apply_sign_mechanism_update()

CK_RV apply_sign_mechanism_update ( yubihsm_pkcs11_op_info * op_info,
CK_BYTE_PTR in,
CK_ULONG in_len )

Definition at line 1943 of file util_pkcs11.c.

1944 {
1945
1946 switch (op_info->mechanism.mechanism) {
1947 case CKM_RSA_PKCS:
1948 // NOTE(adma): Specs say there should be enough space for PKCS#1 padding
1949 if (op_info->buffer_length + in_len >
1950 (op_info->op.sign.key_len + 7) / 8 - 11) {
1951 return CKR_DATA_LEN_RANGE;
1952 }
1953
1954 memcpy(op_info->buffer + op_info->buffer_length, in, in_len);
1955 op_info->buffer_length += in_len;
1956 break;
1957
1958 case CKM_ECDSA:
1959 if (op_info->buffer_length + in_len > 128) {
1960 // NOTE(adma): Specs say ECDSA only supports data up to 1024 bit
1961 return CKR_DATA_LEN_RANGE;
1962 }
1963
1964 memcpy(op_info->buffer + op_info->buffer_length, in, in_len);
1965 op_info->buffer_length += in_len;
1966 break;
1967
1968 case CKM_RSA_PKCS_PSS:
1969 case CKM_SHA_1_HMAC:
1970 case CKM_SHA256_HMAC:
1971 case CKM_SHA384_HMAC:
1972 case CKM_SHA512_HMAC:
1973 if (op_info->buffer_length + in_len > sizeof(op_info->buffer)) {
1974 return CKR_DATA_LEN_RANGE;
1975 }
1976
1977 memcpy(op_info->buffer + op_info->buffer_length, in, in_len);
1978 op_info->buffer_length += in_len;
1979 break;
1980
1981 case CKM_SHA1_RSA_PKCS:
1989 case CKM_ECDSA_SHA1:
1990 case CKM_ECDSA_SHA256:
1991 case CKM_ECDSA_SHA384:
1992 case CKM_ECDSA_SHA512:
1993 if (EVP_DigestUpdate(op_info->op.sign.md_ctx, in, in_len) != 1) {
1994 EVP_MD_CTX_destroy(op_info->op.sign.md_ctx);
1995 op_info->op.sign.md_ctx = NULL;
1996 return CKR_FUNCTION_FAILED;
1997 }
1998 break;
1999
2000 default:
2001 return CKR_FUNCTION_FAILED;
2002 }
2003
2004 return CKR_OK;
2005}
CK_ULONG key_len
Here is the call graph for this function:

◆ apply_verify_mechanism_finalize()

CK_RV apply_verify_mechanism_finalize ( yubihsm_pkcs11_op_info * op_info)

◆ apply_verify_mechanism_init()

CK_RV apply_verify_mechanism_init ( yubihsm_pkcs11_op_info * op_info)

Definition at line 1821 of file util_pkcs11.c.

1821 {
1822
1823 const EVP_MD *md = NULL;
1824
1825 op_info->buffer_length = 0;
1826 op_info->op.verify.padding = 0;
1827 op_info->op.verify.saltLen = 0;
1828 op_info->op.verify.mgf1md = NULL;
1829 op_info->op.verify.md = NULL;
1830 op_info->op.verify.md_ctx = NULL;
1831
1832 switch (op_info->mechanism.mechanism) {
1833 case CKM_RSA_PKCS:
1834 case CKM_RSA_PKCS_PSS:
1835 case CKM_ECDSA:
1836 case CKM_SHA_1_HMAC:
1837 case CKM_SHA256_HMAC:
1838 case CKM_SHA384_HMAC:
1839 case CKM_SHA512_HMAC:
1840 // NOTE(adma): no hash required for these mechanisms
1841 return CKR_OK;
1842
1843 case CKM_SHA1_RSA_PKCS:
1845 case CKM_ECDSA_SHA1:
1846 md = EVP_sha1();
1847 break;
1848
1851 case CKM_ECDSA_SHA256:
1852 md = EVP_sha256();
1853 break;
1854
1857 case CKM_ECDSA_SHA384:
1858 md = EVP_sha384();
1859 break;
1860
1863 case CKM_ECDSA_SHA512:
1864 md = EVP_sha512();
1865 break;
1866
1867 default:
1868 DBG_ERR("Mechanism %lu not supported", op_info->mechanism.mechanism);
1869 return CKR_MECHANISM_INVALID;
1870 }
1871
1872 op_info->op.verify.md = md;
1873 op_info->op.verify.md_ctx = EVP_MD_CTX_create();
1874 if (op_info->op.verify.md_ctx == NULL) {
1875 return CKR_FUNCTION_FAILED;
1876 }
1877 if (EVP_DigestInit(op_info->op.verify.md_ctx, md) == 0) {
1878 return CKR_FUNCTION_FAILED;
1879 }
1880
1881 return CKR_OK;
1882}
unsigned long saltLen
EVP_MD_CTX * md_ctx
const EVP_MD * md
const EVP_MD * mgf1md
verify_info verify
Here is the call graph for this function:

◆ apply_verify_mechanism_update()

CK_RV apply_verify_mechanism_update ( yubihsm_pkcs11_op_info * op_info,
CK_BYTE_PTR in,
CK_ULONG in_len )

Definition at line 2007 of file util_pkcs11.c.

2008 {
2009
2010 switch (op_info->mechanism.mechanism) {
2011 case CKM_SHA_1_HMAC:
2012 case CKM_SHA256_HMAC:
2013 case CKM_SHA384_HMAC:
2014 case CKM_SHA512_HMAC:
2015 case CKM_RSA_PKCS:
2016 case CKM_RSA_PKCS_PSS:
2017 case CKM_ECDSA:
2018 // NOTE(adma): no hash required for these mechanisms
2019 if (op_info->buffer_length + in_len > sizeof(op_info->buffer)) {
2020 return CKR_DATA_LEN_RANGE;
2021 }
2022
2023 memcpy(op_info->buffer + op_info->buffer_length, in, in_len);
2024 op_info->buffer_length += in_len;
2025 break;
2026
2027 case CKM_SHA1_RSA_PKCS:
2029 case CKM_ECDSA_SHA1:
2032 case CKM_ECDSA_SHA256:
2035 case CKM_ECDSA_SHA384:
2038 case CKM_ECDSA_SHA512:
2039 if (EVP_DigestUpdate(op_info->op.verify.md_ctx, in, in_len) != 1) {
2040 EVP_MD_CTX_destroy(op_info->op.verify.md_ctx);
2041 op_info->op.sign.md_ctx = NULL;
2042 return CKR_FUNCTION_FAILED;
2043 }
2044 break;
2045
2046 default:
2047 return CKR_FUNCTION_FAILED;
2048 }
2049
2050 return CKR_OK;
2051}
Here is the call graph for this function:

◆ check_decrypt_mechanism()

bool check_decrypt_mechanism ( yubihsm_pkcs11_slot * slot,
CK_MECHANISM_PTR pMechanism )

Definition at line 1678 of file util_pkcs11.c.

1679 {
1680
1681 CK_MECHANISM_TYPE mechanisms[128];
1682 CK_ULONG count = 128;
1683
1686 return false;
1687 }
1688
1689 if (get_mechanism_list(slot, mechanisms, &count) != CKR_OK) {
1690 return false;
1691 }
1692
1693 for (CK_ULONG i = 0; i < count; i++) {
1694 if (pMechanism->mechanism == mechanisms[i]) {
1695 return true;
1696 }
1697 }
1698
1699 return false;
1700}
int * count
unsigned long int CK_ULONG
Definition pkcs11.h:1194
ck_mechanism_type_t mechanism
Definition pkcs11.h:695
CK_RV get_mechanism_list(yubihsm_pkcs11_slot *slot, CK_MECHANISM_TYPE_PTR pMechanismList, CK_ULONG_PTR count)
Definition util_pkcs11.c:73
bool is_RSA_decrypt_mechanism(CK_MECHANISM_TYPE m)
CK_SESSION_HANDLE CK_MECHANISM_PTR pMechanism
Here is the call graph for this function:

◆ check_digest_mechanism()

bool check_digest_mechanism ( CK_MECHANISM_PTR pMechanism)

Definition at line 1725 of file util_pkcs11.c.

1725 {
1726
1727 switch (pMechanism->mechanism) {
1728 case CKM_SHA_1:
1729 case CKM_SHA256:
1730 case CKM_SHA384:
1731 case CKM_SHA512:
1732 break;
1733 default:
1734 return false;
1735 }
1736
1737 return true;
1738}

◆ check_encrypt_mechanism()

bool check_encrypt_mechanism ( yubihsm_pkcs11_slot * slot,
CK_MECHANISM_PTR pMechanism )

Definition at line 1702 of file util_pkcs11.c.

1703 {
1704
1705 CK_MECHANISM_TYPE mechanisms[128];
1706 CK_ULONG count = 128;
1707
1709 return false;
1710 }
1711
1712 if (get_mechanism_list(slot, mechanisms, &count) != CKR_OK) {
1713 return false;
1714 }
1715
1716 for (CK_ULONG i = 0; i < count; i++) {
1717 if (pMechanism->mechanism == mechanisms[i]) {
1718 return true;
1719 }
1720 }
1721
1722 return false;
1723}
Here is the call graph for this function:

◆ check_sign_mechanism()

bool check_sign_mechanism ( yubihsm_pkcs11_slot * slot,
CK_MECHANISM_PTR pMechanism )

Definition at line 1646 of file util_pkcs11.c.

1647 {
1648
1649 CK_MECHANISM_TYPE mechanisms[128];
1650 CK_ULONG count = 128;
1651
1655
1656 return false;
1657 }
1658
1659 if (get_mechanism_list(slot, mechanisms, &count) != CKR_OK) {
1660 return false;
1661 }
1662
1663 for (CK_ULONG i = 0; i < count; i++) {
1664 if (pMechanism->mechanism == mechanisms[i]) {
1665 return true;
1666 }
1667 }
1668
1669 return false;
1670}
bool is_HMAC_sign_mechanism(CK_MECHANISM_TYPE m)
bool is_RSA_sign_mechanism(CK_MECHANISM_TYPE m)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ check_verify_mechanism()

bool check_verify_mechanism ( yubihsm_pkcs11_slot * slot,
CK_MECHANISM_PTR pMechanism )

Definition at line 1672 of file util_pkcs11.c.

1673 {
1674
1676}
bool check_sign_mechanism(yubihsm_pkcs11_slot *slot, CK_MECHANISM_PTR pMechanism)
Here is the call graph for this function:

◆ check_wrap_mechanism()

bool check_wrap_mechanism ( yubihsm_pkcs11_slot * slot,
CK_MECHANISM_PTR pMechanism )

Definition at line 1740 of file util_pkcs11.c.

1741 {
1742
1743 CK_MECHANISM_TYPE mechanisms[128];
1744 CK_ULONG count = 128;
1745
1747 return false;
1748 }
1749
1750 if (get_mechanism_list(slot, mechanisms, &count) != CKR_OK) {
1751 return false;
1752 }
1753
1754 for (CK_ULONG i = 0; i < count; i++) {
1755 if (pMechanism->mechanism == mechanisms[i]) {
1756 return true;
1757 }
1758 }
1759
1760 return false;
1761}
Here is the call graph for this function:

◆ create_session()

bool create_session ( yubihsm_pkcs11_slot * slot,
CK_FLAGS flags,
CK_SESSION_HANDLE_PTR phSession )

Definition at line 491 of file util_pkcs11.c.

492 {
493
494 bool authed = false;
496 memset(&session, 0, sizeof(session));
500 if (s->session_state & SESSION_AUTHENTICATED) {
501 authed = true;
502 }
503 }
504 if (flags & CKF_RW_SESSION) {
505 session.session_state =
507 } else {
508 session.session_state =
510 }
512 session.slot = slot;
513 list_create(&session.ecdh_session_keys, sizeof(ecdh_session_key), NULL);
514 *phSession = (slot->id << 16) + session.id;
515 return list_append(&slot->pkcs11_sessions, (void *) &session);
516}
CK_SESSION_HANDLE session
#define CKF_RW_SESSION
Definition pkcs11.h:293
void * data
Definition list.h:31
CK_SLOT_ID CK_FLAGS CK_VOID_PTR CK_NOTIFY CK_SESSION_HANDLE_PTR phSession
char * s
pInfo flags
@ SESSION_AUTHENTICATED_RW
@ SESSION_AUTHENTICATED_RO
@ SESSION_RESERVED_RO
@ SESSION_RESERVED_RW
#define SESSION_AUTHENTICATED
Here is the call graph for this function:

◆ decrypt_mechanism_cleanup()

bool decrypt_mechanism_cleanup ( yubihsm_pkcs11_op_info * op_info)

Definition at line 2566 of file util_pkcs11.c.

2566 {
2567
2568 (void) op_info;
2569
2570 return true;
2571}

◆ delete_object_from_cache()

void delete_object_from_cache ( yubihsm_pkcs11_object_desc * objects,
CK_OBJECT_HANDLE objHandle )

Definition at line 1571 of file util_pkcs11.c.

1572 {
1573 uint16_t id = objHandle & 0xffff;
1574 uint8_t type = objHandle >> 16;
1575 uint8_t sequence = objHandle >> 24;
1576
1577 for (uint16_t i = 0; i < YH_MAX_ITEMS_COUNT; i++) {
1578 if (objects[i].object.id == id &&
1579 (objects[i].object.type & 0x7f) == (type & 0x7f) &&
1580 objects[i].object.sequence == sequence) {
1581 memset(&objects[i], 0, sizeof(yubihsm_pkcs11_object_desc));
1582 return;
1583 }
1584 }
1585}
unsigned char uint8_t
Definition stdint.h:124
#define YH_MAX_ITEMS_COUNT
Max items the device may hold.
Definition yubihsm.h:103
Here is the call graph for this function:

◆ delete_session()

bool delete_session ( yubihsm_pkcs11_context * ctx,
CK_SESSION_HANDLE_PTR phSession )

Definition at line 2831 of file util_pkcs11.c.

2832 {
2833 uint16_t slot_id = *phSession >> 16;
2834 uint16_t session_id = *phSession & 0xffff;
2836 bool ret = false;
2837
2838 if (slot) {
2839 ListItem *item =
2840 list_get(&slot->pkcs11_sessions, &session_id, compare_session);
2841 if (item) {
2843 ret = true;
2844 }
2845 release_slot(ctx, slot);
2846 }
2847 return ret;
2848}
void list_delete(List *list, ListItem *item)
Definition list.c:117
ListItem * list_get(List *list, void *data, CompareItemFn compare_item_fn)
Definition list.c:105
#define slot_id
Definition pkcs11.h:146
void release_slot(yubihsm_pkcs11_context *ctx, yubihsm_pkcs11_slot *slot)
yubihsm_pkcs11_slot * get_slot(yubihsm_pkcs11_context *ctx, CK_ULONG id)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ destroy_session()

void destroy_session ( yubihsm_pkcs11_context * ctx,
CK_SESSION_HANDLE hSession )

◆ digest_mechanism_cleanup()

bool digest_mechanism_cleanup ( yubihsm_pkcs11_op_info * op_info)

Definition at line 2573 of file util_pkcs11.c.

2573 {
2574
2575 if (op_info->op.digest.md_ctx != NULL) {
2576 EVP_MD_CTX_destroy(op_info->op.digest.md_ctx);
2577 op_info->op.digest.md_ctx = NULL;
2578 }
2579
2580 return true;
2581}

◆ get_attribute()

CK_RV get_attribute ( CK_ATTRIBUTE_TYPE type,
yh_object_descriptor * object,
CK_VOID_PTR value,
CK_ULONG_PTR length,
yh_session * session )

Definition at line 1455 of file util_pkcs11.c.

1457 {
1458
1459 CK_BYTE tmp[2048];
1460 CK_VOID_PTR ptr;
1461 if (value == NULL) {
1462 // NOTE(adma): we just need the length, use a scratchpad for the data
1463 ptr = tmp;
1464 *length = sizeof(tmp);
1465 } else {
1466 // NOTE(adma): otherwise actually save the data
1467 ptr = value;
1468 }
1469
1470 switch (object->type) {
1471 case YH_OPAQUE:
1472 return get_attribute_opaque(type, object, ptr, length, session);
1473
1474 case YH_WRAP_KEY:
1475 case YH_HMAC_KEY:
1476 return get_attribute_secret_key(type, object, ptr, length);
1477
1478 case YH_ASYMMETRIC_KEY:
1479 return get_attribute_private_key(type, object, ptr, length, session);
1480 case 0x80 | YH_ASYMMETRIC_KEY:
1481 return get_attribute_public_key(type, object, ptr, length, session);
1482
1483 case YH_TEMPLATE:
1485 case YH_OTP_AEAD_KEY:
1486 // TODO: do something good here.
1487 break;
1488 } // TODO(adma): try to check common attributes in some convenience function
1489
1490 return CKR_OK;
1491}
#define value
Definition pkcs11.h:157
unsigned char CK_BYTE
Definition pkcs11.h:1190
void * CK_VOID_PTR
Definition pkcs11.h:1200
yh_object_type type
Object type.
Definition yubihsm.h:550
@ YH_OTP_AEAD_KEY
OTP AEAD Key is a secret key used to decrypt Yubico OTP values.
Definition yubihsm.h:376
@ YH_WRAP_KEY
Definition yubihsm.h:369
@ YH_OPAQUE
Definition yubihsm.h:362
@ YH_HMAC_KEY
HMAC Key is a secret key used when computing and verifying HMAC signatures.
Definition yubihsm.h:371
@ YH_TEMPLATE
Definition yubihsm.h:374
@ YH_ASYMMETRIC_KEY
Asymmetric Key is the private key of an asymmetric key-pair.
Definition yubihsm.h:366
@ YH_AUTHENTICATION_KEY
Authentication Key is used to establish Sessions with a device.
Definition yubihsm.h:364
yh_object_descriptor object
Here is the caller graph for this function:

◆ get_attribute_ecsession_key()

CK_RV get_attribute_ecsession_key ( CK_ATTRIBUTE_TYPE type,
ecdh_session_key * key,
CK_VOID_PTR value,
CK_ULONG_PTR length )

Definition at line 1493 of file util_pkcs11.c.

1494 {
1495
1496 CK_BYTE tmp[2048];
1497 CK_VOID_PTR ptr;
1498 if (value == NULL) {
1499 ptr = tmp;
1500 *length = sizeof(tmp);
1501 } else {
1502 ptr = value;
1503 }
1504
1505 switch (type) {
1506 case CKA_CLASS:
1507 *((CK_OBJECT_CLASS *) ptr) = CKO_SECRET_KEY;
1508 *length = sizeof(CK_OBJECT_CLASS);
1509 break;
1510
1511 case CKA_KEY_TYPE:
1512 *((CK_KEY_TYPE *) ptr) = CKK_GENERIC_SECRET;
1513 *length = sizeof(CK_KEY_TYPE);
1514 break;
1515
1516 case CKA_ID: {
1517 CK_OBJECT_HANDLE *id = ptr;
1518 *id = key->id;
1519 *length = sizeof(CK_OBJECT_HANDLE);
1520 break;
1521 }
1522
1523 case CKA_LABEL:
1524 *length = strlen(key->label);
1525 memcpy(ptr, key->label, *length);
1526 break;
1527
1528 case CKA_LOCAL:
1529 case CKA_TOKEN:
1530 *((CK_BBOOL *) ptr) = CK_FALSE;
1531 *length = sizeof(CK_BBOOL);
1532 break;
1533
1534 case CKA_DESTROYABLE:
1535 case CKA_EXTRACTABLE:
1536 *((CK_BBOOL *) ptr) = CK_TRUE;
1537 *length = sizeof(CK_BBOOL);
1538 break;
1539
1540 case CKA_DERIVE:
1542 case CKA_SENSITIVE:
1544 case CKA_MODIFIABLE:
1545 case CKA_COPYABLE:
1546 case CKA_SIGN:
1547 case CKA_SIGN_RECOVER:
1549 case CKA_UNWRAP:
1550 case CKA_WRAP:
1552 case CKA_VERIFY:
1553 case CKA_ENCRYPT:
1554 *((CK_BBOOL *) ptr) = CK_FALSE;
1555 *length = sizeof(CK_BBOOL);
1556 break;
1557
1558 case CKA_VALUE:
1559 memcpy(ptr, key->ecdh_key, key->len);
1560 *length = key->len;
1561 break;
1562
1563 default:
1566 }
1567
1568 return CKR_OK;
1569}
#define CKA_NEVER_EXTRACTABLE
Definition pkcs11.h:417
#define CKA_TOKEN
Definition pkcs11.h:364
#define CKR_ATTRIBUTE_TYPE_INVALID
Definition pkcs11.h:1104
#define CK_FALSE
Definition pkcs11.h:1202
#define CKA_ID
Definition pkcs11.h:385
#define CKO_SECRET_KEY
Definition pkcs11.h:304
#define CKA_MODIFIABLE
Definition pkcs11.h:420
#define CKA_SENSITIVE
Definition pkcs11.h:386
#define CKA_SIGN_RECOVER
Definition pkcs11.h:392
#define CKA_SIGN
Definition pkcs11.h:391
#define CK_UNAVAILABLE_INFORMATION
Definition pkcs11.h:265
#define CK_TRUE
Definition pkcs11.h:1203
#define CKA_VALUE
Definition pkcs11.h:368
#define CKA_LOCAL
Definition pkcs11.h:416
#define CKA_VERIFY
Definition pkcs11.h:393
#define CKA_COPYABLE
Definition pkcs11.h:421
#define CKA_UNWRAP
Definition pkcs11.h:390
unsigned char CK_BBOOL
Definition pkcs11.h:1193
#define CKA_WRAP_WITH_TRUSTED
Definition pkcs11.h:429
#define CKA_KEY_TYPE
Definition pkcs11.h:383
#define CKA_ALWAYS_AUTHENTICATE
Definition pkcs11.h:428
#define CKK_GENERIC_SECRET
Definition pkcs11.h:326
#define CKA_DESTROYABLE
Definition pkcs11.h:422
#define CKA_DERIVE
Definition pkcs11.h:395
#define CKA_ALWAYS_SENSITIVE
Definition pkcs11.h:418
#define CKA_LABEL
Definition pkcs11.h:366
#define CKA_EXTRACTABLE
Definition pkcs11.h:415
#define CKA_ENCRYPT
Definition pkcs11.h:387
#define CKA_WRAP
Definition pkcs11.h:389
#define CKA_CLASS
Definition pkcs11.h:363
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_digest_bytelength()

CK_ULONG get_digest_bytelength ( CK_MECHANISM_TYPE m)

Definition at line 2583 of file util_pkcs11.c.

2583 {
2584
2585 switch (m) {
2586 case CKM_SHA_1:
2587 return 20;
2588
2589 case CKM_SHA256:
2590 return 32;
2591
2592 case CKM_SHA384:
2593 return 48;
2594
2595 case CKM_SHA512:
2596 return 64;
2597
2598 default:
2599 break;
2600 }
2601
2602 return 0;
2603}

◆ get_mechanism_info()

bool get_mechanism_info ( yubihsm_pkcs11_slot * slot,
CK_MECHANISM_TYPE type,
CK_MECHANISM_INFO_PTR pInfo )

Definition at line 313 of file util_pkcs11.c.

314 {
315
316 if (slot->n_algorithms == 0) {
317 slot->n_algorithms = sizeof(slot->algorithms) / sizeof(slot->algorithms[0]);
318 yh_rc yrc =
319 yh_util_get_device_info(slot->connector, NULL, NULL, NULL, NULL, NULL,
320 NULL, slot->algorithms, &slot->n_algorithms);
321 if (yrc != YHR_SUCCESS) {
322 return false;
323 }
324 }
325
326 pInfo->flags = 0;
327 switch (type) {
328 case CKM_RSA_PKCS:
330
335 find_minmax_rsa_key_length_in_bits(slot->algorithms, slot->n_algorithms,
336 &pInfo->ulMinKeySize,
337 &pInfo->ulMaxKeySize);
339 break;
340
341 case CKM_RSA_PKCS_PSS:
346 find_minmax_rsa_key_length_in_bits(slot->algorithms, slot->n_algorithms,
347 &pInfo->ulMinKeySize,
348 &pInfo->ulMaxKeySize);
350 break;
351
353 find_minmax_rsa_key_length_in_bits(slot->algorithms, slot->n_algorithms,
354 &pInfo->ulMinKeySize,
355 &pInfo->ulMaxKeySize);
357 break;
358
360 find_minmax_rsa_key_length_in_bits(slot->algorithms, slot->n_algorithms,
361 &pInfo->ulMinKeySize,
362 &pInfo->ulMaxKeySize);
364 break;
365
367 find_minmax_ec_key_length_in_bits(slot->algorithms, slot->n_algorithms,
368 &pInfo->ulMinKeySize,
369 &pInfo->ulMaxKeySize);
372 break;
373
374 case CKM_SHA_1_HMAC:
375 pInfo->ulMaxKeySize = 64 * 8;
376 pInfo->ulMinKeySize = 1;
378 break;
379
380 case CKM_SHA256_HMAC:
381 pInfo->ulMaxKeySize = 64 * 8;
382 pInfo->ulMinKeySize = 1;
384 break;
385
386 case CKM_SHA384_HMAC:
387 pInfo->ulMaxKeySize = 128 * 8;
388 pInfo->ulMinKeySize = 1;
390 break;
391
392 case CKM_SHA512_HMAC:
393 pInfo->ulMaxKeySize = 128 * 8;
394 pInfo->ulMinKeySize = 1;
396 break;
397
398 case CKM_ECDSA:
399 case CKM_ECDSA_SHA1:
400 case CKM_ECDSA_SHA256:
401 case CKM_ECDSA_SHA384:
402 case CKM_ECDSA_SHA512:
403 // should all ecdsa mechanisms have all keylengths? or should they be
404 // bounded to length of hash?
405 find_minmax_ec_key_length_in_bits(slot->algorithms, slot->n_algorithms,
406 &pInfo->ulMinKeySize,
407 &pInfo->ulMaxKeySize);
410 break;
411
412 case CKM_ECDH1_DERIVE:
413 find_minmax_ec_key_length_in_bits(slot->algorithms, slot->n_algorithms,
414 &pInfo->ulMinKeySize,
415 &pInfo->ulMaxKeySize);
418 break;
419
420 case CKM_SHA_1:
421 pInfo->ulMaxKeySize = 0; // NOTE(adma): ignored
422 pInfo->ulMinKeySize = 0; // NOTE(adma): ignored
424 break;
425
426 case CKM_SHA256:
427 pInfo->ulMaxKeySize = 0; // NOTE(adma): ignored
428 pInfo->ulMinKeySize = 0; // NOTE(adma): ignored
430 break;
431
432 case CKM_SHA384:
433 pInfo->ulMaxKeySize = 0; // NOTE(adma): ignored
434 pInfo->ulMinKeySize = 0; // NOTE(adma): ignored
436 break;
437
438 case CKM_SHA512:
439 pInfo->ulMaxKeySize = 0; // NOTE(adma): ignored
440 pInfo->ulMinKeySize = 0; // NOTE(adma): ignored
442 break;
443
445 pInfo->ulMaxKeySize = 256;
446 pInfo->ulMinKeySize = 128;
448 break;
449
451 pInfo->ulMaxKeySize =
452 128 * 8; // NOTE: 128*8 is max key size for sha512-hmac keys
453 pInfo->ulMinKeySize = 1;
455 break;
456
457 default:
458 return false;
459 }
460
461 return true;
462}
#define CKF_EC_UNCOMPRESS
Definition pkcs11.h:725
#define CKF_DIGEST
Definition pkcs11.h:709
#define CKF_DERIVE
Definition pkcs11.h:718
#define CKF_VERIFY
Definition pkcs11.h:712
#define CKM_ECDH1_DERIVE
Definition pkcs11.h:652
#define CKM_RSA_PKCS_KEY_PAIR_GEN
Definition pkcs11.h:469
#define CKF_WRAP
Definition pkcs11.h:716
#define CKF_EC_F_P
Definition pkcs11.h:721
#define CKF_GENERATE_KEY_PAIR
Definition pkcs11.h:715
#define CKF_ENCRYPT
Definition pkcs11.h:707
#define CKF_EC_ECPARAMETERS
Definition pkcs11.h:723
#define CKM_GENERIC_SECRET_KEY_GEN
Definition pkcs11.h:586
#define CKM_EC_KEY_PAIR_GEN
Definition pkcs11.h:645
#define CKF_SIGN
Definition pkcs11.h:710
#define CKF_GENERATE
Definition pkcs11.h:714
#define CKF_UNWRAP
Definition pkcs11.h:717
#define CKF_DECRYPT
Definition pkcs11.h:708
#define CKF_HW
Definition pkcs11.h:706
ck_flags_t flags
Definition pkcs11.h:215
yh_algorithm algorithms[YH_MAX_ALGORITHM_COUNT]
yh_rc yh_util_get_device_info(yh_connector *connector, uint8_t *major, uint8_t *minor, uint8_t *patch, uint32_t *serial, uint8_t *log_total, uint8_t *log_used, yh_algorithm *algorithms, size_t *n_algorithms)
Definition yubihsm.c:938
yh_rc
Definition yubihsm.h:170
@ YHR_SUCCESS
Returned value when function was successful.
Definition yubihsm.h:172
CK_SLOT_ID CK_SLOT_INFO_PTR pInfo
yh_rc yrc
Here is the call graph for this function:

◆ get_mechanism_list()

CK_RV get_mechanism_list ( yubihsm_pkcs11_slot * slot,
CK_MECHANISM_TYPE_PTR pMechanismList,
CK_ULONG_PTR count )

Definition at line 73 of file util_pkcs11.c.

75 {
76
77 if (slot->n_algorithms == 0) {
78 slot->n_algorithms = sizeof(slot->algorithms) / sizeof(slot->algorithms[0]);
79 yh_rc yrc =
80 yh_util_get_device_info(slot->connector, NULL, NULL, NULL, NULL, NULL,
82 if (yrc != YHR_SUCCESS) {
84 }
85 }
86
87 CK_MECHANISM_TYPE buffer[128]; // NOTE: this is a bit hardcoded, but much more
88 // than what we might add below.
89 CK_ULONG items = 0;
90
91 for (size_t i = 0; i < slot->n_algorithms; i++) {
92 switch (slot->algorithms[i]) {
94 add_mech(buffer, &items, CKM_RSA_PKCS);
95 add_mech(buffer, &items, CKM_SHA1_RSA_PKCS);
96 break;
97
99 add_mech(buffer, &items, CKM_RSA_PKCS);
100 add_mech(buffer, &items, CKM_SHA256_RSA_PKCS);
101 break;
102
104 add_mech(buffer, &items, CKM_RSA_PKCS);
105 add_mech(buffer, &items, CKM_SHA384_RSA_PKCS);
106 break;
107
109 add_mech(buffer, &items, CKM_RSA_PKCS);
110 add_mech(buffer, &items, CKM_SHA512_RSA_PKCS);
111 break;
112
114 add_mech(buffer, &items, CKM_RSA_PKCS_PSS);
115 add_mech(buffer, &items, CKM_SHA1_RSA_PKCS_PSS);
116 break;
117
119 add_mech(buffer, &items, CKM_RSA_PKCS_PSS);
120 add_mech(buffer, &items, CKM_SHA256_RSA_PKCS_PSS);
121 break;
122
124 add_mech(buffer, &items, CKM_RSA_PKCS_PSS);
125 add_mech(buffer, &items, CKM_SHA384_RSA_PKCS_PSS);
126 break;
127
129 add_mech(buffer, &items, CKM_RSA_PKCS_PSS);
130 add_mech(buffer, &items, CKM_SHA512_RSA_PKCS_PSS);
131 break;
132
133 case YH_ALGO_RSA_2048:
134 case YH_ALGO_RSA_3072:
135 case YH_ALGO_RSA_4096:
136 add_mech(buffer, &items, CKM_RSA_PKCS_KEY_PAIR_GEN);
137 break;
138
139 case YH_ALGO_EC_P224:
140 case YH_ALGO_EC_P256:
141 case YH_ALGO_EC_P384:
142 case YH_ALGO_EC_P521:
143 case YH_ALGO_EC_K256:
144 case YH_ALGO_EC_BP256:
145 case YH_ALGO_EC_BP384:
146 case YH_ALGO_EC_BP512:
147 add_mech(buffer, &items, CKM_EC_KEY_PAIR_GEN);
148 break;
149
151 add_mech(buffer, &items, CKM_SHA_1_HMAC);
152 add_mech(buffer, &items, CKM_GENERIC_SECRET_KEY_GEN);
153 break;
154
156 add_mech(buffer, &items, CKM_SHA256_HMAC);
157 add_mech(buffer, &items, CKM_GENERIC_SECRET_KEY_GEN);
158 break;
159
161 add_mech(buffer, &items, CKM_SHA384_HMAC);
162 add_mech(buffer, &items, CKM_GENERIC_SECRET_KEY_GEN);
163 break;
164
166 add_mech(buffer, &items, CKM_SHA512_HMAC);
167 add_mech(buffer, &items, CKM_GENERIC_SECRET_KEY_GEN);
168 break;
169
171 add_mech(buffer, &items, CKM_ECDSA);
172 add_mech(buffer, &items, CKM_ECDSA_SHA1);
173 break;
174
176 add_mech(buffer, &items, CKM_ECDSA);
177 add_mech(buffer, &items, CKM_ECDSA_SHA256);
178 break;
179
181 add_mech(buffer, &items, CKM_ECDSA);
182 add_mech(buffer, &items, CKM_ECDSA_SHA384);
183 break;
184
186 add_mech(buffer, &items, CKM_ECDSA);
187 add_mech(buffer, &items, CKM_ECDSA_SHA512);
188 break;
189
190 case YH_ALGO_EC_ECDH:
191 add_mech(buffer, &items, CKM_ECDH1_DERIVE);
192 break;
193
198 add_mech(buffer, &items, CKM_RSA_PKCS_OAEP);
199 break;
200
204 add_mech(buffer, &items, CKM_YUBICO_AES_CCM_WRAP);
205 add_mech(buffer, &items, CKM_GENERIC_SECRET_KEY_GEN);
206 break;
207
208 // NOTE: there are algorithms don't have corresponding mechanisms
209 default:
210 break;
211 }
212 }
213
214 // NOTE(adma): manually add digest mechanisms
215 add_mech(buffer, &items, CKM_SHA_1);
216 add_mech(buffer, &items, CKM_SHA256);
217 add_mech(buffer, &items, CKM_SHA384);
218 add_mech(buffer, &items, CKM_SHA512);
219
220 if (pMechanismList != NULL) {
221 if (items > *count) {
222 *count = items;
223
225 }
226
227 memcpy(pMechanismList, buffer, sizeof(CK_MECHANISM_TYPE) * items);
228 }
229
230 *count = items;
231
232 return CKR_OK;
233}
#define CKR_BUFFER_TOO_SMALL
Definition pkcs11.h:1169
@ YH_ALGO_EC_P521
ecp521
Definition yubihsm.h:418
@ YH_ALGO_RSA_PSS_SHA384
rsa-pss-sha384
Definition yubihsm.h:404
@ YH_ALGO_EC_ECDH
ecdh
Definition yubihsm.h:438
@ YH_ALGO_RSA_PKCS1_SHA256
rsa-pkcs1-sha256
Definition yubihsm.h:394
@ YH_ALGO_RSA_PKCS1_SHA1
rsa-pkcs1-sha1
Definition yubihsm.h:392
@ YH_ALGO_RSA_PSS_SHA512
rsa-pss-sha512
Definition yubihsm.h:406
@ YH_ALGO_EC_BP384
ecbp384
Definition yubihsm.h:424
@ YH_ALGO_RSA_PSS_SHA1
rsa-pss-sha1
Definition yubihsm.h:400
@ YH_ALGO_EC_ECDSA_SHA256
ecdsa-sha256
Definition yubihsm.h:476
@ YH_ALGO_EC_P384
ecp384
Definition yubihsm.h:416
@ YH_ALGO_EC_ECDSA_SHA512
ecdsa-sha512
Definition yubihsm.h:480
@ YH_ALGO_RSA_OAEP_SHA256
rsa-oaep-sha256
Definition yubihsm.h:442
@ YH_ALGO_EC_ECDSA_SHA384
ecdsa-sha384
Definition yubihsm.h:478
@ YH_ALGO_HMAC_SHA512
hmac-sha512
Definition yubihsm.h:434
@ YH_ALGO_HMAC_SHA384
hmac-sha384
Definition yubihsm.h:432
@ YH_ALGO_RSA_PKCS1_SHA512
rsa-pkcs1-sha512
Definition yubihsm.h:398
@ YH_ALGO_AES256_CCM_WRAP
aes256-ccm-wrap
Definition yubihsm.h:474
@ YH_ALGO_HMAC_SHA1
hmac-sha1
Definition yubihsm.h:428
@ YH_ALGO_RSA_OAEP_SHA512
rsa-oaep-sha512
Definition yubihsm.h:446
@ YH_ALGO_RSA_2048
rsa2048
Definition yubihsm.h:408
@ YH_ALGO_HMAC_SHA256
hmac-sha256
Definition yubihsm.h:430
@ YH_ALGO_RSA_OAEP_SHA384
rsa-oaep-sha384
Definition yubihsm.h:444
@ YH_ALGO_AES192_CCM_WRAP
aes192-ccm-wrap
Definition yubihsm.h:472
@ YH_ALGO_EC_BP512
ecbp512
Definition yubihsm.h:426
@ YH_ALGO_EC_BP256
ecbp256
Definition yubihsm.h:422
@ YH_ALGO_RSA_PSS_SHA256
rsa-pss-sha256
Definition yubihsm.h:402
@ YH_ALGO_EC_K256
eck256
Definition yubihsm.h:420
@ YH_ALGO_AES128_CCM_WRAP
aes128-ccm-wrap
Definition yubihsm.h:448
@ YH_ALGO_EC_P256
ecp256
Definition yubihsm.h:414
@ YH_ALGO_RSA_4096
rsa4096
Definition yubihsm.h:412
@ YH_ALGO_EC_ECDSA_SHA1
ecdsa-sha1
Definition yubihsm.h:436
@ YH_ALGO_RSA_OAEP_SHA1
rsa-oaep-sha1
Definition yubihsm.h:440
@ YH_ALGO_EC_P224
ecp224
Definition yubihsm.h:484
@ YH_ALGO_RSA_PKCS1_SHA384
rsa-pkcs1-sha384
Definition yubihsm.h:396
@ YH_ALGO_RSA_3072
rsa3072
Definition yubihsm.h:410
CK_SLOT_ID CK_MECHANISM_TYPE_PTR pMechanismList
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_object_desc()

yubihsm_pkcs11_object_desc * get_object_desc ( yh_session * session,
yubihsm_pkcs11_object_desc * objects,
CK_OBJECT_HANDLE objectHandle )

Definition at line 1587 of file util_pkcs11.c.

1589 {
1590
1591 yubihsm_pkcs11_object_desc *object = NULL;
1592 uint16_t id = objHandle & 0xffff;
1593 uint8_t type = objHandle >> 16;
1594 uint8_t sequence = objHandle >> 24;
1595
1596 for (uint16_t i = 0; i < YH_MAX_ITEMS_COUNT; i++) {
1597 if (objects[i].object.id == id &&
1598 (objects[i].object.type & 0x7f) == (type & 0x7f) &&
1599 objects[i].object.sequence == sequence) {
1600 object = &objects[i];
1601 break;
1602 }
1603 }
1604
1605 if (!object) {
1606 uint16_t low;
1607 struct timeval *low_time = NULL;
1608
1609 for (uint16_t i = 0; i < YH_MAX_ITEMS_COUNT; i++) {
1610 if (objects[i].tv.tv_sec == 0) {
1611 low = i;
1612 low_time = &objects[i].tv;
1613 break;
1614 } else {
1615 if (!low_time || objects[i].tv.tv_sec < low_time->tv_sec ||
1616 (objects[i].tv.tv_sec == low_time->tv_sec &&
1617 objects[i].tv.tv_usec < low_time->tv_usec)) {
1618
1619 low_time = &objects[i].tv;
1620 low = i;
1621 }
1622 }
1623 }
1624 object = &objects[low];
1625 memset(object, 0, sizeof(yubihsm_pkcs11_object_desc));
1626 }
1627
1628 if (!object->filled) {
1629 uint16_t real_type =
1630 type & ~0x80; // NOTE(adma): public key are not real objects
1631 yh_rc rc = yh_util_get_object_info(session, id, real_type, &object->object);
1632 if (rc != YHR_SUCCESS) {
1633 return NULL;
1634 }
1635
1636 object->filled = true;
1637 }
1638
1639 object->object.type = type;
1640
1641 gettimeofday(&object->tv, NULL);
1642
1643 return object;
1644}
yh_object_descriptor object
yh_rc yh_util_get_object_info(yh_session *session, uint16_t id, yh_object_type type, yh_object_descriptor *object)
Definition yubihsm.c:1128
yh_object_type type
Definition yubihsm.h:672
yh_rc rc
Here is the call graph for this function:

◆ get_session()

CK_RV get_session ( yubihsm_pkcs11_context * ctx,
CK_SESSION_HANDLE hSession,
yubihsm_pkcs11_session ** session,
int session_state )

Definition at line 2789 of file util_pkcs11.c.

2790 {
2791 uint16_t slot_id = hSession >> 16;
2792 uint16_t session_id = hSession & 0xffff;
2793
2795 if (slot == NULL) {
2796 DBG_ERR("Slot %d doesn't exist", slot_id);
2798 }
2799
2800 ListItem *item =
2801 list_get(&slot->pkcs11_sessions, &session_id, compare_session);
2802 if (item == NULL) {
2803 release_slot(ctx, slot);
2804 DBG_ERR("Session %d doesn't exist", session_id);
2806 }
2807
2808 *session = (yubihsm_pkcs11_session *) item->data;
2809 int state = (int) (*session)->session_state;
2810 if (session_state == 0 || ((session_state & state) == state)) {
2811 // NOTE(thorduri): slot is locked.
2812 return CKR_OK;
2813 }
2814
2816 if (session_state == SESSION_AUTHENTICATED) {
2818 DBG_ERR("Session user not logged in");
2819 } else if (session_state == SESSION_AUTHENTICATED_RW) {
2821 DBG_ERR("Session read only");
2822 } else if (session_state == SESSION_NOT_AUTHENTICATED) {
2824 DBG_ERR("Session user already logged in");
2825 }
2826
2827 release_slot(ctx, slot);
2828 return rv;
2829}
#define CKR_SESSION_READ_ONLY
Definition pkcs11.h:1140
#define CKR_SESSION_HANDLE_INVALID
Definition pkcs11.h:1138
#define CKR_USER_ALREADY_LOGGED_IN
Definition pkcs11.h:1154
#define CKR_USER_NOT_LOGGED_IN
Definition pkcs11.h:1155
CK_SESSION_HANDLE hSession
CK_RV rv
#define SESSION_NOT_AUTHENTICATED
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_slot()

Definition at line 2766 of file util_pkcs11.c.

2766 {
2767
2768 ListItem *item = list_get(&ctx->slots, &id, compare_slot);
2769 if (item) {
2771 if (slot->mutex != NULL) {
2772 if (ctx->lock_mutex(slot->mutex) != CKR_OK) {
2773 return NULL;
2774 }
2775 }
2776 return slot;
2777 }
2778
2779 return NULL;
2780}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ is_ECDSA_sign_mechanism()

bool is_ECDSA_sign_mechanism ( CK_MECHANISM_TYPE m)

Definition at line 2690 of file util_pkcs11.c.

2690 {
2691
2692 switch (m) {
2693 case CKM_ECDSA:
2694 case CKM_ECDSA_SHA1:
2695 case CKM_ECDSA_SHA256:
2696 case CKM_ECDSA_SHA384:
2697 case CKM_ECDSA_SHA512:
2698 return true;
2699
2700 default:
2701 break;
2702 }
2703
2704 return false;
2705}
Here is the caller graph for this function:

◆ is_hashed_mechanism()

bool is_hashed_mechanism ( CK_MECHANISM_TYPE m)

Definition at line 2645 of file util_pkcs11.c.

2645 {
2646
2647 switch (m) {
2648 case CKM_SHA1_RSA_PKCS:
2656 case CKM_ECDSA_SHA1:
2657 case CKM_ECDSA_SHA256:
2658 case CKM_ECDSA_SHA384:
2659 case CKM_ECDSA_SHA512:
2660 case CKM_SHA_1:
2661 case CKM_SHA256:
2662 case CKM_SHA384:
2663 case CKM_SHA512:
2664 return true;
2665
2666 default:
2667 break;
2668 }
2669
2670 return false;
2671}
Here is the caller graph for this function:

◆ is_HMAC_sign_mechanism()

bool is_HMAC_sign_mechanism ( CK_MECHANISM_TYPE m)

Definition at line 2724 of file util_pkcs11.c.

2724 {
2725
2726 switch (m) {
2727 case CKM_SHA_1_HMAC:
2728 case CKM_SHA256_HMAC:
2729 case CKM_SHA384_HMAC:
2730 case CKM_SHA512_HMAC:
2731 return true;
2732
2733 default:
2734 break;
2735 }
2736
2737 return false;
2738}
Here is the caller graph for this function:

◆ is_PKCS1v1_5_sign_mechanism()

bool is_PKCS1v1_5_sign_mechanism ( CK_MECHANISM_TYPE m)

Definition at line 2673 of file util_pkcs11.c.

2673 {
2674
2675 switch (m) {
2676 case CKM_RSA_PKCS:
2677 case CKM_SHA1_RSA_PKCS:
2681 return true;
2682
2683 default:
2684 break;
2685 }
2686
2687 return false;
2688}
Here is the caller graph for this function:

◆ is_PSS_sign_mechanism()

bool is_PSS_sign_mechanism ( CK_MECHANISM_TYPE m)

Definition at line 2707 of file util_pkcs11.c.

2707 {
2708
2709 switch (m) {
2710 case CKM_RSA_PKCS_PSS:
2715 return true;
2716
2717 default:
2718 break;
2719 }
2720
2721 return false;
2722}
Here is the caller graph for this function:

◆ is_RSA_decrypt_mechanism()

bool is_RSA_decrypt_mechanism ( CK_MECHANISM_TYPE m)

Definition at line 2627 of file util_pkcs11.c.

2627 {
2628
2629 switch (m) {
2630 case CKM_RSA_PKCS:
2631 case CKM_SHA1_RSA_PKCS:
2635 case CKM_RSA_PKCS_OAEP:
2636 return true;
2637
2638 default:
2639 break;
2640 }
2641
2642 return false;
2643}
Here is the caller graph for this function:

◆ is_RSA_sign_mechanism()

bool is_RSA_sign_mechanism ( CK_MECHANISM_TYPE m)

Definition at line 2605 of file util_pkcs11.c.

2605 {
2606
2607 switch (m) {
2608 case CKM_RSA_PKCS:
2609 case CKM_SHA1_RSA_PKCS:
2613 case CKM_RSA_PKCS_PSS:
2618 return true;
2619
2620 default:
2621 break;
2622 }
2623
2624 return false;
2625}
Here is the caller graph for this function:

◆ parse_ec_generate_template()

CK_RV parse_ec_generate_template ( CK_ATTRIBUTE_PTR pPublicKeyTemplate,
CK_ULONG ulPublicKeyAttributeCount,
CK_ATTRIBUTE_PTR pPrivateKeyTemplate,
CK_ULONG ulPrivateKeyAttributeCount,
yubihsm_pkcs11_object_template * template )

Definition at line 3570 of file util_pkcs11.c.

3574 {
3575
3576 uint8_t *ecparams = NULL;
3577 uint16_t ecparams_len = 0;
3578 bool label_set = FALSE;
3579 CK_RV rv;
3580
3581 memset(template->label, 0, sizeof(template->label));
3582
3583 for (CK_ULONG i = 0; i < ulPublicKeyAttributeCount; i++) {
3584 switch (pPublicKeyTemplate[i].type) {
3585 case CKA_CLASS:
3586 if (*((CK_ULONG_PTR) pPublicKeyTemplate[i].pValue) != CKO_PUBLIC_KEY) {
3587 DBG_ERR("CKA_CLASS inconsistent in PublicKeyTemplate");
3589 }
3590 break;
3591
3592 case CKA_KEY_TYPE:
3593 if (*((CK_ULONG_PTR) pPublicKeyTemplate[i].pValue) != CKK_EC) {
3594 DBG_ERR("CKA_KEY_TYPE inconsistent in PublicKeyTemplate");
3596 }
3597 break;
3598
3599 case CKA_ID:
3600 if (template->id == 0) {
3601 int id = parse_id_value(pPublicKeyTemplate[i].pValue,
3602 pPublicKeyTemplate[i].ulValueLen);
3603 if (id == -1) {
3604 DBG_ERR("CKA_ID invalid in PublicKeyTemplate");
3606 }
3607 template->id = id;
3608 } else {
3609 DBG_ERR("CKA_ID inconsistent in PublicKeyTemplate");
3611 }
3612 break;
3613
3614 case CKA_EC_PARAMS:
3615 if (ecparams == NULL) {
3616 ecparams = (CK_BYTE_PTR) pPublicKeyTemplate[i].pValue;
3617 ecparams_len = pPublicKeyTemplate[i].ulValueLen;
3618 } else {
3619 DBG_ERR("CKA_PUBLIC_EXPONENT inconsistent in PublicKeyTemplate");
3621 }
3622 break;
3623
3624 case CKA_LABEL:
3625 if (pPublicKeyTemplate[i].ulValueLen > YH_OBJ_LABEL_LEN) {
3626 DBG_ERR("CKA_LABEL invalid in PublicKeyTemplate");
3628 }
3629
3631 pPublicKeyTemplate[i].ulValueLen);
3632
3633 label_set = TRUE;
3634
3635 break;
3636
3637 case CKA_TOKEN:
3638 if ((rv = check_bool_attribute(pPublicKeyTemplate[i].pValue, true)) !=
3639 CKR_OK) {
3640 DBG_ERR("Boolean truth check failed for attribute 0x%lx",
3641 pPublicKeyTemplate[i].type);
3642 return rv;
3643 }
3644 break;
3645
3646 case CKA_MODIFIABLE:
3647 case CKA_DECRYPT:
3648 case CKA_SIGN:
3649 case CKA_WRAP:
3650 case CKA_UNWRAP:
3651 if ((rv = check_bool_attribute(pPublicKeyTemplate[i].pValue, false)) !=
3652 CKR_OK) {
3653 DBG_ERR("Boolean false check failed for attribute 0x%lx",
3654 pPublicKeyTemplate[i].type);
3655 return rv;
3656 }
3657 break;
3658
3659 case CKA_VERIFY:
3660 case CKA_ENCRYPT:
3661 case CKA_COPYABLE:
3662 case CKA_PRIVATE:
3663 case CKA_EXTRACTABLE:
3664 case CKA_DERIVE:
3665 break;
3666
3667 default:
3668 DBG_ERR("invalid attribute type in PublicKeyTemplate: 0x%lx\n",
3669 pPublicKeyTemplate[i].type);
3671 }
3672 }
3673
3674 for (CK_ULONG i = 0; i < ulPrivateKeyAttributeCount; i++) {
3675 switch (pPrivateKeyTemplate[i].type) {
3676 case CKA_CLASS:
3677 if (*((CK_ULONG_PTR) pPrivateKeyTemplate[i].pValue) !=
3679 DBG_ERR("CKA_CLASS inconsistent in PrivateKeyTemplate");
3681 }
3682 break;
3683
3684 case CKA_KEY_TYPE:
3685 if (*((CK_ULONG_PTR) pPrivateKeyTemplate[i].pValue) != CKK_EC) {
3686 DBG_ERR("CKA_KEY_TYPE inconsistent in PrivateKeyTemplate");
3688 }
3689 break;
3690
3691 case CKA_ID: {
3692 int id = parse_id_value(pPrivateKeyTemplate[i].pValue,
3693 pPrivateKeyTemplate[i].ulValueLen);
3694 if (id == -1) {
3695 DBG_ERR("CKA_ID invalid in PrivateKeyTemplate");
3697 }
3698 if (template->id != 0 && template->id != id) {
3699 DBG_ERR("CKA_ID inconsistent in PrivateKeyTemplate");
3701 } else {
3702 template->id = id;
3703 }
3704 } break;
3705
3706 case CKA_SIGN:
3708 pPrivateKeyTemplate[i].pValue)) !=
3709 CKR_OK) {
3710 DBG_ERR("CKA_SIGN inconsistent in PrivateKeyTemplate");
3711 return rv;
3712 }
3713 break;
3714
3715 case CKA_EXTRACTABLE:
3717 pPrivateKeyTemplate[i].pValue)) !=
3718 CKR_OK) {
3719 DBG_ERR("CKA_EXTRACTABLE inconsistent in PrivateKeyTemplate");
3720 return rv;
3721 }
3722 break;
3723
3724 case CKA_DERIVE:
3726 pPrivateKeyTemplate[i].pValue)) !=
3727 CKR_OK) {
3728 DBG_ERR("CKA_DERIVE inconsistent in PrivateKeyTemplate");
3729 return rv;
3730 }
3731 break;
3732
3733 case CKA_LABEL:
3734 if (pPrivateKeyTemplate[i].ulValueLen > YH_OBJ_LABEL_LEN) {
3735 DBG_ERR("CKA_LABEL invalid in PrivateKeyTemplate");
3737 }
3738
3739 if (label_set == TRUE) {
3740 if (memcmp(template->label, pPrivateKeyTemplate[i].pValue,
3741 pPrivateKeyTemplate[i].ulValueLen) != 0) {
3742 DBG_ERR("CKA_LABEL inconsistent in PrivateKeyTemplate");
3744 }
3745 } else {
3747 pPrivateKeyTemplate[i].ulValueLen);
3748 }
3749 break;
3750
3751 case CKA_TOKEN:
3752 case CKA_SENSITIVE:
3753 case CKA_PRIVATE:
3754 case CKA_DESTROYABLE:
3755 if ((rv = check_bool_attribute(pPrivateKeyTemplate[i].pValue, true)) !=
3756 CKR_OK) {
3757 DBG_ERR("Boolean truth check failed for attribute 0x%lx",
3758 pPrivateKeyTemplate[i].type);
3759 return rv;
3760 }
3761 break;
3762
3763 case CKA_UNWRAP:
3764 case CKA_WRAP:
3765 case CKA_MODIFIABLE:
3766 case CKA_COPYABLE:
3767 case CKA_ENCRYPT:
3768 case CKA_VERIFY:
3769 if ((rv = check_bool_attribute(pPrivateKeyTemplate[i].pValue, false)) !=
3770 CKR_OK) {
3771 DBG_ERR("Boolean false check failed for attribute 0x%lx",
3772 pPrivateKeyTemplate[i].type);
3773 return rv;
3774 }
3775 break;
3776
3777 case CKA_SUBJECT:
3778 case CKA_DECRYPT:
3779 break;
3780
3781 default:
3783 }
3784 }
3785
3786 if (ecparams == NULL) {
3787 DBG_ERR("CKA_ECPARAMS not set");
3789 }
3790
3792 rv = parse_ecparams(ecparams, ecparams_len, &template->algorithm, &key_len);
3793 if (rv != CKR_OK) {
3794 DBG_ERR("Failed to parse CKA_ECPARAMS");
3795 return rv;
3796 }
3797
3798 return CKR_OK;
3799}
uint64_t id
Definition code_cache.cpp:0
#define CKO_PUBLIC_KEY
Definition pkcs11.h:302
CK_ULONG * CK_ULONG_PTR
Definition pkcs11.h:1199
#define CKA_PRIVATE
Definition pkcs11.h:365
CK_BYTE * CK_BYTE_PTR
Definition pkcs11.h:1196
#define CKO_PRIVATE_KEY
Definition pkcs11.h:303
#define CKR_TEMPLATE_INCONSISTENT
Definition pkcs11.h:1147
#define CKA_DECRYPT
Definition pkcs11.h:388
#define TRUE
Definition pkcs11.h:1209
#define FALSE
Definition pkcs11.h:1206
#define CKR_ATTRIBUTE_VALUE_INVALID
Definition pkcs11.h:1105
#define CKR_TEMPLATE_INCOMPLETE
Definition pkcs11.h:1146
#define CKK_EC
Definition pkcs11.h:323
#define CKA_EC_PARAMS
Definition pkcs11.h:424
#define CKA_SUBJECT
Definition pkcs11.h:384
yubihsm_pkcs11_attribute sign
char label[YH_OBJ_LABEL_LEN+1]
yubihsm_pkcs11_attribute exportable
yubihsm_pkcs11_attribute derive
CK_RV set_template_attribute(yubihsm_pkcs11_attribute *attribute, void *value)
int parse_id_value(void *value, CK_ULONG len)
#define YH_OBJ_LABEL_LEN
Max length of object labels.
Definition yubihsm.h:123
CK_SESSION_HANDLE CK_MECHANISM_PTR CK_ATTRIBUTE_PTR CK_ULONG CK_ATTRIBUTE_PTR CK_ULONG ulPrivateKeyAttributeCount
yubihsm_pkcs11_object_template template
session operation op sign key_len
CK_SESSION_HANDLE CK_MECHANISM_PTR CK_ATTRIBUTE_PTR CK_ULONG CK_ATTRIBUTE_PTR pPrivateKeyTemplate
CK_SESSION_HANDLE CK_MECHANISM_PTR CK_ATTRIBUTE_PTR CK_ULONG ulPublicKeyAttributeCount
CK_SESSION_HANDLE CK_MECHANISM_PTR CK_ATTRIBUTE_PTR pPublicKeyTemplate
Here is the call graph for this function:

◆ parse_ec_template()

CK_RV parse_ec_template ( CK_ATTRIBUTE_PTR pTemplate,
CK_ULONG ulCount,
yubihsm_pkcs11_object_template * template )

Definition at line 3150 of file util_pkcs11.c.

3151 {
3152
3153 uint8_t *ecparams = NULL;
3154 uint16_t ecparams_len;
3155 CK_RV rv;
3156
3157 for (CK_ULONG i = 0; i < ulCount; i++) {
3158 switch (pTemplate[i].type) {
3159
3160 case CKA_VALUE:
3161 if (template->obj.buf == NULL) {
3162 template->obj.buf = (CK_BYTE_PTR) pTemplate[i].pValue;
3163 template->objlen = pTemplate[i].ulValueLen;
3164 } else {
3166 }
3167 break;
3168
3169 case CKA_EC_PARAMS:
3170 if (ecparams == NULL) {
3171 ecparams = (CK_BYTE_PTR) pTemplate[i].pValue;
3172 ecparams_len = pTemplate[i].ulValueLen;
3173 } else {
3175 }
3176 break;
3177
3178 case CKA_SIGN:
3180 pTemplate[i].pValue)) != CKR_OK) {
3181 return rv;
3182 }
3183 break;
3184
3185 case CKA_DERIVE:
3187 pTemplate[i].pValue)) != CKR_OK) {
3188 return rv;
3189 }
3190 break;
3191
3192 case CKA_TOKEN:
3193 case CKA_PRIVATE:
3194 case CKA_SENSITIVE:
3195 if ((rv = check_bool_attribute(pTemplate[i].pValue, true)) != CKR_OK) {
3196 return rv;
3197 }
3198 break;
3199
3200 case CKA_CLASS:
3201 case CKA_KEY_TYPE:
3202 case CKA_SUBJECT:
3203 case CKA_ID:
3204 case CKA_LABEL:
3205 case CKA_EXTRACTABLE:
3206 break;
3207
3208 default:
3210 }
3211 }
3212 if (ecparams && template->obj.buf) {
3214 CK_RV rv =
3215 parse_ecparams(ecparams, ecparams_len, &template->algorithm, &key_len);
3216 if (rv != CKR_OK) {
3217 return rv;
3218 }
3219 if (key_len != template->objlen) {
3221 }
3222 } else {
3224 }
3225
3226 return CKR_OK;
3227}
union yubihsm_pkcs11_object_template::@114 obj
CK_SESSION_HANDLE CK_ATTRIBUTE_PTR CK_ULONG ulCount
CK_SESSION_HANDLE CK_ATTRIBUTE_PTR pTemplate
Here is the call graph for this function:

◆ parse_hex()

bool parse_hex ( CK_UTF8CHAR_PTR hex,
CK_ULONG hex_len,
uint8_t * parsed )

Definition at line 464 of file util_pkcs11.c.

464 {
465
466 int j = 0;
467
468 for (CK_ULONG i = 0; i < hex_len; i += 2) {
469 if (isxdigit(hex[i]) == 0 || isxdigit(hex[i + 1]) == 0) {
470 return false;
471 }
472
473 if (isdigit(hex[i])) {
474 parsed[j] = (hex[i] - '0') << 4;
475 } else {
476 parsed[j] = (tolower(hex[i]) - 'a' + 10) << 4;
477 }
478
479 if (isdigit(hex[i + 1])) {
480 parsed[j] |= (hex[i + 1] - '0');
481 } else {
482 parsed[j] |= (tolower(hex[i + 1]) - 'a' + 10);
483 }
484
485 j++;
486 }
487
488 return true;
489}
constexpr const char hex[]
uint16_t j

◆ parse_hmac_template()

CK_RV parse_hmac_template ( CK_ATTRIBUTE_PTR pTemplate,
CK_ULONG ulCount,
yubihsm_pkcs11_object_template * template,
bool generate )

Definition at line 3229 of file util_pkcs11.c.

3231 {
3232
3233 CK_RV rv;
3234
3235 for (CK_ULONG i = 0; i < ulCount; i++) {
3236 switch (pTemplate[i].type) {
3237
3238 case CKA_VALUE:
3239 if (generate == false && template->obj.buf == NULL) {
3240 // TODO: consider hanshing the key here if it's longer than blocklen
3241 template->obj.buf = (CK_BYTE_PTR) pTemplate[i].pValue;
3242 template->objlen = pTemplate[i].ulValueLen;
3243 } else {
3245 }
3246 break;
3247
3248 case CKA_SIGN:
3250 pTemplate[i].pValue)) != CKR_OK) {
3251 return rv;
3252 }
3253 break;
3254
3255 case CKA_VERIFY:
3257 pTemplate[i].pValue)) != CKR_OK) {
3258 return rv;
3259 }
3260 break;
3261
3262 case CKA_KEY_TYPE:
3263 switch (*((CK_ULONG_PTR) pTemplate[i].pValue)) {
3264 case CKK_SHA_1_HMAC:
3265 template->algorithm = YH_ALGO_HMAC_SHA1;
3266 break;
3267 case CKK_SHA256_HMAC:
3268 template->algorithm = YH_ALGO_HMAC_SHA256;
3269 break;
3270 case CKK_SHA384_HMAC:
3271 template->algorithm = YH_ALGO_HMAC_SHA384;
3272 break;
3273 case CKK_SHA512_HMAC:
3274 template->algorithm = YH_ALGO_HMAC_SHA512;
3275 break;
3276 default:
3278 }
3279 break;
3280
3281 case CKA_TOKEN:
3282 if ((rv = check_bool_attribute(pTemplate[i].pValue, true)) != CKR_OK) {
3283 return rv;
3284 }
3285 break;
3286
3287 case CKA_CLASS:
3288 case CKA_SUBJECT:
3289 case CKA_ID:
3290 case CKA_LABEL:
3291 case CKA_EXTRACTABLE:
3292 break;
3293
3294 default:
3296 }
3297 }
3298 if (template->algorithm && (generate == true || template->obj.buf)) {
3299 return CKR_OK;
3300 } else {
3302 }
3303}
#define CKK_SHA256_HMAC
Definition pkcs11.h:345
#define CKK_SHA384_HMAC
Definition pkcs11.h:346
#define CKK_SHA_1_HMAC
Definition pkcs11.h:344
#define CKK_SHA512_HMAC
Definition pkcs11.h:347
yubihsm_pkcs11_attribute verify
Here is the call graph for this function:

◆ parse_id_value()

int parse_id_value ( void * value,
CK_ULONG len )

Definition at line 3556 of file util_pkcs11.c.

3556 {
3557 switch (len) {
3558 case 0:
3559 return 0;
3560 case 1:
3561 return *(uint8_t *) value;
3562 case 2:
3563 return ntohs(*(uint16_t *) value);
3564 default:
3565 DBG_INFO("Supplied id is long, truncating it (was %lu bytes)", len);
3566 return ntohs(*(uint16_t *) value);
3567 }
3568}
#define DBG_INFO(...)
Definition debug_lib.h:63
size_t len
Here is the caller graph for this function:

◆ parse_rsa_generate_template()

CK_RV parse_rsa_generate_template ( CK_ATTRIBUTE_PTR pPublicKeyTemplate,
CK_ULONG ulPublicKeyAttributeCount,
CK_ATTRIBUTE_PTR pPrivateKeyTemplate,
CK_ULONG ulPrivateKeyAttributeCount,
yubihsm_pkcs11_object_template * template )

Definition at line 3305 of file util_pkcs11.c.

3309 {
3310
3311 uint8_t *e = NULL;
3312 bool label_set = FALSE;
3313 CK_RV rv;
3314
3315 memset(template->label, 0, sizeof(template->label));
3316
3317 for (CK_ULONG i = 0; i < ulPublicKeyAttributeCount; i++) {
3318 switch (pPublicKeyTemplate[i].type) {
3319 case CKA_CLASS:
3320 if (*((CK_ULONG_PTR) pPublicKeyTemplate[i].pValue) != CKO_PUBLIC_KEY) {
3321 DBG_ERR("CKA_CLASS inconsistent in PublicKeyTemplate");
3323 }
3324 break;
3325
3326 case CKA_KEY_TYPE:
3327 if (*((CK_ULONG_PTR) pPublicKeyTemplate[i].pValue) != CKK_RSA) {
3328 DBG_ERR("CKA_KEY_TYPE inconsistent in PublicKeyTemplate");
3330 }
3331 break;
3332
3333 case CKA_ID:
3334 if (template->id == 0) {
3335 int id = parse_id_value(pPublicKeyTemplate[i].pValue,
3336 pPublicKeyTemplate[i].ulValueLen);
3337 if (id == -1) {
3338 DBG_ERR("CKA_ID invalid in PublicKeyTemplate");
3340 }
3341 template->id = id;
3342 } else {
3343 DBG_ERR("CKA_ID inconsistent in PublicKeyTemplate");
3345 }
3346 break;
3347
3349 if (e == NULL) {
3350 e = (CK_BYTE_PTR) pPublicKeyTemplate[i].pValue;
3351 if (!((pPublicKeyTemplate[i].ulValueLen == 3 &&
3352 memcmp(e, "\x01\x00\x01", 3) == 0) ||
3353 (pPublicKeyTemplate[i].ulValueLen == 4 &&
3354 memcmp(e, "\x00\x01\x00\x01", 4) == 0))) {
3355 DBG_ERR("CKA_PUBLIC_EXPONENT invalid in PublicKeyTemplate");
3357 }
3358 } else {
3359 DBG_ERR("CKA_PUBLIC_EXPONENT inconsistent in PublicKeyTemplate");
3361 }
3362 break;
3363
3364 case CKA_MODULUS_BITS:
3365 switch (*((CK_ULONG_PTR) pPublicKeyTemplate[i].pValue)) {
3366 case 2048:
3367 template->algorithm = YH_ALGO_RSA_2048;
3368 break;
3369
3370 case 3072:
3371 template->algorithm = YH_ALGO_RSA_3072;
3372 break;
3373
3374 case 4096:
3375 template->algorithm = YH_ALGO_RSA_4096;
3376 break;
3377
3378 default:
3379 DBG_ERR("CKA_MODULUS_BITS wrong length in PublicKeyTemplate (%lu)",
3380 *((CK_ULONG_PTR) pPublicKeyTemplate[i].pValue));
3382 }
3383 break;
3384
3385 case CKA_LABEL:
3386 if (pPublicKeyTemplate[i].ulValueLen > YH_OBJ_LABEL_LEN) {
3387 DBG_ERR("CKA_LABEL invalid in PublicKeyTemplate");
3389 }
3390
3392 pPublicKeyTemplate[i].ulValueLen);
3393
3394 label_set = TRUE;
3395
3396 break;
3397
3398 case CKA_TOKEN:
3399 if ((rv = check_bool_attribute(pPublicKeyTemplate[i].pValue, true)) !=
3400 CKR_OK) {
3401 DBG_ERR("Boolean truth check failed for attribute 0x%lx",
3402 pPublicKeyTemplate[i].type);
3403 return rv;
3404 }
3405 break;
3406
3407 case CKA_MODIFIABLE:
3408 case CKA_DECRYPT:
3409 case CKA_SIGN:
3410 case CKA_UNWRAP:
3411 if ((rv = check_bool_attribute(pPublicKeyTemplate[i].pValue, false)) !=
3412 CKR_OK) {
3413 DBG_ERR("Boolean false check failed for attribute 0x%lx",
3414 pPublicKeyTemplate[i].type);
3415 return rv;
3416 }
3417 break;
3418
3419 case CKA_WRAP:
3420 case CKA_VERIFY:
3421 case CKA_ENCRYPT:
3422 case CKA_EXTRACTABLE:
3423 case CKA_PRIVATE:
3424 case CKA_COPYABLE:
3425 case CKA_DESTROYABLE:
3426 break;
3427
3428 default:
3429 DBG_ERR("invalid attribute type in PublicKeyTemplate: 0x%lx",
3430 pPublicKeyTemplate[i].type);
3432 }
3433 }
3434
3435 for (CK_ULONG i = 0; i < ulPrivateKeyAttributeCount; i++) {
3436 switch (pPrivateKeyTemplate[i].type) {
3437 case CKA_CLASS:
3438 if (*((CK_ULONG_PTR) pPrivateKeyTemplate[i].pValue) !=
3440 DBG_ERR("CKA_CLASS inconsistent in PrivateKeyTemplate");
3442 }
3443 break;
3444
3445 case CKA_KEY_TYPE:
3446 if (*((CK_ULONG_PTR) pPrivateKeyTemplate[i].pValue) != CKK_RSA) {
3447 DBG_ERR("CKA_KEY_TYPE inconsistent in PrivateKeyTemplate");
3449 }
3450 break;
3451
3452 case CKA_ID: {
3453 int id = parse_id_value(pPrivateKeyTemplate[i].pValue,
3454 pPrivateKeyTemplate[i].ulValueLen);
3455 if (id == -1) {
3456 DBG_ERR("CKA_ID invalid in PrivateKeyTemplate");
3458 }
3459 if (template->id != 0 && template->id != id) {
3460 DBG_ERR("CKA_ID inconsistent in PrivateKeyTemplate");
3462 } else {
3463 template->id = id;
3464 }
3465 } break;
3466
3467 case CKA_DECRYPT:
3469 pPrivateKeyTemplate[i].pValue)) !=
3470 CKR_OK) {
3471 DBG_ERR("CKA_DECRYPT inconsistent in PrivateKeyTemplate");
3472 return rv;
3473 }
3474 break;
3475
3476 case CKA_SIGN:
3478 pPrivateKeyTemplate[i].pValue)) !=
3479 CKR_OK) {
3480 DBG_ERR("CKA_SIGN inconsistent in PrivateKeyTemplate");
3481 return rv;
3482 }
3483 break;
3484
3485 case CKA_EXTRACTABLE:
3487 pPrivateKeyTemplate[i].pValue)) !=
3488 CKR_OK) {
3489 DBG_ERR("CKA_EXTRACTABLE inconsistent in PrivateKeyTemplate");
3490 return rv;
3491 }
3492 break;
3493
3494 case CKA_LABEL:
3495 if (pPrivateKeyTemplate[i].ulValueLen > YH_OBJ_LABEL_LEN) {
3496 DBG_ERR("CKA_LABEL invalid in PrivateKeyTemplate");
3498 }
3499
3500 if (label_set == TRUE) {
3501 if (memcmp(template->label, pPrivateKeyTemplate[i].pValue,
3502 pPrivateKeyTemplate[i].ulValueLen) != 0) {
3503 DBG_ERR("CKA_LABEL inconsistent in PrivateKeyTemplate");
3505 }
3506 } else {
3508 pPrivateKeyTemplate[i].ulValueLen);
3509 }
3510 break;
3511
3512 case CKA_TOKEN:
3513 case CKA_SENSITIVE:
3514 case CKA_PRIVATE:
3515 case CKA_DESTROYABLE:
3516 if ((rv = check_bool_attribute(pPrivateKeyTemplate[i].pValue, true)) !=
3517 CKR_OK) {
3518 DBG_ERR("Boolean truth check failed for attribute 0x%lx",
3519 pPrivateKeyTemplate[i].type);
3520 return rv;
3521 }
3522 break;
3523
3524 case CKA_WRAP:
3525 case CKA_MODIFIABLE:
3526 case CKA_COPYABLE:
3527 case CKA_ENCRYPT:
3528 case CKA_VERIFY:
3529 if ((rv = check_bool_attribute(pPrivateKeyTemplate[i].pValue, false)) !=
3530 CKR_OK) {
3531 DBG_ERR("Boolean false check failed for attribute 0x%lx",
3532 pPrivateKeyTemplate[i].type);
3533 return rv;
3534 }
3535 break;
3536
3537 case CKA_UNWRAP:
3538 case CKA_SUBJECT:
3539 break;
3540
3541 default:
3542 DBG_ERR("invalid attribute type in PrivateKeyTemplate: 0x%lx",
3543 pPrivateKeyTemplate[i].type);
3545 }
3546 }
3547
3548 if (template->algorithm == 0) {
3549 DBG_ERR("No RSA bitlength set");
3551 }
3552
3553 return CKR_OK;
3554}
#define CKK_RSA
Definition pkcs11.h:319
#define CKA_PUBLIC_EXPONENT
Definition pkcs11.h:400
#define CKA_MODULUS_BITS
Definition pkcs11.h:399
yubihsm_pkcs11_attribute decrypt
Here is the call graph for this function:
Here is the caller graph for this function:

◆ parse_rsa_template()

CK_RV parse_rsa_template ( CK_ATTRIBUTE_PTR pTemplate,
CK_ULONG ulCount,
yubihsm_pkcs11_object_template * template )

Definition at line 2976 of file util_pkcs11.c.

2977 {
2978
2979 uint8_t *e = NULL;
2980 uint16_t primelen = 0;
2981 CK_RV rv;
2982
2983 for (CK_ULONG i = 0; i < ulCount; i++) {
2984 switch (pTemplate[i].type) {
2985
2986 case CKA_PRIME_1:
2987 if (template->obj.rsa.p == NULL) {
2988 template->obj.rsa.p = (CK_BYTE_PTR) pTemplate[i].pValue;
2989 if (pTemplate[i].ulValueLen % 2 != 0) {
2990 pTemplate[i].ulValueLen--;
2991 template->obj.rsa.p++;
2992 }
2993 if (primelen == 0 || primelen == pTemplate[i].ulValueLen) {
2994 primelen = pTemplate[i].ulValueLen;
2995 } else {
2997 }
2998 } else {
3000 }
3001 break;
3002
3003 case CKA_PRIME_2:
3004 if (template->obj.rsa.q == NULL) {
3005 template->obj.rsa.q = (CK_BYTE_PTR) pTemplate[i].pValue;
3006 if (pTemplate[i].ulValueLen % 2 != 0) {
3007 pTemplate[i].ulValueLen--;
3008 template->obj.rsa.q++;
3009 }
3010 if (primelen == 0 || primelen == pTemplate[i].ulValueLen) {
3011 primelen = pTemplate[i].ulValueLen;
3012 } else {
3014 }
3015 } else {
3017 }
3018 break;
3019
3021 if (e == NULL) {
3022 e = (CK_BYTE_PTR) pTemplate[i].pValue;
3023 if (pTemplate[i].ulValueLen != 3 ||
3024 memcmp(e, "\x01\x00\x01", 3) != 0) {
3026 }
3027 } else {
3029 }
3030 break;
3031
3032 case CKA_SIGN:
3034 pTemplate[i].pValue)) != CKR_OK) {
3035 return rv;
3036 }
3037 break;
3038
3039 case CKA_DECRYPT:
3041 pTemplate[i].pValue)) != CKR_OK) {
3042 return rv;
3043 }
3044 break;
3045
3046 case CKA_TOKEN:
3047 case CKA_PRIVATE:
3048 case CKA_SENSITIVE:
3049 if ((rv = check_bool_attribute(pTemplate[i].pValue, true)) != CKR_OK) {
3050 return rv;
3051 }
3052 break;
3053
3054 case CKA_MODULUS:
3056 case CKA_EXPONENT_1:
3057 case CKA_EXPONENT_2:
3058 case CKA_COEFFICIENT:
3059 case CKA_CLASS:
3060 case CKA_KEY_TYPE:
3061 case CKA_SUBJECT:
3062 case CKA_ID:
3063 case CKA_LABEL:
3064 case CKA_EXTRACTABLE:
3065 break;
3066
3067 default:
3069 }
3070 }
3071 if (e && template->obj.rsa.p && template->obj.rsa.q) {
3072 template->objlen = primelen;
3073 switch (primelen) {
3074 case 128:
3075 template->algorithm = YH_ALGO_RSA_2048;
3076 break;
3077 case 192:
3078 template->algorithm = YH_ALGO_RSA_3072;
3079 break;
3080 case 256:
3081 template->algorithm = YH_ALGO_RSA_4096;
3082 break;
3083 default:
3085 }
3086 } else {
3088 }
3089
3090 return CKR_OK;
3091}
#define CKA_PRIVATE_EXPONENT
Definition pkcs11.h:401
#define CKA_MODULUS
Definition pkcs11.h:398
#define CKA_PRIME_1
Definition pkcs11.h:402
#define CKA_EXPONENT_1
Definition pkcs11.h:404
#define CKA_EXPONENT_2
Definition pkcs11.h:405
#define CKA_PRIME_2
Definition pkcs11.h:403
#define CKA_COEFFICIENT
Definition pkcs11.h:406
struct yubihsm_pkcs11_object_template::@114::@115 rsa
Here is the call graph for this function:

◆ parse_wrap_template()

CK_RV parse_wrap_template ( CK_ATTRIBUTE_PTR pTemplate,
CK_ULONG ulCount,
yubihsm_pkcs11_object_template * template,
bool generate )

Definition at line 3801 of file util_pkcs11.c.

3803 {
3804
3805 CK_RV rv;
3806
3807 for (CK_ULONG i = 0; i < ulCount; i++) {
3808 switch (pTemplate[i].type) {
3809
3810 case CKA_VALUE:
3811 if (generate == false && template->obj.buf == NULL) {
3812 template->obj.buf = (CK_BYTE_PTR) pTemplate[i].pValue;
3813 template->objlen = pTemplate[i].ulValueLen;
3814 } else {
3816 }
3817 break;
3818
3819 case CKA_WRAP:
3821 pTemplate[i].pValue)) != CKR_OK) {
3822 return rv;
3823 }
3824 break;
3825
3826 case CKA_UNWRAP:
3828 pTemplate[i].pValue)) != CKR_OK) {
3829 return rv;
3830 }
3831 break;
3832
3833 case CKA_ENCRYPT:
3835 pTemplate[i].pValue)) != CKR_OK) {
3836 return rv;
3837 }
3838 break;
3839
3840 case CKA_DECRYPT:
3842 pTemplate[i].pValue)) != CKR_OK) {
3843 return rv;
3844 }
3845 break;
3846
3847 case CKA_TOKEN:
3848 if ((rv = check_bool_attribute(pTemplate[i].pValue, true)) != CKR_OK) {
3849 return rv;
3850 }
3851 break;
3852
3853 case CKA_KEY_TYPE:
3854 case CKA_CLASS:
3855 case CKA_SUBJECT:
3856 case CKA_ID:
3857 case CKA_LABEL:
3858 case CKA_EXTRACTABLE:
3859 break;
3860
3861 default:
3863 }
3864 }
3865 if (generate == true || template->obj.buf) {
3866 return CKR_OK;
3867 } else {
3869 }
3870}
yubihsm_pkcs11_attribute wrap
yubihsm_pkcs11_attribute encrypt
yubihsm_pkcs11_attribute unwrap
Here is the call graph for this function:

◆ perform_decrypt()

CK_RV perform_decrypt ( yh_session * session,
yubihsm_pkcs11_op_info * op_info,
uint8_t * ciphertext,
uint16_t * ciphertext_len )

Definition at line 2463 of file util_pkcs11.c.

2464 {
2465
2466 yh_rc yrc;
2467 size_t outlen = sizeof(op_info->buffer);
2468
2469 if (op_info->mechanism.mechanism == CKM_RSA_PKCS) {
2471 op_info->buffer, op_info->buffer_length,
2472 op_info->buffer, &outlen);
2473 } else if (op_info->mechanism.mechanism == CKM_RSA_PKCS_OAEP) {
2474 yrc =
2475 yh_util_decrypt_oaep(session, op_info->op.decrypt.key_id, op_info->buffer,
2476 op_info->buffer_length, op_info->buffer, &outlen,
2477 op_info->mechanism.oaep.label,
2478 op_info->mechanism.oaep.label_len,
2479 op_info->mechanism.oaep.mgf1Algo);
2480 } else if (op_info->mechanism.mechanism == CKM_YUBICO_AES_CCM_WRAP) {
2481 yrc =
2482 yh_util_unwrap_data(session, op_info->op.decrypt.key_id, op_info->buffer,
2483 op_info->buffer_length, op_info->buffer, &outlen);
2484 } else {
2485 DBG_ERR("Mechanism %lu not supported", op_info->mechanism.mechanism);
2486 return CKR_MECHANISM_INVALID;
2487 }
2488
2489 if (yrc != YHR_SUCCESS) {
2490 DBG_ERR("Decryption failed: %s", yh_strerror(yrc));
2491 return CKR_FUNCTION_FAILED;
2492 }
2493
2494 if (outlen > *data_len) {
2495 DBG_ERR("Data won't fit in buffer %zu > %d", outlen, *data_len);
2496 *data_len = outlen;
2497 return CKR_BUFFER_TOO_SMALL;
2498 }
2499 memcpy(data, op_info->buffer, outlen);
2500 *data_len = outlen;
2501
2502 return CKR_OK;
2503}
const char * yh_strerror(yh_rc err)
Definition error.c:65
uint16_t key_id
struct mechanism::@110::@112 oaep
yh_rc yh_util_unwrap_data(yh_session *session, uint16_t key_id, const uint8_t *in, size_t in_len, uint8_t *out, size_t *out_len)
Definition yubihsm.c:3716
yh_rc yh_util_decrypt_pkcs1v1_5(yh_session *session, uint16_t key_id, const uint8_t *in, size_t in_len, uint8_t *out, size_t *out_len)
Definition yubihsm.c:2059
yh_rc yh_util_decrypt_oaep(yh_session *session, uint16_t key_id, const uint8_t *in, size_t in_len, uint8_t *out, size_t *out_len, const uint8_t *label, size_t label_len, yh_algorithm mgf1Algo)
Definition yubihsm.c:2107
Here is the call graph for this function:

◆ perform_digest()

CK_RV perform_digest ( yubihsm_pkcs11_op_info * op_info,
uint8_t * digest,
uint16_t * digest_len )

Definition at line 2533 of file util_pkcs11.c.

2534 {
2535
2536 if (op_info->buffer_length > *digest_len) {
2537 return CKR_BUFFER_TOO_SMALL;
2538 }
2539
2540 memcpy(digest, op_info->buffer, op_info->buffer_length);
2541 *digest_len = op_info->buffer_length;
2542
2543 return CKR_OK;
2544}
session operation op digest digest_len
Here is the call graph for this function:

◆ perform_encrypt()

CK_RV perform_encrypt ( yh_session * session,
yubihsm_pkcs11_op_info * op_info,
uint8_t * plaintext,
uint16_t * plaintext_len )

Definition at line 2505 of file util_pkcs11.c.

2506 {
2507
2508 yh_rc yrc;
2509 size_t outlen = sizeof(op_info->buffer);
2510
2512 yrc =
2513 yh_util_wrap_data(session, op_info->op.decrypt.key_id, op_info->buffer,
2514 op_info->buffer_length, op_info->buffer, &outlen);
2515 } else {
2516 DBG_ERR("Mechanism %lu not supported", op_info->mechanism.mechanism);
2517 return CKR_MECHANISM_INVALID;
2518 }
2519
2520 if (yrc != YHR_SUCCESS) {
2521 return CKR_FUNCTION_FAILED;
2522 }
2523
2524 if (outlen > *data_len) {
2525 return CKR_BUFFER_TOO_SMALL;
2526 }
2527 memcpy(data, op_info->buffer, outlen);
2528 *data_len = outlen;
2529
2530 return CKR_OK;
2531}
yh_rc yh_util_wrap_data(yh_session *session, uint16_t key_id, const uint8_t *in, size_t in_len, uint8_t *out, size_t *out_len)
Definition yubihsm.c:3667
Here is the call graph for this function:

◆ perform_signature()

CK_RV perform_signature ( yh_session * session,
yubihsm_pkcs11_op_info * op_info,
uint8_t * signature,
uint16_t * signature_len )

Definition at line 2410 of file util_pkcs11.c.

2411 {
2412
2413 yh_rc yrc;
2414 size_t outlen = sizeof(op_info->buffer);
2415
2418 yrc = yh_util_sign_pss(session, op_info->op.sign.key_id, op_info->buffer,
2419 op_info->buffer_length, op_info->buffer, &outlen,
2420 op_info->mechanism.pss.salt_len,
2421 op_info->mechanism.pss.mgf1Algo);
2422 } else {
2425 op_info->mechanism.mechanism),
2426 op_info->buffer, op_info->buffer_length,
2427 op_info->buffer, &outlen);
2428 }
2429 } else if (is_ECDSA_sign_mechanism(op_info->mechanism.mechanism)) {
2430 yrc = yh_util_sign_ecdsa(session, op_info->op.sign.key_id, op_info->buffer,
2431 op_info->buffer_length, op_info->buffer, &outlen);
2432 } else if (is_HMAC_sign_mechanism(op_info->mechanism.mechanism)) {
2433 yrc = yh_util_sign_hmac(session, op_info->op.sign.key_id, op_info->buffer,
2434 op_info->buffer_length, op_info->buffer, &outlen);
2435
2436 } else {
2437 DBG_ERR("Mechanism %lu not supported", op_info->mechanism.mechanism);
2438 return CKR_MECHANISM_INVALID;
2439 }
2440
2441 if (yrc != YHR_SUCCESS) {
2442 return CKR_FUNCTION_FAILED;
2443 }
2444
2446 // NOTE(adma): ECDSA, we must remove the DER encoding and only
2447 // return R,S as required by the specs
2448 if (strip_DER_encoding_from_ECSIG(op_info->buffer, &outlen,
2449 op_info->op.sign.sig_len) == false) {
2450 return CKR_FUNCTION_FAILED;
2451 }
2452 }
2453
2454 if (outlen > *signature_len) {
2455 return CKR_BUFFER_TOO_SMALL;
2456 }
2457 memcpy(signature, op_info->buffer, outlen);
2458 *signature_len = outlen;
2459
2460 return CKR_OK;
2461}
struct mechanism::@110::@113 pss
uint16_t key_id
bool is_PSS_sign_mechanism(CK_MECHANISM_TYPE m)
yh_rc yh_util_sign_ecdsa(yh_session *session, uint16_t key_id, const uint8_t *in, size_t in_len, uint8_t *out, size_t *out_len)
Definition yubihsm.c:1411
yh_rc yh_util_sign_hmac(yh_session *session, uint16_t key_id, const uint8_t *in, size_t in_len, uint8_t *out, size_t *out_len)
Definition yubihsm.c:1520
yh_rc yh_util_sign_pss(yh_session *session, uint16_t key_id, const uint8_t *in, size_t in_len, uint8_t *out, size_t *out_len, size_t salt_len, yh_algorithm mgf1Algo)
Definition yubihsm.c:1346
yh_rc yh_util_sign_pkcs1v1_5(yh_session *session, uint16_t key_id, bool hashed, const uint8_t *in, size_t in_len, uint8_t *out, size_t *out_len)
Definition yubihsm.c:1287
Here is the call graph for this function:

◆ perform_verify()

CK_RV perform_verify ( yh_session * session,
yubihsm_pkcs11_op_info * op_info,
uint8_t * signature,
uint16_t signature_len )

Definition at line 2225 of file util_pkcs11.c.

2226 {
2227
2229 yh_rc yrc;
2230 bool verified = false;
2231
2232 yrc = yh_util_verify_hmac(session, op_info->op.verify.key_id, signature,
2233 signature_len, op_info->buffer,
2234 op_info->buffer_length, &verified);
2235
2236 if (yrc != YHR_SUCCESS) {
2237 return CKR_FUNCTION_FAILED;
2238 }
2239
2240 if (verified == false) {
2241 return CKR_SIGNATURE_INVALID;
2242 }
2243
2244 return CKR_OK;
2245 } else {
2246 CK_RV rv;
2247 EVP_PKEY *key = EVP_PKEY_new();
2248 uint8_t md_data[EVP_MAX_MD_SIZE];
2249 uint8_t *md = md_data;
2250 unsigned int md_len = sizeof(md_data);
2251 EVP_PKEY_CTX *ctx = NULL;
2252
2253 if (key == NULL) {
2255 goto pv_failure;
2256 }
2257
2258 if (load_public_key(session, op_info->op.verify.key_id, key) == false) {
2260 goto pv_failure;
2261 }
2262
2263 ctx = EVP_PKEY_CTX_new(key, NULL);
2264 if (ctx == NULL) {
2266 goto pv_failure;
2267 }
2268 if (EVP_PKEY_verify_init(ctx) <= 0) {
2270 goto pv_failure;
2271 }
2272
2273 int res;
2274 unsigned char data[2048];
2275 if (is_hashed_mechanism(op_info->mechanism.mechanism)) {
2276 if (EVP_DigestFinal_ex(op_info->op.verify.md_ctx, md, &md_len) <= 0) {
2278 goto pv_failure;
2279 }
2280 } else if (EVP_PKEY_base_id(key) == EVP_PKEY_RSA) {
2281 const EVP_MD *md_type;
2282 int di_len;
2283
2284 if (op_info->mechanism.mechanism == CKM_RSA_PKCS_PSS) {
2285 md = op_info->buffer;
2286 md_len = op_info->buffer_length;
2287 } else {
2288 parse_NID(op_info->buffer, op_info->buffer_length, &md_type, &di_len);
2289 if (md_type == EVP_md_null()) {
2291 goto pv_failure;
2292 }
2293
2294 op_info->op.verify.md = md_type;
2295 md = op_info->buffer + di_len;
2296 md_len = op_info->buffer_length - di_len;
2297 }
2298 } else if (EVP_PKEY_base_id(key) == EVP_PKEY_EC) {
2299 md = op_info->buffer;
2300 md_len = op_info->buffer_length;
2301 if (md_len == 20) {
2302 op_info->op.verify.md = EVP_sha1();
2303 } else if (md_len == 32) {
2304 op_info->op.verify.md = EVP_sha256();
2305 } else if (md_len == 48) {
2306 op_info->op.verify.md = EVP_sha384();
2307 } else if (md_len == 64) {
2308 op_info->op.verify.md = EVP_sha512();
2309 } else {
2311 goto pv_failure;
2312 }
2313 } else {
2315 goto pv_failure;
2316 }
2317 if (EVP_PKEY_CTX_set_signature_md(ctx, op_info->op.verify.md) <= 0) {
2319 goto pv_failure;
2320 }
2321 if (op_info->op.verify.padding) {
2322 if (EVP_PKEY_CTX_set_rsa_padding(ctx, op_info->op.verify.padding) <= 0) {
2324 goto pv_failure;
2325 }
2326 if (op_info->op.verify.padding == RSA_PKCS1_PSS_PADDING) {
2327 if (EVP_PKEY_CTX_set_rsa_pss_saltlen(ctx, op_info->op.verify.saltLen) <=
2328 0) {
2330 goto pv_failure;
2331 }
2332 if (EVP_PKEY_CTX_set_rsa_mgf1_md(ctx, op_info->op.verify.mgf1md) <= 0) {
2334 goto pv_failure;
2335 }
2336 }
2337 }
2339 memcpy(data, signature, signature_len);
2340 signature = data;
2341 if (apply_DER_encoding_to_ECSIG(signature, &signature_len) == false) {
2342 DBG_ERR("Failed to apply DER encoding to ECDSA signature");
2344 goto pv_failure;
2345 }
2346 }
2347 res = EVP_PKEY_verify(ctx, signature, signature_len, md, md_len);
2348
2349 if (res == 1) {
2350 rv = CKR_OK;
2351 } else if (res == 0) {
2353 } else {
2355 }
2356
2357 pv_failure:
2358 if (ctx != NULL) {
2359 EVP_PKEY_CTX_free(ctx);
2360 ctx = NULL;
2361 }
2362
2363 if (key != NULL) {
2364 EVP_PKEY_free(key);
2365 key = NULL;
2366 }
2367
2368 return rv;
2369 }
2370
2371 return CKR_FUNCTION_FAILED;
2372}
fc::array< char, 72 > signature
Definition elliptic.hpp:25
#define CKR_DATA_INVALID
Definition pkcs11.h:1106
#define CKR_SIGNATURE_INVALID
Definition pkcs11.h:1144
uint16_t key_id
void parse_NID(uint8_t *data, uint16_t data_len, const EVP_MD **md_type, int *digestinfo_len)
Definition util.c:452
yh_rc yh_util_verify_hmac(yh_session *session, uint16_t key_id, const uint8_t *signature, size_t signature_len, const uint8_t *data, size_t data_len, bool *verified)
Definition yubihsm.c:1939
Here is the call graph for this function:

◆ populate_template()

CK_RV populate_template ( int type,
void * object,
CK_ATTRIBUTE_PTR pTemplate,
CK_ULONG ulCount,
yh_session * session )

Definition at line 3872 of file util_pkcs11.c.

3873 {
3874
3875 CK_RV rv = CKR_OK;
3876
3877 for (CK_ULONG i = 0; i < ulCount; i++) {
3878 DBG_INFO("Getting attribute 0x%lx", pTemplate[i].type);
3879
3880 CK_VOID_PTR object_ptr;
3881 if (pTemplate[i].pValue == NULL) {
3882 // NOTE(adma): just asking for the length
3883 object_ptr = NULL;
3884 DBG_INFO("Retrieving length");
3885 } else {
3886 // NOTE(adma): actually get the attribute
3887 object_ptr = pTemplate[i].pValue;
3888 DBG_INFO("Retrieving attribute");
3889 }
3890
3891 CK_RV attribute_rc;
3892 if (type == ECDH_KEY_TYPE) {
3893 ecdh_session_key *key = object;
3894 attribute_rc =
3895 get_attribute_ecsession_key(pTemplate[i].type, key, object_ptr,
3896 &pTemplate[i].ulValueLen);
3897 } else {
3899 attribute_rc = get_attribute(pTemplate[i].type, &desc->object, object_ptr,
3900 &pTemplate[i].ulValueLen, session);
3901 }
3902
3903 if (attribute_rc != CKR_OK) {
3904 rv = attribute_rc;
3905 if (attribute_rc == CKR_ATTRIBUTE_TYPE_INVALID) {
3906 DBG_ERR("Unable to get attribute");
3907 } else if (attribute_rc == CKR_BUFFER_TOO_SMALL) {
3908 DBG_ERR("Skipping attribute because buffer is too small");
3909 } else {
3910 DBG_ERR("Get attribute failed. %s", yh_strerror(attribute_rc));
3911 }
3912 } else {
3913 DBG_INFO("Attribute/length successfully returned with length %lu",
3914 pTemplate[i].ulValueLen);
3915 }
3916
3917 // NOTE(adma): Array of attributes like CKA_WRAP_TEMPLATE are special
3918 // cases.
3919 /* In the special case of an attribute whose value is an array of
3920 * attributes, for example CKA_WRAP_TEMPLATE, where it is passed in with
3921 * pValue not NULL, then if the pValue of elements within the array is
3922 * NULL_PTR then the ulValueLen of elements within the array will be set
3923 * to the required length. If the pValue of elements within the array is
3924 * not NULL_PTR, then the ulValueLen element of attributes within the
3925 * array MUST reflect the space that the corresponding pValue points to,
3926 * and pValue is filled in if there is sufficient room. Therefore it is
3927 * important to initialize the contents of a buffer before calling
3928 * C_GetAttributeValue to get such an array value. If any ulValueLen
3929 * within the array isn't large enough, it will be set to
3930 * CK_UNAVAILABLE_INFORMATION and the function will return
3931 * CKR_BUFFER_TOO_SMALL, as it does if an attribute in the pTemplate
3932 * argument has ulValueLen too small. Note that any attribute whose value
3933 * is an array of attributes is identifiable by virtue of the attribute
3934 * type having the CKF_ARRAY_ATTRIBUTE bit set.*/
3935 }
3936
3937 return rv;
3938}
CK_RV get_attribute_ecsession_key(CK_ATTRIBUTE_TYPE type, ecdh_session_key *key, CK_VOID_PTR value, CK_ULONG_PTR length)
CK_RV get_attribute(CK_ATTRIBUTE_TYPE type, yh_object_descriptor *object, CK_VOID_PTR value, CK_ULONG_PTR length, yh_session *session)
#define ECDH_KEY_TYPE
Here is the call graph for this function:

◆ release_session()

void release_session ( yubihsm_pkcs11_context * ctx,
yubihsm_pkcs11_session * session )

Definition at line 2850 of file util_pkcs11.c.

2851 {
2852
2853 release_slot(ctx, session->slot);
2854}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ release_slot()

void release_slot ( yubihsm_pkcs11_context * ctx,
yubihsm_pkcs11_slot * slot )

Definition at line 2782 of file util_pkcs11.c.

2782 {
2783
2784 if (slot->mutex != NULL) {
2785 ctx->unlock_mutex(slot->mutex);
2786 }
2787}
CK_UNLOCKMUTEX unlock_mutex
Here is the caller graph for this function:

◆ set_native_locking()

void set_native_locking ( yubihsm_pkcs11_context * ctx)

Definition at line 2916 of file util_pkcs11.c.

2916 {
2917
2918 ctx->create_mutex = native_create_mutex;
2919 ctx->destroy_mutex = native_destroy_mutex;
2920 ctx->lock_mutex = native_lock_mutex;
2921 ctx->unlock_mutex = native_unlock_mutex;
2922}
CK_DESTROYMUTEX destroy_mutex
Here is the caller graph for this function:

◆ set_template_attribute()

CK_RV set_template_attribute ( yubihsm_pkcs11_attribute * attribute,
void * value )

Definition at line 2953 of file util_pkcs11.c.

2953 {
2954 if (*attribute == ATTRIBUTE_NOT_SET) {
2955 if ((*(CK_BBOOL *) value) == true) {
2956 *attribute = ATTRIBUTE_TRUE;
2957 } else {
2958 *attribute = ATTRIBUTE_FALSE;
2959 }
2960 return CKR_OK;
2961 } else {
2963 }
2964}
@ ATTRIBUTE_NOT_SET
@ ATTRIBUTE_TRUE
@ ATTRIBUTE_FALSE
Here is the caller graph for this function:

◆ sign_mechanism_cleanup()

bool sign_mechanism_cleanup ( yubihsm_pkcs11_op_info * op_info)

Definition at line 2546 of file util_pkcs11.c.

2546 {
2547
2548 if (op_info->op.sign.md_ctx != NULL) {
2549 EVP_MD_CTX_destroy(op_info->op.sign.md_ctx);
2550 op_info->op.sign.md_ctx = NULL;
2551 }
2552
2553 return true;
2554}

◆ validate_derive_key_attribute()

CK_RV validate_derive_key_attribute ( CK_ATTRIBUTE_TYPE type,
void * value )

Definition at line 3940 of file util_pkcs11.c.

3940 {
3941 switch (type) {
3942 case CKA_TOKEN:
3943 if (*((CK_BBOOL *) value) == CK_TRUE) {
3944 DBG_ERR("Derived key can only be a session object");
3946 }
3947 break;
3948
3949 case CKA_CLASS:
3950 if (*((CK_ULONG_PTR) value) != CKO_SECRET_KEY) {
3951 DBG_ERR("Derived key class is unsupported");
3953 }
3954 break;
3955
3956 case CKA_KEY_TYPE:
3958 DBG_ERR("Derived key type is unsupported");
3960 }
3961 break;
3962
3963 case CKA_EXTRACTABLE:
3964 if (*((CK_BBOOL *) value) == CK_FALSE) {
3965 DBG_ERR("The derived key will be extractable");
3967 }
3968 break;
3969
3970 default:
3971 DBG_WARN("ECDH key derive template contains the ignored attribute: %lx",
3972 type);
3973 break;
3974 }
3975
3976 return CKR_OK;
3977}
#define DBG_WARN(...)
Definition debug_p11.h:36

◆ verify_mechanism_cleanup()

bool verify_mechanism_cleanup ( yubihsm_pkcs11_op_info * op_info)

Definition at line 2556 of file util_pkcs11.c.

2556 {
2557
2558 if (op_info->op.verify.md_ctx != NULL) {
2559 EVP_MD_CTX_destroy(op_info->op.verify.md_ctx);
2560 op_info->op.verify.md_ctx = NULL;
2561 }
2562
2563 return true;
2564}