Skip to content

Commit 1af4bbe

Browse files
committed
feat(mosq): Added support for TLS transport using ESP-TLS
1 parent 8c4f392 commit 1af4bbe

File tree

15 files changed

+647
-21
lines changed

15 files changed

+647
-21
lines changed

.github/workflows/mosq__build.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ jobs:
6464
- name: Run Test
6565
working-directory: ${{ env.TEST_DIR }}
6666
run: |
67+
python -m pip install pytest-embedded-serial-esp pytest-embedded-idf pytest-rerunfailures pytest-timeout pytest-ignore-test-results
6768
unzip ci/artifacts.zip -d ci
6869
for dir in `ls -d ci/build_*`; do
6970
rm -rf build sdkconfig.defaults

ci/check_copyright_config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ mosquitto_component:
5353
allowed_licenses:
5454
- EPL-2.0
5555
- Apache-2.0
56+
- BSD-3-Clause
5657

5758
slim_modem_examples:
5859
include:

components/mosquitto/CMakeLists.txt

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ set(m_deps_dir ${m_dir}/deps)
77
set(m_srcs
88
${m_lib_dir}/memory_mosq.c
99
${m_lib_dir}/util_mosq.c
10-
${m_lib_dir}/net_mosq.c
1110
${m_lib_dir}/will_mosq.c
1211
${m_lib_dir}/alias_mosq.c
1312
${m_lib_dir}/send_mosq.c
@@ -46,7 +45,6 @@ set(m_srcs
4645
${m_src_dir}/mux.c
4746
${m_src_dir}/mux_epoll.c
4847
${m_src_dir}/mux_poll.c
49-
${m_src_dir}/net.c
5048
${m_src_dir}/password_mosq.c
5149
${m_src_dir}/persist_read.c
5250
${m_src_dir}/persist_read_v234.c
@@ -73,20 +71,26 @@ set(m_srcs
7371
${m_src_dir}/xtreport.c)
7472

7573
idf_component_register(SRCS ${m_srcs}
76-
port/callbacks.c port/config.c port/signals.c port/ifaddrs.c port/broker.c port/files.c
74+
port/callbacks.c
75+
port/config.c
76+
port/signals.c
77+
port/ifaddrs.c
78+
port/broker.c
79+
port/files.c
80+
port/net__esp_tls.c
7781
PRIV_INCLUDE_DIRS port/priv_include port/priv_include/sys ${m_dir} ${m_src_dir}
7882
${m_incl_dir} ${m_lib_dir} ${m_deps_dir}
7983
INCLUDE_DIRS ${m_incl_dir} port/include
80-
PRIV_REQUIRES newlib
84+
PRIV_REQUIRES newlib esp-tls
8185
)
8286

8387
target_compile_definitions(${COMPONENT_LIB} PRIVATE "WITH_BROKER")
8488
target_compile_options(${COMPONENT_LIB} PRIVATE "-Wno-format")
8589

86-
# Some mosquittos source unconditionally define `_GNU_SOURCE` which collides with IDF build system
90+
# Some mosquitto source unconditionally define `_GNU_SOURCE` which collides with IDF build system
8791
# producing warning: "_GNU_SOURCE" redefined
8892
# This workarounds this issue by undefining the macro for the selected files
89-
set(sources_that_define_gnu_source ${m_lib_dir}/net_mosq.c ${m_src_dir}/loop.c ${m_src_dir}/mux_poll.c)
93+
set(sources_that_define_gnu_source ${m_src_dir}/loop.c ${m_src_dir}/mux_poll.c)
9094
foreach(offending_src ${sources_that_define_gnu_source})
9195
set_source_files_properties(${offending_src} PROPERTIES COMPILE_OPTIONS "-U_GNU_SOURCE")
9296
endforeach()

components/mosquitto/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
# ESP32 Mosquitto Port
22

3-
This is a lightweight port of the Mosquitto broker designed to run on the ESP32. It currently supports a single listener and TCP transport only.
3+
This is a lightweight port of the Mosquitto broker designed to run on the ESP32. It currently supports a single listener with TCP transport or TLS transport based on ESP-TLS library.
44

55
## Supported Options
66

7-
The Espressif port supports a limited set of options (with plans to add more in future releases). These options can be configured through a structure passed to the `mosq_broker_start()` function. For detailed information on available configuration options, refer to the [API documentation](api.md).
7+
The Espressif port supports a limited set of options (with plans to add more in future releases). These options can be configured through a structure passed to the `mosq_broker_run()` function. For detailed information on available configuration options, refer to the [API documentation](api.md).
88

99
## API
1010

1111
### Starting the Broker
1212

13-
To start the broker, call the `mosq_broker_start()` function with a properly configured settings structure. The broker operates in the context of the calling task and does not create a separate task.
13+
To start the broker, call the `mosq_broker_run()` function with a properly configured settings structure. The broker operates in the context of the calling task and does not create a separate task.
1414

1515
It's recommended to analyze the stack size needed for the task, but in general, the broker requires at least 4 kB of stack size.
1616

1717
```c
18-
mosq_broker_start(&config);
18+
mosq_broker_run(&config);
1919
```
2020
2121
## Memory Footprint Considerations

components/mosquitto/api.md

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020

2121
| Type | Name |
2222
| ---: | :--- |
23-
| int | [**mosq\_broker\_start**](#function-mosq_broker_start) (struct [**mosq\_broker\_config**](#struct-mosq_broker_config) \*config) <br>_Start mosquitto broker._ |
23+
| int | [**mosq\_broker\_run**](#function-mosq_broker_run) (struct [**mosq\_broker\_config**](#struct-mosq_broker_config) \*config) <br>_Start mosquitto broker._ |
24+
| void | [**mosq\_broker\_stop**](#function-mosq_broker_stop) (void) <br>_Stops running broker._ |
2425

2526

2627
## Structures and Types Documentation
@@ -37,14 +38,16 @@ Variables:
3738

3839
- int port <br>Port number of the broker to listen to
3940

41+
- esp\_tls\_cfg\_server\_t \* tls_cfg <br>ESP-TLS configuration (if TLS transport used) Please refer to the ESP-TLS official documentation for more details on configuring the TLS options. You can open the respective docs with this idf.py command: `idf.py docs -sp api-reference/protocols/esp_tls.html`
42+
4043

4144
## Functions Documentation
4245

43-
### function `mosq_broker_start`
46+
### function `mosq_broker_run`
4447

4548
_Start mosquitto broker._
4649
```c
47-
int mosq_broker_start (
50+
int mosq_broker_run (
4851
struct mosq_broker_config *config
4952
)
5053
```
@@ -63,3 +66,16 @@ This API runs the broker in the calling thread and blocks until the mosquitto ex
6366
**Returns:**
6467

6568
int Exit code (0 on success)
69+
### function `mosq_broker_stop`
70+
71+
_Stops running broker._
72+
```c
73+
void mosq_broker_stop (
74+
void
75+
)
76+
```
77+
78+
79+
**Note:**
80+
81+
After calling this API, function mosq\_broker\_run() unblocks and returns.

components/mosquitto/examples/broker/README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## Overview
44

5-
This example runs a TCP broker on a specified host and port.
5+
This example runs a broker on TLS or TCP transport, specified host and port.
66

77
### How to use this example
88

@@ -13,6 +13,19 @@ If you enabled also the mqtt client, this example will connect to the local brok
1313

1414
You can connect to the ESP32 mosquitto broker using some other client using the ESP32 IPv4 address and the port specified in the project configuration menu.
1515

16+
> [!IMPORTANT]
17+
> The certificates and keys provided in this example are intended for testing purposes only. They are self-signed, single-use, and configured with a common name of "127.0.0.1". Do not reuse these credentials in any production or real-world applications, as they are not secure for such environments.
18+
19+
For more information on setting up TLS configuration (including certificates and keys), please refer to the ESP-TLS documentation:
20+
```bash
21+
idf.py docs -sp api-reference/protocols/esp_tls.html
22+
```
23+
24+
Configuring the TLS option for the broker is quite similar to setting it up for an HTTPS server, as both involve server-side security configuration. Refer to the HTTPS server documentation for details:
25+
```bash
26+
idf.py docs -sp api-reference/protocols/esp_https_server.html
27+
```
28+
1629
### Test version
1730

1831
This example is also used for testing on loopback interface only, disabling any actual connection, just using the local mqtt client to the loopback interface.
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
idf_component_register(SRCS "broker.c"
2-
PRIV_REQUIRES newlib nvs_flash esp_netif esp_event mqtt)
1+
idf_component_register(SRCS "example_broker.c"
2+
PRIV_REQUIRES newlib nvs_flash esp_netif esp_event mqtt
3+
EMBED_TXTFILES servercert.pem serverkey.pem cacert.pem)

components/mosquitto/examples/broker/main/Kconfig.projbuild

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,11 @@ menu "Example Configuration"
1919
If enabled, it runs a local mqtt client connecting
2020
to the same endpoint ans the broker listens to
2121

22+
config EXAMPLE_BROKER_WITH_TLS
23+
bool "Use TLS"
24+
default y
25+
help
26+
If enabled, the broker (and the client too, if enabled)
27+
uses TLS transport layer
28+
2229
endmenu
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
-----BEGIN CERTIFICATE-----
2+
MIIDIzCCAgugAwIBAgIUXichctvCn6/6xXr0+UOBaqwkBMMwDQYJKoZIhvcNAQEL
3+
BQAwITELMAkGA1UEBhMCQ1oxEjAQBgNVBAMMCUVzcHJlc3NpZjAeFw0yNDA3MDgx
4+
NDE5NDNaFw0yNTA3MDgxNDE5NDNaMCExCzAJBgNVBAYTAkNaMRIwEAYDVQQDDAlF
5+
c3ByZXNzaWYwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCqAGUZALUS
6+
AwWkslBH0RJcgyTTYZ6Q3xadG9rubTGN0DNt8INlguElN9eUhj7VzQZeGxRtAk3A
7+
b4r5MpTWAAC8maDgZU97TOmAaxA04h0P2MHTGG4i1vSm2/jebhh5Ydh8nKs9DdAO
8+
YJWfbtt3XukBe5VJcmp7OICz88LFc/fArrAnBFdmrVX+0Y2l/5KDW6ItvcXhorpz
9+
sO5hOnPXIs4Hq5TYOJbUw6h9E8O6bxUG4AXcSWqqbLJ6PzEFSBMBnjwBQn4HCWvM
10+
GV6w2+I1QbtOTe6yNzBa7O3yqzSYeTcdpjv/FFngo4oRN1RMiCYc1Ae3hJiIhDlN
11+
SRB1CHPi4MblAgMBAAGjUzBRMB0GA1UdDgQWBBTdlh8T2ze2K81IrZCpUv9yhZq2
12+
qjAfBgNVHSMEGDAWgBTdlh8T2ze2K81IrZCpUv9yhZq2qjAPBgNVHRMBAf8EBTAD
13+
AQH/MA0GCSqGSIb3DQEBCwUAA4IBAQBDTt9V3LnyBjHDi9pQa+Q8bjVYCMaOSBFE
14+
LJj8GhkXxtfTzqO2u7vkvfz+2MaRDNpL2lePWDB0BwINT+mSYNWjD5bP2mdgJ2nK
15+
BStzWT6MR4hiQ6u6hXy2Q8brqPN+dP4Pay8fXHe3JNadC/nSk4AC3EvVDpghCJJB
16+
1W5az4YmJzK0F6S84AkKnXYdlYyb94RwWSevn7HYZM+xQjoJmBhQ+XnQ7o2uaEur
17+
52igRRHQQ4xrF5JrbGAqfFVqfA8lJDYiAZCG/aNlV0VpgzyxpDxvPFvvlEYJoL63
18+
/asgSIzYoBknZjNZnPSKcsYGa+0Bjjh7tS50bV++5sN+aW/WDRLd
19+
-----END CERTIFICATE-----

components/mosquitto/examples/broker/main/broker.c renamed to components/mosquitto/examples/broker/main/example_broker.c

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@
1414

1515
const static char *TAG = "mqtt_broker";
1616

17+
#if CONFIG_EXAMPLE_BROKER_WITH_TLS
18+
extern const unsigned char servercert_start[] asm("_binary_servercert_pem_start");
19+
extern const unsigned char servercert_end[] asm("_binary_servercert_pem_end");
20+
extern const unsigned char serverkey_start[] asm("_binary_serverkey_pem_start");
21+
extern const unsigned char serverkey_end[] asm("_binary_serverkey_pem_end");
22+
extern const char cacert_start[] asm("_binary_cacert_pem_start");
23+
extern const char cacert_end[] asm("_binary_cacert_pem_end");
24+
#endif
25+
26+
1727
#if CONFIG_EXAMPLE_BROKER_RUN_LOCAL_MQTT_CLIENT
1828
static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data)
1929
{
@@ -63,7 +73,13 @@ static void mqtt_app_start(struct mosq_broker_config *config)
6373
{
6474
esp_mqtt_client_config_t mqtt_cfg = {
6575
.broker.address.hostname = "127.0.0.1",
66-
.broker.address.transport = MQTT_TRANSPORT_OVER_TCP, // we support only TCP transport now
76+
#if CONFIG_EXAMPLE_BROKER_WITH_TLS
77+
.broker.address.transport = MQTT_TRANSPORT_OVER_SSL,
78+
.broker.verification.certificate = cacert_start,
79+
.broker.verification.certificate_len = cacert_end - cacert_start,
80+
#else
81+
.broker.address.transport = MQTT_TRANSPORT_OVER_TCP,
82+
#endif
6783
.broker.address.port = config->port,
6884
};
6985
esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg);
@@ -79,11 +95,22 @@ void app_main(void)
7995
ESP_ERROR_CHECK(esp_event_loop_create_default());
8096
ESP_ERROR_CHECK(example_connect());
8197

82-
struct mosq_broker_config config = { .host = CONFIG_EXAMPLE_BROKER_HOST, .port = CONFIG_EXAMPLE_BROKER_PORT };
98+
struct mosq_broker_config config = { .host = CONFIG_EXAMPLE_BROKER_HOST, .port = CONFIG_EXAMPLE_BROKER_PORT, .tls_cfg = NULL };
8399

84100
#if CONFIG_EXAMPLE_BROKER_RUN_LOCAL_MQTT_CLIENT
85101
mqtt_app_start(&config);
86102
#endif
103+
104+
#if CONFIG_EXAMPLE_BROKER_WITH_TLS
105+
esp_tls_cfg_server_t tls_cfg = {
106+
.servercert_buf = servercert_start,
107+
.servercert_bytes = servercert_end - servercert_start,
108+
.serverkey_buf = serverkey_start,
109+
.serverkey_bytes = serverkey_end - serverkey_start,
110+
};
111+
config.tls_cfg = &tls_cfg;
112+
#endif
113+
87114
// broker continues to run in this task
88-
mosq_broker_start(&config);
115+
mosq_broker_run(&config);
89116
}

0 commit comments

Comments
 (0)