NetHSM Command Line Interface
A command line interface (CLI) for the Nitrokey NetHSM based on the nethsm crate.
Installation
This crate can be installed using cargo:
Afterwards the nethsm executable is available.
It is recommended to refer to the extensive --help output of the executable and its subcommands.
Usage
The following assumes a recent version of openssl and podman.
Start a test container
Configuration file
The configuration file uses the TOML format.
By default an Operating System specific, well-defined configuration file location is chosen.
Using -c / --config / the NETHSM_CONFIG environment variable it is possible to provide a custom configuration file location.
# use a custom, temporary directory for all generated files
nethsm_tmpdir=""
# set a custom, temporary configuration file location
To be able to interact with a NetHSM (or the testing container), each device must be added to the configuration file.
# add the container using unsafe TLS connection handling for testing
If only one device environment is configured, it is used by default when issuing nethsm commands.
If more than one environment is configured, the target device must be selected using the global -l/ --label option.
The handling of credentials is flexible: Credentials can be stored in the configuration file with or without passphrases or not at all. If credentials are not configured, they are prompted for interactively.
# prepare a temporary passphrase file for the initial admin user passphrase
nethsm_admin_passphrase_file=""
# add the default admin user credentials
Provisioning
Before using a device for the first time, it must be provisioned. This includes setting the passphrase for the initial "admin" user, the unlock passphrase and the system time of the device.
# prepare a temporary passphrase file for the initial unlock passphrase
# reuse the initial admin passphrase
Users
Each user may be in exactly one role ("Administrator", "Operator", "Metrics" or "Backup").
- "Administrator": for adjusting system configuration, managing users and keys
- "Operator": for using cryptographic keys and getting random bytes
- "Metrics": for retrieving metrics of a device
- "Backup": for creating and downloading backups of a device
nethsm_admin1_passphrase_file=""
nethsm_operator1_passphrase_file=""
nethsm_backup1_passphrase_file=""
nethsm_metrics1_passphrase_file=""
# we create a user in each role and add the credentials including passphrases to the configuration
# NOTE: this is for testing purposes! passphrases stored in configuration files can easily be retrieved!
The user names and accompanying information can be queried:
for; do
done
Tags for users can only be created once keys with those tags exists.
Keys
Keys on the device are managed using a user in the "Administrator" role. Depending on restrictions (tags), the keys may then be used by users in the "Operator" role.
Generating keys
Below, we are generating keys of all available types (Curve25519, EcP224, EcP256, EcP384, EcP521, Generic and Rsa). When generating a key, the unique ID for it may be set manually (else it is auto-generated). Tags, which later on allow users access to the keys may also be set during key generation.
Note that some keys require to set the key bit length (i.e. Generic and Rsa).
All key IDs on the device and info about them can be listed:
for; do
done
Importing keys
Keys can also be imported:
ed25519_cert_pem=""
ed25519_cert_der=""
# import requires a PKCS#8 private key in ASN.1 DER-encoded format
# forgot to set a tag!
Access to keys
To provide access to keys for users, the users have to be tagged with the same tags as the keys.
Signing messages
message_digest=""
# if we add the signing2 tag for operator1 it is able to use signing{2-5}
# if we add the signing3 tag for operator1 it is able to use signing6
# create a signature with each key type
# if we remove the signing3 tag for operator1 it is no longer able to use signing6
Encrypting messages
Messages can be encrypted using keys that offer the key mechanisms for this operation.
message=""
# we need to provide access to the key by tagging the user
# let's use our symmetric encryption key to encrypt a message
# the initialization vector must be the same
# now let's decrypt the encrypted message again
The same works for asymmetric keys as well:
# we need to provide access to the key by tagging the user
# unset the initialization vectors as we do not need them for this
# retrieve the public key of the key to use (and overwrite any previously existing)
asymmetric_enc_message=""
# encrypt the previous message
# decrypt the asymmetrically encrypted message and replace any existing output
Public key
Administrators and operators can retrieve the public key of any key:
# keys of type "Generic" don't have a public key, so we do not request them
for; do
done
Certificate Signing Requests for keys
Certificate Signing Requests for a particular target can be issued using the keys.
# get a CSR for example.com
Random bytes
The device can generate an arbitrary number of random bytes on demand.
Metrics
The metrics for a device can only be retrieved by a user in the "Metrics" role.
Device configuration
Several aspects of the device configuration can be retrieved and modified.
Boot mode
The boot mode defines whether the system starts into "Locked" or "Operational" state (the former requiring to supply the unlock passphrase to get to "Operational" state).
# let's set it to unattended
Logging
Each device may send syslog to a remote host.
Network
The devices have a unique and static network configuration.
System Time
The device's system time can be queried and set.
TLS certificate
We can get and set the TLS certificate used for the device.
# this generates a new RSA 4096bit certificate on the device
We can also receive only the public key for the TLS certificate:
Or generate a Certificate Signing Request for the TLS certificate:
Setting passphrases
The backup passphrase is used to decrypt a backup created for the device, when importing. By default it is the empty string ("").
nethsm_backup_passphrase_file=""
nethsm_initial_backup_passphrase_file=""
The unlock passphrase is set during initial provisioning and is used to unlock the device when it is locked.
Locking
The device can be locked and unlocked, which puts it into state "Locked" and "Operational", respectively.
# as we have changed the unlock passphrase, we need to provide the new one
System modifications
The devices offer various system level actions, e.g.:
# reset device to factory settings
# reboot device
# shut down device
# get system info about the device
Backups
The device offers backing up of keys and user data.
A backup can later on be used to restore a device:
Updates
Updates for the operating system/ firmware of the device are uploaded to the device and then applied or aborted.
# apply the update
# abort the update
License
This project may be used under the terms of the Apache-2.0 or MIT license.
Changes to this project - unless stated otherwise - automatically fall under the terms of both of the aforementioned licenses.