From ee09ff45e5b2d898e2a882beb803e61eb37d2a41 Mon Sep 17 00:00:00 2001 From: lhauswald <11362723+lhauswald@users.noreply.github.com> Date: Thu, 16 Mar 2023 00:07:33 +0100 Subject: [PATCH] feat(mqtt_cxx): configure client authentication via certificate/key or secure element --- components/esp_mqtt_cxx/esp_mqtt_cxx.cpp | 31 ++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/components/esp_mqtt_cxx/esp_mqtt_cxx.cpp b/components/esp_mqtt_cxx/esp_mqtt_cxx.cpp index cd8c7d2c1..7e6145c5a 100644 --- a/components/esp_mqtt_cxx/esp_mqtt_cxx.cpp +++ b/components/esp_mqtt_cxx/esp_mqtt_cxx.cpp @@ -92,8 +92,35 @@ void config_client_credentials(esp_mqtt_client_config_t &mqtt_client_cfg, Client { mqtt_client_cfg.credentials.authentication.password = password.data.c_str(); }, - [](ClientCertificate const & certificate) {}, - [](SecureElement const & enable_secure_element) {}, + [&mqtt_client_cfg](ClientCertificate const & certificate) + { + std::visit(overloaded{ + [&mqtt_client_cfg](PEM const & pem) + { + mqtt_client_cfg.credentials.authentication.certificate = pem.data; + }, [&mqtt_client_cfg](DER const & der) + { + mqtt_client_cfg.credentials.authentication.certificate = der.data; + mqtt_client_cfg.credentials.authentication.certificate_len = der.len; + }}, certificate.certificate); + std::visit(overloaded{ + [&mqtt_client_cfg](PEM const & pem) + { + mqtt_client_cfg.credentials.authentication.key = pem.data; + }, [&mqtt_client_cfg](DER const & der) + { + mqtt_client_cfg.credentials.authentication.key = der.data; + mqtt_client_cfg.credentials.authentication.key_len = der.len; + }}, certificate.key); + if (certificate.key_password.has_value()) { + mqtt_client_cfg.credentials.authentication.key_password = certificate.key_password.value().data.c_str(); + mqtt_client_cfg.credentials.authentication.key_password_len = static_cast(certificate.key_password.value().data.size()); + } + }, + [&mqtt_client_cfg](SecureElement const & enable_secure_element) + { + mqtt_client_cfg.credentials.authentication.use_secure_element = true; + }, []([[maybe_unused ]]auto & unknown) { static_assert(always_false, "Missing type handler for variant handler");