blob: a0e72dd387ee73c852fa92953fc4209031baaefb [file] [log] [blame]
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02001/* BEGIN_HEADER */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002#include <mbedtls/ssl.h>
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +02003#include <mbedtls/ssl_internal.h>
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01004#include <mbedtls/ctr_drbg.h>
5#include <mbedtls/entropy.h>
6#include <mbedtls/certs.h>
Andrzej Kurek941962e2020-02-07 09:20:32 -05007#include <mbedtls/timing.h>
Piotr Nowickibde7ee82020-02-21 10:59:50 +01008#include <mbedtls/debug.h>
9
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +020010#include <ssl_invasive.h>
11
Piotr Nowickibde7ee82020-02-21 10:59:50 +010012typedef struct log_pattern
13{
14 const char *pattern;
15 size_t counter;
16} log_pattern;
17
Piotr Nowicki438bf3b2020-03-10 12:59:10 +010018/*
19 * This function can be passed to mbedtls to receive output logs from it. In
Piotr Nowickibde7ee82020-02-21 10:59:50 +010020 * this case, it will count the instances of a log_pattern in the received
21 * logged messages.
22 */
23void log_analyzer( void *ctx, int level,
24 const char *file, int line,
25 const char *str )
26{
27 log_pattern *p = (log_pattern *) ctx;
28
29 (void) level;
30 (void) line;
31 (void) file;
32
33 if( NULL != p &&
34 NULL != p->pattern &&
35 NULL != strstr( str, p->pattern ) )
36 {
37 p->counter++;
38 }
39}
Janos Follath6264e662019-11-26 11:11:15 +000040
Paul Elliottc8570442020-04-15 17:00:50 +010041/* Invalid minor version used when not specifying a min/max version or expecting a test to fail */
42#define TEST_SSL_MINOR_VERSION_NONE -1
43
Andrzej Kurek8a6ff152020-02-26 09:10:14 -050044typedef struct handshake_test_options
45{
46 const char *cipher;
Paul Elliottc8570442020-04-15 17:00:50 +010047 int client_min_version;
48 int client_max_version;
49 int server_min_version;
50 int server_max_version;
51 int expected_negotiated_version;
Andrzej Kurek8a6ff152020-02-26 09:10:14 -050052 int pk_alg;
53 data_t *psk_str;
54 int dtls;
Piotr Nowickibde7ee82020-02-21 10:59:50 +010055 int srv_auth_mode;
Andrzej Kurek8a6ff152020-02-26 09:10:14 -050056 int serialize;
57 int mfl;
58 int cli_msg_len;
59 int srv_msg_len;
60 int expected_cli_fragments;
61 int expected_srv_fragments;
62 int renegotiate;
63 int legacy_renegotiation;
Piotr Nowickibde7ee82020-02-21 10:59:50 +010064 void *srv_log_obj;
65 void *cli_log_obj;
66 void (*srv_log_fun)(void *, int, const char *, int, const char *);
67 void (*cli_log_fun)(void *, int, const char *, int, const char *);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -050068 int resize_buffers;
Andrzej Kurek8a6ff152020-02-26 09:10:14 -050069} handshake_test_options;
70
71void init_handshake_options( handshake_test_options *opts )
72{
73 opts->cipher = "";
Paul Elliottc8570442020-04-15 17:00:50 +010074 opts->client_min_version = TEST_SSL_MINOR_VERSION_NONE;
75 opts->client_max_version = TEST_SSL_MINOR_VERSION_NONE;
76 opts->server_min_version = TEST_SSL_MINOR_VERSION_NONE;
77 opts->server_max_version = TEST_SSL_MINOR_VERSION_NONE;
78 opts->expected_negotiated_version = MBEDTLS_SSL_MINOR_VERSION_3;
Andrzej Kurek8a6ff152020-02-26 09:10:14 -050079 opts->pk_alg = MBEDTLS_PK_RSA;
80 opts->psk_str = NULL;
81 opts->dtls = 0;
Piotr Nowickibde7ee82020-02-21 10:59:50 +010082 opts->srv_auth_mode = MBEDTLS_SSL_VERIFY_NONE;
Andrzej Kurek8a6ff152020-02-26 09:10:14 -050083 opts->serialize = 0;
84 opts->mfl = MBEDTLS_SSL_MAX_FRAG_LEN_NONE;
85 opts->cli_msg_len = 100;
86 opts->srv_msg_len = 100;
87 opts->expected_cli_fragments = 1;
88 opts->expected_srv_fragments = 1;
89 opts->renegotiate = 0;
90 opts->legacy_renegotiation = MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION;
Piotr Nowickibde7ee82020-02-21 10:59:50 +010091 opts->srv_log_obj = NULL;
92 opts->srv_log_obj = NULL;
93 opts->srv_log_fun = NULL;
94 opts->cli_log_fun = NULL;
Andrzej Kurek0afa2a12020-03-03 10:39:58 -050095 opts->resize_buffers = 1;
Andrzej Kurek8a6ff152020-02-26 09:10:14 -050096}
Janos Follath6264e662019-11-26 11:11:15 +000097/*
98 * Buffer structure for custom I/O callbacks.
99 */
100
101typedef struct mbedtls_test_buffer
102{
103 size_t start;
104 size_t content_length;
105 size_t capacity;
106 unsigned char *buffer;
107} mbedtls_test_buffer;
108
109/*
110 * Initialises \p buf. After calling this function it is safe to call
111 * `mbedtls_test_buffer_free()` on \p buf.
112 */
113void mbedtls_test_buffer_init( mbedtls_test_buffer *buf )
114{
115 memset( buf, 0, sizeof( *buf ) );
116}
117
118/*
119 * Sets up \p buf. After calling this function it is safe to call
120 * `mbedtls_test_buffer_put()` and `mbedtls_test_buffer_get()` on \p buf.
121 */
122int mbedtls_test_buffer_setup( mbedtls_test_buffer *buf, size_t capacity )
123{
124 buf->buffer = (unsigned char*) mbedtls_calloc( capacity,
125 sizeof(unsigned char) );
126 if( NULL == buf->buffer )
127 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
128 buf->capacity = capacity;
129
130 return 0;
131}
132
133void mbedtls_test_buffer_free( mbedtls_test_buffer *buf )
134{
135 if( buf->buffer != NULL )
136 mbedtls_free( buf->buffer );
137
138 memset( buf, 0, sizeof( *buf ) );
139}
140
141/*
142 * Puts \p input_len bytes from the \p input buffer into the ring buffer \p buf.
143 *
144 * \p buf must have been initialized and set up by calling
145 * `mbedtls_test_buffer_init()` and `mbedtls_test_buffer_setup()`.
146 *
147 * \retval \p input_len, if the data fits.
148 * \retval 0 <= value < \p input_len, if the data does not fit.
149 * \retval -1, if \p buf is NULL, it hasn't been set up or \p input_len is not
150 * zero and \p input is NULL.
151 */
152int mbedtls_test_buffer_put( mbedtls_test_buffer *buf,
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100153 const unsigned char *input, size_t input_len )
Janos Follath6264e662019-11-26 11:11:15 +0000154{
155 size_t overflow = 0;
156
157 if( ( buf == NULL ) || ( buf->buffer == NULL ) )
158 return -1;
159
160 /* Reduce input_len to a number that fits in the buffer. */
161 if ( ( buf->content_length + input_len ) > buf->capacity )
162 {
163 input_len = buf->capacity - buf->content_length;
164 }
165
166 if( input == NULL )
167 {
168 return ( input_len == 0 ) ? 0 : -1;
169 }
170
Piotr Nowickifb437d72020-01-13 16:59:12 +0100171 /* Check if the buffer has not come full circle and free space is not in
172 * the middle */
173 if( buf->start + buf->content_length < buf->capacity )
Janos Follath6264e662019-11-26 11:11:15 +0000174 {
Piotr Nowickifb437d72020-01-13 16:59:12 +0100175
176 /* Calculate the number of bytes that need to be placed at lower memory
177 * address */
178 if( buf->start + buf->content_length + input_len
179 > buf->capacity )
180 {
181 overflow = ( buf->start + buf->content_length + input_len )
182 % buf->capacity;
183 }
184
185 memcpy( buf->buffer + buf->start + buf->content_length, input,
186 input_len - overflow );
187 memcpy( buf->buffer, input + input_len - overflow, overflow );
188
189 }
190 else
191 {
192 /* The buffer has come full circle and free space is in the middle */
193 memcpy( buf->buffer + buf->start + buf->content_length - buf->capacity,
194 input, input_len );
Janos Follath6264e662019-11-26 11:11:15 +0000195 }
196
Janos Follath6264e662019-11-26 11:11:15 +0000197 buf->content_length += input_len;
Janos Follath6264e662019-11-26 11:11:15 +0000198 return input_len;
199}
200
201/*
Andrzej Kurekf7774142020-01-22 06:34:59 -0500202 * Gets \p output_len bytes from the ring buffer \p buf into the
203 * \p output buffer. The output buffer can be NULL, in this case a part of the
204 * ring buffer will be dropped, if the requested length is available.
Janos Follath6264e662019-11-26 11:11:15 +0000205 *
206 * \p buf must have been initialized and set up by calling
207 * `mbedtls_test_buffer_init()` and `mbedtls_test_buffer_setup()`.
208 *
209 * \retval \p output_len, if the data is available.
210 * \retval 0 <= value < \p output_len, if the data is not available.
Andrzej Kurekf7774142020-01-22 06:34:59 -0500211 * \retval -1, if \buf is NULL or it hasn't been set up.
Janos Follath6264e662019-11-26 11:11:15 +0000212 */
213int mbedtls_test_buffer_get( mbedtls_test_buffer *buf,
214 unsigned char* output, size_t output_len )
215{
216 size_t overflow = 0;
217
218 if( ( buf == NULL ) || ( buf->buffer == NULL ) )
219 return -1;
220
Andrzej Kurekf7774142020-01-22 06:34:59 -0500221 if( output == NULL && output_len == 0 )
222 return 0;
Janos Follath6264e662019-11-26 11:11:15 +0000223
224 if( buf->content_length < output_len )
225 output_len = buf->content_length;
226
227 /* Calculate the number of bytes that need to be drawn from lower memory
228 * address */
229 if( buf->start + output_len > buf->capacity )
230 {
231 overflow = ( buf->start + output_len ) % buf->capacity;
232 }
233
Andrzej Kurekf7774142020-01-22 06:34:59 -0500234 if( output != NULL )
235 {
236 memcpy( output, buf->buffer + buf->start, output_len - overflow );
237 memcpy( output + output_len - overflow, buf->buffer, overflow );
238 }
239
Janos Follath6264e662019-11-26 11:11:15 +0000240 buf->content_length -= output_len;
241 buf->start = ( buf->start + output_len ) % buf->capacity;
242
243 return output_len;
244}
245
Hanno Beckera18d1322018-01-03 14:27:32 +0000246/*
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500247 * Errors used in the message transport mock tests
248 */
249 #define MBEDTLS_TEST_ERROR_ARG_NULL -11
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500250 #define MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED -44
251
252/*
253 * Context for a message metadata queue (fifo) that is on top of the ring buffer.
254 */
255typedef struct mbedtls_test_message_queue
256{
257 size_t *messages;
258 int pos;
259 int num;
260 int capacity;
261} mbedtls_test_message_queue;
262
263/*
264 * Setup and free functions for the message metadata queue.
265 *
266 * \p capacity describes the number of message metadata chunks that can be held
267 * within the queue.
268 *
269 * \retval 0, if a metadata queue of a given length can be allocated.
270 * \retval MBEDTLS_ERR_SSL_ALLOC_FAILED, if allocation failed.
271 */
272int mbedtls_test_message_queue_setup( mbedtls_test_message_queue *queue,
273 size_t capacity )
274{
275 queue->messages = (size_t*) mbedtls_calloc( capacity, sizeof(size_t) );
276 if( NULL == queue->messages )
277 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
278
279 queue->capacity = capacity;
280 queue->pos = 0;
281 queue->num = 0;
282
283 return 0;
284}
285
286void mbedtls_test_message_queue_free( mbedtls_test_message_queue *queue )
287{
288 if( queue == NULL )
289 return;
290
291 if( queue->messages != NULL )
292 mbedtls_free( queue->messages );
293
294 memset( queue, 0, sizeof( *queue ) );
295}
296
297/*
298 * Push message length information onto the message metadata queue.
299 * This will become the last element to leave it (fifo).
300 *
301 * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null.
Andrzej Kurekf46b9122020-02-07 08:19:00 -0500302 * \retval MBEDTLS_ERR_SSL_WANT_WRITE, if the queue is full.
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500303 * \retval \p len, if the push was successful.
304 */
305int mbedtls_test_message_queue_push_info( mbedtls_test_message_queue *queue,
306 size_t len )
307{
308 int place;
309 if( queue == NULL )
310 return MBEDTLS_TEST_ERROR_ARG_NULL;
311
312 if( queue->num >= queue->capacity )
Andrzej Kurekf46b9122020-02-07 08:19:00 -0500313 return MBEDTLS_ERR_SSL_WANT_WRITE;
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500314
315 place = ( queue->pos + queue->num ) % queue->capacity;
316 queue->messages[place] = len;
317 queue->num++;
318 return len;
319}
320
321/*
322 * Pop information about the next message length from the queue. This will be
323 * the oldest inserted message length(fifo). \p msg_len can be null, in which
324 * case the data will be popped from the queue but not copied anywhere.
325 *
326 * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null.
Andrzej Kurekf46b9122020-02-07 08:19:00 -0500327 * \retval MBEDTLS_ERR_SSL_WANT_READ, if the queue is empty.
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500328 * \retval message length, if the pop was successful, up to the given
329 \p buf_len.
330 */
331int mbedtls_test_message_queue_pop_info( mbedtls_test_message_queue *queue,
332 size_t buf_len )
333{
334 size_t message_length;
335 if( queue == NULL )
336 return MBEDTLS_TEST_ERROR_ARG_NULL;
337 if( queue->num == 0 )
Andrzej Kurekf46b9122020-02-07 08:19:00 -0500338 return MBEDTLS_ERR_SSL_WANT_READ;
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500339
340 message_length = queue->messages[queue->pos];
341 queue->messages[queue->pos] = 0;
342 queue->num--;
343 queue->pos++;
344 queue->pos %= queue->capacity;
345 if( queue->pos < 0 )
346 queue->pos += queue->capacity;
347
348 return ( message_length > buf_len ) ? buf_len : message_length;
349}
350
351/*
352 * Take a peek on the info about the next message length from the queue.
353 * This will be the oldest inserted message length(fifo).
354 *
355 * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null.
Andrzej Kurekf46b9122020-02-07 08:19:00 -0500356 * \retval MBEDTLS_ERR_SSL_WANT_READ, if the queue is empty.
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500357 * \retval 0, if the peek was successful.
358 * \retval MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED, if the given buffer length is
359 * too small to fit the message. In this case the \p msg_len will be
360 * set to the full message length so that the
361 * caller knows what portion of the message can be dropped.
362 */
363int mbedtls_test_message_queue_peek_info( mbedtls_test_message_queue *queue,
364 size_t buf_len, size_t* msg_len )
365{
366 if( queue == NULL || msg_len == NULL )
367 return MBEDTLS_TEST_ERROR_ARG_NULL;
368 if( queue->num == 0 )
Andrzej Kurekf46b9122020-02-07 08:19:00 -0500369 return MBEDTLS_ERR_SSL_WANT_READ;
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500370
371 *msg_len = queue->messages[queue->pos];
372 return ( *msg_len > buf_len ) ? MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED : 0;
373}
374/*
Janos Follath031827f2019-11-27 11:12:14 +0000375 * Context for the I/O callbacks simulating network connection.
376 */
377
378#define MBEDTLS_MOCK_SOCKET_CONNECTED 1
379
380typedef struct mbedtls_mock_socket
381{
382 int status;
383 mbedtls_test_buffer *input;
384 mbedtls_test_buffer *output;
385 struct mbedtls_mock_socket *peer;
386} mbedtls_mock_socket;
387
388/*
389 * Setup and teardown functions for mock sockets.
390 */
391void mbedtls_mock_socket_init( mbedtls_mock_socket *socket )
392{
393 memset( socket, 0, sizeof( *socket ) );
394}
395
396/*
397 * Closes the socket \p socket.
398 *
399 * \p socket must have been previously initialized by calling
400 * mbedtls_mock_socket_init().
401 *
402 * This function frees all allocated resources and both sockets are aware of the
403 * new connection state.
404 *
405 * That is, this function does not simulate half-open TCP connections and the
406 * phenomenon that when closing a UDP connection the peer is not aware of the
407 * connection having been closed.
408 */
409void mbedtls_mock_socket_close( mbedtls_mock_socket* socket )
410{
411 if( socket == NULL )
412 return;
413
414 if( socket->input != NULL )
415 {
416 mbedtls_test_buffer_free( socket->input );
417 mbedtls_free( socket->input );
418 }
419
420 if( socket->output != NULL )
421 {
422 mbedtls_test_buffer_free( socket->output );
423 mbedtls_free( socket->output );
424 }
425
426 if( socket->peer != NULL )
427 memset( socket->peer, 0, sizeof( *socket->peer ) );
428
429 memset( socket, 0, sizeof( *socket ) );
430}
431
432/*
433 * Establishes a connection between \p peer1 and \p peer2.
434 *
435 * \p peer1 and \p peer2 must have been previously initialized by calling
436 * mbedtls_mock_socket_init().
437 *
438 * The capacites of the internal buffers are set to \p bufsize. Setting this to
439 * the correct value allows for simulation of MTU, sanity testing the mock
440 * implementation and mocking TCP connections with lower memory cost.
441 */
442int mbedtls_mock_socket_connect( mbedtls_mock_socket* peer1,
443 mbedtls_mock_socket* peer2,
444 size_t bufsize )
445{
446 int ret = -1;
447
Piotr Nowickid796e192020-01-28 12:09:47 +0100448 peer1->output =
Janos Follath031827f2019-11-27 11:12:14 +0000449 (mbedtls_test_buffer*) mbedtls_calloc( 1, sizeof(mbedtls_test_buffer) );
450 if( peer1->output == NULL )
451 {
452 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
453 goto exit;
454 }
455 mbedtls_test_buffer_init( peer1->output );
456 if( 0 != ( ret = mbedtls_test_buffer_setup( peer1->output, bufsize ) ) )
457 {
458 goto exit;
459 }
460
Piotr Nowickid796e192020-01-28 12:09:47 +0100461 peer2->output =
462 (mbedtls_test_buffer*) mbedtls_calloc( 1, sizeof(mbedtls_test_buffer) );
463 if( peer2->output == NULL )
464 {
465 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
466 goto exit;
467 }
468 mbedtls_test_buffer_init( peer2->output );
469 if( 0 != ( ret = mbedtls_test_buffer_setup( peer2->output, bufsize ) ) )
470 {
471 goto exit;
472 }
473
Janos Follath031827f2019-11-27 11:12:14 +0000474 peer1->peer = peer2;
475 peer2->peer = peer1;
Piotr Nowickid796e192020-01-28 12:09:47 +0100476 peer1->input = peer2->output;
477 peer2->input = peer1->output;
Janos Follath031827f2019-11-27 11:12:14 +0000478
479 peer1->status = peer2->status = MBEDTLS_MOCK_SOCKET_CONNECTED;
480 ret = 0;
481
482exit:
483
484 if( ret != 0 )
485 {
486 mbedtls_mock_socket_close( peer1 );
487 mbedtls_mock_socket_close( peer2 );
488 }
489
490 return ret;
491}
492
493/*
494 * Callbacks for simulating blocking I/O over connection-oriented transport.
495 */
496
497int mbedtls_mock_tcp_send_b( void *ctx, const unsigned char *buf, size_t len )
498{
499 mbedtls_mock_socket *socket = (mbedtls_mock_socket*) ctx;
500
501 if( socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED )
502 return -1;
503
504 return mbedtls_test_buffer_put( socket->output, buf, len );
505}
506
507int mbedtls_mock_tcp_recv_b( void *ctx, unsigned char *buf, size_t len )
508{
509 mbedtls_mock_socket *socket = (mbedtls_mock_socket*) ctx;
510
511 if( socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED )
512 return -1;
513
514 return mbedtls_test_buffer_get( socket->input, buf, len );
515}
516
517/*
Janos Follath3766ba52019-11-27 13:31:42 +0000518 * Callbacks for simulating non-blocking I/O over connection-oriented transport.
519 */
520
521int mbedtls_mock_tcp_send_nb( void *ctx, const unsigned char *buf, size_t len )
522{
523 mbedtls_mock_socket *socket = (mbedtls_mock_socket*) ctx;
524
525 if( socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED )
526 return -1;
527
Piotr Nowicki890b5ca2020-01-15 16:19:07 +0100528 if( socket->output->capacity == socket->output->content_length )
Janos Follath3766ba52019-11-27 13:31:42 +0000529 {
Janos Follath3766ba52019-11-27 13:31:42 +0000530 return MBEDTLS_ERR_SSL_WANT_WRITE;
531 }
532
Janos Follath3766ba52019-11-27 13:31:42 +0000533 return mbedtls_test_buffer_put( socket->output, buf, len );
534}
535
536int mbedtls_mock_tcp_recv_nb( void *ctx, unsigned char *buf, size_t len )
537{
538 mbedtls_mock_socket *socket = (mbedtls_mock_socket*) ctx;
539
540 if( socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED )
541 return -1;
542
Andrzej Kurekf40daa32020-02-04 09:00:01 -0500543 if( socket->input->content_length == 0 )
Janos Follath3766ba52019-11-27 13:31:42 +0000544 {
Janos Follath3766ba52019-11-27 13:31:42 +0000545 return MBEDTLS_ERR_SSL_WANT_READ;
546 }
547
Janos Follath3766ba52019-11-27 13:31:42 +0000548 return mbedtls_test_buffer_get( socket->input, buf, len );
549}
550
Andrzej Kurekbc483de2020-01-22 03:40:00 -0500551/* Errors used in the message socket mocks */
552
553#define MBEDTLS_TEST_ERROR_CONTEXT_ERROR -55
554#define MBEDTLS_TEST_ERROR_SEND_FAILED -66
555#define MBEDTLS_TEST_ERROR_RECV_FAILED -77
556
557/*
558 * Structure used as an addon, or a wrapper, around the mocked sockets.
559 * Contains an input queue, to which the other socket pushes metadata,
560 * and an output queue, to which this one pushes metadata. This context is
561 * considered as an owner of the input queue only, which is initialized and
562 * freed in the respective setup and free calls.
563 */
564typedef struct mbedtls_test_message_socket_context
565{
566 mbedtls_test_message_queue* queue_input;
567 mbedtls_test_message_queue* queue_output;
568 mbedtls_mock_socket* socket;
569} mbedtls_test_message_socket_context;
570
Andrzej Kurek45916ba2020-03-05 14:46:22 -0500571void mbedtls_message_socket_init( mbedtls_test_message_socket_context *ctx )
572{
573 ctx->queue_input = NULL;
574 ctx->queue_output = NULL;
575 ctx->socket = NULL;
576}
577
Andrzej Kurekbc483de2020-01-22 03:40:00 -0500578/*
579 * Setup a given mesasge socket context including initialization of
580 * input/output queues to a chosen capacity of messages. Also set the
581 * corresponding mock socket.
582 *
583 * \retval 0, if everything succeeds.
584 * \retval MBEDTLS_ERR_SSL_ALLOC_FAILED, if allocation of a message
585 * queue failed.
586 */
587int mbedtls_message_socket_setup( mbedtls_test_message_queue* queue_input,
588 mbedtls_test_message_queue* queue_output,
589 size_t queue_capacity,
590 mbedtls_mock_socket* socket,
591 mbedtls_test_message_socket_context* ctx )
592{
593 int ret = mbedtls_test_message_queue_setup( queue_input, queue_capacity );
594 if( ret != 0 )
595 return ret;
596 ctx->queue_input = queue_input;
597 ctx->queue_output = queue_output;
598 ctx->socket = socket;
599 mbedtls_mock_socket_init( socket );
600
601 return 0;
602}
603
604/*
605 * Close a given message socket context, along with the socket itself. Free the
606 * memory allocated by the input queue.
607 */
608void mbedtls_message_socket_close( mbedtls_test_message_socket_context* ctx )
609{
610 if( ctx == NULL )
611 return;
612
613 mbedtls_test_message_queue_free( ctx->queue_input );
614 mbedtls_mock_socket_close( ctx->socket );
615 memset( ctx, 0, sizeof( *ctx ) );
616}
617
618/*
619 * Send one message through a given message socket context.
620 *
621 * \retval \p len, if everything succeeds.
622 * \retval MBEDTLS_TEST_ERROR_CONTEXT_ERROR, if any of the needed context
623 * elements or the context itself is null.
624 * \retval MBEDTLS_TEST_ERROR_SEND_FAILED if mbedtls_mock_tcp_send_b failed.
Andrzej Kurekf46b9122020-02-07 08:19:00 -0500625 * \retval MBEDTLS_ERR_SSL_WANT_WRITE, if the output queue is full.
Andrzej Kurekbc483de2020-01-22 03:40:00 -0500626 *
627 * This function will also return any error from
628 * mbedtls_test_message_queue_push_info.
629 */
630int mbedtls_mock_tcp_send_msg( void *ctx, const unsigned char *buf, size_t len )
631{
632 mbedtls_test_message_queue* queue;
633 mbedtls_mock_socket* socket;
634 mbedtls_test_message_socket_context *context = (mbedtls_test_message_socket_context*) ctx;
635
636 if( context == NULL || context->socket == NULL
637 || context->queue_output == NULL )
638 {
639 return MBEDTLS_TEST_ERROR_CONTEXT_ERROR;
640 }
641
642 queue = context->queue_output;
643 socket = context->socket;
644
645 if( queue->num >= queue->capacity )
Andrzej Kurekf46b9122020-02-07 08:19:00 -0500646 return MBEDTLS_ERR_SSL_WANT_WRITE;
Andrzej Kurekbc483de2020-01-22 03:40:00 -0500647
648 if( mbedtls_mock_tcp_send_b( socket, buf, len ) != (int) len )
649 return MBEDTLS_TEST_ERROR_SEND_FAILED;
650
651 return mbedtls_test_message_queue_push_info( queue, len );
652}
653
654/*
655 * Receive one message from a given message socket context and return message
656 * length or an error.
657 *
658 * \retval message length, if everything succeeds.
659 * \retval MBEDTLS_TEST_ERROR_CONTEXT_ERROR, if any of the needed context
660 * elements or the context itself is null.
661 * \retval MBEDTLS_TEST_ERROR_RECV_FAILED if mbedtls_mock_tcp_recv_b failed.
662 *
663 * This function will also return any error other than
664 * MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED from mbedtls_test_message_queue_peek_info.
665 */
666int mbedtls_mock_tcp_recv_msg( void *ctx, unsigned char *buf, size_t buf_len )
667{
668 mbedtls_test_message_queue* queue;
669 mbedtls_mock_socket* socket;
670 mbedtls_test_message_socket_context *context = (mbedtls_test_message_socket_context*) ctx;
Gilles Peskine19e841e2020-03-09 20:43:51 +0100671 size_t drop_len = 0;
Andrzej Kurekbc483de2020-01-22 03:40:00 -0500672 size_t msg_len;
673 int ret;
674
675 if( context == NULL || context->socket == NULL
676 || context->queue_input == NULL )
677 {
678 return MBEDTLS_TEST_ERROR_CONTEXT_ERROR;
679 }
680
681 queue = context->queue_input;
682 socket = context->socket;
683
684 /* Peek first, so that in case of a socket error the data remains in
685 * the queue. */
686 ret = mbedtls_test_message_queue_peek_info( queue, buf_len, &msg_len );
687 if( ret == MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED )
688 {
689 /* Calculate how much to drop */
690 drop_len = msg_len - buf_len;
691
692 /* Set the requested message len to be buffer length */
693 msg_len = buf_len;
694 } else if( ret != 0 )
695 {
696 return ret;
697 }
698
699 if( mbedtls_mock_tcp_recv_b( socket, buf, msg_len ) != (int) msg_len )
700 return MBEDTLS_TEST_ERROR_RECV_FAILED;
701
702 if( ret == MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED )
703 {
704 /* Drop the remaining part of the message */
705 if( mbedtls_mock_tcp_recv_b( socket, NULL, drop_len ) != (int) drop_len )
706 {
707 /* Inconsistent state - part of the message was read,
708 * and a part couldn't. Not much we can do here, but it should not
709 * happen in test environment, unless forced manually. */
710 }
711 }
712 mbedtls_test_message_queue_pop_info( queue, buf_len );
713
714 return msg_len;
715}
716
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +0200717#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
718 defined(MBEDTLS_ENTROPY_C) && \
719 defined(MBEDTLS_CTR_DRBG_C)
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100720
721/*
722 * Structure with endpoint's certificates for SSL communication tests.
723 */
724typedef struct mbedtls_endpoint_certificate
725{
726 mbedtls_x509_crt ca_cert;
727 mbedtls_x509_crt cert;
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100728 mbedtls_pk_context pkey;
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100729} mbedtls_endpoint_certificate;
730
731/*
732 * Endpoint structure for SSL communication tests.
733 */
734typedef struct mbedtls_endpoint
735{
736 const char *name;
737 mbedtls_ssl_context ssl;
738 mbedtls_ssl_config conf;
739 mbedtls_ctr_drbg_context ctr_drbg;
740 mbedtls_entropy_context entropy;
741 mbedtls_mock_socket socket;
742 mbedtls_endpoint_certificate cert;
743} mbedtls_endpoint;
744
745/*
746 * Initializes \p ep_cert structure and assigns it to endpoint
747 * represented by \p ep.
748 *
749 * \retval 0 on success, otherwise error code.
750 */
Andrzej Kurekb2980742020-02-02 19:25:26 -0500751int mbedtls_endpoint_certificate_init( mbedtls_endpoint *ep, int pk_alg )
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100752{
753 int i = 0;
754 int ret = -1;
755 mbedtls_endpoint_certificate *cert;
756
757 if( ep == NULL )
758 {
759 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
760 }
761
762 cert = &( ep->cert );
763 mbedtls_x509_crt_init( &( cert->ca_cert ) );
764 mbedtls_x509_crt_init( &( cert->cert ) );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100765 mbedtls_pk_init( &( cert->pkey ) );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100766
767 /* Load the trusted CA */
768
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100769 for( i = 0; mbedtls_test_cas_der[i] != NULL; i++ )
770 {
771 ret = mbedtls_x509_crt_parse_der( &( cert->ca_cert ),
772 (const unsigned char *) mbedtls_test_cas_der[i],
773 mbedtls_test_cas_der_len[i] );
774 TEST_ASSERT( ret == 0 );
775 }
776
777 /* Load own certificate and private key */
778
779 if( ep->conf.endpoint == MBEDTLS_SSL_IS_SERVER )
780 {
Andrzej Kurekb2980742020-02-02 19:25:26 -0500781 if( pk_alg == MBEDTLS_PK_RSA )
782 {
783 ret = mbedtls_x509_crt_parse( &( cert->cert ),
784 (const unsigned char*) mbedtls_test_srv_crt_rsa_sha256_der,
785 mbedtls_test_srv_crt_rsa_sha256_der_len );
786 TEST_ASSERT( ret == 0 );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100787
Andrzej Kurekb2980742020-02-02 19:25:26 -0500788 ret = mbedtls_pk_parse_key( &( cert->pkey ),
789 (const unsigned char*) mbedtls_test_srv_key_rsa_der,
790 mbedtls_test_srv_key_rsa_der_len, NULL, 0 );
791 TEST_ASSERT( ret == 0 );
792 }
793 else
794 {
795 ret = mbedtls_x509_crt_parse( &( cert->cert ),
796 (const unsigned char*) mbedtls_test_srv_crt_ec_der,
797 mbedtls_test_srv_crt_ec_der_len );
798 TEST_ASSERT( ret == 0 );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100799
Andrzej Kurekb2980742020-02-02 19:25:26 -0500800 ret = mbedtls_pk_parse_key( &( cert->pkey ),
801 (const unsigned char*) mbedtls_test_srv_key_ec_der,
802 mbedtls_test_srv_key_ec_der_len, NULL, 0 );
803 TEST_ASSERT( ret == 0 );
804 }
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100805 }
806 else
807 {
Andrzej Kurekb2980742020-02-02 19:25:26 -0500808 if( pk_alg == MBEDTLS_PK_RSA )
809 {
810 ret = mbedtls_x509_crt_parse( &( cert->cert ),
811 (const unsigned char *) mbedtls_test_cli_crt_rsa_der,
812 mbedtls_test_cli_crt_rsa_der_len );
813 TEST_ASSERT( ret == 0 );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100814
Andrzej Kurekb2980742020-02-02 19:25:26 -0500815 ret = mbedtls_pk_parse_key( &( cert->pkey ),
816 (const unsigned char *) mbedtls_test_cli_key_rsa_der,
817 mbedtls_test_cli_key_rsa_der_len, NULL, 0 );
818 TEST_ASSERT( ret == 0 );
819 }
820 else
821 {
822 ret = mbedtls_x509_crt_parse( &( cert->cert ),
823 (const unsigned char *) mbedtls_test_cli_crt_ec_der,
824 mbedtls_test_cli_crt_ec_len );
825 TEST_ASSERT( ret == 0 );
826
827 ret = mbedtls_pk_parse_key( &( cert->pkey ),
828 (const unsigned char *) mbedtls_test_cli_key_ec_der,
829 mbedtls_test_cli_key_ec_der_len, NULL, 0 );
830 TEST_ASSERT( ret == 0 );
831 }
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100832 }
833
834 mbedtls_ssl_conf_ca_chain( &( ep->conf ), &( cert->ca_cert ), NULL );
835
Andrzej Kurekb2980742020-02-02 19:25:26 -0500836 ret = mbedtls_ssl_conf_own_cert( &( ep->conf ), &( cert->cert ),
837 &( cert->pkey ) );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100838 TEST_ASSERT( ret == 0 );
839
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100840exit:
841 if( ret != 0 )
842 {
843 mbedtls_x509_crt_free( &( cert->ca_cert ) );
844 mbedtls_x509_crt_free( &( cert->cert ) );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100845 mbedtls_pk_free( &( cert->pkey ) );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100846 }
847
848 return ret;
849}
850
851/*
852 * Initializes \p ep structure. It is important to call `mbedtls_endpoint_free()`
853 * after calling this function even if it fails.
854 *
855 * \p endpoint_type must be set as MBEDTLS_SSL_IS_SERVER or
856 * MBEDTLS_SSL_IS_CLIENT.
Andrzej Kurek15daf502020-02-12 09:17:52 -0500857 * \p pk_alg the algorithm to use, currently only MBEDTLS_PK_RSA and
858 * MBEDTLS_PK_ECDSA are supported.
859 * \p dtls_context - in case of DTLS - this is the context handling metadata.
860 * \p input_queue - used only in case of DTLS.
861 * \p output_queue - used only in case of DTLS.
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100862 *
863 * \retval 0 on success, otherwise error code.
864 */
Andrzej Kurek15daf502020-02-12 09:17:52 -0500865int mbedtls_endpoint_init( mbedtls_endpoint *ep, int endpoint_type, int pk_alg,
866 mbedtls_test_message_socket_context *dtls_context,
867 mbedtls_test_message_queue *input_queue,
868 mbedtls_test_message_queue *output_queue )
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100869{
870 int ret = -1;
871
Andrzej Kurek15daf502020-02-12 09:17:52 -0500872 if( dtls_context != NULL && ( input_queue == NULL || output_queue == NULL ) )
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100873 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Andrzej Kurek15daf502020-02-12 09:17:52 -0500874
875 if( ep == NULL )
876 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100877
878 memset( ep, 0, sizeof( *ep ) );
879
880 ep->name = ( endpoint_type == MBEDTLS_SSL_IS_SERVER ) ? "Server" : "Client";
881
882 mbedtls_ssl_init( &( ep->ssl ) );
883 mbedtls_ssl_config_init( &( ep->conf ) );
884 mbedtls_ctr_drbg_init( &( ep->ctr_drbg ) );
885 mbedtls_ssl_conf_rng( &( ep->conf ),
886 mbedtls_ctr_drbg_random,
887 &( ep->ctr_drbg ) );
888 mbedtls_entropy_init( &( ep->entropy ) );
Andrzej Kurek15daf502020-02-12 09:17:52 -0500889 if( dtls_context != NULL )
890 {
891 TEST_ASSERT( mbedtls_message_socket_setup( input_queue, output_queue,
892 100, &( ep->socket ),
893 dtls_context ) == 0 );
894 }
895 else
896 {
897 mbedtls_mock_socket_init( &( ep->socket ) );
898 }
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100899
900 ret = mbedtls_ctr_drbg_seed( &( ep->ctr_drbg ), mbedtls_entropy_func,
901 &( ep->entropy ), (const unsigned char *) ( ep->name ),
902 strlen( ep->name ) );
903 TEST_ASSERT( ret == 0 );
904
905 /* Non-blocking callbacks without timeout */
Andrzej Kurek15daf502020-02-12 09:17:52 -0500906 if( dtls_context != NULL )
907 {
908 mbedtls_ssl_set_bio( &( ep->ssl ), dtls_context,
909 mbedtls_mock_tcp_send_msg,
910 mbedtls_mock_tcp_recv_msg,
911 NULL );
912 }
913 else
914 {
915 mbedtls_ssl_set_bio( &( ep->ssl ), &( ep->socket ),
916 mbedtls_mock_tcp_send_nb,
917 mbedtls_mock_tcp_recv_nb,
918 NULL );
919 }
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100920
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100921 ret = mbedtls_ssl_config_defaults( &( ep->conf ), endpoint_type,
Andrzej Kurek15daf502020-02-12 09:17:52 -0500922 ( dtls_context != NULL ) ?
923 MBEDTLS_SSL_TRANSPORT_DATAGRAM :
924 MBEDTLS_SSL_TRANSPORT_STREAM,
925 MBEDTLS_SSL_PRESET_DEFAULT );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100926 TEST_ASSERT( ret == 0 );
927
Andrzej Kurek1a44a152020-02-07 08:21:32 -0500928 ret = mbedtls_ssl_setup( &( ep->ssl ), &( ep->conf ) );
929 TEST_ASSERT( ret == 0 );
Andrzej Kurek15daf502020-02-12 09:17:52 -0500930
931#if defined(MBEDTLS_SSL_PROTO_DTLS) && defined(MBEDTLS_SSL_SRV_C)
932 if( endpoint_type == MBEDTLS_SSL_IS_SERVER && dtls_context != NULL )
933 mbedtls_ssl_conf_dtls_cookies( &( ep->conf ), NULL, NULL, NULL );
934#endif
935
Andrzej Kurekb2980742020-02-02 19:25:26 -0500936 ret = mbedtls_endpoint_certificate_init( ep, pk_alg );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100937 TEST_ASSERT( ret == 0 );
938
939exit:
940 return ret;
941}
942
943/*
944 * Deinitializes certificates from endpoint represented by \p ep.
945 */
946void mbedtls_endpoint_certificate_free( mbedtls_endpoint *ep )
947{
948 mbedtls_endpoint_certificate *cert = &( ep->cert );
949 mbedtls_x509_crt_free( &( cert->ca_cert ) );
950 mbedtls_x509_crt_free( &( cert->cert ) );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100951 mbedtls_pk_free( &( cert->pkey ) );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100952}
953
954/*
955 * Deinitializes endpoint represented by \p ep.
956 */
Andrzej Kurek15daf502020-02-12 09:17:52 -0500957void mbedtls_endpoint_free( mbedtls_endpoint *ep,
958 mbedtls_test_message_socket_context *context )
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100959{
960 mbedtls_endpoint_certificate_free( ep );
961
962 mbedtls_ssl_free( &( ep->ssl ) );
963 mbedtls_ssl_config_free( &( ep->conf ) );
964 mbedtls_ctr_drbg_free( &( ep->ctr_drbg ) );
965 mbedtls_entropy_free( &( ep->entropy ) );
Andrzej Kurek15daf502020-02-12 09:17:52 -0500966
967 if( context != NULL )
968 {
969 mbedtls_message_socket_close( context );
970 }
971 else
972 {
973 mbedtls_mock_socket_close( &( ep->socket ) );
974 }
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100975}
976
977/*
978 * This function moves ssl handshake from \p ssl to prescribed \p state.
979 * /p second_ssl is used as second endpoint and their sockets have to be
980 * connected before calling this function.
981 *
982 * \retval 0 on success, otherwise error code.
983 */
984int mbedtls_move_handshake_to_state( mbedtls_ssl_context *ssl,
985 mbedtls_ssl_context *second_ssl,
986 int state )
987{
988 enum { BUFFSIZE = 1024 };
989 int max_steps = 1000;
990 int ret = 0;
991
992 if( ssl == NULL || second_ssl == NULL )
993 {
994 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
995 }
996
997 /* Perform communication via connected sockets */
998 while( ( ssl->state != state ) && ( --max_steps >= 0 ) )
999 {
1000 /* If /p second_ssl ends the handshake procedure before /p ssl then
1001 * there is no need to call the next step */
1002 if( second_ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
1003 {
1004 ret = mbedtls_ssl_handshake_step( second_ssl );
1005 if( ret != 0 && ret != MBEDTLS_ERR_SSL_WANT_READ &&
1006 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
1007 {
1008 return ret;
1009 }
1010 }
1011
1012 /* We only care about the \p ssl state and returns, so we call it last,
1013 * to leave the iteration as soon as the state is as expected. */
1014 ret = mbedtls_ssl_handshake_step( ssl );
1015 if( ret != 0 && ret != MBEDTLS_ERR_SSL_WANT_READ &&
1016 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
1017 {
1018 return ret;
1019 }
1020 }
1021
1022 return ( max_steps >= 0 ) ? ret : -1;
1023}
1024
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02001025#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01001026
Janos Follath3766ba52019-11-27 13:31:42 +00001027/*
Piotr Nowicki438bf3b2020-03-10 12:59:10 +01001028 * Write application data. Increase write counter if necessary.
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001029 */
1030int mbedtls_ssl_write_fragment( mbedtls_ssl_context *ssl, unsigned char *buf,
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001031 int buf_len, int *written,
Piotr Nowicki438bf3b2020-03-10 12:59:10 +01001032 const int expected_fragments )
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001033{
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001034 int ret = mbedtls_ssl_write( ssl, buf + *written, buf_len - *written );
1035 if( ret > 0 )
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001036 {
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001037 *written += ret;
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001038 }
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001039
1040 if( expected_fragments == 0 )
1041 {
1042 /* Used for DTLS and the message size larger than MFL. In that case
1043 * the message can not be fragmented and the library should return
1044 * MBEDTLS_ERR_SSL_BAD_INPUT_DATA error. This error must be returned
1045 * to prevent a dead loop inside mbedtls_exchange_data(). */
1046 return ret;
1047 }
1048 else if( expected_fragments == 1 )
1049 {
1050 /* Used for TLS/DTLS and the message size lower than MFL */
1051 TEST_ASSERT( ret == buf_len ||
1052 ret == MBEDTLS_ERR_SSL_WANT_READ ||
1053 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
1054 }
1055 else
1056 {
1057 /* Used for TLS and the message size larger than MFL */
1058 TEST_ASSERT( expected_fragments > 1 );
1059 TEST_ASSERT( ( ret >= 0 && ret <= buf_len ) ||
1060 ret == MBEDTLS_ERR_SSL_WANT_READ ||
1061 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
1062 }
1063
1064 return 0;
1065
1066exit:
1067 /* Some of the tests failed */
1068 return -1;
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001069}
1070
1071/*
Piotr Nowicki438bf3b2020-03-10 12:59:10 +01001072 * Read application data and increase read counter and fragments counter if necessary.
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001073 */
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001074int mbedtls_ssl_read_fragment( mbedtls_ssl_context *ssl, unsigned char *buf,
1075 int buf_len, int *read,
Piotr Nowicki438bf3b2020-03-10 12:59:10 +01001076 int *fragments, const int expected_fragments )
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001077{
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001078 int ret = mbedtls_ssl_read( ssl, buf + *read, buf_len - *read );
1079 if( ret > 0 )
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001080 {
Piotr Nowicki438bf3b2020-03-10 12:59:10 +01001081 ( *fragments )++;
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001082 *read += ret;
1083 }
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001084
1085 if( expected_fragments == 0 )
1086 {
1087 TEST_ASSERT( ret == 0 );
1088 }
1089 else if( expected_fragments == 1 )
1090 {
1091 TEST_ASSERT( ret == buf_len ||
1092 ret == MBEDTLS_ERR_SSL_WANT_READ ||
1093 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
1094 }
1095 else
1096 {
1097 TEST_ASSERT( expected_fragments > 1 );
1098 TEST_ASSERT( ( ret >= 0 && ret <= buf_len ) ||
1099 ret == MBEDTLS_ERR_SSL_WANT_READ ||
1100 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
1101 }
1102
1103 return 0;
1104
1105exit:
1106 /* Some of the tests failed */
1107 return -1;
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001108}
1109
1110/*
Hanno Beckera18d1322018-01-03 14:27:32 +00001111 * Helper function setting up inverse record transformations
1112 * using given cipher, hash, EtM mode, authentication tag length,
1113 * and version.
1114 */
1115
1116#define CHK( x ) \
1117 do \
1118 { \
1119 if( !( x ) ) \
Hanno Becker81e16a32019-03-01 11:21:44 +00001120 { \
Hanno Beckera5780f12019-04-05 09:55:37 +01001121 ret = -1; \
Hanno Becker81e16a32019-03-01 11:21:44 +00001122 goto cleanup; \
1123 } \
Hanno Beckera18d1322018-01-03 14:27:32 +00001124 } while( 0 )
1125
Andrzej Kurekf40daa32020-02-04 09:00:01 -05001126void set_ciphersuite( mbedtls_ssl_config *conf, const char *cipher,
1127 int* forced_ciphersuite )
1128{
1129 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
1130 forced_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id( cipher );
1131 forced_ciphersuite[1] = 0;
1132
1133 ciphersuite_info =
1134 mbedtls_ssl_ciphersuite_from_id( forced_ciphersuite[0] );
1135
1136 TEST_ASSERT( ciphersuite_info != NULL );
1137 TEST_ASSERT( ciphersuite_info->min_minor_ver <= conf->max_minor_ver );
1138 TEST_ASSERT( ciphersuite_info->max_minor_ver >= conf->min_minor_ver );
1139
1140 if( conf->max_minor_ver > ciphersuite_info->max_minor_ver )
1141 {
1142 conf->max_minor_ver = ciphersuite_info->max_minor_ver;
1143 }
1144 if( conf->min_minor_ver < ciphersuite_info->min_minor_ver )
1145 {
1146 conf->min_minor_ver = ciphersuite_info->min_minor_ver;
1147 }
1148
1149 mbedtls_ssl_conf_ciphersuites( conf, forced_ciphersuite );
1150
1151exit:
1152 return;
1153}
1154
Andrzej Kurekcc5169c2020-02-04 09:04:56 -05001155int psk_dummy_callback( void *p_info, mbedtls_ssl_context *ssl,
1156 const unsigned char *name, size_t name_len )
1157{
1158 (void) p_info;
1159 (void) ssl;
1160 (void) name;
1161 (void) name_len;
1162
1163 return ( 0 );
1164}
1165
Hanno Beckerd856c822019-04-29 17:30:59 +01001166#if MBEDTLS_SSL_CID_OUT_LEN_MAX > MBEDTLS_SSL_CID_IN_LEN_MAX
1167#define SSL_CID_LEN_MIN MBEDTLS_SSL_CID_IN_LEN_MAX
1168#else
1169#define SSL_CID_LEN_MIN MBEDTLS_SSL_CID_OUT_LEN_MAX
1170#endif
Hanno Beckera18d1322018-01-03 14:27:32 +00001171
1172static int build_transforms( mbedtls_ssl_transform *t_in,
1173 mbedtls_ssl_transform *t_out,
1174 int cipher_type, int hash_id,
Hanno Beckerd856c822019-04-29 17:30:59 +01001175 int etm, int tag_mode, int ver,
1176 size_t cid0_len,
1177 size_t cid1_len )
Hanno Beckera18d1322018-01-03 14:27:32 +00001178{
1179 mbedtls_cipher_info_t const *cipher_info;
Hanno Beckera5780f12019-04-05 09:55:37 +01001180 int ret = 0;
Hanno Beckera18d1322018-01-03 14:27:32 +00001181
1182 size_t keylen, maclen, ivlen;
Hanno Becker81e16a32019-03-01 11:21:44 +00001183 unsigned char *key0 = NULL, *key1 = NULL;
Paul Elliott6f1eda72020-06-11 20:22:00 +01001184 unsigned char *md0 = NULL, *md1 = NULL;
Hanno Beckera18d1322018-01-03 14:27:32 +00001185 unsigned char iv_enc[16], iv_dec[16];
1186
Hanno Beckera0e20d02019-05-15 14:03:01 +01001187#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd856c822019-04-29 17:30:59 +01001188 unsigned char cid0[ SSL_CID_LEN_MIN ];
1189 unsigned char cid1[ SSL_CID_LEN_MIN ];
1190
Ronald Cron351f0ee2020-06-10 12:12:18 +02001191 mbedtls_test_rnd_std_rand( NULL, cid0, sizeof( cid0 ) );
1192 mbedtls_test_rnd_std_rand( NULL, cid1, sizeof( cid1 ) );
Hanno Becker43c24b82019-05-01 09:45:57 +01001193#else
1194 ((void) cid0_len);
1195 ((void) cid1_len);
Hanno Beckera0e20d02019-05-15 14:03:01 +01001196#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerd856c822019-04-29 17:30:59 +01001197
Hanno Beckera18d1322018-01-03 14:27:32 +00001198 maclen = 0;
1199
1200 /* Pick cipher */
1201 cipher_info = mbedtls_cipher_info_from_type( cipher_type );
1202 CHK( cipher_info != NULL );
1203 CHK( cipher_info->iv_size <= 16 );
1204 CHK( cipher_info->key_bitlen % 8 == 0 );
1205
1206 /* Pick keys */
1207 keylen = cipher_info->key_bitlen / 8;
Hanno Becker78d1f702019-04-05 09:56:10 +01001208 /* Allocate `keylen + 1` bytes to ensure that we get
1209 * a non-NULL pointers from `mbedtls_calloc` even if
1210 * `keylen == 0` in the case of the NULL cipher. */
1211 CHK( ( key0 = mbedtls_calloc( 1, keylen + 1 ) ) != NULL );
1212 CHK( ( key1 = mbedtls_calloc( 1, keylen + 1 ) ) != NULL );
Hanno Beckera18d1322018-01-03 14:27:32 +00001213 memset( key0, 0x1, keylen );
1214 memset( key1, 0x2, keylen );
1215
1216 /* Setup cipher contexts */
1217 CHK( mbedtls_cipher_setup( &t_in->cipher_ctx_enc, cipher_info ) == 0 );
1218 CHK( mbedtls_cipher_setup( &t_in->cipher_ctx_dec, cipher_info ) == 0 );
1219 CHK( mbedtls_cipher_setup( &t_out->cipher_ctx_enc, cipher_info ) == 0 );
1220 CHK( mbedtls_cipher_setup( &t_out->cipher_ctx_dec, cipher_info ) == 0 );
1221
1222#if defined(MBEDTLS_CIPHER_MODE_CBC)
1223 if( cipher_info->mode == MBEDTLS_MODE_CBC )
1224 {
1225 CHK( mbedtls_cipher_set_padding_mode( &t_in->cipher_ctx_enc,
1226 MBEDTLS_PADDING_NONE ) == 0 );
1227 CHK( mbedtls_cipher_set_padding_mode( &t_in->cipher_ctx_dec,
1228 MBEDTLS_PADDING_NONE ) == 0 );
1229 CHK( mbedtls_cipher_set_padding_mode( &t_out->cipher_ctx_enc,
1230 MBEDTLS_PADDING_NONE ) == 0 );
1231 CHK( mbedtls_cipher_set_padding_mode( &t_out->cipher_ctx_dec,
1232 MBEDTLS_PADDING_NONE ) == 0 );
1233 }
1234#endif /* MBEDTLS_CIPHER_MODE_CBC */
1235
1236 CHK( mbedtls_cipher_setkey( &t_in->cipher_ctx_enc, key0,
1237 keylen << 3, MBEDTLS_ENCRYPT ) == 0 );
1238 CHK( mbedtls_cipher_setkey( &t_in->cipher_ctx_dec, key1,
1239 keylen << 3, MBEDTLS_DECRYPT ) == 0 );
1240 CHK( mbedtls_cipher_setkey( &t_out->cipher_ctx_enc, key1,
1241 keylen << 3, MBEDTLS_ENCRYPT ) == 0 );
1242 CHK( mbedtls_cipher_setkey( &t_out->cipher_ctx_dec, key0,
1243 keylen << 3, MBEDTLS_DECRYPT ) == 0 );
Hanno Beckera18d1322018-01-03 14:27:32 +00001244
1245 /* Setup MAC contexts */
1246#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1247 if( cipher_info->mode == MBEDTLS_MODE_CBC ||
1248 cipher_info->mode == MBEDTLS_MODE_STREAM )
1249 {
1250 mbedtls_md_info_t const *md_info;
Hanno Beckera18d1322018-01-03 14:27:32 +00001251
1252 /* Pick hash */
1253 md_info = mbedtls_md_info_from_type( hash_id );
1254 CHK( md_info != NULL );
1255
1256 /* Pick hash keys */
1257 maclen = mbedtls_md_get_size( md_info );
Hanno Becker3ee54212019-04-04 16:31:26 +01001258 CHK( ( md0 = mbedtls_calloc( 1, maclen ) ) != NULL );
1259 CHK( ( md1 = mbedtls_calloc( 1, maclen ) ) != NULL );
Hanno Beckera18d1322018-01-03 14:27:32 +00001260 memset( md0, 0x5, maclen );
1261 memset( md1, 0x6, maclen );
1262
1263 CHK( mbedtls_md_setup( &t_out->md_ctx_enc, md_info, 1 ) == 0 );
1264 CHK( mbedtls_md_setup( &t_out->md_ctx_dec, md_info, 1 ) == 0 );
1265 CHK( mbedtls_md_setup( &t_in->md_ctx_enc, md_info, 1 ) == 0 );
1266 CHK( mbedtls_md_setup( &t_in->md_ctx_dec, md_info, 1 ) == 0 );
1267
1268 if( ver > MBEDTLS_SSL_MINOR_VERSION_0 )
1269 {
1270 CHK( mbedtls_md_hmac_starts( &t_in->md_ctx_enc,
1271 md0, maclen ) == 0 );
1272 CHK( mbedtls_md_hmac_starts( &t_in->md_ctx_dec,
1273 md1, maclen ) == 0 );
1274 CHK( mbedtls_md_hmac_starts( &t_out->md_ctx_enc,
1275 md1, maclen ) == 0 );
1276 CHK( mbedtls_md_hmac_starts( &t_out->md_ctx_dec,
1277 md0, maclen ) == 0 );
1278 }
1279#if defined(MBEDTLS_SSL_PROTO_SSL3)
1280 else
1281 {
1282 memcpy( &t_in->mac_enc, md0, maclen );
1283 memcpy( &t_in->mac_dec, md1, maclen );
1284 memcpy( &t_out->mac_enc, md1, maclen );
1285 memcpy( &t_out->mac_dec, md0, maclen );
1286 }
1287#endif
Hanno Beckera18d1322018-01-03 14:27:32 +00001288 }
1289#else
1290 ((void) hash_id);
1291#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1292
1293
1294 /* Pick IV's (regardless of whether they
1295 * are being used by the transform). */
1296 ivlen = cipher_info->iv_size;
1297 memset( iv_enc, 0x3, sizeof( iv_enc ) );
1298 memset( iv_dec, 0x4, sizeof( iv_dec ) );
1299
1300 /*
1301 * Setup transforms
1302 */
1303
Jaeden Amero2de07f12019-06-05 13:32:08 +01001304#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
1305 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Hanno Beckera18d1322018-01-03 14:27:32 +00001306 t_out->encrypt_then_mac = etm;
1307 t_in->encrypt_then_mac = etm;
1308#else
1309 ((void) etm);
1310#endif
1311
1312 t_out->minor_ver = ver;
1313 t_in->minor_ver = ver;
1314 t_out->ivlen = ivlen;
1315 t_in->ivlen = ivlen;
1316
1317 switch( cipher_info->mode )
1318 {
1319 case MBEDTLS_MODE_GCM:
1320 case MBEDTLS_MODE_CCM:
Hanno Beckere6832872020-05-28 08:29:58 +01001321#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
1322 if( ver == MBEDTLS_SSL_MINOR_VERSION_4 )
1323 {
1324 t_out->fixed_ivlen = 12;
1325 t_in->fixed_ivlen = 12;
1326 }
1327 else
1328#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
1329 {
1330 t_out->fixed_ivlen = 4;
1331 t_in->fixed_ivlen = 4;
1332 }
Hanno Beckera18d1322018-01-03 14:27:32 +00001333 t_out->maclen = 0;
1334 t_in->maclen = 0;
1335 switch( tag_mode )
1336 {
1337 case 0: /* Full tag */
1338 t_out->taglen = 16;
1339 t_in->taglen = 16;
1340 break;
1341 case 1: /* Partial tag */
1342 t_out->taglen = 8;
1343 t_in->taglen = 8;
1344 break;
1345 default:
1346 return( 1 );
1347 }
1348 break;
1349
1350 case MBEDTLS_MODE_CHACHAPOLY:
1351 t_out->fixed_ivlen = 12;
1352 t_in->fixed_ivlen = 12;
1353 t_out->maclen = 0;
1354 t_in->maclen = 0;
1355 switch( tag_mode )
1356 {
1357 case 0: /* Full tag */
1358 t_out->taglen = 16;
1359 t_in->taglen = 16;
1360 break;
1361 case 1: /* Partial tag */
1362 t_out->taglen = 8;
1363 t_in->taglen = 8;
1364 break;
1365 default:
1366 return( 1 );
1367 }
1368 break;
1369
1370 case MBEDTLS_MODE_STREAM:
1371 case MBEDTLS_MODE_CBC:
1372 t_out->fixed_ivlen = 0; /* redundant, must be 0 */
1373 t_in->fixed_ivlen = 0; /* redundant, must be 0 */
1374 t_out->taglen = 0;
1375 t_in->taglen = 0;
1376 switch( tag_mode )
1377 {
1378 case 0: /* Full tag */
1379 t_out->maclen = maclen;
1380 t_in->maclen = maclen;
1381 break;
1382 case 1: /* Partial tag */
1383 t_out->maclen = 10;
1384 t_in->maclen = 10;
1385 break;
1386 default:
1387 return( 1 );
1388 }
1389 break;
1390 default:
1391 return( 1 );
1392 break;
1393 }
1394
1395 /* Setup IV's */
1396
1397 memcpy( &t_in->iv_dec, iv_dec, sizeof( iv_dec ) );
1398 memcpy( &t_in->iv_enc, iv_enc, sizeof( iv_enc ) );
1399 memcpy( &t_out->iv_dec, iv_enc, sizeof( iv_enc ) );
1400 memcpy( &t_out->iv_enc, iv_dec, sizeof( iv_dec ) );
1401
Hanno Beckera0e20d02019-05-15 14:03:01 +01001402#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd856c822019-04-29 17:30:59 +01001403 /* Add CID */
1404 memcpy( &t_in->in_cid, cid0, cid0_len );
1405 memcpy( &t_in->out_cid, cid1, cid1_len );
1406 t_in->in_cid_len = cid0_len;
1407 t_in->out_cid_len = cid1_len;
1408 memcpy( &t_out->in_cid, cid1, cid1_len );
1409 memcpy( &t_out->out_cid, cid0, cid0_len );
1410 t_out->in_cid_len = cid1_len;
1411 t_out->out_cid_len = cid0_len;
Hanno Beckera0e20d02019-05-15 14:03:01 +01001412#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerd856c822019-04-29 17:30:59 +01001413
Hanno Becker81e16a32019-03-01 11:21:44 +00001414cleanup:
1415
Hanno Becker3ee54212019-04-04 16:31:26 +01001416 mbedtls_free( key0 );
1417 mbedtls_free( key1 );
Hanno Becker81e16a32019-03-01 11:21:44 +00001418
Paul Elliott6f1eda72020-06-11 20:22:00 +01001419 mbedtls_free( md0 );
1420 mbedtls_free( md1 );
1421
Hanno Beckera5780f12019-04-05 09:55:37 +01001422 return( ret );
Hanno Beckera18d1322018-01-03 14:27:32 +00001423}
1424
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001425/*
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02001426 * Populate a session structure for serialization tests.
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001427 * Choose dummy values, mostly non-0 to distinguish from the init default.
1428 */
1429static int ssl_populate_session( mbedtls_ssl_session *session,
Manuel Pégourié-Gonnard220403b2019-05-24 09:54:21 +02001430 int ticket_len,
1431 const char *crt_file )
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001432{
1433#if defined(MBEDTLS_HAVE_TIME)
1434 session->start = mbedtls_time( NULL ) - 42;
1435#endif
1436 session->ciphersuite = 0xabcd;
1437 session->compression = 1;
1438 session->id_len = sizeof( session->id );
1439 memset( session->id, 66, session->id_len );
Manuel Pégourié-Gonnard220403b2019-05-24 09:54:21 +02001440 memset( session->master, 17, sizeof( session->master ) );
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001441
Manuel Pégourié-Gonnard1f6033a2019-05-24 10:17:52 +02001442#if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001443 if( strlen( crt_file ) != 0 )
1444 {
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02001445 mbedtls_x509_crt tmp_crt;
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001446 int ret;
Manuel Pégourié-Gonnard6b840702019-05-24 09:40:17 +02001447
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02001448 mbedtls_x509_crt_init( &tmp_crt );
1449 ret = mbedtls_x509_crt_parse_file( &tmp_crt, crt_file );
1450 if( ret != 0 )
1451 return( ret );
1452
1453#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1454 /* Move temporary CRT. */
Manuel Pégourié-Gonnard6b840702019-05-24 09:40:17 +02001455 session->peer_cert = mbedtls_calloc( 1, sizeof( *session->peer_cert ) );
1456 if( session->peer_cert == NULL )
1457 return( -1 );
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02001458 *session->peer_cert = tmp_crt;
1459 memset( &tmp_crt, 0, sizeof( tmp_crt ) );
1460#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
1461 /* Calculate digest of temporary CRT. */
1462 session->peer_cert_digest =
1463 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
1464 if( session->peer_cert_digest == NULL )
1465 return( -1 );
1466 ret = mbedtls_md( mbedtls_md_info_from_type(
1467 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
1468 tmp_crt.raw.p, tmp_crt.raw.len,
1469 session->peer_cert_digest );
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001470 if( ret != 0 )
1471 return( ret );
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02001472 session->peer_cert_digest_type =
1473 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
1474 session->peer_cert_digest_len =
1475 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
1476#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
1477
1478 mbedtls_x509_crt_free( &tmp_crt );
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001479 }
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02001480#else /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_FS_IO */
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001481 (void) crt_file;
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02001482#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_FS_IO */
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001483 session->verify_result = 0xdeadbeef;
1484
1485#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
1486 if( ticket_len != 0 )
1487 {
1488 session->ticket = mbedtls_calloc( 1, ticket_len );
Manuel Pégourié-Gonnard220403b2019-05-24 09:54:21 +02001489 if( session->ticket == NULL )
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001490 return( -1 );
1491 memset( session->ticket, 33, ticket_len );
1492 }
1493 session->ticket_len = ticket_len;
1494 session->ticket_lifetime = 86401;
1495#else
1496 (void) ticket_len;
1497#endif
1498
1499#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1500 session->mfl_code = 1;
1501#endif
1502#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1503 session->trunc_hmac = 1;
1504#endif
1505#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1506 session->encrypt_then_mac = 1;
1507#endif
1508
1509 return( 0 );
1510}
1511
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001512/*
1513 * Perform data exchanging between \p ssl_1 and \p ssl_2 and check if the
1514 * message was sent in the correct number of fragments.
1515 *
1516 * /p ssl_1 and /p ssl_2 Endpoints represented by mbedtls_ssl_context. Both
1517 * of them must be initialized and connected beforehand.
1518 * /p msg_len_1 and /p msg_len_2 specify the size of the message to send.
1519 * /p expected_fragments_1 and /p expected_fragments_2 determine in how many
1520 * fragments the message should be sent.
1521 * expected_fragments is 0: can be used for DTLS testing while the message
1522 * size is larger than MFL. In that case the message
1523 * cannot be fragmented and sent to the second endpoint.
1524 * This value can be used for negative tests.
1525 * expected_fragments is 1: can be used for TLS/DTLS testing while the
1526 * message size is below MFL
1527 * expected_fragments > 1: can be used for TLS testing while the message
1528 * size is larger than MFL
1529 *
1530 * \retval 0 on success, otherwise error code.
1531 */
1532int mbedtls_exchange_data( mbedtls_ssl_context *ssl_1,
1533 int msg_len_1, const int expected_fragments_1,
1534 mbedtls_ssl_context *ssl_2,
1535 int msg_len_2, const int expected_fragments_2 )
1536{
1537 unsigned char *msg_buf_1 = malloc( msg_len_1 );
1538 unsigned char *msg_buf_2 = malloc( msg_len_2 );
1539 unsigned char *in_buf_1 = malloc( msg_len_2 );
1540 unsigned char *in_buf_2 = malloc( msg_len_1 );
1541 int msg_type, ret = -1;
1542
1543 /* Perform this test with two message types. At first use a message
1544 * consisting of only 0x00 for the client and only 0xFF for the server.
1545 * At the second time use message with generated data */
1546 for( msg_type = 0; msg_type < 2; msg_type++ )
1547 {
1548 int written_1 = 0;
1549 int written_2 = 0;
1550 int read_1 = 0;
1551 int read_2 = 0;
1552 int fragments_1 = 0;
1553 int fragments_2 = 0;
1554
1555 if( msg_type == 0 )
1556 {
1557 memset( msg_buf_1, 0x00, msg_len_1 );
1558 memset( msg_buf_2, 0xff, msg_len_2 );
1559 }
1560 else
1561 {
1562 int i, j = 0;
1563 for( i = 0; i < msg_len_1; i++ )
1564 {
1565 msg_buf_1[i] = j++ & 0xFF;
1566 }
1567 for( i = 0; i < msg_len_2; i++ )
1568 {
1569 msg_buf_2[i] = ( j -= 5 ) & 0xFF;
1570 }
1571 }
1572
1573 while( read_1 < msg_len_2 || read_2 < msg_len_1 )
1574 {
1575 /* ssl_1 sending */
1576 if( msg_len_1 > written_1 )
1577 {
1578 ret = mbedtls_ssl_write_fragment( ssl_1, msg_buf_1,
1579 msg_len_1, &written_1,
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001580 expected_fragments_1 );
1581 if( expected_fragments_1 == 0 )
1582 {
1583 /* This error is expected when the message is too large and
1584 * cannot be fragmented */
1585 TEST_ASSERT( ret == MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1586 msg_len_1 = 0;
1587 }
1588 else
1589 {
1590 TEST_ASSERT( ret == 0 );
1591 }
1592 }
1593
1594 /* ssl_2 sending */
1595 if( msg_len_2 > written_2 )
1596 {
1597 ret = mbedtls_ssl_write_fragment( ssl_2, msg_buf_2,
1598 msg_len_2, &written_2,
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001599 expected_fragments_2 );
1600 if( expected_fragments_2 == 0 )
1601 {
1602 /* This error is expected when the message is too large and
1603 * cannot be fragmented */
1604 TEST_ASSERT( ret == MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1605 msg_len_2 = 0;
1606 }
1607 else
1608 {
1609 TEST_ASSERT( ret == 0 );
1610 }
1611 }
1612
1613 /* ssl_1 reading */
1614 if( read_1 < msg_len_2 )
1615 {
1616 ret = mbedtls_ssl_read_fragment( ssl_1, in_buf_1,
1617 msg_len_2, &read_1,
Piotr Nowicki438bf3b2020-03-10 12:59:10 +01001618 &fragments_2,
1619 expected_fragments_2 );
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001620 TEST_ASSERT( ret == 0 );
1621 }
1622
1623 /* ssl_2 reading */
1624 if( read_2 < msg_len_1 )
1625 {
1626 ret = mbedtls_ssl_read_fragment( ssl_2, in_buf_2,
1627 msg_len_1, &read_2,
Piotr Nowicki438bf3b2020-03-10 12:59:10 +01001628 &fragments_1,
1629 expected_fragments_1 );
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001630 TEST_ASSERT( ret == 0 );
1631 }
1632 }
1633
1634 ret = -1;
1635 TEST_ASSERT( 0 == memcmp( msg_buf_1, in_buf_2, msg_len_1 ) );
1636 TEST_ASSERT( 0 == memcmp( msg_buf_2, in_buf_1, msg_len_2 ) );
1637 TEST_ASSERT( fragments_1 == expected_fragments_1 );
1638 TEST_ASSERT( fragments_2 == expected_fragments_2 );
1639 }
1640
1641 ret = 0;
1642
1643exit:
1644 free( msg_buf_1 );
1645 free( in_buf_1 );
1646 free( msg_buf_2 );
1647 free( in_buf_2 );
1648
1649 return ret;
1650}
1651
Piotr Nowicki95e9eb82020-02-14 11:33:34 +01001652/*
1653 * Perform data exchanging between \p ssl_1 and \p ssl_2. Both of endpoints
1654 * must be initialized and connected beforehand.
1655 *
1656 * \retval 0 on success, otherwise error code.
1657 */
1658int exchange_data( mbedtls_ssl_context *ssl_1,
1659 mbedtls_ssl_context *ssl_2 )
1660{
1661 return mbedtls_exchange_data( ssl_1, 256, 1,
1662 ssl_2, 256, 1 );
1663}
1664
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02001665#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
1666 defined(MBEDTLS_ENTROPY_C) && \
1667 defined(MBEDTLS_CTR_DRBG_C)
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001668void perform_handshake( handshake_test_options* options )
1669{
1670 /* forced_ciphersuite needs to last until the end of the handshake */
1671 int forced_ciphersuite[2];
1672 enum { BUFFSIZE = 17000 };
1673 mbedtls_endpoint client, server;
Gilles Peskineeccd8882020-03-10 12:19:08 +01001674#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001675 const char *psk_identity = "foo";
1676#endif
1677#if defined(MBEDTLS_TIMING_C)
1678 mbedtls_timing_delay_context timer_client, timer_server;
1679#endif
1680#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1681 unsigned char *context_buf = NULL;
1682 size_t context_buf_len;
1683#endif
1684#if defined(MBEDTLS_SSL_RENEGOTIATION)
1685 int ret = -1;
1686#endif
Paul Elliottc8570442020-04-15 17:00:50 +01001687 int expected_handshake_result = 0;
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001688
1689 mbedtls_test_message_queue server_queue, client_queue;
1690 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05001691 mbedtls_message_socket_init( &server_context );
1692 mbedtls_message_socket_init( &client_context );
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001693
1694 /* Client side */
1695 if( options->dtls != 0 )
1696 {
1697 TEST_ASSERT( mbedtls_endpoint_init( &client, MBEDTLS_SSL_IS_CLIENT,
1698 options->pk_alg, &client_context,
1699 &client_queue,
1700 &server_queue ) == 0 );
1701#if defined(MBEDTLS_TIMING_C)
1702 mbedtls_ssl_set_timer_cb( &client.ssl, &timer_client,
1703 mbedtls_timing_set_delay,
1704 mbedtls_timing_get_delay );
1705#endif
1706 }
1707 else
1708 {
1709 TEST_ASSERT( mbedtls_endpoint_init( &client, MBEDTLS_SSL_IS_CLIENT,
1710 options->pk_alg, NULL, NULL,
1711 NULL ) == 0 );
1712 }
Paul Elliottc8570442020-04-15 17:00:50 +01001713
1714 if( options->client_min_version != TEST_SSL_MINOR_VERSION_NONE )
1715 {
1716 mbedtls_ssl_conf_min_version( &client.conf, MBEDTLS_SSL_MAJOR_VERSION_3,
1717 options->client_min_version );
1718 }
1719
1720 if( options->client_max_version != TEST_SSL_MINOR_VERSION_NONE )
1721 {
1722 mbedtls_ssl_conf_max_version( &client.conf, MBEDTLS_SSL_MAJOR_VERSION_3,
1723 options->client_max_version );
1724 }
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001725
1726 if( strlen( options->cipher ) > 0 )
1727 {
1728 set_ciphersuite( &client.conf, options->cipher, forced_ciphersuite );
1729 }
Piotr Nowickibde7ee82020-02-21 10:59:50 +01001730
1731#if defined (MBEDTLS_DEBUG_C)
1732 if( options->cli_log_fun )
1733 {
1734 mbedtls_debug_set_threshold( 4 );
1735 mbedtls_ssl_conf_dbg( &client.conf, options->cli_log_fun,
1736 options->cli_log_obj );
1737 }
1738#endif
1739
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001740 /* Server side */
1741 if( options->dtls != 0 )
1742 {
1743 TEST_ASSERT( mbedtls_endpoint_init( &server, MBEDTLS_SSL_IS_SERVER,
1744 options->pk_alg, &server_context,
1745 &server_queue,
1746 &client_queue) == 0 );
1747#if defined(MBEDTLS_TIMING_C)
1748 mbedtls_ssl_set_timer_cb( &server.ssl, &timer_server,
1749 mbedtls_timing_set_delay,
1750 mbedtls_timing_get_delay );
1751#endif
1752 }
1753 else
1754 {
1755 TEST_ASSERT( mbedtls_endpoint_init( &server, MBEDTLS_SSL_IS_SERVER,
1756 options->pk_alg, NULL, NULL, NULL ) == 0 );
1757 }
Piotr Nowickibde7ee82020-02-21 10:59:50 +01001758
1759 mbedtls_ssl_conf_authmode( &server.conf, options->srv_auth_mode );
1760
Paul Elliottc8570442020-04-15 17:00:50 +01001761 if( options->server_min_version != TEST_SSL_MINOR_VERSION_NONE )
1762 {
1763 mbedtls_ssl_conf_min_version( &server.conf, MBEDTLS_SSL_MAJOR_VERSION_3,
1764 options->server_min_version );
1765 }
1766
1767 if( options->server_max_version != TEST_SSL_MINOR_VERSION_NONE )
1768 {
1769 mbedtls_ssl_conf_max_version( &server.conf, MBEDTLS_SSL_MAJOR_VERSION_3,
1770 options->server_max_version );
1771 }
1772
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001773#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1774 TEST_ASSERT( mbedtls_ssl_conf_max_frag_len( &(server.conf),
1775 (unsigned char) options->mfl ) == 0 );
1776 TEST_ASSERT( mbedtls_ssl_conf_max_frag_len( &(client.conf),
1777 (unsigned char) options->mfl ) == 0 );
1778#else
1779 TEST_ASSERT( MBEDTLS_SSL_MAX_FRAG_LEN_NONE == options->mfl );
1780#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
1781
Gilles Peskineeccd8882020-03-10 12:19:08 +01001782#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001783 if( options->psk_str != NULL && options->psk_str->len > 0 )
1784 {
1785 TEST_ASSERT( mbedtls_ssl_conf_psk( &client.conf, options->psk_str->x,
1786 options->psk_str->len,
1787 (const unsigned char *) psk_identity,
1788 strlen( psk_identity ) ) == 0 );
1789
1790 TEST_ASSERT( mbedtls_ssl_conf_psk( &server.conf, options->psk_str->x,
1791 options->psk_str->len,
1792 (const unsigned char *) psk_identity,
1793 strlen( psk_identity ) ) == 0 );
1794
1795 mbedtls_ssl_conf_psk_cb( &server.conf, psk_dummy_callback, NULL );
1796 }
1797#endif
1798#if defined(MBEDTLS_SSL_RENEGOTIATION)
1799 if( options->renegotiate )
1800 {
1801 mbedtls_ssl_conf_renegotiation( &(server.conf),
1802 MBEDTLS_SSL_RENEGOTIATION_ENABLED );
1803 mbedtls_ssl_conf_renegotiation( &(client.conf),
1804 MBEDTLS_SSL_RENEGOTIATION_ENABLED );
1805
1806 mbedtls_ssl_conf_legacy_renegotiation( &(server.conf),
1807 options->legacy_renegotiation );
1808 mbedtls_ssl_conf_legacy_renegotiation( &(client.conf),
1809 options->legacy_renegotiation );
1810 }
1811#endif /* MBEDTLS_SSL_RENEGOTIATION */
1812
Piotr Nowickibde7ee82020-02-21 10:59:50 +01001813#if defined (MBEDTLS_DEBUG_C)
1814 if( options->srv_log_fun )
1815 {
1816 mbedtls_debug_set_threshold( 4 );
1817 mbedtls_ssl_conf_dbg( &server.conf, options->srv_log_fun,
1818 options->srv_log_obj );
1819 }
1820#endif
1821
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001822 TEST_ASSERT( mbedtls_mock_socket_connect( &(client.socket),
1823 &(server.socket),
1824 BUFFSIZE ) == 0 );
1825
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001826#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1827 if( options->resize_buffers != 0 )
1828 {
1829 /* Ensure that the buffer sizes are appropriate before resizes */
1830 TEST_ASSERT( client.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN );
1831 TEST_ASSERT( client.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN );
1832 TEST_ASSERT( server.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN );
1833 TEST_ASSERT( server.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN );
1834 }
1835#endif
1836
Paul Elliottc8570442020-04-15 17:00:50 +01001837 if( options->expected_negotiated_version == TEST_SSL_MINOR_VERSION_NONE )
1838 {
1839 expected_handshake_result = MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION;
1840 }
1841
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001842 TEST_ASSERT( mbedtls_move_handshake_to_state( &(client.ssl),
1843 &(server.ssl),
1844 MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Elliottc8570442020-04-15 17:00:50 +01001845 == expected_handshake_result );
1846
1847 if( expected_handshake_result != 0 )
1848 {
1849 /* Connection will have failed by this point, skip to cleanup */
1850 goto exit;
1851 }
1852
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001853 TEST_ASSERT( client.ssl.state == MBEDTLS_SSL_HANDSHAKE_OVER );
1854 TEST_ASSERT( server.ssl.state == MBEDTLS_SSL_HANDSHAKE_OVER );
1855
Paul Elliottc8570442020-04-15 17:00:50 +01001856 /* Check that we agree on the version... */
1857 TEST_ASSERT( client.ssl.minor_ver == server.ssl.minor_ver );
1858
1859 /* And check that the version negotiated is the expected one. */
1860 TEST_EQUAL( client.ssl.minor_ver, options->expected_negotiated_version );
1861
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001862#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1863 if( options->resize_buffers != 0 )
1864 {
Paul Elliottc8570442020-04-15 17:00:50 +01001865 if( options->expected_negotiated_version != MBEDTLS_SSL_MINOR_VERSION_0 &&
1866 options->expected_negotiated_version != MBEDTLS_SSL_MINOR_VERSION_1 )
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001867 {
1868 /* A server, when using DTLS, might delay a buffer resize to happen
1869 * after it receives a message, so we force it. */
1870 TEST_ASSERT( exchange_data( &(client.ssl), &(server.ssl) ) == 0 );
1871
1872 TEST_ASSERT( client.ssl.out_buf_len ==
1873 mbedtls_ssl_get_output_buflen( &client.ssl ) );
1874 TEST_ASSERT( client.ssl.in_buf_len ==
1875 mbedtls_ssl_get_input_buflen( &client.ssl ) );
1876 TEST_ASSERT( server.ssl.out_buf_len ==
1877 mbedtls_ssl_get_output_buflen( &server.ssl ) );
1878 TEST_ASSERT( server.ssl.in_buf_len ==
1879 mbedtls_ssl_get_input_buflen( &server.ssl ) );
1880 }
1881 }
1882#endif
1883
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001884 if( options->cli_msg_len != 0 || options->srv_msg_len != 0 )
1885 {
1886 /* Start data exchanging test */
1887 TEST_ASSERT( mbedtls_exchange_data( &(client.ssl), options->cli_msg_len,
1888 options->expected_cli_fragments,
1889 &(server.ssl), options->srv_msg_len,
1890 options->expected_srv_fragments )
1891 == 0 );
1892 }
1893#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1894 if( options->serialize == 1 )
1895 {
1896 TEST_ASSERT( options->dtls == 1 );
1897
1898 TEST_ASSERT( mbedtls_ssl_context_save( &(server.ssl), NULL,
1899 0, &context_buf_len )
1900 == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1901
1902 context_buf = mbedtls_calloc( 1, context_buf_len );
1903 TEST_ASSERT( context_buf != NULL );
1904
1905 TEST_ASSERT( mbedtls_ssl_context_save( &(server.ssl), context_buf,
1906 context_buf_len,
1907 &context_buf_len ) == 0 );
1908
1909 mbedtls_ssl_free( &(server.ssl) );
1910 mbedtls_ssl_init( &(server.ssl) );
1911
1912 TEST_ASSERT( mbedtls_ssl_setup( &(server.ssl), &(server.conf) ) == 0 );
1913
1914 mbedtls_ssl_set_bio( &( server.ssl ), &server_context,
1915 mbedtls_mock_tcp_send_msg,
1916 mbedtls_mock_tcp_recv_msg,
1917 NULL );
1918
1919#if defined(MBEDTLS_TIMING_C)
1920 mbedtls_ssl_set_timer_cb( &server.ssl, &timer_server,
1921 mbedtls_timing_set_delay,
1922 mbedtls_timing_get_delay );
1923#endif
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001924#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1925 if( options->resize_buffers != 0 )
1926 {
1927 /* Ensure that the buffer sizes are appropriate before resizes */
1928 TEST_ASSERT( server.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN );
1929 TEST_ASSERT( server.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN );
1930 }
1931#endif
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001932 TEST_ASSERT( mbedtls_ssl_context_load( &( server.ssl ), context_buf,
1933 context_buf_len ) == 0 );
1934
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001935#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1936 /* Validate buffer sizes after context deserialization */
1937 if( options->resize_buffers != 0 )
1938 {
1939 TEST_ASSERT( server.ssl.out_buf_len ==
1940 mbedtls_ssl_get_output_buflen( &server.ssl ) );
1941 TEST_ASSERT( server.ssl.in_buf_len ==
1942 mbedtls_ssl_get_input_buflen( &server.ssl ) );
1943 }
1944#endif
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001945 /* Retest writing/reading */
1946 if( options->cli_msg_len != 0 || options->srv_msg_len != 0 )
1947 {
1948 TEST_ASSERT( mbedtls_exchange_data( &(client.ssl),
1949 options->cli_msg_len,
1950 options->expected_cli_fragments,
1951 &(server.ssl),
1952 options->srv_msg_len,
1953 options->expected_srv_fragments )
1954 == 0 );
1955 }
1956 }
1957#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001958
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001959#if defined(MBEDTLS_SSL_RENEGOTIATION)
1960 if( options->renegotiate )
1961 {
1962 /* Start test with renegotiation */
1963 TEST_ASSERT( server.ssl.renego_status ==
1964 MBEDTLS_SSL_INITIAL_HANDSHAKE );
1965 TEST_ASSERT( client.ssl.renego_status ==
1966 MBEDTLS_SSL_INITIAL_HANDSHAKE );
1967
1968 /* After calling this function for the server, it only sends a handshake
1969 * request. All renegotiation should happen during data exchanging */
1970 TEST_ASSERT( mbedtls_ssl_renegotiate( &(server.ssl) ) == 0 );
1971 TEST_ASSERT( server.ssl.renego_status ==
1972 MBEDTLS_SSL_RENEGOTIATION_PENDING );
1973 TEST_ASSERT( client.ssl.renego_status ==
1974 MBEDTLS_SSL_INITIAL_HANDSHAKE );
1975
1976 TEST_ASSERT( exchange_data( &(client.ssl), &(server.ssl) ) == 0 );
1977 TEST_ASSERT( server.ssl.renego_status ==
1978 MBEDTLS_SSL_RENEGOTIATION_DONE );
1979 TEST_ASSERT( client.ssl.renego_status ==
1980 MBEDTLS_SSL_RENEGOTIATION_DONE );
1981
1982 /* After calling mbedtls_ssl_renegotiate for the client all renegotiation
1983 * should happen inside this function. However in this test, we cannot
1984 * perform simultaneous communication betwen client and server so this
1985 * function will return waiting error on the socket. All rest of
1986 * renegotiation should happen during data exchanging */
1987 ret = mbedtls_ssl_renegotiate( &(client.ssl) );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001988#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1989 if( options->resize_buffers != 0 )
1990 {
1991 /* Ensure that the buffer sizes are appropriate before resizes */
1992 TEST_ASSERT( client.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN );
1993 TEST_ASSERT( client.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN );
1994 }
1995#endif
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001996 TEST_ASSERT( ret == 0 ||
1997 ret == MBEDTLS_ERR_SSL_WANT_READ ||
1998 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
1999 TEST_ASSERT( server.ssl.renego_status ==
2000 MBEDTLS_SSL_RENEGOTIATION_DONE );
2001 TEST_ASSERT( client.ssl.renego_status ==
2002 MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS );
2003
2004 TEST_ASSERT( exchange_data( &(client.ssl), &(server.ssl) ) == 0 );
2005 TEST_ASSERT( server.ssl.renego_status ==
2006 MBEDTLS_SSL_RENEGOTIATION_DONE );
2007 TEST_ASSERT( client.ssl.renego_status ==
2008 MBEDTLS_SSL_RENEGOTIATION_DONE );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05002009#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2010 /* Validate buffer sizes after renegotiation */
2011 if( options->resize_buffers != 0 )
2012 {
2013 TEST_ASSERT( client.ssl.out_buf_len ==
2014 mbedtls_ssl_get_output_buflen( &client.ssl ) );
2015 TEST_ASSERT( client.ssl.in_buf_len ==
2016 mbedtls_ssl_get_input_buflen( &client.ssl ) );
2017 TEST_ASSERT( server.ssl.out_buf_len ==
2018 mbedtls_ssl_get_output_buflen( &server.ssl ) );
2019 TEST_ASSERT( server.ssl.in_buf_len ==
2020 mbedtls_ssl_get_input_buflen( &server.ssl ) );
2021 }
2022#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002023 }
2024#endif /* MBEDTLS_SSL_RENEGOTIATION */
2025
2026exit:
2027 mbedtls_endpoint_free( &client, options->dtls != 0 ? &client_context : NULL );
2028 mbedtls_endpoint_free( &server, options->dtls != 0 ? &server_context : NULL );
Piotr Nowickibde7ee82020-02-21 10:59:50 +01002029#if defined (MBEDTLS_DEBUG_C)
2030 if( options->cli_log_fun || options->srv_log_fun )
2031 {
2032 mbedtls_debug_set_threshold( 0 );
2033 }
2034#endif
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002035#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
2036 if( context_buf != NULL )
2037 mbedtls_free( context_buf );
2038#endif
2039}
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02002040#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002041
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002042/* END_HEADER */
2043
2044/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002045 * depends_on:MBEDTLS_SSL_TLS_C
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002046 * END_DEPENDENCIES
2047 */
2048
Janos Follath6264e662019-11-26 11:11:15 +00002049/* BEGIN_CASE */
2050void test_callback_buffer_sanity()
2051{
2052 enum { MSGLEN = 10 };
2053 mbedtls_test_buffer buf;
2054 unsigned char input[MSGLEN];
2055 unsigned char output[MSGLEN];
2056
2057 memset( input, 0, sizeof(input) );
2058
2059 /* Make sure calling put and get on NULL buffer results in error. */
2060 TEST_ASSERT( mbedtls_test_buffer_put( NULL, input, sizeof( input ) )
2061 == -1 );
2062 TEST_ASSERT( mbedtls_test_buffer_get( NULL, output, sizeof( output ) )
2063 == -1 );
2064 TEST_ASSERT( mbedtls_test_buffer_put( NULL, NULL, sizeof( input ) ) == -1 );
Andrzej Kurekf7774142020-01-22 06:34:59 -05002065
Janos Follath6264e662019-11-26 11:11:15 +00002066 TEST_ASSERT( mbedtls_test_buffer_put( NULL, NULL, 0 ) == -1 );
2067 TEST_ASSERT( mbedtls_test_buffer_get( NULL, NULL, 0 ) == -1 );
2068
2069 /* Make sure calling put and get on a buffer that hasn't been set up results
2070 * in eror. */
2071 mbedtls_test_buffer_init( &buf );
2072
2073 TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, sizeof( input ) ) == -1 );
2074 TEST_ASSERT( mbedtls_test_buffer_get( &buf, output, sizeof( output ) )
2075 == -1 );
2076 TEST_ASSERT( mbedtls_test_buffer_put( &buf, NULL, sizeof( input ) ) == -1 );
Andrzej Kurekf7774142020-01-22 06:34:59 -05002077
Janos Follath6264e662019-11-26 11:11:15 +00002078 TEST_ASSERT( mbedtls_test_buffer_put( &buf, NULL, 0 ) == -1 );
2079 TEST_ASSERT( mbedtls_test_buffer_get( &buf, NULL, 0 ) == -1 );
2080
Andrzej Kurekf7774142020-01-22 06:34:59 -05002081 /* Make sure calling put and get on NULL input only results in
2082 * error if the length is not zero, and that a NULL output is valid for data
2083 * dropping.
2084 */
Janos Follath6264e662019-11-26 11:11:15 +00002085
2086 TEST_ASSERT( mbedtls_test_buffer_setup( &buf, sizeof( input ) ) == 0 );
2087
2088 TEST_ASSERT( mbedtls_test_buffer_put( &buf, NULL, sizeof( input ) ) == -1 );
2089 TEST_ASSERT( mbedtls_test_buffer_get( &buf, NULL, sizeof( output ) )
Andrzej Kurekf7774142020-01-22 06:34:59 -05002090 == 0 );
Janos Follath6264e662019-11-26 11:11:15 +00002091 TEST_ASSERT( mbedtls_test_buffer_put( &buf, NULL, 0 ) == 0 );
2092 TEST_ASSERT( mbedtls_test_buffer_get( &buf, NULL, 0 ) == 0 );
2093
Piotr Nowickifb437d72020-01-13 16:59:12 +01002094 /* Make sure calling put several times in the row is safe */
2095
2096 TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, sizeof( input ) )
2097 == sizeof( input ) );
2098 TEST_ASSERT( mbedtls_test_buffer_get( &buf, output, 2 ) == 2 );
2099 TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, 1 ) == 1 );
2100 TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, 2 ) == 1 );
2101 TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, 2 ) == 0 );
2102
2103
Janos Follath6264e662019-11-26 11:11:15 +00002104exit:
2105
2106 mbedtls_test_buffer_free( &buf );
2107}
2108/* END_CASE */
2109
2110/*
2111 * Test if the implementation of `mbedtls_test_buffer` related functions is
2112 * correct and works as expected.
2113 *
2114 * That is
2115 * - If we try to put in \p put1 bytes then we can put in \p put1_ret bytes.
2116 * - Afterwards if we try to get \p get1 bytes then we can get \get1_ret bytes.
2117 * - Next, if we try to put in \p put1 bytes then we can put in \p put1_ret
2118 * bytes.
2119 * - Afterwards if we try to get \p get1 bytes then we can get \get1_ret bytes.
2120 * - All of the bytes we got match the bytes we put in in a FIFO manner.
2121 */
2122
2123/* BEGIN_CASE */
2124void test_callback_buffer( int size, int put1, int put1_ret,
2125 int get1, int get1_ret, int put2, int put2_ret,
2126 int get2, int get2_ret )
2127{
2128 enum { ROUNDS = 2 };
2129 size_t put[ROUNDS];
2130 int put_ret[ROUNDS];
2131 size_t get[ROUNDS];
2132 int get_ret[ROUNDS];
2133 mbedtls_test_buffer buf;
2134 unsigned char* input = NULL;
2135 size_t input_len;
2136 unsigned char* output = NULL;
2137 size_t output_len;
Janos Follath031827f2019-11-27 11:12:14 +00002138 size_t i, j, written, read;
Janos Follath6264e662019-11-26 11:11:15 +00002139
2140 mbedtls_test_buffer_init( &buf );
2141 TEST_ASSERT( mbedtls_test_buffer_setup( &buf, size ) == 0 );
2142
2143 /* Check the sanity of input parameters and initialise local variables. That
2144 * is, ensure that the amount of data is not negative and that we are not
2145 * expecting more to put or get than we actually asked for. */
2146 TEST_ASSERT( put1 >= 0 );
2147 put[0] = put1;
2148 put_ret[0] = put1_ret;
2149 TEST_ASSERT( put1_ret <= put1 );
2150 TEST_ASSERT( put2 >= 0 );
2151 put[1] = put2;
2152 put_ret[1] = put2_ret;
2153 TEST_ASSERT( put2_ret <= put2 );
2154
2155 TEST_ASSERT( get1 >= 0 );
2156 get[0] = get1;
2157 get_ret[0] = get1_ret;
2158 TEST_ASSERT( get1_ret <= get1 );
2159 TEST_ASSERT( get2 >= 0 );
2160 get[1] = get2;
2161 get_ret[1] = get2_ret;
2162 TEST_ASSERT( get2_ret <= get2 );
2163
2164 input_len = 0;
2165 /* Calculate actual input and output lengths */
2166 for( j = 0; j < ROUNDS; j++ )
2167 {
2168 if( put_ret[j] > 0 )
2169 {
2170 input_len += put_ret[j];
2171 }
2172 }
2173 /* In order to always have a valid pointer we always allocate at least 1
2174 * byte. */
2175 if( input_len == 0 )
2176 input_len = 1;
2177 ASSERT_ALLOC( input, input_len );
2178
2179 output_len = 0;
2180 for( j = 0; j < ROUNDS; j++ )
2181 {
2182 if( get_ret[j] > 0 )
2183 {
2184 output_len += get_ret[j];
2185 }
2186 }
2187 TEST_ASSERT( output_len <= input_len );
2188 /* In order to always have a valid pointer we always allocate at least 1
2189 * byte. */
2190 if( output_len == 0 )
2191 output_len = 1;
2192 ASSERT_ALLOC( output, output_len );
2193
2194 /* Fill up the buffer with structured data so that unwanted changes
2195 * can be detected */
2196 for( i = 0; i < input_len; i++ )
2197 {
2198 input[i] = i & 0xFF;
2199 }
2200
2201 written = read = 0;
2202 for( j = 0; j < ROUNDS; j++ )
2203 {
2204 TEST_ASSERT( put_ret[j] == mbedtls_test_buffer_put( &buf,
2205 input + written, put[j] ) );
2206 written += put_ret[j];
2207 TEST_ASSERT( get_ret[j] == mbedtls_test_buffer_get( &buf,
2208 output + read, get[j] ) );
2209 read += get_ret[j];
2210 TEST_ASSERT( read <= written );
2211 if( get_ret[j] > 0 )
2212 {
2213 TEST_ASSERT( memcmp( output + read - get_ret[j],
2214 input + read - get_ret[j], get_ret[j] )
2215 == 0 );
2216 }
2217 }
2218
2219exit:
2220
2221 mbedtls_free( input );
2222 mbedtls_free( output );
2223 mbedtls_test_buffer_free( &buf );
2224}
2225/* END_CASE */
2226
Janos Follath031827f2019-11-27 11:12:14 +00002227/*
Janos Follathc673c2c2019-12-02 15:47:26 +00002228 * Test if the implementation of `mbedtls_mock_socket` related I/O functions is
2229 * correct and works as expected on unconnected sockets.
2230 */
2231
2232/* BEGIN_CASE */
2233void ssl_mock_sanity( )
2234{
2235 enum { MSGLEN = 105 };
2236 unsigned char message[MSGLEN];
2237 unsigned char received[MSGLEN];
2238 mbedtls_mock_socket socket;
2239
2240 mbedtls_mock_socket_init( &socket );
2241 TEST_ASSERT( mbedtls_mock_tcp_send_b( &socket, message, MSGLEN ) < 0 );
2242 mbedtls_mock_socket_close( &socket );
2243 mbedtls_mock_socket_init( &socket );
2244 TEST_ASSERT( mbedtls_mock_tcp_recv_b( &socket, received, MSGLEN ) < 0 );
2245 mbedtls_mock_socket_close( &socket );
2246
2247 mbedtls_mock_socket_init( &socket );
2248 TEST_ASSERT( mbedtls_mock_tcp_send_nb( &socket, message, MSGLEN ) < 0 );
2249 mbedtls_mock_socket_close( &socket );
2250 mbedtls_mock_socket_init( &socket );
2251 TEST_ASSERT( mbedtls_mock_tcp_recv_nb( &socket, received, MSGLEN ) < 0 );
2252 mbedtls_mock_socket_close( &socket );
2253
2254exit:
2255
2256 mbedtls_mock_socket_close( &socket );
2257}
2258/* END_CASE */
2259
2260/*
2261 * Test if the implementation of `mbedtls_mock_socket` related functions can
2262 * send a single message from the client to the server.
Janos Follath031827f2019-11-27 11:12:14 +00002263 */
2264
2265/* BEGIN_CASE */
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002266void ssl_mock_tcp( int blocking )
Janos Follath031827f2019-11-27 11:12:14 +00002267{
Janos Follathc673c2c2019-12-02 15:47:26 +00002268 enum { MSGLEN = 105 };
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002269 enum { BUFLEN = MSGLEN / 5 };
Janos Follathc673c2c2019-12-02 15:47:26 +00002270 unsigned char message[MSGLEN];
2271 unsigned char received[MSGLEN];
2272 mbedtls_mock_socket client;
2273 mbedtls_mock_socket server;
2274 size_t written, read;
2275 int send_ret, recv_ret;
2276 mbedtls_ssl_send_t *send;
2277 mbedtls_ssl_recv_t *recv;
Janos Follathc673c2c2019-12-02 15:47:26 +00002278 unsigned i;
2279
2280 if( blocking == 0 )
2281 {
2282 send = mbedtls_mock_tcp_send_nb;
2283 recv = mbedtls_mock_tcp_recv_nb;
2284 }
2285 else
2286 {
2287 send = mbedtls_mock_tcp_send_b;
2288 recv = mbedtls_mock_tcp_recv_b;
2289 }
2290
2291 mbedtls_mock_socket_init( &client );
2292 mbedtls_mock_socket_init( &server );
2293
2294 /* Fill up the buffer with structured data so that unwanted changes
2295 * can be detected */
2296 for( i = 0; i < MSGLEN; i++ )
2297 {
2298 message[i] = i & 0xFF;
2299 }
2300
2301 /* Make sure that sending a message takes a few iterations. */
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002302 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, BUFLEN ) );
Janos Follathc673c2c2019-12-02 15:47:26 +00002303
2304 /* Send the message to the server */
2305 send_ret = recv_ret = 1;
2306 written = read = 0;
2307 while( send_ret != 0 || recv_ret != 0 )
2308 {
2309 send_ret = send( &client, message + written, MSGLEN - written );
2310
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002311 TEST_ASSERT( send_ret >= 0 );
2312 TEST_ASSERT( send_ret <= BUFLEN );
2313 written += send_ret;
2314
2315 /* If the buffer is full we can test blocking and non-blocking send */
2316 if ( send_ret == BUFLEN )
Janos Follathc673c2c2019-12-02 15:47:26 +00002317 {
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002318 int blocking_ret = send( &client, message , 1 );
2319 if ( blocking )
2320 {
2321 TEST_ASSERT( blocking_ret == 0 );
2322 }
2323 else
2324 {
2325 TEST_ASSERT( blocking_ret == MBEDTLS_ERR_SSL_WANT_WRITE );
2326 }
Janos Follathc673c2c2019-12-02 15:47:26 +00002327 }
Janos Follathc673c2c2019-12-02 15:47:26 +00002328
2329 recv_ret = recv( &server, received + read, MSGLEN - read );
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002330
2331 /* The result depends on whether any data was sent */
2332 if ( send_ret > 0 )
Janos Follathc673c2c2019-12-02 15:47:26 +00002333 {
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002334 TEST_ASSERT( recv_ret > 0 );
2335 TEST_ASSERT( recv_ret <= BUFLEN );
2336 read += recv_ret;
2337 }
2338 else if( blocking )
2339 {
2340 TEST_ASSERT( recv_ret == 0 );
Janos Follathc673c2c2019-12-02 15:47:26 +00002341 }
2342 else
2343 {
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002344 TEST_ASSERT( recv_ret == MBEDTLS_ERR_SSL_WANT_READ );
2345 recv_ret = 0;
Janos Follathc673c2c2019-12-02 15:47:26 +00002346 }
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002347
2348 /* If the buffer is empty we can test blocking and non-blocking read */
2349 if ( recv_ret == BUFLEN )
2350 {
2351 int blocking_ret = recv( &server, received, 1 );
2352 if ( blocking )
2353 {
2354 TEST_ASSERT( blocking_ret == 0 );
2355 }
2356 else
2357 {
2358 TEST_ASSERT( blocking_ret == MBEDTLS_ERR_SSL_WANT_READ );
2359 }
2360 }
Janos Follathc673c2c2019-12-02 15:47:26 +00002361 }
2362 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
2363
2364exit:
2365
2366 mbedtls_mock_socket_close( &client );
2367 mbedtls_mock_socket_close( &server );
2368}
2369/* END_CASE */
2370
2371/*
2372 * Test if the implementation of `mbedtls_mock_socket` related functions can
2373 * send messages in both direction at the same time (with the I/O calls
2374 * interleaving).
2375 */
2376
2377/* BEGIN_CASE */
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002378void ssl_mock_tcp_interleaving( int blocking )
Janos Follathc673c2c2019-12-02 15:47:26 +00002379{
Janos Follath031827f2019-11-27 11:12:14 +00002380 enum { ROUNDS = 2 };
2381 enum { MSGLEN = 105 };
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002382 enum { BUFLEN = MSGLEN / 5 };
Janos Follath031827f2019-11-27 11:12:14 +00002383 unsigned char message[ROUNDS][MSGLEN];
2384 unsigned char received[ROUNDS][MSGLEN];
2385 mbedtls_mock_socket client;
2386 mbedtls_mock_socket server;
2387 size_t written[ROUNDS];
2388 size_t read[ROUNDS];
2389 int send_ret[ROUNDS];
2390 int recv_ret[ROUNDS];
2391 unsigned i, j, progress;
Janos Follath3766ba52019-11-27 13:31:42 +00002392 mbedtls_ssl_send_t *send;
2393 mbedtls_ssl_recv_t *recv;
Janos Follath3766ba52019-11-27 13:31:42 +00002394
2395 if( blocking == 0 )
2396 {
2397 send = mbedtls_mock_tcp_send_nb;
2398 recv = mbedtls_mock_tcp_recv_nb;
2399 }
2400 else
2401 {
2402 send = mbedtls_mock_tcp_send_b;
2403 recv = mbedtls_mock_tcp_recv_b;
2404 }
Janos Follath031827f2019-11-27 11:12:14 +00002405
2406 mbedtls_mock_socket_init( &client );
2407 mbedtls_mock_socket_init( &server );
2408
2409 /* Fill up the buffers with structured data so that unwanted changes
2410 * can be detected */
2411 for( i = 0; i < ROUNDS; i++ )
2412 {
2413 for( j = 0; j < MSGLEN; j++ )
2414 {
2415 message[i][j] = ( i * MSGLEN + j ) & 0xFF;
2416 }
2417 }
2418
Janos Follath031827f2019-11-27 11:12:14 +00002419 /* Make sure that sending a message takes a few iterations. */
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002420 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, BUFLEN ) );
Janos Follath031827f2019-11-27 11:12:14 +00002421
Janos Follath031827f2019-11-27 11:12:14 +00002422 /* Send the message from both sides, interleaving. */
2423 progress = 1;
2424 for( i = 0; i < ROUNDS; i++ )
2425 {
2426 written[i] = 0;
2427 read[i] = 0;
2428 }
2429 /* This loop does not stop as long as there was a successful write or read
2430 * of at least one byte on either side. */
2431 while( progress != 0 )
2432 {
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002433 mbedtls_mock_socket *socket;
Janos Follath031827f2019-11-27 11:12:14 +00002434
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002435 for( i = 0; i < ROUNDS; i++ )
Janos Follath3766ba52019-11-27 13:31:42 +00002436 {
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002437 /* First sending is from the client */
2438 socket = ( i % 2 == 0 ) ? ( &client ) : ( &server );
Janos Follath031827f2019-11-27 11:12:14 +00002439
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002440 send_ret[i] = send( socket, message[i] + written[i],
2441 MSGLEN - written[i] );
2442 TEST_ASSERT( send_ret[i] >= 0 );
2443 TEST_ASSERT( send_ret[i] <= BUFLEN );
2444 written[i] += send_ret[i];
Janos Follath031827f2019-11-27 11:12:14 +00002445
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002446 /* If the buffer is full we can test blocking and non-blocking
2447 * send */
2448 if ( send_ret[i] == BUFLEN )
2449 {
2450 int blocking_ret = send( socket, message[i] , 1 );
2451 if ( blocking )
2452 {
2453 TEST_ASSERT( blocking_ret == 0 );
2454 }
2455 else
2456 {
2457 TEST_ASSERT( blocking_ret == MBEDTLS_ERR_SSL_WANT_WRITE );
2458 }
2459 }
Janos Follath3766ba52019-11-27 13:31:42 +00002460 }
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002461
2462 for( i = 0; i < ROUNDS; i++ )
Janos Follath3766ba52019-11-27 13:31:42 +00002463 {
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002464 /* First receiving is from the server */
2465 socket = ( i % 2 == 0 ) ? ( &server ) : ( &client );
2466
2467 recv_ret[i] = recv( socket, received[i] + read[i],
2468 MSGLEN - read[i] );
2469
2470 /* The result depends on whether any data was sent */
2471 if ( send_ret[i] > 0 )
2472 {
2473 TEST_ASSERT( recv_ret[i] > 0 );
2474 TEST_ASSERT( recv_ret[i] <= BUFLEN );
2475 read[i] += recv_ret[i];
2476 }
2477 else if( blocking )
2478 {
2479 TEST_ASSERT( recv_ret[i] == 0 );
2480 }
2481 else
2482 {
2483 TEST_ASSERT( recv_ret[i] == MBEDTLS_ERR_SSL_WANT_READ );
2484 recv_ret[i] = 0;
2485 }
2486
2487 /* If the buffer is empty we can test blocking and non-blocking
2488 * read */
2489 if ( recv_ret[i] == BUFLEN )
2490 {
2491 int blocking_ret = recv( socket, received[i], 1 );
2492 if ( blocking )
2493 {
2494 TEST_ASSERT( blocking_ret == 0 );
2495 }
2496 else
2497 {
2498 TEST_ASSERT( blocking_ret == MBEDTLS_ERR_SSL_WANT_READ );
2499 }
2500 }
Janos Follath3766ba52019-11-27 13:31:42 +00002501 }
Janos Follath031827f2019-11-27 11:12:14 +00002502
2503 progress = 0;
2504 for( i = 0; i < ROUNDS; i++ )
2505 {
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002506 progress += send_ret[i] + recv_ret[i];
Janos Follath031827f2019-11-27 11:12:14 +00002507 }
2508 }
2509
2510 for( i = 0; i < ROUNDS; i++ )
2511 TEST_ASSERT( memcmp( message[i], received[i], MSGLEN ) == 0 );
2512
2513exit:
2514
2515 mbedtls_mock_socket_close( &client );
2516 mbedtls_mock_socket_close( &server );
2517}
2518/* END_CASE */
2519
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002520/* BEGIN_CASE */
2521void ssl_message_queue_sanity( )
2522{
2523 mbedtls_test_message_queue queue;
2524
2525 /* Trying to push/pull to an empty queue */
2526 TEST_ASSERT( mbedtls_test_message_queue_push_info( NULL, 1 )
2527 == MBEDTLS_TEST_ERROR_ARG_NULL );
2528 TEST_ASSERT( mbedtls_test_message_queue_pop_info( NULL, 1 )
2529 == MBEDTLS_TEST_ERROR_ARG_NULL );
2530
Andrzej Kurek89bdc582020-03-09 06:29:43 -04002531 TEST_ASSERT( mbedtls_test_message_queue_setup( &queue, 3 ) == 0 );
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002532 TEST_ASSERT( queue.capacity == 3 );
2533 TEST_ASSERT( queue.num == 0 );
2534
2535exit:
2536 mbedtls_test_message_queue_free( &queue );
2537}
2538/* END_CASE */
2539
2540/* BEGIN_CASE */
2541void ssl_message_queue_basic( )
2542{
2543 mbedtls_test_message_queue queue;
2544
Andrzej Kurek89bdc582020-03-09 06:29:43 -04002545 TEST_ASSERT( mbedtls_test_message_queue_setup( &queue, 3 ) == 0 );
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002546
2547 /* Sanity test - 3 pushes and 3 pops with sufficient space */
2548 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 );
2549 TEST_ASSERT( queue.capacity == 3 );
2550 TEST_ASSERT( queue.num == 1 );
2551 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 );
2552 TEST_ASSERT( queue.capacity == 3 );
2553 TEST_ASSERT( queue.num == 2 );
2554 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 2 ) == 2 );
2555 TEST_ASSERT( queue.capacity == 3 );
2556 TEST_ASSERT( queue.num == 3 );
2557
2558 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 );
2559 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 );
2560 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 2 ) == 2 );
2561
2562exit:
2563 mbedtls_test_message_queue_free( &queue );
2564}
2565/* END_CASE */
2566
2567/* BEGIN_CASE */
2568void ssl_message_queue_overflow_underflow( )
2569{
2570 mbedtls_test_message_queue queue;
2571
Andrzej Kurek89bdc582020-03-09 06:29:43 -04002572 TEST_ASSERT( mbedtls_test_message_queue_setup( &queue, 3 ) == 0 );
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002573
2574 /* 4 pushes (last one with an error), 4 pops (last one with an error) */
2575 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 );
2576 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 );
2577 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 2 ) == 2 );
2578 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 3 )
Andrzej Kurekf46b9122020-02-07 08:19:00 -05002579 == MBEDTLS_ERR_SSL_WANT_WRITE );
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002580
2581 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 );
2582 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 );
2583 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 2 ) == 2 );
2584
2585 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 )
Andrzej Kurekf46b9122020-02-07 08:19:00 -05002586 == MBEDTLS_ERR_SSL_WANT_READ );
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002587
2588exit:
2589 mbedtls_test_message_queue_free( &queue );
2590}
2591/* END_CASE */
2592
2593/* BEGIN_CASE */
2594void ssl_message_queue_interleaved( )
2595{
2596 mbedtls_test_message_queue queue;
2597
Andrzej Kurek89bdc582020-03-09 06:29:43 -04002598 TEST_ASSERT( mbedtls_test_message_queue_setup( &queue, 3 ) == 0 );
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002599
2600 /* Interleaved test - [2 pushes, 1 pop] twice, and then two pops
2601 * (to wrap around the buffer) */
2602 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 );
2603 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 );
2604
2605 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 );
2606
2607 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 2 ) == 2 );
2608 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 3 ) == 3 );
2609
2610 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 );
2611 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 2 ) == 2 );
2612
2613 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 5 ) == 5 );
2614 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 8 ) == 8 );
2615
2616 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 3 ) == 3 );
2617
2618 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 5 ) == 5 );
2619
2620 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 8 ) == 8 );
2621
2622exit:
2623 mbedtls_test_message_queue_free( &queue );
2624}
2625/* END_CASE */
2626
2627/* BEGIN_CASE */
2628void ssl_message_queue_insufficient_buffer( )
2629{
2630 mbedtls_test_message_queue queue;
2631 size_t message_len = 10;
2632 size_t buffer_len = 5;
2633
Andrzej Kurek89bdc582020-03-09 06:29:43 -04002634 TEST_ASSERT( mbedtls_test_message_queue_setup( &queue, 1 ) == 0 );
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002635
2636 /* Popping without a sufficient buffer */
2637 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, message_len )
2638 == (int) message_len );
2639 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, buffer_len )
2640 == (int) buffer_len );
2641exit:
2642 mbedtls_test_message_queue_free( &queue );
2643}
2644/* END_CASE */
2645
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002646/* BEGIN_CASE */
2647void ssl_message_mock_uninitialized( )
2648{
2649 enum { MSGLEN = 10 };
2650 unsigned char message[MSGLEN], received[MSGLEN];
2651 mbedtls_mock_socket client, server;
2652 mbedtls_test_message_queue server_queue, client_queue;
2653 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05002654 mbedtls_message_socket_init( &server_context );
2655 mbedtls_message_socket_init( &client_context );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002656
2657 /* Send with a NULL context */
2658 TEST_ASSERT( mbedtls_mock_tcp_send_msg( NULL, message, MSGLEN )
2659 == MBEDTLS_TEST_ERROR_CONTEXT_ERROR );
2660
2661 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( NULL, message, MSGLEN )
2662 == MBEDTLS_TEST_ERROR_CONTEXT_ERROR );
2663
2664 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 1,
2665 &server,
2666 &server_context ) == 0 );
2667
2668 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 1,
2669 &client,
2670 &client_context ) == 0 );
2671
2672 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, MSGLEN )
2673 == MBEDTLS_TEST_ERROR_SEND_FAILED );
2674
2675 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
Andrzej Kurekf46b9122020-02-07 08:19:00 -05002676 == MBEDTLS_ERR_SSL_WANT_READ );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002677
2678 /* Push directly to a queue to later simulate a disconnected behavior */
2679 TEST_ASSERT( mbedtls_test_message_queue_push_info( &server_queue, MSGLEN )
2680 == MSGLEN );
2681
2682 /* Test if there's an error when trying to read from a disconnected
2683 * socket */
2684 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
2685 == MBEDTLS_TEST_ERROR_RECV_FAILED );
2686 exit:
2687 mbedtls_message_socket_close( &server_context );
2688 mbedtls_message_socket_close( &client_context );
2689}
2690/* END_CASE */
2691
2692/* BEGIN_CASE */
2693void ssl_message_mock_basic( )
2694{
2695 enum { MSGLEN = 10 };
2696 unsigned char message[MSGLEN], received[MSGLEN];
2697 mbedtls_mock_socket client, server;
2698 unsigned i;
2699 mbedtls_test_message_queue server_queue, client_queue;
2700 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05002701 mbedtls_message_socket_init( &server_context );
2702 mbedtls_message_socket_init( &client_context );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002703
2704 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 1,
2705 &server,
2706 &server_context ) == 0 );
2707
2708 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 1,
2709 &client,
2710 &client_context ) == 0 );
2711
2712 /* Fill up the buffer with structured data so that unwanted changes
2713 * can be detected */
2714 for( i = 0; i < MSGLEN; i++ )
2715 {
2716 message[i] = i & 0xFF;
2717 }
2718 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
2719 MSGLEN ) );
2720
2721 /* Send the message to the server */
2722 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2723 MSGLEN ) == MSGLEN );
2724
2725 /* Read from the server */
2726 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
2727 == MSGLEN );
2728
2729 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
2730 memset( received, 0, MSGLEN );
2731
2732 /* Send the message to the client */
2733 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &server_context, message,
2734 MSGLEN ) == MSGLEN );
2735
2736 /* Read from the client */
2737 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &client_context, received, MSGLEN )
2738 == MSGLEN );
2739 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
2740
2741 exit:
2742 mbedtls_message_socket_close( &server_context );
2743 mbedtls_message_socket_close( &client_context );
2744}
2745/* END_CASE */
2746
2747/* BEGIN_CASE */
2748void ssl_message_mock_queue_overflow_underflow( )
2749{
2750 enum { MSGLEN = 10 };
2751 unsigned char message[MSGLEN], received[MSGLEN];
2752 mbedtls_mock_socket client, server;
2753 unsigned i;
2754 mbedtls_test_message_queue server_queue, client_queue;
2755 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05002756 mbedtls_message_socket_init( &server_context );
2757 mbedtls_message_socket_init( &client_context );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002758
2759 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 2,
2760 &server,
2761 &server_context ) == 0 );
2762
2763 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 2,
2764 &client,
2765 &client_context ) == 0 );
2766
2767 /* Fill up the buffer with structured data so that unwanted changes
2768 * can be detected */
2769 for( i = 0; i < MSGLEN; i++ )
2770 {
2771 message[i] = i & 0xFF;
2772 }
2773 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
2774 MSGLEN*2 ) );
2775
2776 /* Send three message to the server, last one with an error */
2777 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2778 MSGLEN - 1 ) == MSGLEN - 1 );
2779
2780 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2781 MSGLEN ) == MSGLEN );
2782
2783 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2784 MSGLEN )
Andrzej Kurekf46b9122020-02-07 08:19:00 -05002785 == MBEDTLS_ERR_SSL_WANT_WRITE );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002786
2787 /* Read three messages from the server, last one with an error */
2788 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received,
2789 MSGLEN - 1 ) == MSGLEN - 1 );
2790
2791 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
2792 == MSGLEN );
2793
2794 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
2795
2796 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
Andrzej Kurekf46b9122020-02-07 08:19:00 -05002797 == MBEDTLS_ERR_SSL_WANT_READ );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002798
2799 exit:
2800 mbedtls_message_socket_close( &server_context );
2801 mbedtls_message_socket_close( &client_context );
2802}
2803/* END_CASE */
2804
2805/* BEGIN_CASE */
2806void ssl_message_mock_socket_overflow( )
2807{
2808 enum { MSGLEN = 10 };
2809 unsigned char message[MSGLEN], received[MSGLEN];
2810 mbedtls_mock_socket client, server;
2811 unsigned i;
2812 mbedtls_test_message_queue server_queue, client_queue;
2813 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05002814 mbedtls_message_socket_init( &server_context );
2815 mbedtls_message_socket_init( &client_context );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002816
2817 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 2,
2818 &server,
2819 &server_context ) == 0 );
2820
2821 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 2,
2822 &client,
2823 &client_context ) == 0 );
2824
2825 /* Fill up the buffer with structured data so that unwanted changes
2826 * can be detected */
2827 for( i = 0; i < MSGLEN; i++ )
2828 {
2829 message[i] = i & 0xFF;
2830 }
2831 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
2832 MSGLEN ) );
2833
2834 /* Send two message to the server, second one with an error */
2835 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2836 MSGLEN ) == MSGLEN );
2837
2838 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2839 MSGLEN )
2840 == MBEDTLS_TEST_ERROR_SEND_FAILED );
2841
2842 /* Read the only message from the server */
2843 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
2844 == MSGLEN );
2845
2846 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
2847
2848 exit:
2849 mbedtls_message_socket_close( &server_context );
2850 mbedtls_message_socket_close( &client_context );
2851}
2852/* END_CASE */
2853
2854/* BEGIN_CASE */
2855void ssl_message_mock_truncated( )
2856{
2857 enum { MSGLEN = 10 };
2858 unsigned char message[MSGLEN], received[MSGLEN];
2859 mbedtls_mock_socket client, server;
2860 unsigned i;
2861 mbedtls_test_message_queue server_queue, client_queue;
2862 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05002863 mbedtls_message_socket_init( &server_context );
2864 mbedtls_message_socket_init( &client_context );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002865
2866 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 2,
2867 &server,
2868 &server_context ) == 0 );
2869
2870 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 2,
2871 &client,
2872 &client_context ) == 0 );
2873
2874 memset( received, 0, MSGLEN );
2875 /* Fill up the buffer with structured data so that unwanted changes
2876 * can be detected */
2877 for( i = 0; i < MSGLEN; i++ )
2878 {
2879 message[i] = i & 0xFF;
2880 }
2881 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
2882 2 * MSGLEN ) );
2883
2884 /* Send two messages to the server, the second one small enough to fit in the
2885 * receiver's buffer. */
2886 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2887 MSGLEN ) == MSGLEN );
2888 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2889 MSGLEN / 2 ) == MSGLEN / 2 );
2890 /* Read a truncated message from the server */
2891 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN/2 )
2892 == MSGLEN/2 );
2893
2894 /* Test that the first half of the message is valid, and second one isn't */
2895 TEST_ASSERT( memcmp( message, received, MSGLEN/2 ) == 0 );
2896 TEST_ASSERT( memcmp( message + MSGLEN/2, received + MSGLEN/2, MSGLEN/2 )
2897 != 0 );
2898 memset( received, 0, MSGLEN );
2899
2900 /* Read a full message from the server */
2901 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN/2 )
2902 == MSGLEN / 2 );
2903
2904 /* Test that the first half of the message is valid */
2905 TEST_ASSERT( memcmp( message, received, MSGLEN/2 ) == 0 );
2906
2907 exit:
2908 mbedtls_message_socket_close( &server_context );
2909 mbedtls_message_socket_close( &client_context );
2910}
2911/* END_CASE */
2912
2913/* BEGIN_CASE */
2914void ssl_message_mock_socket_read_error( )
2915{
2916 enum { MSGLEN = 10 };
2917 unsigned char message[MSGLEN], received[MSGLEN];
2918 mbedtls_mock_socket client, server;
2919 unsigned i;
2920 mbedtls_test_message_queue server_queue, client_queue;
2921 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05002922 mbedtls_message_socket_init( &server_context );
2923 mbedtls_message_socket_init( &client_context );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002924
2925 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 1,
2926 &server,
2927 &server_context ) == 0 );
2928
2929 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 1,
2930 &client,
2931 &client_context ) == 0 );
2932
2933 /* Fill up the buffer with structured data so that unwanted changes
2934 * can be detected */
2935 for( i = 0; i < MSGLEN; i++ )
2936 {
2937 message[i] = i & 0xFF;
2938 }
2939 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
2940 MSGLEN ) );
2941
2942 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2943 MSGLEN ) == MSGLEN );
2944
2945 /* Force a read error by disconnecting the socket by hand */
2946 server.status = 0;
2947 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
2948 == MBEDTLS_TEST_ERROR_RECV_FAILED );
2949 /* Return to a valid state */
2950 server.status = MBEDTLS_MOCK_SOCKET_CONNECTED;
2951
2952 memset( received, 0, sizeof( received ) );
2953
2954 /* Test that even though the server tried to read once disconnected, the
2955 * continuity is preserved */
2956 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
2957 == MSGLEN );
2958
2959 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
2960
2961 exit:
2962 mbedtls_message_socket_close( &server_context );
2963 mbedtls_message_socket_close( &client_context );
2964}
2965/* END_CASE */
2966
2967/* BEGIN_CASE */
2968void ssl_message_mock_interleaved_one_way( )
2969{
2970 enum { MSGLEN = 10 };
2971 unsigned char message[MSGLEN], received[MSGLEN];
2972 mbedtls_mock_socket client, server;
2973 unsigned i;
2974 mbedtls_test_message_queue server_queue, client_queue;
2975 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05002976 mbedtls_message_socket_init( &server_context );
2977 mbedtls_message_socket_init( &client_context );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002978
2979 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 3,
2980 &server,
2981 &server_context ) == 0 );
2982
2983 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 3,
2984 &client,
2985 &client_context ) == 0 );
2986
2987 /* Fill up the buffer with structured data so that unwanted changes
2988 * can be detected */
2989 for( i = 0; i < MSGLEN; i++ )
2990 {
2991 message[i] = i & 0xFF;
2992 }
2993 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
2994 MSGLEN*3 ) );
2995
2996 /* Interleaved test - [2 sends, 1 read] twice, and then two reads
2997 * (to wrap around the buffer) */
2998 for( i = 0; i < 2; i++ )
2999 {
3000 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
3001 MSGLEN ) == MSGLEN );
3002
3003 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
3004 MSGLEN ) == MSGLEN );
3005
3006 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received,
3007 MSGLEN ) == MSGLEN );
3008 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
3009 memset( received, 0, sizeof( received ) );
3010 }
3011
3012 for( i = 0; i < 2; i++ )
3013 {
3014 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received,
3015 MSGLEN ) == MSGLEN );
3016
3017 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
3018 }
3019 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
Andrzej Kurekf46b9122020-02-07 08:19:00 -05003020 == MBEDTLS_ERR_SSL_WANT_READ );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003021 exit:
3022 mbedtls_message_socket_close( &server_context );
3023 mbedtls_message_socket_close( &client_context );
3024}
3025/* END_CASE */
3026
3027/* BEGIN_CASE */
3028void ssl_message_mock_interleaved_two_ways( )
3029{
3030 enum { MSGLEN = 10 };
3031 unsigned char message[MSGLEN], received[MSGLEN];
3032 mbedtls_mock_socket client, server;
3033 unsigned i;
3034 mbedtls_test_message_queue server_queue, client_queue;
3035 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05003036 mbedtls_message_socket_init( &server_context );
3037 mbedtls_message_socket_init( &client_context );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003038
3039 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 3,
3040 &server,
3041 &server_context ) == 0 );
3042
3043 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 3,
3044 &client,
3045 &client_context ) == 0 );
3046
3047 /* Fill up the buffer with structured data so that unwanted changes
3048 * can be detected */
3049 for( i = 0; i < MSGLEN; i++ )
3050 {
3051 message[i] = i & 0xFF;
3052 }
3053 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
3054 MSGLEN*3 ) );
3055
3056 /* Interleaved test - [2 sends, 1 read] twice, both ways, and then two reads
3057 * (to wrap around the buffer) both ways. */
3058 for( i = 0; i < 2; i++ )
3059 {
3060 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
3061 MSGLEN ) == MSGLEN );
3062
3063 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
3064 MSGLEN ) == MSGLEN );
3065
3066 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &server_context, message,
3067 MSGLEN ) == MSGLEN );
3068
3069 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &server_context, message,
3070 MSGLEN ) == MSGLEN );
3071
3072 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received,
3073 MSGLEN ) == MSGLEN );
3074
3075 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
3076
3077 memset( received, 0, sizeof( received ) );
3078
3079 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &client_context, received,
3080 MSGLEN ) == MSGLEN );
3081
3082 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
3083
3084 memset( received, 0, sizeof( received ) );
3085 }
3086
3087 for( i = 0; i < 2; i++ )
3088 {
3089 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received,
3090 MSGLEN ) == MSGLEN );
3091
3092 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
3093 memset( received, 0, sizeof( received ) );
3094
3095 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &client_context, received,
3096 MSGLEN ) == MSGLEN );
3097
3098 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
3099 memset( received, 0, sizeof( received ) );
3100 }
3101
3102 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
Andrzej Kurekf46b9122020-02-07 08:19:00 -05003103 == MBEDTLS_ERR_SSL_WANT_READ );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003104
3105 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &client_context, received, MSGLEN )
Andrzej Kurekf46b9122020-02-07 08:19:00 -05003106 == MBEDTLS_ERR_SSL_WANT_READ );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003107 exit:
3108 mbedtls_message_socket_close( &server_context );
3109 mbedtls_message_socket_close( &client_context );
3110}
3111/* END_CASE */
3112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003113/* BEGIN_CASE depends_on:MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Azim Khan5fcca462018-06-29 11:05:32 +01003114void ssl_dtls_replay( data_t * prevs, data_t * new, int ret )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003115{
Azim Khand30ca132017-06-09 04:32:58 +01003116 uint32_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003117 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02003118 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003119
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02003120 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02003121 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02003122
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02003123 TEST_ASSERT( mbedtls_ssl_config_defaults( &conf,
3124 MBEDTLS_SSL_IS_CLIENT,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02003125 MBEDTLS_SSL_TRANSPORT_DATAGRAM,
3126 MBEDTLS_SSL_PRESET_DEFAULT ) == 0 );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02003127 TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003128
3129 /* Read previous record numbers */
Azim Khand30ca132017-06-09 04:32:58 +01003130 for( len = 0; len < prevs->len; len += 6 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003131 {
Azim Khand30ca132017-06-09 04:32:58 +01003132 memcpy( ssl.in_ctr + 2, prevs->x + len, 6 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003133 mbedtls_ssl_dtls_replay_update( &ssl );
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003134 }
3135
3136 /* Check new number */
Azim Khand30ca132017-06-09 04:32:58 +01003137 memcpy( ssl.in_ctr + 2, new->x, 6 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003138 TEST_ASSERT( mbedtls_ssl_dtls_replay_check( &ssl ) == ret );
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003140 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02003141 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003142}
3143/* END_CASE */
Hanno Beckerb25c0c72017-05-05 11:24:30 +01003144
3145/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
3146void ssl_set_hostname_twice( char *hostname0, char *hostname1 )
3147{
3148 mbedtls_ssl_context ssl;
3149 mbedtls_ssl_init( &ssl );
3150
3151 TEST_ASSERT( mbedtls_ssl_set_hostname( &ssl, hostname0 ) == 0 );
3152 TEST_ASSERT( mbedtls_ssl_set_hostname( &ssl, hostname1 ) == 0 );
3153
3154 mbedtls_ssl_free( &ssl );
3155}
Darryl Green11999bb2018-03-13 15:22:58 +00003156/* END_CASE */
Hanno Beckera18d1322018-01-03 14:27:32 +00003157
3158/* BEGIN_CASE */
3159void ssl_crypt_record( int cipher_type, int hash_id,
Hanno Beckerd856c822019-04-29 17:30:59 +01003160 int etm, int tag_mode, int ver,
3161 int cid0_len, int cid1_len )
Hanno Beckera18d1322018-01-03 14:27:32 +00003162{
3163 /*
3164 * Test several record encryptions and decryptions
3165 * with plenty of space before and after the data
3166 * within the record buffer.
3167 */
3168
3169 int ret;
3170 int num_records = 16;
3171 mbedtls_ssl_context ssl; /* ONLY for debugging */
3172
3173 mbedtls_ssl_transform t0, t1;
Hanno Becker81e16a32019-03-01 11:21:44 +00003174 unsigned char *buf = NULL;
Hanno Beckera18d1322018-01-03 14:27:32 +00003175 size_t const buflen = 512;
3176 mbedtls_record rec, rec_backup;
3177
3178 mbedtls_ssl_init( &ssl );
3179 mbedtls_ssl_transform_init( &t0 );
3180 mbedtls_ssl_transform_init( &t1 );
3181 TEST_ASSERT( build_transforms( &t0, &t1, cipher_type, hash_id,
Hanno Beckerd856c822019-04-29 17:30:59 +01003182 etm, tag_mode, ver,
3183 (size_t) cid0_len,
3184 (size_t) cid1_len ) == 0 );
Hanno Beckera18d1322018-01-03 14:27:32 +00003185
Hanno Becker3ee54212019-04-04 16:31:26 +01003186 TEST_ASSERT( ( buf = mbedtls_calloc( 1, buflen ) ) != NULL );
Hanno Beckera18d1322018-01-03 14:27:32 +00003187
3188 while( num_records-- > 0 )
3189 {
3190 mbedtls_ssl_transform *t_dec, *t_enc;
3191 /* Take turns in who's sending and who's receiving. */
3192 if( num_records % 3 == 0 )
3193 {
3194 t_dec = &t0;
3195 t_enc = &t1;
3196 }
3197 else
3198 {
3199 t_dec = &t1;
3200 t_enc = &t0;
3201 }
3202
3203 /*
3204 * The record header affects the transformation in two ways:
3205 * 1) It determines the AEAD additional data
3206 * 2) The record counter sometimes determines the IV.
3207 *
3208 * Apart from that, the fields don't have influence.
3209 * In particular, it is currently not the responsibility
3210 * of ssl_encrypt/decrypt_buf to check if the transform
3211 * version matches the record version, or that the
3212 * type is sensible.
3213 */
3214
3215 memset( rec.ctr, num_records, sizeof( rec.ctr ) );
3216 rec.type = 42;
3217 rec.ver[0] = num_records;
3218 rec.ver[1] = num_records;
Hanno Beckera0e20d02019-05-15 14:03:01 +01003219#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd856c822019-04-29 17:30:59 +01003220 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01003221#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera18d1322018-01-03 14:27:32 +00003222
3223 rec.buf = buf;
3224 rec.buf_len = buflen;
3225 rec.data_offset = 16;
3226 /* Make sure to vary the length to exercise different
3227 * paddings. */
3228 rec.data_len = 1 + num_records;
3229
3230 memset( rec.buf + rec.data_offset, 42, rec.data_len );
3231
3232 /* Make a copy for later comparison */
3233 rec_backup = rec;
3234
3235 /* Encrypt record */
3236 ret = mbedtls_ssl_encrypt_buf( &ssl, t_enc, &rec,
Ronald Cron351f0ee2020-06-10 12:12:18 +02003237 mbedtls_test_rnd_std_rand, NULL );
Hanno Beckera18d1322018-01-03 14:27:32 +00003238 TEST_ASSERT( ret == 0 || ret == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3239 if( ret != 0 )
3240 {
3241 continue;
3242 }
3243
Hanno Beckerb2713ab2020-05-07 14:54:22 +01003244#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3245 if( rec.cid_len != 0 )
3246 {
3247 /* DTLS 1.2 + CID hides the real content type and
3248 * uses a special CID content type in the protected
3249 * record. Double-check this. */
3250 TEST_ASSERT( rec.type == MBEDTLS_SSL_MSG_CID );
3251 }
3252#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3253
3254#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
3255 if( t_enc->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
3256 {
3257 /* TLS 1.3 hides the real content type and
3258 * always uses Application Data as the content type
3259 * for protected records. Double-check this. */
3260 TEST_ASSERT( rec.type == MBEDTLS_SSL_MSG_APPLICATION_DATA );
3261 }
3262#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
3263
Hanno Beckera18d1322018-01-03 14:27:32 +00003264 /* Decrypt record with t_dec */
Hanno Beckerd856c822019-04-29 17:30:59 +01003265 ret = mbedtls_ssl_decrypt_buf( &ssl, t_dec, &rec );
3266 TEST_ASSERT( ret == 0 );
Hanno Beckera18d1322018-01-03 14:27:32 +00003267
3268 /* Compare results */
3269 TEST_ASSERT( rec.type == rec_backup.type );
3270 TEST_ASSERT( memcmp( rec.ctr, rec_backup.ctr, 8 ) == 0 );
3271 TEST_ASSERT( rec.ver[0] == rec_backup.ver[0] );
3272 TEST_ASSERT( rec.ver[1] == rec_backup.ver[1] );
3273 TEST_ASSERT( rec.data_len == rec_backup.data_len );
3274 TEST_ASSERT( rec.data_offset == rec_backup.data_offset );
3275 TEST_ASSERT( memcmp( rec.buf + rec.data_offset,
3276 rec_backup.buf + rec_backup.data_offset,
3277 rec.data_len ) == 0 );
3278 }
3279
Hanno Becker81e16a32019-03-01 11:21:44 +00003280exit:
3281
Hanno Beckera18d1322018-01-03 14:27:32 +00003282 /* Cleanup */
3283 mbedtls_ssl_free( &ssl );
3284 mbedtls_ssl_transform_free( &t0 );
3285 mbedtls_ssl_transform_free( &t1 );
3286
Hanno Becker3ee54212019-04-04 16:31:26 +01003287 mbedtls_free( buf );
Hanno Beckera18d1322018-01-03 14:27:32 +00003288}
3289/* END_CASE */
Hanno Beckerb3268da2018-01-05 15:20:24 +00003290
3291/* BEGIN_CASE */
3292void ssl_crypt_record_small( int cipher_type, int hash_id,
Hanno Beckerd856c822019-04-29 17:30:59 +01003293 int etm, int tag_mode, int ver,
3294 int cid0_len, int cid1_len )
Hanno Beckerb3268da2018-01-05 15:20:24 +00003295{
3296 /*
3297 * Test pairs of encryption and decryption with an increasing
3298 * amount of space in the record buffer - in more detail:
3299 * 1) Try to encrypt with 0, 1, 2, ... bytes available
3300 * in front of the plaintext, and expect the encryption
3301 * to succeed starting from some offset. Always keep
3302 * enough space in the end of the buffer.
3303 * 2) Try to encrypt with 0, 1, 2, ... bytes available
3304 * at the end of the plaintext, and expect the encryption
3305 * to succeed starting from some offset. Always keep
3306 * enough space at the beginning of the buffer.
3307 * 3) Try to encrypt with 0, 1, 2, ... bytes available
3308 * both at the front and end of the plaintext,
3309 * and expect the encryption to succeed starting from
3310 * some offset.
3311 *
3312 * If encryption succeeds, check that decryption succeeds
3313 * and yields the original record.
3314 */
3315
3316 mbedtls_ssl_context ssl; /* ONLY for debugging */
3317
3318 mbedtls_ssl_transform t0, t1;
Hanno Becker81e16a32019-03-01 11:21:44 +00003319 unsigned char *buf = NULL;
Hanno Beckerd856c822019-04-29 17:30:59 +01003320 size_t const buflen = 256;
Hanno Beckerb3268da2018-01-05 15:20:24 +00003321 mbedtls_record rec, rec_backup;
3322
3323 int ret;
Hanno Beckerd856c822019-04-29 17:30:59 +01003324 int mode; /* Mode 1, 2 or 3 as explained above */
3325 size_t offset; /* Available space at beginning/end/both */
3326 size_t threshold = 96; /* Maximum offset to test against */
Hanno Beckerb3268da2018-01-05 15:20:24 +00003327
Hanno Beckerd856c822019-04-29 17:30:59 +01003328 size_t default_pre_padding = 64; /* Pre-padding to use in mode 2 */
3329 size_t default_post_padding = 128; /* Post-padding to use in mode 1 */
Hanno Beckerb3268da2018-01-05 15:20:24 +00003330
3331 int seen_success; /* Indicates if in the current mode we've
3332 * already seen a successful test. */
3333
3334 mbedtls_ssl_init( &ssl );
3335 mbedtls_ssl_transform_init( &t0 );
3336 mbedtls_ssl_transform_init( &t1 );
3337 TEST_ASSERT( build_transforms( &t0, &t1, cipher_type, hash_id,
Hanno Beckerd856c822019-04-29 17:30:59 +01003338 etm, tag_mode, ver,
3339 (size_t) cid0_len,
3340 (size_t) cid1_len ) == 0 );
Hanno Beckerb3268da2018-01-05 15:20:24 +00003341
Hanno Becker3ee54212019-04-04 16:31:26 +01003342 TEST_ASSERT( ( buf = mbedtls_calloc( 1, buflen ) ) != NULL );
Hanno Beckerb3268da2018-01-05 15:20:24 +00003343
3344 for( mode=1; mode <= 3; mode++ )
3345 {
3346 seen_success = 0;
3347 for( offset=0; offset <= threshold; offset++ )
3348 {
3349 mbedtls_ssl_transform *t_dec, *t_enc;
Hanno Becker6c87b3f2019-04-29 17:24:44 +01003350 t_dec = &t0;
3351 t_enc = &t1;
Hanno Beckerb3268da2018-01-05 15:20:24 +00003352
3353 memset( rec.ctr, offset, sizeof( rec.ctr ) );
3354 rec.type = 42;
3355 rec.ver[0] = offset;
3356 rec.ver[1] = offset;
3357 rec.buf = buf;
3358 rec.buf_len = buflen;
Hanno Beckera0e20d02019-05-15 14:03:01 +01003359#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd856c822019-04-29 17:30:59 +01003360 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01003361#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerb3268da2018-01-05 15:20:24 +00003362
3363 switch( mode )
3364 {
3365 case 1: /* Space in the beginning */
3366 rec.data_offset = offset;
3367 rec.data_len = buflen - offset - default_post_padding;
3368 break;
3369
3370 case 2: /* Space in the end */
3371 rec.data_offset = default_pre_padding;
3372 rec.data_len = buflen - default_pre_padding - offset;
3373 break;
3374
3375 case 3: /* Space in the beginning and end */
3376 rec.data_offset = offset;
3377 rec.data_len = buflen - 2 * offset;
3378 break;
3379
3380 default:
3381 TEST_ASSERT( 0 );
3382 break;
3383 }
3384
3385 memset( rec.buf + rec.data_offset, 42, rec.data_len );
3386
3387 /* Make a copy for later comparison */
3388 rec_backup = rec;
3389
3390 /* Encrypt record */
Ronald Cron6c5bd7f2020-06-10 14:08:26 +02003391 ret = mbedtls_ssl_encrypt_buf( &ssl, t_enc, &rec,
3392 mbedtls_test_rnd_std_rand, NULL );
Hanno Beckerb3268da2018-01-05 15:20:24 +00003393
3394 if( ( mode == 1 || mode == 2 ) && seen_success )
3395 {
3396 TEST_ASSERT( ret == 0 );
3397 }
3398 else
3399 {
3400 TEST_ASSERT( ret == 0 || ret == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3401 if( ret == 0 )
3402 seen_success = 1;
3403 }
3404
3405 if( ret != 0 )
3406 continue;
3407
Hanno Beckerb2713ab2020-05-07 14:54:22 +01003408#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3409 if( rec.cid_len != 0 )
3410 {
3411 /* DTLS 1.2 + CID hides the real content type and
3412 * uses a special CID content type in the protected
3413 * record. Double-check this. */
3414 TEST_ASSERT( rec.type == MBEDTLS_SSL_MSG_CID );
3415 }
3416#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3417
3418#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
3419 if( t_enc->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
3420 {
3421 /* TLS 1.3 hides the real content type and
3422 * always uses Application Data as the content type
3423 * for protected records. Double-check this. */
3424 TEST_ASSERT( rec.type == MBEDTLS_SSL_MSG_APPLICATION_DATA );
3425 }
3426#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
3427
Hanno Beckerb3268da2018-01-05 15:20:24 +00003428 /* Decrypt record with t_dec */
3429 TEST_ASSERT( mbedtls_ssl_decrypt_buf( &ssl, t_dec, &rec ) == 0 );
3430
3431 /* Compare results */
3432 TEST_ASSERT( rec.type == rec_backup.type );
3433 TEST_ASSERT( memcmp( rec.ctr, rec_backup.ctr, 8 ) == 0 );
3434 TEST_ASSERT( rec.ver[0] == rec_backup.ver[0] );
3435 TEST_ASSERT( rec.ver[1] == rec_backup.ver[1] );
3436 TEST_ASSERT( rec.data_len == rec_backup.data_len );
3437 TEST_ASSERT( rec.data_offset == rec_backup.data_offset );
3438 TEST_ASSERT( memcmp( rec.buf + rec.data_offset,
3439 rec_backup.buf + rec_backup.data_offset,
3440 rec.data_len ) == 0 );
3441 }
3442
3443 TEST_ASSERT( seen_success == 1 );
3444 }
3445
Hanno Becker81e16a32019-03-01 11:21:44 +00003446exit:
3447
Hanno Beckerb3268da2018-01-05 15:20:24 +00003448 /* Cleanup */
3449 mbedtls_ssl_free( &ssl );
3450 mbedtls_ssl_transform_free( &t0 );
3451 mbedtls_ssl_transform_free( &t1 );
3452
Hanno Becker3ee54212019-04-04 16:31:26 +01003453 mbedtls_free( buf );
Hanno Beckerb3268da2018-01-05 15:20:24 +00003454}
3455/* END_CASE */
Ron Eldor824ad7b2019-05-13 14:09:00 +03003456
3457/* BEGIN_CASE */
3458void ssl_tls_prf( int type, data_t * secret, data_t * random,
3459 char *label, data_t *result_hex_str, int exp_ret )
3460{
3461 unsigned char *output;
3462
3463 output = mbedtls_calloc( 1, result_hex_str->len );
3464 if( output == NULL )
3465 goto exit;
3466
Ron Eldor6b9b1b82019-05-15 17:04:33 +03003467#if defined(MBEDTLS_USE_PSA_CRYPTO)
3468 TEST_ASSERT( psa_crypto_init() == 0 );
3469#endif
3470
Ron Eldor824ad7b2019-05-13 14:09:00 +03003471 TEST_ASSERT( mbedtls_ssl_tls_prf( type, secret->x, secret->len,
3472 label, random->x, random->len,
3473 output, result_hex_str->len ) == exp_ret );
3474
3475 if( exp_ret == 0 )
3476 {
Ronald Cronde70b162020-06-10 11:03:08 +02003477 TEST_ASSERT( mbedtls_test_hexcmp( output, result_hex_str->x,
Ron Eldor824ad7b2019-05-13 14:09:00 +03003478 result_hex_str->len, result_hex_str->len ) == 0 );
3479 }
3480exit:
3481
3482 mbedtls_free( output );
3483}
3484/* END_CASE */
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02003485
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02003486/* BEGIN_CASE */
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02003487void ssl_serialize_session_save_load( int ticket_len, char *crt_file )
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02003488{
3489 mbedtls_ssl_session original, restored;
3490 unsigned char *buf = NULL;
3491 size_t len;
3492
3493 /*
3494 * Test that a save-load pair is the identity
3495 */
3496
3497 mbedtls_ssl_session_init( &original );
3498 mbedtls_ssl_session_init( &restored );
3499
3500 /* Prepare a dummy session to work on */
3501 TEST_ASSERT( ssl_populate_session( &original, ticket_len, crt_file ) == 0 );
3502
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02003503 /* Serialize it */
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02003504 TEST_ASSERT( mbedtls_ssl_session_save( &original, NULL, 0, &len )
3505 == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3506 TEST_ASSERT( ( buf = mbedtls_calloc( 1, len ) ) != NULL );
3507 TEST_ASSERT( mbedtls_ssl_session_save( &original, buf, len, &len )
3508 == 0 );
3509
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02003510 /* Restore session from serialized data */
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02003511 TEST_ASSERT( mbedtls_ssl_session_load( &restored, buf, len) == 0 );
3512
3513 /*
3514 * Make sure both session structures are identical
3515 */
3516#if defined(MBEDTLS_HAVE_TIME)
3517 TEST_ASSERT( original.start == restored.start );
3518#endif
3519 TEST_ASSERT( original.ciphersuite == restored.ciphersuite );
3520 TEST_ASSERT( original.compression == restored.compression );
3521 TEST_ASSERT( original.id_len == restored.id_len );
3522 TEST_ASSERT( memcmp( original.id,
3523 restored.id, sizeof( original.id ) ) == 0 );
3524 TEST_ASSERT( memcmp( original.master,
3525 restored.master, sizeof( original.master ) ) == 0 );
3526
3527#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02003528#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02003529 TEST_ASSERT( ( original.peer_cert == NULL ) ==
3530 ( restored.peer_cert == NULL ) );
3531 if( original.peer_cert != NULL )
3532 {
3533 TEST_ASSERT( original.peer_cert->raw.len ==
3534 restored.peer_cert->raw.len );
3535 TEST_ASSERT( memcmp( original.peer_cert->raw.p,
3536 restored.peer_cert->raw.p,
3537 original.peer_cert->raw.len ) == 0 );
3538 }
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02003539#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3540 TEST_ASSERT( original.peer_cert_digest_type ==
3541 restored.peer_cert_digest_type );
3542 TEST_ASSERT( original.peer_cert_digest_len ==
3543 restored.peer_cert_digest_len );
3544 TEST_ASSERT( ( original.peer_cert_digest == NULL ) ==
3545 ( restored.peer_cert_digest == NULL ) );
3546 if( original.peer_cert_digest != NULL )
3547 {
3548 TEST_ASSERT( memcmp( original.peer_cert_digest,
3549 restored.peer_cert_digest,
3550 original.peer_cert_digest_len ) == 0 );
3551 }
3552#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3553#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02003554 TEST_ASSERT( original.verify_result == restored.verify_result );
3555
3556#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
3557 TEST_ASSERT( original.ticket_len == restored.ticket_len );
3558 if( original.ticket_len != 0 )
3559 {
3560 TEST_ASSERT( original.ticket != NULL );
3561 TEST_ASSERT( restored.ticket != NULL );
3562 TEST_ASSERT( memcmp( original.ticket,
3563 restored.ticket, original.ticket_len ) == 0 );
3564 }
3565 TEST_ASSERT( original.ticket_lifetime == restored.ticket_lifetime );
3566#endif
3567
3568#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3569 TEST_ASSERT( original.mfl_code == restored.mfl_code );
3570#endif
3571
3572#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
3573 TEST_ASSERT( original.trunc_hmac == restored.trunc_hmac );
3574#endif
3575
3576#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
3577 TEST_ASSERT( original.encrypt_then_mac == restored.encrypt_then_mac );
3578#endif
3579
3580exit:
3581 mbedtls_ssl_session_free( &original );
3582 mbedtls_ssl_session_free( &restored );
3583 mbedtls_free( buf );
3584}
3585/* END_CASE */
3586
Manuel Pégourié-Gonnardaa755832019-06-03 10:53:47 +02003587/* BEGIN_CASE */
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02003588void ssl_serialize_session_load_save( int ticket_len, char *crt_file )
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02003589{
3590 mbedtls_ssl_session session;
3591 unsigned char *buf1 = NULL, *buf2 = NULL;
3592 size_t len0, len1, len2;
3593
3594 /*
3595 * Test that a load-save pair is the identity
3596 */
3597
3598 mbedtls_ssl_session_init( &session );
3599
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02003600 /* Prepare a dummy session to work on */
Manuel Pégourié-Gonnard6b840702019-05-24 09:40:17 +02003601 TEST_ASSERT( ssl_populate_session( &session, ticket_len, crt_file ) == 0 );
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02003602
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02003603 /* Get desired buffer size for serializing */
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02003604 TEST_ASSERT( mbedtls_ssl_session_save( &session, NULL, 0, &len0 )
3605 == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3606
3607 /* Allocate first buffer */
3608 buf1 = mbedtls_calloc( 1, len0 );
3609 TEST_ASSERT( buf1 != NULL );
3610
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02003611 /* Serialize to buffer and free live session */
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02003612 TEST_ASSERT( mbedtls_ssl_session_save( &session, buf1, len0, &len1 )
3613 == 0 );
3614 TEST_ASSERT( len0 == len1 );
3615 mbedtls_ssl_session_free( &session );
3616
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02003617 /* Restore session from serialized data */
Manuel Pégourié-Gonnard220403b2019-05-24 09:54:21 +02003618 TEST_ASSERT( mbedtls_ssl_session_load( &session, buf1, len1 ) == 0 );
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02003619
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02003620 /* Allocate second buffer and serialize to it */
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02003621 buf2 = mbedtls_calloc( 1, len0 );
Manuel Pégourié-Gonnardb4079902019-05-24 09:52:10 +02003622 TEST_ASSERT( buf2 != NULL );
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02003623 TEST_ASSERT( mbedtls_ssl_session_save( &session, buf2, len0, &len2 )
3624 == 0 );
3625
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02003626 /* Make sure both serialized versions are identical */
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02003627 TEST_ASSERT( len1 == len2 );
3628 TEST_ASSERT( memcmp( buf1, buf2, len1 ) == 0 );
3629
3630exit:
3631 mbedtls_ssl_session_free( &session );
3632 mbedtls_free( buf1 );
3633 mbedtls_free( buf2 );
3634}
3635/* END_CASE */
Manuel Pégourié-Gonnardf5fa0aa2019-05-23 10:38:11 +02003636
3637/* BEGIN_CASE */
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02003638void ssl_serialize_session_save_buf_size( int ticket_len, char *crt_file )
Manuel Pégourié-Gonnardf5fa0aa2019-05-23 10:38:11 +02003639{
3640 mbedtls_ssl_session session;
3641 unsigned char *buf = NULL;
3642 size_t good_len, bad_len, test_len;
3643
3644 /*
3645 * Test that session_save() fails cleanly on small buffers
3646 */
3647
3648 mbedtls_ssl_session_init( &session );
3649
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02003650 /* Prepare dummy session and get serialized size */
Manuel Pégourié-Gonnard6b840702019-05-24 09:40:17 +02003651 TEST_ASSERT( ssl_populate_session( &session, ticket_len, crt_file ) == 0 );
Manuel Pégourié-Gonnardf5fa0aa2019-05-23 10:38:11 +02003652 TEST_ASSERT( mbedtls_ssl_session_save( &session, NULL, 0, &good_len )
3653 == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3654
3655 /* Try all possible bad lengths */
3656 for( bad_len = 1; bad_len < good_len; bad_len++ )
3657 {
3658 /* Allocate exact size so that asan/valgrind can detect any overwrite */
3659 mbedtls_free( buf );
3660 TEST_ASSERT( ( buf = mbedtls_calloc( 1, bad_len ) ) != NULL );
3661 TEST_ASSERT( mbedtls_ssl_session_save( &session, buf, bad_len,
3662 &test_len )
3663 == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3664 TEST_ASSERT( test_len == good_len );
3665 }
3666
3667exit:
3668 mbedtls_ssl_session_free( &session );
3669 mbedtls_free( buf );
3670}
3671/* END_CASE */
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003672
3673/* BEGIN_CASE */
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02003674void ssl_serialize_session_load_buf_size( int ticket_len, char *crt_file )
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003675{
3676 mbedtls_ssl_session session;
3677 unsigned char *good_buf = NULL, *bad_buf = NULL;
3678 size_t good_len, bad_len;
3679
3680 /*
3681 * Test that session_load() fails cleanly on small buffers
3682 */
3683
3684 mbedtls_ssl_session_init( &session );
3685
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02003686 /* Prepare serialized session data */
Manuel Pégourié-Gonnard6b840702019-05-24 09:40:17 +02003687 TEST_ASSERT( ssl_populate_session( &session, ticket_len, crt_file ) == 0 );
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003688 TEST_ASSERT( mbedtls_ssl_session_save( &session, NULL, 0, &good_len )
3689 == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3690 TEST_ASSERT( ( good_buf = mbedtls_calloc( 1, good_len ) ) != NULL );
3691 TEST_ASSERT( mbedtls_ssl_session_save( &session, good_buf, good_len,
3692 &good_len ) == 0 );
3693 mbedtls_ssl_session_free( &session );
3694
3695 /* Try all possible bad lengths */
3696 for( bad_len = 0; bad_len < good_len; bad_len++ )
3697 {
3698 /* Allocate exact size so that asan/valgrind can detect any overread */
3699 mbedtls_free( bad_buf );
3700 bad_buf = mbedtls_calloc( 1, bad_len ? bad_len : 1 );
3701 TEST_ASSERT( bad_buf != NULL );
3702 memcpy( bad_buf, good_buf, bad_len );
3703
3704 TEST_ASSERT( mbedtls_ssl_session_load( &session, bad_buf, bad_len )
3705 == MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3706 }
3707
3708exit:
3709 mbedtls_ssl_session_free( &session );
3710 mbedtls_free( good_buf );
3711 mbedtls_free( bad_buf );
3712}
3713/* END_CASE */
Hanno Becker861d0bb2019-05-21 16:39:30 +01003714
Hanno Becker363b6462019-05-29 12:44:28 +01003715/* BEGIN_CASE */
3716void ssl_session_serialize_version_check( int corrupt_major,
Hanno Becker861d0bb2019-05-21 16:39:30 +01003717 int corrupt_minor,
3718 int corrupt_patch,
3719 int corrupt_config )
3720{
Hanno Becker363b6462019-05-29 12:44:28 +01003721 unsigned char serialized_session[ 2048 ];
3722 size_t serialized_session_len;
Hanno Beckerfe1275e2019-05-29 12:45:21 +01003723 unsigned cur_byte;
Hanno Becker861d0bb2019-05-21 16:39:30 +01003724 mbedtls_ssl_session session;
Hanno Beckerfe1275e2019-05-29 12:45:21 +01003725 uint8_t should_corrupt_byte[] = { corrupt_major == 1,
3726 corrupt_minor == 1,
3727 corrupt_patch == 1,
3728 corrupt_config == 1,
3729 corrupt_config == 1 };
3730
Hanno Becker861d0bb2019-05-21 16:39:30 +01003731 mbedtls_ssl_session_init( &session );
3732
Hanno Beckerfe1275e2019-05-29 12:45:21 +01003733 /* Infer length of serialized session. */
Hanno Becker861d0bb2019-05-21 16:39:30 +01003734 TEST_ASSERT( mbedtls_ssl_session_save( &session,
Hanno Becker363b6462019-05-29 12:44:28 +01003735 serialized_session,
3736 sizeof( serialized_session ),
3737 &serialized_session_len ) == 0 );
Hanno Becker861d0bb2019-05-21 16:39:30 +01003738
Hanno Beckerfe1275e2019-05-29 12:45:21 +01003739 mbedtls_ssl_session_free( &session );
Hanno Becker861d0bb2019-05-21 16:39:30 +01003740
Hanno Beckerfe1275e2019-05-29 12:45:21 +01003741 /* Without any modification, we should be able to successfully
Hanno Becker363b6462019-05-29 12:44:28 +01003742 * de-serialize the session - double-check that. */
Hanno Becker861d0bb2019-05-21 16:39:30 +01003743 TEST_ASSERT( mbedtls_ssl_session_load( &session,
Hanno Becker363b6462019-05-29 12:44:28 +01003744 serialized_session,
3745 serialized_session_len ) == 0 );
Hanno Becker861d0bb2019-05-21 16:39:30 +01003746 mbedtls_ssl_session_free( &session );
3747
Hanno Beckerfe1275e2019-05-29 12:45:21 +01003748 /* Go through the bytes in the serialized session header and
3749 * corrupt them bit-by-bit. */
3750 for( cur_byte = 0; cur_byte < sizeof( should_corrupt_byte ); cur_byte++ )
Hanno Becker861d0bb2019-05-21 16:39:30 +01003751 {
Hanno Beckerfe1275e2019-05-29 12:45:21 +01003752 int cur_bit;
3753 unsigned char * const byte = &serialized_session[ cur_byte ];
3754
3755 if( should_corrupt_byte[ cur_byte ] == 0 )
3756 continue;
3757
3758 for( cur_bit = 0; cur_bit < CHAR_BIT; cur_bit++ )
3759 {
3760 unsigned char const corrupted_bit = 0x1u << cur_bit;
3761 /* Modify a single bit in the serialized session. */
3762 *byte ^= corrupted_bit;
3763
3764 /* Attempt to deserialize */
3765 TEST_ASSERT( mbedtls_ssl_session_load( &session,
3766 serialized_session,
3767 serialized_session_len ) ==
Hanno Beckerf9b33032019-06-03 12:58:39 +01003768 MBEDTLS_ERR_SSL_VERSION_MISMATCH );
Hanno Beckerfe1275e2019-05-29 12:45:21 +01003769
3770 /* Undo the change */
3771 *byte ^= corrupted_bit;
3772 }
Hanno Becker861d0bb2019-05-21 16:39:30 +01003773 }
3774
Hanno Becker861d0bb2019-05-21 16:39:30 +01003775}
3776/* END_CASE */
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01003777
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02003778/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_ENTROPY_C:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01003779void mbedtls_endpoint_sanity( int endpoint_type )
3780{
3781 enum { BUFFSIZE = 1024 };
3782 mbedtls_endpoint ep;
3783 int ret = -1;
3784
Andrzej Kurek15daf502020-02-12 09:17:52 -05003785 ret = mbedtls_endpoint_init( NULL, endpoint_type, MBEDTLS_PK_RSA,
3786 NULL, NULL, NULL );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01003787 TEST_ASSERT( MBEDTLS_ERR_SSL_BAD_INPUT_DATA == ret );
3788
Andrzej Kurekb2980742020-02-02 19:25:26 -05003789 ret = mbedtls_endpoint_certificate_init( NULL, MBEDTLS_PK_RSA );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01003790 TEST_ASSERT( MBEDTLS_ERR_SSL_BAD_INPUT_DATA == ret );
3791
Andrzej Kurek15daf502020-02-12 09:17:52 -05003792 ret = mbedtls_endpoint_init( &ep, endpoint_type, MBEDTLS_PK_RSA,
3793 NULL, NULL, NULL );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01003794 TEST_ASSERT( ret == 0 );
3795
3796exit:
Andrzej Kurek15daf502020-02-12 09:17:52 -05003797 mbedtls_endpoint_free( &ep, NULL );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01003798}
3799/* END_CASE */
3800
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02003801/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_ENTROPY_C:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01003802void move_handshake_to_state(int endpoint_type, int state, int need_pass)
3803{
3804 enum { BUFFSIZE = 1024 };
3805 mbedtls_endpoint base_ep, second_ep;
3806 int ret = -1;
3807
Andrzej Kurek15daf502020-02-12 09:17:52 -05003808 ret = mbedtls_endpoint_init( &base_ep, endpoint_type, MBEDTLS_PK_RSA,
3809 NULL, NULL, NULL );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01003810 TEST_ASSERT( ret == 0 );
3811
3812 ret = mbedtls_endpoint_init( &second_ep,
3813 ( endpoint_type == MBEDTLS_SSL_IS_SERVER ) ?
Andrzej Kurekb2980742020-02-02 19:25:26 -05003814 MBEDTLS_SSL_IS_CLIENT : MBEDTLS_SSL_IS_SERVER,
Andrzej Kurek15daf502020-02-12 09:17:52 -05003815 MBEDTLS_PK_RSA, NULL, NULL, NULL );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01003816 TEST_ASSERT( ret == 0 );
3817
3818 ret = mbedtls_mock_socket_connect( &(base_ep.socket),
3819 &(second_ep.socket),
3820 BUFFSIZE );
3821 TEST_ASSERT( ret == 0 );
3822
3823 ret = mbedtls_move_handshake_to_state( &(base_ep.ssl),
3824 &(second_ep.ssl),
3825 state );
3826 if( need_pass )
3827 {
3828 TEST_ASSERT( ret == 0 );
3829 TEST_ASSERT( base_ep.ssl.state == state );
3830 }
3831 else
3832 {
3833 TEST_ASSERT( ret != 0 );
3834 TEST_ASSERT( base_ep.ssl.state != state );
3835 }
3836
3837exit:
Andrzej Kurek15daf502020-02-12 09:17:52 -05003838 mbedtls_endpoint_free( &base_ep, NULL );
3839 mbedtls_endpoint_free( &second_ep, NULL );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01003840}
3841/* END_CASE */
Andrzej Kurekf40daa32020-02-04 09:00:01 -05003842
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02003843/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
Paul Elliottc8570442020-04-15 17:00:50 +01003844void handshake_version( int dtls, int client_min_version, int client_max_version,
3845 int server_min_version, int server_max_version,
3846 int expected_negotiated_version )
Andrzej Kurekf40daa32020-02-04 09:00:01 -05003847{
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003848 handshake_test_options options;
3849 init_handshake_options( &options );
Andrzej Kurekda2b6782020-02-12 07:56:36 -05003850
Paul Elliottc8570442020-04-15 17:00:50 +01003851 options.client_min_version = client_min_version;
3852 options.client_max_version = client_max_version;
3853 options.server_min_version = server_min_version;
3854 options.server_max_version = server_max_version;
3855
3856 options.expected_negotiated_version = expected_negotiated_version;
3857
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003858 options.dtls = dtls;
Piotr Nowicki438bf3b2020-03-10 12:59:10 +01003859 /* By default, SSLv3.0 and TLSv1.0 use 1/n-1 splitting when sending data, so
3860 * the number of fragments will be twice as big. */
Paul Elliottc8570442020-04-15 17:00:50 +01003861 if( expected_negotiated_version == MBEDTLS_SSL_MINOR_VERSION_0 ||
3862 expected_negotiated_version == MBEDTLS_SSL_MINOR_VERSION_1 )
Andrzej Kurek941962e2020-02-07 09:20:32 -05003863 {
Piotr Nowicki438bf3b2020-03-10 12:59:10 +01003864 options.expected_cli_fragments = 2;
3865 options.expected_srv_fragments = 2;
Andrzej Kurek941962e2020-02-07 09:20:32 -05003866 }
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003867 perform_handshake( &options );
Andrzej Kurekf40daa32020-02-04 09:00:01 -05003868
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003869 /* The goto below is used to avoid an "unused label" warning.*/
3870 goto exit;
3871}
3872/* END_CASE */
Andrzej Kurek9e9efdc2020-02-26 05:25:23 -05003873
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02003874/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003875void handshake_psk_cipher( char* cipher, int pk_alg, data_t *psk_str, int dtls )
3876{
3877 handshake_test_options options;
3878 init_handshake_options( &options );
Andrzej Kurekf40daa32020-02-04 09:00:01 -05003879
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003880 options.cipher = cipher;
3881 options.dtls = dtls;
3882 options.psk_str = psk_str;
3883 options.pk_alg = pk_alg;
Andrzej Kurekcc5169c2020-02-04 09:04:56 -05003884
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003885 perform_handshake( &options );
Andrzej Kurek316da1f2020-02-26 09:03:47 -05003886
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003887 /* The goto below is used to avoid an "unused label" warning.*/
3888 goto exit;
3889}
3890/* END_CASE */
Andrzej Kurek316da1f2020-02-26 09:03:47 -05003891
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02003892/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003893void handshake_cipher( char* cipher, int pk_alg, int dtls )
3894{
3895 test_handshake_psk_cipher( cipher, pk_alg, NULL, dtls );
Andrzej Kurekf40daa32020-02-04 09:00:01 -05003896
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003897 /* The goto below is used to avoid an "unused label" warning.*/
3898 goto exit;
3899}
3900/* END_CASE */
Andrzej Kurekf40daa32020-02-04 09:00:01 -05003901
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02003902/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003903void app_data( int mfl, int cli_msg_len, int srv_msg_len,
3904 int expected_cli_fragments,
3905 int expected_srv_fragments, int dtls )
3906{
3907 handshake_test_options options;
3908 init_handshake_options( &options );
Andrzej Kurekda2b6782020-02-12 07:56:36 -05003909
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003910 options.mfl = mfl;
3911 options.cli_msg_len = cli_msg_len;
3912 options.srv_msg_len = srv_msg_len;
3913 options.expected_cli_fragments = expected_cli_fragments;
3914 options.expected_srv_fragments = expected_srv_fragments;
3915 options.dtls = dtls;
Andrzej Kurekda2b6782020-02-12 07:56:36 -05003916
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003917 perform_handshake( &options );
3918 /* The goto below is used to avoid an "unused label" warning.*/
3919 goto exit;
3920}
3921/* END_CASE */
Andrzej Kurekda2b6782020-02-12 07:56:36 -05003922
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02003923/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003924void app_data_tls( int mfl, int cli_msg_len, int srv_msg_len,
3925 int expected_cli_fragments,
3926 int expected_srv_fragments )
3927{
3928 test_app_data( mfl, cli_msg_len, srv_msg_len, expected_cli_fragments,
3929 expected_srv_fragments, 0 );
3930 /* The goto below is used to avoid an "unused label" warning.*/
3931 goto exit;
3932}
3933/* END_CASE */
Andrzej Kurekda2b6782020-02-12 07:56:36 -05003934
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02003935/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_SSL_PROTO_DTLS:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003936void app_data_dtls( int mfl, int cli_msg_len, int srv_msg_len,
3937 int expected_cli_fragments,
3938 int expected_srv_fragments )
3939{
3940 test_app_data( mfl, cli_msg_len, srv_msg_len, expected_cli_fragments,
3941 expected_srv_fragments, 1 );
3942 /* The goto below is used to avoid an "unused label" warning.*/
3943 goto exit;
3944}
3945/* END_CASE */
Andrzej Kurekda2b6782020-02-12 07:56:36 -05003946
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02003947/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_SSL_PROTO_DTLS:MBEDTLS_SSL_RENEGOTIATION:MBEDTLS_SSL_CONTEXT_SERIALIZATION:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003948void handshake_serialization( )
3949{
3950 handshake_test_options options;
3951 init_handshake_options( &options );
Andrzej Kurekda2b6782020-02-12 07:56:36 -05003952
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003953 options.serialize = 1;
3954 options.dtls = 1;
3955 perform_handshake( &options );
3956 /* The goto below is used to avoid an "unused label" warning.*/
3957 goto exit;
3958}
3959/* END_CASE */
Andrzej Kurekda2b6782020-02-12 07:56:36 -05003960
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02003961/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_DEBUG_C:MBEDTLS_SSL_MAX_FRAGMENT_LENGTH:MBEDTLS_CIPHER_MODE_CBC:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
Piotr Nowickibde7ee82020-02-21 10:59:50 +01003962void handshake_fragmentation( int mfl, int expected_srv_hs_fragmentation, int expected_cli_hs_fragmentation)
3963{
3964 handshake_test_options options;
3965 log_pattern srv_pattern, cli_pattern;
3966
3967 srv_pattern.pattern = cli_pattern.pattern = "found fragmented DTLS handshake";
3968 srv_pattern.counter = 0;
3969 cli_pattern.counter = 0;
3970
3971 init_handshake_options( &options );
3972 options.dtls = 1;
3973 options.mfl = mfl;
Darryl Greenaad82f92019-12-02 10:53:11 +00003974 /* Set cipher to one using CBC so that record splitting can be tested */
3975 options.cipher = "TLS-DHE-RSA-WITH-AES-256-CBC-SHA256";
Piotr Nowickibde7ee82020-02-21 10:59:50 +01003976 options.srv_auth_mode = MBEDTLS_SSL_VERIFY_REQUIRED;
3977 options.srv_log_obj = &srv_pattern;
3978 options.cli_log_obj = &cli_pattern;
3979 options.srv_log_fun = log_analyzer;
3980 options.cli_log_fun = log_analyzer;
3981
3982 perform_handshake( &options );
3983
3984 /* Test if the server received a fragmented handshake */
3985 if( expected_srv_hs_fragmentation )
3986 {
3987 TEST_ASSERT( srv_pattern.counter >= 1 );
3988 }
3989 /* Test if the client received a fragmented handshake */
3990 if( expected_cli_hs_fragmentation )
3991 {
3992 TEST_ASSERT( cli_pattern.counter >= 1 );
3993 }
3994}
3995/* END_CASE */
3996
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02003997/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_SSL_PROTO_DTLS:MBEDTLS_SSL_RENEGOTIATION:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003998void renegotiation( int legacy_renegotiation )
3999{
4000 handshake_test_options options;
4001 init_handshake_options( &options );
Andrzej Kurekda2b6782020-02-12 07:56:36 -05004002
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004003 options.renegotiate = 1;
4004 options.legacy_renegotiation = legacy_renegotiation;
4005 options.dtls = 1;
Andrzej Kurek316da1f2020-02-26 09:03:47 -05004006
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004007 perform_handshake( &options );
4008 /* The goto below is used to avoid an "unused label" warning.*/
4009 goto exit;
Andrzej Kurekf40daa32020-02-04 09:00:01 -05004010}
4011/* END_CASE */
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004012
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02004013/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004014void resize_buffers( int mfl, int renegotiation, int legacy_renegotiation,
Andrzej Kurek8ea68722020-04-03 06:40:47 -04004015 int serialize, int dtls, char *cipher )
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004016{
4017 handshake_test_options options;
4018 init_handshake_options( &options );
4019
4020 options.mfl = mfl;
Andrzej Kurek8ea68722020-04-03 06:40:47 -04004021 options.cipher = cipher;
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004022 options.renegotiate = renegotiation;
4023 options.legacy_renegotiation = legacy_renegotiation;
4024 options.serialize = serialize;
4025 options.dtls = dtls;
4026 options.resize_buffers = 1;
4027
4028 perform_handshake( &options );
4029 /* The goto below is used to avoid an "unused label" warning.*/
4030 goto exit;
4031}
4032/* END_CASE */
4033
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02004034/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH:MBEDTLS_SSL_CONTEXT_SERIALIZATION:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_SSL_PROTO_DTLS:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004035void resize_buffers_serialize_mfl( int mfl )
4036{
Andrzej Kurek8ea68722020-04-03 06:40:47 -04004037 test_resize_buffers( mfl, 0, MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION, 1, 1,
4038 (char *) "" );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004039
4040 /* The goto below is used to avoid an "unused label" warning.*/
4041 goto exit;
4042}
4043/* END_CASE */
4044
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02004045/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH:MBEDTLS_SSL_RENEGOTIATION:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
Andrzej Kurek8ea68722020-04-03 06:40:47 -04004046void resize_buffers_renegotiate_mfl( int mfl, int legacy_renegotiation,
4047 char *cipher )
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004048{
Andrzej Kurek8ea68722020-04-03 06:40:47 -04004049 test_resize_buffers( mfl, 1, legacy_renegotiation, 0, 1, cipher );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004050
4051 /* The goto below is used to avoid an "unused label" warning.*/
4052 goto exit;
4053}
4054/* END_CASE */
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02004055
Manuel Pégourié-Gonnard65a6fa32020-07-09 09:52:17 +02004056/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC:MBEDTLS_AES_C:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_TEST_HOOKS */
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02004057void ssl_cf_hmac( int hash )
4058{
4059 /*
4060 * Test the function mbedtls_ssl_cf_hmac() against a reference
4061 * implementation.
4062 *
4063 * Note: the dependency is actually on TLS 1.0-1.2 and (AES or ARIA or
4064 * Camellia or DES), but since the test framework doesn't support
4065 * alternation in dependencies, just depend on the most common.
4066 */
4067 mbedtls_md_context_t ctx, ref_ctx;
4068 const mbedtls_md_info_t *md_info;
4069 size_t out_len, block_size;
4070 size_t min_in_len, in_len, max_in_len, i;
4071 /* TLS additional data is 13 bytes (hence the "lucky 13" name) */
4072 unsigned char add_data[13];
4073 unsigned char ref_out[MBEDTLS_MD_MAX_SIZE];
4074 unsigned char *data = NULL;
4075 unsigned char *out = NULL;
4076 unsigned char rec_num = 0;
4077
4078 mbedtls_md_init( &ctx );
4079 mbedtls_md_init( &ref_ctx );
4080
4081 md_info = mbedtls_md_info_from_type( hash );
4082 TEST_ASSERT( md_info != NULL );
4083 out_len = mbedtls_md_get_size( md_info );
4084 TEST_ASSERT( out_len != 0 );
4085 block_size = hash == MBEDTLS_MD_SHA384 ? 128 : 64;
4086
4087 /* Use allocated out buffer to catch overwrites */
4088 ASSERT_ALLOC( out, out_len );
4089
4090 /* Set up contexts with the given hash and a dummy key */
4091 TEST_EQUAL( 0, mbedtls_md_setup( &ctx, md_info, 1 ) );
4092 TEST_EQUAL( 0, mbedtls_md_setup( &ref_ctx, md_info, 1 ) );
4093 memset( ref_out, 42, sizeof( ref_out ) );
4094 TEST_EQUAL( 0, mbedtls_md_hmac_starts( &ctx, ref_out, out_len ) );
4095 TEST_EQUAL( 0, mbedtls_md_hmac_starts( &ref_ctx, ref_out, out_len ) );
4096 memset( ref_out, 0, sizeof( ref_out ) );
4097
4098 /*
4099 * Test all possible lengths up to a point. The difference between
4100 * max_in_len and min_in_len is at most 255, and make sure they both vary
4101 * by at least one block size.
4102 */
4103 for( max_in_len = 0; max_in_len <= 255 + block_size; max_in_len++ )
4104 {
4105 /* Use allocated in buffer to catch overreads */
4106 ASSERT_ALLOC( data, max_in_len != 0 ? max_in_len : 1 );
4107
4108 min_in_len = max_in_len > 255 ? max_in_len - 255 : 0;
4109 for( in_len = min_in_len; in_len <= max_in_len; in_len++ )
4110 {
4111 /* Set up dummy data and add_data */
4112 rec_num++;
4113 memset( add_data, rec_num, sizeof( add_data ) );
4114 for( i = 0; i < in_len; i++ )
4115 data[i] = ( i & 0xff ) ^ rec_num;
4116
4117 /* Get the function's result */
4118 TEST_EQUAL( 0, mbedtls_ssl_cf_hmac( &ctx, add_data, sizeof( add_data ),
4119 data, in_len,
4120 min_in_len, max_in_len,
4121 out ) );
4122
4123 /* Compute the reference result */
4124 TEST_EQUAL( 0, mbedtls_md_hmac_update( &ref_ctx, add_data,
4125 sizeof( add_data ) ) );
4126 TEST_EQUAL( 0, mbedtls_md_hmac_update( &ref_ctx, data, in_len ) );
4127 TEST_EQUAL( 0, mbedtls_md_hmac_finish( &ref_ctx, ref_out ) );
4128 TEST_EQUAL( 0, mbedtls_md_hmac_reset( &ref_ctx ) );
4129
4130 /* Compare */
4131 ASSERT_COMPARE( out, out_len, ref_out, out_len );
4132 }
4133
4134 mbedtls_free( data );
4135 data = NULL;
4136 }
4137
4138exit:
4139 mbedtls_md_free( &ref_ctx );
4140 mbedtls_md_free( &ctx );
4141
4142 mbedtls_free( data );
4143 mbedtls_free( out );
4144}
4145/* END_CASE */