惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
Darknet – Hacking Tools, Hacker News & Cyber Security
AI
AI
S
Security Affairs
S
Secure Thoughts
Attack and Defense Labs
Attack and Defense Labs
Application and Cybersecurity Blog
Application and Cybersecurity Blog
H
Heimdal Security Blog
Forbes - Security
Forbes - Security
Security Archives - TechRepublic
Security Archives - TechRepublic
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Spread Privacy
Spread Privacy
C
Cybersecurity and Infrastructure Security Agency CISA
Latest news
Latest news
T
Tor Project blog
I
Intezer
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Project Zero
Project Zero
V2EX - 技术
V2EX - 技术
V
Vulnerabilities – Threatpost
L
Lohrmann on Cybersecurity
C
CXSECURITY Database RSS Feed - CXSecurity.com
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Hacker News: Ask HN
Hacker News: Ask HN
S
Schneier on Security
T
Threat Research - Cisco Blogs
Webroot Blog
Webroot Blog
T
Threatpost
Help Net Security
Help Net Security
W
WeLiveSecurity
Know Your Adversary
Know Your Adversary
WordPress大学
WordPress大学
L
LINUX DO - 最新话题
月光博客
月光博客
PCI Perspectives
PCI Perspectives
小众软件
小众软件
爱范儿
爱范儿
博客园 - Franky
Security Latest
Security Latest
Microsoft Azure Blog
Microsoft Azure Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Stack Overflow Blog
Stack Overflow Blog
Google DeepMind News
Google DeepMind News
Scott Helme
Scott Helme
P
Privacy International News Feed
Y
Y Combinator Blog
P
Palo Alto Networks Blog
M
MIT News - Artificial intelligence
N
Netflix TechBlog - Medium

ThinkAlone

JavaCard 上手 灵车!开创! Step-CA 日常使用教程 友善 FriendlyELEC NanoPC-T4 CPU跑分测试 CPU Benchmark 常见物品尺寸记录 树莓派5 Raspberry Pi 5 CPU跑分测试 CPU Benchmark 鼎阳 SDS804X HD 示波器带宽与选件升级 OrangePi Zero3 CPU跑分测试 CPU Benchmark Canokey Canary上手 在2024年配置IPv6是什么怎样一种体验 用 Orange Pi Zero 搭建一台 Stratum 1 的 NTP 服务器 难绷的Zip与中文密码 OrangePi Zero CPU跑分测试 CPU Benchmark 使用 Docker 部署 zhenxun_bot(绪山真寻Bot) 在Docker中运行Klipper 如何优雅的跳过/禁止Steam更新你的游戏 使用 GitHub Actions 自动部署Hexo 使用 Travis CI 自动构建 Hexo 博客 使DockerHub的Autobuild自动构建ARM/其他 架构的镜像 CoffeeMiner:劫持WiFi网络接入设备进行“挖矿”的框架
灵上加灵:使用 Pico HSM 和 step-ca 自建一个CA
Disappear9 · 2025-12-10 · via ThinkAlone

引言

前几天在Canokey群看到一个软件:step-ca
简单说就是用这个软件可以自建一个CA来玩,而且软件支持HSM设备


环境确认

硬件信息:
  OrangePi Zero3
  Raspberry Pi Pico2
操作系统:
  Armbian v25.11.2 6.12.58-current-sunxi64
软件版本:
  go1.25.5
  step-ca 0.29.0
  OpenSC 0.26.1

准备

安装Go

https://go.dev/doc/install

1
2
3
4
5
$ wget https://go.dev/dl/go1.25.5.linux-arm64.tar.gz
$ sudo rm -rf /usr/local/go && sudo tar -C /usr/local -xzf go1.25.5.linux-amd64.tar.gz
$ export PATH=$PATH:/usr/local/go/bin
$ go version
go version go1.25.5 linux/arm64

编译 step-ca

由于官方编译的软件包没有HSM支持,所以我们需要手动编译

https://github.com/smallstep/certificates/blob/master/CONTRIBUTING.md#build-step-ca-using-cgo

