Skip to content

Commit 10b41c8

Browse files
committed
Increase plaintext buffer size after handshake is complete
1 parent 6f48f0d commit 10b41c8

File tree

4 files changed

+66
-25
lines changed

4 files changed

+66
-25
lines changed

ssl/config.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
#undef CONFIG_SSL_USE_PKCS12
4747
#define CONFIG_SSL_EXPIRY_TIME 24
4848
#define CONFIG_X509_MAX_CA_CERTS 150
49-
#define CONFIG_SSL_MAX_CERTS 3
49+
#define CONFIG_SSL_MAX_CERTS 1
5050
#undef CONFIG_SSL_CTX_MUTEXING
5151
#undef CONFIG_USE_DEV_URANDOM
5252
#undef CONFIG_WIN32_USE_CRYPTO_LIB

ssl/tls1.c

+57-22
Original file line numberDiff line numberDiff line change
@@ -273,10 +273,8 @@ EXP_FUNC void STDCALL ssl_free(SSL *ssl)
273273
free(ssl->encrypt_ctx);
274274
free(ssl->decrypt_ctx);
275275
disposable_free(ssl);
276-
#ifdef CONFIG_SSL_CERT_VERIFICATION
277-
x509_free(ssl->x509_ctx);
278-
#endif
279-
276+
free(ssl->bm_all_data);
277+
free(ssl->fingerprint);
280278
free(ssl);
281279
}
282280

