Files
simba-rmic-2026/client/scripts/build-windows-release.ps1
T
sloray aea1cf60ae
构建客户端 / 检查(类型 + 单元测试) (push) Canceled after 0s
构建客户端 / Linux x64(deb + AppImage) (push) Canceled after 0s
构建客户端 / Windows x64(NSIS 安装包) (push) Canceled after 0s
构建客户端 / macOS 通用(.app + .dmg) (push) Canceled after 0s
fix(build): 默认复用自签名证书签署 Windows 发布包
- fix(signing): 独立校验文件摘要、CMS 签名与 RFC 3161 时间戳,不修改系统信任库

- build(windows): 发布脚本和 CI 复用本机签名身份,交付公钥证书与真实信任状态
2026-10-31 13:22:25 +08:00

145 lines
9.0 KiB
PowerShell

#requires -Version 7.4
# Build with Tauri's native Authenticode signing support. No private keys or
# personal signing identities are stored in the shared Tauri configuration.
[CmdletBinding()]
param(
[string]$CertificateThumbprint = $env:RMIC_WINDOWS_CERT_THUMBPRINT,
[string]$TimestampUrl = 'http://timestamp.digicert.com',
[switch]$AllowUnsigned,
[switch]$PreflightOnly,
[string]$SigningConfigPath = $env:RMIC_WINDOWS_SIGNING_CONFIG,
[string]$OutputDirectory
)
$ErrorActionPreference = 'Stop'
Set-StrictMode -Version Latest
$clientRoot = [IO.Path]::GetFullPath((Join-Path $PSScriptRoot '..'))
$repoRoot = Split-Path -Parent $clientRoot
if (-not $SigningConfigPath) { $SigningConfigPath = Join-Path $env:LOCALAPPDATA 'RMIC-2026/signing/windows.json' }
if (-not $CertificateThumbprint -and -not $AllowUnsigned) {
if ($PreflightOnly -and -not (Test-Path -LiteralPath $SigningConfigPath)) {
throw 'A code-signing certificate is required. Initialize the local signing identity before a read-only preflight.'
}
$identity = & (Join-Path $PSScriptRoot 'initialize-windows-signing.ps1') -ConfigPath $SigningConfigPath
$CertificateThumbprint = $identity.certificateThumbprint
}
$thumbprint = ($CertificateThumbprint -replace '\s', '').ToUpperInvariant()
$signed = $thumbprint.Length -gt 0
$selfSigned = $false
if ($signed) {
if ($thumbprint -notmatch '^[0-9A-F]{40}$') { throw 'Invalid code-signing certificate thumbprint.' }
$certificate = Get-Item -LiteralPath "Cert:\CurrentUser\My\$thumbprint" -ErrorAction SilentlyContinue
if (-not $certificate) { throw 'Code-signing certificate was not found in CurrentUser/My.' }
$now = Get-Date
if (-not $certificate.HasPrivateKey -or $certificate.NotBefore -gt $now -or $certificate.NotAfter -le $now) {
throw 'The signing certificate needs an accessible private key and must be currently valid.'
}
$selfSigned = [Convert]::ToBase64String($certificate.SubjectName.RawData) -eq [Convert]::ToBase64String($certificate.IssuerName.RawData)
$usageOids = @($certificate.Extensions |
Where-Object { $_ -is [Security.Cryptography.X509Certificates.X509EnhancedKeyUsageExtension] } |
ForEach-Object { $_.EnhancedKeyUsages } | ForEach-Object { $_.Value })
if ('1.3.6.1.5.5.7.3.3' -notin $usageOids) {
throw 'The certificate is not authorized for code signing.'
}
} elseif (-not $AllowUnsigned) {
throw 'A code-signing certificate is required. Set RMIC_WINDOWS_CERT_THUMBPRINT or explicitly use -AllowUnsigned for a test package.'
}
$timestamp = $null
if (-not [Uri]::TryCreate($TimestampUrl, [UriKind]::Absolute, [ref]$timestamp) -or
$timestamp.Scheme -notin @('http', 'https') -or -not $timestamp.Host -or $timestamp.UserInfo) {
throw 'Timestamp URL must be an HTTP(S) server without credentials.'
}
if ($PreflightOnly) {
[pscustomobject]@{ signing = $signed; selfSigned = $selfSigned; certificateThumbprint = $thumbprint;
unsignedExplicitlyAllowed = [bool]$AllowUnsigned; digestAlgorithm = $(if ($signed) { 'sha256' } else { $null }); tsp = $signed }
return
}
& (Join-Path $PSScriptRoot 'prepare-windows-bundle.ps1') -CheckProcessOnly
& node (Join-Path $repoRoot 'scripts/version.mjs') --check
if ($LASTEXITCODE -ne 0) { throw 'Regenerate versions before packaging.' }
$version = Get-Content -LiteralPath (Join-Path $repoRoot 'version.json') -Raw | ConvertFrom-Json
$label = "v$($version.version)+$($version.build)"
if (-not $OutputDirectory) { $OutputDirectory = Join-Path $repoRoot "target/releases/$label" }
$OutputDirectory = [IO.Path]::GetFullPath($OutputDirectory)
New-Item -ItemType Directory -Force -Path $OutputDirectory | Out-Null
$temporaryConfig = Join-Path $OutputDirectory 'windows-signing.local.json'
$windows = if ($signed) {
@{ certificateThumbprint = $thumbprint; digestAlgorithm = 'sha256'; timestampUrl = $TimestampUrl; tsp = $true }
} else {
@{ certificateThumbprint = $null; digestAlgorithm = $null; timestampUrl = $null; tsp = $false; signCommand = $null }
}
$utf8 = New-Object Text.UTF8Encoding($false)
[IO.File]::WriteAllText($temporaryConfig, (@{ bundle = @{ windows = $windows } } | ConvertTo-Json -Depth 6), $utf8)
$previousTarget = $env:CARGO_TARGET_DIR
$previousToolchain = $env:RUSTUP_TOOLCHAIN
try {
Remove-Item Env:CARGO_TARGET_DIR -ErrorAction SilentlyContinue
$env:RUSTUP_TOOLCHAIN = 'stable'
Push-Location $clientRoot
try {
& pnpm tauri build --target x86_64-pc-windows-msvc --bundles nsis --ci --config $temporaryConfig -- --locked --config 'unstable.build-std=[]'
if ($LASTEXITCODE -ne 0) { throw 'Windows build or signing failed; no verified installer was delivered.' }
& (Join-Path $PSScriptRoot 'prepare-windows-bundle.ps1') -CheckOnly
$metadata = (& cargo +stable metadata --no-deps --format-version 1 --manifest-path src-tauri/Cargo.toml | ConvertFrom-Json)
if ($LASTEXITCODE -ne 0) { throw 'Cannot resolve Cargo output path.' }
} finally { Pop-Location }
$release = Join-Path $metadata.target_directory 'x86_64-pc-windows-msvc/release'
$main = Join-Path $release 'cam-desktop.exe'
$installer = Join-Path $release "bundle/nsis/RMIC-2026_$($version.version)_x64-setup.exe"
if ($signed) {
# Tauri signs the bundled copy, then can restore the original standalone
# executable. Sign that final output too, using the same identity.
$signTool = Get-Command signtool.exe -ErrorAction SilentlyContinue
$signToolPath = if ($signTool) { $signTool.Source } else { $null }
if (-not $signToolPath) {
$sdkRoot = (Get-ItemProperty -LiteralPath 'HKLM:\SOFTWARE\Microsoft\Windows Kits\Installed Roots' -Name KitsRoot10).KitsRoot10
$sdkVersions = Get-ChildItem -LiteralPath (Join-Path $sdkRoot 'bin') -Directory |
Where-Object Name -Match '^\d+\.\d+\.\d+\.\d+$' | Sort-Object { [version]$_.Name } -Descending
foreach ($sdk in $sdkVersions) {
$candidate = Join-Path $sdk.FullName 'x64/signtool.exe'
if (Test-Path -LiteralPath $candidate) { $signToolPath = $candidate; break }
}
}
if (-not $signToolPath) { throw 'Windows SDK signtool.exe is required to sign the final standalone executable.' }
& $signToolPath sign /sha1 $thumbprint /s My /fd SHA256 /tr $TimestampUrl /td SHA256 /d RMIC-2026 $main
if ($LASTEXITCODE -ne 0) { throw 'Signing the final standalone executable failed.' }
}
$signatures = @()
foreach ($file in @($main, $installer)) {
$signature = Get-AuthenticodeSignature -LiteralPath $file
if ($signed) {
$signatures += & (Join-Path $PSScriptRoot 'verify-windows-signature.ps1') -Path $file -CertificateThumbprint $thumbprint -SelfSigned:$selfSigned
} else {
if ($signature.Status -ne 'NotSigned') { throw "Unexpected signature on explicitly unsigned output: $file" }
$signatures += @{ file = [IO.Path]::GetFileName($file); status = [string]$signature.Status; timestamped = $false }
}
}
$suffix = if ($selfSigned) { '-self-signed' } elseif ($signed) { '' } else { '-unsigned' }
$delivery = Join-Path $OutputDirectory "RMIC-2026_${label}_windows-x64${suffix}-setup.exe"
Copy-Item -LiteralPath $installer -Destination $delivery
& icacls.exe $delivery /setintegritylevel M | Out-Null
if ($LASTEXITCODE -ne 0) { throw 'Cannot set installer Medium integrity label.' }
$hash = (Get-FileHash -LiteralPath $delivery -Algorithm SHA256).Hash.ToLowerInvariant()
[IO.File]::WriteAllText("$delivery.sha256", "$hash $([IO.Path]::GetFileName($delivery))`n", $utf8)
$publicCertificate = $null
if ($signed) {
$certPath = Join-Path $OutputDirectory 'RMIC-2026-codesign.cer'
[IO.File]::WriteAllBytes($certPath, $certificate.Export([Security.Cryptography.X509Certificates.X509ContentType]::Cert))
$publicCertificate = @{ file = 'RMIC-2026-codesign.cer'; subject = $certificate.Subject;
expiresUtc = $certificate.NotAfter.ToUniversalTime().ToString('o');
sha256 = (Get-FileHash -LiteralPath $certPath -Algorithm SHA256).Hash.ToLowerInvariant() }
}
$report = @{ version = $label; installer = $delivery; sha256 = $hash; signed = $signed;
selfSigned = $selfSigned; certificateThumbprint = $thumbprint;
publicCertificate = $publicCertificate;
signatures = $signatures; smartScreenReputationVerified = $false }
[IO.File]::WriteAllText((Join-Path $OutputDirectory 'windows-signature-verification.json'), ($report | ConvertTo-Json -Depth 6), $utf8)
$report | ConvertTo-Json -Depth 6
} finally {
# This config contains a personal public certificate identity, and is local only.
Remove-Item -LiteralPath $temporaryConfig -ErrorAction SilentlyContinue
if ($null -eq $previousTarget) { Remove-Item Env:CARGO_TARGET_DIR -ErrorAction SilentlyContinue } else { $env:CARGO_TARGET_DIR = $previousTarget }
if ($null -eq $previousToolchain) { Remove-Item Env:RUSTUP_TOOLCHAIN -ErrorAction SilentlyContinue } else { $env:RUSTUP_TOOLCHAIN = $previousToolchain }
}