#!/bin/sh
# Himmelblau HSM PIN Initialization Script
# This script is executed by himmelblau-hsm-pin-init.service at boot time
# to generate or migrate the HSM PIN credential.

set -e

LEGACY=/var/lib/private/himmelblaud/hsm-pin
LEGACY1=/var/lib/private/himmelblaud/hsm-pin.enc
CRED=/var/lib/private/himmelblaud/hsm-pin-nopcr.enc
PIN_RUNTIME=/run/himmelblau-hsm-pin
SRK_HANDLE=0x81000001

gen_pin_hex() (
    if command -v openssl >/dev/null 2>&1; then
        _pin=$(openssl rand -hex 24) || return 1
    else
        _pin=$(head -c 24 /dev/urandom | od -An -t x1 | tr -d ' \n') || return 1
    fi
    _pin=$(printf '%s' "$_pin" | tr -d '\n ')
    if [ "${#_pin}" -ne 48 ]; then
        echo "ERROR: gen_pin_hex produced ${#_pin} characters (expected 48)" >&2
        return 1
    fi
    printf '%s' "$_pin"
)

# Returns 0 if the TPM2 SRK is provisioned at the standard persistent handle
# (0x81000001), as defined by TCG TPM v2.0 Provisioning Guidance section 7.5.1
# and used by systemd-tpm2-setup. Falls back gracefully when tpm2-tools is not
# installed.
srk_is_provisioned() {
    if ! command -v tpm2_getcap >/dev/null 2>&1; then
        # Cannot verify without tpm2-tools; treat as not provisioned so callers
        # fall back to --with-key=auto rather than assuming host+tpm2 is safe.
        return 1
    fi
    tpm2_getcap handles-persistent 2>/dev/null | grep -q "$SRK_HANDLE"
}

# Provision the TPM2 Storage Root Key (SRK) at the standard persistent handle
# 0x81000001 following TCG TPM v2.0 Provisioning Guidance (section 7.5.1).
#
# This replicates what systemd-tpm2-setup.service does, but without requiring
# ConditionSecurity=measured-uki. On Ubuntu 24.04 and other distros that do not
# boot via a measured UKI (GRUB + separate vmlinuz is the default), systemd
# never runs that service, so the SRK is never provisioned and systemd-creds
# silently falls back to host-only key binding.
#
# Algorithm preference matches systemd: ECC P-256 first, RSA-2048 fallback.
# Attributes match the TCG standard:
#   decrypt | fixedparent | fixedtpm | noda | restricted |
#   sensitivedataorigin | userwithauth
#
# Returns 0 on success, 1 on failure (tpm2-tools not installed counts as
# success — caller already verified srk_is_provisioned returned false via
# tpm2_getcap, which implies tpm2-tools IS installed if we get here).
provision_srk() {
    if ! command -v tpm2_createprimary >/dev/null 2>&1 || \
       ! command -v tpm2_evictcontrol >/dev/null 2>&1; then
        # Package name varies by distro:
        #   Debian/Ubuntu/Fedora/Rocky/Amazon: tpm2-tools
        #   openSUSE/SLE:                      tpm2.0-tools
        #   Gentoo:                            app-crypt/tpm2-tools
        echo "WARNING: tpm2-tools not fully installed, cannot provision SRK." \
             "Install tpm2-tools (or tpm2.0-tools on openSUSE/SLE) for TPM-bound HSM PIN support."
        return 1
    fi

    CTX=$(mktemp /tmp/himmelblau-srk.XXXXXX.ctx)
    trap 'rm -f "$CTX"' EXIT

    # Try ECC P-256 first (systemd preference, smaller key, same security level)
    if tpm2_createprimary \
            -C o \
            -G ecc256:aes128cfb \
            -a "decrypt|fixedparent|fixedtpm|noda|restricted|sensitivedataorigin|userwithauth" \
            -c "$CTX" >/dev/null 2>&1; then
        echo "SRK: created ECC P-256 primary key"
    # Fall back to RSA-2048 if ECC is not supported by this TPM
    elif tpm2_createprimary \
            -C o \
            -G rsa2048:aes128cfb \
            -a "decrypt|fixedparent|fixedtpm|noda|restricted|sensitivedataorigin|userwithauth" \
            -c "$CTX" >/dev/null 2>&1; then
        echo "SRK: ECC not supported, created RSA-2048 primary key"
    else
        echo "WARNING: Failed to create TPM2 primary key for SRK provisioning."
        rm -f "$CTX"
        trap - EXIT
        return 1
    fi

    # Persist the key at the standard SRK handle
    if tpm2_evictcontrol -C o -c "$CTX" "$SRK_HANDLE" >/dev/null 2>&1; then
        echo "SRK provisioned at $SRK_HANDLE"
    else
        echo "WARNING: Failed to persist SRK at $SRK_HANDLE."
        rm -f "$CTX"
        trap - EXIT
        return 1
    fi

    rm -f "$CTX"
    trap - EXIT
    return 0
}

