From 04484621d0f6f6921f7d01bbef98eff6ceca0fb1 Mon Sep 17 00:00:00 2001 From: Hanno Becker Date: Mon, 6 Aug 2018 09:49:38 +0100 Subject: [PATCH] Increment record sequence number in ssl_write_record() Previously, the record sequence number was incremented at the end of each successful call to mbedtls_ssl_flush_output(), which works as long as there is precisely one such call for each outgoing record. When packing multiple records into a single datagram, this property is no longer true, and instead the increment of the record sequence number must happen after the record has been prepared, and not after it has been dispatched. This commit moves the code for incrementing the record sequence number from mbedtls_ssl_flush_output() to ssl_write_record(). --- library/ssl_tls.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/library/ssl_tls.c b/library/ssl_tls.c index f2373eb51..9342321af 100644 --- a/library/ssl_tls.c +++ b/library/ssl_tls.c @@ -2648,7 +2648,7 @@ int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want ) int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl ) { int ret; - unsigned char *buf, i; + unsigned char *buf; MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) ); @@ -2691,16 +2691,6 @@ int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl ) ssl->out_left -= ret; } - for( i = 8; i > ssl_ep_len( ssl ); i-- ) - if( ++ssl->out_ctr[i - 1] != 0 ) - break; - - /* The loop goes to its end iff the counter is wrapping */ - if( i == ssl_ep_len( ssl ) ) - { - MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) ); - return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); - } MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) ); @@ -3236,6 +3226,16 @@ int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl ) MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network", ssl->out_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->out_msglen ); + for( i = 8; i > ssl_ep_len( ssl ); i-- ) + if( ++ssl->cur_out_ctr[i - 1] != 0 ) + break; + + /* The loop goes to its end iff the counter is wrapping */ + if( i == ssl_ep_len( ssl ) ) + { + MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) ); + return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING ); + } } if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )