blob: c38d24aa8ec7b063bb89b4b3651866a210b883bc [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;
Gilles Peskinefb2ce052025-05-28 17:36:12 +020074 opts->srv_auth_mode = MBEDTLS_SSL_VERIFY_REQUIRED;
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
Gilles Peskinefb2ce052025-05-28 17:36:12 +0200879 if (MBEDTLS_SSL_IS_SERVER == endpoint_type) {
880 mbedtls_ssl_conf_authmode(&(ep->conf), options->srv_auth_mode);
881 } else {
882 mbedtls_ssl_conf_authmode(&(ep->conf), MBEDTLS_SSL_VERIFY_REQUIRED);
883 }
Yanray Wange6afd912022-10-27 12:11:18 +0800884
Ronald Cronced99be2024-01-26 15:49:12 +0100885#if defined(MBEDTLS_SSL_EARLY_DATA)
886 mbedtls_ssl_conf_early_data(&(ep->conf), options->early_data);
Ronald Cron5d3036e2024-02-23 07:43:45 +0100887#if defined(MBEDTLS_SSL_SRV_C)
888 if (endpoint_type == MBEDTLS_SSL_IS_SERVER &&
889 (options->max_early_data_size >= 0)) {
890 mbedtls_ssl_conf_max_early_data_size(&(ep->conf),
891 options->max_early_data_size);
892 }
893#endif
Gilles Peskine27586d82025-05-28 17:01:42 +0200894
Waleed Elmelegy4dfb0e72024-03-14 01:48:40 +0000895#if defined(MBEDTLS_SSL_ALPN)
896 /* check that alpn_list contains at least one valid entry */
897 if (options->alpn_list[0] != NULL) {
898 mbedtls_ssl_conf_alpn_protocols(&(ep->conf), options->alpn_list);
899 }
900#endif
Ronald Cronced99be2024-01-26 15:49:12 +0100901#endif
902
Gilles Peskine27586d82025-05-28 17:01:42 +0200903#if defined(MBEDTLS_SSL_RENEGOTIATION)
904 if (options->renegotiate) {
905 mbedtls_ssl_conf_renegotiation(&ep->conf,
906 MBEDTLS_SSL_RENEGOTIATION_ENABLED);
907 mbedtls_ssl_conf_legacy_renegotiation(&ep->conf,
908 options->legacy_renegotiation);
909 }
910#endif /* MBEDTLS_SSL_RENEGOTIATION */
911
Yanray Wange6afd912022-10-27 12:11:18 +0800912#if defined(MBEDTLS_SSL_CACHE_C) && defined(MBEDTLS_SSL_SRV_C)
913 if (endpoint_type == MBEDTLS_SSL_IS_SERVER && options->cache != NULL) {
914 mbedtls_ssl_conf_session_cache(&(ep->conf), options->cache,
915 mbedtls_ssl_cache_get,
916 mbedtls_ssl_cache_set);
917 }
918#endif
919
Gilles Peskine27586d82025-05-28 17:01:42 +0200920#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
921 TEST_EQUAL(mbedtls_ssl_conf_max_frag_len(&ep->conf,
922 (unsigned char) options->mfl),
923 0);
924#else
925 TEST_EQUAL(MBEDTLS_SSL_MAX_FRAG_LEN_NONE, options->mfl);
926#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
927
Yanray Wange6afd912022-10-27 12:11:18 +0800928 ret = mbedtls_ssl_setup(&(ep->ssl), &(ep->conf));
Gilles Peskine353eb332025-05-14 17:42:53 +0200929 TEST_EQUAL(ret, 0);
Yanray Wange6afd912022-10-27 12:11:18 +0800930
Gilles Peskine856a3702025-02-13 17:28:49 +0100931 if (MBEDTLS_SSL_IS_CLIENT == endpoint_type) {
932 ret = mbedtls_ssl_set_hostname(&(ep->ssl), "localhost");
Gilles Peskine55b8bb42025-04-08 09:44:34 +0200933 TEST_EQUAL(ret, 0);
Gilles Peskine856a3702025-02-13 17:28:49 +0100934 }
935
Yanray Wange6afd912022-10-27 12:11:18 +0800936#if defined(MBEDTLS_SSL_PROTO_DTLS) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine29969592025-05-27 19:24:28 +0200937 if (endpoint_type == MBEDTLS_SSL_IS_SERVER && options->dtls) {
Yanray Wange6afd912022-10-27 12:11:18 +0800938 mbedtls_ssl_conf_dtls_cookies(&(ep->conf), NULL, NULL, NULL);
939 }
940#endif
941
Ronald Cronec3408d2024-01-16 17:50:40 +0100942#if defined(MBEDTLS_DEBUG_C)
943#if defined(MBEDTLS_SSL_SRV_C)
944 if (endpoint_type == MBEDTLS_SSL_IS_SERVER &&
945 options->srv_log_fun != NULL) {
946 mbedtls_ssl_conf_dbg(&(ep->conf), options->srv_log_fun,
947 options->srv_log_obj);
948 }
949#endif
950#if defined(MBEDTLS_SSL_CLI_C)
951 if (endpoint_type == MBEDTLS_SSL_IS_CLIENT &&
952 options->cli_log_fun != NULL) {
953 mbedtls_ssl_conf_dbg(&(ep->conf), options->cli_log_fun,
954 options->cli_log_obj);
955 }
956#endif
957#endif /* MBEDTLS_DEBUG_C */
958
Yanray Wange6afd912022-10-27 12:11:18 +0800959 ret = mbedtls_test_ssl_endpoint_certificate_init(ep, options->pk_alg,
960 options->opaque_alg,
961 options->opaque_alg2,
962 options->opaque_usage);
Gilles Peskine353eb332025-05-14 17:42:53 +0200963 TEST_EQUAL(ret, 0);
Yanray Wange6afd912022-10-27 12:11:18 +0800964
Gilles Peskine27586d82025-05-28 17:01:42 +0200965#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
966 if (options->psk_str != NULL && options->psk_str->len > 0) {
967 TEST_EQUAL(mbedtls_ssl_conf_psk(
968 &ep->conf, options->psk_str->x,
969 options->psk_str->len,
970 (const unsigned char *) psk_identity,
971 strlen(psk_identity)), 0);
972#if defined(MBEDTLS_SSL_SRV_C)
973 if (MBEDTLS_SSL_IS_SERVER == endpoint_type) {
974 mbedtls_ssl_conf_psk_cb(&ep->conf, psk_dummy_callback, NULL);
975 }
976#endif
977 }
978#endif
979
Yanray Wange6afd912022-10-27 12:11:18 +0800980 TEST_EQUAL(mbedtls_ssl_conf_get_user_data_n(&ep->conf), user_data_n);
981 mbedtls_ssl_conf_set_user_data_p(&ep->conf, ep);
982 TEST_EQUAL(mbedtls_ssl_get_user_data_n(&ep->ssl), user_data_n);
983 mbedtls_ssl_set_user_data_p(&ep->ssl, ep);
984
Gilles Peskine946bf142025-04-08 09:48:40 +0200985 return 0;
986
Yanray Wange6afd912022-10-27 12:11:18 +0800987exit:
Gilles Peskine946bf142025-04-08 09:48:40 +0200988 if (ret == 0) {
989 /* Exiting due to a test assertion that isn't ret == 0 */
990 ret = -1;
991 }
Yanray Wange6afd912022-10-27 12:11:18 +0800992 return ret;
993}
994
Yanray Wange6afd912022-10-27 12:11:18 +0800995void mbedtls_test_ssl_endpoint_free(
Gilles Peskineca8a9ac2025-05-27 20:52:24 +0200996 mbedtls_test_ssl_endpoint *ep)
Yanray Wange6afd912022-10-27 12:11:18 +0800997{
Yanray Wange6afd912022-10-27 12:11:18 +0800998 mbedtls_ssl_free(&(ep->ssl));
999 mbedtls_ssl_config_free(&(ep->conf));
1000
Gilles Peskine2744a432025-05-27 13:27:22 +02001001 mbedtls_free(ep->ciphersuites);
1002 ep->ciphersuites = NULL;
1003 test_ssl_endpoint_certificate_free(ep);
1004
Gilles Peskine29969592025-05-27 19:24:28 +02001005 if (ep->dtls_context.socket != NULL) {
1006 mbedtls_test_message_socket_close(&ep->dtls_context);
Yanray Wange6afd912022-10-27 12:11:18 +08001007 } else {
1008 mbedtls_test_mock_socket_close(&(ep->socket));
1009 }
1010}
1011
Gilles Peskineb092e782025-05-27 20:15:03 +02001012int mbedtls_test_ssl_dtls_join_endpoints(mbedtls_test_ssl_endpoint *client,
1013 mbedtls_test_ssl_endpoint *server)
1014{
1015 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1016
Gilles Peskine6c154e72025-05-27 20:23:52 +02001017 ret = mbedtls_test_message_socket_setup(&client->queue_input,
1018 &server->queue_input,
1019 100, &(client->socket),
1020 &client->dtls_context);
1021 TEST_EQUAL(ret, 0);
Gilles Peskineb092e782025-05-27 20:15:03 +02001022
Gilles Peskine6c154e72025-05-27 20:23:52 +02001023 ret = mbedtls_test_message_socket_setup(&server->queue_input,
1024 &client->queue_input,
1025 100, &(server->socket),
1026 &server->dtls_context);
1027 TEST_EQUAL(ret, 0);
1028
1029exit:
Gilles Peskineb092e782025-05-27 20:15:03 +02001030 return ret;
1031}
1032
Yanray Wange6afd912022-10-27 12:11:18 +08001033int mbedtls_test_move_handshake_to_state(mbedtls_ssl_context *ssl,
1034 mbedtls_ssl_context *second_ssl,
1035 int state)
1036{
1037 enum { BUFFSIZE = 1024 };
1038 int max_steps = 1000;
1039 int ret = 0;
1040
1041 if (ssl == NULL || second_ssl == NULL) {
1042 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
1043 }
1044
1045 /* Perform communication via connected sockets */
1046 while ((ssl->state != state) && (--max_steps >= 0)) {
1047 /* If /p second_ssl ends the handshake procedure before /p ssl then
1048 * there is no need to call the next step */
1049 if (!mbedtls_ssl_is_handshake_over(second_ssl)) {
1050 ret = mbedtls_ssl_handshake_step(second_ssl);
1051 if (ret != 0 && ret != MBEDTLS_ERR_SSL_WANT_READ &&
1052 ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
1053 return ret;
1054 }
1055 }
1056
1057 /* We only care about the \p ssl state and returns, so we call it last,
1058 * to leave the iteration as soon as the state is as expected. */
1059 ret = mbedtls_ssl_handshake_step(ssl);
1060 if (ret != 0 && ret != MBEDTLS_ERR_SSL_WANT_READ &&
1061 ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
1062 return ret;
1063 }
1064 }
1065
1066 return (max_steps >= 0) ? ret : -1;
1067}
1068
1069#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
1070
1071/*
1072 * Write application data. Increase write counter if necessary.
1073 */
Michael Schuster54300d42024-06-04 02:30:22 +02001074static int mbedtls_ssl_write_fragment(mbedtls_ssl_context *ssl,
Michael Schusterbd89b792024-06-04 02:41:10 +02001075 unsigned char *buf, int buf_len,
1076 int *written,
1077 const int expected_fragments)
Yanray Wange6afd912022-10-27 12:11:18 +08001078{
Agathiyan Bragadeesh93212652023-07-12 11:22:59 +01001079 int ret;
Yanray Wange6afd912022-10-27 12:11:18 +08001080 /* Verify that calling mbedtls_ssl_write with a NULL buffer and zero length is
1081 * a valid no-op for TLS connections. */
1082 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Gilles Peskine353eb332025-05-14 17:42:53 +02001083 TEST_EQUAL(mbedtls_ssl_write(ssl, NULL, 0), 0);
Yanray Wange6afd912022-10-27 12:11:18 +08001084 }
1085
Agathiyan Bragadeesh93212652023-07-12 11:22:59 +01001086 ret = mbedtls_ssl_write(ssl, buf + *written, buf_len - *written);
Yanray Wange6afd912022-10-27 12:11:18 +08001087 if (ret > 0) {
1088 *written += ret;
1089 }
1090
1091 if (expected_fragments == 0) {
1092 /* Used for DTLS and the message size larger than MFL. In that case
1093 * the message can not be fragmented and the library should return
1094 * MBEDTLS_ERR_SSL_BAD_INPUT_DATA error. This error must be returned
Yanray Wangb088bfc2023-03-16 12:15:49 +08001095 * to prevent a dead loop inside mbedtls_test_ssl_exchange_data(). */
Yanray Wange6afd912022-10-27 12:11:18 +08001096 return ret;
1097 } else if (expected_fragments == 1) {
1098 /* Used for TLS/DTLS and the message size lower than MFL */
1099 TEST_ASSERT(ret == buf_len ||
1100 ret == MBEDTLS_ERR_SSL_WANT_READ ||
1101 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
1102 } else {
1103 /* Used for TLS and the message size larger than MFL */
1104 TEST_ASSERT(expected_fragments > 1);
1105 TEST_ASSERT((ret >= 0 && ret <= buf_len) ||
1106 ret == MBEDTLS_ERR_SSL_WANT_READ ||
1107 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
1108 }
1109
1110 return 0;
1111
1112exit:
1113 /* Some of the tests failed */
1114 return -1;
1115}
1116
1117/*
1118 * Read application data and increase read counter and fragments counter
1119 * if necessary.
1120 */
Michael Schuster54300d42024-06-04 02:30:22 +02001121static int mbedtls_ssl_read_fragment(mbedtls_ssl_context *ssl,
Michael Schusterbd89b792024-06-04 02:41:10 +02001122 unsigned char *buf, int buf_len,
1123 int *read, int *fragments,
1124 const int expected_fragments)
Yanray Wange6afd912022-10-27 12:11:18 +08001125{
Agathiyan Bragadeesh93212652023-07-12 11:22:59 +01001126 int ret;
Yanray Wange6afd912022-10-27 12:11:18 +08001127 /* Verify that calling mbedtls_ssl_write with a NULL buffer and zero length is
1128 * a valid no-op for TLS connections. */
1129 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Gilles Peskine353eb332025-05-14 17:42:53 +02001130 TEST_EQUAL(mbedtls_ssl_read(ssl, NULL, 0), 0);
Yanray Wange6afd912022-10-27 12:11:18 +08001131 }
1132
Agathiyan Bragadeesh93212652023-07-12 11:22:59 +01001133 ret = mbedtls_ssl_read(ssl, buf + *read, buf_len - *read);
Yanray Wange6afd912022-10-27 12:11:18 +08001134 if (ret > 0) {
1135 (*fragments)++;
1136 *read += ret;
1137 }
1138
1139 if (expected_fragments == 0) {
Gilles Peskine353eb332025-05-14 17:42:53 +02001140 TEST_EQUAL(ret, 0);
Yanray Wange6afd912022-10-27 12:11:18 +08001141 } else if (expected_fragments == 1) {
1142 TEST_ASSERT(ret == buf_len ||
1143 ret == MBEDTLS_ERR_SSL_WANT_READ ||
1144 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
1145 } else {
1146 TEST_ASSERT(expected_fragments > 1);
1147 TEST_ASSERT((ret >= 0 && ret <= buf_len) ||
1148 ret == MBEDTLS_ERR_SSL_WANT_READ ||
1149 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
1150 }
1151
1152 return 0;
1153
1154exit:
1155 /* Some of the tests failed */
1156 return -1;
1157}
1158
Yanray Wange6afd912022-10-27 12:11:18 +08001159#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Elena Uziunaite74342c72024-07-05 11:31:29 +01001160 defined(PSA_WANT_ALG_CBC_NO_PADDING) && defined(PSA_WANT_KEY_TYPE_AES)
Yanray Wange6afd912022-10-27 12:11:18 +08001161int mbedtls_test_psa_cipher_encrypt_helper(mbedtls_ssl_transform *transform,
1162 const unsigned char *iv,
1163 size_t iv_len,
1164 const unsigned char *input,
1165 size_t ilen,
1166 unsigned char *output,
1167 size_t *olen)
1168{
1169#if defined(MBEDTLS_USE_PSA_CRYPTO)
1170 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1171 psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT;
1172 size_t part_len;
1173
1174 status = psa_cipher_encrypt_setup(&cipher_op,
1175 transform->psa_key_enc,
1176 transform->psa_alg);
1177
1178 if (status != PSA_SUCCESS) {
1179 return PSA_TO_MBEDTLS_ERR(status);
1180 }
1181
1182 status = psa_cipher_set_iv(&cipher_op, iv, iv_len);
1183
1184 if (status != PSA_SUCCESS) {
1185 return PSA_TO_MBEDTLS_ERR(status);
1186 }
1187
1188 status = psa_cipher_update(&cipher_op, input, ilen, output, ilen, olen);
1189
1190 if (status != PSA_SUCCESS) {
1191 return PSA_TO_MBEDTLS_ERR(status);
1192 }
1193
1194 status = psa_cipher_finish(&cipher_op, output + *olen, ilen - *olen,
1195 &part_len);
1196
1197 if (status != PSA_SUCCESS) {
1198 return PSA_TO_MBEDTLS_ERR(status);
1199 }
1200
1201 *olen += part_len;
1202 return 0;
1203#else
1204 return mbedtls_cipher_crypt(&transform->cipher_ctx_enc,
1205 iv, iv_len, input, ilen, output, olen);
1206#endif /* MBEDTLS_USE_PSA_CRYPTO */
1207}
Elena Uziunaite74342c72024-07-05 11:31:29 +01001208#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && PSA_WANT_ALG_CBC_NO_PADDING &&
Elena Uziunaite6121a342024-07-05 11:16:53 +01001209 PSA_WANT_KEY_TYPE_AES */
Yanray Wange6afd912022-10-27 12:11:18 +08001210
Valerio Setti31ad3a12023-10-27 11:55:02 +02001211static void mbedtls_test_ssl_cipher_info_from_type(mbedtls_cipher_type_t cipher_type,
1212 mbedtls_cipher_mode_t *cipher_mode,
1213 size_t *key_bits, size_t *iv_len)
1214{
1215 switch (cipher_type) {
1216 case MBEDTLS_CIPHER_AES_128_CBC:
1217 *cipher_mode = MBEDTLS_MODE_CBC;
1218 *key_bits = 128;
1219 *iv_len = 16;
1220 break;
1221 case MBEDTLS_CIPHER_AES_256_CBC:
1222 *cipher_mode = MBEDTLS_MODE_CBC;
1223 *key_bits = 256;
1224 *iv_len = 16;
1225 break;
1226 case MBEDTLS_CIPHER_ARIA_128_CBC:
1227 *cipher_mode = MBEDTLS_MODE_CBC;
1228 *key_bits = 128;
1229 *iv_len = 16;
1230 break;
1231 case MBEDTLS_CIPHER_ARIA_256_CBC:
1232 *cipher_mode = MBEDTLS_MODE_CBC;
1233 *key_bits = 256;
1234 *iv_len = 16;
1235 break;
1236 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
1237 *cipher_mode = MBEDTLS_MODE_CBC;
1238 *key_bits = 128;
1239 *iv_len = 16;
1240 break;
1241 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
1242 *cipher_mode = MBEDTLS_MODE_CBC;
1243 *key_bits = 256;
1244 *iv_len = 16;
1245 break;
1246
1247 case MBEDTLS_CIPHER_AES_128_CCM:
1248 *cipher_mode = MBEDTLS_MODE_CCM;
1249 *key_bits = 128;
1250 *iv_len = 12;
1251 break;
1252 case MBEDTLS_CIPHER_AES_192_CCM:
1253 *cipher_mode = MBEDTLS_MODE_CCM;
1254 *key_bits = 192;
1255 *iv_len = 12;
1256 break;
1257 case MBEDTLS_CIPHER_AES_256_CCM:
1258 *cipher_mode = MBEDTLS_MODE_CCM;
1259 *key_bits = 256;
1260 *iv_len = 12;
1261 break;
1262 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
1263 *cipher_mode = MBEDTLS_MODE_CCM;
1264 *key_bits = 128;
1265 *iv_len = 12;
1266 break;
1267 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
1268 *cipher_mode = MBEDTLS_MODE_CCM;
1269 *key_bits = 192;
1270 *iv_len = 12;
1271 break;
1272 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
1273 *cipher_mode = MBEDTLS_MODE_CCM;
1274 *key_bits = 256;
1275 *iv_len = 12;
1276 break;
1277
1278 case MBEDTLS_CIPHER_AES_128_GCM:
1279 *cipher_mode = MBEDTLS_MODE_GCM;
1280 *key_bits = 128;
1281 *iv_len = 12;
1282 break;
1283 case MBEDTLS_CIPHER_AES_192_GCM:
1284 *cipher_mode = MBEDTLS_MODE_GCM;
1285 *key_bits = 192;
1286 *iv_len = 12;
1287 break;
1288 case MBEDTLS_CIPHER_AES_256_GCM:
1289 *cipher_mode = MBEDTLS_MODE_GCM;
1290 *key_bits = 256;
1291 *iv_len = 12;
1292 break;
1293 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
1294 *cipher_mode = MBEDTLS_MODE_GCM;
1295 *key_bits = 128;
1296 *iv_len = 12;
1297 break;
1298 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
1299 *cipher_mode = MBEDTLS_MODE_GCM;
1300 *key_bits = 192;
1301 *iv_len = 12;
1302 break;
1303 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
1304 *cipher_mode = MBEDTLS_MODE_GCM;
1305 *key_bits = 256;
1306 *iv_len = 12;
1307 break;
1308
1309 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
1310 *cipher_mode = MBEDTLS_MODE_CHACHAPOLY;
1311 *key_bits = 256;
1312 *iv_len = 12;
1313 break;
1314
1315 case MBEDTLS_CIPHER_NULL:
1316 *cipher_mode = MBEDTLS_MODE_STREAM;
1317 *key_bits = 0;
1318 *iv_len = 0;
1319 break;
1320
1321 default:
1322 *cipher_mode = MBEDTLS_MODE_NONE;
1323 *key_bits = 0;
1324 *iv_len = 0;
1325 }
1326}
1327
Yanray Wange6afd912022-10-27 12:11:18 +08001328int mbedtls_test_ssl_build_transforms(mbedtls_ssl_transform *t_in,
1329 mbedtls_ssl_transform *t_out,
1330 int cipher_type, int hash_id,
1331 int etm, int tag_mode,
1332 mbedtls_ssl_protocol_version tls_version,
1333 size_t cid0_len,
1334 size_t cid1_len)
1335{
Valerio Setti31ad3a12023-10-27 11:55:02 +02001336 mbedtls_cipher_mode_t cipher_mode = MBEDTLS_MODE_NONE;
1337 size_t key_bits = 0;
Yanray Wange6afd912022-10-27 12:11:18 +08001338 int ret = 0;
1339
1340#if defined(MBEDTLS_USE_PSA_CRYPTO)
1341 psa_key_type_t key_type;
1342 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1343 psa_algorithm_t alg;
Yanray Wange6afd912022-10-27 12:11:18 +08001344 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Valerio Setti3d59ebe2023-10-30 11:56:56 +01001345#else
Valerio Setti31ad3a12023-10-27 11:55:02 +02001346 mbedtls_cipher_info_t const *cipher_info;
Yanray Wange6afd912022-10-27 12:11:18 +08001347#endif
1348
Valerio Setti31ad3a12023-10-27 11:55:02 +02001349 size_t keylen, maclen, ivlen = 0;
Yanray Wange6afd912022-10-27 12:11:18 +08001350 unsigned char *key0 = NULL, *key1 = NULL;
1351 unsigned char *md0 = NULL, *md1 = NULL;
1352 unsigned char iv_enc[16], iv_dec[16];
1353
1354#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
1355 unsigned char cid0[SSL_CID_LEN_MIN];
1356 unsigned char cid1[SSL_CID_LEN_MIN];
1357
1358 mbedtls_test_rnd_std_rand(NULL, cid0, sizeof(cid0));
1359 mbedtls_test_rnd_std_rand(NULL, cid1, sizeof(cid1));
1360#else
1361 ((void) cid0_len);
1362 ((void) cid1_len);
1363#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
1364
1365 maclen = 0;
Valerio Setti31ad3a12023-10-27 11:55:02 +02001366 mbedtls_test_ssl_cipher_info_from_type((mbedtls_cipher_type_t) cipher_type,
1367 &cipher_mode, &key_bits, &ivlen);
Yanray Wange6afd912022-10-27 12:11:18 +08001368
1369 /* Pick keys */
Valerio Setti31ad3a12023-10-27 11:55:02 +02001370 keylen = key_bits / 8;
Yanray Wange6afd912022-10-27 12:11:18 +08001371 /* Allocate `keylen + 1` bytes to ensure that we get
1372 * a non-NULL pointers from `mbedtls_calloc` even if
1373 * `keylen == 0` in the case of the NULL cipher. */
1374 CHK((key0 = mbedtls_calloc(1, keylen + 1)) != NULL);
1375 CHK((key1 = mbedtls_calloc(1, keylen + 1)) != NULL);
1376 memset(key0, 0x1, keylen);
1377 memset(key1, 0x2, keylen);
1378
1379#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti31ad3a12023-10-27 11:55:02 +02001380 /* Pick cipher */
1381 cipher_info = mbedtls_cipher_info_from_type((mbedtls_cipher_type_t) cipher_type);
1382 CHK(cipher_info != NULL);
1383 CHK(mbedtls_cipher_info_get_iv_size(cipher_info) <= 16);
1384 CHK(mbedtls_cipher_info_get_key_bitlen(cipher_info) % 8 == 0);
Valerio Setti3d59ebe2023-10-30 11:56:56 +01001385
Yanray Wange6afd912022-10-27 12:11:18 +08001386 /* Setup cipher contexts */
1387 CHK(mbedtls_cipher_setup(&t_in->cipher_ctx_enc, cipher_info) == 0);
1388 CHK(mbedtls_cipher_setup(&t_in->cipher_ctx_dec, cipher_info) == 0);
1389 CHK(mbedtls_cipher_setup(&t_out->cipher_ctx_enc, cipher_info) == 0);
1390 CHK(mbedtls_cipher_setup(&t_out->cipher_ctx_dec, cipher_info) == 0);
1391
1392#if defined(MBEDTLS_CIPHER_MODE_CBC)
Valerio Setti31ad3a12023-10-27 11:55:02 +02001393 if (cipher_mode == MBEDTLS_MODE_CBC) {
Yanray Wange6afd912022-10-27 12:11:18 +08001394 CHK(mbedtls_cipher_set_padding_mode(&t_in->cipher_ctx_enc,
1395 MBEDTLS_PADDING_NONE) == 0);
1396 CHK(mbedtls_cipher_set_padding_mode(&t_in->cipher_ctx_dec,
1397 MBEDTLS_PADDING_NONE) == 0);
1398 CHK(mbedtls_cipher_set_padding_mode(&t_out->cipher_ctx_enc,
1399 MBEDTLS_PADDING_NONE) == 0);
1400 CHK(mbedtls_cipher_set_padding_mode(&t_out->cipher_ctx_dec,
1401 MBEDTLS_PADDING_NONE) == 0);
1402 }
1403#endif /* MBEDTLS_CIPHER_MODE_CBC */
1404
1405 CHK(mbedtls_cipher_setkey(&t_in->cipher_ctx_enc, key0,
Yanray Wanga8f445e2022-11-03 11:51:59 +08001406 (keylen << 3 > INT_MAX) ? INT_MAX : (int) keylen << 3,
1407 MBEDTLS_ENCRYPT)
1408 == 0);
Yanray Wange6afd912022-10-27 12:11:18 +08001409 CHK(mbedtls_cipher_setkey(&t_in->cipher_ctx_dec, key1,
Yanray Wanga8f445e2022-11-03 11:51:59 +08001410 (keylen << 3 > INT_MAX) ? INT_MAX : (int) keylen << 3,
1411 MBEDTLS_DECRYPT)
1412 == 0);
Yanray Wange6afd912022-10-27 12:11:18 +08001413 CHK(mbedtls_cipher_setkey(&t_out->cipher_ctx_enc, key1,
Yanray Wanga8f445e2022-11-03 11:51:59 +08001414 (keylen << 3 > INT_MAX) ? INT_MAX : (int) keylen << 3,
1415 MBEDTLS_ENCRYPT)
1416 == 0);
Yanray Wange6afd912022-10-27 12:11:18 +08001417 CHK(mbedtls_cipher_setkey(&t_out->cipher_ctx_dec, key0,
Yanray Wanga8f445e2022-11-03 11:51:59 +08001418 (keylen << 3 > INT_MAX) ? INT_MAX : (int) keylen << 3,
1419 MBEDTLS_DECRYPT)
1420 == 0);
Valerio Setti31ad3a12023-10-27 11:55:02 +02001421#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Yanray Wange6afd912022-10-27 12:11:18 +08001422
1423 /* Setup MAC contexts */
1424#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Valerio Setti31ad3a12023-10-27 11:55:02 +02001425 if (cipher_mode == MBEDTLS_MODE_CBC ||
1426 cipher_mode == MBEDTLS_MODE_STREAM) {
Yanray Wange6afd912022-10-27 12:11:18 +08001427#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Agathiyan Bragadeesh2f017a82023-07-12 11:21:54 +01001428 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 +08001429 CHK(md_info != NULL);
1430#endif
Agathiyan Bragadeesh2f017a82023-07-12 11:21:54 +01001431 maclen = mbedtls_md_get_size_from_type((mbedtls_md_type_t) hash_id);
Yanray Wange6afd912022-10-27 12:11:18 +08001432 CHK(maclen != 0);
1433 /* Pick hash keys */
1434 CHK((md0 = mbedtls_calloc(1, maclen)) != NULL);
1435 CHK((md1 = mbedtls_calloc(1, maclen)) != NULL);
1436 memset(md0, 0x5, maclen);
1437 memset(md1, 0x6, maclen);
1438
1439#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02001440 alg = mbedtls_md_psa_alg_from_type(hash_id);
Yanray Wange6afd912022-10-27 12:11:18 +08001441
1442 CHK(alg != 0);
1443
1444 t_out->psa_mac_alg = PSA_ALG_HMAC(alg);
1445 t_in->psa_mac_alg = PSA_ALG_HMAC(alg);
1446 t_in->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
1447 t_out->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
1448 t_in->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
1449 t_out->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
1450
1451 psa_reset_key_attributes(&attributes);
1452 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
1453 psa_set_key_algorithm(&attributes, PSA_ALG_HMAC(alg));
1454 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
1455
1456 CHK(psa_import_key(&attributes,
1457 md0, maclen,
1458 &t_in->psa_mac_enc) == PSA_SUCCESS);
1459
1460 CHK(psa_import_key(&attributes,
1461 md1, maclen,
1462 &t_out->psa_mac_enc) == PSA_SUCCESS);
1463
Valerio Setti31ad3a12023-10-27 11:55:02 +02001464 if (cipher_mode == MBEDTLS_MODE_STREAM ||
Yanray Wange6afd912022-10-27 12:11:18 +08001465 etm == MBEDTLS_SSL_ETM_DISABLED) {
1466 /* mbedtls_ct_hmac() requires the key to be exportable */
1467 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT |
1468 PSA_KEY_USAGE_VERIFY_HASH);
1469 } else {
1470 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
1471 }
1472
1473 CHK(psa_import_key(&attributes,
1474 md1, maclen,
1475 &t_in->psa_mac_dec) == PSA_SUCCESS);
1476
1477 CHK(psa_import_key(&attributes,
1478 md0, maclen,
1479 &t_out->psa_mac_dec) == PSA_SUCCESS);
1480#else
1481 CHK(mbedtls_md_setup(&t_out->md_ctx_enc, md_info, 1) == 0);
1482 CHK(mbedtls_md_setup(&t_out->md_ctx_dec, md_info, 1) == 0);
1483 CHK(mbedtls_md_setup(&t_in->md_ctx_enc, md_info, 1) == 0);
1484 CHK(mbedtls_md_setup(&t_in->md_ctx_dec, md_info, 1) == 0);
1485
1486 CHK(mbedtls_md_hmac_starts(&t_in->md_ctx_enc,
1487 md0, maclen) == 0);
1488 CHK(mbedtls_md_hmac_starts(&t_in->md_ctx_dec,
1489 md1, maclen) == 0);
1490 CHK(mbedtls_md_hmac_starts(&t_out->md_ctx_enc,
1491 md1, maclen) == 0);
1492 CHK(mbedtls_md_hmac_starts(&t_out->md_ctx_dec,
1493 md0, maclen) == 0);
1494#endif
1495 }
1496#else
1497 ((void) hash_id);
1498#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
1499
1500
1501 /* Pick IV's (regardless of whether they
1502 * are being used by the transform). */
Yanray Wange6afd912022-10-27 12:11:18 +08001503 memset(iv_enc, 0x3, sizeof(iv_enc));
1504 memset(iv_dec, 0x4, sizeof(iv_dec));
1505
1506 /*
1507 * Setup transforms
1508 */
1509
1510#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
1511 defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
1512 t_out->encrypt_then_mac = etm;
1513 t_in->encrypt_then_mac = etm;
1514#else
1515 ((void) etm);
1516#endif
1517
1518 t_out->tls_version = tls_version;
1519 t_in->tls_version = tls_version;
1520 t_out->ivlen = ivlen;
1521 t_in->ivlen = ivlen;
1522
Valerio Setti31ad3a12023-10-27 11:55:02 +02001523 switch (cipher_mode) {
Yanray Wange6afd912022-10-27 12:11:18 +08001524 case MBEDTLS_MODE_GCM:
1525 case MBEDTLS_MODE_CCM:
1526#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1527 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
1528 t_out->fixed_ivlen = 12;
1529 t_in->fixed_ivlen = 12;
1530 } else
1531#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1532 {
1533 t_out->fixed_ivlen = 4;
1534 t_in->fixed_ivlen = 4;
1535 }
1536 t_out->maclen = 0;
1537 t_in->maclen = 0;
1538 switch (tag_mode) {
1539 case 0: /* Full tag */
1540 t_out->taglen = 16;
1541 t_in->taglen = 16;
1542 break;
1543 case 1: /* Partial tag */
1544 t_out->taglen = 8;
1545 t_in->taglen = 8;
1546 break;
1547 default:
1548 ret = 1;
1549 goto cleanup;
1550 }
1551 break;
1552
1553 case MBEDTLS_MODE_CHACHAPOLY:
1554 t_out->fixed_ivlen = 12;
1555 t_in->fixed_ivlen = 12;
1556 t_out->maclen = 0;
1557 t_in->maclen = 0;
1558 switch (tag_mode) {
1559 case 0: /* Full tag */
1560 t_out->taglen = 16;
1561 t_in->taglen = 16;
1562 break;
1563 case 1: /* Partial tag */
1564 t_out->taglen = 8;
1565 t_in->taglen = 8;
1566 break;
1567 default:
1568 ret = 1;
1569 goto cleanup;
1570 }
1571 break;
1572
1573 case MBEDTLS_MODE_STREAM:
1574 case MBEDTLS_MODE_CBC:
1575 t_out->fixed_ivlen = 0; /* redundant, must be 0 */
1576 t_in->fixed_ivlen = 0; /* redundant, must be 0 */
1577 t_out->taglen = 0;
1578 t_in->taglen = 0;
1579 switch (tag_mode) {
1580 case 0: /* Full tag */
1581 t_out->maclen = maclen;
1582 t_in->maclen = maclen;
1583 break;
1584 default:
1585 ret = 1;
1586 goto cleanup;
1587 }
1588 break;
1589 default:
1590 ret = 1;
1591 goto cleanup;
1592 break;
1593 }
1594
1595 /* Setup IV's */
1596
1597 memcpy(&t_in->iv_dec, iv_dec, sizeof(iv_dec));
1598 memcpy(&t_in->iv_enc, iv_enc, sizeof(iv_enc));
1599 memcpy(&t_out->iv_dec, iv_enc, sizeof(iv_enc));
1600 memcpy(&t_out->iv_enc, iv_dec, sizeof(iv_dec));
1601
1602#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
1603 /* Add CID */
1604 memcpy(&t_in->in_cid, cid0, cid0_len);
1605 memcpy(&t_in->out_cid, cid1, cid1_len);
Yanray Wanga8f445e2022-11-03 11:51:59 +08001606 t_in->in_cid_len = (uint8_t) cid0_len;
1607 t_in->out_cid_len = (uint8_t) cid1_len;
Yanray Wange6afd912022-10-27 12:11:18 +08001608 memcpy(&t_out->in_cid, cid1, cid1_len);
1609 memcpy(&t_out->out_cid, cid0, cid0_len);
Yanray Wanga8f445e2022-11-03 11:51:59 +08001610 t_out->in_cid_len = (uint8_t) cid1_len;
1611 t_out->out_cid_len = (uint8_t) cid0_len;
Yanray Wange6afd912022-10-27 12:11:18 +08001612#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
1613
1614#if defined(MBEDTLS_USE_PSA_CRYPTO)
1615 status = mbedtls_ssl_cipher_to_psa(cipher_type,
1616 t_in->taglen,
1617 &alg,
1618 &key_type,
1619 &key_bits);
1620
1621 if (status != PSA_SUCCESS) {
1622 ret = PSA_TO_MBEDTLS_ERR(status);
1623 goto cleanup;
1624 }
1625
1626 t_in->psa_alg = alg;
1627 t_out->psa_alg = alg;
1628
1629 if (alg != MBEDTLS_SSL_NULL_CIPHER) {
1630 psa_reset_key_attributes(&attributes);
1631 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
1632 psa_set_key_algorithm(&attributes, alg);
1633 psa_set_key_type(&attributes, key_type);
1634
1635 status = psa_import_key(&attributes,
1636 key0,
1637 PSA_BITS_TO_BYTES(key_bits),
1638 &t_in->psa_key_enc);
1639
1640 if (status != PSA_SUCCESS) {
1641 ret = PSA_TO_MBEDTLS_ERR(status);
1642 goto cleanup;
1643 }
1644
1645 status = psa_import_key(&attributes,
1646 key1,
1647 PSA_BITS_TO_BYTES(key_bits),
1648 &t_out->psa_key_enc);
1649
1650 if (status != PSA_SUCCESS) {
1651 ret = PSA_TO_MBEDTLS_ERR(status);
1652 goto cleanup;
1653 }
1654
1655 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
1656
1657 status = psa_import_key(&attributes,
1658 key1,
1659 PSA_BITS_TO_BYTES(key_bits),
1660 &t_in->psa_key_dec);
1661
1662 if (status != PSA_SUCCESS) {
1663 ret = PSA_TO_MBEDTLS_ERR(status);
1664 goto cleanup;
1665 }
1666
1667 status = psa_import_key(&attributes,
1668 key0,
1669 PSA_BITS_TO_BYTES(key_bits),
1670 &t_out->psa_key_dec);
1671
1672 if (status != PSA_SUCCESS) {
1673 ret = PSA_TO_MBEDTLS_ERR(status);
1674 goto cleanup;
1675 }
1676 }
1677#endif /* MBEDTLS_USE_PSA_CRYPTO */
1678
1679cleanup:
1680
1681 mbedtls_free(key0);
1682 mbedtls_free(key1);
1683
1684 mbedtls_free(md0);
1685 mbedtls_free(md1);
1686
1687 return ret;
1688}
1689
Gilles Peskine9099d3f2023-09-18 13:11:50 +02001690#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
1691int mbedtls_test_ssl_prepare_record_mac(mbedtls_record *record,
1692 mbedtls_ssl_transform *transform_out)
1693{
1694#if defined(MBEDTLS_USE_PSA_CRYPTO)
1695 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1696#endif
1697
1698 /* Serialized version of record header for MAC purposes */
1699 unsigned char add_data[13];
1700 memcpy(add_data, record->ctr, 8);
1701 add_data[8] = record->type;
1702 add_data[9] = record->ver[0];
1703 add_data[10] = record->ver[1];
1704 add_data[11] = (record->data_len >> 8) & 0xff;
1705 add_data[12] = (record->data_len >> 0) & 0xff;
1706
1707 /* MAC with additional data */
1708#if defined(MBEDTLS_USE_PSA_CRYPTO)
1709 size_t sign_mac_length = 0;
1710 TEST_EQUAL(PSA_SUCCESS, psa_mac_sign_setup(&operation,
1711 transform_out->psa_mac_enc,
1712 transform_out->psa_mac_alg));
1713 TEST_EQUAL(PSA_SUCCESS, psa_mac_update(&operation, add_data, 13));
1714 TEST_EQUAL(PSA_SUCCESS, psa_mac_update(&operation,
1715 record->buf + record->data_offset,
1716 record->data_len));
1717 /* Use a temporary buffer for the MAC, because with the truncated HMAC
1718 * extension, there might not be enough room in the record for the
1719 * full-length MAC. */
1720 unsigned char mac[PSA_HASH_MAX_SIZE];
1721 TEST_EQUAL(PSA_SUCCESS, psa_mac_sign_finish(&operation,
1722 mac, sizeof(mac),
1723 &sign_mac_length));
1724#else
1725 TEST_EQUAL(0, mbedtls_md_hmac_update(&transform_out->md_ctx_enc, add_data, 13));
1726 TEST_EQUAL(0, mbedtls_md_hmac_update(&transform_out->md_ctx_enc,
1727 record->buf + record->data_offset,
1728 record->data_len));
1729 /* Use a temporary buffer for the MAC, because with the truncated HMAC
1730 * extension, there might not be enough room in the record for the
1731 * full-length MAC. */
1732 unsigned char mac[MBEDTLS_MD_MAX_SIZE];
1733 TEST_EQUAL(0, mbedtls_md_hmac_finish(&transform_out->md_ctx_enc, mac));
1734#endif
1735 memcpy(record->buf + record->data_offset + record->data_len, mac, transform_out->maclen);
1736 record->data_len += transform_out->maclen;
1737
1738 return 0;
1739
1740exit:
1741#if defined(MBEDTLS_USE_PSA_CRYPTO)
1742 psa_mac_abort(&operation);
1743#endif
1744 return -1;
1745}
1746#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
1747
Jerry Yu28547c42023-10-31 14:42:50 +08001748#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Yanray Wange6afd912022-10-27 12:11:18 +08001749int mbedtls_test_ssl_tls12_populate_session(mbedtls_ssl_session *session,
1750 int ticket_len,
Ronald Cron7b1921a2023-11-23 12:31:56 +01001751 int endpoint_type,
Yanray Wange6afd912022-10-27 12:11:18 +08001752 const char *crt_file)
1753{
Ronald Cronc57f86e2023-11-22 09:50:01 +01001754 (void) ticket_len;
1755
Yanray Wange6afd912022-10-27 12:11:18 +08001756#if defined(MBEDTLS_HAVE_TIME)
1757 session->start = mbedtls_time(NULL) - 42;
1758#endif
1759 session->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Ronald Cronc7fa82e2024-02-09 09:33:09 +01001760
1761 TEST_ASSERT(endpoint_type == MBEDTLS_SSL_IS_CLIENT ||
1762 endpoint_type == MBEDTLS_SSL_IS_SERVER);
1763
1764 session->endpoint = endpoint_type;
Yanray Wange6afd912022-10-27 12:11:18 +08001765 session->ciphersuite = 0xabcd;
1766 session->id_len = sizeof(session->id);
1767 memset(session->id, 66, session->id_len);
1768 memset(session->master, 17, sizeof(session->master));
1769
1770#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) && defined(MBEDTLS_FS_IO)
1771 if (crt_file != NULL && strlen(crt_file) != 0) {
1772 mbedtls_x509_crt tmp_crt;
1773 int ret;
1774
1775 mbedtls_x509_crt_init(&tmp_crt);
1776 ret = mbedtls_x509_crt_parse_file(&tmp_crt, crt_file);
1777 if (ret != 0) {
1778 return ret;
1779 }
1780
1781#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1782 /* Move temporary CRT. */
1783 session->peer_cert = mbedtls_calloc(1, sizeof(*session->peer_cert));
1784 if (session->peer_cert == NULL) {
1785 return -1;
1786 }
1787 *session->peer_cert = tmp_crt;
1788 memset(&tmp_crt, 0, sizeof(tmp_crt));
1789#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
1790 /* Calculate digest of temporary CRT. */
1791 session->peer_cert_digest =
1792 mbedtls_calloc(1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN);
1793 if (session->peer_cert_digest == NULL) {
1794 return -1;
1795 }
1796
1797#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02001798 psa_algorithm_t psa_alg = mbedtls_md_psa_alg_from_type(
Yanray Wange6afd912022-10-27 12:11:18 +08001799 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE);
1800 size_t hash_size = 0;
1801 psa_status_t status = psa_hash_compute(
1802 psa_alg, tmp_crt.raw.p,
1803 tmp_crt.raw.len,
1804 session->peer_cert_digest,
1805 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN,
1806 &hash_size);
1807 ret = PSA_TO_MBEDTLS_ERR(status);
1808#else
1809 ret = mbedtls_md(mbedtls_md_info_from_type(
1810 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE),
1811 tmp_crt.raw.p, tmp_crt.raw.len,
1812 session->peer_cert_digest);
1813#endif /* MBEDTLS_USE_PSA_CRYPTO */
1814 if (ret != 0) {
1815 return ret;
1816 }
1817 session->peer_cert_digest_type =
1818 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
1819 session->peer_cert_digest_len =
1820 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
1821#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
1822
1823 mbedtls_x509_crt_free(&tmp_crt);
1824 }
1825#else /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED && MBEDTLS_FS_IO */
1826 (void) crt_file;
1827#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED && MBEDTLS_FS_IO */
1828 session->verify_result = 0xdeadbeef;
1829
Ronald Cronc57f86e2023-11-22 09:50:01 +01001830#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1831#if defined(MBEDTLS_SSL_CLI_C)
Yanray Wange6afd912022-10-27 12:11:18 +08001832 if (ticket_len != 0) {
1833 session->ticket = mbedtls_calloc(1, ticket_len);
1834 if (session->ticket == NULL) {
1835 return -1;
1836 }
1837 memset(session->ticket, 33, ticket_len);
1838 }
1839 session->ticket_len = ticket_len;
1840 session->ticket_lifetime = 86401;
Ronald Cronc57f86e2023-11-22 09:50:01 +01001841#endif /* MBEDTLS_SSL_CLI_C */
1842
1843#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_HAVE_TIME)
1844 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
1845 session->ticket_creation_time = mbedtls_ms_time() - 42;
1846 }
Yanray Wange6afd912022-10-27 12:11:18 +08001847#endif
Ronald Cronc57f86e2023-11-22 09:50:01 +01001848#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Yanray Wange6afd912022-10-27 12:11:18 +08001849
1850#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1851 session->mfl_code = 1;
1852#endif
1853#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1854 session->encrypt_then_mac = 1;
1855#endif
1856
Ronald Cronc7fa82e2024-02-09 09:33:09 +01001857exit:
Yanray Wange6afd912022-10-27 12:11:18 +08001858 return 0;
1859}
Jerry Yu28547c42023-10-31 14:42:50 +08001860#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Yanray Wange6afd912022-10-27 12:11:18 +08001861
1862#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1863int mbedtls_test_ssl_tls13_populate_session(mbedtls_ssl_session *session,
1864 int ticket_len,
1865 int endpoint_type)
1866{
1867 ((void) ticket_len);
1868 session->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
1869 session->endpoint = endpoint_type == MBEDTLS_SSL_IS_CLIENT ?
1870 MBEDTLS_SSL_IS_CLIENT : MBEDTLS_SSL_IS_SERVER;
1871 session->ciphersuite = 0xabcd;
Ronald Cron18b92a12024-03-26 10:15:08 +01001872
1873#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Yanray Wange6afd912022-10-27 12:11:18 +08001874 session->ticket_age_add = 0x87654321;
1875 session->ticket_flags = 0x7;
Yanray Wange6afd912022-10-27 12:11:18 +08001876 session->resumption_key_len = 32;
1877 memset(session->resumption_key, 0x99, sizeof(session->resumption_key));
Yanray Wange6afd912022-10-27 12:11:18 +08001878#endif
1879
Ronald Cron18b92a12024-03-26 10:15:08 +01001880#if defined(MBEDTLS_SSL_SRV_C)
1881 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
1882#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1883#if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN)
1884 int ret = mbedtls_ssl_session_set_ticket_alpn(session, "ALPNExample");
1885 if (ret != 0) {
1886 return -1;
1887 }
1888#endif
1889#if defined(MBEDTLS_HAVE_TIME)
1890 session->ticket_creation_time = mbedtls_ms_time() - 42;
1891#endif
1892#endif /* MBEDTLS_SSL_SESSION_TICKETS */
1893 }
1894#endif /* MBEDTLS_SSL_SRV_C */
1895
Yanray Wange6afd912022-10-27 12:11:18 +08001896#if defined(MBEDTLS_SSL_CLI_C)
1897 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Ronald Cron18b92a12024-03-26 10:15:08 +01001898#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Yanray Wange6afd912022-10-27 12:11:18 +08001899#if defined(MBEDTLS_HAVE_TIME)
Jerry Yu342a5552023-11-10 14:23:39 +08001900 session->ticket_reception_time = mbedtls_ms_time() - 40;
Yanray Wange6afd912022-10-27 12:11:18 +08001901#endif
1902 session->ticket_lifetime = 0xfedcba98;
1903
1904 session->ticket_len = ticket_len;
1905 if (ticket_len != 0) {
1906 session->ticket = mbedtls_calloc(1, ticket_len);
1907 if (session->ticket == NULL) {
1908 return -1;
1909 }
1910 memset(session->ticket, 33, ticket_len);
1911 }
Ronald Cron8d15e012024-03-27 09:30:13 +01001912#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1913 char hostname[] = "hostname example";
1914 session->hostname = mbedtls_calloc(1, sizeof(hostname));
1915 if (session->hostname == NULL) {
1916 return -1;
1917 }
1918 memcpy(session->hostname, hostname, sizeof(hostname));
1919#endif
Ronald Cron18b92a12024-03-26 10:15:08 +01001920#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Yanray Wange6afd912022-10-27 12:11:18 +08001921 }
1922#endif /* MBEDTLS_SSL_CLI_C */
1923
Ronald Cron18b92a12024-03-26 10:15:08 +01001924#if defined(MBEDTLS_SSL_EARLY_DATA)
1925 session->max_early_data_size = 0x87654321;
1926#endif /* MBEDTLS_SSL_EARLY_DATA */
1927
Waleed Elmelegy049cd302023-12-20 17:28:31 +00001928#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
1929 session->record_size_limit = 2048;
1930#endif
1931
Yanray Wange6afd912022-10-27 12:11:18 +08001932 return 0;
1933}
1934#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1935
Yanray Wangb088bfc2023-03-16 12:15:49 +08001936int mbedtls_test_ssl_exchange_data(
1937 mbedtls_ssl_context *ssl_1,
1938 int msg_len_1, const int expected_fragments_1,
1939 mbedtls_ssl_context *ssl_2,
1940 int msg_len_2, const int expected_fragments_2)
Yanray Wange6afd912022-10-27 12:11:18 +08001941{
1942 unsigned char *msg_buf_1 = malloc(msg_len_1);
1943 unsigned char *msg_buf_2 = malloc(msg_len_2);
1944 unsigned char *in_buf_1 = malloc(msg_len_2);
1945 unsigned char *in_buf_2 = malloc(msg_len_1);
1946 int msg_type, ret = -1;
1947
1948 /* Perform this test with two message types. At first use a message
1949 * consisting of only 0x00 for the client and only 0xFF for the server.
1950 * At the second time use message with generated data */
1951 for (msg_type = 0; msg_type < 2; msg_type++) {
1952 int written_1 = 0;
1953 int written_2 = 0;
1954 int read_1 = 0;
1955 int read_2 = 0;
1956 int fragments_1 = 0;
1957 int fragments_2 = 0;
1958
1959 if (msg_type == 0) {
1960 memset(msg_buf_1, 0x00, msg_len_1);
1961 memset(msg_buf_2, 0xff, msg_len_2);
1962 } else {
1963 int i, j = 0;
1964 for (i = 0; i < msg_len_1; i++) {
1965 msg_buf_1[i] = j++ & 0xFF;
1966 }
1967 for (i = 0; i < msg_len_2; i++) {
1968 msg_buf_2[i] = (j -= 5) & 0xFF;
1969 }
1970 }
1971
1972 while (read_1 < msg_len_2 || read_2 < msg_len_1) {
1973 /* ssl_1 sending */
1974 if (msg_len_1 > written_1) {
1975 ret = mbedtls_ssl_write_fragment(ssl_1, msg_buf_1,
1976 msg_len_1, &written_1,
1977 expected_fragments_1);
1978 if (expected_fragments_1 == 0) {
1979 /* This error is expected when the message is too large and
1980 * cannot be fragmented */
Gilles Peskine353eb332025-05-14 17:42:53 +02001981 TEST_EQUAL(ret, MBEDTLS_ERR_SSL_BAD_INPUT_DATA);
Yanray Wange6afd912022-10-27 12:11:18 +08001982 msg_len_1 = 0;
1983 } else {
Gilles Peskine353eb332025-05-14 17:42:53 +02001984 TEST_EQUAL(ret, 0);
Yanray Wange6afd912022-10-27 12:11:18 +08001985 }
1986 }
1987
1988 /* ssl_2 sending */
1989 if (msg_len_2 > written_2) {
1990 ret = mbedtls_ssl_write_fragment(ssl_2, msg_buf_2,
1991 msg_len_2, &written_2,
1992 expected_fragments_2);
1993 if (expected_fragments_2 == 0) {
1994 /* This error is expected when the message is too large and
1995 * cannot be fragmented */
Gilles Peskine353eb332025-05-14 17:42:53 +02001996 TEST_EQUAL(ret, MBEDTLS_ERR_SSL_BAD_INPUT_DATA);
Yanray Wange6afd912022-10-27 12:11:18 +08001997 msg_len_2 = 0;
1998 } else {
Gilles Peskine353eb332025-05-14 17:42:53 +02001999 TEST_EQUAL(ret, 0);
Yanray Wange6afd912022-10-27 12:11:18 +08002000 }
2001 }
2002
2003 /* ssl_1 reading */
2004 if (read_1 < msg_len_2) {
2005 ret = mbedtls_ssl_read_fragment(ssl_1, in_buf_1,
2006 msg_len_2, &read_1,
2007 &fragments_2,
2008 expected_fragments_2);
Gilles Peskine353eb332025-05-14 17:42:53 +02002009 TEST_EQUAL(ret, 0);
Yanray Wange6afd912022-10-27 12:11:18 +08002010 }
2011
2012 /* ssl_2 reading */
2013 if (read_2 < msg_len_1) {
2014 ret = mbedtls_ssl_read_fragment(ssl_2, in_buf_2,
2015 msg_len_1, &read_2,
2016 &fragments_1,
2017 expected_fragments_1);
Gilles Peskine353eb332025-05-14 17:42:53 +02002018 TEST_EQUAL(ret, 0);
Yanray Wange6afd912022-10-27 12:11:18 +08002019 }
2020 }
2021
2022 ret = -1;
Gilles Peskine353eb332025-05-14 17:42:53 +02002023 TEST_EQUAL(0, memcmp(msg_buf_1, in_buf_2, msg_len_1));
2024 TEST_EQUAL(0, memcmp(msg_buf_2, in_buf_1, msg_len_2));
2025 TEST_EQUAL(fragments_1, expected_fragments_1);
2026 TEST_EQUAL(fragments_2, expected_fragments_2);
Yanray Wange6afd912022-10-27 12:11:18 +08002027 }
2028
2029 ret = 0;
2030
2031exit:
2032 free(msg_buf_1);
2033 free(in_buf_1);
2034 free(msg_buf_2);
2035 free(in_buf_2);
2036
2037 return ret;
2038}
2039
2040/*
2041 * Perform data exchanging between \p ssl_1 and \p ssl_2. Both of endpoints
2042 * must be initialized and connected beforehand.
2043 *
2044 * \retval 0 on success, otherwise error code.
2045 */
Yanray Wangead70c82023-03-16 12:04:49 +08002046#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) && \
2047 (defined(MBEDTLS_SSL_RENEGOTIATION) || \
2048 defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH))
2049static int exchange_data(mbedtls_ssl_context *ssl_1,
2050 mbedtls_ssl_context *ssl_2)
Yanray Wange6afd912022-10-27 12:11:18 +08002051{
Yanray Wangb088bfc2023-03-16 12:15:49 +08002052 return mbedtls_test_ssl_exchange_data(ssl_1, 256, 1,
2053 ssl_2, 256, 1);
Yanray Wange6afd912022-10-27 12:11:18 +08002054}
Yanray Wangead70c82023-03-16 12:04:49 +08002055#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED &&
2056 (MBEDTLS_SSL_RENEGOTIATION ||
2057 MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) */
Yanray Wange6afd912022-10-27 12:11:18 +08002058
2059#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
2060static int check_ssl_version(
2061 mbedtls_ssl_protocol_version expected_negotiated_version,
Gilles Peskinebd953402025-05-28 15:20:28 +02002062 const mbedtls_ssl_context *client,
2063 const mbedtls_ssl_context *server)
Yanray Wange6afd912022-10-27 12:11:18 +08002064{
Gilles Peskinebd953402025-05-28 15:20:28 +02002065 /* First check that both sides have chosen the same version.
2066 * If so, we can make more sanity checks just on one side.
2067 * If not, something is deeply wrong. */
2068 TEST_EQUAL(client->tls_version, server->tls_version);
2069
2070 /* Make further checks on the client to validate that the
2071 * reported data about the version is correct. */
2072 const char *version_string = mbedtls_ssl_get_version(client);
Yanray Wange6afd912022-10-27 12:11:18 +08002073 mbedtls_ssl_protocol_version version_number =
Gilles Peskinebd953402025-05-28 15:20:28 +02002074 mbedtls_ssl_get_version_number(client);
Yanray Wange6afd912022-10-27 12:11:18 +08002075
Gilles Peskinebd953402025-05-28 15:20:28 +02002076 TEST_EQUAL(client->tls_version, expected_negotiated_version);
Yanray Wange6afd912022-10-27 12:11:18 +08002077
Gilles Peskinebd953402025-05-28 15:20:28 +02002078 if (client->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Yanray Wange6afd912022-10-27 12:11:18 +08002079 TEST_EQUAL(version_string[0], 'D');
2080 ++version_string;
2081 }
2082
2083 switch (expected_negotiated_version) {
2084 case MBEDTLS_SSL_VERSION_TLS1_2:
2085 TEST_EQUAL(version_number, MBEDTLS_SSL_VERSION_TLS1_2);
Gilles Peskine353eb332025-05-14 17:42:53 +02002086 TEST_EQUAL(strcmp(version_string, "TLSv1.2"), 0);
Yanray Wange6afd912022-10-27 12:11:18 +08002087 break;
2088
2089 case MBEDTLS_SSL_VERSION_TLS1_3:
2090 TEST_EQUAL(version_number, MBEDTLS_SSL_VERSION_TLS1_3);
Gilles Peskine353eb332025-05-14 17:42:53 +02002091 TEST_EQUAL(strcmp(version_string, "TLSv1.3"), 0);
Yanray Wange6afd912022-10-27 12:11:18 +08002092 break;
2093
2094 default:
Agathiyan Bragadeeshdc28a5a2023-07-18 11:45:28 +01002095 TEST_FAIL(
Agathiyan Bragadeeshebb40bc2023-07-14 17:28:27 +01002096 "Version check not implemented for this protocol version");
Yanray Wange6afd912022-10-27 12:11:18 +08002097 }
2098
2099 return 1;
2100
2101exit:
2102 return 0;
2103}
2104#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
2105
Yanray Wange6afd912022-10-27 12:11:18 +08002106#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Max Fillingercf007ca2024-10-29 16:57:09 +01002107int mbedtls_test_ssl_do_handshake_with_endpoints(
2108 mbedtls_test_ssl_endpoint *server_ep,
2109 mbedtls_test_ssl_endpoint *client_ep,
Max Fillinger8f12e312024-10-30 00:29:37 +01002110 mbedtls_test_handshake_test_options *options,
Max Fillingercf007ca2024-10-29 16:57:09 +01002111 mbedtls_ssl_protocol_version proto)
2112{
2113 enum { BUFFSIZE = 1024 };
2114
2115 int ret = -1;
Max Fillingercf007ca2024-10-29 16:57:09 +01002116
Max Fillingeree467aa2024-11-08 22:17:33 +01002117 mbedtls_platform_zeroize(server_ep, sizeof(mbedtls_test_ssl_endpoint));
2118 mbedtls_platform_zeroize(client_ep, sizeof(mbedtls_test_ssl_endpoint));
2119
Max Fillinger8f12e312024-10-30 00:29:37 +01002120 mbedtls_test_init_handshake_options(options);
2121 options->server_min_version = proto;
2122 options->client_min_version = proto;
2123 options->server_max_version = proto;
2124 options->client_max_version = proto;
Max Fillingercf007ca2024-10-29 16:57:09 +01002125
Gilles Peskineca8a9ac2025-05-27 20:52:24 +02002126 ret = mbedtls_test_ssl_endpoint_init(client_ep, MBEDTLS_SSL_IS_CLIENT, options);
Max Fillingercf007ca2024-10-29 16:57:09 +01002127 if (ret != 0) {
2128 return ret;
2129 }
Gilles Peskineca8a9ac2025-05-27 20:52:24 +02002130 ret = mbedtls_test_ssl_endpoint_init(server_ep, MBEDTLS_SSL_IS_SERVER, options);
Max Fillingercf007ca2024-10-29 16:57:09 +01002131 if (ret != 0) {
2132 return ret;
2133 }
2134
2135 ret = mbedtls_test_mock_socket_connect(&client_ep->socket, &server_ep->socket, BUFFSIZE);
2136 if (ret != 0) {
2137 return ret;
2138 }
2139
Max Fillingerea1e7772024-10-30 00:49:10 +01002140 ret = mbedtls_test_move_handshake_to_state(&server_ep->ssl,
2141 &client_ep->ssl,
2142 MBEDTLS_SSL_HANDSHAKE_OVER);
Max Fillingercf007ca2024-10-29 16:57:09 +01002143 if (ret != 0 && ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
2144 return ret;
2145 }
Max Fillingerea1e7772024-10-30 00:49:10 +01002146 ret = mbedtls_test_move_handshake_to_state(&client_ep->ssl,
2147 &server_ep->ssl,
2148 MBEDTLS_SSL_HANDSHAKE_OVER);
Max Fillingercf007ca2024-10-29 16:57:09 +01002149 if (ret != 0 && ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
2150 return ret;
2151 }
Max Fillingerea1e7772024-10-30 00:49:10 +01002152 if (!mbedtls_ssl_is_handshake_over(&client_ep->ssl) ||
2153 !mbedtls_ssl_is_handshake_over(&server_ep->ssl)) {
Max Fillingercf007ca2024-10-29 16:57:09 +01002154 return -1;
2155 }
2156
2157 return 0;
2158}
2159#endif /* defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) */
2160
2161#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gilles Peskine78df6ae2025-05-27 21:14:25 +02002162
2163#if defined(MBEDTLS_SSL_RENEGOTIATION)
2164static int test_renegotiation(const mbedtls_test_handshake_test_options *options,
2165 mbedtls_test_ssl_endpoint *client,
2166 mbedtls_test_ssl_endpoint *server)
2167{
2168 int ok = 0;
2169 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2170
2171 (void) options; // only used in some configurations
2172
2173 /* Start test with renegotiation */
2174 TEST_EQUAL(server->ssl.renego_status,
2175 MBEDTLS_SSL_INITIAL_HANDSHAKE);
2176 TEST_EQUAL(client->ssl.renego_status,
2177 MBEDTLS_SSL_INITIAL_HANDSHAKE);
2178
2179 /* After calling this function for the server, it only sends a handshake
2180 * request. All renegotiation should happen during data exchanging */
2181 TEST_EQUAL(mbedtls_ssl_renegotiate(&(server->ssl)), 0);
2182 TEST_EQUAL(server->ssl.renego_status,
2183 MBEDTLS_SSL_RENEGOTIATION_PENDING);
2184 TEST_EQUAL(client->ssl.renego_status,
2185 MBEDTLS_SSL_INITIAL_HANDSHAKE);
2186
2187 TEST_EQUAL(exchange_data(&(client->ssl), &(server->ssl)), 0);
2188 TEST_EQUAL(server->ssl.renego_status,
2189 MBEDTLS_SSL_RENEGOTIATION_DONE);
2190 TEST_EQUAL(client->ssl.renego_status,
2191 MBEDTLS_SSL_RENEGOTIATION_DONE);
2192
2193 /* After calling mbedtls_ssl_renegotiate for the client,
2194 * all renegotiation should happen inside this function.
2195 * However in this test, we cannot perform simultaneous communication
2196 * between client and server so this function will return waiting error
2197 * on the socket. All rest of renegotiation should happen
2198 * during data exchanging */
2199 ret = mbedtls_ssl_renegotiate(&(client->ssl));
2200#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2201 if (options->resize_buffers != 0) {
2202 /* Ensure that the buffer sizes are appropriate before resizes */
2203 TEST_EQUAL(client->ssl.out_buf_len, MBEDTLS_SSL_OUT_BUFFER_LEN);
2204 TEST_EQUAL(client->ssl.in_buf_len, MBEDTLS_SSL_IN_BUFFER_LEN);
2205 }
2206#endif
2207 TEST_ASSERT(ret == 0 ||
2208 ret == MBEDTLS_ERR_SSL_WANT_READ ||
2209 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
2210 TEST_EQUAL(server->ssl.renego_status,
2211 MBEDTLS_SSL_RENEGOTIATION_DONE);
2212 TEST_EQUAL(client->ssl.renego_status,
2213 MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS);
2214
2215 TEST_EQUAL(exchange_data(&(client->ssl), &(server->ssl)), 0);
2216 TEST_EQUAL(server->ssl.renego_status,
2217 MBEDTLS_SSL_RENEGOTIATION_DONE);
2218 TEST_EQUAL(client->ssl.renego_status,
2219 MBEDTLS_SSL_RENEGOTIATION_DONE);
2220#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2221 /* Validate buffer sizes after renegotiation */
2222 if (options->resize_buffers != 0) {
2223 TEST_EQUAL(client->ssl.out_buf_len,
2224 mbedtls_ssl_get_output_buflen(&client->ssl));
2225 TEST_EQUAL(client->ssl.in_buf_len,
2226 mbedtls_ssl_get_input_buflen(&client->ssl));
2227 TEST_EQUAL(server->ssl.out_buf_len,
2228 mbedtls_ssl_get_output_buflen(&server->ssl));
2229 TEST_EQUAL(server->ssl.in_buf_len,
2230 mbedtls_ssl_get_input_buflen(&server->ssl));
2231 }
2232#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
2233
2234 ok = 1;
2235
2236exit:
2237 return ok;
2238}
2239#endif /* MBEDTLS_SSL_RENEGOTIATION */
2240
Gilles Peskinee23a6d12025-05-27 21:17:09 +02002241#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
2242static int test_serialization(const mbedtls_test_handshake_test_options *options,
2243 mbedtls_test_ssl_endpoint *client,
2244 mbedtls_test_ssl_endpoint *server)
2245{
2246 int ok = 0;
2247 unsigned char *context_buf = NULL;
2248 size_t context_buf_len;
2249
2250 TEST_EQUAL(options->dtls, 1);
2251
2252 TEST_EQUAL(mbedtls_ssl_context_save(&(server->ssl), NULL,
2253 0, &context_buf_len),
2254 MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL);
2255
2256 context_buf = mbedtls_calloc(1, context_buf_len);
2257 TEST_ASSERT(context_buf != NULL);
2258
2259 TEST_EQUAL(mbedtls_ssl_context_save(&(server->ssl), context_buf,
2260 context_buf_len,
2261 &context_buf_len),
2262 0);
2263
2264 mbedtls_ssl_free(&(server->ssl));
2265 mbedtls_ssl_init(&(server->ssl));
2266
2267 TEST_EQUAL(mbedtls_ssl_setup(&(server->ssl), &(server->conf)), 0);
2268
2269 mbedtls_ssl_set_bio(&(server->ssl), &server->dtls_context,
2270 mbedtls_test_mock_tcp_send_msg,
2271 mbedtls_test_mock_tcp_recv_msg,
2272 NULL);
2273
2274 mbedtls_ssl_set_user_data_p(&server->ssl, server);
2275
2276#if defined(MBEDTLS_TIMING_C)
2277 mbedtls_ssl_set_timer_cb(&server->ssl, &server->timer,
2278 mbedtls_timing_set_delay,
2279 mbedtls_timing_get_delay);
2280#endif
2281#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2282 if (options->resize_buffers != 0) {
2283 /* Ensure that the buffer sizes are appropriate before resizes */
2284 TEST_EQUAL(server->ssl.out_buf_len, MBEDTLS_SSL_OUT_BUFFER_LEN);
2285 TEST_EQUAL(server->ssl.in_buf_len, MBEDTLS_SSL_IN_BUFFER_LEN);
2286 }
2287#endif
2288 TEST_EQUAL(mbedtls_ssl_context_load(&(server->ssl), context_buf,
2289 context_buf_len), 0);
2290
2291#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2292 /* Validate buffer sizes after context deserialization */
2293 if (options->resize_buffers != 0) {
2294 TEST_EQUAL(server->ssl.out_buf_len,
2295 mbedtls_ssl_get_output_buflen(&server->ssl));
2296 TEST_EQUAL(server->ssl.in_buf_len,
2297 mbedtls_ssl_get_input_buflen(&server->ssl));
2298 }
2299#endif
2300 /* Retest writing/reading */
2301 if (options->cli_msg_len != 0 || options->srv_msg_len != 0) {
2302 TEST_EQUAL(mbedtls_test_ssl_exchange_data(
2303 &(client->ssl), options->cli_msg_len,
2304 options->expected_cli_fragments,
2305 &(server->ssl), options->srv_msg_len,
2306 options->expected_srv_fragments),
2307 0);
2308 }
2309
2310 ok = 1;
2311
2312exit:
2313 mbedtls_free(context_buf);
2314 return ok;
2315}
2316#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
2317
Gilles Peskine7a8fd462025-05-28 15:41:54 +02002318int mbedtls_test_ssl_perform_connection(
2319 const mbedtls_test_handshake_test_options *options,
2320 mbedtls_test_ssl_endpoint *client,
2321 mbedtls_test_ssl_endpoint *server)
2322{
2323 enum { BUFFSIZE = 17000 };
2324 int expected_handshake_result = options->expected_handshake_result;
2325 int ok = 0;
2326
2327 TEST_EQUAL(mbedtls_test_mock_socket_connect(&(client->socket),
2328 &(server->socket),
2329 BUFFSIZE), 0);
2330
2331#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2332 if (options->resize_buffers != 0) {
2333 /* Ensure that the buffer sizes are appropriate before resizes */
2334 TEST_EQUAL(client->ssl.out_buf_len, MBEDTLS_SSL_OUT_BUFFER_LEN);
2335 TEST_EQUAL(client->ssl.in_buf_len, MBEDTLS_SSL_IN_BUFFER_LEN);
2336 TEST_EQUAL(server->ssl.out_buf_len, MBEDTLS_SSL_OUT_BUFFER_LEN);
2337 TEST_EQUAL(server->ssl.in_buf_len, MBEDTLS_SSL_IN_BUFFER_LEN);
2338 }
2339#endif
2340
2341 if (options->expected_negotiated_version == MBEDTLS_SSL_VERSION_UNKNOWN) {
2342 expected_handshake_result = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
2343 }
2344
2345 TEST_EQUAL(mbedtls_test_move_handshake_to_state(&(client->ssl),
2346 &(server->ssl),
2347 MBEDTLS_SSL_HANDSHAKE_OVER),
2348 expected_handshake_result);
2349
2350 if (expected_handshake_result != 0) {
2351 /* Connection will have failed by this point, skip to cleanup */
2352 ok = 1;
2353 goto exit;
2354 }
2355
2356 TEST_EQUAL(mbedtls_ssl_is_handshake_over(&client->ssl), 1);
2357
2358 /* Make sure server state is moved to HANDSHAKE_OVER also. */
2359 TEST_EQUAL(mbedtls_test_move_handshake_to_state(&(server->ssl),
2360 &(client->ssl),
2361 MBEDTLS_SSL_HANDSHAKE_OVER),
2362 0);
2363
2364 TEST_EQUAL(mbedtls_ssl_is_handshake_over(&server->ssl), 1);
2365
2366 /* Check that both sides have negotiated the expected version. */
2367 TEST_ASSERT(check_ssl_version(options->expected_negotiated_version,
2368 &client->ssl,
2369 &server->ssl));
2370
2371 if (options->expected_ciphersuite != 0) {
2372 TEST_EQUAL(server->ssl.session->ciphersuite,
2373 options->expected_ciphersuite);
2374 }
2375
2376#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2377 if (options->resize_buffers != 0) {
2378 /* A server, when using DTLS, might delay a buffer resize to happen
2379 * after it receives a message, so we force it. */
2380 TEST_EQUAL(exchange_data(&(client->ssl), &(server->ssl)), 0);
2381
2382 TEST_EQUAL(client->ssl.out_buf_len,
2383 mbedtls_ssl_get_output_buflen(&client->ssl));
2384 TEST_EQUAL(client->ssl.in_buf_len,
2385 mbedtls_ssl_get_input_buflen(&client->ssl));
2386 TEST_EQUAL(server->ssl.out_buf_len,
2387 mbedtls_ssl_get_output_buflen(&server->ssl));
2388 TEST_EQUAL(server->ssl.in_buf_len,
2389 mbedtls_ssl_get_input_buflen(&server->ssl));
2390 }
2391#endif
2392
2393 if (options->cli_msg_len != 0 || options->srv_msg_len != 0) {
2394 /* Start data exchanging test */
2395 TEST_EQUAL(mbedtls_test_ssl_exchange_data(
2396 &(client->ssl), options->cli_msg_len,
2397 options->expected_cli_fragments,
2398 &(server->ssl), options->srv_msg_len,
2399 options->expected_srv_fragments),
2400 0);
2401 }
2402#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
2403 if (options->serialize == 1) {
2404 TEST_ASSERT(test_serialization(options, client, server));
2405 }
2406#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
2407
2408#if defined(MBEDTLS_SSL_RENEGOTIATION)
2409 if (options->renegotiate) {
2410 TEST_ASSERT(test_renegotiation(options, client, server));
2411 }
2412#endif /* MBEDTLS_SSL_RENEGOTIATION */
2413
2414 ok = 1;
2415
2416exit:
2417 return ok;
2418}
2419
Yanray Wange6afd912022-10-27 12:11:18 +08002420void mbedtls_test_ssl_perform_handshake(
Gilles Peskine9b993682025-05-27 18:44:12 +02002421 const mbedtls_test_handshake_test_options *options)
Yanray Wange6afd912022-10-27 12:11:18 +08002422{
Gilles Peskinee30b5c72025-05-27 21:05:48 +02002423 mbedtls_test_ssl_endpoint client_struct;
2424 memset(&client_struct, 0, sizeof(client_struct));
2425 mbedtls_test_ssl_endpoint *const client = &client_struct;
2426 mbedtls_test_ssl_endpoint server_struct;
2427 memset(&server_struct, 0, sizeof(server_struct));
2428 mbedtls_test_ssl_endpoint *const server = &server_struct;
Yanray Wange6afd912022-10-27 12:11:18 +08002429
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +01002430 MD_OR_USE_PSA_INIT();
Yanray Wange6afd912022-10-27 12:11:18 +08002431
Ronald Cronec3408d2024-01-16 17:50:40 +01002432#if defined(MBEDTLS_DEBUG_C)
2433 if (options->cli_log_fun || options->srv_log_fun) {
2434 mbedtls_debug_set_threshold(4);
2435 }
2436#endif
2437
Yanray Wange6afd912022-10-27 12:11:18 +08002438 /* Client side */
Gilles Peskinee30b5c72025-05-27 21:05:48 +02002439 TEST_EQUAL(mbedtls_test_ssl_endpoint_init(client,
Gilles Peskine07432b92025-05-27 21:07:44 +02002440 MBEDTLS_SSL_IS_CLIENT,
2441 options), 0);
Yanray Wange6afd912022-10-27 12:11:18 +08002442
Yanray Wange6afd912022-10-27 12:11:18 +08002443 /* Server side */
Gilles Peskinee30b5c72025-05-27 21:05:48 +02002444 TEST_EQUAL(mbedtls_test_ssl_endpoint_init(server,
Gilles Peskine07432b92025-05-27 21:07:44 +02002445 MBEDTLS_SSL_IS_SERVER,
2446 options), 0);
Yanray Wange6afd912022-10-27 12:11:18 +08002447
Gilles Peskineb092e782025-05-27 20:15:03 +02002448 if (options->dtls) {
Gilles Peskinee30b5c72025-05-27 21:05:48 +02002449 TEST_EQUAL(mbedtls_test_ssl_dtls_join_endpoints(client, server), 0);
Gilles Peskineb092e782025-05-27 20:15:03 +02002450 }
2451
Gilles Peskine7a8fd462025-05-28 15:41:54 +02002452 TEST_ASSERT(mbedtls_test_ssl_perform_connection(options, client, server));
Yanray Wange6afd912022-10-27 12:11:18 +08002453
Gilles Peskinee30b5c72025-05-27 21:05:48 +02002454 TEST_ASSERT(mbedtls_ssl_conf_get_user_data_p(&client->conf) == client);
2455 TEST_ASSERT(mbedtls_ssl_get_user_data_p(&client->ssl) == client);
2456 TEST_ASSERT(mbedtls_ssl_conf_get_user_data_p(&server->conf) == server);
2457 TEST_ASSERT(mbedtls_ssl_get_user_data_p(&server->ssl) == server);
Yanray Wange6afd912022-10-27 12:11:18 +08002458
2459exit:
Gilles Peskinee30b5c72025-05-27 21:05:48 +02002460 mbedtls_test_ssl_endpoint_free(client);
2461 mbedtls_test_ssl_endpoint_free(server);
Yanray Wange6afd912022-10-27 12:11:18 +08002462#if defined(MBEDTLS_DEBUG_C)
2463 if (options->cli_log_fun || options->srv_log_fun) {
2464 mbedtls_debug_set_threshold(0);
2465 }
2466#endif
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +01002467 MD_OR_USE_PSA_DONE();
Yanray Wange6afd912022-10-27 12:11:18 +08002468}
2469#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
2470
2471#if defined(MBEDTLS_TEST_HOOKS)
Yanray Wangf56181a2023-03-16 12:21:33 +08002472int mbedtls_test_tweak_tls13_certificate_msg_vector_len(
Yanray Wange6afd912022-10-27 12:11:18 +08002473 unsigned char *buf, unsigned char **end, int tweak,
2474 int *expected_result, mbedtls_ssl_chk_buf_ptr_args *args)
2475{
2476/*
2477 * The definition of the tweaks assume that the certificate list contains only
2478 * one certificate.
2479 */
2480
2481/*
2482 * struct {
2483 * opaque cert_data<1..2^24-1>;
2484 * Extension extensions<0..2^16-1>;
2485 * } CertificateEntry;
2486 *
2487 * struct {
2488 * opaque certificate_request_context<0..2^8-1>;
2489 * CertificateEntry certificate_list<0..2^24-1>;
2490 * } Certificate;
2491 */
2492 unsigned char *p_certificate_request_context_len = buf;
2493 size_t certificate_request_context_len = buf[0];
2494
2495 unsigned char *p_certificate_list_len =
2496 buf + 1 + certificate_request_context_len;
2497 unsigned char *certificate_list = p_certificate_list_len + 3;
2498 size_t certificate_list_len =
2499 MBEDTLS_GET_UINT24_BE(p_certificate_list_len, 0);
2500
2501 unsigned char *p_cert_data_len = certificate_list;
2502 unsigned char *cert_data = p_cert_data_len + 3;
2503 size_t cert_data_len = MBEDTLS_GET_UINT24_BE(p_cert_data_len, 0);
2504
2505 unsigned char *p_extensions_len = cert_data + cert_data_len;
2506 unsigned char *extensions = p_extensions_len + 2;
2507 size_t extensions_len = MBEDTLS_GET_UINT16_BE(p_extensions_len, 0);
2508
2509 *expected_result = MBEDTLS_ERR_SSL_DECODE_ERROR;
2510
2511 switch (tweak) {
2512 case 1:
2513 /* Failure when checking if the certificate request context length
2514 * and certificate list length can be read
2515 */
2516 *end = buf + 3;
2517 set_chk_buf_ptr_args(args, buf, *end, 4);
2518 break;
2519
2520 case 2:
2521 /* Invalid certificate request context length.
2522 */
2523 *p_certificate_request_context_len =
Yanray Wanga8f445e2022-11-03 11:51:59 +08002524 (unsigned char) certificate_request_context_len + 1;
Yanray Wange6afd912022-10-27 12:11:18 +08002525 reset_chk_buf_ptr_args(args);
2526 break;
2527
2528 case 3:
2529 /* Failure when checking if certificate_list data can be read. */
2530 MBEDTLS_PUT_UINT24_BE(certificate_list_len + 1,
2531 p_certificate_list_len, 0);
2532 set_chk_buf_ptr_args(args, certificate_list, *end,
2533 certificate_list_len + 1);
2534 break;
2535
2536 case 4:
2537 /* Failure when checking if the cert_data length can be read. */
2538 MBEDTLS_PUT_UINT24_BE(2, p_certificate_list_len, 0);
2539 set_chk_buf_ptr_args(args, p_cert_data_len, certificate_list + 2, 3);
2540 break;
2541
2542 case 5:
2543 /* Failure when checking if cert_data data can be read. */
2544 MBEDTLS_PUT_UINT24_BE(certificate_list_len - 3 + 1,
2545 p_cert_data_len, 0);
2546 set_chk_buf_ptr_args(args, cert_data,
2547 certificate_list + certificate_list_len,
2548 certificate_list_len - 3 + 1);
2549 break;
2550
2551 case 6:
2552 /* Failure when checking if the extensions length can be read. */
2553 MBEDTLS_PUT_UINT24_BE(certificate_list_len - extensions_len - 1,
2554 p_certificate_list_len, 0);
2555 set_chk_buf_ptr_args(
2556 args, p_extensions_len,
2557 certificate_list + certificate_list_len - extensions_len - 1, 2);
2558 break;
2559
2560 case 7:
2561 /* Failure when checking if extensions data can be read. */
2562 MBEDTLS_PUT_UINT16_BE(extensions_len + 1, p_extensions_len, 0);
2563
2564 set_chk_buf_ptr_args(
2565 args, extensions,
2566 certificate_list + certificate_list_len, extensions_len + 1);
2567 break;
2568
2569 default:
2570 return -1;
2571 }
2572
2573 return 0;
2574}
2575#endif /* MBEDTLS_TEST_HOOKS */
Ronald Cron77abfe62024-01-15 11:17:31 +01002576
Ronald Cronfaf026c2024-01-31 14:32:06 +01002577/*
2578 * Functions for tests based on tickets. Implementations of the
2579 * write/parse ticket interfaces as defined by mbedtls_ssl_ticket_write/parse_t.
2580 * Basically same implementations as in ticket.c without the encryption. That
2581 * way we can tweak easily tickets characteristics to simulate misbehaving
2582 * peers.
2583 */
Ronald Cron77abfe62024-01-15 11:17:31 +01002584#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2585int mbedtls_test_ticket_write(
2586 void *p_ticket, const mbedtls_ssl_session *session,
2587 unsigned char *start, const unsigned char *end,
2588 size_t *tlen, uint32_t *lifetime)
2589{
2590 int ret;
2591 ((void) p_ticket);
2592
2593 if ((ret = mbedtls_ssl_session_save(session, start, end - start,
2594 tlen)) != 0) {
2595 return ret;
2596 }
2597
2598 /* Maximum ticket lifetime as defined in RFC 8446 */
2599 *lifetime = 7 * 24 * 3600;
2600
2601 return 0;
2602}
2603
2604int mbedtls_test_ticket_parse(void *p_ticket, mbedtls_ssl_session *session,
2605 unsigned char *buf, size_t len)
2606{
2607 ((void) p_ticket);
2608
2609 return mbedtls_ssl_session_load(session, buf, len);
2610}
2611#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Ronald Cron1f6e4e42024-01-26 16:31:33 +01002612
2613#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SRV_C) && \
2614 defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS) && \
2615 defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
2616int mbedtls_test_get_tls13_ticket(
2617 mbedtls_test_handshake_test_options *client_options,
2618 mbedtls_test_handshake_test_options *server_options,
2619 mbedtls_ssl_session *session)
2620{
2621 int ret = -1;
Gilles Peskine946bf142025-04-08 09:48:40 +02002622 int ok = 0;
Ronald Cron1f6e4e42024-01-26 16:31:33 +01002623 unsigned char buf[64];
2624 mbedtls_test_ssl_endpoint client_ep, server_ep;
2625
2626 mbedtls_platform_zeroize(&client_ep, sizeof(client_ep));
2627 mbedtls_platform_zeroize(&server_ep, sizeof(server_ep));
2628
2629 ret = mbedtls_test_ssl_endpoint_init(&client_ep, MBEDTLS_SSL_IS_CLIENT,
Gilles Peskineca8a9ac2025-05-27 20:52:24 +02002630 client_options);
Ronald Cron1f6e4e42024-01-26 16:31:33 +01002631 TEST_EQUAL(ret, 0);
2632
2633 ret = mbedtls_test_ssl_endpoint_init(&server_ep, MBEDTLS_SSL_IS_SERVER,
Gilles Peskineca8a9ac2025-05-27 20:52:24 +02002634 server_options);
Ronald Cron1f6e4e42024-01-26 16:31:33 +01002635 TEST_EQUAL(ret, 0);
2636
2637 mbedtls_ssl_conf_session_tickets_cb(&server_ep.conf,
2638 mbedtls_test_ticket_write,
2639 mbedtls_test_ticket_parse,
2640 NULL);
2641
2642 ret = mbedtls_test_mock_socket_connect(&(client_ep.socket),
2643 &(server_ep.socket), 1024);
2644 TEST_EQUAL(ret, 0);
2645
2646 TEST_EQUAL(mbedtls_test_move_handshake_to_state(
2647 &(server_ep.ssl), &(client_ep.ssl),
2648 MBEDTLS_SSL_HANDSHAKE_OVER), 0);
2649
2650 TEST_EQUAL(server_ep.ssl.handshake->new_session_tickets_count, 0);
2651
2652 do {
2653 ret = mbedtls_ssl_read(&(client_ep.ssl), buf, sizeof(buf));
2654 } while (ret != MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET);
2655
2656 ret = mbedtls_ssl_get_session(&(client_ep.ssl), session);
2657 TEST_EQUAL(ret, 0);
2658
Gilles Peskine946bf142025-04-08 09:48:40 +02002659 ok = 1;
2660
Ronald Cron1f6e4e42024-01-26 16:31:33 +01002661exit:
Gilles Peskineca8a9ac2025-05-27 20:52:24 +02002662 mbedtls_test_ssl_endpoint_free(&client_ep);
2663 mbedtls_test_ssl_endpoint_free(&server_ep);
Ronald Cron1f6e4e42024-01-26 16:31:33 +01002664
Gilles Peskine946bf142025-04-08 09:48:40 +02002665 if (ret == 0 && !ok) {
2666 /* Exiting due to a test assertion that isn't ret == 0 */
2667 ret = -1;
2668 }
Ronald Cron1f6e4e42024-01-26 16:31:33 +01002669 return ret;
2670}
2671#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SRV_C &&
2672 MBEDTLS_SSL_PROTO_TLS1_3 && MBEDTLS_SSL_SESSION_TICKETS &&
2673 MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
2674
Yanray Wang4d07d1c2022-10-27 15:28:16 +08002675#endif /* MBEDTLS_SSL_TLS_C */