blob: 639e8c370a111baaaf33608bc1b3eef8d0f20f69 [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
10typedef struct log_pattern
11{
12 const char *pattern;
13 size_t counter;
14} log_pattern;
15
Piotr Nowicki438bf3b2020-03-10 12:59:10 +010016/*
17 * This function can be passed to mbedtls to receive output logs from it. In
Piotr Nowickibde7ee82020-02-21 10:59:50 +010018 * this case, it will count the instances of a log_pattern in the received
19 * logged messages.
20 */
21void log_analyzer( void *ctx, int level,
22 const char *file, int line,
23 const char *str )
24{
25 log_pattern *p = (log_pattern *) ctx;
26
27 (void) level;
28 (void) line;
29 (void) file;
30
31 if( NULL != p &&
32 NULL != p->pattern &&
33 NULL != strstr( str, p->pattern ) )
34 {
35 p->counter++;
36 }
37}
Janos Follath6264e662019-11-26 11:11:15 +000038
Andrzej Kurek8a6ff152020-02-26 09:10:14 -050039typedef struct handshake_test_options
40{
41 const char *cipher;
42 int version;
43 int pk_alg;
44 data_t *psk_str;
45 int dtls;
Piotr Nowickibde7ee82020-02-21 10:59:50 +010046 int srv_auth_mode;
Andrzej Kurek8a6ff152020-02-26 09:10:14 -050047 int serialize;
48 int mfl;
49 int cli_msg_len;
50 int srv_msg_len;
51 int expected_cli_fragments;
52 int expected_srv_fragments;
53 int renegotiate;
54 int legacy_renegotiation;
Piotr Nowickibde7ee82020-02-21 10:59:50 +010055 void *srv_log_obj;
56 void *cli_log_obj;
57 void (*srv_log_fun)(void *, int, const char *, int, const char *);
58 void (*cli_log_fun)(void *, int, const char *, int, const char *);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -050059 int resize_buffers;
Andrzej Kurek8a6ff152020-02-26 09:10:14 -050060} handshake_test_options;
61
62void init_handshake_options( handshake_test_options *opts )
63{
64 opts->cipher = "";
65 opts->version = MBEDTLS_SSL_MINOR_VERSION_3;
66 opts->pk_alg = MBEDTLS_PK_RSA;
67 opts->psk_str = NULL;
68 opts->dtls = 0;
Piotr Nowickibde7ee82020-02-21 10:59:50 +010069 opts->srv_auth_mode = MBEDTLS_SSL_VERIFY_NONE;
Andrzej Kurek8a6ff152020-02-26 09:10:14 -050070 opts->serialize = 0;
71 opts->mfl = MBEDTLS_SSL_MAX_FRAG_LEN_NONE;
72 opts->cli_msg_len = 100;
73 opts->srv_msg_len = 100;
74 opts->expected_cli_fragments = 1;
75 opts->expected_srv_fragments = 1;
76 opts->renegotiate = 0;
77 opts->legacy_renegotiation = MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION;
Piotr Nowickibde7ee82020-02-21 10:59:50 +010078 opts->srv_log_obj = NULL;
79 opts->srv_log_obj = NULL;
80 opts->srv_log_fun = NULL;
81 opts->cli_log_fun = NULL;
Andrzej Kurek0afa2a12020-03-03 10:39:58 -050082 opts->resize_buffers = 1;
Andrzej Kurek8a6ff152020-02-26 09:10:14 -050083}
Janos Follath6264e662019-11-26 11:11:15 +000084/*
85 * Buffer structure for custom I/O callbacks.
86 */
87
88typedef struct mbedtls_test_buffer
89{
90 size_t start;
91 size_t content_length;
92 size_t capacity;
93 unsigned char *buffer;
94} mbedtls_test_buffer;
95
96/*
97 * Initialises \p buf. After calling this function it is safe to call
98 * `mbedtls_test_buffer_free()` on \p buf.
99 */
100void mbedtls_test_buffer_init( mbedtls_test_buffer *buf )
101{
102 memset( buf, 0, sizeof( *buf ) );
103}
104
105/*
106 * Sets up \p buf. After calling this function it is safe to call
107 * `mbedtls_test_buffer_put()` and `mbedtls_test_buffer_get()` on \p buf.
108 */
109int mbedtls_test_buffer_setup( mbedtls_test_buffer *buf, size_t capacity )
110{
111 buf->buffer = (unsigned char*) mbedtls_calloc( capacity,
112 sizeof(unsigned char) );
113 if( NULL == buf->buffer )
114 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
115 buf->capacity = capacity;
116
117 return 0;
118}
119
120void mbedtls_test_buffer_free( mbedtls_test_buffer *buf )
121{
122 if( buf->buffer != NULL )
123 mbedtls_free( buf->buffer );
124
125 memset( buf, 0, sizeof( *buf ) );
126}
127
128/*
129 * Puts \p input_len bytes from the \p input buffer into the ring buffer \p buf.
130 *
131 * \p buf must have been initialized and set up by calling
132 * `mbedtls_test_buffer_init()` and `mbedtls_test_buffer_setup()`.
133 *
134 * \retval \p input_len, if the data fits.
135 * \retval 0 <= value < \p input_len, if the data does not fit.
136 * \retval -1, if \p buf is NULL, it hasn't been set up or \p input_len is not
137 * zero and \p input is NULL.
138 */
139int mbedtls_test_buffer_put( mbedtls_test_buffer *buf,
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100140 const unsigned char *input, size_t input_len )
Janos Follath6264e662019-11-26 11:11:15 +0000141{
142 size_t overflow = 0;
143
144 if( ( buf == NULL ) || ( buf->buffer == NULL ) )
145 return -1;
146
147 /* Reduce input_len to a number that fits in the buffer. */
148 if ( ( buf->content_length + input_len ) > buf->capacity )
149 {
150 input_len = buf->capacity - buf->content_length;
151 }
152
153 if( input == NULL )
154 {
155 return ( input_len == 0 ) ? 0 : -1;
156 }
157
Piotr Nowickifb437d72020-01-13 16:59:12 +0100158 /* Check if the buffer has not come full circle and free space is not in
159 * the middle */
160 if( buf->start + buf->content_length < buf->capacity )
Janos Follath6264e662019-11-26 11:11:15 +0000161 {
Piotr Nowickifb437d72020-01-13 16:59:12 +0100162
163 /* Calculate the number of bytes that need to be placed at lower memory
164 * address */
165 if( buf->start + buf->content_length + input_len
166 > buf->capacity )
167 {
168 overflow = ( buf->start + buf->content_length + input_len )
169 % buf->capacity;
170 }
171
172 memcpy( buf->buffer + buf->start + buf->content_length, input,
173 input_len - overflow );
174 memcpy( buf->buffer, input + input_len - overflow, overflow );
175
176 }
177 else
178 {
179 /* The buffer has come full circle and free space is in the middle */
180 memcpy( buf->buffer + buf->start + buf->content_length - buf->capacity,
181 input, input_len );
Janos Follath6264e662019-11-26 11:11:15 +0000182 }
183
Janos Follath6264e662019-11-26 11:11:15 +0000184 buf->content_length += input_len;
Janos Follath6264e662019-11-26 11:11:15 +0000185 return input_len;
186}
187
188/*
Andrzej Kurekf7774142020-01-22 06:34:59 -0500189 * Gets \p output_len bytes from the ring buffer \p buf into the
190 * \p output buffer. The output buffer can be NULL, in this case a part of the
191 * ring buffer will be dropped, if the requested length is available.
Janos Follath6264e662019-11-26 11:11:15 +0000192 *
193 * \p buf must have been initialized and set up by calling
194 * `mbedtls_test_buffer_init()` and `mbedtls_test_buffer_setup()`.
195 *
196 * \retval \p output_len, if the data is available.
197 * \retval 0 <= value < \p output_len, if the data is not available.
Andrzej Kurekf7774142020-01-22 06:34:59 -0500198 * \retval -1, if \buf is NULL or it hasn't been set up.
Janos Follath6264e662019-11-26 11:11:15 +0000199 */
200int mbedtls_test_buffer_get( mbedtls_test_buffer *buf,
201 unsigned char* output, size_t output_len )
202{
203 size_t overflow = 0;
204
205 if( ( buf == NULL ) || ( buf->buffer == NULL ) )
206 return -1;
207
Andrzej Kurekf7774142020-01-22 06:34:59 -0500208 if( output == NULL && output_len == 0 )
209 return 0;
Janos Follath6264e662019-11-26 11:11:15 +0000210
211 if( buf->content_length < output_len )
212 output_len = buf->content_length;
213
214 /* Calculate the number of bytes that need to be drawn from lower memory
215 * address */
216 if( buf->start + output_len > buf->capacity )
217 {
218 overflow = ( buf->start + output_len ) % buf->capacity;
219 }
220
Andrzej Kurekf7774142020-01-22 06:34:59 -0500221 if( output != NULL )
222 {
223 memcpy( output, buf->buffer + buf->start, output_len - overflow );
224 memcpy( output + output_len - overflow, buf->buffer, overflow );
225 }
226
Janos Follath6264e662019-11-26 11:11:15 +0000227 buf->content_length -= output_len;
228 buf->start = ( buf->start + output_len ) % buf->capacity;
229
230 return output_len;
231}
232
Hanno Beckera18d1322018-01-03 14:27:32 +0000233/*
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500234 * Errors used in the message transport mock tests
235 */
236 #define MBEDTLS_TEST_ERROR_ARG_NULL -11
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500237 #define MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED -44
238
239/*
240 * Context for a message metadata queue (fifo) that is on top of the ring buffer.
241 */
242typedef struct mbedtls_test_message_queue
243{
244 size_t *messages;
245 int pos;
246 int num;
247 int capacity;
248} mbedtls_test_message_queue;
249
250/*
251 * Setup and free functions for the message metadata queue.
252 *
253 * \p capacity describes the number of message metadata chunks that can be held
254 * within the queue.
255 *
256 * \retval 0, if a metadata queue of a given length can be allocated.
257 * \retval MBEDTLS_ERR_SSL_ALLOC_FAILED, if allocation failed.
258 */
259int mbedtls_test_message_queue_setup( mbedtls_test_message_queue *queue,
260 size_t capacity )
261{
262 queue->messages = (size_t*) mbedtls_calloc( capacity, sizeof(size_t) );
263 if( NULL == queue->messages )
264 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
265
266 queue->capacity = capacity;
267 queue->pos = 0;
268 queue->num = 0;
269
270 return 0;
271}
272
273void mbedtls_test_message_queue_free( mbedtls_test_message_queue *queue )
274{
275 if( queue == NULL )
276 return;
277
278 if( queue->messages != NULL )
279 mbedtls_free( queue->messages );
280
281 memset( queue, 0, sizeof( *queue ) );
282}
283
284/*
285 * Push message length information onto the message metadata queue.
286 * This will become the last element to leave it (fifo).
287 *
288 * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null.
Andrzej Kurekf46b9122020-02-07 08:19:00 -0500289 * \retval MBEDTLS_ERR_SSL_WANT_WRITE, if the queue is full.
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500290 * \retval \p len, if the push was successful.
291 */
292int mbedtls_test_message_queue_push_info( mbedtls_test_message_queue *queue,
293 size_t len )
294{
295 int place;
296 if( queue == NULL )
297 return MBEDTLS_TEST_ERROR_ARG_NULL;
298
299 if( queue->num >= queue->capacity )
Andrzej Kurekf46b9122020-02-07 08:19:00 -0500300 return MBEDTLS_ERR_SSL_WANT_WRITE;
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500301
302 place = ( queue->pos + queue->num ) % queue->capacity;
303 queue->messages[place] = len;
304 queue->num++;
305 return len;
306}
307
308/*
309 * Pop information about the next message length from the queue. This will be
310 * the oldest inserted message length(fifo). \p msg_len can be null, in which
311 * case the data will be popped from the queue but not copied anywhere.
312 *
313 * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null.
Andrzej Kurekf46b9122020-02-07 08:19:00 -0500314 * \retval MBEDTLS_ERR_SSL_WANT_READ, if the queue is empty.
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500315 * \retval message length, if the pop was successful, up to the given
316 \p buf_len.
317 */
318int mbedtls_test_message_queue_pop_info( mbedtls_test_message_queue *queue,
319 size_t buf_len )
320{
321 size_t message_length;
322 if( queue == NULL )
323 return MBEDTLS_TEST_ERROR_ARG_NULL;
324 if( queue->num == 0 )
Andrzej Kurekf46b9122020-02-07 08:19:00 -0500325 return MBEDTLS_ERR_SSL_WANT_READ;
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500326
327 message_length = queue->messages[queue->pos];
328 queue->messages[queue->pos] = 0;
329 queue->num--;
330 queue->pos++;
331 queue->pos %= queue->capacity;
332 if( queue->pos < 0 )
333 queue->pos += queue->capacity;
334
335 return ( message_length > buf_len ) ? buf_len : message_length;
336}
337
338/*
339 * Take a peek on the info about the next message length from the queue.
340 * This will be the oldest inserted message length(fifo).
341 *
342 * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null.
Andrzej Kurekf46b9122020-02-07 08:19:00 -0500343 * \retval MBEDTLS_ERR_SSL_WANT_READ, if the queue is empty.
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500344 * \retval 0, if the peek was successful.
345 * \retval MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED, if the given buffer length is
346 * too small to fit the message. In this case the \p msg_len will be
347 * set to the full message length so that the
348 * caller knows what portion of the message can be dropped.
349 */
350int mbedtls_test_message_queue_peek_info( mbedtls_test_message_queue *queue,
351 size_t buf_len, size_t* msg_len )
352{
353 if( queue == NULL || msg_len == NULL )
354 return MBEDTLS_TEST_ERROR_ARG_NULL;
355 if( queue->num == 0 )
Andrzej Kurekf46b9122020-02-07 08:19:00 -0500356 return MBEDTLS_ERR_SSL_WANT_READ;
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500357
358 *msg_len = queue->messages[queue->pos];
359 return ( *msg_len > buf_len ) ? MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED : 0;
360}
361/*
Janos Follath031827f2019-11-27 11:12:14 +0000362 * Context for the I/O callbacks simulating network connection.
363 */
364
365#define MBEDTLS_MOCK_SOCKET_CONNECTED 1
366
367typedef struct mbedtls_mock_socket
368{
369 int status;
370 mbedtls_test_buffer *input;
371 mbedtls_test_buffer *output;
372 struct mbedtls_mock_socket *peer;
373} mbedtls_mock_socket;
374
375/*
376 * Setup and teardown functions for mock sockets.
377 */
378void mbedtls_mock_socket_init( mbedtls_mock_socket *socket )
379{
380 memset( socket, 0, sizeof( *socket ) );
381}
382
383/*
384 * Closes the socket \p socket.
385 *
386 * \p socket must have been previously initialized by calling
387 * mbedtls_mock_socket_init().
388 *
389 * This function frees all allocated resources and both sockets are aware of the
390 * new connection state.
391 *
392 * That is, this function does not simulate half-open TCP connections and the
393 * phenomenon that when closing a UDP connection the peer is not aware of the
394 * connection having been closed.
395 */
396void mbedtls_mock_socket_close( mbedtls_mock_socket* socket )
397{
398 if( socket == NULL )
399 return;
400
401 if( socket->input != NULL )
402 {
403 mbedtls_test_buffer_free( socket->input );
404 mbedtls_free( socket->input );
405 }
406
407 if( socket->output != NULL )
408 {
409 mbedtls_test_buffer_free( socket->output );
410 mbedtls_free( socket->output );
411 }
412
413 if( socket->peer != NULL )
414 memset( socket->peer, 0, sizeof( *socket->peer ) );
415
416 memset( socket, 0, sizeof( *socket ) );
417}
418
419/*
420 * Establishes a connection between \p peer1 and \p peer2.
421 *
422 * \p peer1 and \p peer2 must have been previously initialized by calling
423 * mbedtls_mock_socket_init().
424 *
425 * The capacites of the internal buffers are set to \p bufsize. Setting this to
426 * the correct value allows for simulation of MTU, sanity testing the mock
427 * implementation and mocking TCP connections with lower memory cost.
428 */
429int mbedtls_mock_socket_connect( mbedtls_mock_socket* peer1,
430 mbedtls_mock_socket* peer2,
431 size_t bufsize )
432{
433 int ret = -1;
434
Piotr Nowickid796e192020-01-28 12:09:47 +0100435 peer1->output =
Janos Follath031827f2019-11-27 11:12:14 +0000436 (mbedtls_test_buffer*) mbedtls_calloc( 1, sizeof(mbedtls_test_buffer) );
437 if( peer1->output == NULL )
438 {
439 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
440 goto exit;
441 }
442 mbedtls_test_buffer_init( peer1->output );
443 if( 0 != ( ret = mbedtls_test_buffer_setup( peer1->output, bufsize ) ) )
444 {
445 goto exit;
446 }
447
Piotr Nowickid796e192020-01-28 12:09:47 +0100448 peer2->output =
449 (mbedtls_test_buffer*) mbedtls_calloc( 1, sizeof(mbedtls_test_buffer) );
450 if( peer2->output == NULL )
451 {
452 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
453 goto exit;
454 }
455 mbedtls_test_buffer_init( peer2->output );
456 if( 0 != ( ret = mbedtls_test_buffer_setup( peer2->output, bufsize ) ) )
457 {
458 goto exit;
459 }
460
Janos Follath031827f2019-11-27 11:12:14 +0000461 peer1->peer = peer2;
462 peer2->peer = peer1;
Piotr Nowickid796e192020-01-28 12:09:47 +0100463 peer1->input = peer2->output;
464 peer2->input = peer1->output;
Janos Follath031827f2019-11-27 11:12:14 +0000465
466 peer1->status = peer2->status = MBEDTLS_MOCK_SOCKET_CONNECTED;
467 ret = 0;
468
469exit:
470
471 if( ret != 0 )
472 {
473 mbedtls_mock_socket_close( peer1 );
474 mbedtls_mock_socket_close( peer2 );
475 }
476
477 return ret;
478}
479
480/*
481 * Callbacks for simulating blocking I/O over connection-oriented transport.
482 */
483
484int mbedtls_mock_tcp_send_b( void *ctx, const unsigned char *buf, size_t len )
485{
486 mbedtls_mock_socket *socket = (mbedtls_mock_socket*) ctx;
487
488 if( socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED )
489 return -1;
490
491 return mbedtls_test_buffer_put( socket->output, buf, len );
492}
493
494int mbedtls_mock_tcp_recv_b( void *ctx, unsigned char *buf, size_t len )
495{
496 mbedtls_mock_socket *socket = (mbedtls_mock_socket*) ctx;
497
498 if( socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED )
499 return -1;
500
501 return mbedtls_test_buffer_get( socket->input, buf, len );
502}
503
504/*
Janos Follath3766ba52019-11-27 13:31:42 +0000505 * Callbacks for simulating non-blocking I/O over connection-oriented transport.
506 */
507
508int mbedtls_mock_tcp_send_nb( void *ctx, const unsigned char *buf, size_t len )
509{
510 mbedtls_mock_socket *socket = (mbedtls_mock_socket*) ctx;
511
512 if( socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED )
513 return -1;
514
Piotr Nowicki890b5ca2020-01-15 16:19:07 +0100515 if( socket->output->capacity == socket->output->content_length )
Janos Follath3766ba52019-11-27 13:31:42 +0000516 {
Janos Follath3766ba52019-11-27 13:31:42 +0000517 return MBEDTLS_ERR_SSL_WANT_WRITE;
518 }
519
Janos Follath3766ba52019-11-27 13:31:42 +0000520 return mbedtls_test_buffer_put( socket->output, buf, len );
521}
522
523int mbedtls_mock_tcp_recv_nb( void *ctx, unsigned char *buf, size_t len )
524{
525 mbedtls_mock_socket *socket = (mbedtls_mock_socket*) ctx;
526
527 if( socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED )
528 return -1;
529
Andrzej Kurekf40daa32020-02-04 09:00:01 -0500530 if( socket->input->content_length == 0 )
Janos Follath3766ba52019-11-27 13:31:42 +0000531 {
Janos Follath3766ba52019-11-27 13:31:42 +0000532 return MBEDTLS_ERR_SSL_WANT_READ;
533 }
534
Janos Follath3766ba52019-11-27 13:31:42 +0000535 return mbedtls_test_buffer_get( socket->input, buf, len );
536}
537
Andrzej Kurekbc483de2020-01-22 03:40:00 -0500538/* Errors used in the message socket mocks */
539
540#define MBEDTLS_TEST_ERROR_CONTEXT_ERROR -55
541#define MBEDTLS_TEST_ERROR_SEND_FAILED -66
542#define MBEDTLS_TEST_ERROR_RECV_FAILED -77
543
544/*
545 * Structure used as an addon, or a wrapper, around the mocked sockets.
546 * Contains an input queue, to which the other socket pushes metadata,
547 * and an output queue, to which this one pushes metadata. This context is
548 * considered as an owner of the input queue only, which is initialized and
549 * freed in the respective setup and free calls.
550 */
551typedef struct mbedtls_test_message_socket_context
552{
553 mbedtls_test_message_queue* queue_input;
554 mbedtls_test_message_queue* queue_output;
555 mbedtls_mock_socket* socket;
556} mbedtls_test_message_socket_context;
557
Andrzej Kurek45916ba2020-03-05 14:46:22 -0500558void mbedtls_message_socket_init( mbedtls_test_message_socket_context *ctx )
559{
560 ctx->queue_input = NULL;
561 ctx->queue_output = NULL;
562 ctx->socket = NULL;
563}
564
Andrzej Kurekbc483de2020-01-22 03:40:00 -0500565/*
566 * Setup a given mesasge socket context including initialization of
567 * input/output queues to a chosen capacity of messages. Also set the
568 * corresponding mock socket.
569 *
570 * \retval 0, if everything succeeds.
571 * \retval MBEDTLS_ERR_SSL_ALLOC_FAILED, if allocation of a message
572 * queue failed.
573 */
574int mbedtls_message_socket_setup( mbedtls_test_message_queue* queue_input,
575 mbedtls_test_message_queue* queue_output,
576 size_t queue_capacity,
577 mbedtls_mock_socket* socket,
578 mbedtls_test_message_socket_context* ctx )
579{
580 int ret = mbedtls_test_message_queue_setup( queue_input, queue_capacity );
581 if( ret != 0 )
582 return ret;
583 ctx->queue_input = queue_input;
584 ctx->queue_output = queue_output;
585 ctx->socket = socket;
586 mbedtls_mock_socket_init( socket );
587
588 return 0;
589}
590
591/*
592 * Close a given message socket context, along with the socket itself. Free the
593 * memory allocated by the input queue.
594 */
595void mbedtls_message_socket_close( mbedtls_test_message_socket_context* ctx )
596{
597 if( ctx == NULL )
598 return;
599
600 mbedtls_test_message_queue_free( ctx->queue_input );
601 mbedtls_mock_socket_close( ctx->socket );
602 memset( ctx, 0, sizeof( *ctx ) );
603}
604
605/*
606 * Send one message through a given message socket context.
607 *
608 * \retval \p len, if everything succeeds.
609 * \retval MBEDTLS_TEST_ERROR_CONTEXT_ERROR, if any of the needed context
610 * elements or the context itself is null.
611 * \retval MBEDTLS_TEST_ERROR_SEND_FAILED if mbedtls_mock_tcp_send_b failed.
Andrzej Kurekf46b9122020-02-07 08:19:00 -0500612 * \retval MBEDTLS_ERR_SSL_WANT_WRITE, if the output queue is full.
Andrzej Kurekbc483de2020-01-22 03:40:00 -0500613 *
614 * This function will also return any error from
615 * mbedtls_test_message_queue_push_info.
616 */
617int mbedtls_mock_tcp_send_msg( void *ctx, const unsigned char *buf, size_t len )
618{
619 mbedtls_test_message_queue* queue;
620 mbedtls_mock_socket* socket;
621 mbedtls_test_message_socket_context *context = (mbedtls_test_message_socket_context*) ctx;
622
623 if( context == NULL || context->socket == NULL
624 || context->queue_output == NULL )
625 {
626 return MBEDTLS_TEST_ERROR_CONTEXT_ERROR;
627 }
628
629 queue = context->queue_output;
630 socket = context->socket;
631
632 if( queue->num >= queue->capacity )
Andrzej Kurekf46b9122020-02-07 08:19:00 -0500633 return MBEDTLS_ERR_SSL_WANT_WRITE;
Andrzej Kurekbc483de2020-01-22 03:40:00 -0500634
635 if( mbedtls_mock_tcp_send_b( socket, buf, len ) != (int) len )
636 return MBEDTLS_TEST_ERROR_SEND_FAILED;
637
638 return mbedtls_test_message_queue_push_info( queue, len );
639}
640
641/*
642 * Receive one message from a given message socket context and return message
643 * length or an error.
644 *
645 * \retval message length, if everything succeeds.
646 * \retval MBEDTLS_TEST_ERROR_CONTEXT_ERROR, if any of the needed context
647 * elements or the context itself is null.
648 * \retval MBEDTLS_TEST_ERROR_RECV_FAILED if mbedtls_mock_tcp_recv_b failed.
649 *
650 * This function will also return any error other than
651 * MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED from mbedtls_test_message_queue_peek_info.
652 */
653int mbedtls_mock_tcp_recv_msg( void *ctx, unsigned char *buf, size_t buf_len )
654{
655 mbedtls_test_message_queue* queue;
656 mbedtls_mock_socket* socket;
657 mbedtls_test_message_socket_context *context = (mbedtls_test_message_socket_context*) ctx;
Gilles Peskine19e841e2020-03-09 20:43:51 +0100658 size_t drop_len = 0;
Andrzej Kurekbc483de2020-01-22 03:40:00 -0500659 size_t msg_len;
660 int ret;
661
662 if( context == NULL || context->socket == NULL
663 || context->queue_input == NULL )
664 {
665 return MBEDTLS_TEST_ERROR_CONTEXT_ERROR;
666 }
667
668 queue = context->queue_input;
669 socket = context->socket;
670
671 /* Peek first, so that in case of a socket error the data remains in
672 * the queue. */
673 ret = mbedtls_test_message_queue_peek_info( queue, buf_len, &msg_len );
674 if( ret == MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED )
675 {
676 /* Calculate how much to drop */
677 drop_len = msg_len - buf_len;
678
679 /* Set the requested message len to be buffer length */
680 msg_len = buf_len;
681 } else if( ret != 0 )
682 {
683 return ret;
684 }
685
686 if( mbedtls_mock_tcp_recv_b( socket, buf, msg_len ) != (int) msg_len )
687 return MBEDTLS_TEST_ERROR_RECV_FAILED;
688
689 if( ret == MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED )
690 {
691 /* Drop the remaining part of the message */
692 if( mbedtls_mock_tcp_recv_b( socket, NULL, drop_len ) != (int) drop_len )
693 {
694 /* Inconsistent state - part of the message was read,
695 * and a part couldn't. Not much we can do here, but it should not
696 * happen in test environment, unless forced manually. */
697 }
698 }
699 mbedtls_test_message_queue_pop_info( queue, buf_len );
700
701 return msg_len;
702}
703
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +0200704#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
705 defined(MBEDTLS_ENTROPY_C) && \
706 defined(MBEDTLS_CTR_DRBG_C)
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100707
708/*
709 * Structure with endpoint's certificates for SSL communication tests.
710 */
711typedef struct mbedtls_endpoint_certificate
712{
713 mbedtls_x509_crt ca_cert;
714 mbedtls_x509_crt cert;
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100715 mbedtls_pk_context pkey;
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100716} mbedtls_endpoint_certificate;
717
718/*
719 * Endpoint structure for SSL communication tests.
720 */
721typedef struct mbedtls_endpoint
722{
723 const char *name;
724 mbedtls_ssl_context ssl;
725 mbedtls_ssl_config conf;
726 mbedtls_ctr_drbg_context ctr_drbg;
727 mbedtls_entropy_context entropy;
728 mbedtls_mock_socket socket;
729 mbedtls_endpoint_certificate cert;
730} mbedtls_endpoint;
731
732/*
733 * Initializes \p ep_cert structure and assigns it to endpoint
734 * represented by \p ep.
735 *
736 * \retval 0 on success, otherwise error code.
737 */
Andrzej Kurekb2980742020-02-02 19:25:26 -0500738int mbedtls_endpoint_certificate_init( mbedtls_endpoint *ep, int pk_alg )
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100739{
740 int i = 0;
741 int ret = -1;
742 mbedtls_endpoint_certificate *cert;
743
744 if( ep == NULL )
745 {
746 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
747 }
748
749 cert = &( ep->cert );
750 mbedtls_x509_crt_init( &( cert->ca_cert ) );
751 mbedtls_x509_crt_init( &( cert->cert ) );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100752 mbedtls_pk_init( &( cert->pkey ) );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100753
754 /* Load the trusted CA */
755
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100756 for( i = 0; mbedtls_test_cas_der[i] != NULL; i++ )
757 {
758 ret = mbedtls_x509_crt_parse_der( &( cert->ca_cert ),
759 (const unsigned char *) mbedtls_test_cas_der[i],
760 mbedtls_test_cas_der_len[i] );
761 TEST_ASSERT( ret == 0 );
762 }
763
764 /* Load own certificate and private key */
765
766 if( ep->conf.endpoint == MBEDTLS_SSL_IS_SERVER )
767 {
Andrzej Kurekb2980742020-02-02 19:25:26 -0500768 if( pk_alg == MBEDTLS_PK_RSA )
769 {
770 ret = mbedtls_x509_crt_parse( &( cert->cert ),
771 (const unsigned char*) mbedtls_test_srv_crt_rsa_sha256_der,
772 mbedtls_test_srv_crt_rsa_sha256_der_len );
773 TEST_ASSERT( ret == 0 );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100774
Andrzej Kurekb2980742020-02-02 19:25:26 -0500775 ret = mbedtls_pk_parse_key( &( cert->pkey ),
776 (const unsigned char*) mbedtls_test_srv_key_rsa_der,
777 mbedtls_test_srv_key_rsa_der_len, NULL, 0 );
778 TEST_ASSERT( ret == 0 );
779 }
780 else
781 {
782 ret = mbedtls_x509_crt_parse( &( cert->cert ),
783 (const unsigned char*) mbedtls_test_srv_crt_ec_der,
784 mbedtls_test_srv_crt_ec_der_len );
785 TEST_ASSERT( ret == 0 );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100786
Andrzej Kurekb2980742020-02-02 19:25:26 -0500787 ret = mbedtls_pk_parse_key( &( cert->pkey ),
788 (const unsigned char*) mbedtls_test_srv_key_ec_der,
789 mbedtls_test_srv_key_ec_der_len, NULL, 0 );
790 TEST_ASSERT( ret == 0 );
791 }
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100792 }
793 else
794 {
Andrzej Kurekb2980742020-02-02 19:25:26 -0500795 if( pk_alg == MBEDTLS_PK_RSA )
796 {
797 ret = mbedtls_x509_crt_parse( &( cert->cert ),
798 (const unsigned char *) mbedtls_test_cli_crt_rsa_der,
799 mbedtls_test_cli_crt_rsa_der_len );
800 TEST_ASSERT( ret == 0 );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100801
Andrzej Kurekb2980742020-02-02 19:25:26 -0500802 ret = mbedtls_pk_parse_key( &( cert->pkey ),
803 (const unsigned char *) mbedtls_test_cli_key_rsa_der,
804 mbedtls_test_cli_key_rsa_der_len, NULL, 0 );
805 TEST_ASSERT( ret == 0 );
806 }
807 else
808 {
809 ret = mbedtls_x509_crt_parse( &( cert->cert ),
810 (const unsigned char *) mbedtls_test_cli_crt_ec_der,
811 mbedtls_test_cli_crt_ec_len );
812 TEST_ASSERT( ret == 0 );
813
814 ret = mbedtls_pk_parse_key( &( cert->pkey ),
815 (const unsigned char *) mbedtls_test_cli_key_ec_der,
816 mbedtls_test_cli_key_ec_der_len, NULL, 0 );
817 TEST_ASSERT( ret == 0 );
818 }
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100819 }
820
821 mbedtls_ssl_conf_ca_chain( &( ep->conf ), &( cert->ca_cert ), NULL );
822
Andrzej Kurekb2980742020-02-02 19:25:26 -0500823 ret = mbedtls_ssl_conf_own_cert( &( ep->conf ), &( cert->cert ),
824 &( cert->pkey ) );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100825 TEST_ASSERT( ret == 0 );
826
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100827exit:
828 if( ret != 0 )
829 {
830 mbedtls_x509_crt_free( &( cert->ca_cert ) );
831 mbedtls_x509_crt_free( &( cert->cert ) );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100832 mbedtls_pk_free( &( cert->pkey ) );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100833 }
834
835 return ret;
836}
837
838/*
839 * Initializes \p ep structure. It is important to call `mbedtls_endpoint_free()`
840 * after calling this function even if it fails.
841 *
842 * \p endpoint_type must be set as MBEDTLS_SSL_IS_SERVER or
843 * MBEDTLS_SSL_IS_CLIENT.
Andrzej Kurek15daf502020-02-12 09:17:52 -0500844 * \p pk_alg the algorithm to use, currently only MBEDTLS_PK_RSA and
845 * MBEDTLS_PK_ECDSA are supported.
846 * \p dtls_context - in case of DTLS - this is the context handling metadata.
847 * \p input_queue - used only in case of DTLS.
848 * \p output_queue - used only in case of DTLS.
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100849 *
850 * \retval 0 on success, otherwise error code.
851 */
Andrzej Kurek15daf502020-02-12 09:17:52 -0500852int mbedtls_endpoint_init( mbedtls_endpoint *ep, int endpoint_type, int pk_alg,
853 mbedtls_test_message_socket_context *dtls_context,
854 mbedtls_test_message_queue *input_queue,
855 mbedtls_test_message_queue *output_queue )
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100856{
857 int ret = -1;
858
Andrzej Kurek15daf502020-02-12 09:17:52 -0500859 if( dtls_context != NULL && ( input_queue == NULL || output_queue == NULL ) )
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100860 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Andrzej Kurek15daf502020-02-12 09:17:52 -0500861
862 if( ep == NULL )
863 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100864
865 memset( ep, 0, sizeof( *ep ) );
866
867 ep->name = ( endpoint_type == MBEDTLS_SSL_IS_SERVER ) ? "Server" : "Client";
868
869 mbedtls_ssl_init( &( ep->ssl ) );
870 mbedtls_ssl_config_init( &( ep->conf ) );
871 mbedtls_ctr_drbg_init( &( ep->ctr_drbg ) );
872 mbedtls_ssl_conf_rng( &( ep->conf ),
873 mbedtls_ctr_drbg_random,
874 &( ep->ctr_drbg ) );
875 mbedtls_entropy_init( &( ep->entropy ) );
Andrzej Kurek15daf502020-02-12 09:17:52 -0500876 if( dtls_context != NULL )
877 {
878 TEST_ASSERT( mbedtls_message_socket_setup( input_queue, output_queue,
879 100, &( ep->socket ),
880 dtls_context ) == 0 );
881 }
882 else
883 {
884 mbedtls_mock_socket_init( &( ep->socket ) );
885 }
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100886
887 ret = mbedtls_ctr_drbg_seed( &( ep->ctr_drbg ), mbedtls_entropy_func,
888 &( ep->entropy ), (const unsigned char *) ( ep->name ),
889 strlen( ep->name ) );
890 TEST_ASSERT( ret == 0 );
891
892 /* Non-blocking callbacks without timeout */
Andrzej Kurek15daf502020-02-12 09:17:52 -0500893 if( dtls_context != NULL )
894 {
895 mbedtls_ssl_set_bio( &( ep->ssl ), dtls_context,
896 mbedtls_mock_tcp_send_msg,
897 mbedtls_mock_tcp_recv_msg,
898 NULL );
899 }
900 else
901 {
902 mbedtls_ssl_set_bio( &( ep->ssl ), &( ep->socket ),
903 mbedtls_mock_tcp_send_nb,
904 mbedtls_mock_tcp_recv_nb,
905 NULL );
906 }
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100907
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100908 ret = mbedtls_ssl_config_defaults( &( ep->conf ), endpoint_type,
Andrzej Kurek15daf502020-02-12 09:17:52 -0500909 ( dtls_context != NULL ) ?
910 MBEDTLS_SSL_TRANSPORT_DATAGRAM :
911 MBEDTLS_SSL_TRANSPORT_STREAM,
912 MBEDTLS_SSL_PRESET_DEFAULT );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100913 TEST_ASSERT( ret == 0 );
914
Andrzej Kurek1a44a152020-02-07 08:21:32 -0500915 ret = mbedtls_ssl_setup( &( ep->ssl ), &( ep->conf ) );
916 TEST_ASSERT( ret == 0 );
Andrzej Kurek15daf502020-02-12 09:17:52 -0500917
918#if defined(MBEDTLS_SSL_PROTO_DTLS) && defined(MBEDTLS_SSL_SRV_C)
919 if( endpoint_type == MBEDTLS_SSL_IS_SERVER && dtls_context != NULL )
920 mbedtls_ssl_conf_dtls_cookies( &( ep->conf ), NULL, NULL, NULL );
921#endif
922
Andrzej Kurekb2980742020-02-02 19:25:26 -0500923 ret = mbedtls_endpoint_certificate_init( ep, pk_alg );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100924 TEST_ASSERT( ret == 0 );
925
926exit:
927 return ret;
928}
929
930/*
931 * Deinitializes certificates from endpoint represented by \p ep.
932 */
933void mbedtls_endpoint_certificate_free( mbedtls_endpoint *ep )
934{
935 mbedtls_endpoint_certificate *cert = &( ep->cert );
936 mbedtls_x509_crt_free( &( cert->ca_cert ) );
937 mbedtls_x509_crt_free( &( cert->cert ) );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100938 mbedtls_pk_free( &( cert->pkey ) );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100939}
940
941/*
942 * Deinitializes endpoint represented by \p ep.
943 */
Andrzej Kurek15daf502020-02-12 09:17:52 -0500944void mbedtls_endpoint_free( mbedtls_endpoint *ep,
945 mbedtls_test_message_socket_context *context )
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100946{
947 mbedtls_endpoint_certificate_free( ep );
948
949 mbedtls_ssl_free( &( ep->ssl ) );
950 mbedtls_ssl_config_free( &( ep->conf ) );
951 mbedtls_ctr_drbg_free( &( ep->ctr_drbg ) );
952 mbedtls_entropy_free( &( ep->entropy ) );
Andrzej Kurek15daf502020-02-12 09:17:52 -0500953
954 if( context != NULL )
955 {
956 mbedtls_message_socket_close( context );
957 }
958 else
959 {
960 mbedtls_mock_socket_close( &( ep->socket ) );
961 }
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100962}
963
964/*
965 * This function moves ssl handshake from \p ssl to prescribed \p state.
966 * /p second_ssl is used as second endpoint and their sockets have to be
967 * connected before calling this function.
968 *
969 * \retval 0 on success, otherwise error code.
970 */
971int mbedtls_move_handshake_to_state( mbedtls_ssl_context *ssl,
972 mbedtls_ssl_context *second_ssl,
973 int state )
974{
975 enum { BUFFSIZE = 1024 };
976 int max_steps = 1000;
977 int ret = 0;
978
979 if( ssl == NULL || second_ssl == NULL )
980 {
981 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
982 }
983
984 /* Perform communication via connected sockets */
985 while( ( ssl->state != state ) && ( --max_steps >= 0 ) )
986 {
987 /* If /p second_ssl ends the handshake procedure before /p ssl then
988 * there is no need to call the next step */
989 if( second_ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
990 {
991 ret = mbedtls_ssl_handshake_step( second_ssl );
992 if( ret != 0 && ret != MBEDTLS_ERR_SSL_WANT_READ &&
993 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
994 {
995 return ret;
996 }
997 }
998
999 /* We only care about the \p ssl state and returns, so we call it last,
1000 * to leave the iteration as soon as the state is as expected. */
1001 ret = mbedtls_ssl_handshake_step( ssl );
1002 if( ret != 0 && ret != MBEDTLS_ERR_SSL_WANT_READ &&
1003 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
1004 {
1005 return ret;
1006 }
1007 }
1008
1009 return ( max_steps >= 0 ) ? ret : -1;
1010}
1011
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02001012#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01001013
Janos Follath3766ba52019-11-27 13:31:42 +00001014/*
Piotr Nowicki438bf3b2020-03-10 12:59:10 +01001015 * Write application data. Increase write counter if necessary.
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001016 */
1017int mbedtls_ssl_write_fragment( mbedtls_ssl_context *ssl, unsigned char *buf,
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001018 int buf_len, int *written,
Piotr Nowicki438bf3b2020-03-10 12:59:10 +01001019 const int expected_fragments )
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001020{
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001021 int ret = mbedtls_ssl_write( ssl, buf + *written, buf_len - *written );
1022 if( ret > 0 )
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001023 {
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001024 *written += ret;
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001025 }
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001026
1027 if( expected_fragments == 0 )
1028 {
1029 /* Used for DTLS and the message size larger than MFL. In that case
1030 * the message can not be fragmented and the library should return
1031 * MBEDTLS_ERR_SSL_BAD_INPUT_DATA error. This error must be returned
1032 * to prevent a dead loop inside mbedtls_exchange_data(). */
1033 return ret;
1034 }
1035 else if( expected_fragments == 1 )
1036 {
1037 /* Used for TLS/DTLS and the message size lower than MFL */
1038 TEST_ASSERT( ret == buf_len ||
1039 ret == MBEDTLS_ERR_SSL_WANT_READ ||
1040 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
1041 }
1042 else
1043 {
1044 /* Used for TLS and the message size larger than MFL */
1045 TEST_ASSERT( expected_fragments > 1 );
1046 TEST_ASSERT( ( ret >= 0 && ret <= buf_len ) ||
1047 ret == MBEDTLS_ERR_SSL_WANT_READ ||
1048 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
1049 }
1050
1051 return 0;
1052
1053exit:
1054 /* Some of the tests failed */
1055 return -1;
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001056}
1057
1058/*
Piotr Nowicki438bf3b2020-03-10 12:59:10 +01001059 * Read application data and increase read counter and fragments counter if necessary.
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001060 */
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001061int mbedtls_ssl_read_fragment( mbedtls_ssl_context *ssl, unsigned char *buf,
1062 int buf_len, int *read,
Piotr Nowicki438bf3b2020-03-10 12:59:10 +01001063 int *fragments, const int expected_fragments )
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001064{
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001065 int ret = mbedtls_ssl_read( ssl, buf + *read, buf_len - *read );
1066 if( ret > 0 )
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001067 {
Piotr Nowicki438bf3b2020-03-10 12:59:10 +01001068 ( *fragments )++;
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001069 *read += ret;
1070 }
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001071
1072 if( expected_fragments == 0 )
1073 {
1074 TEST_ASSERT( ret == 0 );
1075 }
1076 else if( expected_fragments == 1 )
1077 {
1078 TEST_ASSERT( ret == buf_len ||
1079 ret == MBEDTLS_ERR_SSL_WANT_READ ||
1080 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
1081 }
1082 else
1083 {
1084 TEST_ASSERT( expected_fragments > 1 );
1085 TEST_ASSERT( ( ret >= 0 && ret <= buf_len ) ||
1086 ret == MBEDTLS_ERR_SSL_WANT_READ ||
1087 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
1088 }
1089
1090 return 0;
1091
1092exit:
1093 /* Some of the tests failed */
1094 return -1;
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001095}
1096
1097/*
Hanno Beckera18d1322018-01-03 14:27:32 +00001098 * Helper function setting up inverse record transformations
1099 * using given cipher, hash, EtM mode, authentication tag length,
1100 * and version.
1101 */
1102
1103#define CHK( x ) \
1104 do \
1105 { \
1106 if( !( x ) ) \
Hanno Becker81e16a32019-03-01 11:21:44 +00001107 { \
Hanno Beckera5780f12019-04-05 09:55:37 +01001108 ret = -1; \
Hanno Becker81e16a32019-03-01 11:21:44 +00001109 goto cleanup; \
1110 } \
Hanno Beckera18d1322018-01-03 14:27:32 +00001111 } while( 0 )
1112
Andrzej Kurekf40daa32020-02-04 09:00:01 -05001113void set_ciphersuite( mbedtls_ssl_config *conf, const char *cipher,
1114 int* forced_ciphersuite )
1115{
1116 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
1117 forced_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id( cipher );
1118 forced_ciphersuite[1] = 0;
1119
1120 ciphersuite_info =
1121 mbedtls_ssl_ciphersuite_from_id( forced_ciphersuite[0] );
1122
1123 TEST_ASSERT( ciphersuite_info != NULL );
1124 TEST_ASSERT( ciphersuite_info->min_minor_ver <= conf->max_minor_ver );
1125 TEST_ASSERT( ciphersuite_info->max_minor_ver >= conf->min_minor_ver );
1126
1127 if( conf->max_minor_ver > ciphersuite_info->max_minor_ver )
1128 {
1129 conf->max_minor_ver = ciphersuite_info->max_minor_ver;
1130 }
1131 if( conf->min_minor_ver < ciphersuite_info->min_minor_ver )
1132 {
1133 conf->min_minor_ver = ciphersuite_info->min_minor_ver;
1134 }
1135
1136 mbedtls_ssl_conf_ciphersuites( conf, forced_ciphersuite );
1137
1138exit:
1139 return;
1140}
1141
Andrzej Kurekcc5169c2020-02-04 09:04:56 -05001142int psk_dummy_callback( void *p_info, mbedtls_ssl_context *ssl,
1143 const unsigned char *name, size_t name_len )
1144{
1145 (void) p_info;
1146 (void) ssl;
1147 (void) name;
1148 (void) name_len;
1149
1150 return ( 0 );
1151}
1152
Hanno Beckerd856c822019-04-29 17:30:59 +01001153#if MBEDTLS_SSL_CID_OUT_LEN_MAX > MBEDTLS_SSL_CID_IN_LEN_MAX
1154#define SSL_CID_LEN_MIN MBEDTLS_SSL_CID_IN_LEN_MAX
1155#else
1156#define SSL_CID_LEN_MIN MBEDTLS_SSL_CID_OUT_LEN_MAX
1157#endif
Hanno Beckera18d1322018-01-03 14:27:32 +00001158
1159static int build_transforms( mbedtls_ssl_transform *t_in,
1160 mbedtls_ssl_transform *t_out,
1161 int cipher_type, int hash_id,
Hanno Beckerd856c822019-04-29 17:30:59 +01001162 int etm, int tag_mode, int ver,
1163 size_t cid0_len,
1164 size_t cid1_len )
Hanno Beckera18d1322018-01-03 14:27:32 +00001165{
1166 mbedtls_cipher_info_t const *cipher_info;
Hanno Beckera5780f12019-04-05 09:55:37 +01001167 int ret = 0;
Hanno Beckera18d1322018-01-03 14:27:32 +00001168
1169 size_t keylen, maclen, ivlen;
Hanno Becker81e16a32019-03-01 11:21:44 +00001170 unsigned char *key0 = NULL, *key1 = NULL;
Hanno Beckera18d1322018-01-03 14:27:32 +00001171 unsigned char iv_enc[16], iv_dec[16];
1172
Hanno Beckera0e20d02019-05-15 14:03:01 +01001173#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd856c822019-04-29 17:30:59 +01001174 unsigned char cid0[ SSL_CID_LEN_MIN ];
1175 unsigned char cid1[ SSL_CID_LEN_MIN ];
1176
1177 rnd_std_rand( NULL, cid0, sizeof( cid0 ) );
1178 rnd_std_rand( NULL, cid1, sizeof( cid1 ) );
Hanno Becker43c24b82019-05-01 09:45:57 +01001179#else
1180 ((void) cid0_len);
1181 ((void) cid1_len);
Hanno Beckera0e20d02019-05-15 14:03:01 +01001182#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerd856c822019-04-29 17:30:59 +01001183
Hanno Beckera18d1322018-01-03 14:27:32 +00001184 maclen = 0;
1185
1186 /* Pick cipher */
1187 cipher_info = mbedtls_cipher_info_from_type( cipher_type );
1188 CHK( cipher_info != NULL );
1189 CHK( cipher_info->iv_size <= 16 );
1190 CHK( cipher_info->key_bitlen % 8 == 0 );
1191
1192 /* Pick keys */
1193 keylen = cipher_info->key_bitlen / 8;
Hanno Becker78d1f702019-04-05 09:56:10 +01001194 /* Allocate `keylen + 1` bytes to ensure that we get
1195 * a non-NULL pointers from `mbedtls_calloc` even if
1196 * `keylen == 0` in the case of the NULL cipher. */
1197 CHK( ( key0 = mbedtls_calloc( 1, keylen + 1 ) ) != NULL );
1198 CHK( ( key1 = mbedtls_calloc( 1, keylen + 1 ) ) != NULL );
Hanno Beckera18d1322018-01-03 14:27:32 +00001199 memset( key0, 0x1, keylen );
1200 memset( key1, 0x2, keylen );
1201
1202 /* Setup cipher contexts */
1203 CHK( mbedtls_cipher_setup( &t_in->cipher_ctx_enc, cipher_info ) == 0 );
1204 CHK( mbedtls_cipher_setup( &t_in->cipher_ctx_dec, cipher_info ) == 0 );
1205 CHK( mbedtls_cipher_setup( &t_out->cipher_ctx_enc, cipher_info ) == 0 );
1206 CHK( mbedtls_cipher_setup( &t_out->cipher_ctx_dec, cipher_info ) == 0 );
1207
1208#if defined(MBEDTLS_CIPHER_MODE_CBC)
1209 if( cipher_info->mode == MBEDTLS_MODE_CBC )
1210 {
1211 CHK( mbedtls_cipher_set_padding_mode( &t_in->cipher_ctx_enc,
1212 MBEDTLS_PADDING_NONE ) == 0 );
1213 CHK( mbedtls_cipher_set_padding_mode( &t_in->cipher_ctx_dec,
1214 MBEDTLS_PADDING_NONE ) == 0 );
1215 CHK( mbedtls_cipher_set_padding_mode( &t_out->cipher_ctx_enc,
1216 MBEDTLS_PADDING_NONE ) == 0 );
1217 CHK( mbedtls_cipher_set_padding_mode( &t_out->cipher_ctx_dec,
1218 MBEDTLS_PADDING_NONE ) == 0 );
1219 }
1220#endif /* MBEDTLS_CIPHER_MODE_CBC */
1221
1222 CHK( mbedtls_cipher_setkey( &t_in->cipher_ctx_enc, key0,
1223 keylen << 3, MBEDTLS_ENCRYPT ) == 0 );
1224 CHK( mbedtls_cipher_setkey( &t_in->cipher_ctx_dec, key1,
1225 keylen << 3, MBEDTLS_DECRYPT ) == 0 );
1226 CHK( mbedtls_cipher_setkey( &t_out->cipher_ctx_enc, key1,
1227 keylen << 3, MBEDTLS_ENCRYPT ) == 0 );
1228 CHK( mbedtls_cipher_setkey( &t_out->cipher_ctx_dec, key0,
1229 keylen << 3, MBEDTLS_DECRYPT ) == 0 );
Hanno Beckera18d1322018-01-03 14:27:32 +00001230
1231 /* Setup MAC contexts */
1232#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1233 if( cipher_info->mode == MBEDTLS_MODE_CBC ||
1234 cipher_info->mode == MBEDTLS_MODE_STREAM )
1235 {
1236 mbedtls_md_info_t const *md_info;
1237 unsigned char *md0, *md1;
1238
1239 /* Pick hash */
1240 md_info = mbedtls_md_info_from_type( hash_id );
1241 CHK( md_info != NULL );
1242
1243 /* Pick hash keys */
1244 maclen = mbedtls_md_get_size( md_info );
Hanno Becker3ee54212019-04-04 16:31:26 +01001245 CHK( ( md0 = mbedtls_calloc( 1, maclen ) ) != NULL );
1246 CHK( ( md1 = mbedtls_calloc( 1, maclen ) ) != NULL );
Hanno Beckera18d1322018-01-03 14:27:32 +00001247 memset( md0, 0x5, maclen );
1248 memset( md1, 0x6, maclen );
1249
1250 CHK( mbedtls_md_setup( &t_out->md_ctx_enc, md_info, 1 ) == 0 );
1251 CHK( mbedtls_md_setup( &t_out->md_ctx_dec, md_info, 1 ) == 0 );
1252 CHK( mbedtls_md_setup( &t_in->md_ctx_enc, md_info, 1 ) == 0 );
1253 CHK( mbedtls_md_setup( &t_in->md_ctx_dec, md_info, 1 ) == 0 );
1254
1255 if( ver > MBEDTLS_SSL_MINOR_VERSION_0 )
1256 {
1257 CHK( mbedtls_md_hmac_starts( &t_in->md_ctx_enc,
1258 md0, maclen ) == 0 );
1259 CHK( mbedtls_md_hmac_starts( &t_in->md_ctx_dec,
1260 md1, maclen ) == 0 );
1261 CHK( mbedtls_md_hmac_starts( &t_out->md_ctx_enc,
1262 md1, maclen ) == 0 );
1263 CHK( mbedtls_md_hmac_starts( &t_out->md_ctx_dec,
1264 md0, maclen ) == 0 );
1265 }
1266#if defined(MBEDTLS_SSL_PROTO_SSL3)
1267 else
1268 {
1269 memcpy( &t_in->mac_enc, md0, maclen );
1270 memcpy( &t_in->mac_dec, md1, maclen );
1271 memcpy( &t_out->mac_enc, md1, maclen );
1272 memcpy( &t_out->mac_dec, md0, maclen );
1273 }
1274#endif
1275
Hanno Becker3ee54212019-04-04 16:31:26 +01001276 mbedtls_free( md0 );
1277 mbedtls_free( md1 );
Hanno Beckera18d1322018-01-03 14:27:32 +00001278 }
1279#else
1280 ((void) hash_id);
1281#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1282
1283
1284 /* Pick IV's (regardless of whether they
1285 * are being used by the transform). */
1286 ivlen = cipher_info->iv_size;
1287 memset( iv_enc, 0x3, sizeof( iv_enc ) );
1288 memset( iv_dec, 0x4, sizeof( iv_dec ) );
1289
1290 /*
1291 * Setup transforms
1292 */
1293
Jaeden Amero2de07f12019-06-05 13:32:08 +01001294#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
1295 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Hanno Beckera18d1322018-01-03 14:27:32 +00001296 t_out->encrypt_then_mac = etm;
1297 t_in->encrypt_then_mac = etm;
1298#else
1299 ((void) etm);
1300#endif
1301
1302 t_out->minor_ver = ver;
1303 t_in->minor_ver = ver;
1304 t_out->ivlen = ivlen;
1305 t_in->ivlen = ivlen;
1306
1307 switch( cipher_info->mode )
1308 {
1309 case MBEDTLS_MODE_GCM:
1310 case MBEDTLS_MODE_CCM:
Hanno Beckere6832872020-05-28 08:29:58 +01001311#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
1312 if( ver == MBEDTLS_SSL_MINOR_VERSION_4 )
1313 {
1314 t_out->fixed_ivlen = 12;
1315 t_in->fixed_ivlen = 12;
1316 }
1317 else
1318#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
1319 {
1320 t_out->fixed_ivlen = 4;
1321 t_in->fixed_ivlen = 4;
1322 }
Hanno Beckera18d1322018-01-03 14:27:32 +00001323 t_out->maclen = 0;
1324 t_in->maclen = 0;
1325 switch( tag_mode )
1326 {
1327 case 0: /* Full tag */
1328 t_out->taglen = 16;
1329 t_in->taglen = 16;
1330 break;
1331 case 1: /* Partial tag */
1332 t_out->taglen = 8;
1333 t_in->taglen = 8;
1334 break;
1335 default:
1336 return( 1 );
1337 }
1338 break;
1339
1340 case MBEDTLS_MODE_CHACHAPOLY:
1341 t_out->fixed_ivlen = 12;
1342 t_in->fixed_ivlen = 12;
1343 t_out->maclen = 0;
1344 t_in->maclen = 0;
1345 switch( tag_mode )
1346 {
1347 case 0: /* Full tag */
1348 t_out->taglen = 16;
1349 t_in->taglen = 16;
1350 break;
1351 case 1: /* Partial tag */
1352 t_out->taglen = 8;
1353 t_in->taglen = 8;
1354 break;
1355 default:
1356 return( 1 );
1357 }
1358 break;
1359
1360 case MBEDTLS_MODE_STREAM:
1361 case MBEDTLS_MODE_CBC:
1362 t_out->fixed_ivlen = 0; /* redundant, must be 0 */
1363 t_in->fixed_ivlen = 0; /* redundant, must be 0 */
1364 t_out->taglen = 0;
1365 t_in->taglen = 0;
1366 switch( tag_mode )
1367 {
1368 case 0: /* Full tag */
1369 t_out->maclen = maclen;
1370 t_in->maclen = maclen;
1371 break;
1372 case 1: /* Partial tag */
1373 t_out->maclen = 10;
1374 t_in->maclen = 10;
1375 break;
1376 default:
1377 return( 1 );
1378 }
1379 break;
1380 default:
1381 return( 1 );
1382 break;
1383 }
1384
1385 /* Setup IV's */
1386
1387 memcpy( &t_in->iv_dec, iv_dec, sizeof( iv_dec ) );
1388 memcpy( &t_in->iv_enc, iv_enc, sizeof( iv_enc ) );
1389 memcpy( &t_out->iv_dec, iv_enc, sizeof( iv_enc ) );
1390 memcpy( &t_out->iv_enc, iv_dec, sizeof( iv_dec ) );
1391
Hanno Beckera0e20d02019-05-15 14:03:01 +01001392#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd856c822019-04-29 17:30:59 +01001393 /* Add CID */
1394 memcpy( &t_in->in_cid, cid0, cid0_len );
1395 memcpy( &t_in->out_cid, cid1, cid1_len );
1396 t_in->in_cid_len = cid0_len;
1397 t_in->out_cid_len = cid1_len;
1398 memcpy( &t_out->in_cid, cid1, cid1_len );
1399 memcpy( &t_out->out_cid, cid0, cid0_len );
1400 t_out->in_cid_len = cid1_len;
1401 t_out->out_cid_len = cid0_len;
Hanno Beckera0e20d02019-05-15 14:03:01 +01001402#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerd856c822019-04-29 17:30:59 +01001403
Hanno Becker81e16a32019-03-01 11:21:44 +00001404cleanup:
1405
Hanno Becker3ee54212019-04-04 16:31:26 +01001406 mbedtls_free( key0 );
1407 mbedtls_free( key1 );
Hanno Becker81e16a32019-03-01 11:21:44 +00001408
Hanno Beckera5780f12019-04-05 09:55:37 +01001409 return( ret );
Hanno Beckera18d1322018-01-03 14:27:32 +00001410}
1411
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001412/*
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02001413 * Populate a session structure for serialization tests.
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001414 * Choose dummy values, mostly non-0 to distinguish from the init default.
1415 */
1416static int ssl_populate_session( mbedtls_ssl_session *session,
Manuel Pégourié-Gonnard220403b2019-05-24 09:54:21 +02001417 int ticket_len,
1418 const char *crt_file )
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001419{
1420#if defined(MBEDTLS_HAVE_TIME)
1421 session->start = mbedtls_time( NULL ) - 42;
1422#endif
1423 session->ciphersuite = 0xabcd;
1424 session->compression = 1;
1425 session->id_len = sizeof( session->id );
1426 memset( session->id, 66, session->id_len );
Manuel Pégourié-Gonnard220403b2019-05-24 09:54:21 +02001427 memset( session->master, 17, sizeof( session->master ) );
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001428
Manuel Pégourié-Gonnard1f6033a2019-05-24 10:17:52 +02001429#if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001430 if( strlen( crt_file ) != 0 )
1431 {
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02001432 mbedtls_x509_crt tmp_crt;
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001433 int ret;
Manuel Pégourié-Gonnard6b840702019-05-24 09:40:17 +02001434
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02001435 mbedtls_x509_crt_init( &tmp_crt );
1436 ret = mbedtls_x509_crt_parse_file( &tmp_crt, crt_file );
1437 if( ret != 0 )
1438 return( ret );
1439
1440#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1441 /* Move temporary CRT. */
Manuel Pégourié-Gonnard6b840702019-05-24 09:40:17 +02001442 session->peer_cert = mbedtls_calloc( 1, sizeof( *session->peer_cert ) );
1443 if( session->peer_cert == NULL )
1444 return( -1 );
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02001445 *session->peer_cert = tmp_crt;
1446 memset( &tmp_crt, 0, sizeof( tmp_crt ) );
1447#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
1448 /* Calculate digest of temporary CRT. */
1449 session->peer_cert_digest =
1450 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
1451 if( session->peer_cert_digest == NULL )
1452 return( -1 );
1453 ret = mbedtls_md( mbedtls_md_info_from_type(
1454 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
1455 tmp_crt.raw.p, tmp_crt.raw.len,
1456 session->peer_cert_digest );
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001457 if( ret != 0 )
1458 return( ret );
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02001459 session->peer_cert_digest_type =
1460 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
1461 session->peer_cert_digest_len =
1462 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
1463#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
1464
1465 mbedtls_x509_crt_free( &tmp_crt );
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001466 }
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02001467#else /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_FS_IO */
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001468 (void) crt_file;
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02001469#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_FS_IO */
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001470 session->verify_result = 0xdeadbeef;
1471
1472#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
1473 if( ticket_len != 0 )
1474 {
1475 session->ticket = mbedtls_calloc( 1, ticket_len );
Manuel Pégourié-Gonnard220403b2019-05-24 09:54:21 +02001476 if( session->ticket == NULL )
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001477 return( -1 );
1478 memset( session->ticket, 33, ticket_len );
1479 }
1480 session->ticket_len = ticket_len;
1481 session->ticket_lifetime = 86401;
1482#else
1483 (void) ticket_len;
1484#endif
1485
1486#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1487 session->mfl_code = 1;
1488#endif
1489#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1490 session->trunc_hmac = 1;
1491#endif
1492#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1493 session->encrypt_then_mac = 1;
1494#endif
1495
1496 return( 0 );
1497}
1498
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001499/*
1500 * Perform data exchanging between \p ssl_1 and \p ssl_2 and check if the
1501 * message was sent in the correct number of fragments.
1502 *
1503 * /p ssl_1 and /p ssl_2 Endpoints represented by mbedtls_ssl_context. Both
1504 * of them must be initialized and connected beforehand.
1505 * /p msg_len_1 and /p msg_len_2 specify the size of the message to send.
1506 * /p expected_fragments_1 and /p expected_fragments_2 determine in how many
1507 * fragments the message should be sent.
1508 * expected_fragments is 0: can be used for DTLS testing while the message
1509 * size is larger than MFL. In that case the message
1510 * cannot be fragmented and sent to the second endpoint.
1511 * This value can be used for negative tests.
1512 * expected_fragments is 1: can be used for TLS/DTLS testing while the
1513 * message size is below MFL
1514 * expected_fragments > 1: can be used for TLS testing while the message
1515 * size is larger than MFL
1516 *
1517 * \retval 0 on success, otherwise error code.
1518 */
1519int mbedtls_exchange_data( mbedtls_ssl_context *ssl_1,
1520 int msg_len_1, const int expected_fragments_1,
1521 mbedtls_ssl_context *ssl_2,
1522 int msg_len_2, const int expected_fragments_2 )
1523{
1524 unsigned char *msg_buf_1 = malloc( msg_len_1 );
1525 unsigned char *msg_buf_2 = malloc( msg_len_2 );
1526 unsigned char *in_buf_1 = malloc( msg_len_2 );
1527 unsigned char *in_buf_2 = malloc( msg_len_1 );
1528 int msg_type, ret = -1;
1529
1530 /* Perform this test with two message types. At first use a message
1531 * consisting of only 0x00 for the client and only 0xFF for the server.
1532 * At the second time use message with generated data */
1533 for( msg_type = 0; msg_type < 2; msg_type++ )
1534 {
1535 int written_1 = 0;
1536 int written_2 = 0;
1537 int read_1 = 0;
1538 int read_2 = 0;
1539 int fragments_1 = 0;
1540 int fragments_2 = 0;
1541
1542 if( msg_type == 0 )
1543 {
1544 memset( msg_buf_1, 0x00, msg_len_1 );
1545 memset( msg_buf_2, 0xff, msg_len_2 );
1546 }
1547 else
1548 {
1549 int i, j = 0;
1550 for( i = 0; i < msg_len_1; i++ )
1551 {
1552 msg_buf_1[i] = j++ & 0xFF;
1553 }
1554 for( i = 0; i < msg_len_2; i++ )
1555 {
1556 msg_buf_2[i] = ( j -= 5 ) & 0xFF;
1557 }
1558 }
1559
1560 while( read_1 < msg_len_2 || read_2 < msg_len_1 )
1561 {
1562 /* ssl_1 sending */
1563 if( msg_len_1 > written_1 )
1564 {
1565 ret = mbedtls_ssl_write_fragment( ssl_1, msg_buf_1,
1566 msg_len_1, &written_1,
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001567 expected_fragments_1 );
1568 if( expected_fragments_1 == 0 )
1569 {
1570 /* This error is expected when the message is too large and
1571 * cannot be fragmented */
1572 TEST_ASSERT( ret == MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1573 msg_len_1 = 0;
1574 }
1575 else
1576 {
1577 TEST_ASSERT( ret == 0 );
1578 }
1579 }
1580
1581 /* ssl_2 sending */
1582 if( msg_len_2 > written_2 )
1583 {
1584 ret = mbedtls_ssl_write_fragment( ssl_2, msg_buf_2,
1585 msg_len_2, &written_2,
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001586 expected_fragments_2 );
1587 if( expected_fragments_2 == 0 )
1588 {
1589 /* This error is expected when the message is too large and
1590 * cannot be fragmented */
1591 TEST_ASSERT( ret == MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1592 msg_len_2 = 0;
1593 }
1594 else
1595 {
1596 TEST_ASSERT( ret == 0 );
1597 }
1598 }
1599
1600 /* ssl_1 reading */
1601 if( read_1 < msg_len_2 )
1602 {
1603 ret = mbedtls_ssl_read_fragment( ssl_1, in_buf_1,
1604 msg_len_2, &read_1,
Piotr Nowicki438bf3b2020-03-10 12:59:10 +01001605 &fragments_2,
1606 expected_fragments_2 );
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001607 TEST_ASSERT( ret == 0 );
1608 }
1609
1610 /* ssl_2 reading */
1611 if( read_2 < msg_len_1 )
1612 {
1613 ret = mbedtls_ssl_read_fragment( ssl_2, in_buf_2,
1614 msg_len_1, &read_2,
Piotr Nowicki438bf3b2020-03-10 12:59:10 +01001615 &fragments_1,
1616 expected_fragments_1 );
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001617 TEST_ASSERT( ret == 0 );
1618 }
1619 }
1620
1621 ret = -1;
1622 TEST_ASSERT( 0 == memcmp( msg_buf_1, in_buf_2, msg_len_1 ) );
1623 TEST_ASSERT( 0 == memcmp( msg_buf_2, in_buf_1, msg_len_2 ) );
1624 TEST_ASSERT( fragments_1 == expected_fragments_1 );
1625 TEST_ASSERT( fragments_2 == expected_fragments_2 );
1626 }
1627
1628 ret = 0;
1629
1630exit:
1631 free( msg_buf_1 );
1632 free( in_buf_1 );
1633 free( msg_buf_2 );
1634 free( in_buf_2 );
1635
1636 return ret;
1637}
1638
Piotr Nowicki95e9eb82020-02-14 11:33:34 +01001639/*
1640 * Perform data exchanging between \p ssl_1 and \p ssl_2. Both of endpoints
1641 * must be initialized and connected beforehand.
1642 *
1643 * \retval 0 on success, otherwise error code.
1644 */
1645int exchange_data( mbedtls_ssl_context *ssl_1,
1646 mbedtls_ssl_context *ssl_2 )
1647{
1648 return mbedtls_exchange_data( ssl_1, 256, 1,
1649 ssl_2, 256, 1 );
1650}
1651
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02001652#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
1653 defined(MBEDTLS_ENTROPY_C) && \
1654 defined(MBEDTLS_CTR_DRBG_C)
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001655void perform_handshake( handshake_test_options* options )
1656{
1657 /* forced_ciphersuite needs to last until the end of the handshake */
1658 int forced_ciphersuite[2];
1659 enum { BUFFSIZE = 17000 };
1660 mbedtls_endpoint client, server;
Gilles Peskineeccd8882020-03-10 12:19:08 +01001661#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001662 const char *psk_identity = "foo";
1663#endif
1664#if defined(MBEDTLS_TIMING_C)
1665 mbedtls_timing_delay_context timer_client, timer_server;
1666#endif
1667#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1668 unsigned char *context_buf = NULL;
1669 size_t context_buf_len;
1670#endif
1671#if defined(MBEDTLS_SSL_RENEGOTIATION)
1672 int ret = -1;
1673#endif
1674
1675
1676 mbedtls_test_message_queue server_queue, client_queue;
1677 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05001678 mbedtls_message_socket_init( &server_context );
1679 mbedtls_message_socket_init( &client_context );
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001680
1681 /* Client side */
1682 if( options->dtls != 0 )
1683 {
1684 TEST_ASSERT( mbedtls_endpoint_init( &client, MBEDTLS_SSL_IS_CLIENT,
1685 options->pk_alg, &client_context,
1686 &client_queue,
1687 &server_queue ) == 0 );
1688#if defined(MBEDTLS_TIMING_C)
1689 mbedtls_ssl_set_timer_cb( &client.ssl, &timer_client,
1690 mbedtls_timing_set_delay,
1691 mbedtls_timing_get_delay );
1692#endif
1693 }
1694 else
1695 {
1696 TEST_ASSERT( mbedtls_endpoint_init( &client, MBEDTLS_SSL_IS_CLIENT,
1697 options->pk_alg, NULL, NULL,
1698 NULL ) == 0 );
1699 }
1700 mbedtls_ssl_conf_min_version( &client.conf, MBEDTLS_SSL_MAJOR_VERSION_3,
1701 options->version );
1702 mbedtls_ssl_conf_max_version( &client.conf, MBEDTLS_SSL_MAJOR_VERSION_3,
1703 options->version );
1704
1705 if( strlen( options->cipher ) > 0 )
1706 {
1707 set_ciphersuite( &client.conf, options->cipher, forced_ciphersuite );
1708 }
Piotr Nowickibde7ee82020-02-21 10:59:50 +01001709
1710#if defined (MBEDTLS_DEBUG_C)
1711 if( options->cli_log_fun )
1712 {
1713 mbedtls_debug_set_threshold( 4 );
1714 mbedtls_ssl_conf_dbg( &client.conf, options->cli_log_fun,
1715 options->cli_log_obj );
1716 }
1717#endif
1718
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001719 /* Server side */
1720 if( options->dtls != 0 )
1721 {
1722 TEST_ASSERT( mbedtls_endpoint_init( &server, MBEDTLS_SSL_IS_SERVER,
1723 options->pk_alg, &server_context,
1724 &server_queue,
1725 &client_queue) == 0 );
1726#if defined(MBEDTLS_TIMING_C)
1727 mbedtls_ssl_set_timer_cb( &server.ssl, &timer_server,
1728 mbedtls_timing_set_delay,
1729 mbedtls_timing_get_delay );
1730#endif
1731 }
1732 else
1733 {
1734 TEST_ASSERT( mbedtls_endpoint_init( &server, MBEDTLS_SSL_IS_SERVER,
1735 options->pk_alg, NULL, NULL, NULL ) == 0 );
1736 }
Piotr Nowickibde7ee82020-02-21 10:59:50 +01001737
1738 mbedtls_ssl_conf_authmode( &server.conf, options->srv_auth_mode );
1739
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001740 mbedtls_ssl_conf_min_version( &server.conf, MBEDTLS_SSL_MAJOR_VERSION_3,
1741 options->version );
1742#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1743 TEST_ASSERT( mbedtls_ssl_conf_max_frag_len( &(server.conf),
1744 (unsigned char) options->mfl ) == 0 );
1745 TEST_ASSERT( mbedtls_ssl_conf_max_frag_len( &(client.conf),
1746 (unsigned char) options->mfl ) == 0 );
1747#else
1748 TEST_ASSERT( MBEDTLS_SSL_MAX_FRAG_LEN_NONE == options->mfl );
1749#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
1750
Gilles Peskineeccd8882020-03-10 12:19:08 +01001751#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001752 if( options->psk_str != NULL && options->psk_str->len > 0 )
1753 {
1754 TEST_ASSERT( mbedtls_ssl_conf_psk( &client.conf, options->psk_str->x,
1755 options->psk_str->len,
1756 (const unsigned char *) psk_identity,
1757 strlen( psk_identity ) ) == 0 );
1758
1759 TEST_ASSERT( mbedtls_ssl_conf_psk( &server.conf, options->psk_str->x,
1760 options->psk_str->len,
1761 (const unsigned char *) psk_identity,
1762 strlen( psk_identity ) ) == 0 );
1763
1764 mbedtls_ssl_conf_psk_cb( &server.conf, psk_dummy_callback, NULL );
1765 }
1766#endif
1767#if defined(MBEDTLS_SSL_RENEGOTIATION)
1768 if( options->renegotiate )
1769 {
1770 mbedtls_ssl_conf_renegotiation( &(server.conf),
1771 MBEDTLS_SSL_RENEGOTIATION_ENABLED );
1772 mbedtls_ssl_conf_renegotiation( &(client.conf),
1773 MBEDTLS_SSL_RENEGOTIATION_ENABLED );
1774
1775 mbedtls_ssl_conf_legacy_renegotiation( &(server.conf),
1776 options->legacy_renegotiation );
1777 mbedtls_ssl_conf_legacy_renegotiation( &(client.conf),
1778 options->legacy_renegotiation );
1779 }
1780#endif /* MBEDTLS_SSL_RENEGOTIATION */
1781
Piotr Nowickibde7ee82020-02-21 10:59:50 +01001782#if defined (MBEDTLS_DEBUG_C)
1783 if( options->srv_log_fun )
1784 {
1785 mbedtls_debug_set_threshold( 4 );
1786 mbedtls_ssl_conf_dbg( &server.conf, options->srv_log_fun,
1787 options->srv_log_obj );
1788 }
1789#endif
1790
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001791 TEST_ASSERT( mbedtls_mock_socket_connect( &(client.socket),
1792 &(server.socket),
1793 BUFFSIZE ) == 0 );
1794
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001795#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1796 if( options->resize_buffers != 0 )
1797 {
1798 /* Ensure that the buffer sizes are appropriate before resizes */
1799 TEST_ASSERT( client.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN );
1800 TEST_ASSERT( client.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN );
1801 TEST_ASSERT( server.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN );
1802 TEST_ASSERT( server.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN );
1803 }
1804#endif
1805
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001806 TEST_ASSERT( mbedtls_move_handshake_to_state( &(client.ssl),
1807 &(server.ssl),
1808 MBEDTLS_SSL_HANDSHAKE_OVER )
1809 == 0 );
1810 TEST_ASSERT( client.ssl.state == MBEDTLS_SSL_HANDSHAKE_OVER );
1811 TEST_ASSERT( server.ssl.state == MBEDTLS_SSL_HANDSHAKE_OVER );
1812
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001813#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1814 if( options->resize_buffers != 0 )
1815 {
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001816 if( options->version != MBEDTLS_SSL_MINOR_VERSION_0 &&
1817 options->version != MBEDTLS_SSL_MINOR_VERSION_1 )
1818 {
1819 /* A server, when using DTLS, might delay a buffer resize to happen
1820 * after it receives a message, so we force it. */
1821 TEST_ASSERT( exchange_data( &(client.ssl), &(server.ssl) ) == 0 );
1822
1823 TEST_ASSERT( client.ssl.out_buf_len ==
1824 mbedtls_ssl_get_output_buflen( &client.ssl ) );
1825 TEST_ASSERT( client.ssl.in_buf_len ==
1826 mbedtls_ssl_get_input_buflen( &client.ssl ) );
1827 TEST_ASSERT( server.ssl.out_buf_len ==
1828 mbedtls_ssl_get_output_buflen( &server.ssl ) );
1829 TEST_ASSERT( server.ssl.in_buf_len ==
1830 mbedtls_ssl_get_input_buflen( &server.ssl ) );
1831 }
1832 }
1833#endif
1834
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001835 if( options->cli_msg_len != 0 || options->srv_msg_len != 0 )
1836 {
1837 /* Start data exchanging test */
1838 TEST_ASSERT( mbedtls_exchange_data( &(client.ssl), options->cli_msg_len,
1839 options->expected_cli_fragments,
1840 &(server.ssl), options->srv_msg_len,
1841 options->expected_srv_fragments )
1842 == 0 );
1843 }
1844#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1845 if( options->serialize == 1 )
1846 {
1847 TEST_ASSERT( options->dtls == 1 );
1848
1849 TEST_ASSERT( mbedtls_ssl_context_save( &(server.ssl), NULL,
1850 0, &context_buf_len )
1851 == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1852
1853 context_buf = mbedtls_calloc( 1, context_buf_len );
1854 TEST_ASSERT( context_buf != NULL );
1855
1856 TEST_ASSERT( mbedtls_ssl_context_save( &(server.ssl), context_buf,
1857 context_buf_len,
1858 &context_buf_len ) == 0 );
1859
1860 mbedtls_ssl_free( &(server.ssl) );
1861 mbedtls_ssl_init( &(server.ssl) );
1862
1863 TEST_ASSERT( mbedtls_ssl_setup( &(server.ssl), &(server.conf) ) == 0 );
1864
1865 mbedtls_ssl_set_bio( &( server.ssl ), &server_context,
1866 mbedtls_mock_tcp_send_msg,
1867 mbedtls_mock_tcp_recv_msg,
1868 NULL );
1869
1870#if defined(MBEDTLS_TIMING_C)
1871 mbedtls_ssl_set_timer_cb( &server.ssl, &timer_server,
1872 mbedtls_timing_set_delay,
1873 mbedtls_timing_get_delay );
1874#endif
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001875#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1876 if( options->resize_buffers != 0 )
1877 {
1878 /* Ensure that the buffer sizes are appropriate before resizes */
1879 TEST_ASSERT( server.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN );
1880 TEST_ASSERT( server.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN );
1881 }
1882#endif
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001883 TEST_ASSERT( mbedtls_ssl_context_load( &( server.ssl ), context_buf,
1884 context_buf_len ) == 0 );
1885
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001886#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1887 /* Validate buffer sizes after context deserialization */
1888 if( options->resize_buffers != 0 )
1889 {
1890 TEST_ASSERT( server.ssl.out_buf_len ==
1891 mbedtls_ssl_get_output_buflen( &server.ssl ) );
1892 TEST_ASSERT( server.ssl.in_buf_len ==
1893 mbedtls_ssl_get_input_buflen( &server.ssl ) );
1894 }
1895#endif
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001896 /* Retest writing/reading */
1897 if( options->cli_msg_len != 0 || options->srv_msg_len != 0 )
1898 {
1899 TEST_ASSERT( mbedtls_exchange_data( &(client.ssl),
1900 options->cli_msg_len,
1901 options->expected_cli_fragments,
1902 &(server.ssl),
1903 options->srv_msg_len,
1904 options->expected_srv_fragments )
1905 == 0 );
1906 }
1907 }
1908#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001909
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001910#if defined(MBEDTLS_SSL_RENEGOTIATION)
1911 if( options->renegotiate )
1912 {
1913 /* Start test with renegotiation */
1914 TEST_ASSERT( server.ssl.renego_status ==
1915 MBEDTLS_SSL_INITIAL_HANDSHAKE );
1916 TEST_ASSERT( client.ssl.renego_status ==
1917 MBEDTLS_SSL_INITIAL_HANDSHAKE );
1918
1919 /* After calling this function for the server, it only sends a handshake
1920 * request. All renegotiation should happen during data exchanging */
1921 TEST_ASSERT( mbedtls_ssl_renegotiate( &(server.ssl) ) == 0 );
1922 TEST_ASSERT( server.ssl.renego_status ==
1923 MBEDTLS_SSL_RENEGOTIATION_PENDING );
1924 TEST_ASSERT( client.ssl.renego_status ==
1925 MBEDTLS_SSL_INITIAL_HANDSHAKE );
1926
1927 TEST_ASSERT( exchange_data( &(client.ssl), &(server.ssl) ) == 0 );
1928 TEST_ASSERT( server.ssl.renego_status ==
1929 MBEDTLS_SSL_RENEGOTIATION_DONE );
1930 TEST_ASSERT( client.ssl.renego_status ==
1931 MBEDTLS_SSL_RENEGOTIATION_DONE );
1932
1933 /* After calling mbedtls_ssl_renegotiate for the client all renegotiation
1934 * should happen inside this function. However in this test, we cannot
1935 * perform simultaneous communication betwen client and server so this
1936 * function will return waiting error on the socket. All rest of
1937 * renegotiation should happen during data exchanging */
1938 ret = mbedtls_ssl_renegotiate( &(client.ssl) );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001939#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1940 if( options->resize_buffers != 0 )
1941 {
1942 /* Ensure that the buffer sizes are appropriate before resizes */
1943 TEST_ASSERT( client.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN );
1944 TEST_ASSERT( client.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN );
1945 }
1946#endif
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001947 TEST_ASSERT( ret == 0 ||
1948 ret == MBEDTLS_ERR_SSL_WANT_READ ||
1949 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
1950 TEST_ASSERT( server.ssl.renego_status ==
1951 MBEDTLS_SSL_RENEGOTIATION_DONE );
1952 TEST_ASSERT( client.ssl.renego_status ==
1953 MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS );
1954
1955 TEST_ASSERT( exchange_data( &(client.ssl), &(server.ssl) ) == 0 );
1956 TEST_ASSERT( server.ssl.renego_status ==
1957 MBEDTLS_SSL_RENEGOTIATION_DONE );
1958 TEST_ASSERT( client.ssl.renego_status ==
1959 MBEDTLS_SSL_RENEGOTIATION_DONE );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001960#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1961 /* Validate buffer sizes after renegotiation */
1962 if( options->resize_buffers != 0 )
1963 {
1964 TEST_ASSERT( client.ssl.out_buf_len ==
1965 mbedtls_ssl_get_output_buflen( &client.ssl ) );
1966 TEST_ASSERT( client.ssl.in_buf_len ==
1967 mbedtls_ssl_get_input_buflen( &client.ssl ) );
1968 TEST_ASSERT( server.ssl.out_buf_len ==
1969 mbedtls_ssl_get_output_buflen( &server.ssl ) );
1970 TEST_ASSERT( server.ssl.in_buf_len ==
1971 mbedtls_ssl_get_input_buflen( &server.ssl ) );
1972 }
1973#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001974 }
1975#endif /* MBEDTLS_SSL_RENEGOTIATION */
1976
1977exit:
1978 mbedtls_endpoint_free( &client, options->dtls != 0 ? &client_context : NULL );
1979 mbedtls_endpoint_free( &server, options->dtls != 0 ? &server_context : NULL );
Piotr Nowickibde7ee82020-02-21 10:59:50 +01001980#if defined (MBEDTLS_DEBUG_C)
1981 if( options->cli_log_fun || options->srv_log_fun )
1982 {
1983 mbedtls_debug_set_threshold( 0 );
1984 }
1985#endif
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001986#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1987 if( context_buf != NULL )
1988 mbedtls_free( context_buf );
1989#endif
1990}
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02001991#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001992
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02001993/* END_HEADER */
1994
1995/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001996 * depends_on:MBEDTLS_SSL_TLS_C
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02001997 * END_DEPENDENCIES
1998 */
1999
Janos Follath6264e662019-11-26 11:11:15 +00002000/* BEGIN_CASE */
2001void test_callback_buffer_sanity()
2002{
2003 enum { MSGLEN = 10 };
2004 mbedtls_test_buffer buf;
2005 unsigned char input[MSGLEN];
2006 unsigned char output[MSGLEN];
2007
2008 memset( input, 0, sizeof(input) );
2009
2010 /* Make sure calling put and get on NULL buffer results in error. */
2011 TEST_ASSERT( mbedtls_test_buffer_put( NULL, input, sizeof( input ) )
2012 == -1 );
2013 TEST_ASSERT( mbedtls_test_buffer_get( NULL, output, sizeof( output ) )
2014 == -1 );
2015 TEST_ASSERT( mbedtls_test_buffer_put( NULL, NULL, sizeof( input ) ) == -1 );
Andrzej Kurekf7774142020-01-22 06:34:59 -05002016
Janos Follath6264e662019-11-26 11:11:15 +00002017 TEST_ASSERT( mbedtls_test_buffer_put( NULL, NULL, 0 ) == -1 );
2018 TEST_ASSERT( mbedtls_test_buffer_get( NULL, NULL, 0 ) == -1 );
2019
2020 /* Make sure calling put and get on a buffer that hasn't been set up results
2021 * in eror. */
2022 mbedtls_test_buffer_init( &buf );
2023
2024 TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, sizeof( input ) ) == -1 );
2025 TEST_ASSERT( mbedtls_test_buffer_get( &buf, output, sizeof( output ) )
2026 == -1 );
2027 TEST_ASSERT( mbedtls_test_buffer_put( &buf, NULL, sizeof( input ) ) == -1 );
Andrzej Kurekf7774142020-01-22 06:34:59 -05002028
Janos Follath6264e662019-11-26 11:11:15 +00002029 TEST_ASSERT( mbedtls_test_buffer_put( &buf, NULL, 0 ) == -1 );
2030 TEST_ASSERT( mbedtls_test_buffer_get( &buf, NULL, 0 ) == -1 );
2031
Andrzej Kurekf7774142020-01-22 06:34:59 -05002032 /* Make sure calling put and get on NULL input only results in
2033 * error if the length is not zero, and that a NULL output is valid for data
2034 * dropping.
2035 */
Janos Follath6264e662019-11-26 11:11:15 +00002036
2037 TEST_ASSERT( mbedtls_test_buffer_setup( &buf, sizeof( input ) ) == 0 );
2038
2039 TEST_ASSERT( mbedtls_test_buffer_put( &buf, NULL, sizeof( input ) ) == -1 );
2040 TEST_ASSERT( mbedtls_test_buffer_get( &buf, NULL, sizeof( output ) )
Andrzej Kurekf7774142020-01-22 06:34:59 -05002041 == 0 );
Janos Follath6264e662019-11-26 11:11:15 +00002042 TEST_ASSERT( mbedtls_test_buffer_put( &buf, NULL, 0 ) == 0 );
2043 TEST_ASSERT( mbedtls_test_buffer_get( &buf, NULL, 0 ) == 0 );
2044
Piotr Nowickifb437d72020-01-13 16:59:12 +01002045 /* Make sure calling put several times in the row is safe */
2046
2047 TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, sizeof( input ) )
2048 == sizeof( input ) );
2049 TEST_ASSERT( mbedtls_test_buffer_get( &buf, output, 2 ) == 2 );
2050 TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, 1 ) == 1 );
2051 TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, 2 ) == 1 );
2052 TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, 2 ) == 0 );
2053
2054
Janos Follath6264e662019-11-26 11:11:15 +00002055exit:
2056
2057 mbedtls_test_buffer_free( &buf );
2058}
2059/* END_CASE */
2060
2061/*
2062 * Test if the implementation of `mbedtls_test_buffer` related functions is
2063 * correct and works as expected.
2064 *
2065 * That is
2066 * - If we try to put in \p put1 bytes then we can put in \p put1_ret bytes.
2067 * - Afterwards if we try to get \p get1 bytes then we can get \get1_ret bytes.
2068 * - Next, if we try to put in \p put1 bytes then we can put in \p put1_ret
2069 * bytes.
2070 * - Afterwards if we try to get \p get1 bytes then we can get \get1_ret bytes.
2071 * - All of the bytes we got match the bytes we put in in a FIFO manner.
2072 */
2073
2074/* BEGIN_CASE */
2075void test_callback_buffer( int size, int put1, int put1_ret,
2076 int get1, int get1_ret, int put2, int put2_ret,
2077 int get2, int get2_ret )
2078{
2079 enum { ROUNDS = 2 };
2080 size_t put[ROUNDS];
2081 int put_ret[ROUNDS];
2082 size_t get[ROUNDS];
2083 int get_ret[ROUNDS];
2084 mbedtls_test_buffer buf;
2085 unsigned char* input = NULL;
2086 size_t input_len;
2087 unsigned char* output = NULL;
2088 size_t output_len;
Janos Follath031827f2019-11-27 11:12:14 +00002089 size_t i, j, written, read;
Janos Follath6264e662019-11-26 11:11:15 +00002090
2091 mbedtls_test_buffer_init( &buf );
2092 TEST_ASSERT( mbedtls_test_buffer_setup( &buf, size ) == 0 );
2093
2094 /* Check the sanity of input parameters and initialise local variables. That
2095 * is, ensure that the amount of data is not negative and that we are not
2096 * expecting more to put or get than we actually asked for. */
2097 TEST_ASSERT( put1 >= 0 );
2098 put[0] = put1;
2099 put_ret[0] = put1_ret;
2100 TEST_ASSERT( put1_ret <= put1 );
2101 TEST_ASSERT( put2 >= 0 );
2102 put[1] = put2;
2103 put_ret[1] = put2_ret;
2104 TEST_ASSERT( put2_ret <= put2 );
2105
2106 TEST_ASSERT( get1 >= 0 );
2107 get[0] = get1;
2108 get_ret[0] = get1_ret;
2109 TEST_ASSERT( get1_ret <= get1 );
2110 TEST_ASSERT( get2 >= 0 );
2111 get[1] = get2;
2112 get_ret[1] = get2_ret;
2113 TEST_ASSERT( get2_ret <= get2 );
2114
2115 input_len = 0;
2116 /* Calculate actual input and output lengths */
2117 for( j = 0; j < ROUNDS; j++ )
2118 {
2119 if( put_ret[j] > 0 )
2120 {
2121 input_len += put_ret[j];
2122 }
2123 }
2124 /* In order to always have a valid pointer we always allocate at least 1
2125 * byte. */
2126 if( input_len == 0 )
2127 input_len = 1;
2128 ASSERT_ALLOC( input, input_len );
2129
2130 output_len = 0;
2131 for( j = 0; j < ROUNDS; j++ )
2132 {
2133 if( get_ret[j] > 0 )
2134 {
2135 output_len += get_ret[j];
2136 }
2137 }
2138 TEST_ASSERT( output_len <= input_len );
2139 /* In order to always have a valid pointer we always allocate at least 1
2140 * byte. */
2141 if( output_len == 0 )
2142 output_len = 1;
2143 ASSERT_ALLOC( output, output_len );
2144
2145 /* Fill up the buffer with structured data so that unwanted changes
2146 * can be detected */
2147 for( i = 0; i < input_len; i++ )
2148 {
2149 input[i] = i & 0xFF;
2150 }
2151
2152 written = read = 0;
2153 for( j = 0; j < ROUNDS; j++ )
2154 {
2155 TEST_ASSERT( put_ret[j] == mbedtls_test_buffer_put( &buf,
2156 input + written, put[j] ) );
2157 written += put_ret[j];
2158 TEST_ASSERT( get_ret[j] == mbedtls_test_buffer_get( &buf,
2159 output + read, get[j] ) );
2160 read += get_ret[j];
2161 TEST_ASSERT( read <= written );
2162 if( get_ret[j] > 0 )
2163 {
2164 TEST_ASSERT( memcmp( output + read - get_ret[j],
2165 input + read - get_ret[j], get_ret[j] )
2166 == 0 );
2167 }
2168 }
2169
2170exit:
2171
2172 mbedtls_free( input );
2173 mbedtls_free( output );
2174 mbedtls_test_buffer_free( &buf );
2175}
2176/* END_CASE */
2177
Janos Follath031827f2019-11-27 11:12:14 +00002178/*
Janos Follathc673c2c2019-12-02 15:47:26 +00002179 * Test if the implementation of `mbedtls_mock_socket` related I/O functions is
2180 * correct and works as expected on unconnected sockets.
2181 */
2182
2183/* BEGIN_CASE */
2184void ssl_mock_sanity( )
2185{
2186 enum { MSGLEN = 105 };
2187 unsigned char message[MSGLEN];
2188 unsigned char received[MSGLEN];
2189 mbedtls_mock_socket socket;
2190
2191 mbedtls_mock_socket_init( &socket );
2192 TEST_ASSERT( mbedtls_mock_tcp_send_b( &socket, message, MSGLEN ) < 0 );
2193 mbedtls_mock_socket_close( &socket );
2194 mbedtls_mock_socket_init( &socket );
2195 TEST_ASSERT( mbedtls_mock_tcp_recv_b( &socket, received, MSGLEN ) < 0 );
2196 mbedtls_mock_socket_close( &socket );
2197
2198 mbedtls_mock_socket_init( &socket );
2199 TEST_ASSERT( mbedtls_mock_tcp_send_nb( &socket, message, MSGLEN ) < 0 );
2200 mbedtls_mock_socket_close( &socket );
2201 mbedtls_mock_socket_init( &socket );
2202 TEST_ASSERT( mbedtls_mock_tcp_recv_nb( &socket, received, MSGLEN ) < 0 );
2203 mbedtls_mock_socket_close( &socket );
2204
2205exit:
2206
2207 mbedtls_mock_socket_close( &socket );
2208}
2209/* END_CASE */
2210
2211/*
2212 * Test if the implementation of `mbedtls_mock_socket` related functions can
2213 * send a single message from the client to the server.
Janos Follath031827f2019-11-27 11:12:14 +00002214 */
2215
2216/* BEGIN_CASE */
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002217void ssl_mock_tcp( int blocking )
Janos Follath031827f2019-11-27 11:12:14 +00002218{
Janos Follathc673c2c2019-12-02 15:47:26 +00002219 enum { MSGLEN = 105 };
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002220 enum { BUFLEN = MSGLEN / 5 };
Janos Follathc673c2c2019-12-02 15:47:26 +00002221 unsigned char message[MSGLEN];
2222 unsigned char received[MSGLEN];
2223 mbedtls_mock_socket client;
2224 mbedtls_mock_socket server;
2225 size_t written, read;
2226 int send_ret, recv_ret;
2227 mbedtls_ssl_send_t *send;
2228 mbedtls_ssl_recv_t *recv;
Janos Follathc673c2c2019-12-02 15:47:26 +00002229 unsigned i;
2230
2231 if( blocking == 0 )
2232 {
2233 send = mbedtls_mock_tcp_send_nb;
2234 recv = mbedtls_mock_tcp_recv_nb;
2235 }
2236 else
2237 {
2238 send = mbedtls_mock_tcp_send_b;
2239 recv = mbedtls_mock_tcp_recv_b;
2240 }
2241
2242 mbedtls_mock_socket_init( &client );
2243 mbedtls_mock_socket_init( &server );
2244
2245 /* Fill up the buffer with structured data so that unwanted changes
2246 * can be detected */
2247 for( i = 0; i < MSGLEN; i++ )
2248 {
2249 message[i] = i & 0xFF;
2250 }
2251
2252 /* Make sure that sending a message takes a few iterations. */
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002253 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, BUFLEN ) );
Janos Follathc673c2c2019-12-02 15:47:26 +00002254
2255 /* Send the message to the server */
2256 send_ret = recv_ret = 1;
2257 written = read = 0;
2258 while( send_ret != 0 || recv_ret != 0 )
2259 {
2260 send_ret = send( &client, message + written, MSGLEN - written );
2261
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002262 TEST_ASSERT( send_ret >= 0 );
2263 TEST_ASSERT( send_ret <= BUFLEN );
2264 written += send_ret;
2265
2266 /* If the buffer is full we can test blocking and non-blocking send */
2267 if ( send_ret == BUFLEN )
Janos Follathc673c2c2019-12-02 15:47:26 +00002268 {
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002269 int blocking_ret = send( &client, message , 1 );
2270 if ( blocking )
2271 {
2272 TEST_ASSERT( blocking_ret == 0 );
2273 }
2274 else
2275 {
2276 TEST_ASSERT( blocking_ret == MBEDTLS_ERR_SSL_WANT_WRITE );
2277 }
Janos Follathc673c2c2019-12-02 15:47:26 +00002278 }
Janos Follathc673c2c2019-12-02 15:47:26 +00002279
2280 recv_ret = recv( &server, received + read, MSGLEN - read );
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002281
2282 /* The result depends on whether any data was sent */
2283 if ( send_ret > 0 )
Janos Follathc673c2c2019-12-02 15:47:26 +00002284 {
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002285 TEST_ASSERT( recv_ret > 0 );
2286 TEST_ASSERT( recv_ret <= BUFLEN );
2287 read += recv_ret;
2288 }
2289 else if( blocking )
2290 {
2291 TEST_ASSERT( recv_ret == 0 );
Janos Follathc673c2c2019-12-02 15:47:26 +00002292 }
2293 else
2294 {
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002295 TEST_ASSERT( recv_ret == MBEDTLS_ERR_SSL_WANT_READ );
2296 recv_ret = 0;
Janos Follathc673c2c2019-12-02 15:47:26 +00002297 }
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002298
2299 /* If the buffer is empty we can test blocking and non-blocking read */
2300 if ( recv_ret == BUFLEN )
2301 {
2302 int blocking_ret = recv( &server, received, 1 );
2303 if ( blocking )
2304 {
2305 TEST_ASSERT( blocking_ret == 0 );
2306 }
2307 else
2308 {
2309 TEST_ASSERT( blocking_ret == MBEDTLS_ERR_SSL_WANT_READ );
2310 }
2311 }
Janos Follathc673c2c2019-12-02 15:47:26 +00002312 }
2313 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
2314
2315exit:
2316
2317 mbedtls_mock_socket_close( &client );
2318 mbedtls_mock_socket_close( &server );
2319}
2320/* END_CASE */
2321
2322/*
2323 * Test if the implementation of `mbedtls_mock_socket` related functions can
2324 * send messages in both direction at the same time (with the I/O calls
2325 * interleaving).
2326 */
2327
2328/* BEGIN_CASE */
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002329void ssl_mock_tcp_interleaving( int blocking )
Janos Follathc673c2c2019-12-02 15:47:26 +00002330{
Janos Follath031827f2019-11-27 11:12:14 +00002331 enum { ROUNDS = 2 };
2332 enum { MSGLEN = 105 };
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002333 enum { BUFLEN = MSGLEN / 5 };
Janos Follath031827f2019-11-27 11:12:14 +00002334 unsigned char message[ROUNDS][MSGLEN];
2335 unsigned char received[ROUNDS][MSGLEN];
2336 mbedtls_mock_socket client;
2337 mbedtls_mock_socket server;
2338 size_t written[ROUNDS];
2339 size_t read[ROUNDS];
2340 int send_ret[ROUNDS];
2341 int recv_ret[ROUNDS];
2342 unsigned i, j, progress;
Janos Follath3766ba52019-11-27 13:31:42 +00002343 mbedtls_ssl_send_t *send;
2344 mbedtls_ssl_recv_t *recv;
Janos Follath3766ba52019-11-27 13:31:42 +00002345
2346 if( blocking == 0 )
2347 {
2348 send = mbedtls_mock_tcp_send_nb;
2349 recv = mbedtls_mock_tcp_recv_nb;
2350 }
2351 else
2352 {
2353 send = mbedtls_mock_tcp_send_b;
2354 recv = mbedtls_mock_tcp_recv_b;
2355 }
Janos Follath031827f2019-11-27 11:12:14 +00002356
2357 mbedtls_mock_socket_init( &client );
2358 mbedtls_mock_socket_init( &server );
2359
2360 /* Fill up the buffers with structured data so that unwanted changes
2361 * can be detected */
2362 for( i = 0; i < ROUNDS; i++ )
2363 {
2364 for( j = 0; j < MSGLEN; j++ )
2365 {
2366 message[i][j] = ( i * MSGLEN + j ) & 0xFF;
2367 }
2368 }
2369
Janos Follath031827f2019-11-27 11:12:14 +00002370 /* Make sure that sending a message takes a few iterations. */
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002371 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, BUFLEN ) );
Janos Follath031827f2019-11-27 11:12:14 +00002372
Janos Follath031827f2019-11-27 11:12:14 +00002373 /* Send the message from both sides, interleaving. */
2374 progress = 1;
2375 for( i = 0; i < ROUNDS; i++ )
2376 {
2377 written[i] = 0;
2378 read[i] = 0;
2379 }
2380 /* This loop does not stop as long as there was a successful write or read
2381 * of at least one byte on either side. */
2382 while( progress != 0 )
2383 {
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002384 mbedtls_mock_socket *socket;
Janos Follath031827f2019-11-27 11:12:14 +00002385
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002386 for( i = 0; i < ROUNDS; i++ )
Janos Follath3766ba52019-11-27 13:31:42 +00002387 {
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002388 /* First sending is from the client */
2389 socket = ( i % 2 == 0 ) ? ( &client ) : ( &server );
Janos Follath031827f2019-11-27 11:12:14 +00002390
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002391 send_ret[i] = send( socket, message[i] + written[i],
2392 MSGLEN - written[i] );
2393 TEST_ASSERT( send_ret[i] >= 0 );
2394 TEST_ASSERT( send_ret[i] <= BUFLEN );
2395 written[i] += send_ret[i];
Janos Follath031827f2019-11-27 11:12:14 +00002396
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002397 /* If the buffer is full we can test blocking and non-blocking
2398 * send */
2399 if ( send_ret[i] == BUFLEN )
2400 {
2401 int blocking_ret = send( socket, message[i] , 1 );
2402 if ( blocking )
2403 {
2404 TEST_ASSERT( blocking_ret == 0 );
2405 }
2406 else
2407 {
2408 TEST_ASSERT( blocking_ret == MBEDTLS_ERR_SSL_WANT_WRITE );
2409 }
2410 }
Janos Follath3766ba52019-11-27 13:31:42 +00002411 }
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002412
2413 for( i = 0; i < ROUNDS; i++ )
Janos Follath3766ba52019-11-27 13:31:42 +00002414 {
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002415 /* First receiving is from the server */
2416 socket = ( i % 2 == 0 ) ? ( &server ) : ( &client );
2417
2418 recv_ret[i] = recv( socket, received[i] + read[i],
2419 MSGLEN - read[i] );
2420
2421 /* The result depends on whether any data was sent */
2422 if ( send_ret[i] > 0 )
2423 {
2424 TEST_ASSERT( recv_ret[i] > 0 );
2425 TEST_ASSERT( recv_ret[i] <= BUFLEN );
2426 read[i] += recv_ret[i];
2427 }
2428 else if( blocking )
2429 {
2430 TEST_ASSERT( recv_ret[i] == 0 );
2431 }
2432 else
2433 {
2434 TEST_ASSERT( recv_ret[i] == MBEDTLS_ERR_SSL_WANT_READ );
2435 recv_ret[i] = 0;
2436 }
2437
2438 /* If the buffer is empty we can test blocking and non-blocking
2439 * read */
2440 if ( recv_ret[i] == BUFLEN )
2441 {
2442 int blocking_ret = recv( socket, received[i], 1 );
2443 if ( blocking )
2444 {
2445 TEST_ASSERT( blocking_ret == 0 );
2446 }
2447 else
2448 {
2449 TEST_ASSERT( blocking_ret == MBEDTLS_ERR_SSL_WANT_READ );
2450 }
2451 }
Janos Follath3766ba52019-11-27 13:31:42 +00002452 }
Janos Follath031827f2019-11-27 11:12:14 +00002453
2454 progress = 0;
2455 for( i = 0; i < ROUNDS; i++ )
2456 {
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002457 progress += send_ret[i] + recv_ret[i];
Janos Follath031827f2019-11-27 11:12:14 +00002458 }
2459 }
2460
2461 for( i = 0; i < ROUNDS; i++ )
2462 TEST_ASSERT( memcmp( message[i], received[i], MSGLEN ) == 0 );
2463
2464exit:
2465
2466 mbedtls_mock_socket_close( &client );
2467 mbedtls_mock_socket_close( &server );
2468}
2469/* END_CASE */
2470
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002471/* BEGIN_CASE */
2472void ssl_message_queue_sanity( )
2473{
2474 mbedtls_test_message_queue queue;
2475
2476 /* Trying to push/pull to an empty queue */
2477 TEST_ASSERT( mbedtls_test_message_queue_push_info( NULL, 1 )
2478 == MBEDTLS_TEST_ERROR_ARG_NULL );
2479 TEST_ASSERT( mbedtls_test_message_queue_pop_info( NULL, 1 )
2480 == MBEDTLS_TEST_ERROR_ARG_NULL );
2481
Andrzej Kurek89bdc582020-03-09 06:29:43 -04002482 TEST_ASSERT( mbedtls_test_message_queue_setup( &queue, 3 ) == 0 );
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002483 TEST_ASSERT( queue.capacity == 3 );
2484 TEST_ASSERT( queue.num == 0 );
2485
2486exit:
2487 mbedtls_test_message_queue_free( &queue );
2488}
2489/* END_CASE */
2490
2491/* BEGIN_CASE */
2492void ssl_message_queue_basic( )
2493{
2494 mbedtls_test_message_queue queue;
2495
Andrzej Kurek89bdc582020-03-09 06:29:43 -04002496 TEST_ASSERT( mbedtls_test_message_queue_setup( &queue, 3 ) == 0 );
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002497
2498 /* Sanity test - 3 pushes and 3 pops with sufficient space */
2499 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 );
2500 TEST_ASSERT( queue.capacity == 3 );
2501 TEST_ASSERT( queue.num == 1 );
2502 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 );
2503 TEST_ASSERT( queue.capacity == 3 );
2504 TEST_ASSERT( queue.num == 2 );
2505 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 2 ) == 2 );
2506 TEST_ASSERT( queue.capacity == 3 );
2507 TEST_ASSERT( queue.num == 3 );
2508
2509 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 );
2510 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 );
2511 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 2 ) == 2 );
2512
2513exit:
2514 mbedtls_test_message_queue_free( &queue );
2515}
2516/* END_CASE */
2517
2518/* BEGIN_CASE */
2519void ssl_message_queue_overflow_underflow( )
2520{
2521 mbedtls_test_message_queue queue;
2522
Andrzej Kurek89bdc582020-03-09 06:29:43 -04002523 TEST_ASSERT( mbedtls_test_message_queue_setup( &queue, 3 ) == 0 );
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002524
2525 /* 4 pushes (last one with an error), 4 pops (last one with an error) */
2526 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 );
2527 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 );
2528 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 2 ) == 2 );
2529 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 3 )
Andrzej Kurekf46b9122020-02-07 08:19:00 -05002530 == MBEDTLS_ERR_SSL_WANT_WRITE );
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002531
2532 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 );
2533 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 );
2534 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 2 ) == 2 );
2535
2536 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 )
Andrzej Kurekf46b9122020-02-07 08:19:00 -05002537 == MBEDTLS_ERR_SSL_WANT_READ );
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002538
2539exit:
2540 mbedtls_test_message_queue_free( &queue );
2541}
2542/* END_CASE */
2543
2544/* BEGIN_CASE */
2545void ssl_message_queue_interleaved( )
2546{
2547 mbedtls_test_message_queue queue;
2548
Andrzej Kurek89bdc582020-03-09 06:29:43 -04002549 TEST_ASSERT( mbedtls_test_message_queue_setup( &queue, 3 ) == 0 );
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002550
2551 /* Interleaved test - [2 pushes, 1 pop] twice, and then two pops
2552 * (to wrap around the buffer) */
2553 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 );
2554 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 );
2555
2556 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 );
2557
2558 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 2 ) == 2 );
2559 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 3 ) == 3 );
2560
2561 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 );
2562 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 2 ) == 2 );
2563
2564 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 5 ) == 5 );
2565 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 8 ) == 8 );
2566
2567 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 3 ) == 3 );
2568
2569 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 5 ) == 5 );
2570
2571 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 8 ) == 8 );
2572
2573exit:
2574 mbedtls_test_message_queue_free( &queue );
2575}
2576/* END_CASE */
2577
2578/* BEGIN_CASE */
2579void ssl_message_queue_insufficient_buffer( )
2580{
2581 mbedtls_test_message_queue queue;
2582 size_t message_len = 10;
2583 size_t buffer_len = 5;
2584
Andrzej Kurek89bdc582020-03-09 06:29:43 -04002585 TEST_ASSERT( mbedtls_test_message_queue_setup( &queue, 1 ) == 0 );
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002586
2587 /* Popping without a sufficient buffer */
2588 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, message_len )
2589 == (int) message_len );
2590 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, buffer_len )
2591 == (int) buffer_len );
2592exit:
2593 mbedtls_test_message_queue_free( &queue );
2594}
2595/* END_CASE */
2596
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002597/* BEGIN_CASE */
2598void ssl_message_mock_uninitialized( )
2599{
2600 enum { MSGLEN = 10 };
2601 unsigned char message[MSGLEN], received[MSGLEN];
2602 mbedtls_mock_socket client, server;
2603 mbedtls_test_message_queue server_queue, client_queue;
2604 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05002605 mbedtls_message_socket_init( &server_context );
2606 mbedtls_message_socket_init( &client_context );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002607
2608 /* Send with a NULL context */
2609 TEST_ASSERT( mbedtls_mock_tcp_send_msg( NULL, message, MSGLEN )
2610 == MBEDTLS_TEST_ERROR_CONTEXT_ERROR );
2611
2612 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( NULL, message, MSGLEN )
2613 == MBEDTLS_TEST_ERROR_CONTEXT_ERROR );
2614
2615 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 1,
2616 &server,
2617 &server_context ) == 0 );
2618
2619 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 1,
2620 &client,
2621 &client_context ) == 0 );
2622
2623 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, MSGLEN )
2624 == MBEDTLS_TEST_ERROR_SEND_FAILED );
2625
2626 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
Andrzej Kurekf46b9122020-02-07 08:19:00 -05002627 == MBEDTLS_ERR_SSL_WANT_READ );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002628
2629 /* Push directly to a queue to later simulate a disconnected behavior */
2630 TEST_ASSERT( mbedtls_test_message_queue_push_info( &server_queue, MSGLEN )
2631 == MSGLEN );
2632
2633 /* Test if there's an error when trying to read from a disconnected
2634 * socket */
2635 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
2636 == MBEDTLS_TEST_ERROR_RECV_FAILED );
2637 exit:
2638 mbedtls_message_socket_close( &server_context );
2639 mbedtls_message_socket_close( &client_context );
2640}
2641/* END_CASE */
2642
2643/* BEGIN_CASE */
2644void ssl_message_mock_basic( )
2645{
2646 enum { MSGLEN = 10 };
2647 unsigned char message[MSGLEN], received[MSGLEN];
2648 mbedtls_mock_socket client, server;
2649 unsigned i;
2650 mbedtls_test_message_queue server_queue, client_queue;
2651 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05002652 mbedtls_message_socket_init( &server_context );
2653 mbedtls_message_socket_init( &client_context );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002654
2655 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 1,
2656 &server,
2657 &server_context ) == 0 );
2658
2659 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 1,
2660 &client,
2661 &client_context ) == 0 );
2662
2663 /* Fill up the buffer with structured data so that unwanted changes
2664 * can be detected */
2665 for( i = 0; i < MSGLEN; i++ )
2666 {
2667 message[i] = i & 0xFF;
2668 }
2669 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
2670 MSGLEN ) );
2671
2672 /* Send the message to the server */
2673 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2674 MSGLEN ) == MSGLEN );
2675
2676 /* Read from the server */
2677 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
2678 == MSGLEN );
2679
2680 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
2681 memset( received, 0, MSGLEN );
2682
2683 /* Send the message to the client */
2684 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &server_context, message,
2685 MSGLEN ) == MSGLEN );
2686
2687 /* Read from the client */
2688 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &client_context, received, MSGLEN )
2689 == MSGLEN );
2690 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
2691
2692 exit:
2693 mbedtls_message_socket_close( &server_context );
2694 mbedtls_message_socket_close( &client_context );
2695}
2696/* END_CASE */
2697
2698/* BEGIN_CASE */
2699void ssl_message_mock_queue_overflow_underflow( )
2700{
2701 enum { MSGLEN = 10 };
2702 unsigned char message[MSGLEN], received[MSGLEN];
2703 mbedtls_mock_socket client, server;
2704 unsigned i;
2705 mbedtls_test_message_queue server_queue, client_queue;
2706 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05002707 mbedtls_message_socket_init( &server_context );
2708 mbedtls_message_socket_init( &client_context );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002709
2710 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 2,
2711 &server,
2712 &server_context ) == 0 );
2713
2714 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 2,
2715 &client,
2716 &client_context ) == 0 );
2717
2718 /* Fill up the buffer with structured data so that unwanted changes
2719 * can be detected */
2720 for( i = 0; i < MSGLEN; i++ )
2721 {
2722 message[i] = i & 0xFF;
2723 }
2724 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
2725 MSGLEN*2 ) );
2726
2727 /* Send three message to the server, last one with an error */
2728 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2729 MSGLEN - 1 ) == MSGLEN - 1 );
2730
2731 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2732 MSGLEN ) == MSGLEN );
2733
2734 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2735 MSGLEN )
Andrzej Kurekf46b9122020-02-07 08:19:00 -05002736 == MBEDTLS_ERR_SSL_WANT_WRITE );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002737
2738 /* Read three messages from the server, last one with an error */
2739 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received,
2740 MSGLEN - 1 ) == MSGLEN - 1 );
2741
2742 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
2743 == MSGLEN );
2744
2745 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
2746
2747 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
Andrzej Kurekf46b9122020-02-07 08:19:00 -05002748 == MBEDTLS_ERR_SSL_WANT_READ );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002749
2750 exit:
2751 mbedtls_message_socket_close( &server_context );
2752 mbedtls_message_socket_close( &client_context );
2753}
2754/* END_CASE */
2755
2756/* BEGIN_CASE */
2757void ssl_message_mock_socket_overflow( )
2758{
2759 enum { MSGLEN = 10 };
2760 unsigned char message[MSGLEN], received[MSGLEN];
2761 mbedtls_mock_socket client, server;
2762 unsigned i;
2763 mbedtls_test_message_queue server_queue, client_queue;
2764 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05002765 mbedtls_message_socket_init( &server_context );
2766 mbedtls_message_socket_init( &client_context );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002767
2768 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 2,
2769 &server,
2770 &server_context ) == 0 );
2771
2772 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 2,
2773 &client,
2774 &client_context ) == 0 );
2775
2776 /* Fill up the buffer with structured data so that unwanted changes
2777 * can be detected */
2778 for( i = 0; i < MSGLEN; i++ )
2779 {
2780 message[i] = i & 0xFF;
2781 }
2782 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
2783 MSGLEN ) );
2784
2785 /* Send two message to the server, second one with an error */
2786 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2787 MSGLEN ) == MSGLEN );
2788
2789 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2790 MSGLEN )
2791 == MBEDTLS_TEST_ERROR_SEND_FAILED );
2792
2793 /* Read the only message from the server */
2794 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
2795 == MSGLEN );
2796
2797 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
2798
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_truncated( )
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 memset( received, 0, MSGLEN );
2826 /* Fill up the buffer with structured data so that unwanted changes
2827 * can be detected */
2828 for( i = 0; i < MSGLEN; i++ )
2829 {
2830 message[i] = i & 0xFF;
2831 }
2832 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
2833 2 * MSGLEN ) );
2834
2835 /* Send two messages to the server, the second one small enough to fit in the
2836 * receiver's buffer. */
2837 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2838 MSGLEN ) == MSGLEN );
2839 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2840 MSGLEN / 2 ) == MSGLEN / 2 );
2841 /* Read a truncated message from the server */
2842 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN/2 )
2843 == MSGLEN/2 );
2844
2845 /* Test that the first half of the message is valid, and second one isn't */
2846 TEST_ASSERT( memcmp( message, received, MSGLEN/2 ) == 0 );
2847 TEST_ASSERT( memcmp( message + MSGLEN/2, received + MSGLEN/2, MSGLEN/2 )
2848 != 0 );
2849 memset( received, 0, MSGLEN );
2850
2851 /* Read a full message from the server */
2852 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN/2 )
2853 == MSGLEN / 2 );
2854
2855 /* Test that the first half of the message is valid */
2856 TEST_ASSERT( memcmp( message, received, MSGLEN/2 ) == 0 );
2857
2858 exit:
2859 mbedtls_message_socket_close( &server_context );
2860 mbedtls_message_socket_close( &client_context );
2861}
2862/* END_CASE */
2863
2864/* BEGIN_CASE */
2865void ssl_message_mock_socket_read_error( )
2866{
2867 enum { MSGLEN = 10 };
2868 unsigned char message[MSGLEN], received[MSGLEN];
2869 mbedtls_mock_socket client, server;
2870 unsigned i;
2871 mbedtls_test_message_queue server_queue, client_queue;
2872 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05002873 mbedtls_message_socket_init( &server_context );
2874 mbedtls_message_socket_init( &client_context );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002875
2876 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 1,
2877 &server,
2878 &server_context ) == 0 );
2879
2880 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 1,
2881 &client,
2882 &client_context ) == 0 );
2883
2884 /* Fill up the buffer with structured data so that unwanted changes
2885 * can be detected */
2886 for( i = 0; i < MSGLEN; i++ )
2887 {
2888 message[i] = i & 0xFF;
2889 }
2890 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
2891 MSGLEN ) );
2892
2893 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2894 MSGLEN ) == MSGLEN );
2895
2896 /* Force a read error by disconnecting the socket by hand */
2897 server.status = 0;
2898 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
2899 == MBEDTLS_TEST_ERROR_RECV_FAILED );
2900 /* Return to a valid state */
2901 server.status = MBEDTLS_MOCK_SOCKET_CONNECTED;
2902
2903 memset( received, 0, sizeof( received ) );
2904
2905 /* Test that even though the server tried to read once disconnected, the
2906 * continuity is preserved */
2907 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
2908 == MSGLEN );
2909
2910 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
2911
2912 exit:
2913 mbedtls_message_socket_close( &server_context );
2914 mbedtls_message_socket_close( &client_context );
2915}
2916/* END_CASE */
2917
2918/* BEGIN_CASE */
2919void ssl_message_mock_interleaved_one_way( )
2920{
2921 enum { MSGLEN = 10 };
2922 unsigned char message[MSGLEN], received[MSGLEN];
2923 mbedtls_mock_socket client, server;
2924 unsigned i;
2925 mbedtls_test_message_queue server_queue, client_queue;
2926 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05002927 mbedtls_message_socket_init( &server_context );
2928 mbedtls_message_socket_init( &client_context );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002929
2930 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 3,
2931 &server,
2932 &server_context ) == 0 );
2933
2934 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 3,
2935 &client,
2936 &client_context ) == 0 );
2937
2938 /* Fill up the buffer with structured data so that unwanted changes
2939 * can be detected */
2940 for( i = 0; i < MSGLEN; i++ )
2941 {
2942 message[i] = i & 0xFF;
2943 }
2944 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
2945 MSGLEN*3 ) );
2946
2947 /* Interleaved test - [2 sends, 1 read] twice, and then two reads
2948 * (to wrap around the buffer) */
2949 for( i = 0; i < 2; i++ )
2950 {
2951 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2952 MSGLEN ) == MSGLEN );
2953
2954 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2955 MSGLEN ) == MSGLEN );
2956
2957 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received,
2958 MSGLEN ) == MSGLEN );
2959 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
2960 memset( received, 0, sizeof( received ) );
2961 }
2962
2963 for( i = 0; i < 2; i++ )
2964 {
2965 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received,
2966 MSGLEN ) == MSGLEN );
2967
2968 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
2969 }
2970 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
Andrzej Kurekf46b9122020-02-07 08:19:00 -05002971 == MBEDTLS_ERR_SSL_WANT_READ );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002972 exit:
2973 mbedtls_message_socket_close( &server_context );
2974 mbedtls_message_socket_close( &client_context );
2975}
2976/* END_CASE */
2977
2978/* BEGIN_CASE */
2979void ssl_message_mock_interleaved_two_ways( )
2980{
2981 enum { MSGLEN = 10 };
2982 unsigned char message[MSGLEN], received[MSGLEN];
2983 mbedtls_mock_socket client, server;
2984 unsigned i;
2985 mbedtls_test_message_queue server_queue, client_queue;
2986 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05002987 mbedtls_message_socket_init( &server_context );
2988 mbedtls_message_socket_init( &client_context );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002989
2990 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 3,
2991 &server,
2992 &server_context ) == 0 );
2993
2994 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 3,
2995 &client,
2996 &client_context ) == 0 );
2997
2998 /* Fill up the buffer with structured data so that unwanted changes
2999 * can be detected */
3000 for( i = 0; i < MSGLEN; i++ )
3001 {
3002 message[i] = i & 0xFF;
3003 }
3004 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
3005 MSGLEN*3 ) );
3006
3007 /* Interleaved test - [2 sends, 1 read] twice, both ways, and then two reads
3008 * (to wrap around the buffer) both ways. */
3009 for( i = 0; i < 2; i++ )
3010 {
3011 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
3012 MSGLEN ) == MSGLEN );
3013
3014 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
3015 MSGLEN ) == MSGLEN );
3016
3017 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &server_context, message,
3018 MSGLEN ) == MSGLEN );
3019
3020 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &server_context, message,
3021 MSGLEN ) == MSGLEN );
3022
3023 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received,
3024 MSGLEN ) == MSGLEN );
3025
3026 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
3027
3028 memset( received, 0, sizeof( received ) );
3029
3030 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &client_context, received,
3031 MSGLEN ) == MSGLEN );
3032
3033 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
3034
3035 memset( received, 0, sizeof( received ) );
3036 }
3037
3038 for( i = 0; i < 2; i++ )
3039 {
3040 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received,
3041 MSGLEN ) == MSGLEN );
3042
3043 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
3044 memset( received, 0, sizeof( received ) );
3045
3046 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &client_context, received,
3047 MSGLEN ) == MSGLEN );
3048
3049 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
3050 memset( received, 0, sizeof( received ) );
3051 }
3052
3053 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
Andrzej Kurekf46b9122020-02-07 08:19:00 -05003054 == MBEDTLS_ERR_SSL_WANT_READ );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003055
3056 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &client_context, received, MSGLEN )
Andrzej Kurekf46b9122020-02-07 08:19:00 -05003057 == MBEDTLS_ERR_SSL_WANT_READ );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003058 exit:
3059 mbedtls_message_socket_close( &server_context );
3060 mbedtls_message_socket_close( &client_context );
3061}
3062/* END_CASE */
3063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003064/* BEGIN_CASE depends_on:MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Azim Khan5fcca462018-06-29 11:05:32 +01003065void ssl_dtls_replay( data_t * prevs, data_t * new, int ret )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003066{
Azim Khand30ca132017-06-09 04:32:58 +01003067 uint32_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003068 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02003069 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003070
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02003071 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02003072 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02003073
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02003074 TEST_ASSERT( mbedtls_ssl_config_defaults( &conf,
3075 MBEDTLS_SSL_IS_CLIENT,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02003076 MBEDTLS_SSL_TRANSPORT_DATAGRAM,
3077 MBEDTLS_SSL_PRESET_DEFAULT ) == 0 );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02003078 TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003079
3080 /* Read previous record numbers */
Azim Khand30ca132017-06-09 04:32:58 +01003081 for( len = 0; len < prevs->len; len += 6 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003082 {
Azim Khand30ca132017-06-09 04:32:58 +01003083 memcpy( ssl.in_ctr + 2, prevs->x + len, 6 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003084 mbedtls_ssl_dtls_replay_update( &ssl );
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003085 }
3086
3087 /* Check new number */
Azim Khand30ca132017-06-09 04:32:58 +01003088 memcpy( ssl.in_ctr + 2, new->x, 6 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003089 TEST_ASSERT( mbedtls_ssl_dtls_replay_check( &ssl ) == ret );
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003091 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02003092 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003093}
3094/* END_CASE */
Hanno Beckerb25c0c72017-05-05 11:24:30 +01003095
3096/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
3097void ssl_set_hostname_twice( char *hostname0, char *hostname1 )
3098{
3099 mbedtls_ssl_context ssl;
3100 mbedtls_ssl_init( &ssl );
3101
3102 TEST_ASSERT( mbedtls_ssl_set_hostname( &ssl, hostname0 ) == 0 );
3103 TEST_ASSERT( mbedtls_ssl_set_hostname( &ssl, hostname1 ) == 0 );
3104
3105 mbedtls_ssl_free( &ssl );
3106}
Darryl Green11999bb2018-03-13 15:22:58 +00003107/* END_CASE */
Hanno Beckera18d1322018-01-03 14:27:32 +00003108
3109/* BEGIN_CASE */
3110void ssl_crypt_record( int cipher_type, int hash_id,
Hanno Beckerd856c822019-04-29 17:30:59 +01003111 int etm, int tag_mode, int ver,
3112 int cid0_len, int cid1_len )
Hanno Beckera18d1322018-01-03 14:27:32 +00003113{
3114 /*
3115 * Test several record encryptions and decryptions
3116 * with plenty of space before and after the data
3117 * within the record buffer.
3118 */
3119
3120 int ret;
3121 int num_records = 16;
3122 mbedtls_ssl_context ssl; /* ONLY for debugging */
3123
3124 mbedtls_ssl_transform t0, t1;
Hanno Becker81e16a32019-03-01 11:21:44 +00003125 unsigned char *buf = NULL;
Hanno Beckera18d1322018-01-03 14:27:32 +00003126 size_t const buflen = 512;
3127 mbedtls_record rec, rec_backup;
3128
3129 mbedtls_ssl_init( &ssl );
3130 mbedtls_ssl_transform_init( &t0 );
3131 mbedtls_ssl_transform_init( &t1 );
3132 TEST_ASSERT( build_transforms( &t0, &t1, cipher_type, hash_id,
Hanno Beckerd856c822019-04-29 17:30:59 +01003133 etm, tag_mode, ver,
3134 (size_t) cid0_len,
3135 (size_t) cid1_len ) == 0 );
Hanno Beckera18d1322018-01-03 14:27:32 +00003136
Hanno Becker3ee54212019-04-04 16:31:26 +01003137 TEST_ASSERT( ( buf = mbedtls_calloc( 1, buflen ) ) != NULL );
Hanno Beckera18d1322018-01-03 14:27:32 +00003138
3139 while( num_records-- > 0 )
3140 {
3141 mbedtls_ssl_transform *t_dec, *t_enc;
3142 /* Take turns in who's sending and who's receiving. */
3143 if( num_records % 3 == 0 )
3144 {
3145 t_dec = &t0;
3146 t_enc = &t1;
3147 }
3148 else
3149 {
3150 t_dec = &t1;
3151 t_enc = &t0;
3152 }
3153
3154 /*
3155 * The record header affects the transformation in two ways:
3156 * 1) It determines the AEAD additional data
3157 * 2) The record counter sometimes determines the IV.
3158 *
3159 * Apart from that, the fields don't have influence.
3160 * In particular, it is currently not the responsibility
3161 * of ssl_encrypt/decrypt_buf to check if the transform
3162 * version matches the record version, or that the
3163 * type is sensible.
3164 */
3165
3166 memset( rec.ctr, num_records, sizeof( rec.ctr ) );
3167 rec.type = 42;
3168 rec.ver[0] = num_records;
3169 rec.ver[1] = num_records;
Hanno Beckera0e20d02019-05-15 14:03:01 +01003170#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd856c822019-04-29 17:30:59 +01003171 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01003172#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera18d1322018-01-03 14:27:32 +00003173
3174 rec.buf = buf;
3175 rec.buf_len = buflen;
3176 rec.data_offset = 16;
3177 /* Make sure to vary the length to exercise different
3178 * paddings. */
3179 rec.data_len = 1 + num_records;
3180
3181 memset( rec.buf + rec.data_offset, 42, rec.data_len );
3182
3183 /* Make a copy for later comparison */
3184 rec_backup = rec;
3185
3186 /* Encrypt record */
3187 ret = mbedtls_ssl_encrypt_buf( &ssl, t_enc, &rec,
3188 rnd_std_rand, NULL );
3189 TEST_ASSERT( ret == 0 || ret == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3190 if( ret != 0 )
3191 {
3192 continue;
3193 }
3194
Hanno Beckerb2713ab2020-05-07 14:54:22 +01003195#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3196 if( rec.cid_len != 0 )
3197 {
3198 /* DTLS 1.2 + CID hides the real content type and
3199 * uses a special CID content type in the protected
3200 * record. Double-check this. */
3201 TEST_ASSERT( rec.type == MBEDTLS_SSL_MSG_CID );
3202 }
3203#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3204
3205#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
3206 if( t_enc->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
3207 {
3208 /* TLS 1.3 hides the real content type and
3209 * always uses Application Data as the content type
3210 * for protected records. Double-check this. */
3211 TEST_ASSERT( rec.type == MBEDTLS_SSL_MSG_APPLICATION_DATA );
3212 }
3213#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
3214
Hanno Beckera18d1322018-01-03 14:27:32 +00003215 /* Decrypt record with t_dec */
Hanno Beckerd856c822019-04-29 17:30:59 +01003216 ret = mbedtls_ssl_decrypt_buf( &ssl, t_dec, &rec );
3217 TEST_ASSERT( ret == 0 );
Hanno Beckera18d1322018-01-03 14:27:32 +00003218
3219 /* Compare results */
3220 TEST_ASSERT( rec.type == rec_backup.type );
3221 TEST_ASSERT( memcmp( rec.ctr, rec_backup.ctr, 8 ) == 0 );
3222 TEST_ASSERT( rec.ver[0] == rec_backup.ver[0] );
3223 TEST_ASSERT( rec.ver[1] == rec_backup.ver[1] );
3224 TEST_ASSERT( rec.data_len == rec_backup.data_len );
3225 TEST_ASSERT( rec.data_offset == rec_backup.data_offset );
3226 TEST_ASSERT( memcmp( rec.buf + rec.data_offset,
3227 rec_backup.buf + rec_backup.data_offset,
3228 rec.data_len ) == 0 );
3229 }
3230
Hanno Becker81e16a32019-03-01 11:21:44 +00003231exit:
3232
Hanno Beckera18d1322018-01-03 14:27:32 +00003233 /* Cleanup */
3234 mbedtls_ssl_free( &ssl );
3235 mbedtls_ssl_transform_free( &t0 );
3236 mbedtls_ssl_transform_free( &t1 );
3237
Hanno Becker3ee54212019-04-04 16:31:26 +01003238 mbedtls_free( buf );
Hanno Beckera18d1322018-01-03 14:27:32 +00003239}
3240/* END_CASE */
Hanno Beckerb3268da2018-01-05 15:20:24 +00003241
3242/* BEGIN_CASE */
3243void ssl_crypt_record_small( int cipher_type, int hash_id,
Hanno Beckerd856c822019-04-29 17:30:59 +01003244 int etm, int tag_mode, int ver,
3245 int cid0_len, int cid1_len )
Hanno Beckerb3268da2018-01-05 15:20:24 +00003246{
3247 /*
3248 * Test pairs of encryption and decryption with an increasing
3249 * amount of space in the record buffer - in more detail:
3250 * 1) Try to encrypt with 0, 1, 2, ... bytes available
3251 * in front of the plaintext, and expect the encryption
3252 * to succeed starting from some offset. Always keep
3253 * enough space in the end of the buffer.
3254 * 2) Try to encrypt with 0, 1, 2, ... bytes available
3255 * at the end of the plaintext, and expect the encryption
3256 * to succeed starting from some offset. Always keep
3257 * enough space at the beginning of the buffer.
3258 * 3) Try to encrypt with 0, 1, 2, ... bytes available
3259 * both at the front and end of the plaintext,
3260 * and expect the encryption to succeed starting from
3261 * some offset.
3262 *
3263 * If encryption succeeds, check that decryption succeeds
3264 * and yields the original record.
3265 */
3266
3267 mbedtls_ssl_context ssl; /* ONLY for debugging */
3268
3269 mbedtls_ssl_transform t0, t1;
Hanno Becker81e16a32019-03-01 11:21:44 +00003270 unsigned char *buf = NULL;
Hanno Beckerd856c822019-04-29 17:30:59 +01003271 size_t const buflen = 256;
Hanno Beckerb3268da2018-01-05 15:20:24 +00003272 mbedtls_record rec, rec_backup;
3273
3274 int ret;
Hanno Beckerd856c822019-04-29 17:30:59 +01003275 int mode; /* Mode 1, 2 or 3 as explained above */
3276 size_t offset; /* Available space at beginning/end/both */
3277 size_t threshold = 96; /* Maximum offset to test against */
Hanno Beckerb3268da2018-01-05 15:20:24 +00003278
Hanno Beckerd856c822019-04-29 17:30:59 +01003279 size_t default_pre_padding = 64; /* Pre-padding to use in mode 2 */
3280 size_t default_post_padding = 128; /* Post-padding to use in mode 1 */
Hanno Beckerb3268da2018-01-05 15:20:24 +00003281
3282 int seen_success; /* Indicates if in the current mode we've
3283 * already seen a successful test. */
3284
3285 mbedtls_ssl_init( &ssl );
3286 mbedtls_ssl_transform_init( &t0 );
3287 mbedtls_ssl_transform_init( &t1 );
3288 TEST_ASSERT( build_transforms( &t0, &t1, cipher_type, hash_id,
Hanno Beckerd856c822019-04-29 17:30:59 +01003289 etm, tag_mode, ver,
3290 (size_t) cid0_len,
3291 (size_t) cid1_len ) == 0 );
Hanno Beckerb3268da2018-01-05 15:20:24 +00003292
Hanno Becker3ee54212019-04-04 16:31:26 +01003293 TEST_ASSERT( ( buf = mbedtls_calloc( 1, buflen ) ) != NULL );
Hanno Beckerb3268da2018-01-05 15:20:24 +00003294
3295 for( mode=1; mode <= 3; mode++ )
3296 {
3297 seen_success = 0;
3298 for( offset=0; offset <= threshold; offset++ )
3299 {
3300 mbedtls_ssl_transform *t_dec, *t_enc;
Hanno Becker6c87b3f2019-04-29 17:24:44 +01003301 t_dec = &t0;
3302 t_enc = &t1;
Hanno Beckerb3268da2018-01-05 15:20:24 +00003303
3304 memset( rec.ctr, offset, sizeof( rec.ctr ) );
3305 rec.type = 42;
3306 rec.ver[0] = offset;
3307 rec.ver[1] = offset;
3308 rec.buf = buf;
3309 rec.buf_len = buflen;
Hanno Beckera0e20d02019-05-15 14:03:01 +01003310#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd856c822019-04-29 17:30:59 +01003311 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01003312#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerb3268da2018-01-05 15:20:24 +00003313
3314 switch( mode )
3315 {
3316 case 1: /* Space in the beginning */
3317 rec.data_offset = offset;
3318 rec.data_len = buflen - offset - default_post_padding;
3319 break;
3320
3321 case 2: /* Space in the end */
3322 rec.data_offset = default_pre_padding;
3323 rec.data_len = buflen - default_pre_padding - offset;
3324 break;
3325
3326 case 3: /* Space in the beginning and end */
3327 rec.data_offset = offset;
3328 rec.data_len = buflen - 2 * offset;
3329 break;
3330
3331 default:
3332 TEST_ASSERT( 0 );
3333 break;
3334 }
3335
3336 memset( rec.buf + rec.data_offset, 42, rec.data_len );
3337
3338 /* Make a copy for later comparison */
3339 rec_backup = rec;
3340
3341 /* Encrypt record */
3342 ret = mbedtls_ssl_encrypt_buf( &ssl, t_enc, &rec, rnd_std_rand, NULL );
3343
3344 if( ( mode == 1 || mode == 2 ) && seen_success )
3345 {
3346 TEST_ASSERT( ret == 0 );
3347 }
3348 else
3349 {
3350 TEST_ASSERT( ret == 0 || ret == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3351 if( ret == 0 )
3352 seen_success = 1;
3353 }
3354
3355 if( ret != 0 )
3356 continue;
3357
Hanno Beckerb2713ab2020-05-07 14:54:22 +01003358#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3359 if( rec.cid_len != 0 )
3360 {
3361 /* DTLS 1.2 + CID hides the real content type and
3362 * uses a special CID content type in the protected
3363 * record. Double-check this. */
3364 TEST_ASSERT( rec.type == MBEDTLS_SSL_MSG_CID );
3365 }
3366#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3367
3368#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
3369 if( t_enc->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
3370 {
3371 /* TLS 1.3 hides the real content type and
3372 * always uses Application Data as the content type
3373 * for protected records. Double-check this. */
3374 TEST_ASSERT( rec.type == MBEDTLS_SSL_MSG_APPLICATION_DATA );
3375 }
3376#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
3377
Hanno Beckerb3268da2018-01-05 15:20:24 +00003378 /* Decrypt record with t_dec */
3379 TEST_ASSERT( mbedtls_ssl_decrypt_buf( &ssl, t_dec, &rec ) == 0 );
3380
3381 /* Compare results */
3382 TEST_ASSERT( rec.type == rec_backup.type );
3383 TEST_ASSERT( memcmp( rec.ctr, rec_backup.ctr, 8 ) == 0 );
3384 TEST_ASSERT( rec.ver[0] == rec_backup.ver[0] );
3385 TEST_ASSERT( rec.ver[1] == rec_backup.ver[1] );
3386 TEST_ASSERT( rec.data_len == rec_backup.data_len );
3387 TEST_ASSERT( rec.data_offset == rec_backup.data_offset );
3388 TEST_ASSERT( memcmp( rec.buf + rec.data_offset,
3389 rec_backup.buf + rec_backup.data_offset,
3390 rec.data_len ) == 0 );
3391 }
3392
3393 TEST_ASSERT( seen_success == 1 );
3394 }
3395
Hanno Becker81e16a32019-03-01 11:21:44 +00003396exit:
3397
Hanno Beckerb3268da2018-01-05 15:20:24 +00003398 /* Cleanup */
3399 mbedtls_ssl_free( &ssl );
3400 mbedtls_ssl_transform_free( &t0 );
3401 mbedtls_ssl_transform_free( &t1 );
3402
Hanno Becker3ee54212019-04-04 16:31:26 +01003403 mbedtls_free( buf );
Hanno Beckerb3268da2018-01-05 15:20:24 +00003404}
3405/* END_CASE */
Ron Eldor824ad7b2019-05-13 14:09:00 +03003406
3407/* BEGIN_CASE */
3408void ssl_tls_prf( int type, data_t * secret, data_t * random,
3409 char *label, data_t *result_hex_str, int exp_ret )
3410{
3411 unsigned char *output;
3412
3413 output = mbedtls_calloc( 1, result_hex_str->len );
3414 if( output == NULL )
3415 goto exit;
3416
Ron Eldor6b9b1b82019-05-15 17:04:33 +03003417#if defined(MBEDTLS_USE_PSA_CRYPTO)
3418 TEST_ASSERT( psa_crypto_init() == 0 );
3419#endif
3420
Ron Eldor824ad7b2019-05-13 14:09:00 +03003421 TEST_ASSERT( mbedtls_ssl_tls_prf( type, secret->x, secret->len,
3422 label, random->x, random->len,
3423 output, result_hex_str->len ) == exp_ret );
3424
3425 if( exp_ret == 0 )
3426 {
Ronald Cronde70b162020-06-10 11:03:08 +02003427 TEST_ASSERT( mbedtls_test_hexcmp( output, result_hex_str->x,
Ron Eldor824ad7b2019-05-13 14:09:00 +03003428 result_hex_str->len, result_hex_str->len ) == 0 );
3429 }
3430exit:
3431
3432 mbedtls_free( output );
3433}
3434/* END_CASE */
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02003435
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02003436/* BEGIN_CASE */
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02003437void ssl_serialize_session_save_load( int ticket_len, char *crt_file )
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02003438{
3439 mbedtls_ssl_session original, restored;
3440 unsigned char *buf = NULL;
3441 size_t len;
3442
3443 /*
3444 * Test that a save-load pair is the identity
3445 */
3446
3447 mbedtls_ssl_session_init( &original );
3448 mbedtls_ssl_session_init( &restored );
3449
3450 /* Prepare a dummy session to work on */
3451 TEST_ASSERT( ssl_populate_session( &original, ticket_len, crt_file ) == 0 );
3452
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02003453 /* Serialize it */
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02003454 TEST_ASSERT( mbedtls_ssl_session_save( &original, NULL, 0, &len )
3455 == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3456 TEST_ASSERT( ( buf = mbedtls_calloc( 1, len ) ) != NULL );
3457 TEST_ASSERT( mbedtls_ssl_session_save( &original, buf, len, &len )
3458 == 0 );
3459
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02003460 /* Restore session from serialized data */
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02003461 TEST_ASSERT( mbedtls_ssl_session_load( &restored, buf, len) == 0 );
3462
3463 /*
3464 * Make sure both session structures are identical
3465 */
3466#if defined(MBEDTLS_HAVE_TIME)
3467 TEST_ASSERT( original.start == restored.start );
3468#endif
3469 TEST_ASSERT( original.ciphersuite == restored.ciphersuite );
3470 TEST_ASSERT( original.compression == restored.compression );
3471 TEST_ASSERT( original.id_len == restored.id_len );
3472 TEST_ASSERT( memcmp( original.id,
3473 restored.id, sizeof( original.id ) ) == 0 );
3474 TEST_ASSERT( memcmp( original.master,
3475 restored.master, sizeof( original.master ) ) == 0 );
3476
3477#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02003478#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02003479 TEST_ASSERT( ( original.peer_cert == NULL ) ==
3480 ( restored.peer_cert == NULL ) );
3481 if( original.peer_cert != NULL )
3482 {
3483 TEST_ASSERT( original.peer_cert->raw.len ==
3484 restored.peer_cert->raw.len );
3485 TEST_ASSERT( memcmp( original.peer_cert->raw.p,
3486 restored.peer_cert->raw.p,
3487 original.peer_cert->raw.len ) == 0 );
3488 }
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02003489#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3490 TEST_ASSERT( original.peer_cert_digest_type ==
3491 restored.peer_cert_digest_type );
3492 TEST_ASSERT( original.peer_cert_digest_len ==
3493 restored.peer_cert_digest_len );
3494 TEST_ASSERT( ( original.peer_cert_digest == NULL ) ==
3495 ( restored.peer_cert_digest == NULL ) );
3496 if( original.peer_cert_digest != NULL )
3497 {
3498 TEST_ASSERT( memcmp( original.peer_cert_digest,
3499 restored.peer_cert_digest,
3500 original.peer_cert_digest_len ) == 0 );
3501 }
3502#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3503#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02003504 TEST_ASSERT( original.verify_result == restored.verify_result );
3505
3506#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
3507 TEST_ASSERT( original.ticket_len == restored.ticket_len );
3508 if( original.ticket_len != 0 )
3509 {
3510 TEST_ASSERT( original.ticket != NULL );
3511 TEST_ASSERT( restored.ticket != NULL );
3512 TEST_ASSERT( memcmp( original.ticket,
3513 restored.ticket, original.ticket_len ) == 0 );
3514 }
3515 TEST_ASSERT( original.ticket_lifetime == restored.ticket_lifetime );
3516#endif
3517
3518#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3519 TEST_ASSERT( original.mfl_code == restored.mfl_code );
3520#endif
3521
3522#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
3523 TEST_ASSERT( original.trunc_hmac == restored.trunc_hmac );
3524#endif
3525
3526#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
3527 TEST_ASSERT( original.encrypt_then_mac == restored.encrypt_then_mac );
3528#endif
3529
3530exit:
3531 mbedtls_ssl_session_free( &original );
3532 mbedtls_ssl_session_free( &restored );
3533 mbedtls_free( buf );
3534}
3535/* END_CASE */
3536
Manuel Pégourié-Gonnardaa755832019-06-03 10:53:47 +02003537/* BEGIN_CASE */
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02003538void ssl_serialize_session_load_save( int ticket_len, char *crt_file )
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02003539{
3540 mbedtls_ssl_session session;
3541 unsigned char *buf1 = NULL, *buf2 = NULL;
3542 size_t len0, len1, len2;
3543
3544 /*
3545 * Test that a load-save pair is the identity
3546 */
3547
3548 mbedtls_ssl_session_init( &session );
3549
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02003550 /* Prepare a dummy session to work on */
Manuel Pégourié-Gonnard6b840702019-05-24 09:40:17 +02003551 TEST_ASSERT( ssl_populate_session( &session, ticket_len, crt_file ) == 0 );
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02003552
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02003553 /* Get desired buffer size for serializing */
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02003554 TEST_ASSERT( mbedtls_ssl_session_save( &session, NULL, 0, &len0 )
3555 == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3556
3557 /* Allocate first buffer */
3558 buf1 = mbedtls_calloc( 1, len0 );
3559 TEST_ASSERT( buf1 != NULL );
3560
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02003561 /* Serialize to buffer and free live session */
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02003562 TEST_ASSERT( mbedtls_ssl_session_save( &session, buf1, len0, &len1 )
3563 == 0 );
3564 TEST_ASSERT( len0 == len1 );
3565 mbedtls_ssl_session_free( &session );
3566
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02003567 /* Restore session from serialized data */
Manuel Pégourié-Gonnard220403b2019-05-24 09:54:21 +02003568 TEST_ASSERT( mbedtls_ssl_session_load( &session, buf1, len1 ) == 0 );
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02003569
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02003570 /* Allocate second buffer and serialize to it */
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02003571 buf2 = mbedtls_calloc( 1, len0 );
Manuel Pégourié-Gonnardb4079902019-05-24 09:52:10 +02003572 TEST_ASSERT( buf2 != NULL );
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02003573 TEST_ASSERT( mbedtls_ssl_session_save( &session, buf2, len0, &len2 )
3574 == 0 );
3575
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02003576 /* Make sure both serialized versions are identical */
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02003577 TEST_ASSERT( len1 == len2 );
3578 TEST_ASSERT( memcmp( buf1, buf2, len1 ) == 0 );
3579
3580exit:
3581 mbedtls_ssl_session_free( &session );
3582 mbedtls_free( buf1 );
3583 mbedtls_free( buf2 );
3584}
3585/* END_CASE */
Manuel Pégourié-Gonnardf5fa0aa2019-05-23 10:38:11 +02003586
3587/* BEGIN_CASE */
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02003588void ssl_serialize_session_save_buf_size( int ticket_len, char *crt_file )
Manuel Pégourié-Gonnardf5fa0aa2019-05-23 10:38:11 +02003589{
3590 mbedtls_ssl_session session;
3591 unsigned char *buf = NULL;
3592 size_t good_len, bad_len, test_len;
3593
3594 /*
3595 * Test that session_save() fails cleanly on small buffers
3596 */
3597
3598 mbedtls_ssl_session_init( &session );
3599
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02003600 /* Prepare dummy session and get serialized size */
Manuel Pégourié-Gonnard6b840702019-05-24 09:40:17 +02003601 TEST_ASSERT( ssl_populate_session( &session, ticket_len, crt_file ) == 0 );
Manuel Pégourié-Gonnardf5fa0aa2019-05-23 10:38:11 +02003602 TEST_ASSERT( mbedtls_ssl_session_save( &session, NULL, 0, &good_len )
3603 == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3604
3605 /* Try all possible bad lengths */
3606 for( bad_len = 1; bad_len < good_len; bad_len++ )
3607 {
3608 /* Allocate exact size so that asan/valgrind can detect any overwrite */
3609 mbedtls_free( buf );
3610 TEST_ASSERT( ( buf = mbedtls_calloc( 1, bad_len ) ) != NULL );
3611 TEST_ASSERT( mbedtls_ssl_session_save( &session, buf, bad_len,
3612 &test_len )
3613 == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3614 TEST_ASSERT( test_len == good_len );
3615 }
3616
3617exit:
3618 mbedtls_ssl_session_free( &session );
3619 mbedtls_free( buf );
3620}
3621/* END_CASE */
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003622
3623/* BEGIN_CASE */
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02003624void ssl_serialize_session_load_buf_size( int ticket_len, char *crt_file )
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003625{
3626 mbedtls_ssl_session session;
3627 unsigned char *good_buf = NULL, *bad_buf = NULL;
3628 size_t good_len, bad_len;
3629
3630 /*
3631 * Test that session_load() fails cleanly on small buffers
3632 */
3633
3634 mbedtls_ssl_session_init( &session );
3635
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02003636 /* Prepare serialized session data */
Manuel Pégourié-Gonnard6b840702019-05-24 09:40:17 +02003637 TEST_ASSERT( ssl_populate_session( &session, ticket_len, crt_file ) == 0 );
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003638 TEST_ASSERT( mbedtls_ssl_session_save( &session, NULL, 0, &good_len )
3639 == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3640 TEST_ASSERT( ( good_buf = mbedtls_calloc( 1, good_len ) ) != NULL );
3641 TEST_ASSERT( mbedtls_ssl_session_save( &session, good_buf, good_len,
3642 &good_len ) == 0 );
3643 mbedtls_ssl_session_free( &session );
3644
3645 /* Try all possible bad lengths */
3646 for( bad_len = 0; bad_len < good_len; bad_len++ )
3647 {
3648 /* Allocate exact size so that asan/valgrind can detect any overread */
3649 mbedtls_free( bad_buf );
3650 bad_buf = mbedtls_calloc( 1, bad_len ? bad_len : 1 );
3651 TEST_ASSERT( bad_buf != NULL );
3652 memcpy( bad_buf, good_buf, bad_len );
3653
3654 TEST_ASSERT( mbedtls_ssl_session_load( &session, bad_buf, bad_len )
3655 == MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3656 }
3657
3658exit:
3659 mbedtls_ssl_session_free( &session );
3660 mbedtls_free( good_buf );
3661 mbedtls_free( bad_buf );
3662}
3663/* END_CASE */
Hanno Becker861d0bb2019-05-21 16:39:30 +01003664
Hanno Becker363b6462019-05-29 12:44:28 +01003665/* BEGIN_CASE */
3666void ssl_session_serialize_version_check( int corrupt_major,
Hanno Becker861d0bb2019-05-21 16:39:30 +01003667 int corrupt_minor,
3668 int corrupt_patch,
3669 int corrupt_config )
3670{
Hanno Becker363b6462019-05-29 12:44:28 +01003671 unsigned char serialized_session[ 2048 ];
3672 size_t serialized_session_len;
Hanno Beckerfe1275e2019-05-29 12:45:21 +01003673 unsigned cur_byte;
Hanno Becker861d0bb2019-05-21 16:39:30 +01003674 mbedtls_ssl_session session;
Hanno Beckerfe1275e2019-05-29 12:45:21 +01003675 uint8_t should_corrupt_byte[] = { corrupt_major == 1,
3676 corrupt_minor == 1,
3677 corrupt_patch == 1,
3678 corrupt_config == 1,
3679 corrupt_config == 1 };
3680
Hanno Becker861d0bb2019-05-21 16:39:30 +01003681 mbedtls_ssl_session_init( &session );
3682
Hanno Beckerfe1275e2019-05-29 12:45:21 +01003683 /* Infer length of serialized session. */
Hanno Becker861d0bb2019-05-21 16:39:30 +01003684 TEST_ASSERT( mbedtls_ssl_session_save( &session,
Hanno Becker363b6462019-05-29 12:44:28 +01003685 serialized_session,
3686 sizeof( serialized_session ),
3687 &serialized_session_len ) == 0 );
Hanno Becker861d0bb2019-05-21 16:39:30 +01003688
Hanno Beckerfe1275e2019-05-29 12:45:21 +01003689 mbedtls_ssl_session_free( &session );
Hanno Becker861d0bb2019-05-21 16:39:30 +01003690
Hanno Beckerfe1275e2019-05-29 12:45:21 +01003691 /* Without any modification, we should be able to successfully
Hanno Becker363b6462019-05-29 12:44:28 +01003692 * de-serialize the session - double-check that. */
Hanno Becker861d0bb2019-05-21 16:39:30 +01003693 TEST_ASSERT( mbedtls_ssl_session_load( &session,
Hanno Becker363b6462019-05-29 12:44:28 +01003694 serialized_session,
3695 serialized_session_len ) == 0 );
Hanno Becker861d0bb2019-05-21 16:39:30 +01003696 mbedtls_ssl_session_free( &session );
3697
Hanno Beckerfe1275e2019-05-29 12:45:21 +01003698 /* Go through the bytes in the serialized session header and
3699 * corrupt them bit-by-bit. */
3700 for( cur_byte = 0; cur_byte < sizeof( should_corrupt_byte ); cur_byte++ )
Hanno Becker861d0bb2019-05-21 16:39:30 +01003701 {
Hanno Beckerfe1275e2019-05-29 12:45:21 +01003702 int cur_bit;
3703 unsigned char * const byte = &serialized_session[ cur_byte ];
3704
3705 if( should_corrupt_byte[ cur_byte ] == 0 )
3706 continue;
3707
3708 for( cur_bit = 0; cur_bit < CHAR_BIT; cur_bit++ )
3709 {
3710 unsigned char const corrupted_bit = 0x1u << cur_bit;
3711 /* Modify a single bit in the serialized session. */
3712 *byte ^= corrupted_bit;
3713
3714 /* Attempt to deserialize */
3715 TEST_ASSERT( mbedtls_ssl_session_load( &session,
3716 serialized_session,
3717 serialized_session_len ) ==
Hanno Beckerf9b33032019-06-03 12:58:39 +01003718 MBEDTLS_ERR_SSL_VERSION_MISMATCH );
Hanno Beckerfe1275e2019-05-29 12:45:21 +01003719
3720 /* Undo the change */
3721 *byte ^= corrupted_bit;
3722 }
Hanno Becker861d0bb2019-05-21 16:39:30 +01003723 }
3724
Hanno Becker861d0bb2019-05-21 16:39:30 +01003725}
3726/* END_CASE */
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01003727
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02003728/* 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 +01003729void mbedtls_endpoint_sanity( int endpoint_type )
3730{
3731 enum { BUFFSIZE = 1024 };
3732 mbedtls_endpoint ep;
3733 int ret = -1;
3734
Andrzej Kurek15daf502020-02-12 09:17:52 -05003735 ret = mbedtls_endpoint_init( NULL, endpoint_type, MBEDTLS_PK_RSA,
3736 NULL, NULL, NULL );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01003737 TEST_ASSERT( MBEDTLS_ERR_SSL_BAD_INPUT_DATA == ret );
3738
Andrzej Kurekb2980742020-02-02 19:25:26 -05003739 ret = mbedtls_endpoint_certificate_init( NULL, MBEDTLS_PK_RSA );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01003740 TEST_ASSERT( MBEDTLS_ERR_SSL_BAD_INPUT_DATA == ret );
3741
Andrzej Kurek15daf502020-02-12 09:17:52 -05003742 ret = mbedtls_endpoint_init( &ep, endpoint_type, MBEDTLS_PK_RSA,
3743 NULL, NULL, NULL );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01003744 TEST_ASSERT( ret == 0 );
3745
3746exit:
Andrzej Kurek15daf502020-02-12 09:17:52 -05003747 mbedtls_endpoint_free( &ep, NULL );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01003748}
3749/* END_CASE */
3750
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02003751/* 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 +01003752void move_handshake_to_state(int endpoint_type, int state, int need_pass)
3753{
3754 enum { BUFFSIZE = 1024 };
3755 mbedtls_endpoint base_ep, second_ep;
3756 int ret = -1;
3757
Andrzej Kurek15daf502020-02-12 09:17:52 -05003758 ret = mbedtls_endpoint_init( &base_ep, endpoint_type, MBEDTLS_PK_RSA,
3759 NULL, NULL, NULL );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01003760 TEST_ASSERT( ret == 0 );
3761
3762 ret = mbedtls_endpoint_init( &second_ep,
3763 ( endpoint_type == MBEDTLS_SSL_IS_SERVER ) ?
Andrzej Kurekb2980742020-02-02 19:25:26 -05003764 MBEDTLS_SSL_IS_CLIENT : MBEDTLS_SSL_IS_SERVER,
Andrzej Kurek15daf502020-02-12 09:17:52 -05003765 MBEDTLS_PK_RSA, NULL, NULL, NULL );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01003766 TEST_ASSERT( ret == 0 );
3767
3768 ret = mbedtls_mock_socket_connect( &(base_ep.socket),
3769 &(second_ep.socket),
3770 BUFFSIZE );
3771 TEST_ASSERT( ret == 0 );
3772
3773 ret = mbedtls_move_handshake_to_state( &(base_ep.ssl),
3774 &(second_ep.ssl),
3775 state );
3776 if( need_pass )
3777 {
3778 TEST_ASSERT( ret == 0 );
3779 TEST_ASSERT( base_ep.ssl.state == state );
3780 }
3781 else
3782 {
3783 TEST_ASSERT( ret != 0 );
3784 TEST_ASSERT( base_ep.ssl.state != state );
3785 }
3786
3787exit:
Andrzej Kurek15daf502020-02-12 09:17:52 -05003788 mbedtls_endpoint_free( &base_ep, NULL );
3789 mbedtls_endpoint_free( &second_ep, NULL );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01003790}
3791/* END_CASE */
Andrzej Kurekf40daa32020-02-04 09:00:01 -05003792
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02003793/* 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 */
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003794void handshake_version( int version, int dtls )
Andrzej Kurekf40daa32020-02-04 09:00:01 -05003795{
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003796 handshake_test_options options;
3797 init_handshake_options( &options );
Andrzej Kurekda2b6782020-02-12 07:56:36 -05003798
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003799 options.version = version;
3800 options.dtls = dtls;
Piotr Nowicki438bf3b2020-03-10 12:59:10 +01003801 /* By default, SSLv3.0 and TLSv1.0 use 1/n-1 splitting when sending data, so
3802 * the number of fragments will be twice as big. */
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003803 if( version == MBEDTLS_SSL_MINOR_VERSION_0 ||
3804 version == MBEDTLS_SSL_MINOR_VERSION_1 )
Andrzej Kurek941962e2020-02-07 09:20:32 -05003805 {
Piotr Nowicki438bf3b2020-03-10 12:59:10 +01003806 options.expected_cli_fragments = 2;
3807 options.expected_srv_fragments = 2;
Andrzej Kurek941962e2020-02-07 09:20:32 -05003808 }
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003809 perform_handshake( &options );
Andrzej Kurekf40daa32020-02-04 09:00:01 -05003810
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003811 /* The goto below is used to avoid an "unused label" warning.*/
3812 goto exit;
3813}
3814/* END_CASE */
Andrzej Kurek9e9efdc2020-02-26 05:25:23 -05003815
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02003816/* 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 -05003817void handshake_psk_cipher( char* cipher, int pk_alg, data_t *psk_str, int dtls )
3818{
3819 handshake_test_options options;
3820 init_handshake_options( &options );
Andrzej Kurekf40daa32020-02-04 09:00:01 -05003821
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003822 options.cipher = cipher;
3823 options.dtls = dtls;
3824 options.psk_str = psk_str;
3825 options.pk_alg = pk_alg;
Andrzej Kurekcc5169c2020-02-04 09:04:56 -05003826
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003827 perform_handshake( &options );
Andrzej Kurek316da1f2020-02-26 09:03:47 -05003828
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003829 /* The goto below is used to avoid an "unused label" warning.*/
3830 goto exit;
3831}
3832/* END_CASE */
Andrzej Kurek316da1f2020-02-26 09:03:47 -05003833
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02003834/* 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 -05003835void handshake_cipher( char* cipher, int pk_alg, int dtls )
3836{
3837 test_handshake_psk_cipher( cipher, pk_alg, NULL, dtls );
Andrzej Kurekf40daa32020-02-04 09:00:01 -05003838
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003839 /* The goto below is used to avoid an "unused label" warning.*/
3840 goto exit;
3841}
3842/* END_CASE */
Andrzej Kurekf40daa32020-02-04 09:00:01 -05003843
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02003844/* 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 -05003845void app_data( int mfl, int cli_msg_len, int srv_msg_len,
3846 int expected_cli_fragments,
3847 int expected_srv_fragments, int dtls )
3848{
3849 handshake_test_options options;
3850 init_handshake_options( &options );
Andrzej Kurekda2b6782020-02-12 07:56:36 -05003851
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003852 options.mfl = mfl;
3853 options.cli_msg_len = cli_msg_len;
3854 options.srv_msg_len = srv_msg_len;
3855 options.expected_cli_fragments = expected_cli_fragments;
3856 options.expected_srv_fragments = expected_srv_fragments;
3857 options.dtls = dtls;
Andrzej Kurekda2b6782020-02-12 07:56:36 -05003858
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003859 perform_handshake( &options );
3860 /* The goto below is used to avoid an "unused label" warning.*/
3861 goto exit;
3862}
3863/* END_CASE */
Andrzej Kurekda2b6782020-02-12 07:56:36 -05003864
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02003865/* 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 -05003866void app_data_tls( int mfl, int cli_msg_len, int srv_msg_len,
3867 int expected_cli_fragments,
3868 int expected_srv_fragments )
3869{
3870 test_app_data( mfl, cli_msg_len, srv_msg_len, expected_cli_fragments,
3871 expected_srv_fragments, 0 );
3872 /* The goto below is used to avoid an "unused label" warning.*/
3873 goto exit;
3874}
3875/* END_CASE */
Andrzej Kurekda2b6782020-02-12 07:56:36 -05003876
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02003877/* 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 -05003878void app_data_dtls( int mfl, int cli_msg_len, int srv_msg_len,
3879 int expected_cli_fragments,
3880 int expected_srv_fragments )
3881{
3882 test_app_data( mfl, cli_msg_len, srv_msg_len, expected_cli_fragments,
3883 expected_srv_fragments, 1 );
3884 /* The goto below is used to avoid an "unused label" warning.*/
3885 goto exit;
3886}
3887/* END_CASE */
Andrzej Kurekda2b6782020-02-12 07:56:36 -05003888
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02003889/* 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 -05003890void handshake_serialization( )
3891{
3892 handshake_test_options options;
3893 init_handshake_options( &options );
Andrzej Kurekda2b6782020-02-12 07:56:36 -05003894
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003895 options.serialize = 1;
3896 options.dtls = 1;
3897 perform_handshake( &options );
3898 /* The goto below is used to avoid an "unused label" warning.*/
3899 goto exit;
3900}
3901/* END_CASE */
Andrzej Kurekda2b6782020-02-12 07:56:36 -05003902
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02003903/* 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 +01003904void handshake_fragmentation( int mfl, int expected_srv_hs_fragmentation, int expected_cli_hs_fragmentation)
3905{
3906 handshake_test_options options;
3907 log_pattern srv_pattern, cli_pattern;
3908
3909 srv_pattern.pattern = cli_pattern.pattern = "found fragmented DTLS handshake";
3910 srv_pattern.counter = 0;
3911 cli_pattern.counter = 0;
3912
3913 init_handshake_options( &options );
3914 options.dtls = 1;
3915 options.mfl = mfl;
Darryl Greenaad82f92019-12-02 10:53:11 +00003916 /* Set cipher to one using CBC so that record splitting can be tested */
3917 options.cipher = "TLS-DHE-RSA-WITH-AES-256-CBC-SHA256";
Piotr Nowickibde7ee82020-02-21 10:59:50 +01003918 options.srv_auth_mode = MBEDTLS_SSL_VERIFY_REQUIRED;
3919 options.srv_log_obj = &srv_pattern;
3920 options.cli_log_obj = &cli_pattern;
3921 options.srv_log_fun = log_analyzer;
3922 options.cli_log_fun = log_analyzer;
3923
3924 perform_handshake( &options );
3925
3926 /* Test if the server received a fragmented handshake */
3927 if( expected_srv_hs_fragmentation )
3928 {
3929 TEST_ASSERT( srv_pattern.counter >= 1 );
3930 }
3931 /* Test if the client received a fragmented handshake */
3932 if( expected_cli_hs_fragmentation )
3933 {
3934 TEST_ASSERT( cli_pattern.counter >= 1 );
3935 }
3936}
3937/* END_CASE */
3938
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02003939/* 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 -05003940void renegotiation( int legacy_renegotiation )
3941{
3942 handshake_test_options options;
3943 init_handshake_options( &options );
Andrzej Kurekda2b6782020-02-12 07:56:36 -05003944
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003945 options.renegotiate = 1;
3946 options.legacy_renegotiation = legacy_renegotiation;
3947 options.dtls = 1;
Andrzej Kurek316da1f2020-02-26 09:03:47 -05003948
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003949 perform_handshake( &options );
3950 /* The goto below is used to avoid an "unused label" warning.*/
3951 goto exit;
Andrzej Kurekf40daa32020-02-04 09:00:01 -05003952}
3953/* END_CASE */
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003954
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02003955/* 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 -05003956void resize_buffers( int mfl, int renegotiation, int legacy_renegotiation,
Andrzej Kurek8ea68722020-04-03 06:40:47 -04003957 int serialize, int dtls, char *cipher )
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003958{
3959 handshake_test_options options;
3960 init_handshake_options( &options );
3961
3962 options.mfl = mfl;
Andrzej Kurek8ea68722020-04-03 06:40:47 -04003963 options.cipher = cipher;
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003964 options.renegotiate = renegotiation;
3965 options.legacy_renegotiation = legacy_renegotiation;
3966 options.serialize = serialize;
3967 options.dtls = dtls;
3968 options.resize_buffers = 1;
3969
3970 perform_handshake( &options );
3971 /* The goto below is used to avoid an "unused label" warning.*/
3972 goto exit;
3973}
3974/* END_CASE */
3975
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02003976/* 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 -05003977void resize_buffers_serialize_mfl( int mfl )
3978{
Andrzej Kurek8ea68722020-04-03 06:40:47 -04003979 test_resize_buffers( mfl, 0, MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION, 1, 1,
3980 (char *) "" );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003981
3982 /* The goto below is used to avoid an "unused label" warning.*/
3983 goto exit;
3984}
3985/* END_CASE */
3986
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02003987/* 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 -04003988void resize_buffers_renegotiate_mfl( int mfl, int legacy_renegotiation,
3989 char *cipher )
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003990{
Andrzej Kurek8ea68722020-04-03 06:40:47 -04003991 test_resize_buffers( mfl, 1, legacy_renegotiation, 0, 1, cipher );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003992
3993 /* The goto below is used to avoid an "unused label" warning.*/
3994 goto exit;
3995}
3996/* END_CASE */