#!/usr/bin/env bash # SPDX-License-Identifier: MIT # Authenticode-sign Windows executables with Azure Artifact Signing (formerly # Trusted Signing), from Linux, with jsign. Every signature is RFC 3161 # timestamped: the signing certificates live only a few days, the timestamp is # what keeps a signature valid after that. # # ci/sign-windows.sh FILE.exe [FILE.exe ...] # # Configuration (environment): # AZURE_SIGNING_PROFILE certificate profile name; EMPTY = signing not set # up yet: the files are left unsigned (exit 0) # AZURE_SIGNING_ACCOUNT signing account (default blueflaresigning) # AZURE_SIGNING_ENDPOINT account endpoint (default https://wus2.codesigning.azure.net) # Credentials, one of: # AZURE_SIGNING_TENANT_ID + AZURE_SIGNING_CLIENT_ID + AZURE_SIGNING_CLIENT_SECRET # (the CI service principal alkane-toolkit-ci-signer, role # "Artifact Signing Certificate Profile Signer" on the account only) # AZURE_SIGNING_TOKEN an access token for https://codesigning.azure.net, # e.g. from `az account get-access-token --resource https://codesigning.azure.net` # ALKANE_JSIGN_JAR jsign jar (default /opt/jsign/jsign.jar, from the CI image) # # Once a profile is configured, any failure fails the job: a release is never # shipped half-signed or silently unsigned. set -euo pipefail [ "$#" -gt 0 ] || { sed -n '3,25p' "$0" >&2; exit 2; } profile="${AZURE_SIGNING_PROFILE:-}" if [ -z "$profile" ]; then echo "Windows code signing is not enabled (AZURE_SIGNING_PROFILE unset); leaving $* unsigned" exit 0 fi account="${AZURE_SIGNING_ACCOUNT:-blueflaresigning}" endpoint="${AZURE_SIGNING_ENDPOINT:-https://wus2.codesigning.azure.net}" jar="${ALKANE_JSIGN_JAR:-/opt/jsign/jsign.jar}" [ -f "$jar" ] || { echo "jsign not found at $jar" >&2; exit 1; } if [ -z "${AZURE_SIGNING_TOKEN:-}" ]; then : "${AZURE_SIGNING_TENANT_ID:?signing credentials missing}" : "${AZURE_SIGNING_CLIENT_ID:?signing credentials missing}" : "${AZURE_SIGNING_CLIENT_SECRET:?signing credentials missing}" # Client-credentials token for the signing service. The secret and token # stay in this process's environment, never on a command line. AZURE_SIGNING_TOKEN="$(python3 - <<'PY' import json, os, sys, urllib.parse, urllib.request body = urllib.parse.urlencode({ "grant_type": "client_credentials", "client_id": os.environ["AZURE_SIGNING_CLIENT_ID"], "client_secret": os.environ["AZURE_SIGNING_CLIENT_SECRET"], "scope": "https://codesigning.azure.net/.default", }).encode() url = f"https://login.microsoftonline.com/{os.environ['AZURE_SIGNING_TENANT_ID']}/oauth2/v2.0/token" try: with urllib.request.urlopen(url, body, timeout=60) as r: print(json.load(r)["access_token"]) except Exception as e: sys.exit(f"could not get a signing token: {e}") PY )" fi export AZURE_SIGNING_TOKEN for file in "$@"; do java -jar "$jar" \ --storetype TRUSTEDSIGNING \ --keystore "${endpoint#https://}" \ --storepass env:AZURE_SIGNING_TOKEN \ --alias "$account/$profile" \ --alg SHA-256 \ --tsaurl http://timestamp.acs.microsoft.com --tsmode RFC3161 \ --tsretries 3 --tsretrywait 10 \ --name "Alkane Toolkit" \ --url https://gitea.bluefla.re/blueflare/alkane-toolkit \ "$file" # jsign fails on any error; also make sure a signature actually landed in # the PE certificate table before the file goes anywhere. python3 - "$file" <<'PY' import struct, sys data = open(sys.argv[1], "rb").read() pe = struct.unpack_from("