#!/usr/bin/env bash set -uo pipefail # CASAN Plan-16 SEC-25 (SC-07, offline) — build-artifact attestation (tested==deployed). # # A green CI gate proves the TESTED artifact is sound, but nothing binds it to what # is DEPLOYED — a different artifact could ship. This produces a signed attestation # over an artifact's content hash; verification recomputes the hash and checks the # signature, so a swapped/modified artifact (deployed != tested) or a forged # attestation is REFUSED (fail-closed). Offline form of SLSA-style provenance; # real signed-commit enrollment + full provenance chain need CI/key infra. # # Usage: # artifact-attest.sh attest # -> .att (+ .att.sig) # artifact-attest.sh verify # tested==deployed check # Exit: 0 ok · 2 mismatch/forged/tampered · 3 missing/unsigned/openssl · 64 usage. CMD="${1:-}"; ART="${2:-}" command -v openssl >/dev/null 2>&1 || { echo "OPENSSL_UNAVAILABLE" >&2; exit 3; } sha256_of() { if command -v sha256sum >/dev/null 2>&1; then sha256sum "$1" | awk '{print $1}' else shasum -a 256 "$1" | awk '{print $1}'; fi } case "$CMD" in attest) KEY="${3:-}" [[ -f "$ART" && -f "$KEY" ]] || { echo "usage: artifact-attest.sh attest " >&2; exit 64; } H="$(sha256_of "$ART")" ATT="$ART.att" printf '{"artifact":"%s","sha256":"%s","attested_at":"%s"}\n' \ "$(basename "$ART")" "$H" "$(date -u +%Y-%m-%dT%H:%M:%SZ)" > "$ATT" openssl dgst -sha256 -sign "$KEY" -out "$ATT.sig" "$ATT" 2>/dev/null \ || { echo "ATTEST_SIGN_FAILED" >&2; exit 2; } echo "ARTIFACT_ATTESTED artifact=$(basename "$ART") sha256=${H:0:16}… att=$ATT" exit 0 ;; verify) ATT="${3:-}"; KEY="${4:-}" [[ -f "$ART" ]] || { echo "ARTIFACT_MISSING file=$ART" >&2; exit 3; } [[ -n "$ATT" && -f "$ATT" ]] || { echo "ATTESTATION_MISSING file=$ATT — refusing (fail-closed)" >&2; exit 3; } [[ -f "$KEY" ]] || { echo "ATTEST_PUBKEY_MISSING key=$KEY" >&2; exit 3; } [[ -f "$ATT.sig" ]] || { echo "ATTESTATION_UNSIGNED file=$ATT — refusing (fail-closed)" >&2; exit 3; } if ! openssl dgst -sha256 -verify "$KEY" -signature "$ATT.sig" "$ATT" >/dev/null 2>&1; then echo "ATTESTATION_FORGED file=$ATT — tampered or wrong key" >&2; exit 2 fi WANT="$(python3 -c 'import json,sys;print(json.load(open(sys.argv[1])).get("sha256",""))' "$ATT" 2>/dev/null)" HAVE="$(sha256_of "$ART")" if [[ -z "$WANT" || "$WANT" != "$HAVE" ]]; then echo "ARTIFACT_MISMATCH deployed!=tested want=${WANT:0:16}… have=${HAVE:0:16}…" >&2; exit 2 fi echo "ARTIFACT_VERIFIED tested==deployed sha256=${HAVE:0:16}…" exit 0 ;; *) echo "Usage: artifact-attest.sh {attest |verify }" >&2 exit 64 ;; esac