PKCS#11 C API
All functions are loaded at runtime viadlopen/dlsym in hsm_test.c. Each returns CK_RV; CKR_OK (value 0) indicates success.
C_Initialize — initialize the library
C_Initialize — initialize the library
Initializes the PKCS#11 library. Must be the first function called.
Returns:
| Parameter | Type | Description |
|---|---|---|
pInitArgs | CK_VOID_PTR | Pass NULL for default single-threaded initialization. |
CKR_OK on success.C_Finalize — clean up the library
C_Finalize — clean up the library
Releases all resources held by the PKCS#11 library. Must be called before unloading the module.
Returns:
| Parameter | Type | Description |
|---|---|---|
pReserved | CK_VOID_PTR | Reserved; must be NULL. |
CKR_OK on success.C_GetSlotList — list available slots
C_GetSlotList — list available slots
Returns a list of slot IDs, optionally filtered to slots that have a token present.
Returns:
| Parameter | Type | Description |
|---|---|---|
tokenPresent | CK_BBOOL | CK_TRUE to return only slots with a token present. |
pSlotList | CK_SLOT_ID_PTR | Buffer to receive slot IDs. Pass NULL to query the count only. |
pulCount | CK_ULONG_PTR | On input: capacity of pSlotList. On output: number of slots. |
CKR_OK on success.C_GetTokenInfo — read token metadata
C_GetTokenInfo — read token metadata
Retrieves information about the token in a given slot, including its label, serial number, and flags.
Returns:
| Parameter | Type | Description |
|---|---|---|
slotID | CK_SLOT_ID | The slot containing the token. |
pInfo | CK_TOKEN_INFO_PTR | Pointer to a CK_TOKEN_INFO structure to fill. |
CKR_OK on success.C_OpenSession — open a session
C_OpenSession — open a session
Opens a session with a token in the specified slot.
Returns:
| Parameter | Type | Description |
|---|---|---|
slotID | CK_SLOT_ID | Target slot. |
flags | CK_FLAGS | Session flags. Use CKF_SERIAL_SESSION | CKF_RW_SESSION for a read/write session. CKF_SERIAL_SESSION is always required. |
pApplication | CK_VOID_PTR | Application-defined pointer passed to the notify callback. Pass NULL. |
Notify | CK_NOTIFY | Notification callback function pointer. Pass NULL. |
phSession | CK_SESSION_HANDLE_PTR | Receives the new session handle. |
CKR_OK on success.C_CloseSession — close a session
C_CloseSession — close a session
Closes an open session and releases associated resources.
Returns:
| Parameter | Type | Description |
|---|---|---|
hSession | CK_SESSION_HANDLE | Handle of the session to close. |
CKR_OK on success.C_Login — authenticate as a user
C_Login — authenticate as a user
Authenticates the session with the given PIN. Required before accessing private objects.
Returns:
| Parameter | Type | Description |
|---|---|---|
hSession | CK_SESSION_HANDLE | Active session handle. |
userType | CK_USER_TYPE | CKU_USER for normal user; CKU_SO for security officer. |
pPin | CK_UTF8CHAR_PTR | PIN string (not null-terminated). |
ulPinLen | CK_ULONG | Length of the PIN in bytes. |
CKR_OK on success.C_Logout — end authenticated session
C_Logout — end authenticated session
Logs out the current user, ending the authenticated state of the session.
Returns:
| Parameter | Type | Description |
|---|---|---|
hSession | CK_SESSION_HANDLE | Active session handle. |
CKR_OK on success.C_FindObjectsInit — start an object search
C_FindObjectsInit — start an object search
Initializes an object search using an attribute template as a filter.
Returns:
| Parameter | Type | Description |
|---|---|---|
hSession | CK_SESSION_HANDLE | Active session handle. |
pTemplate | CK_ATTRIBUTE_PTR | Array of CK_ATTRIBUTE structures defining the search filter. Pass NULL with ulCount = 0 to match all objects. |
ulCount | CK_ULONG | Number of attributes in pTemplate. |
CKR_OK on success.C_FindObjects — retrieve matching objects
C_FindObjects — retrieve matching objects
Returns up to
Returns:
ulMaxObjectCount handles for objects matching the template set in C_FindObjectsInit.| Parameter | Type | Description |
|---|---|---|
hSession | CK_SESSION_HANDLE | Active session handle. |
phObject | CK_OBJECT_HANDLE_PTR | Buffer to receive object handles. |
ulMaxObjectCount | CK_ULONG | Maximum number of handles to return. |
pulObjectCount | CK_ULONG_PTR | Receives the actual number of handles returned. |
CKR_OK on success. Returns fewer handles than ulMaxObjectCount when no more objects match.C_FindObjectsFinal — end the object search
C_FindObjectsFinal — end the object search
Terminates an object search operation and releases associated resources.
Returns:
| Parameter | Type | Description |
|---|---|---|
hSession | CK_SESSION_HANDLE | Active session handle. |
CKR_OK on success.C_GetAttributeValue — read object attributes
C_GetAttributeValue — read object attributes
Retrieves the values of one or more attributes of a given object. Commonly used to read
Returns:
CKA_LABEL, CKA_ID, and CKA_CLASS.| Parameter | Type | Description |
|---|---|---|
hSession | CK_SESSION_HANDLE | Active session handle. |
hObject | CK_OBJECT_HANDLE | Handle of the object to query. |
pTemplate | CK_ATTRIBUTE_PTR | Array of CK_ATTRIBUTE structures. Set pValue = NULL and ulValueLen = 0 on first call to query buffer sizes. |
ulCount | CK_ULONG | Number of attributes in pTemplate. |
CKR_OK on success.pkcs11-tool CLI reference
pkcs11-tool is part of the opensc package and is used throughout this project to manage tokens and keys via the SoftHSM2 PKCS#11 module.
Flags
Path to the PKCS#11 shared library. For SoftHSM2 on x86-64 Debian/Ubuntu:
/usr/lib/x86_64-linux-gnu/softhsm/libsofthsm2.soNumeric slot ID. Obtain available slot IDs with
softhsm2-util --show-slots.--login
Prompt for (or accept via
--pin) the user PIN before performing the operation.User PIN supplied non-interactively, e.g.
--pin 1234.--keypairgen
Generate an asymmetric key pair (public + private). Requires
--key-type, --id, and --label.Key algorithm and size, e.g.
rsa:2048 or EC:prime256v1.Hex object ID assigned to the key, e.g.
10. Used to reference the key in sign and read operations.Human-readable label for the key object, e.g.
FirmwareKey.--list-objects
List all objects visible in the current session (after login).
--verbose
Print extended attribute information when listing objects.
--sign
Sign data from
--input-file and write the signature to --output-file. Requires --mechanism and --id.Signing mechanism, e.g.
RSA-PKCS or ECDSA.Path to the file containing data to sign, e.g. a pre-computed SHA-256 digest (
data.sha256).Path where the signature bytes will be written, e.g.
signature.bin.--read-object
Read a single object from the token. Requires
--type and --id.Object type for
--read-object. Use pubkey to export the DER-encoded public key.--delete-object
Delete an object from the token. Requires
--type and --id.