Skip to content

Commit 59fc84e

Browse files
committed
WiFiS3 WiFiSSLClient: avoid duplicated AT message calls using setCACert and setEccSlot
- Also fix setCACert signature to be consistent with other core implementations
1 parent bb8937d commit 59fc84e

File tree

2 files changed

+29
-30
lines changed

2 files changed

+29
-30
lines changed

Diff for: libraries/WiFiS3/src/WiFiSSLClient.cpp

+28-29
Original file line numberDiff line numberDiff line change
@@ -36,54 +36,53 @@ int WiFiSSLClient::connect(IPAddress ip, uint16_t port) {
3636
int WiFiSSLClient::connect(const char* host, uint16_t port) {
3737
/* -------------------------------------------------------------------------- */
3838
getSocket();
39-
if (_root_ca != nullptr) {
40-
setCACert(_root_ca);
39+
40+
/* if _root_ca is NULL it configures default root ca bundle */
41+
string res = "";
42+
if(_root_ca != nullptr) {
43+
size_t size = strlen(_root_ca);
44+
modem.write_nowait(string(PROMPT(_SETCAROOT)),res, "%s%d,%d\r\n" , CMD_WRITE(_SETCAROOT), _sock, size);
45+
if(!modem.passthrough((uint8_t *)_root_ca, size)) {
46+
return 0;
47+
}
4148
} else {
42-
setCACert();
49+
if(!modem.write(string(PROMPT(_SETCAROOT)),res, "%s%d\r\n" , CMD_WRITE(_SETCAROOT), _sock)) {
50+
return 0;
51+
}
4352
}
44-
if ((_ecc_slot != -1) && (_ecc_cert != nullptr) && (_ecc_cert_len != 0)) {
45-
setEccSlot(_ecc_slot, _ecc_cert, _ecc_cert_len);
53+
54+
/* if needed configure software SE for mTLS */
55+
if((_ecc_cert_len > 0) && (_ecc_cert != nullptr) && (_ecc_slot >= 0)) {
56+
modem.write_nowait(string(PROMPT(_SETECCSLOT)),res, "%s%d,%d,%d\r\n" , CMD_WRITE(_SETECCSLOT), _sock, _ecc_slot, _ecc_cert_len);
57+
if(!modem.passthrough((uint8_t *)_ecc_cert, _ecc_cert_len)) {
58+
return 0;
59+
}
4660
}
47-
string res = "";
61+
4862
if (_connectionTimeout) {
4963
if(modem.write(string(PROMPT(_SSLCLIENTCONNECT)),res, "%s%d,%s,%d,%d\r\n" , CMD_WRITE(_SSLCLIENTCONNECT), _sock, host,port, _connectionTimeout)) {
5064
return 1;
5165
}
5266
} else {
53-
if(modem.write(string(PROMPT(_SSLCLIENTCONNECTNAME)),res, "%s%d,%s,%d\r\n" , CMD_WRITE(_SSLCLIENTCONNECTNAME), _sock, host, port)) {
54-
return 1;
55-
}
67+
if(modem.write(string(PROMPT(_SSLCLIENTCONNECTNAME)),res, "%s%d,%s,%d\r\n" , CMD_WRITE(_SSLCLIENTCONNECTNAME), _sock, host, port)) {
68+
return 1;
69+
}
5670
}
5771
return 0;
5872
}
5973

6074
/* -------------------------------------------------------------------------- */
61-
void WiFiSSLClient::setCACert(const char* root_ca, size_t size) {
75+
void WiFiSSLClient::setCACert(const char* root_ca) {
6276
/* -------------------------------------------------------------------------- */
63-
getSocket();
64-
string res = "";
65-
if(size > 0) {
66-
modem.write_nowait(string(PROMPT(_SETCAROOT)),res, "%s%d,%d\r\n" , CMD_WRITE(_SETCAROOT), _sock, size);
67-
if(modem.passthrough((uint8_t *)root_ca, size)) {
68-
_root_ca = root_ca;
69-
}
70-
} else {
71-
modem.write(string(PROMPT(_SETCAROOT)),res, "%s%d\r\n" , CMD_WRITE(_SETCAROOT), _sock);
72-
}
77+
_root_ca = root_ca;
7378
}
7479

7580
/* -------------------------------------------------------------------------- */
7681
void WiFiSSLClient::setEccSlot(int ecc508KeySlot, const byte cert[], int certLength) {
7782
/* -------------------------------------------------------------------------- */
78-
getSocket();
79-
string res = "";
80-
if(certLength > 0) {
81-
modem.write_nowait(string(PROMPT(_SETECCSLOT)),res, "%s%d,%d,%d\r\n" , CMD_WRITE(_SETECCSLOT), _sock, ecc508KeySlot, certLength);
82-
modem.passthrough((uint8_t *)cert, certLength);
83-
_ecc_slot = ecc508KeySlot;
84-
_ecc_cert = cert;
85-
_ecc_cert_len = certLength;
86-
}
83+
_ecc_slot = ecc508KeySlot;
84+
_ecc_cert = cert;
85+
_ecc_cert_len = certLength;
8786
}
8887

8988
/* -------------------------------------------------------------------------- */

Diff for: libraries/WiFiS3/src/WiFiSSLClient.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class WiFiSSLClient : public WiFiClient {
3333
~WiFiSSLClient();
3434
virtual int connect(IPAddress ip, uint16_t port);
3535
virtual int connect(const char* host, uint16_t port);
36-
void setCACert(const char* root_ca = NULL, size_t size = 0);
36+
void setCACert(const char* root_ca);
3737
void setEccSlot(int ecc508KeySlot, const byte cert[], int certLength);
3838
virtual size_t write(uint8_t);
3939
virtual size_t write(const uint8_t *buf, size_t size);

0 commit comments

Comments
 (0)