@@ -35,7 +35,7 @@ class WiFiClientSecure;
35
35
36
36
class WiFiClientSecureCtx : public WiFiClient {
37
37
public:
38
- WiFiClientSecureCtx (const WiFiClientSecure* alter );
38
+ WiFiClientSecureCtx ();
39
39
WiFiClientSecureCtx (const WiFiClientSecureCtx &rhs) = delete ;
40
40
~WiFiClientSecureCtx () override ;
41
41
@@ -149,6 +149,10 @@ class WiFiClientSecureCtx : public WiFiClient {
149
149
// consume bytes after use (see peekBuffer)
150
150
virtual void peekConsume (size_t consume) override ;
151
151
152
+ void setNormalTimeout (unsigned long timeout) { _normalTimeout = timeout; }
153
+ void setHandshakeTimeout (unsigned long timeout) { _handshakeTimeout = timeout; }
154
+ unsigned long getHandshakeTimeout () const { return _handshakeTimeout; }
155
+
152
156
protected:
153
157
bool _connectSSL (const char *hostName); // Do initial SSL handshake
154
158
@@ -218,12 +222,10 @@ class WiFiClientSecureCtx : public WiFiClient {
218
222
219
223
// Methods for handling server.available() call which returns a client connection.
220
224
friend class WiFiClientSecure ; // access to private context constructors
221
- WiFiClientSecureCtx (const WiFiClientSecure* alter,
222
- ClientContext *client, const X509List *chain, unsigned cert_issuer_key_type,
225
+ WiFiClientSecureCtx (ClientContext *client, const X509List *chain, unsigned cert_issuer_key_type,
223
226
const PrivateKey *sk, int iobuf_in_size, int iobuf_out_size, ServerSessions *cache,
224
227
const X509List *client_CA_ta, int tls_min, int tls_max);
225
- WiFiClientSecureCtx (const WiFiClientSecure* alter,
226
- ClientContext* client, const X509List *chain, const PrivateKey *sk,
228
+ WiFiClientSecureCtx (ClientContext* client, const X509List *chain, const PrivateKey *sk,
227
229
int iobuf_in_size, int iobuf_out_size, ServerSessions *cache,
228
230
const X509List *client_CA_ta, int tls_min, int tls_max);
229
231
@@ -241,10 +243,11 @@ class WiFiClientSecureCtx : public WiFiClient {
241
243
uint8_t *_streamLoad (Stream& stream, size_t size);
242
244
243
245
// timeout management
244
- unsigned long _negociationTimeout = 0 ; // negociation timeout
245
- const WiFiClientSecure* _userFacingStream = nullptr ; // user-facing WiFiClientSecure
246
- void setStream (const WiFiClientSecure* upStream) { _userFacingStream = upStream; }
247
- void updateStreamTimeout ();
246
+
247
+ unsigned long _updateStreamTimeout () { return _timeout = _handshake_done? _normalTimeout: _handshakeTimeout; }
248
+ void _set_handshake_done (bool handshake_done) { _handshake_done = handshake_done; _updateStreamTimeout (); }
249
+
250
+ unsigned long _normalTimeout = 5000 , _handshakeTimeout = 15000 ;
248
251
249
252
}; // class WiFiClientSecureCtx
250
253
@@ -265,35 +268,33 @@ class WiFiClientSecure : public WiFiClient {
265
268
266
269
public:
267
270
268
- WiFiClientSecure ():_ctx(new WiFiClientSecureCtx(this )) { _owned = _ctx.get (); }
269
- WiFiClientSecure (const WiFiClientSecure &rhs): WiFiClient(), _ctx(rhs._ctx), _userNegociationTimeout(rhs._userNegociationTimeout) {
270
- if (_ctx) { _owned = _ctx.get (); _ctx->setStream (this ); }
271
- }
271
+ WiFiClientSecure ():_ctx(new WiFiClientSecureCtx()) { _owned = _ctx.get (); }
272
+ WiFiClientSecure (const WiFiClientSecure &rhs): WiFiClient(), _ctx(rhs._ctx) { if (_ctx) _owned = _ctx.get (); }
272
273
~WiFiClientSecure () override { _ctx = nullptr ; }
273
274
274
275
WiFiClientSecure& operator =(const WiFiClientSecure&) = default ;
275
276
276
277
std::unique_ptr<WiFiClient> clone () const override { return std::unique_ptr<WiFiClient>(new WiFiClientSecure (*this )); }
277
278
278
279
uint8_t status () override { return _ctx->status (); }
279
- int connect (IPAddress ip, uint16_t port) override { return _ctx->connect (ip, port); }
280
- int connect (const String& host, uint16_t port) override { return _ctx->connect (host, port); }
281
- int connect (const char * name, uint16_t port) override { return _ctx->connect (name, port); }
282
-
283
- uint8_t connected () override { return _ctx->connected (); }
284
- size_t write (const uint8_t *buf, size_t size) override { return _ctx->write (buf, size); }
285
- size_t write_P (PGM_P buf, size_t size) override { return _ctx->write_P (buf, size); }
286
- size_t write (const char *buf) { return write ((const uint8_t *)buf, strlen (buf)); }
287
- size_t write_P (const char *buf) { return write_P ((PGM_P)buf, strlen_P (buf)); }
288
- size_t write (Stream& stream) /* Note this is not virtual */ { return _ctx->write (stream); }
289
- int read (uint8_t *buf, size_t size) override { return _ctx->read (buf, size); }
290
- int available () override { return _ctx->available (); }
291
- int availableForWrite () override { return _ctx->availableForWrite (); }
292
- int read () override { return _ctx->read (); }
293
- int peek () override { return _ctx->peek (); }
294
- size_t peekBytes (uint8_t *buffer, size_t length) override { return _ctx->peekBytes (buffer, length); }
295
- bool flush (unsigned int maxWaitMs) { return _ctx->flush (maxWaitMs); }
296
- bool stop (unsigned int maxWaitMs) { return _ctx->stop (maxWaitMs); }
280
+ int connect (IPAddress ip, uint16_t port) override { uto (); return _ctx->connect (ip, port); }
281
+ int connect (const String& host, uint16_t port) override { uto (); return _ctx->connect (host, port); }
282
+ int connect (const char * name, uint16_t port) override { uto (); return _ctx->connect (name, port); }
283
+
284
+ uint8_t connected () override { uto (); return _ctx->connected (); }
285
+ size_t write (const uint8_t *buf, size_t size) override { uto (); return _ctx->write (buf, size); }
286
+ size_t write_P (PGM_P buf, size_t size) override { uto (); return _ctx->write_P (buf, size); }
287
+ size_t write (const char *buf) { uto (); return write ((const uint8_t *)buf, strlen (buf)); }
288
+ size_t write_P (const char *buf) { uto (); return write_P ((PGM_P)buf, strlen_P (buf)); }
289
+ size_t write (Stream& stream) /* Note this is not virtual */ { uto (); return _ctx->write (stream); }
290
+ int read (uint8_t *buf, size_t size) override { uto (); return _ctx->read (buf, size); }
291
+ int available () override { uto (); return _ctx->available (); }
292
+ int availableForWrite () override { uto (); return _ctx->availableForWrite (); }
293
+ int read () override { uto (); return _ctx->read (); }
294
+ int peek () override { uto (); return _ctx->peek (); }
295
+ size_t peekBytes (uint8_t *buffer, size_t length) override { uto (); return _ctx->peekBytes (buffer, length); }
296
+ bool flush (unsigned int maxWaitMs) { uto (); return _ctx->flush (maxWaitMs); }
297
+ bool stop (unsigned int maxWaitMs) { uto (); return _ctx->stop (maxWaitMs); }
297
298
void flush () override { (void )flush (0 ); }
298
299
void stop () override { (void )stop (0 ); }
299
300
@@ -362,7 +363,7 @@ class WiFiClientSecure : public WiFiClient {
362
363
virtual bool hasPeekBufferAPI () const override { return true ; }
363
364
364
365
// return number of byte accessible by peekBuffer()
365
- virtual size_t peekAvailable () override { return _ctx-> available (); }
366
+ virtual size_t peekAvailable () override { return available (); }
366
367
367
368
// return a pointer to available data buffer (size = peekAvailable())
368
369
// semantic forbids any kind of read() before calling peekConsume()
@@ -371,28 +372,33 @@ class WiFiClientSecure : public WiFiClient {
371
372
// consume bytes after use (see peekBuffer)
372
373
virtual void peekConsume (size_t consume) override { return _ctx->peekConsume (consume); }
373
374
374
- // allowing to change timeout during negociation
375
- void setNegociationTimeout (unsigned long timeout) { _userNegociationTimeout = timeout; }
376
- unsigned long getNegociationTimeout () const { return _userNegociationTimeout ; }
375
+ // allowing user to set timeout used during handshake
376
+ void setHandshakeTimeout (unsigned long timeout) { _ctx-> setHandshakeTimeout ( timeout) ; }
377
+ unsigned long getHandshakeTimeout () const { return _ctx-> getHandshakeTimeout () ; }
377
378
378
379
private:
379
380
std::shared_ptr<WiFiClientSecureCtx> _ctx;
380
- unsigned long _userNegociationTimeout = 15000 ; // negociation timeout initializer
381
381
382
382
// Methods for handling server.available() call which returns a client connection.
383
383
friend class WiFiServerSecure ; // Server needs to access these constructors
384
384
WiFiClientSecure (ClientContext *client, const X509List *chain, unsigned cert_issuer_key_type,
385
385
const PrivateKey *sk, int iobuf_in_size, int iobuf_out_size, ServerSessions *cache,
386
386
const X509List *client_CA_ta, int tls_min, int tls_max):
387
- _ctx (new WiFiClientSecureCtx(this , client, chain, cert_issuer_key_type, sk, iobuf_in_size, iobuf_out_size, cache, client_CA_ta, tls_min, tls_max)) {
387
+ _ctx (new WiFiClientSecureCtx(client, chain, cert_issuer_key_type, sk, iobuf_in_size, iobuf_out_size, cache, client_CA_ta, tls_min, tls_max)) {
388
388
}
389
389
390
390
WiFiClientSecure (ClientContext* client, const X509List *chain, const PrivateKey *sk,
391
391
int iobuf_in_size, int iobuf_out_size, ServerSessions *cache,
392
392
const X509List *client_CA_ta, int tls_min, int tls_max):
393
- _ctx (new WiFiClientSecureCtx(this , client, chain, sk, iobuf_in_size, iobuf_out_size, cache, client_CA_ta, tls_min, tls_max)) {
393
+ _ctx (new WiFiClientSecureCtx(client, chain, sk, iobuf_in_size, iobuf_out_size, cache, client_CA_ta, tls_min, tls_max)) {
394
394
}
395
395
396
+ // (because Stream::setTimeout() is not virtual,)
397
+ // forward user timeout from Stream:: to SSL context
398
+ // this is internally called on every user operations
399
+ inline void uto () { _ctx->setNormalTimeout (_timeout); }
400
+
401
+
396
402
}; // class WiFiClientSecure
397
403
398
404
}; // namespace BearSSL
0 commit comments