Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
MQTT Essentials - A Lightweight IoT Protocol
MQTT Essentials - A Lightweight IoT Protocol

MQTT Essentials - A Lightweight IoT Protocol: Send and receive messages with the MQTT protocol for your IoT solutions.

Arrow left icon
Profile Icon Gaston C. Hillar
Arrow right icon
€18.99 per month
Full star icon Full star icon Full star icon Full star icon Half star icon 4.1 (7 Ratings)
Paperback Apr 2017 280 pages 1st Edition
eBook
€17.99 €26.99
Paperback
€32.99
Subscription
Free Trial
Renews at €18.99p/m
Arrow left icon
Profile Icon Gaston C. Hillar
Arrow right icon
€18.99 per month
Full star icon Full star icon Full star icon Full star icon Half star icon 4.1 (7 Ratings)
Paperback Apr 2017 280 pages 1st Edition
eBook
€17.99 €26.99
Paperback
€32.99
Subscription
Free Trial
Renews at €18.99p/m
eBook
€17.99 €26.99
Paperback
€32.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
Table of content icon View table of contents Preview book icon Preview Book

MQTT Essentials - A Lightweight IoT Protocol

Chapter 2. Securing an MQTT Mosquitto Server

In this chapter, we will secure a Mosquitto server. We will work with digital certificates to encrypt all the data sent between the MQTT clients and the server. To secure the server, we will:

  • Generate a private certificate authority to use TLS with Mosquitto
  • Create a certificate for the Mosquitto server
  • Configure TLS transport security in Mosquitto
  • Test the MQTT TLS configuration with MQTT.fx
  • Test the MQTT TLS configuration with MQTT-spy
  • Create a certificate for each MQTT client
  • Configure TLS client certificate authentication in Mosquitto
  • Test the MQTT TLS client authentication with MQTT.fx
  • Test the MQTT TLS configuration with MQTT-spy
  • Force the TLS protocol version

Securing a Mosquitto server

Security for IoT, mobile, and web applications is an extremely important topic that deserves entire books dedicated to it. Each solution has its own security requirements, and it is very important to consider all of them when developing each component of the solution.

If we use MQTT to publish values that are neither confidential nor critical for other applications, our only concern may be to keep control over the maximum number of subscribers to topics to make sure that the messages are always available. This way, we can avoid the failure of MQTT in delivering messages to a huge number of subscribers.

However, most of the times, we won't be working on a solution that can share the data with the entire world without limitations and doesn't need to care about data confidentiality and integrity in addition to data availability. Imagine that we are working on a solution that includes a mobile app that allows users to control a huge drone. If the drone...

Generating a private certificate authority to use TLS with Mosquitto

So far, we have been working with a Mosquitto server with its default configuration that listens on port 1883 and uses TCP as the transport protocol. The data sent between each MQTT client and server isn't encrypted. There are no restrictions to subscribers or publishers. If we open the firewall ports and redirect the ports in the router, any MQTT client that has our IP can publish to any topic and can subscribe to any topic.

In our examples in the previous chapter, we didn't make any changes in our configurations to allow incoming connections to port 1883, and therefore, we didn't open our Mosquitto server to the Internet.

We want to use TLS with MQTT and Mosquitto. This way, we will make sure that we can trust the MQTT server because we have confidence that it is who it says; our data will be private because it will be encrypted, and it will have integrity because it won't be altered in the middle...

Creating a certificate for the Mosquitto server

Now that we have a private certificate authority, we can create the certificate for the Mosquitto server, that is, a certificate for the computer that will run the MQTT server.

First, we must generate a new private key that will be different from the private key we generated for our own private certificate authority.

Go to the Terminal in macOS or Linux, or the Command Prompt in Windows. Run the following command to create a 2,048-bit key and save it in the server.key file:

openssl genrsa -out server.key 2048

The following lines show a sample output generated by the previous command:

Generating RSA private key, 2048 bit long modulus
....................................................................................................................................................................+++
......+++
e is 65537 (0x10001)

The previous command will generate the private key in the server.key file. Go back to the Terminal in macOS or Linux...

