Skip to content

Commit c0cfe87

Browse files
Make BearSSL::write() blocking, match axTLS (#4804)
When a message is sent by the app that is larger than the SSL buffer, it will take multiple TLS fragments to transfer. Writes will loop through and not return until either all data is transferred or there is an error.
1 parent ebda795 commit c0cfe87

File tree

1 file changed

+30
-16
lines changed

1 file changed

+30
-16
lines changed

Diff for: libraries/ESP8266WiFi/src/WiFiClientSecureBearSSL.cpp

+30-16
Original file line numberDiff line numberDiff line change
@@ -235,30 +235,44 @@ uint8_t WiFiClientSecure::connected() {
235235
}
236236

237237
size_t WiFiClientSecure::_write(const uint8_t *buf, size_t size, bool pmem) {
238+
size_t sent_bytes = 0;
239+
238240
if (!connected() || !size || !_handshake_done) {
239241
return 0;
240242
}
241243

242-
if (_run_until(BR_SSL_SENDAPP) < 0) {
243-
return 0;
244-
}
244+
do {
245+
// Ensure we yield if we need multiple fragments to avoid WDT
246+
if (sent_bytes) {
247+
optimistic_yield(1000);
248+
}
249+
250+
// Get BearSSL to a state where we can send
251+
if (_run_until(BR_SSL_SENDAPP) < 0) {
252+
break;
253+
}
245254

246-
if (br_ssl_engine_current_state(_eng) & BR_SSL_SENDAPP) {
247-
size_t sendapp_len;
248-
unsigned char *sendapp_buf = br_ssl_engine_sendapp_buf(_eng, &sendapp_len);
249-
int to_send = size > sendapp_len ? sendapp_len : size;
250-
if (pmem) {
251-
memcpy_P(sendapp_buf, buf, to_send);
255+
if (br_ssl_engine_current_state(_eng) & BR_SSL_SENDAPP) {
256+
size_t sendapp_len;
257+
unsigned char *sendapp_buf = br_ssl_engine_sendapp_buf(_eng, &sendapp_len);
258+
int to_send = size > sendapp_len ? sendapp_len : size;
259+
if (pmem) {
260+
memcpy_P(sendapp_buf, buf, to_send);
261+
} else {
262+
memcpy(sendapp_buf, buf, to_send);
263+
}
264+
br_ssl_engine_sendapp_ack(_eng, to_send);
265+
br_ssl_engine_flush(_eng, 0);
266+
flush();
267+
buf += to_send;
268+
sent_bytes += to_send;
269+
size -= to_send;
252270
} else {
253-
memcpy(sendapp_buf, buf, to_send);
271+
break;
254272
}
255-
br_ssl_engine_sendapp_ack(_eng, to_send);
256-
br_ssl_engine_flush(_eng, 0);
257-
flush();
258-
return to_send;
259-
}
273+
} while (size);
260274

261-
return 0;
275+
return sent_bytes;
262276
}
263277

264278
size_t WiFiClientSecure::write(const uint8_t *buf, size_t size) {

0 commit comments

Comments
 (0)