66 lines
4.0 KiB
PowerShell
66 lines
4.0 KiB
PowerShell
# Checks custos-sign against the real tools (docs/SIGNING.md, mandatory test 1):
|
|
# minisign -V on a minisign signature and on a signed SHA256SUMS;
|
|
# signtool /dg -> custos-sign -> signtool /di on an exe, then osslsigncode verify against the test root,
|
|
# and signtool verify /pa (which passes only where the test root is trusted; it is not installed by this script).
|
|
# Needs: Windows SDK signtool; WSL Debian with Nix (minisign, osslsigncode come from nixpkgs).
|
|
# Usage: powershell -File tools/signing/interop.ps1 [-Exe <unsigned exe to sign a copy of>]
|
|
param([string]$Exe = "")
|
|
# Exit codes decide; PowerShell 5.1 would turn a tool's expected stderr into a terminating error under 'Stop'.
|
|
$ErrorActionPreference = 'Continue'
|
|
$root = Split-Path -Parent (Split-Path -Parent $PSScriptRoot)
|
|
$work = Join-Path ([System.IO.Path]::GetTempPath()) ("custos-sign-interop-" + [guid]::NewGuid())
|
|
New-Item -ItemType Directory $work | Out-Null
|
|
$signtool = Get-ChildItem "${env:ProgramFiles(x86)}\Windows Kits\10\bin\*\x64\signtool.exe" | Sort-Object FullName | Select-Object -Last 1
|
|
if (-not $signtool) { throw "signtool not found (Windows SDK)" }
|
|
$failed = 0
|
|
function Check($name, $ok) { if ($ok) { "PASS $name" } else { "FAIL $name"; $script:failed++ } }
|
|
function InWsl($cmd) {
|
|
$w = (wsl -d Debian --exec wslpath -a ($work -replace '\\', '/')).Trim()
|
|
wsl -d Debian --exec bash -lc "cd '$w' && $cmd"
|
|
}
|
|
try {
|
|
Push-Location $root
|
|
cargo build -q --release -p custos-sign --example interop
|
|
if ($LASTEXITCODE -ne 0) { throw "cargo build failed" }
|
|
$tool = Join-Path $root "target\release\examples\interop.exe"
|
|
Pop-Location
|
|
|
|
# minisign
|
|
Set-Content -NoNewline -Encoding ascii (Join-Path $work "payload.bin") "Domi interop payload"
|
|
& $tool minisign $work (Join-Path $work "payload.bin")
|
|
InWsl "nix shell nixpkgs#minisign -c minisign -Vm payload.bin -p minisign.pub" | Out-Host
|
|
Check "minisign -V accepts a custos-sign signature" ($LASTEXITCODE -eq 0)
|
|
InWsl "printf 'tampered' >> payload.bin; nix shell nixpkgs#minisign -c minisign -Vm payload.bin -p minisign.pub" 2>$null | Out-Null
|
|
Check "minisign -V rejects it on a changed file" ($LASTEXITCODE -ne 0)
|
|
|
|
$sums = "{0} a.exe`n{1} b.exe`n" -f ('a' * 64), ('b' * 64)
|
|
[System.IO.File]::WriteAllText((Join-Path $work "SHA256SUMS"), $sums)
|
|
& $tool manifest $work (Join-Path $work "SHA256SUMS")
|
|
InWsl "nix shell nixpkgs#minisign -c minisign -Vm SHA256SUMS -p minisign.pub" | Out-Host
|
|
Check "minisign -V accepts a signed SHA256SUMS" ($LASTEXITCODE -eq 0)
|
|
|
|
# Authenticode through signtool's digest signing
|
|
$ca = Join-Path $work "ca"
|
|
& $tool ca $ca
|
|
if (-not $Exe) { $Exe = (Get-Command cargo).Source }
|
|
$target = Join-Path $work "app.exe"
|
|
Copy-Item $Exe $target
|
|
$dg = Join-Path $work "dg"
|
|
New-Item -ItemType Directory $dg | Out-Null
|
|
& $signtool.FullName sign /dg $dg /fd sha256 /f (Join-Path $ca "leaf.cer") $target | Out-Host
|
|
Check "signtool /dg wrote the digest" (Test-Path (Join-Path $dg "app.exe.dig"))
|
|
& $tool authenticode (Join-Path $ca "leaf.key.der") (Join-Path $dg "app.exe.dig")
|
|
& $signtool.FullName sign /di $dg $target | Out-Host
|
|
Check "signtool /di took the signed digest" ($LASTEXITCODE -eq 0)
|
|
InWsl "nix shell nixpkgs#osslsigncode -c osslsigncode verify -CAfile ca/root.pem -in app.exe" | Out-Host
|
|
Check "osslsigncode verifies the signature to the test root" ($LASTEXITCODE -eq 0)
|
|
$v = & $signtool.FullName verify /pa $target 2>&1 | Out-String
|
|
$trusted = $LASTEXITCODE -eq 0
|
|
if ($trusted) { "PASS signtool verify /pa (the test root is trusted here)" }
|
|
elseif ($v -match "not trusted by the trust provider|could not be built to a trusted root") { "NOTE signtool verify /pa: the signature is whole; the test root is not trusted on this machine" }
|
|
else { Check "signtool verify /pa" $false; $v }
|
|
} finally {
|
|
Remove-Item -Recurse -Force $work -ErrorAction SilentlyContinue
|
|
}
|
|
if ($failed) { "$failed check(s) failed"; exit 1 } else { "all checks passed" }
|