1
2
3
4
5
6
7
8
9
10
11
$ wget https://github.com/smallstep/certificates/archive/refs/tags/v0.29.0.tar.gz
$ tar -xvzf v0.29.0.tar.gz
$ cd certificates-0.29.0
$ sudo apt install libpcsclite-dev gcc make pkg-config
$ make bootstrap
$ make build GO_ENVS="CGO_ENABLED=1"
$ sudo cp bin/step-ca /usr/local/bin
$ sudo setcap CAP_NET_BIND_SERVICE=+eip /usr/local/bin/step-ca
$ step-ca version
Smallstep CA/ (linux/arm64)
Release Date: 2025-12-03 14:16 UTC

安装 step-cli 和 step-kms-plugin

1
2
3
4
5
6
7
8
9
10
$ wget https://dl.smallstep.com/gh-release/cli/gh-release-header/v0.29.0/step-cli_0.29.0-1_arm64.deb
$ sudo dpkg -i step-cli_0.29.0-1_arm64.deb
$ step version
Smallstep CLI/0.29.0 (linux/arm64)
Release Date: 2025-12-03T04:11:27Z
$ wget https://github.com/smallstep/step-kms-plugin/releases/download/v0.16.0/step-kms-plugin_0.16.0-1_arm64.deb
$ sudo dpkg -i step-kms-plugin_0.16.0-1_arm64.deb
$ step kms version
🔐 step-kms-plugin/0.16.0 (linux/arm64)
Release Date: 2025-12-04T22:35:43Z

编译 OpenSC

https://github.com/OpenSC/OpenSC/wiki/Compiling-and-Installing-on-Unix-flavors

1
2
3
4
5
6
7
8
$ wget https://github.com/OpenSC/OpenSC/releases/download/0.26.1/opensc-0.26.1.tar.gz
$ sudo apt install pcscd libccid libpcsclite-dev libssl-dev libreadline-dev autoconf automake build-essential docbook-xsl xsltproc libtool pkg-config zlib1g-dev
$ tar xfvz opensc-0.26.1.tar.gz
$ cd opensc-0.26.1
$ ./bootstrap
$ ./configure --prefix=/usr --sysconfdir=/etc/opensc
$ make
$ sudo make install

配置 TRNG (可选)

这里使用的是 Infinite Noise TRNG 全开源的硬件TRNG

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$ curl -LO https://github.com/leetronics/infnoise/archive/refs/tags/0.3.3.tar.gz
$ tar xvzf 0.3.3.tar.gz
$ cd infnoise-0.3.3/software
$ sudo apt install libftdi-dev libusb-dev
$ make -f Makefile.linux
$ sudo make -f Makefile.linux install
$ infnoise --version
GIT VERSION -
GIT COMMIT -
GIT DATE -

$ sudo init 6
$ infnoise --debug --no-output
Generated 1048576 bits. OK to use data. Estimated entropy per bit: 0.875965, estimated K: 1.835235
num1s:50.019069%, even misfires:0.183222%, odd misfires:0.123520%
Generated 2097152 bits. OK to use data. Estimated entropy per bit: 0.873196, estimated K: 1.831716
num1s:49.909786%, even misfires:0.202971%, odd misfires:0.124232%
......

烧录 pico-hsm 到 Pico2

https://github.com/polhenarejos/pico-hsm
https://github.com/Gadgetoid/pico-universal-flash-nuke

1
2
$ wget https://github.com/polhenarejos/pico-hsm/releases/download/v6.0/pico_hsm_pico2-6.0.uf2
$ wget https://github.com/Gadgetoid/pico-universal-flash-nuke/releases/download/v1.0.1/universal_flash_nuke.uf2

按住 Pico2 开发板上的 BOOT 按钮,连上 USB 线,先刷入universal_flash_nuke.uf2清空Flash,再刷入pico_hsm_pico2-6.0.uf2

初始化 pico-hsm

