Skip to content

Commit 7342403

Browse files
author
Alberto Iannaccone
committed
add authority key identifier and put the thing id in the subject common name
1 parent e601a6d commit 7342403

File tree

1 file changed

+25
-23
lines changed

1 file changed

+25
-23
lines changed

src/sketches/provisioning.ino.js

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ export const provisioningSketch = {
1313
#include <ArduinoBearSSL.h>
1414
#include <ArduinoECCX08.h>
1515
16-
const int keySlot = 0;
17-
const int compressedCertSlot = 10;
18-
const int serialNumberSlot = 11;
19-
const int thingIdSlot = 12;
16+
const int keySlot = 0;
17+
const int compressedCertSlot = 10;
18+
const int serialNumberAndAuthorityKeyIdentifierSlot = 11;
19+
const int thingIdSlot = 12;
2020
2121
void setup() {
2222
Serial.begin(9600);
@@ -63,7 +63,8 @@ void setup() {
6363
while (1);
6464
}
6565
66-
ECCX08Cert.setSubjectCommonName(ECCX08.serialNumber());
66+
String thingId = promptAndReadLine("Please enter the thing id: ");
67+
ECCX08Cert.setSubjectCommonName(thingId);
6768
6869
String csr = ECCX08Cert.endCSR();
6970
@@ -76,37 +77,37 @@ void setup() {
7677
Serial.println();
7778
Serial.println(csr);
7879
79-
String thingId = promptAndReadLine("Please enter the thing id: ");
80-
String issueYear = promptAndReadLine("Please enter the issue year of the certificate (2000 - 2031): ");
81-
String issueMonth = promptAndReadLine("Please enter the issue month of the certificate (1 - 12): ");
82-
String issueDay = promptAndReadLine("Please enter the issue day of the certificate (1 - 31): ");
83-
String issueHour = promptAndReadLine("Please enter the issue hour of the certificate (0 - 23): ");
84-
String expireYears = promptAndReadLine("Please enter how many years the certificate is valid for (0 - 31): ");
85-
String serialNumber = promptAndReadLine("Please enter the certificates serial number: ");
86-
String signature = promptAndReadLine("Please enter the certificates signature: ");
87-
88-
serialNumber.toUpperCase();
89-
signature.toUpperCase();
80+
String issueYear = promptAndReadLine("Please enter the issue year of the certificate (2000 - 2031): ");
81+
String issueMonth = promptAndReadLine("Please enter the issue month of the certificate (1 - 12): ");
82+
String issueDay = promptAndReadLine("Please enter the issue day of the certificate (1 - 31): ");
83+
String issueHour = promptAndReadLine("Please enter the issue hour of the certificate (0 - 23): ");
84+
String expireYears = promptAndReadLine("Please enter how many years the certificate is valid for (0 - 31): ");
85+
String serialNumber = promptAndReadLine("Please enter the certificates serial number: ");
86+
String authorityKeyIdentifier = promptAndReadLine("Please enter the certificates authority key identifier: ");
87+
String signature = promptAndReadLine("Please enter the certificates signature: ");
9088
9189
byte thingIdBytes[72];
9290
byte serialNumberBytes[16];
91+
byte authorityKeyIdentifierBytes[20];
9392
byte signatureBytes[64];
9493
9594
thingId.getBytes(thingIdBytes, sizeof(thingIdBytes));
9695
hexStringToBytes(serialNumber, serialNumberBytes, sizeof(serialNumberBytes));
97-
hexStringToBytes(signature, signatureBytes, 64);
96+
hexStringToBytes(authorityKeyIdentifier, authorityKeyIdentifierBytes, sizeof(authorityKeyIdentifierBytes));
97+
hexStringToBytes(signature, signatureBytes, sizeof(signatureBytes));
9898
9999
if (!ECCX08.writeSlot(thingIdSlot, thingIdBytes, sizeof(thingIdBytes))) {
100100
Serial.println("Error storing thing id!");
101101
while (1);
102102
}
103103
104-
if (!ECCX08Cert.beginStorage(compressedCertSlot, serialNumberSlot)) {
104+
if (!ECCX08Cert.beginStorage(compressedCertSlot, serialNumberAndAuthorityKeyIdentifierSlot)) {
105105
Serial.println("Error starting ECCX08 storage!");
106106
while (1);
107107
}
108108
109109
ECCX08Cert.setSignature(signatureBytes);
110+
ECCX08Cert.setAuthorityKeyIdentifier(authorityKeyIdentifierBytes);
110111
ECCX08Cert.setSerialNumber(serialNumberBytes);
111112
ECCX08Cert.setIssueYear(issueYear.toInt());
112113
ECCX08Cert.setIssueMonth(issueMonth.toInt());
@@ -119,7 +120,7 @@ void setup() {
119120
while (1);
120121
}
121122
122-
if (!ECCX08Cert.beginReconstruction(keySlot, compressedCertSlot, serialNumberSlot)) {
123+
if (!ECCX08Cert.beginReconstruction(keySlot, compressedCertSlot, serialNumberAndAuthorityKeyIdentifierSlot)) {
123124
Serial.println("Error starting ECCX08 cert reconstruction!");
124125
while (1);
125126
}
@@ -168,9 +169,9 @@ String readLine() {
168169
if (Serial.available()) {
169170
char c = Serial.read();
170171
171-
if (c == '\\r') {
172+
if (c == '\r') {
172173
// ignore
173-
} else if (c == '\\n') {
174+
} else if (c == '\n') {
174175
break;
175176
}
176177
@@ -183,8 +184,9 @@ String readLine() {
183184
return line;
184185
}
185186
186-
void hexStringToBytes(const String& in, byte out[], int length) {
187+
void hexStringToBytes(String& in, byte out[], int length) {
187188
int inLength = in.length();
189+
in.toUpperCase();
188190
int outLength = 0;
189191
190192
for (int i = 0; i < inLength && outLength < length; i += 2) {
@@ -194,7 +196,7 @@ void hexStringToBytes(const String& in, byte out[], int length) {
194196
byte highByte = (highChar <= '9') ? (highChar - '0') : (highChar + 10 - 'A');
195197
byte lowByte = (lowChar <= '9') ? (lowChar - '0') : (lowChar + 10 - 'A');
196198
197-
out[outLength++] = (highByte << 4) | lowByte;
199+
out[outLength++] = (highByte << 4) | (lowByte & 0xF);
198200
}
199201
}
200202
`

0 commit comments

Comments
 (0)