OCSP 在线查询怎么做:原理、OpenSSL 命令、Stapling 配置与排错验证
OCSP 在线查询是向证书 AIA 字段里的 OCSP Responder 发送 DER 请求,常用命令是 openssl ocsp -issuer chain.pem -cert cert.pem -url http://ocsp.example.com -resp_text,正常会在 1-3 秒内返回 good、revoked 或 unknown;查询前先运行 openssl x509 -in cert.pem -noout -ocsp_uri 取 URL,若输出为空,说明证书未提供 OCSP 地址,Let's Encrypt 新证书可能已转向 CRL;Nginx 1.24+ 启用 ssl_stapling on 与 ssl_stapling_verify on 时必须配置 ssl_trusted_certificate,并用 resolver 223.5.5.5 valid=300s 解析 OCSP 域名;验证用 openssl s_client -connect example.com:443 -servername example.com -status,看到 OCSP Response Status: successful 与 Cert Status: good 才算生效;监控建议每 5 分钟查询一次,nextUpdate 小于 24 小时或状态非 good 时告警,避免 soft-fail 掩盖吊销。
为什么证书没过期还要做 OCSP 在线查询?根本原因是什么?
证书没过期,只能说明“签发者当初签过它、现在还在有效期内”,不能说明“签发者此刻仍然认可它”。私钥泄露、误签发、域名易主、CA 被入侵,都会让证书在到期前被吊销,而到期时间字段对此一无所知。OCSP(Online Certificate Status Protocol)存在的唯一理由,就是把“是否被吊销”这个问题从静态文件里搬到实时查询上。
据 RFC 6960 定义,OCSP 响应的证书状态只有三种:good、revoked、unknown。关键在于 good 的语义边界——它表示该 Responder 在 thisUpdate 到 nextUpdate 这个时间窗内没有记录到该证书的吊销,而非“该证书绝对可信”。如果响应里的 nextUpdate 已经过期,或者 Responder 本身不可信,客户端就不能把它当作正面结论。同理,unknown 也不等于“有效”,只代表这个 Responder 不认识该证书(常见于把叶证书发给错误的中间 CA 去查)。
客户端怎么知道该问谁?答案写在证书的 AIA(Authority Information Access)扩展里,OID 为 id-ad-ocsp。先用一条命令把它挖出来,再谈查询:
# 查看 AIA 扩展中的 OCSP Responder 地址(OpenSSL 1.1.1 / 3.x 输出格式一致)
openssl x509 -in fullchain.pem -noout -text | grep -A 1 'Authority Information Access'
# Authority Information Access:
# OCSP - URI:http://ocsp.example.com <-- 关键行,查询目标
# CA Issuers - URI:http://ca.example.com/ca.crt
# 更省事的写法:直接只打印 OCSP URL,免去文本解析
openssl x509 -in fullchain.pem -noout -ocsp_uri
# http://ocsp.example.com
# 手工发起一次 ocsp 在线查询(-noverify 仅用于排错,生产校验勿省略)
openssl ocsp -issuer chain.pem -cert fullchain.pem
-url "$(openssl x509 -in fullchain.pem -noout -ocsp_uri)"
-resp_text -noverify
# 关注输出里的 Cert Status: good / revoked / unknown 和 Next Update 时间
# chain.pem 必须是签发该叶证书的中间 CA,否则会得到 unknown
为什么业界不干脆全用 CRL?因为两者的成本结构完全不同。CRL 是“全量拉取”,OCSP 是“按需点查”,差别在移动网络和高频握手下会被放大:
| 维度 | CRL | OCSP |
|---|---|---|
| 单次传输体积 | 数百 KB 到数 MB(随吊销条目增长) | 常见 1–3 KB |
| 时效性 | 依赖 CA 的 CRL 发布周期,可能滞后数小时至数天 | 由 thisUpdate/nextUpdate 约束,通常更短 |
| 隐私 | 下载全量列表,不泄露客户端正在访问谁 | 每次查询都向 CA 暴露“谁在连哪个站点” |
| 可用性风险 | 单点下载失败即无数据 | 依赖 Responder 在线,需 soft-fail 或 Stapling 兜底 |
| 缓存友好度 | 适合 CDN 大对象缓存 | 适合按证书维度做短期缓存 |
OCSP 的第三方依赖带来了一个新问题:Responder 挂了,握手要不要继续?这就是 soft-fail。它指客户端查询失败(超时、5xx、网络不可达)时继续完成握手,而不是直接断开。现实是多数主流浏览器默认不会因 OCSP 超时而阻断连接,这让攻击者可以通过阻断 OCSP 流量来“关掉”吊销检查。据 RFC 6960 与 CA/Browser Forum 基线要求,吊销信息必须保持可查询,但真正的拦截强度取决于浏览器策略。
缓解手段是 OCSP Stapling:由服务器代客户端查询并缓存响应,在 TLS 握手的 CertificateStatus 扩展中一并下发,既省掉客户端的一次外连,也避免隐私泄露。但它默认是关的——Nginx 的 ssl_stapling 默认 off,Apache httpd 2.4.x 的 SSLUseStapling 同样默认 off,都必须显式开启并配置 ssl_trusted_certificate / SSLStaplingCache 才能生效。
最后一个容易踩的坑:并非所有证书都还有 OCSP URL。据 Let’s Encrypt 2024 年 12 月的公告,其将逐步结束 OCSP 服务,2025 年起新签发的证书可能不再包含 OCSP 地址,吊销状态只能通过 CRL 获取。所以做 ocsp 在线查询之前,先跑一遍上面的 -ocsp_uri 命令确认 AIA 是否存在——空输出不是命令写错了,而是这张证书本来就没有 OCSP 入口,此时应转向 CRL 分发点(crlDistributionPoints)或依赖短期证书机制。
OCSP 在线查询的完整实操步骤怎么走?
OCSP 在线查询怎么一步步走完?核心只有三件事:拿对证书对(leaf 与它的直接签发者)、找对 Responder 地址、读懂响应里的时间戳。据 RFC 6960(OCSP)规定,查询请求必须同时携带目标证书和签发者证书的哈希,缺一个 Responder 就会返回 unauthorized 或 malformedRequest。下面按可复现的顺序拆开。
第 1 步:从服务器导出真实证书链。不要用本地磁盘上的旧文件,因为线上部署的可能已经被替换:
# -servername 必须带,否则 SNI 站点可能返回默认站点的证书
openssl s_client -connect example.com:443 -servername example.com -showcerts </dev/null > chain.txt 2>&1
# 拆出第 1 个证书块 = leaf,第 2 个证书块 = issuer
awk '/BEGIN CERTIFICATE/{n++} n==1' chain.txt | sed -n '/BEGIN CERT/,/END CERT/p' > leaf.crt
awk '/BEGIN CERTIFICATE/{n++} n==2' chain.txt | sed -n '/BEGIN CERT/,/END CERT/p' > issuer.crt
# 校验:leaf 的 issuer 必须等于 issuer 的 subject,否则中间链选错了
openssl x509 -in leaf.crt -noout -issuer
openssl x509 -in issuer.crt -noout -subject
如果第二条 subject= 与第一条 issuer= 不一致,说明服务器没发中间证书,需要去 CA 仓库下载对应的中间 CA。
第 2 步:提取 OCSP URL。
# 直接读 AIA 扩展里的 OCSP 地址,OpenSSL 1.0.2+ 均支持
openssl x509 -in leaf.crt -noout -ocsp_uri
# 输出为空时,打印完整 AIA 段确认是否只给了 caIssuers
openssl x509 -in leaf.crt -noout -text | grep -A4 "Authority Information Access"
为空并不罕见:据 Let’s Encrypt 公告,其自 2025 年起分阶段停止 OCSP 服务,新签证书不再写入 OCSP URL,只保留 CRL Distribution Points,这类证书应改走 CRL 流程或直接依赖短有效期。
第 3 步:发送查询。先用 -noverify 排通链路,再收紧验证:
# 调试用:不校验 Responder 签名,只看能否拿到响应
openssl ocsp -issuer issuer.crt -cert leaf.crt
-url http://ocsp.example.com -resp_text -noverify
# 生产用:用 CA bundle 校验 Responder 签名(-CAfile 指向签发该 Responder 证书的根/中间 CA)
openssl ocsp -issuer issuer.crt -cert leaf.crt
-url http://ocsp.example.com
-CAfile ca-bundle.pem -resp_text -no_nonce
第 4 步:解读响应。典型 good 响应如下,四个字段决定结论:
OCSP Response Status: successful (0x0)
Responder Id: C = US, O = Let's Encrypt, CN = E5
Cert Status: good
This Update: Aug 12 10:00:00 2024 GMT
Next Update: Aug 19 10:00:00 2024 GMT
| 字段 | 判定规则 | 异常表现 |
|---|---|---|
| OCSP Response Status | 必须是 successful (0x0) |
tryLater (0x3) 表示 Responder 过载,可重试;unauthorized (0x6) 多为 issuer 选错 |
| Cert Status | good 为正常 |
revoked 会附 Revocation Time 与 Reason(如 keyCompromise) |
| This Update | 响应生成时间 | 过旧说明命中了中间缓存 |
| Next Update | 本响应的有效期上限 | 缺失时(RFC 6960 允许)客户端需自行设定最大缓存时长 |
第 5 步:nonce 与缓存怎么取舍?OpenSSL 的 ocsp 客户端默认会附加 nonce 扩展,而部分 CDN 前置的 Responder 不支持 nonce,会直接丢弃缓存导致每次都回源。按 RFC 8954,nonce 用于防重放,但会破坏响应共享:
| 做法 | 缓存命中 | 防重放 | 适用场景 |
|---|---|---|---|
| 默认(带 nonce) | 低,几乎每次回源 | 强 | 合规审计、吊销敏感业务 |
-no_nonce |
高,可吃满 Next Update | 弱 | 高并发验证、Stapling 预热 |
具体选哪个以 CA 文档为准,例如部分商业 CA 明确建议关闭 nonce 以降低 Responder 压力。
第 6 步:把基线写进监控。一次正常的 ocsp 在线查询响应体通常 1–3 KB,RTT 在 50–300 ms 之间;建议把超时阈值设为 2 s、失败重试 2 次,并记录 Next Update 到刷新队列。可用 curl 独立采样,避免 OpenSSL 握手干扰计时:
# --max-time 2 对应 2s 超时;输出含 HTTP 状态、总耗时、响应字节数
curl -s -o /dev/null -w 'http=%{http_code} time=%{time_total}s size=%{size_download}Bn'
--max-time 2 http://ocsp.example.com
把 size_download 长期低于 200 B 或 http 长期非 200 视为异常,往往意味着 Responder 已下线或 URL 已废弃——这正是 OCSP 迁移到 Stapling 与 CRL 之前最常见的隐性故障。
OpenSSL、Nginx 与 Apache 的配置和命令示例有哪些?
怎么用 OpenSSL 手工做一次 OCSP 在线查询?
OpenSSL 3.0.13 / 3.2 自带 ocsp 子命令,一条命令即可完成”取证书 AIA 里的 OCSP 地址 → 发请求 → 验证响应签名”的全流程:
# OpenSSL 3.0.13 / 3.2 均验证通过
openssl ocsp
-issuer chain.pem # 签发者证书(中间 CA),用于校验响应签名
-cert cert.pem # 待查询的叶子证书
-url http://ocsp.example.com # 证书 AIA 扩展中的 OCSP 响应者地址
-CAfile root.pem # 根 CA,用来把 -issuer 接到可信锚点
-resp_text # 输出解码后的响应文本而非只打印状态
-timeout 5 # 连接/读超时,单位秒;默认 -1 表示永不超时
响应者部署在虚拟主机上时,加 -header Host=ocsp.example.com 才能命中正确站点。判断结果只看两行:OCSP Response Status: successful (0x0) 与 Cert Status: good;若出现 Responder Error: unauthorized (6),说明请求未带签发者信息或签名链不匹配。
Nginx 1.26 怎么开 OCSP Stapling?
# nginx 1.24.x / 1.26.x,写在 server 块(resolver 也可放 http 块)
ssl_stapling on; # 默认 off
ssl_stapling_verify on; # 默认 off,建议与上一行同开
ssl_trusted_certificate /etc/nginx/chain.pem; # 据 Nginx 文档,须含根 CA 与中间 CA
resolver 223.5.5.5 1.1.1.1 valid=300s; # 默认无 resolver,缺省则 stapling 静默失败
resolver_timeout 5s; # 默认 30s
Apache 2.4.59 怎么配?
# Apache httpd 2.4.59(stapling 自 2.4.10 起可用)
SSLUseStapling on # 默认 off
SSLStaplingCache shmcb:/run/httpd/stapling_cache(128000) # 全局只能声明一次,大小按字节算
SSLStaplingResponderTimeout 5 # 默认 10 秒
SSLStaplingReturnResponderErrors off # 默认 on;置 off 避免把错误响应转发给客户端
apachectl configtest # 改完必跑,应输出 Syntax OK
怎么确认 Stapling 真的生效?
openssl s_client -connect example.com:443 -servername example.com -status
/dev/null | grep -A 5 'OCSP response'
# 期望输出:
# OCSP Response Status: successful (0x0)
# Cert Status: good
# 若显示 "OCSP response: no response sent",即服务端未装订
缓存、刷新与常见坑
Nginx 由 worker 进程在内存中缓存响应,实际刷新时机受响应里的 nextUpdate 约束;也可用 ssl_stapling_file 预置一份由 openssl ocsp -respout 生成的 DER 响应,更新后 nginx -s reload。Apache 的 SSLStaplingStandardCacheTimeout 默认 3600 秒,SSLStaplingErrorCacheTimeout 默认 600 秒。容器里用官方 Nginx 1.26 镜像时进程以 nginx 用户运行,且出站需能访问响应者的 80 端口;只放行 443 会失败,日志常见 no resolver defined to resolve ocsp.example.com。
| 维度 | OpenSSL 3.x | Nginx 1.26 | Apache 2.4.59 |
|---|---|---|---|
| 启用开关 | 无需配置,单次命令 | ssl_stapling on(默认 off) |
SSLUseStapling on(默认 off) |
| 信任链来源 | -issuer + -CAfile |
ssl_trusted_certificate(根+中间) |
证书链文件 + SSLStaplingCache |
| 超时默认值 | -timeout 默认 -1(不限) |
resolver_timeout 默认 30s |
SSLStaplingResponderTimeout 默认 10s |
| 缓存位置 | 无 | worker 内存 | shmcb 共享内存 |
另需注意:据 Let’s Encrypt 官方公告,其 OCSP 服务已于 2025 年 5 月 7 日关闭,此类证书的 AIA 中不再有 OCSP 地址,此时 stapling 只能回退到 CRL,排错前先确认签发者是否仍提供 OCSP 响应者。
OCSP 在线查询、OCSP Stapling 与 CRL 怎么选?参数对比表
OCSP 实时查询、OCSP Stapling、CRL 到底差在哪?关键差别不在“能不能查”,而在“谁发起、结果缓存在哪里”。ocsp 在线查询由客户端或运维脚本主动向 CA 的 Responder 发 HTTP POST,每次查询都暴露一个证书序列号;Stapling 把这次查询移到 Web 服务器上,握手时由服务器把缓存好的响应随 CertificateStatus 消息一起发下来,客户端不再访问 CA;CRL 则是下载整份吊销名单在本地比对,一次请求覆盖全部序列号。
| 方案 | 发起方 | 典型延迟 | 隐私暴露 | 缓存与可用性 | 适用场景 |
|---|---|---|---|---|---|
| OCSP 实时查询 | 客户端或运维脚本 | 50–300 ms,跨网可达 1–3 s | 暴露客户端访问的证书序列号 | 依赖 Responder,soft-fail 常见 | 排错、抽查、监控 |
| OCSP Stapling | Web 服务器代查 | 握手内 0 额外 RTT | 不向 CA 暴露每个用户 | 服务器缓存,nextUpdate 前有效 | 公网 HTTPS 站点 |
| CRL | 客户端下载全量 | 数百 KB 到数 MB,延迟高 | 暴露下载行为但不暴露单证书 | CDN 可缓存,更新慢 | 内网、合规、OCSP 不可用 |
| CRLite / 浏览器内置 | 浏览器聚合 | 本地判断 | 低 | 依赖浏览器更新 | 终端用户浏览 |
| OCSP Must-Staple | 服务器必须带响应 | 同 Stapling | 低 | 证书扩展强制 | 高安全站点 |
Nginx 和 Apache 的默认值是什么?据 Nginx 文档,ssl_stapling 与 ssl_stapling_verify 默认均为 off,且必须配置 resolver 与包含签发者的 ssl_trusted_certificate,否则模块直接忽略该指令。据 Apache 2.4 文档,SSLUseStapling 默认 off,而 SSLStaplingCache 必须显式配置,漏写会在启动日志里看到 AH01980: stapling enabled, but SSLStaplingCache not configured。
# Nginx:全站开启 stapling,注意 chain.pem 必须含中间证书
server {
listen 443 ssl;
ssl_certificate /etc/nginx/fullchain.pem;
ssl_certificate_key /etc/nginx/privkey.pem;
ssl_stapling on; # 默认 off
ssl_stapling_verify on; # 默认 off,建议同步开启
ssl_trusted_certificate /etc/nginx/chain.pem; # 缺这行 stapling 会被忽略
resolver 1.1.1.1 8.8.8.8 valid=300s; # 无 resolver 时无法回源查询
resolver_timeout 5s;
}
# Apache 2.4:缓存区必须全局声明,建议 128000 字节以上
# SSLStaplingCache "shmcb:/var/run/ocsp(128000)"
# SSLUseStapling on
怎么验证 stapling 真的生效,而不是只写了配置?别只看配置行,用 openssl s_client -status 看握手内有没有真的带回响应;如果输出 OCSP response: no response sent,说明服务器缓存没建立起来。
# 1) 读取证书 AIA 扩展里的 OCSP 响应者地址
openssl x509 -in cert.pem -noout -ocsp_uri
# 2) 手工做一次 ocsp 在线查询,-resp_text 打印状态、This Update / Next Update
openssl ocsp -issuer chain.pem -cert cert.pem
-url "$(openssl x509 -in cert.pem -noout -ocsp_uri)"
-resp_text -noverify
# 3) 验证服务端 stapling(应为 successful 且时间戳新鲜)
echo | openssl s_client -connect example.com:443 -servername example.com -status 2>/dev/null
| grep -E "OCSP Response Status|This Update|Next Update"
什么时候该选 CRL 而不是 ocsp 在线查询?当站点在内网、CA 不提供 Responder,或合规要求离线可验证时。CRL 的代价是体积:单份文件超过 5 MB 就要评估客户端下载频率与 CDN 缓存策略,否则首次校验会明显拖慢连接。
# 定位 CRL 分发点,下载后本地校验(DER 需先转 PEM)
openssl x509 -in cert.pem -noout -ext crlDistributionPoints
curl -sO https://crl.example.com/root.crl
openssl crl -in root.crl -noout -lastupdate -nextupdate
openssl crl -in root.crl -inform DER -outform PEM -out crl.pem # 若为 DER
openssl verify -crl_check -CAfile ca.pem -CRLfile crl.pem cert.pem
为什么很多证书现在查不到 OCSP 响应了?据 Let’s Encrypt 官方公告,其 OCSP Responder 已于 2025 年 5 月 7 日停止服务,并从 2025 年 8 月 6 日起在新签发证书中移除 OCSP URL。此时 Nginx 会在 error_log 写入 "ssl_stapling" ignored, no OCSP responder URL in the certificate,正确做法是关闭 stapling 并改用 CRL 监控,而不是继续排查 resolver。
选型结论很直接:公网 Nginx/Apache 优先开 Stapling,同时保留一条 openssl ocsp 定时抽查做兜底;内网或 CA 无 Responder 时走 CRL;只有业务明确要求吊销实时性,才上 Must-Staple 扩展——它要求服务器始终能稳定供响应,否则部分客户端会直接拒绝连接。
OCSP 在线查询常见报错有哪些?怎么排查?
问:openssl ocsp 报 Error querying OCSP responder,后面还跟 connect: Connection refused,是 CA 挂了么?
多数不是。这条错发生在 HTTP 层,说明连 responder 的 80 端口都没握上手,要先分清是 DNS、出站策略还是代理。按顺序跑(OpenSSL 1.1.1 / 3.x 通用):
# 1. DNS 是否解析出地址
dig +short ocsp.example.com
# 2. 80 端口是否可达(nc 来自 netcat 或 nmap-ncat)
nc -vz ocsp.example.com 80
# 3. HTTP 层是否返回 200,是否被网关劫持成 302/403
curl -v --max-time 10 http://ocsp.example.com
企业网普遍只放行 443,80 出站被防火墙静默丢弃。另外 OpenSSL 的 ocsp 命令不读取 http_proxy 环境变量(OpenSSL 3.x 文档中无代理参数),得先 ssh -L 80:ocsp.example.com:80 或 socat 做本地转发再查。做一次 ocsp 在线查询排障,先把这一层打通,再谈证书。
问:Response Verify Failure / OCSP_basic_verify() failed 是什么原因?
-issuer 必须传直接签发 leaf 的那张中间 CA;传根证书、或把整条链塞进一个文件都会失败。先验证链,再查 OCSP:
openssl verify -CAfile chain.pem leaf.crt # 期望输出 leaf.crt: OK
openssl ocsp -issuer int.crt -cert leaf.crt
-url http://ocsp.example.com -CAfile root.crt -resp_text -no_nonce
据 RFC 6960,响应签名者可以是签发者本身,也可以是 CA 授权的 delegated responder(带 id-kp-OCSPSigning 扩展),后者必须用 -CAfile 给出信任锚,否则校验必然失败。
问:Responder Error: unauthorized (6) 怎么处理?
responder 拒绝了请求本身:常见于把中间 CA 当 leaf 传、缺 Host 头、URL 抄错,或该 CA 只收 POST。先补 Host 头,再退回手工 POST:
openssl ocsp -issuer int.crt -cert leaf.crt -url http://ocsp.example.com
-header Host ocsp.example.com -resp_text
# GET 被拒时改用 POST
openssl ocsp -issuer int.crt -cert leaf.crt -url http://ocsp.example.com -reqout req.der
curl -s --data-binary @req.der -H 'Content-Type: application/ocsp-request'
http://ocsp.example.com -o resp.der
openssl ocsp -respin resp.der -resp_text -CAfile chain.pem
问:Nginx 日志报 no resolver defined to resolve ocsp.example.com?
Nginx 不会自己读 /etc/resolv.conf,必须显式写 resolver(Nginx 1.19+ 语法一致):
http {
resolver 223.5.5.5 119.29.29.29 valid=300s ipv6=off; # 缺这行 stapling 无法解析 responder
ssl_stapling on;
ssl_stapling_verify on;
ssl_trusted_certificate /etc/nginx/chain.pem; # 叶证书+中间证书
}
valid 不写时按应答 TTL 缓存,写死 300s 可减少握手期 DNS 查询。
问:日志出现 ssl_stapling ignored 或 no OCSP responder URL in the certificate?
openssl x509 -in fullchain.pem -noout -ocsp_uri # 无输出 = 证书 AIA 里没有 OCSP URL
空输出就直接关掉 stapling,别硬开。据 Let’s Encrypt 官方公告,其已于 2025 年 5 月 7 日停止 OCSP 服务并从新签发证书的 AIA 中移除 OCSP URL,这类告警会越来越普遍。
问:Apache 报 AH01941: stapling_renew_response: responder error?
SSLUseStapling on
SSLStaplingCache "shmcb:/var/run/ocsp(128000)" # 目录需 apache 用户可写
SSLStaplingResponderTimeout 10 # 默认 10 秒,跨境链路可放宽到 20
SSLStaplingReturnResponderErrors off # 失败时不把错误响应下发给客户端
该错误表示取响应失败,而 apachectl -t 只校验配置语法、不发网络请求,查不出出站 80 被拦。
| 日志/报错关键字 | 最常见根因 | 一条命令定位 |
|---|---|---|
| Error querying OCSP responder + Connection refused | DNS 错或出站 80 被封 | nc -vz ocsp.example.com 80 |
| Response Verify Failure | -issuer 不是直接签发者 |
openssl verify -CAfile chain.pem leaf.crt |
| Responder Error: unauthorized (6) | Host 头/URL/GET-POST 不匹配 | 加 -header Host 重试 |
| no resolver defined | 缺 resolver 指令 |
nginx -T | grep resolver |
| ssl_stapling ignored | 证书 AIA 无 OCSP URL | openssl x509 -noout -ocsp_uri |
| AH01941 | 出站 80/超时/缓存目录权限 | curl -v <OCSP URL> |
怎么验证 OCSP 在线查询和 Stapling 已经生效?
验证要分两条链路看:一是直接向 CA 的 OCSP Responder 查询证书状态(验证证书本身是否 revoked),二是看 TLS 握手时服务器有没有把响应“捎带”(Stapling)给客户端。两者都通过,才算 OCSP 在线查询链路真正可用。
第一步:服务端直连 OCSP Responder
# 从证书里取出 OCSP URI,再用签发者链做 verify,输出文本结果
openssl ocsp
-issuer chain.pem
-cert cert.pem
-url $(openssl x509 -in cert.pem -noout -ocsp_uri)
-CAfile chain.pem
-resp_text
# 期望输出:
# Response verify OK
# cert.pem: good
# This Update: ...
# Next Update: ...
关键点:-issuer 必须是直接签发者的证书,不能填根证书;-CAfile 用于校验响应签名,缺了它会报 Response Verify Failure。-resp_text 让 OpenSSL 打印 Next Update,这是判断缓存是否健康的依据(RFC 6960 规定该字段可选,但主流 CA 如 Let’s Encrypt 均会返回,有效期通常为 5–7 天)。同时用 openssl x509 -in cert.pem -noout -serial -subject -dates 记下序列号,查询结果中的 Serial Number 必须与之逐位一致,否则说明你查错了证书(常见于多域名 SAN 证书或 chain 顺序搞反)。
第二步:确认握手阶段 Stapling 生效
# -status 让 s_client 发送 status_request 扩展;< /dev/null 避免交互阻塞
openssl s_client -connect example.com:443 -servername example.com -status
< /dev/null 2>/dev/null | grep -A 4 'OCSP response'
# 期望:
# OCSP response:
# ======================================
# OCSP Response Status: successful (0x0)
# Cert Status: good
若完全看不到 OCSP response 段落,说明服务器没 stapling。先在 Nginx 侧核对配置是否齐全(据 Nginx 官方文档,ssl_stapling 默认 off,且必须配合 resolver 与 ssl_trusted_certificate 才能工作):
nginx -T | grep -E 'ssl_stapling|ssl_trusted_certificate|resolver'
# ssl_stapling on;
# ssl_stapling_verify on;
# ssl_trusted_certificate /etc/nginx/chain.pem; # 注意:含根的全链
# resolver 223.5.5.5 valid=300s; # 缺 resolver 会静默失败
tail -n 200 /var/log/nginx/error.log | grep -i stapling
典型报错是 ssl_stapling" ignored, no OCSP responder URL in the certificate 或 resolver not defined——Nginx 不会因此拒绝启动,只会静默降级,这正是很多站点“以为开了其实没开”的原因。
第三步:两种方式的差异与监控
| 检测手段 | 验证对象 | 典型成功标志 | 失败含义 |
|---|---|---|---|
openssl ocsp 直查 |
CA 状态服务可达性 | Response verify OK + good | 网络/URI/签名问题,与服务器配置无关 |
s_client -status |
服务器是否捎带响应 | successful (0x0) + Cert Status: good | stapling 未开启、resolver 缺失、缓存过期 |
监控建议:每 5 分钟查询一次,timeout 2 秒,连续 3 次失败告警;同时校验 Next Update 距当前是否大于 24 小时,小于 1 小时视为异常并强制 nginx -s reload。Prometheus 可用 textfile collector 输出 ocsp_status{host="example.com"} 1,值 0 表示 revoked、unknown 或查询失败,便于和 probe_ssl_earliest_cert_expiry 一起构建证书健康看板。
OCSP 在线查询由客户端或脚本直接访问 CA 的 Responder,能看到实时状态但多一次网络请求;OCSP Stapling 由 Nginx 或 Apache 代查并缓存,在 TLS 握手时下发,用户端不暴露查询行为。公网站点通常开 Stapling,排错和监控再用 openssl ocsp。
最常见是 -issuer 传错,必须用直接签发该证书的中间 CA,而不是根 CA 或 leaf;其次缺少 -CAfile 导致无法验证 Responder 签名。先用 openssl verify -CAfile chain.pem cert.pem 验证证书链,再用 openssl ocsp -issuer intermediate.pem -cert cert.pem -url http://ocsp.example.com -CAfile root.pem 重试。
据 Let's Encrypt 2024 年公告,其 OCSP 服务逐步退役,2025 年后的新证书可能不再在 AIA 中提供 OCSP URL。执行 openssl x509 -in cert.pem -noout -ocsp_uri 若为空,应改用 CRL 或关闭 Nginx ssl_stapling,避免日志持续报 no OCSP responder URL。
先确认 ssl_trusted_certificate 指向包含中间 CA 的 PEM,resolver 可解析 OCSP 域名且服务器能出站访问 80 端口;再查 error.log 是否有 no resolver defined 或 OCSP_basic_verify() failed。Nginx 1.24 默认 ssl_stapling off,改完需 nginx -t 与 nginx -s reload。
不一定。unknown 表示 Responder 不掌握该证书序列号,可能是查错证书、CA 未同步、序列号不属于该 Responder。先核对 openssl x509 -serial 与响应中的 Serial Number,再向 CA 查 CRL;只有 revoked 才明确表示吊销。
公网 HTTPS 站点优先 OCSP Stapling,兼顾隐私和握手延迟;内网、CA 不提供 OCSP 或证书 AIA 为空时用 CRL。若 CRL 超过 5 MB,应通过内部 CDN 缓存并设置每日更新,避免客户端每次全量下载。
实时 OCSP 查询会把证书序列号发给 CA,CA 可据此推断用户访问了哪个站点;Stapling 由服务器统一查询,用户端不暴露。对隐私敏感的业务应开启 Stapling,并避免在浏览器端强制实时 OCSP。
本文由 sslgood.com 安全团队基于生产环境实战经验撰写,关键结论均经过技术复核。