Skip to content
Tauri 中文网

Windows 代码签名

在 Windows 上,需要进行代码签名以允许你的应用在 Microsoft Store 上列出,并防止当从浏览器下载时出现 SmartScreen 警告,提示你的应用不受信任且无法启动。

🌐 Code signing is required on Windows to allow your application to be listed in the Microsoft Store and to prevent a SmartScreen warning that your application is not trusted and can not be started, when downloaded from the browser.

只要终端用户可以忽略 SmartScreen 警告,或者用户不是通过浏览器下载,你的应用就不必在 Windows 上执行。本指南涵盖通过 OV(组织验证)证书和 Azure 密钥保管库进行签名。如果你使用本文未记录的其他签名机制,例如 EV(扩展验证)证书,请查阅你的证书发行机构文档,并参考 自定义签名命令 部分。

🌐 It is not required to execute your application on Windows, as long as your end user is okay with ignoring the SmartScreen warning or your user does not download via the browser. This guide covers signing via OV (Organization Validated) certificates and Azure Key Vault. If you use any other signing mechanism not documented here, such as EV (Extended Validation) certificates, check out your certificate issuer documentation and refer to the custom sign command section.

🌐 OV Certificates

🌐 Prerequisites

  • Windows - 你可能可以使用其他平台,但本教程使用的是 PowerShell 的原生功能。
  • 一个有效的 Tauri 应用
  • 代码签名证书 - 你可以通过 [Microsoft 的文档] 中列出的服务获取其中之一。对于非 EV 证书,可能还有其他颁发机构不在该列表中,请你自行比较并自行承担选择的风险。
    • 请务必获取一个代码签名证书,SSL 证书不起作用!

🌐 Getting Started

我们有一些事情需要做,以准备 Windows 进行代码签名。这包括将我们的证书转换为特定格式、安装该证书,以及从证书中解码所需的信息。

