Skip to content

Commit 69c757f

Browse files
committed
Allow plain buffer size increase during handshake
1 parent 3fdea28 commit 69c757f

File tree

3 files changed

+25
-25
lines changed

3 files changed

+25
-25
lines changed

ssl/tls1.c

+23-22
Original file line numberDiff line numberDiff line change
@@ -53,7 +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);
56+
static int increase_bm_data_size(SSL *ssl, size_t size);
5757

5858
/**
5959
* The server will pick the cipher based on the order that the order that the
@@ -285,6 +285,11 @@ EXP_FUNC int STDCALL ssl_write(SSL *ssl, const uint8_t *out_data, int out_len)
285285
{
286286
int n = out_len, nw, i, tot = 0;
287287
/* maximum size of a TLS packet is around 16kB, so fragment */
288+
289+
if (ssl->can_free_certificates) {
290+
certificate_free(ssl);
291+
}
292+
288293
do
289294
{
290295
nw = n;
@@ -545,9 +550,9 @@ SSL *ssl_new(SSL_CTX *ssl_ctx, int client_fd)
545550
ssl->flag = SSL_NEED_RECORD;
546551
ssl->bm_data = ssl->bm_all_data + BM_RECORD_OFFSET; /* space at the start */
547552
ssl->hs_status = SSL_NOT_OK; /* not connected */
548-
ssl->can_increase_data_size = false;
549553
#ifdef CONFIG_ENABLE_VERIFICATION
550554
ssl->ca_cert_ctx = ssl_ctx->ca_cert_ctx;
555+
ssl->can_free_certificates = false;
551556
#endif
552557
disposable_new(ssl);
553558

@@ -1214,6 +1219,10 @@ int basic_read(SSL *ssl, uint8_t **in_data)
12141219
int read_len, is_client = IS_SET_SSL_FLAG(SSL_IS_CLIENT);
12151220
uint8_t *buf = ssl->bm_data;
12161221

1222+
if (ssl->can_free_certificates) {
1223+
certificate_free(ssl);
1224+
}
1225+
12171226
read_len = SOCKET_READ(ssl->client_fd, &buf[ssl->bm_read_index],
12181227
ssl->need_bytes-ssl->got_bytes);
12191228

@@ -1287,16 +1296,8 @@ int basic_read(SSL *ssl, uint8_t **in_data)
12871296
if (ssl->need_bytes > ssl->max_plain_length+RT_EXTRA-BM_RECORD_OFFSET)
12881297
{
12891298
printf("ssl->need_bytes=%d > %d\r\n", ssl->need_bytes, ssl->max_plain_length+RT_EXTRA-BM_RECORD_OFFSET);
1290-
if (ssl->can_increase_data_size)
1291-
{
1292-
ret = increase_bm_data_size(ssl);
1293-
if (ret != SSL_OK)
1294-
{
1295-
ret = SSL_ERROR_INVALID_PROT_MSG;
1296-
goto error;
1297-
}
1298-
}
1299-
else
1299+
ret = increase_bm_data_size(ssl, ssl->need_bytes + BM_RECORD_OFFSET - RT_EXTRA);
1300+
if (ret != SSL_OK)
13001301
{
13011302
ret = SSL_ERROR_INVALID_PROT_MSG;
13021303
goto error;
@@ -1414,24 +1415,22 @@ int basic_read(SSL *ssl, uint8_t **in_data)
14141415
return ret;
14151416
}
14161417

1417-
int increase_bm_data_size(SSL *ssl)
1418+
int increase_bm_data_size(SSL *ssl, size_t size)
14181419
{
1419-
if (!ssl->can_increase_data_size ||
1420-
ssl->max_plain_length == RT_MAX_PLAIN_LENGTH) {
1420+
if (ssl->max_plain_length == RT_MAX_PLAIN_LENGTH) {
14211421
return SSL_OK;
14221422
}
1423-
certificate_free(ssl);
1424-
free(ssl->bm_all_data);
1425-
ssl->bm_data = 0;
1426-
ssl->bm_all_data = malloc(RT_MAX_PLAIN_LENGTH + RT_EXTRA);
1427-
if (!ssl->bm_all_data) {
1423+
size_t required = (size + 1023) & ~(1023); // round up to 1k
1424+
required = (required < RT_MAX_PLAIN_LENGTH) ? required : RT_MAX_PLAIN_LENGTH;
1425+
uint8_t* new_bm_all_data = (uint8_t*) realloc(ssl->bm_all_data, required + RT_EXTRA);
1426+
if (!new_bm_all_data) {
14281427
printf("failed to grow plain buffer\r\n");
14291428
ssl->hs_status = SSL_ERROR_DEAD;
14301429
return SSL_ERROR_CONN_LOST;
14311430
}
1432-
ssl->can_increase_data_size = false;
1433-
ssl->max_plain_length = RT_MAX_PLAIN_LENGTH;
1431+
ssl->bm_all_data = new_bm_all_data;
14341432
ssl->bm_data = ssl->bm_all_data + BM_RECORD_OFFSET;
1433+
ssl->max_plain_length = required;
14351434
return SSL_OK;
14361435
}
14371436

@@ -1689,6 +1688,7 @@ void disposable_free(SSL *ssl)
16891688
free(ssl->dc);
16901689
ssl->dc = NULL;
16911690
}
1691+
ssl->can_free_certificates = true;
16921692
}
16931693

16941694
static void certificate_free(SSL* ssl)
@@ -1698,6 +1698,7 @@ static void certificate_free(SSL* ssl)
16981698
x509_free(ssl->x509_ctx);
16991699
ssl->x509_ctx = 0;
17001700
}
1701+
ssl->can_free_certificates = false;
17011702
#endif
17021703
}
17031704

ssl/tls1.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ struct _SSL
189189
#endif
190190
#ifdef CONFIG_SSL_CERT_VERIFICATION
191191
X509_CTX *x509_ctx;
192-
bool can_increase_data_size;
192+
bool can_free_certificates;
193193
#endif
194194
uint8_t session_id[SSL_SESSION_ID_SIZE];
195195
uint8_t client_mac[SHA1_SIZE]; /* for HMAC verification */

ssl/tls1_clnt.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ EXP_FUNC SSL * STDCALL ssl_client_new(SSL_CTX *ssl_ctx, int client_fd, const
6666
SET_SSL_FLAG(SSL_SESSION_RESUME); /* just flag for later */
6767
}
6868

69-
if(host_name != NULL && strlen(host_name) > 0 || strlen(host_name) < 255 ) {
69+
if(host_name != NULL && strlen(host_name) > 0) {
7070
ssl->host_name = (char *)strdup(host_name);
7171
}
7272

@@ -123,7 +123,6 @@ int do_clnt_handshake(SSL *ssl, int handshake_type, uint8_t *buf, int hs_len)
123123

124124
case HS_FINISHED:
125125
ret = process_finished(ssl, buf, hs_len);
126-
ssl->can_increase_data_size = true;
127126
disposable_free(ssl);
128127
/* note: client renegotiation is not allowed after this */
129128
break;

0 commit comments

Comments
 (0)