@@ -53,6 +53,7 @@ static int verify_digest(SSL *ssl, int mode, const uint8_t *buf, int read_len);
5353static void * crypt_new (SSL * ssl , uint8_t * key , uint8_t * iv , int is_decrypt , void * cached );
5454static int send_raw_packet (SSL * ssl , uint8_t protocol );
5555static 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 */
259260EXP_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)
285287EXP_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 */
0 commit comments