|
36 | 36 |
|
37 | 37 | 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); |
38 | 38 |
|
| 39 | + |
| 40 | +bool BearSSLClient::_sslio_closing = false; |
| 41 | + |
| 42 | + |
39 | 43 | BearSSLClient::BearSSLClient(Client* client, const br_x509_trust_anchor* myTAs, int myNumTAs, GetTimeCallbackFunc func) : |
40 | 44 | _client(client), |
41 | 45 | _TAs(myTAs), |
@@ -156,6 +160,7 @@ void BearSSLClient::stop() |
156 | 160 | { |
157 | 161 | if (_client->connected()) { |
158 | 162 | if ((br_ssl_engine_current_state(&_sc.eng) & BR_SSL_CLOSED) == 0) { |
| 163 | + BearSSLClient::_sslio_closing = true; |
159 | 164 | br_sslio_close(&_ioc); |
160 | 165 | } |
161 | 166 |
|
@@ -258,6 +263,9 @@ int BearSSLClient::errorCode() |
258 | 263 |
|
259 | 264 | int BearSSLClient::connectSSL(const char* host) |
260 | 265 | { |
| 266 | + /* Ensure this flag is cleared so we don't terminate a just starting connection. */ |
| 267 | + _sslio_closing = false; |
| 268 | + |
261 | 269 | // initialize client context with all necessary algorithms and hardcoded trust anchors. |
262 | 270 | aiotc_client_profile_init(&_sc, &_xc, _TAs, _numTAs); |
263 | 271 |
|
@@ -313,8 +321,18 @@ int BearSSLClient::connectSSL(const char* host) |
313 | 321 |
|
314 | 322 | // #define DEBUGSERIAL Serial |
315 | 323 |
|
| 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 | + |
316 | 329 | int BearSSLClient::clientRead(void *ctx, unsigned char *buf, size_t len) |
317 | 330 | { |
| 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 | + |
318 | 336 | Client* c = (Client*)ctx; |
319 | 337 |
|
320 | 338 | if (!c->connected()) { |
@@ -346,6 +364,11 @@ int BearSSLClient::clientRead(void *ctx, unsigned char *buf, size_t len) |
346 | 364 |
|
347 | 365 | int BearSSLClient::clientWrite(void *ctx, const unsigned char *buf, size_t len) |
348 | 366 | { |
| 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 | + |
349 | 372 | Client* c = (Client*)ctx; |
350 | 373 |
|
351 | 374 | #ifdef DEBUGSERIAL |
|
0 commit comments