2121 * Modified by Eric Grammatico <[email protected] > 2222 *
2323 * Added support to secured connexion to mqtt server thanks to WiFiClientSecure class.
24- * Please see comments in code. You can look for WiFiClientSecure, MY_GATEWAY_ESP8266_SECURE,
25- * MY_SSL_CERT, MY_SSL_FINGERPRINT and MY_SSL_CERT_CLIENT in the code below to see what has
26- * changed. No new method, no new class to be used by my_sensors.
24+ * Please see comments in code. You can look for WiFiClientSecure, MY_GATEWAY_ESP8266_SECURE,
25+ * MY_MQTT_CA_CERT, MY_MQTT_FINGERPRINT and MY_MQTT_CLIENT_CERT in the code below to see what has
26+ * changed. No new method, no new class to be used by my_sensors.
2727 *
2828 * The following constants have to be defined from the gateway code:
2929 * MY_GATEWAY_ESP8266_SECURE in place of MY_GATEWAY_ESP8266 to go to secure connexions.
30- * MY_SSL_CERT_AUTHx Up to three root Certificates Authorities could be defined
30+ * MY_MQTT_CA_CERTx Up to three root Certificates Authorities could be defined
3131 * to validate the mqtt server' certificate. The most secure.
32- * MY_SSL_FINGERPRINT Alternatively, the mqtt server' certificate finger print
33- * could be used. Less secure and less convenient as you'll
32+ * MY_MQTT_FINGERPRINT Alternatively, the mqtt server' certificate finger print
33+ * could be used. Less secure and less convenient as you'll
3434 * have to update the fingerprint each time the mqtt server'
3535 * certificate is updated
36- * If neither MY_SSL_CERT_AUTH1 nor MY_SSL_FINGERPRINT are
36+ * If neither MY_MQTT_CA_CERT1 nor MY_MQTT_FINGERPRINT are
3737 * defined, insecure connexion will be established. The mqtt
3838 * server' certificate will not be validated.
39- * MY_SSL_CERT_CLIENT The mqtt server may require client certificate for
40- * MY_SSL_KEY_CLIENT authentication.
39+ * MY_MQTT_CLIENT_CERT The mqtt server may require client certificate for
40+ * MY_MQTT_CLIENT_KEY authentication.
4141 *
4242 */
4343
7070#undef MY_ESP8266_HOSTNAME // cleanup
7171#endif
7272
73+ #ifdef MY_MQTT_CA_CERT
74+ #warning MY_MQTT_CA_CERT is deprecated, please use MY_MQTT_CA_CERT1 instead!
75+ #define MY_MQTT_CA_CERT1 MY_MQTT_CA_CERT
76+ // #undef MY_MQTT_CA_CERT // cleanup
77+ #endif
78+
7379#ifndef MY_MQTT_USER
7480#define MY_MQTT_USER NULL
7581#endif
109115#define EthernetClient WiFiClient
110116#elif defined(MY_GATEWAY_ESP8266_SECURE)
111117#define EthernetClient WiFiClientSecure
112- #if defined(MY_SSL_CERT_AUTH1 )
118+ #if defined(MY_MQTT_CA_CERT1 )
113119BearSSL::X509List certAuth; // List to store Certificat Authorities
114120#endif
115- #if defined(MY_SSL_CERT_CLIENT ) && defined(MY_SSL_KEY_CLIENT )
121+ #if defined(MY_MQTT_CLIENT_CERT ) && defined(MY_MQTT_CLIENT_KEY )
116122BearSSL::X509List clientCert; // Client public key
117123BearSSL::PrivateKey clientPrivKey; // Client private key
118124#endif
119125// Set time via NTP, as required for x.509 validation
120126// BearSSL checks NotBefore and NotAfter dates in certificates
121127// Thus an approximated date/time is needed.
122- void setClock () {
123- configTime (3 * 3600 , 0 , " pool.ntp.org" , " time.nist.gov" );
124-
125- Serial.print (" Waiting for NTP time sync: " );
126- time_t now = time (nullptr );
127- while (now < 8 * 3600 * 2 ) {
128- delay (500 );
129- Serial.print (" ." );
130- now = time (nullptr );
131- }
132- Serial.println (" " );
133- struct tm timeinfo;
134- gmtime_r (&now, &timeinfo);
135- Serial.print (" Current time: " );
136- Serial.print (asctime (&timeinfo));
128+ void setClock ()
129+ {
130+ configTime (3 * 3600 , 0 , " pool.ntp.org" , " time.nist.gov" );
131+
132+ Serial.print (" Waiting for NTP time sync: " );
133+ time_t now = time (nullptr );
134+ while (now < 8 * 3600 * 2 ) {
135+ delay (500 );
136+ Serial.print (" ." );
137+ now = time (nullptr );
138+ }
139+ Serial.println (" " );
140+ struct tm timeinfo;
141+ gmtime_r (&now, &timeinfo);
142+ Serial.print (" Current time: " );
143+ Serial.print (asctime (&timeinfo));
137144}
138145#elif defined(MY_GATEWAY_LINUX)
139146// Nothing to do here
@@ -191,8 +198,8 @@ bool reconnectMQTT(void)
191198 GATEWAY_DEBUG (PSTR (" GWT:RMQ:CONNECTING...\n " ));
192199
193200#if defined(MY_GATEWAY_ESP8266_SECURE)
194- // Date/time are retrieved to be able to validate certificates.
195- setClock ();
201+ // Date/time are retrieved to be able to validate certificates.
202+ setClock ();
196203#endif
197204
198205 // Attempt to connect
@@ -211,10 +218,10 @@ bool reconnectMQTT(void)
211218 delay (1000 );
212219 GATEWAY_DEBUG (PSTR (" !GWT:RMQ:FAIL\n " ));
213220#if defined(MY_GATEWAY_ESP8266_SECURE)
214- char sslErr[256 ];
215- int errID = _MQTT_ethClient.getLastSSLError (sslErr, sizeof (sslErr));
216- GATEWAY_DEBUG (PSTR (" !GWT:RMQ:(%d) %s\n " ), errID, sslErr);
217- #endif
221+ char sslErr[256 ];
222+ int errID = _MQTT_ethClient.getLastSSLError (sslErr, sizeof (sslErr));
223+ GATEWAY_DEBUG (PSTR (" !GWT:RMQ:(%d) %s\n " ), errID, sslErr);
224+ #endif
218225 return false ;
219226}
220227
@@ -320,33 +327,33 @@ bool gatewayTransportInit(void)
320327#endif
321328
322329#if defined(MY_GATEWAY_ESP8266_SECURE)
323- // Certificate Authorities are stored in the X509 list
324- // At least one is needed, but you may need two, or three
325- // eg to validate one certificate from LetsEncrypt two is needed
326- #if defined(MY_SSL_CERT_AUTH1 )
327- certAuth.append (MY_SSL_CERT_AUTH1 );
328- #if defined(MY_SSL_CERT_AUTH2 )
329- certAuth.append (MY_SSL_CERT_AUTH2 );
330+ // Certificate Authorities are stored in the X509 list
331+ // At least one is needed, but you may need two, or three
332+ // eg to validate one certificate from LetsEncrypt two is needed
333+ #if defined(MY_MQTT_CA_CERT1 )
334+ certAuth.append (MY_MQTT_CA_CERT1 );
335+ #if defined(MY_MQTT_CA_CERT2 )
336+ certAuth.append (MY_MQTT_CA_CERT2 );
330337#endif
331- #if defined(MY_SSL_CERT_AUTH3 )
332- certAuth.append (MY_SSL_CERT_AUTH3 );
338+ #if defined(MY_MQTT_CA_CERT3 )
339+ certAuth.append (MY_MQTT_CA_CERT3 );
333340#endif
334- _MQTT_ethClient.setTrustAnchors (&certAuth);
335- #elif defined(MY_SSL_FINGERPRINT ) // MY_SSL_CERT_AUTH1
336- // Alternatively, the certificate could be validated with its
337- // fingerprint, which is less secure
338- _MQTT_ethClient.setFingerprint (MY_SSL_FINGERPRINT );
339- #else // MY_SSL_CERT_AUTH1
340- // At last, an insecure connexion is accepted. Meaning the
341- // server's certificate is not validated.
342- _MQTT_ethClient.setInsecure ();
343- GATEWAY_DEBUG (PSTR (" GWT:TPC:CONNECTING WITH INSECURE SETTING...\n " ));
344- #endif // MY_SSL_CERT_AUTH1
345- #if defined(MY_SSL_CERT_CLIENT ) && defined(MY_SSL_KEY_CLIENT )
346- // The server may required client certificate
347- clientCert.append (MY_SSL_CERT_CLIENT );
348- clientPrivKey.parse (MY_SSL_KEY_CLIENT );
349- _MQTT_ethClient.setClientRSACert (&clientCert, &clientPrivKey);
341+ _MQTT_ethClient.setTrustAnchors (&certAuth);
342+ #elif defined(MY_MQTT_FINGERPRINT ) // MY_MQTT_CA_CERT1
343+ // Alternatively, the certificate could be validated with its
344+ // fingerprint, which is less secure
345+ _MQTT_ethClient.setFingerprint (MY_MQTT_FINGERPRINT );
346+ #else // MY_MQTT_CA_CERT1
347+ // At last, an insecure connexion is accepted. Meaning the
348+ // server's certificate is not validated.
349+ _MQTT_ethClient.setInsecure ();
350+ GATEWAY_DEBUG (PSTR (" GWT:TPC:CONNECTING WITH INSECURE SETTING...\n " ));
351+ #endif // MY_MQTT_CA_CERT1
352+ #if defined(MY_MQTT_CLIENT_CERT ) && defined(MY_MQTT_CLIENT_KEY )
353+ // The server may required client certificate
354+ clientCert.append (MY_MQTT_CLIENT_CERT );
355+ clientPrivKey.parse (MY_MQTT_CLIENT_KEY );
356+ _MQTT_ethClient.setClientRSACert (&clientCert, &clientPrivKey);
350357#endif
351358#endif // MY_GATEWAY_ESP8266_SECURE
352359
0 commit comments