|
3 | 3 | * Application layered TCP/TLS connection API (to be used from TCPIP thread) |
4 | 4 | * |
5 | 5 | * This file provides a TLS layer using mbedTLS |
6 | | - * |
| 6 | + * |
7 | 7 | * This version is currently compatible with the 2.x.x branch (current LTS). |
8 | 8 | */ |
9 | 9 |
|
|
70 | 70 | /* @todo: which includes are really needed? */ |
71 | 71 | #include "mbedtls/entropy.h" |
72 | 72 | #include "mbedtls/ctr_drbg.h" |
73 | | -#include "mbedtls/certs.h" |
74 | 73 | #include "mbedtls/x509.h" |
75 | 74 | #include "mbedtls/ssl.h" |
76 | 75 | #include "mbedtls/net_sockets.h" |
|
81 | 80 | #include "mbedtls/ssl_cache.h" |
82 | 81 | #include "mbedtls/ssl_ticket.h" |
83 | 82 |
|
84 | | -#include "mbedtls/ssl_internal.h" /* to call mbedtls_flush_output after ERR_MEM */ |
85 | | - |
86 | 83 | #include <string.h> |
87 | 84 |
|
88 | 85 | #ifndef ALTCP_MBEDTLS_ENTROPY_PTR |
@@ -132,6 +129,16 @@ static err_t altcp_mbedtls_lower_recv_process(struct altcp_pcb *conn, altcp_mbed |
132 | 129 | static err_t altcp_mbedtls_handle_rx_appldata(struct altcp_pcb *conn, altcp_mbedtls_state_t *state); |
133 | 130 | static int altcp_mbedtls_bio_send(void *ctx, const unsigned char *dataptr, size_t size); |
134 | 131 |
|
| 132 | +static void |
| 133 | +altcp_mbedtls_flush_output(altcp_mbedtls_state_t *state) |
| 134 | +{ |
| 135 | + if (state->ssl_context.MBEDTLS_PRIVATE(out_left) != 0) { |
| 136 | + int flushed = mbedtls_ssl_send_alert_message(&state->ssl_context, 0, 0); |
| 137 | + if (flushed) { |
| 138 | + LWIP_DEBUGF(ALTCP_MBEDTLS_DEBUG, ("mbedtls_ssl_send_alert_message failed: %d\n", flushed)); |
| 139 | + } |
| 140 | + } |
| 141 | +} |
135 | 142 |
|
136 | 143 | /* callback functions from inner/lower connection: */ |
137 | 144 |
|
@@ -524,14 +531,14 @@ altcp_mbedtls_lower_sent(void *arg, struct altcp_pcb *inner_conn, u16_t len) |
524 | 531 | LWIP_ASSERT("state", state != NULL); |
525 | 532 | LWIP_ASSERT("pcb mismatch", conn->inner_conn == inner_conn); |
526 | 533 | /* calculate TLS overhead part to not send it to application */ |
527 | | - overhead = state->overhead_bytes_adjust + state->ssl_context.out_left; |
| 534 | + overhead = state->overhead_bytes_adjust + state->ssl_context.MBEDTLS_PRIVATE(out_left); |
528 | 535 | if ((unsigned)overhead > len) { |
529 | 536 | overhead = len; |
530 | 537 | } |
531 | 538 | /* remove ACKed bytes from overhead adjust counter */ |
532 | 539 | state->overhead_bytes_adjust -= len; |
533 | 540 | /* try to send more if we failed before (may increase overhead adjust counter) */ |
534 | | - mbedtls_ssl_flush_output(&state->ssl_context); |
| 541 | + altcp_mbedtls_flush_output(state); |
535 | 542 | /* remove calculated overhead from ACKed bytes len */ |
536 | 543 | app_len = len - (u16_t)overhead; |
537 | 544 | /* update application write counter and inform application */ |
@@ -559,7 +566,7 @@ altcp_mbedtls_lower_poll(void *arg, struct altcp_pcb *inner_conn) |
559 | 566 | if (conn->state) { |
560 | 567 | altcp_mbedtls_state_t *state = (altcp_mbedtls_state_t *)conn->state; |
561 | 568 | /* try to send more if we failed before */ |
562 | | - mbedtls_ssl_flush_output(&state->ssl_context); |
| 569 | + altcp_mbedtls_flush_output(state); |
563 | 570 | if (altcp_mbedtls_handle_rx_appldata(conn, state) == ERR_ABRT) { |
564 | 571 | return ERR_ABRT; |
565 | 572 | } |
@@ -683,7 +690,7 @@ altcp_tls_set_session(struct altcp_pcb *conn, struct altcp_tls_session *session) |
683 | 690 | if (session && conn && conn->state) { |
684 | 691 | altcp_mbedtls_state_t *state = (altcp_mbedtls_state_t *)conn->state; |
685 | 692 | int ret = -1; |
686 | | - if (session->data.start) |
| 693 | + if (session->data.MBEDTLS_PRIVATE(start)) |
687 | 694 | ret = mbedtls_ssl_set_session(&state->ssl_context, &session->data); |
688 | 695 | return ret < 0 ? ERR_VAL : ERR_OK; |
689 | 696 | } |
@@ -776,7 +783,7 @@ altcp_tls_create_config(int is_server, u8_t cert_count, u8_t pkey_count, int hav |
776 | 783 | struct altcp_tls_config *conf; |
777 | 784 | mbedtls_x509_crt *mem; |
778 | 785 |
|
779 | | - if (TCP_WND < MBEDTLS_SSL_MAX_CONTENT_LEN) { |
| 786 | + if (TCP_WND < MBEDTLS_SSL_IN_CONTENT_LEN || TCP_WND < MBEDTLS_SSL_OUT_CONTENT_LEN) { |
780 | 787 | LWIP_DEBUGF(ALTCP_MBEDTLS_DEBUG|LWIP_DBG_LEVEL_SERIOUS, |
781 | 788 | ("altcp_tls: TCP_WND is smaller than the RX decrypion buffer, connection RX might stall!\n")); |
782 | 789 | } |
@@ -900,7 +907,7 @@ err_t altcp_tls_config_server_add_privkey_cert(struct altcp_tls_config *config, |
900 | 907 | return ERR_VAL; |
901 | 908 | } |
902 | 909 |
|
903 | | - ret = mbedtls_pk_parse_key(pkey, (const unsigned char *) privkey, privkey_len, privkey_pass, privkey_pass_len); |
| 910 | + ret = mbedtls_pk_parse_key(pkey, (const unsigned char *) privkey, privkey_len, privkey_pass, privkey_pass_len, mbedtls_ctr_drbg_random, &altcp_tls_entropy_rng->ctr_drbg); |
904 | 911 | if (ret != 0) { |
905 | 912 | LWIP_DEBUGF(ALTCP_MBEDTLS_DEBUG, ("mbedtls_pk_parse_public_key failed: %d\n", ret)); |
906 | 913 | mbedtls_x509_crt_free(srvcert); |
@@ -1003,7 +1010,7 @@ altcp_tls_create_config_client_2wayauth(const u8_t *ca, size_t ca_len, const u8_ |
1003 | 1010 | } |
1004 | 1011 |
|
1005 | 1012 | mbedtls_pk_init(conf->pkey); |
1006 | | - ret = mbedtls_pk_parse_key(conf->pkey, privkey, privkey_len, privkey_pass, privkey_pass_len); |
| 1013 | + ret = mbedtls_pk_parse_key(conf->pkey, privkey, privkey_len, privkey_pass, privkey_pass_len, mbedtls_ctr_drbg_random, &altcp_tls_entropy_rng->ctr_drbg); |
1007 | 1014 | if (ret != 0) { |
1008 | 1015 | LWIP_DEBUGF(ALTCP_MBEDTLS_DEBUG, ("mbedtls_pk_parse_key failed: %d 0x%x\n", ret, -1*ret)); |
1009 | 1016 | altcp_tls_free_config(conf); |
@@ -1189,7 +1196,7 @@ altcp_mbedtls_sndbuf(struct altcp_pcb *conn) |
1189 | 1196 | size_t ret; |
1190 | 1197 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
1191 | 1198 | /* @todo: adjust ssl_added to real value related to negotiated cipher */ |
1192 | | - size_t max_frag_len = mbedtls_ssl_get_max_frag_len(&state->ssl_context); |
| 1199 | + size_t max_frag_len = mbedtls_ssl_get_max_in_record_payload(&state->ssl_context); |
1193 | 1200 | max_len = LWIP_MIN(max_frag_len, max_len); |
1194 | 1201 | #endif |
1195 | 1202 | /* Adjust sndbuf of inner_conn with what added by SSL */ |
@@ -1232,9 +1239,9 @@ altcp_mbedtls_write(struct altcp_pcb *conn, const void *dataptr, u16_t len, u8_t |
1232 | 1239 | /* HACK: if there is something left to send, try to flush it and only |
1233 | 1240 | allow sending more if this succeeded (this is a hack because neither |
1234 | 1241 | returning 0 nor MBEDTLS_ERR_SSL_WANT_WRITE worked for me) */ |
1235 | | - if (state->ssl_context.out_left) { |
1236 | | - mbedtls_ssl_flush_output(&state->ssl_context); |
1237 | | - if (state->ssl_context.out_left) { |
| 1242 | + if (state->ssl_context.MBEDTLS_PRIVATE(out_left)) { |
| 1243 | + altcp_mbedtls_flush_output(state); |
| 1244 | + if (state->ssl_context.MBEDTLS_PRIVATE(out_left)) { |
1238 | 1245 | return ERR_MEM; |
1239 | 1246 | } |
1240 | 1247 | } |
@@ -1284,6 +1291,8 @@ altcp_mbedtls_bio_send(void *ctx, const unsigned char *dataptr, size_t size) |
1284 | 1291 | while (size_left) { |
1285 | 1292 | u16_t write_len = (u16_t)LWIP_MIN(size_left, 0xFFFF); |
1286 | 1293 | err_t err = altcp_write(conn->inner_conn, (const void *)dataptr, write_len, apiflags); |
| 1294 | + /* try to send data... */ |
| 1295 | + altcp_output(conn->inner_conn); |
1287 | 1296 | if (err == ERR_OK) { |
1288 | 1297 | written += write_len; |
1289 | 1298 | size_left -= write_len; |
|
0 commit comments