fix(build): 默认复用自签名证书签署 Windows 发布包
- fix(signing): 独立校验文件摘要、CMS 签名与 RFC 3161 时间戳,不修改系统信任库 - build(windows): 发布脚本和 CI 复用本机签名身份,交付公钥证书与真实信任状态
This commit is contained in:
14 files changed
+323
-39
No files matched your search
@@ -1,5 +1,5 @@
|
||||
[CmdletBinding()]
|
||||
param()
|
||||
param([string]$SignedFixture, [string]$CertificateThumbprint, [string]$UntimestampedFixture)
|
||||
$ErrorActionPreference = 'Stop'
|
||||
Set-StrictMode -Version Latest
|
||||
$build = Join-Path $PSScriptRoot 'build-windows-release.ps1'
|
||||
@@ -11,13 +11,45 @@ function Must-Reject([scriptblock]$Action, [string]$Expected) {
|
||||
}
|
||||
if (-not $rejected) { throw 'Signing preflight accepted an invalid configuration.' }
|
||||
}
|
||||
Must-Reject { & $build -CertificateThumbprint '' -PreflightOnly } 'certificate is required'
|
||||
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 -ne 'sha256' -or -not $result.tsp) {
|
||||
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.'
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user