Session flags
| Flag | Value | Description |
|---|---|---|
CKF_RW_SESSION | 0x00000002 | Open a read/write session. Without this flag the session is read-only. |
CKF_SERIAL_SESSION | 0x00000004 | Required by PKCS#11. Must always be set when calling C_OpenSession. |
User types
| Constant | Description |
|---|---|
CKU_USER | Normal user. Can perform cryptographic operations and access private objects after login. |
CKU_SO | Security Officer. Used for token administration (setting the user PIN). Cannot access user private objects. |
CKU_CONTEXT_SPECIFIC | Context-specific login used in certain multi-step operations. |
Session states
The state of a session determines which objects are visible and which operations are permitted.| State | Description |
|---|---|
CKS_RO_PUBLIC_SESSION | Read-only, not logged in. Only public objects are accessible. |
CKS_RW_PUBLIC_SESSION | Read/write, not logged in. Public objects can be created and modified. |
CKS_RO_USER_FUNCTIONS | Read-only user session. User and public objects accessible, no write. |
CKS_RW_USER_FUNCTIONS | Read/write user session. Full access to user and public objects. |
CKS_RW_SO_FUNCTIONS | Read/write SO session. Required to call C_InitPIN or C_SetPIN as SO. |
C_OpenSession
Opens a session on the specified slot.The ID of the slot on which to open the session. The slot must contain an initialized token.
Session flags.
CKF_SERIAL_SESSION must always be set. Include CKF_RW_SESSION for a read/write session.An application-defined pointer passed to the
notify callback. May be NULL_PTR.A callback function for notifications such as surrender events. May be
NULL_PTR; SoftHSM v2 does not currently invoke this callback.Receives the handle of the newly opened session.
CKR_OK, CKR_SLOT_ID_INVALID, CKR_TOKEN_NOT_PRESENT, CKR_TOKEN_NOT_RECOGNIZED, CKR_SESSION_COUNT (implementation limit reached), CKR_SESSION_PARALLEL_NOT_SUPPORTED (if CKF_SERIAL_SESSION is missing), CKR_CRYPTOKI_NOT_INITIALIZED, CKR_ARGUMENTS_BAD.
C_CloseSession
Closes a single session and releases its resources.Handle of the session to close.
CKR_OK, CKR_SESSION_HANDLE_INVALID, CKR_CRYPTOKI_NOT_INITIALIZED.
Closing a session destroys any session objects created within it and terminates any in-progress cryptographic operations.
C_CloseAllSessions
Closes all sessions open on a given slot.The ID of the slot whose sessions should all be closed.
CKR_OK, CKR_SLOT_ID_INVALID, CKR_CRYPTOKI_NOT_INITIALIZED.
Useful before calling C_InitToken, which requires that no sessions are open on the target slot.
C_GetSessionInfo
Returns the current state and metadata of a session.Handle of the session to query.
Pointer to a
CK_SESSION_INFO structure that receives the slot ID, session state (CKS_*), flags, and device error code.CKR_OK, CKR_SESSION_HANDLE_INVALID, CKR_CRYPTOKI_NOT_INITIALIZED, CKR_ARGUMENTS_BAD.
C_GetOperationState / C_SetOperationState
Save and restore the cryptographic state of a session. Useful for multi-part operations that must be suspended and resumed.C_GetOperationState
Handle of the session whose operation state to save.
Buffer that receives the serialized operation state. Pass
NULL_PTR to obtain the required buffer length.On output, the size in bytes of the operation state.
C_SetOperationState
Handle of the target session.
Buffer containing a previously saved operation state from
C_GetOperationState.Length in bytes of
pOperationState.Handle of the encryption/decryption key used in the saved operation, or
CK_INVALID_HANDLE if not applicable.Handle of the authentication key (MAC key) used in the saved operation, or
CK_INVALID_HANDLE if not applicable.CKR_OK, CKR_SESSION_HANDLE_INVALID, CKR_OPERATION_NOT_INITIALIZED, CKR_SAVED_STATE_INVALID, CKR_CRYPTOKI_NOT_INITIALIZED, CKR_ARGUMENTS_BAD.
SoftHSM v2 returns
CKR_FUNCTION_NOT_SUPPORTED for C_GetOperationState and C_SetOperationState. These functions are defined in the PKCS#11 standard but are not implemented.C_Login
Authenticates the user or Security Officer for a session.Handle of any open session on the target slot. The login state applies to all sessions on that slot, not just this one.
The type of user logging in:
CKU_USER for the normal user or CKU_SO for the Security Officer.The PIN as a UTF-8 byte array. Not null-terminated.
Length in bytes of
pPin.CKR_OK, CKR_SESSION_HANDLE_INVALID, CKR_USER_ALREADY_LOGGED_IN, CKR_USER_ANOTHER_ALREADY_LOGGED_IN (SO logged in when user tries, or vice versa), CKR_PIN_INCORRECT, CKR_PIN_LOCKED (too many failed attempts), CKR_USER_PIN_NOT_INITIALIZED (user PIN not set yet), CKR_SESSION_READ_ONLY_EXISTS (SO login blocked by existing read-only session), CKR_CRYPTOKI_NOT_INITIALIZED, CKR_ARGUMENTS_BAD.
C_Logout
Ends the authenticated session state for the slot.Handle of any open session on the slot. Logout affects all sessions on the slot.
CKR_OK, CKR_SESSION_HANDLE_INVALID, CKR_USER_NOT_LOGGED_IN, CKR_CRYPTOKI_NOT_INITIALIZED.
After C_Logout, all sessions on the slot revert to their public state (CKS_RO_PUBLIC_SESSION or CKS_RW_PUBLIC_SESSION). Session objects created with CKA_PRIVATE = CK_TRUE are destroyed.
Example: open, login, use, and close
Open a session
Call
C_OpenSession with CKF_SERIAL_SESSION | CKF_RW_SESSION. Store the returned CK_SESSION_HANDLE.Log in
Call
C_Login with CKU_USER and the user PIN. This unlocks private objects on the token for all sessions on the slot.