blob: 5df4b4753299fe4654d908c25b87ba1bc0850e2b [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
16/* This function can be passed to mbedtls to receive output logs from it. In
17 * this case, it will count the instances of a log_pattern in the received
18 * logged messages.
19 */
20void log_analyzer( void *ctx, int level,
21 const char *file, int line,
22 const char *str )
23{
24 log_pattern *p = (log_pattern *) ctx;
25
26 (void) level;
27 (void) line;
28 (void) file;
29
30 if( NULL != p &&
31 NULL != p->pattern &&
32 NULL != strstr( str, p->pattern ) )
33 {
34 p->counter++;
35 }
36}
Janos Follath6264e662019-11-26 11:11:15 +000037
Andrzej Kurek8a6ff152020-02-26 09:10:14 -050038typedef struct handshake_test_options
39{
40 const char *cipher;
41 int version;
42 int pk_alg;
43 data_t *psk_str;
44 int dtls;
Piotr Nowickibde7ee82020-02-21 10:59:50 +010045 int srv_auth_mode;
Andrzej Kurek8a6ff152020-02-26 09:10:14 -050046 int serialize;
47 int mfl;
48 int cli_msg_len;
49 int srv_msg_len;
50 int expected_cli_fragments;
51 int expected_srv_fragments;
52 int renegotiate;
53 int legacy_renegotiation;
Piotr Nowickibde7ee82020-02-21 10:59:50 +010054 void *srv_log_obj;
55 void *cli_log_obj;
56 void (*srv_log_fun)(void *, int, const char *, int, const char *);
57 void (*cli_log_fun)(void *, int, const char *, int, const char *);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -050058 int resize_buffers;
Andrzej Kurek8a6ff152020-02-26 09:10:14 -050059} handshake_test_options;
60
61void init_handshake_options( handshake_test_options *opts )
62{
63 opts->cipher = "";
64 opts->version = MBEDTLS_SSL_MINOR_VERSION_3;
65 opts->pk_alg = MBEDTLS_PK_RSA;
66 opts->psk_str = NULL;
67 opts->dtls = 0;
Piotr Nowickibde7ee82020-02-21 10:59:50 +010068 opts->srv_auth_mode = MBEDTLS_SSL_VERIFY_NONE;
Andrzej Kurek8a6ff152020-02-26 09:10:14 -050069 opts->serialize = 0;
70 opts->mfl = MBEDTLS_SSL_MAX_FRAG_LEN_NONE;
71 opts->cli_msg_len = 100;
72 opts->srv_msg_len = 100;
73 opts->expected_cli_fragments = 1;
74 opts->expected_srv_fragments = 1;
75 opts->renegotiate = 0;
76 opts->legacy_renegotiation = MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION;
Piotr Nowickibde7ee82020-02-21 10:59:50 +010077 opts->srv_log_obj = NULL;
78 opts->srv_log_obj = NULL;
79 opts->srv_log_fun = NULL;
80 opts->cli_log_fun = NULL;
Andrzej Kurek0afa2a12020-03-03 10:39:58 -050081 opts->resize_buffers = 1;
Andrzej Kurek8a6ff152020-02-26 09:10:14 -050082}
Janos Follath6264e662019-11-26 11:11:15 +000083/*
84 * Buffer structure for custom I/O callbacks.
85 */
86
87typedef struct mbedtls_test_buffer
88{
89 size_t start;
90 size_t content_length;
91 size_t capacity;
92 unsigned char *buffer;
93} mbedtls_test_buffer;
94
95/*
96 * Initialises \p buf. After calling this function it is safe to call
97 * `mbedtls_test_buffer_free()` on \p buf.
98 */
99void mbedtls_test_buffer_init( mbedtls_test_buffer *buf )
100{
101 memset( buf, 0, sizeof( *buf ) );
102}
103
104/*
105 * Sets up \p buf. After calling this function it is safe to call
106 * `mbedtls_test_buffer_put()` and `mbedtls_test_buffer_get()` on \p buf.
107 */
108int mbedtls_test_buffer_setup( mbedtls_test_buffer *buf, size_t capacity )
109{
110 buf->buffer = (unsigned char*) mbedtls_calloc( capacity,
111 sizeof(unsigned char) );
112 if( NULL == buf->buffer )
113 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
114 buf->capacity = capacity;
115
116 return 0;
117}
118
119void mbedtls_test_buffer_free( mbedtls_test_buffer *buf )
120{
121 if( buf->buffer != NULL )
122 mbedtls_free( buf->buffer );
123
124 memset( buf, 0, sizeof( *buf ) );
125}
126
127/*
128 * Puts \p input_len bytes from the \p input buffer into the ring buffer \p buf.
129 *
130 * \p buf must have been initialized and set up by calling
131 * `mbedtls_test_buffer_init()` and `mbedtls_test_buffer_setup()`.
132 *
133 * \retval \p input_len, if the data fits.
134 * \retval 0 <= value < \p input_len, if the data does not fit.
135 * \retval -1, if \p buf is NULL, it hasn't been set up or \p input_len is not
136 * zero and \p input is NULL.
137 */
138int mbedtls_test_buffer_put( mbedtls_test_buffer *buf,
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100139 const unsigned char *input, size_t input_len )
Janos Follath6264e662019-11-26 11:11:15 +0000140{
141 size_t overflow = 0;
142
143 if( ( buf == NULL ) || ( buf->buffer == NULL ) )
144 return -1;
145
146 /* Reduce input_len to a number that fits in the buffer. */
147 if ( ( buf->content_length + input_len ) > buf->capacity )
148 {
149 input_len = buf->capacity - buf->content_length;
150 }
151
152 if( input == NULL )
153 {
154 return ( input_len == 0 ) ? 0 : -1;
155 }
156
Piotr Nowickifb437d72020-01-13 16:59:12 +0100157 /* Check if the buffer has not come full circle and free space is not in
158 * the middle */
159 if( buf->start + buf->content_length < buf->capacity )
Janos Follath6264e662019-11-26 11:11:15 +0000160 {
Piotr Nowickifb437d72020-01-13 16:59:12 +0100161
162 /* Calculate the number of bytes that need to be placed at lower memory
163 * address */
164 if( buf->start + buf->content_length + input_len
165 > buf->capacity )
166 {
167 overflow = ( buf->start + buf->content_length + input_len )
168 % buf->capacity;
169 }
170
171 memcpy( buf->buffer + buf->start + buf->content_length, input,
172 input_len - overflow );
173 memcpy( buf->buffer, input + input_len - overflow, overflow );
174
175 }
176 else
177 {
178 /* The buffer has come full circle and free space is in the middle */
179 memcpy( buf->buffer + buf->start + buf->content_length - buf->capacity,
180 input, input_len );
Janos Follath6264e662019-11-26 11:11:15 +0000181 }
182
Janos Follath6264e662019-11-26 11:11:15 +0000183 buf->content_length += input_len;
Janos Follath6264e662019-11-26 11:11:15 +0000184 return input_len;
185}
186
187/*
Andrzej Kurekf7774142020-01-22 06:34:59 -0500188 * Gets \p output_len bytes from the ring buffer \p buf into the
189 * \p output buffer. The output buffer can be NULL, in this case a part of the
190 * ring buffer will be dropped, if the requested length is available.
Janos Follath6264e662019-11-26 11:11:15 +0000191 *
192 * \p buf must have been initialized and set up by calling
193 * `mbedtls_test_buffer_init()` and `mbedtls_test_buffer_setup()`.
194 *
195 * \retval \p output_len, if the data is available.
196 * \retval 0 <= value < \p output_len, if the data is not available.
Andrzej Kurekf7774142020-01-22 06:34:59 -0500197 * \retval -1, if \buf is NULL or it hasn't been set up.
Janos Follath6264e662019-11-26 11:11:15 +0000198 */
199int mbedtls_test_buffer_get( mbedtls_test_buffer *buf,
200 unsigned char* output, size_t output_len )
201{
202 size_t overflow = 0;
203
204 if( ( buf == NULL ) || ( buf->buffer == NULL ) )
205 return -1;
206
Andrzej Kurekf7774142020-01-22 06:34:59 -0500207 if( output == NULL && output_len == 0 )
208 return 0;
Janos Follath6264e662019-11-26 11:11:15 +0000209
210 if( buf->content_length < output_len )
211 output_len = buf->content_length;
212
213 /* Calculate the number of bytes that need to be drawn from lower memory
214 * address */
215 if( buf->start + output_len > buf->capacity )
216 {
217 overflow = ( buf->start + output_len ) % buf->capacity;
218 }
219
Andrzej Kurekf7774142020-01-22 06:34:59 -0500220 if( output != NULL )
221 {
222 memcpy( output, buf->buffer + buf->start, output_len - overflow );
223 memcpy( output + output_len - overflow, buf->buffer, overflow );
224 }
225
Janos Follath6264e662019-11-26 11:11:15 +0000226 buf->content_length -= output_len;
227 buf->start = ( buf->start + output_len ) % buf->capacity;
228
229 return output_len;
230}
231
Hanno Beckera18d1322018-01-03 14:27:32 +0000232/*
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500233 * Errors used in the message transport mock tests
234 */
235 #define MBEDTLS_TEST_ERROR_ARG_NULL -11
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500236 #define MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED -44
237
238/*
239 * Context for a message metadata queue (fifo) that is on top of the ring buffer.
240 */
241typedef struct mbedtls_test_message_queue
242{
243 size_t *messages;
244 int pos;
245 int num;
246 int capacity;
247} mbedtls_test_message_queue;
248
249/*
250 * Setup and free functions for the message metadata queue.
251 *
252 * \p capacity describes the number of message metadata chunks that can be held
253 * within the queue.
254 *
255 * \retval 0, if a metadata queue of a given length can be allocated.
256 * \retval MBEDTLS_ERR_SSL_ALLOC_FAILED, if allocation failed.
257 */
258int mbedtls_test_message_queue_setup( mbedtls_test_message_queue *queue,
259 size_t capacity )
260{
261 queue->messages = (size_t*) mbedtls_calloc( capacity, sizeof(size_t) );
262 if( NULL == queue->messages )
263 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
264
265 queue->capacity = capacity;
266 queue->pos = 0;
267 queue->num = 0;
268
269 return 0;
270}
271
272void mbedtls_test_message_queue_free( mbedtls_test_message_queue *queue )
273{
274 if( queue == NULL )
275 return;
276
277 if( queue->messages != NULL )
278 mbedtls_free( queue->messages );
279
280 memset( queue, 0, sizeof( *queue ) );
281}
282
283/*
284 * Push message length information onto the message metadata queue.
285 * This will become the last element to leave it (fifo).
286 *
287 * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null.
Andrzej Kurekf46b9122020-02-07 08:19:00 -0500288 * \retval MBEDTLS_ERR_SSL_WANT_WRITE, if the queue is full.
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500289 * \retval \p len, if the push was successful.
290 */
291int mbedtls_test_message_queue_push_info( mbedtls_test_message_queue *queue,
292 size_t len )
293{
294 int place;
295 if( queue == NULL )
296 return MBEDTLS_TEST_ERROR_ARG_NULL;
297
298 if( queue->num >= queue->capacity )
Andrzej Kurekf46b9122020-02-07 08:19:00 -0500299 return MBEDTLS_ERR_SSL_WANT_WRITE;
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500300
301 place = ( queue->pos + queue->num ) % queue->capacity;
302 queue->messages[place] = len;
303 queue->num++;
304 return len;
305}
306
307/*
308 * Pop information about the next message length from the queue. This will be
309 * the oldest inserted message length(fifo). \p msg_len can be null, in which
310 * case the data will be popped from the queue but not copied anywhere.
311 *
312 * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null.
Andrzej Kurekf46b9122020-02-07 08:19:00 -0500313 * \retval MBEDTLS_ERR_SSL_WANT_READ, if the queue is empty.
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500314 * \retval message length, if the pop was successful, up to the given
315 \p buf_len.
316 */
317int mbedtls_test_message_queue_pop_info( mbedtls_test_message_queue *queue,
318 size_t buf_len )
319{
320 size_t message_length;
321 if( queue == NULL )
322 return MBEDTLS_TEST_ERROR_ARG_NULL;
323 if( queue->num == 0 )
Andrzej Kurekf46b9122020-02-07 08:19:00 -0500324 return MBEDTLS_ERR_SSL_WANT_READ;
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500325
326 message_length = queue->messages[queue->pos];
327 queue->messages[queue->pos] = 0;
328 queue->num--;
329 queue->pos++;
330 queue->pos %= queue->capacity;
331 if( queue->pos < 0 )
332 queue->pos += queue->capacity;
333
334 return ( message_length > buf_len ) ? buf_len : message_length;
335}
336
337/*
338 * Take a peek on the info about the next message length from the queue.
339 * This will be the oldest inserted message length(fifo).
340 *
341 * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null.
Andrzej Kurekf46b9122020-02-07 08:19:00 -0500342 * \retval MBEDTLS_ERR_SSL_WANT_READ, if the queue is empty.
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500343 * \retval 0, if the peek was successful.
344 * \retval MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED, if the given buffer length is
345 * too small to fit the message. In this case the \p msg_len will be
346 * set to the full message length so that the
347 * caller knows what portion of the message can be dropped.
348 */
349int mbedtls_test_message_queue_peek_info( mbedtls_test_message_queue *queue,
350 size_t buf_len, size_t* msg_len )
351{
352 if( queue == NULL || msg_len == NULL )
353 return MBEDTLS_TEST_ERROR_ARG_NULL;
354 if( queue->num == 0 )
Andrzej Kurekf46b9122020-02-07 08:19:00 -0500355 return MBEDTLS_ERR_SSL_WANT_READ;
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500356
357 *msg_len = queue->messages[queue->pos];
358 return ( *msg_len > buf_len ) ? MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED : 0;
359}
360/*
Janos Follath031827f2019-11-27 11:12:14 +0000361 * Context for the I/O callbacks simulating network connection.
362 */
363
364#define MBEDTLS_MOCK_SOCKET_CONNECTED 1
365
366typedef struct mbedtls_mock_socket
367{
368 int status;
369 mbedtls_test_buffer *input;
370 mbedtls_test_buffer *output;
371 struct mbedtls_mock_socket *peer;
372} mbedtls_mock_socket;
373
374/*
375 * Setup and teardown functions for mock sockets.
376 */
377void mbedtls_mock_socket_init( mbedtls_mock_socket *socket )
378{
379 memset( socket, 0, sizeof( *socket ) );
380}
381
382/*
383 * Closes the socket \p socket.
384 *
385 * \p socket must have been previously initialized by calling
386 * mbedtls_mock_socket_init().
387 *
388 * This function frees all allocated resources and both sockets are aware of the
389 * new connection state.
390 *
391 * That is, this function does not simulate half-open TCP connections and the
392 * phenomenon that when closing a UDP connection the peer is not aware of the
393 * connection having been closed.
394 */
395void mbedtls_mock_socket_close( mbedtls_mock_socket* socket )
396{
397 if( socket == NULL )
398 return;
399
400 if( socket->input != NULL )
401 {
402 mbedtls_test_buffer_free( socket->input );
403 mbedtls_free( socket->input );
404 }
405
406 if( socket->output != NULL )
407 {
408 mbedtls_test_buffer_free( socket->output );
409 mbedtls_free( socket->output );
410 }
411
412 if( socket->peer != NULL )
413 memset( socket->peer, 0, sizeof( *socket->peer ) );
414
415 memset( socket, 0, sizeof( *socket ) );
416}
417
418/*
419 * Establishes a connection between \p peer1 and \p peer2.
420 *
421 * \p peer1 and \p peer2 must have been previously initialized by calling
422 * mbedtls_mock_socket_init().
423 *
424 * The capacites of the internal buffers are set to \p bufsize. Setting this to
425 * the correct value allows for simulation of MTU, sanity testing the mock
426 * implementation and mocking TCP connections with lower memory cost.
427 */
428int mbedtls_mock_socket_connect( mbedtls_mock_socket* peer1,
429 mbedtls_mock_socket* peer2,
430 size_t bufsize )
431{
432 int ret = -1;
433
Piotr Nowickid796e192020-01-28 12:09:47 +0100434 peer1->output =
Janos Follath031827f2019-11-27 11:12:14 +0000435 (mbedtls_test_buffer*) mbedtls_calloc( 1, sizeof(mbedtls_test_buffer) );
436 if( peer1->output == NULL )
437 {
438 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
439 goto exit;
440 }
441 mbedtls_test_buffer_init( peer1->output );
442 if( 0 != ( ret = mbedtls_test_buffer_setup( peer1->output, bufsize ) ) )
443 {
444 goto exit;
445 }
446
Piotr Nowickid796e192020-01-28 12:09:47 +0100447 peer2->output =
448 (mbedtls_test_buffer*) mbedtls_calloc( 1, sizeof(mbedtls_test_buffer) );
449 if( peer2->output == NULL )
450 {
451 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
452 goto exit;
453 }
454 mbedtls_test_buffer_init( peer2->output );
455 if( 0 != ( ret = mbedtls_test_buffer_setup( peer2->output, bufsize ) ) )
456 {
457 goto exit;
458 }
459
Janos Follath031827f2019-11-27 11:12:14 +0000460 peer1->peer = peer2;
461 peer2->peer = peer1;
Piotr Nowickid796e192020-01-28 12:09:47 +0100462 peer1->input = peer2->output;
463 peer2->input = peer1->output;
Janos Follath031827f2019-11-27 11:12:14 +0000464
465 peer1->status = peer2->status = MBEDTLS_MOCK_SOCKET_CONNECTED;
466 ret = 0;
467
468exit:
469
470 if( ret != 0 )
471 {
472 mbedtls_mock_socket_close( peer1 );
473 mbedtls_mock_socket_close( peer2 );
474 }
475
476 return ret;
477}
478
479/*
480 * Callbacks for simulating blocking I/O over connection-oriented transport.
481 */
482
483int mbedtls_mock_tcp_send_b( void *ctx, const unsigned char *buf, size_t len )
484{
485 mbedtls_mock_socket *socket = (mbedtls_mock_socket*) ctx;
486
487 if( socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED )
488 return -1;
489
490 return mbedtls_test_buffer_put( socket->output, buf, len );
491}
492
493int mbedtls_mock_tcp_recv_b( void *ctx, unsigned char *buf, size_t len )
494{
495 mbedtls_mock_socket *socket = (mbedtls_mock_socket*) ctx;
496
497 if( socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED )
498 return -1;
499
500 return mbedtls_test_buffer_get( socket->input, buf, len );
501}
502
503/*
Janos Follath3766ba52019-11-27 13:31:42 +0000504 * Callbacks for simulating non-blocking I/O over connection-oriented transport.
505 */
506
507int mbedtls_mock_tcp_send_nb( void *ctx, const unsigned char *buf, size_t len )
508{
509 mbedtls_mock_socket *socket = (mbedtls_mock_socket*) ctx;
510
511 if( socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED )
512 return -1;
513
Piotr Nowicki890b5ca2020-01-15 16:19:07 +0100514 if( socket->output->capacity == socket->output->content_length )
Janos Follath3766ba52019-11-27 13:31:42 +0000515 {
Janos Follath3766ba52019-11-27 13:31:42 +0000516 return MBEDTLS_ERR_SSL_WANT_WRITE;
517 }
518
Janos Follath3766ba52019-11-27 13:31:42 +0000519 return mbedtls_test_buffer_put( socket->output, buf, len );
520}
521
522int mbedtls_mock_tcp_recv_nb( void *ctx, unsigned char *buf, size_t len )
523{
524 mbedtls_mock_socket *socket = (mbedtls_mock_socket*) ctx;
525
526 if( socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED )
527 return -1;
528
Andrzej Kurekf40daa32020-02-04 09:00:01 -0500529 if( socket->input->content_length == 0 )
Janos Follath3766ba52019-11-27 13:31:42 +0000530 {
Janos Follath3766ba52019-11-27 13:31:42 +0000531 return MBEDTLS_ERR_SSL_WANT_READ;
532 }
533
Janos Follath3766ba52019-11-27 13:31:42 +0000534 return mbedtls_test_buffer_get( socket->input, buf, len );
535}
536
Andrzej Kurekbc483de2020-01-22 03:40:00 -0500537/* Errors used in the message socket mocks */
538
539#define MBEDTLS_TEST_ERROR_CONTEXT_ERROR -55
540#define MBEDTLS_TEST_ERROR_SEND_FAILED -66
541#define MBEDTLS_TEST_ERROR_RECV_FAILED -77
542
543/*
544 * Structure used as an addon, or a wrapper, around the mocked sockets.
545 * Contains an input queue, to which the other socket pushes metadata,
546 * and an output queue, to which this one pushes metadata. This context is
547 * considered as an owner of the input queue only, which is initialized and
548 * freed in the respective setup and free calls.
549 */
550typedef struct mbedtls_test_message_socket_context
551{
552 mbedtls_test_message_queue* queue_input;
553 mbedtls_test_message_queue* queue_output;
554 mbedtls_mock_socket* socket;
555} mbedtls_test_message_socket_context;
556
Andrzej Kurek45916ba2020-03-05 14:46:22 -0500557void mbedtls_message_socket_init( mbedtls_test_message_socket_context *ctx )
558{
559 ctx->queue_input = NULL;
560 ctx->queue_output = NULL;
561 ctx->socket = NULL;
562}
563
Andrzej Kurekbc483de2020-01-22 03:40:00 -0500564/*
565 * Setup a given mesasge socket context including initialization of
566 * input/output queues to a chosen capacity of messages. Also set the
567 * corresponding mock socket.
568 *
569 * \retval 0, if everything succeeds.
570 * \retval MBEDTLS_ERR_SSL_ALLOC_FAILED, if allocation of a message
571 * queue failed.
572 */
573int mbedtls_message_socket_setup( mbedtls_test_message_queue* queue_input,
574 mbedtls_test_message_queue* queue_output,
575 size_t queue_capacity,
576 mbedtls_mock_socket* socket,
577 mbedtls_test_message_socket_context* ctx )
578{
579 int ret = mbedtls_test_message_queue_setup( queue_input, queue_capacity );
580 if( ret != 0 )
581 return ret;
582 ctx->queue_input = queue_input;
583 ctx->queue_output = queue_output;
584 ctx->socket = socket;
585 mbedtls_mock_socket_init( socket );
586
587 return 0;
588}
589
590/*
591 * Close a given message socket context, along with the socket itself. Free the
592 * memory allocated by the input queue.
593 */
594void mbedtls_message_socket_close( mbedtls_test_message_socket_context* ctx )
595{
596 if( ctx == NULL )
597 return;
598
599 mbedtls_test_message_queue_free( ctx->queue_input );
600 mbedtls_mock_socket_close( ctx->socket );
601 memset( ctx, 0, sizeof( *ctx ) );
602}
603
604/*
605 * Send one message through a given message socket context.
606 *
607 * \retval \p len, if everything succeeds.
608 * \retval MBEDTLS_TEST_ERROR_CONTEXT_ERROR, if any of the needed context
609 * elements or the context itself is null.
610 * \retval MBEDTLS_TEST_ERROR_SEND_FAILED if mbedtls_mock_tcp_send_b failed.
Andrzej Kurekf46b9122020-02-07 08:19:00 -0500611 * \retval MBEDTLS_ERR_SSL_WANT_WRITE, if the output queue is full.
Andrzej Kurekbc483de2020-01-22 03:40:00 -0500612 *
613 * This function will also return any error from
614 * mbedtls_test_message_queue_push_info.
615 */
616int mbedtls_mock_tcp_send_msg( void *ctx, const unsigned char *buf, size_t len )
617{
618 mbedtls_test_message_queue* queue;
619 mbedtls_mock_socket* socket;
620 mbedtls_test_message_socket_context *context = (mbedtls_test_message_socket_context*) ctx;
621
622 if( context == NULL || context->socket == NULL
623 || context->queue_output == NULL )
624 {
625 return MBEDTLS_TEST_ERROR_CONTEXT_ERROR;
626 }
627
628 queue = context->queue_output;
629 socket = context->socket;
630
631 if( queue->num >= queue->capacity )
Andrzej Kurekf46b9122020-02-07 08:19:00 -0500632 return MBEDTLS_ERR_SSL_WANT_WRITE;
Andrzej Kurekbc483de2020-01-22 03:40:00 -0500633
634 if( mbedtls_mock_tcp_send_b( socket, buf, len ) != (int) len )
635 return MBEDTLS_TEST_ERROR_SEND_FAILED;
636
637 return mbedtls_test_message_queue_push_info( queue, len );
638}
639
640/*
641 * Receive one message from a given message socket context and return message
642 * length or an error.
643 *
644 * \retval message length, if everything succeeds.
645 * \retval MBEDTLS_TEST_ERROR_CONTEXT_ERROR, if any of the needed context
646 * elements or the context itself is null.
647 * \retval MBEDTLS_TEST_ERROR_RECV_FAILED if mbedtls_mock_tcp_recv_b failed.
648 *
649 * This function will also return any error other than
650 * MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED from mbedtls_test_message_queue_peek_info.
651 */
652int mbedtls_mock_tcp_recv_msg( void *ctx, unsigned char *buf, size_t buf_len )
653{
654 mbedtls_test_message_queue* queue;
655 mbedtls_mock_socket* socket;
656 mbedtls_test_message_socket_context *context = (mbedtls_test_message_socket_context*) ctx;
657 size_t drop_len;
658 size_t msg_len;
659 int ret;
660
661 if( context == NULL || context->socket == NULL
662 || context->queue_input == NULL )
663 {
664 return MBEDTLS_TEST_ERROR_CONTEXT_ERROR;
665 }
666
667 queue = context->queue_input;
668 socket = context->socket;
669
670 /* Peek first, so that in case of a socket error the data remains in
671 * the queue. */
672 ret = mbedtls_test_message_queue_peek_info( queue, buf_len, &msg_len );
673 if( ret == MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED )
674 {
675 /* Calculate how much to drop */
676 drop_len = msg_len - buf_len;
677
678 /* Set the requested message len to be buffer length */
679 msg_len = buf_len;
680 } else if( ret != 0 )
681 {
682 return ret;
683 }
684
685 if( mbedtls_mock_tcp_recv_b( socket, buf, msg_len ) != (int) msg_len )
686 return MBEDTLS_TEST_ERROR_RECV_FAILED;
687
688 if( ret == MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED )
689 {
690 /* Drop the remaining part of the message */
691 if( mbedtls_mock_tcp_recv_b( socket, NULL, drop_len ) != (int) drop_len )
692 {
693 /* Inconsistent state - part of the message was read,
694 * and a part couldn't. Not much we can do here, but it should not
695 * happen in test environment, unless forced manually. */
696 }
697 }
698 mbedtls_test_message_queue_pop_info( queue, buf_len );
699
700 return msg_len;
701}
702
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100703#if defined(MBEDTLS_X509_CRT_PARSE_C)
704
705/*
706 * Structure with endpoint's certificates for SSL communication tests.
707 */
708typedef struct mbedtls_endpoint_certificate
709{
710 mbedtls_x509_crt ca_cert;
711 mbedtls_x509_crt cert;
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100712 mbedtls_pk_context pkey;
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100713} mbedtls_endpoint_certificate;
714
715/*
716 * Endpoint structure for SSL communication tests.
717 */
718typedef struct mbedtls_endpoint
719{
720 const char *name;
721 mbedtls_ssl_context ssl;
722 mbedtls_ssl_config conf;
723 mbedtls_ctr_drbg_context ctr_drbg;
724 mbedtls_entropy_context entropy;
725 mbedtls_mock_socket socket;
726 mbedtls_endpoint_certificate cert;
727} mbedtls_endpoint;
728
729/*
730 * Initializes \p ep_cert structure and assigns it to endpoint
731 * represented by \p ep.
732 *
733 * \retval 0 on success, otherwise error code.
734 */
Andrzej Kurekb2980742020-02-02 19:25:26 -0500735int mbedtls_endpoint_certificate_init( mbedtls_endpoint *ep, int pk_alg )
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100736{
737 int i = 0;
738 int ret = -1;
739 mbedtls_endpoint_certificate *cert;
740
741 if( ep == NULL )
742 {
743 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
744 }
745
746 cert = &( ep->cert );
747 mbedtls_x509_crt_init( &( cert->ca_cert ) );
748 mbedtls_x509_crt_init( &( cert->cert ) );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100749 mbedtls_pk_init( &( cert->pkey ) );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100750
751 /* Load the trusted CA */
752
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100753 for( i = 0; mbedtls_test_cas_der[i] != NULL; i++ )
754 {
755 ret = mbedtls_x509_crt_parse_der( &( cert->ca_cert ),
756 (const unsigned char *) mbedtls_test_cas_der[i],
757 mbedtls_test_cas_der_len[i] );
758 TEST_ASSERT( ret == 0 );
759 }
760
761 /* Load own certificate and private key */
762
763 if( ep->conf.endpoint == MBEDTLS_SSL_IS_SERVER )
764 {
Andrzej Kurekb2980742020-02-02 19:25:26 -0500765 if( pk_alg == MBEDTLS_PK_RSA )
766 {
767 ret = mbedtls_x509_crt_parse( &( cert->cert ),
768 (const unsigned char*) mbedtls_test_srv_crt_rsa_sha256_der,
769 mbedtls_test_srv_crt_rsa_sha256_der_len );
770 TEST_ASSERT( ret == 0 );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100771
Andrzej Kurekb2980742020-02-02 19:25:26 -0500772 ret = mbedtls_pk_parse_key( &( cert->pkey ),
773 (const unsigned char*) mbedtls_test_srv_key_rsa_der,
774 mbedtls_test_srv_key_rsa_der_len, NULL, 0 );
775 TEST_ASSERT( ret == 0 );
776 }
777 else
778 {
779 ret = mbedtls_x509_crt_parse( &( cert->cert ),
780 (const unsigned char*) mbedtls_test_srv_crt_ec_der,
781 mbedtls_test_srv_crt_ec_der_len );
782 TEST_ASSERT( ret == 0 );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100783
Andrzej Kurekb2980742020-02-02 19:25:26 -0500784 ret = mbedtls_pk_parse_key( &( cert->pkey ),
785 (const unsigned char*) mbedtls_test_srv_key_ec_der,
786 mbedtls_test_srv_key_ec_der_len, NULL, 0 );
787 TEST_ASSERT( ret == 0 );
788 }
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100789 }
790 else
791 {
Andrzej Kurekb2980742020-02-02 19:25:26 -0500792 if( pk_alg == MBEDTLS_PK_RSA )
793 {
794 ret = mbedtls_x509_crt_parse( &( cert->cert ),
795 (const unsigned char *) mbedtls_test_cli_crt_rsa_der,
796 mbedtls_test_cli_crt_rsa_der_len );
797 TEST_ASSERT( ret == 0 );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100798
Andrzej Kurekb2980742020-02-02 19:25:26 -0500799 ret = mbedtls_pk_parse_key( &( cert->pkey ),
800 (const unsigned char *) mbedtls_test_cli_key_rsa_der,
801 mbedtls_test_cli_key_rsa_der_len, NULL, 0 );
802 TEST_ASSERT( ret == 0 );
803 }
804 else
805 {
806 ret = mbedtls_x509_crt_parse( &( cert->cert ),
807 (const unsigned char *) mbedtls_test_cli_crt_ec_der,
808 mbedtls_test_cli_crt_ec_len );
809 TEST_ASSERT( ret == 0 );
810
811 ret = mbedtls_pk_parse_key( &( cert->pkey ),
812 (const unsigned char *) mbedtls_test_cli_key_ec_der,
813 mbedtls_test_cli_key_ec_der_len, NULL, 0 );
814 TEST_ASSERT( ret == 0 );
815 }
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100816 }
817
818 mbedtls_ssl_conf_ca_chain( &( ep->conf ), &( cert->ca_cert ), NULL );
819
Andrzej Kurekb2980742020-02-02 19:25:26 -0500820 ret = mbedtls_ssl_conf_own_cert( &( ep->conf ), &( cert->cert ),
821 &( cert->pkey ) );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100822 TEST_ASSERT( ret == 0 );
823
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100824exit:
825 if( ret != 0 )
826 {
827 mbedtls_x509_crt_free( &( cert->ca_cert ) );
828 mbedtls_x509_crt_free( &( cert->cert ) );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100829 mbedtls_pk_free( &( cert->pkey ) );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100830 }
831
832 return ret;
833}
834
835/*
836 * Initializes \p ep structure. It is important to call `mbedtls_endpoint_free()`
837 * after calling this function even if it fails.
838 *
839 * \p endpoint_type must be set as MBEDTLS_SSL_IS_SERVER or
840 * MBEDTLS_SSL_IS_CLIENT.
Andrzej Kurek15daf502020-02-12 09:17:52 -0500841 * \p pk_alg the algorithm to use, currently only MBEDTLS_PK_RSA and
842 * MBEDTLS_PK_ECDSA are supported.
843 * \p dtls_context - in case of DTLS - this is the context handling metadata.
844 * \p input_queue - used only in case of DTLS.
845 * \p output_queue - used only in case of DTLS.
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100846 *
847 * \retval 0 on success, otherwise error code.
848 */
Andrzej Kurek15daf502020-02-12 09:17:52 -0500849int mbedtls_endpoint_init( mbedtls_endpoint *ep, int endpoint_type, int pk_alg,
850 mbedtls_test_message_socket_context *dtls_context,
851 mbedtls_test_message_queue *input_queue,
852 mbedtls_test_message_queue *output_queue )
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100853{
854 int ret = -1;
855
Andrzej Kurek15daf502020-02-12 09:17:52 -0500856 if( dtls_context != NULL && ( input_queue == NULL || output_queue == NULL ) )
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100857 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Andrzej Kurek15daf502020-02-12 09:17:52 -0500858
859 if( ep == NULL )
860 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100861
862 memset( ep, 0, sizeof( *ep ) );
863
864 ep->name = ( endpoint_type == MBEDTLS_SSL_IS_SERVER ) ? "Server" : "Client";
865
866 mbedtls_ssl_init( &( ep->ssl ) );
867 mbedtls_ssl_config_init( &( ep->conf ) );
868 mbedtls_ctr_drbg_init( &( ep->ctr_drbg ) );
869 mbedtls_ssl_conf_rng( &( ep->conf ),
870 mbedtls_ctr_drbg_random,
871 &( ep->ctr_drbg ) );
872 mbedtls_entropy_init( &( ep->entropy ) );
Andrzej Kurek15daf502020-02-12 09:17:52 -0500873 if( dtls_context != NULL )
874 {
875 TEST_ASSERT( mbedtls_message_socket_setup( input_queue, output_queue,
876 100, &( ep->socket ),
877 dtls_context ) == 0 );
878 }
879 else
880 {
881 mbedtls_mock_socket_init( &( ep->socket ) );
882 }
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100883
884 ret = mbedtls_ctr_drbg_seed( &( ep->ctr_drbg ), mbedtls_entropy_func,
885 &( ep->entropy ), (const unsigned char *) ( ep->name ),
886 strlen( ep->name ) );
887 TEST_ASSERT( ret == 0 );
888
889 /* Non-blocking callbacks without timeout */
Andrzej Kurek15daf502020-02-12 09:17:52 -0500890 if( dtls_context != NULL )
891 {
892 mbedtls_ssl_set_bio( &( ep->ssl ), dtls_context,
893 mbedtls_mock_tcp_send_msg,
894 mbedtls_mock_tcp_recv_msg,
895 NULL );
896 }
897 else
898 {
899 mbedtls_ssl_set_bio( &( ep->ssl ), &( ep->socket ),
900 mbedtls_mock_tcp_send_nb,
901 mbedtls_mock_tcp_recv_nb,
902 NULL );
903 }
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100904
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100905 ret = mbedtls_ssl_config_defaults( &( ep->conf ), endpoint_type,
Andrzej Kurek15daf502020-02-12 09:17:52 -0500906 ( dtls_context != NULL ) ?
907 MBEDTLS_SSL_TRANSPORT_DATAGRAM :
908 MBEDTLS_SSL_TRANSPORT_STREAM,
909 MBEDTLS_SSL_PRESET_DEFAULT );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100910 TEST_ASSERT( ret == 0 );
911
Andrzej Kurek1a44a152020-02-07 08:21:32 -0500912 ret = mbedtls_ssl_setup( &( ep->ssl ), &( ep->conf ) );
913 TEST_ASSERT( ret == 0 );
Andrzej Kurek15daf502020-02-12 09:17:52 -0500914
915#if defined(MBEDTLS_SSL_PROTO_DTLS) && defined(MBEDTLS_SSL_SRV_C)
916 if( endpoint_type == MBEDTLS_SSL_IS_SERVER && dtls_context != NULL )
917 mbedtls_ssl_conf_dtls_cookies( &( ep->conf ), NULL, NULL, NULL );
918#endif
919
Andrzej Kurekb2980742020-02-02 19:25:26 -0500920 ret = mbedtls_endpoint_certificate_init( ep, pk_alg );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100921 TEST_ASSERT( ret == 0 );
922
923exit:
924 return ret;
925}
926
927/*
928 * Deinitializes certificates from endpoint represented by \p ep.
929 */
930void mbedtls_endpoint_certificate_free( mbedtls_endpoint *ep )
931{
932 mbedtls_endpoint_certificate *cert = &( ep->cert );
933 mbedtls_x509_crt_free( &( cert->ca_cert ) );
934 mbedtls_x509_crt_free( &( cert->cert ) );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100935 mbedtls_pk_free( &( cert->pkey ) );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100936}
937
938/*
939 * Deinitializes endpoint represented by \p ep.
940 */
Andrzej Kurek15daf502020-02-12 09:17:52 -0500941void mbedtls_endpoint_free( mbedtls_endpoint *ep,
942 mbedtls_test_message_socket_context *context )
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100943{
944 mbedtls_endpoint_certificate_free( ep );
945
946 mbedtls_ssl_free( &( ep->ssl ) );
947 mbedtls_ssl_config_free( &( ep->conf ) );
948 mbedtls_ctr_drbg_free( &( ep->ctr_drbg ) );
949 mbedtls_entropy_free( &( ep->entropy ) );
Andrzej Kurek15daf502020-02-12 09:17:52 -0500950
951 if( context != NULL )
952 {
953 mbedtls_message_socket_close( context );
954 }
955 else
956 {
957 mbedtls_mock_socket_close( &( ep->socket ) );
958 }
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100959}
960
961/*
962 * This function moves ssl handshake from \p ssl to prescribed \p state.
963 * /p second_ssl is used as second endpoint and their sockets have to be
964 * connected before calling this function.
965 *
966 * \retval 0 on success, otherwise error code.
967 */
968int mbedtls_move_handshake_to_state( mbedtls_ssl_context *ssl,
969 mbedtls_ssl_context *second_ssl,
970 int state )
971{
972 enum { BUFFSIZE = 1024 };
973 int max_steps = 1000;
974 int ret = 0;
975
976 if( ssl == NULL || second_ssl == NULL )
977 {
978 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
979 }
980
981 /* Perform communication via connected sockets */
982 while( ( ssl->state != state ) && ( --max_steps >= 0 ) )
983 {
984 /* If /p second_ssl ends the handshake procedure before /p ssl then
985 * there is no need to call the next step */
986 if( second_ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
987 {
988 ret = mbedtls_ssl_handshake_step( second_ssl );
989 if( ret != 0 && ret != MBEDTLS_ERR_SSL_WANT_READ &&
990 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
991 {
992 return ret;
993 }
994 }
995
996 /* We only care about the \p ssl state and returns, so we call it last,
997 * to leave the iteration as soon as the state is as expected. */
998 ret = mbedtls_ssl_handshake_step( ssl );
999 if( ret != 0 && ret != MBEDTLS_ERR_SSL_WANT_READ &&
1000 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
1001 {
1002 return ret;
1003 }
1004 }
1005
1006 return ( max_steps >= 0 ) ? ret : -1;
1007}
1008
1009#endif /* MBEDTLS_X509_CRT_PARSE_C */
1010
Janos Follath3766ba52019-11-27 13:31:42 +00001011/*
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001012 * Write application data. Increase write counter and fragments counter if
1013 * 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,
1017 int *fragments, 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 {
1022 (*fragments)++;
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001023 *written += ret;
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001024 }
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001025
1026 if( expected_fragments == 0 )
1027 {
1028 /* Used for DTLS and the message size larger than MFL. In that case
1029 * the message can not be fragmented and the library should return
1030 * MBEDTLS_ERR_SSL_BAD_INPUT_DATA error. This error must be returned
1031 * to prevent a dead loop inside mbedtls_exchange_data(). */
1032 return ret;
1033 }
1034 else if( expected_fragments == 1 )
1035 {
1036 /* Used for TLS/DTLS and the message size lower than MFL */
1037 TEST_ASSERT( ret == buf_len ||
1038 ret == MBEDTLS_ERR_SSL_WANT_READ ||
1039 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
1040 }
1041 else
1042 {
1043 /* Used for TLS and the message size larger than MFL */
1044 TEST_ASSERT( expected_fragments > 1 );
1045 TEST_ASSERT( ( ret >= 0 && ret <= buf_len ) ||
1046 ret == MBEDTLS_ERR_SSL_WANT_READ ||
1047 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
1048 }
1049
1050 return 0;
1051
1052exit:
1053 /* Some of the tests failed */
1054 return -1;
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001055}
1056
1057/*
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001058 * Read application data and increase read counter if necessary.
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001059 */
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001060int mbedtls_ssl_read_fragment( mbedtls_ssl_context *ssl, unsigned char *buf,
1061 int buf_len, int *read,
1062 const int expected_fragments )
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001063{
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001064 int ret = mbedtls_ssl_read( ssl, buf + *read, buf_len - *read );
1065 if( ret > 0 )
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001066 {
1067 *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:
1309 t_out->fixed_ivlen = 4;
1310 t_in->fixed_ivlen = 4;
1311 t_out->maclen = 0;
1312 t_in->maclen = 0;
1313 switch( tag_mode )
1314 {
1315 case 0: /* Full tag */
1316 t_out->taglen = 16;
1317 t_in->taglen = 16;
1318 break;
1319 case 1: /* Partial tag */
1320 t_out->taglen = 8;
1321 t_in->taglen = 8;
1322 break;
1323 default:
1324 return( 1 );
1325 }
1326 break;
1327
1328 case MBEDTLS_MODE_CHACHAPOLY:
1329 t_out->fixed_ivlen = 12;
1330 t_in->fixed_ivlen = 12;
1331 t_out->maclen = 0;
1332 t_in->maclen = 0;
1333 switch( tag_mode )
1334 {
1335 case 0: /* Full tag */
1336 t_out->taglen = 16;
1337 t_in->taglen = 16;
1338 break;
1339 case 1: /* Partial tag */
1340 t_out->taglen = 8;
1341 t_in->taglen = 8;
1342 break;
1343 default:
1344 return( 1 );
1345 }
1346 break;
1347
1348 case MBEDTLS_MODE_STREAM:
1349 case MBEDTLS_MODE_CBC:
1350 t_out->fixed_ivlen = 0; /* redundant, must be 0 */
1351 t_in->fixed_ivlen = 0; /* redundant, must be 0 */
1352 t_out->taglen = 0;
1353 t_in->taglen = 0;
1354 switch( tag_mode )
1355 {
1356 case 0: /* Full tag */
1357 t_out->maclen = maclen;
1358 t_in->maclen = maclen;
1359 break;
1360 case 1: /* Partial tag */
1361 t_out->maclen = 10;
1362 t_in->maclen = 10;
1363 break;
1364 default:
1365 return( 1 );
1366 }
1367 break;
1368 default:
1369 return( 1 );
1370 break;
1371 }
1372
1373 /* Setup IV's */
1374
1375 memcpy( &t_in->iv_dec, iv_dec, sizeof( iv_dec ) );
1376 memcpy( &t_in->iv_enc, iv_enc, sizeof( iv_enc ) );
1377 memcpy( &t_out->iv_dec, iv_enc, sizeof( iv_enc ) );
1378 memcpy( &t_out->iv_enc, iv_dec, sizeof( iv_dec ) );
1379
Hanno Beckera0e20d02019-05-15 14:03:01 +01001380#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd856c822019-04-29 17:30:59 +01001381 /* Add CID */
1382 memcpy( &t_in->in_cid, cid0, cid0_len );
1383 memcpy( &t_in->out_cid, cid1, cid1_len );
1384 t_in->in_cid_len = cid0_len;
1385 t_in->out_cid_len = cid1_len;
1386 memcpy( &t_out->in_cid, cid1, cid1_len );
1387 memcpy( &t_out->out_cid, cid0, cid0_len );
1388 t_out->in_cid_len = cid1_len;
1389 t_out->out_cid_len = cid0_len;
Hanno Beckera0e20d02019-05-15 14:03:01 +01001390#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerd856c822019-04-29 17:30:59 +01001391
Hanno Becker81e16a32019-03-01 11:21:44 +00001392cleanup:
1393
Hanno Becker3ee54212019-04-04 16:31:26 +01001394 mbedtls_free( key0 );
1395 mbedtls_free( key1 );
Hanno Becker81e16a32019-03-01 11:21:44 +00001396
Hanno Beckera5780f12019-04-05 09:55:37 +01001397 return( ret );
Hanno Beckera18d1322018-01-03 14:27:32 +00001398}
1399
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001400/*
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02001401 * Populate a session structure for serialization tests.
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001402 * Choose dummy values, mostly non-0 to distinguish from the init default.
1403 */
1404static int ssl_populate_session( mbedtls_ssl_session *session,
Manuel Pégourié-Gonnard220403b2019-05-24 09:54:21 +02001405 int ticket_len,
1406 const char *crt_file )
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001407{
1408#if defined(MBEDTLS_HAVE_TIME)
1409 session->start = mbedtls_time( NULL ) - 42;
1410#endif
1411 session->ciphersuite = 0xabcd;
1412 session->compression = 1;
1413 session->id_len = sizeof( session->id );
1414 memset( session->id, 66, session->id_len );
Manuel Pégourié-Gonnard220403b2019-05-24 09:54:21 +02001415 memset( session->master, 17, sizeof( session->master ) );
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001416
Manuel Pégourié-Gonnard1f6033a2019-05-24 10:17:52 +02001417#if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001418 if( strlen( crt_file ) != 0 )
1419 {
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02001420 mbedtls_x509_crt tmp_crt;
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001421 int ret;
Manuel Pégourié-Gonnard6b840702019-05-24 09:40:17 +02001422
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02001423 mbedtls_x509_crt_init( &tmp_crt );
1424 ret = mbedtls_x509_crt_parse_file( &tmp_crt, crt_file );
1425 if( ret != 0 )
1426 return( ret );
1427
1428#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1429 /* Move temporary CRT. */
Manuel Pégourié-Gonnard6b840702019-05-24 09:40:17 +02001430 session->peer_cert = mbedtls_calloc( 1, sizeof( *session->peer_cert ) );
1431 if( session->peer_cert == NULL )
1432 return( -1 );
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02001433 *session->peer_cert = tmp_crt;
1434 memset( &tmp_crt, 0, sizeof( tmp_crt ) );
1435#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
1436 /* Calculate digest of temporary CRT. */
1437 session->peer_cert_digest =
1438 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
1439 if( session->peer_cert_digest == NULL )
1440 return( -1 );
1441 ret = mbedtls_md( mbedtls_md_info_from_type(
1442 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
1443 tmp_crt.raw.p, tmp_crt.raw.len,
1444 session->peer_cert_digest );
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001445 if( ret != 0 )
1446 return( ret );
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02001447 session->peer_cert_digest_type =
1448 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
1449 session->peer_cert_digest_len =
1450 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
1451#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
1452
1453 mbedtls_x509_crt_free( &tmp_crt );
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001454 }
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02001455#else /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_FS_IO */
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001456 (void) crt_file;
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02001457#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_FS_IO */
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001458 session->verify_result = 0xdeadbeef;
1459
1460#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
1461 if( ticket_len != 0 )
1462 {
1463 session->ticket = mbedtls_calloc( 1, ticket_len );
Manuel Pégourié-Gonnard220403b2019-05-24 09:54:21 +02001464 if( session->ticket == NULL )
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001465 return( -1 );
1466 memset( session->ticket, 33, ticket_len );
1467 }
1468 session->ticket_len = ticket_len;
1469 session->ticket_lifetime = 86401;
1470#else
1471 (void) ticket_len;
1472#endif
1473
1474#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1475 session->mfl_code = 1;
1476#endif
1477#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1478 session->trunc_hmac = 1;
1479#endif
1480#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1481 session->encrypt_then_mac = 1;
1482#endif
1483
1484 return( 0 );
1485}
1486
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001487/*
1488 * Perform data exchanging between \p ssl_1 and \p ssl_2 and check if the
1489 * message was sent in the correct number of fragments.
1490 *
1491 * /p ssl_1 and /p ssl_2 Endpoints represented by mbedtls_ssl_context. Both
1492 * of them must be initialized and connected beforehand.
1493 * /p msg_len_1 and /p msg_len_2 specify the size of the message to send.
1494 * /p expected_fragments_1 and /p expected_fragments_2 determine in how many
1495 * fragments the message should be sent.
1496 * expected_fragments is 0: can be used for DTLS testing while the message
1497 * size is larger than MFL. In that case the message
1498 * cannot be fragmented and sent to the second endpoint.
1499 * This value can be used for negative tests.
1500 * expected_fragments is 1: can be used for TLS/DTLS testing while the
1501 * message size is below MFL
1502 * expected_fragments > 1: can be used for TLS testing while the message
1503 * size is larger than MFL
1504 *
1505 * \retval 0 on success, otherwise error code.
1506 */
1507int mbedtls_exchange_data( mbedtls_ssl_context *ssl_1,
1508 int msg_len_1, const int expected_fragments_1,
1509 mbedtls_ssl_context *ssl_2,
1510 int msg_len_2, const int expected_fragments_2 )
1511{
1512 unsigned char *msg_buf_1 = malloc( msg_len_1 );
1513 unsigned char *msg_buf_2 = malloc( msg_len_2 );
1514 unsigned char *in_buf_1 = malloc( msg_len_2 );
1515 unsigned char *in_buf_2 = malloc( msg_len_1 );
1516 int msg_type, ret = -1;
1517
1518 /* Perform this test with two message types. At first use a message
1519 * consisting of only 0x00 for the client and only 0xFF for the server.
1520 * At the second time use message with generated data */
1521 for( msg_type = 0; msg_type < 2; msg_type++ )
1522 {
1523 int written_1 = 0;
1524 int written_2 = 0;
1525 int read_1 = 0;
1526 int read_2 = 0;
1527 int fragments_1 = 0;
1528 int fragments_2 = 0;
1529
1530 if( msg_type == 0 )
1531 {
1532 memset( msg_buf_1, 0x00, msg_len_1 );
1533 memset( msg_buf_2, 0xff, msg_len_2 );
1534 }
1535 else
1536 {
1537 int i, j = 0;
1538 for( i = 0; i < msg_len_1; i++ )
1539 {
1540 msg_buf_1[i] = j++ & 0xFF;
1541 }
1542 for( i = 0; i < msg_len_2; i++ )
1543 {
1544 msg_buf_2[i] = ( j -= 5 ) & 0xFF;
1545 }
1546 }
1547
1548 while( read_1 < msg_len_2 || read_2 < msg_len_1 )
1549 {
1550 /* ssl_1 sending */
1551 if( msg_len_1 > written_1 )
1552 {
1553 ret = mbedtls_ssl_write_fragment( ssl_1, msg_buf_1,
1554 msg_len_1, &written_1,
1555 &fragments_1,
1556 expected_fragments_1 );
1557 if( expected_fragments_1 == 0 )
1558 {
1559 /* This error is expected when the message is too large and
1560 * cannot be fragmented */
1561 TEST_ASSERT( ret == MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1562 msg_len_1 = 0;
1563 }
1564 else
1565 {
1566 TEST_ASSERT( ret == 0 );
1567 }
1568 }
1569
1570 /* ssl_2 sending */
1571 if( msg_len_2 > written_2 )
1572 {
1573 ret = mbedtls_ssl_write_fragment( ssl_2, msg_buf_2,
1574 msg_len_2, &written_2,
1575 &fragments_2,
1576 expected_fragments_2 );
1577 if( expected_fragments_2 == 0 )
1578 {
1579 /* This error is expected when the message is too large and
1580 * cannot be fragmented */
1581 TEST_ASSERT( ret == MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1582 msg_len_2 = 0;
1583 }
1584 else
1585 {
1586 TEST_ASSERT( ret == 0 );
1587 }
1588 }
1589
1590 /* ssl_1 reading */
1591 if( read_1 < msg_len_2 )
1592 {
1593 ret = mbedtls_ssl_read_fragment( ssl_1, in_buf_1,
1594 msg_len_2, &read_1,
1595 expected_fragments_1 );
1596 TEST_ASSERT( ret == 0 );
1597 }
1598
1599 /* ssl_2 reading */
1600 if( read_2 < msg_len_1 )
1601 {
1602 ret = mbedtls_ssl_read_fragment( ssl_2, in_buf_2,
1603 msg_len_1, &read_2,
1604 expected_fragments_2 );
1605 TEST_ASSERT( ret == 0 );
1606 }
1607 }
1608
1609 ret = -1;
1610 TEST_ASSERT( 0 == memcmp( msg_buf_1, in_buf_2, msg_len_1 ) );
1611 TEST_ASSERT( 0 == memcmp( msg_buf_2, in_buf_1, msg_len_2 ) );
1612 TEST_ASSERT( fragments_1 == expected_fragments_1 );
1613 TEST_ASSERT( fragments_2 == expected_fragments_2 );
1614 }
1615
1616 ret = 0;
1617
1618exit:
1619 free( msg_buf_1 );
1620 free( in_buf_1 );
1621 free( msg_buf_2 );
1622 free( in_buf_2 );
1623
1624 return ret;
1625}
1626
Piotr Nowicki95e9eb82020-02-14 11:33:34 +01001627/*
1628 * Perform data exchanging between \p ssl_1 and \p ssl_2. Both of endpoints
1629 * must be initialized and connected beforehand.
1630 *
1631 * \retval 0 on success, otherwise error code.
1632 */
1633int exchange_data( mbedtls_ssl_context *ssl_1,
1634 mbedtls_ssl_context *ssl_2 )
1635{
1636 return mbedtls_exchange_data( ssl_1, 256, 1,
1637 ssl_2, 256, 1 );
1638}
1639
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001640#if defined(MBEDTLS_X509_CRT_PARSE_C)
1641void perform_handshake( handshake_test_options* options )
1642{
1643 /* forced_ciphersuite needs to last until the end of the handshake */
1644 int forced_ciphersuite[2];
1645 enum { BUFFSIZE = 17000 };
1646 mbedtls_endpoint client, server;
1647#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1648 const char *psk_identity = "foo";
1649#endif
1650#if defined(MBEDTLS_TIMING_C)
1651 mbedtls_timing_delay_context timer_client, timer_server;
1652#endif
1653#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1654 unsigned char *context_buf = NULL;
1655 size_t context_buf_len;
1656#endif
1657#if defined(MBEDTLS_SSL_RENEGOTIATION)
1658 int ret = -1;
1659#endif
1660
1661
1662 mbedtls_test_message_queue server_queue, client_queue;
1663 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05001664 mbedtls_message_socket_init( &server_context );
1665 mbedtls_message_socket_init( &client_context );
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001666
1667 /* Client side */
1668 if( options->dtls != 0 )
1669 {
1670 TEST_ASSERT( mbedtls_endpoint_init( &client, MBEDTLS_SSL_IS_CLIENT,
1671 options->pk_alg, &client_context,
1672 &client_queue,
1673 &server_queue ) == 0 );
1674#if defined(MBEDTLS_TIMING_C)
1675 mbedtls_ssl_set_timer_cb( &client.ssl, &timer_client,
1676 mbedtls_timing_set_delay,
1677 mbedtls_timing_get_delay );
1678#endif
1679 }
1680 else
1681 {
1682 TEST_ASSERT( mbedtls_endpoint_init( &client, MBEDTLS_SSL_IS_CLIENT,
1683 options->pk_alg, NULL, NULL,
1684 NULL ) == 0 );
1685 }
1686 mbedtls_ssl_conf_min_version( &client.conf, MBEDTLS_SSL_MAJOR_VERSION_3,
1687 options->version );
1688 mbedtls_ssl_conf_max_version( &client.conf, MBEDTLS_SSL_MAJOR_VERSION_3,
1689 options->version );
1690
1691 if( strlen( options->cipher ) > 0 )
1692 {
1693 set_ciphersuite( &client.conf, options->cipher, forced_ciphersuite );
1694 }
Piotr Nowickibde7ee82020-02-21 10:59:50 +01001695
1696#if defined (MBEDTLS_DEBUG_C)
1697 if( options->cli_log_fun )
1698 {
1699 mbedtls_debug_set_threshold( 4 );
1700 mbedtls_ssl_conf_dbg( &client.conf, options->cli_log_fun,
1701 options->cli_log_obj );
1702 }
1703#endif
1704
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001705 /* Server side */
1706 if( options->dtls != 0 )
1707 {
1708 TEST_ASSERT( mbedtls_endpoint_init( &server, MBEDTLS_SSL_IS_SERVER,
1709 options->pk_alg, &server_context,
1710 &server_queue,
1711 &client_queue) == 0 );
1712#if defined(MBEDTLS_TIMING_C)
1713 mbedtls_ssl_set_timer_cb( &server.ssl, &timer_server,
1714 mbedtls_timing_set_delay,
1715 mbedtls_timing_get_delay );
1716#endif
1717 }
1718 else
1719 {
1720 TEST_ASSERT( mbedtls_endpoint_init( &server, MBEDTLS_SSL_IS_SERVER,
1721 options->pk_alg, NULL, NULL, NULL ) == 0 );
1722 }
Piotr Nowickibde7ee82020-02-21 10:59:50 +01001723
1724 mbedtls_ssl_conf_authmode( &server.conf, options->srv_auth_mode );
1725
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001726 mbedtls_ssl_conf_min_version( &server.conf, MBEDTLS_SSL_MAJOR_VERSION_3,
1727 options->version );
1728#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1729 TEST_ASSERT( mbedtls_ssl_conf_max_frag_len( &(server.conf),
1730 (unsigned char) options->mfl ) == 0 );
1731 TEST_ASSERT( mbedtls_ssl_conf_max_frag_len( &(client.conf),
1732 (unsigned char) options->mfl ) == 0 );
1733#else
1734 TEST_ASSERT( MBEDTLS_SSL_MAX_FRAG_LEN_NONE == options->mfl );
1735#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
1736
1737#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1738 if( options->psk_str != NULL && options->psk_str->len > 0 )
1739 {
1740 TEST_ASSERT( mbedtls_ssl_conf_psk( &client.conf, options->psk_str->x,
1741 options->psk_str->len,
1742 (const unsigned char *) psk_identity,
1743 strlen( psk_identity ) ) == 0 );
1744
1745 TEST_ASSERT( mbedtls_ssl_conf_psk( &server.conf, options->psk_str->x,
1746 options->psk_str->len,
1747 (const unsigned char *) psk_identity,
1748 strlen( psk_identity ) ) == 0 );
1749
1750 mbedtls_ssl_conf_psk_cb( &server.conf, psk_dummy_callback, NULL );
1751 }
1752#endif
1753#if defined(MBEDTLS_SSL_RENEGOTIATION)
1754 if( options->renegotiate )
1755 {
1756 mbedtls_ssl_conf_renegotiation( &(server.conf),
1757 MBEDTLS_SSL_RENEGOTIATION_ENABLED );
1758 mbedtls_ssl_conf_renegotiation( &(client.conf),
1759 MBEDTLS_SSL_RENEGOTIATION_ENABLED );
1760
1761 mbedtls_ssl_conf_legacy_renegotiation( &(server.conf),
1762 options->legacy_renegotiation );
1763 mbedtls_ssl_conf_legacy_renegotiation( &(client.conf),
1764 options->legacy_renegotiation );
1765 }
1766#endif /* MBEDTLS_SSL_RENEGOTIATION */
1767
Piotr Nowickibde7ee82020-02-21 10:59:50 +01001768#if defined (MBEDTLS_DEBUG_C)
1769 if( options->srv_log_fun )
1770 {
1771 mbedtls_debug_set_threshold( 4 );
1772 mbedtls_ssl_conf_dbg( &server.conf, options->srv_log_fun,
1773 options->srv_log_obj );
1774 }
1775#endif
1776
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001777 TEST_ASSERT( mbedtls_mock_socket_connect( &(client.socket),
1778 &(server.socket),
1779 BUFFSIZE ) == 0 );
1780
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001781#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1782 if( options->resize_buffers != 0 )
1783 {
1784 /* Ensure that the buffer sizes are appropriate before resizes */
1785 TEST_ASSERT( client.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN );
1786 TEST_ASSERT( client.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN );
1787 TEST_ASSERT( server.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN );
1788 TEST_ASSERT( server.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN );
1789 }
1790#endif
1791
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001792 TEST_ASSERT( mbedtls_move_handshake_to_state( &(client.ssl),
1793 &(server.ssl),
1794 MBEDTLS_SSL_HANDSHAKE_OVER )
1795 == 0 );
1796 TEST_ASSERT( client.ssl.state == MBEDTLS_SSL_HANDSHAKE_OVER );
1797 TEST_ASSERT( server.ssl.state == MBEDTLS_SSL_HANDSHAKE_OVER );
1798
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001799#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1800 if( options->resize_buffers != 0 )
1801 {
1802 /* Note - the case below will have to updated, since due to a 1n-1
1803 * split against BEAST the fragment count is different
1804 * than expected when preparing the fragment counting code. */
1805 if( options->version != MBEDTLS_SSL_MINOR_VERSION_0 &&
1806 options->version != MBEDTLS_SSL_MINOR_VERSION_1 )
1807 {
1808 /* A server, when using DTLS, might delay a buffer resize to happen
1809 * after it receives a message, so we force it. */
1810 TEST_ASSERT( exchange_data( &(client.ssl), &(server.ssl) ) == 0 );
1811
1812 TEST_ASSERT( client.ssl.out_buf_len ==
1813 mbedtls_ssl_get_output_buflen( &client.ssl ) );
1814 TEST_ASSERT( client.ssl.in_buf_len ==
1815 mbedtls_ssl_get_input_buflen( &client.ssl ) );
1816 TEST_ASSERT( server.ssl.out_buf_len ==
1817 mbedtls_ssl_get_output_buflen( &server.ssl ) );
1818 TEST_ASSERT( server.ssl.in_buf_len ==
1819 mbedtls_ssl_get_input_buflen( &server.ssl ) );
1820 }
1821 }
1822#endif
1823
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001824 if( options->cli_msg_len != 0 || options->srv_msg_len != 0 )
1825 {
1826 /* Start data exchanging test */
1827 TEST_ASSERT( mbedtls_exchange_data( &(client.ssl), options->cli_msg_len,
1828 options->expected_cli_fragments,
1829 &(server.ssl), options->srv_msg_len,
1830 options->expected_srv_fragments )
1831 == 0 );
1832 }
1833#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1834 if( options->serialize == 1 )
1835 {
1836 TEST_ASSERT( options->dtls == 1 );
1837
1838 TEST_ASSERT( mbedtls_ssl_context_save( &(server.ssl), NULL,
1839 0, &context_buf_len )
1840 == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1841
1842 context_buf = mbedtls_calloc( 1, context_buf_len );
1843 TEST_ASSERT( context_buf != NULL );
1844
1845 TEST_ASSERT( mbedtls_ssl_context_save( &(server.ssl), context_buf,
1846 context_buf_len,
1847 &context_buf_len ) == 0 );
1848
1849 mbedtls_ssl_free( &(server.ssl) );
1850 mbedtls_ssl_init( &(server.ssl) );
1851
1852 TEST_ASSERT( mbedtls_ssl_setup( &(server.ssl), &(server.conf) ) == 0 );
1853
1854 mbedtls_ssl_set_bio( &( server.ssl ), &server_context,
1855 mbedtls_mock_tcp_send_msg,
1856 mbedtls_mock_tcp_recv_msg,
1857 NULL );
1858
1859#if defined(MBEDTLS_TIMING_C)
1860 mbedtls_ssl_set_timer_cb( &server.ssl, &timer_server,
1861 mbedtls_timing_set_delay,
1862 mbedtls_timing_get_delay );
1863#endif
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001864#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1865 if( options->resize_buffers != 0 )
1866 {
1867 /* Ensure that the buffer sizes are appropriate before resizes */
1868 TEST_ASSERT( server.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN );
1869 TEST_ASSERT( server.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN );
1870 }
1871#endif
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001872 TEST_ASSERT( mbedtls_ssl_context_load( &( server.ssl ), context_buf,
1873 context_buf_len ) == 0 );
1874
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001875#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1876 /* Validate buffer sizes after context deserialization */
1877 if( options->resize_buffers != 0 )
1878 {
1879 TEST_ASSERT( server.ssl.out_buf_len ==
1880 mbedtls_ssl_get_output_buflen( &server.ssl ) );
1881 TEST_ASSERT( server.ssl.in_buf_len ==
1882 mbedtls_ssl_get_input_buflen( &server.ssl ) );
1883 }
1884#endif
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001885 /* Retest writing/reading */
1886 if( options->cli_msg_len != 0 || options->srv_msg_len != 0 )
1887 {
1888 TEST_ASSERT( mbedtls_exchange_data( &(client.ssl),
1889 options->cli_msg_len,
1890 options->expected_cli_fragments,
1891 &(server.ssl),
1892 options->srv_msg_len,
1893 options->expected_srv_fragments )
1894 == 0 );
1895 }
1896 }
1897#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001898
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001899#if defined(MBEDTLS_SSL_RENEGOTIATION)
1900 if( options->renegotiate )
1901 {
1902 /* Start test with renegotiation */
1903 TEST_ASSERT( server.ssl.renego_status ==
1904 MBEDTLS_SSL_INITIAL_HANDSHAKE );
1905 TEST_ASSERT( client.ssl.renego_status ==
1906 MBEDTLS_SSL_INITIAL_HANDSHAKE );
1907
1908 /* After calling this function for the server, it only sends a handshake
1909 * request. All renegotiation should happen during data exchanging */
1910 TEST_ASSERT( mbedtls_ssl_renegotiate( &(server.ssl) ) == 0 );
1911 TEST_ASSERT( server.ssl.renego_status ==
1912 MBEDTLS_SSL_RENEGOTIATION_PENDING );
1913 TEST_ASSERT( client.ssl.renego_status ==
1914 MBEDTLS_SSL_INITIAL_HANDSHAKE );
1915
1916 TEST_ASSERT( exchange_data( &(client.ssl), &(server.ssl) ) == 0 );
1917 TEST_ASSERT( server.ssl.renego_status ==
1918 MBEDTLS_SSL_RENEGOTIATION_DONE );
1919 TEST_ASSERT( client.ssl.renego_status ==
1920 MBEDTLS_SSL_RENEGOTIATION_DONE );
1921
1922 /* After calling mbedtls_ssl_renegotiate for the client all renegotiation
1923 * should happen inside this function. However in this test, we cannot
1924 * perform simultaneous communication betwen client and server so this
1925 * function will return waiting error on the socket. All rest of
1926 * renegotiation should happen during data exchanging */
1927 ret = mbedtls_ssl_renegotiate( &(client.ssl) );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001928#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1929 if( options->resize_buffers != 0 )
1930 {
1931 /* Ensure that the buffer sizes are appropriate before resizes */
1932 TEST_ASSERT( client.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN );
1933 TEST_ASSERT( client.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN );
1934 }
1935#endif
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001936 TEST_ASSERT( ret == 0 ||
1937 ret == MBEDTLS_ERR_SSL_WANT_READ ||
1938 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
1939 TEST_ASSERT( server.ssl.renego_status ==
1940 MBEDTLS_SSL_RENEGOTIATION_DONE );
1941 TEST_ASSERT( client.ssl.renego_status ==
1942 MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS );
1943
1944 TEST_ASSERT( exchange_data( &(client.ssl), &(server.ssl) ) == 0 );
1945 TEST_ASSERT( server.ssl.renego_status ==
1946 MBEDTLS_SSL_RENEGOTIATION_DONE );
1947 TEST_ASSERT( client.ssl.renego_status ==
1948 MBEDTLS_SSL_RENEGOTIATION_DONE );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001949#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1950 /* Validate buffer sizes after renegotiation */
1951 if( options->resize_buffers != 0 )
1952 {
1953 TEST_ASSERT( client.ssl.out_buf_len ==
1954 mbedtls_ssl_get_output_buflen( &client.ssl ) );
1955 TEST_ASSERT( client.ssl.in_buf_len ==
1956 mbedtls_ssl_get_input_buflen( &client.ssl ) );
1957 TEST_ASSERT( server.ssl.out_buf_len ==
1958 mbedtls_ssl_get_output_buflen( &server.ssl ) );
1959 TEST_ASSERT( server.ssl.in_buf_len ==
1960 mbedtls_ssl_get_input_buflen( &server.ssl ) );
1961 }
1962#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001963 }
1964#endif /* MBEDTLS_SSL_RENEGOTIATION */
1965
1966exit:
1967 mbedtls_endpoint_free( &client, options->dtls != 0 ? &client_context : NULL );
1968 mbedtls_endpoint_free( &server, options->dtls != 0 ? &server_context : NULL );
Piotr Nowickibde7ee82020-02-21 10:59:50 +01001969#if defined (MBEDTLS_DEBUG_C)
1970 if( options->cli_log_fun || options->srv_log_fun )
1971 {
1972 mbedtls_debug_set_threshold( 0 );
1973 }
1974#endif
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001975#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1976 if( context_buf != NULL )
1977 mbedtls_free( context_buf );
1978#endif
1979}
1980#endif /* MBEDTLS_X509_CRT_PARSE_C */
1981
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02001982/* END_HEADER */
1983
1984/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001985 * depends_on:MBEDTLS_SSL_TLS_C
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02001986 * END_DEPENDENCIES
1987 */
1988
Janos Follath6264e662019-11-26 11:11:15 +00001989/* BEGIN_CASE */
1990void test_callback_buffer_sanity()
1991{
1992 enum { MSGLEN = 10 };
1993 mbedtls_test_buffer buf;
1994 unsigned char input[MSGLEN];
1995 unsigned char output[MSGLEN];
1996
1997 memset( input, 0, sizeof(input) );
1998
1999 /* Make sure calling put and get on NULL buffer results in error. */
2000 TEST_ASSERT( mbedtls_test_buffer_put( NULL, input, sizeof( input ) )
2001 == -1 );
2002 TEST_ASSERT( mbedtls_test_buffer_get( NULL, output, sizeof( output ) )
2003 == -1 );
2004 TEST_ASSERT( mbedtls_test_buffer_put( NULL, NULL, sizeof( input ) ) == -1 );
Andrzej Kurekf7774142020-01-22 06:34:59 -05002005
Janos Follath6264e662019-11-26 11:11:15 +00002006 TEST_ASSERT( mbedtls_test_buffer_put( NULL, NULL, 0 ) == -1 );
2007 TEST_ASSERT( mbedtls_test_buffer_get( NULL, NULL, 0 ) == -1 );
2008
2009 /* Make sure calling put and get on a buffer that hasn't been set up results
2010 * in eror. */
2011 mbedtls_test_buffer_init( &buf );
2012
2013 TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, sizeof( input ) ) == -1 );
2014 TEST_ASSERT( mbedtls_test_buffer_get( &buf, output, sizeof( output ) )
2015 == -1 );
2016 TEST_ASSERT( mbedtls_test_buffer_put( &buf, NULL, sizeof( input ) ) == -1 );
Andrzej Kurekf7774142020-01-22 06:34:59 -05002017
Janos Follath6264e662019-11-26 11:11:15 +00002018 TEST_ASSERT( mbedtls_test_buffer_put( &buf, NULL, 0 ) == -1 );
2019 TEST_ASSERT( mbedtls_test_buffer_get( &buf, NULL, 0 ) == -1 );
2020
Andrzej Kurekf7774142020-01-22 06:34:59 -05002021 /* Make sure calling put and get on NULL input only results in
2022 * error if the length is not zero, and that a NULL output is valid for data
2023 * dropping.
2024 */
Janos Follath6264e662019-11-26 11:11:15 +00002025
2026 TEST_ASSERT( mbedtls_test_buffer_setup( &buf, sizeof( input ) ) == 0 );
2027
2028 TEST_ASSERT( mbedtls_test_buffer_put( &buf, NULL, sizeof( input ) ) == -1 );
2029 TEST_ASSERT( mbedtls_test_buffer_get( &buf, NULL, sizeof( output ) )
Andrzej Kurekf7774142020-01-22 06:34:59 -05002030 == 0 );
Janos Follath6264e662019-11-26 11:11:15 +00002031 TEST_ASSERT( mbedtls_test_buffer_put( &buf, NULL, 0 ) == 0 );
2032 TEST_ASSERT( mbedtls_test_buffer_get( &buf, NULL, 0 ) == 0 );
2033
Piotr Nowickifb437d72020-01-13 16:59:12 +01002034 /* Make sure calling put several times in the row is safe */
2035
2036 TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, sizeof( input ) )
2037 == sizeof( input ) );
2038 TEST_ASSERT( mbedtls_test_buffer_get( &buf, output, 2 ) == 2 );
2039 TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, 1 ) == 1 );
2040 TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, 2 ) == 1 );
2041 TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, 2 ) == 0 );
2042
2043
Janos Follath6264e662019-11-26 11:11:15 +00002044exit:
2045
2046 mbedtls_test_buffer_free( &buf );
2047}
2048/* END_CASE */
2049
2050/*
2051 * Test if the implementation of `mbedtls_test_buffer` related functions is
2052 * correct and works as expected.
2053 *
2054 * That is
2055 * - If we try to put in \p put1 bytes then we can put in \p put1_ret bytes.
2056 * - Afterwards if we try to get \p get1 bytes then we can get \get1_ret bytes.
2057 * - Next, if we try to put in \p put1 bytes then we can put in \p put1_ret
2058 * bytes.
2059 * - Afterwards if we try to get \p get1 bytes then we can get \get1_ret bytes.
2060 * - All of the bytes we got match the bytes we put in in a FIFO manner.
2061 */
2062
2063/* BEGIN_CASE */
2064void test_callback_buffer( int size, int put1, int put1_ret,
2065 int get1, int get1_ret, int put2, int put2_ret,
2066 int get2, int get2_ret )
2067{
2068 enum { ROUNDS = 2 };
2069 size_t put[ROUNDS];
2070 int put_ret[ROUNDS];
2071 size_t get[ROUNDS];
2072 int get_ret[ROUNDS];
2073 mbedtls_test_buffer buf;
2074 unsigned char* input = NULL;
2075 size_t input_len;
2076 unsigned char* output = NULL;
2077 size_t output_len;
Janos Follath031827f2019-11-27 11:12:14 +00002078 size_t i, j, written, read;
Janos Follath6264e662019-11-26 11:11:15 +00002079
2080 mbedtls_test_buffer_init( &buf );
2081 TEST_ASSERT( mbedtls_test_buffer_setup( &buf, size ) == 0 );
2082
2083 /* Check the sanity of input parameters and initialise local variables. That
2084 * is, ensure that the amount of data is not negative and that we are not
2085 * expecting more to put or get than we actually asked for. */
2086 TEST_ASSERT( put1 >= 0 );
2087 put[0] = put1;
2088 put_ret[0] = put1_ret;
2089 TEST_ASSERT( put1_ret <= put1 );
2090 TEST_ASSERT( put2 >= 0 );
2091 put[1] = put2;
2092 put_ret[1] = put2_ret;
2093 TEST_ASSERT( put2_ret <= put2 );
2094
2095 TEST_ASSERT( get1 >= 0 );
2096 get[0] = get1;
2097 get_ret[0] = get1_ret;
2098 TEST_ASSERT( get1_ret <= get1 );
2099 TEST_ASSERT( get2 >= 0 );
2100 get[1] = get2;
2101 get_ret[1] = get2_ret;
2102 TEST_ASSERT( get2_ret <= get2 );
2103
2104 input_len = 0;
2105 /* Calculate actual input and output lengths */
2106 for( j = 0; j < ROUNDS; j++ )
2107 {
2108 if( put_ret[j] > 0 )
2109 {
2110 input_len += put_ret[j];
2111 }
2112 }
2113 /* In order to always have a valid pointer we always allocate at least 1
2114 * byte. */
2115 if( input_len == 0 )
2116 input_len = 1;
2117 ASSERT_ALLOC( input, input_len );
2118
2119 output_len = 0;
2120 for( j = 0; j < ROUNDS; j++ )
2121 {
2122 if( get_ret[j] > 0 )
2123 {
2124 output_len += get_ret[j];
2125 }
2126 }
2127 TEST_ASSERT( output_len <= input_len );
2128 /* In order to always have a valid pointer we always allocate at least 1
2129 * byte. */
2130 if( output_len == 0 )
2131 output_len = 1;
2132 ASSERT_ALLOC( output, output_len );
2133
2134 /* Fill up the buffer with structured data so that unwanted changes
2135 * can be detected */
2136 for( i = 0; i < input_len; i++ )
2137 {
2138 input[i] = i & 0xFF;
2139 }
2140
2141 written = read = 0;
2142 for( j = 0; j < ROUNDS; j++ )
2143 {
2144 TEST_ASSERT( put_ret[j] == mbedtls_test_buffer_put( &buf,
2145 input + written, put[j] ) );
2146 written += put_ret[j];
2147 TEST_ASSERT( get_ret[j] == mbedtls_test_buffer_get( &buf,
2148 output + read, get[j] ) );
2149 read += get_ret[j];
2150 TEST_ASSERT( read <= written );
2151 if( get_ret[j] > 0 )
2152 {
2153 TEST_ASSERT( memcmp( output + read - get_ret[j],
2154 input + read - get_ret[j], get_ret[j] )
2155 == 0 );
2156 }
2157 }
2158
2159exit:
2160
2161 mbedtls_free( input );
2162 mbedtls_free( output );
2163 mbedtls_test_buffer_free( &buf );
2164}
2165/* END_CASE */
2166
Janos Follath031827f2019-11-27 11:12:14 +00002167/*
Janos Follathc673c2c2019-12-02 15:47:26 +00002168 * Test if the implementation of `mbedtls_mock_socket` related I/O functions is
2169 * correct and works as expected on unconnected sockets.
2170 */
2171
2172/* BEGIN_CASE */
2173void ssl_mock_sanity( )
2174{
2175 enum { MSGLEN = 105 };
2176 unsigned char message[MSGLEN];
2177 unsigned char received[MSGLEN];
2178 mbedtls_mock_socket socket;
2179
2180 mbedtls_mock_socket_init( &socket );
2181 TEST_ASSERT( mbedtls_mock_tcp_send_b( &socket, message, MSGLEN ) < 0 );
2182 mbedtls_mock_socket_close( &socket );
2183 mbedtls_mock_socket_init( &socket );
2184 TEST_ASSERT( mbedtls_mock_tcp_recv_b( &socket, received, MSGLEN ) < 0 );
2185 mbedtls_mock_socket_close( &socket );
2186
2187 mbedtls_mock_socket_init( &socket );
2188 TEST_ASSERT( mbedtls_mock_tcp_send_nb( &socket, message, MSGLEN ) < 0 );
2189 mbedtls_mock_socket_close( &socket );
2190 mbedtls_mock_socket_init( &socket );
2191 TEST_ASSERT( mbedtls_mock_tcp_recv_nb( &socket, received, MSGLEN ) < 0 );
2192 mbedtls_mock_socket_close( &socket );
2193
2194exit:
2195
2196 mbedtls_mock_socket_close( &socket );
2197}
2198/* END_CASE */
2199
2200/*
2201 * Test if the implementation of `mbedtls_mock_socket` related functions can
2202 * send a single message from the client to the server.
Janos Follath031827f2019-11-27 11:12:14 +00002203 */
2204
2205/* BEGIN_CASE */
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002206void ssl_mock_tcp( int blocking )
Janos Follath031827f2019-11-27 11:12:14 +00002207{
Janos Follathc673c2c2019-12-02 15:47:26 +00002208 enum { MSGLEN = 105 };
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002209 enum { BUFLEN = MSGLEN / 5 };
Janos Follathc673c2c2019-12-02 15:47:26 +00002210 unsigned char message[MSGLEN];
2211 unsigned char received[MSGLEN];
2212 mbedtls_mock_socket client;
2213 mbedtls_mock_socket server;
2214 size_t written, read;
2215 int send_ret, recv_ret;
2216 mbedtls_ssl_send_t *send;
2217 mbedtls_ssl_recv_t *recv;
Janos Follathc673c2c2019-12-02 15:47:26 +00002218 unsigned i;
2219
2220 if( blocking == 0 )
2221 {
2222 send = mbedtls_mock_tcp_send_nb;
2223 recv = mbedtls_mock_tcp_recv_nb;
2224 }
2225 else
2226 {
2227 send = mbedtls_mock_tcp_send_b;
2228 recv = mbedtls_mock_tcp_recv_b;
2229 }
2230
2231 mbedtls_mock_socket_init( &client );
2232 mbedtls_mock_socket_init( &server );
2233
2234 /* Fill up the buffer with structured data so that unwanted changes
2235 * can be detected */
2236 for( i = 0; i < MSGLEN; i++ )
2237 {
2238 message[i] = i & 0xFF;
2239 }
2240
2241 /* Make sure that sending a message takes a few iterations. */
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002242 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, BUFLEN ) );
Janos Follathc673c2c2019-12-02 15:47:26 +00002243
2244 /* Send the message to the server */
2245 send_ret = recv_ret = 1;
2246 written = read = 0;
2247 while( send_ret != 0 || recv_ret != 0 )
2248 {
2249 send_ret = send( &client, message + written, MSGLEN - written );
2250
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002251 TEST_ASSERT( send_ret >= 0 );
2252 TEST_ASSERT( send_ret <= BUFLEN );
2253 written += send_ret;
2254
2255 /* If the buffer is full we can test blocking and non-blocking send */
2256 if ( send_ret == BUFLEN )
Janos Follathc673c2c2019-12-02 15:47:26 +00002257 {
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002258 int blocking_ret = send( &client, message , 1 );
2259 if ( blocking )
2260 {
2261 TEST_ASSERT( blocking_ret == 0 );
2262 }
2263 else
2264 {
2265 TEST_ASSERT( blocking_ret == MBEDTLS_ERR_SSL_WANT_WRITE );
2266 }
Janos Follathc673c2c2019-12-02 15:47:26 +00002267 }
Janos Follathc673c2c2019-12-02 15:47:26 +00002268
2269 recv_ret = recv( &server, received + read, MSGLEN - read );
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002270
2271 /* The result depends on whether any data was sent */
2272 if ( send_ret > 0 )
Janos Follathc673c2c2019-12-02 15:47:26 +00002273 {
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002274 TEST_ASSERT( recv_ret > 0 );
2275 TEST_ASSERT( recv_ret <= BUFLEN );
2276 read += recv_ret;
2277 }
2278 else if( blocking )
2279 {
2280 TEST_ASSERT( recv_ret == 0 );
Janos Follathc673c2c2019-12-02 15:47:26 +00002281 }
2282 else
2283 {
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002284 TEST_ASSERT( recv_ret == MBEDTLS_ERR_SSL_WANT_READ );
2285 recv_ret = 0;
Janos Follathc673c2c2019-12-02 15:47:26 +00002286 }
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002287
2288 /* If the buffer is empty we can test blocking and non-blocking read */
2289 if ( recv_ret == BUFLEN )
2290 {
2291 int blocking_ret = recv( &server, received, 1 );
2292 if ( blocking )
2293 {
2294 TEST_ASSERT( blocking_ret == 0 );
2295 }
2296 else
2297 {
2298 TEST_ASSERT( blocking_ret == MBEDTLS_ERR_SSL_WANT_READ );
2299 }
2300 }
Janos Follathc673c2c2019-12-02 15:47:26 +00002301 }
2302 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
2303
2304exit:
2305
2306 mbedtls_mock_socket_close( &client );
2307 mbedtls_mock_socket_close( &server );
2308}
2309/* END_CASE */
2310
2311/*
2312 * Test if the implementation of `mbedtls_mock_socket` related functions can
2313 * send messages in both direction at the same time (with the I/O calls
2314 * interleaving).
2315 */
2316
2317/* BEGIN_CASE */
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002318void ssl_mock_tcp_interleaving( int blocking )
Janos Follathc673c2c2019-12-02 15:47:26 +00002319{
Janos Follath031827f2019-11-27 11:12:14 +00002320 enum { ROUNDS = 2 };
2321 enum { MSGLEN = 105 };
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002322 enum { BUFLEN = MSGLEN / 5 };
Janos Follath031827f2019-11-27 11:12:14 +00002323 unsigned char message[ROUNDS][MSGLEN];
2324 unsigned char received[ROUNDS][MSGLEN];
2325 mbedtls_mock_socket client;
2326 mbedtls_mock_socket server;
2327 size_t written[ROUNDS];
2328 size_t read[ROUNDS];
2329 int send_ret[ROUNDS];
2330 int recv_ret[ROUNDS];
2331 unsigned i, j, progress;
Janos Follath3766ba52019-11-27 13:31:42 +00002332 mbedtls_ssl_send_t *send;
2333 mbedtls_ssl_recv_t *recv;
Janos Follath3766ba52019-11-27 13:31:42 +00002334
2335 if( blocking == 0 )
2336 {
2337 send = mbedtls_mock_tcp_send_nb;
2338 recv = mbedtls_mock_tcp_recv_nb;
2339 }
2340 else
2341 {
2342 send = mbedtls_mock_tcp_send_b;
2343 recv = mbedtls_mock_tcp_recv_b;
2344 }
Janos Follath031827f2019-11-27 11:12:14 +00002345
2346 mbedtls_mock_socket_init( &client );
2347 mbedtls_mock_socket_init( &server );
2348
2349 /* Fill up the buffers with structured data so that unwanted changes
2350 * can be detected */
2351 for( i = 0; i < ROUNDS; i++ )
2352 {
2353 for( j = 0; j < MSGLEN; j++ )
2354 {
2355 message[i][j] = ( i * MSGLEN + j ) & 0xFF;
2356 }
2357 }
2358
Janos Follath031827f2019-11-27 11:12:14 +00002359 /* Make sure that sending a message takes a few iterations. */
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002360 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, BUFLEN ) );
Janos Follath031827f2019-11-27 11:12:14 +00002361
Janos Follath031827f2019-11-27 11:12:14 +00002362 /* Send the message from both sides, interleaving. */
2363 progress = 1;
2364 for( i = 0; i < ROUNDS; i++ )
2365 {
2366 written[i] = 0;
2367 read[i] = 0;
2368 }
2369 /* This loop does not stop as long as there was a successful write or read
2370 * of at least one byte on either side. */
2371 while( progress != 0 )
2372 {
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002373 mbedtls_mock_socket *socket;
Janos Follath031827f2019-11-27 11:12:14 +00002374
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002375 for( i = 0; i < ROUNDS; i++ )
Janos Follath3766ba52019-11-27 13:31:42 +00002376 {
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002377 /* First sending is from the client */
2378 socket = ( i % 2 == 0 ) ? ( &client ) : ( &server );
Janos Follath031827f2019-11-27 11:12:14 +00002379
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002380 send_ret[i] = send( socket, message[i] + written[i],
2381 MSGLEN - written[i] );
2382 TEST_ASSERT( send_ret[i] >= 0 );
2383 TEST_ASSERT( send_ret[i] <= BUFLEN );
2384 written[i] += send_ret[i];
Janos Follath031827f2019-11-27 11:12:14 +00002385
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002386 /* If the buffer is full we can test blocking and non-blocking
2387 * send */
2388 if ( send_ret[i] == BUFLEN )
2389 {
2390 int blocking_ret = send( socket, message[i] , 1 );
2391 if ( blocking )
2392 {
2393 TEST_ASSERT( blocking_ret == 0 );
2394 }
2395 else
2396 {
2397 TEST_ASSERT( blocking_ret == MBEDTLS_ERR_SSL_WANT_WRITE );
2398 }
2399 }
Janos Follath3766ba52019-11-27 13:31:42 +00002400 }
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002401
2402 for( i = 0; i < ROUNDS; i++ )
Janos Follath3766ba52019-11-27 13:31:42 +00002403 {
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002404 /* First receiving is from the server */
2405 socket = ( i % 2 == 0 ) ? ( &server ) : ( &client );
2406
2407 recv_ret[i] = recv( socket, received[i] + read[i],
2408 MSGLEN - read[i] );
2409
2410 /* The result depends on whether any data was sent */
2411 if ( send_ret[i] > 0 )
2412 {
2413 TEST_ASSERT( recv_ret[i] > 0 );
2414 TEST_ASSERT( recv_ret[i] <= BUFLEN );
2415 read[i] += recv_ret[i];
2416 }
2417 else if( blocking )
2418 {
2419 TEST_ASSERT( recv_ret[i] == 0 );
2420 }
2421 else
2422 {
2423 TEST_ASSERT( recv_ret[i] == MBEDTLS_ERR_SSL_WANT_READ );
2424 recv_ret[i] = 0;
2425 }
2426
2427 /* If the buffer is empty we can test blocking and non-blocking
2428 * read */
2429 if ( recv_ret[i] == BUFLEN )
2430 {
2431 int blocking_ret = recv( socket, received[i], 1 );
2432 if ( blocking )
2433 {
2434 TEST_ASSERT( blocking_ret == 0 );
2435 }
2436 else
2437 {
2438 TEST_ASSERT( blocking_ret == MBEDTLS_ERR_SSL_WANT_READ );
2439 }
2440 }
Janos Follath3766ba52019-11-27 13:31:42 +00002441 }
Janos Follath031827f2019-11-27 11:12:14 +00002442
2443 progress = 0;
2444 for( i = 0; i < ROUNDS; i++ )
2445 {
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002446 progress += send_ret[i] + recv_ret[i];
Janos Follath031827f2019-11-27 11:12:14 +00002447 }
2448 }
2449
2450 for( i = 0; i < ROUNDS; i++ )
2451 TEST_ASSERT( memcmp( message[i], received[i], MSGLEN ) == 0 );
2452
2453exit:
2454
2455 mbedtls_mock_socket_close( &client );
2456 mbedtls_mock_socket_close( &server );
2457}
2458/* END_CASE */
2459
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002460/* BEGIN_CASE */
2461void ssl_message_queue_sanity( )
2462{
2463 mbedtls_test_message_queue queue;
2464
2465 /* Trying to push/pull to an empty queue */
2466 TEST_ASSERT( mbedtls_test_message_queue_push_info( NULL, 1 )
2467 == MBEDTLS_TEST_ERROR_ARG_NULL );
2468 TEST_ASSERT( mbedtls_test_message_queue_pop_info( NULL, 1 )
2469 == MBEDTLS_TEST_ERROR_ARG_NULL );
2470
Andrzej Kurek89bdc582020-03-09 06:29:43 -04002471 TEST_ASSERT( mbedtls_test_message_queue_setup( &queue, 3 ) == 0 );
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002472 TEST_ASSERT( queue.capacity == 3 );
2473 TEST_ASSERT( queue.num == 0 );
2474
2475exit:
2476 mbedtls_test_message_queue_free( &queue );
2477}
2478/* END_CASE */
2479
2480/* BEGIN_CASE */
2481void ssl_message_queue_basic( )
2482{
2483 mbedtls_test_message_queue queue;
2484
Andrzej Kurek89bdc582020-03-09 06:29:43 -04002485 TEST_ASSERT( mbedtls_test_message_queue_setup( &queue, 3 ) == 0 );
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002486
2487 /* Sanity test - 3 pushes and 3 pops with sufficient space */
2488 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 );
2489 TEST_ASSERT( queue.capacity == 3 );
2490 TEST_ASSERT( queue.num == 1 );
2491 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 );
2492 TEST_ASSERT( queue.capacity == 3 );
2493 TEST_ASSERT( queue.num == 2 );
2494 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 2 ) == 2 );
2495 TEST_ASSERT( queue.capacity == 3 );
2496 TEST_ASSERT( queue.num == 3 );
2497
2498 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 );
2499 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 );
2500 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 2 ) == 2 );
2501
2502exit:
2503 mbedtls_test_message_queue_free( &queue );
2504}
2505/* END_CASE */
2506
2507/* BEGIN_CASE */
2508void ssl_message_queue_overflow_underflow( )
2509{
2510 mbedtls_test_message_queue queue;
2511
Andrzej Kurek89bdc582020-03-09 06:29:43 -04002512 TEST_ASSERT( mbedtls_test_message_queue_setup( &queue, 3 ) == 0 );
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002513
2514 /* 4 pushes (last one with an error), 4 pops (last one with an error) */
2515 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 );
2516 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 );
2517 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 2 ) == 2 );
2518 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 3 )
Andrzej Kurekf46b9122020-02-07 08:19:00 -05002519 == MBEDTLS_ERR_SSL_WANT_WRITE );
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002520
2521 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 );
2522 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 );
2523 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 2 ) == 2 );
2524
2525 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 )
Andrzej Kurekf46b9122020-02-07 08:19:00 -05002526 == MBEDTLS_ERR_SSL_WANT_READ );
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002527
2528exit:
2529 mbedtls_test_message_queue_free( &queue );
2530}
2531/* END_CASE */
2532
2533/* BEGIN_CASE */
2534void ssl_message_queue_interleaved( )
2535{
2536 mbedtls_test_message_queue queue;
2537
Andrzej Kurek89bdc582020-03-09 06:29:43 -04002538 TEST_ASSERT( mbedtls_test_message_queue_setup( &queue, 3 ) == 0 );
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002539
2540 /* Interleaved test - [2 pushes, 1 pop] twice, and then two pops
2541 * (to wrap around the buffer) */
2542 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 );
2543 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 );
2544
2545 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 );
2546
2547 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 2 ) == 2 );
2548 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 3 ) == 3 );
2549
2550 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 );
2551 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 2 ) == 2 );
2552
2553 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 5 ) == 5 );
2554 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 8 ) == 8 );
2555
2556 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 3 ) == 3 );
2557
2558 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 5 ) == 5 );
2559
2560 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 8 ) == 8 );
2561
2562exit:
2563 mbedtls_test_message_queue_free( &queue );
2564}
2565/* END_CASE */
2566
2567/* BEGIN_CASE */
2568void ssl_message_queue_insufficient_buffer( )
2569{
2570 mbedtls_test_message_queue queue;
2571 size_t message_len = 10;
2572 size_t buffer_len = 5;
2573
Andrzej Kurek89bdc582020-03-09 06:29:43 -04002574 TEST_ASSERT( mbedtls_test_message_queue_setup( &queue, 1 ) == 0 );
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002575
2576 /* Popping without a sufficient buffer */
2577 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, message_len )
2578 == (int) message_len );
2579 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, buffer_len )
2580 == (int) buffer_len );
2581exit:
2582 mbedtls_test_message_queue_free( &queue );
2583}
2584/* END_CASE */
2585
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002586/* BEGIN_CASE */
2587void ssl_message_mock_uninitialized( )
2588{
2589 enum { MSGLEN = 10 };
2590 unsigned char message[MSGLEN], received[MSGLEN];
2591 mbedtls_mock_socket client, server;
2592 mbedtls_test_message_queue server_queue, client_queue;
2593 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05002594 mbedtls_message_socket_init( &server_context );
2595 mbedtls_message_socket_init( &client_context );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002596
2597 /* Send with a NULL context */
2598 TEST_ASSERT( mbedtls_mock_tcp_send_msg( NULL, message, MSGLEN )
2599 == MBEDTLS_TEST_ERROR_CONTEXT_ERROR );
2600
2601 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( NULL, message, MSGLEN )
2602 == MBEDTLS_TEST_ERROR_CONTEXT_ERROR );
2603
2604 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 1,
2605 &server,
2606 &server_context ) == 0 );
2607
2608 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 1,
2609 &client,
2610 &client_context ) == 0 );
2611
2612 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, MSGLEN )
2613 == MBEDTLS_TEST_ERROR_SEND_FAILED );
2614
2615 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
Andrzej Kurekf46b9122020-02-07 08:19:00 -05002616 == MBEDTLS_ERR_SSL_WANT_READ );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002617
2618 /* Push directly to a queue to later simulate a disconnected behavior */
2619 TEST_ASSERT( mbedtls_test_message_queue_push_info( &server_queue, MSGLEN )
2620 == MSGLEN );
2621
2622 /* Test if there's an error when trying to read from a disconnected
2623 * socket */
2624 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
2625 == MBEDTLS_TEST_ERROR_RECV_FAILED );
2626 exit:
2627 mbedtls_message_socket_close( &server_context );
2628 mbedtls_message_socket_close( &client_context );
2629}
2630/* END_CASE */
2631
2632/* BEGIN_CASE */
2633void ssl_message_mock_basic( )
2634{
2635 enum { MSGLEN = 10 };
2636 unsigned char message[MSGLEN], received[MSGLEN];
2637 mbedtls_mock_socket client, server;
2638 unsigned i;
2639 mbedtls_test_message_queue server_queue, client_queue;
2640 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05002641 mbedtls_message_socket_init( &server_context );
2642 mbedtls_message_socket_init( &client_context );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002643
2644 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 1,
2645 &server,
2646 &server_context ) == 0 );
2647
2648 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 1,
2649 &client,
2650 &client_context ) == 0 );
2651
2652 /* Fill up the buffer with structured data so that unwanted changes
2653 * can be detected */
2654 for( i = 0; i < MSGLEN; i++ )
2655 {
2656 message[i] = i & 0xFF;
2657 }
2658 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
2659 MSGLEN ) );
2660
2661 /* Send the message to the server */
2662 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2663 MSGLEN ) == MSGLEN );
2664
2665 /* Read from the server */
2666 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
2667 == MSGLEN );
2668
2669 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
2670 memset( received, 0, MSGLEN );
2671
2672 /* Send the message to the client */
2673 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &server_context, message,
2674 MSGLEN ) == MSGLEN );
2675
2676 /* Read from the client */
2677 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &client_context, received, MSGLEN )
2678 == MSGLEN );
2679 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
2680
2681 exit:
2682 mbedtls_message_socket_close( &server_context );
2683 mbedtls_message_socket_close( &client_context );
2684}
2685/* END_CASE */
2686
2687/* BEGIN_CASE */
2688void ssl_message_mock_queue_overflow_underflow( )
2689{
2690 enum { MSGLEN = 10 };
2691 unsigned char message[MSGLEN], received[MSGLEN];
2692 mbedtls_mock_socket client, server;
2693 unsigned i;
2694 mbedtls_test_message_queue server_queue, client_queue;
2695 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05002696 mbedtls_message_socket_init( &server_context );
2697 mbedtls_message_socket_init( &client_context );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002698
2699 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 2,
2700 &server,
2701 &server_context ) == 0 );
2702
2703 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 2,
2704 &client,
2705 &client_context ) == 0 );
2706
2707 /* Fill up the buffer with structured data so that unwanted changes
2708 * can be detected */
2709 for( i = 0; i < MSGLEN; i++ )
2710 {
2711 message[i] = i & 0xFF;
2712 }
2713 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
2714 MSGLEN*2 ) );
2715
2716 /* Send three message to the server, last one with an error */
2717 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2718 MSGLEN - 1 ) == MSGLEN - 1 );
2719
2720 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2721 MSGLEN ) == MSGLEN );
2722
2723 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2724 MSGLEN )
Andrzej Kurekf46b9122020-02-07 08:19:00 -05002725 == MBEDTLS_ERR_SSL_WANT_WRITE );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002726
2727 /* Read three messages from the server, last one with an error */
2728 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received,
2729 MSGLEN - 1 ) == MSGLEN - 1 );
2730
2731 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
2732 == MSGLEN );
2733
2734 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
2735
2736 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
Andrzej Kurekf46b9122020-02-07 08:19:00 -05002737 == MBEDTLS_ERR_SSL_WANT_READ );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002738
2739 exit:
2740 mbedtls_message_socket_close( &server_context );
2741 mbedtls_message_socket_close( &client_context );
2742}
2743/* END_CASE */
2744
2745/* BEGIN_CASE */
2746void ssl_message_mock_socket_overflow( )
2747{
2748 enum { MSGLEN = 10 };
2749 unsigned char message[MSGLEN], received[MSGLEN];
2750 mbedtls_mock_socket client, server;
2751 unsigned i;
2752 mbedtls_test_message_queue server_queue, client_queue;
2753 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05002754 mbedtls_message_socket_init( &server_context );
2755 mbedtls_message_socket_init( &client_context );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002756
2757 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 2,
2758 &server,
2759 &server_context ) == 0 );
2760
2761 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 2,
2762 &client,
2763 &client_context ) == 0 );
2764
2765 /* Fill up the buffer with structured data so that unwanted changes
2766 * can be detected */
2767 for( i = 0; i < MSGLEN; i++ )
2768 {
2769 message[i] = i & 0xFF;
2770 }
2771 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
2772 MSGLEN ) );
2773
2774 /* Send two message to the server, second one with an error */
2775 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2776 MSGLEN ) == MSGLEN );
2777
2778 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2779 MSGLEN )
2780 == MBEDTLS_TEST_ERROR_SEND_FAILED );
2781
2782 /* Read the only message from the server */
2783 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
2784 == MSGLEN );
2785
2786 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
2787
2788 exit:
2789 mbedtls_message_socket_close( &server_context );
2790 mbedtls_message_socket_close( &client_context );
2791}
2792/* END_CASE */
2793
2794/* BEGIN_CASE */
2795void ssl_message_mock_truncated( )
2796{
2797 enum { MSGLEN = 10 };
2798 unsigned char message[MSGLEN], received[MSGLEN];
2799 mbedtls_mock_socket client, server;
2800 unsigned i;
2801 mbedtls_test_message_queue server_queue, client_queue;
2802 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05002803 mbedtls_message_socket_init( &server_context );
2804 mbedtls_message_socket_init( &client_context );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002805
2806 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 2,
2807 &server,
2808 &server_context ) == 0 );
2809
2810 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 2,
2811 &client,
2812 &client_context ) == 0 );
2813
2814 memset( received, 0, MSGLEN );
2815 /* Fill up the buffer with structured data so that unwanted changes
2816 * can be detected */
2817 for( i = 0; i < MSGLEN; i++ )
2818 {
2819 message[i] = i & 0xFF;
2820 }
2821 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
2822 2 * MSGLEN ) );
2823
2824 /* Send two messages to the server, the second one small enough to fit in the
2825 * receiver's buffer. */
2826 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2827 MSGLEN ) == MSGLEN );
2828 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2829 MSGLEN / 2 ) == MSGLEN / 2 );
2830 /* Read a truncated message from the server */
2831 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN/2 )
2832 == MSGLEN/2 );
2833
2834 /* Test that the first half of the message is valid, and second one isn't */
2835 TEST_ASSERT( memcmp( message, received, MSGLEN/2 ) == 0 );
2836 TEST_ASSERT( memcmp( message + MSGLEN/2, received + MSGLEN/2, MSGLEN/2 )
2837 != 0 );
2838 memset( received, 0, MSGLEN );
2839
2840 /* Read a full message from the server */
2841 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN/2 )
2842 == MSGLEN / 2 );
2843
2844 /* Test that the first half of the message is valid */
2845 TEST_ASSERT( memcmp( message, received, MSGLEN/2 ) == 0 );
2846
2847 exit:
2848 mbedtls_message_socket_close( &server_context );
2849 mbedtls_message_socket_close( &client_context );
2850}
2851/* END_CASE */
2852
2853/* BEGIN_CASE */
2854void ssl_message_mock_socket_read_error( )
2855{
2856 enum { MSGLEN = 10 };
2857 unsigned char message[MSGLEN], received[MSGLEN];
2858 mbedtls_mock_socket client, server;
2859 unsigned i;
2860 mbedtls_test_message_queue server_queue, client_queue;
2861 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05002862 mbedtls_message_socket_init( &server_context );
2863 mbedtls_message_socket_init( &client_context );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002864
2865 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 1,
2866 &server,
2867 &server_context ) == 0 );
2868
2869 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 1,
2870 &client,
2871 &client_context ) == 0 );
2872
2873 /* Fill up the buffer with structured data so that unwanted changes
2874 * can be detected */
2875 for( i = 0; i < MSGLEN; i++ )
2876 {
2877 message[i] = i & 0xFF;
2878 }
2879 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
2880 MSGLEN ) );
2881
2882 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2883 MSGLEN ) == MSGLEN );
2884
2885 /* Force a read error by disconnecting the socket by hand */
2886 server.status = 0;
2887 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
2888 == MBEDTLS_TEST_ERROR_RECV_FAILED );
2889 /* Return to a valid state */
2890 server.status = MBEDTLS_MOCK_SOCKET_CONNECTED;
2891
2892 memset( received, 0, sizeof( received ) );
2893
2894 /* Test that even though the server tried to read once disconnected, the
2895 * continuity is preserved */
2896 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
2897 == MSGLEN );
2898
2899 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
2900
2901 exit:
2902 mbedtls_message_socket_close( &server_context );
2903 mbedtls_message_socket_close( &client_context );
2904}
2905/* END_CASE */
2906
2907/* BEGIN_CASE */
2908void ssl_message_mock_interleaved_one_way( )
2909{
2910 enum { MSGLEN = 10 };
2911 unsigned char message[MSGLEN], received[MSGLEN];
2912 mbedtls_mock_socket client, server;
2913 unsigned i;
2914 mbedtls_test_message_queue server_queue, client_queue;
2915 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05002916 mbedtls_message_socket_init( &server_context );
2917 mbedtls_message_socket_init( &client_context );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002918
2919 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 3,
2920 &server,
2921 &server_context ) == 0 );
2922
2923 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 3,
2924 &client,
2925 &client_context ) == 0 );
2926
2927 /* Fill up the buffer with structured data so that unwanted changes
2928 * can be detected */
2929 for( i = 0; i < MSGLEN; i++ )
2930 {
2931 message[i] = i & 0xFF;
2932 }
2933 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
2934 MSGLEN*3 ) );
2935
2936 /* Interleaved test - [2 sends, 1 read] twice, and then two reads
2937 * (to wrap around the buffer) */
2938 for( i = 0; i < 2; i++ )
2939 {
2940 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2941 MSGLEN ) == MSGLEN );
2942
2943 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2944 MSGLEN ) == MSGLEN );
2945
2946 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received,
2947 MSGLEN ) == MSGLEN );
2948 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
2949 memset( received, 0, sizeof( received ) );
2950 }
2951
2952 for( i = 0; i < 2; i++ )
2953 {
2954 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received,
2955 MSGLEN ) == MSGLEN );
2956
2957 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
2958 }
2959 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
Andrzej Kurekf46b9122020-02-07 08:19:00 -05002960 == MBEDTLS_ERR_SSL_WANT_READ );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002961 exit:
2962 mbedtls_message_socket_close( &server_context );
2963 mbedtls_message_socket_close( &client_context );
2964}
2965/* END_CASE */
2966
2967/* BEGIN_CASE */
2968void ssl_message_mock_interleaved_two_ways( )
2969{
2970 enum { MSGLEN = 10 };
2971 unsigned char message[MSGLEN], received[MSGLEN];
2972 mbedtls_mock_socket client, server;
2973 unsigned i;
2974 mbedtls_test_message_queue server_queue, client_queue;
2975 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05002976 mbedtls_message_socket_init( &server_context );
2977 mbedtls_message_socket_init( &client_context );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002978
2979 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 3,
2980 &server,
2981 &server_context ) == 0 );
2982
2983 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 3,
2984 &client,
2985 &client_context ) == 0 );
2986
2987 /* Fill up the buffer with structured data so that unwanted changes
2988 * can be detected */
2989 for( i = 0; i < MSGLEN; i++ )
2990 {
2991 message[i] = i & 0xFF;
2992 }
2993 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
2994 MSGLEN*3 ) );
2995
2996 /* Interleaved test - [2 sends, 1 read] twice, both ways, and then two reads
2997 * (to wrap around the buffer) both ways. */
2998 for( i = 0; i < 2; i++ )
2999 {
3000 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
3001 MSGLEN ) == MSGLEN );
3002
3003 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
3004 MSGLEN ) == MSGLEN );
3005
3006 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &server_context, message,
3007 MSGLEN ) == MSGLEN );
3008
3009 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &server_context, message,
3010 MSGLEN ) == MSGLEN );
3011
3012 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received,
3013 MSGLEN ) == MSGLEN );
3014
3015 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
3016
3017 memset( received, 0, sizeof( received ) );
3018
3019 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &client_context, received,
3020 MSGLEN ) == MSGLEN );
3021
3022 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
3023
3024 memset( received, 0, sizeof( received ) );
3025 }
3026
3027 for( i = 0; i < 2; i++ )
3028 {
3029 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received,
3030 MSGLEN ) == MSGLEN );
3031
3032 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
3033 memset( received, 0, sizeof( received ) );
3034
3035 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &client_context, received,
3036 MSGLEN ) == MSGLEN );
3037
3038 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
3039 memset( received, 0, sizeof( received ) );
3040 }
3041
3042 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
Andrzej Kurekf46b9122020-02-07 08:19:00 -05003043 == MBEDTLS_ERR_SSL_WANT_READ );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003044
3045 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &client_context, received, MSGLEN )
Andrzej Kurekf46b9122020-02-07 08:19:00 -05003046 == MBEDTLS_ERR_SSL_WANT_READ );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003047 exit:
3048 mbedtls_message_socket_close( &server_context );
3049 mbedtls_message_socket_close( &client_context );
3050}
3051/* END_CASE */
3052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003053/* BEGIN_CASE depends_on:MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Azim Khan5fcca462018-06-29 11:05:32 +01003054void ssl_dtls_replay( data_t * prevs, data_t * new, int ret )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003055{
Azim Khand30ca132017-06-09 04:32:58 +01003056 uint32_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003057 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02003058 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003059
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02003060 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02003061 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02003062
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02003063 TEST_ASSERT( mbedtls_ssl_config_defaults( &conf,
3064 MBEDTLS_SSL_IS_CLIENT,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02003065 MBEDTLS_SSL_TRANSPORT_DATAGRAM,
3066 MBEDTLS_SSL_PRESET_DEFAULT ) == 0 );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02003067 TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003068
3069 /* Read previous record numbers */
Azim Khand30ca132017-06-09 04:32:58 +01003070 for( len = 0; len < prevs->len; len += 6 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003071 {
Azim Khand30ca132017-06-09 04:32:58 +01003072 memcpy( ssl.in_ctr + 2, prevs->x + len, 6 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003073 mbedtls_ssl_dtls_replay_update( &ssl );
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003074 }
3075
3076 /* Check new number */
Azim Khand30ca132017-06-09 04:32:58 +01003077 memcpy( ssl.in_ctr + 2, new->x, 6 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003078 TEST_ASSERT( mbedtls_ssl_dtls_replay_check( &ssl ) == ret );
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003080 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02003081 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003082}
3083/* END_CASE */
Hanno Beckerb25c0c72017-05-05 11:24:30 +01003084
3085/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
3086void ssl_set_hostname_twice( char *hostname0, char *hostname1 )
3087{
3088 mbedtls_ssl_context ssl;
3089 mbedtls_ssl_init( &ssl );
3090
3091 TEST_ASSERT( mbedtls_ssl_set_hostname( &ssl, hostname0 ) == 0 );
3092 TEST_ASSERT( mbedtls_ssl_set_hostname( &ssl, hostname1 ) == 0 );
3093
3094 mbedtls_ssl_free( &ssl );
3095}
Darryl Green11999bb2018-03-13 15:22:58 +00003096/* END_CASE */
Hanno Beckera18d1322018-01-03 14:27:32 +00003097
3098/* BEGIN_CASE */
3099void ssl_crypt_record( int cipher_type, int hash_id,
Hanno Beckerd856c822019-04-29 17:30:59 +01003100 int etm, int tag_mode, int ver,
3101 int cid0_len, int cid1_len )
Hanno Beckera18d1322018-01-03 14:27:32 +00003102{
3103 /*
3104 * Test several record encryptions and decryptions
3105 * with plenty of space before and after the data
3106 * within the record buffer.
3107 */
3108
3109 int ret;
3110 int num_records = 16;
3111 mbedtls_ssl_context ssl; /* ONLY for debugging */
3112
3113 mbedtls_ssl_transform t0, t1;
Hanno Becker81e16a32019-03-01 11:21:44 +00003114 unsigned char *buf = NULL;
Hanno Beckera18d1322018-01-03 14:27:32 +00003115 size_t const buflen = 512;
3116 mbedtls_record rec, rec_backup;
3117
3118 mbedtls_ssl_init( &ssl );
3119 mbedtls_ssl_transform_init( &t0 );
3120 mbedtls_ssl_transform_init( &t1 );
3121 TEST_ASSERT( build_transforms( &t0, &t1, cipher_type, hash_id,
Hanno Beckerd856c822019-04-29 17:30:59 +01003122 etm, tag_mode, ver,
3123 (size_t) cid0_len,
3124 (size_t) cid1_len ) == 0 );
Hanno Beckera18d1322018-01-03 14:27:32 +00003125
Hanno Becker3ee54212019-04-04 16:31:26 +01003126 TEST_ASSERT( ( buf = mbedtls_calloc( 1, buflen ) ) != NULL );
Hanno Beckera18d1322018-01-03 14:27:32 +00003127
3128 while( num_records-- > 0 )
3129 {
3130 mbedtls_ssl_transform *t_dec, *t_enc;
3131 /* Take turns in who's sending and who's receiving. */
3132 if( num_records % 3 == 0 )
3133 {
3134 t_dec = &t0;
3135 t_enc = &t1;
3136 }
3137 else
3138 {
3139 t_dec = &t1;
3140 t_enc = &t0;
3141 }
3142
3143 /*
3144 * The record header affects the transformation in two ways:
3145 * 1) It determines the AEAD additional data
3146 * 2) The record counter sometimes determines the IV.
3147 *
3148 * Apart from that, the fields don't have influence.
3149 * In particular, it is currently not the responsibility
3150 * of ssl_encrypt/decrypt_buf to check if the transform
3151 * version matches the record version, or that the
3152 * type is sensible.
3153 */
3154
3155 memset( rec.ctr, num_records, sizeof( rec.ctr ) );
3156 rec.type = 42;
3157 rec.ver[0] = num_records;
3158 rec.ver[1] = num_records;
Hanno Beckera0e20d02019-05-15 14:03:01 +01003159#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd856c822019-04-29 17:30:59 +01003160 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01003161#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera18d1322018-01-03 14:27:32 +00003162
3163 rec.buf = buf;
3164 rec.buf_len = buflen;
3165 rec.data_offset = 16;
3166 /* Make sure to vary the length to exercise different
3167 * paddings. */
3168 rec.data_len = 1 + num_records;
3169
3170 memset( rec.buf + rec.data_offset, 42, rec.data_len );
3171
3172 /* Make a copy for later comparison */
3173 rec_backup = rec;
3174
3175 /* Encrypt record */
3176 ret = mbedtls_ssl_encrypt_buf( &ssl, t_enc, &rec,
3177 rnd_std_rand, NULL );
3178 TEST_ASSERT( ret == 0 || ret == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3179 if( ret != 0 )
3180 {
3181 continue;
3182 }
3183
3184 /* Decrypt record with t_dec */
Hanno Beckerd856c822019-04-29 17:30:59 +01003185 ret = mbedtls_ssl_decrypt_buf( &ssl, t_dec, &rec );
3186 TEST_ASSERT( ret == 0 );
Hanno Beckera18d1322018-01-03 14:27:32 +00003187
3188 /* Compare results */
3189 TEST_ASSERT( rec.type == rec_backup.type );
3190 TEST_ASSERT( memcmp( rec.ctr, rec_backup.ctr, 8 ) == 0 );
3191 TEST_ASSERT( rec.ver[0] == rec_backup.ver[0] );
3192 TEST_ASSERT( rec.ver[1] == rec_backup.ver[1] );
3193 TEST_ASSERT( rec.data_len == rec_backup.data_len );
3194 TEST_ASSERT( rec.data_offset == rec_backup.data_offset );
3195 TEST_ASSERT( memcmp( rec.buf + rec.data_offset,
3196 rec_backup.buf + rec_backup.data_offset,
3197 rec.data_len ) == 0 );
3198 }
3199
Hanno Becker81e16a32019-03-01 11:21:44 +00003200exit:
3201
Hanno Beckera18d1322018-01-03 14:27:32 +00003202 /* Cleanup */
3203 mbedtls_ssl_free( &ssl );
3204 mbedtls_ssl_transform_free( &t0 );
3205 mbedtls_ssl_transform_free( &t1 );
3206
Hanno Becker3ee54212019-04-04 16:31:26 +01003207 mbedtls_free( buf );
Hanno Beckera18d1322018-01-03 14:27:32 +00003208}
3209/* END_CASE */
Hanno Beckerb3268da2018-01-05 15:20:24 +00003210
3211/* BEGIN_CASE */
3212void ssl_crypt_record_small( int cipher_type, int hash_id,
Hanno Beckerd856c822019-04-29 17:30:59 +01003213 int etm, int tag_mode, int ver,
3214 int cid0_len, int cid1_len )
Hanno Beckerb3268da2018-01-05 15:20:24 +00003215{
3216 /*
3217 * Test pairs of encryption and decryption with an increasing
3218 * amount of space in the record buffer - in more detail:
3219 * 1) Try to encrypt with 0, 1, 2, ... bytes available
3220 * in front of the plaintext, and expect the encryption
3221 * to succeed starting from some offset. Always keep
3222 * enough space in the end of the buffer.
3223 * 2) Try to encrypt with 0, 1, 2, ... bytes available
3224 * at the end of the plaintext, and expect the encryption
3225 * to succeed starting from some offset. Always keep
3226 * enough space at the beginning of the buffer.
3227 * 3) Try to encrypt with 0, 1, 2, ... bytes available
3228 * both at the front and end of the plaintext,
3229 * and expect the encryption to succeed starting from
3230 * some offset.
3231 *
3232 * If encryption succeeds, check that decryption succeeds
3233 * and yields the original record.
3234 */
3235
3236 mbedtls_ssl_context ssl; /* ONLY for debugging */
3237
3238 mbedtls_ssl_transform t0, t1;
Hanno Becker81e16a32019-03-01 11:21:44 +00003239 unsigned char *buf = NULL;
Hanno Beckerd856c822019-04-29 17:30:59 +01003240 size_t const buflen = 256;
Hanno Beckerb3268da2018-01-05 15:20:24 +00003241 mbedtls_record rec, rec_backup;
3242
3243 int ret;
Hanno Beckerd856c822019-04-29 17:30:59 +01003244 int mode; /* Mode 1, 2 or 3 as explained above */
3245 size_t offset; /* Available space at beginning/end/both */
3246 size_t threshold = 96; /* Maximum offset to test against */
Hanno Beckerb3268da2018-01-05 15:20:24 +00003247
Hanno Beckerd856c822019-04-29 17:30:59 +01003248 size_t default_pre_padding = 64; /* Pre-padding to use in mode 2 */
3249 size_t default_post_padding = 128; /* Post-padding to use in mode 1 */
Hanno Beckerb3268da2018-01-05 15:20:24 +00003250
3251 int seen_success; /* Indicates if in the current mode we've
3252 * already seen a successful test. */
3253
3254 mbedtls_ssl_init( &ssl );
3255 mbedtls_ssl_transform_init( &t0 );
3256 mbedtls_ssl_transform_init( &t1 );
3257 TEST_ASSERT( build_transforms( &t0, &t1, cipher_type, hash_id,
Hanno Beckerd856c822019-04-29 17:30:59 +01003258 etm, tag_mode, ver,
3259 (size_t) cid0_len,
3260 (size_t) cid1_len ) == 0 );
Hanno Beckerb3268da2018-01-05 15:20:24 +00003261
Hanno Becker3ee54212019-04-04 16:31:26 +01003262 TEST_ASSERT( ( buf = mbedtls_calloc( 1, buflen ) ) != NULL );
Hanno Beckerb3268da2018-01-05 15:20:24 +00003263
3264 for( mode=1; mode <= 3; mode++ )
3265 {
3266 seen_success = 0;
3267 for( offset=0; offset <= threshold; offset++ )
3268 {
3269 mbedtls_ssl_transform *t_dec, *t_enc;
Hanno Becker6c87b3f2019-04-29 17:24:44 +01003270 t_dec = &t0;
3271 t_enc = &t1;
Hanno Beckerb3268da2018-01-05 15:20:24 +00003272
3273 memset( rec.ctr, offset, sizeof( rec.ctr ) );
3274 rec.type = 42;
3275 rec.ver[0] = offset;
3276 rec.ver[1] = offset;
3277 rec.buf = buf;
3278 rec.buf_len = buflen;
Hanno Beckera0e20d02019-05-15 14:03:01 +01003279#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd856c822019-04-29 17:30:59 +01003280 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01003281#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerb3268da2018-01-05 15:20:24 +00003282
3283 switch( mode )
3284 {
3285 case 1: /* Space in the beginning */
3286 rec.data_offset = offset;
3287 rec.data_len = buflen - offset - default_post_padding;
3288 break;
3289
3290 case 2: /* Space in the end */
3291 rec.data_offset = default_pre_padding;
3292 rec.data_len = buflen - default_pre_padding - offset;
3293 break;
3294
3295 case 3: /* Space in the beginning and end */
3296 rec.data_offset = offset;
3297 rec.data_len = buflen - 2 * offset;
3298 break;
3299
3300 default:
3301 TEST_ASSERT( 0 );
3302 break;
3303 }
3304
3305 memset( rec.buf + rec.data_offset, 42, rec.data_len );
3306
3307 /* Make a copy for later comparison */
3308 rec_backup = rec;
3309
3310 /* Encrypt record */
3311 ret = mbedtls_ssl_encrypt_buf( &ssl, t_enc, &rec, rnd_std_rand, NULL );
3312
3313 if( ( mode == 1 || mode == 2 ) && seen_success )
3314 {
3315 TEST_ASSERT( ret == 0 );
3316 }
3317 else
3318 {
3319 TEST_ASSERT( ret == 0 || ret == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3320 if( ret == 0 )
3321 seen_success = 1;
3322 }
3323
3324 if( ret != 0 )
3325 continue;
3326
3327 /* Decrypt record with t_dec */
3328 TEST_ASSERT( mbedtls_ssl_decrypt_buf( &ssl, t_dec, &rec ) == 0 );
3329
3330 /* Compare results */
3331 TEST_ASSERT( rec.type == rec_backup.type );
3332 TEST_ASSERT( memcmp( rec.ctr, rec_backup.ctr, 8 ) == 0 );
3333 TEST_ASSERT( rec.ver[0] == rec_backup.ver[0] );
3334 TEST_ASSERT( rec.ver[1] == rec_backup.ver[1] );
3335 TEST_ASSERT( rec.data_len == rec_backup.data_len );
3336 TEST_ASSERT( rec.data_offset == rec_backup.data_offset );
3337 TEST_ASSERT( memcmp( rec.buf + rec.data_offset,
3338 rec_backup.buf + rec_backup.data_offset,
3339 rec.data_len ) == 0 );
3340 }
3341
3342 TEST_ASSERT( seen_success == 1 );
3343 }
3344
Hanno Becker81e16a32019-03-01 11:21:44 +00003345exit:
3346
Hanno Beckerb3268da2018-01-05 15:20:24 +00003347 /* Cleanup */
3348 mbedtls_ssl_free( &ssl );
3349 mbedtls_ssl_transform_free( &t0 );
3350 mbedtls_ssl_transform_free( &t1 );
3351
Hanno Becker3ee54212019-04-04 16:31:26 +01003352 mbedtls_free( buf );
Hanno Beckerb3268da2018-01-05 15:20:24 +00003353}
3354/* END_CASE */
Ron Eldor824ad7b2019-05-13 14:09:00 +03003355
3356/* BEGIN_CASE */
3357void ssl_tls_prf( int type, data_t * secret, data_t * random,
3358 char *label, data_t *result_hex_str, int exp_ret )
3359{
3360 unsigned char *output;
3361
3362 output = mbedtls_calloc( 1, result_hex_str->len );
3363 if( output == NULL )
3364 goto exit;
3365
Ron Eldor6b9b1b82019-05-15 17:04:33 +03003366#if defined(MBEDTLS_USE_PSA_CRYPTO)
3367 TEST_ASSERT( psa_crypto_init() == 0 );
3368#endif
3369
Ron Eldor824ad7b2019-05-13 14:09:00 +03003370 TEST_ASSERT( mbedtls_ssl_tls_prf( type, secret->x, secret->len,
3371 label, random->x, random->len,
3372 output, result_hex_str->len ) == exp_ret );
3373
3374 if( exp_ret == 0 )
3375 {
3376 TEST_ASSERT( hexcmp( output, result_hex_str->x,
3377 result_hex_str->len, result_hex_str->len ) == 0 );
3378 }
3379exit:
3380
3381 mbedtls_free( output );
3382}
3383/* END_CASE */
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02003384
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02003385/* BEGIN_CASE */
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02003386void ssl_serialize_session_save_load( int ticket_len, char *crt_file )
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02003387{
3388 mbedtls_ssl_session original, restored;
3389 unsigned char *buf = NULL;
3390 size_t len;
3391
3392 /*
3393 * Test that a save-load pair is the identity
3394 */
3395
3396 mbedtls_ssl_session_init( &original );
3397 mbedtls_ssl_session_init( &restored );
3398
3399 /* Prepare a dummy session to work on */
3400 TEST_ASSERT( ssl_populate_session( &original, ticket_len, crt_file ) == 0 );
3401
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02003402 /* Serialize it */
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02003403 TEST_ASSERT( mbedtls_ssl_session_save( &original, NULL, 0, &len )
3404 == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3405 TEST_ASSERT( ( buf = mbedtls_calloc( 1, len ) ) != NULL );
3406 TEST_ASSERT( mbedtls_ssl_session_save( &original, buf, len, &len )
3407 == 0 );
3408
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02003409 /* Restore session from serialized data */
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02003410 TEST_ASSERT( mbedtls_ssl_session_load( &restored, buf, len) == 0 );
3411
3412 /*
3413 * Make sure both session structures are identical
3414 */
3415#if defined(MBEDTLS_HAVE_TIME)
3416 TEST_ASSERT( original.start == restored.start );
3417#endif
3418 TEST_ASSERT( original.ciphersuite == restored.ciphersuite );
3419 TEST_ASSERT( original.compression == restored.compression );
3420 TEST_ASSERT( original.id_len == restored.id_len );
3421 TEST_ASSERT( memcmp( original.id,
3422 restored.id, sizeof( original.id ) ) == 0 );
3423 TEST_ASSERT( memcmp( original.master,
3424 restored.master, sizeof( original.master ) ) == 0 );
3425
3426#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02003427#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02003428 TEST_ASSERT( ( original.peer_cert == NULL ) ==
3429 ( restored.peer_cert == NULL ) );
3430 if( original.peer_cert != NULL )
3431 {
3432 TEST_ASSERT( original.peer_cert->raw.len ==
3433 restored.peer_cert->raw.len );
3434 TEST_ASSERT( memcmp( original.peer_cert->raw.p,
3435 restored.peer_cert->raw.p,
3436 original.peer_cert->raw.len ) == 0 );
3437 }
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02003438#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3439 TEST_ASSERT( original.peer_cert_digest_type ==
3440 restored.peer_cert_digest_type );
3441 TEST_ASSERT( original.peer_cert_digest_len ==
3442 restored.peer_cert_digest_len );
3443 TEST_ASSERT( ( original.peer_cert_digest == NULL ) ==
3444 ( restored.peer_cert_digest == NULL ) );
3445 if( original.peer_cert_digest != NULL )
3446 {
3447 TEST_ASSERT( memcmp( original.peer_cert_digest,
3448 restored.peer_cert_digest,
3449 original.peer_cert_digest_len ) == 0 );
3450 }
3451#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3452#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02003453 TEST_ASSERT( original.verify_result == restored.verify_result );
3454
3455#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
3456 TEST_ASSERT( original.ticket_len == restored.ticket_len );
3457 if( original.ticket_len != 0 )
3458 {
3459 TEST_ASSERT( original.ticket != NULL );
3460 TEST_ASSERT( restored.ticket != NULL );
3461 TEST_ASSERT( memcmp( original.ticket,
3462 restored.ticket, original.ticket_len ) == 0 );
3463 }
3464 TEST_ASSERT( original.ticket_lifetime == restored.ticket_lifetime );
3465#endif
3466
3467#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3468 TEST_ASSERT( original.mfl_code == restored.mfl_code );
3469#endif
3470
3471#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
3472 TEST_ASSERT( original.trunc_hmac == restored.trunc_hmac );
3473#endif
3474
3475#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
3476 TEST_ASSERT( original.encrypt_then_mac == restored.encrypt_then_mac );
3477#endif
3478
3479exit:
3480 mbedtls_ssl_session_free( &original );
3481 mbedtls_ssl_session_free( &restored );
3482 mbedtls_free( buf );
3483}
3484/* END_CASE */
3485
Manuel Pégourié-Gonnardaa755832019-06-03 10:53:47 +02003486/* BEGIN_CASE */
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02003487void ssl_serialize_session_load_save( int ticket_len, char *crt_file )
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02003488{
3489 mbedtls_ssl_session session;
3490 unsigned char *buf1 = NULL, *buf2 = NULL;
3491 size_t len0, len1, len2;
3492
3493 /*
3494 * Test that a load-save pair is the identity
3495 */
3496
3497 mbedtls_ssl_session_init( &session );
3498
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02003499 /* Prepare a dummy session to work on */
Manuel Pégourié-Gonnard6b840702019-05-24 09:40:17 +02003500 TEST_ASSERT( ssl_populate_session( &session, ticket_len, crt_file ) == 0 );
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02003501
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02003502 /* Get desired buffer size for serializing */
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02003503 TEST_ASSERT( mbedtls_ssl_session_save( &session, NULL, 0, &len0 )
3504 == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3505
3506 /* Allocate first buffer */
3507 buf1 = mbedtls_calloc( 1, len0 );
3508 TEST_ASSERT( buf1 != NULL );
3509
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02003510 /* Serialize to buffer and free live session */
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02003511 TEST_ASSERT( mbedtls_ssl_session_save( &session, buf1, len0, &len1 )
3512 == 0 );
3513 TEST_ASSERT( len0 == len1 );
3514 mbedtls_ssl_session_free( &session );
3515
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02003516 /* Restore session from serialized data */
Manuel Pégourié-Gonnard220403b2019-05-24 09:54:21 +02003517 TEST_ASSERT( mbedtls_ssl_session_load( &session, buf1, len1 ) == 0 );
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02003518
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02003519 /* Allocate second buffer and serialize to it */
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02003520 buf2 = mbedtls_calloc( 1, len0 );
Manuel Pégourié-Gonnardb4079902019-05-24 09:52:10 +02003521 TEST_ASSERT( buf2 != NULL );
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02003522 TEST_ASSERT( mbedtls_ssl_session_save( &session, buf2, len0, &len2 )
3523 == 0 );
3524
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02003525 /* Make sure both serialized versions are identical */
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02003526 TEST_ASSERT( len1 == len2 );
3527 TEST_ASSERT( memcmp( buf1, buf2, len1 ) == 0 );
3528
3529exit:
3530 mbedtls_ssl_session_free( &session );
3531 mbedtls_free( buf1 );
3532 mbedtls_free( buf2 );
3533}
3534/* END_CASE */
Manuel Pégourié-Gonnardf5fa0aa2019-05-23 10:38:11 +02003535
3536/* BEGIN_CASE */
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02003537void ssl_serialize_session_save_buf_size( int ticket_len, char *crt_file )
Manuel Pégourié-Gonnardf5fa0aa2019-05-23 10:38:11 +02003538{
3539 mbedtls_ssl_session session;
3540 unsigned char *buf = NULL;
3541 size_t good_len, bad_len, test_len;
3542
3543 /*
3544 * Test that session_save() fails cleanly on small buffers
3545 */
3546
3547 mbedtls_ssl_session_init( &session );
3548
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02003549 /* Prepare dummy session and get serialized size */
Manuel Pégourié-Gonnard6b840702019-05-24 09:40:17 +02003550 TEST_ASSERT( ssl_populate_session( &session, ticket_len, crt_file ) == 0 );
Manuel Pégourié-Gonnardf5fa0aa2019-05-23 10:38:11 +02003551 TEST_ASSERT( mbedtls_ssl_session_save( &session, NULL, 0, &good_len )
3552 == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3553
3554 /* Try all possible bad lengths */
3555 for( bad_len = 1; bad_len < good_len; bad_len++ )
3556 {
3557 /* Allocate exact size so that asan/valgrind can detect any overwrite */
3558 mbedtls_free( buf );
3559 TEST_ASSERT( ( buf = mbedtls_calloc( 1, bad_len ) ) != NULL );
3560 TEST_ASSERT( mbedtls_ssl_session_save( &session, buf, bad_len,
3561 &test_len )
3562 == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3563 TEST_ASSERT( test_len == good_len );
3564 }
3565
3566exit:
3567 mbedtls_ssl_session_free( &session );
3568 mbedtls_free( buf );
3569}
3570/* END_CASE */
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003571
3572/* BEGIN_CASE */
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02003573void ssl_serialize_session_load_buf_size( int ticket_len, char *crt_file )
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003574{
3575 mbedtls_ssl_session session;
3576 unsigned char *good_buf = NULL, *bad_buf = NULL;
3577 size_t good_len, bad_len;
3578
3579 /*
3580 * Test that session_load() fails cleanly on small buffers
3581 */
3582
3583 mbedtls_ssl_session_init( &session );
3584
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02003585 /* Prepare serialized session data */
Manuel Pégourié-Gonnard6b840702019-05-24 09:40:17 +02003586 TEST_ASSERT( ssl_populate_session( &session, ticket_len, crt_file ) == 0 );
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003587 TEST_ASSERT( mbedtls_ssl_session_save( &session, NULL, 0, &good_len )
3588 == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3589 TEST_ASSERT( ( good_buf = mbedtls_calloc( 1, good_len ) ) != NULL );
3590 TEST_ASSERT( mbedtls_ssl_session_save( &session, good_buf, good_len,
3591 &good_len ) == 0 );
3592 mbedtls_ssl_session_free( &session );
3593
3594 /* Try all possible bad lengths */
3595 for( bad_len = 0; bad_len < good_len; bad_len++ )
3596 {
3597 /* Allocate exact size so that asan/valgrind can detect any overread */
3598 mbedtls_free( bad_buf );
3599 bad_buf = mbedtls_calloc( 1, bad_len ? bad_len : 1 );
3600 TEST_ASSERT( bad_buf != NULL );
3601 memcpy( bad_buf, good_buf, bad_len );
3602
3603 TEST_ASSERT( mbedtls_ssl_session_load( &session, bad_buf, bad_len )
3604 == MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3605 }
3606
3607exit:
3608 mbedtls_ssl_session_free( &session );
3609 mbedtls_free( good_buf );
3610 mbedtls_free( bad_buf );
3611}
3612/* END_CASE */
Hanno Becker861d0bb2019-05-21 16:39:30 +01003613
Hanno Becker363b6462019-05-29 12:44:28 +01003614/* BEGIN_CASE */
3615void ssl_session_serialize_version_check( int corrupt_major,
Hanno Becker861d0bb2019-05-21 16:39:30 +01003616 int corrupt_minor,
3617 int corrupt_patch,
3618 int corrupt_config )
3619{
Hanno Becker363b6462019-05-29 12:44:28 +01003620 unsigned char serialized_session[ 2048 ];
3621 size_t serialized_session_len;
Hanno Beckerfe1275e2019-05-29 12:45:21 +01003622 unsigned cur_byte;
Hanno Becker861d0bb2019-05-21 16:39:30 +01003623 mbedtls_ssl_session session;
Hanno Beckerfe1275e2019-05-29 12:45:21 +01003624 uint8_t should_corrupt_byte[] = { corrupt_major == 1,
3625 corrupt_minor == 1,
3626 corrupt_patch == 1,
3627 corrupt_config == 1,
3628 corrupt_config == 1 };
3629
Hanno Becker861d0bb2019-05-21 16:39:30 +01003630 mbedtls_ssl_session_init( &session );
3631
Hanno Beckerfe1275e2019-05-29 12:45:21 +01003632 /* Infer length of serialized session. */
Hanno Becker861d0bb2019-05-21 16:39:30 +01003633 TEST_ASSERT( mbedtls_ssl_session_save( &session,
Hanno Becker363b6462019-05-29 12:44:28 +01003634 serialized_session,
3635 sizeof( serialized_session ),
3636 &serialized_session_len ) == 0 );
Hanno Becker861d0bb2019-05-21 16:39:30 +01003637
Hanno Beckerfe1275e2019-05-29 12:45:21 +01003638 mbedtls_ssl_session_free( &session );
Hanno Becker861d0bb2019-05-21 16:39:30 +01003639
Hanno Beckerfe1275e2019-05-29 12:45:21 +01003640 /* Without any modification, we should be able to successfully
Hanno Becker363b6462019-05-29 12:44:28 +01003641 * de-serialize the session - double-check that. */
Hanno Becker861d0bb2019-05-21 16:39:30 +01003642 TEST_ASSERT( mbedtls_ssl_session_load( &session,
Hanno Becker363b6462019-05-29 12:44:28 +01003643 serialized_session,
3644 serialized_session_len ) == 0 );
Hanno Becker861d0bb2019-05-21 16:39:30 +01003645 mbedtls_ssl_session_free( &session );
3646
Hanno Beckerfe1275e2019-05-29 12:45:21 +01003647 /* Go through the bytes in the serialized session header and
3648 * corrupt them bit-by-bit. */
3649 for( cur_byte = 0; cur_byte < sizeof( should_corrupt_byte ); cur_byte++ )
Hanno Becker861d0bb2019-05-21 16:39:30 +01003650 {
Hanno Beckerfe1275e2019-05-29 12:45:21 +01003651 int cur_bit;
3652 unsigned char * const byte = &serialized_session[ cur_byte ];
3653
3654 if( should_corrupt_byte[ cur_byte ] == 0 )
3655 continue;
3656
3657 for( cur_bit = 0; cur_bit < CHAR_BIT; cur_bit++ )
3658 {
3659 unsigned char const corrupted_bit = 0x1u << cur_bit;
3660 /* Modify a single bit in the serialized session. */
3661 *byte ^= corrupted_bit;
3662
3663 /* Attempt to deserialize */
3664 TEST_ASSERT( mbedtls_ssl_session_load( &session,
3665 serialized_session,
3666 serialized_session_len ) ==
Hanno Beckerf9b33032019-06-03 12:58:39 +01003667 MBEDTLS_ERR_SSL_VERSION_MISMATCH );
Hanno Beckerfe1275e2019-05-29 12:45:21 +01003668
3669 /* Undo the change */
3670 *byte ^= corrupted_bit;
3671 }
Hanno Becker861d0bb2019-05-21 16:39:30 +01003672 }
3673
Hanno Becker861d0bb2019-05-21 16:39:30 +01003674}
3675/* END_CASE */
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01003676
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01003677/* 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 +01003678void mbedtls_endpoint_sanity( int endpoint_type )
3679{
3680 enum { BUFFSIZE = 1024 };
3681 mbedtls_endpoint ep;
3682 int ret = -1;
3683
Andrzej Kurek15daf502020-02-12 09:17:52 -05003684 ret = mbedtls_endpoint_init( NULL, endpoint_type, MBEDTLS_PK_RSA,
3685 NULL, NULL, NULL );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01003686 TEST_ASSERT( MBEDTLS_ERR_SSL_BAD_INPUT_DATA == ret );
3687
Andrzej Kurekb2980742020-02-02 19:25:26 -05003688 ret = mbedtls_endpoint_certificate_init( NULL, MBEDTLS_PK_RSA );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01003689 TEST_ASSERT( MBEDTLS_ERR_SSL_BAD_INPUT_DATA == ret );
3690
Andrzej Kurek15daf502020-02-12 09:17:52 -05003691 ret = mbedtls_endpoint_init( &ep, endpoint_type, MBEDTLS_PK_RSA,
3692 NULL, NULL, NULL );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01003693 TEST_ASSERT( ret == 0 );
3694
3695exit:
Andrzej Kurek15daf502020-02-12 09:17:52 -05003696 mbedtls_endpoint_free( &ep, NULL );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01003697}
3698/* END_CASE */
3699
Andrzej Kurekb2980742020-02-02 19:25:26 -05003700/* 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 +01003701void move_handshake_to_state(int endpoint_type, int state, int need_pass)
3702{
3703 enum { BUFFSIZE = 1024 };
3704 mbedtls_endpoint base_ep, second_ep;
3705 int ret = -1;
3706
Andrzej Kurek15daf502020-02-12 09:17:52 -05003707 ret = mbedtls_endpoint_init( &base_ep, endpoint_type, MBEDTLS_PK_RSA,
3708 NULL, NULL, NULL );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01003709 TEST_ASSERT( ret == 0 );
3710
3711 ret = mbedtls_endpoint_init( &second_ep,
3712 ( endpoint_type == MBEDTLS_SSL_IS_SERVER ) ?
Andrzej Kurekb2980742020-02-02 19:25:26 -05003713 MBEDTLS_SSL_IS_CLIENT : MBEDTLS_SSL_IS_SERVER,
Andrzej Kurek15daf502020-02-12 09:17:52 -05003714 MBEDTLS_PK_RSA, NULL, NULL, NULL );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01003715 TEST_ASSERT( ret == 0 );
3716
3717 ret = mbedtls_mock_socket_connect( &(base_ep.socket),
3718 &(second_ep.socket),
3719 BUFFSIZE );
3720 TEST_ASSERT( ret == 0 );
3721
3722 ret = mbedtls_move_handshake_to_state( &(base_ep.ssl),
3723 &(second_ep.ssl),
3724 state );
3725 if( need_pass )
3726 {
3727 TEST_ASSERT( ret == 0 );
3728 TEST_ASSERT( base_ep.ssl.state == state );
3729 }
3730 else
3731 {
3732 TEST_ASSERT( ret != 0 );
3733 TEST_ASSERT( base_ep.ssl.state != state );
3734 }
3735
3736exit:
Andrzej Kurek15daf502020-02-12 09:17:52 -05003737 mbedtls_endpoint_free( &base_ep, NULL );
3738 mbedtls_endpoint_free( &second_ep, NULL );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01003739}
3740/* END_CASE */
Andrzej Kurekf40daa32020-02-04 09:00:01 -05003741
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003742/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED */
3743void handshake_version( int version, int dtls )
Andrzej Kurekf40daa32020-02-04 09:00:01 -05003744{
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003745 handshake_test_options options;
3746 init_handshake_options( &options );
Andrzej Kurekda2b6782020-02-12 07:56:36 -05003747
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003748 options.version = version;
3749 options.dtls = dtls;
3750 /* Note - the case below will have to updated, since the test sends no data
3751 * due to a 1n-1 split against BEAST, that was not expected when preparing
3752 * the fragment counting code. */
3753 if( version == MBEDTLS_SSL_MINOR_VERSION_0 ||
3754 version == MBEDTLS_SSL_MINOR_VERSION_1 )
Andrzej Kurek941962e2020-02-07 09:20:32 -05003755 {
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003756 options.cli_msg_len = 0;
3757 options.srv_msg_len = 0;
Andrzej Kurek941962e2020-02-07 09:20:32 -05003758 }
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003759 perform_handshake( &options );
Andrzej Kurekf40daa32020-02-04 09:00:01 -05003760
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003761 /* The goto below is used to avoid an "unused label" warning.*/
3762 goto exit;
3763}
3764/* END_CASE */
Andrzej Kurek9e9efdc2020-02-26 05:25:23 -05003765
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003766/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_PROTO_TLS1_2 */
3767void handshake_psk_cipher( char* cipher, int pk_alg, data_t *psk_str, int dtls )
3768{
3769 handshake_test_options options;
3770 init_handshake_options( &options );
Andrzej Kurekf40daa32020-02-04 09:00:01 -05003771
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003772 options.cipher = cipher;
3773 options.dtls = dtls;
3774 options.psk_str = psk_str;
3775 options.pk_alg = pk_alg;
Andrzej Kurekcc5169c2020-02-04 09:04:56 -05003776
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003777 perform_handshake( &options );
Andrzej Kurek316da1f2020-02-26 09:03:47 -05003778
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003779 /* The goto below is used to avoid an "unused label" warning.*/
3780 goto exit;
3781}
3782/* END_CASE */
Andrzej Kurek316da1f2020-02-26 09:03:47 -05003783
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003784/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_PROTO_TLS1_2 */
3785void handshake_cipher( char* cipher, int pk_alg, int dtls )
3786{
3787 test_handshake_psk_cipher( cipher, pk_alg, NULL, dtls );
Andrzej Kurekf40daa32020-02-04 09:00:01 -05003788
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003789 /* The goto below is used to avoid an "unused label" warning.*/
3790 goto exit;
3791}
3792/* END_CASE */
Andrzej Kurekf40daa32020-02-04 09:00:01 -05003793
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003794/* 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 */
3795void app_data( int mfl, int cli_msg_len, int srv_msg_len,
3796 int expected_cli_fragments,
3797 int expected_srv_fragments, int dtls )
3798{
3799 handshake_test_options options;
3800 init_handshake_options( &options );
Andrzej Kurekda2b6782020-02-12 07:56:36 -05003801
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003802 options.mfl = mfl;
3803 options.cli_msg_len = cli_msg_len;
3804 options.srv_msg_len = srv_msg_len;
3805 options.expected_cli_fragments = expected_cli_fragments;
3806 options.expected_srv_fragments = expected_srv_fragments;
3807 options.dtls = dtls;
Andrzej Kurekda2b6782020-02-12 07:56:36 -05003808
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003809 perform_handshake( &options );
3810 /* The goto below is used to avoid an "unused label" warning.*/
3811 goto exit;
3812}
3813/* END_CASE */
Andrzej Kurekda2b6782020-02-12 07:56:36 -05003814
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003815/* 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 */
3816void app_data_tls( int mfl, int cli_msg_len, int srv_msg_len,
3817 int expected_cli_fragments,
3818 int expected_srv_fragments )
3819{
3820 test_app_data( mfl, cli_msg_len, srv_msg_len, expected_cli_fragments,
3821 expected_srv_fragments, 0 );
3822 /* The goto below is used to avoid an "unused label" warning.*/
3823 goto exit;
3824}
3825/* END_CASE */
Andrzej Kurekda2b6782020-02-12 07:56:36 -05003826
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003827/* 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 */
3828void app_data_dtls( int mfl, int cli_msg_len, int srv_msg_len,
3829 int expected_cli_fragments,
3830 int expected_srv_fragments )
3831{
3832 test_app_data( mfl, cli_msg_len, srv_msg_len, expected_cli_fragments,
3833 expected_srv_fragments, 1 );
3834 /* The goto below is used to avoid an "unused label" warning.*/
3835 goto exit;
3836}
3837/* END_CASE */
Andrzej Kurekda2b6782020-02-12 07:56:36 -05003838
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003839/* 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 */
3840void handshake_serialization( )
3841{
3842 handshake_test_options options;
3843 init_handshake_options( &options );
Andrzej Kurekda2b6782020-02-12 07:56:36 -05003844
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003845 options.serialize = 1;
3846 options.dtls = 1;
3847 perform_handshake( &options );
3848 /* The goto below is used to avoid an "unused label" warning.*/
3849 goto exit;
3850}
3851/* END_CASE */
Andrzej Kurekda2b6782020-02-12 07:56:36 -05003852
Darryl Greenaad82f92019-12-02 10:53:11 +00003853/* 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 +01003854void handshake_fragmentation( int mfl, int expected_srv_hs_fragmentation, int expected_cli_hs_fragmentation)
3855{
3856 handshake_test_options options;
3857 log_pattern srv_pattern, cli_pattern;
3858
3859 srv_pattern.pattern = cli_pattern.pattern = "found fragmented DTLS handshake";
3860 srv_pattern.counter = 0;
3861 cli_pattern.counter = 0;
3862
3863 init_handshake_options( &options );
3864 options.dtls = 1;
3865 options.mfl = mfl;
Darryl Greenaad82f92019-12-02 10:53:11 +00003866 /* Set cipher to one using CBC so that record splitting can be tested */
3867 options.cipher = "TLS-DHE-RSA-WITH-AES-256-CBC-SHA256";
Piotr Nowickibde7ee82020-02-21 10:59:50 +01003868 options.srv_auth_mode = MBEDTLS_SSL_VERIFY_REQUIRED;
3869 options.srv_log_obj = &srv_pattern;
3870 options.cli_log_obj = &cli_pattern;
3871 options.srv_log_fun = log_analyzer;
3872 options.cli_log_fun = log_analyzer;
3873
3874 perform_handshake( &options );
3875
3876 /* Test if the server received a fragmented handshake */
3877 if( expected_srv_hs_fragmentation )
3878 {
3879 TEST_ASSERT( srv_pattern.counter >= 1 );
3880 }
3881 /* Test if the client received a fragmented handshake */
3882 if( expected_cli_hs_fragmentation )
3883 {
3884 TEST_ASSERT( cli_pattern.counter >= 1 );
3885 }
3886}
3887/* END_CASE */
3888
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003889/* 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 */
3890void renegotiation( int legacy_renegotiation )
3891{
3892 handshake_test_options options;
3893 init_handshake_options( &options );
Andrzej Kurekda2b6782020-02-12 07:56:36 -05003894
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003895 options.renegotiate = 1;
3896 options.legacy_renegotiation = legacy_renegotiation;
3897 options.dtls = 1;
Andrzej Kurek316da1f2020-02-26 09:03:47 -05003898
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05003899 perform_handshake( &options );
3900 /* The goto below is used to avoid an "unused label" warning.*/
3901 goto exit;
Andrzej Kurekf40daa32020-02-04 09:00:01 -05003902}
3903/* END_CASE */
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05003904
3905/* 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 */
3906void resize_buffers( int mfl, int renegotiation, int legacy_renegotiation,
3907 int serialize, int dtls )
3908{
3909 handshake_test_options options;
3910 init_handshake_options( &options );
3911
3912 options.mfl = mfl;
3913 options.renegotiate = renegotiation;
3914 options.legacy_renegotiation = legacy_renegotiation;
3915 options.serialize = serialize;
3916 options.dtls = dtls;
3917 options.resize_buffers = 1;
3918
3919 perform_handshake( &options );
3920 /* The goto below is used to avoid an "unused label" warning.*/
3921 goto exit;
3922}
3923/* END_CASE */
3924
3925/* 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 */
3926void resize_buffers_serialize_mfl( int mfl )
3927{
3928 test_resize_buffers( mfl, 0, MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION, 1, 1 );
3929
3930 /* The goto below is used to avoid an "unused label" warning.*/
3931 goto exit;
3932}
3933/* END_CASE */
3934
3935/* 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 */
3936void resize_buffers_renegotiate_mfl( int mfl, int legacy_renegotiation )
3937{
3938 test_resize_buffers( mfl, 1, legacy_renegotiation, 0, 1 );
3939
3940 /* The goto below is used to avoid an "unused label" warning.*/
3941 goto exit;
3942}
3943/* END_CASE */