Files
diskless-workstation/files/dracut/95dot1x-macsec/tpm-enrol.sh
T
Audiobuzz 6278f2847d tpm-enrol: pivot host enrolment to a Go binary, built out-of-VM
Replaces the shell/openssl-provider host enrolment (a35f2d6) with a static
Go + go-tpm binary, and moves BOTH host Go binaries out of the Packer VM.

fleet-enrol (files/enrol/, Go + go-tpm, no CGO):
  - talks to /dev/tpmrm0 DIRECTLY via transport.FromReadWriter(os.File) — no
    linuxtpm, so go-tpm is the only dep and there is NO tpm2-tools key-gen /
    openssl tpm2 provider / curl in the image (removes the biggest bench risk,
    the provider handle: key-ref).
  - ensures the ECDSA key @0x81010001, builds the CSR (crypto.Signer over the
    TPM key), POSTs CSR+context to the broker, shows the Plymouth fingerprint,
    polls /status, writes the leaf cert to NV 0x01800001.
  - tpmkey/ is an exported package so the TPM ceremony is tested against the
    go-tpm software simulator from a SEPARATE simtest/ module (CGO stays out of
    the shipped build). Transaction logic (mock broker) tested in-package.
  - deps pinned by go.sum (not vendored): the binary now builds on the CI
    runner, which has network — reproducible via pinned Go 1.26.4 + go.sum.

Out-of-VM build (decision): both fleet-enrol AND fleet-hostapi compile in the
CI "Build host Go binaries" step (Go 1.26.4, CGO off, -trimpath) and Packer
FILE-PROVISIONS the static binaries in. Dropped the in-VM `dnf install golang`
+ the 2d removal — the Rocky build image carries no Go toolchain / go-tpm tree.

Wiring:
  - tpm-enrol.sh: now a thin GATE (empty-NV check, pre-auth DHCP on en*,
    exec /usr/sbin/fleet-enrol, reboot-to-converge). module-setup.sh bundles the
    binary (asserted) + reverts the tpm2-openssl/curl installs.
  - diskless.pkr.hcl: file provisioners for both binaries; enrol installed to
    /usr/sbin before the initramfs build + fapolicyd-trusted; lsinitrd asserts
    fleet-enrol; dnf reverted to baseline (no tpm2-openssl/curl).
  - gitea-main-ci.yaml: Set up Go + build both binaries + run the simulator
    ceremony test, before packer validate.

TESTED here: transaction unit tests + the full TPM ceremony (key-gen -> persist
-> ECDSA sign -> CSR verify -> chunked NV write -> re-enrol) against the go-tpm
simulator. BENCH-VERIFY (real/swtpm, empty owner auth, pre-auth DHCP, Plymouth,
reboot-converge, prod HTTPS) tracked in docs/TODO.md #11.
2026-07-02 09:55:54 +00:00

107 lines
4.9 KiB
Bash

