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 );
8
21
9
22
void setup () {
10
23
Serial.begin (9600 );
11
24
while (!Serial);
12
25
13
- if (!Crypto.begin ()) {
26
+ SecureElement secureElement;
27
+
28
+ if (!secureElement.begin ()) {
14
29
Serial.println (" No crypto present!" );
15
30
while (1 );
16
31
}
17
32
18
- if (!Crypto.locked ()) {
33
+ if (!secureElement.locked ()) {
34
+ /* WARNING: This string is parsed from IoTCloud frontend */
19
35
String lockConfirm = promptAndReadLine (" Your crypto is unlocked, would you like to lock it (y/N): " );
20
36
lockConfirm.toLowerCase ();
21
37
@@ -24,12 +40,14 @@ void setup() {
24
40
while (1 );
25
41
}
26
42
27
- if (!Crypto.writeConfiguration (DEFAULT_ECCX08_TLS_CONFIG)) {
43
+ if (!secureElement.writeConfiguration ()) {
44
+ /* WARNING: This string is parsed from IoTCloud frontend */
28
45
Serial.println (" Writing crypto configuration failed!" );
29
46
while (1 );
30
47
}
31
48
32
- if (!Crypto.lock ()) {
49
+ if (!secureElement.lock ()) {
50
+ /* WARNING: This string is parsed from IoTCloud frontend */
33
51
Serial.println (" Locking crypto configuration failed!" );
34
52
while (1 );
35
53
}
@@ -38,36 +56,43 @@ void setup() {
38
56
Serial.println ();
39
57
}
40
58
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 );
42
61
csrConfirm.toLowerCase ();
43
62
44
63
if (csrConfirm != " y" ) {
45
64
Serial.println (" That's all folks" );
46
65
while (1 );
47
66
}
48
67
68
+ ECP256Certificate Certificate;
69
+
49
70
if (!Certificate.begin ()) {
50
71
Serial.println (" Error starting CSR generation!" );
51
72
while (1 );
52
73
}
53
74
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: " );
55
77
Certificate.setSubjectCommonName (deviceId);
56
78
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 */
58
81
Serial.println (" Error generating CSR!" );
59
82
while (1 );
60
83
}
61
84
62
85
String csr = Certificate.getCSRPEM ();
63
86
64
87
if (!csr) {
88
+ /* WARNING: This string is parsed from IoTCloud frontend */
65
89
Serial.println (" Error generating CSR!" );
66
90
while (1 );
67
91
}
68
92
69
93
Serial.println (" Generated CSR is:" );
70
94
Serial.println ();
95
+ /* WARNING: This string is parsed from IoTCloud frontend */
71
96
Serial.println (csr);
72
97
73
98
String issueYear = promptAndReadLine (" Please enter the issue year of the certificate (2000 - 2031): " );
@@ -79,20 +104,21 @@ void setup() {
79
104
String authorityKeyIdentifier = promptAndReadLine (" Please enter the certificates authority key identifier: " );
80
105
String signature = promptAndReadLine (" Please enter the certificates signature: " );
81
106
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 ];
85
110
86
111
hexStringToBytes (serialNumber, serialNumberBytes, sizeof (serialNumberBytes));
87
112
hexStringToBytes (authorityKeyIdentifier, authorityKeyIdentifierBytes, sizeof (authorityKeyIdentifierBytes));
88
113
hexStringToBytes (signature, signatureBytes, sizeof (signatureBytes));
89
114
90
- if (!Crypto. writeDeviceId ( deviceId, CryptoSlot ::DeviceId)) {
115
+ if (!SElementArduinoCloudDeviceId::write (secureElement, deviceId, SElementArduinoCloudSlot ::DeviceId)) {
91
116
Serial.println (" Error storing device ID!" );
92
117
while (1 );
93
118
}
94
119
95
120
if (!Certificate.begin ()) {
121
+ /* WARNING: This string is parsed from IoTCloud frontend */
96
122
Serial.println (" Error starting crypto storage!" );
97
123
while (1 );
98
124
}
@@ -111,20 +137,17 @@ void setup() {
111
137
Certificate.setIssueHour (issueHour.toInt ());
112
138
Certificate.setExpireYears (expireYears.toInt ());
113
139
114
- if (!Crypto. buildCert ( Certificate, CryptoSlot ::Key)) {
140
+ if (!SElementArduinoCloudCertificate::build (secureElement, Certificate, static_cast < int >(SElementArduinoCloudSlot ::Key) )) {
115
141
Serial.println (" Error building cert!" );
116
142
while (1 );
117
143
}
118
-
119
- if (!Crypto. writeCert ( Certificate, CryptoSlot ::CompressedCertificate)) {
144
+
145
+ if (!SElementArduinoCloudCertificate::write (secureElement, Certificate, SElementArduinoCloudSlot ::CompressedCertificate)) {
120
146
Serial.println (" Error storing cert!" );
121
147
while (1 );
122
148
}
123
149
124
- if (!DEBUG) {
125
- return ;
126
- }
127
-
150
+ /* WARNING: This string is parsed from IoTCloud frontend */
128
151
Serial.println (" Compressed cert = " );
129
152
130
153
const byte* certData = Certificate.bytes ();
@@ -139,23 +162,72 @@ void setup() {
139
162
Serial.print (b, HEX);
140
163
}
141
164
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
142
200
}
143
201
144
202
void loop () {
145
203
}
146
204
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
+ }
150
214
Serial.println (s);
151
215
152
216
return s;
153
217
}
154
218
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
+ }
157
226
158
- while (1 ) {
227
+ String readLine (const unsigned int timeout) {
228
+ String line;
229
+ const unsigned int start = millis ();
230
+ while (!isExpired (start, timeout)) {
159
231
if (Serial.available ()) {
160
232
char c = Serial.read ();
161
233
0 commit comments