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
8
15
9
16
void setup () {
10
17
Serial.begin (9600 );
11
18
while (!Serial);
12
19
13
- if (!Crypto.begin ()) {
20
+ SecureElement secureElement;
21
+
22
+ if (!secureElement.begin ()) {
14
23
Serial.println (" No crypto present!" );
15
24
while (1 );
16
25
}
17
26
18
- if (!Crypto.locked ()) {
27
+ if (!secureElement.locked ()) {
28
+ /* WARNING: This string is parsed from IoTCloud frontend */
19
29
String lockConfirm = promptAndReadLine (" Your crypto is unlocked, would you like to lock it (y/N): " );
20
30
lockConfirm.toLowerCase ();
21
31
@@ -24,12 +34,14 @@ void setup() {
24
34
while (1 );
25
35
}
26
36
27
- if (!Crypto.writeConfiguration (DEFAULT_ECCX08_TLS_CONFIG)) {
37
+ if (!secureElement.writeConfiguration ()) {
38
+ /* WARNING: This string is parsed from IoTCloud frontend */
28
39
Serial.println (" Writing crypto configuration failed!" );
29
40
while (1 );
30
41
}
31
42
32
- if (!Crypto.lock ()) {
43
+ if (!secureElement.lock ()) {
44
+ /* WARNING: This string is parsed from IoTCloud frontend */
33
45
Serial.println (" Locking crypto configuration failed!" );
34
46
while (1 );
35
47
}
@@ -38,6 +50,7 @@ void setup() {
38
50
Serial.println ();
39
51
}
40
52
53
+ /* WARNING: This string is parsed from IoTCloud frontend */
41
54
String csrConfirm = promptAndReadLine (" Would you like to generate a new private key and CSR (y/N): " );
42
55
csrConfirm.toLowerCase ();
43
56
@@ -46,22 +59,27 @@ void setup() {
46
59
while (1 );
47
60
}
48
61
62
+ ECP256Certificate Certificate;
63
+
49
64
if (!Certificate.begin ()) {
50
65
Serial.println (" Error starting CSR generation!" );
51
66
while (1 );
52
67
}
53
68
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: " );
55
71
Certificate.setSubjectCommonName (deviceId);
56
72
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 */
58
75
Serial.println (" Error generating CSR!" );
59
76
while (1 );
60
77
}
61
78
62
79
String csr = Certificate.getCSRPEM ();
63
80
64
81
if (!csr) {
82
+ /* WARNING: This string is parsed from IoTCloud frontend */
65
83
Serial.println (" Error generating CSR!" );
66
84
while (1 );
67
85
}
@@ -79,15 +97,15 @@ void setup() {
79
97
String authorityKeyIdentifier = promptAndReadLine (" Please enter the certificates authority key identifier: " );
80
98
String signature = promptAndReadLine (" Please enter the certificates signature: " );
81
99
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 ];
85
103
86
104
hexStringToBytes (serialNumber, serialNumberBytes, sizeof (serialNumberBytes));
87
105
hexStringToBytes (authorityKeyIdentifier, authorityKeyIdentifierBytes, sizeof (authorityKeyIdentifierBytes));
88
106
hexStringToBytes (signature, signatureBytes, sizeof (signatureBytes));
89
107
90
- if (!Crypto. writeDeviceId ( deviceId, CryptoSlot ::DeviceId)) {
108
+ if (!SElementArduinoCloudDeviceId::write (secureElement, deviceId, SElementArduinoCloudSlot ::DeviceId)) {
91
109
Serial.println (" Error storing device ID!" );
92
110
while (1 );
93
111
}
@@ -111,20 +129,16 @@ void setup() {
111
129
Certificate.setIssueHour (issueHour.toInt ());
112
130
Certificate.setExpireYears (expireYears.toInt ());
113
131
114
- if (!Crypto. buildCert ( Certificate, CryptoSlot ::Key)) {
132
+ if (!SElementArduinoCloudCertificate::build (secureElement, Certificate, static_cast < int >(SElementArduinoCloudSlot ::Key) )) {
115
133
Serial.println (" Error building cert!" );
116
134
while (1 );
117
135
}
118
136
119
- if (!Crypto. writeCert ( Certificate, CryptoSlot ::CompressedCertificate)) {
137
+ if (!SElementArduinoCloudCertificate::write (secureElement, Certificate, SElementArduinoCloudSlot ::CompressedCertificate)) {
120
138
Serial.println (" Error storing cert!" );
121
139
while (1 );
122
140
}
123
141
124
- if (!DEBUG) {
125
- return ;
126
- }
127
-
128
142
Serial.println (" Compressed cert = " );
129
143
130
144
const byte* certData = Certificate.bytes ();
@@ -139,6 +153,41 @@ void setup() {
139
153
Serial.print (b, HEX);
140
154
}
141
155
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
142
191
}
143
192
144
193
void loop () {
0 commit comments