@@ -315,8 +313,8 @@ EXP_FUNC int STDCALL ssl_write(SSL *ssl, const uint8_t *out_data, int out_len)
315313
{
316314
nw = n;
317315

318-
if (nw > RT_MAX_PLAIN_LENGTH) /* fragment if necessary */
319-
nw = RT_MAX_PLAIN_LENGTH;
316+
if (nw > ssl->max_plain_length) /* fragment if necessary */
317+
nw = ssl->max_plain_length;
320318

321319
if ((i = send_packet(ssl, PT_APP_PROTOCOL_DATA,
322320
&out_data[tot], nw)) <= 0)
@@ -564,15 +562,18 @@ SSL *ssl_new(SSL_CTX *ssl_ctx, int client_fd)
564562
{
565563
SSL *ssl = (SSL *)calloc(1, sizeof(SSL));
566564
ssl->ssl_ctx = ssl_ctx;
565+
ssl->max_plain_length = 1460*4;
566+
ssl->bm_all_data = (uint8_t*) calloc(1, ssl->max_plain_length + RT_EXTRA);
567567
ssl->need_bytes = SSL_RECORD_SIZE; /* need a record */
568568
ssl->client_fd = client_fd;
569569
ssl->flag = SSL_NEED_RECORD;
570-
ssl->bm_data = ssl->bm_all_data+BM_RECORD_OFFSET; /* space at the start */
570+
ssl->bm_data = ssl->bm_all_data + BM_RECORD_OFFSET; /* space at the start */
571571
ssl->hs_status = SSL_NOT_OK; /* not connected */
572572
#ifdef CONFIG_ENABLE_VERIFICATION
573573
ssl->ca_cert_ctx = ssl_ctx->ca_cert_ctx;
574574
#endif
575575
disposable_new(ssl);
576+
ssl->fingerprint = 0;
576577

577578
/* a bit hacky but saves a few bytes of memory */
578579
ssl->flag |= ssl_ctx->options;
@@ -646,20 +647,36 @@ static void increment_write_sequence(SSL *ssl)
646647
static void add_hmac_digest(SSL *ssl, int mode, uint8_t *hmac_header,
647648
const uint8_t *buf, int buf_len, uint8_t *hmac_buf)
648649
{
649-
int hmac_len = buf_len + 8 + SSL_RECORD_SIZE;
650-
uint8_t *t_buf = (uint8_t *)malloc(hmac_len+10);
651-
652-
memcpy(t_buf, (mode == SSL_SERVER_WRITE || mode == SSL_CLIENT_WRITE) ?
650+
const prefix_size = 8 + SSL_RECORD_SIZE;
651+
bool hmac_inplace = (uint32_t)buf - (uint32_t)ssl->bm_data >= prefix_size;
652+
uint8_t tmp[prefix_size];
653+
int hmac_len = buf_len + prefix_size;
654+
uint8_t *t_buf;
655+
if (hmac_inplace) {
656+
t_buf = buf - prefix_size;
657+
memcpy(tmp, t_buf, prefix_size);
658+
} else {
659+
t_buf = (uint8_t *)malloc(hmac_len+10);
660+
}
661+
662+
memcpy(t_buf, (mode == SSL_SERVER_WRITE || mode == SSL_CLIENT_WRITE) ?
653663
ssl->write_sequence : ssl->read_sequence, 8);
654664
memcpy(&t_buf[8], hmac_header, SSL_RECORD_SIZE);
655-
memcpy(&t_buf[8+SSL_RECORD_SIZE], buf, buf_len);
665+
if (!hmac_inplace) {
666+
memcpy(&t_buf[8+SSL_RECORD_SIZE], buf, buf_len);
667+
}
656668

657-
ssl->cipher_info->hmac(t_buf, hmac_len,
658-
(mode == SSL_SERVER_WRITE || mode == SSL_CLIENT_READ) ?
659-
ssl->server_mac : ssl->client_mac,
669+
ssl->cipher_info->hmac(t_buf, hmac_len,
670+
(mode == SSL_SERVER_WRITE || mode == SSL_CLIENT_READ) ?
671+
ssl->server_mac : ssl->client_mac,
660672
ssl->cipher_info->digest_size, hmac_buf);
661673

662-
free(t_buf);
674+
if (hmac_inplace) {
675+
memcpy(t_buf, tmp, prefix_size);
676+
}
677+
else {
678+
free(t_buf);
679+
}
663680
#if 0
664681
print_blob("record", hmac_header, SSL_RECORD_SIZE);
665682
print_blob("buf", buf, buf_len);
@@ -1272,9 +1289,10 @@ int basic_read(SSL *ssl, uint8_t **in_data)
12721289
ssl->need_bytes = (buf[3] << 8) + buf[4];
12731290

12741291
/* do we violate the spec with the message size? */
1275-
if (ssl->need_bytes > RT_MAX_PLAIN_LENGTH+RT_EXTRA-BM_RECORD_OFFSET)
1292+
if (ssl->need_bytes > ssl->max_plain_length+RT_EXTRA-BM_RECORD_OFFSET)
12761293
{
12771294
ret = SSL_ERROR_INVALID_PROT_MSG;
1295+
printf("ssl->need_bytes=%d > %d\r\n", ssl->need_bytes, ssl->max_plain_length+RT_EXTRA-BM_RECORD_OFFSET);
12781296
goto error;
12791297
}
12801298

@@ -1389,6 +1407,19 @@ int basic_read(SSL *ssl, uint8_t **in_data)
13891407
return ret;
13901408
}
13911409

1410+
void increase_bm_data_size(SSL *ssl)
1411+
{
1412+
uint8_t* pr = (uint8_t*) realloc(ssl->bm_all_data, RT_MAX_PLAIN_LENGTH + RT_EXTRA);
1413+
if (pr) {
1414+
ssl->max_plain_length = RT_MAX_PLAIN_LENGTH;
1415+
ssl->bm_all_data = pr;
1416+
ssl->bm_data = pr + BM_RECORD_OFFSET;
1417+
}
1418+
else {
1419+
printf("failed to grow plain buffer\r\n");
1420+
}
1421+
}
1422+
13921423
/**
13931424
* Do some basic checking of data and then perform the appropriate handshaking.
13941425
*/
@@ -1643,7 +1674,12 @@ void disposable_free(SSL *ssl)
16431674
free(ssl->dc);
16441675
ssl->dc = NULL;
16451676
}
1646-
1677+
#ifdef CONFIG_SSL_CERT_VERIFICATION
1678+
if (ssl->x509_ctx) {
1679+
x509_free(ssl->x509_ctx);
1680+
ssl->x509_ctx = 0;
1681+
}
1682+
#endif
16471683
}
16481684

16491685
#ifndef CONFIG_SSL_SKELETON_MODE /* no session resumption in this mode */
@@ -1889,10 +1925,9 @@ int process_certificate(SSL *ssl, X509_CTX **x509_ctx)
18891925

18901926
EXP_FUNC int STDCALL ssl_match_fingerprint(const SSL *ssl, const uint8_t* fp)
18911927
{
1892-
uint8_t cert_fp[SHA1_SIZE];
1893-
X509_CTX* x509 = ssl->x509_ctx;
1894-
1895-
return memcmp(x509->fingerprint, fp, SHA1_SIZE);
1928+
if (!ssl->fingerprint)
1929+
return 1;
1930+
return memcmp(ssl->fingerprint, fp, SHA1_SIZE);
18961931
}
18971932

18981933
#endif /* CONFIG_SSL_CERT_VERIFICATION */

ssl/tls1.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ extern "C" {
7575
#define IS_SET_SSL_FLAG(A) (ssl->flag & A)
7676

7777
#define MAX_KEY_BYTE_SIZE 512 /* for a 4096 bit key */
78-
#define RT_MAX_PLAIN_LENGTH 4096
78+
#define RT_MAX_PLAIN_LENGTH 16384
7979
#define RT_EXTRA 1024
8080
#define BM_RECORD_OFFSET 5
8181

@@ -175,10 +175,11 @@ struct _SSL
175175
const cipher_info_t *cipher_info;
176176
void *encrypt_ctx;
177177
void *decrypt_ctx;
178-
uint8_t bm_all_data[RT_MAX_PLAIN_LENGTH+RT_EXTRA];
178+
uint8_t *bm_all_data;
179179
uint8_t *bm_data;
180180
uint16_t bm_index;
181181
uint16_t bm_read_index;
182+
size_t max_plain_length;
182183
struct _SSL *next; /* doubly linked list */
183184
struct _SSL *prev;
184185
struct _SSL_CTX *ssl_ctx; /* back reference to a clnt/svr ctx */
@@ -189,6 +190,7 @@ struct _SSL
189190
#ifdef CONFIG_SSL_CERT_VERIFICATION
190191
X509_CTX *x509_ctx;
191192
#endif
193+
uint8_t* fingerprint;
192194

193195
uint8_t session_id[SSL_SESSION_ID_SIZE];
194196
uint8_t client_mac[SHA1_SIZE]; /* for HMAC verification */
@@ -260,6 +262,7 @@ void remove_ca_certs(CA_CERT_CTX *ca_cert_ctx);
260262
#ifdef CONFIG_SSL_ENABLE_CLIENT
261263
int do_client_connect(SSL *ssl);
262264
#endif
265+
void increase_bm_data_size(SSL *ssl);
263266

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

ssl/tls1_clnt.c

+3
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,10 @@ 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->fingerprint = ssl->x509_ctx->fingerprint;
123+
ssl->x509_ctx->fingerprint = 0;
122124
disposable_free(ssl); /* free up some memory */
125+
increase_bm_data_size(ssl);
123126
/* note: client renegotiation is not allowed after this */
124127
break;
125128

0 commit comments

Comments
 (0)