Skip to content

Commit 869d6d2

Browse files
authored
Added support for MQTT over SSL/TLS (#1387)
* Added support for MQTT over SSL/TLS * Added documentation on new parameters
1 parent 636815b commit 869d6d2

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

MyConfig.h

100644100755
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1485,6 +1485,63 @@
14851485
*/
14861486
//#define MY_MQTT_SUBSCRIBE_TOPIC_PREFIX "mygateway1-in"
14871487

1488+
/**
1489+
* @def MY_MQTT_CA_CERT
1490+
* @brief Set a specific CA certificate needed to validate MQTT server against. Use the certificate as a trust anchor, accepting remote certificates signed by it.
1491+
*
1492+
* This define is mandatory when you need connect MQTT over SSL/TLS.
1493+
* Example: @code
1494+
*
1495+
* const char mqtt_ca_cert[] PROGMEM = R"EOF(
1496+
* ----- BEGIN THE CERTIFICATE -----
1497+
* XXX ... XXX
1498+
* ----- FINISH CERTIFICATE -----
1499+
* )EOF";
1500+
*
1501+
* #define MY_MQTT_CA_CERT mqtt_ca_cert
1502+
*
1503+
* @endcode
1504+
*/
1505+
//#define MY_MQTT_CA_CERT
1506+
1507+
/**
1508+
* @def MY_MQTT_CLIENT_CERT
1509+
* @brief Set a client certificate to send to a MQTT server that requests one over TLS connection.
1510+
*
1511+
* This define is mandatory when you need connect MQTT over SSL/TLS.
1512+
* Example: @code
1513+
*
1514+
* const char mqtt_client_cert[] PROGMEM = R"EOF(
1515+
* ----- BEGIN THE CERTIFICATE -----
1516+
* XXX ... XXX
1517+
* ----- FINISH CERTIFICATE -----
1518+
* )EOF";
1519+
*
1520+
* #define MY_MQTT_CLIENT_CERT mqtt_client_cert
1521+
*
1522+
* @endcode
1523+
*/
1524+
//#define MY_MQTT_CLIENT_CERT
1525+
1526+
/**
1527+
* @def MY_MQTT_CLIENT_KEY
1528+
* @brief Set a client private key to send to a MQTT server that requests one over TLS connection.
1529+
*
1530+
* This define is mandatory when you need connect MQTT over SSL/TLS.
1531+
* Example: @code
1532+
*
1533+
* const char mqtt_client_key[] PROGMEM = R"EOF(
1534+
* ----- START THE RSA PRIVATE KEY -----
1535+
* XXX ... XXX
1536+
* ----- FINISH THE RSA PRIVATE KEY -----
1537+
* )EOF";
1538+
*
1539+
* #define MY_MQTT_CLIENT_KEY mqtt_client_key
1540+
*
1541+
* @endcode
1542+
*/
1543+
//#define MY_MQTT_CLIENT_KEY
1544+
14881545
/**
14891546
* @def MY_IP_ADDRESS
14901547
* @brief Static ip address of gateway. If not defined, DHCP will be used.
@@ -2262,6 +2319,9 @@
22622319
#define MY_MQTT_CLIENT_ID
22632320
#define MY_MQTT_PUBLISH_TOPIC_PREFIX
22642321
#define MY_MQTT_SUBSCRIBE_TOPIC_PREFIX
2322+
#define MY_MQTT_CA_CERT
2323+
#define MY_MQTT_CLIENT_CERT
2324+
#define MY_MQTT_CLIENT_KEY
22652325
#define MY_SIGNAL_REPORT_ENABLED
22662326
// general
22672327
#define MY_WITH_LEDS_BLINKING_INVERSE

core/MyGatewayTransportMQTTClient.cpp

100644100755
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,14 @@
8383
#endif /* End of MY_IP_ADDRESS */
8484

8585
#if defined(MY_GATEWAY_ESP8266) || defined(MY_GATEWAY_ESP32)
86+
#if defined(MY_MQTT_CA_CERT) && defined(MY_MQTT_CLIENT_CERT) && defined(MY_MQTT_CLIENT_KEY)
87+
#define EthernetClient WiFiClientSecure
88+
BearSSL::X509List ca_cert(MY_MQTT_CA_CERT);
89+
BearSSL::X509List client_cert(MY_MQTT_CLIENT_CERT);
90+
BearSSL::PrivateKey client_key(MY_MQTT_CLIENT_KEY);
91+
#else
8692
#define EthernetClient WiFiClient
93+
#endif /* End of MY_MQTT_CA_CERT && MY_MQTT_CLIENT_CERT && MY_MQTT_CLIENT_KEY */
8794
#elif defined(MY_GATEWAY_LINUX)
8895
// Nothing to do here
8996
#else
@@ -138,6 +145,7 @@ void incomingMQTT(char *topic, uint8_t *payload, unsigned int length)
138145
bool reconnectMQTT(void)
139146
{
140147
GATEWAY_DEBUG(PSTR("GWT:RMQ:CONNECTING...\n"));
148+
141149
// Attempt to connect
142150
if (_MQTT_client.connect(MY_MQTT_CLIENT_ID, MY_MQTT_USER, MY_MQTT_PASSWORD)) {
143151
GATEWAY_DEBUG(PSTR("GWT:RMQ:OK\n"));
@@ -261,6 +269,11 @@ bool gatewayTransportInit(void)
261269
(void)WiFi.begin(MY_WIFI_SSID, MY_WIFI_PASSWORD, 0, MY_WIFI_BSSID);
262270
#endif
263271

272+
#if defined(MY_MQTT_CA_CERT) && defined(MY_MQTT_CLIENT_CERT) && defined(MY_MQTT_CLIENT_KEY)
273+
_MQTT_ethClient.setTrustAnchors(&ca_cert);
274+
_MQTT_ethClient.setClientRSACert(&client_cert, &client_key);
275+
#endif /* End of MY_MQTT_CA_CERT && MY_MQTT_CLIENT_CERT && MY_MQTT_CLIENT_KEY */
276+
264277
gatewayTransportConnect();
265278

266279
_MQTT_connecting = false;

keywords.txt

100644100755
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,10 @@ MY_GATEWAY_SERIAL LITERAL1
248248
MY_GATEWAY_W5100 LITERAL1
249249
MY_HOSTNAME LITERAL1
250250
MY_INCLUSION_BUTTON_EXTERNAL_PULLUP LITERAL1
251+
MY_MQTT_CA_CERT LITERAL1
252+
MY_MQTT_CLIENT_CERT LITERAL1
251253
MY_MQTT_CLIENT_ID LITERAL1
254+
MY_MQTT_CLIENT_KEY LITERAL1
252255
MY_MQTT_CLIENT_PUBLISH_RETAIN LITERAL1
253256
MY_MQTT_PASSWORD LITERAL1
254257
MY_MQTT_PUBLISH_TOPIC_PREFIX LITERAL1

0 commit comments

Comments
 (0)