libhsm.h File Reference
`#include <stdint.h>`
`#include <ldns/rbtree.h>`
`#include <pthread.h>`

Go to the source code of this file.

## Data Structures

struct | hsm_config_t
struct | hsm_module_t
struct | hsm_session_t
struct | libhsm_key_t
struct | libhsm_key_info_t
struct | hsm_repository_struct
struct | hsm_ctx_t

## Macros

#define | HSM_MAX_SESSIONS 100
#define | HSM_MAX_SESSIONS 100
#define | HSM_MAX_ALGONAME 16
#define | HSM_ERROR_MSGSIZE 512
#define | HSM_MAX_SIGNATURE_LENGTH 512
#define | HSM_MAX_PIN_LENGTH 255
#define | HSM_OK 0
#define | HSM_ERROR 0x10000001
#define | HSM_PIN_INCORRECT 0x10000002
#define | HSM_CONFIG_FILE_ERROR 0x10000003
#define | HSM_REPOSITORY_NOT_FOUND 0x10000004
#define | HSM_NO_REPOSITORIES 0x10000005
#define | HSM_MODULE_NOT_FOUND 0x10000006
#define | HSM_PIN_FIRST 0 /* Used when getting the PIN for the first time. */
#define | HSM_PIN_RETRY 1 /* Used when we failed to login the first time. */
#define | HSM_PIN_SAVE

## Typedefs

typedef struct hsm_repository_struct | hsm_repository_t

## Functions

void | hsm_ctx_set_error (hsm_ctx_t *ctx, int error, const char *action, const char *message,...)
int | hsm_open2 (hsm_repository_t *rlist, char *(pin_callback)(unsigned int, const char *, unsigned int))
hsm_repository_t * | hsm_repository_new (char *name, char *module, char *tokenlabel, char *pin, uint8_t use_pubkey, uint8_t allowextract, uint8_t require_backup)
void | hsm_repository_free (hsm_repository_t *r)
char * | hsm_prompt_pin (unsigned int id, const char *repository, unsigned int mode)
char * | hsm_check_pin (unsigned int id, const char *repository, unsigned int mode)
int | hsm_logout_pin (void)
void | hsm_close (void)
hsm_ctx_t * | hsm_create_context (void)
int | hsm_check_context ()
void | hsm_destroy_context (hsm_ctx_t *context)
void | libhsm_key_free (libhsm_key_t *key)
libhsm_key_t ** | hsm_list_keys (hsm_ctx_t *context, size_t *count)
libhsm_key_t ** | hsm_list_keys_repository (hsm_ctx_t *context, size_t *count, const char *repository)
libhsm_key_t * | hsm_find_key_by_id (hsm_ctx_t *context, const char *id)
libhsm_key_t * | hsm_generate_rsa_key (hsm_ctx_t *context, const char *repository, unsigned long keysize)
libhsm_key_t * | hsm_generate_dsa_key (hsm_ctx_t *context, const char *repository, unsigned long keysize)
libhsm_key_t * | hsm_generate_gost_key (hsm_ctx_t *context, const char *repository)
libhsm_key_t * | hsm_generate_ecdsa_key (hsm_ctx_t *context, const char *repository, const char *curve)
libhsm_key_t * | hsm_generate_eddsa_key (hsm_ctx_t *context, const char *repository, const char *curve)
int | hsm_remove_key (hsm_ctx_t *context, libhsm_key_t *key)
void | libhsm_key_list_free (libhsm_key_t **key_list, size_t count)
char * | hsm_get_key_id (hsm_ctx_t *context, const libhsm_key_t *key)
libhsm_key_info_t * | hsm_get_key_info (hsm_ctx_t *context, const libhsm_key_t *key)
void | libhsm_key_info_free (libhsm_key_info_t *key_info)
int | hsm_random_buffer (hsm_ctx_t *ctx, unsigned char *buffer, unsigned long length)
uint32_t | hsm_random32 (hsm_ctx_t *ctx)
uint64_t | hsm_random64 (hsm_ctx_t *ctx)
int | hsm_attach (const char *repository, const char *token_name, const char *path, const char *pin, const hsm_config_t *config)
int | hsm_token_attached (hsm_ctx_t *ctx, const char *repository)
char * | hsm_get_error (hsm_ctx_t *gctx)
void | hsm_print_session (hsm_session_t *session)
void | hsm_print_ctx (hsm_ctx_t *ctx)
void | hsm_print_key (hsm_ctx_t *ctx, libhsm_key_t *key)
void | hsm_print_error (hsm_ctx_t *ctx)
void | hsm_print_tokeninfo (hsm_ctx_t *ctx)
void | keycache_create (hsm_ctx_t *ctx)
void | keycache_destroy (hsm_ctx_t *ctx)
const libhsm_key_t * | keycache_lookup (hsm_ctx_t *ctx, const char *locator)

