2014-09-24 11:13:44 +02:00
|
|
|
/* BEGIN_HEADER */
|
2015-03-09 18:05:11 +01:00
|
|
|
#include <mbedtls/ssl.h>
|
2015-05-26 11:57:05 +02:00
|
|
|
#include <mbedtls/ssl_internal.h>
|
2018-01-03 15:27:32 +01:00
|
|
|
|
2019-11-26 12:11:15 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Buffer structure for custom I/O callbacks.
|
|
|
|
*/
|
|
|
|
|
|
|
|
typedef struct mbedtls_test_buffer
|
|
|
|
{
|
|
|
|
size_t start;
|
|
|
|
size_t content_length;
|
|
|
|
size_t capacity;
|
|
|
|
unsigned char *buffer;
|
|
|
|
} mbedtls_test_buffer;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialises \p buf. After calling this function it is safe to call
|
|
|
|
* `mbedtls_test_buffer_free()` on \p buf.
|
|
|
|
*/
|
|
|
|
void mbedtls_test_buffer_init( mbedtls_test_buffer *buf )
|
|
|
|
{
|
|
|
|
memset( buf, 0, sizeof( *buf ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Sets up \p buf. After calling this function it is safe to call
|
|
|
|
* `mbedtls_test_buffer_put()` and `mbedtls_test_buffer_get()` on \p buf.
|
|
|
|
*/
|
|
|
|
int mbedtls_test_buffer_setup( mbedtls_test_buffer *buf, size_t capacity )
|
|
|
|
{
|
|
|
|
buf->buffer = (unsigned char*) mbedtls_calloc( capacity,
|
|
|
|
sizeof(unsigned char) );
|
|
|
|
if( NULL == buf->buffer )
|
|
|
|
return MBEDTLS_ERR_SSL_ALLOC_FAILED;
|
|
|
|
buf->capacity = capacity;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void mbedtls_test_buffer_free( mbedtls_test_buffer *buf )
|
|
|
|
{
|
|
|
|
if( buf->buffer != NULL )
|
|
|
|
mbedtls_free( buf->buffer );
|
|
|
|
|
|
|
|
memset( buf, 0, sizeof( *buf ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Puts \p input_len bytes from the \p input buffer into the ring buffer \p buf.
|
|
|
|
*
|
|
|
|
* \p buf must have been initialized and set up by calling
|
|
|
|
* `mbedtls_test_buffer_init()` and `mbedtls_test_buffer_setup()`.
|
|
|
|
*
|
|
|
|
* \retval \p input_len, if the data fits.
|
|
|
|
* \retval 0 <= value < \p input_len, if the data does not fit.
|
|
|
|
* \retval -1, if \p buf is NULL, it hasn't been set up or \p input_len is not
|
|
|
|
* zero and \p input is NULL.
|
|
|
|
*/
|
|
|
|
int mbedtls_test_buffer_put( mbedtls_test_buffer *buf,
|
|
|
|
const unsigned char* input, size_t input_len )
|
|
|
|
{
|
|
|
|
size_t overflow = 0;
|
|
|
|
|
|
|
|
if( ( buf == NULL ) || ( buf->buffer == NULL ) )
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* Reduce input_len to a number that fits in the buffer. */
|
|
|
|
if ( ( buf->content_length + input_len ) > buf->capacity )
|
|
|
|
{
|
|
|
|
input_len = buf->capacity - buf->content_length;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( input == NULL )
|
|
|
|
{
|
|
|
|
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 )
|
|
|
|
{
|
|
|
|
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 );
|
|
|
|
buf->content_length += input_len;
|
|
|
|
|
|
|
|
return input_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2020-01-22 12:34:59 +01:00
|
|
|
* 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.
|
2019-11-26 12:11:15 +01:00
|
|
|
*
|
|
|
|
* \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.
|
2020-01-22 12:34:59 +01:00
|
|
|
* \retval -1, if \buf is NULL or it hasn't been set up.
|
2019-11-26 12:11:15 +01:00
|
|
|
*/
|
|
|
|
int mbedtls_test_buffer_get( mbedtls_test_buffer *buf,
|
|
|
|
unsigned char* output, size_t output_len )
|
|
|
|
{
|
|
|
|
size_t overflow = 0;
|
|
|
|
|
|
|
|
if( ( buf == NULL ) || ( buf->buffer == NULL ) )
|
|
|
|
return -1;
|
|
|
|
|
2020-01-22 12:34:59 +01:00
|
|
|
if( output == NULL && output_len == 0 )
|
|
|
|
return 0;
|
2019-11-26 12:11:15 +01:00
|
|
|
|
|
|
|
if( buf->content_length < output_len )
|
|
|
|
output_len = buf->content_length;
|
|
|
|
|
|
|
|
/* Calculate the number of bytes that need to be drawn from lower memory
|
|
|
|
* address */
|
|
|
|
if( buf->start + output_len > buf->capacity )
|
|
|
|
{
|
|
|
|
overflow = ( buf->start + output_len ) % buf->capacity;
|
|
|
|
}
|
|
|
|
|
2020-01-22 12:34:59 +01:00
|
|
|
if( output != NULL )
|
|
|
|
{
|
|
|
|
memcpy( output, buf->buffer + buf->start, output_len - overflow );
|
|
|
|
memcpy( output + output_len - overflow, buf->buffer, overflow );
|
|
|
|
}
|
|
|
|
|
2019-11-26 12:11:15 +01:00
|
|
|
buf->content_length -= output_len;
|
|
|
|
buf->start = ( buf->start + output_len ) % buf->capacity;
|
|
|
|
|
|
|
|
return output_len;
|
|
|
|
}
|
|
|
|
|
2019-11-27 12:12:14 +01:00
|
|
|
/*
|
|
|
|
* Context for the I/O callbacks simulating network connection.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define MBEDTLS_MOCK_SOCKET_CONNECTED 1
|
|
|
|
|
|
|
|
typedef struct mbedtls_mock_socket
|
|
|
|
{
|
|
|
|
int status;
|
2019-11-27 14:31:42 +01:00
|
|
|
uint32_t blocking_pattern;
|
2019-11-27 12:12:14 +01:00
|
|
|
mbedtls_test_buffer *input;
|
|
|
|
mbedtls_test_buffer *output;
|
|
|
|
struct mbedtls_mock_socket *peer;
|
|
|
|
} mbedtls_mock_socket;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Setup and teardown functions for mock sockets.
|
|
|
|
*/
|
|
|
|
void mbedtls_mock_socket_init( mbedtls_mock_socket *socket )
|
|
|
|
{
|
|
|
|
memset( socket, 0, sizeof( *socket ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Closes the socket \p socket.
|
|
|
|
*
|
|
|
|
* \p socket must have been previously initialized by calling
|
|
|
|
* mbedtls_mock_socket_init().
|
|
|
|
*
|
|
|
|
* This function frees all allocated resources and both sockets are aware of the
|
|
|
|
* new connection state.
|
|
|
|
*
|
|
|
|
* That is, this function does not simulate half-open TCP connections and the
|
|
|
|
* phenomenon that when closing a UDP connection the peer is not aware of the
|
|
|
|
* connection having been closed.
|
|
|
|
*/
|
|
|
|
void mbedtls_mock_socket_close( mbedtls_mock_socket* socket )
|
|
|
|
{
|
|
|
|
if( socket == NULL )
|
|
|
|
return;
|
|
|
|
|
|
|
|
if( socket->input != NULL )
|
|
|
|
{
|
|
|
|
mbedtls_test_buffer_free( socket->input );
|
|
|
|
mbedtls_free( socket->input );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( socket->output != NULL )
|
|
|
|
{
|
|
|
|
mbedtls_test_buffer_free( socket->output );
|
|
|
|
mbedtls_free( socket->output );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( socket->peer != NULL )
|
|
|
|
memset( socket->peer, 0, sizeof( *socket->peer ) );
|
|
|
|
|
|
|
|
memset( socket, 0, sizeof( *socket ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Establishes a connection between \p peer1 and \p peer2.
|
|
|
|
*
|
|
|
|
* \p peer1 and \p peer2 must have been previously initialized by calling
|
|
|
|
* mbedtls_mock_socket_init().
|
|
|
|
*
|
|
|
|
* The capacites of the internal buffers are set to \p bufsize. Setting this to
|
|
|
|
* the correct value allows for simulation of MTU, sanity testing the mock
|
|
|
|
* implementation and mocking TCP connections with lower memory cost.
|
|
|
|
*/
|
|
|
|
int mbedtls_mock_socket_connect( mbedtls_mock_socket* peer1,
|
|
|
|
mbedtls_mock_socket* peer2,
|
|
|
|
size_t bufsize )
|
|
|
|
{
|
|
|
|
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 =
|
|
|
|
(mbedtls_test_buffer*) mbedtls_calloc( 1, sizeof(mbedtls_test_buffer) );
|
|
|
|
if( peer1->output == NULL )
|
|
|
|
{
|
|
|
|
ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
mbedtls_test_buffer_init( peer1->output );
|
|
|
|
if( 0 != ( ret = mbedtls_test_buffer_setup( peer1->output, bufsize ) ) )
|
|
|
|
{
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
peer1->peer = peer2;
|
|
|
|
peer2->peer = peer1;
|
|
|
|
|
|
|
|
peer1->status = peer2->status = MBEDTLS_MOCK_SOCKET_CONNECTED;
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
exit:
|
|
|
|
|
|
|
|
if( ret != 0 )
|
|
|
|
{
|
|
|
|
mbedtls_mock_socket_close( peer1 );
|
|
|
|
mbedtls_mock_socket_close( peer2 );
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-11-27 14:31:42 +01:00
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2019-11-27 12:12:14 +01:00
|
|
|
/*
|
|
|
|
* Callbacks for simulating blocking I/O over connection-oriented transport.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int mbedtls_mock_tcp_send_b( void *ctx, const unsigned char *buf, size_t len )
|
|
|
|
{
|
|
|
|
mbedtls_mock_socket *socket = (mbedtls_mock_socket*) ctx;
|
|
|
|
|
|
|
|
if( socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED )
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return mbedtls_test_buffer_put( socket->output, buf, len );
|
|
|
|
}
|
|
|
|
|
|
|
|
int mbedtls_mock_tcp_recv_b( void *ctx, unsigned char *buf, size_t len )
|
|
|
|
{
|
|
|
|
mbedtls_mock_socket *socket = (mbedtls_mock_socket*) ctx;
|
|
|
|
|
|
|
|
if( socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED )
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return mbedtls_test_buffer_get( socket->input, buf, len );
|
|
|
|
}
|
|
|
|
|
2019-11-27 14:31:42 +01:00
|
|
|
/*
|
|
|
|
* Callbacks for simulating non-blocking I/O over connection-oriented transport.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int mbedtls_mock_tcp_send_nb( void *ctx, const unsigned char *buf, size_t len )
|
|
|
|
{
|
|
|
|
mbedtls_mock_socket *socket = (mbedtls_mock_socket*) ctx;
|
|
|
|
|
|
|
|
if( socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED )
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if( socket->blocking_pattern & 1 )
|
|
|
|
{
|
|
|
|
socket->blocking_pattern >>= 1;
|
|
|
|
return MBEDTLS_ERR_SSL_WANT_WRITE;
|
|
|
|
}
|
|
|
|
|
|
|
|
socket->blocking_pattern >>= 1;
|
|
|
|
|
|
|
|
return mbedtls_test_buffer_put( socket->output, buf, len );
|
|
|
|
}
|
|
|
|
|
|
|
|
int mbedtls_mock_tcp_recv_nb( void *ctx, unsigned char *buf, size_t len )
|
|
|
|
{
|
|
|
|
mbedtls_mock_socket *socket = (mbedtls_mock_socket*) ctx;
|
|
|
|
|
|
|
|
if( socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED )
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if( socket->blocking_pattern & 1 )
|
|
|
|
{
|
|
|
|
socket->blocking_pattern >>= 1;
|
|
|
|
return MBEDTLS_ERR_SSL_WANT_READ;
|
|
|
|
}
|
|
|
|
|
|
|
|
socket->blocking_pattern >>= 1;
|
|
|
|
|
|
|
|
return mbedtls_test_buffer_get( socket->input, buf, len );
|
|
|
|
}
|
|
|
|
|
2018-01-03 15:27:32 +01:00
|
|
|
/*
|
|
|
|
* Helper function setting up inverse record transformations
|
|
|
|
* using given cipher, hash, EtM mode, authentication tag length,
|
|
|
|
* and version.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define CHK( x ) \
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
if( !( x ) ) \
|
2019-03-01 12:21:44 +01:00
|
|
|
{ \
|
2019-04-05 10:55:37 +02:00
|
|
|
ret = -1; \
|
2019-03-01 12:21:44 +01:00
|
|
|
goto cleanup; \
|
|
|
|
} \
|
2018-01-03 15:27:32 +01:00
|
|
|
} while( 0 )
|
|
|
|
|
2019-04-29 18:30:59 +02:00
|
|
|
#if MBEDTLS_SSL_CID_OUT_LEN_MAX > MBEDTLS_SSL_CID_IN_LEN_MAX
|
|
|
|
#define SSL_CID_LEN_MIN MBEDTLS_SSL_CID_IN_LEN_MAX
|
|
|
|
#else
|
|
|
|
#define SSL_CID_LEN_MIN MBEDTLS_SSL_CID_OUT_LEN_MAX
|
|
|
|
#endif
|
2018-01-03 15:27:32 +01:00
|
|
|
|
|
|
|
static int build_transforms( mbedtls_ssl_transform *t_in,
|
|
|
|
mbedtls_ssl_transform *t_out,
|
|
|
|
int cipher_type, int hash_id,
|
2019-04-29 18:30:59 +02:00
|
|
|
int etm, int tag_mode, int ver,
|
|
|
|
size_t cid0_len,
|
|
|
|
size_t cid1_len )
|
2018-01-03 15:27:32 +01:00
|
|
|
{
|
|
|
|
mbedtls_cipher_info_t const *cipher_info;
|
2019-04-05 10:55:37 +02:00
|
|
|
int ret = 0;
|
2018-01-03 15:27:32 +01:00
|
|
|
|
|
|
|
size_t keylen, maclen, ivlen;
|
2019-03-01 12:21:44 +01:00
|
|
|
unsigned char *key0 = NULL, *key1 = NULL;
|
2018-01-03 15:27:32 +01:00
|
|
|
unsigned char iv_enc[16], iv_dec[16];
|
|
|
|
|
2019-05-15 15:03:01 +02:00
|
|
|
#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
|
2019-04-29 18:30:59 +02:00
|
|
|
unsigned char cid0[ SSL_CID_LEN_MIN ];
|
|
|
|
unsigned char cid1[ SSL_CID_LEN_MIN ];
|
|
|
|
|
|
|
|
rnd_std_rand( NULL, cid0, sizeof( cid0 ) );
|
|
|
|
rnd_std_rand( NULL, cid1, sizeof( cid1 ) );
|
2019-05-01 10:45:57 +02:00
|
|
|
#else
|
|
|
|
((void) cid0_len);
|
|
|
|
((void) cid1_len);
|
2019-05-15 15:03:01 +02:00
|
|
|
#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
|
2019-04-29 18:30:59 +02:00
|
|
|
|
2018-01-03 15:27:32 +01:00
|
|
|
maclen = 0;
|
|
|
|
|
|
|
|
/* Pick cipher */
|
|
|
|
cipher_info = mbedtls_cipher_info_from_type( cipher_type );
|
|
|
|
CHK( cipher_info != NULL );
|
|
|
|
CHK( cipher_info->iv_size <= 16 );
|
|
|
|
CHK( cipher_info->key_bitlen % 8 == 0 );
|
|
|
|
|
|
|
|
/* Pick keys */
|
|
|
|
keylen = cipher_info->key_bitlen / 8;
|
2019-04-05 10:56:10 +02:00
|
|
|
/* Allocate `keylen + 1` bytes to ensure that we get
|
|
|
|
* a non-NULL pointers from `mbedtls_calloc` even if
|
|
|
|
* `keylen == 0` in the case of the NULL cipher. */
|
|
|
|
CHK( ( key0 = mbedtls_calloc( 1, keylen + 1 ) ) != NULL );
|
|
|
|
CHK( ( key1 = mbedtls_calloc( 1, keylen + 1 ) ) != NULL );
|
2018-01-03 15:27:32 +01:00
|
|
|
memset( key0, 0x1, keylen );
|
|
|
|
memset( key1, 0x2, keylen );
|
|
|
|
|
|
|
|
/* Setup cipher contexts */
|
|
|
|
CHK( mbedtls_cipher_setup( &t_in->cipher_ctx_enc, cipher_info ) == 0 );
|
|
|
|
CHK( mbedtls_cipher_setup( &t_in->cipher_ctx_dec, cipher_info ) == 0 );
|
|
|
|
CHK( mbedtls_cipher_setup( &t_out->cipher_ctx_enc, cipher_info ) == 0 );
|
|
|
|
CHK( mbedtls_cipher_setup( &t_out->cipher_ctx_dec, cipher_info ) == 0 );
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_CIPHER_MODE_CBC)
|
|
|
|
if( cipher_info->mode == MBEDTLS_MODE_CBC )
|
|
|
|
{
|
|
|
|
CHK( mbedtls_cipher_set_padding_mode( &t_in->cipher_ctx_enc,
|
|
|
|
MBEDTLS_PADDING_NONE ) == 0 );
|
|
|
|
CHK( mbedtls_cipher_set_padding_mode( &t_in->cipher_ctx_dec,
|
|
|
|
MBEDTLS_PADDING_NONE ) == 0 );
|
|
|
|
CHK( mbedtls_cipher_set_padding_mode( &t_out->cipher_ctx_enc,
|
|
|
|
MBEDTLS_PADDING_NONE ) == 0 );
|
|
|
|
CHK( mbedtls_cipher_set_padding_mode( &t_out->cipher_ctx_dec,
|
|
|
|
MBEDTLS_PADDING_NONE ) == 0 );
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_CIPHER_MODE_CBC */
|
|
|
|
|
|
|
|
CHK( mbedtls_cipher_setkey( &t_in->cipher_ctx_enc, key0,
|
|
|
|
keylen << 3, MBEDTLS_ENCRYPT ) == 0 );
|
|
|
|
CHK( mbedtls_cipher_setkey( &t_in->cipher_ctx_dec, key1,
|
|
|
|
keylen << 3, MBEDTLS_DECRYPT ) == 0 );
|
|
|
|
CHK( mbedtls_cipher_setkey( &t_out->cipher_ctx_enc, key1,
|
|
|
|
keylen << 3, MBEDTLS_ENCRYPT ) == 0 );
|
|
|
|
CHK( mbedtls_cipher_setkey( &t_out->cipher_ctx_dec, key0,
|
|
|
|
keylen << 3, MBEDTLS_DECRYPT ) == 0 );
|
|
|
|
|
|
|
|
/* Setup MAC contexts */
|
|
|
|
#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
|
|
|
|
if( cipher_info->mode == MBEDTLS_MODE_CBC ||
|
|
|
|
cipher_info->mode == MBEDTLS_MODE_STREAM )
|
|
|
|
{
|
|
|
|
mbedtls_md_info_t const *md_info;
|
|
|
|
unsigned char *md0, *md1;
|
|
|
|
|
|
|
|
/* Pick hash */
|
|
|
|
md_info = mbedtls_md_info_from_type( hash_id );
|
|
|
|
CHK( md_info != NULL );
|
|
|
|
|
|
|
|
/* Pick hash keys */
|
|
|
|
maclen = mbedtls_md_get_size( md_info );
|
2019-04-04 17:31:26 +02:00
|
|
|
CHK( ( md0 = mbedtls_calloc( 1, maclen ) ) != NULL );
|
|
|
|
CHK( ( md1 = mbedtls_calloc( 1, maclen ) ) != NULL );
|
2018-01-03 15:27:32 +01:00
|
|
|
memset( md0, 0x5, maclen );
|
|
|
|
memset( md1, 0x6, maclen );
|
|
|
|
|
|
|
|
CHK( mbedtls_md_setup( &t_out->md_ctx_enc, md_info, 1 ) == 0 );
|
|
|
|
CHK( mbedtls_md_setup( &t_out->md_ctx_dec, md_info, 1 ) == 0 );
|
|
|
|
CHK( mbedtls_md_setup( &t_in->md_ctx_enc, md_info, 1 ) == 0 );
|
|
|
|
CHK( mbedtls_md_setup( &t_in->md_ctx_dec, md_info, 1 ) == 0 );
|
|
|
|
|
|
|
|
if( ver > MBEDTLS_SSL_MINOR_VERSION_0 )
|
|
|
|
{
|
|
|
|
CHK( mbedtls_md_hmac_starts( &t_in->md_ctx_enc,
|
|
|
|
md0, maclen ) == 0 );
|
|
|
|
CHK( mbedtls_md_hmac_starts( &t_in->md_ctx_dec,
|
|
|
|
md1, maclen ) == 0 );
|
|
|
|
CHK( mbedtls_md_hmac_starts( &t_out->md_ctx_enc,
|
|
|
|
md1, maclen ) == 0 );
|
|
|
|
CHK( mbedtls_md_hmac_starts( &t_out->md_ctx_dec,
|
|
|
|
md0, maclen ) == 0 );
|
|
|
|
}
|
|
|
|
#if defined(MBEDTLS_SSL_PROTO_SSL3)
|
|
|
|
else
|
|
|
|
{
|
|
|
|
memcpy( &t_in->mac_enc, md0, maclen );
|
|
|
|
memcpy( &t_in->mac_dec, md1, maclen );
|
|
|
|
memcpy( &t_out->mac_enc, md1, maclen );
|
|
|
|
memcpy( &t_out->mac_dec, md0, maclen );
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-04-04 17:31:26 +02:00
|
|
|
mbedtls_free( md0 );
|
|
|
|
mbedtls_free( md1 );
|
2018-01-03 15:27:32 +01:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
((void) hash_id);
|
|
|
|
#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
|
|
|
|
|
|
|
|
|
|
|
|
/* Pick IV's (regardless of whether they
|
|
|
|
* are being used by the transform). */
|
|
|
|
ivlen = cipher_info->iv_size;
|
|
|
|
memset( iv_enc, 0x3, sizeof( iv_enc ) );
|
|
|
|
memset( iv_dec, 0x4, sizeof( iv_dec ) );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Setup transforms
|
|
|
|
*/
|
|
|
|
|
2019-06-05 14:32:08 +02:00
|
|
|
#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
|
|
|
|
defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
|
2018-01-03 15:27:32 +01:00
|
|
|
t_out->encrypt_then_mac = etm;
|
|
|
|
t_in->encrypt_then_mac = etm;
|
|
|
|
#else
|
|
|
|
((void) etm);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
t_out->minor_ver = ver;
|
|
|
|
t_in->minor_ver = ver;
|
|
|
|
t_out->ivlen = ivlen;
|
|
|
|
t_in->ivlen = ivlen;
|
|
|
|
|
|
|
|
switch( cipher_info->mode )
|
|
|
|
{
|
|
|
|
case MBEDTLS_MODE_GCM:
|
|
|
|
case MBEDTLS_MODE_CCM:
|
|
|
|
t_out->fixed_ivlen = 4;
|
|
|
|
t_in->fixed_ivlen = 4;
|
|
|
|
t_out->maclen = 0;
|
|
|
|
t_in->maclen = 0;
|
|
|
|
switch( tag_mode )
|
|
|
|
{
|
|
|
|
case 0: /* Full tag */
|
|
|
|
t_out->taglen = 16;
|
|
|
|
t_in->taglen = 16;
|
|
|
|
break;
|
|
|
|
case 1: /* Partial tag */
|
|
|
|
t_out->taglen = 8;
|
|
|
|
t_in->taglen = 8;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return( 1 );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MBEDTLS_MODE_CHACHAPOLY:
|
|
|
|
t_out->fixed_ivlen = 12;
|
|
|
|
t_in->fixed_ivlen = 12;
|
|
|
|
t_out->maclen = 0;
|
|
|
|
t_in->maclen = 0;
|
|
|
|
switch( tag_mode )
|
|
|
|
{
|
|
|
|
case 0: /* Full tag */
|
|
|
|
t_out->taglen = 16;
|
|
|
|
t_in->taglen = 16;
|
|
|
|
break;
|
|
|
|
case 1: /* Partial tag */
|
|
|
|
t_out->taglen = 8;
|
|
|
|
t_in->taglen = 8;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return( 1 );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MBEDTLS_MODE_STREAM:
|
|
|
|
case MBEDTLS_MODE_CBC:
|
|
|
|
t_out->fixed_ivlen = 0; /* redundant, must be 0 */
|
|
|
|
t_in->fixed_ivlen = 0; /* redundant, must be 0 */
|
|
|
|
t_out->taglen = 0;
|
|
|
|
t_in->taglen = 0;
|
|
|
|
switch( tag_mode )
|
|
|
|
{
|
|
|
|
case 0: /* Full tag */
|
|
|
|
t_out->maclen = maclen;
|
|
|
|
t_in->maclen = maclen;
|
|
|
|
break;
|
|
|
|
case 1: /* Partial tag */
|
|
|
|
t_out->maclen = 10;
|
|
|
|
t_in->maclen = 10;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return( 1 );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return( 1 );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Setup IV's */
|
|
|
|
|
|
|
|
memcpy( &t_in->iv_dec, iv_dec, sizeof( iv_dec ) );
|
|
|
|
memcpy( &t_in->iv_enc, iv_enc, sizeof( iv_enc ) );
|
|
|
|
memcpy( &t_out->iv_dec, iv_enc, sizeof( iv_enc ) );
|
|
|
|
memcpy( &t_out->iv_enc, iv_dec, sizeof( iv_dec ) );
|
|
|
|
|
2019-05-15 15:03:01 +02:00
|
|
|
#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
|
2019-04-29 18:30:59 +02:00
|
|
|
/* Add CID */
|
|
|
|
memcpy( &t_in->in_cid, cid0, cid0_len );
|
|
|
|
memcpy( &t_in->out_cid, cid1, cid1_len );
|
|
|
|
t_in->in_cid_len = cid0_len;
|
|
|
|
t_in->out_cid_len = cid1_len;
|
|
|
|
memcpy( &t_out->in_cid, cid1, cid1_len );
|
|
|
|
memcpy( &t_out->out_cid, cid0, cid0_len );
|
|
|
|
t_out->in_cid_len = cid1_len;
|
|
|
|
t_out->out_cid_len = cid0_len;
|
2019-05-15 15:03:01 +02:00
|
|
|
#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
|
2019-04-29 18:30:59 +02:00
|
|
|
|
2019-03-01 12:21:44 +01:00
|
|
|
cleanup:
|
|
|
|
|
2019-04-04 17:31:26 +02:00
|
|
|
mbedtls_free( key0 );
|
|
|
|
mbedtls_free( key1 );
|
2019-03-01 12:21:44 +01:00
|
|
|
|
2019-04-05 10:55:37 +02:00
|
|
|
return( ret );
|
2018-01-03 15:27:32 +01:00
|
|
|
}
|
|
|
|
|
2019-05-23 10:06:14 +02:00
|
|
|
/*
|
2019-06-03 09:55:16 +02:00
|
|
|
* Populate a session structure for serialization tests.
|
2019-05-23 10:06:14 +02:00
|
|
|
* Choose dummy values, mostly non-0 to distinguish from the init default.
|
|
|
|
*/
|
|
|
|
static int ssl_populate_session( mbedtls_ssl_session *session,
|
2019-05-24 09:54:21 +02:00
|
|
|
int ticket_len,
|
|
|
|
const char *crt_file )
|
2019-05-23 10:06:14 +02:00
|
|
|
{
|
|
|
|
#if defined(MBEDTLS_HAVE_TIME)
|
|
|
|
session->start = mbedtls_time( NULL ) - 42;
|
|
|
|
#endif
|
|
|
|
session->ciphersuite = 0xabcd;
|
|
|
|
session->compression = 1;
|
|
|
|
session->id_len = sizeof( session->id );
|
|
|
|
memset( session->id, 66, session->id_len );
|
2019-05-24 09:54:21 +02:00
|
|
|
memset( session->master, 17, sizeof( session->master ) );
|
2019-05-23 10:06:14 +02:00
|
|
|
|
2019-05-24 10:17:52 +02:00
|
|
|
#if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_FS_IO)
|
2019-05-23 10:06:14 +02:00
|
|
|
if( strlen( crt_file ) != 0 )
|
|
|
|
{
|
2019-07-29 13:00:39 +02:00
|
|
|
mbedtls_x509_crt tmp_crt;
|
2019-05-23 10:06:14 +02:00
|
|
|
int ret;
|
2019-05-24 09:40:17 +02:00
|
|
|
|
2019-07-29 13:00:39 +02:00
|
|
|
mbedtls_x509_crt_init( &tmp_crt );
|
|
|
|
ret = mbedtls_x509_crt_parse_file( &tmp_crt, crt_file );
|
|
|
|
if( ret != 0 )
|
|
|
|
return( ret );
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
|
|
|
|
/* Move temporary CRT. */
|
2019-05-24 09:40:17 +02:00
|
|
|
session->peer_cert = mbedtls_calloc( 1, sizeof( *session->peer_cert ) );
|
|
|
|
if( session->peer_cert == NULL )
|
|
|
|
return( -1 );
|
2019-07-29 13:00:39 +02:00
|
|
|
*session->peer_cert = tmp_crt;
|
|
|
|
memset( &tmp_crt, 0, sizeof( tmp_crt ) );
|
|
|
|
#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
|
|
|
|
/* Calculate digest of temporary CRT. */
|
|
|
|
session->peer_cert_digest =
|
|
|
|
mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
|
|
|
|
if( session->peer_cert_digest == NULL )
|
|
|
|
return( -1 );
|
|
|
|
ret = mbedtls_md( mbedtls_md_info_from_type(
|
|
|
|
MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
|
|
|
|
tmp_crt.raw.p, tmp_crt.raw.len,
|
|
|
|
session->peer_cert_digest );
|
2019-05-23 10:06:14 +02:00
|
|
|
if( ret != 0 )
|
|
|
|
return( ret );
|
2019-07-29 13:00:39 +02:00
|
|
|
session->peer_cert_digest_type =
|
|
|
|
MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
|
|
|
|
session->peer_cert_digest_len =
|
|
|
|
MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
|
|
|
|
#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
|
|
|
|
|
|
|
|
mbedtls_x509_crt_free( &tmp_crt );
|
2019-05-23 10:06:14 +02:00
|
|
|
}
|
2019-07-29 13:00:39 +02:00
|
|
|
#else /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_FS_IO */
|
2019-05-23 10:06:14 +02:00
|
|
|
(void) crt_file;
|
2019-07-29 13:00:39 +02:00
|
|
|
#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_FS_IO */
|
2019-05-23 10:06:14 +02:00
|
|
|
session->verify_result = 0xdeadbeef;
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
|
|
|
|
if( ticket_len != 0 )
|
|
|
|
{
|
|
|
|
session->ticket = mbedtls_calloc( 1, ticket_len );
|
2019-05-24 09:54:21 +02:00
|
|
|
if( session->ticket == NULL )
|
2019-05-23 10:06:14 +02:00
|
|
|
return( -1 );
|
|
|
|
memset( session->ticket, 33, ticket_len );
|
|
|
|
}
|
|
|
|
session->ticket_len = ticket_len;
|
|
|
|
session->ticket_lifetime = 86401;
|
|
|
|
#else
|
|
|
|
(void) ticket_len;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
|
|
|
|
session->mfl_code = 1;
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
|
|
|
|
session->trunc_hmac = 1;
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
|
|
|
|
session->encrypt_then_mac = 1;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2014-09-24 11:13:44 +02:00
|
|
|
/* END_HEADER */
|
|
|
|
|
|
|
|
/* BEGIN_DEPENDENCIES
|
2015-04-08 12:49:31 +02:00
|
|
|
* depends_on:MBEDTLS_SSL_TLS_C
|
2014-09-24 11:13:44 +02:00
|
|
|
* END_DEPENDENCIES
|
|
|
|
*/
|
|
|
|
|
2019-11-26 12:11:15 +01:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void test_callback_buffer_sanity()
|
|
|
|
{
|
|
|
|
enum { MSGLEN = 10 };
|
|
|
|
mbedtls_test_buffer buf;
|
|
|
|
unsigned char input[MSGLEN];
|
|
|
|
unsigned char output[MSGLEN];
|
|
|
|
|
|
|
|
memset( input, 0, sizeof(input) );
|
|
|
|
|
|
|
|
/* Make sure calling put and get on NULL buffer results in error. */
|
|
|
|
TEST_ASSERT( mbedtls_test_buffer_put( NULL, input, sizeof( input ) )
|
|
|
|
== -1 );
|
|
|
|
TEST_ASSERT( mbedtls_test_buffer_get( NULL, output, sizeof( output ) )
|
|
|
|
== -1 );
|
|
|
|
TEST_ASSERT( mbedtls_test_buffer_put( NULL, NULL, sizeof( input ) ) == -1 );
|
2020-01-22 12:34:59 +01:00
|
|
|
|
2019-11-26 12:11:15 +01:00
|
|
|
TEST_ASSERT( mbedtls_test_buffer_put( NULL, NULL, 0 ) == -1 );
|
|
|
|
TEST_ASSERT( mbedtls_test_buffer_get( NULL, NULL, 0 ) == -1 );
|
|
|
|
|
|
|
|
/* Make sure calling put and get on a buffer that hasn't been set up results
|
|
|
|
* in eror. */
|
|
|
|
mbedtls_test_buffer_init( &buf );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, sizeof( input ) ) == -1 );
|
|
|
|
TEST_ASSERT( mbedtls_test_buffer_get( &buf, output, sizeof( output ) )
|
|
|
|
== -1 );
|
|
|
|
TEST_ASSERT( mbedtls_test_buffer_put( &buf, NULL, sizeof( input ) ) == -1 );
|
2020-01-22 12:34:59 +01:00
|
|
|
|
2019-11-26 12:11:15 +01:00
|
|
|
TEST_ASSERT( mbedtls_test_buffer_put( &buf, NULL, 0 ) == -1 );
|
|
|
|
TEST_ASSERT( mbedtls_test_buffer_get( &buf, NULL, 0 ) == -1 );
|
|
|
|
|
2020-01-22 12:34:59 +01:00
|
|
|
/* 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.
|
|
|
|
*/
|
2019-11-26 12:11:15 +01:00
|
|
|
|
|
|
|
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 ) )
|
2020-01-22 12:34:59 +01:00
|
|
|
== 0 );
|
2019-11-26 12:11:15 +01:00
|
|
|
TEST_ASSERT( mbedtls_test_buffer_put( &buf, NULL, 0 ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_test_buffer_get( &buf, NULL, 0 ) == 0 );
|
|
|
|
|
|
|
|
exit:
|
|
|
|
|
|
|
|
mbedtls_test_buffer_free( &buf );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Test if the implementation of `mbedtls_test_buffer` related functions is
|
|
|
|
* correct and works as expected.
|
|
|
|
*
|
|
|
|
* That is
|
|
|
|
* - If we try to put in \p put1 bytes then we can put in \p put1_ret bytes.
|
|
|
|
* - Afterwards if we try to get \p get1 bytes then we can get \get1_ret bytes.
|
|
|
|
* - Next, if we try to put in \p put1 bytes then we can put in \p put1_ret
|
|
|
|
* bytes.
|
|
|
|
* - Afterwards if we try to get \p get1 bytes then we can get \get1_ret bytes.
|
|
|
|
* - All of the bytes we got match the bytes we put in in a FIFO manner.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
|
|
|
void test_callback_buffer( int size, int put1, int put1_ret,
|
|
|
|
int get1, int get1_ret, int put2, int put2_ret,
|
|
|
|
int get2, int get2_ret )
|
|
|
|
{
|
|
|
|
enum { ROUNDS = 2 };
|
|
|
|
size_t put[ROUNDS];
|
|
|
|
int put_ret[ROUNDS];
|
|
|
|
size_t get[ROUNDS];
|
|
|
|
int get_ret[ROUNDS];
|
|
|
|
mbedtls_test_buffer buf;
|
|
|
|
unsigned char* input = NULL;
|
|
|
|
size_t input_len;
|
|
|
|
unsigned char* output = NULL;
|
|
|
|
size_t output_len;
|
2019-11-27 12:12:14 +01:00
|
|
|
size_t i, j, written, read;
|
2019-11-26 12:11:15 +01:00
|
|
|
|
|
|
|
mbedtls_test_buffer_init( &buf );
|
|
|
|
TEST_ASSERT( mbedtls_test_buffer_setup( &buf, size ) == 0 );
|
|
|
|
|
|
|
|
/* Check the sanity of input parameters and initialise local variables. That
|
|
|
|
* is, ensure that the amount of data is not negative and that we are not
|
|
|
|
* expecting more to put or get than we actually asked for. */
|
|
|
|
TEST_ASSERT( put1 >= 0 );
|
|
|
|
put[0] = put1;
|
|
|
|
put_ret[0] = put1_ret;
|
|
|
|
TEST_ASSERT( put1_ret <= put1 );
|
|
|
|
TEST_ASSERT( put2 >= 0 );
|
|
|
|
put[1] = put2;
|
|
|
|
put_ret[1] = put2_ret;
|
|
|
|
TEST_ASSERT( put2_ret <= put2 );
|
|
|
|
|
|
|
|
TEST_ASSERT( get1 >= 0 );
|
|
|
|
get[0] = get1;
|
|
|
|
get_ret[0] = get1_ret;
|
|
|
|
TEST_ASSERT( get1_ret <= get1 );
|
|
|
|
TEST_ASSERT( get2 >= 0 );
|
|
|
|
get[1] = get2;
|
|
|
|
get_ret[1] = get2_ret;
|
|
|
|
TEST_ASSERT( get2_ret <= get2 );
|
|
|
|
|
|
|
|
input_len = 0;
|
|
|
|
/* Calculate actual input and output lengths */
|
|
|
|
for( j = 0; j < ROUNDS; j++ )
|
|
|
|
{
|
|
|
|
if( put_ret[j] > 0 )
|
|
|
|
{
|
|
|
|
input_len += put_ret[j];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* In order to always have a valid pointer we always allocate at least 1
|
|
|
|
* byte. */
|
|
|
|
if( input_len == 0 )
|
|
|
|
input_len = 1;
|
|
|
|
ASSERT_ALLOC( input, input_len );
|
|
|
|
|
|
|
|
output_len = 0;
|
|
|
|
for( j = 0; j < ROUNDS; j++ )
|
|
|
|
{
|
|
|
|
if( get_ret[j] > 0 )
|
|
|
|
{
|
|
|
|
output_len += get_ret[j];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
TEST_ASSERT( output_len <= input_len );
|
|
|
|
/* In order to always have a valid pointer we always allocate at least 1
|
|
|
|
* byte. */
|
|
|
|
if( output_len == 0 )
|
|
|
|
output_len = 1;
|
|
|
|
ASSERT_ALLOC( output, output_len );
|
|
|
|
|
|
|
|
/* Fill up the buffer with structured data so that unwanted changes
|
|
|
|
* can be detected */
|
|
|
|
for( i = 0; i < input_len; i++ )
|
|
|
|
{
|
|
|
|
input[i] = i & 0xFF;
|
|
|
|
}
|
|
|
|
|
|
|
|
written = read = 0;
|
|
|
|
for( j = 0; j < ROUNDS; j++ )
|
|
|
|
{
|
|
|
|
TEST_ASSERT( put_ret[j] == mbedtls_test_buffer_put( &buf,
|
|
|
|
input + written, put[j] ) );
|
|
|
|
written += put_ret[j];
|
|
|
|
TEST_ASSERT( get_ret[j] == mbedtls_test_buffer_get( &buf,
|
|
|
|
output + read, get[j] ) );
|
|
|
|
read += get_ret[j];
|
|
|
|
TEST_ASSERT( read <= written );
|
|
|
|
if( get_ret[j] > 0 )
|
|
|
|
{
|
|
|
|
TEST_ASSERT( memcmp( output + read - get_ret[j],
|
|
|
|
input + read - get_ret[j], get_ret[j] )
|
|
|
|
== 0 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
|
|
|
|
mbedtls_free( input );
|
|
|
|
mbedtls_free( output );
|
|
|
|
mbedtls_test_buffer_free( &buf );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2019-11-27 12:12:14 +01:00
|
|
|
/*
|
2019-12-02 16:47:26 +01:00
|
|
|
* Test if the implementation of `mbedtls_mock_socket` related I/O functions is
|
|
|
|
* correct and works as expected on unconnected sockets.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
|
|
|
void ssl_mock_sanity( )
|
|
|
|
{
|
|
|
|
enum { MSGLEN = 105 };
|
|
|
|
unsigned char message[MSGLEN];
|
|
|
|
unsigned char received[MSGLEN];
|
|
|
|
mbedtls_mock_socket socket;
|
|
|
|
|
|
|
|
mbedtls_mock_socket_init( &socket );
|
|
|
|
TEST_ASSERT( mbedtls_mock_tcp_send_b( &socket, message, MSGLEN ) < 0 );
|
|
|
|
mbedtls_mock_socket_close( &socket );
|
|
|
|
mbedtls_mock_socket_init( &socket );
|
|
|
|
TEST_ASSERT( mbedtls_mock_tcp_recv_b( &socket, received, MSGLEN ) < 0 );
|
|
|
|
mbedtls_mock_socket_close( &socket );
|
|
|
|
|
|
|
|
mbedtls_mock_socket_init( &socket );
|
|
|
|
TEST_ASSERT( mbedtls_mock_tcp_send_nb( &socket, message, MSGLEN ) < 0 );
|
|
|
|
mbedtls_mock_socket_close( &socket );
|
|
|
|
mbedtls_mock_socket_init( &socket );
|
|
|
|
TEST_ASSERT( mbedtls_mock_tcp_recv_nb( &socket, received, MSGLEN ) < 0 );
|
|
|
|
mbedtls_mock_socket_close( &socket );
|
|
|
|
|
|
|
|
exit:
|
|
|
|
|
|
|
|
mbedtls_mock_socket_close( &socket );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Test if the implementation of `mbedtls_mock_socket` related functions can
|
|
|
|
* send a single message from the client to the server.
|
2019-11-27 12:12:14 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2019-11-27 14:31:42 +01:00
|
|
|
void ssl_mock_tcp( int blocking, int client_pattern, int server_pattern )
|
2019-11-27 12:12:14 +01:00
|
|
|
{
|
|
|
|
enum { MSGLEN = 105 };
|
2019-12-02 16:47:26 +01:00
|
|
|
unsigned char message[MSGLEN];
|
|
|
|
unsigned char received[MSGLEN];
|
2019-11-27 12:12:14 +01:00
|
|
|
mbedtls_mock_socket client;
|
|
|
|
mbedtls_mock_socket server;
|
2019-12-02 16:47:26 +01:00
|
|
|
size_t written, read;
|
|
|
|
int send_ret, recv_ret;
|
2019-11-27 14:31:42 +01:00
|
|
|
mbedtls_ssl_send_t *send;
|
|
|
|
mbedtls_ssl_recv_t *recv;
|
|
|
|
uint32_t client_block = client_pattern;
|
|
|
|
uint32_t server_block = server_pattern;
|
2019-12-02 16:47:26 +01:00
|
|
|
unsigned i;
|
2019-11-27 14:31:42 +01:00
|
|
|
|
|
|
|
if( blocking == 0 )
|
|
|
|
{
|
|
|
|
send = mbedtls_mock_tcp_send_nb;
|
|
|
|
recv = mbedtls_mock_tcp_recv_nb;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
send = mbedtls_mock_tcp_send_b;
|
|
|
|
recv = mbedtls_mock_tcp_recv_b;
|
|
|
|
}
|
2019-11-27 12:12:14 +01:00
|
|
|
|
|
|
|
mbedtls_mock_socket_init( &client );
|
|
|
|
mbedtls_mock_socket_init( &server );
|
|
|
|
|
2019-12-02 16:47:26 +01:00
|
|
|
/* Fill up the buffer with structured data so that unwanted changes
|
2019-11-27 12:12:14 +01:00
|
|
|
* can be detected */
|
2019-12-02 16:47:26 +01:00
|
|
|
for( i = 0; i < MSGLEN; i++ )
|
2019-11-27 12:12:14 +01:00
|
|
|
{
|
2019-12-02 16:47:26 +01:00
|
|
|
message[i] = i & 0xFF;
|
2019-11-27 12:12:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Make sure that sending a message takes a few iterations. */
|
|
|
|
TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
|
|
|
|
MSGLEN / 5 ) );
|
2019-11-27 14:31:42 +01:00
|
|
|
TEST_ASSERT( 0 == mbedtls_mock_socket_set_block( &client, client_block ) );
|
|
|
|
TEST_ASSERT( 0 == mbedtls_mock_socket_set_block( &server, server_block ) );
|
2019-11-27 12:12:14 +01:00
|
|
|
|
|
|
|
/* Send the message to the server */
|
2019-12-02 16:47:26 +01:00
|
|
|
send_ret = recv_ret = 1;
|
|
|
|
written = read = 0;
|
|
|
|
while( send_ret != 0 || recv_ret != 0 )
|
2019-11-27 12:12:14 +01:00
|
|
|
{
|
2019-12-02 16:47:26 +01:00
|
|
|
send_ret = send( &client, message + written, MSGLEN - written );
|
2019-11-27 12:12:14 +01:00
|
|
|
|
2019-11-27 14:31:42 +01:00
|
|
|
if( ( blocking == 0 ) && ( client_block & 1 ) )
|
|
|
|
{
|
2019-12-02 16:47:26 +01:00
|
|
|
TEST_ASSERT( send_ret == MBEDTLS_ERR_SSL_WANT_WRITE );
|
2019-11-27 14:31:42 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-12-02 16:47:26 +01:00
|
|
|
TEST_ASSERT( send_ret >= 0 );
|
|
|
|
written += send_ret;
|
2019-11-27 14:31:42 +01:00
|
|
|
}
|
|
|
|
client_block >>= 1;
|
|
|
|
|
2019-12-02 16:47:26 +01:00
|
|
|
recv_ret = recv( &server, received + read, MSGLEN - read );
|
2019-11-27 14:31:42 +01:00
|
|
|
if( ( blocking == 0 ) && ( server_block & 1 ) )
|
|
|
|
{
|
2019-12-02 16:47:26 +01:00
|
|
|
TEST_ASSERT( recv_ret == MBEDTLS_ERR_SSL_WANT_READ );
|
2019-11-27 14:31:42 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-12-02 16:47:26 +01:00
|
|
|
TEST_ASSERT( recv_ret >= 0 );
|
|
|
|
read += recv_ret;
|
2019-11-27 14:31:42 +01:00
|
|
|
}
|
|
|
|
server_block >>= 1;
|
2019-11-27 12:12:14 +01:00
|
|
|
}
|
2019-12-02 16:47:26 +01:00
|
|
|
TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
|
|
|
|
|
|
|
|
exit:
|
2019-11-27 12:12:14 +01:00
|
|
|
|
|
|
|
mbedtls_mock_socket_close( &client );
|
|
|
|
mbedtls_mock_socket_close( &server );
|
2019-12-02 16:47:26 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Test if the implementation of `mbedtls_mock_socket` related functions can
|
|
|
|
* send messages in both direction at the same time (with the I/O calls
|
|
|
|
* interleaving).
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
|
|
|
void ssl_mock_tcp_interleaving( int blocking,
|
|
|
|
int client_pattern, int server_pattern )
|
|
|
|
{
|
|
|
|
enum { ROUNDS = 2 };
|
|
|
|
enum { MSGLEN = 105 };
|
|
|
|
unsigned char message[ROUNDS][MSGLEN];
|
|
|
|
unsigned char received[ROUNDS][MSGLEN];
|
|
|
|
mbedtls_mock_socket client;
|
|
|
|
mbedtls_mock_socket server;
|
|
|
|
size_t written[ROUNDS];
|
|
|
|
size_t read[ROUNDS];
|
|
|
|
int send_ret[ROUNDS];
|
|
|
|
int recv_ret[ROUNDS];
|
|
|
|
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 )
|
|
|
|
{
|
|
|
|
send = mbedtls_mock_tcp_send_nb;
|
|
|
|
recv = mbedtls_mock_tcp_recv_nb;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
send = mbedtls_mock_tcp_send_b;
|
|
|
|
recv = mbedtls_mock_tcp_recv_b;
|
|
|
|
}
|
|
|
|
|
2019-11-27 12:12:14 +01:00
|
|
|
mbedtls_mock_socket_init( &client );
|
|
|
|
mbedtls_mock_socket_init( &server );
|
2019-12-02 16:47:26 +01:00
|
|
|
|
|
|
|
/* Fill up the buffers with structured data so that unwanted changes
|
|
|
|
* can be detected */
|
|
|
|
for( i = 0; i < ROUNDS; i++ )
|
|
|
|
{
|
|
|
|
for( j = 0; j < MSGLEN; j++ )
|
|
|
|
{
|
|
|
|
message[i][j] = ( i * MSGLEN + j ) & 0xFF;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-27 12:12:14 +01:00
|
|
|
/* Make sure that sending a message takes a few iterations. */
|
|
|
|
TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
|
|
|
|
MSGLEN / 5 ) );
|
2019-11-27 14:31:42 +01:00
|
|
|
TEST_ASSERT( 0 == mbedtls_mock_socket_set_block( &client, client_block ) );
|
|
|
|
TEST_ASSERT( 0 == mbedtls_mock_socket_set_block( &server, server_block ) );
|
2019-11-27 12:12:14 +01:00
|
|
|
|
|
|
|
/* Send the message from both sides, interleaving. */
|
|
|
|
progress = 1;
|
|
|
|
for( i = 0; i < ROUNDS; i++ )
|
|
|
|
{
|
|
|
|
written[i] = 0;
|
|
|
|
read[i] = 0;
|
|
|
|
}
|
|
|
|
/* This loop does not stop as long as there was a successful write or read
|
|
|
|
* of at least one byte on either side. */
|
|
|
|
while( progress != 0 )
|
|
|
|
{
|
2019-11-27 14:31:42 +01:00
|
|
|
send_ret[0] = send( &client, message[0] + written[0],
|
2019-11-27 12:12:14 +01:00
|
|
|
MSGLEN - written[0] );
|
2019-11-27 14:31:42 +01:00
|
|
|
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;
|
2019-11-27 12:12:14 +01:00
|
|
|
|
2019-11-27 14:31:42 +01:00
|
|
|
send_ret[1] = send( &server, message[1] + written[1],
|
2019-11-27 12:12:14 +01:00
|
|
|
MSGLEN - written[1] );
|
2019-11-27 14:31:42 +01:00
|
|
|
if( ( blocking == 0 ) && ( server_block & 1 ) )
|
|
|
|
{
|
|
|
|
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;
|
2019-11-27 12:12:14 +01:00
|
|
|
|
2019-11-27 14:31:42 +01:00
|
|
|
recv_ret[0] = recv( &server, received[0] + read[0],
|
2019-11-27 12:12:14 +01:00
|
|
|
MSGLEN - read[0] );
|
2019-11-27 14:31:42 +01:00
|
|
|
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;
|
2019-11-27 12:12:14 +01:00
|
|
|
|
2019-11-27 14:31:42 +01:00
|
|
|
recv_ret[1] = recv( &client, received[1] + read[1],
|
2019-11-27 12:12:14 +01:00
|
|
|
MSGLEN - read[1] );
|
2019-11-27 14:31:42 +01:00
|
|
|
if( ( blocking == 0 ) && ( client_block & 1 ) )
|
|
|
|
{
|
|
|
|
TEST_ASSERT( recv_ret[1] == MBEDTLS_ERR_SSL_WANT_READ );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TEST_ASSERT( recv_ret[1] >= 0 );
|
|
|
|
read[1] += recv_ret[1];
|
|
|
|
}
|
|
|
|
client_block >>= 1;
|
2019-11-27 12:12:14 +01:00
|
|
|
|
|
|
|
progress = 0;
|
|
|
|
for( i = 0; i < ROUNDS; i++ )
|
|
|
|
{
|
2019-11-27 14:31:42 +01:00
|
|
|
if( ( send_ret[i] > 0 ) ||
|
|
|
|
( send_ret[i] == MBEDTLS_ERR_SSL_WANT_WRITE ) )
|
|
|
|
{
|
2019-11-27 12:12:14 +01:00
|
|
|
progress++;
|
2019-11-27 14:31:42 +01:00
|
|
|
}
|
2019-11-27 12:12:14 +01:00
|
|
|
|
2019-11-27 14:31:42 +01:00
|
|
|
if( ( recv_ret[i] > 0 ) ||
|
|
|
|
( recv_ret[i] == MBEDTLS_ERR_SSL_WANT_READ ) )
|
|
|
|
{
|
2019-11-27 12:12:14 +01:00
|
|
|
progress++;
|
2019-11-27 14:31:42 +01:00
|
|
|
}
|
2019-11-27 12:12:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for( i = 0; i < ROUNDS; i++ )
|
|
|
|
TEST_ASSERT( memcmp( message[i], received[i], MSGLEN ) == 0 );
|
|
|
|
|
|
|
|
exit:
|
|
|
|
|
|
|
|
mbedtls_mock_socket_close( &client );
|
|
|
|
mbedtls_mock_socket_close( &server );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_SSL_DTLS_ANTI_REPLAY */
|
2018-06-29 12:05:32 +02:00
|
|
|
void ssl_dtls_replay( data_t * prevs, data_t * new, int ret )
|
2014-09-24 11:13:44 +02:00
|
|
|
{
|
2017-06-09 05:32:58 +02:00
|
|
|
uint32_t len = 0;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ssl_context ssl;
|
2015-05-04 14:56:36 +02:00
|
|
|
mbedtls_ssl_config conf;
|
2014-09-24 11:13:44 +02:00
|
|
|
|
2015-04-29 00:48:22 +02:00
|
|
|
mbedtls_ssl_init( &ssl );
|
2015-05-04 14:56:36 +02:00
|
|
|
mbedtls_ssl_config_init( &conf );
|
2015-04-29 00:48:22 +02:00
|
|
|
|
2015-05-04 19:32:36 +02:00
|
|
|
TEST_ASSERT( mbedtls_ssl_config_defaults( &conf,
|
|
|
|
MBEDTLS_SSL_IS_CLIENT,
|
2015-06-17 13:53:47 +02:00
|
|
|
MBEDTLS_SSL_TRANSPORT_DATAGRAM,
|
|
|
|
MBEDTLS_SSL_PRESET_DEFAULT ) == 0 );
|
2015-05-04 14:56:36 +02:00
|
|
|
TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
|
2014-09-24 11:13:44 +02:00
|
|
|
|
|
|
|
/* Read previous record numbers */
|
2017-06-09 05:32:58 +02:00
|
|
|
for( len = 0; len < prevs->len; len += 6 )
|
2014-09-24 11:13:44 +02:00
|
|
|
{
|
2017-06-09 05:32:58 +02:00
|
|
|
memcpy( ssl.in_ctr + 2, prevs->x + len, 6 );
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ssl_dtls_replay_update( &ssl );
|
2014-09-24 11:13:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check new number */
|
2017-06-09 05:32:58 +02:00
|
|
|
memcpy( ssl.in_ctr + 2, new->x, 6 );
|
2015-04-08 12:49:31 +02:00
|
|
|
TEST_ASSERT( mbedtls_ssl_dtls_replay_check( &ssl ) == ret );
|
2014-09-24 11:13:44 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ssl_free( &ssl );
|
2015-05-04 14:56:36 +02:00
|
|
|
mbedtls_ssl_config_free( &conf );
|
2014-09-24 11:13:44 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2017-05-05 12:24:30 +02:00
|
|
|
|
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
|
|
|
|
void ssl_set_hostname_twice( char *hostname0, char *hostname1 )
|
|
|
|
{
|
|
|
|
mbedtls_ssl_context ssl;
|
|
|
|
mbedtls_ssl_init( &ssl );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_ssl_set_hostname( &ssl, hostname0 ) == 0 );
|
|
|
|
TEST_ASSERT( mbedtls_ssl_set_hostname( &ssl, hostname1 ) == 0 );
|
|
|
|
|
|
|
|
mbedtls_ssl_free( &ssl );
|
|
|
|
}
|
2018-03-13 16:22:58 +01:00
|
|
|
/* END_CASE */
|
2018-01-03 15:27:32 +01:00
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
|
|
|
void ssl_crypt_record( int cipher_type, int hash_id,
|
2019-04-29 18:30:59 +02:00
|
|
|
int etm, int tag_mode, int ver,
|
|
|
|
int cid0_len, int cid1_len )
|
2018-01-03 15:27:32 +01:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Test several record encryptions and decryptions
|
|
|
|
* with plenty of space before and after the data
|
|
|
|
* within the record buffer.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int ret;
|
|
|
|
int num_records = 16;
|
|
|
|
mbedtls_ssl_context ssl; /* ONLY for debugging */
|
|
|
|
|
|
|
|
mbedtls_ssl_transform t0, t1;
|
2019-03-01 12:21:44 +01:00
|
|
|
unsigned char *buf = NULL;
|
2018-01-03 15:27:32 +01:00
|
|
|
size_t const buflen = 512;
|
|
|
|
mbedtls_record rec, rec_backup;
|
|
|
|
|
|
|
|
mbedtls_ssl_init( &ssl );
|
|
|
|
mbedtls_ssl_transform_init( &t0 );
|
|
|
|
mbedtls_ssl_transform_init( &t1 );
|
|
|
|
TEST_ASSERT( build_transforms( &t0, &t1, cipher_type, hash_id,
|
2019-04-29 18:30:59 +02:00
|
|
|
etm, tag_mode, ver,
|
|
|
|
(size_t) cid0_len,
|
|
|
|
(size_t) cid1_len ) == 0 );
|
2018-01-03 15:27:32 +01:00
|
|
|
|
2019-04-04 17:31:26 +02:00
|
|
|
TEST_ASSERT( ( buf = mbedtls_calloc( 1, buflen ) ) != NULL );
|
2018-01-03 15:27:32 +01:00
|
|
|
|
|
|
|
while( num_records-- > 0 )
|
|
|
|
{
|
|
|
|
mbedtls_ssl_transform *t_dec, *t_enc;
|
|
|
|
/* Take turns in who's sending and who's receiving. */
|
|
|
|
if( num_records % 3 == 0 )
|
|
|
|
{
|
|
|
|
t_dec = &t0;
|
|
|
|
t_enc = &t1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
t_dec = &t1;
|
|
|
|
t_enc = &t0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The record header affects the transformation in two ways:
|
|
|
|
* 1) It determines the AEAD additional data
|
|
|
|
* 2) The record counter sometimes determines the IV.
|
|
|
|
*
|
|
|
|
* Apart from that, the fields don't have influence.
|
|
|
|
* In particular, it is currently not the responsibility
|
|
|
|
* of ssl_encrypt/decrypt_buf to check if the transform
|
|
|
|
* version matches the record version, or that the
|
|
|
|
* type is sensible.
|
|
|
|
*/
|
|
|
|
|
|
|
|
memset( rec.ctr, num_records, sizeof( rec.ctr ) );
|
|
|
|
rec.type = 42;
|
|
|
|
rec.ver[0] = num_records;
|
|
|
|
rec.ver[1] = num_records;
|
2019-05-15 15:03:01 +02:00
|
|
|
#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
|
2019-04-29 18:30:59 +02:00
|
|
|
rec.cid_len = 0;
|
2019-05-15 15:03:01 +02:00
|
|
|
#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
|
2018-01-03 15:27:32 +01:00
|
|
|
|
|
|
|
rec.buf = buf;
|
|
|
|
rec.buf_len = buflen;
|
|
|
|
rec.data_offset = 16;
|
|
|
|
/* Make sure to vary the length to exercise different
|
|
|
|
* paddings. */
|
|
|
|
rec.data_len = 1 + num_records;
|
|
|
|
|
|
|
|
memset( rec.buf + rec.data_offset, 42, rec.data_len );
|
|
|
|
|
|
|
|
/* Make a copy for later comparison */
|
|
|
|
rec_backup = rec;
|
|
|
|
|
|
|
|
/* Encrypt record */
|
|
|
|
ret = mbedtls_ssl_encrypt_buf( &ssl, t_enc, &rec,
|
|
|
|
rnd_std_rand, NULL );
|
|
|
|
TEST_ASSERT( ret == 0 || ret == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
|
|
|
|
if( ret != 0 )
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Decrypt record with t_dec */
|
2019-04-29 18:30:59 +02:00
|
|
|
ret = mbedtls_ssl_decrypt_buf( &ssl, t_dec, &rec );
|
|
|
|
TEST_ASSERT( ret == 0 );
|
2018-01-03 15:27:32 +01:00
|
|
|
|
|
|
|
/* Compare results */
|
|
|
|
TEST_ASSERT( rec.type == rec_backup.type );
|
|
|
|
TEST_ASSERT( memcmp( rec.ctr, rec_backup.ctr, 8 ) == 0 );
|
|
|
|
TEST_ASSERT( rec.ver[0] == rec_backup.ver[0] );
|
|
|
|
TEST_ASSERT( rec.ver[1] == rec_backup.ver[1] );
|
|
|
|
TEST_ASSERT( rec.data_len == rec_backup.data_len );
|
|
|
|
TEST_ASSERT( rec.data_offset == rec_backup.data_offset );
|
|
|
|
TEST_ASSERT( memcmp( rec.buf + rec.data_offset,
|
|
|
|
rec_backup.buf + rec_backup.data_offset,
|
|
|
|
rec.data_len ) == 0 );
|
|
|
|
}
|
|
|
|
|
2019-03-01 12:21:44 +01:00
|
|
|
exit:
|
|
|
|
|
2018-01-03 15:27:32 +01:00
|
|
|
/* Cleanup */
|
|
|
|
mbedtls_ssl_free( &ssl );
|
|
|
|
mbedtls_ssl_transform_free( &t0 );
|
|
|
|
mbedtls_ssl_transform_free( &t1 );
|
|
|
|
|
2019-04-04 17:31:26 +02:00
|
|
|
mbedtls_free( buf );
|
2018-01-03 15:27:32 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
Add encryption/decryption tests for small records
This commit adds tests to check the behavior of the record encryption
routine `ssl_encrypt_buf` when the buffer surrounding the plaintext is
too small to hold the expansion in the beginning and end (due to IV's,
padding, and MAC).
Each test starts successively increases the space available at the
beginning, end, or both, of the record buffer, and checks that the
record encryption either fails with a BUFFER_TOO_SMALL error, or
that it succeeds. Moreover, if it succeeds, it is checked that
decryption succeeds, too, and results in the original record.
2018-01-05 16:20:24 +01:00
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
|
|
|
void ssl_crypt_record_small( int cipher_type, int hash_id,
|
2019-04-29 18:30:59 +02:00
|
|
|
int etm, int tag_mode, int ver,
|
|
|
|
int cid0_len, int cid1_len )
|
Add encryption/decryption tests for small records
This commit adds tests to check the behavior of the record encryption
routine `ssl_encrypt_buf` when the buffer surrounding the plaintext is
too small to hold the expansion in the beginning and end (due to IV's,
padding, and MAC).
Each test starts successively increases the space available at the
beginning, end, or both, of the record buffer, and checks that the
record encryption either fails with a BUFFER_TOO_SMALL error, or
that it succeeds. Moreover, if it succeeds, it is checked that
decryption succeeds, too, and results in the original record.
2018-01-05 16:20:24 +01:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Test pairs of encryption and decryption with an increasing
|
|
|
|
* amount of space in the record buffer - in more detail:
|
|
|
|
* 1) Try to encrypt with 0, 1, 2, ... bytes available
|
|
|
|
* in front of the plaintext, and expect the encryption
|
|
|
|
* to succeed starting from some offset. Always keep
|
|
|
|
* enough space in the end of the buffer.
|
|
|
|
* 2) Try to encrypt with 0, 1, 2, ... bytes available
|
|
|
|
* at the end of the plaintext, and expect the encryption
|
|
|
|
* to succeed starting from some offset. Always keep
|
|
|
|
* enough space at the beginning of the buffer.
|
|
|
|
* 3) Try to encrypt with 0, 1, 2, ... bytes available
|
|
|
|
* both at the front and end of the plaintext,
|
|
|
|
* and expect the encryption to succeed starting from
|
|
|
|
* some offset.
|
|
|
|
*
|
|
|
|
* If encryption succeeds, check that decryption succeeds
|
|
|
|
* and yields the original record.
|
|
|
|
*/
|
|
|
|
|
|
|
|
mbedtls_ssl_context ssl; /* ONLY for debugging */
|
|
|
|
|
|
|
|
mbedtls_ssl_transform t0, t1;
|
2019-03-01 12:21:44 +01:00
|
|
|
unsigned char *buf = NULL;
|
2019-04-29 18:30:59 +02:00
|
|
|
size_t const buflen = 256;
|
Add encryption/decryption tests for small records
This commit adds tests to check the behavior of the record encryption
routine `ssl_encrypt_buf` when the buffer surrounding the plaintext is
too small to hold the expansion in the beginning and end (due to IV's,
padding, and MAC).
Each test starts successively increases the space available at the
beginning, end, or both, of the record buffer, and checks that the
record encryption either fails with a BUFFER_TOO_SMALL error, or
that it succeeds. Moreover, if it succeeds, it is checked that
decryption succeeds, too, and results in the original record.
2018-01-05 16:20:24 +01:00
|
|
|
mbedtls_record rec, rec_backup;
|
|
|
|
|
|
|
|
int ret;
|
2019-04-29 18:30:59 +02:00
|
|
|
int mode; /* Mode 1, 2 or 3 as explained above */
|
|
|
|
size_t offset; /* Available space at beginning/end/both */
|
|
|
|
size_t threshold = 96; /* Maximum offset to test against */
|
Add encryption/decryption tests for small records
This commit adds tests to check the behavior of the record encryption
routine `ssl_encrypt_buf` when the buffer surrounding the plaintext is
too small to hold the expansion in the beginning and end (due to IV's,
padding, and MAC).
Each test starts successively increases the space available at the
beginning, end, or both, of the record buffer, and checks that the
record encryption either fails with a BUFFER_TOO_SMALL error, or
that it succeeds. Moreover, if it succeeds, it is checked that
decryption succeeds, too, and results in the original record.
2018-01-05 16:20:24 +01:00
|
|
|
|
2019-04-29 18:30:59 +02:00
|
|
|
size_t default_pre_padding = 64; /* Pre-padding to use in mode 2 */
|
|
|
|
size_t default_post_padding = 128; /* Post-padding to use in mode 1 */
|
Add encryption/decryption tests for small records
This commit adds tests to check the behavior of the record encryption
routine `ssl_encrypt_buf` when the buffer surrounding the plaintext is
too small to hold the expansion in the beginning and end (due to IV's,
padding, and MAC).
Each test starts successively increases the space available at the
beginning, end, or both, of the record buffer, and checks that the
record encryption either fails with a BUFFER_TOO_SMALL error, or
that it succeeds. Moreover, if it succeeds, it is checked that
decryption succeeds, too, and results in the original record.
2018-01-05 16:20:24 +01:00
|
|
|
|
|
|
|
int seen_success; /* Indicates if in the current mode we've
|
|
|
|
* already seen a successful test. */
|
|
|
|
|
|
|
|
mbedtls_ssl_init( &ssl );
|
|
|
|
mbedtls_ssl_transform_init( &t0 );
|
|
|
|
mbedtls_ssl_transform_init( &t1 );
|
|
|
|
TEST_ASSERT( build_transforms( &t0, &t1, cipher_type, hash_id,
|
2019-04-29 18:30:59 +02:00
|
|
|
etm, tag_mode, ver,
|
|
|
|
(size_t) cid0_len,
|
|
|
|
(size_t) cid1_len ) == 0 );
|
Add encryption/decryption tests for small records
This commit adds tests to check the behavior of the record encryption
routine `ssl_encrypt_buf` when the buffer surrounding the plaintext is
too small to hold the expansion in the beginning and end (due to IV's,
padding, and MAC).
Each test starts successively increases the space available at the
beginning, end, or both, of the record buffer, and checks that the
record encryption either fails with a BUFFER_TOO_SMALL error, or
that it succeeds. Moreover, if it succeeds, it is checked that
decryption succeeds, too, and results in the original record.
2018-01-05 16:20:24 +01:00
|
|
|
|
2019-04-04 17:31:26 +02:00
|
|
|
TEST_ASSERT( ( buf = mbedtls_calloc( 1, buflen ) ) != NULL );
|
Add encryption/decryption tests for small records
This commit adds tests to check the behavior of the record encryption
routine `ssl_encrypt_buf` when the buffer surrounding the plaintext is
too small to hold the expansion in the beginning and end (due to IV's,
padding, and MAC).
Each test starts successively increases the space available at the
beginning, end, or both, of the record buffer, and checks that the
record encryption either fails with a BUFFER_TOO_SMALL error, or
that it succeeds. Moreover, if it succeeds, it is checked that
decryption succeeds, too, and results in the original record.
2018-01-05 16:20:24 +01:00
|
|
|
|
|
|
|
for( mode=1; mode <= 3; mode++ )
|
|
|
|
{
|
|
|
|
seen_success = 0;
|
|
|
|
for( offset=0; offset <= threshold; offset++ )
|
|
|
|
{
|
|
|
|
mbedtls_ssl_transform *t_dec, *t_enc;
|
2019-04-29 18:24:44 +02:00
|
|
|
t_dec = &t0;
|
|
|
|
t_enc = &t1;
|
Add encryption/decryption tests for small records
This commit adds tests to check the behavior of the record encryption
routine `ssl_encrypt_buf` when the buffer surrounding the plaintext is
too small to hold the expansion in the beginning and end (due to IV's,
padding, and MAC).
Each test starts successively increases the space available at the
beginning, end, or both, of the record buffer, and checks that the
record encryption either fails with a BUFFER_TOO_SMALL error, or
that it succeeds. Moreover, if it succeeds, it is checked that
decryption succeeds, too, and results in the original record.
2018-01-05 16:20:24 +01:00
|
|
|
|
|
|
|
memset( rec.ctr, offset, sizeof( rec.ctr ) );
|
|
|
|
rec.type = 42;
|
|
|
|
rec.ver[0] = offset;
|
|
|
|
rec.ver[1] = offset;
|
|
|
|
rec.buf = buf;
|
|
|
|
rec.buf_len = buflen;
|
2019-05-15 15:03:01 +02:00
|
|
|
#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
|
2019-04-29 18:30:59 +02:00
|
|
|
rec.cid_len = 0;
|
2019-05-15 15:03:01 +02:00
|
|
|
#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
|
Add encryption/decryption tests for small records
This commit adds tests to check the behavior of the record encryption
routine `ssl_encrypt_buf` when the buffer surrounding the plaintext is
too small to hold the expansion in the beginning and end (due to IV's,
padding, and MAC).
Each test starts successively increases the space available at the
beginning, end, or both, of the record buffer, and checks that the
record encryption either fails with a BUFFER_TOO_SMALL error, or
that it succeeds. Moreover, if it succeeds, it is checked that
decryption succeeds, too, and results in the original record.
2018-01-05 16:20:24 +01:00
|
|
|
|
|
|
|
switch( mode )
|
|
|
|
{
|
|
|
|
case 1: /* Space in the beginning */
|
|
|
|
rec.data_offset = offset;
|
|
|
|
rec.data_len = buflen - offset - default_post_padding;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2: /* Space in the end */
|
|
|
|
rec.data_offset = default_pre_padding;
|
|
|
|
rec.data_len = buflen - default_pre_padding - offset;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3: /* Space in the beginning and end */
|
|
|
|
rec.data_offset = offset;
|
|
|
|
rec.data_len = buflen - 2 * offset;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
TEST_ASSERT( 0 );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset( rec.buf + rec.data_offset, 42, rec.data_len );
|
|
|
|
|
|
|
|
/* Make a copy for later comparison */
|
|
|
|
rec_backup = rec;
|
|
|
|
|
|
|
|
/* Encrypt record */
|
|
|
|
ret = mbedtls_ssl_encrypt_buf( &ssl, t_enc, &rec, rnd_std_rand, NULL );
|
|
|
|
|
|
|
|
if( ( mode == 1 || mode == 2 ) && seen_success )
|
|
|
|
{
|
|
|
|
TEST_ASSERT( ret == 0 );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TEST_ASSERT( ret == 0 || ret == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
|
|
|
|
if( ret == 0 )
|
|
|
|
seen_success = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ret != 0 )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Decrypt record with t_dec */
|
|
|
|
TEST_ASSERT( mbedtls_ssl_decrypt_buf( &ssl, t_dec, &rec ) == 0 );
|
|
|
|
|
|
|
|
/* Compare results */
|
|
|
|
TEST_ASSERT( rec.type == rec_backup.type );
|
|
|
|
TEST_ASSERT( memcmp( rec.ctr, rec_backup.ctr, 8 ) == 0 );
|
|
|
|
TEST_ASSERT( rec.ver[0] == rec_backup.ver[0] );
|
|
|
|
TEST_ASSERT( rec.ver[1] == rec_backup.ver[1] );
|
|
|
|
TEST_ASSERT( rec.data_len == rec_backup.data_len );
|
|
|
|
TEST_ASSERT( rec.data_offset == rec_backup.data_offset );
|
|
|
|
TEST_ASSERT( memcmp( rec.buf + rec.data_offset,
|
|
|
|
rec_backup.buf + rec_backup.data_offset,
|
|
|
|
rec.data_len ) == 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_ASSERT( seen_success == 1 );
|
|
|
|
}
|
|
|
|
|
2019-03-01 12:21:44 +01:00
|
|
|
exit:
|
|
|
|
|
Add encryption/decryption tests for small records
This commit adds tests to check the behavior of the record encryption
routine `ssl_encrypt_buf` when the buffer surrounding the plaintext is
too small to hold the expansion in the beginning and end (due to IV's,
padding, and MAC).
Each test starts successively increases the space available at the
beginning, end, or both, of the record buffer, and checks that the
record encryption either fails with a BUFFER_TOO_SMALL error, or
that it succeeds. Moreover, if it succeeds, it is checked that
decryption succeeds, too, and results in the original record.
2018-01-05 16:20:24 +01:00
|
|
|
/* Cleanup */
|
|
|
|
mbedtls_ssl_free( &ssl );
|
|
|
|
mbedtls_ssl_transform_free( &t0 );
|
|
|
|
mbedtls_ssl_transform_free( &t1 );
|
|
|
|
|
2019-04-04 17:31:26 +02:00
|
|
|
mbedtls_free( buf );
|
Add encryption/decryption tests for small records
This commit adds tests to check the behavior of the record encryption
routine `ssl_encrypt_buf` when the buffer surrounding the plaintext is
too small to hold the expansion in the beginning and end (due to IV's,
padding, and MAC).
Each test starts successively increases the space available at the
beginning, end, or both, of the record buffer, and checks that the
record encryption either fails with a BUFFER_TOO_SMALL error, or
that it succeeds. Moreover, if it succeeds, it is checked that
decryption succeeds, too, and results in the original record.
2018-01-05 16:20:24 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2019-05-13 13:09:00 +02:00
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
|
|
|
void ssl_tls_prf( int type, data_t * secret, data_t * random,
|
|
|
|
char *label, data_t *result_hex_str, int exp_ret )
|
|
|
|
{
|
|
|
|
unsigned char *output;
|
|
|
|
|
|
|
|
output = mbedtls_calloc( 1, result_hex_str->len );
|
|
|
|
if( output == NULL )
|
|
|
|
goto exit;
|
|
|
|
|
2019-05-15 16:04:33 +02:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
TEST_ASSERT( psa_crypto_init() == 0 );
|
|
|
|
#endif
|
|
|
|
|
2019-05-13 13:09:00 +02:00
|
|
|
TEST_ASSERT( mbedtls_ssl_tls_prf( type, secret->x, secret->len,
|
|
|
|
label, random->x, random->len,
|
|
|
|
output, result_hex_str->len ) == exp_ret );
|
|
|
|
|
|
|
|
if( exp_ret == 0 )
|
|
|
|
{
|
|
|
|
TEST_ASSERT( hexcmp( output, result_hex_str->x,
|
|
|
|
result_hex_str->len, result_hex_str->len ) == 0 );
|
|
|
|
}
|
|
|
|
exit:
|
|
|
|
|
|
|
|
mbedtls_free( output );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
2019-05-23 09:30:55 +02:00
|
|
|
|
2019-05-24 09:41:39 +02:00
|
|
|
/* BEGIN_CASE */
|
2019-06-03 09:55:16 +02:00
|
|
|
void ssl_serialize_session_save_load( int ticket_len, char *crt_file )
|
2019-05-24 09:41:39 +02:00
|
|
|
{
|
|
|
|
mbedtls_ssl_session original, restored;
|
|
|
|
unsigned char *buf = NULL;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Test that a save-load pair is the identity
|
|
|
|
*/
|
|
|
|
|
|
|
|
mbedtls_ssl_session_init( &original );
|
|
|
|
mbedtls_ssl_session_init( &restored );
|
|
|
|
|
|
|
|
/* Prepare a dummy session to work on */
|
|
|
|
TEST_ASSERT( ssl_populate_session( &original, ticket_len, crt_file ) == 0 );
|
|
|
|
|
2019-06-03 09:55:16 +02:00
|
|
|
/* Serialize it */
|
2019-05-24 09:41:39 +02:00
|
|
|
TEST_ASSERT( mbedtls_ssl_session_save( &original, NULL, 0, &len )
|
|
|
|
== MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
|
|
|
|
TEST_ASSERT( ( buf = mbedtls_calloc( 1, len ) ) != NULL );
|
|
|
|
TEST_ASSERT( mbedtls_ssl_session_save( &original, buf, len, &len )
|
|
|
|
== 0 );
|
|
|
|
|
2019-06-03 09:55:16 +02:00
|
|
|
/* Restore session from serialized data */
|
2019-05-24 09:41:39 +02:00
|
|
|
TEST_ASSERT( mbedtls_ssl_session_load( &restored, buf, len) == 0 );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make sure both session structures are identical
|
|
|
|
*/
|
|
|
|
#if defined(MBEDTLS_HAVE_TIME)
|
|
|
|
TEST_ASSERT( original.start == restored.start );
|
|
|
|
#endif
|
|
|
|
TEST_ASSERT( original.ciphersuite == restored.ciphersuite );
|
|
|
|
TEST_ASSERT( original.compression == restored.compression );
|
|
|
|
TEST_ASSERT( original.id_len == restored.id_len );
|
|
|
|
TEST_ASSERT( memcmp( original.id,
|
|
|
|
restored.id, sizeof( original.id ) ) == 0 );
|
|
|
|
TEST_ASSERT( memcmp( original.master,
|
|
|
|
restored.master, sizeof( original.master ) ) == 0 );
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
2019-07-29 13:00:39 +02:00
|
|
|
#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
|
2019-05-24 09:41:39 +02:00
|
|
|
TEST_ASSERT( ( original.peer_cert == NULL ) ==
|
|
|
|
( restored.peer_cert == NULL ) );
|
|
|
|
if( original.peer_cert != NULL )
|
|
|
|
{
|
|
|
|
TEST_ASSERT( original.peer_cert->raw.len ==
|
|
|
|
restored.peer_cert->raw.len );
|
|
|
|
TEST_ASSERT( memcmp( original.peer_cert->raw.p,
|
|
|
|
restored.peer_cert->raw.p,
|
|
|
|
original.peer_cert->raw.len ) == 0 );
|
|
|
|
}
|
2019-07-29 13:00:39 +02:00
|
|
|
#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
|
|
|
|
TEST_ASSERT( original.peer_cert_digest_type ==
|
|
|
|
restored.peer_cert_digest_type );
|
|
|
|
TEST_ASSERT( original.peer_cert_digest_len ==
|
|
|
|
restored.peer_cert_digest_len );
|
|
|
|
TEST_ASSERT( ( original.peer_cert_digest == NULL ) ==
|
|
|
|
( restored.peer_cert_digest == NULL ) );
|
|
|
|
if( original.peer_cert_digest != NULL )
|
|
|
|
{
|
|
|
|
TEST_ASSERT( memcmp( original.peer_cert_digest,
|
|
|
|
restored.peer_cert_digest,
|
|
|
|
original.peer_cert_digest_len ) == 0 );
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
|
|
|
|
#endif /* MBEDTLS_X509_CRT_PARSE_C */
|
2019-05-24 09:41:39 +02:00
|
|
|
TEST_ASSERT( original.verify_result == restored.verify_result );
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
|
|
|
|
TEST_ASSERT( original.ticket_len == restored.ticket_len );
|
|
|
|
if( original.ticket_len != 0 )
|
|
|
|
{
|
|
|
|
TEST_ASSERT( original.ticket != NULL );
|
|
|
|
TEST_ASSERT( restored.ticket != NULL );
|
|
|
|
TEST_ASSERT( memcmp( original.ticket,
|
|
|
|
restored.ticket, original.ticket_len ) == 0 );
|
|
|
|
}
|
|
|
|
TEST_ASSERT( original.ticket_lifetime == restored.ticket_lifetime );
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
|
|
|
|
TEST_ASSERT( original.mfl_code == restored.mfl_code );
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
|
|
|
|
TEST_ASSERT( original.trunc_hmac == restored.trunc_hmac );
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
|
|
|
|
TEST_ASSERT( original.encrypt_then_mac == restored.encrypt_then_mac );
|
|
|
|
#endif
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_ssl_session_free( &original );
|
|
|
|
mbedtls_ssl_session_free( &restored );
|
|
|
|
mbedtls_free( buf );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2019-06-03 10:53:47 +02:00
|
|
|
/* BEGIN_CASE */
|
2019-06-03 09:55:16 +02:00
|
|
|
void ssl_serialize_session_load_save( int ticket_len, char *crt_file )
|
2019-05-23 09:30:55 +02:00
|
|
|
{
|
|
|
|
mbedtls_ssl_session session;
|
|
|
|
unsigned char *buf1 = NULL, *buf2 = NULL;
|
|
|
|
size_t len0, len1, len2;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Test that a load-save pair is the identity
|
|
|
|
*/
|
|
|
|
|
|
|
|
mbedtls_ssl_session_init( &session );
|
|
|
|
|
2019-05-23 10:06:14 +02:00
|
|
|
/* Prepare a dummy session to work on */
|
2019-05-24 09:40:17 +02:00
|
|
|
TEST_ASSERT( ssl_populate_session( &session, ticket_len, crt_file ) == 0 );
|
2019-05-23 10:06:14 +02:00
|
|
|
|
2019-06-03 09:55:16 +02:00
|
|
|
/* Get desired buffer size for serializing */
|
2019-05-23 09:30:55 +02:00
|
|
|
TEST_ASSERT( mbedtls_ssl_session_save( &session, NULL, 0, &len0 )
|
|
|
|
== MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
|
|
|
|
|
|
|
|
/* Allocate first buffer */
|
|
|
|
buf1 = mbedtls_calloc( 1, len0 );
|
|
|
|
TEST_ASSERT( buf1 != NULL );
|
|
|
|
|
2019-06-03 09:55:16 +02:00
|
|
|
/* Serialize to buffer and free live session */
|
2019-05-23 09:30:55 +02:00
|
|
|
TEST_ASSERT( mbedtls_ssl_session_save( &session, buf1, len0, &len1 )
|
|
|
|
== 0 );
|
|
|
|
TEST_ASSERT( len0 == len1 );
|
|
|
|
mbedtls_ssl_session_free( &session );
|
|
|
|
|
2019-06-03 09:55:16 +02:00
|
|
|
/* Restore session from serialized data */
|
2019-05-24 09:54:21 +02:00
|
|
|
TEST_ASSERT( mbedtls_ssl_session_load( &session, buf1, len1 ) == 0 );
|
2019-05-23 09:30:55 +02:00
|
|
|
|
2019-06-03 09:55:16 +02:00
|
|
|
/* Allocate second buffer and serialize to it */
|
2019-05-23 09:30:55 +02:00
|
|
|
buf2 = mbedtls_calloc( 1, len0 );
|
2019-05-24 09:52:10 +02:00
|
|
|
TEST_ASSERT( buf2 != NULL );
|
2019-05-23 09:30:55 +02:00
|
|
|
TEST_ASSERT( mbedtls_ssl_session_save( &session, buf2, len0, &len2 )
|
|
|
|
== 0 );
|
|
|
|
|
2019-06-03 09:55:16 +02:00
|
|
|
/* Make sure both serialized versions are identical */
|
2019-05-23 09:30:55 +02:00
|
|
|
TEST_ASSERT( len1 == len2 );
|
|
|
|
TEST_ASSERT( memcmp( buf1, buf2, len1 ) == 0 );
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_ssl_session_free( &session );
|
|
|
|
mbedtls_free( buf1 );
|
|
|
|
mbedtls_free( buf2 );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
2019-05-23 10:38:11 +02:00
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2019-06-03 09:55:16 +02:00
|
|
|
void ssl_serialize_session_save_buf_size( int ticket_len, char *crt_file )
|
2019-05-23 10:38:11 +02:00
|
|
|
{
|
|
|
|
mbedtls_ssl_session session;
|
|
|
|
unsigned char *buf = NULL;
|
|
|
|
size_t good_len, bad_len, test_len;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Test that session_save() fails cleanly on small buffers
|
|
|
|
*/
|
|
|
|
|
|
|
|
mbedtls_ssl_session_init( &session );
|
|
|
|
|
2019-06-03 09:55:16 +02:00
|
|
|
/* Prepare dummy session and get serialized size */
|
2019-05-24 09:40:17 +02:00
|
|
|
TEST_ASSERT( ssl_populate_session( &session, ticket_len, crt_file ) == 0 );
|
2019-05-23 10:38:11 +02:00
|
|
|
TEST_ASSERT( mbedtls_ssl_session_save( &session, NULL, 0, &good_len )
|
|
|
|
== MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
|
|
|
|
|
|
|
|
/* Try all possible bad lengths */
|
|
|
|
for( bad_len = 1; bad_len < good_len; bad_len++ )
|
|
|
|
{
|
|
|
|
/* Allocate exact size so that asan/valgrind can detect any overwrite */
|
|
|
|
mbedtls_free( buf );
|
|
|
|
TEST_ASSERT( ( buf = mbedtls_calloc( 1, bad_len ) ) != NULL );
|
|
|
|
TEST_ASSERT( mbedtls_ssl_session_save( &session, buf, bad_len,
|
|
|
|
&test_len )
|
|
|
|
== MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
|
|
|
|
TEST_ASSERT( test_len == good_len );
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_ssl_session_free( &session );
|
|
|
|
mbedtls_free( buf );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
2019-05-23 12:28:45 +02:00
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2019-06-03 09:55:16 +02:00
|
|
|
void ssl_serialize_session_load_buf_size( int ticket_len, char *crt_file )
|
2019-05-23 12:28:45 +02:00
|
|
|
{
|
|
|
|
mbedtls_ssl_session session;
|
|
|
|
unsigned char *good_buf = NULL, *bad_buf = NULL;
|
|
|
|
size_t good_len, bad_len;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Test that session_load() fails cleanly on small buffers
|
|
|
|
*/
|
|
|
|
|
|
|
|
mbedtls_ssl_session_init( &session );
|
|
|
|
|
2019-06-03 09:55:16 +02:00
|
|
|
/* Prepare serialized session data */
|
2019-05-24 09:40:17 +02:00
|
|
|
TEST_ASSERT( ssl_populate_session( &session, ticket_len, crt_file ) == 0 );
|
2019-05-23 12:28:45 +02:00
|
|
|
TEST_ASSERT( mbedtls_ssl_session_save( &session, NULL, 0, &good_len )
|
|
|
|
== MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
|
|
|
|
TEST_ASSERT( ( good_buf = mbedtls_calloc( 1, good_len ) ) != NULL );
|
|
|
|
TEST_ASSERT( mbedtls_ssl_session_save( &session, good_buf, good_len,
|
|
|
|
&good_len ) == 0 );
|
|
|
|
mbedtls_ssl_session_free( &session );
|
|
|
|
|
|
|
|
/* Try all possible bad lengths */
|
|
|
|
for( bad_len = 0; bad_len < good_len; bad_len++ )
|
|
|
|
{
|
|
|
|
/* Allocate exact size so that asan/valgrind can detect any overread */
|
|
|
|
mbedtls_free( bad_buf );
|
|
|
|
bad_buf = mbedtls_calloc( 1, bad_len ? bad_len : 1 );
|
|
|
|
TEST_ASSERT( bad_buf != NULL );
|
|
|
|
memcpy( bad_buf, good_buf, bad_len );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_ssl_session_load( &session, bad_buf, bad_len )
|
|
|
|
== MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_ssl_session_free( &session );
|
|
|
|
mbedtls_free( good_buf );
|
|
|
|
mbedtls_free( bad_buf );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
2019-05-21 17:39:30 +02:00
|
|
|
|
2019-05-29 13:44:28 +02:00
|
|
|
/* BEGIN_CASE */
|
|
|
|
void ssl_session_serialize_version_check( int corrupt_major,
|
2019-05-21 17:39:30 +02:00
|
|
|
int corrupt_minor,
|
|
|
|
int corrupt_patch,
|
|
|
|
int corrupt_config )
|
|
|
|
{
|
2019-05-29 13:44:28 +02:00
|
|
|
unsigned char serialized_session[ 2048 ];
|
|
|
|
size_t serialized_session_len;
|
2019-05-29 13:45:21 +02:00
|
|
|
unsigned cur_byte;
|
2019-05-21 17:39:30 +02:00
|
|
|
mbedtls_ssl_session session;
|
2019-05-29 13:45:21 +02:00
|
|
|
uint8_t should_corrupt_byte[] = { corrupt_major == 1,
|
|
|
|
corrupt_minor == 1,
|
|
|
|
corrupt_patch == 1,
|
|
|
|
corrupt_config == 1,
|
|
|
|
corrupt_config == 1 };
|
|
|
|
|
2019-05-21 17:39:30 +02:00
|
|
|
mbedtls_ssl_session_init( &session );
|
|
|
|
|
2019-05-29 13:45:21 +02:00
|
|
|
/* Infer length of serialized session. */
|
2019-05-21 17:39:30 +02:00
|
|
|
TEST_ASSERT( mbedtls_ssl_session_save( &session,
|
2019-05-29 13:44:28 +02:00
|
|
|
serialized_session,
|
|
|
|
sizeof( serialized_session ),
|
|
|
|
&serialized_session_len ) == 0 );
|
2019-05-21 17:39:30 +02:00
|
|
|
|
2019-05-29 13:45:21 +02:00
|
|
|
mbedtls_ssl_session_free( &session );
|
2019-05-21 17:39:30 +02:00
|
|
|
|
2019-05-29 13:45:21 +02:00
|
|
|
/* Without any modification, we should be able to successfully
|
2019-05-29 13:44:28 +02:00
|
|
|
* de-serialize the session - double-check that. */
|
2019-05-21 17:39:30 +02:00
|
|
|
TEST_ASSERT( mbedtls_ssl_session_load( &session,
|
2019-05-29 13:44:28 +02:00
|
|
|
serialized_session,
|
|
|
|
serialized_session_len ) == 0 );
|
2019-05-21 17:39:30 +02:00
|
|
|
mbedtls_ssl_session_free( &session );
|
|
|
|
|
2019-05-29 13:45:21 +02:00
|
|
|
/* Go through the bytes in the serialized session header and
|
|
|
|
* corrupt them bit-by-bit. */
|
|
|
|
for( cur_byte = 0; cur_byte < sizeof( should_corrupt_byte ); cur_byte++ )
|
|
|
|
{
|
|
|
|
int cur_bit;
|
|
|
|
unsigned char * const byte = &serialized_session[ cur_byte ];
|
2019-05-21 17:39:30 +02:00
|
|
|
|
2019-05-29 13:45:21 +02:00
|
|
|
if( should_corrupt_byte[ cur_byte ] == 0 )
|
|
|
|
continue;
|
2019-05-21 17:39:30 +02:00
|
|
|
|
2019-05-29 13:45:21 +02:00
|
|
|
for( cur_bit = 0; cur_bit < CHAR_BIT; cur_bit++ )
|
|
|
|
{
|
|
|
|
unsigned char const corrupted_bit = 0x1u << cur_bit;
|
|
|
|
/* Modify a single bit in the serialized session. */
|
|
|
|
*byte ^= corrupted_bit;
|
|
|
|
|
|
|
|
/* Attempt to deserialize */
|
|
|
|
TEST_ASSERT( mbedtls_ssl_session_load( &session,
|
|
|
|
serialized_session,
|
|
|
|
serialized_session_len ) ==
|
2019-06-03 13:58:39 +02:00
|
|
|
MBEDTLS_ERR_SSL_VERSION_MISMATCH );
|
2019-05-29 13:45:21 +02:00
|
|
|
|
|
|
|
/* Undo the change */
|
|
|
|
*byte ^= corrupted_bit;
|
|
|
|
}
|
2019-05-21 17:39:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
/* END_CASE */
|