Skip to content

Commit 223c368

Browse files
authored
Merge pull request #465 from pennam/tls_rm-2.0.0
Use ArduinoBearSSL library
2 parents 517434c + bf25de6 commit 223c368

File tree

310 files changed

+278
-71699
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

310 files changed

+278
-71699
lines changed

Diff for: .github/workflows/compile-examples.yml

+7
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ jobs:
105105
# Install samd platform via Boards Manager
106106
- name: arduino:samd
107107
libraries: |
108+
- name: ArduinoBearSSL
108109
- name: ArduinoECCX08
109110
- name: Blues Wireless Notecard
110111
- name: RTCZero
@@ -122,6 +123,7 @@ jobs:
122123
- name: arduino:samd
123124
- name: arduino:mbed_nano
124125
libraries: |
126+
- name: ArduinoBearSSL
125127
- name: ArduinoECCX08
126128
- name: Blues Wireless Notecard
127129
- name: RTCZero
@@ -154,6 +156,7 @@ jobs:
154156
# Install samd platform via Boards Manager
155157
- name: arduino:samd
156158
libraries: |
159+
- name: ArduinoBearSSL
157160
- name: ArduinoECCX08
158161
- name: Blues Wireless Notecard
159162
- name: RTCZero
@@ -170,6 +173,7 @@ jobs:
170173
# Install samd platform via Boards Manager
171174
- name: arduino:samd
172175
libraries: |
176+
- name: ArduinoBearSSL
173177
- name: ArduinoECCX08
174178
- name: Blues Wireless Notecard
175179
- name: RTCZero
@@ -186,6 +190,7 @@ jobs:
186190
# Install mbed_portenta platform via Boards Manager
187191
- name: arduino:mbed_portenta
188192
libraries: |
193+
- name: ArduinoBearSSL
189194
- name: ArduinoECCX08
190195
- name: Arduino_Cellular
191196
- name: Blues Wireless Notecard
@@ -214,6 +219,7 @@ jobs:
214219
# Install mbed_opta platform via Boards Manager
215220
- name: arduino:mbed_opta
216221
libraries: |
222+
- name: ArduinoBearSSL
217223
- name: ArduinoECCX08
218224
- name: Blues Wireless Notecard
219225
sketch-paths: |
@@ -228,6 +234,7 @@ jobs:
228234
# Install mbed_giga platform via Boards Manager
229235
- name: arduino:mbed_giga
230236
libraries: |
237+
- name: ArduinoBearSSL
231238
- name: ArduinoECCX08
232239
- name: Blues Wireless Notecard
233240
sketch-paths: |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
#include "arduino_secrets.h"
2+
/*
3+
This sketch demonstrates how to connect to ArduinoIoTCloud and AWS IoT core.
4+
5+
The full list of compatible boards can be found here:
6+
- https://github.com/arduino-libraries/ArduinoIoTCloud#what
7+
*/
8+
9+
#include "thingProperties.h"
10+
#include "aws_secrets.h"
11+
12+
Client& getDefaultClient() {
13+
switch(ArduinoIoTPreferredConnection.getInterface()) {
14+
15+
#ifdef BOARD_HAS_WIFI
16+
case NetworkAdapter::WIFI:
17+
static WiFiClient wclient;
18+
return wclient;
19+
#endif
20+
21+
#ifdef BOARD_HAS_ETHERNET
22+
case NetworkAdapter::ETHERNET:
23+
static EthernetClient eclient;
24+
return eclient;
25+
#endif
26+
27+
default:
28+
Serial.println("Error: could not create default AWS client");
29+
break;
30+
}
31+
}
32+
33+
unsigned long publishMillis = 0;
34+
unsigned long connectMillis = 0;
35+
36+
BearSSLClient sslClientAWS(getDefaultClient());
37+
MqttClient mqttClientAWS(sslClientAWS);
38+
39+
void setup() {
40+
/* Initialize serial and wait up to 5 seconds for port to open */
41+
Serial.begin(9600);
42+
43+
/* Configure LED pin as an output */
44+
pinMode(LED_BUILTIN, OUTPUT);
45+
46+
/* This function takes care of connecting your sketch variables to the ArduinoIoTCloud object */
47+
initProperties();
48+
49+
/* Initialize Arduino IoT Cloud library */
50+
ArduinoCloud.begin(ArduinoIoTPreferredConnection, true, "iot.arduino.cc");
51+
52+
setDebugMessageLevel(5);
53+
ArduinoCloud.printDebugInfo();
54+
55+
/* Initialize AWS Client */
56+
ArduinoBearSSL.onGetTime(getTime);
57+
sslClientAWS.setEccSlot(AWS_SLOT, AWS_CERTIFICATE);
58+
59+
mqttClientAWS.setId("ArduinoAWSClient");
60+
mqttClientAWS.onMessage(onMessageReceived);
61+
mqttClientAWS.setConnectionTimeout(10 * 1000);
62+
mqttClientAWS.setKeepAliveInterval(30 * 1000);
63+
mqttClientAWS.setCleanSession(false);
64+
}
65+
66+
void loop() {
67+
ArduinoCloud.update();
68+
potentiometer = analogRead(A0);
69+
seconds = millis() / 1000;
70+
71+
if (!ArduinoCloud.connected()) {
72+
return;
73+
}
74+
75+
if (!mqttClientAWS.connected()) {
76+
if (millis() - connectMillis > 5000) {
77+
connectMillis = millis();
78+
// MQTT client is disconnected, connect
79+
if (!connectMQTT()) {
80+
return;
81+
}
82+
} else {
83+
return;
84+
}
85+
}
86+
87+
// poll for new MQTT messages and send keep alive
88+
mqttClientAWS.poll();
89+
90+
// publish a message roughly every 5 seconds.
91+
if (millis() - publishMillis > 5000) {
92+
publishMillis = millis();
93+
94+
publishMessage();
95+
}
96+
}
97+
98+
/*
99+
* 'onLedChange' is called when the "led" property of your Thing changes
100+
*/
101+
void onLedChange() {
102+
Serial.print("LED set to ");
103+
Serial.println(led);
104+
digitalWrite(LED_BUILTIN, led);
105+
}
106+
107+
void onMessageReceived(int messageSize)
108+
{
109+
// we received a message, print out the topic and contents
110+
Serial.print("Received a message with topic '");
111+
Serial.print(mqttClientAWS.messageTopic());
112+
Serial.print("', length ");
113+
Serial.print(messageSize);
114+
Serial.println(" bytes:");
115+
116+
for (int i = 0; i < messageSize; i++) {
117+
const char c = mqttClientAWS.read();
118+
Serial.print(c);
119+
}
120+
Serial.println();
121+
}
122+
123+
int connectMQTT() {
124+
Serial.print("Attempting to connect to MQTT broker: ");
125+
Serial.print(AWS_BROKER);
126+
Serial.println(" ");
127+
128+
if (!mqttClientAWS.connect(AWS_BROKER, 8883)) {
129+
// failed, retry
130+
Serial.print(".");
131+
return 0;
132+
}
133+
Serial.println();
134+
135+
Serial.println("You're connected to the MQTT broker");
136+
Serial.println();
137+
138+
// subscribe to a topic
139+
mqttClientAWS.subscribe("arduino/incoming");
140+
return 1;
141+
}
142+
143+
void publishMessage() {
144+
Serial.println("Publishing message");
145+
146+
// send message, the Print interface can be used to set the message contents
147+
mqttClientAWS.beginMessage("arduino/outgoing");
148+
mqttClientAWS.print("hello ");
149+
mqttClientAWS.print(millis());
150+
mqttClientAWS.endMessage();
151+
}

