|
| 1 | +## MQTT with TLS |
| 2 | + |
| 3 | +rscp2mqtt can connect the MQTT broker using TLS. |
| 4 | + |
| 5 | +### Configuration |
| 6 | + |
| 7 | +Add these lines to the .config file and adjust the values according to your environment: |
| 8 | + |
| 9 | +``` |
| 10 | +MQTT_TLS=true |
| 11 | +MQTT_TLS_CAFILE=/home/pi/ca.crt |
| 12 | +MQTT_TLS_CERTFILE=/home/pi/client.crt |
| 13 | +MQTT_TLS_KEYFILE=/home/pi/client.key |
| 14 | +``` |
| 15 | + |
| 16 | +### Certificates and broker configuration |
| 17 | + |
| 18 | +Please follow these commands to create an example environment on your computer with a running Mosquitto broker: |
| 19 | + |
| 20 | +Switch to root |
| 21 | +``` |
| 22 | +sudo -i |
| 23 | +``` |
| 24 | + |
| 25 | +Create server key file and certificate |
| 26 | +``` |
| 27 | +cd /etc/mosquitto/ca_certificates |
| 28 | +
|
| 29 | +openssl genrsa -des3 -out ca.key 2048 |
| 30 | +openssl req -new -x509 -days 1826 -key ca.key -out ca.crt |
| 31 | +
|
| 32 | +cd /etc/mosquitto/certs |
| 33 | +
|
| 34 | +openssl genrsa -out mosquitto.key 2048 |
| 35 | +openssl req -new -out mosquitto.csr -key mosquitto.key |
| 36 | +
|
| 37 | +# Common Name = ip address of the server |
| 38 | +
|
| 39 | +openssl x509 -req -in mosquitto.csr -CA /etc/mosquitto/ca_certificates/ca.crt -CAkey /etc/mosquitto/ca_certificates/ca.key -CAcreateserial -out mosquitto.crt |
| 40 | +``` |
| 41 | + |
| 42 | +Create client key file and certificate |
| 43 | +``` |
| 44 | +cd /etc/mosquitto/certs |
| 45 | +
|
| 46 | +openssl genrsa -out client.key 2048 |
| 47 | +openssl req -new -out client.csr -key client.key |
| 48 | +
|
| 49 | +# Common Name = ip address of the server |
| 50 | +
|
| 51 | +openssl x509 -req -in client.csr -CA /etc/mosquitto/ca_certificates/ca.crt -CAkey /etc/mosquitto/ca_certificates/ca.key -CAcreateserial -out client.crt |
| 52 | +
|
| 53 | +chmod a+r * |
| 54 | +``` |
| 55 | + |
| 56 | +### Broker configuration |
| 57 | +``` |
| 58 | +cd /etc/mosquitto/conf.d |
| 59 | +
|
| 60 | +nano 010-listener-with-tls.conf |
| 61 | +``` |
| 62 | + |
| 63 | +Please insert the following lines into 010-listener-with-tls.conf |
| 64 | +``` |
| 65 | +listener 8883 |
| 66 | +certfile /etc/mosquitto/certs/mosquitto.crt |
| 67 | +keyfile /etc/mosquitto/certs/mosquitto.key |
| 68 | +cafile /etc/mosquitto/ca_certificates/ca.crt |
| 69 | +require_certificate true |
| 70 | +``` |
| 71 | + |
| 72 | +Restart the MQTT broker |
| 73 | +``` |
| 74 | +systemctl restart mosquitto.service |
| 75 | +``` |
| 76 | + |
| 77 | +### Prepare client |
| 78 | + |
| 79 | +Copy client key and certificate |
| 80 | +``` |
| 81 | +cd /home/pi |
| 82 | +sudo mv /etc/mosquitto/certs/client.* . |
| 83 | +sudo chown pi.pi client.* |
| 84 | +cp /etc/mosquitto/ca_certificates/ca.crt . |
| 85 | +``` |
| 86 | + |
| 87 | +Adjust .config to the ip address (according to the certificate definition) and the new port number |
| 88 | +``` |
| 89 | +MQTT_HOST=192.168.178.123 |
| 90 | +MQTT_PORT=8883 |
| 91 | +``` |
| 92 | + |
| 93 | +Start rscp2mqtt |
| 94 | + |
| 95 | +Subscribe to the MQTT broker |
| 96 | +``` |
| 97 | +# use the ip address of the server |
| 98 | +mosquitto_sub -h 192.168.178.123 -p 8883 -t "#" --cafile /home/pi/ca.crt --cert /home/pi/client.crt --key /home/pi/client.key |
| 99 | +``` |
0 commit comments