Course contentsShow
← Node.js
Lesson 582 of 1,9806. Networking and HTTP FundamentalsFree lesson

Certificate Revocation Checking

Implement OCSP stapling to verify certificate validity without adding latency to the TLS handshake.

In this lesson, you will learn how to use OCSP Stapling to verify if a certificate has been revoked without forcing your users to wait for an extra network request during the TLS handshake.

The Problem with Traditional Checking

When a client connects to your server via TLS, it needs to ensure your certificate hasn't been revoked by the Certificate Authority (CA). Normally, the client would perform an OCSP (Online Certificate Status Protocol) request to the CA. This adds a round-trip delay to the handshake, slowing down your site, or worse, causing the handshake to fail if the CA's server is down.

The Solution: OCSP Stapling

With OCSP Stapling, your server takes on the responsibility of checking the certificate status. Periodically, your server reaches out to the CA itself, fetches a signed "time-stamped proof" that your certificate is still valid, and caches it.

During the TLS handshake, your server "staples" this proof directly into the initial handshake messages. The client receives the proof immediately, verifies the CA's signature, and trusts that the certificate is valid without ever needing to contact the CA itself.

Implementation Concept

In Node.js, you handle this via the tls module. Instead of the client making a request, you configure the server to provide the stapled response:

const tls = require('tls');

const options = {
  key: fs.readFileSync('server-key.pem'),
  cert: fs.readFileSync('server-cert.pem'),
  // Provide the OCSP response fetched from the CA
  // This is typically managed by a background task
  ocspRequest: (certificate, issuer, callback) => {
    // Logic to return your cached, signed OCSP status
    callback(null, cachedOcspResponse);
  }
};

const server = tls.createServer(options, (socket) => {
  socket.write('Secure connection established with stapled status!');
});

By providing this data proactively, you remove the "Revocation Check" latency from the client's perspective. It makes your server faster, more reliable, and private, as the CA doesn't see which clients are visiting your site.

Key Takeaway: OCSP Stapling allows the server to fetch and "staple" a certificate validity proof to the TLS handshake, eliminating the need for clients to perform slow, external revocation checks.