Configuring TLS transport security in Mosquitto

Now, we will configure Mosquitto to use TLS transport security and work with encrypted communications with different clients. Note that we haven't generated certificates for the clients, and therefore, we won't use client certificates for authentication. This way, any client that has the ca.crt file will be able to establish a communication with the Mosquitto server.

Go to the Mosquitto installation directory and create a new subdirectory named certificates. In Windows, you will need administrator privileges to access the default installation folder.

Copy the following files from the certificates directory, in which we have saved the certificate authority certificate and the server certificate, to the certificates subdirectory we recently created within the Mosquitto installation directory:

  • ca.crt
  • server.crt
  • server.key

In case you are running the Mosquitto server in a Terminal window in macOS or Linux, press Ctrl + C to stop it. In Windows...

Securing a Mosquitto server


Security for IoT, mobile, and web applications is an extremely important topic that deserves entire books dedicated to it. Each solution has its own security requirements, and it is very important to consider all of them when developing each component of the solution.

If we use MQTT to publish values that are neither confidential nor critical for other applications, our only concern may be to keep control over the maximum number of subscribers to topics to make sure that the messages are always available. This way, we can avoid the failure of MQTT in delivering messages to a huge number of subscribers.

However, most of the times, we won't be working on a solution that can share the data with the entire world without limitations and doesn't need to care about data confidentiality and integrity in addition to data availability. Imagine that we are working on a solution that includes a mobile app that allows users to control a huge drone. If the drone flies the wrong...

Generating a private certificate authority to use TLS with Mosquitto


So far, we have been working with a Mosquitto server with its default configuration that listens on port 1883 and uses TCP as the transport protocol. The data sent between each MQTT client and server isn't encrypted. There are no restrictions to subscribers or publishers. If we open the firewall ports and redirect the ports in the router, any MQTT client that has our IP can publish to any topic and can subscribe to any topic.

In our examples in the previous chapter, we didn't make any changes in our configurations to allow incoming connections to port 1883, and therefore, we didn't open our Mosquitto server to the Internet.

We want to use TLS with MQTT and Mosquitto. This way, we will make sure that we can trust the MQTT server because we have confidence that it is who it says; our data will be private because it will be encrypted, and it will have integrity because it won't be altered in the middle of the road. In case you...

Creating a certificate for the Mosquitto server


Now that we have a private certificate authority, we can create the certificate for the Mosquitto server, that is, a certificate for the computer that will run the MQTT server.

First, we must generate a new private key that will be different from the private key we generated for our own private certificate authority.

Go to the Terminal in macOS or Linux, or the Command Prompt in Windows. Run the following command to create a 2,048-bit key and save it in the server.key file:

openssl genrsa -out server.key 2048

The following lines show a sample output generated by the previous command:

Generating RSA private key, 2048 bit long modulus
....................................................................................................................................................................+++
......+++
e is 65537 (0x10001)

The previous command will generate the private key in the server.key file. Go back to the Terminal in macOS or Linux...

Configuring TLS transport security in Mosquitto


Now, we will configure Mosquitto to use TLS transport security and work with encrypted communications with different clients. Note that we haven't generated certificates for the clients, and therefore, we won't use client certificates for authentication. This way, any client that has the ca.crt file will be able to establish a communication with the Mosquitto server.

Go to the Mosquitto installation directory and create a new subdirectory named certificates. In Windows, you will need administrator privileges to access the default installation folder.

Copy the following files from the certificates directory, in which we have saved the certificate authority certificate and the server certificate, to the certificates subdirectory we recently created within the Mosquitto installation directory:

  • ca.crt

  • server.crt

  • server.key

In case you are running the Mosquitto server in a Terminal window in macOS or Linux, press Ctrl + C to stop it. In Windows...

Testing the MQTT TLS configuration with MQTT.fx


