OCSP(Online Certificate Status Protocol)是用于实时检查证书是否被吊销的协议。相比 CRL,OCSP 无需下载完整吊销列表,查询效率更高。本文介绍 OCSP 原理及 OpenSSL 命令行操作。

什么是 OCSP?

当浏览器验证 TLS 证书时,除了检查证书链和有效期,还需确认证书未被吊销。传统方式是下载 CRL(Certificate Revocation List),但列表会随时间增长。OCSP 允许客户端直接查询证书颁发机构,获取特定证书的实时状态。

OCSP 响应类型

状态 含义
good 证书有效,未被吊销
revoked 证书已被吊销
unknown 响应者不知道该证书

从证书提取 OCSP 信息

获取证书的 OCSP 响应者地址

1
openssl x509 -in certificate.crt -noout -ocsp_uri

输出示例:

1
http://ocsp.digicert.com

获取颁发者信息

OCSP 查询需要知道证书的颁发者(CA):

1
2
3
4
5
# 获取证书颁发者
openssl x509 -in certificate.crt -noout -issuer

# 获取颁发者证书
openssl x509 -in certificate.crt -noout -issuer_hash

实际 OCSP 查询示例

使用 OpenSSL 进行 OCSP 查询

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# 获取目标证书和颁发者证书
openssl s_client -connect google.com:443 -showcerts 2>/dev/null | \
    openssl x509 -outform PEM > google.crt

# 获取中间 CA 证书
#(实际查询需要完整的证书链)

# 查询 OCSP 状态(带超时)
timeout 10 openssl ocsp \
    -issuer intermediate.crt \
    -cert google.crt \
    -url http://ocsp.digicert.com \
    -CAfile ca-bundle.crt \
    -text

常用参数说明

参数 说明
-issuer 颁发者证书(CA)
-cert 要检查的服务器证书
-url OCSP 响应者地址
-CAfile 受信任的 CA 证书 bundle
-text 输出详细信息
-timeout 连接超时时间

手动构建 OCSP 请求

从证书文件提取 OCSP 信息

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# 提取 OCSP URI
OCSP_URI=$(openssl x509 -in server.crt -noout -ocsp_uri)

# 提取颁发者证书
openssl x509 -in server.crt -noout -issuer > issuer.crt

# 提取服务器证书哈希(用于构造请求)
openssl ocsp \
    -issuer issuer.crt \
    -cert server.crt \
    -reqout ocsp_request.der

OCSP Stapling

OCSP Stapling 是服务器主动缓存 OCSP 响应并发送给客户端的技术,避免客户端每次都查询 OCSP 服务器。

检查服务器是否支持 OCSP Stapling

1
2
# 使用 openssl s_client 检查
echo | openssl s_client -connect google.com:443 -status 2>&1 | grep -A5 "OCSP response"

输出示例:

1
2
3
OCSP response:
OCSP Response Status: successful (0x0)
Response Type: OCSP Basic Response

在 Nginx 中启用 OCSP Stapling

1
2
3
4
5
6
7
8
9
server {
    # 启用 OCSP Stapling
    ssl_stapling on;
    ssl_stapling_verify on;
    
    # 指定解析器
    resolver 8.8.8.8 8.8.4.4 valid=300s;
    resolver_timeout 5s;
}

OCSP 响应器配置(CA 端)

如果你是证书颁发机构,需要配置 OCSP 响应器:

1
2
3
4
5
6
7
8
9
# 创建 OCSP 响应器证书
openssl ocsp \
    -port 9080 \
    -index index.txt \
    -CA ca.crt \
    -rsigner ocsp_responder.crt \
    -rkey ocsp_responder.key \
    -text \
    -out logfile.log

参数说明:

参数 说明
-port OCSP 服务端口
-index 证书状态索引文件
-CA CA 证书
-rsigner 响应器签名证书
-rkey 响应器私钥

注意事项

  1. 隐私考量:OCSP 查询会向 CA 暴露用户访问的网站,部分浏览器支持 “OCSP Must-Staple” 扩展来解决
  2. 性能影响:OCSP 查询增加握手延迟,建议启用 OCSP Stapling
  3. 容错设计:客户端在 OCSP 查询失败时通常允许继续访问(fail-open),这是安全与可用性的权衡

快速检查命令汇总

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# 1. 提取证书 OCSP 地址
openssl x509 -in cert.crt -noout -ocsp_uri

# 2. 提取颁发者信息
openssl x509 -in cert.crt -noout -issuer

# 3. 简单 OCSP 查询(需要网络)
timeout 5 openssl ocsp \
    -CAfile cabundle.pem \
    -issuer issuer.crt \
    -cert server.crt \
    -url http://ocsp.example.com

# 4. 检查服务器 OCSP Stapling
echo | openssl s_client -connect example.com:443 -status 2>/dev/null

小结

  • OCSP 用于实时查询证书是否被吊销
  • 相比 CRL 更高效,但增加网络延迟
  • OCSP Stapling 由服务器缓存响应,改善性能
  • 客户端应有合理的容错策略

参考来源