接入自签名代码签名:主 exe、watchdog、安装包与卸载器统一签名

mygo.json 增加 windows.signCommand -> scripts/codesign/sign-file.ps1(PowerShell Set-AuthenticodeSignature,不依赖 Windows SDK)。新增 new-cert.ps1 生成 10 年期自签名证书、trust.ps1 导入当前用户 Root + TrustedPublisher(certutil 静默导入)。公钥 certs/ZeroOrigin-CodeSign.cer 进仓库,*.pfx 进 .gitignore。AGENTS.md / 架构文档 / README 同步签名流程与三条坑。
This commit is contained in:
zerorigin2019 committed 2026-11-16 11:06:28 +08:00
1 parent 2886a61d10
commit 37827eb8c7
9 files changed
+237 -10

No files matched your search

+57
View File
@@ -0,0 +1,57 @@
#Requires -Version 5.1
<#
生成自签名代码签名证书(仅用于本机与受控机器,不能消除别人的「未知发布者」提示)。
产物:
certs\ZeroOrigin-CodeSign.cer 公钥证书,可提交仓库,供其他机器导入信任
%APPDATA%\ZerOriginTool\codesign\*.pfx 含私钥的备份(绝不进仓库),重装系统后用它恢复
%APPDATA%\ZerOriginTool\codesign\password.txt pfx 密码
用法:powershell -NoProfile -ExecutionPolicy Bypass -File scripts\codesign\new-cert.ps1 [-Force]
#>
[CmdletBinding()]
param(
[string]$Subject = 'CN=ZeroOrigin ToolBox, O=ZeroOrigin, C=CN',
[int]$Years = 10,
[switch]$Force
)
$ErrorActionPreference = 'Stop'
$root = Split-Path -Parent (Split-Path -Parent $PSScriptRoot) # 仓库根
$cerDir = Join-Path $root 'certs'
$backupDir = Join-Path $env:APPDATA 'ZerOriginTool\codesign'
New-Item -ItemType Directory -Force -Path $cerDir, $backupDir | Out-Null
$existing = Get-ChildItem Cert:\CurrentUser\My -CodeSigningCert |
Where-Object { $_.Subject -eq $Subject -and $_.HasPrivateKey }
if ($existing -and -not $Force) {
"已存在同名证书:$($existing[0].Thumbprint)($($existing[0].NotAfter.ToString('yyyy-MM-dd')) 到期);要重建请加 -Force"
exit 0
}
$cert = New-SelfSignedCertificate `
-Type CodeSigningCert `
-Subject $Subject `
-KeyUsage DigitalSignature `
-KeyAlgorithm RSA `
-KeyLength 3072 `
-HashAlgorithm SHA256 `
-CertStoreLocation Cert:\CurrentUser\My `
-NotAfter (Get-Date).AddYears($Years)
$cerPath = Join-Path $cerDir 'ZeroOrigin-CodeSign.cer'
Export-Certificate -Cert $cert -FilePath $cerPath -Force | Out-Null
$password = -join ((48..57) + (65..90) + (97..122) | Get-Random -Count 24 | ForEach-Object { [char]$_ })
$secure = ConvertTo-SecureString -String $password -AsPlainText -Force
$pfxPath = Join-Path $backupDir 'ZeroOrigin-CodeSign.pfx'
Export-PfxCertificate -Cert $cert -FilePath $pfxPath -Password $secure | Out-Null
Set-Content -Path (Join-Path $backupDir 'password.txt') -Value $password -Encoding ASCII
"证书已生成"
" 主题 : $($cert.Subject)"
" 指纹 : $($cert.Thumbprint)"
" 到期 : $($cert.NotAfter.ToString('yyyy-MM-dd'))"
" 公钥证书 : $cerPath(可提交仓库,供其他机器导入)"
" pfx 备份 : $pfxPath(含私钥,绝不进仓库)"
" pfx 密码 : $password(同目录 password.txt 也有)"
+52
View File
@@ -0,0 +1,52 @@
#Requires -Version 5.1
<#
用本机自签名证书给一个 PE 文件做 Authenticode 签名。
mygo.json 的 windows.signCommand 指向本脚本:mygo build 会给主 exe、资源里的
watchdog.exe、安装包和卸载器逐个调用它(%1 是文件路径,已带引号)。
自签名证书没有 CA 时间戳服务,所以不加 RFC 3161 时间戳:证书到期后旧签名会失效,
到那时重新发证并重签即可(自用场景够)。
用法:powershell -NoProfile -ExecutionPolicy Bypass -File scripts\codesign\sign-file.ps1 <文件>
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $true, Position = 0)][string]$Path,
[string]$Subject = 'CN=ZeroOrigin ToolBox, O=ZeroOrigin, C=CN'
)
$ErrorActionPreference = 'Stop'
if (-not (Test-Path -LiteralPath $Path)) {
"签名失败:文件不存在 $Path"
exit 1
}
$cert = Get-ChildItem Cert:\CurrentUser\My -CodeSigningCert |
Where-Object { $_.Subject -eq $Subject -and $_.HasPrivateKey } |
Sort-Object NotAfter -Descending |
Select-Object -First 1
if (-not $cert) {
"签名失败:找不到证书「$Subject」,先运行 scripts\codesign\new-cert.ps1"
exit 1
}
$leaf = Split-Path -Leaf $Path
$existing = Get-AuthenticodeSignature -LiteralPath $Path
if ($existing.SignerCertificate) {
"已签名,跳过:$leaf"
exit 0
}
try {
$sig = Set-AuthenticodeSignature -LiteralPath $Path -Certificate $cert -HashAlgorithm SHA256
} catch {
"签名失败:$leaf — $($_.Exception.Message)"
exit 1
}
if (-not $sig.SignerCertificate) {
"签名失败:$leaf — $($sig.StatusMessage)"
exit 1
}
"已签名:$leaf($($sig.Status))"
exit 0
+50
View File
@@ -0,0 +1,50 @@
#Requires -Version 5.1
<#
把自签名代码签名证书导入当前用户的「受信任的根证书颁发机构」与「受信任的发布者」。
只影响当前用户、不需要管理员:导入后本机运行我们签名过的 exe 不再提示「未知发布者」。
换一台机器要再导一次;-Remove 可撤销。
用法:powershell -NoProfile -ExecutionPolicy Bypass -File scripts\codesign\trust.ps1 [-CerPath <证书>] [-Remove]
为什么用 certutil 而不是 Import-Certificate:后者向 Root 存储导入要求交互式 UI 确认,
在脚本里直接报 "UI is not allowed in this operation";certutil -user -addstore 是静默的。
#>
[CmdletBinding()]
param(
[string]$CerPath,
[switch]$Remove
)
$ErrorActionPreference = 'Stop'
if (-not $CerPath) {
$CerPath = Join-Path (Split-Path -Parent (Split-Path -Parent $PSScriptRoot)) 'certs\ZeroOrigin-CodeSign.cer'
}
if (-not (Test-Path -LiteralPath $CerPath)) {
"找不到证书文件:$CerPath"
exit 1
}
$full = (Resolve-Path -LiteralPath $CerPath).Path
$cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($full)
$thumb = $cert.Thumbprint
foreach ($store in 'Root', 'TrustedPublisher') {
if ($Remove) {
$proc = Start-Process certutil -ArgumentList @('-user', '-delstore', $store, $thumb) -PassThru -NoNewWindow -Wait
} else {
$proc = Start-Process certutil -ArgumentList @('-user', '-addstore', $store, $full) -PassThru -NoNewWindow -Wait
}
if ($proc.ExitCode -ne 0) {
"操作失败:$store(certutil 退出码 $($proc.ExitCode))"
exit 1
}
}
if ($Remove) {
"已移除:$($cert.Subject)($thumb)"
} else {
"已信任:$($cert.Subject)($thumb)"
"到期:$($cert.NotAfter.ToString('yyyy-MM-dd'))"
}
exit 0