Yubikey 开箱上手
January 11, 2022 · 10 min read
新买的 YubiKey 5 NFC 刚刚送到,趁此机会来水一篇博客(主要是跟着 https://github.com/drduh/YubiKey-Guide 这篇教程走)
首先在 Linux 系统上进行设置
在 Linux 上设置
我使用的是 Manjaro 操作系统,不同的 Linux 发行版安装依赖时所使用的命令可能会有所不同。
配置 GPG
sudo pacman -S hopenpgp-tools yubikey-manager
export GNUPGHOME=~/.gnupgwget -O $GNUPGHOME/gpg.conf https://raw.githubusercontent.com/drduh/config/master/gpg.conf
检查系统的熵是否足够(>2000):
$ cat /proc/sys/kernel/random/entropy_avail3808
将系统切换为离线状态,然后生成密钥。
gpg --expert --full-gen-keyexport KEYID=0x57670CCFA42CCF0A
选择 RSA(自定义,只开启 Certify 功能) 并将 keysize 设为 4096,永不过期.
使用旧密钥给新密钥签名
gpg --default-key 0x79A6DE255728228B --sign-key $KEYIDsec rsa4096/0x57670CCFA42CCF0A created: 2022-01-11 expires: never usage: C trust: ultimate validity: ultimate[ultimate] (1). Levi Zim <rsworktech@outlook.com>gpg: using "0x79A6DE255728228B" as default secret key for signingsec rsa4096/0x57670CCFA42CCF0A created: 2022-01-11 expires: never usage: C trust: ultimate validity: ultimate Primary key fingerprint: 17AA DD67 26DD C58B 8EE5 8817 5767 0CCF A42C CF0A Levi Zim <rsworktech@outlook.com>Are you sure that you want to sign this key with yourkey "Believers in Science Studio <rsworktech@outlook.com>" (0x79A6DE255728228B)Really sign? (y/N) y
然后编辑密钥
gpg --expert --edit-key $KEYID
添加子密钥,分别负责 Sign,Encrypt 和 Authenticate,过期时间一年
addKey
检查密钥
gpg --export $KEYID | hokey lint
然后备份密钥
gpg --export-secret-keys --armor $KEYID > sec-key.keygpg --export-secret-subkeys --armor $KEYID > sec-sub-key.key
生成撤销证书
导出公钥
gpg --armor --export $KEYID > $KEYID.asc
上传公钥
gpg --send-key $KEYIDgpg --keyserver keys.gnupg.net --send-key $KEYID
修改卡的信息
gpg --card-editadminkdf-setup # Key Derived Function, 从明文传递 PIN 改为验证 PIN 的 Hashname # 修改姓名passwd # 修改 PIN, Admin PIN 和 Reset Code
修改重试次数
sudo systemctl start pcscdykman openpgp access set-retries 6 6 6ykman openpgp info
导入 YubiKey(只导入 sub-key)
gpg --edit-key $KEYID依次选择各个 sub-key,执行 keytocard
然后把 secret key 删除,确保主密钥离线
gpg --delete-secret-key $KEYID
然后测试一下能否正常使用
$ echo "test message string" | gpg --encrypt --armor --recipient $KEYID -o encrypted.txt$ gpg --decrypt --armor encrypted.txtgpg: anonymous recipient; trying secret key 0x6F64DD2F6896007A ...gpg: okay, we are the anonymous recipient.gpg: encrypted with RSA key, ID 0x0000000000000000test message string$ echo "test message string" | gpg --armor --clearsign > signed.txt$ gpg --verify signed.txtgpg: Signature made 2022年01月11日 星期二 12时56分17秒 CSTgpg: using RSA key 289670F91C3635F0A17424456F3B98D42FC6C9D8gpg: Good signature from "Levi Zim <rsworktech@outlook.com>" [ultimate]Primary key fingerprint: 17AA DD67 26DD C58B 8EE5 8817 5767 0CCF A42C CF0A Subkey fingerprint: 2896 70F9 1C36 35F0 A174 2445 6F3B 98D4 2FC6 C9D8
Git
git config --global user.signingkey $KEYID
测试
mkdir testingcd testinggit inittouch womangit add womangit commit -sm "Test signing"git log --show-signaturecommit 1e2fe949c5eb0606e083f50b13963814c9a03437 (HEAD -> main)gpg: Signature made 2022年01月11日 星期二 13时25分07秒 CSTgpg: using RSA key 289670F91C3635F0A17424456F3B98D42FC6C9D8gpg: Good signature from "Levi Zim <rsworktech@outlook.com>" [ultimate]Primary key fingerprint: 17AA DD67 26DD C58B 8EE5 8817 5767 0CCF A42C CF0A Subkey fingerprint: 2896 70F9 1C36 35F0 A174 2445 6F3B 98D4 2FC6 C9D8Author: kxxt <rsworktech@outlook.com>Date: Tue Jan 11 13:25:07 2022 +0800 Test signing Signed-off-by: kxxt <rsworktech@outlook.com>
另外要把公钥添加到 Github.