Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
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.

eBook
$9.99 $35.99
Paperback
$43.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

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 : 9781787285149
Category :
Languages :
Tools :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

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

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.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
$199.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
$279.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 $ 126.97
Hands-On MQTT Programming with Python
$38.99
MQTT Essentials - A Lightweight IoT Protocol
$43.99
Hands-On Internet of Things with MQTT
$43.99
Total $ 126.97 Stars icon
Banner background image

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

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.