# Setting MQTTS to MQTT Broker

## L*ab Objective:*

&#x20;   In this lab, participants will delve into the workings of MQTT communication via the Mosquitto broker. Starting with a foundational understanding of public key cryptography, participants will learn how to generate key pairs and create certificate signing requests. With this knowledge, they will then examine and understand the underlying code responsible for establishing an MQTT connection. The ultimate aim is to equip participants with a comprehensive understanding of secure MQTT communications and the importance of public key infrastructure in ensuring data integrity and security in IoT applications.

<figure><img src="https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2FTJPJJqFKi13o99CoNnMV%2Fimage.png?alt=media&#x26;token=6a74675a-295f-4f88-ab4a-78993823a5a5" alt=""><figcaption><p>Overall of this lab</p></figcaption></figure>

<figure><img src="https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2FP4fwaGKqME3lXkAbaehO%2Fimage.png?alt=media&#x26;token=3bdd1f55-608b-42a6-8953-60f1afd18fca" alt=""><figcaption><p>Important files</p></figcaption></figure>

#### 1. client.key:&#x20;

* Purpose: This is your client's private key.&#x20;
* Usage:&#x20;
  * It's used to generate a Certificate Signing Request (CSR).&#x20;
  * During the TLS handshake between the client and the MQTT broker, this key proves the client's identity and is used for encryption purposes.
* Security: This file should remain private and confidential. Never share it.

#### 2. client.csr:&#x20;

* Purpose: This is the Certificate Signing Request generated using the client.key.&#x20;
* Usage:&#x20;
  * It's essentially a request sent to a Certificate Authority (CA) asking it to issue a certificate for the public key corresponding to client.key.&#x20;
  * It includes details like the client's name, domain, location, and public key.&#x20;
  * The CA will verify the information in the CSR before issuing a certificate.

#### 3. client.crt:&#x20;

* Purpose: This is the client's certificate, issued by a CA.&#x20;
* Usage:&#x20;
  * It's the signed version of the client.csr.&#x20;
  * When the client connects to the MQTT broker, it presents this certificate to prove its identity.&#x20;
  * The broker checks this certificate to ensure it's valid and was issued by a trusted CA.&#x20;
  * The certificate contains the client's public key.

#### **4. mosquitto.org.crt:**&#x20;

* Purpose: It appears to be either the CA certificate or the broker's certificate (since "mosquitto.org" is a known MQTT broker).&#x20;
* Usage:&#x20;
  * If it's the CA certificate, it's used by the client to verify the authenticity of certificates presented by the MQTT broker.&#x20;
  * If it's the broker's certificate, then the broker presents this to the client to prove its identity.

## 🔥 Requirements

<table><thead><tr><th width="429">Resources</th><th>Links</th></tr></thead><tbody><tr><td>Computer</td><td>💻</td></tr><tr><td>ModusToolbox™ software v3.0 or later</td><td><a href="https://www.infineon.com/modustoolbox">Link</a></td></tr><tr><td>CY8CKIT-062S2-43012 Infineon Board</td><td><a href="https://github.com/Advance-Innovation-Centre-AIC/BIIL_MTB-100_Hello_World_and_LED_Blinking_Programming_Template/assets/88732241/0215501d-b774-4045-8e64-ef49e28d8404">Link</a></td></tr><tr><td>Technical Report</td><td><a href="https://www.dropbox.com/scl/fi/amaxc94pte0ut2i1r5ewx/Technical-Report-Lab00.paper?rlkey=b3xm3vrerz9xgv1glb30cvy9z&#x26;dl=0">dropbox</a></td></tr></tbody></table>

## 🚩 Let start

### Create Application

<figure><img src="https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2FbZOa5Cm7CUYI6xc0BwSc%2Fimage.png?alt=media&#x26;token=089601d6-1848-42bb-b547-4f0c0d3aaf70" alt=""><figcaption><p>Create Application</p></figcaption></figure>

### Coding

#### 1. Edit wifi\_config.h

