接入自签名代码签名:主 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:
1 parent
2886a61d10
commit
37827eb8c7
9 files changed
+237
-10
No files matched your search
@@ -6,6 +6,9 @@ build/
|
||||
*.exe
|
||||
*.dll
|
||||
|
||||
# 代码签名私钥(公钥 certs/*.cer 可以提交,.pfx 绝不进仓库)
|
||||
*.pfx
|
||||
|
||||
# 本地配置与日志(运行时生成)
|
||||
config.json
|
||||
*.bak
|
||||
|
||||
@@ -43,7 +43,10 @@ Windows 轻量自动化工具箱:**单个 mygo 进程**(托盘常驻 + 原
|
||||
(含亮度 DDC/CI + gamma 兜底);
|
||||
`internal/assets` 托盘 PNG(go:embed);`internal/logging` slog;`internal/version` ldflags;
|
||||
- `resources/windows-amd64/` 随 exe 复制的平台资源(build.bat 编译出 watchdog.exe);
|
||||
`assets/` 图标源文件;`mygo.json` mygo 应用清单;`bin/` 产物(gitignore);`portable/` 便携版。
|
||||
`assets/` 图标源文件;`mygo.json` mygo 应用清单;`bin/` 产物(gitignore);`portable/` 便携版;
|
||||
- `scripts/` 发布与签名脚本:`publish-gitea.ps1`(上传 Gitea Release);`codesign/new-cert.ps1`
|
||||
(生成自签名代码签名证书)、`codesign/sign-file.ps1`(mygo 的 `signCommand` 调用)、
|
||||
`codesign/trust.ps1`(把公钥证书导入当前用户信任存储);`certs/` 只放公钥证书。
|
||||
|
||||
## 构建与验证
|
||||
|
||||
@@ -57,8 +60,17 @@ Windows 轻量自动化工具箱:**单个 mygo 进程**(托盘常驻 + 原
|
||||
`%APPDATA%\mygo\update-keys\mygo-update.key`(**绝不进仓库**,用户需自行备份;`build.bat` 会读进
|
||||
`MYGO_UPDATER_PRIVATE_KEY` 环境变量)。私钥缺失时 `go tool mygo build` 只构建、不产出更新包且不报错,
|
||||
容易误以为已经发版(构建日志里有「已加载更新签名私钥」才是有签名的构建);
|
||||
- 代码签名(Authenticode,自签名):`mygo.json` 的 `windows.signCommand` 指向
|
||||
`scripts/codesign/sign-file.ps1`,mygo 会依次签主 exe、资源里的 watchdog.exe、安装包与卸载器
|
||||
(`signWindowsResources` 只签未签名的 PE,所以 setup 里的卸载器也签上了)。证书由
|
||||
`scripts/codesign/new-cert.ps1` 生成(10 年,`CN=ZeroOrigin ToolBox`),公钥
|
||||
`certs/ZeroOrigin-CodeSign.cer` 进仓库、私钥备份在 `%APPDATA%\ZerOriginTool\codesign\`
|
||||
(**绝不进仓库**,`.gitignore` 已忽略 `*.pfx`);新机器跑 `scripts/codesign/trust.ps1` 导入
|
||||
当前用户的 Root + TrustedPublisher。自签名只让**导入过根证书的机器**不再提示「未知发布者」,
|
||||
对外分发无效(别人仍提示,SmartScreen 甚至更谨慎);
|
||||
- 发版顺序:改 `mygo.json` 的 `version` → 在 `CHANGELOG.md` 加同名 `## <version>` 小节(缺失时 mygo 报
|
||||
no notes)→ `build.bat quick` → `scripts/publish-gitea.ps1 -Owner zerorigin -Repo zerorigin-toolbox`;
|
||||
no notes)→ `build.bat`(要安装版必须完整构建,`quick` 会跳过 setup)→
|
||||
`scripts/publish-gitea.ps1 -Owner zerorigin -Repo zerorigin-toolbox`;
|
||||
- 发布脚本为什么自己写:`go tool mygo build -upload` 只支持 GitHub Release(依赖 gh CLI)或 S3 桶,
|
||||
自建 Gitea 没有内置支持。脚本用 Gitea REST API 把归档、delta、SHA256SUMS、安装版 setup 与便携版 zip
|
||||
传到**固定的 `latest` Release**(新用户一个页面拿全部形态),同名 manifest **先删旧附件再传、
|
||||
@@ -201,8 +213,8 @@ Windows 轻量自动化工具箱:**单个 mygo 进程**(托盘常驻 + 原
|
||||
- 自更新有三条硬前提,缺一条就**静默不做后台检查**:`updates.publicKey` 非空、构建时注入了 feed
|
||||
(`updates.github` 或 `updates.url`,二者互斥)、`!IsDev()`,并且 exe 所在目录可写(`mygo.Updater.Enabled()`);
|
||||
- 发布源是自建 Gitea:`updates.url` = `https://git.zerorigin.cn/zerorigin/zerorigin-toolbox/releases/download/latest`
|
||||
(2026-11-16 实测:TLS 正常、匿名可下载、单附件上限 100MB、`allowed_types` 含 `.gz`/`.json` 但**不含
|
||||
`.delta`**,所以 delta 上传会被拒、脚本直接跳过)。`updates.github` 天然写死 github.com
|
||||
(2026-11-16 实测:TLS 正常、匿名可下载、单附件上限 100MB;`allowed_types` 白名单只约束 issue 类附件,
|
||||
Release 附件连 `.exe` 与 `.delta` 都接受)。`updates.github` 天然写死 github.com
|
||||
(`cmd/mygo/updates.go:126`),国内网络常连接超时(实测 `dial tcp 20.6.7.17:443`),不要换回去;
|
||||
- 固定 tag `latest` 是 URL 的一部分:**改名等于换渠道**,已发出去的 exe 不会跟过来;Release `latest`
|
||||
里的附件不能删,删了客户端就再也检查不到更新;
|
||||
@@ -222,8 +234,19 @@ Windows 轻量自动化工具箱:**单个 mygo 进程**(托盘常驻 + 原
|
||||
- `build.bat` 读 `mygo.json` 的版本号必须 `Get-Content -Raw -Encoding UTF8`:不加会按 GBK 读中文,
|
||||
`ConvertFrom-Json` 报 `Invalid object passed in, ':' or '}' expected`,版本号取空 → 便携版 zip
|
||||
被命名成 `0.0.0`。zip 名对不上发布脚本的过滤器就传不上去;
|
||||
- Gitea 的 `allowed_types` 白名单只影响 issue 之类附件,**Release 附件实测接受 `.exe`**
|
||||
(setup 安装包直接上传成功),不需要再套一层 zip;`.delta` 会被拒,脚本跳过它不影响更新;
|
||||
- Gitea 的 `allowed_types` 白名单只影响 issue 之类附件,**Release 附件实测接受 `.exe` 与 `.delta`**
|
||||
(setup 安装包、增量包都直接上传成功),不需要再套一层 zip;脚本里保留「delta 失败只警告」的容错;
|
||||
- 自签名证书**不能用 mygo 的 `windows.certificate` 路径**:它固定给 signtool 传
|
||||
`/tr http://timestamp.digicert.com`(`cmd/mygo/winsign.go:79-82`,空字符串也会被填成该默认值),
|
||||
DigiCert 不给自签名证书打时间戳,signtool 报错、整个构建失败。自签名必须走 `windows.signCommand`
|
||||
且不带 `/tr`;代价是证书 2036 年到期后旧签名失效,重新发证重签即可;
|
||||
- 往 `Cert:\CurrentUser\Root` 导入证书**不要用 `Import-Certificate`**:它要交互式 UI 确认,脚本里
|
||||
报 `UI is not allowed in this operation`,还会卡住等对话框;用 `certutil -user -addstore Root <cer>`
|
||||
(静默,见 `scripts/codesign/trust.ps1`);
|
||||
- 本机没有 Windows SDK 与 signtool:签名用 PowerShell 内置的 `Set-AuthenticodeSignature`(.NET)即可,
|
||||
不要为签名去装几 GB 的 SDK;签名前必须先停掉正在跑的 exe,否则报「文件正被另一个进程使用」;
|
||||
- 签名会改变 exe 内容:已发布的旧包与本地新包即使版本号相同也不一致,改签名后必须重新构建、
|
||||
重新发布(delta 由 mygo 重新生成,不影响更新链路);
|
||||
- vet 对 crashguard.go / win32.go 的 unsafe.Pointer 警告是有意为之,不要"修复"。
|
||||
|
||||
## 风格
|
||||
|
||||
@@ -20,7 +20,8 @@ Windows 轻量自动化工具箱:**单个 mygo 进程**(托盘常驻 + 原
|
||||
|
||||
1. 改 `mygo.json` 的 `version`;
|
||||
2. 在 `CHANGELOG.md` 里加 `## <版本号>` 段(mygo 用它做更新窗口的发布说明);
|
||||
3. `build.bat quick`(需要 `%APPDATA%\mygo\update-keys\mygo-update.key` 才会签名并产出更新包);
|
||||
3. `build.bat`(完整构建才产出安装版;`quick` 会跳过 setup。需要
|
||||
`%APPDATA%\mygo\update-keys\mygo-update.key` 才会签名并产出更新包);
|
||||
4. 发布:
|
||||
|
||||
```powershell
|
||||
@@ -29,3 +30,20 @@ Windows 轻量自动化工具箱:**单个 mygo 进程**(托盘常驻 + 原
|
||||
```
|
||||
|
||||
脚本会先传归档与 delta,最后传 manifest,并先删除同名的旧 manifest 附件。
|
||||
|
||||
## 代码签名(自签名)
|
||||
|
||||
构建产物(`ZerOriginTool.exe`、`watchdog.exe`、安装包与卸载器)都带 Authenticode 签名,
|
||||
用于消除本机的「发布商:未知发布者」提示:
|
||||
|
||||
- 公钥证书:[`certs/ZeroOrigin-CodeSign.cer`](certs/ZeroOrigin-CodeSign.cer);
|
||||
- 新机器先导入信任(当前用户,无需管理员):
|
||||
|
||||
```powershell
|
||||
powershell -NoProfile -ExecutionPolicy Bypass -File scripts\codesign\trust.ps1
|
||||
```
|
||||
|
||||
- 私钥备份在 `%APPDATA%\ZerOriginTool\codesign\`(**不进仓库**,换机/重装前请备份)。
|
||||
|
||||
自签名只对**导入过这张根证书的机器**有效;对外分发仍会提示未知发布者,要彻底解决需要
|
||||
CA 签发的代码签名证书。
|
||||
Binary file not shown.
@@ -360,12 +360,35 @@ manifest 最后传;只改文案不重传附件用 `-MetadataOnly`。
|
||||
(程序内托盘「检查更新…」自动使用,不用手动下载)。便携版 zip 的校验和由 `build.bat` 追加进
|
||||
`bin/SHA256SUMS`。
|
||||
|
||||
### 9.2 代码签名(Authenticode)
|
||||
|
||||
没有代码签名时,用户双击安装包会看到「无法验证发布者 / 发布商:未知发布者」。彻底消除它需要
|
||||
CA 签发的代码签名证书(OV/EV);自建/自用场景可以先用**自签名证书**,只对导入过该根证书的机器生效。
|
||||
|
||||
当前方案(自签名,2026-11 起):
|
||||
|
||||
- 证书:`scripts/codesign/new-cert.ps1` 生成 10 年期自签名代码签名证书(`CN=ZeroOrigin ToolBox`,
|
||||
EKU=Code Signing),公钥 `certs/ZeroOrigin-CodeSign.cer` 进仓库,私钥导出到
|
||||
`%APPDATA%\ZerOriginTool\codesign\`(`.gitignore` 忽略 `*.pfx`,**绝不进仓库**);
|
||||
- 签名:`mygo.json` 的 `windows.signCommand` 指向 `scripts/codesign/sign-file.ps1`,mygo 构建时
|
||||
依次签主 exe、资源里的 watchdog.exe、安装包与卸载器;
|
||||
- 信任:新机器跑 `scripts/codesign/trust.ps1`,用 `certutil -user -addstore` 把公钥证书写进
|
||||
当前用户的「受信任的根证书颁发机构」与「受信任的发布者」,之后本机双击不再提示未知发布者。
|
||||
|
||||
为什么不用 mygo 的 `windows.certificate`:那条路径固定给 signtool 传 DigiCert 时间戳参数,
|
||||
自签名证书拿不到时间戳,签名直接失败;`signCommand` 走 PowerShell 内置的
|
||||
`Set-AuthenticodeSignature`,不需要 Windows SDK。代价是没有 RFC 3161 时间戳,证书到期后
|
||||
旧包的签名会失效——自用场景可接受,重新发证重签即可。
|
||||
|
||||
自签名不改变对外分发的现实:别人仍然提示「未知发布者」,SmartScreen 甚至更谨慎;要真正消除,
|
||||
需要购买 OV/EV 证书(EV 通过硬件令牌或云签名服务,mygo 用 `windows.signCommand` 同样能接)。
|
||||
|
||||
**发布渠道**:`updates.url` 指向自建 Gitea
|
||||
`https://git.zerorigin.cn/zerorigin/zerorigin-toolbox/releases/download/latest`。
|
||||
`go tool mygo build -upload` 用不上(只支持 GitHub Release 的 gh CLI 或 S3 桶),所以有发布脚本。
|
||||
固定 tag `latest` 是 URL 的一部分,改名等于换渠道;Release 里的附件不能删,删了客户端就检查不到更新。
|
||||
Gitea 单附件上限 100MB、Release 附件实测接受 `.exe`(setup 直接上传,不必再套 zip);
|
||||
`.delta` 会被附件白名单拒掉,脚本跳过即可(客户端退回整包下载)(2026-11-16 实测)。
|
||||
Gitea 单附件上限 100MB;`allowed_types` 白名单只约束 issue 类附件,Release 附件实测连
|
||||
`.exe` 与 `.delta` 都接受(setup 直接上传,不必再套 zip)(2026-11-16 实测)。
|
||||
脚本写 Release 标题/正文必须自己编成 UTF-8 字节发送:PowerShell 5.1 的 `Invoke-RestMethod`
|
||||
传字符串 body 时按 ISO-8859-1 编码,中文全变成 `?`。
|
||||
`updates.github` 天然写死 github.com,国内常连接超时(`dial tcp 20.6.7.17:443`),不要换回去。
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
},
|
||||
"windows": {
|
||||
"displayName": "零源工具箱",
|
||||
"installDirName": "ZerOriginTool"
|
||||
"installDirName": "ZerOriginTool",
|
||||
"signCommand": "powershell -NoProfile -ExecutionPolicy Bypass -File scripts\\codesign\\sign-file.ps1 %1"
|
||||
}
|
||||
}
|
||||
@@ -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 也有)"
|
||||
@@ -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
|
||||
@@ -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
|
||||
Reference in new issue
Block a user