#!/bin/bash # Offline, unprivileged bootstrap verification. Requires GNU coreutils, OpenSSL 3, # jq and Info-ZIP. Does not download, install a service, or execute the candidate. set -euo pipefail export PATH=/usr/bin:/bin LC_ALL=C umask 077 fail() { printf 'Release verification failed: %s\n' "$1" >&2; exit 1; } [[ $# == 5 || $# == 6 ]] || fail 'Usage: verify_linux_installer.sh RELEASE_DIRECTORY TRUSTED_PUBLIC_PEM EXPECTED_VERSION MINIMUM_SEQUENCE NEW_OUTPUT_DIRECTORY [installer|agent]' artifact=${6:-installer} case "$artifact" in installer) schema=sectigo.installer-release.v1; envelope=sectigo.installer-signature.v1; versionfield=installerVersion; executable=quantum-ready-service; prefix=sectigo-quantum-ready-installer;; agent) schema=sectigo.binary-release.v1; envelope=sectigo.binary-signature.v1; versionfield=agentVersion; executable=quantum-ready; prefix=sectigo-quantum-ready;; *) fail 'Select installer or agent explicitly';; esac [[ $(id -u) != 0 ]] || fail 'Run verification as a regular user; installation is a separate administrator action.' for tool in openssl jq unzip zipinfo sha256sum stat date realpath head tail od; do command -v "$tool" >/dev/null || fail "Required tool unavailable: $tool" done [[ $(uname -s) == Linux ]] || fail 'Linux is required' case $(uname -m) in x86_64) target=linux-x86_64; machine=3e00;; aarch64) target=linux-arm64; machine=b700;; *) fail 'Unsupported architecture';; esac release=$1; trusted=$2; version=$3; minimum=$4; output=$5 [[ $version =~ ^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)$ ]] || fail 'Select an explicit installer version' [[ $minimum =~ ^[1-9][0-9]{0,15}$ ]] || fail 'Select an explicit trusted minimum sequence' [[ ! -e $output && ! -L $output ]] || fail 'Output must not already exist' parent=$(realpath -e -- "$(dirname -- "$output")") [[ -d $parent && $(stat -c %u -- "$parent") == "$(id -u)" ]] || fail 'Output parent must be owned by the current user' permissions=$(stat -c %a -- "$parent") (( (8#$permissions & 0022) == 0 )) || fail 'Output parent must not be writable by other users' output="$parent/$(basename -- "$output")" [[ $(basename -- "$output") != . && $(basename -- "$output") != .. ]] || fail 'Invalid output name' work=$(mktemp -d /tmp/sectigo-installer-verify.XXXXXXXX) trap 'rm -rf -- "$work"' EXIT snapshot() { [[ -f $1 && ! -L $1 ]] || fail 'Inputs must be regular files, not symbolic links' head -c "$(( $2 + 1 ))" -- "$1" > "$3" [[ $(stat -c %s -- "$3") -le $2 ]] || fail 'Input exceeds its size limit' } printf 'Checking signed %s release...\n' "$artifact" >&2 snapshot "$release/manifest.json" 2097152 "$work/manifest" snapshot "$release/manifest.sig" 4096 "$work/envelope" snapshot "$trusted" 4096 "$work/key.pem" openssl pkey -pubin -in "$work/key.pem" -pubout -outform DER -out "$work/key.der" 2>/dev/null || fail 'Invalid trusted public key' [[ $(stat -c %s "$work/key.der") == 44 && $(head -c 12 "$work/key.der" | od -An -v -tx1 | tr -d ' \n') == 302a300506032b6570032100 ]] || fail 'Trusted key must be Ed25519' keyid=$(tail -c 32 "$work/key.der" | sha256sum | cut -d ' ' -f1) jq -e --arg key "$keyid" --arg envelope "$envelope" 'type=="object" and (keys==["algorithm","keyId","schema","signature"]) and .schema==$envelope and .algorithm=="Ed25519" and .keyId==$key and (.signature|type=="string" and test("^[A-Za-z0-9+/]{86}==$"))' "$work/envelope" >/dev/null || fail 'Invalid installer signature envelope' jq -r .signature "$work/envelope" | openssl base64 -d -A > "$work/signature" [[ $(stat -c %s "$work/signature") == 64 ]] || fail 'Invalid signature length' { printf '%s\0' "$schema"; cat "$work/manifest"; } > "$work/message" openssl pkeyutl -verify -pubin -inkey "$work/key.pem" -rawin -in "$work/message" -sigfile "$work/signature" >/dev/null 2>&1 || fail 'Signature does not match the independently trusted key' filename="$prefix-$target.zip" jq -e --arg target "$target" --arg version "$version" --arg key "$keyid" --arg filename "$filename" --argjson minimum "$minimum" --arg schema "$schema" --arg versionfield "$versionfield" --arg executable "$executable" ' def integer: type=="number" and floor==.; def digest: type=="string" and test("^[a-f0-9]{64}$"); type=="object" and keys==(["bytes","channel","executable","expandedBytes","expiresAt","filename","files",$versionfield,"issuedAt","schema","sequence","sha256","signed","signingKeyId","target"]|sort) and .schema==$schema and .signed==true and .signingKeyId==$key and .target==$target and .[$versionfield]==$version and .channel=="preview" and .filename==$filename and .executable==$executable and ($minimum<=9007199254740991) and (.sequence|integer and .>=$minimum and .<=9007199254740991) and (.bytes|integer and .>0 and .<=268435456) and (.sha256|digest) and all([.issuedAt,.expiresAt][];type=="string" and test("^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}(\\.[0-9]{1,6})?(Z|\\+00:00)$")) and (.files|type=="object" and length>0 and length<=4096) and (.files|keys|map(ascii_downcase)|length== (unique|length)) and all(.files|to_entries[]; (.key|length<=240 and test("^[A-Za-z0-9_./+ -]+$") and (split("/")|all(.!="" and .!="." and .!=".." and (test("(^ |[ .]$)")|not)))) and (.value|type=="object" and keys==["bytes","mode","sha256"] and (.sha256|digest) and (.bytes|integer and .>=0 and .<=134217728) and (.mode==420 or .mode==493))) and (.files[$executable].mode==493) and (.expandedBytes|integer and .>=0 and .<=536870912) and (.expandedBytes==([.files[].bytes]|add)) ' "$work/manifest" >/dev/null || fail 'Signed installer metadata does not match the selected release or limits' issued=$(date -u -d "$(jq -r .issuedAt "$work/manifest")" +%s) || fail 'Invalid issue time' expires=$(date -u -d "$(jq -r .expiresAt "$work/manifest")" +%s) || fail 'Invalid expiry time' now=$(date -u +%s) (( issued <= now + 300 && expires > now && expires > issued && expires - issued <= 2678400 )) || fail 'Installer is expired or outside its validity period' snapshot "$release/$filename" 268435456 "$work/archive.zip" [[ $(stat -c %s "$work/archive.zip") == $(jq -r .bytes "$work/manifest") ]] || fail 'Archive length mismatch' [[ $(sha256sum "$work/archive.zip" | cut -d ' ' -f1) == $(jq -r .sha256 "$work/manifest") ]] || fail 'Archive digest mismatch' # Check the member list before extracting. All extraction paths come from the # authenticated bounded inventory; never invoke unzip's filesystem extraction. zipinfo -1 "$work/archive.zip" | head -c 2097153 > "$work/members" || fail 'Unreadable archive inventory' [[ $(stat -c %s "$work/members") -le 2097152 ]] || fail 'Archive inventory exceeds limit' sort "$work/members" > "$work/sorted-members" jq -r '.files|keys[]' "$work/manifest" | sort > "$work/expected-members" cmp -s "$work/sorted-members" "$work/expected-members" || fail 'Archive inventory differs from signed files' # Names are already restricted to a character set excluding tabs, newlines and # backslashes. Read the authenticated metadata once; preserve spaces in names. jq -r '.files|to_entries|sort_by(.key)[]|[.key,.value.bytes,.value.sha256,.value.mode]|@tsv' "$work/manifest" > "$work/inventory" count=$(jq '.files|length' "$work/manifest") verified=0 printf 'Signature and archive verified. Checking %s runtime files...\n' "$count" >&2 mkdir "$work/staged" while IFS=$'\t' read -r name bytes digest mode; do mkdir -p -- "$work/staged/$(dirname -- "$name")" unzip -p "$work/archive.zip" "$name" | head -c "$(( bytes + 1 ))" > "$work/staged/$name" || fail 'Archive member could not be read within bounds' [[ $(stat -c %s -- "$work/staged/$name") == "$bytes" ]] || fail 'Member length mismatch' [[ $(sha256sum -- "$work/staged/$name" | cut -d ' ' -f1) == "$digest" ]] || fail 'Member hash mismatch' if [[ $mode == 493 ]]; then chmod 755 -- "$work/staged/$name"; else chmod 644 -- "$work/staged/$name"; fi verified=$((verified + 1)) if (( verified % 100 == 0 || verified == count )); then printf 'Checked %s/%s files.\n' "$verified" "$count" >&2 fi done < "$work/inventory" [[ $verified == "$count" ]] || fail 'Incomplete verified inventory' binary="$work/staged/$executable" [[ $(head -c 6 "$binary" | od -An -v -tx1 | tr -d ' \n') == 7f454c460201 ]] || fail 'Expected a native Linux executable' [[ $(od -An -v -tx1 -j18 -N2 "$binary" | tr -d ' \n') == "$machine" ]] || fail 'Executable architecture mismatch' mv -T --no-clobber -- "$work/staged" "$output" [[ ! -e $work/staged ]] || fail 'Output appeared during verification; nothing was replaced' printf 'Verified %s %s for %s. Files staged at: %s\n' "$artifact" "$version" "$target" "$output" printf 'No installer or service was executed.\n'