#!/usr/bin/env bash # One-time, per-machine provisioning: generate an age keypair and seal the # private half inside that machine's TPM, bound to a PCR policy so it only # unseals on that same machine in the same (measured) boot state. The # *public* key becomes a recipient in .sops.yaml (safe to commit); the # *sealed* TPM object is what metznet/services/secrets.scm's activation # script unseals at boot via tpm2_unseal -- the raw private key never # touches disk outside this script's own tmpdir, and that tmpdir is wiped # on exit. # # Requires a real TPM 2.0 device (or a software one, e.g. swtpm, for # testing -- see docker/README.md's "Secrets (sops + TPM)" section). # Docker Desktop on macOS exposes neither, so this only runs on real # Linux hardware (or inside a container with /dev/tpmrm0 passed through, # or TPM2TOOLS_TCTI pointed at a swtpm instance). # # usage: seal-age-key.sh [pcr-selection] # persistent-handle e.g. 0x81010001 -- must match the tpm-handle field # of the using this key # pcr-selection default: sha256:7 (UEFI Secure Boot state). Adjust # to match your actual measured-boot setup -- this # default has NOT been validated against a real # metznet machine's PCR layout yet. set -euo pipefail handle="${1:?usage: $0 [pcr-selection]}" pcrs="${2:-sha256:7}" command -v tpm2_createprimary >/dev/null || { echo "tpm2-tools not found" >&2; exit 1; } command -v age-keygen >/dev/null || { echo "age-keygen not found" >&2; exit 1; } tmpdir="$(mktemp -d)" trap 'shred -u "$tmpdir"/age-key.txt "$tmpdir"/age-key-raw.txt 2>/dev/null; rm -rf "$tmpdir"' EXIT age-keygen -o "$tmpdir/age-key.txt" 2>"$tmpdir/pubkey.txt" pubkey="$(grep -o 'age1[a-z0-9]*' "$tmpdir/pubkey.txt")" # TPM2 sealed-data objects cap out around 128 bytes -- age-keygen's file # (comments + key, ~190 bytes) doesn't fit, but age/sops don't need the # comments: just the bare AGE-SECRET-KEY-1... line (~74 bytes) is a fully # valid identity file on its own. grep -v '^#' "$tmpdir/age-key.txt" > "$tmpdir/age-key-raw.txt" tpm2_createprimary -C o -c "$tmpdir/primary.ctx" >/dev/null tpm2_startauthsession -S "$tmpdir/session.ctx" >/dev/null tpm2_policypcr -S "$tmpdir/session.ctx" -l "$pcrs" -L "$tmpdir/pcr.policy" >/dev/null tpm2_flushcontext "$tmpdir/session.ctx" >/dev/null # Real and virtual TPMs alike commonly support only 2-3 transient object # slots at once. Each of these commands reloads primary.ctx from disk # (that's the whole point of context files), so flushing every loaded # transient object between steps is always safe -- nothing but the # freshly-saved context files is needed to continue. Without this, # chaining create -> load -> evictcontrol reliably exhausts a 3-slot TPM # ("out of memory for object contexts"). tpm2_flushcontext -t >/dev/null tpm2_create -C "$tmpdir/primary.ctx" -u "$tmpdir/seal.pub" -r "$tmpdir/seal.priv" \ -a "fixedtpm|fixedparent|adminwithpolicy" \ -i "$tmpdir/age-key-raw.txt" -L "$tmpdir/pcr.policy" >/dev/null tpm2_flushcontext -t >/dev/null tpm2_load -C "$tmpdir/primary.ctx" -u "$tmpdir/seal.pub" -r "$tmpdir/seal.priv" \ -c "$tmpdir/seal.ctx" >/dev/null tpm2_flushcontext -t >/dev/null tpm2_evictcontrol -C o -c "$tmpdir/seal.ctx" "$handle" >/dev/null cat < to use: (tpm-handle "$handle") (pcr-policy "$pcrs") Re-encrypt secrets/secrets.enc.yaml against the new recipient with: sops updatekeys secrets/secrets.enc.yaml The unsealed private key existed only in $tmpdir and has been shredded. EOF