blob: 91858e41c39c5cd23940ecebd5549f37c1923215 [file] [log] [blame]
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02001/* BEGIN_HEADER */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002#include <mbedtls/ssl.h>
Chris Jones84a773f2021-03-05 18:38:47 +00003#include <ssl_misc.h>
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01004#include <mbedtls/ctr_drbg.h>
5#include <mbedtls/entropy.h>
Andrzej Kurek941962e2020-02-07 09:20:32 -05006#include <mbedtls/timing.h>
Piotr Nowickibde7ee82020-02-21 10:59:50 +01007#include <mbedtls/debug.h>
Hanno Becker73c825a2020-09-08 10:52:58 +01008#include <ssl_tls13_keys.h>
Mateusz Starzyk1aec6462021-02-08 15:34:42 +01009#include "test/certs.h"
Piotr Nowickibde7ee82020-02-21 10:59:50 +010010
Hanno Becker6667ffd2021-04-19 21:59:22 +010011#include <psa/crypto.h>
12
Gabor Mezei22c9a6f2021-10-20 12:09:35 +020013#include <constant_time_internal.h>
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +020014
Manuel Pégourié-Gonnard9670a592020-07-10 10:21:46 +020015#include <test/constant_flow.h>
16
Hanno Becker1413bd82020-09-09 12:46:09 +010017enum
18{
19#define MBEDTLS_SSL_TLS1_3_LABEL( name, string ) \
Xiaofei Baid25fab62021-12-02 06:36:27 +000020 tls13_label_ ## name,
Hanno Becker70d7fb02020-09-09 10:11:21 +010021MBEDTLS_SSL_TLS1_3_LABEL_LIST
22#undef MBEDTLS_SSL_TLS1_3_LABEL
Hanno Becker1413bd82020-09-09 12:46:09 +010023};
Hanno Becker70d7fb02020-09-09 10:11:21 +010024
Piotr Nowickibde7ee82020-02-21 10:59:50 +010025typedef struct log_pattern
26{
27 const char *pattern;
28 size_t counter;
29} log_pattern;
30
Piotr Nowicki438bf3b2020-03-10 12:59:10 +010031/*
32 * This function can be passed to mbedtls to receive output logs from it. In
Piotr Nowickibde7ee82020-02-21 10:59:50 +010033 * this case, it will count the instances of a log_pattern in the received
34 * logged messages.
35 */
36void log_analyzer( void *ctx, int level,
37 const char *file, int line,
38 const char *str )
39{
40 log_pattern *p = (log_pattern *) ctx;
41
42 (void) level;
43 (void) line;
44 (void) file;
45
46 if( NULL != p &&
47 NULL != p->pattern &&
48 NULL != strstr( str, p->pattern ) )
49 {
50 p->counter++;
51 }
52}
Janos Follath6264e662019-11-26 11:11:15 +000053
Paul Elliottc8570442020-04-15 17:00:50 +010054/* Invalid minor version used when not specifying a min/max version or expecting a test to fail */
55#define TEST_SSL_MINOR_VERSION_NONE -1
56
Andrzej Kurek8a6ff152020-02-26 09:10:14 -050057typedef struct handshake_test_options
58{
59 const char *cipher;
Paul Elliottc8570442020-04-15 17:00:50 +010060 int client_min_version;
61 int client_max_version;
62 int server_min_version;
63 int server_max_version;
64 int expected_negotiated_version;
Andrzej Kurek8a6ff152020-02-26 09:10:14 -050065 int pk_alg;
66 data_t *psk_str;
67 int dtls;
Piotr Nowickibde7ee82020-02-21 10:59:50 +010068 int srv_auth_mode;
Andrzej Kurek8a6ff152020-02-26 09:10:14 -050069 int serialize;
70 int mfl;
71 int cli_msg_len;
72 int srv_msg_len;
73 int expected_cli_fragments;
74 int expected_srv_fragments;
75 int renegotiate;
76 int legacy_renegotiation;
Piotr Nowickibde7ee82020-02-21 10:59:50 +010077 void *srv_log_obj;
78 void *cli_log_obj;
79 void (*srv_log_fun)(void *, int, const char *, int, const char *);
80 void (*cli_log_fun)(void *, int, const char *, int, const char *);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -050081 int resize_buffers;
Andrzej Kurek8a6ff152020-02-26 09:10:14 -050082} handshake_test_options;
83
84void init_handshake_options( handshake_test_options *opts )
85{
86 opts->cipher = "";
Paul Elliottc8570442020-04-15 17:00:50 +010087 opts->client_min_version = TEST_SSL_MINOR_VERSION_NONE;
88 opts->client_max_version = TEST_SSL_MINOR_VERSION_NONE;
89 opts->server_min_version = TEST_SSL_MINOR_VERSION_NONE;
90 opts->server_max_version = TEST_SSL_MINOR_VERSION_NONE;
91 opts->expected_negotiated_version = MBEDTLS_SSL_MINOR_VERSION_3;
Andrzej Kurek8a6ff152020-02-26 09:10:14 -050092 opts->pk_alg = MBEDTLS_PK_RSA;
93 opts->psk_str = NULL;
94 opts->dtls = 0;
Piotr Nowickibde7ee82020-02-21 10:59:50 +010095 opts->srv_auth_mode = MBEDTLS_SSL_VERIFY_NONE;
Andrzej Kurek8a6ff152020-02-26 09:10:14 -050096 opts->serialize = 0;
97 opts->mfl = MBEDTLS_SSL_MAX_FRAG_LEN_NONE;
98 opts->cli_msg_len = 100;
99 opts->srv_msg_len = 100;
100 opts->expected_cli_fragments = 1;
101 opts->expected_srv_fragments = 1;
102 opts->renegotiate = 0;
103 opts->legacy_renegotiation = MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION;
Piotr Nowickibde7ee82020-02-21 10:59:50 +0100104 opts->srv_log_obj = NULL;
105 opts->srv_log_obj = NULL;
106 opts->srv_log_fun = NULL;
107 opts->cli_log_fun = NULL;
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500108 opts->resize_buffers = 1;
Andrzej Kurek8a6ff152020-02-26 09:10:14 -0500109}
Janos Follath6264e662019-11-26 11:11:15 +0000110/*
111 * Buffer structure for custom I/O callbacks.
112 */
113
114typedef struct mbedtls_test_buffer
115{
116 size_t start;
117 size_t content_length;
118 size_t capacity;
119 unsigned char *buffer;
120} mbedtls_test_buffer;
121
122/*
123 * Initialises \p buf. After calling this function it is safe to call
124 * `mbedtls_test_buffer_free()` on \p buf.
125 */
126void mbedtls_test_buffer_init( mbedtls_test_buffer *buf )
127{
128 memset( buf, 0, sizeof( *buf ) );
129}
130
131/*
132 * Sets up \p buf. After calling this function it is safe to call
133 * `mbedtls_test_buffer_put()` and `mbedtls_test_buffer_get()` on \p buf.
134 */
135int mbedtls_test_buffer_setup( mbedtls_test_buffer *buf, size_t capacity )
136{
137 buf->buffer = (unsigned char*) mbedtls_calloc( capacity,
138 sizeof(unsigned char) );
139 if( NULL == buf->buffer )
140 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
141 buf->capacity = capacity;
142
143 return 0;
144}
145
146void mbedtls_test_buffer_free( mbedtls_test_buffer *buf )
147{
148 if( buf->buffer != NULL )
149 mbedtls_free( buf->buffer );
150
151 memset( buf, 0, sizeof( *buf ) );
152}
153
154/*
155 * Puts \p input_len bytes from the \p input buffer into the ring buffer \p buf.
156 *
157 * \p buf must have been initialized and set up by calling
158 * `mbedtls_test_buffer_init()` and `mbedtls_test_buffer_setup()`.
159 *
160 * \retval \p input_len, if the data fits.
161 * \retval 0 <= value < \p input_len, if the data does not fit.
162 * \retval -1, if \p buf is NULL, it hasn't been set up or \p input_len is not
163 * zero and \p input is NULL.
164 */
165int mbedtls_test_buffer_put( mbedtls_test_buffer *buf,
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100166 const unsigned char *input, size_t input_len )
Janos Follath6264e662019-11-26 11:11:15 +0000167{
168 size_t overflow = 0;
169
170 if( ( buf == NULL ) || ( buf->buffer == NULL ) )
171 return -1;
172
173 /* Reduce input_len to a number that fits in the buffer. */
174 if ( ( buf->content_length + input_len ) > buf->capacity )
175 {
176 input_len = buf->capacity - buf->content_length;
177 }
178
179 if( input == NULL )
180 {
181 return ( input_len == 0 ) ? 0 : -1;
182 }
183
Piotr Nowickifb437d72020-01-13 16:59:12 +0100184 /* Check if the buffer has not come full circle and free space is not in
185 * the middle */
186 if( buf->start + buf->content_length < buf->capacity )
Janos Follath6264e662019-11-26 11:11:15 +0000187 {
Piotr Nowickifb437d72020-01-13 16:59:12 +0100188
189 /* Calculate the number of bytes that need to be placed at lower memory
190 * address */
191 if( buf->start + buf->content_length + input_len
192 > buf->capacity )
193 {
194 overflow = ( buf->start + buf->content_length + input_len )
195 % buf->capacity;
196 }
197
198 memcpy( buf->buffer + buf->start + buf->content_length, input,
199 input_len - overflow );
200 memcpy( buf->buffer, input + input_len - overflow, overflow );
201
202 }
203 else
204 {
205 /* The buffer has come full circle and free space is in the middle */
206 memcpy( buf->buffer + buf->start + buf->content_length - buf->capacity,
207 input, input_len );
Janos Follath6264e662019-11-26 11:11:15 +0000208 }
209
Janos Follath6264e662019-11-26 11:11:15 +0000210 buf->content_length += input_len;
Janos Follath6264e662019-11-26 11:11:15 +0000211 return input_len;
212}
213
214/*
Andrzej Kurekf7774142020-01-22 06:34:59 -0500215 * Gets \p output_len bytes from the ring buffer \p buf into the
216 * \p output buffer. The output buffer can be NULL, in this case a part of the
217 * ring buffer will be dropped, if the requested length is available.
Janos Follath6264e662019-11-26 11:11:15 +0000218 *
219 * \p buf must have been initialized and set up by calling
220 * `mbedtls_test_buffer_init()` and `mbedtls_test_buffer_setup()`.
221 *
222 * \retval \p output_len, if the data is available.
223 * \retval 0 <= value < \p output_len, if the data is not available.
Andrzej Kurekf7774142020-01-22 06:34:59 -0500224 * \retval -1, if \buf is NULL or it hasn't been set up.
Janos Follath6264e662019-11-26 11:11:15 +0000225 */
226int mbedtls_test_buffer_get( mbedtls_test_buffer *buf,
227 unsigned char* output, size_t output_len )
228{
229 size_t overflow = 0;
230
231 if( ( buf == NULL ) || ( buf->buffer == NULL ) )
232 return -1;
233
Andrzej Kurekf7774142020-01-22 06:34:59 -0500234 if( output == NULL && output_len == 0 )
235 return 0;
Janos Follath6264e662019-11-26 11:11:15 +0000236
237 if( buf->content_length < output_len )
238 output_len = buf->content_length;
239
240 /* Calculate the number of bytes that need to be drawn from lower memory
241 * address */
242 if( buf->start + output_len > buf->capacity )
243 {
244 overflow = ( buf->start + output_len ) % buf->capacity;
245 }
246
Andrzej Kurekf7774142020-01-22 06:34:59 -0500247 if( output != NULL )
248 {
249 memcpy( output, buf->buffer + buf->start, output_len - overflow );
250 memcpy( output + output_len - overflow, buf->buffer, overflow );
251 }
252
Janos Follath6264e662019-11-26 11:11:15 +0000253 buf->content_length -= output_len;
254 buf->start = ( buf->start + output_len ) % buf->capacity;
255
256 return output_len;
257}
258
Hanno Beckera18d1322018-01-03 14:27:32 +0000259/*
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500260 * Errors used in the message transport mock tests
261 */
262 #define MBEDTLS_TEST_ERROR_ARG_NULL -11
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500263 #define MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED -44
264
265/*
266 * Context for a message metadata queue (fifo) that is on top of the ring buffer.
267 */
268typedef struct mbedtls_test_message_queue
269{
270 size_t *messages;
271 int pos;
272 int num;
273 int capacity;
274} mbedtls_test_message_queue;
275
276/*
277 * Setup and free functions for the message metadata queue.
278 *
279 * \p capacity describes the number of message metadata chunks that can be held
280 * within the queue.
281 *
282 * \retval 0, if a metadata queue of a given length can be allocated.
283 * \retval MBEDTLS_ERR_SSL_ALLOC_FAILED, if allocation failed.
284 */
285int mbedtls_test_message_queue_setup( mbedtls_test_message_queue *queue,
286 size_t capacity )
287{
288 queue->messages = (size_t*) mbedtls_calloc( capacity, sizeof(size_t) );
289 if( NULL == queue->messages )
290 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
291
292 queue->capacity = capacity;
293 queue->pos = 0;
294 queue->num = 0;
295
296 return 0;
297}
298
299void mbedtls_test_message_queue_free( mbedtls_test_message_queue *queue )
300{
301 if( queue == NULL )
302 return;
303
304 if( queue->messages != NULL )
305 mbedtls_free( queue->messages );
306
307 memset( queue, 0, sizeof( *queue ) );
308}
309
310/*
311 * Push message length information onto the message metadata queue.
312 * This will become the last element to leave it (fifo).
313 *
314 * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null.
Andrzej Kurekf46b9122020-02-07 08:19:00 -0500315 * \retval MBEDTLS_ERR_SSL_WANT_WRITE, if the queue is full.
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500316 * \retval \p len, if the push was successful.
317 */
318int mbedtls_test_message_queue_push_info( mbedtls_test_message_queue *queue,
319 size_t len )
320{
321 int place;
322 if( queue == NULL )
323 return MBEDTLS_TEST_ERROR_ARG_NULL;
324
325 if( queue->num >= queue->capacity )
Andrzej Kurekf46b9122020-02-07 08:19:00 -0500326 return MBEDTLS_ERR_SSL_WANT_WRITE;
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500327
328 place = ( queue->pos + queue->num ) % queue->capacity;
329 queue->messages[place] = len;
330 queue->num++;
331 return len;
332}
333
334/*
335 * Pop information about the next message length from the queue. This will be
336 * the oldest inserted message length(fifo). \p msg_len can be null, in which
337 * case the data will be popped from the queue but not copied anywhere.
338 *
339 * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null.
Andrzej Kurekf46b9122020-02-07 08:19:00 -0500340 * \retval MBEDTLS_ERR_SSL_WANT_READ, if the queue is empty.
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500341 * \retval message length, if the pop was successful, up to the given
342 \p buf_len.
343 */
344int mbedtls_test_message_queue_pop_info( mbedtls_test_message_queue *queue,
345 size_t buf_len )
346{
347 size_t message_length;
348 if( queue == NULL )
349 return MBEDTLS_TEST_ERROR_ARG_NULL;
350 if( queue->num == 0 )
Andrzej Kurekf46b9122020-02-07 08:19:00 -0500351 return MBEDTLS_ERR_SSL_WANT_READ;
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500352
353 message_length = queue->messages[queue->pos];
354 queue->messages[queue->pos] = 0;
355 queue->num--;
356 queue->pos++;
357 queue->pos %= queue->capacity;
358 if( queue->pos < 0 )
359 queue->pos += queue->capacity;
360
361 return ( message_length > buf_len ) ? buf_len : message_length;
362}
363
364/*
365 * Take a peek on the info about the next message length from the queue.
366 * This will be the oldest inserted message length(fifo).
367 *
368 * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null.
Andrzej Kurekf46b9122020-02-07 08:19:00 -0500369 * \retval MBEDTLS_ERR_SSL_WANT_READ, if the queue is empty.
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500370 * \retval 0, if the peek was successful.
371 * \retval MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED, if the given buffer length is
372 * too small to fit the message. In this case the \p msg_len will be
373 * set to the full message length so that the
374 * caller knows what portion of the message can be dropped.
375 */
376int mbedtls_test_message_queue_peek_info( mbedtls_test_message_queue *queue,
377 size_t buf_len, size_t* msg_len )
378{
379 if( queue == NULL || msg_len == NULL )
380 return MBEDTLS_TEST_ERROR_ARG_NULL;
381 if( queue->num == 0 )
Andrzej Kurekf46b9122020-02-07 08:19:00 -0500382 return MBEDTLS_ERR_SSL_WANT_READ;
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500383
384 *msg_len = queue->messages[queue->pos];
385 return ( *msg_len > buf_len ) ? MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED : 0;
386}
387/*
Janos Follath031827f2019-11-27 11:12:14 +0000388 * Context for the I/O callbacks simulating network connection.
389 */
390
391#define MBEDTLS_MOCK_SOCKET_CONNECTED 1
392
393typedef struct mbedtls_mock_socket
394{
395 int status;
396 mbedtls_test_buffer *input;
397 mbedtls_test_buffer *output;
398 struct mbedtls_mock_socket *peer;
399} mbedtls_mock_socket;
400
401/*
402 * Setup and teardown functions for mock sockets.
403 */
404void mbedtls_mock_socket_init( mbedtls_mock_socket *socket )
405{
406 memset( socket, 0, sizeof( *socket ) );
407}
408
409/*
410 * Closes the socket \p socket.
411 *
412 * \p socket must have been previously initialized by calling
413 * mbedtls_mock_socket_init().
414 *
415 * This function frees all allocated resources and both sockets are aware of the
416 * new connection state.
417 *
418 * That is, this function does not simulate half-open TCP connections and the
419 * phenomenon that when closing a UDP connection the peer is not aware of the
420 * connection having been closed.
421 */
422void mbedtls_mock_socket_close( mbedtls_mock_socket* socket )
423{
424 if( socket == NULL )
425 return;
426
427 if( socket->input != NULL )
428 {
429 mbedtls_test_buffer_free( socket->input );
430 mbedtls_free( socket->input );
431 }
432
433 if( socket->output != NULL )
434 {
435 mbedtls_test_buffer_free( socket->output );
436 mbedtls_free( socket->output );
437 }
438
439 if( socket->peer != NULL )
440 memset( socket->peer, 0, sizeof( *socket->peer ) );
441
442 memset( socket, 0, sizeof( *socket ) );
443}
444
445/*
446 * Establishes a connection between \p peer1 and \p peer2.
447 *
448 * \p peer1 and \p peer2 must have been previously initialized by calling
449 * mbedtls_mock_socket_init().
450 *
451 * The capacites of the internal buffers are set to \p bufsize. Setting this to
452 * the correct value allows for simulation of MTU, sanity testing the mock
453 * implementation and mocking TCP connections with lower memory cost.
454 */
455int mbedtls_mock_socket_connect( mbedtls_mock_socket* peer1,
456 mbedtls_mock_socket* peer2,
457 size_t bufsize )
458{
459 int ret = -1;
460
Piotr Nowickid796e192020-01-28 12:09:47 +0100461 peer1->output =
Janos Follath031827f2019-11-27 11:12:14 +0000462 (mbedtls_test_buffer*) mbedtls_calloc( 1, sizeof(mbedtls_test_buffer) );
463 if( peer1->output == NULL )
464 {
465 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
466 goto exit;
467 }
468 mbedtls_test_buffer_init( peer1->output );
469 if( 0 != ( ret = mbedtls_test_buffer_setup( peer1->output, bufsize ) ) )
470 {
471 goto exit;
472 }
473
Piotr Nowickid796e192020-01-28 12:09:47 +0100474 peer2->output =
475 (mbedtls_test_buffer*) mbedtls_calloc( 1, sizeof(mbedtls_test_buffer) );
476 if( peer2->output == NULL )
477 {
478 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
479 goto exit;
480 }
481 mbedtls_test_buffer_init( peer2->output );
482 if( 0 != ( ret = mbedtls_test_buffer_setup( peer2->output, bufsize ) ) )
483 {
484 goto exit;
485 }
486
Janos Follath031827f2019-11-27 11:12:14 +0000487 peer1->peer = peer2;
488 peer2->peer = peer1;
Piotr Nowickid796e192020-01-28 12:09:47 +0100489 peer1->input = peer2->output;
490 peer2->input = peer1->output;
Janos Follath031827f2019-11-27 11:12:14 +0000491
492 peer1->status = peer2->status = MBEDTLS_MOCK_SOCKET_CONNECTED;
493 ret = 0;
494
495exit:
496
497 if( ret != 0 )
498 {
499 mbedtls_mock_socket_close( peer1 );
500 mbedtls_mock_socket_close( peer2 );
501 }
502
503 return ret;
504}
505
506/*
507 * Callbacks for simulating blocking I/O over connection-oriented transport.
508 */
509
510int mbedtls_mock_tcp_send_b( void *ctx, const unsigned char *buf, size_t len )
511{
512 mbedtls_mock_socket *socket = (mbedtls_mock_socket*) ctx;
513
514 if( socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED )
515 return -1;
516
517 return mbedtls_test_buffer_put( socket->output, buf, len );
518}
519
520int mbedtls_mock_tcp_recv_b( void *ctx, unsigned char *buf, size_t len )
521{
522 mbedtls_mock_socket *socket = (mbedtls_mock_socket*) ctx;
523
524 if( socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED )
525 return -1;
526
527 return mbedtls_test_buffer_get( socket->input, buf, len );
528}
529
530/*
Janos Follath3766ba52019-11-27 13:31:42 +0000531 * Callbacks for simulating non-blocking I/O over connection-oriented transport.
532 */
533
534int mbedtls_mock_tcp_send_nb( void *ctx, const unsigned char *buf, size_t len )
535{
536 mbedtls_mock_socket *socket = (mbedtls_mock_socket*) ctx;
537
538 if( socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED )
539 return -1;
540
Piotr Nowicki890b5ca2020-01-15 16:19:07 +0100541 if( socket->output->capacity == socket->output->content_length )
Janos Follath3766ba52019-11-27 13:31:42 +0000542 {
Janos Follath3766ba52019-11-27 13:31:42 +0000543 return MBEDTLS_ERR_SSL_WANT_WRITE;
544 }
545
Janos Follath3766ba52019-11-27 13:31:42 +0000546 return mbedtls_test_buffer_put( socket->output, buf, len );
547}
548
549int mbedtls_mock_tcp_recv_nb( void *ctx, unsigned char *buf, size_t len )
550{
551 mbedtls_mock_socket *socket = (mbedtls_mock_socket*) ctx;
552
553 if( socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED )
554 return -1;
555
Andrzej Kurekf40daa32020-02-04 09:00:01 -0500556 if( socket->input->content_length == 0 )
Janos Follath3766ba52019-11-27 13:31:42 +0000557 {
Janos Follath3766ba52019-11-27 13:31:42 +0000558 return MBEDTLS_ERR_SSL_WANT_READ;
559 }
560
Janos Follath3766ba52019-11-27 13:31:42 +0000561 return mbedtls_test_buffer_get( socket->input, buf, len );
562}
563
Andrzej Kurekbc483de2020-01-22 03:40:00 -0500564/* Errors used in the message socket mocks */
565
566#define MBEDTLS_TEST_ERROR_CONTEXT_ERROR -55
567#define MBEDTLS_TEST_ERROR_SEND_FAILED -66
568#define MBEDTLS_TEST_ERROR_RECV_FAILED -77
569
570/*
571 * Structure used as an addon, or a wrapper, around the mocked sockets.
572 * Contains an input queue, to which the other socket pushes metadata,
573 * and an output queue, to which this one pushes metadata. This context is
574 * considered as an owner of the input queue only, which is initialized and
575 * freed in the respective setup and free calls.
576 */
577typedef struct mbedtls_test_message_socket_context
578{
579 mbedtls_test_message_queue* queue_input;
580 mbedtls_test_message_queue* queue_output;
581 mbedtls_mock_socket* socket;
582} mbedtls_test_message_socket_context;
583
Andrzej Kurek45916ba2020-03-05 14:46:22 -0500584void mbedtls_message_socket_init( mbedtls_test_message_socket_context *ctx )
585{
586 ctx->queue_input = NULL;
587 ctx->queue_output = NULL;
588 ctx->socket = NULL;
589}
590
Andrzej Kurekbc483de2020-01-22 03:40:00 -0500591/*
592 * Setup a given mesasge socket context including initialization of
593 * input/output queues to a chosen capacity of messages. Also set the
594 * corresponding mock socket.
595 *
596 * \retval 0, if everything succeeds.
597 * \retval MBEDTLS_ERR_SSL_ALLOC_FAILED, if allocation of a message
598 * queue failed.
599 */
600int mbedtls_message_socket_setup( mbedtls_test_message_queue* queue_input,
601 mbedtls_test_message_queue* queue_output,
602 size_t queue_capacity,
603 mbedtls_mock_socket* socket,
604 mbedtls_test_message_socket_context* ctx )
605{
606 int ret = mbedtls_test_message_queue_setup( queue_input, queue_capacity );
607 if( ret != 0 )
608 return ret;
609 ctx->queue_input = queue_input;
610 ctx->queue_output = queue_output;
611 ctx->socket = socket;
612 mbedtls_mock_socket_init( socket );
613
614 return 0;
615}
616
617/*
618 * Close a given message socket context, along with the socket itself. Free the
619 * memory allocated by the input queue.
620 */
621void mbedtls_message_socket_close( mbedtls_test_message_socket_context* ctx )
622{
623 if( ctx == NULL )
624 return;
625
626 mbedtls_test_message_queue_free( ctx->queue_input );
627 mbedtls_mock_socket_close( ctx->socket );
628 memset( ctx, 0, sizeof( *ctx ) );
629}
630
631/*
632 * Send one message through a given message socket context.
633 *
634 * \retval \p len, if everything succeeds.
635 * \retval MBEDTLS_TEST_ERROR_CONTEXT_ERROR, if any of the needed context
636 * elements or the context itself is null.
637 * \retval MBEDTLS_TEST_ERROR_SEND_FAILED if mbedtls_mock_tcp_send_b failed.
Andrzej Kurekf46b9122020-02-07 08:19:00 -0500638 * \retval MBEDTLS_ERR_SSL_WANT_WRITE, if the output queue is full.
Andrzej Kurekbc483de2020-01-22 03:40:00 -0500639 *
640 * This function will also return any error from
641 * mbedtls_test_message_queue_push_info.
642 */
643int mbedtls_mock_tcp_send_msg( void *ctx, const unsigned char *buf, size_t len )
644{
645 mbedtls_test_message_queue* queue;
646 mbedtls_mock_socket* socket;
647 mbedtls_test_message_socket_context *context = (mbedtls_test_message_socket_context*) ctx;
648
649 if( context == NULL || context->socket == NULL
650 || context->queue_output == NULL )
651 {
652 return MBEDTLS_TEST_ERROR_CONTEXT_ERROR;
653 }
654
655 queue = context->queue_output;
656 socket = context->socket;
657
658 if( queue->num >= queue->capacity )
Andrzej Kurekf46b9122020-02-07 08:19:00 -0500659 return MBEDTLS_ERR_SSL_WANT_WRITE;
Andrzej Kurekbc483de2020-01-22 03:40:00 -0500660
661 if( mbedtls_mock_tcp_send_b( socket, buf, len ) != (int) len )
662 return MBEDTLS_TEST_ERROR_SEND_FAILED;
663
664 return mbedtls_test_message_queue_push_info( queue, len );
665}
666
667/*
668 * Receive one message from a given message socket context and return message
669 * length or an error.
670 *
671 * \retval message length, if everything succeeds.
672 * \retval MBEDTLS_TEST_ERROR_CONTEXT_ERROR, if any of the needed context
673 * elements or the context itself is null.
674 * \retval MBEDTLS_TEST_ERROR_RECV_FAILED if mbedtls_mock_tcp_recv_b failed.
675 *
676 * This function will also return any error other than
677 * MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED from mbedtls_test_message_queue_peek_info.
678 */
679int mbedtls_mock_tcp_recv_msg( void *ctx, unsigned char *buf, size_t buf_len )
680{
681 mbedtls_test_message_queue* queue;
682 mbedtls_mock_socket* socket;
683 mbedtls_test_message_socket_context *context = (mbedtls_test_message_socket_context*) ctx;
Gilles Peskine19e841e2020-03-09 20:43:51 +0100684 size_t drop_len = 0;
Andrzej Kurekbc483de2020-01-22 03:40:00 -0500685 size_t msg_len;
686 int ret;
687
688 if( context == NULL || context->socket == NULL
689 || context->queue_input == NULL )
690 {
691 return MBEDTLS_TEST_ERROR_CONTEXT_ERROR;
692 }
693
694 queue = context->queue_input;
695 socket = context->socket;
696
697 /* Peek first, so that in case of a socket error the data remains in
698 * the queue. */
699 ret = mbedtls_test_message_queue_peek_info( queue, buf_len, &msg_len );
700 if( ret == MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED )
701 {
702 /* Calculate how much to drop */
703 drop_len = msg_len - buf_len;
704
705 /* Set the requested message len to be buffer length */
706 msg_len = buf_len;
707 } else if( ret != 0 )
708 {
709 return ret;
710 }
711
712 if( mbedtls_mock_tcp_recv_b( socket, buf, msg_len ) != (int) msg_len )
713 return MBEDTLS_TEST_ERROR_RECV_FAILED;
714
715 if( ret == MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED )
716 {
717 /* Drop the remaining part of the message */
718 if( mbedtls_mock_tcp_recv_b( socket, NULL, drop_len ) != (int) drop_len )
719 {
720 /* Inconsistent state - part of the message was read,
721 * and a part couldn't. Not much we can do here, but it should not
722 * happen in test environment, unless forced manually. */
723 }
724 }
725 mbedtls_test_message_queue_pop_info( queue, buf_len );
726
727 return msg_len;
728}
729
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +0200730#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
731 defined(MBEDTLS_ENTROPY_C) && \
732 defined(MBEDTLS_CTR_DRBG_C)
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100733
734/*
735 * Structure with endpoint's certificates for SSL communication tests.
736 */
737typedef struct mbedtls_endpoint_certificate
738{
739 mbedtls_x509_crt ca_cert;
740 mbedtls_x509_crt cert;
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100741 mbedtls_pk_context pkey;
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100742} mbedtls_endpoint_certificate;
743
744/*
745 * Endpoint structure for SSL communication tests.
746 */
747typedef struct mbedtls_endpoint
748{
749 const char *name;
750 mbedtls_ssl_context ssl;
751 mbedtls_ssl_config conf;
752 mbedtls_ctr_drbg_context ctr_drbg;
753 mbedtls_entropy_context entropy;
754 mbedtls_mock_socket socket;
755 mbedtls_endpoint_certificate cert;
756} mbedtls_endpoint;
757
758/*
759 * Initializes \p ep_cert structure and assigns it to endpoint
760 * represented by \p ep.
761 *
762 * \retval 0 on success, otherwise error code.
763 */
Andrzej Kurekb2980742020-02-02 19:25:26 -0500764int mbedtls_endpoint_certificate_init( mbedtls_endpoint *ep, int pk_alg )
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100765{
766 int i = 0;
767 int ret = -1;
768 mbedtls_endpoint_certificate *cert;
769
770 if( ep == NULL )
771 {
772 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
773 }
774
775 cert = &( ep->cert );
776 mbedtls_x509_crt_init( &( cert->ca_cert ) );
777 mbedtls_x509_crt_init( &( cert->cert ) );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100778 mbedtls_pk_init( &( cert->pkey ) );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100779
780 /* Load the trusted CA */
781
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100782 for( i = 0; mbedtls_test_cas_der[i] != NULL; i++ )
783 {
784 ret = mbedtls_x509_crt_parse_der( &( cert->ca_cert ),
785 (const unsigned char *) mbedtls_test_cas_der[i],
786 mbedtls_test_cas_der_len[i] );
787 TEST_ASSERT( ret == 0 );
788 }
789
790 /* Load own certificate and private key */
791
792 if( ep->conf.endpoint == MBEDTLS_SSL_IS_SERVER )
793 {
Andrzej Kurekb2980742020-02-02 19:25:26 -0500794 if( pk_alg == MBEDTLS_PK_RSA )
795 {
796 ret = mbedtls_x509_crt_parse( &( cert->cert ),
797 (const unsigned char*) mbedtls_test_srv_crt_rsa_sha256_der,
798 mbedtls_test_srv_crt_rsa_sha256_der_len );
799 TEST_ASSERT( ret == 0 );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100800
Andrzej Kurekb2980742020-02-02 19:25:26 -0500801 ret = mbedtls_pk_parse_key( &( cert->pkey ),
802 (const unsigned char*) mbedtls_test_srv_key_rsa_der,
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200803 mbedtls_test_srv_key_rsa_der_len, NULL, 0,
804 mbedtls_test_rnd_std_rand, NULL );
Andrzej Kurekb2980742020-02-02 19:25:26 -0500805 TEST_ASSERT( ret == 0 );
806 }
807 else
808 {
809 ret = mbedtls_x509_crt_parse( &( cert->cert ),
810 (const unsigned char*) mbedtls_test_srv_crt_ec_der,
811 mbedtls_test_srv_crt_ec_der_len );
812 TEST_ASSERT( ret == 0 );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100813
Andrzej Kurekb2980742020-02-02 19:25:26 -0500814 ret = mbedtls_pk_parse_key( &( cert->pkey ),
815 (const unsigned char*) mbedtls_test_srv_key_ec_der,
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200816 mbedtls_test_srv_key_ec_der_len, NULL, 0,
817 mbedtls_test_rnd_std_rand, NULL );
Andrzej Kurekb2980742020-02-02 19:25:26 -0500818 TEST_ASSERT( ret == 0 );
819 }
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100820 }
821 else
822 {
Andrzej Kurekb2980742020-02-02 19:25:26 -0500823 if( pk_alg == MBEDTLS_PK_RSA )
824 {
825 ret = mbedtls_x509_crt_parse( &( cert->cert ),
826 (const unsigned char *) mbedtls_test_cli_crt_rsa_der,
827 mbedtls_test_cli_crt_rsa_der_len );
828 TEST_ASSERT( ret == 0 );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100829
Andrzej Kurekb2980742020-02-02 19:25:26 -0500830 ret = mbedtls_pk_parse_key( &( cert->pkey ),
831 (const unsigned char *) mbedtls_test_cli_key_rsa_der,
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200832 mbedtls_test_cli_key_rsa_der_len, NULL, 0,
833 mbedtls_test_rnd_std_rand, NULL );
Andrzej Kurekb2980742020-02-02 19:25:26 -0500834 TEST_ASSERT( ret == 0 );
835 }
836 else
837 {
838 ret = mbedtls_x509_crt_parse( &( cert->cert ),
839 (const unsigned char *) mbedtls_test_cli_crt_ec_der,
840 mbedtls_test_cli_crt_ec_len );
841 TEST_ASSERT( ret == 0 );
842
843 ret = mbedtls_pk_parse_key( &( cert->pkey ),
844 (const unsigned char *) mbedtls_test_cli_key_ec_der,
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200845 mbedtls_test_cli_key_ec_der_len, NULL, 0,
846 mbedtls_test_rnd_std_rand, NULL );
Andrzej Kurekb2980742020-02-02 19:25:26 -0500847 TEST_ASSERT( ret == 0 );
848 }
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100849 }
850
851 mbedtls_ssl_conf_ca_chain( &( ep->conf ), &( cert->ca_cert ), NULL );
852
Andrzej Kurekb2980742020-02-02 19:25:26 -0500853 ret = mbedtls_ssl_conf_own_cert( &( ep->conf ), &( cert->cert ),
854 &( cert->pkey ) );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100855 TEST_ASSERT( ret == 0 );
856
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100857exit:
858 if( ret != 0 )
859 {
860 mbedtls_x509_crt_free( &( cert->ca_cert ) );
861 mbedtls_x509_crt_free( &( cert->cert ) );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100862 mbedtls_pk_free( &( cert->pkey ) );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100863 }
864
865 return ret;
866}
867
868/*
869 * Initializes \p ep structure. It is important to call `mbedtls_endpoint_free()`
870 * after calling this function even if it fails.
871 *
872 * \p endpoint_type must be set as MBEDTLS_SSL_IS_SERVER or
873 * MBEDTLS_SSL_IS_CLIENT.
Andrzej Kurek15daf502020-02-12 09:17:52 -0500874 * \p pk_alg the algorithm to use, currently only MBEDTLS_PK_RSA and
875 * MBEDTLS_PK_ECDSA are supported.
876 * \p dtls_context - in case of DTLS - this is the context handling metadata.
877 * \p input_queue - used only in case of DTLS.
878 * \p output_queue - used only in case of DTLS.
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100879 *
880 * \retval 0 on success, otherwise error code.
881 */
Andrzej Kurek15daf502020-02-12 09:17:52 -0500882int mbedtls_endpoint_init( mbedtls_endpoint *ep, int endpoint_type, int pk_alg,
883 mbedtls_test_message_socket_context *dtls_context,
884 mbedtls_test_message_queue *input_queue,
885 mbedtls_test_message_queue *output_queue )
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100886{
887 int ret = -1;
888
Andrzej Kurek15daf502020-02-12 09:17:52 -0500889 if( dtls_context != NULL && ( input_queue == NULL || output_queue == NULL ) )
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100890 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Andrzej Kurek15daf502020-02-12 09:17:52 -0500891
892 if( ep == NULL )
893 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100894
895 memset( ep, 0, sizeof( *ep ) );
896
897 ep->name = ( endpoint_type == MBEDTLS_SSL_IS_SERVER ) ? "Server" : "Client";
898
899 mbedtls_ssl_init( &( ep->ssl ) );
900 mbedtls_ssl_config_init( &( ep->conf ) );
901 mbedtls_ctr_drbg_init( &( ep->ctr_drbg ) );
902 mbedtls_ssl_conf_rng( &( ep->conf ),
903 mbedtls_ctr_drbg_random,
904 &( ep->ctr_drbg ) );
905 mbedtls_entropy_init( &( ep->entropy ) );
Andrzej Kurek15daf502020-02-12 09:17:52 -0500906 if( dtls_context != NULL )
907 {
908 TEST_ASSERT( mbedtls_message_socket_setup( input_queue, output_queue,
909 100, &( ep->socket ),
910 dtls_context ) == 0 );
911 }
912 else
913 {
914 mbedtls_mock_socket_init( &( ep->socket ) );
915 }
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100916
917 ret = mbedtls_ctr_drbg_seed( &( ep->ctr_drbg ), mbedtls_entropy_func,
918 &( ep->entropy ), (const unsigned char *) ( ep->name ),
919 strlen( ep->name ) );
920 TEST_ASSERT( ret == 0 );
921
922 /* Non-blocking callbacks without timeout */
Andrzej Kurek15daf502020-02-12 09:17:52 -0500923 if( dtls_context != NULL )
924 {
925 mbedtls_ssl_set_bio( &( ep->ssl ), dtls_context,
926 mbedtls_mock_tcp_send_msg,
927 mbedtls_mock_tcp_recv_msg,
928 NULL );
929 }
930 else
931 {
932 mbedtls_ssl_set_bio( &( ep->ssl ), &( ep->socket ),
933 mbedtls_mock_tcp_send_nb,
934 mbedtls_mock_tcp_recv_nb,
935 NULL );
936 }
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100937
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100938 ret = mbedtls_ssl_config_defaults( &( ep->conf ), endpoint_type,
Andrzej Kurek15daf502020-02-12 09:17:52 -0500939 ( dtls_context != NULL ) ?
940 MBEDTLS_SSL_TRANSPORT_DATAGRAM :
941 MBEDTLS_SSL_TRANSPORT_STREAM,
942 MBEDTLS_SSL_PRESET_DEFAULT );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100943 TEST_ASSERT( ret == 0 );
944
Andrzej Kurek1a44a152020-02-07 08:21:32 -0500945 ret = mbedtls_ssl_setup( &( ep->ssl ), &( ep->conf ) );
946 TEST_ASSERT( ret == 0 );
Andrzej Kurek15daf502020-02-12 09:17:52 -0500947
948#if defined(MBEDTLS_SSL_PROTO_DTLS) && defined(MBEDTLS_SSL_SRV_C)
949 if( endpoint_type == MBEDTLS_SSL_IS_SERVER && dtls_context != NULL )
950 mbedtls_ssl_conf_dtls_cookies( &( ep->conf ), NULL, NULL, NULL );
951#endif
952
Andrzej Kurekb2980742020-02-02 19:25:26 -0500953 ret = mbedtls_endpoint_certificate_init( ep, pk_alg );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100954 TEST_ASSERT( ret == 0 );
955
956exit:
957 return ret;
958}
959
960/*
961 * Deinitializes certificates from endpoint represented by \p ep.
962 */
963void mbedtls_endpoint_certificate_free( mbedtls_endpoint *ep )
964{
965 mbedtls_endpoint_certificate *cert = &( ep->cert );
966 mbedtls_x509_crt_free( &( cert->ca_cert ) );
967 mbedtls_x509_crt_free( &( cert->cert ) );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100968 mbedtls_pk_free( &( cert->pkey ) );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100969}
970
971/*
972 * Deinitializes endpoint represented by \p ep.
973 */
Andrzej Kurek15daf502020-02-12 09:17:52 -0500974void mbedtls_endpoint_free( mbedtls_endpoint *ep,
975 mbedtls_test_message_socket_context *context )
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100976{
977 mbedtls_endpoint_certificate_free( ep );
978
979 mbedtls_ssl_free( &( ep->ssl ) );
980 mbedtls_ssl_config_free( &( ep->conf ) );
981 mbedtls_ctr_drbg_free( &( ep->ctr_drbg ) );
982 mbedtls_entropy_free( &( ep->entropy ) );
Andrzej Kurek15daf502020-02-12 09:17:52 -0500983
984 if( context != NULL )
985 {
986 mbedtls_message_socket_close( context );
987 }
988 else
989 {
990 mbedtls_mock_socket_close( &( ep->socket ) );
991 }
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100992}
993
994/*
995 * This function moves ssl handshake from \p ssl to prescribed \p state.
996 * /p second_ssl is used as second endpoint and their sockets have to be
997 * connected before calling this function.
998 *
999 * \retval 0 on success, otherwise error code.
1000 */
1001int mbedtls_move_handshake_to_state( mbedtls_ssl_context *ssl,
1002 mbedtls_ssl_context *second_ssl,
1003 int state )
1004{
1005 enum { BUFFSIZE = 1024 };
1006 int max_steps = 1000;
1007 int ret = 0;
1008
1009 if( ssl == NULL || second_ssl == NULL )
1010 {
1011 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
1012 }
1013
1014 /* Perform communication via connected sockets */
1015 while( ( ssl->state != state ) && ( --max_steps >= 0 ) )
1016 {
1017 /* If /p second_ssl ends the handshake procedure before /p ssl then
1018 * there is no need to call the next step */
1019 if( second_ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
1020 {
1021 ret = mbedtls_ssl_handshake_step( second_ssl );
1022 if( ret != 0 && ret != MBEDTLS_ERR_SSL_WANT_READ &&
1023 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
1024 {
1025 return ret;
1026 }
1027 }
1028
1029 /* We only care about the \p ssl state and returns, so we call it last,
1030 * to leave the iteration as soon as the state is as expected. */
1031 ret = mbedtls_ssl_handshake_step( ssl );
1032 if( ret != 0 && ret != MBEDTLS_ERR_SSL_WANT_READ &&
1033 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
1034 {
1035 return ret;
1036 }
1037 }
1038
1039 return ( max_steps >= 0 ) ? ret : -1;
1040}
1041
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02001042#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01001043
Janos Follath3766ba52019-11-27 13:31:42 +00001044/*
Piotr Nowicki438bf3b2020-03-10 12:59:10 +01001045 * Write application data. Increase write counter if necessary.
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001046 */
1047int mbedtls_ssl_write_fragment( mbedtls_ssl_context *ssl, unsigned char *buf,
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001048 int buf_len, int *written,
Piotr Nowicki438bf3b2020-03-10 12:59:10 +01001049 const int expected_fragments )
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001050{
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001051 int ret = mbedtls_ssl_write( ssl, buf + *written, buf_len - *written );
1052 if( ret > 0 )
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001053 {
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001054 *written += ret;
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001055 }
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001056
1057 if( expected_fragments == 0 )
1058 {
1059 /* Used for DTLS and the message size larger than MFL. In that case
1060 * the message can not be fragmented and the library should return
1061 * MBEDTLS_ERR_SSL_BAD_INPUT_DATA error. This error must be returned
1062 * to prevent a dead loop inside mbedtls_exchange_data(). */
1063 return ret;
1064 }
1065 else if( expected_fragments == 1 )
1066 {
1067 /* Used for TLS/DTLS and the message size lower than MFL */
1068 TEST_ASSERT( ret == buf_len ||
1069 ret == MBEDTLS_ERR_SSL_WANT_READ ||
1070 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
1071 }
1072 else
1073 {
1074 /* Used for TLS and the message size larger than MFL */
1075 TEST_ASSERT( expected_fragments > 1 );
1076 TEST_ASSERT( ( ret >= 0 && ret <= buf_len ) ||
1077 ret == MBEDTLS_ERR_SSL_WANT_READ ||
1078 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
1079 }
1080
1081 return 0;
1082
1083exit:
1084 /* Some of the tests failed */
1085 return -1;
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001086}
1087
1088/*
Piotr Nowicki438bf3b2020-03-10 12:59:10 +01001089 * Read application data and increase read counter and fragments counter if necessary.
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001090 */
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001091int mbedtls_ssl_read_fragment( mbedtls_ssl_context *ssl, unsigned char *buf,
1092 int buf_len, int *read,
Piotr Nowicki438bf3b2020-03-10 12:59:10 +01001093 int *fragments, const int expected_fragments )
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001094{
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001095 int ret = mbedtls_ssl_read( ssl, buf + *read, buf_len - *read );
1096 if( ret > 0 )
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001097 {
Piotr Nowicki438bf3b2020-03-10 12:59:10 +01001098 ( *fragments )++;
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001099 *read += ret;
1100 }
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001101
1102 if( expected_fragments == 0 )
1103 {
1104 TEST_ASSERT( ret == 0 );
1105 }
1106 else if( expected_fragments == 1 )
1107 {
1108 TEST_ASSERT( ret == buf_len ||
1109 ret == MBEDTLS_ERR_SSL_WANT_READ ||
1110 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
1111 }
1112 else
1113 {
1114 TEST_ASSERT( expected_fragments > 1 );
1115 TEST_ASSERT( ( ret >= 0 && ret <= buf_len ) ||
1116 ret == MBEDTLS_ERR_SSL_WANT_READ ||
1117 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
1118 }
1119
1120 return 0;
1121
1122exit:
1123 /* Some of the tests failed */
1124 return -1;
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001125}
1126
1127/*
Hanno Beckera18d1322018-01-03 14:27:32 +00001128 * Helper function setting up inverse record transformations
1129 * using given cipher, hash, EtM mode, authentication tag length,
1130 * and version.
1131 */
1132
1133#define CHK( x ) \
1134 do \
1135 { \
1136 if( !( x ) ) \
Hanno Becker81e16a32019-03-01 11:21:44 +00001137 { \
Hanno Beckera5780f12019-04-05 09:55:37 +01001138 ret = -1; \
Hanno Becker81e16a32019-03-01 11:21:44 +00001139 goto cleanup; \
1140 } \
Hanno Beckera18d1322018-01-03 14:27:32 +00001141 } while( 0 )
1142
Andrzej Kurekf40daa32020-02-04 09:00:01 -05001143void set_ciphersuite( mbedtls_ssl_config *conf, const char *cipher,
1144 int* forced_ciphersuite )
1145{
1146 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
1147 forced_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id( cipher );
1148 forced_ciphersuite[1] = 0;
1149
1150 ciphersuite_info =
1151 mbedtls_ssl_ciphersuite_from_id( forced_ciphersuite[0] );
1152
1153 TEST_ASSERT( ciphersuite_info != NULL );
1154 TEST_ASSERT( ciphersuite_info->min_minor_ver <= conf->max_minor_ver );
1155 TEST_ASSERT( ciphersuite_info->max_minor_ver >= conf->min_minor_ver );
1156
1157 if( conf->max_minor_ver > ciphersuite_info->max_minor_ver )
1158 {
1159 conf->max_minor_ver = ciphersuite_info->max_minor_ver;
1160 }
1161 if( conf->min_minor_ver < ciphersuite_info->min_minor_ver )
1162 {
1163 conf->min_minor_ver = ciphersuite_info->min_minor_ver;
1164 }
1165
1166 mbedtls_ssl_conf_ciphersuites( conf, forced_ciphersuite );
1167
1168exit:
1169 return;
1170}
1171
Andrzej Kurekcc5169c2020-02-04 09:04:56 -05001172int psk_dummy_callback( void *p_info, mbedtls_ssl_context *ssl,
1173 const unsigned char *name, size_t name_len )
1174{
1175 (void) p_info;
1176 (void) ssl;
1177 (void) name;
1178 (void) name_len;
1179
1180 return ( 0 );
1181}
1182
Hanno Beckerd856c822019-04-29 17:30:59 +01001183#if MBEDTLS_SSL_CID_OUT_LEN_MAX > MBEDTLS_SSL_CID_IN_LEN_MAX
1184#define SSL_CID_LEN_MIN MBEDTLS_SSL_CID_IN_LEN_MAX
1185#else
1186#define SSL_CID_LEN_MIN MBEDTLS_SSL_CID_OUT_LEN_MAX
1187#endif
Hanno Beckera18d1322018-01-03 14:27:32 +00001188
1189static int build_transforms( mbedtls_ssl_transform *t_in,
1190 mbedtls_ssl_transform *t_out,
1191 int cipher_type, int hash_id,
Hanno Beckerd856c822019-04-29 17:30:59 +01001192 int etm, int tag_mode, int ver,
1193 size_t cid0_len,
1194 size_t cid1_len )
Hanno Beckera18d1322018-01-03 14:27:32 +00001195{
1196 mbedtls_cipher_info_t const *cipher_info;
Hanno Beckera5780f12019-04-05 09:55:37 +01001197 int ret = 0;
Hanno Beckera18d1322018-01-03 14:27:32 +00001198
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01001199#if defined(MBEDTLS_USE_PSA_CRYPTO)
1200 psa_key_type_t key_type;
1201 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1202 psa_algorithm_t alg;
1203 size_t key_bits;
1204 psa_status_t status;
1205#endif
1206
Hanno Beckera18d1322018-01-03 14:27:32 +00001207 size_t keylen, maclen, ivlen;
Hanno Becker81e16a32019-03-01 11:21:44 +00001208 unsigned char *key0 = NULL, *key1 = NULL;
Paul Elliott6f1eda72020-06-11 20:22:00 +01001209 unsigned char *md0 = NULL, *md1 = NULL;
Hanno Beckera18d1322018-01-03 14:27:32 +00001210 unsigned char iv_enc[16], iv_dec[16];
1211
Hanno Beckera0e20d02019-05-15 14:03:01 +01001212#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd856c822019-04-29 17:30:59 +01001213 unsigned char cid0[ SSL_CID_LEN_MIN ];
1214 unsigned char cid1[ SSL_CID_LEN_MIN ];
1215
Ronald Cron351f0ee2020-06-10 12:12:18 +02001216 mbedtls_test_rnd_std_rand( NULL, cid0, sizeof( cid0 ) );
1217 mbedtls_test_rnd_std_rand( NULL, cid1, sizeof( cid1 ) );
Hanno Becker43c24b82019-05-01 09:45:57 +01001218#else
1219 ((void) cid0_len);
1220 ((void) cid1_len);
Hanno Beckera0e20d02019-05-15 14:03:01 +01001221#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerd856c822019-04-29 17:30:59 +01001222
Hanno Beckera18d1322018-01-03 14:27:32 +00001223 maclen = 0;
1224
1225 /* Pick cipher */
1226 cipher_info = mbedtls_cipher_info_from_type( cipher_type );
1227 CHK( cipher_info != NULL );
1228 CHK( cipher_info->iv_size <= 16 );
1229 CHK( cipher_info->key_bitlen % 8 == 0 );
1230
1231 /* Pick keys */
1232 keylen = cipher_info->key_bitlen / 8;
Hanno Becker78d1f702019-04-05 09:56:10 +01001233 /* Allocate `keylen + 1` bytes to ensure that we get
1234 * a non-NULL pointers from `mbedtls_calloc` even if
1235 * `keylen == 0` in the case of the NULL cipher. */
1236 CHK( ( key0 = mbedtls_calloc( 1, keylen + 1 ) ) != NULL );
1237 CHK( ( key1 = mbedtls_calloc( 1, keylen + 1 ) ) != NULL );
Hanno Beckera18d1322018-01-03 14:27:32 +00001238 memset( key0, 0x1, keylen );
1239 memset( key1, 0x2, keylen );
1240
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01001241#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckera18d1322018-01-03 14:27:32 +00001242 /* Setup cipher contexts */
1243 CHK( mbedtls_cipher_setup( &t_in->cipher_ctx_enc, cipher_info ) == 0 );
1244 CHK( mbedtls_cipher_setup( &t_in->cipher_ctx_dec, cipher_info ) == 0 );
1245 CHK( mbedtls_cipher_setup( &t_out->cipher_ctx_enc, cipher_info ) == 0 );
1246 CHK( mbedtls_cipher_setup( &t_out->cipher_ctx_dec, cipher_info ) == 0 );
1247
1248#if defined(MBEDTLS_CIPHER_MODE_CBC)
1249 if( cipher_info->mode == MBEDTLS_MODE_CBC )
1250 {
1251 CHK( mbedtls_cipher_set_padding_mode( &t_in->cipher_ctx_enc,
1252 MBEDTLS_PADDING_NONE ) == 0 );
1253 CHK( mbedtls_cipher_set_padding_mode( &t_in->cipher_ctx_dec,
1254 MBEDTLS_PADDING_NONE ) == 0 );
1255 CHK( mbedtls_cipher_set_padding_mode( &t_out->cipher_ctx_enc,
1256 MBEDTLS_PADDING_NONE ) == 0 );
1257 CHK( mbedtls_cipher_set_padding_mode( &t_out->cipher_ctx_dec,
1258 MBEDTLS_PADDING_NONE ) == 0 );
1259 }
1260#endif /* MBEDTLS_CIPHER_MODE_CBC */
1261
1262 CHK( mbedtls_cipher_setkey( &t_in->cipher_ctx_enc, key0,
1263 keylen << 3, MBEDTLS_ENCRYPT ) == 0 );
1264 CHK( mbedtls_cipher_setkey( &t_in->cipher_ctx_dec, key1,
1265 keylen << 3, MBEDTLS_DECRYPT ) == 0 );
1266 CHK( mbedtls_cipher_setkey( &t_out->cipher_ctx_enc, key1,
1267 keylen << 3, MBEDTLS_ENCRYPT ) == 0 );
1268 CHK( mbedtls_cipher_setkey( &t_out->cipher_ctx_dec, key0,
1269 keylen << 3, MBEDTLS_DECRYPT ) == 0 );
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01001270#endif
Hanno Beckera18d1322018-01-03 14:27:32 +00001271
1272 /* Setup MAC contexts */
Hanno Beckerfd86ca82020-11-30 08:54:23 +00001273#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Hanno Beckera18d1322018-01-03 14:27:32 +00001274 if( cipher_info->mode == MBEDTLS_MODE_CBC ||
1275 cipher_info->mode == MBEDTLS_MODE_STREAM )
1276 {
1277 mbedtls_md_info_t const *md_info;
Hanno Beckera18d1322018-01-03 14:27:32 +00001278
1279 /* Pick hash */
1280 md_info = mbedtls_md_info_from_type( hash_id );
1281 CHK( md_info != NULL );
1282
1283 /* Pick hash keys */
1284 maclen = mbedtls_md_get_size( md_info );
Hanno Becker3ee54212019-04-04 16:31:26 +01001285 CHK( ( md0 = mbedtls_calloc( 1, maclen ) ) != NULL );
1286 CHK( ( md1 = mbedtls_calloc( 1, maclen ) ) != NULL );
Hanno Beckera18d1322018-01-03 14:27:32 +00001287 memset( md0, 0x5, maclen );
1288 memset( md1, 0x6, maclen );
1289
1290 CHK( mbedtls_md_setup( &t_out->md_ctx_enc, md_info, 1 ) == 0 );
1291 CHK( mbedtls_md_setup( &t_out->md_ctx_dec, md_info, 1 ) == 0 );
1292 CHK( mbedtls_md_setup( &t_in->md_ctx_enc, md_info, 1 ) == 0 );
1293 CHK( mbedtls_md_setup( &t_in->md_ctx_dec, md_info, 1 ) == 0 );
1294
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001295 CHK( mbedtls_md_hmac_starts( &t_in->md_ctx_enc,
1296 md0, maclen ) == 0 );
1297 CHK( mbedtls_md_hmac_starts( &t_in->md_ctx_dec,
1298 md1, maclen ) == 0 );
1299 CHK( mbedtls_md_hmac_starts( &t_out->md_ctx_enc,
1300 md1, maclen ) == 0 );
1301 CHK( mbedtls_md_hmac_starts( &t_out->md_ctx_dec,
1302 md0, maclen ) == 0 );
Hanno Beckera18d1322018-01-03 14:27:32 +00001303 }
1304#else
1305 ((void) hash_id);
Hanno Beckerfd86ca82020-11-30 08:54:23 +00001306#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Hanno Beckera18d1322018-01-03 14:27:32 +00001307
1308
1309 /* Pick IV's (regardless of whether they
1310 * are being used by the transform). */
1311 ivlen = cipher_info->iv_size;
1312 memset( iv_enc, 0x3, sizeof( iv_enc ) );
1313 memset( iv_dec, 0x4, sizeof( iv_dec ) );
1314
1315 /*
1316 * Setup transforms
1317 */
1318
Jaeden Amero2de07f12019-06-05 13:32:08 +01001319#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
Hanno Beckerfd86ca82020-11-30 08:54:23 +00001320 defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Hanno Beckera18d1322018-01-03 14:27:32 +00001321 t_out->encrypt_then_mac = etm;
1322 t_in->encrypt_then_mac = etm;
1323#else
1324 ((void) etm);
1325#endif
1326
1327 t_out->minor_ver = ver;
1328 t_in->minor_ver = ver;
1329 t_out->ivlen = ivlen;
1330 t_in->ivlen = ivlen;
1331
1332 switch( cipher_info->mode )
1333 {
1334 case MBEDTLS_MODE_GCM:
1335 case MBEDTLS_MODE_CCM:
Ronald Cron6f135e12021-12-08 16:57:54 +01001336#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Beckere6832872020-05-28 08:29:58 +01001337 if( ver == MBEDTLS_SSL_MINOR_VERSION_4 )
1338 {
1339 t_out->fixed_ivlen = 12;
1340 t_in->fixed_ivlen = 12;
1341 }
1342 else
Ronald Cron6f135e12021-12-08 16:57:54 +01001343#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckere6832872020-05-28 08:29:58 +01001344 {
1345 t_out->fixed_ivlen = 4;
1346 t_in->fixed_ivlen = 4;
1347 }
Hanno Beckera18d1322018-01-03 14:27:32 +00001348 t_out->maclen = 0;
1349 t_in->maclen = 0;
1350 switch( tag_mode )
1351 {
1352 case 0: /* Full tag */
1353 t_out->taglen = 16;
1354 t_in->taglen = 16;
1355 break;
1356 case 1: /* Partial tag */
1357 t_out->taglen = 8;
1358 t_in->taglen = 8;
1359 break;
1360 default:
Paul Elliottc7b53742021-02-03 13:18:33 +00001361 ret = 1;
1362 goto cleanup;
Hanno Beckera18d1322018-01-03 14:27:32 +00001363 }
1364 break;
1365
1366 case MBEDTLS_MODE_CHACHAPOLY:
1367 t_out->fixed_ivlen = 12;
1368 t_in->fixed_ivlen = 12;
1369 t_out->maclen = 0;
1370 t_in->maclen = 0;
1371 switch( tag_mode )
1372 {
1373 case 0: /* Full tag */
1374 t_out->taglen = 16;
1375 t_in->taglen = 16;
1376 break;
1377 case 1: /* Partial tag */
1378 t_out->taglen = 8;
1379 t_in->taglen = 8;
1380 break;
1381 default:
Paul Elliottc7b53742021-02-03 13:18:33 +00001382 ret = 1;
1383 goto cleanup;
Hanno Beckera18d1322018-01-03 14:27:32 +00001384 }
1385 break;
1386
1387 case MBEDTLS_MODE_STREAM:
1388 case MBEDTLS_MODE_CBC:
1389 t_out->fixed_ivlen = 0; /* redundant, must be 0 */
1390 t_in->fixed_ivlen = 0; /* redundant, must be 0 */
1391 t_out->taglen = 0;
1392 t_in->taglen = 0;
1393 switch( tag_mode )
1394 {
1395 case 0: /* Full tag */
1396 t_out->maclen = maclen;
1397 t_in->maclen = maclen;
1398 break;
1399 case 1: /* Partial tag */
1400 t_out->maclen = 10;
1401 t_in->maclen = 10;
1402 break;
1403 default:
Paul Elliottc7b53742021-02-03 13:18:33 +00001404 ret = 1;
1405 goto cleanup;
Hanno Beckera18d1322018-01-03 14:27:32 +00001406 }
1407 break;
1408 default:
Paul Elliottc7b53742021-02-03 13:18:33 +00001409 ret = 1;
1410 goto cleanup;
Hanno Beckera18d1322018-01-03 14:27:32 +00001411 break;
1412 }
1413
1414 /* Setup IV's */
1415
1416 memcpy( &t_in->iv_dec, iv_dec, sizeof( iv_dec ) );
1417 memcpy( &t_in->iv_enc, iv_enc, sizeof( iv_enc ) );
1418 memcpy( &t_out->iv_dec, iv_enc, sizeof( iv_enc ) );
1419 memcpy( &t_out->iv_enc, iv_dec, sizeof( iv_dec ) );
1420
Hanno Beckera0e20d02019-05-15 14:03:01 +01001421#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd856c822019-04-29 17:30:59 +01001422 /* Add CID */
1423 memcpy( &t_in->in_cid, cid0, cid0_len );
1424 memcpy( &t_in->out_cid, cid1, cid1_len );
1425 t_in->in_cid_len = cid0_len;
1426 t_in->out_cid_len = cid1_len;
1427 memcpy( &t_out->in_cid, cid1, cid1_len );
1428 memcpy( &t_out->out_cid, cid0, cid0_len );
1429 t_out->in_cid_len = cid1_len;
1430 t_out->out_cid_len = cid0_len;
Hanno Beckera0e20d02019-05-15 14:03:01 +01001431#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerd856c822019-04-29 17:30:59 +01001432
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01001433#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemyslaw Stekielf57b4562022-01-25 00:04:18 +01001434 status = mbedtls_ssl_cipher_to_psa( cipher_type,
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01001435 t_in->taglen,
1436 &alg,
1437 &key_type,
1438 &key_bits );
1439
1440 if ( status != PSA_SUCCESS)
1441 {
1442 ret = psa_status_to_mbedtls( status );
1443 goto cleanup;
1444 }
1445
1446 t_in->psa_alg = alg;
1447 t_out->psa_alg = alg;
1448
1449 if ( alg != MBEDTLS_SSL_NULL_CIPHER )
1450 {
1451 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
1452 psa_set_key_algorithm( &attributes, alg );
1453 psa_set_key_type( &attributes, key_type );
1454
1455 status = psa_import_key( &attributes,
1456 key0,
1457 PSA_BITS_TO_BYTES( key_bits ),
1458 &t_in->psa_key_enc );
1459
1460 if ( status != PSA_SUCCESS)
1461 {
1462 ret = psa_status_to_mbedtls( status );
1463 goto cleanup;
1464 }
1465
1466 status = psa_import_key( &attributes,
1467 key1,
1468 PSA_BITS_TO_BYTES( key_bits ),
1469 &t_in->psa_key_dec );
1470
1471 if ( status != PSA_SUCCESS)
1472 {
1473 ret = psa_status_to_mbedtls( status );
1474 goto cleanup;
1475 }
1476
1477 status = psa_import_key( &attributes,
1478 key1,
1479 PSA_BITS_TO_BYTES( key_bits ),
1480 &t_out->psa_key_enc );
1481
1482 if ( status != PSA_SUCCESS)
1483 {
1484 ret = psa_status_to_mbedtls( status );
1485 goto cleanup;
1486 }
1487
1488 status = psa_import_key( &attributes,
1489 key0,
1490 PSA_BITS_TO_BYTES( key_bits ),
1491 &t_out->psa_key_dec );
1492
1493 if ( status != PSA_SUCCESS)
1494 {
1495 ret = psa_status_to_mbedtls( status );
1496 goto cleanup;
1497 }
1498 }
1499#endif /* MBEDTLS_USE_PSA_CRYPTO */
1500
Hanno Becker81e16a32019-03-01 11:21:44 +00001501cleanup:
1502
Hanno Becker3ee54212019-04-04 16:31:26 +01001503 mbedtls_free( key0 );
1504 mbedtls_free( key1 );
Hanno Becker81e16a32019-03-01 11:21:44 +00001505
Paul Elliott6f1eda72020-06-11 20:22:00 +01001506 mbedtls_free( md0 );
1507 mbedtls_free( md1 );
1508
Hanno Beckera5780f12019-04-05 09:55:37 +01001509 return( ret );
Hanno Beckera18d1322018-01-03 14:27:32 +00001510}
1511
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001512/*
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02001513 * Populate a session structure for serialization tests.
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001514 * Choose dummy values, mostly non-0 to distinguish from the init default.
1515 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01001516static int ssl_populate_session_tls12( mbedtls_ssl_session *session,
1517 int ticket_len,
1518 const char *crt_file )
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001519{
1520#if defined(MBEDTLS_HAVE_TIME)
1521 session->start = mbedtls_time( NULL ) - 42;
1522#endif
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01001523 session->minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001524 session->ciphersuite = 0xabcd;
1525 session->compression = 1;
1526 session->id_len = sizeof( session->id );
1527 memset( session->id, 66, session->id_len );
Manuel Pégourié-Gonnard220403b2019-05-24 09:54:21 +02001528 memset( session->master, 17, sizeof( session->master ) );
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001529
Manuel Pégourié-Gonnard1f6033a2019-05-24 10:17:52 +02001530#if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_FS_IO)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01001531 if( crt_file != NULL && strlen( crt_file ) != 0 )
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001532 {
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02001533 mbedtls_x509_crt tmp_crt;
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001534 int ret;
Manuel Pégourié-Gonnard6b840702019-05-24 09:40:17 +02001535
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02001536 mbedtls_x509_crt_init( &tmp_crt );
1537 ret = mbedtls_x509_crt_parse_file( &tmp_crt, crt_file );
1538 if( ret != 0 )
1539 return( ret );
1540
1541#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1542 /* Move temporary CRT. */
Manuel Pégourié-Gonnard6b840702019-05-24 09:40:17 +02001543 session->peer_cert = mbedtls_calloc( 1, sizeof( *session->peer_cert ) );
1544 if( session->peer_cert == NULL )
1545 return( -1 );
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02001546 *session->peer_cert = tmp_crt;
1547 memset( &tmp_crt, 0, sizeof( tmp_crt ) );
1548#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
1549 /* Calculate digest of temporary CRT. */
1550 session->peer_cert_digest =
1551 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
1552 if( session->peer_cert_digest == NULL )
1553 return( -1 );
1554 ret = mbedtls_md( mbedtls_md_info_from_type(
1555 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
1556 tmp_crt.raw.p, tmp_crt.raw.len,
1557 session->peer_cert_digest );
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001558 if( ret != 0 )
1559 return( ret );
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02001560 session->peer_cert_digest_type =
1561 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
1562 session->peer_cert_digest_len =
1563 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
1564#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
1565
1566 mbedtls_x509_crt_free( &tmp_crt );
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001567 }
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02001568#else /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_FS_IO */
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001569 (void) crt_file;
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02001570#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_FS_IO */
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001571 session->verify_result = 0xdeadbeef;
1572
1573#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
1574 if( ticket_len != 0 )
1575 {
1576 session->ticket = mbedtls_calloc( 1, ticket_len );
Manuel Pégourié-Gonnard220403b2019-05-24 09:54:21 +02001577 if( session->ticket == NULL )
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001578 return( -1 );
1579 memset( session->ticket, 33, ticket_len );
1580 }
1581 session->ticket_len = ticket_len;
1582 session->ticket_lifetime = 86401;
1583#else
1584 (void) ticket_len;
1585#endif
1586
1587#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1588 session->mfl_code = 1;
1589#endif
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001590#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1591 session->encrypt_then_mac = 1;
1592#endif
1593
1594 return( 0 );
1595}
1596
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001597/*
1598 * Perform data exchanging between \p ssl_1 and \p ssl_2 and check if the
1599 * message was sent in the correct number of fragments.
1600 *
1601 * /p ssl_1 and /p ssl_2 Endpoints represented by mbedtls_ssl_context. Both
1602 * of them must be initialized and connected beforehand.
1603 * /p msg_len_1 and /p msg_len_2 specify the size of the message to send.
1604 * /p expected_fragments_1 and /p expected_fragments_2 determine in how many
1605 * fragments the message should be sent.
1606 * expected_fragments is 0: can be used for DTLS testing while the message
1607 * size is larger than MFL. In that case the message
1608 * cannot be fragmented and sent to the second endpoint.
1609 * This value can be used for negative tests.
1610 * expected_fragments is 1: can be used for TLS/DTLS testing while the
1611 * message size is below MFL
1612 * expected_fragments > 1: can be used for TLS testing while the message
1613 * size is larger than MFL
1614 *
1615 * \retval 0 on success, otherwise error code.
1616 */
1617int mbedtls_exchange_data( mbedtls_ssl_context *ssl_1,
1618 int msg_len_1, const int expected_fragments_1,
1619 mbedtls_ssl_context *ssl_2,
1620 int msg_len_2, const int expected_fragments_2 )
1621{
1622 unsigned char *msg_buf_1 = malloc( msg_len_1 );
1623 unsigned char *msg_buf_2 = malloc( msg_len_2 );
1624 unsigned char *in_buf_1 = malloc( msg_len_2 );
1625 unsigned char *in_buf_2 = malloc( msg_len_1 );
1626 int msg_type, ret = -1;
1627
1628 /* Perform this test with two message types. At first use a message
1629 * consisting of only 0x00 for the client and only 0xFF for the server.
1630 * At the second time use message with generated data */
1631 for( msg_type = 0; msg_type < 2; msg_type++ )
1632 {
1633 int written_1 = 0;
1634 int written_2 = 0;
1635 int read_1 = 0;
1636 int read_2 = 0;
1637 int fragments_1 = 0;
1638 int fragments_2 = 0;
1639
1640 if( msg_type == 0 )
1641 {
1642 memset( msg_buf_1, 0x00, msg_len_1 );
1643 memset( msg_buf_2, 0xff, msg_len_2 );
1644 }
1645 else
1646 {
1647 int i, j = 0;
1648 for( i = 0; i < msg_len_1; i++ )
1649 {
1650 msg_buf_1[i] = j++ & 0xFF;
1651 }
1652 for( i = 0; i < msg_len_2; i++ )
1653 {
1654 msg_buf_2[i] = ( j -= 5 ) & 0xFF;
1655 }
1656 }
1657
1658 while( read_1 < msg_len_2 || read_2 < msg_len_1 )
1659 {
1660 /* ssl_1 sending */
1661 if( msg_len_1 > written_1 )
1662 {
1663 ret = mbedtls_ssl_write_fragment( ssl_1, msg_buf_1,
1664 msg_len_1, &written_1,
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001665 expected_fragments_1 );
1666 if( expected_fragments_1 == 0 )
1667 {
1668 /* This error is expected when the message is too large and
1669 * cannot be fragmented */
1670 TEST_ASSERT( ret == MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1671 msg_len_1 = 0;
1672 }
1673 else
1674 {
1675 TEST_ASSERT( ret == 0 );
1676 }
1677 }
1678
1679 /* ssl_2 sending */
1680 if( msg_len_2 > written_2 )
1681 {
1682 ret = mbedtls_ssl_write_fragment( ssl_2, msg_buf_2,
1683 msg_len_2, &written_2,
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001684 expected_fragments_2 );
1685 if( expected_fragments_2 == 0 )
1686 {
1687 /* This error is expected when the message is too large and
1688 * cannot be fragmented */
1689 TEST_ASSERT( ret == MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1690 msg_len_2 = 0;
1691 }
1692 else
1693 {
1694 TEST_ASSERT( ret == 0 );
1695 }
1696 }
1697
1698 /* ssl_1 reading */
1699 if( read_1 < msg_len_2 )
1700 {
1701 ret = mbedtls_ssl_read_fragment( ssl_1, in_buf_1,
1702 msg_len_2, &read_1,
Piotr Nowicki438bf3b2020-03-10 12:59:10 +01001703 &fragments_2,
1704 expected_fragments_2 );
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001705 TEST_ASSERT( ret == 0 );
1706 }
1707
1708 /* ssl_2 reading */
1709 if( read_2 < msg_len_1 )
1710 {
1711 ret = mbedtls_ssl_read_fragment( ssl_2, in_buf_2,
1712 msg_len_1, &read_2,
Piotr Nowicki438bf3b2020-03-10 12:59:10 +01001713 &fragments_1,
1714 expected_fragments_1 );
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001715 TEST_ASSERT( ret == 0 );
1716 }
1717 }
1718
1719 ret = -1;
1720 TEST_ASSERT( 0 == memcmp( msg_buf_1, in_buf_2, msg_len_1 ) );
1721 TEST_ASSERT( 0 == memcmp( msg_buf_2, in_buf_1, msg_len_2 ) );
1722 TEST_ASSERT( fragments_1 == expected_fragments_1 );
1723 TEST_ASSERT( fragments_2 == expected_fragments_2 );
1724 }
1725
1726 ret = 0;
1727
1728exit:
1729 free( msg_buf_1 );
1730 free( in_buf_1 );
1731 free( msg_buf_2 );
1732 free( in_buf_2 );
1733
1734 return ret;
1735}
1736
Piotr Nowicki95e9eb82020-02-14 11:33:34 +01001737/*
1738 * Perform data exchanging between \p ssl_1 and \p ssl_2. Both of endpoints
1739 * must be initialized and connected beforehand.
1740 *
1741 * \retval 0 on success, otherwise error code.
1742 */
1743int exchange_data( mbedtls_ssl_context *ssl_1,
1744 mbedtls_ssl_context *ssl_2 )
1745{
1746 return mbedtls_exchange_data( ssl_1, 256, 1,
1747 ssl_2, 256, 1 );
1748}
1749
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02001750#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
1751 defined(MBEDTLS_ENTROPY_C) && \
1752 defined(MBEDTLS_CTR_DRBG_C)
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001753void perform_handshake( handshake_test_options* options )
1754{
1755 /* forced_ciphersuite needs to last until the end of the handshake */
1756 int forced_ciphersuite[2];
1757 enum { BUFFSIZE = 17000 };
1758 mbedtls_endpoint client, server;
Gilles Peskineeccd8882020-03-10 12:19:08 +01001759#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001760 const char *psk_identity = "foo";
1761#endif
1762#if defined(MBEDTLS_TIMING_C)
1763 mbedtls_timing_delay_context timer_client, timer_server;
1764#endif
1765#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1766 unsigned char *context_buf = NULL;
1767 size_t context_buf_len;
1768#endif
1769#if defined(MBEDTLS_SSL_RENEGOTIATION)
1770 int ret = -1;
1771#endif
Paul Elliottc8570442020-04-15 17:00:50 +01001772 int expected_handshake_result = 0;
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001773
1774 mbedtls_test_message_queue server_queue, client_queue;
1775 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05001776 mbedtls_message_socket_init( &server_context );
1777 mbedtls_message_socket_init( &client_context );
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001778
1779 /* Client side */
1780 if( options->dtls != 0 )
1781 {
1782 TEST_ASSERT( mbedtls_endpoint_init( &client, MBEDTLS_SSL_IS_CLIENT,
1783 options->pk_alg, &client_context,
1784 &client_queue,
1785 &server_queue ) == 0 );
1786#if defined(MBEDTLS_TIMING_C)
1787 mbedtls_ssl_set_timer_cb( &client.ssl, &timer_client,
1788 mbedtls_timing_set_delay,
1789 mbedtls_timing_get_delay );
1790#endif
1791 }
1792 else
1793 {
1794 TEST_ASSERT( mbedtls_endpoint_init( &client, MBEDTLS_SSL_IS_CLIENT,
1795 options->pk_alg, NULL, NULL,
1796 NULL ) == 0 );
1797 }
Paul Elliottc8570442020-04-15 17:00:50 +01001798
1799 if( options->client_min_version != TEST_SSL_MINOR_VERSION_NONE )
1800 {
1801 mbedtls_ssl_conf_min_version( &client.conf, MBEDTLS_SSL_MAJOR_VERSION_3,
1802 options->client_min_version );
1803 }
1804
1805 if( options->client_max_version != TEST_SSL_MINOR_VERSION_NONE )
1806 {
1807 mbedtls_ssl_conf_max_version( &client.conf, MBEDTLS_SSL_MAJOR_VERSION_3,
1808 options->client_max_version );
1809 }
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001810
1811 if( strlen( options->cipher ) > 0 )
1812 {
1813 set_ciphersuite( &client.conf, options->cipher, forced_ciphersuite );
1814 }
Piotr Nowickibde7ee82020-02-21 10:59:50 +01001815
1816#if defined (MBEDTLS_DEBUG_C)
1817 if( options->cli_log_fun )
1818 {
1819 mbedtls_debug_set_threshold( 4 );
1820 mbedtls_ssl_conf_dbg( &client.conf, options->cli_log_fun,
1821 options->cli_log_obj );
1822 }
1823#endif
1824
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001825 /* Server side */
1826 if( options->dtls != 0 )
1827 {
1828 TEST_ASSERT( mbedtls_endpoint_init( &server, MBEDTLS_SSL_IS_SERVER,
1829 options->pk_alg, &server_context,
1830 &server_queue,
1831 &client_queue) == 0 );
1832#if defined(MBEDTLS_TIMING_C)
1833 mbedtls_ssl_set_timer_cb( &server.ssl, &timer_server,
1834 mbedtls_timing_set_delay,
1835 mbedtls_timing_get_delay );
1836#endif
1837 }
1838 else
1839 {
1840 TEST_ASSERT( mbedtls_endpoint_init( &server, MBEDTLS_SSL_IS_SERVER,
1841 options->pk_alg, NULL, NULL, NULL ) == 0 );
1842 }
Piotr Nowickibde7ee82020-02-21 10:59:50 +01001843
1844 mbedtls_ssl_conf_authmode( &server.conf, options->srv_auth_mode );
1845
Paul Elliottc8570442020-04-15 17:00:50 +01001846 if( options->server_min_version != TEST_SSL_MINOR_VERSION_NONE )
1847 {
1848 mbedtls_ssl_conf_min_version( &server.conf, MBEDTLS_SSL_MAJOR_VERSION_3,
1849 options->server_min_version );
1850 }
1851
1852 if( options->server_max_version != TEST_SSL_MINOR_VERSION_NONE )
1853 {
1854 mbedtls_ssl_conf_max_version( &server.conf, MBEDTLS_SSL_MAJOR_VERSION_3,
1855 options->server_max_version );
1856 }
1857
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001858#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1859 TEST_ASSERT( mbedtls_ssl_conf_max_frag_len( &(server.conf),
1860 (unsigned char) options->mfl ) == 0 );
1861 TEST_ASSERT( mbedtls_ssl_conf_max_frag_len( &(client.conf),
1862 (unsigned char) options->mfl ) == 0 );
1863#else
1864 TEST_ASSERT( MBEDTLS_SSL_MAX_FRAG_LEN_NONE == options->mfl );
1865#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
1866
Gilles Peskineeccd8882020-03-10 12:19:08 +01001867#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001868 if( options->psk_str != NULL && options->psk_str->len > 0 )
1869 {
1870 TEST_ASSERT( mbedtls_ssl_conf_psk( &client.conf, options->psk_str->x,
1871 options->psk_str->len,
1872 (const unsigned char *) psk_identity,
1873 strlen( psk_identity ) ) == 0 );
1874
1875 TEST_ASSERT( mbedtls_ssl_conf_psk( &server.conf, options->psk_str->x,
1876 options->psk_str->len,
1877 (const unsigned char *) psk_identity,
1878 strlen( psk_identity ) ) == 0 );
1879
1880 mbedtls_ssl_conf_psk_cb( &server.conf, psk_dummy_callback, NULL );
1881 }
1882#endif
1883#if defined(MBEDTLS_SSL_RENEGOTIATION)
1884 if( options->renegotiate )
1885 {
1886 mbedtls_ssl_conf_renegotiation( &(server.conf),
1887 MBEDTLS_SSL_RENEGOTIATION_ENABLED );
1888 mbedtls_ssl_conf_renegotiation( &(client.conf),
1889 MBEDTLS_SSL_RENEGOTIATION_ENABLED );
1890
1891 mbedtls_ssl_conf_legacy_renegotiation( &(server.conf),
1892 options->legacy_renegotiation );
1893 mbedtls_ssl_conf_legacy_renegotiation( &(client.conf),
1894 options->legacy_renegotiation );
1895 }
1896#endif /* MBEDTLS_SSL_RENEGOTIATION */
1897
Piotr Nowickibde7ee82020-02-21 10:59:50 +01001898#if defined (MBEDTLS_DEBUG_C)
1899 if( options->srv_log_fun )
1900 {
1901 mbedtls_debug_set_threshold( 4 );
1902 mbedtls_ssl_conf_dbg( &server.conf, options->srv_log_fun,
1903 options->srv_log_obj );
1904 }
1905#endif
1906
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001907 TEST_ASSERT( mbedtls_mock_socket_connect( &(client.socket),
1908 &(server.socket),
1909 BUFFSIZE ) == 0 );
1910
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001911#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1912 if( options->resize_buffers != 0 )
1913 {
1914 /* Ensure that the buffer sizes are appropriate before resizes */
1915 TEST_ASSERT( client.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN );
1916 TEST_ASSERT( client.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN );
1917 TEST_ASSERT( server.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN );
1918 TEST_ASSERT( server.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN );
1919 }
1920#endif
1921
Paul Elliottc8570442020-04-15 17:00:50 +01001922 if( options->expected_negotiated_version == TEST_SSL_MINOR_VERSION_NONE )
1923 {
Hanno Beckerbc000442021-06-24 09:18:19 +01001924 expected_handshake_result = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
Paul Elliottc8570442020-04-15 17:00:50 +01001925 }
1926
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001927 TEST_ASSERT( mbedtls_move_handshake_to_state( &(client.ssl),
1928 &(server.ssl),
1929 MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Elliottc8570442020-04-15 17:00:50 +01001930 == expected_handshake_result );
1931
1932 if( expected_handshake_result != 0 )
1933 {
1934 /* Connection will have failed by this point, skip to cleanup */
1935 goto exit;
1936 }
1937
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001938 TEST_ASSERT( client.ssl.state == MBEDTLS_SSL_HANDSHAKE_OVER );
1939 TEST_ASSERT( server.ssl.state == MBEDTLS_SSL_HANDSHAKE_OVER );
1940
Paul Elliottc8570442020-04-15 17:00:50 +01001941 /* Check that we agree on the version... */
1942 TEST_ASSERT( client.ssl.minor_ver == server.ssl.minor_ver );
1943
1944 /* And check that the version negotiated is the expected one. */
1945 TEST_EQUAL( client.ssl.minor_ver, options->expected_negotiated_version );
1946
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001947#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1948 if( options->resize_buffers != 0 )
1949 {
TRodziewicz2abf03c2021-06-25 14:40:09 +02001950 /* A server, when using DTLS, might delay a buffer resize to happen
1951 * after it receives a message, so we force it. */
1952 TEST_ASSERT( exchange_data( &(client.ssl), &(server.ssl) ) == 0 );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001953
TRodziewicz2abf03c2021-06-25 14:40:09 +02001954 TEST_ASSERT( client.ssl.out_buf_len ==
1955 mbedtls_ssl_get_output_buflen( &client.ssl ) );
1956 TEST_ASSERT( client.ssl.in_buf_len ==
1957 mbedtls_ssl_get_input_buflen( &client.ssl ) );
1958 TEST_ASSERT( server.ssl.out_buf_len ==
1959 mbedtls_ssl_get_output_buflen( &server.ssl ) );
1960 TEST_ASSERT( server.ssl.in_buf_len ==
1961 mbedtls_ssl_get_input_buflen( &server.ssl ) );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001962 }
1963#endif
1964
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001965 if( options->cli_msg_len != 0 || options->srv_msg_len != 0 )
1966 {
1967 /* Start data exchanging test */
1968 TEST_ASSERT( mbedtls_exchange_data( &(client.ssl), options->cli_msg_len,
1969 options->expected_cli_fragments,
1970 &(server.ssl), options->srv_msg_len,
1971 options->expected_srv_fragments )
1972 == 0 );
1973 }
1974#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1975 if( options->serialize == 1 )
1976 {
1977 TEST_ASSERT( options->dtls == 1 );
1978
1979 TEST_ASSERT( mbedtls_ssl_context_save( &(server.ssl), NULL,
1980 0, &context_buf_len )
1981 == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1982
1983 context_buf = mbedtls_calloc( 1, context_buf_len );
1984 TEST_ASSERT( context_buf != NULL );
1985
1986 TEST_ASSERT( mbedtls_ssl_context_save( &(server.ssl), context_buf,
1987 context_buf_len,
1988 &context_buf_len ) == 0 );
1989
1990 mbedtls_ssl_free( &(server.ssl) );
1991 mbedtls_ssl_init( &(server.ssl) );
1992
1993 TEST_ASSERT( mbedtls_ssl_setup( &(server.ssl), &(server.conf) ) == 0 );
1994
1995 mbedtls_ssl_set_bio( &( server.ssl ), &server_context,
1996 mbedtls_mock_tcp_send_msg,
1997 mbedtls_mock_tcp_recv_msg,
1998 NULL );
1999
2000#if defined(MBEDTLS_TIMING_C)
2001 mbedtls_ssl_set_timer_cb( &server.ssl, &timer_server,
2002 mbedtls_timing_set_delay,
2003 mbedtls_timing_get_delay );
2004#endif
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05002005#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2006 if( options->resize_buffers != 0 )
2007 {
2008 /* Ensure that the buffer sizes are appropriate before resizes */
2009 TEST_ASSERT( server.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN );
2010 TEST_ASSERT( server.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN );
2011 }
2012#endif
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002013 TEST_ASSERT( mbedtls_ssl_context_load( &( server.ssl ), context_buf,
2014 context_buf_len ) == 0 );
2015
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05002016#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2017 /* Validate buffer sizes after context deserialization */
2018 if( options->resize_buffers != 0 )
2019 {
2020 TEST_ASSERT( server.ssl.out_buf_len ==
2021 mbedtls_ssl_get_output_buflen( &server.ssl ) );
2022 TEST_ASSERT( server.ssl.in_buf_len ==
2023 mbedtls_ssl_get_input_buflen( &server.ssl ) );
2024 }
2025#endif
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002026 /* Retest writing/reading */
2027 if( options->cli_msg_len != 0 || options->srv_msg_len != 0 )
2028 {
2029 TEST_ASSERT( mbedtls_exchange_data( &(client.ssl),
2030 options->cli_msg_len,
2031 options->expected_cli_fragments,
2032 &(server.ssl),
2033 options->srv_msg_len,
2034 options->expected_srv_fragments )
2035 == 0 );
2036 }
2037 }
2038#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05002039
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002040#if defined(MBEDTLS_SSL_RENEGOTIATION)
2041 if( options->renegotiate )
2042 {
2043 /* Start test with renegotiation */
2044 TEST_ASSERT( server.ssl.renego_status ==
2045 MBEDTLS_SSL_INITIAL_HANDSHAKE );
2046 TEST_ASSERT( client.ssl.renego_status ==
2047 MBEDTLS_SSL_INITIAL_HANDSHAKE );
2048
2049 /* After calling this function for the server, it only sends a handshake
2050 * request. All renegotiation should happen during data exchanging */
2051 TEST_ASSERT( mbedtls_ssl_renegotiate( &(server.ssl) ) == 0 );
2052 TEST_ASSERT( server.ssl.renego_status ==
2053 MBEDTLS_SSL_RENEGOTIATION_PENDING );
2054 TEST_ASSERT( client.ssl.renego_status ==
2055 MBEDTLS_SSL_INITIAL_HANDSHAKE );
2056
2057 TEST_ASSERT( exchange_data( &(client.ssl), &(server.ssl) ) == 0 );
2058 TEST_ASSERT( server.ssl.renego_status ==
2059 MBEDTLS_SSL_RENEGOTIATION_DONE );
2060 TEST_ASSERT( client.ssl.renego_status ==
2061 MBEDTLS_SSL_RENEGOTIATION_DONE );
2062
2063 /* After calling mbedtls_ssl_renegotiate for the client all renegotiation
2064 * should happen inside this function. However in this test, we cannot
2065 * perform simultaneous communication betwen client and server so this
2066 * function will return waiting error on the socket. All rest of
2067 * renegotiation should happen during data exchanging */
2068 ret = mbedtls_ssl_renegotiate( &(client.ssl) );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05002069#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2070 if( options->resize_buffers != 0 )
2071 {
2072 /* Ensure that the buffer sizes are appropriate before resizes */
2073 TEST_ASSERT( client.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN );
2074 TEST_ASSERT( client.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN );
2075 }
2076#endif
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002077 TEST_ASSERT( ret == 0 ||
2078 ret == MBEDTLS_ERR_SSL_WANT_READ ||
2079 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
2080 TEST_ASSERT( server.ssl.renego_status ==
2081 MBEDTLS_SSL_RENEGOTIATION_DONE );
2082 TEST_ASSERT( client.ssl.renego_status ==
2083 MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS );
2084
2085 TEST_ASSERT( exchange_data( &(client.ssl), &(server.ssl) ) == 0 );
2086 TEST_ASSERT( server.ssl.renego_status ==
2087 MBEDTLS_SSL_RENEGOTIATION_DONE );
2088 TEST_ASSERT( client.ssl.renego_status ==
2089 MBEDTLS_SSL_RENEGOTIATION_DONE );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05002090#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2091 /* Validate buffer sizes after renegotiation */
2092 if( options->resize_buffers != 0 )
2093 {
2094 TEST_ASSERT( client.ssl.out_buf_len ==
2095 mbedtls_ssl_get_output_buflen( &client.ssl ) );
2096 TEST_ASSERT( client.ssl.in_buf_len ==
2097 mbedtls_ssl_get_input_buflen( &client.ssl ) );
2098 TEST_ASSERT( server.ssl.out_buf_len ==
2099 mbedtls_ssl_get_output_buflen( &server.ssl ) );
2100 TEST_ASSERT( server.ssl.in_buf_len ==
2101 mbedtls_ssl_get_input_buflen( &server.ssl ) );
2102 }
2103#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002104 }
2105#endif /* MBEDTLS_SSL_RENEGOTIATION */
2106
2107exit:
2108 mbedtls_endpoint_free( &client, options->dtls != 0 ? &client_context : NULL );
2109 mbedtls_endpoint_free( &server, options->dtls != 0 ? &server_context : NULL );
Piotr Nowickibde7ee82020-02-21 10:59:50 +01002110#if defined (MBEDTLS_DEBUG_C)
2111 if( options->cli_log_fun || options->srv_log_fun )
2112 {
2113 mbedtls_debug_set_threshold( 0 );
2114 }
2115#endif
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002116#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
2117 if( context_buf != NULL )
2118 mbedtls_free( context_buf );
2119#endif
2120}
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02002121#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002122
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002123/* END_HEADER */
2124
2125/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002126 * depends_on:MBEDTLS_SSL_TLS_C
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002127 * END_DEPENDENCIES
2128 */
2129
Janos Follath6264e662019-11-26 11:11:15 +00002130/* BEGIN_CASE */
2131void test_callback_buffer_sanity()
2132{
2133 enum { MSGLEN = 10 };
2134 mbedtls_test_buffer buf;
2135 unsigned char input[MSGLEN];
2136 unsigned char output[MSGLEN];
2137
2138 memset( input, 0, sizeof(input) );
2139
2140 /* Make sure calling put and get on NULL buffer results in error. */
2141 TEST_ASSERT( mbedtls_test_buffer_put( NULL, input, sizeof( input ) )
2142 == -1 );
2143 TEST_ASSERT( mbedtls_test_buffer_get( NULL, output, sizeof( output ) )
2144 == -1 );
2145 TEST_ASSERT( mbedtls_test_buffer_put( NULL, NULL, sizeof( input ) ) == -1 );
Andrzej Kurekf7774142020-01-22 06:34:59 -05002146
Janos Follath6264e662019-11-26 11:11:15 +00002147 TEST_ASSERT( mbedtls_test_buffer_put( NULL, NULL, 0 ) == -1 );
2148 TEST_ASSERT( mbedtls_test_buffer_get( NULL, NULL, 0 ) == -1 );
2149
2150 /* Make sure calling put and get on a buffer that hasn't been set up results
2151 * in eror. */
2152 mbedtls_test_buffer_init( &buf );
2153
2154 TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, sizeof( input ) ) == -1 );
2155 TEST_ASSERT( mbedtls_test_buffer_get( &buf, output, sizeof( output ) )
2156 == -1 );
2157 TEST_ASSERT( mbedtls_test_buffer_put( &buf, NULL, sizeof( input ) ) == -1 );
Andrzej Kurekf7774142020-01-22 06:34:59 -05002158
Janos Follath6264e662019-11-26 11:11:15 +00002159 TEST_ASSERT( mbedtls_test_buffer_put( &buf, NULL, 0 ) == -1 );
2160 TEST_ASSERT( mbedtls_test_buffer_get( &buf, NULL, 0 ) == -1 );
2161
Andrzej Kurekf7774142020-01-22 06:34:59 -05002162 /* Make sure calling put and get on NULL input only results in
2163 * error if the length is not zero, and that a NULL output is valid for data
2164 * dropping.
2165 */
Janos Follath6264e662019-11-26 11:11:15 +00002166
2167 TEST_ASSERT( mbedtls_test_buffer_setup( &buf, sizeof( input ) ) == 0 );
2168
2169 TEST_ASSERT( mbedtls_test_buffer_put( &buf, NULL, sizeof( input ) ) == -1 );
2170 TEST_ASSERT( mbedtls_test_buffer_get( &buf, NULL, sizeof( output ) )
Andrzej Kurekf7774142020-01-22 06:34:59 -05002171 == 0 );
Janos Follath6264e662019-11-26 11:11:15 +00002172 TEST_ASSERT( mbedtls_test_buffer_put( &buf, NULL, 0 ) == 0 );
2173 TEST_ASSERT( mbedtls_test_buffer_get( &buf, NULL, 0 ) == 0 );
2174
Piotr Nowickifb437d72020-01-13 16:59:12 +01002175 /* Make sure calling put several times in the row is safe */
2176
2177 TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, sizeof( input ) )
2178 == sizeof( input ) );
2179 TEST_ASSERT( mbedtls_test_buffer_get( &buf, output, 2 ) == 2 );
2180 TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, 1 ) == 1 );
2181 TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, 2 ) == 1 );
2182 TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, 2 ) == 0 );
2183
2184
Janos Follath6264e662019-11-26 11:11:15 +00002185exit:
2186
2187 mbedtls_test_buffer_free( &buf );
2188}
2189/* END_CASE */
2190
2191/*
2192 * Test if the implementation of `mbedtls_test_buffer` related functions is
2193 * correct and works as expected.
2194 *
2195 * That is
2196 * - If we try to put in \p put1 bytes then we can put in \p put1_ret bytes.
2197 * - Afterwards if we try to get \p get1 bytes then we can get \get1_ret bytes.
2198 * - Next, if we try to put in \p put1 bytes then we can put in \p put1_ret
2199 * bytes.
2200 * - Afterwards if we try to get \p get1 bytes then we can get \get1_ret bytes.
2201 * - All of the bytes we got match the bytes we put in in a FIFO manner.
2202 */
2203
2204/* BEGIN_CASE */
2205void test_callback_buffer( int size, int put1, int put1_ret,
2206 int get1, int get1_ret, int put2, int put2_ret,
2207 int get2, int get2_ret )
2208{
2209 enum { ROUNDS = 2 };
2210 size_t put[ROUNDS];
2211 int put_ret[ROUNDS];
2212 size_t get[ROUNDS];
2213 int get_ret[ROUNDS];
2214 mbedtls_test_buffer buf;
2215 unsigned char* input = NULL;
2216 size_t input_len;
2217 unsigned char* output = NULL;
2218 size_t output_len;
Janos Follath031827f2019-11-27 11:12:14 +00002219 size_t i, j, written, read;
Janos Follath6264e662019-11-26 11:11:15 +00002220
2221 mbedtls_test_buffer_init( &buf );
2222 TEST_ASSERT( mbedtls_test_buffer_setup( &buf, size ) == 0 );
2223
2224 /* Check the sanity of input parameters and initialise local variables. That
2225 * is, ensure that the amount of data is not negative and that we are not
2226 * expecting more to put or get than we actually asked for. */
2227 TEST_ASSERT( put1 >= 0 );
2228 put[0] = put1;
2229 put_ret[0] = put1_ret;
2230 TEST_ASSERT( put1_ret <= put1 );
2231 TEST_ASSERT( put2 >= 0 );
2232 put[1] = put2;
2233 put_ret[1] = put2_ret;
2234 TEST_ASSERT( put2_ret <= put2 );
2235
2236 TEST_ASSERT( get1 >= 0 );
2237 get[0] = get1;
2238 get_ret[0] = get1_ret;
2239 TEST_ASSERT( get1_ret <= get1 );
2240 TEST_ASSERT( get2 >= 0 );
2241 get[1] = get2;
2242 get_ret[1] = get2_ret;
2243 TEST_ASSERT( get2_ret <= get2 );
2244
2245 input_len = 0;
2246 /* Calculate actual input and output lengths */
2247 for( j = 0; j < ROUNDS; j++ )
2248 {
2249 if( put_ret[j] > 0 )
2250 {
2251 input_len += put_ret[j];
2252 }
2253 }
2254 /* In order to always have a valid pointer we always allocate at least 1
2255 * byte. */
2256 if( input_len == 0 )
2257 input_len = 1;
2258 ASSERT_ALLOC( input, input_len );
2259
2260 output_len = 0;
2261 for( j = 0; j < ROUNDS; j++ )
2262 {
2263 if( get_ret[j] > 0 )
2264 {
2265 output_len += get_ret[j];
2266 }
2267 }
2268 TEST_ASSERT( output_len <= input_len );
2269 /* In order to always have a valid pointer we always allocate at least 1
2270 * byte. */
2271 if( output_len == 0 )
2272 output_len = 1;
2273 ASSERT_ALLOC( output, output_len );
2274
2275 /* Fill up the buffer with structured data so that unwanted changes
2276 * can be detected */
2277 for( i = 0; i < input_len; i++ )
2278 {
2279 input[i] = i & 0xFF;
2280 }
2281
2282 written = read = 0;
2283 for( j = 0; j < ROUNDS; j++ )
2284 {
2285 TEST_ASSERT( put_ret[j] == mbedtls_test_buffer_put( &buf,
2286 input + written, put[j] ) );
2287 written += put_ret[j];
2288 TEST_ASSERT( get_ret[j] == mbedtls_test_buffer_get( &buf,
2289 output + read, get[j] ) );
2290 read += get_ret[j];
2291 TEST_ASSERT( read <= written );
2292 if( get_ret[j] > 0 )
2293 {
2294 TEST_ASSERT( memcmp( output + read - get_ret[j],
2295 input + read - get_ret[j], get_ret[j] )
2296 == 0 );
2297 }
2298 }
2299
2300exit:
2301
2302 mbedtls_free( input );
2303 mbedtls_free( output );
2304 mbedtls_test_buffer_free( &buf );
2305}
2306/* END_CASE */
2307
Janos Follath031827f2019-11-27 11:12:14 +00002308/*
Janos Follathc673c2c2019-12-02 15:47:26 +00002309 * Test if the implementation of `mbedtls_mock_socket` related I/O functions is
2310 * correct and works as expected on unconnected sockets.
2311 */
2312
2313/* BEGIN_CASE */
2314void ssl_mock_sanity( )
2315{
2316 enum { MSGLEN = 105 };
Paul Elliott21c8fe52021-11-24 16:54:26 +00002317 unsigned char message[MSGLEN] = { 0 };
2318 unsigned char received[MSGLEN] = { 0 };
Janos Follathc673c2c2019-12-02 15:47:26 +00002319 mbedtls_mock_socket socket;
2320
2321 mbedtls_mock_socket_init( &socket );
2322 TEST_ASSERT( mbedtls_mock_tcp_send_b( &socket, message, MSGLEN ) < 0 );
2323 mbedtls_mock_socket_close( &socket );
2324 mbedtls_mock_socket_init( &socket );
2325 TEST_ASSERT( mbedtls_mock_tcp_recv_b( &socket, received, MSGLEN ) < 0 );
2326 mbedtls_mock_socket_close( &socket );
2327
2328 mbedtls_mock_socket_init( &socket );
2329 TEST_ASSERT( mbedtls_mock_tcp_send_nb( &socket, message, MSGLEN ) < 0 );
2330 mbedtls_mock_socket_close( &socket );
2331 mbedtls_mock_socket_init( &socket );
2332 TEST_ASSERT( mbedtls_mock_tcp_recv_nb( &socket, received, MSGLEN ) < 0 );
2333 mbedtls_mock_socket_close( &socket );
2334
2335exit:
2336
2337 mbedtls_mock_socket_close( &socket );
2338}
2339/* END_CASE */
2340
2341/*
2342 * Test if the implementation of `mbedtls_mock_socket` related functions can
2343 * send a single message from the client to the server.
Janos Follath031827f2019-11-27 11:12:14 +00002344 */
2345
2346/* BEGIN_CASE */
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002347void ssl_mock_tcp( int blocking )
Janos Follath031827f2019-11-27 11:12:14 +00002348{
Janos Follathc673c2c2019-12-02 15:47:26 +00002349 enum { MSGLEN = 105 };
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002350 enum { BUFLEN = MSGLEN / 5 };
Janos Follathc673c2c2019-12-02 15:47:26 +00002351 unsigned char message[MSGLEN];
2352 unsigned char received[MSGLEN];
2353 mbedtls_mock_socket client;
2354 mbedtls_mock_socket server;
2355 size_t written, read;
2356 int send_ret, recv_ret;
2357 mbedtls_ssl_send_t *send;
2358 mbedtls_ssl_recv_t *recv;
Janos Follathc673c2c2019-12-02 15:47:26 +00002359 unsigned i;
2360
2361 if( blocking == 0 )
2362 {
2363 send = mbedtls_mock_tcp_send_nb;
2364 recv = mbedtls_mock_tcp_recv_nb;
2365 }
2366 else
2367 {
2368 send = mbedtls_mock_tcp_send_b;
2369 recv = mbedtls_mock_tcp_recv_b;
2370 }
2371
2372 mbedtls_mock_socket_init( &client );
2373 mbedtls_mock_socket_init( &server );
2374
2375 /* Fill up the buffer with structured data so that unwanted changes
2376 * can be detected */
2377 for( i = 0; i < MSGLEN; i++ )
2378 {
2379 message[i] = i & 0xFF;
2380 }
2381
2382 /* Make sure that sending a message takes a few iterations. */
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002383 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, BUFLEN ) );
Janos Follathc673c2c2019-12-02 15:47:26 +00002384
2385 /* Send the message to the server */
2386 send_ret = recv_ret = 1;
2387 written = read = 0;
2388 while( send_ret != 0 || recv_ret != 0 )
2389 {
2390 send_ret = send( &client, message + written, MSGLEN - written );
2391
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002392 TEST_ASSERT( send_ret >= 0 );
2393 TEST_ASSERT( send_ret <= BUFLEN );
2394 written += send_ret;
2395
2396 /* If the buffer is full we can test blocking and non-blocking send */
2397 if ( send_ret == BUFLEN )
Janos Follathc673c2c2019-12-02 15:47:26 +00002398 {
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002399 int blocking_ret = send( &client, message , 1 );
2400 if ( blocking )
2401 {
2402 TEST_ASSERT( blocking_ret == 0 );
2403 }
2404 else
2405 {
2406 TEST_ASSERT( blocking_ret == MBEDTLS_ERR_SSL_WANT_WRITE );
2407 }
Janos Follathc673c2c2019-12-02 15:47:26 +00002408 }
Janos Follathc673c2c2019-12-02 15:47:26 +00002409
2410 recv_ret = recv( &server, received + read, MSGLEN - read );
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002411
2412 /* The result depends on whether any data was sent */
2413 if ( send_ret > 0 )
Janos Follathc673c2c2019-12-02 15:47:26 +00002414 {
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002415 TEST_ASSERT( recv_ret > 0 );
2416 TEST_ASSERT( recv_ret <= BUFLEN );
2417 read += recv_ret;
2418 }
2419 else if( blocking )
2420 {
2421 TEST_ASSERT( recv_ret == 0 );
Janos Follathc673c2c2019-12-02 15:47:26 +00002422 }
2423 else
2424 {
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002425 TEST_ASSERT( recv_ret == MBEDTLS_ERR_SSL_WANT_READ );
2426 recv_ret = 0;
Janos Follathc673c2c2019-12-02 15:47:26 +00002427 }
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002428
2429 /* If the buffer is empty we can test blocking and non-blocking read */
2430 if ( recv_ret == BUFLEN )
2431 {
2432 int blocking_ret = recv( &server, received, 1 );
2433 if ( blocking )
2434 {
2435 TEST_ASSERT( blocking_ret == 0 );
2436 }
2437 else
2438 {
2439 TEST_ASSERT( blocking_ret == MBEDTLS_ERR_SSL_WANT_READ );
2440 }
2441 }
Janos Follathc673c2c2019-12-02 15:47:26 +00002442 }
2443 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
2444
2445exit:
2446
2447 mbedtls_mock_socket_close( &client );
2448 mbedtls_mock_socket_close( &server );
2449}
2450/* END_CASE */
2451
2452/*
2453 * Test if the implementation of `mbedtls_mock_socket` related functions can
2454 * send messages in both direction at the same time (with the I/O calls
2455 * interleaving).
2456 */
2457
2458/* BEGIN_CASE */
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002459void ssl_mock_tcp_interleaving( int blocking )
Janos Follathc673c2c2019-12-02 15:47:26 +00002460{
Janos Follath031827f2019-11-27 11:12:14 +00002461 enum { ROUNDS = 2 };
2462 enum { MSGLEN = 105 };
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002463 enum { BUFLEN = MSGLEN / 5 };
Janos Follath031827f2019-11-27 11:12:14 +00002464 unsigned char message[ROUNDS][MSGLEN];
2465 unsigned char received[ROUNDS][MSGLEN];
2466 mbedtls_mock_socket client;
2467 mbedtls_mock_socket server;
2468 size_t written[ROUNDS];
2469 size_t read[ROUNDS];
2470 int send_ret[ROUNDS];
2471 int recv_ret[ROUNDS];
2472 unsigned i, j, progress;
Janos Follath3766ba52019-11-27 13:31:42 +00002473 mbedtls_ssl_send_t *send;
2474 mbedtls_ssl_recv_t *recv;
Janos Follath3766ba52019-11-27 13:31:42 +00002475
2476 if( blocking == 0 )
2477 {
2478 send = mbedtls_mock_tcp_send_nb;
2479 recv = mbedtls_mock_tcp_recv_nb;
2480 }
2481 else
2482 {
2483 send = mbedtls_mock_tcp_send_b;
2484 recv = mbedtls_mock_tcp_recv_b;
2485 }
Janos Follath031827f2019-11-27 11:12:14 +00002486
2487 mbedtls_mock_socket_init( &client );
2488 mbedtls_mock_socket_init( &server );
2489
2490 /* Fill up the buffers with structured data so that unwanted changes
2491 * can be detected */
2492 for( i = 0; i < ROUNDS; i++ )
2493 {
2494 for( j = 0; j < MSGLEN; j++ )
2495 {
2496 message[i][j] = ( i * MSGLEN + j ) & 0xFF;
2497 }
2498 }
2499
Janos Follath031827f2019-11-27 11:12:14 +00002500 /* Make sure that sending a message takes a few iterations. */
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002501 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, BUFLEN ) );
Janos Follath031827f2019-11-27 11:12:14 +00002502
Janos Follath031827f2019-11-27 11:12:14 +00002503 /* Send the message from both sides, interleaving. */
2504 progress = 1;
2505 for( i = 0; i < ROUNDS; i++ )
2506 {
2507 written[i] = 0;
2508 read[i] = 0;
2509 }
2510 /* This loop does not stop as long as there was a successful write or read
2511 * of at least one byte on either side. */
2512 while( progress != 0 )
2513 {
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002514 mbedtls_mock_socket *socket;
Janos Follath031827f2019-11-27 11:12:14 +00002515
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002516 for( i = 0; i < ROUNDS; i++ )
Janos Follath3766ba52019-11-27 13:31:42 +00002517 {
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002518 /* First sending is from the client */
2519 socket = ( i % 2 == 0 ) ? ( &client ) : ( &server );
Janos Follath031827f2019-11-27 11:12:14 +00002520
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002521 send_ret[i] = send( socket, message[i] + written[i],
2522 MSGLEN - written[i] );
2523 TEST_ASSERT( send_ret[i] >= 0 );
2524 TEST_ASSERT( send_ret[i] <= BUFLEN );
2525 written[i] += send_ret[i];
Janos Follath031827f2019-11-27 11:12:14 +00002526
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002527 /* If the buffer is full we can test blocking and non-blocking
2528 * send */
2529 if ( send_ret[i] == BUFLEN )
2530 {
2531 int blocking_ret = send( socket, message[i] , 1 );
2532 if ( blocking )
2533 {
2534 TEST_ASSERT( blocking_ret == 0 );
2535 }
2536 else
2537 {
2538 TEST_ASSERT( blocking_ret == MBEDTLS_ERR_SSL_WANT_WRITE );
2539 }
2540 }
Janos Follath3766ba52019-11-27 13:31:42 +00002541 }
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002542
2543 for( i = 0; i < ROUNDS; i++ )
Janos Follath3766ba52019-11-27 13:31:42 +00002544 {
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002545 /* First receiving is from the server */
2546 socket = ( i % 2 == 0 ) ? ( &server ) : ( &client );
2547
2548 recv_ret[i] = recv( socket, received[i] + read[i],
2549 MSGLEN - read[i] );
2550
2551 /* The result depends on whether any data was sent */
2552 if ( send_ret[i] > 0 )
2553 {
2554 TEST_ASSERT( recv_ret[i] > 0 );
2555 TEST_ASSERT( recv_ret[i] <= BUFLEN );
2556 read[i] += recv_ret[i];
2557 }
2558 else if( blocking )
2559 {
2560 TEST_ASSERT( recv_ret[i] == 0 );
2561 }
2562 else
2563 {
2564 TEST_ASSERT( recv_ret[i] == MBEDTLS_ERR_SSL_WANT_READ );
2565 recv_ret[i] = 0;
2566 }
2567
2568 /* If the buffer is empty we can test blocking and non-blocking
2569 * read */
2570 if ( recv_ret[i] == BUFLEN )
2571 {
2572 int blocking_ret = recv( socket, received[i], 1 );
2573 if ( blocking )
2574 {
2575 TEST_ASSERT( blocking_ret == 0 );
2576 }
2577 else
2578 {
2579 TEST_ASSERT( blocking_ret == MBEDTLS_ERR_SSL_WANT_READ );
2580 }
2581 }
Janos Follath3766ba52019-11-27 13:31:42 +00002582 }
Janos Follath031827f2019-11-27 11:12:14 +00002583
2584 progress = 0;
2585 for( i = 0; i < ROUNDS; i++ )
2586 {
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002587 progress += send_ret[i] + recv_ret[i];
Janos Follath031827f2019-11-27 11:12:14 +00002588 }
2589 }
2590
2591 for( i = 0; i < ROUNDS; i++ )
2592 TEST_ASSERT( memcmp( message[i], received[i], MSGLEN ) == 0 );
2593
2594exit:
2595
2596 mbedtls_mock_socket_close( &client );
2597 mbedtls_mock_socket_close( &server );
2598}
2599/* END_CASE */
2600
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002601/* BEGIN_CASE */
2602void ssl_message_queue_sanity( )
2603{
2604 mbedtls_test_message_queue queue;
2605
2606 /* Trying to push/pull to an empty queue */
2607 TEST_ASSERT( mbedtls_test_message_queue_push_info( NULL, 1 )
2608 == MBEDTLS_TEST_ERROR_ARG_NULL );
2609 TEST_ASSERT( mbedtls_test_message_queue_pop_info( NULL, 1 )
2610 == MBEDTLS_TEST_ERROR_ARG_NULL );
2611
Andrzej Kurek89bdc582020-03-09 06:29:43 -04002612 TEST_ASSERT( mbedtls_test_message_queue_setup( &queue, 3 ) == 0 );
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002613 TEST_ASSERT( queue.capacity == 3 );
2614 TEST_ASSERT( queue.num == 0 );
2615
2616exit:
2617 mbedtls_test_message_queue_free( &queue );
2618}
2619/* END_CASE */
2620
2621/* BEGIN_CASE */
2622void ssl_message_queue_basic( )
2623{
2624 mbedtls_test_message_queue queue;
2625
Andrzej Kurek89bdc582020-03-09 06:29:43 -04002626 TEST_ASSERT( mbedtls_test_message_queue_setup( &queue, 3 ) == 0 );
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002627
2628 /* Sanity test - 3 pushes and 3 pops with sufficient space */
2629 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 );
2630 TEST_ASSERT( queue.capacity == 3 );
2631 TEST_ASSERT( queue.num == 1 );
2632 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 );
2633 TEST_ASSERT( queue.capacity == 3 );
2634 TEST_ASSERT( queue.num == 2 );
2635 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 2 ) == 2 );
2636 TEST_ASSERT( queue.capacity == 3 );
2637 TEST_ASSERT( queue.num == 3 );
2638
2639 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 );
2640 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 );
2641 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 2 ) == 2 );
2642
2643exit:
2644 mbedtls_test_message_queue_free( &queue );
2645}
2646/* END_CASE */
2647
2648/* BEGIN_CASE */
2649void ssl_message_queue_overflow_underflow( )
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 /* 4 pushes (last one with an error), 4 pops (last one with an error) */
2656 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 );
2657 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 );
2658 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 2 ) == 2 );
2659 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 3 )
Andrzej Kurekf46b9122020-02-07 08:19:00 -05002660 == MBEDTLS_ERR_SSL_WANT_WRITE );
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002661
2662 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 );
2663 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 );
2664 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 2 ) == 2 );
2665
2666 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 )
Andrzej Kurekf46b9122020-02-07 08:19:00 -05002667 == MBEDTLS_ERR_SSL_WANT_READ );
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002668
2669exit:
2670 mbedtls_test_message_queue_free( &queue );
2671}
2672/* END_CASE */
2673
2674/* BEGIN_CASE */
2675void ssl_message_queue_interleaved( )
2676{
2677 mbedtls_test_message_queue queue;
2678
Andrzej Kurek89bdc582020-03-09 06:29:43 -04002679 TEST_ASSERT( mbedtls_test_message_queue_setup( &queue, 3 ) == 0 );
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002680
2681 /* Interleaved test - [2 pushes, 1 pop] twice, and then two pops
2682 * (to wrap around the buffer) */
2683 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 );
2684 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 );
2685
2686 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 );
2687
2688 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 2 ) == 2 );
2689 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 3 ) == 3 );
2690
2691 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 );
2692 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 2 ) == 2 );
2693
2694 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 5 ) == 5 );
2695 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 8 ) == 8 );
2696
2697 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 3 ) == 3 );
2698
2699 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 5 ) == 5 );
2700
2701 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 8 ) == 8 );
2702
2703exit:
2704 mbedtls_test_message_queue_free( &queue );
2705}
2706/* END_CASE */
2707
2708/* BEGIN_CASE */
2709void ssl_message_queue_insufficient_buffer( )
2710{
2711 mbedtls_test_message_queue queue;
2712 size_t message_len = 10;
2713 size_t buffer_len = 5;
2714
Andrzej Kurek89bdc582020-03-09 06:29:43 -04002715 TEST_ASSERT( mbedtls_test_message_queue_setup( &queue, 1 ) == 0 );
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002716
2717 /* Popping without a sufficient buffer */
2718 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, message_len )
2719 == (int) message_len );
2720 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, buffer_len )
2721 == (int) buffer_len );
2722exit:
2723 mbedtls_test_message_queue_free( &queue );
2724}
2725/* END_CASE */
2726
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002727/* BEGIN_CASE */
2728void ssl_message_mock_uninitialized( )
2729{
2730 enum { MSGLEN = 10 };
Shawn Carey03092f52021-05-13 10:38:32 -04002731 unsigned char message[MSGLEN] = {0}, received[MSGLEN];
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002732 mbedtls_mock_socket client, server;
2733 mbedtls_test_message_queue server_queue, client_queue;
2734 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05002735 mbedtls_message_socket_init( &server_context );
2736 mbedtls_message_socket_init( &client_context );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002737
2738 /* Send with a NULL context */
2739 TEST_ASSERT( mbedtls_mock_tcp_send_msg( NULL, message, MSGLEN )
2740 == MBEDTLS_TEST_ERROR_CONTEXT_ERROR );
2741
2742 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( NULL, message, MSGLEN )
2743 == MBEDTLS_TEST_ERROR_CONTEXT_ERROR );
2744
2745 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 1,
2746 &server,
2747 &server_context ) == 0 );
2748
2749 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 1,
2750 &client,
2751 &client_context ) == 0 );
2752
2753 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, MSGLEN )
2754 == MBEDTLS_TEST_ERROR_SEND_FAILED );
2755
2756 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
Andrzej Kurekf46b9122020-02-07 08:19:00 -05002757 == MBEDTLS_ERR_SSL_WANT_READ );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002758
2759 /* Push directly to a queue to later simulate a disconnected behavior */
2760 TEST_ASSERT( mbedtls_test_message_queue_push_info( &server_queue, MSGLEN )
2761 == MSGLEN );
2762
2763 /* Test if there's an error when trying to read from a disconnected
2764 * socket */
2765 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
2766 == MBEDTLS_TEST_ERROR_RECV_FAILED );
2767 exit:
2768 mbedtls_message_socket_close( &server_context );
2769 mbedtls_message_socket_close( &client_context );
2770}
2771/* END_CASE */
2772
2773/* BEGIN_CASE */
2774void ssl_message_mock_basic( )
2775{
2776 enum { MSGLEN = 10 };
2777 unsigned char message[MSGLEN], received[MSGLEN];
2778 mbedtls_mock_socket client, server;
2779 unsigned i;
2780 mbedtls_test_message_queue server_queue, client_queue;
2781 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05002782 mbedtls_message_socket_init( &server_context );
2783 mbedtls_message_socket_init( &client_context );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002784
2785 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 1,
2786 &server,
2787 &server_context ) == 0 );
2788
2789 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 1,
2790 &client,
2791 &client_context ) == 0 );
2792
2793 /* Fill up the buffer with structured data so that unwanted changes
2794 * can be detected */
2795 for( i = 0; i < MSGLEN; i++ )
2796 {
2797 message[i] = i & 0xFF;
2798 }
2799 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
2800 MSGLEN ) );
2801
2802 /* Send the message to the server */
2803 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2804 MSGLEN ) == MSGLEN );
2805
2806 /* Read from the server */
2807 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
2808 == MSGLEN );
2809
2810 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
2811 memset( received, 0, MSGLEN );
2812
2813 /* Send the message to the client */
2814 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &server_context, message,
2815 MSGLEN ) == MSGLEN );
2816
2817 /* Read from the client */
2818 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &client_context, received, MSGLEN )
2819 == MSGLEN );
2820 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
2821
2822 exit:
2823 mbedtls_message_socket_close( &server_context );
2824 mbedtls_message_socket_close( &client_context );
2825}
2826/* END_CASE */
2827
2828/* BEGIN_CASE */
2829void ssl_message_mock_queue_overflow_underflow( )
2830{
2831 enum { MSGLEN = 10 };
2832 unsigned char message[MSGLEN], received[MSGLEN];
2833 mbedtls_mock_socket client, server;
2834 unsigned i;
2835 mbedtls_test_message_queue server_queue, client_queue;
2836 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05002837 mbedtls_message_socket_init( &server_context );
2838 mbedtls_message_socket_init( &client_context );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002839
2840 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 2,
2841 &server,
2842 &server_context ) == 0 );
2843
2844 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 2,
2845 &client,
2846 &client_context ) == 0 );
2847
2848 /* Fill up the buffer with structured data so that unwanted changes
2849 * can be detected */
2850 for( i = 0; i < MSGLEN; i++ )
2851 {
2852 message[i] = i & 0xFF;
2853 }
2854 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
2855 MSGLEN*2 ) );
2856
2857 /* Send three message to the server, last one with an error */
2858 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2859 MSGLEN - 1 ) == MSGLEN - 1 );
2860
2861 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2862 MSGLEN ) == MSGLEN );
2863
2864 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2865 MSGLEN )
Andrzej Kurekf46b9122020-02-07 08:19:00 -05002866 == MBEDTLS_ERR_SSL_WANT_WRITE );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002867
2868 /* Read three messages from the server, last one with an error */
2869 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received,
2870 MSGLEN - 1 ) == MSGLEN - 1 );
2871
2872 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
2873 == MSGLEN );
2874
2875 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
2876
2877 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
Andrzej Kurekf46b9122020-02-07 08:19:00 -05002878 == MBEDTLS_ERR_SSL_WANT_READ );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002879
2880 exit:
2881 mbedtls_message_socket_close( &server_context );
2882 mbedtls_message_socket_close( &client_context );
2883}
2884/* END_CASE */
2885
2886/* BEGIN_CASE */
2887void ssl_message_mock_socket_overflow( )
2888{
2889 enum { MSGLEN = 10 };
2890 unsigned char message[MSGLEN], received[MSGLEN];
2891 mbedtls_mock_socket client, server;
2892 unsigned i;
2893 mbedtls_test_message_queue server_queue, client_queue;
2894 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05002895 mbedtls_message_socket_init( &server_context );
2896 mbedtls_message_socket_init( &client_context );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002897
2898 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 2,
2899 &server,
2900 &server_context ) == 0 );
2901
2902 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 2,
2903 &client,
2904 &client_context ) == 0 );
2905
2906 /* Fill up the buffer with structured data so that unwanted changes
2907 * can be detected */
2908 for( i = 0; i < MSGLEN; i++ )
2909 {
2910 message[i] = i & 0xFF;
2911 }
2912 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
2913 MSGLEN ) );
2914
2915 /* Send two message to the server, second one with an error */
2916 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2917 MSGLEN ) == MSGLEN );
2918
2919 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2920 MSGLEN )
2921 == MBEDTLS_TEST_ERROR_SEND_FAILED );
2922
2923 /* Read the only message from the server */
2924 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
2925 == MSGLEN );
2926
2927 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
2928
2929 exit:
2930 mbedtls_message_socket_close( &server_context );
2931 mbedtls_message_socket_close( &client_context );
2932}
2933/* END_CASE */
2934
2935/* BEGIN_CASE */
2936void ssl_message_mock_truncated( )
2937{
2938 enum { MSGLEN = 10 };
2939 unsigned char message[MSGLEN], received[MSGLEN];
2940 mbedtls_mock_socket client, server;
2941 unsigned i;
2942 mbedtls_test_message_queue server_queue, client_queue;
2943 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05002944 mbedtls_message_socket_init( &server_context );
2945 mbedtls_message_socket_init( &client_context );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002946
2947 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 2,
2948 &server,
2949 &server_context ) == 0 );
2950
2951 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 2,
2952 &client,
2953 &client_context ) == 0 );
2954
2955 memset( received, 0, MSGLEN );
2956 /* Fill up the buffer with structured data so that unwanted changes
2957 * can be detected */
2958 for( i = 0; i < MSGLEN; i++ )
2959 {
2960 message[i] = i & 0xFF;
2961 }
2962 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
2963 2 * MSGLEN ) );
2964
2965 /* Send two messages to the server, the second one small enough to fit in the
2966 * receiver's buffer. */
2967 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2968 MSGLEN ) == MSGLEN );
2969 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2970 MSGLEN / 2 ) == MSGLEN / 2 );
2971 /* Read a truncated message from the server */
2972 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN/2 )
2973 == MSGLEN/2 );
2974
2975 /* Test that the first half of the message is valid, and second one isn't */
2976 TEST_ASSERT( memcmp( message, received, MSGLEN/2 ) == 0 );
2977 TEST_ASSERT( memcmp( message + MSGLEN/2, received + MSGLEN/2, MSGLEN/2 )
2978 != 0 );
2979 memset( received, 0, MSGLEN );
2980
2981 /* Read a full message from the server */
2982 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN/2 )
2983 == MSGLEN / 2 );
2984
2985 /* Test that the first half of the message is valid */
2986 TEST_ASSERT( memcmp( message, received, MSGLEN/2 ) == 0 );
2987
2988 exit:
2989 mbedtls_message_socket_close( &server_context );
2990 mbedtls_message_socket_close( &client_context );
2991}
2992/* END_CASE */
2993
2994/* BEGIN_CASE */
2995void ssl_message_mock_socket_read_error( )
2996{
2997 enum { MSGLEN = 10 };
2998 unsigned char message[MSGLEN], received[MSGLEN];
2999 mbedtls_mock_socket client, server;
3000 unsigned i;
3001 mbedtls_test_message_queue server_queue, client_queue;
3002 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05003003 mbedtls_message_socket_init( &server_context );
3004 mbedtls_message_socket_init( &client_context );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003005
3006 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 1,
3007 &server,
3008 &server_context ) == 0 );
3009
3010 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 1,
3011 &client,
3012 &client_context ) == 0 );
3013
3014 /* Fill up the buffer with structured data so that unwanted changes
3015 * can be detected */
3016 for( i = 0; i < MSGLEN; i++ )
3017 {
3018 message[i] = i & 0xFF;
3019 }
3020 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
3021 MSGLEN ) );
3022
3023 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
3024 MSGLEN ) == MSGLEN );
3025
3026 /* Force a read error by disconnecting the socket by hand */
3027 server.status = 0;
3028 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
3029 == MBEDTLS_TEST_ERROR_RECV_FAILED );
3030 /* Return to a valid state */
3031 server.status = MBEDTLS_MOCK_SOCKET_CONNECTED;
3032
3033 memset( received, 0, sizeof( received ) );
3034
3035 /* Test that even though the server tried to read once disconnected, the
3036 * continuity is preserved */
3037 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
3038 == MSGLEN );
3039
3040 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
3041
3042 exit:
3043 mbedtls_message_socket_close( &server_context );
3044 mbedtls_message_socket_close( &client_context );
3045}
3046/* END_CASE */
3047
3048/* BEGIN_CASE */
3049void ssl_message_mock_interleaved_one_way( )
3050{
3051 enum { MSGLEN = 10 };
3052 unsigned char message[MSGLEN], received[MSGLEN];
3053 mbedtls_mock_socket client, server;
3054 unsigned i;
3055 mbedtls_test_message_queue server_queue, client_queue;
3056 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05003057 mbedtls_message_socket_init( &server_context );
3058 mbedtls_message_socket_init( &client_context );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003059
3060 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 3,
3061 &server,
3062 &server_context ) == 0 );
3063
3064 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 3,
3065 &client,
3066 &client_context ) == 0 );
3067
3068 /* Fill up the buffer with structured data so that unwanted changes
3069 * can be detected */
3070 for( i = 0; i < MSGLEN; i++ )
3071 {
3072 message[i] = i & 0xFF;
3073 }
3074 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
3075 MSGLEN*3 ) );
3076
3077 /* Interleaved test - [2 sends, 1 read] twice, and then two reads
3078 * (to wrap around the buffer) */
3079 for( i = 0; i < 2; i++ )
3080 {
3081 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
3082 MSGLEN ) == MSGLEN );
3083
3084 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
3085 MSGLEN ) == MSGLEN );
3086
3087 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received,
3088 MSGLEN ) == MSGLEN );
3089 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
3090 memset( received, 0, sizeof( received ) );
3091 }
3092
3093 for( i = 0; i < 2; i++ )
3094 {
3095 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received,
3096 MSGLEN ) == MSGLEN );
3097
3098 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
3099 }
3100 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
Andrzej Kurekf46b9122020-02-07 08:19:00 -05003101 == MBEDTLS_ERR_SSL_WANT_READ );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003102 exit:
3103 mbedtls_message_socket_close( &server_context );
3104 mbedtls_message_socket_close( &client_context );
3105}
3106/* END_CASE */
3107
3108/* BEGIN_CASE */
3109void ssl_message_mock_interleaved_two_ways( )
3110{
3111 enum { MSGLEN = 10 };
3112 unsigned char message[MSGLEN], received[MSGLEN];
3113 mbedtls_mock_socket client, server;
3114 unsigned i;
3115 mbedtls_test_message_queue server_queue, client_queue;
3116 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05003117 mbedtls_message_socket_init( &server_context );
3118 mbedtls_message_socket_init( &client_context );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003119
3120 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 3,
3121 &server,
3122 &server_context ) == 0 );
3123
3124 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 3,
3125 &client,
3126 &client_context ) == 0 );
3127
3128 /* Fill up the buffer with structured data so that unwanted changes
3129 * can be detected */
3130 for( i = 0; i < MSGLEN; i++ )
3131 {
3132 message[i] = i & 0xFF;
3133 }
3134 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
3135 MSGLEN*3 ) );
3136
3137 /* Interleaved test - [2 sends, 1 read] twice, both ways, and then two reads
3138 * (to wrap around the buffer) both ways. */
3139 for( i = 0; i < 2; i++ )
3140 {
3141 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
3142 MSGLEN ) == MSGLEN );
3143
3144 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
3145 MSGLEN ) == MSGLEN );
3146
3147 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &server_context, message,
3148 MSGLEN ) == MSGLEN );
3149
3150 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &server_context, message,
3151 MSGLEN ) == MSGLEN );
3152
3153 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received,
3154 MSGLEN ) == MSGLEN );
3155
3156 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
3157
3158 memset( received, 0, sizeof( received ) );
3159
3160 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &client_context, received,
3161 MSGLEN ) == MSGLEN );
3162
3163 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
3164
3165 memset( received, 0, sizeof( received ) );
3166 }
3167
3168 for( i = 0; i < 2; i++ )
3169 {
3170 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received,
3171 MSGLEN ) == MSGLEN );
3172
3173 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
3174 memset( received, 0, sizeof( received ) );
3175
3176 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &client_context, received,
3177 MSGLEN ) == MSGLEN );
3178
3179 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
3180 memset( received, 0, sizeof( received ) );
3181 }
3182
3183 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
Andrzej Kurekf46b9122020-02-07 08:19:00 -05003184 == MBEDTLS_ERR_SSL_WANT_READ );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003185
3186 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &client_context, received, MSGLEN )
Andrzej Kurekf46b9122020-02-07 08:19:00 -05003187 == MBEDTLS_ERR_SSL_WANT_READ );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003188 exit:
3189 mbedtls_message_socket_close( &server_context );
3190 mbedtls_message_socket_close( &client_context );
3191}
3192/* END_CASE */
3193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003194/* BEGIN_CASE depends_on:MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Azim Khan5fcca462018-06-29 11:05:32 +01003195void ssl_dtls_replay( data_t * prevs, data_t * new, int ret )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003196{
Azim Khand30ca132017-06-09 04:32:58 +01003197 uint32_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003198 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02003199 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003200
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02003201 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02003202 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02003203
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02003204 TEST_ASSERT( mbedtls_ssl_config_defaults( &conf,
3205 MBEDTLS_SSL_IS_CLIENT,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02003206 MBEDTLS_SSL_TRANSPORT_DATAGRAM,
3207 MBEDTLS_SSL_PRESET_DEFAULT ) == 0 );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02003208 TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003209
3210 /* Read previous record numbers */
Azim Khand30ca132017-06-09 04:32:58 +01003211 for( len = 0; len < prevs->len; len += 6 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003212 {
Azim Khand30ca132017-06-09 04:32:58 +01003213 memcpy( ssl.in_ctr + 2, prevs->x + len, 6 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003214 mbedtls_ssl_dtls_replay_update( &ssl );
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003215 }
3216
3217 /* Check new number */
Azim Khand30ca132017-06-09 04:32:58 +01003218 memcpy( ssl.in_ctr + 2, new->x, 6 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003219 TEST_ASSERT( mbedtls_ssl_dtls_replay_check( &ssl ) == ret );
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003221 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02003222 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003223}
3224/* END_CASE */
Hanno Beckerb25c0c72017-05-05 11:24:30 +01003225
3226/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
3227void ssl_set_hostname_twice( char *hostname0, char *hostname1 )
3228{
3229 mbedtls_ssl_context ssl;
3230 mbedtls_ssl_init( &ssl );
3231
3232 TEST_ASSERT( mbedtls_ssl_set_hostname( &ssl, hostname0 ) == 0 );
3233 TEST_ASSERT( mbedtls_ssl_set_hostname( &ssl, hostname1 ) == 0 );
3234
3235 mbedtls_ssl_free( &ssl );
3236}
Darryl Green11999bb2018-03-13 15:22:58 +00003237/* END_CASE */
Hanno Beckera18d1322018-01-03 14:27:32 +00003238
3239/* BEGIN_CASE */
3240void ssl_crypt_record( int cipher_type, int hash_id,
Hanno Beckerd856c822019-04-29 17:30:59 +01003241 int etm, int tag_mode, int ver,
3242 int cid0_len, int cid1_len )
Hanno Beckera18d1322018-01-03 14:27:32 +00003243{
3244 /*
3245 * Test several record encryptions and decryptions
3246 * with plenty of space before and after the data
3247 * within the record buffer.
3248 */
3249
3250 int ret;
3251 int num_records = 16;
3252 mbedtls_ssl_context ssl; /* ONLY for debugging */
3253
3254 mbedtls_ssl_transform t0, t1;
Hanno Becker81e16a32019-03-01 11:21:44 +00003255 unsigned char *buf = NULL;
Hanno Beckera18d1322018-01-03 14:27:32 +00003256 size_t const buflen = 512;
3257 mbedtls_record rec, rec_backup;
3258
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01003259 USE_PSA_INIT( );
3260
Hanno Beckera18d1322018-01-03 14:27:32 +00003261 mbedtls_ssl_init( &ssl );
3262 mbedtls_ssl_transform_init( &t0 );
3263 mbedtls_ssl_transform_init( &t1 );
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01003264 ret = build_transforms( &t0, &t1, cipher_type, hash_id,
3265 etm, tag_mode, ver,
3266 (size_t) cid0_len,
3267 (size_t) cid1_len );
3268
3269 TEST_ASSERT( ret == 0 );
Hanno Beckera18d1322018-01-03 14:27:32 +00003270
Hanno Becker3ee54212019-04-04 16:31:26 +01003271 TEST_ASSERT( ( buf = mbedtls_calloc( 1, buflen ) ) != NULL );
Hanno Beckera18d1322018-01-03 14:27:32 +00003272
3273 while( num_records-- > 0 )
3274 {
3275 mbedtls_ssl_transform *t_dec, *t_enc;
3276 /* Take turns in who's sending and who's receiving. */
3277 if( num_records % 3 == 0 )
3278 {
3279 t_dec = &t0;
3280 t_enc = &t1;
3281 }
3282 else
3283 {
3284 t_dec = &t1;
3285 t_enc = &t0;
3286 }
3287
3288 /*
3289 * The record header affects the transformation in two ways:
3290 * 1) It determines the AEAD additional data
3291 * 2) The record counter sometimes determines the IV.
3292 *
3293 * Apart from that, the fields don't have influence.
3294 * In particular, it is currently not the responsibility
3295 * of ssl_encrypt/decrypt_buf to check if the transform
3296 * version matches the record version, or that the
3297 * type is sensible.
3298 */
3299
3300 memset( rec.ctr, num_records, sizeof( rec.ctr ) );
3301 rec.type = 42;
3302 rec.ver[0] = num_records;
3303 rec.ver[1] = num_records;
Hanno Beckera0e20d02019-05-15 14:03:01 +01003304#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd856c822019-04-29 17:30:59 +01003305 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01003306#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera18d1322018-01-03 14:27:32 +00003307
3308 rec.buf = buf;
3309 rec.buf_len = buflen;
3310 rec.data_offset = 16;
3311 /* Make sure to vary the length to exercise different
3312 * paddings. */
3313 rec.data_len = 1 + num_records;
3314
3315 memset( rec.buf + rec.data_offset, 42, rec.data_len );
3316
3317 /* Make a copy for later comparison */
3318 rec_backup = rec;
3319
3320 /* Encrypt record */
3321 ret = mbedtls_ssl_encrypt_buf( &ssl, t_enc, &rec,
Ronald Cron351f0ee2020-06-10 12:12:18 +02003322 mbedtls_test_rnd_std_rand, NULL );
Hanno Beckera18d1322018-01-03 14:27:32 +00003323 TEST_ASSERT( ret == 0 || ret == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3324 if( ret != 0 )
3325 {
3326 continue;
3327 }
3328
Hanno Beckerb2713ab2020-05-07 14:54:22 +01003329#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3330 if( rec.cid_len != 0 )
3331 {
3332 /* DTLS 1.2 + CID hides the real content type and
3333 * uses a special CID content type in the protected
3334 * record. Double-check this. */
3335 TEST_ASSERT( rec.type == MBEDTLS_SSL_MSG_CID );
3336 }
3337#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3338
Ronald Cron6f135e12021-12-08 16:57:54 +01003339#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Beckerb2713ab2020-05-07 14:54:22 +01003340 if( t_enc->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
3341 {
3342 /* TLS 1.3 hides the real content type and
3343 * always uses Application Data as the content type
3344 * for protected records. Double-check this. */
3345 TEST_ASSERT( rec.type == MBEDTLS_SSL_MSG_APPLICATION_DATA );
3346 }
Ronald Cron6f135e12021-12-08 16:57:54 +01003347#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb2713ab2020-05-07 14:54:22 +01003348
Hanno Beckera18d1322018-01-03 14:27:32 +00003349 /* Decrypt record with t_dec */
Hanno Beckerd856c822019-04-29 17:30:59 +01003350 ret = mbedtls_ssl_decrypt_buf( &ssl, t_dec, &rec );
3351 TEST_ASSERT( ret == 0 );
Hanno Beckera18d1322018-01-03 14:27:32 +00003352
3353 /* Compare results */
3354 TEST_ASSERT( rec.type == rec_backup.type );
3355 TEST_ASSERT( memcmp( rec.ctr, rec_backup.ctr, 8 ) == 0 );
3356 TEST_ASSERT( rec.ver[0] == rec_backup.ver[0] );
3357 TEST_ASSERT( rec.ver[1] == rec_backup.ver[1] );
3358 TEST_ASSERT( rec.data_len == rec_backup.data_len );
3359 TEST_ASSERT( rec.data_offset == rec_backup.data_offset );
3360 TEST_ASSERT( memcmp( rec.buf + rec.data_offset,
3361 rec_backup.buf + rec_backup.data_offset,
3362 rec.data_len ) == 0 );
3363 }
3364
Hanno Becker81e16a32019-03-01 11:21:44 +00003365exit:
3366
Hanno Beckera18d1322018-01-03 14:27:32 +00003367 /* Cleanup */
3368 mbedtls_ssl_free( &ssl );
3369 mbedtls_ssl_transform_free( &t0 );
3370 mbedtls_ssl_transform_free( &t1 );
3371
Hanno Becker3ee54212019-04-04 16:31:26 +01003372 mbedtls_free( buf );
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01003373 USE_PSA_DONE( );
Hanno Beckera18d1322018-01-03 14:27:32 +00003374}
3375/* END_CASE */
Hanno Beckerb3268da2018-01-05 15:20:24 +00003376
3377/* BEGIN_CASE */
3378void ssl_crypt_record_small( int cipher_type, int hash_id,
Hanno Beckerd856c822019-04-29 17:30:59 +01003379 int etm, int tag_mode, int ver,
3380 int cid0_len, int cid1_len )
Hanno Beckerb3268da2018-01-05 15:20:24 +00003381{
3382 /*
3383 * Test pairs of encryption and decryption with an increasing
3384 * amount of space in the record buffer - in more detail:
3385 * 1) Try to encrypt with 0, 1, 2, ... bytes available
3386 * in front of the plaintext, and expect the encryption
3387 * to succeed starting from some offset. Always keep
3388 * enough space in the end of the buffer.
3389 * 2) Try to encrypt with 0, 1, 2, ... bytes available
3390 * at the end of the plaintext, and expect the encryption
3391 * to succeed starting from some offset. Always keep
3392 * enough space at the beginning of the buffer.
3393 * 3) Try to encrypt with 0, 1, 2, ... bytes available
3394 * both at the front and end of the plaintext,
3395 * and expect the encryption to succeed starting from
3396 * some offset.
3397 *
3398 * If encryption succeeds, check that decryption succeeds
3399 * and yields the original record.
3400 */
3401
3402 mbedtls_ssl_context ssl; /* ONLY for debugging */
3403
3404 mbedtls_ssl_transform t0, t1;
Hanno Becker81e16a32019-03-01 11:21:44 +00003405 unsigned char *buf = NULL;
Hanno Beckerd856c822019-04-29 17:30:59 +01003406 size_t const buflen = 256;
Hanno Beckerb3268da2018-01-05 15:20:24 +00003407 mbedtls_record rec, rec_backup;
3408
3409 int ret;
Hanno Beckerd856c822019-04-29 17:30:59 +01003410 int mode; /* Mode 1, 2 or 3 as explained above */
3411 size_t offset; /* Available space at beginning/end/both */
3412 size_t threshold = 96; /* Maximum offset to test against */
Hanno Beckerb3268da2018-01-05 15:20:24 +00003413
Hanno Beckerd856c822019-04-29 17:30:59 +01003414 size_t default_pre_padding = 64; /* Pre-padding to use in mode 2 */
3415 size_t default_post_padding = 128; /* Post-padding to use in mode 1 */
Hanno Beckerb3268da2018-01-05 15:20:24 +00003416
3417 int seen_success; /* Indicates if in the current mode we've
3418 * already seen a successful test. */
3419
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01003420 USE_PSA_INIT( );
3421
Hanno Beckerb3268da2018-01-05 15:20:24 +00003422 mbedtls_ssl_init( &ssl );
3423 mbedtls_ssl_transform_init( &t0 );
3424 mbedtls_ssl_transform_init( &t1 );
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01003425 ret = build_transforms( &t0, &t1, cipher_type, hash_id,
Hanno Beckerd856c822019-04-29 17:30:59 +01003426 etm, tag_mode, ver,
3427 (size_t) cid0_len,
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01003428 (size_t) cid1_len );
3429
3430 TEST_ASSERT( ret == 0 );
Hanno Beckerb3268da2018-01-05 15:20:24 +00003431
Hanno Becker3ee54212019-04-04 16:31:26 +01003432 TEST_ASSERT( ( buf = mbedtls_calloc( 1, buflen ) ) != NULL );
Hanno Beckerb3268da2018-01-05 15:20:24 +00003433
3434 for( mode=1; mode <= 3; mode++ )
3435 {
3436 seen_success = 0;
3437 for( offset=0; offset <= threshold; offset++ )
3438 {
3439 mbedtls_ssl_transform *t_dec, *t_enc;
Hanno Becker6c87b3f2019-04-29 17:24:44 +01003440 t_dec = &t0;
3441 t_enc = &t1;
Hanno Beckerb3268da2018-01-05 15:20:24 +00003442
3443 memset( rec.ctr, offset, sizeof( rec.ctr ) );
3444 rec.type = 42;
3445 rec.ver[0] = offset;
3446 rec.ver[1] = offset;
3447 rec.buf = buf;
3448 rec.buf_len = buflen;
Hanno Beckera0e20d02019-05-15 14:03:01 +01003449#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd856c822019-04-29 17:30:59 +01003450 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01003451#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerb3268da2018-01-05 15:20:24 +00003452
3453 switch( mode )
3454 {
3455 case 1: /* Space in the beginning */
3456 rec.data_offset = offset;
3457 rec.data_len = buflen - offset - default_post_padding;
3458 break;
3459
3460 case 2: /* Space in the end */
3461 rec.data_offset = default_pre_padding;
3462 rec.data_len = buflen - default_pre_padding - offset;
3463 break;
3464
3465 case 3: /* Space in the beginning and end */
3466 rec.data_offset = offset;
3467 rec.data_len = buflen - 2 * offset;
3468 break;
3469
3470 default:
3471 TEST_ASSERT( 0 );
3472 break;
3473 }
3474
3475 memset( rec.buf + rec.data_offset, 42, rec.data_len );
3476
3477 /* Make a copy for later comparison */
3478 rec_backup = rec;
3479
3480 /* Encrypt record */
Ronald Cron6c5bd7f2020-06-10 14:08:26 +02003481 ret = mbedtls_ssl_encrypt_buf( &ssl, t_enc, &rec,
3482 mbedtls_test_rnd_std_rand, NULL );
Hanno Beckerb3268da2018-01-05 15:20:24 +00003483
3484 if( ( mode == 1 || mode == 2 ) && seen_success )
3485 {
3486 TEST_ASSERT( ret == 0 );
3487 }
3488 else
3489 {
3490 TEST_ASSERT( ret == 0 || ret == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3491 if( ret == 0 )
3492 seen_success = 1;
3493 }
3494
3495 if( ret != 0 )
3496 continue;
3497
Hanno Beckerb2713ab2020-05-07 14:54:22 +01003498#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3499 if( rec.cid_len != 0 )
3500 {
3501 /* DTLS 1.2 + CID hides the real content type and
3502 * uses a special CID content type in the protected
3503 * record. Double-check this. */
3504 TEST_ASSERT( rec.type == MBEDTLS_SSL_MSG_CID );
3505 }
3506#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3507
Ronald Cron6f135e12021-12-08 16:57:54 +01003508#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Beckerb2713ab2020-05-07 14:54:22 +01003509 if( t_enc->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
3510 {
3511 /* TLS 1.3 hides the real content type and
3512 * always uses Application Data as the content type
3513 * for protected records. Double-check this. */
3514 TEST_ASSERT( rec.type == MBEDTLS_SSL_MSG_APPLICATION_DATA );
3515 }
Ronald Cron6f135e12021-12-08 16:57:54 +01003516#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb2713ab2020-05-07 14:54:22 +01003517
Hanno Beckerb3268da2018-01-05 15:20:24 +00003518 /* Decrypt record with t_dec */
3519 TEST_ASSERT( mbedtls_ssl_decrypt_buf( &ssl, t_dec, &rec ) == 0 );
3520
3521 /* Compare results */
3522 TEST_ASSERT( rec.type == rec_backup.type );
3523 TEST_ASSERT( memcmp( rec.ctr, rec_backup.ctr, 8 ) == 0 );
3524 TEST_ASSERT( rec.ver[0] == rec_backup.ver[0] );
3525 TEST_ASSERT( rec.ver[1] == rec_backup.ver[1] );
3526 TEST_ASSERT( rec.data_len == rec_backup.data_len );
3527 TEST_ASSERT( rec.data_offset == rec_backup.data_offset );
3528 TEST_ASSERT( memcmp( rec.buf + rec.data_offset,
3529 rec_backup.buf + rec_backup.data_offset,
3530 rec.data_len ) == 0 );
3531 }
3532
3533 TEST_ASSERT( seen_success == 1 );
3534 }
3535
Hanno Becker81e16a32019-03-01 11:21:44 +00003536exit:
3537
Hanno Beckerb3268da2018-01-05 15:20:24 +00003538 /* Cleanup */
3539 mbedtls_ssl_free( &ssl );
3540 mbedtls_ssl_transform_free( &t0 );
3541 mbedtls_ssl_transform_free( &t1 );
3542
Hanno Becker3ee54212019-04-04 16:31:26 +01003543 mbedtls_free( buf );
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01003544 USE_PSA_DONE( );
Hanno Beckerb3268da2018-01-05 15:20:24 +00003545}
3546/* END_CASE */
Ron Eldor824ad7b2019-05-13 14:09:00 +03003547
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01003548/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC:MBEDTLS_AES_C:MBEDTLS_SSL_PROTO_TLS1_2:!MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003549void ssl_decrypt_non_etm_cbc( int cipher_type, int hash_id, int trunc_hmac,
Manuel Pégourié-Gonnard864abbf2020-07-21 10:37:14 +02003550 int length_selector )
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003551{
3552 /*
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003553 * Test record decryption for CBC without EtM, focused on the verification
3554 * of padding and MAC.
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003555 *
TRodziewicz299510e2021-07-09 16:55:11 +02003556 * Actually depends on TLS 1.2 and either AES, ARIA or Camellia, but since
3557 * the test framework doesn't support alternation in dependency statements,
3558 * just depend on AES.
Manuel Pégourié-Gonnard864abbf2020-07-21 10:37:14 +02003559 *
3560 * The length_selector argument is interpreted as follows:
3561 * - if it's -1, the plaintext length is 0 and minimal padding is applied
3562 * - if it's -2, the plaintext length is 0 and maximal padding is applied
3563 * - otherwise it must be in [0, 255] and is padding_length from RFC 5246:
3564 * it's the length of the rest of the padding, that is, excluding the
3565 * byte that encodes the length. The minimal non-zero plaintext length
3566 * that gives this padding_length is automatically selected.
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003567 */
3568 mbedtls_ssl_context ssl; /* ONLY for debugging */
3569 mbedtls_ssl_transform t0, t1;
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003570 mbedtls_record rec, rec_save;
3571 unsigned char *buf = NULL, *buf_save = NULL;
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003572 size_t buflen, olen = 0;
Manuel Pégourié-Gonnard864abbf2020-07-21 10:37:14 +02003573 size_t plaintext_len, block_size, i;
Manuel Pégourié-Gonnarde55653f2020-07-22 11:42:57 +02003574 unsigned char padlen; /* excluding the padding_length byte */
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003575 unsigned char add_data[13];
3576 unsigned char mac[MBEDTLS_MD_MAX_SIZE];
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003577 int exp_ret;
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01003578 int ret;
Manuel Pégourié-Gonnard4adc04a2020-07-16 10:00:48 +02003579 const unsigned char pad_max_len = 255; /* Per the standard */
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003580
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01003581 USE_PSA_INIT( );
3582
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003583 mbedtls_ssl_init( &ssl );
3584 mbedtls_ssl_transform_init( &t0 );
3585 mbedtls_ssl_transform_init( &t1 );
3586
3587 /* Set up transforms with dummy keys */
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01003588 ret = build_transforms( &t0, &t1, cipher_type, hash_id,
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003589 0, trunc_hmac,
3590 MBEDTLS_SSL_MINOR_VERSION_3,
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01003591 0 , 0 );
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003592
Manuel Pégourié-Gonnard864abbf2020-07-21 10:37:14 +02003593 /* Determine padding/plaintext length */
3594 TEST_ASSERT( length_selector >= -2 && length_selector <= 255 );
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003595 block_size = t0.ivlen;
Manuel Pégourié-Gonnard864abbf2020-07-21 10:37:14 +02003596 if( length_selector < 0 )
3597 {
3598 plaintext_len = 0;
3599
Manuel Pégourié-Gonnarde55653f2020-07-22 11:42:57 +02003600 /* Minimal padding
3601 * The +1 is for the padding_length byte, not counted in padlen. */
Manuel Pégourié-Gonnard864abbf2020-07-21 10:37:14 +02003602 padlen = block_size - ( t0.maclen + 1 ) % block_size;
3603
3604 /* Maximal padding? */
3605 if( length_selector == -2 )
3606 padlen += block_size * ( ( pad_max_len - padlen ) / block_size );
3607 }
3608 else
3609 {
3610 padlen = length_selector;
3611
Manuel Pégourié-Gonnarde55653f2020-07-22 11:42:57 +02003612 /* Minimal non-zero plaintext_length giving desired padding.
3613 * The +1 is for the padding_length byte, not counted in padlen. */
Manuel Pégourié-Gonnard864abbf2020-07-21 10:37:14 +02003614 plaintext_len = block_size - ( padlen + t0.maclen + 1 ) % block_size;
3615 }
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003616
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003617 /* Prepare a buffer for record data */
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003618 buflen = block_size
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003619 + plaintext_len
3620 + t0.maclen
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003621 + padlen + 1;
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003622 ASSERT_ALLOC( buf, buflen );
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003623 ASSERT_ALLOC( buf_save, buflen );
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003624
3625 /* Prepare a dummy record header */
3626 memset( rec.ctr, 0, sizeof( rec.ctr ) );
3627 rec.type = MBEDTLS_SSL_MSG_APPLICATION_DATA;
3628 rec.ver[0] = MBEDTLS_SSL_MAJOR_VERSION_3;
3629 rec.ver[1] = MBEDTLS_SSL_MINOR_VERSION_3;
3630#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3631 rec.cid_len = 0;
3632#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3633
3634 /* Prepare dummy record content */
3635 rec.buf = buf;
3636 rec.buf_len = buflen;
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003637 rec.data_offset = block_size;
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003638 rec.data_len = plaintext_len;
3639 memset( rec.buf + rec.data_offset, 42, rec.data_len );
3640
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003641 /* Serialized version of record header for MAC purposes */
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003642 memcpy( add_data, rec.ctr, 8 );
3643 add_data[8] = rec.type;
3644 add_data[9] = rec.ver[0];
3645 add_data[10] = rec.ver[1];
3646 add_data[11] = ( rec.data_len >> 8 ) & 0xff;
3647 add_data[12] = ( rec.data_len >> 0 ) & 0xff;
3648
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003649 /* Set dummy IV */
3650 memset( t0.iv_enc, 0x55, t0.ivlen );
3651 memcpy( rec.buf, t0.iv_enc, t0.ivlen );
3652
3653 /*
3654 * Prepare a pre-encryption record (with MAC and padding), and save it.
3655 */
3656
3657 /* MAC with additional data */
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003658 TEST_EQUAL( 0, mbedtls_md_hmac_update( &t0.md_ctx_enc, add_data, 13 ) );
3659 TEST_EQUAL( 0, mbedtls_md_hmac_update( &t0.md_ctx_enc,
3660 rec.buf + rec.data_offset,
3661 rec.data_len ) );
3662 TEST_EQUAL( 0, mbedtls_md_hmac_finish( &t0.md_ctx_enc, mac ) );
3663
3664 memcpy( rec.buf + rec.data_offset + rec.data_len, mac, t0.maclen );
3665 rec.data_len += t0.maclen;
3666
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003667 /* Pad */
3668 memset( rec.buf + rec.data_offset + rec.data_len, padlen, padlen + 1 );
3669 rec.data_len += padlen + 1;
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003670
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003671 /* Save correct pre-encryption record */
3672 rec_save = rec;
3673 rec_save.buf = buf_save;
3674 memcpy( buf_save, buf, buflen );
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003675
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003676 /*
3677 * Encrypt and decrypt the correct record, expecting success
3678 */
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003679 TEST_EQUAL( 0, mbedtls_cipher_crypt( &t0.cipher_ctx_enc,
3680 t0.iv_enc, t0.ivlen,
3681 rec.buf + rec.data_offset, rec.data_len,
3682 rec.buf + rec.data_offset, &olen ) );
3683 rec.data_offset -= t0.ivlen;
3684 rec.data_len += t0.ivlen;
3685
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003686 TEST_EQUAL( 0, mbedtls_ssl_decrypt_buf( &ssl, &t1, &rec ) );
3687
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003688 /*
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003689 * Modify each byte of the pre-encryption record before encrypting and
3690 * decrypting it, expecting failure every time.
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003691 */
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003692 for( i = block_size; i < buflen; i++ )
3693 {
Chris Jones9634bb12021-01-20 15:56:42 +00003694 mbedtls_test_set_step( i );
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003695
3696 /* Restore correct pre-encryption record */
3697 rec = rec_save;
3698 rec.buf = buf;
3699 memcpy( buf, buf_save, buflen );
3700
Manuel Pégourié-Gonnardb51f0442020-07-21 10:40:25 +02003701 /* Corrupt one byte of the data (could be plaintext, MAC or padding) */
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003702 rec.buf[i] ^= 0x01;
3703
3704 /* Encrypt */
3705 TEST_EQUAL( 0, mbedtls_cipher_crypt( &t0.cipher_ctx_enc,
3706 t0.iv_enc, t0.ivlen,
3707 rec.buf + rec.data_offset, rec.data_len,
3708 rec.buf + rec.data_offset, &olen ) );
3709 rec.data_offset -= t0.ivlen;
3710 rec.data_len += t0.ivlen;
3711
3712 /* Decrypt and expect failure */
3713 TEST_EQUAL( MBEDTLS_ERR_SSL_INVALID_MAC,
3714 mbedtls_ssl_decrypt_buf( &ssl, &t1, &rec ) );
3715 }
3716
3717 /*
3718 * Use larger values of the padding bytes - with small buffers, this tests
3719 * the case where the announced padlen would be larger than the buffer
3720 * (and before that, than the buffer minus the size of the MAC), to make
3721 * sure our padding checking code does not perform any out-of-bounds reads
3722 * in this case. (With larger buffers, ie when the plaintext is long or
3723 * maximal length padding is used, this is less relevant but still doesn't
3724 * hurt to test.)
3725 *
3726 * (Start the loop with correct padding, just to double-check that record
3727 * saving did work, and that we're overwriting the correct bytes.)
3728 */
Manuel Pégourié-Gonnard4adc04a2020-07-16 10:00:48 +02003729 for( i = padlen; i <= pad_max_len; i++ )
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003730 {
Chris Jones9634bb12021-01-20 15:56:42 +00003731 mbedtls_test_set_step( i );
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003732
3733 /* Restore correct pre-encryption record */
3734 rec = rec_save;
3735 rec.buf = buf;
3736 memcpy( buf, buf_save, buflen );
3737
3738 /* Set padding bytes to new value */
3739 memset( buf + buflen - padlen - 1, i, padlen + 1 );
3740
3741 /* Encrypt */
3742 TEST_EQUAL( 0, mbedtls_cipher_crypt( &t0.cipher_ctx_enc,
3743 t0.iv_enc, t0.ivlen,
3744 rec.buf + rec.data_offset, rec.data_len,
3745 rec.buf + rec.data_offset, &olen ) );
3746 rec.data_offset -= t0.ivlen;
3747 rec.data_len += t0.ivlen;
3748
3749 /* Decrypt and expect failure except the first time */
3750 exp_ret = ( i == padlen ) ? 0 : MBEDTLS_ERR_SSL_INVALID_MAC;
3751 TEST_EQUAL( exp_ret, mbedtls_ssl_decrypt_buf( &ssl, &t1, &rec ) );
3752 }
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003753
3754exit:
3755 mbedtls_ssl_free( &ssl );
3756 mbedtls_ssl_transform_free( &t0 );
3757 mbedtls_ssl_transform_free( &t1 );
3758 mbedtls_free( buf );
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003759 mbedtls_free( buf_save );
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01003760 USE_PSA_DONE( );
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003761}
3762/* END_CASE */
3763
Ronald Cron6f135e12021-12-08 16:57:54 +01003764/* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00003765void ssl_tls13_hkdf_expand_label( int hash_alg,
3766 data_t *secret,
3767 int label_idx,
3768 data_t *ctx,
3769 int desired_length,
3770 data_t *expected )
Hanno Becker39ff4922020-08-21 13:36:56 +01003771{
3772 unsigned char dst[ 100 ];
3773
Hanno Becker70d7fb02020-09-09 10:11:21 +01003774 unsigned char const *lbl = NULL;
3775 size_t lbl_len;
Xiaofei Baid25fab62021-12-02 06:36:27 +00003776#define MBEDTLS_SSL_TLS1_3_LABEL( name, string ) \
3777 if( label_idx == (int) tls13_label_ ## name ) \
3778 { \
Xiaofei Bai746f9482021-11-12 08:53:56 +00003779 lbl = mbedtls_ssl_tls13_labels.name; \
3780 lbl_len = sizeof( mbedtls_ssl_tls13_labels.name ); \
Hanno Becker70d7fb02020-09-09 10:11:21 +01003781 }
3782MBEDTLS_SSL_TLS1_3_LABEL_LIST
3783#undef MBEDTLS_SSL_TLS1_3_LABEL
3784 TEST_ASSERT( lbl != NULL );
Hanno Becker39ff4922020-08-21 13:36:56 +01003785
3786 /* Check sanity of test parameters. */
3787 TEST_ASSERT( (size_t) desired_length <= sizeof(dst) );
3788 TEST_ASSERT( (size_t) desired_length == expected->len );
3789
Xiaofei Bai746f9482021-11-12 08:53:56 +00003790 TEST_ASSERT( mbedtls_ssl_tls13_hkdf_expand_label(
Hanno Becker39ff4922020-08-21 13:36:56 +01003791 (mbedtls_md_type_t) hash_alg,
3792 secret->x, secret->len,
Hanno Becker70d7fb02020-09-09 10:11:21 +01003793 lbl, lbl_len,
Hanno Becker39ff4922020-08-21 13:36:56 +01003794 ctx->x, ctx->len,
3795 dst, desired_length ) == 0 );
3796
Hanno Beckerfb080962020-09-08 10:58:42 +01003797 ASSERT_COMPARE( dst, (size_t) desired_length,
3798 expected->x, (size_t) expected->len );
Hanno Becker39ff4922020-08-21 13:36:56 +01003799}
3800/* END_CASE */
3801
Ronald Cron6f135e12021-12-08 16:57:54 +01003802/* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00003803void ssl_tls13_traffic_key_generation( int hash_alg,
3804 data_t *server_secret,
3805 data_t *client_secret,
3806 int desired_iv_len,
3807 int desired_key_len,
3808 data_t *expected_server_write_key,
3809 data_t *expected_server_write_iv,
3810 data_t *expected_client_write_key,
3811 data_t *expected_client_write_iv )
Hanno Becker19498f82020-08-21 13:37:08 +01003812{
3813 mbedtls_ssl_key_set keys;
3814
3815 /* Check sanity of test parameters. */
3816 TEST_ASSERT( client_secret->len == server_secret->len );
3817 TEST_ASSERT( expected_client_write_iv->len == expected_server_write_iv->len &&
3818 expected_client_write_iv->len == (size_t) desired_iv_len );
3819 TEST_ASSERT( expected_client_write_key->len == expected_server_write_key->len &&
3820 expected_client_write_key->len == (size_t) desired_key_len );
3821
Xiaofei Bai746f9482021-11-12 08:53:56 +00003822 TEST_ASSERT( mbedtls_ssl_tls13_make_traffic_keys(
Hanno Becker19498f82020-08-21 13:37:08 +01003823 (mbedtls_md_type_t) hash_alg,
3824 client_secret->x,
3825 server_secret->x,
3826 client_secret->len /* == server_secret->len */,
3827 desired_key_len, desired_iv_len,
3828 &keys ) == 0 );
3829
Hanno Beckerfb080962020-09-08 10:58:42 +01003830 ASSERT_COMPARE( keys.client_write_key,
Hanno Becker493ea7f2020-09-08 11:01:00 +01003831 keys.key_len,
Hanno Beckerfb080962020-09-08 10:58:42 +01003832 expected_client_write_key->x,
3833 (size_t) desired_key_len );
3834 ASSERT_COMPARE( keys.server_write_key,
Hanno Becker493ea7f2020-09-08 11:01:00 +01003835 keys.key_len,
Hanno Beckerfb080962020-09-08 10:58:42 +01003836 expected_server_write_key->x,
3837 (size_t) desired_key_len );
3838 ASSERT_COMPARE( keys.client_write_iv,
Hanno Becker493ea7f2020-09-08 11:01:00 +01003839 keys.iv_len,
Hanno Beckerfb080962020-09-08 10:58:42 +01003840 expected_client_write_iv->x,
3841 (size_t) desired_iv_len );
3842 ASSERT_COMPARE( keys.server_write_iv,
Hanno Becker493ea7f2020-09-08 11:01:00 +01003843 keys.iv_len,
Hanno Beckerfb080962020-09-08 10:58:42 +01003844 expected_server_write_iv->x,
3845 (size_t) desired_iv_len );
Hanno Becker19498f82020-08-21 13:37:08 +01003846}
3847/* END_CASE */
3848
Ronald Cron6f135e12021-12-08 16:57:54 +01003849/* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00003850void ssl_tls13_derive_secret( int hash_alg,
3851 data_t *secret,
3852 int label_idx,
3853 data_t *ctx,
3854 int desired_length,
3855 int already_hashed,
3856 data_t *expected )
Hanno Beckere4849d12020-08-21 14:14:14 +01003857{
3858 unsigned char dst[ 100 ];
3859
Hanno Becker70d7fb02020-09-09 10:11:21 +01003860 unsigned char const *lbl = NULL;
3861 size_t lbl_len;
Xiaofei Baid25fab62021-12-02 06:36:27 +00003862#define MBEDTLS_SSL_TLS1_3_LABEL( name, string ) \
3863 if( label_idx == (int) tls13_label_ ## name ) \
3864 { \
Xiaofei Bai746f9482021-11-12 08:53:56 +00003865 lbl = mbedtls_ssl_tls13_labels.name; \
3866 lbl_len = sizeof( mbedtls_ssl_tls13_labels.name ); \
Hanno Becker70d7fb02020-09-09 10:11:21 +01003867 }
3868MBEDTLS_SSL_TLS1_3_LABEL_LIST
3869#undef MBEDTLS_SSL_TLS1_3_LABEL
3870 TEST_ASSERT( lbl != NULL );
3871
Hanno Beckere4849d12020-08-21 14:14:14 +01003872 /* Check sanity of test parameters. */
3873 TEST_ASSERT( (size_t) desired_length <= sizeof(dst) );
3874 TEST_ASSERT( (size_t) desired_length == expected->len );
3875
Xiaofei Bai746f9482021-11-12 08:53:56 +00003876 TEST_ASSERT( mbedtls_ssl_tls13_derive_secret(
Hanno Beckere4849d12020-08-21 14:14:14 +01003877 (mbedtls_md_type_t) hash_alg,
3878 secret->x, secret->len,
Hanno Becker70d7fb02020-09-09 10:11:21 +01003879 lbl, lbl_len,
Hanno Beckere4849d12020-08-21 14:14:14 +01003880 ctx->x, ctx->len,
3881 already_hashed,
3882 dst, desired_length ) == 0 );
3883
Hanno Beckerfb080962020-09-08 10:58:42 +01003884 ASSERT_COMPARE( dst, desired_length,
3885 expected->x, desired_length );
Hanno Beckere4849d12020-08-21 14:14:14 +01003886}
3887/* END_CASE */
3888
Ronald Cron6f135e12021-12-08 16:57:54 +01003889/* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00003890void ssl_tls13_derive_early_secrets( int hash_alg,
3891 data_t *secret,
3892 data_t *transcript,
3893 data_t *traffic_expected,
3894 data_t *exporter_expected )
Hanno Beckera4f40a02021-05-24 06:42:11 +01003895{
Xiaofei Bai746f9482021-11-12 08:53:56 +00003896 mbedtls_ssl_tls13_early_secrets secrets;
Hanno Beckera4f40a02021-05-24 06:42:11 +01003897
3898 /* Double-check that we've passed sane parameters. */
3899 mbedtls_md_type_t md_type = (mbedtls_md_type_t) hash_alg;
3900 mbedtls_md_info_t const * const md_info = mbedtls_md_info_from_type( md_type );
3901 size_t const md_size = mbedtls_md_get_size( md_info );
3902 TEST_ASSERT( md_info != 0 &&
3903 secret->len == md_size &&
3904 transcript->len == md_size &&
3905 traffic_expected->len == md_size &&
3906 exporter_expected->len == md_size );
3907
Xiaofei Bai746f9482021-11-12 08:53:56 +00003908 TEST_ASSERT( mbedtls_ssl_tls13_derive_early_secrets(
Hanno Beckera4f40a02021-05-24 06:42:11 +01003909 md_type, secret->x, transcript->x, transcript->len,
3910 &secrets ) == 0 );
3911
3912 ASSERT_COMPARE( secrets.client_early_traffic_secret, md_size,
3913 traffic_expected->x, traffic_expected->len );
3914 ASSERT_COMPARE( secrets.early_exporter_master_secret, md_size,
3915 exporter_expected->x, exporter_expected->len );
3916}
3917/* END_CASE */
3918
Ronald Cron6f135e12021-12-08 16:57:54 +01003919/* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00003920void ssl_tls13_derive_handshake_secrets( int hash_alg,
3921 data_t *secret,
3922 data_t *transcript,
3923 data_t *client_expected,
3924 data_t *server_expected )
Hanno Beckera4f40a02021-05-24 06:42:11 +01003925{
Xiaofei Bai746f9482021-11-12 08:53:56 +00003926 mbedtls_ssl_tls13_handshake_secrets secrets;
Hanno Beckera4f40a02021-05-24 06:42:11 +01003927
3928 /* Double-check that we've passed sane parameters. */
3929 mbedtls_md_type_t md_type = (mbedtls_md_type_t) hash_alg;
3930 mbedtls_md_info_t const * const md_info = mbedtls_md_info_from_type( md_type );
3931 size_t const md_size = mbedtls_md_get_size( md_info );
3932 TEST_ASSERT( md_info != 0 &&
3933 secret->len == md_size &&
3934 transcript->len == md_size &&
3935 client_expected->len == md_size &&
3936 server_expected->len == md_size );
3937
Xiaofei Bai746f9482021-11-12 08:53:56 +00003938 TEST_ASSERT( mbedtls_ssl_tls13_derive_handshake_secrets(
Hanno Beckera4f40a02021-05-24 06:42:11 +01003939 md_type, secret->x, transcript->x, transcript->len,
3940 &secrets ) == 0 );
3941
3942 ASSERT_COMPARE( secrets.client_handshake_traffic_secret, md_size,
3943 client_expected->x, client_expected->len );
3944 ASSERT_COMPARE( secrets.server_handshake_traffic_secret, md_size,
3945 server_expected->x, server_expected->len );
3946}
3947/* END_CASE */
3948
Ronald Cron6f135e12021-12-08 16:57:54 +01003949/* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00003950void ssl_tls13_derive_application_secrets( int hash_alg,
3951 data_t *secret,
3952 data_t *transcript,
3953 data_t *client_expected,
3954 data_t *server_expected,
3955 data_t *exporter_expected )
Hanno Beckera4f40a02021-05-24 06:42:11 +01003956{
Xiaofei Bai746f9482021-11-12 08:53:56 +00003957 mbedtls_ssl_tls13_application_secrets secrets;
Hanno Beckera4f40a02021-05-24 06:42:11 +01003958
3959 /* Double-check that we've passed sane parameters. */
3960 mbedtls_md_type_t md_type = (mbedtls_md_type_t) hash_alg;
3961 mbedtls_md_info_t const * const md_info = mbedtls_md_info_from_type( md_type );
3962 size_t const md_size = mbedtls_md_get_size( md_info );
3963 TEST_ASSERT( md_info != 0 &&
3964 secret->len == md_size &&
3965 transcript->len == md_size &&
3966 client_expected->len == md_size &&
3967 server_expected->len == md_size &&
3968 exporter_expected->len == md_size );
3969
Xiaofei Bai746f9482021-11-12 08:53:56 +00003970 TEST_ASSERT( mbedtls_ssl_tls13_derive_application_secrets(
Hanno Beckera4f40a02021-05-24 06:42:11 +01003971 md_type, secret->x, transcript->x, transcript->len,
3972 &secrets ) == 0 );
3973
3974 ASSERT_COMPARE( secrets.client_application_traffic_secret_N, md_size,
3975 client_expected->x, client_expected->len );
3976 ASSERT_COMPARE( secrets.server_application_traffic_secret_N, md_size,
3977 server_expected->x, server_expected->len );
3978 ASSERT_COMPARE( secrets.exporter_master_secret, md_size,
3979 exporter_expected->x, exporter_expected->len );
3980}
3981/* END_CASE */
3982
Ronald Cron6f135e12021-12-08 16:57:54 +01003983/* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00003984void ssl_tls13_derive_resumption_secrets( int hash_alg,
3985 data_t *secret,
3986 data_t *transcript,
3987 data_t *resumption_expected )
Hanno Beckera4f40a02021-05-24 06:42:11 +01003988{
Xiaofei Bai746f9482021-11-12 08:53:56 +00003989 mbedtls_ssl_tls13_application_secrets secrets;
Hanno Beckera4f40a02021-05-24 06:42:11 +01003990
3991 /* Double-check that we've passed sane parameters. */
3992 mbedtls_md_type_t md_type = (mbedtls_md_type_t) hash_alg;
3993 mbedtls_md_info_t const * const md_info = mbedtls_md_info_from_type( md_type );
3994 size_t const md_size = mbedtls_md_get_size( md_info );
3995 TEST_ASSERT( md_info != 0 &&
3996 secret->len == md_size &&
3997 transcript->len == md_size &&
3998 resumption_expected->len == md_size );
3999
Xiaofei Bai746f9482021-11-12 08:53:56 +00004000 TEST_ASSERT( mbedtls_ssl_tls13_derive_resumption_master_secret(
Hanno Beckera4f40a02021-05-24 06:42:11 +01004001 md_type, secret->x, transcript->x, transcript->len,
4002 &secrets ) == 0 );
4003
4004 ASSERT_COMPARE( secrets.resumption_master_secret, md_size,
4005 resumption_expected->x, resumption_expected->len );
4006}
4007/* END_CASE */
4008
Ronald Cron6f135e12021-12-08 16:57:54 +01004009/* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00004010void ssl_tls13_create_psk_binder( int hash_alg,
4011 data_t *psk,
4012 int psk_type,
4013 data_t *transcript,
4014 data_t *binder_expected )
Hanno Becker55bc2c52021-05-24 06:53:52 +01004015{
4016 unsigned char binder[ MBEDTLS_MD_MAX_SIZE ];
4017
4018 /* Double-check that we've passed sane parameters. */
4019 mbedtls_md_type_t md_type = (mbedtls_md_type_t) hash_alg;
4020 mbedtls_md_info_t const * const md_info = mbedtls_md_info_from_type( md_type );
4021 size_t const md_size = mbedtls_md_get_size( md_info );
4022 TEST_ASSERT( md_info != 0 &&
4023 transcript->len == md_size &&
4024 binder_expected->len == md_size );
4025
Xiaofei Bai746f9482021-11-12 08:53:56 +00004026 TEST_ASSERT( mbedtls_ssl_tls13_create_psk_binder(
Hanno Becker55bc2c52021-05-24 06:53:52 +01004027 NULL, /* SSL context for debugging only */
4028 md_type,
4029 psk->x, psk->len,
4030 psk_type,
4031 transcript->x,
4032 binder ) == 0 );
4033
4034 ASSERT_COMPARE( binder, md_size,
4035 binder_expected->x, binder_expected->len );
4036}
4037/* END_CASE */
4038
Ronald Cron6f135e12021-12-08 16:57:54 +01004039/* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00004040void ssl_tls13_record_protection( int ciphersuite,
4041 int endpoint,
4042 int ctr,
4043 int padding_used,
4044 data_t *server_write_key,
4045 data_t *server_write_iv,
4046 data_t *client_write_key,
4047 data_t *client_write_iv,
4048 data_t *plaintext,
4049 data_t *ciphertext )
Hanno Beckera77d0052021-03-22 15:16:33 +00004050{
4051 mbedtls_ssl_key_set keys;
4052 mbedtls_ssl_transform transform_send;
4053 mbedtls_ssl_transform transform_recv;
4054 mbedtls_record rec;
4055 unsigned char *buf = NULL;
Hanno Becker1f918782021-08-01 19:18:28 +01004056 size_t buf_len;
Hanno Beckera77d0052021-03-22 15:16:33 +00004057 int other_endpoint;
4058
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01004059 USE_PSA_INIT( );
4060
Hanno Beckera77d0052021-03-22 15:16:33 +00004061 TEST_ASSERT( endpoint == MBEDTLS_SSL_IS_CLIENT ||
4062 endpoint == MBEDTLS_SSL_IS_SERVER );
4063
4064 if( endpoint == MBEDTLS_SSL_IS_SERVER )
4065 other_endpoint = MBEDTLS_SSL_IS_CLIENT;
4066 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4067 other_endpoint = MBEDTLS_SSL_IS_SERVER;
4068
4069 TEST_ASSERT( server_write_key->len == client_write_key->len );
4070 TEST_ASSERT( server_write_iv->len == client_write_iv->len );
4071
4072 memcpy( keys.client_write_key,
4073 client_write_key->x, client_write_key->len );
4074 memcpy( keys.client_write_iv,
4075 client_write_iv->x, client_write_iv->len );
4076 memcpy( keys.server_write_key,
4077 server_write_key->x, server_write_key->len );
4078 memcpy( keys.server_write_iv,
4079 server_write_iv->x, server_write_iv->len );
4080
4081 keys.key_len = server_write_key->len;
4082 keys.iv_len = server_write_iv->len;
4083
4084 mbedtls_ssl_transform_init( &transform_recv );
4085 mbedtls_ssl_transform_init( &transform_send );
4086
4087 TEST_ASSERT( mbedtls_ssl_tls13_populate_transform(
4088 &transform_send, endpoint,
4089 ciphersuite, &keys, NULL ) == 0 );
4090 TEST_ASSERT( mbedtls_ssl_tls13_populate_transform(
4091 &transform_recv, other_endpoint,
4092 ciphersuite, &keys, NULL ) == 0 );
4093
Hanno Becker1f918782021-08-01 19:18:28 +01004094 /* Make sure we have enough space in the buffer even if
4095 * we use more padding than the KAT. */
4096 buf_len = ciphertext->len + MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY;
4097 ASSERT_ALLOC( buf, buf_len );
Hanno Beckera77d0052021-03-22 15:16:33 +00004098 rec.type = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Hanno Becker41537452021-04-20 05:35:28 +01004099
4100 /* TLS 1.3 uses the version identifier from TLS 1.2 on the wire. */
Hanno Beckera77d0052021-03-22 15:16:33 +00004101 mbedtls_ssl_write_version( MBEDTLS_SSL_MAJOR_VERSION_3,
4102 MBEDTLS_SSL_MINOR_VERSION_3,
4103 MBEDTLS_SSL_TRANSPORT_STREAM,
4104 rec.ver );
4105
4106 /* Copy plaintext into record structure */
4107 rec.buf = buf;
Hanno Becker1f918782021-08-01 19:18:28 +01004108 rec.buf_len = buf_len;
Hanno Beckera77d0052021-03-22 15:16:33 +00004109 rec.data_offset = 0;
4110 TEST_ASSERT( plaintext->len <= ciphertext->len );
4111 memcpy( rec.buf + rec.data_offset, plaintext->x, plaintext->len );
4112 rec.data_len = plaintext->len;
4113#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4114 rec.cid_len = 0;
4115#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4116
4117 memset( &rec.ctr[0], 0, 8 );
4118 rec.ctr[7] = ctr;
4119
4120 TEST_ASSERT( mbedtls_ssl_encrypt_buf( NULL, &transform_send, &rec,
4121 NULL, NULL ) == 0 );
Hanno Becker1f918782021-08-01 19:18:28 +01004122
4123 if( padding_used == MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY )
4124 {
4125 ASSERT_COMPARE( rec.buf + rec.data_offset, rec.data_len,
4126 ciphertext->x, ciphertext->len );
4127 }
Hanno Beckera77d0052021-03-22 15:16:33 +00004128
4129 TEST_ASSERT( mbedtls_ssl_decrypt_buf( NULL, &transform_recv, &rec ) == 0 );
4130 ASSERT_COMPARE( rec.buf + rec.data_offset, rec.data_len,
4131 plaintext->x, plaintext->len );
4132
Hanno Becker80e760e2021-03-23 06:00:21 +00004133 mbedtls_free( buf );
Hanno Beckera77d0052021-03-22 15:16:33 +00004134 mbedtls_ssl_transform_free( &transform_send );
4135 mbedtls_ssl_transform_free( &transform_recv );
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01004136 USE_PSA_DONE( );
Hanno Beckera77d0052021-03-22 15:16:33 +00004137}
4138/* END_CASE */
4139
Ronald Cron6f135e12021-12-08 16:57:54 +01004140/* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00004141void ssl_tls13_key_evolution( int hash_alg,
4142 data_t *secret,
4143 data_t *input,
4144 data_t *expected )
Hanno Becker2d2c3eb2020-08-20 14:54:24 +01004145{
4146 unsigned char secret_new[ MBEDTLS_MD_MAX_SIZE ];
4147
Xiaofei Bai746f9482021-11-12 08:53:56 +00004148 TEST_ASSERT( mbedtls_ssl_tls13_evolve_secret(
Hanno Becker2d2c3eb2020-08-20 14:54:24 +01004149 (mbedtls_md_type_t) hash_alg,
4150 secret->len ? secret->x : NULL,
4151 input->len ? input->x : NULL, input->len,
4152 secret_new ) == 0 );
4153
Hanno Beckerfb080962020-09-08 10:58:42 +01004154 ASSERT_COMPARE( secret_new, (size_t) expected->len,
4155 expected->x, (size_t) expected->len );
Hanno Becker2d2c3eb2020-08-20 14:54:24 +01004156}
4157/* END_CASE */
4158
Ron Eldor824ad7b2019-05-13 14:09:00 +03004159/* BEGIN_CASE */
4160void ssl_tls_prf( int type, data_t * secret, data_t * random,
Ronald Cronac6ae352020-06-26 14:33:03 +02004161 char *label, data_t *result_str, int exp_ret )
Ron Eldor824ad7b2019-05-13 14:09:00 +03004162{
4163 unsigned char *output;
4164
Ronald Cronac6ae352020-06-26 14:33:03 +02004165 output = mbedtls_calloc( 1, result_str->len );
Ron Eldor824ad7b2019-05-13 14:09:00 +03004166 if( output == NULL )
4167 goto exit;
4168
Gilles Peskine9de97e22021-02-02 21:00:11 +01004169 USE_PSA_INIT( );
Ron Eldor6b9b1b82019-05-15 17:04:33 +03004170
Ron Eldor824ad7b2019-05-13 14:09:00 +03004171 TEST_ASSERT( mbedtls_ssl_tls_prf( type, secret->x, secret->len,
4172 label, random->x, random->len,
Ronald Cronac6ae352020-06-26 14:33:03 +02004173 output, result_str->len ) == exp_ret );
Ron Eldor824ad7b2019-05-13 14:09:00 +03004174
4175 if( exp_ret == 0 )
4176 {
Ronald Cronac6ae352020-06-26 14:33:03 +02004177 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
4178 result_str->len, result_str->len ) == 0 );
Ron Eldor824ad7b2019-05-13 14:09:00 +03004179 }
4180exit:
4181
4182 mbedtls_free( output );
Gilles Peskine1f186ff2021-02-02 21:04:06 +01004183 USE_PSA_DONE( );
Ron Eldor824ad7b2019-05-13 14:09:00 +03004184}
4185/* END_CASE */
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02004186
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02004187/* BEGIN_CASE */
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02004188void ssl_serialize_session_save_load( int ticket_len, char *crt_file )
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02004189{
4190 mbedtls_ssl_session original, restored;
4191 unsigned char *buf = NULL;
4192 size_t len;
4193
4194 /*
4195 * Test that a save-load pair is the identity
4196 */
4197
4198 mbedtls_ssl_session_init( &original );
4199 mbedtls_ssl_session_init( &restored );
4200
4201 /* Prepare a dummy session to work on */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004202 TEST_ASSERT( ssl_populate_session_tls12( &original, ticket_len, crt_file ) == 0 );
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02004203
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02004204 /* Serialize it */
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02004205 TEST_ASSERT( mbedtls_ssl_session_save( &original, NULL, 0, &len )
4206 == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4207 TEST_ASSERT( ( buf = mbedtls_calloc( 1, len ) ) != NULL );
4208 TEST_ASSERT( mbedtls_ssl_session_save( &original, buf, len, &len )
4209 == 0 );
4210
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02004211 /* Restore session from serialized data */
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02004212 TEST_ASSERT( mbedtls_ssl_session_load( &restored, buf, len) == 0 );
4213
4214 /*
4215 * Make sure both session structures are identical
4216 */
4217#if defined(MBEDTLS_HAVE_TIME)
4218 TEST_ASSERT( original.start == restored.start );
4219#endif
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004220 TEST_ASSERT( original.minor_ver == restored.minor_ver );
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02004221 TEST_ASSERT( original.ciphersuite == restored.ciphersuite );
4222 TEST_ASSERT( original.compression == restored.compression );
4223 TEST_ASSERT( original.id_len == restored.id_len );
4224 TEST_ASSERT( memcmp( original.id,
4225 restored.id, sizeof( original.id ) ) == 0 );
4226 TEST_ASSERT( memcmp( original.master,
4227 restored.master, sizeof( original.master ) ) == 0 );
4228
4229#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02004230#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02004231 TEST_ASSERT( ( original.peer_cert == NULL ) ==
4232 ( restored.peer_cert == NULL ) );
4233 if( original.peer_cert != NULL )
4234 {
4235 TEST_ASSERT( original.peer_cert->raw.len ==
4236 restored.peer_cert->raw.len );
4237 TEST_ASSERT( memcmp( original.peer_cert->raw.p,
4238 restored.peer_cert->raw.p,
4239 original.peer_cert->raw.len ) == 0 );
4240 }
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02004241#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4242 TEST_ASSERT( original.peer_cert_digest_type ==
4243 restored.peer_cert_digest_type );
4244 TEST_ASSERT( original.peer_cert_digest_len ==
4245 restored.peer_cert_digest_len );
4246 TEST_ASSERT( ( original.peer_cert_digest == NULL ) ==
4247 ( restored.peer_cert_digest == NULL ) );
4248 if( original.peer_cert_digest != NULL )
4249 {
4250 TEST_ASSERT( memcmp( original.peer_cert_digest,
4251 restored.peer_cert_digest,
4252 original.peer_cert_digest_len ) == 0 );
4253 }
4254#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4255#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02004256 TEST_ASSERT( original.verify_result == restored.verify_result );
4257
4258#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
4259 TEST_ASSERT( original.ticket_len == restored.ticket_len );
4260 if( original.ticket_len != 0 )
4261 {
4262 TEST_ASSERT( original.ticket != NULL );
4263 TEST_ASSERT( restored.ticket != NULL );
4264 TEST_ASSERT( memcmp( original.ticket,
4265 restored.ticket, original.ticket_len ) == 0 );
4266 }
4267 TEST_ASSERT( original.ticket_lifetime == restored.ticket_lifetime );
4268#endif
4269
4270#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
4271 TEST_ASSERT( original.mfl_code == restored.mfl_code );
4272#endif
4273
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02004274#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
4275 TEST_ASSERT( original.encrypt_then_mac == restored.encrypt_then_mac );
4276#endif
4277
4278exit:
4279 mbedtls_ssl_session_free( &original );
4280 mbedtls_ssl_session_free( &restored );
4281 mbedtls_free( buf );
4282}
4283/* END_CASE */
4284
Manuel Pégourié-Gonnardaa755832019-06-03 10:53:47 +02004285/* BEGIN_CASE */
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02004286void ssl_serialize_session_load_save( int ticket_len, char *crt_file )
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02004287{
4288 mbedtls_ssl_session session;
4289 unsigned char *buf1 = NULL, *buf2 = NULL;
4290 size_t len0, len1, len2;
4291
4292 /*
4293 * Test that a load-save pair is the identity
4294 */
4295
4296 mbedtls_ssl_session_init( &session );
4297
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02004298 /* Prepare a dummy session to work on */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004299 TEST_ASSERT( ssl_populate_session_tls12( &session, ticket_len, crt_file ) == 0 );
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02004300
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02004301 /* Get desired buffer size for serializing */
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02004302 TEST_ASSERT( mbedtls_ssl_session_save( &session, NULL, 0, &len0 )
4303 == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4304
4305 /* Allocate first buffer */
4306 buf1 = mbedtls_calloc( 1, len0 );
4307 TEST_ASSERT( buf1 != NULL );
4308
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02004309 /* Serialize to buffer and free live session */
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02004310 TEST_ASSERT( mbedtls_ssl_session_save( &session, buf1, len0, &len1 )
4311 == 0 );
4312 TEST_ASSERT( len0 == len1 );
4313 mbedtls_ssl_session_free( &session );
4314
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02004315 /* Restore session from serialized data */
Manuel Pégourié-Gonnard220403b2019-05-24 09:54:21 +02004316 TEST_ASSERT( mbedtls_ssl_session_load( &session, buf1, len1 ) == 0 );
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02004317
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02004318 /* Allocate second buffer and serialize to it */
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02004319 buf2 = mbedtls_calloc( 1, len0 );
Manuel Pégourié-Gonnardb4079902019-05-24 09:52:10 +02004320 TEST_ASSERT( buf2 != NULL );
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02004321 TEST_ASSERT( mbedtls_ssl_session_save( &session, buf2, len0, &len2 )
4322 == 0 );
4323
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02004324 /* Make sure both serialized versions are identical */
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02004325 TEST_ASSERT( len1 == len2 );
4326 TEST_ASSERT( memcmp( buf1, buf2, len1 ) == 0 );
4327
4328exit:
4329 mbedtls_ssl_session_free( &session );
4330 mbedtls_free( buf1 );
4331 mbedtls_free( buf2 );
4332}
4333/* END_CASE */
Manuel Pégourié-Gonnardf5fa0aa2019-05-23 10:38:11 +02004334
4335/* BEGIN_CASE */
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02004336void ssl_serialize_session_save_buf_size( int ticket_len, char *crt_file )
Manuel Pégourié-Gonnardf5fa0aa2019-05-23 10:38:11 +02004337{
4338 mbedtls_ssl_session session;
4339 unsigned char *buf = NULL;
4340 size_t good_len, bad_len, test_len;
4341
4342 /*
4343 * Test that session_save() fails cleanly on small buffers
4344 */
4345
4346 mbedtls_ssl_session_init( &session );
4347
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02004348 /* Prepare dummy session and get serialized size */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004349 TEST_ASSERT( ssl_populate_session_tls12( &session, ticket_len, crt_file ) == 0 );
Manuel Pégourié-Gonnardf5fa0aa2019-05-23 10:38:11 +02004350 TEST_ASSERT( mbedtls_ssl_session_save( &session, NULL, 0, &good_len )
4351 == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4352
4353 /* Try all possible bad lengths */
4354 for( bad_len = 1; bad_len < good_len; bad_len++ )
4355 {
4356 /* Allocate exact size so that asan/valgrind can detect any overwrite */
4357 mbedtls_free( buf );
4358 TEST_ASSERT( ( buf = mbedtls_calloc( 1, bad_len ) ) != NULL );
4359 TEST_ASSERT( mbedtls_ssl_session_save( &session, buf, bad_len,
4360 &test_len )
4361 == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4362 TEST_ASSERT( test_len == good_len );
4363 }
4364
4365exit:
4366 mbedtls_ssl_session_free( &session );
4367 mbedtls_free( buf );
4368}
4369/* END_CASE */
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02004370
4371/* BEGIN_CASE */
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02004372void ssl_serialize_session_load_buf_size( int ticket_len, char *crt_file )
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02004373{
4374 mbedtls_ssl_session session;
4375 unsigned char *good_buf = NULL, *bad_buf = NULL;
4376 size_t good_len, bad_len;
4377
4378 /*
4379 * Test that session_load() fails cleanly on small buffers
4380 */
4381
4382 mbedtls_ssl_session_init( &session );
4383
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02004384 /* Prepare serialized session data */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004385 TEST_ASSERT( ssl_populate_session_tls12( &session, ticket_len, crt_file ) == 0 );
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02004386 TEST_ASSERT( mbedtls_ssl_session_save( &session, NULL, 0, &good_len )
4387 == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4388 TEST_ASSERT( ( good_buf = mbedtls_calloc( 1, good_len ) ) != NULL );
4389 TEST_ASSERT( mbedtls_ssl_session_save( &session, good_buf, good_len,
4390 &good_len ) == 0 );
4391 mbedtls_ssl_session_free( &session );
4392
4393 /* Try all possible bad lengths */
4394 for( bad_len = 0; bad_len < good_len; bad_len++ )
4395 {
4396 /* Allocate exact size so that asan/valgrind can detect any overread */
4397 mbedtls_free( bad_buf );
4398 bad_buf = mbedtls_calloc( 1, bad_len ? bad_len : 1 );
4399 TEST_ASSERT( bad_buf != NULL );
4400 memcpy( bad_buf, good_buf, bad_len );
4401
4402 TEST_ASSERT( mbedtls_ssl_session_load( &session, bad_buf, bad_len )
4403 == MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4404 }
4405
4406exit:
4407 mbedtls_ssl_session_free( &session );
4408 mbedtls_free( good_buf );
4409 mbedtls_free( bad_buf );
4410}
4411/* END_CASE */
Hanno Becker861d0bb2019-05-21 16:39:30 +01004412
Hanno Becker363b6462019-05-29 12:44:28 +01004413/* BEGIN_CASE */
4414void ssl_session_serialize_version_check( int corrupt_major,
Hanno Becker861d0bb2019-05-21 16:39:30 +01004415 int corrupt_minor,
4416 int corrupt_patch,
4417 int corrupt_config )
4418{
Hanno Becker363b6462019-05-29 12:44:28 +01004419 unsigned char serialized_session[ 2048 ];
4420 size_t serialized_session_len;
Hanno Beckerfe1275e2019-05-29 12:45:21 +01004421 unsigned cur_byte;
Hanno Becker861d0bb2019-05-21 16:39:30 +01004422 mbedtls_ssl_session session;
Hanno Beckerfe1275e2019-05-29 12:45:21 +01004423 uint8_t should_corrupt_byte[] = { corrupt_major == 1,
4424 corrupt_minor == 1,
4425 corrupt_patch == 1,
4426 corrupt_config == 1,
4427 corrupt_config == 1 };
4428
Hanno Becker861d0bb2019-05-21 16:39:30 +01004429 mbedtls_ssl_session_init( &session );
Paul Elliott46a6c202021-12-09 18:16:13 +00004430 TEST_ASSERT( ssl_populate_session_tls12( &session, 0, NULL ) == 0 );
Hanno Becker861d0bb2019-05-21 16:39:30 +01004431
Hanno Beckerfe1275e2019-05-29 12:45:21 +01004432 /* Infer length of serialized session. */
Hanno Becker861d0bb2019-05-21 16:39:30 +01004433 TEST_ASSERT( mbedtls_ssl_session_save( &session,
Hanno Becker363b6462019-05-29 12:44:28 +01004434 serialized_session,
4435 sizeof( serialized_session ),
4436 &serialized_session_len ) == 0 );
Hanno Becker861d0bb2019-05-21 16:39:30 +01004437
Hanno Beckerfe1275e2019-05-29 12:45:21 +01004438 mbedtls_ssl_session_free( &session );
Hanno Becker861d0bb2019-05-21 16:39:30 +01004439
Hanno Beckerfe1275e2019-05-29 12:45:21 +01004440 /* Without any modification, we should be able to successfully
Hanno Becker363b6462019-05-29 12:44:28 +01004441 * de-serialize the session - double-check that. */
Hanno Becker861d0bb2019-05-21 16:39:30 +01004442 TEST_ASSERT( mbedtls_ssl_session_load( &session,
Hanno Becker363b6462019-05-29 12:44:28 +01004443 serialized_session,
4444 serialized_session_len ) == 0 );
Hanno Becker861d0bb2019-05-21 16:39:30 +01004445 mbedtls_ssl_session_free( &session );
4446
Hanno Beckerfe1275e2019-05-29 12:45:21 +01004447 /* Go through the bytes in the serialized session header and
4448 * corrupt them bit-by-bit. */
4449 for( cur_byte = 0; cur_byte < sizeof( should_corrupt_byte ); cur_byte++ )
Hanno Becker861d0bb2019-05-21 16:39:30 +01004450 {
Hanno Beckerfe1275e2019-05-29 12:45:21 +01004451 int cur_bit;
4452 unsigned char * const byte = &serialized_session[ cur_byte ];
4453
4454 if( should_corrupt_byte[ cur_byte ] == 0 )
4455 continue;
4456
4457 for( cur_bit = 0; cur_bit < CHAR_BIT; cur_bit++ )
4458 {
4459 unsigned char const corrupted_bit = 0x1u << cur_bit;
4460 /* Modify a single bit in the serialized session. */
4461 *byte ^= corrupted_bit;
4462
4463 /* Attempt to deserialize */
4464 TEST_ASSERT( mbedtls_ssl_session_load( &session,
4465 serialized_session,
4466 serialized_session_len ) ==
Hanno Beckerf9b33032019-06-03 12:58:39 +01004467 MBEDTLS_ERR_SSL_VERSION_MISMATCH );
Hanno Beckerfe1275e2019-05-29 12:45:21 +01004468
4469 /* Undo the change */
4470 *byte ^= corrupted_bit;
4471 }
Hanno Becker861d0bb2019-05-21 16:39:30 +01004472 }
4473
Hanno Becker861d0bb2019-05-21 16:39:30 +01004474}
4475/* END_CASE */
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01004476
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02004477/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_ENTROPY_C:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01004478void mbedtls_endpoint_sanity( int endpoint_type )
4479{
4480 enum { BUFFSIZE = 1024 };
4481 mbedtls_endpoint ep;
4482 int ret = -1;
4483
Andrzej Kurek15daf502020-02-12 09:17:52 -05004484 ret = mbedtls_endpoint_init( NULL, endpoint_type, MBEDTLS_PK_RSA,
4485 NULL, NULL, NULL );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01004486 TEST_ASSERT( MBEDTLS_ERR_SSL_BAD_INPUT_DATA == ret );
4487
Andrzej Kurekb2980742020-02-02 19:25:26 -05004488 ret = mbedtls_endpoint_certificate_init( NULL, MBEDTLS_PK_RSA );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01004489 TEST_ASSERT( MBEDTLS_ERR_SSL_BAD_INPUT_DATA == ret );
4490
Andrzej Kurek15daf502020-02-12 09:17:52 -05004491 ret = mbedtls_endpoint_init( &ep, endpoint_type, MBEDTLS_PK_RSA,
4492 NULL, NULL, NULL );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01004493 TEST_ASSERT( ret == 0 );
4494
4495exit:
Andrzej Kurek15daf502020-02-12 09:17:52 -05004496 mbedtls_endpoint_free( &ep, NULL );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01004497}
4498/* END_CASE */
4499
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02004500/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_ENTROPY_C:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01004501void move_handshake_to_state(int endpoint_type, int state, int need_pass)
4502{
4503 enum { BUFFSIZE = 1024 };
4504 mbedtls_endpoint base_ep, second_ep;
4505 int ret = -1;
4506
Andrzej Kurek15daf502020-02-12 09:17:52 -05004507 ret = mbedtls_endpoint_init( &base_ep, endpoint_type, MBEDTLS_PK_RSA,
4508 NULL, NULL, NULL );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01004509 TEST_ASSERT( ret == 0 );
4510
4511 ret = mbedtls_endpoint_init( &second_ep,
4512 ( endpoint_type == MBEDTLS_SSL_IS_SERVER ) ?
Andrzej Kurekb2980742020-02-02 19:25:26 -05004513 MBEDTLS_SSL_IS_CLIENT : MBEDTLS_SSL_IS_SERVER,
Andrzej Kurek15daf502020-02-12 09:17:52 -05004514 MBEDTLS_PK_RSA, NULL, NULL, NULL );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01004515 TEST_ASSERT( ret == 0 );
4516
4517 ret = mbedtls_mock_socket_connect( &(base_ep.socket),
4518 &(second_ep.socket),
4519 BUFFSIZE );
4520 TEST_ASSERT( ret == 0 );
4521
4522 ret = mbedtls_move_handshake_to_state( &(base_ep.ssl),
4523 &(second_ep.ssl),
4524 state );
4525 if( need_pass )
4526 {
4527 TEST_ASSERT( ret == 0 );
4528 TEST_ASSERT( base_ep.ssl.state == state );
4529 }
4530 else
4531 {
4532 TEST_ASSERT( ret != 0 );
4533 TEST_ASSERT( base_ep.ssl.state != state );
4534 }
4535
4536exit:
Andrzej Kurek15daf502020-02-12 09:17:52 -05004537 mbedtls_endpoint_free( &base_ep, NULL );
4538 mbedtls_endpoint_free( &second_ep, NULL );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01004539}
4540/* END_CASE */
Andrzej Kurekf40daa32020-02-04 09:00:01 -05004541
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02004542/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
Paul Elliottc8570442020-04-15 17:00:50 +01004543void handshake_version( int dtls, int client_min_version, int client_max_version,
4544 int server_min_version, int server_max_version,
4545 int expected_negotiated_version )
Andrzej Kurekf40daa32020-02-04 09:00:01 -05004546{
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004547 handshake_test_options options;
4548 init_handshake_options( &options );
Andrzej Kurekda2b6782020-02-12 07:56:36 -05004549
Paul Elliottc8570442020-04-15 17:00:50 +01004550 options.client_min_version = client_min_version;
4551 options.client_max_version = client_max_version;
4552 options.server_min_version = server_min_version;
4553 options.server_max_version = server_max_version;
4554
4555 options.expected_negotiated_version = expected_negotiated_version;
4556
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004557 options.dtls = dtls;
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004558 perform_handshake( &options );
Andrzej Kurekf40daa32020-02-04 09:00:01 -05004559
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004560 /* The goto below is used to avoid an "unused label" warning.*/
4561 goto exit;
4562}
4563/* END_CASE */
Andrzej Kurek9e9efdc2020-02-26 05:25:23 -05004564
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02004565/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004566void handshake_psk_cipher( char* cipher, int pk_alg, data_t *psk_str, int dtls )
4567{
4568 handshake_test_options options;
4569 init_handshake_options( &options );
Andrzej Kurekf40daa32020-02-04 09:00:01 -05004570
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004571 options.cipher = cipher;
4572 options.dtls = dtls;
4573 options.psk_str = psk_str;
4574 options.pk_alg = pk_alg;
Andrzej Kurekcc5169c2020-02-04 09:04:56 -05004575
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004576 perform_handshake( &options );
Andrzej Kurek316da1f2020-02-26 09:03:47 -05004577
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004578 /* The goto below is used to avoid an "unused label" warning.*/
4579 goto exit;
4580}
4581/* END_CASE */
Andrzej Kurek316da1f2020-02-26 09:03:47 -05004582
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02004583/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004584void handshake_cipher( char* cipher, int pk_alg, int dtls )
4585{
4586 test_handshake_psk_cipher( cipher, pk_alg, NULL, dtls );
Andrzej Kurekf40daa32020-02-04 09:00:01 -05004587
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004588 /* The goto below is used to avoid an "unused label" warning.*/
4589 goto exit;
4590}
4591/* END_CASE */
Andrzej Kurekf40daa32020-02-04 09:00:01 -05004592
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02004593/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004594void app_data( int mfl, int cli_msg_len, int srv_msg_len,
4595 int expected_cli_fragments,
4596 int expected_srv_fragments, int dtls )
4597{
4598 handshake_test_options options;
4599 init_handshake_options( &options );
Andrzej Kurekda2b6782020-02-12 07:56:36 -05004600
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004601 options.mfl = mfl;
4602 options.cli_msg_len = cli_msg_len;
4603 options.srv_msg_len = srv_msg_len;
4604 options.expected_cli_fragments = expected_cli_fragments;
4605 options.expected_srv_fragments = expected_srv_fragments;
4606 options.dtls = dtls;
Andrzej Kurekda2b6782020-02-12 07:56:36 -05004607
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004608 perform_handshake( &options );
4609 /* The goto below is used to avoid an "unused label" warning.*/
4610 goto exit;
4611}
4612/* END_CASE */
Andrzej Kurekda2b6782020-02-12 07:56:36 -05004613
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02004614/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004615void app_data_tls( int mfl, int cli_msg_len, int srv_msg_len,
4616 int expected_cli_fragments,
4617 int expected_srv_fragments )
4618{
4619 test_app_data( mfl, cli_msg_len, srv_msg_len, expected_cli_fragments,
4620 expected_srv_fragments, 0 );
4621 /* The goto below is used to avoid an "unused label" warning.*/
4622 goto exit;
4623}
4624/* END_CASE */
Andrzej Kurekda2b6782020-02-12 07:56:36 -05004625
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02004626/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_SSL_PROTO_DTLS:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004627void app_data_dtls( int mfl, int cli_msg_len, int srv_msg_len,
4628 int expected_cli_fragments,
4629 int expected_srv_fragments )
4630{
4631 test_app_data( mfl, cli_msg_len, srv_msg_len, expected_cli_fragments,
4632 expected_srv_fragments, 1 );
4633 /* The goto below is used to avoid an "unused label" warning.*/
4634 goto exit;
4635}
4636/* END_CASE */
Andrzej Kurekda2b6782020-02-12 07:56:36 -05004637
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02004638/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_SSL_PROTO_DTLS:MBEDTLS_SSL_RENEGOTIATION:MBEDTLS_SSL_CONTEXT_SERIALIZATION:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004639void handshake_serialization( )
4640{
4641 handshake_test_options options;
4642 init_handshake_options( &options );
Andrzej Kurekda2b6782020-02-12 07:56:36 -05004643
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004644 options.serialize = 1;
4645 options.dtls = 1;
4646 perform_handshake( &options );
4647 /* The goto below is used to avoid an "unused label" warning.*/
4648 goto exit;
4649}
4650/* END_CASE */
Andrzej Kurekda2b6782020-02-12 07:56:36 -05004651
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02004652/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_DEBUG_C:MBEDTLS_SSL_MAX_FRAGMENT_LENGTH:MBEDTLS_CIPHER_MODE_CBC:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
Piotr Nowickibde7ee82020-02-21 10:59:50 +01004653void handshake_fragmentation( int mfl, int expected_srv_hs_fragmentation, int expected_cli_hs_fragmentation)
4654{
4655 handshake_test_options options;
4656 log_pattern srv_pattern, cli_pattern;
4657
4658 srv_pattern.pattern = cli_pattern.pattern = "found fragmented DTLS handshake";
4659 srv_pattern.counter = 0;
4660 cli_pattern.counter = 0;
4661
4662 init_handshake_options( &options );
4663 options.dtls = 1;
4664 options.mfl = mfl;
Darryl Greenaad82f92019-12-02 10:53:11 +00004665 /* Set cipher to one using CBC so that record splitting can be tested */
4666 options.cipher = "TLS-DHE-RSA-WITH-AES-256-CBC-SHA256";
Piotr Nowickibde7ee82020-02-21 10:59:50 +01004667 options.srv_auth_mode = MBEDTLS_SSL_VERIFY_REQUIRED;
4668 options.srv_log_obj = &srv_pattern;
4669 options.cli_log_obj = &cli_pattern;
4670 options.srv_log_fun = log_analyzer;
4671 options.cli_log_fun = log_analyzer;
4672
4673 perform_handshake( &options );
4674
4675 /* Test if the server received a fragmented handshake */
4676 if( expected_srv_hs_fragmentation )
4677 {
4678 TEST_ASSERT( srv_pattern.counter >= 1 );
4679 }
4680 /* Test if the client received a fragmented handshake */
4681 if( expected_cli_hs_fragmentation )
4682 {
4683 TEST_ASSERT( cli_pattern.counter >= 1 );
4684 }
4685}
4686/* END_CASE */
4687
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02004688/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_SSL_PROTO_DTLS:MBEDTLS_SSL_RENEGOTIATION:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004689void renegotiation( int legacy_renegotiation )
4690{
4691 handshake_test_options options;
4692 init_handshake_options( &options );
Andrzej Kurekda2b6782020-02-12 07:56:36 -05004693
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004694 options.renegotiate = 1;
4695 options.legacy_renegotiation = legacy_renegotiation;
4696 options.dtls = 1;
Andrzej Kurek316da1f2020-02-26 09:03:47 -05004697
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004698 perform_handshake( &options );
4699 /* The goto below is used to avoid an "unused label" warning.*/
4700 goto exit;
Andrzej Kurekf40daa32020-02-04 09:00:01 -05004701}
4702/* END_CASE */
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004703
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02004704/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004705void resize_buffers( int mfl, int renegotiation, int legacy_renegotiation,
Andrzej Kurek8ea68722020-04-03 06:40:47 -04004706 int serialize, int dtls, char *cipher )
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004707{
4708 handshake_test_options options;
4709 init_handshake_options( &options );
4710
4711 options.mfl = mfl;
Andrzej Kurek8ea68722020-04-03 06:40:47 -04004712 options.cipher = cipher;
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004713 options.renegotiate = renegotiation;
4714 options.legacy_renegotiation = legacy_renegotiation;
4715 options.serialize = serialize;
4716 options.dtls = dtls;
4717 options.resize_buffers = 1;
4718
4719 perform_handshake( &options );
4720 /* The goto below is used to avoid an "unused label" warning.*/
4721 goto exit;
4722}
4723/* END_CASE */
4724
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02004725/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH:MBEDTLS_SSL_CONTEXT_SERIALIZATION:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_SSL_PROTO_DTLS:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004726void resize_buffers_serialize_mfl( int mfl )
4727{
Andrzej Kurek8ea68722020-04-03 06:40:47 -04004728 test_resize_buffers( mfl, 0, MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION, 1, 1,
4729 (char *) "" );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004730
4731 /* The goto below is used to avoid an "unused label" warning.*/
4732 goto exit;
4733}
4734/* END_CASE */
4735
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02004736/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH:MBEDTLS_SSL_RENEGOTIATION:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
Andrzej Kurek8ea68722020-04-03 06:40:47 -04004737void resize_buffers_renegotiate_mfl( int mfl, int legacy_renegotiation,
4738 char *cipher )
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004739{
Andrzej Kurek8ea68722020-04-03 06:40:47 -04004740 test_resize_buffers( mfl, 1, legacy_renegotiation, 0, 1, cipher );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004741
4742 /* The goto below is used to avoid an "unused label" warning.*/
4743 goto exit;
4744}
4745/* END_CASE */
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02004746
Manuel Pégourié-Gonnarded0e8642020-07-21 11:20:30 +02004747/* BEGIN_CASE depends_on:MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC:MBEDTLS_TEST_HOOKS */
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02004748void ssl_cf_hmac( int hash )
4749{
4750 /*
Gabor Mezei90437e32021-10-20 11:59:27 +02004751 * Test the function mbedtls_ct_hmac() against a reference
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02004752 * implementation.
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02004753 */
4754 mbedtls_md_context_t ctx, ref_ctx;
4755 const mbedtls_md_info_t *md_info;
4756 size_t out_len, block_size;
4757 size_t min_in_len, in_len, max_in_len, i;
4758 /* TLS additional data is 13 bytes (hence the "lucky 13" name) */
4759 unsigned char add_data[13];
4760 unsigned char ref_out[MBEDTLS_MD_MAX_SIZE];
4761 unsigned char *data = NULL;
4762 unsigned char *out = NULL;
4763 unsigned char rec_num = 0;
4764
4765 mbedtls_md_init( &ctx );
4766 mbedtls_md_init( &ref_ctx );
4767
4768 md_info = mbedtls_md_info_from_type( hash );
4769 TEST_ASSERT( md_info != NULL );
4770 out_len = mbedtls_md_get_size( md_info );
4771 TEST_ASSERT( out_len != 0 );
4772 block_size = hash == MBEDTLS_MD_SHA384 ? 128 : 64;
4773
4774 /* Use allocated out buffer to catch overwrites */
4775 ASSERT_ALLOC( out, out_len );
4776
4777 /* Set up contexts with the given hash and a dummy key */
4778 TEST_EQUAL( 0, mbedtls_md_setup( &ctx, md_info, 1 ) );
4779 TEST_EQUAL( 0, mbedtls_md_setup( &ref_ctx, md_info, 1 ) );
4780 memset( ref_out, 42, sizeof( ref_out ) );
4781 TEST_EQUAL( 0, mbedtls_md_hmac_starts( &ctx, ref_out, out_len ) );
4782 TEST_EQUAL( 0, mbedtls_md_hmac_starts( &ref_ctx, ref_out, out_len ) );
4783 memset( ref_out, 0, sizeof( ref_out ) );
4784
4785 /*
4786 * Test all possible lengths up to a point. The difference between
4787 * max_in_len and min_in_len is at most 255, and make sure they both vary
4788 * by at least one block size.
4789 */
4790 for( max_in_len = 0; max_in_len <= 255 + block_size; max_in_len++ )
4791 {
Chris Jones9634bb12021-01-20 15:56:42 +00004792 mbedtls_test_set_step( max_in_len * 10000 );
Manuel Pégourié-Gonnardca8287c2020-07-22 10:29:39 +02004793
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02004794 /* Use allocated in buffer to catch overreads */
Manuel Pégourié-Gonnardc3219002020-07-22 10:32:52 +02004795 ASSERT_ALLOC( data, max_in_len );
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02004796
4797 min_in_len = max_in_len > 255 ? max_in_len - 255 : 0;
4798 for( in_len = min_in_len; in_len <= max_in_len; in_len++ )
4799 {
Chris Jones9634bb12021-01-20 15:56:42 +00004800 mbedtls_test_set_step( max_in_len * 10000 + in_len );
Manuel Pégourié-Gonnardca8287c2020-07-22 10:29:39 +02004801
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02004802 /* Set up dummy data and add_data */
4803 rec_num++;
4804 memset( add_data, rec_num, sizeof( add_data ) );
4805 for( i = 0; i < in_len; i++ )
4806 data[i] = ( i & 0xff ) ^ rec_num;
4807
4808 /* Get the function's result */
Manuel Pégourié-Gonnard9670a592020-07-10 10:21:46 +02004809 TEST_CF_SECRET( &in_len, sizeof( in_len ) );
Gabor Mezei90437e32021-10-20 11:59:27 +02004810 TEST_EQUAL( 0, mbedtls_ct_hmac( &ctx, add_data, sizeof( add_data ),
gabor-mezei-arm9fa43ce2021-09-28 16:14:47 +02004811 data, in_len,
4812 min_in_len, max_in_len,
4813 out ) );
Manuel Pégourié-Gonnard9670a592020-07-10 10:21:46 +02004814 TEST_CF_PUBLIC( &in_len, sizeof( in_len ) );
4815 TEST_CF_PUBLIC( out, out_len );
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02004816
4817 /* Compute the reference result */
4818 TEST_EQUAL( 0, mbedtls_md_hmac_update( &ref_ctx, add_data,
4819 sizeof( add_data ) ) );
4820 TEST_EQUAL( 0, mbedtls_md_hmac_update( &ref_ctx, data, in_len ) );
4821 TEST_EQUAL( 0, mbedtls_md_hmac_finish( &ref_ctx, ref_out ) );
4822 TEST_EQUAL( 0, mbedtls_md_hmac_reset( &ref_ctx ) );
4823
4824 /* Compare */
4825 ASSERT_COMPARE( out, out_len, ref_out, out_len );
4826 }
4827
4828 mbedtls_free( data );
4829 data = NULL;
4830 }
4831
4832exit:
4833 mbedtls_md_free( &ref_ctx );
4834 mbedtls_md_free( &ctx );
4835
4836 mbedtls_free( data );
4837 mbedtls_free( out );
4838}
4839/* END_CASE */
Manuel Pégourié-Gonnard7fe2c5f2020-08-18 12:02:54 +02004840
4841/* BEGIN_CASE depends_on:MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC:MBEDTLS_TEST_HOOKS */
4842void ssl_cf_memcpy_offset( int offset_min, int offset_max, int len )
4843{
4844 unsigned char *dst = NULL;
4845 unsigned char *src = NULL;
4846 size_t src_len = offset_max + len;
4847 size_t secret;
4848
4849 ASSERT_ALLOC( dst, len );
4850 ASSERT_ALLOC( src, src_len );
4851
4852 /* Fill src in a way that we can detect if we copied the right bytes */
4853 mbedtls_test_rnd_std_rand( NULL, src, src_len );
4854
4855 for( secret = offset_min; secret <= (size_t) offset_max; secret++ )
4856 {
Chris Jones9634bb12021-01-20 15:56:42 +00004857 mbedtls_test_set_step( (int) secret );
Manuel Pégourié-Gonnard7fe2c5f2020-08-18 12:02:54 +02004858
4859 TEST_CF_SECRET( &secret, sizeof( secret ) );
Gabor Mezei90437e32021-10-20 11:59:27 +02004860 mbedtls_ct_memcpy_offset( dst, src, secret,
gabor-mezei-arm9fa43ce2021-09-28 16:14:47 +02004861 offset_min, offset_max, len );
Manuel Pégourié-Gonnard7fe2c5f2020-08-18 12:02:54 +02004862 TEST_CF_PUBLIC( &secret, sizeof( secret ) );
4863 TEST_CF_PUBLIC( dst, len );
4864
4865 ASSERT_COMPARE( dst, len, src + secret, len );
4866 }
4867
4868exit:
4869 mbedtls_free( dst );
4870 mbedtls_free( src );
4871}
4872/* END_CASE */
Hanno Becker6667ffd2021-04-19 21:59:22 +01004873
4874/* BEGIN_CASE depends_on:MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
4875void test_multiple_psks()
4876{
4877 unsigned char psk0[10] = { 0 };
4878 unsigned char psk0_identity[] = { 'f', 'o', 'o' };
4879
4880 unsigned char psk1[10] = { 0 };
4881 unsigned char psk1_identity[] = { 'b', 'a', 'r' };
4882
4883 mbedtls_ssl_config conf;
4884
4885 mbedtls_ssl_config_init( &conf );
4886
4887 TEST_ASSERT( mbedtls_ssl_conf_psk( &conf,
4888 psk0, sizeof( psk0 ),
4889 psk0_identity, sizeof( psk0_identity ) ) == 0 );
4890 TEST_ASSERT( mbedtls_ssl_conf_psk( &conf,
4891 psk1, sizeof( psk1 ),
4892 psk1_identity, sizeof( psk1_identity ) ) ==
4893 MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4894
4895exit:
4896
4897 mbedtls_ssl_config_free( &conf );
4898}
4899/* END_CASE */
4900
4901/* BEGIN_CASE depends_on:MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED:MBEDTLS_USE_PSA_CRYPTO */
4902void test_multiple_psks_opaque( int mode )
4903{
4904 /*
4905 * Mode 0: Raw PSK, then opaque PSK
4906 * Mode 1: Opaque PSK, then raw PSK
4907 * Mode 2: 2x opaque PSK
4908 */
4909
4910 unsigned char psk0_raw[10] = { 0 };
4911 unsigned char psk0_raw_identity[] = { 'f', 'o', 'o' };
4912
Andrzej Kurek03e01462022-01-03 12:53:24 +01004913 mbedtls_svc_key_id_t psk0_opaque = mbedtls_svc_key_id_make( 0x1, (psa_key_id_t) 1 );
4914
Hanno Becker6667ffd2021-04-19 21:59:22 +01004915 unsigned char psk0_opaque_identity[] = { 'f', 'o', 'o' };
4916
4917 unsigned char psk1_raw[10] = { 0 };
4918 unsigned char psk1_raw_identity[] = { 'b', 'a', 'r' };
4919
Andrzej Kurek03e01462022-01-03 12:53:24 +01004920 mbedtls_svc_key_id_t psk1_opaque = mbedtls_svc_key_id_make( 0x1, (psa_key_id_t) 2 );
4921
Hanno Becker6667ffd2021-04-19 21:59:22 +01004922 unsigned char psk1_opaque_identity[] = { 'b', 'a', 'r' };
4923
4924 mbedtls_ssl_config conf;
4925
4926 USE_PSA_INIT( );
4927 mbedtls_ssl_config_init( &conf );
4928
4929 switch( mode )
4930 {
4931 case 0:
4932
4933 TEST_ASSERT( mbedtls_ssl_conf_psk( &conf,
4934 psk0_raw, sizeof( psk0_raw ),
4935 psk0_raw_identity, sizeof( psk0_raw_identity ) )
4936 == 0 );
4937 TEST_ASSERT( mbedtls_ssl_conf_psk_opaque( &conf,
4938 psk1_opaque,
4939 psk1_opaque_identity, sizeof( psk1_opaque_identity ) )
4940 == MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4941 break;
4942
4943 case 1:
4944
4945 TEST_ASSERT( mbedtls_ssl_conf_psk_opaque( &conf,
4946 psk0_opaque,
4947 psk0_opaque_identity, sizeof( psk0_opaque_identity ) )
4948 == 0 );
4949 TEST_ASSERT( mbedtls_ssl_conf_psk( &conf,
4950 psk1_raw, sizeof( psk1_raw ),
4951 psk1_raw_identity, sizeof( psk1_raw_identity ) )
4952 == MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4953
4954 break;
4955
4956 case 2:
4957
4958 TEST_ASSERT( mbedtls_ssl_conf_psk_opaque( &conf,
4959 psk0_opaque,
4960 psk0_opaque_identity, sizeof( psk0_opaque_identity ) )
4961 == 0 );
4962 TEST_ASSERT( mbedtls_ssl_conf_psk_opaque( &conf,
4963 psk1_opaque,
4964 psk1_opaque_identity, sizeof( psk1_opaque_identity ) )
4965 == MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4966
4967 break;
4968
4969 default:
4970 TEST_ASSERT( 0 );
4971 break;
4972 }
4973
4974exit:
4975
4976 mbedtls_ssl_config_free( &conf );
4977 USE_PSA_DONE( );
4978
4979}
4980/* END_CASE */
Brett Warren7f813d52021-10-20 23:08:38 +01004981
4982/* BEGIN_CASE depends_on:MBEDTLS_ECP_C:!MBEDTLS_DEPRECATED_REMOVED:!MBEDTLS_DEPRECATED_WARNING:MBEDTLS_ECP_DP_SECP192R1_ENABLED:MBEDTLS_ECP_DP_SECP224R1_ENABLED:MBEDTLS_ECP_DP_SECP256R1_ENABLED */
4983void conf_curve()
4984{
4985
4986 mbedtls_ecp_group_id curve_list[] = { MBEDTLS_ECP_DP_SECP192R1,
4987 MBEDTLS_ECP_DP_SECP224R1,
4988 MBEDTLS_ECP_DP_SECP256R1,
4989 MBEDTLS_ECP_DP_NONE };
4990 mbedtls_ecp_group_id iana_tls_group_list[] = { MBEDTLS_SSL_IANA_TLS_GROUP_SECP192R1,
4991 MBEDTLS_SSL_IANA_TLS_GROUP_SECP224R1,
4992 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
4993 MBEDTLS_SSL_IANA_TLS_GROUP_NONE };
4994
4995 mbedtls_ssl_config conf;
4996 mbedtls_ssl_config_init( &conf );
4997
4998 mbedtls_ssl_conf_max_version( &conf, 3, 3 );
4999 mbedtls_ssl_conf_min_version( &conf, 3, 3 );
5000 mbedtls_ssl_conf_curves( &conf, curve_list );
5001
5002 mbedtls_ssl_context ssl;
5003 mbedtls_ssl_init( &ssl );
Paul Elliott46a6c202021-12-09 18:16:13 +00005004 TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
Brett Warren7f813d52021-10-20 23:08:38 +01005005
5006 TEST_ASSERT( ssl.handshake != NULL && ssl.handshake->group_list != NULL );
5007 TEST_ASSERT( ssl.conf != NULL && ssl.conf->group_list == NULL );
5008
5009 TEST_EQUAL( ssl.handshake->group_list[ARRAY_LENGTH( iana_tls_group_list ) - 1], MBEDTLS_SSL_IANA_TLS_GROUP_NONE );
5010
5011 for( size_t i = 0; i < ARRAY_LENGTH( iana_tls_group_list ); i++ )
5012 TEST_EQUAL( iana_tls_group_list[i], ssl.handshake->group_list[i] );
5013
5014 mbedtls_ssl_free( &ssl );
5015 mbedtls_ssl_config_free( &conf );
5016}
5017/* END_CASE */
5018
5019/* BEGIN_CASE depends_on:MBEDTLS_DEPRECATED_REMOVED */
5020void conf_group()
5021{
5022 uint16_t iana_tls_group_list[] = { MBEDTLS_SSL_IANA_TLS_GROUP_SECP192R1,
5023 MBEDTLS_SSL_IANA_TLS_GROUP_SECP224R1,
5024 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
5025 MBEDTLS_SSL_IANA_TLS_GROUP_NONE };
5026
5027 mbedtls_ssl_config conf;
5028 mbedtls_ssl_config_init( &conf );
5029
5030 mbedtls_ssl_conf_max_version( &conf, 3, 3 );
5031 mbedtls_ssl_conf_min_version( &conf, 3, 3 );
5032
5033 mbedtls_ssl_conf_groups( &conf, iana_tls_group_list );
5034
5035 mbedtls_ssl_context ssl;
5036 mbedtls_ssl_init( &ssl );
Paul Elliott46a6c202021-12-09 18:16:13 +00005037 TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
Brett Warren7f813d52021-10-20 23:08:38 +01005038
5039 TEST_ASSERT( ssl.conf != NULL && ssl.conf->group_list != NULL );
5040
5041 TEST_EQUAL( ssl.conf->group_list[ARRAY_LENGTH( iana_tls_group_list ) - 1], MBEDTLS_SSL_IANA_TLS_GROUP_NONE );
5042
5043 for( size_t i = 0; i < ARRAY_LENGTH( iana_tls_group_list ); i++ )
5044 TEST_EQUAL( iana_tls_group_list[i], ssl.conf->group_list[i] );
5045
5046 mbedtls_ssl_free( &ssl );
5047 mbedtls_ssl_config_free( &conf );
5048}
5049/* END_CASE */