为什么手机是难办的情况
step-ca 的默认设置是为服务器准备的。服务器每天早上自己续期,你自己的证书颁发机构:容器里的 step-ca 展示了这在 ACME 客户端和任何能运行 step ca renew 的东西上是怎么工作的。那篇文章还讲了启动 CA、它的两个密码以及信任根证书,本文从那里接着讲。一部为 Home Assistant 应用 持有客户端证书的手机什么都续不了,而一旦它丢了,你需要今天就把它锁在外面,而不是等到它的证书过期。挡在路上的默认设置有五个,没有一个会大声报错。它们只是让你手里的证书明天就失效,吊销列表拒绝所有人。
| 默认设置 | 对手机的影响 | 解决办法 |
|---|---|---|
| 证书最长 24 小时 | 手机的证书明天就失效,Home Assistant 应用无法续期 | 在 ca.json 里调高 maxTLSCertDuration |
| CRL 关闭 | 吊销一部丢失的手机,什么地方都不会变 | 打开 crl 配置块 |
| CRL 作用范围与证书不匹配 | nginx 拒绝每一部手机,好的也不例外 | 设置 idpURL,并加一个带 crlDistributionPoints 的模板 |
| 中间证书没有 CRL | nginx 仍然拒绝每一部手机 | 每年签一份空的根 CRL |
| CRL 有效期 24 小时 | 过期时 nginx 又拒绝每一部手机 | 用定时器刷新 |
Cloudflare 在免费计划上替你做了 CA 这一半,Cloudflare 版本 里有展示。自己做,就意味着五件都得做。下面的测试跑在一个名为 Home Assistant CA 的 CA 上,输出里看到的就是这个名字。
默认设置一:证书只有一天
申请一年,CA 会拒绝:
The request was forbidden by the certificate authority: requested duration of
8760h1m0s is more than the authorized maximum certificate duration of 24h1m0s.
对服务器和 ACME 客户端来说这是正确的默认值,它们整天都在续期,没人察觉。手机是例外。Home Assistant 应用没有办法自己续期客户端证书,所以 24 小时的证书意味着每天都要走到每一部手机跟前去。
有效期是一个设置,就在 step-ca 自己的配置里。ca.json 里的每个 provisioner 都接受 claims1:
"claims": {
"maxTLSCertDuration": "8760h",
"defaultTLSCertDuration": "24h"
}
| Claim | 含义 | 设置为 |
|---|---|---|
maxTLSCertDuration | 这个 provisioner 肯签的最长证书 | 8760h,一年 |
defaultTLSCertDuration | 不特别要求时得到的有效期 | 保持 24h,这样一年永远是刻意的选择 |
minTLSCertDuration | 它肯签的最短证书 | 不动 |
重启容器,一年的申请就能通过。一年是判断,不是规矩。越短,被偷的证书能派上用场的时间越少,你在屋里来回跑着重装证书的次数越多。
默认设置二:CRL 是关着的
证书吊销列表(CRL)是 CA 签名的一份清单,列出它已经撤回的证书。丢失的手机就是靠它在证书过期之前被锁在外面的,而 step-ca 不被告知就不会生成它。文档把这个功能标为实验性的1。
"crl": {
"enabled": true,
"generateOnRevoke": true,
"cacheDuration": "24h"
}
| 字段 | 作用 | 默认值 |
|---|---|---|
enabled | 是否生成 CRL | 关 |
generateOnRevoke | 证书一被吊销就重建列表 | 关 |
cacheDuration | 每份列表的有效时长 | 24 小时1 |
idpURL | 列表声称自己覆盖的 URL | CA 自己的名字加上 /1.0/crl2 |
记住这个 24 小时。它还会回来。
默认设置三:一份谁都不接受的 CRL
CRL 打开以后,吊销一部手机,然后用它检查两部手机:
$ openssl verify -CAfile client-ca.pem -CRLfile crl.pem -crl_check phone1.crt phone2.crt
CN=phone1
error 44 at 0 depth lookup: different CRL scope
error phone1.crt: verification failed
CN=phone2
error 44 at 0 depth lookup: different CRL scope
error phone2.crt: verification failed
两部都失败,好的那部也是。把这份列表放到 nginx 前面,谁都进不来。
列表自己就说明了原因。step-ca 给每份 CRL 都打上一个关键(critical)的 Issuing Distribution Point,意思是“这份列表覆盖指向这里的证书”。而它签发的证书哪儿也没指:
$ openssl crl -in crl.pem -noout -text
X509v3 Issuing Distribution Point: critical
Full Name:
URI:https://localhost/1.0/crl
$ openssl x509 -in phone1.crt -noout -ext crlDistributionPoints
No extensions in certificate
OpenSSL 不会把作用范围与证书不匹配的列表套用上去。所以它以失败关闭。这是对的,但对谁都没用。
| CRL 声称覆盖 | 证书说它的 CRL 在 | 结果 | |
|---|---|---|---|
| 开箱即用 | https://localhost/1.0/crl,critical | 哪儿都没有 | different CRL scope,所有人被拒 |
idpURL 加模板 | http://ca.home.example/1.0/crl | http://ca.home.example/1.0/crl | 好手机通过,被吊销的失败 |
默认的 URL 甚至不是 CA 实际提供服务的地址。step-ca 的源码自己也这么说:“this is currently using the port 443 by default”2。两个设置能解决它,而且两者必须写同一个 URL。idpURL 放进 ca.json 的 crl 块,provisioner 上的一个模板把匹配的 crlDistributionPoints 盖进每张证书3:
{
"subject": {{ toJson .Subject }},
"sans": {{ toJson .SANs }},
"keyUsage": ["digitalSignature"],
"extKeyUsage": ["serverAuth", "clientAuth"],
"crlDistributionPoints": ["http://ca.home.example/1.0/crl"]
}
"options": { "x509": { "templateFile": "templates/leaf-crl.tpl" } }
重新签发手机的证书,因为只有新证书才带这个字段,同样的检查就给出了正确答案:
CN=phone2
error 23 at 0 depth lookup: certificate revoked
error phone2.crt: verification failed
phone1.crt: OK
根证书也需要 CRL
那次检查只看了手机的证书。nginx 看的是整条链。它设置了 OpenSSL 的 CRL_CHECK_ALL 标志4,文档也说得很明白:“When using intermediate certificates, their CRLs should be specified in the same file”5。step-ca 用它的中间证书签发证书,而且只列出中间证书吊销的东西。没有任何东西说明中间证书本身是否仍然有效:
$ openssl verify -CAfile client-ca.pem -CRLfile crl.pem -crl_check_all phone1.crt
O=Home Assistant CA, CN=Home Assistant CA Intermediate CA
error 3 at 1 depth lookup: unable to get certificate CRL
解决办法是一份由根签名、什么都不吊销的 CRL。OpenSSL 用根密钥和 CA 自己的密码文件就能生成,而且可以有效一年,因为根平时什么都不吊销:
[ca]
default_ca = root
[root]
database = index.txt
crlnumber = crlnumber
default_md = sha256
default_crl_days = 365
private_key = root_ca_key
certificate = root_ca.crt
crl_extensions = crl_ext
[crl_ext]
authorityKeyIdentifier = keyid:always
touch index.txt && echo 1000 > crlnumber
openssl ca -config root-crl.cnf -gencrl -passin file:password -out root.crl.pem
两份列表放进一个文件,整条链就检查通过了:
$ openssl verify -CAfile client-ca.pem -CRLfile crl-chain.pem -crl_check_all phone1.crt phone2.crt
CN=phone2
error 23 at 0 depth lookup: certificate revoked
error phone2.crt: verification failed
phone1.crt: OK
在日历上记下一年后的日期。根 CRL 过期的那天,每一部手机又会被锁在外面。
发布 CRL
CRL 必须放在每张证书里那个 URL 指向的地方。不是 CA 本身。
| 选择 | 原因 |
|---|---|
| 前面放一个 Web 服务器,而不是 9000 端口 | 9000 端口是 step-ca 的整个 API,包括签名。除了列表,谁都不需要其中任何东西 |
| 普通 HTTP,不用 HTTPS | RFC 5280 说分发点“SHOULD include at least one LDAP or HTTP URI”6。通过 HTTPS 获取需要检查证书,而检查证书又需要列表。HTTP 打破了这个循环 |
| 不担心篡改 | 列表是签过名的。改一个字节,签名就失效 |
| 只有两个 CRL 路径会应答 | 其他一律 404 |
两种 Web 服务器几行配置就能做到,都把列表从 step-ca 原样转出,并把根 CRL 作为文件提供:
server {
listen 80;
listen [::]:80;
server_name ca.home.example;
location = /1.0/crl {
proxy_pass https://127.0.0.1:9000;
proxy_ssl_trusted_certificate /etc/step/root_ca.crt;
proxy_ssl_verify on;
proxy_ssl_name localhost;
}
location = /root.crl {
alias /etc/step/root.crl;
default_type application/pkix-crl;
}
location / {
return 404;
}
}
http://ca.home.example {
handle /1.0/crl {
reverse_proxy https://127.0.0.1:9000 {
transport http {
tls_trust_pool file /etc/step/root_ca.crt
tls_server_name localhost
}
}
}
handle /root.crl {
header Content-Type application/pkix-crl
root * /etc/step
file_server
}
handle {
respond 404
}
}
| 请求 | nginx | Caddy |
|---|---|---|
/1.0/crl | 200,application/pkix-crl,签发者为中间证书 | 200,application/pkix-crl,签发者为中间证书 |
/root.crl | 200,application/pkix-crl | 200,application/pkix-crl |
/roots.pem、/1.0/sign 和 API 的其余部分 | 404 | 404 |
两者在 IPv6 和 IPv4 上都应答,也理应如此。Caddy 默认两个都监听。nginx 只在被告知的地方监听,所以单独一行 listen 80; 只有 IPv4,旁边还需要 listen [::]:80;。通过 ::1 测试,nginx 加上那一行之后,两者返回列表都是 200。step-ca 本身开箱就监听两者。
测试跑在 8080 和 8081 端口上,所以测试证书里的 URL 带了端口。实际使用时是 80 端口,URL 和模板里写的完全一样。
保持新鲜
step-ca 生成的每份 CRL 都带一个 24 小时之后的 nextUpdate。过了那个时间它就过期了,而过期的列表和缺失的列表以同样的方式失败:
$ openssl verify -CAfile client-ca.pem -CRLfile crl-chain.pem -crl_check_all \
-attime $(( $(date +%s) + 172800 )) phone1.crt
error 12 at 0 depth lookup: CRL has expired
这是那部好手机。两天之后。被锁在外面,因为没有东西去取新的列表。
nginx 只在启动或重载时读取 ssl_crl。所以必须有东西每隔几个小时去取列表、重建文件、重载 nginx,只要这套东西还在运行就一直如此,不管有没有人在看。
#!/bin/sh
# Fetch step-ca's CRL, add the root CRL, and reload nginx only if both parse.
set -eu
dir=${CRL_DIR:-/etc/nginx/pki}
tmp=$(mktemp)
trap 'rm -f "$tmp" "$tmp.pem"' EXIT
curl -fsS --max-time 20 "${CRL_URL:-http://ca.home.example/1.0/crl}" -o "$tmp"
openssl crl -inform DER -in "$tmp" -out "$tmp.pem"
openssl crl -in "$dir/root.crl.pem" -noout
cat "$tmp.pem" "$dir/root.crl.pem" > "$dir/crl-chain.pem.new"
mv "$dir/crl-chain.pem.new" "$dir/crl-chain.pem"
${RELOAD:-nginx -s reload}
| 运行 | 结果 |
|---|---|
| 正常 | 新列表就位,nginx 已重载,phone1 返回 200,已吊销的 phone2 返回 400 |
| CA 无法访问 | curl: (22) The requested URL returned error: 404,退出码 22,旧列表原封不动,不重载 |
真正干活的是 set -e。一次失败的获取绝不能用一个空文件覆盖好的文件,因为坏掉的列表和过期的列表一样肯定会把所有人锁在外面。用 systemd 定时器或 cron 每隔几个小时跑一次。这样在任何东西过期之前它有好几次机会。没什么巧妙的,但门能一直用,靠的就是这一步。
给手机签发证书
每部手机一张证书,以手机命名,有效期一年:
step ca certificate phone1 phone1.crt phone1.key \
--provisioner admin --provisioner-password-file prov-pass \
--not-after 8760h --kty EC
手机要的是证书、私钥和中间证书合在一个 PKCS12 文件里,step 自己就能打包7:
step certificate p12 phone1.p12 phone1.crt phone1.key \
--ca certs/intermediate_ca.crt
MAC: sha256, Iteration 2048
PKCS7 Encrypted data: PBES2, PBKDF2, AES-256-CBC, Iteration 2048, PRF hmacWithSHA256
这是现代的加密方式,也正是 Home Assistant Companion 文档说应该先试的,不行再退回旧格式的容器8。
手机需要从 CA 拿三样东西,而不是一样:
| 文件 | 安装为 | 原因 |
|---|---|---|
root_ca.crt | CA 证书 | 让手机信任 step-ca 为代理签发的服务器证书 |
intermediate_ca.crt | CA 证书 | 根与 CA 实际签发的每张证书之间的那一环 |
phone1.p12 | VPN 和应用用户证书 | 手机自己的私钥,就是靠它进门 |
Home Assistant Android 应用除了内置的 CA 证书,也信任用户安装的 CA 证书9,所以 step-ca 签发的服务器证书可以用。对这件事来说它也是更好的一把锁。这个主机名只在家里和手机上有意义,一台没有你的根证书的设备连服务器都不信任,更别说通过它了。
手机丢了的时候:
step ca revoke --cert phone2.crt --key phone2.key --reason "phone lost"
Certificate with Serial Number 89334534279272070053632987406394420097 has been revoked.
新列表当场就生成了,下一次刷新会把它放到 nginx 前面。
CA 是你每天都要兑现的承诺
跑这套东西,让我印象最深的是签发证书的工作量有多小。一条命令。仅此而已。活儿全在它周围的承诺里:被吊销的手机一直进不来,好的手机一直进得来,列表永远不过期。step-ca 兑现了第一条,其余的留给你,而它的默认设置假定你是一群每天早上自己续期的服务器。
这不是缺陷,它本来就是为那种用户做的。但这正是一个工具实际做什么和它被宣传成做什么之间的差距。“自己运行一个 CA”听起来是一个容器加一个下午。实际上是一个容器、一个下午、一个模板、一份在日历上记着日期的根 CRL,还有一个你会忘记它存在、直到它停下那天的定时器。
还是去做。你自己运行的 CA,别人关不掉、改不了价、也读不到。只是要知道,拥有密钥就意味着承担杂活,而正是这些杂活让门一直关着。
Smallstep — step-ca configuration —
maxTLSCertDuration、defaultTLSCertDuration、minTLSCertDuration;crl配置块标为 “(experimental)”,cacheDuration“Defaults to 24h”。 ↩︎ ↩︎ ↩︎smallstep/certificates v0.30.2 — authority/tls.go — CRL 的 Issuing Distribution Point 在设置了
idpURL时取它,否则取 CA 自己的名字加/1.0/crl,并标为 critical。 ↩︎ ↩︎Smallstep — Certificate templates —
crlDistributionPoints“defines URLs with the certificate revocation list”。 ↩︎nginx — src/event/ngx_event_openssl.c —
ssl_crl设置X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL。 ↩︎nginx — ngx_http_ssl_module, ssl_crl — “When using intermediate certificates, their CRLs should be specified in the same file.” ↩︎
RFC 5280 — Internet X.509 PKI Certificate and CRL Profile — 第 4.2.1.13 节:DistributionPointName “SHOULD include at least one LDAP or HTTP URI”。 ↩︎
Smallstep — step certificate p12 — 把证书、它的私钥和 CA 证书打包成一个 PKCS12 文件。 ↩︎
Home Assistant Companion — Networking, TLS Client Authentication — 先试当前默认的 PKCS12,只有失败时才用 OpenSSL
-legacy容器。 ↩︎home-assistant/android — network_security_config.xml — 信任锚是
system加user:“Additionally trust user added CAs”。 ↩︎