🌐 There are a few things we have to do to get Windows prepared for code signing. This includes converting our certificate to a specific format, installing this certificate, and decoding the required information from the certificate.

    • 你将需要以下物品:
      • 证书文件(我的的是 cert.cer
      • 私钥文件(我的的是 private-key.key
    • 打开命令提示符并使用 cd Documents/Certs 切换到你当前的目录
    • 使用 openssl pkcs12 -export -in cert.cer -inkey private-key.key -out certificate.pfx 将你的 .cer 转换为 .pfx
    • 系统将提示你输入导出密码 别忘了它!
    • 我们现在需要导入我们的 .pfx 文件。
    • 使用 $WINDOWS_PFX_PASSWORD = 'MYPASSWORD' 将你的导出密码分配给一个变量
    • 现在使用 Import-PfxCertificate -FilePath certificate.pfx -CertStoreLocation Cert:\CurrentUser\My -Password (ConvertTo-SecureString -String $WINDOWS_PFX_PASSWORD -Force -AsPlainText) 导入证书
    • 开始 ➡️ certmgr.msc 打开个人证书管理,然后打开个人/证书。
    • 找到我们刚刚导入的证书并双击它,然后点击“详细信息”选项卡。
    • 签名哈希算法将是我们的 digestAlgorithm。(提示:这很可能是 sha256
    • 向下滚动到指纹部分。应该会有一个类似 A1B1A2B2A3B3A4B4A5B5A6B6A7B7A8B8A9B9A0B0 的值。这就是我们的 certificateThumbprint
    • 我们还需要一个时间戳 URL;这是一个用于验证证书签名时间的时间服务器。我正在使用 http://timestamp.comodoca.com,但无论你从谁那里获得你的证书,他们可能也有一个。

🌐 Prepare tauri.conf.json file

  1. 现在我们有了 certificateThumbprintdigestAlgorithmtimestampUrl,我们将打开 tauri.conf.json
  2. tauri.conf.json 中,你需要查找 tauri -> bundle -> windows 部分。我们已经捕获的信息有三个变量。像下面这样填写。
"windows": {
"certificateThumbprint": "A1B1A2B2A3B3A4B4A5B5A6B6A7B7A8B8A9B9A0B0",
"digestAlgorithm": "sha256",
"timestampUrl": "http://timestamp.comodoca.com"
}
  1. 保存并运行 tauri build
  2. 在控制台输出中,你应该看到以下输出。
info: signing app
info: running signtool "C:\\Program Files (x86)\\Windows Kits\\10\\bin\\10.0.19041.0\\x64\\signtool.exe"
info: "Done Adding Additional Store\r\nSuccessfully signed: APPLICATION FILE PATH HERE

这表明你已成功签署了 .exe

🌐 Which shows you have successfully signed the .exe.

就是这样!你已经成功为 Windows 设置了 Tauri 应用的签名。

🌐 And that’s it! You have successfully set up your Tauri application for Windows signing.

🌐 Sign your application with GitHub Actions.

我们还可以创建一个工作流,使用 GitHub 操作对应用进行签名。

🌐 We can also create a workflow to sign the application with GitHub actions.

🌐 GitHub Secrets

我们需要添加一些 GitHub 密钥,以便正确配置 GitHub Action。这些密钥可以按照你喜欢的方式命名。

🌐 We need to add a few GitHub secrets for the proper configuration of the GitHub Action. These can be named however you would like.

  • 你可以查看关于如何添加 GitHub secrets 的[加密秘密]指南。

我们使用的秘密如下

🌐 The secrets we used are as follows

GitHub 秘密变量的值
WINDOWS_CERTIFICATE你的 .pfx 证书的 Base64 编码版本,可以使用此命令 certutil -encode certificate.pfx base64cert.txt 来完成
WINDOWS_CERTIFICATE_PASSWORD创建 .pfx 证书时使用的证书导出密码

🌐 Workflow Modifications

  1. 我们需要在工作流程中添加一个步骤,将证书导入到 Windows 环境中。此工作流程实现以下内容
    1. 将 GitHub 密钥分配给环境变量
    2. 创建一个新的 certificate 目录
    3. WINDOWS_CERTIFICATE 导入到 tempCert.txt
    4. 使用 certutil 将 tempCert.txt 从 base64 解码为 .pfx 文件。
    5. 删除 tempCert.txt
    6. .pfx 文件导入到 Windows 的证书存储中,并将 WINDOWS_CERTIFICATE_PASSWORD 转换为可在导入命令中使用的安全字符串。
  2. 我们将使用 [tauri-action 发布模板]。
name: 'publish'
on:
push:
branches:
- release
jobs:
publish-tauri:
strategy:
fail-fast: false
matrix:
platform: [macos-latest, ubuntu-latest, windows-latest]
runs-on: ${{ matrix.platform }}
steps:
- uses: actions/checkout@v2
- name: setup node
uses: actions/setup-node@v1
with:
node-version: 12
- name: install Rust stable
uses: actions-rs/toolchain@v1
with:
toolchain: stable
- name: install webkit2gtk (ubuntu only)
if: matrix.platform == 'ubuntu-latest'
run: |
sudo apt-get update
sudo apt-get install -y webkit2gtk-4.0
- name: install app dependencies and build it
run: yarn && yarn build
- uses: tauri-apps/tauri-action@v0
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tagName: app-v__VERSION__ # the action automatically replaces \_\_VERSION\_\_ with the app version
releaseName: 'App v__VERSION__'
releaseBody: 'See the assets to download this version and install.'
releaseDraft: true
prerelease: false
  1. -name: install app dependencies and build it 的正上方,你需要添加以下步骤
- name: import windows certificate
if: matrix.platform == 'windows-latest'
env:
WINDOWS_CERTIFICATE: ${{ secrets.WINDOWS_CERTIFICATE }}
WINDOWS_CERTIFICATE_PASSWORD: ${{ secrets.WINDOWS_CERTIFICATE_PASSWORD }}
run: |
New-Item -ItemType directory -Path certificate
Set-Content -Path certificate/tempCert.txt -Value $env:WINDOWS_CERTIFICATE
certutil -decode certificate/tempCert.txt certificate/certificate.pfx
Remove-Item -path certificate -include tempCert.txt
Import-PfxCertificate -FilePath certificate/certificate.pfx -CertStoreLocation Cert:\CurrentUser\My -Password (ConvertTo-SecureString -String $env:WINDOWS_CERTIFICATE_PASSWORD -Force -AsPlainText)
  1. 保存并推送到你的存储库。
  2. 你的工作流程现在可以导入你的 Windows 证书并将其导入 GitHub 运行器,从而实现自动代码签名!

你可以通过提供 Azure Key Vault 证书和凭据来签署 Windows 可执行文件。

🌐 You can sign the Windows executables by providing an Azure Key Vault certificate and credentials.

  1. Key Vault

在 [Azure 门户] 中,导航到 [密钥保管服务],通过点击“创建”按钮来创建一个新的密钥保管。记住“密钥保管名称”,因为你需要该信息来配置证书 URL。

🌐 In the Azure Portal navigate to the Key vaults service to create a new key vault by clicking the “Create” button. Remember the “Key vault name” as you will need that information to configure the certificate URL.

  1. 证书

创建密钥保管库后,选择它并转到“对象 > 证书”页面以创建新证书,然后点击“生成/导入”按钮。记住“证书名称”,因为你需要该信息来配置证书 URL。

🌐 After creating a key vault, select it and go to the “Objects > Certificates” page to create a new certificate and click the “Generate/Import” button. Remember the “Certificate name” as you will need that information to configure the certificate URL.

  1. Tauri 配置

relic 使用配置文件来确定应使用哪个签名密钥。对于 Azure Key Vault,你还需要证书的 URL。 在 src-tauri 文件夹中创建一个 relic.conf 文件,并配置 relic 使用你的证书:

src-tauri/relic.conf
tokens:
azure:
type: azure
keys:
azure:
token: azure
id: https://\<KEY_VAULT_NAME\>.vault.azure.net/certificates/\<CERTIFICATE_NAME\>

注意,你必须用 <KEY_VAULT_NAME> 和 <CERTIFICATE_NAME> 前面步骤中合适的名称替换。

要配置 Tauri 使用你的 Azure 密钥保管配置进行签名,请更改 bundle > windows > signCommand 配置值:

🌐 To configure Tauri to use your Azure Key Vault configuration for signing change the bundle > windows > signCommand config value:

tauri.conf.json
{
"bundle": {
"windows": {
"signCommand": "relic sign --file %1 --key azure --config relic.conf"
}
}
}
  1. 凭据

relic 必须使用 Azure 进行身份验证才能加载证书。
在 Azure 门户主页上,转到“Microsoft Entra ID”服务,然后进入“管理 > 应用注册”页面。
点击“新建注册”以创建一个新应用。创建应用后,你将被重定向到应用详情页面,在那里可以看到“应用(客户端)ID”和“目录(租户)ID”的值。
将这些 ID 分别设置到 AZURE_CLIENT_IDAZURE_TENANT_ID 环境变量中。

在“管理 > 证书和密钥”页面上点击“新建客户端密钥”按钮,并将“值”列中的文本设置为 AZURE_CLIENT_SECRET 环境变量。

🌐 In the “Manage > Certificates & secrets” page click the “New client secret” button and set the text in the “Value” column as the AZURE_CLIENT_SECRET environment variable.

设置好所有凭据后,返回到你的密钥保管库页面,并导航到“访问控制(IAM)”页面。你必须将“密钥保管证书用户”和“密钥保管加密用户”角色分配给你新创建的应用。

🌐 After setting up all the credentials, head back to your key vault’s page and navigate to the “Access control (IAM)” page. You must assign the “Key Vault Certificate User” and “Key Vault Crypto User” roles to your newly created application.

在设置好所有这些变量后,运行 tauri build 将生成带签名的 Windows 安装程序!

🌐 After setting up all these variables, running tauri build will produce signed Windows installers!

🌐 Custom Sign Command

在上面的 Azure Key Vault 文档中,我们使用了一个强大的 Tauri Windows 签名配置来强制 Tauri CLI 使用特殊的 shell 命令来签署 Windows 安装程序可执行文件。配置选项 bundle > windows > signCommand 可用于使用任何可以签署 Windows 可执行文件的代码签名工具。

🌐 In the Azure Key Vault documentation above we used a powerful Tauri Windows signing configuration to force the Tauri CLI to use a special shell command to sign Windows installer executables. The bundle > windows > signCommand configuration option can be used to use any codesign tool that can sign Windows executables.

🌐 Azure Code Signing

你可以通过提供 Azure 代码签名证书和凭据来签署 Windows 可执行文件。如果你还没有 Azure 代码签名账户,可以按照这个 教程 进行操作。

🌐 You can sign the Windows executables by providing an Azure Code signing certificate and credentials. If you don’t have an Azure Code signing Account yet you can follow this tutorial.

🌐 Prerequisites

如果你想使用 Github Actions 签名,则应该安装所有内容。

🌐 If you want to sign with Github Actions everything should be installed.

  1. 受信任的签名账户 和已配置的权限
  2. .NET(建议使用 .NET 6 或更高版本)
  3. Azure CLI
  4. Signtool(推荐使用 Windows 11 SDK 10.0.22000.0 或更高版本)

🌐 Getting Started

你需要安装 trusted-signing-cli 并配置你的环境变量。

🌐 You need to install trusted-signing-cli and configure your environment variables.

    • cargo install trusted-signing-cli
    • trusted-signing-cli 需要设置以下环境变量,别忘了将它们作为 Github Actions secrets 添加:
      • AZURE_CLIENT_ID:你的 应用注册 的客户端 ID
      • AZURE_CLIENT_SECRET应用注册 的客户端密钥
      • AZURE_TENANT_ID:你的 Azure 目录的租户 ID,你也可以从你的 应用注册 获取
    • 你可以修改你的 tauri.conf.json,或者为 Windows 创建一个特定的配置文件。将 URL 和证书名称替换为你自己的值。
      • -e:你的 Azure 代码签名账户的终端节点
      • -a:你的 Azure 代码签名账户名称
      • -c:你在 Azure 代码签名账户中的证书配置文件名称
      • -d: 签名内容的描述(可选)。在签署 .msi 安装程序时,该描述将显示为 UAC 提示中的安装程序名称,如果未设置,则会显示为随机字符。
    tauri.conf.json
    {
    "bundle": {
    "windows": {
    "signCommand": "trusted-signing-cli -e https://wus2.codesigning.azure.net -a MyAccount -c MyProfile -d MyApp %1"
    }
    }
    }

Tauri 中文网 - 粤ICP备13048890号
Nodejs.cn 旗下网站