OCSP Stapling 原理与优势
本文将阐述 OCSP Stapling 的技术原理及其在提升连接性能方面的必要性。
在线证书状态协议(Online Certificate Status Protocol,简称 OCSP)是 RFC 6960 定义的用于查询 X.509 数字证书撤销状态的网际协议。该协议主要用于验证证书的合法性,通常由颁发证书的 CA 提供查询服务。由于 OCSP 查询本质上是一次完整的 HTTP 请求与响应过程,涉及 DNS 解析、TCP 连接建立及 Web 服务端处理等环节,这些额外步骤会延长 TLS 握手的耗时。
为解决上述延迟问题,OCSP Stapling(OCSP 封套)机制应运而生。在该机制下,Web 服务器主动从 CA 获取 OCSP 查询结果,并在 TLS 握手过程中将其作为“封套”随证书一同发送给客户端。这使得客户端无需再独立发起验证请求,从而有效提升了 TLS 握手的效率。
生成 OCSP Stapling 文件
以下是生成用于 OCSP Stapling 验证所需文件的详细步骤。
首先,需要收集三份关键的证书文件:
站点证书(website.pem)、根证书(root.pem)以及中间证书(intermediate.pem)。
其中,中间证书和根证书需根据你所使用的证书颁发机构(CA)的具体要求下载对应的版本。
以下提供了 Let's Encrypt 相关证书的下载地址列表:
根证书: DST Root CA X3 https://ssl-tools.net/certificates/dac9024f54d8f6df94935fb1732638ca6ad77c13.pem ISRG Root X1 https://letsencrypt.org/certs/isrgrootx1.pem
中间证书: Let's Encrypt Authority X1 https://letsencrypt.org/certs/lets-encrypt-x1-cross-signed.pem Let's Encrypt Authority X2 https://letsencrypt.org/certs/lets-encrypt-x2-cross-signed.pem Let's Encrypt Authority X3 https://letsencrypt.org/certs/lets-encrypt-x3-cross-signed.pem Let's Encrypt Authority X4 https://letsencrypt.org/certs/lets-encrypt-x4-cross-signed.pem
本文以 DST Root CA X3 根证书配合 Let's Encrypt Authority X3 中间证书为例(目前 Let's Encrypt 签发的证书多采用此组合):
# 下载根证书和中间证书
wget -O root.pem https://ssl-tools.net/certificates/dac9024f54d8f6df94935fb1732638ca6ad77c13.pem
wget -O intermediate.pem https://letsencrypt.org/certs/lets-encrypt-x3-cross-signed.pem
# 生成 OCSP Stapling 验证文件
# 注意,中间证书在上、根证书在下
cat cat intermediate.pem > chained.pem
cat root.pem >> chained.pem
完成合并后,生成的 chained.pem 即为所需的 OCSP Stapling 验证文件。
OCSP Stapling Response
openssl x509 -in website.pem -noout -ocsp_uri
执行相应命令后,系统将返回你的证书所关联的 OCSP 服务地址。
例如,Let's Encrypt 当前使用的 OCSP 服务地址为 http://ocsp.int-x3.letsencrypt.org/。
以 Let's Encrypt 为例,获取站点证书的 OCSP 响应信息。
openssl ocsp -no_nonce \
-issuer intermediate.pem \
-CAfile chained.pem \
-VAfile chained.pem \
-cert website.pem \
-url http://ocsp.int-x3.letsencrypt.org \
-text
若操作无误,终端将输出如下内容:
Response verify OK website.pem: good This Update: Oct 24 00:00:41 2026 GMT Next Update: Oct 31 00:00:41 2026 GMT
Nginx 启用 OCSP Stapling
ssl_stapling on;
ssl_stapling_verify on;
ssl_trusted_certificate ~/chained.pem;
resolver 208.67.222.222 valid=300s;
resolver_timeout 5s;
随后重启 Nginx 服务,即可成功启用 OCSP Stapling 功能。
OCSP Stapling Status
复制代码 代码如下: openssl s_client -connect sometimesnaive.org:443 -status -tlsextdebug < /dev/null 2>&1 | grep -i "OCSP response"
如果站点已成功启用 OCSP Stapling,将会返回以下信息:
OCSP response:
OCSP Response Data:
OCSP Response Status: successful (0x0)
Response Type: Basic OCSP Response
如果返回此提示,则表明配置失败或未生效。
OCSP response: no response sent
此外,还可以访问 ssllabs 进行全面的 SSL 测试,报告中也会明确显示 OCSP Stapling 的开启状态。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持本站。