@@ -66,7 +66,7 @@ class SSLContext {
66
66
public:
67
67
SSLContext () {
68
68
if (_ssl_ctx_refcnt == 0 ) {
69
- _ssl_ctx = ssl_ctx_new (SSL_SERVER_VERIFY_LATER | SSL_DEBUG_OPTS, 0 );
69
+ _ssl_ctx = ssl_ctx_new (SSL_SERVER_VERIFY_LATER | SSL_DEBUG_OPTS | SSL_CONNECT_IN_PARTS , 0 );
70
70
}
71
71
++_ssl_ctx_refcnt;
72
72
}
@@ -93,8 +93,21 @@ class SSLContext {
93
93
}
94
94
}
95
95
96
- void connect (ClientContext* ctx) {
96
+ void connect (ClientContext* ctx, uint32_t timeout_ms ) {
97
97
_ssl = ssl_client_new (_ssl_ctx, reinterpret_cast <int >(ctx), nullptr , 0 );
98
+ uint32_t t = millis ();
99
+
100
+ while (millis () - t < timeout_ms && ssl_handshake_status (_ssl) != SSL_OK) {
101
+ uint8_t * data;
102
+ int rc = ssl_read (_ssl, &data);
103
+ if (rc < SSL_OK) {
104
+ break ;
105
+ }
106
+ }
107
+ }
108
+
109
+ bool connected () {
110
+ return _ssl != nullptr && ssl_handshake_status (_ssl) == SSL_OK;
98
111
}
99
112
100
113
int read (uint8_t * dst, size_t size) {
@@ -246,7 +259,7 @@ int WiFiClientSecure::_connectSSL() {
246
259
247
260
_ssl = new SSLContext;
248
261
_ssl->ref ();
249
- _ssl->connect (_client);
262
+ _ssl->connect (_client, 5000 );
250
263
251
264
auto status = ssl_handshake_status (*_ssl);
252
265
if (status != SSL_OK) {
@@ -266,6 +279,11 @@ size_t WiFiClientSecure::write(const uint8_t *buf, size_t size) {
266
279
if (rc >= 0 )
267
280
return rc;
268
281
282
+ if (rc != SSL_CLOSE_NOTIFY) {
283
+ _ssl->unref ();
284
+ _ssl = nullptr ;
285
+ }
286
+
269
287
return 0 ;
270
288
}
271
289
@@ -318,17 +336,25 @@ int WiFiClientSecure::available() {
318
336
return _ssl->available ();
319
337
}
320
338
321
- uint8_t WiFiClientSecure::connected () {
322
- if (!_client)
323
- return 0 ;
324
-
325
- if (_client->state () == ESTABLISHED)
326
- return 1 ;
327
339
328
- if (!_ssl)
329
- return 0 ;
330
-
331
- return _ssl->available () > 0 ;
340
+ /*
341
+ SSL TCP RX data connected
342
+ null x x N
343
+ !null x Y Y
344
+ Y Y x Y
345
+ x N N N
346
+ err x N N
347
+ */
348
+ uint8_t WiFiClientSecure::connected () {
349
+ if (_ssl) {
350
+ if (_ssl->available ()) {
351
+ return true ;
352
+ }
353
+ if (_client && _client->state () == ESTABLISHED && _ssl->connected ()) {
354
+ return true ;
355
+ }
356
+ }
357
+ return false ;
332
358
}
333
359
334
360
void WiFiClientSecure::stop () {
0 commit comments