Mutual TLS (mTLS)
27 Dec 2020Introduction
TLS has forever played a very large part in securing internet communications. Secure Socket Layer (SSL) filled this space prior to TLS coming to the fore.
In today’s article, I’m going to walk through an exercise of mTLS which is just an extension of TLS.
CA
First of all, we need a certificate authority (CA) that both the client and the server will trust. We generate these using openssl
.
This now puts a private key in ca.key
and a certificate in ca.crt
on our filesystem. We can inspect these a little further with the following.
Looking at the output, we see some interesting things about our CA certificate. Most importantly the X509v3 Basic Constraints
value is set CA:TRUE
, telling us that this certificate can be used to sign other certificates (like CA certificates can).
Server
The server now needs a key and certificate. Key generation is simple, as usual:
We need to create a certificate that has been signed by our CA. This means we need to generate a certificate signing request, which is then used to produce the signed certificate.
This gives us a signing request for the domain of localhost
as mentioned in the -subj
parameter. This signing request now gets used by the CA to generate the certificate.
Inspecting the server certificate, you can see that it’s quite a bit simpler than the CA certificate. We’re only able to use this certificate for the subject that we nominated; localhost
.
Client
The generation of the client certificates is very much the same as the server.
The subject in this case is my-client
.
The -CAcreateserial
number also ensures that we have unique serial numbers between the server and client certificates. Again, this can be verified when you inspect the certificate.
Only the last segment was incremented here. You get the idea though. Unique.
Appliation
Now, we setup a basic node.js server that requires mTLS.
Most important here is that the server’s options specify rejectUnauthorized
as well as requestCert
. This will force the mTLS feedback look back to the client.
A curl request now verifies that the solution is secured by this system of certificates.
The client’s key, certificate, and the ca cert accompany a successful request. A request in any other format simply fails as the authentication requirements have not been met.