After having published my post about how to implement a webserver on the esp32 chip, some readers correctly warned me that everyone, once connected to the wifi network, could control the relay and asked me how to limit access to the webpage.
A classic solution (that I also used in this previous tutorial) is to ask the user to enter a password. It’s very easy to implement, but has a negative aspect, that is you have to type the password everytime you need to switch the relay.
Today I’ll show you how to protect your website using a feature of the SSL/TLS protocol: the mutual authentication.
Everytime you connect to a website using the https protocol, the server which hosts the site sends to your browser the SSL certificate of the site. Thanks to this certificate, you can verify the identity of the server and establish a protected connection to it:
The TLS protocol also offers the possibility for the server to require a certificate from the client: this means a two-way authentication, that is server and client authenticates each other using SSL certificates:
With this authentication method, a user has only to install on his device the client certificate (and the corresponding private key) and choose which certificate to send to the server if more than one is present:
The authentication with SSL certificates is therefore based on the principle of something I own (for example, a key) and not of something I know (for example, a password).
Generally speaking, SSL certificates can be:
- signed by a Certification Authority
The first ones are perfect for internal use or for testing, while the second ones, normally signed by trusted CAs, are widely used in production environments or on the Internet. Sometimes you probably received this warning message when surfing on the Internet:
that means that your browser doesn’t trust the certificate sent by the server because it’s self-signed or signed by a non trusted CA.
For this tutorial, I’m going to use OpenSSL as a certification authority to create the certificates we need. If you’re using the toolchain provided by Espressif, you don’t need to install anything else because OpenSSL is included.
First create a folder that will contains all the files required by OpenSSL to operate as a CA in your home directory:
cd mkdir myCA
Now move into that folder and create some empty files:
cd myCA mkdir csr certs crl newcerts private touch index.txt echo 1000 > serial
Copy the certification authority‘s configuration file (openssl.cnf) from my Github repository into the myCA folder. Open the file and change the main path accordingly to your system:
Now you have to generate the private key and the certificate for the CA. Let’s start with the key (its length will be 2048bit, enough to ensure good security):
winpty openssl genrsa -aes256 -out private/ca.key 2048
(all the commands must start with winpty only if you’re using Windows!)
You’re prompted for a password; it’s very important to remember it because you’ll have to type it everytime you use the CA:
Now generate the CA certificate (with a duration of 3650 days, that is 10 years):
winpty openssl req -config openssl.cnf -key private/ca.key -new -x509 -days 3650 -sha256 -extensions ca_cert -out certs/ca.cer
You have to enter some informations… the only mandatory is the common name which is the name of the CA, that will be included in all the certificates the CA will sign. At the end of the process you can open the file (ca.cer) and verify name and duration:
Let’s now generate the server and client certificates. The steps are the same for both:
- generate a new private key – openssl genrsa
- generate the CSR (Certificate Signing Request) file – openssl req
- sign the CSR file with the CA to obtain the final certificate – openssl ca
winpty openssl genrsa -out private/esp-server.key 2048 winpty openssl req -config openssl.cnf -key private/espserver.key -new -sha256 -out csr/espserver.csr winpty openssl ca -config openssl.cnf -extensions server_cert -days 365 -notext -md sha256 -in csr/espserver.csr -out certs/espserver.cer
winpty openssl genrsa -out private/esp-user.key 2048 winpty openssl req -config openssl.cnf -key private/esp-user.key -new -sha256 -out csr/esp-user.csr winpty openssl ca -config openssl.cnf -extensions usr_cert -days 365 -notext -md sha256 -in csr/esp-user.csr -out certs/esp-user.cer
As said before, the client device must own both the certificate and the private key. You can merge certificate and key in a single p12 (or pfx) file with the following command:
winpty openssl pkcs12 -export -out esp-user.pfx -inkey private/esp-user.key -in certs/esp-user.cer
The way you install the P12 file on your device depends on the specific operating system it runs: on Windows you only need to double-click the file and follow the import wizard.
The source code for the program running on the esp32 devboard is available in my Github repository.
First you have to copy in the prioject folder the certificates (CA and server ones) and the private key for the server certificate and include them – as text data – in the program (I’ve already explained how to include binary files in my previous post):
To implement the SSL protocol I’m using the mbedTLS library, included in the esp framework.
First include all the required header files:
#include "mbedtls/platform.h" #include "mbedtls/net.h" #include "mbedtls/ssl.h" #include "mbedtls/entropy.h" #include "mbedtls/ctr_drbg.h" #include "mbedtls/debug.h" #include "mbedtls/error.h"
Before being able to accept SSL connections, you have to define and initialize all the variables you’re going to use:
// mbed TLS variables mbedtls_ssl_config conf; mbedtls_ssl_context ssl; mbedtls_net_context listen_fd, client_fd; mbedtls_entropy_context entropy; mbedtls_ctr_drbg_context ctr_drbg; mbedtls_x509_crt srvcert; mbedtls_x509_crt cachain; mbedtls_pk_context pkey; [...] // initialize mbedTLS components mbedtls_net_init(&listen_fd); mbedtls_net_init(&client_fd); mbedtls_ssl_config_init(&conf); mbedtls_ctr_drbg_init(&ctr_drbg); mbedtls_entropy_init(&entropy); mbedtls_x509_crt_init(&srvcert); mbedtls_x509_crt_init(&cachain); mbedtls_pk_init(&pkey);
// initialize mbedTLS components
Now parse the certificates and the private key and store them in the variables:
mbedtls_x509_crt_parse(&cachain, ca_cer_start, ca_cer_end - ca_cer_start); mbedtls_x509_crt_parse(&srvcert, espserver_cer_start, espserver_cer_end - espserver_cer_start); mbedtls_pk_parse_key(&pkey, espserver_key_start, espserver_key_end - espserver_key_start, NULL, 0);
Tell the library which variables to use for CA chain and own certificate and require the mutual authentication (MBEDTLS_SSL_VERIFY_REQUIRED):
mbedtls_ssl_conf_ca_chain(&conf, &cachain, NULL); mbedtls_ssl_conf_own_cert(&conf, &srvcert, &pkey); mbedtls_ssl_conf_authmode(&conf, MBEDTLS_SSL_VERIFY_REQUIRED);
After having configured other elements of the library (the random number generator and the debug function) you can bind it to the standard TCP port for the https protocol (443) and accept new incoming connections:
mbedtls_net_bind(&listen_fd, NULL, "443", MBEDTLS_NET_PROTO_TCP); mbedtls_net_accept(&listen_fd, &client_fd, NULL, 0, NULL);
After having accepted a connection, you only have to call the function:
during the handshake process the library sends to the client its own certificate and require the one from the client, verifying that this is signed by the specified CA: the mutual authentication is implemented!
After the handshake, the program is similar to the previous example, you only have to use mbedTLS specific send/receive functions (mbedtls_ssl_read e mbedtls_ssl_write).