## Macro Definition Documentation

## HSM_CONFIG_FILE_ERROR

#define HSM_CONFIG_FILE_ERROR 0x10000003

Definition at line 68 of file libhsm.h.

## HSM_ERROR

#define HSM_ERROR 0x10000001

Definition at line 66 of file libhsm.h.

## HSM_ERROR_MSGSIZE

#define HSM_ERROR_MSGSIZE 512

Definition at line 49 of file libhsm.h.

## HSM_MAX_ALGONAME

#define HSM_MAX_ALGONAME 16

Definition at line 47 of file libhsm.h.

## HSM_MAX_PIN_LENGTH

#define HSM_MAX_PIN_LENGTH 255

Definition at line 59 of file libhsm.h.

## HSM_MAX_SESSIONS [1/2]

#define HSM_MAX_SESSIONS 100

Definition at line 35 of file libhsm.h.

## HSM_MAX_SESSIONS [2/2]

#define HSM_MAX_SESSIONS 100

Definition at line 35 of file libhsm.h.

## HSM_MAX_SIGNATURE_LENGTH

#define HSM_MAX_SIGNATURE_LENGTH 512

Definition at line 53 of file libhsm.h.

## HSM_MODULE_NOT_FOUND

#define HSM_MODULE_NOT_FOUND 0x10000006

Definition at line 71 of file libhsm.h.

## HSM_NO_REPOSITORIES

#define HSM_NO_REPOSITORIES 0x10000005

Definition at line 70 of file libhsm.h.

## HSM_OK

#define HSM_OK 0

Return codes for some of the functions

These should be different than the list of CKR_ values defined by pkcs11 (for easier debugging purposes of calling applications)

Definition at line 65 of file libhsm.h.

## HSM_PIN_FIRST

#define HSM_PIN_FIRST 0 /* Used when getting the PIN for the first time. */

The mode for the PIN callback functions

Definition at line 74 of file libhsm.h.

## HSM_PIN_INCORRECT

#define HSM_PIN_INCORRECT 0x10000002

Definition at line 67 of file libhsm.h.

## HSM_PIN_RETRY

#define HSM_PIN_RETRY 1 /* Used when we failed to login the first time. */

Definition at line 75 of file libhsm.h.

## HSM_PIN_SAVE

#define HSM_PIN_SAVE

Value:
2 /* The latest PIN can be saved for future use. Called
after a successful login. */

Definition at line 76 of file libhsm.h.

## HSM_REPOSITORY_NOT_FOUND

#define HSM_REPOSITORY_NOT_FOUND 0x10000004

Definition at line 69 of file libhsm.h.

## Typedef Documentation

## hsm_repository_t

typedef struct hsm_repository_struct hsm_repository_t

HSM Repositories

Definition at line 117 of file libhsm.h.

## Function Documentation

## hsm_attach()

int hsm_attach | ( | const char * | repository,
|  | const char * | token_name,
|  | const char * | path,
|  | const char * | pin,
|  | const hsm_config_t * | config
| ) |  |

extern

Attached a named HSM using a PKCS#11 shared library and optional credentials (may be NULL, but then undefined) This function changes the global state, and is not threadsafe

