#!/bin/sh
set -eu

output_directory="${1:-/images/boot/addons}"
secure_boot_directory="/etc/puavo-secure-boot"

# Device Secure Boot key (DB)
# The key is used for signing device-specific modifications (e.g. kernel command-line).
db_private_key="${output_directory}/secure-boot.priv"
db_pem="${output_directory}/secure-boot.pem"
db_auth="${output_directory}/db.auth"
db_enrolled="${output_directory}/db.enrolled.json"

# Device-specific Platform Key (PK)
pk_private_key="${output_directory}/pk.priv"
pk_pem="${output_directory}/pk.pem"
pk_auth="${output_directory}/pk.auth"

# Device-specific Key Exchange Key (KEK)
kek_private_key="${output_directory}/kek.priv"
kek_pem="${output_directory}/kek.pem"
kek_auth="${output_directory}/kek.auth"

mkdir -p "$output_directory"

echo "creating device Secure Boot keys in ${output_directory}..."

# Generate device-specific PK
openssl req -nodes -new -x509 -newkey rsa:4096 \
    -keyout "$pk_private_key" \
    -out "$pk_pem" -days 36500 \
    -subj "/CN=Puavo Device Platform Key/" >/dev/null 2>&1
chmod 600 "$pk_private_key" "$pk_pem"

# Generate device-specific KEK
openssl req -nodes -new -x509 -newkey rsa:4096 \
    -keyout "$kek_private_key" \
    -out "$kek_pem" -days 36500 \
    -subj "/CN=Puavo Device Key Exchange Key/" >/dev/null 2>&1
chmod 600 "$kek_private_key" "$kek_pem"

# Generate device Secure Boot key (DB)
# The TPM holds this key at runtime, and most TPMs do not support RSA 4096.
openssl req -nodes -new -x509 -newkey rsa:2048 \
    -keyout "$db_private_key" \
    -out "$db_pem" -days 36500 \
    -subj "/CN=Puavo Device Secure Boot Key/" >/dev/null 2>&1
chmod 600 "$db_private_key" "$db_pem"

scratch=$(mktemp -d)
cleanup() { rm -rf "$scratch"; }
trap cleanup EXIT INT TERM
guid='7cb44677-9bb9-4504-bb8f-923def5fa3b1'

# The enrolled DB is the device certificate followed by the image database.
db_esl="${scratch}/db.esl"
cert-to-efi-sig-list -g "$guid" "$db_pem" "$db_esl"
image_db_esl="${secure_boot_directory}/db.esl"

if [ ! -f "$image_db_esl" ]; then
  echo "error: no Secure Boot database at ${image_db_esl}" >&2
  exit 1
fi

built_file="${secure_boot_directory}/built"
if [ ! -f "$built_file" ]; then
  echo "error: no Secure Boot database date at ${built_file}" >&2
  exit 1
fi

cat "$image_db_esl" >> "$db_esl"

# Build signature list for the device KEK
kek_esl="${scratch}/KEK.esl"
cert-to-efi-sig-list -g "$guid" "$kek_pem" "$kek_esl"

# Build signature list for the device PK
pk_esl="${scratch}/PK.esl"
cert-to-efi-sig-list -g "$guid" "$pk_pem" "$pk_esl"

# The firmware rejects an update whose timestamp is not later than the stored
# one, so the date comes from the database, not from the clock of this machine.
built=$(cat "$built_file")
case $built in
  [0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]) ;;
  *)
    echo "error: '${built}' in ${built_file} is not a build date in the form YYYYMMDDHHMMSS" >&2
    exit 1
    ;;
esac
signed_at=$(echo "$built" \
  | sed -E 's/(....)(..)(..)(..)(..)(..)/\1-\2-\3 \4:\5:\6/')

sign-efi-sig-list -t "$signed_at" -k "$kek_private_key" -c "$kek_pem" \
  db "$db_esl" "$db_auth"
sign-efi-sig-list -t "$signed_at" -k "$pk_private_key" -c "$pk_pem" \
  KEK "$kek_esl" "$kek_auth"
sign-efi-sig-list -t "$signed_at" -k "$pk_private_key" -c "$pk_pem" \
  PK "$pk_esl" "$pk_auth"

# The enrolled database, read by the boot trust manager to detect a newer one.
printf '{"digest":"%s","built":%s}\n' \
  "$(sha256sum "$image_db_esl" | cut -d' ' -f1)" "$built" > "$db_enrolled"

echo "successfully created device Secure Boot keys"
