接入 Windows 云签名并增加独立验签任务
正式构建通过 eSigner 签名应用、后端、代理内核、卸载器和安装器,并验证可信证书及时间戳。新增仅 main 手动触发的临时可执行文件验签任务,保留现有显式发版入口。 验证:45 项相关 Node 检查、PowerShell 签名失败保护和 actionlint 通过。Windows 真实签名与信任检查由独立任务执行。
正式构建通过 eSigner 签名应用、后端、代理内核、卸载器和安装器,并验证可信证书及时间戳。新增仅 main 手动触发的临时可执行文件验签任务,保留现有显式发版入口。 验证:45 项相关 Node 检查、PowerShell 签名失败保护和 actionlint 通过。Windows 真实签名与信任检查由独立任务执行。
| @@ -23,10 +23,15 @@ jobs: | ||
| 23 | 23 | - uses: actions/setup-go@v5 |
| 24 | 24 | with: |
| 25 | 25 | go-version-file: go.mod |
| 26 | 新增: - uses: actions/setup-java@v4 | |
| 27 | 新增: with: | |
| 28 | 新增: distribution: temurin | |
| 29 | 新增: java-version: '21' | |
| 26 | 30 | - name: Version consistency and release safety tests |
| 27 | 31 | run: | |
| 28 | 32 | node scripts/check-version.mjs |
| 29 | 删除: node --test scripts/release-version.test.mjs scripts/release-control.test.mjs scripts/prepare-release-assets.test.mjs | |
| 33 | 新增: node --test scripts/release-version.test.mjs scripts/release-control.test.mjs scripts/prepare-release-assets.test.mjs scripts/esigner.test.mjs | |
| 34 | 新增: pwsh -NoProfile -File scripts/test-windows-signing.ps1 | |
| 30 | 35 | - name: Validate workflow syntax |
| 31 | 36 | run: go run github.com/rhysd/actionlint/cmd/[email protected] -color |
| 32 | 37 | macos: |
| @@ -27,6 +27,10 @@ jobs: | ||
| 27 | 27 | - uses: actions/setup-node@v4 |
| 28 | 28 | with: |
| 29 | 29 | node-version: 24 |
| 30 | 新增: - uses: actions/setup-java@v4 | |
| 31 | 新增: with: | |
| 32 | 新增: distribution: temurin | |
| 33 | 新增: java-version: '21' | |
| 30 | 34 | - name: Check release configuration before creating a candidate |
| 31 | 35 | env: |
| 32 | 36 | CAGE_MACOS_P12_BASE64: ${{ secrets.CAGE_MACOS_P12_BASE64 }} |
| @@ -35,6 +39,12 @@ jobs: | ||
| 35 | 39 | CAGE_MACOS_NOTARY_KEY_BASE64: ${{ secrets.CAGE_MACOS_NOTARY_KEY_BASE64 }} |
| 36 | 40 | CAGE_MACOS_NOTARY_KEY_ID: ${{ secrets.CAGE_MACOS_NOTARY_KEY_ID }} |
| 37 | 41 | CAGE_MACOS_NOTARY_ISSUER_ID: ${{ secrets.CAGE_MACOS_NOTARY_ISSUER_ID }} |
| 42 | 新增: CAGE_WINDOWS_SIGNING_PROVIDER: ${{ vars.CAGE_WINDOWS_SIGNING_PROVIDER }} | |
| 43 | 新增: CAGE_ESIGNER_USERNAME: ${{ secrets.CAGE_ESIGNER_USERNAME }} | |
| 44 | 新增: CAGE_ESIGNER_PASSWORD: ${{ secrets.CAGE_ESIGNER_PASSWORD }} | |
| 45 | 新增: CAGE_ESIGNER_CREDENTIAL_ID: ${{ secrets.CAGE_ESIGNER_CREDENTIAL_ID }} | |
| 46 | 新增: CAGE_ESIGNER_CERTIFICATE_SERIAL: ${{ vars.CAGE_ESIGNER_CERTIFICATE_SERIAL }} | |
| 47 | 新增: CAGE_ESIGNER_TOTP_SECRET: ${{ secrets.CAGE_ESIGNER_TOTP_SECRET }} | |
| 38 | 48 | CAGE_WINDOWS_PFX_BASE64: ${{ secrets.CAGE_WINDOWS_PFX_BASE64 }} |
| 39 | 49 | CAGE_WINDOWS_PFX_PASSWORD: ${{ secrets.CAGE_WINDOWS_PFX_PASSWORD }} |
| 40 | 50 | CAGE_WINDOWS_QEMU_SOURCE_URL: ${{ vars.CAGE_WINDOWS_QEMU_SOURCE_URL }} |
| @@ -43,7 +53,7 @@ jobs: | ||
| 43 | 53 | - name: Validate versions and release behavior |
| 44 | 54 | run: | |
| 45 | 55 | node scripts/check-version.mjs |
| 46 | 删除: node --test scripts/release-version.test.mjs scripts/release-control.test.mjs scripts/prepare-release-assets.test.mjs | |
| 56 | 新增: node --test scripts/release-version.test.mjs scripts/release-control.test.mjs scripts/prepare-release-assets.test.mjs scripts/esigner.test.mjs | |
| 47 | 57 | prepare: |
| 48 | 58 | needs: readiness |
| 49 | 59 | runs-on: ubuntu-24.04 |
| @@ -98,9 +98,19 @@ jobs: | ||
| 98 | 98 | with: |
| 99 | 99 | path: _output/windows-deps |
| 100 | 100 | key: cage-windows-deps-${{ hashFiles('packaging/windows/dependencies.json') }} |
| 101 | 新增: - uses: actions/setup-java@v4 | |
| 102 | 新增: with: | |
| 103 | 新增: distribution: temurin | |
| 104 | 新增: java-version: '21' | |
| 101 | 105 | - name: Build, sign, test and install the release candidate |
| 102 | 106 | env: |
| 103 | 107 | CAGE_BUILD_REF: ${{ inputs.ref }} |
| 108 | 新增: CAGE_WINDOWS_SIGNING_PROVIDER: ${{ vars.CAGE_WINDOWS_SIGNING_PROVIDER }} | |
| 109 | 新增: CAGE_ESIGNER_USERNAME: ${{ secrets.CAGE_ESIGNER_USERNAME }} | |
| 110 | 新增: CAGE_ESIGNER_PASSWORD: ${{ secrets.CAGE_ESIGNER_PASSWORD }} | |
| 111 | 新增: CAGE_ESIGNER_CREDENTIAL_ID: ${{ secrets.CAGE_ESIGNER_CREDENTIAL_ID }} | |
| 112 | 新增: CAGE_ESIGNER_CERTIFICATE_SERIAL: ${{ vars.CAGE_ESIGNER_CERTIFICATE_SERIAL }} | |
| 113 | 新增: CAGE_ESIGNER_TOTP_SECRET: ${{ secrets.CAGE_ESIGNER_TOTP_SECRET }} | |
| 104 | 114 | CAGE_WINDOWS_PFX_BASE64: ${{ secrets.CAGE_WINDOWS_PFX_BASE64 }} |
| 105 | 115 | CAGE_WINDOWS_PFX_PASSWORD: ${{ secrets.CAGE_WINDOWS_PFX_PASSWORD }} |
| 106 | 116 | CAGE_WINDOWS_QEMU_SOURCE_URL: ${{ vars.CAGE_WINDOWS_QEMU_SOURCE_URL }} |
| @@ -0,0 +1,44 @@ | ||
| 1 | 新增:name: Verify Windows cloud signing without a release | |
| 2 | 新增:on: | |
| 3 | 新增: workflow_dispatch: | |
| 4 | 新增:permissions: | |
| 5 | 新增: contents: read | |
| 6 | 新增:concurrency: | |
| 7 | 新增: group: cage-windows-signing-check | |
| 8 | 新增: cancel-in-progress: false | |
| 9 | 新增:jobs: | |
| 10 | 新增: verify: | |
| 11 | 新增: if: github.repository == 'Lee-xeo/Claude-Cage' && github.ref == 'refs/heads/main' | |
| 12 | 新增: runs-on: windows-2025 | |
| 13 | 新增: environment: cage-release | |
| 14 | 新增: timeout-minutes: 20 | |
| 15 | 新增: steps: | |
| 16 | 新增: - uses: actions/checkout@v4 | |
| 17 | 新增: with: | |
| 18 | 新增: ref: ${{ github.sha }} | |
| 19 | 新增: persist-credentials: false | |
| 20 | 新增: - uses: actions/setup-node@v4 | |
| 21 | 新增: with: | |
| 22 | 新增: node-version: 24 | |
| 23 | 新增: - uses: actions/setup-java@v4 | |
| 24 | 新增: with: | |
| 25 | 新增: distribution: temurin | |
| 26 | 新增: java-version: '21' | |
| 27 | 新增: - name: Sign one disposable executable and verify Windows trust | |
| 28 | 新增: shell: pwsh | |
| 29 | 新增: env: | |
| 30 | 新增: CAGE_WINDOWS_SIGNING_PROVIDER: ${{ vars.CAGE_WINDOWS_SIGNING_PROVIDER }} | |
| 31 | 新增: CAGE_ESIGNER_USERNAME: ${{ secrets.CAGE_ESIGNER_USERNAME }} | |
| 32 | 新增: CAGE_ESIGNER_PASSWORD: ${{ secrets.CAGE_ESIGNER_PASSWORD }} | |
| 33 | 新增: CAGE_ESIGNER_CREDENTIAL_ID: ${{ secrets.CAGE_ESIGNER_CREDENTIAL_ID }} | |
| 34 | 新增: CAGE_ESIGNER_CERTIFICATE_SERIAL: ${{ vars.CAGE_ESIGNER_CERTIFICATE_SERIAL }} | |
| 35 | 新增: CAGE_ESIGNER_TOTP_SECRET: ${{ secrets.CAGE_ESIGNER_TOTP_SECRET }} | |
| 36 | 新增: run: ./scripts/check-windows-signing.ps1 | |
| 37 | 新增: - name: Preserve verification facts only | |
| 38 | 新增: if: always() | |
| 39 | 新增: uses: actions/upload-artifact@v4 | |
| 40 | 新增: with: | |
| 41 | 新增: name: windows-signing-verification-${{ github.run_id }} | |
| 42 | 新增: path: _output/signing-check/result.json | |
| 43 | 新增: if-no-files-found: ignore | |
| 44 | 新增: retention-days: 14 | |
| @@ -60,21 +60,27 @@ gh workflow run release.yml --repo Lee-xeo/Claude-Cage --ref main -f bump=prerel | ||
| 60 | 60 | | `CAGE_MACOS_NOTARY_KEY_ID` | API Key ID | |
| 61 | 61 | | `CAGE_MACOS_NOTARY_ISSUER_ID` | API Issuer ID | |
| 62 | 62 | | `CAGE_MACOS_PROVISIONING_PROFILE_BASE64` | 可选;原生能力需要时提供对应 Provisioning Profile | |
| 63 | 删除:| `CAGE_WINDOWS_PFX_BASE64` | 含可信代码签名私钥的 PFX 文件,以 Base64 保存 | | |
| 64 | 删除:| `CAGE_WINDOWS_PFX_PASSWORD` | PFX 密码 | | |
| 63 | 新增:| `CAGE_ESIGNER_USERNAME` | 使用 eSigner 时,SSL.com 登录用户名 | | |
| 64 | 新增:| `CAGE_ESIGNER_PASSWORD` | 使用 eSigner 时,SSL.com 登录密码 | | |
| 65 | 新增:| `CAGE_ESIGNER_CREDENTIAL_ID` | 使用 eSigner 时,已启用的签名凭据 ID | | |
| 66 | 新增:| `CAGE_ESIGNER_TOTP_SECRET` | 使用 eSigner 无人值守签名时,验证器的 TOTP 密钥;不是每 30 秒变化的六位验证码 | | |
| 67 | 新增:| `CAGE_WINDOWS_PFX_BASE64` | 仅证书存储方案需要:含可信代码签名私钥的 PFX 文件,以 Base64 保存 | | |
| 68 | 新增:| `CAGE_WINDOWS_PFX_PASSWORD` | 仅证书存储方案需要:PFX 密码 | | |
| 65 | 69 | |
| 66 | 删除:Windows 对应源码地址放入同一环境的 Variables: | |
| 70 | 新增:Windows 签名方式和对应源码地址放入同一环境的 Variables: | |
| 67 | 71 | |
| 68 | 72 | | Variable | 用途 | |
| 69 | 73 | |---|---| |
| 74 | 新增:| `CAGE_WINDOWS_SIGNING_PROVIDER` | 云签名填 `esigner`;留空或填 `certificate-store` 使用证书存储方案 | | |
| 75 | 新增:| `CAGE_ESIGNER_CERTIFICATE_SERIAL` | 使用 eSigner 时,预期发布者证书的十六进制序列号,不含空格 | | |
| 70 | 76 | | `CAGE_WINDOWS_QEMU_SOURCE_URL` | 经核对的完整 QEMU 对应源码归档公开 HTTPS 地址,不含认证信息或临时签名参数,包含随附库与固件源码 | |
| 71 | 77 | | `CAGE_WINDOWS_QEMU_SOURCE_SHA256` | 该归档的 SHA-256 | |
| 72 | 78 | |
| 73 | 删除:Windows 的普通开发包不需要证书。正式发布缺少可信证书时停止,不能用自签名证书或未签名包代替。当前签名接入使用 PFX;若以后采用硬件密钥或云签名服务,应调整签名接入并重新验收。 | |
| 79 | 新增:Windows 的普通开发包不需要证书。正式发布缺少可信证书时停止,不能用自签名证书或未签名包代替。eSigner 使用云端私钥,不要求 PFX;构建逐个签名应用、后端、Mihomo、卸载器和安装器,再由 Windows 验证信任链、时间戳及预期证书。任何一步失败就停止。首次签名的本机操作见 [Windows 维护者说明](windows.md#维护者构建与验收)。 | |
| 74 | 80 | |
| 75 | 删除:macOS 使用临时钥匙串,Windows 使用临时证书导入;成功、失败及中断路径均清理签名材料。发布产物只包含安装包、对应源码和摘要;中间公证凭据、私钥与配置文件不进入附件。 | |
| 81 | 新增:macOS 使用临时钥匙串,Windows 证书存储方案使用临时证书导入。eSigner 使用固定摘要的官方 CodeSignTool 和 Java 21,通过临时参数文件传递密码,进程命令行只包含该文件路径;运行结束清理参数文件及工具日志。保留服务端恶意软件检查设置。发布产物只包含安装包、对应源码和摘要;中间公证凭据、私钥与配置文件不进入附件。 | |
| 76 | 82 | |
| 77 | 删除:签名文件通常体积较小,按 GitHub Secret 的限制保存;首次使用前应在相应平台确认凭据可用。材料存在不代表签名、公证或安装一定成功,正式构建仍会验证。 | |
| 83 | 新增:签名文件通常体积较小,按 GitHub Secret 的限制保存;首次使用前应在相应平台确认凭据可用。是否把账号密码和 TOTP 密钥交给 GitHub 必须由证书持有人决定;只做本机手动签名不需要保存这些 Secrets。材料存在不代表签名、公证或安装一定成功,正式构建仍会验证。 | |
| 78 | 84 | |
| 79 | 85 | ## 失败后继续 |
| 80 | 86 | |
| @@ -71,6 +71,19 @@ pwsh -File scripts/build-windows.ps1 | ||
| 71 | 71 | pwsh -File scripts/package-windows.ps1 -SkipBuild |
| 72 | 72 | ``` |
| 73 | 73 | |
| 74 | 删除:应用目录输出到 `_output/windows-amd64/app`,安装器与校验文件输出到 `_output/releases/<版本>/windows`。正式签名构建使用构建脚本的 `-Release` 模式,并配置当前用户证书存储中的代码签名证书;发布脚本还要求与 QEMU 构建相对应的源码归档。参数以脚本帮助为准。签名完成后再生成发行物摘要,不能对签名后的文件重新修改。两个平台同批发布时,使用[公开发行流程](public-releases.md#发布安装包)生成共用校验清单;官网仅在公开 Release 的 Windows 附件和摘要有效时显示下载按钮。 | |
| 74 | 新增:应用目录输出到 `_output/windows-amd64/app`,安装器与校验文件输出到 `_output/releases/<版本>/windows`。正式签名构建使用构建脚本的 `-Release` 模式,支持 SSL.com eSigner 或当前用户证书存储中的代码签名证书;发布脚本还要求与 QEMU 构建相对应的源码归档。参数以脚本帮助为准。签名完成后再生成发行物摘要,不能对签名后的文件重新修改。两个平台同批发布时,使用[公开发行流程](public-releases.md#发布安装包)生成共用校验清单;官网仅在公开 Release 的 Windows 附件和摘要有效时显示下载按钮。 | |
| 75 | 新增: | |
| 76 | 新增:首次验证 eSigner 时,在自己的终端运行下面的命令。需要 Java 21 和 Node.js;macOS、Windows 和 Linux 都可以调用签名工具。工具固定版本与摘要保存在 `packaging/windows/signing.json`,下载内容变化时会停止,须审核后更新。输入 SSL.com 用户名、密码、凭据 ID,等工具提示后再输入验证器当时显示的六位验证码。密码和验证码不显示、不保存;签名后的副本写入新的输出目录,原文件保留。 | |
| 77 | 新增: | |
| 78 | 新增:```sh | |
| 79 | 新增:node scripts/esigner.mjs install | |
| 80 | 新增:node scripts/esigner.mjs sign /absolute/path/to/Cage.exe --output-dir /absolute/path/to/fresh-signed-output | |
| 81 | 新增:``` | |
| 82 | 新增: | |
| 83 | 新增:签名单个文件不会发版。完整 Windows 构建选择 `CAGE_WINDOWS_SIGNING_PROVIDER=esigner`,并设置 `CAGE_ESIGNER_USERNAME`、`CAGE_ESIGNER_PASSWORD`、`CAGE_ESIGNER_CREDENTIAL_ID`、`CAGE_ESIGNER_CERTIFICATE_SERIAL` 和 `CAGE_ESIGNER_TOTP_SECRET`;其中 TOTP 密钥供无人值守流程生成验证码,不能用六位验证码代替。NSIS 的卸载器签名子进程不提供交互终端,因此完整打包需要这些环境配置。密码和密钥不要写入 shell 命令历史或源码。GitHub 环境配置见 [CI/CD 说明](ci-cd.md#首次配置发布材料)。 | |
| 84 | 新增: | |
| 85 | 新增:Windows 正式构建会检查可信 Authenticode 签名、时间戳与指定证书;只通过这些检查的副本才会替换待打包文件。其他平台上的签名测试不能替代 Windows 信任与安装验收。自动更新继续要求旧应用和新安装器使用相同发布者公钥,换用新密钥前需要单独安排更新迁移。 | |
| 86 | 新增: | |
| 87 | 新增:工具参数与验证器配置以 SSL.com 的 [CodeSignTool 指南](https://www.ssl.com/guide/esigner-codesigntool-command-guide/)和 [签名凭据指南](https://www.ssl.com/guide/esigner-signing-credential-guide/)为准。 | |
| 75 | 88 | |
| 76 | 89 | CI 负责源码、原生窗口、安装器和 Windows 可执行测试;干净 Windows 10/11 上的虚拟化仍需要设备验收。每次正式发行至少记录:首次安装、WebView2 首次启动、无虚拟化时的提示、代理导入、VM 首次下载与启动、网络隔离检查、中文和空格路径共享、终端输入与缩放、退出恢复、从旧签名版升级、失败恢复、卸载后数据保留。只有真实执行的项目才能标记通过。 |
| @@ -40,8 +40,8 @@ VIAddVersionKey /LANG=1033 "FileVersion" "${VERSION}" | ||
| 40 | 40 | VIAddVersionKey /LANG=1033 "FileDescription" "Cage Windows Installer" |
| 41 | 41 | VIAddVersionKey /LANG=1033 "LegalCopyright" "Cage contributors" |
| 42 | 42 | |
| 43 | 删除:!ifdef SIGNTOOL | |
| 44 | 删除: !uninstfinalize '"${SIGNTOOL}" sign /sha1 "${CERT_SHA1}" /fd SHA256 /tr http://timestamp.digicert.com /td SHA256 "%1"' = 0 | |
| 43 | 新增:!ifdef SIGN_SCRIPT | |
| 44 | 新增: !uninstfinalize '"${SIGN_PWSH}" -NoProfile -File "${SIGN_SCRIPT}" -Path "%1"' = 0 | |
| 45 | 45 | !endif |
| 46 | 46 | |
| 47 | 47 | !define MUI_ABORTWARNING |
| @@ -0,0 +1,8 @@ | ||
| 1 | 新增:{ | |
| 2 | 新增: "version": "1.3.3", | |
| 3 | 新增: "url": "https://www.ssl.com/download/codesigntool-for-linux-and-macos/", | |
| 4 | 新增: "archiveSHA256": "ecdb16fb60c9be1145f450924b8b1e615bee08ac45180d5f66b47712a223a6ec", | |
| 5 | 新增: "jar": "jar/code_sign_tool-1.3.3.jar", | |
| 6 | 新增: "jarSHA256": "caa356347aa64ba04666545d548dad87211c9aa960f16db8797a880a67ba91d1", | |
| 7 | 新增: "configSHA256": "3068bc196b30e05c79d117ece98ea66bca043c839c6dfdea884efd5fe5301bb8" | |
| 8 | 新增:} | |
| @@ -13,9 +13,7 @@ if (-not $DependencyCache) { $DependencyCache = Join-Path $repository '_output/w | ||
| 13 | 13 | $OutputDirectory = [IO.Path]::GetFullPath($OutputDirectory) |
| 14 | 14 | if (Test-Path -LiteralPath (Join-Path $OutputDirectory 'Uninstall.exe')) { throw 'Do not build over an installed application. Choose a separate output directory and use the installer to upgrade.' } |
| 15 | 15 | $version = Get-CageVersion $repository |
| 16 | 删除:if ($Release -and (-not $IsWindows -or $env:CAGE_WINDOWS_CERT_SHA1 -notmatch '^[0-9A-Fa-f]{40}$')) { | |
| 17 | 删除: throw 'A release build requires Windows and CAGE_WINDOWS_CERT_SHA1 for a trusted code-signing certificate in the current user certificate store.' | |
| 18 | 删除:} | |
| 16 | 新增:if ($Release) { Assert-WindowsSigningConfiguration } | |
| 19 | 17 | $go = Get-BuildCommand @('go') |
| 20 | 18 | $dotnet = Get-BuildCommand @('dotnet') |
| 21 | 19 | $node = Get-BuildCommand @('node') |
| @@ -2,14 +2,25 @@ | ||
| 2 | 2 | const required = [ |
| 3 | 3 | 'CAGE_MACOS_P12_BASE64', 'CAGE_MACOS_P12_PASSWORD', 'CAGE_MACOS_SIGNING_IDENTITY', |
| 4 | 4 | 'CAGE_MACOS_NOTARY_KEY_BASE64', 'CAGE_MACOS_NOTARY_KEY_ID', 'CAGE_MACOS_NOTARY_ISSUER_ID', |
| 5 | 删除: 'CAGE_WINDOWS_PFX_BASE64', 'CAGE_WINDOWS_PFX_PASSWORD', | |
| 6 | 5 | 'CAGE_WINDOWS_QEMU_SOURCE_URL', 'CAGE_WINDOWS_QEMU_SOURCE_SHA256', |
| 7 | 6 | ]; |
| 7 | 新增:const provider = process.env.CAGE_WINDOWS_SIGNING_PROVIDER || 'certificate-store'; | |
| 8 | 新增:if (provider === 'esigner') { | |
| 9 | 新增: required.push('CAGE_ESIGNER_USERNAME', 'CAGE_ESIGNER_PASSWORD', 'CAGE_ESIGNER_CREDENTIAL_ID', 'CAGE_ESIGNER_CERTIFICATE_SERIAL', 'CAGE_ESIGNER_TOTP_SECRET'); | |
| 10 | 新增:} else if (provider === 'certificate-store') { | |
| 11 | 新增: required.push('CAGE_WINDOWS_PFX_BASE64', 'CAGE_WINDOWS_PFX_PASSWORD'); | |
| 12 | 新增:} else { | |
| 13 | 新增: throw new Error('Unknown CAGE_WINDOWS_SIGNING_PROVIDER; use esigner or certificate-store.'); | |
| 14 | 新增:} | |
| 8 | 15 | const missing = required.filter(name => !process.env[name]?.trim()); |
| 9 | 16 | if (missing.length) { |
| 10 | 17 | console.error(`Configure these cage-release environment secrets/variables before requesting a release:\n${missing.join('\n')}`); |
| 11 | 18 | process.exitCode = 1; |
| 12 | 19 | } else { |
| 20 | 新增: if (provider === 'esigner' && (!/^[0-9a-f-]{36}$/i.test(process.env.CAGE_ESIGNER_CREDENTIAL_ID) || | |
| 21 | 新增: !/^[0-9a-f]{32,40}$/i.test(process.env.CAGE_ESIGNER_CERTIFICATE_SERIAL))) { | |
| 22 | 新增: throw new Error('Invalid eSigner credential ID or expected certificate serial.'); | |
| 23 | 新增: } | |
| 13 | 24 | let source; |
| 14 | 25 | try { source = new URL(process.env.CAGE_WINDOWS_QEMU_SOURCE_URL); } |
| 15 | 26 | catch { throw new Error('Reviewed QEMU source requires a valid public HTTPS URL.'); } |
| @@ -0,0 +1,55 @@ | ||
| 1 | 新增:#requires -Version 7.0 | |
| 2 | 新增:# One real signature on a disposable PE file. No package, version or release writes. | |
| 3 | 新增:[CmdletBinding()] | |
| 4 | 新增:param() | |
| 5 | 新增:. (Join-Path $PSScriptRoot 'windows-common.ps1') | |
| 6 | 新增:if (-not $IsWindows) { throw 'This verification requires Windows trust APIs.' } | |
| 7 | 新增:if ($env:GITHUB_ACTIONS -ne 'true' -or $env:GITHUB_EVENT_NAME -ne 'workflow_dispatch' -or | |
| 8 | 新增: $env:GITHUB_REPOSITORY -ne 'Lee-xeo/Claude-Cage' -or $env:GITHUB_REF -ne 'refs/heads/main') { | |
| 9 | 新增: throw 'Run the explicit main-branch signing verification workflow.' | |
| 10 | 新增:} | |
| 11 | 新增:if ($env:CAGE_WINDOWS_SIGNING_PROVIDER -ne 'esigner') { throw 'Select esigner for this verification.' } | |
| 12 | 新增:foreach ($name in @('CAGE_ESIGNER_USERNAME', 'CAGE_ESIGNER_PASSWORD', 'CAGE_ESIGNER_CREDENTIAL_ID', 'CAGE_ESIGNER_CERTIFICATE_SERIAL', 'CAGE_ESIGNER_TOTP_SECRET')) { | |
| 13 | 新增: if (-not [Environment]::GetEnvironmentVariable($name)) { throw "Missing cage-release setting: $name" } | |
| 14 | 新增:} | |
| 15 | 新增:Assert-WindowsSigningConfiguration | |
| 16 | 新增:$repository = Split-Path $PSScriptRoot -Parent | |
| 17 | 新增:$reportDirectory = Join-Path $repository '_output/signing-check' | |
| 18 | 新增:$stage = Join-Path ([IO.Path]::GetTempPath()) ('cage-signing-check-' + [guid]::NewGuid().ToString('N')) | |
| 19 | 新增:$report = [ordered]@{ status = 'failed'; commit = $env:GITHUB_SHA; windowsTrustTested = $false; installationTested = $false } | |
| 20 | 新增:$savedCertificate = $env:CAGE_WINDOWS_CERT_SHA1 | |
| 21 | 新增:try { | |
| 22 | 新增: New-Item -ItemType Directory -Path $stage | Out-Null | |
| 23 | 新增: $source = Join-Path $stage 'SigningProbe.cs' | |
| 24 | 新增: @' | |
| 25 | 新增:using System; | |
| 26 | 新增:public static class SigningProbe { | |
| 27 | 新增: public static void Main() { Console.WriteLine("Cage code signing verification fixture."); } | |
| 28 | 新增:} | |
| 29 | 新增:'@ | Set-Content -LiteralPath $source -Encoding utf8 | |
| 30 | 新增: $compiler = Join-Path $env:WINDIR 'Microsoft.NET/Framework64/v4.0.30319/csc.exe' | |
| 31 | 新增: $executable = Join-Path $stage 'probe.exe' | |
| 32 | 新增: Invoke-BuildTool $compiler @('/nologo', '/target:exe', "/out:$executable", $source) | |
| 33 | 新增: # NSIS hands its signing hook a PE file without an extension. Exercise that path. | |
| 34 | 新增: $probe = Join-Path $stage 'uninstaller-probe' | |
| 35 | 新增: Move-Item -LiteralPath $executable -Destination $probe | |
| 36 | 新增: $report.unsignedSHA256 = (Get-FileHash -LiteralPath $probe -Algorithm SHA256).Hash.ToLowerInvariant() | |
| 37 | 新增: $env:CAGE_WINDOWS_CERT_SHA1 = $null | |
| 38 | 新增: Sign-CageFile $probe (Get-SigningTool) | |
| 39 | 新增: $signature = Get-AuthenticodeSignature -LiteralPath $probe | |
| 40 | 新增: $report.windowsTrustTested = $true | |
| 41 | 新增: $report.publisher = $signature.SignerCertificate.GetNameInfo([Security.Cryptography.X509Certificates.X509NameType]::SimpleName, $false) | |
| 42 | 新增: $report.certificateSerial = $signature.SignerCertificate.SerialNumber | |
| 43 | 新增: $report.certificateThumbprint = $signature.SignerCertificate.Thumbprint | |
| 44 | 新增: $report.timestampAuthority = $signature.TimeStamperCertificate.Subject | |
| 45 | 新增: $report.signedSHA256 = (Get-FileHash -LiteralPath $probe -Algorithm SHA256).Hash.ToLowerInvariant() | |
| 46 | 新增: if ($report.unsignedSHA256 -eq $report.signedSHA256) { throw 'Signing did not change the executable.' } | |
| 47 | 新增: $report.status = 'passed' | |
| 48 | 新增: Write-Host 'Passed: cloud signing, configured publisher certificate, timestamp and Windows trust.' | |
| 49 | 新增:} finally { | |
| 50 | 新增: $env:CAGE_WINDOWS_CERT_SHA1 = $savedCertificate | |
| 51 | 新增: Remove-Item Env:CAGE_ESIGNER_PASSWORD, Env:CAGE_ESIGNER_TOTP_SECRET -ErrorAction SilentlyContinue | |
| 52 | 新增: if (Test-Path -LiteralPath $stage) { Remove-Item -LiteralPath $stage -Recurse -Force } | |
| 53 | 新增: New-Item -ItemType Directory -Path $reportDirectory -Force | Out-Null | |
| 54 | 新增: $report | ConvertTo-Json | Set-Content -LiteralPath (Join-Path $reportDirectory 'result.json') -Encoding utf8 | |
| 55 | 新增:} | |
| @@ -18,7 +18,15 @@ try { | ||
| 18 | 18 | if ($LASTEXITCODE -ne 0 -or $actual -ne $env:CAGE_BUILD_REF) { throw 'Checkout differs from the requested commit.' } |
| 19 | 19 | if ($Release) { |
| 20 | 20 | if ($env:GITHUB_EVENT_NAME -ne 'workflow_dispatch') { throw 'Release candidates require an explicitly dispatched release workflow.' } |
| 21 | 删除: if (-not $env:CAGE_WINDOWS_PFX_BASE64 -or -not $env:CAGE_WINDOWS_PFX_PASSWORD) { | |
| 21 | 新增: if ($env:CAGE_WINDOWS_SIGNING_PROVIDER -eq 'esigner') { | |
| 22 | 新增: foreach ($name in @('CAGE_ESIGNER_USERNAME', 'CAGE_ESIGNER_PASSWORD', 'CAGE_ESIGNER_CREDENTIAL_ID', 'CAGE_ESIGNER_CERTIFICATE_SERIAL', 'CAGE_ESIGNER_TOTP_SECRET')) { | |
| 23 | 新增: if (-not [Environment]::GetEnvironmentVariable($name)) { throw "Missing cage-release setting: $name" } | |
| 24 | 新增: } | |
| 25 | 新增: Assert-WindowsSigningConfiguration | |
| 26 | 新增: Invoke-BuildTool 'node' @('scripts/esigner.mjs', 'install') | |
| 27 | 新增: } elseif ($env:CAGE_WINDOWS_SIGNING_PROVIDER -and $env:CAGE_WINDOWS_SIGNING_PROVIDER -ne 'certificate-store') { | |
| 28 | 新增: throw 'Unknown Windows signing provider.' | |
| 29 | 新增: } elseif (-not $env:CAGE_WINDOWS_PFX_BASE64 -or -not $env:CAGE_WINDOWS_PFX_PASSWORD) { | |
| 22 | 30 | throw 'Configure CAGE_WINDOWS_PFX_BASE64 and CAGE_WINDOWS_PFX_PASSWORD in the cage-release environment.' |
| 23 | 31 | } |
| 24 | 32 | $sourceUri = $null |
| @@ -36,18 +44,20 @@ try { | ||
| 36 | 44 | if ((Get-FileHash -LiteralPath $qemuSource -Algorithm SHA256).Hash -ne $env:CAGE_WINDOWS_QEMU_SOURCE_SHA256) { |
| 37 | 45 | throw 'Reviewed corresponding-source archive checksum mismatch.' |
| 38 | 46 | } |
| 39 | 删除: $pfx = Join-Path $env:RUNNER_TEMP ('cage-signing-' + [guid]::NewGuid().ToString('N') + '.pfx') | |
| 40 | 删除: [IO.File]::WriteAllBytes($pfx, [Convert]::FromBase64String($env:CAGE_WINDOWS_PFX_BASE64)) | |
| 41 | 删除: $password = ConvertTo-SecureString $env:CAGE_WINDOWS_PFX_PASSWORD -AsPlainText -Force | |
| 42 | 删除: $certificates = @(Import-PfxCertificate -FilePath $pfx -CertStoreLocation Cert:\CurrentUser\My -Password $password) | |
| 43 | 删除: Remove-Item -LiteralPath $pfx -Force | |
| 44 | 删除: $signers = @($certificates | Where-Object { | |
| 45 | 删除: $_.HasPrivateKey -and @($_.EnhancedKeyUsageList | Where-Object { $_.ObjectId -eq '1.3.6.1.5.5.7.3.3' }).Count -gt 0 | |
| 46 | 删除: }) | |
| 47 | 删除: if ($signers.Count -ne 1) { throw 'PFX must contain exactly one code-signing certificate with its private key.' } | |
| 48 | 删除: $certificate = $signers[0] | |
| 49 | 删除: $env:CAGE_WINDOWS_CERT_SHA1 = $certificate.Thumbprint | |
| 50 | 删除: Remove-Item Env:CAGE_WINDOWS_PFX_BASE64, Env:CAGE_WINDOWS_PFX_PASSWORD | |
| 47 | 新增: if ($env:CAGE_WINDOWS_SIGNING_PROVIDER -ne 'esigner') { | |
| 48 | 新增: $pfx = Join-Path $env:RUNNER_TEMP ('cage-signing-' + [guid]::NewGuid().ToString('N') + '.pfx') | |
| 49 | 新增: [IO.File]::WriteAllBytes($pfx, [Convert]::FromBase64String($env:CAGE_WINDOWS_PFX_BASE64)) | |
| 50 | 新增: $password = ConvertTo-SecureString $env:CAGE_WINDOWS_PFX_PASSWORD -AsPlainText -Force | |
| 51 | 新增: $certificates = @(Import-PfxCertificate -FilePath $pfx -CertStoreLocation Cert:\CurrentUser\My -Password $password) | |
| 52 | 新增: Remove-Item -LiteralPath $pfx -Force | |
| 53 | 新增: $signers = @($certificates | Where-Object { | |
| 54 | 新增: $_.HasPrivateKey -and @($_.EnhancedKeyUsageList | Where-Object { $_.ObjectId -eq '1.3.6.1.5.5.7.3.3' }).Count -gt 0 | |
| 55 | 新增: }) | |
| 56 | 新增: if ($signers.Count -ne 1) { throw 'PFX must contain exactly one code-signing certificate with its private key.' } | |
| 57 | 新增: $certificate = $signers[0] | |
| 58 | 新增: $env:CAGE_WINDOWS_CERT_SHA1 = $certificate.Thumbprint | |
| 59 | 新增: Remove-Item Env:CAGE_WINDOWS_PFX_BASE64, Env:CAGE_WINDOWS_PFX_PASSWORD | |
| 60 | 新增: } | |
| 51 | 61 | } |
| 52 | 62 | |
| 53 | 63 | & (Join-Path $PSScriptRoot 'install-nsis.ps1') -Destination $env:RUNNER_TEMP |
| @@ -88,5 +98,6 @@ try { | ||
| 88 | 98 | if ($qemuSource) { Remove-Item -LiteralPath $qemuSource -Force -ErrorAction SilentlyContinue } |
| 89 | 99 | $env:PATH = $savedPath |
| 90 | 100 | $env:CAGE_WINDOWS_CERT_SHA1 = $savedCertificate |
| 101 | 新增: Remove-Item Env:CAGE_ESIGNER_PASSWORD, Env:CAGE_ESIGNER_TOTP_SECRET -ErrorAction SilentlyContinue | |
| 91 | 102 | Pop-Location |
| 92 | 103 | } |
| @@ -0,0 +1,213 @@ | ||
| 1 | 新增:// Windows Authenticode signing via SSL.com's official Java tool. No release mutations. | |
| 2 | 新增:import { createHash } from 'node:crypto'; | |
| 3 | 新增:import { execFileSync, spawn } from 'node:child_process'; | |
| 4 | 新增:import { copyFile, mkdir, mkdtemp, readFile, rm, stat, writeFile, access, open } from 'node:fs/promises'; | |
| 5 | 新增:import { tmpdir } from 'node:os'; | |
| 6 | 新增:import { basename, dirname, join, resolve } from 'node:path'; | |
| 7 | 新增:import { fileURLToPath, pathToFileURL } from 'node:url'; | |
| 8 | 新增:import { createInterface } from 'node:readline/promises'; | |
| 9 | 新增:import { Writable } from 'node:stream'; | |
| 10 | 新增: | |
| 11 | 新增:const root = fileURLToPath(new URL('../', import.meta.url)); | |
| 12 | 新增:const pin = JSON.parse(await readFile(new URL('../packaging/windows/signing.json', import.meta.url), 'utf8')); | |
| 13 | 新增:const configPath = 'conf/code_sign_tool.properties'; | |
| 14 | 新增:const secretNames = ['CAGE_ESIGNER_PASSWORD', 'CAGE_ESIGNER_TOTP_SECRET']; | |
| 15 | 新增:const hash = bytes => createHash('sha256').update(bytes).digest('hex'); | |
| 16 | 新增:const toolDirectory = () => resolve(process.env.CAGE_ESIGNER_TOOL_DIR || join(root, '_output/esigner', `CodeSignTool-v${pin.version}`)); | |
| 17 | 新增: | |
| 18 | 新增:export function javaArgument(value) { | |
| 19 | 新增: // Java launcher @files, not a shell. Quotes/backslashes must round-trip literally. | |
| 20 | 新增: if (/[\r\n\0]/u.test(value)) throw new Error('Signing arguments must not contain newlines or NUL.'); | |
| 21 | 新增: return `"${value.replaceAll('\\', '\\\\').replaceAll('"', '\\"')}"`; | |
| 22 | 新增:} | |
| 23 | 新增: | |
| 24 | 新增:export function redact(text, secrets) { | |
| 25 | 新增: for (const secret of secrets.filter(Boolean).sort((a, b) => b.length - a.length)) text = text.replaceAll(secret, '[redacted]'); | |
| 26 | 新增: return text; | |
| 27 | 新增:} | |
| 28 | 新增: | |
| 29 | 新增:export async function verifyTool(directory) { | |
| 30 | 新增: for (const [name, digest] of [[pin.jar, pin.jarSHA256], [configPath, pin.configSHA256]]) { | |
| 31 | 新增: if (hash(await readFile(join(directory, name))) !== digest) throw new Error(`CodeSignTool checksum mismatch: ${name}`); | |
| 32 | 新增: } | |
| 33 | 新增:} | |
| 34 | 新增: | |
| 35 | 新增:async function installTool() { | |
| 36 | 新增: const directory = toolDirectory(); | |
| 37 | 新增: try { await verifyTool(directory); return directory; } | |
| 38 | 新增: catch (error) { if (error.code !== 'ENOENT') throw error; } | |
| 39 | 新增: await mkdir(dirname(directory), { recursive: true }); | |
| 40 | 新增: const temp = await mkdtemp(join(dirname(directory), 'download-')); | |
| 41 | 新增: try { | |
| 42 | 新增: const archive = join(temp, 'CodeSignTool.zip'); | |
| 43 | 新增: execFileSync(process.platform === 'win32' ? 'curl.exe' : 'curl', | |
| 44 | 新增: ['--fail', '--location', '--silent', '--show-error', '--max-time', '180', '--output', archive, pin.url], { stdio: 'inherit' }); | |
| 45 | 新增: if (hash(await readFile(archive)) !== pin.archiveSHA256) throw new Error('CodeSignTool ZIP checksum mismatch. Review a new vendor version before changing the pin.'); | |
| 46 | 新增: const extracted = join(temp, 'tool'); | |
| 47 | 新增: await mkdir(extracted); | |
| 48 | 新增: execFileSync('tar', ['-xf', archive, '-C', extracted]); | |
| 49 | 新增: await verifyTool(extracted); | |
| 50 | 新增: // Copy only the pinned files. Vendor launchers and logs are never executed or reused. | |
| 51 | 新增: for (const name of [pin.jar, configPath]) { | |
| 52 | 新增: await mkdir(dirname(join(directory, name)), { recursive: true }); | |
| 53 | 新增: await copyFile(join(extracted, name), join(directory, name)); | |
| 54 | 新增: } | |
| 55 | 新增: await verifyTool(directory); | |
| 56 | 新增: return directory; | |
| 57 | 新增: } finally { await rm(temp, { recursive: true, force: true }); } | |
| 58 | 新增:} | |
| 59 | 新增: | |
| 60 | 新增:async function prompt(label, hidden = false, signal) { | |
| 61 | 新增: if (!process.stdin.isTTY) throw new Error(`Missing ${label}. Run this command in your own terminal, or configure the signing environment secrets.`); | |
| 62 | 新增: const sink = new Writable({ write(_chunk, _encoding, callback) { callback(); } }); | |
| 63 | 新增: const rl = createInterface({ input: process.stdin, output: hidden ? sink : process.stdout, terminal: true }); | |
| 64 | 新增: const controller = new AbortController(); | |
| 65 | 新增: const abort = () => controller.abort(); | |
| 66 | 新增: rl.once('SIGINT', abort); | |
| 67 | 新增: signal?.addEventListener('abort', abort, { once: true }); | |
| 68 | 新增: try { | |
| 69 | 新增: if (signal?.aborted) controller.abort(); | |
| 70 | 新增: if (hidden) process.stdout.write(`${label}: `); | |
| 71 | 新增: return await rl.question(hidden ? '' : `${label}: `, { signal: controller.signal }); | |
| 72 | 新增: } finally { | |
| 73 | 新增: signal?.removeEventListener('abort', abort); | |
| 74 | 新增: rl.close(); | |
| 75 | 新增: if (hidden) process.stdout.write('\n'); | |
| 76 | 新增: } | |
| 77 | 新增:} | |
| 78 | 新增: | |
| 79 | 新增:// Exported for exercising the process boundary with an offline Java fixture. | |
| 80 | 新增:export async function invokeJava({ java = 'java', jar, config, arguments: args, otp, secrets = [] }) { | |
| 81 | 新增: const temp = await mkdtemp(join(tmpdir(), 'cage-esigner-')); | |
| 82 | 新增: let child; | |
| 83 | 新增: const controller = new AbortController(); | |
| 84 | 新增: const knownSecrets = [...secrets]; | |
| 85 | 新增: try { | |
| 86 | 新增: if (process.platform !== 'win32') await (await import('node:fs/promises')).chmod(temp, 0o700); | |
| 87 | 新增: await mkdir(join(temp, 'conf')); | |
| 88 | 新增: await copyFile(config, join(temp, configPath)); | |
| 89 | 新增: const argumentFile = join(temp, 'java.args'); | |
| 90 | 新增: await writeFile(argumentFile, ['-Xmx2048m', '-jar', resolve(jar), ...args].map(javaArgument).join('\n'), { mode: 0o600, flag: 'wx' }); | |
| 91 | 新增: const env = { ...process.env }; | |
| 92 | 新增: for (const name of secretNames) delete env[name]; | |
| 93 | 新增: // Only an argument-file path appears in the child process command line. | |
| 94 | 新增: child = spawn(java, [`@${argumentFile}`], { cwd: temp, env, stdio: ['pipe', 'pipe', 'pipe'], windowsHide: true }); | |
| 95 | 新增: child.stdin.on('error', () => {}); // A failed signer can close stdin before an OTP arrives. | |
| 96 | 新增: let stdout = '', stderr = '', otpRequested = false, otpError; | |
| 97 | 新增: let otpTask = Promise.resolve(); | |
| 98 | 新增: child.stdout.on('data', chunk => { | |
| 99 | 新增: stdout += chunk.toString(); | |
| 100 | 新增: if (!otpRequested && stdout.includes('Enter the OTP')) { | |
| 101 | 新增: otpRequested = true; | |
| 102 | 新增: otpTask = (async () => { | |
| 103 | 新增: try { | |
| 104 | 新增: const value = (await otp(controller.signal)).trim(); | |
| 105 | 新增: if (!/^\d{6}$/u.test(value)) throw new Error('The authenticator code must contain six digits.'); | |
| 106 | 新增: knownSecrets.push(value); | |
| 107 | 新增: child.stdin.end(`${value}\n`); | |
| 108 | 新增: } catch (error) { otpError = error; child.kill(); } | |
| 109 | 新增: })(); | |
| 110 | 新增: } | |
| 111 | 新增: }); | |
| 112 | 新增: child.stderr.on('data', chunk => { stderr += chunk.toString(); }); | |
| 113 | 新增: // Closing or interrupting a command must not leave a signer waiting indefinitely. | |
| 114 | 新增: const stop = () => { controller.abort(); child.kill(); }; | |
| 115 | 新增: const timeout = setTimeout(stop, 15 * 60 * 1000); | |
| 116 | 新增: process.once('SIGINT', stop); | |
| 117 | 新增: process.once('SIGTERM', stop); | |
| 118 | 新增: let code; | |
| 119 | 新增: try { | |
| 120 | 新增: code = await new Promise((done, reject) => { child.once('error', reject); child.once('close', done); }); | |
| 121 | 新增: controller.abort(); | |
| 122 | 新增: await otpTask; | |
| 123 | 新增: } finally { clearTimeout(timeout); process.off('SIGINT', stop); process.off('SIGTERM', stop); } | |
| 124 | 新增: if (otpError) throw otpError; | |
| 125 | 新增: const output = redact(`${stdout}\n${stderr}`, knownSecrets); | |
| 126 | 新增: if (code !== 0 || /\b(?:Error:|Exception|Missing required option|Unmatched argument)/u.test(output)) { | |
| 127 | 新增: throw new Error(`CodeSignTool failed (exit ${code}). ${output.slice(-2500).trim()}`); | |
| 128 | 新增: } | |
| 129 | 新增: return output; | |
| 130 | 新增: } finally { | |
| 131 | 新增: controller.abort(); | |
| 132 | 新增: child?.kill(); | |
| 133 | 新增: // This also removes vendor logs, which may contain authentication responses. | |
| 134 | 新增: await rm(temp, { recursive: true, force: true }); | |
| 135 | 新增: } | |
| 136 | 新增:} | |
| 137 | 新增: | |
| 138 | 新增:export async function assertWindowsExecutable(path) { | |
| 139 | 新增: if (!(await stat(path)).isFile()) throw new Error('Provide a Windows PE executable.'); | |
| 140 | 新增: // NSIS passes a temporary uninstaller with no .exe suffix. Check the file format. | |
| 141 | 新增: const file = await open(path, 'r'); | |
| 142 | 新增: try { | |
| 143 | 新增: const header = Buffer.alloc(64); | |
| 144 | 新增: const pe = Buffer.alloc(4); | |
| 145 | 新增: const first = await file.read(header, 0, 64, 0); | |
| 146 | 新增: if (first.bytesRead !== 64 || header.readUInt16LE(0) !== 0x5a4d) throw new Error('Input is not a Windows PE executable.'); | |
| 147 | 新增: const signature = await file.read(pe, 0, 4, header.readUInt32LE(60)); | |
| 148 | 新增: if (signature.bytesRead !== 4 || pe.readUInt32LE(0) !== 0x4550) throw new Error('Input is not a Windows PE executable.'); | |
| 149 | 新增: } finally { await file.close(); } | |
| 150 | 新增:} | |
| 151 | 新增: | |
| 152 | 新增:export async function prepareVendorInput(input, stage) { | |
| 153 | 新增: // CodeSignTool chooses its signing format by extension, while NSIS omits it. | |
| 154 | 新增: // Give the vendor a separate .exe copy; keep the caller's filename and bytes intact. | |
| 155 | 新增: const path = join(stage, 'input', 'payload.exe'); | |
| 156 | 新增: const output = join(stage, 'signed'); | |
| 157 | 新增: await mkdir(dirname(path)); | |
| 158 | 新增: await mkdir(output); | |
| 159 | 新增: await copyFile(input, path); | |
| 160 | 新增: return { path, output }; | |
| 161 | 新增:} | |
| 162 | 新增: | |
| 163 | 新增:async function credentials() { | |
| 164 | 新增: const username = process.env.CAGE_ESIGNER_USERNAME || await prompt('SSL.com username'); | |
| 165 | 新增: const password = process.env.CAGE_ESIGNER_PASSWORD || await prompt('SSL.com password', true); | |
| 166 | 新增: const credential = process.env.CAGE_ESIGNER_CREDENTIAL_ID || await prompt('eSigner credential ID'); | |
| 167 | 新增: if (!username || !password || !/^[a-f0-9-]{36}$/iu.test(credential)) throw new Error('A username, password and valid eSigner credential ID are required.'); | |
| 168 | 新增: return { username, password, credential, totp: process.env.CAGE_ESIGNER_TOTP_SECRET }; | |
| 169 | 新增:} | |
| 170 | 新增: | |
| 171 | 新增:async function main(argv) { | |
| 172 | 新增: const [command, input, flag, destination] = argv; | |
| 173 | 新增: if (command === 'install' && argv.length === 1) { console.log(`Verified CodeSignTool ${pin.version}: ${await installTool()}`); return; } | |
| 174 | 新增: if (!['info', 'sign'].includes(command) || (command === 'info' && argv.length !== 1) || | |
| 175 | 新增: (command === 'sign' && (argv.length !== 4 || flag !== '--output-dir'))) { | |
| 176 | 新增: throw new Error('Usage: node scripts/esigner.mjs install | info | sign INPUT.exe --output-dir DIRECTORY'); | |
| 177 | 新增: } | |
| 178 | 新增: if (command === 'sign') await assertWindowsExecutable(resolve(input)); | |
| 179 | 新增: const directory = await installTool(); | |
| 180 | 新增: const auth = await credentials(); | |
| 181 | 新增: if (command === 'sign' && !auth.totp && !process.stdin.isTTY) throw new Error('Non-interactive signing requires CAGE_ESIGNER_TOTP_SECRET.'); | |
| 182 | 新增: const args = [command === 'info' ? 'credential_info' : 'sign', `-username=${auth.username}`, `-password=${auth.password}`, `-credential_id=${auth.credential}`]; | |
| 183 | 新增: let staged, output, vendor; | |
| 184 | 新增: try { | |
| 185 | 新增: if (command === 'sign') { | |
| 186 | 新增: const path = resolve(input); | |
| 187 | 新增: await mkdir(resolve(destination), { recursive: true }); | |
| 188 | 新增: output = join(resolve(destination), basename(path)); | |
| 189 | 新增: try { await access(output); throw new Error('Signed output already exists; use a fresh output directory.'); } | |
| 190 | 新增: catch (error) { if (error.code !== 'ENOENT') throw error; } | |
| 191 | 新增: staged = await mkdtemp(join(resolve(destination), '.sign-')); | |
| 192 | 新增: vendor = await prepareVendorInput(path, staged); | |
| 193 | 新增: args.push(`-input_file_path=${vendor.path}`, `-output_dir_path=${vendor.output}`); | |
| 194 | 新增: if (auth.totp) args.push(`-totp_secret=${auth.totp}`); | |
| 195 | 新增: } | |
| 196 | 新增: const result = await invokeJava({ jar: join(directory, pin.jar), config: join(directory, configPath), arguments: args, | |
| 197 | 新增: otp: signal => prompt('Current six-digit authenticator code', true, signal), secrets: [auth.password, auth.totp] }); | |
| 198 | 新增: if (command === 'sign') { | |
| 199 | 新增: const signed = join(vendor.output, basename(vendor.path)); | |
| 200 | 新增: const bytes = await readFile(signed); | |
| 201 | 新增: if (!bytes.length || hash(bytes) === hash(await readFile(resolve(input)))) throw new Error('CodeSignTool did not produce a changed signed file.'); | |
| 202 | 新增: await copyFile(signed, output, (await import('node:fs')).constants.COPYFILE_EXCL); | |
| 203 | 新增: console.log(`Signed copy: ${output}\nWindows trust and publisher verification are still required.`); | |
| 204 | 新增: } else console.log(result.trim()); | |
| 205 | 新增: } finally { if (staged) await rm(staged, { recursive: true, force: true }); } | |
| 206 | 新增:} | |
| 207 | 新增: | |
| 208 | 新增:if (process.argv[1] && import.meta.url === pathToFileURL(resolve(process.argv[1])).href) { | |
| 209 | 新增: main(process.argv.slice(2)).catch(error => { | |
| 210 | 新增: console.error(redact(error.message, secretNames.map(name => process.env[name]))); | |
| 211 | 新增: process.exitCode = 1; | |
| 212 | 新增: }); | |
| 213 | 新增:} | |
| @@ -0,0 +1,127 @@ | ||
| 1 | 新增:import assert from 'node:assert/strict'; | |
| 2 | 新增:import { execFileSync, spawnSync } from 'node:child_process'; | |
| 3 | 新增:import { mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'; | |
| 4 | 新增:import { join } from 'node:path'; | |
| 5 | 新增:import { tmpdir } from 'node:os'; | |
| 6 | 新增:import { fileURLToPath } from 'node:url'; | |
| 7 | 新增:import { test } from 'node:test'; | |
| 8 | 新增:import { assertWindowsExecutable, invokeJava, javaArgument, prepareVendorInput, redact, verifyTool } from './esigner.mjs'; | |
| 9 | 新增: | |
| 10 | 新增:test('Java arguments reject line injection and redact overlapping secrets', () => { | |
| 11 | 新增: for (const value of ['line\nbreak', 'line\rbreak', 'a\0b']) assert.throws(() => javaArgument(value)); | |
| 12 | 新增: assert.equal(redact('secret-long and secret', ['secret', 'secret-long']), '[redacted] and [redacted]'); | |
| 13 | 新增:}); | |
| 14 | 新增: | |
| 15 | 新增:test('a real JVM receives quoted Unicode arguments, no secrets in process arguments, and OTP through stdin', async t => { | |
| 16 | 新增: const temp = await mkdtemp(join(tmpdir(), 'cage-esigner-test-')); | |
| 17 | 新增: t.after(() => rm(temp, { recursive: true, force: true })); | |
| 18 | 新增: const source = join(temp, 'Signer.java'); | |
| 19 | 新增: const config = join(temp, 'config.properties'); | |
| 20 | 新增: await writeFile(config, 'TEST=true\n'); | |
| 21 | 新增: await writeFile(source, ` | |
| 22 | 新增:import java.nio.file.*; | |
| 23 | 新增:import java.util.*; | |
| 24 | 新增:public class Signer { | |
| 25 | 新增: public static void main(String[] args) throws Exception { | |
| 26 | 新增: String temp = System.getProperty("user.dir"); | |
| 27 | 新增: Files.writeString(Path.of(args[0]), temp); | |
| 28 | 新增: String command = ProcessHandle.current().info().commandLine().orElse(""); | |
| 29 | 新增: if (!command.contains("@") || command.contains("password=") || command.contains("totp_secret=")) System.exit(13); | |
| 30 | 新增: if (System.getenv("CAGE_ESIGNER_PASSWORD") != null || System.getenv("CAGE_ESIGNER_TOTP_SECRET") != null) System.exit(14); | |
| 31 | 新增: if (!Files.readString(Path.of("conf", "code_sign_tool.properties")).contains("TEST=true")) System.exit(15); | |
| 32 | 新增: for (int i = 1; i < args.length; i++) System.out.println("ARG[" + i + "]=" + args[i]); | |
| 33 | 新增: Files.createDirectories(Path.of("logs")); | |
| 34 | 新增: Files.writeString(Path.of("logs", "sensitive.log"), "must be removed"); | |
| 35 | 新增: System.out.print("Enter the OTP - Press enter to continue:"); | |
| 36 | 新增: System.out.flush(); | |
| 37 | 新增: String otp = new Scanner(System.in).nextLine(); | |
| 38 | 新增: if (!otp.equals("123456")) System.exit(16); | |
| 39 | 新增: System.out.println(otp); | |
| 40 | 新增: if (Arrays.asList(args).contains("fail")) { System.out.println("Error: dummy-password"); System.exit(4); } | |
| 41 | 新增: } | |
| 42 | 新增:} | |
| 43 | 新增:`); | |
| 44 | 新增: execFileSync('javac', ['-d', temp, source]); | |
| 45 | 新增: const jar = join(temp, 'fixture.jar'); | |
| 46 | 新增: execFileSync('jar', ['--create', '--file', jar, '--main-class', 'Signer', '-C', temp, 'Signer.class']); | |
| 47 | 新增: const values = [' spaces ', 'quote"slash\\tab\t', '中文路径/空 格.exe', '#hash', '@value', '-password=dummy-password']; | |
| 48 | 新增: for (const fail of [false, true]) { | |
| 49 | 新增: const marker = join(temp, fail ? 'failure-marker' : 'success-marker'); | |
| 50 | 新增: let calls = 0; | |
| 51 | 新增: const previous = process.env.CAGE_ESIGNER_PASSWORD; | |
| 52 | 新增: process.env.CAGE_ESIGNER_PASSWORD = 'dummy-password'; | |
| 53 | 新增: try { | |
| 54 | 新增: const operation = invokeJava({ jar, config, arguments: [marker, ...values, ...(fail ? ['fail'] : [])], | |
| 55 | 新增: otp: async () => { calls++; return '123456'; }, secrets: ['dummy-password'] }); | |
| 56 | 新增: if (fail) { | |
| 57 | 新增: await assert.rejects(operation, error => !error.message.includes('dummy-password') && !error.message.includes('123456') && /exit 4/u.test(error.message)); | |
| 58 | 新增: } else { | |
| 59 | 新增: const output = await operation; | |
| 60 | 新增: for (const [i, value] of values.entries()) assert.ok(output.includes(`ARG[${i + 1}]=${redact(value, ['dummy-password'])}`)); | |
| 61 | 新增: assert.ok(!output.includes('123456')); | |
| 62 | 新增: } | |
| 63 | 新增: assert.equal(calls, 1); | |
| 64 | 新增: const working = await readFile(marker, 'utf8'); | |
| 65 | 新增: await assert.rejects(readFile(join(working, 'java.args')), { code: 'ENOENT' }); | |
| 66 | 新增: await assert.rejects(readFile(join(working, 'logs/sensitive.log')), { code: 'ENOENT' }); | |
| 67 | 新增: } finally { | |
| 68 | 新增: if (previous === undefined) delete process.env.CAGE_ESIGNER_PASSWORD; | |
| 69 | 新增: else process.env.CAGE_ESIGNER_PASSWORD = previous; | |
| 70 | 新增: } | |
| 71 | 新增: } | |
| 72 | 新增:}); | |
| 73 | 新增: | |
| 74 | 新增:test('modified vendor binaries are refused before executing Java', async t => { | |
| 75 | 新增: const temp = await mkdtemp(join(tmpdir(), 'cage-esigner-tampered-')); | |
| 76 | 新增: t.after(() => rm(temp, { recursive: true, force: true })); | |
| 77 | 新增: const { mkdir } = await import('node:fs/promises'); | |
| 78 | 新增: await mkdir(join(temp, 'jar')); | |
| 79 | 新增: await writeFile(join(temp, 'jar/code_sign_tool-1.3.3.jar'), 'modified'); | |
| 80 | 新增: await assert.rejects(verifyTool(temp), /checksum mismatch/u); | |
| 81 | 新增:}); | |
| 82 | 新增: | |
| 83 | 新增:test('NSIS temporary executables without an extension are accepted, unrelated files are refused', async t => { | |
| 84 | 新增: const temp = await mkdtemp(join(tmpdir(), 'cage-esigner-pe-')); | |
| 85 | 新增: t.after(() => rm(temp, { recursive: true, force: true })); | |
| 86 | 新增: const path = join(temp, 'makensis123'); | |
| 87 | 新增: const bytes = Buffer.alloc(132); | |
| 88 | 新增: bytes.writeUInt16LE(0x5a4d, 0); | |
| 89 | 新增: bytes.writeUInt32LE(128, 60); | |
| 90 | 新增: bytes.writeUInt32LE(0x4550, 128); | |
| 91 | 新增: await writeFile(path, bytes); | |
| 92 | 新增: await assertWindowsExecutable(path); | |
| 93 | 新增: const prepared = await prepareVendorInput(path, temp); | |
| 94 | 新增: assert.ok(prepared.path.endsWith('.exe'), 'The vendor requires an extension to choose Authenticode.'); | |
| 95 | 新增: assert.deepEqual(await readFile(prepared.path), bytes); | |
| 96 | 新增: assert.deepEqual(await readFile(path), bytes); | |
| 97 | 新增: bytes.writeUInt32LE(0, 128); | |
| 98 | 新增: await writeFile(path, bytes); | |
| 99 | 新增: await assert.rejects(assertWindowsExecutable(path), /not a Windows PE/u); | |
| 100 | 新增: await writeFile(path, 'plain text'); | |
| 101 | 新增: await assert.rejects(assertWindowsExecutable(path), /not a Windows PE/u); | |
| 102 | 新增:}); | |
| 103 | 新增: | |
| 104 | 新增:test('release readiness accepts eSigner without a PFX, requires all secrets and rejects unknown providers', () => { | |
| 105 | 新增: const script = fileURLToPath(new URL('./check-release-readiness.mjs', import.meta.url)); | |
| 106 | 新增: const env = { | |
| 107 | 新增: ...process.env, | |
| 108 | 新增: CAGE_MACOS_P12_BASE64: 'fixture', CAGE_MACOS_P12_PASSWORD: 'fixture', CAGE_MACOS_SIGNING_IDENTITY: 'Developer ID Application: Fixture', | |
| 109 | 新增: CAGE_MACOS_NOTARY_KEY_BASE64: 'fixture', CAGE_MACOS_NOTARY_KEY_ID: 'fixture', CAGE_MACOS_NOTARY_ISSUER_ID: 'fixture', | |
| 110 | 新增: CAGE_WINDOWS_QEMU_SOURCE_URL: 'https://example.invalid/source.zip', CAGE_WINDOWS_QEMU_SOURCE_SHA256: 'a'.repeat(64), | |
| 111 | 新增: CAGE_WINDOWS_SIGNING_PROVIDER: 'esigner', CAGE_ESIGNER_USERNAME: 'fixture', CAGE_ESIGNER_PASSWORD: 'private-fixture-password', | |
| 112 | 新增: CAGE_ESIGNER_CREDENTIAL_ID: '11111111-2222-3333-4444-555555555555', CAGE_ESIGNER_TOTP_SECRET: 'private-fixture-seed', | |
| 113 | 新增: CAGE_ESIGNER_CERTIFICATE_SERIAL: '7'.repeat(32), CAGE_WINDOWS_PFX_BASE64: '', CAGE_WINDOWS_PFX_PASSWORD: '', | |
| 114 | 新增: }; | |
| 115 | 新增: const run = change => spawnSync(process.execPath, [script], { env: { ...env, ...change }, encoding: 'utf8' }); | |
| 116 | 新增: assert.equal(run({}).status, 0); | |
| 117 | 新增: for (const name of ['CAGE_ESIGNER_USERNAME', 'CAGE_ESIGNER_PASSWORD', 'CAGE_ESIGNER_CREDENTIAL_ID', 'CAGE_ESIGNER_TOTP_SECRET', 'CAGE_ESIGNER_CERTIFICATE_SERIAL']) { | |
| 118 | 新增: const result = run({ [name]: '' }); | |
| 119 | 新增: assert.equal(result.status, 1); | |
| 120 | 新增: assert.ok(result.stderr.includes(name)); | |
| 121 | 新增: assert.ok(!result.stderr.includes('private-fixture-password') && !result.stderr.includes('private-fixture-seed')); | |
| 122 | 新增: } | |
| 123 | 新增: assert.equal(run({ CAGE_WINDOWS_SIGNING_PROVIDER: 'typo' }).status, 1); | |
| 124 | 新增: assert.equal(run({ CAGE_ESIGNER_CERTIFICATE_SERIAL: 'bad' }).status, 1); | |
| 125 | 新增: assert.equal(run({ CAGE_WINDOWS_SIGNING_PROVIDER: '', CAGE_WINDOWS_PFX_BASE64: 'fixture', CAGE_WINDOWS_PFX_PASSWORD: 'fixture' }).status, 0); | |
| 126 | 新增: assert.equal(run({ CAGE_WINDOWS_SIGNING_PROVIDER: '' }).status, 1); | |
| 127 | 新增:}); | |
| @@ -27,7 +27,8 @@ if ($IsWindows) { | ||
| 27 | 27 | } |
| 28 | 28 | $signTool = $null |
| 29 | 29 | if ($Release) { |
| 30 | 删除: if (-not $IsWindows -or $env:CAGE_WINDOWS_CERT_SHA1 -notmatch '^[0-9A-Fa-f]{40}$') { throw 'Release packaging requires Windows and CAGE_WINDOWS_CERT_SHA1.' } | |
| 30 | 新增: Assert-WindowsSigningConfiguration | |
| 31 | 新增: if ($env:CAGE_WINDOWS_CERT_SHA1 -notmatch '^[0-9A-Fa-f]{40}$') { throw 'Build and verify the signed app before packaging.' } | |
| 31 | 32 | if (Test-Path -LiteralPath (Join-Path $AppDirectory 'DEVELOPMENT-BUILD.txt')) { throw 'Use build-windows.ps1 -Release before packaging a release.' } |
| 32 | 33 | if (-not $QemuSourceArchive -or -not (Test-Path -LiteralPath $QemuSourceArchive -PathType Leaf)) { |
| 33 | 34 | throw 'Release packaging requires -QemuSourceArchive containing the corresponding patched QEMU and bundled library sources. A generic upstream QEMU tarball is not the source for the Weilnetz build.' |
| @@ -62,7 +63,9 @@ foreach ($directory in (Get-ChildItem -LiteralPath $AppDirectory -Directory -Rec | ||
| 62 | 63 | $numericVersion = ($version -split '-')[0] + '.0' |
| 63 | 64 | $prefix = if ($IsWindows) { '/' } else { '-' } |
| 64 | 65 | $arguments = @("${prefix}V3", "${prefix}DAPP_DIR=$AppDirectory", "${prefix}DVERSION=$version", "${prefix}DVERSION_NUMERIC=$numericVersion", "${prefix}DOUTPUT_FILE=$artifact", "${prefix}DUNINSTALL_MANIFEST=$manifest") |
| 65 | 删除:if ($Release) { $arguments += @("${prefix}DSIGNTOOL=$signTool", "${prefix}DCERT_SHA1=$env:CAGE_WINDOWS_CERT_SHA1") } | |
| 66 | 新增:if ($Release) { | |
| 67 | 新增: $arguments += @("${prefix}DSIGN_PWSH=$((Get-Process -Id $PID).Path)", "${prefix}DSIGN_SCRIPT=$(Join-Path $PSScriptRoot 'sign-windows-file.ps1')") | |
| 68 | 新增:} | |
| 66 | 69 | $arguments += (Join-Path $repository 'packaging/windows/Cage.nsi') |
| 67 | 70 | Invoke-BuildTool $makeNSIS $arguments |
| 68 | 71 | if ($Release) { Sign-CageFile $artifact $signTool } |
| @@ -0,0 +1,7 @@ | ||
| 1 | 新增:#requires -Version 7.0 | |
| 2 | 新增:# Called by NSIS for the generated uninstaller; contains no credentials. | |
| 3 | 新增:[CmdletBinding()] | |
| 4 | 新增:param([Parameter(Mandatory)][string]$Path) | |
| 5 | 新增:. (Join-Path $PSScriptRoot 'windows-common.ps1') | |
| 6 | 新增:try { Sign-CageFile $Path (Get-SigningTool) } | |
| 7 | 新增:catch { Write-Error $_; exit 1 } | |
| @@ -0,0 +1,48 @@ | ||
| 1 | 新增:#requires -Version 7.0 | |
| 2 | 新增:# Offline orchestration checks; Windows trust is exercised separately on Windows. | |
| 3 | 新增:. (Join-Path $PSScriptRoot 'windows-common.ps1') | |
| 4 | 新增:$stage = Join-Path ([IO.Path]::GetTempPath()) ('cage-signing-test-' + [guid]::NewGuid().ToString('N')) | |
| 5 | 新增:New-Item -ItemType Directory -Path $stage | Out-Null | |
| 6 | 新增:$names = @('CAGE_WINDOWS_SIGNING_PROVIDER', 'CAGE_ESIGNER_CERTIFICATE_SERIAL', 'CAGE_WINDOWS_CERT_SHA1') | |
| 7 | 新增:$saved = @{} | |
| 8 | 新增:foreach ($name in $names) { $saved[$name] = [Environment]::GetEnvironmentVariable($name) } | |
| 9 | 新增:try { | |
| 10 | 新增: $env:CAGE_WINDOWS_SIGNING_PROVIDER = 'esigner' | |
| 11 | 新增: $env:CAGE_ESIGNER_CERTIFICATE_SERIAL = '7' * 32 | |
| 12 | 新增: function Assert-WindowsSigningConfiguration {} | |
| 13 | 新增: function Get-BuildCommand { return 'fixture-node' } | |
| 14 | 新增: $script:mode = 'valid' | |
| 15 | 新增: function Invoke-BuildTool { | |
| 16 | 新增: param([string]$Tool, [string[]]$Arguments) | |
| 17 | 新增: if ($Tool -eq 'fixture-node') { | |
| 18 | 新增: New-Item -ItemType Directory -Path $Arguments[4] -Force | Out-Null | |
| 19 | 新增: [IO.File]::WriteAllText((Join-Path $Arguments[4] ([IO.Path]::GetFileName($Arguments[2]))), 'signed fixture') | |
| 20 | 新增: } elseif ($script:mode -eq 'untrusted') { throw 'Fixture trust failure' } | |
| 21 | 新增: } | |
| 22 | 新增: function Get-AuthenticodeSignature { | |
| 23 | 新增: return [pscustomobject]@{ | |
| 24 | 新增: Status = 'Valid' | |
| 25 | 新增: TimeStamperCertificate = if ($script:mode -eq 'no-timestamp') { $null } else { 'fixture' } | |
| 26 | 新增: SignerCertificate = [pscustomobject]@{ | |
| 27 | 新增: SerialNumber = if ($script:mode -eq 'wrong-serial') { '8' * 32 } else { '7' * 32 } | |
| 28 | 新增: Thumbprint = if ($script:mode -eq 'wrong-key') { 'b' * 40 } else { 'a' * 40 } | |
| 29 | 新增: } | |
| 30 | 新增: } | |
| 31 | 新增: } | |
| 32 | 新增: $inputFile = Join-Path $stage 'Cage 空格.exe' | |
| 33 | 新增: foreach ($script:mode in @('valid', 'untrusted', 'no-timestamp', 'wrong-serial', 'wrong-key')) { | |
| 34 | 新增: [IO.File]::WriteAllText($inputFile, 'original fixture') | |
| 35 | 新增: $env:CAGE_WINDOWS_CERT_SHA1 = 'a' * 40 | |
| 36 | 新增: $rejected = $false | |
| 37 | 新增: try { Sign-CageFile $inputFile 'fixture-signtool' } catch { $rejected = $true } | |
| 38 | 新增: if ($script:mode -eq 'valid') { | |
| 39 | 新增: if ($rejected -or [IO.File]::ReadAllText($inputFile) -ne 'signed fixture') { throw 'Valid signed copy was not promoted.' } | |
| 40 | 新增: } else { | |
| 41 | 新增: if (-not $rejected -or [IO.File]::ReadAllText($inputFile) -ne 'original fixture') { throw "Original file was replaced on $script:mode." } | |
| 42 | 新增: } | |
| 43 | 新增: } | |
| 44 | 新增: Write-Host 'Passed: valid output promoted; untrusted, untimestamped, wrong-serial and wrong-key outputs rejected without replacing originals.' | |
| 45 | 新增:} finally { | |
| 46 | 新增: foreach ($name in $names) { [Environment]::SetEnvironmentVariable($name, $saved[$name]) } | |
| 47 | 新增: Remove-Item -LiteralPath $stage -Recurse -Force | |
| 48 | 新增:} | |
| @@ -91,9 +91,43 @@ function Get-SigningTool { | ||
| 91 | 91 | throw 'Release signing requires Windows SDK signtool.exe on Windows.' |
| 92 | 92 | } |
| 93 | 93 | |
| 94 | 新增:function Assert-WindowsSigningConfiguration { | |
| 95 | 新增: if (-not $IsWindows) { throw 'Trusted Windows build verification requires Windows.' } | |
| 96 | 新增: $null = Get-SigningTool | |
| 97 | 新增: if ($env:CAGE_WINDOWS_SIGNING_PROVIDER -eq 'esigner') { | |
| 98 | 新增: if ($env:CAGE_ESIGNER_CREDENTIAL_ID -notmatch '^[0-9A-Fa-f-]{36}$' -or | |
| 99 | 新增: $env:CAGE_ESIGNER_CERTIFICATE_SERIAL -notmatch '^[0-9A-Fa-f]{32,40}$') { | |
| 100 | 新增: throw 'eSigner requires CAGE_ESIGNER_CREDENTIAL_ID and the expected CAGE_ESIGNER_CERTIFICATE_SERIAL.' | |
| 101 | 新增: } | |
| 102 | 新增: } elseif ($env:CAGE_WINDOWS_SIGNING_PROVIDER -and $env:CAGE_WINDOWS_SIGNING_PROVIDER -ne 'certificate-store') { | |
| 103 | 新增: throw 'Unknown CAGE_WINDOWS_SIGNING_PROVIDER; use esigner or certificate-store.' | |
| 104 | 新增: } elseif ($env:CAGE_WINDOWS_CERT_SHA1 -notmatch '^[0-9A-Fa-f]{40}$') { | |
| 105 | 新增: throw 'Certificate-store signing requires CAGE_WINDOWS_CERT_SHA1.' | |
| 106 | 新增: } | |
| 107 | 新增:} | |
| 108 | 新增: | |
| 94 | 109 | function Sign-CageFile { |
| 95 | 110 | param([string]$Path, [string]$SignTool) |
| 96 | 删除: Invoke-BuildTool $SignTool @('sign', '/sha1', $env:CAGE_WINDOWS_CERT_SHA1, '/fd', 'SHA256', | |
| 97 | 删除: '/tr', 'http://timestamp.digicert.com', '/td', 'SHA256', '/d', 'Cage', $Path) | |
| 98 | 删除: Invoke-BuildTool $SignTool @('verify', '/pa', '/all', $Path) | |
| 111 | 新增: Assert-WindowsSigningConfiguration | |
| 112 | 新增: if ($env:CAGE_WINDOWS_SIGNING_PROVIDER -eq 'esigner') { | |
| 113 | 新增: $Path = [IO.Path]::GetFullPath($Path) | |
| 114 | 新增: $stage = Join-Path ([IO.Path]::GetTempPath()) ('cage-signed-' + [guid]::NewGuid().ToString('N')) | |
| 115 | 新增: try { | |
| 116 | 新增: Invoke-BuildTool (Get-BuildCommand @('node.exe', 'node')) @((Join-Path $PSScriptRoot 'esigner.mjs'), 'sign', $Path, '--output-dir', $stage) | |
| 117 | 新增: $signed = Join-Path $stage ([IO.Path]::GetFileName($Path)) | |
| 118 | 新增: Invoke-BuildTool $SignTool @('verify', '/pa', '/all', '/tw', $signed) | |
| 119 | 新增: $signature = Get-AuthenticodeSignature -LiteralPath $signed | |
| 120 | 新增: if ($signature.Status -ne 'Valid' -or -not $signature.TimeStamperCertificate -or | |
| 121 | 新增: $signature.SignerCertificate.SerialNumber -ne $env:CAGE_ESIGNER_CERTIFICATE_SERIAL -or | |
| 122 | 新增: ($env:CAGE_WINDOWS_CERT_SHA1 -and $signature.SignerCertificate.Thumbprint -ne $env:CAGE_WINDOWS_CERT_SHA1)) { | |
| 123 | 新增: throw 'eSigner output lacks the expected trusted publisher certificate or timestamp. Original file retained.' | |
| 124 | 新增: } | |
| 125 | 新增: $env:CAGE_WINDOWS_CERT_SHA1 = $signature.SignerCertificate.Thumbprint | |
| 126 | 新增: Copy-Item -LiteralPath $signed -Destination $Path -Force | |
| 127 | 新增: } finally { if (Test-Path -LiteralPath $stage) { Remove-Item -LiteralPath $stage -Recurse -Force } } | |
| 128 | 新增: } else { | |
| 129 | 新增: Invoke-BuildTool $SignTool @('sign', '/sha1', $env:CAGE_WINDOWS_CERT_SHA1, '/fd', 'SHA256', | |
| 130 | 新增: '/tr', 'http://timestamp.digicert.com', '/td', 'SHA256', '/d', 'Cage', $Path) | |
| 131 | 新增: Invoke-BuildTool $SignTool @('verify', '/pa', '/all', $Path) | |
| 132 | 新增: } | |
| 99 | 133 | } |