Parameters

  repository | the name of the repository
  token_label | the name of the token to attach
  path | the path of the shared PKCS#11 library
  pin | the PIN to log into the token
  config | optional configuration

Returns
  0 on success, -1 on error

Definition at line 3373 of file libhsm.c.

References _hsm_ctx, and HSM_OK.

Referenced by hsm_open2().

## hsm_check_context()

int hsm_check_context | ( |  | ) |

extern

Check HSM context

Check if the associated sessions are still alive. If they are not alive, then try re-open libhsm.

Parameters

  context | HSM context

Returns
  0 if successful, !0 if failed

Definition at line 2383 of file libhsm.c.

References _hsm_ctx, _hsm_ctx_mutex, CKF_RW_SESSION, CKF_SERIAL_SESSION, CKS_RW_USER_FUNCTIONS, ctx, hsm_ctx_set_error(), HSM_ERROR, HSM_OK, hsm_session_t::session, hsm_ctx_t::session, hsm_ctx_t::session_count, and ck_session_info::state.

## hsm_check_pin()

char * hsm_check_pin | ( | unsigned int | id,
|  | const char * | repository,
|  | unsigned int | mode
| ) |  |

extern

Function that will check if there is a PIN in the shared memory and returns it.

Parameters

  id | Used for identifying the repository. Will have a value between zero and HSM_MAX_SESSIONS.
  repository | The repository name will be included in the prompt
  mode | The type of mode the function should run in.

Returns
  The string the user enters

Definition at line 325 of file pin.c.

References _hsm_ctx, hsm_ctx_set_error(), HSM_ERROR, HSM_MAX_PIN_LENGTH, HSM_MAX_SESSIONS, HSM_PIN_FIRST, HSM_PIN_RETRY, and HSM_PIN_SAVE.

## hsm_close()

void hsm_close | ( | void |  | ) |

extern

Close HSM library

Log out and detach from all configured HSMs This cleans up all data for libhsm, and should be the last function called.

Definition at line 2363 of file libhsm.c.

References _hsm_ctx, _hsm_ctx_mutex, and keycache_destroy().

Referenced by main().

## hsm_create_context()

hsm_ctx_t * hsm_create_context | ( | void |  | ) |

extern

Create new HSM context

Creates a new session for each attached HSM. The returned hsm_ctx_t * can be freed with hsm_destroy_context()

Definition at line 2373 of file libhsm.c.

References _hsm_ctx, and _hsm_ctx_mutex.

Referenced by hsm_keytag(), and main().

## hsm_ctx_set_error()

void hsm_ctx_set_error | ( | hsm_ctx_t * | ctx,
|  | int | error,
|  | const char * | action,
|  | const char * | message,
|  |  | ...
| ) |  |

extern

Set HSM Context Error

If the ctx is given, and it's error value is still 0, the value will be set to 'error', and the error_message and error_action will be set to the given strings.

Parameters

  ctx | HSM context
  error | error code
  action | action for which the error occured
  message | error message format string

Definition at line 209 of file libhsm.c.

## hsm_destroy_context()

void hsm_destroy_context | ( | hsm_ctx_t * | context | ) |

extern

Destroy HSM context

Parameters

  context | HSM context

Also destroys any associated sessions.

Definition at line 2438 of file libhsm.c.

References ctx.

Referenced by hsm_keytag(), and main().

## hsm_find_key_by_id()

libhsm_key_t * hsm_find_key_by_id | ( | hsm_ctx_t * | context,
|  | const char * | id
| ) |  |

extern

Find a key pair by CKA_ID (as hex string)

The returned key structure can be freed with libhsm_key_free()

Parameters

  context | HSM context
  id | CKA_ID of key to find (null-terminated string of hex characters)

Returns
  key identifier or NULL if not found (or invalid input)

Definition at line 2520 of file libhsm.c.

References ctx.

Referenced by hsm_keytag(), keycache_lookup(), and main().

## hsm_generate_dsa_key()

