How to setup a mqtt communication over websocket with TLS/SSL using mosquitto
井民全, Jing, mqjing@gmail.com
This document shows you how to setup a mqtt communication over websocket with TLS/SSL using mosquitto. In order to test the mqtt with secure https TLS/SSL over websocket.
The broker certification, server.crt, I use self-singed. The CA that i created for my self. The webserver, I use the KOA that using the same certification file server.crt for https.
Here are the following procedures:
Create a CA key-pair for self-signed testing
Output: 3 files: ca.crt and self-signed server.key and server.crt
Install a mosquitto server for the mqtt broker
Configuration: ca.crt, server.key and server.crt
Output: a mqtt broker over websocket with TLS/SSL
Install a KOA javascript web server for the javascript container that provide secure https TLS/SSL with the self-signed certification server.key and the server.crt
Import the ca.crt to the Chrome browser for trusting the Customized CA.
Testing procedures for ensure:
paho utilities: mosquitto_pub and mosquitto_sub are used to test the broker
Python code: are used to demo and test the mqtt communication with the broker.
Html + Javascript code: are used to simulate a mqtt client that running in a browser to communicate with the broker over websocket with secure https TLS/SSL. Here, I use Eclipse Paho Javascript Client library.
Table of Contents
1.1. Step 1: Install/Setup the mosquitto server 2
1.2. Step 2: Setup the TLS security 3
1.3. Step 3: Control the broker services 3
2. Enable TLS over websocket 5
3. Detail for Setup the TLS security 6
3.1. Setup a self-signed CA key-pair and the certification 6
3.2. Create key-pair for broker server 6
3.3. Create server certification using the "ca.key" 7
3.4. Edit the configure file 8
5.1.2. MQTT Client (with TLS) 9
5.3. Python Client (TLS + WebSockets) 12
5.4. Javascript Client (TLS + WebSocket) 15
5.4.2. (Only for self-signed case) Add the customized CA cert to the Chrome browser 15
5.4.3. Setup a web server with https enabled 17
6.1.1. Message: error 18 at 0 depth lookup:self signed certificate (ref) 23
1. Quick
1.1. Step 1: Install/Setup the mosquitto server
1.2. Step 2: Setup the TLS security
1.3. Step 3: Control the broker services
1.4. Step 4: Test
Download the Windows binary
https://mosquitto.org/download/
# On the other machines
1.4.1. Command Line Test
1.4.2. Python Test
pip install paho-mqtt
1.5. Trobule shotting
2. Enable TLS over websocket
Step 1: vi the mosquitto.conf
Enable the websockets
File: /etc/mosquitto/conf.d/mosquitto.conf
Step 2: restart the broker
sudo service mosquitto restart
Step 3: Testing
Subscriber (source)
Publish (source)
3. Detail for Setup the TLS security
3.1. Setup a self-signed CA key-pair and the certification
Step 1: Create a key pair for the self-signed CA
Command
openssl genrsa -des3 -out ca.key 2048
Step 2: Create a the ca.cert using the ca.key
Command
openssl req -new -x509 -days 1826 -key ca.key -out ca.crt
Common Name: CA-DOMAIN NAME (if you want to test self-signed, leave empty)
3.2. Create key-pair for broker server
Step 1: Create the server.key
openssl genrsa -out server.key 2048
Step 2: Create the server.csr
openssl req -new -out server.csr -key server.key
Common Name: using your broker server's domain name
3.3. Create server certification using the "ca.key"
Step 1: Create server.crt using self-signed CA using the ca.key
openssl x509 -req -in server.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out server.crt -days 360
Verify the server.crt
Command
openssl verify -CAfile ca.crt server.crt
Result
Step 2: Copy the certifications and the server key
3.4. Edit the configure file
vi mosquitto.conf
4. Start the Broker
sudo service mosquitto restart
// Debug
sudo cat /var/log/mosquitto/mosquitto.log
5. Testing
5.1. Command Line
5.1.1. MQTT Client (Normal)
5.1.2. MQTT Client (with TLS)
5.2. Python Client (TLS)
5.2.1. Requirement
pip install paho-mqtt
5.2.2. Publish
File: publish_tls.py
5.2.3. Subcriber
File: subscriber_tls.py
Result
python subscribe_tls.py
python publish_tls.py
5.3. Python Client (TLS + WebSockets)
5.3.1. Requirement
pip install paho-mqtt
5.3.2. Publish
5.3.3. Subcriber
5.4. Javascript Client (TLS + WebSocket)
5.4.1. The file structure
5.4.2. (Only for self-signed case) Add the customized CA cert to the Chrome browser
Note: If you miss this step, you will got the the error of WebSocket connection to 'wss://xxx.xxx.xxx.xxx:8081/mqtt' failed.
Step 1: [Setting] - [Privacy and security] -> [Security]
Step 2: Check [Manage certificates]
Step 3: import your customized CA certification file: the ca.crt
For Windows version
[Personal] -> [Import...]
For Linux version
[Authorities] -> [Import...]
5.4.3. Setup a web server with https enabled
Step 1: Install KOA
Step 2: Create start js code
Note: the key and certification using the same as broker. (if you use self-signed). If you using different server.cer, you will got the error of WebSocket connection to 'wss://xxx.xxx.xxx.xxx:8081/mqtt' failed.
File: ./webserver.js
Step 3: start the web server
node webserver.js
5.4.4. Html
File:./public/index.html
5.4.5. Javascript
File: ./public/index.js
Result
6. Trouble-shooting
6.1. Error messages
6.1.1. Message: error 18 at 0 depth lookup:self signed certificate (ref)
Solution
6.1.2. Message: ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain
Solution
6.1.3. Message: mqttws31.min.js:36 WebSocket connection to 'wss://xxx.xxx.xxx.xxx:8081/mqtt' failed: Error in connection establishment: net::ERR_CERT_AUTHORITY_INVALID
Solution
7. References
Mosquitto Official Site, https://mosquitto.org/
Test your mqtt client, https://test.mosquitto.org/