Skip to content

Commit 46a40fc

Browse files
committed
Add setEccSlot API to set private key slot and cert bytes
instead or in constructor
1 parent 3ab7146 commit 46a40fc

File tree

3 files changed

+51
-23
lines changed

3 files changed

+51
-23
lines changed

Diff for: examples/WiFi101_AWS_IoT/WiFi101_AWS_IoT.ino

+38-10
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#include <WiFi101.h>
1414
#include <MQTTClient.h>
1515
#include <ArduinoBearSSL.h>
16+
#include <utility/ECC508.h>
17+
#include <utility/ECC508Cert.h>
1618

1719
// ssid and pass are the wifi settings
1820
const char ssid[] = "XXX";
@@ -22,19 +24,16 @@ const char pass[] = "XXX";
2224
const char server[] = "xxxxxxxxxxxxxx.iot.xx-xxxx-x.amazonaws.com";
2325

2426
// id is the ThingName in aws IOT
25-
const char id[] = "XXX"
27+
const String id = "XXX";
2628

2729
// Get the cert data by:
2830
// 1) Creating a CSR using the ArduinoBearSSL -> Tools -> ECC508CSR example for key slot 0
29-
// 2) Creating a new thing and uploading the CSR for it
30-
// 3) Downloading the public key for the thing in AWS IoT
31-
// 4) Convert the base64 encoded cert to binary
32-
const byte cert[] = {
33-
// ...
34-
};
31+
// 2) Use the "Go tool" to generate a public cert from the CSR
32+
// 3) Store the cert params in 1)
33+
// 4) Activate the cert in AWS IoT and attach policy + thing
3534

3635
WiFiClient wifiClient;
37-
BearSSLClient net(wifiClient, 0, cert, sizeof(cert));
36+
BearSSLClient net(wifiClient);
3837
MQTTClient client;
3938

4039
unsigned long lastMillis = 0;
@@ -45,8 +44,37 @@ unsigned long getTime() {
4544

4645
void setup() {
4746
Serial.begin(115200);
47+
while (!Serial);
48+
49+
if (!ECC508.begin()) {
50+
Serial.println("No ECC508 present!");
51+
while (1);
52+
}
4853

4954
ArduinoBearSSL.onGetTime(getTime);
55+
56+
ECC508Cert.begin(0, 9, 10);
57+
ECC508Cert.setIssuerCountryName("US");
58+
ECC508Cert.setIssuerOrganizationName("Arduino LLC US");
59+
ECC508Cert.setIssuerOrganizationalUnitName("IT");
60+
ECC508Cert.setIssuerCommonName("Arduino");
61+
ECC508Cert.setSubjectCommonName(ECC508.serialNumber());
62+
ECC508Cert.uncompress();
63+
64+
const byte* certData = ECC508Cert.bytes();
65+
int certLength = ECC508Cert.length();
66+
67+
for (int i = 0; i < certLength; i++) {
68+
byte b = certData[i];
69+
70+
if (b < 16) {
71+
Serial.print('0');
72+
}
73+
Serial.print(b, HEX);
74+
}
75+
Serial.println();
76+
77+
net.setEccSlot(0, ECC508Cert.bytes(), ECC508Cert.length());
5078

5179
WiFi.begin(ssid, pass);
5280

@@ -68,7 +96,7 @@ void connect() {
6896
}
6997

7098
Serial.print("\nconnecting...");
71-
while (!client.connect(id)) {
99+
while (!client.connect(id.c_str())) {
72100
Serial.print(".");
73101
delay(1000);
74102
}
@@ -94,4 +122,4 @@ void loop() {
94122

95123
void messageReceived(String &topic, String &payload) {
96124
Serial.println("incoming: " + topic + " - " + payload);
97-
}
125+
}

Diff for: src/BearSSLClient.cpp

+11-12
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,6 @@ BearSSLClient::BearSSLClient(Client& client) :
1717
_ecCert.data_len = 0;
1818
}
1919

20-
BearSSLClient::BearSSLClient(Client& client, int ecc508KeySlot, const byte cert[], int certLength) :
21-
BearSSLClient(client)
22-
{
23-
// HACK: put the key slot info. in the br_ec_private_key structure
24-
_ecKey.curve = 23;
25-
_ecKey.x = (unsigned char*)ecc508KeySlot;
26-
_ecKey.xlen = 32;
27-
28-
_ecCert.data = (unsigned char*)cert;
29-
_ecCert.data_len = certLength;
30-
}
31-
3220
BearSSLClient::~BearSSLClient()
3321
{
3422
}
@@ -157,6 +145,17 @@ BearSSLClient::operator bool()
157145
return (*_client);
158146
}
159147

148+
void BearSSLClient::setEccSlot(int ecc508KeySlot, const byte cert[], int certLength)
149+
{
150+
// HACK: put the key slot info. in the br_ec_private_key structure
151+
_ecKey.curve = 23;
152+
_ecKey.x = (unsigned char*)ecc508KeySlot;
153+
_ecKey.xlen = 32;
154+
155+
_ecCert.data = (unsigned char*)cert;
156+
_ecCert.data_len = certLength;
157+
}
158+
160159
int BearSSLClient::connectSSL(const char* host)
161160
{
162161
/*

Diff for: src/BearSSLClient.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ class BearSSLClient : public Client {
1010

1111
public:
1212
BearSSLClient(Client& client);
13-
BearSSLClient(Client& client, int ecc508KeySlot, const byte cert[], int certLength);
1413
virtual ~BearSSLClient();
1514

1615
virtual int connect(IPAddress ip, uint16_t port);
@@ -28,6 +27,8 @@ class BearSSLClient : public Client {
2827

2928
using Print::write;
3029

30+
void setEccSlot(int ecc508KeySlot, const byte cert[], int certLength);
31+
3132
private:
3233
int connectSSL(const char* host);
3334
static int clientRead(void *ctx, unsigned char *buf, size_t len);

0 commit comments

Comments
 (0)