libhsm_key_t * hsm_generate_dsa_key | ( | hsm_ctx_t * | context,
|  | const char * | repository,
|  | unsigned long | keysize
| ) |  |

extern

Generate new key pair in HSM

Keys generated by libhsm will have a 16-byte identifier set as CKA_ID and the hexadecimal representation of it set as CKA_LABEL.

The returned key structure can be freed with libhsm_key_free()

Parameters

  context | HSM context
  repository | repository in where to create the key
  keysize | Size of DSA key

Returns
  return key identifier or NULL if key generation failed

Definition at line 2637 of file libhsm.c.

References CK_FALSE, CK_TRUE, CKA_BASE, CKA_DECRYPT, CKA_ENCRYPT, CKA_EXTRACTABLE, CKA_ID, CKA_KEY_TYPE, CKA_LABEL, CKA_PRIME, CKA_PRIME_BITS, CKA_PRIVATE, CKA_SENSITIVE, CKA_SIGN, CKA_SUBPRIME, CKA_TOKEN, CKA_UNWRAP, CKA_VERIFY, CKA_WRAP, CKK_DSA, CKM_DSA_KEY_PAIR_GEN, CKM_DSA_PARAMETER_GEN, ctx, libhsm_key_t::modulename, NULL_PTR, libhsm_key_t::private_key, libhsm_key_t::public_key, and hsm_session_t::session.

Referenced by hsm_test().

## hsm_generate_ecdsa_key()

libhsm_key_t * hsm_generate_ecdsa_key | ( | hsm_ctx_t * | context,
|  | const char * | repository,
|  | const char * | curve
| ) |  |

extern

Generate new key pair in HSM

Keys generated by libhsm will have a 16-byte identifier set as CKA_ID and the hexadecimal representation of it set as CKA_LABEL.

The returned key structure can be freed with libhsm_key_free()

Parameters

  context | HSM context
  repository | repository in where to create the key
  curve | which curve to use

Returns
  return key identifier or NULL if key generation failed

Definition at line 2831 of file libhsm.c.

References CK_FALSE, CK_TRUE, CKA_DECRYPT, CKA_EC_PARAMS, CKA_ENCRYPT, CKA_EXTRACTABLE, CKA_ID, CKA_KEY_TYPE, CKA_LABEL, CKA_PRIVATE, CKA_SENSITIVE, CKA_SIGN, CKA_TOKEN, CKA_UNWRAP, CKA_VERIFY, CKA_WRAP, CKK_EC, CKM_EC_KEY_PAIR_GEN, ctx, libhsm_key_t::modulename, NULL_PTR, libhsm_key_t::private_key, libhsm_key_t::public_key, and hsm_session_t::session.

Referenced by hsm_test().

## hsm_generate_eddsa_key()

libhsm_key_t * hsm_generate_eddsa_key | ( | hsm_ctx_t * | context,
|  | const char * | repository,
|  | const char * | curve
| ) |  |

Generate new key pair in HSM

Keys generated by libhsm will have a 16-byte identifier set as CKA_ID and the hexadecimal representation of it set as CKA_LABEL.

The returned key structure can be freed with libhsm_key_free()

Parameters

  context | HSM context
  repository | repository in where to create the key
  curve | which curve to use

Returns
  return key identifier or NULL if key generation failed

Definition at line 2927 of file libhsm.c.

References CK_FALSE, CK_TRUE, CKA_DECRYPT, CKA_EC_PARAMS, CKA_ENCRYPT, CKA_EXTRACTABLE, CKA_ID, CKA_KEY_TYPE, CKA_LABEL, CKA_PRIVATE, CKA_SENSITIVE, CKA_SIGN, CKA_TOKEN, CKA_UNWRAP, CKA_VERIFY, CKA_WRAP, CKK_EC_EDWARDS, CKM_EC_EDWARDS_KEY_PAIR_GEN, ctx, libhsm_key_t::modulename, NULL_PTR, libhsm_key_t::private_key, libhsm_key_t::public_key, and hsm_session_t::session.

