Skip to main content
Techlogia — AI and Web Development Berlin

Practise this module for free on a real VM

Sign up for free
 All courses
Free to read – no sign-up

Protect a website with client certificates

You build your own certificate authority with openssl, issue a server and a client certificate and let nginx admit only visitors with a valid certificate (mTLS). Complements the TLS and Let's Encrypt modules.

Duration: 75 minLevel: IntermediateExercises: 7

Your own CA and certificates

With normal HTTPS only the server proves its identity. With mTLS (mutual TLS) the client must also present a certificate – useful for admin interfaces, server-to-server APIs or devices in a company network.

For that you need your own certificate authority (CA). Everything lives in ~/pki. openssl creates the private keys (*.key) with mode 600 – only you can read them.

Exercises

  1. 1. Create your own certificate authority (CA)

    Create the folder ~/pki and generate a CA in it: private key ca.key and self-signed certificate ca.crt with the name (CN) Lab-CA.

    mkdir -p ~/pki && cd ~/pki
    openssl req -x509 -newkey rsa:2048 -noenc -days 365 \
      -keyout ca.key -out ca.crt -subj "/CN=Lab-CA"

    Look at the result: openssl x509 -in ca.crt -noout -text | less

  2. 2. Issue a server certificate for localhost

    Generate a key and a certificate signing request (CSR) for localhost for nginx and have your CA sign it:

    cd ~/pki
    openssl req -newkey rsa:2048 -noenc -keyout server.key -out server.csr \
      -subj "/CN=localhost" -addext "subjectAltName=DNS:localhost"
    openssl x509 -req -in server.csr -CA ca.crt -CAkey ca.key -CAcreateserial \
      -days 365 -copy_extensions copy -out server.crt
  3. 3. Issue a client certificate for yourself

    Now you get a certificate: key client.key, a request with your name as CN (e.g. /CN=schueler01), signed by your CA as client.crt.

    Then check: openssl verify -CAfile ca.crt client.crt

nginx requires a client certificate

Now nginx gets your server certificate and the CA. Everything runs locally on your VM: you test with curl against https://localhost/.

Exercises

  1. 1. nginx with your server certificate

    Copy ca.crt, server.crt and server.key to /etc/nginx/mtls/ and create a site /etc/nginx/sites-available/mtls that answers on port 443 with your server certificate. Enable it, test and reload nginx:

    sudo install -d -m 755 /etc/nginx/mtls
    sudo cp ~/pki/ca.crt ~/pki/server.crt /etc/nginx/mtls/
    sudo install -m 600 ~/pki/server.key /etc/nginx/mtls/server.key
    sudo ln -s /etc/nginx/sites-available/mtls /etc/nginx/____/mtls
    sudo nginx ____ && sudo systemctl reload nginx

    Site skeleton:

    server {
        listen ____ ssl;
        server_name localhost;
        ssl_certificate     /etc/nginx/mtls/____;
        ssl_certificate_key /etc/nginx/mtls/server.key;
    }

    Test: curl --cacert ~/pki/ca.crt https://localhost/ – without -k, curl now knows your CA.

  2. 2. nginx requires a client certificate

    Add two lines to the server block: nginx should only let in clients whose certificate was signed by your CA.

    ssl_client_certificate /etc/nginx/mtls/____;
    ssl_verify_client ____;

    Then sudo nginx -t && sudo systemctl reload nginx. A request without a certificate must now fail:

    curl --cacert ~/pki/ca.crt https://localhost/

    nginx answers with 400 “No required SSL certificate was sent”.

  3. 3. Get in with a certificate: 200

    Let nginx show who it recognised. Replace the location block:

    location / {
        default_type text/plain;
        return 200 "Hallo ____\n";
    }

    Reload and request with your certificate:

    curl --cacert ~/pki/ca.crt --cert ~/pki/client.crt --key ~/pki/client.key https://localhost/

    Expected: Hallo CN=schueler01 (with your CN).

  4. 4. Counter-check: a foreign certificate

    A certificate only counts if your CA signed it. Create a self-signed certificate ~/pki/fremd.crt with key ~/pki/fremd.key (like the CA in task 1, but with a different CN) and call curl with it (--cert fremd.crt --key fremd.key). nginx must refuse: 400, “The SSL certificate error”.

Knowledge check: client certificates

Six short questions without a VM – works on your phone too.

Related courses

Now practice it yourself

Reading is good – doing is better. Practise this module on a real Linux VM, right in your browser. A free account is all it takes.

Start for free

Lab content under CC BY 4.0 – free to use with attribution (© TechLogia).

How do you like this page?