blob: e6d66c079f51c0bb67f231b1cebee19f1ac164e5 [file] [log] [blame]
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02001/* BEGIN_HEADER */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002#include <mbedtls/ssl.h>
Chris Jones84a773f2021-03-05 18:38:47 +00003#include <ssl_misc.h>
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01004#include <mbedtls/ctr_drbg.h>
5#include <mbedtls/entropy.h>
Andrzej Kurek941962e2020-02-07 09:20:32 -05006#include <mbedtls/timing.h>
Piotr Nowickibde7ee82020-02-21 10:59:50 +01007#include <mbedtls/debug.h>
Hanno Becker73c825a2020-09-08 10:52:58 +01008#include <ssl_tls13_keys.h>
Mateusz Starzyk1aec6462021-02-08 15:34:42 +01009#include "test/certs.h"
Piotr Nowickibde7ee82020-02-21 10:59:50 +010010
Hanno Becker6667ffd2021-04-19 21:59:22 +010011#include <psa/crypto.h>
12
Gabor Mezei22c9a6f2021-10-20 12:09:35 +020013#include <constant_time_internal.h>
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +020014
Manuel Pégourié-Gonnard9670a592020-07-10 10:21:46 +020015#include <test/constant_flow.h>
16
Hanno Becker1413bd82020-09-09 12:46:09 +010017enum
18{
19#define MBEDTLS_SSL_TLS1_3_LABEL( name, string ) \
Xiaofei Baid25fab62021-12-02 06:36:27 +000020 tls13_label_ ## name,
Hanno Becker70d7fb02020-09-09 10:11:21 +010021MBEDTLS_SSL_TLS1_3_LABEL_LIST
22#undef MBEDTLS_SSL_TLS1_3_LABEL
Hanno Becker1413bd82020-09-09 12:46:09 +010023};
Hanno Becker70d7fb02020-09-09 10:11:21 +010024
Piotr Nowickibde7ee82020-02-21 10:59:50 +010025typedef struct log_pattern
26{
27 const char *pattern;
28 size_t counter;
29} log_pattern;
30
Piotr Nowicki438bf3b2020-03-10 12:59:10 +010031/*
32 * This function can be passed to mbedtls to receive output logs from it. In
Piotr Nowickibde7ee82020-02-21 10:59:50 +010033 * this case, it will count the instances of a log_pattern in the received
34 * logged messages.
35 */
36void log_analyzer( void *ctx, int level,
37 const char *file, int line,
38 const char *str )
39{
40 log_pattern *p = (log_pattern *) ctx;
41
42 (void) level;
43 (void) line;
44 (void) file;
45
46 if( NULL != p &&
47 NULL != p->pattern &&
48 NULL != strstr( str, p->pattern ) )
49 {
50 p->counter++;
51 }
52}
Janos Follath6264e662019-11-26 11:11:15 +000053
Paul Elliottc8570442020-04-15 17:00:50 +010054/* Invalid minor version used when not specifying a min/max version or expecting a test to fail */
55#define TEST_SSL_MINOR_VERSION_NONE -1
56
Andrzej Kurek8a6ff152020-02-26 09:10:14 -050057typedef struct handshake_test_options
58{
59 const char *cipher;
Paul Elliottc8570442020-04-15 17:00:50 +010060 int client_min_version;
61 int client_max_version;
62 int server_min_version;
63 int server_max_version;
64 int expected_negotiated_version;
Andrzej Kurek8a6ff152020-02-26 09:10:14 -050065 int pk_alg;
66 data_t *psk_str;
67 int dtls;
Piotr Nowickibde7ee82020-02-21 10:59:50 +010068 int srv_auth_mode;
Andrzej Kurek8a6ff152020-02-26 09:10:14 -050069 int serialize;
70 int mfl;
71 int cli_msg_len;
72 int srv_msg_len;
73 int expected_cli_fragments;
74 int expected_srv_fragments;
75 int renegotiate;
76 int legacy_renegotiation;
Piotr Nowickibde7ee82020-02-21 10:59:50 +010077 void *srv_log_obj;
78 void *cli_log_obj;
79 void (*srv_log_fun)(void *, int, const char *, int, const char *);
80 void (*cli_log_fun)(void *, int, const char *, int, const char *);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -050081 int resize_buffers;
Andrzej Kurek8a6ff152020-02-26 09:10:14 -050082} handshake_test_options;
83
84void init_handshake_options( handshake_test_options *opts )
85{
86 opts->cipher = "";
Paul Elliottc8570442020-04-15 17:00:50 +010087 opts->client_min_version = TEST_SSL_MINOR_VERSION_NONE;
88 opts->client_max_version = TEST_SSL_MINOR_VERSION_NONE;
89 opts->server_min_version = TEST_SSL_MINOR_VERSION_NONE;
90 opts->server_max_version = TEST_SSL_MINOR_VERSION_NONE;
91 opts->expected_negotiated_version = MBEDTLS_SSL_MINOR_VERSION_3;
Andrzej Kurek8a6ff152020-02-26 09:10:14 -050092 opts->pk_alg = MBEDTLS_PK_RSA;
93 opts->psk_str = NULL;
94 opts->dtls = 0;
Piotr Nowickibde7ee82020-02-21 10:59:50 +010095 opts->srv_auth_mode = MBEDTLS_SSL_VERIFY_NONE;
Andrzej Kurek8a6ff152020-02-26 09:10:14 -050096 opts->serialize = 0;
97 opts->mfl = MBEDTLS_SSL_MAX_FRAG_LEN_NONE;
98 opts->cli_msg_len = 100;
99 opts->srv_msg_len = 100;
100 opts->expected_cli_fragments = 1;
101 opts->expected_srv_fragments = 1;
102 opts->renegotiate = 0;
103 opts->legacy_renegotiation = MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION;
Piotr Nowickibde7ee82020-02-21 10:59:50 +0100104 opts->srv_log_obj = NULL;
105 opts->srv_log_obj = NULL;
106 opts->srv_log_fun = NULL;
107 opts->cli_log_fun = NULL;
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500108 opts->resize_buffers = 1;
Andrzej Kurek8a6ff152020-02-26 09:10:14 -0500109}
Janos Follath6264e662019-11-26 11:11:15 +0000110/*
111 * Buffer structure for custom I/O callbacks.
112 */
113
114typedef struct mbedtls_test_buffer
115{
116 size_t start;
117 size_t content_length;
118 size_t capacity;
119 unsigned char *buffer;
120} mbedtls_test_buffer;
121
122/*
123 * Initialises \p buf. After calling this function it is safe to call
124 * `mbedtls_test_buffer_free()` on \p buf.
125 */
126void mbedtls_test_buffer_init( mbedtls_test_buffer *buf )
127{
128 memset( buf, 0, sizeof( *buf ) );
129}
130
131/*
132 * Sets up \p buf. After calling this function it is safe to call
133 * `mbedtls_test_buffer_put()` and `mbedtls_test_buffer_get()` on \p buf.
134 */
135int mbedtls_test_buffer_setup( mbedtls_test_buffer *buf, size_t capacity )
136{
137 buf->buffer = (unsigned char*) mbedtls_calloc( capacity,
138 sizeof(unsigned char) );
139 if( NULL == buf->buffer )
140 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
141 buf->capacity = capacity;
142
143 return 0;
144}
145
146void mbedtls_test_buffer_free( mbedtls_test_buffer *buf )
147{
148 if( buf->buffer != NULL )
149 mbedtls_free( buf->buffer );
150
151 memset( buf, 0, sizeof( *buf ) );
152}
153
154/*
155 * Puts \p input_len bytes from the \p input buffer into the ring buffer \p buf.
156 *
157 * \p buf must have been initialized and set up by calling
158 * `mbedtls_test_buffer_init()` and `mbedtls_test_buffer_setup()`.
159 *
160 * \retval \p input_len, if the data fits.
161 * \retval 0 <= value < \p input_len, if the data does not fit.
162 * \retval -1, if \p buf is NULL, it hasn't been set up or \p input_len is not
163 * zero and \p input is NULL.
164 */
165int mbedtls_test_buffer_put( mbedtls_test_buffer *buf,
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100166 const unsigned char *input, size_t input_len )
Janos Follath6264e662019-11-26 11:11:15 +0000167{
168 size_t overflow = 0;
169
170 if( ( buf == NULL ) || ( buf->buffer == NULL ) )
171 return -1;
172
173 /* Reduce input_len to a number that fits in the buffer. */
174 if ( ( buf->content_length + input_len ) > buf->capacity )
175 {
176 input_len = buf->capacity - buf->content_length;
177 }
178
179 if( input == NULL )
180 {
181 return ( input_len == 0 ) ? 0 : -1;
182 }
183
Piotr Nowickifb437d72020-01-13 16:59:12 +0100184 /* Check if the buffer has not come full circle and free space is not in
185 * the middle */
186 if( buf->start + buf->content_length < buf->capacity )
Janos Follath6264e662019-11-26 11:11:15 +0000187 {
Piotr Nowickifb437d72020-01-13 16:59:12 +0100188
189 /* Calculate the number of bytes that need to be placed at lower memory
190 * address */
191 if( buf->start + buf->content_length + input_len
192 > buf->capacity )
193 {
194 overflow = ( buf->start + buf->content_length + input_len )
195 % buf->capacity;
196 }
197
198 memcpy( buf->buffer + buf->start + buf->content_length, input,
199 input_len - overflow );
200 memcpy( buf->buffer, input + input_len - overflow, overflow );
201
202 }
203 else
204 {
205 /* The buffer has come full circle and free space is in the middle */
206 memcpy( buf->buffer + buf->start + buf->content_length - buf->capacity,
207 input, input_len );
Janos Follath6264e662019-11-26 11:11:15 +0000208 }
209
Janos Follath6264e662019-11-26 11:11:15 +0000210 buf->content_length += input_len;
Janos Follath6264e662019-11-26 11:11:15 +0000211 return input_len;
212}
213
214/*
Andrzej Kurekf7774142020-01-22 06:34:59 -0500215 * Gets \p output_len bytes from the ring buffer \p buf into the
216 * \p output buffer. The output buffer can be NULL, in this case a part of the
217 * ring buffer will be dropped, if the requested length is available.
Janos Follath6264e662019-11-26 11:11:15 +0000218 *
219 * \p buf must have been initialized and set up by calling
220 * `mbedtls_test_buffer_init()` and `mbedtls_test_buffer_setup()`.
221 *
222 * \retval \p output_len, if the data is available.
223 * \retval 0 <= value < \p output_len, if the data is not available.
Andrzej Kurekf7774142020-01-22 06:34:59 -0500224 * \retval -1, if \buf is NULL or it hasn't been set up.
Janos Follath6264e662019-11-26 11:11:15 +0000225 */
226int mbedtls_test_buffer_get( mbedtls_test_buffer *buf,
227 unsigned char* output, size_t output_len )
228{
229 size_t overflow = 0;
230
231 if( ( buf == NULL ) || ( buf->buffer == NULL ) )
232 return -1;
233
Andrzej Kurekf7774142020-01-22 06:34:59 -0500234 if( output == NULL && output_len == 0 )
235 return 0;
Janos Follath6264e662019-11-26 11:11:15 +0000236
237 if( buf->content_length < output_len )
238 output_len = buf->content_length;
239
240 /* Calculate the number of bytes that need to be drawn from lower memory
241 * address */
242 if( buf->start + output_len > buf->capacity )
243 {
244 overflow = ( buf->start + output_len ) % buf->capacity;
245 }
246
Andrzej Kurekf7774142020-01-22 06:34:59 -0500247 if( output != NULL )
248 {
249 memcpy( output, buf->buffer + buf->start, output_len - overflow );
250 memcpy( output + output_len - overflow, buf->buffer, overflow );
251 }
252
Janos Follath6264e662019-11-26 11:11:15 +0000253 buf->content_length -= output_len;
254 buf->start = ( buf->start + output_len ) % buf->capacity;
255
256 return output_len;
257}
258
Hanno Beckera18d1322018-01-03 14:27:32 +0000259/*
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500260 * Errors used in the message transport mock tests
261 */
262 #define MBEDTLS_TEST_ERROR_ARG_NULL -11
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500263 #define MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED -44
264
265/*
266 * Context for a message metadata queue (fifo) that is on top of the ring buffer.
267 */
268typedef struct mbedtls_test_message_queue
269{
270 size_t *messages;
271 int pos;
272 int num;
273 int capacity;
274} mbedtls_test_message_queue;
275
276/*
277 * Setup and free functions for the message metadata queue.
278 *
279 * \p capacity describes the number of message metadata chunks that can be held
280 * within the queue.
281 *
282 * \retval 0, if a metadata queue of a given length can be allocated.
283 * \retval MBEDTLS_ERR_SSL_ALLOC_FAILED, if allocation failed.
284 */
285int mbedtls_test_message_queue_setup( mbedtls_test_message_queue *queue,
286 size_t capacity )
287{
288 queue->messages = (size_t*) mbedtls_calloc( capacity, sizeof(size_t) );
289 if( NULL == queue->messages )
290 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
291
292 queue->capacity = capacity;
293 queue->pos = 0;
294 queue->num = 0;
295
296 return 0;
297}
298
299void mbedtls_test_message_queue_free( mbedtls_test_message_queue *queue )
300{
301 if( queue == NULL )
302 return;
303
304 if( queue->messages != NULL )
305 mbedtls_free( queue->messages );
306
307 memset( queue, 0, sizeof( *queue ) );
308}
309
310/*
311 * Push message length information onto the message metadata queue.
312 * This will become the last element to leave it (fifo).
313 *
314 * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null.
Andrzej Kurekf46b9122020-02-07 08:19:00 -0500315 * \retval MBEDTLS_ERR_SSL_WANT_WRITE, if the queue is full.
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500316 * \retval \p len, if the push was successful.
317 */
318int mbedtls_test_message_queue_push_info( mbedtls_test_message_queue *queue,
319 size_t len )
320{
321 int place;
322 if( queue == NULL )
323 return MBEDTLS_TEST_ERROR_ARG_NULL;
324
325 if( queue->num >= queue->capacity )
Andrzej Kurekf46b9122020-02-07 08:19:00 -0500326 return MBEDTLS_ERR_SSL_WANT_WRITE;
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500327
328 place = ( queue->pos + queue->num ) % queue->capacity;
329 queue->messages[place] = len;
330 queue->num++;
331 return len;
332}
333
334/*
335 * Pop information about the next message length from the queue. This will be
336 * the oldest inserted message length(fifo). \p msg_len can be null, in which
337 * case the data will be popped from the queue but not copied anywhere.
338 *
339 * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null.
Andrzej Kurekf46b9122020-02-07 08:19:00 -0500340 * \retval MBEDTLS_ERR_SSL_WANT_READ, if the queue is empty.
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500341 * \retval message length, if the pop was successful, up to the given
342 \p buf_len.
343 */
344int mbedtls_test_message_queue_pop_info( mbedtls_test_message_queue *queue,
345 size_t buf_len )
346{
347 size_t message_length;
348 if( queue == NULL )
349 return MBEDTLS_TEST_ERROR_ARG_NULL;
350 if( queue->num == 0 )
Andrzej Kurekf46b9122020-02-07 08:19:00 -0500351 return MBEDTLS_ERR_SSL_WANT_READ;
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500352
353 message_length = queue->messages[queue->pos];
354 queue->messages[queue->pos] = 0;
355 queue->num--;
356 queue->pos++;
357 queue->pos %= queue->capacity;
358 if( queue->pos < 0 )
359 queue->pos += queue->capacity;
360
361 return ( message_length > buf_len ) ? buf_len : message_length;
362}
363
364/*
365 * Take a peek on the info about the next message length from the queue.
366 * This will be the oldest inserted message length(fifo).
367 *
368 * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null.
Andrzej Kurekf46b9122020-02-07 08:19:00 -0500369 * \retval MBEDTLS_ERR_SSL_WANT_READ, if the queue is empty.
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500370 * \retval 0, if the peek was successful.
371 * \retval MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED, if the given buffer length is
372 * too small to fit the message. In this case the \p msg_len will be
373 * set to the full message length so that the
374 * caller knows what portion of the message can be dropped.
375 */
376int mbedtls_test_message_queue_peek_info( mbedtls_test_message_queue *queue,
377 size_t buf_len, size_t* msg_len )
378{
379 if( queue == NULL || msg_len == NULL )
380 return MBEDTLS_TEST_ERROR_ARG_NULL;
381 if( queue->num == 0 )
Andrzej Kurekf46b9122020-02-07 08:19:00 -0500382 return MBEDTLS_ERR_SSL_WANT_READ;
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500383
384 *msg_len = queue->messages[queue->pos];
385 return ( *msg_len > buf_len ) ? MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED : 0;
386}
387/*
Janos Follath031827f2019-11-27 11:12:14 +0000388 * Context for the I/O callbacks simulating network connection.
389 */
390
391#define MBEDTLS_MOCK_SOCKET_CONNECTED 1
392
393typedef struct mbedtls_mock_socket
394{
395 int status;
396 mbedtls_test_buffer *input;
397 mbedtls_test_buffer *output;
398 struct mbedtls_mock_socket *peer;
399} mbedtls_mock_socket;
400
401/*
402 * Setup and teardown functions for mock sockets.
403 */
404void mbedtls_mock_socket_init( mbedtls_mock_socket *socket )
405{
406 memset( socket, 0, sizeof( *socket ) );
407}
408
409/*
410 * Closes the socket \p socket.
411 *
412 * \p socket must have been previously initialized by calling
413 * mbedtls_mock_socket_init().
414 *
415 * This function frees all allocated resources and both sockets are aware of the
416 * new connection state.
417 *
418 * That is, this function does not simulate half-open TCP connections and the
419 * phenomenon that when closing a UDP connection the peer is not aware of the
420 * connection having been closed.
421 */
422void mbedtls_mock_socket_close( mbedtls_mock_socket* socket )
423{
424 if( socket == NULL )
425 return;
426
427 if( socket->input != NULL )
428 {
429 mbedtls_test_buffer_free( socket->input );
430 mbedtls_free( socket->input );
431 }
432
433 if( socket->output != NULL )
434 {
435 mbedtls_test_buffer_free( socket->output );
436 mbedtls_free( socket->output );
437 }
438
439 if( socket->peer != NULL )
440 memset( socket->peer, 0, sizeof( *socket->peer ) );
441
442 memset( socket, 0, sizeof( *socket ) );
443}
444
445/*
446 * Establishes a connection between \p peer1 and \p peer2.
447 *
448 * \p peer1 and \p peer2 must have been previously initialized by calling
449 * mbedtls_mock_socket_init().
450 *
451 * The capacites of the internal buffers are set to \p bufsize. Setting this to
452 * the correct value allows for simulation of MTU, sanity testing the mock
453 * implementation and mocking TCP connections with lower memory cost.
454 */
455int mbedtls_mock_socket_connect( mbedtls_mock_socket* peer1,
456 mbedtls_mock_socket* peer2,
457 size_t bufsize )
458{
459 int ret = -1;
460
Piotr Nowickid796e192020-01-28 12:09:47 +0100461 peer1->output =
Janos Follath031827f2019-11-27 11:12:14 +0000462 (mbedtls_test_buffer*) mbedtls_calloc( 1, sizeof(mbedtls_test_buffer) );
463 if( peer1->output == NULL )
464 {
465 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
466 goto exit;
467 }
468 mbedtls_test_buffer_init( peer1->output );
469 if( 0 != ( ret = mbedtls_test_buffer_setup( peer1->output, bufsize ) ) )
470 {
471 goto exit;
472 }
473
Piotr Nowickid796e192020-01-28 12:09:47 +0100474 peer2->output =
475 (mbedtls_test_buffer*) mbedtls_calloc( 1, sizeof(mbedtls_test_buffer) );
476 if( peer2->output == NULL )
477 {
478 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
479 goto exit;
480 }
481 mbedtls_test_buffer_init( peer2->output );
482 if( 0 != ( ret = mbedtls_test_buffer_setup( peer2->output, bufsize ) ) )
483 {
484 goto exit;
485 }
486
Janos Follath031827f2019-11-27 11:12:14 +0000487 peer1->peer = peer2;
488 peer2->peer = peer1;
Piotr Nowickid796e192020-01-28 12:09:47 +0100489 peer1->input = peer2->output;
490 peer2->input = peer1->output;
Janos Follath031827f2019-11-27 11:12:14 +0000491
492 peer1->status = peer2->status = MBEDTLS_MOCK_SOCKET_CONNECTED;
493 ret = 0;
494
495exit:
496
497 if( ret != 0 )
498 {
499 mbedtls_mock_socket_close( peer1 );
500 mbedtls_mock_socket_close( peer2 );
501 }
502
503 return ret;
504}
505
506/*
507 * Callbacks for simulating blocking I/O over connection-oriented transport.
508 */
509
510int mbedtls_mock_tcp_send_b( void *ctx, const unsigned char *buf, size_t len )
511{
512 mbedtls_mock_socket *socket = (mbedtls_mock_socket*) ctx;
513
514 if( socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED )
515 return -1;
516
517 return mbedtls_test_buffer_put( socket->output, buf, len );
518}
519
520int mbedtls_mock_tcp_recv_b( void *ctx, unsigned char *buf, size_t len )
521{
522 mbedtls_mock_socket *socket = (mbedtls_mock_socket*) ctx;
523
524 if( socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED )
525 return -1;
526
527 return mbedtls_test_buffer_get( socket->input, buf, len );
528}
529
530/*
Janos Follath3766ba52019-11-27 13:31:42 +0000531 * Callbacks for simulating non-blocking I/O over connection-oriented transport.
532 */
533
534int mbedtls_mock_tcp_send_nb( void *ctx, const unsigned char *buf, size_t len )
535{
536 mbedtls_mock_socket *socket = (mbedtls_mock_socket*) ctx;
537
538 if( socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED )
539 return -1;
540
Piotr Nowicki890b5ca2020-01-15 16:19:07 +0100541 if( socket->output->capacity == socket->output->content_length )
Janos Follath3766ba52019-11-27 13:31:42 +0000542 {
Janos Follath3766ba52019-11-27 13:31:42 +0000543 return MBEDTLS_ERR_SSL_WANT_WRITE;
544 }
545
Janos Follath3766ba52019-11-27 13:31:42 +0000546 return mbedtls_test_buffer_put( socket->output, buf, len );
547}
548
549int mbedtls_mock_tcp_recv_nb( void *ctx, unsigned char *buf, size_t len )
550{
551 mbedtls_mock_socket *socket = (mbedtls_mock_socket*) ctx;
552
553 if( socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED )
554 return -1;
555
Andrzej Kurekf40daa32020-02-04 09:00:01 -0500556 if( socket->input->content_length == 0 )
Janos Follath3766ba52019-11-27 13:31:42 +0000557 {
Janos Follath3766ba52019-11-27 13:31:42 +0000558 return MBEDTLS_ERR_SSL_WANT_READ;
559 }
560
Janos Follath3766ba52019-11-27 13:31:42 +0000561 return mbedtls_test_buffer_get( socket->input, buf, len );
562}
563
Andrzej Kurekbc483de2020-01-22 03:40:00 -0500564/* Errors used in the message socket mocks */
565
566#define MBEDTLS_TEST_ERROR_CONTEXT_ERROR -55
567#define MBEDTLS_TEST_ERROR_SEND_FAILED -66
568#define MBEDTLS_TEST_ERROR_RECV_FAILED -77
569
570/*
571 * Structure used as an addon, or a wrapper, around the mocked sockets.
572 * Contains an input queue, to which the other socket pushes metadata,
573 * and an output queue, to which this one pushes metadata. This context is
574 * considered as an owner of the input queue only, which is initialized and
575 * freed in the respective setup and free calls.
576 */
577typedef struct mbedtls_test_message_socket_context
578{
579 mbedtls_test_message_queue* queue_input;
580 mbedtls_test_message_queue* queue_output;
581 mbedtls_mock_socket* socket;
582} mbedtls_test_message_socket_context;
583
Andrzej Kurek45916ba2020-03-05 14:46:22 -0500584void mbedtls_message_socket_init( mbedtls_test_message_socket_context *ctx )
585{
586 ctx->queue_input = NULL;
587 ctx->queue_output = NULL;
588 ctx->socket = NULL;
589}
590
Andrzej Kurekbc483de2020-01-22 03:40:00 -0500591/*
592 * Setup a given mesasge socket context including initialization of
593 * input/output queues to a chosen capacity of messages. Also set the
594 * corresponding mock socket.
595 *
596 * \retval 0, if everything succeeds.
597 * \retval MBEDTLS_ERR_SSL_ALLOC_FAILED, if allocation of a message
598 * queue failed.
599 */
600int mbedtls_message_socket_setup( mbedtls_test_message_queue* queue_input,
601 mbedtls_test_message_queue* queue_output,
602 size_t queue_capacity,
603 mbedtls_mock_socket* socket,
604 mbedtls_test_message_socket_context* ctx )
605{
606 int ret = mbedtls_test_message_queue_setup( queue_input, queue_capacity );
607 if( ret != 0 )
608 return ret;
609 ctx->queue_input = queue_input;
610 ctx->queue_output = queue_output;
611 ctx->socket = socket;
612 mbedtls_mock_socket_init( socket );
613
614 return 0;
615}
616
617/*
618 * Close a given message socket context, along with the socket itself. Free the
619 * memory allocated by the input queue.
620 */
621void mbedtls_message_socket_close( mbedtls_test_message_socket_context* ctx )
622{
623 if( ctx == NULL )
624 return;
625
626 mbedtls_test_message_queue_free( ctx->queue_input );
627 mbedtls_mock_socket_close( ctx->socket );
628 memset( ctx, 0, sizeof( *ctx ) );
629}
630
631/*
632 * Send one message through a given message socket context.
633 *
634 * \retval \p len, if everything succeeds.
635 * \retval MBEDTLS_TEST_ERROR_CONTEXT_ERROR, if any of the needed context
636 * elements or the context itself is null.
637 * \retval MBEDTLS_TEST_ERROR_SEND_FAILED if mbedtls_mock_tcp_send_b failed.
Andrzej Kurekf46b9122020-02-07 08:19:00 -0500638 * \retval MBEDTLS_ERR_SSL_WANT_WRITE, if the output queue is full.
Andrzej Kurekbc483de2020-01-22 03:40:00 -0500639 *
640 * This function will also return any error from
641 * mbedtls_test_message_queue_push_info.
642 */
643int mbedtls_mock_tcp_send_msg( void *ctx, const unsigned char *buf, size_t len )
644{
645 mbedtls_test_message_queue* queue;
646 mbedtls_mock_socket* socket;
647 mbedtls_test_message_socket_context *context = (mbedtls_test_message_socket_context*) ctx;
648
649 if( context == NULL || context->socket == NULL
650 || context->queue_output == NULL )
651 {
652 return MBEDTLS_TEST_ERROR_CONTEXT_ERROR;
653 }
654
655 queue = context->queue_output;
656 socket = context->socket;
657
658 if( queue->num >= queue->capacity )
Andrzej Kurekf46b9122020-02-07 08:19:00 -0500659 return MBEDTLS_ERR_SSL_WANT_WRITE;
Andrzej Kurekbc483de2020-01-22 03:40:00 -0500660
661 if( mbedtls_mock_tcp_send_b( socket, buf, len ) != (int) len )
662 return MBEDTLS_TEST_ERROR_SEND_FAILED;
663
664 return mbedtls_test_message_queue_push_info( queue, len );
665}
666
667/*
668 * Receive one message from a given message socket context and return message
669 * length or an error.
670 *
671 * \retval message length, if everything succeeds.
672 * \retval MBEDTLS_TEST_ERROR_CONTEXT_ERROR, if any of the needed context
673 * elements or the context itself is null.
674 * \retval MBEDTLS_TEST_ERROR_RECV_FAILED if mbedtls_mock_tcp_recv_b failed.
675 *
676 * This function will also return any error other than
677 * MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED from mbedtls_test_message_queue_peek_info.
678 */
679int mbedtls_mock_tcp_recv_msg( void *ctx, unsigned char *buf, size_t buf_len )
680{
681 mbedtls_test_message_queue* queue;
682 mbedtls_mock_socket* socket;
683 mbedtls_test_message_socket_context *context = (mbedtls_test_message_socket_context*) ctx;
Gilles Peskine19e841e2020-03-09 20:43:51 +0100684 size_t drop_len = 0;
Andrzej Kurekbc483de2020-01-22 03:40:00 -0500685 size_t msg_len;
686 int ret;
687
688 if( context == NULL || context->socket == NULL
689 || context->queue_input == NULL )
690 {
691 return MBEDTLS_TEST_ERROR_CONTEXT_ERROR;
692 }
693
694 queue = context->queue_input;
695 socket = context->socket;
696
697 /* Peek first, so that in case of a socket error the data remains in
698 * the queue. */
699 ret = mbedtls_test_message_queue_peek_info( queue, buf_len, &msg_len );
700 if( ret == MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED )
701 {
702 /* Calculate how much to drop */
703 drop_len = msg_len - buf_len;
704
705 /* Set the requested message len to be buffer length */
706 msg_len = buf_len;
707 } else if( ret != 0 )
708 {
709 return ret;
710 }
711
712 if( mbedtls_mock_tcp_recv_b( socket, buf, msg_len ) != (int) msg_len )
713 return MBEDTLS_TEST_ERROR_RECV_FAILED;
714
715 if( ret == MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED )
716 {
717 /* Drop the remaining part of the message */
718 if( mbedtls_mock_tcp_recv_b( socket, NULL, drop_len ) != (int) drop_len )
719 {
720 /* Inconsistent state - part of the message was read,
721 * and a part couldn't. Not much we can do here, but it should not
722 * happen in test environment, unless forced manually. */
723 }
724 }
725 mbedtls_test_message_queue_pop_info( queue, buf_len );
726
727 return msg_len;
728}
729
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +0200730#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
731 defined(MBEDTLS_ENTROPY_C) && \
732 defined(MBEDTLS_CTR_DRBG_C)
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100733
734/*
735 * Structure with endpoint's certificates for SSL communication tests.
736 */
737typedef struct mbedtls_endpoint_certificate
738{
739 mbedtls_x509_crt ca_cert;
740 mbedtls_x509_crt cert;
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100741 mbedtls_pk_context pkey;
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100742} mbedtls_endpoint_certificate;
743
744/*
745 * Endpoint structure for SSL communication tests.
746 */
747typedef struct mbedtls_endpoint
748{
749 const char *name;
750 mbedtls_ssl_context ssl;
751 mbedtls_ssl_config conf;
752 mbedtls_ctr_drbg_context ctr_drbg;
753 mbedtls_entropy_context entropy;
754 mbedtls_mock_socket socket;
755 mbedtls_endpoint_certificate cert;
756} mbedtls_endpoint;
757
758/*
759 * Initializes \p ep_cert structure and assigns it to endpoint
760 * represented by \p ep.
761 *
762 * \retval 0 on success, otherwise error code.
763 */
Andrzej Kurekb2980742020-02-02 19:25:26 -0500764int mbedtls_endpoint_certificate_init( mbedtls_endpoint *ep, int pk_alg )
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100765{
766 int i = 0;
767 int ret = -1;
768 mbedtls_endpoint_certificate *cert;
769
770 if( ep == NULL )
771 {
772 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
773 }
774
775 cert = &( ep->cert );
776 mbedtls_x509_crt_init( &( cert->ca_cert ) );
777 mbedtls_x509_crt_init( &( cert->cert ) );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100778 mbedtls_pk_init( &( cert->pkey ) );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100779
780 /* Load the trusted CA */
781
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100782 for( i = 0; mbedtls_test_cas_der[i] != NULL; i++ )
783 {
784 ret = mbedtls_x509_crt_parse_der( &( cert->ca_cert ),
785 (const unsigned char *) mbedtls_test_cas_der[i],
786 mbedtls_test_cas_der_len[i] );
787 TEST_ASSERT( ret == 0 );
788 }
789
790 /* Load own certificate and private key */
791
792 if( ep->conf.endpoint == MBEDTLS_SSL_IS_SERVER )
793 {
Andrzej Kurekb2980742020-02-02 19:25:26 -0500794 if( pk_alg == MBEDTLS_PK_RSA )
795 {
796 ret = mbedtls_x509_crt_parse( &( cert->cert ),
797 (const unsigned char*) mbedtls_test_srv_crt_rsa_sha256_der,
798 mbedtls_test_srv_crt_rsa_sha256_der_len );
799 TEST_ASSERT( ret == 0 );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100800
Andrzej Kurekb2980742020-02-02 19:25:26 -0500801 ret = mbedtls_pk_parse_key( &( cert->pkey ),
802 (const unsigned char*) mbedtls_test_srv_key_rsa_der,
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200803 mbedtls_test_srv_key_rsa_der_len, NULL, 0,
804 mbedtls_test_rnd_std_rand, NULL );
Andrzej Kurekb2980742020-02-02 19:25:26 -0500805 TEST_ASSERT( ret == 0 );
806 }
807 else
808 {
809 ret = mbedtls_x509_crt_parse( &( cert->cert ),
810 (const unsigned char*) mbedtls_test_srv_crt_ec_der,
811 mbedtls_test_srv_crt_ec_der_len );
812 TEST_ASSERT( ret == 0 );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100813
Andrzej Kurekb2980742020-02-02 19:25:26 -0500814 ret = mbedtls_pk_parse_key( &( cert->pkey ),
815 (const unsigned char*) mbedtls_test_srv_key_ec_der,
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200816 mbedtls_test_srv_key_ec_der_len, NULL, 0,
817 mbedtls_test_rnd_std_rand, NULL );
Andrzej Kurekb2980742020-02-02 19:25:26 -0500818 TEST_ASSERT( ret == 0 );
819 }
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100820 }
821 else
822 {
Andrzej Kurekb2980742020-02-02 19:25:26 -0500823 if( pk_alg == MBEDTLS_PK_RSA )
824 {
825 ret = mbedtls_x509_crt_parse( &( cert->cert ),
826 (const unsigned char *) mbedtls_test_cli_crt_rsa_der,
827 mbedtls_test_cli_crt_rsa_der_len );
828 TEST_ASSERT( ret == 0 );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100829
Andrzej Kurekb2980742020-02-02 19:25:26 -0500830 ret = mbedtls_pk_parse_key( &( cert->pkey ),
831 (const unsigned char *) mbedtls_test_cli_key_rsa_der,
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200832 mbedtls_test_cli_key_rsa_der_len, NULL, 0,
833 mbedtls_test_rnd_std_rand, NULL );
Andrzej Kurekb2980742020-02-02 19:25:26 -0500834 TEST_ASSERT( ret == 0 );
835 }
836 else
837 {
838 ret = mbedtls_x509_crt_parse( &( cert->cert ),
839 (const unsigned char *) mbedtls_test_cli_crt_ec_der,
840 mbedtls_test_cli_crt_ec_len );
841 TEST_ASSERT( ret == 0 );
842
843 ret = mbedtls_pk_parse_key( &( cert->pkey ),
844 (const unsigned char *) mbedtls_test_cli_key_ec_der,
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200845 mbedtls_test_cli_key_ec_der_len, NULL, 0,
846 mbedtls_test_rnd_std_rand, NULL );
Andrzej Kurekb2980742020-02-02 19:25:26 -0500847 TEST_ASSERT( ret == 0 );
848 }
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100849 }
850
851 mbedtls_ssl_conf_ca_chain( &( ep->conf ), &( cert->ca_cert ), NULL );
852
Andrzej Kurekb2980742020-02-02 19:25:26 -0500853 ret = mbedtls_ssl_conf_own_cert( &( ep->conf ), &( cert->cert ),
854 &( cert->pkey ) );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100855 TEST_ASSERT( ret == 0 );
856
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100857exit:
858 if( ret != 0 )
859 {
860 mbedtls_x509_crt_free( &( cert->ca_cert ) );
861 mbedtls_x509_crt_free( &( cert->cert ) );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100862 mbedtls_pk_free( &( cert->pkey ) );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100863 }
864
865 return ret;
866}
867
868/*
869 * Initializes \p ep structure. It is important to call `mbedtls_endpoint_free()`
870 * after calling this function even if it fails.
871 *
872 * \p endpoint_type must be set as MBEDTLS_SSL_IS_SERVER or
873 * MBEDTLS_SSL_IS_CLIENT.
Andrzej Kurek15daf502020-02-12 09:17:52 -0500874 * \p pk_alg the algorithm to use, currently only MBEDTLS_PK_RSA and
875 * MBEDTLS_PK_ECDSA are supported.
876 * \p dtls_context - in case of DTLS - this is the context handling metadata.
877 * \p input_queue - used only in case of DTLS.
878 * \p output_queue - used only in case of DTLS.
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100879 *
880 * \retval 0 on success, otherwise error code.
881 */
Andrzej Kurek15daf502020-02-12 09:17:52 -0500882int mbedtls_endpoint_init( mbedtls_endpoint *ep, int endpoint_type, int pk_alg,
883 mbedtls_test_message_socket_context *dtls_context,
884 mbedtls_test_message_queue *input_queue,
885 mbedtls_test_message_queue *output_queue )
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100886{
887 int ret = -1;
888
Andrzej Kurek15daf502020-02-12 09:17:52 -0500889 if( dtls_context != NULL && ( input_queue == NULL || output_queue == NULL ) )
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100890 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Andrzej Kurek15daf502020-02-12 09:17:52 -0500891
892 if( ep == NULL )
893 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100894
895 memset( ep, 0, sizeof( *ep ) );
896
897 ep->name = ( endpoint_type == MBEDTLS_SSL_IS_SERVER ) ? "Server" : "Client";
898
899 mbedtls_ssl_init( &( ep->ssl ) );
900 mbedtls_ssl_config_init( &( ep->conf ) );
901 mbedtls_ctr_drbg_init( &( ep->ctr_drbg ) );
902 mbedtls_ssl_conf_rng( &( ep->conf ),
903 mbedtls_ctr_drbg_random,
904 &( ep->ctr_drbg ) );
905 mbedtls_entropy_init( &( ep->entropy ) );
Andrzej Kurek15daf502020-02-12 09:17:52 -0500906 if( dtls_context != NULL )
907 {
908 TEST_ASSERT( mbedtls_message_socket_setup( input_queue, output_queue,
909 100, &( ep->socket ),
910 dtls_context ) == 0 );
911 }
912 else
913 {
914 mbedtls_mock_socket_init( &( ep->socket ) );
915 }
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100916
917 ret = mbedtls_ctr_drbg_seed( &( ep->ctr_drbg ), mbedtls_entropy_func,
918 &( ep->entropy ), (const unsigned char *) ( ep->name ),
919 strlen( ep->name ) );
920 TEST_ASSERT( ret == 0 );
921
922 /* Non-blocking callbacks without timeout */
Andrzej Kurek15daf502020-02-12 09:17:52 -0500923 if( dtls_context != NULL )
924 {
925 mbedtls_ssl_set_bio( &( ep->ssl ), dtls_context,
926 mbedtls_mock_tcp_send_msg,
927 mbedtls_mock_tcp_recv_msg,
928 NULL );
929 }
930 else
931 {
932 mbedtls_ssl_set_bio( &( ep->ssl ), &( ep->socket ),
933 mbedtls_mock_tcp_send_nb,
934 mbedtls_mock_tcp_recv_nb,
935 NULL );
936 }
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100937
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100938 ret = mbedtls_ssl_config_defaults( &( ep->conf ), endpoint_type,
Andrzej Kurek15daf502020-02-12 09:17:52 -0500939 ( dtls_context != NULL ) ?
940 MBEDTLS_SSL_TRANSPORT_DATAGRAM :
941 MBEDTLS_SSL_TRANSPORT_STREAM,
942 MBEDTLS_SSL_PRESET_DEFAULT );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100943 TEST_ASSERT( ret == 0 );
944
Andrzej Kurek1a44a152020-02-07 08:21:32 -0500945 ret = mbedtls_ssl_setup( &( ep->ssl ), &( ep->conf ) );
946 TEST_ASSERT( ret == 0 );
Andrzej Kurek15daf502020-02-12 09:17:52 -0500947
948#if defined(MBEDTLS_SSL_PROTO_DTLS) && defined(MBEDTLS_SSL_SRV_C)
949 if( endpoint_type == MBEDTLS_SSL_IS_SERVER && dtls_context != NULL )
950 mbedtls_ssl_conf_dtls_cookies( &( ep->conf ), NULL, NULL, NULL );
951#endif
952
Andrzej Kurekb2980742020-02-02 19:25:26 -0500953 ret = mbedtls_endpoint_certificate_init( ep, pk_alg );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100954 TEST_ASSERT( ret == 0 );
955
956exit:
957 return ret;
958}
959
960/*
961 * Deinitializes certificates from endpoint represented by \p ep.
962 */
963void mbedtls_endpoint_certificate_free( mbedtls_endpoint *ep )
964{
965 mbedtls_endpoint_certificate *cert = &( ep->cert );
966 mbedtls_x509_crt_free( &( cert->ca_cert ) );
967 mbedtls_x509_crt_free( &( cert->cert ) );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100968 mbedtls_pk_free( &( cert->pkey ) );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100969}
970
971/*
972 * Deinitializes endpoint represented by \p ep.
973 */
Andrzej Kurek15daf502020-02-12 09:17:52 -0500974void mbedtls_endpoint_free( mbedtls_endpoint *ep,
975 mbedtls_test_message_socket_context *context )
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100976{
977 mbedtls_endpoint_certificate_free( ep );
978
979 mbedtls_ssl_free( &( ep->ssl ) );
980 mbedtls_ssl_config_free( &( ep->conf ) );
981 mbedtls_ctr_drbg_free( &( ep->ctr_drbg ) );
982 mbedtls_entropy_free( &( ep->entropy ) );
Andrzej Kurek15daf502020-02-12 09:17:52 -0500983
984 if( context != NULL )
985 {
986 mbedtls_message_socket_close( context );
987 }
988 else
989 {
990 mbedtls_mock_socket_close( &( ep->socket ) );
991 }
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100992}
993
994/*
995 * This function moves ssl handshake from \p ssl to prescribed \p state.
996 * /p second_ssl is used as second endpoint and their sockets have to be
997 * connected before calling this function.
998 *
999 * \retval 0 on success, otherwise error code.
1000 */
1001int mbedtls_move_handshake_to_state( mbedtls_ssl_context *ssl,
1002 mbedtls_ssl_context *second_ssl,
1003 int state )
1004{
1005 enum { BUFFSIZE = 1024 };
1006 int max_steps = 1000;
1007 int ret = 0;
1008
1009 if( ssl == NULL || second_ssl == NULL )
1010 {
1011 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
1012 }
1013
1014 /* Perform communication via connected sockets */
1015 while( ( ssl->state != state ) && ( --max_steps >= 0 ) )
1016 {
1017 /* If /p second_ssl ends the handshake procedure before /p ssl then
1018 * there is no need to call the next step */
1019 if( second_ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
1020 {
1021 ret = mbedtls_ssl_handshake_step( second_ssl );
1022 if( ret != 0 && ret != MBEDTLS_ERR_SSL_WANT_READ &&
1023 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
1024 {
1025 return ret;
1026 }
1027 }
1028
1029 /* We only care about the \p ssl state and returns, so we call it last,
1030 * to leave the iteration as soon as the state is as expected. */
1031 ret = mbedtls_ssl_handshake_step( ssl );
1032 if( ret != 0 && ret != MBEDTLS_ERR_SSL_WANT_READ &&
1033 ret != MBEDTLS_ERR_SSL_WANT_WRITE )
1034 {
1035 return ret;
1036 }
1037 }
1038
1039 return ( max_steps >= 0 ) ? ret : -1;
1040}
1041
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02001042#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01001043
Janos Follath3766ba52019-11-27 13:31:42 +00001044/*
Piotr Nowicki438bf3b2020-03-10 12:59:10 +01001045 * Write application data. Increase write counter if necessary.
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001046 */
1047int mbedtls_ssl_write_fragment( mbedtls_ssl_context *ssl, unsigned char *buf,
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001048 int buf_len, int *written,
Piotr Nowicki438bf3b2020-03-10 12:59:10 +01001049 const int expected_fragments )
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001050{
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001051 int ret = mbedtls_ssl_write( ssl, buf + *written, buf_len - *written );
1052 if( ret > 0 )
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001053 {
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001054 *written += ret;
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001055 }
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001056
1057 if( expected_fragments == 0 )
1058 {
1059 /* Used for DTLS and the message size larger than MFL. In that case
1060 * the message can not be fragmented and the library should return
1061 * MBEDTLS_ERR_SSL_BAD_INPUT_DATA error. This error must be returned
1062 * to prevent a dead loop inside mbedtls_exchange_data(). */
1063 return ret;
1064 }
1065 else if( expected_fragments == 1 )
1066 {
1067 /* Used for TLS/DTLS and the message size lower than MFL */
1068 TEST_ASSERT( ret == buf_len ||
1069 ret == MBEDTLS_ERR_SSL_WANT_READ ||
1070 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
1071 }
1072 else
1073 {
1074 /* Used for TLS and the message size larger than MFL */
1075 TEST_ASSERT( expected_fragments > 1 );
1076 TEST_ASSERT( ( ret >= 0 && ret <= buf_len ) ||
1077 ret == MBEDTLS_ERR_SSL_WANT_READ ||
1078 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
1079 }
1080
1081 return 0;
1082
1083exit:
1084 /* Some of the tests failed */
1085 return -1;
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001086}
1087
1088/*
Piotr Nowicki438bf3b2020-03-10 12:59:10 +01001089 * Read application data and increase read counter and fragments counter if necessary.
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001090 */
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001091int mbedtls_ssl_read_fragment( mbedtls_ssl_context *ssl, unsigned char *buf,
1092 int buf_len, int *read,
Piotr Nowicki438bf3b2020-03-10 12:59:10 +01001093 int *fragments, const int expected_fragments )
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001094{
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001095 int ret = mbedtls_ssl_read( ssl, buf + *read, buf_len - *read );
1096 if( ret > 0 )
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001097 {
Piotr Nowicki438bf3b2020-03-10 12:59:10 +01001098 ( *fragments )++;
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001099 *read += ret;
1100 }
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001101
1102 if( expected_fragments == 0 )
1103 {
1104 TEST_ASSERT( ret == 0 );
1105 }
1106 else if( expected_fragments == 1 )
1107 {
1108 TEST_ASSERT( ret == buf_len ||
1109 ret == MBEDTLS_ERR_SSL_WANT_READ ||
1110 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
1111 }
1112 else
1113 {
1114 TEST_ASSERT( expected_fragments > 1 );
1115 TEST_ASSERT( ( ret >= 0 && ret <= buf_len ) ||
1116 ret == MBEDTLS_ERR_SSL_WANT_READ ||
1117 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
1118 }
1119
1120 return 0;
1121
1122exit:
1123 /* Some of the tests failed */
1124 return -1;
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001125}
1126
1127/*
Hanno Beckera18d1322018-01-03 14:27:32 +00001128 * Helper function setting up inverse record transformations
1129 * using given cipher, hash, EtM mode, authentication tag length,
1130 * and version.
1131 */
1132
1133#define CHK( x ) \
1134 do \
1135 { \
1136 if( !( x ) ) \
Hanno Becker81e16a32019-03-01 11:21:44 +00001137 { \
Hanno Beckera5780f12019-04-05 09:55:37 +01001138 ret = -1; \
Hanno Becker81e16a32019-03-01 11:21:44 +00001139 goto cleanup; \
1140 } \
Hanno Beckera18d1322018-01-03 14:27:32 +00001141 } while( 0 )
1142
Andrzej Kurekf40daa32020-02-04 09:00:01 -05001143void set_ciphersuite( mbedtls_ssl_config *conf, const char *cipher,
1144 int* forced_ciphersuite )
1145{
1146 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
1147 forced_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id( cipher );
1148 forced_ciphersuite[1] = 0;
1149
1150 ciphersuite_info =
1151 mbedtls_ssl_ciphersuite_from_id( forced_ciphersuite[0] );
1152
1153 TEST_ASSERT( ciphersuite_info != NULL );
1154 TEST_ASSERT( ciphersuite_info->min_minor_ver <= conf->max_minor_ver );
1155 TEST_ASSERT( ciphersuite_info->max_minor_ver >= conf->min_minor_ver );
1156
1157 if( conf->max_minor_ver > ciphersuite_info->max_minor_ver )
1158 {
1159 conf->max_minor_ver = ciphersuite_info->max_minor_ver;
1160 }
1161 if( conf->min_minor_ver < ciphersuite_info->min_minor_ver )
1162 {
1163 conf->min_minor_ver = ciphersuite_info->min_minor_ver;
1164 }
1165
1166 mbedtls_ssl_conf_ciphersuites( conf, forced_ciphersuite );
1167
1168exit:
1169 return;
1170}
1171
Andrzej Kurekcc5169c2020-02-04 09:04:56 -05001172int psk_dummy_callback( void *p_info, mbedtls_ssl_context *ssl,
1173 const unsigned char *name, size_t name_len )
1174{
1175 (void) p_info;
1176 (void) ssl;
1177 (void) name;
1178 (void) name_len;
1179
1180 return ( 0 );
1181}
1182
Hanno Beckerd856c822019-04-29 17:30:59 +01001183#if MBEDTLS_SSL_CID_OUT_LEN_MAX > MBEDTLS_SSL_CID_IN_LEN_MAX
1184#define SSL_CID_LEN_MIN MBEDTLS_SSL_CID_IN_LEN_MAX
1185#else
1186#define SSL_CID_LEN_MIN MBEDTLS_SSL_CID_OUT_LEN_MAX
1187#endif
Hanno Beckera18d1322018-01-03 14:27:32 +00001188
1189static int build_transforms( mbedtls_ssl_transform *t_in,
1190 mbedtls_ssl_transform *t_out,
1191 int cipher_type, int hash_id,
Hanno Beckerd856c822019-04-29 17:30:59 +01001192 int etm, int tag_mode, int ver,
1193 size_t cid0_len,
1194 size_t cid1_len )
Hanno Beckera18d1322018-01-03 14:27:32 +00001195{
1196 mbedtls_cipher_info_t const *cipher_info;
Hanno Beckera5780f12019-04-05 09:55:37 +01001197 int ret = 0;
Hanno Beckera18d1322018-01-03 14:27:32 +00001198
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01001199#if defined(MBEDTLS_USE_PSA_CRYPTO)
1200 psa_key_type_t key_type;
1201 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1202 psa_algorithm_t alg;
1203 size_t key_bits;
1204 psa_status_t status;
1205#endif
1206
Hanno Beckera18d1322018-01-03 14:27:32 +00001207 size_t keylen, maclen, ivlen;
Hanno Becker81e16a32019-03-01 11:21:44 +00001208 unsigned char *key0 = NULL, *key1 = NULL;
Paul Elliott6f1eda72020-06-11 20:22:00 +01001209 unsigned char *md0 = NULL, *md1 = NULL;
Hanno Beckera18d1322018-01-03 14:27:32 +00001210 unsigned char iv_enc[16], iv_dec[16];
1211
Hanno Beckera0e20d02019-05-15 14:03:01 +01001212#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd856c822019-04-29 17:30:59 +01001213 unsigned char cid0[ SSL_CID_LEN_MIN ];
1214 unsigned char cid1[ SSL_CID_LEN_MIN ];
1215
Ronald Cron351f0ee2020-06-10 12:12:18 +02001216 mbedtls_test_rnd_std_rand( NULL, cid0, sizeof( cid0 ) );
1217 mbedtls_test_rnd_std_rand( NULL, cid1, sizeof( cid1 ) );
Hanno Becker43c24b82019-05-01 09:45:57 +01001218#else
1219 ((void) cid0_len);
1220 ((void) cid1_len);
Hanno Beckera0e20d02019-05-15 14:03:01 +01001221#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerd856c822019-04-29 17:30:59 +01001222
Hanno Beckera18d1322018-01-03 14:27:32 +00001223 maclen = 0;
1224
1225 /* Pick cipher */
1226 cipher_info = mbedtls_cipher_info_from_type( cipher_type );
1227 CHK( cipher_info != NULL );
1228 CHK( cipher_info->iv_size <= 16 );
1229 CHK( cipher_info->key_bitlen % 8 == 0 );
1230
1231 /* Pick keys */
1232 keylen = cipher_info->key_bitlen / 8;
Hanno Becker78d1f702019-04-05 09:56:10 +01001233 /* Allocate `keylen + 1` bytes to ensure that we get
1234 * a non-NULL pointers from `mbedtls_calloc` even if
1235 * `keylen == 0` in the case of the NULL cipher. */
1236 CHK( ( key0 = mbedtls_calloc( 1, keylen + 1 ) ) != NULL );
1237 CHK( ( key1 = mbedtls_calloc( 1, keylen + 1 ) ) != NULL );
Hanno Beckera18d1322018-01-03 14:27:32 +00001238 memset( key0, 0x1, keylen );
1239 memset( key1, 0x2, keylen );
1240
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01001241#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckera18d1322018-01-03 14:27:32 +00001242 /* Setup cipher contexts */
1243 CHK( mbedtls_cipher_setup( &t_in->cipher_ctx_enc, cipher_info ) == 0 );
1244 CHK( mbedtls_cipher_setup( &t_in->cipher_ctx_dec, cipher_info ) == 0 );
1245 CHK( mbedtls_cipher_setup( &t_out->cipher_ctx_enc, cipher_info ) == 0 );
1246 CHK( mbedtls_cipher_setup( &t_out->cipher_ctx_dec, cipher_info ) == 0 );
1247
1248#if defined(MBEDTLS_CIPHER_MODE_CBC)
1249 if( cipher_info->mode == MBEDTLS_MODE_CBC )
1250 {
1251 CHK( mbedtls_cipher_set_padding_mode( &t_in->cipher_ctx_enc,
1252 MBEDTLS_PADDING_NONE ) == 0 );
1253 CHK( mbedtls_cipher_set_padding_mode( &t_in->cipher_ctx_dec,
1254 MBEDTLS_PADDING_NONE ) == 0 );
1255 CHK( mbedtls_cipher_set_padding_mode( &t_out->cipher_ctx_enc,
1256 MBEDTLS_PADDING_NONE ) == 0 );
1257 CHK( mbedtls_cipher_set_padding_mode( &t_out->cipher_ctx_dec,
1258 MBEDTLS_PADDING_NONE ) == 0 );
1259 }
1260#endif /* MBEDTLS_CIPHER_MODE_CBC */
1261
1262 CHK( mbedtls_cipher_setkey( &t_in->cipher_ctx_enc, key0,
1263 keylen << 3, MBEDTLS_ENCRYPT ) == 0 );
1264 CHK( mbedtls_cipher_setkey( &t_in->cipher_ctx_dec, key1,
1265 keylen << 3, MBEDTLS_DECRYPT ) == 0 );
1266 CHK( mbedtls_cipher_setkey( &t_out->cipher_ctx_enc, key1,
1267 keylen << 3, MBEDTLS_ENCRYPT ) == 0 );
1268 CHK( mbedtls_cipher_setkey( &t_out->cipher_ctx_dec, key0,
1269 keylen << 3, MBEDTLS_DECRYPT ) == 0 );
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01001270#endif
Hanno Beckera18d1322018-01-03 14:27:32 +00001271
1272 /* Setup MAC contexts */
Hanno Beckerfd86ca82020-11-30 08:54:23 +00001273#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Hanno Beckera18d1322018-01-03 14:27:32 +00001274 if( cipher_info->mode == MBEDTLS_MODE_CBC ||
1275 cipher_info->mode == MBEDTLS_MODE_STREAM )
1276 {
1277 mbedtls_md_info_t const *md_info;
Hanno Beckera18d1322018-01-03 14:27:32 +00001278
1279 /* Pick hash */
1280 md_info = mbedtls_md_info_from_type( hash_id );
1281 CHK( md_info != NULL );
1282
1283 /* Pick hash keys */
1284 maclen = mbedtls_md_get_size( md_info );
Hanno Becker3ee54212019-04-04 16:31:26 +01001285 CHK( ( md0 = mbedtls_calloc( 1, maclen ) ) != NULL );
1286 CHK( ( md1 = mbedtls_calloc( 1, maclen ) ) != NULL );
Hanno Beckera18d1322018-01-03 14:27:32 +00001287 memset( md0, 0x5, maclen );
1288 memset( md1, 0x6, maclen );
1289
1290 CHK( mbedtls_md_setup( &t_out->md_ctx_enc, md_info, 1 ) == 0 );
1291 CHK( mbedtls_md_setup( &t_out->md_ctx_dec, md_info, 1 ) == 0 );
1292 CHK( mbedtls_md_setup( &t_in->md_ctx_enc, md_info, 1 ) == 0 );
1293 CHK( mbedtls_md_setup( &t_in->md_ctx_dec, md_info, 1 ) == 0 );
1294
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001295 CHK( mbedtls_md_hmac_starts( &t_in->md_ctx_enc,
1296 md0, maclen ) == 0 );
1297 CHK( mbedtls_md_hmac_starts( &t_in->md_ctx_dec,
1298 md1, maclen ) == 0 );
1299 CHK( mbedtls_md_hmac_starts( &t_out->md_ctx_enc,
1300 md1, maclen ) == 0 );
1301 CHK( mbedtls_md_hmac_starts( &t_out->md_ctx_dec,
1302 md0, maclen ) == 0 );
Hanno Beckera18d1322018-01-03 14:27:32 +00001303 }
1304#else
1305 ((void) hash_id);
Hanno Beckerfd86ca82020-11-30 08:54:23 +00001306#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Hanno Beckera18d1322018-01-03 14:27:32 +00001307
1308
1309 /* Pick IV's (regardless of whether they
1310 * are being used by the transform). */
1311 ivlen = cipher_info->iv_size;
1312 memset( iv_enc, 0x3, sizeof( iv_enc ) );
1313 memset( iv_dec, 0x4, sizeof( iv_dec ) );
1314
1315 /*
1316 * Setup transforms
1317 */
1318
Jaeden Amero2de07f12019-06-05 13:32:08 +01001319#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
Hanno Beckerfd86ca82020-11-30 08:54:23 +00001320 defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Hanno Beckera18d1322018-01-03 14:27:32 +00001321 t_out->encrypt_then_mac = etm;
1322 t_in->encrypt_then_mac = etm;
1323#else
1324 ((void) etm);
1325#endif
1326
1327 t_out->minor_ver = ver;
1328 t_in->minor_ver = ver;
1329 t_out->ivlen = ivlen;
1330 t_in->ivlen = ivlen;
1331
1332 switch( cipher_info->mode )
1333 {
1334 case MBEDTLS_MODE_GCM:
1335 case MBEDTLS_MODE_CCM:
Ronald Cron6f135e12021-12-08 16:57:54 +01001336#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Beckere6832872020-05-28 08:29:58 +01001337 if( ver == MBEDTLS_SSL_MINOR_VERSION_4 )
1338 {
1339 t_out->fixed_ivlen = 12;
1340 t_in->fixed_ivlen = 12;
1341 }
1342 else
Ronald Cron6f135e12021-12-08 16:57:54 +01001343#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckere6832872020-05-28 08:29:58 +01001344 {
1345 t_out->fixed_ivlen = 4;
1346 t_in->fixed_ivlen = 4;
1347 }
Hanno Beckera18d1322018-01-03 14:27:32 +00001348 t_out->maclen = 0;
1349 t_in->maclen = 0;
1350 switch( tag_mode )
1351 {
1352 case 0: /* Full tag */
1353 t_out->taglen = 16;
1354 t_in->taglen = 16;
1355 break;
1356 case 1: /* Partial tag */
1357 t_out->taglen = 8;
1358 t_in->taglen = 8;
1359 break;
1360 default:
Paul Elliottc7b53742021-02-03 13:18:33 +00001361 ret = 1;
1362 goto cleanup;
Hanno Beckera18d1322018-01-03 14:27:32 +00001363 }
1364 break;
1365
1366 case MBEDTLS_MODE_CHACHAPOLY:
1367 t_out->fixed_ivlen = 12;
1368 t_in->fixed_ivlen = 12;
1369 t_out->maclen = 0;
1370 t_in->maclen = 0;
1371 switch( tag_mode )
1372 {
1373 case 0: /* Full tag */
1374 t_out->taglen = 16;
1375 t_in->taglen = 16;
1376 break;
1377 case 1: /* Partial tag */
1378 t_out->taglen = 8;
1379 t_in->taglen = 8;
1380 break;
1381 default:
Paul Elliottc7b53742021-02-03 13:18:33 +00001382 ret = 1;
1383 goto cleanup;
Hanno Beckera18d1322018-01-03 14:27:32 +00001384 }
1385 break;
1386
1387 case MBEDTLS_MODE_STREAM:
1388 case MBEDTLS_MODE_CBC:
1389 t_out->fixed_ivlen = 0; /* redundant, must be 0 */
1390 t_in->fixed_ivlen = 0; /* redundant, must be 0 */
1391 t_out->taglen = 0;
1392 t_in->taglen = 0;
1393 switch( tag_mode )
1394 {
1395 case 0: /* Full tag */
1396 t_out->maclen = maclen;
1397 t_in->maclen = maclen;
1398 break;
1399 case 1: /* Partial tag */
1400 t_out->maclen = 10;
1401 t_in->maclen = 10;
1402 break;
1403 default:
Paul Elliottc7b53742021-02-03 13:18:33 +00001404 ret = 1;
1405 goto cleanup;
Hanno Beckera18d1322018-01-03 14:27:32 +00001406 }
1407 break;
1408 default:
Paul Elliottc7b53742021-02-03 13:18:33 +00001409 ret = 1;
1410 goto cleanup;
Hanno Beckera18d1322018-01-03 14:27:32 +00001411 break;
1412 }
1413
1414 /* Setup IV's */
1415
1416 memcpy( &t_in->iv_dec, iv_dec, sizeof( iv_dec ) );
1417 memcpy( &t_in->iv_enc, iv_enc, sizeof( iv_enc ) );
1418 memcpy( &t_out->iv_dec, iv_enc, sizeof( iv_enc ) );
1419 memcpy( &t_out->iv_enc, iv_dec, sizeof( iv_dec ) );
1420
Hanno Beckera0e20d02019-05-15 14:03:01 +01001421#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd856c822019-04-29 17:30:59 +01001422 /* Add CID */
1423 memcpy( &t_in->in_cid, cid0, cid0_len );
1424 memcpy( &t_in->out_cid, cid1, cid1_len );
1425 t_in->in_cid_len = cid0_len;
1426 t_in->out_cid_len = cid1_len;
1427 memcpy( &t_out->in_cid, cid1, cid1_len );
1428 memcpy( &t_out->out_cid, cid0, cid0_len );
1429 t_out->in_cid_len = cid1_len;
1430 t_out->out_cid_len = cid0_len;
Hanno Beckera0e20d02019-05-15 14:03:01 +01001431#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerd856c822019-04-29 17:30:59 +01001432
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01001433#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemyslaw Stekielf57b4562022-01-25 00:04:18 +01001434 status = mbedtls_ssl_cipher_to_psa( cipher_type,
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01001435 t_in->taglen,
1436 &alg,
1437 &key_type,
1438 &key_bits );
1439
1440 if ( status != PSA_SUCCESS)
1441 {
Przemyslaw Stekiel77aec8d2022-01-31 20:22:53 +01001442 ret = psa_ssl_status_to_mbedtls( status );
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01001443 goto cleanup;
1444 }
1445
1446 t_in->psa_alg = alg;
1447 t_out->psa_alg = alg;
1448
1449 if ( alg != MBEDTLS_SSL_NULL_CIPHER )
1450 {
Przemyslaw Stekielf4ca3f02022-01-25 00:25:59 +01001451 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT );
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01001452 psa_set_key_algorithm( &attributes, alg );
1453 psa_set_key_type( &attributes, key_type );
1454
1455 status = psa_import_key( &attributes,
1456 key0,
1457 PSA_BITS_TO_BYTES( key_bits ),
1458 &t_in->psa_key_enc );
1459
1460 if ( status != PSA_SUCCESS)
1461 {
Przemyslaw Stekiel77aec8d2022-01-31 20:22:53 +01001462 ret = psa_ssl_status_to_mbedtls( status );
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01001463 goto cleanup;
1464 }
1465
1466 status = psa_import_key( &attributes,
1467 key1,
1468 PSA_BITS_TO_BYTES( key_bits ),
Przemyslaw Stekielf4ca3f02022-01-25 00:25:59 +01001469 &t_out->psa_key_enc );
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01001470
1471 if ( status != PSA_SUCCESS)
1472 {
Przemyslaw Stekiel77aec8d2022-01-31 20:22:53 +01001473 ret = psa_ssl_status_to_mbedtls( status );
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01001474 goto cleanup;
1475 }
1476
Przemyslaw Stekielf4ca3f02022-01-25 00:25:59 +01001477 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT );
1478
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01001479 status = psa_import_key( &attributes,
1480 key1,
1481 PSA_BITS_TO_BYTES( key_bits ),
Przemyslaw Stekielf4ca3f02022-01-25 00:25:59 +01001482 &t_in->psa_key_dec );
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01001483
1484 if ( status != PSA_SUCCESS)
1485 {
Przemyslaw Stekiel77aec8d2022-01-31 20:22:53 +01001486 ret = psa_ssl_status_to_mbedtls( status );
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01001487 goto cleanup;
1488 }
1489
1490 status = psa_import_key( &attributes,
1491 key0,
1492 PSA_BITS_TO_BYTES( key_bits ),
1493 &t_out->psa_key_dec );
1494
1495 if ( status != PSA_SUCCESS)
1496 {
Przemyslaw Stekiel77aec8d2022-01-31 20:22:53 +01001497 ret = psa_ssl_status_to_mbedtls( status );
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01001498 goto cleanup;
1499 }
1500 }
1501#endif /* MBEDTLS_USE_PSA_CRYPTO */
1502
Hanno Becker81e16a32019-03-01 11:21:44 +00001503cleanup:
1504
Hanno Becker3ee54212019-04-04 16:31:26 +01001505 mbedtls_free( key0 );
1506 mbedtls_free( key1 );
Hanno Becker81e16a32019-03-01 11:21:44 +00001507
Paul Elliott6f1eda72020-06-11 20:22:00 +01001508 mbedtls_free( md0 );
1509 mbedtls_free( md1 );
1510
Hanno Beckera5780f12019-04-05 09:55:37 +01001511 return( ret );
Hanno Beckera18d1322018-01-03 14:27:32 +00001512}
1513
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001514/*
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02001515 * Populate a session structure for serialization tests.
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001516 * Choose dummy values, mostly non-0 to distinguish from the init default.
1517 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01001518static int ssl_populate_session_tls12( mbedtls_ssl_session *session,
1519 int ticket_len,
1520 const char *crt_file )
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001521{
1522#if defined(MBEDTLS_HAVE_TIME)
1523 session->start = mbedtls_time( NULL ) - 42;
1524#endif
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01001525 session->minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001526 session->ciphersuite = 0xabcd;
1527 session->compression = 1;
1528 session->id_len = sizeof( session->id );
1529 memset( session->id, 66, session->id_len );
Manuel Pégourié-Gonnard220403b2019-05-24 09:54:21 +02001530 memset( session->master, 17, sizeof( session->master ) );
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001531
Manuel Pégourié-Gonnard1f6033a2019-05-24 10:17:52 +02001532#if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_FS_IO)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01001533 if( crt_file != NULL && strlen( crt_file ) != 0 )
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001534 {
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02001535 mbedtls_x509_crt tmp_crt;
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001536 int ret;
Manuel Pégourié-Gonnard6b840702019-05-24 09:40:17 +02001537
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02001538 mbedtls_x509_crt_init( &tmp_crt );
1539 ret = mbedtls_x509_crt_parse_file( &tmp_crt, crt_file );
1540 if( ret != 0 )
1541 return( ret );
1542
1543#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1544 /* Move temporary CRT. */
Manuel Pégourié-Gonnard6b840702019-05-24 09:40:17 +02001545 session->peer_cert = mbedtls_calloc( 1, sizeof( *session->peer_cert ) );
1546 if( session->peer_cert == NULL )
1547 return( -1 );
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02001548 *session->peer_cert = tmp_crt;
1549 memset( &tmp_crt, 0, sizeof( tmp_crt ) );
1550#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
1551 /* Calculate digest of temporary CRT. */
1552 session->peer_cert_digest =
1553 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
1554 if( session->peer_cert_digest == NULL )
1555 return( -1 );
1556 ret = mbedtls_md( mbedtls_md_info_from_type(
1557 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
1558 tmp_crt.raw.p, tmp_crt.raw.len,
1559 session->peer_cert_digest );
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001560 if( ret != 0 )
1561 return( ret );
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02001562 session->peer_cert_digest_type =
1563 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
1564 session->peer_cert_digest_len =
1565 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
1566#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
1567
1568 mbedtls_x509_crt_free( &tmp_crt );
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001569 }
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02001570#else /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_FS_IO */
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001571 (void) crt_file;
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02001572#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_FS_IO */
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001573 session->verify_result = 0xdeadbeef;
1574
1575#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
1576 if( ticket_len != 0 )
1577 {
1578 session->ticket = mbedtls_calloc( 1, ticket_len );
Manuel Pégourié-Gonnard220403b2019-05-24 09:54:21 +02001579 if( session->ticket == NULL )
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001580 return( -1 );
1581 memset( session->ticket, 33, ticket_len );
1582 }
1583 session->ticket_len = ticket_len;
1584 session->ticket_lifetime = 86401;
1585#else
1586 (void) ticket_len;
1587#endif
1588
1589#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1590 session->mfl_code = 1;
1591#endif
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001592#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1593 session->encrypt_then_mac = 1;
1594#endif
1595
1596 return( 0 );
1597}
1598
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001599/*
1600 * Perform data exchanging between \p ssl_1 and \p ssl_2 and check if the
1601 * message was sent in the correct number of fragments.
1602 *
1603 * /p ssl_1 and /p ssl_2 Endpoints represented by mbedtls_ssl_context. Both
1604 * of them must be initialized and connected beforehand.
1605 * /p msg_len_1 and /p msg_len_2 specify the size of the message to send.
1606 * /p expected_fragments_1 and /p expected_fragments_2 determine in how many
1607 * fragments the message should be sent.
1608 * expected_fragments is 0: can be used for DTLS testing while the message
1609 * size is larger than MFL. In that case the message
1610 * cannot be fragmented and sent to the second endpoint.
1611 * This value can be used for negative tests.
1612 * expected_fragments is 1: can be used for TLS/DTLS testing while the
1613 * message size is below MFL
1614 * expected_fragments > 1: can be used for TLS testing while the message
1615 * size is larger than MFL
1616 *
1617 * \retval 0 on success, otherwise error code.
1618 */
1619int mbedtls_exchange_data( mbedtls_ssl_context *ssl_1,
1620 int msg_len_1, const int expected_fragments_1,
1621 mbedtls_ssl_context *ssl_2,
1622 int msg_len_2, const int expected_fragments_2 )
1623{
1624 unsigned char *msg_buf_1 = malloc( msg_len_1 );
1625 unsigned char *msg_buf_2 = malloc( msg_len_2 );
1626 unsigned char *in_buf_1 = malloc( msg_len_2 );
1627 unsigned char *in_buf_2 = malloc( msg_len_1 );
1628 int msg_type, ret = -1;
1629
1630 /* Perform this test with two message types. At first use a message
1631 * consisting of only 0x00 for the client and only 0xFF for the server.
1632 * At the second time use message with generated data */
1633 for( msg_type = 0; msg_type < 2; msg_type++ )
1634 {
1635 int written_1 = 0;
1636 int written_2 = 0;
1637 int read_1 = 0;
1638 int read_2 = 0;
1639 int fragments_1 = 0;
1640 int fragments_2 = 0;
1641
1642 if( msg_type == 0 )
1643 {
1644 memset( msg_buf_1, 0x00, msg_len_1 );
1645 memset( msg_buf_2, 0xff, msg_len_2 );
1646 }
1647 else
1648 {
1649 int i, j = 0;
1650 for( i = 0; i < msg_len_1; i++ )
1651 {
1652 msg_buf_1[i] = j++ & 0xFF;
1653 }
1654 for( i = 0; i < msg_len_2; i++ )
1655 {
1656 msg_buf_2[i] = ( j -= 5 ) & 0xFF;
1657 }
1658 }
1659
1660 while( read_1 < msg_len_2 || read_2 < msg_len_1 )
1661 {
1662 /* ssl_1 sending */
1663 if( msg_len_1 > written_1 )
1664 {
1665 ret = mbedtls_ssl_write_fragment( ssl_1, msg_buf_1,
1666 msg_len_1, &written_1,
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001667 expected_fragments_1 );
1668 if( expected_fragments_1 == 0 )
1669 {
1670 /* This error is expected when the message is too large and
1671 * cannot be fragmented */
1672 TEST_ASSERT( ret == MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1673 msg_len_1 = 0;
1674 }
1675 else
1676 {
1677 TEST_ASSERT( ret == 0 );
1678 }
1679 }
1680
1681 /* ssl_2 sending */
1682 if( msg_len_2 > written_2 )
1683 {
1684 ret = mbedtls_ssl_write_fragment( ssl_2, msg_buf_2,
1685 msg_len_2, &written_2,
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001686 expected_fragments_2 );
1687 if( expected_fragments_2 == 0 )
1688 {
1689 /* This error is expected when the message is too large and
1690 * cannot be fragmented */
1691 TEST_ASSERT( ret == MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1692 msg_len_2 = 0;
1693 }
1694 else
1695 {
1696 TEST_ASSERT( ret == 0 );
1697 }
1698 }
1699
1700 /* ssl_1 reading */
1701 if( read_1 < msg_len_2 )
1702 {
1703 ret = mbedtls_ssl_read_fragment( ssl_1, in_buf_1,
1704 msg_len_2, &read_1,
Piotr Nowicki438bf3b2020-03-10 12:59:10 +01001705 &fragments_2,
1706 expected_fragments_2 );
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001707 TEST_ASSERT( ret == 0 );
1708 }
1709
1710 /* ssl_2 reading */
1711 if( read_2 < msg_len_1 )
1712 {
1713 ret = mbedtls_ssl_read_fragment( ssl_2, in_buf_2,
1714 msg_len_1, &read_2,
Piotr Nowicki438bf3b2020-03-10 12:59:10 +01001715 &fragments_1,
1716 expected_fragments_1 );
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001717 TEST_ASSERT( ret == 0 );
1718 }
1719 }
1720
1721 ret = -1;
1722 TEST_ASSERT( 0 == memcmp( msg_buf_1, in_buf_2, msg_len_1 ) );
1723 TEST_ASSERT( 0 == memcmp( msg_buf_2, in_buf_1, msg_len_2 ) );
1724 TEST_ASSERT( fragments_1 == expected_fragments_1 );
1725 TEST_ASSERT( fragments_2 == expected_fragments_2 );
1726 }
1727
1728 ret = 0;
1729
1730exit:
1731 free( msg_buf_1 );
1732 free( in_buf_1 );
1733 free( msg_buf_2 );
1734 free( in_buf_2 );
1735
1736 return ret;
1737}
1738
Piotr Nowicki95e9eb82020-02-14 11:33:34 +01001739/*
1740 * Perform data exchanging between \p ssl_1 and \p ssl_2. Both of endpoints
1741 * must be initialized and connected beforehand.
1742 *
1743 * \retval 0 on success, otherwise error code.
1744 */
1745int exchange_data( mbedtls_ssl_context *ssl_1,
1746 mbedtls_ssl_context *ssl_2 )
1747{
1748 return mbedtls_exchange_data( ssl_1, 256, 1,
1749 ssl_2, 256, 1 );
1750}
1751
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02001752#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
1753 defined(MBEDTLS_ENTROPY_C) && \
1754 defined(MBEDTLS_CTR_DRBG_C)
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001755void perform_handshake( handshake_test_options* options )
1756{
1757 /* forced_ciphersuite needs to last until the end of the handshake */
1758 int forced_ciphersuite[2];
1759 enum { BUFFSIZE = 17000 };
1760 mbedtls_endpoint client, server;
Gilles Peskineeccd8882020-03-10 12:19:08 +01001761#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001762 const char *psk_identity = "foo";
1763#endif
1764#if defined(MBEDTLS_TIMING_C)
1765 mbedtls_timing_delay_context timer_client, timer_server;
1766#endif
1767#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1768 unsigned char *context_buf = NULL;
1769 size_t context_buf_len;
1770#endif
1771#if defined(MBEDTLS_SSL_RENEGOTIATION)
1772 int ret = -1;
1773#endif
Paul Elliottc8570442020-04-15 17:00:50 +01001774 int expected_handshake_result = 0;
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001775
1776 mbedtls_test_message_queue server_queue, client_queue;
1777 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05001778 mbedtls_message_socket_init( &server_context );
1779 mbedtls_message_socket_init( &client_context );
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001780
1781 /* Client side */
1782 if( options->dtls != 0 )
1783 {
1784 TEST_ASSERT( mbedtls_endpoint_init( &client, MBEDTLS_SSL_IS_CLIENT,
1785 options->pk_alg, &client_context,
1786 &client_queue,
1787 &server_queue ) == 0 );
1788#if defined(MBEDTLS_TIMING_C)
1789 mbedtls_ssl_set_timer_cb( &client.ssl, &timer_client,
1790 mbedtls_timing_set_delay,
1791 mbedtls_timing_get_delay );
1792#endif
1793 }
1794 else
1795 {
1796 TEST_ASSERT( mbedtls_endpoint_init( &client, MBEDTLS_SSL_IS_CLIENT,
1797 options->pk_alg, NULL, NULL,
1798 NULL ) == 0 );
1799 }
Paul Elliottc8570442020-04-15 17:00:50 +01001800
1801 if( options->client_min_version != TEST_SSL_MINOR_VERSION_NONE )
1802 {
1803 mbedtls_ssl_conf_min_version( &client.conf, MBEDTLS_SSL_MAJOR_VERSION_3,
1804 options->client_min_version );
1805 }
1806
1807 if( options->client_max_version != TEST_SSL_MINOR_VERSION_NONE )
1808 {
1809 mbedtls_ssl_conf_max_version( &client.conf, MBEDTLS_SSL_MAJOR_VERSION_3,
1810 options->client_max_version );
1811 }
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001812
1813 if( strlen( options->cipher ) > 0 )
1814 {
1815 set_ciphersuite( &client.conf, options->cipher, forced_ciphersuite );
1816 }
Piotr Nowickibde7ee82020-02-21 10:59:50 +01001817
1818#if defined (MBEDTLS_DEBUG_C)
1819 if( options->cli_log_fun )
1820 {
1821 mbedtls_debug_set_threshold( 4 );
1822 mbedtls_ssl_conf_dbg( &client.conf, options->cli_log_fun,
1823 options->cli_log_obj );
1824 }
1825#endif
1826
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001827 /* Server side */
1828 if( options->dtls != 0 )
1829 {
1830 TEST_ASSERT( mbedtls_endpoint_init( &server, MBEDTLS_SSL_IS_SERVER,
1831 options->pk_alg, &server_context,
1832 &server_queue,
1833 &client_queue) == 0 );
1834#if defined(MBEDTLS_TIMING_C)
1835 mbedtls_ssl_set_timer_cb( &server.ssl, &timer_server,
1836 mbedtls_timing_set_delay,
1837 mbedtls_timing_get_delay );
1838#endif
1839 }
1840 else
1841 {
1842 TEST_ASSERT( mbedtls_endpoint_init( &server, MBEDTLS_SSL_IS_SERVER,
1843 options->pk_alg, NULL, NULL, NULL ) == 0 );
1844 }
Piotr Nowickibde7ee82020-02-21 10:59:50 +01001845
1846 mbedtls_ssl_conf_authmode( &server.conf, options->srv_auth_mode );
1847
Paul Elliottc8570442020-04-15 17:00:50 +01001848 if( options->server_min_version != TEST_SSL_MINOR_VERSION_NONE )
1849 {
1850 mbedtls_ssl_conf_min_version( &server.conf, MBEDTLS_SSL_MAJOR_VERSION_3,
1851 options->server_min_version );
1852 }
1853
1854 if( options->server_max_version != TEST_SSL_MINOR_VERSION_NONE )
1855 {
1856 mbedtls_ssl_conf_max_version( &server.conf, MBEDTLS_SSL_MAJOR_VERSION_3,
1857 options->server_max_version );
1858 }
1859
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001860#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1861 TEST_ASSERT( mbedtls_ssl_conf_max_frag_len( &(server.conf),
1862 (unsigned char) options->mfl ) == 0 );
1863 TEST_ASSERT( mbedtls_ssl_conf_max_frag_len( &(client.conf),
1864 (unsigned char) options->mfl ) == 0 );
1865#else
1866 TEST_ASSERT( MBEDTLS_SSL_MAX_FRAG_LEN_NONE == options->mfl );
1867#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
1868
Gilles Peskineeccd8882020-03-10 12:19:08 +01001869#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001870 if( options->psk_str != NULL && options->psk_str->len > 0 )
1871 {
1872 TEST_ASSERT( mbedtls_ssl_conf_psk( &client.conf, options->psk_str->x,
1873 options->psk_str->len,
1874 (const unsigned char *) psk_identity,
1875 strlen( psk_identity ) ) == 0 );
1876
1877 TEST_ASSERT( mbedtls_ssl_conf_psk( &server.conf, options->psk_str->x,
1878 options->psk_str->len,
1879 (const unsigned char *) psk_identity,
1880 strlen( psk_identity ) ) == 0 );
1881
1882 mbedtls_ssl_conf_psk_cb( &server.conf, psk_dummy_callback, NULL );
1883 }
1884#endif
1885#if defined(MBEDTLS_SSL_RENEGOTIATION)
1886 if( options->renegotiate )
1887 {
1888 mbedtls_ssl_conf_renegotiation( &(server.conf),
1889 MBEDTLS_SSL_RENEGOTIATION_ENABLED );
1890 mbedtls_ssl_conf_renegotiation( &(client.conf),
1891 MBEDTLS_SSL_RENEGOTIATION_ENABLED );
1892
1893 mbedtls_ssl_conf_legacy_renegotiation( &(server.conf),
1894 options->legacy_renegotiation );
1895 mbedtls_ssl_conf_legacy_renegotiation( &(client.conf),
1896 options->legacy_renegotiation );
1897 }
1898#endif /* MBEDTLS_SSL_RENEGOTIATION */
1899
Piotr Nowickibde7ee82020-02-21 10:59:50 +01001900#if defined (MBEDTLS_DEBUG_C)
1901 if( options->srv_log_fun )
1902 {
1903 mbedtls_debug_set_threshold( 4 );
1904 mbedtls_ssl_conf_dbg( &server.conf, options->srv_log_fun,
1905 options->srv_log_obj );
1906 }
1907#endif
1908
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001909 TEST_ASSERT( mbedtls_mock_socket_connect( &(client.socket),
1910 &(server.socket),
1911 BUFFSIZE ) == 0 );
1912
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001913#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1914 if( options->resize_buffers != 0 )
1915 {
1916 /* Ensure that the buffer sizes are appropriate before resizes */
1917 TEST_ASSERT( client.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN );
1918 TEST_ASSERT( client.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN );
1919 TEST_ASSERT( server.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN );
1920 TEST_ASSERT( server.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN );
1921 }
1922#endif
1923
Paul Elliottc8570442020-04-15 17:00:50 +01001924 if( options->expected_negotiated_version == TEST_SSL_MINOR_VERSION_NONE )
1925 {
Hanno Beckerbc000442021-06-24 09:18:19 +01001926 expected_handshake_result = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
Paul Elliottc8570442020-04-15 17:00:50 +01001927 }
1928
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001929 TEST_ASSERT( mbedtls_move_handshake_to_state( &(client.ssl),
1930 &(server.ssl),
1931 MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Elliottc8570442020-04-15 17:00:50 +01001932 == expected_handshake_result );
1933
1934 if( expected_handshake_result != 0 )
1935 {
1936 /* Connection will have failed by this point, skip to cleanup */
1937 goto exit;
1938 }
1939
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001940 TEST_ASSERT( client.ssl.state == MBEDTLS_SSL_HANDSHAKE_OVER );
1941 TEST_ASSERT( server.ssl.state == MBEDTLS_SSL_HANDSHAKE_OVER );
1942
Paul Elliottc8570442020-04-15 17:00:50 +01001943 /* Check that we agree on the version... */
1944 TEST_ASSERT( client.ssl.minor_ver == server.ssl.minor_ver );
1945
1946 /* And check that the version negotiated is the expected one. */
1947 TEST_EQUAL( client.ssl.minor_ver, options->expected_negotiated_version );
1948
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001949#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1950 if( options->resize_buffers != 0 )
1951 {
TRodziewicz2abf03c2021-06-25 14:40:09 +02001952 /* A server, when using DTLS, might delay a buffer resize to happen
1953 * after it receives a message, so we force it. */
1954 TEST_ASSERT( exchange_data( &(client.ssl), &(server.ssl) ) == 0 );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001955
TRodziewicz2abf03c2021-06-25 14:40:09 +02001956 TEST_ASSERT( client.ssl.out_buf_len ==
1957 mbedtls_ssl_get_output_buflen( &client.ssl ) );
1958 TEST_ASSERT( client.ssl.in_buf_len ==
1959 mbedtls_ssl_get_input_buflen( &client.ssl ) );
1960 TEST_ASSERT( server.ssl.out_buf_len ==
1961 mbedtls_ssl_get_output_buflen( &server.ssl ) );
1962 TEST_ASSERT( server.ssl.in_buf_len ==
1963 mbedtls_ssl_get_input_buflen( &server.ssl ) );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001964 }
1965#endif
1966
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001967 if( options->cli_msg_len != 0 || options->srv_msg_len != 0 )
1968 {
1969 /* Start data exchanging test */
1970 TEST_ASSERT( mbedtls_exchange_data( &(client.ssl), options->cli_msg_len,
1971 options->expected_cli_fragments,
1972 &(server.ssl), options->srv_msg_len,
1973 options->expected_srv_fragments )
1974 == 0 );
1975 }
1976#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1977 if( options->serialize == 1 )
1978 {
1979 TEST_ASSERT( options->dtls == 1 );
1980
1981 TEST_ASSERT( mbedtls_ssl_context_save( &(server.ssl), NULL,
1982 0, &context_buf_len )
1983 == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
1984
1985 context_buf = mbedtls_calloc( 1, context_buf_len );
1986 TEST_ASSERT( context_buf != NULL );
1987
1988 TEST_ASSERT( mbedtls_ssl_context_save( &(server.ssl), context_buf,
1989 context_buf_len,
1990 &context_buf_len ) == 0 );
1991
1992 mbedtls_ssl_free( &(server.ssl) );
1993 mbedtls_ssl_init( &(server.ssl) );
1994
1995 TEST_ASSERT( mbedtls_ssl_setup( &(server.ssl), &(server.conf) ) == 0 );
1996
1997 mbedtls_ssl_set_bio( &( server.ssl ), &server_context,
1998 mbedtls_mock_tcp_send_msg,
1999 mbedtls_mock_tcp_recv_msg,
2000 NULL );
2001
2002#if defined(MBEDTLS_TIMING_C)
2003 mbedtls_ssl_set_timer_cb( &server.ssl, &timer_server,
2004 mbedtls_timing_set_delay,
2005 mbedtls_timing_get_delay );
2006#endif
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05002007#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2008 if( options->resize_buffers != 0 )
2009 {
2010 /* Ensure that the buffer sizes are appropriate before resizes */
2011 TEST_ASSERT( server.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN );
2012 TEST_ASSERT( server.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN );
2013 }
2014#endif
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002015 TEST_ASSERT( mbedtls_ssl_context_load( &( server.ssl ), context_buf,
2016 context_buf_len ) == 0 );
2017
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05002018#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2019 /* Validate buffer sizes after context deserialization */
2020 if( options->resize_buffers != 0 )
2021 {
2022 TEST_ASSERT( server.ssl.out_buf_len ==
2023 mbedtls_ssl_get_output_buflen( &server.ssl ) );
2024 TEST_ASSERT( server.ssl.in_buf_len ==
2025 mbedtls_ssl_get_input_buflen( &server.ssl ) );
2026 }
2027#endif
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002028 /* Retest writing/reading */
2029 if( options->cli_msg_len != 0 || options->srv_msg_len != 0 )
2030 {
2031 TEST_ASSERT( mbedtls_exchange_data( &(client.ssl),
2032 options->cli_msg_len,
2033 options->expected_cli_fragments,
2034 &(server.ssl),
2035 options->srv_msg_len,
2036 options->expected_srv_fragments )
2037 == 0 );
2038 }
2039 }
2040#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05002041
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002042#if defined(MBEDTLS_SSL_RENEGOTIATION)
2043 if( options->renegotiate )
2044 {
2045 /* Start test with renegotiation */
2046 TEST_ASSERT( server.ssl.renego_status ==
2047 MBEDTLS_SSL_INITIAL_HANDSHAKE );
2048 TEST_ASSERT( client.ssl.renego_status ==
2049 MBEDTLS_SSL_INITIAL_HANDSHAKE );
2050
2051 /* After calling this function for the server, it only sends a handshake
2052 * request. All renegotiation should happen during data exchanging */
2053 TEST_ASSERT( mbedtls_ssl_renegotiate( &(server.ssl) ) == 0 );
2054 TEST_ASSERT( server.ssl.renego_status ==
2055 MBEDTLS_SSL_RENEGOTIATION_PENDING );
2056 TEST_ASSERT( client.ssl.renego_status ==
2057 MBEDTLS_SSL_INITIAL_HANDSHAKE );
2058
2059 TEST_ASSERT( exchange_data( &(client.ssl), &(server.ssl) ) == 0 );
2060 TEST_ASSERT( server.ssl.renego_status ==
2061 MBEDTLS_SSL_RENEGOTIATION_DONE );
2062 TEST_ASSERT( client.ssl.renego_status ==
2063 MBEDTLS_SSL_RENEGOTIATION_DONE );
2064
2065 /* After calling mbedtls_ssl_renegotiate for the client all renegotiation
2066 * should happen inside this function. However in this test, we cannot
2067 * perform simultaneous communication betwen client and server so this
2068 * function will return waiting error on the socket. All rest of
2069 * renegotiation should happen during data exchanging */
2070 ret = mbedtls_ssl_renegotiate( &(client.ssl) );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05002071#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2072 if( options->resize_buffers != 0 )
2073 {
2074 /* Ensure that the buffer sizes are appropriate before resizes */
2075 TEST_ASSERT( client.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN );
2076 TEST_ASSERT( client.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN );
2077 }
2078#endif
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002079 TEST_ASSERT( ret == 0 ||
2080 ret == MBEDTLS_ERR_SSL_WANT_READ ||
2081 ret == MBEDTLS_ERR_SSL_WANT_WRITE );
2082 TEST_ASSERT( server.ssl.renego_status ==
2083 MBEDTLS_SSL_RENEGOTIATION_DONE );
2084 TEST_ASSERT( client.ssl.renego_status ==
2085 MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS );
2086
2087 TEST_ASSERT( exchange_data( &(client.ssl), &(server.ssl) ) == 0 );
2088 TEST_ASSERT( server.ssl.renego_status ==
2089 MBEDTLS_SSL_RENEGOTIATION_DONE );
2090 TEST_ASSERT( client.ssl.renego_status ==
2091 MBEDTLS_SSL_RENEGOTIATION_DONE );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05002092#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2093 /* Validate buffer sizes after renegotiation */
2094 if( options->resize_buffers != 0 )
2095 {
2096 TEST_ASSERT( client.ssl.out_buf_len ==
2097 mbedtls_ssl_get_output_buflen( &client.ssl ) );
2098 TEST_ASSERT( client.ssl.in_buf_len ==
2099 mbedtls_ssl_get_input_buflen( &client.ssl ) );
2100 TEST_ASSERT( server.ssl.out_buf_len ==
2101 mbedtls_ssl_get_output_buflen( &server.ssl ) );
2102 TEST_ASSERT( server.ssl.in_buf_len ==
2103 mbedtls_ssl_get_input_buflen( &server.ssl ) );
2104 }
2105#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002106 }
2107#endif /* MBEDTLS_SSL_RENEGOTIATION */
2108
2109exit:
2110 mbedtls_endpoint_free( &client, options->dtls != 0 ? &client_context : NULL );
2111 mbedtls_endpoint_free( &server, options->dtls != 0 ? &server_context : NULL );
Piotr Nowickibde7ee82020-02-21 10:59:50 +01002112#if defined (MBEDTLS_DEBUG_C)
2113 if( options->cli_log_fun || options->srv_log_fun )
2114 {
2115 mbedtls_debug_set_threshold( 0 );
2116 }
2117#endif
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002118#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
2119 if( context_buf != NULL )
2120 mbedtls_free( context_buf );
2121#endif
2122}
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02002123#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002124
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002125/* END_HEADER */
2126
2127/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002128 * depends_on:MBEDTLS_SSL_TLS_C
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002129 * END_DEPENDENCIES
2130 */
2131
Janos Follath6264e662019-11-26 11:11:15 +00002132/* BEGIN_CASE */
2133void test_callback_buffer_sanity()
2134{
2135 enum { MSGLEN = 10 };
2136 mbedtls_test_buffer buf;
2137 unsigned char input[MSGLEN];
2138 unsigned char output[MSGLEN];
2139
2140 memset( input, 0, sizeof(input) );
2141
2142 /* Make sure calling put and get on NULL buffer results in error. */
2143 TEST_ASSERT( mbedtls_test_buffer_put( NULL, input, sizeof( input ) )
2144 == -1 );
2145 TEST_ASSERT( mbedtls_test_buffer_get( NULL, output, sizeof( output ) )
2146 == -1 );
2147 TEST_ASSERT( mbedtls_test_buffer_put( NULL, NULL, sizeof( input ) ) == -1 );
Andrzej Kurekf7774142020-01-22 06:34:59 -05002148
Janos Follath6264e662019-11-26 11:11:15 +00002149 TEST_ASSERT( mbedtls_test_buffer_put( NULL, NULL, 0 ) == -1 );
2150 TEST_ASSERT( mbedtls_test_buffer_get( NULL, NULL, 0 ) == -1 );
2151
2152 /* Make sure calling put and get on a buffer that hasn't been set up results
2153 * in eror. */
2154 mbedtls_test_buffer_init( &buf );
2155
2156 TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, sizeof( input ) ) == -1 );
2157 TEST_ASSERT( mbedtls_test_buffer_get( &buf, output, sizeof( output ) )
2158 == -1 );
2159 TEST_ASSERT( mbedtls_test_buffer_put( &buf, NULL, sizeof( input ) ) == -1 );
Andrzej Kurekf7774142020-01-22 06:34:59 -05002160
Janos Follath6264e662019-11-26 11:11:15 +00002161 TEST_ASSERT( mbedtls_test_buffer_put( &buf, NULL, 0 ) == -1 );
2162 TEST_ASSERT( mbedtls_test_buffer_get( &buf, NULL, 0 ) == -1 );
2163
Andrzej Kurekf7774142020-01-22 06:34:59 -05002164 /* Make sure calling put and get on NULL input only results in
2165 * error if the length is not zero, and that a NULL output is valid for data
2166 * dropping.
2167 */
Janos Follath6264e662019-11-26 11:11:15 +00002168
2169 TEST_ASSERT( mbedtls_test_buffer_setup( &buf, sizeof( input ) ) == 0 );
2170
2171 TEST_ASSERT( mbedtls_test_buffer_put( &buf, NULL, sizeof( input ) ) == -1 );
2172 TEST_ASSERT( mbedtls_test_buffer_get( &buf, NULL, sizeof( output ) )
Andrzej Kurekf7774142020-01-22 06:34:59 -05002173 == 0 );
Janos Follath6264e662019-11-26 11:11:15 +00002174 TEST_ASSERT( mbedtls_test_buffer_put( &buf, NULL, 0 ) == 0 );
2175 TEST_ASSERT( mbedtls_test_buffer_get( &buf, NULL, 0 ) == 0 );
2176
Piotr Nowickifb437d72020-01-13 16:59:12 +01002177 /* Make sure calling put several times in the row is safe */
2178
2179 TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, sizeof( input ) )
2180 == sizeof( input ) );
2181 TEST_ASSERT( mbedtls_test_buffer_get( &buf, output, 2 ) == 2 );
2182 TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, 1 ) == 1 );
2183 TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, 2 ) == 1 );
2184 TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, 2 ) == 0 );
2185
2186
Janos Follath6264e662019-11-26 11:11:15 +00002187exit:
2188
2189 mbedtls_test_buffer_free( &buf );
2190}
2191/* END_CASE */
2192
2193/*
2194 * Test if the implementation of `mbedtls_test_buffer` related functions is
2195 * correct and works as expected.
2196 *
2197 * That is
2198 * - If we try to put in \p put1 bytes then we can put in \p put1_ret bytes.
2199 * - Afterwards if we try to get \p get1 bytes then we can get \get1_ret bytes.
2200 * - Next, if we try to put in \p put1 bytes then we can put in \p put1_ret
2201 * bytes.
2202 * - Afterwards if we try to get \p get1 bytes then we can get \get1_ret bytes.
2203 * - All of the bytes we got match the bytes we put in in a FIFO manner.
2204 */
2205
2206/* BEGIN_CASE */
2207void test_callback_buffer( int size, int put1, int put1_ret,
2208 int get1, int get1_ret, int put2, int put2_ret,
2209 int get2, int get2_ret )
2210{
2211 enum { ROUNDS = 2 };
2212 size_t put[ROUNDS];
2213 int put_ret[ROUNDS];
2214 size_t get[ROUNDS];
2215 int get_ret[ROUNDS];
2216 mbedtls_test_buffer buf;
2217 unsigned char* input = NULL;
2218 size_t input_len;
2219 unsigned char* output = NULL;
2220 size_t output_len;
Janos Follath031827f2019-11-27 11:12:14 +00002221 size_t i, j, written, read;
Janos Follath6264e662019-11-26 11:11:15 +00002222
2223 mbedtls_test_buffer_init( &buf );
2224 TEST_ASSERT( mbedtls_test_buffer_setup( &buf, size ) == 0 );
2225
2226 /* Check the sanity of input parameters and initialise local variables. That
2227 * is, ensure that the amount of data is not negative and that we are not
2228 * expecting more to put or get than we actually asked for. */
2229 TEST_ASSERT( put1 >= 0 );
2230 put[0] = put1;
2231 put_ret[0] = put1_ret;
2232 TEST_ASSERT( put1_ret <= put1 );
2233 TEST_ASSERT( put2 >= 0 );
2234 put[1] = put2;
2235 put_ret[1] = put2_ret;
2236 TEST_ASSERT( put2_ret <= put2 );
2237
2238 TEST_ASSERT( get1 >= 0 );
2239 get[0] = get1;
2240 get_ret[0] = get1_ret;
2241 TEST_ASSERT( get1_ret <= get1 );
2242 TEST_ASSERT( get2 >= 0 );
2243 get[1] = get2;
2244 get_ret[1] = get2_ret;
2245 TEST_ASSERT( get2_ret <= get2 );
2246
2247 input_len = 0;
2248 /* Calculate actual input and output lengths */
2249 for( j = 0; j < ROUNDS; j++ )
2250 {
2251 if( put_ret[j] > 0 )
2252 {
2253 input_len += put_ret[j];
2254 }
2255 }
2256 /* In order to always have a valid pointer we always allocate at least 1
2257 * byte. */
2258 if( input_len == 0 )
2259 input_len = 1;
2260 ASSERT_ALLOC( input, input_len );
2261
2262 output_len = 0;
2263 for( j = 0; j < ROUNDS; j++ )
2264 {
2265 if( get_ret[j] > 0 )
2266 {
2267 output_len += get_ret[j];
2268 }
2269 }
2270 TEST_ASSERT( output_len <= input_len );
2271 /* In order to always have a valid pointer we always allocate at least 1
2272 * byte. */
2273 if( output_len == 0 )
2274 output_len = 1;
2275 ASSERT_ALLOC( output, output_len );
2276
2277 /* Fill up the buffer with structured data so that unwanted changes
2278 * can be detected */
2279 for( i = 0; i < input_len; i++ )
2280 {
2281 input[i] = i & 0xFF;
2282 }
2283
2284 written = read = 0;
2285 for( j = 0; j < ROUNDS; j++ )
2286 {
2287 TEST_ASSERT( put_ret[j] == mbedtls_test_buffer_put( &buf,
2288 input + written, put[j] ) );
2289 written += put_ret[j];
2290 TEST_ASSERT( get_ret[j] == mbedtls_test_buffer_get( &buf,
2291 output + read, get[j] ) );
2292 read += get_ret[j];
2293 TEST_ASSERT( read <= written );
2294 if( get_ret[j] > 0 )
2295 {
2296 TEST_ASSERT( memcmp( output + read - get_ret[j],
2297 input + read - get_ret[j], get_ret[j] )
2298 == 0 );
2299 }
2300 }
2301
2302exit:
2303
2304 mbedtls_free( input );
2305 mbedtls_free( output );
2306 mbedtls_test_buffer_free( &buf );
2307}
2308/* END_CASE */
2309
Janos Follath031827f2019-11-27 11:12:14 +00002310/*
Janos Follathc673c2c2019-12-02 15:47:26 +00002311 * Test if the implementation of `mbedtls_mock_socket` related I/O functions is
2312 * correct and works as expected on unconnected sockets.
2313 */
2314
2315/* BEGIN_CASE */
2316void ssl_mock_sanity( )
2317{
2318 enum { MSGLEN = 105 };
Paul Elliott21c8fe52021-11-24 16:54:26 +00002319 unsigned char message[MSGLEN] = { 0 };
2320 unsigned char received[MSGLEN] = { 0 };
Janos Follathc673c2c2019-12-02 15:47:26 +00002321 mbedtls_mock_socket socket;
2322
2323 mbedtls_mock_socket_init( &socket );
2324 TEST_ASSERT( mbedtls_mock_tcp_send_b( &socket, message, MSGLEN ) < 0 );
2325 mbedtls_mock_socket_close( &socket );
2326 mbedtls_mock_socket_init( &socket );
2327 TEST_ASSERT( mbedtls_mock_tcp_recv_b( &socket, received, MSGLEN ) < 0 );
2328 mbedtls_mock_socket_close( &socket );
2329
2330 mbedtls_mock_socket_init( &socket );
2331 TEST_ASSERT( mbedtls_mock_tcp_send_nb( &socket, message, MSGLEN ) < 0 );
2332 mbedtls_mock_socket_close( &socket );
2333 mbedtls_mock_socket_init( &socket );
2334 TEST_ASSERT( mbedtls_mock_tcp_recv_nb( &socket, received, MSGLEN ) < 0 );
2335 mbedtls_mock_socket_close( &socket );
2336
2337exit:
2338
2339 mbedtls_mock_socket_close( &socket );
2340}
2341/* END_CASE */
2342
2343/*
2344 * Test if the implementation of `mbedtls_mock_socket` related functions can
2345 * send a single message from the client to the server.
Janos Follath031827f2019-11-27 11:12:14 +00002346 */
2347
2348/* BEGIN_CASE */
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002349void ssl_mock_tcp( int blocking )
Janos Follath031827f2019-11-27 11:12:14 +00002350{
Janos Follathc673c2c2019-12-02 15:47:26 +00002351 enum { MSGLEN = 105 };
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002352 enum { BUFLEN = MSGLEN / 5 };
Janos Follathc673c2c2019-12-02 15:47:26 +00002353 unsigned char message[MSGLEN];
2354 unsigned char received[MSGLEN];
2355 mbedtls_mock_socket client;
2356 mbedtls_mock_socket server;
2357 size_t written, read;
2358 int send_ret, recv_ret;
2359 mbedtls_ssl_send_t *send;
2360 mbedtls_ssl_recv_t *recv;
Janos Follathc673c2c2019-12-02 15:47:26 +00002361 unsigned i;
2362
2363 if( blocking == 0 )
2364 {
2365 send = mbedtls_mock_tcp_send_nb;
2366 recv = mbedtls_mock_tcp_recv_nb;
2367 }
2368 else
2369 {
2370 send = mbedtls_mock_tcp_send_b;
2371 recv = mbedtls_mock_tcp_recv_b;
2372 }
2373
2374 mbedtls_mock_socket_init( &client );
2375 mbedtls_mock_socket_init( &server );
2376
2377 /* Fill up the buffer with structured data so that unwanted changes
2378 * can be detected */
2379 for( i = 0; i < MSGLEN; i++ )
2380 {
2381 message[i] = i & 0xFF;
2382 }
2383
2384 /* Make sure that sending a message takes a few iterations. */
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002385 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, BUFLEN ) );
Janos Follathc673c2c2019-12-02 15:47:26 +00002386
2387 /* Send the message to the server */
2388 send_ret = recv_ret = 1;
2389 written = read = 0;
2390 while( send_ret != 0 || recv_ret != 0 )
2391 {
2392 send_ret = send( &client, message + written, MSGLEN - written );
2393
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002394 TEST_ASSERT( send_ret >= 0 );
2395 TEST_ASSERT( send_ret <= BUFLEN );
2396 written += send_ret;
2397
2398 /* If the buffer is full we can test blocking and non-blocking send */
2399 if ( send_ret == BUFLEN )
Janos Follathc673c2c2019-12-02 15:47:26 +00002400 {
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002401 int blocking_ret = send( &client, message , 1 );
2402 if ( blocking )
2403 {
2404 TEST_ASSERT( blocking_ret == 0 );
2405 }
2406 else
2407 {
2408 TEST_ASSERT( blocking_ret == MBEDTLS_ERR_SSL_WANT_WRITE );
2409 }
Janos Follathc673c2c2019-12-02 15:47:26 +00002410 }
Janos Follathc673c2c2019-12-02 15:47:26 +00002411
2412 recv_ret = recv( &server, received + read, MSGLEN - read );
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002413
2414 /* The result depends on whether any data was sent */
2415 if ( send_ret > 0 )
Janos Follathc673c2c2019-12-02 15:47:26 +00002416 {
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002417 TEST_ASSERT( recv_ret > 0 );
2418 TEST_ASSERT( recv_ret <= BUFLEN );
2419 read += recv_ret;
2420 }
2421 else if( blocking )
2422 {
2423 TEST_ASSERT( recv_ret == 0 );
Janos Follathc673c2c2019-12-02 15:47:26 +00002424 }
2425 else
2426 {
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002427 TEST_ASSERT( recv_ret == MBEDTLS_ERR_SSL_WANT_READ );
2428 recv_ret = 0;
Janos Follathc673c2c2019-12-02 15:47:26 +00002429 }
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002430
2431 /* If the buffer is empty we can test blocking and non-blocking read */
2432 if ( recv_ret == BUFLEN )
2433 {
2434 int blocking_ret = recv( &server, received, 1 );
2435 if ( blocking )
2436 {
2437 TEST_ASSERT( blocking_ret == 0 );
2438 }
2439 else
2440 {
2441 TEST_ASSERT( blocking_ret == MBEDTLS_ERR_SSL_WANT_READ );
2442 }
2443 }
Janos Follathc673c2c2019-12-02 15:47:26 +00002444 }
2445 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
2446
2447exit:
2448
2449 mbedtls_mock_socket_close( &client );
2450 mbedtls_mock_socket_close( &server );
2451}
2452/* END_CASE */
2453
2454/*
2455 * Test if the implementation of `mbedtls_mock_socket` related functions can
2456 * send messages in both direction at the same time (with the I/O calls
2457 * interleaving).
2458 */
2459
2460/* BEGIN_CASE */
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002461void ssl_mock_tcp_interleaving( int blocking )
Janos Follathc673c2c2019-12-02 15:47:26 +00002462{
Janos Follath031827f2019-11-27 11:12:14 +00002463 enum { ROUNDS = 2 };
2464 enum { MSGLEN = 105 };
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002465 enum { BUFLEN = MSGLEN / 5 };
Janos Follath031827f2019-11-27 11:12:14 +00002466 unsigned char message[ROUNDS][MSGLEN];
2467 unsigned char received[ROUNDS][MSGLEN];
2468 mbedtls_mock_socket client;
2469 mbedtls_mock_socket server;
2470 size_t written[ROUNDS];
2471 size_t read[ROUNDS];
2472 int send_ret[ROUNDS];
2473 int recv_ret[ROUNDS];
2474 unsigned i, j, progress;
Janos Follath3766ba52019-11-27 13:31:42 +00002475 mbedtls_ssl_send_t *send;
2476 mbedtls_ssl_recv_t *recv;
Janos Follath3766ba52019-11-27 13:31:42 +00002477
2478 if( blocking == 0 )
2479 {
2480 send = mbedtls_mock_tcp_send_nb;
2481 recv = mbedtls_mock_tcp_recv_nb;
2482 }
2483 else
2484 {
2485 send = mbedtls_mock_tcp_send_b;
2486 recv = mbedtls_mock_tcp_recv_b;
2487 }
Janos Follath031827f2019-11-27 11:12:14 +00002488
2489 mbedtls_mock_socket_init( &client );
2490 mbedtls_mock_socket_init( &server );
2491
2492 /* Fill up the buffers with structured data so that unwanted changes
2493 * can be detected */
2494 for( i = 0; i < ROUNDS; i++ )
2495 {
2496 for( j = 0; j < MSGLEN; j++ )
2497 {
2498 message[i][j] = ( i * MSGLEN + j ) & 0xFF;
2499 }
2500 }
2501
Janos Follath031827f2019-11-27 11:12:14 +00002502 /* Make sure that sending a message takes a few iterations. */
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002503 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, BUFLEN ) );
Janos Follath031827f2019-11-27 11:12:14 +00002504
Janos Follath031827f2019-11-27 11:12:14 +00002505 /* Send the message from both sides, interleaving. */
2506 progress = 1;
2507 for( i = 0; i < ROUNDS; i++ )
2508 {
2509 written[i] = 0;
2510 read[i] = 0;
2511 }
2512 /* This loop does not stop as long as there was a successful write or read
2513 * of at least one byte on either side. */
2514 while( progress != 0 )
2515 {
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002516 mbedtls_mock_socket *socket;
Janos Follath031827f2019-11-27 11:12:14 +00002517
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002518 for( i = 0; i < ROUNDS; i++ )
Janos Follath3766ba52019-11-27 13:31:42 +00002519 {
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002520 /* First sending is from the client */
2521 socket = ( i % 2 == 0 ) ? ( &client ) : ( &server );
Janos Follath031827f2019-11-27 11:12:14 +00002522
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002523 send_ret[i] = send( socket, message[i] + written[i],
2524 MSGLEN - written[i] );
2525 TEST_ASSERT( send_ret[i] >= 0 );
2526 TEST_ASSERT( send_ret[i] <= BUFLEN );
2527 written[i] += send_ret[i];
Janos Follath031827f2019-11-27 11:12:14 +00002528
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002529 /* If the buffer is full we can test blocking and non-blocking
2530 * send */
2531 if ( send_ret[i] == BUFLEN )
2532 {
2533 int blocking_ret = send( socket, message[i] , 1 );
2534 if ( blocking )
2535 {
2536 TEST_ASSERT( blocking_ret == 0 );
2537 }
2538 else
2539 {
2540 TEST_ASSERT( blocking_ret == MBEDTLS_ERR_SSL_WANT_WRITE );
2541 }
2542 }
Janos Follath3766ba52019-11-27 13:31:42 +00002543 }
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002544
2545 for( i = 0; i < ROUNDS; i++ )
Janos Follath3766ba52019-11-27 13:31:42 +00002546 {
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002547 /* First receiving is from the server */
2548 socket = ( i % 2 == 0 ) ? ( &server ) : ( &client );
2549
2550 recv_ret[i] = recv( socket, received[i] + read[i],
2551 MSGLEN - read[i] );
2552
2553 /* The result depends on whether any data was sent */
2554 if ( send_ret[i] > 0 )
2555 {
2556 TEST_ASSERT( recv_ret[i] > 0 );
2557 TEST_ASSERT( recv_ret[i] <= BUFLEN );
2558 read[i] += recv_ret[i];
2559 }
2560 else if( blocking )
2561 {
2562 TEST_ASSERT( recv_ret[i] == 0 );
2563 }
2564 else
2565 {
2566 TEST_ASSERT( recv_ret[i] == MBEDTLS_ERR_SSL_WANT_READ );
2567 recv_ret[i] = 0;
2568 }
2569
2570 /* If the buffer is empty we can test blocking and non-blocking
2571 * read */
2572 if ( recv_ret[i] == BUFLEN )
2573 {
2574 int blocking_ret = recv( socket, received[i], 1 );
2575 if ( blocking )
2576 {
2577 TEST_ASSERT( blocking_ret == 0 );
2578 }
2579 else
2580 {
2581 TEST_ASSERT( blocking_ret == MBEDTLS_ERR_SSL_WANT_READ );
2582 }
2583 }
Janos Follath3766ba52019-11-27 13:31:42 +00002584 }
Janos Follath031827f2019-11-27 11:12:14 +00002585
2586 progress = 0;
2587 for( i = 0; i < ROUNDS; i++ )
2588 {
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002589 progress += send_ret[i] + recv_ret[i];
Janos Follath031827f2019-11-27 11:12:14 +00002590 }
2591 }
2592
2593 for( i = 0; i < ROUNDS; i++ )
2594 TEST_ASSERT( memcmp( message[i], received[i], MSGLEN ) == 0 );
2595
2596exit:
2597
2598 mbedtls_mock_socket_close( &client );
2599 mbedtls_mock_socket_close( &server );
2600}
2601/* END_CASE */
2602
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002603/* BEGIN_CASE */
2604void ssl_message_queue_sanity( )
2605{
2606 mbedtls_test_message_queue queue;
2607
2608 /* Trying to push/pull to an empty queue */
2609 TEST_ASSERT( mbedtls_test_message_queue_push_info( NULL, 1 )
2610 == MBEDTLS_TEST_ERROR_ARG_NULL );
2611 TEST_ASSERT( mbedtls_test_message_queue_pop_info( NULL, 1 )
2612 == MBEDTLS_TEST_ERROR_ARG_NULL );
2613
Andrzej Kurek89bdc582020-03-09 06:29:43 -04002614 TEST_ASSERT( mbedtls_test_message_queue_setup( &queue, 3 ) == 0 );
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002615 TEST_ASSERT( queue.capacity == 3 );
2616 TEST_ASSERT( queue.num == 0 );
2617
2618exit:
2619 mbedtls_test_message_queue_free( &queue );
2620}
2621/* END_CASE */
2622
2623/* BEGIN_CASE */
2624void ssl_message_queue_basic( )
2625{
2626 mbedtls_test_message_queue queue;
2627
Andrzej Kurek89bdc582020-03-09 06:29:43 -04002628 TEST_ASSERT( mbedtls_test_message_queue_setup( &queue, 3 ) == 0 );
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002629
2630 /* Sanity test - 3 pushes and 3 pops with sufficient space */
2631 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 );
2632 TEST_ASSERT( queue.capacity == 3 );
2633 TEST_ASSERT( queue.num == 1 );
2634 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 );
2635 TEST_ASSERT( queue.capacity == 3 );
2636 TEST_ASSERT( queue.num == 2 );
2637 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 2 ) == 2 );
2638 TEST_ASSERT( queue.capacity == 3 );
2639 TEST_ASSERT( queue.num == 3 );
2640
2641 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 );
2642 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 );
2643 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 2 ) == 2 );
2644
2645exit:
2646 mbedtls_test_message_queue_free( &queue );
2647}
2648/* END_CASE */
2649
2650/* BEGIN_CASE */
2651void ssl_message_queue_overflow_underflow( )
2652{
2653 mbedtls_test_message_queue queue;
2654
Andrzej Kurek89bdc582020-03-09 06:29:43 -04002655 TEST_ASSERT( mbedtls_test_message_queue_setup( &queue, 3 ) == 0 );
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002656
2657 /* 4 pushes (last one with an error), 4 pops (last one with an error) */
2658 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 );
2659 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 );
2660 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 2 ) == 2 );
2661 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 3 )
Andrzej Kurekf46b9122020-02-07 08:19:00 -05002662 == MBEDTLS_ERR_SSL_WANT_WRITE );
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002663
2664 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 );
2665 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 );
2666 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 2 ) == 2 );
2667
2668 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 )
Andrzej Kurekf46b9122020-02-07 08:19:00 -05002669 == MBEDTLS_ERR_SSL_WANT_READ );
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002670
2671exit:
2672 mbedtls_test_message_queue_free( &queue );
2673}
2674/* END_CASE */
2675
2676/* BEGIN_CASE */
2677void ssl_message_queue_interleaved( )
2678{
2679 mbedtls_test_message_queue queue;
2680
Andrzej Kurek89bdc582020-03-09 06:29:43 -04002681 TEST_ASSERT( mbedtls_test_message_queue_setup( &queue, 3 ) == 0 );
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002682
2683 /* Interleaved test - [2 pushes, 1 pop] twice, and then two pops
2684 * (to wrap around the buffer) */
2685 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 );
2686 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 );
2687
2688 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 );
2689
2690 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 2 ) == 2 );
2691 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 3 ) == 3 );
2692
2693 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 );
2694 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 2 ) == 2 );
2695
2696 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 5 ) == 5 );
2697 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 8 ) == 8 );
2698
2699 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 3 ) == 3 );
2700
2701 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 5 ) == 5 );
2702
2703 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 8 ) == 8 );
2704
2705exit:
2706 mbedtls_test_message_queue_free( &queue );
2707}
2708/* END_CASE */
2709
2710/* BEGIN_CASE */
2711void ssl_message_queue_insufficient_buffer( )
2712{
2713 mbedtls_test_message_queue queue;
2714 size_t message_len = 10;
2715 size_t buffer_len = 5;
2716
Andrzej Kurek89bdc582020-03-09 06:29:43 -04002717 TEST_ASSERT( mbedtls_test_message_queue_setup( &queue, 1 ) == 0 );
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002718
2719 /* Popping without a sufficient buffer */
2720 TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, message_len )
2721 == (int) message_len );
2722 TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, buffer_len )
2723 == (int) buffer_len );
2724exit:
2725 mbedtls_test_message_queue_free( &queue );
2726}
2727/* END_CASE */
2728
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002729/* BEGIN_CASE */
2730void ssl_message_mock_uninitialized( )
2731{
2732 enum { MSGLEN = 10 };
Shawn Carey03092f52021-05-13 10:38:32 -04002733 unsigned char message[MSGLEN] = {0}, received[MSGLEN];
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002734 mbedtls_mock_socket client, server;
2735 mbedtls_test_message_queue server_queue, client_queue;
2736 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05002737 mbedtls_message_socket_init( &server_context );
2738 mbedtls_message_socket_init( &client_context );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002739
2740 /* Send with a NULL context */
2741 TEST_ASSERT( mbedtls_mock_tcp_send_msg( NULL, message, MSGLEN )
2742 == MBEDTLS_TEST_ERROR_CONTEXT_ERROR );
2743
2744 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( NULL, message, MSGLEN )
2745 == MBEDTLS_TEST_ERROR_CONTEXT_ERROR );
2746
2747 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 1,
2748 &server,
2749 &server_context ) == 0 );
2750
2751 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 1,
2752 &client,
2753 &client_context ) == 0 );
2754
2755 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, MSGLEN )
2756 == MBEDTLS_TEST_ERROR_SEND_FAILED );
2757
2758 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
Andrzej Kurekf46b9122020-02-07 08:19:00 -05002759 == MBEDTLS_ERR_SSL_WANT_READ );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002760
2761 /* Push directly to a queue to later simulate a disconnected behavior */
2762 TEST_ASSERT( mbedtls_test_message_queue_push_info( &server_queue, MSGLEN )
2763 == MSGLEN );
2764
2765 /* Test if there's an error when trying to read from a disconnected
2766 * socket */
2767 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
2768 == MBEDTLS_TEST_ERROR_RECV_FAILED );
2769 exit:
2770 mbedtls_message_socket_close( &server_context );
2771 mbedtls_message_socket_close( &client_context );
2772}
2773/* END_CASE */
2774
2775/* BEGIN_CASE */
2776void ssl_message_mock_basic( )
2777{
2778 enum { MSGLEN = 10 };
2779 unsigned char message[MSGLEN], received[MSGLEN];
2780 mbedtls_mock_socket client, server;
2781 unsigned i;
2782 mbedtls_test_message_queue server_queue, client_queue;
2783 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05002784 mbedtls_message_socket_init( &server_context );
2785 mbedtls_message_socket_init( &client_context );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002786
2787 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 1,
2788 &server,
2789 &server_context ) == 0 );
2790
2791 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 1,
2792 &client,
2793 &client_context ) == 0 );
2794
2795 /* Fill up the buffer with structured data so that unwanted changes
2796 * can be detected */
2797 for( i = 0; i < MSGLEN; i++ )
2798 {
2799 message[i] = i & 0xFF;
2800 }
2801 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
2802 MSGLEN ) );
2803
2804 /* Send the message to the server */
2805 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2806 MSGLEN ) == MSGLEN );
2807
2808 /* Read from the server */
2809 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
2810 == MSGLEN );
2811
2812 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
2813 memset( received, 0, MSGLEN );
2814
2815 /* Send the message to the client */
2816 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &server_context, message,
2817 MSGLEN ) == MSGLEN );
2818
2819 /* Read from the client */
2820 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &client_context, received, MSGLEN )
2821 == MSGLEN );
2822 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
2823
2824 exit:
2825 mbedtls_message_socket_close( &server_context );
2826 mbedtls_message_socket_close( &client_context );
2827}
2828/* END_CASE */
2829
2830/* BEGIN_CASE */
2831void ssl_message_mock_queue_overflow_underflow( )
2832{
2833 enum { MSGLEN = 10 };
2834 unsigned char message[MSGLEN], received[MSGLEN];
2835 mbedtls_mock_socket client, server;
2836 unsigned i;
2837 mbedtls_test_message_queue server_queue, client_queue;
2838 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05002839 mbedtls_message_socket_init( &server_context );
2840 mbedtls_message_socket_init( &client_context );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002841
2842 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 2,
2843 &server,
2844 &server_context ) == 0 );
2845
2846 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 2,
2847 &client,
2848 &client_context ) == 0 );
2849
2850 /* Fill up the buffer with structured data so that unwanted changes
2851 * can be detected */
2852 for( i = 0; i < MSGLEN; i++ )
2853 {
2854 message[i] = i & 0xFF;
2855 }
2856 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
2857 MSGLEN*2 ) );
2858
2859 /* Send three message to the server, last one with an error */
2860 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2861 MSGLEN - 1 ) == MSGLEN - 1 );
2862
2863 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2864 MSGLEN ) == MSGLEN );
2865
2866 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2867 MSGLEN )
Andrzej Kurekf46b9122020-02-07 08:19:00 -05002868 == MBEDTLS_ERR_SSL_WANT_WRITE );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002869
2870 /* Read three messages from the server, last one with an error */
2871 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received,
2872 MSGLEN - 1 ) == MSGLEN - 1 );
2873
2874 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
2875 == MSGLEN );
2876
2877 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
2878
2879 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
Andrzej Kurekf46b9122020-02-07 08:19:00 -05002880 == MBEDTLS_ERR_SSL_WANT_READ );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002881
2882 exit:
2883 mbedtls_message_socket_close( &server_context );
2884 mbedtls_message_socket_close( &client_context );
2885}
2886/* END_CASE */
2887
2888/* BEGIN_CASE */
2889void ssl_message_mock_socket_overflow( )
2890{
2891 enum { MSGLEN = 10 };
2892 unsigned char message[MSGLEN], received[MSGLEN];
2893 mbedtls_mock_socket client, server;
2894 unsigned i;
2895 mbedtls_test_message_queue server_queue, client_queue;
2896 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05002897 mbedtls_message_socket_init( &server_context );
2898 mbedtls_message_socket_init( &client_context );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002899
2900 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 2,
2901 &server,
2902 &server_context ) == 0 );
2903
2904 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 2,
2905 &client,
2906 &client_context ) == 0 );
2907
2908 /* Fill up the buffer with structured data so that unwanted changes
2909 * can be detected */
2910 for( i = 0; i < MSGLEN; i++ )
2911 {
2912 message[i] = i & 0xFF;
2913 }
2914 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
2915 MSGLEN ) );
2916
2917 /* Send two message to the server, second one with an error */
2918 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2919 MSGLEN ) == MSGLEN );
2920
2921 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2922 MSGLEN )
2923 == MBEDTLS_TEST_ERROR_SEND_FAILED );
2924
2925 /* Read the only message from the server */
2926 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
2927 == MSGLEN );
2928
2929 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
2930
2931 exit:
2932 mbedtls_message_socket_close( &server_context );
2933 mbedtls_message_socket_close( &client_context );
2934}
2935/* END_CASE */
2936
2937/* BEGIN_CASE */
2938void ssl_message_mock_truncated( )
2939{
2940 enum { MSGLEN = 10 };
2941 unsigned char message[MSGLEN], received[MSGLEN];
2942 mbedtls_mock_socket client, server;
2943 unsigned i;
2944 mbedtls_test_message_queue server_queue, client_queue;
2945 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05002946 mbedtls_message_socket_init( &server_context );
2947 mbedtls_message_socket_init( &client_context );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05002948
2949 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 2,
2950 &server,
2951 &server_context ) == 0 );
2952
2953 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 2,
2954 &client,
2955 &client_context ) == 0 );
2956
2957 memset( received, 0, MSGLEN );
2958 /* Fill up the buffer with structured data so that unwanted changes
2959 * can be detected */
2960 for( i = 0; i < MSGLEN; i++ )
2961 {
2962 message[i] = i & 0xFF;
2963 }
2964 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
2965 2 * MSGLEN ) );
2966
2967 /* Send two messages to the server, the second one small enough to fit in the
2968 * receiver's buffer. */
2969 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2970 MSGLEN ) == MSGLEN );
2971 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
2972 MSGLEN / 2 ) == MSGLEN / 2 );
2973 /* Read a truncated message from the server */
2974 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN/2 )
2975 == MSGLEN/2 );
2976
2977 /* Test that the first half of the message is valid, and second one isn't */
2978 TEST_ASSERT( memcmp( message, received, MSGLEN/2 ) == 0 );
2979 TEST_ASSERT( memcmp( message + MSGLEN/2, received + MSGLEN/2, MSGLEN/2 )
2980 != 0 );
2981 memset( received, 0, MSGLEN );
2982
2983 /* Read a full message from the server */
2984 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN/2 )
2985 == MSGLEN / 2 );
2986
2987 /* Test that the first half of the message is valid */
2988 TEST_ASSERT( memcmp( message, received, MSGLEN/2 ) == 0 );
2989
2990 exit:
2991 mbedtls_message_socket_close( &server_context );
2992 mbedtls_message_socket_close( &client_context );
2993}
2994/* END_CASE */
2995
2996/* BEGIN_CASE */
2997void ssl_message_mock_socket_read_error( )
2998{
2999 enum { MSGLEN = 10 };
3000 unsigned char message[MSGLEN], received[MSGLEN];
3001 mbedtls_mock_socket client, server;
3002 unsigned i;
3003 mbedtls_test_message_queue server_queue, client_queue;
3004 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05003005 mbedtls_message_socket_init( &server_context );
3006 mbedtls_message_socket_init( &client_context );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003007
3008 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 1,
3009 &server,
3010 &server_context ) == 0 );
3011
3012 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 1,
3013 &client,
3014 &client_context ) == 0 );
3015
3016 /* Fill up the buffer with structured data so that unwanted changes
3017 * can be detected */
3018 for( i = 0; i < MSGLEN; i++ )
3019 {
3020 message[i] = i & 0xFF;
3021 }
3022 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
3023 MSGLEN ) );
3024
3025 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
3026 MSGLEN ) == MSGLEN );
3027
3028 /* Force a read error by disconnecting the socket by hand */
3029 server.status = 0;
3030 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
3031 == MBEDTLS_TEST_ERROR_RECV_FAILED );
3032 /* Return to a valid state */
3033 server.status = MBEDTLS_MOCK_SOCKET_CONNECTED;
3034
3035 memset( received, 0, sizeof( received ) );
3036
3037 /* Test that even though the server tried to read once disconnected, the
3038 * continuity is preserved */
3039 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
3040 == MSGLEN );
3041
3042 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
3043
3044 exit:
3045 mbedtls_message_socket_close( &server_context );
3046 mbedtls_message_socket_close( &client_context );
3047}
3048/* END_CASE */
3049
3050/* BEGIN_CASE */
3051void ssl_message_mock_interleaved_one_way( )
3052{
3053 enum { MSGLEN = 10 };
3054 unsigned char message[MSGLEN], received[MSGLEN];
3055 mbedtls_mock_socket client, server;
3056 unsigned i;
3057 mbedtls_test_message_queue server_queue, client_queue;
3058 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05003059 mbedtls_message_socket_init( &server_context );
3060 mbedtls_message_socket_init( &client_context );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003061
3062 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 3,
3063 &server,
3064 &server_context ) == 0 );
3065
3066 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 3,
3067 &client,
3068 &client_context ) == 0 );
3069
3070 /* Fill up the buffer with structured data so that unwanted changes
3071 * can be detected */
3072 for( i = 0; i < MSGLEN; i++ )
3073 {
3074 message[i] = i & 0xFF;
3075 }
3076 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
3077 MSGLEN*3 ) );
3078
3079 /* Interleaved test - [2 sends, 1 read] twice, and then two reads
3080 * (to wrap around the buffer) */
3081 for( i = 0; i < 2; i++ )
3082 {
3083 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
3084 MSGLEN ) == MSGLEN );
3085
3086 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
3087 MSGLEN ) == MSGLEN );
3088
3089 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received,
3090 MSGLEN ) == MSGLEN );
3091 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
3092 memset( received, 0, sizeof( received ) );
3093 }
3094
3095 for( i = 0; i < 2; i++ )
3096 {
3097 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received,
3098 MSGLEN ) == MSGLEN );
3099
3100 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
3101 }
3102 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
Andrzej Kurekf46b9122020-02-07 08:19:00 -05003103 == MBEDTLS_ERR_SSL_WANT_READ );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003104 exit:
3105 mbedtls_message_socket_close( &server_context );
3106 mbedtls_message_socket_close( &client_context );
3107}
3108/* END_CASE */
3109
3110/* BEGIN_CASE */
3111void ssl_message_mock_interleaved_two_ways( )
3112{
3113 enum { MSGLEN = 10 };
3114 unsigned char message[MSGLEN], received[MSGLEN];
3115 mbedtls_mock_socket client, server;
3116 unsigned i;
3117 mbedtls_test_message_queue server_queue, client_queue;
3118 mbedtls_test_message_socket_context server_context, client_context;
Andrzej Kurek45916ba2020-03-05 14:46:22 -05003119 mbedtls_message_socket_init( &server_context );
3120 mbedtls_message_socket_init( &client_context );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003121
3122 TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 3,
3123 &server,
3124 &server_context ) == 0 );
3125
3126 TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 3,
3127 &client,
3128 &client_context ) == 0 );
3129
3130 /* Fill up the buffer with structured data so that unwanted changes
3131 * can be detected */
3132 for( i = 0; i < MSGLEN; i++ )
3133 {
3134 message[i] = i & 0xFF;
3135 }
3136 TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server,
3137 MSGLEN*3 ) );
3138
3139 /* Interleaved test - [2 sends, 1 read] twice, both ways, and then two reads
3140 * (to wrap around the buffer) both ways. */
3141 for( i = 0; i < 2; i++ )
3142 {
3143 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
3144 MSGLEN ) == MSGLEN );
3145
3146 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message,
3147 MSGLEN ) == MSGLEN );
3148
3149 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &server_context, message,
3150 MSGLEN ) == MSGLEN );
3151
3152 TEST_ASSERT( mbedtls_mock_tcp_send_msg( &server_context, message,
3153 MSGLEN ) == MSGLEN );
3154
3155 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received,
3156 MSGLEN ) == MSGLEN );
3157
3158 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
3159
3160 memset( received, 0, sizeof( received ) );
3161
3162 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &client_context, received,
3163 MSGLEN ) == MSGLEN );
3164
3165 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
3166
3167 memset( received, 0, sizeof( received ) );
3168 }
3169
3170 for( i = 0; i < 2; i++ )
3171 {
3172 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received,
3173 MSGLEN ) == MSGLEN );
3174
3175 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
3176 memset( received, 0, sizeof( received ) );
3177
3178 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &client_context, received,
3179 MSGLEN ) == MSGLEN );
3180
3181 TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 );
3182 memset( received, 0, sizeof( received ) );
3183 }
3184
3185 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN )
Andrzej Kurekf46b9122020-02-07 08:19:00 -05003186 == MBEDTLS_ERR_SSL_WANT_READ );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003187
3188 TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &client_context, received, MSGLEN )
Andrzej Kurekf46b9122020-02-07 08:19:00 -05003189 == MBEDTLS_ERR_SSL_WANT_READ );
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003190 exit:
3191 mbedtls_message_socket_close( &server_context );
3192 mbedtls_message_socket_close( &client_context );
3193}
3194/* END_CASE */
3195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003196/* BEGIN_CASE depends_on:MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Azim Khan5fcca462018-06-29 11:05:32 +01003197void ssl_dtls_replay( data_t * prevs, data_t * new, int ret )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003198{
Azim Khand30ca132017-06-09 04:32:58 +01003199 uint32_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003200 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02003201 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003202
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02003203 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02003204 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02003205
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02003206 TEST_ASSERT( mbedtls_ssl_config_defaults( &conf,
3207 MBEDTLS_SSL_IS_CLIENT,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02003208 MBEDTLS_SSL_TRANSPORT_DATAGRAM,
3209 MBEDTLS_SSL_PRESET_DEFAULT ) == 0 );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02003210 TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003211
3212 /* Read previous record numbers */
Azim Khand30ca132017-06-09 04:32:58 +01003213 for( len = 0; len < prevs->len; len += 6 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003214 {
Azim Khand30ca132017-06-09 04:32:58 +01003215 memcpy( ssl.in_ctr + 2, prevs->x + len, 6 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003216 mbedtls_ssl_dtls_replay_update( &ssl );
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003217 }
3218
3219 /* Check new number */
Azim Khand30ca132017-06-09 04:32:58 +01003220 memcpy( ssl.in_ctr + 2, new->x, 6 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003221 TEST_ASSERT( mbedtls_ssl_dtls_replay_check( &ssl ) == ret );
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003223 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02003224 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003225}
3226/* END_CASE */
Hanno Beckerb25c0c72017-05-05 11:24:30 +01003227
3228/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
3229void ssl_set_hostname_twice( char *hostname0, char *hostname1 )
3230{
3231 mbedtls_ssl_context ssl;
3232 mbedtls_ssl_init( &ssl );
3233
3234 TEST_ASSERT( mbedtls_ssl_set_hostname( &ssl, hostname0 ) == 0 );
3235 TEST_ASSERT( mbedtls_ssl_set_hostname( &ssl, hostname1 ) == 0 );
3236
3237 mbedtls_ssl_free( &ssl );
3238}
Darryl Green11999bb2018-03-13 15:22:58 +00003239/* END_CASE */
Hanno Beckera18d1322018-01-03 14:27:32 +00003240
3241/* BEGIN_CASE */
3242void ssl_crypt_record( int cipher_type, int hash_id,
Hanno Beckerd856c822019-04-29 17:30:59 +01003243 int etm, int tag_mode, int ver,
3244 int cid0_len, int cid1_len )
Hanno Beckera18d1322018-01-03 14:27:32 +00003245{
3246 /*
3247 * Test several record encryptions and decryptions
3248 * with plenty of space before and after the data
3249 * within the record buffer.
3250 */
3251
3252 int ret;
3253 int num_records = 16;
3254 mbedtls_ssl_context ssl; /* ONLY for debugging */
3255
3256 mbedtls_ssl_transform t0, t1;
Hanno Becker81e16a32019-03-01 11:21:44 +00003257 unsigned char *buf = NULL;
Hanno Beckera18d1322018-01-03 14:27:32 +00003258 size_t const buflen = 512;
3259 mbedtls_record rec, rec_backup;
3260
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01003261 USE_PSA_INIT( );
3262
Hanno Beckera18d1322018-01-03 14:27:32 +00003263 mbedtls_ssl_init( &ssl );
3264 mbedtls_ssl_transform_init( &t0 );
3265 mbedtls_ssl_transform_init( &t1 );
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01003266 ret = build_transforms( &t0, &t1, cipher_type, hash_id,
3267 etm, tag_mode, ver,
3268 (size_t) cid0_len,
3269 (size_t) cid1_len );
3270
3271 TEST_ASSERT( ret == 0 );
Hanno Beckera18d1322018-01-03 14:27:32 +00003272
Hanno Becker3ee54212019-04-04 16:31:26 +01003273 TEST_ASSERT( ( buf = mbedtls_calloc( 1, buflen ) ) != NULL );
Hanno Beckera18d1322018-01-03 14:27:32 +00003274
3275 while( num_records-- > 0 )
3276 {
3277 mbedtls_ssl_transform *t_dec, *t_enc;
3278 /* Take turns in who's sending and who's receiving. */
3279 if( num_records % 3 == 0 )
3280 {
3281 t_dec = &t0;
3282 t_enc = &t1;
3283 }
3284 else
3285 {
3286 t_dec = &t1;
3287 t_enc = &t0;
3288 }
3289
3290 /*
3291 * The record header affects the transformation in two ways:
3292 * 1) It determines the AEAD additional data
3293 * 2) The record counter sometimes determines the IV.
3294 *
3295 * Apart from that, the fields don't have influence.
3296 * In particular, it is currently not the responsibility
3297 * of ssl_encrypt/decrypt_buf to check if the transform
3298 * version matches the record version, or that the
3299 * type is sensible.
3300 */
3301
3302 memset( rec.ctr, num_records, sizeof( rec.ctr ) );
3303 rec.type = 42;
3304 rec.ver[0] = num_records;
3305 rec.ver[1] = num_records;
Hanno Beckera0e20d02019-05-15 14:03:01 +01003306#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd856c822019-04-29 17:30:59 +01003307 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01003308#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera18d1322018-01-03 14:27:32 +00003309
3310 rec.buf = buf;
3311 rec.buf_len = buflen;
3312 rec.data_offset = 16;
3313 /* Make sure to vary the length to exercise different
3314 * paddings. */
3315 rec.data_len = 1 + num_records;
3316
3317 memset( rec.buf + rec.data_offset, 42, rec.data_len );
3318
3319 /* Make a copy for later comparison */
3320 rec_backup = rec;
3321
3322 /* Encrypt record */
3323 ret = mbedtls_ssl_encrypt_buf( &ssl, t_enc, &rec,
Ronald Cron351f0ee2020-06-10 12:12:18 +02003324 mbedtls_test_rnd_std_rand, NULL );
Hanno Beckera18d1322018-01-03 14:27:32 +00003325 TEST_ASSERT( ret == 0 || ret == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3326 if( ret != 0 )
3327 {
3328 continue;
3329 }
3330
Hanno Beckerb2713ab2020-05-07 14:54:22 +01003331#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3332 if( rec.cid_len != 0 )
3333 {
3334 /* DTLS 1.2 + CID hides the real content type and
3335 * uses a special CID content type in the protected
3336 * record. Double-check this. */
3337 TEST_ASSERT( rec.type == MBEDTLS_SSL_MSG_CID );
3338 }
3339#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3340
Ronald Cron6f135e12021-12-08 16:57:54 +01003341#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Beckerb2713ab2020-05-07 14:54:22 +01003342 if( t_enc->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
3343 {
3344 /* TLS 1.3 hides the real content type and
3345 * always uses Application Data as the content type
3346 * for protected records. Double-check this. */
3347 TEST_ASSERT( rec.type == MBEDTLS_SSL_MSG_APPLICATION_DATA );
3348 }
Ronald Cron6f135e12021-12-08 16:57:54 +01003349#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb2713ab2020-05-07 14:54:22 +01003350
Hanno Beckera18d1322018-01-03 14:27:32 +00003351 /* Decrypt record with t_dec */
Hanno Beckerd856c822019-04-29 17:30:59 +01003352 ret = mbedtls_ssl_decrypt_buf( &ssl, t_dec, &rec );
3353 TEST_ASSERT( ret == 0 );
Hanno Beckera18d1322018-01-03 14:27:32 +00003354
3355 /* Compare results */
3356 TEST_ASSERT( rec.type == rec_backup.type );
3357 TEST_ASSERT( memcmp( rec.ctr, rec_backup.ctr, 8 ) == 0 );
3358 TEST_ASSERT( rec.ver[0] == rec_backup.ver[0] );
3359 TEST_ASSERT( rec.ver[1] == rec_backup.ver[1] );
3360 TEST_ASSERT( rec.data_len == rec_backup.data_len );
3361 TEST_ASSERT( rec.data_offset == rec_backup.data_offset );
3362 TEST_ASSERT( memcmp( rec.buf + rec.data_offset,
3363 rec_backup.buf + rec_backup.data_offset,
3364 rec.data_len ) == 0 );
3365 }
3366
Hanno Becker81e16a32019-03-01 11:21:44 +00003367exit:
3368
Hanno Beckera18d1322018-01-03 14:27:32 +00003369 /* Cleanup */
3370 mbedtls_ssl_free( &ssl );
3371 mbedtls_ssl_transform_free( &t0 );
3372 mbedtls_ssl_transform_free( &t1 );
3373
Hanno Becker3ee54212019-04-04 16:31:26 +01003374 mbedtls_free( buf );
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01003375 USE_PSA_DONE( );
Hanno Beckera18d1322018-01-03 14:27:32 +00003376}
3377/* END_CASE */
Hanno Beckerb3268da2018-01-05 15:20:24 +00003378
3379/* BEGIN_CASE */
3380void ssl_crypt_record_small( int cipher_type, int hash_id,
Hanno Beckerd856c822019-04-29 17:30:59 +01003381 int etm, int tag_mode, int ver,
3382 int cid0_len, int cid1_len )
Hanno Beckerb3268da2018-01-05 15:20:24 +00003383{
3384 /*
3385 * Test pairs of encryption and decryption with an increasing
3386 * amount of space in the record buffer - in more detail:
3387 * 1) Try to encrypt with 0, 1, 2, ... bytes available
3388 * in front of the plaintext, and expect the encryption
3389 * to succeed starting from some offset. Always keep
3390 * enough space in the end of the buffer.
3391 * 2) Try to encrypt with 0, 1, 2, ... bytes available
3392 * at the end of the plaintext, and expect the encryption
3393 * to succeed starting from some offset. Always keep
3394 * enough space at the beginning of the buffer.
3395 * 3) Try to encrypt with 0, 1, 2, ... bytes available
3396 * both at the front and end of the plaintext,
3397 * and expect the encryption to succeed starting from
3398 * some offset.
3399 *
3400 * If encryption succeeds, check that decryption succeeds
3401 * and yields the original record.
3402 */
3403
3404 mbedtls_ssl_context ssl; /* ONLY for debugging */
3405
3406 mbedtls_ssl_transform t0, t1;
Hanno Becker81e16a32019-03-01 11:21:44 +00003407 unsigned char *buf = NULL;
Hanno Beckerd856c822019-04-29 17:30:59 +01003408 size_t const buflen = 256;
Hanno Beckerb3268da2018-01-05 15:20:24 +00003409 mbedtls_record rec, rec_backup;
3410
3411 int ret;
Hanno Beckerd856c822019-04-29 17:30:59 +01003412 int mode; /* Mode 1, 2 or 3 as explained above */
3413 size_t offset; /* Available space at beginning/end/both */
3414 size_t threshold = 96; /* Maximum offset to test against */
Hanno Beckerb3268da2018-01-05 15:20:24 +00003415
Hanno Beckerd856c822019-04-29 17:30:59 +01003416 size_t default_pre_padding = 64; /* Pre-padding to use in mode 2 */
3417 size_t default_post_padding = 128; /* Post-padding to use in mode 1 */
Hanno Beckerb3268da2018-01-05 15:20:24 +00003418
3419 int seen_success; /* Indicates if in the current mode we've
3420 * already seen a successful test. */
3421
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01003422 USE_PSA_INIT( );
3423
Hanno Beckerb3268da2018-01-05 15:20:24 +00003424 mbedtls_ssl_init( &ssl );
3425 mbedtls_ssl_transform_init( &t0 );
3426 mbedtls_ssl_transform_init( &t1 );
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01003427 ret = build_transforms( &t0, &t1, cipher_type, hash_id,
Hanno Beckerd856c822019-04-29 17:30:59 +01003428 etm, tag_mode, ver,
3429 (size_t) cid0_len,
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01003430 (size_t) cid1_len );
3431
3432 TEST_ASSERT( ret == 0 );
Hanno Beckerb3268da2018-01-05 15:20:24 +00003433
Hanno Becker3ee54212019-04-04 16:31:26 +01003434 TEST_ASSERT( ( buf = mbedtls_calloc( 1, buflen ) ) != NULL );
Hanno Beckerb3268da2018-01-05 15:20:24 +00003435
3436 for( mode=1; mode <= 3; mode++ )
3437 {
3438 seen_success = 0;
3439 for( offset=0; offset <= threshold; offset++ )
3440 {
3441 mbedtls_ssl_transform *t_dec, *t_enc;
Hanno Becker6c87b3f2019-04-29 17:24:44 +01003442 t_dec = &t0;
3443 t_enc = &t1;
Hanno Beckerb3268da2018-01-05 15:20:24 +00003444
3445 memset( rec.ctr, offset, sizeof( rec.ctr ) );
3446 rec.type = 42;
3447 rec.ver[0] = offset;
3448 rec.ver[1] = offset;
3449 rec.buf = buf;
3450 rec.buf_len = buflen;
Hanno Beckera0e20d02019-05-15 14:03:01 +01003451#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd856c822019-04-29 17:30:59 +01003452 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01003453#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerb3268da2018-01-05 15:20:24 +00003454
3455 switch( mode )
3456 {
3457 case 1: /* Space in the beginning */
3458 rec.data_offset = offset;
3459 rec.data_len = buflen - offset - default_post_padding;
3460 break;
3461
3462 case 2: /* Space in the end */
3463 rec.data_offset = default_pre_padding;
3464 rec.data_len = buflen - default_pre_padding - offset;
3465 break;
3466
3467 case 3: /* Space in the beginning and end */
3468 rec.data_offset = offset;
3469 rec.data_len = buflen - 2 * offset;
3470 break;
3471
3472 default:
3473 TEST_ASSERT( 0 );
3474 break;
3475 }
3476
3477 memset( rec.buf + rec.data_offset, 42, rec.data_len );
3478
3479 /* Make a copy for later comparison */
3480 rec_backup = rec;
3481
3482 /* Encrypt record */
Ronald Cron6c5bd7f2020-06-10 14:08:26 +02003483 ret = mbedtls_ssl_encrypt_buf( &ssl, t_enc, &rec,
3484 mbedtls_test_rnd_std_rand, NULL );
Hanno Beckerb3268da2018-01-05 15:20:24 +00003485
3486 if( ( mode == 1 || mode == 2 ) && seen_success )
3487 {
3488 TEST_ASSERT( ret == 0 );
3489 }
3490 else
3491 {
3492 TEST_ASSERT( ret == 0 || ret == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
3493 if( ret == 0 )
3494 seen_success = 1;
3495 }
3496
3497 if( ret != 0 )
3498 continue;
3499
Hanno Beckerb2713ab2020-05-07 14:54:22 +01003500#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3501 if( rec.cid_len != 0 )
3502 {
3503 /* DTLS 1.2 + CID hides the real content type and
3504 * uses a special CID content type in the protected
3505 * record. Double-check this. */
3506 TEST_ASSERT( rec.type == MBEDTLS_SSL_MSG_CID );
3507 }
3508#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3509
Ronald Cron6f135e12021-12-08 16:57:54 +01003510#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Hanno Beckerb2713ab2020-05-07 14:54:22 +01003511 if( t_enc->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
3512 {
3513 /* TLS 1.3 hides the real content type and
3514 * always uses Application Data as the content type
3515 * for protected records. Double-check this. */
3516 TEST_ASSERT( rec.type == MBEDTLS_SSL_MSG_APPLICATION_DATA );
3517 }
Ronald Cron6f135e12021-12-08 16:57:54 +01003518#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb2713ab2020-05-07 14:54:22 +01003519
Hanno Beckerb3268da2018-01-05 15:20:24 +00003520 /* Decrypt record with t_dec */
3521 TEST_ASSERT( mbedtls_ssl_decrypt_buf( &ssl, t_dec, &rec ) == 0 );
3522
3523 /* Compare results */
3524 TEST_ASSERT( rec.type == rec_backup.type );
3525 TEST_ASSERT( memcmp( rec.ctr, rec_backup.ctr, 8 ) == 0 );
3526 TEST_ASSERT( rec.ver[0] == rec_backup.ver[0] );
3527 TEST_ASSERT( rec.ver[1] == rec_backup.ver[1] );
3528 TEST_ASSERT( rec.data_len == rec_backup.data_len );
3529 TEST_ASSERT( rec.data_offset == rec_backup.data_offset );
3530 TEST_ASSERT( memcmp( rec.buf + rec.data_offset,
3531 rec_backup.buf + rec_backup.data_offset,
3532 rec.data_len ) == 0 );
3533 }
3534
3535 TEST_ASSERT( seen_success == 1 );
3536 }
3537
Hanno Becker81e16a32019-03-01 11:21:44 +00003538exit:
3539
Hanno Beckerb3268da2018-01-05 15:20:24 +00003540 /* Cleanup */
3541 mbedtls_ssl_free( &ssl );
3542 mbedtls_ssl_transform_free( &t0 );
3543 mbedtls_ssl_transform_free( &t1 );
3544
Hanno Becker3ee54212019-04-04 16:31:26 +01003545 mbedtls_free( buf );
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01003546 USE_PSA_DONE( );
Hanno Beckerb3268da2018-01-05 15:20:24 +00003547}
3548/* END_CASE */
Ron Eldor824ad7b2019-05-13 14:09:00 +03003549
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01003550/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC:MBEDTLS_AES_C:MBEDTLS_SSL_PROTO_TLS1_2:!MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003551void ssl_decrypt_non_etm_cbc( int cipher_type, int hash_id, int trunc_hmac,
Manuel Pégourié-Gonnard864abbf2020-07-21 10:37:14 +02003552 int length_selector )
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003553{
3554 /*
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003555 * Test record decryption for CBC without EtM, focused on the verification
3556 * of padding and MAC.
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003557 *
TRodziewicz299510e2021-07-09 16:55:11 +02003558 * Actually depends on TLS 1.2 and either AES, ARIA or Camellia, but since
3559 * the test framework doesn't support alternation in dependency statements,
3560 * just depend on AES.
Manuel Pégourié-Gonnard864abbf2020-07-21 10:37:14 +02003561 *
3562 * The length_selector argument is interpreted as follows:
3563 * - if it's -1, the plaintext length is 0 and minimal padding is applied
3564 * - if it's -2, the plaintext length is 0 and maximal padding is applied
3565 * - otherwise it must be in [0, 255] and is padding_length from RFC 5246:
3566 * it's the length of the rest of the padding, that is, excluding the
3567 * byte that encodes the length. The minimal non-zero plaintext length
3568 * that gives this padding_length is automatically selected.
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003569 */
3570 mbedtls_ssl_context ssl; /* ONLY for debugging */
3571 mbedtls_ssl_transform t0, t1;
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003572 mbedtls_record rec, rec_save;
3573 unsigned char *buf = NULL, *buf_save = NULL;
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003574 size_t buflen, olen = 0;
Manuel Pégourié-Gonnard864abbf2020-07-21 10:37:14 +02003575 size_t plaintext_len, block_size, i;
Manuel Pégourié-Gonnarde55653f2020-07-22 11:42:57 +02003576 unsigned char padlen; /* excluding the padding_length byte */
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003577 unsigned char add_data[13];
3578 unsigned char mac[MBEDTLS_MD_MAX_SIZE];
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003579 int exp_ret;
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01003580 int ret;
Manuel Pégourié-Gonnard4adc04a2020-07-16 10:00:48 +02003581 const unsigned char pad_max_len = 255; /* Per the standard */
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003582
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01003583 USE_PSA_INIT( );
3584
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003585 mbedtls_ssl_init( &ssl );
3586 mbedtls_ssl_transform_init( &t0 );
3587 mbedtls_ssl_transform_init( &t1 );
3588
3589 /* Set up transforms with dummy keys */
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01003590 ret = build_transforms( &t0, &t1, cipher_type, hash_id,
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003591 0, trunc_hmac,
3592 MBEDTLS_SSL_MINOR_VERSION_3,
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01003593 0 , 0 );
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003594
Przemyslaw Stekiel4a36dd32022-01-25 00:43:58 +01003595 TEST_ASSERT( ret == 0 );
3596
Manuel Pégourié-Gonnard864abbf2020-07-21 10:37:14 +02003597 /* Determine padding/plaintext length */
3598 TEST_ASSERT( length_selector >= -2 && length_selector <= 255 );
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003599 block_size = t0.ivlen;
Manuel Pégourié-Gonnard864abbf2020-07-21 10:37:14 +02003600 if( length_selector < 0 )
3601 {
3602 plaintext_len = 0;
3603
Manuel Pégourié-Gonnarde55653f2020-07-22 11:42:57 +02003604 /* Minimal padding
3605 * The +1 is for the padding_length byte, not counted in padlen. */
Manuel Pégourié-Gonnard864abbf2020-07-21 10:37:14 +02003606 padlen = block_size - ( t0.maclen + 1 ) % block_size;
3607
3608 /* Maximal padding? */
3609 if( length_selector == -2 )
3610 padlen += block_size * ( ( pad_max_len - padlen ) / block_size );
3611 }
3612 else
3613 {
3614 padlen = length_selector;
3615
Manuel Pégourié-Gonnarde55653f2020-07-22 11:42:57 +02003616 /* Minimal non-zero plaintext_length giving desired padding.
3617 * The +1 is for the padding_length byte, not counted in padlen. */
Manuel Pégourié-Gonnard864abbf2020-07-21 10:37:14 +02003618 plaintext_len = block_size - ( padlen + t0.maclen + 1 ) % block_size;
3619 }
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003620
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003621 /* Prepare a buffer for record data */
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003622 buflen = block_size
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003623 + plaintext_len
3624 + t0.maclen
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003625 + padlen + 1;
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003626 ASSERT_ALLOC( buf, buflen );
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003627 ASSERT_ALLOC( buf_save, buflen );
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003628
3629 /* Prepare a dummy record header */
3630 memset( rec.ctr, 0, sizeof( rec.ctr ) );
3631 rec.type = MBEDTLS_SSL_MSG_APPLICATION_DATA;
3632 rec.ver[0] = MBEDTLS_SSL_MAJOR_VERSION_3;
3633 rec.ver[1] = MBEDTLS_SSL_MINOR_VERSION_3;
3634#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3635 rec.cid_len = 0;
3636#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3637
3638 /* Prepare dummy record content */
3639 rec.buf = buf;
3640 rec.buf_len = buflen;
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003641 rec.data_offset = block_size;
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003642 rec.data_len = plaintext_len;
3643 memset( rec.buf + rec.data_offset, 42, rec.data_len );
3644
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003645 /* Serialized version of record header for MAC purposes */
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003646 memcpy( add_data, rec.ctr, 8 );
3647 add_data[8] = rec.type;
3648 add_data[9] = rec.ver[0];
3649 add_data[10] = rec.ver[1];
3650 add_data[11] = ( rec.data_len >> 8 ) & 0xff;
3651 add_data[12] = ( rec.data_len >> 0 ) & 0xff;
3652
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003653 /* Set dummy IV */
3654 memset( t0.iv_enc, 0x55, t0.ivlen );
3655 memcpy( rec.buf, t0.iv_enc, t0.ivlen );
3656
3657 /*
3658 * Prepare a pre-encryption record (with MAC and padding), and save it.
3659 */
3660
3661 /* MAC with additional data */
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003662 TEST_EQUAL( 0, mbedtls_md_hmac_update( &t0.md_ctx_enc, add_data, 13 ) );
3663 TEST_EQUAL( 0, mbedtls_md_hmac_update( &t0.md_ctx_enc,
3664 rec.buf + rec.data_offset,
3665 rec.data_len ) );
3666 TEST_EQUAL( 0, mbedtls_md_hmac_finish( &t0.md_ctx_enc, mac ) );
3667
3668 memcpy( rec.buf + rec.data_offset + rec.data_len, mac, t0.maclen );
3669 rec.data_len += t0.maclen;
3670
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003671 /* Pad */
3672 memset( rec.buf + rec.data_offset + rec.data_len, padlen, padlen + 1 );
3673 rec.data_len += padlen + 1;
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003674
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003675 /* Save correct pre-encryption record */
3676 rec_save = rec;
3677 rec_save.buf = buf_save;
3678 memcpy( buf_save, buf, buflen );
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003679
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003680 /*
3681 * Encrypt and decrypt the correct record, expecting success
3682 */
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003683 TEST_EQUAL( 0, mbedtls_cipher_crypt( &t0.cipher_ctx_enc,
3684 t0.iv_enc, t0.ivlen,
3685 rec.buf + rec.data_offset, rec.data_len,
3686 rec.buf + rec.data_offset, &olen ) );
3687 rec.data_offset -= t0.ivlen;
3688 rec.data_len += t0.ivlen;
3689
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003690 TEST_EQUAL( 0, mbedtls_ssl_decrypt_buf( &ssl, &t1, &rec ) );
3691
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003692 /*
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003693 * Modify each byte of the pre-encryption record before encrypting and
3694 * decrypting it, expecting failure every time.
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003695 */
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003696 for( i = block_size; i < buflen; i++ )
3697 {
Chris Jones9634bb12021-01-20 15:56:42 +00003698 mbedtls_test_set_step( i );
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003699
3700 /* Restore correct pre-encryption record */
3701 rec = rec_save;
3702 rec.buf = buf;
3703 memcpy( buf, buf_save, buflen );
3704
Manuel Pégourié-Gonnardb51f0442020-07-21 10:40:25 +02003705 /* Corrupt one byte of the data (could be plaintext, MAC or padding) */
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003706 rec.buf[i] ^= 0x01;
3707
3708 /* Encrypt */
3709 TEST_EQUAL( 0, mbedtls_cipher_crypt( &t0.cipher_ctx_enc,
3710 t0.iv_enc, t0.ivlen,
3711 rec.buf + rec.data_offset, rec.data_len,
3712 rec.buf + rec.data_offset, &olen ) );
3713 rec.data_offset -= t0.ivlen;
3714 rec.data_len += t0.ivlen;
3715
3716 /* Decrypt and expect failure */
3717 TEST_EQUAL( MBEDTLS_ERR_SSL_INVALID_MAC,
3718 mbedtls_ssl_decrypt_buf( &ssl, &t1, &rec ) );
3719 }
3720
3721 /*
3722 * Use larger values of the padding bytes - with small buffers, this tests
3723 * the case where the announced padlen would be larger than the buffer
3724 * (and before that, than the buffer minus the size of the MAC), to make
3725 * sure our padding checking code does not perform any out-of-bounds reads
3726 * in this case. (With larger buffers, ie when the plaintext is long or
3727 * maximal length padding is used, this is less relevant but still doesn't
3728 * hurt to test.)
3729 *
3730 * (Start the loop with correct padding, just to double-check that record
3731 * saving did work, and that we're overwriting the correct bytes.)
3732 */
Manuel Pégourié-Gonnard4adc04a2020-07-16 10:00:48 +02003733 for( i = padlen; i <= pad_max_len; i++ )
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003734 {
Chris Jones9634bb12021-01-20 15:56:42 +00003735 mbedtls_test_set_step( i );
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003736
3737 /* Restore correct pre-encryption record */
3738 rec = rec_save;
3739 rec.buf = buf;
3740 memcpy( buf, buf_save, buflen );
3741
3742 /* Set padding bytes to new value */
3743 memset( buf + buflen - padlen - 1, i, padlen + 1 );
3744
3745 /* Encrypt */
3746 TEST_EQUAL( 0, mbedtls_cipher_crypt( &t0.cipher_ctx_enc,
3747 t0.iv_enc, t0.ivlen,
3748 rec.buf + rec.data_offset, rec.data_len,
3749 rec.buf + rec.data_offset, &olen ) );
3750 rec.data_offset -= t0.ivlen;
3751 rec.data_len += t0.ivlen;
3752
3753 /* Decrypt and expect failure except the first time */
3754 exp_ret = ( i == padlen ) ? 0 : MBEDTLS_ERR_SSL_INVALID_MAC;
3755 TEST_EQUAL( exp_ret, mbedtls_ssl_decrypt_buf( &ssl, &t1, &rec ) );
3756 }
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003757
3758exit:
3759 mbedtls_ssl_free( &ssl );
3760 mbedtls_ssl_transform_free( &t0 );
3761 mbedtls_ssl_transform_free( &t1 );
3762 mbedtls_free( buf );
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003763 mbedtls_free( buf_save );
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01003764 USE_PSA_DONE( );
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003765}
3766/* END_CASE */
3767
Ronald Cron6f135e12021-12-08 16:57:54 +01003768/* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00003769void ssl_tls13_hkdf_expand_label( int hash_alg,
3770 data_t *secret,
3771 int label_idx,
3772 data_t *ctx,
3773 int desired_length,
3774 data_t *expected )
Hanno Becker39ff4922020-08-21 13:36:56 +01003775{
3776 unsigned char dst[ 100 ];
3777
Hanno Becker70d7fb02020-09-09 10:11:21 +01003778 unsigned char const *lbl = NULL;
3779 size_t lbl_len;
Xiaofei Baid25fab62021-12-02 06:36:27 +00003780#define MBEDTLS_SSL_TLS1_3_LABEL( name, string ) \
3781 if( label_idx == (int) tls13_label_ ## name ) \
3782 { \
Xiaofei Bai746f9482021-11-12 08:53:56 +00003783 lbl = mbedtls_ssl_tls13_labels.name; \
3784 lbl_len = sizeof( mbedtls_ssl_tls13_labels.name ); \
Hanno Becker70d7fb02020-09-09 10:11:21 +01003785 }
3786MBEDTLS_SSL_TLS1_3_LABEL_LIST
3787#undef MBEDTLS_SSL_TLS1_3_LABEL
3788 TEST_ASSERT( lbl != NULL );
Hanno Becker39ff4922020-08-21 13:36:56 +01003789
3790 /* Check sanity of test parameters. */
3791 TEST_ASSERT( (size_t) desired_length <= sizeof(dst) );
3792 TEST_ASSERT( (size_t) desired_length == expected->len );
3793
Xiaofei Bai746f9482021-11-12 08:53:56 +00003794 TEST_ASSERT( mbedtls_ssl_tls13_hkdf_expand_label(
Hanno Becker39ff4922020-08-21 13:36:56 +01003795 (mbedtls_md_type_t) hash_alg,
3796 secret->x, secret->len,
Hanno Becker70d7fb02020-09-09 10:11:21 +01003797 lbl, lbl_len,
Hanno Becker39ff4922020-08-21 13:36:56 +01003798 ctx->x, ctx->len,
3799 dst, desired_length ) == 0 );
3800
Hanno Beckerfb080962020-09-08 10:58:42 +01003801 ASSERT_COMPARE( dst, (size_t) desired_length,
3802 expected->x, (size_t) expected->len );
Hanno Becker39ff4922020-08-21 13:36:56 +01003803}
3804/* END_CASE */
3805
Ronald Cron6f135e12021-12-08 16:57:54 +01003806/* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00003807void ssl_tls13_traffic_key_generation( int hash_alg,
3808 data_t *server_secret,
3809 data_t *client_secret,
3810 int desired_iv_len,
3811 int desired_key_len,
3812 data_t *expected_server_write_key,
3813 data_t *expected_server_write_iv,
3814 data_t *expected_client_write_key,
3815 data_t *expected_client_write_iv )
Hanno Becker19498f82020-08-21 13:37:08 +01003816{
3817 mbedtls_ssl_key_set keys;
3818
3819 /* Check sanity of test parameters. */
3820 TEST_ASSERT( client_secret->len == server_secret->len );
3821 TEST_ASSERT( expected_client_write_iv->len == expected_server_write_iv->len &&
3822 expected_client_write_iv->len == (size_t) desired_iv_len );
3823 TEST_ASSERT( expected_client_write_key->len == expected_server_write_key->len &&
3824 expected_client_write_key->len == (size_t) desired_key_len );
3825
Xiaofei Bai746f9482021-11-12 08:53:56 +00003826 TEST_ASSERT( mbedtls_ssl_tls13_make_traffic_keys(
Hanno Becker19498f82020-08-21 13:37:08 +01003827 (mbedtls_md_type_t) hash_alg,
3828 client_secret->x,
3829 server_secret->x,
3830 client_secret->len /* == server_secret->len */,
3831 desired_key_len, desired_iv_len,
3832 &keys ) == 0 );
3833
Hanno Beckerfb080962020-09-08 10:58:42 +01003834 ASSERT_COMPARE( keys.client_write_key,
Hanno Becker493ea7f2020-09-08 11:01:00 +01003835 keys.key_len,
Hanno Beckerfb080962020-09-08 10:58:42 +01003836 expected_client_write_key->x,
3837 (size_t) desired_key_len );
3838 ASSERT_COMPARE( keys.server_write_key,
Hanno Becker493ea7f2020-09-08 11:01:00 +01003839 keys.key_len,
Hanno Beckerfb080962020-09-08 10:58:42 +01003840 expected_server_write_key->x,
3841 (size_t) desired_key_len );
3842 ASSERT_COMPARE( keys.client_write_iv,
Hanno Becker493ea7f2020-09-08 11:01:00 +01003843 keys.iv_len,
Hanno Beckerfb080962020-09-08 10:58:42 +01003844 expected_client_write_iv->x,
3845 (size_t) desired_iv_len );
3846 ASSERT_COMPARE( keys.server_write_iv,
Hanno Becker493ea7f2020-09-08 11:01:00 +01003847 keys.iv_len,
Hanno Beckerfb080962020-09-08 10:58:42 +01003848 expected_server_write_iv->x,
3849 (size_t) desired_iv_len );
Hanno Becker19498f82020-08-21 13:37:08 +01003850}
3851/* END_CASE */
3852
Ronald Cron6f135e12021-12-08 16:57:54 +01003853/* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00003854void ssl_tls13_derive_secret( int hash_alg,
3855 data_t *secret,
3856 int label_idx,
3857 data_t *ctx,
3858 int desired_length,
3859 int already_hashed,
3860 data_t *expected )
Hanno Beckere4849d12020-08-21 14:14:14 +01003861{
3862 unsigned char dst[ 100 ];
3863
Hanno Becker70d7fb02020-09-09 10:11:21 +01003864 unsigned char const *lbl = NULL;
3865 size_t lbl_len;
Xiaofei Baid25fab62021-12-02 06:36:27 +00003866#define MBEDTLS_SSL_TLS1_3_LABEL( name, string ) \
3867 if( label_idx == (int) tls13_label_ ## name ) \
3868 { \
Xiaofei Bai746f9482021-11-12 08:53:56 +00003869 lbl = mbedtls_ssl_tls13_labels.name; \
3870 lbl_len = sizeof( mbedtls_ssl_tls13_labels.name ); \
Hanno Becker70d7fb02020-09-09 10:11:21 +01003871 }
3872MBEDTLS_SSL_TLS1_3_LABEL_LIST
3873#undef MBEDTLS_SSL_TLS1_3_LABEL
3874 TEST_ASSERT( lbl != NULL );
3875
Hanno Beckere4849d12020-08-21 14:14:14 +01003876 /* Check sanity of test parameters. */
3877 TEST_ASSERT( (size_t) desired_length <= sizeof(dst) );
3878 TEST_ASSERT( (size_t) desired_length == expected->len );
3879
Xiaofei Bai746f9482021-11-12 08:53:56 +00003880 TEST_ASSERT( mbedtls_ssl_tls13_derive_secret(
Hanno Beckere4849d12020-08-21 14:14:14 +01003881 (mbedtls_md_type_t) hash_alg,
3882 secret->x, secret->len,
Hanno Becker70d7fb02020-09-09 10:11:21 +01003883 lbl, lbl_len,
Hanno Beckere4849d12020-08-21 14:14:14 +01003884 ctx->x, ctx->len,
3885 already_hashed,
3886 dst, desired_length ) == 0 );
3887
Hanno Beckerfb080962020-09-08 10:58:42 +01003888 ASSERT_COMPARE( dst, desired_length,
3889 expected->x, desired_length );
Hanno Beckere4849d12020-08-21 14:14:14 +01003890}
3891/* END_CASE */
3892
Ronald Cron6f135e12021-12-08 16:57:54 +01003893/* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00003894void ssl_tls13_derive_early_secrets( int hash_alg,
3895 data_t *secret,
3896 data_t *transcript,
3897 data_t *traffic_expected,
3898 data_t *exporter_expected )
Hanno Beckera4f40a02021-05-24 06:42:11 +01003899{
Xiaofei Bai746f9482021-11-12 08:53:56 +00003900 mbedtls_ssl_tls13_early_secrets secrets;
Hanno Beckera4f40a02021-05-24 06:42:11 +01003901
3902 /* Double-check that we've passed sane parameters. */
3903 mbedtls_md_type_t md_type = (mbedtls_md_type_t) hash_alg;
3904 mbedtls_md_info_t const * const md_info = mbedtls_md_info_from_type( md_type );
3905 size_t const md_size = mbedtls_md_get_size( md_info );
3906 TEST_ASSERT( md_info != 0 &&
3907 secret->len == md_size &&
3908 transcript->len == md_size &&
3909 traffic_expected->len == md_size &&
3910 exporter_expected->len == md_size );
3911
Xiaofei Bai746f9482021-11-12 08:53:56 +00003912 TEST_ASSERT( mbedtls_ssl_tls13_derive_early_secrets(
Hanno Beckera4f40a02021-05-24 06:42:11 +01003913 md_type, secret->x, transcript->x, transcript->len,
3914 &secrets ) == 0 );
3915
3916 ASSERT_COMPARE( secrets.client_early_traffic_secret, md_size,
3917 traffic_expected->x, traffic_expected->len );
3918 ASSERT_COMPARE( secrets.early_exporter_master_secret, md_size,
3919 exporter_expected->x, exporter_expected->len );
3920}
3921/* END_CASE */
3922
Ronald Cron6f135e12021-12-08 16:57:54 +01003923/* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00003924void ssl_tls13_derive_handshake_secrets( int hash_alg,
3925 data_t *secret,
3926 data_t *transcript,
3927 data_t *client_expected,
3928 data_t *server_expected )
Hanno Beckera4f40a02021-05-24 06:42:11 +01003929{
Xiaofei Bai746f9482021-11-12 08:53:56 +00003930 mbedtls_ssl_tls13_handshake_secrets secrets;
Hanno Beckera4f40a02021-05-24 06:42:11 +01003931
3932 /* Double-check that we've passed sane parameters. */
3933 mbedtls_md_type_t md_type = (mbedtls_md_type_t) hash_alg;
3934 mbedtls_md_info_t const * const md_info = mbedtls_md_info_from_type( md_type );
3935 size_t const md_size = mbedtls_md_get_size( md_info );
3936 TEST_ASSERT( md_info != 0 &&
3937 secret->len == md_size &&
3938 transcript->len == md_size &&
3939 client_expected->len == md_size &&
3940 server_expected->len == md_size );
3941
Xiaofei Bai746f9482021-11-12 08:53:56 +00003942 TEST_ASSERT( mbedtls_ssl_tls13_derive_handshake_secrets(
Hanno Beckera4f40a02021-05-24 06:42:11 +01003943 md_type, secret->x, transcript->x, transcript->len,
3944 &secrets ) == 0 );
3945
3946 ASSERT_COMPARE( secrets.client_handshake_traffic_secret, md_size,
3947 client_expected->x, client_expected->len );
3948 ASSERT_COMPARE( secrets.server_handshake_traffic_secret, md_size,
3949 server_expected->x, server_expected->len );
3950}
3951/* END_CASE */
3952
Ronald Cron6f135e12021-12-08 16:57:54 +01003953/* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00003954void ssl_tls13_derive_application_secrets( int hash_alg,
3955 data_t *secret,
3956 data_t *transcript,
3957 data_t *client_expected,
3958 data_t *server_expected,
3959 data_t *exporter_expected )
Hanno Beckera4f40a02021-05-24 06:42:11 +01003960{
Xiaofei Bai746f9482021-11-12 08:53:56 +00003961 mbedtls_ssl_tls13_application_secrets secrets;
Hanno Beckera4f40a02021-05-24 06:42:11 +01003962
3963 /* Double-check that we've passed sane parameters. */
3964 mbedtls_md_type_t md_type = (mbedtls_md_type_t) hash_alg;
3965 mbedtls_md_info_t const * const md_info = mbedtls_md_info_from_type( md_type );
3966 size_t const md_size = mbedtls_md_get_size( md_info );
3967 TEST_ASSERT( md_info != 0 &&
3968 secret->len == md_size &&
3969 transcript->len == md_size &&
3970 client_expected->len == md_size &&
3971 server_expected->len == md_size &&
3972 exporter_expected->len == md_size );
3973
Xiaofei Bai746f9482021-11-12 08:53:56 +00003974 TEST_ASSERT( mbedtls_ssl_tls13_derive_application_secrets(
Hanno Beckera4f40a02021-05-24 06:42:11 +01003975 md_type, secret->x, transcript->x, transcript->len,
3976 &secrets ) == 0 );
3977
3978 ASSERT_COMPARE( secrets.client_application_traffic_secret_N, md_size,
3979 client_expected->x, client_expected->len );
3980 ASSERT_COMPARE( secrets.server_application_traffic_secret_N, md_size,
3981 server_expected->x, server_expected->len );
3982 ASSERT_COMPARE( secrets.exporter_master_secret, md_size,
3983 exporter_expected->x, exporter_expected->len );
3984}
3985/* END_CASE */
3986
Ronald Cron6f135e12021-12-08 16:57:54 +01003987/* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00003988void ssl_tls13_derive_resumption_secrets( int hash_alg,
3989 data_t *secret,
3990 data_t *transcript,
3991 data_t *resumption_expected )
Hanno Beckera4f40a02021-05-24 06:42:11 +01003992{
Xiaofei Bai746f9482021-11-12 08:53:56 +00003993 mbedtls_ssl_tls13_application_secrets secrets;
Hanno Beckera4f40a02021-05-24 06:42:11 +01003994
3995 /* Double-check that we've passed sane parameters. */
3996 mbedtls_md_type_t md_type = (mbedtls_md_type_t) hash_alg;
3997 mbedtls_md_info_t const * const md_info = mbedtls_md_info_from_type( md_type );
3998 size_t const md_size = mbedtls_md_get_size( md_info );
3999 TEST_ASSERT( md_info != 0 &&
4000 secret->len == md_size &&
4001 transcript->len == md_size &&
4002 resumption_expected->len == md_size );
4003
Xiaofei Bai746f9482021-11-12 08:53:56 +00004004 TEST_ASSERT( mbedtls_ssl_tls13_derive_resumption_master_secret(
Hanno Beckera4f40a02021-05-24 06:42:11 +01004005 md_type, secret->x, transcript->x, transcript->len,
4006 &secrets ) == 0 );
4007
4008 ASSERT_COMPARE( secrets.resumption_master_secret, md_size,
4009 resumption_expected->x, resumption_expected->len );
4010}
4011/* END_CASE */
4012
Ronald Cron6f135e12021-12-08 16:57:54 +01004013/* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00004014void ssl_tls13_create_psk_binder( int hash_alg,
4015 data_t *psk,
4016 int psk_type,
4017 data_t *transcript,
4018 data_t *binder_expected )
Hanno Becker55bc2c52021-05-24 06:53:52 +01004019{
4020 unsigned char binder[ MBEDTLS_MD_MAX_SIZE ];
4021
4022 /* Double-check that we've passed sane parameters. */
4023 mbedtls_md_type_t md_type = (mbedtls_md_type_t) hash_alg;
4024 mbedtls_md_info_t const * const md_info = mbedtls_md_info_from_type( md_type );
4025 size_t const md_size = mbedtls_md_get_size( md_info );
4026 TEST_ASSERT( md_info != 0 &&
4027 transcript->len == md_size &&
4028 binder_expected->len == md_size );
4029
Xiaofei Bai746f9482021-11-12 08:53:56 +00004030 TEST_ASSERT( mbedtls_ssl_tls13_create_psk_binder(
Hanno Becker55bc2c52021-05-24 06:53:52 +01004031 NULL, /* SSL context for debugging only */
4032 md_type,
4033 psk->x, psk->len,
4034 psk_type,
4035 transcript->x,
4036 binder ) == 0 );
4037
4038 ASSERT_COMPARE( binder, md_size,
4039 binder_expected->x, binder_expected->len );
4040}
4041/* END_CASE */
4042
Ronald Cron6f135e12021-12-08 16:57:54 +01004043/* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00004044void ssl_tls13_record_protection( int ciphersuite,
4045 int endpoint,
4046 int ctr,
4047 int padding_used,
4048 data_t *server_write_key,
4049 data_t *server_write_iv,
4050 data_t *client_write_key,
4051 data_t *client_write_iv,
4052 data_t *plaintext,
4053 data_t *ciphertext )
Hanno Beckera77d0052021-03-22 15:16:33 +00004054{
4055 mbedtls_ssl_key_set keys;
4056 mbedtls_ssl_transform transform_send;
4057 mbedtls_ssl_transform transform_recv;
4058 mbedtls_record rec;
4059 unsigned char *buf = NULL;
Hanno Becker1f918782021-08-01 19:18:28 +01004060 size_t buf_len;
Hanno Beckera77d0052021-03-22 15:16:33 +00004061 int other_endpoint;
4062
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01004063 USE_PSA_INIT( );
4064
Hanno Beckera77d0052021-03-22 15:16:33 +00004065 TEST_ASSERT( endpoint == MBEDTLS_SSL_IS_CLIENT ||
4066 endpoint == MBEDTLS_SSL_IS_SERVER );
4067
4068 if( endpoint == MBEDTLS_SSL_IS_SERVER )
4069 other_endpoint = MBEDTLS_SSL_IS_CLIENT;
4070 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
4071 other_endpoint = MBEDTLS_SSL_IS_SERVER;
4072
4073 TEST_ASSERT( server_write_key->len == client_write_key->len );
4074 TEST_ASSERT( server_write_iv->len == client_write_iv->len );
4075
4076 memcpy( keys.client_write_key,
4077 client_write_key->x, client_write_key->len );
4078 memcpy( keys.client_write_iv,
4079 client_write_iv->x, client_write_iv->len );
4080 memcpy( keys.server_write_key,
4081 server_write_key->x, server_write_key->len );
4082 memcpy( keys.server_write_iv,
4083 server_write_iv->x, server_write_iv->len );
4084
4085 keys.key_len = server_write_key->len;
4086 keys.iv_len = server_write_iv->len;
4087
4088 mbedtls_ssl_transform_init( &transform_recv );
4089 mbedtls_ssl_transform_init( &transform_send );
4090
4091 TEST_ASSERT( mbedtls_ssl_tls13_populate_transform(
4092 &transform_send, endpoint,
4093 ciphersuite, &keys, NULL ) == 0 );
4094 TEST_ASSERT( mbedtls_ssl_tls13_populate_transform(
4095 &transform_recv, other_endpoint,
4096 ciphersuite, &keys, NULL ) == 0 );
4097
Hanno Becker1f918782021-08-01 19:18:28 +01004098 /* Make sure we have enough space in the buffer even if
4099 * we use more padding than the KAT. */
4100 buf_len = ciphertext->len + MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY;
4101 ASSERT_ALLOC( buf, buf_len );
Hanno Beckera77d0052021-03-22 15:16:33 +00004102 rec.type = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Hanno Becker41537452021-04-20 05:35:28 +01004103
4104 /* TLS 1.3 uses the version identifier from TLS 1.2 on the wire. */
Hanno Beckera77d0052021-03-22 15:16:33 +00004105 mbedtls_ssl_write_version( MBEDTLS_SSL_MAJOR_VERSION_3,
4106 MBEDTLS_SSL_MINOR_VERSION_3,
4107 MBEDTLS_SSL_TRANSPORT_STREAM,
4108 rec.ver );
4109
4110 /* Copy plaintext into record structure */
4111 rec.buf = buf;
Hanno Becker1f918782021-08-01 19:18:28 +01004112 rec.buf_len = buf_len;
Hanno Beckera77d0052021-03-22 15:16:33 +00004113 rec.data_offset = 0;
4114 TEST_ASSERT( plaintext->len <= ciphertext->len );
4115 memcpy( rec.buf + rec.data_offset, plaintext->x, plaintext->len );
4116 rec.data_len = plaintext->len;
4117#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4118 rec.cid_len = 0;
4119#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4120
4121 memset( &rec.ctr[0], 0, 8 );
4122 rec.ctr[7] = ctr;
4123
4124 TEST_ASSERT( mbedtls_ssl_encrypt_buf( NULL, &transform_send, &rec,
4125 NULL, NULL ) == 0 );
Hanno Becker1f918782021-08-01 19:18:28 +01004126
4127 if( padding_used == MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY )
4128 {
4129 ASSERT_COMPARE( rec.buf + rec.data_offset, rec.data_len,
4130 ciphertext->x, ciphertext->len );
4131 }
Hanno Beckera77d0052021-03-22 15:16:33 +00004132
4133 TEST_ASSERT( mbedtls_ssl_decrypt_buf( NULL, &transform_recv, &rec ) == 0 );
4134 ASSERT_COMPARE( rec.buf + rec.data_offset, rec.data_len,
4135 plaintext->x, plaintext->len );
4136
Hanno Becker80e760e2021-03-23 06:00:21 +00004137 mbedtls_free( buf );
Hanno Beckera77d0052021-03-22 15:16:33 +00004138 mbedtls_ssl_transform_free( &transform_send );
4139 mbedtls_ssl_transform_free( &transform_recv );
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01004140 USE_PSA_DONE( );
Hanno Beckera77d0052021-03-22 15:16:33 +00004141}
4142/* END_CASE */
4143
Ronald Cron6f135e12021-12-08 16:57:54 +01004144/* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00004145void ssl_tls13_key_evolution( int hash_alg,
4146 data_t *secret,
4147 data_t *input,
4148 data_t *expected )
Hanno Becker2d2c3eb2020-08-20 14:54:24 +01004149{
4150 unsigned char secret_new[ MBEDTLS_MD_MAX_SIZE ];
4151
Xiaofei Bai746f9482021-11-12 08:53:56 +00004152 TEST_ASSERT( mbedtls_ssl_tls13_evolve_secret(
Hanno Becker2d2c3eb2020-08-20 14:54:24 +01004153 (mbedtls_md_type_t) hash_alg,
4154 secret->len ? secret->x : NULL,
4155 input->len ? input->x : NULL, input->len,
4156 secret_new ) == 0 );
4157
Hanno Beckerfb080962020-09-08 10:58:42 +01004158 ASSERT_COMPARE( secret_new, (size_t) expected->len,
4159 expected->x, (size_t) expected->len );
Hanno Becker2d2c3eb2020-08-20 14:54:24 +01004160}
4161/* END_CASE */
4162
Ron Eldor824ad7b2019-05-13 14:09:00 +03004163/* BEGIN_CASE */
4164void ssl_tls_prf( int type, data_t * secret, data_t * random,
Ronald Cronac6ae352020-06-26 14:33:03 +02004165 char *label, data_t *result_str, int exp_ret )
Ron Eldor824ad7b2019-05-13 14:09:00 +03004166{
4167 unsigned char *output;
4168
Ronald Cronac6ae352020-06-26 14:33:03 +02004169 output = mbedtls_calloc( 1, result_str->len );
Ron Eldor824ad7b2019-05-13 14:09:00 +03004170 if( output == NULL )
4171 goto exit;
4172
Gilles Peskine9de97e22021-02-02 21:00:11 +01004173 USE_PSA_INIT( );
Ron Eldor6b9b1b82019-05-15 17:04:33 +03004174
Ron Eldor824ad7b2019-05-13 14:09:00 +03004175 TEST_ASSERT( mbedtls_ssl_tls_prf( type, secret->x, secret->len,
4176 label, random->x, random->len,
Ronald Cronac6ae352020-06-26 14:33:03 +02004177 output, result_str->len ) == exp_ret );
Ron Eldor824ad7b2019-05-13 14:09:00 +03004178
4179 if( exp_ret == 0 )
4180 {
Ronald Cronac6ae352020-06-26 14:33:03 +02004181 TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
4182 result_str->len, result_str->len ) == 0 );
Ron Eldor824ad7b2019-05-13 14:09:00 +03004183 }
4184exit:
4185
4186 mbedtls_free( output );
Gilles Peskine1f186ff2021-02-02 21:04:06 +01004187 USE_PSA_DONE( );
Ron Eldor824ad7b2019-05-13 14:09:00 +03004188}
4189/* END_CASE */
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02004190
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02004191/* BEGIN_CASE */
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02004192void ssl_serialize_session_save_load( int ticket_len, char *crt_file )
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02004193{
4194 mbedtls_ssl_session original, restored;
4195 unsigned char *buf = NULL;
4196 size_t len;
4197
4198 /*
4199 * Test that a save-load pair is the identity
4200 */
4201
4202 mbedtls_ssl_session_init( &original );
4203 mbedtls_ssl_session_init( &restored );
4204
4205 /* Prepare a dummy session to work on */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004206 TEST_ASSERT( ssl_populate_session_tls12( &original, ticket_len, crt_file ) == 0 );
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02004207
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02004208 /* Serialize it */
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02004209 TEST_ASSERT( mbedtls_ssl_session_save( &original, NULL, 0, &len )
4210 == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4211 TEST_ASSERT( ( buf = mbedtls_calloc( 1, len ) ) != NULL );
4212 TEST_ASSERT( mbedtls_ssl_session_save( &original, buf, len, &len )
4213 == 0 );
4214
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02004215 /* Restore session from serialized data */
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02004216 TEST_ASSERT( mbedtls_ssl_session_load( &restored, buf, len) == 0 );
4217
4218 /*
4219 * Make sure both session structures are identical
4220 */
4221#if defined(MBEDTLS_HAVE_TIME)
4222 TEST_ASSERT( original.start == restored.start );
4223#endif
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004224 TEST_ASSERT( original.minor_ver == restored.minor_ver );
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02004225 TEST_ASSERT( original.ciphersuite == restored.ciphersuite );
4226 TEST_ASSERT( original.compression == restored.compression );
4227 TEST_ASSERT( original.id_len == restored.id_len );
4228 TEST_ASSERT( memcmp( original.id,
4229 restored.id, sizeof( original.id ) ) == 0 );
4230 TEST_ASSERT( memcmp( original.master,
4231 restored.master, sizeof( original.master ) ) == 0 );
4232
4233#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02004234#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02004235 TEST_ASSERT( ( original.peer_cert == NULL ) ==
4236 ( restored.peer_cert == NULL ) );
4237 if( original.peer_cert != NULL )
4238 {
4239 TEST_ASSERT( original.peer_cert->raw.len ==
4240 restored.peer_cert->raw.len );
4241 TEST_ASSERT( memcmp( original.peer_cert->raw.p,
4242 restored.peer_cert->raw.p,
4243 original.peer_cert->raw.len ) == 0 );
4244 }
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02004245#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4246 TEST_ASSERT( original.peer_cert_digest_type ==
4247 restored.peer_cert_digest_type );
4248 TEST_ASSERT( original.peer_cert_digest_len ==
4249 restored.peer_cert_digest_len );
4250 TEST_ASSERT( ( original.peer_cert_digest == NULL ) ==
4251 ( restored.peer_cert_digest == NULL ) );
4252 if( original.peer_cert_digest != NULL )
4253 {
4254 TEST_ASSERT( memcmp( original.peer_cert_digest,
4255 restored.peer_cert_digest,
4256 original.peer_cert_digest_len ) == 0 );
4257 }
4258#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4259#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02004260 TEST_ASSERT( original.verify_result == restored.verify_result );
4261
4262#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
4263 TEST_ASSERT( original.ticket_len == restored.ticket_len );
4264 if( original.ticket_len != 0 )
4265 {
4266 TEST_ASSERT( original.ticket != NULL );
4267 TEST_ASSERT( restored.ticket != NULL );
4268 TEST_ASSERT( memcmp( original.ticket,
4269 restored.ticket, original.ticket_len ) == 0 );
4270 }
4271 TEST_ASSERT( original.ticket_lifetime == restored.ticket_lifetime );
4272#endif
4273
4274#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
4275 TEST_ASSERT( original.mfl_code == restored.mfl_code );
4276#endif
4277
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02004278#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
4279 TEST_ASSERT( original.encrypt_then_mac == restored.encrypt_then_mac );
4280#endif
4281
4282exit:
4283 mbedtls_ssl_session_free( &original );
4284 mbedtls_ssl_session_free( &restored );
4285 mbedtls_free( buf );
4286}
4287/* END_CASE */
4288
Manuel Pégourié-Gonnardaa755832019-06-03 10:53:47 +02004289/* BEGIN_CASE */
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02004290void ssl_serialize_session_load_save( int ticket_len, char *crt_file )
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02004291{
4292 mbedtls_ssl_session session;
4293 unsigned char *buf1 = NULL, *buf2 = NULL;
4294 size_t len0, len1, len2;
4295
4296 /*
4297 * Test that a load-save pair is the identity
4298 */
4299
4300 mbedtls_ssl_session_init( &session );
4301
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02004302 /* Prepare a dummy session to work on */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004303 TEST_ASSERT( ssl_populate_session_tls12( &session, ticket_len, crt_file ) == 0 );
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02004304
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02004305 /* Get desired buffer size for serializing */
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02004306 TEST_ASSERT( mbedtls_ssl_session_save( &session, NULL, 0, &len0 )
4307 == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4308
4309 /* Allocate first buffer */
4310 buf1 = mbedtls_calloc( 1, len0 );
4311 TEST_ASSERT( buf1 != NULL );
4312
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02004313 /* Serialize to buffer and free live session */
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02004314 TEST_ASSERT( mbedtls_ssl_session_save( &session, buf1, len0, &len1 )
4315 == 0 );
4316 TEST_ASSERT( len0 == len1 );
4317 mbedtls_ssl_session_free( &session );
4318
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02004319 /* Restore session from serialized data */
Manuel Pégourié-Gonnard220403b2019-05-24 09:54:21 +02004320 TEST_ASSERT( mbedtls_ssl_session_load( &session, buf1, len1 ) == 0 );
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02004321
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02004322 /* Allocate second buffer and serialize to it */
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02004323 buf2 = mbedtls_calloc( 1, len0 );
Manuel Pégourié-Gonnardb4079902019-05-24 09:52:10 +02004324 TEST_ASSERT( buf2 != NULL );
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02004325 TEST_ASSERT( mbedtls_ssl_session_save( &session, buf2, len0, &len2 )
4326 == 0 );
4327
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02004328 /* Make sure both serialized versions are identical */
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02004329 TEST_ASSERT( len1 == len2 );
4330 TEST_ASSERT( memcmp( buf1, buf2, len1 ) == 0 );
4331
4332exit:
4333 mbedtls_ssl_session_free( &session );
4334 mbedtls_free( buf1 );
4335 mbedtls_free( buf2 );
4336}
4337/* END_CASE */
Manuel Pégourié-Gonnardf5fa0aa2019-05-23 10:38:11 +02004338
4339/* BEGIN_CASE */
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02004340void ssl_serialize_session_save_buf_size( int ticket_len, char *crt_file )
Manuel Pégourié-Gonnardf5fa0aa2019-05-23 10:38:11 +02004341{
4342 mbedtls_ssl_session session;
4343 unsigned char *buf = NULL;
4344 size_t good_len, bad_len, test_len;
4345
4346 /*
4347 * Test that session_save() fails cleanly on small buffers
4348 */
4349
4350 mbedtls_ssl_session_init( &session );
4351
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02004352 /* Prepare dummy session and get serialized size */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004353 TEST_ASSERT( ssl_populate_session_tls12( &session, ticket_len, crt_file ) == 0 );
Manuel Pégourié-Gonnardf5fa0aa2019-05-23 10:38:11 +02004354 TEST_ASSERT( mbedtls_ssl_session_save( &session, NULL, 0, &good_len )
4355 == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4356
4357 /* Try all possible bad lengths */
4358 for( bad_len = 1; bad_len < good_len; bad_len++ )
4359 {
4360 /* Allocate exact size so that asan/valgrind can detect any overwrite */
4361 mbedtls_free( buf );
4362 TEST_ASSERT( ( buf = mbedtls_calloc( 1, bad_len ) ) != NULL );
4363 TEST_ASSERT( mbedtls_ssl_session_save( &session, buf, bad_len,
4364 &test_len )
4365 == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4366 TEST_ASSERT( test_len == good_len );
4367 }
4368
4369exit:
4370 mbedtls_ssl_session_free( &session );
4371 mbedtls_free( buf );
4372}
4373/* END_CASE */
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02004374
4375/* BEGIN_CASE */
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02004376void ssl_serialize_session_load_buf_size( int ticket_len, char *crt_file )
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02004377{
4378 mbedtls_ssl_session session;
4379 unsigned char *good_buf = NULL, *bad_buf = NULL;
4380 size_t good_len, bad_len;
4381
4382 /*
4383 * Test that session_load() fails cleanly on small buffers
4384 */
4385
4386 mbedtls_ssl_session_init( &session );
4387
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02004388 /* Prepare serialized session data */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01004389 TEST_ASSERT( ssl_populate_session_tls12( &session, ticket_len, crt_file ) == 0 );
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02004390 TEST_ASSERT( mbedtls_ssl_session_save( &session, NULL, 0, &good_len )
4391 == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4392 TEST_ASSERT( ( good_buf = mbedtls_calloc( 1, good_len ) ) != NULL );
4393 TEST_ASSERT( mbedtls_ssl_session_save( &session, good_buf, good_len,
4394 &good_len ) == 0 );
4395 mbedtls_ssl_session_free( &session );
4396
4397 /* Try all possible bad lengths */
4398 for( bad_len = 0; bad_len < good_len; bad_len++ )
4399 {
4400 /* Allocate exact size so that asan/valgrind can detect any overread */
4401 mbedtls_free( bad_buf );
4402 bad_buf = mbedtls_calloc( 1, bad_len ? bad_len : 1 );
4403 TEST_ASSERT( bad_buf != NULL );
4404 memcpy( bad_buf, good_buf, bad_len );
4405
4406 TEST_ASSERT( mbedtls_ssl_session_load( &session, bad_buf, bad_len )
4407 == MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4408 }
4409
4410exit:
4411 mbedtls_ssl_session_free( &session );
4412 mbedtls_free( good_buf );
4413 mbedtls_free( bad_buf );
4414}
4415/* END_CASE */
Hanno Becker861d0bb2019-05-21 16:39:30 +01004416
Hanno Becker363b6462019-05-29 12:44:28 +01004417/* BEGIN_CASE */
4418void ssl_session_serialize_version_check( int corrupt_major,
Hanno Becker861d0bb2019-05-21 16:39:30 +01004419 int corrupt_minor,
4420 int corrupt_patch,
4421 int corrupt_config )
4422{
Hanno Becker363b6462019-05-29 12:44:28 +01004423 unsigned char serialized_session[ 2048 ];
4424 size_t serialized_session_len;
Hanno Beckerfe1275e2019-05-29 12:45:21 +01004425 unsigned cur_byte;
Hanno Becker861d0bb2019-05-21 16:39:30 +01004426 mbedtls_ssl_session session;
Hanno Beckerfe1275e2019-05-29 12:45:21 +01004427 uint8_t should_corrupt_byte[] = { corrupt_major == 1,
4428 corrupt_minor == 1,
4429 corrupt_patch == 1,
4430 corrupt_config == 1,
4431 corrupt_config == 1 };
4432
Hanno Becker861d0bb2019-05-21 16:39:30 +01004433 mbedtls_ssl_session_init( &session );
Paul Elliott46a6c202021-12-09 18:16:13 +00004434 TEST_ASSERT( ssl_populate_session_tls12( &session, 0, NULL ) == 0 );
Hanno Becker861d0bb2019-05-21 16:39:30 +01004435
Hanno Beckerfe1275e2019-05-29 12:45:21 +01004436 /* Infer length of serialized session. */
Hanno Becker861d0bb2019-05-21 16:39:30 +01004437 TEST_ASSERT( mbedtls_ssl_session_save( &session,
Hanno Becker363b6462019-05-29 12:44:28 +01004438 serialized_session,
4439 sizeof( serialized_session ),
4440 &serialized_session_len ) == 0 );
Hanno Becker861d0bb2019-05-21 16:39:30 +01004441
Hanno Beckerfe1275e2019-05-29 12:45:21 +01004442 mbedtls_ssl_session_free( &session );
Hanno Becker861d0bb2019-05-21 16:39:30 +01004443
Hanno Beckerfe1275e2019-05-29 12:45:21 +01004444 /* Without any modification, we should be able to successfully
Hanno Becker363b6462019-05-29 12:44:28 +01004445 * de-serialize the session - double-check that. */
Hanno Becker861d0bb2019-05-21 16:39:30 +01004446 TEST_ASSERT( mbedtls_ssl_session_load( &session,
Hanno Becker363b6462019-05-29 12:44:28 +01004447 serialized_session,
4448 serialized_session_len ) == 0 );
Hanno Becker861d0bb2019-05-21 16:39:30 +01004449 mbedtls_ssl_session_free( &session );
4450
Hanno Beckerfe1275e2019-05-29 12:45:21 +01004451 /* Go through the bytes in the serialized session header and
4452 * corrupt them bit-by-bit. */
4453 for( cur_byte = 0; cur_byte < sizeof( should_corrupt_byte ); cur_byte++ )
Hanno Becker861d0bb2019-05-21 16:39:30 +01004454 {
Hanno Beckerfe1275e2019-05-29 12:45:21 +01004455 int cur_bit;
4456 unsigned char * const byte = &serialized_session[ cur_byte ];
4457
4458 if( should_corrupt_byte[ cur_byte ] == 0 )
4459 continue;
4460
4461 for( cur_bit = 0; cur_bit < CHAR_BIT; cur_bit++ )
4462 {
4463 unsigned char const corrupted_bit = 0x1u << cur_bit;
4464 /* Modify a single bit in the serialized session. */
4465 *byte ^= corrupted_bit;
4466
4467 /* Attempt to deserialize */
4468 TEST_ASSERT( mbedtls_ssl_session_load( &session,
4469 serialized_session,
4470 serialized_session_len ) ==
Hanno Beckerf9b33032019-06-03 12:58:39 +01004471 MBEDTLS_ERR_SSL_VERSION_MISMATCH );
Hanno Beckerfe1275e2019-05-29 12:45:21 +01004472
4473 /* Undo the change */
4474 *byte ^= corrupted_bit;
4475 }
Hanno Becker861d0bb2019-05-21 16:39:30 +01004476 }
4477
Hanno Becker861d0bb2019-05-21 16:39:30 +01004478}
4479/* END_CASE */
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01004480
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02004481/* 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 +01004482void mbedtls_endpoint_sanity( int endpoint_type )
4483{
4484 enum { BUFFSIZE = 1024 };
4485 mbedtls_endpoint ep;
4486 int ret = -1;
4487
Andrzej Kurek15daf502020-02-12 09:17:52 -05004488 ret = mbedtls_endpoint_init( NULL, endpoint_type, MBEDTLS_PK_RSA,
4489 NULL, NULL, NULL );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01004490 TEST_ASSERT( MBEDTLS_ERR_SSL_BAD_INPUT_DATA == ret );
4491
Andrzej Kurekb2980742020-02-02 19:25:26 -05004492 ret = mbedtls_endpoint_certificate_init( NULL, MBEDTLS_PK_RSA );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01004493 TEST_ASSERT( MBEDTLS_ERR_SSL_BAD_INPUT_DATA == ret );
4494
Andrzej Kurek15daf502020-02-12 09:17:52 -05004495 ret = mbedtls_endpoint_init( &ep, endpoint_type, MBEDTLS_PK_RSA,
4496 NULL, NULL, NULL );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01004497 TEST_ASSERT( ret == 0 );
4498
4499exit:
Andrzej Kurek15daf502020-02-12 09:17:52 -05004500 mbedtls_endpoint_free( &ep, NULL );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01004501}
4502/* END_CASE */
4503
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02004504/* 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 +01004505void move_handshake_to_state(int endpoint_type, int state, int need_pass)
4506{
4507 enum { BUFFSIZE = 1024 };
4508 mbedtls_endpoint base_ep, second_ep;
4509 int ret = -1;
4510
Andrzej Kurek15daf502020-02-12 09:17:52 -05004511 ret = mbedtls_endpoint_init( &base_ep, endpoint_type, MBEDTLS_PK_RSA,
4512 NULL, NULL, NULL );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01004513 TEST_ASSERT( ret == 0 );
4514
4515 ret = mbedtls_endpoint_init( &second_ep,
4516 ( endpoint_type == MBEDTLS_SSL_IS_SERVER ) ?
Andrzej Kurekb2980742020-02-02 19:25:26 -05004517 MBEDTLS_SSL_IS_CLIENT : MBEDTLS_SSL_IS_SERVER,
Andrzej Kurek15daf502020-02-12 09:17:52 -05004518 MBEDTLS_PK_RSA, NULL, NULL, NULL );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01004519 TEST_ASSERT( ret == 0 );
4520
4521 ret = mbedtls_mock_socket_connect( &(base_ep.socket),
4522 &(second_ep.socket),
4523 BUFFSIZE );
4524 TEST_ASSERT( ret == 0 );
4525
4526 ret = mbedtls_move_handshake_to_state( &(base_ep.ssl),
4527 &(second_ep.ssl),
4528 state );
4529 if( need_pass )
4530 {
4531 TEST_ASSERT( ret == 0 );
4532 TEST_ASSERT( base_ep.ssl.state == state );
4533 }
4534 else
4535 {
4536 TEST_ASSERT( ret != 0 );
4537 TEST_ASSERT( base_ep.ssl.state != state );
4538 }
4539
4540exit:
Andrzej Kurek15daf502020-02-12 09:17:52 -05004541 mbedtls_endpoint_free( &base_ep, NULL );
4542 mbedtls_endpoint_free( &second_ep, NULL );
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01004543}
4544/* END_CASE */
Andrzej Kurekf40daa32020-02-04 09:00:01 -05004545
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02004546/* 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 +01004547void handshake_version( int dtls, int client_min_version, int client_max_version,
4548 int server_min_version, int server_max_version,
4549 int expected_negotiated_version )
Andrzej Kurekf40daa32020-02-04 09:00:01 -05004550{
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004551 handshake_test_options options;
4552 init_handshake_options( &options );
Andrzej Kurekda2b6782020-02-12 07:56:36 -05004553
Paul Elliottc8570442020-04-15 17:00:50 +01004554 options.client_min_version = client_min_version;
4555 options.client_max_version = client_max_version;
4556 options.server_min_version = server_min_version;
4557 options.server_max_version = server_max_version;
4558
4559 options.expected_negotiated_version = expected_negotiated_version;
4560
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004561 options.dtls = dtls;
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004562 perform_handshake( &options );
Andrzej Kurekf40daa32020-02-04 09:00:01 -05004563
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004564 /* The goto below is used to avoid an "unused label" warning.*/
4565 goto exit;
4566}
4567/* END_CASE */
Andrzej Kurek9e9efdc2020-02-26 05:25:23 -05004568
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02004569/* 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 -05004570void handshake_psk_cipher( char* cipher, int pk_alg, data_t *psk_str, int dtls )
4571{
4572 handshake_test_options options;
4573 init_handshake_options( &options );
Andrzej Kurekf40daa32020-02-04 09:00:01 -05004574
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004575 options.cipher = cipher;
4576 options.dtls = dtls;
4577 options.psk_str = psk_str;
4578 options.pk_alg = pk_alg;
Andrzej Kurekcc5169c2020-02-04 09:04:56 -05004579
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004580 perform_handshake( &options );
Andrzej Kurek316da1f2020-02-26 09:03:47 -05004581
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004582 /* The goto below is used to avoid an "unused label" warning.*/
4583 goto exit;
4584}
4585/* END_CASE */
Andrzej Kurek316da1f2020-02-26 09:03:47 -05004586
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02004587/* 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 -05004588void handshake_cipher( char* cipher, int pk_alg, int dtls )
4589{
4590 test_handshake_psk_cipher( cipher, pk_alg, NULL, dtls );
Andrzej Kurekf40daa32020-02-04 09:00:01 -05004591
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004592 /* The goto below is used to avoid an "unused label" warning.*/
4593 goto exit;
4594}
4595/* END_CASE */
Andrzej Kurekf40daa32020-02-04 09:00:01 -05004596
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02004597/* 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 -05004598void app_data( int mfl, int cli_msg_len, int srv_msg_len,
4599 int expected_cli_fragments,
4600 int expected_srv_fragments, int dtls )
4601{
4602 handshake_test_options options;
4603 init_handshake_options( &options );
Andrzej Kurekda2b6782020-02-12 07:56:36 -05004604
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004605 options.mfl = mfl;
4606 options.cli_msg_len = cli_msg_len;
4607 options.srv_msg_len = srv_msg_len;
4608 options.expected_cli_fragments = expected_cli_fragments;
4609 options.expected_srv_fragments = expected_srv_fragments;
4610 options.dtls = dtls;
Andrzej Kurekda2b6782020-02-12 07:56:36 -05004611
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004612 perform_handshake( &options );
4613 /* The goto below is used to avoid an "unused label" warning.*/
4614 goto exit;
4615}
4616/* END_CASE */
Andrzej Kurekda2b6782020-02-12 07:56:36 -05004617
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02004618/* 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 -05004619void app_data_tls( int mfl, int cli_msg_len, int srv_msg_len,
4620 int expected_cli_fragments,
4621 int expected_srv_fragments )
4622{
4623 test_app_data( mfl, cli_msg_len, srv_msg_len, expected_cli_fragments,
4624 expected_srv_fragments, 0 );
4625 /* The goto below is used to avoid an "unused label" warning.*/
4626 goto exit;
4627}
4628/* END_CASE */
Andrzej Kurekda2b6782020-02-12 07:56:36 -05004629
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02004630/* 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 -05004631void app_data_dtls( int mfl, int cli_msg_len, int srv_msg_len,
4632 int expected_cli_fragments,
4633 int expected_srv_fragments )
4634{
4635 test_app_data( mfl, cli_msg_len, srv_msg_len, expected_cli_fragments,
4636 expected_srv_fragments, 1 );
4637 /* The goto below is used to avoid an "unused label" warning.*/
4638 goto exit;
4639}
4640/* END_CASE */
Andrzej Kurekda2b6782020-02-12 07:56:36 -05004641
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02004642/* 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 -05004643void handshake_serialization( )
4644{
4645 handshake_test_options options;
4646 init_handshake_options( &options );
Andrzej Kurekda2b6782020-02-12 07:56:36 -05004647
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004648 options.serialize = 1;
4649 options.dtls = 1;
4650 perform_handshake( &options );
4651 /* The goto below is used to avoid an "unused label" warning.*/
4652 goto exit;
4653}
4654/* END_CASE */
Andrzej Kurekda2b6782020-02-12 07:56:36 -05004655
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02004656/* 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 +01004657void handshake_fragmentation( int mfl, int expected_srv_hs_fragmentation, int expected_cli_hs_fragmentation)
4658{
4659 handshake_test_options options;
4660 log_pattern srv_pattern, cli_pattern;
4661
4662 srv_pattern.pattern = cli_pattern.pattern = "found fragmented DTLS handshake";
4663 srv_pattern.counter = 0;
4664 cli_pattern.counter = 0;
4665
4666 init_handshake_options( &options );
4667 options.dtls = 1;
4668 options.mfl = mfl;
Darryl Greenaad82f92019-12-02 10:53:11 +00004669 /* Set cipher to one using CBC so that record splitting can be tested */
4670 options.cipher = "TLS-DHE-RSA-WITH-AES-256-CBC-SHA256";
Piotr Nowickibde7ee82020-02-21 10:59:50 +01004671 options.srv_auth_mode = MBEDTLS_SSL_VERIFY_REQUIRED;
4672 options.srv_log_obj = &srv_pattern;
4673 options.cli_log_obj = &cli_pattern;
4674 options.srv_log_fun = log_analyzer;
4675 options.cli_log_fun = log_analyzer;
4676
4677 perform_handshake( &options );
4678
4679 /* Test if the server received a fragmented handshake */
4680 if( expected_srv_hs_fragmentation )
4681 {
4682 TEST_ASSERT( srv_pattern.counter >= 1 );
4683 }
4684 /* Test if the client received a fragmented handshake */
4685 if( expected_cli_hs_fragmentation )
4686 {
4687 TEST_ASSERT( cli_pattern.counter >= 1 );
4688 }
4689}
4690/* END_CASE */
4691
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02004692/* 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 -05004693void renegotiation( int legacy_renegotiation )
4694{
4695 handshake_test_options options;
4696 init_handshake_options( &options );
Andrzej Kurekda2b6782020-02-12 07:56:36 -05004697
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004698 options.renegotiate = 1;
4699 options.legacy_renegotiation = legacy_renegotiation;
4700 options.dtls = 1;
Andrzej Kurek316da1f2020-02-26 09:03:47 -05004701
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05004702 perform_handshake( &options );
4703 /* The goto below is used to avoid an "unused label" warning.*/
4704 goto exit;
Andrzej Kurekf40daa32020-02-04 09:00:01 -05004705}
4706/* END_CASE */
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004707
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02004708/* 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 -05004709void resize_buffers( int mfl, int renegotiation, int legacy_renegotiation,
Andrzej Kurek8ea68722020-04-03 06:40:47 -04004710 int serialize, int dtls, char *cipher )
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004711{
4712 handshake_test_options options;
4713 init_handshake_options( &options );
4714
4715 options.mfl = mfl;
Andrzej Kurek8ea68722020-04-03 06:40:47 -04004716 options.cipher = cipher;
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004717 options.renegotiate = renegotiation;
4718 options.legacy_renegotiation = legacy_renegotiation;
4719 options.serialize = serialize;
4720 options.dtls = dtls;
4721 options.resize_buffers = 1;
4722
4723 perform_handshake( &options );
4724 /* The goto below is used to avoid an "unused label" warning.*/
4725 goto exit;
4726}
4727/* END_CASE */
4728
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02004729/* 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 -05004730void resize_buffers_serialize_mfl( int mfl )
4731{
Andrzej Kurek8ea68722020-04-03 06:40:47 -04004732 test_resize_buffers( mfl, 0, MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION, 1, 1,
4733 (char *) "" );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004734
4735 /* The goto below is used to avoid an "unused label" warning.*/
4736 goto exit;
4737}
4738/* END_CASE */
4739
Manuel Pégourié-Gonnardd12402f2020-05-20 10:34:25 +02004740/* 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 -04004741void resize_buffers_renegotiate_mfl( int mfl, int legacy_renegotiation,
4742 char *cipher )
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004743{
Andrzej Kurek8ea68722020-04-03 06:40:47 -04004744 test_resize_buffers( mfl, 1, legacy_renegotiation, 0, 1, cipher );
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004745
4746 /* The goto below is used to avoid an "unused label" warning.*/
4747 goto exit;
4748}
4749/* END_CASE */
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02004750
Manuel Pégourié-Gonnarded0e8642020-07-21 11:20:30 +02004751/* BEGIN_CASE depends_on:MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC:MBEDTLS_TEST_HOOKS */
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02004752void ssl_cf_hmac( int hash )
4753{
4754 /*
Gabor Mezei90437e32021-10-20 11:59:27 +02004755 * Test the function mbedtls_ct_hmac() against a reference
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02004756 * implementation.
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02004757 */
4758 mbedtls_md_context_t ctx, ref_ctx;
4759 const mbedtls_md_info_t *md_info;
4760 size_t out_len, block_size;
4761 size_t min_in_len, in_len, max_in_len, i;
4762 /* TLS additional data is 13 bytes (hence the "lucky 13" name) */
4763 unsigned char add_data[13];
4764 unsigned char ref_out[MBEDTLS_MD_MAX_SIZE];
4765 unsigned char *data = NULL;
4766 unsigned char *out = NULL;
4767 unsigned char rec_num = 0;
4768
4769 mbedtls_md_init( &ctx );
4770 mbedtls_md_init( &ref_ctx );
4771
4772 md_info = mbedtls_md_info_from_type( hash );
4773 TEST_ASSERT( md_info != NULL );
4774 out_len = mbedtls_md_get_size( md_info );
4775 TEST_ASSERT( out_len != 0 );
4776 block_size = hash == MBEDTLS_MD_SHA384 ? 128 : 64;
4777
4778 /* Use allocated out buffer to catch overwrites */
4779 ASSERT_ALLOC( out, out_len );
4780
4781 /* Set up contexts with the given hash and a dummy key */
4782 TEST_EQUAL( 0, mbedtls_md_setup( &ctx, md_info, 1 ) );
4783 TEST_EQUAL( 0, mbedtls_md_setup( &ref_ctx, md_info, 1 ) );
4784 memset( ref_out, 42, sizeof( ref_out ) );
4785 TEST_EQUAL( 0, mbedtls_md_hmac_starts( &ctx, ref_out, out_len ) );
4786 TEST_EQUAL( 0, mbedtls_md_hmac_starts( &ref_ctx, ref_out, out_len ) );
4787 memset( ref_out, 0, sizeof( ref_out ) );
4788
4789 /*
4790 * Test all possible lengths up to a point. The difference between
4791 * max_in_len and min_in_len is at most 255, and make sure they both vary
4792 * by at least one block size.
4793 */
4794 for( max_in_len = 0; max_in_len <= 255 + block_size; max_in_len++ )
4795 {
Chris Jones9634bb12021-01-20 15:56:42 +00004796 mbedtls_test_set_step( max_in_len * 10000 );
Manuel Pégourié-Gonnardca8287c2020-07-22 10:29:39 +02004797
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02004798 /* Use allocated in buffer to catch overreads */
Manuel Pégourié-Gonnardc3219002020-07-22 10:32:52 +02004799 ASSERT_ALLOC( data, max_in_len );
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02004800
4801 min_in_len = max_in_len > 255 ? max_in_len - 255 : 0;
4802 for( in_len = min_in_len; in_len <= max_in_len; in_len++ )
4803 {
Chris Jones9634bb12021-01-20 15:56:42 +00004804 mbedtls_test_set_step( max_in_len * 10000 + in_len );
Manuel Pégourié-Gonnardca8287c2020-07-22 10:29:39 +02004805
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02004806 /* Set up dummy data and add_data */
4807 rec_num++;
4808 memset( add_data, rec_num, sizeof( add_data ) );
4809 for( i = 0; i < in_len; i++ )
4810 data[i] = ( i & 0xff ) ^ rec_num;
4811
4812 /* Get the function's result */
Manuel Pégourié-Gonnard9670a592020-07-10 10:21:46 +02004813 TEST_CF_SECRET( &in_len, sizeof( in_len ) );
Gabor Mezei90437e32021-10-20 11:59:27 +02004814 TEST_EQUAL( 0, mbedtls_ct_hmac( &ctx, add_data, sizeof( add_data ),
gabor-mezei-arm9fa43ce2021-09-28 16:14:47 +02004815 data, in_len,
4816 min_in_len, max_in_len,
4817 out ) );
Manuel Pégourié-Gonnard9670a592020-07-10 10:21:46 +02004818 TEST_CF_PUBLIC( &in_len, sizeof( in_len ) );
4819 TEST_CF_PUBLIC( out, out_len );
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02004820
4821 /* Compute the reference result */
4822 TEST_EQUAL( 0, mbedtls_md_hmac_update( &ref_ctx, add_data,
4823 sizeof( add_data ) ) );
4824 TEST_EQUAL( 0, mbedtls_md_hmac_update( &ref_ctx, data, in_len ) );
4825 TEST_EQUAL( 0, mbedtls_md_hmac_finish( &ref_ctx, ref_out ) );
4826 TEST_EQUAL( 0, mbedtls_md_hmac_reset( &ref_ctx ) );
4827
4828 /* Compare */
4829 ASSERT_COMPARE( out, out_len, ref_out, out_len );
4830 }
4831
4832 mbedtls_free( data );
4833 data = NULL;
4834 }
4835
4836exit:
4837 mbedtls_md_free( &ref_ctx );
4838 mbedtls_md_free( &ctx );
4839
4840 mbedtls_free( data );
4841 mbedtls_free( out );
4842}
4843/* END_CASE */
Manuel Pégourié-Gonnard7fe2c5f2020-08-18 12:02:54 +02004844
4845/* BEGIN_CASE depends_on:MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC:MBEDTLS_TEST_HOOKS */
4846void ssl_cf_memcpy_offset( int offset_min, int offset_max, int len )
4847{
4848 unsigned char *dst = NULL;
4849 unsigned char *src = NULL;
4850 size_t src_len = offset_max + len;
4851 size_t secret;
4852
4853 ASSERT_ALLOC( dst, len );
4854 ASSERT_ALLOC( src, src_len );
4855
4856 /* Fill src in a way that we can detect if we copied the right bytes */
4857 mbedtls_test_rnd_std_rand( NULL, src, src_len );
4858
4859 for( secret = offset_min; secret <= (size_t) offset_max; secret++ )
4860 {
Chris Jones9634bb12021-01-20 15:56:42 +00004861 mbedtls_test_set_step( (int) secret );
Manuel Pégourié-Gonnard7fe2c5f2020-08-18 12:02:54 +02004862
4863 TEST_CF_SECRET( &secret, sizeof( secret ) );
Gabor Mezei90437e32021-10-20 11:59:27 +02004864 mbedtls_ct_memcpy_offset( dst, src, secret,
gabor-mezei-arm9fa43ce2021-09-28 16:14:47 +02004865 offset_min, offset_max, len );
Manuel Pégourié-Gonnard7fe2c5f2020-08-18 12:02:54 +02004866 TEST_CF_PUBLIC( &secret, sizeof( secret ) );
4867 TEST_CF_PUBLIC( dst, len );
4868
4869 ASSERT_COMPARE( dst, len, src + secret, len );
4870 }
4871
4872exit:
4873 mbedtls_free( dst );
4874 mbedtls_free( src );
4875}
4876/* END_CASE */
Hanno Becker6667ffd2021-04-19 21:59:22 +01004877
4878/* BEGIN_CASE depends_on:MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
4879void test_multiple_psks()
4880{
4881 unsigned char psk0[10] = { 0 };
4882 unsigned char psk0_identity[] = { 'f', 'o', 'o' };
4883
4884 unsigned char psk1[10] = { 0 };
4885 unsigned char psk1_identity[] = { 'b', 'a', 'r' };
4886
4887 mbedtls_ssl_config conf;
4888
4889 mbedtls_ssl_config_init( &conf );
4890
4891 TEST_ASSERT( mbedtls_ssl_conf_psk( &conf,
4892 psk0, sizeof( psk0 ),
4893 psk0_identity, sizeof( psk0_identity ) ) == 0 );
4894 TEST_ASSERT( mbedtls_ssl_conf_psk( &conf,
4895 psk1, sizeof( psk1 ),
4896 psk1_identity, sizeof( psk1_identity ) ) ==
4897 MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4898
4899exit:
4900
4901 mbedtls_ssl_config_free( &conf );
4902}
4903/* END_CASE */
4904
4905/* BEGIN_CASE depends_on:MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED:MBEDTLS_USE_PSA_CRYPTO */
4906void test_multiple_psks_opaque( int mode )
4907{
4908 /*
4909 * Mode 0: Raw PSK, then opaque PSK
4910 * Mode 1: Opaque PSK, then raw PSK
4911 * Mode 2: 2x opaque PSK
4912 */
4913
4914 unsigned char psk0_raw[10] = { 0 };
4915 unsigned char psk0_raw_identity[] = { 'f', 'o', 'o' };
4916
Andrzej Kurek03e01462022-01-03 12:53:24 +01004917 mbedtls_svc_key_id_t psk0_opaque = mbedtls_svc_key_id_make( 0x1, (psa_key_id_t) 1 );
4918
Hanno Becker6667ffd2021-04-19 21:59:22 +01004919 unsigned char psk0_opaque_identity[] = { 'f', 'o', 'o' };
4920
4921 unsigned char psk1_raw[10] = { 0 };
4922 unsigned char psk1_raw_identity[] = { 'b', 'a', 'r' };
4923
Andrzej Kurek03e01462022-01-03 12:53:24 +01004924 mbedtls_svc_key_id_t psk1_opaque = mbedtls_svc_key_id_make( 0x1, (psa_key_id_t) 2 );
4925
Hanno Becker6667ffd2021-04-19 21:59:22 +01004926 unsigned char psk1_opaque_identity[] = { 'b', 'a', 'r' };
4927
4928 mbedtls_ssl_config conf;
4929
4930 USE_PSA_INIT( );
4931 mbedtls_ssl_config_init( &conf );
4932
4933 switch( mode )
4934 {
4935 case 0:
4936
4937 TEST_ASSERT( mbedtls_ssl_conf_psk( &conf,
4938 psk0_raw, sizeof( psk0_raw ),
4939 psk0_raw_identity, sizeof( psk0_raw_identity ) )
4940 == 0 );
4941 TEST_ASSERT( mbedtls_ssl_conf_psk_opaque( &conf,
4942 psk1_opaque,
4943 psk1_opaque_identity, sizeof( psk1_opaque_identity ) )
4944 == MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4945 break;
4946
4947 case 1:
4948
4949 TEST_ASSERT( mbedtls_ssl_conf_psk_opaque( &conf,
4950 psk0_opaque,
4951 psk0_opaque_identity, sizeof( psk0_opaque_identity ) )
4952 == 0 );
4953 TEST_ASSERT( mbedtls_ssl_conf_psk( &conf,
4954 psk1_raw, sizeof( psk1_raw ),
4955 psk1_raw_identity, sizeof( psk1_raw_identity ) )
4956 == MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4957
4958 break;
4959
4960 case 2:
4961
4962 TEST_ASSERT( mbedtls_ssl_conf_psk_opaque( &conf,
4963 psk0_opaque,
4964 psk0_opaque_identity, sizeof( psk0_opaque_identity ) )
4965 == 0 );
4966 TEST_ASSERT( mbedtls_ssl_conf_psk_opaque( &conf,
4967 psk1_opaque,
4968 psk1_opaque_identity, sizeof( psk1_opaque_identity ) )
4969 == MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
4970
4971 break;
4972
4973 default:
4974 TEST_ASSERT( 0 );
4975 break;
4976 }
4977
4978exit:
4979
4980 mbedtls_ssl_config_free( &conf );
4981 USE_PSA_DONE( );
4982
4983}
4984/* END_CASE */
Brett Warren7f813d52021-10-20 23:08:38 +01004985
4986/* 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 */
4987void conf_curve()
4988{
4989
4990 mbedtls_ecp_group_id curve_list[] = { MBEDTLS_ECP_DP_SECP192R1,
4991 MBEDTLS_ECP_DP_SECP224R1,
4992 MBEDTLS_ECP_DP_SECP256R1,
4993 MBEDTLS_ECP_DP_NONE };
4994 mbedtls_ecp_group_id iana_tls_group_list[] = { MBEDTLS_SSL_IANA_TLS_GROUP_SECP192R1,
4995 MBEDTLS_SSL_IANA_TLS_GROUP_SECP224R1,
4996 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
4997 MBEDTLS_SSL_IANA_TLS_GROUP_NONE };
4998
4999 mbedtls_ssl_config conf;
5000 mbedtls_ssl_config_init( &conf );
5001
5002 mbedtls_ssl_conf_max_version( &conf, 3, 3 );
5003 mbedtls_ssl_conf_min_version( &conf, 3, 3 );
5004 mbedtls_ssl_conf_curves( &conf, curve_list );
5005
5006 mbedtls_ssl_context ssl;
5007 mbedtls_ssl_init( &ssl );
Paul Elliott46a6c202021-12-09 18:16:13 +00005008 TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
Brett Warren7f813d52021-10-20 23:08:38 +01005009
5010 TEST_ASSERT( ssl.handshake != NULL && ssl.handshake->group_list != NULL );
5011 TEST_ASSERT( ssl.conf != NULL && ssl.conf->group_list == NULL );
5012
5013 TEST_EQUAL( ssl.handshake->group_list[ARRAY_LENGTH( iana_tls_group_list ) - 1], MBEDTLS_SSL_IANA_TLS_GROUP_NONE );
5014
5015 for( size_t i = 0; i < ARRAY_LENGTH( iana_tls_group_list ); i++ )
5016 TEST_EQUAL( iana_tls_group_list[i], ssl.handshake->group_list[i] );
5017
5018 mbedtls_ssl_free( &ssl );
5019 mbedtls_ssl_config_free( &conf );
5020}
5021/* END_CASE */
5022
5023/* BEGIN_CASE depends_on:MBEDTLS_DEPRECATED_REMOVED */
5024void conf_group()
5025{
5026 uint16_t iana_tls_group_list[] = { MBEDTLS_SSL_IANA_TLS_GROUP_SECP192R1,
5027 MBEDTLS_SSL_IANA_TLS_GROUP_SECP224R1,
5028 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
5029 MBEDTLS_SSL_IANA_TLS_GROUP_NONE };
5030
5031 mbedtls_ssl_config conf;
5032 mbedtls_ssl_config_init( &conf );
5033
5034 mbedtls_ssl_conf_max_version( &conf, 3, 3 );
5035 mbedtls_ssl_conf_min_version( &conf, 3, 3 );
5036
5037 mbedtls_ssl_conf_groups( &conf, iana_tls_group_list );
5038
5039 mbedtls_ssl_context ssl;
5040 mbedtls_ssl_init( &ssl );
Paul Elliott46a6c202021-12-09 18:16:13 +00005041 TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
Brett Warren7f813d52021-10-20 23:08:38 +01005042
5043 TEST_ASSERT( ssl.conf != NULL && ssl.conf->group_list != NULL );
5044
5045 TEST_EQUAL( ssl.conf->group_list[ARRAY_LENGTH( iana_tls_group_list ) - 1], MBEDTLS_SSL_IANA_TLS_GROUP_NONE );
5046
5047 for( size_t i = 0; i < ARRAY_LENGTH( iana_tls_group_list ); i++ )
5048 TEST_EQUAL( iana_tls_group_list[i], ssl.conf->group_list[i] );
5049
5050 mbedtls_ssl_free( &ssl );
5051 mbedtls_ssl_config_free( &conf );
5052}
5053/* END_CASE */