Skip to content

Commit 23e7f09

Browse files
authored
Merge pull request #307 from bcmi-labs/generic_crypto
ECCX08Cert and CryptoUtil: arrangements for the introduction of a new hardware crypto
2 parents 57a6cac + bc88de4 commit 23e7f09

File tree

9 files changed

+1334
-1191
lines changed

9 files changed

+1334
-1191
lines changed

examples/utility/Provisioning/Provisioning.ino

+46-53
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,40 @@
11
#include <ArduinoIoTCloud.h>
22
#include "ECCX08TLSConfig.h"
33

4-
#include <ArduinoECCX08.h>
5-
64
const 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

149
void 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];

src/ArduinoIoTCloudTCP.cpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -204,36 +204,36 @@ int ArduinoIoTCloudTCP::begin(bool const enable_watchdog, String brokerAddress,
204204
#endif /* OTA_ENABLED */
205205

206206
#ifdef BOARD_HAS_OFFLOADED_ECCX08
207-
if (!ECCX08.begin())
207+
if (!_crypto.begin())
208208
{
209-
DEBUG_ERROR("ECCX08.begin() failed.");
209+
DEBUG_ERROR("_crypto.begin() failed.");
210210
return 0;
211211
}
212-
if (!CryptoUtil::readDeviceId(ECCX08, getDeviceId(), ECCX08Slot::DeviceId))
212+
if (!_crypto.readDeviceId(getDeviceId(), CryptoSlot::DeviceId))
213213
{
214-
DEBUG_ERROR("CryptoUtil::readDeviceId(...) failed.");
214+
DEBUG_ERROR("_crypto.readDeviceId(...) failed.");
215215
return 0;
216216
}
217217
#endif
218218

219219
#ifdef BOARD_HAS_ECCX08
220-
if (!ECCX08.begin())
220+
if (!_crypto.begin())
221221
{
222222
DEBUG_ERROR("Cryptography processor failure. Make sure you have a compatible board.");
223223
return 0;
224224
}
225-
if (!CryptoUtil::readDeviceId(ECCX08, getDeviceId(), ECCX08Slot::DeviceId))
225+
if (!_crypto.readDeviceId(getDeviceId(), CryptoSlot::DeviceId))
226226
{
227227
DEBUG_ERROR("Cryptography processor read failure.");
228228
return 0;
229229
}
230-
if (!CryptoUtil::reconstructCertificate(_eccx08_cert, getDeviceId(), ECCX08Slot::Key, ECCX08Slot::CompressedCertificate, ECCX08Slot::SerialNumberAndAuthorityKeyIdentifier))
230+
if (!_crypto.readCert(_cert, CryptoSlot::CompressedCertificate))
231231
{
232232
DEBUG_ERROR("Cryptography certificate reconstruction failure.");
233233
return 0;
234234
}
235235
_sslClient.setClient(_connection->getClient());
236-
_sslClient.setEccSlot(static_cast<int>(ECCX08Slot::Key), _eccx08_cert.bytes(), _eccx08_cert.length());
236+
_sslClient.setEccSlot(static_cast<int>(CryptoSlot::Key), _cert.bytes(), _cert.length());
237237
#elif defined(BOARD_ESP)
238238
_sslClient.setInsecure();
239239
#endif

src/ArduinoIoTCloudTCP.h

+6-4
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@
2828

2929
#ifdef BOARD_HAS_ECCX08
3030
#include "tls/BearSSLClient.h"
31-
#include "tls/utility/ECCX08Cert.h"
31+
#include "tls/utility/CryptoUtil.h"
3232
#elif defined(BOARD_ESP)
3333
#include <WiFiClientSecure.h>
3434
#endif
3535

3636
#ifdef BOARD_HAS_OFFLOADED_ECCX08
37-
#include "tls/utility/ECCX08Cert.h"
37+
#include "tls/utility/CryptoUtil.h"
3838
#include <WiFiSSLClient.h>
3939
#endif
4040

@@ -133,11 +133,13 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass
133133
bool _mqtt_data_request_retransmit;
134134

135135
#if defined(BOARD_HAS_ECCX08)
136-
ECCX08CertClass _eccx08_cert;
136+
ArduinoIoTCloudCertClass _cert;
137137
BearSSLClient _sslClient;
138+
CryptoUtil _crypto;
138139
#elif defined(BOARD_HAS_OFFLOADED_ECCX08)
139-
ECCX08CertClass _eccx08_cert;
140+
ArduinoIoTCloudCertClass _cert;
140141
WiFiBearSSLClient _sslClient;
142+
CryptoUtil _crypto;
141143
#elif defined(BOARD_ESP)
142144
WiFiClientSecure _sslClient;
143145
String _password;

0 commit comments

Comments
 (0)