Files
xieyi-weixin-/sign_artifact.ps1
T
2026-09-21 10:56:48 +08:00

74 lines
2.6 KiB
PowerShell
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
param(
[Parameter(Mandatory = $true)][string]$Path
)
# 给构建产物加 Authenticode 签名。
#
# 签名用于验证发布者和文件完整性;它不证明程序无风险,不能消除行为检测。
# 未知发布者/下载信誉提示与 Defender 病毒或行为告警必须分别调查。使用已有证书时:
# - 证书在系统证书库里: 设 WECOM_CODESIGN_THUMBPRINT=<证书指纹>
# - 证书是 PFX 文件: 设 WECOM_CODESIGN_PFX=<pfx 路径>,
# 密码放 WECOM_CODESIGN_PFX_PASSWORD
# 两者都没设置时本脚本直接跳过,不影响构建。
$ErrorActionPreference = "Stop"
if (-not (Test-Path -LiteralPath $Path)) {
throw ("Cannot sign, file not found: " + $Path)
}
$thumbprint = ([string]$env:WECOM_CODESIGN_THUMBPRINT).Trim()
$pfxPath = ([string]$env:WECOM_CODESIGN_PFX).Trim()
if (-not $thumbprint -and -not $pfxPath) {
Write-Output ("Code signing skipped (no certificate configured): " + [System.IO.Path]::GetFileName($Path))
exit 0
}
function Find-SignTool {
$fromPath = Get-Command signtool.exe -ErrorAction SilentlyContinue
if ($fromPath) { return $fromPath.Source }
$kitRoots = @(
"${env:ProgramFiles(x86)}\Windows Kits\10\bin",
"$env:ProgramFiles\Windows Kits\10\bin"
)
foreach ($root in $kitRoots) {
if (-not (Test-Path -LiteralPath $root)) { continue }
$candidate = Get-ChildItem -Path $root -Recurse -Filter signtool.exe -ErrorAction SilentlyContinue |
Where-Object { $_.FullName -match "\\x64\\" } |
Sort-Object FullName -Descending |
Select-Object -First 1
if ($candidate) { return $candidate.FullName }
}
return $null
}
$signtool = Find-SignTool
if (-not $signtool) {
throw "Signing certificate is configured but signtool.exe was not found. Install the Windows 10/11 SDK (Signing Tools component)."
}
$timestampUrl = ([string]$env:WECOM_CODESIGN_TIMESTAMP_URL).Trim()
if (-not $timestampUrl) { $timestampUrl = "http://timestamp.digicert.com" }
$arguments = @("sign", "/fd", "SHA256", "/td", "SHA256", "/tr", $timestampUrl)
if ($thumbprint) {
$arguments += @("/sha1", $thumbprint)
}
else {
$arguments += @("/f", $pfxPath)
$pfxPassword = ([string]$env:WECOM_CODESIGN_PFX_PASSWORD).Trim()
if ($pfxPassword) { $arguments += @("/p", $pfxPassword) }
}
$arguments += $Path
& $signtool @arguments
if ($LASTEXITCODE -ne 0) {
throw ("Code signing failed: " + $Path)
}
& $signtool verify /pa $Path
if ($LASTEXITCODE -ne 0) {
throw ("Signature verification failed: " + $Path)
}
Write-Output ("Signed: " + $Path)