Eclipse Mosquitto is an open-source MQTT broker supporting the MQTT protocol 5.0, 3.1.1, and 3.1. It supports authentication, certificates, and is small and easy to configure. It can also be used to create a redundant solution with the following configuration:
This document provides an example of a basic configuration of the Eclipse Mosquitto MQTT broker.
The example shows the configuration on Windows; the paths on Linux OS need to be adjusted (the configuration file itself can be located, for example, in /etc/mosquitto/mosquitto.conf).
#MQTTS listener on port 8883 |
Note: It is also possible to authenticate MQTT clients based on name and password. In this case, the use of identity must be disabled and a password file must be defined:
use_identity_as_username false
password_file c:\Program Files (x86)\mosquitto\pwfile
In the pwfile file, you need to define usernames and passwords using the mosquitto_passwd utility. The first time you use it, you need to use the -c switch to create the file, but this is not necessary for subsequent uses. The -b parameter activates batch mode, which allows you to enter passwords on the command line:
mosquitto_passwd.exe -b -c pwfile myuser1 mypassword1
mosquitto_passwd.exe -b pwfile myuser2 mypassword2
Warning: If MQTT clients are authenticated based on name and password, and certificates are required, there is no defined binding between the user and the certificate. So if somebody were to obtain another user's certificate (and private key), they could use it to connect to the MQTT broker.
The configuration file contains rules for MQTT clients that define which MQTT topics they can read (subscribe to) and write to.
#user without username: anonymous is forbidden, but just to make sure: deny everything |
#user without username: anonymous is forbidden, but just to make sure: deny everything |
To generate keys and TLS certificates and sign them, you must have the OpenSSL package installed. On Windows, you can use, for example, the Win32/Win64 OpenSSL Installation Project (installed in c:\Program Files\OpenSSL-Win64\bin).
The following procedure describes how to create keys and TLS certificates for the MQTT broker, the MQTT client myPLC (PLC or other device), and the MQTT client myD2000 (D2000 KOM process).
This step can be skipped if you already have an existing certification authority, or if someone else signs your certificates. The -days parameter specifies the validity period of the certificate. The -keyout parameter defines the name of the private key file (which must be protected from theft), and the -out parameter defines the name of the certificate (which must be published).
openssl req -new -x509 -days 1000 -extensions v3_ca -keyout caMQTT.key -out caMQTT.crt
When creating a key, you must enter a password to protect it.
The certification authority certificate (caMQTT.crt) must be copied so that the D2000 KOM has access to it (the easiest way is to put it in the application directory) and set the path to it as the "Partner certificate" parameter (#APPDIR#\caMQTT.crt) in the TCP/IP-TCP Redundant line configuration.
The certification authority certificate (caMQTT.crt) must be copied to the MQTT broker so that the MQTT broker can use it to verify the validity of MQTT client certificates.
Note: In the case of redundant MQTT brokers and redundant D2000 application servers (and other MQTT clients), it is necessary to copy the certification authority certificate to all relevant servers!
This step must be repeated for each of the MQTT brokers.
Creating a key (2048 bits):
openssl genrsa -out broker.key 2048
Creating a certificate signing request (csr). Several parameters must be entered; the values given in the example must be modified. It is recommended that the CN (Common Name) match the name of the computer with the MQTT broker.
openssl req -out broker.csr -key broker.key -new
Country Name (2 letter code) [AU]:SK
State or Province Name (full name) [Some-State]:Slovakia
Locality Name (eg, city) []:Žilina
Organization Name (eg, company) [Internet Widgits Pty Ltd]:Ipesoft
Organizational Unit Name (eg, section) []:D2000
Common Name (e.g. server FQDN or YOUR name) []:myserver
Email Address []:ipesoft@ipesoft.sk
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
This step must be repeated for each of the MQTT brokers.
If you created a certification authority above, you are creating a certificate (signing a certificate signing request). Otherwise, you send the broker.csr file for signing to the appropriate certification authority (e.g., the company's IT department).
The -days parameter specifies the certificate validity period in days.
openssl x509 -req -in broker.csr -CA caMQTT.crt -CAkey caMQTT.key -CAcreateserial -out broker.crt -days 1000
The broker.crt file (MQTT broker certificate) must be copied to the MQTT broker, along with the broker.key file (MQTT broker private key). It is also recommended to protect the broker.key file (with access rights, encryption) so that only the user under whom the MQTT broker is running has access to it.
Similar to the MQTT broker, you need to create a key and a certificate signing request for the MQTT client. The procedure is the same, only the file names are different.
Creating a key (with a size of 2048 bits):
openssl genrsa -out myPLC.key 2048
Creating a certificate signing request (csr). It is necessary to enter several parameters, the values given in the example must be modified. It is necessary to enter a CN (Common Name) that corresponds to the MQTT user name:
openssl req -out myPLC.csr -key myPLC.key -new
Country Name (2 letter code) [AU]:SK
State or Province Name (full name) [Some-State]:Slovakia
Locality Name (eg, city) []:Žilina
Organization Name (eg, company) [Internet Widgits Pty Ltd]:Ipesoft
Organizational Unit Name (eg, section) []:D2000
Common Name (e.g. server FQDN or YOUR name) []:myPLC
Email Address []:ipesoft@ipesoft.sk
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
If you created a certification authority above, you are creating a certificate (signing a certificate signing request). Otherwise, you send the myPLC.csr file for signing to the appropriate certification authority (e.g., the company's IT department).
The -days parameter specifies the certificate validity period in days.
openssl x509 -req -inmyPLC.csr -CA caMQTT.crt -CAkey caMQTT.key -CAcreateserial -out myPLC.crt -days 1000
The myPLC.crt file (MQTT client certificate) must be copied to the MQTT client, along with the myPLC.key file (MQTT client private key). It is also recommended to protect the myPLC.key file (with access rights, encryption) so that only the user under whom the MQTT client is running has access to it.
Just as the key for the myPLC MQTT client was created and signed, it is necessary to create and sign the key for the myD2000 MQTT client. Its public certificate (myD2000.crt) and private key (myD2000.key) must be copied so that D2000 KOM can access them (the easiest way is to the application directory) and set the path to them as the parameters "My certificate" (#APPDIR#\myD2000.crt) and "My key" (#APPDIR#\myD2000.key) in the TCP/IP-TCP Redundant configuration.
The configuration of a D2000 line of type TCP/IP-TCP Redundant will look like this:

Note: The private key for the D2000 MQTT client (myD2000.key) can also be protected with a password, which must be entered in the "Pre-shared key" field. For more information, see the TCP/IP-TCP Redundant line description.