Skip to content

Commit 64d84a9

Browse files
committed
Store thing id in a slot, make serial number 16 bytes
1 parent c40bd42 commit 64d84a9

File tree

6 files changed

+29
-14
lines changed

6 files changed

+29
-14
lines changed

examples/MKR1000_Cloud_Blink/MKR1000_Cloud_Blink.ino

+1-3
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ int status = WL_IDLE_STATUS; // the WiFi radio's status
99

1010
WiFiClient wifiClient;
1111

12-
const char thingId[] = "blah";
13-
1412
unsigned long getTime() {
1513
return WiFi.getTime();
1614
}
@@ -29,7 +27,7 @@ void setup() {
2927
while (true);
3028
}
3129

32-
if (!ArduinoCloud.begin(wifiClient, thingId)) {
30+
if (!ArduinoCloud.begin(wifiClient)) {
3331
Serial.println("Starting Arduino Cloud failed!");
3432
while (true);
3533
}

examples/utility/Provisioning/Provisioning.ino

+10-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
const int keySlot = 0;
99
const int compressedCertSlot = 10;
1010
const int serialNumberSlot = 11;
11+
const int thingIdSlot = 12;
1112

1213
void setup() {
1314
Serial.begin(9600);
@@ -53,6 +54,7 @@ void setup() {
5354
Serial.println();
5455
Serial.println(csr);
5556

57+
String thingId = promptAndReadLine("Please enter the thing id: ");
5658
String issueYear = promptAndReadLine("Please enter the issue year of the certificate (2000 - 2031): ");
5759
String issueMonth = promptAndReadLine("Please enter the issue month of the certificate (1 - 12): ");
5860
String issueDay = promptAndReadLine("Please enter the issue day of the certificate (1 - 31): ");
@@ -64,12 +66,19 @@ void setup() {
6466
serialNumber.toUpperCase();
6567
signature.toUpperCase();
6668

67-
byte serialNumberBytes[72];
69+
byte thingIdBytes[72];
70+
byte serialNumberBytes[16];
6871
byte signatureBytes[64];
6972

73+
thingId.getBytes(thingIdBytes, sizeof(thingIdBytes));
7074
hexStringToBytes(serialNumber, serialNumberBytes, sizeof(serialNumberBytes));
7175
hexStringToBytes(signature, signatureBytes, 64);
7276

77+
if (!ECCX08.writeSlot(thingIdSlot, thingIdBytes, sizeof(thingIdBytes))) {
78+
Serial.println("Error storing thing id!");
79+
while (1);
80+
}
81+
7382
if (!ECCX08Cert.beginStorage(compressedCertSlot, serialNumberSlot)) {
7483
Serial.println("Error starting ECCX08 storage!");
7584
while (1);

src/ArduinoCloud.cpp

+9-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const static char server[] = "a19g5nbe27wn47.iot.eu-west-1.amazonaws.com"; //"xx
1010
const static int keySlot = 0;
1111
const static int compressedCertSlot = 10;
1212
const static int serialNumberSlot = 11;
13+
const static int thingIdSlot = 12;
1314

1415
ArduinoCloudClass::ArduinoCloudClass() :
1516
_bearSslClient(NULL),
@@ -24,12 +25,19 @@ ArduinoCloudClass::~ArduinoCloudClass()
2425
}
2526
}
2627

27-
int ArduinoCloudClass::begin(Client& net, const String& id)
28+
int ArduinoCloudClass::begin(Client& net)
2829
{
30+
byte thingIdBytes[72];
31+
2932
if (!ECCX08.begin()) {
3033
return 0;
3134
}
3235

36+
if (!ECCX08.readSlot(thingIdSlot, thingIdBytes, sizeof(thingIdBytes))) {
37+
return 0;
38+
}
39+
_id = (char*)thingIdBytes;
40+
3341
if (!ECCX08Cert.beginReconstruction(keySlot, compressedCertSlot, serialNumberSlot)) {
3442
return 0;
3543
}
@@ -53,8 +61,6 @@ int ArduinoCloudClass::begin(Client& net, const String& id)
5361
_mqttClient.onMessageAdvanced(ArduinoCloudClass::onMessage);
5462
_mqttClient.begin(server, 8883, *_bearSslClient);
5563

56-
_id = id;
57-
5864
_stdoutTopic = "$aws/things/" + _id + "/stdout";
5965
_stdinTopic = "$aws/things/" + _id + "/stdin";
6066

src/ArduinoCloudV2.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class ArduinoCloudClass {
1212
ArduinoCloudClass();
1313
~ArduinoCloudClass();
1414

15-
int begin(Client& net, const String& id);
15+
int begin(Client& net);
1616

1717
int connect();
1818

src/utility/ECCX08Cert.cpp

+7-5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ struct __attribute__((__packed__)) CompressedCert {
1818
byte unused[5];
1919
};
2020

21+
#define SERIAL_NUMBER_LENGTH 16
22+
2123
static String base64Encode(const byte in[], unsigned int length, const char* prefix, const char* suffix)
2224
{
2325
static const char* CODES = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
@@ -255,7 +257,7 @@ void ECCX08CertClass::setExpireYears(int expireYears)
255257

256258
void ECCX08CertClass::setSerialNumber(byte serialNumber[])
257259
{
258-
memcpy(&_temp[72], serialNumber, 72);
260+
memcpy(&_temp[72], serialNumber, SERIAL_NUMBER_LENGTH);
259261
}
260262

261263
int ECCX08CertClass::endStorage()
@@ -264,7 +266,7 @@ int ECCX08CertClass::endStorage()
264266
return 0;
265267
}
266268

267-
if (!ECCX08.writeSlot(_serialNumberSlot, &_temp[72], 72)) {
269+
if (!ECCX08.writeSlot(_serialNumberSlot, &_temp[72], SERIAL_NUMBER_LENGTH)) {
268270
return 0;
269271
}
270272

@@ -296,7 +298,7 @@ int ECCX08CertClass::endReconstruction()
296298
{
297299
byte publicKey[64];
298300
struct CompressedCert compressedCert;
299-
byte serialNumber[72];
301+
byte serialNumber[SERIAL_NUMBER_LENGTH];
300302

301303
if (!ECCX08.generatePublicKey(_keySlot, publicKey)) {
302304
return 0;
@@ -575,7 +577,7 @@ int ECCX08CertClass::signatureLength(const byte signature[])
575577

576578
int ECCX08CertClass::serialNumberLength(const byte serialNumber[])
577579
{
578-
int length = 72;
580+
int length = SERIAL_NUMBER_LENGTH;
579581

580582
while (*serialNumber == 0 && length) {
581583
serialNumber++;
@@ -752,7 +754,7 @@ void ECCX08CertClass::appendSignature(const byte signature[], byte out[])
752754

753755
void ECCX08CertClass::appendSerialNumber(const byte serialNumber[], byte out[])
754756
{
755-
int length = 72;
757+
int length = SERIAL_NUMBER_LENGTH;
756758

757759
while (*serialNumber == 0 && length) {
758760
serialNumber++;

src/utility/ECCX08Cert.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ class ECCX08CertClass {
103103
String _subjectOrganizationalUnitName;
104104
String _subjectCommonName;
105105

106-
byte _temp[144];
106+
byte _temp[88];
107107
byte* _bytes;
108108
int _length;
109109
};

0 commit comments

Comments
 (0)