diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 65d953725..18b263ab2 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -2,22 +2,27 @@ name: Build # Builds ZFlash for Linux (AppImage), Windows (installer) and macOS (DMG). # Runs on every pull request, and is reused by the release workflow. +# +# With sign: true the Windows and macOS jobs run in the zflash-release +# environment (which needs an approval) and sign with ZEDEDA's certificates: +# Windows through Azure Artifact Signing, macOS with the Developer ID and +# Apple notarization. Pull request builds are never signed. on: pull_request: workflow_call: inputs: - sign-windows: - description: Sign the Windows binaries (needs the WINDOWS_SIGNING_PFX secrets) + sign: + description: Sign the Windows and macOS packages (zflash-release environment) type: boolean default: false - secrets: - WINDOWS_SIGNING_PFX: - description: Base64-encoded .pfx code signing certificate - required: false - WINDOWS_SIGNING_PFX_PASSWORD: - description: Password of the .pfx - required: false + outputs: + macos-notarization: + description: "notarized, pending (Apple still reviewing) or unsigned" + value: ${{ jobs.macos.outputs.notarization }} + macos-submission: + description: Apple notary submission ID of the macOS DMG + value: ${{ jobs.macos.outputs.submission }} permissions: contents: read @@ -82,6 +87,16 @@ jobs: windows: name: Windows (x64 installer) runs-on: windows-2022 + environment: ${{ inputs.sign && 'zflash-release' || '' }} + permissions: + contents: read + id-token: write + env: + SIGN: ${{ inputs.sign && 'true' || 'false' }} + # Pinned versions of the Azure signing client and of a signtool new + # enough to load it (needs Windows SDK 10.0.22621.755 or later). + ARTIFACT_SIGNING_CLIENT_VERSION: "1.0.151" + SDK_BUILD_TOOLS_VERSION: "10.0.28000.2957" steps: - uses: actions/checkout@v7 @@ -102,29 +117,68 @@ jobs: choco install innosetup --no-progress -y } - - name: Import signing certificate - id: cert - if: inputs.sign-windows + - name: Sign in to Azure + if: inputs.sign + uses: azure/login@v2 + with: + client-id: ${{ vars.AZURE_CLIENT_ID }} + tenant-id: ${{ vars.AZURE_TENANT_ID }} + allow-no-subscriptions: true + + - name: Prepare Azure Artifact Signing + id: signing + if: inputs.sign shell: pwsh env: - PFX_B64: ${{ secrets.WINDOWS_SIGNING_PFX }} - PFX_PASSWORD: ${{ secrets.WINDOWS_SIGNING_PFX_PASSWORD }} + AZURE_SIGN_ENDPOINT: ${{ vars.AZURE_SIGN_ENDPOINT }} + AZURE_SIGN_ACCOUNT: ${{ vars.AZURE_SIGN_ACCOUNT }} + AZURE_SIGN_PROFILE: ${{ vars.AZURE_SIGN_PROFILE }} run: | - if (-not $env:PFX_B64) { throw "sign-windows requested but the WINDOWS_SIGNING_PFX secret is not set" } - $pfx = Join-Path $env:RUNNER_TEMP "sign.pfx" - [IO.File]::WriteAllBytes($pfx, [Convert]::FromBase64String($env:PFX_B64)) - $pw = ConvertTo-SecureString $env:PFX_PASSWORD -AsPlainText -Force - $cert = Import-PfxCertificate -FilePath $pfx -Password $pw -CertStoreLocation Cert:\CurrentUser\My | - Where-Object { $_.HasPrivateKey } | Select-Object -First 1 - Remove-Item $pfx - if (-not $cert) { throw "No certificate with a private key found in WINDOWS_SIGNING_PFX" } - Write-Host "Signing with $($cert.Subject) ($($cert.Thumbprint))" - "thumbprint=$($cert.Thumbprint)" >> $env:GITHUB_OUTPUT + $ErrorActionPreference = 'Stop' + foreach ($name in 'AZURE_SIGN_ENDPOINT','AZURE_SIGN_ACCOUNT','AZURE_SIGN_PROFILE') { + if (-not (Get-Item "env:$name").Value) { throw "$name is not set on the zflash-release environment" } + } + # Inno Setup passes the signing command line unquoted: keep these paths free of spaces. + $tools = Join-Path $env:RUNNER_TEMP 'signing' + New-Item -ItemType Directory -Force -Path $tools | Out-Null + function Get-Package($id, $version) { + $dir = Join-Path $tools $id + $pkg = "$dir.zip" + Invoke-WebRequest -Uri "https://api.nuget.org/v3-flatcontainer/$($id.ToLower())/$version/$($id.ToLower()).$version.nupkg" -OutFile $pkg + Expand-Archive -Path $pkg -DestinationPath $dir -Force + Remove-Item $pkg + return $dir + } + $client = Get-Package 'Microsoft.ArtifactSigning.Client' $env:ARTIFACT_SIGNING_CLIENT_VERSION + $sdk = Get-Package 'Microsoft.Windows.SDK.BuildTools' $env:SDK_BUILD_TOOLS_VERSION + $dlib = Join-Path $client 'bin\x64\Azure.CodeSigning.Dlib.dll' + $signtool = Get-ChildItem -Path (Join-Path $sdk 'bin') -Recurse -Filter signtool.exe | + Where-Object { $_.Directory.Name -eq 'x64' } | Select-Object -First 1 + if (-not (Test-Path $dlib)) { throw "Azure.CodeSigning.Dlib.dll not found in the signing client package" } + if (-not $signtool) { throw "signtool.exe not found in the SDK build tools package" } + # Only the Azure CLI sign-in from the previous step is used. + $metadata = Join-Path $tools 'metadata.json' + @{ + Endpoint = $env:AZURE_SIGN_ENDPOINT + CodeSigningAccountName = $env:AZURE_SIGN_ACCOUNT + CertificateProfileName = $env:AZURE_SIGN_PROFILE + ExcludeCredentials = @( + 'EnvironmentCredential', 'WorkloadIdentityCredential', 'ManagedIdentityCredential', + 'SharedTokenCacheCredential', 'VisualStudioCredential', 'VisualStudioCodeCredential', + 'AzurePowerShellCredential', 'AzureDeveloperCliCredential', 'InteractiveBrowserCredential' + ) + } | ConvertTo-Json | Set-Content -Path $metadata -Encoding utf8 + Write-Host "signtool: $($signtool.FullName)" + "signtool=$($signtool.FullName)" >> $env:GITHUB_OUTPUT + "dlib=$dlib" >> $env:GITHUB_OUTPUT + "metadata=$metadata" >> $env:GITHUB_OUTPUT - name: Build installer shell: pwsh env: - THUMBPRINT: ${{ steps.cert.outputs.thumbprint }} + SIGNTOOL: ${{ steps.signing.outputs.signtool }} + SIGN_DLIB: ${{ steps.signing.outputs.dlib }} + SIGN_METADATA: ${{ steps.signing.outputs.metadata }} run: | $mingw = Join-Path $env:RUNNER_WORKSPACE "Qt\Tools\mingw1310_64" if (-not (Test-Path "$mingw\bin\g++.exe")) { throw "MinGW not found at $mingw" } @@ -140,8 +194,14 @@ jobs: "-DMINGW64_ROOT=$mingw", "-DENABLE_INNO_INSTALLER=ON" ) - if ($env:THUMBPRINT) { - $cmakeArgs += @("-DIMAGER_SIGNED_APP=ON", "-DIMAGER_SIGN_CERT_SHA1=$env:THUMBPRINT") + if ($env:SIGN -eq 'true') { + $cmakeArgs += @( + "-DIMAGER_SIGNED_APP=ON", + "-DSIGNTOOL=$env:SIGNTOOL", + "-DIMAGER_SIGN_DLIB=$env:SIGN_DLIB", + "-DIMAGER_SIGN_DLIB_METADATA=$env:SIGN_METADATA", + "-DIMAGER_SIGN_TIMESTAMP_URL=http://timestamp.acs.microsoft.com" + ) } cmake @cmakeArgs if ($LASTEXITCODE -ne 0) { throw "CMake configure failed" } @@ -156,6 +216,24 @@ jobs: mv build/installer/imager-*.exe "dist/ZFlash-${ver}-windows-x64-setup.exe" ls -l dist + - name: Check the signatures + if: inputs.sign + shell: powershell + env: + EXPECTED_PUBLISHER: ${{ vars.WINDOWS_PUBLISHER }} + run: | + $ErrorActionPreference = 'Stop' + if (-not $env:EXPECTED_PUBLISHER) { throw "WINDOWS_PUBLISHER is not set on the zflash-release environment" } + $files = @(Get-ChildItem dist -Filter '*-setup.exe') + @(Get-Item build\deploy\zflash.exe) + $failed = 0 + foreach ($file in $files) { + $sig = Get-AuthenticodeSignature -FilePath $file.FullName + $subject = if ($sig.SignerCertificate) { $sig.SignerCertificate.Subject } else { '(none)' } + Write-Host "$($file.Name): $($sig.Status), signer: $subject, timestamped: $([bool]$sig.TimeStamperCertificate)" + if ($sig.Status -ne 'Valid' -or $subject -ne $env:EXPECTED_PUBLISHER -or -not $sig.TimeStamperCertificate) { $failed++ } + } + if ($failed) { throw "$failed file(s) failed the signature check" } + - uses: actions/upload-artifact@v7 with: name: zflash-windows-x64 @@ -166,6 +244,16 @@ jobs: macos: name: macOS (universal DMG) runs-on: macos-latest + environment: ${{ inputs.sign && 'zflash-release' || '' }} + outputs: + notarization: ${{ steps.notary.outputs.status || 'unsigned' }} + submission: ${{ steps.notary.outputs.submission }} + env: + SIGN: ${{ inputs.sign && 'true' || 'false' }} + # How long the job waits for Apple. A new app can be held for + # "in-depth analysis" for hours or days; the release then stays a draft + # and the "Finish macOS notarization" workflow completes it later. + NOTARY_WAIT: 20m steps: - uses: actions/checkout@v7 @@ -178,9 +266,45 @@ jobs: arch: clang_64 cache: true + - name: Import the Developer ID certificate + id: cert + if: inputs.sign + env: + CSC_LINK: ${{ secrets.CSC_LINK }} + CSC_KEY_PASSWORD: ${{ secrets.CSC_KEY_PASSWORD }} + run: | + set -euo pipefail + for name in CSC_LINK CSC_KEY_PASSWORD; do + [ -n "${!name}" ] || { echo "::error::$name is not set on the zflash-release environment"; exit 1; } + done + brew install create-dmg + # A keychain of this job's own, removed in the last step. + kc="$RUNNER_TEMP/zflash-signing.keychain-db" + kcpw=$(openssl rand -hex 24) + p12="$RUNNER_TEMP/zflash-signing.p12" + trap 'rm -f "$p12"' EXIT + security create-keychain -p "$kcpw" "$kc" + security set-keychain-settings -lut 21600 "$kc" + security unlock-keychain -p "$kcpw" "$kc" + printf '%s' "$CSC_LINK" | base64 --decode > "$p12" + security import "$p12" -k "$kc" -P "$CSC_KEY_PASSWORD" -T /usr/bin/codesign -T /usr/bin/security + security set-key-partition-list -S apple-tool:,apple:,codesign: -s -k "$kcpw" "$kc" >/dev/null + security list-keychains -d user -s "$kc" $(security list-keychains -d user | tr -d '"') + identity=$(security find-identity -v -p codesigning "$kc" | sed -nE 's/.*"(Developer ID Application: [^"]+)".*/\1/p' | head -1) + [ -n "$identity" ] || { echo "::error::No Developer ID Application identity in CSC_LINK"; exit 1; } + echo "Signing as: $identity" + echo "keychain=$kc" >> "$GITHUB_OUTPUT" + echo "identity=$identity" >> "$GITHUB_OUTPUT" + - name: Build DMG + env: + IDENTITY: ${{ steps.cert.outputs.identity }} run: | - cmake -S src -B build -DCMAKE_BUILD_TYPE=MinSizeRel -DQt6_ROOT="$QT_ROOT_DIR" + args=(-S src -B build -DCMAKE_BUILD_TYPE=MinSizeRel -DQt6_ROOT="$QT_ROOT_DIR") + if [ "$SIGN" = "true" ]; then + args+=(-DIMAGER_SIGNED_APP=ON -DIMAGER_SIGNING_IDENTITY="$IDENTITY") + fi + cmake "${args[@]}" cmake --build build -j "$(sysctl -n hw.ncpu)" cmake --build build --target dmg @@ -191,6 +315,82 @@ jobs: mv "build/ZFlash Imager-${ver}.dmg" "dist/ZFlash-${ver}-macos-universal.dmg" ls -l dist + - name: Notarize + id: notary + if: inputs.sign + env: + APPLE_API_KEY_P8: ${{ secrets.APPLE_API_KEY_P8 }} + APPLE_API_KEY_ID: ${{ secrets.APPLE_API_KEY_ID }} + APPLE_API_ISSUER: ${{ secrets.APPLE_API_ISSUER }} + run: | + set -euo pipefail + for name in APPLE_API_KEY_P8 APPLE_API_KEY_ID APPLE_API_ISSUER; do + [ -n "${!name}" ] || { echo "::error::$name is not set on the zflash-release environment"; exit 1; } + done + key="$RUNNER_TEMP/zflash-notary.p8" + trap 'rm -f "$key"' EXIT + printf '%s\n' "$APPLE_API_KEY_P8" > "$key" + auth=(--key "$key" --key-id "$APPLE_API_KEY_ID" --issuer "$APPLE_API_ISSUER") + dmg=$(ls dist/*.dmg) + field() { python3 -c 'import json,sys;print(json.load(open(sys.argv[1]))[sys.argv[2]])' "$@"; } + + xcrun notarytool submit "$dmg" "${auth[@]}" --output-format json > submit.json + id=$(field submit.json id) + echo "Submitted to Apple: $id" + echo "submission=$id" >> "$GITHUB_OUTPUT" + + # Waits at most NOTARY_WAIT; Apple keeps reviewing after a timeout. + xcrun notarytool wait "$id" "${auth[@]}" --timeout "$NOTARY_WAIT" || true + xcrun notarytool info "$id" "${auth[@]}" --output-format json > info.json + status=$(field info.json status) + echo "Apple status: $status" + case "$status" in + Accepted) + xcrun stapler staple "$dmg" + echo "status=notarized" >> "$GITHUB_OUTPUT" + ;; + "In Progress") + echo "::warning::Apple is still reviewing the DMG (submission $id). The release stays a draft; run the 'Finish macOS notarization' workflow later." + echo "status=pending" >> "$GITHUB_OUTPUT" + ;; + *) + xcrun notarytool log "$id" "${auth[@]}" || true + echo "::error::Notarization ended with status $status" + exit 1 + ;; + esac + + - name: Check the signatures + if: inputs.sign + env: + IDENTITY: ${{ steps.cert.outputs.identity }} + NOTARIZATION: ${{ steps.notary.outputs.status }} + run: | + set -euo pipefail + dmg=$(ls dist/*.dmg) + codesign --verify --strict --verbose=2 "$dmg" + codesign -dvv "$dmg" 2>&1 | grep -qF "Authority=$IDENTITY" || { echo "::error::DMG is not signed by $IDENTITY"; exit 1; } + mnt=$(mktemp -d) + hdiutil attach -nobrowse -readonly -noautoopen -mountpoint "$mnt" "$dmg" >/dev/null + trap 'hdiutil detach "$mnt" >/dev/null || true' EXIT + app=$(ls -d "$mnt"/*.app) + codesign --verify --deep --strict --verbose=2 "$app" + codesign -dvv "$app" 2>&1 | grep -qF "Authority=$IDENTITY" || { echo "::error::App is not signed by $IDENTITY"; exit 1; } + # Gatekeeper accepts the DMG only once it is notarized. + if [ "$NOTARIZATION" = "notarized" ]; then + xcrun stapler validate "$dmg" + spctl --assess --type open --context context:primary-signature -vv "$dmg" + spctl --assess --type execute -vv "$app" + else + echo "Notarization pending: the Gatekeeper checks run in 'Finish macOS notarization'." + fi + + - name: Remove the signing keychain + if: always() && steps.cert.outputs.keychain + env: + KEYCHAIN: ${{ steps.cert.outputs.keychain }} + run: security delete-keychain "$KEYCHAIN" || true + - uses: actions/upload-artifact@v7 with: name: zflash-macos-universal diff --git a/.github/workflows/finish-notarization.yml b/.github/workflows/finish-notarization.yml new file mode 100644 index 000000000..485920395 --- /dev/null +++ b/.github/workflows/finish-notarization.yml @@ -0,0 +1,152 @@ +name: Finish macOS notarization + +# Manually triggered. Completes a release whose macOS DMG Apple was still +# notarizing when the Release workflow ended (Apple can hold a new app for +# "in-depth analysis" for hours or days). +# +# Asks Apple about the submission recorded in the draft release: +# - still in progress: says so and stops (run it again later); +# - accepted: staples the ticket to the DMG, checks it with Gatekeeper, +# replaces the DMG and its SHA256SUMS line in the release, and removes +# the pending note. The release stays a draft until someone publishes it; +# - rejected: prints Apple's log and fails. + +on: + workflow_dispatch: + inputs: + tag: + description: Release tag (e.g. v1.2.0) + required: true + type: string + +permissions: + contents: write + +concurrency: + group: finish-notarization-${{ inputs.tag }} + cancel-in-progress: false + +jobs: + finish: + name: Finish macOS notarization + runs-on: macos-latest + environment: zflash-release + timeout-minutes: 30 + env: + GH_TOKEN: ${{ github.token }} + TAG: ${{ inputs.tag }} + steps: + - name: Find the pending submission + id: release + run: | + set -euo pipefail + # Drafts have no tag yet, so look the release up by its tag name. + gh api --paginate "repos/$GITHUB_REPOSITORY/releases" \ + --jq ".[] | select(.tag_name == \"$TAG\")" > release.json + [ -s release.json ] || { echo "::error::No release with tag $TAG"; exit 1; } + python3 - <<'PY' + import json, os, re + r = json.load(open("release.json")) + m = re.search(r"", r.get("body") or "") + dmgs = [a for a in r["assets"] if a["name"].endswith("-macos-universal.dmg")] + sums = [a for a in r["assets"] if a["name"] == "SHA256SUMS"] + out = open(os.environ["GITHUB_OUTPUT"], "a") + out.write(f"id={r['id']}\n") + out.write(f"submission={m.group(1) if m else ''}\n") + if m: + if len(dmgs) != 1 or len(sums) != 1: + raise SystemExit("::error::Expected one macOS DMG and one SHA256SUMS in the release") + out.write(f"dmg_name={dmgs[0]['name']}\ndmg_asset={dmgs[0]['id']}\nsums_asset={sums[0]['id']}\n") + PY + if [ -z "$(grep '^submission=' "$GITHUB_OUTPUT" | tail -1 | cut -d= -f2)" ]; then + echo "Release $TAG has no pending macOS notarization. Nothing to do." + fi + + - name: Ask Apple + id: apple + if: steps.release.outputs.submission != '' + env: + SUBMISSION: ${{ steps.release.outputs.submission }} + APPLE_API_KEY_P8: ${{ secrets.APPLE_API_KEY_P8 }} + APPLE_API_KEY_ID: ${{ secrets.APPLE_API_KEY_ID }} + APPLE_API_ISSUER: ${{ secrets.APPLE_API_ISSUER }} + run: | + set -euo pipefail + key="$RUNNER_TEMP/zflash-notary.p8" + trap 'rm -f "$key"' EXIT + printf '%s\n' "$APPLE_API_KEY_P8" > "$key" + auth=(--key "$key" --key-id "$APPLE_API_KEY_ID" --issuer "$APPLE_API_ISSUER") + xcrun notarytool info "$SUBMISSION" "${auth[@]}" --output-format json > info.json + status=$(python3 -c 'import json;print(json.load(open("info.json"))["status"])') + echo "Apple status of $SUBMISSION: $status" + case "$status" in + Accepted) echo "accepted=true" >> "$GITHUB_OUTPUT" ;; + "In Progress") + echo "::notice::Apple is still reviewing submission $SUBMISSION. Run this workflow again later." + ;; + *) + xcrun notarytool log "$SUBMISSION" "${auth[@]}" || true + echo "::error::Notarization ended with status $status" + exit 1 + ;; + esac + + - name: Staple and check the DMG + if: steps.apple.outputs.accepted == 'true' + env: + DMG_NAME: ${{ steps.release.outputs.dmg_name }} + DMG_ASSET: ${{ steps.release.outputs.dmg_asset }} + run: | + set -euo pipefail + mkdir -p dist + dmg="dist/$DMG_NAME" + gh api -H "Accept: application/octet-stream" \ + "repos/$GITHUB_REPOSITORY/releases/assets/$DMG_ASSET" > "$dmg" + # Fails unless Apple's ticket matches this exact file. + xcrun stapler staple "$dmg" + xcrun stapler validate "$dmg" + codesign --verify --strict --verbose=2 "$dmg" + spctl --assess --type open --context context:primary-signature -vv "$dmg" + mnt=$(mktemp -d) + hdiutil attach -nobrowse -readonly -noautoopen -mountpoint "$mnt" "$dmg" >/dev/null + trap 'hdiutil detach "$mnt" >/dev/null || true' EXIT + app=$(ls -d "$mnt"/*.app) + codesign --verify --deep --strict --verbose=2 "$app" + spctl --assess --type execute -vv "$app" + + - name: Update the release + if: steps.apple.outputs.accepted == 'true' + env: + RELEASE_ID: ${{ steps.release.outputs.id }} + DMG_NAME: ${{ steps.release.outputs.dmg_name }} + DMG_ASSET: ${{ steps.release.outputs.dmg_asset }} + SUMS_ASSET: ${{ steps.release.outputs.sums_asset }} + run: | + set -euo pipefail + repo="$GITHUB_REPOSITORY" + upload() { # name, file, content type + gh api -X POST -H "Content-Type: $3" --input "$2" \ + "https://uploads.github.com/repos/$repo/releases/$RELEASE_ID/assets?name=$1" >/dev/null + } + # SHA256SUMS: replace the DMG's line (stapling changed the file). + gh api -H "Accept: application/octet-stream" "repos/$repo/releases/assets/$SUMS_ASSET" > SHA256SUMS + new=$(cd dist && shasum -a 256 "$DMG_NAME") + grep -vF " $DMG_NAME" SHA256SUMS > SHA256SUMS.new || true + echo "$new" >> SHA256SUMS.new + sort -k2 SHA256SUMS.new > SHA256SUMS + cat SHA256SUMS + + gh api -X DELETE "repos/$repo/releases/assets/$DMG_ASSET" + upload "$DMG_NAME" "dist/$DMG_NAME" application/x-apple-diskimage + gh api -X DELETE "repos/$repo/releases/assets/$SUMS_ASSET" + upload SHA256SUMS SHA256SUMS text/plain + + # Remove the pending note from the release notes. + python3 - <<'PY' + import json, re + body = json.load(open("release.json")).get("body") or "" + body = re.sub(r"\n*", "", body, flags=re.S) + json.dump({"body": body}, open("patch.json", "w")) + PY + gh api -X PATCH "repos/$repo/releases/$RELEASE_ID" --input patch.json >/dev/null + echo "::notice::The macOS DMG of $TAG is notarized and stapled. The release is still a draft: publish it when ready." diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index aa8f396a3..3d6f26d6b 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -1,7 +1,10 @@ name: Release # Manually triggered: builds ZFlash for Linux, Windows and macOS, creates the -# GitHub release and uploads the packages to it. +# GitHub release and uploads the packages to it. By default the Windows +# installer is signed through Azure Artifact Signing and the macOS DMG is +# signed with ZEDEDA's Developer ID and notarized; both jobs wait for an +# approval of the zflash-release environment. on: workflow_dispatch: @@ -18,10 +21,10 @@ on: description: Mark the release as a pre-release type: boolean default: false - sign-windows: - description: Sign the Windows binaries (needs the WINDOWS_SIGNING_PFX secrets) + sign: + description: Sign the Windows and macOS packages (needs approval of the zflash-release environment) type: boolean - default: false + default: true permissions: contents: read @@ -59,9 +62,13 @@ jobs: build: name: Build needs: prepare + permissions: + contents: read + # Lets the Windows job sign in to Azure Artifact Signing with OIDC. + id-token: write uses: ./.github/workflows/build.yml with: - sign-windows: ${{ inputs.sign-windows }} + sign: ${{ inputs.sign }} secrets: inherit release: @@ -91,14 +98,29 @@ jobs: VERSION: ${{ needs.prepare.outputs.version }} DRAFT: ${{ inputs.draft }} PRERELEASE: ${{ inputs.prerelease }} + NOTARIZATION: ${{ needs.build.outputs.macos-notarization }} + SUBMISSION: ${{ needs.build.outputs.macos-submission }} run: | flags=() + notes="" [ "$DRAFT" = "true" ] && flags+=(--draft) [ "$PRERELEASE" = "true" ] && flags+=(--prerelease) + if [ "$NOTARIZATION" = "pending" ]; then + # The DMG is signed but Apple has not finished notarizing it, so + # Gatekeeper would still block it: never publish it like this. + [ "$DRAFT" = "true" ] || echo "::warning::Created as a draft: Apple is still notarizing the macOS DMG." + [ "$DRAFT" = "true" ] || flags+=(--draft) + notes=$(printf '%s\n' \ + "" \ + "> [!WARNING]" \ + "> Apple is still notarizing the macOS DMG (submission \`$SUBMISSION\`). Run the **Finish macOS notarization** workflow with tag \`$TAG\` before publishing; it staples the DMG and removes this note." \ + "" "") + fi # Creates the tag on the built commit if it does not exist yet. gh release create "$TAG" dist/* \ --repo "$GITHUB_REPOSITORY" \ --target "$GITHUB_SHA" \ --title "ZFlash $VERSION" \ + --notes "$notes" \ --generate-notes \ "${flags[@]}" diff --git a/DEVELOPER.md b/DEVELOPER.md index c31d2e793..1c521f377 100644 --- a/DEVELOPER.md +++ b/DEVELOPER.md @@ -115,9 +115,29 @@ Building ZFlash on Windows is best done with Visual Studio Code (or a derivative - `.github/workflows/release.yml` is started manually (Actions → Release → Run workflow). It builds the same packages, creates the GitHub release (tag defaults to `v` from `src/CMakeLists.txt`, created as a draft by default) and uploads the packages plus a `SHA256SUMS` file. -- To sign the Windows binaries in a release, add the repository secrets `WINDOWS_SIGNING_PFX` - (the .pfx file, base64-encoded: `base64 -w0 sign.pfx`) and `WINDOWS_SIGNING_PFX_PASSWORD`, then - tick "Sign the Windows binaries" when running the workflow. +- Releases are signed by default ("Sign the Windows and macOS packages" in the Run workflow form). + The Windows and macOS jobs then run in the `zflash-release` environment, which waits for an + approval and is limited to the `zflash` branch: + - Windows: the app, the installer and its uninstaller are signed by **Zededa Germany GmbH** through + Azure Artifact Signing. The job signs in to Azure with GitHub OIDC (no stored password); the + environment variables `AZURE_CLIENT_ID`, `AZURE_TENANT_ID`, `AZURE_SIGN_ENDPOINT`, + `AZURE_SIGN_ACCOUNT`, `AZURE_SIGN_PROFILE` and `WINDOWS_PUBLISHER` configure it. Locally, CMake + takes the same setup through `-DSIGNTOOL`, `-DIMAGER_SIGN_DLIB` and `-DIMAGER_SIGN_DLIB_METADATA`. + - macOS: the app and the DMG are signed with the Developer ID of Zededa Germany GmbH, then the DMG + is notarized by Apple and the ticket stapled. The environment secrets `CSC_LINK` (base64 `.p12`), + `CSC_KEY_PASSWORD`, `APPLE_API_KEY_P8`, `APPLE_API_KEY_ID` and `APPLE_API_ISSUER` configure it + (the API key is "ZFlash CI" in App Store Connect). + - Apple sometimes holds a submission for "in-depth analysis" for hours or days, mostly for new apps. + The Release workflow waits 20 minutes; if Apple has not answered by then, it still creates the + release as a **draft** with the signed but not yet notarized DMG and a note naming the Apple + submission. Later, run Actions → **Finish macOS notarization** with the release tag: if Apple has + accepted the submission it staples the DMG, checks it with Gatekeeper, replaces it and its + `SHA256SUMS` line in the draft and removes the note; if Apple is still reviewing it, it says so and + stops, so it can simply be run again. Do not publish a release while the note is there, and do not + re-run the Release workflow just to retry: a new upload queues behind the held one. + - Both jobs end with a check that fails the release unless the signatures are valid and from + the expected signer. +- Pull request builds are never signed. ## Other notes diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index b6d7c5a05..64a5a4921 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -45,6 +45,11 @@ if (WIN32) set(IMAGER_SIGNED_APP OFF CACHE BOOL "Sign Imager and its installer as part of the build. Requires a valid Code Signing certificate.") set(IMAGER_SIGN_CERT_SHA1 "" CACHE STRING "SHA1 thumbprint of the code signing certificate (CurrentUser\\My store). Empty: let signtool pick the best one (/a).") set(IMAGER_SIGN_TIMESTAMP_URL "http://timestamp.digicert.com" CACHE STRING "RFC 3161 timestamp server used when signing. Empty: do not timestamp.") + # Remote signing (e.g. Azure Artifact Signing): signtool loads this library and its + # metadata file instead of using a certificate from the local store. Paths must not + # contain spaces, because Inno Setup passes the signing command line unquoted. + set(IMAGER_SIGN_DLIB "" CACHE FILEPATH "Signing library passed to signtool /dlib (e.g. Azure.CodeSigning.Dlib.dll). Empty: sign with a local certificate.") + set(IMAGER_SIGN_DLIB_METADATA "" CACHE FILEPATH "Metadata file passed to signtool /dmdf together with IMAGER_SIGN_DLIB.") endif() if (APPLE) @@ -807,7 +812,12 @@ if (WIN32) if (IMAGER_SIGN_TIMESTAMP_URL) list(APPEND SIGNTOOL_ARGS /tr ${IMAGER_SIGN_TIMESTAMP_URL} /td sha256) endif() - if (IMAGER_SIGN_CERT_SHA1) + if (IMAGER_SIGN_DLIB) + if (NOT IMAGER_SIGN_DLIB_METADATA) + message(FATAL_ERROR "IMAGER_SIGN_DLIB needs IMAGER_SIGN_DLIB_METADATA") + endif() + list(APPEND SIGNTOOL_ARGS /dlib ${IMAGER_SIGN_DLIB} /dmdf ${IMAGER_SIGN_DLIB_METADATA}) + elseif (IMAGER_SIGN_CERT_SHA1) list(APPEND SIGNTOOL_ARGS /sha1 ${IMAGER_SIGN_CERT_SHA1}) else() list(APPEND SIGNTOOL_ARGS /a) diff --git a/src/mac/create_styled_dmg.sh.in b/src/mac/create_styled_dmg.sh.in index 539ec3f45..145ffd4f0 100755 --- a/src/mac/create_styled_dmg.sh.in +++ b/src/mac/create_styled_dmg.sh.in @@ -6,11 +6,11 @@ set -e # Configuration -APP_NAME="Raspberry Pi Imager" +APP_NAME="@APP_NAME@" PROJECT_NAME="@CMAKE_PROJECT_NAME@" SOURCE_APP_PATH="@CMAKE_BINARY_DIR@/${PROJECT_NAME}.app" DMG_APP_PATH="@CMAKE_BINARY_DIR@/${APP_NAME}.app" -FINAL_DMG_PATH="@CMAKE_BINARY_DIR@/${PROJECT_NAME}-@IMAGER_VERSION_STR@.dmg" +FINAL_DMG_PATH="@FINAL_DMG_PATH@" DMG_BACKGROUND_PATH="@CMAKE_BINARY_DIR@/dmg_background.png" SIGNING_IDENTITY="@IMAGER_SIGNING_IDENTITY@" SHOULD_NOTARIZE="@IMAGER_NOTARIZE_APP@" @@ -80,34 +80,26 @@ if [ -n "$SIGNING_IDENTITY" ] && [ "$SIGNING_IDENTITY" != "NONE" ] && [ "$SIGNIN echo "Signing the deployed app bundle..." echo "Signing identity: $SIGNING_IDENTITY" - # Sign all the frameworks first - find "$DMG_APP_PATH" -name "*.framework" -type d | while read framework; do - echo "Signing framework: $framework" - codesign --force --verify --verbose --sign "$SIGNING_IDENTITY" --timestamp --options runtime "$framework" || { - echo "Warning: Failed to sign framework $framework" - } - done - - # Sign all dylibs - find "$DMG_APP_PATH" -name "*.dylib" -type f | while read dylib; do + # Sign from the inside out, so each bundle is sealed after its contents: + # loose dylibs and plug-ins first, then the frameworks, then the app. + # Any failure stops the build; an app with an unsigned part cannot be notarized. + sign() { + codesign --force --verbose --sign "$SIGNING_IDENTITY" --timestamp --options runtime "$1" + } + while IFS= read -r -d '' dylib; do echo "Signing dylib: $dylib" - codesign --force --verify --verbose --sign "$SIGNING_IDENTITY" --timestamp --options runtime "$dylib" || { - echo "Warning: Failed to sign dylib $dylib" - } - done - - # Sign the main app bundle + sign "$dylib" + done < <(find "$DMG_APP_PATH" -name "*.dylib" -type f -not -path "*.framework/*" -print0) + + while IFS= read -r -d '' framework; do + echo "Signing framework: $framework" + sign "$framework" + done < <(find "$DMG_APP_PATH" -name "*.framework" -type d -print0) + echo "Signing main app bundle: $DMG_APP_PATH" - - # Sign without entitlements to avoid provisioning profile requirements - echo "Signing without entitlements to avoid taskgated provisioning profile issues" - CODESIGN_CMD=(codesign --force --verify --verbose --sign "$SIGNING_IDENTITY" --timestamp --options runtime "$DMG_APP_PATH") - - "${CODESIGN_CMD[@]}" || { - echo "Error: Failed to sign main app bundle" - exit 1 - } - + sign "$DMG_APP_PATH" + codesign --verify --deep --strict --verbose=2 "$DMG_APP_PATH" + echo "✓ App bundle signed successfully" else echo "No signing identity provided, skipping app bundle signing" @@ -129,7 +121,7 @@ echo "Creating DMG with create-dmg..." # Build create-dmg command CREATE_DMG_CMD=( create-dmg - --volname "Raspberry Pi Imager" + --volname "${APP_NAME}" --window-pos 200 120 --window-size 600 400 --icon-size 128 @@ -145,8 +137,10 @@ else echo "No custom background image found, using default" fi -# Note: We don't add --codesign to create-dmg since we've already signed the app bundle -# create-dmg will sign the DMG itself, but the app bundle is already properly signed +# Sign the disk image itself too; Gatekeeper checks it before mounting. +if [ -n "$SIGNING_IDENTITY" ] && [ "$SIGNING_IDENTITY" != "NONE" ]; then + CREATE_DMG_CMD+=(--codesign "$SIGNING_IDENTITY") +fi # Add notarization if requested if [ "$SHOULD_NOTARIZE" = "ON" ] && [ -n "$KEYCHAIN_PROFILE" ] && [ "$KEYCHAIN_PROFILE" != "" ]; then diff --git a/src/mac/macos_post_build.sh.in b/src/mac/macos_post_build.sh.in index 84ec73698..2a17bd5a7 100644 --- a/src/mac/macos_post_build.sh.in +++ b/src/mac/macos_post_build.sh.in @@ -10,7 +10,7 @@ echo "Starting macOS post-build process..." # Configuration APP_NAME="@CMAKE_PROJECT_NAME@" APP_PATH="@CMAKE_BINARY_DIR@/${APP_NAME}.app" -FINAL_DMG_PATH="@CMAKE_BINARY_DIR@/${APP_NAME}-@IMAGER_VERSION_STR@.dmg" +FINAL_DMG_PATH="@FINAL_DMG_PATH@" SIGNING_IDENTITY="@IMAGER_SIGNING_IDENTITY@" SHOULD_NOTARIZE="@IMAGER_NOTARIZE_APP@" KEYCHAIN_PROFILE="@IMAGER_NOTARIZE_KEYCHAIN_PROFILE@"