Now, we will use the MQTT.fx GUI utility to generate another MQTT client that uses an encrypted connection to publish messages to the same topic, sensors/drone01/altitude. We have to make changes to the connection options to enable TLS and specify the certificate authority certificate file. Follow the next steps:

  1. Launch MQTT.fx, select local mosquitto in the dropdown located on the upper-left corner, and click on the configuration icon on the right-hand side of this dropdown and on the left-hand side of the Connect button. MQTT.fx will display the Edit Connection Profiles dialog box with different options for the connection profile named local mosquitto.

  2. Change the value specified in Broker Port from 1883 to 8883 .

  3. Press the SSL/TLS button.

  4. Activate the Enable SSL/TLS checkbox.

  5. Activate the CA certificate file radio button.

  6. Enter or select the full path to the ca.crt file that you created in the certificates folder in the CA Certificate File textbox...

Testing the MQTT TLS configuration with MQTT-spy


There is another very popular utility to generate MQTT clients that can subscribe to topics and publish to topics: MQTT-spy. This utility is open source and can run on any computer that has Java 8 or a higher version installed on it. You can find more information about MQTT-spy at http://kamilfb.github.io/mqtt-spy. The options to establish a connection with an MQTT server with a certificate authority certificate file are similar to the ones we analyzed for MQTT.fx. However, in case you also want to work with this utility, it is convenient to analyze them in detail.

Now, we will use the MQTT-spy GUI utility to generate another MQTT client that uses an encrypted connection to publish messages to the same topic, that is, sensors/drone01/altitude. Follow the next steps:

  1. Launch MQTT-spy.

  2. Select Connections | New connection. The Connection list dialog box will appear.

  3. Click on the Connectivity tab and select MQTT 3.1.1 in the Protocol version dropdown...

Creating a certificate for each MQTT client


Now, we want to require each MQTT client to provide a valid certificate to establish a connection with the MQTT server. This way, only the clients that have a valid certificate will be able to publish or subscribe to topics. We will use the previously created private certificate authority to create client certificates for authentication.

We will generate a sample certificate for our local computer that will act as a client. We can follow the same procedure to generate additional certificates for additional devices that we want to be able to connect to the Mosquitto server. We just need to use a different name for the file and use a different device name in the corresponding option.

Tip

We must use the same certificate authority certificate that we used to generate the server certificate to generate the client certificates.

First, we must generate a new private key that will be different from the private keys we generated for our own private certificate...

Configuring TLS client certificate authentication in Mosquitto


Now, we will configure Mosquitto to use TLS client certificate authentication. This way, any client will require the ca.crt file and a client certificate to establish a communication with the Mosquitto server.

In case you are running the Mosquitto server in a Terminal window in macOS or Linux, press CtrlC to stop it. In Windows, stop the appropriate service.

Go to the Mosquitto installation directory and open the mosquitto.conf configuration file.

In macOS, Linux. or Windows, add the following lines at the end of the configuration file:

require_certificate true 

We specified the true value for the require_certificate option to make Mosquitto require a valid client certificate for any client that requests a connection to Mosquitto.

Save the changes to the mosquitto.conf configuration file and launch Mosquitto again. We will use the mosquitto_sub command-line utility included in Mosquitto to generate a simple MQTT client that subscribes...

Testing the MQTT TLS client authentication with MQTT.fx


Now, we will use the MQTT.fx GUI utility to generate another MQTT client that uses an encrypted connection and TLS client authentication to publish messages to a topic that matches the topic filter we used for the subscription, sensors/drone25/altitude. We have to make changes to the connection options we used when we enabled TLS and specify the client certificate and client key files. Follow the next steps:

  1. Launch MQTT.fx, select local mosquitto in the dropdown located on the upper-left corner, and click on the configuration icon on the right-hand side of this dropdown and on the left-hand side of the Connect button. MQTT.fx will display the Edit Connection Profiles dialog box with different options for the connection profile named local mosquitto.

  2. Make sure the value specified in Broker Port is 8883 .

  3. Press the SSL/TLS button.

  4. Make sure the Enable SSL/TLS checkbox is activated.

  5. Activate the Self signed certificates radio button.

  6. Enter...

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Make your connected devices less prone to attackers by understanding practical security mechanisms
  • Dive deep into one of IoT’s extremely lightweight machines to enable connectivity protocol with some real-world examples
  • Learn to take advantage of the features included in MQTT for IoT and Machine-to-Machine communications with complete real-life examples

