Skip to content

Commit 31fa0f8

Browse files
committed
Examples: update provisioning sketch
1 parent b4db6bd commit 31fa0f8

File tree

1 file changed

+72
-23
lines changed

1 file changed

+72
-23
lines changed

examples/utility/Provisioning/Provisioning.ino

Lines changed: 72 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,31 @@
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
815

916
void setup() {
1017
Serial.begin(9600);
1118
while (!Serial);
1219

13-
if (!Crypto.begin()) {
20+
SecureElement secureElement;
21+
22+
if (!secureElement.begin()) {
1423
Serial.println("No crypto present!");
1524
while (1);
1625
}
1726

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

@@ -24,12 +34,14 @@ void setup() {
2434
while (1);
2535
}
2636

27-
if (!Crypto.writeConfiguration(DEFAULT_ECCX08_TLS_CONFIG)) {
37+
if (!secureElement.writeConfiguration()) {
38+
/* WARNING: This string is parsed from IoTCloud frontend */
2839
Serial.println("Writing crypto configuration failed!");
2940
while (1);
3041
}
3142

32-
if (!Crypto.lock()) {
43+
if (!secureElement.lock()) {
44+
/* WARNING: This string is parsed from IoTCloud frontend */
3345
Serial.println("Locking crypto configuration failed!");
3446
while (1);
3547
}
@@ -38,6 +50,7 @@ void setup() {
3850
Serial.println();
3951
}
4052

53+
/* WARNING: This string is parsed from IoTCloud frontend */
4154
String csrConfirm = promptAndReadLine("Would you like to generate a new private key and CSR (y/N): ");
4255
csrConfirm.toLowerCase();
4356

@@ -46,22 +59,27 @@ void setup() {
4659
while (1);
4760
}
4861

62+
ECP256Certificate Certificate;
63+
4964
if (!Certificate.begin()) {
5065
Serial.println("Error starting CSR generation!");
5166
while (1);
5267
}
5368

54-
String deviceId = promptAndReadLine("Please enter the device ID: ");
69+
/* WARNING: This string is parsed from IoTCloud frontend */
70+
String deviceId = promptAndReadLine("Please enter the device id: ");
5571
Certificate.setSubjectCommonName(deviceId);
5672

57-
if (!Crypto.buildCSR(Certificate, CryptoSlot::Key, true)) {
73+
if (!SElementCSR::build(secureElement, Certificate, (int)SElementArduinoCloudSlot::Key, true)) {
74+
/* WARNING: This string is parsed from IoTCloud frontend */
5875
Serial.println("Error generating CSR!");
5976
while (1);
6077
}
6178

6279
String csr = Certificate.getCSRPEM();
6380

6481
if (!csr) {
82+
/* WARNING: This string is parsed from IoTCloud frontend */
6583
Serial.println("Error generating CSR!");
6684
while (1);
6785
}
@@ -79,15 +97,15 @@ void setup() {
7997
String authorityKeyIdentifier = promptAndReadLine("Please enter the certificates authority key identifier: ");
8098
String signature = promptAndReadLine("Please enter the certificates signature: ");
8199

82-
byte serialNumberBytes[CERT_SERIAL_NUMBER_LENGTH];
83-
byte authorityKeyIdentifierBytes[CERT_AUTHORITY_KEY_ID_LENGTH];
84-
byte signatureBytes[CERT_SIGNATURE_LENGTH];
100+
byte serialNumberBytes[ECP256_CERT_SERIAL_NUMBER_LENGTH];
101+
byte authorityKeyIdentifierBytes[ECP256_CERT_AUTHORITY_KEY_ID_LENGTH];
102+
byte signatureBytes[ECP256_CERT_SIGNATURE_LENGTH];
85103

86104
hexStringToBytes(serialNumber, serialNumberBytes, sizeof(serialNumberBytes));
87105
hexStringToBytes(authorityKeyIdentifier, authorityKeyIdentifierBytes, sizeof(authorityKeyIdentifierBytes));
88106
hexStringToBytes(signature, signatureBytes, sizeof(signatureBytes));
89107

90-
if (!Crypto.writeDeviceId(deviceId, CryptoSlot::DeviceId)) {
108+
if (!SElementArduinoCloudDeviceId::write(secureElement, deviceId, SElementArduinoCloudSlot::DeviceId)) {
91109
Serial.println("Error storing device ID!");
92110
while (1);
93111
}
@@ -111,20 +129,16 @@ void setup() {
111129
Certificate.setIssueHour(issueHour.toInt());
112130
Certificate.setExpireYears(expireYears.toInt());
113131

114-
if (!Crypto.buildCert(Certificate, CryptoSlot::Key)) {
132+
if (!SElementArduinoCloudCertificate::build(secureElement, Certificate, static_cast<int>(SElementArduinoCloudSlot::Key))) {
115133
Serial.println("Error building cert!");
116134
while (1);
117135
}
118136

119-
if (!Crypto.writeCert(Certificate, CryptoSlot::CompressedCertificate)) {
137+
if (!SElementArduinoCloudCertificate::write(secureElement, Certificate, SElementArduinoCloudSlot::CompressedCertificate)) {
120138
Serial.println("Error storing cert!");
121139
while (1);
122140
}
123141

124-
if (!DEBUG) {
125-
return;
126-
}
127-
128142
Serial.println("Compressed cert = ");
129143

130144
const byte* certData = Certificate.bytes();
@@ -139,6 +153,41 @@ void setup() {
139153
Serial.print(b, HEX);
140154
}
141155
Serial.println();
156+
157+
158+
String cert = Certificate.getCertPEM();
159+
if (!cert) {
160+
Serial.println("Error generating cert!");
161+
while (1);
162+
}
163+
Serial.println("Cert PEM = ");
164+
Serial.println();
165+
Serial.println(cert);
166+
167+
168+
#ifdef LATEST_WIFI_FIRMWARE_VERSION
169+
Serial.println("Checking firmware of WiFi module...");
170+
Serial.println();
171+
String fv = WiFi.firmwareVersion();
172+
/* WARNING: This string is parsed from IoTCloud frontend */
173+
Serial.print("Current firmware version: ");
174+
/* WARNING: This string is parsed from IoTCloud frontend */
175+
Serial.println(fv);
176+
177+
String latestFv = LATEST_WIFI_FIRMWARE_VERSION;
178+
if (fv >= latestFv) {
179+
/* WARNING: This string is parsed from IoTCloud frontend */
180+
Serial.println("Latest firmware version correctly installed.");
181+
} else {
182+
/* WARNING: This string is parsed from IoTCloud frontend */
183+
String latestFvStr = "The firmware is not up to date. Latest version available: " + latestFv;
184+
Serial.println(latestFvStr);
185+
}
186+
#else
187+
Serial.println();
188+
/* WARNING: This string is parsed from IoTCloud frontend */
189+
Serial.println("Program finished.");
190+
#endif
142191
}
143192

144193
void loop() {

0 commit comments

Comments
 (0)