| 1 | #!/bin/bash |
| 2 | |
| 3 | |
| 4 | set -euo pipefail |
| 5 | [[ $# == 2 && -d $1 ]] || { echo 'Usage: sign-macos-guest.sh /guest-kit signing-identity' >&2; exit 2; } |
| 6 | kit=$1 identity=$2 |
| 7 | src=$(cd "$(dirname "$0")" && pwd) |
| 8 | sign=(/usr/bin/codesign --force --sign "$identity") |
| 9 | if [[ $identity != - ]]; then |
| 10 | [[ $identity == 'Developer ID Application:'* || $identity =~ ^[[:xdigit:]]{40}$ ]] || { echo 'Guest release code requires a Developer ID Application name or certificate fingerprint.' >&2; exit 2; } |
| 11 | sign+=(--timestamp --options runtime) |
| 12 | fi |
| 13 | count=0 |
| 14 | while IFS= read -r -d '' executable; do |
| 15 | kind=$(/usr/bin/file -b "$executable") |
| 16 | [[ $kind == *Mach-O* ]] || continue |
| 17 | if [[ $executable == "$kit/project/cage-sshfs" ]]; then |
| 18 | |
| 19 | "${sign[@]}" --entitlements "$src/sshfs.entitlements" "$executable" |
| 20 | else |
| 21 | "${sign[@]}" "$executable" |
| 22 | fi |
| 23 | /usr/bin/codesign --verify --strict "$executable" |
| 24 | if [[ $identity != - ]]; then |
| 25 | metadata=$(/usr/bin/codesign -d --verbose=4 "$executable" 2>&1) |
| 26 | grep -q '^Authority=Developer ID Application:' <<< "$metadata" |
| 27 | grep -q 'flags=.*runtime' <<< "$metadata" |
| 28 | grep -q '^Timestamp=' <<< "$metadata" |
| 29 | fi |
| 30 | count=$((count+1)) |
| 31 | done < <(/usr/bin/find "$kit" -type f -print0) |
| 32 | [[ $count -gt 0 ]] || { echo 'Guest kit contains no native executable.' >&2; exit 1; } |
| 33 | while IFS= read -r -d '' app; do |
| 34 | "${sign[@]}" "$app" |
| 35 | /usr/bin/codesign --verify --deep --strict "$app" |
| 36 | done < <(/usr/bin/find "$kit" -depth -type d -name '*.app' -print0) |
| 37 | printf 'Signed and verified %s guest Mach-O files.\n' "$count" |