electron-userland / electron-builder

A complete solution to package and build a ready for distribution Electron app with “auto update” support out of the box
https://www.electron.build
MIT License
13.61k stars 1.74k forks source link

Integration of USB Hardware Token for EV Code Signing with electron-builder #8072

Closed ogawa-ma closed 7 months ago

ogawa-ma commented 7 months ago

Hello,

Due to the limitations of machine translation, there may be inaccuracies in my expression. Apologies for any inconvenience caused.

I am currently utilizing the CSC_LINK and CSC_KEY_PASSWORD environment variables for EV code signing within my Electron application development process. As the validity period of my current certificate is nearing its end, I have contracted a new EV code signing certificate from DigiCert, which includes a USB hardware token for the signing process.

With this transition to using a USB for signing, I am wondering if it's possible to maintain the automation of the signing process as with the previous method. Specifically, is there a way to integrate the USB hardware token signing into electron-builder’s build process to automatically sign the application during the build?

Additionally, if integrating the signing process with electron-builder proves challenging, I am considering manually signing the application using SignTool.exe. In our existing build process, both the generated .exe file and the executables within .7z archives were signed. If proceeding with manual signing, is it necessary to sign both the .exe file and the executables inside the .7z archives?

Thank you for your time and assistance. I look forward to any guidance or suggestions you can provide.

KaminoRyo commented 7 months ago

@ogawa-ma おそらく日本人だと思うので日本語で答えますが、つい先日自分もやってマニュアルを作ったので参考にしていただければ。 Sectigo(Comodo)から購入しましたがある程度、流用できるかと。

1. SafeNet Authentication Client をインストールする

Sectigo(Comodo)の話なので、DigiCertだと別のやつ使ってるかもしれませんが、似たようなアプリはインストールしているはずです。 署名する際に USB ドングルと通信を行うためのソフトをインストールする。 リンク ちょっと分かりづらいが、下部にダウンロードリンクがある。

2. (おすすめ)トークンのシングルログオンを有効にする

初期設定では署名する度にパスワードを入力することになるが、インストーラーやアンインストーラー含め署名する必要があるので、ビルドする度に毎回 6 ~ 8 回ほどパスワードを入力することになる。 あまりに面倒すぎるので上記インストールしたソフトのシングルログオンという機能を有効にする。

2-1. SafeNet Authentication Client の設定画面を開く

このソフトは常駐型のため Windows のタスクバーの右下の常駐アプリの所に「SafeNet Authentication Client」のアイコンもある。右クリックして「ツール」を選択すると設定画面が開く。

2-2. シングルログオンを有効にする

このページに画面写真付きで説明されているので従う。

3. cer ファイルを取得する。

3-1. クライアントソフトを起動後、USB(eToken ドングル)を挿入する

3-2. 【Safenet の設定】 証明書(.cer)をエクスポートする

ソフト記号後に[歯車]アイコン →Safenet→ ユーザー証明書 →【署名の名義】→ エクスポートのアイコンをクリック

4. WindowsSDK (Windows SDK Signing Tools for Desktop Apps のみ)をインストール

MS のページからダウンロードする。

インストーラーでは後半、複数のツールのうち何を入れるか選択する画面があるが、「Windows SDK Signing Tools for Desktop Apps」 のみ選択して「Install」をクリックし完了。

(以下任意) 環境変数からユーザー環境変数の「Path」を編集。Windows タスクバーで「環境変数」で検索。 環境変数に「C:\Program Files (x86)\Windows Kits\10\App Certification Kit」を追加。

5. 電子証明書をインストールする

signtool を直接 shell で使う場合はファイルパスを指定できるが、electron-builder 任せにしたい場合は Windows に電子証明書をインストールする。

5-1. Explorer 上で W クリック(開く)

5-2. 証明書の詳細ウィンドウが表示されるので「証明書のインストール」を押す

5-3. インストールされているか確認する

Windows に証明書ストアという機能があり、登録しておける。mac でいうところの keychain に相当する。

  1. Windows のタスクバーの左側に検索アイコンや入力欄があるので、 certmgr.msc と入力
  2. 現在のログオン ユーザーの証明書ストアが確認できます。

6. electron-builder に必要なサブジェクト名を確認する

electron-builder は自動で Windows の証明書ストアを検索するが、検索するキーワード(サブジェクト名)を登録する必要があり、確認する。

先ほど Windows の証明書ストアを開いたと思うが、該当の証明書を右クリックして「開く」とウィンドウが表示される。そこの「詳細」タブをクリックする。

サブジェクト名は書き方の規格があるが(CN=みたいな書き方をしている部分)、electron-builder で設定する分にはCNの値を控えるだけでよい。

7. electron-builder の設定オブジェクトに設定する

先ほど控えたサブジェクト名を electron-builder の設定オブジェクトのwinプロパティ内のcertificateSubjectNameプロパティに登録する。

// 例
win: {
    // Windowsに証明書をインストールしておくと、この名義で検索して署名してくれる。当然ドングルは必要
    certificateSubjectName: "【署名の名義(Subject名のCN)】"
  },

8. アプリの exe やインストーラーをビルドする

正しく設定されており、USB ドングルが認識されているなら、途中で 1 回ぐらいは USB ドングルのパスワード入力画面が表示されるかもしれないが入力する。

9. 電子署名できているか確認する

インストーラーでもアプリ本体の Exe でもなんでもいいので、 右クリックして「プロパティ」をクリックすると、電子署名済の場合、「デジタル署名」というタブが表示されるので署名者やタイムスタンプを確認する。

ogawa-ma commented 7 months ago

@KaminoRyo 非常に丁寧なマニュアルを共有していただき、ありがとうございました。 教えていただいた手順に従って、署名が正常に行えることを確認いたしました。 この度は本当にありがとうございました。 問題が解決しましたので、こちらの件はクローズさせていただきます。