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

推荐订阅源

PCI Perspectives
PCI Perspectives
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
CXSECURITY Database RSS Feed - CXSecurity.com
P
Palo Alto Networks Blog
S
Schneier on Security
Scott Helme
Scott Helme
T
Threat Research - Cisco Blogs
K
Kaspersky official blog
Microsoft Azure Blog
Microsoft Azure Blog
T
The Exploit Database - CXSecurity.com
C
Cybersecurity and Infrastructure Security Agency CISA
T
Tenable Blog
G
GRAHAM CLULEY
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
I
Intezer
D
Docker
月光博客
月光博客
L
Lohrmann on Cybersecurity
Latest news
Latest news
B
Blog
罗磊的独立博客
M
MIT News - Artificial intelligence
S
Securelist
Know Your Adversary
Know Your Adversary
Help Net Security
Help Net Security
Recorded Future
Recorded Future
S
SegmentFault 最新的问题
N
Netflix TechBlog - Medium
T
Threatpost
H
Hacker News: Front Page
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
人人都是产品经理
人人都是产品经理
F
Fortinet All Blogs
博客园 - Franky
P
Proofpoint News Feed
大猫的无限游戏
大猫的无限游戏
Blog — PlanetScale
Blog — PlanetScale
有赞技术团队
有赞技术团队
博客园 - 【当耐特】
A
About on SuperTechFans
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
T
Tor Project blog
Google Online Security Blog
Google Online Security Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Engineering at Meta
Engineering at Meta
Webroot Blog
Webroot Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
Microsoft Security Blog
Microsoft Security Blog

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

(完)