Skip to content

Commit e9f0ea2

Browse files
committed
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
1 parent 74aec43 commit e9f0ea2

File tree

2 files changed

+39
-38
lines changed

2 files changed

+39
-38
lines changed

libraries/ESP8266WiFi/src/WiFiClientSecure.cpp

+39-38
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ extern "C"
5050
#define SSL_DEBUG_OPTS 0
5151
#endif
5252

53-
#define SSL_RX_BUF_SIZE 4096
5453

5554
class SSLContext {
5655
public:
@@ -59,8 +58,6 @@ class SSLContext {
5958
_ssl_ctx = ssl_ctx_new(SSL_SERVER_VERIFY_LATER | SSL_DEBUG_OPTS, 0);
6059
}
6160
++_ssl_ctx_refcnt;
62-
63-
_rxbuf = new cbuf(SSL_RX_BUF_SIZE);
6461
}
6562

6663
~SSLContext() {
@@ -73,8 +70,6 @@ class SSLContext {
7370
if (_ssl_ctx_refcnt == 0) {
7471
ssl_ctx_free(_ssl_ctx);
7572
}
76-
77-
delete _rxbuf;
7873
}
7974

8075
void ref() {
@@ -92,38 +87,50 @@ class SSLContext {
9287
}
9388

9489
int read(uint8_t* dst, size_t size) {
95-
if (!_rxbuf->getSize()) {
96-
_readAll();
90+
if (!_available) {
91+
if (!_readAll())
92+
return 0;
9793
}
98-
size_t available = _rxbuf->getSize();
99-
size_t will_read = (available < size) ? available : size;
100-
return _rxbuf->read(reinterpret_cast<char*>(dst), will_read);
94+
size_t will_copy = (_available < size) ? _available : size;
95+
memcpy(dst, _read_ptr, will_copy);
96+
_read_ptr += will_copy;
97+
_available -= will_copy;
98+
if (_available == 0) {
99+
_read_ptr = nullptr;
100+
}
101+
return will_copy;
101102
}
102103

103104
int read() {
104-
optimistic_yield(100);
105-
if (!_rxbuf->getSize()) {
106-
_readAll();
105+
if (!_available) {
106+
if (!_readAll())
107+
return -1;
107108
}
108-
return _rxbuf->read();
109+
int result = _read_ptr[0];
110+
++_read_ptr;
111+
--_available;
112+
if (_available == 0) {
113+
_read_ptr = nullptr;
114+
}
115+
return result;
109116
}
110117

111118
int peek() {
112-
if (!_rxbuf->getSize()) {
113-
_readAll();
119+
if (!_available) {
120+
if (!_readAll())
121+
return -1;
114122
}
115-
return _rxbuf->peek();
123+
return _read_ptr[0];
116124
}
117125

118126
int available() {
119-
auto rc = _rxbuf->getSize();
120-
if (rc == 0) {
121-
_readAll();
122-
rc = _rxbuf->getSize();
127+
auto cb = _available;
128+
if (cb == 0) {
129+
cb = _readAll();
123130
} else {
124131
optimistic_yield(100);
125132
}
126-
return rc;
133+
return cb;
127134
}
128135

129136
operator SSL*() {
@@ -135,6 +142,8 @@ class SSLContext {
135142
if (!_ssl)
136143
return 0;
137144

145+
optimistic_yield(100);
146+
138147
uint8_t* data;
139148
int rc = ssl_read(_ssl, &data);
140149
if (rc <= 0) {
@@ -144,25 +153,18 @@ class SSLContext {
144153
}
145154
return 0;
146155
}
147-
148-
149-
if (rc > _rxbuf->room()) {
150-
DEBUGV("WiFiClientSecure rx overflow");
151-
rc = _rxbuf->room();
152-
}
153-
int result = 0;
154-
size_t sizeBefore = _rxbuf->getSize();
155-
if (rc)
156-
result = _rxbuf->write(reinterpret_cast<const char*>(data), rc);
157-
DEBUGV("*** rb: %d + %d = %d\r\n", sizeBefore, rc, _rxbuf->getSize());
158-
return result;
156+
DEBUGV(":wcs ra %d", rc);
157+
_read_ptr = data;
158+
_available = rc;
159+
return _available;
159160
}
160161

161162
static SSL_CTX* _ssl_ctx;
162163
static int _ssl_ctx_refcnt;
163164
SSL* _ssl = nullptr;
164165
int _refcnt = 0;
165-
cbuf* _rxbuf;
166+
const uint8_t* _read_ptr = nullptr;
167+
size_t _available = 0;
166168
};
167169

168170
SSL_CTX* SSLContext::_ssl_ctx = nullptr;
@@ -313,14 +315,13 @@ bool WiFiClientSecure::verify(const char* fp, const char* url) {
313315
while (pos < len && fp[pos] == ' ') {
314316
++pos;
315317
}
316-
DEBUGV("pos:%d ", pos);
317318
if (pos > len - 2) {
318-
DEBUGV("fingerprint too short\r\n");
319+
DEBUGV("pos:%d len:%d fingerprint too short\r\n", pos, len);
319320
return false;
320321
}
321322
uint8_t high, low;
322323
if (!parseHexNibble(fp[pos], &high) || !parseHexNibble(fp[pos+1], &low)) {
323-
DEBUGV("invalid hex sequence: %c%c\r\n", fp[pos], fp[pos+1]);
324+
DEBUGV("pos:%d len:%d invalid hex sequence: %c%c\r\n", pos, len, fp[pos], fp[pos+1]);
324325
return false;
325326
}
326327
pos += 2;

tools/sdk/lib/libaxtls.a

1.56 KB
Binary file not shown.

0 commit comments

Comments
 (0)