Add function making space for current message reassembly
This commit adds a static function ssl_buffer_make_space() which
takes a buffer size as an argument and attempts to free as many
future message bufffers as necessary to ensure that the desired
amount of buffering space is available without violating the
total buffering limit set by MBEDTLS_SSL_DTLS_MAX_BUFFERING.
diff --git a/library/ssl_tls.c b/library/ssl_tls.c
index bb4c000..a1cf574 100644
--- a/library/ssl_tls.c
+++ b/library/ssl_tls.c
@@ -4438,6 +4438,35 @@
return( ret );
}
+static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
+ size_t desired )
+{
+ int offset;
+ mbedtls_ssl_handshake_params * const hs = ssl->handshake;
+
+
+ /* We don't have enough space to buffer the next expected
+ * handshake message. Remove buffers used for future msgs
+ * to gain space, starting with the most distant one. */
+ for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
+ offset >= 0; offset-- )
+ {
+ MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
+ offset ) );
+
+ ssl_buffering_free_slot( ssl, offset );
+
+ /* Check if we have enough space available now. */
+ if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
+ hs->buffering.total_bytes_buffered ) )
+ {
+ return( 0 );
+ }
+ }
+
+ return( -1 );
+}
+
static int ssl_buffer_message( mbedtls_ssl_context *ssl )
{
int ret = 0;
@@ -4522,8 +4551,6 @@
if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
hs->buffering.total_bytes_buffered ) )
{
- int offset;
-
if( recv_msg_seq_offset > 0 )
{
/* If we can't buffer a future message because
@@ -4540,27 +4567,7 @@
(unsigned) hs->buffering.total_bytes_buffered ) );
}
- /* We don't have enough space to buffer the next expected
- * handshake message. Remove buffers used for future msgs
- * to gain space, starting with the most distant one. */
- for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
- offset >= 0; offset-- )
- {
- MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
- offset ) );
-
- ssl_buffering_free_slot( ssl, offset );
-
- /* Check if we have enough space available now. */
- if( reassembly_buf_sz <=
- ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
- hs->buffering.total_bytes_buffered ) )
- {
- break;
- }
- }
-
- if( offset == -1 )
+ if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
{
MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reassembly of next message of size %u would exceed the compile-time limit %u (already %u bytes buffered) -- fail\n",
(unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,