Referenced by hsm_test().

## hsm_generate_gost_key()

libhsm_key_t * hsm_generate_gost_key | ( | hsm_ctx_t * | context,
|  | const char * | repository
| ) |  |

extern

Generate new key pair in HSM

Keys generated by libhsm will have a 16-byte identifier set as CKA_ID and the hexadecimal representation of it set as CKA_LABEL.

The returned key structure can be freed with libhsm_key_free()

Parameters

  context | HSM context
  repository | repository in where to create the key

Returns
  return key identifier or NULL if key generation failed

Definition at line 2751 of file libhsm.c.

References CK_FALSE, CK_TRUE, CKA_DECRYPT, CKA_ENCRYPT, CKA_EXTRACTABLE, CKA_GOSTR3410PARAMS, CKA_GOSTR3411PARAMS, CKA_ID, CKA_KEY_TYPE, CKA_LABEL, CKA_PRIVATE, CKA_SENSITIVE, CKA_SIGN, CKA_TOKEN, CKA_UNWRAP, CKA_VERIFY, CKA_WRAP, CKK_GOSTR3410, CKM_GOSTR3410_KEY_PAIR_GEN, ctx, libhsm_key_t::modulename, NULL_PTR, libhsm_key_t::private_key, libhsm_key_t::public_key, and hsm_session_t::session.

Referenced by hsm_test().

## hsm_generate_rsa_key()

libhsm_key_t * hsm_generate_rsa_key | ( | hsm_ctx_t * | context,
|  | const char * | repository,
|  | unsigned long | keysize
| ) |  |

extern

Generate new key pair in HSM

Keys generated by libhsm will have a 16-byte identifier set as CKA_ID and the hexadecimal representation of it set as CKA_LABEL. Other stuff, like exponent, may be needed here as well.

The returned key structure can be freed with libhsm_key_free()

Parameters

  context | HSM context
  repository | repository in where to create the key
  keysize | Size of RSA key

Returns
  return key identifier or NULL if key generation failed

Definition at line 2549 of file libhsm.c.

References CK_FALSE, CK_TRUE, CKA_DECRYPT, CKA_ENCRYPT, CKA_EXTRACTABLE, CKA_ID, CKA_KEY_TYPE, CKA_LABEL, CKA_MODULUS_BITS, CKA_PRIVATE, CKA_PUBLIC_EXPONENT, CKA_SENSITIVE, CKA_SIGN, CKA_TOKEN, CKA_UNWRAP, CKA_VERIFY, CKA_WRAP, CKK_RSA, CKM_RSA_PKCS_KEY_PAIR_GEN, ctx, libhsm_key_t::modulename, NULL_PTR, libhsm_key_t::private_key, libhsm_key_t::public_key, and hsm_session_t::session.

Referenced by hsm_test(), and main().

## hsm_get_error()

char * hsm_get_error | ( | hsm_ctx_t * | gctx | ) |

extern

Return the current error message

The returned message is allocated data, and must be free()d by the caller

Parameters

  ctx | HSM context

Returns
  error message string

Definition at line 3413 of file libhsm.c.

References _hsm_ctx, ctx, hsm_ctx_t::error, hsm_ctx_t::error_action, hsm_ctx_t::error_message, and HSM_ERROR_MSGSIZE.

Referenced by hsm_print_error(), and main().

## hsm_get_key_id()

char * hsm_get_key_id | ( | hsm_ctx_t * | context,
|  | const libhsm_key_t * | key
| ) |  |

extern

Get id as null-terminated hex string using key identifier

The returned id is allocated data, and must be free()d by the caller

Parameters

  context | HSM context
  key | Key pair to get the ID from

Returns
  id of key pair

Definition at line 3062 of file libhsm.c.

References ctx, and libhsm_key_t::private_key.

Referenced by hsm_get_key_info(), hsm_test(), and main().

## hsm_get_key_info()

libhsm_key_info_t * hsm_get_key_info | ( | hsm_ctx_t * | context,
|  | const libhsm_key_t * | key
| ) |  |

