[CmdletBinding()] param([string]$SignedFixture, [string]$CertificateThumbprint, [string]$UntimestampedFixture) $ErrorActionPreference = 'Stop' Set-StrictMode -Version Latest $build = Join-Path $PSScriptRoot 'build-windows-release.ps1' function Must-Reject([scriptblock]$Action, [string]$Expected) { $rejected = $false try { & $Action } catch { if ($_.Exception.Message -notmatch $Expected) { throw } $rejected = $true } if (-not $rejected) { throw 'Signing preflight accepted an invalid configuration.' } } Must-Reject { & $build -CertificateThumbprint '' -SigningConfigPath (Join-Path $PSScriptRoot '../target/no-signing-identity.json') -PreflightOnly } 'certificate is required' Must-Reject { & $build -CertificateThumbprint 'not-a-certificate' -AllowUnsigned -PreflightOnly } 'Invalid.*thumbprint' Must-Reject { & $build -CertificateThumbprint ('0' * 40) -AllowUnsigned -PreflightOnly } 'not found' Must-Reject { & $build -CertificateThumbprint '' -AllowUnsigned -TimestampUrl 'file:///tmp/timestamp' -PreflightOnly } 'Timestamp URL' Must-Reject { & $build -CertificateThumbprint '' -AllowUnsigned -TimestampUrl 'https://user:secret@example.invalid' -PreflightOnly } 'Timestamp URL' $result = & $build -CertificateThumbprint '' -AllowUnsigned -PreflightOnly if ($result.signing -or -not $result.unsignedExplicitlyAllowed -or $result.digestAlgorithm -or $result.tsp) { throw 'Explicit unsigned preflight returned inconsistent signing metadata.' } Write-Host 'Windows signing preflight: 6 checks passed; no certificates or system settings were changed.' if ($SignedFixture) { if (-not $CertificateThumbprint) { throw 'Integration checks need a pinned fixture certificate thumbprint.' } $verify = Join-Path $PSScriptRoot 'verify-windows-signature.ps1' $verified = & $verify -Path $SignedFixture -CertificateThumbprint $CertificateThumbprint -SelfSigned if (-not $verified.integrityVerified -or -not $verified.timestampVerified -or -not $verified.selfSigned) { throw 'Signed fixture was not verified.' } Must-Reject { & $verify -Path $SignedFixture -CertificateThumbprint ('0' * 40) -SelfSigned } 'Unexpected signing certificate' Must-Reject { & $verify -Path $SignedFixture -CertificateThumbprint $CertificateThumbprint } 'certificate chain is invalid' $tempDir = Join-Path $PSScriptRoot "../target/signing-tests/tamper-$([guid]::NewGuid().ToString('N'))" New-Item -ItemType Directory -Path $tempDir | Out-Null $tampered = Join-Path $tempDir 'tampered.exe' try { $bytes = [IO.File]::ReadAllBytes([IO.Path]::GetFullPath($SignedFixture)) $pe = [BitConverter]::ToInt32($bytes, 0x3c) $section = $pe + 24 + [BitConverter]::ToUInt16($bytes, $pe + 20) $codeOffset = [BitConverter]::ToInt32($bytes, $section + 20) $bytes[$codeOffset + 32] = $bytes[$codeOffset + 32] -bxor 1 [IO.File]::WriteAllBytes([IO.Path]::GetFullPath($tampered), $bytes) Must-Reject { & $verify -Path $tampered -CertificateThumbprint $CertificateThumbprint -SelfSigned } 'PE file digest mismatch' } finally { Remove-Item -LiteralPath $tampered -ErrorAction SilentlyContinue Remove-Item -LiteralPath $tempDir } if (Test-Path -LiteralPath "Cert:\CurrentUser\Root\$CertificateThumbprint") { throw 'Project signing identity was imported into Root.' } if (Test-Path -LiteralPath "Cert:\CurrentUser\TrustedPublisher\$CertificateThumbprint") { throw 'Project identity was imported into TrustedPublisher.' } $identity = & (Join-Path $PSScriptRoot 'initialize-windows-signing.ps1') if ($identity.certificateThumbprint -ne $CertificateThumbprint) { throw 'Default signing identity changed between builds.' } Write-Host 'Self-signed integration: valid signature/timestamp, wrong identity, CA trust requirement, PE tampering, trust-store isolation and identity reuse passed.' if ($UntimestampedFixture) { Must-Reject { & $verify -Path $UntimestampedFixture -CertificateThumbprint $CertificateThumbprint -SelfSigned } 'Expected one RFC 3161 timestamp' Write-Host 'Missing RFC 3161 timestamp was rejected.' } }