@@ -273,10 +273,8 @@ EXP_FUNC void STDCALL ssl_free(SSL *ssl)
273
273
free (ssl -> encrypt_ctx );
274
274
free (ssl -> decrypt_ctx );
275
275
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 );
280
278
free (ssl );
281
279
}
282
280
@@ -315,8 +313,8 @@ EXP_FUNC int STDCALL ssl_write(SSL *ssl, const uint8_t *out_data, int out_len)
315
313
{
316
314
nw = n ;
317
315
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 ;
320
318
321
319
if ((i = send_packet (ssl , PT_APP_PROTOCOL_DATA ,
322
320
& out_data [tot ], nw )) <= 0 )
@@ -564,15 +562,18 @@ SSL *ssl_new(SSL_CTX *ssl_ctx, int client_fd)
564
562
{
565
563
SSL * ssl = (SSL * )calloc (1 , sizeof (SSL ));
566
564
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 );
567
567
ssl -> need_bytes = SSL_RECORD_SIZE ; /* need a record */
568
568
ssl -> client_fd = client_fd ;
569
569
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 */
571
571
ssl -> hs_status = SSL_NOT_OK ; /* not connected */
572
572
#ifdef CONFIG_ENABLE_VERIFICATION
573
573
ssl -> ca_cert_ctx = ssl_ctx -> ca_cert_ctx ;
574
574
#endif
575
575
disposable_new (ssl );
576
+ ssl -> fingerprint = 0 ;
576
577
577
578
/* a bit hacky but saves a few bytes of memory */
578
579
ssl -> flag |= ssl_ctx -> options ;
@@ -646,20 +647,36 @@ static void increment_write_sequence(SSL *ssl)
646
647
static void add_hmac_digest (SSL * ssl , int mode , uint8_t * hmac_header ,
647
648
const uint8_t * buf , int buf_len , uint8_t * hmac_buf )
648
649
{
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 ) ?
653
663
ssl -> write_sequence : ssl -> read_sequence , 8 );
654
664
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
+ }
656
668
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 ,
660
672
ssl -> cipher_info -> digest_size , hmac_buf );
661
673
662
- free (t_buf );
674
+ if (hmac_inplace ) {
675
+ memcpy (t_buf , tmp , prefix_size );
676
+ }
677
+ else {
678
+ free (t_buf );
679
+ }
663
680
#if 0
664
681
print_blob ("record" , hmac_header , SSL_RECORD_SIZE );
665
682
print_blob ("buf" , buf , buf_len );
@@ -1272,9 +1289,10 @@ int basic_read(SSL *ssl, uint8_t **in_data)
1272
1289
ssl -> need_bytes = (buf [3 ] << 8 ) + buf [4 ];
1273
1290
1274
1291
/* 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 )
1276
1293
{
1277
1294
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 );
1278
1296
goto error ;
1279
1297
}
1280
1298
@@ -1389,6 +1407,19 @@ int basic_read(SSL *ssl, uint8_t **in_data)
1389
1407
return ret ;
1390
1408
}
1391
1409
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
+
1392
1423
/**
1393
1424
* Do some basic checking of data and then perform the appropriate handshaking.
1394
1425
*/
@@ -1643,7 +1674,12 @@ void disposable_free(SSL *ssl)
1643
1674
free (ssl -> dc );
1644
1675
ssl -> dc = NULL ;
1645
1676
}
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
1647
1683
}
1648
1684
1649
1685
#ifndef CONFIG_SSL_SKELETON_MODE /* no session resumption in this mode */
@@ -1889,10 +1925,9 @@ int process_certificate(SSL *ssl, X509_CTX **x509_ctx)
1889
1925
1890
1926
EXP_FUNC int STDCALL ssl_match_fingerprint (const SSL * ssl , const uint8_t * fp )
1891
1927
{
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 );
1896
1931
}
1897
1932
1898
1933
#endif /* CONFIG_SSL_CERT_VERIFICATION */
0 commit comments