🎑

Archlinux(WSL) で Yubikey を使って GPG 署名したい

に公開

前置き

筆者は Linux ミリしか知らない初心者なので間違った点があればご指摘願いたい。

今回使う手法

usbipd-win を使い、Linux 側で Yubikey を認識させる手法をとる。

https://learn.microsoft.com/ja-jp/windows/wsl/connect-usb

やり方

下準備

今回使うパッケージをインストール

$ pacman -S ccid pcsclite cyme

お好みの応じて yubikey-manager などもどうぞ

Yubikey が認識されているか確認

$ cyme

YubiKey OTP+FIDO+CCID みたいな感じで反応されていることを確認する

サービス・ソケットの起動・有効化

$ systemctl start pcscd.service
$ systemctl enable pcscd.service
$ systemctl start pcscd.socket
$ systemctl enable pcscd.socket

サービスとソケットが動作しているか調べる

$ systemctl status pcscd.service
$ systemctl status pcscd.socket

GnuPG で pcscd を常に利用

~/.gnupg/scdaemon.conf に次のような設定を記述

~/.gnupg/scdaemon.conf
pcsc-driver /usr/lib/libpcsclite.so
card-timeout 5
disable-ccid

Polkit の設定を変更

Root ユーザーでなくても GnuPG が Yubikey を認識できるように Polkit のルールを編集

このルールの場合ユーザーが wheel グループにいる場合に動くため修正が必要な場合はここで修正すること

/etc/polkit-1/rules.d/99-pcscd.rules
polkit.addRule(function(action, subject) {
    if (action.id == "org.debian.pcsc-lite.access_card" &&
        subject.isInGroup("wheel")) {
        return polkit.Result.YES;
    }
});
polkit.addRule(function(action, subject) {
    if (action.id == "org.debian.pcsc-lite.access_pcsc" &&
        subject.isInGroup("wheel")) {
        return polkit.Result.YES;
    }
});

再起動

いつもの
[distro] は各自の名前に合わせて入力

> wsl -t [distro]
> wsl -d [distro]

完成

$ gpg --card-status

これで出力確認できればOK!

おまけ

このままでは VSCode から Git を管理する際に、署名鍵が無いことを知らせるモーダルが出てきて心臓に悪いのでこのまま設定する。

~/.zshrcexport GPG_TTY=$(tty) を置くだけでは VSCode では動かないためひと手間必要

pinentry を Gpg4win のものに向ける

~/.gnupg/gpg-agent.conf
# pinentry
pinentry-program /mnt/c/Program Files (x86)/Gpg4win/bin/pinentry.exe

保存後はエージェントをリロードする

$ gpg-connect-agent reloadagent /bye

https://github.com/microsoft/vscode/issues/130415#issuecomment-2093371365
https://zenn.dev/himaoka/articles/dev_env_on_windows_via_wsl

参考

公式情報じゃないとアレルギーが出る方はこちら
https://wiki.archlinux.org/title/GnuPG#Smartcards

Discussion