Skip to content

Commit 0f70b68

Browse files
committed
Through usage of the dedicated flag _sslio_closing we do manage to prevent getting stuck up in low_read/low_write (BearSSLClient::clientRead/clientWrite.
1 parent 68f4b51 commit 0f70b68

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/tls/BearSSLClient.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@
3636

3737
extern "C" void aiotc_client_profile_init(br_ssl_client_context *cc, br_x509_minimal_context *xc, const br_x509_trust_anchor *trust_anchors, size_t trust_anchors_num);
3838

39+
40+
bool BearSSLClient::_sslio_closing = false;
41+
42+
3943
BearSSLClient::BearSSLClient(Client* client, const br_x509_trust_anchor* myTAs, int myNumTAs, GetTimeCallbackFunc func) :
4044
_client(client),
4145
_TAs(myTAs),
@@ -156,6 +160,7 @@ void BearSSLClient::stop()
156160
{
157161
if (_client->connected()) {
158162
if ((br_ssl_engine_current_state(&_sc.eng) & BR_SSL_CLOSED) == 0) {
163+
BearSSLClient::_sslio_closing = true;
159164
br_sslio_close(&_ioc);
160165
}
161166

@@ -258,6 +263,9 @@ int BearSSLClient::errorCode()
258263

259264
int BearSSLClient::connectSSL(const char* host)
260265
{
266+
/* Ensure this flag is cleared so we don't terminate a just starting connection. */
267+
_sslio_closing = false;
268+
261269
// initialize client context with all necessary algorithms and hardcoded trust anchors.
262270
aiotc_client_profile_init(&_sc, &_xc, _TAs, _numTAs);
263271

@@ -313,8 +321,18 @@ int BearSSLClient::connectSSL(const char* host)
313321

314322
// #define DEBUGSERIAL Serial
315323

324+
/* Define the prototype so that it can be found by the compiler,
325+
* the correct function is then assigned at link time.
326+
*/
327+
extern "C" void br_ssl_engine_fail(br_ssl_engine_context *rc, int err);
328+
316329
int BearSSLClient::clientRead(void *ctx, unsigned char *buf, size_t len)
317330
{
331+
if (BearSSLClient::_sslio_closing) {
332+
br_ssl_engine_fail(reinterpret_cast<br_sslio_context *>(ctx)->engine, BR_ERR_IO);
333+
return -1;
334+
}
335+
318336
Client* c = (Client*)ctx;
319337

320338
if (!c->connected()) {
@@ -346,6 +364,11 @@ int BearSSLClient::clientRead(void *ctx, unsigned char *buf, size_t len)
346364

347365
int BearSSLClient::clientWrite(void *ctx, const unsigned char *buf, size_t len)
348366
{
367+
if (BearSSLClient::_sslio_closing) {
368+
br_ssl_engine_fail(reinterpret_cast<br_sslio_context *>(ctx)->engine, BR_ERR_IO);
369+
return -1;
370+
}
371+
349372
Client* c = (Client*)ctx;
350373

351374
#ifdef DEBUGSERIAL

src/tls/BearSSLClient.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ class BearSSLClient : public Client {
9898
br_x509_certificate _ecCert;
9999
bool _ecCertDynamic;
100100

101+
static bool _sslio_closing;
101102
br_ssl_client_context _sc;
102103
br_x509_minimal_context _xc;
103104
unsigned char _ibuf[BEAR_SSL_CLIENT_IBUF_SIZE];

0 commit comments

Comments
 (0)