Skip to content

Key protection: HSM and cloud KMS

The most important secrets of BastionXP are the private keys of its CAs: the root CA's key, and the intermediate CA key of each authority. The kms setting in /etc/bastionxp/config.yaml decides where these keys are kept. There are five choices:

kms.type Where the CA keys are kept What BastionXP needs to use them
Not set (the default) In BastionXP's database, encrypted with the key-encryption key. The key-encryption key.
pkcs11 In a hardware security module (HSM). BastionXP uses it through the HSM vendor's PKCS#11 library. The HSM token's PIN, in the config file.
awskms In AWS Key Management Service. AWS credentials, found the usual AWS way: the machine's instance role, the AWS_* environment variables, or ~/.aws/credentials.
gcpkms In Google Cloud KMS. A service account key file, or the machine's Application Default Credentials.
azurekms In Azure Key Vault or Azure Managed HSM. Azure credentials, found the usual Azure way: a managed identity, environment variables, or the Azure CLI.

With an HSM or a cloud KMS, the CA's private keys never leave it. When BastionXP needs a signature, it asks the HSM or KMS to sign. BastionXP itself keeps only the certificates and the names of the keys.

Choose where to keep the keys before you create the root CA and the authorities. A key stays where it was created: changing the kms setting later does not move existing keys.

The key-encryption key (KEK)

BastionXP keeps some private keys and secrets in its database. It encrypts all of them with a key-encryption key (KEK). The KEK itself is kept outside the database. So a copy of the database, such as a backup or a pg_dump, does not reveal any key.

The KEK is needed even if you use an HSM or a cloud KMS, because some keys are always kept in the database:

Key When it is kept in the database
The root and intermediate CA keys Only when there is no HSM or KMS
The SSH user and host CA keys Always
The OCSP responder key and the SCEP RA key Always
The keys that sign tokens, and the ACME EAB master key Always

BastionXP does not start without its KEK. It also does not start with a different KEK than the one its database was encrypted with.

Create the KEK

Create the KEK once, before BastionXP starts for the first time. Run the command as the user BastionXP runs as. For the packaged service, that is root:

$ sudo bastionxp kek create --out /etc/bastionxp/kek
KEK:     /etc/bastionxp/kek
KEK id:  cbbb91e72bc77a92

Back it up now, separately from the database: in a password manager or a
safe, never next to the database backups. Losing the KEK means losing every
certificate authority. Every BastionXP server needs the same KEK.

Then tell BastionXP where the KEK is, in the config file:

kek_file: /etc/bastionxp/kek

The KEK file must be readable only by its owner (mode 0600). It must also be owned by the user BastionXP runs as. Otherwise BastionXP does not start.

bastionxp kek create never overwrites an existing file, so you cannot lose a KEK by running it twice.

Back up the KEK

Keep a copy of the KEK in a safe place, such as a password manager or a safe. Keep it apart from the database backups. Without the KEK, the database cannot be decrypted, and every certificate authority in it is lost.

With systemd: an encrypted credential

In production, we recommend letting systemd keep the KEK. systemd stores the KEK encrypted on disk, with the machine's TPM or host key, and gives it only to the BastionXP service.

First, turn the KEK into an encrypted credential. Then remove the plain KEK file, after you have backed it up:

sudo systemd-creds encrypt --name=kek /etc/bastionxp/kek /etc/bastionxp/kek.cred
sudo shred -u /etc/bastionxp/kek

Then tell systemd to give the credential to BastionXP. Run sudo systemctl edit bastionxp, and add:

[Service]
LoadCredentialEncrypted=kek:/etc/bastionxp/kek.cred

Remove the kek_file setting from the config file. Without it, BastionXP reads the KEK from the systemd credential, $CREDENTIALS_DIRECTORY/kek.

If BastionXP does not start

BastionXP explains the problem in its log, and on stderr (journalctl -u bastionxp):

The message says What to do
no key-encryption key (KEK) is configured Create a KEK with bastionxp kek create, and set kek_file in the config file.
... must be readable by its owner only (chmod 600 ...) Run chmod 600 on the KEK file.
... is owned by user ..., but BastionXP runs as user ...: chown it Make the user BastionXP runs as the owner of the KEK file, with chown.
the KEK ... (id ...) is not the one the database was encrypted with (id ...) Give BastionXP the KEK that its database was encrypted with. The message shows the ID of the KEK that is needed.

