Skip to content

Commit 8e0212c

Browse files
committed
Fix not reading last TLS small record with fast closure
Fixes #3104 MbedTLS API fairplay
1 parent 3861e2e commit 8e0212c

File tree

4 files changed

+15
-7
lines changed

4 files changed

+15
-7
lines changed

mongoose.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8404,7 +8404,7 @@ static void read_conn(struct mg_connection *c) {
84048404
if (c->rtls.len == 0 || m < 0) {
84058405
// Close only when we have fully drained both rtls and TLS buffers
84068406
c->is_closing = 1; // or there's nothing we can do about it.
8407-
m = MG_IO_ERR;
8407+
if (m < 0) m = MG_IO_ERR; // but return last record data, see #3104
84088408
} else { // see #2885
84098409
// TLS buffer is capped to max record size, even though, there can
84108410
// be more than one record, give TLS a chance to process them.
@@ -13647,7 +13647,11 @@ long mg_tls_send(struct mg_connection *c, const void *buf, size_t len) {
1364713647
c->is_tls_throttled =
1364813648
(n == MBEDTLS_ERR_SSL_WANT_READ || n == MBEDTLS_ERR_SSL_WANT_WRITE);
1364913649
if (was_throttled) return MG_IO_WAIT; // flushed throttled data instead
13650-
if (c->is_tls_throttled) return len; // already encripted that when throttled
13650+
if (c->is_tls_throttled) {
13651+
tls->throttled_buf = (unsigned char *)buf; // MbedTLS code actually ignores
13652+
tls->throttled_len = len; // these, but let's play API rules
13653+
return (long) len; // already encripted that when throttled
13654+
}
1365113655
if (n <= 0) return MG_IO_ERR;
1365213656
return n;
1365313657
}

mongoose.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3469,18 +3469,18 @@ struct mg_tcpip_driver_xmc7_data {
34693469

34703470
#define MG_TCPIP_DRIVER_INIT(mgr) \
34713471
do { \
3472-
static struct mg_tcpip_driver_xmc7_data driver_data_; \
3472+
static struct mg_tcpip_driver_xmc7_data driver_data_; \
34733473
static struct mg_tcpip_if mif_; \
34743474
driver_data_.mdc_cr = MG_DRIVER_MDC_CR; \
34753475
driver_data_.phy_addr = MG_TCPIP_PHY_ADDR; \
34763476
mif_.ip = MG_TCPIP_IP; \
34773477
mif_.mask = MG_TCPIP_MASK; \
34783478
mif_.gw = MG_TCPIP_GW; \
3479-
mif_.driver = &mg_tcpip_driver_xmc7; \
3479+
mif_.driver = &mg_tcpip_driver_xmc7; \
34803480
mif_.driver_data = &driver_data_; \
34813481
MG_SET_MAC_ADDRESS(mif_.mac); \
34823482
mg_tcpip_init(mgr, &mif_); \
3483-
MG_INFO(("Driver: xmc7, MAC: %M", mg_print_mac, mif_.mac)); \
3483+
MG_INFO(("Driver: xmc7, MAC: %M", mg_print_mac, mif_.mac)); \
34843484
} while (0)
34853485

34863486
#endif

src/sock.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ static void read_conn(struct mg_connection *c) {
291291
if (c->rtls.len == 0 || m < 0) {
292292
// Close only when we have fully drained both rtls and TLS buffers
293293
c->is_closing = 1; // or there's nothing we can do about it.
294-
m = MG_IO_ERR;
294+
if (m < 0) m = MG_IO_ERR; // but return last record data, see #3104
295295
} else { // see #2885
296296
// TLS buffer is capped to max record size, even though, there can
297297
// be more than one record, give TLS a chance to process them.

src/tls_mbed.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,11 @@ long mg_tls_send(struct mg_connection *c, const void *buf, size_t len) {
200200
c->is_tls_throttled =
201201
(n == MBEDTLS_ERR_SSL_WANT_READ || n == MBEDTLS_ERR_SSL_WANT_WRITE);
202202
if (was_throttled) return MG_IO_WAIT; // flushed throttled data instead
203-
if (c->is_tls_throttled) return len; // already encripted that when throttled
203+
if (c->is_tls_throttled) {
204+
tls->throttled_buf = (unsigned char *)buf; // MbedTLS code actually ignores
205+
tls->throttled_len = len; // these, but let's play API rules
206+
return (long) len; // already encripted that when throttled
207+
}
204208
if (n <= 0) return MG_IO_ERR;
205209
return n;
206210
}

0 commit comments

Comments
 (0)