From c0cfe875c2c5801450f55a496c66c256d5b09886 Mon Sep 17 00:00:00 2001 From: "Earle F. Philhower, III" Date: Tue, 12 Jun 2018 13:18:00 -0700 Subject: [PATCH] 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. --- .../src/WiFiClientSecureBearSSL.cpp | 50 ++++++++++++------- 1 file changed, 32 insertions(+), 18 deletions(-) diff --git a/libraries/ESP8266WiFi/src/WiFiClientSecureBearSSL.cpp b/libraries/ESP8266WiFi/src/WiFiClientSecureBearSSL.cpp index 6aebf1f6c..89fbffd3e 100644 --- a/libraries/ESP8266WiFi/src/WiFiClientSecureBearSSL.cpp +++ b/libraries/ESP8266WiFi/src/WiFiClientSecureBearSSL.cpp @@ -235,30 +235,44 @@ uint8_t WiFiClientSecure::connected() { } size_t WiFiClientSecure::_write(const uint8_t *buf, size_t size, bool pmem) { + size_t sent_bytes = 0; + if (!connected() || !size || !_handshake_done) { return 0; } - if (_run_until(BR_SSL_SENDAPP) < 0) { - return 0; - } - - if (br_ssl_engine_current_state(_eng) & BR_SSL_SENDAPP) { - size_t sendapp_len; - unsigned char *sendapp_buf = br_ssl_engine_sendapp_buf(_eng, &sendapp_len); - int to_send = size > sendapp_len ? sendapp_len : size; - if (pmem) { - memcpy_P(sendapp_buf, buf, to_send); - } else { - memcpy(sendapp_buf, buf, to_send); + do { + // Ensure we yield if we need multiple fragments to avoid WDT + if (sent_bytes) { + optimistic_yield(1000); } - br_ssl_engine_sendapp_ack(_eng, to_send); - br_ssl_engine_flush(_eng, 0); - flush(); - return to_send; - } - return 0; + // Get BearSSL to a state where we can send + if (_run_until(BR_SSL_SENDAPP) < 0) { + break; + } + + if (br_ssl_engine_current_state(_eng) & BR_SSL_SENDAPP) { + size_t sendapp_len; + unsigned char *sendapp_buf = br_ssl_engine_sendapp_buf(_eng, &sendapp_len); + int to_send = size > sendapp_len ? sendapp_len : size; + if (pmem) { + memcpy_P(sendapp_buf, buf, to_send); + } else { + memcpy(sendapp_buf, buf, to_send); + } + br_ssl_engine_sendapp_ack(_eng, to_send); + br_ssl_engine_flush(_eng, 0); + flush(); + buf += to_send; + sent_bytes += to_send; + size -= to_send; + } else { + break; + } + } while (size); + + return sent_bytes; } size_t WiFiClientSecure::write(const uint8_t *buf, size_t size) {