extern

Get extended key information

The returned id is allocated data, and must be freed by the caller With libhsm_key_info_free()

Parameters

  context | HSM context
  key | Key pair to get information about

Returns
  key information

Definition at line 3088 of file libhsm.c.

References libhsm_key_info_t::algorithm, libhsm_key_info_t::algorithm_name, CKK_DSA, CKK_EC, CKK_EC_EDWARDS, CKK_GOSTR3410, CKK_RSA, ctx, hsm_get_key_id(), HSM_MAX_ALGONAME, libhsm_key_info_t::id, and libhsm_key_info_t::keysize.

Referenced by hsm_print_key().

## hsm_list_keys()

libhsm_key_t ** hsm_list_keys | ( | hsm_ctx_t * | context,
|  | size_t * | count
| ) |  |

extern

List all known keys in all attached HSMs

After the function has run, the value at count contains the number of keys found.

The resulting key list can be freed with libhsm_key_list_free() Alternatively, each individual key structure in the list could be freed with libhsm_key_free()

Parameters

  context | HSM context
  count | location to store the number of keys found

Definition at line 2477 of file libhsm.c.

References ctx, hsm_ctx_t::session, and hsm_ctx_t::session_count.

Referenced by main().

## hsm_list_keys_repository()

libhsm_key_t ** hsm_list_keys_repository | ( | hsm_ctx_t * | context,
|  | size_t * | count,
|  | const char * | repository
| ) |  |

extern

List all known keys in a HSM

After the function has run, the value at count contains the number of keys found.

The resulting key list can be freed with libhsm_key_list_free() Alternatively, each individual key structure in the list could be freed with libhsm_key_free()

Parameters

  context | HSM context
  count | location to store the number of keys found
  repository | repository to list the keys in

Definition at line 2503 of file libhsm.c.

References ctx.

## hsm_logout_pin()

int hsm_logout_pin | ( | void |  | ) |

extern

Logout

Function that will logout the user by deleting the shared memory and semaphore. Any authenticated process will still be able to interact with the HSM.

Definition at line 413 of file pin.c.

References _hsm_ctx, hsm_ctx_set_error(), HSM_ERROR, HSM_OK, SEM_KEY, SHM_KEY, and semun::val.

## hsm_open2()

int hsm_open2 | ( | hsm_repository_t * | rlist,
|  | char * | pin_callback)(unsigned int, const char *, unsigned int
| ) |  |

extern

Open HSM library

Parameters

  rlist | Repository list.
  pin_callback | This function will be called for tokens that have no PIN configured. The default hsm_prompt_pin() can be used. If this value is NULL, these tokens will be skipped.

Returns
  0 if successful, !0 if failed

Attaches all HSMs in the repository list, querying for PINs (using the given callback function) if not known. Also creates initial sessions (not part of any context; every API function that takes a context can be passed NULL, in which case the global context will be used) and log into each HSM.

Definition at line 2296 of file libhsm.c.

References _hsm_ctx, _hsm_ctx_mutex, hsm_config_t::allow_extract, hsm_repository_struct::allow_extract, hsm_attach(), hsm_ctx_set_error(), HSM_ERROR, HSM_NO_REPOSITORIES, HSM_OK, HSM_PIN_FIRST, HSM_PIN_INCORRECT, HSM_PIN_RETRY, HSM_PIN_SAVE, keycache_create(), hsm_repository_struct::name, hsm_repository_struct::next, hsm_repository_struct::pin, hsm_ctx_t::session_count, hsm_repository_struct::tokenlabel, hsm_config_t::use_pubkey, and hsm_repository_struct::use_pubkey.

Referenced by main().

## hsm_print_ctx()

void hsm_print_ctx | ( | hsm_ctx_t * | ctx | ) |

extern

Definition at line 3455 of file libhsm.c.

References ctx, hsm_print_session(), hsm_ctx_t::session, and hsm_ctx_t::session_count.

Referenced by main().

