Generate SSL certifications for Mosquitto MQTT TLS Security in Windows
This is my Windows batch code to generate all the necessary SSL certificates for Mosquitto MQTT TLS security. This batch code will run unattended and it is recommended that you make your necessary adjustment to the code below.
I wrote this windows batch code based on Mosquitto SSL Configuration -MQTT TLS Security. Please refer to the guide for more information on how to set up MQTT TLS with Mosquitto.
I wrote this windows batch code based on Mosquitto SSL Configuration -MQTT TLS Security. Please refer to the guide for more information on how to set up MQTT TLS with Mosquitto.
-----Code starts-----
@echo off
echo Creating a key pair (ca.key) for Certificate Authority (CA).
openssl genrsa -des3 -passout pass:123456 -out ca.key 2048
echo Creating a CA Certificate (ca.crt) and signing it with ca.key.
openssl req -new -x509 -days 3650 -key ca.key -out ca.crt -passin pass:123456 -subj "/C=US/ST=New York/L=Staten/O=dreamcatcher/OU=RnD/CN=192.168.0.104"
echo Creating a Broker key pair (broker.key).
openssl genrsa -out broker.key 2048
echo Creating a Broker certificate request file (broker.csr) and signing it with broker.key.
openssl req -new -out broker.csr -key broker.key -subj "/C=US/ST=New York/L=Manhattan/O=dreamcatcher/OU=Marketing/CN=192.168.0.104"
echo Signing the Broker certificate with the CA certificate (ca.crt)
openssl x509 -req -in broker.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out broker.crt -days 720 -passin pass:123456
echo Moving all created files to a MQTT_TLS_Certs folder
mkdir MQTT_TLS_Certs
move *.crt MQTT_TLS_Certs
move *.key MQTT_TLS_Certs
move *.csr MQTT_TLS_Certs
move *.srl MQTT_TLS_Certs
echo port 8883 > MQTT_TLS_Certs\broker.conf
echo cafile C:\Program Files\mosquitto\MQTT_TLS_Certs\ca.crt >> MQTT_TLS_Certs\broker.conf
echo keyfile C:\Program Files\mosquitto\MQTT_TLS_Certs\broker.key >> MQTT_TLS_Certs\broker.conf
echo certfile C:\Program Files\mosquitto\MQTT_TLS_Certs\broker.crt >> MQTT_TLS_Certs\broker.conf
echo tls_version tlsv1.2 >> MQTT_TLS_Certs\broker.conf
-----Code Ends-----
To start Mosquitto broker/server on Windows:
mosquitto.exe -c MQTT_TLS_Certs\broker.conf -v
To start Mosquitto subscriber on Windows:
mosquitto_sub.exe -h IPADDRESS -t /SensorData --cafile MQTT_TLS_Certs\ca.crt -p 8883
To use Mosquitto to publish a message to broker:
mosquitto_pub.exe -h IPADDRESS -t /SensorData --cafile MQTT_TLS_Certs\ca.crt -p 8883 -m “hello world”
mosquitto.exe -c MQTT_TLS_Certs\broker.conf -v
To start Mosquitto subscriber on Windows:
mosquitto_sub.exe -h IPADDRESS -t /SensorData --cafile MQTT_TLS_Certs\ca.crt -p 8883
To use Mosquitto to publish a message to broker:
mosquitto_pub.exe -h IPADDRESS -t /SensorData --cafile MQTT_TLS_Certs\ca.crt -p 8883 -m “hello world”
Comments
Post a Comment
Thank you for visiting Almost a Technocrat. Due to many spam comments, your comment will be moderated.