Node.js

A Complete Guide to Securely Connecting Node.js Servers and Clients Using Mutual TLS

How to use TLS, client authentication, and CA certificates in Node.js Servers and Clients

Create a private key and request a certificate for your Node.js server

Before you can teach your server to speak TLS, you will need a certificate issued by a trusted certificate authority (CA). If your organization already runs its own CA and you have a private key and certificate for your Node.js server, along with your CA's root certificate, you can skip to the next step.

To request a certificate from your CA using the step CLI, bootstrap your CA with step ca bootstrap and run the following command (sub the server name for the actual name / DNS name of your Node.js server).

$ step ca certificate "myserver.internal.net" server.crt server.key

Your certificate and private key will be saved in server.crt and server.key respectively.

Request a copy of your CA root certificate, which will be used to make sure each application can trust certificates presented by other applications.

$ step ca root ca.crt

Your certificate will be saved in ca.crt.

Configure Node.js to authenticate itself with its TLS certificate

We now want to instruct our Node.js server to identify itself using the certificate issued in the last step and to force clients to connect over TLS.

In your https server, specify the locations of the server's certificate and private key.

const fs = require('fs'); const https = require('https'); https .createServer( { // ... cert: fs.readFileSync('server.crt'), key: fs.readFileSync('server.key') // ... }, (req, res) => { res.writeHead(200); res.end('Hello, world!'); } ) .listen(9443);

Improve this content

Configure Node.js to require clients to authenticate with a certificate issued by your CA

To tell Node.js to use mutual TLS and not just one-way TLS, we must instruct it to require client authentication to ensure clients present a certificate from our CA when they connect.

In your https server, specify the location of your CA root certificate to use for authenticating client certificates.

In this case, we instruct our server to request client certificates, but not to reject unauthorized requests so that we can check for authorization later and provide a friendly message on client authentication failures.

const fs = require('fs'); const https = require('https'); https .createServer( { // ... requestCert: true, rejectUnauthorized: false, ca: fs.readFileSync('ca.crt'), // ... }, (req, res) => { if (!req.client.authorized) { res.writeHead(401); return res.end('Invalid client certificate authentication.'); } res.writeHead(200); res.end('Hello, world!'); } ) .listen(9443);

That's it! Node.js should now be able to receive TLS connections from clients who authenticate themselves using a certificate issued by your trusted CA.

Create a private key and request a certificate for your Node.js client

Request a new certificate from your CA to represent your Node.js client.

$ step ca certificate "myuser" client.crt client.key

Your certificate and private key will be saved in client.crt and client.key respectively.

Make a request from Node.js using mutual TLS

Now, we need only to configure our Node.js client to make authenticated requests using our certificate and private key. The CA root certificate will be used to verify that the client can trust the certificate presented by the server.

Pass your certificate, private key, and root CA certificate to https.request to authenticate your request over TLS.

const fs = require('fs'); const https = require('https'); const req = https.request( { hostname: 'myserver.internal.net', port: 9443, path: '/', method: 'GET', cert: fs.readFileSync('client.crt'), key: fs.readFileSync('client.key'), ca: fs.readFileSync('ca.crt') }, res => { res.on('data', function(data) { // do something with response }); } ); req.end();

Automate certificate renewal

By default, step-ca issues certificates with a 24 hour expiration. Short-lived certificates have many benefits but also require that you renew your certificates each day before they expire. How you renew certificates is often dependent on how you deploy your application. See the step-ca certificate lifecycle management docs for more information.

All documentation content from the Hello mTLS project is licensed under Creative Commons Attribution 4.0 International (CC BY 4.0).

Creative Commons License
Subscribe
Subscribe to updates

Unsubscribe anytime. See our privacy policy.