MQTT Broker (Mosquitto) with server certificate (Let's Encrypt)

Translation of the original in Spanish. Read it in: CatalàCastellano

Part 4 of 5Certificates, PKI and MQTT over TLS
  1. Certificate management (PKI) - easy-rsa
  2. MQTT Broker (Mosquitto) with server certificate (self-signed)
  3. MQTT Broker (Mosquitto) with server certificate (self-signed) and client certificate
  4. MQTT Broker (Mosquitto) with server certificate (Let's Encrypt)
  5. Certificate formats: PEM, DER and PKCS#12 and private key handling

Introduction

In this post we present a proof-of-concept for configuring an MQTT broker using Mosquitto, in which TLS is implemented thanks to Let's Encrypt certificates. This method brings several advantages over traditional solutions:

  • No public exposure required: The server does not have to be exposed to the Internet; only the domain name must resolve correctly in DNS, which minimises the attack surface.

  • Ease of use: With Certbot and automated tools (such as Just), the certificate issuance and renewal process is simplified.

  • Cost-effective: Let's Encrypt offers free certificates, reducing operating costs.

This approach is ideal for private, development or proof-of-concept environments, where a balance between security and convenience is wanted.

Video demonstration

MQTT Broker (Mosquitto) con certificado servidor (Let’s Encrypt) · Original on YouTube ↗

Requirements

  • Domain name: A valid domain (for example, mqtt.example.com).

  • Certbot: Tool for obtaining Let's Encrypt certificates.

  • Just: A command runner that simplifies running tasks (check the Justfile to see the available commands).

  • Mosquitto: The MQTT broker software.

Configuration

Obtaining certificates with Certbot

Let's Encrypt allows obtaining free, automated TLS certificates. Keep in mind that, unlike other implementations, Let's Encrypt does not support client certificates, so only the server certificate will be generated.

There are two common methods for validating domain ownership:

  • HTTP-01 Challenge: Requires having a running web server.

  • DNS-01 Challenge: Consists of adding a TXT record in DNS, ideal if you do not have a public IP or a web server.

Important: Make sure the distinguished name (DN) of the certificate matches exactly the hostname that clients will use to connect and that it resolves correctly in DNS.

Installation

To start, create a virtual environment and install Certbot:

uv venv
uv pip install certbot

Requesting the certificate

Use the following syntax to request the certificate:

just certbot certonly -m <tu-email> --preferred-challenges dns-01 --manual -d <dominio1>,<dominio2>,...

For example:

just certbot certonly -m oriol@joor.net --preferred-challenges dns-01 --manual -d mqtt.joor.net,pki.joor.net

If the request is successful, the certificates will be located in the path:

certbot/config/live/<dominio>

Inside this directory you will find:

  • privkey.pem: Your private key.

  • fullchain.pem: The complete certificate chain, which is the one used in most server configurations.

  • chain.pem: Intermediate certificates (useful, for example, for OCSP stapling in Nginx >=1.3.7).

  • cert.pem: A certificate that can cause conflicts in some scenarios, so using fullchain.pem is generally recommended.

For Mosquitto, only the fullchain.pem and privkey.pem files will be required.

Running the MQTT broker

The Mosquitto configuration is already prepared to reference the certificates issued by Let's Encrypt. To launch the service, run:

just mqtt

This command starts Mosquitto using the mosquitto.conf configuration file, which contains the correct paths to the certificates.

Certificate details

  • Private key (privkey.pem): Guarantees the secure identity of the server.

  • Complete chain (fullchain.pem): Includes both the server certificate and the necessary intermediate certificates.

Since clients usually trust the Let's Encrypt CAs included in their operating systems, there is no need to provide an additional CA certificate on the broker.

Certificate renewal

Let's Encrypt certificates are valid for 90 days, so renewing them periodically is essential to maintain security. To renew the certificates, you can use:

just certbot renew
# o alternativamente:
just certbot_renew

After renewal, if the paths in the Mosquitto configuration file remain the same, the broker will automatically use the new certificate when it restarts or when the configuration is reloaded.

Notes

  • This configuration is a proof of concept. For production environments, consider fully automating the renewal process and reviewing other additional security measures.

  • Make sure to update the DNS records so that domain validation is done without problems.

  • Modify the configuration files only if you understand the associated security implications.

References