Skip to main content
softhsm2-util is the primary support tool for libsofthsm2. It initializes tokens, imports key material, and manages the lifecycle of PKCS#11 tokens. It can also be used with any other PKCS#11 library via --module.
softhsm2-util [ACTION] [OPTIONS]

Actions

--show-slots
flag
Display all available slots and their current status, including whether a token is initialized and its label and serial number.
--show-config var
string
Show configuration info. var may be one of:
  • default-pkcs11-lib — print the path to the default PKCS#11 library.
--init-token
flag
Initialize the token at a given slot, token label, or serial number. If the token is already initialized, this command reinitializes it, erasing all objects in the token. The matching Security Officer (SO) PIN must be provided when reinitializing. After initialization, the token is reassigned to a slot based on its serial number.Use with --slot, --token, --serial, or --free; and --label, --so-pin, and --pin.
Any existing content in the token will be permanently erased.
--import path
string
required
Import an object from the file at path. Supports keypairs (PKCS#8 PEM), AES keys (raw binary), and X.509 certificates (PEM).Use with --import-type, --slot or --token or --serial, --file-pin, --pin, --no-public-key, --label, and --id.
--delete-token
flag
Delete the token at a given slot. Use with --token or --serial.
Any content in the token will be permanently erased.
--help, -h
flag
Show the help screen.
--version, -v
flag
Show version info.

Options

--import-type type
string
default:"keypair"
Specify the type of object to import. type may be one of:
  • keypair (default) — the file must be in PKCS#8 PEM format.
  • aes — the file must be in raw binary format.
  • cert — the file must be in X.509 PEM format.
--aes
flag
deprecated
Tells --import to treat the input file as-is and import it as an AES key.
Deprecated. Use --import-type aes instead.
--file-pin PIN
string
The PIN used to decrypt an encrypted PKCS#8 input file. If not provided, the file is assumed to be unencrypted.
--force
flag
Override warnings and force the given action. When importing a key pair, use this flag if the specified --id already exists in the token.
--free
flag
Use the first free (uninitialized) token slot. Use with --init-token.
--id hex
string
required
The ID to assign to the imported object. Must be provided as a hexadecimal string of variable length (e.g., A1B2). Use --force if another object with the same ID already exists.
--label text
string
required
The label to assign to the object (when importing) or to the token (when initializing).
--module path
string
Use an alternative PKCS#11 library instead of the default SoftHSM library. Provide the path to the .so or .dylib file.
--no-public-key
flag
Do not import the public key portion of a key pair. Only the private key object is stored in the token.
--pin PIN
string
The PIN for the normal (user) role. Required for operations that access token objects.
--serial number
string
Select the token whose serial number matches number. An alternative to --slot and --token.
--slot number
number
The slot number where the target token is located.
--so-pin PIN
string
The PIN for the Security Officer (SO) role. Required when initializing or reinitializing a token.
--token label
string
Select the token whose label matches label. An alternative to --slot and --serial.

Examples

Initialize a new token

Use --free to pick the first available slot automatically:
softhsm2-util --init-token --free --label "mytoken" \
  --so-pin 1234 --pin 5678
Or target a specific slot:
softhsm2-util --init-token --slot 1 --label "mytoken"

List all slots

softhsm2-util --show-slots

Import a key pair

Import a PKCS#8 PEM key file into an existing token:
softhsm2-util --import key1.pem --token "mytoken" \
  --label "My key" --id A1B2 --pin 123456
If the key file is encrypted with a PIN:
softhsm2-util --import key1.pem --token "mytoken" \
  --label "My key" --id A1B2 --pin 123456 --file-pin secretpassword

Import an AES key

softhsm2-util --import aes.key --import-type aes \
  --token "mytoken" --label "AES key" --id 0102 --pin 123456

Import an X.509 certificate

softhsm2-util --import cert.pem --import-type cert \
  --token "mytoken" --label "My cert" --id 0304 --pin 123456

Delete a token

softhsm2-util --delete-token --token "mytoken"

Show the default PKCS#11 library path

softhsm2-util --show-config default-pkcs11-lib

Use with a non-SoftHSM PKCS#11 library

softhsm2-util --show-slots --module /usr/lib/libmypkcs11.so

Key import workflow

1

Prepare the key file

Keys must be in PKCS#8 PEM format for keypairs, raw binary for AES, or X.509 PEM for certificates. To convert from BIND .private-key format, use softhsm2-keyconv.
2

Initialize a token

If you have not already done so, create a token:
softhsm2-util --init-token --free --label "mytoken" \
  --so-pin 1234 --pin 5678
3

Import the key

Import the prepared key file into the token:
softhsm2-util --import key.pem --token "mytoken" \
  --label "My key" --id A1B2 --pin 5678
4

Verify

List slots to confirm the token and its objects are visible:
softhsm2-util --show-slots

See also

  • softhsm2-keyconv — convert BIND .private-key files to PKCS#8
  • softhsm2-migrate — migrate SoftHSM v1 token databases
  • softhsm2.conf(5) — SoftHSM configuration file reference

Build docs developers (and LLMs) love