| 1 | #requires -Version 7.0 |
| 2 | # Explicit signing or read-only credential verification. No package, version or release writes. |
| 3 | [CmdletBinding()] |
| 4 | param([ValidateSet('sign', 'credential-info')][string]$Mode = 'sign') |
| 5 | . (Join-Path $PSScriptRoot 'windows-common.ps1') |
| 6 | if (-not $IsWindows) { throw 'This verification requires Windows trust APIs.' } |
| 7 | if ($env:GITHUB_ACTIONS -ne 'true' -or $env:GITHUB_EVENT_NAME -ne 'workflow_dispatch' -or |
| 8 | $env:GITHUB_REPOSITORY -ne 'Lee-xeo/Claude-Cage' -or |
| 9 | $env:GITHUB_REF -ne 'refs/heads/main') { |
| 10 | throw 'Run the explicit main-branch signing verification workflow.' |
| 11 | } |
| 12 | if ($env:CAGE_WINDOWS_SIGNING_PROVIDER -ne 'esigner') { throw 'Select esigner for this verification.' } |
| 13 | if ($Mode -eq 'credential-info') { |
| 14 | try { Invoke-BuildTool 'node' @((Join-Path $PSScriptRoot 'esigner.mjs'), 'diagnose') } |
| 15 | finally { Remove-Item Env:CAGE_ESIGNER_PASSWORD, Env:CAGE_ESIGNER_TOTP_SECRET -ErrorAction SilentlyContinue } |
| 16 | return |
| 17 | } |
| 18 | foreach ($name in @('CAGE_ESIGNER_USERNAME', 'CAGE_ESIGNER_PASSWORD', 'CAGE_ESIGNER_CREDENTIAL_ID', 'CAGE_ESIGNER_CERTIFICATE_SERIAL', 'CAGE_ESIGNER_TOTP_SECRET')) { |
| 19 | if (-not [Environment]::GetEnvironmentVariable($name)) { throw "Missing cage-release setting: $name" } |
| 20 | } |
| 21 | Assert-WindowsSigningConfiguration |
| 22 | $repository = Split-Path $PSScriptRoot -Parent |
| 23 | $reportDirectory = Join-Path $repository '_output/signing-check' |
| 24 | $stage = Join-Path ([IO.Path]::GetTempPath()) ('cage-signing-check-' + [guid]::NewGuid().ToString('N')) |
| 25 | $report = [ordered]@{ status = 'failed'; commit = $env:GITHUB_SHA; windowsTrustTested = $false; installationTested = $false } |
| 26 | $savedCertificate = $env:CAGE_WINDOWS_CERT_SHA1 |
| 27 | try { |
| 28 | New-Item -ItemType Directory -Path $stage | Out-Null |
| 29 | $source = Join-Path $stage 'SigningProbe.cs' |
| 30 | @' |
| 31 | using System; |
| 32 | public static class SigningProbe { |
| 33 | public static void Main() { Console.WriteLine("Cage code signing verification fixture."); } |
| 34 | } |
| 35 | '@ | Set-Content -LiteralPath $source -Encoding utf8 |
| 36 | $compiler = Join-Path $env:WINDIR 'Microsoft.NET/Framework64/v4.0.30319/csc.exe' |
| 37 | $executable = Join-Path $stage 'probe.exe' |
| 38 | Invoke-BuildTool $compiler @('/nologo', '/target:exe', "/out:$executable", $source) |
| 39 | # NSIS hands its signing hook a PE file without an extension. Exercise that path. |
| 40 | $probe = Join-Path $stage 'uninstaller-probe' |
| 41 | Move-Item -LiteralPath $executable -Destination $probe |
| 42 | $report.unsignedSHA256 = (Get-FileHash -LiteralPath $probe -Algorithm SHA256).Hash.ToLowerInvariant() |
| 43 | $env:CAGE_WINDOWS_CERT_SHA1 = $null |
| 44 | Sign-CageFile $probe (Get-SigningTool) |
| 45 | $signature = Get-AuthenticodeSignature -LiteralPath $probe |
| 46 | $report.windowsTrustTested = $true |
| 47 | $report.publisher = $signature.SignerCertificate.GetNameInfo([Security.Cryptography.X509Certificates.X509NameType]::SimpleName, $false) |
| 48 | $report.certificateSerial = $signature.SignerCertificate.SerialNumber |
| 49 | $report.certificateThumbprint = $signature.SignerCertificate.Thumbprint |
| 50 | $report.timestampAuthority = $signature.TimeStamperCertificate.Subject |
| 51 | $report.signedSHA256 = (Get-FileHash -LiteralPath $probe -Algorithm SHA256).Hash.ToLowerInvariant() |
| 52 | if ($report.unsignedSHA256 -eq $report.signedSHA256) { throw 'Signing did not change the executable.' } |
| 53 | $report.status = 'passed' |
| 54 | Write-Host 'Passed: cloud signing, configured publisher certificate, timestamp and Windows trust.' |
| 55 | } finally { |
| 56 | $env:CAGE_WINDOWS_CERT_SHA1 = $savedCertificate |
| 57 | Remove-Item Env:CAGE_ESIGNER_PASSWORD, Env:CAGE_ESIGNER_TOTP_SECRET -ErrorAction SilentlyContinue |
| 58 | if (Test-Path -LiteralPath $stage) { Remove-Item -LiteralPath $stage -Recurse -Force } |
| 59 | New-Item -ItemType Directory -Path $reportDirectory -Force | Out-Null |
| 60 | $report | ConvertTo-Json | Set-Content -LiteralPath (Join-Path $reportDirectory 'result.json') -Encoding utf8 |
| 61 | } |