Description

This step-by-step guide will help you gain a deep understanding of the lightweight MQTT protocol. We’ll begin with the specific vocabulary of MQTT and its working modes, followed by installing a Mosquitto MQTT broker. Then, you will use best practices to secure the MQTT Mosquitto broker to ensure that only authorized clients are able to publish and receive messages. Once you have secured the broker with the appropriate configuration, you will develop a solution that controls a drone with Python. Further on, you will use Python on a Raspberry Pi 3 board to process commands and Python on Intel Boards (Joule, Edison and Galileo). You will then connect to the MQTT broker, subscribe to topics, send messages, and receive messages in Python. You will also develop a solution that interacts with sensors in Java by working with MQTT messages. Moving forward, you will work with an asynchronous API with callbacks to make the sensors interact with MQTT messages. Following the same process, you will develop an iOS app with Swift 3, build a website that uses WebSockets to connect to the MQTT broker, and control home automation devices with HTML5, JavaScript code, Node.js and MQTT messages

Who is this book for?

This book is a great resource for developers who want to learn more about the MQTT protocol to apply it to their individual IoT projects. Prior knowledge of working with IoT devices is essential.

What you will learn

  • Understand how MQTTv3.1 and v3.1.1 works in detail
  • Install and secure a Mosquitto MQTT broker by following best practices
  • Design and develop IoT solutions combined with mobile and web apps that use MQTT messages to communicate
  • Explore the features included in MQTT for IoT and Machine-to-Machine communications
  • Publish and receive MQTT messages with Python, Java, Swift, JavaScript, and Node.js
  • Implement the security best practices while setting up the MQTT Mosquitto broker

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Apr 14, 2017
Length: 280 pages
Edition : 1st
Language : English
ISBN-13 : 9781787287815
Category :
Languages :
Tools :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

Publication date : Apr 14, 2017
Length: 280 pages
Edition : 1st
Language : English
ISBN-13 : 9781787287815
Category :
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 95.97
Hands-On MQTT Programming with Python
€29.99
MQTT Essentials - A Lightweight IoT Protocol
€32.99
Hands-On Internet of Things with MQTT
€32.99
Total 95.97 Stars icon

Table of Contents

7 Chapters
1. Lightweight Messaging with MQTT 3.1.1 and Mosquitto Chevron down icon Chevron up icon
2. Securing an MQTT Mosquitto Server Chevron down icon Chevron up icon
3. Controlling a Drone with Python Chevron down icon Chevron up icon
4. Interacting with Sensors in Java Chevron down icon Chevron up icon
5. Controlling Home Automation in JavaScript Chevron down icon Chevron up icon
6. Interacting with Actuators in Swift Chevron down icon Chevron up icon
A. Exersise Answers Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Full star icon Half star icon 4.1
(7 Ratings)
5 star 57.1%
4 star 28.6%
3 star 0%
2 star 0%
1 star 14.3%
Filter icon Filter
Top Reviews

Filter reviews by




Brett Oct 25, 2023
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Subscriber review Packt
Grim Sleepless Oct 26, 2017
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Great book on MQTT. I only had issue replicating the TLS part!
Amazon Verified review Amazon
Alexander Pabel Apr 08, 2021
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Good book which helped me to understand Mqtt protocol and use it in applications of a device which communicates with another device's application via a broker
Amazon Verified review Amazon
Steven C. Ollmann Apr 02, 2019
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I purchased to learn about and create working samples for MQTT client. Tested against a sample MQTT server/broker and existing system.I was disappointed that when applying to Google IoT CORE, that Google implements differently. Without this book I would have had a bumpier road to travel. Glad I made this purchase! I learned a lot about MQTT implementations.
Amazon Verified review Amazon
Nitish Nov 04, 2017
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
Good simple language and explanation is good and easy to understand. Overall a good and useful book. Thanks.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.