'OCSPRequest' 事件


当客户端发送证书状态请求时会触发 'OCSPRequest' 事件。 监听器回调在调用时传入三个参数:

  • certificate <Buffer> 服务器证书
  • issuer <Buffer> 发行人证书
  • callback <Function> 必须调用的回调函数来提供 OCSP 请求的结果。

可以解析服务器当前的证书,获取 OCSP URL 和证书 ID;获取 OCSP 响应后,再调用 callback(null, resp),其中 resp 是包含 OCSP 响应的 Buffer 实例。 certificateissuer 都是主证书和颁发者证书的 Buffer DER 表示。 这些可用于获取 OCSP 证书 ID 和 OCSP 端点 URL。

或者,可以调用 callback(null, null),表示没有 OCSP 响应。

调用 callback(err) 将导致调用 socket.destroy(err)

OCSP 请求的典型流程如下:

  1. 客户端连接到服务器并发送 'OCSPRequest'(通过 ClientHello 中的状态信息扩展)。
  2. 服务器收到请求并触发 'OCSPRequest' 事件,如果已注册则调用监听器。
  3. 服务器从 certificateissuer 中提取 OCSP URL,并向 CA 执行 OCSP 请求
  4. 服务器从 CA 接收 'OCSPResponse' 并通过 callback 参数将其发送回客户端
  5. 客户端验证响应并销毁套接字或执行握手。

如果证书是自签名证书或颁发者不在根证书列表中,则 issuer 可以是 null。 (在建立 TLS 连接时可以通过 ca 选项提供颁发者。)

监听此事件只会对添加事件监听器后建立的连接有影响。

asn1.js 这样的 npm 模块可用于解析证书。

The 'OCSPRequest' event is emitted when the client sends a certificate status request. The listener callback is passed three arguments when called:

  • certificate <Buffer> The server certificate
  • issuer <Buffer> The issuer's certificate
  • callback <Function> A callback function that must be invoked to provide the results of the OCSP request.

The server's current certificate can be parsed to obtain the OCSP URL and certificate ID; after obtaining an OCSP response, callback(null, resp) is then invoked, where resp is a Buffer instance containing the OCSP response. Both certificate and issuer are Buffer DER-representations of the primary and issuer's certificates. These can be used to obtain the OCSP certificate ID and OCSP endpoint URL.

Alternatively, callback(null, null) may be called, indicating that there was no OCSP response.

Calling callback(err) will result in a socket.destroy(err) call.

The typical flow of an OCSP Request is as follows:

  1. Client connects to the server and sends an 'OCSPRequest' (via the status info extension in ClientHello).
  2. Server receives the request and emits the 'OCSPRequest' event, calling the listener if registered.
  3. Server extracts the OCSP URL from either the certificate or issuer and performs an OCSP request to the CA.
  4. Server receives 'OCSPResponse' from the CA and sends it back to the client via the callback argument
  5. Client validates the response and either destroys the socket or performs a handshake.

The issuer can be null if the certificate is either self-signed or the issuer is not in the root certificates list. (An issuer may be provided via the ca option when establishing the TLS connection.)

Listening for this event will have an effect only on connections established after the addition of the event listener.

An npm module like asn1.js may be used to parse the certificates.