interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / scripts / pi5-8 / owner_ceremony.sh

258 lines · 17382 bytes Raw Blame History
1#!/usr/bin/env bash
2# SPDX-License-Identifier: Apache-2.0
3# Copyright 2026 Drake Stapleton (AIEN / aien-dev)
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17# owner_ceremony.sh -- PI5-8 Q2: the AIENOS owner-key ceremony on Drake's YubiKey (PIV, Ed25519, slot 9c).
18#
19# owner_ceremony.sh --dry-run [--out DIR] rehearsal with a SOFTWARE key (openssl). Labelled TEST. Never touches a YubiKey.
20# owner_ceremony.sh --real [--out DIR] the ceremony. Writes to the YubiKey. Run it only when the Spark is offline.
21# owner_ceremony.sh --real --online-by-owner-decision [--out DIR]
22# the ceremony on a Spark that stays online (owner decision, used 2026-10-14
23# because the Spark has no screen). Skips only the offline check and records
24# the network state in ceremony-report.txt.
25# owner_ceremony.sh --sign-manifest MANIFEST SIGOUT OWNERFILE
26# after the ceremony: the YubiKey signs the statement that binds MANIFEST
27# (PIN once, then touch). Checks the signature against OWNERFILE before it finishes.
28# owner_ceremony.sh --check-network exit 0 only if the machine looks offline (what --real checks first)
29#
30# Decisions (PI5-8 Q2, Drake): the owner key is an Ed25519 key generated ON the YubiKey (PIV, slot 9c, touch policy always, PIN
31# policy once). The private key never leaves the device. The Spark is offline while the key is made. Drake types the PIV PIN,
32# PUK and management key himself; this script never receives, echoes, stores or logs them: the YubiKey tools ask for them on
33# the terminal. The owner key is NOT the public-release signing key (that one is generated only at the first public release).
34#
35# What is signed: a small fixed statement that binds the manifest (ck_manifest_sig.c, docs/native/PI5-8-OWNER-CEREMONY.md).
36# Real mode writes (and only ever reads from the YubiKey the public parts): slot 9c key, PIN, PUK, management key.
37# It refuses to run if slot 9c is already occupied. Output folder (default ~/aienos-owner-ceremony/<UTC time>) holds PUBLIC
38# files only: slot-9c-public.pem, owner-pubkeys.txt, owner-pubkeys.txt.sha256, ceremony-statement, ceremony-signature,
39# ceremony-report.txt.
40set -euo pipefail
41export LC_ALL=C
42case $- in *x*) echo "REFUSED: shell tracing (set -x) would print secrets and commands; run without it" >&2; exit 2;; esac
43here="$(cd "$(dirname "$0")" && pwd)"
44root="$(cd "${here}/../.." && pwd)"
45PIV="${AIENOS_PIV_TOOL:-${HOME}/.local/yubico-piv-tool/bin/yubico-piv-tool}"
46YKMAN="${AIENOS_YKMAN:-${HOME}/.local/ykman-venv/bin/ykman}"
47export LD_LIBRARY_PATH="$(dirname "$(dirname "${PIV}")")/lib${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}"
48SLOT=9c
49mode="" out="" tmp="" online_ok="" online_state=""
50say() { printf '%s\n' "$*"; }
51die() { printf 'REFUSED: %s\n' "$*" >&2; exit "${2:-1}"; }
52cleanup() { [ -z "${tmp}" ] || rm -rf "${tmp}"; }
53trap cleanup EXIT
54
55# ---- network check (real mode): refuse unless the machine is offline ----------------------------------------------
56network_state() { # prints reasons the machine is online, nothing when offline (refuses to guess without ip)
57 command -v ip >/dev/null || { echo "cannot check: the ip tool is missing"; return 0; }
58 ip -o route show default 2>/dev/null | sed 's/^/default route: /'
59 ip -o -6 route show default 2>/dev/null | sed 's/^/default route (v6): /'
60 local d n
61 for d in "${AIENOS_SYSFS_NET:-/sys/class/net}"/*; do
62 n="$(basename "${d}")"
63 case "$(readlink -f "${d}")" in /sys/devices/virtual/*) continue;; esac
64 [ "$(cat "${d}/operstate" 2>/dev/null || echo unknown)" = up ] && echo "network card ${n} is up"
65 done
66 return 0
67}
68require_offline() {
69 local s; s="$(network_state)"
70 if [ -n "${s}" ]; then
71 printf '%s\n' "${s}" >&2
72 die "the Spark is still online. Unplug the network cable and turn Wi-Fi off, wait ten seconds, run this again." 3
73 fi
74}
75
76# ---- tools -------------------------------------------------------------------------------------------------------------
77vge() { [ "$(printf '%s\n%s\n' "$2" "$1" | sort -V | head -n 1)" = "$2" ]; } # vge A B: A >= B
78need_tools() {
79 [ -x "${PIV}" ] || die "yubico-piv-tool not found at ${PIV}; run scripts/pi5-8/install_owner_tools.sh" 2
80 [ -x "${YKMAN}" ] || die "ykman not found at ${YKMAN}; run scripts/pi5-8/install_owner_tools.sh" 2
81 local v; v="$("${PIV}" --version 2>&1 | awk '{print $2}')"
82 vge "${v}" 2.5.0 || die "yubico-piv-tool ${v} is too old for PIV Ed25519 (needs 2.5.0 or newer; installed script builds 2.7.3)" 2
83}
84build_host_tools() { # in-tree verifier and owner-file checker, built from the repo
85 cc -std=gnu11 -O1 -Wall -Wextra -Werror -I"${root}/native/sig" -I"${root}/native/argus" -o "${tmp}/ck_manifest_sig" \
86 "${root}/native/kernel/tools/ck_manifest_sig.c" "${root}/native/sig/ed25519.c" "${root}/native/sig/sha512.c" \
87 "${root}/native/sig/ct.c" "${root}/native/argus/sha256.c"
88 cc -std=gnu11 -O1 -Wall -Wextra -Werror -o "${tmp}/ck_owner_gen" "${root}/native/kernel/tools/ck_owner_gen.c"
89}
90
91# ---- backends: software (dry run) and YubiKey (real) --------------------------------------------------------------------
92sw_generate() { # PUBPEM
93 openssl genpkey -algorithm ED25519 -out "${tmp}/TEST-software-key.pem" 2>/dev/null
94 openssl pkey -in "${tmp}/TEST-software-key.pem" -pubout -out "$1"
95}
96sw_sign() { openssl pkeyutl -sign -rawin -inkey "${tmp}/TEST-software-key.pem" -in "$1" -out "$2"; }
97yk_info() {
98 say "YubiKey seen by the smart card service:"
99 "${PIV}" -a list-readers
100 local n; n="$("${PIV}" -a list-readers | grep -ci yubikey || true)"
101 [ "${n}" = 1 ] || die "need exactly one YubiKey plugged in, found ${n}" 2
102 "${YKMAN}" info | grep -E '^(Device type|Serial number|Firmware version|Form factor)'
103 local fw; fw="$("${PIV}" -a version 2>&1 | sed -n 's/^Firmware version \([0-9.]*\) found.*/\1/p')"
104 vge "${fw:-0}" 5.7.0 || die "firmware ${fw:-unknown} cannot hold PIV Ed25519 keys (needs 5.7.0 or newer)" 2
105}
106yk_slot_must_be_empty() {
107 if "${PIV}" -a status 2>/dev/null | grep -qi "^Slot ${SLOT}"; then die "slot ${SLOT} already holds a key; this script never overwrites a key" 2; fi
108 say "Slot ${SLOT} is empty."
109}
110yk_is_default() { "${YKMAN}" piv info 2>/dev/null | grep -q "WARNING: Using default $1"; } # $1: PIN | PUK | Management key
111yk_set_secrets() {
112 # The YubiKey tool asks for each new secret twice, so a typing slip cannot lock you out. Factory values are public
113 # (PIN 123456, PUK 12345678, management key 0102...08 x3) and are passed only while the device still reports them.
114 local ok
115 if yk_is_default PIN; then
116 say "STEP 3a: choose your new PIV PIN (6 to 8 characters). Type it, then type it again."
117 "${PIV}" -a change-pin -P 123456
118 else
119 say "The PIN is no longer the factory PIN. Change it again only if you want to."
120 read -r -p "Change the PIN now? [y/N] " ok; if [ "${ok}" = y ]; then "${PIV}" -a change-pin; fi
121 fi
122 if yk_is_default PUK; then
123 say "STEP 3b: choose your new PUK (6 to 8 characters). It unblocks the PIN if the PIN gets blocked. Type it, then again."
124 "${PIV}" -a change-puk -P 12345678
125 else
126 say "The PUK is no longer the factory PUK; keeping it."
127 fi
128 if yk_is_default "Management key"; then
129 say "STEP 3c: choose your new management key: 64 hexadecimal characters (0-9, a-f), the one you wrote on paper. Type it, then again."
130 "${PIV}" -a set-mgm-key -m AES256
131 else
132 say "The management key is no longer the factory one; keeping it."
133 fi
134 "${YKMAN}" piv info | grep -E 'tries remaining|Management key algorithm' || true
135 if "${YKMAN}" piv info | grep -q "WARNING: Using default"; then die "something is still set to the factory value; stopping" 1; fi
136}
137yk_generate() { # PUBPEM
138 say "STEP: the YubiKey now makes the owner key inside itself (Ed25519, slot ${SLOT}, touch always, PIN once). Type the management key when asked."
139 "${PIV}" -k -a generate -s "${SLOT}" -A ED25519 --pin-policy=once --touch-policy=always -o "$1"
140}
141yk_sign() { # IN OUT
142 say "STEP: the YubiKey signs. Type the PIN when asked, then TOUCH the metal contact on the key when it blinks."
143 "${PIV}" -a verify-pin --sign -s "${SLOT}" -A ED25519 -i "$1" -o "$2"
144}
145yk_check_policy() {
146 local info; info="$("${YKMAN}" piv keys info "${SLOT}")"
147 say "${info}"
148 grep -qi "Algorithm:.*ED25519" <<<"${info}" || die "slot ${SLOT} is not an Ed25519 key" 1
149 grep -qi "Origin:.*GENERATED" <<<"${info}" || die "slot ${SLOT} key was not generated on the YubiKey" 1
150 grep -qi "PIN required for use:.*ONCE" <<<"${info}" || die "PIN policy is not ONCE" 1
151 grep -qi "Touch required for use:.*ALWAYS" <<<"${info}" || die "touch policy is not ALWAYS" 1
152}
153
154# ---- shared steps ------------------------------------------------------------------------------------------------------
155write_and_check_owner_file() { # PUBPEM OUTDIR LABEL
156 local hex
157 hex="$("${here}/owner_pubkey_file.sh" "$1" "$2/owner-pubkeys.txt" "$3")" || die "could not convert the public key" 1
158 ( cd "$2" && sha256sum owner-pubkeys.txt > owner-pubkeys.txt.sha256 )
159 # ck_owner_gen is the same checker the image build uses; it also refuses RFC 8032 TEST keys and all-equal keys.
160 printf 'machine_id %s\nstore_uuid %s\n' "$(openssl rand -hex 32)" "$(openssl rand -hex 16)" > "${tmp}/placeholder-machine.txt" # random placeholder, only checks the owner line
161 "${tmp}/ck_owner_gen" "$2/owner-pubkeys.txt" "${tmp}/placeholder-machine.txt" "${tmp}/owner_prov.h" || die "the image build would refuse this owner file" 1
162 OWNER_HEX="${hex}"
163 say "Owner public key (PUBLIC): ${hex}"
164 say "Owner file sha256: $(cut -d' ' -f1 "$2/owner-pubkeys.txt.sha256")"
165}
166sign_and_verify() { # SIGN_FN MANIFEST SIGOUT HEX LABEL
167 local fn="$1" man="$2" sig="$3" hex="$4" st="${tmp}/statement"
168 "${tmp}/ck_manifest_sig" statement "${man}" "${st}"
169 "${fn}" "${st}" "${sig}"
170 [ "$(wc -c < "${sig}")" = 64 ] || die "signature is not 64 bytes" 1
171 "${tmp}/ck_manifest_sig" verify "${man}" "${sig}" "${hex}" || die "the signature does not verify with the in-tree Ed25519" 1
172 # second, independent verifier: openssl over the same statement bytes (plain Ed25519, no hashing wrapper)
173 local pem="${tmp}/v.pem"
174 # SPKI DER = fixed 12-byte prefix + the 32 key bytes, written as raw bytes (not through $(...), which drops NUL)
175 printf "$(printf '%s' "302a300506032b6570032100${hex}" | sed 's/../\\x&/g')" | openssl pkey -pubin -inform DER -out "${pem}" 2>/dev/null \
176 || die "could not rebuild the public key for the second check" 1
177 openssl pkeyutl -verify -rawin -pubin -inkey "${pem}" -in "${st}" -sigfile "${sig}" >/dev/null || die "openssl does not verify the signature" 1
178 say "Signature verified two ways (in-tree Ed25519 and openssl)."
179 cp "${man}" "${tmp}/m.bad"; printf 'X' | dd of="${tmp}/m.bad" bs=1 seek=3 conv=notrunc 2>/dev/null
180 if "${tmp}/ck_manifest_sig" verify "${tmp}/m.bad" "${sig}" "${hex}" >/dev/null; then die "a changed manifest still verified" 1; fi
181 say "A changed manifest is refused, as it must be."
182}
183test_manifest() { printf 'AIENOS-OWNER-CEREMONY-TEST-MANIFEST-V1\nthis is a fixed test text, not a receipts manifest\n' > "$1"; }
184
185ceremony() { # dry|real
186 local kind="$1" label
187 if [ -z "${out}" ]; then if [ "${kind}" = real ]; then out="${HOME}/aienos-owner-ceremony/$(date -u +%Y%m%dT%H%M%SZ)"; else out="$(mktemp -d /tmp/aienos-owner-dryrun.XXXXXX)"; fi; fi
188 build_host_tools
189 if [ "${kind}" = dry ]; then
190 say "DRY RUN (TEST): a software key stands in for the YubiKey. No YubiKey is touched, nothing real is created."
191 say "Steps 1 to 4 of the real ceremony (offline check, YubiKey, PIN/PUK/management key, key generation) are replaced by a software key."
192 label="TEST software key (dry run), not an owner key"
193 mkdir -p "${out}"; chmod 700 "${out}"
194 sw_generate "${out}/slot-9c-public.pem"
195 else
196 [ -t 0 ] && [ -t 1 ] || die "run this at the keyboard of the Spark (it asks you to type secrets)" 2
197 if [ -n "${online_ok}" ]; then
198 online_state="$(network_state | tr "\n" ";")"
199 say "STEP 1: offline check SKIPPED on purpose (--online-by-owner-decision). The Spark is ONLINE: ${online_state}"
200 say "This is recorded in the ceremony report."
201 else say "STEP 1: checking the Spark is offline."; require_offline; say "Offline: OK."; fi
202 say "STEP 2: checking the tools and the YubiKey."; need_tools; yk_info
203 read -r -p "Does that serial number match your YubiKey (type YES to go on)? " ans; [ "${ans}" = YES ] || die "stopped by you" 1
204 yk_slot_must_be_empty
205 mkdir -p "${out}"; chmod 700 "${out}"
206 say "STEP 3: PIN, PUK and management key (typed by you, never shown here)."; yk_set_secrets
207 say "STEP 4: making the owner key inside the YubiKey."; yk_generate "${out}/slot-9c-public.pem"
208 "${PIV}" -a read-public-key -s "${SLOT}" -o "${tmp}/read-back.pem"
209 cmp -s "${tmp}/read-back.pem" "${out}/slot-9c-public.pem" || die "the key read back from slot ${SLOT} differs from the generated one" 1
210 yk_check_policy
211 label="Ed25519 key generated on YubiKey serial $("${YKMAN}" info | sed -n 's/^Serial number: *//p'), PIV slot ${SLOT}"
212 fi
213 say "STEP 5: owner public key file."; write_and_check_owner_file "${out}/slot-9c-public.pem" "${out}" "${label}"
214 test_manifest "${out}/ceremony-test-manifest"
215 say "STEP 6: signing and checking a fixed test statement."
216 if [ "${kind}" = dry ]; then sign_and_verify sw_sign "${out}/ceremony-test-manifest" "${out}/ceremony-signature" "${OWNER_HEX}" TEST
217 else sign_and_verify yk_sign "${out}/ceremony-test-manifest" "${out}/ceremony-signature" "${OWNER_HEX}" real; fi
218 cp "${tmp}/statement" "${out}/ceremony-statement"
219 {
220 echo "AIENOS owner ceremony report (${kind}); PUBLIC data only"
221 echo "kind: $([ "${kind}" = dry ] && echo 'DRY RUN, TEST software key, not an owner key' || echo 'REAL, key generated on the YubiKey')"
222 echo "utc: $(date -u +%Y-%m-%dT%H:%M:%SZ)"
223 echo "key: ${label}"
224 echo "owner_root_ed25519: ${OWNER_HEX}"
225 echo "owner_file_sha256: $(cut -d' ' -f1 "${out}/owner-pubkeys.txt.sha256")"
226 echo "signed_statement_sha256: $(sha256sum < "${out}/ceremony-statement" | cut -d' ' -f1)"
227 net="ok"; [ "${kind}" = dry ] && net="n/a"
228 [ -n "${online_ok}" ] && net="NOT CHECKED: ONLINE by owner decision (Drake 2026-10-14, Spark has no screen output, run over a remote login); state: ${online_state}"
229 echo "checks: network offline ${net}; owner file accepted by ck_owner_gen; signature verified by in-tree Ed25519 and openssl; changed manifest refused"
230 } > "${out}/ceremony-report.txt"
231 say ""
232 if [ "${kind}" = dry ]; then say "DRY RUN COMPLETE (TEST). Files in ${out}"; say "CEREMONY_DRY_RUN: PASS"
233 else say "CEREMONY COMPLETE. Public files in ${out}. You may reconnect the network now."; say "CEREMONY_REAL: PASS"; fi
234}
235
236sign_manifest() { # MANIFEST SIGOUT OWNERFILE (real YubiKey)
237 local man="$1" sigout="$2" ofile="$3" hex
238 [ -r "${man}" ] || die "cannot read manifest ${man}" 2
239 hex="$(sed -n 's/^owner_root_ed25519 \([0-9a-f]\{64\}\)$/\1/p' "${ofile}")"; [ -n "${hex}" ] || die "no owner_root_ed25519 line in ${ofile}" 2
240 need_tools; build_host_tools
241 "${PIV}" -a read-public-key -s "${SLOT}" -o "${tmp}/slot.pem" || die "cannot read the public key of slot ${SLOT}" 1
242 "${here}/owner_pubkey_file.sh" "${tmp}/slot.pem" "${tmp}/slot-owner.txt" >/dev/null 2>&1 || die "slot ${SLOT} does not hold an Ed25519 key" 1
243 grep -qx "owner_root_ed25519 ${hex}" "${tmp}/slot-owner.txt" || die "the key in slot ${SLOT} is not the owner key in ${ofile}" 1
244 sign_and_verify yk_sign "${man}" "${tmp}/sig.out" "${hex}" real
245 cp "${tmp}/sig.out" "${sigout}"
246 say "Wrote ${sigout}. MANIFEST_OWNER_SIGNED: PASS"
247}
248
249tmp="$(mktemp -d)"
250case "${1:-}" in
251 --dry-run) mode=dry; shift;;
252 --real) mode=real; shift;;
253 --check-network) s="$(network_state)"; if [ -n "${s}" ]; then printf '%s\n' "${s}"; echo "ONLINE"; exit 1; fi; echo "OFFLINE"; exit 0;;
254 --sign-manifest) [ $# = 4 ] || die "usage: --sign-manifest MANIFEST SIGOUT OWNERFILE" 2; sign_manifest "$2" "$3" "$4"; exit 0;;
255 *) sed -n '2,13p' "$0" | sed 's/^# \{0,1\}//' >&2; exit 2;;
256esac
257while [ $# -gt 0 ]; do case "$1" in --out) out="${2:?--out DIR}"; shift 2;; --online-by-owner-decision) online_ok=1; shift;; *) die "unknown argument $1" 2;; esac; done
258ceremony "${mode}"