Skip to content

Commit 070f84d

Browse files
authored
Merge pull request #292 from pennam/ta-override
Add command to override BearSSL TrustAnchor
2 parents 2a19b6f + a19dfe7 commit 070f84d

File tree

7 files changed

+64
-1
lines changed

7 files changed

+64
-1
lines changed

src/WiFiClient.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,11 @@ int WiFiClient::connectBearSSL(const char *host, uint16_t port)
194194
return 1;
195195
}
196196

197+
int WiFiClient::setECTrustAnchorBearSSL(const uint8_t *dName, uint32_t dNameSize, uint16_t flags, uint16_t curve, const uint8_t *key, uint32_t keySize)
198+
{
199+
return ServerDrv::setECTrustAnchorBearSSL(dName, dNameSize, flags, curve, key, keySize);
200+
}
201+
197202
size_t WiFiClient::write(uint8_t b) {
198203
return write(&b, 1);
199204
}

src/WiFiClient.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class WiFiClient : public Client {
3838
virtual int connectSSL(const char *host, uint16_t port);
3939
virtual int connectBearSSL(IPAddress ip, uint16_t port);
4040
virtual int connectBearSSL(const char *host, uint16_t port);
41+
virtual int setECTrustAnchorBearSSL(const uint8_t *dName, uint32_t dNameSize, uint16_t flags, uint16_t curve, const uint8_t *key, uint32_t keySize);
4142
virtual size_t write(uint8_t);
4243
virtual size_t write(const uint8_t *buf, size_t size);
4344
virtual size_t retry(const uint8_t *buf, size_t size, bool write);

src/WiFiSSLClient.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,8 @@ int WiFiBearSSLClient::connect(const char* host, uint16_t port)
5858
{
5959
return WiFiClient::connectBearSSL(host, port);
6060
}
61+
62+
int WiFiBearSSLClient::setECTrustAnchor(const uint8_t *dName, uint32_t dNameSize, uint16_t flags, uint16_t curve, const uint8_t *key, uint32_t keySize)
63+
{
64+
return WiFiClient::setECTrustAnchorBearSSL(dName, dNameSize, flags, curve, key, keySize);
65+
}

src/WiFiSSLClient.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class WiFiBearSSLClient : public WiFiClient {
4040

4141
virtual int connect(IPAddress ip, uint16_t port);
4242
virtual int connect(const char* host, uint16_t port);
43+
virtual int setECTrustAnchor(const uint8_t *dName, uint32_t dNameSize, uint16_t flags, uint16_t curve, const uint8_t *key, uint32_t keySize);
4344
};
4445

4546
#endif /* WIFISSLCLIENT_H */

src/utility/server_drv.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,4 +514,49 @@ uint8_t ServerDrv::getSocket()
514514
return _data;
515515
}
516516

517+
uint8_t ServerDrv::setECTrustAnchorBearSSL(const uint8_t *dName, uint32_t dNameSize, uint16_t flags, uint16_t curve, const uint8_t *key, uint32_t keySize)
518+
{
519+
WAIT_FOR_SLAVE_SELECT();
520+
521+
int commandSize = 4;
522+
SpiDrv::sendCmd(BRSSL_SET_EC_TA, PARAM_NUMS_4);
523+
524+
/* Send distinguished name */
525+
SpiDrv::sendBuffer((uint8_t*)dName, dNameSize);
526+
commandSize += dNameSize + 1;
527+
528+
/* Send flags */
529+
SpiDrv::sendParam(flags);
530+
commandSize += 2;
531+
532+
/* Send curve */
533+
SpiDrv::sendParam(curve);
534+
commandSize += 2;
535+
536+
/* Send key */
537+
SpiDrv::sendBuffer((uint8_t*)key, keySize, LAST_PARAM);
538+
commandSize += keySize + 1;
539+
540+
// pad to multiple of 4
541+
while (commandSize % 4) {
542+
SpiDrv::readChar();
543+
commandSize++;
544+
}
545+
546+
SpiDrv::spiSlaveDeselect();
547+
//Wait the reply elaboration
548+
SpiDrv::waitForSlaveReady();
549+
SpiDrv::spiSlaveSelect();
550+
551+
// Wait for reply
552+
uint8_t result = 0;
553+
uint8_t len = 1;
554+
SpiDrv::waitResponseCmd(BRSSL_SET_EC_TA, PARAM_NUMS_1, (uint8_t*)&result, &len);
555+
556+
SpiDrv::spiSlaveDeselect();
557+
558+
// if everything went ok the returned value is 0
559+
return result == 0;
560+
}
561+
517562
ServerDrv serverDrv;

src/utility/server_drv.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class ServerDrv
4040
static void startClient(const char* host, uint8_t host_len, uint32_t ipAddress, uint16_t port, uint8_t sock, uint8_t protMode=TCP_MODE);
4141

4242
static void stopClient(uint8_t sock);
43-
43+
4444
static uint8_t getServerState(uint8_t sock);
4545

4646
static uint8_t getClientState(uint8_t sock);
@@ -62,6 +62,8 @@ class ServerDrv
6262
static uint8_t checkDataSent(uint8_t sock);
6363

6464
static uint8_t getSocket();
65+
66+
static uint8_t setECTrustAnchorBearSSL(const uint8_t *dName, uint32_t dNameSize, uint16_t flags, uint16_t curve, const uint8_t *key, uint32_t keySize);
6567
};
6668

6769
extern ServerDrv serverDrv;

src/utility/wifi_spi.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@ enum {
126126
APPLY_OTA_COMMAND = 0x65,
127127
RENAME_FILE = 0x66,
128128
DOWNLOAD_OTA = 0x67,
129+
BRSSL_SET_EC_TA = 0x68,
130+
131+
// Low-level BSD-like sockets functions.
132+
// From 0x70 to 0x7F
129133
};
130134

131135

0 commit comments

Comments
 (0)