Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/scripts/native_package.sh # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / scripts / native_package.sh` 703 lines ยท 38227 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 # native_package.sh -- reproducible AIENOS native package with exact manifests 18 # and owner-signed verification (AIEN native v1 plan, P11 cut 1b, gate N7). 19 # 20 # A package is a directory: 21 # image/BOOTAA64.EFI the image, built twice from a clean tree; the two 22 # builds must be byte-identical or the package is 23 # refused as not signable (TRUST-1 Q24) 24 # PACKAGE.txt the provenance record, "key value" lines (format 3): 25 # source revision (commit, commit time, tree_clean), 26 # toolchain (cross prefix, gcc/ld/objcopy/make version 27 # lines, build host arch), inputs (make goal and 28 # variables, Makefile and image.ld digests, component 29 # pins, model and firmware digests, owner file digests) 30 # and the resulting image (bytes, SHA-256, SHA-512). 31 # See write_package_txt and docs/NATIVE_PACKAGE.md. 32 # loader/SLOTLDR.EFI the signed A/B slot loader (P11 cut 3), built twice like 33 # the image: owner keys (CK_SLOT_KEYS = --owner-pubkeys) 34 # for hardware-staging, the labelled TEST key otherwise 35 # SHA256SUMS sha256sum lines over image/BOOTAA64.EFI, loader/SLOTLDR.EFI 36 # and PACKAGE.txt 37 # PROVENANCE.txt "package_sha256 ": the resulting 38 # package digest, which is also the slot manifest's 39 # provenance field (it cannot live in PACKAGE.txt, 40 # which SHA256SUMS covers) 41 # slot/MANIFEST.bin unsigned slot manifest (native/boot/slot_format.h); 42 # its provenance field is package_sha256, so the 43 # owner's one signature over the manifest binds the 44 # whole package 45 # slot/MANIFEST.tbs the 448 bytes the owner signs offline, written by 46 # `tbs --pubkey` (or `build --pubkey`): signer_id = 47 # SHA-256(pubkey) is inside the signed bytes. 48 # openssl pkeyutl -sign -inkey owner_root.key.pem -rawin \ 49 # -in MANIFEST.tbs -out MANIFEST.sig 50 # slot/MANIFEST.signed.bin after `sign` (or `sign-test`, TEST key only) 51 # 52 # native_package.sh build --out DIR --generation N [--variant core|full|hardware-staging|service-hardware-staging] 53 # [--owner-pubkeys F --machine-id F] (both hardware-staging profiles) 54 # [--policy-generation N] [--min-rollback N] [--pins FILE] [--model FILE] 55 # [--firmware FILE]... [--pubkey HEX64] [--allow-dirty] 56 # native_package.sh tbs --package DIR --pubkey HEX64 57 # native_package.sh sign --package DIR --sig FILE --pubkey HEX64 [--allow-dirty] 58 # native_package.sh sign-test --package DIR RFC 8032 TEST 1 key, tests only 59 # native_package.sh verify --package DIR (--keys FILE | --test-key) [--allow-unsigned] 60 # [--same-host] [--allow-dirty] 61 # 62 # --pins FILE: lines " <40 hex>" copied into PACKAGE.txt after validation. 63 # --model FILE: its sha256 and size are recorded (the frozen model of P09/P12). 64 # --firmware FILE: each file's sha256 and size are recorded (GB10 firmware blobs 65 # of docs/GB10_FIRMWARE_INVENTORY.md). Nothing is loaded or booted. 66 # --allow-dirty (tests only): build from a dirty tree, recorded as tree_clean no; 67 # verify and sign refuse such a package unless given --allow-dirty too 68 # (sign-test does not refuse it: the TEST key is never for hardware). 69 # --same-host: verify compares the recorded toolchain lines with the toolchain 70 # running here; without it they are printed as "recorded". 71 # Any CK_* variable in the environment is refused: every make variable that 72 # selects the image is passed explicitly and recorded. 73 # Build-only. Never writes outside DIR and the scratch build directories. 74 # Every refusal prints "refused: " and exits 1. 75 set -euo pipefail 76 77 here=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) 78 repo=$(cd "$here/.." && pwd) 79 kdir="$repo/native/kernel" 80 FORMAT=3 # 3: P11 cut 3 adds loader/SLOTLDR.EFI and its loader_* lines 81 MAKEFILE_REL=native/kernel/Makefile 82 IMAGE_LD_REL=native/boot/image.ld 83 INFER_MANIFEST_REL=crates/aienos-infer-kernel/Cargo.toml 84 INFER_LOCK_REL=crates/aienos-infer-kernel/Cargo.lock 85 INFER_ARCHIVE=inputs/libaienos_infer_kernel.a 86 87 refuse() { echo "refused: $*" >&2; exit 1; } 88 hex40() { [[ "$1" =~ ^[0-9a-f]{40}$ ]]; } 89 hex64() { [[ "$1" =~ ^[0-9a-f]{64}$ ]]; } 90 sha256_of() { sha256sum "$1" | cut -d' ' -f1; } 91 sha512_of() { sha512sum "$1" | cut -d' ' -f1; } 92 size_of() { stat -c %s "$1"; } 93 94 slot_tool() { 95 local out="${NATIVE_PACKAGE_TOOL_OUT:-$repo/target/native-kernel}" 96 make -s -C "$kdir" OUT="$out" slot-tool >/dev/null 97 echo "$out/host/ck_slot_tool" 98 } 99 100 # ---- toolchain and inputs (what the record says and verify recomputes) ------------- 101 # The cross prefix exactly as the kernel Makefile resolves it (CROSS ?= ..., by host 102 # arch, environment and MAKEFLAGS included), asked from make itself. 103 cross_prefix() { 104 make -s --no-print-directory -C "$kdir" --eval 'np-print-cross: ; @echo "[$(CROSS)]"' np-print-cross \ 105 | sed -n 's/^\[\(.*\)\]$/\1/p' 106 } 107 cross_name() { if [ -n "$1" ]; then echo "$1"; else echo none; fi; } 108 cross_value() { if [ "$1" = none ]; then echo ""; else echo "$1"; fi; } 109 first_line() { "$@" 2>/dev/null | head -1; } 110 # toolchain_lines CROSS: the toolchain lines of PACKAGE.txt, in order. 111 toolchain_lines() { 112 local c="$1" 113 echo "toolchain_cross $(cross_name "$c")" 114 echo "toolchain_cc $(first_line "${c}gcc" --version)" 115 echo "toolchain_ld $(first_line "${c}ld" --version)" 116 echo "toolchain_objcopy $(first_line "${c}objcopy" --version)" 117 echo "toolchain_make $(first_line make --version)" 118 echo "build_host_arch $(uname -m)" 119 } 120 # The make goal and variables build_once passes for a variant ($2/$3: owner file digests). 121 make_goal_of() { case "$1" in core) echo all;; *) echo full;; esac; } 122 make_vars_of() { 123 if [ "$1" = service-hardware-staging ]; then 124 echo "CK_SERVICE=1 CK_HARDWARE_STAGING=1 CK_OWNER_PUBKEYS=sha256:$2 CK_MACHINE_ID=sha256:$3 CK_INFER_LIB=sha256:$4" 125 elif [ "$1" = hardware-staging ]; then 126 echo "CK_HARDWARE_STAGING=1 CK_OWNER_PUBKEYS=sha256:$2 CK_MACHINE_ID=sha256:$3" 127 else 128 echo none 129 fi 130 } 131 owner_variant() { [[ "$1" = hardware-staging || "$1" = service-hardware-staging ]]; } 132 133 # Fresh Cargo home/target per build: user Cargo configuration, environment flags, 134 # wrappers and incremental artifacts cannot silently select the inference image. 135 infer_env() { 136 local home="$1"; shift 137 local wd="$home/work" parent 138 mkdir -p "$wd" 139 # Cargo discovers configuration from cwd and its ancestors even with a fresh 140 # CARGO_HOME. Never invoke it from the caller's or repository's directory. 141 parent=$(realpath "$wd") 142 while :; do 143 [ ! -f "$parent/.cargo/config" ] && [ ! -f "$parent/.cargo/config.toml" ] \ 144 || refuse "unrecorded Cargo configuration above inference scratch directory" 145 [ "$parent" != / ] || break 146 parent=$(dirname "$parent") 147 done 148 (cd "$wd" && env -i PATH="$PATH" HOME="$HOME" RUSTUP_HOME="${RUSTUP_HOME:-$HOME/.rustup}" CARGO_HOME="$home" "$@") 149 } 150 infer_cargo() { local home="$1"; shift; infer_env "$home" cargo "$@"; } 151 infer_toolchain_lines() { 152 local cv rv 153 cv=$(infer_cargo "$1" --version) || return 1 154 rv=$(infer_env "$1" rustc --version) || return 1 155 printf 'toolchain_cargo %s\ntoolchain_rustc %s\n' "$cv" "$rv" 156 } 157 build_infer_once() { 158 local out="$1" 159 mkdir -p "$out" 160 infer_cargo "$out/cargo-home" build --manifest-path "$repo/$INFER_MANIFEST_REL" --offline --locked --release \ 161 --target aarch64-unknown-none --target-dir "$out/target" >"$out/build.log" 2>&1 \ 162 || refuse "inference library build failed (see $out/build.log)" 163 echo "$out/target/aarch64-unknown-none/release/libaienos_infer_kernel.a" 164 } 165 ck_env() { # CK_* in the environment, or passed down from an outer make in MAKEFLAGS 166 { env | sed -n 's/^\(CK_[A-Za-z0-9_]*\)=.*/\1/p'; echo "${MAKEFLAGS:-}" | tr ' ' '\n' | sed -n 's/^\(CK_[A-Za-z0-9_]*\)=.*/\1/p'; } | sort -u | tr '\n' ' ' | sed 's/ $//' 167 } 168 169 # ---- build --------------------------------------------------------------------- 170 build_once() { 171 # $1 = OUT dir, $2 = variant, $3 = owner pubkeys, $4 = machine id 172 local out="$1" variant="$2" own="$3" mid="$4" infer="${5:-}" 173 case "$variant" in 174 core) make -s -C "$kdir" OUT="$out" >"$out.log" 2>&1 || refuse "build failed (see $out.log)"; echo "$out/BOOTAA64.EFI";; 175 full) make -s -C "$kdir" OUT="$out" full >"$out.log" 2>&1 || refuse "build failed (see $out.log)"; echo "$out/full/BOOTAA64.EFI";; 176 hardware-staging) 177 make -s -C "$kdir" OUT="$out" full CK_HARDWARE_STAGING=1 CK_OWNER_PUBKEYS="$own" CK_MACHINE_ID="$mid" >"$out.log" 2>&1 || refuse "build failed (see $out.log)" 178 echo "$out/full-hardware-staging/BOOTAA64.EFI";; 179 service-hardware-staging) 180 make -s -C "$kdir" OUT="$out" full CK_SERVICE=1 CK_HARDWARE_STAGING=1 CK_OWNER_PUBKEYS="$own" CK_MACHINE_ID="$mid" \ 181 CK_INFER_LIB="$infer" >"$out.log" 2>&1 || refuse "build failed (see $out.log)" 182 echo "$out/full-service-hardware-staging/BOOTAA64.EFI";; 183 *) refuse "unknown variant $variant";; 184 esac 185 } 186 187 # P11 cut 3: the slot loader that boots the package's slots (docs/NATIVE_PACKAGE.md, 188 # "Slot loader in the package"). hardware-staging: the owner public keys file is the 189 # loader's trusted-key table (ck_slot_tool keys-c refuses TEST keys in it); core and 190 # full: the labelled RFC 8032 TEST key loader (never for hardware). 191 loader_keys_of() { if owner_variant "$1"; then echo owner; else echo test; fi; } 192 build_loader_once() { 193 # $1 = OUT dir, $2 = variant, $3 = owner pubkeys, $4 = machine id 194 local out="$1" variant="$2" own="$3" mid="$4" 195 if owner_variant "$variant"; then 196 make -s -C "$kdir" OUT="$out" slot-loader CK_HARDWARE_STAGING=1 CK_OWNER_PUBKEYS="$own" CK_MACHINE_ID="$mid" \ 197 CK_SLOT_KEYS="$own" >"$out.loader.log" 2>&1 || refuse "slot loader build failed (see $out.loader.log)" 198 else 199 make -s -C "$kdir" OUT="$out" slot-loader CK_TEST_SLOT_KEY=1 >"$out.loader.log" 2>&1 \ 200 || refuse "slot loader build failed (see $out.loader.log)" 201 fi 202 echo "$out/SLOTLDR.EFI" 203 } 204 205 206 write_package_txt() { 207 # $1 = package dir, then named globals of cmd_build 208 local dir="$1" own_sha="" mid_sha="" 209 if [ -n "$own" ]; then own_sha=$(sha256_of "$own"); mid_sha=$(sha256_of "$mid"); fi 210 { 211 echo "format $FORMAT" 212 # source revision 213 echo "repo aien-dev/aienux" 214 echo "commit $commit" 215 echo "commit_time $commit_time" 216 echo "tree_clean $tree_clean" 217 # what was asked for 218 echo "variant $variant" 219 echo "generation $generation" 220 echo "policy_generation $policy_generation" 221 echo "min_rollback_generation $min_rollback" 222 # toolchain (the one make used: CROSS-prefixed tools) 223 toolchain_lines "$cross" 224 # inputs that select the image 225 echo "input_make_goal $(make_goal_of "$variant")" 226 echo "input_make_vars $(make_vars_of "$variant" "$own_sha" "$mid_sha" "$infer_sha")" 227 echo "input_env_ck none" 228 echo "input_makefile_sha256 $(sha256_of "$repo/$MAKEFILE_REL")" 229 echo "input_image_ld_sha256 $(sha256_of "$repo/$IMAGE_LD_REL")" 230 if [ "$variant" = service-hardware-staging ]; then 231 printf '%s\n' "$infer_toolchain" 232 echo "input_infer_manifest_sha256 $(sha256_of "$repo/$INFER_MANIFEST_REL")" 233 echo "input_infer_lock_sha256 $(sha256_of "$repo/$INFER_LOCK_REL")" 234 echo "input_infer_build offline_locked_release_aarch64-unknown-none" 235 echo "infer_library $INFER_ARCHIVE" 236 echo "infer_library_bytes $(size_of "$dir/$INFER_ARCHIVE")" 237 echo "infer_library_sha256 $infer_sha" 238 echo "infer_reproducible two_clean_builds_identical" 239 fi 240 if [ -n "$pins" ]; then 241 while read -r r s; do 242 [ -z "$r" ] && continue 243 case "$r" in \#*) continue;; esac 244 echo "pin $r $s" 245 done <"$pins" 246 fi 247 if [ -n "$model" ]; then 248 echo "model_bytes $(size_of "$model")" 249 echo "model_sha256 $(sha256_of "$model")" 250 fi 251 for f in "${firmware[@]}"; do 252 echo "firmware $(basename "$f") $(size_of "$f") $(sha256_of "$f")" 253 done 254 if [ -n "$own" ]; then 255 echo "owner_pubkeys_sha256 $own_sha" 256 echo "machine_id_sha256 $mid_sha" 257 fi 258 # resulting image 259 echo "image image/BOOTAA64.EFI" 260 echo "image_bytes $(size_of "$dir/image/BOOTAA64.EFI")" 261 echo "image_sha256 $(sha256_of "$dir/image/BOOTAA64.EFI")" 262 echo "image_sha512 $(sha512_of "$dir/image/BOOTAA64.EFI")" 263 echo "reproducible two_clean_builds_identical" 264 # the slot loader (P11 cut 3) 265 echo "loader loader/SLOTLDR.EFI" 266 echo "loader_keys $(loader_keys_of "$variant")" 267 echo "loader_bytes $(size_of "$dir/loader/SLOTLDR.EFI")" 268 echo "loader_sha256 $(sha256_of "$dir/loader/SLOTLDR.EFI")" 269 echo "loader_reproducible two_clean_builds_identical" 270 } >"$dir/PACKAGE.txt" 271 } 272 273 write_tbs() { 274 # $1 = package dir, $2 = pubkey hex, $3 = tool 275 hex64 "$2" || refuse "--pubkey must be 64 hex characters" 276 local tout 277 tout=$("$3" tbs "$1/slot/MANIFEST.bin" --pubkey "$2" --out "$1/slot/MANIFEST.tbs" 2>&1) || { 278 echo "$tout" | /usr/bin/grep '^refused:' || true 279 refuse "to-be-signed bytes not written"; } 280 } 281 282 cmd_build() { 283 local out="" generation="" variant="full" own="" mid="" policy_generation=0 min_rollback=0 284 local pins="" model="" allow_dirty=0 pubkey="" 285 local -a firmware=() 286 while [ $# -gt 0 ]; do 287 case "$1" in 288 --out) out="$2"; shift 2;; 289 --generation) generation="$2"; shift 2;; 290 --variant) variant="$2"; shift 2;; 291 --owner-pubkeys) own="$2"; shift 2;; 292 --machine-id) mid="$2"; shift 2;; 293 --policy-generation) policy_generation="$2"; shift 2;; 294 --min-rollback) min_rollback="$2"; shift 2;; 295 --pins) pins="$2"; shift 2;; 296 --model) model="$2"; shift 2;; 297 --firmware) firmware+=("$2"); shift 2;; 298 --pubkey) pubkey="$2"; shift 2;; 299 --allow-dirty) allow_dirty=1; shift;; 300 *) refuse "build: unknown argument $1";; 301 esac 302 done 303 [ -n "$out" ] || refuse "build needs --out DIR" 304 [[ "$generation" =~ ^[1-9][0-9]*$ ]] || refuse "build needs --generation N (positive integer)" 305 [[ "$policy_generation" =~ ^[0-9]+$ ]] || refuse "--policy-generation must be an integer" 306 [[ "$min_rollback" =~ ^[0-9]+$ ]] || refuse "--min-rollback must be an integer" 307 [ -z "$pubkey" ] || hex64 "$pubkey" || refuse "--pubkey must be 64 hex characters" 308 [ -e "$out" ] && refuse "--out $out already exists (packages are never overwritten)" 309 case "$variant" in core|full|hardware-staging|service-hardware-staging) ;; *) refuse "unknown variant $variant";; esac 310 if owner_variant "$variant"; then 311 [ -n "$own" ] && [ -n "$mid" ] || refuse "hardware-staging needs --owner-pubkeys and --machine-id" 312 [ -f "$own" ] && [ -f "$mid" ] || refuse "owner pubkeys or machine id file missing" 313 own=$(realpath "$own"); mid=$(realpath "$mid") 314 else 315 [ -z "$own" ] && [ -z "$mid" ] || refuse "--owner-pubkeys/--machine-id only with --variant hardware-staging or service-hardware-staging" 316 fi 317 [ -z "$pins" ] || [ -f "$pins" ] || refuse "pins file missing" 318 if [ -n "$pins" ]; then 319 while read -r r s; do 320 [ -z "$r" ] && continue 321 case "$r" in \#*) continue;; esac 322 hex40 "$s" || refuse "pins file: $r is not followed by 40 hex characters" 323 done <"$pins" 324 fi 325 [ -z "$model" ] || [ -f "$model" ] || refuse "model file missing" 326 [ "$variant" != service-hardware-staging ] || [ -n "$model" ] || refuse "service-hardware-staging needs --model (boot-disk identity, not embedded model bytes)" 327 [ "$variant" != service-hardware-staging ] || [ -s "$model" ] || refuse "service model file is empty" 328 if [ "$variant" = service-hardware-staging ]; then 329 # Cargo auto-discovers build.rs and Rust modules, including ignored files. 330 # A tracked-only dirty check cannot attest those extra build inputs. 331 [ -z "$(git -C "$repo" ls-files --others -- crates/aienos-infer crates/aienos-infer-kernel)" ] \ 332 || refuse "untracked inference build inputs (including ignored files)" 333 fi 334 for f in "${firmware[@]}"; do [ -f "$f" ] || refuse "firmware file missing: $f"; done 335 local envck 336 envck=$(ck_env) 337 [ -z "$envck" ] || refuse "CK_* variables in the environment change the image without being recorded: $envck" 338 339 local dirty tree_clean=yes 340 dirty=$(git -C "$repo" status --porcelain --untracked-files=no) 341 if [ -n "$dirty" ]; then 342 [ "$allow_dirty" = 1 ] || refuse "working tree is dirty; a package is built from a committed tree only (or --allow-dirty for tests)" 343 tree_clean=no 344 fi 345 local commit commit_time cross 346 commit=$(git -C "$repo" rev-parse HEAD) 347 commit_time=$(git -C "$repo" log -1 --format=%ct) 348 hex40 "$commit" || refuse "cannot read the commit" 349 cross=$(cross_prefix) 350 351 local scratch 352 scratch=$(mktemp -d "${TMPDIR:-/tmp}/native-package.XXXXXX") 353 local img_a img_b infer_a="" infer_b="" infer_sha="" infer_toolchain="" 354 if [ "$variant" = service-hardware-staging ]; then 355 infer_toolchain=$(infer_toolchain_lines "$scratch/cargo-version") 356 infer_a=$(build_infer_once "$scratch/infer-a") 357 infer_b=$(build_infer_once "$scratch/infer-b") 358 if [ -n "${NATIVE_PACKAGE_TEST_CORRUPT_INFER_SECOND:-}" ]; then printf '\0' >>"$infer_b"; fi 359 infer_sha=$(sha256_of "$infer_a") 360 [ "$infer_sha" = "$(sha256_of "$infer_b")" ] || refuse "two clean inference library builds differ: not reproducible, not signable" 361 fi 362 img_a=$(build_once "$scratch/a" "$variant" "$own" "$mid" "$infer_a") 363 img_b=$(build_once "$scratch/b" "$variant" "$own" "$mid" "$infer_b") 364 if [ -n "${NATIVE_PACKAGE_TEST_CORRUPT_SECOND:-}" ]; then 365 printf '\0' >>"$img_b" # test hook: make the second build differ 366 fi 367 local da db 368 da=$(sha256_of "$img_a"); db=$(sha256_of "$img_b") 369 if [ "$da" != "$db" ]; then 370 rm -rf "$scratch" 371 refuse "two clean builds differ ($da vs $db): not reproducible, not signable" 372 fi 373 374 local ldr_a ldr_b 375 ldr_a=$(build_loader_once "$scratch/a" "$variant" "$own" "$mid") 376 ldr_b=$(build_loader_once "$scratch/b" "$variant" "$own" "$mid") 377 if [ "$(sha256_of "$ldr_a")" != "$(sha256_of "$ldr_b")" ]; then 378 rm -rf "$scratch" 379 refuse "two clean slot loader builds differ: not reproducible, not signable" 380 fi 381 382 mkdir -p "$out/image" "$out/slot" "$out/loader" 383 cp "$img_a" "$out/image/BOOTAA64.EFI" 384 chmod 444 "$out/image/BOOTAA64.EFI" 385 cp "$ldr_a" "$out/loader/SLOTLDR.EFI" 386 chmod 444 "$out/loader/SLOTLDR.EFI" 387 if [ "$variant" = service-hardware-staging ]; then 388 mkdir -p "$out/inputs" 389 cp "$infer_a" "$out/$INFER_ARCHIVE" 390 chmod 444 "$out/$INFER_ARCHIVE" 391 fi 392 rm -rf "$scratch" 393 write_package_txt "$out" 394 (cd "$out" && sha256sum image/BOOTAA64.EFI loader/SLOTLDR.EFI PACKAGE.txt >SHA256SUMS) 395 if [ "$variant" = service-hardware-staging ]; then 396 (cd "$out" && sha256sum "$INFER_ARCHIVE" >>SHA256SUMS) 397 fi 398 local prov tool 399 prov=$(sha256_of "$out/SHA256SUMS") 400 echo "package_sha256 $prov" >"$out/PROVENANCE.txt" 401 tool=$(slot_tool) 402 "$tool" make-manifest --image "$out/image/BOOTAA64.EFI" --generation "$generation" \ 403 --policy-generation "$policy_generation" --min-rollback "$min_rollback" \ 404 --commit "$commit" --provenance "$prov" --timestamp "$commit_time" \ 405 --out "$out/slot/MANIFEST.bin" >/dev/null || refuse "slot manifest not built" 406 [ -z "$pubkey" ] || write_tbs "$out" "$pubkey" "$tool" 407 echo "package built: $out" 408 echo " commit $commit (tree_clean $tree_clean) variant $variant generation $generation" 409 echo " image sha256 $da ($(size_of "$out/image/BOOTAA64.EFI") bytes), two clean builds identical" 410 echo " package_sha256 (sha256 of SHA256SUMS, the slot provenance) $prov" 411 if [ -z "$pubkey" ]; then 412 echo " next: $0 tbs --package $out --pubkey " 413 fi 414 echo " sign offline: openssl pkeyutl -sign -inkey owner_root.key.pem -rawin -in $out/slot/MANIFEST.tbs -out MANIFEST.sig" 415 echo " then: $0 sign --package $out --sig MANIFEST.sig --pubkey <64 hex>" 416 } 417 418 # ---- package integrity (shared by tbs, sign and verify) ----------------------------- 419 rec() { awk -v k="$2" '$1==k{sub(/^[^ ]+ /,""); print}' "$1/PACKAGE.txt"; } 420 421 check_package_files() { 422 local dir="$1" 423 [ -d "$dir" ] || refuse "package directory missing" 424 for f in image/BOOTAA64.EFI loader/SLOTLDR.EFI PACKAGE.txt SHA256SUMS PROVENANCE.txt slot/MANIFEST.bin; do 425 [ -f "$dir/$f" ] || refuse "package is missing $f" 426 done 427 (cd "$dir" && sha256sum -c --quiet --strict SHA256SUMS >/dev/null 2>&1) || refuse "SHA256SUMS does not match the package files" 428 for f in image/BOOTAA64.EFI loader/SLOTLDR.EFI PACKAGE.txt; do 429 [ -n "$(awk -v f="$f" '$2==f' "$dir/SHA256SUMS")" ] || refuse "SHA256SUMS does not cover $f" 430 done 431 if [ "$(rec "$dir" variant)" = service-hardware-staging ]; then 432 [ -f "$dir/$INFER_ARCHIVE" ] || refuse "package is missing $INFER_ARCHIVE" 433 [ "$(awk -v f="$INFER_ARCHIVE" '$2==f{n++}END{print n+0}' "$dir/SHA256SUMS")" = 1 ] || refuse "SHA256SUMS must cover $INFER_ARCHIVE exactly once" 434 fi 435 local fmt 436 fmt=$(rec "$dir" format) 437 [ "$fmt" = "$FORMAT" ] || refuse "PACKAGE.txt format $fmt is not $FORMAT" 438 local want have 439 want=$(awk '$1=="package_sha256"{print $2}' "$dir/PROVENANCE.txt") 440 have=$(sha256_of "$dir/SHA256SUMS") 441 [ "$want" = "$have" ] || refuse "PROVENANCE.txt package_sha256 is not the SHA-256 of SHA256SUMS" 442 } 443 444 # check_record DIR SAME_HOST ALLOW_DIRTY: every PACKAGE.txt line that can be recomputed 445 # here must match; the rest are printed as recorded. Prints " checked|recorded key value". 446 check_record() { 447 local dir="$1" same_host="$2" allow_dirty="$3" k 448 local -a required=(format repo commit commit_time tree_clean variant generation policy_generation 449 min_rollback_generation toolchain_cross toolchain_cc toolchain_ld toolchain_objcopy toolchain_make 450 build_host_arch input_make_goal input_make_vars input_env_ck input_makefile_sha256 451 input_image_ld_sha256 image image_bytes image_sha256 image_sha512 reproducible 452 loader loader_keys loader_bytes loader_sha256 loader_reproducible) 453 for k in "${required[@]}"; do 454 [ "$(awk -v k="$k" '$1==k' "$dir/PACKAGE.txt" | wc -l)" = 1 ] || refuse "PACKAGE.txt must hold exactly one $k line" 455 done 456 local img="$dir/image/BOOTAA64.EFI" 457 [ "$(rec "$dir" image)" = image/BOOTAA64.EFI ] || refuse "PACKAGE.txt image is not image/BOOTAA64.EFI" 458 [ "$(rec "$dir" image_bytes)" = "$(size_of "$img")" ] || refuse "PACKAGE.txt image_bytes does not match the image" 459 [ "$(rec "$dir" image_sha256)" = "$(sha256_of "$img")" ] || refuse "PACKAGE.txt image_sha256 does not match the image" 460 [ "$(rec "$dir" image_sha512)" = "$(sha512_of "$img")" ] || refuse "PACKAGE.txt image_sha512 does not match the image" 461 [ "$(rec "$dir" reproducible)" = two_clean_builds_identical ] || refuse "PACKAGE.txt does not record a reproducible build" 462 # The slot loader (P11 cut 3): bytes, digest, reproducibility, and a key source that 463 # matches the variant (TEST-key loaders carry their "TEST-ONLY key" note, owner ones none). 464 local ldr="$dir/loader/SLOTLDR.EFI" 465 [ "$(rec "$dir" loader)" = loader/SLOTLDR.EFI ] || refuse "PACKAGE.txt loader is not loader/SLOTLDR.EFI" 466 [ "$(rec "$dir" loader_bytes)" = "$(size_of "$ldr")" ] || refuse "PACKAGE.txt loader_bytes does not match the loader" 467 [ "$(rec "$dir" loader_sha256)" = "$(sha256_of "$ldr")" ] || refuse "PACKAGE.txt loader_sha256 does not match the loader" 468 [ "$(rec "$dir" loader_reproducible)" = two_clean_builds_identical ] || refuse "PACKAGE.txt does not record a reproducible loader build" 469 [ "$(rec "$dir" loader_keys)" = "$(loader_keys_of "$(rec "$dir" variant)")" ] || refuse "PACKAGE.txt loader_keys does not match the variant" 470 if [ "$(rec "$dir" loader_keys)" = test ]; then 471 LC_ALL=C /usr/bin/grep -aqF "TEST-ONLY key" "$ldr" || refuse "TEST-key loader lacks its TEST-ONLY note" 472 elif LC_ALL=C /usr/bin/grep -aqF "TEST-ONLY" "$ldr"; then 473 refuse "owner-key loader carries a TEST-ONLY note" 474 fi 475 local tc; tc=$(rec "$dir" tree_clean) 476 case "$tc" in 477 yes) ;; 478 no) [ "$allow_dirty" = 1 ] || refuse "package was built from a dirty tree (tree_clean no): not a release candidate";; 479 *) refuse "PACKAGE.txt tree_clean is neither yes nor no";; 480 esac 481 [ "$(rec "$dir" input_env_ck)" = none ] || refuse "PACKAGE.txt input_env_ck is not none" 482 local variant own_sha mid_sha 483 variant=$(rec "$dir" variant) 484 case "$variant" in core|full|hardware-staging|service-hardware-staging) ;; *) refuse "PACKAGE.txt variant $variant is unknown";; esac 485 own_sha=$(rec "$dir" owner_pubkeys_sha256); mid_sha=$(rec "$dir" machine_id_sha256) 486 if owner_variant "$variant"; then 487 hex64 "$own_sha" && hex64 "$mid_sha" || refuse "hardware-staging PACKAGE.txt lacks the owner file digests" 488 else 489 [ -z "$own_sha$mid_sha" ] || refuse "PACKAGE.txt has owner file digests on a $variant package" 490 fi 491 [ "$(rec "$dir" input_make_goal)" = "$(make_goal_of "$variant")" ] || refuse "PACKAGE.txt input_make_goal does not match variant $variant" 492 local infer_sha="" 493 if [ "$variant" = service-hardware-staging ]; then 494 for k in toolchain_cargo toolchain_rustc input_infer_manifest_sha256 input_infer_lock_sha256 input_infer_build infer_library infer_library_bytes infer_library_sha256 infer_reproducible model_bytes model_sha256 owner_pubkeys_sha256 machine_id_sha256; do 495 [ "$(awk -v k="$k" '$1==k' "$dir/PACKAGE.txt" | wc -l)" = 1 ] || refuse "PACKAGE.txt must hold exactly one $k line" 496 done 497 infer_sha=$(rec "$dir" infer_library_sha256) 498 [ "$(rec "$dir" infer_library)" = "$INFER_ARCHIVE" ] || refuse "PACKAGE.txt infer_library is not $INFER_ARCHIVE" 499 [ "$infer_sha" = "$(sha256_of "$dir/$INFER_ARCHIVE")" ] || refuse "PACKAGE.txt infer_library_sha256 does not match the library" 500 [ "$(rec "$dir" infer_library_bytes)" = "$(size_of "$dir/$INFER_ARCHIVE")" ] || refuse "PACKAGE.txt infer_library_bytes does not match the library" 501 [ "$(rec "$dir" infer_reproducible)" = two_clean_builds_identical ] || refuse "PACKAGE.txt does not record a reproducible inference build" 502 [ "$(rec "$dir" input_infer_build)" = offline_locked_release_aarch64-unknown-none ] || refuse "PACKAGE.txt input_infer_build differs" 503 hex64 "$(rec "$dir" input_infer_manifest_sha256)" && hex64 "$(rec "$dir" input_infer_lock_sha256)" || refuse "service inference source digest is invalid" 504 [[ "$(rec "$dir" toolchain_cargo)" = cargo\ * ]] && [[ "$(rec "$dir" toolchain_rustc)" = rustc\ * ]] || refuse "service Rust toolchain record is invalid" 505 hex64 "$(rec "$dir" model_sha256)" || refuse "service model digest is invalid" 506 [[ "$(rec "$dir" model_bytes)" =~ ^[1-9][0-9]*$ ]] || refuse "service model size is invalid" 507 if [ "$same_host" = 1 ]; then 508 local ch; ch=$(mktemp -d) 509 local line got 510 got=$(infer_toolchain_lines "$ch") || { rm -rf "$ch"; refuse "could not resolve this host's Rust toolchain"; } 511 while read -r line; do 512 k=${line%% *} 513 [ "$(rec "$dir" "$k")" = "${line#* }" ] || { rm -rf "$ch"; refuse "$k differs from this host"; } 514 done <<<"$got" 515 rm -rf "$ch" 516 fi 517 for k in input_infer_build infer_library infer_library_bytes infer_library_sha256 infer_reproducible; do 518 echo " checked $k $(rec "$dir" "$k")" 519 done 520 for k in toolchain_cargo toolchain_rustc; do 521 echo " $([ "$same_host" = 1 ] && echo checked || echo recorded) $k $(rec "$dir" "$k")" 522 done 523 elif awk '$1~/^(infer_|input_infer_|toolchain_cargo$|toolchain_rustc$)/{found=1}END{exit !found}' "$dir/PACKAGE.txt"; then 524 refuse "PACKAGE.txt has inference fields on a $variant package" 525 fi 526 [ "$(rec "$dir" input_make_vars)" = "$(make_vars_of "$variant" "$own_sha" "$mid_sha" "$infer_sha")" ] || refuse "PACKAGE.txt input_make_vars does not match variant $variant" 527 echo "record:" 528 for k in image image_bytes image_sha256 image_sha512 reproducible loader loader_keys loader_bytes loader_sha256 loader_reproducible tree_clean input_env_ck variant input_make_goal input_make_vars; do 529 echo " checked $k $(rec "$dir" "$k")" 530 done 531 532 # Source revision and source inputs: recomputed from the commit when this repository 533 # has it (clean package) or from the working tree (dirty package, tests only). 534 local commit; commit=$(rec "$dir" commit) 535 hex40 "$commit" || refuse "PACKAGE.txt commit is not 40 hex characters" 536 if git -C "$repo" cat-file -e "$commit^{commit}" 2>/dev/null; then 537 [ "$(rec "$dir" commit_time)" = "$(git -C "$repo" log -1 --format=%ct "$commit")" ] || refuse "PACKAGE.txt commit_time does not match commit $commit" 538 echo " checked commit $commit" 539 echo " checked commit_time $(rec "$dir" commit_time)" 540 local f key have 541 local -a source_inputs=("$MAKEFILE_REL:input_makefile_sha256" "$IMAGE_LD_REL:input_image_ld_sha256") 542 if [ "$variant" = service-hardware-staging ]; then 543 source_inputs+=("$INFER_MANIFEST_REL:input_infer_manifest_sha256" "$INFER_LOCK_REL:input_infer_lock_sha256") 544 fi 545 for f in "${source_inputs[@]}"; do 546 key=${f#*:}; f=${f%%:*} 547 if [ "$tc" = yes ]; then 548 have=$(git -C "$repo" show "$commit:$f" | sha256sum | cut -d' ' -f1) 549 else 550 have=$(sha256_of "$repo/$f") 551 fi 552 [ "$(rec "$dir" "$key")" = "$have" ] || refuse "PACKAGE.txt $key does not match $f at $([ "$tc" = yes ] && echo "commit $commit" || echo "the working tree")" 553 echo " checked $key $have" 554 done 555 else 556 for k in commit commit_time input_makefile_sha256 input_image_ld_sha256; do 557 echo " recorded $k $(rec "$dir" "$k") (commit not in this repository)" 558 done 559 fi 560 561 # Toolchain: compared with the running toolchain only when asked (--same-host). 562 if [ "$same_host" = 1 ]; then 563 local c line 564 c=$(cross_value "$(rec "$dir" toolchain_cross)") 565 [ "$(cross_name "$(cross_prefix)")" = "$(rec "$dir" toolchain_cross)" ] || refuse "toolchain_cross differs from this host's ($(cross_name "$(cross_prefix)"))" 566 while read -r line; do 567 k=${line%% *} 568 [ "$(rec "$dir" "$k")" = "${line#* }" ] || refuse "$k differs from this host (recorded '$(rec "$dir" "$k")', here '${line#* }')" 569 echo " checked $line" 570 done < <(toolchain_lines "$c") 571 else 572 for k in toolchain_cross toolchain_cc toolchain_ld toolchain_objcopy toolchain_make build_host_arch; do 573 echo " recorded $k $(rec "$dir" "$k")" 574 done 575 fi 576 # Not recomputable from the package alone: generations are checked through the 577 # manifest; pins, model and firmware are the digests of files the package does not carry. 578 awk '$1=="generation"||$1=="policy_generation"||$1=="min_rollback_generation"||$1=="pin"||$1=="model_bytes"||$1=="model_sha256"||$1=="firmware"||$1=="owner_pubkeys_sha256"||$1=="machine_id_sha256"{print " recorded " $0}' "$dir/PACKAGE.txt" 579 echo " checked package_sha256 $(sha256_of "$dir/SHA256SUMS")" 580 } 581 582 manifest_provenance_check() { 583 # $1 = dir, $2 = manifest file. The manifest's provenance must be package_sha256 584 # and its commit must be PACKAGE.txt's commit. Read via the tool's print lines. 585 local dir="$1" mf="$2" tool="$3" 586 local line prov commit want_prov want_commit 587 line=$("$tool" show "$mf" 2>/dev/null | /usr/bin/grep -m1 '^ commit=' || true) 588 commit=$(echo "$line" | sed -n 's/^ commit=\([0-9a-f]\{40\}\) provenance=.*/\1/p') 589 prov=$(echo "$line" | sed -n 's/^ commit=[0-9a-f]\{40\} provenance=\([0-9a-f]\{64\}\).*/\1/p') 590 want_prov=$(sha256_of "$dir/SHA256SUMS") 591 want_commit=$(rec "$dir" commit) 592 [ "$prov" = "$want_prov" ] || refuse "manifest provenance does not bind this package's SHA256SUMS" 593 [ "$commit" = "$want_commit" ] || refuse "manifest commit does not match PACKAGE.txt" 594 } 595 596 cmd_tbs() { 597 local dir="" pk="" 598 while [ $# -gt 0 ]; do 599 case "$1" in 600 --package) dir="$2"; shift 2;; 601 --pubkey) pk="$2"; shift 2;; 602 *) refuse "tbs: unknown argument $1";; 603 esac 604 done 605 [ -n "$dir" ] && [ -n "$pk" ] || refuse "tbs needs --package DIR and --pubkey HEX64" 606 check_package_files "$dir" 607 local tool 608 tool=$(slot_tool) 609 manifest_provenance_check "$dir" "$dir/slot/MANIFEST.bin" "$tool" 610 write_tbs "$dir" "$pk" "$tool" 611 echo "to-be-signed bytes for signer $pk: $dir/slot/MANIFEST.tbs" 612 } 613 614 cmd_sign() { 615 local dir="" sig="" pk="" test_key=0 allow_dirty=0 616 while [ $# -gt 0 ]; do 617 case "$1" in 618 --package) dir="$2"; shift 2;; 619 --sig) sig="$2"; shift 2;; 620 --pubkey) pk="$2"; shift 2;; 621 --test) test_key=1; shift;; 622 --allow-dirty) allow_dirty=1; shift;; 623 *) refuse "sign: unknown argument $1";; 624 esac 625 done 626 [ -n "$dir" ] || refuse "sign needs --package DIR" 627 check_package_files "$dir" 628 # A real signature never goes on a dirty-tree package (--allow-dirty: tests only); 629 # TEST signing may. 630 local rout 631 [ "$test_key" = 1 ] && allow_dirty=1 632 rout=$(check_record "$dir" 0 "$allow_dirty") || { echo "$rout" | /usr/bin/grep '^refused:'; exit 1; } 633 local tool 634 tool=$(slot_tool) 635 manifest_provenance_check "$dir" "$dir/slot/MANIFEST.bin" "$tool" 636 [ -e "$dir/slot/MANIFEST.signed.bin" ] && refuse "package is already signed (never re-signed in place)" 637 if [ "$test_key" = 1 ]; then 638 "$tool" sign-test "$dir/slot/MANIFEST.bin" --out "$dir/slot/MANIFEST.signed.bin" | tail -1 639 echo "TEST-SIGNED package (never for hardware): $dir" 640 return 0 641 fi 642 [ -n "$sig" ] && [ -n "$pk" ] || refuse "sign needs --sig FILE and --pubkey HEX64" 643 hex64 "$pk" || refuse "--pubkey must be 64 hex characters" 644 local aout 645 if ! aout=$("$tool" attach-sig "$dir/slot/MANIFEST.bin" --sig "$sig" --pubkey "$pk" --out "$dir/slot/MANIFEST.signed.bin" 2>&1); then 646 echo "$aout" | /usr/bin/grep '^refused:' || true 647 refuse "signature not attached" 648 fi 649 echo "$aout" | tail -1 650 echo "signed package: $dir" 651 } 652 653 cmd_verify() { 654 local dir="" keys="" test_key=0 allow_unsigned=0 same_host=0 allow_dirty=0 655 while [ $# -gt 0 ]; do 656 case "$1" in 657 --package) dir="$2"; shift 2;; 658 --keys) keys="$2"; shift 2;; 659 --test-key) test_key=1; shift;; 660 --allow-unsigned) allow_unsigned=1; shift;; 661 --same-host) same_host=1; shift;; 662 --allow-dirty) allow_dirty=1; shift;; 663 *) refuse "verify: unknown argument $1";; 664 esac 665 done 666 [ -n "$dir" ] || refuse "verify needs --package DIR" 667 [ -n "$keys" ] || [ "$test_key" = 1 ] || refuse "verify needs --keys FILE or --test-key" 668 check_package_files "$dir" 669 local record 670 record=$(check_record "$dir" "$same_host" "$allow_dirty") || { echo "$record" | /usr/bin/grep '^refused:'; exit 1; } 671 local tool 672 tool=$(slot_tool) 673 local mf="$dir/slot/MANIFEST.signed.bin" 674 if [ ! -f "$mf" ]; then 675 [ "$allow_unsigned" = 1 ] || refuse "package is UNSIGNED (slot/MANIFEST.signed.bin missing)" 676 manifest_provenance_check "$dir" "$dir/slot/MANIFEST.bin" "$tool" 677 echo "VERIFY: UNSIGNED package is internally consistent (not installable)" 678 echo "$record" 679 return 0 680 fi 681 manifest_provenance_check "$dir" "$mf" "$tool" 682 local -a karg 683 if [ "$test_key" = 1 ]; then karg=(--test-key); else karg=(--keys "$keys"); fi 684 local vout 685 if ! vout=$("$tool" verify "$mf" --image "$dir/image/BOOTAA64.EFI" "${karg[@]}" 2>&1); then 686 echo "$vout" | /usr/bin/grep '^refused:' || true 687 refuse "slot manifest signature or digest check failed" 688 fi 689 # The unsigned manifest must equal the signed one in every field but signer/signature. 690 cmp -s <(head -c 184 "$dir/slot/MANIFEST.bin") <(head -c 184 "$mf") || refuse "signed manifest differs from the built manifest" 691 echo "VERIFY: PASS $dir" 692 echo "$vout" | /usr/bin/grep -E '^(verify:|manifest:|keys:)' 693 echo "$record" 694 } 695 696 case "${1:-}" in 697 build) shift; cmd_build "$@";; 698 tbs) shift; cmd_tbs "$@";; 699 sign) shift; cmd_sign "$@";; 700 sign-test) shift; cmd_sign --test "$@";; 701 verify) shift; cmd_verify "$@";; 702 *) refuse "usage: native_package.sh build|tbs|sign|sign-test|verify ... (see the header)";; 703 esac