## hsm_print_error()

void hsm_print_error | ( | hsm_ctx_t * | ctx | ) |

extern

Definition at line 3492 of file libhsm.c.

References hsm_get_error().

Referenced by hsm_test(), and main().

## hsm_print_key()

void hsm_print_key | ( | hsm_ctx_t * | ctx,
|  | libhsm_key_t * | key
| ) |  |

extern

Definition at line 3466 of file libhsm.c.

References libhsm_key_info_t::algorithm_name, ctx, hsm_get_key_info(), libhsm_key_info_t::id, libhsm_key_info_t::keysize, libhsm_key_info_free(), libhsm_key_t::modulename, libhsm_key_t::private_key, and libhsm_key_t::public_key.

Referenced by main().

## hsm_print_session()

void hsm_print_session | ( | hsm_session_t * | session | ) |

extern

Definition at line 3445 of file libhsm.c.

References hsm_session_t::session.

Referenced by hsm_print_ctx().

## hsm_print_tokeninfo()

void hsm_print_tokeninfo | ( | hsm_ctx_t * | ctx | ) |

extern

Definition at line 3507 of file libhsm.c.

References ctx, HSM_OK, ck_token_info::label, ck_token_info::model, hsm_ctx_t::session, hsm_ctx_t::session_count, and slot_id.

## hsm_prompt_pin()

char * hsm_prompt_pin | ( | unsigned int | id,
|  | const char * | repository,
|  | unsigned int | mode
| ) |  |

extern

Function that queries for a PIN, can be used as callback for hsm_open(). Stores the PIN in the shared memory.

Parameters

  id | Used for identifying the repository. Will have a value between zero and HSM_MAX_SESSIONS.
  repository | The repository name will be included in the prompt
  mode | The type of mode the function should run in.

Returns
  The string the user enters

Definition at line 228 of file pin.c.

References HSM_MAX_PIN_LENGTH, HSM_MAX_SESSIONS, HSM_PIN_FIRST, HSM_PIN_RETRY, and HSM_PIN_SAVE.

Referenced by main().

## hsm_random32()

uint32_t hsm_random32 | ( | hsm_ctx_t * | ctx | ) |

extern

Return unsigned 32-bit random number from any attached HSM

Parameters

  context | HSM context

Returns
  32-bit random number, or 0 if no HSM with a random generator is attached

Definition at line 3339 of file libhsm.c.

References ctx, and hsm_random_buffer().

Referenced by main().

## hsm_random64()

uint64_t hsm_random64 | ( | hsm_ctx_t * | ctx | ) |

extern

Return unsigned 64-bit random number from any attached HSM

Parameters

  context | HSM context

Returns
  64-bit random number, or 0 if no HSM with a random generator is attached

Definition at line 3354 of file libhsm.c.

References ctx, and hsm_random_buffer().

Referenced by main().

## hsm_random_buffer()

int hsm_random_buffer | ( | hsm_ctx_t * | ctx,
|  | unsigned char * | buffer,
|  | unsigned long | length
| ) |  |

extern

Fill a buffer with random data from any attached HSM

Parameters

  context | HSM context
  buffer | Buffer to fill with random data
  length | Size of random buffer

Returns
  0 if successful, !0 if failed

Definition at line 3312 of file libhsm.c.

References CKR_OK, ctx, hsm_session_t::session, hsm_ctx_t::session, and hsm_ctx_t::session_count.

Referenced by hsm_random32(), and hsm_random64().

## hsm_remove_key()

int hsm_remove_key | ( | hsm_ctx_t * | context,
|  | libhsm_key_t * | key
| ) |  |

extern

Remove a key pair from HSM

When a key is removed, the module pointer is set to NULL, and the public and private key handles are set to 0. The structure still needs to be freed.

Parameters

  context | HSM context
  key | Key pair to be removed

Returns
  0 if successful, !0 if failed

Definition at line 3023 of file libhsm.c.

References ctx, libhsm_key_t::private_key, libhsm_key_t::public_key, and hsm_session_t::session.

