blob: 90cc4ad0cef8da042fb3a77064c628b377e07e12 [file] [log] [blame]
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02001/* BEGIN_HEADER */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002#include <mbedtls/ssl.h>
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +02003#include <mbedtls/ssl_internal.h>
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01004#include <mbedtls/ctr_drbg.h>
5#include <mbedtls/entropy.h>
6#include <mbedtls/certs.h>
Andrzej Kurek941962e2020-02-07 09:20:32 -05007#include <mbedtls/timing.h>
Piotr Nowickibde7ee82020-02-21 10:59:50 +01008#include <mbedtls/debug.h>
Hanno Becker73c825a2020-09-08 10:52:58 +01009#include <ssl_tls13_keys.h>
Piotr Nowickibde7ee82020-02-21 10:59:50 +010010
Gabor Mezeic0ae1cf2021-10-20 12:09:35 +020011#include <constant_time_internal.h>
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +020012
Manuel Pégourié-Gonnard9670a592020-07-10 10:21:46 +020013#include <test/constant_flow.h>
14
Hanno Becker1413bd82020-09-09 12:46:09 +010015enum
16{
17#define MBEDTLS_SSL_TLS1_3_LABEL( name, string ) \
18 tls1_3_label_ ## name,
Hanno Becker70d7fb02020-09-09 10:11:21 +010019MBEDTLS_SSL_TLS1_3_LABEL_LIST
20#undef MBEDTLS_SSL_TLS1_3_LABEL
Hanno Becker1413bd82020-09-09 12:46:09 +010021};
Hanno Becker70d7fb02020-09-09 10:11:21 +010022
Piotr Nowickibde7ee82020-02-21 10:59:50 +010023typedef struct log_pattern
24{
25 const char *pattern;
26 size_t counter;
27} log_pattern;
28
Piotr Nowicki438bf3b2020-03-10 12:59:10 +010029/*
30 * This function can be passed to mbedtls to receive output logs from it. In
Piotr Nowickibde7ee82020-02-21 10:59:50 +010031 * this case, it will count the instances of a log_pattern in the received
32 * logged messages.
33 */
34void log_analyzer( void *ctx, int level,
35 const char *file, int line,
36 const char *str )
37{
38 log_pattern *p = (log_pattern *) ctx;
39
40 (void) level;
41 (void) line;
42 (void) file;
43
44 if( NULL != p &&
45 NULL != p->pattern &&
46 NULL != strstr( str, p->pattern ) )
47 {
48 p->counter++;
49 }
50}
Janos Follath6264e662019-11-26 11:11:15 +000051
Paul Elliottc8570442020-04-15 17:00:50 +010052/* Invalid minor version used when not specifying a min/max version or expecting a test to fail */
53#define TEST_SSL_MINOR_VERSION_NONE -1
54
Andrzej Kurek8a6ff152020-02-26 09:10:14 -050055typedef struct handshake_test_options
56{
57 const char *cipher;
Paul Elliottc8570442020-04-15 17:00:50 +010058 int client_min_version;
59 int client_max_version;
60 int server_min_version;
61 int server_max_version;
62 int expected_negotiated_version;
Andrzej Kurek8a6ff152020-02-26 09:10:14 -050063 int pk_alg;
64 data_t *psk_str;
65 int dtls;
Piotr Nowickibde7ee82020-02-21 10:59:50 +010066 int srv_auth_mode;
Andrzej Kurek8a6ff152020-02-26 09:10:14 -050067 int serialize;
68 int mfl;
69 int cli_msg_len;
70 int srv_msg_len;
71 int expected_cli_fragments;
72 int expected_srv_fragments;
73 int renegotiate;
74 int legacy_renegotiation;
Piotr Nowickibde7ee82020-02-21 10:59:50 +010075 void *srv_log_obj;
76 void *cli_log_obj;
77 void (*srv_log_fun)(void *, int, const char *, int, const char *);
78 void (*cli_log_fun)(void *, int, const char *, int, const char *);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -050079 int resize_buffers;
Andrzej Kurek8a6ff152020-02-26 09:10:14 -050080} handshake_test_options;
81
82void init_handshake_options( handshake_test_options *opts )
83{
84 opts->cipher = "";
Paul Elliottc8570442020-04-15 17:00:50 +010085 opts->client_min_version = TEST_SSL_MINOR_VERSION_NONE;
86 opts->client_max_version = TEST_SSL_MINOR_VERSION_NONE;
87 opts->server_min_version = TEST_SSL_MINOR_VERSION_NONE;
88 opts->server_max_version = TEST_SSL_MINOR_VERSION_NONE;
89 opts->expected_negotiated_version = MBEDTLS_SSL_MINOR_VERSION_3;
Andrzej Kurek8a6ff152020-02-26 09:10:14 -050090 opts->pk_alg = MBEDTLS_PK_RSA;
91 opts->psk_str = NULL;
92 opts->dtls = 0;
Piotr Nowickibde7ee82020-02-21 10:59:50 +010093 opts->srv_auth_mode = MBEDTLS_SSL_VERIFY_NONE;
Andrzej Kurek8a6ff152020-02-26 09:10:14 -050094 opts->serialize = 0;
95 opts->mfl = MBEDTLS_SSL_MAX_FRAG_LEN_NONE;
96 opts->cli_msg_len = 100;
97 opts->srv_msg_len = 100;
98 opts->expected_cli_fragments = 1;
99 opts->expected_srv_fragments = 1;
100 opts->renegotiate = 0;
101 opts->legacy_renegotiation = MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION;
Piotr Nowickibde7ee82020-02-21 10:59:50 +0100102 opts->srv_log_obj = NULL;
103 opts->srv_log_obj = NULL;
104 opts->srv_log_fun = NULL;
105 opts->cli_log_fun = NULL;
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500106 opts->resize_buffers = 1;
Andrzej Kurek8a6ff152020-02-26 09:10:14 -0500107}
Janos Follath6264e662019-11-26 11:11:15 +0000108/*
109 * Buffer structure for custom I/O callbacks.
110 */
111
112typedef struct mbedtls_test_buffer
113{
114 size_t start;
115 size_t content_length;
116 size_t capacity;
117 unsigned char *buffer;
118} mbedtls_test_buffer;
119
120/*
121 * Initialises \p buf. After calling this function it is safe to call
122 * `mbedtls_test_buffer_free()` on \p buf.
123 */
124void mbedtls_test_buffer_init( mbedtls_test_buffer *buf )
125{
126 memset( buf, 0, sizeof( *buf ) );
127}
128
129/*
130 * Sets up \p buf. After calling this function it is safe to call
131 * `mbedtls_test_buffer_put()` and `mbedtls_test_buffer_get()` on \p buf.
132 */
133int mbedtls_test_buffer_setup( mbedtls_test_buffer *buf, size_t capacity )
134{
135 buf->buffer = (unsigned char*) mbedtls_calloc( capacity,
136 sizeof(unsigned char) );
137 if( NULL == buf->buffer )
138 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
139 buf->capacity = capacity;
140
141 return 0;
142}
143
144void mbedtls_test_buffer_free( mbedtls_test_buffer *buf )
145{
146 if( buf->buffer != NULL )
147 mbedtls_free( buf->buffer );
148
149 memset( buf, 0, sizeof( *buf ) );
150}
151
152/*
153 * Puts \p input_len bytes from the \p input buffer into the ring buffer \p buf.
154 *
155 * \p buf must have been initialized and set up by calling
156 * `mbedtls_test_buffer_init()` and `mbedtls_test_buffer_setup()`.
157 *
158 * \retval \p input_len, if the data fits.
159 * \retval 0 <= value < \p input_len, if the data does not fit.
160 * \retval -1, if \p buf is NULL, it hasn't been set up or \p input_len is not
161 * zero and \p input is NULL.
162 */
163int mbedtls_test_buffer_put( mbedtls_test_buffer *buf,
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100164 const unsigned char *input, size_t input_len )
Janos Follath6264e662019-11-26 11:11:15 +0000165{
166 size_t overflow = 0;
167
168 if( ( buf == NULL ) || ( buf->buffer == NULL ) )
169 return -1;
170
171 /* Reduce input_len to a number that fits in the buffer. */
172 if ( ( buf->content_length + input_len ) > buf->capacity )
173 {
174 input_len = buf->capacity - buf->content_length;
175 }
176
177 if( input == NULL )
178 {
179 return ( input_len == 0 ) ? 0 : -1;
180 }
181
Piotr Nowickifb437d72020-01-13 16:59:12 +0100182 /* Check if the buffer has not come full circle and free space is not in
183 * the middle */
184 if( buf->start + buf->content_length < buf->capacity )
Janos Follath6264e662019-11-26 11:11:15 +0000185 {
Piotr Nowickifb437d72020-01-13 16:59:12 +0100186
187 /* Calculate the number of bytes that need to be placed at lower memory
188 * address */
189 if( buf->start + buf->content_length + input_len
190 > buf->capacity )
191 {
192 overflow = ( buf->start + buf->content_length + input_len )
193 % buf->capacity;
194 }
195
196 memcpy( buf->buffer + buf->start + buf->content_length, input,
197 input_len - overflow );
198 memcpy( buf->buffer, input + input_len - overflow, overflow );
199
200 }
201 else
202 {
203 /* The buffer has come full circle and free space is in the middle */
204 memcpy( buf->buffer + buf->start + buf->content_length - buf->capacity,
205 input, input_len );
Janos Follath6264e662019-11-26 11:11:15 +0000206 }
207
Janos Follath6264e662019-11-26 11:11:15 +0000208 buf->content_length += input_len;
Janos Follath6264e662019-11-26 11:11:15 +0000209 return input_len;
210}
211
212/*
Andrzej Kurekf7774142020-01-22 06:34:59 -0500213 * Gets \p output_len bytes from the ring buffer \p buf into the
214 * \p output buffer. The output buffer can be NULL, in this case a part of the
215 * ring buffer will be dropped, if the requested length is available.
Janos Follath6264e662019-11-26 11:11:15 +0000216 *
217 * \p buf must have been initialized and set up by calling
218 * `mbedtls_test_buffer_init()` and `mbedtls_test_buffer_setup()`.
219 *
220 * \retval \p output_len, if the data is available.
221 * \retval 0 <= value < \p output_len, if the data is not available.
Andrzej Kurekf7774142020-01-22 06:34:59 -0500222 * \retval -1, if \buf is NULL or it hasn't been set up.
Janos Follath6264e662019-11-26 11:11:15 +0000223 */
224int mbedtls_test_buffer_get( mbedtls_test_buffer *buf,
225 unsigned char* output, size_t output_len )
226{
227 size_t overflow = 0;
228
229 if( ( buf == NULL ) || ( buf->buffer == NULL ) )
230 return -1;
231
Andrzej Kurekf7774142020-01-22 06:34:59 -0500232 if( output == NULL && output_len == 0 )
233 return 0;
Janos Follath6264e662019-11-26 11:11:15 +0000234
235 if( buf->content_length < output_len )
236 output_len = buf->content_length;
237
238 /* Calculate the number of bytes that need to be drawn from lower memory
239 * address */
240 if( buf->start + output_len > buf->capacity )
241 {
242 overflow = ( buf->start + output_len ) % buf->capacity;
243 }
244
Andrzej Kurekf7774142020-01-22 06:34:59 -0500245 if( output != NULL )
246 {
247 memcpy( output, buf->buffer + buf->start, output_len - overflow );
248 memcpy( output + output_len - overflow, buf->buffer, overflow );
249 }
250
Janos Follath6264e662019-11-26 11:11:15 +0000251 buf->content_length -= output_len;
252 buf->start = ( buf->start + output_len ) % buf->capacity;
253
254 return output_len;
255}
256
Hanno Beckera18d1322018-01-03 14:27:32 +0000257/*
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500258 * Errors used in the message transport mock tests
259 */
260 #define MBEDTLS_TEST_ERROR_ARG_NULL -11
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500261 #define MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED -44
262
263/*
264 * Context for a message metadata queue (fifo) that is on top of the ring buffer.
265 */
266typedef struct mbedtls_test_message_queue
267{
268 size_t *messages;
269 int pos;
270 int num;
271 int capacity;
272} mbedtls_test_message_queue;
273
274/*
275 * Setup and free functions for the message metadata queue.
276 *
277 * \p capacity describes the number of message metadata chunks that can be held
278 * within the queue.
279 *
280 * \retval 0, if a metadata queue of a given length can be allocated.
281 * \retval MBEDTLS_ERR_SSL_ALLOC_FAILED, if allocation failed.
282 */
283int mbedtls_test_message_queue_setup( mbedtls_test_message_queue *queue,
284 size_t capacity )
285{
286 queue->messages = (size_t*) mbedtls_calloc( capacity, sizeof(size_t) );
287 if( NULL == queue->messages )
288 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
289
290 queue->capacity = capacity;
291 queue->pos = 0;
292 queue->num = 0;
293
294 return 0;
295}
296
297void mbedtls_test_message_queue_free( mbedtls_test_message_queue *queue )
298{
299 if( queue == NULL )
300 return;
301
302 if( queue->messages != NULL )
303 mbedtls_free( queue->messages );
304
305 memset( queue, 0, sizeof( *queue ) );
306}
307
308/*
309 * Push message length information onto the message metadata queue.
310 * This will become the last element to leave it (fifo).
311 *
312 * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null.
Andrzej Kurekf46b9122020-02-07 08:19:00 -0500313 * \retval MBEDTLS_ERR_SSL_WANT_WRITE, if the queue is full.
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500314 * \retval \p len, if the push was successful.
315 */
316int mbedtls_test_message_queue_push_info( mbedtls_test_message_queue *queue,
317 size_t len )
318{
319 int place;
320 if( queue == NULL )
321 return MBEDTLS_TEST_ERROR_ARG_NULL;
322
323 if( queue->num >= queue->capacity )
Andrzej Kurekf46b9122020-02-07 08:19:00 -0500324 return MBEDTLS_ERR_SSL_WANT_WRITE;
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500325
326 place = ( queue->pos + queue->num ) % queue->capacity;
327 queue->messages[place] = len;
328 queue->num++;
329 return len;
330}
331
332/*
333 * Pop information about the next message length from the queue. This will be
334 * the oldest inserted message length(fifo). \p msg_len can be null, in which
335 * case the data will be popped from the queue but not copied anywhere.
336 *
337 * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null.
Andrzej Kurekf46b9122020-02-07 08:19:00 -0500338 * \retval MBEDTLS_ERR_SSL_WANT_READ, if the queue is empty.
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500339 * \retval message length, if the pop was successful, up to the given
340 \p buf_len.
341 */
342int mbedtls_test_message_queue_pop_info( mbedtls_test_message_queue *queue,
343 size_t buf_len )
344{
345 size_t message_length;
346 if( queue == NULL )
347 return MBEDTLS_TEST_ERROR_ARG_NULL;
348 if( queue->num == 0 )
Andrzej Kurekf46b9122020-02-07 08:19:00 -0500349 return MBEDTLS_ERR_SSL_WANT_READ;
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500350
351 message_length = queue->messages[queue->pos];
352 queue->messages[queue->pos] = 0;
353 queue->num--;
354 queue->pos++;
355 queue->pos %= queue->capacity;
356 if( queue->pos < 0 )
357 queue->pos += queue->capacity;
358
359 return ( message_length > buf_len ) ? buf_len : message_length;
360}
361
362/*
363 * Take a peek on the info about the next message length from the queue.
364 * This will be the oldest inserted message length(fifo).
365 *
366 * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null.
Andrzej Kurekf46b9122020-02-07 08:19:00 -0500367 * \retval MBEDTLS_ERR_SSL_WANT_READ, if the queue is empty.
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500368 * \retval 0, if the peek was successful.
369 * \retval MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED, if the given buffer length is
370 * too small to fit the message. In this case the \p msg_len will be
371 * set to the full message length so that the
372 * caller knows what portion of the message can be dropped.
373 */
374int mbedtls_test_message_queue_peek_info( mbedtls_test_message_queue *queue,
375 size_t buf_len, size_t* msg_len )
376{
377 if( queue == NULL || msg_len == NULL )
378 return MBEDTLS_TEST_ERROR_ARG_NULL;
379 if( queue->num == 0 )
Andrzej Kurekf46b9122020-02-07 08:19:00 -0500380 return MBEDTLS_ERR_SSL_WANT_READ;
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500381
382 *msg_len = queue->messages[queue->pos];
383 return ( *msg_len > buf_len ) ? MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED : 0;
384}
385/*
Janos Follath031827f2019-11-27 11:12:14 +0000386 * Context for the I/O callbacks simulating network connection.
387 */
388
389#define MBEDTLS_MOCK_SOCKET_CONNECTED 1
390
391typedef struct mbedtls_mock_socket
392{
393 int status;
394 mbedtls_test_buffer *input;
395 mbedtls_test_buffer *output;
396 struct mbedtls_mock_socket *peer;
397} mbedtls_mock_socket;
398
399/*
400 * Setup and teardown functions for mock sockets.
401 */
402void mbedtls_mock_socket_init( mbedtls_mock_socket *socket )
403{
404 memset( socket, 0, sizeof( *socket ) );
405}
406
407/*
408 * Closes the socket \p socket.
409 *
410 * \p socket must have been previously initialized by calling
411 * mbedtls_mock_socket_init().
412 *
413 * This function frees all allocated resources and both sockets are aware of the
414 * new connection state.
415 *
416 * That is, this function does not simulate half-open TCP connections and the
417 * phenomenon that when closing a UDP connection the peer is not aware of the
418 * connection having been closed.
419 */
420void mbedtls_mock_socket_close( mbedtls_mock_socket* socket )
421{
422 if( socket == NULL )
423 return;
424
425 if( socket->input != NULL )
426 {
427 mbedtls_test_buffer_free( socket->input );
428 mbedtls_free( socket->input );
429 }
430
431 if( socket->output != NULL )
432 {
433 mbedtls_test_buffer_free( socket->output );
434 mbedtls_free( socket->output );
435 }
436
437 if( socket->peer != NULL )
438 memset( socket->peer, 0, sizeof( *socket->peer ) );
439
440 memset( socket, 0, sizeof( *socket ) );
441}
442
443/*
444 * Establishes a connection between \p peer1 and \p peer2.
445 *
446 * \p peer1 and \p peer2 must have been previously initialized by calling
447 * mbedtls_mock_socket_init().
448 *
449 * The capacites of the internal buffers are set to \p bufsize. Setting this to
450 * the correct value allows for simulation of MTU, sanity testing the mock
451 * implementation and mocking TCP connections with lower memory cost.
452 */
453int mbedtls_mock_socket_connect( mbedtls_mock_socket* peer1,
454 mbedtls_mock_socket* peer2,
455 size_t bufsize )
456{
457 int ret = -1;
458
Piotr Nowickid796e192020-01-28 12:09:47 +0100459 peer1->output =
Janos Follath031827f2019-11-27 11:12:14 +0000460 (mbedtls_test_buffer*) mbedtls_calloc( 1, sizeof(mbedtls_test_buffer) );
461 if( peer1->output == NULL )
462 {
463 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
464 goto exit;
465 }
466 mbedtls_test_buffer_init( peer1->output );
467 if( 0 != ( ret = mbedtls_test_buffer_setup( peer1->output, bufsize ) ) )
468 {
469 goto exit;
470 }
471
Piotr Nowickid796e192020-01-28 12:09:47 +0100472 peer2->output =
473 (mbedtls_test_buffer*) mbedtls_calloc( 1, sizeof(mbedtls_test_buffer) );
474 if( peer2->output == NULL )
475 {
476 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
477 goto exit;
478 }
479 mbedtls_test_buffer_init( peer2->output );
480 if( 0 != ( ret = mbedtls_test_buffer_setup( peer2->output, bufsize ) ) )
481 {
482 goto exit;
483 }
484
Janos Follath031827f2019-11-27 11:12:14 +0000485 peer1->peer = peer2;
486 peer2->peer = peer1;
Piotr Nowickid796e192020-01-28 12:09:47 +0100487 peer1->input = peer2->output;
488 peer2->input = peer1->output;
Janos Follath031827f2019-11-27 11:12:14 +0000489
490 peer1->status = peer2->status = MBEDTLS_MOCK_SOCKET_CONNECTED;
491 ret = 0;
492
493exit:
494
495 if( ret != 0 )
496 {
497 mbedtls_mock_socket_close( peer1 );
498 mbedtls_mock_socket_close( peer2 );
499 }
500
501 return ret;
502}
503
504/*
505 * Callbacks for simulating blocking I/O over connection-oriented transport.
506 */
507
508int mbedtls_mock_tcp_send_b( void *ctx, const unsigned char *buf, size_t len )
509{
510 mbedtls_mock_socket *socket = (mbedtls_mock_socket*) ctx;
511
512 if( socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED )
513 return -1;
514
515 return mbedtls_test_buffer_put( socket->output, buf, len );
516}
517
518int mbedtls_mock_tcp_recv_b( void *ctx, unsigned char *buf, size_t len )
519{
520 mbedtls_mock_socket *socket = (mbedtls_mock_socket*) ctx;
521
522 if( socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED )
523 return -1;
524
525 return mbedtls_test_buffer_get( socket->input, buf, len );
526}
527
528/*
Janos Follath3766ba52019-11-27 13:31:42 +0000529 * Callbacks for simulating non-blocking I/O over connection-oriented transport.
530 */
531
532int mbedtls_mock_tcp_send_nb( void *ctx, const unsigned char *buf, size_t len )
533{
534 mbedtls_mock_socket *socket = (mbedtls_mock_socket*) ctx;
535
536 if( socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED )
537 return -1;
538
Piotr Nowicki890b5ca2020-01-15 16:19:07 +0100539 if( socket->output->capacity == socket->output->content_length )
Janos Follath3766ba52019-11-27 13:31:42 +0000540 {
Janos Follath3766ba52019-11-27 13:31:42 +0000541 return MBEDTLS_ERR_SSL_WANT_WRITE;
542 }
543
Janos Follath3766ba52019-11-27 13:31:42 +0000544 return mbedtls_test_buffer_put( socket->output, buf, len );
545}
546
547int mbedtls_mock_tcp_recv_nb( void *ctx, unsigned char *buf, size_t len )
548{
549 mbedtls_mock_socket *socket = (mbedtls_mock_socket*) ctx;
550
551 if( socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED )
552 return -1;
553
Andrzej Kurekf40daa32020-02-04 09:00:01 -0500554 if( socket->input->content_length == 0 )
Janos Follath3766ba52019-11-27 13:31:42 +0000555 {
Janos Follath3766ba52019-11-27 13:31:42 +0000556 return MBEDTLS_ERR_SSL_WANT_READ;
557 }
558
Janos Follath3766ba52019-11-27 13:31:42 +0000559 return mbedtls_test_buffer_get( socket->input, buf, len );
560}
561
Andrzej Kurekbc483de2020-01-22 03:40:00 -0500562/* Errors used in the message socket mocks */
563
564#define MBEDTLS_TEST_ERROR_CONTEXT_ERROR -55
565#define MBEDTLS_TEST_ERROR_SEND_FAILED -66
566#define MBEDTLS_TEST_ERROR_RECV_FAILED -77
567
568/*
569 * Structure used as an addon, or a wrapper, around the mocked sockets.
570 * Contains an input queue, to which the other socket pushes metadata,
571 * and an output queue, to which this one pushes metadata. This context is
572 * considered as an owner of the input queue only, which is initialized and
573 * freed in the respective setup and free calls.
574 */
575typedef struct mbedtls_test_message_socket_context
576{
577 mbedtls_test_message_queue* queue_input;
578 mbedtls_test_message_queue* queue_output;
579 mbedtls_mock_socket* socket;
580} mbedtls_test_message_socket_context;
581
Andrzej Kurek45916ba2020-03-05 14:46:22 -0500582void mbedtls_message_socket_init( mbedtls_test_message_socket_context *ctx )
583{
584 ctx->queue_input = NULL;
585 ctx->queue_output = NULL;
586 ctx->socket = NULL;
587}
588
Andrzej Kurekbc483de2020-01-22 03:40:00 -0500589/*
590 * Setup a given mesasge socket context including initialization of
591 * input/output queues to a chosen capacity of messages. Also set the
592 * corresponding mock socket.
593 *
594 * \retval 0, if everything succeeds.
595 * \retval MBEDTLS_ERR_SSL_ALLOC_FAILED, if allocation of a message
596 * queue failed.
597 */
598int mbedtls_message_socket_setup( mbedtls_test_message_queue* queue_input,
599 mbedtls_test_message_queue* queue_output,
600 size_t queue_capacity,
601 mbedtls_mock_socket* socket,
602 mbedtls_test_message_socket_context* ctx )
603{
604 int ret = mbedtls_test_message_queue_setup( queue_input, queue_capacity );
605 if( ret != 0 )
606 return ret;
607 ctx->queue_input = queue_input;
608 ctx->queue_output = queue_output;
609 ctx->socket = socket;
610 mbedtls_mock_socket_init( socket );
611
612 return 0;
613}
614
615/*
616 * Close a given message socket context, along with the socket itself. Free the
617 * memory allocated by the input queue.
618 */
619void mbedtls_message_socket_close( mbedtls_test_message_socket_context* ctx )
620{
621 if( ctx == NULL )
622 return;
623
624 mbedtls_test_message_queue_free( ctx->queue_input );
625 mbedtls_mock_socket_close( ctx->socket );
626 memset( ctx, 0, sizeof( *ctx ) );
627}
628
629/*
630 * Send one message through a given message socket context.
631 *
632 * \retval \p len, if everything succeeds.
633 * \retval MBEDTLS_TEST_ERROR_CONTEXT_ERROR, if any of the needed context
634 * elements or the context itself is null.
635 * \retval MBEDTLS_TEST_ERROR_SEND_FAILED if mbedtls_mock_tcp_send_b failed.
Andrzej Kurekf46b9122020-02-07 08:19:00 -0500636 * \retval MBEDTLS_ERR_SSL_WANT_WRITE, if the output queue is full.
Andrzej Kurekbc483de2020-01-22 03:40:00 -0500637 *
638 * This function will also return any error from
639 * mbedtls_test_message_queue_push_info.
640 */
641int mbedtls_mock_tcp_send_msg( void *ctx, const unsigned char *buf, size_t len )
642{
643 mbedtls_test_message_queue* queue;
644 mbedtls_mock_socket* socket;
645 mbedtls_test_message_socket_context *context = (mbedtls_test_message_socket_context*) ctx;
646
647 if( context == NULL || context->socket == NULL
648 || context->queue_output == NULL )
649 {
650 return MBEDTLS_TEST_ERROR_CONTEXT_ERROR;
651 }
652
653 queue = context->queue_output;
654 socket = context->socket;
655
656 if( queue->num >= queue->capacity )
Andrzej Kurekf46b9122020-02-07 08:19:00 -0500657 return MBEDTLS_ERR_SSL_WANT_WRITE;
Andrzej Kurekbc483de2020-01-22 03:40:00 -0500658
659 if( mbedtls_mock_tcp_send_b( socket, buf, len ) != (int) len )
660 return MBEDTLS_TEST_ERROR_SEND_FAILED;
661
662 return mbedtls_test_message_queue_push_info( queue, len );
663}
664
665/*
666 * Receive one message from a given message socket context and return message
667 * length or an error.
668 *
669 * \retval message length, if everything succeeds.
670 * \retval MBEDTLS_TEST_ERROR_CONTEXT_ERROR, if any of the needed context
671 * elements or the context itself is null.
672 * \retval MBEDTLS_TEST_ERROR_RECV_FAILED if mbedtls_mock_tcp_recv_b failed.
673 *
674 * This function will also return any error other than
675 * MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED from mbedtls_test_message_queue_peek_info.
676 */
677int mbedtls_mock_tcp_recv_msg( void *ctx, unsigned char *buf, size_t buf_len )
678{
679 mbedtls_test_message_queue* queue;
680 mbedtls_mock_socket* socket;
681 mbedtls_test_message_socket_context *context = (mbedtls_test_message_socket_context*) ctx;
Gilles Peskine19e841e2020-03-09 20:43:51 +0100682 size_t drop_len = 0;
Andrzej Kurekbc483de2020-01-22 03:40:00 -0500683 size_t msg_len;
684 int ret;
685
686 if( context == NULL || context->socket == NULL
687 || context->queue_input == NULL )
688 {
689 return MBEDTLS_TEST_ERROR_CONTEXT_ERROR;
690 }
691
692 queue = context->queue_input;
693 socket = context->socket;
694
695 /* Peek first, so that in case of a socket error the data remains in
696 * the queue. */
697 ret = mbedtls_test_message_queue_peek_info( queue, buf_len, &msg_len );
698 if( ret == MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED )
699 {
700 /* Calculate how much to drop */
701 drop_len = msg_len - buf_len;
702
703 /* Set the requested message len to be buffer length */
704 msg_len = buf_len;
705 } else if( ret != 0 )
706 {
707 return ret;
708 }
709
710 if( mbedtls_mock_tcp_recv_b( socket, buf, msg_len ) != (int) msg_len )
711 return MBEDTLS_TEST_ERROR_RECV_FAILED;
712
713 if( ret == MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED )
714 {
715 /* Drop the remaining part of the message */
716 if( mbedtls_mock_tcp_recv_b( socket, NULL, drop_len ) != (int) drop_len )
717 {
718 /* Inconsistent state - part of the message was read,
719 * and a part couldn't. Not much we can do here, but it should not
720 * happen in test environment, unless forced manually. */
721 }
722 }
723 mbedtls_test_message_queue_pop_info( queue, buf_len );
724
725 return msg_len;
726}
727
Andrzej Kurek9155e7f2022-10-18 09:36:19 -0400728#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) && \
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +0200729 defined(MBEDTLS_ENTROPY_C) && \
730 defined(MBEDTLS_CTR_DRBG_C)
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100731
732/*
733 * Structure with endpoint's certificates for SSL communication tests.
734 */
735typedef struct mbedtls_endpoint_certificate
736{
Andrzej Kurek0d2982b2022-10-18 07:55:46 -0400737 mbedtls_x509_crt* ca_cert;
738 mbedtls_x509_crt* cert;
739 mbedtls_pk_context* pkey;
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100740} mbedtls_endpoint_certificate;
741
742/*
743 * Endpoint structure for SSL communication tests.
744 */
745typedef struct mbedtls_endpoint
746{
747 const char *name;
748 mbedtls_ssl_context ssl;
749 mbedtls_ssl_config conf;
750 mbedtls_ctr_drbg_context ctr_drbg;
751 mbedtls_entropy_context entropy;
752 mbedtls_mock_socket socket;
753 mbedtls_endpoint_certificate cert;
754} mbedtls_endpoint;
755
756/*
Andrzej Kurek0d2982b2022-10-18 07:55:46 -0400757 * Deinitializes certificates from endpoint represented by \p ep.
758 */
759void mbedtls_endpoint_certificate_free( mbedtls_endpoint *ep )
760{
761 mbedtls_endpoint_certificate *cert = &( ep->cert );
762 if( cert != NULL )
763 {
764 if( cert->ca_cert != NULL )
765 {
766 mbedtls_x509_crt_free( cert->ca_cert );
767 mbedtls_free( cert->ca_cert );
768 cert->ca_cert = NULL;
769 }
770 if( cert->cert != NULL )
771 {
772 mbedtls_x509_crt_free( cert->cert );
773 mbedtls_free( cert->cert );
774 cert->cert = NULL;
775 }
776 if( cert->pkey != NULL )
777 {
778#if defined(MBEDTLS_USE_PSA_CRYPTO)
779 if( mbedtls_pk_get_type( cert->pkey ) == MBEDTLS_PK_OPAQUE )
780 {
781 mbedtls_svc_key_id_t *key_slot = cert->pkey->pk_ctx;
782 psa_destroy_key( *key_slot );
783 }
784#endif
785 mbedtls_pk_free( cert->pkey );
786 mbedtls_free( cert->pkey );
787 cert->pkey = NULL;
788 }
789 }
790}
791
792/*
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100793 * Initializes \p ep_cert structure and assigns it to endpoint
794 * represented by \p ep.
795 *
796 * \retval 0 on success, otherwise error code.
797 */
Andrzej Kurekb2980742020-02-02 19:25:26 -0500798int mbedtls_endpoint_certificate_init( mbedtls_endpoint *ep, int pk_alg )
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100799{
800 int i = 0;
801 int ret = -1;
Andrzej Kurek0d2982b2022-10-18 07:55:46 -0400802 mbedtls_endpoint_certificate *cert = NULL;
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100803
804 if( ep == NULL )
805 {
806 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
807 }
808
809 cert = &( ep->cert );
Andrzej Kurek0b56ce02022-10-13 08:22:08 -0400810 ASSERT_ALLOC( cert->ca_cert, 1 );
811 ASSERT_ALLOC( cert->cert, 1 );
812 ASSERT_ALLOC( cert->pkey, 1 );
Andrzej Kurek0d2982b2022-10-18 07:55:46 -0400813
814 mbedtls_x509_crt_init( cert->ca_cert );
815 mbedtls_x509_crt_init( cert->cert );
816 mbedtls_pk_init( cert->pkey );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100817
818 /* Load the trusted CA */
819
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100820 for( i = 0; mbedtls_test_cas_der[i] != NULL; i++ )
821 {
Andrzej Kurek0d2982b2022-10-18 07:55:46 -0400822 ret = mbedtls_x509_crt_parse_der( cert->ca_cert,
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100823 (const unsigned char *) mbedtls_test_cas_der[i],
824 mbedtls_test_cas_der_len[i] );
825 TEST_ASSERT( ret == 0 );
826 }
827
828 /* Load own certificate and private key */
829
830 if( ep->conf.endpoint == MBEDTLS_SSL_IS_SERVER )
831 {
Andrzej Kurekb2980742020-02-02 19:25:26 -0500832 if( pk_alg == MBEDTLS_PK_RSA )
833 {
Andrzej Kurek0d2982b2022-10-18 07:55:46 -0400834 ret = mbedtls_x509_crt_parse( cert->cert,
Andrzej Kurekb2980742020-02-02 19:25:26 -0500835 (const unsigned char*) mbedtls_test_srv_crt_rsa_sha256_der,
836 mbedtls_test_srv_crt_rsa_sha256_der_len );
837 TEST_ASSERT( ret == 0 );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100838
Andrzej Kurek0d2982b2022-10-18 07:55:46 -0400839 ret = mbedtls_pk_parse_key( cert->pkey,
Andrzej Kurekb2980742020-02-02 19:25:26 -0500840 (const unsigned char*) mbedtls_test_srv_key_rsa_der,
841 mbedtls_test_srv_key_rsa_der_len, NULL, 0 );
842 TEST_ASSERT( ret == 0 );
843 }
844 else
845 {
Andrzej Kurek0d2982b2022-10-18 07:55:46 -0400846 ret = mbedtls_x509_crt_parse( cert->cert,
Andrzej Kurekb2980742020-02-02 19:25:26 -0500847 (const unsigned char*) mbedtls_test_srv_crt_ec_der,
848 mbedtls_test_srv_crt_ec_der_len );
849 TEST_ASSERT( ret == 0 );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100850
Andrzej Kurek0d2982b2022-10-18 07:55:46 -0400851 ret = mbedtls_pk_parse_key( cert->pkey,
Andrzej Kurekb2980742020-02-02 19:25:26 -0500852 (const unsigned char*) mbedtls_test_srv_key_ec_der,
853 mbedtls_test_srv_key_ec_der_len, NULL, 0 );
854 TEST_ASSERT( ret == 0 );
855 }
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100856 }
857 else
858 {
Andrzej Kurekb2980742020-02-02 19:25:26 -0500859 if( pk_alg == MBEDTLS_PK_RSA )
860 {
Andrzej Kurek0d2982b2022-10-18 07:55:46 -0400861 ret = mbedtls_x509_crt_parse( cert->cert,
Andrzej Kurekb2980742020-02-02 19:25:26 -0500862 (const unsigned char *) mbedtls_test_cli_crt_rsa_der,
863 mbedtls_test_cli_crt_rsa_der_len );
864 TEST_ASSERT( ret == 0 );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100865
Andrzej Kurek0d2982b2022-10-18 07:55:46 -0400866 ret = mbedtls_pk_parse_key( cert->pkey,
Andrzej Kurekb2980742020-02-02 19:25:26 -0500867 (const unsigned char *) mbedtls_test_cli_key_rsa_der,
868 mbedtls_test_cli_key_rsa_der_len, NULL, 0 );
869 TEST_ASSERT( ret == 0 );
870 }
871 else
872 {
Andrzej Kurek0d2982b2022-10-18 07:55:46 -0400873 ret = mbedtls_x509_crt_parse( cert->cert,
Andrzej Kurekb2980742020-02-02 19:25:26 -0500874 (const unsigned char *) mbedtls_test_cli_crt_ec_der,
875 mbedtls_test_cli_crt_ec_len );
876 TEST_ASSERT( ret == 0 );
877
Andrzej Kurek0d2982b2022-10-18 07:55:46 -0400878 ret = mbedtls_pk_parse_key( cert->pkey,
Andrzej Kurekb2980742020-02-02 19:25:26 -0500879 (const unsigned char *) mbedtls_test_cli_key_ec_der,
880 mbedtls_test_cli_key_ec_der_len, NULL, 0 );
881 TEST_ASSERT( ret == 0 );
882 }
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100883 }
884
Andrzej Kurek0d2982b2022-10-18 07:55:46 -0400885 mbedtls_ssl_conf_ca_chain( &( ep->conf ), cert->ca_cert, NULL );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100886
Andrzej Kurek0d2982b2022-10-18 07:55:46 -0400887 ret = mbedtls_ssl_conf_own_cert( &( ep->conf ), cert->cert,
888 cert->pkey );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100889 TEST_ASSERT( ret == 0 );
890
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100891exit:
892 if( ret != 0 )
893 {
Andrzej Kurek0d2982b2022-10-18 07:55:46 -0400894 mbedtls_endpoint_certificate_free( ep );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100895 }
896
897 return ret;
898}
899
900/*
901 * Initializes \p ep structure. It is important to call `mbedtls_endpoint_free()`
902 * after calling this function even if it fails.
903 *
904 * \p endpoint_type must be set as MBEDTLS_SSL_IS_SERVER or
905 * MBEDTLS_SSL_IS_CLIENT.
Andrzej Kurek15daf502020-02-12 09:17:52 -0500906 * \p pk_alg the algorithm to use, currently only MBEDTLS_PK_RSA and
907 * MBEDTLS_PK_ECDSA are supported.
908 * \p dtls_context - in case of DTLS - this is the context handling metadata.
909 * \p input_queue - used only in case of DTLS.
910 * \p output_queue - used only in case of DTLS.
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100911 *
912 * \retval 0 on success, otherwise error code.
913 */
Andrzej Kurek15daf502020-02-12 09:17:52 -0500914int mbedtls_endpoint_init( mbedtls_endpoint *ep, int endpoint_type, int pk_alg,
915 mbedtls_test_message_socket_context *dtls_context,
916 mbedtls_test_message_queue *input_queue,
Andrzej Kurek535cd172022-03-08 06:50:12 -0500917 mbedtls_test_message_queue *output_queue,
918 const mbedtls_ecp_group_id *curves )
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100919{
920 int ret = -1;
921
Andrzej Kurek15daf502020-02-12 09:17:52 -0500922 if( dtls_context != NULL && ( input_queue == NULL || output_queue == NULL ) )
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100923 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Andrzej Kurek15daf502020-02-12 09:17:52 -0500924
925 if( ep == NULL )
926 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100927
928 memset( ep, 0, sizeof( *ep ) );
929
930 ep->name = ( endpoint_type == MBEDTLS_SSL_IS_SERVER ) ? "Server" : "Client";
931
932 mbedtls_ssl_init( &( ep->ssl ) );
933 mbedtls_ssl_config_init( &( ep->conf ) );
934 mbedtls_ctr_drbg_init( &( ep->ctr_drbg ) );
935 mbedtls_ssl_conf_rng( &( ep->conf ),
936 mbedtls_ctr_drbg_random,
937 &( ep->ctr_drbg ) );
938 mbedtls_entropy_init( &( ep->entropy ) );
Andrzej Kurek15daf502020-02-12 09:17:52 -0500939 if( dtls_context != NULL )
940 {
941 TEST_ASSERT( mbedtls_message_socket_setup( input_queue, output_queue,
942 100, &( ep->socket ),
943 dtls_context ) == 0 );
944 }
945 else
946 {
947 mbedtls_mock_socket_init( &( ep->socket ) );
948 }
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100949
950 ret = mbedtls_ctr_drbg_seed( &( ep->ctr_drbg ), mbedtls_entropy_func,
951 &( ep->entropy ), (const unsigned char *) ( ep->name ),
952 strlen( ep->name ) );
953 TEST_ASSERT( ret == 0 );
954
955 /* Non-blocking callbacks without timeout */
Andrzej Kurek15daf502020-02-12 09:17:52 -0500956 if( dtls_context != NULL )
957 {
958 mbedtls_ssl_set_bio( &( ep->ssl ), dtls_context,
959 mbedtls_mock_tcp_send_msg,
960 mbedtls_mock_tcp_recv_msg,
961 NULL );
962 }
963 else
964 {
965 mbedtls_ssl_set_bio( &( ep->ssl ), &( ep->socket ),
966 mbedtls_mock_tcp_send_nb,
967 mbedtls_mock_tcp_recv_nb,
968 NULL );
969 }
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100970
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100971 ret = mbedtls_ssl_config_defaults( &( ep->conf ), endpoint_type,
Andrzej Kurek15daf502020-02-12 09:17:52 -0500972 ( dtls_context != NULL ) ?
973 MBEDTLS_SSL_TRANSPORT_DATAGRAM :
974 MBEDTLS_SSL_TRANSPORT_STREAM,
975 MBEDTLS_SSL_PRESET_DEFAULT );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100976 TEST_ASSERT( ret == 0 );
977
Andrzej Kurek96bf3d12022-04-15 07:35:16 -0400978#if defined(MBEDTLS_ECP_C)
Andrzej Kurek535cd172022-03-08 06:50:12 -0500979 if( curves != NULL )
980 mbedtls_ssl_conf_curves( &(ep->conf), curves );
Andrzej Kurek96bf3d12022-04-15 07:35:16 -0400981#else
982 (void) curves;
983#endif
Andrzej Kurek535cd172022-03-08 06:50:12 -0500984
Andrzej Kurek1a44a152020-02-07 08:21:32 -0500985 ret = mbedtls_ssl_setup( &( ep->ssl ), &( ep->conf ) );
986 TEST_ASSERT( ret == 0 );
Andrzej Kurek15daf502020-02-12 09:17:52 -0500987
988#if defined(MBEDTLS_SSL_PROTO_DTLS) && defined(MBEDTLS_SSL_SRV_C)
989 if( endpoint_type == MBEDTLS_SSL_IS_SERVER && dtls_context != NULL )
990 mbedtls_ssl_conf_dtls_cookies( &( ep->conf ), NULL, NULL, NULL );
991#endif
992
Andrzej Kurekb2980742020-02-02 19:25:26 -0500993 ret = mbedtls_endpoint_certificate_init( ep, pk_alg );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100994 TEST_ASSERT( ret == 0 );
995
996exit:
997 return ret;
998}
999
1000/*
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01001001 * Deinitializes endpoint represented by \p ep.
1002 */
Andrzej Kurek15daf502020-02-12 09:17:52 -05001003void mbedtls_endpoint_free( mbedtls_endpoint *ep,
1004 mbedtls_test_message_socket_context *context )
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01001005{
1006 mbedtls_endpoint_certificate_free( ep );
1007
1008 mbedtls_ssl_free( &( ep->ssl ) );
1009 mbedtls_ssl_config_free( &( ep->conf ) );
1010 mbedtls_ctr_drbg_free( &( ep->ctr_drbg ) );
1011 mbedtls_entropy_free( &( ep->entropy ) );
Andrzej Kurek15daf502020-02-12 09:17:52 -05001012
1013 if( context != NULL )
1014 {
1015 mbedtls_message_socket_close( context );
1016 }
1017 else
1018 {
1019 mbedtls_mock_socket_close( &( ep->socket ) );
1020 }
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01001021}
1022
1023/*
1024 * This function moves ssl handshake from \p ssl to prescribed \p state.
1025 * /p second_ssl is used as second endpoint and their sockets have to be
1026 * connected before calling this function.
1027 *
1028 * \retval 0 on success, otherwise error code.
1029 */
1030int mbedtls_move_handshake_to_state( mbedtls_ssl_context *ssl,
1031 mbedtls_ssl_context *second_ssl,
1032 int state )
1033{
1034 enum { BUFFSIZE = 1024 };
1035 int max_steps = 1000;
1036 int ret = 0;
1037
1038 if( ssl == NULL || second_ssl == NULL )
1039 {
1040 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
1041 }
1042
1043 /* Perform communication via connected sockets */
1044 while( ( ssl->state != state ) && ( --max_steps >= 0 ) )
1045 {
1046 /* If /p second_ssl ends the handshake procedure before /p ssl then
1047 * there is no need to call the next step */
1048 if( second_ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
1049 {
1050 ret = mbedtls_ssl_handshake_step( second_ssl );
1051 if( ret != 0 && ret != MBEDTLS_ERR_SSL_WANT_READ &&
1052 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
1053 {
1054 return ret;
1055 }
1056 }
1057
1058 /* We only care about the \p ssl state and returns, so we call it last,
1059 * to leave the iteration as soon as the state is as expected. */
1060 ret = mbedtls_ssl_handshake_step( ssl );
1061 if( ret != 0 && ret != MBEDTLS_ERR_SSL_WANT_READ &&
1062 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
1063 {
1064 return ret;
1065 }
1066 }
1067
1068 return ( max_steps >= 0 ) ? ret : -1;
1069}
1070
Andrzej Kurek9155e7f2022-10-18 09:36:19 -04001071#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01001072
Janos Follath3766ba52019-11-27 13:31:42 +00001073/*
Piotr Nowicki438bf3b2020-03-10 12:59:10 +01001074 * Write application data. Increase write counter if necessary.
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001075 */
1076int mbedtls_ssl_write_fragment( mbedtls_ssl_context *ssl, unsigned char *buf,
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001077 int buf_len, int *written,
Piotr Nowicki438bf3b2020-03-10 12:59:10 +01001078 const int expected_fragments )
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001079{
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001080 int ret = mbedtls_ssl_write( ssl, buf + *written, buf_len - *written );
1081 if( ret > 0 )
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001082 {
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001083 *written += ret;
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001084 }
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001085
1086 if( expected_fragments == 0 )
1087 {
1088 /* Used for DTLS and the message size larger than MFL. In that case
1089 * the message can not be fragmented and the library should return
1090 * MBEDTLS_ERR_SSL_BAD_INPUT_DATA error. This error must be returned
1091 * to prevent a dead loop inside mbedtls_exchange_data(). */
1092 return ret;
1093 }
1094 else if( expected_fragments == 1 )
1095 {
1096 /* Used for TLS/DTLS and the message size lower than MFL */
1097 TEST_ASSERT( ret == buf_len ||
1098 ret == MBEDTLS_ERR_SSL_WANT_READ ||
1099 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
1100 }
1101 else
1102 {
1103 /* Used for TLS and the message size larger than MFL */
1104 TEST_ASSERT( expected_fragments > 1 );
1105 TEST_ASSERT( ( ret >= 0 && ret <= buf_len ) ||
1106 ret == MBEDTLS_ERR_SSL_WANT_READ ||
1107 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
1108 }
1109
1110 return 0;
1111
1112exit:
1113 /* Some of the tests failed */
1114 return -1;
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001115}
1116
1117/*
Piotr Nowicki438bf3b2020-03-10 12:59:10 +01001118 * Read application data and increase read counter and fragments counter if necessary.
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001119 */
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001120int mbedtls_ssl_read_fragment( mbedtls_ssl_context *ssl, unsigned char *buf,
1121 int buf_len, int *read,
Piotr Nowicki438bf3b2020-03-10 12:59:10 +01001122 int *fragments, const int expected_fragments )
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001123{
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001124 int ret = mbedtls_ssl_read( ssl, buf + *read, buf_len - *read );
1125 if( ret > 0 )
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001126 {
Piotr Nowicki438bf3b2020-03-10 12:59:10 +01001127 ( *fragments )++;
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001128 *read += ret;
1129 }
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001130
1131 if( expected_fragments == 0 )
1132 {
1133 TEST_ASSERT( ret == 0 );
1134 }
1135 else if( expected_fragments == 1 )
1136 {
1137 TEST_ASSERT( ret == buf_len ||
1138 ret == MBEDTLS_ERR_SSL_WANT_READ ||
1139 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
1140 }
1141 else
1142 {
1143 TEST_ASSERT( expected_fragments > 1 );
1144 TEST_ASSERT( ( ret >= 0 && ret <= buf_len ) ||
1145 ret == MBEDTLS_ERR_SSL_WANT_READ ||
1146 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
1147 }
1148
1149 return 0;
1150
1151exit:
1152 /* Some of the tests failed */
1153 return -1;
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001154}
1155
1156/*
Hanno Beckera18d1322018-01-03 14:27:32 +00001157 * Helper function setting up inverse record transformations
1158 * using given cipher, hash, EtM mode, authentication tag length,
1159 * and version.
1160 */
1161
1162#define CHK( x ) \
1163 do \
1164 { \
1165 if( !( x ) ) \
Hanno Becker81e16a32019-03-01 11:21:44 +00001166 { \
Hanno Beckera5780f12019-04-05 09:55:37 +01001167 ret = -1; \
Hanno Becker81e16a32019-03-01 11:21:44 +00001168 goto cleanup; \
1169 } \
Hanno Beckera18d1322018-01-03 14:27:32 +00001170 } while( 0 )
1171
Andrzej Kurekf40daa32020-02-04 09:00:01 -05001172void set_ciphersuite( mbedtls_ssl_config *conf, const char *cipher,
1173 int* forced_ciphersuite )
1174{
1175 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
1176 forced_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id( cipher );
1177 forced_ciphersuite[1] = 0;
1178
1179 ciphersuite_info =
1180 mbedtls_ssl_ciphersuite_from_id( forced_ciphersuite[0] );
1181
1182 TEST_ASSERT( ciphersuite_info != NULL );
1183 TEST_ASSERT( ciphersuite_info->min_minor_ver <= conf->max_minor_ver );
1184 TEST_ASSERT( ciphersuite_info->max_minor_ver >= conf->min_minor_ver );
1185
1186 if( conf->max_minor_ver > ciphersuite_info->max_minor_ver )
1187 {
1188 conf->max_minor_ver = ciphersuite_info->max_minor_ver;
1189 }
1190 if( conf->min_minor_ver < ciphersuite_info->min_minor_ver )
1191 {
1192 conf->min_minor_ver = ciphersuite_info->min_minor_ver;
1193 }
1194
1195 mbedtls_ssl_conf_ciphersuites( conf, forced_ciphersuite );
1196
1197exit:
1198 return;
1199}
1200
Andrzej Kurekcc5169c2020-02-04 09:04:56 -05001201int psk_dummy_callback( void *p_info, mbedtls_ssl_context *ssl,
1202 const unsigned char *name, size_t name_len )
1203{
1204 (void) p_info;
1205 (void) ssl;
1206 (void) name;
1207 (void) name_len;
1208
1209 return ( 0 );
1210}
1211
Hanno Beckerd856c822019-04-29 17:30:59 +01001212#if MBEDTLS_SSL_CID_OUT_LEN_MAX > MBEDTLS_SSL_CID_IN_LEN_MAX
1213#define SSL_CID_LEN_MIN MBEDTLS_SSL_CID_IN_LEN_MAX
1214#else
1215#define SSL_CID_LEN_MIN MBEDTLS_SSL_CID_OUT_LEN_MAX
1216#endif
Hanno Beckera18d1322018-01-03 14:27:32 +00001217
1218static int build_transforms( mbedtls_ssl_transform *t_in,
1219 mbedtls_ssl_transform *t_out,
1220 int cipher_type, int hash_id,
Hanno Beckerd856c822019-04-29 17:30:59 +01001221 int etm, int tag_mode, int ver,
1222 size_t cid0_len,
1223 size_t cid1_len )
Hanno Beckera18d1322018-01-03 14:27:32 +00001224{
1225 mbedtls_cipher_info_t const *cipher_info;
Hanno Beckera5780f12019-04-05 09:55:37 +01001226 int ret = 0;
Hanno Beckera18d1322018-01-03 14:27:32 +00001227
1228 size_t keylen, maclen, ivlen;
Hanno Becker81e16a32019-03-01 11:21:44 +00001229 unsigned char *key0 = NULL, *key1 = NULL;
Paul Elliott6f1eda72020-06-11 20:22:00 +01001230 unsigned char *md0 = NULL, *md1 = NULL;
Hanno Beckera18d1322018-01-03 14:27:32 +00001231 unsigned char iv_enc[16], iv_dec[16];
1232
Hanno Beckera0e20d02019-05-15 14:03:01 +01001233#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd856c822019-04-29 17:30:59 +01001234 unsigned char cid0[ SSL_CID_LEN_MIN ];
1235 unsigned char cid1[ SSL_CID_LEN_MIN ];
1236
Ronald Cron351f0ee2020-06-10 12:12:18 +02001237 mbedtls_test_rnd_std_rand( NULL, cid0, sizeof( cid0 ) );
1238 mbedtls_test_rnd_std_rand( NULL, cid1, sizeof( cid1 ) );
Hanno Becker43c24b82019-05-01 09:45:57 +01001239#else
1240 ((void) cid0_len);
1241 ((void) cid1_len);
Hanno Beckera0e20d02019-05-15 14:03:01 +01001242#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerd856c822019-04-29 17:30:59 +01001243
Hanno Beckera18d1322018-01-03 14:27:32 +00001244 maclen = 0;
1245
1246 /* Pick cipher */
1247 cipher_info = mbedtls_cipher_info_from_type( cipher_type );
1248 CHK( cipher_info != NULL );
1249 CHK( cipher_info->iv_size <= 16 );
1250 CHK( cipher_info->key_bitlen % 8 == 0 );
1251
1252 /* Pick keys */
1253 keylen = cipher_info->key_bitlen / 8;
Hanno Becker78d1f702019-04-05 09:56:10 +01001254 /* Allocate `keylen + 1` bytes to ensure that we get
1255 * a non-NULL pointers from `mbedtls_calloc` even if
1256 * `keylen == 0` in the case of the NULL cipher. */
1257 CHK( ( key0 = mbedtls_calloc( 1, keylen + 1 ) ) != NULL );
1258 CHK( ( key1 = mbedtls_calloc( 1, keylen + 1 ) ) != NULL );
Hanno Beckera18d1322018-01-03 14:27:32 +00001259 memset( key0, 0x1, keylen );
1260 memset( key1, 0x2, keylen );
1261
1262 /* Setup cipher contexts */
1263 CHK( mbedtls_cipher_setup( &t_in->cipher_ctx_enc, cipher_info ) == 0 );
1264 CHK( mbedtls_cipher_setup( &t_in->cipher_ctx_dec, cipher_info ) == 0 );
1265 CHK( mbedtls_cipher_setup( &t_out->cipher_ctx_enc, cipher_info ) == 0 );
1266 CHK( mbedtls_cipher_setup( &t_out->cipher_ctx_dec, cipher_info ) == 0 );
1267
1268#if defined(MBEDTLS_CIPHER_MODE_CBC)
1269 if( cipher_info->mode == MBEDTLS_MODE_CBC )
1270 {
1271 CHK( mbedtls_cipher_set_padding_mode( &t_in->cipher_ctx_enc,
1272 MBEDTLS_PADDING_NONE ) == 0 );
1273 CHK( mbedtls_cipher_set_padding_mode( &t_in->cipher_ctx_dec,
1274 MBEDTLS_PADDING_NONE ) == 0 );
1275 CHK( mbedtls_cipher_set_padding_mode( &t_out->cipher_ctx_enc,
1276 MBEDTLS_PADDING_NONE ) == 0 );
1277 CHK( mbedtls_cipher_set_padding_mode( &t_out->cipher_ctx_dec,
1278 MBEDTLS_PADDING_NONE ) == 0 );
1279 }
1280#endif /* MBEDTLS_CIPHER_MODE_CBC */
1281
1282 CHK( mbedtls_cipher_setkey( &t_in->cipher_ctx_enc, key0,
1283 keylen << 3, MBEDTLS_ENCRYPT ) == 0 );
1284 CHK( mbedtls_cipher_setkey( &t_in->cipher_ctx_dec, key1,
1285 keylen << 3, MBEDTLS_DECRYPT ) == 0 );
1286 CHK( mbedtls_cipher_setkey( &t_out->cipher_ctx_enc, key1,
1287 keylen << 3, MBEDTLS_ENCRYPT ) == 0 );
1288 CHK( mbedtls_cipher_setkey( &t_out->cipher_ctx_dec, key0,
1289 keylen << 3, MBEDTLS_DECRYPT ) == 0 );
Hanno Beckera18d1322018-01-03 14:27:32 +00001290
1291 /* Setup MAC contexts */
1292#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1293 if( cipher_info->mode == MBEDTLS_MODE_CBC ||
1294 cipher_info->mode == MBEDTLS_MODE_STREAM )
1295 {
1296 mbedtls_md_info_t const *md_info;
Hanno Beckera18d1322018-01-03 14:27:32 +00001297
1298 /* Pick hash */
1299 md_info = mbedtls_md_info_from_type( hash_id );
1300 CHK( md_info != NULL );
1301
1302 /* Pick hash keys */
1303 maclen = mbedtls_md_get_size( md_info );
Hanno Becker3ee54212019-04-04 16:31:26 +01001304 CHK( ( md0 = mbedtls_calloc( 1, maclen ) ) != NULL );
1305 CHK( ( md1 = mbedtls_calloc( 1, maclen ) ) != NULL );
Hanno Beckera18d1322018-01-03 14:27:32 +00001306 memset( md0, 0x5, maclen );
1307 memset( md1, 0x6, maclen );
1308
1309 CHK( mbedtls_md_setup( &t_out->md_ctx_enc, md_info, 1 ) == 0 );
1310 CHK( mbedtls_md_setup( &t_out->md_ctx_dec, md_info, 1 ) == 0 );
1311 CHK( mbedtls_md_setup( &t_in->md_ctx_enc, md_info, 1 ) == 0 );
1312 CHK( mbedtls_md_setup( &t_in->md_ctx_dec, md_info, 1 ) == 0 );
1313
1314 if( ver > MBEDTLS_SSL_MINOR_VERSION_0 )
1315 {
1316 CHK( mbedtls_md_hmac_starts( &t_in->md_ctx_enc,
1317 md0, maclen ) == 0 );
1318 CHK( mbedtls_md_hmac_starts( &t_in->md_ctx_dec,
1319 md1, maclen ) == 0 );
1320 CHK( mbedtls_md_hmac_starts( &t_out->md_ctx_enc,
1321 md1, maclen ) == 0 );
1322 CHK( mbedtls_md_hmac_starts( &t_out->md_ctx_dec,
1323 md0, maclen ) == 0 );
1324 }
1325#if defined(MBEDTLS_SSL_PROTO_SSL3)
1326 else
1327 {
1328 memcpy( &t_in->mac_enc, md0, maclen );
1329 memcpy( &t_in->mac_dec, md1, maclen );
1330 memcpy( &t_out->mac_enc, md1, maclen );
1331 memcpy( &t_out->mac_dec, md0, maclen );
1332 }
1333#endif
Hanno Beckera18d1322018-01-03 14:27:32 +00001334 }
1335#else
1336 ((void) hash_id);
1337#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1338
1339
1340 /* Pick IV's (regardless of whether they
1341 * are being used by the transform). */
1342 ivlen = cipher_info->iv_size;
1343 memset( iv_enc, 0x3, sizeof( iv_enc ) );
1344 memset( iv_dec, 0x4, sizeof( iv_dec ) );
1345
1346 /*
1347 * Setup transforms
1348 */
1349
Jaeden Amero2de07f12019-06-05 13:32:08 +01001350#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
1351 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Hanno Beckera18d1322018-01-03 14:27:32 +00001352 t_out->encrypt_then_mac = etm;
1353 t_in->encrypt_then_mac = etm;
1354#else
1355 ((void) etm);
1356#endif
1357
1358 t_out->minor_ver = ver;
1359 t_in->minor_ver = ver;
1360 t_out->ivlen = ivlen;
1361 t_in->ivlen = ivlen;
1362
1363 switch( cipher_info->mode )
1364 {
1365 case MBEDTLS_MODE_GCM:
1366 case MBEDTLS_MODE_CCM:
Hanno Beckere6832872020-05-28 08:29:58 +01001367#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
1368 if( ver == MBEDTLS_SSL_MINOR_VERSION_4 )
1369 {
1370 t_out->fixed_ivlen = 12;
1371 t_in->fixed_ivlen = 12;
1372 }
1373 else
1374#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
1375 {
1376 t_out->fixed_ivlen = 4;
1377 t_in->fixed_ivlen = 4;
1378 }
Hanno Beckera18d1322018-01-03 14:27:32 +00001379 t_out->maclen = 0;
1380 t_in->maclen = 0;
1381 switch( tag_mode )
1382 {
1383 case 0: /* Full tag */
1384 t_out->taglen = 16;
1385 t_in->taglen = 16;
1386 break;
1387 case 1: /* Partial tag */
1388 t_out->taglen = 8;
1389 t_in->taglen = 8;
1390 break;
1391 default:
Paul Elliottc7b53742021-02-03 13:18:33 +00001392 ret = 1;
1393 goto cleanup;
Hanno Beckera18d1322018-01-03 14:27:32 +00001394 }
1395 break;
1396
1397 case MBEDTLS_MODE_CHACHAPOLY:
1398 t_out->fixed_ivlen = 12;
1399 t_in->fixed_ivlen = 12;
1400 t_out->maclen = 0;
1401 t_in->maclen = 0;
1402 switch( tag_mode )
1403 {
1404 case 0: /* Full tag */
1405 t_out->taglen = 16;
1406 t_in->taglen = 16;
1407 break;
1408 case 1: /* Partial tag */
1409 t_out->taglen = 8;
1410 t_in->taglen = 8;
1411 break;
1412 default:
Paul Elliottc7b53742021-02-03 13:18:33 +00001413 ret = 1;
1414 goto cleanup;
Hanno Beckera18d1322018-01-03 14:27:32 +00001415 }
1416 break;
1417
1418 case MBEDTLS_MODE_STREAM:
1419 case MBEDTLS_MODE_CBC:
1420 t_out->fixed_ivlen = 0; /* redundant, must be 0 */
1421 t_in->fixed_ivlen = 0; /* redundant, must be 0 */
1422 t_out->taglen = 0;
1423 t_in->taglen = 0;
1424 switch( tag_mode )
1425 {
1426 case 0: /* Full tag */
1427 t_out->maclen = maclen;
1428 t_in->maclen = maclen;
1429 break;
1430 case 1: /* Partial tag */
1431 t_out->maclen = 10;
1432 t_in->maclen = 10;
1433 break;
1434 default:
Paul Elliottc7b53742021-02-03 13:18:33 +00001435 ret = 1;
1436 goto cleanup;
Hanno Beckera18d1322018-01-03 14:27:32 +00001437 }
1438 break;
1439 default:
Paul Elliottc7b53742021-02-03 13:18:33 +00001440 ret = 1;
1441 goto cleanup;
Hanno Beckera18d1322018-01-03 14:27:32 +00001442 break;
1443 }
1444
1445 /* Setup IV's */
1446
1447 memcpy( &t_in->iv_dec, iv_dec, sizeof( iv_dec ) );
1448 memcpy( &t_in->iv_enc, iv_enc, sizeof( iv_enc ) );
1449 memcpy( &t_out->iv_dec, iv_enc, sizeof( iv_enc ) );
1450 memcpy( &t_out->iv_enc, iv_dec, sizeof( iv_dec ) );
1451
Hanno Beckera0e20d02019-05-15 14:03:01 +01001452#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd856c822019-04-29 17:30:59 +01001453 /* Add CID */
1454 memcpy( &t_in->in_cid, cid0, cid0_len );
1455 memcpy( &t_in->out_cid, cid1, cid1_len );
1456 t_in->in_cid_len = cid0_len;
1457 t_in->out_cid_len = cid1_len;
1458 memcpy( &t_out->in_cid, cid1, cid1_len );
1459 memcpy( &t_out->out_cid, cid0, cid0_len );
1460 t_out->in_cid_len = cid1_len;
1461 t_out->out_cid_len = cid0_len;
Hanno Beckera0e20d02019-05-15 14:03:01 +01001462#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerd856c822019-04-29 17:30:59 +01001463
Hanno Becker81e16a32019-03-01 11:21:44 +00001464cleanup:
1465
Hanno Becker3ee54212019-04-04 16:31:26 +01001466 mbedtls_free( key0 );
1467 mbedtls_free( key1 );
Hanno Becker81e16a32019-03-01 11:21:44 +00001468
Paul Elliott6f1eda72020-06-11 20:22:00 +01001469 mbedtls_free( md0 );
1470 mbedtls_free( md1 );
1471
Hanno Beckera5780f12019-04-05 09:55:37 +01001472 return( ret );
Hanno Beckera18d1322018-01-03 14:27:32 +00001473}
1474
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001475/*
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02001476 * Populate a session structure for serialization tests.
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001477 * Choose dummy values, mostly non-0 to distinguish from the init default.
1478 */
1479static int ssl_populate_session( mbedtls_ssl_session *session,
Manuel Pégourié-Gonnard220403b2019-05-24 09:54:21 +02001480 int ticket_len,
1481 const char *crt_file )
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001482{
1483#if defined(MBEDTLS_HAVE_TIME)
1484 session->start = mbedtls_time( NULL ) - 42;
1485#endif
1486 session->ciphersuite = 0xabcd;
1487 session->compression = 1;
1488 session->id_len = sizeof( session->id );
1489 memset( session->id, 66, session->id_len );
Manuel Pégourié-Gonnard220403b2019-05-24 09:54:21 +02001490 memset( session->master, 17, sizeof( session->master ) );
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001491
Andrzej Kurek9155e7f2022-10-18 09:36:19 -04001492#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) && defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001493 if( strlen( crt_file ) != 0 )
1494 {
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02001495 mbedtls_x509_crt tmp_crt;
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001496 int ret;
Manuel Pégourié-Gonnard6b840702019-05-24 09:40:17 +02001497
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02001498 mbedtls_x509_crt_init( &tmp_crt );
1499 ret = mbedtls_x509_crt_parse_file( &tmp_crt, crt_file );
1500 if( ret != 0 )
1501 return( ret );
1502
1503#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1504 /* Move temporary CRT. */
Manuel Pégourié-Gonnard6b840702019-05-24 09:40:17 +02001505 session->peer_cert = mbedtls_calloc( 1, sizeof( *session->peer_cert ) );
1506 if( session->peer_cert == NULL )
1507 return( -1 );
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02001508 *session->peer_cert = tmp_crt;
1509 memset( &tmp_crt, 0, sizeof( tmp_crt ) );
1510#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
1511 /* Calculate digest of temporary CRT. */
1512 session->peer_cert_digest =
1513 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
1514 if( session->peer_cert_digest == NULL )
1515 return( -1 );
1516 ret = mbedtls_md( mbedtls_md_info_from_type(
1517 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
1518 tmp_crt.raw.p, tmp_crt.raw.len,
1519 session->peer_cert_digest );
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001520 if( ret != 0 )
1521 return( ret );
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02001522 session->peer_cert_digest_type =
1523 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
1524 session->peer_cert_digest_len =
1525 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
1526#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
1527
1528 mbedtls_x509_crt_free( &tmp_crt );
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001529 }
Andrzej Kurek9155e7f2022-10-18 09:36:19 -04001530#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED && MBEDTLS_FS_IO */
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001531 (void) crt_file;
Andrzej Kurek9155e7f2022-10-18 09:36:19 -04001532#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED && MBEDTLS_FS_IO */
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001533 session->verify_result = 0xdeadbeef;
1534
1535#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
1536 if( ticket_len != 0 )
1537 {
1538 session->ticket = mbedtls_calloc( 1, ticket_len );
Manuel Pégourié-Gonnard220403b2019-05-24 09:54:21 +02001539 if( session->ticket == NULL )
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001540 return( -1 );
1541 memset( session->ticket, 33, ticket_len );
1542 }
1543 session->ticket_len = ticket_len;
1544 session->ticket_lifetime = 86401;
1545#else
1546 (void) ticket_len;
1547#endif
1548
1549#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1550 session->mfl_code = 1;
1551#endif
1552#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1553 session->trunc_hmac = 1;
1554#endif
1555#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1556 session->encrypt_then_mac = 1;
1557#endif
1558
1559 return( 0 );
1560}
1561
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001562/*
1563 * Perform data exchanging between \p ssl_1 and \p ssl_2 and check if the
1564 * message was sent in the correct number of fragments.
1565 *
1566 * /p ssl_1 and /p ssl_2 Endpoints represented by mbedtls_ssl_context. Both
1567 * of them must be initialized and connected beforehand.
1568 * /p msg_len_1 and /p msg_len_2 specify the size of the message to send.
1569 * /p expected_fragments_1 and /p expected_fragments_2 determine in how many
1570 * fragments the message should be sent.
1571 * expected_fragments is 0: can be used for DTLS testing while the message
1572 * size is larger than MFL. In that case the message
1573 * cannot be fragmented and sent to the second endpoint.
1574 * This value can be used for negative tests.
1575 * expected_fragments is 1: can be used for TLS/DTLS testing while the
1576 * message size is below MFL
1577 * expected_fragments > 1: can be used for TLS testing while the message
1578 * size is larger than MFL
1579 *
1580 * \retval 0 on success, otherwise error code.
1581 */
1582int mbedtls_exchange_data( mbedtls_ssl_context *ssl_1,
1583 int msg_len_1, const int expected_fragments_1,
1584 mbedtls_ssl_context *ssl_2,
1585 int msg_len_2, const int expected_fragments_2 )
1586{
1587 unsigned char *msg_buf_1 = malloc( msg_len_1 );
1588 unsigned char *msg_buf_2 = malloc( msg_len_2 );
1589 unsigned char *in_buf_1 = malloc( msg_len_2 );
1590 unsigned char *in_buf_2 = malloc( msg_len_1 );
1591 int msg_type, ret = -1;
1592
1593 /* Perform this test with two message types. At first use a message
1594 * consisting of only 0x00 for the client and only 0xFF for the server.
1595 * At the second time use message with generated data */
1596 for( msg_type = 0; msg_type < 2; msg_type++ )
1597 {
1598 int written_1 = 0;
1599 int written_2 = 0;
1600 int read_1 = 0;
1601 int read_2 = 0;
1602 int fragments_1 = 0;
1603 int fragments_2 = 0;
1604
1605 if( msg_type == 0 )
1606 {
1607 memset( msg_buf_1, 0x00, msg_len_1 );
1608 memset( msg_buf_2, 0xff, msg_len_2 );
1609 }
1610 else
1611 {
1612 int i, j = 0;
1613 for( i = 0; i < msg_len_1; i++ )
1614 {
1615 msg_buf_1[i] = j++ & 0xFF;
1616 }
1617 for( i = 0; i < msg_len_2; i++ )
1618 {
1619 msg_buf_2[i] = ( j -= 5 ) & 0xFF;
1620 }
1621 }
1622
1623 while( read_1 < msg_len_2 || read_2 < msg_len_1 )
1624 {
1625 /* ssl_1 sending */
1626 if( msg_len_1 > written_1 )
1627 {
1628 ret = mbedtls_ssl_write_fragment( ssl_1, msg_buf_1,
1629 msg_len_1, &written_1,
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001630 expected_fragments_1 );
1631 if( expected_fragments_1 == 0 )
1632 {
1633 /* This error is expected when the message is too large and
1634 * cannot be fragmented */
1635 TEST_ASSERT( ret == MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1636 msg_len_1 = 0;
1637 }
1638 else
1639 {
1640 TEST_ASSERT( ret == 0 );
1641 }
1642 }
1643
1644 /* ssl_2 sending */
1645 if( msg_len_2 > written_2 )
1646 {
1647 ret = mbedtls_ssl_write_fragment( ssl_2, msg_buf_2,
1648 msg_len_2, &written_2,
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001649 expected_fragments_2 );
1650 if( expected_fragments_2 == 0 )
1651 {
1652 /* This error is expected when the message is too large and
1653 * cannot be fragmented */
1654 TEST_ASSERT( ret == MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1655 msg_len_2 = 0;
1656 }
1657 else
1658 {
1659 TEST_ASSERT( ret == 0 );
1660 }
1661 }
1662
1663 /* ssl_1 reading */
1664 if( read_1 < msg_len_2 )
1665 {
1666 ret = mbedtls_ssl_read_fragment( ssl_1, in_buf_1,
1667 msg_len_2, &read_1,
Piotr Nowicki438bf3b2020-03-10 12:59:10 +01001668 &fragments_2,
1669 expected_fragments_2 );
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001670 TEST_ASSERT( ret == 0 );
1671 }
1672
1673 /* ssl_2 reading */
1674 if( read_2 < msg_len_1 )
1675 {
1676 ret = mbedtls_ssl_read_fragment( ssl_2, in_buf_2,
1677 msg_len_1, &read_2,
Piotr Nowicki438bf3b2020-03-10 12:59:10 +01001678 &fragments_1,
1679 expected_fragments_1 );
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001680 TEST_ASSERT( ret == 0 );
1681 }
1682 }
1683
1684 ret = -1;
1685 TEST_ASSERT( 0 == memcmp( msg_buf_1, in_buf_2, msg_len_1 ) );
1686 TEST_ASSERT( 0 == memcmp( msg_buf_2, in_buf_1, msg_len_2 ) );
1687 TEST_ASSERT( fragments_1 == expected_fragments_1 );
1688 TEST_ASSERT( fragments_2 == expected_fragments_2 );
1689 }
1690
1691 ret = 0;
1692
1693exit:
1694 free( msg_buf_1 );
1695 free( in_buf_1 );
1696 free( msg_buf_2 );
1697 free( in_buf_2 );
1698
1699 return ret;
1700}
1701
Piotr Nowicki95e9eb82020-02-14 11:33:34 +01001702/*
1703 * Perform data exchanging between \p ssl_1 and \p ssl_2. Both of endpoints
1704 * must be initialized and connected beforehand.
1705 *
1706 * \retval 0 on success, otherwise error code.
1707 */
1708int exchange_data( mbedtls_ssl_context *ssl_1,
1709 mbedtls_ssl_context *ssl_2 )
1710{
1711 return mbedtls_exchange_data( ssl_1, 256, 1,
1712 ssl_2, 256, 1 );
1713}
1714
Andrzej Kurek9155e7f2022-10-18 09:36:19 -04001715#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) && \
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02001716 defined(MBEDTLS_ENTROPY_C) && \
1717 defined(MBEDTLS_CTR_DRBG_C)
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001718void perform_handshake( handshake_test_options* options )
1719{
1720 /* forced_ciphersuite needs to last until the end of the handshake */
1721 int forced_ciphersuite[2];
1722 enum { BUFFSIZE = 17000 };
1723 mbedtls_endpoint client, server;
Gilles Peskineeccd8882020-03-10 12:19:08 +01001724#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001725 const char *psk_identity = "foo";
1726#endif
1727#if defined(MBEDTLS_TIMING_C)
1728 mbedtls_timing_delay_context timer_client, timer_server;
1729#endif
1730#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1731 unsigned char *context_buf = NULL;
1732 size_t context_buf_len;
1733#endif
1734#if defined(MBEDTLS_SSL_RENEGOTIATION)
1735 int ret = -1;
1736#endif
Paul Elliottc8570442020-04-15 17:00:50 +01001737 int expected_handshake_result = 0;
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001738
Andrzej Kurek0d2982b2022-10-18 07:55:46 -04001739 USE_PSA_INIT( );
1740 mbedtls_platform_zeroize( &client, sizeof(client) );
1741 mbedtls_platform_zeroize( &server, sizeof(server) );
1742
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001743 mbedtls_test_message_queue server_queue, client_queue;
1744 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05001745 mbedtls_message_socket_init( &server_context );
1746 mbedtls_message_socket_init( &client_context );
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001747
1748 /* Client side */
1749 if( options->dtls != 0 )
1750 {
1751 TEST_ASSERT( mbedtls_endpoint_init( &client, MBEDTLS_SSL_IS_CLIENT,
1752 options->pk_alg, &client_context,
1753 &client_queue,
Andrzej Kurek535cd172022-03-08 06:50:12 -05001754 &server_queue, NULL ) == 0 );
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001755#if defined(MBEDTLS_TIMING_C)
1756 mbedtls_ssl_set_timer_cb( &client.ssl, &timer_client,
1757 mbedtls_timing_set_delay,
1758 mbedtls_timing_get_delay );
1759#endif
1760 }
1761 else
1762 {
1763 TEST_ASSERT( mbedtls_endpoint_init( &client, MBEDTLS_SSL_IS_CLIENT,
1764 options->pk_alg, NULL, NULL,
Andrzej Kurek535cd172022-03-08 06:50:12 -05001765 NULL, NULL ) == 0 );
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001766 }
Paul Elliottc8570442020-04-15 17:00:50 +01001767
1768 if( options->client_min_version != TEST_SSL_MINOR_VERSION_NONE )
1769 {
1770 mbedtls_ssl_conf_min_version( &client.conf, MBEDTLS_SSL_MAJOR_VERSION_3,
1771 options->client_min_version );
1772 }
1773
1774 if( options->client_max_version != TEST_SSL_MINOR_VERSION_NONE )
1775 {
1776 mbedtls_ssl_conf_max_version( &client.conf, MBEDTLS_SSL_MAJOR_VERSION_3,
1777 options->client_max_version );
1778 }
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001779
1780 if( strlen( options->cipher ) > 0 )
1781 {
1782 set_ciphersuite( &client.conf, options->cipher, forced_ciphersuite );
1783 }
Piotr Nowickibde7ee82020-02-21 10:59:50 +01001784
1785#if defined (MBEDTLS_DEBUG_C)
1786 if( options->cli_log_fun )
1787 {
1788 mbedtls_debug_set_threshold( 4 );
1789 mbedtls_ssl_conf_dbg( &client.conf, options->cli_log_fun,
1790 options->cli_log_obj );
1791 }
1792#endif
1793
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001794 /* Server side */
1795 if( options->dtls != 0 )
1796 {
1797 TEST_ASSERT( mbedtls_endpoint_init( &server, MBEDTLS_SSL_IS_SERVER,
1798 options->pk_alg, &server_context,
1799 &server_queue,
Andrzej Kurek535cd172022-03-08 06:50:12 -05001800 &client_queue, NULL ) == 0 );
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001801#if defined(MBEDTLS_TIMING_C)
1802 mbedtls_ssl_set_timer_cb( &server.ssl, &timer_server,
1803 mbedtls_timing_set_delay,
1804 mbedtls_timing_get_delay );
1805#endif
1806 }
1807 else
1808 {
1809 TEST_ASSERT( mbedtls_endpoint_init( &server, MBEDTLS_SSL_IS_SERVER,
Andrzej Kurek535cd172022-03-08 06:50:12 -05001810 options->pk_alg, NULL, NULL,
1811 NULL, NULL ) == 0 );
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001812 }
Piotr Nowickibde7ee82020-02-21 10:59:50 +01001813
1814 mbedtls_ssl_conf_authmode( &server.conf, options->srv_auth_mode );
1815
Paul Elliottc8570442020-04-15 17:00:50 +01001816 if( options->server_min_version != TEST_SSL_MINOR_VERSION_NONE )
1817 {
1818 mbedtls_ssl_conf_min_version( &server.conf, MBEDTLS_SSL_MAJOR_VERSION_3,
1819 options->server_min_version );
1820 }
1821
1822 if( options->server_max_version != TEST_SSL_MINOR_VERSION_NONE )
1823 {
1824 mbedtls_ssl_conf_max_version( &server.conf, MBEDTLS_SSL_MAJOR_VERSION_3,
1825 options->server_max_version );
1826 }
1827
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001828#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1829 TEST_ASSERT( mbedtls_ssl_conf_max_frag_len( &(server.conf),
1830 (unsigned char) options->mfl ) == 0 );
1831 TEST_ASSERT( mbedtls_ssl_conf_max_frag_len( &(client.conf),
1832 (unsigned char) options->mfl ) == 0 );
1833#else
1834 TEST_ASSERT( MBEDTLS_SSL_MAX_FRAG_LEN_NONE == options->mfl );
1835#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
1836
Gilles Peskineeccd8882020-03-10 12:19:08 +01001837#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001838 if( options->psk_str != NULL && options->psk_str->len > 0 )
1839 {
1840 TEST_ASSERT( mbedtls_ssl_conf_psk( &client.conf, options->psk_str->x,
1841 options->psk_str->len,
1842 (const unsigned char *) psk_identity,
1843 strlen( psk_identity ) ) == 0 );
1844
1845 TEST_ASSERT( mbedtls_ssl_conf_psk( &server.conf, options->psk_str->x,
1846 options->psk_str->len,
1847 (const unsigned char *) psk_identity,
1848 strlen( psk_identity ) ) == 0 );
1849
1850 mbedtls_ssl_conf_psk_cb( &server.conf, psk_dummy_callback, NULL );
1851 }
1852#endif
1853#if defined(MBEDTLS_SSL_RENEGOTIATION)
1854 if( options->renegotiate )
1855 {
1856 mbedtls_ssl_conf_renegotiation( &(server.conf),
1857 MBEDTLS_SSL_RENEGOTIATION_ENABLED );
1858 mbedtls_ssl_conf_renegotiation( &(client.conf),
1859 MBEDTLS_SSL_RENEGOTIATION_ENABLED );
1860
1861 mbedtls_ssl_conf_legacy_renegotiation( &(server.conf),
1862 options->legacy_renegotiation );
1863 mbedtls_ssl_conf_legacy_renegotiation( &(client.conf),
1864 options->legacy_renegotiation );
1865 }
1866#endif /* MBEDTLS_SSL_RENEGOTIATION */
1867
Piotr Nowickibde7ee82020-02-21 10:59:50 +01001868#if defined (MBEDTLS_DEBUG_C)
1869 if( options->srv_log_fun )
1870 {
1871 mbedtls_debug_set_threshold( 4 );
1872 mbedtls_ssl_conf_dbg( &server.conf, options->srv_log_fun,
1873 options->srv_log_obj );
1874 }
1875#endif
1876
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001877 TEST_ASSERT( mbedtls_mock_socket_connect( &(client.socket),
1878 &(server.socket),
1879 BUFFSIZE ) == 0 );
1880
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001881#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1882 if( options->resize_buffers != 0 )
1883 {
1884 /* Ensure that the buffer sizes are appropriate before resizes */
1885 TEST_ASSERT( client.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN );
1886 TEST_ASSERT( client.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN );
1887 TEST_ASSERT( server.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN );
1888 TEST_ASSERT( server.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN );
1889 }
1890#endif
1891
Paul Elliottc8570442020-04-15 17:00:50 +01001892 if( options->expected_negotiated_version == TEST_SSL_MINOR_VERSION_NONE )
1893 {
1894 expected_handshake_result = MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION;
1895 }
1896
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001897 TEST_ASSERT( mbedtls_move_handshake_to_state( &(client.ssl),
1898 &(server.ssl),
1899 MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Elliottc8570442020-04-15 17:00:50 +01001900 == expected_handshake_result );
1901
1902 if( expected_handshake_result != 0 )
1903 {
1904 /* Connection will have failed by this point, skip to cleanup */
1905 goto exit;
1906 }
1907
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001908 TEST_ASSERT( client.ssl.state == MBEDTLS_SSL_HANDSHAKE_OVER );
1909 TEST_ASSERT( server.ssl.state == MBEDTLS_SSL_HANDSHAKE_OVER );
1910
Paul Elliottc8570442020-04-15 17:00:50 +01001911 /* Check that we agree on the version... */
1912 TEST_ASSERT( client.ssl.minor_ver == server.ssl.minor_ver );
1913
1914 /* And check that the version negotiated is the expected one. */
1915 TEST_EQUAL( client.ssl.minor_ver, options->expected_negotiated_version );
1916
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001917#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1918 if( options->resize_buffers != 0 )
1919 {
Paul Elliottc8570442020-04-15 17:00:50 +01001920 if( options->expected_negotiated_version != MBEDTLS_SSL_MINOR_VERSION_0 &&
1921 options->expected_negotiated_version != MBEDTLS_SSL_MINOR_VERSION_1 )
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001922 {
1923 /* A server, when using DTLS, might delay a buffer resize to happen
1924 * after it receives a message, so we force it. */
1925 TEST_ASSERT( exchange_data( &(client.ssl), &(server.ssl) ) == 0 );
1926
1927 TEST_ASSERT( client.ssl.out_buf_len ==
1928 mbedtls_ssl_get_output_buflen( &client.ssl ) );
1929 TEST_ASSERT( client.ssl.in_buf_len ==
1930 mbedtls_ssl_get_input_buflen( &client.ssl ) );
1931 TEST_ASSERT( server.ssl.out_buf_len ==
1932 mbedtls_ssl_get_output_buflen( &server.ssl ) );
1933 TEST_ASSERT( server.ssl.in_buf_len ==
1934 mbedtls_ssl_get_input_buflen( &server.ssl ) );
1935 }
1936 }
1937#endif
1938
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001939 if( options->cli_msg_len != 0 || options->srv_msg_len != 0 )
1940 {
1941 /* Start data exchanging test */
1942 TEST_ASSERT( mbedtls_exchange_data( &(client.ssl), options->cli_msg_len,
1943 options->expected_cli_fragments,
1944 &(server.ssl), options->srv_msg_len,
1945 options->expected_srv_fragments )
1946 == 0 );
1947 }
1948#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1949 if( options->serialize == 1 )
1950 {
1951 TEST_ASSERT( options->dtls == 1 );
1952
1953 TEST_ASSERT( mbedtls_ssl_context_save( &(server.ssl), NULL,
1954 0, &context_buf_len )
1955 == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1956
1957 context_buf = mbedtls_calloc( 1, context_buf_len );
1958 TEST_ASSERT( context_buf != NULL );
1959
1960 TEST_ASSERT( mbedtls_ssl_context_save( &(server.ssl), context_buf,
1961 context_buf_len,
1962 &context_buf_len ) == 0 );
1963
1964 mbedtls_ssl_free( &(server.ssl) );
1965 mbedtls_ssl_init( &(server.ssl) );
1966
1967 TEST_ASSERT( mbedtls_ssl_setup( &(server.ssl), &(server.conf) ) == 0 );
1968
1969 mbedtls_ssl_set_bio( &( server.ssl ), &server_context,
1970 mbedtls_mock_tcp_send_msg,
1971 mbedtls_mock_tcp_recv_msg,
1972 NULL );
1973
1974#if defined(MBEDTLS_TIMING_C)
1975 mbedtls_ssl_set_timer_cb( &server.ssl, &timer_server,
1976 mbedtls_timing_set_delay,
1977 mbedtls_timing_get_delay );
1978#endif
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001979#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1980 if( options->resize_buffers != 0 )
1981 {
1982 /* Ensure that the buffer sizes are appropriate before resizes */
1983 TEST_ASSERT( server.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN );
1984 TEST_ASSERT( server.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN );
1985 }
1986#endif
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001987 TEST_ASSERT( mbedtls_ssl_context_load( &( server.ssl ), context_buf,
1988 context_buf_len ) == 0 );
1989
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001990#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1991 /* Validate buffer sizes after context deserialization */
1992 if( options->resize_buffers != 0 )
1993 {
1994 TEST_ASSERT( server.ssl.out_buf_len ==
1995 mbedtls_ssl_get_output_buflen( &server.ssl ) );
1996 TEST_ASSERT( server.ssl.in_buf_len ==
1997 mbedtls_ssl_get_input_buflen( &server.ssl ) );
1998 }
1999#endif
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002000 /* Retest writing/reading */
2001 if( options->cli_msg_len != 0 || options->srv_msg_len != 0 )
2002 {
2003 TEST_ASSERT( mbedtls_exchange_data( &(client.ssl),
2004 options->cli_msg_len,
2005 options->expected_cli_fragments,
2006 &(server.ssl),
2007 options->srv_msg_len,
2008 options->expected_srv_fragments )
2009 == 0 );
2010 }
2011 }
2012#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05002013
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002014#if defined(MBEDTLS_SSL_RENEGOTIATION)
2015 if( options->renegotiate )
2016 {
2017 /* Start test with renegotiation */
2018 TEST_ASSERT( server.ssl.renego_status ==
2019 MBEDTLS_SSL_INITIAL_HANDSHAKE );
2020 TEST_ASSERT( client.ssl.renego_status ==
2021 MBEDTLS_SSL_INITIAL_HANDSHAKE );
2022
2023 /* After calling this function for the server, it only sends a handshake
2024 * request. All renegotiation should happen during data exchanging */
2025 TEST_ASSERT( mbedtls_ssl_renegotiate( &(server.ssl) ) == 0 );
2026 TEST_ASSERT( server.ssl.renego_status ==
2027 MBEDTLS_SSL_RENEGOTIATION_PENDING );
2028 TEST_ASSERT( client.ssl.renego_status ==
2029 MBEDTLS_SSL_INITIAL_HANDSHAKE );
2030
2031 TEST_ASSERT( exchange_data( &(client.ssl), &(server.ssl) ) == 0 );
2032 TEST_ASSERT( server.ssl.renego_status ==
2033 MBEDTLS_SSL_RENEGOTIATION_DONE );
2034 TEST_ASSERT( client.ssl.renego_status ==
2035 MBEDTLS_SSL_RENEGOTIATION_DONE );
2036
2037 /* After calling mbedtls_ssl_renegotiate for the client all renegotiation
2038 * should happen inside this function. However in this test, we cannot
Shaun Case0e7791f2021-12-20 21:14:10 -08002039 * perform simultaneous communication between client and server so this
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002040 * function will return waiting error on the socket. All rest of
2041 * renegotiation should happen during data exchanging */
2042 ret = mbedtls_ssl_renegotiate( &(client.ssl) );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05002043#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2044 if( options->resize_buffers != 0 )
2045 {
2046 /* Ensure that the buffer sizes are appropriate before resizes */
2047 TEST_ASSERT( client.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN );
2048 TEST_ASSERT( client.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN );
2049 }
2050#endif
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002051 TEST_ASSERT( ret == 0 ||
2052 ret == MBEDTLS_ERR_SSL_WANT_READ ||
2053 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
2054 TEST_ASSERT( server.ssl.renego_status ==
2055 MBEDTLS_SSL_RENEGOTIATION_DONE );
2056 TEST_ASSERT( client.ssl.renego_status ==
2057 MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS );
2058
2059 TEST_ASSERT( exchange_data( &(client.ssl), &(server.ssl) ) == 0 );
2060 TEST_ASSERT( server.ssl.renego_status ==
2061 MBEDTLS_SSL_RENEGOTIATION_DONE );
2062 TEST_ASSERT( client.ssl.renego_status ==
2063 MBEDTLS_SSL_RENEGOTIATION_DONE );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05002064#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2065 /* Validate buffer sizes after renegotiation */
2066 if( options->resize_buffers != 0 )
2067 {
2068 TEST_ASSERT( client.ssl.out_buf_len ==
2069 mbedtls_ssl_get_output_buflen( &client.ssl ) );
2070 TEST_ASSERT( client.ssl.in_buf_len ==
2071 mbedtls_ssl_get_input_buflen( &client.ssl ) );
2072 TEST_ASSERT( server.ssl.out_buf_len ==
2073 mbedtls_ssl_get_output_buflen( &server.ssl ) );
2074 TEST_ASSERT( server.ssl.in_buf_len ==
2075 mbedtls_ssl_get_input_buflen( &server.ssl ) );
2076 }
2077#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002078 }
2079#endif /* MBEDTLS_SSL_RENEGOTIATION */
2080
2081exit:
2082 mbedtls_endpoint_free( &client, options->dtls != 0 ? &client_context : NULL );
2083 mbedtls_endpoint_free( &server, options->dtls != 0 ? &server_context : NULL );
Piotr Nowickibde7ee82020-02-21 10:59:50 +01002084#if defined (MBEDTLS_DEBUG_C)
2085 if( options->cli_log_fun || options->srv_log_fun )
2086 {
2087 mbedtls_debug_set_threshold( 0 );
2088 }
2089#endif
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002090#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
2091 if( context_buf != NULL )
2092 mbedtls_free( context_buf );
2093#endif
2094}
Andrzej Kurek9155e7f2022-10-18 09:36:19 -04002095#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002096
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002097/* END_HEADER */
2098
2099/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002100 * depends_on:MBEDTLS_SSL_TLS_C
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002101 * END_DEPENDENCIES
2102 */
2103
Janos Follath6264e662019-11-26 11:11:15 +00002104/* BEGIN_CASE */
2105void test_callback_buffer_sanity()
2106{
2107 enum { MSGLEN = 10 };
2108 mbedtls_test_buffer buf;
2109 unsigned char input[MSGLEN];
2110 unsigned char output[MSGLEN];
2111
2112 memset( input, 0, sizeof(input) );
2113
2114 /* Make sure calling put and get on NULL buffer results in error. */
2115 TEST_ASSERT( mbedtls_test_buffer_put( NULL, input, sizeof( input ) )
2116 == -1 );
2117 TEST_ASSERT( mbedtls_test_buffer_get( NULL, output, sizeof( output ) )
2118 == -1 );
2119 TEST_ASSERT( mbedtls_test_buffer_put( NULL, NULL, sizeof( input ) ) == -1 );
Andrzej Kurekf7774142020-01-22 06:34:59 -05002120
Janos Follath6264e662019-11-26 11:11:15 +00002121 TEST_ASSERT( mbedtls_test_buffer_put( NULL, NULL, 0 ) == -1 );
2122 TEST_ASSERT( mbedtls_test_buffer_get( NULL, NULL, 0 ) == -1 );
2123
2124 /* Make sure calling put and get on a buffer that hasn't been set up results
Shaun Case0e7791f2021-12-20 21:14:10 -08002125 * in error. */
Janos Follath6264e662019-11-26 11:11:15 +00002126 mbedtls_test_buffer_init( &buf );
2127
2128 TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, sizeof( input ) ) == -1 );
2129 TEST_ASSERT( mbedtls_test_buffer_get( &buf, output, sizeof( output ) )
2130 == -1 );
2131 TEST_ASSERT( mbedtls_test_buffer_put( &buf, NULL, sizeof( input ) ) == -1 );
Andrzej Kurekf7774142020-01-22 06:34:59 -05002132
Janos Follath6264e662019-11-26 11:11:15 +00002133 TEST_ASSERT( mbedtls_test_buffer_put( &buf, NULL, 0 ) == -1 );
2134 TEST_ASSERT( mbedtls_test_buffer_get( &buf, NULL, 0 ) == -1 );
2135
Andrzej Kurekf7774142020-01-22 06:34:59 -05002136 /* Make sure calling put and get on NULL input only results in
2137 * error if the length is not zero, and that a NULL output is valid for data
2138 * dropping.
2139 */
Janos Follath6264e662019-11-26 11:11:15 +00002140
2141 TEST_ASSERT( mbedtls_test_buffer_setup( &buf, sizeof( input ) ) == 0 );
2142
2143 TEST_ASSERT( mbedtls_test_buffer_put( &buf, NULL, sizeof( input ) ) == -1 );
2144 TEST_ASSERT( mbedtls_test_buffer_get( &buf, NULL, sizeof( output ) )
Andrzej Kurekf7774142020-01-22 06:34:59 -05002145 == 0 );
Janos Follath6264e662019-11-26 11:11:15 +00002146 TEST_ASSERT( mbedtls_test_buffer_put( &buf, NULL, 0 ) == 0 );
2147 TEST_ASSERT( mbedtls_test_buffer_get( &buf, NULL, 0 ) == 0 );
2148
Piotr Nowickifb437d72020-01-13 16:59:12 +01002149 /* Make sure calling put several times in the row is safe */
2150
2151 TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, sizeof( input ) )
2152 == sizeof( input ) );
2153 TEST_ASSERT( mbedtls_test_buffer_get( &buf, output, 2 ) == 2 );
2154 TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, 1 ) == 1 );
2155 TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, 2 ) == 1 );
2156 TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, 2 ) == 0 );
2157
2158
Janos Follath6264e662019-11-26 11:11:15 +00002159exit:
2160
2161 mbedtls_test_buffer_free( &buf );
2162}
2163/* END_CASE */
2164
2165/*
2166 * Test if the implementation of `mbedtls_test_buffer` related functions is
2167 * correct and works as expected.
2168 *
2169 * That is
2170 * - If we try to put in \p put1 bytes then we can put in \p put1_ret bytes.
2171 * - Afterwards if we try to get \p get1 bytes then we can get \get1_ret bytes.
2172 * - Next, if we try to put in \p put1 bytes then we can put in \p put1_ret
2173 * bytes.
2174 * - Afterwards if we try to get \p get1 bytes then we can get \get1_ret bytes.
2175 * - All of the bytes we got match the bytes we put in in a FIFO manner.
2176 */
2177
2178/* BEGIN_CASE */
2179void test_callback_buffer( int size, int put1, int put1_ret,
2180 int get1, int get1_ret, int put2, int put2_ret,
2181 int get2, int get2_ret )
2182{
2183 enum { ROUNDS = 2 };
2184 size_t put[ROUNDS];
2185 int put_ret[ROUNDS];
2186 size_t get[ROUNDS];
2187 int get_ret[ROUNDS];
2188 mbedtls_test_buffer buf;
2189 unsigned char* input = NULL;
2190 size_t input_len;
2191 unsigned char* output = NULL;
2192 size_t output_len;
Janos Follath031827f2019-11-27 11:12:14 +00002193 size_t i, j, written, read;
Janos Follath6264e662019-11-26 11:11:15 +00002194
2195 mbedtls_test_buffer_init( &buf );
2196 TEST_ASSERT( mbedtls_test_buffer_setup( &buf, size ) == 0 );
2197
2198 /* Check the sanity of input parameters and initialise local variables. That
2199 * is, ensure that the amount of data is not negative and that we are not
2200 * expecting more to put or get than we actually asked for. */
2201 TEST_ASSERT( put1 >= 0 );
2202 put[0] = put1;
2203 put_ret[0] = put1_ret;
2204 TEST_ASSERT( put1_ret <= put1 );
2205 TEST_ASSERT( put2 >= 0 );
2206 put[1] = put2;
2207 put_ret[1] = put2_ret;
2208 TEST_ASSERT( put2_ret <= put2 );
2209
2210 TEST_ASSERT( get1 >= 0 );
2211 get[0] = get1;
2212 get_ret[0] = get1_ret;
2213 TEST_ASSERT( get1_ret <= get1 );
2214 TEST_ASSERT( get2 >= 0 );
2215 get[1] = get2;
2216 get_ret[1] = get2_ret;
2217 TEST_ASSERT( get2_ret <= get2 );
2218
2219 input_len = 0;
2220 /* Calculate actual input and output lengths */
2221 for( j = 0; j < ROUNDS; j++ )
2222 {
2223 if( put_ret[j] > 0 )
2224 {
2225 input_len += put_ret[j];
2226 }
2227 }
2228 /* In order to always have a valid pointer we always allocate at least 1
2229 * byte. */
2230 if( input_len == 0 )
2231 input_len = 1;
2232 ASSERT_ALLOC( input, input_len );
2233
2234 output_len = 0;
2235 for( j = 0; j < ROUNDS; j++ )
2236 {
2237 if( get_ret[j] > 0 )
2238 {
2239 output_len += get_ret[j];
2240 }
2241 }
2242 TEST_ASSERT( output_len <= input_len );
2243 /* In order to always have a valid pointer we always allocate at least 1
2244 * byte. */
2245 if( output_len == 0 )
2246 output_len = 1;
2247 ASSERT_ALLOC( output, output_len );
2248
2249 /* Fill up the buffer with structured data so that unwanted changes
2250 * can be detected */
2251 for( i = 0; i < input_len; i++ )
2252 {
2253 input[i] = i & 0xFF;
2254 }
2255
2256 written = read = 0;
2257 for( j = 0; j < ROUNDS; j++ )
2258 {
2259 TEST_ASSERT( put_ret[j] == mbedtls_test_buffer_put( &buf,
2260 input + written, put[j] ) );
2261 written += put_ret[j];
2262 TEST_ASSERT( get_ret[j] == mbedtls_test_buffer_get( &buf,
2263 output + read, get[j] ) );
2264 read += get_ret[j];
2265 TEST_ASSERT( read <= written );
2266 if( get_ret[j] > 0 )
2267 {
2268 TEST_ASSERT( memcmp( output + read - get_ret[j],
2269 input + read - get_ret[j], get_ret[j] )
2270 == 0 );
2271 }
2272 }
2273
2274exit:
2275
2276 mbedtls_free( input );
2277 mbedtls_free( output );
2278 mbedtls_test_buffer_free( &buf );
2279}
2280/* END_CASE */
2281
Janos Follath031827f2019-11-27 11:12:14 +00002282/*
Janos Follathc673c2c2019-12-02 15:47:26 +00002283 * Test if the implementation of `mbedtls_mock_socket` related I/O functions is
2284 * correct and works as expected on unconnected sockets.
2285 */
2286
2287/* BEGIN_CASE */
2288void ssl_mock_sanity( )
2289{
2290 enum { MSGLEN = 105 };
Paul Elliott95457862021-11-24 16:54:26 +00002291 unsigned char message[MSGLEN] = { 0 };
2292 unsigned char received[MSGLEN] = { 0 };
Janos Follathc673c2c2019-12-02 15:47:26 +00002293 mbedtls_mock_socket socket;
2294
2295 mbedtls_mock_socket_init( &socket );
2296 TEST_ASSERT( mbedtls_mock_tcp_send_b( &socket, message, MSGLEN ) < 0 );
2297 mbedtls_mock_socket_close( &socket );
2298 mbedtls_mock_socket_init( &socket );
2299 TEST_ASSERT( mbedtls_mock_tcp_recv_b( &socket, received, MSGLEN ) < 0 );
2300 mbedtls_mock_socket_close( &socket );
2301
2302 mbedtls_mock_socket_init( &socket );
2303 TEST_ASSERT( mbedtls_mock_tcp_send_nb( &socket, message, MSGLEN ) < 0 );
2304 mbedtls_mock_socket_close( &socket );
2305 mbedtls_mock_socket_init( &socket );
2306 TEST_ASSERT( mbedtls_mock_tcp_recv_nb( &socket, received, MSGLEN ) < 0 );
2307 mbedtls_mock_socket_close( &socket );
2308
2309exit:
2310
2311 mbedtls_mock_socket_close( &socket );
2312}
2313/* END_CASE */
2314
2315/*
2316 * Test if the implementation of `mbedtls_mock_socket` related functions can
2317 * send a single message from the client to the server.
Janos Follath031827f2019-11-27 11:12:14 +00002318 */
2319
2320/* BEGIN_CASE */
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002321void ssl_mock_tcp( int blocking )
Janos Follath031827f2019-11-27 11:12:14 +00002322{
Janos Follathc673c2c2019-12-02 15:47:26 +00002323 enum { MSGLEN = 105 };
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002324 enum { BUFLEN = MSGLEN / 5 };
Janos Follathc673c2c2019-12-02 15:47:26 +00002325 unsigned char message[MSGLEN];
2326 unsigned char received[MSGLEN];
2327 mbedtls_mock_socket client;
2328 mbedtls_mock_socket server;
2329 size_t written, read;
2330 int send_ret, recv_ret;
2331 mbedtls_ssl_send_t *send;
2332 mbedtls_ssl_recv_t *recv;
Janos Follathc673c2c2019-12-02 15:47:26 +00002333 unsigned i;
2334
2335 if( blocking == 0 )
2336 {
2337 send = mbedtls_mock_tcp_send_nb;
2338 recv = mbedtls_mock_tcp_recv_nb;
2339 }
2340 else
2341 {
2342 send = mbedtls_mock_tcp_send_b;
2343 recv = mbedtls_mock_tcp_recv_b;
2344 }
2345
2346 mbedtls_mock_socket_init( &client );
2347 mbedtls_mock_socket_init( &server );
2348
2349 /* Fill up the buffer with structured data so that unwanted changes
2350 * can be detected */
2351 for( i = 0; i < MSGLEN; i++ )
2352 {
2353 message[i] = i & 0xFF;
2354 }
2355
2356 /* Make sure that sending a message takes a few iterations. */
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002357 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, BUFLEN ) );
Janos Follathc673c2c2019-12-02 15:47:26 +00002358
2359 /* Send the message to the server */
2360 send_ret = recv_ret = 1;
2361 written = read = 0;
2362 while( send_ret != 0 || recv_ret != 0 )
2363 {
2364 send_ret = send( &client, message + written, MSGLEN - written );
2365
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002366 TEST_ASSERT( send_ret >= 0 );
2367 TEST_ASSERT( send_ret <= BUFLEN );
2368 written += send_ret;
2369
2370 /* If the buffer is full we can test blocking and non-blocking send */
2371 if ( send_ret == BUFLEN )
Janos Follathc673c2c2019-12-02 15:47:26 +00002372 {
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002373 int blocking_ret = send( &client, message , 1 );
2374 if ( blocking )
2375 {
2376 TEST_ASSERT( blocking_ret == 0 );
2377 }
2378 else
2379 {
2380 TEST_ASSERT( blocking_ret == MBEDTLS_ERR_SSL_WANT_WRITE );
2381 }
Janos Follathc673c2c2019-12-02 15:47:26 +00002382 }
Janos Follathc673c2c2019-12-02 15:47:26 +00002383
2384 recv_ret = recv( &server, received + read, MSGLEN - read );
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002385
2386 /* The result depends on whether any data was sent */
2387 if ( send_ret > 0 )
Janos Follathc673c2c2019-12-02 15:47:26 +00002388 {
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002389 TEST_ASSERT( recv_ret > 0 );
2390 TEST_ASSERT( recv_ret <= BUFLEN );
2391 read += recv_ret;
2392 }
2393 else if( blocking )
2394 {
2395 TEST_ASSERT( recv_ret == 0 );
Janos Follathc673c2c2019-12-02 15:47:26 +00002396 }
2397 else
2398 {
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002399 TEST_ASSERT( recv_ret == MBEDTLS_ERR_SSL_WANT_READ );
2400 recv_ret = 0;
Janos Follathc673c2c2019-12-02 15:47:26 +00002401 }
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002402
2403 /* If the buffer is empty we can test blocking and non-blocking read */
2404 if ( recv_ret == BUFLEN )
2405 {
2406 int blocking_ret = recv( &server, received, 1 );
2407 if ( blocking )
2408 {
2409 TEST_ASSERT( blocking_ret == 0 );
2410 }
2411 else
2412 {
2413 TEST_ASSERT( blocking_ret == MBEDTLS_ERR_SSL_WANT_READ );
2414 }
2415 }
Janos Follathc673c2c2019-12-02 15:47:26 +00002416 }
2417 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
2418
2419exit:
2420
2421 mbedtls_mock_socket_close( &client );
2422 mbedtls_mock_socket_close( &server );
2423}
2424/* END_CASE */
2425
2426/*
2427 * Test if the implementation of `mbedtls_mock_socket` related functions can
2428 * send messages in both direction at the same time (with the I/O calls
2429 * interleaving).
2430 */
2431
2432/* BEGIN_CASE */
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002433void ssl_mock_tcp_interleaving( int blocking )
Janos Follathc673c2c2019-12-02 15:47:26 +00002434{
Janos Follath031827f2019-11-27 11:12:14 +00002435 enum { ROUNDS = 2 };
2436 enum { MSGLEN = 105 };
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002437 enum { BUFLEN = MSGLEN / 5 };
Janos Follath031827f2019-11-27 11:12:14 +00002438 unsigned char message[ROUNDS][MSGLEN];
2439 unsigned char received[ROUNDS][MSGLEN];
2440 mbedtls_mock_socket client;
2441 mbedtls_mock_socket server;
2442 size_t written[ROUNDS];
2443 size_t read[ROUNDS];
2444 int send_ret[ROUNDS];
2445 int recv_ret[ROUNDS];
2446 unsigned i, j, progress;
Janos Follath3766ba52019-11-27 13:31:42 +00002447 mbedtls_ssl_send_t *send;
2448 mbedtls_ssl_recv_t *recv;
Janos Follath3766ba52019-11-27 13:31:42 +00002449
2450 if( blocking == 0 )
2451 {
2452 send = mbedtls_mock_tcp_send_nb;
2453 recv = mbedtls_mock_tcp_recv_nb;
2454 }
2455 else
2456 {
2457 send = mbedtls_mock_tcp_send_b;
2458 recv = mbedtls_mock_tcp_recv_b;
2459 }
Janos Follath031827f2019-11-27 11:12:14 +00002460
2461 mbedtls_mock_socket_init( &client );
2462 mbedtls_mock_socket_init( &server );
2463
2464 /* Fill up the buffers with structured data so that unwanted changes
2465 * can be detected */
2466 for( i = 0; i < ROUNDS; i++ )
2467 {
2468 for( j = 0; j < MSGLEN; j++ )
2469 {
2470 message[i][j] = ( i * MSGLEN + j ) & 0xFF;
2471 }
2472 }
2473
Janos Follath031827f2019-11-27 11:12:14 +00002474 /* Make sure that sending a message takes a few iterations. */
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002475 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, BUFLEN ) );
Janos Follath031827f2019-11-27 11:12:14 +00002476
Janos Follath031827f2019-11-27 11:12:14 +00002477 /* Send the message from both sides, interleaving. */
2478 progress = 1;
2479 for( i = 0; i < ROUNDS; i++ )
2480 {
2481 written[i] = 0;
2482 read[i] = 0;
2483 }
2484 /* This loop does not stop as long as there was a successful write or read
2485 * of at least one byte on either side. */
2486 while( progress != 0 )
2487 {
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002488 mbedtls_mock_socket *socket;
Janos Follath031827f2019-11-27 11:12:14 +00002489
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002490 for( i = 0; i < ROUNDS; i++ )
Janos Follath3766ba52019-11-27 13:31:42 +00002491 {
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002492 /* First sending is from the client */
2493 socket = ( i % 2 == 0 ) ? ( &client ) : ( &server );
Janos Follath031827f2019-11-27 11:12:14 +00002494
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002495 send_ret[i] = send( socket, message[i] + written[i],
2496 MSGLEN - written[i] );
2497 TEST_ASSERT( send_ret[i] >= 0 );
2498 TEST_ASSERT( send_ret[i] <= BUFLEN );
2499 written[i] += send_ret[i];
Janos Follath031827f2019-11-27 11:12:14 +00002500
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002501 /* If the buffer is full we can test blocking and non-blocking
2502 * send */
2503 if ( send_ret[i] == BUFLEN )
2504 {
2505 int blocking_ret = send( socket, message[i] , 1 );
2506 if ( blocking )
2507 {
2508 TEST_ASSERT( blocking_ret == 0 );
2509 }
2510 else
2511 {
2512 TEST_ASSERT( blocking_ret == MBEDTLS_ERR_SSL_WANT_WRITE );
2513 }
2514 }
Janos Follath3766ba52019-11-27 13:31:42 +00002515 }
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002516
2517 for( i = 0; i < ROUNDS; i++ )
Janos Follath3766ba52019-11-27 13:31:42 +00002518 {
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002519 /* First receiving is from the server */
2520 socket = ( i % 2 == 0 ) ? ( &server ) : ( &client );
2521
2522 recv_ret[i] = recv( socket, received[i] + read[i],
2523 MSGLEN - read[i] );
2524
2525 /* The result depends on whether any data was sent */
2526 if ( send_ret[i] > 0 )
2527 {
2528 TEST_ASSERT( recv_ret[i] > 0 );
2529 TEST_ASSERT( recv_ret[i] <= BUFLEN );
2530 read[i] += recv_ret[i];
2531 }
2532 else if( blocking )
2533 {
2534 TEST_ASSERT( recv_ret[i] == 0 );
2535 }
2536 else
2537 {
2538 TEST_ASSERT( recv_ret[i] == MBEDTLS_ERR_SSL_WANT_READ );
2539 recv_ret[i] = 0;
2540 }
2541
2542 /* If the buffer is empty we can test blocking and non-blocking
2543 * read */
2544 if ( recv_ret[i] == BUFLEN )
2545 {
2546 int blocking_ret = recv( socket, received[i], 1 );
2547 if ( blocking )
2548 {
2549 TEST_ASSERT( blocking_ret == 0 );
2550 }
2551 else
2552 {
2553 TEST_ASSERT( blocking_ret == MBEDTLS_ERR_SSL_WANT_READ );
2554 }
2555 }
Janos Follath3766ba52019-11-27 13:31:42 +00002556 }
Janos Follath031827f2019-11-27 11:12:14 +00002557
2558 progress = 0;
2559 for( i = 0; i < ROUNDS; i++ )
2560 {
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002561 progress += send_ret[i] + recv_ret[i];
Janos Follath031827f2019-11-27 11:12:14 +00002562 }
2563 }
2564
2565 for( i = 0; i < ROUNDS; i++ )
2566 TEST_ASSERT( memcmp( message[i], received[i], MSGLEN ) == 0 );
2567
2568exit:
2569
2570 mbedtls_mock_socket_close( &client );
2571 mbedtls_mock_socket_close( &server );
2572}
2573/* END_CASE */
2574
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002575/* BEGIN_CASE */
2576void ssl_message_queue_sanity( )
2577{
2578 mbedtls_test_message_queue queue;
2579
2580 /* Trying to push/pull to an empty queue */
2581 TEST_ASSERT( mbedtls_test_message_queue_push_info( NULL, 1 )
2582 == MBEDTLS_TEST_ERROR_ARG_NULL );
2583 TEST_ASSERT( mbedtls_test_message_queue_pop_info( NULL, 1 )
2584 == MBEDTLS_TEST_ERROR_ARG_NULL );
2585
Andrzej Kurek89bdc582020-03-09 06:29:43 -04002586 TEST_ASSERT( mbedtls_test_message_queue_setup( &queue, 3 ) == 0 );
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002587 TEST_ASSERT( queue.capacity == 3 );
2588 TEST_ASSERT( queue.num == 0 );
2589
2590exit:
2591 mbedtls_test_message_queue_free( &queue );
2592}
2593/* END_CASE */
2594
2595/* BEGIN_CASE */
2596void ssl_message_queue_basic( )
2597{
2598 mbedtls_test_message_queue queue;
2599
Andrzej Kurek89bdc582020-03-09 06:29:43 -04002600 TEST_ASSERT( mbedtls_test_message_queue_setup( &queue, 3 ) == 0 );
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002601
2602 /* Sanity test - 3 pushes and 3 pops with sufficient space */
2603 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 );
2604 TEST_ASSERT( queue.capacity == 3 );
2605 TEST_ASSERT( queue.num == 1 );
2606 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 );
2607 TEST_ASSERT( queue.capacity == 3 );
2608 TEST_ASSERT( queue.num == 2 );
2609 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 2 ) == 2 );
2610 TEST_ASSERT( queue.capacity == 3 );
2611 TEST_ASSERT( queue.num == 3 );
2612
2613 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 );
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
2617exit:
2618 mbedtls_test_message_queue_free( &queue );
2619}
2620/* END_CASE */
2621
2622/* BEGIN_CASE */
2623void ssl_message_queue_overflow_underflow( )
2624{
2625 mbedtls_test_message_queue queue;
2626
Andrzej Kurek89bdc582020-03-09 06:29:43 -04002627 TEST_ASSERT( mbedtls_test_message_queue_setup( &queue, 3 ) == 0 );
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002628
2629 /* 4 pushes (last one with an error), 4 pops (last one with an error) */
2630 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 );
2631 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 );
2632 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 2 ) == 2 );
2633 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 3 )
Andrzej Kurekf46b9122020-02-07 08:19:00 -05002634 == MBEDTLS_ERR_SSL_WANT_WRITE );
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002635
2636 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 );
2637 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 );
2638 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 2 ) == 2 );
2639
2640 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 )
Andrzej Kurekf46b9122020-02-07 08:19:00 -05002641 == MBEDTLS_ERR_SSL_WANT_READ );
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002642
2643exit:
2644 mbedtls_test_message_queue_free( &queue );
2645}
2646/* END_CASE */
2647
2648/* BEGIN_CASE */
2649void ssl_message_queue_interleaved( )
2650{
2651 mbedtls_test_message_queue queue;
2652
Andrzej Kurek89bdc582020-03-09 06:29:43 -04002653 TEST_ASSERT( mbedtls_test_message_queue_setup( &queue, 3 ) == 0 );
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002654
2655 /* Interleaved test - [2 pushes, 1 pop] twice, and then two pops
2656 * (to wrap around the buffer) */
2657 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 );
2658 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 );
2659
2660 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 );
2661
2662 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 2 ) == 2 );
2663 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 3 ) == 3 );
2664
2665 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 );
2666 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 2 ) == 2 );
2667
2668 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 5 ) == 5 );
2669 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 8 ) == 8 );
2670
2671 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 3 ) == 3 );
2672
2673 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 5 ) == 5 );
2674
2675 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 8 ) == 8 );
2676
2677exit:
2678 mbedtls_test_message_queue_free( &queue );
2679}
2680/* END_CASE */
2681
2682/* BEGIN_CASE */
2683void ssl_message_queue_insufficient_buffer( )
2684{
2685 mbedtls_test_message_queue queue;
2686 size_t message_len = 10;
2687 size_t buffer_len = 5;
2688
Andrzej Kurek89bdc582020-03-09 06:29:43 -04002689 TEST_ASSERT( mbedtls_test_message_queue_setup( &queue, 1 ) == 0 );
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002690
2691 /* Popping without a sufficient buffer */
2692 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, message_len )
2693 == (int) message_len );
2694 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, buffer_len )
2695 == (int) buffer_len );
2696exit:
2697 mbedtls_test_message_queue_free( &queue );
2698}
2699/* END_CASE */
2700
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002701/* BEGIN_CASE */
2702void ssl_message_mock_uninitialized( )
2703{
2704 enum { MSGLEN = 10 };
Shawn Carey63ee8812021-05-13 10:26:52 -04002705 unsigned char message[MSGLEN] = {0}, received[MSGLEN];
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002706 mbedtls_mock_socket client, server;
2707 mbedtls_test_message_queue server_queue, client_queue;
2708 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05002709 mbedtls_message_socket_init( &server_context );
2710 mbedtls_message_socket_init( &client_context );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002711
2712 /* Send with a NULL context */
2713 TEST_ASSERT( mbedtls_mock_tcp_send_msg( NULL, message, MSGLEN )
2714 == MBEDTLS_TEST_ERROR_CONTEXT_ERROR );
2715
2716 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( NULL, message, MSGLEN )
2717 == MBEDTLS_TEST_ERROR_CONTEXT_ERROR );
2718
2719 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 1,
2720 &server,
2721 &server_context ) == 0 );
2722
2723 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 1,
2724 &client,
2725 &client_context ) == 0 );
2726
2727 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, MSGLEN )
2728 == MBEDTLS_TEST_ERROR_SEND_FAILED );
2729
2730 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
Andrzej Kurekf46b9122020-02-07 08:19:00 -05002731 == MBEDTLS_ERR_SSL_WANT_READ );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002732
2733 /* Push directly to a queue to later simulate a disconnected behavior */
2734 TEST_ASSERT( mbedtls_test_message_queue_push_info( &server_queue, MSGLEN )
2735 == MSGLEN );
2736
2737 /* Test if there's an error when trying to read from a disconnected
2738 * socket */
2739 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
2740 == MBEDTLS_TEST_ERROR_RECV_FAILED );
2741 exit:
2742 mbedtls_message_socket_close( &server_context );
2743 mbedtls_message_socket_close( &client_context );
2744}
2745/* END_CASE */
2746
2747/* BEGIN_CASE */
2748void ssl_message_mock_basic( )
2749{
2750 enum { MSGLEN = 10 };
2751 unsigned char message[MSGLEN], received[MSGLEN];
2752 mbedtls_mock_socket client, server;
2753 unsigned i;
2754 mbedtls_test_message_queue server_queue, client_queue;
2755 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05002756 mbedtls_message_socket_init( &server_context );
2757 mbedtls_message_socket_init( &client_context );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002758
2759 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 1,
2760 &server,
2761 &server_context ) == 0 );
2762
2763 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 1,
2764 &client,
2765 &client_context ) == 0 );
2766
2767 /* Fill up the buffer with structured data so that unwanted changes
2768 * can be detected */
2769 for( i = 0; i < MSGLEN; i++ )
2770 {
2771 message[i] = i & 0xFF;
2772 }
2773 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
2774 MSGLEN ) );
2775
2776 /* Send the message to the server */
2777 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2778 MSGLEN ) == MSGLEN );
2779
2780 /* Read from the server */
2781 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
2782 == MSGLEN );
2783
2784 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
2785 memset( received, 0, MSGLEN );
2786
2787 /* Send the message to the client */
2788 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &server_context, message,
2789 MSGLEN ) == MSGLEN );
2790
2791 /* Read from the client */
2792 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &client_context, received, MSGLEN )
2793 == MSGLEN );
2794 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
2795
2796 exit:
2797 mbedtls_message_socket_close( &server_context );
2798 mbedtls_message_socket_close( &client_context );
2799}
2800/* END_CASE */
2801
2802/* BEGIN_CASE */
2803void ssl_message_mock_queue_overflow_underflow( )
2804{
2805 enum { MSGLEN = 10 };
2806 unsigned char message[MSGLEN], received[MSGLEN];
2807 mbedtls_mock_socket client, server;
2808 unsigned i;
2809 mbedtls_test_message_queue server_queue, client_queue;
2810 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05002811 mbedtls_message_socket_init( &server_context );
2812 mbedtls_message_socket_init( &client_context );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002813
2814 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 2,
2815 &server,
2816 &server_context ) == 0 );
2817
2818 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 2,
2819 &client,
2820 &client_context ) == 0 );
2821
2822 /* Fill up the buffer with structured data so that unwanted changes
2823 * can be detected */
2824 for( i = 0; i < MSGLEN; i++ )
2825 {
2826 message[i] = i & 0xFF;
2827 }
2828 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
2829 MSGLEN*2 ) );
2830
2831 /* Send three message to the server, last one with an error */
2832 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2833 MSGLEN - 1 ) == MSGLEN - 1 );
2834
2835 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2836 MSGLEN ) == MSGLEN );
2837
2838 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2839 MSGLEN )
Andrzej Kurekf46b9122020-02-07 08:19:00 -05002840 == MBEDTLS_ERR_SSL_WANT_WRITE );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002841
2842 /* Read three messages from the server, last one with an error */
2843 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received,
2844 MSGLEN - 1 ) == MSGLEN - 1 );
2845
2846 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
2847 == MSGLEN );
2848
2849 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
2850
2851 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
Andrzej Kurekf46b9122020-02-07 08:19:00 -05002852 == MBEDTLS_ERR_SSL_WANT_READ );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002853
2854 exit:
2855 mbedtls_message_socket_close( &server_context );
2856 mbedtls_message_socket_close( &client_context );
2857}
2858/* END_CASE */
2859
2860/* BEGIN_CASE */
2861void ssl_message_mock_socket_overflow( )
2862{
2863 enum { MSGLEN = 10 };
2864 unsigned char message[MSGLEN], received[MSGLEN];
2865 mbedtls_mock_socket client, server;
2866 unsigned i;
2867 mbedtls_test_message_queue server_queue, client_queue;
2868 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05002869 mbedtls_message_socket_init( &server_context );
2870 mbedtls_message_socket_init( &client_context );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002871
2872 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 2,
2873 &server,
2874 &server_context ) == 0 );
2875
2876 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 2,
2877 &client,
2878 &client_context ) == 0 );
2879
2880 /* Fill up the buffer with structured data so that unwanted changes
2881 * can be detected */
2882 for( i = 0; i < MSGLEN; i++ )
2883 {
2884 message[i] = i & 0xFF;
2885 }
2886 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
2887 MSGLEN ) );
2888
2889 /* Send two message to the server, second one with an error */
2890 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2891 MSGLEN ) == MSGLEN );
2892
2893 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2894 MSGLEN )
2895 == MBEDTLS_TEST_ERROR_SEND_FAILED );
2896
2897 /* Read the only message from the server */
2898 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
2899 == MSGLEN );
2900
2901 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
2902
2903 exit:
2904 mbedtls_message_socket_close( &server_context );
2905 mbedtls_message_socket_close( &client_context );
2906}
2907/* END_CASE */
2908
2909/* BEGIN_CASE */
2910void ssl_message_mock_truncated( )
2911{
2912 enum { MSGLEN = 10 };
2913 unsigned char message[MSGLEN], received[MSGLEN];
2914 mbedtls_mock_socket client, server;
2915 unsigned i;
2916 mbedtls_test_message_queue server_queue, client_queue;
2917 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05002918 mbedtls_message_socket_init( &server_context );
2919 mbedtls_message_socket_init( &client_context );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002920
2921 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 2,
2922 &server,
2923 &server_context ) == 0 );
2924
2925 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 2,
2926 &client,
2927 &client_context ) == 0 );
2928
2929 memset( received, 0, MSGLEN );
2930 /* Fill up the buffer with structured data so that unwanted changes
2931 * can be detected */
2932 for( i = 0; i < MSGLEN; i++ )
2933 {
2934 message[i] = i & 0xFF;
2935 }
2936 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
2937 2 * MSGLEN ) );
2938
2939 /* Send two messages to the server, the second one small enough to fit in the
2940 * receiver's buffer. */
2941 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2942 MSGLEN ) == MSGLEN );
2943 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2944 MSGLEN / 2 ) == MSGLEN / 2 );
2945 /* Read a truncated message from the server */
2946 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN/2 )
2947 == MSGLEN/2 );
2948
2949 /* Test that the first half of the message is valid, and second one isn't */
2950 TEST_ASSERT( memcmp( message, received, MSGLEN/2 ) == 0 );
2951 TEST_ASSERT( memcmp( message + MSGLEN/2, received + MSGLEN/2, MSGLEN/2 )
2952 != 0 );
2953 memset( received, 0, MSGLEN );
2954
2955 /* Read a full message from the server */
2956 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN/2 )
2957 == MSGLEN / 2 );
2958
2959 /* Test that the first half of the message is valid */
2960 TEST_ASSERT( memcmp( message, received, MSGLEN/2 ) == 0 );
2961
2962 exit:
2963 mbedtls_message_socket_close( &server_context );
2964 mbedtls_message_socket_close( &client_context );
2965}
2966/* END_CASE */
2967
2968/* BEGIN_CASE */
2969void ssl_message_mock_socket_read_error( )
2970{
2971 enum { MSGLEN = 10 };
2972 unsigned char message[MSGLEN], received[MSGLEN];
2973 mbedtls_mock_socket client, server;
2974 unsigned i;
2975 mbedtls_test_message_queue server_queue, client_queue;
2976 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05002977 mbedtls_message_socket_init( &server_context );
2978 mbedtls_message_socket_init( &client_context );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002979
2980 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 1,
2981 &server,
2982 &server_context ) == 0 );
2983
2984 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 1,
2985 &client,
2986 &client_context ) == 0 );
2987
2988 /* Fill up the buffer with structured data so that unwanted changes
2989 * can be detected */
2990 for( i = 0; i < MSGLEN; i++ )
2991 {
2992 message[i] = i & 0xFF;
2993 }
2994 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
2995 MSGLEN ) );
2996
2997 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2998 MSGLEN ) == MSGLEN );
2999
3000 /* Force a read error by disconnecting the socket by hand */
3001 server.status = 0;
3002 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
3003 == MBEDTLS_TEST_ERROR_RECV_FAILED );
3004 /* Return to a valid state */
3005 server.status = MBEDTLS_MOCK_SOCKET_CONNECTED;
3006
3007 memset( received, 0, sizeof( received ) );
3008
3009 /* Test that even though the server tried to read once disconnected, the
3010 * continuity is preserved */
3011 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
3012 == MSGLEN );
3013
3014 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
3015
3016 exit:
3017 mbedtls_message_socket_close( &server_context );
3018 mbedtls_message_socket_close( &client_context );
3019}
3020/* END_CASE */
3021
3022/* BEGIN_CASE */
3023void ssl_message_mock_interleaved_one_way( )
3024{
3025 enum { MSGLEN = 10 };
3026 unsigned char message[MSGLEN], received[MSGLEN];
3027 mbedtls_mock_socket client, server;
3028 unsigned i;
3029 mbedtls_test_message_queue server_queue, client_queue;
3030 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05003031 mbedtls_message_socket_init( &server_context );
3032 mbedtls_message_socket_init( &client_context );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003033
3034 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 3,
3035 &server,
3036 &server_context ) == 0 );
3037
3038 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 3,
3039 &client,
3040 &client_context ) == 0 );
3041
3042 /* Fill up the buffer with structured data so that unwanted changes
3043 * can be detected */
3044 for( i = 0; i < MSGLEN; i++ )
3045 {
3046 message[i] = i & 0xFF;
3047 }
3048 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
3049 MSGLEN*3 ) );
3050
3051 /* Interleaved test - [2 sends, 1 read] twice, and then two reads
3052 * (to wrap around the buffer) */
3053 for( i = 0; i < 2; i++ )
3054 {
3055 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
3056 MSGLEN ) == MSGLEN );
3057
3058 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
3059 MSGLEN ) == MSGLEN );
3060
3061 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received,
3062 MSGLEN ) == MSGLEN );
3063 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
3064 memset( received, 0, sizeof( received ) );
3065 }
3066
3067 for( i = 0; i < 2; i++ )
3068 {
3069 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received,
3070 MSGLEN ) == MSGLEN );
3071
3072 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
3073 }
3074 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
Andrzej Kurekf46b9122020-02-07 08:19:00 -05003075 == MBEDTLS_ERR_SSL_WANT_READ );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003076 exit:
3077 mbedtls_message_socket_close( &server_context );
3078 mbedtls_message_socket_close( &client_context );
3079}
3080/* END_CASE */
3081
3082/* BEGIN_CASE */
3083void ssl_message_mock_interleaved_two_ways( )
3084{
3085 enum { MSGLEN = 10 };
3086 unsigned char message[MSGLEN], received[MSGLEN];
3087 mbedtls_mock_socket client, server;
3088 unsigned i;
3089 mbedtls_test_message_queue server_queue, client_queue;
3090 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05003091 mbedtls_message_socket_init( &server_context );
3092 mbedtls_message_socket_init( &client_context );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003093
3094 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 3,
3095 &server,
3096 &server_context ) == 0 );
3097
3098 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 3,
3099 &client,
3100 &client_context ) == 0 );
3101
3102 /* Fill up the buffer with structured data so that unwanted changes
3103 * can be detected */
3104 for( i = 0; i < MSGLEN; i++ )
3105 {
3106 message[i] = i & 0xFF;
3107 }
3108 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
3109 MSGLEN*3 ) );
3110
3111 /* Interleaved test - [2 sends, 1 read] twice, both ways, and then two reads
3112 * (to wrap around the buffer) both ways. */
3113 for( i = 0; i < 2; i++ )
3114 {
3115 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
3116 MSGLEN ) == MSGLEN );
3117
3118 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
3119 MSGLEN ) == MSGLEN );
3120
3121 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &server_context, message,
3122 MSGLEN ) == MSGLEN );
3123
3124 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &server_context, message,
3125 MSGLEN ) == MSGLEN );
3126
3127 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received,
3128 MSGLEN ) == MSGLEN );
3129
3130 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
3131
3132 memset( received, 0, sizeof( received ) );
3133
3134 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &client_context, received,
3135 MSGLEN ) == MSGLEN );
3136
3137 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
3138
3139 memset( received, 0, sizeof( received ) );
3140 }
3141
3142 for( i = 0; i < 2; i++ )
3143 {
3144 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received,
3145 MSGLEN ) == MSGLEN );
3146
3147 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
3148 memset( received, 0, sizeof( received ) );
3149
3150 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &client_context, received,
3151 MSGLEN ) == MSGLEN );
3152
3153 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
3154 memset( received, 0, sizeof( received ) );
3155 }
3156
3157 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
Andrzej Kurekf46b9122020-02-07 08:19:00 -05003158 == MBEDTLS_ERR_SSL_WANT_READ );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003159
3160 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &client_context, received, MSGLEN )
Andrzej Kurekf46b9122020-02-07 08:19:00 -05003161 == MBEDTLS_ERR_SSL_WANT_READ );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003162 exit:
3163 mbedtls_message_socket_close( &server_context );
3164 mbedtls_message_socket_close( &client_context );
3165}
3166/* END_CASE */
3167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003168/* BEGIN_CASE depends_on:MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Azim Khan5fcca462018-06-29 11:05:32 +01003169void ssl_dtls_replay( data_t * prevs, data_t * new, int ret )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003170{
Azim Khand30ca132017-06-09 04:32:58 +01003171 uint32_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003172 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02003173 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003174
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02003175 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02003176 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02003177
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02003178 TEST_ASSERT( mbedtls_ssl_config_defaults( &conf,
3179 MBEDTLS_SSL_IS_CLIENT,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02003180 MBEDTLS_SSL_TRANSPORT_DATAGRAM,
3181 MBEDTLS_SSL_PRESET_DEFAULT ) == 0 );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02003182 TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003183
3184 /* Read previous record numbers */
Azim Khand30ca132017-06-09 04:32:58 +01003185 for( len = 0; len < prevs->len; len += 6 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003186 {
Azim Khand30ca132017-06-09 04:32:58 +01003187 memcpy( ssl.in_ctr + 2, prevs->x + len, 6 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003188 mbedtls_ssl_dtls_replay_update( &ssl );
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003189 }
3190
3191 /* Check new number */
Azim Khand30ca132017-06-09 04:32:58 +01003192 memcpy( ssl.in_ctr + 2, new->x, 6 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003193 TEST_ASSERT( mbedtls_ssl_dtls_replay_check( &ssl ) == ret );
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003195 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02003196 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003197}
3198/* END_CASE */
Hanno Beckerb25c0c72017-05-05 11:24:30 +01003199
Andrzej Kurek9155e7f2022-10-18 09:36:19 -04003200/* BEGIN_CASE depends_on:MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Hanno Beckerb25c0c72017-05-05 11:24:30 +01003201void ssl_set_hostname_twice( char *hostname0, char *hostname1 )
3202{
3203 mbedtls_ssl_context ssl;
3204 mbedtls_ssl_init( &ssl );
3205
3206 TEST_ASSERT( mbedtls_ssl_set_hostname( &ssl, hostname0 ) == 0 );
3207 TEST_ASSERT( mbedtls_ssl_set_hostname( &ssl, hostname1 ) == 0 );
3208
3209 mbedtls_ssl_free( &ssl );
3210}
Darryl Green11999bb2018-03-13 15:22:58 +00003211/* END_CASE */
Hanno Beckera18d1322018-01-03 14:27:32 +00003212
3213/* BEGIN_CASE */
3214void ssl_crypt_record( int cipher_type, int hash_id,
Hanno Beckerd856c822019-04-29 17:30:59 +01003215 int etm, int tag_mode, int ver,
3216 int cid0_len, int cid1_len )
Hanno Beckera18d1322018-01-03 14:27:32 +00003217{
3218 /*
3219 * Test several record encryptions and decryptions
3220 * with plenty of space before and after the data
3221 * within the record buffer.
3222 */
3223
3224 int ret;
3225 int num_records = 16;
3226 mbedtls_ssl_context ssl; /* ONLY for debugging */
3227
3228 mbedtls_ssl_transform t0, t1;
Hanno Becker81e16a32019-03-01 11:21:44 +00003229 unsigned char *buf = NULL;
Hanno Beckera18d1322018-01-03 14:27:32 +00003230 size_t const buflen = 512;
3231 mbedtls_record rec, rec_backup;
3232
3233 mbedtls_ssl_init( &ssl );
3234 mbedtls_ssl_transform_init( &t0 );
3235 mbedtls_ssl_transform_init( &t1 );
3236 TEST_ASSERT( build_transforms( &t0, &t1, cipher_type, hash_id,
Hanno Beckerd856c822019-04-29 17:30:59 +01003237 etm, tag_mode, ver,
3238 (size_t) cid0_len,
3239 (size_t) cid1_len ) == 0 );
Hanno Beckera18d1322018-01-03 14:27:32 +00003240
Hanno Becker3ee54212019-04-04 16:31:26 +01003241 TEST_ASSERT( ( buf = mbedtls_calloc( 1, buflen ) ) != NULL );
Hanno Beckera18d1322018-01-03 14:27:32 +00003242
3243 while( num_records-- > 0 )
3244 {
3245 mbedtls_ssl_transform *t_dec, *t_enc;
3246 /* Take turns in who's sending and who's receiving. */
3247 if( num_records % 3 == 0 )
3248 {
3249 t_dec = &t0;
3250 t_enc = &t1;
3251 }
3252 else
3253 {
3254 t_dec = &t1;
3255 t_enc = &t0;
3256 }
3257
3258 /*
3259 * The record header affects the transformation in two ways:
3260 * 1) It determines the AEAD additional data
3261 * 2) The record counter sometimes determines the IV.
3262 *
3263 * Apart from that, the fields don't have influence.
3264 * In particular, it is currently not the responsibility
3265 * of ssl_encrypt/decrypt_buf to check if the transform
3266 * version matches the record version, or that the
3267 * type is sensible.
3268 */
3269
3270 memset( rec.ctr, num_records, sizeof( rec.ctr ) );
3271 rec.type = 42;
3272 rec.ver[0] = num_records;
3273 rec.ver[1] = num_records;
Hanno Beckera0e20d02019-05-15 14:03:01 +01003274#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd856c822019-04-29 17:30:59 +01003275 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01003276#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera18d1322018-01-03 14:27:32 +00003277
3278 rec.buf = buf;
3279 rec.buf_len = buflen;
3280 rec.data_offset = 16;
3281 /* Make sure to vary the length to exercise different
3282 * paddings. */
3283 rec.data_len = 1 + num_records;
3284
3285 memset( rec.buf + rec.data_offset, 42, rec.data_len );
3286
3287 /* Make a copy for later comparison */
3288 rec_backup = rec;
3289
3290 /* Encrypt record */
3291 ret = mbedtls_ssl_encrypt_buf( &ssl, t_enc, &rec,
Ronald Cron351f0ee2020-06-10 12:12:18 +02003292 mbedtls_test_rnd_std_rand, NULL );
Hanno Beckera18d1322018-01-03 14:27:32 +00003293 TEST_ASSERT( ret == 0 || ret == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3294 if( ret != 0 )
3295 {
3296 continue;
3297 }
3298
Hanno Beckerb2713ab2020-05-07 14:54:22 +01003299#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3300 if( rec.cid_len != 0 )
3301 {
3302 /* DTLS 1.2 + CID hides the real content type and
3303 * uses a special CID content type in the protected
3304 * record. Double-check this. */
3305 TEST_ASSERT( rec.type == MBEDTLS_SSL_MSG_CID );
3306 }
3307#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3308
3309#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
3310 if( t_enc->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
3311 {
3312 /* TLS 1.3 hides the real content type and
3313 * always uses Application Data as the content type
3314 * for protected records. Double-check this. */
3315 TEST_ASSERT( rec.type == MBEDTLS_SSL_MSG_APPLICATION_DATA );
3316 }
3317#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
3318
Hanno Beckera18d1322018-01-03 14:27:32 +00003319 /* Decrypt record with t_dec */
Hanno Beckerd856c822019-04-29 17:30:59 +01003320 ret = mbedtls_ssl_decrypt_buf( &ssl, t_dec, &rec );
3321 TEST_ASSERT( ret == 0 );
Hanno Beckera18d1322018-01-03 14:27:32 +00003322
3323 /* Compare results */
3324 TEST_ASSERT( rec.type == rec_backup.type );
3325 TEST_ASSERT( memcmp( rec.ctr, rec_backup.ctr, 8 ) == 0 );
3326 TEST_ASSERT( rec.ver[0] == rec_backup.ver[0] );
3327 TEST_ASSERT( rec.ver[1] == rec_backup.ver[1] );
3328 TEST_ASSERT( rec.data_len == rec_backup.data_len );
3329 TEST_ASSERT( rec.data_offset == rec_backup.data_offset );
3330 TEST_ASSERT( memcmp( rec.buf + rec.data_offset,
3331 rec_backup.buf + rec_backup.data_offset,
3332 rec.data_len ) == 0 );
3333 }
3334
Hanno Becker81e16a32019-03-01 11:21:44 +00003335exit:
3336
Hanno Beckera18d1322018-01-03 14:27:32 +00003337 /* Cleanup */
3338 mbedtls_ssl_free( &ssl );
3339 mbedtls_ssl_transform_free( &t0 );
3340 mbedtls_ssl_transform_free( &t1 );
3341
Hanno Becker3ee54212019-04-04 16:31:26 +01003342 mbedtls_free( buf );
Hanno Beckera18d1322018-01-03 14:27:32 +00003343}
3344/* END_CASE */
Hanno Beckerb3268da2018-01-05 15:20:24 +00003345
3346/* BEGIN_CASE */
3347void ssl_crypt_record_small( int cipher_type, int hash_id,
Hanno Beckerd856c822019-04-29 17:30:59 +01003348 int etm, int tag_mode, int ver,
3349 int cid0_len, int cid1_len )
Hanno Beckerb3268da2018-01-05 15:20:24 +00003350{
3351 /*
3352 * Test pairs of encryption and decryption with an increasing
3353 * amount of space in the record buffer - in more detail:
3354 * 1) Try to encrypt with 0, 1, 2, ... bytes available
3355 * in front of the plaintext, and expect the encryption
3356 * to succeed starting from some offset. Always keep
3357 * enough space in the end of the buffer.
3358 * 2) Try to encrypt with 0, 1, 2, ... bytes available
3359 * at the end of the plaintext, and expect the encryption
3360 * to succeed starting from some offset. Always keep
3361 * enough space at the beginning of the buffer.
3362 * 3) Try to encrypt with 0, 1, 2, ... bytes available
3363 * both at the front and end of the plaintext,
3364 * and expect the encryption to succeed starting from
3365 * some offset.
3366 *
3367 * If encryption succeeds, check that decryption succeeds
3368 * and yields the original record.
3369 */
3370
3371 mbedtls_ssl_context ssl; /* ONLY for debugging */
3372
3373 mbedtls_ssl_transform t0, t1;
Hanno Becker81e16a32019-03-01 11:21:44 +00003374 unsigned char *buf = NULL;
Hanno Beckerd856c822019-04-29 17:30:59 +01003375 size_t const buflen = 256;
Hanno Beckerb3268da2018-01-05 15:20:24 +00003376 mbedtls_record rec, rec_backup;
3377
3378 int ret;
Hanno Beckerd856c822019-04-29 17:30:59 +01003379 int mode; /* Mode 1, 2 or 3 as explained above */
3380 size_t offset; /* Available space at beginning/end/both */
3381 size_t threshold = 96; /* Maximum offset to test against */
Hanno Beckerb3268da2018-01-05 15:20:24 +00003382
Hanno Beckerd856c822019-04-29 17:30:59 +01003383 size_t default_pre_padding = 64; /* Pre-padding to use in mode 2 */
3384 size_t default_post_padding = 128; /* Post-padding to use in mode 1 */
Hanno Beckerb3268da2018-01-05 15:20:24 +00003385
3386 int seen_success; /* Indicates if in the current mode we've
3387 * already seen a successful test. */
3388
3389 mbedtls_ssl_init( &ssl );
3390 mbedtls_ssl_transform_init( &t0 );
3391 mbedtls_ssl_transform_init( &t1 );
3392 TEST_ASSERT( build_transforms( &t0, &t1, cipher_type, hash_id,
Hanno Beckerd856c822019-04-29 17:30:59 +01003393 etm, tag_mode, ver,
3394 (size_t) cid0_len,
3395 (size_t) cid1_len ) == 0 );
Hanno Beckerb3268da2018-01-05 15:20:24 +00003396
Hanno Becker3ee54212019-04-04 16:31:26 +01003397 TEST_ASSERT( ( buf = mbedtls_calloc( 1, buflen ) ) != NULL );
Hanno Beckerb3268da2018-01-05 15:20:24 +00003398
3399 for( mode=1; mode <= 3; mode++ )
3400 {
3401 seen_success = 0;
3402 for( offset=0; offset <= threshold; offset++ )
3403 {
3404 mbedtls_ssl_transform *t_dec, *t_enc;
Hanno Becker6c87b3f2019-04-29 17:24:44 +01003405 t_dec = &t0;
3406 t_enc = &t1;
Hanno Beckerb3268da2018-01-05 15:20:24 +00003407
3408 memset( rec.ctr, offset, sizeof( rec.ctr ) );
3409 rec.type = 42;
3410 rec.ver[0] = offset;
3411 rec.ver[1] = offset;
3412 rec.buf = buf;
3413 rec.buf_len = buflen;
Hanno Beckera0e20d02019-05-15 14:03:01 +01003414#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd856c822019-04-29 17:30:59 +01003415 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01003416#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerb3268da2018-01-05 15:20:24 +00003417
3418 switch( mode )
3419 {
3420 case 1: /* Space in the beginning */
3421 rec.data_offset = offset;
3422 rec.data_len = buflen - offset - default_post_padding;
3423 break;
3424
3425 case 2: /* Space in the end */
3426 rec.data_offset = default_pre_padding;
3427 rec.data_len = buflen - default_pre_padding - offset;
3428 break;
3429
3430 case 3: /* Space in the beginning and end */
3431 rec.data_offset = offset;
3432 rec.data_len = buflen - 2 * offset;
3433 break;
3434
3435 default:
3436 TEST_ASSERT( 0 );
3437 break;
3438 }
3439
3440 memset( rec.buf + rec.data_offset, 42, rec.data_len );
3441
3442 /* Make a copy for later comparison */
3443 rec_backup = rec;
3444
3445 /* Encrypt record */
Ronald Cron6c5bd7f2020-06-10 14:08:26 +02003446 ret = mbedtls_ssl_encrypt_buf( &ssl, t_enc, &rec,
3447 mbedtls_test_rnd_std_rand, NULL );
Hanno Beckerb3268da2018-01-05 15:20:24 +00003448
3449 if( ( mode == 1 || mode == 2 ) && seen_success )
3450 {
3451 TEST_ASSERT( ret == 0 );
3452 }
3453 else
3454 {
3455 TEST_ASSERT( ret == 0 || ret == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3456 if( ret == 0 )
3457 seen_success = 1;
3458 }
3459
3460 if( ret != 0 )
3461 continue;
3462
Hanno Beckerb2713ab2020-05-07 14:54:22 +01003463#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3464 if( rec.cid_len != 0 )
3465 {
3466 /* DTLS 1.2 + CID hides the real content type and
3467 * uses a special CID content type in the protected
3468 * record. Double-check this. */
3469 TEST_ASSERT( rec.type == MBEDTLS_SSL_MSG_CID );
3470 }
3471#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3472
3473#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
3474 if( t_enc->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
3475 {
3476 /* TLS 1.3 hides the real content type and
3477 * always uses Application Data as the content type
3478 * for protected records. Double-check this. */
3479 TEST_ASSERT( rec.type == MBEDTLS_SSL_MSG_APPLICATION_DATA );
3480 }
3481#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
3482
Hanno Beckerb3268da2018-01-05 15:20:24 +00003483 /* Decrypt record with t_dec */
3484 TEST_ASSERT( mbedtls_ssl_decrypt_buf( &ssl, t_dec, &rec ) == 0 );
3485
3486 /* Compare results */
3487 TEST_ASSERT( rec.type == rec_backup.type );
3488 TEST_ASSERT( memcmp( rec.ctr, rec_backup.ctr, 8 ) == 0 );
3489 TEST_ASSERT( rec.ver[0] == rec_backup.ver[0] );
3490 TEST_ASSERT( rec.ver[1] == rec_backup.ver[1] );
3491 TEST_ASSERT( rec.data_len == rec_backup.data_len );
3492 TEST_ASSERT( rec.data_offset == rec_backup.data_offset );
3493 TEST_ASSERT( memcmp( rec.buf + rec.data_offset,
3494 rec_backup.buf + rec_backup.data_offset,
3495 rec.data_len ) == 0 );
3496 }
3497
3498 TEST_ASSERT( seen_success == 1 );
3499 }
3500
Hanno Becker81e16a32019-03-01 11:21:44 +00003501exit:
3502
Hanno Beckerb3268da2018-01-05 15:20:24 +00003503 /* Cleanup */
3504 mbedtls_ssl_free( &ssl );
3505 mbedtls_ssl_transform_free( &t0 );
3506 mbedtls_ssl_transform_free( &t1 );
3507
Hanno Becker3ee54212019-04-04 16:31:26 +01003508 mbedtls_free( buf );
Hanno Beckerb3268da2018-01-05 15:20:24 +00003509}
3510/* END_CASE */
Ron Eldor824ad7b2019-05-13 14:09:00 +03003511
Manuel Pégourié-Gonnard913a2042020-07-09 10:02:41 +02003512/* 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 +02003513void ssl_decrypt_non_etm_cbc( int cipher_type, int hash_id, int trunc_hmac,
Manuel Pégourié-Gonnard864abbf2020-07-21 10:37:14 +02003514 int length_selector )
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003515{
3516 /*
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003517 * Test record decryption for CBC without EtM, focused on the verification
3518 * of padding and MAC.
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003519 *
3520 * Actually depends on TLS >= 1.0 (SSL 3.0 computes the MAC differently),
Manuel Pégourié-Gonnard913a2042020-07-09 10:02:41 +02003521 * and either AES, ARIA, Camellia or DES, but since the test framework
3522 * doesn't support alternation in dependency statements, just depend on
3523 * TLS 1.2 and AES.
Manuel Pégourié-Gonnard864abbf2020-07-21 10:37:14 +02003524 *
3525 * The length_selector argument is interpreted as follows:
3526 * - if it's -1, the plaintext length is 0 and minimal padding is applied
3527 * - if it's -2, the plaintext length is 0 and maximal padding is applied
3528 * - otherwise it must be in [0, 255] and is padding_length from RFC 5246:
3529 * it's the length of the rest of the padding, that is, excluding the
3530 * byte that encodes the length. The minimal non-zero plaintext length
3531 * that gives this padding_length is automatically selected.
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003532 */
3533 mbedtls_ssl_context ssl; /* ONLY for debugging */
3534 mbedtls_ssl_transform t0, t1;
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003535 mbedtls_record rec, rec_save;
3536 unsigned char *buf = NULL, *buf_save = NULL;
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003537 size_t buflen, olen = 0;
Manuel Pégourié-Gonnard864abbf2020-07-21 10:37:14 +02003538 size_t plaintext_len, block_size, i;
Manuel Pégourié-Gonnarde55653f2020-07-22 11:42:57 +02003539 unsigned char padlen; /* excluding the padding_length byte */
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003540 unsigned char add_data[13];
3541 unsigned char mac[MBEDTLS_MD_MAX_SIZE];
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003542 int exp_ret;
Manuel Pégourié-Gonnard4adc04a2020-07-16 10:00:48 +02003543 const unsigned char pad_max_len = 255; /* Per the standard */
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003544
3545 mbedtls_ssl_init( &ssl );
3546 mbedtls_ssl_transform_init( &t0 );
3547 mbedtls_ssl_transform_init( &t1 );
3548
3549 /* Set up transforms with dummy keys */
3550 TEST_ASSERT( build_transforms( &t0, &t1, cipher_type, hash_id,
3551 0, trunc_hmac,
3552 MBEDTLS_SSL_MINOR_VERSION_3,
3553 0 , 0 ) == 0 );
3554
Manuel Pégourié-Gonnard864abbf2020-07-21 10:37:14 +02003555 /* Determine padding/plaintext length */
3556 TEST_ASSERT( length_selector >= -2 && length_selector <= 255 );
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003557 block_size = t0.ivlen;
Manuel Pégourié-Gonnard864abbf2020-07-21 10:37:14 +02003558 if( length_selector < 0 )
3559 {
3560 plaintext_len = 0;
3561
Manuel Pégourié-Gonnarde55653f2020-07-22 11:42:57 +02003562 /* Minimal padding
3563 * The +1 is for the padding_length byte, not counted in padlen. */
Manuel Pégourié-Gonnard864abbf2020-07-21 10:37:14 +02003564 padlen = block_size - ( t0.maclen + 1 ) % block_size;
3565
3566 /* Maximal padding? */
3567 if( length_selector == -2 )
3568 padlen += block_size * ( ( pad_max_len - padlen ) / block_size );
3569 }
3570 else
3571 {
3572 padlen = length_selector;
3573
Manuel Pégourié-Gonnarde55653f2020-07-22 11:42:57 +02003574 /* Minimal non-zero plaintext_length giving desired padding.
3575 * The +1 is for the padding_length byte, not counted in padlen. */
Manuel Pégourié-Gonnard864abbf2020-07-21 10:37:14 +02003576 plaintext_len = block_size - ( padlen + t0.maclen + 1 ) % block_size;
3577 }
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003578
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003579 /* Prepare a buffer for record data */
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003580 buflen = block_size
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003581 + plaintext_len
3582 + t0.maclen
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003583 + padlen + 1;
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003584 ASSERT_ALLOC( buf, buflen );
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003585 ASSERT_ALLOC( buf_save, buflen );
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003586
3587 /* Prepare a dummy record header */
3588 memset( rec.ctr, 0, sizeof( rec.ctr ) );
3589 rec.type = MBEDTLS_SSL_MSG_APPLICATION_DATA;
3590 rec.ver[0] = MBEDTLS_SSL_MAJOR_VERSION_3;
3591 rec.ver[1] = MBEDTLS_SSL_MINOR_VERSION_3;
3592#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3593 rec.cid_len = 0;
3594#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3595
3596 /* Prepare dummy record content */
3597 rec.buf = buf;
3598 rec.buf_len = buflen;
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003599 rec.data_offset = block_size;
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003600 rec.data_len = plaintext_len;
3601 memset( rec.buf + rec.data_offset, 42, rec.data_len );
3602
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003603 /* Serialized version of record header for MAC purposes */
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003604 memcpy( add_data, rec.ctr, 8 );
3605 add_data[8] = rec.type;
3606 add_data[9] = rec.ver[0];
3607 add_data[10] = rec.ver[1];
3608 add_data[11] = ( rec.data_len >> 8 ) & 0xff;
3609 add_data[12] = ( rec.data_len >> 0 ) & 0xff;
3610
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003611 /* Set dummy IV */
3612 memset( t0.iv_enc, 0x55, t0.ivlen );
3613 memcpy( rec.buf, t0.iv_enc, t0.ivlen );
3614
3615 /*
3616 * Prepare a pre-encryption record (with MAC and padding), and save it.
3617 */
3618
3619 /* MAC with additional data */
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003620 TEST_EQUAL( 0, mbedtls_md_hmac_update( &t0.md_ctx_enc, add_data, 13 ) );
3621 TEST_EQUAL( 0, mbedtls_md_hmac_update( &t0.md_ctx_enc,
3622 rec.buf + rec.data_offset,
3623 rec.data_len ) );
3624 TEST_EQUAL( 0, mbedtls_md_hmac_finish( &t0.md_ctx_enc, mac ) );
3625
3626 memcpy( rec.buf + rec.data_offset + rec.data_len, mac, t0.maclen );
3627 rec.data_len += t0.maclen;
3628
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003629 /* Pad */
3630 memset( rec.buf + rec.data_offset + rec.data_len, padlen, padlen + 1 );
3631 rec.data_len += padlen + 1;
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003632
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003633 /* Save correct pre-encryption record */
3634 rec_save = rec;
3635 rec_save.buf = buf_save;
3636 memcpy( buf_save, buf, buflen );
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003637
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003638 /*
3639 * Encrypt and decrypt the correct record, expecting success
3640 */
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003641 TEST_EQUAL( 0, mbedtls_cipher_crypt( &t0.cipher_ctx_enc,
3642 t0.iv_enc, t0.ivlen,
3643 rec.buf + rec.data_offset, rec.data_len,
3644 rec.buf + rec.data_offset, &olen ) );
3645 rec.data_offset -= t0.ivlen;
3646 rec.data_len += t0.ivlen;
3647
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003648 TEST_EQUAL( 0, mbedtls_ssl_decrypt_buf( &ssl, &t1, &rec ) );
3649
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003650 /*
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003651 * Modify each byte of the pre-encryption record before encrypting and
3652 * decrypting it, expecting failure every time.
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003653 */
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003654 for( i = block_size; i < buflen; i++ )
3655 {
Chris Jones9634bb12021-01-20 15:56:42 +00003656 mbedtls_test_set_step( i );
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003657
3658 /* Restore correct pre-encryption record */
3659 rec = rec_save;
3660 rec.buf = buf;
3661 memcpy( buf, buf_save, buflen );
3662
Manuel Pégourié-Gonnardb51f0442020-07-21 10:40:25 +02003663 /* Corrupt one byte of the data (could be plaintext, MAC or padding) */
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003664 rec.buf[i] ^= 0x01;
3665
3666 /* Encrypt */
3667 TEST_EQUAL( 0, mbedtls_cipher_crypt( &t0.cipher_ctx_enc,
3668 t0.iv_enc, t0.ivlen,
3669 rec.buf + rec.data_offset, rec.data_len,
3670 rec.buf + rec.data_offset, &olen ) );
3671 rec.data_offset -= t0.ivlen;
3672 rec.data_len += t0.ivlen;
3673
3674 /* Decrypt and expect failure */
3675 TEST_EQUAL( MBEDTLS_ERR_SSL_INVALID_MAC,
3676 mbedtls_ssl_decrypt_buf( &ssl, &t1, &rec ) );
3677 }
3678
3679 /*
3680 * Use larger values of the padding bytes - with small buffers, this tests
3681 * the case where the announced padlen would be larger than the buffer
3682 * (and before that, than the buffer minus the size of the MAC), to make
3683 * sure our padding checking code does not perform any out-of-bounds reads
3684 * in this case. (With larger buffers, ie when the plaintext is long or
3685 * maximal length padding is used, this is less relevant but still doesn't
3686 * hurt to test.)
3687 *
3688 * (Start the loop with correct padding, just to double-check that record
3689 * saving did work, and that we're overwriting the correct bytes.)
3690 */
Manuel Pégourié-Gonnard4adc04a2020-07-16 10:00:48 +02003691 for( i = padlen; i <= pad_max_len; i++ )
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003692 {
Chris Jones9634bb12021-01-20 15:56:42 +00003693 mbedtls_test_set_step( i );
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003694
3695 /* Restore correct pre-encryption record */
3696 rec = rec_save;
3697 rec.buf = buf;
3698 memcpy( buf, buf_save, buflen );
3699
3700 /* Set padding bytes to new value */
3701 memset( buf + buflen - padlen - 1, i, padlen + 1 );
3702
3703 /* Encrypt */
3704 TEST_EQUAL( 0, mbedtls_cipher_crypt( &t0.cipher_ctx_enc,
3705 t0.iv_enc, t0.ivlen,
3706 rec.buf + rec.data_offset, rec.data_len,
3707 rec.buf + rec.data_offset, &olen ) );
3708 rec.data_offset -= t0.ivlen;
3709 rec.data_len += t0.ivlen;
3710
3711 /* Decrypt and expect failure except the first time */
3712 exp_ret = ( i == padlen ) ? 0 : MBEDTLS_ERR_SSL_INVALID_MAC;
3713 TEST_EQUAL( exp_ret, mbedtls_ssl_decrypt_buf( &ssl, &t1, &rec ) );
3714 }
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003715
3716exit:
3717 mbedtls_ssl_free( &ssl );
3718 mbedtls_ssl_transform_free( &t0 );
3719 mbedtls_ssl_transform_free( &t1 );
3720 mbedtls_free( buf );
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003721 mbedtls_free( buf_save );
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003722}
3723/* END_CASE */
3724
Hanno Becker39ff4922020-08-21 13:36:56 +01003725/* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
3726void ssl_tls1_3_hkdf_expand_label( int hash_alg,
3727 data_t *secret,
Hanno Becker70d7fb02020-09-09 10:11:21 +01003728 int label_idx,
Hanno Becker39ff4922020-08-21 13:36:56 +01003729 data_t *ctx,
3730 int desired_length,
3731 data_t *expected )
3732{
3733 unsigned char dst[ 100 ];
3734
Hanno Becker70d7fb02020-09-09 10:11:21 +01003735 unsigned char const *lbl = NULL;
3736 size_t lbl_len;
Hanno Becker1413bd82020-09-09 12:46:09 +01003737#define MBEDTLS_SSL_TLS1_3_LABEL( name, string ) \
3738 if( label_idx == (int) tls1_3_label_ ## name ) \
Hanno Becker70d7fb02020-09-09 10:11:21 +01003739 { \
3740 lbl = mbedtls_ssl_tls1_3_labels.name; \
3741 lbl_len = sizeof( mbedtls_ssl_tls1_3_labels.name ); \
3742 }
3743MBEDTLS_SSL_TLS1_3_LABEL_LIST
3744#undef MBEDTLS_SSL_TLS1_3_LABEL
3745 TEST_ASSERT( lbl != NULL );
Hanno Becker39ff4922020-08-21 13:36:56 +01003746
3747 /* Check sanity of test parameters. */
3748 TEST_ASSERT( (size_t) desired_length <= sizeof(dst) );
3749 TEST_ASSERT( (size_t) desired_length == expected->len );
3750
3751 TEST_ASSERT( mbedtls_ssl_tls1_3_hkdf_expand_label(
3752 (mbedtls_md_type_t) hash_alg,
3753 secret->x, secret->len,
Hanno Becker70d7fb02020-09-09 10:11:21 +01003754 lbl, lbl_len,
Hanno Becker39ff4922020-08-21 13:36:56 +01003755 ctx->x, ctx->len,
3756 dst, desired_length ) == 0 );
3757
Hanno Beckerfb080962020-09-08 10:58:42 +01003758 ASSERT_COMPARE( dst, (size_t) desired_length,
3759 expected->x, (size_t) expected->len );
Hanno Becker39ff4922020-08-21 13:36:56 +01003760}
3761/* END_CASE */
3762
Hanno Becker19498f82020-08-21 13:37:08 +01003763/* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
3764void ssl_tls1_3_traffic_key_generation( int hash_alg,
3765 data_t *server_secret,
3766 data_t *client_secret,
3767 int desired_iv_len,
3768 int desired_key_len,
3769 data_t *expected_server_write_key,
3770 data_t *expected_server_write_iv,
3771 data_t *expected_client_write_key,
3772 data_t *expected_client_write_iv )
3773{
3774 mbedtls_ssl_key_set keys;
3775
3776 /* Check sanity of test parameters. */
3777 TEST_ASSERT( client_secret->len == server_secret->len );
3778 TEST_ASSERT( expected_client_write_iv->len == expected_server_write_iv->len &&
3779 expected_client_write_iv->len == (size_t) desired_iv_len );
3780 TEST_ASSERT( expected_client_write_key->len == expected_server_write_key->len &&
3781 expected_client_write_key->len == (size_t) desired_key_len );
3782
3783 TEST_ASSERT( mbedtls_ssl_tls1_3_make_traffic_keys(
3784 (mbedtls_md_type_t) hash_alg,
3785 client_secret->x,
3786 server_secret->x,
3787 client_secret->len /* == server_secret->len */,
3788 desired_key_len, desired_iv_len,
3789 &keys ) == 0 );
3790
Hanno Beckerfb080962020-09-08 10:58:42 +01003791 ASSERT_COMPARE( keys.client_write_key,
Hanno Becker493ea7f2020-09-08 11:01:00 +01003792 keys.key_len,
Hanno Beckerfb080962020-09-08 10:58:42 +01003793 expected_client_write_key->x,
3794 (size_t) desired_key_len );
3795 ASSERT_COMPARE( keys.server_write_key,
Hanno Becker493ea7f2020-09-08 11:01:00 +01003796 keys.key_len,
Hanno Beckerfb080962020-09-08 10:58:42 +01003797 expected_server_write_key->x,
3798 (size_t) desired_key_len );
3799 ASSERT_COMPARE( keys.client_write_iv,
Hanno Becker493ea7f2020-09-08 11:01:00 +01003800 keys.iv_len,
Hanno Beckerfb080962020-09-08 10:58:42 +01003801 expected_client_write_iv->x,
3802 (size_t) desired_iv_len );
3803 ASSERT_COMPARE( keys.server_write_iv,
Hanno Becker493ea7f2020-09-08 11:01:00 +01003804 keys.iv_len,
Hanno Beckerfb080962020-09-08 10:58:42 +01003805 expected_server_write_iv->x,
3806 (size_t) desired_iv_len );
Hanno Becker19498f82020-08-21 13:37:08 +01003807}
3808/* END_CASE */
3809
Hanno Beckere4849d12020-08-21 14:14:14 +01003810/* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
3811void ssl_tls1_3_derive_secret( int hash_alg,
3812 data_t *secret,
Hanno Becker70d7fb02020-09-09 10:11:21 +01003813 int label_idx,
Hanno Beckere4849d12020-08-21 14:14:14 +01003814 data_t *ctx,
3815 int desired_length,
3816 int already_hashed,
3817 data_t *expected )
3818{
3819 unsigned char dst[ 100 ];
3820
Hanno Becker70d7fb02020-09-09 10:11:21 +01003821 unsigned char const *lbl = NULL;
3822 size_t lbl_len;
Hanno Becker1413bd82020-09-09 12:46:09 +01003823#define MBEDTLS_SSL_TLS1_3_LABEL( name, string ) \
3824 if( label_idx == (int) tls1_3_label_ ## name ) \
Hanno Becker70d7fb02020-09-09 10:11:21 +01003825 { \
3826 lbl = mbedtls_ssl_tls1_3_labels.name; \
3827 lbl_len = sizeof( mbedtls_ssl_tls1_3_labels.name ); \
3828 }
3829MBEDTLS_SSL_TLS1_3_LABEL_LIST
3830#undef MBEDTLS_SSL_TLS1_3_LABEL
3831 TEST_ASSERT( lbl != NULL );
3832
Hanno Beckere4849d12020-08-21 14:14:14 +01003833 /* Check sanity of test parameters. */
3834 TEST_ASSERT( (size_t) desired_length <= sizeof(dst) );
3835 TEST_ASSERT( (size_t) desired_length == expected->len );
3836
3837 TEST_ASSERT( mbedtls_ssl_tls1_3_derive_secret(
3838 (mbedtls_md_type_t) hash_alg,
3839 secret->x, secret->len,
Hanno Becker70d7fb02020-09-09 10:11:21 +01003840 lbl, lbl_len,
Hanno Beckere4849d12020-08-21 14:14:14 +01003841 ctx->x, ctx->len,
3842 already_hashed,
3843 dst, desired_length ) == 0 );
3844
Hanno Beckerfb080962020-09-08 10:58:42 +01003845 ASSERT_COMPARE( dst, desired_length,
3846 expected->x, desired_length );
Hanno Beckere4849d12020-08-21 14:14:14 +01003847}
3848/* END_CASE */
3849
Hanno Becker2d2c3eb2020-08-20 14:54:24 +01003850/* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
3851void ssl_tls1_3_key_evolution( int hash_alg,
3852 data_t *secret,
3853 data_t *input,
3854 data_t *expected )
3855{
3856 unsigned char secret_new[ MBEDTLS_MD_MAX_SIZE ];
3857
3858 TEST_ASSERT( mbedtls_ssl_tls1_3_evolve_secret(
3859 (mbedtls_md_type_t) hash_alg,
3860 secret->len ? secret->x : NULL,
3861 input->len ? input->x : NULL, input->len,
3862 secret_new ) == 0 );
3863
Hanno Beckerfb080962020-09-08 10:58:42 +01003864 ASSERT_COMPARE( secret_new, (size_t) expected->len,
3865 expected->x, (size_t) expected->len );
Hanno Becker2d2c3eb2020-08-20 14:54:24 +01003866}
3867/* END_CASE */
3868
Ron Eldor824ad7b2019-05-13 14:09:00 +03003869/* BEGIN_CASE */
3870void ssl_tls_prf( int type, data_t * secret, data_t * random,
Ronald Cronac6ae352020-06-26 14:33:03 +02003871 char *label, data_t *result_str, int exp_ret )
Ron Eldor824ad7b2019-05-13 14:09:00 +03003872{
3873 unsigned char *output;
3874
Ronald Cronac6ae352020-06-26 14:33:03 +02003875 output = mbedtls_calloc( 1, result_str->len );
Ron Eldor824ad7b2019-05-13 14:09:00 +03003876 if( output == NULL )
3877 goto exit;
3878
Gilles Peskine9de97e22021-02-02 21:00:11 +01003879 USE_PSA_INIT( );
Ron Eldor6b9b1b82019-05-15 17:04:33 +03003880
Ron Eldor824ad7b2019-05-13 14:09:00 +03003881 TEST_ASSERT( mbedtls_ssl_tls_prf( type, secret->x, secret->len,
3882 label, random->x, random->len,
Ronald Cronac6ae352020-06-26 14:33:03 +02003883 output, result_str->len ) == exp_ret );
Ron Eldor824ad7b2019-05-13 14:09:00 +03003884
3885 if( exp_ret == 0 )
3886 {
Ronald Cronac6ae352020-06-26 14:33:03 +02003887 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
3888 result_str->len, result_str->len ) == 0 );
Ron Eldor824ad7b2019-05-13 14:09:00 +03003889 }
3890exit:
3891
3892 mbedtls_free( output );
Gilles Peskine1f186ff2021-02-02 21:04:06 +01003893 USE_PSA_DONE( );
Ron Eldor824ad7b2019-05-13 14:09:00 +03003894}
3895/* END_CASE */
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02003896
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02003897/* BEGIN_CASE */
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02003898void ssl_serialize_session_save_load( int ticket_len, char *crt_file )
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02003899{
3900 mbedtls_ssl_session original, restored;
3901 unsigned char *buf = NULL;
3902 size_t len;
3903
3904 /*
3905 * Test that a save-load pair is the identity
3906 */
3907
3908 mbedtls_ssl_session_init( &original );
3909 mbedtls_ssl_session_init( &restored );
3910
3911 /* Prepare a dummy session to work on */
3912 TEST_ASSERT( ssl_populate_session( &original, ticket_len, crt_file ) == 0 );
3913
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02003914 /* Serialize it */
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02003915 TEST_ASSERT( mbedtls_ssl_session_save( &original, NULL, 0, &len )
3916 == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3917 TEST_ASSERT( ( buf = mbedtls_calloc( 1, len ) ) != NULL );
3918 TEST_ASSERT( mbedtls_ssl_session_save( &original, buf, len, &len )
3919 == 0 );
3920
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02003921 /* Restore session from serialized data */
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02003922 TEST_ASSERT( mbedtls_ssl_session_load( &restored, buf, len) == 0 );
3923
3924 /*
3925 * Make sure both session structures are identical
3926 */
3927#if defined(MBEDTLS_HAVE_TIME)
3928 TEST_ASSERT( original.start == restored.start );
3929#endif
3930 TEST_ASSERT( original.ciphersuite == restored.ciphersuite );
3931 TEST_ASSERT( original.compression == restored.compression );
3932 TEST_ASSERT( original.id_len == restored.id_len );
3933 TEST_ASSERT( memcmp( original.id,
3934 restored.id, sizeof( original.id ) ) == 0 );
3935 TEST_ASSERT( memcmp( original.master,
3936 restored.master, sizeof( original.master ) ) == 0 );
3937
Andrzej Kurek9155e7f2022-10-18 09:36:19 -04003938#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02003939#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02003940 TEST_ASSERT( ( original.peer_cert == NULL ) ==
3941 ( restored.peer_cert == NULL ) );
3942 if( original.peer_cert != NULL )
3943 {
3944 TEST_ASSERT( original.peer_cert->raw.len ==
3945 restored.peer_cert->raw.len );
3946 TEST_ASSERT( memcmp( original.peer_cert->raw.p,
3947 restored.peer_cert->raw.p,
3948 original.peer_cert->raw.len ) == 0 );
3949 }
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02003950#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
3951 TEST_ASSERT( original.peer_cert_digest_type ==
3952 restored.peer_cert_digest_type );
3953 TEST_ASSERT( original.peer_cert_digest_len ==
3954 restored.peer_cert_digest_len );
3955 TEST_ASSERT( ( original.peer_cert_digest == NULL ) ==
3956 ( restored.peer_cert_digest == NULL ) );
3957 if( original.peer_cert_digest != NULL )
3958 {
3959 TEST_ASSERT( memcmp( original.peer_cert_digest,
3960 restored.peer_cert_digest,
3961 original.peer_cert_digest_len ) == 0 );
3962 }
3963#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Andrzej Kurek9155e7f2022-10-18 09:36:19 -04003964#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02003965 TEST_ASSERT( original.verify_result == restored.verify_result );
3966
3967#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
3968 TEST_ASSERT( original.ticket_len == restored.ticket_len );
3969 if( original.ticket_len != 0 )
3970 {
3971 TEST_ASSERT( original.ticket != NULL );
3972 TEST_ASSERT( restored.ticket != NULL );
3973 TEST_ASSERT( memcmp( original.ticket,
3974 restored.ticket, original.ticket_len ) == 0 );
3975 }
3976 TEST_ASSERT( original.ticket_lifetime == restored.ticket_lifetime );
3977#endif
3978
3979#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3980 TEST_ASSERT( original.mfl_code == restored.mfl_code );
3981#endif
3982
3983#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
3984 TEST_ASSERT( original.trunc_hmac == restored.trunc_hmac );
3985#endif
3986
3987#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
3988 TEST_ASSERT( original.encrypt_then_mac == restored.encrypt_then_mac );
3989#endif
3990
3991exit:
3992 mbedtls_ssl_session_free( &original );
3993 mbedtls_ssl_session_free( &restored );
3994 mbedtls_free( buf );
3995}
3996/* END_CASE */
3997
Manuel Pégourié-Gonnardaa755832019-06-03 10:53:47 +02003998/* BEGIN_CASE */
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02003999void ssl_serialize_session_load_save( int ticket_len, char *crt_file )
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02004000{
4001 mbedtls_ssl_session session;
4002 unsigned char *buf1 = NULL, *buf2 = NULL;
4003 size_t len0, len1, len2;
4004
4005 /*
4006 * Test that a load-save pair is the identity
4007 */
4008
4009 mbedtls_ssl_session_init( &session );
4010
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02004011 /* Prepare a dummy session to work on */
Manuel Pégourié-Gonnard6b840702019-05-24 09:40:17 +02004012 TEST_ASSERT( ssl_populate_session( &session, ticket_len, crt_file ) == 0 );
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02004013
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02004014 /* Get desired buffer size for serializing */
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02004015 TEST_ASSERT( mbedtls_ssl_session_save( &session, NULL, 0, &len0 )
4016 == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4017
4018 /* Allocate first buffer */
4019 buf1 = mbedtls_calloc( 1, len0 );
4020 TEST_ASSERT( buf1 != NULL );
4021
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02004022 /* Serialize to buffer and free live session */
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02004023 TEST_ASSERT( mbedtls_ssl_session_save( &session, buf1, len0, &len1 )
4024 == 0 );
4025 TEST_ASSERT( len0 == len1 );
4026 mbedtls_ssl_session_free( &session );
4027
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02004028 /* Restore session from serialized data */
Manuel Pégourié-Gonnard220403b2019-05-24 09:54:21 +02004029 TEST_ASSERT( mbedtls_ssl_session_load( &session, buf1, len1 ) == 0 );
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02004030
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02004031 /* Allocate second buffer and serialize to it */
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02004032 buf2 = mbedtls_calloc( 1, len0 );
Manuel Pégourié-Gonnardb4079902019-05-24 09:52:10 +02004033 TEST_ASSERT( buf2 != NULL );
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02004034 TEST_ASSERT( mbedtls_ssl_session_save( &session, buf2, len0, &len2 )
4035 == 0 );
4036
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02004037 /* Make sure both serialized versions are identical */
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02004038 TEST_ASSERT( len1 == len2 );
4039 TEST_ASSERT( memcmp( buf1, buf2, len1 ) == 0 );
4040
4041exit:
4042 mbedtls_ssl_session_free( &session );
4043 mbedtls_free( buf1 );
4044 mbedtls_free( buf2 );
4045}
4046/* END_CASE */
Manuel Pégourié-Gonnardf5fa0aa2019-05-23 10:38:11 +02004047
4048/* BEGIN_CASE */
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02004049void ssl_serialize_session_save_buf_size( int ticket_len, char *crt_file )
Manuel Pégourié-Gonnardf5fa0aa2019-05-23 10:38:11 +02004050{
4051 mbedtls_ssl_session session;
4052 unsigned char *buf = NULL;
4053 size_t good_len, bad_len, test_len;
4054
4055 /*
4056 * Test that session_save() fails cleanly on small buffers
4057 */
4058
4059 mbedtls_ssl_session_init( &session );
4060
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02004061 /* Prepare dummy session and get serialized size */
Manuel Pégourié-Gonnard6b840702019-05-24 09:40:17 +02004062 TEST_ASSERT( ssl_populate_session( &session, ticket_len, crt_file ) == 0 );
Manuel Pégourié-Gonnardf5fa0aa2019-05-23 10:38:11 +02004063 TEST_ASSERT( mbedtls_ssl_session_save( &session, NULL, 0, &good_len )
4064 == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4065
4066 /* Try all possible bad lengths */
4067 for( bad_len = 1; bad_len < good_len; bad_len++ )
4068 {
4069 /* Allocate exact size so that asan/valgrind can detect any overwrite */
4070 mbedtls_free( buf );
4071 TEST_ASSERT( ( buf = mbedtls_calloc( 1, bad_len ) ) != NULL );
4072 TEST_ASSERT( mbedtls_ssl_session_save( &session, buf, bad_len,
4073 &test_len )
4074 == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4075 TEST_ASSERT( test_len == good_len );
4076 }
4077
4078exit:
4079 mbedtls_ssl_session_free( &session );
4080 mbedtls_free( buf );
4081}
4082/* END_CASE */
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02004083
4084/* BEGIN_CASE */
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02004085void ssl_serialize_session_load_buf_size( int ticket_len, char *crt_file )
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02004086{
4087 mbedtls_ssl_session session;
4088 unsigned char *good_buf = NULL, *bad_buf = NULL;
4089 size_t good_len, bad_len;
4090
4091 /*
4092 * Test that session_load() fails cleanly on small buffers
4093 */
4094
4095 mbedtls_ssl_session_init( &session );
4096
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02004097 /* Prepare serialized session data */
Manuel Pégourié-Gonnard6b840702019-05-24 09:40:17 +02004098 TEST_ASSERT( ssl_populate_session( &session, ticket_len, crt_file ) == 0 );
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02004099 TEST_ASSERT( mbedtls_ssl_session_save( &session, NULL, 0, &good_len )
4100 == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4101 TEST_ASSERT( ( good_buf = mbedtls_calloc( 1, good_len ) ) != NULL );
4102 TEST_ASSERT( mbedtls_ssl_session_save( &session, good_buf, good_len,
4103 &good_len ) == 0 );
4104 mbedtls_ssl_session_free( &session );
4105
4106 /* Try all possible bad lengths */
4107 for( bad_len = 0; bad_len < good_len; bad_len++ )
4108 {
4109 /* Allocate exact size so that asan/valgrind can detect any overread */
4110 mbedtls_free( bad_buf );
4111 bad_buf = mbedtls_calloc( 1, bad_len ? bad_len : 1 );
4112 TEST_ASSERT( bad_buf != NULL );
4113 memcpy( bad_buf, good_buf, bad_len );
4114
4115 TEST_ASSERT( mbedtls_ssl_session_load( &session, bad_buf, bad_len )
4116 == MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4117 }
4118
4119exit:
4120 mbedtls_ssl_session_free( &session );
4121 mbedtls_free( good_buf );
4122 mbedtls_free( bad_buf );
4123}
4124/* END_CASE */
Hanno Becker861d0bb2019-05-21 16:39:30 +01004125
Hanno Becker363b6462019-05-29 12:44:28 +01004126/* BEGIN_CASE */
4127void ssl_session_serialize_version_check( int corrupt_major,
Hanno Becker861d0bb2019-05-21 16:39:30 +01004128 int corrupt_minor,
4129 int corrupt_patch,
4130 int corrupt_config )
4131{
Hanno Becker363b6462019-05-29 12:44:28 +01004132 unsigned char serialized_session[ 2048 ];
4133 size_t serialized_session_len;
Hanno Beckerfe1275e2019-05-29 12:45:21 +01004134 unsigned cur_byte;
Hanno Becker861d0bb2019-05-21 16:39:30 +01004135 mbedtls_ssl_session session;
Hanno Beckerfe1275e2019-05-29 12:45:21 +01004136 uint8_t should_corrupt_byte[] = { corrupt_major == 1,
4137 corrupt_minor == 1,
4138 corrupt_patch == 1,
4139 corrupt_config == 1,
4140 corrupt_config == 1 };
4141
Hanno Becker861d0bb2019-05-21 16:39:30 +01004142 mbedtls_ssl_session_init( &session );
4143
Hanno Beckerfe1275e2019-05-29 12:45:21 +01004144 /* Infer length of serialized session. */
Hanno Becker861d0bb2019-05-21 16:39:30 +01004145 TEST_ASSERT( mbedtls_ssl_session_save( &session,
Hanno Becker363b6462019-05-29 12:44:28 +01004146 serialized_session,
4147 sizeof( serialized_session ),
4148 &serialized_session_len ) == 0 );
Hanno Becker861d0bb2019-05-21 16:39:30 +01004149
Hanno Beckerfe1275e2019-05-29 12:45:21 +01004150 mbedtls_ssl_session_free( &session );
Hanno Becker861d0bb2019-05-21 16:39:30 +01004151
Hanno Beckerfe1275e2019-05-29 12:45:21 +01004152 /* Without any modification, we should be able to successfully
Hanno Becker363b6462019-05-29 12:44:28 +01004153 * de-serialize the session - double-check that. */
Hanno Becker861d0bb2019-05-21 16:39:30 +01004154 TEST_ASSERT( mbedtls_ssl_session_load( &session,
Hanno Becker363b6462019-05-29 12:44:28 +01004155 serialized_session,
4156 serialized_session_len ) == 0 );
Hanno Becker861d0bb2019-05-21 16:39:30 +01004157 mbedtls_ssl_session_free( &session );
4158
Hanno Beckerfe1275e2019-05-29 12:45:21 +01004159 /* Go through the bytes in the serialized session header and
4160 * corrupt them bit-by-bit. */
4161 for( cur_byte = 0; cur_byte < sizeof( should_corrupt_byte ); cur_byte++ )
Hanno Becker861d0bb2019-05-21 16:39:30 +01004162 {
Hanno Beckerfe1275e2019-05-29 12:45:21 +01004163 int cur_bit;
4164 unsigned char * const byte = &serialized_session[ cur_byte ];
4165
4166 if( should_corrupt_byte[ cur_byte ] == 0 )
4167 continue;
4168
4169 for( cur_bit = 0; cur_bit < CHAR_BIT; cur_bit++ )
4170 {
4171 unsigned char const corrupted_bit = 0x1u << cur_bit;
4172 /* Modify a single bit in the serialized session. */
4173 *byte ^= corrupted_bit;
4174
4175 /* Attempt to deserialize */
4176 TEST_ASSERT( mbedtls_ssl_session_load( &session,
4177 serialized_session,
4178 serialized_session_len ) ==
Hanno Beckerf9b33032019-06-03 12:58:39 +01004179 MBEDTLS_ERR_SSL_VERSION_MISMATCH );
Hanno Beckerfe1275e2019-05-29 12:45:21 +01004180
4181 /* Undo the change */
4182 *byte ^= corrupted_bit;
4183 }
Hanno Becker861d0bb2019-05-21 16:39:30 +01004184 }
4185
Hanno Becker861d0bb2019-05-21 16:39:30 +01004186}
4187/* END_CASE */
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01004188
Andrzej Kurek9155e7f2022-10-18 09:36:19 -04004189/* BEGIN_CASE depends_on:MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED: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 +01004190void mbedtls_endpoint_sanity( int endpoint_type )
4191{
4192 enum { BUFFSIZE = 1024 };
4193 mbedtls_endpoint ep;
4194 int ret = -1;
4195
Andrzej Kurek15daf502020-02-12 09:17:52 -05004196 ret = mbedtls_endpoint_init( NULL, endpoint_type, MBEDTLS_PK_RSA,
Andrzej Kurek535cd172022-03-08 06:50:12 -05004197 NULL, NULL, NULL, NULL );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01004198 TEST_ASSERT( MBEDTLS_ERR_SSL_BAD_INPUT_DATA == ret );
4199
Andrzej Kurekb2980742020-02-02 19:25:26 -05004200 ret = mbedtls_endpoint_certificate_init( NULL, MBEDTLS_PK_RSA );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01004201 TEST_ASSERT( MBEDTLS_ERR_SSL_BAD_INPUT_DATA == ret );
4202
Andrzej Kurek15daf502020-02-12 09:17:52 -05004203 ret = mbedtls_endpoint_init( &ep, endpoint_type, MBEDTLS_PK_RSA,
Andrzej Kurek535cd172022-03-08 06:50:12 -05004204 NULL, NULL, NULL, NULL );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01004205 TEST_ASSERT( ret == 0 );
4206
4207exit:
Andrzej Kurek15daf502020-02-12 09:17:52 -05004208 mbedtls_endpoint_free( &ep, NULL );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01004209}
4210/* END_CASE */
4211
Andrzej Kurek0d3b3a82022-10-20 14:50:35 -04004212/* BEGIN_CASE depends_on:MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED: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:MBEDTLS_ECP_C */
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01004213void move_handshake_to_state(int endpoint_type, int state, int need_pass)
4214{
4215 enum { BUFFSIZE = 1024 };
4216 mbedtls_endpoint base_ep, second_ep;
4217 int ret = -1;
4218
Andrzej Kurek0d2982b2022-10-18 07:55:46 -04004219 mbedtls_platform_zeroize( &base_ep, sizeof(base_ep) );
4220 mbedtls_platform_zeroize( &second_ep, sizeof(second_ep) );
4221
Andrzej Kurek15daf502020-02-12 09:17:52 -05004222 ret = mbedtls_endpoint_init( &base_ep, endpoint_type, MBEDTLS_PK_RSA,
Andrzej Kurek535cd172022-03-08 06:50:12 -05004223 NULL, NULL, NULL, NULL );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01004224 TEST_ASSERT( ret == 0 );
4225
4226 ret = mbedtls_endpoint_init( &second_ep,
4227 ( endpoint_type == MBEDTLS_SSL_IS_SERVER ) ?
Andrzej Kurekb2980742020-02-02 19:25:26 -05004228 MBEDTLS_SSL_IS_CLIENT : MBEDTLS_SSL_IS_SERVER,
Andrzej Kurek535cd172022-03-08 06:50:12 -05004229 MBEDTLS_PK_RSA, NULL, NULL, NULL, NULL );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01004230 TEST_ASSERT( ret == 0 );
4231
4232 ret = mbedtls_mock_socket_connect( &(base_ep.socket),
4233 &(second_ep.socket),
4234 BUFFSIZE );
4235 TEST_ASSERT( ret == 0 );
4236
4237 ret = mbedtls_move_handshake_to_state( &(base_ep.ssl),
4238 &(second_ep.ssl),
4239 state );
4240 if( need_pass )
4241 {
4242 TEST_ASSERT( ret == 0 );
4243 TEST_ASSERT( base_ep.ssl.state == state );
4244 }
4245 else
4246 {
4247 TEST_ASSERT( ret != 0 );
4248 TEST_ASSERT( base_ep.ssl.state != state );
4249 }
4250
4251exit:
Andrzej Kurek15daf502020-02-12 09:17:52 -05004252 mbedtls_endpoint_free( &base_ep, NULL );
4253 mbedtls_endpoint_free( &second_ep, NULL );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01004254}
4255/* END_CASE */
Andrzej Kurekf40daa32020-02-04 09:00:01 -05004256
Andrzej Kurek0d3b3a82022-10-20 14:50:35 -04004257/* BEGIN_CASE depends_on:MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C:MBEDTLS_ECP_C */
Paul Elliottc8570442020-04-15 17:00:50 +01004258void handshake_version( int dtls, int client_min_version, int client_max_version,
4259 int server_min_version, int server_max_version,
4260 int expected_negotiated_version )
Andrzej Kurekf40daa32020-02-04 09:00:01 -05004261{
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004262 handshake_test_options options;
4263 init_handshake_options( &options );
Andrzej Kurekda2b6782020-02-12 07:56:36 -05004264
Paul Elliottc8570442020-04-15 17:00:50 +01004265 options.client_min_version = client_min_version;
4266 options.client_max_version = client_max_version;
4267 options.server_min_version = server_min_version;
4268 options.server_max_version = server_max_version;
4269
4270 options.expected_negotiated_version = expected_negotiated_version;
4271
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004272 options.dtls = dtls;
Piotr Nowicki438bf3b2020-03-10 12:59:10 +01004273 /* By default, SSLv3.0 and TLSv1.0 use 1/n-1 splitting when sending data, so
4274 * the number of fragments will be twice as big. */
Paul Elliottc8570442020-04-15 17:00:50 +01004275 if( expected_negotiated_version == MBEDTLS_SSL_MINOR_VERSION_0 ||
4276 expected_negotiated_version == MBEDTLS_SSL_MINOR_VERSION_1 )
Andrzej Kurek941962e2020-02-07 09:20:32 -05004277 {
Piotr Nowicki438bf3b2020-03-10 12:59:10 +01004278 options.expected_cli_fragments = 2;
4279 options.expected_srv_fragments = 2;
Andrzej Kurek941962e2020-02-07 09:20:32 -05004280 }
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004281 perform_handshake( &options );
Andrzej Kurekf40daa32020-02-04 09:00:01 -05004282
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004283 /* The goto below is used to avoid an "unused label" warning.*/
4284 goto exit;
4285}
4286/* END_CASE */
Andrzej Kurek9e9efdc2020-02-26 05:25:23 -05004287
Andrzej Kurek9155e7f2022-10-18 09:36:19 -04004288/* BEGIN_CASE depends_on:MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED:!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 -05004289void handshake_psk_cipher( char* cipher, int pk_alg, data_t *psk_str, int dtls )
4290{
4291 handshake_test_options options;
4292 init_handshake_options( &options );
Andrzej Kurekf40daa32020-02-04 09:00:01 -05004293
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004294 options.cipher = cipher;
4295 options.dtls = dtls;
4296 options.psk_str = psk_str;
4297 options.pk_alg = pk_alg;
Andrzej Kurekcc5169c2020-02-04 09:04:56 -05004298
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004299 perform_handshake( &options );
Andrzej Kurek316da1f2020-02-26 09:03:47 -05004300
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004301 /* The goto below is used to avoid an "unused label" warning.*/
4302 goto exit;
4303}
4304/* END_CASE */
Andrzej Kurek316da1f2020-02-26 09:03:47 -05004305
Andrzej Kurek9155e7f2022-10-18 09:36:19 -04004306/* BEGIN_CASE depends_on:MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED:!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 -05004307void handshake_cipher( char* cipher, int pk_alg, int dtls )
4308{
4309 test_handshake_psk_cipher( cipher, pk_alg, NULL, dtls );
Andrzej Kurekf40daa32020-02-04 09:00:01 -05004310
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004311 /* The goto below is used to avoid an "unused label" warning.*/
4312 goto exit;
4313}
4314/* END_CASE */
Andrzej Kurekf40daa32020-02-04 09:00:01 -05004315
Andrzej Kurek9155e7f2022-10-18 09:36:19 -04004316/* BEGIN_CASE depends_on:MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED:!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 -05004317void app_data( int mfl, int cli_msg_len, int srv_msg_len,
4318 int expected_cli_fragments,
4319 int expected_srv_fragments, int dtls )
4320{
4321 handshake_test_options options;
4322 init_handshake_options( &options );
Andrzej Kurekda2b6782020-02-12 07:56:36 -05004323
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004324 options.mfl = mfl;
4325 options.cli_msg_len = cli_msg_len;
4326 options.srv_msg_len = srv_msg_len;
4327 options.expected_cli_fragments = expected_cli_fragments;
4328 options.expected_srv_fragments = expected_srv_fragments;
4329 options.dtls = dtls;
Andrzej Kurekda2b6782020-02-12 07:56:36 -05004330
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004331 perform_handshake( &options );
4332 /* The goto below is used to avoid an "unused label" warning.*/
4333 goto exit;
4334}
4335/* END_CASE */
Andrzej Kurekda2b6782020-02-12 07:56:36 -05004336
Andrzej Kurek0d3b3a82022-10-20 14:50:35 -04004337/* BEGIN_CASE depends_on:MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED:!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:MBEDTLS_ECP_C */
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004338void app_data_tls( int mfl, int cli_msg_len, int srv_msg_len,
4339 int expected_cli_fragments,
4340 int expected_srv_fragments )
4341{
4342 test_app_data( mfl, cli_msg_len, srv_msg_len, expected_cli_fragments,
4343 expected_srv_fragments, 0 );
4344 /* The goto below is used to avoid an "unused label" warning.*/
4345 goto exit;
4346}
4347/* END_CASE */
Andrzej Kurekda2b6782020-02-12 07:56:36 -05004348
Andrzej Kurek9155e7f2022-10-18 09:36:19 -04004349/* BEGIN_CASE depends_on:MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED:!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 -05004350void app_data_dtls( int mfl, int cli_msg_len, int srv_msg_len,
4351 int expected_cli_fragments,
4352 int expected_srv_fragments )
4353{
4354 test_app_data( mfl, cli_msg_len, srv_msg_len, expected_cli_fragments,
4355 expected_srv_fragments, 1 );
4356 /* The goto below is used to avoid an "unused label" warning.*/
4357 goto exit;
4358}
4359/* END_CASE */
Andrzej Kurekda2b6782020-02-12 07:56:36 -05004360
Andrzej Kurek9155e7f2022-10-18 09:36:19 -04004361/* BEGIN_CASE depends_on:MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED:!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 -05004362void handshake_serialization( )
4363{
4364 handshake_test_options options;
4365 init_handshake_options( &options );
Andrzej Kurekda2b6782020-02-12 07:56:36 -05004366
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004367 options.serialize = 1;
4368 options.dtls = 1;
4369 perform_handshake( &options );
4370 /* The goto below is used to avoid an "unused label" warning.*/
4371 goto exit;
4372}
4373/* END_CASE */
Andrzej Kurekda2b6782020-02-12 07:56:36 -05004374
Andrzej Kurek9155e7f2022-10-18 09:36:19 -04004375/* BEGIN_CASE depends_on:MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED:!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 +01004376void handshake_fragmentation( int mfl, int expected_srv_hs_fragmentation, int expected_cli_hs_fragmentation)
4377{
4378 handshake_test_options options;
4379 log_pattern srv_pattern, cli_pattern;
4380
4381 srv_pattern.pattern = cli_pattern.pattern = "found fragmented DTLS handshake";
4382 srv_pattern.counter = 0;
4383 cli_pattern.counter = 0;
4384
4385 init_handshake_options( &options );
4386 options.dtls = 1;
4387 options.mfl = mfl;
Darryl Greenaad82f92019-12-02 10:53:11 +00004388 /* Set cipher to one using CBC so that record splitting can be tested */
4389 options.cipher = "TLS-DHE-RSA-WITH-AES-256-CBC-SHA256";
Piotr Nowickibde7ee82020-02-21 10:59:50 +01004390 options.srv_auth_mode = MBEDTLS_SSL_VERIFY_REQUIRED;
4391 options.srv_log_obj = &srv_pattern;
4392 options.cli_log_obj = &cli_pattern;
4393 options.srv_log_fun = log_analyzer;
4394 options.cli_log_fun = log_analyzer;
4395
4396 perform_handshake( &options );
4397
4398 /* Test if the server received a fragmented handshake */
4399 if( expected_srv_hs_fragmentation )
4400 {
4401 TEST_ASSERT( srv_pattern.counter >= 1 );
4402 }
4403 /* Test if the client received a fragmented handshake */
4404 if( expected_cli_hs_fragmentation )
4405 {
4406 TEST_ASSERT( cli_pattern.counter >= 1 );
4407 }
4408}
4409/* END_CASE */
4410
Andrzej Kurek9155e7f2022-10-18 09:36:19 -04004411/* BEGIN_CASE depends_on:MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED:!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 -05004412void renegotiation( int legacy_renegotiation )
4413{
4414 handshake_test_options options;
4415 init_handshake_options( &options );
Andrzej Kurekda2b6782020-02-12 07:56:36 -05004416
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004417 options.renegotiate = 1;
4418 options.legacy_renegotiation = legacy_renegotiation;
4419 options.dtls = 1;
Andrzej Kurek316da1f2020-02-26 09:03:47 -05004420
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004421 perform_handshake( &options );
4422 /* The goto below is used to avoid an "unused label" warning.*/
4423 goto exit;
Andrzej Kurekf40daa32020-02-04 09:00:01 -05004424}
4425/* END_CASE */
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004426
Andrzej Kurek9155e7f2022-10-18 09:36:19 -04004427/* BEGIN_CASE depends_on:MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED:!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 -05004428void resize_buffers( int mfl, int renegotiation, int legacy_renegotiation,
Andrzej Kurek8ea68722020-04-03 06:40:47 -04004429 int serialize, int dtls, char *cipher )
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004430{
4431 handshake_test_options options;
4432 init_handshake_options( &options );
4433
4434 options.mfl = mfl;
Andrzej Kurek8ea68722020-04-03 06:40:47 -04004435 options.cipher = cipher;
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004436 options.renegotiate = renegotiation;
4437 options.legacy_renegotiation = legacy_renegotiation;
4438 options.serialize = serialize;
4439 options.dtls = dtls;
4440 options.resize_buffers = 1;
4441
4442 perform_handshake( &options );
4443 /* The goto below is used to avoid an "unused label" warning.*/
4444 goto exit;
4445}
4446/* END_CASE */
4447
Andrzej Kurek9155e7f2022-10-18 09:36:19 -04004448/* BEGIN_CASE depends_on:MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED:!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 -05004449void resize_buffers_serialize_mfl( int mfl )
4450{
Andrzej Kurek8ea68722020-04-03 06:40:47 -04004451 test_resize_buffers( mfl, 0, MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION, 1, 1,
4452 (char *) "" );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004453
4454 /* The goto below is used to avoid an "unused label" warning.*/
4455 goto exit;
4456}
4457/* END_CASE */
4458
Andrzej Kurek9155e7f2022-10-18 09:36:19 -04004459/* BEGIN_CASE depends_on:MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED:!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 -04004460void resize_buffers_renegotiate_mfl( int mfl, int legacy_renegotiation,
4461 char *cipher )
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004462{
Andrzej Kurek8ea68722020-04-03 06:40:47 -04004463 test_resize_buffers( mfl, 1, legacy_renegotiation, 0, 1, cipher );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004464
4465 /* The goto below is used to avoid an "unused label" warning.*/
4466 goto exit;
4467}
4468/* END_CASE */
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02004469
Manuel Pégourié-Gonnarded0e8642020-07-21 11:20:30 +02004470/* BEGIN_CASE depends_on:MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC:MBEDTLS_TEST_HOOKS */
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02004471void ssl_cf_hmac( int hash )
4472{
4473 /*
Gabor Mezei18a44942021-10-20 11:59:27 +02004474 * Test the function mbedtls_ct_hmac() against a reference
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02004475 * implementation.
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02004476 */
4477 mbedtls_md_context_t ctx, ref_ctx;
4478 const mbedtls_md_info_t *md_info;
4479 size_t out_len, block_size;
4480 size_t min_in_len, in_len, max_in_len, i;
4481 /* TLS additional data is 13 bytes (hence the "lucky 13" name) */
4482 unsigned char add_data[13];
4483 unsigned char ref_out[MBEDTLS_MD_MAX_SIZE];
4484 unsigned char *data = NULL;
4485 unsigned char *out = NULL;
4486 unsigned char rec_num = 0;
4487
4488 mbedtls_md_init( &ctx );
4489 mbedtls_md_init( &ref_ctx );
4490
4491 md_info = mbedtls_md_info_from_type( hash );
4492 TEST_ASSERT( md_info != NULL );
4493 out_len = mbedtls_md_get_size( md_info );
4494 TEST_ASSERT( out_len != 0 );
4495 block_size = hash == MBEDTLS_MD_SHA384 ? 128 : 64;
4496
4497 /* Use allocated out buffer to catch overwrites */
4498 ASSERT_ALLOC( out, out_len );
4499
4500 /* Set up contexts with the given hash and a dummy key */
4501 TEST_EQUAL( 0, mbedtls_md_setup( &ctx, md_info, 1 ) );
4502 TEST_EQUAL( 0, mbedtls_md_setup( &ref_ctx, md_info, 1 ) );
4503 memset( ref_out, 42, sizeof( ref_out ) );
4504 TEST_EQUAL( 0, mbedtls_md_hmac_starts( &ctx, ref_out, out_len ) );
4505 TEST_EQUAL( 0, mbedtls_md_hmac_starts( &ref_ctx, ref_out, out_len ) );
4506 memset( ref_out, 0, sizeof( ref_out ) );
4507
4508 /*
4509 * Test all possible lengths up to a point. The difference between
4510 * max_in_len and min_in_len is at most 255, and make sure they both vary
4511 * by at least one block size.
4512 */
4513 for( max_in_len = 0; max_in_len <= 255 + block_size; max_in_len++ )
4514 {
Chris Jones9634bb12021-01-20 15:56:42 +00004515 mbedtls_test_set_step( max_in_len * 10000 );
Manuel Pégourié-Gonnardca8287c2020-07-22 10:29:39 +02004516
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02004517 /* Use allocated in buffer to catch overreads */
Manuel Pégourié-Gonnardc3219002020-07-22 10:32:52 +02004518 ASSERT_ALLOC( data, max_in_len );
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02004519
4520 min_in_len = max_in_len > 255 ? max_in_len - 255 : 0;
4521 for( in_len = min_in_len; in_len <= max_in_len; in_len++ )
4522 {
Chris Jones9634bb12021-01-20 15:56:42 +00004523 mbedtls_test_set_step( max_in_len * 10000 + in_len );
Manuel Pégourié-Gonnardca8287c2020-07-22 10:29:39 +02004524
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02004525 /* Set up dummy data and add_data */
4526 rec_num++;
4527 memset( add_data, rec_num, sizeof( add_data ) );
4528 for( i = 0; i < in_len; i++ )
4529 data[i] = ( i & 0xff ) ^ rec_num;
4530
4531 /* Get the function's result */
Manuel Pégourié-Gonnard9670a592020-07-10 10:21:46 +02004532 TEST_CF_SECRET( &in_len, sizeof( in_len ) );
Gabor Mezei18a44942021-10-20 11:59:27 +02004533 TEST_EQUAL( 0, mbedtls_ct_hmac( &ctx, add_data, sizeof( add_data ),
gabor-mezei-arme41e3e82021-09-28 16:14:47 +02004534 data, in_len,
4535 min_in_len, max_in_len,
4536 out ) );
Manuel Pégourié-Gonnard9670a592020-07-10 10:21:46 +02004537 TEST_CF_PUBLIC( &in_len, sizeof( in_len ) );
4538 TEST_CF_PUBLIC( out, out_len );
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02004539
4540 /* Compute the reference result */
4541 TEST_EQUAL( 0, mbedtls_md_hmac_update( &ref_ctx, add_data,
4542 sizeof( add_data ) ) );
4543 TEST_EQUAL( 0, mbedtls_md_hmac_update( &ref_ctx, data, in_len ) );
4544 TEST_EQUAL( 0, mbedtls_md_hmac_finish( &ref_ctx, ref_out ) );
4545 TEST_EQUAL( 0, mbedtls_md_hmac_reset( &ref_ctx ) );
4546
4547 /* Compare */
4548 ASSERT_COMPARE( out, out_len, ref_out, out_len );
4549 }
4550
4551 mbedtls_free( data );
4552 data = NULL;
4553 }
4554
4555exit:
4556 mbedtls_md_free( &ref_ctx );
4557 mbedtls_md_free( &ctx );
4558
4559 mbedtls_free( data );
4560 mbedtls_free( out );
4561}
4562/* END_CASE */
Manuel Pégourié-Gonnard7fe2c5f2020-08-18 12:02:54 +02004563
4564/* BEGIN_CASE depends_on:MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC:MBEDTLS_TEST_HOOKS */
4565void ssl_cf_memcpy_offset( int offset_min, int offset_max, int len )
4566{
4567 unsigned char *dst = NULL;
4568 unsigned char *src = NULL;
4569 size_t src_len = offset_max + len;
4570 size_t secret;
4571
4572 ASSERT_ALLOC( dst, len );
4573 ASSERT_ALLOC( src, src_len );
4574
4575 /* Fill src in a way that we can detect if we copied the right bytes */
4576 mbedtls_test_rnd_std_rand( NULL, src, src_len );
4577
4578 for( secret = offset_min; secret <= (size_t) offset_max; secret++ )
4579 {
Chris Jones9634bb12021-01-20 15:56:42 +00004580 mbedtls_test_set_step( (int) secret );
Manuel Pégourié-Gonnard7fe2c5f2020-08-18 12:02:54 +02004581
4582 TEST_CF_SECRET( &secret, sizeof( secret ) );
Gabor Mezei18a44942021-10-20 11:59:27 +02004583 mbedtls_ct_memcpy_offset( dst, src, secret,
gabor-mezei-arme41e3e82021-09-28 16:14:47 +02004584 offset_min, offset_max, len );
Manuel Pégourié-Gonnard7fe2c5f2020-08-18 12:02:54 +02004585 TEST_CF_PUBLIC( &secret, sizeof( secret ) );
4586 TEST_CF_PUBLIC( dst, len );
4587
4588 ASSERT_COMPARE( dst, len, src + secret, len );
4589 }
4590
4591exit:
4592 mbedtls_free( dst );
4593 mbedtls_free( src );
4594}
4595/* END_CASE */
Andrzej Kurekb4eedf72022-04-15 05:41:14 -04004596
Andrzej Kurek9155e7f2022-10-18 09:36:19 -04004597/* BEGIN_CASE depends_on:MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED:MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_ECP_DP_SECP256R1_ENABLED:MBEDTLS_ENTROPY_C:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_CTR_DRBG_C:MBEDTLS_ECP_C:MBEDTLS_ECDSA_C */
Gilles Peskine6dd489c2022-04-15 05:54:40 -04004598void raw_key_agreement_fail( int bad_server_ecdhe_key )
Andrzej Kurekb4eedf72022-04-15 05:41:14 -04004599{
4600 enum { BUFFSIZE = 17000 };
4601 mbedtls_endpoint client, server;
4602 mbedtls_psa_stats_t stats;
Andrzej Kurek2582ba32022-03-31 06:30:54 -04004603 size_t free_slots_before = -1;
Andrzej Kurekb4eedf72022-04-15 05:41:14 -04004604
Andrzej Kurekb4eedf72022-04-15 05:41:14 -04004605 mbedtls_ecp_group_id curve_list[] = { MBEDTLS_ECP_DP_SECP256R1,
4606 MBEDTLS_ECP_DP_NONE };
Andrzej Kurekb4eedf72022-04-15 05:41:14 -04004607 USE_PSA_INIT( );
Andrzej Kurek0d2982b2022-10-18 07:55:46 -04004608 mbedtls_platform_zeroize( &client, sizeof(client) );
4609 mbedtls_platform_zeroize( &server, sizeof(server) );
Andrzej Kurekb4eedf72022-04-15 05:41:14 -04004610
4611 /* Client side, force SECP256R1 to make one key bitflip fail
Andrzej Kurek9cb14d42022-04-14 08:51:41 -04004612 * the raw key agreement. Flipping the first byte makes the
4613 * required 0x04 identifier invalid. */
Andrzej Kurekee9488d2022-04-15 06:51:56 -04004614 TEST_EQUAL( mbedtls_endpoint_init( &client, MBEDTLS_SSL_IS_CLIENT,
Andrzej Kurek86029e02022-04-15 06:50:56 -04004615 MBEDTLS_PK_ECDSA, NULL, NULL,
Andrzej Kurekee9488d2022-04-15 06:51:56 -04004616 NULL, curve_list ), 0 );
Andrzej Kurekb4eedf72022-04-15 05:41:14 -04004617
4618 /* Server side */
Andrzej Kurekee9488d2022-04-15 06:51:56 -04004619 TEST_EQUAL( mbedtls_endpoint_init( &server, MBEDTLS_SSL_IS_SERVER,
Andrzej Kurek86029e02022-04-15 06:50:56 -04004620 MBEDTLS_PK_ECDSA, NULL, NULL,
Andrzej Kurekee9488d2022-04-15 06:51:56 -04004621 NULL, NULL ), 0 );
Andrzej Kurekb4eedf72022-04-15 05:41:14 -04004622
Andrzej Kurekee9488d2022-04-15 06:51:56 -04004623 TEST_EQUAL( mbedtls_mock_socket_connect( &(client.socket),
Andrzej Kurekb4eedf72022-04-15 05:41:14 -04004624 &(server.socket),
Andrzej Kurekee9488d2022-04-15 06:51:56 -04004625 BUFFSIZE ), 0 );
Andrzej Kurekb4eedf72022-04-15 05:41:14 -04004626
Andrzej Kurekee9488d2022-04-15 06:51:56 -04004627 TEST_EQUAL( mbedtls_move_handshake_to_state( &(client.ssl),
Andrzej Kurekb4eedf72022-04-15 05:41:14 -04004628 &(server.ssl),
4629 MBEDTLS_SSL_CLIENT_KEY_EXCHANGE )
Andrzej Kurekee9488d2022-04-15 06:51:56 -04004630 , 0 );
Andrzej Kurekb4eedf72022-04-15 05:41:14 -04004631
Andrzej Kurek2582ba32022-03-31 06:30:54 -04004632 mbedtls_psa_get_stats( &stats );
4633 /* Save the number of slots in use up to this point.
4634 * With PSA, one can be used for the ECDH private key. */
4635 free_slots_before = stats.empty_slots;
Andrzej Kurek99f67782022-03-31 07:17:18 -04004636
Gilles Peskine6dd489c2022-04-15 05:54:40 -04004637 if( bad_server_ecdhe_key )
4638 {
4639 /* Force a simulated bitflip in the server key. to make the
4640 * raw key agreement in ssl_write_client_key_exchange fail. */
4641 (client.ssl).handshake->ecdh_psa_peerkey[0] ^= 0x02;
4642 }
Andrzej Kurekb4eedf72022-04-15 05:41:14 -04004643
Gilles Peskine6dd489c2022-04-15 05:54:40 -04004644 TEST_EQUAL( mbedtls_move_handshake_to_state( &(client.ssl),
4645 &(server.ssl),
4646 MBEDTLS_SSL_HANDSHAKE_OVER ),
4647 bad_server_ecdhe_key ? MBEDTLS_ERR_SSL_HW_ACCEL_FAILED : 0 );
Andrzej Kurekb4eedf72022-04-15 05:41:14 -04004648
4649 mbedtls_psa_get_stats( &stats );
4650
Gilles Peskine6dd489c2022-04-15 05:54:40 -04004651 /* Make sure that the key slot is already destroyed in case of failure,
4652 * without waiting to close the connection. */
4653 if( bad_server_ecdhe_key )
4654 TEST_EQUAL( free_slots_before, stats.empty_slots );
Andrzej Kurekb4eedf72022-04-15 05:41:14 -04004655
4656exit:
Andrzej Kurek86029e02022-04-15 06:50:56 -04004657 mbedtls_endpoint_free( &client, NULL );
4658 mbedtls_endpoint_free( &server, NULL );
Andrzej Kurek99f67782022-03-31 07:17:18 -04004659
Andrzej Kurekb4eedf72022-04-15 05:41:14 -04004660 USE_PSA_DONE( );
4661}
4662/* END_CASE */
Andrzej Kurek862acb82022-06-06 13:08:23 -04004663
Andrzej Kurek3c036f52022-06-08 11:57:57 -04004664/* BEGIN_CASE depends_on:MBEDTLS_SSL_SRV_C:MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE:MBEDTLS_TEST_HOOKS */
Andrzej Kurek862acb82022-06-06 13:08:23 -04004665void cookie_parsing( data_t *cookie, int exp_ret )
4666{
4667 mbedtls_ssl_context ssl;
4668 mbedtls_ssl_config conf;
4669 size_t len;
4670
4671 mbedtls_ssl_init( &ssl );
4672 mbedtls_ssl_config_init( &conf );
4673 TEST_EQUAL( mbedtls_ssl_config_defaults( &conf, MBEDTLS_SSL_IS_SERVER,
4674 MBEDTLS_SSL_TRANSPORT_DATAGRAM,
4675 MBEDTLS_SSL_PRESET_DEFAULT ),
4676 0 );
4677
4678 TEST_EQUAL( mbedtls_ssl_setup( &ssl, &conf ), 0 );
Andrzej Kurek33f41a82022-06-08 11:47:33 -04004679 TEST_EQUAL( mbedtls_ssl_check_dtls_clihlo_cookie( &ssl, ssl.cli_id,
4680 ssl.cli_id_len,
4681 cookie->x, cookie->len,
4682 ssl.out_buf,
4683 MBEDTLS_SSL_OUT_CONTENT_LEN,
4684 &len ),
Andrzej Kurek862acb82022-06-06 13:08:23 -04004685 exp_ret );
4686
4687 mbedtls_ssl_free( &ssl );
4688 mbedtls_ssl_config_free( &conf );
4689}
4690/* END_CASE */