-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Respect timeout with SSL #8899
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Respect timeout with SSL #8899
Changes from 9 commits
fadfc9d
c6b2311
d62ac3a
74cabd1
1efc1de
fa00511
ed0d6fa
d97e0fb
cc1951a
4702c93
e3aa6fe
d9118bb
a199c8d
d324077
3d796a1
b0c75ac
7735f37
ae64f5b
e5ea7b3
1e0cfff
19420e0
6182300
ac9d961
d64e0c1
6fa35d0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,8 @@ | |
|
||
namespace BearSSL { | ||
|
||
class WiFiClientSecure; | ||
|
||
class WiFiClientSecureCtx : public WiFiClient { | ||
public: | ||
WiFiClientSecureCtx(); | ||
|
@@ -147,6 +149,10 @@ class WiFiClientSecureCtx : public WiFiClient { | |
// consume bytes after use (see peekBuffer) | ||
virtual void peekConsume (size_t consume) override; | ||
|
||
void setNormalTimeout (unsigned long timeout) { _normalTimeout = timeout; } | ||
void setHandshakeTimeout (unsigned long timeout) { _handshakeTimeout = timeout; } | ||
unsigned long getHandshakeTimeout () const { return _handshakeTimeout; } | ||
|
||
protected: | ||
bool _connectSSL(const char *hostName); // Do initial SSL handshake | ||
|
||
|
@@ -235,6 +241,14 @@ class WiFiClientSecureCtx : public WiFiClient { | |
bool _installServerX509Validator(const X509List *client_CA_ta); // Setup X509 client cert validation, if supplied | ||
|
||
uint8_t *_streamLoad(Stream& stream, size_t size); | ||
|
||
// timeout management | ||
|
||
unsigned long _updateStreamTimeout () { return _timeout = _handshake_done? _normalTimeout: _handshakeTimeout; } | ||
void _set_handshake_done (bool handshake_done) { _handshake_done = handshake_done; _updateStreamTimeout(); } | ||
|
||
unsigned long _normalTimeout = 5000, _handshakeTimeout = 15000; | ||
|
||
}; // class WiFiClientSecureCtx | ||
|
||
|
||
|
@@ -263,24 +277,24 @@ class WiFiClientSecure : public WiFiClient { | |
std::unique_ptr<WiFiClient> clone() const override { return std::unique_ptr<WiFiClient>(new WiFiClientSecure(*this)); } | ||
|
||
uint8_t status() override { return _ctx->status(); } | ||
int connect(IPAddress ip, uint16_t port) override { return _ctx->connect(ip, port); } | ||
int connect(const String& host, uint16_t port) override { return _ctx->connect(host, port); } | ||
int connect(const char* name, uint16_t port) override { return _ctx->connect(name, port); } | ||
|
||
uint8_t connected() override { return _ctx->connected(); } | ||
size_t write(const uint8_t *buf, size_t size) override { return _ctx->write(buf, size); } | ||
size_t write_P(PGM_P buf, size_t size) override { return _ctx->write_P(buf, size); } | ||
size_t write(const char *buf) { return write((const uint8_t*)buf, strlen(buf)); } | ||
size_t write_P(const char *buf) { return write_P((PGM_P)buf, strlen_P(buf)); } | ||
size_t write(Stream& stream) /* Note this is not virtual */ { return _ctx->write(stream); } | ||
int read(uint8_t *buf, size_t size) override { return _ctx->read(buf, size); } | ||
int available() override { return _ctx->available(); } | ||
int availableForWrite() override { return _ctx->availableForWrite(); } | ||
int read() override { return _ctx->read(); } | ||
int peek() override { return _ctx->peek(); } | ||
size_t peekBytes(uint8_t *buffer, size_t length) override { return _ctx->peekBytes(buffer, length); } | ||
bool flush(unsigned int maxWaitMs) { return _ctx->flush(maxWaitMs); } | ||
bool stop(unsigned int maxWaitMs) { return _ctx->stop(maxWaitMs); } | ||
int connect(IPAddress ip, uint16_t port) override { uto(); return _ctx->connect(ip, port); } | ||
int connect(const String& host, uint16_t port) override { uto(); return _ctx->connect(host, port); } | ||
int connect(const char* name, uint16_t port) override { uto(); return _ctx->connect(name, port); } | ||
|
||
uint8_t connected() override { uto(); return _ctx->connected(); } | ||
size_t write(const uint8_t *buf, size_t size) override { uto(); return _ctx->write(buf, size); } | ||
size_t write_P(PGM_P buf, size_t size) override { uto(); return _ctx->write_P(buf, size); } | ||
size_t write(const char *buf) { uto(); return write((const uint8_t*)buf, strlen(buf)); } | ||
size_t write_P(const char *buf) { uto(); return write_P((PGM_P)buf, strlen_P(buf)); } | ||
size_t write(Stream& stream) /* Note this is not virtual */ { uto(); return _ctx->write(stream); } | ||
int read(uint8_t *buf, size_t size) override { uto(); return _ctx->read(buf, size); } | ||
int available() override { uto(); return _ctx->available(); } | ||
int availableForWrite() override { uto(); return _ctx->availableForWrite(); } | ||
int read() override { uto(); return _ctx->read(); } | ||
int peek() override { uto(); return _ctx->peek(); } | ||
size_t peekBytes(uint8_t *buffer, size_t length) override { uto(); return _ctx->peekBytes(buffer, length); } | ||
bool flush(unsigned int maxWaitMs) { uto(); return _ctx->flush(maxWaitMs); } | ||
bool stop(unsigned int maxWaitMs) { uto(); return _ctx->stop(maxWaitMs); } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. d-a-v/Arduino@ssltimeout...mcspr:esp8266-Arduino:ssltimeout ? reiterating on the matrix discussion - no need to touch _timeout when not necessary, ctx class can have its own timeout state There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
edit This commit reverts this one where there was an unwanted pointer of the client stream inside the context to help reaching user timeout. edit2 now |
||
void flush() override { (void)flush(0); } | ||
void stop() override { (void)stop(0); } | ||
|
||
|
@@ -349,7 +363,7 @@ class WiFiClientSecure : public WiFiClient { | |
virtual bool hasPeekBufferAPI () const override { return true; } | ||
|
||
// return number of byte accessible by peekBuffer() | ||
virtual size_t peekAvailable () override { return _ctx->available(); } | ||
virtual size_t peekAvailable () override { return available(); } | ||
|
||
// return a pointer to available data buffer (size = peekAvailable()) | ||
// semantic forbids any kind of read() before calling peekConsume() | ||
|
@@ -358,6 +372,10 @@ class WiFiClientSecure : public WiFiClient { | |
// consume bytes after use (see peekBuffer) | ||
virtual void peekConsume (size_t consume) override { return _ctx->peekConsume(consume); } | ||
|
||
// allowing user to set timeout used during handshake | ||
void setHandshakeTimeout (unsigned long timeout) { _ctx->setHandshakeTimeout(timeout); } | ||
unsigned long getHandshakeTimeout () const { return _ctx->getHandshakeTimeout(); } | ||
|
||
private: | ||
std::shared_ptr<WiFiClientSecureCtx> _ctx; | ||
|
||
|
@@ -375,6 +393,12 @@ class WiFiClientSecure : public WiFiClient { | |
_ctx(new WiFiClientSecureCtx(client, chain, sk, iobuf_in_size, iobuf_out_size, cache, client_CA_ta, tls_min, tls_max)) { | ||
} | ||
|
||
// (because Stream::setTimeout() is not virtual,) | ||
// forward user timeout from Stream:: to SSL context | ||
// this is internally called on every user operations | ||
inline void uto () { _ctx->setNormalTimeout(_timeout); } | ||
|
||
|
||
}; // class WiFiClientSecure | ||
|
||
}; // namespace BearSSL | ||
|
Uh oh!
There was an error while loading. Please reload this page.