#!/usr/bin/env bash # # Verifies downloaded IntegrationGuard release files before they are installed or used for an upgrade: # # ./verify-release.sh --key integrationguard-release-key.pem [--fingerprint HEX] SHA256SUMS [FILE...] # # SHA256SUMS lists the SHA-256 of every file of a release; SHA256SUMS.sig next to it is its Ed25519 signature made # with the IntegrationGuard release key. The script checks that signature with the public key given, prints the # key's fingerprint (compare it with the fingerprint IntegrationGuard publishes in the release notes and the customer # portal, or pass it with --fingerprint to have it compared), then checks the SHA-256 of each FILE (default: every # listed file that is present next to SHA256SUMS). Exits with status 1 when anything does not match. # Needs OpenSSL 3 and sha256sum. Shipped with both deployment packages and published with every release. set -Eeuo pipefail die() { printf 'verify-release: %s\n' "$*" >&2 exit 1 } usage() { sed -n '3,12p' "${BASH_SOURCE[0]}" | sed 's/^# \{0,1\}//'; } KEY='' EXPECTED='' SUMS='' FILES=() while (($#)); do case $1 in --key) [[ -n ${2:-} ]] || die "--key needs the public key file." KEY=$2 && shift 2 ;; --fingerprint) [[ -n ${2:-} ]] || die "--fingerprint needs the published fingerprint." EXPECTED=$2 && shift 2 ;; -h | --help) usage && exit 0 ;; -*) usage >&2 && exit 2 ;; *) if [[ -z $SUMS ]]; then SUMS=$1; else FILES+=("$1"); fi shift ;; esac done [[ -n $KEY && -n $SUMS ]] || { usage >&2 && exit 2; } command -v openssl >/dev/null || die "openssl is required." command -v sha256sum >/dev/null || die "sha256sum is required." [[ $(openssl version 2>/dev/null) =~ ^OpenSSL\ ([3-9]|[1-9][0-9]) ]] || die "OpenSSL 3 or later is required ($(openssl version 2>/dev/null || printf 'none'))." [[ -f $KEY ]] || die "$KEY not found." [[ -f $SUMS ]] || die "$SUMS not found." [[ -f $SUMS.sig ]] || die "$SUMS.sig (the signature) not found next to $SUMS." if openssl pkey -in "$KEY" -noout 2>/dev/null; then die "$KEY holds a private key. Only the public release key is needed to verify; keep private keys off this machine." fi key_text=$(openssl pkey -pubin -in "$KEY" -noout -text 2>/dev/null) || key_text= [[ $key_text == "ED25519 Public-Key"* ]] || die "$KEY is not an Ed25519 public key." fingerprint=$(openssl pkey -pubin -in "$KEY" -outform DER 2>/dev/null | sha256sum | cut -c1-64) printf 'Release key fingerprint (SHA-256): %s\n' "$(sed 's/.\{8\}/& /g; s/ $//' <<<"$fingerprint")" if [[ -n $EXPECTED ]]; then expected=$(tr -d ' :' <<<"${EXPECTED,,}") [[ $expected == "$fingerprint" ]] || die "the key's fingerprint does not match the published one ($EXPECTED)." printf 'Fingerprint matches the published one.\n' else printf 'Compare it with the fingerprint IntegrationGuard publishes (release notes, customer portal).\n' fi openssl pkeyutl -verify -pubin -inkey "$KEY" -rawin -in "$SUMS" -sigfile "$SUMS.sig" >/dev/null 2>&1 || die "the signature of $SUMS is NOT valid for this key: do not use these files." printf 'Signature of %s: valid\n' "$(basename "$SUMS")" dir=$(cd "$(dirname "$SUMS")" && pwd) list=$(mktemp) trap 'rm -f "$list"' EXIT if ((${#FILES[@]} > 0)); then for file in "${FILES[@]}"; do name=$(basename "$file") [[ $(cd "$(dirname "$file")" && pwd) == "$dir" ]] || die "$file must be in the same folder as $SUMS." grep -E "^[0-9a-f]{64} \*?$(sed 's/[][\.*^$+?(){}|/]/\\&/g' <<<"$name")\$" "$SUMS" >>"$list" || die "$name is not listed in $SUMS." done else while read -r sum name; do name=${name#\*} [[ -f $dir/$name ]] && printf '%s %s\n' "$sum" "$name" >>"$list" done <"$SUMS" [[ -s $list ]] || die "none of the files listed in $SUMS is present in $dir." fi if ! (cd "$dir" && sha256sum --check --strict "$list"); then die "a file does not match its checksum: do not use it." fi printf 'Verified %d file(s).\n' "$(wc -l <"$list")"