Skip to content

Commit 594831d

Browse files
authored
WiFiClientSecure: use context (#7680)
* move WiFiClientSecure to WiFiClientSecureCtx and add WiFiClientSecure wrapper to handle the context * explicitely disable context copy constructor (similar to operator=) * move (static) probeMaxFragmentLength back from ctx to WiFiClientSecure * route sslclient::status() to context's ::status()
1 parent 35a5a70 commit 594831d

File tree

4 files changed

+156
-67
lines changed

4 files changed

+156
-67
lines changed

libraries/ESP8266WiFi/src/BearSSLHelpers.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ class X509List {
134134
class WiFiClientSecure;
135135

136136
class Session {
137-
friend class WiFiClientSecure;
137+
friend class WiFiClientSecureCtx;
138138

139139
public:
140140
Session() { memset(&_session, 0, sizeof(_session)); }

libraries/ESP8266WiFi/src/WiFiClient.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class WiFiClient : public Client, public SList<WiFiClient> {
5252
WiFiClient(const WiFiClient&);
5353
WiFiClient& operator=(const WiFiClient&);
5454

55-
uint8_t status();
55+
virtual uint8_t status();
5656
virtual int connect(IPAddress ip, uint16_t port) override;
5757
virtual int connect(const char *host, uint16_t port) override;
5858
virtual int connect(const String& host, uint16_t port);

libraries/ESP8266WiFi/src/WiFiClientSecureBearSSL.cpp

+38-43
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ extern "C" {
6767

6868
namespace BearSSL {
6969

70-
void WiFiClientSecure::_clear() {
70+
void WiFiClientSecureCtx::_clear() {
7171
// TLS handshake may take more than the 5 second default timeout
7272
_timeout = 15000;
7373

@@ -91,7 +91,7 @@ void WiFiClientSecure::_clear() {
9191
_cipher_cnt = 0;
9292
}
9393

94-
void WiFiClientSecure::_clearAuthenticationSettings() {
94+
void WiFiClientSecureCtx::_clearAuthenticationSettings() {
9595
_use_insecure = false;
9696
_use_fingerprint = false;
9797
_use_self_signed = false;
@@ -100,20 +100,15 @@ void WiFiClientSecure::_clearAuthenticationSettings() {
100100
}
101101

102102

103-
WiFiClientSecure::WiFiClientSecure() : WiFiClient() {
103+
WiFiClientSecureCtx::WiFiClientSecureCtx() : WiFiClient() {
104104
_clear();
105105
_clearAuthenticationSettings();
106106
_certStore = nullptr; // Don't want to remove cert store on a clear, should be long lived
107107
_sk = nullptr;
108108
stack_thunk_add_ref();
109109
}
110110

111-
WiFiClientSecure::WiFiClientSecure(const WiFiClientSecure &rhs) : WiFiClient(rhs) {
112-
*this = rhs;
113-
stack_thunk_add_ref();
114-
}
115-
116-
WiFiClientSecure::~WiFiClientSecure() {
111+
WiFiClientSecureCtx::~WiFiClientSecureCtx() {
117112
if (_client) {
118113
_client->unref();
119114
_client = nullptr;
@@ -123,7 +118,7 @@ WiFiClientSecure::~WiFiClientSecure() {
123118
stack_thunk_del_ref();
124119
}
125120

126-
WiFiClientSecure::WiFiClientSecure(ClientContext* client,
121+
WiFiClientSecureCtx::WiFiClientSecureCtx(ClientContext* client,
127122
const X509List *chain, const PrivateKey *sk,
128123
int iobuf_in_size, int iobuf_out_size, const X509List *client_CA_ta) {
129124
_clear();
@@ -140,7 +135,7 @@ WiFiClientSecure::WiFiClientSecure(ClientContext* client,
140135
}
141136
}
142137

143-
WiFiClientSecure::WiFiClientSecure(ClientContext *client,
138+
WiFiClientSecureCtx::WiFiClientSecureCtx(ClientContext *client,
144139
const X509List *chain,
145140
unsigned cert_issuer_key_type, const PrivateKey *sk,
146141
int iobuf_in_size, int iobuf_out_size, const X509List *client_CA_ta) {
@@ -158,20 +153,20 @@ WiFiClientSecure::WiFiClientSecure(ClientContext *client,
158153
}
159154
}
160155

161-
void WiFiClientSecure::setClientRSACert(const X509List *chain, const PrivateKey *sk) {
156+
void WiFiClientSecureCtx::setClientRSACert(const X509List *chain, const PrivateKey *sk) {
162157
_chain = chain;
163158
_sk = sk;
164159
}
165160

166-
void WiFiClientSecure::setClientECCert(const X509List *chain,
161+
void WiFiClientSecureCtx::setClientECCert(const X509List *chain,
167162
const PrivateKey *sk, unsigned allowed_usages, unsigned cert_issuer_key_type) {
168163
_chain = chain;
169164
_sk = sk;
170165
_allowed_usages = allowed_usages;
171166
_cert_issuer_key_type = cert_issuer_key_type;
172167
}
173168

174-
void WiFiClientSecure::setBufferSizes(int recv, int xmit) {
169+
void WiFiClientSecureCtx::setBufferSizes(int recv, int xmit) {
175170
// Following constants taken from bearssl/src/ssl/ssl_engine.c (not exported unfortunately)
176171
const int MAX_OUT_OVERHEAD = 85;
177172
const int MAX_IN_OVERHEAD = 325;
@@ -187,7 +182,7 @@ void WiFiClientSecure::setBufferSizes(int recv, int xmit) {
187182
_iobuf_out_size = xmit;
188183
}
189184

190-
bool WiFiClientSecure::stop(unsigned int maxWaitMs) {
185+
bool WiFiClientSecureCtx::stop(unsigned int maxWaitMs) {
191186
bool ret = WiFiClient::stop(maxWaitMs); // calls our virtual flush()
192187
// Only if we've already connected, store session params and clear the connection options
193188
if (_handshake_done) {
@@ -199,19 +194,19 @@ bool WiFiClientSecure::stop(unsigned int maxWaitMs) {
199194
return ret;
200195
}
201196

202-
bool WiFiClientSecure::flush(unsigned int maxWaitMs) {
197+
bool WiFiClientSecureCtx::flush(unsigned int maxWaitMs) {
203198
(void) _run_until(BR_SSL_SENDAPP);
204199
return WiFiClient::flush(maxWaitMs);
205200
}
206201

207-
int WiFiClientSecure::connect(IPAddress ip, uint16_t port) {
202+
int WiFiClientSecureCtx::connect(IPAddress ip, uint16_t port) {
208203
if (!WiFiClient::connect(ip, port)) {
209204
return 0;
210205
}
211206
return _connectSSL(nullptr);
212207
}
213208

214-
int WiFiClientSecure::connect(const char* name, uint16_t port) {
209+
int WiFiClientSecureCtx::connect(const char* name, uint16_t port) {
215210
IPAddress remote_addr;
216211
if (!WiFi.hostByName(name, remote_addr)) {
217212
DEBUG_BSSL("connect: Name lookup failure\n");
@@ -224,11 +219,11 @@ int WiFiClientSecure::connect(const char* name, uint16_t port) {
224219
return _connectSSL(name);
225220
}
226221

227-
int WiFiClientSecure::connect(const String& host, uint16_t port) {
222+
int WiFiClientSecureCtx::connect(const String& host, uint16_t port) {
228223
return connect(host.c_str(), port);
229224
}
230225

231-
void WiFiClientSecure::_freeSSL() {
226+
void WiFiClientSecureCtx::_freeSSL() {
232227
// These are smart pointers and will free if refcnt==0
233228
_sc = nullptr;
234229
_sc_svr = nullptr;
@@ -245,18 +240,18 @@ void WiFiClientSecure::_freeSSL() {
245240
_timeout = 15000;
246241
}
247242

248-
bool WiFiClientSecure::_clientConnected() {
243+
bool WiFiClientSecureCtx::_clientConnected() {
249244
return (_client && _client->state() == ESTABLISHED);
250245
}
251246

252-
uint8_t WiFiClientSecure::connected() {
247+
uint8_t WiFiClientSecureCtx::connected() {
253248
if (available() || (_clientConnected() && _handshake_done && (br_ssl_engine_current_state(_eng) != BR_SSL_CLOSED))) {
254249
return true;
255250
}
256251
return false;
257252
}
258253

259-
size_t WiFiClientSecure::_write(const uint8_t *buf, size_t size, bool pmem) {
254+
size_t WiFiClientSecureCtx::_write(const uint8_t *buf, size_t size, bool pmem) {
260255
size_t sent_bytes = 0;
261256

262257
if (!connected() || !size || !_handshake_done) {
@@ -297,16 +292,16 @@ size_t WiFiClientSecure::_write(const uint8_t *buf, size_t size, bool pmem) {
297292
return sent_bytes;
298293
}
299294

300-
size_t WiFiClientSecure::write(const uint8_t *buf, size_t size) {
295+
size_t WiFiClientSecureCtx::write(const uint8_t *buf, size_t size) {
301296
return _write(buf, size, false);
302297
}
303298

304-
size_t WiFiClientSecure::write_P(PGM_P buf, size_t size) {
299+
size_t WiFiClientSecureCtx::write_P(PGM_P buf, size_t size) {
305300
return _write((const uint8_t *)buf, size, true);
306301
}
307302

308303
// We have to manually read and send individual chunks.
309-
size_t WiFiClientSecure::write(Stream& stream) {
304+
size_t WiFiClientSecureCtx::write(Stream& stream) {
310305
size_t totalSent = 0;
311306
size_t countRead;
312307
size_t countSent;
@@ -329,7 +324,7 @@ size_t WiFiClientSecure::write(Stream& stream) {
329324
return totalSent;
330325
}
331326

332-
int WiFiClientSecure::read(uint8_t *buf, size_t size) {
327+
int WiFiClientSecureCtx::read(uint8_t *buf, size_t size) {
333328
if (!ctx_present() || !_handshake_done) {
334329
return -1;
335330
}
@@ -361,7 +356,7 @@ int WiFiClientSecure::read(uint8_t *buf, size_t size) {
361356
return 0; // If we're connected, no error but no read.
362357
}
363358

364-
int WiFiClientSecure::read() {
359+
int WiFiClientSecureCtx::read() {
365360
uint8_t c;
366361
if (1 == read(&c, 1)) {
367362
return c;
@@ -370,7 +365,7 @@ int WiFiClientSecure::read() {
370365
return -1;
371366
}
372367

373-
int WiFiClientSecure::available() {
368+
int WiFiClientSecureCtx::available() {
374369
if (_recvapp_buf) {
375370
return _recvapp_len; // Anything from last call?
376371
}
@@ -391,7 +386,7 @@ int WiFiClientSecure::available() {
391386
return 0;
392387
}
393388

394-
int WiFiClientSecure::peek() {
389+
int WiFiClientSecureCtx::peek() {
395390
if (!ctx_present() || !available()) {
396391
DEBUG_BSSL("peek: Not connected, none left available\n");
397392
return -1;
@@ -403,7 +398,7 @@ int WiFiClientSecure::peek() {
403398
return -1;
404399
}
405400

406-
size_t WiFiClientSecure::peekBytes(uint8_t *buffer, size_t length) {
401+
size_t WiFiClientSecureCtx::peekBytes(uint8_t *buffer, size_t length) {
407402
size_t to_copy = 0;
408403
if (!ctx_present()) {
409404
DEBUG_BSSL("peekBytes: Not connected\n");
@@ -426,7 +421,7 @@ size_t WiFiClientSecure::peekBytes(uint8_t *buffer, size_t length) {
426421
combination of both (the combination matches either). When a match is
427422
achieved, this function returns 0. On error, it returns -1.
428423
*/
429-
int WiFiClientSecure::_run_until(unsigned target, bool blocking) {
424+
int WiFiClientSecureCtx::_run_until(unsigned target, bool blocking) {
430425
if (!ctx_present()) {
431426
DEBUG_BSSL("_run_until: Not connected\n");
432427
return -1;
@@ -550,7 +545,7 @@ int WiFiClientSecure::_run_until(unsigned target, bool blocking) {
550545
return -1;
551546
}
552547

553-
bool WiFiClientSecure::_wait_for_handshake() {
548+
bool WiFiClientSecureCtx::_wait_for_handshake() {
554549
_handshake_done = false;
555550
while (!_handshake_done && _clientConnected()) {
556551
int ret = _run_until(BR_SSL_SENDAPP);
@@ -575,7 +570,7 @@ static uint8_t htoi (unsigned char c)
575570
}
576571

577572
// Set a fingerprint by parsing an ASCII string
578-
bool WiFiClientSecure::setFingerprint(const char *fpStr) {
573+
bool WiFiClientSecureCtx::setFingerprint(const char *fpStr) {
579574
int idx = 0;
580575
uint8_t c, d;
581576
uint8_t fp[20];
@@ -968,7 +963,7 @@ extern "C" {
968963
}
969964

970965
// Set custom list of ciphers
971-
bool WiFiClientSecure::setCiphers(const uint16_t *cipherAry, int cipherCount) {
966+
bool WiFiClientSecureCtx::setCiphers(const uint16_t *cipherAry, int cipherCount) {
972967
_cipher_list = nullptr;
973968
_cipher_list = std::shared_ptr<uint16_t>(new (std::nothrow) uint16_t[cipherCount], std::default_delete<uint16_t[]>());
974969
if (!_cipher_list.get()) {
@@ -980,16 +975,16 @@ bool WiFiClientSecure::setCiphers(const uint16_t *cipherAry, int cipherCount) {
980975
return true;
981976
}
982977

983-
bool WiFiClientSecure::setCiphersLessSecure() {
978+
bool WiFiClientSecureCtx::setCiphersLessSecure() {
984979
return setCiphers(faster_suites_P, sizeof(faster_suites_P)/sizeof(faster_suites_P[0]));
985980
}
986981

987-
bool WiFiClientSecure::setCiphers(std::vector<uint16_t> list) {
982+
bool WiFiClientSecureCtx::setCiphers(const std::vector<uint16_t>& list) {
988983
return setCiphers(&list[0], list.size());
989984
}
990985

991986
// Installs the appropriate X509 cert validation method for a client connection
992-
bool WiFiClientSecure::_installClientX509Validator() {
987+
bool WiFiClientSecureCtx::_installClientX509Validator() {
993988
if (_use_insecure || _use_fingerprint || _use_self_signed) {
994989
// Use common insecure x509 authenticator
995990
_x509_insecure = std::make_shared<struct br_x509_insecure_context>();
@@ -1046,7 +1041,7 @@ bool WiFiClientSecure::_installClientX509Validator() {
10461041

10471042
// Called by connect() to do the actual SSL setup and handshake.
10481043
// Returns if the SSL handshake succeeded.
1049-
bool WiFiClientSecure::_connectSSL(const char* hostName) {
1044+
bool WiFiClientSecureCtx::_connectSSL(const char* hostName) {
10501045
DEBUG_BSSL("_connectSSL: start connection\n");
10511046
_freeSSL();
10521047
_oom_err = false;
@@ -1136,7 +1131,7 @@ bool WiFiClientSecure::_connectSSL(const char* hostName) {
11361131

11371132
// Slightly different X509 setup for servers who want to validate client
11381133
// certificates, so factor it out as it's used in RSA and EC servers.
1139-
bool WiFiClientSecure::_installServerX509Validator(const X509List *client_CA_ta) {
1134+
bool WiFiClientSecureCtx::_installServerX509Validator(const X509List *client_CA_ta) {
11401135
if (client_CA_ta) {
11411136
_ta = client_CA_ta;
11421137
// X509 minimal validator. Checks dates, cert chain for trusted CA, etc.
@@ -1169,7 +1164,7 @@ bool WiFiClientSecure::_installServerX509Validator(const X509List *client_CA_ta)
11691164

11701165

11711166
// Called by WiFiServerBearSSL when an RSA cert/key is specified.
1172-
bool WiFiClientSecure::_connectSSLServerRSA(const X509List *chain,
1167+
bool WiFiClientSecureCtx::_connectSSLServerRSA(const X509List *chain,
11731168
const PrivateKey *sk,
11741169
const X509List *client_CA_ta) {
11751170
_freeSSL();
@@ -1205,7 +1200,7 @@ bool WiFiClientSecure::_connectSSLServerRSA(const X509List *chain,
12051200
}
12061201

12071202
// Called by WiFiServerBearSSL when an elliptic curve cert/key is specified.
1208-
bool WiFiClientSecure::_connectSSLServerEC(const X509List *chain,
1203+
bool WiFiClientSecureCtx::_connectSSLServerEC(const X509List *chain,
12091204
unsigned cert_issuer_key_type, const PrivateKey *sk,
12101205
const X509List *client_CA_ta) {
12111206
#ifndef BEARSSL_SSL_BASIC
@@ -1251,7 +1246,7 @@ bool WiFiClientSecure::_connectSSLServerEC(const X509List *chain,
12511246

12521247
// Returns an error ID and possibly a string (if dest != null) of the last
12531248
// BearSSL reported error.
1254-
int WiFiClientSecure::getLastSSLError(char *dest, size_t len) {
1249+
int WiFiClientSecureCtx::getLastSSLError(char *dest, size_t len) {
12551250
int err = 0;
12561251
const char *t = PSTR("OK");
12571252
const char *recv_fatal = "";

0 commit comments

Comments
 (0)