* Use your mobile hotspot as an access point. Edit “wifi\_config.h” to match your mobile SSID setting:
  * \#define WIFI\_SSID “mobile\_ssid”&#x20;
  * \#define WIFI\_PASSWORD “password”

<figure><img src="https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2FSPO8z5KQW5W8YSaOgdrL%2Fimage.png?alt=media&#x26;token=74cefe4a-9553-43b5-9142-f41bd33e795a" alt=""><figcaption></figcaption></figure>

#### 2. Generate Client key

* Run the following commands with Terminal to generate the Client key (Client.key).

```
$ openssl genrsa –out client.key 2048 
```

<figure><img src="https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2FNPopqa0gRN4zERbvglBW%2Fimage.png?alt=media&#x26;token=deb0324a-8409-456c-b9ea-18d8606380cf" alt=""><figcaption><p>You can use run the cmd on Terminal on Eclipse ModusToolbox, or modus-shell</p></figcaption></figure>

<figure><img src="https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2Fw02WJUadPr1axn7r1vJH%2Fimage.png?alt=media&#x26;token=18bc5af9-12e6-48e6-a687-f9acdb7565e3" alt=""><figcaption><p>Now, you got client.key</p></figcaption></figure>

* Generate the Client certificate (client.csr) with the following commands:

```
$ openssl req –out client.csr –key client.key –new
```

* Enter the info required for the certificate signing request

<figure><img src="https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2FawrpyzTb9jVKlOZPHhbB%2Fimage.png?alt=media&#x26;token=1c4990fa-b834-48ee-b165-507956ffbce1" alt=""><figcaption><p>Genereate Client Certificate</p></figcaption></figure>

<figure><img src="https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2FYCJYcnrTFtyNpvo711zd%2Fimage.png?alt=media&#x26;token=66ce6af5-e332-454a-a7cc-66a1f1e7eff1" alt=""><figcaption></figcaption></figure>

#### 3. Open client.csr, copy and paste content to <https://test.mosquitto.org/ssl/>

* Save the generated client certificate into the project root folder

<figure><img src="https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2FepjKf0FrK1IphcC1kFrP%2Fimage.png?alt=media&#x26;token=040083aa-c9ca-4458-883f-a850d9268db4" alt=""><figcaption><p>Save the generated client certificate into the project root folder</p></figcaption></figure>

<figure><img src="https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2F9RqfVY0gztcOojRs45I6%2Fimage.png?alt=media&#x26;token=e0b54e13-2266-4e9c-87e0-a45ac77d9698" alt=""><figcaption><p>you can drag and drop in your project </p></figcaption></figure>

#### 4.Download CA certificate

