blob: c555d74a2068f3d7ef9d482d18fe3b621a57fa16 [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>
Chris Jones84a773f2021-03-05 18:38:47 +00003#include <ssl_misc.h>
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01004#include <mbedtls/ctr_drbg.h>
5#include <mbedtls/entropy.h>
Andrzej Kurek941962e2020-02-07 09:20:32 -05006#include <mbedtls/timing.h>
Piotr Nowickibde7ee82020-02-21 10:59:50 +01007#include <mbedtls/debug.h>
Hanno Becker73c825a2020-09-08 10:52:58 +01008#include <ssl_tls13_keys.h>
Mateusz Starzyk1aec6462021-02-08 15:34:42 +01009#include "test/certs.h"
Piotr Nowickibde7ee82020-02-21 10:59:50 +010010
Hanno Becker6667ffd2021-04-19 21:59:22 +010011#include <psa/crypto.h>
12
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +020013#include <ssl_invasive.h>
14
Manuel Pégourié-Gonnard9670a592020-07-10 10:21:46 +020015#include <test/constant_flow.h>
16
Hanno Becker1413bd82020-09-09 12:46:09 +010017enum
18{
19#define MBEDTLS_SSL_TLS1_3_LABEL( name, string ) \
20 tls1_3_label_ ## name,
Hanno Becker70d7fb02020-09-09 10:11:21 +010021MBEDTLS_SSL_TLS1_3_LABEL_LIST
22#undef MBEDTLS_SSL_TLS1_3_LABEL
Hanno Becker1413bd82020-09-09 12:46:09 +010023};
Hanno Becker70d7fb02020-09-09 10:11:21 +010024
Piotr Nowickibde7ee82020-02-21 10:59:50 +010025typedef struct log_pattern
26{
27 const char *pattern;
28 size_t counter;
29} log_pattern;
30
Piotr Nowicki438bf3b2020-03-10 12:59:10 +010031/*
32 * This function can be passed to mbedtls to receive output logs from it. In
Piotr Nowickibde7ee82020-02-21 10:59:50 +010033 * this case, it will count the instances of a log_pattern in the received
34 * logged messages.
35 */
36void log_analyzer( void *ctx, int level,
37 const char *file, int line,
38 const char *str )
39{
40 log_pattern *p = (log_pattern *) ctx;
41
42 (void) level;
43 (void) line;
44 (void) file;
45
46 if( NULL != p &&
47 NULL != p->pattern &&
48 NULL != strstr( str, p->pattern ) )
49 {
50 p->counter++;
51 }
52}
Janos Follath6264e662019-11-26 11:11:15 +000053
Paul Elliottc8570442020-04-15 17:00:50 +010054/* Invalid minor version used when not specifying a min/max version or expecting a test to fail */
55#define TEST_SSL_MINOR_VERSION_NONE -1
56
Andrzej Kurek8a6ff152020-02-26 09:10:14 -050057typedef struct handshake_test_options
58{
59 const char *cipher;
Paul Elliottc8570442020-04-15 17:00:50 +010060 int client_min_version;
61 int client_max_version;
62 int server_min_version;
63 int server_max_version;
64 int expected_negotiated_version;
Andrzej Kurek8a6ff152020-02-26 09:10:14 -050065 int pk_alg;
66 data_t *psk_str;
67 int dtls;
Piotr Nowickibde7ee82020-02-21 10:59:50 +010068 int srv_auth_mode;
Andrzej Kurek8a6ff152020-02-26 09:10:14 -050069 int serialize;
70 int mfl;
71 int cli_msg_len;
72 int srv_msg_len;
73 int expected_cli_fragments;
74 int expected_srv_fragments;
75 int renegotiate;
76 int legacy_renegotiation;
Piotr Nowickibde7ee82020-02-21 10:59:50 +010077 void *srv_log_obj;
78 void *cli_log_obj;
79 void (*srv_log_fun)(void *, int, const char *, int, const char *);
80 void (*cli_log_fun)(void *, int, const char *, int, const char *);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -050081 int resize_buffers;
Andrzej Kurek8a6ff152020-02-26 09:10:14 -050082} handshake_test_options;
83
84void init_handshake_options( handshake_test_options *opts )
85{
86 opts->cipher = "";
Paul Elliottc8570442020-04-15 17:00:50 +010087 opts->client_min_version = TEST_SSL_MINOR_VERSION_NONE;
88 opts->client_max_version = TEST_SSL_MINOR_VERSION_NONE;
89 opts->server_min_version = TEST_SSL_MINOR_VERSION_NONE;
90 opts->server_max_version = TEST_SSL_MINOR_VERSION_NONE;
91 opts->expected_negotiated_version = MBEDTLS_SSL_MINOR_VERSION_3;
Andrzej Kurek8a6ff152020-02-26 09:10:14 -050092 opts->pk_alg = MBEDTLS_PK_RSA;
93 opts->psk_str = NULL;
94 opts->dtls = 0;
Piotr Nowickibde7ee82020-02-21 10:59:50 +010095 opts->srv_auth_mode = MBEDTLS_SSL_VERIFY_NONE;
Andrzej Kurek8a6ff152020-02-26 09:10:14 -050096 opts->serialize = 0;
97 opts->mfl = MBEDTLS_SSL_MAX_FRAG_LEN_NONE;
98 opts->cli_msg_len = 100;
99 opts->srv_msg_len = 100;
100 opts->expected_cli_fragments = 1;
101 opts->expected_srv_fragments = 1;
102 opts->renegotiate = 0;
103 opts->legacy_renegotiation = MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION;
Piotr Nowickibde7ee82020-02-21 10:59:50 +0100104 opts->srv_log_obj = NULL;
105 opts->srv_log_obj = NULL;
106 opts->srv_log_fun = NULL;
107 opts->cli_log_fun = NULL;
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500108 opts->resize_buffers = 1;
Andrzej Kurek8a6ff152020-02-26 09:10:14 -0500109}
Janos Follath6264e662019-11-26 11:11:15 +0000110/*
111 * Buffer structure for custom I/O callbacks.
112 */
113
114typedef struct mbedtls_test_buffer
115{
116 size_t start;
117 size_t content_length;
118 size_t capacity;
119 unsigned char *buffer;
120} mbedtls_test_buffer;
121
122/*
123 * Initialises \p buf. After calling this function it is safe to call
124 * `mbedtls_test_buffer_free()` on \p buf.
125 */
126void mbedtls_test_buffer_init( mbedtls_test_buffer *buf )
127{
128 memset( buf, 0, sizeof( *buf ) );
129}
130
131/*
132 * Sets up \p buf. After calling this function it is safe to call
133 * `mbedtls_test_buffer_put()` and `mbedtls_test_buffer_get()` on \p buf.
134 */
135int mbedtls_test_buffer_setup( mbedtls_test_buffer *buf, size_t capacity )
136{
137 buf->buffer = (unsigned char*) mbedtls_calloc( capacity,
138 sizeof(unsigned char) );
139 if( NULL == buf->buffer )
140 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
141 buf->capacity = capacity;
142
143 return 0;
144}
145
146void mbedtls_test_buffer_free( mbedtls_test_buffer *buf )
147{
148 if( buf->buffer != NULL )
149 mbedtls_free( buf->buffer );
150
151 memset( buf, 0, sizeof( *buf ) );
152}
153
154/*
155 * Puts \p input_len bytes from the \p input buffer into the ring buffer \p buf.
156 *
157 * \p buf must have been initialized and set up by calling
158 * `mbedtls_test_buffer_init()` and `mbedtls_test_buffer_setup()`.
159 *
160 * \retval \p input_len, if the data fits.
161 * \retval 0 <= value < \p input_len, if the data does not fit.
162 * \retval -1, if \p buf is NULL, it hasn't been set up or \p input_len is not
163 * zero and \p input is NULL.
164 */
165int mbedtls_test_buffer_put( mbedtls_test_buffer *buf,
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100166 const unsigned char *input, size_t input_len )
Janos Follath6264e662019-11-26 11:11:15 +0000167{
168 size_t overflow = 0;
169
170 if( ( buf == NULL ) || ( buf->buffer == NULL ) )
171 return -1;
172
173 /* Reduce input_len to a number that fits in the buffer. */
174 if ( ( buf->content_length + input_len ) > buf->capacity )
175 {
176 input_len = buf->capacity - buf->content_length;
177 }
178
179 if( input == NULL )
180 {
181 return ( input_len == 0 ) ? 0 : -1;
182 }
183
Piotr Nowickifb437d72020-01-13 16:59:12 +0100184 /* Check if the buffer has not come full circle and free space is not in
185 * the middle */
186 if( buf->start + buf->content_length < buf->capacity )
Janos Follath6264e662019-11-26 11:11:15 +0000187 {
Piotr Nowickifb437d72020-01-13 16:59:12 +0100188
189 /* Calculate the number of bytes that need to be placed at lower memory
190 * address */
191 if( buf->start + buf->content_length + input_len
192 > buf->capacity )
193 {
194 overflow = ( buf->start + buf->content_length + input_len )
195 % buf->capacity;
196 }
197
198 memcpy( buf->buffer + buf->start + buf->content_length, input,
199 input_len - overflow );
200 memcpy( buf->buffer, input + input_len - overflow, overflow );
201
202 }
203 else
204 {
205 /* The buffer has come full circle and free space is in the middle */
206 memcpy( buf->buffer + buf->start + buf->content_length - buf->capacity,
207 input, input_len );
Janos Follath6264e662019-11-26 11:11:15 +0000208 }
209
Janos Follath6264e662019-11-26 11:11:15 +0000210 buf->content_length += input_len;
Janos Follath6264e662019-11-26 11:11:15 +0000211 return input_len;
212}
213
214/*
Andrzej Kurekf7774142020-01-22 06:34:59 -0500215 * Gets \p output_len bytes from the ring buffer \p buf into the
216 * \p output buffer. The output buffer can be NULL, in this case a part of the
217 * ring buffer will be dropped, if the requested length is available.
Janos Follath6264e662019-11-26 11:11:15 +0000218 *
219 * \p buf must have been initialized and set up by calling
220 * `mbedtls_test_buffer_init()` and `mbedtls_test_buffer_setup()`.
221 *
222 * \retval \p output_len, if the data is available.
223 * \retval 0 <= value < \p output_len, if the data is not available.
Andrzej Kurekf7774142020-01-22 06:34:59 -0500224 * \retval -1, if \buf is NULL or it hasn't been set up.
Janos Follath6264e662019-11-26 11:11:15 +0000225 */
226int mbedtls_test_buffer_get( mbedtls_test_buffer *buf,
227 unsigned char* output, size_t output_len )
228{
229 size_t overflow = 0;
230
231 if( ( buf == NULL ) || ( buf->buffer == NULL ) )
232 return -1;
233
Andrzej Kurekf7774142020-01-22 06:34:59 -0500234 if( output == NULL && output_len == 0 )
235 return 0;
Janos Follath6264e662019-11-26 11:11:15 +0000236
237 if( buf->content_length < output_len )
238 output_len = buf->content_length;
239
240 /* Calculate the number of bytes that need to be drawn from lower memory
241 * address */
242 if( buf->start + output_len > buf->capacity )
243 {
244 overflow = ( buf->start + output_len ) % buf->capacity;
245 }
246
Andrzej Kurekf7774142020-01-22 06:34:59 -0500247 if( output != NULL )
248 {
249 memcpy( output, buf->buffer + buf->start, output_len - overflow );
250 memcpy( output + output_len - overflow, buf->buffer, overflow );
251 }
252
Janos Follath6264e662019-11-26 11:11:15 +0000253 buf->content_length -= output_len;
254 buf->start = ( buf->start + output_len ) % buf->capacity;
255
256 return output_len;
257}
258
Hanno Beckera18d1322018-01-03 14:27:32 +0000259/*
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500260 * Errors used in the message transport mock tests
261 */
262 #define MBEDTLS_TEST_ERROR_ARG_NULL -11
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500263 #define MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED -44
264
265/*
266 * Context for a message metadata queue (fifo) that is on top of the ring buffer.
267 */
268typedef struct mbedtls_test_message_queue
269{
270 size_t *messages;
271 int pos;
272 int num;
273 int capacity;
274} mbedtls_test_message_queue;
275
276/*
277 * Setup and free functions for the message metadata queue.
278 *
279 * \p capacity describes the number of message metadata chunks that can be held
280 * within the queue.
281 *
282 * \retval 0, if a metadata queue of a given length can be allocated.
283 * \retval MBEDTLS_ERR_SSL_ALLOC_FAILED, if allocation failed.
284 */
285int mbedtls_test_message_queue_setup( mbedtls_test_message_queue *queue,
286 size_t capacity )
287{
288 queue->messages = (size_t*) mbedtls_calloc( capacity, sizeof(size_t) );
289 if( NULL == queue->messages )
290 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
291
292 queue->capacity = capacity;
293 queue->pos = 0;
294 queue->num = 0;
295
296 return 0;
297}
298
299void mbedtls_test_message_queue_free( mbedtls_test_message_queue *queue )
300{
301 if( queue == NULL )
302 return;
303
304 if( queue->messages != NULL )
305 mbedtls_free( queue->messages );
306
307 memset( queue, 0, sizeof( *queue ) );
308}
309
310/*
311 * Push message length information onto the message metadata queue.
312 * This will become the last element to leave it (fifo).
313 *
314 * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null.
Andrzej Kurekf46b9122020-02-07 08:19:00 -0500315 * \retval MBEDTLS_ERR_SSL_WANT_WRITE, if the queue is full.
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500316 * \retval \p len, if the push was successful.
317 */
318int mbedtls_test_message_queue_push_info( mbedtls_test_message_queue *queue,
319 size_t len )
320{
321 int place;
322 if( queue == NULL )
323 return MBEDTLS_TEST_ERROR_ARG_NULL;
324
325 if( queue->num >= queue->capacity )
Andrzej Kurekf46b9122020-02-07 08:19:00 -0500326 return MBEDTLS_ERR_SSL_WANT_WRITE;
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500327
328 place = ( queue->pos + queue->num ) % queue->capacity;
329 queue->messages[place] = len;
330 queue->num++;
331 return len;
332}
333
334/*
335 * Pop information about the next message length from the queue. This will be
336 * the oldest inserted message length(fifo). \p msg_len can be null, in which
337 * case the data will be popped from the queue but not copied anywhere.
338 *
339 * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null.
Andrzej Kurekf46b9122020-02-07 08:19:00 -0500340 * \retval MBEDTLS_ERR_SSL_WANT_READ, if the queue is empty.
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500341 * \retval message length, if the pop was successful, up to the given
342 \p buf_len.
343 */
344int mbedtls_test_message_queue_pop_info( mbedtls_test_message_queue *queue,
345 size_t buf_len )
346{
347 size_t message_length;
348 if( queue == NULL )
349 return MBEDTLS_TEST_ERROR_ARG_NULL;
350 if( queue->num == 0 )
Andrzej Kurekf46b9122020-02-07 08:19:00 -0500351 return MBEDTLS_ERR_SSL_WANT_READ;
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500352
353 message_length = queue->messages[queue->pos];
354 queue->messages[queue->pos] = 0;
355 queue->num--;
356 queue->pos++;
357 queue->pos %= queue->capacity;
358 if( queue->pos < 0 )
359 queue->pos += queue->capacity;
360
361 return ( message_length > buf_len ) ? buf_len : message_length;
362}
363
364/*
365 * Take a peek on the info about the next message length from the queue.
366 * This will be the oldest inserted message length(fifo).
367 *
368 * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null.
Andrzej Kurekf46b9122020-02-07 08:19:00 -0500369 * \retval MBEDTLS_ERR_SSL_WANT_READ, if the queue is empty.
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500370 * \retval 0, if the peek was successful.
371 * \retval MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED, if the given buffer length is
372 * too small to fit the message. In this case the \p msg_len will be
373 * set to the full message length so that the
374 * caller knows what portion of the message can be dropped.
375 */
376int mbedtls_test_message_queue_peek_info( mbedtls_test_message_queue *queue,
377 size_t buf_len, size_t* msg_len )
378{
379 if( queue == NULL || msg_len == NULL )
380 return MBEDTLS_TEST_ERROR_ARG_NULL;
381 if( queue->num == 0 )
Andrzej Kurekf46b9122020-02-07 08:19:00 -0500382 return MBEDTLS_ERR_SSL_WANT_READ;
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500383
384 *msg_len = queue->messages[queue->pos];
385 return ( *msg_len > buf_len ) ? MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED : 0;
386}
387/*
Janos Follath031827f2019-11-27 11:12:14 +0000388 * Context for the I/O callbacks simulating network connection.
389 */
390
391#define MBEDTLS_MOCK_SOCKET_CONNECTED 1
392
393typedef struct mbedtls_mock_socket
394{
395 int status;
396 mbedtls_test_buffer *input;
397 mbedtls_test_buffer *output;
398 struct mbedtls_mock_socket *peer;
399} mbedtls_mock_socket;
400
401/*
402 * Setup and teardown functions for mock sockets.
403 */
404void mbedtls_mock_socket_init( mbedtls_mock_socket *socket )
405{
406 memset( socket, 0, sizeof( *socket ) );
407}
408
409/*
410 * Closes the socket \p socket.
411 *
412 * \p socket must have been previously initialized by calling
413 * mbedtls_mock_socket_init().
414 *
415 * This function frees all allocated resources and both sockets are aware of the
416 * new connection state.
417 *
418 * That is, this function does not simulate half-open TCP connections and the
419 * phenomenon that when closing a UDP connection the peer is not aware of the
420 * connection having been closed.
421 */
422void mbedtls_mock_socket_close( mbedtls_mock_socket* socket )
423{
424 if( socket == NULL )
425 return;
426
427 if( socket->input != NULL )
428 {
429 mbedtls_test_buffer_free( socket->input );
430 mbedtls_free( socket->input );
431 }
432
433 if( socket->output != NULL )
434 {
435 mbedtls_test_buffer_free( socket->output );
436 mbedtls_free( socket->output );
437 }
438
439 if( socket->peer != NULL )
440 memset( socket->peer, 0, sizeof( *socket->peer ) );
441
442 memset( socket, 0, sizeof( *socket ) );
443}
444
445/*
446 * Establishes a connection between \p peer1 and \p peer2.
447 *
448 * \p peer1 and \p peer2 must have been previously initialized by calling
449 * mbedtls_mock_socket_init().
450 *
451 * The capacites of the internal buffers are set to \p bufsize. Setting this to
452 * the correct value allows for simulation of MTU, sanity testing the mock
453 * implementation and mocking TCP connections with lower memory cost.
454 */
455int mbedtls_mock_socket_connect( mbedtls_mock_socket* peer1,
456 mbedtls_mock_socket* peer2,
457 size_t bufsize )
458{
459 int ret = -1;
460
Piotr Nowickid796e192020-01-28 12:09:47 +0100461 peer1->output =
Janos Follath031827f2019-11-27 11:12:14 +0000462 (mbedtls_test_buffer*) mbedtls_calloc( 1, sizeof(mbedtls_test_buffer) );
463 if( peer1->output == NULL )
464 {
465 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
466 goto exit;
467 }
468 mbedtls_test_buffer_init( peer1->output );
469 if( 0 != ( ret = mbedtls_test_buffer_setup( peer1->output, bufsize ) ) )
470 {
471 goto exit;
472 }
473
Piotr Nowickid796e192020-01-28 12:09:47 +0100474 peer2->output =
475 (mbedtls_test_buffer*) mbedtls_calloc( 1, sizeof(mbedtls_test_buffer) );
476 if( peer2->output == NULL )
477 {
478 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
479 goto exit;
480 }
481 mbedtls_test_buffer_init( peer2->output );
482 if( 0 != ( ret = mbedtls_test_buffer_setup( peer2->output, bufsize ) ) )
483 {
484 goto exit;
485 }
486
Janos Follath031827f2019-11-27 11:12:14 +0000487 peer1->peer = peer2;
488 peer2->peer = peer1;
Piotr Nowickid796e192020-01-28 12:09:47 +0100489 peer1->input = peer2->output;
490 peer2->input = peer1->output;
Janos Follath031827f2019-11-27 11:12:14 +0000491
492 peer1->status = peer2->status = MBEDTLS_MOCK_SOCKET_CONNECTED;
493 ret = 0;
494
495exit:
496
497 if( ret != 0 )
498 {
499 mbedtls_mock_socket_close( peer1 );
500 mbedtls_mock_socket_close( peer2 );
501 }
502
503 return ret;
504}
505
506/*
507 * Callbacks for simulating blocking I/O over connection-oriented transport.
508 */
509
510int mbedtls_mock_tcp_send_b( void *ctx, const unsigned char *buf, size_t len )
511{
512 mbedtls_mock_socket *socket = (mbedtls_mock_socket*) ctx;
513
514 if( socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED )
515 return -1;
516
517 return mbedtls_test_buffer_put( socket->output, buf, len );
518}
519
520int mbedtls_mock_tcp_recv_b( void *ctx, unsigned char *buf, size_t len )
521{
522 mbedtls_mock_socket *socket = (mbedtls_mock_socket*) ctx;
523
524 if( socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED )
525 return -1;
526
527 return mbedtls_test_buffer_get( socket->input, buf, len );
528}
529
530/*
Janos Follath3766ba52019-11-27 13:31:42 +0000531 * Callbacks for simulating non-blocking I/O over connection-oriented transport.
532 */
533
534int mbedtls_mock_tcp_send_nb( void *ctx, const unsigned char *buf, size_t len )
535{
536 mbedtls_mock_socket *socket = (mbedtls_mock_socket*) ctx;
537
538 if( socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED )
539 return -1;
540
Piotr Nowicki890b5ca2020-01-15 16:19:07 +0100541 if( socket->output->capacity == socket->output->content_length )
Janos Follath3766ba52019-11-27 13:31:42 +0000542 {
Janos Follath3766ba52019-11-27 13:31:42 +0000543 return MBEDTLS_ERR_SSL_WANT_WRITE;
544 }
545
Janos Follath3766ba52019-11-27 13:31:42 +0000546 return mbedtls_test_buffer_put( socket->output, buf, len );
547}
548
549int mbedtls_mock_tcp_recv_nb( void *ctx, unsigned char *buf, size_t len )
550{
551 mbedtls_mock_socket *socket = (mbedtls_mock_socket*) ctx;
552
553 if( socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED )
554 return -1;
555
Andrzej Kurekf40daa32020-02-04 09:00:01 -0500556 if( socket->input->content_length == 0 )
Janos Follath3766ba52019-11-27 13:31:42 +0000557 {
Janos Follath3766ba52019-11-27 13:31:42 +0000558 return MBEDTLS_ERR_SSL_WANT_READ;
559 }
560
Janos Follath3766ba52019-11-27 13:31:42 +0000561 return mbedtls_test_buffer_get( socket->input, buf, len );
562}
563
Andrzej Kurekbc483de2020-01-22 03:40:00 -0500564/* Errors used in the message socket mocks */
565
566#define MBEDTLS_TEST_ERROR_CONTEXT_ERROR -55
567#define MBEDTLS_TEST_ERROR_SEND_FAILED -66
568#define MBEDTLS_TEST_ERROR_RECV_FAILED -77
569
570/*
571 * Structure used as an addon, or a wrapper, around the mocked sockets.
572 * Contains an input queue, to which the other socket pushes metadata,
573 * and an output queue, to which this one pushes metadata. This context is
574 * considered as an owner of the input queue only, which is initialized and
575 * freed in the respective setup and free calls.
576 */
577typedef struct mbedtls_test_message_socket_context
578{
579 mbedtls_test_message_queue* queue_input;
580 mbedtls_test_message_queue* queue_output;
581 mbedtls_mock_socket* socket;
582} mbedtls_test_message_socket_context;
583
Andrzej Kurek45916ba2020-03-05 14:46:22 -0500584void mbedtls_message_socket_init( mbedtls_test_message_socket_context *ctx )
585{
586 ctx->queue_input = NULL;
587 ctx->queue_output = NULL;
588 ctx->socket = NULL;
589}
590
Andrzej Kurekbc483de2020-01-22 03:40:00 -0500591/*
592 * Setup a given mesasge socket context including initialization of
593 * input/output queues to a chosen capacity of messages. Also set the
594 * corresponding mock socket.
595 *
596 * \retval 0, if everything succeeds.
597 * \retval MBEDTLS_ERR_SSL_ALLOC_FAILED, if allocation of a message
598 * queue failed.
599 */
600int mbedtls_message_socket_setup( mbedtls_test_message_queue* queue_input,
601 mbedtls_test_message_queue* queue_output,
602 size_t queue_capacity,
603 mbedtls_mock_socket* socket,
604 mbedtls_test_message_socket_context* ctx )
605{
606 int ret = mbedtls_test_message_queue_setup( queue_input, queue_capacity );
607 if( ret != 0 )
608 return ret;
609 ctx->queue_input = queue_input;
610 ctx->queue_output = queue_output;
611 ctx->socket = socket;
612 mbedtls_mock_socket_init( socket );
613
614 return 0;
615}
616
617/*
618 * Close a given message socket context, along with the socket itself. Free the
619 * memory allocated by the input queue.
620 */
621void mbedtls_message_socket_close( mbedtls_test_message_socket_context* ctx )
622{
623 if( ctx == NULL )
624 return;
625
626 mbedtls_test_message_queue_free( ctx->queue_input );
627 mbedtls_mock_socket_close( ctx->socket );
628 memset( ctx, 0, sizeof( *ctx ) );
629}
630
631/*
632 * Send one message through a given message socket context.
633 *
634 * \retval \p len, if everything succeeds.
635 * \retval MBEDTLS_TEST_ERROR_CONTEXT_ERROR, if any of the needed context
636 * elements or the context itself is null.
637 * \retval MBEDTLS_TEST_ERROR_SEND_FAILED if mbedtls_mock_tcp_send_b failed.
Andrzej Kurekf46b9122020-02-07 08:19:00 -0500638 * \retval MBEDTLS_ERR_SSL_WANT_WRITE, if the output queue is full.
Andrzej Kurekbc483de2020-01-22 03:40:00 -0500639 *
640 * This function will also return any error from
641 * mbedtls_test_message_queue_push_info.
642 */
643int mbedtls_mock_tcp_send_msg( void *ctx, const unsigned char *buf, size_t len )
644{
645 mbedtls_test_message_queue* queue;
646 mbedtls_mock_socket* socket;
647 mbedtls_test_message_socket_context *context = (mbedtls_test_message_socket_context*) ctx;
648
649 if( context == NULL || context->socket == NULL
650 || context->queue_output == NULL )
651 {
652 return MBEDTLS_TEST_ERROR_CONTEXT_ERROR;
653 }
654
655 queue = context->queue_output;
656 socket = context->socket;
657
658 if( queue->num >= queue->capacity )
Andrzej Kurekf46b9122020-02-07 08:19:00 -0500659 return MBEDTLS_ERR_SSL_WANT_WRITE;
Andrzej Kurekbc483de2020-01-22 03:40:00 -0500660
661 if( mbedtls_mock_tcp_send_b( socket, buf, len ) != (int) len )
662 return MBEDTLS_TEST_ERROR_SEND_FAILED;
663
664 return mbedtls_test_message_queue_push_info( queue, len );
665}
666
667/*
668 * Receive one message from a given message socket context and return message
669 * length or an error.
670 *
671 * \retval message length, if everything succeeds.
672 * \retval MBEDTLS_TEST_ERROR_CONTEXT_ERROR, if any of the needed context
673 * elements or the context itself is null.
674 * \retval MBEDTLS_TEST_ERROR_RECV_FAILED if mbedtls_mock_tcp_recv_b failed.
675 *
676 * This function will also return any error other than
677 * MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED from mbedtls_test_message_queue_peek_info.
678 */
679int mbedtls_mock_tcp_recv_msg( void *ctx, unsigned char *buf, size_t buf_len )
680{
681 mbedtls_test_message_queue* queue;
682 mbedtls_mock_socket* socket;
683 mbedtls_test_message_socket_context *context = (mbedtls_test_message_socket_context*) ctx;
Gilles Peskine19e841e2020-03-09 20:43:51 +0100684 size_t drop_len = 0;
Andrzej Kurekbc483de2020-01-22 03:40:00 -0500685 size_t msg_len;
686 int ret;
687
688 if( context == NULL || context->socket == NULL
689 || context->queue_input == NULL )
690 {
691 return MBEDTLS_TEST_ERROR_CONTEXT_ERROR;
692 }
693
694 queue = context->queue_input;
695 socket = context->socket;
696
697 /* Peek first, so that in case of a socket error the data remains in
698 * the queue. */
699 ret = mbedtls_test_message_queue_peek_info( queue, buf_len, &msg_len );
700 if( ret == MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED )
701 {
702 /* Calculate how much to drop */
703 drop_len = msg_len - buf_len;
704
705 /* Set the requested message len to be buffer length */
706 msg_len = buf_len;
707 } else if( ret != 0 )
708 {
709 return ret;
710 }
711
712 if( mbedtls_mock_tcp_recv_b( socket, buf, msg_len ) != (int) msg_len )
713 return MBEDTLS_TEST_ERROR_RECV_FAILED;
714
715 if( ret == MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED )
716 {
717 /* Drop the remaining part of the message */
718 if( mbedtls_mock_tcp_recv_b( socket, NULL, drop_len ) != (int) drop_len )
719 {
720 /* Inconsistent state - part of the message was read,
721 * and a part couldn't. Not much we can do here, but it should not
722 * happen in test environment, unless forced manually. */
723 }
724 }
725 mbedtls_test_message_queue_pop_info( queue, buf_len );
726
727 return msg_len;
728}
729
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +0200730#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
731 defined(MBEDTLS_ENTROPY_C) && \
732 defined(MBEDTLS_CTR_DRBG_C)
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100733
734/*
735 * Structure with endpoint's certificates for SSL communication tests.
736 */
737typedef struct mbedtls_endpoint_certificate
738{
739 mbedtls_x509_crt ca_cert;
740 mbedtls_x509_crt cert;
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100741 mbedtls_pk_context pkey;
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100742} mbedtls_endpoint_certificate;
743
744/*
745 * Endpoint structure for SSL communication tests.
746 */
747typedef struct mbedtls_endpoint
748{
749 const char *name;
750 mbedtls_ssl_context ssl;
751 mbedtls_ssl_config conf;
752 mbedtls_ctr_drbg_context ctr_drbg;
753 mbedtls_entropy_context entropy;
754 mbedtls_mock_socket socket;
755 mbedtls_endpoint_certificate cert;
756} mbedtls_endpoint;
757
758/*
759 * Initializes \p ep_cert structure and assigns it to endpoint
760 * represented by \p ep.
761 *
762 * \retval 0 on success, otherwise error code.
763 */
Andrzej Kurekb2980742020-02-02 19:25:26 -0500764int mbedtls_endpoint_certificate_init( mbedtls_endpoint *ep, int pk_alg )
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100765{
766 int i = 0;
767 int ret = -1;
768 mbedtls_endpoint_certificate *cert;
769
770 if( ep == NULL )
771 {
772 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
773 }
774
775 cert = &( ep->cert );
776 mbedtls_x509_crt_init( &( cert->ca_cert ) );
777 mbedtls_x509_crt_init( &( cert->cert ) );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100778 mbedtls_pk_init( &( cert->pkey ) );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100779
780 /* Load the trusted CA */
781
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100782 for( i = 0; mbedtls_test_cas_der[i] != NULL; i++ )
783 {
784 ret = mbedtls_x509_crt_parse_der( &( cert->ca_cert ),
785 (const unsigned char *) mbedtls_test_cas_der[i],
786 mbedtls_test_cas_der_len[i] );
787 TEST_ASSERT( ret == 0 );
788 }
789
790 /* Load own certificate and private key */
791
792 if( ep->conf.endpoint == MBEDTLS_SSL_IS_SERVER )
793 {
Andrzej Kurekb2980742020-02-02 19:25:26 -0500794 if( pk_alg == MBEDTLS_PK_RSA )
795 {
796 ret = mbedtls_x509_crt_parse( &( cert->cert ),
797 (const unsigned char*) mbedtls_test_srv_crt_rsa_sha256_der,
798 mbedtls_test_srv_crt_rsa_sha256_der_len );
799 TEST_ASSERT( ret == 0 );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100800
Andrzej Kurekb2980742020-02-02 19:25:26 -0500801 ret = mbedtls_pk_parse_key( &( cert->pkey ),
802 (const unsigned char*) mbedtls_test_srv_key_rsa_der,
803 mbedtls_test_srv_key_rsa_der_len, NULL, 0 );
804 TEST_ASSERT( ret == 0 );
805 }
806 else
807 {
808 ret = mbedtls_x509_crt_parse( &( cert->cert ),
809 (const unsigned char*) mbedtls_test_srv_crt_ec_der,
810 mbedtls_test_srv_crt_ec_der_len );
811 TEST_ASSERT( ret == 0 );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100812
Andrzej Kurekb2980742020-02-02 19:25:26 -0500813 ret = mbedtls_pk_parse_key( &( cert->pkey ),
814 (const unsigned char*) mbedtls_test_srv_key_ec_der,
815 mbedtls_test_srv_key_ec_der_len, NULL, 0 );
816 TEST_ASSERT( ret == 0 );
817 }
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100818 }
819 else
820 {
Andrzej Kurekb2980742020-02-02 19:25:26 -0500821 if( pk_alg == MBEDTLS_PK_RSA )
822 {
823 ret = mbedtls_x509_crt_parse( &( cert->cert ),
824 (const unsigned char *) mbedtls_test_cli_crt_rsa_der,
825 mbedtls_test_cli_crt_rsa_der_len );
826 TEST_ASSERT( ret == 0 );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100827
Andrzej Kurekb2980742020-02-02 19:25:26 -0500828 ret = mbedtls_pk_parse_key( &( cert->pkey ),
829 (const unsigned char *) mbedtls_test_cli_key_rsa_der,
830 mbedtls_test_cli_key_rsa_der_len, NULL, 0 );
831 TEST_ASSERT( ret == 0 );
832 }
833 else
834 {
835 ret = mbedtls_x509_crt_parse( &( cert->cert ),
836 (const unsigned char *) mbedtls_test_cli_crt_ec_der,
837 mbedtls_test_cli_crt_ec_len );
838 TEST_ASSERT( ret == 0 );
839
840 ret = mbedtls_pk_parse_key( &( cert->pkey ),
841 (const unsigned char *) mbedtls_test_cli_key_ec_der,
842 mbedtls_test_cli_key_ec_der_len, NULL, 0 );
843 TEST_ASSERT( ret == 0 );
844 }
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100845 }
846
847 mbedtls_ssl_conf_ca_chain( &( ep->conf ), &( cert->ca_cert ), NULL );
848
Andrzej Kurekb2980742020-02-02 19:25:26 -0500849 ret = mbedtls_ssl_conf_own_cert( &( ep->conf ), &( cert->cert ),
850 &( cert->pkey ) );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100851 TEST_ASSERT( ret == 0 );
852
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100853exit:
854 if( ret != 0 )
855 {
856 mbedtls_x509_crt_free( &( cert->ca_cert ) );
857 mbedtls_x509_crt_free( &( cert->cert ) );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100858 mbedtls_pk_free( &( cert->pkey ) );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100859 }
860
861 return ret;
862}
863
864/*
865 * Initializes \p ep structure. It is important to call `mbedtls_endpoint_free()`
866 * after calling this function even if it fails.
867 *
868 * \p endpoint_type must be set as MBEDTLS_SSL_IS_SERVER or
869 * MBEDTLS_SSL_IS_CLIENT.
Andrzej Kurek15daf502020-02-12 09:17:52 -0500870 * \p pk_alg the algorithm to use, currently only MBEDTLS_PK_RSA and
871 * MBEDTLS_PK_ECDSA are supported.
872 * \p dtls_context - in case of DTLS - this is the context handling metadata.
873 * \p input_queue - used only in case of DTLS.
874 * \p output_queue - used only in case of DTLS.
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100875 *
876 * \retval 0 on success, otherwise error code.
877 */
Andrzej Kurek15daf502020-02-12 09:17:52 -0500878int mbedtls_endpoint_init( mbedtls_endpoint *ep, int endpoint_type, int pk_alg,
879 mbedtls_test_message_socket_context *dtls_context,
880 mbedtls_test_message_queue *input_queue,
881 mbedtls_test_message_queue *output_queue )
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100882{
883 int ret = -1;
884
Andrzej Kurek15daf502020-02-12 09:17:52 -0500885 if( dtls_context != NULL && ( input_queue == NULL || output_queue == NULL ) )
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100886 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Andrzej Kurek15daf502020-02-12 09:17:52 -0500887
888 if( ep == NULL )
889 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100890
891 memset( ep, 0, sizeof( *ep ) );
892
893 ep->name = ( endpoint_type == MBEDTLS_SSL_IS_SERVER ) ? "Server" : "Client";
894
895 mbedtls_ssl_init( &( ep->ssl ) );
896 mbedtls_ssl_config_init( &( ep->conf ) );
897 mbedtls_ctr_drbg_init( &( ep->ctr_drbg ) );
898 mbedtls_ssl_conf_rng( &( ep->conf ),
899 mbedtls_ctr_drbg_random,
900 &( ep->ctr_drbg ) );
901 mbedtls_entropy_init( &( ep->entropy ) );
Andrzej Kurek15daf502020-02-12 09:17:52 -0500902 if( dtls_context != NULL )
903 {
904 TEST_ASSERT( mbedtls_message_socket_setup( input_queue, output_queue,
905 100, &( ep->socket ),
906 dtls_context ) == 0 );
907 }
908 else
909 {
910 mbedtls_mock_socket_init( &( ep->socket ) );
911 }
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100912
913 ret = mbedtls_ctr_drbg_seed( &( ep->ctr_drbg ), mbedtls_entropy_func,
914 &( ep->entropy ), (const unsigned char *) ( ep->name ),
915 strlen( ep->name ) );
916 TEST_ASSERT( ret == 0 );
917
918 /* Non-blocking callbacks without timeout */
Andrzej Kurek15daf502020-02-12 09:17:52 -0500919 if( dtls_context != NULL )
920 {
921 mbedtls_ssl_set_bio( &( ep->ssl ), dtls_context,
922 mbedtls_mock_tcp_send_msg,
923 mbedtls_mock_tcp_recv_msg,
924 NULL );
925 }
926 else
927 {
928 mbedtls_ssl_set_bio( &( ep->ssl ), &( ep->socket ),
929 mbedtls_mock_tcp_send_nb,
930 mbedtls_mock_tcp_recv_nb,
931 NULL );
932 }
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100933
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100934 ret = mbedtls_ssl_config_defaults( &( ep->conf ), endpoint_type,
Andrzej Kurek15daf502020-02-12 09:17:52 -0500935 ( dtls_context != NULL ) ?
936 MBEDTLS_SSL_TRANSPORT_DATAGRAM :
937 MBEDTLS_SSL_TRANSPORT_STREAM,
938 MBEDTLS_SSL_PRESET_DEFAULT );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100939 TEST_ASSERT( ret == 0 );
940
Andrzej Kurek1a44a152020-02-07 08:21:32 -0500941 ret = mbedtls_ssl_setup( &( ep->ssl ), &( ep->conf ) );
942 TEST_ASSERT( ret == 0 );
Andrzej Kurek15daf502020-02-12 09:17:52 -0500943
944#if defined(MBEDTLS_SSL_PROTO_DTLS) && defined(MBEDTLS_SSL_SRV_C)
945 if( endpoint_type == MBEDTLS_SSL_IS_SERVER && dtls_context != NULL )
946 mbedtls_ssl_conf_dtls_cookies( &( ep->conf ), NULL, NULL, NULL );
947#endif
948
Andrzej Kurekb2980742020-02-02 19:25:26 -0500949 ret = mbedtls_endpoint_certificate_init( ep, pk_alg );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100950 TEST_ASSERT( ret == 0 );
951
952exit:
953 return ret;
954}
955
956/*
957 * Deinitializes certificates from endpoint represented by \p ep.
958 */
959void mbedtls_endpoint_certificate_free( mbedtls_endpoint *ep )
960{
961 mbedtls_endpoint_certificate *cert = &( ep->cert );
962 mbedtls_x509_crt_free( &( cert->ca_cert ) );
963 mbedtls_x509_crt_free( &( cert->cert ) );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100964 mbedtls_pk_free( &( cert->pkey ) );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100965}
966
967/*
968 * Deinitializes endpoint represented by \p ep.
969 */
Andrzej Kurek15daf502020-02-12 09:17:52 -0500970void mbedtls_endpoint_free( mbedtls_endpoint *ep,
971 mbedtls_test_message_socket_context *context )
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100972{
973 mbedtls_endpoint_certificate_free( ep );
974
975 mbedtls_ssl_free( &( ep->ssl ) );
976 mbedtls_ssl_config_free( &( ep->conf ) );
977 mbedtls_ctr_drbg_free( &( ep->ctr_drbg ) );
978 mbedtls_entropy_free( &( ep->entropy ) );
Andrzej Kurek15daf502020-02-12 09:17:52 -0500979
980 if( context != NULL )
981 {
982 mbedtls_message_socket_close( context );
983 }
984 else
985 {
986 mbedtls_mock_socket_close( &( ep->socket ) );
987 }
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100988}
989
990/*
991 * This function moves ssl handshake from \p ssl to prescribed \p state.
992 * /p second_ssl is used as second endpoint and their sockets have to be
993 * connected before calling this function.
994 *
995 * \retval 0 on success, otherwise error code.
996 */
997int mbedtls_move_handshake_to_state( mbedtls_ssl_context *ssl,
998 mbedtls_ssl_context *second_ssl,
999 int state )
1000{
1001 enum { BUFFSIZE = 1024 };
1002 int max_steps = 1000;
1003 int ret = 0;
1004
1005 if( ssl == NULL || second_ssl == NULL )
1006 {
1007 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
1008 }
1009
1010 /* Perform communication via connected sockets */
1011 while( ( ssl->state != state ) && ( --max_steps >= 0 ) )
1012 {
1013 /* If /p second_ssl ends the handshake procedure before /p ssl then
1014 * there is no need to call the next step */
1015 if( second_ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
1016 {
1017 ret = mbedtls_ssl_handshake_step( second_ssl );
1018 if( ret != 0 && ret != MBEDTLS_ERR_SSL_WANT_READ &&
1019 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
1020 {
1021 return ret;
1022 }
1023 }
1024
1025 /* We only care about the \p ssl state and returns, so we call it last,
1026 * to leave the iteration as soon as the state is as expected. */
1027 ret = mbedtls_ssl_handshake_step( ssl );
1028 if( ret != 0 && ret != MBEDTLS_ERR_SSL_WANT_READ &&
1029 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
1030 {
1031 return ret;
1032 }
1033 }
1034
1035 return ( max_steps >= 0 ) ? ret : -1;
1036}
1037
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02001038#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01001039
Janos Follath3766ba52019-11-27 13:31:42 +00001040/*
Piotr Nowicki438bf3b2020-03-10 12:59:10 +01001041 * Write application data. Increase write counter if necessary.
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001042 */
1043int mbedtls_ssl_write_fragment( mbedtls_ssl_context *ssl, unsigned char *buf,
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001044 int buf_len, int *written,
Piotr Nowicki438bf3b2020-03-10 12:59:10 +01001045 const int expected_fragments )
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001046{
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001047 int ret = mbedtls_ssl_write( ssl, buf + *written, buf_len - *written );
1048 if( ret > 0 )
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001049 {
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001050 *written += ret;
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001051 }
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001052
1053 if( expected_fragments == 0 )
1054 {
1055 /* Used for DTLS and the message size larger than MFL. In that case
1056 * the message can not be fragmented and the library should return
1057 * MBEDTLS_ERR_SSL_BAD_INPUT_DATA error. This error must be returned
1058 * to prevent a dead loop inside mbedtls_exchange_data(). */
1059 return ret;
1060 }
1061 else if( expected_fragments == 1 )
1062 {
1063 /* Used for TLS/DTLS and the message size lower than MFL */
1064 TEST_ASSERT( ret == buf_len ||
1065 ret == MBEDTLS_ERR_SSL_WANT_READ ||
1066 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
1067 }
1068 else
1069 {
1070 /* Used for TLS and the message size larger than MFL */
1071 TEST_ASSERT( expected_fragments > 1 );
1072 TEST_ASSERT( ( ret >= 0 && ret <= buf_len ) ||
1073 ret == MBEDTLS_ERR_SSL_WANT_READ ||
1074 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
1075 }
1076
1077 return 0;
1078
1079exit:
1080 /* Some of the tests failed */
1081 return -1;
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001082}
1083
1084/*
Piotr Nowicki438bf3b2020-03-10 12:59:10 +01001085 * Read application data and increase read counter and fragments counter if necessary.
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001086 */
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001087int mbedtls_ssl_read_fragment( mbedtls_ssl_context *ssl, unsigned char *buf,
1088 int buf_len, int *read,
Piotr Nowicki438bf3b2020-03-10 12:59:10 +01001089 int *fragments, const int expected_fragments )
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001090{
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001091 int ret = mbedtls_ssl_read( ssl, buf + *read, buf_len - *read );
1092 if( ret > 0 )
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001093 {
Piotr Nowicki438bf3b2020-03-10 12:59:10 +01001094 ( *fragments )++;
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001095 *read += ret;
1096 }
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001097
1098 if( expected_fragments == 0 )
1099 {
1100 TEST_ASSERT( ret == 0 );
1101 }
1102 else if( expected_fragments == 1 )
1103 {
1104 TEST_ASSERT( ret == buf_len ||
1105 ret == MBEDTLS_ERR_SSL_WANT_READ ||
1106 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
1107 }
1108 else
1109 {
1110 TEST_ASSERT( expected_fragments > 1 );
1111 TEST_ASSERT( ( ret >= 0 && ret <= buf_len ) ||
1112 ret == MBEDTLS_ERR_SSL_WANT_READ ||
1113 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
1114 }
1115
1116 return 0;
1117
1118exit:
1119 /* Some of the tests failed */
1120 return -1;
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001121}
1122
1123/*
Hanno Beckera18d1322018-01-03 14:27:32 +00001124 * Helper function setting up inverse record transformations
1125 * using given cipher, hash, EtM mode, authentication tag length,
1126 * and version.
1127 */
1128
1129#define CHK( x ) \
1130 do \
1131 { \
1132 if( !( x ) ) \
Hanno Becker81e16a32019-03-01 11:21:44 +00001133 { \
Hanno Beckera5780f12019-04-05 09:55:37 +01001134 ret = -1; \
Hanno Becker81e16a32019-03-01 11:21:44 +00001135 goto cleanup; \
1136 } \
Hanno Beckera18d1322018-01-03 14:27:32 +00001137 } while( 0 )
1138
Andrzej Kurekf40daa32020-02-04 09:00:01 -05001139void set_ciphersuite( mbedtls_ssl_config *conf, const char *cipher,
1140 int* forced_ciphersuite )
1141{
1142 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
1143 forced_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id( cipher );
1144 forced_ciphersuite[1] = 0;
1145
1146 ciphersuite_info =
1147 mbedtls_ssl_ciphersuite_from_id( forced_ciphersuite[0] );
1148
1149 TEST_ASSERT( ciphersuite_info != NULL );
1150 TEST_ASSERT( ciphersuite_info->min_minor_ver <= conf->max_minor_ver );
1151 TEST_ASSERT( ciphersuite_info->max_minor_ver >= conf->min_minor_ver );
1152
1153 if( conf->max_minor_ver > ciphersuite_info->max_minor_ver )
1154 {
1155 conf->max_minor_ver = ciphersuite_info->max_minor_ver;
1156 }
1157 if( conf->min_minor_ver < ciphersuite_info->min_minor_ver )
1158 {
1159 conf->min_minor_ver = ciphersuite_info->min_minor_ver;
1160 }
1161
1162 mbedtls_ssl_conf_ciphersuites( conf, forced_ciphersuite );
1163
1164exit:
1165 return;
1166}
1167
Andrzej Kurekcc5169c2020-02-04 09:04:56 -05001168int psk_dummy_callback( void *p_info, mbedtls_ssl_context *ssl,
1169 const unsigned char *name, size_t name_len )
1170{
1171 (void) p_info;
1172 (void) ssl;
1173 (void) name;
1174 (void) name_len;
1175
1176 return ( 0 );
1177}
1178
Hanno Beckerd856c822019-04-29 17:30:59 +01001179#if MBEDTLS_SSL_CID_OUT_LEN_MAX > MBEDTLS_SSL_CID_IN_LEN_MAX
1180#define SSL_CID_LEN_MIN MBEDTLS_SSL_CID_IN_LEN_MAX
1181#else
1182#define SSL_CID_LEN_MIN MBEDTLS_SSL_CID_OUT_LEN_MAX
1183#endif
Hanno Beckera18d1322018-01-03 14:27:32 +00001184
1185static int build_transforms( mbedtls_ssl_transform *t_in,
1186 mbedtls_ssl_transform *t_out,
1187 int cipher_type, int hash_id,
Hanno Beckerd856c822019-04-29 17:30:59 +01001188 int etm, int tag_mode, int ver,
1189 size_t cid0_len,
1190 size_t cid1_len )
Hanno Beckera18d1322018-01-03 14:27:32 +00001191{
1192 mbedtls_cipher_info_t const *cipher_info;
Hanno Beckera5780f12019-04-05 09:55:37 +01001193 int ret = 0;
Hanno Beckera18d1322018-01-03 14:27:32 +00001194
1195 size_t keylen, maclen, ivlen;
Hanno Becker81e16a32019-03-01 11:21:44 +00001196 unsigned char *key0 = NULL, *key1 = NULL;
Paul Elliott6f1eda72020-06-11 20:22:00 +01001197 unsigned char *md0 = NULL, *md1 = NULL;
Hanno Beckera18d1322018-01-03 14:27:32 +00001198 unsigned char iv_enc[16], iv_dec[16];
1199
Hanno Beckera0e20d02019-05-15 14:03:01 +01001200#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd856c822019-04-29 17:30:59 +01001201 unsigned char cid0[ SSL_CID_LEN_MIN ];
1202 unsigned char cid1[ SSL_CID_LEN_MIN ];
1203
Ronald Cron351f0ee2020-06-10 12:12:18 +02001204 mbedtls_test_rnd_std_rand( NULL, cid0, sizeof( cid0 ) );
1205 mbedtls_test_rnd_std_rand( NULL, cid1, sizeof( cid1 ) );
Hanno Becker43c24b82019-05-01 09:45:57 +01001206#else
1207 ((void) cid0_len);
1208 ((void) cid1_len);
Hanno Beckera0e20d02019-05-15 14:03:01 +01001209#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerd856c822019-04-29 17:30:59 +01001210
Hanno Beckera18d1322018-01-03 14:27:32 +00001211 maclen = 0;
1212
1213 /* Pick cipher */
1214 cipher_info = mbedtls_cipher_info_from_type( cipher_type );
1215 CHK( cipher_info != NULL );
1216 CHK( cipher_info->iv_size <= 16 );
1217 CHK( cipher_info->key_bitlen % 8 == 0 );
1218
1219 /* Pick keys */
1220 keylen = cipher_info->key_bitlen / 8;
Hanno Becker78d1f702019-04-05 09:56:10 +01001221 /* Allocate `keylen + 1` bytes to ensure that we get
1222 * a non-NULL pointers from `mbedtls_calloc` even if
1223 * `keylen == 0` in the case of the NULL cipher. */
1224 CHK( ( key0 = mbedtls_calloc( 1, keylen + 1 ) ) != NULL );
1225 CHK( ( key1 = mbedtls_calloc( 1, keylen + 1 ) ) != NULL );
Hanno Beckera18d1322018-01-03 14:27:32 +00001226 memset( key0, 0x1, keylen );
1227 memset( key1, 0x2, keylen );
1228
1229 /* Setup cipher contexts */
1230 CHK( mbedtls_cipher_setup( &t_in->cipher_ctx_enc, cipher_info ) == 0 );
1231 CHK( mbedtls_cipher_setup( &t_in->cipher_ctx_dec, cipher_info ) == 0 );
1232 CHK( mbedtls_cipher_setup( &t_out->cipher_ctx_enc, cipher_info ) == 0 );
1233 CHK( mbedtls_cipher_setup( &t_out->cipher_ctx_dec, cipher_info ) == 0 );
1234
1235#if defined(MBEDTLS_CIPHER_MODE_CBC)
1236 if( cipher_info->mode == MBEDTLS_MODE_CBC )
1237 {
1238 CHK( mbedtls_cipher_set_padding_mode( &t_in->cipher_ctx_enc,
1239 MBEDTLS_PADDING_NONE ) == 0 );
1240 CHK( mbedtls_cipher_set_padding_mode( &t_in->cipher_ctx_dec,
1241 MBEDTLS_PADDING_NONE ) == 0 );
1242 CHK( mbedtls_cipher_set_padding_mode( &t_out->cipher_ctx_enc,
1243 MBEDTLS_PADDING_NONE ) == 0 );
1244 CHK( mbedtls_cipher_set_padding_mode( &t_out->cipher_ctx_dec,
1245 MBEDTLS_PADDING_NONE ) == 0 );
1246 }
1247#endif /* MBEDTLS_CIPHER_MODE_CBC */
1248
1249 CHK( mbedtls_cipher_setkey( &t_in->cipher_ctx_enc, key0,
1250 keylen << 3, MBEDTLS_ENCRYPT ) == 0 );
1251 CHK( mbedtls_cipher_setkey( &t_in->cipher_ctx_dec, key1,
1252 keylen << 3, MBEDTLS_DECRYPT ) == 0 );
1253 CHK( mbedtls_cipher_setkey( &t_out->cipher_ctx_enc, key1,
1254 keylen << 3, MBEDTLS_ENCRYPT ) == 0 );
1255 CHK( mbedtls_cipher_setkey( &t_out->cipher_ctx_dec, key0,
1256 keylen << 3, MBEDTLS_DECRYPT ) == 0 );
Hanno Beckera18d1322018-01-03 14:27:32 +00001257
1258 /* Setup MAC contexts */
Hanno Beckerfd86ca82020-11-30 08:54:23 +00001259#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Hanno Beckera18d1322018-01-03 14:27:32 +00001260 if( cipher_info->mode == MBEDTLS_MODE_CBC ||
1261 cipher_info->mode == MBEDTLS_MODE_STREAM )
1262 {
1263 mbedtls_md_info_t const *md_info;
Hanno Beckera18d1322018-01-03 14:27:32 +00001264
1265 /* Pick hash */
1266 md_info = mbedtls_md_info_from_type( hash_id );
1267 CHK( md_info != NULL );
1268
1269 /* Pick hash keys */
1270 maclen = mbedtls_md_get_size( md_info );
Hanno Becker3ee54212019-04-04 16:31:26 +01001271 CHK( ( md0 = mbedtls_calloc( 1, maclen ) ) != NULL );
1272 CHK( ( md1 = mbedtls_calloc( 1, maclen ) ) != NULL );
Hanno Beckera18d1322018-01-03 14:27:32 +00001273 memset( md0, 0x5, maclen );
1274 memset( md1, 0x6, maclen );
1275
1276 CHK( mbedtls_md_setup( &t_out->md_ctx_enc, md_info, 1 ) == 0 );
1277 CHK( mbedtls_md_setup( &t_out->md_ctx_dec, md_info, 1 ) == 0 );
1278 CHK( mbedtls_md_setup( &t_in->md_ctx_enc, md_info, 1 ) == 0 );
1279 CHK( mbedtls_md_setup( &t_in->md_ctx_dec, md_info, 1 ) == 0 );
1280
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001281 CHK( mbedtls_md_hmac_starts( &t_in->md_ctx_enc,
1282 md0, maclen ) == 0 );
1283 CHK( mbedtls_md_hmac_starts( &t_in->md_ctx_dec,
1284 md1, maclen ) == 0 );
1285 CHK( mbedtls_md_hmac_starts( &t_out->md_ctx_enc,
1286 md1, maclen ) == 0 );
1287 CHK( mbedtls_md_hmac_starts( &t_out->md_ctx_dec,
1288 md0, maclen ) == 0 );
Hanno Beckera18d1322018-01-03 14:27:32 +00001289 }
1290#else
1291 ((void) hash_id);
Hanno Beckerfd86ca82020-11-30 08:54:23 +00001292#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Hanno Beckera18d1322018-01-03 14:27:32 +00001293
1294
1295 /* Pick IV's (regardless of whether they
1296 * are being used by the transform). */
1297 ivlen = cipher_info->iv_size;
1298 memset( iv_enc, 0x3, sizeof( iv_enc ) );
1299 memset( iv_dec, 0x4, sizeof( iv_dec ) );
1300
1301 /*
1302 * Setup transforms
1303 */
1304
Jaeden Amero2de07f12019-06-05 13:32:08 +01001305#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
Hanno Beckerfd86ca82020-11-30 08:54:23 +00001306 defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Hanno Beckera18d1322018-01-03 14:27:32 +00001307 t_out->encrypt_then_mac = etm;
1308 t_in->encrypt_then_mac = etm;
1309#else
1310 ((void) etm);
1311#endif
1312
1313 t_out->minor_ver = ver;
1314 t_in->minor_ver = ver;
1315 t_out->ivlen = ivlen;
1316 t_in->ivlen = ivlen;
1317
1318 switch( cipher_info->mode )
1319 {
1320 case MBEDTLS_MODE_GCM:
1321 case MBEDTLS_MODE_CCM:
Hanno Beckere6832872020-05-28 08:29:58 +01001322#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
1323 if( ver == MBEDTLS_SSL_MINOR_VERSION_4 )
1324 {
1325 t_out->fixed_ivlen = 12;
1326 t_in->fixed_ivlen = 12;
1327 }
1328 else
1329#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
1330 {
1331 t_out->fixed_ivlen = 4;
1332 t_in->fixed_ivlen = 4;
1333 }
Hanno Beckera18d1322018-01-03 14:27:32 +00001334 t_out->maclen = 0;
1335 t_in->maclen = 0;
1336 switch( tag_mode )
1337 {
1338 case 0: /* Full tag */
1339 t_out->taglen = 16;
1340 t_in->taglen = 16;
1341 break;
1342 case 1: /* Partial tag */
1343 t_out->taglen = 8;
1344 t_in->taglen = 8;
1345 break;
1346 default:
Paul Elliottc7b53742021-02-03 13:18:33 +00001347 ret = 1;
1348 goto cleanup;
Hanno Beckera18d1322018-01-03 14:27:32 +00001349 }
1350 break;
1351
1352 case MBEDTLS_MODE_CHACHAPOLY:
1353 t_out->fixed_ivlen = 12;
1354 t_in->fixed_ivlen = 12;
1355 t_out->maclen = 0;
1356 t_in->maclen = 0;
1357 switch( tag_mode )
1358 {
1359 case 0: /* Full tag */
1360 t_out->taglen = 16;
1361 t_in->taglen = 16;
1362 break;
1363 case 1: /* Partial tag */
1364 t_out->taglen = 8;
1365 t_in->taglen = 8;
1366 break;
1367 default:
Paul Elliottc7b53742021-02-03 13:18:33 +00001368 ret = 1;
1369 goto cleanup;
Hanno Beckera18d1322018-01-03 14:27:32 +00001370 }
1371 break;
1372
1373 case MBEDTLS_MODE_STREAM:
1374 case MBEDTLS_MODE_CBC:
1375 t_out->fixed_ivlen = 0; /* redundant, must be 0 */
1376 t_in->fixed_ivlen = 0; /* redundant, must be 0 */
1377 t_out->taglen = 0;
1378 t_in->taglen = 0;
1379 switch( tag_mode )
1380 {
1381 case 0: /* Full tag */
1382 t_out->maclen = maclen;
1383 t_in->maclen = maclen;
1384 break;
1385 case 1: /* Partial tag */
1386 t_out->maclen = 10;
1387 t_in->maclen = 10;
1388 break;
1389 default:
Paul Elliottc7b53742021-02-03 13:18:33 +00001390 ret = 1;
1391 goto cleanup;
Hanno Beckera18d1322018-01-03 14:27:32 +00001392 }
1393 break;
1394 default:
Paul Elliottc7b53742021-02-03 13:18:33 +00001395 ret = 1;
1396 goto cleanup;
Hanno Beckera18d1322018-01-03 14:27:32 +00001397 break;
1398 }
1399
1400 /* Setup IV's */
1401
1402 memcpy( &t_in->iv_dec, iv_dec, sizeof( iv_dec ) );
1403 memcpy( &t_in->iv_enc, iv_enc, sizeof( iv_enc ) );
1404 memcpy( &t_out->iv_dec, iv_enc, sizeof( iv_enc ) );
1405 memcpy( &t_out->iv_enc, iv_dec, sizeof( iv_dec ) );
1406
Hanno Beckera0e20d02019-05-15 14:03:01 +01001407#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd856c822019-04-29 17:30:59 +01001408 /* Add CID */
1409 memcpy( &t_in->in_cid, cid0, cid0_len );
1410 memcpy( &t_in->out_cid, cid1, cid1_len );
1411 t_in->in_cid_len = cid0_len;
1412 t_in->out_cid_len = cid1_len;
1413 memcpy( &t_out->in_cid, cid1, cid1_len );
1414 memcpy( &t_out->out_cid, cid0, cid0_len );
1415 t_out->in_cid_len = cid1_len;
1416 t_out->out_cid_len = cid0_len;
Hanno Beckera0e20d02019-05-15 14:03:01 +01001417#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerd856c822019-04-29 17:30:59 +01001418
Hanno Becker81e16a32019-03-01 11:21:44 +00001419cleanup:
1420
Hanno Becker3ee54212019-04-04 16:31:26 +01001421 mbedtls_free( key0 );
1422 mbedtls_free( key1 );
Hanno Becker81e16a32019-03-01 11:21:44 +00001423
Paul Elliott6f1eda72020-06-11 20:22:00 +01001424 mbedtls_free( md0 );
1425 mbedtls_free( md1 );
1426
Hanno Beckera5780f12019-04-05 09:55:37 +01001427 return( ret );
Hanno Beckera18d1322018-01-03 14:27:32 +00001428}
1429
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001430/*
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02001431 * Populate a session structure for serialization tests.
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001432 * Choose dummy values, mostly non-0 to distinguish from the init default.
1433 */
1434static int ssl_populate_session( mbedtls_ssl_session *session,
Manuel Pégourié-Gonnard220403b2019-05-24 09:54:21 +02001435 int ticket_len,
1436 const char *crt_file )
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001437{
1438#if defined(MBEDTLS_HAVE_TIME)
1439 session->start = mbedtls_time( NULL ) - 42;
1440#endif
1441 session->ciphersuite = 0xabcd;
1442 session->compression = 1;
1443 session->id_len = sizeof( session->id );
1444 memset( session->id, 66, session->id_len );
Manuel Pégourié-Gonnard220403b2019-05-24 09:54:21 +02001445 memset( session->master, 17, sizeof( session->master ) );
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001446
Manuel Pégourié-Gonnard1f6033a2019-05-24 10:17:52 +02001447#if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001448 if( strlen( crt_file ) != 0 )
1449 {
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02001450 mbedtls_x509_crt tmp_crt;
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001451 int ret;
Manuel Pégourié-Gonnard6b840702019-05-24 09:40:17 +02001452
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02001453 mbedtls_x509_crt_init( &tmp_crt );
1454 ret = mbedtls_x509_crt_parse_file( &tmp_crt, crt_file );
1455 if( ret != 0 )
1456 return( ret );
1457
1458#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1459 /* Move temporary CRT. */
Manuel Pégourié-Gonnard6b840702019-05-24 09:40:17 +02001460 session->peer_cert = mbedtls_calloc( 1, sizeof( *session->peer_cert ) );
1461 if( session->peer_cert == NULL )
1462 return( -1 );
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02001463 *session->peer_cert = tmp_crt;
1464 memset( &tmp_crt, 0, sizeof( tmp_crt ) );
1465#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
1466 /* Calculate digest of temporary CRT. */
1467 session->peer_cert_digest =
1468 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
1469 if( session->peer_cert_digest == NULL )
1470 return( -1 );
1471 ret = mbedtls_md( mbedtls_md_info_from_type(
1472 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
1473 tmp_crt.raw.p, tmp_crt.raw.len,
1474 session->peer_cert_digest );
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001475 if( ret != 0 )
1476 return( ret );
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02001477 session->peer_cert_digest_type =
1478 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
1479 session->peer_cert_digest_len =
1480 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
1481#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
1482
1483 mbedtls_x509_crt_free( &tmp_crt );
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001484 }
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02001485#else /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_FS_IO */
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001486 (void) crt_file;
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02001487#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_FS_IO */
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001488 session->verify_result = 0xdeadbeef;
1489
1490#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
1491 if( ticket_len != 0 )
1492 {
1493 session->ticket = mbedtls_calloc( 1, ticket_len );
Manuel Pégourié-Gonnard220403b2019-05-24 09:54:21 +02001494 if( session->ticket == NULL )
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001495 return( -1 );
1496 memset( session->ticket, 33, ticket_len );
1497 }
1498 session->ticket_len = ticket_len;
1499 session->ticket_lifetime = 86401;
1500#else
1501 (void) ticket_len;
1502#endif
1503
1504#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1505 session->mfl_code = 1;
1506#endif
1507#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1508 session->trunc_hmac = 1;
1509#endif
1510#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1511 session->encrypt_then_mac = 1;
1512#endif
1513
1514 return( 0 );
1515}
1516
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001517/*
1518 * Perform data exchanging between \p ssl_1 and \p ssl_2 and check if the
1519 * message was sent in the correct number of fragments.
1520 *
1521 * /p ssl_1 and /p ssl_2 Endpoints represented by mbedtls_ssl_context. Both
1522 * of them must be initialized and connected beforehand.
1523 * /p msg_len_1 and /p msg_len_2 specify the size of the message to send.
1524 * /p expected_fragments_1 and /p expected_fragments_2 determine in how many
1525 * fragments the message should be sent.
1526 * expected_fragments is 0: can be used for DTLS testing while the message
1527 * size is larger than MFL. In that case the message
1528 * cannot be fragmented and sent to the second endpoint.
1529 * This value can be used for negative tests.
1530 * expected_fragments is 1: can be used for TLS/DTLS testing while the
1531 * message size is below MFL
1532 * expected_fragments > 1: can be used for TLS testing while the message
1533 * size is larger than MFL
1534 *
1535 * \retval 0 on success, otherwise error code.
1536 */
1537int mbedtls_exchange_data( mbedtls_ssl_context *ssl_1,
1538 int msg_len_1, const int expected_fragments_1,
1539 mbedtls_ssl_context *ssl_2,
1540 int msg_len_2, const int expected_fragments_2 )
1541{
1542 unsigned char *msg_buf_1 = malloc( msg_len_1 );
1543 unsigned char *msg_buf_2 = malloc( msg_len_2 );
1544 unsigned char *in_buf_1 = malloc( msg_len_2 );
1545 unsigned char *in_buf_2 = malloc( msg_len_1 );
1546 int msg_type, ret = -1;
1547
1548 /* Perform this test with two message types. At first use a message
1549 * consisting of only 0x00 for the client and only 0xFF for the server.
1550 * At the second time use message with generated data */
1551 for( msg_type = 0; msg_type < 2; msg_type++ )
1552 {
1553 int written_1 = 0;
1554 int written_2 = 0;
1555 int read_1 = 0;
1556 int read_2 = 0;
1557 int fragments_1 = 0;
1558 int fragments_2 = 0;
1559
1560 if( msg_type == 0 )
1561 {
1562 memset( msg_buf_1, 0x00, msg_len_1 );
1563 memset( msg_buf_2, 0xff, msg_len_2 );
1564 }
1565 else
1566 {
1567 int i, j = 0;
1568 for( i = 0; i < msg_len_1; i++ )
1569 {
1570 msg_buf_1[i] = j++ & 0xFF;
1571 }
1572 for( i = 0; i < msg_len_2; i++ )
1573 {
1574 msg_buf_2[i] = ( j -= 5 ) & 0xFF;
1575 }
1576 }
1577
1578 while( read_1 < msg_len_2 || read_2 < msg_len_1 )
1579 {
1580 /* ssl_1 sending */
1581 if( msg_len_1 > written_1 )
1582 {
1583 ret = mbedtls_ssl_write_fragment( ssl_1, msg_buf_1,
1584 msg_len_1, &written_1,
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001585 expected_fragments_1 );
1586 if( expected_fragments_1 == 0 )
1587 {
1588 /* This error is expected when the message is too large and
1589 * cannot be fragmented */
1590 TEST_ASSERT( ret == MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1591 msg_len_1 = 0;
1592 }
1593 else
1594 {
1595 TEST_ASSERT( ret == 0 );
1596 }
1597 }
1598
1599 /* ssl_2 sending */
1600 if( msg_len_2 > written_2 )
1601 {
1602 ret = mbedtls_ssl_write_fragment( ssl_2, msg_buf_2,
1603 msg_len_2, &written_2,
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001604 expected_fragments_2 );
1605 if( expected_fragments_2 == 0 )
1606 {
1607 /* This error is expected when the message is too large and
1608 * cannot be fragmented */
1609 TEST_ASSERT( ret == MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1610 msg_len_2 = 0;
1611 }
1612 else
1613 {
1614 TEST_ASSERT( ret == 0 );
1615 }
1616 }
1617
1618 /* ssl_1 reading */
1619 if( read_1 < msg_len_2 )
1620 {
1621 ret = mbedtls_ssl_read_fragment( ssl_1, in_buf_1,
1622 msg_len_2, &read_1,
Piotr Nowicki438bf3b2020-03-10 12:59:10 +01001623 &fragments_2,
1624 expected_fragments_2 );
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001625 TEST_ASSERT( ret == 0 );
1626 }
1627
1628 /* ssl_2 reading */
1629 if( read_2 < msg_len_1 )
1630 {
1631 ret = mbedtls_ssl_read_fragment( ssl_2, in_buf_2,
1632 msg_len_1, &read_2,
Piotr Nowicki438bf3b2020-03-10 12:59:10 +01001633 &fragments_1,
1634 expected_fragments_1 );
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001635 TEST_ASSERT( ret == 0 );
1636 }
1637 }
1638
1639 ret = -1;
1640 TEST_ASSERT( 0 == memcmp( msg_buf_1, in_buf_2, msg_len_1 ) );
1641 TEST_ASSERT( 0 == memcmp( msg_buf_2, in_buf_1, msg_len_2 ) );
1642 TEST_ASSERT( fragments_1 == expected_fragments_1 );
1643 TEST_ASSERT( fragments_2 == expected_fragments_2 );
1644 }
1645
1646 ret = 0;
1647
1648exit:
1649 free( msg_buf_1 );
1650 free( in_buf_1 );
1651 free( msg_buf_2 );
1652 free( in_buf_2 );
1653
1654 return ret;
1655}
1656
Piotr Nowicki95e9eb82020-02-14 11:33:34 +01001657/*
1658 * Perform data exchanging between \p ssl_1 and \p ssl_2. Both of endpoints
1659 * must be initialized and connected beforehand.
1660 *
1661 * \retval 0 on success, otherwise error code.
1662 */
1663int exchange_data( mbedtls_ssl_context *ssl_1,
1664 mbedtls_ssl_context *ssl_2 )
1665{
1666 return mbedtls_exchange_data( ssl_1, 256, 1,
1667 ssl_2, 256, 1 );
1668}
1669
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02001670#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
1671 defined(MBEDTLS_ENTROPY_C) && \
1672 defined(MBEDTLS_CTR_DRBG_C)
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001673void perform_handshake( handshake_test_options* options )
1674{
1675 /* forced_ciphersuite needs to last until the end of the handshake */
1676 int forced_ciphersuite[2];
1677 enum { BUFFSIZE = 17000 };
1678 mbedtls_endpoint client, server;
Gilles Peskineeccd8882020-03-10 12:19:08 +01001679#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001680 const char *psk_identity = "foo";
1681#endif
1682#if defined(MBEDTLS_TIMING_C)
1683 mbedtls_timing_delay_context timer_client, timer_server;
1684#endif
1685#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1686 unsigned char *context_buf = NULL;
1687 size_t context_buf_len;
1688#endif
1689#if defined(MBEDTLS_SSL_RENEGOTIATION)
1690 int ret = -1;
1691#endif
Paul Elliottc8570442020-04-15 17:00:50 +01001692 int expected_handshake_result = 0;
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001693
1694 mbedtls_test_message_queue server_queue, client_queue;
1695 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05001696 mbedtls_message_socket_init( &server_context );
1697 mbedtls_message_socket_init( &client_context );
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001698
1699 /* Client side */
1700 if( options->dtls != 0 )
1701 {
1702 TEST_ASSERT( mbedtls_endpoint_init( &client, MBEDTLS_SSL_IS_CLIENT,
1703 options->pk_alg, &client_context,
1704 &client_queue,
1705 &server_queue ) == 0 );
1706#if defined(MBEDTLS_TIMING_C)
1707 mbedtls_ssl_set_timer_cb( &client.ssl, &timer_client,
1708 mbedtls_timing_set_delay,
1709 mbedtls_timing_get_delay );
1710#endif
1711 }
1712 else
1713 {
1714 TEST_ASSERT( mbedtls_endpoint_init( &client, MBEDTLS_SSL_IS_CLIENT,
1715 options->pk_alg, NULL, NULL,
1716 NULL ) == 0 );
1717 }
Paul Elliottc8570442020-04-15 17:00:50 +01001718
1719 if( options->client_min_version != TEST_SSL_MINOR_VERSION_NONE )
1720 {
1721 mbedtls_ssl_conf_min_version( &client.conf, MBEDTLS_SSL_MAJOR_VERSION_3,
1722 options->client_min_version );
1723 }
1724
1725 if( options->client_max_version != TEST_SSL_MINOR_VERSION_NONE )
1726 {
1727 mbedtls_ssl_conf_max_version( &client.conf, MBEDTLS_SSL_MAJOR_VERSION_3,
1728 options->client_max_version );
1729 }
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001730
1731 if( strlen( options->cipher ) > 0 )
1732 {
1733 set_ciphersuite( &client.conf, options->cipher, forced_ciphersuite );
1734 }
Piotr Nowickibde7ee82020-02-21 10:59:50 +01001735
1736#if defined (MBEDTLS_DEBUG_C)
1737 if( options->cli_log_fun )
1738 {
1739 mbedtls_debug_set_threshold( 4 );
1740 mbedtls_ssl_conf_dbg( &client.conf, options->cli_log_fun,
1741 options->cli_log_obj );
1742 }
1743#endif
1744
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001745 /* Server side */
1746 if( options->dtls != 0 )
1747 {
1748 TEST_ASSERT( mbedtls_endpoint_init( &server, MBEDTLS_SSL_IS_SERVER,
1749 options->pk_alg, &server_context,
1750 &server_queue,
1751 &client_queue) == 0 );
1752#if defined(MBEDTLS_TIMING_C)
1753 mbedtls_ssl_set_timer_cb( &server.ssl, &timer_server,
1754 mbedtls_timing_set_delay,
1755 mbedtls_timing_get_delay );
1756#endif
1757 }
1758 else
1759 {
1760 TEST_ASSERT( mbedtls_endpoint_init( &server, MBEDTLS_SSL_IS_SERVER,
1761 options->pk_alg, NULL, NULL, NULL ) == 0 );
1762 }
Piotr Nowickibde7ee82020-02-21 10:59:50 +01001763
1764 mbedtls_ssl_conf_authmode( &server.conf, options->srv_auth_mode );
1765
Paul Elliottc8570442020-04-15 17:00:50 +01001766 if( options->server_min_version != TEST_SSL_MINOR_VERSION_NONE )
1767 {
1768 mbedtls_ssl_conf_min_version( &server.conf, MBEDTLS_SSL_MAJOR_VERSION_3,
1769 options->server_min_version );
1770 }
1771
1772 if( options->server_max_version != TEST_SSL_MINOR_VERSION_NONE )
1773 {
1774 mbedtls_ssl_conf_max_version( &server.conf, MBEDTLS_SSL_MAJOR_VERSION_3,
1775 options->server_max_version );
1776 }
1777
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001778#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1779 TEST_ASSERT( mbedtls_ssl_conf_max_frag_len( &(server.conf),
1780 (unsigned char) options->mfl ) == 0 );
1781 TEST_ASSERT( mbedtls_ssl_conf_max_frag_len( &(client.conf),
1782 (unsigned char) options->mfl ) == 0 );
1783#else
1784 TEST_ASSERT( MBEDTLS_SSL_MAX_FRAG_LEN_NONE == options->mfl );
1785#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
1786
Gilles Peskineeccd8882020-03-10 12:19:08 +01001787#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001788 if( options->psk_str != NULL && options->psk_str->len > 0 )
1789 {
1790 TEST_ASSERT( mbedtls_ssl_conf_psk( &client.conf, options->psk_str->x,
1791 options->psk_str->len,
1792 (const unsigned char *) psk_identity,
1793 strlen( psk_identity ) ) == 0 );
1794
1795 TEST_ASSERT( mbedtls_ssl_conf_psk( &server.conf, options->psk_str->x,
1796 options->psk_str->len,
1797 (const unsigned char *) psk_identity,
1798 strlen( psk_identity ) ) == 0 );
1799
1800 mbedtls_ssl_conf_psk_cb( &server.conf, psk_dummy_callback, NULL );
1801 }
1802#endif
1803#if defined(MBEDTLS_SSL_RENEGOTIATION)
1804 if( options->renegotiate )
1805 {
1806 mbedtls_ssl_conf_renegotiation( &(server.conf),
1807 MBEDTLS_SSL_RENEGOTIATION_ENABLED );
1808 mbedtls_ssl_conf_renegotiation( &(client.conf),
1809 MBEDTLS_SSL_RENEGOTIATION_ENABLED );
1810
1811 mbedtls_ssl_conf_legacy_renegotiation( &(server.conf),
1812 options->legacy_renegotiation );
1813 mbedtls_ssl_conf_legacy_renegotiation( &(client.conf),
1814 options->legacy_renegotiation );
1815 }
1816#endif /* MBEDTLS_SSL_RENEGOTIATION */
1817
Piotr Nowickibde7ee82020-02-21 10:59:50 +01001818#if defined (MBEDTLS_DEBUG_C)
1819 if( options->srv_log_fun )
1820 {
1821 mbedtls_debug_set_threshold( 4 );
1822 mbedtls_ssl_conf_dbg( &server.conf, options->srv_log_fun,
1823 options->srv_log_obj );
1824 }
1825#endif
1826
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001827 TEST_ASSERT( mbedtls_mock_socket_connect( &(client.socket),
1828 &(server.socket),
1829 BUFFSIZE ) == 0 );
1830
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001831#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1832 if( options->resize_buffers != 0 )
1833 {
1834 /* Ensure that the buffer sizes are appropriate before resizes */
1835 TEST_ASSERT( client.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN );
1836 TEST_ASSERT( client.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN );
1837 TEST_ASSERT( server.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN );
1838 TEST_ASSERT( server.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN );
1839 }
1840#endif
1841
Paul Elliottc8570442020-04-15 17:00:50 +01001842 if( options->expected_negotiated_version == TEST_SSL_MINOR_VERSION_NONE )
1843 {
1844 expected_handshake_result = MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION;
1845 }
1846
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001847 TEST_ASSERT( mbedtls_move_handshake_to_state( &(client.ssl),
1848 &(server.ssl),
1849 MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Elliottc8570442020-04-15 17:00:50 +01001850 == expected_handshake_result );
1851
1852 if( expected_handshake_result != 0 )
1853 {
1854 /* Connection will have failed by this point, skip to cleanup */
1855 goto exit;
1856 }
1857
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001858 TEST_ASSERT( client.ssl.state == MBEDTLS_SSL_HANDSHAKE_OVER );
1859 TEST_ASSERT( server.ssl.state == MBEDTLS_SSL_HANDSHAKE_OVER );
1860
Paul Elliottc8570442020-04-15 17:00:50 +01001861 /* Check that we agree on the version... */
1862 TEST_ASSERT( client.ssl.minor_ver == server.ssl.minor_ver );
1863
1864 /* And check that the version negotiated is the expected one. */
1865 TEST_EQUAL( client.ssl.minor_ver, options->expected_negotiated_version );
1866
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001867#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1868 if( options->resize_buffers != 0 )
1869 {
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001870 if( options->expected_negotiated_version != MBEDTLS_SSL_MINOR_VERSION_1 )
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001871 {
1872 /* A server, when using DTLS, might delay a buffer resize to happen
1873 * after it receives a message, so we force it. */
1874 TEST_ASSERT( exchange_data( &(client.ssl), &(server.ssl) ) == 0 );
1875
1876 TEST_ASSERT( client.ssl.out_buf_len ==
1877 mbedtls_ssl_get_output_buflen( &client.ssl ) );
1878 TEST_ASSERT( client.ssl.in_buf_len ==
1879 mbedtls_ssl_get_input_buflen( &client.ssl ) );
1880 TEST_ASSERT( server.ssl.out_buf_len ==
1881 mbedtls_ssl_get_output_buflen( &server.ssl ) );
1882 TEST_ASSERT( server.ssl.in_buf_len ==
1883 mbedtls_ssl_get_input_buflen( &server.ssl ) );
1884 }
1885 }
1886#endif
1887
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001888 if( options->cli_msg_len != 0 || options->srv_msg_len != 0 )
1889 {
1890 /* Start data exchanging test */
1891 TEST_ASSERT( mbedtls_exchange_data( &(client.ssl), options->cli_msg_len,
1892 options->expected_cli_fragments,
1893 &(server.ssl), options->srv_msg_len,
1894 options->expected_srv_fragments )
1895 == 0 );
1896 }
1897#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1898 if( options->serialize == 1 )
1899 {
1900 TEST_ASSERT( options->dtls == 1 );
1901
1902 TEST_ASSERT( mbedtls_ssl_context_save( &(server.ssl), NULL,
1903 0, &context_buf_len )
1904 == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1905
1906 context_buf = mbedtls_calloc( 1, context_buf_len );
1907 TEST_ASSERT( context_buf != NULL );
1908
1909 TEST_ASSERT( mbedtls_ssl_context_save( &(server.ssl), context_buf,
1910 context_buf_len,
1911 &context_buf_len ) == 0 );
1912
1913 mbedtls_ssl_free( &(server.ssl) );
1914 mbedtls_ssl_init( &(server.ssl) );
1915
1916 TEST_ASSERT( mbedtls_ssl_setup( &(server.ssl), &(server.conf) ) == 0 );
1917
1918 mbedtls_ssl_set_bio( &( server.ssl ), &server_context,
1919 mbedtls_mock_tcp_send_msg,
1920 mbedtls_mock_tcp_recv_msg,
1921 NULL );
1922
1923#if defined(MBEDTLS_TIMING_C)
1924 mbedtls_ssl_set_timer_cb( &server.ssl, &timer_server,
1925 mbedtls_timing_set_delay,
1926 mbedtls_timing_get_delay );
1927#endif
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( server.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN );
1933 TEST_ASSERT( server.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN );
1934 }
1935#endif
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001936 TEST_ASSERT( mbedtls_ssl_context_load( &( server.ssl ), context_buf,
1937 context_buf_len ) == 0 );
1938
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001939#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1940 /* Validate buffer sizes after context deserialization */
1941 if( options->resize_buffers != 0 )
1942 {
1943 TEST_ASSERT( server.ssl.out_buf_len ==
1944 mbedtls_ssl_get_output_buflen( &server.ssl ) );
1945 TEST_ASSERT( server.ssl.in_buf_len ==
1946 mbedtls_ssl_get_input_buflen( &server.ssl ) );
1947 }
1948#endif
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001949 /* Retest writing/reading */
1950 if( options->cli_msg_len != 0 || options->srv_msg_len != 0 )
1951 {
1952 TEST_ASSERT( mbedtls_exchange_data( &(client.ssl),
1953 options->cli_msg_len,
1954 options->expected_cli_fragments,
1955 &(server.ssl),
1956 options->srv_msg_len,
1957 options->expected_srv_fragments )
1958 == 0 );
1959 }
1960 }
1961#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001962
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001963#if defined(MBEDTLS_SSL_RENEGOTIATION)
1964 if( options->renegotiate )
1965 {
1966 /* Start test with renegotiation */
1967 TEST_ASSERT( server.ssl.renego_status ==
1968 MBEDTLS_SSL_INITIAL_HANDSHAKE );
1969 TEST_ASSERT( client.ssl.renego_status ==
1970 MBEDTLS_SSL_INITIAL_HANDSHAKE );
1971
1972 /* After calling this function for the server, it only sends a handshake
1973 * request. All renegotiation should happen during data exchanging */
1974 TEST_ASSERT( mbedtls_ssl_renegotiate( &(server.ssl) ) == 0 );
1975 TEST_ASSERT( server.ssl.renego_status ==
1976 MBEDTLS_SSL_RENEGOTIATION_PENDING );
1977 TEST_ASSERT( client.ssl.renego_status ==
1978 MBEDTLS_SSL_INITIAL_HANDSHAKE );
1979
1980 TEST_ASSERT( exchange_data( &(client.ssl), &(server.ssl) ) == 0 );
1981 TEST_ASSERT( server.ssl.renego_status ==
1982 MBEDTLS_SSL_RENEGOTIATION_DONE );
1983 TEST_ASSERT( client.ssl.renego_status ==
1984 MBEDTLS_SSL_RENEGOTIATION_DONE );
1985
1986 /* After calling mbedtls_ssl_renegotiate for the client all renegotiation
1987 * should happen inside this function. However in this test, we cannot
1988 * perform simultaneous communication betwen client and server so this
1989 * function will return waiting error on the socket. All rest of
1990 * renegotiation should happen during data exchanging */
1991 ret = mbedtls_ssl_renegotiate( &(client.ssl) );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001992#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1993 if( options->resize_buffers != 0 )
1994 {
1995 /* Ensure that the buffer sizes are appropriate before resizes */
1996 TEST_ASSERT( client.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN );
1997 TEST_ASSERT( client.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN );
1998 }
1999#endif
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002000 TEST_ASSERT( ret == 0 ||
2001 ret == MBEDTLS_ERR_SSL_WANT_READ ||
2002 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
2003 TEST_ASSERT( server.ssl.renego_status ==
2004 MBEDTLS_SSL_RENEGOTIATION_DONE );
2005 TEST_ASSERT( client.ssl.renego_status ==
2006 MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS );
2007
2008 TEST_ASSERT( exchange_data( &(client.ssl), &(server.ssl) ) == 0 );
2009 TEST_ASSERT( server.ssl.renego_status ==
2010 MBEDTLS_SSL_RENEGOTIATION_DONE );
2011 TEST_ASSERT( client.ssl.renego_status ==
2012 MBEDTLS_SSL_RENEGOTIATION_DONE );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05002013#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2014 /* Validate buffer sizes after renegotiation */
2015 if( options->resize_buffers != 0 )
2016 {
2017 TEST_ASSERT( client.ssl.out_buf_len ==
2018 mbedtls_ssl_get_output_buflen( &client.ssl ) );
2019 TEST_ASSERT( client.ssl.in_buf_len ==
2020 mbedtls_ssl_get_input_buflen( &client.ssl ) );
2021 TEST_ASSERT( server.ssl.out_buf_len ==
2022 mbedtls_ssl_get_output_buflen( &server.ssl ) );
2023 TEST_ASSERT( server.ssl.in_buf_len ==
2024 mbedtls_ssl_get_input_buflen( &server.ssl ) );
2025 }
2026#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002027 }
2028#endif /* MBEDTLS_SSL_RENEGOTIATION */
2029
2030exit:
2031 mbedtls_endpoint_free( &client, options->dtls != 0 ? &client_context : NULL );
2032 mbedtls_endpoint_free( &server, options->dtls != 0 ? &server_context : NULL );
Piotr Nowickibde7ee82020-02-21 10:59:50 +01002033#if defined (MBEDTLS_DEBUG_C)
2034 if( options->cli_log_fun || options->srv_log_fun )
2035 {
2036 mbedtls_debug_set_threshold( 0 );
2037 }
2038#endif
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002039#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
2040 if( context_buf != NULL )
2041 mbedtls_free( context_buf );
2042#endif
2043}
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02002044#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002045
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002046/* END_HEADER */
2047
2048/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002049 * depends_on:MBEDTLS_SSL_TLS_C
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002050 * END_DEPENDENCIES
2051 */
2052
Janos Follath6264e662019-11-26 11:11:15 +00002053/* BEGIN_CASE */
2054void test_callback_buffer_sanity()
2055{
2056 enum { MSGLEN = 10 };
2057 mbedtls_test_buffer buf;
2058 unsigned char input[MSGLEN];
2059 unsigned char output[MSGLEN];
2060
2061 memset( input, 0, sizeof(input) );
2062
2063 /* Make sure calling put and get on NULL buffer results in error. */
2064 TEST_ASSERT( mbedtls_test_buffer_put( NULL, input, sizeof( input ) )
2065 == -1 );
2066 TEST_ASSERT( mbedtls_test_buffer_get( NULL, output, sizeof( output ) )
2067 == -1 );
2068 TEST_ASSERT( mbedtls_test_buffer_put( NULL, NULL, sizeof( input ) ) == -1 );
Andrzej Kurekf7774142020-01-22 06:34:59 -05002069
Janos Follath6264e662019-11-26 11:11:15 +00002070 TEST_ASSERT( mbedtls_test_buffer_put( NULL, NULL, 0 ) == -1 );
2071 TEST_ASSERT( mbedtls_test_buffer_get( NULL, NULL, 0 ) == -1 );
2072
2073 /* Make sure calling put and get on a buffer that hasn't been set up results
2074 * in eror. */
2075 mbedtls_test_buffer_init( &buf );
2076
2077 TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, sizeof( input ) ) == -1 );
2078 TEST_ASSERT( mbedtls_test_buffer_get( &buf, output, sizeof( output ) )
2079 == -1 );
2080 TEST_ASSERT( mbedtls_test_buffer_put( &buf, NULL, sizeof( input ) ) == -1 );
Andrzej Kurekf7774142020-01-22 06:34:59 -05002081
Janos Follath6264e662019-11-26 11:11:15 +00002082 TEST_ASSERT( mbedtls_test_buffer_put( &buf, NULL, 0 ) == -1 );
2083 TEST_ASSERT( mbedtls_test_buffer_get( &buf, NULL, 0 ) == -1 );
2084
Andrzej Kurekf7774142020-01-22 06:34:59 -05002085 /* Make sure calling put and get on NULL input only results in
2086 * error if the length is not zero, and that a NULL output is valid for data
2087 * dropping.
2088 */
Janos Follath6264e662019-11-26 11:11:15 +00002089
2090 TEST_ASSERT( mbedtls_test_buffer_setup( &buf, sizeof( input ) ) == 0 );
2091
2092 TEST_ASSERT( mbedtls_test_buffer_put( &buf, NULL, sizeof( input ) ) == -1 );
2093 TEST_ASSERT( mbedtls_test_buffer_get( &buf, NULL, sizeof( output ) )
Andrzej Kurekf7774142020-01-22 06:34:59 -05002094 == 0 );
Janos Follath6264e662019-11-26 11:11:15 +00002095 TEST_ASSERT( mbedtls_test_buffer_put( &buf, NULL, 0 ) == 0 );
2096 TEST_ASSERT( mbedtls_test_buffer_get( &buf, NULL, 0 ) == 0 );
2097
Piotr Nowickifb437d72020-01-13 16:59:12 +01002098 /* Make sure calling put several times in the row is safe */
2099
2100 TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, sizeof( input ) )
2101 == sizeof( input ) );
2102 TEST_ASSERT( mbedtls_test_buffer_get( &buf, output, 2 ) == 2 );
2103 TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, 1 ) == 1 );
2104 TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, 2 ) == 1 );
2105 TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, 2 ) == 0 );
2106
2107
Janos Follath6264e662019-11-26 11:11:15 +00002108exit:
2109
2110 mbedtls_test_buffer_free( &buf );
2111}
2112/* END_CASE */
2113
2114/*
2115 * Test if the implementation of `mbedtls_test_buffer` related functions is
2116 * correct and works as expected.
2117 *
2118 * That is
2119 * - If we try to put in \p put1 bytes then we can put in \p put1_ret bytes.
2120 * - Afterwards if we try to get \p get1 bytes then we can get \get1_ret bytes.
2121 * - Next, if we try to put in \p put1 bytes then we can put in \p put1_ret
2122 * bytes.
2123 * - Afterwards if we try to get \p get1 bytes then we can get \get1_ret bytes.
2124 * - All of the bytes we got match the bytes we put in in a FIFO manner.
2125 */
2126
2127/* BEGIN_CASE */
2128void test_callback_buffer( int size, int put1, int put1_ret,
2129 int get1, int get1_ret, int put2, int put2_ret,
2130 int get2, int get2_ret )
2131{
2132 enum { ROUNDS = 2 };
2133 size_t put[ROUNDS];
2134 int put_ret[ROUNDS];
2135 size_t get[ROUNDS];
2136 int get_ret[ROUNDS];
2137 mbedtls_test_buffer buf;
2138 unsigned char* input = NULL;
2139 size_t input_len;
2140 unsigned char* output = NULL;
2141 size_t output_len;
Janos Follath031827f2019-11-27 11:12:14 +00002142 size_t i, j, written, read;
Janos Follath6264e662019-11-26 11:11:15 +00002143
2144 mbedtls_test_buffer_init( &buf );
2145 TEST_ASSERT( mbedtls_test_buffer_setup( &buf, size ) == 0 );
2146
2147 /* Check the sanity of input parameters and initialise local variables. That
2148 * is, ensure that the amount of data is not negative and that we are not
2149 * expecting more to put or get than we actually asked for. */
2150 TEST_ASSERT( put1 >= 0 );
2151 put[0] = put1;
2152 put_ret[0] = put1_ret;
2153 TEST_ASSERT( put1_ret <= put1 );
2154 TEST_ASSERT( put2 >= 0 );
2155 put[1] = put2;
2156 put_ret[1] = put2_ret;
2157 TEST_ASSERT( put2_ret <= put2 );
2158
2159 TEST_ASSERT( get1 >= 0 );
2160 get[0] = get1;
2161 get_ret[0] = get1_ret;
2162 TEST_ASSERT( get1_ret <= get1 );
2163 TEST_ASSERT( get2 >= 0 );
2164 get[1] = get2;
2165 get_ret[1] = get2_ret;
2166 TEST_ASSERT( get2_ret <= get2 );
2167
2168 input_len = 0;
2169 /* Calculate actual input and output lengths */
2170 for( j = 0; j < ROUNDS; j++ )
2171 {
2172 if( put_ret[j] > 0 )
2173 {
2174 input_len += put_ret[j];
2175 }
2176 }
2177 /* In order to always have a valid pointer we always allocate at least 1
2178 * byte. */
2179 if( input_len == 0 )
2180 input_len = 1;
2181 ASSERT_ALLOC( input, input_len );
2182
2183 output_len = 0;
2184 for( j = 0; j < ROUNDS; j++ )
2185 {
2186 if( get_ret[j] > 0 )
2187 {
2188 output_len += get_ret[j];
2189 }
2190 }
2191 TEST_ASSERT( output_len <= input_len );
2192 /* In order to always have a valid pointer we always allocate at least 1
2193 * byte. */
2194 if( output_len == 0 )
2195 output_len = 1;
2196 ASSERT_ALLOC( output, output_len );
2197
2198 /* Fill up the buffer with structured data so that unwanted changes
2199 * can be detected */
2200 for( i = 0; i < input_len; i++ )
2201 {
2202 input[i] = i & 0xFF;
2203 }
2204
2205 written = read = 0;
2206 for( j = 0; j < ROUNDS; j++ )
2207 {
2208 TEST_ASSERT( put_ret[j] == mbedtls_test_buffer_put( &buf,
2209 input + written, put[j] ) );
2210 written += put_ret[j];
2211 TEST_ASSERT( get_ret[j] == mbedtls_test_buffer_get( &buf,
2212 output + read, get[j] ) );
2213 read += get_ret[j];
2214 TEST_ASSERT( read <= written );
2215 if( get_ret[j] > 0 )
2216 {
2217 TEST_ASSERT( memcmp( output + read - get_ret[j],
2218 input + read - get_ret[j], get_ret[j] )
2219 == 0 );
2220 }
2221 }
2222
2223exit:
2224
2225 mbedtls_free( input );
2226 mbedtls_free( output );
2227 mbedtls_test_buffer_free( &buf );
2228}
2229/* END_CASE */
2230
Janos Follath031827f2019-11-27 11:12:14 +00002231/*
Janos Follathc673c2c2019-12-02 15:47:26 +00002232 * Test if the implementation of `mbedtls_mock_socket` related I/O functions is
2233 * correct and works as expected on unconnected sockets.
2234 */
2235
2236/* BEGIN_CASE */
2237void ssl_mock_sanity( )
2238{
2239 enum { MSGLEN = 105 };
2240 unsigned char message[MSGLEN];
2241 unsigned char received[MSGLEN];
2242 mbedtls_mock_socket socket;
2243
2244 mbedtls_mock_socket_init( &socket );
2245 TEST_ASSERT( mbedtls_mock_tcp_send_b( &socket, message, MSGLEN ) < 0 );
2246 mbedtls_mock_socket_close( &socket );
2247 mbedtls_mock_socket_init( &socket );
2248 TEST_ASSERT( mbedtls_mock_tcp_recv_b( &socket, received, MSGLEN ) < 0 );
2249 mbedtls_mock_socket_close( &socket );
2250
2251 mbedtls_mock_socket_init( &socket );
2252 TEST_ASSERT( mbedtls_mock_tcp_send_nb( &socket, message, MSGLEN ) < 0 );
2253 mbedtls_mock_socket_close( &socket );
2254 mbedtls_mock_socket_init( &socket );
2255 TEST_ASSERT( mbedtls_mock_tcp_recv_nb( &socket, received, MSGLEN ) < 0 );
2256 mbedtls_mock_socket_close( &socket );
2257
2258exit:
2259
2260 mbedtls_mock_socket_close( &socket );
2261}
2262/* END_CASE */
2263
2264/*
2265 * Test if the implementation of `mbedtls_mock_socket` related functions can
2266 * send a single message from the client to the server.
Janos Follath031827f2019-11-27 11:12:14 +00002267 */
2268
2269/* BEGIN_CASE */
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002270void ssl_mock_tcp( int blocking )
Janos Follath031827f2019-11-27 11:12:14 +00002271{
Janos Follathc673c2c2019-12-02 15:47:26 +00002272 enum { MSGLEN = 105 };
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002273 enum { BUFLEN = MSGLEN / 5 };
Janos Follathc673c2c2019-12-02 15:47:26 +00002274 unsigned char message[MSGLEN];
2275 unsigned char received[MSGLEN];
2276 mbedtls_mock_socket client;
2277 mbedtls_mock_socket server;
2278 size_t written, read;
2279 int send_ret, recv_ret;
2280 mbedtls_ssl_send_t *send;
2281 mbedtls_ssl_recv_t *recv;
Janos Follathc673c2c2019-12-02 15:47:26 +00002282 unsigned i;
2283
2284 if( blocking == 0 )
2285 {
2286 send = mbedtls_mock_tcp_send_nb;
2287 recv = mbedtls_mock_tcp_recv_nb;
2288 }
2289 else
2290 {
2291 send = mbedtls_mock_tcp_send_b;
2292 recv = mbedtls_mock_tcp_recv_b;
2293 }
2294
2295 mbedtls_mock_socket_init( &client );
2296 mbedtls_mock_socket_init( &server );
2297
2298 /* Fill up the buffer with structured data so that unwanted changes
2299 * can be detected */
2300 for( i = 0; i < MSGLEN; i++ )
2301 {
2302 message[i] = i & 0xFF;
2303 }
2304
2305 /* Make sure that sending a message takes a few iterations. */
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002306 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, BUFLEN ) );
Janos Follathc673c2c2019-12-02 15:47:26 +00002307
2308 /* Send the message to the server */
2309 send_ret = recv_ret = 1;
2310 written = read = 0;
2311 while( send_ret != 0 || recv_ret != 0 )
2312 {
2313 send_ret = send( &client, message + written, MSGLEN - written );
2314
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002315 TEST_ASSERT( send_ret >= 0 );
2316 TEST_ASSERT( send_ret <= BUFLEN );
2317 written += send_ret;
2318
2319 /* If the buffer is full we can test blocking and non-blocking send */
2320 if ( send_ret == BUFLEN )
Janos Follathc673c2c2019-12-02 15:47:26 +00002321 {
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002322 int blocking_ret = send( &client, message , 1 );
2323 if ( blocking )
2324 {
2325 TEST_ASSERT( blocking_ret == 0 );
2326 }
2327 else
2328 {
2329 TEST_ASSERT( blocking_ret == MBEDTLS_ERR_SSL_WANT_WRITE );
2330 }
Janos Follathc673c2c2019-12-02 15:47:26 +00002331 }
Janos Follathc673c2c2019-12-02 15:47:26 +00002332
2333 recv_ret = recv( &server, received + read, MSGLEN - read );
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002334
2335 /* The result depends on whether any data was sent */
2336 if ( send_ret > 0 )
Janos Follathc673c2c2019-12-02 15:47:26 +00002337 {
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002338 TEST_ASSERT( recv_ret > 0 );
2339 TEST_ASSERT( recv_ret <= BUFLEN );
2340 read += recv_ret;
2341 }
2342 else if( blocking )
2343 {
2344 TEST_ASSERT( recv_ret == 0 );
Janos Follathc673c2c2019-12-02 15:47:26 +00002345 }
2346 else
2347 {
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002348 TEST_ASSERT( recv_ret == MBEDTLS_ERR_SSL_WANT_READ );
2349 recv_ret = 0;
Janos Follathc673c2c2019-12-02 15:47:26 +00002350 }
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002351
2352 /* If the buffer is empty we can test blocking and non-blocking read */
2353 if ( recv_ret == BUFLEN )
2354 {
2355 int blocking_ret = recv( &server, received, 1 );
2356 if ( blocking )
2357 {
2358 TEST_ASSERT( blocking_ret == 0 );
2359 }
2360 else
2361 {
2362 TEST_ASSERT( blocking_ret == MBEDTLS_ERR_SSL_WANT_READ );
2363 }
2364 }
Janos Follathc673c2c2019-12-02 15:47:26 +00002365 }
2366 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
2367
2368exit:
2369
2370 mbedtls_mock_socket_close( &client );
2371 mbedtls_mock_socket_close( &server );
2372}
2373/* END_CASE */
2374
2375/*
2376 * Test if the implementation of `mbedtls_mock_socket` related functions can
2377 * send messages in both direction at the same time (with the I/O calls
2378 * interleaving).
2379 */
2380
2381/* BEGIN_CASE */
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002382void ssl_mock_tcp_interleaving( int blocking )
Janos Follathc673c2c2019-12-02 15:47:26 +00002383{
Janos Follath031827f2019-11-27 11:12:14 +00002384 enum { ROUNDS = 2 };
2385 enum { MSGLEN = 105 };
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002386 enum { BUFLEN = MSGLEN / 5 };
Janos Follath031827f2019-11-27 11:12:14 +00002387 unsigned char message[ROUNDS][MSGLEN];
2388 unsigned char received[ROUNDS][MSGLEN];
2389 mbedtls_mock_socket client;
2390 mbedtls_mock_socket server;
2391 size_t written[ROUNDS];
2392 size_t read[ROUNDS];
2393 int send_ret[ROUNDS];
2394 int recv_ret[ROUNDS];
2395 unsigned i, j, progress;
Janos Follath3766ba52019-11-27 13:31:42 +00002396 mbedtls_ssl_send_t *send;
2397 mbedtls_ssl_recv_t *recv;
Janos Follath3766ba52019-11-27 13:31:42 +00002398
2399 if( blocking == 0 )
2400 {
2401 send = mbedtls_mock_tcp_send_nb;
2402 recv = mbedtls_mock_tcp_recv_nb;
2403 }
2404 else
2405 {
2406 send = mbedtls_mock_tcp_send_b;
2407 recv = mbedtls_mock_tcp_recv_b;
2408 }
Janos Follath031827f2019-11-27 11:12:14 +00002409
2410 mbedtls_mock_socket_init( &client );
2411 mbedtls_mock_socket_init( &server );
2412
2413 /* Fill up the buffers with structured data so that unwanted changes
2414 * can be detected */
2415 for( i = 0; i < ROUNDS; i++ )
2416 {
2417 for( j = 0; j < MSGLEN; j++ )
2418 {
2419 message[i][j] = ( i * MSGLEN + j ) & 0xFF;
2420 }
2421 }
2422
Janos Follath031827f2019-11-27 11:12:14 +00002423 /* Make sure that sending a message takes a few iterations. */
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002424 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, BUFLEN ) );
Janos Follath031827f2019-11-27 11:12:14 +00002425
Janos Follath031827f2019-11-27 11:12:14 +00002426 /* Send the message from both sides, interleaving. */
2427 progress = 1;
2428 for( i = 0; i < ROUNDS; i++ )
2429 {
2430 written[i] = 0;
2431 read[i] = 0;
2432 }
2433 /* This loop does not stop as long as there was a successful write or read
2434 * of at least one byte on either side. */
2435 while( progress != 0 )
2436 {
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002437 mbedtls_mock_socket *socket;
Janos Follath031827f2019-11-27 11:12:14 +00002438
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002439 for( i = 0; i < ROUNDS; i++ )
Janos Follath3766ba52019-11-27 13:31:42 +00002440 {
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002441 /* First sending is from the client */
2442 socket = ( i % 2 == 0 ) ? ( &client ) : ( &server );
Janos Follath031827f2019-11-27 11:12:14 +00002443
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002444 send_ret[i] = send( socket, message[i] + written[i],
2445 MSGLEN - written[i] );
2446 TEST_ASSERT( send_ret[i] >= 0 );
2447 TEST_ASSERT( send_ret[i] <= BUFLEN );
2448 written[i] += send_ret[i];
Janos Follath031827f2019-11-27 11:12:14 +00002449
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002450 /* If the buffer is full we can test blocking and non-blocking
2451 * send */
2452 if ( send_ret[i] == BUFLEN )
2453 {
2454 int blocking_ret = send( socket, message[i] , 1 );
2455 if ( blocking )
2456 {
2457 TEST_ASSERT( blocking_ret == 0 );
2458 }
2459 else
2460 {
2461 TEST_ASSERT( blocking_ret == MBEDTLS_ERR_SSL_WANT_WRITE );
2462 }
2463 }
Janos Follath3766ba52019-11-27 13:31:42 +00002464 }
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002465
2466 for( i = 0; i < ROUNDS; i++ )
Janos Follath3766ba52019-11-27 13:31:42 +00002467 {
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002468 /* First receiving is from the server */
2469 socket = ( i % 2 == 0 ) ? ( &server ) : ( &client );
2470
2471 recv_ret[i] = recv( socket, received[i] + read[i],
2472 MSGLEN - read[i] );
2473
2474 /* The result depends on whether any data was sent */
2475 if ( send_ret[i] > 0 )
2476 {
2477 TEST_ASSERT( recv_ret[i] > 0 );
2478 TEST_ASSERT( recv_ret[i] <= BUFLEN );
2479 read[i] += recv_ret[i];
2480 }
2481 else if( blocking )
2482 {
2483 TEST_ASSERT( recv_ret[i] == 0 );
2484 }
2485 else
2486 {
2487 TEST_ASSERT( recv_ret[i] == MBEDTLS_ERR_SSL_WANT_READ );
2488 recv_ret[i] = 0;
2489 }
2490
2491 /* If the buffer is empty we can test blocking and non-blocking
2492 * read */
2493 if ( recv_ret[i] == BUFLEN )
2494 {
2495 int blocking_ret = recv( socket, received[i], 1 );
2496 if ( blocking )
2497 {
2498 TEST_ASSERT( blocking_ret == 0 );
2499 }
2500 else
2501 {
2502 TEST_ASSERT( blocking_ret == MBEDTLS_ERR_SSL_WANT_READ );
2503 }
2504 }
Janos Follath3766ba52019-11-27 13:31:42 +00002505 }
Janos Follath031827f2019-11-27 11:12:14 +00002506
2507 progress = 0;
2508 for( i = 0; i < ROUNDS; i++ )
2509 {
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002510 progress += send_ret[i] + recv_ret[i];
Janos Follath031827f2019-11-27 11:12:14 +00002511 }
2512 }
2513
2514 for( i = 0; i < ROUNDS; i++ )
2515 TEST_ASSERT( memcmp( message[i], received[i], MSGLEN ) == 0 );
2516
2517exit:
2518
2519 mbedtls_mock_socket_close( &client );
2520 mbedtls_mock_socket_close( &server );
2521}
2522/* END_CASE */
2523
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002524/* BEGIN_CASE */
2525void ssl_message_queue_sanity( )
2526{
2527 mbedtls_test_message_queue queue;
2528
2529 /* Trying to push/pull to an empty queue */
2530 TEST_ASSERT( mbedtls_test_message_queue_push_info( NULL, 1 )
2531 == MBEDTLS_TEST_ERROR_ARG_NULL );
2532 TEST_ASSERT( mbedtls_test_message_queue_pop_info( NULL, 1 )
2533 == MBEDTLS_TEST_ERROR_ARG_NULL );
2534
Andrzej Kurek89bdc582020-03-09 06:29:43 -04002535 TEST_ASSERT( mbedtls_test_message_queue_setup( &queue, 3 ) == 0 );
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002536 TEST_ASSERT( queue.capacity == 3 );
2537 TEST_ASSERT( queue.num == 0 );
2538
2539exit:
2540 mbedtls_test_message_queue_free( &queue );
2541}
2542/* END_CASE */
2543
2544/* BEGIN_CASE */
2545void ssl_message_queue_basic( )
2546{
2547 mbedtls_test_message_queue queue;
2548
Andrzej Kurek89bdc582020-03-09 06:29:43 -04002549 TEST_ASSERT( mbedtls_test_message_queue_setup( &queue, 3 ) == 0 );
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002550
2551 /* Sanity test - 3 pushes and 3 pops with sufficient space */
2552 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 );
2553 TEST_ASSERT( queue.capacity == 3 );
2554 TEST_ASSERT( queue.num == 1 );
2555 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 );
2556 TEST_ASSERT( queue.capacity == 3 );
2557 TEST_ASSERT( queue.num == 2 );
2558 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 2 ) == 2 );
2559 TEST_ASSERT( queue.capacity == 3 );
2560 TEST_ASSERT( queue.num == 3 );
2561
2562 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 );
2563 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 );
2564 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 2 ) == 2 );
2565
2566exit:
2567 mbedtls_test_message_queue_free( &queue );
2568}
2569/* END_CASE */
2570
2571/* BEGIN_CASE */
2572void ssl_message_queue_overflow_underflow( )
2573{
2574 mbedtls_test_message_queue queue;
2575
Andrzej Kurek89bdc582020-03-09 06:29:43 -04002576 TEST_ASSERT( mbedtls_test_message_queue_setup( &queue, 3 ) == 0 );
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002577
2578 /* 4 pushes (last one with an error), 4 pops (last one with an error) */
2579 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 );
2580 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 );
2581 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 2 ) == 2 );
2582 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 3 )
Andrzej Kurekf46b9122020-02-07 08:19:00 -05002583 == MBEDTLS_ERR_SSL_WANT_WRITE );
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002584
2585 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 );
2586 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 );
2587 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 2 ) == 2 );
2588
2589 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 )
Andrzej Kurekf46b9122020-02-07 08:19:00 -05002590 == MBEDTLS_ERR_SSL_WANT_READ );
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002591
2592exit:
2593 mbedtls_test_message_queue_free( &queue );
2594}
2595/* END_CASE */
2596
2597/* BEGIN_CASE */
2598void ssl_message_queue_interleaved( )
2599{
2600 mbedtls_test_message_queue queue;
2601
Andrzej Kurek89bdc582020-03-09 06:29:43 -04002602 TEST_ASSERT( mbedtls_test_message_queue_setup( &queue, 3 ) == 0 );
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002603
2604 /* Interleaved test - [2 pushes, 1 pop] twice, and then two pops
2605 * (to wrap around the buffer) */
2606 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 );
2607 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 );
2608
2609 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 );
2610
2611 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 2 ) == 2 );
2612 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 3 ) == 3 );
2613
2614 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 );
2615 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 2 ) == 2 );
2616
2617 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 5 ) == 5 );
2618 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 8 ) == 8 );
2619
2620 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 3 ) == 3 );
2621
2622 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 5 ) == 5 );
2623
2624 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 8 ) == 8 );
2625
2626exit:
2627 mbedtls_test_message_queue_free( &queue );
2628}
2629/* END_CASE */
2630
2631/* BEGIN_CASE */
2632void ssl_message_queue_insufficient_buffer( )
2633{
2634 mbedtls_test_message_queue queue;
2635 size_t message_len = 10;
2636 size_t buffer_len = 5;
2637
Andrzej Kurek89bdc582020-03-09 06:29:43 -04002638 TEST_ASSERT( mbedtls_test_message_queue_setup( &queue, 1 ) == 0 );
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002639
2640 /* Popping without a sufficient buffer */
2641 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, message_len )
2642 == (int) message_len );
2643 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, buffer_len )
2644 == (int) buffer_len );
2645exit:
2646 mbedtls_test_message_queue_free( &queue );
2647}
2648/* END_CASE */
2649
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002650/* BEGIN_CASE */
2651void ssl_message_mock_uninitialized( )
2652{
2653 enum { MSGLEN = 10 };
Shawn Carey03092f52021-05-13 10:38:32 -04002654 unsigned char message[MSGLEN] = {0}, received[MSGLEN];
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002655 mbedtls_mock_socket client, server;
2656 mbedtls_test_message_queue server_queue, client_queue;
2657 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05002658 mbedtls_message_socket_init( &server_context );
2659 mbedtls_message_socket_init( &client_context );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002660
2661 /* Send with a NULL context */
2662 TEST_ASSERT( mbedtls_mock_tcp_send_msg( NULL, message, MSGLEN )
2663 == MBEDTLS_TEST_ERROR_CONTEXT_ERROR );
2664
2665 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( NULL, message, MSGLEN )
2666 == MBEDTLS_TEST_ERROR_CONTEXT_ERROR );
2667
2668 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 1,
2669 &server,
2670 &server_context ) == 0 );
2671
2672 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 1,
2673 &client,
2674 &client_context ) == 0 );
2675
2676 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, MSGLEN )
2677 == MBEDTLS_TEST_ERROR_SEND_FAILED );
2678
2679 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
Andrzej Kurekf46b9122020-02-07 08:19:00 -05002680 == MBEDTLS_ERR_SSL_WANT_READ );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002681
2682 /* Push directly to a queue to later simulate a disconnected behavior */
2683 TEST_ASSERT( mbedtls_test_message_queue_push_info( &server_queue, MSGLEN )
2684 == MSGLEN );
2685
2686 /* Test if there's an error when trying to read from a disconnected
2687 * socket */
2688 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
2689 == MBEDTLS_TEST_ERROR_RECV_FAILED );
2690 exit:
2691 mbedtls_message_socket_close( &server_context );
2692 mbedtls_message_socket_close( &client_context );
2693}
2694/* END_CASE */
2695
2696/* BEGIN_CASE */
2697void ssl_message_mock_basic( )
2698{
2699 enum { MSGLEN = 10 };
2700 unsigned char message[MSGLEN], received[MSGLEN];
2701 mbedtls_mock_socket client, server;
2702 unsigned i;
2703 mbedtls_test_message_queue server_queue, client_queue;
2704 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05002705 mbedtls_message_socket_init( &server_context );
2706 mbedtls_message_socket_init( &client_context );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002707
2708 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 1,
2709 &server,
2710 &server_context ) == 0 );
2711
2712 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 1,
2713 &client,
2714 &client_context ) == 0 );
2715
2716 /* Fill up the buffer with structured data so that unwanted changes
2717 * can be detected */
2718 for( i = 0; i < MSGLEN; i++ )
2719 {
2720 message[i] = i & 0xFF;
2721 }
2722 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
2723 MSGLEN ) );
2724
2725 /* Send the message to the server */
2726 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2727 MSGLEN ) == MSGLEN );
2728
2729 /* Read from the server */
2730 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
2731 == MSGLEN );
2732
2733 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
2734 memset( received, 0, MSGLEN );
2735
2736 /* Send the message to the client */
2737 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &server_context, message,
2738 MSGLEN ) == MSGLEN );
2739
2740 /* Read from the client */
2741 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &client_context, received, MSGLEN )
2742 == MSGLEN );
2743 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
2744
2745 exit:
2746 mbedtls_message_socket_close( &server_context );
2747 mbedtls_message_socket_close( &client_context );
2748}
2749/* END_CASE */
2750
2751/* BEGIN_CASE */
2752void ssl_message_mock_queue_overflow_underflow( )
2753{
2754 enum { MSGLEN = 10 };
2755 unsigned char message[MSGLEN], received[MSGLEN];
2756 mbedtls_mock_socket client, server;
2757 unsigned i;
2758 mbedtls_test_message_queue server_queue, client_queue;
2759 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05002760 mbedtls_message_socket_init( &server_context );
2761 mbedtls_message_socket_init( &client_context );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002762
2763 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 2,
2764 &server,
2765 &server_context ) == 0 );
2766
2767 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 2,
2768 &client,
2769 &client_context ) == 0 );
2770
2771 /* Fill up the buffer with structured data so that unwanted changes
2772 * can be detected */
2773 for( i = 0; i < MSGLEN; i++ )
2774 {
2775 message[i] = i & 0xFF;
2776 }
2777 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
2778 MSGLEN*2 ) );
2779
2780 /* Send three message to the server, last one with an error */
2781 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2782 MSGLEN - 1 ) == MSGLEN - 1 );
2783
2784 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2785 MSGLEN ) == MSGLEN );
2786
2787 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2788 MSGLEN )
Andrzej Kurekf46b9122020-02-07 08:19:00 -05002789 == MBEDTLS_ERR_SSL_WANT_WRITE );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002790
2791 /* Read three messages from the server, last one with an error */
2792 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received,
2793 MSGLEN - 1 ) == MSGLEN - 1 );
2794
2795 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
2796 == MSGLEN );
2797
2798 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
2799
2800 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
Andrzej Kurekf46b9122020-02-07 08:19:00 -05002801 == MBEDTLS_ERR_SSL_WANT_READ );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002802
2803 exit:
2804 mbedtls_message_socket_close( &server_context );
2805 mbedtls_message_socket_close( &client_context );
2806}
2807/* END_CASE */
2808
2809/* BEGIN_CASE */
2810void ssl_message_mock_socket_overflow( )
2811{
2812 enum { MSGLEN = 10 };
2813 unsigned char message[MSGLEN], received[MSGLEN];
2814 mbedtls_mock_socket client, server;
2815 unsigned i;
2816 mbedtls_test_message_queue server_queue, client_queue;
2817 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05002818 mbedtls_message_socket_init( &server_context );
2819 mbedtls_message_socket_init( &client_context );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002820
2821 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 2,
2822 &server,
2823 &server_context ) == 0 );
2824
2825 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 2,
2826 &client,
2827 &client_context ) == 0 );
2828
2829 /* Fill up the buffer with structured data so that unwanted changes
2830 * can be detected */
2831 for( i = 0; i < MSGLEN; i++ )
2832 {
2833 message[i] = i & 0xFF;
2834 }
2835 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
2836 MSGLEN ) );
2837
2838 /* Send two message to the server, second one with an error */
2839 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2840 MSGLEN ) == MSGLEN );
2841
2842 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2843 MSGLEN )
2844 == MBEDTLS_TEST_ERROR_SEND_FAILED );
2845
2846 /* Read the only message from the server */
2847 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
2848 == MSGLEN );
2849
2850 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
2851
2852 exit:
2853 mbedtls_message_socket_close( &server_context );
2854 mbedtls_message_socket_close( &client_context );
2855}
2856/* END_CASE */
2857
2858/* BEGIN_CASE */
2859void ssl_message_mock_truncated( )
2860{
2861 enum { MSGLEN = 10 };
2862 unsigned char message[MSGLEN], received[MSGLEN];
2863 mbedtls_mock_socket client, server;
2864 unsigned i;
2865 mbedtls_test_message_queue server_queue, client_queue;
2866 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05002867 mbedtls_message_socket_init( &server_context );
2868 mbedtls_message_socket_init( &client_context );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002869
2870 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 2,
2871 &server,
2872 &server_context ) == 0 );
2873
2874 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 2,
2875 &client,
2876 &client_context ) == 0 );
2877
2878 memset( received, 0, MSGLEN );
2879 /* Fill up the buffer with structured data so that unwanted changes
2880 * can be detected */
2881 for( i = 0; i < MSGLEN; i++ )
2882 {
2883 message[i] = i & 0xFF;
2884 }
2885 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
2886 2 * MSGLEN ) );
2887
2888 /* Send two messages to the server, the second one small enough to fit in the
2889 * receiver's buffer. */
2890 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2891 MSGLEN ) == MSGLEN );
2892 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2893 MSGLEN / 2 ) == MSGLEN / 2 );
2894 /* Read a truncated message from the server */
2895 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN/2 )
2896 == MSGLEN/2 );
2897
2898 /* Test that the first half of the message is valid, and second one isn't */
2899 TEST_ASSERT( memcmp( message, received, MSGLEN/2 ) == 0 );
2900 TEST_ASSERT( memcmp( message + MSGLEN/2, received + MSGLEN/2, MSGLEN/2 )
2901 != 0 );
2902 memset( received, 0, MSGLEN );
2903
2904 /* Read a full message from the server */
2905 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN/2 )
2906 == MSGLEN / 2 );
2907
2908 /* Test that the first half of the message is valid */
2909 TEST_ASSERT( memcmp( message, received, MSGLEN/2 ) == 0 );
2910
2911 exit:
2912 mbedtls_message_socket_close( &server_context );
2913 mbedtls_message_socket_close( &client_context );
2914}
2915/* END_CASE */
2916
2917/* BEGIN_CASE */
2918void ssl_message_mock_socket_read_error( )
2919{
2920 enum { MSGLEN = 10 };
2921 unsigned char message[MSGLEN], received[MSGLEN];
2922 mbedtls_mock_socket client, server;
2923 unsigned i;
2924 mbedtls_test_message_queue server_queue, client_queue;
2925 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05002926 mbedtls_message_socket_init( &server_context );
2927 mbedtls_message_socket_init( &client_context );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002928
2929 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 1,
2930 &server,
2931 &server_context ) == 0 );
2932
2933 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 1,
2934 &client,
2935 &client_context ) == 0 );
2936
2937 /* Fill up the buffer with structured data so that unwanted changes
2938 * can be detected */
2939 for( i = 0; i < MSGLEN; i++ )
2940 {
2941 message[i] = i & 0xFF;
2942 }
2943 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
2944 MSGLEN ) );
2945
2946 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2947 MSGLEN ) == MSGLEN );
2948
2949 /* Force a read error by disconnecting the socket by hand */
2950 server.status = 0;
2951 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
2952 == MBEDTLS_TEST_ERROR_RECV_FAILED );
2953 /* Return to a valid state */
2954 server.status = MBEDTLS_MOCK_SOCKET_CONNECTED;
2955
2956 memset( received, 0, sizeof( received ) );
2957
2958 /* Test that even though the server tried to read once disconnected, the
2959 * continuity is preserved */
2960 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
2961 == MSGLEN );
2962
2963 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
2964
2965 exit:
2966 mbedtls_message_socket_close( &server_context );
2967 mbedtls_message_socket_close( &client_context );
2968}
2969/* END_CASE */
2970
2971/* BEGIN_CASE */
2972void ssl_message_mock_interleaved_one_way( )
2973{
2974 enum { MSGLEN = 10 };
2975 unsigned char message[MSGLEN], received[MSGLEN];
2976 mbedtls_mock_socket client, server;
2977 unsigned i;
2978 mbedtls_test_message_queue server_queue, client_queue;
2979 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05002980 mbedtls_message_socket_init( &server_context );
2981 mbedtls_message_socket_init( &client_context );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002982
2983 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 3,
2984 &server,
2985 &server_context ) == 0 );
2986
2987 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 3,
2988 &client,
2989 &client_context ) == 0 );
2990
2991 /* Fill up the buffer with structured data so that unwanted changes
2992 * can be detected */
2993 for( i = 0; i < MSGLEN; i++ )
2994 {
2995 message[i] = i & 0xFF;
2996 }
2997 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
2998 MSGLEN*3 ) );
2999
3000 /* Interleaved test - [2 sends, 1 read] twice, and then two reads
3001 * (to wrap around the buffer) */
3002 for( i = 0; i < 2; i++ )
3003 {
3004 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
3005 MSGLEN ) == MSGLEN );
3006
3007 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
3008 MSGLEN ) == MSGLEN );
3009
3010 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received,
3011 MSGLEN ) == MSGLEN );
3012 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
3013 memset( received, 0, sizeof( received ) );
3014 }
3015
3016 for( i = 0; i < 2; i++ )
3017 {
3018 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received,
3019 MSGLEN ) == MSGLEN );
3020
3021 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
3022 }
3023 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
Andrzej Kurekf46b9122020-02-07 08:19:00 -05003024 == MBEDTLS_ERR_SSL_WANT_READ );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003025 exit:
3026 mbedtls_message_socket_close( &server_context );
3027 mbedtls_message_socket_close( &client_context );
3028}
3029/* END_CASE */
3030
3031/* BEGIN_CASE */
3032void ssl_message_mock_interleaved_two_ways( )
3033{
3034 enum { MSGLEN = 10 };
3035 unsigned char message[MSGLEN], received[MSGLEN];
3036 mbedtls_mock_socket client, server;
3037 unsigned i;
3038 mbedtls_test_message_queue server_queue, client_queue;
3039 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05003040 mbedtls_message_socket_init( &server_context );
3041 mbedtls_message_socket_init( &client_context );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003042
3043 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 3,
3044 &server,
3045 &server_context ) == 0 );
3046
3047 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 3,
3048 &client,
3049 &client_context ) == 0 );
3050
3051 /* Fill up the buffer with structured data so that unwanted changes
3052 * can be detected */
3053 for( i = 0; i < MSGLEN; i++ )
3054 {
3055 message[i] = i & 0xFF;
3056 }
3057 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
3058 MSGLEN*3 ) );
3059
3060 /* Interleaved test - [2 sends, 1 read] twice, both ways, and then two reads
3061 * (to wrap around the buffer) both ways. */
3062 for( i = 0; i < 2; i++ )
3063 {
3064 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
3065 MSGLEN ) == MSGLEN );
3066
3067 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
3068 MSGLEN ) == MSGLEN );
3069
3070 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &server_context, message,
3071 MSGLEN ) == MSGLEN );
3072
3073 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &server_context, message,
3074 MSGLEN ) == MSGLEN );
3075
3076 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received,
3077 MSGLEN ) == MSGLEN );
3078
3079 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
3080
3081 memset( received, 0, sizeof( received ) );
3082
3083 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &client_context, received,
3084 MSGLEN ) == MSGLEN );
3085
3086 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
3087
3088 memset( received, 0, sizeof( received ) );
3089 }
3090
3091 for( i = 0; i < 2; i++ )
3092 {
3093 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received,
3094 MSGLEN ) == MSGLEN );
3095
3096 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
3097 memset( received, 0, sizeof( received ) );
3098
3099 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &client_context, received,
3100 MSGLEN ) == MSGLEN );
3101
3102 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
3103 memset( received, 0, sizeof( received ) );
3104 }
3105
3106 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
Andrzej Kurekf46b9122020-02-07 08:19:00 -05003107 == MBEDTLS_ERR_SSL_WANT_READ );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003108
3109 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &client_context, received, MSGLEN )
Andrzej Kurekf46b9122020-02-07 08:19:00 -05003110 == MBEDTLS_ERR_SSL_WANT_READ );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003111 exit:
3112 mbedtls_message_socket_close( &server_context );
3113 mbedtls_message_socket_close( &client_context );
3114}
3115/* END_CASE */
3116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003117/* BEGIN_CASE depends_on:MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Azim Khan5fcca462018-06-29 11:05:32 +01003118void ssl_dtls_replay( data_t * prevs, data_t * new, int ret )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003119{
Azim Khand30ca132017-06-09 04:32:58 +01003120 uint32_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003121 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02003122 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003123
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02003124 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02003125 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02003126
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02003127 TEST_ASSERT( mbedtls_ssl_config_defaults( &conf,
3128 MBEDTLS_SSL_IS_CLIENT,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02003129 MBEDTLS_SSL_TRANSPORT_DATAGRAM,
3130 MBEDTLS_SSL_PRESET_DEFAULT ) == 0 );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02003131 TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003132
3133 /* Read previous record numbers */
Azim Khand30ca132017-06-09 04:32:58 +01003134 for( len = 0; len < prevs->len; len += 6 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003135 {
Azim Khand30ca132017-06-09 04:32:58 +01003136 memcpy( ssl.in_ctr + 2, prevs->x + len, 6 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003137 mbedtls_ssl_dtls_replay_update( &ssl );
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003138 }
3139
3140 /* Check new number */
Azim Khand30ca132017-06-09 04:32:58 +01003141 memcpy( ssl.in_ctr + 2, new->x, 6 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003142 TEST_ASSERT( mbedtls_ssl_dtls_replay_check( &ssl ) == ret );
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003144 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02003145 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003146}
3147/* END_CASE */
Hanno Beckerb25c0c72017-05-05 11:24:30 +01003148
3149/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
3150void ssl_set_hostname_twice( char *hostname0, char *hostname1 )
3151{
3152 mbedtls_ssl_context ssl;
3153 mbedtls_ssl_init( &ssl );
3154
3155 TEST_ASSERT( mbedtls_ssl_set_hostname( &ssl, hostname0 ) == 0 );
3156 TEST_ASSERT( mbedtls_ssl_set_hostname( &ssl, hostname1 ) == 0 );
3157
3158 mbedtls_ssl_free( &ssl );
3159}
Darryl Green11999bb2018-03-13 15:22:58 +00003160/* END_CASE */
Hanno Beckera18d1322018-01-03 14:27:32 +00003161
3162/* BEGIN_CASE */
3163void ssl_crypt_record( int cipher_type, int hash_id,
Hanno Beckerd856c822019-04-29 17:30:59 +01003164 int etm, int tag_mode, int ver,
3165 int cid0_len, int cid1_len )
Hanno Beckera18d1322018-01-03 14:27:32 +00003166{
3167 /*
3168 * Test several record encryptions and decryptions
3169 * with plenty of space before and after the data
3170 * within the record buffer.
3171 */
3172
3173 int ret;
3174 int num_records = 16;
3175 mbedtls_ssl_context ssl; /* ONLY for debugging */
3176
3177 mbedtls_ssl_transform t0, t1;
Hanno Becker81e16a32019-03-01 11:21:44 +00003178 unsigned char *buf = NULL;
Hanno Beckera18d1322018-01-03 14:27:32 +00003179 size_t const buflen = 512;
3180 mbedtls_record rec, rec_backup;
3181
3182 mbedtls_ssl_init( &ssl );
3183 mbedtls_ssl_transform_init( &t0 );
3184 mbedtls_ssl_transform_init( &t1 );
3185 TEST_ASSERT( build_transforms( &t0, &t1, cipher_type, hash_id,
Hanno Beckerd856c822019-04-29 17:30:59 +01003186 etm, tag_mode, ver,
3187 (size_t) cid0_len,
3188 (size_t) cid1_len ) == 0 );
Hanno Beckera18d1322018-01-03 14:27:32 +00003189
Hanno Becker3ee54212019-04-04 16:31:26 +01003190 TEST_ASSERT( ( buf = mbedtls_calloc( 1, buflen ) ) != NULL );
Hanno Beckera18d1322018-01-03 14:27:32 +00003191
3192 while( num_records-- > 0 )
3193 {
3194 mbedtls_ssl_transform *t_dec, *t_enc;
3195 /* Take turns in who's sending and who's receiving. */
3196 if( num_records % 3 == 0 )
3197 {
3198 t_dec = &t0;
3199 t_enc = &t1;
3200 }
3201 else
3202 {
3203 t_dec = &t1;
3204 t_enc = &t0;
3205 }
3206
3207 /*
3208 * The record header affects the transformation in two ways:
3209 * 1) It determines the AEAD additional data
3210 * 2) The record counter sometimes determines the IV.
3211 *
3212 * Apart from that, the fields don't have influence.
3213 * In particular, it is currently not the responsibility
3214 * of ssl_encrypt/decrypt_buf to check if the transform
3215 * version matches the record version, or that the
3216 * type is sensible.
3217 */
3218
3219 memset( rec.ctr, num_records, sizeof( rec.ctr ) );
3220 rec.type = 42;
3221 rec.ver[0] = num_records;
3222 rec.ver[1] = num_records;
Hanno Beckera0e20d02019-05-15 14:03:01 +01003223#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd856c822019-04-29 17:30:59 +01003224 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01003225#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera18d1322018-01-03 14:27:32 +00003226
3227 rec.buf = buf;
3228 rec.buf_len = buflen;
3229 rec.data_offset = 16;
3230 /* Make sure to vary the length to exercise different
3231 * paddings. */
3232 rec.data_len = 1 + num_records;
3233
3234 memset( rec.buf + rec.data_offset, 42, rec.data_len );
3235
3236 /* Make a copy for later comparison */
3237 rec_backup = rec;
3238
3239 /* Encrypt record */
3240 ret = mbedtls_ssl_encrypt_buf( &ssl, t_enc, &rec,
Ronald Cron351f0ee2020-06-10 12:12:18 +02003241 mbedtls_test_rnd_std_rand, NULL );
Hanno Beckera18d1322018-01-03 14:27:32 +00003242 TEST_ASSERT( ret == 0 || ret == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3243 if( ret != 0 )
3244 {
3245 continue;
3246 }
3247
Hanno Beckerb2713ab2020-05-07 14:54:22 +01003248#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3249 if( rec.cid_len != 0 )
3250 {
3251 /* DTLS 1.2 + CID hides the real content type and
3252 * uses a special CID content type in the protected
3253 * record. Double-check this. */
3254 TEST_ASSERT( rec.type == MBEDTLS_SSL_MSG_CID );
3255 }
3256#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3257
3258#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
3259 if( t_enc->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
3260 {
3261 /* TLS 1.3 hides the real content type and
3262 * always uses Application Data as the content type
3263 * for protected records. Double-check this. */
3264 TEST_ASSERT( rec.type == MBEDTLS_SSL_MSG_APPLICATION_DATA );
3265 }
3266#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
3267
Hanno Beckera18d1322018-01-03 14:27:32 +00003268 /* Decrypt record with t_dec */
Hanno Beckerd856c822019-04-29 17:30:59 +01003269 ret = mbedtls_ssl_decrypt_buf( &ssl, t_dec, &rec );
3270 TEST_ASSERT( ret == 0 );
Hanno Beckera18d1322018-01-03 14:27:32 +00003271
3272 /* Compare results */
3273 TEST_ASSERT( rec.type == rec_backup.type );
3274 TEST_ASSERT( memcmp( rec.ctr, rec_backup.ctr, 8 ) == 0 );
3275 TEST_ASSERT( rec.ver[0] == rec_backup.ver[0] );
3276 TEST_ASSERT( rec.ver[1] == rec_backup.ver[1] );
3277 TEST_ASSERT( rec.data_len == rec_backup.data_len );
3278 TEST_ASSERT( rec.data_offset == rec_backup.data_offset );
3279 TEST_ASSERT( memcmp( rec.buf + rec.data_offset,
3280 rec_backup.buf + rec_backup.data_offset,
3281 rec.data_len ) == 0 );
3282 }
3283
Hanno Becker81e16a32019-03-01 11:21:44 +00003284exit:
3285
Hanno Beckera18d1322018-01-03 14:27:32 +00003286 /* Cleanup */
3287 mbedtls_ssl_free( &ssl );
3288 mbedtls_ssl_transform_free( &t0 );
3289 mbedtls_ssl_transform_free( &t1 );
3290
Hanno Becker3ee54212019-04-04 16:31:26 +01003291 mbedtls_free( buf );
Hanno Beckera18d1322018-01-03 14:27:32 +00003292}
3293/* END_CASE */
Hanno Beckerb3268da2018-01-05 15:20:24 +00003294
3295/* BEGIN_CASE */
3296void ssl_crypt_record_small( int cipher_type, int hash_id,
Hanno Beckerd856c822019-04-29 17:30:59 +01003297 int etm, int tag_mode, int ver,
3298 int cid0_len, int cid1_len )
Hanno Beckerb3268da2018-01-05 15:20:24 +00003299{
3300 /*
3301 * Test pairs of encryption and decryption with an increasing
3302 * amount of space in the record buffer - in more detail:
3303 * 1) Try to encrypt with 0, 1, 2, ... bytes available
3304 * in front of the plaintext, and expect the encryption
3305 * to succeed starting from some offset. Always keep
3306 * enough space in the end of the buffer.
3307 * 2) Try to encrypt with 0, 1, 2, ... bytes available
3308 * at the end of the plaintext, and expect the encryption
3309 * to succeed starting from some offset. Always keep
3310 * enough space at the beginning of the buffer.
3311 * 3) Try to encrypt with 0, 1, 2, ... bytes available
3312 * both at the front and end of the plaintext,
3313 * and expect the encryption to succeed starting from
3314 * some offset.
3315 *
3316 * If encryption succeeds, check that decryption succeeds
3317 * and yields the original record.
3318 */
3319
3320 mbedtls_ssl_context ssl; /* ONLY for debugging */
3321
3322 mbedtls_ssl_transform t0, t1;
Hanno Becker81e16a32019-03-01 11:21:44 +00003323 unsigned char *buf = NULL;
Hanno Beckerd856c822019-04-29 17:30:59 +01003324 size_t const buflen = 256;
Hanno Beckerb3268da2018-01-05 15:20:24 +00003325 mbedtls_record rec, rec_backup;
3326
3327 int ret;
Hanno Beckerd856c822019-04-29 17:30:59 +01003328 int mode; /* Mode 1, 2 or 3 as explained above */
3329 size_t offset; /* Available space at beginning/end/both */
3330 size_t threshold = 96; /* Maximum offset to test against */
Hanno Beckerb3268da2018-01-05 15:20:24 +00003331
Hanno Beckerd856c822019-04-29 17:30:59 +01003332 size_t default_pre_padding = 64; /* Pre-padding to use in mode 2 */
3333 size_t default_post_padding = 128; /* Post-padding to use in mode 1 */
Hanno Beckerb3268da2018-01-05 15:20:24 +00003334
3335 int seen_success; /* Indicates if in the current mode we've
3336 * already seen a successful test. */
3337
3338 mbedtls_ssl_init( &ssl );
3339 mbedtls_ssl_transform_init( &t0 );
3340 mbedtls_ssl_transform_init( &t1 );
3341 TEST_ASSERT( build_transforms( &t0, &t1, cipher_type, hash_id,
Hanno Beckerd856c822019-04-29 17:30:59 +01003342 etm, tag_mode, ver,
3343 (size_t) cid0_len,
3344 (size_t) cid1_len ) == 0 );
Hanno Beckerb3268da2018-01-05 15:20:24 +00003345
Hanno Becker3ee54212019-04-04 16:31:26 +01003346 TEST_ASSERT( ( buf = mbedtls_calloc( 1, buflen ) ) != NULL );
Hanno Beckerb3268da2018-01-05 15:20:24 +00003347
3348 for( mode=1; mode <= 3; mode++ )
3349 {
3350 seen_success = 0;
3351 for( offset=0; offset <= threshold; offset++ )
3352 {
3353 mbedtls_ssl_transform *t_dec, *t_enc;
Hanno Becker6c87b3f2019-04-29 17:24:44 +01003354 t_dec = &t0;
3355 t_enc = &t1;
Hanno Beckerb3268da2018-01-05 15:20:24 +00003356
3357 memset( rec.ctr, offset, sizeof( rec.ctr ) );
3358 rec.type = 42;
3359 rec.ver[0] = offset;
3360 rec.ver[1] = offset;
3361 rec.buf = buf;
3362 rec.buf_len = buflen;
Hanno Beckera0e20d02019-05-15 14:03:01 +01003363#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd856c822019-04-29 17:30:59 +01003364 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01003365#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerb3268da2018-01-05 15:20:24 +00003366
3367 switch( mode )
3368 {
3369 case 1: /* Space in the beginning */
3370 rec.data_offset = offset;
3371 rec.data_len = buflen - offset - default_post_padding;
3372 break;
3373
3374 case 2: /* Space in the end */
3375 rec.data_offset = default_pre_padding;
3376 rec.data_len = buflen - default_pre_padding - offset;
3377 break;
3378
3379 case 3: /* Space in the beginning and end */
3380 rec.data_offset = offset;
3381 rec.data_len = buflen - 2 * offset;
3382 break;
3383
3384 default:
3385 TEST_ASSERT( 0 );
3386 break;
3387 }
3388
3389 memset( rec.buf + rec.data_offset, 42, rec.data_len );
3390
3391 /* Make a copy for later comparison */
3392 rec_backup = rec;
3393
3394 /* Encrypt record */
Ronald Cron6c5bd7f2020-06-10 14:08:26 +02003395 ret = mbedtls_ssl_encrypt_buf( &ssl, t_enc, &rec,
3396 mbedtls_test_rnd_std_rand, NULL );
Hanno Beckerb3268da2018-01-05 15:20:24 +00003397
3398 if( ( mode == 1 || mode == 2 ) && seen_success )
3399 {
3400 TEST_ASSERT( ret == 0 );
3401 }
3402 else
3403 {
3404 TEST_ASSERT( ret == 0 || ret == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3405 if( ret == 0 )
3406 seen_success = 1;
3407 }
3408
3409 if( ret != 0 )
3410 continue;
3411
Hanno Beckerb2713ab2020-05-07 14:54:22 +01003412#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3413 if( rec.cid_len != 0 )
3414 {
3415 /* DTLS 1.2 + CID hides the real content type and
3416 * uses a special CID content type in the protected
3417 * record. Double-check this. */
3418 TEST_ASSERT( rec.type == MBEDTLS_SSL_MSG_CID );
3419 }
3420#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3421
3422#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
3423 if( t_enc->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
3424 {
3425 /* TLS 1.3 hides the real content type and
3426 * always uses Application Data as the content type
3427 * for protected records. Double-check this. */
3428 TEST_ASSERT( rec.type == MBEDTLS_SSL_MSG_APPLICATION_DATA );
3429 }
3430#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
3431
Hanno Beckerb3268da2018-01-05 15:20:24 +00003432 /* Decrypt record with t_dec */
3433 TEST_ASSERT( mbedtls_ssl_decrypt_buf( &ssl, t_dec, &rec ) == 0 );
3434
3435 /* Compare results */
3436 TEST_ASSERT( rec.type == rec_backup.type );
3437 TEST_ASSERT( memcmp( rec.ctr, rec_backup.ctr, 8 ) == 0 );
3438 TEST_ASSERT( rec.ver[0] == rec_backup.ver[0] );
3439 TEST_ASSERT( rec.ver[1] == rec_backup.ver[1] );
3440 TEST_ASSERT( rec.data_len == rec_backup.data_len );
3441 TEST_ASSERT( rec.data_offset == rec_backup.data_offset );
3442 TEST_ASSERT( memcmp( rec.buf + rec.data_offset,
3443 rec_backup.buf + rec_backup.data_offset,
3444 rec.data_len ) == 0 );
3445 }
3446
3447 TEST_ASSERT( seen_success == 1 );
3448 }
3449
Hanno Becker81e16a32019-03-01 11:21:44 +00003450exit:
3451
Hanno Beckerb3268da2018-01-05 15:20:24 +00003452 /* Cleanup */
3453 mbedtls_ssl_free( &ssl );
3454 mbedtls_ssl_transform_free( &t0 );
3455 mbedtls_ssl_transform_free( &t1 );
3456
Hanno Becker3ee54212019-04-04 16:31:26 +01003457 mbedtls_free( buf );
Hanno Beckerb3268da2018-01-05 15:20:24 +00003458}
3459/* END_CASE */
Ron Eldor824ad7b2019-05-13 14:09:00 +03003460
Manuel Pégourié-Gonnard913a2042020-07-09 10:02:41 +02003461/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC:MBEDTLS_AES_C:MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003462void ssl_decrypt_non_etm_cbc( int cipher_type, int hash_id, int trunc_hmac,
Manuel Pégourié-Gonnard864abbf2020-07-21 10:37:14 +02003463 int length_selector )
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003464{
3465 /*
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003466 * Test record decryption for CBC without EtM, focused on the verification
3467 * of padding and MAC.
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003468 *
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01003469 * Actually depends on TLS >= 1.0 and either AES, ARIA, Camellia or DES,
3470 * but since the test framework doesn't support alternation in
3471 * dependency statements, just depend on TLS 1.2 and AES.
Manuel Pégourié-Gonnard864abbf2020-07-21 10:37:14 +02003472 *
3473 * The length_selector argument is interpreted as follows:
3474 * - if it's -1, the plaintext length is 0 and minimal padding is applied
3475 * - if it's -2, the plaintext length is 0 and maximal padding is applied
3476 * - otherwise it must be in [0, 255] and is padding_length from RFC 5246:
3477 * it's the length of the rest of the padding, that is, excluding the
3478 * byte that encodes the length. The minimal non-zero plaintext length
3479 * that gives this padding_length is automatically selected.
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003480 */
3481 mbedtls_ssl_context ssl; /* ONLY for debugging */
3482 mbedtls_ssl_transform t0, t1;
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003483 mbedtls_record rec, rec_save;
3484 unsigned char *buf = NULL, *buf_save = NULL;
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003485 size_t buflen, olen = 0;
Manuel Pégourié-Gonnard864abbf2020-07-21 10:37:14 +02003486 size_t plaintext_len, block_size, i;
Manuel Pégourié-Gonnarde55653f2020-07-22 11:42:57 +02003487 unsigned char padlen; /* excluding the padding_length byte */
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003488 unsigned char add_data[13];
3489 unsigned char mac[MBEDTLS_MD_MAX_SIZE];
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003490 int exp_ret;
Manuel Pégourié-Gonnard4adc04a2020-07-16 10:00:48 +02003491 const unsigned char pad_max_len = 255; /* Per the standard */
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003492
3493 mbedtls_ssl_init( &ssl );
3494 mbedtls_ssl_transform_init( &t0 );
3495 mbedtls_ssl_transform_init( &t1 );
3496
3497 /* Set up transforms with dummy keys */
3498 TEST_ASSERT( build_transforms( &t0, &t1, cipher_type, hash_id,
3499 0, trunc_hmac,
3500 MBEDTLS_SSL_MINOR_VERSION_3,
3501 0 , 0 ) == 0 );
3502
Manuel Pégourié-Gonnard864abbf2020-07-21 10:37:14 +02003503 /* Determine padding/plaintext length */
3504 TEST_ASSERT( length_selector >= -2 && length_selector <= 255 );
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003505 block_size = t0.ivlen;
Manuel Pégourié-Gonnard864abbf2020-07-21 10:37:14 +02003506 if( length_selector < 0 )
3507 {
3508 plaintext_len = 0;
3509
Manuel Pégourié-Gonnarde55653f2020-07-22 11:42:57 +02003510 /* Minimal padding
3511 * The +1 is for the padding_length byte, not counted in padlen. */
Manuel Pégourié-Gonnard864abbf2020-07-21 10:37:14 +02003512 padlen = block_size - ( t0.maclen + 1 ) % block_size;
3513
3514 /* Maximal padding? */
3515 if( length_selector == -2 )
3516 padlen += block_size * ( ( pad_max_len - padlen ) / block_size );
3517 }
3518 else
3519 {
3520 padlen = length_selector;
3521
Manuel Pégourié-Gonnarde55653f2020-07-22 11:42:57 +02003522 /* Minimal non-zero plaintext_length giving desired padding.
3523 * The +1 is for the padding_length byte, not counted in padlen. */
Manuel Pégourié-Gonnard864abbf2020-07-21 10:37:14 +02003524 plaintext_len = block_size - ( padlen + t0.maclen + 1 ) % block_size;
3525 }
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003526
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003527 /* Prepare a buffer for record data */
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003528 buflen = block_size
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003529 + plaintext_len
3530 + t0.maclen
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003531 + padlen + 1;
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003532 ASSERT_ALLOC( buf, buflen );
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003533 ASSERT_ALLOC( buf_save, buflen );
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003534
3535 /* Prepare a dummy record header */
3536 memset( rec.ctr, 0, sizeof( rec.ctr ) );
3537 rec.type = MBEDTLS_SSL_MSG_APPLICATION_DATA;
3538 rec.ver[0] = MBEDTLS_SSL_MAJOR_VERSION_3;
3539 rec.ver[1] = MBEDTLS_SSL_MINOR_VERSION_3;
3540#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3541 rec.cid_len = 0;
3542#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3543
3544 /* Prepare dummy record content */
3545 rec.buf = buf;
3546 rec.buf_len = buflen;
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003547 rec.data_offset = block_size;
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003548 rec.data_len = plaintext_len;
3549 memset( rec.buf + rec.data_offset, 42, rec.data_len );
3550
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003551 /* Serialized version of record header for MAC purposes */
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003552 memcpy( add_data, rec.ctr, 8 );
3553 add_data[8] = rec.type;
3554 add_data[9] = rec.ver[0];
3555 add_data[10] = rec.ver[1];
3556 add_data[11] = ( rec.data_len >> 8 ) & 0xff;
3557 add_data[12] = ( rec.data_len >> 0 ) & 0xff;
3558
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003559 /* Set dummy IV */
3560 memset( t0.iv_enc, 0x55, t0.ivlen );
3561 memcpy( rec.buf, t0.iv_enc, t0.ivlen );
3562
3563 /*
3564 * Prepare a pre-encryption record (with MAC and padding), and save it.
3565 */
3566
3567 /* MAC with additional data */
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003568 TEST_EQUAL( 0, mbedtls_md_hmac_update( &t0.md_ctx_enc, add_data, 13 ) );
3569 TEST_EQUAL( 0, mbedtls_md_hmac_update( &t0.md_ctx_enc,
3570 rec.buf + rec.data_offset,
3571 rec.data_len ) );
3572 TEST_EQUAL( 0, mbedtls_md_hmac_finish( &t0.md_ctx_enc, mac ) );
3573
3574 memcpy( rec.buf + rec.data_offset + rec.data_len, mac, t0.maclen );
3575 rec.data_len += t0.maclen;
3576
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003577 /* Pad */
3578 memset( rec.buf + rec.data_offset + rec.data_len, padlen, padlen + 1 );
3579 rec.data_len += padlen + 1;
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003580
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003581 /* Save correct pre-encryption record */
3582 rec_save = rec;
3583 rec_save.buf = buf_save;
3584 memcpy( buf_save, buf, buflen );
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003585
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003586 /*
3587 * Encrypt and decrypt the correct record, expecting success
3588 */
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003589 TEST_EQUAL( 0, mbedtls_cipher_crypt( &t0.cipher_ctx_enc,
3590 t0.iv_enc, t0.ivlen,
3591 rec.buf + rec.data_offset, rec.data_len,
3592 rec.buf + rec.data_offset, &olen ) );
3593 rec.data_offset -= t0.ivlen;
3594 rec.data_len += t0.ivlen;
3595
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003596 TEST_EQUAL( 0, mbedtls_ssl_decrypt_buf( &ssl, &t1, &rec ) );
3597
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003598 /*
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003599 * Modify each byte of the pre-encryption record before encrypting and
3600 * decrypting it, expecting failure every time.
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003601 */
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003602 for( i = block_size; i < buflen; i++ )
3603 {
Chris Jones9634bb12021-01-20 15:56:42 +00003604 mbedtls_test_set_step( i );
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003605
3606 /* Restore correct pre-encryption record */
3607 rec = rec_save;
3608 rec.buf = buf;
3609 memcpy( buf, buf_save, buflen );
3610
Manuel Pégourié-Gonnardb51f0442020-07-21 10:40:25 +02003611 /* Corrupt one byte of the data (could be plaintext, MAC or padding) */
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003612 rec.buf[i] ^= 0x01;
3613
3614 /* Encrypt */
3615 TEST_EQUAL( 0, mbedtls_cipher_crypt( &t0.cipher_ctx_enc,
3616 t0.iv_enc, t0.ivlen,
3617 rec.buf + rec.data_offset, rec.data_len,
3618 rec.buf + rec.data_offset, &olen ) );
3619 rec.data_offset -= t0.ivlen;
3620 rec.data_len += t0.ivlen;
3621
3622 /* Decrypt and expect failure */
3623 TEST_EQUAL( MBEDTLS_ERR_SSL_INVALID_MAC,
3624 mbedtls_ssl_decrypt_buf( &ssl, &t1, &rec ) );
3625 }
3626
3627 /*
3628 * Use larger values of the padding bytes - with small buffers, this tests
3629 * the case where the announced padlen would be larger than the buffer
3630 * (and before that, than the buffer minus the size of the MAC), to make
3631 * sure our padding checking code does not perform any out-of-bounds reads
3632 * in this case. (With larger buffers, ie when the plaintext is long or
3633 * maximal length padding is used, this is less relevant but still doesn't
3634 * hurt to test.)
3635 *
3636 * (Start the loop with correct padding, just to double-check that record
3637 * saving did work, and that we're overwriting the correct bytes.)
3638 */
Manuel Pégourié-Gonnard4adc04a2020-07-16 10:00:48 +02003639 for( i = padlen; i <= pad_max_len; i++ )
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003640 {
Chris Jones9634bb12021-01-20 15:56:42 +00003641 mbedtls_test_set_step( i );
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003642
3643 /* Restore correct pre-encryption record */
3644 rec = rec_save;
3645 rec.buf = buf;
3646 memcpy( buf, buf_save, buflen );
3647
3648 /* Set padding bytes to new value */
3649 memset( buf + buflen - padlen - 1, i, padlen + 1 );
3650
3651 /* Encrypt */
3652 TEST_EQUAL( 0, mbedtls_cipher_crypt( &t0.cipher_ctx_enc,
3653 t0.iv_enc, t0.ivlen,
3654 rec.buf + rec.data_offset, rec.data_len,
3655 rec.buf + rec.data_offset, &olen ) );
3656 rec.data_offset -= t0.ivlen;
3657 rec.data_len += t0.ivlen;
3658
3659 /* Decrypt and expect failure except the first time */
3660 exp_ret = ( i == padlen ) ? 0 : MBEDTLS_ERR_SSL_INVALID_MAC;
3661 TEST_EQUAL( exp_ret, mbedtls_ssl_decrypt_buf( &ssl, &t1, &rec ) );
3662 }
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003663
3664exit:
3665 mbedtls_ssl_free( &ssl );
3666 mbedtls_ssl_transform_free( &t0 );
3667 mbedtls_ssl_transform_free( &t1 );
3668 mbedtls_free( buf );
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003669 mbedtls_free( buf_save );
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003670}
3671/* END_CASE */
3672
Hanno Becker39ff4922020-08-21 13:36:56 +01003673/* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
3674void ssl_tls1_3_hkdf_expand_label( int hash_alg,
3675 data_t *secret,
Hanno Becker70d7fb02020-09-09 10:11:21 +01003676 int label_idx,
Hanno Becker39ff4922020-08-21 13:36:56 +01003677 data_t *ctx,
3678 int desired_length,
3679 data_t *expected )
3680{
3681 unsigned char dst[ 100 ];
3682
Hanno Becker70d7fb02020-09-09 10:11:21 +01003683 unsigned char const *lbl = NULL;
3684 size_t lbl_len;
Hanno Becker1413bd82020-09-09 12:46:09 +01003685#define MBEDTLS_SSL_TLS1_3_LABEL( name, string ) \
3686 if( label_idx == (int) tls1_3_label_ ## name ) \
Hanno Becker70d7fb02020-09-09 10:11:21 +01003687 { \
3688 lbl = mbedtls_ssl_tls1_3_labels.name; \
3689 lbl_len = sizeof( mbedtls_ssl_tls1_3_labels.name ); \
3690 }
3691MBEDTLS_SSL_TLS1_3_LABEL_LIST
3692#undef MBEDTLS_SSL_TLS1_3_LABEL
3693 TEST_ASSERT( lbl != NULL );
Hanno Becker39ff4922020-08-21 13:36:56 +01003694
3695 /* Check sanity of test parameters. */
3696 TEST_ASSERT( (size_t) desired_length <= sizeof(dst) );
3697 TEST_ASSERT( (size_t) desired_length == expected->len );
3698
3699 TEST_ASSERT( mbedtls_ssl_tls1_3_hkdf_expand_label(
3700 (mbedtls_md_type_t) hash_alg,
3701 secret->x, secret->len,
Hanno Becker70d7fb02020-09-09 10:11:21 +01003702 lbl, lbl_len,
Hanno Becker39ff4922020-08-21 13:36:56 +01003703 ctx->x, ctx->len,
3704 dst, desired_length ) == 0 );
3705
Hanno Beckerfb080962020-09-08 10:58:42 +01003706 ASSERT_COMPARE( dst, (size_t) desired_length,
3707 expected->x, (size_t) expected->len );
Hanno Becker39ff4922020-08-21 13:36:56 +01003708}
3709/* END_CASE */
3710
Hanno Becker19498f82020-08-21 13:37:08 +01003711/* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
3712void ssl_tls1_3_traffic_key_generation( int hash_alg,
3713 data_t *server_secret,
3714 data_t *client_secret,
3715 int desired_iv_len,
3716 int desired_key_len,
3717 data_t *expected_server_write_key,
3718 data_t *expected_server_write_iv,
3719 data_t *expected_client_write_key,
3720 data_t *expected_client_write_iv )
3721{
3722 mbedtls_ssl_key_set keys;
3723
3724 /* Check sanity of test parameters. */
3725 TEST_ASSERT( client_secret->len == server_secret->len );
3726 TEST_ASSERT( expected_client_write_iv->len == expected_server_write_iv->len &&
3727 expected_client_write_iv->len == (size_t) desired_iv_len );
3728 TEST_ASSERT( expected_client_write_key->len == expected_server_write_key->len &&
3729 expected_client_write_key->len == (size_t) desired_key_len );
3730
3731 TEST_ASSERT( mbedtls_ssl_tls1_3_make_traffic_keys(
3732 (mbedtls_md_type_t) hash_alg,
3733 client_secret->x,
3734 server_secret->x,
3735 client_secret->len /* == server_secret->len */,
3736 desired_key_len, desired_iv_len,
3737 &keys ) == 0 );
3738
Hanno Beckerfb080962020-09-08 10:58:42 +01003739 ASSERT_COMPARE( keys.client_write_key,
Hanno Becker493ea7f2020-09-08 11:01:00 +01003740 keys.key_len,
Hanno Beckerfb080962020-09-08 10:58:42 +01003741 expected_client_write_key->x,
3742 (size_t) desired_key_len );
3743 ASSERT_COMPARE( keys.server_write_key,
Hanno Becker493ea7f2020-09-08 11:01:00 +01003744 keys.key_len,
Hanno Beckerfb080962020-09-08 10:58:42 +01003745 expected_server_write_key->x,
3746 (size_t) desired_key_len );
3747 ASSERT_COMPARE( keys.client_write_iv,
Hanno Becker493ea7f2020-09-08 11:01:00 +01003748 keys.iv_len,
Hanno Beckerfb080962020-09-08 10:58:42 +01003749 expected_client_write_iv->x,
3750 (size_t) desired_iv_len );
3751 ASSERT_COMPARE( keys.server_write_iv,
Hanno Becker493ea7f2020-09-08 11:01:00 +01003752 keys.iv_len,
Hanno Beckerfb080962020-09-08 10:58:42 +01003753 expected_server_write_iv->x,
3754 (size_t) desired_iv_len );
Hanno Becker19498f82020-08-21 13:37:08 +01003755}
3756/* END_CASE */
3757
Hanno Beckere4849d12020-08-21 14:14:14 +01003758/* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
3759void ssl_tls1_3_derive_secret( int hash_alg,
3760 data_t *secret,
Hanno Becker70d7fb02020-09-09 10:11:21 +01003761 int label_idx,
Hanno Beckere4849d12020-08-21 14:14:14 +01003762 data_t *ctx,
3763 int desired_length,
3764 int already_hashed,
3765 data_t *expected )
3766{
3767 unsigned char dst[ 100 ];
3768
Hanno Becker70d7fb02020-09-09 10:11:21 +01003769 unsigned char const *lbl = NULL;
3770 size_t lbl_len;
Hanno Becker1413bd82020-09-09 12:46:09 +01003771#define MBEDTLS_SSL_TLS1_3_LABEL( name, string ) \
3772 if( label_idx == (int) tls1_3_label_ ## name ) \
Hanno Becker70d7fb02020-09-09 10:11:21 +01003773 { \
3774 lbl = mbedtls_ssl_tls1_3_labels.name; \
3775 lbl_len = sizeof( mbedtls_ssl_tls1_3_labels.name ); \
3776 }
3777MBEDTLS_SSL_TLS1_3_LABEL_LIST
3778#undef MBEDTLS_SSL_TLS1_3_LABEL
3779 TEST_ASSERT( lbl != NULL );
3780
Hanno Beckere4849d12020-08-21 14:14:14 +01003781 /* Check sanity of test parameters. */
3782 TEST_ASSERT( (size_t) desired_length <= sizeof(dst) );
3783 TEST_ASSERT( (size_t) desired_length == expected->len );
3784
3785 TEST_ASSERT( mbedtls_ssl_tls1_3_derive_secret(
3786 (mbedtls_md_type_t) hash_alg,
3787 secret->x, secret->len,
Hanno Becker70d7fb02020-09-09 10:11:21 +01003788 lbl, lbl_len,
Hanno Beckere4849d12020-08-21 14:14:14 +01003789 ctx->x, ctx->len,
3790 already_hashed,
3791 dst, desired_length ) == 0 );
3792
Hanno Beckerfb080962020-09-08 10:58:42 +01003793 ASSERT_COMPARE( dst, desired_length,
3794 expected->x, desired_length );
Hanno Beckere4849d12020-08-21 14:14:14 +01003795}
3796/* END_CASE */
3797
Hanno Becker2d2c3eb2020-08-20 14:54:24 +01003798/* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
Hanno Beckera4f40a02021-05-24 06:42:11 +01003799void ssl_tls1_3_derive_early_secrets( int hash_alg,
3800 data_t *secret,
3801 data_t *transcript,
3802 data_t *traffic_expected,
3803 data_t *exporter_expected )
3804{
3805 mbedtls_ssl_tls1_3_early_secrets secrets;
3806
3807 /* Double-check that we've passed sane parameters. */
3808 mbedtls_md_type_t md_type = (mbedtls_md_type_t) hash_alg;
3809 mbedtls_md_info_t const * const md_info = mbedtls_md_info_from_type( md_type );
3810 size_t const md_size = mbedtls_md_get_size( md_info );
3811 TEST_ASSERT( md_info != 0 &&
3812 secret->len == md_size &&
3813 transcript->len == md_size &&
3814 traffic_expected->len == md_size &&
3815 exporter_expected->len == md_size );
3816
3817 TEST_ASSERT( mbedtls_ssl_tls1_3_derive_early_secrets(
3818 md_type, secret->x, transcript->x, transcript->len,
3819 &secrets ) == 0 );
3820
3821 ASSERT_COMPARE( secrets.client_early_traffic_secret, md_size,
3822 traffic_expected->x, traffic_expected->len );
3823 ASSERT_COMPARE( secrets.early_exporter_master_secret, md_size,
3824 exporter_expected->x, exporter_expected->len );
3825}
3826/* END_CASE */
3827
3828/* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
3829void ssl_tls1_3_derive_handshake_secrets( int hash_alg,
3830 data_t *secret,
3831 data_t *transcript,
3832 data_t *client_expected,
3833 data_t *server_expected )
3834{
3835 mbedtls_ssl_tls1_3_handshake_secrets secrets;
3836
3837 /* Double-check that we've passed sane parameters. */
3838 mbedtls_md_type_t md_type = (mbedtls_md_type_t) hash_alg;
3839 mbedtls_md_info_t const * const md_info = mbedtls_md_info_from_type( md_type );
3840 size_t const md_size = mbedtls_md_get_size( md_info );
3841 TEST_ASSERT( md_info != 0 &&
3842 secret->len == md_size &&
3843 transcript->len == md_size &&
3844 client_expected->len == md_size &&
3845 server_expected->len == md_size );
3846
3847 TEST_ASSERT( mbedtls_ssl_tls1_3_derive_handshake_secrets(
3848 md_type, secret->x, transcript->x, transcript->len,
3849 &secrets ) == 0 );
3850
3851 ASSERT_COMPARE( secrets.client_handshake_traffic_secret, md_size,
3852 client_expected->x, client_expected->len );
3853 ASSERT_COMPARE( secrets.server_handshake_traffic_secret, md_size,
3854 server_expected->x, server_expected->len );
3855}
3856/* END_CASE */
3857
3858/* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
3859void ssl_tls1_3_derive_application_secrets( int hash_alg,
3860 data_t *secret,
3861 data_t *transcript,
3862 data_t *client_expected,
3863 data_t *server_expected,
3864 data_t *exporter_expected )
3865{
3866 mbedtls_ssl_tls1_3_application_secrets secrets;
3867
3868 /* Double-check that we've passed sane parameters. */
3869 mbedtls_md_type_t md_type = (mbedtls_md_type_t) hash_alg;
3870 mbedtls_md_info_t const * const md_info = mbedtls_md_info_from_type( md_type );
3871 size_t const md_size = mbedtls_md_get_size( md_info );
3872 TEST_ASSERT( md_info != 0 &&
3873 secret->len == md_size &&
3874 transcript->len == md_size &&
3875 client_expected->len == md_size &&
3876 server_expected->len == md_size &&
3877 exporter_expected->len == md_size );
3878
3879 TEST_ASSERT( mbedtls_ssl_tls1_3_derive_application_secrets(
3880 md_type, secret->x, transcript->x, transcript->len,
3881 &secrets ) == 0 );
3882
3883 ASSERT_COMPARE( secrets.client_application_traffic_secret_N, md_size,
3884 client_expected->x, client_expected->len );
3885 ASSERT_COMPARE( secrets.server_application_traffic_secret_N, md_size,
3886 server_expected->x, server_expected->len );
3887 ASSERT_COMPARE( secrets.exporter_master_secret, md_size,
3888 exporter_expected->x, exporter_expected->len );
3889}
3890/* END_CASE */
3891
3892/* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
3893void ssl_tls1_3_derive_resumption_secrets( int hash_alg,
3894 data_t *secret,
3895 data_t *transcript,
3896 data_t *resumption_expected )
3897{
3898 mbedtls_ssl_tls1_3_application_secrets secrets;
3899
3900 /* Double-check that we've passed sane parameters. */
3901 mbedtls_md_type_t md_type = (mbedtls_md_type_t) hash_alg;
3902 mbedtls_md_info_t const * const md_info = mbedtls_md_info_from_type( md_type );
3903 size_t const md_size = mbedtls_md_get_size( md_info );
3904 TEST_ASSERT( md_info != 0 &&
3905 secret->len == md_size &&
3906 transcript->len == md_size &&
3907 resumption_expected->len == md_size );
3908
3909 TEST_ASSERT( mbedtls_ssl_tls1_3_derive_resumption_master_secret(
3910 md_type, secret->x, transcript->x, transcript->len,
3911 &secrets ) == 0 );
3912
3913 ASSERT_COMPARE( secrets.resumption_master_secret, md_size,
3914 resumption_expected->x, resumption_expected->len );
3915}
3916/* END_CASE */
3917
3918/* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
Hanno Becker55bc2c52021-05-24 06:53:52 +01003919void ssl_tls1_3_create_psk_binder( int hash_alg,
3920 data_t *psk,
3921 int psk_type,
3922 data_t *transcript,
3923 data_t *binder_expected )
3924{
3925 unsigned char binder[ MBEDTLS_MD_MAX_SIZE ];
3926
3927 /* Double-check that we've passed sane parameters. */
3928 mbedtls_md_type_t md_type = (mbedtls_md_type_t) hash_alg;
3929 mbedtls_md_info_t const * const md_info = mbedtls_md_info_from_type( md_type );
3930 size_t const md_size = mbedtls_md_get_size( md_info );
3931 TEST_ASSERT( md_info != 0 &&
3932 transcript->len == md_size &&
3933 binder_expected->len == md_size );
3934
3935 TEST_ASSERT( mbedtls_ssl_tls1_3_create_psk_binder(
3936 NULL, /* SSL context for debugging only */
3937 md_type,
3938 psk->x, psk->len,
3939 psk_type,
3940 transcript->x,
3941 binder ) == 0 );
3942
3943 ASSERT_COMPARE( binder, md_size,
3944 binder_expected->x, binder_expected->len );
3945}
3946/* END_CASE */
3947
3948/* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
Hanno Becker2d2c3eb2020-08-20 14:54:24 +01003949void ssl_tls1_3_key_evolution( int hash_alg,
3950 data_t *secret,
3951 data_t *input,
3952 data_t *expected )
3953{
3954 unsigned char secret_new[ MBEDTLS_MD_MAX_SIZE ];
3955
3956 TEST_ASSERT( mbedtls_ssl_tls1_3_evolve_secret(
3957 (mbedtls_md_type_t) hash_alg,
3958 secret->len ? secret->x : NULL,
3959 input->len ? input->x : NULL, input->len,
3960 secret_new ) == 0 );
3961
Hanno Beckerfb080962020-09-08 10:58:42 +01003962 ASSERT_COMPARE( secret_new, (size_t) expected->len,
3963 expected->x, (size_t) expected->len );
Hanno Becker2d2c3eb2020-08-20 14:54:24 +01003964}
3965/* END_CASE */
3966
Ron Eldor824ad7b2019-05-13 14:09:00 +03003967/* BEGIN_CASE */
3968void ssl_tls_prf( int type, data_t * secret, data_t * random,
Ronald Cronac6ae352020-06-26 14:33:03 +02003969 char *label, data_t *result_str, int exp_ret )
Ron Eldor824ad7b2019-05-13 14:09:00 +03003970{
3971 unsigned char *output;
3972
Ronald Cronac6ae352020-06-26 14:33:03 +02003973 output = mbedtls_calloc( 1, result_str->len );
Ron Eldor824ad7b2019-05-13 14:09:00 +03003974 if( output == NULL )
3975 goto exit;
3976
Gilles Peskine9de97e22021-02-02 21:00:11 +01003977 USE_PSA_INIT( );
Ron Eldor6b9b1b82019-05-15 17:04:33 +03003978
Ron Eldor824ad7b2019-05-13 14:09:00 +03003979 TEST_ASSERT( mbedtls_ssl_tls_prf( type, secret->x, secret->len,
3980 label, random->x, random->len,
Ronald Cronac6ae352020-06-26 14:33:03 +02003981 output, result_str->len ) == exp_ret );
Ron Eldor824ad7b2019-05-13 14:09:00 +03003982
3983 if( exp_ret == 0 )
3984 {
Ronald Cronac6ae352020-06-26 14:33:03 +02003985 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
3986 result_str->len, result_str->len ) == 0 );
Ron Eldor824ad7b2019-05-13 14:09:00 +03003987 }
3988exit:
3989
3990 mbedtls_free( output );
Gilles Peskine1f186ff2021-02-02 21:04:06 +01003991 USE_PSA_DONE( );
Ron Eldor824ad7b2019-05-13 14:09:00 +03003992}
3993/* END_CASE */
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02003994
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02003995/* BEGIN_CASE */
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02003996void ssl_serialize_session_save_load( int ticket_len, char *crt_file )
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02003997{
3998 mbedtls_ssl_session original, restored;
3999 unsigned char *buf = NULL;
4000 size_t len;
4001
4002 /*
4003 * Test that a save-load pair is the identity
4004 */
4005
4006 mbedtls_ssl_session_init( &original );
4007 mbedtls_ssl_session_init( &restored );
4008
4009 /* Prepare a dummy session to work on */
4010 TEST_ASSERT( ssl_populate_session( &original, ticket_len, crt_file ) == 0 );
4011
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02004012 /* Serialize it */
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02004013 TEST_ASSERT( mbedtls_ssl_session_save( &original, NULL, 0, &len )
4014 == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4015 TEST_ASSERT( ( buf = mbedtls_calloc( 1, len ) ) != NULL );
4016 TEST_ASSERT( mbedtls_ssl_session_save( &original, buf, len, &len )
4017 == 0 );
4018
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02004019 /* Restore session from serialized data */
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02004020 TEST_ASSERT( mbedtls_ssl_session_load( &restored, buf, len) == 0 );
4021
4022 /*
4023 * Make sure both session structures are identical
4024 */
4025#if defined(MBEDTLS_HAVE_TIME)
4026 TEST_ASSERT( original.start == restored.start );
4027#endif
4028 TEST_ASSERT( original.ciphersuite == restored.ciphersuite );
4029 TEST_ASSERT( original.compression == restored.compression );
4030 TEST_ASSERT( original.id_len == restored.id_len );
4031 TEST_ASSERT( memcmp( original.id,
4032 restored.id, sizeof( original.id ) ) == 0 );
4033 TEST_ASSERT( memcmp( original.master,
4034 restored.master, sizeof( original.master ) ) == 0 );
4035
4036#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02004037#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02004038 TEST_ASSERT( ( original.peer_cert == NULL ) ==
4039 ( restored.peer_cert == NULL ) );
4040 if( original.peer_cert != NULL )
4041 {
4042 TEST_ASSERT( original.peer_cert->raw.len ==
4043 restored.peer_cert->raw.len );
4044 TEST_ASSERT( memcmp( original.peer_cert->raw.p,
4045 restored.peer_cert->raw.p,
4046 original.peer_cert->raw.len ) == 0 );
4047 }
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02004048#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4049 TEST_ASSERT( original.peer_cert_digest_type ==
4050 restored.peer_cert_digest_type );
4051 TEST_ASSERT( original.peer_cert_digest_len ==
4052 restored.peer_cert_digest_len );
4053 TEST_ASSERT( ( original.peer_cert_digest == NULL ) ==
4054 ( restored.peer_cert_digest == NULL ) );
4055 if( original.peer_cert_digest != NULL )
4056 {
4057 TEST_ASSERT( memcmp( original.peer_cert_digest,
4058 restored.peer_cert_digest,
4059 original.peer_cert_digest_len ) == 0 );
4060 }
4061#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4062#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02004063 TEST_ASSERT( original.verify_result == restored.verify_result );
4064
4065#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
4066 TEST_ASSERT( original.ticket_len == restored.ticket_len );
4067 if( original.ticket_len != 0 )
4068 {
4069 TEST_ASSERT( original.ticket != NULL );
4070 TEST_ASSERT( restored.ticket != NULL );
4071 TEST_ASSERT( memcmp( original.ticket,
4072 restored.ticket, original.ticket_len ) == 0 );
4073 }
4074 TEST_ASSERT( original.ticket_lifetime == restored.ticket_lifetime );
4075#endif
4076
4077#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
4078 TEST_ASSERT( original.mfl_code == restored.mfl_code );
4079#endif
4080
4081#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
4082 TEST_ASSERT( original.trunc_hmac == restored.trunc_hmac );
4083#endif
4084
4085#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
4086 TEST_ASSERT( original.encrypt_then_mac == restored.encrypt_then_mac );
4087#endif
4088
4089exit:
4090 mbedtls_ssl_session_free( &original );
4091 mbedtls_ssl_session_free( &restored );
4092 mbedtls_free( buf );
4093}
4094/* END_CASE */
4095
Manuel Pégourié-Gonnardaa755832019-06-03 10:53:47 +02004096/* BEGIN_CASE */
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02004097void ssl_serialize_session_load_save( int ticket_len, char *crt_file )
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02004098{
4099 mbedtls_ssl_session session;
4100 unsigned char *buf1 = NULL, *buf2 = NULL;
4101 size_t len0, len1, len2;
4102
4103 /*
4104 * Test that a load-save pair is the identity
4105 */
4106
4107 mbedtls_ssl_session_init( &session );
4108
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02004109 /* Prepare a dummy session to work on */
Manuel Pégourié-Gonnard6b840702019-05-24 09:40:17 +02004110 TEST_ASSERT( ssl_populate_session( &session, ticket_len, crt_file ) == 0 );
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02004111
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02004112 /* Get desired buffer size for serializing */
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02004113 TEST_ASSERT( mbedtls_ssl_session_save( &session, NULL, 0, &len0 )
4114 == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4115
4116 /* Allocate first buffer */
4117 buf1 = mbedtls_calloc( 1, len0 );
4118 TEST_ASSERT( buf1 != NULL );
4119
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02004120 /* Serialize to buffer and free live session */
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02004121 TEST_ASSERT( mbedtls_ssl_session_save( &session, buf1, len0, &len1 )
4122 == 0 );
4123 TEST_ASSERT( len0 == len1 );
4124 mbedtls_ssl_session_free( &session );
4125
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02004126 /* Restore session from serialized data */
Manuel Pégourié-Gonnard220403b2019-05-24 09:54:21 +02004127 TEST_ASSERT( mbedtls_ssl_session_load( &session, buf1, len1 ) == 0 );
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02004128
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02004129 /* Allocate second buffer and serialize to it */
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02004130 buf2 = mbedtls_calloc( 1, len0 );
Manuel Pégourié-Gonnardb4079902019-05-24 09:52:10 +02004131 TEST_ASSERT( buf2 != NULL );
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02004132 TEST_ASSERT( mbedtls_ssl_session_save( &session, buf2, len0, &len2 )
4133 == 0 );
4134
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02004135 /* Make sure both serialized versions are identical */
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02004136 TEST_ASSERT( len1 == len2 );
4137 TEST_ASSERT( memcmp( buf1, buf2, len1 ) == 0 );
4138
4139exit:
4140 mbedtls_ssl_session_free( &session );
4141 mbedtls_free( buf1 );
4142 mbedtls_free( buf2 );
4143}
4144/* END_CASE */
Manuel Pégourié-Gonnardf5fa0aa2019-05-23 10:38:11 +02004145
4146/* BEGIN_CASE */
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02004147void ssl_serialize_session_save_buf_size( int ticket_len, char *crt_file )
Manuel Pégourié-Gonnardf5fa0aa2019-05-23 10:38:11 +02004148{
4149 mbedtls_ssl_session session;
4150 unsigned char *buf = NULL;
4151 size_t good_len, bad_len, test_len;
4152
4153 /*
4154 * Test that session_save() fails cleanly on small buffers
4155 */
4156
4157 mbedtls_ssl_session_init( &session );
4158
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02004159 /* Prepare dummy session and get serialized size */
Manuel Pégourié-Gonnard6b840702019-05-24 09:40:17 +02004160 TEST_ASSERT( ssl_populate_session( &session, ticket_len, crt_file ) == 0 );
Manuel Pégourié-Gonnardf5fa0aa2019-05-23 10:38:11 +02004161 TEST_ASSERT( mbedtls_ssl_session_save( &session, NULL, 0, &good_len )
4162 == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4163
4164 /* Try all possible bad lengths */
4165 for( bad_len = 1; bad_len < good_len; bad_len++ )
4166 {
4167 /* Allocate exact size so that asan/valgrind can detect any overwrite */
4168 mbedtls_free( buf );
4169 TEST_ASSERT( ( buf = mbedtls_calloc( 1, bad_len ) ) != NULL );
4170 TEST_ASSERT( mbedtls_ssl_session_save( &session, buf, bad_len,
4171 &test_len )
4172 == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4173 TEST_ASSERT( test_len == good_len );
4174 }
4175
4176exit:
4177 mbedtls_ssl_session_free( &session );
4178 mbedtls_free( buf );
4179}
4180/* END_CASE */
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02004181
4182/* BEGIN_CASE */
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02004183void ssl_serialize_session_load_buf_size( int ticket_len, char *crt_file )
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02004184{
4185 mbedtls_ssl_session session;
4186 unsigned char *good_buf = NULL, *bad_buf = NULL;
4187 size_t good_len, bad_len;
4188
4189 /*
4190 * Test that session_load() fails cleanly on small buffers
4191 */
4192
4193 mbedtls_ssl_session_init( &session );
4194
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02004195 /* Prepare serialized session data */
Manuel Pégourié-Gonnard6b840702019-05-24 09:40:17 +02004196 TEST_ASSERT( ssl_populate_session( &session, ticket_len, crt_file ) == 0 );
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02004197 TEST_ASSERT( mbedtls_ssl_session_save( &session, NULL, 0, &good_len )
4198 == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4199 TEST_ASSERT( ( good_buf = mbedtls_calloc( 1, good_len ) ) != NULL );
4200 TEST_ASSERT( mbedtls_ssl_session_save( &session, good_buf, good_len,
4201 &good_len ) == 0 );
4202 mbedtls_ssl_session_free( &session );
4203
4204 /* Try all possible bad lengths */
4205 for( bad_len = 0; bad_len < good_len; bad_len++ )
4206 {
4207 /* Allocate exact size so that asan/valgrind can detect any overread */
4208 mbedtls_free( bad_buf );
4209 bad_buf = mbedtls_calloc( 1, bad_len ? bad_len : 1 );
4210 TEST_ASSERT( bad_buf != NULL );
4211 memcpy( bad_buf, good_buf, bad_len );
4212
4213 TEST_ASSERT( mbedtls_ssl_session_load( &session, bad_buf, bad_len )
4214 == MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4215 }
4216
4217exit:
4218 mbedtls_ssl_session_free( &session );
4219 mbedtls_free( good_buf );
4220 mbedtls_free( bad_buf );
4221}
4222/* END_CASE */
Hanno Becker861d0bb2019-05-21 16:39:30 +01004223
Hanno Becker363b6462019-05-29 12:44:28 +01004224/* BEGIN_CASE */
4225void ssl_session_serialize_version_check( int corrupt_major,
Hanno Becker861d0bb2019-05-21 16:39:30 +01004226 int corrupt_minor,
4227 int corrupt_patch,
4228 int corrupt_config )
4229{
Hanno Becker363b6462019-05-29 12:44:28 +01004230 unsigned char serialized_session[ 2048 ];
4231 size_t serialized_session_len;
Hanno Beckerfe1275e2019-05-29 12:45:21 +01004232 unsigned cur_byte;
Hanno Becker861d0bb2019-05-21 16:39:30 +01004233 mbedtls_ssl_session session;
Hanno Beckerfe1275e2019-05-29 12:45:21 +01004234 uint8_t should_corrupt_byte[] = { corrupt_major == 1,
4235 corrupt_minor == 1,
4236 corrupt_patch == 1,
4237 corrupt_config == 1,
4238 corrupt_config == 1 };
4239
Hanno Becker861d0bb2019-05-21 16:39:30 +01004240 mbedtls_ssl_session_init( &session );
4241
Hanno Beckerfe1275e2019-05-29 12:45:21 +01004242 /* Infer length of serialized session. */
Hanno Becker861d0bb2019-05-21 16:39:30 +01004243 TEST_ASSERT( mbedtls_ssl_session_save( &session,
Hanno Becker363b6462019-05-29 12:44:28 +01004244 serialized_session,
4245 sizeof( serialized_session ),
4246 &serialized_session_len ) == 0 );
Hanno Becker861d0bb2019-05-21 16:39:30 +01004247
Hanno Beckerfe1275e2019-05-29 12:45:21 +01004248 mbedtls_ssl_session_free( &session );
Hanno Becker861d0bb2019-05-21 16:39:30 +01004249
Hanno Beckerfe1275e2019-05-29 12:45:21 +01004250 /* Without any modification, we should be able to successfully
Hanno Becker363b6462019-05-29 12:44:28 +01004251 * de-serialize the session - double-check that. */
Hanno Becker861d0bb2019-05-21 16:39:30 +01004252 TEST_ASSERT( mbedtls_ssl_session_load( &session,
Hanno Becker363b6462019-05-29 12:44:28 +01004253 serialized_session,
4254 serialized_session_len ) == 0 );
Hanno Becker861d0bb2019-05-21 16:39:30 +01004255 mbedtls_ssl_session_free( &session );
4256
Hanno Beckerfe1275e2019-05-29 12:45:21 +01004257 /* Go through the bytes in the serialized session header and
4258 * corrupt them bit-by-bit. */
4259 for( cur_byte = 0; cur_byte < sizeof( should_corrupt_byte ); cur_byte++ )
Hanno Becker861d0bb2019-05-21 16:39:30 +01004260 {
Hanno Beckerfe1275e2019-05-29 12:45:21 +01004261 int cur_bit;
4262 unsigned char * const byte = &serialized_session[ cur_byte ];
4263
4264 if( should_corrupt_byte[ cur_byte ] == 0 )
4265 continue;
4266
4267 for( cur_bit = 0; cur_bit < CHAR_BIT; cur_bit++ )
4268 {
4269 unsigned char const corrupted_bit = 0x1u << cur_bit;
4270 /* Modify a single bit in the serialized session. */
4271 *byte ^= corrupted_bit;
4272
4273 /* Attempt to deserialize */
4274 TEST_ASSERT( mbedtls_ssl_session_load( &session,
4275 serialized_session,
4276 serialized_session_len ) ==
Hanno Beckerf9b33032019-06-03 12:58:39 +01004277 MBEDTLS_ERR_SSL_VERSION_MISMATCH );
Hanno Beckerfe1275e2019-05-29 12:45:21 +01004278
4279 /* Undo the change */
4280 *byte ^= corrupted_bit;
4281 }
Hanno Becker861d0bb2019-05-21 16:39:30 +01004282 }
4283
Hanno Becker861d0bb2019-05-21 16:39:30 +01004284}
4285/* END_CASE */
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01004286
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02004287/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_ENTROPY_C:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01004288void mbedtls_endpoint_sanity( int endpoint_type )
4289{
4290 enum { BUFFSIZE = 1024 };
4291 mbedtls_endpoint ep;
4292 int ret = -1;
4293
Andrzej Kurek15daf502020-02-12 09:17:52 -05004294 ret = mbedtls_endpoint_init( NULL, endpoint_type, MBEDTLS_PK_RSA,
4295 NULL, NULL, NULL );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01004296 TEST_ASSERT( MBEDTLS_ERR_SSL_BAD_INPUT_DATA == ret );
4297
Andrzej Kurekb2980742020-02-02 19:25:26 -05004298 ret = mbedtls_endpoint_certificate_init( NULL, MBEDTLS_PK_RSA );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01004299 TEST_ASSERT( MBEDTLS_ERR_SSL_BAD_INPUT_DATA == ret );
4300
Andrzej Kurek15daf502020-02-12 09:17:52 -05004301 ret = mbedtls_endpoint_init( &ep, endpoint_type, MBEDTLS_PK_RSA,
4302 NULL, NULL, NULL );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01004303 TEST_ASSERT( ret == 0 );
4304
4305exit:
Andrzej Kurek15daf502020-02-12 09:17:52 -05004306 mbedtls_endpoint_free( &ep, NULL );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01004307}
4308/* END_CASE */
4309
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02004310/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_ENTROPY_C:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01004311void move_handshake_to_state(int endpoint_type, int state, int need_pass)
4312{
4313 enum { BUFFSIZE = 1024 };
4314 mbedtls_endpoint base_ep, second_ep;
4315 int ret = -1;
4316
Andrzej Kurek15daf502020-02-12 09:17:52 -05004317 ret = mbedtls_endpoint_init( &base_ep, endpoint_type, MBEDTLS_PK_RSA,
4318 NULL, NULL, NULL );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01004319 TEST_ASSERT( ret == 0 );
4320
4321 ret = mbedtls_endpoint_init( &second_ep,
4322 ( endpoint_type == MBEDTLS_SSL_IS_SERVER ) ?
Andrzej Kurekb2980742020-02-02 19:25:26 -05004323 MBEDTLS_SSL_IS_CLIENT : MBEDTLS_SSL_IS_SERVER,
Andrzej Kurek15daf502020-02-12 09:17:52 -05004324 MBEDTLS_PK_RSA, NULL, NULL, NULL );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01004325 TEST_ASSERT( ret == 0 );
4326
4327 ret = mbedtls_mock_socket_connect( &(base_ep.socket),
4328 &(second_ep.socket),
4329 BUFFSIZE );
4330 TEST_ASSERT( ret == 0 );
4331
4332 ret = mbedtls_move_handshake_to_state( &(base_ep.ssl),
4333 &(second_ep.ssl),
4334 state );
4335 if( need_pass )
4336 {
4337 TEST_ASSERT( ret == 0 );
4338 TEST_ASSERT( base_ep.ssl.state == state );
4339 }
4340 else
4341 {
4342 TEST_ASSERT( ret != 0 );
4343 TEST_ASSERT( base_ep.ssl.state != state );
4344 }
4345
4346exit:
Andrzej Kurek15daf502020-02-12 09:17:52 -05004347 mbedtls_endpoint_free( &base_ep, NULL );
4348 mbedtls_endpoint_free( &second_ep, NULL );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01004349}
4350/* END_CASE */
Andrzej Kurekf40daa32020-02-04 09:00:01 -05004351
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02004352/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
Paul Elliottc8570442020-04-15 17:00:50 +01004353void handshake_version( int dtls, int client_min_version, int client_max_version,
4354 int server_min_version, int server_max_version,
4355 int expected_negotiated_version )
Andrzej Kurekf40daa32020-02-04 09:00:01 -05004356{
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004357 handshake_test_options options;
4358 init_handshake_options( &options );
Andrzej Kurekda2b6782020-02-12 07:56:36 -05004359
Paul Elliottc8570442020-04-15 17:00:50 +01004360 options.client_min_version = client_min_version;
4361 options.client_max_version = client_max_version;
4362 options.server_min_version = server_min_version;
4363 options.server_max_version = server_max_version;
4364
4365 options.expected_negotiated_version = expected_negotiated_version;
4366
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004367 options.dtls = dtls;
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01004368 /* By default, TLSv1.0 use 1/n-1 splitting when sending data, so
Piotr Nowicki438bf3b2020-03-10 12:59:10 +01004369 * the number of fragments will be twice as big. */
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01004370 if( expected_negotiated_version == MBEDTLS_SSL_MINOR_VERSION_1 )
Andrzej Kurek941962e2020-02-07 09:20:32 -05004371 {
Piotr Nowicki438bf3b2020-03-10 12:59:10 +01004372 options.expected_cli_fragments = 2;
4373 options.expected_srv_fragments = 2;
Andrzej Kurek941962e2020-02-07 09:20:32 -05004374 }
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004375 perform_handshake( &options );
Andrzej Kurekf40daa32020-02-04 09:00:01 -05004376
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004377 /* The goto below is used to avoid an "unused label" warning.*/
4378 goto exit;
4379}
4380/* END_CASE */
Andrzej Kurek9e9efdc2020-02-26 05:25:23 -05004381
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02004382/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004383void handshake_psk_cipher( char* cipher, int pk_alg, data_t *psk_str, int dtls )
4384{
4385 handshake_test_options options;
4386 init_handshake_options( &options );
Andrzej Kurekf40daa32020-02-04 09:00:01 -05004387
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004388 options.cipher = cipher;
4389 options.dtls = dtls;
4390 options.psk_str = psk_str;
4391 options.pk_alg = pk_alg;
Andrzej Kurekcc5169c2020-02-04 09:04:56 -05004392
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004393 perform_handshake( &options );
Andrzej Kurek316da1f2020-02-26 09:03:47 -05004394
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004395 /* The goto below is used to avoid an "unused label" warning.*/
4396 goto exit;
4397}
4398/* END_CASE */
Andrzej Kurek316da1f2020-02-26 09:03:47 -05004399
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02004400/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004401void handshake_cipher( char* cipher, int pk_alg, int dtls )
4402{
4403 test_handshake_psk_cipher( cipher, pk_alg, NULL, dtls );
Andrzej Kurekf40daa32020-02-04 09:00:01 -05004404
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004405 /* The goto below is used to avoid an "unused label" warning.*/
4406 goto exit;
4407}
4408/* END_CASE */
Andrzej Kurekf40daa32020-02-04 09:00:01 -05004409
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02004410/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004411void app_data( int mfl, int cli_msg_len, int srv_msg_len,
4412 int expected_cli_fragments,
4413 int expected_srv_fragments, int dtls )
4414{
4415 handshake_test_options options;
4416 init_handshake_options( &options );
Andrzej Kurekda2b6782020-02-12 07:56:36 -05004417
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004418 options.mfl = mfl;
4419 options.cli_msg_len = cli_msg_len;
4420 options.srv_msg_len = srv_msg_len;
4421 options.expected_cli_fragments = expected_cli_fragments;
4422 options.expected_srv_fragments = expected_srv_fragments;
4423 options.dtls = dtls;
Andrzej Kurekda2b6782020-02-12 07:56:36 -05004424
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004425 perform_handshake( &options );
4426 /* The goto below is used to avoid an "unused label" warning.*/
4427 goto exit;
4428}
4429/* END_CASE */
Andrzej Kurekda2b6782020-02-12 07:56:36 -05004430
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02004431/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004432void app_data_tls( int mfl, int cli_msg_len, int srv_msg_len,
4433 int expected_cli_fragments,
4434 int expected_srv_fragments )
4435{
4436 test_app_data( mfl, cli_msg_len, srv_msg_len, expected_cli_fragments,
4437 expected_srv_fragments, 0 );
4438 /* The goto below is used to avoid an "unused label" warning.*/
4439 goto exit;
4440}
4441/* END_CASE */
Andrzej Kurekda2b6782020-02-12 07:56:36 -05004442
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02004443/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_SSL_PROTO_DTLS:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004444void app_data_dtls( int mfl, int cli_msg_len, int srv_msg_len,
4445 int expected_cli_fragments,
4446 int expected_srv_fragments )
4447{
4448 test_app_data( mfl, cli_msg_len, srv_msg_len, expected_cli_fragments,
4449 expected_srv_fragments, 1 );
4450 /* The goto below is used to avoid an "unused label" warning.*/
4451 goto exit;
4452}
4453/* END_CASE */
Andrzej Kurekda2b6782020-02-12 07:56:36 -05004454
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02004455/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_SSL_PROTO_DTLS:MBEDTLS_SSL_RENEGOTIATION:MBEDTLS_SSL_CONTEXT_SERIALIZATION:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004456void handshake_serialization( )
4457{
4458 handshake_test_options options;
4459 init_handshake_options( &options );
Andrzej Kurekda2b6782020-02-12 07:56:36 -05004460
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004461 options.serialize = 1;
4462 options.dtls = 1;
4463 perform_handshake( &options );
4464 /* The goto below is used to avoid an "unused label" warning.*/
4465 goto exit;
4466}
4467/* END_CASE */
Andrzej Kurekda2b6782020-02-12 07:56:36 -05004468
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02004469/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_DEBUG_C:MBEDTLS_SSL_MAX_FRAGMENT_LENGTH:MBEDTLS_CIPHER_MODE_CBC:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
Piotr Nowickibde7ee82020-02-21 10:59:50 +01004470void handshake_fragmentation( int mfl, int expected_srv_hs_fragmentation, int expected_cli_hs_fragmentation)
4471{
4472 handshake_test_options options;
4473 log_pattern srv_pattern, cli_pattern;
4474
4475 srv_pattern.pattern = cli_pattern.pattern = "found fragmented DTLS handshake";
4476 srv_pattern.counter = 0;
4477 cli_pattern.counter = 0;
4478
4479 init_handshake_options( &options );
4480 options.dtls = 1;
4481 options.mfl = mfl;
Darryl Greenaad82f92019-12-02 10:53:11 +00004482 /* Set cipher to one using CBC so that record splitting can be tested */
4483 options.cipher = "TLS-DHE-RSA-WITH-AES-256-CBC-SHA256";
Piotr Nowickibde7ee82020-02-21 10:59:50 +01004484 options.srv_auth_mode = MBEDTLS_SSL_VERIFY_REQUIRED;
4485 options.srv_log_obj = &srv_pattern;
4486 options.cli_log_obj = &cli_pattern;
4487 options.srv_log_fun = log_analyzer;
4488 options.cli_log_fun = log_analyzer;
4489
4490 perform_handshake( &options );
4491
4492 /* Test if the server received a fragmented handshake */
4493 if( expected_srv_hs_fragmentation )
4494 {
4495 TEST_ASSERT( srv_pattern.counter >= 1 );
4496 }
4497 /* Test if the client received a fragmented handshake */
4498 if( expected_cli_hs_fragmentation )
4499 {
4500 TEST_ASSERT( cli_pattern.counter >= 1 );
4501 }
4502}
4503/* END_CASE */
4504
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02004505/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_SSL_PROTO_DTLS:MBEDTLS_SSL_RENEGOTIATION:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004506void renegotiation( int legacy_renegotiation )
4507{
4508 handshake_test_options options;
4509 init_handshake_options( &options );
Andrzej Kurekda2b6782020-02-12 07:56:36 -05004510
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004511 options.renegotiate = 1;
4512 options.legacy_renegotiation = legacy_renegotiation;
4513 options.dtls = 1;
Andrzej Kurek316da1f2020-02-26 09:03:47 -05004514
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004515 perform_handshake( &options );
4516 /* The goto below is used to avoid an "unused label" warning.*/
4517 goto exit;
Andrzej Kurekf40daa32020-02-04 09:00:01 -05004518}
4519/* END_CASE */
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004520
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02004521/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004522void resize_buffers( int mfl, int renegotiation, int legacy_renegotiation,
Andrzej Kurek8ea68722020-04-03 06:40:47 -04004523 int serialize, int dtls, char *cipher )
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004524{
4525 handshake_test_options options;
4526 init_handshake_options( &options );
4527
4528 options.mfl = mfl;
Andrzej Kurek8ea68722020-04-03 06:40:47 -04004529 options.cipher = cipher;
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004530 options.renegotiate = renegotiation;
4531 options.legacy_renegotiation = legacy_renegotiation;
4532 options.serialize = serialize;
4533 options.dtls = dtls;
4534 options.resize_buffers = 1;
4535
4536 perform_handshake( &options );
4537 /* The goto below is used to avoid an "unused label" warning.*/
4538 goto exit;
4539}
4540/* END_CASE */
4541
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02004542/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH:MBEDTLS_SSL_CONTEXT_SERIALIZATION:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_SSL_PROTO_DTLS:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004543void resize_buffers_serialize_mfl( int mfl )
4544{
Andrzej Kurek8ea68722020-04-03 06:40:47 -04004545 test_resize_buffers( mfl, 0, MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION, 1, 1,
4546 (char *) "" );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004547
4548 /* The goto below is used to avoid an "unused label" warning.*/
4549 goto exit;
4550}
4551/* END_CASE */
4552
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02004553/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH:MBEDTLS_SSL_RENEGOTIATION:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
Andrzej Kurek8ea68722020-04-03 06:40:47 -04004554void resize_buffers_renegotiate_mfl( int mfl, int legacy_renegotiation,
4555 char *cipher )
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004556{
Andrzej Kurek8ea68722020-04-03 06:40:47 -04004557 test_resize_buffers( mfl, 1, legacy_renegotiation, 0, 1, cipher );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004558
4559 /* The goto below is used to avoid an "unused label" warning.*/
4560 goto exit;
4561}
4562/* END_CASE */
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02004563
Manuel Pégourié-Gonnarded0e8642020-07-21 11:20:30 +02004564/* BEGIN_CASE depends_on:MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC:MBEDTLS_TEST_HOOKS */
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02004565void ssl_cf_hmac( int hash )
4566{
4567 /*
4568 * Test the function mbedtls_ssl_cf_hmac() against a reference
4569 * implementation.
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02004570 */
4571 mbedtls_md_context_t ctx, ref_ctx;
4572 const mbedtls_md_info_t *md_info;
4573 size_t out_len, block_size;
4574 size_t min_in_len, in_len, max_in_len, i;
4575 /* TLS additional data is 13 bytes (hence the "lucky 13" name) */
4576 unsigned char add_data[13];
4577 unsigned char ref_out[MBEDTLS_MD_MAX_SIZE];
4578 unsigned char *data = NULL;
4579 unsigned char *out = NULL;
4580 unsigned char rec_num = 0;
4581
4582 mbedtls_md_init( &ctx );
4583 mbedtls_md_init( &ref_ctx );
4584
4585 md_info = mbedtls_md_info_from_type( hash );
4586 TEST_ASSERT( md_info != NULL );
4587 out_len = mbedtls_md_get_size( md_info );
4588 TEST_ASSERT( out_len != 0 );
4589 block_size = hash == MBEDTLS_MD_SHA384 ? 128 : 64;
4590
4591 /* Use allocated out buffer to catch overwrites */
4592 ASSERT_ALLOC( out, out_len );
4593
4594 /* Set up contexts with the given hash and a dummy key */
4595 TEST_EQUAL( 0, mbedtls_md_setup( &ctx, md_info, 1 ) );
4596 TEST_EQUAL( 0, mbedtls_md_setup( &ref_ctx, md_info, 1 ) );
4597 memset( ref_out, 42, sizeof( ref_out ) );
4598 TEST_EQUAL( 0, mbedtls_md_hmac_starts( &ctx, ref_out, out_len ) );
4599 TEST_EQUAL( 0, mbedtls_md_hmac_starts( &ref_ctx, ref_out, out_len ) );
4600 memset( ref_out, 0, sizeof( ref_out ) );
4601
4602 /*
4603 * Test all possible lengths up to a point. The difference between
4604 * max_in_len and min_in_len is at most 255, and make sure they both vary
4605 * by at least one block size.
4606 */
4607 for( max_in_len = 0; max_in_len <= 255 + block_size; max_in_len++ )
4608 {
Chris Jones9634bb12021-01-20 15:56:42 +00004609 mbedtls_test_set_step( max_in_len * 10000 );
Manuel Pégourié-Gonnardca8287c2020-07-22 10:29:39 +02004610
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02004611 /* Use allocated in buffer to catch overreads */
Manuel Pégourié-Gonnardc3219002020-07-22 10:32:52 +02004612 ASSERT_ALLOC( data, max_in_len );
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02004613
4614 min_in_len = max_in_len > 255 ? max_in_len - 255 : 0;
4615 for( in_len = min_in_len; in_len <= max_in_len; in_len++ )
4616 {
Chris Jones9634bb12021-01-20 15:56:42 +00004617 mbedtls_test_set_step( max_in_len * 10000 + in_len );
Manuel Pégourié-Gonnardca8287c2020-07-22 10:29:39 +02004618
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02004619 /* Set up dummy data and add_data */
4620 rec_num++;
4621 memset( add_data, rec_num, sizeof( add_data ) );
4622 for( i = 0; i < in_len; i++ )
4623 data[i] = ( i & 0xff ) ^ rec_num;
4624
4625 /* Get the function's result */
Manuel Pégourié-Gonnard9670a592020-07-10 10:21:46 +02004626 TEST_CF_SECRET( &in_len, sizeof( in_len ) );
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02004627 TEST_EQUAL( 0, mbedtls_ssl_cf_hmac( &ctx, add_data, sizeof( add_data ),
4628 data, in_len,
4629 min_in_len, max_in_len,
4630 out ) );
Manuel Pégourié-Gonnard9670a592020-07-10 10:21:46 +02004631 TEST_CF_PUBLIC( &in_len, sizeof( in_len ) );
4632 TEST_CF_PUBLIC( out, out_len );
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02004633
4634 /* Compute the reference result */
4635 TEST_EQUAL( 0, mbedtls_md_hmac_update( &ref_ctx, add_data,
4636 sizeof( add_data ) ) );
4637 TEST_EQUAL( 0, mbedtls_md_hmac_update( &ref_ctx, data, in_len ) );
4638 TEST_EQUAL( 0, mbedtls_md_hmac_finish( &ref_ctx, ref_out ) );
4639 TEST_EQUAL( 0, mbedtls_md_hmac_reset( &ref_ctx ) );
4640
4641 /* Compare */
4642 ASSERT_COMPARE( out, out_len, ref_out, out_len );
4643 }
4644
4645 mbedtls_free( data );
4646 data = NULL;
4647 }
4648
4649exit:
4650 mbedtls_md_free( &ref_ctx );
4651 mbedtls_md_free( &ctx );
4652
4653 mbedtls_free( data );
4654 mbedtls_free( out );
4655}
4656/* END_CASE */
Manuel Pégourié-Gonnard7fe2c5f2020-08-18 12:02:54 +02004657
4658/* BEGIN_CASE depends_on:MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC:MBEDTLS_TEST_HOOKS */
4659void ssl_cf_memcpy_offset( int offset_min, int offset_max, int len )
4660{
4661 unsigned char *dst = NULL;
4662 unsigned char *src = NULL;
4663 size_t src_len = offset_max + len;
4664 size_t secret;
4665
4666 ASSERT_ALLOC( dst, len );
4667 ASSERT_ALLOC( src, src_len );
4668
4669 /* Fill src in a way that we can detect if we copied the right bytes */
4670 mbedtls_test_rnd_std_rand( NULL, src, src_len );
4671
4672 for( secret = offset_min; secret <= (size_t) offset_max; secret++ )
4673 {
Chris Jones9634bb12021-01-20 15:56:42 +00004674 mbedtls_test_set_step( (int) secret );
Manuel Pégourié-Gonnard7fe2c5f2020-08-18 12:02:54 +02004675
4676 TEST_CF_SECRET( &secret, sizeof( secret ) );
4677 mbedtls_ssl_cf_memcpy_offset( dst, src, secret,
4678 offset_min, offset_max, len );
4679 TEST_CF_PUBLIC( &secret, sizeof( secret ) );
4680 TEST_CF_PUBLIC( dst, len );
4681
4682 ASSERT_COMPARE( dst, len, src + secret, len );
4683 }
4684
4685exit:
4686 mbedtls_free( dst );
4687 mbedtls_free( src );
4688}
4689/* END_CASE */
Hanno Becker6667ffd2021-04-19 21:59:22 +01004690
4691/* BEGIN_CASE depends_on:MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
4692void test_multiple_psks()
4693{
4694 unsigned char psk0[10] = { 0 };
4695 unsigned char psk0_identity[] = { 'f', 'o', 'o' };
4696
4697 unsigned char psk1[10] = { 0 };
4698 unsigned char psk1_identity[] = { 'b', 'a', 'r' };
4699
4700 mbedtls_ssl_config conf;
4701
4702 mbedtls_ssl_config_init( &conf );
4703
4704 TEST_ASSERT( mbedtls_ssl_conf_psk( &conf,
4705 psk0, sizeof( psk0 ),
4706 psk0_identity, sizeof( psk0_identity ) ) == 0 );
4707 TEST_ASSERT( mbedtls_ssl_conf_psk( &conf,
4708 psk1, sizeof( psk1 ),
4709 psk1_identity, sizeof( psk1_identity ) ) ==
4710 MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4711
4712exit:
4713
4714 mbedtls_ssl_config_free( &conf );
4715}
4716/* END_CASE */
4717
4718/* BEGIN_CASE depends_on:MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED:MBEDTLS_USE_PSA_CRYPTO */
4719void test_multiple_psks_opaque( int mode )
4720{
4721 /*
4722 * Mode 0: Raw PSK, then opaque PSK
4723 * Mode 1: Opaque PSK, then raw PSK
4724 * Mode 2: 2x opaque PSK
4725 */
4726
4727 unsigned char psk0_raw[10] = { 0 };
4728 unsigned char psk0_raw_identity[] = { 'f', 'o', 'o' };
4729
4730 psa_key_id_t psk0_opaque = (psa_key_id_t) 1;
4731 unsigned char psk0_opaque_identity[] = { 'f', 'o', 'o' };
4732
4733 unsigned char psk1_raw[10] = { 0 };
4734 unsigned char psk1_raw_identity[] = { 'b', 'a', 'r' };
4735
4736 psa_key_id_t psk1_opaque = (psa_key_id_t) 2;
4737 unsigned char psk1_opaque_identity[] = { 'b', 'a', 'r' };
4738
4739 mbedtls_ssl_config conf;
4740
4741 USE_PSA_INIT( );
4742 mbedtls_ssl_config_init( &conf );
4743
4744 switch( mode )
4745 {
4746 case 0:
4747
4748 TEST_ASSERT( mbedtls_ssl_conf_psk( &conf,
4749 psk0_raw, sizeof( psk0_raw ),
4750 psk0_raw_identity, sizeof( psk0_raw_identity ) )
4751 == 0 );
4752 TEST_ASSERT( mbedtls_ssl_conf_psk_opaque( &conf,
4753 psk1_opaque,
4754 psk1_opaque_identity, sizeof( psk1_opaque_identity ) )
4755 == MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4756 break;
4757
4758 case 1:
4759
4760 TEST_ASSERT( mbedtls_ssl_conf_psk_opaque( &conf,
4761 psk0_opaque,
4762 psk0_opaque_identity, sizeof( psk0_opaque_identity ) )
4763 == 0 );
4764 TEST_ASSERT( mbedtls_ssl_conf_psk( &conf,
4765 psk1_raw, sizeof( psk1_raw ),
4766 psk1_raw_identity, sizeof( psk1_raw_identity ) )
4767 == MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4768
4769 break;
4770
4771 case 2:
4772
4773 TEST_ASSERT( mbedtls_ssl_conf_psk_opaque( &conf,
4774 psk0_opaque,
4775 psk0_opaque_identity, sizeof( psk0_opaque_identity ) )
4776 == 0 );
4777 TEST_ASSERT( mbedtls_ssl_conf_psk_opaque( &conf,
4778 psk1_opaque,
4779 psk1_opaque_identity, sizeof( psk1_opaque_identity ) )
4780 == MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4781
4782 break;
4783
4784 default:
4785 TEST_ASSERT( 0 );
4786 break;
4787 }
4788
4789exit:
4790
4791 mbedtls_ssl_config_free( &conf );
4792 USE_PSA_DONE( );
4793
4794}
4795/* END_CASE */