1
1
#include < ArduinoIoTCloud.h>
2
2
#include " ECCX08TLSConfig.h"
3
3
4
- #include < ArduinoECCX08.h>
5
-
6
4
const bool DEBUG = true ;
7
- const int keySlot = 0 ;
8
- const int compressedCertSlot = 10 ;
9
- const int serialNumberAndAuthorityKeyIdentifierSlot = 11 ;
10
- const int deviceIdSlot = 12 ;
11
5
12
- ECCX08CertClass ECCX08Cert;
6
+ ArduinoIoTCloudCertClass Certificate;
7
+ CryptoUtil Crypto;
13
8
14
9
void setup () {
15
10
Serial.begin (9600 );
16
11
while (!Serial);
17
12
18
- if (!ECCX08 .begin ()) {
19
- Serial.println (" No ECCX08 present!" );
13
+ if (!Crypto .begin ()) {
14
+ Serial.println (" No crypto present!" );
20
15
while (1 );
21
16
}
22
17
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): " );
25
20
lockConfirm.toLowerCase ();
26
21
27
22
if (lockConfirm != " y" ) {
28
23
Serial.println (" That's all folks" );
29
24
while (1 );
30
25
}
31
26
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!" );
34
29
while (1 );
35
30
}
36
31
37
- if (!ECCX08 .lock ()) {
38
- Serial.println (" Locking ECCX08 configuration failed!" );
32
+ if (!Crypto .lock ()) {
33
+ Serial.println (" Locking crypto configuration failed!" );
39
34
while (1 );
40
35
}
41
36
42
- Serial.println (" ECCX08 locked successfully" );
37
+ Serial.println (" crypto locked successfully" );
43
38
Serial.println ();
44
39
}
45
40
@@ -51,15 +46,20 @@ void setup() {
51
46
while (1 );
52
47
}
53
48
54
- if (!ECCX08Cert. beginCSR (keySlot, true )) {
49
+ if (!Certificate. begin ( )) {
55
50
Serial.println (" Error starting CSR generation!" );
56
51
while (1 );
57
52
}
58
53
59
54
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
+ }
61
61
62
- String csr = ECCX08Cert. endCSR ();
62
+ String csr = Certificate. getCSRPEM ();
63
63
64
64
if (!csr) {
65
65
Serial.println (" Error generating CSR!" );
@@ -79,52 +79,45 @@ void setup() {
79
79
String authorityKeyIdentifier = promptAndReadLine (" Please enter the certificates authority key identifier: " );
80
80
String signature = promptAndReadLine (" Please enter the certificates signature: " );
81
81
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];
86
85
87
- deviceId.getBytes (deviceIdBytes, sizeof (deviceIdBytes));
88
86
hexStringToBytes (serialNumber, serialNumberBytes, sizeof (serialNumberBytes));
89
87
hexStringToBytes (authorityKeyIdentifier, authorityKeyIdentifierBytes, sizeof (authorityKeyIdentifierBytes));
90
88
hexStringToBytes (signature, signatureBytes, sizeof (signatureBytes));
91
89
92
- if (!ECCX08. writeSlot (deviceIdSlot, deviceIdBytes, sizeof (deviceIdBytes) )) {
90
+ if (!Crypto. writeDeviceId (deviceId, CryptoSlot::DeviceId )) {
93
91
Serial.println (" Error storing device id!" );
94
92
while (1 );
95
93
}
96
94
97
- if (!ECCX08Cert. beginStorage (compressedCertSlot, serialNumberAndAuthorityKeyIdentifierSlot )) {
98
- Serial.println (" Error starting ECCX08 storage!" );
95
+ if (!Certificate. begin ( )) {
96
+ Serial.println (" Error starting crypto storage!" );
99
97
while (1 );
100
98
}
101
99
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!" );
113
116
while (1 );
114
117
}
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!" );
128
121
while (1 );
129
122
}
130
123
@@ -134,8 +127,8 @@ void setup() {
134
127
135
128
Serial.println (" Compressed cert = " );
136
129
137
- const byte* certData = ECCX08Cert .bytes ();
138
- int certLength = ECCX08Cert .length ();
130
+ const byte* certData = Certificate .bytes ();
131
+ int certLength = Certificate .length ();
139
132
140
133
for (int i = 0 ; i < certLength; i++) {
141
134
byte b = certData[i];
0 commit comments