Skip to content

Add FreeRTOS + wolfIP + wolfMQTT TLS example #492

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions fullstack/freertos-wolfip-wolfmqtt/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
!freertos/utils/posix_utils.c
190 changes: 190 additions & 0 deletions fullstack/freertos-wolfip-wolfmqtt/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
cmake_minimum_required(VERSION 3.13)
project(freertos_sim C)

# Set build type if not specified
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Debug)
endif()

# FreeRTOS source files
set(FREERTOS_DIR "${CMAKE_CURRENT_SOURCE_DIR}/freertos")
set(FREERTOS_KERNEL_DIR "${FREERTOS_DIR}/FreeRTOS-Kernel")

# wolfSSL directories
set(WOLFSSL_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../wolfssl")
set(WOLFIP_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../wolfip")
set(WOLFMQTT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../wolfMQTT")

# Certificate paths
set(WOLFSSL_CERTS_DIR "${WOLFSSL_DIR}/certs")

# Add definitions for certificate paths
add_definitions(
-DMQTT_TLS_CA_CERT="${WOLFSSL_CERTS_DIR}/ca-cert.pem"
-DMQTT_TLS_CLIENT_CERT="${WOLFSSL_CERTS_DIR}/client-cert.pem"
-DMQTT_TLS_CLIENT_KEY="${WOLFSSL_CERTS_DIR}/client-key.pem"
)

# FreeRTOS configuration
add_definitions(
-DPOSIX_PORT
-DPOSIX
-D_POSIX_C_SOURCE=200809L
-D_POSIX_THREAD_SAFE_FUNCTIONS
-DENABLE_MQTT_TLS
-DDEBUG=0
)

# wolfSSL configuration
add_definitions(
-DWOLFSSL_USER_SETTINGS
-DHAVE_TLS_EXTENSIONS
-DHAVE_SUPPORTED_CURVES
-DTFM_TIMING_RESISTANT
-DECC_TIMING_RESISTANT
-DWC_RSA_BLINDING
-DHAVE_AESGCM
-DHAVE_CHACHA
-DHAVE_POLY1305
-DHAVE_ECC
-DHAVE_CURVE25519
-DHAVE_ED25519
-DENABLE_MQTT_TLS
)

# wolfIP definitions
add_definitions(
-DLINK_MTU=1500
-DRXBUF_SIZE=8192
-DTXBUF_SIZE=8192
-DMAX_TCPSOCKETS=8
-DMAX_UDPSOCKETS=8
-DMAX_TIMERS=24
-DWOLFIP_DEBUG
-DWOLFSSL_DEBUG
-DWOLFMQTT_DEBUG
-DETH_TYPE_IP=0x0800
)

# FreeRTOS source files
set(FREERTOS_SRC
${FREERTOS_KERNEL_DIR}/tasks.c
${FREERTOS_KERNEL_DIR}/queue.c
${FREERTOS_KERNEL_DIR}/list.c
${FREERTOS_KERNEL_DIR}/timers.c
${FREERTOS_KERNEL_DIR}/event_groups.c
${FREERTOS_KERNEL_DIR}/stream_buffer.c
${FREERTOS_KERNEL_DIR}/portable/ThirdParty/GCC/Posix/port.c
${FREERTOS_KERNEL_DIR}/portable/MemMang/heap_3.c
)

# Create utils.c if it doesn't exist
if(NOT EXISTS "${FREERTOS_DIR}/utils/utils.c")
file(MAKE_DIRECTORY "${FREERTOS_DIR}/utils")
file(WRITE "${FREERTOS_DIR}/utils/utils.c" "/* utils.c
*
* Copyright (C) 2006-2024 wolfSSL Inc.
*
* This file is part of wolfSSL.
*
* wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* wolfSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

/* Utility functions for FreeRTOS */
void utils_init(void)
{
/* Initialize random seed */
srand(time(NULL));
}
")
endif()

# Add utils.c and events.c to FreeRTOS sources
list(APPEND FREERTOS_SRC
${FREERTOS_DIR}/utils/utils.c
${FREERTOS_DIR}/utils/events.c
)

# wolfIP source files - use stub implementation
set(WOLFIP_SRC
${CMAKE_CURRENT_SOURCE_DIR}/src/wolfip_stub.c
)

# wolfMQTT source files - use stub implementation
set(WOLFMQTT_SRC
${CMAKE_CURRENT_SOURCE_DIR}/src/wolfmqtt_stub.c
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why use the stub instead of the actual client? What is the purpose of the stub?

)

# Application source files
set(APP_SRC
src/main.c
src/wolfip_freertos.c
src/wolfip_utils.c
src/tap_interface.c
${CMAKE_CURRENT_SOURCE_DIR}/freertos/utils/utils.c
)

# Include directories
include_directories(
${CMAKE_CURRENT_SOURCE_DIR}/include
${CMAKE_CURRENT_SOURCE_DIR}/include/FreeRTOSConfig
${CMAKE_CURRENT_SOURCE_DIR}/src
${FREERTOS_KERNEL_DIR}/include
${FREERTOS_KERNEL_DIR}/portable/ThirdParty/GCC/Posix
${WOLFSSL_DIR}
${WOLFIP_DIR}
${WOLFIP_DIR}/src/include
${WOLFMQTT_DIR}
)

# Create static library for wolfIP
add_library(wolfip STATIC ${WOLFIP_SRC})

# Set compiler flags for wolfIP library
target_compile_options(wolfip PRIVATE
-Wall
-Wextra
-g
-fpack-struct
)

# Create executable
add_executable(freertos_sim
${FREERTOS_SRC}
${APP_SRC}
${WOLFMQTT_SRC}
)

# Link libraries
target_link_libraries(freertos_sim
wolfip
wolfssl
pthread
m
rt
)

# Compiler flags
target_compile_options(freertos_sim PRIVATE
-Wall
-Wextra
-g
-fpack-struct
)
125 changes: 125 additions & 0 deletions fullstack/freertos-wolfip-wolfmqtt/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
# FreeRTOS + wolfIP + wolfMQTT TLS Example

This example demonstrates a full-stack embedded MQTT client using FreeRTOS, wolfIP, and wolfMQTT with TLS security. It shows how to establish a secure MQTT connection from an embedded device to a broker using wolfSSL for TLS.

## Software Bill of Materials (SBOM)

| Component | Version | License | Description |
|-----------|---------|---------|-------------|
| FreeRTOS | 10.5.1 | MIT | Real-time operating system for embedded devices |
| wolfSSL | 5.6.3 | GPLv2 | Embedded SSL/TLS library |
| wolfMQTT | 1.16.0 | GPLv2 | Embedded MQTT client library |
| wolfIP | 1.0.0 | GPLv2 | Embedded TCP/IP stack |
| Mosquitto | 2.0.15 | EPL/EDL | MQTT broker for testing |

## Prerequisites

- Linux development environment
- CMake 3.13 or later
- GCC compiler
- Mosquitto MQTT broker
- Root/sudo privileges (for TAP interface setup)

## Setup

1. Clone the required repositories:
```
./setup.sh
```

This script will:
- Clone FreeRTOS kernel
- Clone FreeRTOS repository
- Create necessary directories
- Configure the build environment

2. Configure the TAP interface on your host system:
```
sudo ip tuntap add dev tap0 mode tap
sudo ip addr add 10.10.0.1/24 dev tap0
sudo ip link set dev tap0 up
```

3. Configure Mosquitto broker with TLS:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be less invasive to launch an instance of mosquitto configured to listen on a different port (eg. 118883). That would remove the need to edit a system file and mess with the mosquitto service.

```
sudo nano /etc/mosquitto/mosquitto.conf
```

Add the following configuration:
```
listener 8883
allow_anonymous true
cafile /path/to/wolfssl/certs/ca-cert.pem
certfile /path/to/wolfssl/certs/server-cert.pem
keyfile /path/to/wolfssl/certs/server-key.pem
tls_version tlsv1.3
```

Restart Mosquitto:
```
sudo systemctl restart mosquitto
```

## Building the Example

1. Create a build directory:
```
mkdir build && cd build
```

2. Configure with CMake:
```
cmake ..
```

3. Build the example:
```
make
```

## Running the Example

1. Run the FreeRTOS simulator:
```
./freertos_sim
```

2. The application will:
- Initialize the FreeRTOS kernel
- Configure the TAP interface with IP 10.10.0.10
- Establish a TLS connection to the Mosquitto broker at 10.10.0.1:8883
- Connect to the MQTT broker
- Subscribe to the test topic
- Publish messages to the test topic
- Process incoming messages

3. Monitor the MQTT traffic with Mosquitto client:
```
mosquitto_sub -h 10.10.0.1 -p 8883 -t "test/topic" --cafile /path/to/wolfssl/certs/ca-cert.pem --insecure
```

## Troubleshooting

1. Verify TAP interface is up and configured:
```
ip addr show tap0
```

2. Check Mosquitto broker is running:
```
sudo systemctl status mosquitto
```

3. Verify Mosquitto is listening on the correct port:
```
sudo netstat -tulpn | grep mosquitto
```

4. Run Mosquitto in verbose mode for debugging:
```
mosquitto -c /etc/mosquitto/mosquitto.conf -v
```

## License

This example is licensed under the GPLv2 license. See the LICENSE file for details.
Loading