Skip to content

Commit 50181e7

Browse files
committed
Combine certificate serial number and authority key identifier data into one slot
1 parent 1e3b023 commit 50181e7

File tree

4 files changed

+47
-64
lines changed

4 files changed

+47
-64
lines changed

examples/utility/Provisioning/Provisioning.ino

+6-7
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@
55
#include <ArduinoBearSSL.h>
66
#include <ArduinoECCX08.h>
77

8-
const int keySlot = 0;
9-
const int compressedCertSlot = 10;
10-
const int serialNumberSlot = 11;
11-
const int authorityKeyIdentifierSlot = 12;
12-
const int thingIdSlot = 13;
8+
const int keySlot = 0;
9+
const int compressedCertSlot = 10;
10+
const int serialNumberAndAuthorityKeyIdentifierSlot = 11;
11+
const int thingIdSlot = 12;
1312

1413
void setup() {
1514
Serial.begin(9600);
@@ -97,7 +96,7 @@ void setup() {
9796
while (1);
9897
}
9998

100-
if (!ECCX08Cert.beginStorage(compressedCertSlot, serialNumberSlot, authorityKeyIdentifierSlot)) {
99+
if (!ECCX08Cert.beginStorage(compressedCertSlot, serialNumberAndAuthorityKeyIdentifierSlot)) {
101100
Serial.println("Error starting ECCX08 storage!");
102101
while (1);
103102
}
@@ -116,7 +115,7 @@ void setup() {
116115
while (1);
117116
}
118117

119-
if (!ECCX08Cert.beginReconstruction(keySlot, compressedCertSlot, serialNumberSlot, authorityKeyIdentifierSlot)) {
118+
if (!ECCX08Cert.beginReconstruction(keySlot, compressedCertSlot, serialNumberAndAuthorityKeyIdentifierSlot)) {
120119
Serial.println("Error starting ECCX08 cert reconstruction!");
121120
while (1);
122121
}

src/ArduinoCloud.cpp

+5-6
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@
77

88
const static char server[] = "a19g5nbe27wn47.iot.us-east-1.amazonaws.com"; //"xxxxxxxxxxxxxx.iot.xx-xxxx-x.amazonaws.com";
99

10-
const static int keySlot = 0;
11-
const static int compressedCertSlot = 10;
12-
const static int serialNumberSlot = 11;
13-
const static int authorityKeyIdentifierSlot = 12;
14-
const static int thingIdSlot = 13;
10+
const static int keySlot = 0;
11+
const static int compressedCertSlot = 10;
12+
const static int serialNumberAndAuthorityKeyIdentifierSlot = 11;
13+
const static int thingIdSlot = 12;
1514

1615
ArduinoCloudClass::ArduinoCloudClass() :
1716
_bearSslClient(NULL),
@@ -39,7 +38,7 @@ int ArduinoCloudClass::begin(Client& net)
3938
}
4039
_id = (char*)thingIdBytes;
4140

42-
if (!ECCX08Cert.beginReconstruction(keySlot, compressedCertSlot, serialNumberSlot, authorityKeyIdentifierSlot)) {
41+
if (!ECCX08Cert.beginReconstruction(keySlot, compressedCertSlot, serialNumberAndAuthorityKeyIdentifierSlot)) {
4342
return 0;
4443
}
4544

src/utility/ECCX08Cert.cpp

+32-46
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ struct __attribute__((__packed__)) CompressedCert {
2121
#define SERIAL_NUMBER_LENGTH 16
2222
#define AUTHORITY_KEY_IDENTIFIER_LENGTH 20
2323

24+
struct __attribute__((__packed__)) SerialNumberAndAuthorityKeyIdentifier {
25+
byte serialNumber[SERIAL_NUMBER_LENGTH];
26+
byte authorityKeyIdentifier[AUTHORITY_KEY_IDENTIFIER_LENGTH];
27+
};
28+
2429
static String base64Encode(const byte in[], unsigned int length, const char* prefix, const char* suffix)
2530
{
2631
static const char* CODES = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
@@ -73,8 +78,7 @@ static String base64Encode(const byte in[], unsigned int length, const char* pre
7378
ECCX08CertClass::ECCX08CertClass() :
7479
_keySlot(-1),
7580
_compressedCertSlot(-1),
76-
_serialNumberSlot(-1),
77-
_authorityKeyIdentifierSlot(-1),
81+
_serialNumberAndAuthorityKeyIdentifierSlot(-1),
7882
_bytes(NULL),
7983
_length(0)
8084
{
@@ -186,25 +190,18 @@ String ECCX08CertClass::endCSR()
186190
return base64Encode(csr, csrLen + csrHeaderLen, "-----BEGIN CERTIFICATE REQUEST-----\n", "\n-----END CERTIFICATE REQUEST-----\n");
187191
}
188192

189-
int ECCX08CertClass::beginStorage(int compressedCertSlot, int serialNumberSlot, int authorityKeyIdentifierSlot)
193+
int ECCX08CertClass::beginStorage(int compressedCertSlot, int serialNumberAndAuthorityKeyIdentifierSlot)
190194
{
191195
if (compressedCertSlot < 8 || compressedCertSlot > 15) {
192196
return 0;
193197
}
194198

195-
if (serialNumberSlot < 8 || serialNumberSlot > 15) {
199+
if (serialNumberAndAuthorityKeyIdentifierSlot < 8 || serialNumberAndAuthorityKeyIdentifierSlot > 15) {
196200
return 0;
197201
}
198202

199-
if (authorityKeyIdentifierSlot > -1) {
200-
if (authorityKeyIdentifierSlot < 8 || authorityKeyIdentifierSlot > 15) {
201-
return 0;
202-
}
203-
}
204-
205203
_compressedCertSlot = compressedCertSlot;
206-
_serialNumberSlot = serialNumberSlot;
207-
_authorityKeyIdentifierSlot = authorityKeyIdentifierSlot;
204+
_serialNumberAndAuthorityKeyIdentifierSlot = serialNumberAndAuthorityKeyIdentifierSlot;
208205

209206
memset(_temp, 0x00, sizeof(_temp));
210207

@@ -280,18 +277,14 @@ int ECCX08CertClass::endStorage()
280277
return 0;
281278
}
282279

283-
if (!ECCX08.writeSlot(_serialNumberSlot, &_temp[72], SERIAL_NUMBER_LENGTH)) {
284-
return 0;
285-
}
286-
287-
if (!ECCX08.writeSlot(_authorityKeyIdentifierSlot, &_temp[88], AUTHORITY_KEY_IDENTIFIER_LENGTH)) {
280+
if (!ECCX08.writeSlot(_serialNumberAndAuthorityKeyIdentifierSlot, &_temp[72], SERIAL_NUMBER_LENGTH + AUTHORITY_KEY_IDENTIFIER_LENGTH)) {
288281
return 0;
289282
}
290283

291284
return 1;
292285
}
293286

294-
int ECCX08CertClass::beginReconstruction(int keySlot, int compressedCertSlot, int serialNumberSlot, int authorityKeyIdentifierSlot)
287+
int ECCX08CertClass::beginReconstruction(int keySlot, int compressedCertSlot, int serialNumberAndAuthorityKeyIdentifierSlot)
295288
{
296289
if (keySlot < 0 || keySlot > 8) {
297290
return 0;
@@ -301,20 +294,13 @@ int ECCX08CertClass::beginReconstruction(int keySlot, int compressedCertSlot, in
301294
return 0;
302295
}
303296

304-
if (serialNumberSlot < 8 || serialNumberSlot > 15) {
297+
if (serialNumberAndAuthorityKeyIdentifierSlot < 8 || serialNumberAndAuthorityKeyIdentifierSlot > 15) {
305298
return 0;
306299
}
307300

308-
if (authorityKeyIdentifierSlot > -1) {
309-
if (authorityKeyIdentifierSlot < 8 || authorityKeyIdentifierSlot > 15) {
310-
return 0;
311-
}
312-
}
313-
314301
_keySlot = keySlot;
315302
_compressedCertSlot = compressedCertSlot;
316-
_serialNumberSlot = serialNumberSlot;
317-
_authorityKeyIdentifierSlot = authorityKeyIdentifierSlot;
303+
_serialNumberAndAuthorityKeyIdentifierSlot = serialNumberAndAuthorityKeyIdentifierSlot;
318304

319305
return 1;
320306
}
@@ -323,8 +309,7 @@ int ECCX08CertClass::endReconstruction()
323309
{
324310
byte publicKey[64];
325311
struct CompressedCert compressedCert;
326-
byte serialNumber[SERIAL_NUMBER_LENGTH];
327-
byte authorityKeyIdentifier[AUTHORITY_KEY_IDENTIFIER_LENGTH];
312+
struct SerialNumberAndAuthorityKeyIdentifier serialNumberAndAuthorityKeyIdentifier;
328313

329314
if (!ECCX08.generatePublicKey(_keySlot, publicKey)) {
330315
return 0;
@@ -334,16 +319,11 @@ int ECCX08CertClass::endReconstruction()
334319
return 0;
335320
}
336321

337-
if (!ECCX08.readSlot(_serialNumberSlot, serialNumber, sizeof(serialNumber))) {
322+
if (!ECCX08.readSlot(_serialNumberAndAuthorityKeyIdentifierSlot, (byte*)&serialNumberAndAuthorityKeyIdentifier, sizeof(serialNumberAndAuthorityKeyIdentifier))) {
338323
return 0;
339324
}
340325

341-
if (_authorityKeyIdentifierSlot > -1 &&
342-
!ECCX08.readSlot(_authorityKeyIdentifierSlot, authorityKeyIdentifier, sizeof(authorityKeyIdentifier))) {
343-
return 0;
344-
}
345-
346-
int serialNumberLen = serialNumberLength(serialNumber);
326+
int serialNumberLen = serialNumberLength(serialNumberAndAuthorityKeyIdentifier.serialNumber);
347327

348328
int issuerLen = issuerOrSubjectLength(_issuerCountryName,
349329
_issuerStateProvinceName,
@@ -365,12 +345,8 @@ int ECCX08CertClass::endReconstruction()
365345

366346
int publicKeyLen = publicKeyLength();
367347

368-
int authorityKeyIdentifierLen = 0;
369-
370-
if (_authorityKeyIdentifierSlot > -1) {
371-
authorityKeyIdentifierLen = authorityKeyIdentifierLength();
372-
}
373-
348+
int authorityKeyIdentifierLen = authorityKeyIdentifierLength(serialNumberAndAuthorityKeyIdentifier.authorityKeyIdentifier);
349+
374350
int signatureLen = signatureLength(compressedCert.signature);
375351

376352
int certInfoLen = 5 + serialNumberLen + 12 + issuerHeaderLen + issuerLen + 32 +
@@ -411,7 +387,7 @@ int ECCX08CertClass::endReconstruction()
411387
*out++ = 0x02;
412388

413389
// serial number
414-
appendSerialNumber(serialNumber, out);
390+
appendSerialNumber(serialNumberAndAuthorityKeyIdentifier.serialNumber, out);
415391
out += serialNumberLen;
416392

417393
// ecdsaWithSHA256
@@ -456,7 +432,7 @@ int ECCX08CertClass::endReconstruction()
456432
out += publicKeyLen;
457433

458434
if (authorityKeyIdentifierLen) {
459-
appendAuthorityKeyIdentifier(authorityKeyIdentifier, out);
435+
appendAuthorityKeyIdentifier(serialNumberAndAuthorityKeyIdentifier.authorityKeyIdentifier, out);
460436
out += authorityKeyIdentifierLen;
461437
} else {
462438
// null sequence
@@ -595,9 +571,19 @@ int ECCX08CertClass::publicKeyLength()
595571
return (2 + 2 + 9 + 10 + 4 + 64);
596572
}
597573

598-
int ECCX08CertClass::authorityKeyIdentifierLength()
574+
int ECCX08CertClass::authorityKeyIdentifierLength(const byte authorityKeyIdentifier[])
599575
{
600-
return 37;
576+
bool set = false;
577+
578+
// check if the authority key identifier is non-zero
579+
for (int i = 0; i < AUTHORITY_KEY_IDENTIFIER_LENGTH; i++) {
580+
if (authorityKeyIdentifier[i] != 0) {
581+
set = true;
582+
break;
583+
}
584+
}
585+
586+
return (set ? 37 : 0);
601587
}
602588

603589
int ECCX08CertClass::signatureLength(const byte signature[])

src/utility/ECCX08Cert.h

+4-5
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class ECCX08CertClass {
1212
int beginCSR(int keySlot, bool newPrivateKey = true);
1313
String endCSR();
1414

15-
int beginStorage(int compressedCertSlot, int serialNumberSlot, int authorityKeyIdentifierSlot);
15+
int beginStorage(int compressedCertSlot, int serialNumberAndAuthorityKeyIdentifierSlot);
1616
void setSignature(byte signature[]);
1717
void setIssueYear(int issueYear);
1818
void setIssueMonth(int issueMonth);
@@ -23,7 +23,7 @@ class ECCX08CertClass {
2323
void setAuthorityKeyIdentifier(const byte authorityKeyIdentifier[]);
2424
int endStorage();
2525

26-
int beginReconstruction(int keySlot, int compressedCertSlot, int serialNumberSlot, int authorityKeyIdentifierSlot);
26+
int beginReconstruction(int keySlot, int compressedCertSlot, int serialNumberAndAuthorityKeyIdentifierSlot);
2727
int endReconstruction();
2828

2929
byte* bytes();
@@ -55,7 +55,7 @@ class ECCX08CertClass {
5555

5656
int publicKeyLength();
5757

58-
int authorityKeyIdentifierLength();
58+
int authorityKeyIdentifierLength(const byte authorityKeyIdentifier[]);
5959

6060
int signatureLength(const byte signature[]);
6161

@@ -92,8 +92,7 @@ class ECCX08CertClass {
9292
private:
9393
int _keySlot;
9494
int _compressedCertSlot;
95-
int _serialNumberSlot;
96-
int _authorityKeyIdentifierSlot;
95+
int _serialNumberAndAuthorityKeyIdentifierSlot;
9796

9897
String _issuerCountryName;
9998
String _issuerStateProvinceName;

0 commit comments

Comments
 (0)