买 “忆百特” 的 E104-BT5040U - 刷 Canokey 的固件 - 充当 GPG 智能卡使用
目的:使用 gpg 创建个人的密匙对,然后将“加密”、“签名”和“认证”的子私匙放到 E104-BT5040U 里,当 gpg 智能卡使用。例如,需要远程登录 Linux 服务器时,插入 U 盘形状的 E104-BT5040U ,然后在 MobaXterm 里发起 ssh 登录,最后在弹出的询问框里,输入 gpg 智能卡的 PIN 码,登录成功
用到的:
a. 1台 Debian 12 虚拟机或物理机,用于编译出固件 canokey.hex 、执行初始化任务(生成序列号等)和 管理 gpg 密匙对等
b. 1台 Windows 10 机,用于烧录固件
c. 两个 E104-BT5040U ,因为卖家的淘宝店里有“试用套装”——一次买两个,比单个买,价格实惠些。一个日常使用,另一个作为备用
c. 1个 U 盘,用于备份 gpg 的密匙等
编译出 canokey.hex 文件
请在 Debian 12 机里编译:
- apt install cmake gcc-arm-none-eabi gdb-arm-none-eabi # 安装编译环境
从 github 里复制 canokey 的文件
clone https://github.com/canokeys/canokey-nrf52 cd canokey-nrf52/ git submodule update --init --recursive
- unzip -d nrf_sdk nRF5_SDK_17.1.0_ddde560.zip # 把下载好的 nRF5_SDK 放到目录里的 nrf_sdk 目录里
- mkdir build && cd build # 创建 build 目录
开始 cmake ,
cmake \ -DCROSS_COMPILE=/usr/bin/arm-none-eabi- \ -DCMAKE_TOOLCHAIN_FILE=../toolchain.cmake \ -DCMAKE_BUILD_TYPE=Release ..
- make canokey_flash.uf2
- apt install libnewlib-arm-none-eabi # 如果提示找不到 string.h ,请安装 libnewlib
- ln -s /usr/bin/python3 /usr/bin/python # 如果提示找不到 python,请创建一个软链接
烧录 canokey.hex 文件
按网友的指导,在 Windows机 里下载、安装好厂家“诺迪克” nrfconnect 的软件,将 canokey.hex 烧录到 E104-BT5040U 设备里。
初始化 canokey ,随机生成序列号
回到 Debian 12 机,执行 device-config-init.sh 脚本,初始化 canokey:
- apt install pcscd pcsc-tools scdaemon # 安装相应的工具
- apt install python3.11-venv # 如果提示,建议使用 venv 安装非 deb 的 python 包,那么先安装 venv
- python3 -m venv /root/canokeypy/venv # 创建一个新的 python 和 pip 目录
- /root/canokeypy/venv/bin/pip install scriptor # 安装 scriptor
- export PATH="/root/canokeypy/venv/bin:$PATH"
- pcsc_scan # 插入扫描 usb 设备,如果顺利,可以看到有“Canokeys Canokey [OpenPGP PIV OATH] (00000000) 00 00”的设备
- ./device-config-init.sh 'Canokeys Canokey [OpenPGP PIV OATH] (00000000) 00 00' # 在 canokey/utils 里执行脚本
- gpg --card-status # 如果顺利,那么在 gpg 的智能卡信息页里,序列号将是非全 0 ,是随机生成的
ssh 远程登录 认证
- Windows 机里安装 gpg4win ,安装过程中,要选上 gpg agen ,创建好密匙对,备份好
- 在 Kleopatra 配置里,GnuPG 系统,Private Keys ,勾选 Enable ssh upport 和 Enable putty support
- gpg-connect-agent killagent /bye # 退出当前的 agent
- gpg-connect-agent /bye # 重新运行。也可以使用重载的命令:gpg-connect-agent.exe reload /bye
- 将导出的 ssh-key (--export-ssh-key)公钥内容追加到 ssh 服务端的 ~/authorized_keys 文件中
- 顺利的话,putty 发起 ssh 连接时,会弹框要求输入智能卡的 PIN 码
- 习惯使用 MobaXterm 的话,请在 Settings 里的 SSH 页里,勾选 Use external Pageant
console.canokeys
a. 在 Windows 机里,插入设备,右下角会弹框提示可以访问 console ,如 https://console.canokeys.org/#/settings 会显示具体的设备信息,如果型号、固件版本和序号等
b. 如果转到旧版 console ,https://console-legacy.canokeys.org/#/ ,可以看到 gpg 的详情
参考:
- https://github.com/canokeys/canokey-nrf52/tree/master (我复制时,约是 2024-08-11 日的版本)
- https://kwaa.dev/canokey-nrf52
- https://www.nordicsemi.com/Products/Development-tools/nRF-Connect-for-Desktop/Download#infotabs
- https://kabe.dev/canokey%E5%9B%BA%E4%BB%B6/
- https://tccmu.com/2024/08/13/canokey/
- https://www.bilibili.com/video/BV1rJ4m1H7gN/?spm_id_from=333.1391.0.0
- https://www.0eac.com/posts/e104-bt5040u-and-canokey-nrf52
- https://www.cyqsd.cn/9466.html
- https://docs.canokeys.org/zh-hans/userguide/setup/
- https://www.mjollnir.cc/archives/216.html
- https://www.gnupg.org/howtos/card-howto/en/ch03s03.html
- https://blog.dejavu.moe/posts/canokey-openpgp/
- https://www.gnupg.org/gph/en/manual/book1.html
- https://wiki.gnupg.org/WKDHosting
- https://ulyc.github.io/2021/01/13/2021%E5%B9%B4-%E7%94%A8%E6%9B%B4%E7%8E%B0%E4%BB%A3%E7%9A%84%E6%96%B9%E6%B3%95%E4%BD%BF%E7%94%A8PGP-%E4%B8%8A/
- https://docs.nitrokey.com/nitrokeys/features/openpgp-card/ssh/index
- https://wiki.debian.org/Smartcards/OpenPGP#SSH
- https://viccuad.me/blog/Revisited-secure-yourself-part-1-airgapped-computer-and-gpg-smartcards
- https://wiki.debian.org/GnuPG/AirgappedMasterKey