Skip to content

Commit 4f0b377

Browse files
authored
Merge pull request #106 from arduino/bearssl-ta
BearSSL: add command to override default TA
2 parents 70c24a4 + 6be15ad commit 4f0b377

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

arduino/libraries/ArduinoBearSSL/src/BearSSLClient.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class BearSSLClient : public Client {
4949

5050
inline void setClient(Client& client) { _client = &client; }
5151
inline Client* getClient() { return _client; }
52+
inline void setTrustAnchors(const br_x509_trust_anchor* myTAs, int myNumTAs) { _TAs = myTAs; _numTAs = myNumTAs; }
5253

5354

5455
virtual int connect(IPAddress ip, uint16_t port);

main/CommandHandler.cpp

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ WiFiServer tcpServers[MAX_SOCKETS];
5757

5858
WiFiClient bearssl_tcp_client;
5959
BearSSLClient bearsslClient(bearssl_tcp_client, ArduinoIoTCloudTrustAnchor, ArduinoIoTCloudTrustAnchor_NUM);
60+
br_x509_trust_anchor customTrustAnchor;
6061

6162
int setNet(const uint8_t command[], uint8_t response[])
6263
{
@@ -1618,6 +1619,63 @@ int downloadOTA(const uint8_t command[], uint8_t response[])
16181619
return 6;
16191620
}
16201621

1622+
int brSetECTrustAnchor(const uint8_t command[], uint8_t response[])
1623+
{
1624+
//[0] CMD_START < 0xE0 >
1625+
//[1] Command < 1 byte >
1626+
//[2] N args < 1 byte >
1627+
//[3] dn size < 1 byte MSB >
1628+
//[4] dn size < 1 byte LSB >
1629+
//[5] dn < dn size bytes >
1630+
//[[5] + 5] flags size < 1 byte >
1631+
//[[5] + 6] flags < 1 byte MSB >
1632+
//[[5] + 7] flags < 1 byte LSB >
1633+
//[[5] + 8] curve size < 1 byte >
1634+
//[[5] + 9] curve < 1 byte MSB >
1635+
//[[5] + 10] curve < 1 byte LSB >
1636+
//[[5] + 11] key size < 1 byte MSB >
1637+
//[[5] + 12] key size < 1 byte LSB >
1638+
//[[5] + 13] key < key size bytes >
1639+
1640+
if(customTrustAnchor.dn.data != NULL){
1641+
free(customTrustAnchor.dn.data);
1642+
}
1643+
1644+
if(customTrustAnchor.pkey.key.ec.q != NULL){
1645+
free(customTrustAnchor.pkey.key.ec.q);
1646+
}
1647+
1648+
response[2] = 1;
1649+
response[3] = 1;
1650+
response[4] = 0;
1651+
1652+
uint8_t dnSize = command[4];
1653+
customTrustAnchor.dn.data = (unsigned char*)malloc(dnSize);
1654+
if(customTrustAnchor.dn.data == NULL){
1655+
return 6;
1656+
}
1657+
memcpy(customTrustAnchor.dn.data, &command[5], dnSize);
1658+
customTrustAnchor.dn.len = dnSize;
1659+
1660+
customTrustAnchor.flags = command[7 + dnSize];
1661+
customTrustAnchor.pkey.key_type = BR_KEYTYPE_EC;
1662+
customTrustAnchor.pkey.key.ec.curve = command[10 + dnSize];
1663+
1664+
uint8_t keySize = command[12 + dnSize];
1665+
customTrustAnchor.pkey.key.ec.q = (unsigned char*)malloc(keySize);
1666+
if(customTrustAnchor.pkey.key.ec.q == NULL){
1667+
free(customTrustAnchor.dn.data);
1668+
return 6;
1669+
}
1670+
memcpy(customTrustAnchor.pkey.key.ec.q, &command[13 + dnSize], keySize);
1671+
customTrustAnchor.pkey.key.ec.qlen = keySize;
1672+
1673+
bearsslClient.setTrustAnchors(&customTrustAnchor, 1);
1674+
response[4] = 1;
1675+
1676+
return 6;
1677+
}
1678+
16211679
//
16221680
// Low-level BSD-like sockets functions
16231681
//
@@ -2094,7 +2152,7 @@ const CommandHandlerType commandHandlers[] = {
20942152
setPinMode, setDigitalWrite, setAnalogWrite, getDigitalRead, getAnalogRead, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
20952153

20962154
// 0x60 -> 0x6f
2097-
writeFile, readFile, deleteFile, existsFile, downloadFile, applyOTA, renameFile, downloadOTA, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
2155+
writeFile, readFile, deleteFile, existsFile, downloadFile, applyOTA, renameFile, downloadOTA, brSetECTrustAnchor, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
20982156

20992157
// Low-level BSD-like sockets functions.
21002158
// 0x70 -> 0x7f

0 commit comments

Comments
 (0)