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 / trust1_key_ceremony.sh

340 lines · 16303 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# trust1_key_ceremony.sh: TRUST-1 Gate 3 owner key ceremony tool.
18#
19# Run ONLY on the offline ceremony machine the operator chose, never on the
20# Spark. Creates the generation-1 owner hierarchy:
21# owner_root Ed25519 signs the authority manifest; never on the Spark
22# boot_signer RSA-2048 self-signed X.509 (the cert UEFI db will hold
23# later, at Gate 6; nothing is enrolled here)
24# release_signer Ed25519 signs kernels/manifests under the loader
25# operator_approval Ed25519 approves boundary changes; never boots
26#
27# Private keys are written ENCRYPTED from the start (openssl genpkey with a
28# passphrase): no plaintext private key file is ever created. Public halves,
29# the signed authority manifest and the ceremony record contain no private
30# material and are the only things that leave the machine.
31#
32# Subcommands:
33# preflight [--allow-online] tool versions; refuses if a network route exists
34# generate OUT_DIR [--allow-online] create keys + signed manifest (OUT_DIR must not exist)
35# backup OUT_DIR DEST_DIR LABEL copy encrypted keys to backup media, then
36# prove every key decrypts FROM THE COPY
37# verify PUBLIC_DIR [MIN_GEN] check manifest signature, fingerprints and
38# that generation >= MIN_GEN (anti-rollback)
39# record OUT_DIR write public/ceremony_record.txt
40# rotate OUT_DIR ROLE add|revoke rotate release_signer or operator_approval:
41# 'add' signs a manifest trusting old AND new;
42# exercise recovery; then 'revoke' signs one
43# trusting only new (old key quarantined,
44# never deleted). Each step is a new generation.
45#
46# Passphrase: asked twice on the terminal, or from AIENOS_CEREMONY_PASS_FILE
47# (self-test only). Works with OpenSSL 3 on Linux or macOS (set AIENOS_OPENSSL
48# to a Homebrew openssl@3 binary; the macOS system LibreSSL is refused).
49# Exit: 0 ok, 1 check failed, 2 usage or environment problem.
50
51set -euo pipefail
52
53OPENSSL="${AIENOS_OPENSSL:-openssl}"
54ROLES="owner_root boot_signer release_signer operator_approval"
55
56die() { echo "Error: $*" >&2; exit 2; }
57sha256() { "${OPENSSL}" dgst -sha256 -r "$1" | cut -d' ' -f1; }
58now_utc() { date -u +%Y-%m-%dT%H:%M:%SZ; }
59
60check_tools() {
61 command -v "${OPENSSL}" >/dev/null || die "openssl not found"
62 local v
63 v="$("${OPENSSL}" version)"
64 case "${v}" in
65 "OpenSSL 3."*) ;;
66 *) die "need OpenSSL 3 (found: ${v}); on macOS: brew install openssl@3 and set AIENOS_OPENSSL" ;;
67 esac
68}
69
70network_route() {
71 if command -v ip >/dev/null 2>&1; then
72 ip route show default 2>/dev/null | grep -q . && return 0
73 ip -6 route show default 2>/dev/null | grep -q . && return 0
74 return 1
75 fi
76 if command -v route >/dev/null 2>&1; then
77 route -n get default >/dev/null 2>&1 && return 0
78 return 1
79 fi
80 return 0 # cannot tell: treat as online
81}
82
83preflight() {
84 local allow_online="${1:-}"
85 check_tools
86 echo "openssl: $("${OPENSSL}" version)"
87 echo "host: $(uname -s) $(uname -m)"
88 if network_route; then
89 if [[ "${allow_online}" == "--allow-online" ]]; then
90 echo "network: ONLINE (allowed for a test run only)"
91 else
92 die "a network route exists; disconnect Wi-Fi and cables first (ceremony must be offline)"
93 fi
94 else
95 echo "network: offline"
96 fi
97}
98
99get_pass() {
100 if [[ -n "${AIENOS_CEREMONY_PASS_FILE:-}" ]]; then
101 AIENOS_CEREMONY_PASS="$(cat "${AIENOS_CEREMONY_PASS_FILE}")"
102 else
103 local a b
104 read -r -s -p "Ceremony passphrase: " a; echo
105 read -r -s -p "Again: " b; echo
106 [[ "${a}" == "${b}" ]] || die "passphrases differ"
107 AIENOS_CEREMONY_PASS="${a}"
108 fi
109 [[ ${#AIENOS_CEREMONY_PASS} -ge 12 ]] || die "passphrase shorter than 12 characters"
110 export AIENOS_CEREMONY_PASS
111}
112
113pub_fingerprint() { # role public_dir
114 if [[ "$1" == boot_signer ]]; then
115 sha256 "$2/boot_signer.crt.der"
116 else
117 "${OPENSSL}" pkey -pubin -in "$2/$1.pub.pem" -outform DER 2>/dev/null \
118 | "${OPENSSL}" dgst -sha256 -r | cut -d' ' -f1
119 fi
120}
121
122generate() {
123 local out="$1" allow="${2:-}"
124 [[ ! -e "${out}" ]] || die "${out} already exists; never overwrite a ceremony"
125 preflight "${allow}"
126 get_pass
127 umask 077
128 mkdir -p "${out}/private" "${out}/public"
129 local r
130 for r in owner_root release_signer operator_approval; do
131 "${OPENSSL}" genpkey -algorithm ed25519 -aes-256-cbc -pass env:AIENOS_CEREMONY_PASS \
132 -out "${out}/private/${r}.key.pem"
133 "${OPENSSL}" pkey -in "${out}/private/${r}.key.pem" -passin env:AIENOS_CEREMONY_PASS \
134 -pubout -out "${out}/public/${r}.pub.pem"
135 done
136 "${OPENSSL}" genpkey -algorithm rsa -pkeyopt rsa_keygen_bits:2048 -aes-256-cbc \
137 -pass env:AIENOS_CEREMONY_PASS -out "${out}/private/boot_signer.key.pem"
138 "${OPENSSL}" req -new -x509 -sha256 -days 3650 -key "${out}/private/boot_signer.key.pem" \
139 -passin env:AIENOS_CEREMONY_PASS -subj "/CN=AIENOS Owner Boot Signer gen1/O=AIEN/" \
140 -addext "extendedKeyUsage=codeSigning" -out "${out}/public/boot_signer.crt.pem"
141 "${OPENSSL}" x509 -in "${out}/public/boot_signer.crt.pem" -outform DER \
142 -out "${out}/public/boot_signer.crt.der"
143
144 local m="${out}/public/authority_manifest.txt"
145 {
146 echo "aienos_authority_manifest=1"
147 echo "generation=1"
148 echo "created_utc=$(now_utc)"
149 echo "tool=$("${OPENSSL}" version)"
150 echo "rotation_rule=add new key, verify, exercise recovery, then revoke old; never delete first"
151 echo "root_replacement=special recovery ceremony only"
152 echo "role.owner_root.alg=ed25519"
153 echo "role.owner_root.duty=signs authority manifests; never on the Spark"
154 echo "role.boot_signer.alg=rsa2048-x509"
155 echo "role.boot_signer.duty=signs the small stable loader; firmware db will trust this cert"
156 echo "role.release_signer.alg=ed25519"
157 echo "role.release_signer.duty=signs kernels and manifests under the loader; replaceable by a root-signed manifest"
158 echo "role.operator_approval.alg=ed25519"
159 echo "role.operator_approval.duty=approves boundary changes; never boots"
160 for r in ${ROLES}; do
161 echo "role.${r}.pub_sha256=$(pub_fingerprint "${r}" "${out}/public")"
162 done
163 } >"${m}"
164 "${OPENSSL}" pkeyutl -sign -rawin -inkey "${out}/private/owner_root.key.pem" \
165 -passin env:AIENOS_CEREMONY_PASS -in "${m}" -out "${out}/public/authority_manifest.sig"
166 cp "${m}" "${out}/public/authority_manifest.gen1.txt"
167 cp "${out}/public/authority_manifest.sig" "${out}/public/authority_manifest.gen1.sig"
168 chmod 644 "${out}/public/"*
169 verify "${out}/public"
170 echo "generated: ${out} (private/ stays offline; public/ may be copied out)"
171}
172
173verify() {
174 local pub="$1" min_gen="${2:-1}" r want got gen status=0
175 check_tools
176 local m="${pub}/authority_manifest.txt"
177 [[ -f "${m}" && -f "${pub}/authority_manifest.sig" ]] || { echo "FAIL manifest or signature missing"; return 1; }
178 if "${OPENSSL}" pkeyutl -verify -rawin -pubin -inkey "${pub}/owner_root.pub.pem" \
179 -in "${m}" -sigfile "${pub}/authority_manifest.sig" >/dev/null 2>&1; then
180 echo "PASS authority manifest signature verifies with owner_root"
181 else
182 echo "FAIL authority manifest signature does not verify"; return 1
183 fi
184 for r in ${ROLES}; do
185 want="$(sed -n "s/^role\.${r}\.pub_sha256=//p" "${m}")"
186 got="$(pub_fingerprint "${r}" "${pub}" || true)"
187 if [[ -n "${want}" && "${want}" == "${got}" ]]; then
188 echo "PASS ${r} public key matches manifest (${got})"
189 else
190 echo "FAIL ${r} public key does not match manifest"; status=1
191 fi
192 want="$(sed -n "s/^role\.${r}\.next\.pub_sha256=//p" "${m}")"
193 if [[ -n "${want}" ]]; then
194 got="$(pub_fingerprint "${r}.next" "${pub}" || true)"
195 if [[ "${want}" == "${got}" ]]; then
196 echo "PASS ${r} next key (transition) matches manifest (${got})"
197 else
198 echo "FAIL ${r} next key does not match manifest"; status=1
199 fi
200 fi
201 done
202 gen="$(sed -n 's/^generation=//p' "${m}")"
203 if [[ "${gen}" =~ ^[0-9]+$ && "${gen}" -ge "${min_gen}" ]]; then
204 echo "PASS generation ${gen} (minimum accepted ${min_gen})"
205 else
206 echo "FAIL generation ${gen:-missing} is older than the minimum ${min_gen} (rollback refused)"; status=1
207 fi
208 if grep -l "PRIVATE KEY" "${pub}"/* >/dev/null 2>&1; then
209 echo "FAIL private key material found in public folder"; status=1
210 fi
211 echo "manifest_sha256=$(sha256 "${m}")"
212 return "${status}"
213}
214
215backup() {
216 local out="$1" dest="$2" label="$3" r ok=1
217 [[ -d "${out}/private" ]] || die "${out}/private not found"
218 [[ -d "${dest}" ]] || die "backup destination ${dest} is not a folder (mount the backup medium first)"
219 local b="${dest}/aienos-owner-keys-gen1"
220 [[ ! -e "${b}" ]] || die "${b} already exists on this medium"
221 get_pass
222 umask 077
223 mkdir -p "${b}"
224 cp "${out}/private/"*.key.pem "${out}/public/"* "${b}/"
225 sync
226 for r in ${ROLES}; do
227 if "${OPENSSL}" pkey -in "${b}/${r}.key.pem" -passin env:AIENOS_CEREMONY_PASS -noout 2>/dev/null; then
228 echo "PASS ${label}: ${r} decrypts from the backup copy"
229 else
230 echo "FAIL ${label}: ${r} does not decrypt from the backup copy"; ok=0
231 fi
232 done
233 [[ "${ok}" == 1 ]] || return 1
234 local digest
235 digest="$(cat "${b}"/*.key.pem | "${OPENSSL}" dgst -sha256 -r | cut -d' ' -f1)"
236 echo "backup label=${label} encrypted_keys_sha256=${digest} verified_utc=$(now_utc)" \
237 >>"${out}/public/backups.txt"
238 echo "recorded backup ${label}"
239}
240
241record() {
242 local out="$1" pub="$1/public"
243 verify "${pub}" >/dev/null || { echo "FAIL verify failed; no record written"; return 1; }
244 local n=0
245 [[ -f "${pub}/backups.txt" ]] && n="$(grep -c '^backup ' "${pub}/backups.txt")"
246 {
247 echo "aienos_key_ceremony_record=1"
248 echo "recorded_utc=$(now_utc)"
249 echo "tool=$("${OPENSSL}" version)"
250 echo "host=$(uname -s) $(uname -m)"
251 echo "procedure=docs/TRUST-1-OPERATOR-STEPS.md (Gate 3)"
252 echo "manifest_sha256=$(sha256 "${pub}/authority_manifest.txt")"
253 grep '^role\..*\.pub_sha256=' "${pub}/authority_manifest.txt"
254 echo "backup_count=${n}"
255 [[ -f "${pub}/backups.txt" ]] && cat "${pub}/backups.txt"
256 echo "firmware_changed=no"
257 echo "operator_signature=<operator signs here by hand or with operator_approval>"
258 } >"${pub}/ceremony_record.txt"
259 if grep -q "PRIVATE KEY" "${pub}/ceremony_record.txt"; then
260 echo "FAIL record contains private material"; return 1
261 fi
262 [[ "${n}" -ge 2 ]] || echo "NOTE only ${n} verified backup(s); Gate 3 needs 2 in separate places"
263 echo "wrote ${pub}/ceremony_record.txt"
264}
265
266# Rotation (Gate 3 acceptance: add -> verify -> exercise recovery -> revoke;
267# never delete first). Only release_signer and operator_approval rotate here:
268# boot_signer rotation touches firmware db (a Gate 7 style attended step) and
269# owner_root replacement is a special recovery ceremony.
270write_manifest() { # out generation extra_lines_file
271 local out="$1" gen="$2" extra="$3" m="$1/public/authority_manifest.txt" r
272 {
273 echo "aienos_authority_manifest=1"
274 echo "generation=${gen}"
275 echo "created_utc=$(now_utc)"
276 echo "tool=$("${OPENSSL}" version)"
277 sed -n '/^rotation_rule=/p;/^root_replacement=/p;/^role\.[a-z_]*\.alg=/p;/^role\.[a-z_]*\.duty=/p' \
278 "${out}/public/authority_manifest.gen1.txt" 2>/dev/null || true
279 for r in ${ROLES}; do
280 echo "role.${r}.pub_sha256=$(pub_fingerprint "${r}" "${out}/public")"
281 done
282 cat "${extra}"
283 } >"${m}"
284 "${OPENSSL}" pkeyutl -sign -rawin -inkey "${out}/private/owner_root.key.pem" \
285 -passin env:AIENOS_CEREMONY_PASS -in "${m}" -out "${out}/public/authority_manifest.sig"
286 cp "${m}" "${out}/public/authority_manifest.gen${gen}.txt"
287 cp "${out}/public/authority_manifest.sig" "${out}/public/authority_manifest.gen${gen}.sig"
288 chmod 644 "${out}/public/"*
289}
290
291rotate() {
292 local out="$1" role="$2" stage="$3" gen m="$1/public/authority_manifest.txt"
293 case "${role}" in release_signer|operator_approval) ;; *) die "rotate supports release_signer or operator_approval only" ;; esac
294 verify "${out}/public" >/dev/null || die "current manifest does not verify; fix that first"
295 gen="$(sed -n 's/^generation=//p' "${m}")"
296 [[ -f "${out}/public/authority_manifest.gen${gen}.txt" ]] || {
297 cp "${m}" "${out}/public/authority_manifest.gen${gen}.txt"
298 cp "${out}/public/authority_manifest.sig" "${out}/public/authority_manifest.gen${gen}.sig"; }
299 get_pass
300 umask 077
301 local extra; extra="$(mktemp)"
302 case "${stage}" in
303 add)
304 [[ ! -e "${out}/private/${role}.next.key.pem" ]] || die "a next ${role} already exists"
305 "${OPENSSL}" genpkey -algorithm ed25519 -aes-256-cbc -pass env:AIENOS_CEREMONY_PASS \
306 -out "${out}/private/${role}.next.key.pem"
307 "${OPENSSL}" pkey -in "${out}/private/${role}.next.key.pem" -passin env:AIENOS_CEREMONY_PASS \
308 -pubout -out "${out}/public/${role}.next.pub.pem"
309 grep '^role\..*\.revoked_pub_sha256=' "${m}" >"${extra}" || true
310 echo "role.${role}.next.pub_sha256=$(pub_fingerprint "${role}.next" "${out}/public")" >>"${extra}"
311 echo "role.${role}.state=transition (current and next both accepted)" >>"${extra}"
312 ;;
313 revoke)
314 [[ -f "${out}/private/${role}.next.key.pem" ]] || die "run 'rotate ${out} ${role} add' first"
315 grep '^role\..*\.revoked_pub_sha256=' "${m}" >"${extra}" || true
316 echo "role.${role}.revoked_pub_sha256=$(pub_fingerprint "${role}" "${out}/public")" >>"${extra}"
317 # Old key is quarantined, never deleted: kept encrypted for audit.
318 mv "${out}/private/${role}.key.pem" "${out}/private/${role}.gen${gen}.revoked.key.pem"
319 mv "${out}/public/${role}.pub.pem" "${out}/public/${role}.gen${gen}.revoked.pub.pem"
320 mv "${out}/private/${role}.next.key.pem" "${out}/private/${role}.key.pem"
321 mv "${out}/public/${role}.next.pub.pem" "${out}/public/${role}.pub.pem"
322 ;;
323 *) rm -f "${extra}"; die "stage must be add or revoke" ;;
324 esac
325 write_manifest "${out}" "$((gen + 1))" "${extra}"
326 rm -f "${extra}"
327 verify "${out}/public"
328 echo "rotation ${role} ${stage}: generation $((gen + 1)) signed by owner_root"
329}
330
331cmd="${1:-}"; shift || true
332case "${cmd}" in
333 preflight) preflight "${1:-}" ;;
334 generate) [[ $# -ge 1 ]] || die "usage: generate OUT_DIR [--allow-online]"; generate "$1" "${2:-}" ;;
335 backup) [[ $# -eq 3 ]] || die "usage: backup OUT_DIR DEST_DIR LABEL"; backup "$1" "$2" "$3" ;;
336 verify) [[ $# -ge 1 ]] || die "usage: verify PUBLIC_DIR [MIN_GENERATION]"; verify "$1" "${2:-1}" ;;
337 rotate) [[ $# -eq 3 ]] || die "usage: rotate OUT_DIR ROLE add|revoke"; rotate "$1" "$2" "$3" ;;
338 record) [[ $# -eq 1 ]] || die "usage: record OUT_DIR"; record "$1" ;;
339 *) sed -n '2,/^# Exit:/p' "$0"; exit 2 ;;
340esac