Each start also writes an audit event. The event is kek_loaded, with the KEK's ID, when the KEK is right, or kek_load_failed, with the reason, when it is not.

Rotate the KEK

To replace the KEK with a new one, create the new KEK, then re-encrypt everything with it:

sudo bastionxp kek create --out /etc/bastionxp/kek.new
sudo bastionxp kek rotate --config /etc/bastionxp/config.yaml \
    --old /etc/bastionxp/kek --new /etc/bastionxp/kek.new

bastionxp kek rotate re-encrypts every key in the database with the new KEK, in one transaction.

While the rotation runs, the BastionXP servers must be able to read the database. You have two ways to do this:

  • Stop the BastionXP servers before the rotation, and start them again afterwards.
  • Or keep them running, and give each server both KEKs while they change over: set kek_file to the new KEK and kek_previous_file to the old one.

After the rotation, on every server, set kek_file to the new KEK, remove kek_previous_file, and restart BastionXP. Keep the old KEK until every server runs with the new one.

HSM (PKCS#11)

kms:
  type: pkcs11
  pkcs11:
    library_path: /usr/lib/softhsm/libsofthsm2.so
    token_label: bastionxp
    pin: "1234"
    max_sessions: 16
Setting What it means
library_path The path of the HSM vendor's PKCS#11 library.
token_label The label of the HSM token (partition) that holds the keys.
pin The token's user PIN.
max_sessions The most sessions BastionXP opens to the HSM. If several BastionXP servers share one HSM partition, keep their total below the HSM's limit.

If BastionXP cannot reach the HSM, or the PIN is wrong, BastionXP does not start.

AWS KMS

kms:
  type: awskms
  aws_kms:
    region: us-east-1

Google Cloud KMS

kms:
  type: gcpkms
  gcp_kms:
    key_ring: projects/my-project/locations/global/keyRings/bastionxp-ca
    credentials_file: /etc/bastionxp/gcp-sa.json

On Google Compute Engine or GKE, you can leave out credentials_file. BastionXP then uses the machine's Application Default Credentials.

Azure Key Vault

kms:
  type: azurekms
  azure_kms:
    vault_url: https://bastionxp-ca.vault.azure.net

The root CA's key

An admin creates the root CA in the web portal, under Authority Manager, in Root CAs. Where its key is generated depends on the kms setting:

  • With an HSM or a cloud KMS, the key is generated inside it, with the label bastionxp-ecdsa-root-ca (or bastionxp-rsa-root-ca for the RSA root).
  • Without one, the key is generated in the database, encrypted with the KEK.

Each authority's intermediate CA key is generated in the same place when the authority is created. Its label is bastionxp-ecdsa-inter-ca-<authority ID>. When you rotate the intermediate CA, the new key gets a label of its own, so that it never replaces an earlier key.

A root key you made yourself

On a self-hosted installation, your organization can make its root CA in a key ceremony. If it does, the root CA's key stays in your own HSM, offline. In Authority Manager, you import the root CA's certificate. BastionXP then never holds the root CA's key.

Each authority under such a root CA brings its own intermediate CA from the ceremony. Its key must be in the HSM or cloud KMS that BastionXP uses, and you give BastionXP the key's ID and the intermediate CA's certificate when you add the authority. BastionXP signs certificates with that key, and the key never leaves the HSM or KMS.

Because BastionXP cannot sign with an offline root CA, you replace the root CA in a key ceremony. You also rotate or revoke an authority's intermediate CA there. See CA lifetimes, rotation and replacement.

Deleting keys

A cloud KMS does not destroy a key at once. When you delete an authority whose key is in a cloud KMS, this is what happens:

  1. BastionXP schedules the key for deletion. The KMS stops using the key for signing straight away, so the authority issues no more certificates.
  2. Until the KMS's recovery period ends, you can still restore the authority, together with its key.
  3. After the recovery period, you purge the authority to remove it for good.

How long the recovery period lasts depends on the KMS:

KMS The recovery period
AWS KMS AWS's default waiting period: 30 days.
Google Cloud KMS The key's "destroy scheduled duration" setting.
Azure Key Vault The vault's soft-delete retention: between 7 and 90 days.
HSM (PKCS#11) There is none. The key stays on the token until you purge the authority, and is destroyed then.

Many HSMs need the Security Officer's credentials to destroy a key. If the purge cannot destroy the key, destroy it with the HSM vendor's tools or with pkcs11-tool.

See also Delete and restore an authority.