softhsm2-util command-line utility or directly through the PKCS#11 C_InitToken function.
Using softhsm2-util
Select a slot to initialize
You can target a slot in three ways:| Flag | Behavior |
|---|---|
--free | Use the first available uninitialized slot. This is the recommended approach. |
--slot <number> | Target a specific slot by its numeric ID. |
--token <label> | Re-initialize an existing token identified by its label. |
Required flags
| Flag | Description |
|---|---|
--label <text> | A label for the token, up to 32 characters. Required. |
--so-pin <PIN> | The Security Officer PIN. Required (unless entered interactively). |
--pin <PIN> | The normal user PIN. Required (unless entered interactively). |
--so-pin and --pin are omitted, the tool prompts for them interactively.
Initialize using the free slot
The recommended way to create a new token is with--free, which targets the always-available uninitialized slot without needing to know its slot ID:
Initialize by slot number
You can also pass a specific slot number. Slot0 is usually the first available slot on a fresh installation:
--so-pin and --pin, the tool prompts for both PINs interactively.
Slot reassignment after initialization
After initialization, SoftHSM reassigns the token to a new slot whose ID is derived from the token’s serial number. The slot number you used during--init-token does not persist.
Additionally, SoftHSM automatically adds a new free (uninitialized) slot so that another token can be created in the future.
To see the new slot assignment after initialization:
Re-initializing an existing token
If a token is already initialized, running--init-token against it will erase all objects on the token and reset it with the new label and PINs. You must supply the current SO PIN to authorize this operation:
Using the PKCS#11 C_InitToken function
Applications can initialize tokens directly using the PKCS#11 API withoutsofthsm2-util. The relevant function is C_InitToken:
C_InitToken succeeds, set the user PIN by opening an SO session and calling C_InitPIN:
The
pLabel parameter to C_InitToken must be exactly 32 bytes, padded with spaces on the right. SoftHSM handles this padding internally when called via softhsm2-util, but PKCS#11 applications must pad the label themselves.What happens after initialization
Token receives a serial number
SoftHSM generates a unique serial number for the new token at initialization time.
Token is moved to a new slot
The token is reassigned to a slot whose ID is derived from the serial number. The original slot used during initialization no longer holds this token.
A new free slot is added
SoftHSM adds a new empty slot so that another token can be initialized in the future. There is always at least one free slot available.