MQTT Secure Connection Over TLS
MQTT Secure Connection Over TLS
MQTT Secure Connection Over TLS
MQTT architecture
1
MQTT Stack without security layer
2
Configuration of MQTT broker and MQTT client to use TLS security:
We will be using openssl to create our own Certificate authority (CA), Server keys and
certificates.
The steps covered here will create an encrypted connection between the MQTT broker and
the MQTT clients.
#! /bin/bash
if [ "$#" -ne 1 ]
then
echo "Error: No domain name argument provided"
echo "Usage: Provide a domain name as an argument"
exit 1
fi
DOMAIN="server_com"
# Create root CA & Private key
openssl req -x509 -sha256 -days 356 -nodes -newkey rsa:2048 -subj "/CN=$
{DOMAIN}/C=TN/L=Tunisia" -keyout rootCA.key -out rootCA.crt
# Generate Private key
openssl genrsa -out ${DOMAIN}.key 2048
# Create csf conf
cat > csr.conf <<EOF
[ req ]
default_bits = 2048
prompt = no
default_md = sha256
req_extensions = req_ext
distinguished_name = dn
[ dn ]
C = TN
ST = Tunis
L = Tunisia
O = IAT
3
OU = Dev
CN = ${DOMAIN}
[ req_ext ]
subjectAltName = @alt_names
[ alt_names ]
DNS.1 = ${DOMAIN}
DNS.2 = www.${DOMAIN}
IP.1 = 127.0.0.1
IP.2 = 127.0.0.1
EOF
# create CSR request using private key
openssl req -new -key ${DOMAIN}.key -out ${DOMAIN}.csr -config csr.conf
# Create a external config file for the certificate
cat > cert.conf <<EOF
authorityKeyIdentifier=keyid,issuer
basicConstraints=CA:FALSE
keyUsage = digitalSignature, nonRepudiation, keyEncipherment,
dataEncipherment
subjectAltName = @alt_names
[alt_names]
DNS.1 = ${DOMAIN}
EOF
- chmod +x ssl.sh
3. Execute the script with the domain name or IP. For example,
- ./ssl.sh server_com
4
The script will create all the certificates and keys. The SSL certificate and private keys get named with
the domain name you pass as the script argument. For example, server_com.key and
server_com.crt.
6. Update ca-certificates
- sudo update-ca-certificates
5
2. Add key and certificate paths to broker code and modify the port.
3. Add ca certificate path to client code and change the server address as the same in broker
code.
6
Testing the MQTT secure connection between MQTT Publisher and a
broker over TLS.
MQTT Publisher
Mqtt publisher will publish a topic « hello » containing a message « Mqtt Publisher publishing: Hello
secure C++ world ! »
Server
Wireshark visualization
7
8