Skip to content

Commit d589571

Browse files
committed
MySensors code styling applied by GIT
1 parent e009ea8 commit d589571

File tree

5 files changed

+158
-178
lines changed

5 files changed

+158
-178
lines changed

MyConfig.h

+26-23
Original file line numberDiff line numberDiff line change
@@ -1548,11 +1548,11 @@
15481548
//#define MY_MQTT_SUBSCRIBE_TOPIC_PREFIX "mygateway1-in"
15491549

15501550
/**
1551-
* @def MY_SSL_CERT_AUTHx
1551+
* @def MY_MQTT_CA_CERTx
15521552
* @brief Up to three root Certificates Authorities could be defined to validate the mqtt server' certificate. The most secure.
15531553
*
15541554
* This define is mandatory when you need connect MQTT over SSL/TLS. Certificate Authorities.
1555-
* The best method to validate server certificates.
1555+
* The best method to validate server certificates.
15561556
* Advised to retrieve root Certificate Authorities as they expire less often than server certificates.
15571557
* With let's encrypt you may need up to three Certificate Authorities
15581558
*
@@ -1563,7 +1563,7 @@
15631563
* XXX ... XXX
15641564
* ----- FINISH CERTIFICATE -----
15651565
* )EOF";
1566-
*
1566+
*
15671567
* const char cert_isrgrootx2_Authority[] PROGMEM = R"EOF(
15681568
* ----- BEGIN THE CERTIFICATE -----
15691569
* XXX ... XXX
@@ -1576,16 +1576,19 @@
15761576
* ----- FINISH CERTIFICATE -----
15771577
* )EOF";
15781578
*
1579-
* #define MY_SSL_CERT_AUTH1 cert_isrgrootx1_Authority
1580-
* #define MY_SSL_CERT_AUTH2 cert_isrgrootx2_Authority
1581-
* #define MY_SSL_CERT_AUTH3 cert_letsEncryptR3_Authority
1579+
* #define MY_MQTT_CA_CERT1 cert_isrgrootx1_Authority
1580+
* #define MY_MQTT_CA_CERT2 cert_isrgrootx2_Authority
1581+
* #define MY_MQTT_CA_CERT3 cert_letsEncryptR3_Authority
15821582
*
15831583
* @endcode
15841584
*/
1585-
//#define MY_SSL_CERT_AUTH1
1585+
//#define MY_MQTT_CA_CERT1
1586+
//#define MY_MQTT_CA_CERT2
1587+
//#define MY_MQTT_CA_CERT3
1588+
15861589

15871590
/**
1588-
* @def MY_SSL_FINGERPRINT
1591+
* @def MY_MQTT_FINGERPRINT
15891592
* @brief Server certificate validation with its fingerprint
15901593
*
15911594
* The finger print to validate the mqtt server certificate. This is less secure and less convenient
@@ -1596,13 +1599,13 @@
15961599
* awk -F= '{print $2}'
15971600
*
15981601
* Example: @code
1599-
* const char mqtt_fingerprint [] PROGMEM = "CA:CE:2B:ED:D3:32:A7:F1:8C:73:9E:9B:B7:D5:75:4A:10:61:E4:05";
1602+
* const char mqtt_fingerprint [] PROGMEM = "CA:CE:2B:MD:D3:32:A3:F1:8C:73:9E:1B:B7:D5:75:4A:10:61:E4:05";
16001603
* @endcode
16011604
*/
1602-
//#define MY_SSL_FINGERPRINT
1605+
//#define MY_MQTT_FINGERPRINT
16031606

16041607
/**
1605-
* @def MY_SSL_CERT_CLIENT
1608+
* @def MY_MQTT_CLIENT_CERT
16061609
* @brief Set a client certificate to send to a MQTT server that requests one over TLS connection.
16071610
*
16081611
* This define is mandatory when you need connect MQTT over SSL/TLS and client certificate is requested.
@@ -1614,15 +1617,15 @@
16141617
* ----- FINISH CERTIFICATE -----
16151618
* )EOF";
16161619
*
1617-
* #define MY_SSL_CERT_CLIENT mqtt_client_cert
1620+
* #define MY_MQTT_CLIENT_CERT mqtt_client_cert
16181621
*
16191622
* @endcode
16201623
*/
1621-
//#define MY_SSL_CERT_CLIENT
1624+
//#define MY_MQTT_CLIENT_CERT
16221625

16231626
/**
1624-
* @def MY_SSL_KEY_CLIENT
1625-
* @brief Set the client private key generated with the MY_SSL_CERT_CLIENT.
1627+
* @def MY_MQTT_CLIENT_KEY
1628+
* @brief Set the client private key generated with the MY_MQTT_CLIENT_CERT.
16261629
*
16271630
* This define is mandatory when you need connect MQTT over SSL/TLS and client certificate is requested.
16281631
* Example: @code
@@ -1633,11 +1636,11 @@
16331636
* ----- FINISH THE RSA PRIVATE KEY -----
16341637
* )EOF";
16351638
*
1636-
* #define MY_SSL_KEY_CLIENT mqtt_client_key
1639+
* #define MY_MQTT_CLIENT_KEY mqtt_client_key
16371640
*
16381641
* @endcode
16391642
*/
1640-
//#define MY_SSL_KEY_CLIENT
1643+
//#define MY_MQTT_CLIENT_KEY
16411644

16421645
/**
16431646
* @def MY_IP_ADDRESS
@@ -2408,12 +2411,12 @@
24082411
#define MY_MQTT_CLIENT_ID
24092412
#define MY_MQTT_PUBLISH_TOPIC_PREFIX
24102413
#define MY_MQTT_SUBSCRIBE_TOPIC_PREFIX
2411-
#define MY_SSL_CERT_AUTH1
2412-
#define MY_SSL_CERT_AUTH2
2413-
#define MY_SSL_CERT_AUTH3
2414-
#define MY_SSL_FINGERPRINT
2415-
#define MY_SSL_CERT_CLIENT
2416-
#define MY_SSL_KEY_CLIENT
2414+
#define MY_MQTT_CA_CERT1
2415+
#define MY_MQTT_CA_CERT2
2416+
#define MY_MQTT_CA_CERT3
2417+
#define MY_MQTT_FINGERPRINT
2418+
#define MY_MQTT_CLIENT_CERT
2419+
#define MY_MQTT_CLIENT_KEY
24172420
#define MY_SIGNAL_REPORT_ENABLED
24182421
// general
24192422
#define MY_WITH_LEDS_BLINKING_INVERSE

core/MyGatewayTransportMQTTClient.cpp

100755100644
+64-57
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,23 @@
2121
* Modified by Eric Grammatico <eric@grammatico.me>
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

@@ -70,6 +70,12 @@
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
@@ -109,31 +115,32 @@
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)
113119
BearSSL::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)
116122
BearSSL::X509List clientCert; //Client public key
117123
BearSSL::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

Comments
 (0)