Merge pull request #2963 from jiblime/zlib-fix into development
diff --git a/ChangeLog b/ChangeLog
index 58f0e72..e3e77f2 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,13 +1,69 @@
mbed TLS ChangeLog (Sorted per branch, date)
-= mbed TLS x.x.x branch released xxxx-xx-xx
+= mbed TLS X.X.X branch released XXXX-XX-XX
Bugfix
+ * Allow loading symlinked certificates. Fixes #3005. Reported and fixed
+ by Jonathan Bennett <JBennett@incomsystems.biz> via #3008.
+ * Fix an unchecked call to mbedtls_md() in the x509write module.
* Fix build failure with MBEDTLS_ZLIB_SUPPORT enabled. Reported by
Jack Lloyd in #2859. Fix submitted by jiblime in #2963.
= mbed TLS 2.20.0 branch released 2020-01-15
+Default behavior changes
+ * The initial seeding of a CTR_DRBG instance makes a second call to the
+ entropy function to obtain entropy for a nonce if the entropy size is less
+ than 3/2 times the key size. In case you want to disable the extra call to
+ grab entropy, you can call mbedtls_ctr_drbg_set_nonce_len() to force the
+ nonce length to 0.
+
+Security
+ * Enforce that mbedtls_entropy_func() gathers a total of
+ MBEDTLS_ENTROPY_BLOCK_SIZE bytes or more from strong sources. In the
+ default configuration, on a platform with a single entropy source, the
+ entropy module formerly only grabbed 32 bytes, which is good enough for
+ security if the source is genuinely strong, but less than the expected 64
+ bytes (size of the entropy accumulator).
+ * Zeroize local variables in mbedtls_internal_aes_encrypt() and
+ mbedtls_internal_aes_decrypt() before exiting the function. The value of
+ these variables can be used to recover the last round key. To follow best
+ practice and to limit the impact of buffer overread vulnerabilities (like
+ Heartbleed) we need to zeroize them before exiting the function.
+ Issue reported by Tuba Yavuz, Farhaan Fowze, Ken (Yihang) Bai,
+ Grant Hernandez, and Kevin Butler (University of Florida) and
+ Dave Tian (Purdue University).
+ * Fix side channel vulnerability in ECDSA. Our bignum implementation is not
+ constant time/constant trace, so side channel attacks can retrieve the
+ blinded value, factor it (as it is smaller than RSA keys and not guaranteed
+ to have only large prime factors), and then, by brute force, recover the
+ key. Reported by Alejandro Cabrera Aldaya and Billy Brumley.
+ * Fix side channel vulnerability in ECDSA key generation. Obtaining precise
+ timings on the comparison in the key generation enabled the attacker to
+ learn leading bits of the ephemeral key used during ECDSA signatures and to
+ recover the private key. Reported by Jeremy Dubeuf.
+ * Catch failure of AES functions in mbedtls_ctr_drbg_random(). Uncaught
+ failures could happen with alternative implementations of AES. Bug
+ reported and fix proposed by Johan Uppman Bruce and Christoffer Lauri,
+ Sectra.
+
+Features
+ * Key derivation inputs in the PSA API can now either come from a key object
+ or from a buffer regardless of the step type.
+ * The CTR_DRBG module can grab a nonce from the entropy source during the
+ initial seeding. The default nonce length is chosen based on the key size
+ to achieve the security strength defined by NIST SP 800-90A. You can
+ change it with mbedtls_ctr_drbg_set_nonce_len().
+ * Add ENUMERATED tag support to the ASN.1 module. Contributed by
+ msopiha-linaro in ARMmbed/mbed-crypto#307.
+
+API changes
+ * In the PSA API, forbid zero-length keys. To pass a zero-length input to a
+ key derivation function, use a buffer instead (this is now always
+ possible).
+ * Rename psa_asymmetric_sign() to psa_sign_hash() and
+ psa_asymmetric_verify() to psa_verify_hash().
+
Bugfix
* Fix an incorrect size in a debugging message. Reported and fix
submitted by irwir. Fixes #2717.
@@ -15,10 +71,40 @@
Reported and fix submitted by irwir. Fixes #2800.
* Remove a useless assignment. Reported and fix submitted by irwir.
Fixes #2801.
+ * Fix a buffer overflow in the PSA HMAC code when using a long key with an
+ unsupported algorithm. Fixes ARMmbed/mbed-crypto#254.
+ * Fix mbedtls_asn1_get_int to support any number of leading zeros. Credit
+ to OSS-Fuzz for finding a bug in an intermediate version of the fix.
+ * Fix mbedtls_asn1_get_bitstring_null to correctly parse bitstrings of at
+ most 2 bytes.
+ * mbedtls_ctr_drbg_set_entropy_len() and
+ mbedtls_hmac_drbg_set_entropy_len() now work if you call them before
+ mbedtls_ctr_drbg_seed() or mbedtls_hmac_drbg_seed().
+ * Fix some false-positive uninitialized variable warnings. Fix contributed
+ by apple-ihack-geek in #2663.
+
+Changes
+ * Remove the technical possibility to define custom mbedtls_md_info
+ structures, which was exposed only in an internal header.
+ * psa_close_key(0) and psa_destroy_key(0) now succeed (doing nothing, as
+ before).
+ * Variables containing error codes are now initialized to an error code
+ rather than success, so that coding mistakes or memory corruption tends to
+ cause functions to return this error code rather than a success. There are
+ no known instances where this changes the behavior of the library: this is
+ merely a robustness improvement. ARMmbed/mbed-crypto#323
+ * Remove a useless call to mbedtls_ecp_group_free(). Contributed by
+ Alexander Krizhanovsky in ARMmbed/mbed-crypto#210.
+ * Speed up PBKDF2 by caching the digest calculation. Contributed by Jack
+ Lloyd and Fortanix Inc in ARMmbed/mbed-crypto#277.
+ * Small performance improvement of mbedtls_mpi_div_mpi(). Contributed by
+ Alexander Krizhanovsky in ARMmbed/mbed-crypto#308.
= mbed TLS 2.19.1 branch released 2019-09-16
Features
+ * Declare include headers as PUBLIC to propagate to CMake project consumers
+ Contributed by Zachary J. Fields in PR #2949.
* Add nss_keylog to ssl_client2 and ssl_server2, enabling easier analysis of
TLS sessions with tools like Wireshark.
diff --git a/crypto b/crypto
index d27a884..1146b4e 160000
--- a/crypto
+++ b/crypto
@@ -1 +1 @@
-Subproject commit d27a88438fe785f0906c01e3216044d1c98be875
+Subproject commit 1146b4e06011b69a6437e6b728f2af043a06ec19
diff --git a/doxygen/input/doc_mainpage.h b/doxygen/input/doc_mainpage.h
index d5ead37..5b5aefb 100644
--- a/doxygen/input/doc_mainpage.h
+++ b/doxygen/input/doc_mainpage.h
@@ -24,7 +24,7 @@
*/
/**
- * @mainpage mbed TLS v2.19.1 source code documentation
+ * @mainpage mbed TLS v2.20.0 source code documentation
*
* This documentation describes the internal structure of mbed TLS. It was
* automatically generated from specially formatted comment blocks in
diff --git a/doxygen/mbedtls.doxyfile b/doxygen/mbedtls.doxyfile
index eb2d96e..47a9740 100644
--- a/doxygen/mbedtls.doxyfile
+++ b/doxygen/mbedtls.doxyfile
@@ -28,7 +28,7 @@
# identify the project. Note that if you do not use Doxywizard you need
# to put quotes around the project name if it contains spaces.
-PROJECT_NAME = "mbed TLS v2.19.1"
+PROJECT_NAME = "mbed TLS v2.20.0"
# The PROJECT_NUMBER tag can be used to enter a project or revision number.
# This could be handy for archiving the generated documentation or
diff --git a/include/CMakeLists.txt b/include/CMakeLists.txt
index c2f2bd4..a3ee3e4 100644
--- a/include/CMakeLists.txt
+++ b/include/CMakeLists.txt
@@ -1,5 +1,11 @@
option(INSTALL_MBEDTLS_HEADERS "Install mbed TLS headers." ON)
+# Declare include headers as PUBLIC to propogate to project consumers
+target_include_directories(
+ mbedtls
+ PUBLIC ${CMAKE_CURRENT_LIST_DIR}
+)
+
if(INSTALL_MBEDTLS_HEADERS)
file(GLOB headers "mbedtls/*.h")
diff --git a/include/mbedtls/version.h b/include/mbedtls/version.h
index ae694ee..d4e5d54 100644
--- a/include/mbedtls/version.h
+++ b/include/mbedtls/version.h
@@ -39,17 +39,17 @@
* Major, Minor, Patchlevel
*/
#define MBEDTLS_VERSION_MAJOR 2
-#define MBEDTLS_VERSION_MINOR 19
-#define MBEDTLS_VERSION_PATCH 1
+#define MBEDTLS_VERSION_MINOR 20
+#define MBEDTLS_VERSION_PATCH 0
/**
* The single version number has the following structure:
* MMNNPP00
* Major version | Minor version | Patch version
*/
-#define MBEDTLS_VERSION_NUMBER 0x02130100
-#define MBEDTLS_VERSION_STRING "2.19.1"
-#define MBEDTLS_VERSION_STRING_FULL "mbed TLS 2.19.1"
+#define MBEDTLS_VERSION_NUMBER 0x02140000
+#define MBEDTLS_VERSION_STRING "2.20.0"
+#define MBEDTLS_VERSION_STRING_FULL "mbed TLS 2.20.0"
#if defined(MBEDTLS_VERSION_C)
diff --git a/library/CMakeLists.txt b/library/CMakeLists.txt
index 5e36a5b..d990d43 100644
--- a/library/CMakeLists.txt
+++ b/library/CMakeLists.txt
@@ -174,14 +174,14 @@
if(USE_SHARED_MBEDTLS_LIBRARY)
add_library(mbedx509 SHARED ${src_x509})
- set_target_properties(mbedx509 PROPERTIES VERSION 2.19.1 SOVERSION 1)
+ set_target_properties(mbedx509 PROPERTIES VERSION 2.20.0 SOVERSION 1)
target_link_libraries(mbedx509 ${libs} mbedcrypto)
target_include_directories(mbedx509
PUBLIC ${MBEDTLS_DIR}/include/
PUBLIC ${MBEDTLS_DIR}/crypto/include/)
add_library(mbedtls SHARED ${src_tls})
- set_target_properties(mbedtls PROPERTIES VERSION 2.19.1 SOVERSION 13)
+ set_target_properties(mbedtls PROPERTIES VERSION 2.20.0 SOVERSION 13)
target_link_libraries(mbedtls ${libs} mbedx509)
target_include_directories(mbedtls
PUBLIC ${MBEDTLS_DIR}/include/
diff --git a/library/Makefile b/library/Makefile
index bc9363d..6fc110c 100644
--- a/library/Makefile
+++ b/library/Makefile
@@ -37,7 +37,7 @@
SOEXT_TLS=so.13
SOEXT_X509=so.1
-SOEXT_CRYPTO=so.3
+SOEXT_CRYPTO=so.4
# Set AR_DASH= (empty string) to use an ar implementation that does not accept
# the - prefix for command line options (e.g. llvm-ar)
diff --git a/library/x509_crt.c b/library/x509_crt.c
index 38e69cc..ca60011 100644
--- a/library/x509_crt.c
+++ b/library/x509_crt.c
@@ -1613,7 +1613,7 @@
goto cleanup;
}
- if( !S_ISREG( sb.st_mode ) )
+ if( !( S_ISREG( sb.st_mode ) || S_ISLNK( sb.st_mode ) ) )
continue;
// Ignore parse errors
diff --git a/library/x509write_csr.c b/library/x509write_csr.c
index 0c3c396..7c51798 100644
--- a/library/x509write_csr.c
+++ b/library/x509write_csr.c
@@ -214,7 +214,9 @@
return( MBEDTLS_ERR_X509_FATAL_ERROR );
}
#else /* MBEDTLS_USE_PSA_CRYPTO */
- mbedtls_md( mbedtls_md_info_from_type( ctx->md_alg ), c, len, hash );
+ ret = mbedtls_md( mbedtls_md_info_from_type( ctx->md_alg ), c, len, hash );
+ if( ret != 0 )
+ return( ret );
#endif
if( ( ret = mbedtls_pk_sign( ctx->key, ctx->md_alg, hash, 0, sig, &sig_len,
f_rng, p_rng ) ) != 0 )
diff --git a/programs/ssl/ssl_client2.c b/programs/ssl/ssl_client2.c
index 8f0d3b5..c188900 100644
--- a/programs/ssl/ssl_client2.c
+++ b/programs/ssl/ssl_client2.c
@@ -619,6 +619,7 @@
if( fwrite( nss_keylog_line, 1, len, f ) != len )
{
ret = -1;
+ fclose( f );
goto exit;
}
diff --git a/tests/suites/host_test.function b/tests/suites/host_test.function
index 9e56ca3..b956c0c 100644
--- a/tests/suites/host_test.function
+++ b/tests/suites/host_test.function
@@ -525,15 +525,6 @@
mbedtls_memory_buffer_alloc_init( alloc_buf, sizeof( alloc_buf ) );
#endif
- if( outcome_file_name != NULL )
- {
- outcome_file = fopen( outcome_file_name, "a" );
- if( outcome_file == NULL )
- {
- mbedtls_fprintf( stderr, "Unable to open outcome file. Continuing anyway.\n" );
- }
- }
-
/*
* The C standard doesn't guarantee that all-bits-0 is the representation
* of a NULL pointer. We do however use that in our code for initializing
@@ -555,6 +546,15 @@
return( 1 );
}
+ if( outcome_file_name != NULL )
+ {
+ outcome_file = fopen( outcome_file_name, "a" );
+ if( outcome_file == NULL )
+ {
+ mbedtls_fprintf( stderr, "Unable to open outcome file. Continuing anyway.\n" );
+ }
+ }
+
while( arg_index < argc )
{
next_arg = argv[arg_index];
@@ -607,6 +607,8 @@
{
mbedtls_fprintf( stderr, "Failed to open test file: %s\n",
test_filename );
+ if( outcome_file != NULL )
+ fclose( outcome_file );
return( 1 );
}
diff --git a/tests/suites/test_suite_ssl.data b/tests/suites/test_suite_ssl.data
index 83ef691..3326b43 100644
--- a/tests/suites/test_suite_ssl.data
+++ b/tests/suites/test_suite_ssl.data
@@ -35,34 +35,55 @@
ssl_mock_sanity:
Test mock blocking TCP connection
-ssl_mock_tcp:1:0:0
+ssl_mock_tcp:1
-Test mock non-blocking TCP connection: would not block
-ssl_mock_tcp:0:0:0
-
-Test mock non-blocking TCP connection: client would block
-ssl_mock_tcp:0:0xB509:0
-
-Test mock non-blocking TCP connection: server would block
-ssl_mock_tcp:0:0x0FB1:0
-
-Test mock non-blocking TCP connection: both peers would block
-ssl_mock_tcp:0:0x1111:0xEEEE
+Test mock non-blocking TCP connection
+ssl_mock_tcp:0
Test mock blocking TCP connection (interleaving)
-ssl_mock_tcp_interleaving:1:0:0
+ssl_mock_tcp_interleaving:1
-Test mock non-blocking TCP connection: would not block (interleaving)
-ssl_mock_tcp_interleaving:0:0:0
+Test mock non-blocking TCP connection (interleaving)
+ssl_mock_tcp_interleaving:0
-Test mock non-blocking TCP connection: client would block (interleaving)
-ssl_mock_tcp_interleaving:0:0xB509:0
+Message queue - sanity
+ssl_message_queue_sanity:
-Test mock non-blocking TCP connection: server would block (interleaving)
-ssl_mock_tcp_interleaving:0:0x0FB1:0
+Message queue - basic test
+ssl_message_queue_basic:
-Test mock non-blocking TCP connection: both peers would block (interleaving)
-ssl_mock_tcp_interleaving:0:0x1111:0xEEEE
+Message queue - overflow/underflow
+ssl_message_queue_overflow_underflow:
+
+Message queue - interleaved
+ssl_message_queue_interleaved:
+
+Message queue - insufficient buffer
+ssl_message_queue_insufficient_buffer:
+
+Message transport mock - uninitialized structures
+ssl_message_mock_uninitialized:
+
+Message transport mock - basic test
+ssl_message_mock_basic:
+
+Message transport mock - queue overflow/underflow
+ssl_message_mock_queue_overflow_underflow:
+
+Message transport mock - socket overflow
+ssl_message_mock_socket_overflow:
+
+Message transport mock - truncated message
+ssl_message_mock_truncated:
+
+Message transport mock - socket read error
+ssl_message_mock_socket_read_error:
+
+Message transport mock - one-way interleaved sends/reads
+ssl_message_mock_interleaved_one_way:
+
+Message transport mock - two-way interleaved sends/reads
+ssl_message_mock_interleaved_two_ways:
SSL DTLS replay: initial state, seqnum 0
ssl_dtls_replay:"":"000000000000":0
diff --git a/tests/suites/test_suite_ssl.function b/tests/suites/test_suite_ssl.function
index a57e256..2d080c7 100644
--- a/tests/suites/test_suite_ssl.function
+++ b/tests/suites/test_suite_ssl.function
@@ -77,34 +77,47 @@
return ( input_len == 0 ) ? 0 : -1;
}
- /* Calculate the number of bytes that need to be placed at lower memory
- * address */
- if( buf->start + buf->content_length + input_len
- > buf->capacity )
+ /* Check if the buffer has not come full circle and free space is not in
+ * the middle */
+ if( buf->start + buf->content_length < buf->capacity )
{
- overflow = ( buf->start + buf->content_length + input_len )
- % buf->capacity;
+
+ /* Calculate the number of bytes that need to be placed at lower memory
+ * address */
+ if( buf->start + buf->content_length + input_len
+ > buf->capacity )
+ {
+ overflow = ( buf->start + buf->content_length + input_len )
+ % buf->capacity;
+ }
+
+ memcpy( buf->buffer + buf->start + buf->content_length, input,
+ input_len - overflow );
+ memcpy( buf->buffer, input + input_len - overflow, overflow );
+
+ }
+ else
+ {
+ /* The buffer has come full circle and free space is in the middle */
+ memcpy( buf->buffer + buf->start + buf->content_length - buf->capacity,
+ input, input_len );
}
- memcpy( buf->buffer + buf->start + buf->content_length, input,
- input_len - overflow );
- memcpy( buf->buffer, input + input_len - overflow, overflow );
buf->content_length += input_len;
-
return input_len;
}
/*
- * Gets \p output_len bytes from the \p output buffer into the ring buffer
- * \p buf.
+ * Gets \p output_len bytes from the ring buffer \p buf into the
+ * \p output buffer. The output buffer can be NULL, in this case a part of the
+ * ring buffer will be dropped, if the requested length is available.
*
* \p buf must have been initialized and set up by calling
* `mbedtls_test_buffer_init()` and `mbedtls_test_buffer_setup()`.
*
* \retval \p output_len, if the data is available.
* \retval 0 <= value < \p output_len, if the data is not available.
- * \retval -1, if \buf is NULL, it hasn't been set up or \p output_len is not
- * zero and \p output is NULL
+ * \retval -1, if \buf is NULL or it hasn't been set up.
*/
int mbedtls_test_buffer_get( mbedtls_test_buffer *buf,
unsigned char* output, size_t output_len )
@@ -114,10 +127,8 @@
if( ( buf == NULL ) || ( buf->buffer == NULL ) )
return -1;
- if( output == NULL )
- {
- return ( output_len == 0 ) ? 0 : -1;
- }
+ if( output == NULL && output_len == 0 )
+ return 0;
if( buf->content_length < output_len )
output_len = buf->content_length;
@@ -129,8 +140,12 @@
overflow = ( buf->start + output_len ) % buf->capacity;
}
- memcpy( output, buf->buffer + buf->start, output_len - overflow );
- memcpy( output + output_len - overflow, buf->buffer, overflow );
+ if( output != NULL )
+ {
+ memcpy( output, buf->buffer + buf->start, output_len - overflow );
+ memcpy( output + output_len - overflow, buf->buffer, overflow );
+ }
+
buf->content_length -= output_len;
buf->start = ( buf->start + output_len ) % buf->capacity;
@@ -138,6 +153,136 @@
}
/*
+ * Errors used in the message transport mock tests
+ */
+ #define MBEDTLS_TEST_ERROR_ARG_NULL -11
+ #define MBEDTLS_TEST_ERROR_QUEUE_FULL -22
+ #define MBEDTLS_TEST_ERROR_QUEUE_EMPTY -33
+ #define MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED -44
+
+/*
+ * Context for a message metadata queue (fifo) that is on top of the ring buffer.
+ */
+typedef struct mbedtls_test_message_queue
+{
+ size_t *messages;
+ int pos;
+ int num;
+ int capacity;
+} mbedtls_test_message_queue;
+
+/*
+ * Setup and free functions for the message metadata queue.
+ *
+ * \p capacity describes the number of message metadata chunks that can be held
+ * within the queue.
+ *
+ * \retval 0, if a metadata queue of a given length can be allocated.
+ * \retval MBEDTLS_ERR_SSL_ALLOC_FAILED, if allocation failed.
+ */
+int mbedtls_test_message_queue_setup( mbedtls_test_message_queue *queue,
+ size_t capacity )
+{
+ queue->messages = (size_t*) mbedtls_calloc( capacity, sizeof(size_t) );
+ if( NULL == queue->messages )
+ return MBEDTLS_ERR_SSL_ALLOC_FAILED;
+
+ queue->capacity = capacity;
+ queue->pos = 0;
+ queue->num = 0;
+
+ return 0;
+}
+
+void mbedtls_test_message_queue_free( mbedtls_test_message_queue *queue )
+{
+ if( queue == NULL )
+ return;
+
+ if( queue->messages != NULL )
+ mbedtls_free( queue->messages );
+
+ memset( queue, 0, sizeof( *queue ) );
+}
+
+/*
+ * Push message length information onto the message metadata queue.
+ * This will become the last element to leave it (fifo).
+ *
+ * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null.
+ * \retval MBEDTLS_TEST_ERROR_QUEUE_FULL, if the queue is full.
+ * \retval \p len, if the push was successful.
+ */
+int mbedtls_test_message_queue_push_info( mbedtls_test_message_queue *queue,
+ size_t len )
+{
+ int place;
+ if( queue == NULL )
+ return MBEDTLS_TEST_ERROR_ARG_NULL;
+
+ if( queue->num >= queue->capacity )
+ return MBEDTLS_TEST_ERROR_QUEUE_FULL;
+
+ place = ( queue->pos + queue->num ) % queue->capacity;
+ queue->messages[place] = len;
+ queue->num++;
+ return len;
+}
+
+/*
+ * Pop information about the next message length from the queue. This will be
+ * the oldest inserted message length(fifo). \p msg_len can be null, in which
+ * case the data will be popped from the queue but not copied anywhere.
+ *
+ * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null.
+ * \retval MBEDTLS_TEST_ERROR_QUEUE_EMPTY, if the queue is empty.
+ * \retval message length, if the pop was successful, up to the given
+ \p buf_len.
+ */
+int mbedtls_test_message_queue_pop_info( mbedtls_test_message_queue *queue,
+ size_t buf_len )
+{
+ size_t message_length;
+ if( queue == NULL )
+ return MBEDTLS_TEST_ERROR_ARG_NULL;
+ if( queue->num == 0 )
+ return MBEDTLS_TEST_ERROR_QUEUE_EMPTY;
+
+ message_length = queue->messages[queue->pos];
+ queue->messages[queue->pos] = 0;
+ queue->num--;
+ queue->pos++;
+ queue->pos %= queue->capacity;
+ if( queue->pos < 0 )
+ queue->pos += queue->capacity;
+
+ return ( message_length > buf_len ) ? buf_len : message_length;
+}
+
+/*
+ * Take a peek on the info about the next message length from the queue.
+ * This will be the oldest inserted message length(fifo).
+ *
+ * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null.
+ * \retval MBEDTLS_TEST_ERROR_QUEUE_EMPTY, if the queue is empty.
+ * \retval 0, if the peek was successful.
+ * \retval MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED, if the given buffer length is
+ * too small to fit the message. In this case the \p msg_len will be
+ * set to the full message length so that the
+ * caller knows what portion of the message can be dropped.
+ */
+int mbedtls_test_message_queue_peek_info( mbedtls_test_message_queue *queue,
+ size_t buf_len, size_t* msg_len )
+{
+ if( queue == NULL || msg_len == NULL )
+ return MBEDTLS_TEST_ERROR_ARG_NULL;
+ if( queue->num == 0 )
+ return MBEDTLS_TEST_ERROR_QUEUE_EMPTY;
+
+ *msg_len = queue->messages[queue->pos];
+ return ( *msg_len > buf_len ) ? MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED : 0;
+}
+/*
* Context for the I/O callbacks simulating network connection.
*/
@@ -146,7 +291,6 @@
typedef struct mbedtls_mock_socket
{
int status;
- uint32_t blocking_pattern;
mbedtls_test_buffer *input;
mbedtls_test_buffer *output;
struct mbedtls_mock_socket *peer;
@@ -212,20 +356,7 @@
{
int ret = -1;
- peer1->input = peer2->output =
- (mbedtls_test_buffer*) mbedtls_calloc( 1, sizeof(mbedtls_test_buffer) );
- if( peer1->input == NULL )
- {
- ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
- goto exit;
- }
- mbedtls_test_buffer_init( peer1->input );
- if( 0 != ( ret = mbedtls_test_buffer_setup( peer1->input, bufsize ) ) )
- {
- goto exit;
- }
-
- peer1->output = peer2->input =
+ peer1->output =
(mbedtls_test_buffer*) mbedtls_calloc( 1, sizeof(mbedtls_test_buffer) );
if( peer1->output == NULL )
{
@@ -238,8 +369,23 @@
goto exit;
}
+ peer2->output =
+ (mbedtls_test_buffer*) mbedtls_calloc( 1, sizeof(mbedtls_test_buffer) );
+ if( peer2->output == NULL )
+ {
+ ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
+ goto exit;
+ }
+ mbedtls_test_buffer_init( peer2->output );
+ if( 0 != ( ret = mbedtls_test_buffer_setup( peer2->output, bufsize ) ) )
+ {
+ goto exit;
+ }
+
peer1->peer = peer2;
peer2->peer = peer1;
+ peer1->input = peer2->output;
+ peer2->input = peer1->output;
peer1->status = peer2->status = MBEDTLS_MOCK_SOCKET_CONNECTED;
ret = 0;
@@ -256,26 +402,6 @@
}
/*
- * Set the blocking pattern for the socket.
- *
- * For every bit of \p blocking_pattern set to one the socket will simulate a
- * "would block" event. The bits are processed starting with the least
- * significant bit and every call to a non-blocking I/O function consumes one.
- *
- * The behaviour of blocking I/O functions remains unchanged.
- */
-int mbedtls_mock_socket_set_block( mbedtls_mock_socket* socket,
- uint32_t blocking_pattern )
-{
- if( socket == NULL )
- return -1;
-
- socket->blocking_pattern = blocking_pattern;
-
- return 0;
-}
-
-/*
* Callbacks for simulating blocking I/O over connection-oriented transport.
*/
@@ -310,14 +436,11 @@
if( socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED )
return -1;
- if( socket->blocking_pattern & 1 )
+ if( socket->output->capacity == socket->output->content_length )
{
- socket->blocking_pattern >>= 1;
return MBEDTLS_ERR_SSL_WANT_WRITE;
}
- socket->blocking_pattern >>= 1;
-
return mbedtls_test_buffer_put( socket->output, buf, len );
}
@@ -328,17 +451,173 @@
if( socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED )
return -1;
- if( socket->blocking_pattern & 1 )
+ if( socket->input->content_length == 0)
{
- socket->blocking_pattern >>= 1;
return MBEDTLS_ERR_SSL_WANT_READ;
}
- socket->blocking_pattern >>= 1;
-
return mbedtls_test_buffer_get( socket->input, buf, len );
}
+/* Errors used in the message socket mocks */
+
+#define MBEDTLS_TEST_ERROR_CONTEXT_ERROR -55
+#define MBEDTLS_TEST_ERROR_SEND_FAILED -66
+#define MBEDTLS_TEST_ERROR_RECV_FAILED -77
+
+/*
+ * Structure used as an addon, or a wrapper, around the mocked sockets.
+ * Contains an input queue, to which the other socket pushes metadata,
+ * and an output queue, to which this one pushes metadata. This context is
+ * considered as an owner of the input queue only, which is initialized and
+ * freed in the respective setup and free calls.
+ */
+typedef struct mbedtls_test_message_socket_context
+{
+ mbedtls_test_message_queue* queue_input;
+ mbedtls_test_message_queue* queue_output;
+ mbedtls_mock_socket* socket;
+} mbedtls_test_message_socket_context;
+
+/*
+ * Setup a given mesasge socket context including initialization of
+ * input/output queues to a chosen capacity of messages. Also set the
+ * corresponding mock socket.
+ *
+ * \retval 0, if everything succeeds.
+ * \retval MBEDTLS_ERR_SSL_ALLOC_FAILED, if allocation of a message
+ * queue failed.
+ */
+int mbedtls_message_socket_setup( mbedtls_test_message_queue* queue_input,
+ mbedtls_test_message_queue* queue_output,
+ size_t queue_capacity,
+ mbedtls_mock_socket* socket,
+ mbedtls_test_message_socket_context* ctx )
+{
+ int ret = mbedtls_test_message_queue_setup( queue_input, queue_capacity );
+ if( ret != 0 )
+ return ret;
+ ctx->queue_input = queue_input;
+ ctx->queue_output = queue_output;
+ ctx->socket = socket;
+ mbedtls_mock_socket_init( socket );
+
+ return 0;
+}
+
+/*
+ * Close a given message socket context, along with the socket itself. Free the
+ * memory allocated by the input queue.
+ */
+void mbedtls_message_socket_close( mbedtls_test_message_socket_context* ctx )
+{
+ if( ctx == NULL )
+ return;
+
+ mbedtls_test_message_queue_free( ctx->queue_input );
+ mbedtls_mock_socket_close( ctx->socket );
+ memset( ctx, 0, sizeof( *ctx ) );
+}
+
+/*
+ * Send one message through a given message socket context.
+ *
+ * \retval \p len, if everything succeeds.
+ * \retval MBEDTLS_TEST_ERROR_CONTEXT_ERROR, if any of the needed context
+ * elements or the context itself is null.
+ * \retval MBEDTLS_TEST_ERROR_SEND_FAILED if mbedtls_mock_tcp_send_b failed.
+ * \retval MBEDTLS_TEST_ERROR_QUEUE_FULL, if the output queue is full.
+ *
+ * This function will also return any error from
+ * mbedtls_test_message_queue_push_info.
+ */
+int mbedtls_mock_tcp_send_msg( void *ctx, const unsigned char *buf, size_t len )
+{
+ mbedtls_test_message_queue* queue;
+ mbedtls_mock_socket* socket;
+ mbedtls_test_message_socket_context *context = (mbedtls_test_message_socket_context*) ctx;
+
+ if( context == NULL || context->socket == NULL
+ || context->queue_output == NULL )
+ {
+ return MBEDTLS_TEST_ERROR_CONTEXT_ERROR;
+ }
+
+ queue = context->queue_output;
+ socket = context->socket;
+
+ if( queue->num >= queue->capacity )
+ return MBEDTLS_TEST_ERROR_QUEUE_FULL;
+
+ if( mbedtls_mock_tcp_send_b( socket, buf, len ) != (int) len )
+ return MBEDTLS_TEST_ERROR_SEND_FAILED;
+
+ return mbedtls_test_message_queue_push_info( queue, len );
+}
+
+/*
+ * Receive one message from a given message socket context and return message
+ * length or an error.
+ *
+ * \retval message length, if everything succeeds.
+ * \retval MBEDTLS_TEST_ERROR_CONTEXT_ERROR, if any of the needed context
+ * elements or the context itself is null.
+ * \retval MBEDTLS_TEST_ERROR_RECV_FAILED if mbedtls_mock_tcp_recv_b failed.
+ *
+ * This function will also return any error other than
+ * MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED from mbedtls_test_message_queue_peek_info.
+ */
+int mbedtls_mock_tcp_recv_msg( void *ctx, unsigned char *buf, size_t buf_len )
+{
+ mbedtls_test_message_queue* queue;
+ mbedtls_mock_socket* socket;
+ mbedtls_test_message_socket_context *context = (mbedtls_test_message_socket_context*) ctx;
+ size_t drop_len;
+ size_t msg_len;
+ int ret;
+
+ if( context == NULL || context->socket == NULL
+ || context->queue_input == NULL )
+ {
+ return MBEDTLS_TEST_ERROR_CONTEXT_ERROR;
+ }
+
+ queue = context->queue_input;
+ socket = context->socket;
+
+ /* Peek first, so that in case of a socket error the data remains in
+ * the queue. */
+ ret = mbedtls_test_message_queue_peek_info( queue, buf_len, &msg_len );
+ if( ret == MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED )
+ {
+ /* Calculate how much to drop */
+ drop_len = msg_len - buf_len;
+
+ /* Set the requested message len to be buffer length */
+ msg_len = buf_len;
+ } else if( ret != 0 )
+ {
+ return ret;
+ }
+
+ if( mbedtls_mock_tcp_recv_b( socket, buf, msg_len ) != (int) msg_len )
+ return MBEDTLS_TEST_ERROR_RECV_FAILED;
+
+ if( ret == MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED )
+ {
+ /* Drop the remaining part of the message */
+ if( mbedtls_mock_tcp_recv_b( socket, NULL, drop_len ) != (int) drop_len )
+ {
+ /* Inconsistent state - part of the message was read,
+ * and a part couldn't. Not much we can do here, but it should not
+ * happen in test environment, unless forced manually. */
+ }
+ }
+ mbedtls_test_message_queue_pop_info( queue, buf_len );
+
+ return msg_len;
+}
+
/*
* Helper function setting up inverse record transformations
* using given cipher, hash, EtM mode, authentication tag length,
@@ -714,8 +993,7 @@
TEST_ASSERT( mbedtls_test_buffer_get( NULL, output, sizeof( output ) )
== -1 );
TEST_ASSERT( mbedtls_test_buffer_put( NULL, NULL, sizeof( input ) ) == -1 );
- TEST_ASSERT( mbedtls_test_buffer_get( NULL, NULL, sizeof( output ) )
- == -1 );
+
TEST_ASSERT( mbedtls_test_buffer_put( NULL, NULL, 0 ) == -1 );
TEST_ASSERT( mbedtls_test_buffer_get( NULL, NULL, 0 ) == -1 );
@@ -727,22 +1005,33 @@
TEST_ASSERT( mbedtls_test_buffer_get( &buf, output, sizeof( output ) )
== -1 );
TEST_ASSERT( mbedtls_test_buffer_put( &buf, NULL, sizeof( input ) ) == -1 );
- TEST_ASSERT( mbedtls_test_buffer_get( &buf, NULL, sizeof( output ) )
- == -1 );
+
TEST_ASSERT( mbedtls_test_buffer_put( &buf, NULL, 0 ) == -1 );
TEST_ASSERT( mbedtls_test_buffer_get( &buf, NULL, 0 ) == -1 );
- /* Make sure calling put end get on NULL input and output only results in
- * error if the length is not zero. */
+ /* Make sure calling put and get on NULL input only results in
+ * error if the length is not zero, and that a NULL output is valid for data
+ * dropping.
+ */
TEST_ASSERT( mbedtls_test_buffer_setup( &buf, sizeof( input ) ) == 0 );
TEST_ASSERT( mbedtls_test_buffer_put( &buf, NULL, sizeof( input ) ) == -1 );
TEST_ASSERT( mbedtls_test_buffer_get( &buf, NULL, sizeof( output ) )
- == -1 );
+ == 0 );
TEST_ASSERT( mbedtls_test_buffer_put( &buf, NULL, 0 ) == 0 );
TEST_ASSERT( mbedtls_test_buffer_get( &buf, NULL, 0 ) == 0 );
+ /* Make sure calling put several times in the row is safe */
+
+ TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, sizeof( input ) )
+ == sizeof( input ) );
+ TEST_ASSERT( mbedtls_test_buffer_get( &buf, output, 2 ) == 2 );
+ TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, 1 ) == 1 );
+ TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, 2 ) == 1 );
+ TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, 2 ) == 0 );
+
+
exit:
mbedtls_test_buffer_free( &buf );
@@ -905,9 +1194,10 @@
*/
/* BEGIN_CASE */
-void ssl_mock_tcp( int blocking, int client_pattern, int server_pattern )
+void ssl_mock_tcp( int blocking )
{
enum { MSGLEN = 105 };
+ enum { BUFLEN = MSGLEN / 5 };
unsigned char message[MSGLEN];
unsigned char received[MSGLEN];
mbedtls_mock_socket client;
@@ -916,8 +1206,6 @@
int send_ret, recv_ret;
mbedtls_ssl_send_t *send;
mbedtls_ssl_recv_t *recv;
- uint32_t client_block = client_pattern;
- uint32_t server_block = server_pattern;
unsigned i;
if( blocking == 0 )
@@ -942,10 +1230,7 @@
}
/* Make sure that sending a message takes a few iterations. */
- TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
- MSGLEN / 5 ) );
- TEST_ASSERT( 0 == mbedtls_mock_socket_set_block( &client, client_block ) );
- TEST_ASSERT( 0 == mbedtls_mock_socket_set_block( &server, server_block ) );
+ TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, BUFLEN ) );
/* Send the message to the server */
send_ret = recv_ret = 1;
@@ -954,28 +1239,56 @@
{
send_ret = send( &client, message + written, MSGLEN - written );
- if( ( blocking == 0 ) && ( client_block & 1 ) )
+ TEST_ASSERT( send_ret >= 0 );
+ TEST_ASSERT( send_ret <= BUFLEN );
+ written += send_ret;
+
+ /* If the buffer is full we can test blocking and non-blocking send */
+ if ( send_ret == BUFLEN )
{
- TEST_ASSERT( send_ret == MBEDTLS_ERR_SSL_WANT_WRITE );
+ int blocking_ret = send( &client, message , 1 );
+ if ( blocking )
+ {
+ TEST_ASSERT( blocking_ret == 0 );
+ }
+ else
+ {
+ TEST_ASSERT( blocking_ret == MBEDTLS_ERR_SSL_WANT_WRITE );
+ }
}
- else
- {
- TEST_ASSERT( send_ret >= 0 );
- written += send_ret;
- }
- client_block >>= 1;
recv_ret = recv( &server, received + read, MSGLEN - read );
- if( ( blocking == 0 ) && ( server_block & 1 ) )
+
+ /* The result depends on whether any data was sent */
+ if ( send_ret > 0 )
{
- TEST_ASSERT( recv_ret == MBEDTLS_ERR_SSL_WANT_READ );
+ TEST_ASSERT( recv_ret > 0 );
+ TEST_ASSERT( recv_ret <= BUFLEN );
+ read += recv_ret;
+ }
+ else if( blocking )
+ {
+ TEST_ASSERT( recv_ret == 0 );
}
else
{
- TEST_ASSERT( recv_ret >= 0 );
- read += recv_ret;
+ TEST_ASSERT( recv_ret == MBEDTLS_ERR_SSL_WANT_READ );
+ recv_ret = 0;
}
- server_block >>= 1;
+
+ /* If the buffer is empty we can test blocking and non-blocking read */
+ if ( recv_ret == BUFLEN )
+ {
+ int blocking_ret = recv( &server, received, 1 );
+ if ( blocking )
+ {
+ TEST_ASSERT( blocking_ret == 0 );
+ }
+ else
+ {
+ TEST_ASSERT( blocking_ret == MBEDTLS_ERR_SSL_WANT_READ );
+ }
+ }
}
TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
@@ -993,11 +1306,11 @@
*/
/* BEGIN_CASE */
-void ssl_mock_tcp_interleaving( int blocking,
- int client_pattern, int server_pattern )
+void ssl_mock_tcp_interleaving( int blocking )
{
enum { ROUNDS = 2 };
enum { MSGLEN = 105 };
+ enum { BUFLEN = MSGLEN / 5 };
unsigned char message[ROUNDS][MSGLEN];
unsigned char received[ROUNDS][MSGLEN];
mbedtls_mock_socket client;
@@ -1009,8 +1322,6 @@
unsigned i, j, progress;
mbedtls_ssl_send_t *send;
mbedtls_ssl_recv_t *recv;
- uint32_t client_block = client_pattern;
- uint32_t server_block = server_pattern;
if( blocking == 0 )
{
@@ -1037,10 +1348,7 @@
}
/* Make sure that sending a message takes a few iterations. */
- TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
- MSGLEN / 5 ) );
- TEST_ASSERT( 0 == mbedtls_mock_socket_set_block( &client, client_block ) );
- TEST_ASSERT( 0 == mbedtls_mock_socket_set_block( &server, server_block ) );
+ TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, BUFLEN ) );
/* Send the message from both sides, interleaving. */
progress = 1;
@@ -1053,72 +1361,80 @@
* of at least one byte on either side. */
while( progress != 0 )
{
- send_ret[0] = send( &client, message[0] + written[0],
- MSGLEN - written[0] );
- if( ( blocking == 0 ) && ( client_block & 1 ) )
- {
- TEST_ASSERT( send_ret[0] == MBEDTLS_ERR_SSL_WANT_WRITE );
- }
- else
- {
- TEST_ASSERT( send_ret[0] >= 0 );
- written[0] += send_ret[0];
- }
- client_block >>= 1;
+ mbedtls_mock_socket *socket;
- send_ret[1] = send( &server, message[1] + written[1],
- MSGLEN - written[1] );
- if( ( blocking == 0 ) && ( server_block & 1 ) )
+ for( i = 0; i < ROUNDS; i++ )
{
- TEST_ASSERT( send_ret[1] == MBEDTLS_ERR_SSL_WANT_WRITE );
- }
- else
- {
- TEST_ASSERT( send_ret[1] >= 0 );
- written[1] += send_ret[1];
- }
- server_block >>= 1;
+ /* First sending is from the client */
+ socket = ( i % 2 == 0 ) ? ( &client ) : ( &server );
- recv_ret[0] = recv( &server, received[0] + read[0],
- MSGLEN - read[0] );
- if( ( blocking == 0 ) && ( server_block & 1 ) )
- {
- TEST_ASSERT( recv_ret[0] == MBEDTLS_ERR_SSL_WANT_READ );
- }
- else
- {
- TEST_ASSERT( recv_ret[0] >= 0 );
- read[0] += recv_ret[0];
- }
- server_block >>= 1;
+ send_ret[i] = send( socket, message[i] + written[i],
+ MSGLEN - written[i] );
+ TEST_ASSERT( send_ret[i] >= 0 );
+ TEST_ASSERT( send_ret[i] <= BUFLEN );
+ written[i] += send_ret[i];
- recv_ret[1] = recv( &client, received[1] + read[1],
- MSGLEN - read[1] );
- if( ( blocking == 0 ) && ( client_block & 1 ) )
- {
- TEST_ASSERT( recv_ret[1] == MBEDTLS_ERR_SSL_WANT_READ );
+ /* If the buffer is full we can test blocking and non-blocking
+ * send */
+ if ( send_ret[i] == BUFLEN )
+ {
+ int blocking_ret = send( socket, message[i] , 1 );
+ if ( blocking )
+ {
+ TEST_ASSERT( blocking_ret == 0 );
+ }
+ else
+ {
+ TEST_ASSERT( blocking_ret == MBEDTLS_ERR_SSL_WANT_WRITE );
+ }
+ }
}
- else
+
+ for( i = 0; i < ROUNDS; i++ )
{
- TEST_ASSERT( recv_ret[1] >= 0 );
- read[1] += recv_ret[1];
+ /* First receiving is from the server */
+ socket = ( i % 2 == 0 ) ? ( &server ) : ( &client );
+
+ recv_ret[i] = recv( socket, received[i] + read[i],
+ MSGLEN - read[i] );
+
+ /* The result depends on whether any data was sent */
+ if ( send_ret[i] > 0 )
+ {
+ TEST_ASSERT( recv_ret[i] > 0 );
+ TEST_ASSERT( recv_ret[i] <= BUFLEN );
+ read[i] += recv_ret[i];
+ }
+ else if( blocking )
+ {
+ TEST_ASSERT( recv_ret[i] == 0 );
+ }
+ else
+ {
+ TEST_ASSERT( recv_ret[i] == MBEDTLS_ERR_SSL_WANT_READ );
+ recv_ret[i] = 0;
+ }
+
+ /* If the buffer is empty we can test blocking and non-blocking
+ * read */
+ if ( recv_ret[i] == BUFLEN )
+ {
+ int blocking_ret = recv( socket, received[i], 1 );
+ if ( blocking )
+ {
+ TEST_ASSERT( blocking_ret == 0 );
+ }
+ else
+ {
+ TEST_ASSERT( blocking_ret == MBEDTLS_ERR_SSL_WANT_READ );
+ }
+ }
}
- client_block >>= 1;
progress = 0;
for( i = 0; i < ROUNDS; i++ )
{
- if( ( send_ret[i] > 0 ) ||
- ( send_ret[i] == MBEDTLS_ERR_SSL_WANT_WRITE ) )
- {
- progress++;
- }
-
- if( ( recv_ret[i] > 0 ) ||
- ( recv_ret[i] == MBEDTLS_ERR_SSL_WANT_READ ) )
- {
- progress++;
- }
+ progress += send_ret[i] + recv_ret[i];
}
}
@@ -1132,6 +1448,583 @@
}
/* END_CASE */
+/* BEGIN_CASE */
+void ssl_message_queue_sanity( )
+{
+ mbedtls_test_message_queue queue;
+
+ /* Trying to push/pull to an empty queue */
+ TEST_ASSERT( mbedtls_test_message_queue_push_info( NULL, 1 )
+ == MBEDTLS_TEST_ERROR_ARG_NULL );
+ TEST_ASSERT( mbedtls_test_message_queue_pop_info( NULL, 1 )
+ == MBEDTLS_TEST_ERROR_ARG_NULL );
+
+ mbedtls_test_message_queue_setup( &queue, 3 );
+ TEST_ASSERT( queue.capacity == 3 );
+ TEST_ASSERT( queue.num == 0 );
+
+exit:
+ mbedtls_test_message_queue_free( &queue );
+}
+/* END_CASE */
+
+/* BEGIN_CASE */
+void ssl_message_queue_basic( )
+{
+ mbedtls_test_message_queue queue;
+
+ mbedtls_test_message_queue_setup( &queue, 3 );
+
+ /* Sanity test - 3 pushes and 3 pops with sufficient space */
+ TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 );
+ TEST_ASSERT( queue.capacity == 3 );
+ TEST_ASSERT( queue.num == 1 );
+ TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 );
+ TEST_ASSERT( queue.capacity == 3 );
+ TEST_ASSERT( queue.num == 2 );
+ TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 2 ) == 2 );
+ TEST_ASSERT( queue.capacity == 3 );
+ TEST_ASSERT( queue.num == 3 );
+
+ TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 );
+ TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 );
+ TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 2 ) == 2 );
+
+exit:
+ mbedtls_test_message_queue_free( &queue );
+}
+/* END_CASE */
+
+/* BEGIN_CASE */
+void ssl_message_queue_overflow_underflow( )
+{
+ mbedtls_test_message_queue queue;
+
+ mbedtls_test_message_queue_setup( &queue, 3 );
+
+ /* 4 pushes (last one with an error), 4 pops (last one with an error) */
+ TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 );
+ TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 );
+ TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 2 ) == 2 );
+ TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 3 )
+ == MBEDTLS_TEST_ERROR_QUEUE_FULL );
+
+ TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 );
+ TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 );
+ TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 2 ) == 2 );
+
+ TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 )
+ == MBEDTLS_TEST_ERROR_QUEUE_EMPTY );
+
+exit:
+ mbedtls_test_message_queue_free( &queue );
+}
+/* END_CASE */
+
+/* BEGIN_CASE */
+void ssl_message_queue_interleaved( )
+{
+ mbedtls_test_message_queue queue;
+
+ mbedtls_test_message_queue_setup( &queue, 3 );
+
+ /* Interleaved test - [2 pushes, 1 pop] twice, and then two pops
+ * (to wrap around the buffer) */
+ TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 );
+ TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 );
+
+ TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 );
+
+ TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 2 ) == 2 );
+ TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 3 ) == 3 );
+
+ TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 );
+ TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 2 ) == 2 );
+
+ TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 5 ) == 5 );
+ TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 8 ) == 8 );
+
+ TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 3 ) == 3 );
+
+ TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 5 ) == 5 );
+
+ TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 8 ) == 8 );
+
+exit:
+ mbedtls_test_message_queue_free( &queue );
+}
+/* END_CASE */
+
+/* BEGIN_CASE */
+void ssl_message_queue_insufficient_buffer( )
+{
+ mbedtls_test_message_queue queue;
+ size_t message_len = 10;
+ size_t buffer_len = 5;
+
+ mbedtls_test_message_queue_setup( &queue, 1 );
+
+ /* Popping without a sufficient buffer */
+ TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, message_len )
+ == (int) message_len );
+ TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, buffer_len )
+ == (int) buffer_len );
+exit:
+ mbedtls_test_message_queue_free( &queue );
+}
+/* END_CASE */
+
+/* BEGIN_CASE */
+void ssl_message_mock_uninitialized( )
+{
+ enum { MSGLEN = 10 };
+ unsigned char message[MSGLEN], received[MSGLEN];
+ mbedtls_mock_socket client, server;
+ mbedtls_test_message_queue server_queue, client_queue;
+ mbedtls_test_message_socket_context server_context, client_context;
+
+ /* Send with a NULL context */
+ TEST_ASSERT( mbedtls_mock_tcp_send_msg( NULL, message, MSGLEN )
+ == MBEDTLS_TEST_ERROR_CONTEXT_ERROR );
+
+ TEST_ASSERT( mbedtls_mock_tcp_recv_msg( NULL, message, MSGLEN )
+ == MBEDTLS_TEST_ERROR_CONTEXT_ERROR );
+
+ TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 1,
+ &server,
+ &server_context ) == 0 );
+
+ TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 1,
+ &client,
+ &client_context ) == 0 );
+
+ TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, MSGLEN )
+ == MBEDTLS_TEST_ERROR_SEND_FAILED );
+
+ TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
+ == MBEDTLS_TEST_ERROR_QUEUE_EMPTY );
+
+ /* Push directly to a queue to later simulate a disconnected behavior */
+ TEST_ASSERT( mbedtls_test_message_queue_push_info( &server_queue, MSGLEN )
+ == MSGLEN );
+
+ /* Test if there's an error when trying to read from a disconnected
+ * socket */
+ TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
+ == MBEDTLS_TEST_ERROR_RECV_FAILED );
+ exit:
+ mbedtls_message_socket_close( &server_context );
+ mbedtls_message_socket_close( &client_context );
+}
+/* END_CASE */
+
+/* BEGIN_CASE */
+void ssl_message_mock_basic( )
+{
+ enum { MSGLEN = 10 };
+ unsigned char message[MSGLEN], received[MSGLEN];
+ mbedtls_mock_socket client, server;
+ unsigned i;
+ mbedtls_test_message_queue server_queue, client_queue;
+ mbedtls_test_message_socket_context server_context, client_context;
+
+ TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 1,
+ &server,
+ &server_context ) == 0 );
+
+ TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 1,
+ &client,
+ &client_context ) == 0 );
+
+ /* Fill up the buffer with structured data so that unwanted changes
+ * can be detected */
+ for( i = 0; i < MSGLEN; i++ )
+ {
+ message[i] = i & 0xFF;
+ }
+ TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
+ MSGLEN ) );
+
+ /* Send the message to the server */
+ TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
+ MSGLEN ) == MSGLEN );
+
+ /* Read from the server */
+ TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
+ == MSGLEN );
+
+ TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
+ memset( received, 0, MSGLEN );
+
+ /* Send the message to the client */
+ TEST_ASSERT( mbedtls_mock_tcp_send_msg( &server_context, message,
+ MSGLEN ) == MSGLEN );
+
+ /* Read from the client */
+ TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &client_context, received, MSGLEN )
+ == MSGLEN );
+ TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
+
+ exit:
+ mbedtls_message_socket_close( &server_context );
+ mbedtls_message_socket_close( &client_context );
+}
+/* END_CASE */
+
+/* BEGIN_CASE */
+void ssl_message_mock_queue_overflow_underflow( )
+{
+ enum { MSGLEN = 10 };
+ unsigned char message[MSGLEN], received[MSGLEN];
+ mbedtls_mock_socket client, server;
+ unsigned i;
+ mbedtls_test_message_queue server_queue, client_queue;
+ mbedtls_test_message_socket_context server_context, client_context;
+
+ TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 2,
+ &server,
+ &server_context ) == 0 );
+
+ TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 2,
+ &client,
+ &client_context ) == 0 );
+
+ /* Fill up the buffer with structured data so that unwanted changes
+ * can be detected */
+ for( i = 0; i < MSGLEN; i++ )
+ {
+ message[i] = i & 0xFF;
+ }
+ TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
+ MSGLEN*2 ) );
+
+ /* Send three message to the server, last one with an error */
+ TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
+ MSGLEN - 1 ) == MSGLEN - 1 );
+
+ TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
+ MSGLEN ) == MSGLEN );
+
+ TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
+ MSGLEN )
+ == MBEDTLS_TEST_ERROR_QUEUE_FULL );
+
+ /* Read three messages from the server, last one with an error */
+ TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received,
+ MSGLEN - 1 ) == MSGLEN - 1 );
+
+ TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
+ == MSGLEN );
+
+ TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
+
+ TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
+ == MBEDTLS_TEST_ERROR_QUEUE_EMPTY );
+
+ exit:
+ mbedtls_message_socket_close( &server_context );
+ mbedtls_message_socket_close( &client_context );
+}
+/* END_CASE */
+
+/* BEGIN_CASE */
+void ssl_message_mock_socket_overflow( )
+{
+ enum { MSGLEN = 10 };
+ unsigned char message[MSGLEN], received[MSGLEN];
+ mbedtls_mock_socket client, server;
+ unsigned i;
+ mbedtls_test_message_queue server_queue, client_queue;
+ mbedtls_test_message_socket_context server_context, client_context;
+
+ TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 2,
+ &server,
+ &server_context ) == 0 );
+
+ TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 2,
+ &client,
+ &client_context ) == 0 );
+
+ /* Fill up the buffer with structured data so that unwanted changes
+ * can be detected */
+ for( i = 0; i < MSGLEN; i++ )
+ {
+ message[i] = i & 0xFF;
+ }
+ TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
+ MSGLEN ) );
+
+ /* Send two message to the server, second one with an error */
+ TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
+ MSGLEN ) == MSGLEN );
+
+ TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
+ MSGLEN )
+ == MBEDTLS_TEST_ERROR_SEND_FAILED );
+
+ /* Read the only message from the server */
+ TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
+ == MSGLEN );
+
+ TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
+
+ exit:
+ mbedtls_message_socket_close( &server_context );
+ mbedtls_message_socket_close( &client_context );
+}
+/* END_CASE */
+
+/* BEGIN_CASE */
+void ssl_message_mock_truncated( )
+{
+ enum { MSGLEN = 10 };
+ unsigned char message[MSGLEN], received[MSGLEN];
+ mbedtls_mock_socket client, server;
+ unsigned i;
+ mbedtls_test_message_queue server_queue, client_queue;
+ mbedtls_test_message_socket_context server_context, client_context;
+
+ TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 2,
+ &server,
+ &server_context ) == 0 );
+
+ TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 2,
+ &client,
+ &client_context ) == 0 );
+
+ memset( received, 0, MSGLEN );
+ /* Fill up the buffer with structured data so that unwanted changes
+ * can be detected */
+ for( i = 0; i < MSGLEN; i++ )
+ {
+ message[i] = i & 0xFF;
+ }
+ TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
+ 2 * MSGLEN ) );
+
+ /* Send two messages to the server, the second one small enough to fit in the
+ * receiver's buffer. */
+ TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
+ MSGLEN ) == MSGLEN );
+ TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
+ MSGLEN / 2 ) == MSGLEN / 2 );
+ /* Read a truncated message from the server */
+ TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN/2 )
+ == MSGLEN/2 );
+
+ /* Test that the first half of the message is valid, and second one isn't */
+ TEST_ASSERT( memcmp( message, received, MSGLEN/2 ) == 0 );
+ TEST_ASSERT( memcmp( message + MSGLEN/2, received + MSGLEN/2, MSGLEN/2 )
+ != 0 );
+ memset( received, 0, MSGLEN );
+
+ /* Read a full message from the server */
+ TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN/2 )
+ == MSGLEN / 2 );
+
+ /* Test that the first half of the message is valid */
+ TEST_ASSERT( memcmp( message, received, MSGLEN/2 ) == 0 );
+
+ exit:
+ mbedtls_message_socket_close( &server_context );
+ mbedtls_message_socket_close( &client_context );
+}
+/* END_CASE */
+
+/* BEGIN_CASE */
+void ssl_message_mock_socket_read_error( )
+{
+ enum { MSGLEN = 10 };
+ unsigned char message[MSGLEN], received[MSGLEN];
+ mbedtls_mock_socket client, server;
+ unsigned i;
+ mbedtls_test_message_queue server_queue, client_queue;
+ mbedtls_test_message_socket_context server_context, client_context;
+
+ TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 1,
+ &server,
+ &server_context ) == 0 );
+
+ TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 1,
+ &client,
+ &client_context ) == 0 );
+
+ /* Fill up the buffer with structured data so that unwanted changes
+ * can be detected */
+ for( i = 0; i < MSGLEN; i++ )
+ {
+ message[i] = i & 0xFF;
+ }
+ TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
+ MSGLEN ) );
+
+ TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
+ MSGLEN ) == MSGLEN );
+
+ /* Force a read error by disconnecting the socket by hand */
+ server.status = 0;
+ TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
+ == MBEDTLS_TEST_ERROR_RECV_FAILED );
+ /* Return to a valid state */
+ server.status = MBEDTLS_MOCK_SOCKET_CONNECTED;
+
+ memset( received, 0, sizeof( received ) );
+
+ /* Test that even though the server tried to read once disconnected, the
+ * continuity is preserved */
+ TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
+ == MSGLEN );
+
+ TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
+
+ exit:
+ mbedtls_message_socket_close( &server_context );
+ mbedtls_message_socket_close( &client_context );
+}
+/* END_CASE */
+
+/* BEGIN_CASE */
+void ssl_message_mock_interleaved_one_way( )
+{
+ enum { MSGLEN = 10 };
+ unsigned char message[MSGLEN], received[MSGLEN];
+ mbedtls_mock_socket client, server;
+ unsigned i;
+ mbedtls_test_message_queue server_queue, client_queue;
+ mbedtls_test_message_socket_context server_context, client_context;
+
+ TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 3,
+ &server,
+ &server_context ) == 0 );
+
+ TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 3,
+ &client,
+ &client_context ) == 0 );
+
+ /* Fill up the buffer with structured data so that unwanted changes
+ * can be detected */
+ for( i = 0; i < MSGLEN; i++ )
+ {
+ message[i] = i & 0xFF;
+ }
+ TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
+ MSGLEN*3 ) );
+
+ /* Interleaved test - [2 sends, 1 read] twice, and then two reads
+ * (to wrap around the buffer) */
+ for( i = 0; i < 2; i++ )
+ {
+ TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
+ MSGLEN ) == MSGLEN );
+
+ TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
+ MSGLEN ) == MSGLEN );
+
+ TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received,
+ MSGLEN ) == MSGLEN );
+ TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
+ memset( received, 0, sizeof( received ) );
+ }
+
+ for( i = 0; i < 2; i++ )
+ {
+ TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received,
+ MSGLEN ) == MSGLEN );
+
+ TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
+ }
+ TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
+ == MBEDTLS_TEST_ERROR_QUEUE_EMPTY );
+ exit:
+ mbedtls_message_socket_close( &server_context );
+ mbedtls_message_socket_close( &client_context );
+}
+/* END_CASE */
+
+/* BEGIN_CASE */
+void ssl_message_mock_interleaved_two_ways( )
+{
+ enum { MSGLEN = 10 };
+ unsigned char message[MSGLEN], received[MSGLEN];
+ mbedtls_mock_socket client, server;
+ unsigned i;
+ mbedtls_test_message_queue server_queue, client_queue;
+ mbedtls_test_message_socket_context server_context, client_context;
+
+ TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 3,
+ &server,
+ &server_context ) == 0 );
+
+ TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 3,
+ &client,
+ &client_context ) == 0 );
+
+ /* Fill up the buffer with structured data so that unwanted changes
+ * can be detected */
+ for( i = 0; i < MSGLEN; i++ )
+ {
+ message[i] = i & 0xFF;
+ }
+ TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
+ MSGLEN*3 ) );
+
+ /* Interleaved test - [2 sends, 1 read] twice, both ways, and then two reads
+ * (to wrap around the buffer) both ways. */
+ for( i = 0; i < 2; i++ )
+ {
+ TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
+ MSGLEN ) == MSGLEN );
+
+ TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
+ MSGLEN ) == MSGLEN );
+
+ TEST_ASSERT( mbedtls_mock_tcp_send_msg( &server_context, message,
+ MSGLEN ) == MSGLEN );
+
+ TEST_ASSERT( mbedtls_mock_tcp_send_msg( &server_context, message,
+ MSGLEN ) == MSGLEN );
+
+ TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received,
+ MSGLEN ) == MSGLEN );
+
+ TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
+
+ memset( received, 0, sizeof( received ) );
+
+ TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &client_context, received,
+ MSGLEN ) == MSGLEN );
+
+ TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
+
+ memset( received, 0, sizeof( received ) );
+ }
+
+ for( i = 0; i < 2; i++ )
+ {
+ TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received,
+ MSGLEN ) == MSGLEN );
+
+ TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
+ memset( received, 0, sizeof( received ) );
+
+ TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &client_context, received,
+ MSGLEN ) == MSGLEN );
+
+ TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
+ memset( received, 0, sizeof( received ) );
+ }
+
+ TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
+ == MBEDTLS_TEST_ERROR_QUEUE_EMPTY );
+
+ TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &client_context, received, MSGLEN )
+ == MBEDTLS_TEST_ERROR_QUEUE_EMPTY );
+ exit:
+ mbedtls_message_socket_close( &server_context );
+ mbedtls_message_socket_close( &client_context );
+}
+/* END_CASE */
+
/* BEGIN_CASE depends_on:MBEDTLS_SSL_DTLS_ANTI_REPLAY */
void ssl_dtls_replay( data_t * prevs, data_t * new, int ret )
{
diff --git a/tests/suites/test_suite_version.data b/tests/suites/test_suite_version.data
index b6dca23..ff0612b 100644
--- a/tests/suites/test_suite_version.data
+++ b/tests/suites/test_suite_version.data
@@ -1,8 +1,8 @@
Check compiletime library version
-check_compiletime_version:"2.19.1"
+check_compiletime_version:"2.20.0"
Check runtime library version
-check_runtime_version:"2.19.1"
+check_runtime_version:"2.20.0"
Check for MBEDTLS_VERSION_C
check_feature:"MBEDTLS_VERSION_C":0