| 1 | #requires -Version 7.0 |
| 2 | # Offline orchestration checks; Windows trust is exercised separately on Windows. |
| 3 | . (Join-Path $PSScriptRoot 'windows-common.ps1') |
| 4 | $stage = Join-Path ([IO.Path]::GetTempPath()) ('cage-signing-test-' + [guid]::NewGuid().ToString('N')) |
| 5 | New-Item -ItemType Directory -Path $stage | Out-Null |
| 6 | $names = @('CAGE_WINDOWS_SIGNING_PROVIDER', 'CAGE_ESIGNER_CERTIFICATE_SERIAL', 'CAGE_WINDOWS_CERT_SHA1') |
| 7 | $saved = @{} |
| 8 | foreach ($name in $names) { $saved[$name] = [Environment]::GetEnvironmentVariable($name) } |
| 9 | try { |
| 10 | $env:CAGE_WINDOWS_SIGNING_PROVIDER = 'esigner' |
| 11 | $env:CAGE_ESIGNER_CERTIFICATE_SERIAL = '7' * 32 |
| 12 | function Assert-WindowsSigningConfiguration {} |
| 13 | function Get-BuildCommand { return 'fixture-node' } |
| 14 | $script:mode = 'valid' |
| 15 | function Invoke-BuildTool { |
| 16 | param([string]$Tool, [string[]]$Arguments) |
| 17 | if ($Tool -eq 'fixture-node') { |
| 18 | New-Item -ItemType Directory -Path $Arguments[4] -Force | Out-Null |
| 19 | [IO.File]::WriteAllText((Join-Path $Arguments[4] ([IO.Path]::GetFileName($Arguments[2]))), 'signed fixture') |
| 20 | } elseif ($script:mode -eq 'untrusted') { throw 'Fixture trust failure' } |
| 21 | } |
| 22 | function Get-AuthenticodeSignature { |
| 23 | return [pscustomobject]@{ |
| 24 | Status = 'Valid' |
| 25 | TimeStamperCertificate = if ($script:mode -eq 'no-timestamp') { $null } else { 'fixture' } |
| 26 | SignerCertificate = [pscustomobject]@{ |
| 27 | SerialNumber = if ($script:mode -eq 'wrong-serial') { '8' * 32 } else { '7' * 32 } |
| 28 | Thumbprint = if ($script:mode -eq 'wrong-key') { 'b' * 40 } else { 'a' * 40 } |
| 29 | } |
| 30 | } |
| 31 | } |
| 32 | $inputFile = Join-Path $stage 'Cage 空格.exe' |
| 33 | foreach ($script:mode in @('valid', 'untrusted', 'no-timestamp', 'wrong-serial', 'wrong-key')) { |
| 34 | [IO.File]::WriteAllText($inputFile, 'original fixture') |
| 35 | $env:CAGE_WINDOWS_CERT_SHA1 = 'a' * 40 |
| 36 | $rejected = $false |
| 37 | try { Sign-CageFile $inputFile 'fixture-signtool' } catch { $rejected = $true } |
| 38 | if ($script:mode -eq 'valid') { |
| 39 | if ($rejected -or [IO.File]::ReadAllText($inputFile) -ne 'signed fixture') { throw 'Valid signed copy was not promoted.' } |
| 40 | } else { |
| 41 | if (-not $rejected -or [IO.File]::ReadAllText($inputFile) -ne 'original fixture') { throw "Original file was replaced on $script:mode." } |
| 42 | } |
| 43 | } |
| 44 | Write-Host 'Passed: valid output promoted; untrusted, untimestamped, wrong-serial and wrong-key outputs rejected without replacing originals.' |
| 45 | } finally { |
| 46 | foreach ($name in $names) { [Environment]::SetEnvironmentVariable($name, $saved[$name]) } |
| 47 | Remove-Item -LiteralPath $stage -Recurse -Force |
| 48 | } |