# Ensure the directory exists
mkdir -p /var/lib/private/himmelblaud
chmod 700 /var/lib/private/himmelblaud

# Check if systemd-creds is available
if ! command -v systemd-creds >/dev/null 2>&1; then
    echo "ERROR: systemd-creds not available, cannot create encrypted credential"
    exit 1
fi

# systemd provides a private tmpfs mount which is unmounted on service exit,
# including SIGKILL. Refuse manual invocation on a persistent or unsafe path.
if [ ! -d "$PIN_RUNTIME" ] || [ -L "$PIN_RUNTIME" ] ||
   [ "$(stat -c '%u:%g:%a' "$PIN_RUNTIME")" != "0:0:700" ] ||
   [ "$(stat -f -c '%T' "$PIN_RUNTIME")" != "tmpfs" ]; then
    echo "ERROR: HSM PIN initialization requires its root-only runtime tmpfs." >&2
    exit 1
fi

# Attempt to self-provision the SRK if the TPM is present but the SRK is
# missing. This is the common case on Ubuntu 24.04 (and any distro that boots
# via GRUB + separate vmlinuz rather than a measured UKI), where
# systemd-tpm2-setup.service is gated on ConditionSecurity=measured-uki and
# therefore never runs.
if [ -e /dev/tpmrm0 ] || [ -e /dev/tpm0 ]; then
    if ! srk_is_provisioned; then
        echo "TPM present but SRK not provisioned at $SRK_HANDLE — attempting self-provisioning..."
        if provision_srk; then
            echo "SRK self-provisioning succeeded"
        else
            echo "WARNING: SRK self-provisioning failed. HSM PIN will not be TPM-bound."
        fi
    fi
fi

# Keep plaintext out of shell variables throughout validation and migration.
HSM_PIN_FILE=$(mktemp "$PIN_RUNTIME/pin.XXXXXX")
HSM_PIN_INSPECTION=
trap 'rm -f "$HSM_PIN_FILE"; [ -z "$HSM_PIN_INSPECTION" ] || rm -f "$HSM_PIN_INSPECTION"' EXIT
trap 'exit 129' HUP
trap 'exit 130' INT
trap 'exit 143' TERM

# Only numeric byte values enter shell variables. Use POSIX shell arithmetic
# rather than adding an awk runtime dependency to minimal installations.
effective_pin_bytes() {
    if ! od -An -v -t u1 "$HSM_PIN_FILE" >"$HSM_PIN_INSPECTION"; then
        echo "ERROR: Could not inspect HSM PIN. Preserving credentials and aborting." >&2
        return 1
    fi
    _pin_total=0
    _pin_effective=0
    while IFS= read -r _pin_line; do
        for _pin_byte in $_pin_line; do
            _pin_total=$((_pin_total + 1))
            if [ "$_pin_byte" -ne 10 ]; then
                _pin_effective=$_pin_total
            fi
        done
    done <"$HSM_PIN_INSPECTION"
    printf '%s\n' "$_pin_effective"
}
HSM_PIN_INSPECTION=$(mktemp "$PIN_RUNTIME/inspection.XXXXXX")

# If the encrypted credential already exists, validate it and optionally upgrade
# it to TPM-bound encryption. This handles the case where the credential was
# created during cloud-init or before the SRK was provisioned.
if [ -f "$CRED" ]; then
    # Decrypt the existing credential to validate it.
    # - Decrypt fails: likely a transient/environmental issue (TPM state, machine
    #   key rotation).  Preserve the credential and abort — do NOT auto-delete,
    #   because replacing the HSM PIN can make existing cached keys unusable.
    # - Decrypt succeeds but plaintext < 24 bytes: credential is demonstrably
    #   invalid after trailing-newline normalization; safe to replace.
    # - Decrypt succeeds and plaintext ≥ 24 bytes: credential is valid; keep it.
    # The daemon strips trailing LF bytes before AuthValue::try_from. Count
    # the effective bytes through numeric od output, preserving embedded NUL
    # and LF bytes without putting plaintext in a shell variable.
    if systemd-creds decrypt --name=hsm-pin "$CRED" - >"$HSM_PIN_FILE" 2>/dev/null; then
        PIN_BYTES=$(effective_pin_bytes) || exit 1
        if [ "$PIN_BYTES" -lt 24 ]; then
            echo "WARNING: Existing HSM PIN is too short ($PIN_BYTES bytes, need >=24). Recreating..."
            rm -f "$CRED"
        fi
    else
        echo "ERROR: Could not decrypt existing HSM PIN credential. Leaving it intact and aborting."
        exit 1
    fi