Referenced by hsm_test(), and main().

## hsm_repository_free()

void hsm_repository_free | ( | hsm_repository_t * | r | ) |

Free configured repositories.

Parameters

  r | Repository list.

Definition at line 407 of file libhsm.c.

References hsm_repository_free(), hsm_repository_struct::name, hsm_repository_struct::next, hsm_repository_struct::pin, and hsm_repository_struct::tokenlabel.

Referenced by hsm_repository_free(), and hsm_repository_new().

## hsm_repository_new()

hsm_repository_t * hsm_repository_new | ( | char * | name,
|  | char * | module,
|  | char * | tokenlabel,
|  | char * | pin,
|  | uint8_t | use_pubkey,
|  | uint8_t | allowextract,
|  | uint8_t | require_backup
| ) |  |

Create new repository as specified in conf.xml.

Parameters

  name | Repository name.
  module | PKCS#11 module.
  tokenlabel | PKCS#11 token label.
  pin | PKCS#11 login credentials.
  use_pubkey | Whether to store the public key in the HSM.

Returns
  The created repository.

Definition at line 374 of file libhsm.c.

References hsm_repository_struct::allow_extract, hsm_repository_free(), hsm_repository_struct::name, hsm_repository_struct::next, hsm_repository_struct::pin, hsm_repository_struct::require_backup, hsm_repository_struct::tokenlabel, and hsm_repository_struct::use_pubkey.

Referenced by parse_conf_repositories().

## hsm_token_attached()

int hsm_token_attached | ( | hsm_ctx_t * | ctx,
|  | const char * | repository
| ) |  |

extern

Check whether a named token has been initialized in this context

Parameters

  ctx | HSM context
  token_name | The name of the token

Returns
  1 if the token is attached, 0 if not found

Definition at line 3396 of file libhsm.c.

References ctx, hsm_ctx_set_error(), HSM_REPOSITORY_NOT_FOUND, hsm_ctx_t::session, and hsm_ctx_t::session_count.

Referenced by hsm_test().

## keycache_create()

void keycache_create | ( | hsm_ctx_t * | ctx | ) |

extern

Definition at line 3567 of file libhsm.c.

References _hsm_ctx, ctx, hsm_ctx_t::keycache, and hsm_ctx_t::keycache_lock.

Referenced by hsm_open2().

## keycache_destroy()

void keycache_destroy | ( | hsm_ctx_t * | ctx | ) |

extern

Definition at line 3575 of file libhsm.c.

References ctx, hsm_ctx_t::keycache, and hsm_ctx_t::keycache_lock.

Referenced by hsm_close().

## keycache_lookup()

const libhsm_key_t * keycache_lookup | ( | hsm_ctx_t * | ctx,
|  | const char * | locator
| ) |  |

extern

Definition at line 3585 of file libhsm.c.

References ctx, hsm_find_key_by_id(), hsm_ctx_t::keycache, and hsm_ctx_t::keycache_lock.

## libhsm_key_free()

void libhsm_key_free | ( | libhsm_key_t * | key | ) |

extern

Definition at line 2470 of file libhsm.c.

References libhsm_key_t::modulename.

Referenced by hsm_keytag(), hsm_test(), libhsm_key_list_free(), and main().

## libhsm_key_info_free()

void libhsm_key_info_free | ( | libhsm_key_info_t * | key_info | ) |

extern

Frees the libhsm_key_info_t structure

Parameters

  key_info | The structure to free

Definition at line 3139 of file libhsm.c.

References libhsm_key_info_t::algorithm_name, and libhsm_key_info_t::id.

Referenced by hsm_print_key().

## libhsm_key_list_free()

void libhsm_key_list_free | ( | libhsm_key_t ** | key_list,
|  | size_t | count
| ) |  |

extern

Free the memory of an array of key structures, as returned by hsm_list_keys()

Parameters

  key_list | The array of keys to free
  count | The number of keys in the array

Definition at line 3052 of file libhsm.c.

References libhsm_key_free().