authcha learn

Learn / Operations

The encryption key and rotating secrets

Module 4 of 6 · 25 minutes

By the end you can

  • Say what PKI_KEY_ENCRYPTION_KEY protects and what happens if it is lost
  • Escrow the key properly and check it with kek-check
  • Rotate the key with kek-rotate and clean up afterwards
  • Rotate an OTP service token without downtime

One key, many secrets

PKI_KEY_ENCRYPTION_KEY in .env is 64 hexadecimal characters, made by the installer. It encrypts every long-lived secret Authcha holds in the database, with AES-256-GCM, each one tied to its own row so a copied value does not decrypt elsewhere.

What it protects
Certificate authority private keys (802.1X CA and RadSec CA)
The SCEP registration-authority key
Identity-provider token-signing keys
MDM (Intune, Jamf) client secrets
MikroTik gateway REST passwords
Enforcement connector secrets (firewall API keys, webhook signing secrets)

The good news: a database dump or backup alone cannot issue certificates or sign tokens. The bad news is the flip side: it is a single point of loss. If the key is lost, every one of those secrets is unrecoverable, the certificate authorities included, and every certificate you issued would have to be replaced.

Escrow it

Keep at least two copies, offline, in different places: a password-manager vault with restricted access, a sealed envelope in a safe. Keep them separate from the backups. The key is never part of a backup on purpose, because a backup and its key stored together defeat the encryption. Record which backups were made with which key: the small radius-meta-<timestamp>.txt next to each dump holds a short fingerprint of the key it used.

If the key is not set, the console still works, but certificate issuance is unavailable and identity-provider signing keys are stored unencrypted (it warns at every start, and the metric authcha_oidc_plaintext_keys shows it). Set the key and restart: existing keys are encrypted in place.

Check it

sudo authcha kek-check

It decrypts every sealed item with the key in .env and prints the counts. It changes nothing, and it is part of the restore drill.

Rotate it

Rotate on a schedule you set, when someone who knew the key leaves, and at once if the key, or a host backup together with .env, may have been exposed. Rotation re-encrypts every item under a new key. It does not re-issue certificates or change the certificate authority.

sudo authcha kek-rotate --dry-run      # decrypt everything with the current key, verify, change nothing
sudo authcha kek-rotate                # do it

In order, the command: does a dry run; takes a fresh backup; writes the new key to a .pending file so it cannot be lost if the host fails; stops the console; re-encrypts every item in one database transaction, checking each against the original before it may commit (any failure rolls everything back and restarts on the old key); replaces the key in .env; starts the console; and runs kek-check. Expect about a minute without the console. RADIUS authentication is not interrupted.

To use a key you made yourself, export it first: sudo NEW_PKI_KEY_ENCRYPTION_KEY=<64 hex> authcha kek-rotate.

After a successful rotation:

  1. Put the new key into escrow now.
  2. Backups taken before the rotation need the old key. The command leaves the old .env at backups/env-before-kek-rotation-<time>. Move the old key into escrow, labelled with the dates it covers, and delete that file. Keep it as long as you keep those backups.
  3. Run sudo authcha backup verify.

Rotating the other secrets

For each change to .env or otp.env, recreate the listed services so they read the new value.

SecretProcedureRecreateSide effects
ADMIN_SESSION_SECRETReplace with a long random valueadminEveryone is signed out
PROXY_TRUST_SECRETopenssl rand -hex 32admin caddy, togetherNone
STATE_HMAC_SECRETopenssl rand -hex 32freeradiusLogins waiting at the code prompt must restart
LOCAL_RADIUS_SECRETopenssl rand -hex 16freeradiusNone
RADIUS shared secretsAuthorization Policies → RADIUS ClientsRADIUS restartsDevices fail until updated
CoA secretsChange on the device and in AuthchaNoneNone
API tokensIssue a new one, update automation, revoke the oldNoneNone

OTP service token with no downtime

REST_OTP_TOKEN authenticates the RADIUS server and console to the OTP service. The OTP service accepts a list of tokens, so you can rotate in three steps:

cd /opt/authcha
openssl rand -hex 24          # the new token
# 1. otp.env: VALID_API_KEYS=<old>,<new>        then recreate otp_service
# 2. .env:    REST_OTP_TOKEN=<new>              then recreate freeradius admin
# 3. otp.env: VALID_API_KEYS=<new>              then recreate otp_service

LabHands-on

Use a test install for steps 3 to 6.

  1. Open Settings on the demo as demo-viewer (password Authcha-View-2026). The demo is public and read-only, so you can see where settings live but not change them.
  2. On your install, read the key's fingerprint from the newest radius-meta-*.txt in /opt/authcha/backups/.
  3. Run sudo authcha kek-check and read the counts.
  4. Run sudo authcha kek-rotate --dry-run. Nothing changes.
  5. Write down where your two escrow copies would be, and who can reach them.
  6. Run the real sudo authcha kek-rotate on the test install. Move the old key to escrow, then run sudo authcha backup verify.

Check yourselfQuiz

1What happens to the certificate authority if the encryption key is lost?
2Where should the key be stored?
3What does kek-rotate change?
4A backup from before a rotation. Which key opens it?
5Why does the OTP service accept a list of tokens?

Cheat sheetKeep this

  • PKI_KEY_ENCRYPTION_KEY: 64 hex, AES-256-GCM, protects CA keys, SCEP, IdP signing keys, MDM and connector secrets.
  • Lost key = lost CAs. Two offline copies, separate from backups.
  • Check: sudo authcha kek-check. Rotate: sudo authcha kek-rotate --dry-run, then sudo authcha kek-rotate (about 1 minute without the console, RADIUS keeps running).
  • After rotating: new key to escrow, old key kept for old backups, then sudo authcha backup verify.
  • Changed .env? Recreate the service.
  • Read more: Operations.