#!/bin/bash
# Verify a published STIG image on the host that runs it (read-only; no secrets).
# Run on a Proxmox node:  ssh root@<node> "STORE=/mnt/pve/ct-stig-images TPL=<template> FPR=<fingerprint> bash -s" < verify-image.sh
# Checks, with the pinned evidence-key fingerprint: public key fingerprint, index.json signature,
# template.json sha256 + signature, qcow2 sha256 + signature. Exit 0 and VERIFY_OK on success.
set -euo pipefail
: "${STORE:?}" "${TPL:?}" "${FPR:?}"
cd "$STORE/rhel8-stig"
H="$(mktemp -d /tmp/stgv.XXXXXX)"; chmod 700 "$H"
trap 'gpgconf --homedir "$H" --kill all >/dev/null 2>&1; rm -rf "$H"' EXIT
G="gpg --homedir $H --batch --no-tty --yes"
$G --import stig-evidence-signing.pub.asc >/dev/null 2>&1
$G --list-keys --with-colons 2>/dev/null | grep -q "^fpr:::::::::$FPR:" || { echo "pubkey fingerprint mismatch" >&2; exit 1; }
vok() { $G --status-fd 1 --verify "$2" "$1" 2>/dev/null | grep VALIDSIG | grep -q "$FPR" || { echo "BAD signature: $1" >&2; exit 1; }; }
vok index.json index.json.asc
read -r QS TS < <(python3 -I - "$TPL" <<'PY'
import json, sys
i = json.load(open("index.json"))
v = [x for x in i["versions"] if x["name"] == sys.argv[1]]
if not v: sys.exit("template not in index.json")
print(v[0]["qcow2_sha256"], v[0]["template_json_sha256"])
PY
)
[ "$(sha256sum "$TPL/template.json" | cut -d' ' -f1)" = "$TS" ] || { echo "template.json sha256 mismatch" >&2; exit 1; }
vok "$TPL/template.json" "$TPL/template.json.asc"
[ "$(sha256sum "../import/$TPL.qcow2" | cut -d' ' -f1)" = "$QS" ] || { echo "qcow2 sha256 mismatch" >&2; exit 1; }
vok "../import/$TPL.qcow2" "$TPL/$TPL.qcow2.asc"
echo "VERIFY_OK qcow2_sha256=$QS template_json_sha256=$TS"