* Download CA certificate from [http://test.mosquitto.org/ssl/mosquitto.org.crt ](http://test.mosquitto.org/ssl/mosquitto.org.crt)and save it to the project root folder

<figure><img src="https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2FEOfDaYYmmhK07Pvm9A1z%2Fimage.png?alt=media&#x26;token=9c61303f-3c6d-4cd3-899a-697d48d4dd43" alt=""><figcaption></figcaption></figure>

#### 5. Edit file mqtt\_client.h

Edit the following in “configs/mqtt\_client.h”:

* MQTT\_BROKER\_ADDRESS to “test.mosquitto.org”&#x20;
* Set the macros MQTT\_PORT to 8884&#x20;
* MQTT\_SECURE\_CONNECTION to 1&#x20;
* MQTT\_USERNAME to “” MQTT\_PUB\_TOPIC to “unique\_topic”&#x20;
* MQTT\_SUB\_TOPIC to “unique\_topic”&#x20;
* MQTT\_SNI\_HOSTNAME to “test.mosquitto.org”

<figure><img src="https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2FNtktQcxfuaJ2O9VZ4Xr7%2Fimage.png?alt=media&#x26;token=42a2dd8c-999b-4f5d-a297-43e7112c8606" alt=""><figcaption></figcaption></figure>

#### 6. Open the PEMfileToCstring

<figure><img src="https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2FL1tBqSs0ydb8BW4u8HGb%2Fimage.png?alt=media&#x26;token=b5ae5a3e-64e2-4757-827d-ec20643280e9" alt=""><figcaption><p>PEMfileToCstring</p></figcaption></figure>

#### 7. Use PEM to C String Conversion Tool to generate data format and copy to appropriate define in “configs/mqtt\_client\_config.h”&#x20;

* client.crt -> #define CLIENT\_CERTIFICATE&#x20;
* client.key -> #define CLIENT\_PRIVATE\_KEY&#x20;
* mosquitto.org.crt -> #define ROOT\_CA\_CERTIFICATE

<figure><img src="https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2Fdh6tsKoJf3pAOVDZkZpW%2Fimage.png?alt=media&#x26;token=1b8c4337-72b3-4f24-b924-03afc67f2005" alt=""><figcaption><p>Use PEM to C String Conversion Tool to generate data format and copy to appropriate define in “configs/mqtt_client_config.h”</p></figcaption></figure>

#### 8. Save the file

#### 9. Build and Launch the Application

### Open Putty, baud rate 115200

### Result&#x20;

* Press user button
* The GPIO interrupt service routine (ISR) notifies the publisher task.
* The publisher task publishes a message on a topic.
* The MQTT broker sends back the message to the MQTT client because it is also subscribed to the same topic.
* When the message is received, the subscriber task turns the LED ON or OFF. As a result, the user LED toggles every time the user presses the button.

<figure><img src="https://1856353139-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MClo3nC-1US0rbK8Qau%2Fuploads%2FFEbPIE4BF3PzdDYe1LQc%2Fimage.png?alt=media&#x26;token=43d92c8c-aac8-4285-bfd2-436d999ff70f" alt=""><figcaption></figcaption></figure>

> #### 🎉 <mark style="color:blue;">Congratulations! You can now complete this lab</mark>

## Supported toolchains (make variable 'TOOLCHAIN')

* GNU Arm® embedded compiler v10.3.1 (`GCC_ARM`) - Default value of `TOOLCHAIN`
* Arm® compiler v6.16 (`ARM`)
* IAR C/C++ compiler v9.30.1 (`IAR`)

## Supported kits (make variable 'TARGET')

* [PSoC™ 62S2 Wi-Fi Bluetooth® pioneer kit](https://www.infineon.com/CY8CKIT-062S2-43012) (`CY8CKIT-062S2-43012`)
* [PSoC™ 62S1 Wi-Fi Bluetooth® pioneer kit](https://www.infineon.com/CYW9P62S1-43438EVB-01) (`CYW9P62S1-43438EVB-01`)
* [PSoC™ 62S1 Wi-Fi Bluetooth® pioneer kit](https://www.infineon.com/CYW9P62S1-43012EVB-01) (`CYW9P62S1-43012EVB-01`)
* [PSoC™ 62S3 Wi-Fi Bluetooth® prototyping kit](https://www.infineon.com/CY8CPROTO-062S3-4343W) (`CY8CPROTO-062S3-4343W`)

## Related resources

| Resources                       | Links                                                                                                                                                      |
| ------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------- |
| ModusToolbox™ Software Training | [link](https://www.dropbox.com/sh/waj898o4o8eccx0/AAB3hBBaIQo2OvJ5-fubGJIha/training-modustoolbox-level1-getting-started-master/Manual/Ch2-Tools.pdf?dl=0) |

## Other resources

Infineon provides a wealth of data at [www.infineon.com](http://www.infineon.com) to help you select the right device, and quickly and effectively integrate it into your design.

## Document history

<table><thead><tr><th width="149">Version</th><th>Description of change</th></tr></thead><tbody><tr><td>1.0.0</td><td>MQTT via Mosquitto broker</td></tr></tbody></table>

## Authors:

* *Assoc. Prof. Wiroon Sriborrirux*
* *Mr. Sriengchhun Chheang*
* *Mr. Sabol Socare*<br>

<br>

***

© BDH Corporation, 2022-2023
