@@ -190,6 +190,11 @@ bool reconnectMQTT(void)
190190{
191191 GATEWAY_DEBUG (PSTR (" GWT:RMQ:CONNECTING...\n " ));
192192
193+ #if defined(MY_GATEWAY_ESP8266_SECURE)
194+ // Date/time are retrieved to be able to validate certificates.
195+ setClock ();
196+ #endif
197+
193198 // Attempt to connect
194199 if (_MQTT_client.connect (MY_MQTT_CLIENT_ID, MY_MQTT_USER, MY_MQTT_PASSWORD)) {
195200 GATEWAY_DEBUG (PSTR (" GWT:RMQ:OK\n " ));
@@ -223,39 +228,6 @@ bool gatewayTransportConnect(void)
223228 }
224229 GATEWAY_DEBUG (PSTR (" GWT:TPC:IP=%s\n " ), WiFi.localIP ().toString ().c_str ());
225230
226- #if defined(MY_GATEWAY_ESP8266_SECURE)
227- // Certificate Authorities are stored in the X509 list
228- // At least one is needed, but you may need two, or three
229- // eg to validate one certificate from LetsEncrypt two is needed
230- #if defined(MY_SSL_CERT_AUTH1)
231- certAuth.append (MY_SSL_CERT_AUTH1);
232- #if defined(MY_SSL_CERT_AUTH2)
233- certAuth.append (MY_SSL_CERT_AUTH2);
234- #endif
235- #if defined(MY_SSL_CERT_AUTH3)
236- certAuth.append (MY_SSL_CERT_AUTH3);
237- #endif
238- _MQTT_ethClient.setTrustAnchors (&certAuth);
239- #elif defined(MY_SSL_FINGERPRINT) // MY_SSL_CERT_AUTH1
240- // Alternatively, the certificate could be validated with its
241- // fingerprint, which is less secure
242- _MQTT_ethClient.setFingerprint (MY_SSL_FINGERPRINT);
243- #else // MY_SSL_CERT_AUTH1
244- // At last, an insecure connexion is accepted. Meaning the
245- // server's certificate is not validated.
246- _MQTT_ethClient.setInsecure ();
247- GATEWAY_DEBUG (PSTR (" GWT:TPC:CONNECTING WITH INSECURE SETTING...\n " ));
248- #endif // MY_SSL_CERT_AUTH1
249- #if defined(MY_SSL_CERT_CLIENT) && defined(MY_SSL_KEY_CLIENT)
250- // The server may required client certificate
251- clientCert.append (MY_SSL_CERT_CLIENT);
252- clientPrivKey.parse (MY_SSL_KEY_CLIENT);
253- _MQTT_ethClient.setClientRSACert (&clientCert, &clientPrivKey);
254- #endif
255- // Once the secure connexion settings are done, date/time are retrieved
256- // to be able to validate certificates.
257- setClock ();
258- #endif // MY_GATEWAY_ESP8266_SECURE
259231#elif defined(MY_GATEWAY_LINUX)
260232#if defined(MY_IP_ADDRESS)
261233 _MQTT_ethClient.bind (_MQTT_clientIp);
@@ -347,6 +319,37 @@ bool gatewayTransportInit(void)
347319 (void )WiFi.begin (MY_WIFI_SSID, MY_WIFI_PASSWORD, 0 , MY_WIFI_BSSID);
348320#endif
349321
322+ #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+ #endif
331+ #if defined(MY_SSL_CERT_AUTH3)
332+ certAuth.append (MY_SSL_CERT_AUTH3);
333+ #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);
350+ #endif
351+ #endif // MY_GATEWAY_ESP8266_SECURE
352+
350353 gatewayTransportConnect ();
351354
352355 _MQTT_connecting = false ;
0 commit comments