Skip to content

Commit 240f27b

Browse files
committed
Arduino UNO R4 WiFi: allow both username/password and mTLS authentication
1 parent 4907eba commit 240f27b

File tree

4 files changed

+89
-82
lines changed

4 files changed

+89
-82
lines changed

src/AIoTC_Config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@
158158

159159
#if defined(ARDUINO_UNOR4_WIFI)
160160
#define BOARD_HAS_SOFTSE
161+
#define BOARD_HAS_SECRET_KEY
161162
#define HAS_TCP
162163
#endif
163164

src/ArduinoIoTCloudTCP.cpp

Lines changed: 46 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#ifdef HAS_TCP
2525
#include <ArduinoIoTCloudTCP.h>
2626

27-
#ifdef BOARD_HAS_SECRET_KEY
27+
#if defined(BOARD_HAS_SECRET_KEY)
2828
#include "tls/AIoTCUPCert.h"
2929
#endif
3030

@@ -114,8 +114,13 @@ ArduinoIoTCloudTCP::ArduinoIoTCloudTCP()
114114
int ArduinoIoTCloudTCP::begin(ConnectionHandler & connection, bool const enable_watchdog, String brokerAddress, uint16_t brokerPort)
115115
{
116116
_connection = &connection;
117+
#ifdef BOARD_HAS_SECRET_KEY
118+
_brokerAddress = _password.length() ? DEFAULT_BROKER_ADDRESS_USER_PASS_AUTH : brokerAddress;
119+
_brokerPort = _password.length() ? DEFAULT_BROKER_PORT_USER_PASS_AUTH : brokerPort;
120+
#else
117121
_brokerAddress = brokerAddress;
118122
_brokerPort = brokerPort;
123+
#endif
119124
_time_service.begin(&connection);
120125
return begin(enable_watchdog, _brokerAddress, _brokerPort);
121126
}
@@ -130,55 +135,60 @@ int ArduinoIoTCloudTCP::begin(bool const enable_watchdog, String brokerAddress,
130135
DEBUG_VERBOSE("SHA256: HASH(%d) = %s", strlen(_ota_img_sha256.c_str()), _ota_img_sha256.c_str());
131136
#endif /* OTA_ENABLED */
132137

133-
#if !defined(BOARD_HAS_SECRET_KEY)
134-
if (!_crypto.begin())
135-
{
136-
DEBUG_ERROR("_crypto.begin() failed.");
137-
return 0;
138-
}
139-
if (!SElementArduinoCloudDeviceId::read(_crypto, getDeviceId(), SElementArduinoCloudSlot::DeviceId))
138+
#if defined(BOARD_HAS_SECRET_KEY)
139+
/* If board is not configured for username and password login */
140+
if(!_password.length())
140141
{
141-
DEBUG_ERROR("_crypto.readDeviceId(...) failed.");
142-
return 0;
143-
}
144142
#endif
145-
146-
#if defined(BOARD_HAS_ECCX08) || defined(BOARD_HAS_SE050) || defined(BOARD_HAS_SOFTSE)
147-
if (!SElementArduinoCloudCertificate::read(_crypto, _cert, SElementArduinoCloudSlot::CompressedCertificate))
148-
{
149-
DEBUG_ERROR("Cryptography certificate reconstruction failure.");
150-
return 0;
143+
#if defined(BOARD_HAS_SECURE_ELEMENT)
144+
if (!_crypto.begin())
145+
{
146+
DEBUG_ERROR("_crypto.begin() failed.");
147+
return 0;
148+
}
149+
if (!SElementArduinoCloudDeviceId::read(_crypto, getDeviceId(), SElementArduinoCloudSlot::DeviceId))
150+
{
151+
DEBUG_ERROR("_crypto.readDeviceId(...) failed.");
152+
return 0;
153+
}
154+
#if !defined(BOARD_HAS_OFFLOADED_ECCX08)
155+
if (!SElementArduinoCloudCertificate::read(_crypto, _cert, SElementArduinoCloudSlot::CompressedCertificate))
156+
{
157+
DEBUG_ERROR("Cryptography certificate reconstruction failure.");
158+
return 0;
159+
}
160+
#endif
161+
#endif
162+
#if defined(BOARD_HAS_SECRET_KEY)
151163
}
152-
_sslClient.setEccSlot(static_cast<int>(SElementArduinoCloudSlot::Key), _cert.bytes(), _cert.length());
153164
#endif
154165

166+
#if defined(BOARD_HAS_OFFLOADED_ECCX08)
155167

156-
#if defined(BOARD_HAS_SECRET_KEY)
157-
#if defined(ARDUINO_EDGE_CONTROL)
158-
_sslClient.appendCustomCACert(AIoTUPCert);
159-
#elif defined(ARDUINO_ARCH_ESP32)
160-
_sslClient.setCACertBundle(x509_crt_bundle);
161-
#else
162-
_sslClient.setInsecure();
163-
#endif
164-
#else
165-
#if defined(BOARD_HAS_ECCX08)
168+
#elif defined(BOARD_HAS_ECCX08)
166169
_sslClient.setClient(_connection->getClient());
167-
#elif defined(BOARD_HAS_SE050)
168-
#if defined(ARDUINO_PORTENTA_C33)
170+
_sslClient.setEccSlot(static_cast<int>(SElementArduinoCloudSlot::Key), _cert.bytes(), _cert.length());
171+
#elif defined(ARDUINO_PORTENTA_C33)
169172
_sslClient.setClient(_connection->getClient());
170173
_sslClient.setCACert(AIoTSSCert);
171-
#else
174+
#elif defined(NICLA_VISION)
172175
_sslClient.appendCustomCACert(AIoTSSCert);
173-
#endif
174-
#elif defined(BOARD_HAS_SOFTSE)
175-
_sslClient.setCACert(AIoTSSCert, strlen(AIoTSSCert));
176-
#endif
176+
#elif defined(ARDUINO_EDGE_CONTROL)
177+
_sslClient.appendCustomCACert(AIoTUPCert);
178+
#elif defined(ARDUINO_UNOR4_WIFI)
179+
180+
#elif defined(ARDUINO_ARCH_ESP32)
181+
_sslClient.setCACertBundle(x509_crt_bundle);
182+
#elif defined(ARDUINO_ARCH_ESP8266)
183+
_sslClient.setInsecure();
177184
#endif
178185

179186
_mqttClient.setClient(_sslClient);
180187
#ifdef BOARD_HAS_SECRET_KEY
181-
_mqttClient.setUsernamePassword(getDeviceId(), _password);
188+
if(_password.length())
189+
{
190+
_mqttClient.setUsernamePassword(getDeviceId(), _password);
191+
}
182192
#endif
183193
_mqttClient.onMessage(ArduinoIoTCloudTCP::onMessage);
184194
_mqttClient.setKeepAliveInterval(30 * 1000);

src/ArduinoIoTCloudTCP.h

Lines changed: 41 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -26,42 +26,42 @@
2626
#include <ArduinoIoTCloud.h>
2727
#include <ArduinoMqttClient.h>
2828

29-
#if defined(BOARD_HAS_SECRET_KEY)
30-
#if defined(BOARD_ESP)
31-
#include <WiFiClientSecure.h>
32-
#elif defined(ARDUINO_EDGE_CONTROL)
33-
#include <GSMSSLClient.h>
34-
#endif
35-
#else
29+
#if defined(BOARD_HAS_SECURE_ELEMENT)
3630
#include <Arduino_SecureElement.h>
3731
#include <utility/SElementArduinoCloudDeviceId.h>
38-
#if defined(BOARD_HAS_OFFLOADED_ECCX08)
39-
#else
32+
#if !defined(BOARD_HAS_OFFLOADED_ECCX08)
4033
#include <utility/SElementArduinoCloudCertificate.h>
41-
#ifdef BOARD_HAS_ECCX08
42-
#include "tls/BearSSLClient.h"
43-
#elif defined(BOARD_HAS_OFFLOADED_ECCX08)
44-
#include <WiFiSSLClient.h>
45-
#elif defined(BOARD_HAS_SE050)
46-
#if defined(ARDUINO_PORTENTA_C33)
47-
#include <SSLClient.h>
48-
#else
49-
#include <WiFiSSLSE050Client.h>
50-
#endif
51-
#elif defined(BOARD_HAS_SOFTSE)
52-
#include <WiFiSSLClient.h>
53-
#endif
5434
#endif
5535
#endif
5636

37+
#if defined(BOARD_HAS_OFFLOADED_ECCX08)
38+
#include "WiFiSSLClient.h"
39+
#elif defined(BOARD_HAS_ECCX08)
40+
#include "tls/BearSSLClient.h"
41+
#elif defined(ARDUINO_PORTENTA_C33)
42+
#include <SSLClient.h>
43+
#elif defined(NICLA_VISION)
44+
#include <WiFiSSLSE050Client.h>
45+
#elif defined(ARDUINO_EDGE_CONTROL)
46+
#include <GSMSSLClient.h>
47+
#elif defined(ARDUINO_UNOR4_WIFI)
48+
#include <WiFiSSLClient.h>
49+
#elif defined(BOARD_ESP)
50+
#include <WiFiClientSecure.h>
51+
#endif
52+
5753
/******************************************************************************
5854
CONSTANTS
5955
******************************************************************************/
60-
56+
#if defined(BOARD_HAS_SECURE_ELEMENT)
6157
static char const DEFAULT_BROKER_ADDRESS_SECURE_AUTH[] = "mqtts-sa.iot.arduino.cc";
6258
static uint16_t const DEFAULT_BROKER_PORT_SECURE_AUTH = 8883;
59+
#endif
60+
61+
#if defined(BOARD_HAS_SECRET_KEY)
6362
static char const DEFAULT_BROKER_ADDRESS_USER_PASS_AUTH[] = "mqtts-up.iot.arduino.cc";
6463
static uint16_t const DEFAULT_BROKER_PORT_USER_PASS_AUTH = 8884;
64+
#endif
6565

6666
/******************************************************************************
6767
* TYPEDEF
@@ -85,11 +85,7 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass
8585
virtual int connected () override;
8686
virtual void printDebugInfo() override;
8787

88-
#if !defined(BOARD_HAS_SECRET_KEY)
8988
int begin(ConnectionHandler & connection, bool const enable_watchdog = true, String brokerAddress = DEFAULT_BROKER_ADDRESS_SECURE_AUTH, uint16_t brokerPort = DEFAULT_BROKER_PORT_SECURE_AUTH);
90-
#else
91-
int begin(ConnectionHandler & connection, bool const enable_watchdog = true, String brokerAddress = DEFAULT_BROKER_ADDRESS_USER_PASS_AUTH, uint16_t brokerPort = DEFAULT_BROKER_PORT_USER_PASS_AUTH);
92-
#endif
9389
int begin(bool const enable_watchdog = true, String brokerAddress = DEFAULT_BROKER_ADDRESS_SECURE_AUTH, uint16_t brokerPort = DEFAULT_BROKER_PORT_SECURE_AUTH);
9490

9591
#ifdef BOARD_HAS_SECRET_KEY
@@ -148,29 +144,29 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass
148144

149145
#if defined(BOARD_HAS_SECRET_KEY)
150146
String _password;
151-
#if defined(BOARD_ESP)
152-
WiFiClientSecure _sslClient;
153-
#elif defined(ARDUINO_EDGE_CONTROL)
154-
GSMSSLClient _sslClient;
155-
#endif
156-
#else
147+
#endif
148+
149+
#if defined(BOARD_HAS_SECURE_ELEMENT)
157150
SecureElement _crypto;
158-
#if defined(BOARD_HAS_OFFLOADED_ECCX08)
159-
WiFiBearSSLClient _sslClient;
160-
#else
161-
ECP256Certificate _cert;
162-
#if defined(BOARD_HAS_ECCX08)
151+
#if !defined(BOARD_HAS_OFFLOADED_ECCX08)
152+
ECP256Certificate _cert;
153+
#endif
154+
#endif
155+
156+
#if defined(BOARD_HAS_OFFLOADED_ECCX08)
157+
WiFiSSLClient _sslClient;
158+
#elif defined(BOARD_HAS_ECCX08)
163159
BearSSLClient _sslClient;
164-
#elif defined(BOARD_HAS_SE050)
165-
#if defined(ARDUINO_PORTENTA_C33)
160+
#elif defined(ARDUINO_PORTENTA_C33)
166161
SSLClient _sslClient;
167-
#else
162+
#elif defined(NICLA_VISION)
168163
WiFiSSLSE050Client _sslClient;
169-
#endif
170-
#elif defined(BOARD_HAS_SOFTSE)
164+
#elif defined(ARDUINO_EDGE_CONTROL)
165+
GSMSSLClient _sslClient;
166+
#elif defined(ARDUINO_UNOR4_WIFI)
171167
WiFiSSLClient _sslClient;
172-
#endif
173-
#endif
168+
#elif defined(BOARD_ESP)
169+
WiFiClientSecure _sslClient;
174170
#endif
175171

176172
MqttClient _mqttClient;

src/tls/AIoTCUPCert.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
******************************************************************************/
2525

2626
#include <AIoTC_Config.h>
27-
#ifdef ARDUINO_ARCH_ESP32
27+
#if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_UNOR4_WIFI)
2828

2929
/******************************************************************************
3030
* CONSTANTS

0 commit comments

Comments
 (0)