Skip to content

Commit 324c2fd

Browse files
committed
Terminate connection if increase_bm_data_size fails
As suggested in igrr/axtls-8266#2 (comment)
1 parent 96fbb39 commit 324c2fd

File tree

3 files changed

+20
-12
lines changed

3 files changed

+20
-12
lines changed

ssl/tls1.c

+18-11
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ static int verify_digest(SSL *ssl, int mode, const uint8_t *buf, int read_len);
5353
static void *crypt_new(SSL *ssl, uint8_t *key, uint8_t *iv, int is_decrypt, void* cached);
5454
static int send_raw_packet(SSL *ssl, uint8_t protocol);
5555
static void certificate_free(SSL* ssl);
56+
static int increase_bm_data_size(SSL *ssl);
5657

5758
/**
5859
* The server will pick the cipher based on the order that the order that the
@@ -258,10 +259,11 @@ EXP_FUNC void STDCALL ssl_free(SSL *ssl)
258259
*/
259260
EXP_FUNC int STDCALL ssl_read(SSL *ssl, uint8_t **in_data)
260261
{
261-
if (ssl->hs_status == SSL_OK) {
262-
certificate_free(ssl);
262+
int ret = increase_bm_data_size(ssl);
263+
if (ret != SSL_OK) {
264+
return ret;
263265
}
264-
int ret = basic_read(ssl, in_data);
266+
ret = basic_read(ssl, in_data);
265267

266268
/* check for return code so we can send an alert */
267269
if (ret < SSL_OK && ret != SSL_CLOSE_NOTIFY)
@@ -285,8 +287,9 @@ EXP_FUNC int STDCALL ssl_read(SSL *ssl, uint8_t **in_data)
285287
EXP_FUNC int STDCALL ssl_write(SSL *ssl, const uint8_t *out_data, int out_len)
286288
{
287289
int n = out_len, nw, i, tot = 0;
288-
if (ssl->hs_status == SSL_OK) {
289-
certificate_free(ssl);
290+
int ret = increase_bm_data_size(ssl);
291+
if (ret != SSL_OK) {
292+
return ret;
290293
}
291294
/* maximum size of a TLS packet is around 16kB, so fragment */
292295
do
@@ -549,6 +552,7 @@ SSL *ssl_new(SSL_CTX *ssl_ctx, int client_fd)
549552
ssl->flag = SSL_NEED_RECORD;
550553
ssl->bm_data = ssl->bm_all_data + BM_RECORD_OFFSET; /* space at the start */
551554
ssl->hs_status = SSL_NOT_OK; /* not connected */
555+
ssl->can_increase_data_size = false;
552556
#ifdef CONFIG_ENABLE_VERIFICATION
553557
ssl->ca_cert_ctx = ssl_ctx->ca_cert_ctx;
554558
#endif
@@ -1405,21 +1409,25 @@ int basic_read(SSL *ssl, uint8_t **in_data)
14051409
return ret;
14061410
}
14071411

1408-
void increase_bm_data_size(SSL *ssl)
1412+
int increase_bm_data_size(SSL *ssl)
14091413
{
1410-
if (ssl->max_plain_length == RT_MAX_PLAIN_LENGTH) {
1411-
return;
1414+
if (!ssl->can_increase_data_size ||
1415+
ssl->max_plain_length == RT_MAX_PLAIN_LENGTH) {
1416+
return SSL_OK;
14121417
}
1413-
1418+
ssl->can_increase_data_size = false;
1419+
certificate_free(ssl);
14141420
free(ssl->bm_all_data);
14151421
ssl->bm_data = 0;
14161422
ssl->bm_all_data = malloc(RT_MAX_PLAIN_LENGTH + RT_EXTRA);
14171423
if (!ssl->bm_all_data) {
14181424
printf("failed to grow plain buffer\r\n");
1419-
return;
1425+
ssl->hs_status == SSL_ERROR_DEAD;
1426+
return SSL_ERROR_CONN_LOST;
14201427
}
14211428
ssl->max_plain_length = RT_MAX_PLAIN_LENGTH;
14221429
ssl->bm_data = ssl->bm_all_data + BM_RECORD_OFFSET;
1430+
return SSL_OK;
14231431
}
14241432

14251433
/**
@@ -1686,7 +1694,6 @@ static void certificate_free(SSL* ssl)
16861694
ssl->x509_ctx = 0;
16871695
}
16881696
#endif
1689-
increase_bm_data_size(ssl);
16901697
}
16911698

16921699
#ifndef CONFIG_SSL_SKELETON_MODE /* no session resumption in this mode */

ssl/tls1.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ struct _SSL
189189
#endif
190190
#ifdef CONFIG_SSL_CERT_VERIFICATION
191191
X509_CTX *x509_ctx;
192+
bool can_increase_data_size;
192193
#endif
193194
uint8_t session_id[SSL_SESSION_ID_SIZE];
194195
uint8_t client_mac[SHA1_SIZE]; /* for HMAC verification */
@@ -261,7 +262,6 @@ void remove_ca_certs(CA_CERT_CTX *ca_cert_ctx);
261262
#ifdef CONFIG_SSL_ENABLE_CLIENT
262263
int do_client_connect(SSL *ssl);
263264
#endif
264-
void increase_bm_data_size(SSL *ssl);
265265

266266
#ifdef CONFIG_SSL_FULL_MODE
267267
void DISPLAY_STATE(SSL *ssl, int is_send, uint8_t state, int not_ok);

ssl/tls1_clnt.c

+1
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ int do_clnt_handshake(SSL *ssl, int handshake_type, uint8_t *buf, int hs_len)
119119

120120
case HS_FINISHED:
121121
ret = process_finished(ssl, buf, hs_len);
122+
ssl->can_increase_data_size = true;
122123
disposable_free(ssl);
123124
/* note: client renegotiation is not allowed after this */
124125
break;

0 commit comments

Comments
 (0)