<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: sheng chen</title>
    <description>The latest articles on DEV Community by sheng chen (@sheng_chen_5979882122c747).</description>
    <link>https://dev.to/sheng_chen_5979882122c747</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2311357%2F11b0045a-22b6-4247-83e9-c9a2b9e65c10.png</url>
      <title>DEV Community: sheng chen</title>
      <link>https://dev.to/sheng_chen_5979882122c747</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sheng_chen_5979882122c747"/>
    <language>en</language>
    <item>
      <title>Configuring TLS in the Mosquitto MQTT broker</title>
      <dc:creator>sheng chen</dc:creator>
      <pubDate>Wed, 08 Oct 2025 13:44:13 +0000</pubDate>
      <link>https://dev.to/sheng_chen_5979882122c747/configuring-tls-in-the-mosquitto-mqtt-broker-3jnb</link>
      <guid>https://dev.to/sheng_chen_5979882122c747/configuring-tls-in-the-mosquitto-mqtt-broker-3jnb</guid>
      <description>&lt;p&gt;Configuring TLS in the Mosquitto MQTT broker involves setting up secure communication between the broker and clients (or bridges) using OpenSSL or a compatible library like wolfSSL. The configuration is primarily defined in the Mosquitto configuration file (mosquitto.conf) and involves specifying certificates, keys, and TLS options to ensure encrypted connections, server authentication, and optionally client authentication. Below are the details of TLS configuration for the Mosquitto broker, covering key settings, their purposes, and practical considerations.&lt;br&gt;
TLS Configuration in Mosquitto Broker&lt;br&gt;
Key Configuration Options in mosquitto.conf&lt;br&gt;
The following settings in the Mosquitto configuration file control TLS behavior for listeners (ports where the broker accepts connections) and bridges (connections to other brokers). These settings can be applied globally or per listener/bridge.&lt;/p&gt;

&lt;p&gt;Enabling TLS on a Listener:&lt;/p&gt;

&lt;p&gt;Setting: listener  [bind_address]&lt;/p&gt;

&lt;p&gt;Defines a port for the broker to listen on (e.g., listener 8883 for the standard MQTT TLS port).&lt;/p&gt;

&lt;p&gt;TLS Enablement:&lt;/p&gt;

&lt;p&gt;require_certificate: If set to true, clients must provide a valid certificate for mutual authentication. Default is false.&lt;br&gt;
use_identity_as_username: If true, uses the client certificate’s Common Name (CN) as the MQTT username. Useful for authentication without passwords.&lt;br&gt;
use_subject_as_username: If true, uses the full client certificate subject as the username.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;listener 8883
cafile /path/to/ca.crt
certfile /path/to/server.crt
keyfile /path/to/server.key
require_certificate true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Certificate and Key Files:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cafile: Path to the Certificate Authority (CA) certificate file used to verify client certificates (required for require_certificate true).
capath: Directory containing CA certificates (alternative to cafile for multiple CAs).
certfile: Path to the broker’s server certificate (required for TLS).
keyfile: Path to the broker’s private key (required for TLS).
keyform: Format of the private key (pem or engine, default is pem).
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cafile /etc/mosquitto/certs/ca.crt
certfile /etc/mosquitto/certs/server.crt
keyfile /etc/mosquitto/certs/server.key
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;TLS Protocol and Cipher Settings:&lt;/p&gt;

&lt;p&gt;tls_version: Specifies the minimum TLS version (e.g., tlsv1.3, tlsv1.2, tlsv1.1). Default is unset, allowing OpenSSL to negotiate the highest supported version.&lt;br&gt;
ciphers: Specifies allowed cipher suites (e.g., TLS_AES_256_GCM_SHA384). Use OpenSSL cipher list format. Default is unset, using OpenSSL defaults.&lt;br&gt;
ciphers_tls1.3: Specific ciphers for TLS 1.3 (if supported by OpenSSL).&lt;br&gt;
Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tls_version tlsv1.2
ciphers ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Pre-Shared Key (PSK) Support:&lt;/p&gt;

&lt;p&gt;psk_hint: A hint sent to clients for PSK-based authentication (optional).&lt;br&gt;
psk_file: Path to a file containing PSK identities and keys (format: identity:key in hex).&lt;br&gt;
use_identity_as_username: If true, uses the PSK identity as the MQTT username.&lt;br&gt;
Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;listener 8883
psk_hint mybroker
psk_file /etc/mosquitto/psk.txt
use_identity_as_username true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example psk.txt:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;client1:1a2b3c4d5e6f7a8b9c0d1e2f3a4b5c6d
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;CRL and OCSP:&lt;/p&gt;

&lt;p&gt;crlfile: Path to a Certificate Revocation List (CRL) to check for revoked client certificates.&lt;br&gt;
**require_ocsp: If true, enforces OCSP (Online Certificate Status Protocol) validation for client certificates. Default is false.&lt;br&gt;
Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;crlfile /etc/mosquitto/certs/revoked.crl
require_ocsp false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;TLS Engine (Hardware Acceleration):&lt;/p&gt;

&lt;p&gt;tls_engine: Specifies an OpenSSL engine for hardware-based cryptography (e.g., pkcs11 for HSMs).&lt;br&gt;
tls_engine_kpass_sha1: SHA1 hash of the key password for the engine (if required).&lt;br&gt;
Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tls_engine pkcs11
tls_engine_kpass_sha1 1234567890abcdef
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Bridge-Specific TLS Settings:&lt;/p&gt;

