mirror of
https://github.com/esp8266/Arduino.git
synced 2025-04-21 10:26:06 +03:00
WiFiClientSecure: handle full size TLS fragments (#43)
- free up some memory by getting rid of intermediate buffer - libaxtls: update to 6830d98 - allocate plaintext buffer in two stages: 4*MSS initially, grow to 16k after handshake - free certificate data after handshake is complete - preallocate some structures to reduce memory fragmentation
This commit is contained in:
parent
74aec438ae
commit
e9f0ea2afe
@ -50,7 +50,6 @@ extern "C"
|
|||||||
#define SSL_DEBUG_OPTS 0
|
#define SSL_DEBUG_OPTS 0
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define SSL_RX_BUF_SIZE 4096
|
|
||||||
|
|
||||||
class SSLContext {
|
class SSLContext {
|
||||||
public:
|
public:
|
||||||
@ -59,8 +58,6 @@ public:
|
|||||||
_ssl_ctx = ssl_ctx_new(SSL_SERVER_VERIFY_LATER | SSL_DEBUG_OPTS, 0);
|
_ssl_ctx = ssl_ctx_new(SSL_SERVER_VERIFY_LATER | SSL_DEBUG_OPTS, 0);
|
||||||
}
|
}
|
||||||
++_ssl_ctx_refcnt;
|
++_ssl_ctx_refcnt;
|
||||||
|
|
||||||
_rxbuf = new cbuf(SSL_RX_BUF_SIZE);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
~SSLContext() {
|
~SSLContext() {
|
||||||
@ -73,8 +70,6 @@ public:
|
|||||||
if (_ssl_ctx_refcnt == 0) {
|
if (_ssl_ctx_refcnt == 0) {
|
||||||
ssl_ctx_free(_ssl_ctx);
|
ssl_ctx_free(_ssl_ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
delete _rxbuf;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ref() {
|
void ref() {
|
||||||
@ -92,38 +87,50 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
int read(uint8_t* dst, size_t size) {
|
int read(uint8_t* dst, size_t size) {
|
||||||
if (!_rxbuf->getSize()) {
|
if (!_available) {
|
||||||
_readAll();
|
if (!_readAll())
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
size_t available = _rxbuf->getSize();
|
size_t will_copy = (_available < size) ? _available : size;
|
||||||
size_t will_read = (available < size) ? available : size;
|
memcpy(dst, _read_ptr, will_copy);
|
||||||
return _rxbuf->read(reinterpret_cast<char*>(dst), will_read);
|
_read_ptr += will_copy;
|
||||||
|
_available -= will_copy;
|
||||||
|
if (_available == 0) {
|
||||||
|
_read_ptr = nullptr;
|
||||||
|
}
|
||||||
|
return will_copy;
|
||||||
}
|
}
|
||||||
|
|
||||||
int read() {
|
int read() {
|
||||||
optimistic_yield(100);
|
if (!_available) {
|
||||||
if (!_rxbuf->getSize()) {
|
if (!_readAll())
|
||||||
_readAll();
|
return -1;
|
||||||
}
|
}
|
||||||
return _rxbuf->read();
|
int result = _read_ptr[0];
|
||||||
|
++_read_ptr;
|
||||||
|
--_available;
|
||||||
|
if (_available == 0) {
|
||||||
|
_read_ptr = nullptr;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
int peek() {
|
int peek() {
|
||||||
if (!_rxbuf->getSize()) {
|
if (!_available) {
|
||||||
_readAll();
|
if (!_readAll())
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
return _rxbuf->peek();
|
return _read_ptr[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
int available() {
|
int available() {
|
||||||
auto rc = _rxbuf->getSize();
|
auto cb = _available;
|
||||||
if (rc == 0) {
|
if (cb == 0) {
|
||||||
_readAll();
|
cb = _readAll();
|
||||||
rc = _rxbuf->getSize();
|
|
||||||
} else {
|
} else {
|
||||||
optimistic_yield(100);
|
optimistic_yield(100);
|
||||||
}
|
}
|
||||||
return rc;
|
return cb;
|
||||||
}
|
}
|
||||||
|
|
||||||
operator SSL*() {
|
operator SSL*() {
|
||||||
@ -135,6 +142,8 @@ protected:
|
|||||||
if (!_ssl)
|
if (!_ssl)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
|
optimistic_yield(100);
|
||||||
|
|
||||||
uint8_t* data;
|
uint8_t* data;
|
||||||
int rc = ssl_read(_ssl, &data);
|
int rc = ssl_read(_ssl, &data);
|
||||||
if (rc <= 0) {
|
if (rc <= 0) {
|
||||||
@ -144,25 +153,18 @@ protected:
|
|||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
DEBUGV(":wcs ra %d", rc);
|
||||||
|
_read_ptr = data;
|
||||||
if (rc > _rxbuf->room()) {
|
_available = rc;
|
||||||
DEBUGV("WiFiClientSecure rx overflow");
|
return _available;
|
||||||
rc = _rxbuf->room();
|
|
||||||
}
|
|
||||||
int result = 0;
|
|
||||||
size_t sizeBefore = _rxbuf->getSize();
|
|
||||||
if (rc)
|
|
||||||
result = _rxbuf->write(reinterpret_cast<const char*>(data), rc);
|
|
||||||
DEBUGV("*** rb: %d + %d = %d\r\n", sizeBefore, rc, _rxbuf->getSize());
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static SSL_CTX* _ssl_ctx;
|
static SSL_CTX* _ssl_ctx;
|
||||||
static int _ssl_ctx_refcnt;
|
static int _ssl_ctx_refcnt;
|
||||||
SSL* _ssl = nullptr;
|
SSL* _ssl = nullptr;
|
||||||
int _refcnt = 0;
|
int _refcnt = 0;
|
||||||
cbuf* _rxbuf;
|
const uint8_t* _read_ptr = nullptr;
|
||||||
|
size_t _available = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
SSL_CTX* SSLContext::_ssl_ctx = nullptr;
|
SSL_CTX* SSLContext::_ssl_ctx = nullptr;
|
||||||
@ -313,14 +315,13 @@ bool WiFiClientSecure::verify(const char* fp, const char* url) {
|
|||||||
while (pos < len && fp[pos] == ' ') {
|
while (pos < len && fp[pos] == ' ') {
|
||||||
++pos;
|
++pos;
|
||||||
}
|
}
|
||||||
DEBUGV("pos:%d ", pos);
|
|
||||||
if (pos > len - 2) {
|
if (pos > len - 2) {
|
||||||
DEBUGV("fingerprint too short\r\n");
|
DEBUGV("pos:%d len:%d fingerprint too short\r\n", pos, len);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
uint8_t high, low;
|
uint8_t high, low;
|
||||||
if (!parseHexNibble(fp[pos], &high) || !parseHexNibble(fp[pos+1], &low)) {
|
if (!parseHexNibble(fp[pos], &high) || !parseHexNibble(fp[pos+1], &low)) {
|
||||||
DEBUGV("invalid hex sequence: %c%c\r\n", fp[pos], fp[pos+1]);
|
DEBUGV("pos:%d len:%d invalid hex sequence: %c%c\r\n", pos, len, fp[pos], fp[pos+1]);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
pos += 2;
|
pos += 2;
|
||||||
|
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user