Diff for: examples/ArduinoIoTCloud-AWS-Basic/arduino_secrets.h

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#define SECRET_SSID ""
2+
#define SECRET_OPTIONAL_PASS ""

Diff for: examples/ArduinoIoTCloud-AWS-Basic/aws_secrets.h

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/* Fill in the hostname of your AWS IoT broker */
2+
#define AWS_BROKER ""
3+
4+
#define AWS_SLOT 4
5+
6+
/* Fill in the boards public certificate */
7+
const char AWS_CERTIFICATE[] = R"(
8+
-----BEGIN CERTIFICATE-----
9+
-----END CERTIFICATE-----
10+
)";

Diff for: examples/ArduinoIoTCloud-AWS-Basic/thingProperties.h

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Code generated by Arduino IoT Cloud, DO NOT EDIT.
2+
3+
#include <ArduinoIoTCloud.h>
4+
#include <Arduino_ConnectionHandler.h>
5+
6+
const char SSID[] = SECRET_SSID; // Network SSID (name)
7+
const char PASS[] = SECRET_OPTIONAL_PASS; // Network password (use for WPA, or use as key for WEP)
8+
9+
void onLedChange();
10+
11+
bool led;
12+
int potentiometer;
13+
int seconds;
14+
15+
void initProperties() {
16+
ArduinoCloud.addProperty(led, Permission::Write).onUpdate(onLedChange);
17+
ArduinoCloud.addProperty(potentiometer, Permission::Read).publishOnChange(10);
18+
ArduinoCloud.addProperty(seconds, Permission::Read).publishOnChange(1);
19+
}
20+
21+
WiFiConnectionHandler ArduinoIoTPreferredConnection(SECRET_SSID, SECRET_OPTIONAL_PASS);

