11#include < ArduinoIoTCloud.h>
22#include " ECCX08TLSConfig.h"
33
4- #include < ArduinoECCX08.h>
5-
64const bool DEBUG = true ;
7- const int keySlot = 0 ;
8- const int compressedCertSlot = 10 ;
9- const int serialNumberAndAuthorityKeyIdentifierSlot = 11 ;
10- const int deviceIdSlot = 12 ;
115
12- ECCX08CertClass ECCX08Cert;
6+ ArduinoIoTCloudCertClass Certificate;
7+ CryptoUtil Crypto;
138
149void setup () {
1510 Serial.begin (9600 );
1611 while (!Serial);
1712
18- if (!ECCX08 .begin ()) {
19- Serial.println (" No ECCX08 present!" );
13+ if (!Crypto .begin ()) {
14+ Serial.println (" No crypto present!" );
2015 while (1 );
2116 }
2217
23- if (!ECCX08 .locked ()) {
24- String lockConfirm = promptAndReadLine (" Your ECCX08 is unlocked, would you like to lock it (y/N): " );
18+ if (!Crypto .locked ()) {
19+ String lockConfirm = promptAndReadLine (" Your crypto is unlocked, would you like to lock it (y/N): " );
2520 lockConfirm.toLowerCase ();
2621
2722 if (lockConfirm != " y" ) {
2823 Serial.println (" That's all folks" );
2924 while (1 );
3025 }
3126
32- if (!ECCX08 .writeConfiguration (DEFAULT_ECCX08_TLS_CONFIG)) {
33- Serial.println (" Writing ECCX08 configuration failed!" );
27+ if (!Crypto .writeConfiguration (DEFAULT_ECCX08_TLS_CONFIG)) {
28+ Serial.println (" Writing crypto configuration failed!" );
3429 while (1 );
3530 }
3631
37- if (!ECCX08 .lock ()) {
38- Serial.println (" Locking ECCX08 configuration failed!" );
32+ if (!Crypto .lock ()) {
33+ Serial.println (" Locking crypto configuration failed!" );
3934 while (1 );
4035 }
4136
42- Serial.println (" ECCX08 locked successfully" );
37+ Serial.println (" crypto locked successfully" );
4338 Serial.println ();
4439 }
4540
@@ -51,15 +46,20 @@ void setup() {
5146 while (1 );
5247 }
5348
54- if (!ECCX08Cert. beginCSR (keySlot, true )) {
49+ if (!Certificate. begin ( )) {
5550 Serial.println (" Error starting CSR generation!" );
5651 while (1 );
5752 }
5853
5954 String deviceId = promptAndReadLine (" Please enter the device id: " );
60- ECCX08Cert.setSubjectCommonName (deviceId);
55+ Certificate.setSubjectCommonName (deviceId);
56+
57+ if (!Crypto.buildCSR (Certificate, CryptoSlot::Key, true )) {
58+ Serial.println (" Error generating CSR!" );
59+ while (1 );
60+ }
6161
62- String csr = ECCX08Cert. endCSR ();
62+ String csr = Certificate. getCSRPEM ();
6363
6464 if (!csr) {
6565 Serial.println (" Error generating CSR!" );
@@ -79,52 +79,45 @@ void setup() {
7979 String authorityKeyIdentifier = promptAndReadLine (" Please enter the certificates authority key identifier: " );
8080 String signature = promptAndReadLine (" Please enter the certificates signature: " );
8181
82- byte deviceIdBytes[72 ];
83- byte serialNumberBytes[16 ];
84- byte authorityKeyIdentifierBytes[20 ];
85- byte signatureBytes[64 ];
82+ byte serialNumberBytes[CERT_SERIAL_NUMBER_LENGTH];
83+ byte authorityKeyIdentifierBytes[CERT_AUTHORITY_KEY_ID_LENGTH];
84+ byte signatureBytes[CERT_SIGNATURE_LENGTH];
8685
87- deviceId.getBytes (deviceIdBytes, sizeof (deviceIdBytes));
8886 hexStringToBytes (serialNumber, serialNumberBytes, sizeof (serialNumberBytes));
8987 hexStringToBytes (authorityKeyIdentifier, authorityKeyIdentifierBytes, sizeof (authorityKeyIdentifierBytes));
9088 hexStringToBytes (signature, signatureBytes, sizeof (signatureBytes));
9189
92- if (!ECCX08. writeSlot (deviceIdSlot, deviceIdBytes, sizeof (deviceIdBytes) )) {
90+ if (!Crypto. writeDeviceId (deviceId, CryptoSlot::DeviceId )) {
9391 Serial.println (" Error storing device id!" );
9492 while (1 );
9593 }
9694
97- if (!ECCX08Cert. beginStorage (compressedCertSlot, serialNumberAndAuthorityKeyIdentifierSlot )) {
98- Serial.println (" Error starting ECCX08 storage!" );
95+ if (!Certificate. begin ( )) {
96+ Serial.println (" Error starting crypto storage!" );
9997 while (1 );
10098 }
10199
102- ECCX08Cert.setSignature (signatureBytes);
103- ECCX08Cert.setAuthorityKeyIdentifier (authorityKeyIdentifierBytes);
104- ECCX08Cert.setSerialNumber (serialNumberBytes);
105- ECCX08Cert.setIssueYear (issueYear.toInt ());
106- ECCX08Cert.setIssueMonth (issueMonth.toInt ());
107- ECCX08Cert.setIssueDay (issueDay.toInt ());
108- ECCX08Cert.setIssueHour (issueHour.toInt ());
109- ECCX08Cert.setExpireYears (expireYears.toInt ());
110-
111- if (!ECCX08Cert.endStorage ()) {
112- Serial.println (" Error storing ECCX08 compressed cert!" );
100+ Certificate.setSubjectCommonName (deviceId);
101+ Certificate.setIssuerCountryName (" US" );
102+ Certificate.setIssuerOrganizationName (" Arduino LLC US" );
103+ Certificate.setIssuerOrganizationalUnitName (" IT" );
104+ Certificate.setIssuerCommonName (" Arduino" );
105+ Certificate.setSignature (signatureBytes, sizeof (signatureBytes));
106+ Certificate.setAuthorityKeyId (authorityKeyIdentifierBytes, sizeof (authorityKeyIdentifierBytes));
107+ Certificate.setSerialNumber (serialNumberBytes, sizeof (serialNumberBytes));
108+ Certificate.setIssueYear (issueYear.toInt ());
109+ Certificate.setIssueMonth (issueMonth.toInt ());
110+ Certificate.setIssueDay (issueDay.toInt ());
111+ Certificate.setIssueHour (issueHour.toInt ());
112+ Certificate.setExpireYears (expireYears.toInt ());
113+
114+ if (!Crypto.buildCert (Certificate, CryptoSlot::Key)) {
115+ Serial.println (" Error building cert!" );
113116 while (1 );
114117 }
115-
116- if (!ECCX08Cert.beginReconstruction (keySlot, compressedCertSlot, serialNumberAndAuthorityKeyIdentifierSlot)) {
117- Serial.println (" Error starting ECCX08 cert reconstruction!" );
118- while (1 );
119- }
120-
121- ECCX08Cert.setIssuerCountryName (" US" );
122- ECCX08Cert.setIssuerOrganizationName (" Arduino LLC US" );
123- ECCX08Cert.setIssuerOrganizationalUnitName (" IT" );
124- ECCX08Cert.setIssuerCommonName (" Arduino" );
125-
126- if (!ECCX08Cert.endReconstruction ()) {
127- Serial.println (" Error reconstructing ECCX08 compressed cert!" );
118+
119+ if (!Crypto.writeCert (Certificate, CryptoSlot::CompressedCertificate)) {
120+ Serial.println (" Error storing cert!" );
128121 while (1 );
129122 }
130123
@@ -134,8 +127,8 @@ void setup() {
134127
135128 Serial.println (" Compressed cert = " );
136129
137- const byte* certData = ECCX08Cert .bytes ();
138- int certLength = ECCX08Cert .length ();
130+ const byte* certData = Certificate .bytes ();
131+ int certLength = Certificate .length ();
139132
140133 for (int i = 0 ; i < certLength; i++) {
141134 byte b = certData[i];
0 commit comments