blob: a7b154a7e119fe7b0bc43a1e8f53fcc6ea1c6141 [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
Gilles Peskine27586d82025-05-28 17:01:42 +0200575
576#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) && \
577 defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED) && \
578 defined(MBEDTLS_SSL_SRV_C)
579static int psk_dummy_callback(void *p_info, mbedtls_ssl_context *ssl,
580 const unsigned char *name, size_t name_len)
581{
582 (void) p_info;
583 (void) ssl;
584 (void) name;
585 (void) name_len;
586
587 return 0;
588}
589#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED &&
590 MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED &&
591 MBEDTLS_SSL_SRV_C */
592
Yanray Wange6afd912022-10-27 12:11:18 +0800593#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
594
Gilles Peskine27586d82025-05-28 17:01:42 +0200595static int set_ciphersuite(mbedtls_test_ssl_endpoint *ep,
596 const char *cipher)
597{
598 if (cipher == NULL || cipher[0] == 0) {
599 return 1;
600 }
601
602 int ok = 0;
603
604 TEST_CALLOC(ep->ciphersuites, 2);
605 ep->ciphersuites[0] = mbedtls_ssl_get_ciphersuite_id(cipher);
606 ep->ciphersuites[1] = 0;
607
608 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
609 mbedtls_ssl_ciphersuite_from_id(ep->ciphersuites[0]);
610
611 TEST_ASSERT(ciphersuite_info != NULL);
612 TEST_ASSERT(ciphersuite_info->min_tls_version <= ep->conf.max_tls_version);
613 TEST_ASSERT(ciphersuite_info->max_tls_version >= ep->conf.min_tls_version);
614
615 if (ep->conf.max_tls_version > ciphersuite_info->max_tls_version) {
616 ep->conf.max_tls_version = (mbedtls_ssl_protocol_version) ciphersuite_info->max_tls_version;
617 }
618 if (ep->conf.min_tls_version < ciphersuite_info->min_tls_version) {
619 ep->conf.min_tls_version = (mbedtls_ssl_protocol_version) ciphersuite_info->min_tls_version;
620 }
621
622 mbedtls_ssl_conf_ciphersuites(&ep->conf, ep->ciphersuites);
623 ok = 1;
624
625exit:
626 return ok;
627}
628
Yanray Wange6afd912022-10-27 12:11:18 +0800629/*
630 * Deinitializes certificates from endpoint represented by \p ep.
631 */
Yanray Wangf6f71902023-03-15 16:05:14 +0800632static void test_ssl_endpoint_certificate_free(mbedtls_test_ssl_endpoint *ep)
Yanray Wange6afd912022-10-27 12:11:18 +0800633{
Gilles Peskineb6bb3fb2025-05-26 21:57:52 +0200634 if (ep->ca_chain != NULL) {
635 mbedtls_x509_crt_free(ep->ca_chain);
636 mbedtls_free(ep->ca_chain);
637 ep->ca_chain = NULL;
638 }
639 if (ep->cert != NULL) {
640 mbedtls_x509_crt_free(ep->cert);
641 mbedtls_free(ep->cert);
642 ep->cert = NULL;
643 }
644 if (ep->pkey != NULL) {
Yanray Wange6afd912022-10-27 12:11:18 +0800645#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskineb6bb3fb2025-05-26 21:57:52 +0200646 if (mbedtls_pk_get_type(ep->pkey) == MBEDTLS_PK_OPAQUE) {
647 psa_destroy_key(ep->pkey->priv_id);
Yanray Wange6afd912022-10-27 12:11:18 +0800648 }
Gilles Peskineb6bb3fb2025-05-26 21:57:52 +0200649#endif
650 mbedtls_pk_free(ep->pkey);
651 mbedtls_free(ep->pkey);
652 ep->pkey = NULL;
Yanray Wange6afd912022-10-27 12:11:18 +0800653 }
654}
655
Yanray Wange6afd912022-10-27 12:11:18 +0800656int mbedtls_test_ssl_endpoint_certificate_init(mbedtls_test_ssl_endpoint *ep,
657 int pk_alg,
658 int opaque_alg, int opaque_alg2,
659 int opaque_usage)
660{
661 int i = 0;
662 int ret = -1;
Gilles Peskine946bf142025-04-08 09:48:40 +0200663 int ok = 0;
Yanray Wange6afd912022-10-27 12:11:18 +0800664#if defined(MBEDTLS_USE_PSA_CRYPTO)
665 mbedtls_svc_key_id_t key_slot = MBEDTLS_SVC_KEY_ID_INIT;
666#endif
667
668 if (ep == NULL) {
669 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
670 }
671
Gilles Peskineb6bb3fb2025-05-26 21:57:52 +0200672 TEST_CALLOC(ep->ca_chain, 1);
673 TEST_CALLOC(ep->cert, 1);
674 TEST_CALLOC(ep->pkey, 1);
Yanray Wange6afd912022-10-27 12:11:18 +0800675
Gilles Peskineb6bb3fb2025-05-26 21:57:52 +0200676 mbedtls_x509_crt_init(ep->ca_chain);
677 mbedtls_x509_crt_init(ep->cert);
678 mbedtls_pk_init(ep->pkey);
Yanray Wange6afd912022-10-27 12:11:18 +0800679
680 /* Load the trusted CA */
681
682 for (i = 0; mbedtls_test_cas_der[i] != NULL; i++) {
683 ret = mbedtls_x509_crt_parse_der(
Gilles Peskineb6bb3fb2025-05-26 21:57:52 +0200684 ep->ca_chain,
Yanray Wange6afd912022-10-27 12:11:18 +0800685 (const unsigned char *) mbedtls_test_cas_der[i],
686 mbedtls_test_cas_der_len[i]);
Gilles Peskine353eb332025-05-14 17:42:53 +0200687 TEST_EQUAL(ret, 0);
Yanray Wange6afd912022-10-27 12:11:18 +0800688 }
689
690 /* Load own certificate and private key */
691
692 if (ep->conf.endpoint == MBEDTLS_SSL_IS_SERVER) {
693 if (pk_alg == MBEDTLS_PK_RSA) {
694 ret = mbedtls_x509_crt_parse(
Gilles Peskineb6bb3fb2025-05-26 21:57:52 +0200695 ep->cert,
Yanray Wange6afd912022-10-27 12:11:18 +0800696 (const unsigned char *) mbedtls_test_srv_crt_rsa_sha256_der,
697 mbedtls_test_srv_crt_rsa_sha256_der_len);
Gilles Peskine353eb332025-05-14 17:42:53 +0200698 TEST_EQUAL(ret, 0);
Yanray Wange6afd912022-10-27 12:11:18 +0800699
700 ret = mbedtls_pk_parse_key(
Gilles Peskineb6bb3fb2025-05-26 21:57:52 +0200701 ep->pkey,
Yanray Wange6afd912022-10-27 12:11:18 +0800702 (const unsigned char *) mbedtls_test_srv_key_rsa_der,
Ben Taylor440cb2a2025-03-05 09:40:08 +0000703 mbedtls_test_srv_key_rsa_der_len, NULL, 0);
Gilles Peskine353eb332025-05-14 17:42:53 +0200704 TEST_EQUAL(ret, 0);
Yanray Wange6afd912022-10-27 12:11:18 +0800705 } else {
706 ret = mbedtls_x509_crt_parse(
Gilles Peskineb6bb3fb2025-05-26 21:57:52 +0200707 ep->cert,
Yanray Wange6afd912022-10-27 12:11:18 +0800708 (const unsigned char *) mbedtls_test_srv_crt_ec_der,
709 mbedtls_test_srv_crt_ec_der_len);
Gilles Peskine353eb332025-05-14 17:42:53 +0200710 TEST_EQUAL(ret, 0);
Yanray Wange6afd912022-10-27 12:11:18 +0800711
712 ret = mbedtls_pk_parse_key(
Gilles Peskineb6bb3fb2025-05-26 21:57:52 +0200713 ep->pkey,
Yanray Wange6afd912022-10-27 12:11:18 +0800714 (const unsigned char *) mbedtls_test_srv_key_ec_der,
Ben Taylor440cb2a2025-03-05 09:40:08 +0000715 mbedtls_test_srv_key_ec_der_len, NULL, 0);
Gilles Peskine353eb332025-05-14 17:42:53 +0200716 TEST_EQUAL(ret, 0);
Yanray Wange6afd912022-10-27 12:11:18 +0800717 }
718 } else {
719 if (pk_alg == MBEDTLS_PK_RSA) {
720 ret = mbedtls_x509_crt_parse(
Gilles Peskineb6bb3fb2025-05-26 21:57:52 +0200721 ep->cert,
Yanray Wange6afd912022-10-27 12:11:18 +0800722 (const unsigned char *) mbedtls_test_cli_crt_rsa_der,
723 mbedtls_test_cli_crt_rsa_der_len);
Gilles Peskine353eb332025-05-14 17:42:53 +0200724 TEST_EQUAL(ret, 0);
Yanray Wange6afd912022-10-27 12:11:18 +0800725
726 ret = mbedtls_pk_parse_key(
Gilles Peskineb6bb3fb2025-05-26 21:57:52 +0200727 ep->pkey,
Yanray Wange6afd912022-10-27 12:11:18 +0800728 (const unsigned char *) mbedtls_test_cli_key_rsa_der,
Ben Taylor440cb2a2025-03-05 09:40:08 +0000729 mbedtls_test_cli_key_rsa_der_len, NULL, 0);
Gilles Peskine353eb332025-05-14 17:42:53 +0200730 TEST_EQUAL(ret, 0);
Yanray Wange6afd912022-10-27 12:11:18 +0800731 } else {
732 ret = mbedtls_x509_crt_parse(
Gilles Peskineb6bb3fb2025-05-26 21:57:52 +0200733 ep->cert,
Yanray Wange6afd912022-10-27 12:11:18 +0800734 (const unsigned char *) mbedtls_test_cli_crt_ec_der,
735 mbedtls_test_cli_crt_ec_len);
Gilles Peskine353eb332025-05-14 17:42:53 +0200736 TEST_EQUAL(ret, 0);
Yanray Wange6afd912022-10-27 12:11:18 +0800737
738 ret = mbedtls_pk_parse_key(
Gilles Peskineb6bb3fb2025-05-26 21:57:52 +0200739 ep->pkey,
Yanray Wange6afd912022-10-27 12:11:18 +0800740 (const unsigned char *) mbedtls_test_cli_key_ec_der,
Ben Taylor440cb2a2025-03-05 09:40:08 +0000741 mbedtls_test_cli_key_ec_der_len, NULL, 0);
Gilles Peskine353eb332025-05-14 17:42:53 +0200742 TEST_EQUAL(ret, 0);
Yanray Wange6afd912022-10-27 12:11:18 +0800743 }
744 }
745
746#if defined(MBEDTLS_USE_PSA_CRYPTO)
747 if (opaque_alg != 0) {
Valerio Setti1fa2f6e2024-02-27 08:11:25 +0100748 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
749 /* Use a fake key usage to get a successful initial guess for the PSA attributes. */
Gilles Peskineb6bb3fb2025-05-26 21:57:52 +0200750 TEST_EQUAL(mbedtls_pk_get_psa_attributes(ep->pkey, PSA_KEY_USAGE_SIGN_HASH,
Valerio Setti1fa2f6e2024-02-27 08:11:25 +0100751 &key_attr), 0);
Valerio Settia9de9442024-02-27 13:56:09 +0100752 /* Then manually usage, alg and alg2 as requested by the test. */
Valerio Setti1fa2f6e2024-02-27 08:11:25 +0100753 psa_set_key_usage_flags(&key_attr, opaque_usage);
754 psa_set_key_algorithm(&key_attr, opaque_alg);
755 if (opaque_alg2 != PSA_ALG_NONE) {
756 psa_set_key_enrollment_algorithm(&key_attr, opaque_alg2);
757 }
Gilles Peskineb6bb3fb2025-05-26 21:57:52 +0200758 TEST_EQUAL(mbedtls_pk_import_into_psa(ep->pkey, &key_attr, &key_slot), 0);
759 mbedtls_pk_free(ep->pkey);
760 mbedtls_pk_init(ep->pkey);
761 TEST_EQUAL(mbedtls_pk_setup_opaque(ep->pkey, key_slot), 0);
Yanray Wange6afd912022-10-27 12:11:18 +0800762 }
763#else
764 (void) opaque_alg;
765 (void) opaque_alg2;
766 (void) opaque_usage;
767#endif
768
Gilles Peskineb6bb3fb2025-05-26 21:57:52 +0200769 mbedtls_ssl_conf_ca_chain(&(ep->conf), ep->ca_chain, NULL);
Yanray Wange6afd912022-10-27 12:11:18 +0800770
Gilles Peskineb6bb3fb2025-05-26 21:57:52 +0200771 ret = mbedtls_ssl_conf_own_cert(&(ep->conf), ep->cert,
772 ep->pkey);
Gilles Peskine353eb332025-05-14 17:42:53 +0200773 TEST_EQUAL(ret, 0);
Yanray Wange6afd912022-10-27 12:11:18 +0800774
Gilles Peskine946bf142025-04-08 09:48:40 +0200775 ok = 1;
776
Yanray Wange6afd912022-10-27 12:11:18 +0800777exit:
Gilles Peskine946bf142025-04-08 09:48:40 +0200778 if (ret == 0 && !ok) {
779 /* Exiting due to a test assertion that isn't ret == 0 */
780 ret = -1;
781 }
Yanray Wange6afd912022-10-27 12:11:18 +0800782 if (ret != 0) {
Yanray Wangf6f71902023-03-15 16:05:14 +0800783 test_ssl_endpoint_certificate_free(ep);
Yanray Wange6afd912022-10-27 12:11:18 +0800784 }
785
786 return ret;
787}
788
Yanray Wange6afd912022-10-27 12:11:18 +0800789int mbedtls_test_ssl_endpoint_init(
790 mbedtls_test_ssl_endpoint *ep, int endpoint_type,
Gilles Peskineca8a9ac2025-05-27 20:52:24 +0200791 const mbedtls_test_handshake_test_options *options)
Yanray Wange6afd912022-10-27 12:11:18 +0800792{
793 int ret = -1;
794 uintptr_t user_data_n;
Gilles Peskine27586d82025-05-28 17:01:42 +0200795#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
796 const char *psk_identity = "foo";
797#endif
Yanray Wange6afd912022-10-27 12:11:18 +0800798
Yanray Wange6afd912022-10-27 12:11:18 +0800799 if (ep == NULL) {
800 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
801 }
802
803 memset(ep, 0, sizeof(*ep));
804
805 ep->name = (endpoint_type == MBEDTLS_SSL_IS_SERVER) ? "Server" : "Client";
806
807 mbedtls_ssl_init(&(ep->ssl));
808 mbedtls_ssl_config_init(&(ep->conf));
Gilles Peskine29969592025-05-27 19:24:28 +0200809 mbedtls_test_message_socket_init(&ep->dtls_context);
Yanray Wange6afd912022-10-27 12:11:18 +0800810
811 TEST_ASSERT(mbedtls_ssl_conf_get_user_data_p(&ep->conf) == NULL);
812 TEST_EQUAL(mbedtls_ssl_conf_get_user_data_n(&ep->conf), 0);
813 TEST_ASSERT(mbedtls_ssl_get_user_data_p(&ep->ssl) == NULL);
814 TEST_EQUAL(mbedtls_ssl_get_user_data_n(&ep->ssl), 0);
815
816 (void) mbedtls_test_rnd_std_rand(NULL,
817 (void *) &user_data_n,
818 sizeof(user_data_n));
819 mbedtls_ssl_conf_set_user_data_n(&ep->conf, user_data_n);
820 mbedtls_ssl_set_user_data_n(&ep->ssl, user_data_n);
821
Gilles Peskine6c154e72025-05-27 20:23:52 +0200822 mbedtls_test_mock_socket_init(&(ep->socket));
Yanray Wange6afd912022-10-27 12:11:18 +0800823
824 /* Non-blocking callbacks without timeout */
Gilles Peskine29969592025-05-27 19:24:28 +0200825 if (options->dtls) {
826 mbedtls_ssl_set_bio(&(ep->ssl), &ep->dtls_context,
Yanray Wange6afd912022-10-27 12:11:18 +0800827 mbedtls_test_mock_tcp_send_msg,
828 mbedtls_test_mock_tcp_recv_msg,
829 NULL);
Gilles Peskine0677e022025-05-27 18:05:20 +0200830#if defined(MBEDTLS_TIMING_C)
831 mbedtls_ssl_set_timer_cb(&ep->ssl, &ep->timer,
832 mbedtls_timing_set_delay,
833 mbedtls_timing_get_delay);
834#endif
Yanray Wange6afd912022-10-27 12:11:18 +0800835 } else {
836 mbedtls_ssl_set_bio(&(ep->ssl), &(ep->socket),
837 mbedtls_test_mock_tcp_send_nb,
838 mbedtls_test_mock_tcp_recv_nb,
839 NULL);
840 }
841
842 ret = mbedtls_ssl_config_defaults(&(ep->conf), endpoint_type,
Gilles Peskine29969592025-05-27 19:24:28 +0200843 options->dtls ?
Yanray Wange6afd912022-10-27 12:11:18 +0800844 MBEDTLS_SSL_TRANSPORT_DATAGRAM :
845 MBEDTLS_SSL_TRANSPORT_STREAM,
846 MBEDTLS_SSL_PRESET_DEFAULT);
Gilles Peskine353eb332025-05-14 17:42:53 +0200847 TEST_EQUAL(ret, 0);
Yanray Wange6afd912022-10-27 12:11:18 +0800848
Ronald Crona697a712023-03-09 17:47:42 +0100849 if (MBEDTLS_SSL_IS_CLIENT == endpoint_type) {
850 if (options->client_min_version != MBEDTLS_SSL_VERSION_UNKNOWN) {
851 mbedtls_ssl_conf_min_tls_version(&(ep->conf),
852 options->client_min_version);
853 }
854
855 if (options->client_max_version != MBEDTLS_SSL_VERSION_UNKNOWN) {
856 mbedtls_ssl_conf_max_tls_version(&(ep->conf),
857 options->client_max_version);
858 }
859 } else {
860 if (options->server_min_version != MBEDTLS_SSL_VERSION_UNKNOWN) {
861 mbedtls_ssl_conf_min_tls_version(&(ep->conf),
862 options->server_min_version);
863 }
864
865 if (options->server_max_version != MBEDTLS_SSL_VERSION_UNKNOWN) {
866 mbedtls_ssl_conf_max_tls_version(&(ep->conf),
867 options->server_max_version);
868 }
869 }
870
Gilles Peskine27586d82025-05-28 17:01:42 +0200871 if (MBEDTLS_SSL_IS_CLIENT == endpoint_type) {
872 TEST_ASSERT(set_ciphersuite(ep, options->cipher));
873 }
874
Ronald Cronfb536472024-01-26 14:55:25 +0100875 if (options->group_list != NULL) {
876 mbedtls_ssl_conf_groups(&(ep->conf), options->group_list);
Yanray Wange6afd912022-10-27 12:11:18 +0800877 }
878
879 mbedtls_ssl_conf_authmode(&(ep->conf), MBEDTLS_SSL_VERIFY_REQUIRED);
880
Ronald Cronced99be2024-01-26 15:49:12 +0100881#if defined(MBEDTLS_SSL_EARLY_DATA)
882 mbedtls_ssl_conf_early_data(&(ep->conf), options->early_data);
Ronald Cron5d3036e2024-02-23 07:43:45 +0100883#if defined(MBEDTLS_SSL_SRV_C)
884 if (endpoint_type == MBEDTLS_SSL_IS_SERVER &&
885 (options->max_early_data_size >= 0)) {
886 mbedtls_ssl_conf_max_early_data_size(&(ep->conf),
887 options->max_early_data_size);
888 }
889#endif
Gilles Peskine27586d82025-05-28 17:01:42 +0200890
Waleed Elmelegy4dfb0e72024-03-14 01:48:40 +0000891#if defined(MBEDTLS_SSL_ALPN)
892 /* check that alpn_list contains at least one valid entry */
893 if (options->alpn_list[0] != NULL) {
894 mbedtls_ssl_conf_alpn_protocols(&(ep->conf), options->alpn_list);
895 }
896#endif
Ronald Cronced99be2024-01-26 15:49:12 +0100897#endif
898
Gilles Peskine27586d82025-05-28 17:01:42 +0200899#if defined(MBEDTLS_SSL_RENEGOTIATION)
900 if (options->renegotiate) {
901 mbedtls_ssl_conf_renegotiation(&ep->conf,
902 MBEDTLS_SSL_RENEGOTIATION_ENABLED);
903 mbedtls_ssl_conf_legacy_renegotiation(&ep->conf,
904 options->legacy_renegotiation);
905 }
906#endif /* MBEDTLS_SSL_RENEGOTIATION */
907
Yanray Wange6afd912022-10-27 12:11:18 +0800908#if defined(MBEDTLS_SSL_CACHE_C) && defined(MBEDTLS_SSL_SRV_C)
909 if (endpoint_type == MBEDTLS_SSL_IS_SERVER && options->cache != NULL) {
910 mbedtls_ssl_conf_session_cache(&(ep->conf), options->cache,
911 mbedtls_ssl_cache_get,
912 mbedtls_ssl_cache_set);
913 }
914#endif
915
Gilles Peskine27586d82025-05-28 17:01:42 +0200916#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
917 TEST_EQUAL(mbedtls_ssl_conf_max_frag_len(&ep->conf,
918 (unsigned char) options->mfl),
919 0);
920#else
921 TEST_EQUAL(MBEDTLS_SSL_MAX_FRAG_LEN_NONE, options->mfl);
922#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
923
Yanray Wange6afd912022-10-27 12:11:18 +0800924 ret = mbedtls_ssl_setup(&(ep->ssl), &(ep->conf));
Gilles Peskine353eb332025-05-14 17:42:53 +0200925 TEST_EQUAL(ret, 0);
Yanray Wange6afd912022-10-27 12:11:18 +0800926
Gilles Peskine856a3702025-02-13 17:28:49 +0100927 if (MBEDTLS_SSL_IS_CLIENT == endpoint_type) {
928 ret = mbedtls_ssl_set_hostname(&(ep->ssl), "localhost");
Gilles Peskine55b8bb42025-04-08 09:44:34 +0200929 TEST_EQUAL(ret, 0);
Gilles Peskine856a3702025-02-13 17:28:49 +0100930 }
931
Yanray Wange6afd912022-10-27 12:11:18 +0800932#if defined(MBEDTLS_SSL_PROTO_DTLS) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine29969592025-05-27 19:24:28 +0200933 if (endpoint_type == MBEDTLS_SSL_IS_SERVER && options->dtls) {
Yanray Wange6afd912022-10-27 12:11:18 +0800934 mbedtls_ssl_conf_dtls_cookies(&(ep->conf), NULL, NULL, NULL);
935 }
936#endif
937
Ronald Cronec3408d2024-01-16 17:50:40 +0100938#if defined(MBEDTLS_DEBUG_C)
939#if defined(MBEDTLS_SSL_SRV_C)
940 if (endpoint_type == MBEDTLS_SSL_IS_SERVER &&
941 options->srv_log_fun != NULL) {
942 mbedtls_ssl_conf_dbg(&(ep->conf), options->srv_log_fun,
943 options->srv_log_obj);
944 }
945#endif
946#if defined(MBEDTLS_SSL_CLI_C)
947 if (endpoint_type == MBEDTLS_SSL_IS_CLIENT &&
948 options->cli_log_fun != NULL) {
949 mbedtls_ssl_conf_dbg(&(ep->conf), options->cli_log_fun,
950 options->cli_log_obj);
951 }
952#endif
953#endif /* MBEDTLS_DEBUG_C */
954
Yanray Wange6afd912022-10-27 12:11:18 +0800955 ret = mbedtls_test_ssl_endpoint_certificate_init(ep, options->pk_alg,
956 options->opaque_alg,
957 options->opaque_alg2,
958 options->opaque_usage);
Gilles Peskine353eb332025-05-14 17:42:53 +0200959 TEST_EQUAL(ret, 0);
Yanray Wange6afd912022-10-27 12:11:18 +0800960
Gilles Peskine27586d82025-05-28 17:01:42 +0200961#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
962 if (options->psk_str != NULL && options->psk_str->len > 0) {
963 TEST_EQUAL(mbedtls_ssl_conf_psk(
964 &ep->conf, options->psk_str->x,
965 options->psk_str->len,
966 (const unsigned char *) psk_identity,
967 strlen(psk_identity)), 0);
968#if defined(MBEDTLS_SSL_SRV_C)
969 if (MBEDTLS_SSL_IS_SERVER == endpoint_type) {
970 mbedtls_ssl_conf_psk_cb(&ep->conf, psk_dummy_callback, NULL);
971 }
972#endif
973 }
974#endif
975
Yanray Wange6afd912022-10-27 12:11:18 +0800976 TEST_EQUAL(mbedtls_ssl_conf_get_user_data_n(&ep->conf), user_data_n);
977 mbedtls_ssl_conf_set_user_data_p(&ep->conf, ep);
978 TEST_EQUAL(mbedtls_ssl_get_user_data_n(&ep->ssl), user_data_n);
979 mbedtls_ssl_set_user_data_p(&ep->ssl, ep);
980
Gilles Peskine946bf142025-04-08 09:48:40 +0200981 return 0;
982
Yanray Wange6afd912022-10-27 12:11:18 +0800983exit:
Gilles Peskine946bf142025-04-08 09:48:40 +0200984 if (ret == 0) {
985 /* Exiting due to a test assertion that isn't ret == 0 */
986 ret = -1;
987 }
Yanray Wange6afd912022-10-27 12:11:18 +0800988 return ret;
989}
990
Yanray Wange6afd912022-10-27 12:11:18 +0800991void mbedtls_test_ssl_endpoint_free(
Gilles Peskineca8a9ac2025-05-27 20:52:24 +0200992 mbedtls_test_ssl_endpoint *ep)
Yanray Wange6afd912022-10-27 12:11:18 +0800993{
Yanray Wange6afd912022-10-27 12:11:18 +0800994 mbedtls_ssl_free(&(ep->ssl));
995 mbedtls_ssl_config_free(&(ep->conf));
996
Gilles Peskine2744a432025-05-27 13:27:22 +0200997 mbedtls_free(ep->ciphersuites);
998 ep->ciphersuites = NULL;
999 test_ssl_endpoint_certificate_free(ep);
1000
Gilles Peskine29969592025-05-27 19:24:28 +02001001 if (ep->dtls_context.socket != NULL) {
1002 mbedtls_test_message_socket_close(&ep->dtls_context);
Yanray Wange6afd912022-10-27 12:11:18 +08001003 } else {
1004 mbedtls_test_mock_socket_close(&(ep->socket));
1005 }
1006}
1007
Gilles Peskineb092e782025-05-27 20:15:03 +02001008int mbedtls_test_ssl_dtls_join_endpoints(mbedtls_test_ssl_endpoint *client,
1009 mbedtls_test_ssl_endpoint *server)
1010{
1011 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1012
Gilles Peskine6c154e72025-05-27 20:23:52 +02001013 ret = mbedtls_test_message_socket_setup(&client->queue_input,
1014 &server->queue_input,
1015 100, &(client->socket),
1016 &client->dtls_context);
1017 TEST_EQUAL(ret, 0);
Gilles Peskineb092e782025-05-27 20:15:03 +02001018
Gilles Peskine6c154e72025-05-27 20:23:52 +02001019 ret = mbedtls_test_message_socket_setup(&server->queue_input,
1020 &client->queue_input,
1021 100, &(server->socket),
1022 &server->dtls_context);
1023 TEST_EQUAL(ret, 0);
1024
1025exit:
Gilles Peskineb092e782025-05-27 20:15:03 +02001026 return ret;
1027}
1028
Yanray Wange6afd912022-10-27 12:11:18 +08001029int mbedtls_test_move_handshake_to_state(mbedtls_ssl_context *ssl,
1030 mbedtls_ssl_context *second_ssl,
1031 int state)
1032{
1033 enum { BUFFSIZE = 1024 };
1034 int max_steps = 1000;
1035 int ret = 0;
1036
1037 if (ssl == NULL || second_ssl == NULL) {
1038 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
1039 }
1040
1041 /* Perform communication via connected sockets */
1042 while ((ssl->state != state) && (--max_steps >= 0)) {
1043 /* If /p second_ssl ends the handshake procedure before /p ssl then
1044 * there is no need to call the next step */
1045 if (!mbedtls_ssl_is_handshake_over(second_ssl)) {
1046 ret = mbedtls_ssl_handshake_step(second_ssl);
1047 if (ret != 0 && ret != MBEDTLS_ERR_SSL_WANT_READ &&
1048 ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
1049 return ret;
1050 }
1051 }
1052
1053 /* We only care about the \p ssl state and returns, so we call it last,
1054 * to leave the iteration as soon as the state is as expected. */
1055 ret = mbedtls_ssl_handshake_step(ssl);
1056 if (ret != 0 && ret != MBEDTLS_ERR_SSL_WANT_READ &&
1057 ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
1058 return ret;
1059 }
1060 }
1061
1062 return (max_steps >= 0) ? ret : -1;
1063}
1064
1065#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
1066
1067/*
1068 * Write application data. Increase write counter if necessary.
1069 */
Michael Schuster54300d42024-06-04 02:30:22 +02001070static int mbedtls_ssl_write_fragment(mbedtls_ssl_context *ssl,
Michael Schusterbd89b792024-06-04 02:41:10 +02001071 unsigned char *buf, int buf_len,
1072 int *written,
1073 const int expected_fragments)
Yanray Wange6afd912022-10-27 12:11:18 +08001074{
Agathiyan Bragadeesh93212652023-07-12 11:22:59 +01001075 int ret;
Yanray Wange6afd912022-10-27 12:11:18 +08001076 /* Verify that calling mbedtls_ssl_write with a NULL buffer and zero length is
1077 * a valid no-op for TLS connections. */
1078 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Gilles Peskine353eb332025-05-14 17:42:53 +02001079 TEST_EQUAL(mbedtls_ssl_write(ssl, NULL, 0), 0);
Yanray Wange6afd912022-10-27 12:11:18 +08001080 }
1081
Agathiyan Bragadeesh93212652023-07-12 11:22:59 +01001082 ret = mbedtls_ssl_write(ssl, buf + *written, buf_len - *written);
Yanray Wange6afd912022-10-27 12:11:18 +08001083 if (ret > 0) {
1084 *written += ret;
1085 }
1086
1087 if (expected_fragments == 0) {
1088 /* Used for DTLS and the message size larger than MFL. In that case
1089 * the message can not be fragmented and the library should return
1090 * MBEDTLS_ERR_SSL_BAD_INPUT_DATA error. This error must be returned
Yanray Wangb088bfc2023-03-16 12:15:49 +08001091 * to prevent a dead loop inside mbedtls_test_ssl_exchange_data(). */
Yanray Wange6afd912022-10-27 12:11:18 +08001092 return ret;
1093 } else if (expected_fragments == 1) {
1094 /* Used for TLS/DTLS and the message size lower than MFL */
1095 TEST_ASSERT(ret == buf_len ||
1096 ret == MBEDTLS_ERR_SSL_WANT_READ ||
1097 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
1098 } else {
1099 /* Used for TLS and the message size larger than MFL */
1100 TEST_ASSERT(expected_fragments > 1);
1101 TEST_ASSERT((ret >= 0 && ret <= buf_len) ||
1102 ret == MBEDTLS_ERR_SSL_WANT_READ ||
1103 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
1104 }
1105
1106 return 0;
1107
1108exit:
1109 /* Some of the tests failed */
1110 return -1;
1111}
1112
1113/*
1114 * Read application data and increase read counter and fragments counter
1115 * if necessary.
1116 */
Michael Schuster54300d42024-06-04 02:30:22 +02001117static int mbedtls_ssl_read_fragment(mbedtls_ssl_context *ssl,
Michael Schusterbd89b792024-06-04 02:41:10 +02001118 unsigned char *buf, int buf_len,
1119 int *read, int *fragments,
1120 const int expected_fragments)
Yanray Wange6afd912022-10-27 12:11:18 +08001121{
Agathiyan Bragadeesh93212652023-07-12 11:22:59 +01001122 int ret;
Yanray Wange6afd912022-10-27 12:11:18 +08001123 /* Verify that calling mbedtls_ssl_write with a NULL buffer and zero length is
1124 * a valid no-op for TLS connections. */
1125 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Gilles Peskine353eb332025-05-14 17:42:53 +02001126 TEST_EQUAL(mbedtls_ssl_read(ssl, NULL, 0), 0);
Yanray Wange6afd912022-10-27 12:11:18 +08001127 }
1128
Agathiyan Bragadeesh93212652023-07-12 11:22:59 +01001129 ret = mbedtls_ssl_read(ssl, buf + *read, buf_len - *read);
Yanray Wange6afd912022-10-27 12:11:18 +08001130 if (ret > 0) {
1131 (*fragments)++;
1132 *read += ret;
1133 }
1134
1135 if (expected_fragments == 0) {
Gilles Peskine353eb332025-05-14 17:42:53 +02001136 TEST_EQUAL(ret, 0);
Yanray Wange6afd912022-10-27 12:11:18 +08001137 } else if (expected_fragments == 1) {
1138 TEST_ASSERT(ret == buf_len ||
1139 ret == MBEDTLS_ERR_SSL_WANT_READ ||
1140 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
1141 } else {
1142 TEST_ASSERT(expected_fragments > 1);
1143 TEST_ASSERT((ret >= 0 && ret <= buf_len) ||
1144 ret == MBEDTLS_ERR_SSL_WANT_READ ||
1145 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
1146 }
1147
1148 return 0;
1149
1150exit:
1151 /* Some of the tests failed */
1152 return -1;
1153}
1154
Yanray Wange6afd912022-10-27 12:11:18 +08001155#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Elena Uziunaite74342c72024-07-05 11:31:29 +01001156 defined(PSA_WANT_ALG_CBC_NO_PADDING) && defined(PSA_WANT_KEY_TYPE_AES)
Yanray Wange6afd912022-10-27 12:11:18 +08001157int mbedtls_test_psa_cipher_encrypt_helper(mbedtls_ssl_transform *transform,
1158 const unsigned char *iv,
1159 size_t iv_len,
1160 const unsigned char *input,
1161 size_t ilen,
1162 unsigned char *output,
1163 size_t *olen)
1164{
1165#if defined(MBEDTLS_USE_PSA_CRYPTO)
1166 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1167 psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT;
1168 size_t part_len;
1169
1170 status = psa_cipher_encrypt_setup(&cipher_op,
1171 transform->psa_key_enc,
1172 transform->psa_alg);
1173
1174 if (status != PSA_SUCCESS) {
1175 return PSA_TO_MBEDTLS_ERR(status);
1176 }
1177
1178 status = psa_cipher_set_iv(&cipher_op, iv, iv_len);
1179
1180 if (status != PSA_SUCCESS) {
1181 return PSA_TO_MBEDTLS_ERR(status);
1182 }
1183
1184 status = psa_cipher_update(&cipher_op, input, ilen, output, ilen, olen);
1185
1186 if (status != PSA_SUCCESS) {
1187 return PSA_TO_MBEDTLS_ERR(status);
1188 }
1189
1190 status = psa_cipher_finish(&cipher_op, output + *olen, ilen - *olen,
1191 &part_len);
1192
1193 if (status != PSA_SUCCESS) {
1194 return PSA_TO_MBEDTLS_ERR(status);
1195 }
1196
1197 *olen += part_len;
1198 return 0;
1199#else
1200 return mbedtls_cipher_crypt(&transform->cipher_ctx_enc,
1201 iv, iv_len, input, ilen, output, olen);
1202#endif /* MBEDTLS_USE_PSA_CRYPTO */
1203}
Elena Uziunaite74342c72024-07-05 11:31:29 +01001204#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && PSA_WANT_ALG_CBC_NO_PADDING &&
Elena Uziunaite6121a342024-07-05 11:16:53 +01001205 PSA_WANT_KEY_TYPE_AES */
Yanray Wange6afd912022-10-27 12:11:18 +08001206
Valerio Setti31ad3a12023-10-27 11:55:02 +02001207static void mbedtls_test_ssl_cipher_info_from_type(mbedtls_cipher_type_t cipher_type,
1208 mbedtls_cipher_mode_t *cipher_mode,
1209 size_t *key_bits, size_t *iv_len)
1210{
1211 switch (cipher_type) {
1212 case MBEDTLS_CIPHER_AES_128_CBC:
1213 *cipher_mode = MBEDTLS_MODE_CBC;
1214 *key_bits = 128;
1215 *iv_len = 16;
1216 break;
1217 case MBEDTLS_CIPHER_AES_256_CBC:
1218 *cipher_mode = MBEDTLS_MODE_CBC;
1219 *key_bits = 256;
1220 *iv_len = 16;
1221 break;
1222 case MBEDTLS_CIPHER_ARIA_128_CBC:
1223 *cipher_mode = MBEDTLS_MODE_CBC;
1224 *key_bits = 128;
1225 *iv_len = 16;
1226 break;
1227 case MBEDTLS_CIPHER_ARIA_256_CBC:
1228 *cipher_mode = MBEDTLS_MODE_CBC;
1229 *key_bits = 256;
1230 *iv_len = 16;
1231 break;
1232 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
1233 *cipher_mode = MBEDTLS_MODE_CBC;
1234 *key_bits = 128;
1235 *iv_len = 16;
1236 break;
1237 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
1238 *cipher_mode = MBEDTLS_MODE_CBC;
1239 *key_bits = 256;
1240 *iv_len = 16;
1241 break;
1242
1243 case MBEDTLS_CIPHER_AES_128_CCM:
1244 *cipher_mode = MBEDTLS_MODE_CCM;
1245 *key_bits = 128;
1246 *iv_len = 12;
1247 break;
1248 case MBEDTLS_CIPHER_AES_192_CCM:
1249 *cipher_mode = MBEDTLS_MODE_CCM;
1250 *key_bits = 192;
1251 *iv_len = 12;
1252 break;
1253 case MBEDTLS_CIPHER_AES_256_CCM:
1254 *cipher_mode = MBEDTLS_MODE_CCM;
1255 *key_bits = 256;
1256 *iv_len = 12;
1257 break;
1258 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
1259 *cipher_mode = MBEDTLS_MODE_CCM;
1260 *key_bits = 128;
1261 *iv_len = 12;
1262 break;
1263 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
1264 *cipher_mode = MBEDTLS_MODE_CCM;
1265 *key_bits = 192;
1266 *iv_len = 12;
1267 break;
1268 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
1269 *cipher_mode = MBEDTLS_MODE_CCM;
1270 *key_bits = 256;
1271 *iv_len = 12;
1272 break;
1273
1274 case MBEDTLS_CIPHER_AES_128_GCM:
1275 *cipher_mode = MBEDTLS_MODE_GCM;
1276 *key_bits = 128;
1277 *iv_len = 12;
1278 break;
1279 case MBEDTLS_CIPHER_AES_192_GCM:
1280 *cipher_mode = MBEDTLS_MODE_GCM;
1281 *key_bits = 192;
1282 *iv_len = 12;
1283 break;
1284 case MBEDTLS_CIPHER_AES_256_GCM:
1285 *cipher_mode = MBEDTLS_MODE_GCM;
1286 *key_bits = 256;
1287 *iv_len = 12;
1288 break;
1289 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
1290 *cipher_mode = MBEDTLS_MODE_GCM;
1291 *key_bits = 128;
1292 *iv_len = 12;
1293 break;
1294 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
1295 *cipher_mode = MBEDTLS_MODE_GCM;
1296 *key_bits = 192;
1297 *iv_len = 12;
1298 break;
1299 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
1300 *cipher_mode = MBEDTLS_MODE_GCM;
1301 *key_bits = 256;
1302 *iv_len = 12;
1303 break;
1304
1305 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
1306 *cipher_mode = MBEDTLS_MODE_CHACHAPOLY;
1307 *key_bits = 256;
1308 *iv_len = 12;
1309 break;
1310
1311 case MBEDTLS_CIPHER_NULL:
1312 *cipher_mode = MBEDTLS_MODE_STREAM;
1313 *key_bits = 0;
1314 *iv_len = 0;
1315 break;
1316
1317 default:
1318 *cipher_mode = MBEDTLS_MODE_NONE;
1319 *key_bits = 0;
1320 *iv_len = 0;
1321 }
1322}
1323
Yanray Wange6afd912022-10-27 12:11:18 +08001324int mbedtls_test_ssl_build_transforms(mbedtls_ssl_transform *t_in,
1325 mbedtls_ssl_transform *t_out,
1326 int cipher_type, int hash_id,
1327 int etm, int tag_mode,
1328 mbedtls_ssl_protocol_version tls_version,
1329 size_t cid0_len,
1330 size_t cid1_len)
1331{
Valerio Setti31ad3a12023-10-27 11:55:02 +02001332 mbedtls_cipher_mode_t cipher_mode = MBEDTLS_MODE_NONE;
1333 size_t key_bits = 0;
Yanray Wange6afd912022-10-27 12:11:18 +08001334 int ret = 0;
1335
1336#if defined(MBEDTLS_USE_PSA_CRYPTO)
1337 psa_key_type_t key_type;
1338 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1339 psa_algorithm_t alg;
Yanray Wange6afd912022-10-27 12:11:18 +08001340 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Valerio Setti3d59ebe2023-10-30 11:56:56 +01001341#else
Valerio Setti31ad3a12023-10-27 11:55:02 +02001342 mbedtls_cipher_info_t const *cipher_info;
Yanray Wange6afd912022-10-27 12:11:18 +08001343#endif
1344
Valerio Setti31ad3a12023-10-27 11:55:02 +02001345 size_t keylen, maclen, ivlen = 0;
Yanray Wange6afd912022-10-27 12:11:18 +08001346 unsigned char *key0 = NULL, *key1 = NULL;
1347 unsigned char *md0 = NULL, *md1 = NULL;
1348 unsigned char iv_enc[16], iv_dec[16];
1349
1350#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
1351 unsigned char cid0[SSL_CID_LEN_MIN];
1352 unsigned char cid1[SSL_CID_LEN_MIN];
1353
1354 mbedtls_test_rnd_std_rand(NULL, cid0, sizeof(cid0));
1355 mbedtls_test_rnd_std_rand(NULL, cid1, sizeof(cid1));
1356#else
1357 ((void) cid0_len);
1358 ((void) cid1_len);
1359#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
1360
1361 maclen = 0;
Valerio Setti31ad3a12023-10-27 11:55:02 +02001362 mbedtls_test_ssl_cipher_info_from_type((mbedtls_cipher_type_t) cipher_type,
1363 &cipher_mode, &key_bits, &ivlen);
Yanray Wange6afd912022-10-27 12:11:18 +08001364
1365 /* Pick keys */
Valerio Setti31ad3a12023-10-27 11:55:02 +02001366 keylen = key_bits / 8;
Yanray Wange6afd912022-10-27 12:11:18 +08001367 /* Allocate `keylen + 1` bytes to ensure that we get
1368 * a non-NULL pointers from `mbedtls_calloc` even if
1369 * `keylen == 0` in the case of the NULL cipher. */
1370 CHK((key0 = mbedtls_calloc(1, keylen + 1)) != NULL);
1371 CHK((key1 = mbedtls_calloc(1, keylen + 1)) != NULL);
1372 memset(key0, 0x1, keylen);
1373 memset(key1, 0x2, keylen);
1374
1375#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti31ad3a12023-10-27 11:55:02 +02001376 /* Pick cipher */
1377 cipher_info = mbedtls_cipher_info_from_type((mbedtls_cipher_type_t) cipher_type);
1378 CHK(cipher_info != NULL);
1379 CHK(mbedtls_cipher_info_get_iv_size(cipher_info) <= 16);
1380 CHK(mbedtls_cipher_info_get_key_bitlen(cipher_info) % 8 == 0);
Valerio Setti3d59ebe2023-10-30 11:56:56 +01001381
Yanray Wange6afd912022-10-27 12:11:18 +08001382 /* Setup cipher contexts */
1383 CHK(mbedtls_cipher_setup(&t_in->cipher_ctx_enc, cipher_info) == 0);
1384 CHK(mbedtls_cipher_setup(&t_in->cipher_ctx_dec, cipher_info) == 0);
1385 CHK(mbedtls_cipher_setup(&t_out->cipher_ctx_enc, cipher_info) == 0);
1386 CHK(mbedtls_cipher_setup(&t_out->cipher_ctx_dec, cipher_info) == 0);
1387
1388#if defined(MBEDTLS_CIPHER_MODE_CBC)
Valerio Setti31ad3a12023-10-27 11:55:02 +02001389 if (cipher_mode == MBEDTLS_MODE_CBC) {
Yanray Wange6afd912022-10-27 12:11:18 +08001390 CHK(mbedtls_cipher_set_padding_mode(&t_in->cipher_ctx_enc,
1391 MBEDTLS_PADDING_NONE) == 0);
1392 CHK(mbedtls_cipher_set_padding_mode(&t_in->cipher_ctx_dec,
1393 MBEDTLS_PADDING_NONE) == 0);
1394 CHK(mbedtls_cipher_set_padding_mode(&t_out->cipher_ctx_enc,
1395 MBEDTLS_PADDING_NONE) == 0);
1396 CHK(mbedtls_cipher_set_padding_mode(&t_out->cipher_ctx_dec,
1397 MBEDTLS_PADDING_NONE) == 0);
1398 }
1399#endif /* MBEDTLS_CIPHER_MODE_CBC */
1400
1401 CHK(mbedtls_cipher_setkey(&t_in->cipher_ctx_enc, key0,
Yanray Wanga8f445e2022-11-03 11:51:59 +08001402 (keylen << 3 > INT_MAX) ? INT_MAX : (int) keylen << 3,
1403 MBEDTLS_ENCRYPT)
1404 == 0);
Yanray Wange6afd912022-10-27 12:11:18 +08001405 CHK(mbedtls_cipher_setkey(&t_in->cipher_ctx_dec, key1,
Yanray Wanga8f445e2022-11-03 11:51:59 +08001406 (keylen << 3 > INT_MAX) ? INT_MAX : (int) keylen << 3,
1407 MBEDTLS_DECRYPT)
1408 == 0);
Yanray Wange6afd912022-10-27 12:11:18 +08001409 CHK(mbedtls_cipher_setkey(&t_out->cipher_ctx_enc, key1,
Yanray Wanga8f445e2022-11-03 11:51:59 +08001410 (keylen << 3 > INT_MAX) ? INT_MAX : (int) keylen << 3,
1411 MBEDTLS_ENCRYPT)
1412 == 0);
Yanray Wange6afd912022-10-27 12:11:18 +08001413 CHK(mbedtls_cipher_setkey(&t_out->cipher_ctx_dec, key0,
Yanray Wanga8f445e2022-11-03 11:51:59 +08001414 (keylen << 3 > INT_MAX) ? INT_MAX : (int) keylen << 3,
1415 MBEDTLS_DECRYPT)
1416 == 0);
Valerio Setti31ad3a12023-10-27 11:55:02 +02001417#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Yanray Wange6afd912022-10-27 12:11:18 +08001418
1419 /* Setup MAC contexts */
1420#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Valerio Setti31ad3a12023-10-27 11:55:02 +02001421 if (cipher_mode == MBEDTLS_MODE_CBC ||
1422 cipher_mode == MBEDTLS_MODE_STREAM) {
Yanray Wange6afd912022-10-27 12:11:18 +08001423#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Agathiyan Bragadeesh2f017a82023-07-12 11:21:54 +01001424 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 +08001425 CHK(md_info != NULL);
1426#endif
Agathiyan Bragadeesh2f017a82023-07-12 11:21:54 +01001427 maclen = mbedtls_md_get_size_from_type((mbedtls_md_type_t) hash_id);
Yanray Wange6afd912022-10-27 12:11:18 +08001428 CHK(maclen != 0);
1429 /* Pick hash keys */
1430 CHK((md0 = mbedtls_calloc(1, maclen)) != NULL);
1431 CHK((md1 = mbedtls_calloc(1, maclen)) != NULL);
1432 memset(md0, 0x5, maclen);
1433 memset(md1, 0x6, maclen);
1434
1435#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02001436 alg = mbedtls_md_psa_alg_from_type(hash_id);
Yanray Wange6afd912022-10-27 12:11:18 +08001437
1438 CHK(alg != 0);
1439
1440 t_out->psa_mac_alg = PSA_ALG_HMAC(alg);
1441 t_in->psa_mac_alg = PSA_ALG_HMAC(alg);
1442 t_in->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
1443 t_out->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
1444 t_in->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
1445 t_out->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
1446
1447 psa_reset_key_attributes(&attributes);
1448 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
1449 psa_set_key_algorithm(&attributes, PSA_ALG_HMAC(alg));
1450 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
1451
1452 CHK(psa_import_key(&attributes,
1453 md0, maclen,
1454 &t_in->psa_mac_enc) == PSA_SUCCESS);
1455
1456 CHK(psa_import_key(&attributes,
1457 md1, maclen,
1458 &t_out->psa_mac_enc) == PSA_SUCCESS);
1459
Valerio Setti31ad3a12023-10-27 11:55:02 +02001460 if (cipher_mode == MBEDTLS_MODE_STREAM ||
Yanray Wange6afd912022-10-27 12:11:18 +08001461 etm == MBEDTLS_SSL_ETM_DISABLED) {
1462 /* mbedtls_ct_hmac() requires the key to be exportable */
1463 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT |
1464 PSA_KEY_USAGE_VERIFY_HASH);
1465 } else {
1466 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
1467 }
1468
1469 CHK(psa_import_key(&attributes,
1470 md1, maclen,
1471 &t_in->psa_mac_dec) == PSA_SUCCESS);
1472
1473 CHK(psa_import_key(&attributes,
1474 md0, maclen,
1475 &t_out->psa_mac_dec) == PSA_SUCCESS);
1476#else
1477 CHK(mbedtls_md_setup(&t_out->md_ctx_enc, md_info, 1) == 0);
1478 CHK(mbedtls_md_setup(&t_out->md_ctx_dec, md_info, 1) == 0);
1479 CHK(mbedtls_md_setup(&t_in->md_ctx_enc, md_info, 1) == 0);
1480 CHK(mbedtls_md_setup(&t_in->md_ctx_dec, md_info, 1) == 0);
1481
1482 CHK(mbedtls_md_hmac_starts(&t_in->md_ctx_enc,
1483 md0, maclen) == 0);
1484 CHK(mbedtls_md_hmac_starts(&t_in->md_ctx_dec,
1485 md1, maclen) == 0);
1486 CHK(mbedtls_md_hmac_starts(&t_out->md_ctx_enc,
1487 md1, maclen) == 0);
1488 CHK(mbedtls_md_hmac_starts(&t_out->md_ctx_dec,
1489 md0, maclen) == 0);
1490#endif
1491 }
1492#else
1493 ((void) hash_id);
1494#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
1495
1496
1497 /* Pick IV's (regardless of whether they
1498 * are being used by the transform). */
Yanray Wange6afd912022-10-27 12:11:18 +08001499 memset(iv_enc, 0x3, sizeof(iv_enc));
1500 memset(iv_dec, 0x4, sizeof(iv_dec));
1501
1502 /*
1503 * Setup transforms
1504 */
1505
1506#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
1507 defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
1508 t_out->encrypt_then_mac = etm;
1509 t_in->encrypt_then_mac = etm;
1510#else
1511 ((void) etm);
1512#endif
1513
1514 t_out->tls_version = tls_version;
1515 t_in->tls_version = tls_version;
1516 t_out->ivlen = ivlen;
1517 t_in->ivlen = ivlen;
1518
Valerio Setti31ad3a12023-10-27 11:55:02 +02001519 switch (cipher_mode) {
Yanray Wange6afd912022-10-27 12:11:18 +08001520 case MBEDTLS_MODE_GCM:
1521 case MBEDTLS_MODE_CCM:
1522#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1523 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
1524 t_out->fixed_ivlen = 12;
1525 t_in->fixed_ivlen = 12;
1526 } else
1527#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1528 {
1529 t_out->fixed_ivlen = 4;
1530 t_in->fixed_ivlen = 4;
1531 }
1532 t_out->maclen = 0;
1533 t_in->maclen = 0;
1534 switch (tag_mode) {
1535 case 0: /* Full tag */
1536 t_out->taglen = 16;
1537 t_in->taglen = 16;
1538 break;
1539 case 1: /* Partial tag */
1540 t_out->taglen = 8;
1541 t_in->taglen = 8;
1542 break;
1543 default:
1544 ret = 1;
1545 goto cleanup;
1546 }
1547 break;
1548
1549 case MBEDTLS_MODE_CHACHAPOLY:
1550 t_out->fixed_ivlen = 12;
1551 t_in->fixed_ivlen = 12;
1552 t_out->maclen = 0;
1553 t_in->maclen = 0;
1554 switch (tag_mode) {
1555 case 0: /* Full tag */
1556 t_out->taglen = 16;
1557 t_in->taglen = 16;
1558 break;
1559 case 1: /* Partial tag */
1560 t_out->taglen = 8;
1561 t_in->taglen = 8;
1562 break;
1563 default:
1564 ret = 1;
1565 goto cleanup;
1566 }
1567 break;
1568
1569 case MBEDTLS_MODE_STREAM:
1570 case MBEDTLS_MODE_CBC:
1571 t_out->fixed_ivlen = 0; /* redundant, must be 0 */
1572 t_in->fixed_ivlen = 0; /* redundant, must be 0 */
1573 t_out->taglen = 0;
1574 t_in->taglen = 0;
1575 switch (tag_mode) {
1576 case 0: /* Full tag */
1577 t_out->maclen = maclen;
1578 t_in->maclen = maclen;
1579 break;
1580 default:
1581 ret = 1;
1582 goto cleanup;
1583 }
1584 break;
1585 default:
1586 ret = 1;
1587 goto cleanup;
1588 break;
1589 }
1590
1591 /* Setup IV's */
1592
1593 memcpy(&t_in->iv_dec, iv_dec, sizeof(iv_dec));
1594 memcpy(&t_in->iv_enc, iv_enc, sizeof(iv_enc));
1595 memcpy(&t_out->iv_dec, iv_enc, sizeof(iv_enc));
1596 memcpy(&t_out->iv_enc, iv_dec, sizeof(iv_dec));
1597
1598#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
1599 /* Add CID */
1600 memcpy(&t_in->in_cid, cid0, cid0_len);
1601 memcpy(&t_in->out_cid, cid1, cid1_len);
Yanray Wanga8f445e2022-11-03 11:51:59 +08001602 t_in->in_cid_len = (uint8_t) cid0_len;
1603 t_in->out_cid_len = (uint8_t) cid1_len;
Yanray Wange6afd912022-10-27 12:11:18 +08001604 memcpy(&t_out->in_cid, cid1, cid1_len);
1605 memcpy(&t_out->out_cid, cid0, cid0_len);
Yanray Wanga8f445e2022-11-03 11:51:59 +08001606 t_out->in_cid_len = (uint8_t) cid1_len;
1607 t_out->out_cid_len = (uint8_t) cid0_len;
Yanray Wange6afd912022-10-27 12:11:18 +08001608#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
1609
1610#if defined(MBEDTLS_USE_PSA_CRYPTO)
1611 status = mbedtls_ssl_cipher_to_psa(cipher_type,
1612 t_in->taglen,
1613 &alg,
1614 &key_type,
1615 &key_bits);
1616
1617 if (status != PSA_SUCCESS) {
1618 ret = PSA_TO_MBEDTLS_ERR(status);
1619 goto cleanup;
1620 }
1621
1622 t_in->psa_alg = alg;
1623 t_out->psa_alg = alg;
1624
1625 if (alg != MBEDTLS_SSL_NULL_CIPHER) {
1626 psa_reset_key_attributes(&attributes);
1627 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
1628 psa_set_key_algorithm(&attributes, alg);
1629 psa_set_key_type(&attributes, key_type);
1630
1631 status = psa_import_key(&attributes,
1632 key0,
1633 PSA_BITS_TO_BYTES(key_bits),
1634 &t_in->psa_key_enc);
1635
1636 if (status != PSA_SUCCESS) {
1637 ret = PSA_TO_MBEDTLS_ERR(status);
1638 goto cleanup;
1639 }
1640
1641 status = psa_import_key(&attributes,
1642 key1,
1643 PSA_BITS_TO_BYTES(key_bits),
1644 &t_out->psa_key_enc);
1645
1646 if (status != PSA_SUCCESS) {
1647 ret = PSA_TO_MBEDTLS_ERR(status);
1648 goto cleanup;
1649 }
1650
1651 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
1652
1653 status = psa_import_key(&attributes,
1654 key1,
1655 PSA_BITS_TO_BYTES(key_bits),
1656 &t_in->psa_key_dec);
1657
1658 if (status != PSA_SUCCESS) {
1659 ret = PSA_TO_MBEDTLS_ERR(status);
1660 goto cleanup;
1661 }
1662
1663 status = psa_import_key(&attributes,
1664 key0,
1665 PSA_BITS_TO_BYTES(key_bits),
1666 &t_out->psa_key_dec);
1667
1668 if (status != PSA_SUCCESS) {
1669 ret = PSA_TO_MBEDTLS_ERR(status);
1670 goto cleanup;
1671 }
1672 }
1673#endif /* MBEDTLS_USE_PSA_CRYPTO */
1674
1675cleanup:
1676
1677 mbedtls_free(key0);
1678 mbedtls_free(key1);
1679
1680 mbedtls_free(md0);
1681 mbedtls_free(md1);
1682
1683 return ret;
1684}
1685
Gilles Peskine9099d3f2023-09-18 13:11:50 +02001686#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
1687int mbedtls_test_ssl_prepare_record_mac(mbedtls_record *record,
1688 mbedtls_ssl_transform *transform_out)
1689{
1690#if defined(MBEDTLS_USE_PSA_CRYPTO)
1691 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1692#endif
1693
1694 /* Serialized version of record header for MAC purposes */
1695 unsigned char add_data[13];
1696 memcpy(add_data, record->ctr, 8);
1697 add_data[8] = record->type;
1698 add_data[9] = record->ver[0];
1699 add_data[10] = record->ver[1];
1700 add_data[11] = (record->data_len >> 8) & 0xff;
1701 add_data[12] = (record->data_len >> 0) & 0xff;
1702
1703 /* MAC with additional data */
1704#if defined(MBEDTLS_USE_PSA_CRYPTO)
1705 size_t sign_mac_length = 0;
1706 TEST_EQUAL(PSA_SUCCESS, psa_mac_sign_setup(&operation,
1707 transform_out->psa_mac_enc,
1708 transform_out->psa_mac_alg));
1709 TEST_EQUAL(PSA_SUCCESS, psa_mac_update(&operation, add_data, 13));
1710 TEST_EQUAL(PSA_SUCCESS, psa_mac_update(&operation,
1711 record->buf + record->data_offset,
1712 record->data_len));
1713 /* Use a temporary buffer for the MAC, because with the truncated HMAC
1714 * extension, there might not be enough room in the record for the
1715 * full-length MAC. */
1716 unsigned char mac[PSA_HASH_MAX_SIZE];
1717 TEST_EQUAL(PSA_SUCCESS, psa_mac_sign_finish(&operation,
1718 mac, sizeof(mac),
1719 &sign_mac_length));
1720#else
1721 TEST_EQUAL(0, mbedtls_md_hmac_update(&transform_out->md_ctx_enc, add_data, 13));
1722 TEST_EQUAL(0, mbedtls_md_hmac_update(&transform_out->md_ctx_enc,
1723 record->buf + record->data_offset,
1724 record->data_len));
1725 /* Use a temporary buffer for the MAC, because with the truncated HMAC
1726 * extension, there might not be enough room in the record for the
1727 * full-length MAC. */
1728 unsigned char mac[MBEDTLS_MD_MAX_SIZE];
1729 TEST_EQUAL(0, mbedtls_md_hmac_finish(&transform_out->md_ctx_enc, mac));
1730#endif
1731 memcpy(record->buf + record->data_offset + record->data_len, mac, transform_out->maclen);
1732 record->data_len += transform_out->maclen;
1733
1734 return 0;
1735
1736exit:
1737#if defined(MBEDTLS_USE_PSA_CRYPTO)
1738 psa_mac_abort(&operation);
1739#endif
1740 return -1;
1741}
1742#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
1743
Jerry Yu28547c42023-10-31 14:42:50 +08001744#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Yanray Wange6afd912022-10-27 12:11:18 +08001745int mbedtls_test_ssl_tls12_populate_session(mbedtls_ssl_session *session,
1746 int ticket_len,
Ronald Cron7b1921a2023-11-23 12:31:56 +01001747 int endpoint_type,
Yanray Wange6afd912022-10-27 12:11:18 +08001748 const char *crt_file)
1749{
Ronald Cronc57f86e2023-11-22 09:50:01 +01001750 (void) ticket_len;
1751
Yanray Wange6afd912022-10-27 12:11:18 +08001752#if defined(MBEDTLS_HAVE_TIME)
1753 session->start = mbedtls_time(NULL) - 42;
1754#endif
1755 session->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Ronald Cronc7fa82e2024-02-09 09:33:09 +01001756
1757 TEST_ASSERT(endpoint_type == MBEDTLS_SSL_IS_CLIENT ||
1758 endpoint_type == MBEDTLS_SSL_IS_SERVER);
1759
1760 session->endpoint = endpoint_type;
Yanray Wange6afd912022-10-27 12:11:18 +08001761 session->ciphersuite = 0xabcd;
1762 session->id_len = sizeof(session->id);
1763 memset(session->id, 66, session->id_len);
1764 memset(session->master, 17, sizeof(session->master));
1765
1766#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) && defined(MBEDTLS_FS_IO)
1767 if (crt_file != NULL && strlen(crt_file) != 0) {
1768 mbedtls_x509_crt tmp_crt;
1769 int ret;
1770
1771 mbedtls_x509_crt_init(&tmp_crt);
1772 ret = mbedtls_x509_crt_parse_file(&tmp_crt, crt_file);
1773 if (ret != 0) {
1774 return ret;
1775 }
1776
1777#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1778 /* Move temporary CRT. */
1779 session->peer_cert = mbedtls_calloc(1, sizeof(*session->peer_cert));
1780 if (session->peer_cert == NULL) {
1781 return -1;
1782 }
1783 *session->peer_cert = tmp_crt;
1784 memset(&tmp_crt, 0, sizeof(tmp_crt));
1785#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
1786 /* Calculate digest of temporary CRT. */
1787 session->peer_cert_digest =
1788 mbedtls_calloc(1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN);
1789 if (session->peer_cert_digest == NULL) {
1790 return -1;
1791 }
1792
1793#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02001794 psa_algorithm_t psa_alg = mbedtls_md_psa_alg_from_type(
Yanray Wange6afd912022-10-27 12:11:18 +08001795 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE);
1796 size_t hash_size = 0;
1797 psa_status_t status = psa_hash_compute(
1798 psa_alg, tmp_crt.raw.p,
1799 tmp_crt.raw.len,
1800 session->peer_cert_digest,
1801 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN,
1802 &hash_size);
1803 ret = PSA_TO_MBEDTLS_ERR(status);
1804#else
1805 ret = mbedtls_md(mbedtls_md_info_from_type(
1806 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE),
1807 tmp_crt.raw.p, tmp_crt.raw.len,
1808 session->peer_cert_digest);
1809#endif /* MBEDTLS_USE_PSA_CRYPTO */
1810 if (ret != 0) {
1811 return ret;
1812 }
1813 session->peer_cert_digest_type =
1814 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
1815 session->peer_cert_digest_len =
1816 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
1817#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
1818
1819 mbedtls_x509_crt_free(&tmp_crt);
1820 }
1821#else /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED && MBEDTLS_FS_IO */
1822 (void) crt_file;
1823#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED && MBEDTLS_FS_IO */
1824 session->verify_result = 0xdeadbeef;
1825
Ronald Cronc57f86e2023-11-22 09:50:01 +01001826#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1827#if defined(MBEDTLS_SSL_CLI_C)
Yanray Wange6afd912022-10-27 12:11:18 +08001828 if (ticket_len != 0) {
1829 session->ticket = mbedtls_calloc(1, ticket_len);
1830 if (session->ticket == NULL) {
1831 return -1;
1832 }
1833 memset(session->ticket, 33, ticket_len);
1834 }
1835 session->ticket_len = ticket_len;
1836 session->ticket_lifetime = 86401;
Ronald Cronc57f86e2023-11-22 09:50:01 +01001837#endif /* MBEDTLS_SSL_CLI_C */
1838
1839#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_HAVE_TIME)
1840 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
1841 session->ticket_creation_time = mbedtls_ms_time() - 42;
1842 }
Yanray Wange6afd912022-10-27 12:11:18 +08001843#endif
Ronald Cronc57f86e2023-11-22 09:50:01 +01001844#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Yanray Wange6afd912022-10-27 12:11:18 +08001845
1846#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1847 session->mfl_code = 1;
1848#endif
1849#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1850 session->encrypt_then_mac = 1;
1851#endif
1852
Ronald Cronc7fa82e2024-02-09 09:33:09 +01001853exit:
Yanray Wange6afd912022-10-27 12:11:18 +08001854 return 0;
1855}
Jerry Yu28547c42023-10-31 14:42:50 +08001856#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Yanray Wange6afd912022-10-27 12:11:18 +08001857
1858#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1859int mbedtls_test_ssl_tls13_populate_session(mbedtls_ssl_session *session,
1860 int ticket_len,
1861 int endpoint_type)
1862{
1863 ((void) ticket_len);
1864 session->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
1865 session->endpoint = endpoint_type == MBEDTLS_SSL_IS_CLIENT ?
1866 MBEDTLS_SSL_IS_CLIENT : MBEDTLS_SSL_IS_SERVER;
1867 session->ciphersuite = 0xabcd;
Ronald Cron18b92a12024-03-26 10:15:08 +01001868
1869#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Yanray Wange6afd912022-10-27 12:11:18 +08001870 session->ticket_age_add = 0x87654321;
1871 session->ticket_flags = 0x7;
Yanray Wange6afd912022-10-27 12:11:18 +08001872 session->resumption_key_len = 32;
1873 memset(session->resumption_key, 0x99, sizeof(session->resumption_key));
Yanray Wange6afd912022-10-27 12:11:18 +08001874#endif
1875
Ronald Cron18b92a12024-03-26 10:15:08 +01001876#if defined(MBEDTLS_SSL_SRV_C)
1877 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
1878#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1879#if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN)
1880 int ret = mbedtls_ssl_session_set_ticket_alpn(session, "ALPNExample");
1881 if (ret != 0) {
1882 return -1;
1883 }
1884#endif
1885#if defined(MBEDTLS_HAVE_TIME)
1886 session->ticket_creation_time = mbedtls_ms_time() - 42;
1887#endif
1888#endif /* MBEDTLS_SSL_SESSION_TICKETS */
1889 }
1890#endif /* MBEDTLS_SSL_SRV_C */
1891
Yanray Wange6afd912022-10-27 12:11:18 +08001892#if defined(MBEDTLS_SSL_CLI_C)
1893 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Ronald Cron18b92a12024-03-26 10:15:08 +01001894#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Yanray Wange6afd912022-10-27 12:11:18 +08001895#if defined(MBEDTLS_HAVE_TIME)
Jerry Yu342a5552023-11-10 14:23:39 +08001896 session->ticket_reception_time = mbedtls_ms_time() - 40;
Yanray Wange6afd912022-10-27 12:11:18 +08001897#endif
1898 session->ticket_lifetime = 0xfedcba98;
1899
1900 session->ticket_len = ticket_len;
1901 if (ticket_len != 0) {
1902 session->ticket = mbedtls_calloc(1, ticket_len);
1903 if (session->ticket == NULL) {
1904 return -1;
1905 }
1906 memset(session->ticket, 33, ticket_len);
1907 }
Ronald Cron8d15e012024-03-27 09:30:13 +01001908#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1909 char hostname[] = "hostname example";
1910 session->hostname = mbedtls_calloc(1, sizeof(hostname));
1911 if (session->hostname == NULL) {
1912 return -1;
1913 }
1914 memcpy(session->hostname, hostname, sizeof(hostname));
1915#endif
Ronald Cron18b92a12024-03-26 10:15:08 +01001916#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Yanray Wange6afd912022-10-27 12:11:18 +08001917 }
1918#endif /* MBEDTLS_SSL_CLI_C */
1919
Ronald Cron18b92a12024-03-26 10:15:08 +01001920#if defined(MBEDTLS_SSL_EARLY_DATA)
1921 session->max_early_data_size = 0x87654321;
1922#endif /* MBEDTLS_SSL_EARLY_DATA */
1923
Waleed Elmelegy049cd302023-12-20 17:28:31 +00001924#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
1925 session->record_size_limit = 2048;
1926#endif
1927
Yanray Wange6afd912022-10-27 12:11:18 +08001928 return 0;
1929}
1930#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1931
Yanray Wangb088bfc2023-03-16 12:15:49 +08001932int mbedtls_test_ssl_exchange_data(
1933 mbedtls_ssl_context *ssl_1,
1934 int msg_len_1, const int expected_fragments_1,
1935 mbedtls_ssl_context *ssl_2,
1936 int msg_len_2, const int expected_fragments_2)
Yanray Wange6afd912022-10-27 12:11:18 +08001937{
1938 unsigned char *msg_buf_1 = malloc(msg_len_1);
1939 unsigned char *msg_buf_2 = malloc(msg_len_2);
1940 unsigned char *in_buf_1 = malloc(msg_len_2);
1941 unsigned char *in_buf_2 = malloc(msg_len_1);
1942 int msg_type, ret = -1;
1943
1944 /* Perform this test with two message types. At first use a message
1945 * consisting of only 0x00 for the client and only 0xFF for the server.
1946 * At the second time use message with generated data */
1947 for (msg_type = 0; msg_type < 2; msg_type++) {
1948 int written_1 = 0;
1949 int written_2 = 0;
1950 int read_1 = 0;
1951 int read_2 = 0;
1952 int fragments_1 = 0;
1953 int fragments_2 = 0;
1954
1955 if (msg_type == 0) {
1956 memset(msg_buf_1, 0x00, msg_len_1);
1957 memset(msg_buf_2, 0xff, msg_len_2);
1958 } else {
1959 int i, j = 0;
1960 for (i = 0; i < msg_len_1; i++) {
1961 msg_buf_1[i] = j++ & 0xFF;
1962 }
1963 for (i = 0; i < msg_len_2; i++) {
1964 msg_buf_2[i] = (j -= 5) & 0xFF;
1965 }
1966 }
1967
1968 while (read_1 < msg_len_2 || read_2 < msg_len_1) {
1969 /* ssl_1 sending */
1970 if (msg_len_1 > written_1) {
1971 ret = mbedtls_ssl_write_fragment(ssl_1, msg_buf_1,
1972 msg_len_1, &written_1,
1973 expected_fragments_1);
1974 if (expected_fragments_1 == 0) {
1975 /* This error is expected when the message is too large and
1976 * cannot be fragmented */
Gilles Peskine353eb332025-05-14 17:42:53 +02001977 TEST_EQUAL(ret, MBEDTLS_ERR_SSL_BAD_INPUT_DATA);
Yanray Wange6afd912022-10-27 12:11:18 +08001978 msg_len_1 = 0;
1979 } else {
Gilles Peskine353eb332025-05-14 17:42:53 +02001980 TEST_EQUAL(ret, 0);
Yanray Wange6afd912022-10-27 12:11:18 +08001981 }
1982 }
1983
1984 /* ssl_2 sending */
1985 if (msg_len_2 > written_2) {
1986 ret = mbedtls_ssl_write_fragment(ssl_2, msg_buf_2,
1987 msg_len_2, &written_2,
1988 expected_fragments_2);
1989 if (expected_fragments_2 == 0) {
1990 /* This error is expected when the message is too large and
1991 * cannot be fragmented */
Gilles Peskine353eb332025-05-14 17:42:53 +02001992 TEST_EQUAL(ret, MBEDTLS_ERR_SSL_BAD_INPUT_DATA);
Yanray Wange6afd912022-10-27 12:11:18 +08001993 msg_len_2 = 0;
1994 } else {
Gilles Peskine353eb332025-05-14 17:42:53 +02001995 TEST_EQUAL(ret, 0);
Yanray Wange6afd912022-10-27 12:11:18 +08001996 }
1997 }
1998
1999 /* ssl_1 reading */
2000 if (read_1 < msg_len_2) {
2001 ret = mbedtls_ssl_read_fragment(ssl_1, in_buf_1,
2002 msg_len_2, &read_1,
2003 &fragments_2,
2004 expected_fragments_2);
Gilles Peskine353eb332025-05-14 17:42:53 +02002005 TEST_EQUAL(ret, 0);
Yanray Wange6afd912022-10-27 12:11:18 +08002006 }
2007
2008 /* ssl_2 reading */
2009 if (read_2 < msg_len_1) {
2010 ret = mbedtls_ssl_read_fragment(ssl_2, in_buf_2,
2011 msg_len_1, &read_2,
2012 &fragments_1,
2013 expected_fragments_1);
Gilles Peskine353eb332025-05-14 17:42:53 +02002014 TEST_EQUAL(ret, 0);
Yanray Wange6afd912022-10-27 12:11:18 +08002015 }
2016 }
2017
2018 ret = -1;
Gilles Peskine353eb332025-05-14 17:42:53 +02002019 TEST_EQUAL(0, memcmp(msg_buf_1, in_buf_2, msg_len_1));
2020 TEST_EQUAL(0, memcmp(msg_buf_2, in_buf_1, msg_len_2));
2021 TEST_EQUAL(fragments_1, expected_fragments_1);
2022 TEST_EQUAL(fragments_2, expected_fragments_2);
Yanray Wange6afd912022-10-27 12:11:18 +08002023 }
2024
2025 ret = 0;
2026
2027exit:
2028 free(msg_buf_1);
2029 free(in_buf_1);
2030 free(msg_buf_2);
2031 free(in_buf_2);
2032
2033 return ret;
2034}
2035
2036/*
2037 * Perform data exchanging between \p ssl_1 and \p ssl_2. Both of endpoints
2038 * must be initialized and connected beforehand.
2039 *
2040 * \retval 0 on success, otherwise error code.
2041 */
Yanray Wangead70c82023-03-16 12:04:49 +08002042#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) && \
2043 (defined(MBEDTLS_SSL_RENEGOTIATION) || \
2044 defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH))
2045static int exchange_data(mbedtls_ssl_context *ssl_1,
2046 mbedtls_ssl_context *ssl_2)
Yanray Wange6afd912022-10-27 12:11:18 +08002047{
Yanray Wangb088bfc2023-03-16 12:15:49 +08002048 return mbedtls_test_ssl_exchange_data(ssl_1, 256, 1,
2049 ssl_2, 256, 1);
Yanray Wange6afd912022-10-27 12:11:18 +08002050}
Yanray Wangead70c82023-03-16 12:04:49 +08002051#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED &&
2052 (MBEDTLS_SSL_RENEGOTIATION ||
2053 MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) */
Yanray Wange6afd912022-10-27 12:11:18 +08002054
2055#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
2056static int check_ssl_version(
2057 mbedtls_ssl_protocol_version expected_negotiated_version,
Gilles Peskinebd953402025-05-28 15:20:28 +02002058 const mbedtls_ssl_context *client,
2059 const mbedtls_ssl_context *server)
Yanray Wange6afd912022-10-27 12:11:18 +08002060{
Gilles Peskinebd953402025-05-28 15:20:28 +02002061 /* First check that both sides have chosen the same version.
2062 * If so, we can make more sanity checks just on one side.
2063 * If not, something is deeply wrong. */
2064 TEST_EQUAL(client->tls_version, server->tls_version);
2065
2066 /* Make further checks on the client to validate that the
2067 * reported data about the version is correct. */
2068 const char *version_string = mbedtls_ssl_get_version(client);
Yanray Wange6afd912022-10-27 12:11:18 +08002069 mbedtls_ssl_protocol_version version_number =
Gilles Peskinebd953402025-05-28 15:20:28 +02002070 mbedtls_ssl_get_version_number(client);
Yanray Wange6afd912022-10-27 12:11:18 +08002071
Gilles Peskinebd953402025-05-28 15:20:28 +02002072 TEST_EQUAL(client->tls_version, expected_negotiated_version);
Yanray Wange6afd912022-10-27 12:11:18 +08002073
Gilles Peskinebd953402025-05-28 15:20:28 +02002074 if (client->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Yanray Wange6afd912022-10-27 12:11:18 +08002075 TEST_EQUAL(version_string[0], 'D');
2076 ++version_string;
2077 }
2078
2079 switch (expected_negotiated_version) {
2080 case MBEDTLS_SSL_VERSION_TLS1_2:
2081 TEST_EQUAL(version_number, MBEDTLS_SSL_VERSION_TLS1_2);
Gilles Peskine353eb332025-05-14 17:42:53 +02002082 TEST_EQUAL(strcmp(version_string, "TLSv1.2"), 0);
Yanray Wange6afd912022-10-27 12:11:18 +08002083 break;
2084
2085 case MBEDTLS_SSL_VERSION_TLS1_3:
2086 TEST_EQUAL(version_number, MBEDTLS_SSL_VERSION_TLS1_3);
Gilles Peskine353eb332025-05-14 17:42:53 +02002087 TEST_EQUAL(strcmp(version_string, "TLSv1.3"), 0);
Yanray Wange6afd912022-10-27 12:11:18 +08002088 break;
2089
2090 default:
Agathiyan Bragadeeshdc28a5a2023-07-18 11:45:28 +01002091 TEST_FAIL(
Agathiyan Bragadeeshebb40bc2023-07-14 17:28:27 +01002092 "Version check not implemented for this protocol version");
Yanray Wange6afd912022-10-27 12:11:18 +08002093 }
2094
2095 return 1;
2096
2097exit:
2098 return 0;
2099}
2100#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
2101
Yanray Wange6afd912022-10-27 12:11:18 +08002102#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Max Fillingercf007ca2024-10-29 16:57:09 +01002103int mbedtls_test_ssl_do_handshake_with_endpoints(
2104 mbedtls_test_ssl_endpoint *server_ep,
2105 mbedtls_test_ssl_endpoint *client_ep,
Max Fillinger8f12e312024-10-30 00:29:37 +01002106 mbedtls_test_handshake_test_options *options,
Max Fillingercf007ca2024-10-29 16:57:09 +01002107 mbedtls_ssl_protocol_version proto)
2108{
2109 enum { BUFFSIZE = 1024 };
2110
2111 int ret = -1;
Max Fillingercf007ca2024-10-29 16:57:09 +01002112
Max Fillingeree467aa2024-11-08 22:17:33 +01002113 mbedtls_platform_zeroize(server_ep, sizeof(mbedtls_test_ssl_endpoint));
2114 mbedtls_platform_zeroize(client_ep, sizeof(mbedtls_test_ssl_endpoint));
2115
Max Fillinger8f12e312024-10-30 00:29:37 +01002116 mbedtls_test_init_handshake_options(options);
2117 options->server_min_version = proto;
2118 options->client_min_version = proto;
2119 options->server_max_version = proto;
2120 options->client_max_version = proto;
Max Fillingercf007ca2024-10-29 16:57:09 +01002121
Gilles Peskineca8a9ac2025-05-27 20:52:24 +02002122 ret = mbedtls_test_ssl_endpoint_init(client_ep, MBEDTLS_SSL_IS_CLIENT, options);
Max Fillingercf007ca2024-10-29 16:57:09 +01002123 if (ret != 0) {
2124 return ret;
2125 }
Gilles Peskineca8a9ac2025-05-27 20:52:24 +02002126 ret = mbedtls_test_ssl_endpoint_init(server_ep, MBEDTLS_SSL_IS_SERVER, options);
Max Fillingercf007ca2024-10-29 16:57:09 +01002127 if (ret != 0) {
2128 return ret;
2129 }
2130
2131 ret = mbedtls_test_mock_socket_connect(&client_ep->socket, &server_ep->socket, BUFFSIZE);
2132 if (ret != 0) {
2133 return ret;
2134 }
2135
Max Fillingerea1e7772024-10-30 00:49:10 +01002136 ret = mbedtls_test_move_handshake_to_state(&server_ep->ssl,
2137 &client_ep->ssl,
2138 MBEDTLS_SSL_HANDSHAKE_OVER);
Max Fillingercf007ca2024-10-29 16:57:09 +01002139 if (ret != 0 && ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
2140 return ret;
2141 }
Max Fillingerea1e7772024-10-30 00:49:10 +01002142 ret = mbedtls_test_move_handshake_to_state(&client_ep->ssl,
2143 &server_ep->ssl,
2144 MBEDTLS_SSL_HANDSHAKE_OVER);
Max Fillingercf007ca2024-10-29 16:57:09 +01002145 if (ret != 0 && ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
2146 return ret;
2147 }
Max Fillingerea1e7772024-10-30 00:49:10 +01002148 if (!mbedtls_ssl_is_handshake_over(&client_ep->ssl) ||
2149 !mbedtls_ssl_is_handshake_over(&server_ep->ssl)) {
Max Fillingercf007ca2024-10-29 16:57:09 +01002150 return -1;
2151 }
2152
2153 return 0;
2154}
2155#endif /* defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) */
2156
2157#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gilles Peskine78df6ae2025-05-27 21:14:25 +02002158
2159#if defined(MBEDTLS_SSL_RENEGOTIATION)
2160static int test_renegotiation(const mbedtls_test_handshake_test_options *options,
2161 mbedtls_test_ssl_endpoint *client,
2162 mbedtls_test_ssl_endpoint *server)
2163{
2164 int ok = 0;
2165 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2166
2167 (void) options; // only used in some configurations
2168
2169 /* Start test with renegotiation */
2170 TEST_EQUAL(server->ssl.renego_status,
2171 MBEDTLS_SSL_INITIAL_HANDSHAKE);
2172 TEST_EQUAL(client->ssl.renego_status,
2173 MBEDTLS_SSL_INITIAL_HANDSHAKE);
2174
2175 /* After calling this function for the server, it only sends a handshake
2176 * request. All renegotiation should happen during data exchanging */
2177 TEST_EQUAL(mbedtls_ssl_renegotiate(&(server->ssl)), 0);
2178 TEST_EQUAL(server->ssl.renego_status,
2179 MBEDTLS_SSL_RENEGOTIATION_PENDING);
2180 TEST_EQUAL(client->ssl.renego_status,
2181 MBEDTLS_SSL_INITIAL_HANDSHAKE);
2182
2183 TEST_EQUAL(exchange_data(&(client->ssl), &(server->ssl)), 0);
2184 TEST_EQUAL(server->ssl.renego_status,
2185 MBEDTLS_SSL_RENEGOTIATION_DONE);
2186 TEST_EQUAL(client->ssl.renego_status,
2187 MBEDTLS_SSL_RENEGOTIATION_DONE);
2188
2189 /* After calling mbedtls_ssl_renegotiate for the client,
2190 * all renegotiation should happen inside this function.
2191 * However in this test, we cannot perform simultaneous communication
2192 * between client and server so this function will return waiting error
2193 * on the socket. All rest of renegotiation should happen
2194 * during data exchanging */
2195 ret = mbedtls_ssl_renegotiate(&(client->ssl));
2196#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2197 if (options->resize_buffers != 0) {
2198 /* Ensure that the buffer sizes are appropriate before resizes */
2199 TEST_EQUAL(client->ssl.out_buf_len, MBEDTLS_SSL_OUT_BUFFER_LEN);
2200 TEST_EQUAL(client->ssl.in_buf_len, MBEDTLS_SSL_IN_BUFFER_LEN);
2201 }
2202#endif
2203 TEST_ASSERT(ret == 0 ||
2204 ret == MBEDTLS_ERR_SSL_WANT_READ ||
2205 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
2206 TEST_EQUAL(server->ssl.renego_status,
2207 MBEDTLS_SSL_RENEGOTIATION_DONE);
2208 TEST_EQUAL(client->ssl.renego_status,
2209 MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS);
2210
2211 TEST_EQUAL(exchange_data(&(client->ssl), &(server->ssl)), 0);
2212 TEST_EQUAL(server->ssl.renego_status,
2213 MBEDTLS_SSL_RENEGOTIATION_DONE);
2214 TEST_EQUAL(client->ssl.renego_status,
2215 MBEDTLS_SSL_RENEGOTIATION_DONE);
2216#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2217 /* Validate buffer sizes after renegotiation */
2218 if (options->resize_buffers != 0) {
2219 TEST_EQUAL(client->ssl.out_buf_len,
2220 mbedtls_ssl_get_output_buflen(&client->ssl));
2221 TEST_EQUAL(client->ssl.in_buf_len,
2222 mbedtls_ssl_get_input_buflen(&client->ssl));
2223 TEST_EQUAL(server->ssl.out_buf_len,
2224 mbedtls_ssl_get_output_buflen(&server->ssl));
2225 TEST_EQUAL(server->ssl.in_buf_len,
2226 mbedtls_ssl_get_input_buflen(&server->ssl));
2227 }
2228#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
2229
2230 ok = 1;
2231
2232exit:
2233 return ok;
2234}
2235#endif /* MBEDTLS_SSL_RENEGOTIATION */
2236
Gilles Peskinee23a6d12025-05-27 21:17:09 +02002237#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
2238static int test_serialization(const mbedtls_test_handshake_test_options *options,
2239 mbedtls_test_ssl_endpoint *client,
2240 mbedtls_test_ssl_endpoint *server)
2241{
2242 int ok = 0;
2243 unsigned char *context_buf = NULL;
2244 size_t context_buf_len;
2245
2246 TEST_EQUAL(options->dtls, 1);
2247
2248 TEST_EQUAL(mbedtls_ssl_context_save(&(server->ssl), NULL,
2249 0, &context_buf_len),
2250 MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL);
2251
2252 context_buf = mbedtls_calloc(1, context_buf_len);
2253 TEST_ASSERT(context_buf != NULL);
2254
2255 TEST_EQUAL(mbedtls_ssl_context_save(&(server->ssl), context_buf,
2256 context_buf_len,
2257 &context_buf_len),
2258 0);
2259
2260 mbedtls_ssl_free(&(server->ssl));
2261 mbedtls_ssl_init(&(server->ssl));
2262
2263 TEST_EQUAL(mbedtls_ssl_setup(&(server->ssl), &(server->conf)), 0);
2264
2265 mbedtls_ssl_set_bio(&(server->ssl), &server->dtls_context,
2266 mbedtls_test_mock_tcp_send_msg,
2267 mbedtls_test_mock_tcp_recv_msg,
2268 NULL);
2269
2270 mbedtls_ssl_set_user_data_p(&server->ssl, server);
2271
2272#if defined(MBEDTLS_TIMING_C)
2273 mbedtls_ssl_set_timer_cb(&server->ssl, &server->timer,
2274 mbedtls_timing_set_delay,
2275 mbedtls_timing_get_delay);
2276#endif
2277#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2278 if (options->resize_buffers != 0) {
2279 /* Ensure that the buffer sizes are appropriate before resizes */
2280 TEST_EQUAL(server->ssl.out_buf_len, MBEDTLS_SSL_OUT_BUFFER_LEN);
2281 TEST_EQUAL(server->ssl.in_buf_len, MBEDTLS_SSL_IN_BUFFER_LEN);
2282 }
2283#endif
2284 TEST_EQUAL(mbedtls_ssl_context_load(&(server->ssl), context_buf,
2285 context_buf_len), 0);
2286
2287#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2288 /* Validate buffer sizes after context deserialization */
2289 if (options->resize_buffers != 0) {
2290 TEST_EQUAL(server->ssl.out_buf_len,
2291 mbedtls_ssl_get_output_buflen(&server->ssl));
2292 TEST_EQUAL(server->ssl.in_buf_len,
2293 mbedtls_ssl_get_input_buflen(&server->ssl));
2294 }
2295#endif
2296 /* Retest writing/reading */
2297 if (options->cli_msg_len != 0 || options->srv_msg_len != 0) {
2298 TEST_EQUAL(mbedtls_test_ssl_exchange_data(
2299 &(client->ssl), options->cli_msg_len,
2300 options->expected_cli_fragments,
2301 &(server->ssl), options->srv_msg_len,
2302 options->expected_srv_fragments),
2303 0);
2304 }
2305
2306 ok = 1;
2307
2308exit:
2309 mbedtls_free(context_buf);
2310 return ok;
2311}
2312#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
2313
Gilles Peskine7a8fd462025-05-28 15:41:54 +02002314int mbedtls_test_ssl_perform_connection(
2315 const mbedtls_test_handshake_test_options *options,
2316 mbedtls_test_ssl_endpoint *client,
2317 mbedtls_test_ssl_endpoint *server)
2318{
2319 enum { BUFFSIZE = 17000 };
2320 int expected_handshake_result = options->expected_handshake_result;
2321 int ok = 0;
2322
2323 TEST_EQUAL(mbedtls_test_mock_socket_connect(&(client->socket),
2324 &(server->socket),
2325 BUFFSIZE), 0);
2326
2327#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2328 if (options->resize_buffers != 0) {
2329 /* Ensure that the buffer sizes are appropriate before resizes */
2330 TEST_EQUAL(client->ssl.out_buf_len, MBEDTLS_SSL_OUT_BUFFER_LEN);
2331 TEST_EQUAL(client->ssl.in_buf_len, MBEDTLS_SSL_IN_BUFFER_LEN);
2332 TEST_EQUAL(server->ssl.out_buf_len, MBEDTLS_SSL_OUT_BUFFER_LEN);
2333 TEST_EQUAL(server->ssl.in_buf_len, MBEDTLS_SSL_IN_BUFFER_LEN);
2334 }
2335#endif
2336
2337 if (options->expected_negotiated_version == MBEDTLS_SSL_VERSION_UNKNOWN) {
2338 expected_handshake_result = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
2339 }
2340
2341 TEST_EQUAL(mbedtls_test_move_handshake_to_state(&(client->ssl),
2342 &(server->ssl),
2343 MBEDTLS_SSL_HANDSHAKE_OVER),
2344 expected_handshake_result);
2345
2346 if (expected_handshake_result != 0) {
2347 /* Connection will have failed by this point, skip to cleanup */
2348 ok = 1;
2349 goto exit;
2350 }
2351
2352 TEST_EQUAL(mbedtls_ssl_is_handshake_over(&client->ssl), 1);
2353
2354 /* Make sure server state is moved to HANDSHAKE_OVER also. */
2355 TEST_EQUAL(mbedtls_test_move_handshake_to_state(&(server->ssl),
2356 &(client->ssl),
2357 MBEDTLS_SSL_HANDSHAKE_OVER),
2358 0);
2359
2360 TEST_EQUAL(mbedtls_ssl_is_handshake_over(&server->ssl), 1);
2361
2362 /* Check that both sides have negotiated the expected version. */
2363 TEST_ASSERT(check_ssl_version(options->expected_negotiated_version,
2364 &client->ssl,
2365 &server->ssl));
2366
2367 if (options->expected_ciphersuite != 0) {
2368 TEST_EQUAL(server->ssl.session->ciphersuite,
2369 options->expected_ciphersuite);
2370 }
2371
2372#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2373 if (options->resize_buffers != 0) {
2374 /* A server, when using DTLS, might delay a buffer resize to happen
2375 * after it receives a message, so we force it. */
2376 TEST_EQUAL(exchange_data(&(client->ssl), &(server->ssl)), 0);
2377
2378 TEST_EQUAL(client->ssl.out_buf_len,
2379 mbedtls_ssl_get_output_buflen(&client->ssl));
2380 TEST_EQUAL(client->ssl.in_buf_len,
2381 mbedtls_ssl_get_input_buflen(&client->ssl));
2382 TEST_EQUAL(server->ssl.out_buf_len,
2383 mbedtls_ssl_get_output_buflen(&server->ssl));
2384 TEST_EQUAL(server->ssl.in_buf_len,
2385 mbedtls_ssl_get_input_buflen(&server->ssl));
2386 }
2387#endif
2388
2389 if (options->cli_msg_len != 0 || options->srv_msg_len != 0) {
2390 /* Start data exchanging test */
2391 TEST_EQUAL(mbedtls_test_ssl_exchange_data(
2392 &(client->ssl), options->cli_msg_len,
2393 options->expected_cli_fragments,
2394 &(server->ssl), options->srv_msg_len,
2395 options->expected_srv_fragments),
2396 0);
2397 }
2398#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
2399 if (options->serialize == 1) {
2400 TEST_ASSERT(test_serialization(options, client, server));
2401 }
2402#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
2403
2404#if defined(MBEDTLS_SSL_RENEGOTIATION)
2405 if (options->renegotiate) {
2406 TEST_ASSERT(test_renegotiation(options, client, server));
2407 }
2408#endif /* MBEDTLS_SSL_RENEGOTIATION */
2409
2410 ok = 1;
2411
2412exit:
2413 return ok;
2414}
2415
Yanray Wange6afd912022-10-27 12:11:18 +08002416void mbedtls_test_ssl_perform_handshake(
Gilles Peskine9b993682025-05-27 18:44:12 +02002417 const mbedtls_test_handshake_test_options *options)
Yanray Wange6afd912022-10-27 12:11:18 +08002418{
Gilles Peskinee30b5c72025-05-27 21:05:48 +02002419 mbedtls_test_ssl_endpoint client_struct;
2420 memset(&client_struct, 0, sizeof(client_struct));
2421 mbedtls_test_ssl_endpoint *const client = &client_struct;
2422 mbedtls_test_ssl_endpoint server_struct;
2423 memset(&server_struct, 0, sizeof(server_struct));
2424 mbedtls_test_ssl_endpoint *const server = &server_struct;
Yanray Wange6afd912022-10-27 12:11:18 +08002425
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +01002426 MD_OR_USE_PSA_INIT();
Yanray Wange6afd912022-10-27 12:11:18 +08002427
Ronald Cronec3408d2024-01-16 17:50:40 +01002428#if defined(MBEDTLS_DEBUG_C)
2429 if (options->cli_log_fun || options->srv_log_fun) {
2430 mbedtls_debug_set_threshold(4);
2431 }
2432#endif
2433
Yanray Wange6afd912022-10-27 12:11:18 +08002434 /* Client side */
Gilles Peskinee30b5c72025-05-27 21:05:48 +02002435 TEST_EQUAL(mbedtls_test_ssl_endpoint_init(client,
Gilles Peskine07432b92025-05-27 21:07:44 +02002436 MBEDTLS_SSL_IS_CLIENT,
2437 options), 0);
Yanray Wange6afd912022-10-27 12:11:18 +08002438
Yanray Wange6afd912022-10-27 12:11:18 +08002439 /* Server side */
Gilles Peskinee30b5c72025-05-27 21:05:48 +02002440 TEST_EQUAL(mbedtls_test_ssl_endpoint_init(server,
Gilles Peskine07432b92025-05-27 21:07:44 +02002441 MBEDTLS_SSL_IS_SERVER,
2442 options), 0);
Gilles Peskinee30b5c72025-05-27 21:05:48 +02002443 mbedtls_ssl_conf_authmode(&server->conf, options->srv_auth_mode);
Yanray Wange6afd912022-10-27 12:11:18 +08002444
Gilles Peskineb092e782025-05-27 20:15:03 +02002445 if (options->dtls) {
Gilles Peskinee30b5c72025-05-27 21:05:48 +02002446 TEST_EQUAL(mbedtls_test_ssl_dtls_join_endpoints(client, server), 0);
Gilles Peskineb092e782025-05-27 20:15:03 +02002447 }
2448
Gilles Peskine7a8fd462025-05-28 15:41:54 +02002449 TEST_ASSERT(mbedtls_test_ssl_perform_connection(options, client, server));
Yanray Wange6afd912022-10-27 12:11:18 +08002450
Gilles Peskinee30b5c72025-05-27 21:05:48 +02002451 TEST_ASSERT(mbedtls_ssl_conf_get_user_data_p(&client->conf) == client);
2452 TEST_ASSERT(mbedtls_ssl_get_user_data_p(&client->ssl) == client);
2453 TEST_ASSERT(mbedtls_ssl_conf_get_user_data_p(&server->conf) == server);
2454 TEST_ASSERT(mbedtls_ssl_get_user_data_p(&server->ssl) == server);
Yanray Wange6afd912022-10-27 12:11:18 +08002455
2456exit:
Gilles Peskinee30b5c72025-05-27 21:05:48 +02002457 mbedtls_test_ssl_endpoint_free(client);
2458 mbedtls_test_ssl_endpoint_free(server);
Yanray Wange6afd912022-10-27 12:11:18 +08002459#if defined(MBEDTLS_DEBUG_C)
2460 if (options->cli_log_fun || options->srv_log_fun) {
2461 mbedtls_debug_set_threshold(0);
2462 }
2463#endif
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +01002464 MD_OR_USE_PSA_DONE();
Yanray Wange6afd912022-10-27 12:11:18 +08002465}
2466#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
2467
2468#if defined(MBEDTLS_TEST_HOOKS)
Yanray Wangf56181a2023-03-16 12:21:33 +08002469int mbedtls_test_tweak_tls13_certificate_msg_vector_len(
Yanray Wange6afd912022-10-27 12:11:18 +08002470 unsigned char *buf, unsigned char **end, int tweak,
2471 int *expected_result, mbedtls_ssl_chk_buf_ptr_args *args)
2472{
2473/*
2474 * The definition of the tweaks assume that the certificate list contains only
2475 * one certificate.
2476 */
2477
2478/*
2479 * struct {
2480 * opaque cert_data<1..2^24-1>;
2481 * Extension extensions<0..2^16-1>;
2482 * } CertificateEntry;
2483 *
2484 * struct {
2485 * opaque certificate_request_context<0..2^8-1>;
2486 * CertificateEntry certificate_list<0..2^24-1>;
2487 * } Certificate;
2488 */
2489 unsigned char *p_certificate_request_context_len = buf;
2490 size_t certificate_request_context_len = buf[0];
2491
2492 unsigned char *p_certificate_list_len =
2493 buf + 1 + certificate_request_context_len;
2494 unsigned char *certificate_list = p_certificate_list_len + 3;
2495 size_t certificate_list_len =
2496 MBEDTLS_GET_UINT24_BE(p_certificate_list_len, 0);
2497
2498 unsigned char *p_cert_data_len = certificate_list;
2499 unsigned char *cert_data = p_cert_data_len + 3;
2500 size_t cert_data_len = MBEDTLS_GET_UINT24_BE(p_cert_data_len, 0);
2501
2502 unsigned char *p_extensions_len = cert_data + cert_data_len;
2503 unsigned char *extensions = p_extensions_len + 2;
2504 size_t extensions_len = MBEDTLS_GET_UINT16_BE(p_extensions_len, 0);
2505
2506 *expected_result = MBEDTLS_ERR_SSL_DECODE_ERROR;
2507
2508 switch (tweak) {
2509 case 1:
2510 /* Failure when checking if the certificate request context length
2511 * and certificate list length can be read
2512 */
2513 *end = buf + 3;
2514 set_chk_buf_ptr_args(args, buf, *end, 4);
2515 break;
2516
2517 case 2:
2518 /* Invalid certificate request context length.
2519 */
2520 *p_certificate_request_context_len =
Yanray Wanga8f445e2022-11-03 11:51:59 +08002521 (unsigned char) certificate_request_context_len + 1;
Yanray Wange6afd912022-10-27 12:11:18 +08002522 reset_chk_buf_ptr_args(args);
2523 break;
2524
2525 case 3:
2526 /* Failure when checking if certificate_list data can be read. */
2527 MBEDTLS_PUT_UINT24_BE(certificate_list_len + 1,
2528 p_certificate_list_len, 0);
2529 set_chk_buf_ptr_args(args, certificate_list, *end,
2530 certificate_list_len + 1);
2531 break;
2532
2533 case 4:
2534 /* Failure when checking if the cert_data length can be read. */
2535 MBEDTLS_PUT_UINT24_BE(2, p_certificate_list_len, 0);
2536 set_chk_buf_ptr_args(args, p_cert_data_len, certificate_list + 2, 3);
2537 break;
2538
2539 case 5:
2540 /* Failure when checking if cert_data data can be read. */
2541 MBEDTLS_PUT_UINT24_BE(certificate_list_len - 3 + 1,
2542 p_cert_data_len, 0);
2543 set_chk_buf_ptr_args(args, cert_data,
2544 certificate_list + certificate_list_len,
2545 certificate_list_len - 3 + 1);
2546 break;
2547
2548 case 6:
2549 /* Failure when checking if the extensions length can be read. */
2550 MBEDTLS_PUT_UINT24_BE(certificate_list_len - extensions_len - 1,
2551 p_certificate_list_len, 0);
2552 set_chk_buf_ptr_args(
2553 args, p_extensions_len,
2554 certificate_list + certificate_list_len - extensions_len - 1, 2);
2555 break;
2556
2557 case 7:
2558 /* Failure when checking if extensions data can be read. */
2559 MBEDTLS_PUT_UINT16_BE(extensions_len + 1, p_extensions_len, 0);
2560
2561 set_chk_buf_ptr_args(
2562 args, extensions,
2563 certificate_list + certificate_list_len, extensions_len + 1);
2564 break;
2565
2566 default:
2567 return -1;
2568 }
2569
2570 return 0;
2571}
2572#endif /* MBEDTLS_TEST_HOOKS */
Ronald Cron77abfe62024-01-15 11:17:31 +01002573
Ronald Cronfaf026c2024-01-31 14:32:06 +01002574/*
2575 * Functions for tests based on tickets. Implementations of the
2576 * write/parse ticket interfaces as defined by mbedtls_ssl_ticket_write/parse_t.
2577 * Basically same implementations as in ticket.c without the encryption. That
2578 * way we can tweak easily tickets characteristics to simulate misbehaving
2579 * peers.
2580 */
Ronald Cron77abfe62024-01-15 11:17:31 +01002581#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2582int mbedtls_test_ticket_write(
2583 void *p_ticket, const mbedtls_ssl_session *session,
2584 unsigned char *start, const unsigned char *end,
2585 size_t *tlen, uint32_t *lifetime)
2586{
2587 int ret;
2588 ((void) p_ticket);
2589
2590 if ((ret = mbedtls_ssl_session_save(session, start, end - start,
2591 tlen)) != 0) {
2592 return ret;
2593 }
2594
2595 /* Maximum ticket lifetime as defined in RFC 8446 */
2596 *lifetime = 7 * 24 * 3600;
2597
2598 return 0;
2599}
2600
2601int mbedtls_test_ticket_parse(void *p_ticket, mbedtls_ssl_session *session,
2602 unsigned char *buf, size_t len)
2603{
2604 ((void) p_ticket);
2605
2606 return mbedtls_ssl_session_load(session, buf, len);
2607}
2608#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Ronald Cron1f6e4e42024-01-26 16:31:33 +01002609
2610#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SRV_C) && \
2611 defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS) && \
2612 defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
2613int mbedtls_test_get_tls13_ticket(
2614 mbedtls_test_handshake_test_options *client_options,
2615 mbedtls_test_handshake_test_options *server_options,
2616 mbedtls_ssl_session *session)
2617{
2618 int ret = -1;
Gilles Peskine946bf142025-04-08 09:48:40 +02002619 int ok = 0;
Ronald Cron1f6e4e42024-01-26 16:31:33 +01002620 unsigned char buf[64];
2621 mbedtls_test_ssl_endpoint client_ep, server_ep;
2622
2623 mbedtls_platform_zeroize(&client_ep, sizeof(client_ep));
2624 mbedtls_platform_zeroize(&server_ep, sizeof(server_ep));
2625
2626 ret = mbedtls_test_ssl_endpoint_init(&client_ep, MBEDTLS_SSL_IS_CLIENT,
Gilles Peskineca8a9ac2025-05-27 20:52:24 +02002627 client_options);
Ronald Cron1f6e4e42024-01-26 16:31:33 +01002628 TEST_EQUAL(ret, 0);
2629
2630 ret = mbedtls_test_ssl_endpoint_init(&server_ep, MBEDTLS_SSL_IS_SERVER,
Gilles Peskineca8a9ac2025-05-27 20:52:24 +02002631 server_options);
Ronald Cron1f6e4e42024-01-26 16:31:33 +01002632 TEST_EQUAL(ret, 0);
2633
2634 mbedtls_ssl_conf_session_tickets_cb(&server_ep.conf,
2635 mbedtls_test_ticket_write,
2636 mbedtls_test_ticket_parse,
2637 NULL);
2638
2639 ret = mbedtls_test_mock_socket_connect(&(client_ep.socket),
2640 &(server_ep.socket), 1024);
2641 TEST_EQUAL(ret, 0);
2642
2643 TEST_EQUAL(mbedtls_test_move_handshake_to_state(
2644 &(server_ep.ssl), &(client_ep.ssl),
2645 MBEDTLS_SSL_HANDSHAKE_OVER), 0);
2646
2647 TEST_EQUAL(server_ep.ssl.handshake->new_session_tickets_count, 0);
2648
2649 do {
2650 ret = mbedtls_ssl_read(&(client_ep.ssl), buf, sizeof(buf));
2651 } while (ret != MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET);
2652
2653 ret = mbedtls_ssl_get_session(&(client_ep.ssl), session);
2654 TEST_EQUAL(ret, 0);
2655
Gilles Peskine946bf142025-04-08 09:48:40 +02002656 ok = 1;
2657
Ronald Cron1f6e4e42024-01-26 16:31:33 +01002658exit:
Gilles Peskineca8a9ac2025-05-27 20:52:24 +02002659 mbedtls_test_ssl_endpoint_free(&client_ep);
2660 mbedtls_test_ssl_endpoint_free(&server_ep);
Ronald Cron1f6e4e42024-01-26 16:31:33 +01002661
Gilles Peskine946bf142025-04-08 09:48:40 +02002662 if (ret == 0 && !ok) {
2663 /* Exiting due to a test assertion that isn't ret == 0 */
2664 ret = -1;
2665 }
Ronald Cron1f6e4e42024-01-26 16:31:33 +01002666 return ret;
2667}
2668#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SRV_C &&
2669 MBEDTLS_SSL_PROTO_TLS1_3 && MBEDTLS_SSL_SESSION_TICKETS &&
2670 MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
2671
Yanray Wang4d07d1c2022-10-27 15:28:16 +08002672#endif /* MBEDTLS_SSL_TLS_C */