更新:目前如果要使用SCS3 tool导入证书,只能使用Pico Commissioner初始化,否则会导致SCS3一直报认证错误。
更新2:这B作者把Pico Commissioner的页面和pypicohsm等工具全删了,现有的网页存档也被作者下了,然后强制用户使用一个新的需要付费30欧元每个Key的应用来初始化。
更新3:有个印度老哥做了分叉Libre Keys,大部分工具如pypicohsm等都可以在这里下载了。
更新4:写了一个自己生成cvc证书的教程,按教程操作后就可以用SCS3管理pico-hsm了,直接使用原2.0.2版本的pypicohsm会从原作者的服务器上请求cvc证书,也能用,但是鉴于这B目前的吃相这个API还能活多久我不好说。

  • pico-hsm-tool.py

  • PicoCommissioner(已失效)

1
2
3
4
5
6
7
$ sudo apt install python3-dev
$ wget https://github.com/librekeys/pico-hsm/raw/refs/heads/master/tools/pico-hsm-tool.py
$ python3 -m venv venv
$ source venv/bin/activate
$ pip install pycvc cryptography pypicohsm
$ python3 pico-hsm-tool.py
$ deactivate

更改pico-hsm的vid和pid

1
2
3
4
5
6
7
8
9
$ sudo -i
$ source venv/bin/activate
$ python pico-hsm-tool.py phy vidpid 20A0:4230
$ exit
$ lsusb
Bus 006 Device 003: ID 20a0:4230 Clay Logic Nitrokey HSM
Bus 006 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 003 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
...

初始化 pico-hsm

1
$ python3 pico-hsm-tool.py --pin 648219 initialize --so-pin 57621880

创建RootCA和中间CA

这里我们使用的是创建后导入的方式,虽然在使用正规HSM产品的时候都是建议仅在设备上生成密钥且不要导入导出,但是我们这个10块钱的开发板指不定什么时候就会坏,所以多一份备份是必须的。

生成RootCA私钥

1
2
3
$ mkdir -p certificate-authority/newcerts
$ touch certificate-authority/index.txt
$ openssl ecparam -genkey -name secp384r1 -noout -out root-ca-key.pem
create_root_cert.ini
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
[ ca ]

default_ca = CA_default

[ CA_default ]

dir = certificate-authority
certs = $dir/certs
crl_dir = $dir/crl
new_certs_dir = $dir/newcerts
database = $dir/index.txt
serial = $dir/serial


default_md = sha512

name_opt = ca_default
cert_opt = ca_default
default_days = 375
preserve = no
policy = policy_strict

[ policy_strict ]


countryName = match
stateOrProvinceName = match
organizationName = match
organizationalUnitName = optional
commonName = supplied
emailAddress = optional

[ req ]

default_bits = 4096
distinguished_name = req_distinguished_name
string_mask = utf8only
prompt = no


default_md = sha512

[ req_distinguished_name ]
C = CN
O = D9Lab
OU = D9Lab Zero Certificate Authority
CN = D9Lab Zero Root CA

[ v3_ca ]

subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid:always,issuer
basicConstraints = critical, CA:true
keyUsage = critical, digitalSignature, cRLSign, keyCertSign

生成RootCA证书

1
$ openssl req -config create_root_cert.ini -new -key root-ca-key.pem -x509 -days 3650 -sha512 -extensions v3_ca -out root-ca.crt

RootCA的密钥和证书如果有条件的话建议导入到不那么灵车的设备里,如Canokey中。

生成中间CA私钥

  • 使用HSM设备

  • 不使用HSM设备

首先暂时拔掉Pico HSM,插上Canokey/YubiKey之类的设备
将证书和私钥打包成p12格式

1
2

$ openssl pkcs12 -export -out root-ca.p12 -inkey root-ca-key.pem -in root-ca.crt

p12格式的RootCA证书导入到HSM设备中 以Canokey为例

1
$ yubico-piv-tool -r canokeys -s 9a -i root-ca.p12 -KPKCS12 -a import-key -a import-cert

找到导入进Canokey的证书id

1
$ pkcs11-tool --module /usr/lib/opensc-pkcs11.so -O