#!/bin/bash
# tpm-enrol.sh — initqueue/settled hook, runs BEFORE dot1x-macsec-up.sh.
#
# INLINE empty-NV enrolment GATE (docs/tpm-enrolment-design.md). On a secure
# build whose TPM NV 0x01800001 is empty, it brings up a pre-auth network on the
# physical NIC and hands off to the out-of-VM `fleet-enrol` binary (Go + go-tpm),
# which generates the key/CSR, talks to the broker, shows the Plymouth
# fingerprint, and writes the issued cert to NV. On success this hook REBOOTS
# into the normal secure path. It is a no-op when a cert is already present
# (normal boot — dot1x-macsec-up.sh reads NV) or under rd.dot1x=0 (test path).
#
# The crypto/HTTP/NV work lives in the binary (testable, TPM-direct); this hook
# owns only the boot-environment concerns: the empty-NV gate, the pre-auth DHCP
# (which the secure-mode nm-unmanaged.sh otherwise withholds from en*), and the
# reboot-to-converge. Empty-NV and the secure path are mutually exclusive, so
# this never reorders the load-bearing boot sequence (invariant #2).
type getarg >/dev/null 2>&1 || . /lib/dracut-lib.sh
STAMP=/run/dot1x/.enrol-done
[ -e "$STAMP" ] && exit 0
mkdir -p /run/dot1x
export TPM2TOOLS_TCTI="device:/dev/tpmrm0"
# --- skip conditions --------------------------------------------------------
[ "$(getarg rd.dot1x=)" = "0" ] && exit 0 # test path: no identity to enrol
if [ "$(getarg rd.fleet.enrol=)" = "0" ]; then
info "tpm-enrol: disabled by rd.fleet.enrol=0"; exit 0
fi
# --- knobs (override on the kernel cmdline) ---------------------------------
NVINDEX=$(getarg rd.dot1x.nvindex=); [ -n "$NVINDEX" ] || NVINDEX=0x01800001
ENROL_URL=$(getarg rd.fleet.enrol_url=); [ -n "$ENROL_URL" ] || ENROL_URL=http://boot.fleet.lab/enrol
NETTMO=$(getarg rd.dot1x.timeout=); [ -n "$NETTMO" ] || NETTMO=60
# Wait briefly for the TPM resource-manager device so an early settle (before the
# TPM driver is up) doesn't misread NV as empty and wrongly trigger enrolment.
i=0
while [ ! -e /dev/tpmrm0 ] && [ "$i" -lt 10 ]; do sleep 1; i=$((i + 1)); done
[ -e /dev/tpmrm0 ] || { warn "tpm-enrol: no TPM (/dev/tpmrm0) — leaving to the secure path"; exit 0; }
# --- already enrolled? then this is a normal boot — do NOT touch the network -
# (Bringing up en* here would fight the secure path, which needs it UNMANAGED for
# wpa_supplicant. So gate on NV BEFORE any network action.)
if tpm2_nvread "$NVINDEX" -o /run/dot1x/nvprobe.der >/dev/null 2>&1 \
&& [ -s /run/dot1x/nvprobe.der ]; then
info "tpm-enrol: client cert present at NV $NVINDEX — normal boot"
rm -f /run/dot1x/nvprobe.der
exit 0
fi
# Committed to enrolling — one attempt per boot.
: > "$STAMP"
warn "tpm-enrol: NV $NVINDEX empty — starting FIRST-TIME TPM enrolment"
# NIC: explicit rd.dot1x.if=, else first non-loopback, non-macsec interface.
NIC=$(getarg rd.dot1x.if=)
if [ -z "$NIC" ]; then
for _dev in /sys/class/net/*; do
_name=${_dev##*/}
case "$_name" in lo|macsec*) continue ;; esac
NIC=$_name; break
done
fi
[ -n "$NIC" ] || { warn "tpm-enrol: no network interface found"; exit 1; }
# --- pre-auth network on the PHYSICAL NIC -----------------------------------
# Secure-mode nm-unmanaged.sh leaves en* UNMANAGED (wpa_supplicant would own it),
# but there is no supplicant yet — so explicitly hand the NIC to NM for a
# plaintext pre-auth DHCP lease (same permit-set the firmware used for the UKI).
# The runtime `nmcli device set managed yes` overrides the unmanaged drop-in.
ip link set "$NIC" up 2>/dev/null || true
nmcli device set "$NIC" managed yes 2>/dev/null || true
nmcli -w 0 device connect "$NIC" 2>/dev/null || true
i=0
while [ "$i" -lt "$NETTMO" ]; do
ip -4 addr show "$NIC" 2>/dev/null | grep -q 'inet ' && break
sleep 1; i=$((i + 1))
done
if [ "$i" -ge "$NETTMO" ]; then
warn "tpm-enrol: no pre-auth DHCP lease on $NIC after ${NETTMO}s"
plymouth message --text="TPM enrolment FAILED (no network)" 2>/dev/null || true
exit 1
fi
info "tpm-enrol: pre-auth lease on $NIC; handing off to fleet-enrol ($ENROL_URL)"
# --- hand off to the enrolment binary ---------------------------------------
# It drives the TPM (key/CSR/NV), the broker exchange, and the Plymouth
# fingerprint. Exit 0 = enrolled (reboot to converge); 10 = already enrolled
# (shouldn't reach here after the NV gate, but treat as normal boot); else fail.
/usr/sbin/fleet-enrol -url "$ENROL_URL" -iface "$NIC" -nvindex "$NVINDEX"
rc=$?
case "$rc" in
0)
info "tpm-enrol: enrolled — rebooting into the secure path"
sleep 2
systemctl --no-block reboot 2>/dev/null || reboot -f 2>/dev/null || \
{ echo 1 > /proc/sys/kernel/sysrq 2>/dev/null; echo b > /proc/sysrq-trigger 2>/dev/null; }
exit 0 ;;
10)
info "tpm-enrol: fleet-enrol reports cert already present — normal boot"
exit 0 ;;
*)
warn "tpm-enrol: fleet-enrol failed (rc=$rc) — reboot to retry"
exit 1 ;;
esac