Skip to content

Commit d120862

Browse files
committed
Examples: update provisioning sketch
- Use Arduino_SecureElement library - Add WiFi version check - Align code to "Cloud version" provisioning sketch
1 parent 2f6cf50 commit d120862

File tree

1 file changed

+103
-31
lines changed

1 file changed

+103
-31
lines changed

examples/utility/Provisioning/Provisioning.ino

+103-31
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,37 @@
1-
#include <ArduinoIoTCloud.h>
2-
#include "ECCX08TLSConfig.h"
3-
4-
const bool DEBUG = true;
5-
6-
ArduinoIoTCloudCertClass Certificate;
7-
CryptoUtil Crypto;
1+
#include <Arduino_SecureElement.h>
2+
#include <utility/SElementArduinoCloud.h>
3+
#include <utility/SElementArduinoCloudCertificate.h>
4+
#include <utility/SElementArduinoCloudDeviceId.h>
5+
#include <utility/SElementCSR.h>
6+
7+
#ifdef ARDUINO_SAMD_MKR1000
8+
#include <WiFi101.h>
9+
#define LATEST_WIFI_FIRMWARE_VERSION WIFI_FIRMWARE_LATEST_MODEL_B
10+
#endif
11+
#if defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_SAMD_NANO_33_IOT) || defined(ARDUINO_NANO_RP2040_CONNECT)
12+
#include <WiFiNINA.h>
13+
#define LATEST_WIFI_FIRMWARE_VERSION WIFI_FIRMWARE_LATEST_VERSION
14+
#endif
15+
#if defined(ARDUINO_UNOR4_WIFI)
16+
#include <WiFiS3.h>
17+
#define LATEST_WIFI_FIRMWARE_VERSION WIFI_FIRMWARE_LATEST_VERSION
18+
#endif
19+
20+
String promptAndReadLine(const char* prompt, const unsigned int timeout = 0);
821

