Skip to content

Commit 2934090

Browse files
authored
Merge pull request #524 from pennam/fix-unor4-up
Fix UNO R4 WiFi username password authentication
2 parents 772c35f + 1587be2 commit 2934090

File tree

5 files changed

+32
-13
lines changed

5 files changed

+32
-13
lines changed

Diff for: src/ArduinoIoTCloudTCP.cpp

+11-9
Original file line numberDiff line numberDiff line change
@@ -84,20 +84,25 @@ int ArduinoIoTCloudTCP::begin(ConnectionHandler & connection, bool const enable_
8484
_connection = &connection;
8585
_brokerAddress = brokerAddress;
8686

87+
ArduinoIoTAuthenticationMode authMode = ArduinoIoTAuthenticationMode::CERTIFICATE;
88+
#if defined (BOARD_HAS_SECRET_KEY)
89+
/* If board supports and sketch is configured for username and password login */
90+
if(_password.length()) {
91+
authMode = ArduinoIoTAuthenticationMode::PASSWORD;
92+
}
93+
#endif
94+
8795
/* Setup broker TLS client */
88-
_brokerClient.begin(connection);
96+
_brokerClient.begin(connection, authMode);
8997

9098
#if OTA_ENABLED
9199
/* Setup OTA TLS client */
92100
_otaClient.begin(connection);
93101
#endif
94102

95-
#if defined (BOARD_HAS_SECRET_KEY)
96-
/* If board is not configured for username and password login */
97-
if(!_password.length())
103+
/* If board is configured for certificate authentication and mTLS */
104+
if(authMode == ArduinoIoTAuthenticationMode::CERTIFICATE)
98105
{
99-
#endif
100-
101106
#if defined(BOARD_HAS_SECURE_ELEMENT)
102107
if (!_selement.begin())
103108
{
@@ -130,14 +135,11 @@ int ArduinoIoTCloudTCP::begin(ConnectionHandler & connection, bool const enable_
130135
#endif
131136
_brokerPort = (brokerPort == DEFAULT_BROKER_PORT_AUTO) ? mqttPort() : brokerPort;
132137
#endif
133-
134-
#if defined(BOARD_HAS_SECRET_KEY)
135138
}
136139
else
137140
{
138141
_brokerPort = (brokerPort == DEFAULT_BROKER_PORT_AUTO) ? DEFAULT_BROKER_PORT_USER_PASS_AUTH : brokerPort;
139142
}
140-
#endif
141143

142144
/* Setup TimeService */
143145
_time_service.begin(_connection);

Diff for: src/ArduinoIoTCloudTCP.h

-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,6 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass
162162
String _dataTopicOut;
163163
String _dataTopicIn;
164164

165-
166165
#if OTA_ENABLED
167166
TLSClientOta _otaClient;
168167
ArduinoCloudOTA _ota;

Diff for: src/tls/utility/TLSClientMqtt.cpp

+12-2
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,29 @@
3333
}
3434
#endif
3535

36-
void TLSClientMqtt::begin(ConnectionHandler & connection) {
36+
37+
void TLSClientMqtt::begin(ConnectionHandler & connection, ArduinoIoTAuthenticationMode authMode) {
3738

3839
#if defined(BOARD_HAS_OFFLOADED_ECCX08)
3940
/* Arduino Root CA is configured in nina-fw
4041
* https://github.com/arduino/nina-fw/blob/master/arduino/libraries/ArduinoBearSSL/src/BearSSLTrustAnchors.h
4142
*/
43+
(void)authMode;
4244
#elif defined(BOARD_HAS_ECCX08)
45+
(void)authMode;
4346
setClient(connection.getClient());
4447
setProfile(aiotc_client_profile_init);
4548
setTrustAnchors(ArduinoIoTCloudTrustAnchor, ArduinoIoTCloudTrustAnchor_NUM);
4649
onGetTime(getTime);
4750
#elif defined(ARDUINO_PORTENTA_C33)
51+
(void)authMode;
4852
setClient(connection.getClient());
4953
setCACert(AIoTSSCert);
5054
#elif defined(ARDUINO_NICLA_VISION)
55+
(void)authMode;
5156
appendCustomCACert(AIoTSSCert);
5257
#elif defined(ARDUINO_EDGE_CONTROL)
58+
(void)authMode;
5359
appendCustomCACert(AIoTUPCert);
5460
#elif defined(ARDUINO_UNOR4_WIFI)
5561
/* Arduino Root CA is configured in uno-r4-wifi-usb-bridge fw >= 0.4.1
@@ -60,10 +66,14 @@ void TLSClientMqtt::begin(ConnectionHandler & connection) {
6066
*/
6167
(void)connection;
6268
/* Temporary force CACert to add new CA without rebuilding firmware */
63-
setCACert(AIoTSSCert);
69+
if (authMode == ArduinoIoTAuthenticationMode::CERTIFICATE) {
70+
setCACert(AIoTSSCert);
71+
}
6472
#elif defined(ARDUINO_ARCH_ESP32)
73+
(void)authMode;
6574
setCACert(AIoTUPCert);
6675
#elif defined(ARDUINO_ARCH_ESP8266)
76+
(void)authMode;
6777
setInsecure();
6878
#endif
6979
}

Diff for: src/tls/utility/TLSClientMqtt.h

+8-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@
1313
#include <Arduino_ConnectionHandler.h>
1414
#include <AIoTC_Config.h>
1515

16+
enum class ArduinoIoTAuthenticationMode
17+
{
18+
PASSWORD,
19+
CERTIFICATE
20+
};
21+
1622
#if defined(BOARD_HAS_OFFLOADED_ECCX08)
1723
/*
1824
* Arduino MKR WiFi1010 - WiFi
@@ -24,6 +30,7 @@
2430
/*
2531
* Arduino MKR GSM 1400
2632
* Arduino MKR NB 1500
33+
* Arduino NANO RP 2040
2734
* Arduino Portenta H7
2835
* Arduino Giga R1
2936
* OPTA
@@ -64,6 +71,6 @@
6471
#endif
6572

6673
public:
67-
void begin(ConnectionHandler & connection);
74+
void begin(ConnectionHandler & connection, ArduinoIoTAuthenticationMode authMode = ArduinoIoTAuthenticationMode::CERTIFICATE);
6875

6976
};

Diff for: src/tls/utility/TLSClientOta.h

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
/*
2525
* Arduino MKR GSM 1400
2626
* Arduino MKR NB 1500
27+
* Arduino NANO RP 2040
2728
* Arduino Portenta H7
2829
* Arduino Giga R1
2930
* OPTA

0 commit comments

Comments
 (0)