&lt;p&gt;When configuring a bridge (Mosquitto connecting as a client to another broker), TLS settings are specified under a bridge_ prefix.&lt;br&gt;
Key Settings:&lt;/p&gt;

&lt;p&gt;bridge_cafile, bridge_capath: CA certificate(s) to verify the remote broker.&lt;br&gt;
bridge_certfile, bridge_keyfile: Client certificate and key for mutual authentication.&lt;br&gt;
bridge_tls_version: TLS version for the bridge connection.&lt;br&gt;
bridge_psk, bridge_identity: PSK and identity for PSK-based authentication.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;connection mybridge
address remotebroker.com:8883
bridge_cafile /etc/mosquitto/certs/remote_ca.crt
bridge_certfile /etc/mosquitto/certs/client.crt
bridge_keyfile /etc/mosquitto/certs/client.key
bridge_tls_version tlsv1.2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;WebSocket TLS:&lt;/p&gt;

&lt;p&gt;For WebSocket listeners (protocol websockets), TLS settings are identical to MQTT listeners but applied to the WebSocket port.&lt;br&gt;
Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;listener 9001
protocol websockets
cafile /etc/mosquitto/certs/ca.crt
certfile /etc/mosquitto/certs/server.crt
keyfile /etc/mosquitto/certs/server.key
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Implementation Details in Mosquitto Broker&lt;br&gt;
The TLS implementation for the broker is primarily handled in net.c and related files, leveraging OpenSSL for cryptographic operations:&lt;/p&gt;

&lt;p&gt;TLS Context Setup: The broker creates an SSL_CTX for each listener with TLS enabled, using settings from mosquitto.conf. It loads certificates and keys via SSL_CTX_use_certificate_file and SSL_CTX_use_PrivateKey_file.&lt;br&gt;
Handshake: For each client connection, an SSL object is created and bound to the socket. The handshake (SSL_accept) verifies client certificates if require_certificate is true.&lt;br&gt;
Data Transfer: Uses SSL_read and SSL_write for encrypted communication, integrated with the broker’s event loop in net.c.&lt;br&gt;
PSK Handling: Implements a PSK callback (SSL_CTX_set_psk_server_callback) to validate client PSKs against the psk_file.&lt;br&gt;
Error Handling: TLS errors are logged using Mosquitto’s logging system, and clients are disconnected on failures (e.g., invalid certificates).&lt;/p&gt;

&lt;p&gt;Practical Considerations&lt;/p&gt;

&lt;p&gt;Certificate Setup:&lt;/p&gt;

&lt;p&gt;Generate certificates using OpenSSL or a CA like Let’s Encrypt. &lt;br&gt;
Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;openssl req -x509 -newkey rsa:2048 -keyout server.key -out server.crt -days 365 -nodes

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ensure file permissions are restrictive (e.g., chmod 600 server.key).&lt;/p&gt;

&lt;p&gt;Security:&lt;/p&gt;

&lt;p&gt;Use tls_version tlsv1.2 or tlsv1.3 to avoid deprecated protocols.&lt;br&gt;
Specify secure ciphers to mitigate vulnerabilities (e.g., disable weak ciphers like CBC-based ones).&lt;br&gt;
Enable require_certificate for high-security environments, but ensure clients are configured with valid certificates.&lt;/p&gt;

&lt;p&gt;Performance:&lt;/p&gt;

&lt;p&gt;TLS handshakes add CPU overhead; consider session resumption for frequent client connections.&lt;br&gt;
Use hardware acceleration (tls_engine) for large-scale deployments.&lt;/p&gt;

&lt;p&gt;Testing:&lt;/p&gt;

&lt;p&gt;Use tools like mosquitto_sub or mosquitto_pub with --cafile, --cert, and --key to test TLS connections.&lt;br&gt;
Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mosquitto_sub -h localhost -p 8883 -t test/topic --cafile ca.crt --cert client.crt --key client.key
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Example Full TLS Configuration&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;conflistener 8883
protocol mqtt
cafile /etc/mosquitto/certs/ca.crt
certfile /etc/mosquitto/certs/server.crt
keyfile /etc/mosquitto/certs/server.key
tls_version tlsv1.2
require_certificate true
use_identity_as_username true

listener 9001
protocol websockets
cafile /etc/mosquitto/certs/ca.crt
certfile /etc/mosquitto/certs/server.crt
keyfile /etc/mosquitto/certs/server.key
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;connection mybridge&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;address remotebroker.com:8883
bridge_cafile /etc/mosquitto/certs/remote_ca.crt
bridge_certfile /etc/mosquitto/certs/client.crt
bridge_keyfile /etc/mosquitto/certs/client.key
Debugging TLS Issues
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Enable verbose logging with log_type all in mosquitto.conf to capture TLS errors.&lt;br&gt;
Check OpenSSL errors in the Mosquitto log (e.g., /var/log/mosquitto/mosquitto.log).&lt;br&gt;
Use openssl s_client to test the broker’s TLS setup:&lt;br&gt;
bashopenssl s_client -connect localhost:8883 -CAfile ca.crt&lt;/p&gt;

&lt;p&gt;Source Reference&lt;br&gt;
These details are based on Mosquitto versions up to 2.0.18, with configuration options documented in man mosquitto.conf and the source code (net.c, security.c). Check the Mosquitto GitHub repository or official documentation for the latest updates.&lt;/p&gt;

</description>
      <category>iot</category>
      <category>networking</category>
      <category>security</category>
    </item>
  </channel>
</rss>
