Skip to content

Commit 617ba34

Browse files
author
Alberto Iannaccone
authored
Merge pull request #30 from arduino/vernemq-provisioning
Vernemq provisioning
2 parents e601a6d + 1cf0fd7 commit 617ba34

File tree

2 files changed

+43
-29
lines changed

2 files changed

+43
-29
lines changed

src/board-configuration.js

+20-8
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ export default class BoardConfiguration {
9090
partialMessage = '';
9191
this.daemon.writeSerial(board.port, 'y\n');
9292
}
93+
if (partialMessage.indexOf('Please enter the thing id:') !== -1) {
94+
partialMessage = '';
95+
this.daemon.writeSerial(board.port, `${board.deviceId}\n`);
96+
}
9397

9498
const begin = partialMessage.indexOf('-----BEGIN CERTIFICATE REQUEST-----');
9599
const end = partialMessage.indexOf('-----END CERTIFICATE REQUEST-----');
@@ -121,13 +125,13 @@ export default class BoardConfiguration {
121125
const notBefore = new Date(compressedCert.not_before);
122126
const notAfter = new Date(compressedCert.not_after);
123127
// eslint-disable-next-line prefer-template
124-
const answers = board.deviceId + '\n' +
125-
notBefore.getUTCFullYear() + '\n' +
128+
const answers = notBefore.getUTCFullYear() + '\n' +
126129
(notBefore.getUTCMonth() + 1) + '\n' +
127130
notBefore.getUTCDate() + '\n' +
128131
notBefore.getUTCHours() + '\n' +
129132
(notAfter.getUTCFullYear() - notBefore.getUTCFullYear()) + '\n' +
130133
compressedCert.serial + '\n' +
134+
compressedCert.authority_key_identifier + '\n' +
131135
compressedCert.signature + '\n';
132136
this.daemon.writeSerial(board.port, answers);
133137
});
@@ -168,7 +172,7 @@ export default class BoardConfiguration {
168172
* @param {Object} board contains the board data
169173
* @param {function} createDeviceCb used to create the device associated to the user
170174
*/
171-
configure(compiledSketch, board, createDeviceCb) {
175+
configure(compiledSketch, board, createDeviceCb, generateCertificateCb) {
172176
this.daemon.initUpload();
173177
this.configuring.next({ status: this.CONFIGURE_IN_PROGRESS, msg: 'Uploading provisioning sketch...' });
174178
if (!this.daemon.channelOpen.getValue()) {
@@ -196,7 +200,7 @@ export default class BoardConfiguration {
196200
this.daemon.uploadingDone.subscribe(() => {
197201
this.configuring.next({
198202
status: this.CONFIGURE_IN_PROGRESS,
199-
msg: 'Provisioning sketch uploaded successfully. Opening serial monitor...'
203+
msg: 'Provisioning sketch uploaded successfully. Creating device...'
200204
});
201205
this.daemon.serialMonitorOpened.pipe(takeUntil(this.daemon.serialMonitorOpened.pipe(filter(open => open))))
202206
.subscribe(() => {
@@ -208,14 +212,14 @@ export default class BoardConfiguration {
208212
.then(csr => {
209213
this.configuring.next({
210214
status: this.CONFIGURE_IN_PROGRESS,
211-
msg: 'CSR generated. Creating device...'
215+
msg: 'CSR generated. Generating certificate...'
212216
});
213-
return createDeviceCb(csr);
217+
return generateCertificateCb(csr);
214218
})
215219
.then(data => {
216220
this.configuring.next({
217221
status: this.CONFIGURE_IN_PROGRESS,
218-
msg: 'Device created. Storing certificate...'
222+
msg: 'Certificate generated. Storing certificate...'
219223
});
220224
return this.storeCertificate(data.compressed, board);
221225
})
@@ -234,7 +238,15 @@ export default class BoardConfiguration {
234238
err: error.toString()
235239
});
236240
});
237-
this.daemon.openSerialMonitor(board.port, BAUDRATE);
241+
createDeviceCb()
242+
.then(data => {
243+
this.configuring.next({
244+
status: this.CONFIGURE_IN_PROGRESS,
245+
msg: 'Device created. Opening serial monitor...'
246+
});
247+
board.deviceId = data.id; // eslint-disable-line no-param-reassign
248+
this.daemon.openSerialMonitor(board.port, BAUDRATE);
249+
});
238250
});
239251

240252
this.daemon.uploadingError.subscribe(upload => {

src/sketches/provisioning.ino.js

+23-21
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
}
@@ -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)