blob: 205abc60e37d683dc53a5c23fc709a12396145ba [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
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100704#if defined(MBEDTLS_X509_CRT_PARSE_C)
705
706/*
707 * Structure with endpoint's certificates for SSL communication tests.
708 */
709typedef struct mbedtls_endpoint_certificate
710{
711 mbedtls_x509_crt ca_cert;
712 mbedtls_x509_crt cert;
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100713 mbedtls_pk_context pkey;
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100714} mbedtls_endpoint_certificate;
715
716/*
717 * Endpoint structure for SSL communication tests.
718 */
719typedef struct mbedtls_endpoint
720{
721 const char *name;
722 mbedtls_ssl_context ssl;
723 mbedtls_ssl_config conf;
724 mbedtls_ctr_drbg_context ctr_drbg;
725 mbedtls_entropy_context entropy;
726 mbedtls_mock_socket socket;
727 mbedtls_endpoint_certificate cert;
728} mbedtls_endpoint;
729
730/*
731 * Initializes \p ep_cert structure and assigns it to endpoint
732 * represented by \p ep.
733 *
734 * \retval 0 on success, otherwise error code.
735 */
Andrzej Kurekb2980742020-02-02 19:25:26 -0500736int mbedtls_endpoint_certificate_init( mbedtls_endpoint *ep, int pk_alg )
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100737{
738 int i = 0;
739 int ret = -1;
740 mbedtls_endpoint_certificate *cert;
741
742 if( ep == NULL )
743 {
744 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
745 }
746
747 cert = &( ep->cert );
748 mbedtls_x509_crt_init( &( cert->ca_cert ) );
749 mbedtls_x509_crt_init( &( cert->cert ) );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100750 mbedtls_pk_init( &( cert->pkey ) );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100751
752 /* Load the trusted CA */
753
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100754 for( i = 0; mbedtls_test_cas_der[i] != NULL; i++ )
755 {
756 ret = mbedtls_x509_crt_parse_der( &( cert->ca_cert ),
757 (const unsigned char *) mbedtls_test_cas_der[i],
758 mbedtls_test_cas_der_len[i] );
759 TEST_ASSERT( ret == 0 );
760 }
761
762 /* Load own certificate and private key */
763
764 if( ep->conf.endpoint == MBEDTLS_SSL_IS_SERVER )
765 {
Andrzej Kurekb2980742020-02-02 19:25:26 -0500766 if( pk_alg == MBEDTLS_PK_RSA )
767 {
768 ret = mbedtls_x509_crt_parse( &( cert->cert ),
769 (const unsigned char*) mbedtls_test_srv_crt_rsa_sha256_der,
770 mbedtls_test_srv_crt_rsa_sha256_der_len );
771 TEST_ASSERT( ret == 0 );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100772
Andrzej Kurekb2980742020-02-02 19:25:26 -0500773 ret = mbedtls_pk_parse_key( &( cert->pkey ),
774 (const unsigned char*) mbedtls_test_srv_key_rsa_der,
775 mbedtls_test_srv_key_rsa_der_len, NULL, 0 );
776 TEST_ASSERT( ret == 0 );
777 }
778 else
779 {
780 ret = mbedtls_x509_crt_parse( &( cert->cert ),
781 (const unsigned char*) mbedtls_test_srv_crt_ec_der,
782 mbedtls_test_srv_crt_ec_der_len );
783 TEST_ASSERT( ret == 0 );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100784
Andrzej Kurekb2980742020-02-02 19:25:26 -0500785 ret = mbedtls_pk_parse_key( &( cert->pkey ),
786 (const unsigned char*) mbedtls_test_srv_key_ec_der,
787 mbedtls_test_srv_key_ec_der_len, NULL, 0 );
788 TEST_ASSERT( ret == 0 );
789 }
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100790 }
791 else
792 {
Andrzej Kurekb2980742020-02-02 19:25:26 -0500793 if( pk_alg == MBEDTLS_PK_RSA )
794 {
795 ret = mbedtls_x509_crt_parse( &( cert->cert ),
796 (const unsigned char *) mbedtls_test_cli_crt_rsa_der,
797 mbedtls_test_cli_crt_rsa_der_len );
798 TEST_ASSERT( ret == 0 );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100799
Andrzej Kurekb2980742020-02-02 19:25:26 -0500800 ret = mbedtls_pk_parse_key( &( cert->pkey ),
801 (const unsigned char *) mbedtls_test_cli_key_rsa_der,
802 mbedtls_test_cli_key_rsa_der_len, NULL, 0 );
803 TEST_ASSERT( ret == 0 );
804 }
805 else
806 {
807 ret = mbedtls_x509_crt_parse( &( cert->cert ),
808 (const unsigned char *) mbedtls_test_cli_crt_ec_der,
809 mbedtls_test_cli_crt_ec_len );
810 TEST_ASSERT( ret == 0 );
811
812 ret = mbedtls_pk_parse_key( &( cert->pkey ),
813 (const unsigned char *) mbedtls_test_cli_key_ec_der,
814 mbedtls_test_cli_key_ec_der_len, NULL, 0 );
815 TEST_ASSERT( ret == 0 );
816 }
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100817 }
818
819 mbedtls_ssl_conf_ca_chain( &( ep->conf ), &( cert->ca_cert ), NULL );
820
Andrzej Kurekb2980742020-02-02 19:25:26 -0500821 ret = mbedtls_ssl_conf_own_cert( &( ep->conf ), &( cert->cert ),
822 &( cert->pkey ) );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100823 TEST_ASSERT( ret == 0 );
824
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100825exit:
826 if( ret != 0 )
827 {
828 mbedtls_x509_crt_free( &( cert->ca_cert ) );
829 mbedtls_x509_crt_free( &( cert->cert ) );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100830 mbedtls_pk_free( &( cert->pkey ) );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100831 }
832
833 return ret;
834}
835
836/*
837 * Initializes \p ep structure. It is important to call `mbedtls_endpoint_free()`
838 * after calling this function even if it fails.
839 *
840 * \p endpoint_type must be set as MBEDTLS_SSL_IS_SERVER or
841 * MBEDTLS_SSL_IS_CLIENT.
Andrzej Kurek15daf502020-02-12 09:17:52 -0500842 * \p pk_alg the algorithm to use, currently only MBEDTLS_PK_RSA and
843 * MBEDTLS_PK_ECDSA are supported.
844 * \p dtls_context - in case of DTLS - this is the context handling metadata.
845 * \p input_queue - used only in case of DTLS.
846 * \p output_queue - used only in case of DTLS.
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100847 *
848 * \retval 0 on success, otherwise error code.
849 */
Andrzej Kurek15daf502020-02-12 09:17:52 -0500850int mbedtls_endpoint_init( mbedtls_endpoint *ep, int endpoint_type, int pk_alg,
851 mbedtls_test_message_socket_context *dtls_context,
852 mbedtls_test_message_queue *input_queue,
853 mbedtls_test_message_queue *output_queue )
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100854{
855 int ret = -1;
856
Andrzej Kurek15daf502020-02-12 09:17:52 -0500857 if( dtls_context != NULL && ( input_queue == NULL || output_queue == NULL ) )
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100858 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Andrzej Kurek15daf502020-02-12 09:17:52 -0500859
860 if( ep == NULL )
861 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100862
863 memset( ep, 0, sizeof( *ep ) );
864
865 ep->name = ( endpoint_type == MBEDTLS_SSL_IS_SERVER ) ? "Server" : "Client";
866
867 mbedtls_ssl_init( &( ep->ssl ) );
868 mbedtls_ssl_config_init( &( ep->conf ) );
869 mbedtls_ctr_drbg_init( &( ep->ctr_drbg ) );
870 mbedtls_ssl_conf_rng( &( ep->conf ),
871 mbedtls_ctr_drbg_random,
872 &( ep->ctr_drbg ) );
873 mbedtls_entropy_init( &( ep->entropy ) );
Andrzej Kurek15daf502020-02-12 09:17:52 -0500874 if( dtls_context != NULL )
875 {
876 TEST_ASSERT( mbedtls_message_socket_setup( input_queue, output_queue,
877 100, &( ep->socket ),
878 dtls_context ) == 0 );
879 }
880 else
881 {
882 mbedtls_mock_socket_init( &( ep->socket ) );
883 }
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100884
885 ret = mbedtls_ctr_drbg_seed( &( ep->ctr_drbg ), mbedtls_entropy_func,
886 &( ep->entropy ), (const unsigned char *) ( ep->name ),
887 strlen( ep->name ) );
888 TEST_ASSERT( ret == 0 );
889
890 /* Non-blocking callbacks without timeout */
Andrzej Kurek15daf502020-02-12 09:17:52 -0500891 if( dtls_context != NULL )
892 {
893 mbedtls_ssl_set_bio( &( ep->ssl ), dtls_context,
894 mbedtls_mock_tcp_send_msg,
895 mbedtls_mock_tcp_recv_msg,
896 NULL );
897 }
898 else
899 {
900 mbedtls_ssl_set_bio( &( ep->ssl ), &( ep->socket ),
901 mbedtls_mock_tcp_send_nb,
902 mbedtls_mock_tcp_recv_nb,
903 NULL );
904 }
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100905
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100906 ret = mbedtls_ssl_config_defaults( &( ep->conf ), endpoint_type,
Andrzej Kurek15daf502020-02-12 09:17:52 -0500907 ( dtls_context != NULL ) ?
908 MBEDTLS_SSL_TRANSPORT_DATAGRAM :
909 MBEDTLS_SSL_TRANSPORT_STREAM,
910 MBEDTLS_SSL_PRESET_DEFAULT );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100911 TEST_ASSERT( ret == 0 );
912
Andrzej Kurek1a44a152020-02-07 08:21:32 -0500913 ret = mbedtls_ssl_setup( &( ep->ssl ), &( ep->conf ) );
914 TEST_ASSERT( ret == 0 );
Andrzej Kurek15daf502020-02-12 09:17:52 -0500915
916#if defined(MBEDTLS_SSL_PROTO_DTLS) && defined(MBEDTLS_SSL_SRV_C)
917 if( endpoint_type == MBEDTLS_SSL_IS_SERVER && dtls_context != NULL )
918 mbedtls_ssl_conf_dtls_cookies( &( ep->conf ), NULL, NULL, NULL );
919#endif
920
Andrzej Kurekb2980742020-02-02 19:25:26 -0500921 ret = mbedtls_endpoint_certificate_init( ep, pk_alg );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100922 TEST_ASSERT( ret == 0 );
923
924exit:
925 return ret;
926}
927
928/*
929 * Deinitializes certificates from endpoint represented by \p ep.
930 */
931void mbedtls_endpoint_certificate_free( mbedtls_endpoint *ep )
932{
933 mbedtls_endpoint_certificate *cert = &( ep->cert );
934 mbedtls_x509_crt_free( &( cert->ca_cert ) );
935 mbedtls_x509_crt_free( &( cert->cert ) );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100936 mbedtls_pk_free( &( cert->pkey ) );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100937}
938
939/*
940 * Deinitializes endpoint represented by \p ep.
941 */
Andrzej Kurek15daf502020-02-12 09:17:52 -0500942void mbedtls_endpoint_free( mbedtls_endpoint *ep,
943 mbedtls_test_message_socket_context *context )
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100944{
945 mbedtls_endpoint_certificate_free( ep );
946
947 mbedtls_ssl_free( &( ep->ssl ) );
948 mbedtls_ssl_config_free( &( ep->conf ) );
949 mbedtls_ctr_drbg_free( &( ep->ctr_drbg ) );
950 mbedtls_entropy_free( &( ep->entropy ) );
Andrzej Kurek15daf502020-02-12 09:17:52 -0500951
952 if( context != NULL )
953 {
954 mbedtls_message_socket_close( context );
955 }
956 else
957 {
958 mbedtls_mock_socket_close( &( ep->socket ) );
959 }
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100960}
961
962/*
963 * This function moves ssl handshake from \p ssl to prescribed \p state.
964 * /p second_ssl is used as second endpoint and their sockets have to be
965 * connected before calling this function.
966 *
967 * \retval 0 on success, otherwise error code.
968 */
969int mbedtls_move_handshake_to_state( mbedtls_ssl_context *ssl,
970 mbedtls_ssl_context *second_ssl,
971 int state )
972{
973 enum { BUFFSIZE = 1024 };
974 int max_steps = 1000;
975 int ret = 0;
976
977 if( ssl == NULL || second_ssl == NULL )
978 {
979 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
980 }
981
982 /* Perform communication via connected sockets */
983 while( ( ssl->state != state ) && ( --max_steps >= 0 ) )
984 {
985 /* If /p second_ssl ends the handshake procedure before /p ssl then
986 * there is no need to call the next step */
987 if( second_ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
988 {
989 ret = mbedtls_ssl_handshake_step( second_ssl );
990 if( ret != 0 && ret != MBEDTLS_ERR_SSL_WANT_READ &&
991 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
992 {
993 return ret;
994 }
995 }
996
997 /* We only care about the \p ssl state and returns, so we call it last,
998 * to leave the iteration as soon as the state is as expected. */
999 ret = mbedtls_ssl_handshake_step( ssl );
1000 if( ret != 0 && ret != MBEDTLS_ERR_SSL_WANT_READ &&
1001 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
1002 {
1003 return ret;
1004 }
1005 }
1006
1007 return ( max_steps >= 0 ) ? ret : -1;
1008}
1009
1010#endif /* MBEDTLS_X509_CRT_PARSE_C */
1011
Janos Follath3766ba52019-11-27 13:31:42 +00001012/*
Piotr Nowicki438bf3b2020-03-10 12:59:10 +01001013 * Write application data. Increase write counter if necessary.
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001014 */
1015int mbedtls_ssl_write_fragment( mbedtls_ssl_context *ssl, unsigned char *buf,
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001016 int buf_len, int *written,
Piotr Nowicki438bf3b2020-03-10 12:59:10 +01001017 const int expected_fragments )
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001018{
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001019 int ret = mbedtls_ssl_write( ssl, buf + *written, buf_len - *written );
1020 if( ret > 0 )
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001021 {
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001022 *written += ret;
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001023 }
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001024
1025 if( expected_fragments == 0 )
1026 {
1027 /* Used for DTLS and the message size larger than MFL. In that case
1028 * the message can not be fragmented and the library should return
1029 * MBEDTLS_ERR_SSL_BAD_INPUT_DATA error. This error must be returned
1030 * to prevent a dead loop inside mbedtls_exchange_data(). */
1031 return ret;
1032 }
1033 else if( expected_fragments == 1 )
1034 {
1035 /* Used for TLS/DTLS and the message size lower than MFL */
1036 TEST_ASSERT( ret == buf_len ||
1037 ret == MBEDTLS_ERR_SSL_WANT_READ ||
1038 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
1039 }
1040 else
1041 {
1042 /* Used for TLS and the message size larger than MFL */
1043 TEST_ASSERT( expected_fragments > 1 );
1044 TEST_ASSERT( ( ret >= 0 && ret <= buf_len ) ||
1045 ret == MBEDTLS_ERR_SSL_WANT_READ ||
1046 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
1047 }
1048
1049 return 0;
1050
1051exit:
1052 /* Some of the tests failed */
1053 return -1;
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001054}
1055
1056/*
Piotr Nowicki438bf3b2020-03-10 12:59:10 +01001057 * Read application data and increase read counter and fragments counter if necessary.
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001058 */
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001059int mbedtls_ssl_read_fragment( mbedtls_ssl_context *ssl, unsigned char *buf,
1060 int buf_len, int *read,
Piotr Nowicki438bf3b2020-03-10 12:59:10 +01001061 int *fragments, const int expected_fragments )
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001062{
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001063 int ret = mbedtls_ssl_read( ssl, buf + *read, buf_len - *read );
1064 if( ret > 0 )
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001065 {
Piotr Nowicki438bf3b2020-03-10 12:59:10 +01001066 ( *fragments )++;
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001067 *read += ret;
1068 }
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001069
1070 if( expected_fragments == 0 )
1071 {
1072 TEST_ASSERT( ret == 0 );
1073 }
1074 else if( expected_fragments == 1 )
1075 {
1076 TEST_ASSERT( ret == buf_len ||
1077 ret == MBEDTLS_ERR_SSL_WANT_READ ||
1078 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
1079 }
1080 else
1081 {
1082 TEST_ASSERT( expected_fragments > 1 );
1083 TEST_ASSERT( ( ret >= 0 && ret <= buf_len ) ||
1084 ret == MBEDTLS_ERR_SSL_WANT_READ ||
1085 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
1086 }
1087
1088 return 0;
1089
1090exit:
1091 /* Some of the tests failed */
1092 return -1;
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001093}
1094
1095/*
Hanno Beckera18d1322018-01-03 14:27:32 +00001096 * Helper function setting up inverse record transformations
1097 * using given cipher, hash, EtM mode, authentication tag length,
1098 * and version.
1099 */
1100
1101#define CHK( x ) \
1102 do \
1103 { \
1104 if( !( x ) ) \
Hanno Becker81e16a32019-03-01 11:21:44 +00001105 { \
Hanno Beckera5780f12019-04-05 09:55:37 +01001106 ret = -1; \
Hanno Becker81e16a32019-03-01 11:21:44 +00001107 goto cleanup; \
1108 } \
Hanno Beckera18d1322018-01-03 14:27:32 +00001109 } while( 0 )
1110
Andrzej Kurekf40daa32020-02-04 09:00:01 -05001111void set_ciphersuite( mbedtls_ssl_config *conf, const char *cipher,
1112 int* forced_ciphersuite )
1113{
1114 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
1115 forced_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id( cipher );
1116 forced_ciphersuite[1] = 0;
1117
1118 ciphersuite_info =
1119 mbedtls_ssl_ciphersuite_from_id( forced_ciphersuite[0] );
1120
1121 TEST_ASSERT( ciphersuite_info != NULL );
1122 TEST_ASSERT( ciphersuite_info->min_minor_ver <= conf->max_minor_ver );
1123 TEST_ASSERT( ciphersuite_info->max_minor_ver >= conf->min_minor_ver );
1124
1125 if( conf->max_minor_ver > ciphersuite_info->max_minor_ver )
1126 {
1127 conf->max_minor_ver = ciphersuite_info->max_minor_ver;
1128 }
1129 if( conf->min_minor_ver < ciphersuite_info->min_minor_ver )
1130 {
1131 conf->min_minor_ver = ciphersuite_info->min_minor_ver;
1132 }
1133
1134 mbedtls_ssl_conf_ciphersuites( conf, forced_ciphersuite );
1135
1136exit:
1137 return;
1138}
1139
Andrzej Kurekcc5169c2020-02-04 09:04:56 -05001140int psk_dummy_callback( void *p_info, mbedtls_ssl_context *ssl,
1141 const unsigned char *name, size_t name_len )
1142{
1143 (void) p_info;
1144 (void) ssl;
1145 (void) name;
1146 (void) name_len;
1147
1148 return ( 0 );
1149}
1150
Hanno Beckerd856c822019-04-29 17:30:59 +01001151#if MBEDTLS_SSL_CID_OUT_LEN_MAX > MBEDTLS_SSL_CID_IN_LEN_MAX
1152#define SSL_CID_LEN_MIN MBEDTLS_SSL_CID_IN_LEN_MAX
1153#else
1154#define SSL_CID_LEN_MIN MBEDTLS_SSL_CID_OUT_LEN_MAX
1155#endif
Hanno Beckera18d1322018-01-03 14:27:32 +00001156
1157static int build_transforms( mbedtls_ssl_transform *t_in,
1158 mbedtls_ssl_transform *t_out,
1159 int cipher_type, int hash_id,
Hanno Beckerd856c822019-04-29 17:30:59 +01001160 int etm, int tag_mode, int ver,
1161 size_t cid0_len,
1162 size_t cid1_len )
Hanno Beckera18d1322018-01-03 14:27:32 +00001163{
1164 mbedtls_cipher_info_t const *cipher_info;
Hanno Beckera5780f12019-04-05 09:55:37 +01001165 int ret = 0;
Hanno Beckera18d1322018-01-03 14:27:32 +00001166
1167 size_t keylen, maclen, ivlen;
Hanno Becker81e16a32019-03-01 11:21:44 +00001168 unsigned char *key0 = NULL, *key1 = NULL;
Hanno Beckera18d1322018-01-03 14:27:32 +00001169 unsigned char iv_enc[16], iv_dec[16];
1170
Hanno Beckera0e20d02019-05-15 14:03:01 +01001171#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd856c822019-04-29 17:30:59 +01001172 unsigned char cid0[ SSL_CID_LEN_MIN ];
1173 unsigned char cid1[ SSL_CID_LEN_MIN ];
1174
1175 rnd_std_rand( NULL, cid0, sizeof( cid0 ) );
1176 rnd_std_rand( NULL, cid1, sizeof( cid1 ) );
Hanno Becker43c24b82019-05-01 09:45:57 +01001177#else
1178 ((void) cid0_len);
1179 ((void) cid1_len);
Hanno Beckera0e20d02019-05-15 14:03:01 +01001180#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerd856c822019-04-29 17:30:59 +01001181
Hanno Beckera18d1322018-01-03 14:27:32 +00001182 maclen = 0;
1183
1184 /* Pick cipher */
1185 cipher_info = mbedtls_cipher_info_from_type( cipher_type );
1186 CHK( cipher_info != NULL );
1187 CHK( cipher_info->iv_size <= 16 );
1188 CHK( cipher_info->key_bitlen % 8 == 0 );
1189
1190 /* Pick keys */
1191 keylen = cipher_info->key_bitlen / 8;
Hanno Becker78d1f702019-04-05 09:56:10 +01001192 /* Allocate `keylen + 1` bytes to ensure that we get
1193 * a non-NULL pointers from `mbedtls_calloc` even if
1194 * `keylen == 0` in the case of the NULL cipher. */
1195 CHK( ( key0 = mbedtls_calloc( 1, keylen + 1 ) ) != NULL );
1196 CHK( ( key1 = mbedtls_calloc( 1, keylen + 1 ) ) != NULL );
Hanno Beckera18d1322018-01-03 14:27:32 +00001197 memset( key0, 0x1, keylen );
1198 memset( key1, 0x2, keylen );
1199
1200 /* Setup cipher contexts */
1201 CHK( mbedtls_cipher_setup( &t_in->cipher_ctx_enc, cipher_info ) == 0 );
1202 CHK( mbedtls_cipher_setup( &t_in->cipher_ctx_dec, cipher_info ) == 0 );
1203 CHK( mbedtls_cipher_setup( &t_out->cipher_ctx_enc, cipher_info ) == 0 );
1204 CHK( mbedtls_cipher_setup( &t_out->cipher_ctx_dec, cipher_info ) == 0 );
1205
1206#if defined(MBEDTLS_CIPHER_MODE_CBC)
1207 if( cipher_info->mode == MBEDTLS_MODE_CBC )
1208 {
1209 CHK( mbedtls_cipher_set_padding_mode( &t_in->cipher_ctx_enc,
1210 MBEDTLS_PADDING_NONE ) == 0 );
1211 CHK( mbedtls_cipher_set_padding_mode( &t_in->cipher_ctx_dec,
1212 MBEDTLS_PADDING_NONE ) == 0 );
1213 CHK( mbedtls_cipher_set_padding_mode( &t_out->cipher_ctx_enc,
1214 MBEDTLS_PADDING_NONE ) == 0 );
1215 CHK( mbedtls_cipher_set_padding_mode( &t_out->cipher_ctx_dec,
1216 MBEDTLS_PADDING_NONE ) == 0 );
1217 }
1218#endif /* MBEDTLS_CIPHER_MODE_CBC */
1219
1220 CHK( mbedtls_cipher_setkey( &t_in->cipher_ctx_enc, key0,
1221 keylen << 3, MBEDTLS_ENCRYPT ) == 0 );
1222 CHK( mbedtls_cipher_setkey( &t_in->cipher_ctx_dec, key1,
1223 keylen << 3, MBEDTLS_DECRYPT ) == 0 );
1224 CHK( mbedtls_cipher_setkey( &t_out->cipher_ctx_enc, key1,
1225 keylen << 3, MBEDTLS_ENCRYPT ) == 0 );
1226 CHK( mbedtls_cipher_setkey( &t_out->cipher_ctx_dec, key0,
1227 keylen << 3, MBEDTLS_DECRYPT ) == 0 );
Hanno Beckera18d1322018-01-03 14:27:32 +00001228
1229 /* Setup MAC contexts */
1230#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1231 if( cipher_info->mode == MBEDTLS_MODE_CBC ||
1232 cipher_info->mode == MBEDTLS_MODE_STREAM )
1233 {
1234 mbedtls_md_info_t const *md_info;
1235 unsigned char *md0, *md1;
1236
1237 /* Pick hash */
1238 md_info = mbedtls_md_info_from_type( hash_id );
1239 CHK( md_info != NULL );
1240
1241 /* Pick hash keys */
1242 maclen = mbedtls_md_get_size( md_info );
Hanno Becker3ee54212019-04-04 16:31:26 +01001243 CHK( ( md0 = mbedtls_calloc( 1, maclen ) ) != NULL );
1244 CHK( ( md1 = mbedtls_calloc( 1, maclen ) ) != NULL );
Hanno Beckera18d1322018-01-03 14:27:32 +00001245 memset( md0, 0x5, maclen );
1246 memset( md1, 0x6, maclen );
1247
1248 CHK( mbedtls_md_setup( &t_out->md_ctx_enc, md_info, 1 ) == 0 );
1249 CHK( mbedtls_md_setup( &t_out->md_ctx_dec, md_info, 1 ) == 0 );
1250 CHK( mbedtls_md_setup( &t_in->md_ctx_enc, md_info, 1 ) == 0 );
1251 CHK( mbedtls_md_setup( &t_in->md_ctx_dec, md_info, 1 ) == 0 );
1252
1253 if( ver > MBEDTLS_SSL_MINOR_VERSION_0 )
1254 {
1255 CHK( mbedtls_md_hmac_starts( &t_in->md_ctx_enc,
1256 md0, maclen ) == 0 );
1257 CHK( mbedtls_md_hmac_starts( &t_in->md_ctx_dec,
1258 md1, maclen ) == 0 );
1259 CHK( mbedtls_md_hmac_starts( &t_out->md_ctx_enc,
1260 md1, maclen ) == 0 );
1261 CHK( mbedtls_md_hmac_starts( &t_out->md_ctx_dec,
1262 md0, maclen ) == 0 );
1263 }
1264#if defined(MBEDTLS_SSL_PROTO_SSL3)
1265 else
1266 {
1267 memcpy( &t_in->mac_enc, md0, maclen );
1268 memcpy( &t_in->mac_dec, md1, maclen );
1269 memcpy( &t_out->mac_enc, md1, maclen );
1270 memcpy( &t_out->mac_dec, md0, maclen );
1271 }
1272#endif
1273
Hanno Becker3ee54212019-04-04 16:31:26 +01001274 mbedtls_free( md0 );
1275 mbedtls_free( md1 );
Hanno Beckera18d1322018-01-03 14:27:32 +00001276 }
1277#else
1278 ((void) hash_id);
1279#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1280
1281
1282 /* Pick IV's (regardless of whether they
1283 * are being used by the transform). */
1284 ivlen = cipher_info->iv_size;
1285 memset( iv_enc, 0x3, sizeof( iv_enc ) );
1286 memset( iv_dec, 0x4, sizeof( iv_dec ) );
1287
1288 /*
1289 * Setup transforms
1290 */
1291
Jaeden Amero2de07f12019-06-05 13:32:08 +01001292#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
1293 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Hanno Beckera18d1322018-01-03 14:27:32 +00001294 t_out->encrypt_then_mac = etm;
1295 t_in->encrypt_then_mac = etm;
1296#else
1297 ((void) etm);
1298#endif
1299
1300 t_out->minor_ver = ver;
1301 t_in->minor_ver = ver;
1302 t_out->ivlen = ivlen;
1303 t_in->ivlen = ivlen;
1304
1305 switch( cipher_info->mode )
1306 {
1307 case MBEDTLS_MODE_GCM:
1308 case MBEDTLS_MODE_CCM:
Hanno Beckere6832872020-05-28 08:29:58 +01001309#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
1310 if( ver == MBEDTLS_SSL_MINOR_VERSION_4 )
1311 {
1312 t_out->fixed_ivlen = 12;
1313 t_in->fixed_ivlen = 12;
1314 }
1315 else
1316#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
1317 {
1318 t_out->fixed_ivlen = 4;
1319 t_in->fixed_ivlen = 4;
1320 }
Hanno Beckera18d1322018-01-03 14:27:32 +00001321 t_out->maclen = 0;
1322 t_in->maclen = 0;
1323 switch( tag_mode )
1324 {
1325 case 0: /* Full tag */
1326 t_out->taglen = 16;
1327 t_in->taglen = 16;
1328 break;
1329 case 1: /* Partial tag */
1330 t_out->taglen = 8;
1331 t_in->taglen = 8;
1332 break;
1333 default:
1334 return( 1 );
1335 }
1336 break;
1337
1338 case MBEDTLS_MODE_CHACHAPOLY:
1339 t_out->fixed_ivlen = 12;
1340 t_in->fixed_ivlen = 12;
1341 t_out->maclen = 0;
1342 t_in->maclen = 0;
1343 switch( tag_mode )
1344 {
1345 case 0: /* Full tag */
1346 t_out->taglen = 16;
1347 t_in->taglen = 16;
1348 break;
1349 case 1: /* Partial tag */
1350 t_out->taglen = 8;
1351 t_in->taglen = 8;
1352 break;
1353 default:
1354 return( 1 );
1355 }
1356 break;
1357
1358 case MBEDTLS_MODE_STREAM:
1359 case MBEDTLS_MODE_CBC:
1360 t_out->fixed_ivlen = 0; /* redundant, must be 0 */
1361 t_in->fixed_ivlen = 0; /* redundant, must be 0 */
1362 t_out->taglen = 0;
1363 t_in->taglen = 0;
1364 switch( tag_mode )
1365 {
1366 case 0: /* Full tag */
1367 t_out->maclen = maclen;
1368 t_in->maclen = maclen;
1369 break;
1370 case 1: /* Partial tag */
1371 t_out->maclen = 10;
1372 t_in->maclen = 10;
1373 break;
1374 default:
1375 return( 1 );
1376 }
1377 break;
1378 default:
1379 return( 1 );
1380 break;
1381 }
1382
1383 /* Setup IV's */
1384
1385 memcpy( &t_in->iv_dec, iv_dec, sizeof( iv_dec ) );
1386 memcpy( &t_in->iv_enc, iv_enc, sizeof( iv_enc ) );
1387 memcpy( &t_out->iv_dec, iv_enc, sizeof( iv_enc ) );
1388 memcpy( &t_out->iv_enc, iv_dec, sizeof( iv_dec ) );
1389
Hanno Beckera0e20d02019-05-15 14:03:01 +01001390#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd856c822019-04-29 17:30:59 +01001391 /* Add CID */
1392 memcpy( &t_in->in_cid, cid0, cid0_len );
1393 memcpy( &t_in->out_cid, cid1, cid1_len );
1394 t_in->in_cid_len = cid0_len;
1395 t_in->out_cid_len = cid1_len;
1396 memcpy( &t_out->in_cid, cid1, cid1_len );
1397 memcpy( &t_out->out_cid, cid0, cid0_len );
1398 t_out->in_cid_len = cid1_len;
1399 t_out->out_cid_len = cid0_len;
Hanno Beckera0e20d02019-05-15 14:03:01 +01001400#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerd856c822019-04-29 17:30:59 +01001401
Hanno Becker81e16a32019-03-01 11:21:44 +00001402cleanup:
1403
Hanno Becker3ee54212019-04-04 16:31:26 +01001404 mbedtls_free( key0 );
1405 mbedtls_free( key1 );
Hanno Becker81e16a32019-03-01 11:21:44 +00001406
Hanno Beckera5780f12019-04-05 09:55:37 +01001407 return( ret );
Hanno Beckera18d1322018-01-03 14:27:32 +00001408}
1409
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001410/*
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02001411 * Populate a session structure for serialization tests.
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001412 * Choose dummy values, mostly non-0 to distinguish from the init default.
1413 */
1414static int ssl_populate_session( mbedtls_ssl_session *session,
Manuel Pégourié-Gonnard220403b2019-05-24 09:54:21 +02001415 int ticket_len,
1416 const char *crt_file )
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001417{
1418#if defined(MBEDTLS_HAVE_TIME)
1419 session->start = mbedtls_time( NULL ) - 42;
1420#endif
1421 session->ciphersuite = 0xabcd;
1422 session->compression = 1;
1423 session->id_len = sizeof( session->id );
1424 memset( session->id, 66, session->id_len );
Manuel Pégourié-Gonnard220403b2019-05-24 09:54:21 +02001425 memset( session->master, 17, sizeof( session->master ) );
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001426
Manuel Pégourié-Gonnard1f6033a2019-05-24 10:17:52 +02001427#if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001428 if( strlen( crt_file ) != 0 )
1429 {
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02001430 mbedtls_x509_crt tmp_crt;
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001431 int ret;
Manuel Pégourié-Gonnard6b840702019-05-24 09:40:17 +02001432
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02001433 mbedtls_x509_crt_init( &tmp_crt );
1434 ret = mbedtls_x509_crt_parse_file( &tmp_crt, crt_file );
1435 if( ret != 0 )
1436 return( ret );
1437
1438#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1439 /* Move temporary CRT. */
Manuel Pégourié-Gonnard6b840702019-05-24 09:40:17 +02001440 session->peer_cert = mbedtls_calloc( 1, sizeof( *session->peer_cert ) );
1441 if( session->peer_cert == NULL )
1442 return( -1 );
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02001443 *session->peer_cert = tmp_crt;
1444 memset( &tmp_crt, 0, sizeof( tmp_crt ) );
1445#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
1446 /* Calculate digest of temporary CRT. */
1447 session->peer_cert_digest =
1448 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
1449 if( session->peer_cert_digest == NULL )
1450 return( -1 );
1451 ret = mbedtls_md( mbedtls_md_info_from_type(
1452 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
1453 tmp_crt.raw.p, tmp_crt.raw.len,
1454 session->peer_cert_digest );
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001455 if( ret != 0 )
1456 return( ret );
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02001457 session->peer_cert_digest_type =
1458 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
1459 session->peer_cert_digest_len =
1460 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
1461#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
1462
1463 mbedtls_x509_crt_free( &tmp_crt );
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001464 }
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02001465#else /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_FS_IO */
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001466 (void) crt_file;
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02001467#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_FS_IO */
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001468 session->verify_result = 0xdeadbeef;
1469
1470#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
1471 if( ticket_len != 0 )
1472 {
1473 session->ticket = mbedtls_calloc( 1, ticket_len );
Manuel Pégourié-Gonnard220403b2019-05-24 09:54:21 +02001474 if( session->ticket == NULL )
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001475 return( -1 );
1476 memset( session->ticket, 33, ticket_len );
1477 }
1478 session->ticket_len = ticket_len;
1479 session->ticket_lifetime = 86401;
1480#else
1481 (void) ticket_len;
1482#endif
1483
1484#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1485 session->mfl_code = 1;
1486#endif
1487#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1488 session->trunc_hmac = 1;
1489#endif
1490#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1491 session->encrypt_then_mac = 1;
1492#endif
1493
1494 return( 0 );
1495}
1496
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001497/*
1498 * Perform data exchanging between \p ssl_1 and \p ssl_2 and check if the
1499 * message was sent in the correct number of fragments.
1500 *
1501 * /p ssl_1 and /p ssl_2 Endpoints represented by mbedtls_ssl_context. Both
1502 * of them must be initialized and connected beforehand.
1503 * /p msg_len_1 and /p msg_len_2 specify the size of the message to send.
1504 * /p expected_fragments_1 and /p expected_fragments_2 determine in how many
1505 * fragments the message should be sent.
1506 * expected_fragments is 0: can be used for DTLS testing while the message
1507 * size is larger than MFL. In that case the message
1508 * cannot be fragmented and sent to the second endpoint.
1509 * This value can be used for negative tests.
1510 * expected_fragments is 1: can be used for TLS/DTLS testing while the
1511 * message size is below MFL
1512 * expected_fragments > 1: can be used for TLS testing while the message
1513 * size is larger than MFL
1514 *
1515 * \retval 0 on success, otherwise error code.
1516 */
1517int mbedtls_exchange_data( mbedtls_ssl_context *ssl_1,
1518 int msg_len_1, const int expected_fragments_1,
1519 mbedtls_ssl_context *ssl_2,
1520 int msg_len_2, const int expected_fragments_2 )
1521{
1522 unsigned char *msg_buf_1 = malloc( msg_len_1 );
1523 unsigned char *msg_buf_2 = malloc( msg_len_2 );
1524 unsigned char *in_buf_1 = malloc( msg_len_2 );
1525 unsigned char *in_buf_2 = malloc( msg_len_1 );
1526 int msg_type, ret = -1;
1527
1528 /* Perform this test with two message types. At first use a message
1529 * consisting of only 0x00 for the client and only 0xFF for the server.
1530 * At the second time use message with generated data */
1531 for( msg_type = 0; msg_type < 2; msg_type++ )
1532 {
1533 int written_1 = 0;
1534 int written_2 = 0;
1535 int read_1 = 0;
1536 int read_2 = 0;
1537 int fragments_1 = 0;
1538 int fragments_2 = 0;
1539
1540 if( msg_type == 0 )
1541 {
1542 memset( msg_buf_1, 0x00, msg_len_1 );
1543 memset( msg_buf_2, 0xff, msg_len_2 );
1544 }
1545 else
1546 {
1547 int i, j = 0;
1548 for( i = 0; i < msg_len_1; i++ )
1549 {
1550 msg_buf_1[i] = j++ & 0xFF;
1551 }
1552 for( i = 0; i < msg_len_2; i++ )
1553 {
1554 msg_buf_2[i] = ( j -= 5 ) & 0xFF;
1555 }
1556 }
1557
1558 while( read_1 < msg_len_2 || read_2 < msg_len_1 )
1559 {
1560 /* ssl_1 sending */
1561 if( msg_len_1 > written_1 )
1562 {
1563 ret = mbedtls_ssl_write_fragment( ssl_1, msg_buf_1,
1564 msg_len_1, &written_1,
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001565 expected_fragments_1 );
1566 if( expected_fragments_1 == 0 )
1567 {
1568 /* This error is expected when the message is too large and
1569 * cannot be fragmented */
1570 TEST_ASSERT( ret == MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1571 msg_len_1 = 0;
1572 }
1573 else
1574 {
1575 TEST_ASSERT( ret == 0 );
1576 }
1577 }
1578
1579 /* ssl_2 sending */
1580 if( msg_len_2 > written_2 )
1581 {
1582 ret = mbedtls_ssl_write_fragment( ssl_2, msg_buf_2,
1583 msg_len_2, &written_2,
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001584 expected_fragments_2 );
1585 if( expected_fragments_2 == 0 )
1586 {
1587 /* This error is expected when the message is too large and
1588 * cannot be fragmented */
1589 TEST_ASSERT( ret == MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1590 msg_len_2 = 0;
1591 }
1592 else
1593 {
1594 TEST_ASSERT( ret == 0 );
1595 }
1596 }
1597
1598 /* ssl_1 reading */
1599 if( read_1 < msg_len_2 )
1600 {
1601 ret = mbedtls_ssl_read_fragment( ssl_1, in_buf_1,
1602 msg_len_2, &read_1,
Piotr Nowicki438bf3b2020-03-10 12:59:10 +01001603 &fragments_2,
1604 expected_fragments_2 );
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001605 TEST_ASSERT( ret == 0 );
1606 }
1607
1608 /* ssl_2 reading */
1609 if( read_2 < msg_len_1 )
1610 {
1611 ret = mbedtls_ssl_read_fragment( ssl_2, in_buf_2,
1612 msg_len_1, &read_2,
Piotr Nowicki438bf3b2020-03-10 12:59:10 +01001613 &fragments_1,
1614 expected_fragments_1 );
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001615 TEST_ASSERT( ret == 0 );
1616 }
1617 }
1618
1619 ret = -1;
1620 TEST_ASSERT( 0 == memcmp( msg_buf_1, in_buf_2, msg_len_1 ) );
1621 TEST_ASSERT( 0 == memcmp( msg_buf_2, in_buf_1, msg_len_2 ) );
1622 TEST_ASSERT( fragments_1 == expected_fragments_1 );
1623 TEST_ASSERT( fragments_2 == expected_fragments_2 );
1624 }
1625
1626 ret = 0;
1627
1628exit:
1629 free( msg_buf_1 );
1630 free( in_buf_1 );
1631 free( msg_buf_2 );
1632 free( in_buf_2 );
1633
1634 return ret;
1635}
1636
Piotr Nowicki95e9eb82020-02-14 11:33:34 +01001637/*
1638 * Perform data exchanging between \p ssl_1 and \p ssl_2. Both of endpoints
1639 * must be initialized and connected beforehand.
1640 *
1641 * \retval 0 on success, otherwise error code.
1642 */
1643int exchange_data( mbedtls_ssl_context *ssl_1,
1644 mbedtls_ssl_context *ssl_2 )
1645{
1646 return mbedtls_exchange_data( ssl_1, 256, 1,
1647 ssl_2, 256, 1 );
1648}
1649
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001650#if defined(MBEDTLS_X509_CRT_PARSE_C)
1651void perform_handshake( handshake_test_options* options )
1652{
1653 /* forced_ciphersuite needs to last until the end of the handshake */
1654 int forced_ciphersuite[2];
1655 enum { BUFFSIZE = 17000 };
1656 mbedtls_endpoint client, server;
Gilles Peskineeccd8882020-03-10 12:19:08 +01001657#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001658 const char *psk_identity = "foo";
1659#endif
1660#if defined(MBEDTLS_TIMING_C)
1661 mbedtls_timing_delay_context timer_client, timer_server;
1662#endif
1663#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1664 unsigned char *context_buf = NULL;
1665 size_t context_buf_len;
1666#endif
1667#if defined(MBEDTLS_SSL_RENEGOTIATION)
1668 int ret = -1;
1669#endif
1670
1671
1672 mbedtls_test_message_queue server_queue, client_queue;
1673 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05001674 mbedtls_message_socket_init( &server_context );
1675 mbedtls_message_socket_init( &client_context );
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001676
1677 /* Client side */
1678 if( options->dtls != 0 )
1679 {
1680 TEST_ASSERT( mbedtls_endpoint_init( &client, MBEDTLS_SSL_IS_CLIENT,
1681 options->pk_alg, &client_context,
1682 &client_queue,
1683 &server_queue ) == 0 );
1684#if defined(MBEDTLS_TIMING_C)
1685 mbedtls_ssl_set_timer_cb( &client.ssl, &timer_client,
1686 mbedtls_timing_set_delay,
1687 mbedtls_timing_get_delay );
1688#endif
1689 }
1690 else
1691 {
1692 TEST_ASSERT( mbedtls_endpoint_init( &client, MBEDTLS_SSL_IS_CLIENT,
1693 options->pk_alg, NULL, NULL,
1694 NULL ) == 0 );
1695 }
1696 mbedtls_ssl_conf_min_version( &client.conf, MBEDTLS_SSL_MAJOR_VERSION_3,
1697 options->version );
1698 mbedtls_ssl_conf_max_version( &client.conf, MBEDTLS_SSL_MAJOR_VERSION_3,
1699 options->version );
1700
1701 if( strlen( options->cipher ) > 0 )
1702 {
1703 set_ciphersuite( &client.conf, options->cipher, forced_ciphersuite );
1704 }
Piotr Nowickibde7ee82020-02-21 10:59:50 +01001705
1706#if defined (MBEDTLS_DEBUG_C)
1707 if( options->cli_log_fun )
1708 {
1709 mbedtls_debug_set_threshold( 4 );
1710 mbedtls_ssl_conf_dbg( &client.conf, options->cli_log_fun,
1711 options->cli_log_obj );
1712 }
1713#endif
1714
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001715 /* Server side */
1716 if( options->dtls != 0 )
1717 {
1718 TEST_ASSERT( mbedtls_endpoint_init( &server, MBEDTLS_SSL_IS_SERVER,
1719 options->pk_alg, &server_context,
1720 &server_queue,
1721 &client_queue) == 0 );
1722#if defined(MBEDTLS_TIMING_C)
1723 mbedtls_ssl_set_timer_cb( &server.ssl, &timer_server,
1724 mbedtls_timing_set_delay,
1725 mbedtls_timing_get_delay );
1726#endif
1727 }
1728 else
1729 {
1730 TEST_ASSERT( mbedtls_endpoint_init( &server, MBEDTLS_SSL_IS_SERVER,
1731 options->pk_alg, NULL, NULL, NULL ) == 0 );
1732 }
Piotr Nowickibde7ee82020-02-21 10:59:50 +01001733
1734 mbedtls_ssl_conf_authmode( &server.conf, options->srv_auth_mode );
1735
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001736 mbedtls_ssl_conf_min_version( &server.conf, MBEDTLS_SSL_MAJOR_VERSION_3,
1737 options->version );
1738#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1739 TEST_ASSERT( mbedtls_ssl_conf_max_frag_len( &(server.conf),
1740 (unsigned char) options->mfl ) == 0 );
1741 TEST_ASSERT( mbedtls_ssl_conf_max_frag_len( &(client.conf),
1742 (unsigned char) options->mfl ) == 0 );
1743#else
1744 TEST_ASSERT( MBEDTLS_SSL_MAX_FRAG_LEN_NONE == options->mfl );
1745#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
1746
Gilles Peskineeccd8882020-03-10 12:19:08 +01001747#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001748 if( options->psk_str != NULL && options->psk_str->len > 0 )
1749 {
1750 TEST_ASSERT( mbedtls_ssl_conf_psk( &client.conf, options->psk_str->x,
1751 options->psk_str->len,
1752 (const unsigned char *) psk_identity,
1753 strlen( psk_identity ) ) == 0 );
1754
1755 TEST_ASSERT( mbedtls_ssl_conf_psk( &server.conf, options->psk_str->x,
1756 options->psk_str->len,
1757 (const unsigned char *) psk_identity,
1758 strlen( psk_identity ) ) == 0 );
1759
1760 mbedtls_ssl_conf_psk_cb( &server.conf, psk_dummy_callback, NULL );
1761 }
1762#endif
1763#if defined(MBEDTLS_SSL_RENEGOTIATION)
1764 if( options->renegotiate )
1765 {
1766 mbedtls_ssl_conf_renegotiation( &(server.conf),
1767 MBEDTLS_SSL_RENEGOTIATION_ENABLED );
1768 mbedtls_ssl_conf_renegotiation( &(client.conf),
1769 MBEDTLS_SSL_RENEGOTIATION_ENABLED );
1770
1771 mbedtls_ssl_conf_legacy_renegotiation( &(server.conf),
1772 options->legacy_renegotiation );
1773 mbedtls_ssl_conf_legacy_renegotiation( &(client.conf),
1774 options->legacy_renegotiation );
1775 }
1776#endif /* MBEDTLS_SSL_RENEGOTIATION */
1777
Piotr Nowickibde7ee82020-02-21 10:59:50 +01001778#if defined (MBEDTLS_DEBUG_C)
1779 if( options->srv_log_fun )
1780 {
1781 mbedtls_debug_set_threshold( 4 );
1782 mbedtls_ssl_conf_dbg( &server.conf, options->srv_log_fun,
1783 options->srv_log_obj );
1784 }
1785#endif
1786
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001787 TEST_ASSERT( mbedtls_mock_socket_connect( &(client.socket),
1788 &(server.socket),
1789 BUFFSIZE ) == 0 );
1790
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001791#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1792 if( options->resize_buffers != 0 )
1793 {
1794 /* Ensure that the buffer sizes are appropriate before resizes */
1795 TEST_ASSERT( client.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN );
1796 TEST_ASSERT( client.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN );
1797 TEST_ASSERT( server.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN );
1798 TEST_ASSERT( server.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN );
1799 }
1800#endif
1801
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001802 TEST_ASSERT( mbedtls_move_handshake_to_state( &(client.ssl),
1803 &(server.ssl),
1804 MBEDTLS_SSL_HANDSHAKE_OVER )
1805 == 0 );
1806 TEST_ASSERT( client.ssl.state == MBEDTLS_SSL_HANDSHAKE_OVER );
1807 TEST_ASSERT( server.ssl.state == MBEDTLS_SSL_HANDSHAKE_OVER );
1808
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001809#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1810 if( options->resize_buffers != 0 )
1811 {
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001812 if( options->version != MBEDTLS_SSL_MINOR_VERSION_0 &&
1813 options->version != MBEDTLS_SSL_MINOR_VERSION_1 )
1814 {
1815 /* A server, when using DTLS, might delay a buffer resize to happen
1816 * after it receives a message, so we force it. */
1817 TEST_ASSERT( exchange_data( &(client.ssl), &(server.ssl) ) == 0 );
1818
1819 TEST_ASSERT( client.ssl.out_buf_len ==
1820 mbedtls_ssl_get_output_buflen( &client.ssl ) );
1821 TEST_ASSERT( client.ssl.in_buf_len ==
1822 mbedtls_ssl_get_input_buflen( &client.ssl ) );
1823 TEST_ASSERT( server.ssl.out_buf_len ==
1824 mbedtls_ssl_get_output_buflen( &server.ssl ) );
1825 TEST_ASSERT( server.ssl.in_buf_len ==
1826 mbedtls_ssl_get_input_buflen( &server.ssl ) );
1827 }
1828 }
1829#endif
1830
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001831 if( options->cli_msg_len != 0 || options->srv_msg_len != 0 )
1832 {
1833 /* Start data exchanging test */
1834 TEST_ASSERT( mbedtls_exchange_data( &(client.ssl), options->cli_msg_len,
1835 options->expected_cli_fragments,
1836 &(server.ssl), options->srv_msg_len,
1837 options->expected_srv_fragments )
1838 == 0 );
1839 }
1840#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1841 if( options->serialize == 1 )
1842 {
1843 TEST_ASSERT( options->dtls == 1 );
1844
1845 TEST_ASSERT( mbedtls_ssl_context_save( &(server.ssl), NULL,
1846 0, &context_buf_len )
1847 == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1848
1849 context_buf = mbedtls_calloc( 1, context_buf_len );
1850 TEST_ASSERT( context_buf != NULL );
1851
1852 TEST_ASSERT( mbedtls_ssl_context_save( &(server.ssl), context_buf,
1853 context_buf_len,
1854 &context_buf_len ) == 0 );
1855
1856 mbedtls_ssl_free( &(server.ssl) );
1857 mbedtls_ssl_init( &(server.ssl) );
1858
1859 TEST_ASSERT( mbedtls_ssl_setup( &(server.ssl), &(server.conf) ) == 0 );
1860
1861 mbedtls_ssl_set_bio( &( server.ssl ), &server_context,
1862 mbedtls_mock_tcp_send_msg,
1863 mbedtls_mock_tcp_recv_msg,
1864 NULL );
1865
1866#if defined(MBEDTLS_TIMING_C)
1867 mbedtls_ssl_set_timer_cb( &server.ssl, &timer_server,
1868 mbedtls_timing_set_delay,
1869 mbedtls_timing_get_delay );
1870#endif
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001871#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1872 if( options->resize_buffers != 0 )
1873 {
1874 /* Ensure that the buffer sizes are appropriate before resizes */
1875 TEST_ASSERT( server.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN );
1876 TEST_ASSERT( server.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN );
1877 }
1878#endif
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001879 TEST_ASSERT( mbedtls_ssl_context_load( &( server.ssl ), context_buf,
1880 context_buf_len ) == 0 );
1881
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001882#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1883 /* Validate buffer sizes after context deserialization */
1884 if( options->resize_buffers != 0 )
1885 {
1886 TEST_ASSERT( server.ssl.out_buf_len ==
1887 mbedtls_ssl_get_output_buflen( &server.ssl ) );
1888 TEST_ASSERT( server.ssl.in_buf_len ==
1889 mbedtls_ssl_get_input_buflen( &server.ssl ) );
1890 }
1891#endif
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001892 /* Retest writing/reading */
1893 if( options->cli_msg_len != 0 || options->srv_msg_len != 0 )
1894 {
1895 TEST_ASSERT( mbedtls_exchange_data( &(client.ssl),
1896 options->cli_msg_len,
1897 options->expected_cli_fragments,
1898 &(server.ssl),
1899 options->srv_msg_len,
1900 options->expected_srv_fragments )
1901 == 0 );
1902 }
1903 }
1904#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001905
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001906#if defined(MBEDTLS_SSL_RENEGOTIATION)
1907 if( options->renegotiate )
1908 {
1909 /* Start test with renegotiation */
1910 TEST_ASSERT( server.ssl.renego_status ==
1911 MBEDTLS_SSL_INITIAL_HANDSHAKE );
1912 TEST_ASSERT( client.ssl.renego_status ==
1913 MBEDTLS_SSL_INITIAL_HANDSHAKE );
1914
1915 /* After calling this function for the server, it only sends a handshake
1916 * request. All renegotiation should happen during data exchanging */
1917 TEST_ASSERT( mbedtls_ssl_renegotiate( &(server.ssl) ) == 0 );
1918 TEST_ASSERT( server.ssl.renego_status ==
1919 MBEDTLS_SSL_RENEGOTIATION_PENDING );
1920 TEST_ASSERT( client.ssl.renego_status ==
1921 MBEDTLS_SSL_INITIAL_HANDSHAKE );
1922
1923 TEST_ASSERT( exchange_data( &(client.ssl), &(server.ssl) ) == 0 );
1924 TEST_ASSERT( server.ssl.renego_status ==
1925 MBEDTLS_SSL_RENEGOTIATION_DONE );
1926 TEST_ASSERT( client.ssl.renego_status ==
1927 MBEDTLS_SSL_RENEGOTIATION_DONE );
1928
1929 /* After calling mbedtls_ssl_renegotiate for the client all renegotiation
1930 * should happen inside this function. However in this test, we cannot
1931 * perform simultaneous communication betwen client and server so this
1932 * function will return waiting error on the socket. All rest of
1933 * renegotiation should happen during data exchanging */
1934 ret = mbedtls_ssl_renegotiate( &(client.ssl) );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001935#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1936 if( options->resize_buffers != 0 )
1937 {
1938 /* Ensure that the buffer sizes are appropriate before resizes */
1939 TEST_ASSERT( client.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN );
1940 TEST_ASSERT( client.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN );
1941 }
1942#endif
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001943 TEST_ASSERT( ret == 0 ||
1944 ret == MBEDTLS_ERR_SSL_WANT_READ ||
1945 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
1946 TEST_ASSERT( server.ssl.renego_status ==
1947 MBEDTLS_SSL_RENEGOTIATION_DONE );
1948 TEST_ASSERT( client.ssl.renego_status ==
1949 MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS );
1950
1951 TEST_ASSERT( exchange_data( &(client.ssl), &(server.ssl) ) == 0 );
1952 TEST_ASSERT( server.ssl.renego_status ==
1953 MBEDTLS_SSL_RENEGOTIATION_DONE );
1954 TEST_ASSERT( client.ssl.renego_status ==
1955 MBEDTLS_SSL_RENEGOTIATION_DONE );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001956#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1957 /* Validate buffer sizes after renegotiation */
1958 if( options->resize_buffers != 0 )
1959 {
1960 TEST_ASSERT( client.ssl.out_buf_len ==
1961 mbedtls_ssl_get_output_buflen( &client.ssl ) );
1962 TEST_ASSERT( client.ssl.in_buf_len ==
1963 mbedtls_ssl_get_input_buflen( &client.ssl ) );
1964 TEST_ASSERT( server.ssl.out_buf_len ==
1965 mbedtls_ssl_get_output_buflen( &server.ssl ) );
1966 TEST_ASSERT( server.ssl.in_buf_len ==
1967 mbedtls_ssl_get_input_buflen( &server.ssl ) );
1968 }
1969#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001970 }
1971#endif /* MBEDTLS_SSL_RENEGOTIATION */
1972
1973exit:
1974 mbedtls_endpoint_free( &client, options->dtls != 0 ? &client_context : NULL );
1975 mbedtls_endpoint_free( &server, options->dtls != 0 ? &server_context : NULL );
Piotr Nowickibde7ee82020-02-21 10:59:50 +01001976#if defined (MBEDTLS_DEBUG_C)
1977 if( options->cli_log_fun || options->srv_log_fun )
1978 {
1979 mbedtls_debug_set_threshold( 0 );
1980 }
1981#endif
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001982#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1983 if( context_buf != NULL )
1984 mbedtls_free( context_buf );
1985#endif
1986}
1987#endif /* MBEDTLS_X509_CRT_PARSE_C */
1988
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02001989/* END_HEADER */
1990
1991/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001992 * depends_on:MBEDTLS_SSL_TLS_C
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02001993 * END_DEPENDENCIES
1994 */
1995
Janos Follath6264e662019-11-26 11:11:15 +00001996/* BEGIN_CASE */
1997void test_callback_buffer_sanity()
1998{
1999 enum { MSGLEN = 10 };
2000 mbedtls_test_buffer buf;
2001 unsigned char input[MSGLEN];
2002 unsigned char output[MSGLEN];
2003
2004 memset( input, 0, sizeof(input) );
2005
2006 /* Make sure calling put and get on NULL buffer results in error. */
2007 TEST_ASSERT( mbedtls_test_buffer_put( NULL, input, sizeof( input ) )
2008 == -1 );
2009 TEST_ASSERT( mbedtls_test_buffer_get( NULL, output, sizeof( output ) )
2010 == -1 );
2011 TEST_ASSERT( mbedtls_test_buffer_put( NULL, NULL, sizeof( input ) ) == -1 );
Andrzej Kurekf7774142020-01-22 06:34:59 -05002012
Janos Follath6264e662019-11-26 11:11:15 +00002013 TEST_ASSERT( mbedtls_test_buffer_put( NULL, NULL, 0 ) == -1 );
2014 TEST_ASSERT( mbedtls_test_buffer_get( NULL, NULL, 0 ) == -1 );
2015
2016 /* Make sure calling put and get on a buffer that hasn't been set up results
2017 * in eror. */
2018 mbedtls_test_buffer_init( &buf );
2019
2020 TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, sizeof( input ) ) == -1 );
2021 TEST_ASSERT( mbedtls_test_buffer_get( &buf, output, sizeof( output ) )
2022 == -1 );
2023 TEST_ASSERT( mbedtls_test_buffer_put( &buf, NULL, sizeof( input ) ) == -1 );
Andrzej Kurekf7774142020-01-22 06:34:59 -05002024
Janos Follath6264e662019-11-26 11:11:15 +00002025 TEST_ASSERT( mbedtls_test_buffer_put( &buf, NULL, 0 ) == -1 );
2026 TEST_ASSERT( mbedtls_test_buffer_get( &buf, NULL, 0 ) == -1 );
2027
Andrzej Kurekf7774142020-01-22 06:34:59 -05002028 /* Make sure calling put and get on NULL input only results in
2029 * error if the length is not zero, and that a NULL output is valid for data
2030 * dropping.
2031 */
Janos Follath6264e662019-11-26 11:11:15 +00002032
2033 TEST_ASSERT( mbedtls_test_buffer_setup( &buf, sizeof( input ) ) == 0 );
2034
2035 TEST_ASSERT( mbedtls_test_buffer_put( &buf, NULL, sizeof( input ) ) == -1 );
2036 TEST_ASSERT( mbedtls_test_buffer_get( &buf, NULL, sizeof( output ) )
Andrzej Kurekf7774142020-01-22 06:34:59 -05002037 == 0 );
Janos Follath6264e662019-11-26 11:11:15 +00002038 TEST_ASSERT( mbedtls_test_buffer_put( &buf, NULL, 0 ) == 0 );
2039 TEST_ASSERT( mbedtls_test_buffer_get( &buf, NULL, 0 ) == 0 );
2040
Piotr Nowickifb437d72020-01-13 16:59:12 +01002041 /* Make sure calling put several times in the row is safe */
2042
2043 TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, sizeof( input ) )
2044 == sizeof( input ) );
2045 TEST_ASSERT( mbedtls_test_buffer_get( &buf, output, 2 ) == 2 );
2046 TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, 1 ) == 1 );
2047 TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, 2 ) == 1 );
2048 TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, 2 ) == 0 );
2049
2050
Janos Follath6264e662019-11-26 11:11:15 +00002051exit:
2052
2053 mbedtls_test_buffer_free( &buf );
2054}
2055/* END_CASE */
2056
2057/*
2058 * Test if the implementation of `mbedtls_test_buffer` related functions is
2059 * correct and works as expected.
2060 *
2061 * That is
2062 * - If we try to put in \p put1 bytes then we can put in \p put1_ret bytes.
2063 * - Afterwards if we try to get \p get1 bytes then we can get \get1_ret bytes.
2064 * - Next, if we try to put in \p put1 bytes then we can put in \p put1_ret
2065 * bytes.
2066 * - Afterwards if we try to get \p get1 bytes then we can get \get1_ret bytes.
2067 * - All of the bytes we got match the bytes we put in in a FIFO manner.
2068 */
2069
2070/* BEGIN_CASE */
2071void test_callback_buffer( int size, int put1, int put1_ret,
2072 int get1, int get1_ret, int put2, int put2_ret,
2073 int get2, int get2_ret )
2074{
2075 enum { ROUNDS = 2 };
2076 size_t put[ROUNDS];
2077 int put_ret[ROUNDS];
2078 size_t get[ROUNDS];
2079 int get_ret[ROUNDS];
2080 mbedtls_test_buffer buf;
2081 unsigned char* input = NULL;
2082 size_t input_len;
2083 unsigned char* output = NULL;
2084 size_t output_len;
Janos Follath031827f2019-11-27 11:12:14 +00002085 size_t i, j, written, read;
Janos Follath6264e662019-11-26 11:11:15 +00002086
2087 mbedtls_test_buffer_init( &buf );
2088 TEST_ASSERT( mbedtls_test_buffer_setup( &buf, size ) == 0 );
2089
2090 /* Check the sanity of input parameters and initialise local variables. That
2091 * is, ensure that the amount of data is not negative and that we are not
2092 * expecting more to put or get than we actually asked for. */
2093 TEST_ASSERT( put1 >= 0 );
2094 put[0] = put1;
2095 put_ret[0] = put1_ret;
2096 TEST_ASSERT( put1_ret <= put1 );
2097 TEST_ASSERT( put2 >= 0 );
2098 put[1] = put2;
2099 put_ret[1] = put2_ret;
2100 TEST_ASSERT( put2_ret <= put2 );
2101
2102 TEST_ASSERT( get1 >= 0 );
2103 get[0] = get1;
2104 get_ret[0] = get1_ret;
2105 TEST_ASSERT( get1_ret <= get1 );
2106 TEST_ASSERT( get2 >= 0 );
2107 get[1] = get2;
2108 get_ret[1] = get2_ret;
2109 TEST_ASSERT( get2_ret <= get2 );
2110
2111 input_len = 0;
2112 /* Calculate actual input and output lengths */
2113 for( j = 0; j < ROUNDS; j++ )
2114 {
2115 if( put_ret[j] > 0 )
2116 {
2117 input_len += put_ret[j];
2118 }
2119 }
2120 /* In order to always have a valid pointer we always allocate at least 1
2121 * byte. */
2122 if( input_len == 0 )
2123 input_len = 1;
2124 ASSERT_ALLOC( input, input_len );
2125
2126 output_len = 0;
2127 for( j = 0; j < ROUNDS; j++ )
2128 {
2129 if( get_ret[j] > 0 )
2130 {
2131 output_len += get_ret[j];
2132 }
2133 }
2134 TEST_ASSERT( output_len <= input_len );
2135 /* In order to always have a valid pointer we always allocate at least 1
2136 * byte. */
2137 if( output_len == 0 )
2138 output_len = 1;
2139 ASSERT_ALLOC( output, output_len );
2140
2141 /* Fill up the buffer with structured data so that unwanted changes
2142 * can be detected */
2143 for( i = 0; i < input_len; i++ )
2144 {
2145 input[i] = i & 0xFF;
2146 }
2147
2148 written = read = 0;
2149 for( j = 0; j < ROUNDS; j++ )
2150 {
2151 TEST_ASSERT( put_ret[j] == mbedtls_test_buffer_put( &buf,
2152 input + written, put[j] ) );
2153 written += put_ret[j];
2154 TEST_ASSERT( get_ret[j] == mbedtls_test_buffer_get( &buf,
2155 output + read, get[j] ) );
2156 read += get_ret[j];
2157 TEST_ASSERT( read <= written );
2158 if( get_ret[j] > 0 )
2159 {
2160 TEST_ASSERT( memcmp( output + read - get_ret[j],
2161 input + read - get_ret[j], get_ret[j] )
2162 == 0 );
2163 }
2164 }
2165
2166exit:
2167
2168 mbedtls_free( input );
2169 mbedtls_free( output );
2170 mbedtls_test_buffer_free( &buf );
2171}
2172/* END_CASE */
2173
Janos Follath031827f2019-11-27 11:12:14 +00002174/*
Janos Follathc673c2c2019-12-02 15:47:26 +00002175 * Test if the implementation of `mbedtls_mock_socket` related I/O functions is
2176 * correct and works as expected on unconnected sockets.
2177 */
2178
2179/* BEGIN_CASE */
2180void ssl_mock_sanity( )
2181{
2182 enum { MSGLEN = 105 };
2183 unsigned char message[MSGLEN];
2184 unsigned char received[MSGLEN];
2185 mbedtls_mock_socket socket;
2186
2187 mbedtls_mock_socket_init( &socket );
2188 TEST_ASSERT( mbedtls_mock_tcp_send_b( &socket, message, MSGLEN ) < 0 );
2189 mbedtls_mock_socket_close( &socket );
2190 mbedtls_mock_socket_init( &socket );
2191 TEST_ASSERT( mbedtls_mock_tcp_recv_b( &socket, received, MSGLEN ) < 0 );
2192 mbedtls_mock_socket_close( &socket );
2193
2194 mbedtls_mock_socket_init( &socket );
2195 TEST_ASSERT( mbedtls_mock_tcp_send_nb( &socket, message, MSGLEN ) < 0 );
2196 mbedtls_mock_socket_close( &socket );
2197 mbedtls_mock_socket_init( &socket );
2198 TEST_ASSERT( mbedtls_mock_tcp_recv_nb( &socket, received, MSGLEN ) < 0 );
2199 mbedtls_mock_socket_close( &socket );
2200
2201exit:
2202
2203 mbedtls_mock_socket_close( &socket );
2204}
2205/* END_CASE */
2206
2207/*
2208 * Test if the implementation of `mbedtls_mock_socket` related functions can
2209 * send a single message from the client to the server.
Janos Follath031827f2019-11-27 11:12:14 +00002210 */
2211
2212/* BEGIN_CASE */
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002213void ssl_mock_tcp( int blocking )
Janos Follath031827f2019-11-27 11:12:14 +00002214{
Janos Follathc673c2c2019-12-02 15:47:26 +00002215 enum { MSGLEN = 105 };
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002216 enum { BUFLEN = MSGLEN / 5 };
Janos Follathc673c2c2019-12-02 15:47:26 +00002217 unsigned char message[MSGLEN];
2218 unsigned char received[MSGLEN];
2219 mbedtls_mock_socket client;
2220 mbedtls_mock_socket server;
2221 size_t written, read;
2222 int send_ret, recv_ret;
2223 mbedtls_ssl_send_t *send;
2224 mbedtls_ssl_recv_t *recv;
Janos Follathc673c2c2019-12-02 15:47:26 +00002225 unsigned i;
2226
2227 if( blocking == 0 )
2228 {
2229 send = mbedtls_mock_tcp_send_nb;
2230 recv = mbedtls_mock_tcp_recv_nb;
2231 }
2232 else
2233 {
2234 send = mbedtls_mock_tcp_send_b;
2235 recv = mbedtls_mock_tcp_recv_b;
2236 }
2237
2238 mbedtls_mock_socket_init( &client );
2239 mbedtls_mock_socket_init( &server );
2240
2241 /* Fill up the buffer with structured data so that unwanted changes
2242 * can be detected */
2243 for( i = 0; i < MSGLEN; i++ )
2244 {
2245 message[i] = i & 0xFF;
2246 }
2247
2248 /* Make sure that sending a message takes a few iterations. */
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002249 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, BUFLEN ) );
Janos Follathc673c2c2019-12-02 15:47:26 +00002250
2251 /* Send the message to the server */
2252 send_ret = recv_ret = 1;
2253 written = read = 0;
2254 while( send_ret != 0 || recv_ret != 0 )
2255 {
2256 send_ret = send( &client, message + written, MSGLEN - written );
2257
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002258 TEST_ASSERT( send_ret >= 0 );
2259 TEST_ASSERT( send_ret <= BUFLEN );
2260 written += send_ret;
2261
2262 /* If the buffer is full we can test blocking and non-blocking send */
2263 if ( send_ret == BUFLEN )
Janos Follathc673c2c2019-12-02 15:47:26 +00002264 {
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002265 int blocking_ret = send( &client, message , 1 );
2266 if ( blocking )
2267 {
2268 TEST_ASSERT( blocking_ret == 0 );
2269 }
2270 else
2271 {
2272 TEST_ASSERT( blocking_ret == MBEDTLS_ERR_SSL_WANT_WRITE );
2273 }
Janos Follathc673c2c2019-12-02 15:47:26 +00002274 }
Janos Follathc673c2c2019-12-02 15:47:26 +00002275
2276 recv_ret = recv( &server, received + read, MSGLEN - read );
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002277
2278 /* The result depends on whether any data was sent */
2279 if ( send_ret > 0 )
Janos Follathc673c2c2019-12-02 15:47:26 +00002280 {
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002281 TEST_ASSERT( recv_ret > 0 );
2282 TEST_ASSERT( recv_ret <= BUFLEN );
2283 read += recv_ret;
2284 }
2285 else if( blocking )
2286 {
2287 TEST_ASSERT( recv_ret == 0 );
Janos Follathc673c2c2019-12-02 15:47:26 +00002288 }
2289 else
2290 {
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002291 TEST_ASSERT( recv_ret == MBEDTLS_ERR_SSL_WANT_READ );
2292 recv_ret = 0;
Janos Follathc673c2c2019-12-02 15:47:26 +00002293 }
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002294
2295 /* If the buffer is empty we can test blocking and non-blocking read */
2296 if ( recv_ret == BUFLEN )
2297 {
2298 int blocking_ret = recv( &server, received, 1 );
2299 if ( blocking )
2300 {
2301 TEST_ASSERT( blocking_ret == 0 );
2302 }
2303 else
2304 {
2305 TEST_ASSERT( blocking_ret == MBEDTLS_ERR_SSL_WANT_READ );
2306 }
2307 }
Janos Follathc673c2c2019-12-02 15:47:26 +00002308 }
2309 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
2310
2311exit:
2312
2313 mbedtls_mock_socket_close( &client );
2314 mbedtls_mock_socket_close( &server );
2315}
2316/* END_CASE */
2317
2318/*
2319 * Test if the implementation of `mbedtls_mock_socket` related functions can
2320 * send messages in both direction at the same time (with the I/O calls
2321 * interleaving).
2322 */
2323
2324/* BEGIN_CASE */
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002325void ssl_mock_tcp_interleaving( int blocking )
Janos Follathc673c2c2019-12-02 15:47:26 +00002326{
Janos Follath031827f2019-11-27 11:12:14 +00002327 enum { ROUNDS = 2 };
2328 enum { MSGLEN = 105 };
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002329 enum { BUFLEN = MSGLEN / 5 };
Janos Follath031827f2019-11-27 11:12:14 +00002330 unsigned char message[ROUNDS][MSGLEN];
2331 unsigned char received[ROUNDS][MSGLEN];
2332 mbedtls_mock_socket client;
2333 mbedtls_mock_socket server;
2334 size_t written[ROUNDS];
2335 size_t read[ROUNDS];
2336 int send_ret[ROUNDS];
2337 int recv_ret[ROUNDS];
2338 unsigned i, j, progress;
Janos Follath3766ba52019-11-27 13:31:42 +00002339 mbedtls_ssl_send_t *send;
2340 mbedtls_ssl_recv_t *recv;
Janos Follath3766ba52019-11-27 13:31:42 +00002341
2342 if( blocking == 0 )
2343 {
2344 send = mbedtls_mock_tcp_send_nb;
2345 recv = mbedtls_mock_tcp_recv_nb;
2346 }
2347 else
2348 {
2349 send = mbedtls_mock_tcp_send_b;
2350 recv = mbedtls_mock_tcp_recv_b;
2351 }
Janos Follath031827f2019-11-27 11:12:14 +00002352
2353 mbedtls_mock_socket_init( &client );
2354 mbedtls_mock_socket_init( &server );
2355
2356 /* Fill up the buffers with structured data so that unwanted changes
2357 * can be detected */
2358 for( i = 0; i < ROUNDS; i++ )
2359 {
2360 for( j = 0; j < MSGLEN; j++ )
2361 {
2362 message[i][j] = ( i * MSGLEN + j ) & 0xFF;
2363 }
2364 }
2365
Janos Follath031827f2019-11-27 11:12:14 +00002366 /* Make sure that sending a message takes a few iterations. */
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002367 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, BUFLEN ) );
Janos Follath031827f2019-11-27 11:12:14 +00002368
Janos Follath031827f2019-11-27 11:12:14 +00002369 /* Send the message from both sides, interleaving. */
2370 progress = 1;
2371 for( i = 0; i < ROUNDS; i++ )
2372 {
2373 written[i] = 0;
2374 read[i] = 0;
2375 }
2376 /* This loop does not stop as long as there was a successful write or read
2377 * of at least one byte on either side. */
2378 while( progress != 0 )
2379 {
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002380 mbedtls_mock_socket *socket;
Janos Follath031827f2019-11-27 11:12:14 +00002381
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002382 for( i = 0; i < ROUNDS; i++ )
Janos Follath3766ba52019-11-27 13:31:42 +00002383 {
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002384 /* First sending is from the client */
2385 socket = ( i % 2 == 0 ) ? ( &client ) : ( &server );
Janos Follath031827f2019-11-27 11:12:14 +00002386
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002387 send_ret[i] = send( socket, message[i] + written[i],
2388 MSGLEN - written[i] );
2389 TEST_ASSERT( send_ret[i] >= 0 );
2390 TEST_ASSERT( send_ret[i] <= BUFLEN );
2391 written[i] += send_ret[i];
Janos Follath031827f2019-11-27 11:12:14 +00002392
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002393 /* If the buffer is full we can test blocking and non-blocking
2394 * send */
2395 if ( send_ret[i] == BUFLEN )
2396 {
2397 int blocking_ret = send( socket, message[i] , 1 );
2398 if ( blocking )
2399 {
2400 TEST_ASSERT( blocking_ret == 0 );
2401 }
2402 else
2403 {
2404 TEST_ASSERT( blocking_ret == MBEDTLS_ERR_SSL_WANT_WRITE );
2405 }
2406 }
Janos Follath3766ba52019-11-27 13:31:42 +00002407 }
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002408
2409 for( i = 0; i < ROUNDS; i++ )
Janos Follath3766ba52019-11-27 13:31:42 +00002410 {
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002411 /* First receiving is from the server */
2412 socket = ( i % 2 == 0 ) ? ( &server ) : ( &client );
2413
2414 recv_ret[i] = recv( socket, received[i] + read[i],
2415 MSGLEN - read[i] );
2416
2417 /* The result depends on whether any data was sent */
2418 if ( send_ret[i] > 0 )
2419 {
2420 TEST_ASSERT( recv_ret[i] > 0 );
2421 TEST_ASSERT( recv_ret[i] <= BUFLEN );
2422 read[i] += recv_ret[i];
2423 }
2424 else if( blocking )
2425 {
2426 TEST_ASSERT( recv_ret[i] == 0 );
2427 }
2428 else
2429 {
2430 TEST_ASSERT( recv_ret[i] == MBEDTLS_ERR_SSL_WANT_READ );
2431 recv_ret[i] = 0;
2432 }
2433
2434 /* If the buffer is empty we can test blocking and non-blocking
2435 * read */
2436 if ( recv_ret[i] == BUFLEN )
2437 {
2438 int blocking_ret = recv( socket, received[i], 1 );
2439 if ( blocking )
2440 {
2441 TEST_ASSERT( blocking_ret == 0 );
2442 }
2443 else
2444 {
2445 TEST_ASSERT( blocking_ret == MBEDTLS_ERR_SSL_WANT_READ );
2446 }
2447 }
Janos Follath3766ba52019-11-27 13:31:42 +00002448 }
Janos Follath031827f2019-11-27 11:12:14 +00002449
2450 progress = 0;
2451 for( i = 0; i < ROUNDS; i++ )
2452 {
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002453 progress += send_ret[i] + recv_ret[i];
Janos Follath031827f2019-11-27 11:12:14 +00002454 }
2455 }
2456
2457 for( i = 0; i < ROUNDS; i++ )
2458 TEST_ASSERT( memcmp( message[i], received[i], MSGLEN ) == 0 );
2459
2460exit:
2461
2462 mbedtls_mock_socket_close( &client );
2463 mbedtls_mock_socket_close( &server );
2464}
2465/* END_CASE */
2466
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002467/* BEGIN_CASE */
2468void ssl_message_queue_sanity( )
2469{
2470 mbedtls_test_message_queue queue;
2471
2472 /* Trying to push/pull to an empty queue */
2473 TEST_ASSERT( mbedtls_test_message_queue_push_info( NULL, 1 )
2474 == MBEDTLS_TEST_ERROR_ARG_NULL );
2475 TEST_ASSERT( mbedtls_test_message_queue_pop_info( NULL, 1 )
2476 == MBEDTLS_TEST_ERROR_ARG_NULL );
2477
Andrzej Kurek89bdc582020-03-09 06:29:43 -04002478 TEST_ASSERT( mbedtls_test_message_queue_setup( &queue, 3 ) == 0 );
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002479 TEST_ASSERT( queue.capacity == 3 );
2480 TEST_ASSERT( queue.num == 0 );
2481
2482exit:
2483 mbedtls_test_message_queue_free( &queue );
2484}
2485/* END_CASE */
2486
2487/* BEGIN_CASE */
2488void ssl_message_queue_basic( )
2489{
2490 mbedtls_test_message_queue queue;
2491
Andrzej Kurek89bdc582020-03-09 06:29:43 -04002492 TEST_ASSERT( mbedtls_test_message_queue_setup( &queue, 3 ) == 0 );
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002493
2494 /* Sanity test - 3 pushes and 3 pops with sufficient space */
2495 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 );
2496 TEST_ASSERT( queue.capacity == 3 );
2497 TEST_ASSERT( queue.num == 1 );
2498 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 );
2499 TEST_ASSERT( queue.capacity == 3 );
2500 TEST_ASSERT( queue.num == 2 );
2501 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 2 ) == 2 );
2502 TEST_ASSERT( queue.capacity == 3 );
2503 TEST_ASSERT( queue.num == 3 );
2504
2505 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 );
2506 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 );
2507 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 2 ) == 2 );
2508
2509exit:
2510 mbedtls_test_message_queue_free( &queue );
2511}
2512/* END_CASE */
2513
2514/* BEGIN_CASE */
2515void ssl_message_queue_overflow_underflow( )
2516{
2517 mbedtls_test_message_queue queue;
2518
Andrzej Kurek89bdc582020-03-09 06:29:43 -04002519 TEST_ASSERT( mbedtls_test_message_queue_setup( &queue, 3 ) == 0 );
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002520
2521 /* 4 pushes (last one with an error), 4 pops (last one with an error) */
2522 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 );
2523 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 );
2524 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 2 ) == 2 );
2525 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 3 )
Andrzej Kurekf46b9122020-02-07 08:19:00 -05002526 == MBEDTLS_ERR_SSL_WANT_WRITE );
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002527
2528 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 );
2529 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 );
2530 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 2 ) == 2 );
2531
2532 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 )
Andrzej Kurekf46b9122020-02-07 08:19:00 -05002533 == MBEDTLS_ERR_SSL_WANT_READ );
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002534
2535exit:
2536 mbedtls_test_message_queue_free( &queue );
2537}
2538/* END_CASE */
2539
2540/* BEGIN_CASE */
2541void ssl_message_queue_interleaved( )
2542{
2543 mbedtls_test_message_queue queue;
2544
Andrzej Kurek89bdc582020-03-09 06:29:43 -04002545 TEST_ASSERT( mbedtls_test_message_queue_setup( &queue, 3 ) == 0 );
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002546
2547 /* Interleaved test - [2 pushes, 1 pop] twice, and then two pops
2548 * (to wrap around the buffer) */
2549 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 );
2550 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 );
2551
2552 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 );
2553
2554 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 2 ) == 2 );
2555 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 3 ) == 3 );
2556
2557 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 );
2558 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 2 ) == 2 );
2559
2560 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 5 ) == 5 );
2561 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 8 ) == 8 );
2562
2563 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 3 ) == 3 );
2564
2565 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 5 ) == 5 );
2566
2567 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 8 ) == 8 );
2568
2569exit:
2570 mbedtls_test_message_queue_free( &queue );
2571}
2572/* END_CASE */
2573
2574/* BEGIN_CASE */
2575void ssl_message_queue_insufficient_buffer( )
2576{
2577 mbedtls_test_message_queue queue;
2578 size_t message_len = 10;
2579 size_t buffer_len = 5;
2580
Andrzej Kurek89bdc582020-03-09 06:29:43 -04002581 TEST_ASSERT( mbedtls_test_message_queue_setup( &queue, 1 ) == 0 );
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002582
2583 /* Popping without a sufficient buffer */
2584 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, message_len )
2585 == (int) message_len );
2586 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, buffer_len )
2587 == (int) buffer_len );
2588exit:
2589 mbedtls_test_message_queue_free( &queue );
2590}
2591/* END_CASE */
2592
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002593/* BEGIN_CASE */
2594void ssl_message_mock_uninitialized( )
2595{
2596 enum { MSGLEN = 10 };
2597 unsigned char message[MSGLEN], received[MSGLEN];
2598 mbedtls_mock_socket client, server;
2599 mbedtls_test_message_queue server_queue, client_queue;
2600 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05002601 mbedtls_message_socket_init( &server_context );
2602 mbedtls_message_socket_init( &client_context );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002603
2604 /* Send with a NULL context */
2605 TEST_ASSERT( mbedtls_mock_tcp_send_msg( NULL, message, MSGLEN )
2606 == MBEDTLS_TEST_ERROR_CONTEXT_ERROR );
2607
2608 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( NULL, message, MSGLEN )
2609 == MBEDTLS_TEST_ERROR_CONTEXT_ERROR );
2610
2611 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 1,
2612 &server,
2613 &server_context ) == 0 );
2614
2615 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 1,
2616 &client,
2617 &client_context ) == 0 );
2618
2619 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, MSGLEN )
2620 == MBEDTLS_TEST_ERROR_SEND_FAILED );
2621
2622 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
Andrzej Kurekf46b9122020-02-07 08:19:00 -05002623 == MBEDTLS_ERR_SSL_WANT_READ );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002624
2625 /* Push directly to a queue to later simulate a disconnected behavior */
2626 TEST_ASSERT( mbedtls_test_message_queue_push_info( &server_queue, MSGLEN )
2627 == MSGLEN );
2628
2629 /* Test if there's an error when trying to read from a disconnected
2630 * socket */
2631 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
2632 == MBEDTLS_TEST_ERROR_RECV_FAILED );
2633 exit:
2634 mbedtls_message_socket_close( &server_context );
2635 mbedtls_message_socket_close( &client_context );
2636}
2637/* END_CASE */
2638
2639/* BEGIN_CASE */
2640void ssl_message_mock_basic( )
2641{
2642 enum { MSGLEN = 10 };
2643 unsigned char message[MSGLEN], received[MSGLEN];
2644 mbedtls_mock_socket client, server;
2645 unsigned i;
2646 mbedtls_test_message_queue server_queue, client_queue;
2647 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05002648 mbedtls_message_socket_init( &server_context );
2649 mbedtls_message_socket_init( &client_context );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002650
2651 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 1,
2652 &server,
2653 &server_context ) == 0 );
2654
2655 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 1,
2656 &client,
2657 &client_context ) == 0 );
2658
2659 /* Fill up the buffer with structured data so that unwanted changes
2660 * can be detected */
2661 for( i = 0; i < MSGLEN; i++ )
2662 {
2663 message[i] = i & 0xFF;
2664 }
2665 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
2666 MSGLEN ) );
2667
2668 /* Send the message to the server */
2669 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2670 MSGLEN ) == MSGLEN );
2671
2672 /* Read from the server */
2673 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
2674 == MSGLEN );
2675
2676 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
2677 memset( received, 0, MSGLEN );
2678
2679 /* Send the message to the client */
2680 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &server_context, message,
2681 MSGLEN ) == MSGLEN );
2682
2683 /* Read from the client */
2684 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &client_context, received, MSGLEN )
2685 == MSGLEN );
2686 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
2687
2688 exit:
2689 mbedtls_message_socket_close( &server_context );
2690 mbedtls_message_socket_close( &client_context );
2691}
2692/* END_CASE */
2693
2694/* BEGIN_CASE */
2695void ssl_message_mock_queue_overflow_underflow( )
2696{
2697 enum { MSGLEN = 10 };
2698 unsigned char message[MSGLEN], received[MSGLEN];
2699 mbedtls_mock_socket client, server;
2700 unsigned i;
2701 mbedtls_test_message_queue server_queue, client_queue;
2702 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05002703 mbedtls_message_socket_init( &server_context );
2704 mbedtls_message_socket_init( &client_context );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002705
2706 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 2,
2707 &server,
2708 &server_context ) == 0 );
2709
2710 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 2,
2711 &client,
2712 &client_context ) == 0 );
2713
2714 /* Fill up the buffer with structured data so that unwanted changes
2715 * can be detected */
2716 for( i = 0; i < MSGLEN; i++ )
2717 {
2718 message[i] = i & 0xFF;
2719 }
2720 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
2721 MSGLEN*2 ) );
2722
2723 /* Send three message to the server, last one with an error */
2724 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2725 MSGLEN - 1 ) == MSGLEN - 1 );
2726
2727 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2728 MSGLEN ) == MSGLEN );
2729
2730 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2731 MSGLEN )
Andrzej Kurekf46b9122020-02-07 08:19:00 -05002732 == MBEDTLS_ERR_SSL_WANT_WRITE );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002733
2734 /* Read three messages from the server, last one with an error */
2735 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received,
2736 MSGLEN - 1 ) == MSGLEN - 1 );
2737
2738 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
2739 == MSGLEN );
2740
2741 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
2742
2743 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
Andrzej Kurekf46b9122020-02-07 08:19:00 -05002744 == MBEDTLS_ERR_SSL_WANT_READ );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002745
2746 exit:
2747 mbedtls_message_socket_close( &server_context );
2748 mbedtls_message_socket_close( &client_context );
2749}
2750/* END_CASE */
2751
2752/* BEGIN_CASE */
2753void ssl_message_mock_socket_overflow( )
2754{
2755 enum { MSGLEN = 10 };
2756 unsigned char message[MSGLEN], received[MSGLEN];
2757 mbedtls_mock_socket client, server;
2758 unsigned i;
2759 mbedtls_test_message_queue server_queue, client_queue;
2760 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05002761 mbedtls_message_socket_init( &server_context );
2762 mbedtls_message_socket_init( &client_context );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002763
2764 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 2,
2765 &server,
2766 &server_context ) == 0 );
2767
2768 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 2,
2769 &client,
2770 &client_context ) == 0 );
2771
2772 /* Fill up the buffer with structured data so that unwanted changes
2773 * can be detected */
2774 for( i = 0; i < MSGLEN; i++ )
2775 {
2776 message[i] = i & 0xFF;
2777 }
2778 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
2779 MSGLEN ) );
2780
2781 /* Send two message to the server, second one with an error */
2782 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2783 MSGLEN ) == MSGLEN );
2784
2785 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2786 MSGLEN )
2787 == MBEDTLS_TEST_ERROR_SEND_FAILED );
2788
2789 /* Read the only message from the server */
2790 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
2791 == MSGLEN );
2792
2793 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
2794
2795 exit:
2796 mbedtls_message_socket_close( &server_context );
2797 mbedtls_message_socket_close( &client_context );
2798}
2799/* END_CASE */
2800
2801/* BEGIN_CASE */
2802void ssl_message_mock_truncated( )
2803{
2804 enum { MSGLEN = 10 };
2805 unsigned char message[MSGLEN], received[MSGLEN];
2806 mbedtls_mock_socket client, server;
2807 unsigned i;
2808 mbedtls_test_message_queue server_queue, client_queue;
2809 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05002810 mbedtls_message_socket_init( &server_context );
2811 mbedtls_message_socket_init( &client_context );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002812
2813 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 2,
2814 &server,
2815 &server_context ) == 0 );
2816
2817 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 2,
2818 &client,
2819 &client_context ) == 0 );
2820
2821 memset( received, 0, MSGLEN );
2822 /* Fill up the buffer with structured data so that unwanted changes
2823 * can be detected */
2824 for( i = 0; i < MSGLEN; i++ )
2825 {
2826 message[i] = i & 0xFF;
2827 }
2828 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
2829 2 * MSGLEN ) );
2830
2831 /* Send two messages to the server, the second one small enough to fit in the
2832 * receiver's buffer. */
2833 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2834 MSGLEN ) == MSGLEN );
2835 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2836 MSGLEN / 2 ) == MSGLEN / 2 );
2837 /* Read a truncated message from the server */
2838 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN/2 )
2839 == MSGLEN/2 );
2840
2841 /* Test that the first half of the message is valid, and second one isn't */
2842 TEST_ASSERT( memcmp( message, received, MSGLEN/2 ) == 0 );
2843 TEST_ASSERT( memcmp( message + MSGLEN/2, received + MSGLEN/2, MSGLEN/2 )
2844 != 0 );
2845 memset( received, 0, MSGLEN );
2846
2847 /* Read a full message from the server */
2848 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN/2 )
2849 == MSGLEN / 2 );
2850
2851 /* Test that the first half of the message is valid */
2852 TEST_ASSERT( memcmp( message, received, MSGLEN/2 ) == 0 );
2853
2854 exit:
2855 mbedtls_message_socket_close( &server_context );
2856 mbedtls_message_socket_close( &client_context );
2857}
2858/* END_CASE */
2859
2860/* BEGIN_CASE */
2861void ssl_message_mock_socket_read_error( )
2862{
2863 enum { MSGLEN = 10 };
2864 unsigned char message[MSGLEN], received[MSGLEN];
2865 mbedtls_mock_socket client, server;
2866 unsigned i;
2867 mbedtls_test_message_queue server_queue, client_queue;
2868 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05002869 mbedtls_message_socket_init( &server_context );
2870 mbedtls_message_socket_init( &client_context );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002871
2872 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 1,
2873 &server,
2874 &server_context ) == 0 );
2875
2876 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 1,
2877 &client,
2878 &client_context ) == 0 );
2879
2880 /* Fill up the buffer with structured data so that unwanted changes
2881 * can be detected */
2882 for( i = 0; i < MSGLEN; i++ )
2883 {
2884 message[i] = i & 0xFF;
2885 }
2886 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
2887 MSGLEN ) );
2888
2889 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2890 MSGLEN ) == MSGLEN );
2891
2892 /* Force a read error by disconnecting the socket by hand */
2893 server.status = 0;
2894 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
2895 == MBEDTLS_TEST_ERROR_RECV_FAILED );
2896 /* Return to a valid state */
2897 server.status = MBEDTLS_MOCK_SOCKET_CONNECTED;
2898
2899 memset( received, 0, sizeof( received ) );
2900
2901 /* Test that even though the server tried to read once disconnected, the
2902 * continuity is preserved */
2903 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
2904 == MSGLEN );
2905
2906 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
2907
2908 exit:
2909 mbedtls_message_socket_close( &server_context );
2910 mbedtls_message_socket_close( &client_context );
2911}
2912/* END_CASE */
2913
2914/* BEGIN_CASE */
2915void ssl_message_mock_interleaved_one_way( )
2916{
2917 enum { MSGLEN = 10 };
2918 unsigned char message[MSGLEN], received[MSGLEN];
2919 mbedtls_mock_socket client, server;
2920 unsigned i;
2921 mbedtls_test_message_queue server_queue, client_queue;
2922 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05002923 mbedtls_message_socket_init( &server_context );
2924 mbedtls_message_socket_init( &client_context );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002925
2926 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 3,
2927 &server,
2928 &server_context ) == 0 );
2929
2930 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 3,
2931 &client,
2932 &client_context ) == 0 );
2933
2934 /* Fill up the buffer with structured data so that unwanted changes
2935 * can be detected */
2936 for( i = 0; i < MSGLEN; i++ )
2937 {
2938 message[i] = i & 0xFF;
2939 }
2940 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
2941 MSGLEN*3 ) );
2942
2943 /* Interleaved test - [2 sends, 1 read] twice, and then two reads
2944 * (to wrap around the buffer) */
2945 for( i = 0; i < 2; i++ )
2946 {
2947 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2948 MSGLEN ) == MSGLEN );
2949
2950 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2951 MSGLEN ) == MSGLEN );
2952
2953 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received,
2954 MSGLEN ) == MSGLEN );
2955 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
2956 memset( received, 0, sizeof( received ) );
2957 }
2958
2959 for( i = 0; i < 2; i++ )
2960 {
2961 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received,
2962 MSGLEN ) == MSGLEN );
2963
2964 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
2965 }
2966 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
Andrzej Kurekf46b9122020-02-07 08:19:00 -05002967 == MBEDTLS_ERR_SSL_WANT_READ );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002968 exit:
2969 mbedtls_message_socket_close( &server_context );
2970 mbedtls_message_socket_close( &client_context );
2971}
2972/* END_CASE */
2973
2974/* BEGIN_CASE */
2975void ssl_message_mock_interleaved_two_ways( )
2976{
2977 enum { MSGLEN = 10 };
2978 unsigned char message[MSGLEN], received[MSGLEN];
2979 mbedtls_mock_socket client, server;
2980 unsigned i;
2981 mbedtls_test_message_queue server_queue, client_queue;
2982 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05002983 mbedtls_message_socket_init( &server_context );
2984 mbedtls_message_socket_init( &client_context );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002985
2986 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 3,
2987 &server,
2988 &server_context ) == 0 );
2989
2990 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 3,
2991 &client,
2992 &client_context ) == 0 );
2993
2994 /* Fill up the buffer with structured data so that unwanted changes
2995 * can be detected */
2996 for( i = 0; i < MSGLEN; i++ )
2997 {
2998 message[i] = i & 0xFF;
2999 }
3000 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
3001 MSGLEN*3 ) );
3002
3003 /* Interleaved test - [2 sends, 1 read] twice, both ways, and then two reads
3004 * (to wrap around the buffer) both ways. */
3005 for( i = 0; i < 2; i++ )
3006 {
3007 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
3008 MSGLEN ) == MSGLEN );
3009
3010 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
3011 MSGLEN ) == MSGLEN );
3012
3013 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &server_context, message,
3014 MSGLEN ) == MSGLEN );
3015
3016 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &server_context, message,
3017 MSGLEN ) == MSGLEN );
3018
3019 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received,
3020 MSGLEN ) == MSGLEN );
3021
3022 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
3023
3024 memset( received, 0, sizeof( received ) );
3025
3026 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &client_context, received,
3027 MSGLEN ) == MSGLEN );
3028
3029 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
3030
3031 memset( received, 0, sizeof( received ) );
3032 }
3033
3034 for( i = 0; i < 2; i++ )
3035 {
3036 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received,
3037 MSGLEN ) == MSGLEN );
3038
3039 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
3040 memset( received, 0, sizeof( received ) );
3041
3042 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &client_context, received,
3043 MSGLEN ) == MSGLEN );
3044
3045 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
3046 memset( received, 0, sizeof( received ) );
3047 }
3048
3049 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
Andrzej Kurekf46b9122020-02-07 08:19:00 -05003050 == MBEDTLS_ERR_SSL_WANT_READ );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003051
3052 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &client_context, received, MSGLEN )
Andrzej Kurekf46b9122020-02-07 08:19:00 -05003053 == MBEDTLS_ERR_SSL_WANT_READ );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003054 exit:
3055 mbedtls_message_socket_close( &server_context );
3056 mbedtls_message_socket_close( &client_context );
3057}
3058/* END_CASE */
3059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003060/* BEGIN_CASE depends_on:MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Azim Khan5fcca462018-06-29 11:05:32 +01003061void ssl_dtls_replay( data_t * prevs, data_t * new, int ret )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003062{
Azim Khand30ca132017-06-09 04:32:58 +01003063 uint32_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003064 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02003065 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003066
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02003067 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02003068 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02003069
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02003070 TEST_ASSERT( mbedtls_ssl_config_defaults( &conf,
3071 MBEDTLS_SSL_IS_CLIENT,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02003072 MBEDTLS_SSL_TRANSPORT_DATAGRAM,
3073 MBEDTLS_SSL_PRESET_DEFAULT ) == 0 );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02003074 TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003075
3076 /* Read previous record numbers */
Azim Khand30ca132017-06-09 04:32:58 +01003077 for( len = 0; len < prevs->len; len += 6 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003078 {
Azim Khand30ca132017-06-09 04:32:58 +01003079 memcpy( ssl.in_ctr + 2, prevs->x + len, 6 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003080 mbedtls_ssl_dtls_replay_update( &ssl );
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003081 }
3082
3083 /* Check new number */
Azim Khand30ca132017-06-09 04:32:58 +01003084 memcpy( ssl.in_ctr + 2, new->x, 6 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003085 TEST_ASSERT( mbedtls_ssl_dtls_replay_check( &ssl ) == ret );
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003087 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02003088 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003089}
3090/* END_CASE */
Hanno Beckerb25c0c72017-05-05 11:24:30 +01003091
3092/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
3093void ssl_set_hostname_twice( char *hostname0, char *hostname1 )
3094{
3095 mbedtls_ssl_context ssl;
3096 mbedtls_ssl_init( &ssl );
3097
3098 TEST_ASSERT( mbedtls_ssl_set_hostname( &ssl, hostname0 ) == 0 );
3099 TEST_ASSERT( mbedtls_ssl_set_hostname( &ssl, hostname1 ) == 0 );
3100
3101 mbedtls_ssl_free( &ssl );
3102}
Darryl Green11999bb2018-03-13 15:22:58 +00003103/* END_CASE */
Hanno Beckera18d1322018-01-03 14:27:32 +00003104
3105/* BEGIN_CASE */
3106void ssl_crypt_record( int cipher_type, int hash_id,
Hanno Beckerd856c822019-04-29 17:30:59 +01003107 int etm, int tag_mode, int ver,
3108 int cid0_len, int cid1_len )
Hanno Beckera18d1322018-01-03 14:27:32 +00003109{
3110 /*
3111 * Test several record encryptions and decryptions
3112 * with plenty of space before and after the data
3113 * within the record buffer.
3114 */
3115
3116 int ret;
3117 int num_records = 16;
3118 mbedtls_ssl_context ssl; /* ONLY for debugging */
3119
3120 mbedtls_ssl_transform t0, t1;
Hanno Becker81e16a32019-03-01 11:21:44 +00003121 unsigned char *buf = NULL;
Hanno Beckera18d1322018-01-03 14:27:32 +00003122 size_t const buflen = 512;
3123 mbedtls_record rec, rec_backup;
3124
3125 mbedtls_ssl_init( &ssl );
3126 mbedtls_ssl_transform_init( &t0 );
3127 mbedtls_ssl_transform_init( &t1 );
3128 TEST_ASSERT( build_transforms( &t0, &t1, cipher_type, hash_id,
Hanno Beckerd856c822019-04-29 17:30:59 +01003129 etm, tag_mode, ver,
3130 (size_t) cid0_len,
3131 (size_t) cid1_len ) == 0 );
Hanno Beckera18d1322018-01-03 14:27:32 +00003132
Hanno Becker3ee54212019-04-04 16:31:26 +01003133 TEST_ASSERT( ( buf = mbedtls_calloc( 1, buflen ) ) != NULL );
Hanno Beckera18d1322018-01-03 14:27:32 +00003134
3135 while( num_records-- > 0 )
3136 {
3137 mbedtls_ssl_transform *t_dec, *t_enc;
3138 /* Take turns in who's sending and who's receiving. */
3139 if( num_records % 3 == 0 )
3140 {
3141 t_dec = &t0;
3142 t_enc = &t1;
3143 }
3144 else
3145 {
3146 t_dec = &t1;
3147 t_enc = &t0;
3148 }
3149
3150 /*
3151 * The record header affects the transformation in two ways:
3152 * 1) It determines the AEAD additional data
3153 * 2) The record counter sometimes determines the IV.
3154 *
3155 * Apart from that, the fields don't have influence.
3156 * In particular, it is currently not the responsibility
3157 * of ssl_encrypt/decrypt_buf to check if the transform
3158 * version matches the record version, or that the
3159 * type is sensible.
3160 */
3161
3162 memset( rec.ctr, num_records, sizeof( rec.ctr ) );
3163 rec.type = 42;
3164 rec.ver[0] = num_records;
3165 rec.ver[1] = num_records;
Hanno Beckera0e20d02019-05-15 14:03:01 +01003166#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd856c822019-04-29 17:30:59 +01003167 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01003168#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera18d1322018-01-03 14:27:32 +00003169
3170 rec.buf = buf;
3171 rec.buf_len = buflen;
3172 rec.data_offset = 16;
3173 /* Make sure to vary the length to exercise different
3174 * paddings. */
3175 rec.data_len = 1 + num_records;
3176
3177 memset( rec.buf + rec.data_offset, 42, rec.data_len );
3178
3179 /* Make a copy for later comparison */
3180 rec_backup = rec;
3181
3182 /* Encrypt record */
3183 ret = mbedtls_ssl_encrypt_buf( &ssl, t_enc, &rec,
3184 rnd_std_rand, NULL );
3185 TEST_ASSERT( ret == 0 || ret == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3186 if( ret != 0 )
3187 {
3188 continue;
3189 }
3190
Hanno Beckerb2713ab2020-05-07 14:54:22 +01003191#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3192 if( rec.cid_len != 0 )
3193 {
3194 /* DTLS 1.2 + CID hides the real content type and
3195 * uses a special CID content type in the protected
3196 * record. Double-check this. */
3197 TEST_ASSERT( rec.type == MBEDTLS_SSL_MSG_CID );
3198 }
3199#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3200
3201#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
3202 if( t_enc->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
3203 {
3204 /* TLS 1.3 hides the real content type and
3205 * always uses Application Data as the content type
3206 * for protected records. Double-check this. */
3207 TEST_ASSERT( rec.type == MBEDTLS_SSL_MSG_APPLICATION_DATA );
3208 }
3209#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
3210
Hanno Beckera18d1322018-01-03 14:27:32 +00003211 /* Decrypt record with t_dec */
Hanno Beckerd856c822019-04-29 17:30:59 +01003212 ret = mbedtls_ssl_decrypt_buf( &ssl, t_dec, &rec );
3213 TEST_ASSERT( ret == 0 );
Hanno Beckera18d1322018-01-03 14:27:32 +00003214
3215 /* Compare results */
3216 TEST_ASSERT( rec.type == rec_backup.type );
3217 TEST_ASSERT( memcmp( rec.ctr, rec_backup.ctr, 8 ) == 0 );
3218 TEST_ASSERT( rec.ver[0] == rec_backup.ver[0] );
3219 TEST_ASSERT( rec.ver[1] == rec_backup.ver[1] );
3220 TEST_ASSERT( rec.data_len == rec_backup.data_len );
3221 TEST_ASSERT( rec.data_offset == rec_backup.data_offset );
3222 TEST_ASSERT( memcmp( rec.buf + rec.data_offset,
3223 rec_backup.buf + rec_backup.data_offset,
3224 rec.data_len ) == 0 );
3225 }
3226
Hanno Becker81e16a32019-03-01 11:21:44 +00003227exit:
3228
Hanno Beckera18d1322018-01-03 14:27:32 +00003229 /* Cleanup */
3230 mbedtls_ssl_free( &ssl );
3231 mbedtls_ssl_transform_free( &t0 );
3232 mbedtls_ssl_transform_free( &t1 );
3233
Hanno Becker3ee54212019-04-04 16:31:26 +01003234 mbedtls_free( buf );
Hanno Beckera18d1322018-01-03 14:27:32 +00003235}
3236/* END_CASE */
Hanno Beckerb3268da2018-01-05 15:20:24 +00003237
3238/* BEGIN_CASE */
3239void ssl_crypt_record_small( int cipher_type, int hash_id,
Hanno Beckerd856c822019-04-29 17:30:59 +01003240 int etm, int tag_mode, int ver,
3241 int cid0_len, int cid1_len )
Hanno Beckerb3268da2018-01-05 15:20:24 +00003242{
3243 /*
3244 * Test pairs of encryption and decryption with an increasing
3245 * amount of space in the record buffer - in more detail:
3246 * 1) Try to encrypt with 0, 1, 2, ... bytes available
3247 * in front of the plaintext, and expect the encryption
3248 * to succeed starting from some offset. Always keep
3249 * enough space in the end of the buffer.
3250 * 2) Try to encrypt with 0, 1, 2, ... bytes available
3251 * at the end of the plaintext, and expect the encryption
3252 * to succeed starting from some offset. Always keep
3253 * enough space at the beginning of the buffer.
3254 * 3) Try to encrypt with 0, 1, 2, ... bytes available
3255 * both at the front and end of the plaintext,
3256 * and expect the encryption to succeed starting from
3257 * some offset.
3258 *
3259 * If encryption succeeds, check that decryption succeeds
3260 * and yields the original record.
3261 */
3262
3263 mbedtls_ssl_context ssl; /* ONLY for debugging */
3264
3265 mbedtls_ssl_transform t0, t1;
Hanno Becker81e16a32019-03-01 11:21:44 +00003266 unsigned char *buf = NULL;
Hanno Beckerd856c822019-04-29 17:30:59 +01003267 size_t const buflen = 256;
Hanno Beckerb3268da2018-01-05 15:20:24 +00003268 mbedtls_record rec, rec_backup;
3269
3270 int ret;
Hanno Beckerd856c822019-04-29 17:30:59 +01003271 int mode; /* Mode 1, 2 or 3 as explained above */
3272 size_t offset; /* Available space at beginning/end/both */
3273 size_t threshold = 96; /* Maximum offset to test against */
Hanno Beckerb3268da2018-01-05 15:20:24 +00003274
Hanno Beckerd856c822019-04-29 17:30:59 +01003275 size_t default_pre_padding = 64; /* Pre-padding to use in mode 2 */
3276 size_t default_post_padding = 128; /* Post-padding to use in mode 1 */
Hanno Beckerb3268da2018-01-05 15:20:24 +00003277
3278 int seen_success; /* Indicates if in the current mode we've
3279 * already seen a successful test. */
3280
3281 mbedtls_ssl_init( &ssl );
3282 mbedtls_ssl_transform_init( &t0 );
3283 mbedtls_ssl_transform_init( &t1 );
3284 TEST_ASSERT( build_transforms( &t0, &t1, cipher_type, hash_id,
Hanno Beckerd856c822019-04-29 17:30:59 +01003285 etm, tag_mode, ver,
3286 (size_t) cid0_len,
3287 (size_t) cid1_len ) == 0 );
Hanno Beckerb3268da2018-01-05 15:20:24 +00003288
Hanno Becker3ee54212019-04-04 16:31:26 +01003289 TEST_ASSERT( ( buf = mbedtls_calloc( 1, buflen ) ) != NULL );
Hanno Beckerb3268da2018-01-05 15:20:24 +00003290
3291 for( mode=1; mode <= 3; mode++ )
3292 {
3293 seen_success = 0;
3294 for( offset=0; offset <= threshold; offset++ )
3295 {
3296 mbedtls_ssl_transform *t_dec, *t_enc;
Hanno Becker6c87b3f2019-04-29 17:24:44 +01003297 t_dec = &t0;
3298 t_enc = &t1;
Hanno Beckerb3268da2018-01-05 15:20:24 +00003299
3300 memset( rec.ctr, offset, sizeof( rec.ctr ) );
3301 rec.type = 42;
3302 rec.ver[0] = offset;
3303 rec.ver[1] = offset;
3304 rec.buf = buf;
3305 rec.buf_len = buflen;
Hanno Beckera0e20d02019-05-15 14:03:01 +01003306#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd856c822019-04-29 17:30:59 +01003307 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01003308#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerb3268da2018-01-05 15:20:24 +00003309
3310 switch( mode )
3311 {
3312 case 1: /* Space in the beginning */
3313 rec.data_offset = offset;
3314 rec.data_len = buflen - offset - default_post_padding;
3315 break;
3316
3317 case 2: /* Space in the end */
3318 rec.data_offset = default_pre_padding;
3319 rec.data_len = buflen - default_pre_padding - offset;
3320 break;
3321
3322 case 3: /* Space in the beginning and end */
3323 rec.data_offset = offset;
3324 rec.data_len = buflen - 2 * offset;
3325 break;
3326
3327 default:
3328 TEST_ASSERT( 0 );
3329 break;
3330 }
3331
3332 memset( rec.buf + rec.data_offset, 42, rec.data_len );
3333
3334 /* Make a copy for later comparison */
3335 rec_backup = rec;
3336
3337 /* Encrypt record */
3338 ret = mbedtls_ssl_encrypt_buf( &ssl, t_enc, &rec, rnd_std_rand, NULL );
3339
3340 if( ( mode == 1 || mode == 2 ) && seen_success )
3341 {
3342 TEST_ASSERT( ret == 0 );
3343 }
3344 else
3345 {
3346 TEST_ASSERT( ret == 0 || ret == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3347 if( ret == 0 )
3348 seen_success = 1;
3349 }
3350
3351 if( ret != 0 )
3352 continue;
3353
Hanno Beckerb2713ab2020-05-07 14:54:22 +01003354#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3355 if( rec.cid_len != 0 )
3356 {
3357 /* DTLS 1.2 + CID hides the real content type and
3358 * uses a special CID content type in the protected
3359 * record. Double-check this. */
3360 TEST_ASSERT( rec.type == MBEDTLS_SSL_MSG_CID );
3361 }
3362#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3363
3364#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
3365 if( t_enc->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
3366 {
3367 /* TLS 1.3 hides the real content type and
3368 * always uses Application Data as the content type
3369 * for protected records. Double-check this. */
3370 TEST_ASSERT( rec.type == MBEDTLS_SSL_MSG_APPLICATION_DATA );
3371 }
3372#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
3373
Hanno Beckerb3268da2018-01-05 15:20:24 +00003374 /* Decrypt record with t_dec */
3375 TEST_ASSERT( mbedtls_ssl_decrypt_buf( &ssl, t_dec, &rec ) == 0 );
3376
3377 /* Compare results */
3378 TEST_ASSERT( rec.type == rec_backup.type );
3379 TEST_ASSERT( memcmp( rec.ctr, rec_backup.ctr, 8 ) == 0 );
3380 TEST_ASSERT( rec.ver[0] == rec_backup.ver[0] );
3381 TEST_ASSERT( rec.ver[1] == rec_backup.ver[1] );
3382 TEST_ASSERT( rec.data_len == rec_backup.data_len );
3383 TEST_ASSERT( rec.data_offset == rec_backup.data_offset );
3384 TEST_ASSERT( memcmp( rec.buf + rec.data_offset,
3385 rec_backup.buf + rec_backup.data_offset,
3386 rec.data_len ) == 0 );
3387 }
3388
3389 TEST_ASSERT( seen_success == 1 );
3390 }
3391
Hanno Becker81e16a32019-03-01 11:21:44 +00003392exit:
3393
Hanno Beckerb3268da2018-01-05 15:20:24 +00003394 /* Cleanup */
3395 mbedtls_ssl_free( &ssl );
3396 mbedtls_ssl_transform_free( &t0 );
3397 mbedtls_ssl_transform_free( &t1 );
3398
Hanno Becker3ee54212019-04-04 16:31:26 +01003399 mbedtls_free( buf );
Hanno Beckerb3268da2018-01-05 15:20:24 +00003400}
3401/* END_CASE */
Ron Eldor824ad7b2019-05-13 14:09:00 +03003402
3403/* BEGIN_CASE */
3404void ssl_tls_prf( int type, data_t * secret, data_t * random,
3405 char *label, data_t *result_hex_str, int exp_ret )
3406{
3407 unsigned char *output;
3408
3409 output = mbedtls_calloc( 1, result_hex_str->len );
3410 if( output == NULL )
3411 goto exit;
3412
Ron Eldor6b9b1b82019-05-15 17:04:33 +03003413#if defined(MBEDTLS_USE_PSA_CRYPTO)
3414 TEST_ASSERT( psa_crypto_init() == 0 );
3415#endif
3416
Ron Eldor824ad7b2019-05-13 14:09:00 +03003417 TEST_ASSERT( mbedtls_ssl_tls_prf( type, secret->x, secret->len,
3418 label, random->x, random->len,
3419 output, result_hex_str->len ) == exp_ret );
3420
3421 if( exp_ret == 0 )
3422 {
3423 TEST_ASSERT( hexcmp( output, result_hex_str->x,
3424 result_hex_str->len, result_hex_str->len ) == 0 );
3425 }
3426exit:
3427
3428 mbedtls_free( output );
3429}
3430/* END_CASE */
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02003431
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02003432/* BEGIN_CASE */
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02003433void ssl_serialize_session_save_load( int ticket_len, char *crt_file )
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02003434{
3435 mbedtls_ssl_session original, restored;
3436 unsigned char *buf = NULL;
3437 size_t len;
3438
3439 /*
3440 * Test that a save-load pair is the identity
3441 */
3442
3443 mbedtls_ssl_session_init( &original );
3444 mbedtls_ssl_session_init( &restored );
3445
3446 /* Prepare a dummy session to work on */
3447 TEST_ASSERT( ssl_populate_session( &original, ticket_len, crt_file ) == 0 );
3448
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02003449 /* Serialize it */
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02003450 TEST_ASSERT( mbedtls_ssl_session_save( &original, NULL, 0, &len )
3451 == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3452 TEST_ASSERT( ( buf = mbedtls_calloc( 1, len ) ) != NULL );
3453 TEST_ASSERT( mbedtls_ssl_session_save( &original, buf, len, &len )
3454 == 0 );
3455
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02003456 /* Restore session from serialized data */
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02003457 TEST_ASSERT( mbedtls_ssl_session_load( &restored, buf, len) == 0 );
3458
3459 /*
3460 * Make sure both session structures are identical
3461 */
3462#if defined(MBEDTLS_HAVE_TIME)
3463 TEST_ASSERT( original.start == restored.start );
3464#endif
3465 TEST_ASSERT( original.ciphersuite == restored.ciphersuite );
3466 TEST_ASSERT( original.compression == restored.compression );
3467 TEST_ASSERT( original.id_len == restored.id_len );
3468 TEST_ASSERT( memcmp( original.id,
3469 restored.id, sizeof( original.id ) ) == 0 );
3470 TEST_ASSERT( memcmp( original.master,
3471 restored.master, sizeof( original.master ) ) == 0 );
3472
3473#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02003474#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02003475 TEST_ASSERT( ( original.peer_cert == NULL ) ==
3476 ( restored.peer_cert == NULL ) );
3477 if( original.peer_cert != NULL )
3478 {
3479 TEST_ASSERT( original.peer_cert->raw.len ==
3480 restored.peer_cert->raw.len );
3481 TEST_ASSERT( memcmp( original.peer_cert->raw.p,
3482 restored.peer_cert->raw.p,
3483 original.peer_cert->raw.len ) == 0 );
3484 }
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02003485#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3486 TEST_ASSERT( original.peer_cert_digest_type ==
3487 restored.peer_cert_digest_type );
3488 TEST_ASSERT( original.peer_cert_digest_len ==
3489 restored.peer_cert_digest_len );
3490 TEST_ASSERT( ( original.peer_cert_digest == NULL ) ==
3491 ( restored.peer_cert_digest == NULL ) );
3492 if( original.peer_cert_digest != NULL )
3493 {
3494 TEST_ASSERT( memcmp( original.peer_cert_digest,
3495 restored.peer_cert_digest,
3496 original.peer_cert_digest_len ) == 0 );
3497 }
3498#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3499#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02003500 TEST_ASSERT( original.verify_result == restored.verify_result );
3501
3502#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
3503 TEST_ASSERT( original.ticket_len == restored.ticket_len );
3504 if( original.ticket_len != 0 )
3505 {
3506 TEST_ASSERT( original.ticket != NULL );
3507 TEST_ASSERT( restored.ticket != NULL );
3508 TEST_ASSERT( memcmp( original.ticket,
3509 restored.ticket, original.ticket_len ) == 0 );
3510 }
3511 TEST_ASSERT( original.ticket_lifetime == restored.ticket_lifetime );
3512#endif
3513
3514#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3515 TEST_ASSERT( original.mfl_code == restored.mfl_code );
3516#endif
3517
3518#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
3519 TEST_ASSERT( original.trunc_hmac == restored.trunc_hmac );
3520#endif
3521
3522#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
3523 TEST_ASSERT( original.encrypt_then_mac == restored.encrypt_then_mac );
3524#endif
3525
3526exit:
3527 mbedtls_ssl_session_free( &original );
3528 mbedtls_ssl_session_free( &restored );
3529 mbedtls_free( buf );
3530}
3531/* END_CASE */
3532
Manuel Pégourié-Gonnardaa755832019-06-03 10:53:47 +02003533/* BEGIN_CASE */
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02003534void ssl_serialize_session_load_save( int ticket_len, char *crt_file )
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02003535{
3536 mbedtls_ssl_session session;
3537 unsigned char *buf1 = NULL, *buf2 = NULL;
3538 size_t len0, len1, len2;
3539
3540 /*
3541 * Test that a load-save pair is the identity
3542 */
3543
3544 mbedtls_ssl_session_init( &session );
3545
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02003546 /* Prepare a dummy session to work on */
Manuel Pégourié-Gonnard6b840702019-05-24 09:40:17 +02003547 TEST_ASSERT( ssl_populate_session( &session, ticket_len, crt_file ) == 0 );
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02003548
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02003549 /* Get desired buffer size for serializing */
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02003550 TEST_ASSERT( mbedtls_ssl_session_save( &session, NULL, 0, &len0 )
3551 == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3552
3553 /* Allocate first buffer */
3554 buf1 = mbedtls_calloc( 1, len0 );
3555 TEST_ASSERT( buf1 != NULL );
3556
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02003557 /* Serialize to buffer and free live session */
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02003558 TEST_ASSERT( mbedtls_ssl_session_save( &session, buf1, len0, &len1 )
3559 == 0 );
3560 TEST_ASSERT( len0 == len1 );
3561 mbedtls_ssl_session_free( &session );
3562
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02003563 /* Restore session from serialized data */
Manuel Pégourié-Gonnard220403b2019-05-24 09:54:21 +02003564 TEST_ASSERT( mbedtls_ssl_session_load( &session, buf1, len1 ) == 0 );
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02003565
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02003566 /* Allocate second buffer and serialize to it */
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02003567 buf2 = mbedtls_calloc( 1, len0 );
Manuel Pégourié-Gonnardb4079902019-05-24 09:52:10 +02003568 TEST_ASSERT( buf2 != NULL );
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02003569 TEST_ASSERT( mbedtls_ssl_session_save( &session, buf2, len0, &len2 )
3570 == 0 );
3571
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02003572 /* Make sure both serialized versions are identical */
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02003573 TEST_ASSERT( len1 == len2 );
3574 TEST_ASSERT( memcmp( buf1, buf2, len1 ) == 0 );
3575
3576exit:
3577 mbedtls_ssl_session_free( &session );
3578 mbedtls_free( buf1 );
3579 mbedtls_free( buf2 );
3580}
3581/* END_CASE */
Manuel Pégourié-Gonnardf5fa0aa2019-05-23 10:38:11 +02003582
3583/* BEGIN_CASE */
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02003584void ssl_serialize_session_save_buf_size( int ticket_len, char *crt_file )
Manuel Pégourié-Gonnardf5fa0aa2019-05-23 10:38:11 +02003585{
3586 mbedtls_ssl_session session;
3587 unsigned char *buf = NULL;
3588 size_t good_len, bad_len, test_len;
3589
3590 /*
3591 * Test that session_save() fails cleanly on small buffers
3592 */
3593
3594 mbedtls_ssl_session_init( &session );
3595
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02003596 /* Prepare dummy session and get serialized size */
Manuel Pégourié-Gonnard6b840702019-05-24 09:40:17 +02003597 TEST_ASSERT( ssl_populate_session( &session, ticket_len, crt_file ) == 0 );
Manuel Pégourié-Gonnardf5fa0aa2019-05-23 10:38:11 +02003598 TEST_ASSERT( mbedtls_ssl_session_save( &session, NULL, 0, &good_len )
3599 == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3600
3601 /* Try all possible bad lengths */
3602 for( bad_len = 1; bad_len < good_len; bad_len++ )
3603 {
3604 /* Allocate exact size so that asan/valgrind can detect any overwrite */
3605 mbedtls_free( buf );
3606 TEST_ASSERT( ( buf = mbedtls_calloc( 1, bad_len ) ) != NULL );
3607 TEST_ASSERT( mbedtls_ssl_session_save( &session, buf, bad_len,
3608 &test_len )
3609 == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3610 TEST_ASSERT( test_len == good_len );
3611 }
3612
3613exit:
3614 mbedtls_ssl_session_free( &session );
3615 mbedtls_free( buf );
3616}
3617/* END_CASE */
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003618
3619/* BEGIN_CASE */
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02003620void ssl_serialize_session_load_buf_size( int ticket_len, char *crt_file )
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003621{
3622 mbedtls_ssl_session session;
3623 unsigned char *good_buf = NULL, *bad_buf = NULL;
3624 size_t good_len, bad_len;
3625
3626 /*
3627 * Test that session_load() fails cleanly on small buffers
3628 */
3629
3630 mbedtls_ssl_session_init( &session );
3631
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02003632 /* Prepare serialized session data */
Manuel Pégourié-Gonnard6b840702019-05-24 09:40:17 +02003633 TEST_ASSERT( ssl_populate_session( &session, ticket_len, crt_file ) == 0 );
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003634 TEST_ASSERT( mbedtls_ssl_session_save( &session, NULL, 0, &good_len )
3635 == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3636 TEST_ASSERT( ( good_buf = mbedtls_calloc( 1, good_len ) ) != NULL );
3637 TEST_ASSERT( mbedtls_ssl_session_save( &session, good_buf, good_len,
3638 &good_len ) == 0 );
3639 mbedtls_ssl_session_free( &session );
3640
3641 /* Try all possible bad lengths */
3642 for( bad_len = 0; bad_len < good_len; bad_len++ )
3643 {
3644 /* Allocate exact size so that asan/valgrind can detect any overread */
3645 mbedtls_free( bad_buf );
3646 bad_buf = mbedtls_calloc( 1, bad_len ? bad_len : 1 );
3647 TEST_ASSERT( bad_buf != NULL );
3648 memcpy( bad_buf, good_buf, bad_len );
3649
3650 TEST_ASSERT( mbedtls_ssl_session_load( &session, bad_buf, bad_len )
3651 == MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3652 }
3653
3654exit:
3655 mbedtls_ssl_session_free( &session );
3656 mbedtls_free( good_buf );
3657 mbedtls_free( bad_buf );
3658}
3659/* END_CASE */
Hanno Becker861d0bb2019-05-21 16:39:30 +01003660
Hanno Becker363b6462019-05-29 12:44:28 +01003661/* BEGIN_CASE */
3662void ssl_session_serialize_version_check( int corrupt_major,
Hanno Becker861d0bb2019-05-21 16:39:30 +01003663 int corrupt_minor,
3664 int corrupt_patch,
3665 int corrupt_config )
3666{
Hanno Becker363b6462019-05-29 12:44:28 +01003667 unsigned char serialized_session[ 2048 ];
3668 size_t serialized_session_len;
Hanno Beckerfe1275e2019-05-29 12:45:21 +01003669 unsigned cur_byte;
Hanno Becker861d0bb2019-05-21 16:39:30 +01003670 mbedtls_ssl_session session;
Hanno Beckerfe1275e2019-05-29 12:45:21 +01003671 uint8_t should_corrupt_byte[] = { corrupt_major == 1,
3672 corrupt_minor == 1,
3673 corrupt_patch == 1,
3674 corrupt_config == 1,
3675 corrupt_config == 1 };
3676
Hanno Becker861d0bb2019-05-21 16:39:30 +01003677 mbedtls_ssl_session_init( &session );
3678
Hanno Beckerfe1275e2019-05-29 12:45:21 +01003679 /* Infer length of serialized session. */
Hanno Becker861d0bb2019-05-21 16:39:30 +01003680 TEST_ASSERT( mbedtls_ssl_session_save( &session,
Hanno Becker363b6462019-05-29 12:44:28 +01003681 serialized_session,
3682 sizeof( serialized_session ),
3683 &serialized_session_len ) == 0 );
Hanno Becker861d0bb2019-05-21 16:39:30 +01003684
Hanno Beckerfe1275e2019-05-29 12:45:21 +01003685 mbedtls_ssl_session_free( &session );
Hanno Becker861d0bb2019-05-21 16:39:30 +01003686
Hanno Beckerfe1275e2019-05-29 12:45:21 +01003687 /* Without any modification, we should be able to successfully
Hanno Becker363b6462019-05-29 12:44:28 +01003688 * de-serialize the session - double-check that. */
Hanno Becker861d0bb2019-05-21 16:39:30 +01003689 TEST_ASSERT( mbedtls_ssl_session_load( &session,
Hanno Becker363b6462019-05-29 12:44:28 +01003690 serialized_session,
3691 serialized_session_len ) == 0 );
Hanno Becker861d0bb2019-05-21 16:39:30 +01003692 mbedtls_ssl_session_free( &session );
3693
Hanno Beckerfe1275e2019-05-29 12:45:21 +01003694 /* Go through the bytes in the serialized session header and
3695 * corrupt them bit-by-bit. */
3696 for( cur_byte = 0; cur_byte < sizeof( should_corrupt_byte ); cur_byte++ )
Hanno Becker861d0bb2019-05-21 16:39:30 +01003697 {
Hanno Beckerfe1275e2019-05-29 12:45:21 +01003698 int cur_bit;
3699 unsigned char * const byte = &serialized_session[ cur_byte ];
3700
3701 if( should_corrupt_byte[ cur_byte ] == 0 )
3702 continue;
3703
3704 for( cur_bit = 0; cur_bit < CHAR_BIT; cur_bit++ )
3705 {
3706 unsigned char const corrupted_bit = 0x1u << cur_bit;
3707 /* Modify a single bit in the serialized session. */
3708 *byte ^= corrupted_bit;
3709
3710 /* Attempt to deserialize */
3711 TEST_ASSERT( mbedtls_ssl_session_load( &session,
3712 serialized_session,
3713 serialized_session_len ) ==
Hanno Beckerf9b33032019-06-03 12:58:39 +01003714 MBEDTLS_ERR_SSL_VERSION_MISMATCH );
Hanno Beckerfe1275e2019-05-29 12:45:21 +01003715
3716 /* Undo the change */
3717 *byte ^= corrupted_bit;
3718 }
Hanno Becker861d0bb2019-05-21 16:39:30 +01003719 }
3720
Hanno Becker861d0bb2019-05-21 16:39:30 +01003721}
3722/* END_CASE */
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01003723
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01003724/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15 */
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01003725void mbedtls_endpoint_sanity( int endpoint_type )
3726{
3727 enum { BUFFSIZE = 1024 };
3728 mbedtls_endpoint ep;
3729 int ret = -1;
3730
Andrzej Kurek15daf502020-02-12 09:17:52 -05003731 ret = mbedtls_endpoint_init( NULL, endpoint_type, MBEDTLS_PK_RSA,
3732 NULL, NULL, NULL );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01003733 TEST_ASSERT( MBEDTLS_ERR_SSL_BAD_INPUT_DATA == ret );
3734
Andrzej Kurekb2980742020-02-02 19:25:26 -05003735 ret = mbedtls_endpoint_certificate_init( NULL, MBEDTLS_PK_RSA );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01003736 TEST_ASSERT( MBEDTLS_ERR_SSL_BAD_INPUT_DATA == ret );
3737
Andrzej Kurek15daf502020-02-12 09:17:52 -05003738 ret = mbedtls_endpoint_init( &ep, endpoint_type, MBEDTLS_PK_RSA,
3739 NULL, NULL, NULL );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01003740 TEST_ASSERT( ret == 0 );
3741
3742exit:
Andrzej Kurek15daf502020-02-12 09:17:52 -05003743 mbedtls_endpoint_free( &ep, NULL );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01003744}
3745/* END_CASE */
3746
Andrzej Kurekb2980742020-02-02 19:25:26 -05003747/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15 */
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01003748void move_handshake_to_state(int endpoint_type, int state, int need_pass)
3749{
3750 enum { BUFFSIZE = 1024 };
3751 mbedtls_endpoint base_ep, second_ep;
3752 int ret = -1;
3753
Andrzej Kurek15daf502020-02-12 09:17:52 -05003754 ret = mbedtls_endpoint_init( &base_ep, endpoint_type, MBEDTLS_PK_RSA,
3755 NULL, NULL, NULL );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01003756 TEST_ASSERT( ret == 0 );
3757
3758 ret = mbedtls_endpoint_init( &second_ep,
3759 ( endpoint_type == MBEDTLS_SSL_IS_SERVER ) ?
Andrzej Kurekb2980742020-02-02 19:25:26 -05003760 MBEDTLS_SSL_IS_CLIENT : MBEDTLS_SSL_IS_SERVER,
Andrzej Kurek15daf502020-02-12 09:17:52 -05003761 MBEDTLS_PK_RSA, NULL, NULL, NULL );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01003762 TEST_ASSERT( ret == 0 );
3763
3764 ret = mbedtls_mock_socket_connect( &(base_ep.socket),
3765 &(second_ep.socket),
3766 BUFFSIZE );
3767 TEST_ASSERT( ret == 0 );
3768
3769 ret = mbedtls_move_handshake_to_state( &(base_ep.ssl),
3770 &(second_ep.ssl),
3771 state );
3772 if( need_pass )
3773 {
3774 TEST_ASSERT( ret == 0 );
3775 TEST_ASSERT( base_ep.ssl.state == state );
3776 }
3777 else
3778 {
3779 TEST_ASSERT( ret != 0 );
3780 TEST_ASSERT( base_ep.ssl.state != state );
3781 }
3782
3783exit:
Andrzej Kurek15daf502020-02-12 09:17:52 -05003784 mbedtls_endpoint_free( &base_ep, NULL );
3785 mbedtls_endpoint_free( &second_ep, NULL );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01003786}
3787/* END_CASE */
Andrzej Kurekf40daa32020-02-04 09:00:01 -05003788
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003789/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED */
3790void handshake_version( int version, int dtls )
Andrzej Kurekf40daa32020-02-04 09:00:01 -05003791{
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003792 handshake_test_options options;
3793 init_handshake_options( &options );
Andrzej Kurekda2b6782020-02-12 07:56:36 -05003794
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003795 options.version = version;
3796 options.dtls = dtls;
Piotr Nowicki438bf3b2020-03-10 12:59:10 +01003797 /* By default, SSLv3.0 and TLSv1.0 use 1/n-1 splitting when sending data, so
3798 * the number of fragments will be twice as big. */
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003799 if( version == MBEDTLS_SSL_MINOR_VERSION_0 ||
3800 version == MBEDTLS_SSL_MINOR_VERSION_1 )
Andrzej Kurek941962e2020-02-07 09:20:32 -05003801 {
Piotr Nowicki438bf3b2020-03-10 12:59:10 +01003802 options.expected_cli_fragments = 2;
3803 options.expected_srv_fragments = 2;
Andrzej Kurek941962e2020-02-07 09:20:32 -05003804 }
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003805 perform_handshake( &options );
Andrzej Kurekf40daa32020-02-04 09:00:01 -05003806
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003807 /* The goto below is used to avoid an "unused label" warning.*/
3808 goto exit;
3809}
3810/* END_CASE */
Andrzej Kurek9e9efdc2020-02-26 05:25:23 -05003811
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003812/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_PROTO_TLS1_2 */
3813void handshake_psk_cipher( char* cipher, int pk_alg, data_t *psk_str, int dtls )
3814{
3815 handshake_test_options options;
3816 init_handshake_options( &options );
Andrzej Kurekf40daa32020-02-04 09:00:01 -05003817
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003818 options.cipher = cipher;
3819 options.dtls = dtls;
3820 options.psk_str = psk_str;
3821 options.pk_alg = pk_alg;
Andrzej Kurekcc5169c2020-02-04 09:04:56 -05003822
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003823 perform_handshake( &options );
Andrzej Kurek316da1f2020-02-26 09:03:47 -05003824
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003825 /* The goto below is used to avoid an "unused label" warning.*/
3826 goto exit;
3827}
3828/* END_CASE */
Andrzej Kurek316da1f2020-02-26 09:03:47 -05003829
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003830/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_PROTO_TLS1_2 */
3831void handshake_cipher( char* cipher, int pk_alg, int dtls )
3832{
3833 test_handshake_psk_cipher( cipher, pk_alg, NULL, dtls );
Andrzej Kurekf40daa32020-02-04 09:00:01 -05003834
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003835 /* The goto below is used to avoid an "unused label" warning.*/
3836 goto exit;
3837}
3838/* END_CASE */
Andrzej Kurekf40daa32020-02-04 09:00:01 -05003839
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003840/* 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 */
3841void app_data( int mfl, int cli_msg_len, int srv_msg_len,
3842 int expected_cli_fragments,
3843 int expected_srv_fragments, int dtls )
3844{
3845 handshake_test_options options;
3846 init_handshake_options( &options );
Andrzej Kurekda2b6782020-02-12 07:56:36 -05003847
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003848 options.mfl = mfl;
3849 options.cli_msg_len = cli_msg_len;
3850 options.srv_msg_len = srv_msg_len;
3851 options.expected_cli_fragments = expected_cli_fragments;
3852 options.expected_srv_fragments = expected_srv_fragments;
3853 options.dtls = dtls;
Andrzej Kurekda2b6782020-02-12 07:56:36 -05003854
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003855 perform_handshake( &options );
3856 /* The goto below is used to avoid an "unused label" warning.*/
3857 goto exit;
3858}
3859/* END_CASE */
Andrzej Kurekda2b6782020-02-12 07:56:36 -05003860
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003861/* 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 */
3862void app_data_tls( int mfl, int cli_msg_len, int srv_msg_len,
3863 int expected_cli_fragments,
3864 int expected_srv_fragments )
3865{
3866 test_app_data( mfl, cli_msg_len, srv_msg_len, expected_cli_fragments,
3867 expected_srv_fragments, 0 );
3868 /* The goto below is used to avoid an "unused label" warning.*/
3869 goto exit;
3870}
3871/* END_CASE */
Andrzej Kurekda2b6782020-02-12 07:56:36 -05003872
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003873/* 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 */
3874void app_data_dtls( int mfl, int cli_msg_len, int srv_msg_len,
3875 int expected_cli_fragments,
3876 int expected_srv_fragments )
3877{
3878 test_app_data( mfl, cli_msg_len, srv_msg_len, expected_cli_fragments,
3879 expected_srv_fragments, 1 );
3880 /* The goto below is used to avoid an "unused label" warning.*/
3881 goto exit;
3882}
3883/* END_CASE */
Andrzej Kurekda2b6782020-02-12 07:56:36 -05003884
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003885/* 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 */
3886void handshake_serialization( )
3887{
3888 handshake_test_options options;
3889 init_handshake_options( &options );
Andrzej Kurekda2b6782020-02-12 07:56:36 -05003890
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003891 options.serialize = 1;
3892 options.dtls = 1;
3893 perform_handshake( &options );
3894 /* The goto below is used to avoid an "unused label" warning.*/
3895 goto exit;
3896}
3897/* END_CASE */
Andrzej Kurekda2b6782020-02-12 07:56:36 -05003898
Darryl Greenaad82f92019-12-02 10:53:11 +00003899/* 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 */
Piotr Nowickibde7ee82020-02-21 10:59:50 +01003900void handshake_fragmentation( int mfl, int expected_srv_hs_fragmentation, int expected_cli_hs_fragmentation)
3901{
3902 handshake_test_options options;
3903 log_pattern srv_pattern, cli_pattern;
3904
3905 srv_pattern.pattern = cli_pattern.pattern = "found fragmented DTLS handshake";
3906 srv_pattern.counter = 0;
3907 cli_pattern.counter = 0;
3908
3909 init_handshake_options( &options );
3910 options.dtls = 1;
3911 options.mfl = mfl;
Darryl Greenaad82f92019-12-02 10:53:11 +00003912 /* Set cipher to one using CBC so that record splitting can be tested */
3913 options.cipher = "TLS-DHE-RSA-WITH-AES-256-CBC-SHA256";
Piotr Nowickibde7ee82020-02-21 10:59:50 +01003914 options.srv_auth_mode = MBEDTLS_SSL_VERIFY_REQUIRED;
3915 options.srv_log_obj = &srv_pattern;
3916 options.cli_log_obj = &cli_pattern;
3917 options.srv_log_fun = log_analyzer;
3918 options.cli_log_fun = log_analyzer;
3919
3920 perform_handshake( &options );
3921
3922 /* Test if the server received a fragmented handshake */
3923 if( expected_srv_hs_fragmentation )
3924 {
3925 TEST_ASSERT( srv_pattern.counter >= 1 );
3926 }
3927 /* Test if the client received a fragmented handshake */
3928 if( expected_cli_hs_fragmentation )
3929 {
3930 TEST_ASSERT( cli_pattern.counter >= 1 );
3931 }
3932}
3933/* END_CASE */
3934
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003935/* 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 */
3936void renegotiation( int legacy_renegotiation )
3937{
3938 handshake_test_options options;
3939 init_handshake_options( &options );
Andrzej Kurekda2b6782020-02-12 07:56:36 -05003940
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003941 options.renegotiate = 1;
3942 options.legacy_renegotiation = legacy_renegotiation;
3943 options.dtls = 1;
Andrzej Kurek316da1f2020-02-26 09:03:47 -05003944
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003945 perform_handshake( &options );
3946 /* The goto below is used to avoid an "unused label" warning.*/
3947 goto exit;
Andrzej Kurekf40daa32020-02-04 09:00:01 -05003948}
3949/* END_CASE */
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003950
3951/* 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 */
3952void resize_buffers( int mfl, int renegotiation, int legacy_renegotiation,
Andrzej Kurek8ea68722020-04-03 06:40:47 -04003953 int serialize, int dtls, char *cipher )
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003954{
3955 handshake_test_options options;
3956 init_handshake_options( &options );
3957
3958 options.mfl = mfl;
Andrzej Kurek8ea68722020-04-03 06:40:47 -04003959 options.cipher = cipher;
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003960 options.renegotiate = renegotiation;
3961 options.legacy_renegotiation = legacy_renegotiation;
3962 options.serialize = serialize;
3963 options.dtls = dtls;
3964 options.resize_buffers = 1;
3965
3966 perform_handshake( &options );
3967 /* The goto below is used to avoid an "unused label" warning.*/
3968 goto exit;
3969}
3970/* END_CASE */
3971
3972/* 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 */
3973void resize_buffers_serialize_mfl( int mfl )
3974{
Andrzej Kurek8ea68722020-04-03 06:40:47 -04003975 test_resize_buffers( mfl, 0, MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION, 1, 1,
3976 (char *) "" );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003977
3978 /* The goto below is used to avoid an "unused label" warning.*/
3979 goto exit;
3980}
3981/* END_CASE */
3982
3983/* 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 */
Andrzej Kurek8ea68722020-04-03 06:40:47 -04003984void resize_buffers_renegotiate_mfl( int mfl, int legacy_renegotiation,
3985 char *cipher )
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003986{
Andrzej Kurek8ea68722020-04-03 06:40:47 -04003987 test_resize_buffers( mfl, 1, legacy_renegotiation, 0, 1, cipher );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003988
3989 /* The goto below is used to avoid an "unused label" warning.*/
3990 goto exit;
3991}
3992/* END_CASE */