922
void setup() {
1023
Serial.begin(9600);
1124
while (!Serial);
1225

13-
if (!Crypto.begin()) {
26+
SecureElement secureElement;
27+
28+
if (!secureElement.begin()) {
1429
Serial.println("No crypto present!");
1530
while (1);
1631
}
1732

18-
if (!Crypto.locked()) {
33+
if (!secureElement.locked()) {
34+
/* WARNING: This string is parsed from IoTCloud frontend */
1935
String lockConfirm = promptAndReadLine("Your crypto is unlocked, would you like to lock it (y/N): ");
2036
lockConfirm.toLowerCase();
2137

@@ -24,12 +40,14 @@ void setup() {
2440
while (1);
2541
}
2642

27-
if (!Crypto.writeConfiguration(DEFAULT_ECCX08_TLS_CONFIG)) {
43+
if (!secureElement.writeConfiguration()) {
44+
/* WARNING: This string is parsed from IoTCloud frontend */
2845
Serial.println("Writing crypto configuration failed!");
2946
while (1);
3047
}
3148

32-
if (!Crypto.lock()) {
49+
if (!secureElement.lock()) {
50+
/* WARNING: This string is parsed from IoTCloud frontend */
3351
Serial.println("Locking crypto configuration failed!");
3452
while (1);
3553
}
@@ -38,36 +56,43 @@ void setup() {
3856
Serial.println();
3957
}
4058

41-
String csrConfirm = promptAndReadLine("Would you like to generate a new private key and CSR (y/N): ");
59+
/* WARNING: This string is parsed from IoTCloud frontend */
60+
String csrConfirm = promptAndReadLine("Would you like to generate a new private key and CSR (y/N): ", 5000);
4261
csrConfirm.toLowerCase();
4362

4463
if (csrConfirm != "y") {
4564
Serial.println("That's all folks");
4665
while (1);
4766
}
4867

68+
ECP256Certificate Certificate;
69+
4970
if (!Certificate.begin()) {
5071
Serial.println("Error starting CSR generation!");
5172
while (1);
5273
}
5374

54-
String deviceId = promptAndReadLine("Please enter the device ID: ");
75+
/* WARNING: This string is parsed from IoTCloud frontend */
76+
String deviceId = promptAndReadLine("Please enter the device id: ");
5577
Certificate.setSubjectCommonName(deviceId);
5678

57-
if (!Crypto.buildCSR(Certificate, CryptoSlot::Key, true)) {
79+
if (!SElementCSR::build(secureElement, Certificate, (int)SElementArduinoCloudSlot::Key, true)) {
80+
/* WARNING: This string is parsed from IoTCloud frontend */
5881
Serial.println("Error generating CSR!");
5982
while (1);
6083
}
6184

6285
String csr = Certificate.getCSRPEM();
6386

6487
if (!csr) {
88+
/* WARNING: This string is parsed from IoTCloud frontend */
6589
Serial.println("Error generating CSR!");
6690
while (1);
6791
}
6892

6993
Serial.println("Generated CSR is:");
7094
Serial.println();
95+
/* WARNING: This string is parsed from IoTCloud frontend */
7196
Serial.println(csr);
7297

7398
String issueYear = promptAndReadLine("Please enter the issue year of the certificate (2000 - 2031): ");
@@ -79,20 +104,21 @@ void setup() {
79104
String authorityKeyIdentifier = promptAndReadLine("Please enter the certificates authority key identifier: ");
80105
String signature = promptAndReadLine("Please enter the certificates signature: ");
81106

82-
byte serialNumberBytes[CERT_SERIAL_NUMBER_LENGTH];
83-
byte authorityKeyIdentifierBytes[CERT_AUTHORITY_KEY_ID_LENGTH];
84-
byte signatureBytes[CERT_SIGNATURE_LENGTH];
107+
byte serialNumberBytes[ECP256_CERT_SERIAL_NUMBER_LENGTH];
108+
byte authorityKeyIdentifierBytes[ECP256_CERT_AUTHORITY_KEY_ID_LENGTH];
109+
byte signatureBytes[ECP256_CERT_SIGNATURE_LENGTH];
85110

86111
hexStringToBytes(serialNumber, serialNumberBytes, sizeof(serialNumberBytes));
87112
hexStringToBytes(authorityKeyIdentifier, authorityKeyIdentifierBytes, sizeof(authorityKeyIdentifierBytes));
88113
hexStringToBytes(signature, signatureBytes, sizeof(signatureBytes));
89114

90-
if (!Crypto.writeDeviceId(deviceId, CryptoSlot::DeviceId)) {
115+
if (!SElementArduinoCloudDeviceId::write(secureElement, deviceId, SElementArduinoCloudSlot::DeviceId)) {
91116
Serial.println("Error storing device ID!");
92117
while (1);
93118
}
94119

95120
if (!Certificate.begin()) {
121+
/* WARNING: This string is parsed from IoTCloud frontend */
96122
Serial.println("Error starting crypto storage!");
97123
while (1);
98124
}
@@ -111,20 +137,17 @@ void setup() {
111137
Certificate.setIssueHour(issueHour.toInt());
112138
Certificate.setExpireYears(expireYears.toInt());
113139

114-
if (!Crypto.buildCert(Certificate, CryptoSlot::Key)) {
140+
if (!SElementArduinoCloudCertificate::build(secureElement, Certificate, static_cast<int>(SElementArduinoCloudSlot::Key))) {
115141
Serial.println("Error building cert!");
116142
while (1);
117143
}
118-
119-
if (!Crypto.writeCert(Certificate, CryptoSlot::CompressedCertificate)) {
144+
145+
if (!SElementArduinoCloudCertificate::write(secureElement, Certificate, SElementArduinoCloudSlot::CompressedCertificate)) {
120146
Serial.println("Error storing cert!");
121147
while (1);
122148
}
123149

124-
if (!DEBUG) {
125-
return;
126-
}
127-
150+
/* WARNING: This string is parsed from IoTCloud frontend */
128151
Serial.println("Compressed cert = ");
129152

130153
const byte* certData = Certificate.bytes();
@@ -139,23 +162,72 @@ void setup() {
139162
Serial.print(b, HEX);
140163
}
141164
Serial.println();
165+
166+
167+
String cert = Certificate.getCertPEM();
168+
if (!cert) {
169+
Serial.println("Error generating cert!");
170+
while (1);
171+
}
172+
Serial.println("Cert PEM = ");
173+
Serial.println();
174+
Serial.println(cert);
175+
176+
177+
#ifdef LATEST_WIFI_FIRMWARE_VERSION
178+
Serial.println("Checking firmware of WiFi module...");
179+
Serial.println();
180+
String fv = WiFi.firmwareVersion();
181+
/* WARNING: This string is parsed from IoTCloud frontend */
182+
Serial.print("Current firmware version: ");
183+
/* WARNING: This string is parsed from IoTCloud frontend */
184+
Serial.println(fv);
185+
186+
String latestFv = LATEST_WIFI_FIRMWARE_VERSION;
187+
if (fv >= latestFv) {
188+
/* WARNING: This string is parsed from IoTCloud frontend */
189+
Serial.println("Latest firmware version correctly installed.");
190+
} else {
191+
/* WARNING: This string is parsed from IoTCloud frontend */
192+
String latestFvStr = "The firmware is not up to date. Latest version available: " + latestFv;
193+
Serial.println(latestFvStr);
194+
}
195+
#else
196+
Serial.println();
197+
/* WARNING: This string is parsed from IoTCloud frontend */
198+
Serial.println("Program finished.");
199+
#endif
142200
}
143201

144202
void loop() {
145203
}
146204

147-
String promptAndReadLine(const char* prompt) {
148-
Serial.print(prompt);
149-
String s = readLine();
205+
String promptAndReadLine(const char* prompt, const unsigned int timeout) {
206+
String s = "";
207+
while(1) {
208+
Serial.print(prompt);
209+
s = readLine(timeout);
210+
if (s.length() > 0) {
211+
break;
212+
}
213+
}
150214
Serial.println(s);
151215

152216
return s;
153217
}
154218

155-
String readLine() {
156-
String line;
219+
bool isExpired(const unsigned int start, const unsigned int timeout) {
220+
if (timeout) {
221+
return (millis() - start) > timeout;
222+
} else {
223+
return false;
224+
}
225+
}
157226

158-
while (1) {
227+
String readLine(const unsigned int timeout) {
228+
String line;
229+
const unsigned int start = millis();
230+
while (!isExpired(start, timeout)) {
159231
if (Serial.available()) {
160232
char c = Serial.read();
161233

0 commit comments

Comments
 (0)