-
Notifications
You must be signed in to change notification settings - Fork 138
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
How to add psk_hint_key for ESP mosquitto broker? (IDFGH-14083) #697
Comments
This is currently not supported. ESP mosquitto port uses ESP-TLS which does support PSK, but only on the client side. Adding this option to server side is very simple, but might take some time and discussion (not the best option in terms of security, but very useful in local setup IMO). To test this, you can apply the attached patch to IDF: --- a/components/mosquitto/examples/broker/main/example_broker.c
+++ b/components/mosquitto/examples/broker/main/example_broker.c
@@ -69,14 +69,15 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_
}
}
+static psk_hint_key_t psk = { .hint = "client1", .key = (const uint8_t*) "key", .key_size = 3 };
+
static void mqtt_app_start(struct mosq_broker_config *config)
{
esp_mqtt_client_config_t mqtt_cfg = {
.broker.address.hostname = "127.0.0.1",
#if CONFIG_EXAMPLE_BROKER_WITH_TLS
.broker.address.transport = MQTT_TRANSPORT_OVER_SSL,
- .broker.verification.certificate = cacert_start,
- .broker.verification.certificate_len = cacert_end - cacert_start,
+ .broker.verification.psk_hint_key = &psk,
#else
.broker.address.transport = MQTT_TRANSPORT_OVER_TCP,
#endif
@@ -103,10 +104,7 @@ void app_main(void)
#if CONFIG_EXAMPLE_BROKER_WITH_TLS
esp_tls_cfg_server_t tls_cfg = {
- .servercert_buf = servercert_start,
- .servercert_bytes = servercert_end - servercert_start,
- .serverkey_buf = serverkey_start,
- .serverkey_bytes = serverkey_end - serverkey_start,
+ .psk_hint_key = &psk
};
config.tls_cfg = &tls_cfg;
#endif |
Hi, thank you very much @david-cermak After applying patches to the IDF and to the mosquitto broker example, everything works. |
Please disable the
You can use this command:
to create a separate project from that example, which you can easily modify. |
My mistake @david-cermak! |
This must work! Let's start with something simple -- one broker on one ESP32, only one client on another ESP32: Server side (from IDF softAP example):
--- a/examples/wifi/getting_started/softAP/main/softap_example_main.c
+++ b/examples/wifi/getting_started/softAP/main/softap_example_main.c
@@ -89,6 +89,8 @@ void wifi_init_softap(void)
EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS, EXAMPLE_ESP_WIFI_CHANNEL);
}
+#include "mosq_broker.h"
+
void app_main(void)
{
//Initialize NVS
@@ -101,4 +103,9 @@ void app_main(void)
ESP_LOGI(TAG, "ESP_WIFI_MODE_AP");
wifi_init_softap();
+
+ static psk_hint_key_t psk = { .hint = "hint", .key = (const uint8_t*) "key", .key_size = 3 };
+ esp_tls_cfg_server_t tls_cfg = { .psk_hint_key = &psk };
+ struct mosq_broker_config config = { .host = "0.0.0.0", .port = 8883, .tls_cfg = &tls_cfg };
+ mosq_broker_run(&config);
} Client side (from IDF mqtt example):
--- a/examples/protocols/mqtt/tcp/main/app_main.c
+++ b/examples/protocols/mqtt/tcp/main/app_main.c
@@ -98,10 +98,14 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_
}
}
+#include "esp_tls.h"
+
static void mqtt_app_start(void)
{
+ static psk_hint_key_t psk = { .hint = "hint", .key = (const uint8_t*) "key", .key_size = 3 };
esp_mqtt_client_config_t mqtt_cfg = {
.broker.address.uri = CONFIG_BROKER_URL,
+ .broker.verification.psk_hint_key = &psk,
};
#if CONFIG_BROKER_URL_FROM_STDIN
char line[128]; |
Thanks @david-cermak! I try this, but Your previous example and patches works in clean example, bud not in my project. On my project works only client, and connection with broker from example (Ethernet and Wi-Fi). |
About resources, yes good to check for memory and number of sockets available on the system. One TLS connection takes about 40k (counts for each endpoint, server(s) and client(s)). But I'd expect a different errors if you're running out of memory. |
Memory is not likely to be an issue, I have 145 KB of free DMA and over 6 MB of SPIRAM. Still, as if you know how to PSK run a Home Assistant 😉 alt that's a whole other topic. |
You could be hitting the socket limit, as adding a broker and a local client would take up 3 sockets, plus if you're having some other external clients which connect and disconnect (esp. not gracefully), the number of sockets used could rise very quickly.
Are you using Home Assistant with ESPHome? AFAIK it uses Arduino by default, but you can optionally use ESP-IDF framework, where the PSK authentication should be available in menuconfig -- at least for the client side, ATM. |
This is possible. I have max (16) LWIP socket, and 12 for HAP HTTPD (HomeKit).
OK, this I don't know ;-)
Never. I made this system from scratch https://luon.eu/en/ I have now started to add Home Assistant connectivity to our system because customers have been asking for it, and in the process I have ‘discovered’ the possibility of communication between our system's exchanges using MQTT and the tops that will be prepared for HA. However, I would also like to give the possibility to use such connections without having to install an additional broker. Therefore, I want to run the broker on one of our network devices. And on top of that, I don't like unencrypted connections even on the local network 😉
I will fight with the socket settings. If it doesn't help, the broker will run on a screen urn that doesn't need to connect to HomeKit. Thanks again for your help. If I manage it I'll let you know what worked in my case. |
How to add psk_hint_key for ESP mosquitto broker for encrypted communication with esp_mqtt client via ssl_psk?
The text was updated successfully, but these errors were encountered: