21
21
* Modified by Eric Grammatico <eric@grammatico.me>
22
22
*
23
23
* 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.
27
27
*
28
28
* The following constants have to be defined from the gateway code:
29
29
* 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
31
31
* 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
34
34
* have to update the fingerprint each time the mqtt server'
35
35
* 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
37
37
* defined, insecure connexion will be established. The mqtt
38
38
* 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.
41
41
*
42
42
*/
43
43
70
70
#undef MY_ESP8266_HOSTNAME // cleanup
71
71
#endif
72
72
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
+
73
79
#ifndef MY_MQTT_USER
74
80
#define MY_MQTT_USER NULL
75
81
#endif
109
115
#define EthernetClient WiFiClient
110
116
#elif defined(MY_GATEWAY_ESP8266_SECURE)
111
117
#define EthernetClient WiFiClientSecure
112
- #if defined(MY_SSL_CERT_AUTH1 )
118
+ #if defined(MY_MQTT_CA_CERT1 )
113
119
BearSSL::X509List certAuth; // List to store Certificat Authorities
114
120
#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 )
116
122
BearSSL::X509List clientCert; // Client public key
117
123
BearSSL::PrivateKey clientPrivKey; // Client private key
118
124
#endif
119
125
// Set time via NTP, as required for x.509 validation
120
126
// BearSSL checks NotBefore and NotAfter dates in certificates
121
127
// 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));
137
144
}
138
145
#elif defined(MY_GATEWAY_LINUX)
139
146
// Nothing to do here
@@ -191,8 +198,8 @@ bool reconnectMQTT(void)
191
198
GATEWAY_DEBUG (PSTR (" GWT:RMQ:CONNECTING...\n " ));
192
199
193
200
#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 ();
196
203
#endif
197
204
198
205
// Attempt to connect
@@ -211,10 +218,10 @@ bool reconnectMQTT(void)
211
218
delay (1000 );
212
219
GATEWAY_DEBUG (PSTR (" !GWT:RMQ:FAIL\n " ));
213
220
#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
218
225
return false ;
219
226
}
220
227
@@ -320,33 +327,33 @@ bool gatewayTransportInit(void)
320
327
#endif
321
328
322
329
#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 );
330
337
#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 );
333
340
#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);
350
357
#endif
351
358
#endif // MY_GATEWAY_ESP8266_SECURE
352
359
0 commit comments