Windows - 本機和 GitHub Actions 的程式碼簽署指南
簡介
為應用程式進行程式碼簽署,讓使用者知道他們下載的是應用程式的官方可執行檔,而不是偽裝成應用程式的第三方惡意軟體。雖然這不是必要的,但它能提升使用者對應用程式的信心。
本指南僅適用於 2023 年 6 月 1 日之前取得的 OV 程式碼簽署憑證!對於使用 EV 憑證和該日期之後取得的 OV 憑證進行程式碼簽署,請參閱憑證發行者的文件。
先決條件
- Windows - 你可能可以使用其他平台,但本教學使用 Powershell 原生功能。
- 運作中的 Tauri 應用程式
- 程式碼簽署憑證 - 你可以在 Microsoft 文件 中列出的服務取得其中一個。除了該清單中所列的非 EV 憑證,可能還有其他認證機構,請自行比較並自行承擔風險選擇一個。
- 請務必取得程式碼簽署憑證,SSL 憑證無法使用!
本指南假設您擁有標準的程式碼簽署憑證,如果您擁有 EV 憑證(通常涉及硬體代幣),請改為遵循您的發行者的文件。
如果您使用 EV 憑證簽署應用程式,它將立即獲得 Microsoft SmartScreen 的信譽,且不會對使用者顯示任何警告。
如果您選擇 OV 憑證,它通常較便宜且個人可以使用,Microsoft SmartScreen 仍會在使用者下載應用程式時顯示警告。您的憑證建立足夠信譽可能需要一些時間。您可以選擇提交您的應用程式給 Microsoft 進行手動審查。雖然無法保證,但如果應用程式不包含任何惡意程式碼,Microsoft 可能會授予額外的信譽,並有可能移除特定上傳檔案的警告。
入門
我們必須做一些事才能讓 Windows 準備好進行程式碼簽署。這包括將我們的憑證轉換為特定格式、安裝此憑證,以及從憑證解碼所需的資訊。
A. 將您的 .cer
轉換為 .pfx
您需要以下項目
- 憑證檔案 (我的檔案是
cert.cer
) - 私人金鑰檔案 (我的檔案是
private-key.key
)
- 憑證檔案 (我的檔案是
開啟命令提示字元,並使用
cd Documents/Certs
變更為目前目錄使用
openssl pkcs12 -export -in cert.cer -inkey private-key.key -out certificate.pfx
將您的.cer
轉換為.pfx
系統會提示您輸入匯出密碼,請務必記住!
B. 將您的 .pfx
檔案匯入金鑰庫。
我們現在需要匯入 .pfx
檔案。
使用
$WINDOWS_PFX_PASSWORD = 'MYPASSWORD'
將您的匯出密碼指定給變數現在使用
Import-PfxCertificate -FilePath certificate.pfx -CertStoreLocation Cert:\CurrentUser\My -Password (ConvertTo-SecureString -String $WINDOWS_PFX_PASSWORD -Force -AsPlainText)
匯入憑證
C. 準備變數
開始 ➡️
certmgr.msc
以開啟個人憑證管理,然後開啟個人/憑證。找到我們剛才匯入的憑證並按兩下,然後按一下 [詳細資料] 標籤。
簽章雜湊演算法將會是我們的
digestAlgorithm
。(提示:這可能是sha256
)向下捲動至 [拇印]。應該會有一個類似
A1B1A2B2A3B3A4B4A5B5A6B6A7B7A8B8A9B9A0B0
的值。這是我們的certificateThumbprint
。我們還需要一個時間戳記 URL;這是一個用於驗證憑證簽署時間的時間伺服器。我使用的是
http://timestamp.comodoca.com
,但您取得憑證的任何人都可能也有。
準備 tauri.conf.json
檔案
現在我們有了
certificateThumbprint
、digestAlgorithm
和timestampUrl
,我們將開啟tauri.conf.json
。在
tauri.conf.json
中,您將尋找tauri
->bundle
->windows
區段。您會看到,我們擷取的資訊有三個變數。請照以下範例填寫。
"windows": {
"certificateThumbprint": "A1B1A2B2A3B3A4B4A5B5A6B6A7B7A8B8A9B9A0B0",
"digestAlgorithm": "sha256",
"timestampUrl": "http://timestamp.comodoca.com"
}
儲存並執行
yarn | yarn build
在主控台輸出中,您應該會看到以下輸出。
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
。
這樣就完成了!您已成功簽署 .exe 檔案。
加值功能:使用 GitHub Actions 簽署您的應用程式。
我們也可以建立一個工作流程,以 GitHub Actions 簽署應用程式。
GitHub Secrets
我們需要新增幾個 GitHub Secrets,以適當設定 GitHub Action。這些 Secrets 可以隨意命名。
- 您可以查看加密 Secrets指南,了解如何新增 GitHub Secrets。
我們使用的 Secrets 如下
GitHub Secrets | 變數值 |
---|---|
WINDOWS_CERTIFICATE | 您的 .pfx 憑證的 Base64 編碼版本,可以使用此命令完成 certutil -encode certificate.pfx base64cert.txt |
WINDOWS_CERTIFICATE_PASSWORD | 建立憑證 .pfx 時使用的憑證匯出密碼 |
工作流程修改
我們需要在工作流程中新增一個步驟,以將憑證匯入 Windows 環境。此工作流程會完成下列動作
- 將 GitHub 機密指定給環境變數
- 建立新的
certificate
目錄 - 將
WINDOWS_CERTIFICATE
匯入 tempCert.txt - 使用
certutil
將 tempCert.txt 從 base64 解碼為.pfx
檔案。 - 移除 tempCert.txt
- 將
.pfx
檔案匯入 Windows 的憑證儲存區,並將WINDOWS_CERTIFICATE_PASSWORD
轉換為匯入指令中使用的安全字串。
我們將使用
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
- 在
-name: 安裝應用程式相依性並建置
正上方,您需要新增下列步驟
- 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)
儲存並推送到您的儲存庫。
您的工作流程現在可以匯入您的 Windows 憑證並將其匯入 GitHub 執行器,允許自動程式碼簽署!