正常的话应该会有类似这样的输出:

1
2
3
4
5
6
7
8
...
Certificate Object; type = X.509 cert
label: Certificate for Key Management
subject: DN: C=CN, O=D9Lab, OU=D9Lab Zero Certificate Authority, CN=D9Lab Zero Root CA
serial: ************
ID: 03
uri: pkcs11:model=PKCS%2315%20emulated;manufacturer=piv_II;serial=************;token=Disappear9%20%27s%20CanoKey;id=%03;object=Certificate%20for%20Key%20Management;type=cert
...

记下这个ID 03,接下来会用到

创建中间CA模板

intermediate.tpl
1
2
3
4
5
6
7
8
9
{
"subject": { { toJson .Subject } },
"keyUsage": ["certSign", "crlSign"],
"basicConstraints": {
"isCA": true,
"maxPathLen": 0
},
"crlDistributionPoints": ["http://ca.lab.d9lab.eu.org/1.0/crl"]
}

生成中间CA私钥

1
2
3
4
5
6
7
8
9
10
11
$ step certificate create \
"D9Lab Zero Intermediate CA" \
intermediate-ca.crt \
intermediate_ca_key_enc \
--template intermediate.tpl \
--ca root-ca.crt \
--ca-kms 'pkcs11:module-path=/usr/lib/opensc-pkcs11.so;serial=************?pin-value=648219' \
--ca-key 'pkcs11:id=03' \
--not-before '2025-01-10T00:00:00+08:00' \
--not-after '2030-01-10T00:00:00+08:00' \
--kty=EC --curve=P-384

拔下Canokey/YubiKey,换回Pico HSM

将证书和私钥打包成p12格式

1
2
3
$ openssl ec -in intermediate_ca_key_enc -out intermediate-ca-key.pem

$ openssl pkcs12 -export -out intermediate-ca.p12 -inkey intermediate-ca-key.pem -in intermediate-ca.crt -certfile root-ca.crt

将中间CA证书和密钥导入 pico-hsm

首先参考 pico-hsm 作者的说明,下载并修改SCS3 tool:
https://github.com/polhenarejos/pico-hsm/blob/master/doc/scs3.md

然后参考 docs.nitrokey.com 导入intermediate-ca.p12

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Inside the unpacked directory you will find scsh3gui, which can be started using bash scsh3gui (for windows double-click on: scsh3gui.cmd).

Start key-manager (File -> Keymanager)
Right-click “Smartcard-HSM” -> create DKEK share
Choose file location
Choose DKEK share password
Right-click “Smartcard-HSM” -> Initialize device
Enter SO-PIN
(optional) Enter label and enter URL/Host
Select authentication method: “User PIN”
Allow RESET RETRY COUNTER: “Resetting and unblocking PIN with SO-PIN not allowed”
Enter and confirm User PIN
“Select Device Key Encryption scheme” -> “DKEK shares”
Enter number of DKEK shares: 1
Right-click DKEK set-up in progress -> “Import DKEK share”
Choose DKEK share file location
Password for DKEK share
Right-click “SmartCard-HSM” -> “Import from PKCS#12(Old)”
Enter number of shares -> 1
Enter file location of DKEK share
Enter Password for DKEK share
Select PKCS#12 container for import (Enter password)
Select Key
Select Name to be used (intermediate-ca)
Import more keys, if needed

导入完成后运行pkcs11-tool -O应该就可以看到导入的证书了

1
2
3
4
5
6
7
$ pkcs11-tool -O
Using slot 0 with a present token (0x0)
Certificate Object; type = X.509 cert
label: intermediate-ca
subject: DN: C=CN, O=D9Lab, OU=D9Lab Zero Certificate Authority, CN=D9Lab Zero Intermediate CA
ID: 01
...

记下IDlabel,后面要用到。

把这些文件拿7z打个加密压缩包,密码用KeePass生成一个够长的保存,然后找个你喜欢的网盘存好或者刻张光盘放衣柜里。

