blob: 3d4901c092b190021257e3a37d91392f191efb4e [file] [log] [blame]
Yanray Wang47907a42022-10-24 14:42:01 +08001/** \file ssl_helpers.c
2 *
3 * \brief Helper functions to set up a TLS connection.
4 */
5
6/*
7 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00008 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Yanray Wang47907a42022-10-24 14:42:01 +08009 */
10
11#include <test/ssl_helpers.h>
Valerio Setti384fbde2024-01-02 13:26:40 +010012#include "mbedtls/psa_util.h"
Yanray Wange6afd912022-10-27 12:11:18 +080013
Yanray Wang4d07d1c2022-10-27 15:28:16 +080014#if defined(MBEDTLS_SSL_TLS_C)
Ronald Cron10b040f2024-02-05 09:38:09 +010015int mbedtls_test_random(void *p_rng, unsigned char *output, size_t output_len)
Yanray Wange6afd912022-10-27 12:11:18 +080016{
17 (void) p_rng;
18 for (size_t i = 0; i < output_len; i++) {
19 output[i] = rand();
20 }
21
22 return 0;
23}
Yanray Wange6afd912022-10-27 12:11:18 +080024
Yanray Wange6afd912022-10-27 12:11:18 +080025void mbedtls_test_ssl_log_analyzer(void *ctx, int level,
26 const char *file, int line,
27 const char *str)
28{
29 mbedtls_test_ssl_log_pattern *p = (mbedtls_test_ssl_log_pattern *) ctx;
30
Manuel Pégourié-Gonnard6637ef72025-02-11 13:19:45 +010031/* Change 0 to 1 for debugging of test cases that use this function. */
32#if 0
33 const char *q, *basename;
34 /* Extract basename from file */
35 for (q = basename = file; *q != '\0'; q++) {
36 if (*q == '/' || *q == '\\') {
37 basename = q + 1;
38 }
39 }
40 printf("%s:%04d: |%d| %s",
41 basename, line, level, str);
42#else
Yanray Wange6afd912022-10-27 12:11:18 +080043 (void) level;
44 (void) line;
45 (void) file;
Manuel Pégourié-Gonnard6637ef72025-02-11 13:19:45 +010046#endif
Yanray Wange6afd912022-10-27 12:11:18 +080047
48 if (NULL != p &&
49 NULL != p->pattern &&
50 NULL != strstr(str, p->pattern)) {
51 p->counter++;
52 }
53}
54
55void mbedtls_test_init_handshake_options(
56 mbedtls_test_handshake_test_options *opts)
57{
58#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Ronald Cron987cf892024-03-04 10:24:27 +010059 static int rng_seed = 0xBEEF;
Yanray Wanga72bc9a2023-12-01 23:34:27 +080060
Yanray Wange6afd912022-10-27 12:11:18 +080061 srand(rng_seed);
62 rng_seed += 0xD0;
63#endif
Ronald Cronb4ad3e72024-01-26 14:57:53 +010064
65 memset(opts, 0, sizeof(*opts));
66
Yanray Wange6afd912022-10-27 12:11:18 +080067 opts->cipher = "";
68 opts->client_min_version = MBEDTLS_SSL_VERSION_UNKNOWN;
69 opts->client_max_version = MBEDTLS_SSL_VERSION_UNKNOWN;
70 opts->server_min_version = MBEDTLS_SSL_VERSION_UNKNOWN;
71 opts->server_max_version = MBEDTLS_SSL_VERSION_UNKNOWN;
Ronald Cron097ba142023-03-08 16:18:00 +010072 opts->expected_negotiated_version = MBEDTLS_SSL_VERSION_TLS1_3;
Yanray Wange6afd912022-10-27 12:11:18 +080073 opts->pk_alg = MBEDTLS_PK_RSA;
Yanray Wange6afd912022-10-27 12:11:18 +080074 opts->srv_auth_mode = MBEDTLS_SSL_VERIFY_NONE;
Yanray Wange6afd912022-10-27 12:11:18 +080075 opts->mfl = MBEDTLS_SSL_MAX_FRAG_LEN_NONE;
76 opts->cli_msg_len = 100;
77 opts->srv_msg_len = 100;
78 opts->expected_cli_fragments = 1;
79 opts->expected_srv_fragments = 1;
Yanray Wange6afd912022-10-27 12:11:18 +080080 opts->legacy_renegotiation = MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION;
Yanray Wange6afd912022-10-27 12:11:18 +080081 opts->resize_buffers = 1;
Ronald Cronced99be2024-01-26 15:49:12 +010082 opts->early_data = MBEDTLS_SSL_EARLY_DATA_DISABLED;
Ronald Cron5d3036e2024-02-23 07:43:45 +010083 opts->max_early_data_size = -1;
Yanray Wange6afd912022-10-27 12:11:18 +080084#if defined(MBEDTLS_SSL_CACHE_C)
Tom Cosgrove05b2a872023-07-21 11:31:13 +010085 TEST_CALLOC(opts->cache, 1);
Yanray Wange6afd912022-10-27 12:11:18 +080086 mbedtls_ssl_cache_init(opts->cache);
Pengyu Lv5cbb93e2023-07-10 11:09:40 +080087#if defined(MBEDTLS_HAVE_TIME)
88 TEST_EQUAL(mbedtls_ssl_cache_get_timeout(opts->cache),
89 MBEDTLS_SSL_CACHE_DEFAULT_TIMEOUT);
90#endif
Yanray Wange6afd912022-10-27 12:11:18 +080091exit:
92 return;
93#endif
94}
95
96void mbedtls_test_free_handshake_options(
97 mbedtls_test_handshake_test_options *opts)
98{
99#if defined(MBEDTLS_SSL_CACHE_C)
100 mbedtls_ssl_cache_free(opts->cache);
101 mbedtls_free(opts->cache);
102#else
103 (void) opts;
104#endif
105}
106
107#if defined(MBEDTLS_TEST_HOOKS)
108static void set_chk_buf_ptr_args(
109 mbedtls_ssl_chk_buf_ptr_args *args,
110 unsigned char *cur, unsigned char *end, size_t need)
111{
112 args->cur = cur;
113 args->end = end;
114 args->need = need;
115}
116
117static void reset_chk_buf_ptr_args(mbedtls_ssl_chk_buf_ptr_args *args)
118{
119 memset(args, 0, sizeof(*args));
120}
121#endif /* MBEDTLS_TEST_HOOKS */
122
Yanray Wange6afd912022-10-27 12:11:18 +0800123void mbedtls_test_ssl_buffer_init(mbedtls_test_ssl_buffer *buf)
124{
125 memset(buf, 0, sizeof(*buf));
126}
127
Yanray Wange6afd912022-10-27 12:11:18 +0800128int mbedtls_test_ssl_buffer_setup(mbedtls_test_ssl_buffer *buf,
129 size_t capacity)
130{
131 buf->buffer = (unsigned char *) mbedtls_calloc(capacity,
132 sizeof(unsigned char));
133 if (NULL == buf->buffer) {
134 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
135 }
136 buf->capacity = capacity;
137
138 return 0;
139}
140
141void mbedtls_test_ssl_buffer_free(mbedtls_test_ssl_buffer *buf)
142{
143 if (buf->buffer != NULL) {
144 mbedtls_free(buf->buffer);
145 }
146
147 memset(buf, 0, sizeof(*buf));
148}
149
Yanray Wange6afd912022-10-27 12:11:18 +0800150int mbedtls_test_ssl_buffer_put(mbedtls_test_ssl_buffer *buf,
151 const unsigned char *input, size_t input_len)
152{
153 size_t overflow = 0;
154
155 if ((buf == NULL) || (buf->buffer == NULL)) {
156 return -1;
157 }
158
159 /* Reduce input_len to a number that fits in the buffer. */
160 if ((buf->content_length + input_len) > buf->capacity) {
161 input_len = buf->capacity - buf->content_length;
162 }
163
164 if (input == NULL) {
165 return (input_len == 0) ? 0 : -1;
166 }
167
168 /* Check if the buffer has not come full circle and free space is not in
169 * the middle */
170 if (buf->start + buf->content_length < buf->capacity) {
171
172 /* Calculate the number of bytes that need to be placed at lower memory
173 * address */
174 if (buf->start + buf->content_length + input_len
175 > buf->capacity) {
176 overflow = (buf->start + buf->content_length + input_len)
177 % buf->capacity;
178 }
179
180 memcpy(buf->buffer + buf->start + buf->content_length, input,
181 input_len - overflow);
182 memcpy(buf->buffer, input + input_len - overflow, overflow);
183
184 } else {
185 /* The buffer has come full circle and free space is in the middle */
186 memcpy(buf->buffer + buf->start + buf->content_length - buf->capacity,
187 input, input_len);
188 }
189
190 buf->content_length += input_len;
Yanray Wanga8f445e2022-11-03 11:51:59 +0800191 return (input_len > INT_MAX) ? INT_MAX : (int) input_len;
Yanray Wange6afd912022-10-27 12:11:18 +0800192}
193
Yanray Wange6afd912022-10-27 12:11:18 +0800194int mbedtls_test_ssl_buffer_get(mbedtls_test_ssl_buffer *buf,
195 unsigned char *output, size_t output_len)
196{
197 size_t overflow = 0;
198
199 if ((buf == NULL) || (buf->buffer == NULL)) {
200 return -1;
201 }
202
203 if (output == NULL && output_len == 0) {
204 return 0;
205 }
206
207 if (buf->content_length < output_len) {
208 output_len = buf->content_length;
209 }
210
211 /* Calculate the number of bytes that need to be drawn from lower memory
212 * address */
213 if (buf->start + output_len > buf->capacity) {
214 overflow = (buf->start + output_len) % buf->capacity;
215 }
216
217 if (output != NULL) {
218 memcpy(output, buf->buffer + buf->start, output_len - overflow);
219 memcpy(output + output_len - overflow, buf->buffer, overflow);
220 }
221
222 buf->content_length -= output_len;
223 buf->start = (buf->start + output_len) % buf->capacity;
224
Yanray Wanga8f445e2022-11-03 11:51:59 +0800225 return (output_len > INT_MAX) ? INT_MAX : (int) output_len;
Yanray Wange6afd912022-10-27 12:11:18 +0800226}
227
Yanray Wangd19894f2023-03-16 11:47:39 +0800228int mbedtls_test_ssl_message_queue_setup(
229 mbedtls_test_ssl_message_queue *queue, size_t capacity)
Yanray Wange6afd912022-10-27 12:11:18 +0800230{
231 queue->messages = (size_t *) mbedtls_calloc(capacity, sizeof(size_t));
232 if (NULL == queue->messages) {
233 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
234 }
235
Yanray Wanga8f445e2022-11-03 11:51:59 +0800236 queue->capacity = (capacity > INT_MAX) ? INT_MAX : (int) capacity;
Yanray Wange6afd912022-10-27 12:11:18 +0800237 queue->pos = 0;
238 queue->num = 0;
239
240 return 0;
241}
242
Yanray Wangd19894f2023-03-16 11:47:39 +0800243void mbedtls_test_ssl_message_queue_free(
244 mbedtls_test_ssl_message_queue *queue)
Yanray Wange6afd912022-10-27 12:11:18 +0800245{
246 if (queue == NULL) {
247 return;
248 }
249
250 if (queue->messages != NULL) {
251 mbedtls_free(queue->messages);
252 }
253
254 memset(queue, 0, sizeof(*queue));
255}
256
Yanray Wange6afd912022-10-27 12:11:18 +0800257int mbedtls_test_ssl_message_queue_push_info(
258 mbedtls_test_ssl_message_queue *queue, size_t len)
259{
260 int place;
261 if (queue == NULL) {
262 return MBEDTLS_TEST_ERROR_ARG_NULL;
263 }
264
265 if (queue->num >= queue->capacity) {
266 return MBEDTLS_ERR_SSL_WANT_WRITE;
267 }
268
269 place = (queue->pos + queue->num) % queue->capacity;
270 queue->messages[place] = len;
271 queue->num++;
Yanray Wanga8f445e2022-11-03 11:51:59 +0800272 return (len > INT_MAX) ? INT_MAX : (int) len;
Yanray Wange6afd912022-10-27 12:11:18 +0800273}
274
Yanray Wange6afd912022-10-27 12:11:18 +0800275int mbedtls_test_ssl_message_queue_pop_info(
276 mbedtls_test_ssl_message_queue *queue, size_t buf_len)
277{
278 size_t message_length;
279 if (queue == NULL) {
280 return MBEDTLS_TEST_ERROR_ARG_NULL;
281 }
282 if (queue->num == 0) {
283 return MBEDTLS_ERR_SSL_WANT_READ;
284 }
285
286 message_length = queue->messages[queue->pos];
287 queue->messages[queue->pos] = 0;
288 queue->num--;
289 queue->pos++;
290 queue->pos %= queue->capacity;
291 if (queue->pos < 0) {
292 queue->pos += queue->capacity;
293 }
294
Yanray Wanga8f445e2022-11-03 11:51:59 +0800295 return (message_length > INT_MAX && buf_len > INT_MAX) ? INT_MAX :
296 (message_length > buf_len) ? (int) buf_len : (int) message_length;
Yanray Wange6afd912022-10-27 12:11:18 +0800297}
298
299/*
300 * Take a peek on the info about the next message length from the queue.
301 * This will be the oldest inserted message length(fifo).
302 *
303 * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null.
304 * \retval MBEDTLS_ERR_SSL_WANT_READ, if the queue is empty.
305 * \retval 0, if the peek was successful.
306 * \retval MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED, if the given buffer length is
307 * too small to fit the message. In this case the \p msg_len will be
308 * set to the full message length so that the
309 * caller knows what portion of the message can be dropped.
310 */
Yanray Wang5e22a922023-03-16 14:57:54 +0800311static int test_ssl_message_queue_peek_info(
312 mbedtls_test_ssl_message_queue *queue,
313 size_t buf_len, size_t *msg_len)
Yanray Wange6afd912022-10-27 12:11:18 +0800314{
315 if (queue == NULL || msg_len == NULL) {
316 return MBEDTLS_TEST_ERROR_ARG_NULL;
317 }
318 if (queue->num == 0) {
319 return MBEDTLS_ERR_SSL_WANT_READ;
320 }
321
322 *msg_len = queue->messages[queue->pos];
323 return (*msg_len > buf_len) ? MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED : 0;
324}
325
Yanray Wang5f86a422023-03-15 16:02:29 +0800326void mbedtls_test_mock_socket_init(mbedtls_test_mock_socket *socket)
Yanray Wange6afd912022-10-27 12:11:18 +0800327{
328 memset(socket, 0, sizeof(*socket));
329}
330
Yanray Wange6afd912022-10-27 12:11:18 +0800331void mbedtls_test_mock_socket_close(mbedtls_test_mock_socket *socket)
332{
333 if (socket == NULL) {
334 return;
335 }
336
337 if (socket->input != NULL) {
338 mbedtls_test_ssl_buffer_free(socket->input);
339 mbedtls_free(socket->input);
340 }
341
342 if (socket->output != NULL) {
343 mbedtls_test_ssl_buffer_free(socket->output);
344 mbedtls_free(socket->output);
345 }
346
347 if (socket->peer != NULL) {
348 memset(socket->peer, 0, sizeof(*socket->peer));
349 }
350
351 memset(socket, 0, sizeof(*socket));
352}
353
Yanray Wange6afd912022-10-27 12:11:18 +0800354int mbedtls_test_mock_socket_connect(mbedtls_test_mock_socket *peer1,
355 mbedtls_test_mock_socket *peer2,
356 size_t bufsize)
357{
358 int ret = -1;
359
360 peer1->output =
361 (mbedtls_test_ssl_buffer *) mbedtls_calloc(
362 1, sizeof(mbedtls_test_ssl_buffer));
363 if (peer1->output == NULL) {
364 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
365 goto exit;
366 }
367 mbedtls_test_ssl_buffer_init(peer1->output);
368 if (0 != (ret = mbedtls_test_ssl_buffer_setup(peer1->output, bufsize))) {
369 goto exit;
370 }
371
372 peer2->output =
373 (mbedtls_test_ssl_buffer *) mbedtls_calloc(
374 1, sizeof(mbedtls_test_ssl_buffer));
375 if (peer2->output == NULL) {
376 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
377 goto exit;
378 }
379 mbedtls_test_ssl_buffer_init(peer2->output);
380 if (0 != (ret = mbedtls_test_ssl_buffer_setup(peer2->output, bufsize))) {
381 goto exit;
382 }
383
384 peer1->peer = peer2;
385 peer2->peer = peer1;
386 peer1->input = peer2->output;
387 peer2->input = peer1->output;
388
389 peer1->status = peer2->status = MBEDTLS_MOCK_SOCKET_CONNECTED;
390 ret = 0;
391
392exit:
393
394 if (ret != 0) {
395 mbedtls_test_mock_socket_close(peer1);
396 mbedtls_test_mock_socket_close(peer2);
397 }
398
399 return ret;
400}
401
Yanray Wangaf727a22023-03-13 19:22:36 +0800402int mbedtls_test_mock_tcp_send_b(void *ctx,
403 const unsigned char *buf, size_t len)
Yanray Wange6afd912022-10-27 12:11:18 +0800404{
405 mbedtls_test_mock_socket *socket = (mbedtls_test_mock_socket *) ctx;
406
407 if (socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED) {
408 return -1;
409 }
410
411 return mbedtls_test_ssl_buffer_put(socket->output, buf, len);
412}
413
414int mbedtls_test_mock_tcp_recv_b(void *ctx, unsigned char *buf, size_t len)
415{
416 mbedtls_test_mock_socket *socket = (mbedtls_test_mock_socket *) ctx;
417
418 if (socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED) {
419 return -1;
420 }
421
422 return mbedtls_test_ssl_buffer_get(socket->input, buf, len);
423}
424
Yanray Wang34634352023-02-14 17:56:51 +0800425int mbedtls_test_mock_tcp_send_nb(void *ctx,
426 const unsigned char *buf, size_t len)
Yanray Wange6afd912022-10-27 12:11:18 +0800427{
428 mbedtls_test_mock_socket *socket = (mbedtls_test_mock_socket *) ctx;
429
430 if (socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED) {
431 return -1;
432 }
433
434 if (socket->output->capacity == socket->output->content_length) {
435 return MBEDTLS_ERR_SSL_WANT_WRITE;
436 }
437
438 return mbedtls_test_ssl_buffer_put(socket->output, buf, len);
439}
440
441int mbedtls_test_mock_tcp_recv_nb(void *ctx, unsigned char *buf, size_t len)
442{
443 mbedtls_test_mock_socket *socket = (mbedtls_test_mock_socket *) ctx;
444
445 if (socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED) {
446 return -1;
447 }
448
449 if (socket->input->content_length == 0) {
450 return MBEDTLS_ERR_SSL_WANT_READ;
451 }
452
453 return mbedtls_test_ssl_buffer_get(socket->input, buf, len);
454}
455
Yanray Wangd19894f2023-03-16 11:47:39 +0800456void mbedtls_test_message_socket_init(
457 mbedtls_test_message_socket_context *ctx)
Yanray Wange6afd912022-10-27 12:11:18 +0800458{
459 ctx->queue_input = NULL;
460 ctx->queue_output = NULL;
461 ctx->socket = NULL;
462}
463
Yanray Wange6afd912022-10-27 12:11:18 +0800464int mbedtls_test_message_socket_setup(
465 mbedtls_test_ssl_message_queue *queue_input,
466 mbedtls_test_ssl_message_queue *queue_output,
467 size_t queue_capacity,
468 mbedtls_test_mock_socket *socket,
469 mbedtls_test_message_socket_context *ctx)
470{
471 int ret = mbedtls_test_ssl_message_queue_setup(queue_input, queue_capacity);
472 if (ret != 0) {
473 return ret;
474 }
475 ctx->queue_input = queue_input;
476 ctx->queue_output = queue_output;
477 ctx->socket = socket;
Yanray Wang5f86a422023-03-15 16:02:29 +0800478 mbedtls_test_mock_socket_init(socket);
Yanray Wange6afd912022-10-27 12:11:18 +0800479
480 return 0;
481}
482
Yanray Wangd19894f2023-03-16 11:47:39 +0800483void mbedtls_test_message_socket_close(
484 mbedtls_test_message_socket_context *ctx)
Yanray Wange6afd912022-10-27 12:11:18 +0800485{
486 if (ctx == NULL) {
487 return;
488 }
489
490 mbedtls_test_ssl_message_queue_free(ctx->queue_input);
491 mbedtls_test_mock_socket_close(ctx->socket);
492 memset(ctx, 0, sizeof(*ctx));
493}
494
Yanray Wang34634352023-02-14 17:56:51 +0800495int mbedtls_test_mock_tcp_send_msg(void *ctx,
496 const unsigned char *buf, size_t len)
Yanray Wange6afd912022-10-27 12:11:18 +0800497{
498 mbedtls_test_ssl_message_queue *queue;
499 mbedtls_test_mock_socket *socket;
500 mbedtls_test_message_socket_context *context =
501 (mbedtls_test_message_socket_context *) ctx;
502
503 if (context == NULL || context->socket == NULL
504 || context->queue_output == NULL) {
505 return MBEDTLS_TEST_ERROR_CONTEXT_ERROR;
506 }
507
508 queue = context->queue_output;
509 socket = context->socket;
510
511 if (queue->num >= queue->capacity) {
512 return MBEDTLS_ERR_SSL_WANT_WRITE;
513 }
514
515 if (mbedtls_test_mock_tcp_send_b(socket, buf, len) != (int) len) {
516 return MBEDTLS_TEST_ERROR_SEND_FAILED;
517 }
518
519 return mbedtls_test_ssl_message_queue_push_info(queue, len);
520}
521
Yanray Wang34634352023-02-14 17:56:51 +0800522int mbedtls_test_mock_tcp_recv_msg(void *ctx,
523 unsigned char *buf, size_t buf_len)
Yanray Wange6afd912022-10-27 12:11:18 +0800524{
525 mbedtls_test_ssl_message_queue *queue;
526 mbedtls_test_mock_socket *socket;
527 mbedtls_test_message_socket_context *context =
528 (mbedtls_test_message_socket_context *) ctx;
529 size_t drop_len = 0;
530 size_t msg_len;
531 int ret;
532
533 if (context == NULL || context->socket == NULL
534 || context->queue_input == NULL) {
535 return MBEDTLS_TEST_ERROR_CONTEXT_ERROR;
536 }
537
538 queue = context->queue_input;
539 socket = context->socket;
540
541 /* Peek first, so that in case of a socket error the data remains in
542 * the queue. */
Yanray Wang5e22a922023-03-16 14:57:54 +0800543 ret = test_ssl_message_queue_peek_info(queue, buf_len, &msg_len);
Yanray Wange6afd912022-10-27 12:11:18 +0800544 if (ret == MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED) {
545 /* Calculate how much to drop */
546 drop_len = msg_len - buf_len;
547
548 /* Set the requested message len to be buffer length */
549 msg_len = buf_len;
550 } else if (ret != 0) {
551 return ret;
552 }
553
554 if (mbedtls_test_mock_tcp_recv_b(socket, buf, msg_len) != (int) msg_len) {
555 return MBEDTLS_TEST_ERROR_RECV_FAILED;
556 }
557
558 if (ret == MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED) {
559 /* Drop the remaining part of the message */
Yanray Wangaf727a22023-03-13 19:22:36 +0800560 if (mbedtls_test_mock_tcp_recv_b(socket, NULL, drop_len) !=
561 (int) drop_len) {
Yanray Wange6afd912022-10-27 12:11:18 +0800562 /* Inconsistent state - part of the message was read,
563 * and a part couldn't. Not much we can do here, but it should not
564 * happen in test environment, unless forced manually. */
565 }
566 }
Tomás Gonzáleza9b90de2024-02-01 11:12:20 +0000567 ret = mbedtls_test_ssl_message_queue_pop_info(queue, buf_len);
568 if (ret < 0) {
569 return ret;
570 }
Yanray Wange6afd912022-10-27 12:11:18 +0800571
Yanray Wanga8f445e2022-11-03 11:51:59 +0800572 return (msg_len > INT_MAX) ? INT_MAX : (int) msg_len;
Yanray Wange6afd912022-10-27 12:11:18 +0800573}
574
575#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
576
577/*
578 * Deinitializes certificates from endpoint represented by \p ep.
579 */
Yanray Wangf6f71902023-03-15 16:05:14 +0800580static void test_ssl_endpoint_certificate_free(mbedtls_test_ssl_endpoint *ep)
Yanray Wange6afd912022-10-27 12:11:18 +0800581{
582 mbedtls_test_ssl_endpoint_certificate *cert = &(ep->cert);
583 if (cert != NULL) {
584 if (cert->ca_cert != NULL) {
585 mbedtls_x509_crt_free(cert->ca_cert);
586 mbedtls_free(cert->ca_cert);
587 cert->ca_cert = NULL;
588 }
589 if (cert->cert != NULL) {
590 mbedtls_x509_crt_free(cert->cert);
591 mbedtls_free(cert->cert);
592 cert->cert = NULL;
593 }
594 if (cert->pkey != NULL) {
595#if defined(MBEDTLS_USE_PSA_CRYPTO)
596 if (mbedtls_pk_get_type(cert->pkey) == MBEDTLS_PK_OPAQUE) {
Valerio Setti4f387ef2023-05-02 14:15:59 +0200597 psa_destroy_key(cert->pkey->priv_id);
Yanray Wange6afd912022-10-27 12:11:18 +0800598 }
599#endif
600 mbedtls_pk_free(cert->pkey);
601 mbedtls_free(cert->pkey);
602 cert->pkey = NULL;
603 }
604 }
605}
606
Yanray Wange6afd912022-10-27 12:11:18 +0800607int mbedtls_test_ssl_endpoint_certificate_init(mbedtls_test_ssl_endpoint *ep,
608 int pk_alg,
609 int opaque_alg, int opaque_alg2,
610 int opaque_usage)
611{
612 int i = 0;
613 int ret = -1;
Gilles Peskine946bf142025-04-08 09:48:40 +0200614 int ok = 0;
Yanray Wange6afd912022-10-27 12:11:18 +0800615 mbedtls_test_ssl_endpoint_certificate *cert = NULL;
616#if defined(MBEDTLS_USE_PSA_CRYPTO)
617 mbedtls_svc_key_id_t key_slot = MBEDTLS_SVC_KEY_ID_INIT;
618#endif
619
620 if (ep == NULL) {
621 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
622 }
623
624 cert = &(ep->cert);
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100625 TEST_CALLOC(cert->ca_cert, 1);
626 TEST_CALLOC(cert->cert, 1);
627 TEST_CALLOC(cert->pkey, 1);
Yanray Wange6afd912022-10-27 12:11:18 +0800628
629 mbedtls_x509_crt_init(cert->ca_cert);
630 mbedtls_x509_crt_init(cert->cert);
631 mbedtls_pk_init(cert->pkey);
632
633 /* Load the trusted CA */
634
635 for (i = 0; mbedtls_test_cas_der[i] != NULL; i++) {
636 ret = mbedtls_x509_crt_parse_der(
637 cert->ca_cert,
638 (const unsigned char *) mbedtls_test_cas_der[i],
639 mbedtls_test_cas_der_len[i]);
Gilles Peskine353eb332025-05-14 17:42:53 +0200640 TEST_EQUAL(ret, 0);
Yanray Wange6afd912022-10-27 12:11:18 +0800641 }
642
643 /* Load own certificate and private key */
644
645 if (ep->conf.endpoint == MBEDTLS_SSL_IS_SERVER) {
646 if (pk_alg == MBEDTLS_PK_RSA) {
647 ret = mbedtls_x509_crt_parse(
648 cert->cert,
649 (const unsigned char *) mbedtls_test_srv_crt_rsa_sha256_der,
650 mbedtls_test_srv_crt_rsa_sha256_der_len);
Gilles Peskine353eb332025-05-14 17:42:53 +0200651 TEST_EQUAL(ret, 0);
Yanray Wange6afd912022-10-27 12:11:18 +0800652
653 ret = mbedtls_pk_parse_key(
654 cert->pkey,
655 (const unsigned char *) mbedtls_test_srv_key_rsa_der,
Ben Taylor440cb2a2025-03-05 09:40:08 +0000656 mbedtls_test_srv_key_rsa_der_len, NULL, 0);
Gilles Peskine353eb332025-05-14 17:42:53 +0200657 TEST_EQUAL(ret, 0);
Yanray Wange6afd912022-10-27 12:11:18 +0800658 } else {
659 ret = mbedtls_x509_crt_parse(
660 cert->cert,
661 (const unsigned char *) mbedtls_test_srv_crt_ec_der,
662 mbedtls_test_srv_crt_ec_der_len);
Gilles Peskine353eb332025-05-14 17:42:53 +0200663 TEST_EQUAL(ret, 0);
Yanray Wange6afd912022-10-27 12:11:18 +0800664
665 ret = mbedtls_pk_parse_key(
666 cert->pkey,
667 (const unsigned char *) mbedtls_test_srv_key_ec_der,
Ben Taylor440cb2a2025-03-05 09:40:08 +0000668 mbedtls_test_srv_key_ec_der_len, NULL, 0);
Gilles Peskine353eb332025-05-14 17:42:53 +0200669 TEST_EQUAL(ret, 0);
Yanray Wange6afd912022-10-27 12:11:18 +0800670 }
671 } else {
672 if (pk_alg == MBEDTLS_PK_RSA) {
673 ret = mbedtls_x509_crt_parse(
674 cert->cert,
675 (const unsigned char *) mbedtls_test_cli_crt_rsa_der,
676 mbedtls_test_cli_crt_rsa_der_len);
Gilles Peskine353eb332025-05-14 17:42:53 +0200677 TEST_EQUAL(ret, 0);
Yanray Wange6afd912022-10-27 12:11:18 +0800678
679 ret = mbedtls_pk_parse_key(
680 cert->pkey,
681 (const unsigned char *) mbedtls_test_cli_key_rsa_der,
Ben Taylor440cb2a2025-03-05 09:40:08 +0000682 mbedtls_test_cli_key_rsa_der_len, NULL, 0);
Gilles Peskine353eb332025-05-14 17:42:53 +0200683 TEST_EQUAL(ret, 0);
Yanray Wange6afd912022-10-27 12:11:18 +0800684 } else {
685 ret = mbedtls_x509_crt_parse(
686 cert->cert,
687 (const unsigned char *) mbedtls_test_cli_crt_ec_der,
688 mbedtls_test_cli_crt_ec_len);
Gilles Peskine353eb332025-05-14 17:42:53 +0200689 TEST_EQUAL(ret, 0);
Yanray Wange6afd912022-10-27 12:11:18 +0800690
691 ret = mbedtls_pk_parse_key(
692 cert->pkey,
693 (const unsigned char *) mbedtls_test_cli_key_ec_der,
Ben Taylor440cb2a2025-03-05 09:40:08 +0000694 mbedtls_test_cli_key_ec_der_len, NULL, 0);
Gilles Peskine353eb332025-05-14 17:42:53 +0200695 TEST_EQUAL(ret, 0);
Yanray Wange6afd912022-10-27 12:11:18 +0800696 }
697 }
698
699#if defined(MBEDTLS_USE_PSA_CRYPTO)
700 if (opaque_alg != 0) {
Valerio Setti1fa2f6e2024-02-27 08:11:25 +0100701 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
702 /* Use a fake key usage to get a successful initial guess for the PSA attributes. */
Valerio Settia9de9442024-02-27 13:56:09 +0100703 TEST_EQUAL(mbedtls_pk_get_psa_attributes(cert->pkey, PSA_KEY_USAGE_SIGN_HASH,
Valerio Setti1fa2f6e2024-02-27 08:11:25 +0100704 &key_attr), 0);
Valerio Settia9de9442024-02-27 13:56:09 +0100705 /* Then manually usage, alg and alg2 as requested by the test. */
Valerio Setti1fa2f6e2024-02-27 08:11:25 +0100706 psa_set_key_usage_flags(&key_attr, opaque_usage);
707 psa_set_key_algorithm(&key_attr, opaque_alg);
708 if (opaque_alg2 != PSA_ALG_NONE) {
709 psa_set_key_enrollment_algorithm(&key_attr, opaque_alg2);
710 }
711 TEST_EQUAL(mbedtls_pk_import_into_psa(cert->pkey, &key_attr, &key_slot), 0);
712 mbedtls_pk_free(cert->pkey);
713 mbedtls_pk_init(cert->pkey);
714 TEST_EQUAL(mbedtls_pk_setup_opaque(cert->pkey, key_slot), 0);
Yanray Wange6afd912022-10-27 12:11:18 +0800715 }
716#else
717 (void) opaque_alg;
718 (void) opaque_alg2;
719 (void) opaque_usage;
720#endif
721
722 mbedtls_ssl_conf_ca_chain(&(ep->conf), cert->ca_cert, NULL);
723
724 ret = mbedtls_ssl_conf_own_cert(&(ep->conf), cert->cert,
725 cert->pkey);
Gilles Peskine353eb332025-05-14 17:42:53 +0200726 TEST_EQUAL(ret, 0);
Yanray Wange6afd912022-10-27 12:11:18 +0800727 TEST_ASSERT(ep->conf.key_cert != NULL);
728
729 ret = mbedtls_ssl_conf_own_cert(&(ep->conf), NULL, NULL);
Gilles Peskine353eb332025-05-14 17:42:53 +0200730 TEST_EQUAL(ret, 0);
Yanray Wange6afd912022-10-27 12:11:18 +0800731 TEST_ASSERT(ep->conf.key_cert == NULL);
732
733 ret = mbedtls_ssl_conf_own_cert(&(ep->conf), cert->cert,
734 cert->pkey);
Gilles Peskine353eb332025-05-14 17:42:53 +0200735 TEST_EQUAL(ret, 0);
Yanray Wange6afd912022-10-27 12:11:18 +0800736
Gilles Peskine946bf142025-04-08 09:48:40 +0200737 ok = 1;
738
Yanray Wange6afd912022-10-27 12:11:18 +0800739exit:
Gilles Peskine946bf142025-04-08 09:48:40 +0200740 if (ret == 0 && !ok) {
741 /* Exiting due to a test assertion that isn't ret == 0 */
742 ret = -1;
743 }
Yanray Wange6afd912022-10-27 12:11:18 +0800744 if (ret != 0) {
Yanray Wangf6f71902023-03-15 16:05:14 +0800745 test_ssl_endpoint_certificate_free(ep);
Yanray Wange6afd912022-10-27 12:11:18 +0800746 }
747
748 return ret;
749}
750
Yanray Wange6afd912022-10-27 12:11:18 +0800751int mbedtls_test_ssl_endpoint_init(
752 mbedtls_test_ssl_endpoint *ep, int endpoint_type,
753 mbedtls_test_handshake_test_options *options,
754 mbedtls_test_message_socket_context *dtls_context,
755 mbedtls_test_ssl_message_queue *input_queue,
Ronald Cronfb536472024-01-26 14:55:25 +0100756 mbedtls_test_ssl_message_queue *output_queue)
Yanray Wange6afd912022-10-27 12:11:18 +0800757{
758 int ret = -1;
759 uintptr_t user_data_n;
760
761 if (dtls_context != NULL &&
762 (input_queue == NULL || output_queue == NULL)) {
763 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
764
765 }
766
767 if (ep == NULL) {
768 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
769 }
770
771 memset(ep, 0, sizeof(*ep));
772
773 ep->name = (endpoint_type == MBEDTLS_SSL_IS_SERVER) ? "Server" : "Client";
774
775 mbedtls_ssl_init(&(ep->ssl));
776 mbedtls_ssl_config_init(&(ep->conf));
Yanray Wange6afd912022-10-27 12:11:18 +0800777
778 TEST_ASSERT(mbedtls_ssl_conf_get_user_data_p(&ep->conf) == NULL);
779 TEST_EQUAL(mbedtls_ssl_conf_get_user_data_n(&ep->conf), 0);
780 TEST_ASSERT(mbedtls_ssl_get_user_data_p(&ep->ssl) == NULL);
781 TEST_EQUAL(mbedtls_ssl_get_user_data_n(&ep->ssl), 0);
782
783 (void) mbedtls_test_rnd_std_rand(NULL,
784 (void *) &user_data_n,
785 sizeof(user_data_n));
786 mbedtls_ssl_conf_set_user_data_n(&ep->conf, user_data_n);
787 mbedtls_ssl_set_user_data_n(&ep->ssl, user_data_n);
788
789 if (dtls_context != NULL) {
Gilles Peskine353eb332025-05-14 17:42:53 +0200790 TEST_EQUAL(mbedtls_test_message_socket_setup(input_queue, output_queue,
791 100, &(ep->socket),
792 dtls_context), 0);
Yanray Wange6afd912022-10-27 12:11:18 +0800793 } else {
Yanray Wang5f86a422023-03-15 16:02:29 +0800794 mbedtls_test_mock_socket_init(&(ep->socket));
Yanray Wange6afd912022-10-27 12:11:18 +0800795 }
796
797 /* Non-blocking callbacks without timeout */
798 if (dtls_context != NULL) {
799 mbedtls_ssl_set_bio(&(ep->ssl), dtls_context,
800 mbedtls_test_mock_tcp_send_msg,
801 mbedtls_test_mock_tcp_recv_msg,
802 NULL);
803 } else {
804 mbedtls_ssl_set_bio(&(ep->ssl), &(ep->socket),
805 mbedtls_test_mock_tcp_send_nb,
806 mbedtls_test_mock_tcp_recv_nb,
807 NULL);
808 }
809
810 ret = mbedtls_ssl_config_defaults(&(ep->conf), endpoint_type,
811 (dtls_context != NULL) ?
812 MBEDTLS_SSL_TRANSPORT_DATAGRAM :
813 MBEDTLS_SSL_TRANSPORT_STREAM,
814 MBEDTLS_SSL_PRESET_DEFAULT);
Gilles Peskine353eb332025-05-14 17:42:53 +0200815 TEST_EQUAL(ret, 0);
Yanray Wange6afd912022-10-27 12:11:18 +0800816
Ronald Crona697a712023-03-09 17:47:42 +0100817 if (MBEDTLS_SSL_IS_CLIENT == endpoint_type) {
818 if (options->client_min_version != MBEDTLS_SSL_VERSION_UNKNOWN) {
819 mbedtls_ssl_conf_min_tls_version(&(ep->conf),
820 options->client_min_version);
821 }
822
823 if (options->client_max_version != MBEDTLS_SSL_VERSION_UNKNOWN) {
824 mbedtls_ssl_conf_max_tls_version(&(ep->conf),
825 options->client_max_version);
826 }
827 } else {
828 if (options->server_min_version != MBEDTLS_SSL_VERSION_UNKNOWN) {
829 mbedtls_ssl_conf_min_tls_version(&(ep->conf),
830 options->server_min_version);
831 }
832
833 if (options->server_max_version != MBEDTLS_SSL_VERSION_UNKNOWN) {
834 mbedtls_ssl_conf_max_tls_version(&(ep->conf),
835 options->server_max_version);
836 }
837 }
838
Ronald Cronfb536472024-01-26 14:55:25 +0100839 if (options->group_list != NULL) {
840 mbedtls_ssl_conf_groups(&(ep->conf), options->group_list);
Yanray Wange6afd912022-10-27 12:11:18 +0800841 }
842
843 mbedtls_ssl_conf_authmode(&(ep->conf), MBEDTLS_SSL_VERIFY_REQUIRED);
844
Ronald Cronced99be2024-01-26 15:49:12 +0100845#if defined(MBEDTLS_SSL_EARLY_DATA)
846 mbedtls_ssl_conf_early_data(&(ep->conf), options->early_data);
Ronald Cron5d3036e2024-02-23 07:43:45 +0100847#if defined(MBEDTLS_SSL_SRV_C)
848 if (endpoint_type == MBEDTLS_SSL_IS_SERVER &&
849 (options->max_early_data_size >= 0)) {
850 mbedtls_ssl_conf_max_early_data_size(&(ep->conf),
851 options->max_early_data_size);
852 }
853#endif
Waleed Elmelegy4dfb0e72024-03-14 01:48:40 +0000854#if defined(MBEDTLS_SSL_ALPN)
855 /* check that alpn_list contains at least one valid entry */
856 if (options->alpn_list[0] != NULL) {
857 mbedtls_ssl_conf_alpn_protocols(&(ep->conf), options->alpn_list);
858 }
859#endif
Ronald Cronced99be2024-01-26 15:49:12 +0100860#endif
861
Yanray Wange6afd912022-10-27 12:11:18 +0800862#if defined(MBEDTLS_SSL_CACHE_C) && defined(MBEDTLS_SSL_SRV_C)
863 if (endpoint_type == MBEDTLS_SSL_IS_SERVER && options->cache != NULL) {
864 mbedtls_ssl_conf_session_cache(&(ep->conf), options->cache,
865 mbedtls_ssl_cache_get,
866 mbedtls_ssl_cache_set);
867 }
868#endif
869
870 ret = mbedtls_ssl_setup(&(ep->ssl), &(ep->conf));
Gilles Peskine353eb332025-05-14 17:42:53 +0200871 TEST_EQUAL(ret, 0);
Yanray Wange6afd912022-10-27 12:11:18 +0800872
Gilles Peskine856a3702025-02-13 17:28:49 +0100873 if (MBEDTLS_SSL_IS_CLIENT == endpoint_type) {
874 ret = mbedtls_ssl_set_hostname(&(ep->ssl), "localhost");
Gilles Peskine55b8bb42025-04-08 09:44:34 +0200875 TEST_EQUAL(ret, 0);
Gilles Peskine856a3702025-02-13 17:28:49 +0100876 }
877
Yanray Wange6afd912022-10-27 12:11:18 +0800878#if defined(MBEDTLS_SSL_PROTO_DTLS) && defined(MBEDTLS_SSL_SRV_C)
879 if (endpoint_type == MBEDTLS_SSL_IS_SERVER && dtls_context != NULL) {
880 mbedtls_ssl_conf_dtls_cookies(&(ep->conf), NULL, NULL, NULL);
881 }
882#endif
883
Ronald Cronec3408d2024-01-16 17:50:40 +0100884#if defined(MBEDTLS_DEBUG_C)
885#if defined(MBEDTLS_SSL_SRV_C)
886 if (endpoint_type == MBEDTLS_SSL_IS_SERVER &&
887 options->srv_log_fun != NULL) {
888 mbedtls_ssl_conf_dbg(&(ep->conf), options->srv_log_fun,
889 options->srv_log_obj);
890 }
891#endif
892#if defined(MBEDTLS_SSL_CLI_C)
893 if (endpoint_type == MBEDTLS_SSL_IS_CLIENT &&
894 options->cli_log_fun != NULL) {
895 mbedtls_ssl_conf_dbg(&(ep->conf), options->cli_log_fun,
896 options->cli_log_obj);
897 }
898#endif
899#endif /* MBEDTLS_DEBUG_C */
900
Yanray Wange6afd912022-10-27 12:11:18 +0800901 ret = mbedtls_test_ssl_endpoint_certificate_init(ep, options->pk_alg,
902 options->opaque_alg,
903 options->opaque_alg2,
904 options->opaque_usage);
Gilles Peskine353eb332025-05-14 17:42:53 +0200905 TEST_EQUAL(ret, 0);
Yanray Wange6afd912022-10-27 12:11:18 +0800906
907 TEST_EQUAL(mbedtls_ssl_conf_get_user_data_n(&ep->conf), user_data_n);
908 mbedtls_ssl_conf_set_user_data_p(&ep->conf, ep);
909 TEST_EQUAL(mbedtls_ssl_get_user_data_n(&ep->ssl), user_data_n);
910 mbedtls_ssl_set_user_data_p(&ep->ssl, ep);
911
Gilles Peskine946bf142025-04-08 09:48:40 +0200912 return 0;
913
Yanray Wange6afd912022-10-27 12:11:18 +0800914exit:
Gilles Peskine946bf142025-04-08 09:48:40 +0200915 if (ret == 0) {
916 /* Exiting due to a test assertion that isn't ret == 0 */
917 ret = -1;
918 }
Yanray Wange6afd912022-10-27 12:11:18 +0800919 return ret;
920}
921
Yanray Wange6afd912022-10-27 12:11:18 +0800922void mbedtls_test_ssl_endpoint_free(
923 mbedtls_test_ssl_endpoint *ep,
924 mbedtls_test_message_socket_context *context)
925{
Yanray Wangf6f71902023-03-15 16:05:14 +0800926 test_ssl_endpoint_certificate_free(ep);
Yanray Wange6afd912022-10-27 12:11:18 +0800927
928 mbedtls_ssl_free(&(ep->ssl));
929 mbedtls_ssl_config_free(&(ep->conf));
930
931 if (context != NULL) {
932 mbedtls_test_message_socket_close(context);
933 } else {
934 mbedtls_test_mock_socket_close(&(ep->socket));
935 }
936}
937
Yanray Wange6afd912022-10-27 12:11:18 +0800938int mbedtls_test_move_handshake_to_state(mbedtls_ssl_context *ssl,
939 mbedtls_ssl_context *second_ssl,
940 int state)
941{
942 enum { BUFFSIZE = 1024 };
943 int max_steps = 1000;
944 int ret = 0;
945
946 if (ssl == NULL || second_ssl == NULL) {
947 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
948 }
949
950 /* Perform communication via connected sockets */
951 while ((ssl->state != state) && (--max_steps >= 0)) {
952 /* If /p second_ssl ends the handshake procedure before /p ssl then
953 * there is no need to call the next step */
954 if (!mbedtls_ssl_is_handshake_over(second_ssl)) {
955 ret = mbedtls_ssl_handshake_step(second_ssl);
956 if (ret != 0 && ret != MBEDTLS_ERR_SSL_WANT_READ &&
957 ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
958 return ret;
959 }
960 }
961
962 /* We only care about the \p ssl state and returns, so we call it last,
963 * to leave the iteration as soon as the state is as expected. */
964 ret = mbedtls_ssl_handshake_step(ssl);
965 if (ret != 0 && ret != MBEDTLS_ERR_SSL_WANT_READ &&
966 ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
967 return ret;
968 }
969 }
970
971 return (max_steps >= 0) ? ret : -1;
972}
973
974#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
975
976/*
977 * Write application data. Increase write counter if necessary.
978 */
Michael Schuster54300d42024-06-04 02:30:22 +0200979static int mbedtls_ssl_write_fragment(mbedtls_ssl_context *ssl,
Michael Schusterbd89b792024-06-04 02:41:10 +0200980 unsigned char *buf, int buf_len,
981 int *written,
982 const int expected_fragments)
Yanray Wange6afd912022-10-27 12:11:18 +0800983{
Agathiyan Bragadeesh93212652023-07-12 11:22:59 +0100984 int ret;
Yanray Wange6afd912022-10-27 12:11:18 +0800985 /* Verify that calling mbedtls_ssl_write with a NULL buffer and zero length is
986 * a valid no-op for TLS connections. */
987 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Gilles Peskine353eb332025-05-14 17:42:53 +0200988 TEST_EQUAL(mbedtls_ssl_write(ssl, NULL, 0), 0);
Yanray Wange6afd912022-10-27 12:11:18 +0800989 }
990
Agathiyan Bragadeesh93212652023-07-12 11:22:59 +0100991 ret = mbedtls_ssl_write(ssl, buf + *written, buf_len - *written);
Yanray Wange6afd912022-10-27 12:11:18 +0800992 if (ret > 0) {
993 *written += ret;
994 }
995
996 if (expected_fragments == 0) {
997 /* Used for DTLS and the message size larger than MFL. In that case
998 * the message can not be fragmented and the library should return
999 * MBEDTLS_ERR_SSL_BAD_INPUT_DATA error. This error must be returned
Yanray Wangb088bfc2023-03-16 12:15:49 +08001000 * to prevent a dead loop inside mbedtls_test_ssl_exchange_data(). */
Yanray Wange6afd912022-10-27 12:11:18 +08001001 return ret;
1002 } else if (expected_fragments == 1) {
1003 /* Used for TLS/DTLS and the message size lower than MFL */
1004 TEST_ASSERT(ret == buf_len ||
1005 ret == MBEDTLS_ERR_SSL_WANT_READ ||
1006 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
1007 } else {
1008 /* Used for TLS and the message size larger than MFL */
1009 TEST_ASSERT(expected_fragments > 1);
1010 TEST_ASSERT((ret >= 0 && ret <= buf_len) ||
1011 ret == MBEDTLS_ERR_SSL_WANT_READ ||
1012 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
1013 }
1014
1015 return 0;
1016
1017exit:
1018 /* Some of the tests failed */
1019 return -1;
1020}
1021
1022/*
1023 * Read application data and increase read counter and fragments counter
1024 * if necessary.
1025 */
Michael Schuster54300d42024-06-04 02:30:22 +02001026static int mbedtls_ssl_read_fragment(mbedtls_ssl_context *ssl,
Michael Schusterbd89b792024-06-04 02:41:10 +02001027 unsigned char *buf, int buf_len,
1028 int *read, int *fragments,
1029 const int expected_fragments)
Yanray Wange6afd912022-10-27 12:11:18 +08001030{
Agathiyan Bragadeesh93212652023-07-12 11:22:59 +01001031 int ret;
Yanray Wange6afd912022-10-27 12:11:18 +08001032 /* Verify that calling mbedtls_ssl_write with a NULL buffer and zero length is
1033 * a valid no-op for TLS connections. */
1034 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Gilles Peskine353eb332025-05-14 17:42:53 +02001035 TEST_EQUAL(mbedtls_ssl_read(ssl, NULL, 0), 0);
Yanray Wange6afd912022-10-27 12:11:18 +08001036 }
1037
Agathiyan Bragadeesh93212652023-07-12 11:22:59 +01001038 ret = mbedtls_ssl_read(ssl, buf + *read, buf_len - *read);
Yanray Wange6afd912022-10-27 12:11:18 +08001039 if (ret > 0) {
1040 (*fragments)++;
1041 *read += ret;
1042 }
1043
1044 if (expected_fragments == 0) {
Gilles Peskine353eb332025-05-14 17:42:53 +02001045 TEST_EQUAL(ret, 0);
Yanray Wange6afd912022-10-27 12:11:18 +08001046 } else if (expected_fragments == 1) {
1047 TEST_ASSERT(ret == buf_len ||
1048 ret == MBEDTLS_ERR_SSL_WANT_READ ||
1049 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
1050 } else {
1051 TEST_ASSERT(expected_fragments > 1);
1052 TEST_ASSERT((ret >= 0 && ret <= buf_len) ||
1053 ret == MBEDTLS_ERR_SSL_WANT_READ ||
1054 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
1055 }
1056
1057 return 0;
1058
1059exit:
1060 /* Some of the tests failed */
1061 return -1;
1062}
1063
Yanray Wangead70c82023-03-16 12:04:49 +08001064#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
1065static void set_ciphersuite(mbedtls_ssl_config *conf, const char *cipher,
1066 int *forced_ciphersuite)
Yanray Wange6afd912022-10-27 12:11:18 +08001067{
1068 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
1069 forced_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id(cipher);
1070 forced_ciphersuite[1] = 0;
1071
1072 ciphersuite_info =
1073 mbedtls_ssl_ciphersuite_from_id(forced_ciphersuite[0]);
1074
1075 TEST_ASSERT(ciphersuite_info != NULL);
1076 TEST_ASSERT(ciphersuite_info->min_tls_version <= conf->max_tls_version);
1077 TEST_ASSERT(ciphersuite_info->max_tls_version >= conf->min_tls_version);
1078
1079 if (conf->max_tls_version > ciphersuite_info->max_tls_version) {
Agathiyan Bragadeesh2f017a82023-07-12 11:21:54 +01001080 conf->max_tls_version = (mbedtls_ssl_protocol_version) ciphersuite_info->max_tls_version;
Yanray Wange6afd912022-10-27 12:11:18 +08001081 }
1082 if (conf->min_tls_version < ciphersuite_info->min_tls_version) {
Agathiyan Bragadeesh2f017a82023-07-12 11:21:54 +01001083 conf->min_tls_version = (mbedtls_ssl_protocol_version) ciphersuite_info->min_tls_version;
Yanray Wange6afd912022-10-27 12:11:18 +08001084 }
1085
1086 mbedtls_ssl_conf_ciphersuites(conf, forced_ciphersuite);
1087
1088exit:
1089 return;
1090}
Yanray Wangead70c82023-03-16 12:04:49 +08001091#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Yanray Wange6afd912022-10-27 12:11:18 +08001092
Yanray Wangead70c82023-03-16 12:04:49 +08001093#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) && \
1094 defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED) && \
1095 defined(MBEDTLS_SSL_SRV_C)
1096static int psk_dummy_callback(void *p_info, mbedtls_ssl_context *ssl,
1097 const unsigned char *name, size_t name_len)
Yanray Wange6afd912022-10-27 12:11:18 +08001098{
1099 (void) p_info;
1100 (void) ssl;
1101 (void) name;
1102 (void) name_len;
1103
1104 return 0;
1105}
Yanray Wangead70c82023-03-16 12:04:49 +08001106#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED &&
1107 MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED &&
1108 MBEDTLS_SSL_SRV_C */
Yanray Wange6afd912022-10-27 12:11:18 +08001109
1110#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Elena Uziunaite74342c72024-07-05 11:31:29 +01001111 defined(PSA_WANT_ALG_CBC_NO_PADDING) && defined(PSA_WANT_KEY_TYPE_AES)
Yanray Wange6afd912022-10-27 12:11:18 +08001112int mbedtls_test_psa_cipher_encrypt_helper(mbedtls_ssl_transform *transform,
1113 const unsigned char *iv,
1114 size_t iv_len,
1115 const unsigned char *input,
1116 size_t ilen,
1117 unsigned char *output,
1118 size_t *olen)
1119{
1120#if defined(MBEDTLS_USE_PSA_CRYPTO)
1121 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1122 psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT;
1123 size_t part_len;
1124
1125 status = psa_cipher_encrypt_setup(&cipher_op,
1126 transform->psa_key_enc,
1127 transform->psa_alg);
1128
1129 if (status != PSA_SUCCESS) {
1130 return PSA_TO_MBEDTLS_ERR(status);
1131 }
1132
1133 status = psa_cipher_set_iv(&cipher_op, iv, iv_len);
1134
1135 if (status != PSA_SUCCESS) {
1136 return PSA_TO_MBEDTLS_ERR(status);
1137 }
1138
1139 status = psa_cipher_update(&cipher_op, input, ilen, output, ilen, olen);
1140
1141 if (status != PSA_SUCCESS) {
1142 return PSA_TO_MBEDTLS_ERR(status);
1143 }
1144
1145 status = psa_cipher_finish(&cipher_op, output + *olen, ilen - *olen,
1146 &part_len);
1147
1148 if (status != PSA_SUCCESS) {
1149 return PSA_TO_MBEDTLS_ERR(status);
1150 }
1151
1152 *olen += part_len;
1153 return 0;
1154#else
1155 return mbedtls_cipher_crypt(&transform->cipher_ctx_enc,
1156 iv, iv_len, input, ilen, output, olen);
1157#endif /* MBEDTLS_USE_PSA_CRYPTO */
1158}
Elena Uziunaite74342c72024-07-05 11:31:29 +01001159#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && PSA_WANT_ALG_CBC_NO_PADDING &&
Elena Uziunaite6121a342024-07-05 11:16:53 +01001160 PSA_WANT_KEY_TYPE_AES */
Yanray Wange6afd912022-10-27 12:11:18 +08001161
Valerio Setti31ad3a12023-10-27 11:55:02 +02001162static void mbedtls_test_ssl_cipher_info_from_type(mbedtls_cipher_type_t cipher_type,
1163 mbedtls_cipher_mode_t *cipher_mode,
1164 size_t *key_bits, size_t *iv_len)
1165{
1166 switch (cipher_type) {
1167 case MBEDTLS_CIPHER_AES_128_CBC:
1168 *cipher_mode = MBEDTLS_MODE_CBC;
1169 *key_bits = 128;
1170 *iv_len = 16;
1171 break;
1172 case MBEDTLS_CIPHER_AES_256_CBC:
1173 *cipher_mode = MBEDTLS_MODE_CBC;
1174 *key_bits = 256;
1175 *iv_len = 16;
1176 break;
1177 case MBEDTLS_CIPHER_ARIA_128_CBC:
1178 *cipher_mode = MBEDTLS_MODE_CBC;
1179 *key_bits = 128;
1180 *iv_len = 16;
1181 break;
1182 case MBEDTLS_CIPHER_ARIA_256_CBC:
1183 *cipher_mode = MBEDTLS_MODE_CBC;
1184 *key_bits = 256;
1185 *iv_len = 16;
1186 break;
1187 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
1188 *cipher_mode = MBEDTLS_MODE_CBC;
1189 *key_bits = 128;
1190 *iv_len = 16;
1191 break;
1192 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
1193 *cipher_mode = MBEDTLS_MODE_CBC;
1194 *key_bits = 256;
1195 *iv_len = 16;
1196 break;
1197
1198 case MBEDTLS_CIPHER_AES_128_CCM:
1199 *cipher_mode = MBEDTLS_MODE_CCM;
1200 *key_bits = 128;
1201 *iv_len = 12;
1202 break;
1203 case MBEDTLS_CIPHER_AES_192_CCM:
1204 *cipher_mode = MBEDTLS_MODE_CCM;
1205 *key_bits = 192;
1206 *iv_len = 12;
1207 break;
1208 case MBEDTLS_CIPHER_AES_256_CCM:
1209 *cipher_mode = MBEDTLS_MODE_CCM;
1210 *key_bits = 256;
1211 *iv_len = 12;
1212 break;
1213 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
1214 *cipher_mode = MBEDTLS_MODE_CCM;
1215 *key_bits = 128;
1216 *iv_len = 12;
1217 break;
1218 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
1219 *cipher_mode = MBEDTLS_MODE_CCM;
1220 *key_bits = 192;
1221 *iv_len = 12;
1222 break;
1223 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
1224 *cipher_mode = MBEDTLS_MODE_CCM;
1225 *key_bits = 256;
1226 *iv_len = 12;
1227 break;
1228
1229 case MBEDTLS_CIPHER_AES_128_GCM:
1230 *cipher_mode = MBEDTLS_MODE_GCM;
1231 *key_bits = 128;
1232 *iv_len = 12;
1233 break;
1234 case MBEDTLS_CIPHER_AES_192_GCM:
1235 *cipher_mode = MBEDTLS_MODE_GCM;
1236 *key_bits = 192;
1237 *iv_len = 12;
1238 break;
1239 case MBEDTLS_CIPHER_AES_256_GCM:
1240 *cipher_mode = MBEDTLS_MODE_GCM;
1241 *key_bits = 256;
1242 *iv_len = 12;
1243 break;
1244 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
1245 *cipher_mode = MBEDTLS_MODE_GCM;
1246 *key_bits = 128;
1247 *iv_len = 12;
1248 break;
1249 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
1250 *cipher_mode = MBEDTLS_MODE_GCM;
1251 *key_bits = 192;
1252 *iv_len = 12;
1253 break;
1254 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
1255 *cipher_mode = MBEDTLS_MODE_GCM;
1256 *key_bits = 256;
1257 *iv_len = 12;
1258 break;
1259
1260 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
1261 *cipher_mode = MBEDTLS_MODE_CHACHAPOLY;
1262 *key_bits = 256;
1263 *iv_len = 12;
1264 break;
1265
1266 case MBEDTLS_CIPHER_NULL:
1267 *cipher_mode = MBEDTLS_MODE_STREAM;
1268 *key_bits = 0;
1269 *iv_len = 0;
1270 break;
1271
1272 default:
1273 *cipher_mode = MBEDTLS_MODE_NONE;
1274 *key_bits = 0;
1275 *iv_len = 0;
1276 }
1277}
1278
Yanray Wange6afd912022-10-27 12:11:18 +08001279int mbedtls_test_ssl_build_transforms(mbedtls_ssl_transform *t_in,
1280 mbedtls_ssl_transform *t_out,
1281 int cipher_type, int hash_id,
1282 int etm, int tag_mode,
1283 mbedtls_ssl_protocol_version tls_version,
1284 size_t cid0_len,
1285 size_t cid1_len)
1286{
Valerio Setti31ad3a12023-10-27 11:55:02 +02001287 mbedtls_cipher_mode_t cipher_mode = MBEDTLS_MODE_NONE;
1288 size_t key_bits = 0;
Yanray Wange6afd912022-10-27 12:11:18 +08001289 int ret = 0;
1290
1291#if defined(MBEDTLS_USE_PSA_CRYPTO)
1292 psa_key_type_t key_type;
1293 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1294 psa_algorithm_t alg;
Yanray Wange6afd912022-10-27 12:11:18 +08001295 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Valerio Setti3d59ebe2023-10-30 11:56:56 +01001296#else
Valerio Setti31ad3a12023-10-27 11:55:02 +02001297 mbedtls_cipher_info_t const *cipher_info;
Yanray Wange6afd912022-10-27 12:11:18 +08001298#endif
1299
Valerio Setti31ad3a12023-10-27 11:55:02 +02001300 size_t keylen, maclen, ivlen = 0;
Yanray Wange6afd912022-10-27 12:11:18 +08001301 unsigned char *key0 = NULL, *key1 = NULL;
1302 unsigned char *md0 = NULL, *md1 = NULL;
1303 unsigned char iv_enc[16], iv_dec[16];
1304
1305#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
1306 unsigned char cid0[SSL_CID_LEN_MIN];
1307 unsigned char cid1[SSL_CID_LEN_MIN];
1308
1309 mbedtls_test_rnd_std_rand(NULL, cid0, sizeof(cid0));
1310 mbedtls_test_rnd_std_rand(NULL, cid1, sizeof(cid1));
1311#else
1312 ((void) cid0_len);
1313 ((void) cid1_len);
1314#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
1315
1316 maclen = 0;
Valerio Setti31ad3a12023-10-27 11:55:02 +02001317 mbedtls_test_ssl_cipher_info_from_type((mbedtls_cipher_type_t) cipher_type,
1318 &cipher_mode, &key_bits, &ivlen);
Yanray Wange6afd912022-10-27 12:11:18 +08001319
1320 /* Pick keys */
Valerio Setti31ad3a12023-10-27 11:55:02 +02001321 keylen = key_bits / 8;
Yanray Wange6afd912022-10-27 12:11:18 +08001322 /* Allocate `keylen + 1` bytes to ensure that we get
1323 * a non-NULL pointers from `mbedtls_calloc` even if
1324 * `keylen == 0` in the case of the NULL cipher. */
1325 CHK((key0 = mbedtls_calloc(1, keylen + 1)) != NULL);
1326 CHK((key1 = mbedtls_calloc(1, keylen + 1)) != NULL);
1327 memset(key0, 0x1, keylen);
1328 memset(key1, 0x2, keylen);
1329
1330#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti31ad3a12023-10-27 11:55:02 +02001331 /* Pick cipher */
1332 cipher_info = mbedtls_cipher_info_from_type((mbedtls_cipher_type_t) cipher_type);
1333 CHK(cipher_info != NULL);
1334 CHK(mbedtls_cipher_info_get_iv_size(cipher_info) <= 16);
1335 CHK(mbedtls_cipher_info_get_key_bitlen(cipher_info) % 8 == 0);
Valerio Setti3d59ebe2023-10-30 11:56:56 +01001336
Yanray Wange6afd912022-10-27 12:11:18 +08001337 /* Setup cipher contexts */
1338 CHK(mbedtls_cipher_setup(&t_in->cipher_ctx_enc, cipher_info) == 0);
1339 CHK(mbedtls_cipher_setup(&t_in->cipher_ctx_dec, cipher_info) == 0);
1340 CHK(mbedtls_cipher_setup(&t_out->cipher_ctx_enc, cipher_info) == 0);
1341 CHK(mbedtls_cipher_setup(&t_out->cipher_ctx_dec, cipher_info) == 0);
1342
1343#if defined(MBEDTLS_CIPHER_MODE_CBC)
Valerio Setti31ad3a12023-10-27 11:55:02 +02001344 if (cipher_mode == MBEDTLS_MODE_CBC) {
Yanray Wange6afd912022-10-27 12:11:18 +08001345 CHK(mbedtls_cipher_set_padding_mode(&t_in->cipher_ctx_enc,
1346 MBEDTLS_PADDING_NONE) == 0);
1347 CHK(mbedtls_cipher_set_padding_mode(&t_in->cipher_ctx_dec,
1348 MBEDTLS_PADDING_NONE) == 0);
1349 CHK(mbedtls_cipher_set_padding_mode(&t_out->cipher_ctx_enc,
1350 MBEDTLS_PADDING_NONE) == 0);
1351 CHK(mbedtls_cipher_set_padding_mode(&t_out->cipher_ctx_dec,
1352 MBEDTLS_PADDING_NONE) == 0);
1353 }
1354#endif /* MBEDTLS_CIPHER_MODE_CBC */
1355
1356 CHK(mbedtls_cipher_setkey(&t_in->cipher_ctx_enc, key0,
Yanray Wanga8f445e2022-11-03 11:51:59 +08001357 (keylen << 3 > INT_MAX) ? INT_MAX : (int) keylen << 3,
1358 MBEDTLS_ENCRYPT)
1359 == 0);
Yanray Wange6afd912022-10-27 12:11:18 +08001360 CHK(mbedtls_cipher_setkey(&t_in->cipher_ctx_dec, key1,
Yanray Wanga8f445e2022-11-03 11:51:59 +08001361 (keylen << 3 > INT_MAX) ? INT_MAX : (int) keylen << 3,
1362 MBEDTLS_DECRYPT)
1363 == 0);
Yanray Wange6afd912022-10-27 12:11:18 +08001364 CHK(mbedtls_cipher_setkey(&t_out->cipher_ctx_enc, key1,
Yanray Wanga8f445e2022-11-03 11:51:59 +08001365 (keylen << 3 > INT_MAX) ? INT_MAX : (int) keylen << 3,
1366 MBEDTLS_ENCRYPT)
1367 == 0);
Yanray Wange6afd912022-10-27 12:11:18 +08001368 CHK(mbedtls_cipher_setkey(&t_out->cipher_ctx_dec, key0,
Yanray Wanga8f445e2022-11-03 11:51:59 +08001369 (keylen << 3 > INT_MAX) ? INT_MAX : (int) keylen << 3,
1370 MBEDTLS_DECRYPT)
1371 == 0);
Valerio Setti31ad3a12023-10-27 11:55:02 +02001372#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Yanray Wange6afd912022-10-27 12:11:18 +08001373
1374 /* Setup MAC contexts */
1375#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Valerio Setti31ad3a12023-10-27 11:55:02 +02001376 if (cipher_mode == MBEDTLS_MODE_CBC ||
1377 cipher_mode == MBEDTLS_MODE_STREAM) {
Yanray Wange6afd912022-10-27 12:11:18 +08001378#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Agathiyan Bragadeesh2f017a82023-07-12 11:21:54 +01001379 mbedtls_md_info_t const *md_info = mbedtls_md_info_from_type((mbedtls_md_type_t) hash_id);
Yanray Wange6afd912022-10-27 12:11:18 +08001380 CHK(md_info != NULL);
1381#endif
Agathiyan Bragadeesh2f017a82023-07-12 11:21:54 +01001382 maclen = mbedtls_md_get_size_from_type((mbedtls_md_type_t) hash_id);
Yanray Wange6afd912022-10-27 12:11:18 +08001383 CHK(maclen != 0);
1384 /* Pick hash keys */
1385 CHK((md0 = mbedtls_calloc(1, maclen)) != NULL);
1386 CHK((md1 = mbedtls_calloc(1, maclen)) != NULL);
1387 memset(md0, 0x5, maclen);
1388 memset(md1, 0x6, maclen);
1389
1390#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02001391 alg = mbedtls_md_psa_alg_from_type(hash_id);
Yanray Wange6afd912022-10-27 12:11:18 +08001392
1393 CHK(alg != 0);
1394
1395 t_out->psa_mac_alg = PSA_ALG_HMAC(alg);
1396 t_in->psa_mac_alg = PSA_ALG_HMAC(alg);
1397 t_in->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
1398 t_out->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
1399 t_in->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
1400 t_out->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
1401
1402 psa_reset_key_attributes(&attributes);
1403 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
1404 psa_set_key_algorithm(&attributes, PSA_ALG_HMAC(alg));
1405 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
1406
1407 CHK(psa_import_key(&attributes,
1408 md0, maclen,
1409 &t_in->psa_mac_enc) == PSA_SUCCESS);
1410
1411 CHK(psa_import_key(&attributes,
1412 md1, maclen,
1413 &t_out->psa_mac_enc) == PSA_SUCCESS);
1414
Valerio Setti31ad3a12023-10-27 11:55:02 +02001415 if (cipher_mode == MBEDTLS_MODE_STREAM ||
Yanray Wange6afd912022-10-27 12:11:18 +08001416 etm == MBEDTLS_SSL_ETM_DISABLED) {
1417 /* mbedtls_ct_hmac() requires the key to be exportable */
1418 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT |
1419 PSA_KEY_USAGE_VERIFY_HASH);
1420 } else {
1421 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
1422 }
1423
1424 CHK(psa_import_key(&attributes,
1425 md1, maclen,
1426 &t_in->psa_mac_dec) == PSA_SUCCESS);
1427
1428 CHK(psa_import_key(&attributes,
1429 md0, maclen,
1430 &t_out->psa_mac_dec) == PSA_SUCCESS);
1431#else
1432 CHK(mbedtls_md_setup(&t_out->md_ctx_enc, md_info, 1) == 0);
1433 CHK(mbedtls_md_setup(&t_out->md_ctx_dec, md_info, 1) == 0);
1434 CHK(mbedtls_md_setup(&t_in->md_ctx_enc, md_info, 1) == 0);
1435 CHK(mbedtls_md_setup(&t_in->md_ctx_dec, md_info, 1) == 0);
1436
1437 CHK(mbedtls_md_hmac_starts(&t_in->md_ctx_enc,
1438 md0, maclen) == 0);
1439 CHK(mbedtls_md_hmac_starts(&t_in->md_ctx_dec,
1440 md1, maclen) == 0);
1441 CHK(mbedtls_md_hmac_starts(&t_out->md_ctx_enc,
1442 md1, maclen) == 0);
1443 CHK(mbedtls_md_hmac_starts(&t_out->md_ctx_dec,
1444 md0, maclen) == 0);
1445#endif
1446 }
1447#else
1448 ((void) hash_id);
1449#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
1450
1451
1452 /* Pick IV's (regardless of whether they
1453 * are being used by the transform). */
Yanray Wange6afd912022-10-27 12:11:18 +08001454 memset(iv_enc, 0x3, sizeof(iv_enc));
1455 memset(iv_dec, 0x4, sizeof(iv_dec));
1456
1457 /*
1458 * Setup transforms
1459 */
1460
1461#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
1462 defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
1463 t_out->encrypt_then_mac = etm;
1464 t_in->encrypt_then_mac = etm;
1465#else
1466 ((void) etm);
1467#endif
1468
1469 t_out->tls_version = tls_version;
1470 t_in->tls_version = tls_version;
1471 t_out->ivlen = ivlen;
1472 t_in->ivlen = ivlen;
1473
Valerio Setti31ad3a12023-10-27 11:55:02 +02001474 switch (cipher_mode) {
Yanray Wange6afd912022-10-27 12:11:18 +08001475 case MBEDTLS_MODE_GCM:
1476 case MBEDTLS_MODE_CCM:
1477#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1478 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
1479 t_out->fixed_ivlen = 12;
1480 t_in->fixed_ivlen = 12;
1481 } else
1482#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1483 {
1484 t_out->fixed_ivlen = 4;
1485 t_in->fixed_ivlen = 4;
1486 }
1487 t_out->maclen = 0;
1488 t_in->maclen = 0;
1489 switch (tag_mode) {
1490 case 0: /* Full tag */
1491 t_out->taglen = 16;
1492 t_in->taglen = 16;
1493 break;
1494 case 1: /* Partial tag */
1495 t_out->taglen = 8;
1496 t_in->taglen = 8;
1497 break;
1498 default:
1499 ret = 1;
1500 goto cleanup;
1501 }
1502 break;
1503
1504 case MBEDTLS_MODE_CHACHAPOLY:
1505 t_out->fixed_ivlen = 12;
1506 t_in->fixed_ivlen = 12;
1507 t_out->maclen = 0;
1508 t_in->maclen = 0;
1509 switch (tag_mode) {
1510 case 0: /* Full tag */
1511 t_out->taglen = 16;
1512 t_in->taglen = 16;
1513 break;
1514 case 1: /* Partial tag */
1515 t_out->taglen = 8;
1516 t_in->taglen = 8;
1517 break;
1518 default:
1519 ret = 1;
1520 goto cleanup;
1521 }
1522 break;
1523
1524 case MBEDTLS_MODE_STREAM:
1525 case MBEDTLS_MODE_CBC:
1526 t_out->fixed_ivlen = 0; /* redundant, must be 0 */
1527 t_in->fixed_ivlen = 0; /* redundant, must be 0 */
1528 t_out->taglen = 0;
1529 t_in->taglen = 0;
1530 switch (tag_mode) {
1531 case 0: /* Full tag */
1532 t_out->maclen = maclen;
1533 t_in->maclen = maclen;
1534 break;
1535 default:
1536 ret = 1;
1537 goto cleanup;
1538 }
1539 break;
1540 default:
1541 ret = 1;
1542 goto cleanup;
1543 break;
1544 }
1545
1546 /* Setup IV's */
1547
1548 memcpy(&t_in->iv_dec, iv_dec, sizeof(iv_dec));
1549 memcpy(&t_in->iv_enc, iv_enc, sizeof(iv_enc));
1550 memcpy(&t_out->iv_dec, iv_enc, sizeof(iv_enc));
1551 memcpy(&t_out->iv_enc, iv_dec, sizeof(iv_dec));
1552
1553#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
1554 /* Add CID */
1555 memcpy(&t_in->in_cid, cid0, cid0_len);
1556 memcpy(&t_in->out_cid, cid1, cid1_len);
Yanray Wanga8f445e2022-11-03 11:51:59 +08001557 t_in->in_cid_len = (uint8_t) cid0_len;
1558 t_in->out_cid_len = (uint8_t) cid1_len;
Yanray Wange6afd912022-10-27 12:11:18 +08001559 memcpy(&t_out->in_cid, cid1, cid1_len);
1560 memcpy(&t_out->out_cid, cid0, cid0_len);
Yanray Wanga8f445e2022-11-03 11:51:59 +08001561 t_out->in_cid_len = (uint8_t) cid1_len;
1562 t_out->out_cid_len = (uint8_t) cid0_len;
Yanray Wange6afd912022-10-27 12:11:18 +08001563#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
1564
1565#if defined(MBEDTLS_USE_PSA_CRYPTO)
1566 status = mbedtls_ssl_cipher_to_psa(cipher_type,
1567 t_in->taglen,
1568 &alg,
1569 &key_type,
1570 &key_bits);
1571
1572 if (status != PSA_SUCCESS) {
1573 ret = PSA_TO_MBEDTLS_ERR(status);
1574 goto cleanup;
1575 }
1576
1577 t_in->psa_alg = alg;
1578 t_out->psa_alg = alg;
1579
1580 if (alg != MBEDTLS_SSL_NULL_CIPHER) {
1581 psa_reset_key_attributes(&attributes);
1582 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
1583 psa_set_key_algorithm(&attributes, alg);
1584 psa_set_key_type(&attributes, key_type);
1585
1586 status = psa_import_key(&attributes,
1587 key0,
1588 PSA_BITS_TO_BYTES(key_bits),
1589 &t_in->psa_key_enc);
1590
1591 if (status != PSA_SUCCESS) {
1592 ret = PSA_TO_MBEDTLS_ERR(status);
1593 goto cleanup;
1594 }
1595
1596 status = psa_import_key(&attributes,
1597 key1,
1598 PSA_BITS_TO_BYTES(key_bits),
1599 &t_out->psa_key_enc);
1600
1601 if (status != PSA_SUCCESS) {
1602 ret = PSA_TO_MBEDTLS_ERR(status);
1603 goto cleanup;
1604 }
1605
1606 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
1607
1608 status = psa_import_key(&attributes,
1609 key1,
1610 PSA_BITS_TO_BYTES(key_bits),
1611 &t_in->psa_key_dec);
1612
1613 if (status != PSA_SUCCESS) {
1614 ret = PSA_TO_MBEDTLS_ERR(status);
1615 goto cleanup;
1616 }
1617
1618 status = psa_import_key(&attributes,
1619 key0,
1620 PSA_BITS_TO_BYTES(key_bits),
1621 &t_out->psa_key_dec);
1622
1623 if (status != PSA_SUCCESS) {
1624 ret = PSA_TO_MBEDTLS_ERR(status);
1625 goto cleanup;
1626 }
1627 }
1628#endif /* MBEDTLS_USE_PSA_CRYPTO */
1629
1630cleanup:
1631
1632 mbedtls_free(key0);
1633 mbedtls_free(key1);
1634
1635 mbedtls_free(md0);
1636 mbedtls_free(md1);
1637
1638 return ret;
1639}
1640
Gilles Peskine9099d3f2023-09-18 13:11:50 +02001641#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
1642int mbedtls_test_ssl_prepare_record_mac(mbedtls_record *record,
1643 mbedtls_ssl_transform *transform_out)
1644{
1645#if defined(MBEDTLS_USE_PSA_CRYPTO)
1646 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1647#endif
1648
1649 /* Serialized version of record header for MAC purposes */
1650 unsigned char add_data[13];
1651 memcpy(add_data, record->ctr, 8);
1652 add_data[8] = record->type;
1653 add_data[9] = record->ver[0];
1654 add_data[10] = record->ver[1];
1655 add_data[11] = (record->data_len >> 8) & 0xff;
1656 add_data[12] = (record->data_len >> 0) & 0xff;
1657
1658 /* MAC with additional data */
1659#if defined(MBEDTLS_USE_PSA_CRYPTO)
1660 size_t sign_mac_length = 0;
1661 TEST_EQUAL(PSA_SUCCESS, psa_mac_sign_setup(&operation,
1662 transform_out->psa_mac_enc,
1663 transform_out->psa_mac_alg));
1664 TEST_EQUAL(PSA_SUCCESS, psa_mac_update(&operation, add_data, 13));
1665 TEST_EQUAL(PSA_SUCCESS, psa_mac_update(&operation,
1666 record->buf + record->data_offset,
1667 record->data_len));
1668 /* Use a temporary buffer for the MAC, because with the truncated HMAC
1669 * extension, there might not be enough room in the record for the
1670 * full-length MAC. */
1671 unsigned char mac[PSA_HASH_MAX_SIZE];
1672 TEST_EQUAL(PSA_SUCCESS, psa_mac_sign_finish(&operation,
1673 mac, sizeof(mac),
1674 &sign_mac_length));
1675#else
1676 TEST_EQUAL(0, mbedtls_md_hmac_update(&transform_out->md_ctx_enc, add_data, 13));
1677 TEST_EQUAL(0, mbedtls_md_hmac_update(&transform_out->md_ctx_enc,
1678 record->buf + record->data_offset,
1679 record->data_len));
1680 /* Use a temporary buffer for the MAC, because with the truncated HMAC
1681 * extension, there might not be enough room in the record for the
1682 * full-length MAC. */
1683 unsigned char mac[MBEDTLS_MD_MAX_SIZE];
1684 TEST_EQUAL(0, mbedtls_md_hmac_finish(&transform_out->md_ctx_enc, mac));
1685#endif
1686 memcpy(record->buf + record->data_offset + record->data_len, mac, transform_out->maclen);
1687 record->data_len += transform_out->maclen;
1688
1689 return 0;
1690
1691exit:
1692#if defined(MBEDTLS_USE_PSA_CRYPTO)
1693 psa_mac_abort(&operation);
1694#endif
1695 return -1;
1696}
1697#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
1698
Jerry Yu28547c42023-10-31 14:42:50 +08001699#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Yanray Wange6afd912022-10-27 12:11:18 +08001700int mbedtls_test_ssl_tls12_populate_session(mbedtls_ssl_session *session,
1701 int ticket_len,
Ronald Cron7b1921a2023-11-23 12:31:56 +01001702 int endpoint_type,
Yanray Wange6afd912022-10-27 12:11:18 +08001703 const char *crt_file)
1704{
Ronald Cronc57f86e2023-11-22 09:50:01 +01001705 (void) ticket_len;
1706
Yanray Wange6afd912022-10-27 12:11:18 +08001707#if defined(MBEDTLS_HAVE_TIME)
1708 session->start = mbedtls_time(NULL) - 42;
1709#endif
1710 session->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Ronald Cronc7fa82e2024-02-09 09:33:09 +01001711
1712 TEST_ASSERT(endpoint_type == MBEDTLS_SSL_IS_CLIENT ||
1713 endpoint_type == MBEDTLS_SSL_IS_SERVER);
1714
1715 session->endpoint = endpoint_type;
Yanray Wange6afd912022-10-27 12:11:18 +08001716 session->ciphersuite = 0xabcd;
1717 session->id_len = sizeof(session->id);
1718 memset(session->id, 66, session->id_len);
1719 memset(session->master, 17, sizeof(session->master));
1720
1721#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) && defined(MBEDTLS_FS_IO)
1722 if (crt_file != NULL && strlen(crt_file) != 0) {
1723 mbedtls_x509_crt tmp_crt;
1724 int ret;
1725
1726 mbedtls_x509_crt_init(&tmp_crt);
1727 ret = mbedtls_x509_crt_parse_file(&tmp_crt, crt_file);
1728 if (ret != 0) {
1729 return ret;
1730 }
1731
1732#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1733 /* Move temporary CRT. */
1734 session->peer_cert = mbedtls_calloc(1, sizeof(*session->peer_cert));
1735 if (session->peer_cert == NULL) {
1736 return -1;
1737 }
1738 *session->peer_cert = tmp_crt;
1739 memset(&tmp_crt, 0, sizeof(tmp_crt));
1740#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
1741 /* Calculate digest of temporary CRT. */
1742 session->peer_cert_digest =
1743 mbedtls_calloc(1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN);
1744 if (session->peer_cert_digest == NULL) {
1745 return -1;
1746 }
1747
1748#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02001749 psa_algorithm_t psa_alg = mbedtls_md_psa_alg_from_type(
Yanray Wange6afd912022-10-27 12:11:18 +08001750 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE);
1751 size_t hash_size = 0;
1752 psa_status_t status = psa_hash_compute(
1753 psa_alg, tmp_crt.raw.p,
1754 tmp_crt.raw.len,
1755 session->peer_cert_digest,
1756 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN,
1757 &hash_size);
1758 ret = PSA_TO_MBEDTLS_ERR(status);
1759#else
1760 ret = mbedtls_md(mbedtls_md_info_from_type(
1761 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE),
1762 tmp_crt.raw.p, tmp_crt.raw.len,
1763 session->peer_cert_digest);
1764#endif /* MBEDTLS_USE_PSA_CRYPTO */
1765 if (ret != 0) {
1766 return ret;
1767 }
1768 session->peer_cert_digest_type =
1769 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
1770 session->peer_cert_digest_len =
1771 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
1772#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
1773
1774 mbedtls_x509_crt_free(&tmp_crt);
1775 }
1776#else /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED && MBEDTLS_FS_IO */
1777 (void) crt_file;
1778#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED && MBEDTLS_FS_IO */
1779 session->verify_result = 0xdeadbeef;
1780
Ronald Cronc57f86e2023-11-22 09:50:01 +01001781#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1782#if defined(MBEDTLS_SSL_CLI_C)
Yanray Wange6afd912022-10-27 12:11:18 +08001783 if (ticket_len != 0) {
1784 session->ticket = mbedtls_calloc(1, ticket_len);
1785 if (session->ticket == NULL) {
1786 return -1;
1787 }
1788 memset(session->ticket, 33, ticket_len);
1789 }
1790 session->ticket_len = ticket_len;
1791 session->ticket_lifetime = 86401;
Ronald Cronc57f86e2023-11-22 09:50:01 +01001792#endif /* MBEDTLS_SSL_CLI_C */
1793
1794#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_HAVE_TIME)
1795 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
1796 session->ticket_creation_time = mbedtls_ms_time() - 42;
1797 }
Yanray Wange6afd912022-10-27 12:11:18 +08001798#endif
Ronald Cronc57f86e2023-11-22 09:50:01 +01001799#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Yanray Wange6afd912022-10-27 12:11:18 +08001800
1801#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1802 session->mfl_code = 1;
1803#endif
1804#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1805 session->encrypt_then_mac = 1;
1806#endif
1807
Ronald Cronc7fa82e2024-02-09 09:33:09 +01001808exit:
Yanray Wange6afd912022-10-27 12:11:18 +08001809 return 0;
1810}
Jerry Yu28547c42023-10-31 14:42:50 +08001811#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Yanray Wange6afd912022-10-27 12:11:18 +08001812
1813#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1814int mbedtls_test_ssl_tls13_populate_session(mbedtls_ssl_session *session,
1815 int ticket_len,
1816 int endpoint_type)
1817{
1818 ((void) ticket_len);
1819 session->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
1820 session->endpoint = endpoint_type == MBEDTLS_SSL_IS_CLIENT ?
1821 MBEDTLS_SSL_IS_CLIENT : MBEDTLS_SSL_IS_SERVER;
1822 session->ciphersuite = 0xabcd;
Ronald Cron18b92a12024-03-26 10:15:08 +01001823
1824#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Yanray Wange6afd912022-10-27 12:11:18 +08001825 session->ticket_age_add = 0x87654321;
1826 session->ticket_flags = 0x7;
Yanray Wange6afd912022-10-27 12:11:18 +08001827 session->resumption_key_len = 32;
1828 memset(session->resumption_key, 0x99, sizeof(session->resumption_key));
Yanray Wange6afd912022-10-27 12:11:18 +08001829#endif
1830
Ronald Cron18b92a12024-03-26 10:15:08 +01001831#if defined(MBEDTLS_SSL_SRV_C)
1832 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
1833#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1834#if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN)
1835 int ret = mbedtls_ssl_session_set_ticket_alpn(session, "ALPNExample");
1836 if (ret != 0) {
1837 return -1;
1838 }
1839#endif
1840#if defined(MBEDTLS_HAVE_TIME)
1841 session->ticket_creation_time = mbedtls_ms_time() - 42;
1842#endif
1843#endif /* MBEDTLS_SSL_SESSION_TICKETS */
1844 }
1845#endif /* MBEDTLS_SSL_SRV_C */
1846
Yanray Wange6afd912022-10-27 12:11:18 +08001847#if defined(MBEDTLS_SSL_CLI_C)
1848 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Ronald Cron18b92a12024-03-26 10:15:08 +01001849#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Yanray Wange6afd912022-10-27 12:11:18 +08001850#if defined(MBEDTLS_HAVE_TIME)
Jerry Yu342a5552023-11-10 14:23:39 +08001851 session->ticket_reception_time = mbedtls_ms_time() - 40;
Yanray Wange6afd912022-10-27 12:11:18 +08001852#endif
1853 session->ticket_lifetime = 0xfedcba98;
1854
1855 session->ticket_len = ticket_len;
1856 if (ticket_len != 0) {
1857 session->ticket = mbedtls_calloc(1, ticket_len);
1858 if (session->ticket == NULL) {
1859 return -1;
1860 }
1861 memset(session->ticket, 33, ticket_len);
1862 }
Ronald Cron8d15e012024-03-27 09:30:13 +01001863#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1864 char hostname[] = "hostname example";
1865 session->hostname = mbedtls_calloc(1, sizeof(hostname));
1866 if (session->hostname == NULL) {
1867 return -1;
1868 }
1869 memcpy(session->hostname, hostname, sizeof(hostname));
1870#endif
Ronald Cron18b92a12024-03-26 10:15:08 +01001871#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Yanray Wange6afd912022-10-27 12:11:18 +08001872 }
1873#endif /* MBEDTLS_SSL_CLI_C */
1874
Ronald Cron18b92a12024-03-26 10:15:08 +01001875#if defined(MBEDTLS_SSL_EARLY_DATA)
1876 session->max_early_data_size = 0x87654321;
1877#endif /* MBEDTLS_SSL_EARLY_DATA */
1878
Waleed Elmelegy049cd302023-12-20 17:28:31 +00001879#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
1880 session->record_size_limit = 2048;
1881#endif
1882
Yanray Wange6afd912022-10-27 12:11:18 +08001883 return 0;
1884}
1885#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1886
Yanray Wangb088bfc2023-03-16 12:15:49 +08001887int mbedtls_test_ssl_exchange_data(
1888 mbedtls_ssl_context *ssl_1,
1889 int msg_len_1, const int expected_fragments_1,
1890 mbedtls_ssl_context *ssl_2,
1891 int msg_len_2, const int expected_fragments_2)
Yanray Wange6afd912022-10-27 12:11:18 +08001892{
1893 unsigned char *msg_buf_1 = malloc(msg_len_1);
1894 unsigned char *msg_buf_2 = malloc(msg_len_2);
1895 unsigned char *in_buf_1 = malloc(msg_len_2);
1896 unsigned char *in_buf_2 = malloc(msg_len_1);
1897 int msg_type, ret = -1;
1898
1899 /* Perform this test with two message types. At first use a message
1900 * consisting of only 0x00 for the client and only 0xFF for the server.
1901 * At the second time use message with generated data */
1902 for (msg_type = 0; msg_type < 2; msg_type++) {
1903 int written_1 = 0;
1904 int written_2 = 0;
1905 int read_1 = 0;
1906 int read_2 = 0;
1907 int fragments_1 = 0;
1908 int fragments_2 = 0;
1909
1910 if (msg_type == 0) {
1911 memset(msg_buf_1, 0x00, msg_len_1);
1912 memset(msg_buf_2, 0xff, msg_len_2);
1913 } else {
1914 int i, j = 0;
1915 for (i = 0; i < msg_len_1; i++) {
1916 msg_buf_1[i] = j++ & 0xFF;
1917 }
1918 for (i = 0; i < msg_len_2; i++) {
1919 msg_buf_2[i] = (j -= 5) & 0xFF;
1920 }
1921 }
1922
1923 while (read_1 < msg_len_2 || read_2 < msg_len_1) {
1924 /* ssl_1 sending */
1925 if (msg_len_1 > written_1) {
1926 ret = mbedtls_ssl_write_fragment(ssl_1, msg_buf_1,
1927 msg_len_1, &written_1,
1928 expected_fragments_1);
1929 if (expected_fragments_1 == 0) {
1930 /* This error is expected when the message is too large and
1931 * cannot be fragmented */
Gilles Peskine353eb332025-05-14 17:42:53 +02001932 TEST_EQUAL(ret, MBEDTLS_ERR_SSL_BAD_INPUT_DATA);
Yanray Wange6afd912022-10-27 12:11:18 +08001933 msg_len_1 = 0;
1934 } else {
Gilles Peskine353eb332025-05-14 17:42:53 +02001935 TEST_EQUAL(ret, 0);
Yanray Wange6afd912022-10-27 12:11:18 +08001936 }
1937 }
1938
1939 /* ssl_2 sending */
1940 if (msg_len_2 > written_2) {
1941 ret = mbedtls_ssl_write_fragment(ssl_2, msg_buf_2,
1942 msg_len_2, &written_2,
1943 expected_fragments_2);
1944 if (expected_fragments_2 == 0) {
1945 /* This error is expected when the message is too large and
1946 * cannot be fragmented */
Gilles Peskine353eb332025-05-14 17:42:53 +02001947 TEST_EQUAL(ret, MBEDTLS_ERR_SSL_BAD_INPUT_DATA);
Yanray Wange6afd912022-10-27 12:11:18 +08001948 msg_len_2 = 0;
1949 } else {
Gilles Peskine353eb332025-05-14 17:42:53 +02001950 TEST_EQUAL(ret, 0);
Yanray Wange6afd912022-10-27 12:11:18 +08001951 }
1952 }
1953
1954 /* ssl_1 reading */
1955 if (read_1 < msg_len_2) {
1956 ret = mbedtls_ssl_read_fragment(ssl_1, in_buf_1,
1957 msg_len_2, &read_1,
1958 &fragments_2,
1959 expected_fragments_2);
Gilles Peskine353eb332025-05-14 17:42:53 +02001960 TEST_EQUAL(ret, 0);
Yanray Wange6afd912022-10-27 12:11:18 +08001961 }
1962
1963 /* ssl_2 reading */
1964 if (read_2 < msg_len_1) {
1965 ret = mbedtls_ssl_read_fragment(ssl_2, in_buf_2,
1966 msg_len_1, &read_2,
1967 &fragments_1,
1968 expected_fragments_1);
Gilles Peskine353eb332025-05-14 17:42:53 +02001969 TEST_EQUAL(ret, 0);
Yanray Wange6afd912022-10-27 12:11:18 +08001970 }
1971 }
1972
1973 ret = -1;
Gilles Peskine353eb332025-05-14 17:42:53 +02001974 TEST_EQUAL(0, memcmp(msg_buf_1, in_buf_2, msg_len_1));
1975 TEST_EQUAL(0, memcmp(msg_buf_2, in_buf_1, msg_len_2));
1976 TEST_EQUAL(fragments_1, expected_fragments_1);
1977 TEST_EQUAL(fragments_2, expected_fragments_2);
Yanray Wange6afd912022-10-27 12:11:18 +08001978 }
1979
1980 ret = 0;
1981
1982exit:
1983 free(msg_buf_1);
1984 free(in_buf_1);
1985 free(msg_buf_2);
1986 free(in_buf_2);
1987
1988 return ret;
1989}
1990
1991/*
1992 * Perform data exchanging between \p ssl_1 and \p ssl_2. Both of endpoints
1993 * must be initialized and connected beforehand.
1994 *
1995 * \retval 0 on success, otherwise error code.
1996 */
Yanray Wangead70c82023-03-16 12:04:49 +08001997#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) && \
1998 (defined(MBEDTLS_SSL_RENEGOTIATION) || \
1999 defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH))
2000static int exchange_data(mbedtls_ssl_context *ssl_1,
2001 mbedtls_ssl_context *ssl_2)
Yanray Wange6afd912022-10-27 12:11:18 +08002002{
Yanray Wangb088bfc2023-03-16 12:15:49 +08002003 return mbedtls_test_ssl_exchange_data(ssl_1, 256, 1,
2004 ssl_2, 256, 1);
Yanray Wange6afd912022-10-27 12:11:18 +08002005}
Yanray Wangead70c82023-03-16 12:04:49 +08002006#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED &&
2007 (MBEDTLS_SSL_RENEGOTIATION ||
2008 MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) */
Yanray Wange6afd912022-10-27 12:11:18 +08002009
2010#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
2011static int check_ssl_version(
2012 mbedtls_ssl_protocol_version expected_negotiated_version,
2013 const mbedtls_ssl_context *ssl)
2014{
2015 const char *version_string = mbedtls_ssl_get_version(ssl);
2016 mbedtls_ssl_protocol_version version_number =
2017 mbedtls_ssl_get_version_number(ssl);
2018
2019 TEST_EQUAL(ssl->tls_version, expected_negotiated_version);
2020
2021 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
2022 TEST_EQUAL(version_string[0], 'D');
2023 ++version_string;
2024 }
2025
2026 switch (expected_negotiated_version) {
2027 case MBEDTLS_SSL_VERSION_TLS1_2:
2028 TEST_EQUAL(version_number, MBEDTLS_SSL_VERSION_TLS1_2);
Gilles Peskine353eb332025-05-14 17:42:53 +02002029 TEST_EQUAL(strcmp(version_string, "TLSv1.2"), 0);
Yanray Wange6afd912022-10-27 12:11:18 +08002030 break;
2031
2032 case MBEDTLS_SSL_VERSION_TLS1_3:
2033 TEST_EQUAL(version_number, MBEDTLS_SSL_VERSION_TLS1_3);
Gilles Peskine353eb332025-05-14 17:42:53 +02002034 TEST_EQUAL(strcmp(version_string, "TLSv1.3"), 0);
Yanray Wange6afd912022-10-27 12:11:18 +08002035 break;
2036
2037 default:
Agathiyan Bragadeeshdc28a5a2023-07-18 11:45:28 +01002038 TEST_FAIL(
Agathiyan Bragadeeshebb40bc2023-07-14 17:28:27 +01002039 "Version check not implemented for this protocol version");
Yanray Wange6afd912022-10-27 12:11:18 +08002040 }
2041
2042 return 1;
2043
2044exit:
2045 return 0;
2046}
2047#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
2048
Yanray Wange6afd912022-10-27 12:11:18 +08002049#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Max Fillingercf007ca2024-10-29 16:57:09 +01002050int mbedtls_test_ssl_do_handshake_with_endpoints(
2051 mbedtls_test_ssl_endpoint *server_ep,
2052 mbedtls_test_ssl_endpoint *client_ep,
Max Fillinger8f12e312024-10-30 00:29:37 +01002053 mbedtls_test_handshake_test_options *options,
Max Fillingercf007ca2024-10-29 16:57:09 +01002054 mbedtls_ssl_protocol_version proto)
2055{
2056 enum { BUFFSIZE = 1024 };
2057
2058 int ret = -1;
Max Fillingercf007ca2024-10-29 16:57:09 +01002059
Max Fillingeree467aa2024-11-08 22:17:33 +01002060 mbedtls_platform_zeroize(server_ep, sizeof(mbedtls_test_ssl_endpoint));
2061 mbedtls_platform_zeroize(client_ep, sizeof(mbedtls_test_ssl_endpoint));
2062
Max Fillinger8f12e312024-10-30 00:29:37 +01002063 mbedtls_test_init_handshake_options(options);
2064 options->server_min_version = proto;
2065 options->client_min_version = proto;
2066 options->server_max_version = proto;
2067 options->client_max_version = proto;
Max Fillingercf007ca2024-10-29 16:57:09 +01002068
Max Fillinger8f12e312024-10-30 00:29:37 +01002069 ret = mbedtls_test_ssl_endpoint_init(client_ep, MBEDTLS_SSL_IS_CLIENT, options,
Max Fillingercf007ca2024-10-29 16:57:09 +01002070 NULL, NULL, NULL);
2071 if (ret != 0) {
2072 return ret;
2073 }
Max Fillinger8f12e312024-10-30 00:29:37 +01002074 ret = mbedtls_test_ssl_endpoint_init(server_ep, MBEDTLS_SSL_IS_SERVER, options,
Max Fillingercf007ca2024-10-29 16:57:09 +01002075 NULL, NULL, NULL);
2076 if (ret != 0) {
2077 return ret;
2078 }
2079
2080 ret = mbedtls_test_mock_socket_connect(&client_ep->socket, &server_ep->socket, BUFFSIZE);
2081 if (ret != 0) {
2082 return ret;
2083 }
2084
Max Fillingerea1e7772024-10-30 00:49:10 +01002085 ret = mbedtls_test_move_handshake_to_state(&server_ep->ssl,
2086 &client_ep->ssl,
2087 MBEDTLS_SSL_HANDSHAKE_OVER);
Max Fillingercf007ca2024-10-29 16:57:09 +01002088 if (ret != 0 && ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
2089 return ret;
2090 }
Max Fillingerea1e7772024-10-30 00:49:10 +01002091 ret = mbedtls_test_move_handshake_to_state(&client_ep->ssl,
2092 &server_ep->ssl,
2093 MBEDTLS_SSL_HANDSHAKE_OVER);
Max Fillingercf007ca2024-10-29 16:57:09 +01002094 if (ret != 0 && ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
2095 return ret;
2096 }
Max Fillingerea1e7772024-10-30 00:49:10 +01002097 if (!mbedtls_ssl_is_handshake_over(&client_ep->ssl) ||
2098 !mbedtls_ssl_is_handshake_over(&server_ep->ssl)) {
Max Fillingercf007ca2024-10-29 16:57:09 +01002099 return -1;
2100 }
2101
2102 return 0;
2103}
2104#endif /* defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) */
2105
2106#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Yanray Wange6afd912022-10-27 12:11:18 +08002107void mbedtls_test_ssl_perform_handshake(
2108 mbedtls_test_handshake_test_options *options)
2109{
2110 /* forced_ciphersuite needs to last until the end of the handshake */
2111 int forced_ciphersuite[2];
2112 enum { BUFFSIZE = 17000 };
2113 mbedtls_test_ssl_endpoint client, server;
2114#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
2115 const char *psk_identity = "foo";
2116#endif
2117#if defined(MBEDTLS_TIMING_C)
2118 mbedtls_timing_delay_context timer_client, timer_server;
2119#endif
2120#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
2121 unsigned char *context_buf = NULL;
2122 size_t context_buf_len;
2123#endif
2124#if defined(MBEDTLS_SSL_RENEGOTIATION)
2125 int ret = -1;
2126#endif
2127 int expected_handshake_result = options->expected_handshake_result;
2128
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +01002129 MD_OR_USE_PSA_INIT();
Yanray Wange6afd912022-10-27 12:11:18 +08002130 mbedtls_platform_zeroize(&client, sizeof(client));
2131 mbedtls_platform_zeroize(&server, sizeof(server));
2132 mbedtls_test_ssl_message_queue server_queue, client_queue;
2133 mbedtls_test_message_socket_context server_context, client_context;
2134 mbedtls_test_message_socket_init(&server_context);
2135 mbedtls_test_message_socket_init(&client_context);
2136
Ronald Cronec3408d2024-01-16 17:50:40 +01002137#if defined(MBEDTLS_DEBUG_C)
2138 if (options->cli_log_fun || options->srv_log_fun) {
2139 mbedtls_debug_set_threshold(4);
2140 }
2141#endif
2142
Yanray Wange6afd912022-10-27 12:11:18 +08002143 /* Client side */
2144 if (options->dtls != 0) {
Gilles Peskine353eb332025-05-14 17:42:53 +02002145 TEST_EQUAL(mbedtls_test_ssl_endpoint_init(&client,
2146 MBEDTLS_SSL_IS_CLIENT,
2147 options, &client_context,
2148 &client_queue,
2149 &server_queue), 0);
Yanray Wange6afd912022-10-27 12:11:18 +08002150#if defined(MBEDTLS_TIMING_C)
2151 mbedtls_ssl_set_timer_cb(&client.ssl, &timer_client,
2152 mbedtls_timing_set_delay,
2153 mbedtls_timing_get_delay);
2154#endif
2155 } else {
Gilles Peskine353eb332025-05-14 17:42:53 +02002156 TEST_EQUAL(mbedtls_test_ssl_endpoint_init(&client,
2157 MBEDTLS_SSL_IS_CLIENT,
2158 options, NULL, NULL,
2159 NULL), 0);
Yanray Wange6afd912022-10-27 12:11:18 +08002160 }
2161
Yanray Wange6afd912022-10-27 12:11:18 +08002162 if (strlen(options->cipher) > 0) {
2163 set_ciphersuite(&client.conf, options->cipher, forced_ciphersuite);
2164 }
2165
Yanray Wange6afd912022-10-27 12:11:18 +08002166 /* Server side */
2167 if (options->dtls != 0) {
Gilles Peskine353eb332025-05-14 17:42:53 +02002168 TEST_EQUAL(mbedtls_test_ssl_endpoint_init(&server,
2169 MBEDTLS_SSL_IS_SERVER,
2170 options, &server_context,
2171 &server_queue,
2172 &client_queue), 0);
Yanray Wange6afd912022-10-27 12:11:18 +08002173#if defined(MBEDTLS_TIMING_C)
2174 mbedtls_ssl_set_timer_cb(&server.ssl, &timer_server,
2175 mbedtls_timing_set_delay,
2176 mbedtls_timing_get_delay);
2177#endif
2178 } else {
Gilles Peskine353eb332025-05-14 17:42:53 +02002179 TEST_EQUAL(mbedtls_test_ssl_endpoint_init(&server,
2180 MBEDTLS_SSL_IS_SERVER,
2181 options, NULL, NULL,
2182 NULL), 0);
Yanray Wange6afd912022-10-27 12:11:18 +08002183 }
2184
2185 mbedtls_ssl_conf_authmode(&server.conf, options->srv_auth_mode);
2186
Yanray Wange6afd912022-10-27 12:11:18 +08002187#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine353eb332025-05-14 17:42:53 +02002188 TEST_EQUAL(mbedtls_ssl_conf_max_frag_len(&(server.conf),
2189 (unsigned char) options->mfl),
2190 0);
2191 TEST_EQUAL(mbedtls_ssl_conf_max_frag_len(&(client.conf),
2192 (unsigned char) options->mfl),
2193 0);
Yanray Wange6afd912022-10-27 12:11:18 +08002194#else
Gilles Peskine353eb332025-05-14 17:42:53 +02002195 TEST_EQUAL(MBEDTLS_SSL_MAX_FRAG_LEN_NONE, options->mfl);
Yanray Wange6afd912022-10-27 12:11:18 +08002196#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2197
2198#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
2199 if (options->psk_str != NULL && options->psk_str->len > 0) {
Gilles Peskine353eb332025-05-14 17:42:53 +02002200 TEST_EQUAL(mbedtls_ssl_conf_psk(
2201 &client.conf, options->psk_str->x,
2202 options->psk_str->len,
2203 (const unsigned char *) psk_identity,
2204 strlen(psk_identity)), 0);
Yanray Wange6afd912022-10-27 12:11:18 +08002205
Gilles Peskine353eb332025-05-14 17:42:53 +02002206 TEST_EQUAL(mbedtls_ssl_conf_psk(
2207 &server.conf, options->psk_str->x,
2208 options->psk_str->len,
2209 (const unsigned char *) psk_identity,
2210 strlen(psk_identity)), 0);
Yanray Wange6afd912022-10-27 12:11:18 +08002211#if defined(MBEDTLS_SSL_SRV_C)
2212 mbedtls_ssl_conf_psk_cb(&server.conf, psk_dummy_callback, NULL);
2213#endif
2214 }
2215#endif
2216#if defined(MBEDTLS_SSL_RENEGOTIATION)
2217 if (options->renegotiate) {
2218 mbedtls_ssl_conf_renegotiation(&(server.conf),
2219 MBEDTLS_SSL_RENEGOTIATION_ENABLED);
2220 mbedtls_ssl_conf_renegotiation(&(client.conf),
2221 MBEDTLS_SSL_RENEGOTIATION_ENABLED);
2222
2223 mbedtls_ssl_conf_legacy_renegotiation(&(server.conf),
2224 options->legacy_renegotiation);
2225 mbedtls_ssl_conf_legacy_renegotiation(&(client.conf),
2226 options->legacy_renegotiation);
2227 }
2228#endif /* MBEDTLS_SSL_RENEGOTIATION */
2229
Gilles Peskine353eb332025-05-14 17:42:53 +02002230 TEST_EQUAL(mbedtls_test_mock_socket_connect(&(client.socket),
2231 &(server.socket),
2232 BUFFSIZE), 0);
Yanray Wange6afd912022-10-27 12:11:18 +08002233
2234#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2235 if (options->resize_buffers != 0) {
2236 /* Ensure that the buffer sizes are appropriate before resizes */
Gilles Peskine353eb332025-05-14 17:42:53 +02002237 TEST_EQUAL(client.ssl.out_buf_len, MBEDTLS_SSL_OUT_BUFFER_LEN);
2238 TEST_EQUAL(client.ssl.in_buf_len, MBEDTLS_SSL_IN_BUFFER_LEN);
2239 TEST_EQUAL(server.ssl.out_buf_len, MBEDTLS_SSL_OUT_BUFFER_LEN);
2240 TEST_EQUAL(server.ssl.in_buf_len, MBEDTLS_SSL_IN_BUFFER_LEN);
Yanray Wange6afd912022-10-27 12:11:18 +08002241 }
2242#endif
2243
2244 if (options->expected_negotiated_version == MBEDTLS_SSL_VERSION_UNKNOWN) {
2245 expected_handshake_result = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
2246 }
2247
Gilles Peskine353eb332025-05-14 17:42:53 +02002248 TEST_EQUAL(mbedtls_test_move_handshake_to_state(&(client.ssl),
2249 &(server.ssl),
2250 MBEDTLS_SSL_HANDSHAKE_OVER),
2251 expected_handshake_result);
Yanray Wange6afd912022-10-27 12:11:18 +08002252
2253 if (expected_handshake_result != 0) {
2254 /* Connection will have failed by this point, skip to cleanup */
2255 goto exit;
2256 }
2257
Gilles Peskine353eb332025-05-14 17:42:53 +02002258 TEST_EQUAL(mbedtls_ssl_is_handshake_over(&client.ssl), 1);
Yanray Wange6afd912022-10-27 12:11:18 +08002259
2260 /* Make sure server state is moved to HANDSHAKE_OVER also. */
2261 TEST_EQUAL(mbedtls_test_move_handshake_to_state(&(server.ssl),
2262 &(client.ssl),
2263 MBEDTLS_SSL_HANDSHAKE_OVER),
2264 0);
2265
Gilles Peskine353eb332025-05-14 17:42:53 +02002266 TEST_EQUAL(mbedtls_ssl_is_handshake_over(&server.ssl), 1);
Yanray Wange6afd912022-10-27 12:11:18 +08002267 /* Check that both sides have negotiated the expected version. */
2268 mbedtls_test_set_step(0);
2269 if (!check_ssl_version(options->expected_negotiated_version,
2270 &client.ssl)) {
2271 goto exit;
2272 }
2273
2274 mbedtls_test_set_step(1);
2275 if (!check_ssl_version(options->expected_negotiated_version,
2276 &server.ssl)) {
2277 goto exit;
2278 }
2279
2280 if (options->expected_ciphersuite != 0) {
2281 TEST_EQUAL(server.ssl.session->ciphersuite,
2282 options->expected_ciphersuite);
2283 }
2284
2285#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2286 if (options->resize_buffers != 0) {
2287 /* A server, when using DTLS, might delay a buffer resize to happen
2288 * after it receives a message, so we force it. */
Gilles Peskine353eb332025-05-14 17:42:53 +02002289 TEST_EQUAL(exchange_data(&(client.ssl), &(server.ssl)), 0);
Yanray Wange6afd912022-10-27 12:11:18 +08002290
Gilles Peskine353eb332025-05-14 17:42:53 +02002291 TEST_EQUAL(client.ssl.out_buf_len,
2292 mbedtls_ssl_get_output_buflen(&client.ssl));
2293 TEST_EQUAL(client.ssl.in_buf_len,
2294 mbedtls_ssl_get_input_buflen(&client.ssl));
2295 TEST_EQUAL(server.ssl.out_buf_len,
2296 mbedtls_ssl_get_output_buflen(&server.ssl));
2297 TEST_EQUAL(server.ssl.in_buf_len,
2298 mbedtls_ssl_get_input_buflen(&server.ssl));
Yanray Wange6afd912022-10-27 12:11:18 +08002299 }
2300#endif
2301
2302 if (options->cli_msg_len != 0 || options->srv_msg_len != 0) {
2303 /* Start data exchanging test */
Gilles Peskine353eb332025-05-14 17:42:53 +02002304 TEST_EQUAL(mbedtls_test_ssl_exchange_data(
2305 &(client.ssl), options->cli_msg_len,
2306 options->expected_cli_fragments,
2307 &(server.ssl), options->srv_msg_len,
2308 options->expected_srv_fragments),
2309 0);
Yanray Wange6afd912022-10-27 12:11:18 +08002310 }
2311#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
2312 if (options->serialize == 1) {
Gilles Peskine353eb332025-05-14 17:42:53 +02002313 TEST_EQUAL(options->dtls, 1);
Yanray Wange6afd912022-10-27 12:11:18 +08002314
Gilles Peskine353eb332025-05-14 17:42:53 +02002315 TEST_EQUAL(mbedtls_ssl_context_save(&(server.ssl), NULL,
2316 0, &context_buf_len),
2317 MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL);
Yanray Wange6afd912022-10-27 12:11:18 +08002318
2319 context_buf = mbedtls_calloc(1, context_buf_len);
2320 TEST_ASSERT(context_buf != NULL);
2321
Gilles Peskine353eb332025-05-14 17:42:53 +02002322 TEST_EQUAL(mbedtls_ssl_context_save(&(server.ssl), context_buf,
2323 context_buf_len,
2324 &context_buf_len),
2325 0);
Yanray Wange6afd912022-10-27 12:11:18 +08002326
2327 mbedtls_ssl_free(&(server.ssl));
2328 mbedtls_ssl_init(&(server.ssl));
2329
Gilles Peskine353eb332025-05-14 17:42:53 +02002330 TEST_EQUAL(mbedtls_ssl_setup(&(server.ssl), &(server.conf)), 0);
Yanray Wange6afd912022-10-27 12:11:18 +08002331
2332 mbedtls_ssl_set_bio(&(server.ssl), &server_context,
2333 mbedtls_test_mock_tcp_send_msg,
2334 mbedtls_test_mock_tcp_recv_msg,
2335 NULL);
2336
2337 mbedtls_ssl_set_user_data_p(&server.ssl, &server);
2338
2339#if defined(MBEDTLS_TIMING_C)
2340 mbedtls_ssl_set_timer_cb(&server.ssl, &timer_server,
2341 mbedtls_timing_set_delay,
2342 mbedtls_timing_get_delay);
2343#endif
2344#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2345 if (options->resize_buffers != 0) {
2346 /* Ensure that the buffer sizes are appropriate before resizes */
Gilles Peskine353eb332025-05-14 17:42:53 +02002347 TEST_EQUAL(server.ssl.out_buf_len, MBEDTLS_SSL_OUT_BUFFER_LEN);
2348 TEST_EQUAL(server.ssl.in_buf_len, MBEDTLS_SSL_IN_BUFFER_LEN);
Yanray Wange6afd912022-10-27 12:11:18 +08002349 }
2350#endif
Gilles Peskine353eb332025-05-14 17:42:53 +02002351 TEST_EQUAL(mbedtls_ssl_context_load(&(server.ssl), context_buf,
2352 context_buf_len), 0);
Yanray Wange6afd912022-10-27 12:11:18 +08002353
2354#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2355 /* Validate buffer sizes after context deserialization */
2356 if (options->resize_buffers != 0) {
Gilles Peskine353eb332025-05-14 17:42:53 +02002357 TEST_EQUAL(server.ssl.out_buf_len,
2358 mbedtls_ssl_get_output_buflen(&server.ssl));
2359 TEST_EQUAL(server.ssl.in_buf_len,
2360 mbedtls_ssl_get_input_buflen(&server.ssl));
Yanray Wange6afd912022-10-27 12:11:18 +08002361 }
2362#endif
2363 /* Retest writing/reading */
2364 if (options->cli_msg_len != 0 || options->srv_msg_len != 0) {
Gilles Peskine353eb332025-05-14 17:42:53 +02002365 TEST_EQUAL(mbedtls_test_ssl_exchange_data(
2366 &(client.ssl), options->cli_msg_len,
2367 options->expected_cli_fragments,
2368 &(server.ssl), options->srv_msg_len,
2369 options->expected_srv_fragments),
2370 0);
Yanray Wange6afd912022-10-27 12:11:18 +08002371 }
2372 }
2373#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
2374
2375#if defined(MBEDTLS_SSL_RENEGOTIATION)
2376 if (options->renegotiate) {
2377 /* Start test with renegotiation */
Gilles Peskine353eb332025-05-14 17:42:53 +02002378 TEST_EQUAL(server.ssl.renego_status,
2379 MBEDTLS_SSL_INITIAL_HANDSHAKE);
2380 TEST_EQUAL(client.ssl.renego_status,
2381 MBEDTLS_SSL_INITIAL_HANDSHAKE);
Yanray Wange6afd912022-10-27 12:11:18 +08002382
2383 /* After calling this function for the server, it only sends a handshake
2384 * request. All renegotiation should happen during data exchanging */
Gilles Peskine353eb332025-05-14 17:42:53 +02002385 TEST_EQUAL(mbedtls_ssl_renegotiate(&(server.ssl)), 0);
2386 TEST_EQUAL(server.ssl.renego_status,
2387 MBEDTLS_SSL_RENEGOTIATION_PENDING);
2388 TEST_EQUAL(client.ssl.renego_status,
2389 MBEDTLS_SSL_INITIAL_HANDSHAKE);
Yanray Wange6afd912022-10-27 12:11:18 +08002390
Gilles Peskine353eb332025-05-14 17:42:53 +02002391 TEST_EQUAL(exchange_data(&(client.ssl), &(server.ssl)), 0);
2392 TEST_EQUAL(server.ssl.renego_status,
2393 MBEDTLS_SSL_RENEGOTIATION_DONE);
2394 TEST_EQUAL(client.ssl.renego_status,
2395 MBEDTLS_SSL_RENEGOTIATION_DONE);
Yanray Wange6afd912022-10-27 12:11:18 +08002396
2397 /* After calling mbedtls_ssl_renegotiate for the client,
2398 * all renegotiation should happen inside this function.
2399 * However in this test, we cannot perform simultaneous communication
2400 * between client and server so this function will return waiting error
2401 * on the socket. All rest of renegotiation should happen
2402 * during data exchanging */
2403 ret = mbedtls_ssl_renegotiate(&(client.ssl));
2404#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2405 if (options->resize_buffers != 0) {
2406 /* Ensure that the buffer sizes are appropriate before resizes */
Gilles Peskine353eb332025-05-14 17:42:53 +02002407 TEST_EQUAL(client.ssl.out_buf_len, MBEDTLS_SSL_OUT_BUFFER_LEN);
2408 TEST_EQUAL(client.ssl.in_buf_len, MBEDTLS_SSL_IN_BUFFER_LEN);
Yanray Wange6afd912022-10-27 12:11:18 +08002409 }
2410#endif
2411 TEST_ASSERT(ret == 0 ||
2412 ret == MBEDTLS_ERR_SSL_WANT_READ ||
2413 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
Gilles Peskine353eb332025-05-14 17:42:53 +02002414 TEST_EQUAL(server.ssl.renego_status,
2415 MBEDTLS_SSL_RENEGOTIATION_DONE);
2416 TEST_EQUAL(client.ssl.renego_status,
2417 MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS);
Yanray Wange6afd912022-10-27 12:11:18 +08002418
Gilles Peskine353eb332025-05-14 17:42:53 +02002419 TEST_EQUAL(exchange_data(&(client.ssl), &(server.ssl)), 0);
2420 TEST_EQUAL(server.ssl.renego_status,
2421 MBEDTLS_SSL_RENEGOTIATION_DONE);
2422 TEST_EQUAL(client.ssl.renego_status,
2423 MBEDTLS_SSL_RENEGOTIATION_DONE);
Yanray Wange6afd912022-10-27 12:11:18 +08002424#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2425 /* Validate buffer sizes after renegotiation */
2426 if (options->resize_buffers != 0) {
Gilles Peskine353eb332025-05-14 17:42:53 +02002427 TEST_EQUAL(client.ssl.out_buf_len,
2428 mbedtls_ssl_get_output_buflen(&client.ssl));
2429 TEST_EQUAL(client.ssl.in_buf_len,
2430 mbedtls_ssl_get_input_buflen(&client.ssl));
2431 TEST_EQUAL(server.ssl.out_buf_len,
2432 mbedtls_ssl_get_output_buflen(&server.ssl));
2433 TEST_EQUAL(server.ssl.in_buf_len,
2434 mbedtls_ssl_get_input_buflen(&server.ssl));
Yanray Wange6afd912022-10-27 12:11:18 +08002435 }
2436#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
2437 }
2438#endif /* MBEDTLS_SSL_RENEGOTIATION */
2439
2440 TEST_ASSERT(mbedtls_ssl_conf_get_user_data_p(&client.conf) == &client);
2441 TEST_ASSERT(mbedtls_ssl_get_user_data_p(&client.ssl) == &client);
2442 TEST_ASSERT(mbedtls_ssl_conf_get_user_data_p(&server.conf) == &server);
2443 TEST_ASSERT(mbedtls_ssl_get_user_data_p(&server.ssl) == &server);
2444
2445exit:
2446 mbedtls_test_ssl_endpoint_free(&client,
Yanray Wangaf727a22023-03-13 19:22:36 +08002447 options->dtls != 0 ? &client_context : NULL);
Yanray Wange6afd912022-10-27 12:11:18 +08002448 mbedtls_test_ssl_endpoint_free(&server,
Yanray Wangaf727a22023-03-13 19:22:36 +08002449 options->dtls != 0 ? &server_context : NULL);
Yanray Wange6afd912022-10-27 12:11:18 +08002450#if defined(MBEDTLS_DEBUG_C)
2451 if (options->cli_log_fun || options->srv_log_fun) {
2452 mbedtls_debug_set_threshold(0);
2453 }
2454#endif
2455#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
2456 if (context_buf != NULL) {
2457 mbedtls_free(context_buf);
2458 }
2459#endif
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +01002460 MD_OR_USE_PSA_DONE();
Yanray Wange6afd912022-10-27 12:11:18 +08002461}
2462#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
2463
2464#if defined(MBEDTLS_TEST_HOOKS)
Yanray Wangf56181a2023-03-16 12:21:33 +08002465int mbedtls_test_tweak_tls13_certificate_msg_vector_len(
Yanray Wange6afd912022-10-27 12:11:18 +08002466 unsigned char *buf, unsigned char **end, int tweak,
2467 int *expected_result, mbedtls_ssl_chk_buf_ptr_args *args)
2468{
2469/*
2470 * The definition of the tweaks assume that the certificate list contains only
2471 * one certificate.
2472 */
2473
2474/*
2475 * struct {
2476 * opaque cert_data<1..2^24-1>;
2477 * Extension extensions<0..2^16-1>;
2478 * } CertificateEntry;
2479 *
2480 * struct {
2481 * opaque certificate_request_context<0..2^8-1>;
2482 * CertificateEntry certificate_list<0..2^24-1>;
2483 * } Certificate;
2484 */
2485 unsigned char *p_certificate_request_context_len = buf;
2486 size_t certificate_request_context_len = buf[0];
2487
2488 unsigned char *p_certificate_list_len =
2489 buf + 1 + certificate_request_context_len;
2490 unsigned char *certificate_list = p_certificate_list_len + 3;
2491 size_t certificate_list_len =
2492 MBEDTLS_GET_UINT24_BE(p_certificate_list_len, 0);
2493
2494 unsigned char *p_cert_data_len = certificate_list;
2495 unsigned char *cert_data = p_cert_data_len + 3;
2496 size_t cert_data_len = MBEDTLS_GET_UINT24_BE(p_cert_data_len, 0);
2497
2498 unsigned char *p_extensions_len = cert_data + cert_data_len;
2499 unsigned char *extensions = p_extensions_len + 2;
2500 size_t extensions_len = MBEDTLS_GET_UINT16_BE(p_extensions_len, 0);
2501
2502 *expected_result = MBEDTLS_ERR_SSL_DECODE_ERROR;
2503
2504 switch (tweak) {
2505 case 1:
2506 /* Failure when checking if the certificate request context length
2507 * and certificate list length can be read
2508 */
2509 *end = buf + 3;
2510 set_chk_buf_ptr_args(args, buf, *end, 4);
2511 break;
2512
2513 case 2:
2514 /* Invalid certificate request context length.
2515 */
2516 *p_certificate_request_context_len =
Yanray Wanga8f445e2022-11-03 11:51:59 +08002517 (unsigned char) certificate_request_context_len + 1;
Yanray Wange6afd912022-10-27 12:11:18 +08002518 reset_chk_buf_ptr_args(args);
2519 break;
2520
2521 case 3:
2522 /* Failure when checking if certificate_list data can be read. */
2523 MBEDTLS_PUT_UINT24_BE(certificate_list_len + 1,
2524 p_certificate_list_len, 0);
2525 set_chk_buf_ptr_args(args, certificate_list, *end,
2526 certificate_list_len + 1);
2527 break;
2528
2529 case 4:
2530 /* Failure when checking if the cert_data length can be read. */
2531 MBEDTLS_PUT_UINT24_BE(2, p_certificate_list_len, 0);
2532 set_chk_buf_ptr_args(args, p_cert_data_len, certificate_list + 2, 3);
2533 break;
2534
2535 case 5:
2536 /* Failure when checking if cert_data data can be read. */
2537 MBEDTLS_PUT_UINT24_BE(certificate_list_len - 3 + 1,
2538 p_cert_data_len, 0);
2539 set_chk_buf_ptr_args(args, cert_data,
2540 certificate_list + certificate_list_len,
2541 certificate_list_len - 3 + 1);
2542 break;
2543
2544 case 6:
2545 /* Failure when checking if the extensions length can be read. */
2546 MBEDTLS_PUT_UINT24_BE(certificate_list_len - extensions_len - 1,
2547 p_certificate_list_len, 0);
2548 set_chk_buf_ptr_args(
2549 args, p_extensions_len,
2550 certificate_list + certificate_list_len - extensions_len - 1, 2);
2551 break;
2552
2553 case 7:
2554 /* Failure when checking if extensions data can be read. */
2555 MBEDTLS_PUT_UINT16_BE(extensions_len + 1, p_extensions_len, 0);
2556
2557 set_chk_buf_ptr_args(
2558 args, extensions,
2559 certificate_list + certificate_list_len, extensions_len + 1);
2560 break;
2561
2562 default:
2563 return -1;
2564 }
2565
2566 return 0;
2567}
2568#endif /* MBEDTLS_TEST_HOOKS */
Ronald Cron77abfe62024-01-15 11:17:31 +01002569
Ronald Cronfaf026c2024-01-31 14:32:06 +01002570/*
2571 * Functions for tests based on tickets. Implementations of the
2572 * write/parse ticket interfaces as defined by mbedtls_ssl_ticket_write/parse_t.
2573 * Basically same implementations as in ticket.c without the encryption. That
2574 * way we can tweak easily tickets characteristics to simulate misbehaving
2575 * peers.
2576 */
Ronald Cron77abfe62024-01-15 11:17:31 +01002577#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2578int mbedtls_test_ticket_write(
2579 void *p_ticket, const mbedtls_ssl_session *session,
2580 unsigned char *start, const unsigned char *end,
2581 size_t *tlen, uint32_t *lifetime)
2582{
2583 int ret;
2584 ((void) p_ticket);
2585
2586 if ((ret = mbedtls_ssl_session_save(session, start, end - start,
2587 tlen)) != 0) {
2588 return ret;
2589 }
2590
2591 /* Maximum ticket lifetime as defined in RFC 8446 */
2592 *lifetime = 7 * 24 * 3600;
2593
2594 return 0;
2595}
2596
2597int mbedtls_test_ticket_parse(void *p_ticket, mbedtls_ssl_session *session,
2598 unsigned char *buf, size_t len)
2599{
2600 ((void) p_ticket);
2601
2602 return mbedtls_ssl_session_load(session, buf, len);
2603}
2604#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Ronald Cron1f6e4e42024-01-26 16:31:33 +01002605
2606#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SRV_C) && \
2607 defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS) && \
2608 defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
2609int mbedtls_test_get_tls13_ticket(
2610 mbedtls_test_handshake_test_options *client_options,
2611 mbedtls_test_handshake_test_options *server_options,
2612 mbedtls_ssl_session *session)
2613{
2614 int ret = -1;
Gilles Peskine946bf142025-04-08 09:48:40 +02002615 int ok = 0;
Ronald Cron1f6e4e42024-01-26 16:31:33 +01002616 unsigned char buf[64];
2617 mbedtls_test_ssl_endpoint client_ep, server_ep;
2618
2619 mbedtls_platform_zeroize(&client_ep, sizeof(client_ep));
2620 mbedtls_platform_zeroize(&server_ep, sizeof(server_ep));
2621
2622 ret = mbedtls_test_ssl_endpoint_init(&client_ep, MBEDTLS_SSL_IS_CLIENT,
2623 client_options, NULL, NULL, NULL);
2624 TEST_EQUAL(ret, 0);
2625
2626 ret = mbedtls_test_ssl_endpoint_init(&server_ep, MBEDTLS_SSL_IS_SERVER,
2627 server_options, NULL, NULL, NULL);
2628 TEST_EQUAL(ret, 0);
2629
2630 mbedtls_ssl_conf_session_tickets_cb(&server_ep.conf,
2631 mbedtls_test_ticket_write,
2632 mbedtls_test_ticket_parse,
2633 NULL);
2634
2635 ret = mbedtls_test_mock_socket_connect(&(client_ep.socket),
2636 &(server_ep.socket), 1024);
2637 TEST_EQUAL(ret, 0);
2638
2639 TEST_EQUAL(mbedtls_test_move_handshake_to_state(
2640 &(server_ep.ssl), &(client_ep.ssl),
2641 MBEDTLS_SSL_HANDSHAKE_OVER), 0);
2642
2643 TEST_EQUAL(server_ep.ssl.handshake->new_session_tickets_count, 0);
2644
2645 do {
2646 ret = mbedtls_ssl_read(&(client_ep.ssl), buf, sizeof(buf));
2647 } while (ret != MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET);
2648
2649 ret = mbedtls_ssl_get_session(&(client_ep.ssl), session);
2650 TEST_EQUAL(ret, 0);
2651
Gilles Peskine946bf142025-04-08 09:48:40 +02002652 ok = 1;
2653
Ronald Cron1f6e4e42024-01-26 16:31:33 +01002654exit:
2655 mbedtls_test_ssl_endpoint_free(&client_ep, NULL);
2656 mbedtls_test_ssl_endpoint_free(&server_ep, NULL);
2657
Gilles Peskine946bf142025-04-08 09:48:40 +02002658 if (ret == 0 && !ok) {
2659 /* Exiting due to a test assertion that isn't ret == 0 */
2660 ret = -1;
2661 }
Ronald Cron1f6e4e42024-01-26 16:31:33 +01002662 return ret;
2663}
2664#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SRV_C &&
2665 MBEDTLS_SSL_PROTO_TLS1_3 && MBEDTLS_SSL_SESSION_TICKETS &&
2666 MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
2667
Yanray Wang4d07d1c2022-10-27 15:28:16 +08002668#endif /* MBEDTLS_SSL_TLS_C */