#!/bin/sh
# Blackbox: bind this boot's SSH host key to an SEV-SNP attestation report.
#
# The initramfs is public and identical for every customer, so it cannot carry
# a host key. One is generated here on the first boot and kept for the life of
# the machine, sealed to this guest (see below), and its SHA-256 is placed in
# REPORT_DATA of a report signed by the CPU on every boot. A customer's SSH client
# shows that same SHA-256 as the host key fingerprint, so comparing the two,
# after checking the report's signature and measurement, proves the machine
# holding the key is a genuine SNP guest running exactly this image. Only then
# is it safe to type a passphrase into it.
PREREQ="udev"
prereqs() { echo "$PREREQ"; }
case "$1" in prereqs) prereqs; exit 0 ;; esac
. /scripts/functions

mkdir -p /run/bb
chmod 700 /run/bb
BB_KEY=""; BB_REL="unknown"; BB_HOST="blackbox"
for x in $(cat /proc/cmdline); do
    case "$x" in
        bb.key=*)     BB_KEY="${x#bb.key=}" ;;
        bb.release=*) BB_REL="${x#bb.release=}" ;;
        bb.host=*)    BB_HOST="${x#bb.host=}" ;;
    esac
done

# The only key allowed into this shell is the one on the measured command line.
mkdir -p /root-bb/.ssh
chmod 700 /root-bb /root-bb/.ssh
: > /root-bb/.ssh/authorized_keys
chmod 600 /root-bb/.ssh/authorized_keys
if [ -n "$BB_KEY" ]; then
    printf '%s' "$BB_KEY" | base64 -d > /root-bb/.ssh/authorized_keys 2>/dev/null || : > /root-bb/.ssh/authorized_keys
    echo >> /root-bb/.ssh/authorized_keys
fi

modprobe sev-guest 2>/dev/null || true
rm -f /etc/dropbear/dropbear_*_host_key
mkdir -p /etc/dropbear
HK=/etc/dropbear/dropbear_ed25519_host_key

# One host key for the machine, not one per boot, so "ssh root@<ip>" keeps
# working: it lives on the second partition (the template's /boot, which a
# measured guest never uses: it does not boot from its own disk) sealed with an
# SEV-SNP derived key, which only this measured release with this command line
# and policy, on this chip, can obtain (snpreport --seal). Nothing outside the
# guest can open it. Missing, damaged, or sealed under another measurement
# (a new release, an IP change, another machine): a new key is made and sealed
# in its place, and the customer's client warns once.
HK_STATE=ephemeral
SEALED=/run/bb/boot/.blackbox-hostkey
if [ -c /dev/sev-guest ]; then
    modprobe ext4 2>/dev/null || true
    i=0
    while [ ! -b /dev/sda2 ] && [ $i -lt 100 ]; do sleep 0.1; i=$((i + 1)); done
    mkdir -p /run/bb/boot
    if [ -b /dev/sda2 ] && mount -t ext4 -o ro /dev/sda2 /run/bb/boot 2>/dev/null; then
        if [ -f "$SEALED" ] && snpreport --unseal "$SEALED" "$HK" 2>>/run/bb/hostkey.log; then
            HK_STATE=kept
        else
            rm -f "$HK"
            dropbearkey -t ed25519 -f "$HK" >/dev/null 2>&1
            if mount -o remount,rw /run/bb/boot 2>/dev/null \
                && snpreport --seal "$HK" "$SEALED.new" 2>>/run/bb/hostkey.log \
                && mv -f "$SEALED.new" "$SEALED"; then
                sync
                HK_STATE=new
            fi
            rm -f "$SEALED.new"
        fi
        umount /run/bb/boot 2>/dev/null || true
    fi
fi
[ -s "$HK" ] || dropbearkey -t ed25519 -f "$HK" >/dev/null 2>&1
echo "$HK_STATE" > /run/bb/hostkey.state
PUB=$(dropbearkey -y -f "$HK" 2>/dev/null | grep '^ssh-ed25519 ' | head -1)

# The booted system serves this same key: bb-seed hands it to cloud-init on
# the first boot, bb-hostkey keeps it in place on later ones.
if dropbearconvert dropbear openssh "$HK" /run/bb/ssh_host_ed25519_key >/dev/null 2>&1; then
    chmod 600 /run/bb/ssh_host_ed25519_key
    printf '%s root@%s\n' "$(printf '%s' "$PUB" | cut -d' ' -f1,2)" "$BB_HOST" > /run/bb/ssh_host_ed25519_key.pub
fi
printf '%s' "$(printf '%s' "$PUB" | cut -d' ' -f2)" | base64 -d > /run/bb/hostkey.bin 2>/dev/null

STATUS="ok"
if [ -c /dev/sev-guest ]; then
    FP=$(snpreport /run/bb/hostkey.bin /run/bb/report.bin 2>/run/bb/snpreport.err) || { STATUS="report_failed"; FP=$(snpreport --fp /run/bb/hostkey.bin); }
else
    STATUS="not_confidential"
    FP=$(snpreport --fp /run/bb/hostkey.bin)
fi
REPORT=""
[ -s /run/bb/report.bin ] && REPORT=$(base64 /run/bb/report.bin | tr -d '\n')
CMDLINE=$(sed 's/\\/\\\\/g; s/"/\\"/g' /proc/cmdline | tr -d '\n')

cat > /run/bb/attest.json <<JSON
{"version": 1, "release": "$BB_REL", "status": "$STATUS", "hostkey": "$PUB", "fingerprint": "$FP", "report_b64": "$REPORT", "cmdline": "$CMDLINE"}
JSON
chmod 644 /run/bb/attest.json

{
    echo ""
    echo "== Blackbox measured boot =="
    echo "release: $BB_REL    status: $STATUS"
    echo "host key fingerprint: $FP"
    echo "  (must equal the fingerprint your SSH client just showed you)"
    echo "SEV-SNP attestation report, REPORT_DATA = SHA-256 of that host key:"
    if [ -n "$REPORT" ]; then echo "$REPORT" | fold -w 76; else echo "  NONE: this VM did not produce a report. Do not enter a passphrase."; fi
    echo ""
    echo "Verify this VM from your own computer before typing a passphrase."
    echo "What blackbox-verify is, where to get it and how to run it:"
    echo "  https://servury.com/docs/guides/measured-boot/#verify"
    echo ""
} > /run/bb/banner
chmod 644 /run/bb/banner