1
2
3
4
5
6
7
intermediate-ca-key.pem
intermediate-ca.crt
intermediate-ca.p12
pde文件(DKEK)
root-ca-key.pem
root-ca.crt
root-ca.p12

之后只保留intermediate-ca.crtroot-ca.crt,其余文件全部删除

配置step-ca

https://smallstep.com/docs/step-ca/cryptographic-protection/#pkcs-11

初始化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
$ sudo systemctl enable pcscd
$ sudo systemctl start pcscd
$ sudo useradd step
$ sudo passwd -l step
$ sudo mkdir /etc/step-ca
$ export STEPPATH=/etc/step-ca
$ sudo --preserve-env step ca init --name="D9Lab Zero CA" \
--dns="ca.lab.d9lab.eu.org" --address=":4443" \
--provisioner="[email protected]" \
--deployment-type standalone \
--remote-management


$ sudo rm /etc/step-ca/certs/*

$ sudo rm -rf /etc/step-ca/secrets
$ sudo chown -R step:step /etc/step-ca
$ step kms create --json --kms "pkcs11:module-path=/usr/lib/opensc-pkcs11.so;serial=ESPICOHSMTR?pin-value=648219" "pkcs11:id=2000;object=ssh-host-ca"
$ step kms create --json --kms "pkcs11:module-path=/usr/lib/opensc-pkcs11.so;serial=ESPICOHSMTR?pin-value=648219" "pkcs11:id=2001;object=ssh-user-ca"

编辑/etc/step-ca/config/ca.json

ca.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
"root": "/etc/step-ca/certs/root-ca.crt",
"crt": "/etc/step-ca/certs/intermediate-ca.crt",
"key": "pkcs11:id=01;object=intermediate-ca",
"kms": {
"type": "pkcs11",
"uri": "pkcs11:module-path=/usr/lib/opensc-pkcs11.so;serial=ESPICOHSMTR?pin-value=648219"
},
"ssh": {
"hostKey": "pkcs11:id=2000;object=ssh-host-ca",
"userKey": "pkcs11:id=2001;object=ssh-user-ca"
}
}

尝试运行

1
2
3
4

$ screen
$ sudo -u step step-ca /etc/step-ca/config/ca.json

新开一个窗口

1
2
3
4
5
6
7
8
$ unset STEPPATH
$ step ca bootstrap --ca-url "https://ca.lab.d9lab.eu.org:4443" --fingerprint d6b3b9ef79a42aeeabcd5580b2b516458ddb25d1af4ea7ff0845e624ec1bb609
The root certificate has been saved in /home/disappear9/.step/certs/root_ca.crt.
The authority configuration has been saved in /home/disappear9/.step/config/defaults.json.


$ step ca certificate "localhost" localhost.crt localhost.key
$ step ca provisioner add acme --type acme --admin-name step

配置服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
$ sudo tee /etc/udev/rules.d/75-picohsm.rules > /dev/null << EOF
ACTION=="add", SUBSYSTEM=="usb", ENV{PRODUCT}=="20a0/4230/*", TAG+="systemd", SYMLINK+="picohsm"
ACTION=="remove", SUBSYSTEM=="usb", ENV{PRODUCT}=="20a0/4230/*", TAG+="systemd"
EOF
$ sudo udevadm control --reload-rules
$ sudo tee /etc/systemd/system/step-ca.service > /dev/null << EOF
[Unit]
Description=step-ca
BindsTo=dev-picohsm.device
After=dev-picohsm.device
[Service]
User=step
Group=step
ExecStart=/bin/sh -c '/usr/local/bin/step-ca /etc/step-ca/config/ca.json'
Type=simple
Restart=on-failure
RestartSec=10
[Install]
WantedBy=multi-user.target
EOF
$ sudo mkdir /etc/systemd/system/dev-picohsm.device.wants
$ sudo ln -s /etc/systemd/system/step-ca.service /etc/systemd/system/dev-picohsm.device.wants/
$ sudo systemctl daemon-reload
$ sudo systemctl enable step-ca
$ sudo init 6


$ systemctl status step-ca

(完)