blob: 0351db093004e5a075fe323c8a5d21a6ff48e6b5 [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
Przemyslaw Stekielf4facef2022-02-02 21:31:04 +01001189#if defined(MBEDTLS_USE_PSA_CRYPTO)
1190static int psa_cipher_encrypt_helper( mbedtls_ssl_transform *transform,
1191 const unsigned char *iv, size_t iv_len,
1192 const unsigned char *input, size_t ilen,
1193 unsigned char *output, size_t *olen )
1194{
1195 psa_status_t status;
1196 psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT;
1197 size_t part_len;
1198
1199 status = psa_cipher_encrypt_setup( &cipher_op,
1200 transform->psa_key_enc, transform->psa_alg );
1201
1202 if( status != PSA_SUCCESS )
1203 return( psa_ssl_status_to_mbedtls( status ) );
1204
1205 status = psa_cipher_set_iv( &cipher_op, iv, iv_len );
1206
1207 if( status != PSA_SUCCESS )
1208 return( psa_ssl_status_to_mbedtls( status ) );
1209
1210 status = psa_cipher_update( &cipher_op,
1211 input, ilen, output, ilen, olen );
1212
1213 if( status != PSA_SUCCESS )
1214 return( psa_ssl_status_to_mbedtls( status ) );
1215
1216 status = psa_cipher_finish( &cipher_op,
1217 output + *olen, ilen - *olen, &part_len );
1218
1219 if( status != PSA_SUCCESS )
1220 return( psa_ssl_status_to_mbedtls( status ) );
1221
1222 *olen += part_len;
1223 return( 0 );
1224}
1225#endif /* MBEDTLS_USE_PSA_CRYPTO */
1226
Hanno Beckera18d1322018-01-03 14:27:32 +00001227static int build_transforms( mbedtls_ssl_transform *t_in,
1228 mbedtls_ssl_transform *t_out,
1229 int cipher_type, int hash_id,
Hanno Beckerd856c822019-04-29 17:30:59 +01001230 int etm, int tag_mode, int ver,
1231 size_t cid0_len,
1232 size_t cid1_len )
Hanno Beckera18d1322018-01-03 14:27:32 +00001233{
1234 mbedtls_cipher_info_t const *cipher_info;
Hanno Beckera5780f12019-04-05 09:55:37 +01001235 int ret = 0;
Hanno Beckera18d1322018-01-03 14:27:32 +00001236
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01001237#if defined(MBEDTLS_USE_PSA_CRYPTO)
1238 psa_key_type_t key_type;
1239 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1240 psa_algorithm_t alg;
1241 size_t key_bits;
1242 psa_status_t status;
1243#endif
1244
Hanno Beckera18d1322018-01-03 14:27:32 +00001245 size_t keylen, maclen, ivlen;
Hanno Becker81e16a32019-03-01 11:21:44 +00001246 unsigned char *key0 = NULL, *key1 = NULL;
Paul Elliott6f1eda72020-06-11 20:22:00 +01001247 unsigned char *md0 = NULL, *md1 = NULL;
Hanno Beckera18d1322018-01-03 14:27:32 +00001248 unsigned char iv_enc[16], iv_dec[16];
1249
Hanno Beckera0e20d02019-05-15 14:03:01 +01001250#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd856c822019-04-29 17:30:59 +01001251 unsigned char cid0[ SSL_CID_LEN_MIN ];
1252 unsigned char cid1[ SSL_CID_LEN_MIN ];
1253
Ronald Cron351f0ee2020-06-10 12:12:18 +02001254 mbedtls_test_rnd_std_rand( NULL, cid0, sizeof( cid0 ) );
1255 mbedtls_test_rnd_std_rand( NULL, cid1, sizeof( cid1 ) );
Hanno Becker43c24b82019-05-01 09:45:57 +01001256#else
1257 ((void) cid0_len);
1258 ((void) cid1_len);
Hanno Beckera0e20d02019-05-15 14:03:01 +01001259#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerd856c822019-04-29 17:30:59 +01001260
Hanno Beckera18d1322018-01-03 14:27:32 +00001261 maclen = 0;
1262
1263 /* Pick cipher */
1264 cipher_info = mbedtls_cipher_info_from_type( cipher_type );
1265 CHK( cipher_info != NULL );
1266 CHK( cipher_info->iv_size <= 16 );
1267 CHK( cipher_info->key_bitlen % 8 == 0 );
1268
1269 /* Pick keys */
1270 keylen = cipher_info->key_bitlen / 8;
Hanno Becker78d1f702019-04-05 09:56:10 +01001271 /* Allocate `keylen + 1` bytes to ensure that we get
1272 * a non-NULL pointers from `mbedtls_calloc` even if
1273 * `keylen == 0` in the case of the NULL cipher. */
1274 CHK( ( key0 = mbedtls_calloc( 1, keylen + 1 ) ) != NULL );
1275 CHK( ( key1 = mbedtls_calloc( 1, keylen + 1 ) ) != NULL );
Hanno Beckera18d1322018-01-03 14:27:32 +00001276 memset( key0, 0x1, keylen );
1277 memset( key1, 0x2, keylen );
1278
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01001279#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckera18d1322018-01-03 14:27:32 +00001280 /* Setup cipher contexts */
1281 CHK( mbedtls_cipher_setup( &t_in->cipher_ctx_enc, cipher_info ) == 0 );
1282 CHK( mbedtls_cipher_setup( &t_in->cipher_ctx_dec, cipher_info ) == 0 );
1283 CHK( mbedtls_cipher_setup( &t_out->cipher_ctx_enc, cipher_info ) == 0 );
1284 CHK( mbedtls_cipher_setup( &t_out->cipher_ctx_dec, cipher_info ) == 0 );
1285
1286#if defined(MBEDTLS_CIPHER_MODE_CBC)
1287 if( cipher_info->mode == MBEDTLS_MODE_CBC )
1288 {
1289 CHK( mbedtls_cipher_set_padding_mode( &t_in->cipher_ctx_enc,
1290 MBEDTLS_PADDING_NONE ) == 0 );
1291 CHK( mbedtls_cipher_set_padding_mode( &t_in->cipher_ctx_dec,
1292 MBEDTLS_PADDING_NONE ) == 0 );
1293 CHK( mbedtls_cipher_set_padding_mode( &t_out->cipher_ctx_enc,
1294 MBEDTLS_PADDING_NONE ) == 0 );
1295 CHK( mbedtls_cipher_set_padding_mode( &t_out->cipher_ctx_dec,
1296 MBEDTLS_PADDING_NONE ) == 0 );
1297 }
1298#endif /* MBEDTLS_CIPHER_MODE_CBC */
1299
1300 CHK( mbedtls_cipher_setkey( &t_in->cipher_ctx_enc, key0,
1301 keylen << 3, MBEDTLS_ENCRYPT ) == 0 );
1302 CHK( mbedtls_cipher_setkey( &t_in->cipher_ctx_dec, key1,
1303 keylen << 3, MBEDTLS_DECRYPT ) == 0 );
1304 CHK( mbedtls_cipher_setkey( &t_out->cipher_ctx_enc, key1,
1305 keylen << 3, MBEDTLS_ENCRYPT ) == 0 );
1306 CHK( mbedtls_cipher_setkey( &t_out->cipher_ctx_dec, key0,
1307 keylen << 3, MBEDTLS_DECRYPT ) == 0 );
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01001308#endif
Hanno Beckera18d1322018-01-03 14:27:32 +00001309
1310 /* Setup MAC contexts */
Hanno Beckerfd86ca82020-11-30 08:54:23 +00001311#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Hanno Beckera18d1322018-01-03 14:27:32 +00001312 if( cipher_info->mode == MBEDTLS_MODE_CBC ||
1313 cipher_info->mode == MBEDTLS_MODE_STREAM )
1314 {
1315 mbedtls_md_info_t const *md_info;
Hanno Beckera18d1322018-01-03 14:27:32 +00001316
1317 /* Pick hash */
1318 md_info = mbedtls_md_info_from_type( hash_id );
1319 CHK( md_info != NULL );
1320
1321 /* Pick hash keys */
1322 maclen = mbedtls_md_get_size( md_info );
Hanno Becker3ee54212019-04-04 16:31:26 +01001323 CHK( ( md0 = mbedtls_calloc( 1, maclen ) ) != NULL );
1324 CHK( ( md1 = mbedtls_calloc( 1, maclen ) ) != NULL );
Hanno Beckera18d1322018-01-03 14:27:32 +00001325 memset( md0, 0x5, maclen );
1326 memset( md1, 0x6, maclen );
1327
1328 CHK( mbedtls_md_setup( &t_out->md_ctx_enc, md_info, 1 ) == 0 );
1329 CHK( mbedtls_md_setup( &t_out->md_ctx_dec, md_info, 1 ) == 0 );
1330 CHK( mbedtls_md_setup( &t_in->md_ctx_enc, md_info, 1 ) == 0 );
1331 CHK( mbedtls_md_setup( &t_in->md_ctx_dec, md_info, 1 ) == 0 );
1332
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001333 CHK( mbedtls_md_hmac_starts( &t_in->md_ctx_enc,
1334 md0, maclen ) == 0 );
1335 CHK( mbedtls_md_hmac_starts( &t_in->md_ctx_dec,
1336 md1, maclen ) == 0 );
1337 CHK( mbedtls_md_hmac_starts( &t_out->md_ctx_enc,
1338 md1, maclen ) == 0 );
1339 CHK( mbedtls_md_hmac_starts( &t_out->md_ctx_dec,
1340 md0, maclen ) == 0 );
Hanno Beckera18d1322018-01-03 14:27:32 +00001341 }
1342#else
1343 ((void) hash_id);
Hanno Beckerfd86ca82020-11-30 08:54:23 +00001344#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Hanno Beckera18d1322018-01-03 14:27:32 +00001345
1346
1347 /* Pick IV's (regardless of whether they
1348 * are being used by the transform). */
1349 ivlen = cipher_info->iv_size;
1350 memset( iv_enc, 0x3, sizeof( iv_enc ) );
1351 memset( iv_dec, 0x4, sizeof( iv_dec ) );
1352
1353 /*
1354 * Setup transforms
1355 */
1356
Jaeden Amero2de07f12019-06-05 13:32:08 +01001357#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
Hanno Beckerfd86ca82020-11-30 08:54:23 +00001358 defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Hanno Beckera18d1322018-01-03 14:27:32 +00001359 t_out->encrypt_then_mac = etm;
1360 t_in->encrypt_then_mac = etm;
1361#else
1362 ((void) etm);
1363#endif
1364
1365 t_out->minor_ver = ver;
1366 t_in->minor_ver = ver;
1367 t_out->ivlen = ivlen;
1368 t_in->ivlen = ivlen;
1369
1370 switch( cipher_info->mode )
1371 {
1372 case MBEDTLS_MODE_GCM:
1373 case MBEDTLS_MODE_CCM:
Ronald Cron6f135e12021-12-08 16:57:54 +01001374#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Beckere6832872020-05-28 08:29:58 +01001375 if( ver == MBEDTLS_SSL_MINOR_VERSION_4 )
1376 {
1377 t_out->fixed_ivlen = 12;
1378 t_in->fixed_ivlen = 12;
1379 }
1380 else
Ronald Cron6f135e12021-12-08 16:57:54 +01001381#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckere6832872020-05-28 08:29:58 +01001382 {
1383 t_out->fixed_ivlen = 4;
1384 t_in->fixed_ivlen = 4;
1385 }
Hanno Beckera18d1322018-01-03 14:27:32 +00001386 t_out->maclen = 0;
1387 t_in->maclen = 0;
1388 switch( tag_mode )
1389 {
1390 case 0: /* Full tag */
1391 t_out->taglen = 16;
1392 t_in->taglen = 16;
1393 break;
1394 case 1: /* Partial tag */
1395 t_out->taglen = 8;
1396 t_in->taglen = 8;
1397 break;
1398 default:
Paul Elliottc7b53742021-02-03 13:18:33 +00001399 ret = 1;
1400 goto cleanup;
Hanno Beckera18d1322018-01-03 14:27:32 +00001401 }
1402 break;
1403
1404 case MBEDTLS_MODE_CHACHAPOLY:
1405 t_out->fixed_ivlen = 12;
1406 t_in->fixed_ivlen = 12;
1407 t_out->maclen = 0;
1408 t_in->maclen = 0;
1409 switch( tag_mode )
1410 {
1411 case 0: /* Full tag */
1412 t_out->taglen = 16;
1413 t_in->taglen = 16;
1414 break;
1415 case 1: /* Partial tag */
1416 t_out->taglen = 8;
1417 t_in->taglen = 8;
1418 break;
1419 default:
Paul Elliottc7b53742021-02-03 13:18:33 +00001420 ret = 1;
1421 goto cleanup;
Hanno Beckera18d1322018-01-03 14:27:32 +00001422 }
1423 break;
1424
1425 case MBEDTLS_MODE_STREAM:
1426 case MBEDTLS_MODE_CBC:
1427 t_out->fixed_ivlen = 0; /* redundant, must be 0 */
1428 t_in->fixed_ivlen = 0; /* redundant, must be 0 */
1429 t_out->taglen = 0;
1430 t_in->taglen = 0;
1431 switch( tag_mode )
1432 {
1433 case 0: /* Full tag */
1434 t_out->maclen = maclen;
1435 t_in->maclen = maclen;
1436 break;
1437 case 1: /* Partial tag */
1438 t_out->maclen = 10;
1439 t_in->maclen = 10;
1440 break;
1441 default:
Paul Elliottc7b53742021-02-03 13:18:33 +00001442 ret = 1;
1443 goto cleanup;
Hanno Beckera18d1322018-01-03 14:27:32 +00001444 }
1445 break;
1446 default:
Paul Elliottc7b53742021-02-03 13:18:33 +00001447 ret = 1;
1448 goto cleanup;
Hanno Beckera18d1322018-01-03 14:27:32 +00001449 break;
1450 }
1451
1452 /* Setup IV's */
1453
1454 memcpy( &t_in->iv_dec, iv_dec, sizeof( iv_dec ) );
1455 memcpy( &t_in->iv_enc, iv_enc, sizeof( iv_enc ) );
1456 memcpy( &t_out->iv_dec, iv_enc, sizeof( iv_enc ) );
1457 memcpy( &t_out->iv_enc, iv_dec, sizeof( iv_dec ) );
1458
Hanno Beckera0e20d02019-05-15 14:03:01 +01001459#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd856c822019-04-29 17:30:59 +01001460 /* Add CID */
1461 memcpy( &t_in->in_cid, cid0, cid0_len );
1462 memcpy( &t_in->out_cid, cid1, cid1_len );
1463 t_in->in_cid_len = cid0_len;
1464 t_in->out_cid_len = cid1_len;
1465 memcpy( &t_out->in_cid, cid1, cid1_len );
1466 memcpy( &t_out->out_cid, cid0, cid0_len );
1467 t_out->in_cid_len = cid1_len;
1468 t_out->out_cid_len = cid0_len;
Hanno Beckera0e20d02019-05-15 14:03:01 +01001469#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerd856c822019-04-29 17:30:59 +01001470
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01001471#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemyslaw Stekielf57b4562022-01-25 00:04:18 +01001472 status = mbedtls_ssl_cipher_to_psa( cipher_type,
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01001473 t_in->taglen,
1474 &alg,
1475 &key_type,
1476 &key_bits );
1477
1478 if ( status != PSA_SUCCESS)
1479 {
Przemyslaw Stekiel77aec8d2022-01-31 20:22:53 +01001480 ret = psa_ssl_status_to_mbedtls( status );
Przemyslaw Stekielf4facef2022-02-02 21:31:04 +01001481 mbedtls_fprintf( stderr, "mbedtls_ssl_cipher_to_psa: %d\n", (int)status);
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01001482 goto cleanup;
1483 }
1484
1485 t_in->psa_alg = alg;
1486 t_out->psa_alg = alg;
1487
1488 if ( alg != MBEDTLS_SSL_NULL_CIPHER )
1489 {
Przemyslaw Stekielf4ca3f02022-01-25 00:25:59 +01001490 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01001491 psa_set_key_algorithm( &attributes, alg );
1492 psa_set_key_type( &attributes, key_type );
1493
1494 status = psa_import_key( &attributes,
1495 key0,
1496 PSA_BITS_TO_BYTES( key_bits ),
1497 &t_in->psa_key_enc );
1498
1499 if ( status != PSA_SUCCESS)
1500 {
Przemyslaw Stekiel77aec8d2022-01-31 20:22:53 +01001501 ret = psa_ssl_status_to_mbedtls( status );
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01001502 goto cleanup;
1503 }
1504
1505 status = psa_import_key( &attributes,
1506 key1,
1507 PSA_BITS_TO_BYTES( key_bits ),
Przemyslaw Stekielf4ca3f02022-01-25 00:25:59 +01001508 &t_out->psa_key_enc );
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01001509
1510 if ( status != PSA_SUCCESS)
1511 {
Przemyslaw Stekiel77aec8d2022-01-31 20:22:53 +01001512 ret = psa_ssl_status_to_mbedtls( status );
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01001513 goto cleanup;
1514 }
1515
Przemyslaw Stekielf4ca3f02022-01-25 00:25:59 +01001516 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
1517
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01001518 status = psa_import_key( &attributes,
1519 key1,
1520 PSA_BITS_TO_BYTES( key_bits ),
Przemyslaw Stekielf4ca3f02022-01-25 00:25:59 +01001521 &t_in->psa_key_dec );
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01001522
1523 if ( status != PSA_SUCCESS)
1524 {
Przemyslaw Stekiel77aec8d2022-01-31 20:22:53 +01001525 ret = psa_ssl_status_to_mbedtls( status );
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01001526 goto cleanup;
1527 }
1528
1529 status = psa_import_key( &attributes,
1530 key0,
1531 PSA_BITS_TO_BYTES( key_bits ),
1532 &t_out->psa_key_dec );
1533
1534 if ( status != PSA_SUCCESS)
1535 {
Przemyslaw Stekiel77aec8d2022-01-31 20:22:53 +01001536 ret = psa_ssl_status_to_mbedtls( status );
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01001537 goto cleanup;
1538 }
1539 }
1540#endif /* MBEDTLS_USE_PSA_CRYPTO */
1541
Hanno Becker81e16a32019-03-01 11:21:44 +00001542cleanup:
1543
Hanno Becker3ee54212019-04-04 16:31:26 +01001544 mbedtls_free( key0 );
1545 mbedtls_free( key1 );
Hanno Becker81e16a32019-03-01 11:21:44 +00001546
Paul Elliott6f1eda72020-06-11 20:22:00 +01001547 mbedtls_free( md0 );
1548 mbedtls_free( md1 );
1549
Hanno Beckera5780f12019-04-05 09:55:37 +01001550 return( ret );
Hanno Beckera18d1322018-01-03 14:27:32 +00001551}
1552
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001553/*
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02001554 * Populate a session structure for serialization tests.
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001555 * Choose dummy values, mostly non-0 to distinguish from the init default.
1556 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01001557static int ssl_populate_session_tls12( mbedtls_ssl_session *session,
1558 int ticket_len,
1559 const char *crt_file )
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001560{
1561#if defined(MBEDTLS_HAVE_TIME)
1562 session->start = mbedtls_time( NULL ) - 42;
1563#endif
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01001564 session->minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001565 session->ciphersuite = 0xabcd;
1566 session->compression = 1;
1567 session->id_len = sizeof( session->id );
1568 memset( session->id, 66, session->id_len );
Manuel Pégourié-Gonnard220403b2019-05-24 09:54:21 +02001569 memset( session->master, 17, sizeof( session->master ) );
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001570
Manuel Pégourié-Gonnard1f6033a2019-05-24 10:17:52 +02001571#if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_FS_IO)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01001572 if( crt_file != NULL && strlen( crt_file ) != 0 )
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001573 {
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02001574 mbedtls_x509_crt tmp_crt;
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001575 int ret;
Manuel Pégourié-Gonnard6b840702019-05-24 09:40:17 +02001576
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02001577 mbedtls_x509_crt_init( &tmp_crt );
1578 ret = mbedtls_x509_crt_parse_file( &tmp_crt, crt_file );
1579 if( ret != 0 )
1580 return( ret );
1581
1582#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1583 /* Move temporary CRT. */
Manuel Pégourié-Gonnard6b840702019-05-24 09:40:17 +02001584 session->peer_cert = mbedtls_calloc( 1, sizeof( *session->peer_cert ) );
1585 if( session->peer_cert == NULL )
1586 return( -1 );
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02001587 *session->peer_cert = tmp_crt;
1588 memset( &tmp_crt, 0, sizeof( tmp_crt ) );
1589#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
1590 /* Calculate digest of temporary CRT. */
1591 session->peer_cert_digest =
1592 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
1593 if( session->peer_cert_digest == NULL )
1594 return( -1 );
1595 ret = mbedtls_md( mbedtls_md_info_from_type(
1596 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
1597 tmp_crt.raw.p, tmp_crt.raw.len,
1598 session->peer_cert_digest );
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001599 if( ret != 0 )
1600 return( ret );
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02001601 session->peer_cert_digest_type =
1602 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
1603 session->peer_cert_digest_len =
1604 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
1605#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
1606
1607 mbedtls_x509_crt_free( &tmp_crt );
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001608 }
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02001609#else /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_FS_IO */
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001610 (void) crt_file;
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02001611#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_FS_IO */
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001612 session->verify_result = 0xdeadbeef;
1613
1614#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
1615 if( ticket_len != 0 )
1616 {
1617 session->ticket = mbedtls_calloc( 1, ticket_len );
Manuel Pégourié-Gonnard220403b2019-05-24 09:54:21 +02001618 if( session->ticket == NULL )
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001619 return( -1 );
1620 memset( session->ticket, 33, ticket_len );
1621 }
1622 session->ticket_len = ticket_len;
1623 session->ticket_lifetime = 86401;
1624#else
1625 (void) ticket_len;
1626#endif
1627
1628#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1629 session->mfl_code = 1;
1630#endif
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001631#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1632 session->encrypt_then_mac = 1;
1633#endif
1634
1635 return( 0 );
1636}
1637
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001638/*
1639 * Perform data exchanging between \p ssl_1 and \p ssl_2 and check if the
1640 * message was sent in the correct number of fragments.
1641 *
1642 * /p ssl_1 and /p ssl_2 Endpoints represented by mbedtls_ssl_context. Both
1643 * of them must be initialized and connected beforehand.
1644 * /p msg_len_1 and /p msg_len_2 specify the size of the message to send.
1645 * /p expected_fragments_1 and /p expected_fragments_2 determine in how many
1646 * fragments the message should be sent.
1647 * expected_fragments is 0: can be used for DTLS testing while the message
1648 * size is larger than MFL. In that case the message
1649 * cannot be fragmented and sent to the second endpoint.
1650 * This value can be used for negative tests.
1651 * expected_fragments is 1: can be used for TLS/DTLS testing while the
1652 * message size is below MFL
1653 * expected_fragments > 1: can be used for TLS testing while the message
1654 * size is larger than MFL
1655 *
1656 * \retval 0 on success, otherwise error code.
1657 */
1658int mbedtls_exchange_data( mbedtls_ssl_context *ssl_1,
1659 int msg_len_1, const int expected_fragments_1,
1660 mbedtls_ssl_context *ssl_2,
1661 int msg_len_2, const int expected_fragments_2 )
1662{
1663 unsigned char *msg_buf_1 = malloc( msg_len_1 );
1664 unsigned char *msg_buf_2 = malloc( msg_len_2 );
1665 unsigned char *in_buf_1 = malloc( msg_len_2 );
1666 unsigned char *in_buf_2 = malloc( msg_len_1 );
1667 int msg_type, ret = -1;
1668
1669 /* Perform this test with two message types. At first use a message
1670 * consisting of only 0x00 for the client and only 0xFF for the server.
1671 * At the second time use message with generated data */
1672 for( msg_type = 0; msg_type < 2; msg_type++ )
1673 {
1674 int written_1 = 0;
1675 int written_2 = 0;
1676 int read_1 = 0;
1677 int read_2 = 0;
1678 int fragments_1 = 0;
1679 int fragments_2 = 0;
1680
1681 if( msg_type == 0 )
1682 {
1683 memset( msg_buf_1, 0x00, msg_len_1 );
1684 memset( msg_buf_2, 0xff, msg_len_2 );
1685 }
1686 else
1687 {
1688 int i, j = 0;
1689 for( i = 0; i < msg_len_1; i++ )
1690 {
1691 msg_buf_1[i] = j++ & 0xFF;
1692 }
1693 for( i = 0; i < msg_len_2; i++ )
1694 {
1695 msg_buf_2[i] = ( j -= 5 ) & 0xFF;
1696 }
1697 }
1698
1699 while( read_1 < msg_len_2 || read_2 < msg_len_1 )
1700 {
1701 /* ssl_1 sending */
1702 if( msg_len_1 > written_1 )
1703 {
1704 ret = mbedtls_ssl_write_fragment( ssl_1, msg_buf_1,
1705 msg_len_1, &written_1,
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001706 expected_fragments_1 );
1707 if( expected_fragments_1 == 0 )
1708 {
1709 /* This error is expected when the message is too large and
1710 * cannot be fragmented */
1711 TEST_ASSERT( ret == MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1712 msg_len_1 = 0;
1713 }
1714 else
1715 {
1716 TEST_ASSERT( ret == 0 );
1717 }
1718 }
1719
1720 /* ssl_2 sending */
1721 if( msg_len_2 > written_2 )
1722 {
1723 ret = mbedtls_ssl_write_fragment( ssl_2, msg_buf_2,
1724 msg_len_2, &written_2,
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001725 expected_fragments_2 );
1726 if( expected_fragments_2 == 0 )
1727 {
1728 /* This error is expected when the message is too large and
1729 * cannot be fragmented */
1730 TEST_ASSERT( ret == MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1731 msg_len_2 = 0;
1732 }
1733 else
1734 {
1735 TEST_ASSERT( ret == 0 );
1736 }
1737 }
1738
1739 /* ssl_1 reading */
1740 if( read_1 < msg_len_2 )
1741 {
1742 ret = mbedtls_ssl_read_fragment( ssl_1, in_buf_1,
1743 msg_len_2, &read_1,
Piotr Nowicki438bf3b2020-03-10 12:59:10 +01001744 &fragments_2,
1745 expected_fragments_2 );
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001746 TEST_ASSERT( ret == 0 );
1747 }
1748
1749 /* ssl_2 reading */
1750 if( read_2 < msg_len_1 )
1751 {
1752 ret = mbedtls_ssl_read_fragment( ssl_2, in_buf_2,
1753 msg_len_1, &read_2,
Piotr Nowicki438bf3b2020-03-10 12:59:10 +01001754 &fragments_1,
1755 expected_fragments_1 );
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001756 TEST_ASSERT( ret == 0 );
1757 }
1758 }
1759
1760 ret = -1;
1761 TEST_ASSERT( 0 == memcmp( msg_buf_1, in_buf_2, msg_len_1 ) );
1762 TEST_ASSERT( 0 == memcmp( msg_buf_2, in_buf_1, msg_len_2 ) );
1763 TEST_ASSERT( fragments_1 == expected_fragments_1 );
1764 TEST_ASSERT( fragments_2 == expected_fragments_2 );
1765 }
1766
1767 ret = 0;
1768
1769exit:
1770 free( msg_buf_1 );
1771 free( in_buf_1 );
1772 free( msg_buf_2 );
1773 free( in_buf_2 );
1774
1775 return ret;
1776}
1777
Piotr Nowicki95e9eb82020-02-14 11:33:34 +01001778/*
1779 * Perform data exchanging between \p ssl_1 and \p ssl_2. Both of endpoints
1780 * must be initialized and connected beforehand.
1781 *
1782 * \retval 0 on success, otherwise error code.
1783 */
1784int exchange_data( mbedtls_ssl_context *ssl_1,
1785 mbedtls_ssl_context *ssl_2 )
1786{
1787 return mbedtls_exchange_data( ssl_1, 256, 1,
1788 ssl_2, 256, 1 );
1789}
1790
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02001791#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
1792 defined(MBEDTLS_ENTROPY_C) && \
1793 defined(MBEDTLS_CTR_DRBG_C)
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001794void perform_handshake( handshake_test_options* options )
1795{
1796 /* forced_ciphersuite needs to last until the end of the handshake */
1797 int forced_ciphersuite[2];
1798 enum { BUFFSIZE = 17000 };
1799 mbedtls_endpoint client, server;
Gilles Peskineeccd8882020-03-10 12:19:08 +01001800#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001801 const char *psk_identity = "foo";
1802#endif
1803#if defined(MBEDTLS_TIMING_C)
1804 mbedtls_timing_delay_context timer_client, timer_server;
1805#endif
1806#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1807 unsigned char *context_buf = NULL;
1808 size_t context_buf_len;
1809#endif
1810#if defined(MBEDTLS_SSL_RENEGOTIATION)
1811 int ret = -1;
1812#endif
Paul Elliottc8570442020-04-15 17:00:50 +01001813 int expected_handshake_result = 0;
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001814
1815 mbedtls_test_message_queue server_queue, client_queue;
1816 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05001817 mbedtls_message_socket_init( &server_context );
1818 mbedtls_message_socket_init( &client_context );
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001819
1820 /* Client side */
1821 if( options->dtls != 0 )
1822 {
1823 TEST_ASSERT( mbedtls_endpoint_init( &client, MBEDTLS_SSL_IS_CLIENT,
1824 options->pk_alg, &client_context,
1825 &client_queue,
1826 &server_queue ) == 0 );
1827#if defined(MBEDTLS_TIMING_C)
1828 mbedtls_ssl_set_timer_cb( &client.ssl, &timer_client,
1829 mbedtls_timing_set_delay,
1830 mbedtls_timing_get_delay );
1831#endif
1832 }
1833 else
1834 {
1835 TEST_ASSERT( mbedtls_endpoint_init( &client, MBEDTLS_SSL_IS_CLIENT,
1836 options->pk_alg, NULL, NULL,
1837 NULL ) == 0 );
1838 }
Paul Elliottc8570442020-04-15 17:00:50 +01001839
1840 if( options->client_min_version != TEST_SSL_MINOR_VERSION_NONE )
1841 {
1842 mbedtls_ssl_conf_min_version( &client.conf, MBEDTLS_SSL_MAJOR_VERSION_3,
1843 options->client_min_version );
1844 }
1845
1846 if( options->client_max_version != TEST_SSL_MINOR_VERSION_NONE )
1847 {
1848 mbedtls_ssl_conf_max_version( &client.conf, MBEDTLS_SSL_MAJOR_VERSION_3,
1849 options->client_max_version );
1850 }
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001851
1852 if( strlen( options->cipher ) > 0 )
1853 {
1854 set_ciphersuite( &client.conf, options->cipher, forced_ciphersuite );
1855 }
Piotr Nowickibde7ee82020-02-21 10:59:50 +01001856
1857#if defined (MBEDTLS_DEBUG_C)
1858 if( options->cli_log_fun )
1859 {
1860 mbedtls_debug_set_threshold( 4 );
1861 mbedtls_ssl_conf_dbg( &client.conf, options->cli_log_fun,
1862 options->cli_log_obj );
1863 }
1864#endif
1865
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001866 /* Server side */
1867 if( options->dtls != 0 )
1868 {
1869 TEST_ASSERT( mbedtls_endpoint_init( &server, MBEDTLS_SSL_IS_SERVER,
1870 options->pk_alg, &server_context,
1871 &server_queue,
1872 &client_queue) == 0 );
1873#if defined(MBEDTLS_TIMING_C)
1874 mbedtls_ssl_set_timer_cb( &server.ssl, &timer_server,
1875 mbedtls_timing_set_delay,
1876 mbedtls_timing_get_delay );
1877#endif
1878 }
1879 else
1880 {
1881 TEST_ASSERT( mbedtls_endpoint_init( &server, MBEDTLS_SSL_IS_SERVER,
1882 options->pk_alg, NULL, NULL, NULL ) == 0 );
1883 }
Piotr Nowickibde7ee82020-02-21 10:59:50 +01001884
1885 mbedtls_ssl_conf_authmode( &server.conf, options->srv_auth_mode );
1886
Paul Elliottc8570442020-04-15 17:00:50 +01001887 if( options->server_min_version != TEST_SSL_MINOR_VERSION_NONE )
1888 {
1889 mbedtls_ssl_conf_min_version( &server.conf, MBEDTLS_SSL_MAJOR_VERSION_3,
1890 options->server_min_version );
1891 }
1892
1893 if( options->server_max_version != TEST_SSL_MINOR_VERSION_NONE )
1894 {
1895 mbedtls_ssl_conf_max_version( &server.conf, MBEDTLS_SSL_MAJOR_VERSION_3,
1896 options->server_max_version );
1897 }
1898
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001899#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1900 TEST_ASSERT( mbedtls_ssl_conf_max_frag_len( &(server.conf),
1901 (unsigned char) options->mfl ) == 0 );
1902 TEST_ASSERT( mbedtls_ssl_conf_max_frag_len( &(client.conf),
1903 (unsigned char) options->mfl ) == 0 );
1904#else
1905 TEST_ASSERT( MBEDTLS_SSL_MAX_FRAG_LEN_NONE == options->mfl );
1906#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
1907
Gilles Peskineeccd8882020-03-10 12:19:08 +01001908#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001909 if( options->psk_str != NULL && options->psk_str->len > 0 )
1910 {
1911 TEST_ASSERT( mbedtls_ssl_conf_psk( &client.conf, options->psk_str->x,
1912 options->psk_str->len,
1913 (const unsigned char *) psk_identity,
1914 strlen( psk_identity ) ) == 0 );
1915
1916 TEST_ASSERT( mbedtls_ssl_conf_psk( &server.conf, options->psk_str->x,
1917 options->psk_str->len,
1918 (const unsigned char *) psk_identity,
1919 strlen( psk_identity ) ) == 0 );
1920
1921 mbedtls_ssl_conf_psk_cb( &server.conf, psk_dummy_callback, NULL );
1922 }
1923#endif
1924#if defined(MBEDTLS_SSL_RENEGOTIATION)
1925 if( options->renegotiate )
1926 {
1927 mbedtls_ssl_conf_renegotiation( &(server.conf),
1928 MBEDTLS_SSL_RENEGOTIATION_ENABLED );
1929 mbedtls_ssl_conf_renegotiation( &(client.conf),
1930 MBEDTLS_SSL_RENEGOTIATION_ENABLED );
1931
1932 mbedtls_ssl_conf_legacy_renegotiation( &(server.conf),
1933 options->legacy_renegotiation );
1934 mbedtls_ssl_conf_legacy_renegotiation( &(client.conf),
1935 options->legacy_renegotiation );
1936 }
1937#endif /* MBEDTLS_SSL_RENEGOTIATION */
1938
Piotr Nowickibde7ee82020-02-21 10:59:50 +01001939#if defined (MBEDTLS_DEBUG_C)
1940 if( options->srv_log_fun )
1941 {
1942 mbedtls_debug_set_threshold( 4 );
1943 mbedtls_ssl_conf_dbg( &server.conf, options->srv_log_fun,
1944 options->srv_log_obj );
1945 }
1946#endif
1947
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001948 TEST_ASSERT( mbedtls_mock_socket_connect( &(client.socket),
1949 &(server.socket),
1950 BUFFSIZE ) == 0 );
1951
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001952#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1953 if( options->resize_buffers != 0 )
1954 {
1955 /* Ensure that the buffer sizes are appropriate before resizes */
1956 TEST_ASSERT( client.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN );
1957 TEST_ASSERT( client.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN );
1958 TEST_ASSERT( server.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN );
1959 TEST_ASSERT( server.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN );
1960 }
1961#endif
1962
Paul Elliottc8570442020-04-15 17:00:50 +01001963 if( options->expected_negotiated_version == TEST_SSL_MINOR_VERSION_NONE )
1964 {
Hanno Beckerbc000442021-06-24 09:18:19 +01001965 expected_handshake_result = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
Paul Elliottc8570442020-04-15 17:00:50 +01001966 }
1967
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001968 TEST_ASSERT( mbedtls_move_handshake_to_state( &(client.ssl),
1969 &(server.ssl),
1970 MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Elliottc8570442020-04-15 17:00:50 +01001971 == expected_handshake_result );
1972
1973 if( expected_handshake_result != 0 )
1974 {
1975 /* Connection will have failed by this point, skip to cleanup */
1976 goto exit;
1977 }
1978
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001979 TEST_ASSERT( client.ssl.state == MBEDTLS_SSL_HANDSHAKE_OVER );
1980 TEST_ASSERT( server.ssl.state == MBEDTLS_SSL_HANDSHAKE_OVER );
1981
Paul Elliottc8570442020-04-15 17:00:50 +01001982 /* Check that we agree on the version... */
1983 TEST_ASSERT( client.ssl.minor_ver == server.ssl.minor_ver );
1984
1985 /* And check that the version negotiated is the expected one. */
1986 TEST_EQUAL( client.ssl.minor_ver, options->expected_negotiated_version );
1987
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001988#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1989 if( options->resize_buffers != 0 )
1990 {
TRodziewicz2abf03c2021-06-25 14:40:09 +02001991 /* A server, when using DTLS, might delay a buffer resize to happen
1992 * after it receives a message, so we force it. */
1993 TEST_ASSERT( exchange_data( &(client.ssl), &(server.ssl) ) == 0 );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001994
TRodziewicz2abf03c2021-06-25 14:40:09 +02001995 TEST_ASSERT( client.ssl.out_buf_len ==
1996 mbedtls_ssl_get_output_buflen( &client.ssl ) );
1997 TEST_ASSERT( client.ssl.in_buf_len ==
1998 mbedtls_ssl_get_input_buflen( &client.ssl ) );
1999 TEST_ASSERT( server.ssl.out_buf_len ==
2000 mbedtls_ssl_get_output_buflen( &server.ssl ) );
2001 TEST_ASSERT( server.ssl.in_buf_len ==
2002 mbedtls_ssl_get_input_buflen( &server.ssl ) );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05002003 }
2004#endif
2005
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002006 if( options->cli_msg_len != 0 || options->srv_msg_len != 0 )
2007 {
2008 /* Start data exchanging test */
2009 TEST_ASSERT( mbedtls_exchange_data( &(client.ssl), options->cli_msg_len,
2010 options->expected_cli_fragments,
2011 &(server.ssl), options->srv_msg_len,
2012 options->expected_srv_fragments )
2013 == 0 );
2014 }
2015#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
2016 if( options->serialize == 1 )
2017 {
2018 TEST_ASSERT( options->dtls == 1 );
2019
2020 TEST_ASSERT( mbedtls_ssl_context_save( &(server.ssl), NULL,
2021 0, &context_buf_len )
2022 == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2023
2024 context_buf = mbedtls_calloc( 1, context_buf_len );
2025 TEST_ASSERT( context_buf != NULL );
2026
2027 TEST_ASSERT( mbedtls_ssl_context_save( &(server.ssl), context_buf,
2028 context_buf_len,
2029 &context_buf_len ) == 0 );
2030
2031 mbedtls_ssl_free( &(server.ssl) );
2032 mbedtls_ssl_init( &(server.ssl) );
2033
2034 TEST_ASSERT( mbedtls_ssl_setup( &(server.ssl), &(server.conf) ) == 0 );
2035
2036 mbedtls_ssl_set_bio( &( server.ssl ), &server_context,
2037 mbedtls_mock_tcp_send_msg,
2038 mbedtls_mock_tcp_recv_msg,
2039 NULL );
2040
2041#if defined(MBEDTLS_TIMING_C)
2042 mbedtls_ssl_set_timer_cb( &server.ssl, &timer_server,
2043 mbedtls_timing_set_delay,
2044 mbedtls_timing_get_delay );
2045#endif
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05002046#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2047 if( options->resize_buffers != 0 )
2048 {
2049 /* Ensure that the buffer sizes are appropriate before resizes */
2050 TEST_ASSERT( server.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN );
2051 TEST_ASSERT( server.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN );
2052 }
2053#endif
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002054 TEST_ASSERT( mbedtls_ssl_context_load( &( server.ssl ), context_buf,
2055 context_buf_len ) == 0 );
2056
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05002057#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2058 /* Validate buffer sizes after context deserialization */
2059 if( options->resize_buffers != 0 )
2060 {
2061 TEST_ASSERT( server.ssl.out_buf_len ==
2062 mbedtls_ssl_get_output_buflen( &server.ssl ) );
2063 TEST_ASSERT( server.ssl.in_buf_len ==
2064 mbedtls_ssl_get_input_buflen( &server.ssl ) );
2065 }
2066#endif
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002067 /* Retest writing/reading */
2068 if( options->cli_msg_len != 0 || options->srv_msg_len != 0 )
2069 {
2070 TEST_ASSERT( mbedtls_exchange_data( &(client.ssl),
2071 options->cli_msg_len,
2072 options->expected_cli_fragments,
2073 &(server.ssl),
2074 options->srv_msg_len,
2075 options->expected_srv_fragments )
2076 == 0 );
2077 }
2078 }
2079#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05002080
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002081#if defined(MBEDTLS_SSL_RENEGOTIATION)
2082 if( options->renegotiate )
2083 {
2084 /* Start test with renegotiation */
2085 TEST_ASSERT( server.ssl.renego_status ==
2086 MBEDTLS_SSL_INITIAL_HANDSHAKE );
2087 TEST_ASSERT( client.ssl.renego_status ==
2088 MBEDTLS_SSL_INITIAL_HANDSHAKE );
2089
2090 /* After calling this function for the server, it only sends a handshake
2091 * request. All renegotiation should happen during data exchanging */
2092 TEST_ASSERT( mbedtls_ssl_renegotiate( &(server.ssl) ) == 0 );
2093 TEST_ASSERT( server.ssl.renego_status ==
2094 MBEDTLS_SSL_RENEGOTIATION_PENDING );
2095 TEST_ASSERT( client.ssl.renego_status ==
2096 MBEDTLS_SSL_INITIAL_HANDSHAKE );
2097
2098 TEST_ASSERT( exchange_data( &(client.ssl), &(server.ssl) ) == 0 );
2099 TEST_ASSERT( server.ssl.renego_status ==
2100 MBEDTLS_SSL_RENEGOTIATION_DONE );
2101 TEST_ASSERT( client.ssl.renego_status ==
2102 MBEDTLS_SSL_RENEGOTIATION_DONE );
2103
2104 /* After calling mbedtls_ssl_renegotiate for the client all renegotiation
2105 * should happen inside this function. However in this test, we cannot
2106 * perform simultaneous communication betwen client and server so this
2107 * function will return waiting error on the socket. All rest of
2108 * renegotiation should happen during data exchanging */
2109 ret = mbedtls_ssl_renegotiate( &(client.ssl) );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05002110#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2111 if( options->resize_buffers != 0 )
2112 {
2113 /* Ensure that the buffer sizes are appropriate before resizes */
2114 TEST_ASSERT( client.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN );
2115 TEST_ASSERT( client.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN );
2116 }
2117#endif
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002118 TEST_ASSERT( ret == 0 ||
2119 ret == MBEDTLS_ERR_SSL_WANT_READ ||
2120 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
2121 TEST_ASSERT( server.ssl.renego_status ==
2122 MBEDTLS_SSL_RENEGOTIATION_DONE );
2123 TEST_ASSERT( client.ssl.renego_status ==
2124 MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS );
2125
2126 TEST_ASSERT( exchange_data( &(client.ssl), &(server.ssl) ) == 0 );
2127 TEST_ASSERT( server.ssl.renego_status ==
2128 MBEDTLS_SSL_RENEGOTIATION_DONE );
2129 TEST_ASSERT( client.ssl.renego_status ==
2130 MBEDTLS_SSL_RENEGOTIATION_DONE );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05002131#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2132 /* Validate buffer sizes after renegotiation */
2133 if( options->resize_buffers != 0 )
2134 {
2135 TEST_ASSERT( client.ssl.out_buf_len ==
2136 mbedtls_ssl_get_output_buflen( &client.ssl ) );
2137 TEST_ASSERT( client.ssl.in_buf_len ==
2138 mbedtls_ssl_get_input_buflen( &client.ssl ) );
2139 TEST_ASSERT( server.ssl.out_buf_len ==
2140 mbedtls_ssl_get_output_buflen( &server.ssl ) );
2141 TEST_ASSERT( server.ssl.in_buf_len ==
2142 mbedtls_ssl_get_input_buflen( &server.ssl ) );
2143 }
2144#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002145 }
2146#endif /* MBEDTLS_SSL_RENEGOTIATION */
2147
2148exit:
2149 mbedtls_endpoint_free( &client, options->dtls != 0 ? &client_context : NULL );
2150 mbedtls_endpoint_free( &server, options->dtls != 0 ? &server_context : NULL );
Piotr Nowickibde7ee82020-02-21 10:59:50 +01002151#if defined (MBEDTLS_DEBUG_C)
2152 if( options->cli_log_fun || options->srv_log_fun )
2153 {
2154 mbedtls_debug_set_threshold( 0 );
2155 }
2156#endif
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002157#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
2158 if( context_buf != NULL )
2159 mbedtls_free( context_buf );
2160#endif
2161}
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02002162#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002163
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002164/* END_HEADER */
2165
2166/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002167 * depends_on:MBEDTLS_SSL_TLS_C
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002168 * END_DEPENDENCIES
2169 */
2170
Janos Follath6264e662019-11-26 11:11:15 +00002171/* BEGIN_CASE */
2172void test_callback_buffer_sanity()
2173{
2174 enum { MSGLEN = 10 };
2175 mbedtls_test_buffer buf;
2176 unsigned char input[MSGLEN];
2177 unsigned char output[MSGLEN];
2178
2179 memset( input, 0, sizeof(input) );
2180
2181 /* Make sure calling put and get on NULL buffer results in error. */
2182 TEST_ASSERT( mbedtls_test_buffer_put( NULL, input, sizeof( input ) )
2183 == -1 );
2184 TEST_ASSERT( mbedtls_test_buffer_get( NULL, output, sizeof( output ) )
2185 == -1 );
2186 TEST_ASSERT( mbedtls_test_buffer_put( NULL, NULL, sizeof( input ) ) == -1 );
Andrzej Kurekf7774142020-01-22 06:34:59 -05002187
Janos Follath6264e662019-11-26 11:11:15 +00002188 TEST_ASSERT( mbedtls_test_buffer_put( NULL, NULL, 0 ) == -1 );
2189 TEST_ASSERT( mbedtls_test_buffer_get( NULL, NULL, 0 ) == -1 );
2190
2191 /* Make sure calling put and get on a buffer that hasn't been set up results
2192 * in eror. */
2193 mbedtls_test_buffer_init( &buf );
2194
2195 TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, sizeof( input ) ) == -1 );
2196 TEST_ASSERT( mbedtls_test_buffer_get( &buf, output, sizeof( output ) )
2197 == -1 );
2198 TEST_ASSERT( mbedtls_test_buffer_put( &buf, NULL, sizeof( input ) ) == -1 );
Andrzej Kurekf7774142020-01-22 06:34:59 -05002199
Janos Follath6264e662019-11-26 11:11:15 +00002200 TEST_ASSERT( mbedtls_test_buffer_put( &buf, NULL, 0 ) == -1 );
2201 TEST_ASSERT( mbedtls_test_buffer_get( &buf, NULL, 0 ) == -1 );
2202
Andrzej Kurekf7774142020-01-22 06:34:59 -05002203 /* Make sure calling put and get on NULL input only results in
2204 * error if the length is not zero, and that a NULL output is valid for data
2205 * dropping.
2206 */
Janos Follath6264e662019-11-26 11:11:15 +00002207
2208 TEST_ASSERT( mbedtls_test_buffer_setup( &buf, sizeof( input ) ) == 0 );
2209
2210 TEST_ASSERT( mbedtls_test_buffer_put( &buf, NULL, sizeof( input ) ) == -1 );
2211 TEST_ASSERT( mbedtls_test_buffer_get( &buf, NULL, sizeof( output ) )
Andrzej Kurekf7774142020-01-22 06:34:59 -05002212 == 0 );
Janos Follath6264e662019-11-26 11:11:15 +00002213 TEST_ASSERT( mbedtls_test_buffer_put( &buf, NULL, 0 ) == 0 );
2214 TEST_ASSERT( mbedtls_test_buffer_get( &buf, NULL, 0 ) == 0 );
2215
Piotr Nowickifb437d72020-01-13 16:59:12 +01002216 /* Make sure calling put several times in the row is safe */
2217
2218 TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, sizeof( input ) )
2219 == sizeof( input ) );
2220 TEST_ASSERT( mbedtls_test_buffer_get( &buf, output, 2 ) == 2 );
2221 TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, 1 ) == 1 );
2222 TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, 2 ) == 1 );
2223 TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, 2 ) == 0 );
2224
2225
Janos Follath6264e662019-11-26 11:11:15 +00002226exit:
2227
2228 mbedtls_test_buffer_free( &buf );
2229}
2230/* END_CASE */
2231
2232/*
2233 * Test if the implementation of `mbedtls_test_buffer` related functions is
2234 * correct and works as expected.
2235 *
2236 * That is
2237 * - If we try to put in \p put1 bytes then we can put in \p put1_ret bytes.
2238 * - Afterwards if we try to get \p get1 bytes then we can get \get1_ret bytes.
2239 * - Next, if we try to put in \p put1 bytes then we can put in \p put1_ret
2240 * bytes.
2241 * - Afterwards if we try to get \p get1 bytes then we can get \get1_ret bytes.
2242 * - All of the bytes we got match the bytes we put in in a FIFO manner.
2243 */
2244
2245/* BEGIN_CASE */
2246void test_callback_buffer( int size, int put1, int put1_ret,
2247 int get1, int get1_ret, int put2, int put2_ret,
2248 int get2, int get2_ret )
2249{
2250 enum { ROUNDS = 2 };
2251 size_t put[ROUNDS];
2252 int put_ret[ROUNDS];
2253 size_t get[ROUNDS];
2254 int get_ret[ROUNDS];
2255 mbedtls_test_buffer buf;
2256 unsigned char* input = NULL;
2257 size_t input_len;
2258 unsigned char* output = NULL;
2259 size_t output_len;
Janos Follath031827f2019-11-27 11:12:14 +00002260 size_t i, j, written, read;
Janos Follath6264e662019-11-26 11:11:15 +00002261
2262 mbedtls_test_buffer_init( &buf );
2263 TEST_ASSERT( mbedtls_test_buffer_setup( &buf, size ) == 0 );
2264
2265 /* Check the sanity of input parameters and initialise local variables. That
2266 * is, ensure that the amount of data is not negative and that we are not
2267 * expecting more to put or get than we actually asked for. */
2268 TEST_ASSERT( put1 >= 0 );
2269 put[0] = put1;
2270 put_ret[0] = put1_ret;
2271 TEST_ASSERT( put1_ret <= put1 );
2272 TEST_ASSERT( put2 >= 0 );
2273 put[1] = put2;
2274 put_ret[1] = put2_ret;
2275 TEST_ASSERT( put2_ret <= put2 );
2276
2277 TEST_ASSERT( get1 >= 0 );
2278 get[0] = get1;
2279 get_ret[0] = get1_ret;
2280 TEST_ASSERT( get1_ret <= get1 );
2281 TEST_ASSERT( get2 >= 0 );
2282 get[1] = get2;
2283 get_ret[1] = get2_ret;
2284 TEST_ASSERT( get2_ret <= get2 );
2285
2286 input_len = 0;
2287 /* Calculate actual input and output lengths */
2288 for( j = 0; j < ROUNDS; j++ )
2289 {
2290 if( put_ret[j] > 0 )
2291 {
2292 input_len += put_ret[j];
2293 }
2294 }
2295 /* In order to always have a valid pointer we always allocate at least 1
2296 * byte. */
2297 if( input_len == 0 )
2298 input_len = 1;
2299 ASSERT_ALLOC( input, input_len );
2300
2301 output_len = 0;
2302 for( j = 0; j < ROUNDS; j++ )
2303 {
2304 if( get_ret[j] > 0 )
2305 {
2306 output_len += get_ret[j];
2307 }
2308 }
2309 TEST_ASSERT( output_len <= input_len );
2310 /* In order to always have a valid pointer we always allocate at least 1
2311 * byte. */
2312 if( output_len == 0 )
2313 output_len = 1;
2314 ASSERT_ALLOC( output, output_len );
2315
2316 /* Fill up the buffer with structured data so that unwanted changes
2317 * can be detected */
2318 for( i = 0; i < input_len; i++ )
2319 {
2320 input[i] = i & 0xFF;
2321 }
2322
2323 written = read = 0;
2324 for( j = 0; j < ROUNDS; j++ )
2325 {
2326 TEST_ASSERT( put_ret[j] == mbedtls_test_buffer_put( &buf,
2327 input + written, put[j] ) );
2328 written += put_ret[j];
2329 TEST_ASSERT( get_ret[j] == mbedtls_test_buffer_get( &buf,
2330 output + read, get[j] ) );
2331 read += get_ret[j];
2332 TEST_ASSERT( read <= written );
2333 if( get_ret[j] > 0 )
2334 {
2335 TEST_ASSERT( memcmp( output + read - get_ret[j],
2336 input + read - get_ret[j], get_ret[j] )
2337 == 0 );
2338 }
2339 }
2340
2341exit:
2342
2343 mbedtls_free( input );
2344 mbedtls_free( output );
2345 mbedtls_test_buffer_free( &buf );
2346}
2347/* END_CASE */
2348
Janos Follath031827f2019-11-27 11:12:14 +00002349/*
Janos Follathc673c2c2019-12-02 15:47:26 +00002350 * Test if the implementation of `mbedtls_mock_socket` related I/O functions is
2351 * correct and works as expected on unconnected sockets.
2352 */
2353
2354/* BEGIN_CASE */
2355void ssl_mock_sanity( )
2356{
2357 enum { MSGLEN = 105 };
Paul Elliott21c8fe52021-11-24 16:54:26 +00002358 unsigned char message[MSGLEN] = { 0 };
2359 unsigned char received[MSGLEN] = { 0 };
Janos Follathc673c2c2019-12-02 15:47:26 +00002360 mbedtls_mock_socket socket;
2361
2362 mbedtls_mock_socket_init( &socket );
2363 TEST_ASSERT( mbedtls_mock_tcp_send_b( &socket, message, MSGLEN ) < 0 );
2364 mbedtls_mock_socket_close( &socket );
2365 mbedtls_mock_socket_init( &socket );
2366 TEST_ASSERT( mbedtls_mock_tcp_recv_b( &socket, received, MSGLEN ) < 0 );
2367 mbedtls_mock_socket_close( &socket );
2368
2369 mbedtls_mock_socket_init( &socket );
2370 TEST_ASSERT( mbedtls_mock_tcp_send_nb( &socket, message, MSGLEN ) < 0 );
2371 mbedtls_mock_socket_close( &socket );
2372 mbedtls_mock_socket_init( &socket );
2373 TEST_ASSERT( mbedtls_mock_tcp_recv_nb( &socket, received, MSGLEN ) < 0 );
2374 mbedtls_mock_socket_close( &socket );
2375
2376exit:
2377
2378 mbedtls_mock_socket_close( &socket );
2379}
2380/* END_CASE */
2381
2382/*
2383 * Test if the implementation of `mbedtls_mock_socket` related functions can
2384 * send a single message from the client to the server.
Janos Follath031827f2019-11-27 11:12:14 +00002385 */
2386
2387/* BEGIN_CASE */
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002388void ssl_mock_tcp( int blocking )
Janos Follath031827f2019-11-27 11:12:14 +00002389{
Janos Follathc673c2c2019-12-02 15:47:26 +00002390 enum { MSGLEN = 105 };
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002391 enum { BUFLEN = MSGLEN / 5 };
Janos Follathc673c2c2019-12-02 15:47:26 +00002392 unsigned char message[MSGLEN];
2393 unsigned char received[MSGLEN];
2394 mbedtls_mock_socket client;
2395 mbedtls_mock_socket server;
2396 size_t written, read;
2397 int send_ret, recv_ret;
2398 mbedtls_ssl_send_t *send;
2399 mbedtls_ssl_recv_t *recv;
Janos Follathc673c2c2019-12-02 15:47:26 +00002400 unsigned i;
2401
2402 if( blocking == 0 )
2403 {
2404 send = mbedtls_mock_tcp_send_nb;
2405 recv = mbedtls_mock_tcp_recv_nb;
2406 }
2407 else
2408 {
2409 send = mbedtls_mock_tcp_send_b;
2410 recv = mbedtls_mock_tcp_recv_b;
2411 }
2412
2413 mbedtls_mock_socket_init( &client );
2414 mbedtls_mock_socket_init( &server );
2415
2416 /* Fill up the buffer with structured data so that unwanted changes
2417 * can be detected */
2418 for( i = 0; i < MSGLEN; i++ )
2419 {
2420 message[i] = i & 0xFF;
2421 }
2422
2423 /* Make sure that sending a message takes a few iterations. */
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002424 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, BUFLEN ) );
Janos Follathc673c2c2019-12-02 15:47:26 +00002425
2426 /* Send the message to the server */
2427 send_ret = recv_ret = 1;
2428 written = read = 0;
2429 while( send_ret != 0 || recv_ret != 0 )
2430 {
2431 send_ret = send( &client, message + written, MSGLEN - written );
2432
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002433 TEST_ASSERT( send_ret >= 0 );
2434 TEST_ASSERT( send_ret <= BUFLEN );
2435 written += send_ret;
2436
2437 /* If the buffer is full we can test blocking and non-blocking send */
2438 if ( send_ret == BUFLEN )
Janos Follathc673c2c2019-12-02 15:47:26 +00002439 {
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002440 int blocking_ret = send( &client, message , 1 );
2441 if ( blocking )
2442 {
2443 TEST_ASSERT( blocking_ret == 0 );
2444 }
2445 else
2446 {
2447 TEST_ASSERT( blocking_ret == MBEDTLS_ERR_SSL_WANT_WRITE );
2448 }
Janos Follathc673c2c2019-12-02 15:47:26 +00002449 }
Janos Follathc673c2c2019-12-02 15:47:26 +00002450
2451 recv_ret = recv( &server, received + read, MSGLEN - read );
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002452
2453 /* The result depends on whether any data was sent */
2454 if ( send_ret > 0 )
Janos Follathc673c2c2019-12-02 15:47:26 +00002455 {
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002456 TEST_ASSERT( recv_ret > 0 );
2457 TEST_ASSERT( recv_ret <= BUFLEN );
2458 read += recv_ret;
2459 }
2460 else if( blocking )
2461 {
2462 TEST_ASSERT( recv_ret == 0 );
Janos Follathc673c2c2019-12-02 15:47:26 +00002463 }
2464 else
2465 {
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002466 TEST_ASSERT( recv_ret == MBEDTLS_ERR_SSL_WANT_READ );
2467 recv_ret = 0;
Janos Follathc673c2c2019-12-02 15:47:26 +00002468 }
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002469
2470 /* If the buffer is empty we can test blocking and non-blocking read */
2471 if ( recv_ret == BUFLEN )
2472 {
2473 int blocking_ret = recv( &server, received, 1 );
2474 if ( blocking )
2475 {
2476 TEST_ASSERT( blocking_ret == 0 );
2477 }
2478 else
2479 {
2480 TEST_ASSERT( blocking_ret == MBEDTLS_ERR_SSL_WANT_READ );
2481 }
2482 }
Janos Follathc673c2c2019-12-02 15:47:26 +00002483 }
2484 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
2485
2486exit:
2487
2488 mbedtls_mock_socket_close( &client );
2489 mbedtls_mock_socket_close( &server );
2490}
2491/* END_CASE */
2492
2493/*
2494 * Test if the implementation of `mbedtls_mock_socket` related functions can
2495 * send messages in both direction at the same time (with the I/O calls
2496 * interleaving).
2497 */
2498
2499/* BEGIN_CASE */
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002500void ssl_mock_tcp_interleaving( int blocking )
Janos Follathc673c2c2019-12-02 15:47:26 +00002501{
Janos Follath031827f2019-11-27 11:12:14 +00002502 enum { ROUNDS = 2 };
2503 enum { MSGLEN = 105 };
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002504 enum { BUFLEN = MSGLEN / 5 };
Janos Follath031827f2019-11-27 11:12:14 +00002505 unsigned char message[ROUNDS][MSGLEN];
2506 unsigned char received[ROUNDS][MSGLEN];
2507 mbedtls_mock_socket client;
2508 mbedtls_mock_socket server;
2509 size_t written[ROUNDS];
2510 size_t read[ROUNDS];
2511 int send_ret[ROUNDS];
2512 int recv_ret[ROUNDS];
2513 unsigned i, j, progress;
Janos Follath3766ba52019-11-27 13:31:42 +00002514 mbedtls_ssl_send_t *send;
2515 mbedtls_ssl_recv_t *recv;
Janos Follath3766ba52019-11-27 13:31:42 +00002516
2517 if( blocking == 0 )
2518 {
2519 send = mbedtls_mock_tcp_send_nb;
2520 recv = mbedtls_mock_tcp_recv_nb;
2521 }
2522 else
2523 {
2524 send = mbedtls_mock_tcp_send_b;
2525 recv = mbedtls_mock_tcp_recv_b;
2526 }
Janos Follath031827f2019-11-27 11:12:14 +00002527
2528 mbedtls_mock_socket_init( &client );
2529 mbedtls_mock_socket_init( &server );
2530
2531 /* Fill up the buffers with structured data so that unwanted changes
2532 * can be detected */
2533 for( i = 0; i < ROUNDS; i++ )
2534 {
2535 for( j = 0; j < MSGLEN; j++ )
2536 {
2537 message[i][j] = ( i * MSGLEN + j ) & 0xFF;
2538 }
2539 }
2540
Janos Follath031827f2019-11-27 11:12:14 +00002541 /* Make sure that sending a message takes a few iterations. */
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002542 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, BUFLEN ) );
Janos Follath031827f2019-11-27 11:12:14 +00002543
Janos Follath031827f2019-11-27 11:12:14 +00002544 /* Send the message from both sides, interleaving. */
2545 progress = 1;
2546 for( i = 0; i < ROUNDS; i++ )
2547 {
2548 written[i] = 0;
2549 read[i] = 0;
2550 }
2551 /* This loop does not stop as long as there was a successful write or read
2552 * of at least one byte on either side. */
2553 while( progress != 0 )
2554 {
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002555 mbedtls_mock_socket *socket;
Janos Follath031827f2019-11-27 11:12:14 +00002556
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002557 for( i = 0; i < ROUNDS; i++ )
Janos Follath3766ba52019-11-27 13:31:42 +00002558 {
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002559 /* First sending is from the client */
2560 socket = ( i % 2 == 0 ) ? ( &client ) : ( &server );
Janos Follath031827f2019-11-27 11:12:14 +00002561
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002562 send_ret[i] = send( socket, message[i] + written[i],
2563 MSGLEN - written[i] );
2564 TEST_ASSERT( send_ret[i] >= 0 );
2565 TEST_ASSERT( send_ret[i] <= BUFLEN );
2566 written[i] += send_ret[i];
Janos Follath031827f2019-11-27 11:12:14 +00002567
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002568 /* If the buffer is full we can test blocking and non-blocking
2569 * send */
2570 if ( send_ret[i] == BUFLEN )
2571 {
2572 int blocking_ret = send( socket, message[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_WRITE );
2580 }
2581 }
Janos Follath3766ba52019-11-27 13:31:42 +00002582 }
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002583
2584 for( i = 0; i < ROUNDS; i++ )
Janos Follath3766ba52019-11-27 13:31:42 +00002585 {
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002586 /* First receiving is from the server */
2587 socket = ( i % 2 == 0 ) ? ( &server ) : ( &client );
2588
2589 recv_ret[i] = recv( socket, received[i] + read[i],
2590 MSGLEN - read[i] );
2591
2592 /* The result depends on whether any data was sent */
2593 if ( send_ret[i] > 0 )
2594 {
2595 TEST_ASSERT( recv_ret[i] > 0 );
2596 TEST_ASSERT( recv_ret[i] <= BUFLEN );
2597 read[i] += recv_ret[i];
2598 }
2599 else if( blocking )
2600 {
2601 TEST_ASSERT( recv_ret[i] == 0 );
2602 }
2603 else
2604 {
2605 TEST_ASSERT( recv_ret[i] == MBEDTLS_ERR_SSL_WANT_READ );
2606 recv_ret[i] = 0;
2607 }
2608
2609 /* If the buffer is empty we can test blocking and non-blocking
2610 * read */
2611 if ( recv_ret[i] == BUFLEN )
2612 {
2613 int blocking_ret = recv( socket, received[i], 1 );
2614 if ( blocking )
2615 {
2616 TEST_ASSERT( blocking_ret == 0 );
2617 }
2618 else
2619 {
2620 TEST_ASSERT( blocking_ret == MBEDTLS_ERR_SSL_WANT_READ );
2621 }
2622 }
Janos Follath3766ba52019-11-27 13:31:42 +00002623 }
Janos Follath031827f2019-11-27 11:12:14 +00002624
2625 progress = 0;
2626 for( i = 0; i < ROUNDS; i++ )
2627 {
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002628 progress += send_ret[i] + recv_ret[i];
Janos Follath031827f2019-11-27 11:12:14 +00002629 }
2630 }
2631
2632 for( i = 0; i < ROUNDS; i++ )
2633 TEST_ASSERT( memcmp( message[i], received[i], MSGLEN ) == 0 );
2634
2635exit:
2636
2637 mbedtls_mock_socket_close( &client );
2638 mbedtls_mock_socket_close( &server );
2639}
2640/* END_CASE */
2641
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002642/* BEGIN_CASE */
2643void ssl_message_queue_sanity( )
2644{
2645 mbedtls_test_message_queue queue;
2646
2647 /* Trying to push/pull to an empty queue */
2648 TEST_ASSERT( mbedtls_test_message_queue_push_info( NULL, 1 )
2649 == MBEDTLS_TEST_ERROR_ARG_NULL );
2650 TEST_ASSERT( mbedtls_test_message_queue_pop_info( NULL, 1 )
2651 == MBEDTLS_TEST_ERROR_ARG_NULL );
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 TEST_ASSERT( queue.capacity == 3 );
2655 TEST_ASSERT( queue.num == 0 );
2656
2657exit:
2658 mbedtls_test_message_queue_free( &queue );
2659}
2660/* END_CASE */
2661
2662/* BEGIN_CASE */
2663void ssl_message_queue_basic( )
2664{
2665 mbedtls_test_message_queue queue;
2666
Andrzej Kurek89bdc582020-03-09 06:29:43 -04002667 TEST_ASSERT( mbedtls_test_message_queue_setup( &queue, 3 ) == 0 );
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002668
2669 /* Sanity test - 3 pushes and 3 pops with sufficient space */
2670 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 );
2671 TEST_ASSERT( queue.capacity == 3 );
2672 TEST_ASSERT( queue.num == 1 );
2673 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 );
2674 TEST_ASSERT( queue.capacity == 3 );
2675 TEST_ASSERT( queue.num == 2 );
2676 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 2 ) == 2 );
2677 TEST_ASSERT( queue.capacity == 3 );
2678 TEST_ASSERT( queue.num == 3 );
2679
2680 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 );
2681 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 );
2682 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 2 ) == 2 );
2683
2684exit:
2685 mbedtls_test_message_queue_free( &queue );
2686}
2687/* END_CASE */
2688
2689/* BEGIN_CASE */
2690void ssl_message_queue_overflow_underflow( )
2691{
2692 mbedtls_test_message_queue queue;
2693
Andrzej Kurek89bdc582020-03-09 06:29:43 -04002694 TEST_ASSERT( mbedtls_test_message_queue_setup( &queue, 3 ) == 0 );
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002695
2696 /* 4 pushes (last one with an error), 4 pops (last one with an error) */
2697 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 );
2698 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 );
2699 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 2 ) == 2 );
2700 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 3 )
Andrzej Kurekf46b9122020-02-07 08:19:00 -05002701 == MBEDTLS_ERR_SSL_WANT_WRITE );
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002702
2703 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 );
2704 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 );
2705 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 2 ) == 2 );
2706
2707 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 )
Andrzej Kurekf46b9122020-02-07 08:19:00 -05002708 == MBEDTLS_ERR_SSL_WANT_READ );
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002709
2710exit:
2711 mbedtls_test_message_queue_free( &queue );
2712}
2713/* END_CASE */
2714
2715/* BEGIN_CASE */
2716void ssl_message_queue_interleaved( )
2717{
2718 mbedtls_test_message_queue queue;
2719
Andrzej Kurek89bdc582020-03-09 06:29:43 -04002720 TEST_ASSERT( mbedtls_test_message_queue_setup( &queue, 3 ) == 0 );
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002721
2722 /* Interleaved test - [2 pushes, 1 pop] twice, and then two pops
2723 * (to wrap around the buffer) */
2724 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 );
2725 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 );
2726
2727 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 );
2728
2729 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 2 ) == 2 );
2730 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 3 ) == 3 );
2731
2732 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 );
2733 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 2 ) == 2 );
2734
2735 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 5 ) == 5 );
2736 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 8 ) == 8 );
2737
2738 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 3 ) == 3 );
2739
2740 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 5 ) == 5 );
2741
2742 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 8 ) == 8 );
2743
2744exit:
2745 mbedtls_test_message_queue_free( &queue );
2746}
2747/* END_CASE */
2748
2749/* BEGIN_CASE */
2750void ssl_message_queue_insufficient_buffer( )
2751{
2752 mbedtls_test_message_queue queue;
2753 size_t message_len = 10;
2754 size_t buffer_len = 5;
2755
Andrzej Kurek89bdc582020-03-09 06:29:43 -04002756 TEST_ASSERT( mbedtls_test_message_queue_setup( &queue, 1 ) == 0 );
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002757
2758 /* Popping without a sufficient buffer */
2759 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, message_len )
2760 == (int) message_len );
2761 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, buffer_len )
2762 == (int) buffer_len );
2763exit:
2764 mbedtls_test_message_queue_free( &queue );
2765}
2766/* END_CASE */
2767
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002768/* BEGIN_CASE */
2769void ssl_message_mock_uninitialized( )
2770{
2771 enum { MSGLEN = 10 };
Shawn Carey03092f52021-05-13 10:38:32 -04002772 unsigned char message[MSGLEN] = {0}, received[MSGLEN];
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002773 mbedtls_mock_socket client, server;
2774 mbedtls_test_message_queue server_queue, client_queue;
2775 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05002776 mbedtls_message_socket_init( &server_context );
2777 mbedtls_message_socket_init( &client_context );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002778
2779 /* Send with a NULL context */
2780 TEST_ASSERT( mbedtls_mock_tcp_send_msg( NULL, message, MSGLEN )
2781 == MBEDTLS_TEST_ERROR_CONTEXT_ERROR );
2782
2783 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( NULL, message, MSGLEN )
2784 == MBEDTLS_TEST_ERROR_CONTEXT_ERROR );
2785
2786 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 1,
2787 &server,
2788 &server_context ) == 0 );
2789
2790 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 1,
2791 &client,
2792 &client_context ) == 0 );
2793
2794 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, MSGLEN )
2795 == MBEDTLS_TEST_ERROR_SEND_FAILED );
2796
2797 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
Andrzej Kurekf46b9122020-02-07 08:19:00 -05002798 == MBEDTLS_ERR_SSL_WANT_READ );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002799
2800 /* Push directly to a queue to later simulate a disconnected behavior */
2801 TEST_ASSERT( mbedtls_test_message_queue_push_info( &server_queue, MSGLEN )
2802 == MSGLEN );
2803
2804 /* Test if there's an error when trying to read from a disconnected
2805 * socket */
2806 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
2807 == MBEDTLS_TEST_ERROR_RECV_FAILED );
2808 exit:
2809 mbedtls_message_socket_close( &server_context );
2810 mbedtls_message_socket_close( &client_context );
2811}
2812/* END_CASE */
2813
2814/* BEGIN_CASE */
2815void ssl_message_mock_basic( )
2816{
2817 enum { MSGLEN = 10 };
2818 unsigned char message[MSGLEN], received[MSGLEN];
2819 mbedtls_mock_socket client, server;
2820 unsigned i;
2821 mbedtls_test_message_queue server_queue, client_queue;
2822 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05002823 mbedtls_message_socket_init( &server_context );
2824 mbedtls_message_socket_init( &client_context );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002825
2826 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 1,
2827 &server,
2828 &server_context ) == 0 );
2829
2830 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 1,
2831 &client,
2832 &client_context ) == 0 );
2833
2834 /* Fill up the buffer with structured data so that unwanted changes
2835 * can be detected */
2836 for( i = 0; i < MSGLEN; i++ )
2837 {
2838 message[i] = i & 0xFF;
2839 }
2840 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
2841 MSGLEN ) );
2842
2843 /* Send the message to the server */
2844 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2845 MSGLEN ) == MSGLEN );
2846
2847 /* Read from the server */
2848 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
2849 == MSGLEN );
2850
2851 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
2852 memset( received, 0, MSGLEN );
2853
2854 /* Send the message to the client */
2855 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &server_context, message,
2856 MSGLEN ) == MSGLEN );
2857
2858 /* Read from the client */
2859 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &client_context, received, MSGLEN )
2860 == MSGLEN );
2861 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
2862
2863 exit:
2864 mbedtls_message_socket_close( &server_context );
2865 mbedtls_message_socket_close( &client_context );
2866}
2867/* END_CASE */
2868
2869/* BEGIN_CASE */
2870void ssl_message_mock_queue_overflow_underflow( )
2871{
2872 enum { MSGLEN = 10 };
2873 unsigned char message[MSGLEN], received[MSGLEN];
2874 mbedtls_mock_socket client, server;
2875 unsigned i;
2876 mbedtls_test_message_queue server_queue, client_queue;
2877 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05002878 mbedtls_message_socket_init( &server_context );
2879 mbedtls_message_socket_init( &client_context );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002880
2881 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 2,
2882 &server,
2883 &server_context ) == 0 );
2884
2885 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 2,
2886 &client,
2887 &client_context ) == 0 );
2888
2889 /* Fill up the buffer with structured data so that unwanted changes
2890 * can be detected */
2891 for( i = 0; i < MSGLEN; i++ )
2892 {
2893 message[i] = i & 0xFF;
2894 }
2895 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
2896 MSGLEN*2 ) );
2897
2898 /* Send three message to the server, last one with an error */
2899 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2900 MSGLEN - 1 ) == MSGLEN - 1 );
2901
2902 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2903 MSGLEN ) == MSGLEN );
2904
2905 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2906 MSGLEN )
Andrzej Kurekf46b9122020-02-07 08:19:00 -05002907 == MBEDTLS_ERR_SSL_WANT_WRITE );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002908
2909 /* Read three messages from the server, last one with an error */
2910 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received,
2911 MSGLEN - 1 ) == MSGLEN - 1 );
2912
2913 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
2914 == MSGLEN );
2915
2916 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
2917
2918 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
Andrzej Kurekf46b9122020-02-07 08:19:00 -05002919 == MBEDTLS_ERR_SSL_WANT_READ );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002920
2921 exit:
2922 mbedtls_message_socket_close( &server_context );
2923 mbedtls_message_socket_close( &client_context );
2924}
2925/* END_CASE */
2926
2927/* BEGIN_CASE */
2928void ssl_message_mock_socket_overflow( )
2929{
2930 enum { MSGLEN = 10 };
2931 unsigned char message[MSGLEN], received[MSGLEN];
2932 mbedtls_mock_socket client, server;
2933 unsigned i;
2934 mbedtls_test_message_queue server_queue, client_queue;
2935 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05002936 mbedtls_message_socket_init( &server_context );
2937 mbedtls_message_socket_init( &client_context );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002938
2939 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 2,
2940 &server,
2941 &server_context ) == 0 );
2942
2943 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 2,
2944 &client,
2945 &client_context ) == 0 );
2946
2947 /* Fill up the buffer with structured data so that unwanted changes
2948 * can be detected */
2949 for( i = 0; i < MSGLEN; i++ )
2950 {
2951 message[i] = i & 0xFF;
2952 }
2953 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
2954 MSGLEN ) );
2955
2956 /* Send two message to the server, second one with an error */
2957 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2958 MSGLEN ) == MSGLEN );
2959
2960 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2961 MSGLEN )
2962 == MBEDTLS_TEST_ERROR_SEND_FAILED );
2963
2964 /* Read the only message from the server */
2965 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
2966 == MSGLEN );
2967
2968 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
2969
2970 exit:
2971 mbedtls_message_socket_close( &server_context );
2972 mbedtls_message_socket_close( &client_context );
2973}
2974/* END_CASE */
2975
2976/* BEGIN_CASE */
2977void ssl_message_mock_truncated( )
2978{
2979 enum { MSGLEN = 10 };
2980 unsigned char message[MSGLEN], received[MSGLEN];
2981 mbedtls_mock_socket client, server;
2982 unsigned i;
2983 mbedtls_test_message_queue server_queue, client_queue;
2984 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05002985 mbedtls_message_socket_init( &server_context );
2986 mbedtls_message_socket_init( &client_context );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002987
2988 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 2,
2989 &server,
2990 &server_context ) == 0 );
2991
2992 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 2,
2993 &client,
2994 &client_context ) == 0 );
2995
2996 memset( received, 0, MSGLEN );
2997 /* Fill up the buffer with structured data so that unwanted changes
2998 * can be detected */
2999 for( i = 0; i < MSGLEN; i++ )
3000 {
3001 message[i] = i & 0xFF;
3002 }
3003 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
3004 2 * MSGLEN ) );
3005
3006 /* Send two messages to the server, the second one small enough to fit in the
3007 * receiver's buffer. */
3008 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
3009 MSGLEN ) == MSGLEN );
3010 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
3011 MSGLEN / 2 ) == MSGLEN / 2 );
3012 /* Read a truncated message from the server */
3013 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN/2 )
3014 == MSGLEN/2 );
3015
3016 /* Test that the first half of the message is valid, and second one isn't */
3017 TEST_ASSERT( memcmp( message, received, MSGLEN/2 ) == 0 );
3018 TEST_ASSERT( memcmp( message + MSGLEN/2, received + MSGLEN/2, MSGLEN/2 )
3019 != 0 );
3020 memset( received, 0, MSGLEN );
3021
3022 /* Read a full message from the server */
3023 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN/2 )
3024 == MSGLEN / 2 );
3025
3026 /* Test that the first half of the message is valid */
3027 TEST_ASSERT( memcmp( message, received, MSGLEN/2 ) == 0 );
3028
3029 exit:
3030 mbedtls_message_socket_close( &server_context );
3031 mbedtls_message_socket_close( &client_context );
3032}
3033/* END_CASE */
3034
3035/* BEGIN_CASE */
3036void ssl_message_mock_socket_read_error( )
3037{
3038 enum { MSGLEN = 10 };
3039 unsigned char message[MSGLEN], received[MSGLEN];
3040 mbedtls_mock_socket client, server;
3041 unsigned i;
3042 mbedtls_test_message_queue server_queue, client_queue;
3043 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05003044 mbedtls_message_socket_init( &server_context );
3045 mbedtls_message_socket_init( &client_context );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003046
3047 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 1,
3048 &server,
3049 &server_context ) == 0 );
3050
3051 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 1,
3052 &client,
3053 &client_context ) == 0 );
3054
3055 /* Fill up the buffer with structured data so that unwanted changes
3056 * can be detected */
3057 for( i = 0; i < MSGLEN; i++ )
3058 {
3059 message[i] = i & 0xFF;
3060 }
3061 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
3062 MSGLEN ) );
3063
3064 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
3065 MSGLEN ) == MSGLEN );
3066
3067 /* Force a read error by disconnecting the socket by hand */
3068 server.status = 0;
3069 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
3070 == MBEDTLS_TEST_ERROR_RECV_FAILED );
3071 /* Return to a valid state */
3072 server.status = MBEDTLS_MOCK_SOCKET_CONNECTED;
3073
3074 memset( received, 0, sizeof( received ) );
3075
3076 /* Test that even though the server tried to read once disconnected, the
3077 * continuity is preserved */
3078 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
3079 == MSGLEN );
3080
3081 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
3082
3083 exit:
3084 mbedtls_message_socket_close( &server_context );
3085 mbedtls_message_socket_close( &client_context );
3086}
3087/* END_CASE */
3088
3089/* BEGIN_CASE */
3090void ssl_message_mock_interleaved_one_way( )
3091{
3092 enum { MSGLEN = 10 };
3093 unsigned char message[MSGLEN], received[MSGLEN];
3094 mbedtls_mock_socket client, server;
3095 unsigned i;
3096 mbedtls_test_message_queue server_queue, client_queue;
3097 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05003098 mbedtls_message_socket_init( &server_context );
3099 mbedtls_message_socket_init( &client_context );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003100
3101 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 3,
3102 &server,
3103 &server_context ) == 0 );
3104
3105 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 3,
3106 &client,
3107 &client_context ) == 0 );
3108
3109 /* Fill up the buffer with structured data so that unwanted changes
3110 * can be detected */
3111 for( i = 0; i < MSGLEN; i++ )
3112 {
3113 message[i] = i & 0xFF;
3114 }
3115 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
3116 MSGLEN*3 ) );
3117
3118 /* Interleaved test - [2 sends, 1 read] twice, and then two reads
3119 * (to wrap around the buffer) */
3120 for( i = 0; i < 2; i++ )
3121 {
3122 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
3123 MSGLEN ) == MSGLEN );
3124
3125 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
3126 MSGLEN ) == MSGLEN );
3127
3128 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received,
3129 MSGLEN ) == MSGLEN );
3130 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
3131 memset( received, 0, sizeof( received ) );
3132 }
3133
3134 for( i = 0; i < 2; i++ )
3135 {
3136 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received,
3137 MSGLEN ) == MSGLEN );
3138
3139 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
3140 }
3141 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
Andrzej Kurekf46b9122020-02-07 08:19:00 -05003142 == MBEDTLS_ERR_SSL_WANT_READ );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003143 exit:
3144 mbedtls_message_socket_close( &server_context );
3145 mbedtls_message_socket_close( &client_context );
3146}
3147/* END_CASE */
3148
3149/* BEGIN_CASE */
3150void ssl_message_mock_interleaved_two_ways( )
3151{
3152 enum { MSGLEN = 10 };
3153 unsigned char message[MSGLEN], received[MSGLEN];
3154 mbedtls_mock_socket client, server;
3155 unsigned i;
3156 mbedtls_test_message_queue server_queue, client_queue;
3157 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05003158 mbedtls_message_socket_init( &server_context );
3159 mbedtls_message_socket_init( &client_context );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003160
3161 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 3,
3162 &server,
3163 &server_context ) == 0 );
3164
3165 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 3,
3166 &client,
3167 &client_context ) == 0 );
3168
3169 /* Fill up the buffer with structured data so that unwanted changes
3170 * can be detected */
3171 for( i = 0; i < MSGLEN; i++ )
3172 {
3173 message[i] = i & 0xFF;
3174 }
3175 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
3176 MSGLEN*3 ) );
3177
3178 /* Interleaved test - [2 sends, 1 read] twice, both ways, and then two reads
3179 * (to wrap around the buffer) both ways. */
3180 for( i = 0; i < 2; i++ )
3181 {
3182 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
3183 MSGLEN ) == MSGLEN );
3184
3185 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
3186 MSGLEN ) == MSGLEN );
3187
3188 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &server_context, message,
3189 MSGLEN ) == MSGLEN );
3190
3191 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &server_context, message,
3192 MSGLEN ) == MSGLEN );
3193
3194 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received,
3195 MSGLEN ) == MSGLEN );
3196
3197 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
3198
3199 memset( received, 0, sizeof( received ) );
3200
3201 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &client_context, received,
3202 MSGLEN ) == MSGLEN );
3203
3204 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
3205
3206 memset( received, 0, sizeof( received ) );
3207 }
3208
3209 for( i = 0; i < 2; i++ )
3210 {
3211 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received,
3212 MSGLEN ) == MSGLEN );
3213
3214 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
3215 memset( received, 0, sizeof( received ) );
3216
3217 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &client_context, received,
3218 MSGLEN ) == MSGLEN );
3219
3220 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
3221 memset( received, 0, sizeof( received ) );
3222 }
3223
3224 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
Andrzej Kurekf46b9122020-02-07 08:19:00 -05003225 == MBEDTLS_ERR_SSL_WANT_READ );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003226
3227 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &client_context, received, MSGLEN )
Andrzej Kurekf46b9122020-02-07 08:19:00 -05003228 == MBEDTLS_ERR_SSL_WANT_READ );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003229 exit:
3230 mbedtls_message_socket_close( &server_context );
3231 mbedtls_message_socket_close( &client_context );
3232}
3233/* END_CASE */
3234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003235/* BEGIN_CASE depends_on:MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Azim Khan5fcca462018-06-29 11:05:32 +01003236void ssl_dtls_replay( data_t * prevs, data_t * new, int ret )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003237{
Azim Khand30ca132017-06-09 04:32:58 +01003238 uint32_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003239 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02003240 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003241
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02003242 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02003243 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02003244
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02003245 TEST_ASSERT( mbedtls_ssl_config_defaults( &conf,
3246 MBEDTLS_SSL_IS_CLIENT,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02003247 MBEDTLS_SSL_TRANSPORT_DATAGRAM,
3248 MBEDTLS_SSL_PRESET_DEFAULT ) == 0 );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02003249 TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003250
3251 /* Read previous record numbers */
Azim Khand30ca132017-06-09 04:32:58 +01003252 for( len = 0; len < prevs->len; len += 6 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003253 {
Azim Khand30ca132017-06-09 04:32:58 +01003254 memcpy( ssl.in_ctr + 2, prevs->x + len, 6 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003255 mbedtls_ssl_dtls_replay_update( &ssl );
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003256 }
3257
3258 /* Check new number */
Azim Khand30ca132017-06-09 04:32:58 +01003259 memcpy( ssl.in_ctr + 2, new->x, 6 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003260 TEST_ASSERT( mbedtls_ssl_dtls_replay_check( &ssl ) == ret );
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003262 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02003263 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003264}
3265/* END_CASE */
Hanno Beckerb25c0c72017-05-05 11:24:30 +01003266
3267/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
3268void ssl_set_hostname_twice( char *hostname0, char *hostname1 )
3269{
3270 mbedtls_ssl_context ssl;
3271 mbedtls_ssl_init( &ssl );
3272
3273 TEST_ASSERT( mbedtls_ssl_set_hostname( &ssl, hostname0 ) == 0 );
3274 TEST_ASSERT( mbedtls_ssl_set_hostname( &ssl, hostname1 ) == 0 );
3275
3276 mbedtls_ssl_free( &ssl );
3277}
Darryl Green11999bb2018-03-13 15:22:58 +00003278/* END_CASE */
Hanno Beckera18d1322018-01-03 14:27:32 +00003279
3280/* BEGIN_CASE */
3281void ssl_crypt_record( int cipher_type, int hash_id,
Hanno Beckerd856c822019-04-29 17:30:59 +01003282 int etm, int tag_mode, int ver,
3283 int cid0_len, int cid1_len )
Hanno Beckera18d1322018-01-03 14:27:32 +00003284{
3285 /*
3286 * Test several record encryptions and decryptions
3287 * with plenty of space before and after the data
3288 * within the record buffer.
3289 */
3290
3291 int ret;
3292 int num_records = 16;
3293 mbedtls_ssl_context ssl; /* ONLY for debugging */
3294
3295 mbedtls_ssl_transform t0, t1;
Hanno Becker81e16a32019-03-01 11:21:44 +00003296 unsigned char *buf = NULL;
Hanno Beckera18d1322018-01-03 14:27:32 +00003297 size_t const buflen = 512;
3298 mbedtls_record rec, rec_backup;
3299
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01003300 USE_PSA_INIT( );
3301
Hanno Beckera18d1322018-01-03 14:27:32 +00003302 mbedtls_ssl_init( &ssl );
3303 mbedtls_ssl_transform_init( &t0 );
3304 mbedtls_ssl_transform_init( &t1 );
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01003305 ret = build_transforms( &t0, &t1, cipher_type, hash_id,
3306 etm, tag_mode, ver,
3307 (size_t) cid0_len,
3308 (size_t) cid1_len );
3309
3310 TEST_ASSERT( ret == 0 );
Hanno Beckera18d1322018-01-03 14:27:32 +00003311
Hanno Becker3ee54212019-04-04 16:31:26 +01003312 TEST_ASSERT( ( buf = mbedtls_calloc( 1, buflen ) ) != NULL );
Hanno Beckera18d1322018-01-03 14:27:32 +00003313
3314 while( num_records-- > 0 )
3315 {
3316 mbedtls_ssl_transform *t_dec, *t_enc;
3317 /* Take turns in who's sending and who's receiving. */
3318 if( num_records % 3 == 0 )
3319 {
3320 t_dec = &t0;
3321 t_enc = &t1;
3322 }
3323 else
3324 {
3325 t_dec = &t1;
3326 t_enc = &t0;
3327 }
3328
3329 /*
3330 * The record header affects the transformation in two ways:
3331 * 1) It determines the AEAD additional data
3332 * 2) The record counter sometimes determines the IV.
3333 *
3334 * Apart from that, the fields don't have influence.
3335 * In particular, it is currently not the responsibility
3336 * of ssl_encrypt/decrypt_buf to check if the transform
3337 * version matches the record version, or that the
3338 * type is sensible.
3339 */
3340
3341 memset( rec.ctr, num_records, sizeof( rec.ctr ) );
3342 rec.type = 42;
3343 rec.ver[0] = num_records;
3344 rec.ver[1] = num_records;
Hanno Beckera0e20d02019-05-15 14:03:01 +01003345#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd856c822019-04-29 17:30:59 +01003346 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01003347#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera18d1322018-01-03 14:27:32 +00003348
3349 rec.buf = buf;
3350 rec.buf_len = buflen;
3351 rec.data_offset = 16;
3352 /* Make sure to vary the length to exercise different
3353 * paddings. */
3354 rec.data_len = 1 + num_records;
3355
3356 memset( rec.buf + rec.data_offset, 42, rec.data_len );
3357
3358 /* Make a copy for later comparison */
3359 rec_backup = rec;
3360
3361 /* Encrypt record */
3362 ret = mbedtls_ssl_encrypt_buf( &ssl, t_enc, &rec,
Ronald Cron351f0ee2020-06-10 12:12:18 +02003363 mbedtls_test_rnd_std_rand, NULL );
Hanno Beckera18d1322018-01-03 14:27:32 +00003364 TEST_ASSERT( ret == 0 || ret == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3365 if( ret != 0 )
3366 {
3367 continue;
3368 }
3369
Hanno Beckerb2713ab2020-05-07 14:54:22 +01003370#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3371 if( rec.cid_len != 0 )
3372 {
3373 /* DTLS 1.2 + CID hides the real content type and
3374 * uses a special CID content type in the protected
3375 * record. Double-check this. */
3376 TEST_ASSERT( rec.type == MBEDTLS_SSL_MSG_CID );
3377 }
3378#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3379
Ronald Cron6f135e12021-12-08 16:57:54 +01003380#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Beckerb2713ab2020-05-07 14:54:22 +01003381 if( t_enc->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
3382 {
3383 /* TLS 1.3 hides the real content type and
3384 * always uses Application Data as the content type
3385 * for protected records. Double-check this. */
3386 TEST_ASSERT( rec.type == MBEDTLS_SSL_MSG_APPLICATION_DATA );
3387 }
Ronald Cron6f135e12021-12-08 16:57:54 +01003388#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb2713ab2020-05-07 14:54:22 +01003389
Hanno Beckera18d1322018-01-03 14:27:32 +00003390 /* Decrypt record with t_dec */
Hanno Beckerd856c822019-04-29 17:30:59 +01003391 ret = mbedtls_ssl_decrypt_buf( &ssl, t_dec, &rec );
3392 TEST_ASSERT( ret == 0 );
Hanno Beckera18d1322018-01-03 14:27:32 +00003393
3394 /* Compare results */
3395 TEST_ASSERT( rec.type == rec_backup.type );
3396 TEST_ASSERT( memcmp( rec.ctr, rec_backup.ctr, 8 ) == 0 );
3397 TEST_ASSERT( rec.ver[0] == rec_backup.ver[0] );
3398 TEST_ASSERT( rec.ver[1] == rec_backup.ver[1] );
3399 TEST_ASSERT( rec.data_len == rec_backup.data_len );
3400 TEST_ASSERT( rec.data_offset == rec_backup.data_offset );
3401 TEST_ASSERT( memcmp( rec.buf + rec.data_offset,
3402 rec_backup.buf + rec_backup.data_offset,
3403 rec.data_len ) == 0 );
3404 }
3405
Hanno Becker81e16a32019-03-01 11:21:44 +00003406exit:
3407
Hanno Beckera18d1322018-01-03 14:27:32 +00003408 /* Cleanup */
3409 mbedtls_ssl_free( &ssl );
3410 mbedtls_ssl_transform_free( &t0 );
3411 mbedtls_ssl_transform_free( &t1 );
3412
Hanno Becker3ee54212019-04-04 16:31:26 +01003413 mbedtls_free( buf );
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01003414 USE_PSA_DONE( );
Hanno Beckera18d1322018-01-03 14:27:32 +00003415}
3416/* END_CASE */
Hanno Beckerb3268da2018-01-05 15:20:24 +00003417
3418/* BEGIN_CASE */
3419void ssl_crypt_record_small( int cipher_type, int hash_id,
Hanno Beckerd856c822019-04-29 17:30:59 +01003420 int etm, int tag_mode, int ver,
3421 int cid0_len, int cid1_len )
Hanno Beckerb3268da2018-01-05 15:20:24 +00003422{
3423 /*
3424 * Test pairs of encryption and decryption with an increasing
3425 * amount of space in the record buffer - in more detail:
3426 * 1) Try to encrypt with 0, 1, 2, ... bytes available
3427 * in front of the plaintext, and expect the encryption
3428 * to succeed starting from some offset. Always keep
3429 * enough space in the end of the buffer.
3430 * 2) Try to encrypt with 0, 1, 2, ... bytes available
3431 * at the end of the plaintext, and expect the encryption
3432 * to succeed starting from some offset. Always keep
3433 * enough space at the beginning of the buffer.
3434 * 3) Try to encrypt with 0, 1, 2, ... bytes available
3435 * both at the front and end of the plaintext,
3436 * and expect the encryption to succeed starting from
3437 * some offset.
3438 *
3439 * If encryption succeeds, check that decryption succeeds
3440 * and yields the original record.
3441 */
3442
3443 mbedtls_ssl_context ssl; /* ONLY for debugging */
3444
3445 mbedtls_ssl_transform t0, t1;
Hanno Becker81e16a32019-03-01 11:21:44 +00003446 unsigned char *buf = NULL;
Hanno Beckerd856c822019-04-29 17:30:59 +01003447 size_t const buflen = 256;
Hanno Beckerb3268da2018-01-05 15:20:24 +00003448 mbedtls_record rec, rec_backup;
3449
3450 int ret;
Hanno Beckerd856c822019-04-29 17:30:59 +01003451 int mode; /* Mode 1, 2 or 3 as explained above */
3452 size_t offset; /* Available space at beginning/end/both */
3453 size_t threshold = 96; /* Maximum offset to test against */
Hanno Beckerb3268da2018-01-05 15:20:24 +00003454
Hanno Beckerd856c822019-04-29 17:30:59 +01003455 size_t default_pre_padding = 64; /* Pre-padding to use in mode 2 */
3456 size_t default_post_padding = 128; /* Post-padding to use in mode 1 */
Hanno Beckerb3268da2018-01-05 15:20:24 +00003457
3458 int seen_success; /* Indicates if in the current mode we've
3459 * already seen a successful test. */
3460
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01003461 USE_PSA_INIT( );
3462
Hanno Beckerb3268da2018-01-05 15:20:24 +00003463 mbedtls_ssl_init( &ssl );
3464 mbedtls_ssl_transform_init( &t0 );
3465 mbedtls_ssl_transform_init( &t1 );
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01003466 ret = build_transforms( &t0, &t1, cipher_type, hash_id,
Hanno Beckerd856c822019-04-29 17:30:59 +01003467 etm, tag_mode, ver,
3468 (size_t) cid0_len,
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01003469 (size_t) cid1_len );
3470
3471 TEST_ASSERT( ret == 0 );
Hanno Beckerb3268da2018-01-05 15:20:24 +00003472
Hanno Becker3ee54212019-04-04 16:31:26 +01003473 TEST_ASSERT( ( buf = mbedtls_calloc( 1, buflen ) ) != NULL );
Hanno Beckerb3268da2018-01-05 15:20:24 +00003474
3475 for( mode=1; mode <= 3; mode++ )
3476 {
3477 seen_success = 0;
3478 for( offset=0; offset <= threshold; offset++ )
3479 {
3480 mbedtls_ssl_transform *t_dec, *t_enc;
Hanno Becker6c87b3f2019-04-29 17:24:44 +01003481 t_dec = &t0;
3482 t_enc = &t1;
Hanno Beckerb3268da2018-01-05 15:20:24 +00003483
3484 memset( rec.ctr, offset, sizeof( rec.ctr ) );
3485 rec.type = 42;
3486 rec.ver[0] = offset;
3487 rec.ver[1] = offset;
3488 rec.buf = buf;
3489 rec.buf_len = buflen;
Hanno Beckera0e20d02019-05-15 14:03:01 +01003490#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd856c822019-04-29 17:30:59 +01003491 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01003492#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerb3268da2018-01-05 15:20:24 +00003493
3494 switch( mode )
3495 {
3496 case 1: /* Space in the beginning */
3497 rec.data_offset = offset;
3498 rec.data_len = buflen - offset - default_post_padding;
3499 break;
3500
3501 case 2: /* Space in the end */
3502 rec.data_offset = default_pre_padding;
3503 rec.data_len = buflen - default_pre_padding - offset;
3504 break;
3505
3506 case 3: /* Space in the beginning and end */
3507 rec.data_offset = offset;
3508 rec.data_len = buflen - 2 * offset;
3509 break;
3510
3511 default:
3512 TEST_ASSERT( 0 );
3513 break;
3514 }
3515
3516 memset( rec.buf + rec.data_offset, 42, rec.data_len );
3517
3518 /* Make a copy for later comparison */
3519 rec_backup = rec;
3520
3521 /* Encrypt record */
Ronald Cron6c5bd7f2020-06-10 14:08:26 +02003522 ret = mbedtls_ssl_encrypt_buf( &ssl, t_enc, &rec,
3523 mbedtls_test_rnd_std_rand, NULL );
Hanno Beckerb3268da2018-01-05 15:20:24 +00003524
3525 if( ( mode == 1 || mode == 2 ) && seen_success )
3526 {
3527 TEST_ASSERT( ret == 0 );
3528 }
3529 else
3530 {
3531 TEST_ASSERT( ret == 0 || ret == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3532 if( ret == 0 )
3533 seen_success = 1;
3534 }
3535
3536 if( ret != 0 )
3537 continue;
3538
Hanno Beckerb2713ab2020-05-07 14:54:22 +01003539#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3540 if( rec.cid_len != 0 )
3541 {
3542 /* DTLS 1.2 + CID hides the real content type and
3543 * uses a special CID content type in the protected
3544 * record. Double-check this. */
3545 TEST_ASSERT( rec.type == MBEDTLS_SSL_MSG_CID );
3546 }
3547#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3548
Ronald Cron6f135e12021-12-08 16:57:54 +01003549#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Beckerb2713ab2020-05-07 14:54:22 +01003550 if( t_enc->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
3551 {
3552 /* TLS 1.3 hides the real content type and
3553 * always uses Application Data as the content type
3554 * for protected records. Double-check this. */
3555 TEST_ASSERT( rec.type == MBEDTLS_SSL_MSG_APPLICATION_DATA );
3556 }
Ronald Cron6f135e12021-12-08 16:57:54 +01003557#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb2713ab2020-05-07 14:54:22 +01003558
Hanno Beckerb3268da2018-01-05 15:20:24 +00003559 /* Decrypt record with t_dec */
3560 TEST_ASSERT( mbedtls_ssl_decrypt_buf( &ssl, t_dec, &rec ) == 0 );
3561
3562 /* Compare results */
3563 TEST_ASSERT( rec.type == rec_backup.type );
3564 TEST_ASSERT( memcmp( rec.ctr, rec_backup.ctr, 8 ) == 0 );
3565 TEST_ASSERT( rec.ver[0] == rec_backup.ver[0] );
3566 TEST_ASSERT( rec.ver[1] == rec_backup.ver[1] );
3567 TEST_ASSERT( rec.data_len == rec_backup.data_len );
3568 TEST_ASSERT( rec.data_offset == rec_backup.data_offset );
3569 TEST_ASSERT( memcmp( rec.buf + rec.data_offset,
3570 rec_backup.buf + rec_backup.data_offset,
3571 rec.data_len ) == 0 );
3572 }
3573
3574 TEST_ASSERT( seen_success == 1 );
3575 }
3576
Hanno Becker81e16a32019-03-01 11:21:44 +00003577exit:
3578
Hanno Beckerb3268da2018-01-05 15:20:24 +00003579 /* Cleanup */
3580 mbedtls_ssl_free( &ssl );
3581 mbedtls_ssl_transform_free( &t0 );
3582 mbedtls_ssl_transform_free( &t1 );
3583
Hanno Becker3ee54212019-04-04 16:31:26 +01003584 mbedtls_free( buf );
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01003585 USE_PSA_DONE( );
Hanno Beckerb3268da2018-01-05 15:20:24 +00003586}
3587/* END_CASE */
Ron Eldor824ad7b2019-05-13 14:09:00 +03003588
Przemyslaw Stekielf4facef2022-02-02 21:31:04 +01003589/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC:MBEDTLS_AES_C:MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003590void ssl_decrypt_non_etm_cbc( int cipher_type, int hash_id, int trunc_hmac,
Manuel Pégourié-Gonnard864abbf2020-07-21 10:37:14 +02003591 int length_selector )
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003592{
3593 /*
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003594 * Test record decryption for CBC without EtM, focused on the verification
3595 * of padding and MAC.
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003596 *
TRodziewicz299510e2021-07-09 16:55:11 +02003597 * Actually depends on TLS 1.2 and either AES, ARIA or Camellia, but since
3598 * the test framework doesn't support alternation in dependency statements,
3599 * just depend on AES.
Manuel Pégourié-Gonnard864abbf2020-07-21 10:37:14 +02003600 *
3601 * The length_selector argument is interpreted as follows:
3602 * - if it's -1, the plaintext length is 0 and minimal padding is applied
3603 * - if it's -2, the plaintext length is 0 and maximal padding is applied
3604 * - otherwise it must be in [0, 255] and is padding_length from RFC 5246:
3605 * it's the length of the rest of the padding, that is, excluding the
3606 * byte that encodes the length. The minimal non-zero plaintext length
3607 * that gives this padding_length is automatically selected.
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003608 */
3609 mbedtls_ssl_context ssl; /* ONLY for debugging */
3610 mbedtls_ssl_transform t0, t1;
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003611 mbedtls_record rec, rec_save;
3612 unsigned char *buf = NULL, *buf_save = NULL;
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003613 size_t buflen, olen = 0;
Manuel Pégourié-Gonnard864abbf2020-07-21 10:37:14 +02003614 size_t plaintext_len, block_size, i;
Manuel Pégourié-Gonnarde55653f2020-07-22 11:42:57 +02003615 unsigned char padlen; /* excluding the padding_length byte */
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003616 unsigned char add_data[13];
3617 unsigned char mac[MBEDTLS_MD_MAX_SIZE];
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003618 int exp_ret;
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01003619 int ret;
Manuel Pégourié-Gonnard4adc04a2020-07-16 10:00:48 +02003620 const unsigned char pad_max_len = 255; /* Per the standard */
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003621
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01003622 USE_PSA_INIT( );
3623
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003624 mbedtls_ssl_init( &ssl );
3625 mbedtls_ssl_transform_init( &t0 );
3626 mbedtls_ssl_transform_init( &t1 );
3627
3628 /* Set up transforms with dummy keys */
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01003629 ret = build_transforms( &t0, &t1, cipher_type, hash_id,
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003630 0, trunc_hmac,
3631 MBEDTLS_SSL_MINOR_VERSION_3,
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01003632 0 , 0 );
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003633
Przemyslaw Stekiel4a36dd32022-01-25 00:43:58 +01003634 TEST_ASSERT( ret == 0 );
3635
Manuel Pégourié-Gonnard864abbf2020-07-21 10:37:14 +02003636 /* Determine padding/plaintext length */
3637 TEST_ASSERT( length_selector >= -2 && length_selector <= 255 );
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003638 block_size = t0.ivlen;
Manuel Pégourié-Gonnard864abbf2020-07-21 10:37:14 +02003639 if( length_selector < 0 )
3640 {
3641 plaintext_len = 0;
3642
Manuel Pégourié-Gonnarde55653f2020-07-22 11:42:57 +02003643 /* Minimal padding
3644 * The +1 is for the padding_length byte, not counted in padlen. */
Manuel Pégourié-Gonnard864abbf2020-07-21 10:37:14 +02003645 padlen = block_size - ( t0.maclen + 1 ) % block_size;
3646
3647 /* Maximal padding? */
3648 if( length_selector == -2 )
3649 padlen += block_size * ( ( pad_max_len - padlen ) / block_size );
3650 }
3651 else
3652 {
3653 padlen = length_selector;
3654
Manuel Pégourié-Gonnarde55653f2020-07-22 11:42:57 +02003655 /* Minimal non-zero plaintext_length giving desired padding.
3656 * The +1 is for the padding_length byte, not counted in padlen. */
Manuel Pégourié-Gonnard864abbf2020-07-21 10:37:14 +02003657 plaintext_len = block_size - ( padlen + t0.maclen + 1 ) % block_size;
3658 }
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003659
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003660 /* Prepare a buffer for record data */
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003661 buflen = block_size
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003662 + plaintext_len
3663 + t0.maclen
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003664 + padlen + 1;
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003665 ASSERT_ALLOC( buf, buflen );
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003666 ASSERT_ALLOC( buf_save, buflen );
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003667
3668 /* Prepare a dummy record header */
3669 memset( rec.ctr, 0, sizeof( rec.ctr ) );
3670 rec.type = MBEDTLS_SSL_MSG_APPLICATION_DATA;
3671 rec.ver[0] = MBEDTLS_SSL_MAJOR_VERSION_3;
3672 rec.ver[1] = MBEDTLS_SSL_MINOR_VERSION_3;
3673#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3674 rec.cid_len = 0;
3675#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3676
3677 /* Prepare dummy record content */
3678 rec.buf = buf;
3679 rec.buf_len = buflen;
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003680 rec.data_offset = block_size;
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003681 rec.data_len = plaintext_len;
3682 memset( rec.buf + rec.data_offset, 42, rec.data_len );
3683
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003684 /* Serialized version of record header for MAC purposes */
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003685 memcpy( add_data, rec.ctr, 8 );
3686 add_data[8] = rec.type;
3687 add_data[9] = rec.ver[0];
3688 add_data[10] = rec.ver[1];
3689 add_data[11] = ( rec.data_len >> 8 ) & 0xff;
3690 add_data[12] = ( rec.data_len >> 0 ) & 0xff;
3691
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003692 /* Set dummy IV */
3693 memset( t0.iv_enc, 0x55, t0.ivlen );
3694 memcpy( rec.buf, t0.iv_enc, t0.ivlen );
3695
3696 /*
3697 * Prepare a pre-encryption record (with MAC and padding), and save it.
3698 */
3699
3700 /* MAC with additional data */
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003701 TEST_EQUAL( 0, mbedtls_md_hmac_update( &t0.md_ctx_enc, add_data, 13 ) );
3702 TEST_EQUAL( 0, mbedtls_md_hmac_update( &t0.md_ctx_enc,
3703 rec.buf + rec.data_offset,
3704 rec.data_len ) );
3705 TEST_EQUAL( 0, mbedtls_md_hmac_finish( &t0.md_ctx_enc, mac ) );
3706
3707 memcpy( rec.buf + rec.data_offset + rec.data_len, mac, t0.maclen );
3708 rec.data_len += t0.maclen;
3709
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003710 /* Pad */
3711 memset( rec.buf + rec.data_offset + rec.data_len, padlen, padlen + 1 );
3712 rec.data_len += padlen + 1;
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003713
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003714 /* Save correct pre-encryption record */
3715 rec_save = rec;
3716 rec_save.buf = buf_save;
3717 memcpy( buf_save, buf, buflen );
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003718
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003719 /*
3720 * Encrypt and decrypt the correct record, expecting success
3721 */
Przemyslaw Stekielf4facef2022-02-02 21:31:04 +01003722#if defined(MBEDTLS_USE_PSA_CRYPTO)
3723 TEST_EQUAL( 0, psa_cipher_encrypt_helper(&t0, t0.iv_enc, t0.ivlen,
3724 rec.buf + rec.data_offset, rec.data_len,
3725 rec.buf + rec.data_offset, &olen ) );
3726#else
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003727 TEST_EQUAL( 0, mbedtls_cipher_crypt( &t0.cipher_ctx_enc,
3728 t0.iv_enc, t0.ivlen,
3729 rec.buf + rec.data_offset, rec.data_len,
3730 rec.buf + rec.data_offset, &olen ) );
Przemyslaw Stekielf4facef2022-02-02 21:31:04 +01003731#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003732 rec.data_offset -= t0.ivlen;
3733 rec.data_len += t0.ivlen;
3734
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003735 TEST_EQUAL( 0, mbedtls_ssl_decrypt_buf( &ssl, &t1, &rec ) );
3736
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003737 /*
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003738 * Modify each byte of the pre-encryption record before encrypting and
3739 * decrypting it, expecting failure every time.
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003740 */
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003741 for( i = block_size; i < buflen; i++ )
3742 {
Chris Jones9634bb12021-01-20 15:56:42 +00003743 mbedtls_test_set_step( i );
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003744
3745 /* Restore correct pre-encryption record */
3746 rec = rec_save;
3747 rec.buf = buf;
3748 memcpy( buf, buf_save, buflen );
3749
Manuel Pégourié-Gonnardb51f0442020-07-21 10:40:25 +02003750 /* Corrupt one byte of the data (could be plaintext, MAC or padding) */
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003751 rec.buf[i] ^= 0x01;
3752
3753 /* Encrypt */
Przemyslaw Stekielf4facef2022-02-02 21:31:04 +01003754#if defined(MBEDTLS_USE_PSA_CRYPTO)
3755 TEST_EQUAL( 0, psa_cipher_encrypt_helper(&t0, t0.iv_enc, t0.ivlen,
3756 rec.buf + rec.data_offset, rec.data_len,
3757 rec.buf + rec.data_offset, &olen ) );
3758#else
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003759 TEST_EQUAL( 0, mbedtls_cipher_crypt( &t0.cipher_ctx_enc,
3760 t0.iv_enc, t0.ivlen,
3761 rec.buf + rec.data_offset, rec.data_len,
3762 rec.buf + rec.data_offset, &olen ) );
Przemyslaw Stekielf4facef2022-02-02 21:31:04 +01003763#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003764 rec.data_offset -= t0.ivlen;
3765 rec.data_len += t0.ivlen;
3766
3767 /* Decrypt and expect failure */
3768 TEST_EQUAL( MBEDTLS_ERR_SSL_INVALID_MAC,
3769 mbedtls_ssl_decrypt_buf( &ssl, &t1, &rec ) );
3770 }
3771
3772 /*
3773 * Use larger values of the padding bytes - with small buffers, this tests
3774 * the case where the announced padlen would be larger than the buffer
3775 * (and before that, than the buffer minus the size of the MAC), to make
3776 * sure our padding checking code does not perform any out-of-bounds reads
3777 * in this case. (With larger buffers, ie when the plaintext is long or
3778 * maximal length padding is used, this is less relevant but still doesn't
3779 * hurt to test.)
3780 *
3781 * (Start the loop with correct padding, just to double-check that record
3782 * saving did work, and that we're overwriting the correct bytes.)
3783 */
Manuel Pégourié-Gonnard4adc04a2020-07-16 10:00:48 +02003784 for( i = padlen; i <= pad_max_len; i++ )
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003785 {
Chris Jones9634bb12021-01-20 15:56:42 +00003786 mbedtls_test_set_step( i );
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003787
3788 /* Restore correct pre-encryption record */
3789 rec = rec_save;
3790 rec.buf = buf;
3791 memcpy( buf, buf_save, buflen );
3792
3793 /* Set padding bytes to new value */
3794 memset( buf + buflen - padlen - 1, i, padlen + 1 );
3795
3796 /* Encrypt */
Przemyslaw Stekielf4facef2022-02-02 21:31:04 +01003797#if defined(MBEDTLS_USE_PSA_CRYPTO)
3798 TEST_EQUAL( 0, psa_cipher_encrypt_helper(&t0, t0.iv_enc, t0.ivlen,
3799 rec.buf + rec.data_offset, rec.data_len,
3800 rec.buf + rec.data_offset, &olen ) );
3801#else
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003802 TEST_EQUAL( 0, mbedtls_cipher_crypt( &t0.cipher_ctx_enc,
3803 t0.iv_enc, t0.ivlen,
3804 rec.buf + rec.data_offset, rec.data_len,
3805 rec.buf + rec.data_offset, &olen ) );
Przemyslaw Stekielf4facef2022-02-02 21:31:04 +01003806#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003807 rec.data_offset -= t0.ivlen;
3808 rec.data_len += t0.ivlen;
3809
3810 /* Decrypt and expect failure except the first time */
3811 exp_ret = ( i == padlen ) ? 0 : MBEDTLS_ERR_SSL_INVALID_MAC;
3812 TEST_EQUAL( exp_ret, mbedtls_ssl_decrypt_buf( &ssl, &t1, &rec ) );
3813 }
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003814
3815exit:
3816 mbedtls_ssl_free( &ssl );
3817 mbedtls_ssl_transform_free( &t0 );
3818 mbedtls_ssl_transform_free( &t1 );
3819 mbedtls_free( buf );
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003820 mbedtls_free( buf_save );
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01003821 USE_PSA_DONE( );
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003822}
3823/* END_CASE */
3824
Ronald Cron6f135e12021-12-08 16:57:54 +01003825/* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00003826void ssl_tls13_hkdf_expand_label( int hash_alg,
3827 data_t *secret,
3828 int label_idx,
3829 data_t *ctx,
3830 int desired_length,
3831 data_t *expected )
Hanno Becker39ff4922020-08-21 13:36:56 +01003832{
3833 unsigned char dst[ 100 ];
3834
Hanno Becker70d7fb02020-09-09 10:11:21 +01003835 unsigned char const *lbl = NULL;
3836 size_t lbl_len;
Xiaofei Baid25fab62021-12-02 06:36:27 +00003837#define MBEDTLS_SSL_TLS1_3_LABEL( name, string ) \
3838 if( label_idx == (int) tls13_label_ ## name ) \
3839 { \
Xiaofei Bai746f9482021-11-12 08:53:56 +00003840 lbl = mbedtls_ssl_tls13_labels.name; \
3841 lbl_len = sizeof( mbedtls_ssl_tls13_labels.name ); \
Hanno Becker70d7fb02020-09-09 10:11:21 +01003842 }
3843MBEDTLS_SSL_TLS1_3_LABEL_LIST
3844#undef MBEDTLS_SSL_TLS1_3_LABEL
3845 TEST_ASSERT( lbl != NULL );
Hanno Becker39ff4922020-08-21 13:36:56 +01003846
3847 /* Check sanity of test parameters. */
3848 TEST_ASSERT( (size_t) desired_length <= sizeof(dst) );
3849 TEST_ASSERT( (size_t) desired_length == expected->len );
3850
Xiaofei Bai746f9482021-11-12 08:53:56 +00003851 TEST_ASSERT( mbedtls_ssl_tls13_hkdf_expand_label(
Hanno Becker39ff4922020-08-21 13:36:56 +01003852 (mbedtls_md_type_t) hash_alg,
3853 secret->x, secret->len,
Hanno Becker70d7fb02020-09-09 10:11:21 +01003854 lbl, lbl_len,
Hanno Becker39ff4922020-08-21 13:36:56 +01003855 ctx->x, ctx->len,
3856 dst, desired_length ) == 0 );
3857
Hanno Beckerfb080962020-09-08 10:58:42 +01003858 ASSERT_COMPARE( dst, (size_t) desired_length,
3859 expected->x, (size_t) expected->len );
Hanno Becker39ff4922020-08-21 13:36:56 +01003860}
3861/* END_CASE */
3862
Ronald Cron6f135e12021-12-08 16:57:54 +01003863/* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00003864void ssl_tls13_traffic_key_generation( int hash_alg,
3865 data_t *server_secret,
3866 data_t *client_secret,
3867 int desired_iv_len,
3868 int desired_key_len,
3869 data_t *expected_server_write_key,
3870 data_t *expected_server_write_iv,
3871 data_t *expected_client_write_key,
3872 data_t *expected_client_write_iv )
Hanno Becker19498f82020-08-21 13:37:08 +01003873{
3874 mbedtls_ssl_key_set keys;
3875
3876 /* Check sanity of test parameters. */
3877 TEST_ASSERT( client_secret->len == server_secret->len );
3878 TEST_ASSERT( expected_client_write_iv->len == expected_server_write_iv->len &&
3879 expected_client_write_iv->len == (size_t) desired_iv_len );
3880 TEST_ASSERT( expected_client_write_key->len == expected_server_write_key->len &&
3881 expected_client_write_key->len == (size_t) desired_key_len );
3882
Xiaofei Bai746f9482021-11-12 08:53:56 +00003883 TEST_ASSERT( mbedtls_ssl_tls13_make_traffic_keys(
Hanno Becker19498f82020-08-21 13:37:08 +01003884 (mbedtls_md_type_t) hash_alg,
3885 client_secret->x,
3886 server_secret->x,
3887 client_secret->len /* == server_secret->len */,
3888 desired_key_len, desired_iv_len,
3889 &keys ) == 0 );
3890
Hanno Beckerfb080962020-09-08 10:58:42 +01003891 ASSERT_COMPARE( keys.client_write_key,
Hanno Becker493ea7f2020-09-08 11:01:00 +01003892 keys.key_len,
Hanno Beckerfb080962020-09-08 10:58:42 +01003893 expected_client_write_key->x,
3894 (size_t) desired_key_len );
3895 ASSERT_COMPARE( keys.server_write_key,
Hanno Becker493ea7f2020-09-08 11:01:00 +01003896 keys.key_len,
Hanno Beckerfb080962020-09-08 10:58:42 +01003897 expected_server_write_key->x,
3898 (size_t) desired_key_len );
3899 ASSERT_COMPARE( keys.client_write_iv,
Hanno Becker493ea7f2020-09-08 11:01:00 +01003900 keys.iv_len,
Hanno Beckerfb080962020-09-08 10:58:42 +01003901 expected_client_write_iv->x,
3902 (size_t) desired_iv_len );
3903 ASSERT_COMPARE( keys.server_write_iv,
Hanno Becker493ea7f2020-09-08 11:01:00 +01003904 keys.iv_len,
Hanno Beckerfb080962020-09-08 10:58:42 +01003905 expected_server_write_iv->x,
3906 (size_t) desired_iv_len );
Hanno Becker19498f82020-08-21 13:37:08 +01003907}
3908/* END_CASE */
3909
Ronald Cron6f135e12021-12-08 16:57:54 +01003910/* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00003911void ssl_tls13_derive_secret( int hash_alg,
3912 data_t *secret,
3913 int label_idx,
3914 data_t *ctx,
3915 int desired_length,
3916 int already_hashed,
3917 data_t *expected )
Hanno Beckere4849d12020-08-21 14:14:14 +01003918{
3919 unsigned char dst[ 100 ];
3920
Hanno Becker70d7fb02020-09-09 10:11:21 +01003921 unsigned char const *lbl = NULL;
3922 size_t lbl_len;
Xiaofei Baid25fab62021-12-02 06:36:27 +00003923#define MBEDTLS_SSL_TLS1_3_LABEL( name, string ) \
3924 if( label_idx == (int) tls13_label_ ## name ) \
3925 { \
Xiaofei Bai746f9482021-11-12 08:53:56 +00003926 lbl = mbedtls_ssl_tls13_labels.name; \
3927 lbl_len = sizeof( mbedtls_ssl_tls13_labels.name ); \
Hanno Becker70d7fb02020-09-09 10:11:21 +01003928 }
3929MBEDTLS_SSL_TLS1_3_LABEL_LIST
3930#undef MBEDTLS_SSL_TLS1_3_LABEL
3931 TEST_ASSERT( lbl != NULL );
3932
Hanno Beckere4849d12020-08-21 14:14:14 +01003933 /* Check sanity of test parameters. */
3934 TEST_ASSERT( (size_t) desired_length <= sizeof(dst) );
3935 TEST_ASSERT( (size_t) desired_length == expected->len );
3936
Xiaofei Bai746f9482021-11-12 08:53:56 +00003937 TEST_ASSERT( mbedtls_ssl_tls13_derive_secret(
Hanno Beckere4849d12020-08-21 14:14:14 +01003938 (mbedtls_md_type_t) hash_alg,
3939 secret->x, secret->len,
Hanno Becker70d7fb02020-09-09 10:11:21 +01003940 lbl, lbl_len,
Hanno Beckere4849d12020-08-21 14:14:14 +01003941 ctx->x, ctx->len,
3942 already_hashed,
3943 dst, desired_length ) == 0 );
3944
Hanno Beckerfb080962020-09-08 10:58:42 +01003945 ASSERT_COMPARE( dst, desired_length,
3946 expected->x, desired_length );
Hanno Beckere4849d12020-08-21 14:14:14 +01003947}
3948/* END_CASE */
3949
Ronald Cron6f135e12021-12-08 16:57:54 +01003950/* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00003951void ssl_tls13_derive_early_secrets( int hash_alg,
3952 data_t *secret,
3953 data_t *transcript,
3954 data_t *traffic_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_early_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 traffic_expected->len == md_size &&
3967 exporter_expected->len == md_size );
3968
Xiaofei Bai746f9482021-11-12 08:53:56 +00003969 TEST_ASSERT( mbedtls_ssl_tls13_derive_early_secrets(
Hanno Beckera4f40a02021-05-24 06:42:11 +01003970 md_type, secret->x, transcript->x, transcript->len,
3971 &secrets ) == 0 );
3972
3973 ASSERT_COMPARE( secrets.client_early_traffic_secret, md_size,
3974 traffic_expected->x, traffic_expected->len );
3975 ASSERT_COMPARE( secrets.early_exporter_master_secret, md_size,
3976 exporter_expected->x, exporter_expected->len );
3977}
3978/* END_CASE */
3979
Ronald Cron6f135e12021-12-08 16:57:54 +01003980/* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00003981void ssl_tls13_derive_handshake_secrets( int hash_alg,
3982 data_t *secret,
3983 data_t *transcript,
3984 data_t *client_expected,
3985 data_t *server_expected )
Hanno Beckera4f40a02021-05-24 06:42:11 +01003986{
Xiaofei Bai746f9482021-11-12 08:53:56 +00003987 mbedtls_ssl_tls13_handshake_secrets secrets;
Hanno Beckera4f40a02021-05-24 06:42:11 +01003988
3989 /* Double-check that we've passed sane parameters. */
3990 mbedtls_md_type_t md_type = (mbedtls_md_type_t) hash_alg;
3991 mbedtls_md_info_t const * const md_info = mbedtls_md_info_from_type( md_type );
3992 size_t const md_size = mbedtls_md_get_size( md_info );
3993 TEST_ASSERT( md_info != 0 &&
3994 secret->len == md_size &&
3995 transcript->len == md_size &&
3996 client_expected->len == md_size &&
3997 server_expected->len == md_size );
3998
Xiaofei Bai746f9482021-11-12 08:53:56 +00003999 TEST_ASSERT( mbedtls_ssl_tls13_derive_handshake_secrets(
Hanno Beckera4f40a02021-05-24 06:42:11 +01004000 md_type, secret->x, transcript->x, transcript->len,
4001 &secrets ) == 0 );
4002
4003 ASSERT_COMPARE( secrets.client_handshake_traffic_secret, md_size,
4004 client_expected->x, client_expected->len );
4005 ASSERT_COMPARE( secrets.server_handshake_traffic_secret, md_size,
4006 server_expected->x, server_expected->len );
4007}
4008/* END_CASE */
4009
Ronald Cron6f135e12021-12-08 16:57:54 +01004010/* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00004011void ssl_tls13_derive_application_secrets( int hash_alg,
4012 data_t *secret,
4013 data_t *transcript,
4014 data_t *client_expected,
4015 data_t *server_expected,
4016 data_t *exporter_expected )
Hanno Beckera4f40a02021-05-24 06:42:11 +01004017{
Xiaofei Bai746f9482021-11-12 08:53:56 +00004018 mbedtls_ssl_tls13_application_secrets secrets;
Hanno Beckera4f40a02021-05-24 06:42:11 +01004019
4020 /* Double-check that we've passed sane parameters. */
4021 mbedtls_md_type_t md_type = (mbedtls_md_type_t) hash_alg;
4022 mbedtls_md_info_t const * const md_info = mbedtls_md_info_from_type( md_type );
4023 size_t const md_size = mbedtls_md_get_size( md_info );
4024 TEST_ASSERT( md_info != 0 &&
4025 secret->len == md_size &&
4026 transcript->len == md_size &&
4027 client_expected->len == md_size &&
4028 server_expected->len == md_size &&
4029 exporter_expected->len == md_size );
4030
Xiaofei Bai746f9482021-11-12 08:53:56 +00004031 TEST_ASSERT( mbedtls_ssl_tls13_derive_application_secrets(
Hanno Beckera4f40a02021-05-24 06:42:11 +01004032 md_type, secret->x, transcript->x, transcript->len,
4033 &secrets ) == 0 );
4034
4035 ASSERT_COMPARE( secrets.client_application_traffic_secret_N, md_size,
4036 client_expected->x, client_expected->len );
4037 ASSERT_COMPARE( secrets.server_application_traffic_secret_N, md_size,
4038 server_expected->x, server_expected->len );
4039 ASSERT_COMPARE( secrets.exporter_master_secret, md_size,
4040 exporter_expected->x, exporter_expected->len );
4041}
4042/* END_CASE */
4043
Ronald Cron6f135e12021-12-08 16:57:54 +01004044/* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00004045void ssl_tls13_derive_resumption_secrets( int hash_alg,
4046 data_t *secret,
4047 data_t *transcript,
4048 data_t *resumption_expected )
Hanno Beckera4f40a02021-05-24 06:42:11 +01004049{
Xiaofei Bai746f9482021-11-12 08:53:56 +00004050 mbedtls_ssl_tls13_application_secrets secrets;
Hanno Beckera4f40a02021-05-24 06:42:11 +01004051
4052 /* Double-check that we've passed sane parameters. */
4053 mbedtls_md_type_t md_type = (mbedtls_md_type_t) hash_alg;
4054 mbedtls_md_info_t const * const md_info = mbedtls_md_info_from_type( md_type );
4055 size_t const md_size = mbedtls_md_get_size( md_info );
4056 TEST_ASSERT( md_info != 0 &&
4057 secret->len == md_size &&
4058 transcript->len == md_size &&
4059 resumption_expected->len == md_size );
4060
Xiaofei Bai746f9482021-11-12 08:53:56 +00004061 TEST_ASSERT( mbedtls_ssl_tls13_derive_resumption_master_secret(
Hanno Beckera4f40a02021-05-24 06:42:11 +01004062 md_type, secret->x, transcript->x, transcript->len,
4063 &secrets ) == 0 );
4064
4065 ASSERT_COMPARE( secrets.resumption_master_secret, md_size,
4066 resumption_expected->x, resumption_expected->len );
4067}
4068/* END_CASE */
4069
Ronald Cron6f135e12021-12-08 16:57:54 +01004070/* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00004071void ssl_tls13_create_psk_binder( int hash_alg,
4072 data_t *psk,
4073 int psk_type,
4074 data_t *transcript,
4075 data_t *binder_expected )
Hanno Becker55bc2c52021-05-24 06:53:52 +01004076{
4077 unsigned char binder[ MBEDTLS_MD_MAX_SIZE ];
4078
4079 /* Double-check that we've passed sane parameters. */
4080 mbedtls_md_type_t md_type = (mbedtls_md_type_t) hash_alg;
4081 mbedtls_md_info_t const * const md_info = mbedtls_md_info_from_type( md_type );
4082 size_t const md_size = mbedtls_md_get_size( md_info );
4083 TEST_ASSERT( md_info != 0 &&
4084 transcript->len == md_size &&
4085 binder_expected->len == md_size );
4086
Xiaofei Bai746f9482021-11-12 08:53:56 +00004087 TEST_ASSERT( mbedtls_ssl_tls13_create_psk_binder(
Hanno Becker55bc2c52021-05-24 06:53:52 +01004088 NULL, /* SSL context for debugging only */
4089 md_type,
4090 psk->x, psk->len,
4091 psk_type,
4092 transcript->x,
4093 binder ) == 0 );
4094
4095 ASSERT_COMPARE( binder, md_size,
4096 binder_expected->x, binder_expected->len );
4097}
4098/* END_CASE */
4099
Ronald Cron6f135e12021-12-08 16:57:54 +01004100/* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00004101void ssl_tls13_record_protection( int ciphersuite,
4102 int endpoint,
4103 int ctr,
4104 int padding_used,
4105 data_t *server_write_key,
4106 data_t *server_write_iv,
4107 data_t *client_write_key,
4108 data_t *client_write_iv,
4109 data_t *plaintext,
4110 data_t *ciphertext )
Hanno Beckera77d0052021-03-22 15:16:33 +00004111{
4112 mbedtls_ssl_key_set keys;
4113 mbedtls_ssl_transform transform_send;
4114 mbedtls_ssl_transform transform_recv;
4115 mbedtls_record rec;
4116 unsigned char *buf = NULL;
Hanno Becker1f918782021-08-01 19:18:28 +01004117 size_t buf_len;
Hanno Beckera77d0052021-03-22 15:16:33 +00004118 int other_endpoint;
4119
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01004120 USE_PSA_INIT( );
4121
Hanno Beckera77d0052021-03-22 15:16:33 +00004122 TEST_ASSERT( endpoint == MBEDTLS_SSL_IS_CLIENT ||
4123 endpoint == MBEDTLS_SSL_IS_SERVER );
4124
4125 if( endpoint == MBEDTLS_SSL_IS_SERVER )
4126 other_endpoint = MBEDTLS_SSL_IS_CLIENT;
4127 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4128 other_endpoint = MBEDTLS_SSL_IS_SERVER;
4129
4130 TEST_ASSERT( server_write_key->len == client_write_key->len );
4131 TEST_ASSERT( server_write_iv->len == client_write_iv->len );
4132
4133 memcpy( keys.client_write_key,
4134 client_write_key->x, client_write_key->len );
4135 memcpy( keys.client_write_iv,
4136 client_write_iv->x, client_write_iv->len );
4137 memcpy( keys.server_write_key,
4138 server_write_key->x, server_write_key->len );
4139 memcpy( keys.server_write_iv,
4140 server_write_iv->x, server_write_iv->len );
4141
4142 keys.key_len = server_write_key->len;
4143 keys.iv_len = server_write_iv->len;
4144
4145 mbedtls_ssl_transform_init( &transform_recv );
4146 mbedtls_ssl_transform_init( &transform_send );
4147
4148 TEST_ASSERT( mbedtls_ssl_tls13_populate_transform(
4149 &transform_send, endpoint,
4150 ciphersuite, &keys, NULL ) == 0 );
4151 TEST_ASSERT( mbedtls_ssl_tls13_populate_transform(
4152 &transform_recv, other_endpoint,
4153 ciphersuite, &keys, NULL ) == 0 );
4154
Hanno Becker1f918782021-08-01 19:18:28 +01004155 /* Make sure we have enough space in the buffer even if
4156 * we use more padding than the KAT. */
4157 buf_len = ciphertext->len + MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY;
4158 ASSERT_ALLOC( buf, buf_len );
Hanno Beckera77d0052021-03-22 15:16:33 +00004159 rec.type = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Hanno Becker41537452021-04-20 05:35:28 +01004160
4161 /* TLS 1.3 uses the version identifier from TLS 1.2 on the wire. */
Hanno Beckera77d0052021-03-22 15:16:33 +00004162 mbedtls_ssl_write_version( MBEDTLS_SSL_MAJOR_VERSION_3,
4163 MBEDTLS_SSL_MINOR_VERSION_3,
4164 MBEDTLS_SSL_TRANSPORT_STREAM,
4165 rec.ver );
4166
4167 /* Copy plaintext into record structure */
4168 rec.buf = buf;
Hanno Becker1f918782021-08-01 19:18:28 +01004169 rec.buf_len = buf_len;
Hanno Beckera77d0052021-03-22 15:16:33 +00004170 rec.data_offset = 0;
4171 TEST_ASSERT( plaintext->len <= ciphertext->len );
4172 memcpy( rec.buf + rec.data_offset, plaintext->x, plaintext->len );
4173 rec.data_len = plaintext->len;
4174#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4175 rec.cid_len = 0;
4176#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4177
4178 memset( &rec.ctr[0], 0, 8 );
4179 rec.ctr[7] = ctr;
4180
4181 TEST_ASSERT( mbedtls_ssl_encrypt_buf( NULL, &transform_send, &rec,
4182 NULL, NULL ) == 0 );
Hanno Becker1f918782021-08-01 19:18:28 +01004183
4184 if( padding_used == MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY )
4185 {
4186 ASSERT_COMPARE( rec.buf + rec.data_offset, rec.data_len,
4187 ciphertext->x, ciphertext->len );
4188 }
Hanno Beckera77d0052021-03-22 15:16:33 +00004189
4190 TEST_ASSERT( mbedtls_ssl_decrypt_buf( NULL, &transform_recv, &rec ) == 0 );
4191 ASSERT_COMPARE( rec.buf + rec.data_offset, rec.data_len,
4192 plaintext->x, plaintext->len );
4193
Hanno Becker80e760e2021-03-23 06:00:21 +00004194 mbedtls_free( buf );
Hanno Beckera77d0052021-03-22 15:16:33 +00004195 mbedtls_ssl_transform_free( &transform_send );
4196 mbedtls_ssl_transform_free( &transform_recv );
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01004197 USE_PSA_DONE( );
Hanno Beckera77d0052021-03-22 15:16:33 +00004198}
4199/* END_CASE */
4200
Ronald Cron6f135e12021-12-08 16:57:54 +01004201/* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00004202void ssl_tls13_key_evolution( int hash_alg,
4203 data_t *secret,
4204 data_t *input,
4205 data_t *expected )
Hanno Becker2d2c3eb2020-08-20 14:54:24 +01004206{
4207 unsigned char secret_new[ MBEDTLS_MD_MAX_SIZE ];
4208
Xiaofei Bai746f9482021-11-12 08:53:56 +00004209 TEST_ASSERT( mbedtls_ssl_tls13_evolve_secret(
Hanno Becker2d2c3eb2020-08-20 14:54:24 +01004210 (mbedtls_md_type_t) hash_alg,
4211 secret->len ? secret->x : NULL,
4212 input->len ? input->x : NULL, input->len,
4213 secret_new ) == 0 );
4214
Hanno Beckerfb080962020-09-08 10:58:42 +01004215 ASSERT_COMPARE( secret_new, (size_t) expected->len,
4216 expected->x, (size_t) expected->len );
Hanno Becker2d2c3eb2020-08-20 14:54:24 +01004217}
4218/* END_CASE */
4219
Ron Eldor824ad7b2019-05-13 14:09:00 +03004220/* BEGIN_CASE */
4221void ssl_tls_prf( int type, data_t * secret, data_t * random,
Ronald Cronac6ae352020-06-26 14:33:03 +02004222 char *label, data_t *result_str, int exp_ret )
Ron Eldor824ad7b2019-05-13 14:09:00 +03004223{
4224 unsigned char *output;
4225
Ronald Cronac6ae352020-06-26 14:33:03 +02004226 output = mbedtls_calloc( 1, result_str->len );
Ron Eldor824ad7b2019-05-13 14:09:00 +03004227 if( output == NULL )
4228 goto exit;
4229
Gilles Peskine9de97e22021-02-02 21:00:11 +01004230 USE_PSA_INIT( );
Ron Eldor6b9b1b82019-05-15 17:04:33 +03004231
Ron Eldor824ad7b2019-05-13 14:09:00 +03004232 TEST_ASSERT( mbedtls_ssl_tls_prf( type, secret->x, secret->len,
4233 label, random->x, random->len,
Ronald Cronac6ae352020-06-26 14:33:03 +02004234 output, result_str->len ) == exp_ret );
Ron Eldor824ad7b2019-05-13 14:09:00 +03004235
4236 if( exp_ret == 0 )
4237 {
Ronald Cronac6ae352020-06-26 14:33:03 +02004238 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
4239 result_str->len, result_str->len ) == 0 );
Ron Eldor824ad7b2019-05-13 14:09:00 +03004240 }
4241exit:
4242
4243 mbedtls_free( output );
Gilles Peskine1f186ff2021-02-02 21:04:06 +01004244 USE_PSA_DONE( );
Ron Eldor824ad7b2019-05-13 14:09:00 +03004245}
4246/* END_CASE */
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02004247
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02004248/* BEGIN_CASE */
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02004249void ssl_serialize_session_save_load( int ticket_len, char *crt_file )
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02004250{
4251 mbedtls_ssl_session original, restored;
4252 unsigned char *buf = NULL;
4253 size_t len;
4254
4255 /*
4256 * Test that a save-load pair is the identity
4257 */
4258
4259 mbedtls_ssl_session_init( &original );
4260 mbedtls_ssl_session_init( &restored );
4261
4262 /* Prepare a dummy session to work on */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004263 TEST_ASSERT( ssl_populate_session_tls12( &original, ticket_len, crt_file ) == 0 );
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02004264
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02004265 /* Serialize it */
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02004266 TEST_ASSERT( mbedtls_ssl_session_save( &original, NULL, 0, &len )
4267 == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4268 TEST_ASSERT( ( buf = mbedtls_calloc( 1, len ) ) != NULL );
4269 TEST_ASSERT( mbedtls_ssl_session_save( &original, buf, len, &len )
4270 == 0 );
4271
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02004272 /* Restore session from serialized data */
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02004273 TEST_ASSERT( mbedtls_ssl_session_load( &restored, buf, len) == 0 );
4274
4275 /*
4276 * Make sure both session structures are identical
4277 */
4278#if defined(MBEDTLS_HAVE_TIME)
4279 TEST_ASSERT( original.start == restored.start );
4280#endif
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004281 TEST_ASSERT( original.minor_ver == restored.minor_ver );
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02004282 TEST_ASSERT( original.ciphersuite == restored.ciphersuite );
4283 TEST_ASSERT( original.compression == restored.compression );
4284 TEST_ASSERT( original.id_len == restored.id_len );
4285 TEST_ASSERT( memcmp( original.id,
4286 restored.id, sizeof( original.id ) ) == 0 );
4287 TEST_ASSERT( memcmp( original.master,
4288 restored.master, sizeof( original.master ) ) == 0 );
4289
4290#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02004291#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02004292 TEST_ASSERT( ( original.peer_cert == NULL ) ==
4293 ( restored.peer_cert == NULL ) );
4294 if( original.peer_cert != NULL )
4295 {
4296 TEST_ASSERT( original.peer_cert->raw.len ==
4297 restored.peer_cert->raw.len );
4298 TEST_ASSERT( memcmp( original.peer_cert->raw.p,
4299 restored.peer_cert->raw.p,
4300 original.peer_cert->raw.len ) == 0 );
4301 }
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02004302#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4303 TEST_ASSERT( original.peer_cert_digest_type ==
4304 restored.peer_cert_digest_type );
4305 TEST_ASSERT( original.peer_cert_digest_len ==
4306 restored.peer_cert_digest_len );
4307 TEST_ASSERT( ( original.peer_cert_digest == NULL ) ==
4308 ( restored.peer_cert_digest == NULL ) );
4309 if( original.peer_cert_digest != NULL )
4310 {
4311 TEST_ASSERT( memcmp( original.peer_cert_digest,
4312 restored.peer_cert_digest,
4313 original.peer_cert_digest_len ) == 0 );
4314 }
4315#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4316#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02004317 TEST_ASSERT( original.verify_result == restored.verify_result );
4318
4319#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
4320 TEST_ASSERT( original.ticket_len == restored.ticket_len );
4321 if( original.ticket_len != 0 )
4322 {
4323 TEST_ASSERT( original.ticket != NULL );
4324 TEST_ASSERT( restored.ticket != NULL );
4325 TEST_ASSERT( memcmp( original.ticket,
4326 restored.ticket, original.ticket_len ) == 0 );
4327 }
4328 TEST_ASSERT( original.ticket_lifetime == restored.ticket_lifetime );
4329#endif
4330
4331#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
4332 TEST_ASSERT( original.mfl_code == restored.mfl_code );
4333#endif
4334
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02004335#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
4336 TEST_ASSERT( original.encrypt_then_mac == restored.encrypt_then_mac );
4337#endif
4338
4339exit:
4340 mbedtls_ssl_session_free( &original );
4341 mbedtls_ssl_session_free( &restored );
4342 mbedtls_free( buf );
4343}
4344/* END_CASE */
4345
Manuel Pégourié-Gonnardaa755832019-06-03 10:53:47 +02004346/* BEGIN_CASE */
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02004347void ssl_serialize_session_load_save( int ticket_len, char *crt_file )
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02004348{
4349 mbedtls_ssl_session session;
4350 unsigned char *buf1 = NULL, *buf2 = NULL;
4351 size_t len0, len1, len2;
4352
4353 /*
4354 * Test that a load-save pair is the identity
4355 */
4356
4357 mbedtls_ssl_session_init( &session );
4358
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02004359 /* Prepare a dummy session to work on */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004360 TEST_ASSERT( ssl_populate_session_tls12( &session, ticket_len, crt_file ) == 0 );
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02004361
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02004362 /* Get desired buffer size for serializing */
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02004363 TEST_ASSERT( mbedtls_ssl_session_save( &session, NULL, 0, &len0 )
4364 == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4365
4366 /* Allocate first buffer */
4367 buf1 = mbedtls_calloc( 1, len0 );
4368 TEST_ASSERT( buf1 != NULL );
4369
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02004370 /* Serialize to buffer and free live session */
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02004371 TEST_ASSERT( mbedtls_ssl_session_save( &session, buf1, len0, &len1 )
4372 == 0 );
4373 TEST_ASSERT( len0 == len1 );
4374 mbedtls_ssl_session_free( &session );
4375
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02004376 /* Restore session from serialized data */
Manuel Pégourié-Gonnard220403b2019-05-24 09:54:21 +02004377 TEST_ASSERT( mbedtls_ssl_session_load( &session, buf1, len1 ) == 0 );
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02004378
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02004379 /* Allocate second buffer and serialize to it */
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02004380 buf2 = mbedtls_calloc( 1, len0 );
Manuel Pégourié-Gonnardb4079902019-05-24 09:52:10 +02004381 TEST_ASSERT( buf2 != NULL );
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02004382 TEST_ASSERT( mbedtls_ssl_session_save( &session, buf2, len0, &len2 )
4383 == 0 );
4384
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02004385 /* Make sure both serialized versions are identical */
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02004386 TEST_ASSERT( len1 == len2 );
4387 TEST_ASSERT( memcmp( buf1, buf2, len1 ) == 0 );
4388
4389exit:
4390 mbedtls_ssl_session_free( &session );
4391 mbedtls_free( buf1 );
4392 mbedtls_free( buf2 );
4393}
4394/* END_CASE */
Manuel Pégourié-Gonnardf5fa0aa2019-05-23 10:38:11 +02004395
4396/* BEGIN_CASE */
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02004397void ssl_serialize_session_save_buf_size( int ticket_len, char *crt_file )
Manuel Pégourié-Gonnardf5fa0aa2019-05-23 10:38:11 +02004398{
4399 mbedtls_ssl_session session;
4400 unsigned char *buf = NULL;
4401 size_t good_len, bad_len, test_len;
4402
4403 /*
4404 * Test that session_save() fails cleanly on small buffers
4405 */
4406
4407 mbedtls_ssl_session_init( &session );
4408
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02004409 /* Prepare dummy session and get serialized size */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004410 TEST_ASSERT( ssl_populate_session_tls12( &session, ticket_len, crt_file ) == 0 );
Manuel Pégourié-Gonnardf5fa0aa2019-05-23 10:38:11 +02004411 TEST_ASSERT( mbedtls_ssl_session_save( &session, NULL, 0, &good_len )
4412 == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4413
4414 /* Try all possible bad lengths */
4415 for( bad_len = 1; bad_len < good_len; bad_len++ )
4416 {
4417 /* Allocate exact size so that asan/valgrind can detect any overwrite */
4418 mbedtls_free( buf );
4419 TEST_ASSERT( ( buf = mbedtls_calloc( 1, bad_len ) ) != NULL );
4420 TEST_ASSERT( mbedtls_ssl_session_save( &session, buf, bad_len,
4421 &test_len )
4422 == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4423 TEST_ASSERT( test_len == good_len );
4424 }
4425
4426exit:
4427 mbedtls_ssl_session_free( &session );
4428 mbedtls_free( buf );
4429}
4430/* END_CASE */
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02004431
4432/* BEGIN_CASE */
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02004433void ssl_serialize_session_load_buf_size( int ticket_len, char *crt_file )
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02004434{
4435 mbedtls_ssl_session session;
4436 unsigned char *good_buf = NULL, *bad_buf = NULL;
4437 size_t good_len, bad_len;
4438
4439 /*
4440 * Test that session_load() fails cleanly on small buffers
4441 */
4442
4443 mbedtls_ssl_session_init( &session );
4444
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02004445 /* Prepare serialized session data */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004446 TEST_ASSERT( ssl_populate_session_tls12( &session, ticket_len, crt_file ) == 0 );
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02004447 TEST_ASSERT( mbedtls_ssl_session_save( &session, NULL, 0, &good_len )
4448 == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4449 TEST_ASSERT( ( good_buf = mbedtls_calloc( 1, good_len ) ) != NULL );
4450 TEST_ASSERT( mbedtls_ssl_session_save( &session, good_buf, good_len,
4451 &good_len ) == 0 );
4452 mbedtls_ssl_session_free( &session );
4453
4454 /* Try all possible bad lengths */
4455 for( bad_len = 0; bad_len < good_len; bad_len++ )
4456 {
4457 /* Allocate exact size so that asan/valgrind can detect any overread */
4458 mbedtls_free( bad_buf );
4459 bad_buf = mbedtls_calloc( 1, bad_len ? bad_len : 1 );
4460 TEST_ASSERT( bad_buf != NULL );
4461 memcpy( bad_buf, good_buf, bad_len );
4462
4463 TEST_ASSERT( mbedtls_ssl_session_load( &session, bad_buf, bad_len )
4464 == MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4465 }
4466
4467exit:
4468 mbedtls_ssl_session_free( &session );
4469 mbedtls_free( good_buf );
4470 mbedtls_free( bad_buf );
4471}
4472/* END_CASE */
Hanno Becker861d0bb2019-05-21 16:39:30 +01004473
Hanno Becker363b6462019-05-29 12:44:28 +01004474/* BEGIN_CASE */
4475void ssl_session_serialize_version_check( int corrupt_major,
Hanno Becker861d0bb2019-05-21 16:39:30 +01004476 int corrupt_minor,
4477 int corrupt_patch,
4478 int corrupt_config )
4479{
Hanno Becker363b6462019-05-29 12:44:28 +01004480 unsigned char serialized_session[ 2048 ];
4481 size_t serialized_session_len;
Hanno Beckerfe1275e2019-05-29 12:45:21 +01004482 unsigned cur_byte;
Hanno Becker861d0bb2019-05-21 16:39:30 +01004483 mbedtls_ssl_session session;
Hanno Beckerfe1275e2019-05-29 12:45:21 +01004484 uint8_t should_corrupt_byte[] = { corrupt_major == 1,
4485 corrupt_minor == 1,
4486 corrupt_patch == 1,
4487 corrupt_config == 1,
4488 corrupt_config == 1 };
4489
Hanno Becker861d0bb2019-05-21 16:39:30 +01004490 mbedtls_ssl_session_init( &session );
Paul Elliott46a6c202021-12-09 18:16:13 +00004491 TEST_ASSERT( ssl_populate_session_tls12( &session, 0, NULL ) == 0 );
Hanno Becker861d0bb2019-05-21 16:39:30 +01004492
Hanno Beckerfe1275e2019-05-29 12:45:21 +01004493 /* Infer length of serialized session. */
Hanno Becker861d0bb2019-05-21 16:39:30 +01004494 TEST_ASSERT( mbedtls_ssl_session_save( &session,
Hanno Becker363b6462019-05-29 12:44:28 +01004495 serialized_session,
4496 sizeof( serialized_session ),
4497 &serialized_session_len ) == 0 );
Hanno Becker861d0bb2019-05-21 16:39:30 +01004498
Hanno Beckerfe1275e2019-05-29 12:45:21 +01004499 mbedtls_ssl_session_free( &session );
Hanno Becker861d0bb2019-05-21 16:39:30 +01004500
Hanno Beckerfe1275e2019-05-29 12:45:21 +01004501 /* Without any modification, we should be able to successfully
Hanno Becker363b6462019-05-29 12:44:28 +01004502 * de-serialize the session - double-check that. */
Hanno Becker861d0bb2019-05-21 16:39:30 +01004503 TEST_ASSERT( mbedtls_ssl_session_load( &session,
Hanno Becker363b6462019-05-29 12:44:28 +01004504 serialized_session,
4505 serialized_session_len ) == 0 );
Hanno Becker861d0bb2019-05-21 16:39:30 +01004506 mbedtls_ssl_session_free( &session );
4507
Hanno Beckerfe1275e2019-05-29 12:45:21 +01004508 /* Go through the bytes in the serialized session header and
4509 * corrupt them bit-by-bit. */
4510 for( cur_byte = 0; cur_byte < sizeof( should_corrupt_byte ); cur_byte++ )
Hanno Becker861d0bb2019-05-21 16:39:30 +01004511 {
Hanno Beckerfe1275e2019-05-29 12:45:21 +01004512 int cur_bit;
4513 unsigned char * const byte = &serialized_session[ cur_byte ];
4514
4515 if( should_corrupt_byte[ cur_byte ] == 0 )
4516 continue;
4517
4518 for( cur_bit = 0; cur_bit < CHAR_BIT; cur_bit++ )
4519 {
4520 unsigned char const corrupted_bit = 0x1u << cur_bit;
4521 /* Modify a single bit in the serialized session. */
4522 *byte ^= corrupted_bit;
4523
4524 /* Attempt to deserialize */
4525 TEST_ASSERT( mbedtls_ssl_session_load( &session,
4526 serialized_session,
4527 serialized_session_len ) ==
Hanno Beckerf9b33032019-06-03 12:58:39 +01004528 MBEDTLS_ERR_SSL_VERSION_MISMATCH );
Hanno Beckerfe1275e2019-05-29 12:45:21 +01004529
4530 /* Undo the change */
4531 *byte ^= corrupted_bit;
4532 }
Hanno Becker861d0bb2019-05-21 16:39:30 +01004533 }
4534
Hanno Becker861d0bb2019-05-21 16:39:30 +01004535}
4536/* END_CASE */
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01004537
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02004538/* 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 +01004539void mbedtls_endpoint_sanity( int endpoint_type )
4540{
4541 enum { BUFFSIZE = 1024 };
4542 mbedtls_endpoint ep;
4543 int ret = -1;
4544
Andrzej Kurek15daf502020-02-12 09:17:52 -05004545 ret = mbedtls_endpoint_init( NULL, endpoint_type, MBEDTLS_PK_RSA,
4546 NULL, NULL, NULL );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01004547 TEST_ASSERT( MBEDTLS_ERR_SSL_BAD_INPUT_DATA == ret );
4548
Andrzej Kurekb2980742020-02-02 19:25:26 -05004549 ret = mbedtls_endpoint_certificate_init( NULL, MBEDTLS_PK_RSA );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01004550 TEST_ASSERT( MBEDTLS_ERR_SSL_BAD_INPUT_DATA == ret );
4551
Andrzej Kurek15daf502020-02-12 09:17:52 -05004552 ret = mbedtls_endpoint_init( &ep, endpoint_type, MBEDTLS_PK_RSA,
4553 NULL, NULL, NULL );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01004554 TEST_ASSERT( ret == 0 );
4555
4556exit:
Andrzej Kurek15daf502020-02-12 09:17:52 -05004557 mbedtls_endpoint_free( &ep, NULL );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01004558}
4559/* END_CASE */
4560
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02004561/* 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 +01004562void move_handshake_to_state(int endpoint_type, int state, int need_pass)
4563{
4564 enum { BUFFSIZE = 1024 };
4565 mbedtls_endpoint base_ep, second_ep;
4566 int ret = -1;
4567
Andrzej Kurek15daf502020-02-12 09:17:52 -05004568 ret = mbedtls_endpoint_init( &base_ep, endpoint_type, MBEDTLS_PK_RSA,
4569 NULL, NULL, NULL );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01004570 TEST_ASSERT( ret == 0 );
4571
4572 ret = mbedtls_endpoint_init( &second_ep,
4573 ( endpoint_type == MBEDTLS_SSL_IS_SERVER ) ?
Andrzej Kurekb2980742020-02-02 19:25:26 -05004574 MBEDTLS_SSL_IS_CLIENT : MBEDTLS_SSL_IS_SERVER,
Andrzej Kurek15daf502020-02-12 09:17:52 -05004575 MBEDTLS_PK_RSA, NULL, NULL, NULL );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01004576 TEST_ASSERT( ret == 0 );
4577
4578 ret = mbedtls_mock_socket_connect( &(base_ep.socket),
4579 &(second_ep.socket),
4580 BUFFSIZE );
4581 TEST_ASSERT( ret == 0 );
4582
4583 ret = mbedtls_move_handshake_to_state( &(base_ep.ssl),
4584 &(second_ep.ssl),
4585 state );
4586 if( need_pass )
4587 {
4588 TEST_ASSERT( ret == 0 );
4589 TEST_ASSERT( base_ep.ssl.state == state );
4590 }
4591 else
4592 {
4593 TEST_ASSERT( ret != 0 );
4594 TEST_ASSERT( base_ep.ssl.state != state );
4595 }
4596
4597exit:
Andrzej Kurek15daf502020-02-12 09:17:52 -05004598 mbedtls_endpoint_free( &base_ep, NULL );
4599 mbedtls_endpoint_free( &second_ep, NULL );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01004600}
4601/* END_CASE */
Andrzej Kurekf40daa32020-02-04 09:00:01 -05004602
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02004603/* 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 +01004604void handshake_version( int dtls, int client_min_version, int client_max_version,
4605 int server_min_version, int server_max_version,
4606 int expected_negotiated_version )
Andrzej Kurekf40daa32020-02-04 09:00:01 -05004607{
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004608 handshake_test_options options;
4609 init_handshake_options( &options );
Andrzej Kurekda2b6782020-02-12 07:56:36 -05004610
Paul Elliottc8570442020-04-15 17:00:50 +01004611 options.client_min_version = client_min_version;
4612 options.client_max_version = client_max_version;
4613 options.server_min_version = server_min_version;
4614 options.server_max_version = server_max_version;
4615
4616 options.expected_negotiated_version = expected_negotiated_version;
4617
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004618 options.dtls = dtls;
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004619 perform_handshake( &options );
Andrzej Kurekf40daa32020-02-04 09:00:01 -05004620
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004621 /* The goto below is used to avoid an "unused label" warning.*/
4622 goto exit;
4623}
4624/* END_CASE */
Andrzej Kurek9e9efdc2020-02-26 05:25:23 -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_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004627void handshake_psk_cipher( char* cipher, int pk_alg, data_t *psk_str, int dtls )
4628{
4629 handshake_test_options options;
4630 init_handshake_options( &options );
Andrzej Kurekf40daa32020-02-04 09:00:01 -05004631
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004632 options.cipher = cipher;
4633 options.dtls = dtls;
4634 options.psk_str = psk_str;
4635 options.pk_alg = pk_alg;
Andrzej Kurekcc5169c2020-02-04 09:04:56 -05004636
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004637 perform_handshake( &options );
Andrzej Kurek316da1f2020-02-26 09:03:47 -05004638
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004639 /* The goto below is used to avoid an "unused label" warning.*/
4640 goto exit;
4641}
4642/* END_CASE */
Andrzej Kurek316da1f2020-02-26 09:03:47 -05004643
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02004644/* 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 -05004645void handshake_cipher( char* cipher, int pk_alg, int dtls )
4646{
4647 test_handshake_psk_cipher( cipher, pk_alg, NULL, dtls );
Andrzej Kurekf40daa32020-02-04 09:00:01 -05004648
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004649 /* The goto below is used to avoid an "unused label" warning.*/
4650 goto exit;
4651}
4652/* END_CASE */
Andrzej Kurekf40daa32020-02-04 09:00:01 -05004653
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02004654/* 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 -05004655void app_data( int mfl, int cli_msg_len, int srv_msg_len,
4656 int expected_cli_fragments,
4657 int expected_srv_fragments, int dtls )
4658{
4659 handshake_test_options options;
4660 init_handshake_options( &options );
Andrzej Kurekda2b6782020-02-12 07:56:36 -05004661
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004662 options.mfl = mfl;
4663 options.cli_msg_len = cli_msg_len;
4664 options.srv_msg_len = srv_msg_len;
4665 options.expected_cli_fragments = expected_cli_fragments;
4666 options.expected_srv_fragments = expected_srv_fragments;
4667 options.dtls = dtls;
Andrzej Kurekda2b6782020-02-12 07:56:36 -05004668
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004669 perform_handshake( &options );
4670 /* The goto below is used to avoid an "unused label" warning.*/
4671 goto exit;
4672}
4673/* END_CASE */
Andrzej Kurekda2b6782020-02-12 07:56:36 -05004674
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02004675/* 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 -05004676void app_data_tls( int mfl, int cli_msg_len, int srv_msg_len,
4677 int expected_cli_fragments,
4678 int expected_srv_fragments )
4679{
4680 test_app_data( mfl, cli_msg_len, srv_msg_len, expected_cli_fragments,
4681 expected_srv_fragments, 0 );
4682 /* The goto below is used to avoid an "unused label" warning.*/
4683 goto exit;
4684}
4685/* END_CASE */
Andrzej Kurekda2b6782020-02-12 07:56:36 -05004686
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02004687/* 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 -05004688void app_data_dtls( int mfl, int cli_msg_len, int srv_msg_len,
4689 int expected_cli_fragments,
4690 int expected_srv_fragments )
4691{
4692 test_app_data( mfl, cli_msg_len, srv_msg_len, expected_cli_fragments,
4693 expected_srv_fragments, 1 );
4694 /* The goto below is used to avoid an "unused label" warning.*/
4695 goto exit;
4696}
4697/* END_CASE */
Andrzej Kurekda2b6782020-02-12 07:56:36 -05004698
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02004699/* 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 -05004700void handshake_serialization( )
4701{
4702 handshake_test_options options;
4703 init_handshake_options( &options );
Andrzej Kurekda2b6782020-02-12 07:56:36 -05004704
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004705 options.serialize = 1;
4706 options.dtls = 1;
4707 perform_handshake( &options );
4708 /* The goto below is used to avoid an "unused label" warning.*/
4709 goto exit;
4710}
4711/* END_CASE */
Andrzej Kurekda2b6782020-02-12 07:56:36 -05004712
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02004713/* 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 +01004714void handshake_fragmentation( int mfl, int expected_srv_hs_fragmentation, int expected_cli_hs_fragmentation)
4715{
4716 handshake_test_options options;
4717 log_pattern srv_pattern, cli_pattern;
4718
4719 srv_pattern.pattern = cli_pattern.pattern = "found fragmented DTLS handshake";
4720 srv_pattern.counter = 0;
4721 cli_pattern.counter = 0;
4722
4723 init_handshake_options( &options );
4724 options.dtls = 1;
4725 options.mfl = mfl;
Darryl Greenaad82f92019-12-02 10:53:11 +00004726 /* Set cipher to one using CBC so that record splitting can be tested */
4727 options.cipher = "TLS-DHE-RSA-WITH-AES-256-CBC-SHA256";
Piotr Nowickibde7ee82020-02-21 10:59:50 +01004728 options.srv_auth_mode = MBEDTLS_SSL_VERIFY_REQUIRED;
4729 options.srv_log_obj = &srv_pattern;
4730 options.cli_log_obj = &cli_pattern;
4731 options.srv_log_fun = log_analyzer;
4732 options.cli_log_fun = log_analyzer;
4733
4734 perform_handshake( &options );
4735
4736 /* Test if the server received a fragmented handshake */
4737 if( expected_srv_hs_fragmentation )
4738 {
4739 TEST_ASSERT( srv_pattern.counter >= 1 );
4740 }
4741 /* Test if the client received a fragmented handshake */
4742 if( expected_cli_hs_fragmentation )
4743 {
4744 TEST_ASSERT( cli_pattern.counter >= 1 );
4745 }
4746}
4747/* END_CASE */
4748
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02004749/* 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 -05004750void renegotiation( int legacy_renegotiation )
4751{
4752 handshake_test_options options;
4753 init_handshake_options( &options );
Andrzej Kurekda2b6782020-02-12 07:56:36 -05004754
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004755 options.renegotiate = 1;
4756 options.legacy_renegotiation = legacy_renegotiation;
4757 options.dtls = 1;
Andrzej Kurek316da1f2020-02-26 09:03:47 -05004758
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004759 perform_handshake( &options );
4760 /* The goto below is used to avoid an "unused label" warning.*/
4761 goto exit;
Andrzej Kurekf40daa32020-02-04 09:00:01 -05004762}
4763/* END_CASE */
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004764
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02004765/* 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 -05004766void resize_buffers( int mfl, int renegotiation, int legacy_renegotiation,
Andrzej Kurek8ea68722020-04-03 06:40:47 -04004767 int serialize, int dtls, char *cipher )
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004768{
4769 handshake_test_options options;
4770 init_handshake_options( &options );
4771
4772 options.mfl = mfl;
Andrzej Kurek8ea68722020-04-03 06:40:47 -04004773 options.cipher = cipher;
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004774 options.renegotiate = renegotiation;
4775 options.legacy_renegotiation = legacy_renegotiation;
4776 options.serialize = serialize;
4777 options.dtls = dtls;
4778 options.resize_buffers = 1;
4779
4780 perform_handshake( &options );
4781 /* The goto below is used to avoid an "unused label" warning.*/
4782 goto exit;
4783}
4784/* END_CASE */
4785
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02004786/* 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 -05004787void resize_buffers_serialize_mfl( int mfl )
4788{
Andrzej Kurek8ea68722020-04-03 06:40:47 -04004789 test_resize_buffers( mfl, 0, MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION, 1, 1,
4790 (char *) "" );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004791
4792 /* The goto below is used to avoid an "unused label" warning.*/
4793 goto exit;
4794}
4795/* END_CASE */
4796
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02004797/* 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 -04004798void resize_buffers_renegotiate_mfl( int mfl, int legacy_renegotiation,
4799 char *cipher )
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004800{
Andrzej Kurek8ea68722020-04-03 06:40:47 -04004801 test_resize_buffers( mfl, 1, legacy_renegotiation, 0, 1, cipher );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004802
4803 /* The goto below is used to avoid an "unused label" warning.*/
4804 goto exit;
4805}
4806/* END_CASE */
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02004807
Manuel Pégourié-Gonnarded0e8642020-07-21 11:20:30 +02004808/* BEGIN_CASE depends_on:MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC:MBEDTLS_TEST_HOOKS */
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02004809void ssl_cf_hmac( int hash )
4810{
4811 /*
Gabor Mezei90437e32021-10-20 11:59:27 +02004812 * Test the function mbedtls_ct_hmac() against a reference
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02004813 * implementation.
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02004814 */
4815 mbedtls_md_context_t ctx, ref_ctx;
4816 const mbedtls_md_info_t *md_info;
4817 size_t out_len, block_size;
4818 size_t min_in_len, in_len, max_in_len, i;
4819 /* TLS additional data is 13 bytes (hence the "lucky 13" name) */
4820 unsigned char add_data[13];
4821 unsigned char ref_out[MBEDTLS_MD_MAX_SIZE];
4822 unsigned char *data = NULL;
4823 unsigned char *out = NULL;
4824 unsigned char rec_num = 0;
4825
4826 mbedtls_md_init( &ctx );
4827 mbedtls_md_init( &ref_ctx );
4828
4829 md_info = mbedtls_md_info_from_type( hash );
4830 TEST_ASSERT( md_info != NULL );
4831 out_len = mbedtls_md_get_size( md_info );
4832 TEST_ASSERT( out_len != 0 );
4833 block_size = hash == MBEDTLS_MD_SHA384 ? 128 : 64;
4834
4835 /* Use allocated out buffer to catch overwrites */
4836 ASSERT_ALLOC( out, out_len );
4837
4838 /* Set up contexts with the given hash and a dummy key */
4839 TEST_EQUAL( 0, mbedtls_md_setup( &ctx, md_info, 1 ) );
4840 TEST_EQUAL( 0, mbedtls_md_setup( &ref_ctx, md_info, 1 ) );
4841 memset( ref_out, 42, sizeof( ref_out ) );
4842 TEST_EQUAL( 0, mbedtls_md_hmac_starts( &ctx, ref_out, out_len ) );
4843 TEST_EQUAL( 0, mbedtls_md_hmac_starts( &ref_ctx, ref_out, out_len ) );
4844 memset( ref_out, 0, sizeof( ref_out ) );
4845
4846 /*
4847 * Test all possible lengths up to a point. The difference between
4848 * max_in_len and min_in_len is at most 255, and make sure they both vary
4849 * by at least one block size.
4850 */
4851 for( max_in_len = 0; max_in_len <= 255 + block_size; max_in_len++ )
4852 {
Chris Jones9634bb12021-01-20 15:56:42 +00004853 mbedtls_test_set_step( max_in_len * 10000 );
Manuel Pégourié-Gonnardca8287c2020-07-22 10:29:39 +02004854
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02004855 /* Use allocated in buffer to catch overreads */
Manuel Pégourié-Gonnardc3219002020-07-22 10:32:52 +02004856 ASSERT_ALLOC( data, max_in_len );
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02004857
4858 min_in_len = max_in_len > 255 ? max_in_len - 255 : 0;
4859 for( in_len = min_in_len; in_len <= max_in_len; in_len++ )
4860 {
Chris Jones9634bb12021-01-20 15:56:42 +00004861 mbedtls_test_set_step( max_in_len * 10000 + in_len );
Manuel Pégourié-Gonnardca8287c2020-07-22 10:29:39 +02004862
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02004863 /* Set up dummy data and add_data */
4864 rec_num++;
4865 memset( add_data, rec_num, sizeof( add_data ) );
4866 for( i = 0; i < in_len; i++ )
4867 data[i] = ( i & 0xff ) ^ rec_num;
4868
4869 /* Get the function's result */
Manuel Pégourié-Gonnard9670a592020-07-10 10:21:46 +02004870 TEST_CF_SECRET( &in_len, sizeof( in_len ) );
Gabor Mezei90437e32021-10-20 11:59:27 +02004871 TEST_EQUAL( 0, mbedtls_ct_hmac( &ctx, add_data, sizeof( add_data ),
gabor-mezei-arm9fa43ce2021-09-28 16:14:47 +02004872 data, in_len,
4873 min_in_len, max_in_len,
4874 out ) );
Manuel Pégourié-Gonnard9670a592020-07-10 10:21:46 +02004875 TEST_CF_PUBLIC( &in_len, sizeof( in_len ) );
4876 TEST_CF_PUBLIC( out, out_len );
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02004877
4878 /* Compute the reference result */
4879 TEST_EQUAL( 0, mbedtls_md_hmac_update( &ref_ctx, add_data,
4880 sizeof( add_data ) ) );
4881 TEST_EQUAL( 0, mbedtls_md_hmac_update( &ref_ctx, data, in_len ) );
4882 TEST_EQUAL( 0, mbedtls_md_hmac_finish( &ref_ctx, ref_out ) );
4883 TEST_EQUAL( 0, mbedtls_md_hmac_reset( &ref_ctx ) );
4884
4885 /* Compare */
4886 ASSERT_COMPARE( out, out_len, ref_out, out_len );
4887 }
4888
4889 mbedtls_free( data );
4890 data = NULL;
4891 }
4892
4893exit:
4894 mbedtls_md_free( &ref_ctx );
4895 mbedtls_md_free( &ctx );
4896
4897 mbedtls_free( data );
4898 mbedtls_free( out );
4899}
4900/* END_CASE */
Manuel Pégourié-Gonnard7fe2c5f2020-08-18 12:02:54 +02004901
4902/* BEGIN_CASE depends_on:MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC:MBEDTLS_TEST_HOOKS */
4903void ssl_cf_memcpy_offset( int offset_min, int offset_max, int len )
4904{
4905 unsigned char *dst = NULL;
4906 unsigned char *src = NULL;
4907 size_t src_len = offset_max + len;
4908 size_t secret;
4909
4910 ASSERT_ALLOC( dst, len );
4911 ASSERT_ALLOC( src, src_len );
4912
4913 /* Fill src in a way that we can detect if we copied the right bytes */
4914 mbedtls_test_rnd_std_rand( NULL, src, src_len );
4915
4916 for( secret = offset_min; secret <= (size_t) offset_max; secret++ )
4917 {
Chris Jones9634bb12021-01-20 15:56:42 +00004918 mbedtls_test_set_step( (int) secret );
Manuel Pégourié-Gonnard7fe2c5f2020-08-18 12:02:54 +02004919
4920 TEST_CF_SECRET( &secret, sizeof( secret ) );
Gabor Mezei90437e32021-10-20 11:59:27 +02004921 mbedtls_ct_memcpy_offset( dst, src, secret,
gabor-mezei-arm9fa43ce2021-09-28 16:14:47 +02004922 offset_min, offset_max, len );
Manuel Pégourié-Gonnard7fe2c5f2020-08-18 12:02:54 +02004923 TEST_CF_PUBLIC( &secret, sizeof( secret ) );
4924 TEST_CF_PUBLIC( dst, len );
4925
4926 ASSERT_COMPARE( dst, len, src + secret, len );
4927 }
4928
4929exit:
4930 mbedtls_free( dst );
4931 mbedtls_free( src );
4932}
4933/* END_CASE */
Hanno Becker6667ffd2021-04-19 21:59:22 +01004934
4935/* BEGIN_CASE depends_on:MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
4936void test_multiple_psks()
4937{
4938 unsigned char psk0[10] = { 0 };
4939 unsigned char psk0_identity[] = { 'f', 'o', 'o' };
4940
4941 unsigned char psk1[10] = { 0 };
4942 unsigned char psk1_identity[] = { 'b', 'a', 'r' };
4943
4944 mbedtls_ssl_config conf;
4945
4946 mbedtls_ssl_config_init( &conf );
4947
4948 TEST_ASSERT( mbedtls_ssl_conf_psk( &conf,
4949 psk0, sizeof( psk0 ),
4950 psk0_identity, sizeof( psk0_identity ) ) == 0 );
4951 TEST_ASSERT( mbedtls_ssl_conf_psk( &conf,
4952 psk1, sizeof( psk1 ),
4953 psk1_identity, sizeof( psk1_identity ) ) ==
4954 MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4955
4956exit:
4957
4958 mbedtls_ssl_config_free( &conf );
4959}
4960/* END_CASE */
4961
4962/* BEGIN_CASE depends_on:MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED:MBEDTLS_USE_PSA_CRYPTO */
4963void test_multiple_psks_opaque( int mode )
4964{
4965 /*
4966 * Mode 0: Raw PSK, then opaque PSK
4967 * Mode 1: Opaque PSK, then raw PSK
4968 * Mode 2: 2x opaque PSK
4969 */
4970
4971 unsigned char psk0_raw[10] = { 0 };
4972 unsigned char psk0_raw_identity[] = { 'f', 'o', 'o' };
4973
Andrzej Kurek03e01462022-01-03 12:53:24 +01004974 mbedtls_svc_key_id_t psk0_opaque = mbedtls_svc_key_id_make( 0x1, (psa_key_id_t) 1 );
4975
Hanno Becker6667ffd2021-04-19 21:59:22 +01004976 unsigned char psk0_opaque_identity[] = { 'f', 'o', 'o' };
4977
4978 unsigned char psk1_raw[10] = { 0 };
4979 unsigned char psk1_raw_identity[] = { 'b', 'a', 'r' };
4980
Andrzej Kurek03e01462022-01-03 12:53:24 +01004981 mbedtls_svc_key_id_t psk1_opaque = mbedtls_svc_key_id_make( 0x1, (psa_key_id_t) 2 );
4982
Hanno Becker6667ffd2021-04-19 21:59:22 +01004983 unsigned char psk1_opaque_identity[] = { 'b', 'a', 'r' };
4984
4985 mbedtls_ssl_config conf;
4986
4987 USE_PSA_INIT( );
4988 mbedtls_ssl_config_init( &conf );
4989
4990 switch( mode )
4991 {
4992 case 0:
4993
4994 TEST_ASSERT( mbedtls_ssl_conf_psk( &conf,
4995 psk0_raw, sizeof( psk0_raw ),
4996 psk0_raw_identity, sizeof( psk0_raw_identity ) )
4997 == 0 );
4998 TEST_ASSERT( mbedtls_ssl_conf_psk_opaque( &conf,
4999 psk1_opaque,
5000 psk1_opaque_identity, sizeof( psk1_opaque_identity ) )
5001 == MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
5002 break;
5003
5004 case 1:
5005
5006 TEST_ASSERT( mbedtls_ssl_conf_psk_opaque( &conf,
5007 psk0_opaque,
5008 psk0_opaque_identity, sizeof( psk0_opaque_identity ) )
5009 == 0 );
5010 TEST_ASSERT( mbedtls_ssl_conf_psk( &conf,
5011 psk1_raw, sizeof( psk1_raw ),
5012 psk1_raw_identity, sizeof( psk1_raw_identity ) )
5013 == MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
5014
5015 break;
5016
5017 case 2:
5018
5019 TEST_ASSERT( mbedtls_ssl_conf_psk_opaque( &conf,
5020 psk0_opaque,
5021 psk0_opaque_identity, sizeof( psk0_opaque_identity ) )
5022 == 0 );
5023 TEST_ASSERT( mbedtls_ssl_conf_psk_opaque( &conf,
5024 psk1_opaque,
5025 psk1_opaque_identity, sizeof( psk1_opaque_identity ) )
5026 == MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
5027
5028 break;
5029
5030 default:
5031 TEST_ASSERT( 0 );
5032 break;
5033 }
5034
5035exit:
5036
5037 mbedtls_ssl_config_free( &conf );
5038 USE_PSA_DONE( );
5039
5040}
5041/* END_CASE */
Brett Warren7f813d52021-10-20 23:08:38 +01005042
5043/* 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 */
5044void conf_curve()
5045{
5046
5047 mbedtls_ecp_group_id curve_list[] = { MBEDTLS_ECP_DP_SECP192R1,
5048 MBEDTLS_ECP_DP_SECP224R1,
5049 MBEDTLS_ECP_DP_SECP256R1,
5050 MBEDTLS_ECP_DP_NONE };
5051 mbedtls_ecp_group_id iana_tls_group_list[] = { MBEDTLS_SSL_IANA_TLS_GROUP_SECP192R1,
5052 MBEDTLS_SSL_IANA_TLS_GROUP_SECP224R1,
5053 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
5054 MBEDTLS_SSL_IANA_TLS_GROUP_NONE };
5055
5056 mbedtls_ssl_config conf;
5057 mbedtls_ssl_config_init( &conf );
5058
5059 mbedtls_ssl_conf_max_version( &conf, 3, 3 );
5060 mbedtls_ssl_conf_min_version( &conf, 3, 3 );
5061 mbedtls_ssl_conf_curves( &conf, curve_list );
5062
5063 mbedtls_ssl_context ssl;
5064 mbedtls_ssl_init( &ssl );
Paul Elliott46a6c202021-12-09 18:16:13 +00005065 TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
Brett Warren7f813d52021-10-20 23:08:38 +01005066
5067 TEST_ASSERT( ssl.handshake != NULL && ssl.handshake->group_list != NULL );
5068 TEST_ASSERT( ssl.conf != NULL && ssl.conf->group_list == NULL );
5069
5070 TEST_EQUAL( ssl.handshake->group_list[ARRAY_LENGTH( iana_tls_group_list ) - 1], MBEDTLS_SSL_IANA_TLS_GROUP_NONE );
5071
5072 for( size_t i = 0; i < ARRAY_LENGTH( iana_tls_group_list ); i++ )
5073 TEST_EQUAL( iana_tls_group_list[i], ssl.handshake->group_list[i] );
5074
5075 mbedtls_ssl_free( &ssl );
5076 mbedtls_ssl_config_free( &conf );
5077}
5078/* END_CASE */
5079
5080/* BEGIN_CASE depends_on:MBEDTLS_DEPRECATED_REMOVED */
5081void conf_group()
5082{
5083 uint16_t iana_tls_group_list[] = { MBEDTLS_SSL_IANA_TLS_GROUP_SECP192R1,
5084 MBEDTLS_SSL_IANA_TLS_GROUP_SECP224R1,
5085 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
5086 MBEDTLS_SSL_IANA_TLS_GROUP_NONE };
5087
5088 mbedtls_ssl_config conf;
5089 mbedtls_ssl_config_init( &conf );
5090
5091 mbedtls_ssl_conf_max_version( &conf, 3, 3 );
5092 mbedtls_ssl_conf_min_version( &conf, 3, 3 );
5093
5094 mbedtls_ssl_conf_groups( &conf, iana_tls_group_list );
5095
5096 mbedtls_ssl_context ssl;
5097 mbedtls_ssl_init( &ssl );
Paul Elliott46a6c202021-12-09 18:16:13 +00005098 TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
Brett Warren7f813d52021-10-20 23:08:38 +01005099
5100 TEST_ASSERT( ssl.conf != NULL && ssl.conf->group_list != NULL );
5101
5102 TEST_EQUAL( ssl.conf->group_list[ARRAY_LENGTH( iana_tls_group_list ) - 1], MBEDTLS_SSL_IANA_TLS_GROUP_NONE );
5103
5104 for( size_t i = 0; i < ARRAY_LENGTH( iana_tls_group_list ); i++ )
5105 TEST_EQUAL( iana_tls_group_list[i], ssl.conf->group_list[i] );
5106
5107 mbedtls_ssl_free( &ssl );
5108 mbedtls_ssl_config_free( &conf );
5109}
5110/* END_CASE */