Diff for: library.properties

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ category=Communication
88
url=https://github.com/arduino-libraries/ArduinoIoTCloud
99
architectures=mbed,samd,esp8266,mbed_nano,mbed_portenta,mbed_nicla,esp32,mbed_opta,mbed_giga,renesas_portenta,renesas_uno,mbed_edge,stm32
1010
includes=ArduinoIoTCloud.h
11-
depends=Arduino_ConnectionHandler,Arduino_DebugUtils,Arduino_SecureElement,ArduinoMqttClient,ArduinoECCX08,RTCZero,Adafruit SleepyDog Library,ArduinoHttpClient,Arduino_CloudUtils
11+
depends=Arduino_ConnectionHandler,Arduino_DebugUtils,Arduino_SecureElement,ArduinoMqttClient,ArduinoECCX08,RTCZero,Adafruit SleepyDog Library,ArduinoHttpClient,Arduino_CloudUtils,ArduinoBearSSL
12+

Diff for: src/AIoTC_Config.h

-5
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,6 @@
122122
#define HAS_TCP
123123
#endif
124124

125-
#if defined(ARDUINO_NANO_RP2040_CONNECT)
126-
#define BEAR_SSL_CLIENT_IBUF_SIZE (16384 + 325) // Allows download from storage API
127-
#endif
128-
129125
#if defined(ARDUINO_EDGE_CONTROL)
130126
#define BOARD_HAS_SECRET_KEY
131127
#define HAS_TCP
@@ -149,7 +145,6 @@
149145
#endif // HAS_NOTECARD
150146

151147
#if defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_NICLA_VISION) || defined(ARDUINO_OPTA) || defined(ARDUINO_GIGA)
152-
#define BEAR_SSL_CLIENT_IBUF_SIZE (16384 + 325) // Allows download from storage API
153148
#define BOARD_STM32H7
154149
#endif
155150

Diff for: src/ArduinoBearSSLConfig.h

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
This file is part of ArduinoIoTCloud.
3+
4+
Copyright 2024 ARDUINO SA (http://www.arduino.cc/)
5+
6+
This software is released under the GNU General Public License version 3,
7+
which covers the main part of arduino-cli.
8+
The terms of this license can be found at:
9+
https://www.gnu.org/licenses/gpl-3.0.en.html
10+
11+
You can be released from the requirements of the above licenses by purchasing
12+
a commercial license. Buying such a license is mandatory if you want to modify or
13+
otherwise use the software for commercial activities involving the Arduino
14+
software without disclosing the source code of your own applications. To purchase
15+
a commercial license, send an email to [email protected].
16+
*/
17+
18+
#ifndef ARDUINO_BEARSSL_CONFIG_H_
19+
#define ARDUINO_BEARSSL_CONFIG_H_
20+
21+
/* Enabling this define allows the usage of ArduinoBearSSL without crypto chip. */
22+
//#define ARDUINO_DISABLE_ECCX08
23+
24+
/* Enable/Disable global instances*/
25+
#define ARDUINO_BEARSSL_DISABLE_AES128
26+
#define ARDUINO_BEARSSL_DISABLE_DES
27+
#define ARDUINO_BEARSSL_DISABLE_MD5
28+
#define ARDUINO_BEARSSL_DISABLE_SHA1
29+
#define ARDUINO_BEARSSL_DISABLE_SHA256
30+
31+
#define ARDUINO_BEARSSL_DISABLE_KEY_DECODER
32+
33+
/* If uncommented profile should be configured using client.setProfile(...) */
34+
//#define ARDUINO_BEARSSL_DISABLE_FULL_CLIENT_PROFILE
35+
36+
/* If uncommented TA should be configured via constructor */
37+
//#define ARDUINO_BEARSSL_DISABLE_BUILTIN_TRUST_ANCHORS
38+
39+
/* If uncommented disables br_sslio_close call.From BearSSL docs:
40+
*
41+
* br_sslio_close(): perform the SSL closure protocol. This entails sending a
42+
* close_notify alert, and receiving a close_notify response.
43+
*
44+
* Note that a number of deployed SSL implementations do not follow the protocol
45+
* for closure, and may drop the underlying socket abruptly. As such, errors are
46+
* often reported by br_sslio_close().
47+
*
48+
* In case of mbed-os + ArduinoIoTCloud br_sslio_close is endless looping
49+
* blocking sketch execution.
50+
*/
51+
#define ARDUINO_BEARSSL_DISABLE_TLS_CLOSE
52+
53+
#define BEAR_SSL_CLIENT_CHAIN_SIZE 1
54+
55+
#if defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_NICLA_VISION) || defined(ARDUINO_OPTA) ||\
56+
defined(ARDUINO_GIGA) || defined(ARDUINO_NANO_RP2040_CONNECT)
57+
/* Allows download from OTA storage API */
58+
#define BEAR_SSL_CLIENT_IBUF_SIZE (16384 + 325)
59+
#endif
60+
61+
#endif /* ARDUINO_BEARSSL_CONFIG_H_ */

0 commit comments

Comments
 (0)