Friday, April 3, 2020


MQTT installations on aws Ubuntu Instance with SSL/TLS :


sudo apt update
sudo apt install mosquitto mosquitto-clients

After installation ,lets subscribe a topic name called "test" and push hello World message to the topic .

mosquitto_sub -h localhost -t test
mosquitto_pub -h localhost -t test -m "hello world"

You may get this Issues :-

If mqtt not started ,then  provide permission to the log files .

sudo chmod -R  777 /var/log/mosquito
Step 2 — Configuring MQTT Username/Passwords

Let's create an user called admin using command below .

sudo mosquitto_passwd -c /etc/mosquitto/passwd admin

provide your password ,I have given as password .

Now we’ll open up a new configuration file for Mosquitto and tell it to use this password file to require logins for all connections:

sudo nano /etc/mosquitto/conf.d/default.conf
allow_anonymous false
password_file /etc/mosquitto/passwd

sudo systemctl restart mosquito

Try to publish a message without a password:
mosquitto_pub -h localhost -t "test" -m "hello world"
The message should be rejected:
Output
Connection Refused: not authorised.

Let's subscribe a topic using username and password 

mosquitto_sub -h localhost -t test -u "admin" -P "password"

Now publish a message

mosquitto_pub -h localhost -t "test" -m "hello world" -u "admin" -P "password"

You ll able to get the message in the topic name called test .

For SSL/TLS set up :

1. Generate ca.key
 instance-4@instance-4:~/mymosquitto$

                openssl genrsa -des3 -out ca.key 2048

 given passphrase is mythingcenter

common name= ip-10-0-10-239.ec2.internal (System hostname )

2. Generate ca.crt using the ca.key
                openssl req -new -x509 -days 1826 -key ca.key -out ca.crt
               
Fill all the details
-------------------------------------------------------------
                                 You are about to be asked to enter information that will be incorporated
                                into your certificate request.
                                What you are about to enter is what is called a Distinguished Name or a DN.
                                There are quite a few fields but you can leave some blank
                                For some fields there will be a default value,
                                If you enter '.', the field will be left blank.
                                -----
                                Country Name (2 letter code) [AU]:IN
                                State or Province Name (full name) [Some-State]:KAR
                                Locality Name (eg, city) []:BENGALURU
                                Organization Name (eg, company) [Internet Widgits Pty Ltd]:HK
                                Organizational Unit Name (eg, section) []:p
                                Common Name (e.g. server FQDN or YOUR name) []:ip-10-0-*-*.ec2.internal 
                                Email Address []:chchchch
-------------------------------------------------------------

3. List to ensure the certificates are generated

                instance-4@instance-4:~/mymosquitto$ ls
                ca.crt  ca.key

4. Generate the server key
                sudo openssl genrsa -out server.key 2048

5. Generate the server certificate
                sudo openssl req -new -out server.csr -key server.key

6. Now Generate the server certificate using the server key and ca certificate and ca key

                sudo openssl x509 -req -in server.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out server.crt -days 360

7. Copy these certificates to desired locations

  server.crt and server.key to the location /etc/mosquitto/certs and ca.crt to the location /etc/mosquitto/ca_certificates

goto certificate directory and enter the below command for copy 
sudo cp server.* /etc/mosquitto/certs/
sudo cp ca.* /etc/mosquitto/ca_certificates/
              

8. Provide the settings in the configurations file available in the locations

sudo nano  /etc/mosquitto/conf.d/default.conf
               
cafile /etc/mosquitto/ca_certificates/ca.crt
certfile /etc/mosquitto/certs/server.crt
keyfile /etc/mosquitto/certs/server.key


make sure your firewall is allowing both the port ,otherwise type
sudo ufw allow 1883,8883

9. Restart the mosquitto either one of the steps
                sudo systemctl restart mosquitto
               
10. Once done then open the log file
                tailf /var/log/mosquitto/mosquitto.log
If unable to start ,then provide permission to log file .chmod +x *

11. Test the Pub and Sub any one should not show TLS error, but we are getting TLS errors.
Below are the commands of SUB and PUB

Standard with certificate

mosquitto_pub -h ip-10-0-*-*.ec2.internal -t test -m "hello again" -p 8883 --cafile ~/instances/cert/ca.crt  -u "admin" -P "password"
mosquitto_sub -h ip-10-0-*-*.ec2.internal -t test --cafile ~/instances/cert/ca.crt  -u "admin" -P "password" -p 8883

Imp:-
You should provide same domain/hostname while creating certificate .I am giving here

ip-10-0-*-*.ec2.internal which is same as common name while creating certificate .