fi

# If the credential is still present after validation it is good — optionally
# upgrade it to TPM-bound encryption then exit.
if [ -f "$CRED" ]; then
    # If a TPM is present and the SRK is now provisioned (including via
    # self-provisioning above), try to upgrade the credential to TPM-bound.
    if [ -e /dev/tpmrm0 ] || [ -e /dev/tpm0 ]; then
        if srk_is_provisioned; then
            if systemd-creds decrypt --name=hsm-pin "$CRED" - >"$HSM_PIN_FILE" 2>/dev/null; then
                CRED_TMP="${CRED}.tmp"
                if systemd-creds encrypt \
                        --name=hsm-pin --with-key=host+tpm2 --tpm2-device=auto \
                        "$HSM_PIN_FILE" "$CRED_TMP" 2>/dev/null; then
                    mv -f "$CRED_TMP" "$CRED"
                    echo "HSM PIN credential upgraded to TPM-bound encryption"
                else
                    rm -f "$CRED_TMP" 2>/dev/null || true
                    echo "WARNING: Re-encryption to TPM-bound key failed. Keeping existing credential."
                fi
            else
                echo "WARNING: Could not decrypt existing credential for TPM upgrade. Keeping existing credential."
            fi
        fi
    fi

    # Only a validated encrypted credential makes the legacy plaintext stale.
    rm -f "$LEGACY"
    echo "HSM PIN credential already exists and is valid, skipping initialization"
    exit 0
fi

# Generate a new PIN if one doesn't exist, otherwise migrate the existing one
if [ -f "$LEGACY" ]; then
    echo "Migrating existing HSM-PIN to encrypted credential"
    cp "$LEGACY" "$HSM_PIN_FILE"
elif [ -f "$LEGACY1" ]; then
    echo "Migrating existing HSM-PIN to no PCR lock"
    systemd-creds decrypt --name=hsm-pin "$LEGACY1" - >"$HSM_PIN_FILE"
else
    echo "Generating new HSM-PIN"
    if ! gen_pin_hex >"$HSM_PIN_FILE"; then
        echo "ERROR: Failed to generate a valid HSM PIN. Aborting."
        exit 1
    fi
fi

# Legacy inputs need the same normalization and length validation as the
# current encrypted credential. Preserve their bytes until encryption succeeds.
PIN_BYTES=$(effective_pin_bytes) || exit 1
if [ "$PIN_BYTES" -lt 24 ]; then
    echo "WARNING: Migration HSM PIN is too short ($PIN_BYTES bytes, need >=24). Recreating..."
    if ! gen_pin_hex >"$HSM_PIN_FILE"; then
        echo "ERROR: Failed to generate a valid replacement HSM PIN. Aborting." >&2
        exit 1
    fi
fi

# Choose the strongest key binding available:
#   host+tpm2  — bound to both this machine's credential secret AND the TPM SRK
#   auto       — systemd picks the best available (may silently omit TPM if SRK absent)
if [ -e /dev/tpmrm0 ] || [ -e /dev/tpm0 ]; then
    if srk_is_provisioned; then
        KEY_ARG="--with-key=host+tpm2 --tpm2-pcrs="
    else
        echo "WARNING: TPM present but SRK provisioning failed. HSM PIN will use host key only."
        KEY_ARG="--with-key=auto --tpm2-pcrs="
    fi
else
    KEY_ARG="--with-key=auto --tpm2-pcrs="
fi

# Encrypt the PIN
if systemd-creds encrypt --name=hsm-pin $KEY_ARG \
        --tpm2-device=auto "$HSM_PIN_FILE" "$CRED"; then
    echo "HSM PIN credential created successfully"
    rm -f "$LEGACY" 2>/dev/null || true
    rm -f "$LEGACY1" 2>/dev/null || true
    exit 0
else
    echo "ERROR: Failed to create HSM PIN credential"
    exit 1
fi
