blob: dc34892084a4ea1b6a7d21d42b7b5b7776b002dd [file] [log] [blame]
Yanray Wang47907a42022-10-24 14:42:01 +08001/** \file ssl_helpers.c
2 *
3 * \brief Helper functions to set up a TLS connection.
4 */
5
6/*
7 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00008 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Yanray Wang47907a42022-10-24 14:42:01 +08009 */
10
11#include <test/ssl_helpers.h>
Valerio Setti384fbde2024-01-02 13:26:40 +010012#include "mbedtls/psa_util.h"
Yanray Wange6afd912022-10-27 12:11:18 +080013
Yanray Wang4d07d1c2022-10-27 15:28:16 +080014#if defined(MBEDTLS_SSL_TLS_C)
Ronald Cron10b040f2024-02-05 09:38:09 +010015int mbedtls_test_random(void *p_rng, unsigned char *output, size_t output_len)
Yanray Wange6afd912022-10-27 12:11:18 +080016{
17 (void) p_rng;
18 for (size_t i = 0; i < output_len; i++) {
19 output[i] = rand();
20 }
21
22 return 0;
23}
Yanray Wange6afd912022-10-27 12:11:18 +080024
Yanray Wange6afd912022-10-27 12:11:18 +080025void mbedtls_test_ssl_log_analyzer(void *ctx, int level,
26 const char *file, int line,
27 const char *str)
28{
29 mbedtls_test_ssl_log_pattern *p = (mbedtls_test_ssl_log_pattern *) ctx;
30
Manuel Pégourié-Gonnard6637ef72025-02-11 13:19:45 +010031/* Change 0 to 1 for debugging of test cases that use this function. */
32#if 0
33 const char *q, *basename;
34 /* Extract basename from file */
35 for (q = basename = file; *q != '\0'; q++) {
36 if (*q == '/' || *q == '\\') {
37 basename = q + 1;
38 }
39 }
40 printf("%s:%04d: |%d| %s",
41 basename, line, level, str);
42#else
Yanray Wange6afd912022-10-27 12:11:18 +080043 (void) level;
44 (void) line;
45 (void) file;
Manuel Pégourié-Gonnard6637ef72025-02-11 13:19:45 +010046#endif
Yanray Wange6afd912022-10-27 12:11:18 +080047
48 if (NULL != p &&
49 NULL != p->pattern &&
50 NULL != strstr(str, p->pattern)) {
51 p->counter++;
52 }
53}
54
55void mbedtls_test_init_handshake_options(
56 mbedtls_test_handshake_test_options *opts)
57{
58#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Ronald Cron987cf892024-03-04 10:24:27 +010059 static int rng_seed = 0xBEEF;
Yanray Wanga72bc9a2023-12-01 23:34:27 +080060
Yanray Wange6afd912022-10-27 12:11:18 +080061 srand(rng_seed);
62 rng_seed += 0xD0;
63#endif
Ronald Cronb4ad3e72024-01-26 14:57:53 +010064
65 memset(opts, 0, sizeof(*opts));
66
Yanray Wange6afd912022-10-27 12:11:18 +080067 opts->cipher = "";
68 opts->client_min_version = MBEDTLS_SSL_VERSION_UNKNOWN;
69 opts->client_max_version = MBEDTLS_SSL_VERSION_UNKNOWN;
70 opts->server_min_version = MBEDTLS_SSL_VERSION_UNKNOWN;
71 opts->server_max_version = MBEDTLS_SSL_VERSION_UNKNOWN;
Ronald Cron097ba142023-03-08 16:18:00 +010072 opts->expected_negotiated_version = MBEDTLS_SSL_VERSION_TLS1_3;
Yanray Wange6afd912022-10-27 12:11:18 +080073 opts->pk_alg = MBEDTLS_PK_RSA;
Yanray Wange6afd912022-10-27 12:11:18 +080074 opts->srv_auth_mode = MBEDTLS_SSL_VERIFY_NONE;
Yanray Wange6afd912022-10-27 12:11:18 +080075 opts->mfl = MBEDTLS_SSL_MAX_FRAG_LEN_NONE;
76 opts->cli_msg_len = 100;
77 opts->srv_msg_len = 100;
78 opts->expected_cli_fragments = 1;
79 opts->expected_srv_fragments = 1;
Yanray Wange6afd912022-10-27 12:11:18 +080080 opts->legacy_renegotiation = MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION;
Yanray Wange6afd912022-10-27 12:11:18 +080081 opts->resize_buffers = 1;
Ronald Cronced99be2024-01-26 15:49:12 +010082 opts->early_data = MBEDTLS_SSL_EARLY_DATA_DISABLED;
Ronald Cron5d3036e2024-02-23 07:43:45 +010083 opts->max_early_data_size = -1;
Yanray Wange6afd912022-10-27 12:11:18 +080084#if defined(MBEDTLS_SSL_CACHE_C)
Tom Cosgrove05b2a872023-07-21 11:31:13 +010085 TEST_CALLOC(opts->cache, 1);
Yanray Wange6afd912022-10-27 12:11:18 +080086 mbedtls_ssl_cache_init(opts->cache);
Pengyu Lv5cbb93e2023-07-10 11:09:40 +080087#if defined(MBEDTLS_HAVE_TIME)
88 TEST_EQUAL(mbedtls_ssl_cache_get_timeout(opts->cache),
89 MBEDTLS_SSL_CACHE_DEFAULT_TIMEOUT);
90#endif
Yanray Wange6afd912022-10-27 12:11:18 +080091exit:
92 return;
93#endif
94}
95
96void mbedtls_test_free_handshake_options(
97 mbedtls_test_handshake_test_options *opts)
98{
99#if defined(MBEDTLS_SSL_CACHE_C)
100 mbedtls_ssl_cache_free(opts->cache);
101 mbedtls_free(opts->cache);
102#else
103 (void) opts;
104#endif
105}
106
107#if defined(MBEDTLS_TEST_HOOKS)
108static void set_chk_buf_ptr_args(
109 mbedtls_ssl_chk_buf_ptr_args *args,
110 unsigned char *cur, unsigned char *end, size_t need)
111{
112 args->cur = cur;
113 args->end = end;
114 args->need = need;
115}
116
117static void reset_chk_buf_ptr_args(mbedtls_ssl_chk_buf_ptr_args *args)
118{
119 memset(args, 0, sizeof(*args));
120}
121#endif /* MBEDTLS_TEST_HOOKS */
122
Yanray Wange6afd912022-10-27 12:11:18 +0800123void mbedtls_test_ssl_buffer_init(mbedtls_test_ssl_buffer *buf)
124{
125 memset(buf, 0, sizeof(*buf));
126}
127
Yanray Wange6afd912022-10-27 12:11:18 +0800128int mbedtls_test_ssl_buffer_setup(mbedtls_test_ssl_buffer *buf,
129 size_t capacity)
130{
131 buf->buffer = (unsigned char *) mbedtls_calloc(capacity,
132 sizeof(unsigned char));
133 if (NULL == buf->buffer) {
134 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
135 }
136 buf->capacity = capacity;
137
138 return 0;
139}
140
141void mbedtls_test_ssl_buffer_free(mbedtls_test_ssl_buffer *buf)
142{
143 if (buf->buffer != NULL) {
144 mbedtls_free(buf->buffer);
145 }
146
147 memset(buf, 0, sizeof(*buf));
148}
149
Yanray Wange6afd912022-10-27 12:11:18 +0800150int mbedtls_test_ssl_buffer_put(mbedtls_test_ssl_buffer *buf,
151 const unsigned char *input, size_t input_len)
152{
153 size_t overflow = 0;
154
155 if ((buf == NULL) || (buf->buffer == NULL)) {
156 return -1;
157 }
158
159 /* Reduce input_len to a number that fits in the buffer. */
160 if ((buf->content_length + input_len) > buf->capacity) {
161 input_len = buf->capacity - buf->content_length;
162 }
163
164 if (input == NULL) {
165 return (input_len == 0) ? 0 : -1;
166 }
167
168 /* Check if the buffer has not come full circle and free space is not in
169 * the middle */
170 if (buf->start + buf->content_length < buf->capacity) {
171
172 /* Calculate the number of bytes that need to be placed at lower memory
173 * address */
174 if (buf->start + buf->content_length + input_len
175 > buf->capacity) {
176 overflow = (buf->start + buf->content_length + input_len)
177 % buf->capacity;
178 }
179
180 memcpy(buf->buffer + buf->start + buf->content_length, input,
181 input_len - overflow);
182 memcpy(buf->buffer, input + input_len - overflow, overflow);
183
184 } else {
185 /* The buffer has come full circle and free space is in the middle */
186 memcpy(buf->buffer + buf->start + buf->content_length - buf->capacity,
187 input, input_len);
188 }
189
190 buf->content_length += input_len;
Yanray Wanga8f445e2022-11-03 11:51:59 +0800191 return (input_len > INT_MAX) ? INT_MAX : (int) input_len;
Yanray Wange6afd912022-10-27 12:11:18 +0800192}
193
Yanray Wange6afd912022-10-27 12:11:18 +0800194int mbedtls_test_ssl_buffer_get(mbedtls_test_ssl_buffer *buf,
195 unsigned char *output, size_t output_len)
196{
197 size_t overflow = 0;
198
199 if ((buf == NULL) || (buf->buffer == NULL)) {
200 return -1;
201 }
202
203 if (output == NULL && output_len == 0) {
204 return 0;
205 }
206
207 if (buf->content_length < output_len) {
208 output_len = buf->content_length;
209 }
210
211 /* Calculate the number of bytes that need to be drawn from lower memory
212 * address */
213 if (buf->start + output_len > buf->capacity) {
214 overflow = (buf->start + output_len) % buf->capacity;
215 }
216
217 if (output != NULL) {
218 memcpy(output, buf->buffer + buf->start, output_len - overflow);
219 memcpy(output + output_len - overflow, buf->buffer, overflow);
220 }
221
222 buf->content_length -= output_len;
223 buf->start = (buf->start + output_len) % buf->capacity;
224
Yanray Wanga8f445e2022-11-03 11:51:59 +0800225 return (output_len > INT_MAX) ? INT_MAX : (int) output_len;
Yanray Wange6afd912022-10-27 12:11:18 +0800226}
227
Yanray Wangd19894f2023-03-16 11:47:39 +0800228int mbedtls_test_ssl_message_queue_setup(
229 mbedtls_test_ssl_message_queue *queue, size_t capacity)
Yanray Wange6afd912022-10-27 12:11:18 +0800230{
231 queue->messages = (size_t *) mbedtls_calloc(capacity, sizeof(size_t));
232 if (NULL == queue->messages) {
233 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
234 }
235
Yanray Wanga8f445e2022-11-03 11:51:59 +0800236 queue->capacity = (capacity > INT_MAX) ? INT_MAX : (int) capacity;
Yanray Wange6afd912022-10-27 12:11:18 +0800237 queue->pos = 0;
238 queue->num = 0;
239
240 return 0;
241}
242
Yanray Wangd19894f2023-03-16 11:47:39 +0800243void mbedtls_test_ssl_message_queue_free(
244 mbedtls_test_ssl_message_queue *queue)
Yanray Wange6afd912022-10-27 12:11:18 +0800245{
246 if (queue == NULL) {
247 return;
248 }
249
250 if (queue->messages != NULL) {
251 mbedtls_free(queue->messages);
252 }
253
254 memset(queue, 0, sizeof(*queue));
255}
256
Yanray Wange6afd912022-10-27 12:11:18 +0800257int mbedtls_test_ssl_message_queue_push_info(
258 mbedtls_test_ssl_message_queue *queue, size_t len)
259{
260 int place;
261 if (queue == NULL) {
262 return MBEDTLS_TEST_ERROR_ARG_NULL;
263 }
264
265 if (queue->num >= queue->capacity) {
266 return MBEDTLS_ERR_SSL_WANT_WRITE;
267 }
268
269 place = (queue->pos + queue->num) % queue->capacity;
270 queue->messages[place] = len;
271 queue->num++;
Yanray Wanga8f445e2022-11-03 11:51:59 +0800272 return (len > INT_MAX) ? INT_MAX : (int) len;
Yanray Wange6afd912022-10-27 12:11:18 +0800273}
274
Yanray Wange6afd912022-10-27 12:11:18 +0800275int mbedtls_test_ssl_message_queue_pop_info(
276 mbedtls_test_ssl_message_queue *queue, size_t buf_len)
277{
278 size_t message_length;
279 if (queue == NULL) {
280 return MBEDTLS_TEST_ERROR_ARG_NULL;
281 }
282 if (queue->num == 0) {
283 return MBEDTLS_ERR_SSL_WANT_READ;
284 }
285
286 message_length = queue->messages[queue->pos];
287 queue->messages[queue->pos] = 0;
288 queue->num--;
289 queue->pos++;
290 queue->pos %= queue->capacity;
291 if (queue->pos < 0) {
292 queue->pos += queue->capacity;
293 }
294
Yanray Wanga8f445e2022-11-03 11:51:59 +0800295 return (message_length > INT_MAX && buf_len > INT_MAX) ? INT_MAX :
296 (message_length > buf_len) ? (int) buf_len : (int) message_length;
Yanray Wange6afd912022-10-27 12:11:18 +0800297}
298
299/*
300 * Take a peek on the info about the next message length from the queue.
301 * This will be the oldest inserted message length(fifo).
302 *
303 * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null.
304 * \retval MBEDTLS_ERR_SSL_WANT_READ, if the queue is empty.
305 * \retval 0, if the peek was successful.
306 * \retval MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED, if the given buffer length is
307 * too small to fit the message. In this case the \p msg_len will be
308 * set to the full message length so that the
309 * caller knows what portion of the message can be dropped.
310 */
Yanray Wang5e22a922023-03-16 14:57:54 +0800311static int test_ssl_message_queue_peek_info(
312 mbedtls_test_ssl_message_queue *queue,
313 size_t buf_len, size_t *msg_len)
Yanray Wange6afd912022-10-27 12:11:18 +0800314{
315 if (queue == NULL || msg_len == NULL) {
316 return MBEDTLS_TEST_ERROR_ARG_NULL;
317 }
318 if (queue->num == 0) {
319 return MBEDTLS_ERR_SSL_WANT_READ;
320 }
321
322 *msg_len = queue->messages[queue->pos];
323 return (*msg_len > buf_len) ? MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED : 0;
324}
325
Yanray Wang5f86a422023-03-15 16:02:29 +0800326void mbedtls_test_mock_socket_init(mbedtls_test_mock_socket *socket)
Yanray Wange6afd912022-10-27 12:11:18 +0800327{
328 memset(socket, 0, sizeof(*socket));
329}
330
Yanray Wange6afd912022-10-27 12:11:18 +0800331void mbedtls_test_mock_socket_close(mbedtls_test_mock_socket *socket)
332{
333 if (socket == NULL) {
334 return;
335 }
336
337 if (socket->input != NULL) {
338 mbedtls_test_ssl_buffer_free(socket->input);
339 mbedtls_free(socket->input);
340 }
341
342 if (socket->output != NULL) {
343 mbedtls_test_ssl_buffer_free(socket->output);
344 mbedtls_free(socket->output);
345 }
346
347 if (socket->peer != NULL) {
348 memset(socket->peer, 0, sizeof(*socket->peer));
349 }
350
351 memset(socket, 0, sizeof(*socket));
352}
353
Yanray Wange6afd912022-10-27 12:11:18 +0800354int mbedtls_test_mock_socket_connect(mbedtls_test_mock_socket *peer1,
355 mbedtls_test_mock_socket *peer2,
356 size_t bufsize)
357{
358 int ret = -1;
359
360 peer1->output =
361 (mbedtls_test_ssl_buffer *) mbedtls_calloc(
362 1, sizeof(mbedtls_test_ssl_buffer));
363 if (peer1->output == NULL) {
364 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
365 goto exit;
366 }
367 mbedtls_test_ssl_buffer_init(peer1->output);
368 if (0 != (ret = mbedtls_test_ssl_buffer_setup(peer1->output, bufsize))) {
369 goto exit;
370 }
371
372 peer2->output =
373 (mbedtls_test_ssl_buffer *) mbedtls_calloc(
374 1, sizeof(mbedtls_test_ssl_buffer));
375 if (peer2->output == NULL) {
376 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
377 goto exit;
378 }
379 mbedtls_test_ssl_buffer_init(peer2->output);
380 if (0 != (ret = mbedtls_test_ssl_buffer_setup(peer2->output, bufsize))) {
381 goto exit;
382 }
383
384 peer1->peer = peer2;
385 peer2->peer = peer1;
386 peer1->input = peer2->output;
387 peer2->input = peer1->output;
388
389 peer1->status = peer2->status = MBEDTLS_MOCK_SOCKET_CONNECTED;
390 ret = 0;
391
392exit:
393
394 if (ret != 0) {
395 mbedtls_test_mock_socket_close(peer1);
396 mbedtls_test_mock_socket_close(peer2);
397 }
398
399 return ret;
400}
401
Yanray Wangaf727a22023-03-13 19:22:36 +0800402int mbedtls_test_mock_tcp_send_b(void *ctx,
403 const unsigned char *buf, size_t len)
Yanray Wange6afd912022-10-27 12:11:18 +0800404{
405 mbedtls_test_mock_socket *socket = (mbedtls_test_mock_socket *) ctx;
406
407 if (socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED) {
408 return -1;
409 }
410
411 return mbedtls_test_ssl_buffer_put(socket->output, buf, len);
412}
413
414int mbedtls_test_mock_tcp_recv_b(void *ctx, unsigned char *buf, size_t len)
415{
416 mbedtls_test_mock_socket *socket = (mbedtls_test_mock_socket *) ctx;
417
418 if (socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED) {
419 return -1;
420 }
421
422 return mbedtls_test_ssl_buffer_get(socket->input, buf, len);
423}
424
Yanray Wang34634352023-02-14 17:56:51 +0800425int mbedtls_test_mock_tcp_send_nb(void *ctx,
426 const unsigned char *buf, size_t len)
Yanray Wange6afd912022-10-27 12:11:18 +0800427{
428 mbedtls_test_mock_socket *socket = (mbedtls_test_mock_socket *) ctx;
429
430 if (socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED) {
431 return -1;
432 }
433
434 if (socket->output->capacity == socket->output->content_length) {
435 return MBEDTLS_ERR_SSL_WANT_WRITE;
436 }
437
438 return mbedtls_test_ssl_buffer_put(socket->output, buf, len);
439}
440
441int mbedtls_test_mock_tcp_recv_nb(void *ctx, unsigned char *buf, size_t len)
442{
443 mbedtls_test_mock_socket *socket = (mbedtls_test_mock_socket *) ctx;
444
445 if (socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED) {
446 return -1;
447 }
448
449 if (socket->input->content_length == 0) {
450 return MBEDTLS_ERR_SSL_WANT_READ;
451 }
452
453 return mbedtls_test_ssl_buffer_get(socket->input, buf, len);
454}
455
Yanray Wangd19894f2023-03-16 11:47:39 +0800456void mbedtls_test_message_socket_init(
457 mbedtls_test_message_socket_context *ctx)
Yanray Wange6afd912022-10-27 12:11:18 +0800458{
459 ctx->queue_input = NULL;
460 ctx->queue_output = NULL;
461 ctx->socket = NULL;
462}
463
Yanray Wange6afd912022-10-27 12:11:18 +0800464int mbedtls_test_message_socket_setup(
465 mbedtls_test_ssl_message_queue *queue_input,
466 mbedtls_test_ssl_message_queue *queue_output,
467 size_t queue_capacity,
468 mbedtls_test_mock_socket *socket,
469 mbedtls_test_message_socket_context *ctx)
470{
471 int ret = mbedtls_test_ssl_message_queue_setup(queue_input, queue_capacity);
472 if (ret != 0) {
473 return ret;
474 }
475 ctx->queue_input = queue_input;
476 ctx->queue_output = queue_output;
477 ctx->socket = socket;
Yanray Wang5f86a422023-03-15 16:02:29 +0800478 mbedtls_test_mock_socket_init(socket);
Yanray Wange6afd912022-10-27 12:11:18 +0800479
480 return 0;
481}
482
Yanray Wangd19894f2023-03-16 11:47:39 +0800483void mbedtls_test_message_socket_close(
484 mbedtls_test_message_socket_context *ctx)
Yanray Wange6afd912022-10-27 12:11:18 +0800485{
486 if (ctx == NULL) {
487 return;
488 }
489
490 mbedtls_test_ssl_message_queue_free(ctx->queue_input);
491 mbedtls_test_mock_socket_close(ctx->socket);
492 memset(ctx, 0, sizeof(*ctx));
493}
494
Yanray Wang34634352023-02-14 17:56:51 +0800495int mbedtls_test_mock_tcp_send_msg(void *ctx,
496 const unsigned char *buf, size_t len)
Yanray Wange6afd912022-10-27 12:11:18 +0800497{
498 mbedtls_test_ssl_message_queue *queue;
499 mbedtls_test_mock_socket *socket;
500 mbedtls_test_message_socket_context *context =
501 (mbedtls_test_message_socket_context *) ctx;
502
503 if (context == NULL || context->socket == NULL
504 || context->queue_output == NULL) {
505 return MBEDTLS_TEST_ERROR_CONTEXT_ERROR;
506 }
507
508 queue = context->queue_output;
509 socket = context->socket;
510
511 if (queue->num >= queue->capacity) {
512 return MBEDTLS_ERR_SSL_WANT_WRITE;
513 }
514
515 if (mbedtls_test_mock_tcp_send_b(socket, buf, len) != (int) len) {
516 return MBEDTLS_TEST_ERROR_SEND_FAILED;
517 }
518
519 return mbedtls_test_ssl_message_queue_push_info(queue, len);
520}
521
Yanray Wang34634352023-02-14 17:56:51 +0800522int mbedtls_test_mock_tcp_recv_msg(void *ctx,
523 unsigned char *buf, size_t buf_len)
Yanray Wange6afd912022-10-27 12:11:18 +0800524{
525 mbedtls_test_ssl_message_queue *queue;
526 mbedtls_test_mock_socket *socket;
527 mbedtls_test_message_socket_context *context =
528 (mbedtls_test_message_socket_context *) ctx;
529 size_t drop_len = 0;
530 size_t msg_len;
531 int ret;
532
533 if (context == NULL || context->socket == NULL
534 || context->queue_input == NULL) {
535 return MBEDTLS_TEST_ERROR_CONTEXT_ERROR;
536 }
537
538 queue = context->queue_input;
539 socket = context->socket;
540
541 /* Peek first, so that in case of a socket error the data remains in
542 * the queue. */
Yanray Wang5e22a922023-03-16 14:57:54 +0800543 ret = test_ssl_message_queue_peek_info(queue, buf_len, &msg_len);
Yanray Wange6afd912022-10-27 12:11:18 +0800544 if (ret == MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED) {
545 /* Calculate how much to drop */
546 drop_len = msg_len - buf_len;
547
548 /* Set the requested message len to be buffer length */
549 msg_len = buf_len;
550 } else if (ret != 0) {
551 return ret;
552 }
553
554 if (mbedtls_test_mock_tcp_recv_b(socket, buf, msg_len) != (int) msg_len) {
555 return MBEDTLS_TEST_ERROR_RECV_FAILED;
556 }
557
558 if (ret == MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED) {
559 /* Drop the remaining part of the message */
Yanray Wangaf727a22023-03-13 19:22:36 +0800560 if (mbedtls_test_mock_tcp_recv_b(socket, NULL, drop_len) !=
561 (int) drop_len) {
Yanray Wange6afd912022-10-27 12:11:18 +0800562 /* Inconsistent state - part of the message was read,
563 * and a part couldn't. Not much we can do here, but it should not
564 * happen in test environment, unless forced manually. */
565 }
566 }
Tomás Gonzáleza9b90de2024-02-01 11:12:20 +0000567 ret = mbedtls_test_ssl_message_queue_pop_info(queue, buf_len);
568 if (ret < 0) {
569 return ret;
570 }
Yanray Wange6afd912022-10-27 12:11:18 +0800571
Yanray Wanga8f445e2022-11-03 11:51:59 +0800572 return (msg_len > INT_MAX) ? INT_MAX : (int) msg_len;
Yanray Wange6afd912022-10-27 12:11:18 +0800573}
574
575#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
576
577/*
578 * Deinitializes certificates from endpoint represented by \p ep.
579 */
Yanray Wangf6f71902023-03-15 16:05:14 +0800580static void test_ssl_endpoint_certificate_free(mbedtls_test_ssl_endpoint *ep)
Yanray Wange6afd912022-10-27 12:11:18 +0800581{
Gilles Peskineb6bb3fb2025-05-26 21:57:52 +0200582 if (ep->ca_chain != NULL) {
583 mbedtls_x509_crt_free(ep->ca_chain);
584 mbedtls_free(ep->ca_chain);
585 ep->ca_chain = NULL;
586 }
587 if (ep->cert != NULL) {
588 mbedtls_x509_crt_free(ep->cert);
589 mbedtls_free(ep->cert);
590 ep->cert = NULL;
591 }
592 if (ep->pkey != NULL) {
Yanray Wange6afd912022-10-27 12:11:18 +0800593#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskineb6bb3fb2025-05-26 21:57:52 +0200594 if (mbedtls_pk_get_type(ep->pkey) == MBEDTLS_PK_OPAQUE) {
595 psa_destroy_key(ep->pkey->priv_id);
Yanray Wange6afd912022-10-27 12:11:18 +0800596 }
Gilles Peskineb6bb3fb2025-05-26 21:57:52 +0200597#endif
598 mbedtls_pk_free(ep->pkey);
599 mbedtls_free(ep->pkey);
600 ep->pkey = NULL;
Yanray Wange6afd912022-10-27 12:11:18 +0800601 }
602}
603
Yanray Wange6afd912022-10-27 12:11:18 +0800604int mbedtls_test_ssl_endpoint_certificate_init(mbedtls_test_ssl_endpoint *ep,
605 int pk_alg,
606 int opaque_alg, int opaque_alg2,
607 int opaque_usage)
608{
609 int i = 0;
610 int ret = -1;
Gilles Peskine946bf142025-04-08 09:48:40 +0200611 int ok = 0;
Yanray Wange6afd912022-10-27 12:11:18 +0800612#if defined(MBEDTLS_USE_PSA_CRYPTO)
613 mbedtls_svc_key_id_t key_slot = MBEDTLS_SVC_KEY_ID_INIT;
614#endif
615
616 if (ep == NULL) {
617 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
618 }
619
Gilles Peskineb6bb3fb2025-05-26 21:57:52 +0200620 TEST_CALLOC(ep->ca_chain, 1);
621 TEST_CALLOC(ep->cert, 1);
622 TEST_CALLOC(ep->pkey, 1);
Yanray Wange6afd912022-10-27 12:11:18 +0800623
Gilles Peskineb6bb3fb2025-05-26 21:57:52 +0200624 mbedtls_x509_crt_init(ep->ca_chain);
625 mbedtls_x509_crt_init(ep->cert);
626 mbedtls_pk_init(ep->pkey);
Yanray Wange6afd912022-10-27 12:11:18 +0800627
628 /* Load the trusted CA */
629
630 for (i = 0; mbedtls_test_cas_der[i] != NULL; i++) {
631 ret = mbedtls_x509_crt_parse_der(
Gilles Peskineb6bb3fb2025-05-26 21:57:52 +0200632 ep->ca_chain,
Yanray Wange6afd912022-10-27 12:11:18 +0800633 (const unsigned char *) mbedtls_test_cas_der[i],
634 mbedtls_test_cas_der_len[i]);
Gilles Peskine353eb332025-05-14 17:42:53 +0200635 TEST_EQUAL(ret, 0);
Yanray Wange6afd912022-10-27 12:11:18 +0800636 }
637
638 /* Load own certificate and private key */
639
640 if (ep->conf.endpoint == MBEDTLS_SSL_IS_SERVER) {
641 if (pk_alg == MBEDTLS_PK_RSA) {
642 ret = mbedtls_x509_crt_parse(
Gilles Peskineb6bb3fb2025-05-26 21:57:52 +0200643 ep->cert,
Yanray Wange6afd912022-10-27 12:11:18 +0800644 (const unsigned char *) mbedtls_test_srv_crt_rsa_sha256_der,
645 mbedtls_test_srv_crt_rsa_sha256_der_len);
Gilles Peskine353eb332025-05-14 17:42:53 +0200646 TEST_EQUAL(ret, 0);
Yanray Wange6afd912022-10-27 12:11:18 +0800647
648 ret = mbedtls_pk_parse_key(
Gilles Peskineb6bb3fb2025-05-26 21:57:52 +0200649 ep->pkey,
Yanray Wange6afd912022-10-27 12:11:18 +0800650 (const unsigned char *) mbedtls_test_srv_key_rsa_der,
Ben Taylor440cb2a2025-03-05 09:40:08 +0000651 mbedtls_test_srv_key_rsa_der_len, NULL, 0);
Gilles Peskine353eb332025-05-14 17:42:53 +0200652 TEST_EQUAL(ret, 0);
Yanray Wange6afd912022-10-27 12:11:18 +0800653 } else {
654 ret = mbedtls_x509_crt_parse(
Gilles Peskineb6bb3fb2025-05-26 21:57:52 +0200655 ep->cert,
Yanray Wange6afd912022-10-27 12:11:18 +0800656 (const unsigned char *) mbedtls_test_srv_crt_ec_der,
657 mbedtls_test_srv_crt_ec_der_len);
Gilles Peskine353eb332025-05-14 17:42:53 +0200658 TEST_EQUAL(ret, 0);
Yanray Wange6afd912022-10-27 12:11:18 +0800659
660 ret = mbedtls_pk_parse_key(
Gilles Peskineb6bb3fb2025-05-26 21:57:52 +0200661 ep->pkey,
Yanray Wange6afd912022-10-27 12:11:18 +0800662 (const unsigned char *) mbedtls_test_srv_key_ec_der,
Ben Taylor440cb2a2025-03-05 09:40:08 +0000663 mbedtls_test_srv_key_ec_der_len, NULL, 0);
Gilles Peskine353eb332025-05-14 17:42:53 +0200664 TEST_EQUAL(ret, 0);
Yanray Wange6afd912022-10-27 12:11:18 +0800665 }
666 } else {
667 if (pk_alg == MBEDTLS_PK_RSA) {
668 ret = mbedtls_x509_crt_parse(
Gilles Peskineb6bb3fb2025-05-26 21:57:52 +0200669 ep->cert,
Yanray Wange6afd912022-10-27 12:11:18 +0800670 (const unsigned char *) mbedtls_test_cli_crt_rsa_der,
671 mbedtls_test_cli_crt_rsa_der_len);
Gilles Peskine353eb332025-05-14 17:42:53 +0200672 TEST_EQUAL(ret, 0);
Yanray Wange6afd912022-10-27 12:11:18 +0800673
674 ret = mbedtls_pk_parse_key(
Gilles Peskineb6bb3fb2025-05-26 21:57:52 +0200675 ep->pkey,
Yanray Wange6afd912022-10-27 12:11:18 +0800676 (const unsigned char *) mbedtls_test_cli_key_rsa_der,
Ben Taylor440cb2a2025-03-05 09:40:08 +0000677 mbedtls_test_cli_key_rsa_der_len, NULL, 0);
Gilles Peskine353eb332025-05-14 17:42:53 +0200678 TEST_EQUAL(ret, 0);
Yanray Wange6afd912022-10-27 12:11:18 +0800679 } else {
680 ret = mbedtls_x509_crt_parse(
Gilles Peskineb6bb3fb2025-05-26 21:57:52 +0200681 ep->cert,
Yanray Wange6afd912022-10-27 12:11:18 +0800682 (const unsigned char *) mbedtls_test_cli_crt_ec_der,
683 mbedtls_test_cli_crt_ec_len);
Gilles Peskine353eb332025-05-14 17:42:53 +0200684 TEST_EQUAL(ret, 0);
Yanray Wange6afd912022-10-27 12:11:18 +0800685
686 ret = mbedtls_pk_parse_key(
Gilles Peskineb6bb3fb2025-05-26 21:57:52 +0200687 ep->pkey,
Yanray Wange6afd912022-10-27 12:11:18 +0800688 (const unsigned char *) mbedtls_test_cli_key_ec_der,
Ben Taylor440cb2a2025-03-05 09:40:08 +0000689 mbedtls_test_cli_key_ec_der_len, NULL, 0);
Gilles Peskine353eb332025-05-14 17:42:53 +0200690 TEST_EQUAL(ret, 0);
Yanray Wange6afd912022-10-27 12:11:18 +0800691 }
692 }
693
694#if defined(MBEDTLS_USE_PSA_CRYPTO)
695 if (opaque_alg != 0) {
Valerio Setti1fa2f6e2024-02-27 08:11:25 +0100696 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
697 /* Use a fake key usage to get a successful initial guess for the PSA attributes. */
Gilles Peskineb6bb3fb2025-05-26 21:57:52 +0200698 TEST_EQUAL(mbedtls_pk_get_psa_attributes(ep->pkey, PSA_KEY_USAGE_SIGN_HASH,
Valerio Setti1fa2f6e2024-02-27 08:11:25 +0100699 &key_attr), 0);
Valerio Settia9de9442024-02-27 13:56:09 +0100700 /* Then manually usage, alg and alg2 as requested by the test. */
Valerio Setti1fa2f6e2024-02-27 08:11:25 +0100701 psa_set_key_usage_flags(&key_attr, opaque_usage);
702 psa_set_key_algorithm(&key_attr, opaque_alg);
703 if (opaque_alg2 != PSA_ALG_NONE) {
704 psa_set_key_enrollment_algorithm(&key_attr, opaque_alg2);
705 }
Gilles Peskineb6bb3fb2025-05-26 21:57:52 +0200706 TEST_EQUAL(mbedtls_pk_import_into_psa(ep->pkey, &key_attr, &key_slot), 0);
707 mbedtls_pk_free(ep->pkey);
708 mbedtls_pk_init(ep->pkey);
709 TEST_EQUAL(mbedtls_pk_setup_opaque(ep->pkey, key_slot), 0);
Yanray Wange6afd912022-10-27 12:11:18 +0800710 }
711#else
712 (void) opaque_alg;
713 (void) opaque_alg2;
714 (void) opaque_usage;
715#endif
716
Gilles Peskineb6bb3fb2025-05-26 21:57:52 +0200717 mbedtls_ssl_conf_ca_chain(&(ep->conf), ep->ca_chain, NULL);
Yanray Wange6afd912022-10-27 12:11:18 +0800718
Gilles Peskineb6bb3fb2025-05-26 21:57:52 +0200719 ret = mbedtls_ssl_conf_own_cert(&(ep->conf), ep->cert,
720 ep->pkey);
Gilles Peskine353eb332025-05-14 17:42:53 +0200721 TEST_EQUAL(ret, 0);
Yanray Wange6afd912022-10-27 12:11:18 +0800722 TEST_ASSERT(ep->conf.key_cert != NULL);
723
724 ret = mbedtls_ssl_conf_own_cert(&(ep->conf), NULL, NULL);
Gilles Peskine353eb332025-05-14 17:42:53 +0200725 TEST_EQUAL(ret, 0);
Yanray Wange6afd912022-10-27 12:11:18 +0800726 TEST_ASSERT(ep->conf.key_cert == NULL);
727
Gilles Peskineb6bb3fb2025-05-26 21:57:52 +0200728 ret = mbedtls_ssl_conf_own_cert(&(ep->conf), ep->cert,
729 ep->pkey);
Gilles Peskine353eb332025-05-14 17:42:53 +0200730 TEST_EQUAL(ret, 0);
Yanray Wange6afd912022-10-27 12:11:18 +0800731
Gilles Peskine946bf142025-04-08 09:48:40 +0200732 ok = 1;
733
Yanray Wange6afd912022-10-27 12:11:18 +0800734exit:
Gilles Peskine946bf142025-04-08 09:48:40 +0200735 if (ret == 0 && !ok) {
736 /* Exiting due to a test assertion that isn't ret == 0 */
737 ret = -1;
738 }
Yanray Wange6afd912022-10-27 12:11:18 +0800739 if (ret != 0) {
Yanray Wangf6f71902023-03-15 16:05:14 +0800740 test_ssl_endpoint_certificate_free(ep);
Yanray Wange6afd912022-10-27 12:11:18 +0800741 }
742
743 return ret;
744}
745
Yanray Wange6afd912022-10-27 12:11:18 +0800746int mbedtls_test_ssl_endpoint_init(
747 mbedtls_test_ssl_endpoint *ep, int endpoint_type,
748 mbedtls_test_handshake_test_options *options,
749 mbedtls_test_message_socket_context *dtls_context,
750 mbedtls_test_ssl_message_queue *input_queue,
Ronald Cronfb536472024-01-26 14:55:25 +0100751 mbedtls_test_ssl_message_queue *output_queue)
Yanray Wange6afd912022-10-27 12:11:18 +0800752{
753 int ret = -1;
754 uintptr_t user_data_n;
755
756 if (dtls_context != NULL &&
757 (input_queue == NULL || output_queue == NULL)) {
758 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
759
760 }
761
762 if (ep == NULL) {
763 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
764 }
765
766 memset(ep, 0, sizeof(*ep));
767
768 ep->name = (endpoint_type == MBEDTLS_SSL_IS_SERVER) ? "Server" : "Client";
769
770 mbedtls_ssl_init(&(ep->ssl));
771 mbedtls_ssl_config_init(&(ep->conf));
Yanray Wange6afd912022-10-27 12:11:18 +0800772
773 TEST_ASSERT(mbedtls_ssl_conf_get_user_data_p(&ep->conf) == NULL);
774 TEST_EQUAL(mbedtls_ssl_conf_get_user_data_n(&ep->conf), 0);
775 TEST_ASSERT(mbedtls_ssl_get_user_data_p(&ep->ssl) == NULL);
776 TEST_EQUAL(mbedtls_ssl_get_user_data_n(&ep->ssl), 0);
777
778 (void) mbedtls_test_rnd_std_rand(NULL,
779 (void *) &user_data_n,
780 sizeof(user_data_n));
781 mbedtls_ssl_conf_set_user_data_n(&ep->conf, user_data_n);
782 mbedtls_ssl_set_user_data_n(&ep->ssl, user_data_n);
783
784 if (dtls_context != NULL) {
Gilles Peskine353eb332025-05-14 17:42:53 +0200785 TEST_EQUAL(mbedtls_test_message_socket_setup(input_queue, output_queue,
786 100, &(ep->socket),
787 dtls_context), 0);
Yanray Wange6afd912022-10-27 12:11:18 +0800788 } else {
Yanray Wang5f86a422023-03-15 16:02:29 +0800789 mbedtls_test_mock_socket_init(&(ep->socket));
Yanray Wange6afd912022-10-27 12:11:18 +0800790 }
791
792 /* Non-blocking callbacks without timeout */
793 if (dtls_context != NULL) {
794 mbedtls_ssl_set_bio(&(ep->ssl), dtls_context,
795 mbedtls_test_mock_tcp_send_msg,
796 mbedtls_test_mock_tcp_recv_msg,
797 NULL);
798 } else {
799 mbedtls_ssl_set_bio(&(ep->ssl), &(ep->socket),
800 mbedtls_test_mock_tcp_send_nb,
801 mbedtls_test_mock_tcp_recv_nb,
802 NULL);
803 }
804
805 ret = mbedtls_ssl_config_defaults(&(ep->conf), endpoint_type,
806 (dtls_context != NULL) ?
807 MBEDTLS_SSL_TRANSPORT_DATAGRAM :
808 MBEDTLS_SSL_TRANSPORT_STREAM,
809 MBEDTLS_SSL_PRESET_DEFAULT);
Gilles Peskine353eb332025-05-14 17:42:53 +0200810 TEST_EQUAL(ret, 0);
Yanray Wange6afd912022-10-27 12:11:18 +0800811
Ronald Crona697a712023-03-09 17:47:42 +0100812 if (MBEDTLS_SSL_IS_CLIENT == endpoint_type) {
813 if (options->client_min_version != MBEDTLS_SSL_VERSION_UNKNOWN) {
814 mbedtls_ssl_conf_min_tls_version(&(ep->conf),
815 options->client_min_version);
816 }
817
818 if (options->client_max_version != MBEDTLS_SSL_VERSION_UNKNOWN) {
819 mbedtls_ssl_conf_max_tls_version(&(ep->conf),
820 options->client_max_version);
821 }
822 } else {
823 if (options->server_min_version != MBEDTLS_SSL_VERSION_UNKNOWN) {
824 mbedtls_ssl_conf_min_tls_version(&(ep->conf),
825 options->server_min_version);
826 }
827
828 if (options->server_max_version != MBEDTLS_SSL_VERSION_UNKNOWN) {
829 mbedtls_ssl_conf_max_tls_version(&(ep->conf),
830 options->server_max_version);
831 }
832 }
833
Ronald Cronfb536472024-01-26 14:55:25 +0100834 if (options->group_list != NULL) {
835 mbedtls_ssl_conf_groups(&(ep->conf), options->group_list);
Yanray Wange6afd912022-10-27 12:11:18 +0800836 }
837
838 mbedtls_ssl_conf_authmode(&(ep->conf), MBEDTLS_SSL_VERIFY_REQUIRED);
839
Ronald Cronced99be2024-01-26 15:49:12 +0100840#if defined(MBEDTLS_SSL_EARLY_DATA)
841 mbedtls_ssl_conf_early_data(&(ep->conf), options->early_data);
Ronald Cron5d3036e2024-02-23 07:43:45 +0100842#if defined(MBEDTLS_SSL_SRV_C)
843 if (endpoint_type == MBEDTLS_SSL_IS_SERVER &&
844 (options->max_early_data_size >= 0)) {
845 mbedtls_ssl_conf_max_early_data_size(&(ep->conf),
846 options->max_early_data_size);
847 }
848#endif
Waleed Elmelegy4dfb0e72024-03-14 01:48:40 +0000849#if defined(MBEDTLS_SSL_ALPN)
850 /* check that alpn_list contains at least one valid entry */
851 if (options->alpn_list[0] != NULL) {
852 mbedtls_ssl_conf_alpn_protocols(&(ep->conf), options->alpn_list);
853 }
854#endif
Ronald Cronced99be2024-01-26 15:49:12 +0100855#endif
856
Yanray Wange6afd912022-10-27 12:11:18 +0800857#if defined(MBEDTLS_SSL_CACHE_C) && defined(MBEDTLS_SSL_SRV_C)
858 if (endpoint_type == MBEDTLS_SSL_IS_SERVER && options->cache != NULL) {
859 mbedtls_ssl_conf_session_cache(&(ep->conf), options->cache,
860 mbedtls_ssl_cache_get,
861 mbedtls_ssl_cache_set);
862 }
863#endif
864
865 ret = mbedtls_ssl_setup(&(ep->ssl), &(ep->conf));
Gilles Peskine353eb332025-05-14 17:42:53 +0200866 TEST_EQUAL(ret, 0);
Yanray Wange6afd912022-10-27 12:11:18 +0800867
Gilles Peskine856a3702025-02-13 17:28:49 +0100868 if (MBEDTLS_SSL_IS_CLIENT == endpoint_type) {
869 ret = mbedtls_ssl_set_hostname(&(ep->ssl), "localhost");
Gilles Peskine55b8bb42025-04-08 09:44:34 +0200870 TEST_EQUAL(ret, 0);
Gilles Peskine856a3702025-02-13 17:28:49 +0100871 }
872
Yanray Wange6afd912022-10-27 12:11:18 +0800873#if defined(MBEDTLS_SSL_PROTO_DTLS) && defined(MBEDTLS_SSL_SRV_C)
874 if (endpoint_type == MBEDTLS_SSL_IS_SERVER && dtls_context != NULL) {
875 mbedtls_ssl_conf_dtls_cookies(&(ep->conf), NULL, NULL, NULL);
876 }
877#endif
878
Ronald Cronec3408d2024-01-16 17:50:40 +0100879#if defined(MBEDTLS_DEBUG_C)
880#if defined(MBEDTLS_SSL_SRV_C)
881 if (endpoint_type == MBEDTLS_SSL_IS_SERVER &&
882 options->srv_log_fun != NULL) {
883 mbedtls_ssl_conf_dbg(&(ep->conf), options->srv_log_fun,
884 options->srv_log_obj);
885 }
886#endif
887#if defined(MBEDTLS_SSL_CLI_C)
888 if (endpoint_type == MBEDTLS_SSL_IS_CLIENT &&
889 options->cli_log_fun != NULL) {
890 mbedtls_ssl_conf_dbg(&(ep->conf), options->cli_log_fun,
891 options->cli_log_obj);
892 }
893#endif
894#endif /* MBEDTLS_DEBUG_C */
895
Yanray Wange6afd912022-10-27 12:11:18 +0800896 ret = mbedtls_test_ssl_endpoint_certificate_init(ep, options->pk_alg,
897 options->opaque_alg,
898 options->opaque_alg2,
899 options->opaque_usage);
Gilles Peskine353eb332025-05-14 17:42:53 +0200900 TEST_EQUAL(ret, 0);
Yanray Wange6afd912022-10-27 12:11:18 +0800901
902 TEST_EQUAL(mbedtls_ssl_conf_get_user_data_n(&ep->conf), user_data_n);
903 mbedtls_ssl_conf_set_user_data_p(&ep->conf, ep);
904 TEST_EQUAL(mbedtls_ssl_get_user_data_n(&ep->ssl), user_data_n);
905 mbedtls_ssl_set_user_data_p(&ep->ssl, ep);
906
Gilles Peskine946bf142025-04-08 09:48:40 +0200907 return 0;
908
Yanray Wange6afd912022-10-27 12:11:18 +0800909exit:
Gilles Peskine946bf142025-04-08 09:48:40 +0200910 if (ret == 0) {
911 /* Exiting due to a test assertion that isn't ret == 0 */
912 ret = -1;
913 }
Yanray Wange6afd912022-10-27 12:11:18 +0800914 return ret;
915}
916
Yanray Wange6afd912022-10-27 12:11:18 +0800917void mbedtls_test_ssl_endpoint_free(
918 mbedtls_test_ssl_endpoint *ep,
919 mbedtls_test_message_socket_context *context)
920{
Yanray Wangf6f71902023-03-15 16:05:14 +0800921 test_ssl_endpoint_certificate_free(ep);
Yanray Wange6afd912022-10-27 12:11:18 +0800922
923 mbedtls_ssl_free(&(ep->ssl));
924 mbedtls_ssl_config_free(&(ep->conf));
925
926 if (context != NULL) {
927 mbedtls_test_message_socket_close(context);
928 } else {
929 mbedtls_test_mock_socket_close(&(ep->socket));
930 }
931}
932
Yanray Wange6afd912022-10-27 12:11:18 +0800933int mbedtls_test_move_handshake_to_state(mbedtls_ssl_context *ssl,
934 mbedtls_ssl_context *second_ssl,
935 int state)
936{
937 enum { BUFFSIZE = 1024 };
938 int max_steps = 1000;
939 int ret = 0;
940
941 if (ssl == NULL || second_ssl == NULL) {
942 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
943 }
944
945 /* Perform communication via connected sockets */
946 while ((ssl->state != state) && (--max_steps >= 0)) {
947 /* If /p second_ssl ends the handshake procedure before /p ssl then
948 * there is no need to call the next step */
949 if (!mbedtls_ssl_is_handshake_over(second_ssl)) {
950 ret = mbedtls_ssl_handshake_step(second_ssl);
951 if (ret != 0 && ret != MBEDTLS_ERR_SSL_WANT_READ &&
952 ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
953 return ret;
954 }
955 }
956
957 /* We only care about the \p ssl state and returns, so we call it last,
958 * to leave the iteration as soon as the state is as expected. */
959 ret = mbedtls_ssl_handshake_step(ssl);
960 if (ret != 0 && ret != MBEDTLS_ERR_SSL_WANT_READ &&
961 ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
962 return ret;
963 }
964 }
965
966 return (max_steps >= 0) ? ret : -1;
967}
968
969#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
970
971/*
972 * Write application data. Increase write counter if necessary.
973 */
Michael Schuster54300d42024-06-04 02:30:22 +0200974static int mbedtls_ssl_write_fragment(mbedtls_ssl_context *ssl,
Michael Schusterbd89b792024-06-04 02:41:10 +0200975 unsigned char *buf, int buf_len,
976 int *written,
977 const int expected_fragments)
Yanray Wange6afd912022-10-27 12:11:18 +0800978{
Agathiyan Bragadeesh93212652023-07-12 11:22:59 +0100979 int ret;
Yanray Wange6afd912022-10-27 12:11:18 +0800980 /* Verify that calling mbedtls_ssl_write with a NULL buffer and zero length is
981 * a valid no-op for TLS connections. */
982 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Gilles Peskine353eb332025-05-14 17:42:53 +0200983 TEST_EQUAL(mbedtls_ssl_write(ssl, NULL, 0), 0);
Yanray Wange6afd912022-10-27 12:11:18 +0800984 }
985
Agathiyan Bragadeesh93212652023-07-12 11:22:59 +0100986 ret = mbedtls_ssl_write(ssl, buf + *written, buf_len - *written);
Yanray Wange6afd912022-10-27 12:11:18 +0800987 if (ret > 0) {
988 *written += ret;
989 }
990
991 if (expected_fragments == 0) {
992 /* Used for DTLS and the message size larger than MFL. In that case
993 * the message can not be fragmented and the library should return
994 * MBEDTLS_ERR_SSL_BAD_INPUT_DATA error. This error must be returned
Yanray Wangb088bfc2023-03-16 12:15:49 +0800995 * to prevent a dead loop inside mbedtls_test_ssl_exchange_data(). */
Yanray Wange6afd912022-10-27 12:11:18 +0800996 return ret;
997 } else if (expected_fragments == 1) {
998 /* Used for TLS/DTLS and the message size lower than MFL */
999 TEST_ASSERT(ret == buf_len ||
1000 ret == MBEDTLS_ERR_SSL_WANT_READ ||
1001 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
1002 } else {
1003 /* Used for TLS and the message size larger than MFL */
1004 TEST_ASSERT(expected_fragments > 1);
1005 TEST_ASSERT((ret >= 0 && ret <= buf_len) ||
1006 ret == MBEDTLS_ERR_SSL_WANT_READ ||
1007 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
1008 }
1009
1010 return 0;
1011
1012exit:
1013 /* Some of the tests failed */
1014 return -1;
1015}
1016
1017/*
1018 * Read application data and increase read counter and fragments counter
1019 * if necessary.
1020 */
Michael Schuster54300d42024-06-04 02:30:22 +02001021static int mbedtls_ssl_read_fragment(mbedtls_ssl_context *ssl,
Michael Schusterbd89b792024-06-04 02:41:10 +02001022 unsigned char *buf, int buf_len,
1023 int *read, int *fragments,
1024 const int expected_fragments)
Yanray Wange6afd912022-10-27 12:11:18 +08001025{
Agathiyan Bragadeesh93212652023-07-12 11:22:59 +01001026 int ret;
Yanray Wange6afd912022-10-27 12:11:18 +08001027 /* Verify that calling mbedtls_ssl_write with a NULL buffer and zero length is
1028 * a valid no-op for TLS connections. */
1029 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Gilles Peskine353eb332025-05-14 17:42:53 +02001030 TEST_EQUAL(mbedtls_ssl_read(ssl, NULL, 0), 0);
Yanray Wange6afd912022-10-27 12:11:18 +08001031 }
1032
Agathiyan Bragadeesh93212652023-07-12 11:22:59 +01001033 ret = mbedtls_ssl_read(ssl, buf + *read, buf_len - *read);
Yanray Wange6afd912022-10-27 12:11:18 +08001034 if (ret > 0) {
1035 (*fragments)++;
1036 *read += ret;
1037 }
1038
1039 if (expected_fragments == 0) {
Gilles Peskine353eb332025-05-14 17:42:53 +02001040 TEST_EQUAL(ret, 0);
Yanray Wange6afd912022-10-27 12:11:18 +08001041 } else if (expected_fragments == 1) {
1042 TEST_ASSERT(ret == buf_len ||
1043 ret == MBEDTLS_ERR_SSL_WANT_READ ||
1044 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
1045 } else {
1046 TEST_ASSERT(expected_fragments > 1);
1047 TEST_ASSERT((ret >= 0 && ret <= buf_len) ||
1048 ret == MBEDTLS_ERR_SSL_WANT_READ ||
1049 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
1050 }
1051
1052 return 0;
1053
1054exit:
1055 /* Some of the tests failed */
1056 return -1;
1057}
1058
Yanray Wangead70c82023-03-16 12:04:49 +08001059#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
1060static void set_ciphersuite(mbedtls_ssl_config *conf, const char *cipher,
1061 int *forced_ciphersuite)
Yanray Wange6afd912022-10-27 12:11:18 +08001062{
1063 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
1064 forced_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id(cipher);
1065 forced_ciphersuite[1] = 0;
1066
1067 ciphersuite_info =
1068 mbedtls_ssl_ciphersuite_from_id(forced_ciphersuite[0]);
1069
1070 TEST_ASSERT(ciphersuite_info != NULL);
1071 TEST_ASSERT(ciphersuite_info->min_tls_version <= conf->max_tls_version);
1072 TEST_ASSERT(ciphersuite_info->max_tls_version >= conf->min_tls_version);
1073
1074 if (conf->max_tls_version > ciphersuite_info->max_tls_version) {
Agathiyan Bragadeesh2f017a82023-07-12 11:21:54 +01001075 conf->max_tls_version = (mbedtls_ssl_protocol_version) ciphersuite_info->max_tls_version;
Yanray Wange6afd912022-10-27 12:11:18 +08001076 }
1077 if (conf->min_tls_version < ciphersuite_info->min_tls_version) {
Agathiyan Bragadeesh2f017a82023-07-12 11:21:54 +01001078 conf->min_tls_version = (mbedtls_ssl_protocol_version) ciphersuite_info->min_tls_version;
Yanray Wange6afd912022-10-27 12:11:18 +08001079 }
1080
1081 mbedtls_ssl_conf_ciphersuites(conf, forced_ciphersuite);
1082
1083exit:
1084 return;
1085}
Yanray Wangead70c82023-03-16 12:04:49 +08001086#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Yanray Wange6afd912022-10-27 12:11:18 +08001087
Yanray Wangead70c82023-03-16 12:04:49 +08001088#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) && \
1089 defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED) && \
1090 defined(MBEDTLS_SSL_SRV_C)
1091static int psk_dummy_callback(void *p_info, mbedtls_ssl_context *ssl,
1092 const unsigned char *name, size_t name_len)
Yanray Wange6afd912022-10-27 12:11:18 +08001093{
1094 (void) p_info;
1095 (void) ssl;
1096 (void) name;
1097 (void) name_len;
1098
1099 return 0;
1100}
Yanray Wangead70c82023-03-16 12:04:49 +08001101#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED &&
1102 MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED &&
1103 MBEDTLS_SSL_SRV_C */
Yanray Wange6afd912022-10-27 12:11:18 +08001104
1105#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Elena Uziunaite74342c72024-07-05 11:31:29 +01001106 defined(PSA_WANT_ALG_CBC_NO_PADDING) && defined(PSA_WANT_KEY_TYPE_AES)
Yanray Wange6afd912022-10-27 12:11:18 +08001107int mbedtls_test_psa_cipher_encrypt_helper(mbedtls_ssl_transform *transform,
1108 const unsigned char *iv,
1109 size_t iv_len,
1110 const unsigned char *input,
1111 size_t ilen,
1112 unsigned char *output,
1113 size_t *olen)
1114{
1115#if defined(MBEDTLS_USE_PSA_CRYPTO)
1116 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1117 psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT;
1118 size_t part_len;
1119
1120 status = psa_cipher_encrypt_setup(&cipher_op,
1121 transform->psa_key_enc,
1122 transform->psa_alg);
1123
1124 if (status != PSA_SUCCESS) {
1125 return PSA_TO_MBEDTLS_ERR(status);
1126 }
1127
1128 status = psa_cipher_set_iv(&cipher_op, iv, iv_len);
1129
1130 if (status != PSA_SUCCESS) {
1131 return PSA_TO_MBEDTLS_ERR(status);
1132 }
1133
1134 status = psa_cipher_update(&cipher_op, input, ilen, output, ilen, olen);
1135
1136 if (status != PSA_SUCCESS) {
1137 return PSA_TO_MBEDTLS_ERR(status);
1138 }
1139
1140 status = psa_cipher_finish(&cipher_op, output + *olen, ilen - *olen,
1141 &part_len);
1142
1143 if (status != PSA_SUCCESS) {
1144 return PSA_TO_MBEDTLS_ERR(status);
1145 }
1146
1147 *olen += part_len;
1148 return 0;
1149#else
1150 return mbedtls_cipher_crypt(&transform->cipher_ctx_enc,
1151 iv, iv_len, input, ilen, output, olen);
1152#endif /* MBEDTLS_USE_PSA_CRYPTO */
1153}
Elena Uziunaite74342c72024-07-05 11:31:29 +01001154#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && PSA_WANT_ALG_CBC_NO_PADDING &&
Elena Uziunaite6121a342024-07-05 11:16:53 +01001155 PSA_WANT_KEY_TYPE_AES */
Yanray Wange6afd912022-10-27 12:11:18 +08001156
Valerio Setti31ad3a12023-10-27 11:55:02 +02001157static void mbedtls_test_ssl_cipher_info_from_type(mbedtls_cipher_type_t cipher_type,
1158 mbedtls_cipher_mode_t *cipher_mode,
1159 size_t *key_bits, size_t *iv_len)
1160{
1161 switch (cipher_type) {
1162 case MBEDTLS_CIPHER_AES_128_CBC:
1163 *cipher_mode = MBEDTLS_MODE_CBC;
1164 *key_bits = 128;
1165 *iv_len = 16;
1166 break;
1167 case MBEDTLS_CIPHER_AES_256_CBC:
1168 *cipher_mode = MBEDTLS_MODE_CBC;
1169 *key_bits = 256;
1170 *iv_len = 16;
1171 break;
1172 case MBEDTLS_CIPHER_ARIA_128_CBC:
1173 *cipher_mode = MBEDTLS_MODE_CBC;
1174 *key_bits = 128;
1175 *iv_len = 16;
1176 break;
1177 case MBEDTLS_CIPHER_ARIA_256_CBC:
1178 *cipher_mode = MBEDTLS_MODE_CBC;
1179 *key_bits = 256;
1180 *iv_len = 16;
1181 break;
1182 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
1183 *cipher_mode = MBEDTLS_MODE_CBC;
1184 *key_bits = 128;
1185 *iv_len = 16;
1186 break;
1187 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
1188 *cipher_mode = MBEDTLS_MODE_CBC;
1189 *key_bits = 256;
1190 *iv_len = 16;
1191 break;
1192
1193 case MBEDTLS_CIPHER_AES_128_CCM:
1194 *cipher_mode = MBEDTLS_MODE_CCM;
1195 *key_bits = 128;
1196 *iv_len = 12;
1197 break;
1198 case MBEDTLS_CIPHER_AES_192_CCM:
1199 *cipher_mode = MBEDTLS_MODE_CCM;
1200 *key_bits = 192;
1201 *iv_len = 12;
1202 break;
1203 case MBEDTLS_CIPHER_AES_256_CCM:
1204 *cipher_mode = MBEDTLS_MODE_CCM;
1205 *key_bits = 256;
1206 *iv_len = 12;
1207 break;
1208 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
1209 *cipher_mode = MBEDTLS_MODE_CCM;
1210 *key_bits = 128;
1211 *iv_len = 12;
1212 break;
1213 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
1214 *cipher_mode = MBEDTLS_MODE_CCM;
1215 *key_bits = 192;
1216 *iv_len = 12;
1217 break;
1218 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
1219 *cipher_mode = MBEDTLS_MODE_CCM;
1220 *key_bits = 256;
1221 *iv_len = 12;
1222 break;
1223
1224 case MBEDTLS_CIPHER_AES_128_GCM:
1225 *cipher_mode = MBEDTLS_MODE_GCM;
1226 *key_bits = 128;
1227 *iv_len = 12;
1228 break;
1229 case MBEDTLS_CIPHER_AES_192_GCM:
1230 *cipher_mode = MBEDTLS_MODE_GCM;
1231 *key_bits = 192;
1232 *iv_len = 12;
1233 break;
1234 case MBEDTLS_CIPHER_AES_256_GCM:
1235 *cipher_mode = MBEDTLS_MODE_GCM;
1236 *key_bits = 256;
1237 *iv_len = 12;
1238 break;
1239 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
1240 *cipher_mode = MBEDTLS_MODE_GCM;
1241 *key_bits = 128;
1242 *iv_len = 12;
1243 break;
1244 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
1245 *cipher_mode = MBEDTLS_MODE_GCM;
1246 *key_bits = 192;
1247 *iv_len = 12;
1248 break;
1249 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
1250 *cipher_mode = MBEDTLS_MODE_GCM;
1251 *key_bits = 256;
1252 *iv_len = 12;
1253 break;
1254
1255 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
1256 *cipher_mode = MBEDTLS_MODE_CHACHAPOLY;
1257 *key_bits = 256;
1258 *iv_len = 12;
1259 break;
1260
1261 case MBEDTLS_CIPHER_NULL:
1262 *cipher_mode = MBEDTLS_MODE_STREAM;
1263 *key_bits = 0;
1264 *iv_len = 0;
1265 break;
1266
1267 default:
1268 *cipher_mode = MBEDTLS_MODE_NONE;
1269 *key_bits = 0;
1270 *iv_len = 0;
1271 }
1272}
1273
Yanray Wange6afd912022-10-27 12:11:18 +08001274int mbedtls_test_ssl_build_transforms(mbedtls_ssl_transform *t_in,
1275 mbedtls_ssl_transform *t_out,
1276 int cipher_type, int hash_id,
1277 int etm, int tag_mode,
1278 mbedtls_ssl_protocol_version tls_version,
1279 size_t cid0_len,
1280 size_t cid1_len)
1281{
Valerio Setti31ad3a12023-10-27 11:55:02 +02001282 mbedtls_cipher_mode_t cipher_mode = MBEDTLS_MODE_NONE;
1283 size_t key_bits = 0;
Yanray Wange6afd912022-10-27 12:11:18 +08001284 int ret = 0;
1285
1286#if defined(MBEDTLS_USE_PSA_CRYPTO)
1287 psa_key_type_t key_type;
1288 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1289 psa_algorithm_t alg;
Yanray Wange6afd912022-10-27 12:11:18 +08001290 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Valerio Setti3d59ebe2023-10-30 11:56:56 +01001291#else
Valerio Setti31ad3a12023-10-27 11:55:02 +02001292 mbedtls_cipher_info_t const *cipher_info;
Yanray Wange6afd912022-10-27 12:11:18 +08001293#endif
1294
Valerio Setti31ad3a12023-10-27 11:55:02 +02001295 size_t keylen, maclen, ivlen = 0;
Yanray Wange6afd912022-10-27 12:11:18 +08001296 unsigned char *key0 = NULL, *key1 = NULL;
1297 unsigned char *md0 = NULL, *md1 = NULL;
1298 unsigned char iv_enc[16], iv_dec[16];
1299
1300#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
1301 unsigned char cid0[SSL_CID_LEN_MIN];
1302 unsigned char cid1[SSL_CID_LEN_MIN];
1303
1304 mbedtls_test_rnd_std_rand(NULL, cid0, sizeof(cid0));
1305 mbedtls_test_rnd_std_rand(NULL, cid1, sizeof(cid1));
1306#else
1307 ((void) cid0_len);
1308 ((void) cid1_len);
1309#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
1310
1311 maclen = 0;
Valerio Setti31ad3a12023-10-27 11:55:02 +02001312 mbedtls_test_ssl_cipher_info_from_type((mbedtls_cipher_type_t) cipher_type,
1313 &cipher_mode, &key_bits, &ivlen);
Yanray Wange6afd912022-10-27 12:11:18 +08001314
1315 /* Pick keys */
Valerio Setti31ad3a12023-10-27 11:55:02 +02001316 keylen = key_bits / 8;
Yanray Wange6afd912022-10-27 12:11:18 +08001317 /* Allocate `keylen + 1` bytes to ensure that we get
1318 * a non-NULL pointers from `mbedtls_calloc` even if
1319 * `keylen == 0` in the case of the NULL cipher. */
1320 CHK((key0 = mbedtls_calloc(1, keylen + 1)) != NULL);
1321 CHK((key1 = mbedtls_calloc(1, keylen + 1)) != NULL);
1322 memset(key0, 0x1, keylen);
1323 memset(key1, 0x2, keylen);
1324
1325#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti31ad3a12023-10-27 11:55:02 +02001326 /* Pick cipher */
1327 cipher_info = mbedtls_cipher_info_from_type((mbedtls_cipher_type_t) cipher_type);
1328 CHK(cipher_info != NULL);
1329 CHK(mbedtls_cipher_info_get_iv_size(cipher_info) <= 16);
1330 CHK(mbedtls_cipher_info_get_key_bitlen(cipher_info) % 8 == 0);
Valerio Setti3d59ebe2023-10-30 11:56:56 +01001331
Yanray Wange6afd912022-10-27 12:11:18 +08001332 /* Setup cipher contexts */
1333 CHK(mbedtls_cipher_setup(&t_in->cipher_ctx_enc, cipher_info) == 0);
1334 CHK(mbedtls_cipher_setup(&t_in->cipher_ctx_dec, cipher_info) == 0);
1335 CHK(mbedtls_cipher_setup(&t_out->cipher_ctx_enc, cipher_info) == 0);
1336 CHK(mbedtls_cipher_setup(&t_out->cipher_ctx_dec, cipher_info) == 0);
1337
1338#if defined(MBEDTLS_CIPHER_MODE_CBC)
Valerio Setti31ad3a12023-10-27 11:55:02 +02001339 if (cipher_mode == MBEDTLS_MODE_CBC) {
Yanray Wange6afd912022-10-27 12:11:18 +08001340 CHK(mbedtls_cipher_set_padding_mode(&t_in->cipher_ctx_enc,
1341 MBEDTLS_PADDING_NONE) == 0);
1342 CHK(mbedtls_cipher_set_padding_mode(&t_in->cipher_ctx_dec,
1343 MBEDTLS_PADDING_NONE) == 0);
1344 CHK(mbedtls_cipher_set_padding_mode(&t_out->cipher_ctx_enc,
1345 MBEDTLS_PADDING_NONE) == 0);
1346 CHK(mbedtls_cipher_set_padding_mode(&t_out->cipher_ctx_dec,
1347 MBEDTLS_PADDING_NONE) == 0);
1348 }
1349#endif /* MBEDTLS_CIPHER_MODE_CBC */
1350
1351 CHK(mbedtls_cipher_setkey(&t_in->cipher_ctx_enc, key0,
Yanray Wanga8f445e2022-11-03 11:51:59 +08001352 (keylen << 3 > INT_MAX) ? INT_MAX : (int) keylen << 3,
1353 MBEDTLS_ENCRYPT)
1354 == 0);
Yanray Wange6afd912022-10-27 12:11:18 +08001355 CHK(mbedtls_cipher_setkey(&t_in->cipher_ctx_dec, key1,
Yanray Wanga8f445e2022-11-03 11:51:59 +08001356 (keylen << 3 > INT_MAX) ? INT_MAX : (int) keylen << 3,
1357 MBEDTLS_DECRYPT)
1358 == 0);
Yanray Wange6afd912022-10-27 12:11:18 +08001359 CHK(mbedtls_cipher_setkey(&t_out->cipher_ctx_enc, key1,
Yanray Wanga8f445e2022-11-03 11:51:59 +08001360 (keylen << 3 > INT_MAX) ? INT_MAX : (int) keylen << 3,
1361 MBEDTLS_ENCRYPT)
1362 == 0);
Yanray Wange6afd912022-10-27 12:11:18 +08001363 CHK(mbedtls_cipher_setkey(&t_out->cipher_ctx_dec, key0,
Yanray Wanga8f445e2022-11-03 11:51:59 +08001364 (keylen << 3 > INT_MAX) ? INT_MAX : (int) keylen << 3,
1365 MBEDTLS_DECRYPT)
1366 == 0);
Valerio Setti31ad3a12023-10-27 11:55:02 +02001367#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Yanray Wange6afd912022-10-27 12:11:18 +08001368
1369 /* Setup MAC contexts */
1370#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Valerio Setti31ad3a12023-10-27 11:55:02 +02001371 if (cipher_mode == MBEDTLS_MODE_CBC ||
1372 cipher_mode == MBEDTLS_MODE_STREAM) {
Yanray Wange6afd912022-10-27 12:11:18 +08001373#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Agathiyan Bragadeesh2f017a82023-07-12 11:21:54 +01001374 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 +08001375 CHK(md_info != NULL);
1376#endif
Agathiyan Bragadeesh2f017a82023-07-12 11:21:54 +01001377 maclen = mbedtls_md_get_size_from_type((mbedtls_md_type_t) hash_id);
Yanray Wange6afd912022-10-27 12:11:18 +08001378 CHK(maclen != 0);
1379 /* Pick hash keys */
1380 CHK((md0 = mbedtls_calloc(1, maclen)) != NULL);
1381 CHK((md1 = mbedtls_calloc(1, maclen)) != NULL);
1382 memset(md0, 0x5, maclen);
1383 memset(md1, 0x6, maclen);
1384
1385#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02001386 alg = mbedtls_md_psa_alg_from_type(hash_id);
Yanray Wange6afd912022-10-27 12:11:18 +08001387
1388 CHK(alg != 0);
1389
1390 t_out->psa_mac_alg = PSA_ALG_HMAC(alg);
1391 t_in->psa_mac_alg = PSA_ALG_HMAC(alg);
1392 t_in->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
1393 t_out->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
1394 t_in->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
1395 t_out->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
1396
1397 psa_reset_key_attributes(&attributes);
1398 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
1399 psa_set_key_algorithm(&attributes, PSA_ALG_HMAC(alg));
1400 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
1401
1402 CHK(psa_import_key(&attributes,
1403 md0, maclen,
1404 &t_in->psa_mac_enc) == PSA_SUCCESS);
1405
1406 CHK(psa_import_key(&attributes,
1407 md1, maclen,
1408 &t_out->psa_mac_enc) == PSA_SUCCESS);
1409
Valerio Setti31ad3a12023-10-27 11:55:02 +02001410 if (cipher_mode == MBEDTLS_MODE_STREAM ||
Yanray Wange6afd912022-10-27 12:11:18 +08001411 etm == MBEDTLS_SSL_ETM_DISABLED) {
1412 /* mbedtls_ct_hmac() requires the key to be exportable */
1413 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT |
1414 PSA_KEY_USAGE_VERIFY_HASH);
1415 } else {
1416 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
1417 }
1418
1419 CHK(psa_import_key(&attributes,
1420 md1, maclen,
1421 &t_in->psa_mac_dec) == PSA_SUCCESS);
1422
1423 CHK(psa_import_key(&attributes,
1424 md0, maclen,
1425 &t_out->psa_mac_dec) == PSA_SUCCESS);
1426#else
1427 CHK(mbedtls_md_setup(&t_out->md_ctx_enc, md_info, 1) == 0);
1428 CHK(mbedtls_md_setup(&t_out->md_ctx_dec, md_info, 1) == 0);
1429 CHK(mbedtls_md_setup(&t_in->md_ctx_enc, md_info, 1) == 0);
1430 CHK(mbedtls_md_setup(&t_in->md_ctx_dec, md_info, 1) == 0);
1431
1432 CHK(mbedtls_md_hmac_starts(&t_in->md_ctx_enc,
1433 md0, maclen) == 0);
1434 CHK(mbedtls_md_hmac_starts(&t_in->md_ctx_dec,
1435 md1, maclen) == 0);
1436 CHK(mbedtls_md_hmac_starts(&t_out->md_ctx_enc,
1437 md1, maclen) == 0);
1438 CHK(mbedtls_md_hmac_starts(&t_out->md_ctx_dec,
1439 md0, maclen) == 0);
1440#endif
1441 }
1442#else
1443 ((void) hash_id);
1444#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
1445
1446
1447 /* Pick IV's (regardless of whether they
1448 * are being used by the transform). */
Yanray Wange6afd912022-10-27 12:11:18 +08001449 memset(iv_enc, 0x3, sizeof(iv_enc));
1450 memset(iv_dec, 0x4, sizeof(iv_dec));
1451
1452 /*
1453 * Setup transforms
1454 */
1455
1456#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
1457 defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
1458 t_out->encrypt_then_mac = etm;
1459 t_in->encrypt_then_mac = etm;
1460#else
1461 ((void) etm);
1462#endif
1463
1464 t_out->tls_version = tls_version;
1465 t_in->tls_version = tls_version;
1466 t_out->ivlen = ivlen;
1467 t_in->ivlen = ivlen;
1468
Valerio Setti31ad3a12023-10-27 11:55:02 +02001469 switch (cipher_mode) {
Yanray Wange6afd912022-10-27 12:11:18 +08001470 case MBEDTLS_MODE_GCM:
1471 case MBEDTLS_MODE_CCM:
1472#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1473 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
1474 t_out->fixed_ivlen = 12;
1475 t_in->fixed_ivlen = 12;
1476 } else
1477#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1478 {
1479 t_out->fixed_ivlen = 4;
1480 t_in->fixed_ivlen = 4;
1481 }
1482 t_out->maclen = 0;
1483 t_in->maclen = 0;
1484 switch (tag_mode) {
1485 case 0: /* Full tag */
1486 t_out->taglen = 16;
1487 t_in->taglen = 16;
1488 break;
1489 case 1: /* Partial tag */
1490 t_out->taglen = 8;
1491 t_in->taglen = 8;
1492 break;
1493 default:
1494 ret = 1;
1495 goto cleanup;
1496 }
1497 break;
1498
1499 case MBEDTLS_MODE_CHACHAPOLY:
1500 t_out->fixed_ivlen = 12;
1501 t_in->fixed_ivlen = 12;
1502 t_out->maclen = 0;
1503 t_in->maclen = 0;
1504 switch (tag_mode) {
1505 case 0: /* Full tag */
1506 t_out->taglen = 16;
1507 t_in->taglen = 16;
1508 break;
1509 case 1: /* Partial tag */
1510 t_out->taglen = 8;
1511 t_in->taglen = 8;
1512 break;
1513 default:
1514 ret = 1;
1515 goto cleanup;
1516 }
1517 break;
1518
1519 case MBEDTLS_MODE_STREAM:
1520 case MBEDTLS_MODE_CBC:
1521 t_out->fixed_ivlen = 0; /* redundant, must be 0 */
1522 t_in->fixed_ivlen = 0; /* redundant, must be 0 */
1523 t_out->taglen = 0;
1524 t_in->taglen = 0;
1525 switch (tag_mode) {
1526 case 0: /* Full tag */
1527 t_out->maclen = maclen;
1528 t_in->maclen = maclen;
1529 break;
1530 default:
1531 ret = 1;
1532 goto cleanup;
1533 }
1534 break;
1535 default:
1536 ret = 1;
1537 goto cleanup;
1538 break;
1539 }
1540
1541 /* Setup IV's */
1542
1543 memcpy(&t_in->iv_dec, iv_dec, sizeof(iv_dec));
1544 memcpy(&t_in->iv_enc, iv_enc, sizeof(iv_enc));
1545 memcpy(&t_out->iv_dec, iv_enc, sizeof(iv_enc));
1546 memcpy(&t_out->iv_enc, iv_dec, sizeof(iv_dec));
1547
1548#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
1549 /* Add CID */
1550 memcpy(&t_in->in_cid, cid0, cid0_len);
1551 memcpy(&t_in->out_cid, cid1, cid1_len);
Yanray Wanga8f445e2022-11-03 11:51:59 +08001552 t_in->in_cid_len = (uint8_t) cid0_len;
1553 t_in->out_cid_len = (uint8_t) cid1_len;
Yanray Wange6afd912022-10-27 12:11:18 +08001554 memcpy(&t_out->in_cid, cid1, cid1_len);
1555 memcpy(&t_out->out_cid, cid0, cid0_len);
Yanray Wanga8f445e2022-11-03 11:51:59 +08001556 t_out->in_cid_len = (uint8_t) cid1_len;
1557 t_out->out_cid_len = (uint8_t) cid0_len;
Yanray Wange6afd912022-10-27 12:11:18 +08001558#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
1559
1560#if defined(MBEDTLS_USE_PSA_CRYPTO)
1561 status = mbedtls_ssl_cipher_to_psa(cipher_type,
1562 t_in->taglen,
1563 &alg,
1564 &key_type,
1565 &key_bits);
1566
1567 if (status != PSA_SUCCESS) {
1568 ret = PSA_TO_MBEDTLS_ERR(status);
1569 goto cleanup;
1570 }
1571
1572 t_in->psa_alg = alg;
1573 t_out->psa_alg = alg;
1574
1575 if (alg != MBEDTLS_SSL_NULL_CIPHER) {
1576 psa_reset_key_attributes(&attributes);
1577 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
1578 psa_set_key_algorithm(&attributes, alg);
1579 psa_set_key_type(&attributes, key_type);
1580
1581 status = psa_import_key(&attributes,
1582 key0,
1583 PSA_BITS_TO_BYTES(key_bits),
1584 &t_in->psa_key_enc);
1585
1586 if (status != PSA_SUCCESS) {
1587 ret = PSA_TO_MBEDTLS_ERR(status);
1588 goto cleanup;
1589 }
1590
1591 status = psa_import_key(&attributes,
1592 key1,
1593 PSA_BITS_TO_BYTES(key_bits),
1594 &t_out->psa_key_enc);
1595
1596 if (status != PSA_SUCCESS) {
1597 ret = PSA_TO_MBEDTLS_ERR(status);
1598 goto cleanup;
1599 }
1600
1601 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
1602
1603 status = psa_import_key(&attributes,
1604 key1,
1605 PSA_BITS_TO_BYTES(key_bits),
1606 &t_in->psa_key_dec);
1607
1608 if (status != PSA_SUCCESS) {
1609 ret = PSA_TO_MBEDTLS_ERR(status);
1610 goto cleanup;
1611 }
1612
1613 status = psa_import_key(&attributes,
1614 key0,
1615 PSA_BITS_TO_BYTES(key_bits),
1616 &t_out->psa_key_dec);
1617
1618 if (status != PSA_SUCCESS) {
1619 ret = PSA_TO_MBEDTLS_ERR(status);
1620 goto cleanup;
1621 }
1622 }
1623#endif /* MBEDTLS_USE_PSA_CRYPTO */
1624
1625cleanup:
1626
1627 mbedtls_free(key0);
1628 mbedtls_free(key1);
1629
1630 mbedtls_free(md0);
1631 mbedtls_free(md1);
1632
1633 return ret;
1634}
1635
Gilles Peskine9099d3f2023-09-18 13:11:50 +02001636#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
1637int mbedtls_test_ssl_prepare_record_mac(mbedtls_record *record,
1638 mbedtls_ssl_transform *transform_out)
1639{
1640#if defined(MBEDTLS_USE_PSA_CRYPTO)
1641 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1642#endif
1643
1644 /* Serialized version of record header for MAC purposes */
1645 unsigned char add_data[13];
1646 memcpy(add_data, record->ctr, 8);
1647 add_data[8] = record->type;
1648 add_data[9] = record->ver[0];
1649 add_data[10] = record->ver[1];
1650 add_data[11] = (record->data_len >> 8) & 0xff;
1651 add_data[12] = (record->data_len >> 0) & 0xff;
1652
1653 /* MAC with additional data */
1654#if defined(MBEDTLS_USE_PSA_CRYPTO)
1655 size_t sign_mac_length = 0;
1656 TEST_EQUAL(PSA_SUCCESS, psa_mac_sign_setup(&operation,
1657 transform_out->psa_mac_enc,
1658 transform_out->psa_mac_alg));
1659 TEST_EQUAL(PSA_SUCCESS, psa_mac_update(&operation, add_data, 13));
1660 TEST_EQUAL(PSA_SUCCESS, psa_mac_update(&operation,
1661 record->buf + record->data_offset,
1662 record->data_len));
1663 /* Use a temporary buffer for the MAC, because with the truncated HMAC
1664 * extension, there might not be enough room in the record for the
1665 * full-length MAC. */
1666 unsigned char mac[PSA_HASH_MAX_SIZE];
1667 TEST_EQUAL(PSA_SUCCESS, psa_mac_sign_finish(&operation,
1668 mac, sizeof(mac),
1669 &sign_mac_length));
1670#else
1671 TEST_EQUAL(0, mbedtls_md_hmac_update(&transform_out->md_ctx_enc, add_data, 13));
1672 TEST_EQUAL(0, mbedtls_md_hmac_update(&transform_out->md_ctx_enc,
1673 record->buf + record->data_offset,
1674 record->data_len));
1675 /* Use a temporary buffer for the MAC, because with the truncated HMAC
1676 * extension, there might not be enough room in the record for the
1677 * full-length MAC. */
1678 unsigned char mac[MBEDTLS_MD_MAX_SIZE];
1679 TEST_EQUAL(0, mbedtls_md_hmac_finish(&transform_out->md_ctx_enc, mac));
1680#endif
1681 memcpy(record->buf + record->data_offset + record->data_len, mac, transform_out->maclen);
1682 record->data_len += transform_out->maclen;
1683
1684 return 0;
1685
1686exit:
1687#if defined(MBEDTLS_USE_PSA_CRYPTO)
1688 psa_mac_abort(&operation);
1689#endif
1690 return -1;
1691}
1692#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
1693
Jerry Yu28547c42023-10-31 14:42:50 +08001694#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Yanray Wange6afd912022-10-27 12:11:18 +08001695int mbedtls_test_ssl_tls12_populate_session(mbedtls_ssl_session *session,
1696 int ticket_len,
Ronald Cron7b1921a2023-11-23 12:31:56 +01001697 int endpoint_type,
Yanray Wange6afd912022-10-27 12:11:18 +08001698 const char *crt_file)
1699{
Ronald Cronc57f86e2023-11-22 09:50:01 +01001700 (void) ticket_len;
1701
Yanray Wange6afd912022-10-27 12:11:18 +08001702#if defined(MBEDTLS_HAVE_TIME)
1703 session->start = mbedtls_time(NULL) - 42;
1704#endif
1705 session->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Ronald Cronc7fa82e2024-02-09 09:33:09 +01001706
1707 TEST_ASSERT(endpoint_type == MBEDTLS_SSL_IS_CLIENT ||
1708 endpoint_type == MBEDTLS_SSL_IS_SERVER);
1709
1710 session->endpoint = endpoint_type;
Yanray Wange6afd912022-10-27 12:11:18 +08001711 session->ciphersuite = 0xabcd;
1712 session->id_len = sizeof(session->id);
1713 memset(session->id, 66, session->id_len);
1714 memset(session->master, 17, sizeof(session->master));
1715
1716#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) && defined(MBEDTLS_FS_IO)
1717 if (crt_file != NULL && strlen(crt_file) != 0) {
1718 mbedtls_x509_crt tmp_crt;
1719 int ret;
1720
1721 mbedtls_x509_crt_init(&tmp_crt);
1722 ret = mbedtls_x509_crt_parse_file(&tmp_crt, crt_file);
1723 if (ret != 0) {
1724 return ret;
1725 }
1726
1727#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1728 /* Move temporary CRT. */
1729 session->peer_cert = mbedtls_calloc(1, sizeof(*session->peer_cert));
1730 if (session->peer_cert == NULL) {
1731 return -1;
1732 }
1733 *session->peer_cert = tmp_crt;
1734 memset(&tmp_crt, 0, sizeof(tmp_crt));
1735#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
1736 /* Calculate digest of temporary CRT. */
1737 session->peer_cert_digest =
1738 mbedtls_calloc(1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN);
1739 if (session->peer_cert_digest == NULL) {
1740 return -1;
1741 }
1742
1743#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02001744 psa_algorithm_t psa_alg = mbedtls_md_psa_alg_from_type(
Yanray Wange6afd912022-10-27 12:11:18 +08001745 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE);
1746 size_t hash_size = 0;
1747 psa_status_t status = psa_hash_compute(
1748 psa_alg, tmp_crt.raw.p,
1749 tmp_crt.raw.len,
1750 session->peer_cert_digest,
1751 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN,
1752 &hash_size);
1753 ret = PSA_TO_MBEDTLS_ERR(status);
1754#else
1755 ret = mbedtls_md(mbedtls_md_info_from_type(
1756 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE),
1757 tmp_crt.raw.p, tmp_crt.raw.len,
1758 session->peer_cert_digest);
1759#endif /* MBEDTLS_USE_PSA_CRYPTO */
1760 if (ret != 0) {
1761 return ret;
1762 }
1763 session->peer_cert_digest_type =
1764 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
1765 session->peer_cert_digest_len =
1766 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
1767#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
1768
1769 mbedtls_x509_crt_free(&tmp_crt);
1770 }
1771#else /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED && MBEDTLS_FS_IO */
1772 (void) crt_file;
1773#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED && MBEDTLS_FS_IO */
1774 session->verify_result = 0xdeadbeef;
1775
Ronald Cronc57f86e2023-11-22 09:50:01 +01001776#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1777#if defined(MBEDTLS_SSL_CLI_C)
Yanray Wange6afd912022-10-27 12:11:18 +08001778 if (ticket_len != 0) {
1779 session->ticket = mbedtls_calloc(1, ticket_len);
1780 if (session->ticket == NULL) {
1781 return -1;
1782 }
1783 memset(session->ticket, 33, ticket_len);
1784 }
1785 session->ticket_len = ticket_len;
1786 session->ticket_lifetime = 86401;
Ronald Cronc57f86e2023-11-22 09:50:01 +01001787#endif /* MBEDTLS_SSL_CLI_C */
1788
1789#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_HAVE_TIME)
1790 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
1791 session->ticket_creation_time = mbedtls_ms_time() - 42;
1792 }
Yanray Wange6afd912022-10-27 12:11:18 +08001793#endif
Ronald Cronc57f86e2023-11-22 09:50:01 +01001794#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Yanray Wange6afd912022-10-27 12:11:18 +08001795
1796#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1797 session->mfl_code = 1;
1798#endif
1799#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1800 session->encrypt_then_mac = 1;
1801#endif
1802
Ronald Cronc7fa82e2024-02-09 09:33:09 +01001803exit:
Yanray Wange6afd912022-10-27 12:11:18 +08001804 return 0;
1805}
Jerry Yu28547c42023-10-31 14:42:50 +08001806#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Yanray Wange6afd912022-10-27 12:11:18 +08001807
1808#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1809int mbedtls_test_ssl_tls13_populate_session(mbedtls_ssl_session *session,
1810 int ticket_len,
1811 int endpoint_type)
1812{
1813 ((void) ticket_len);
1814 session->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
1815 session->endpoint = endpoint_type == MBEDTLS_SSL_IS_CLIENT ?
1816 MBEDTLS_SSL_IS_CLIENT : MBEDTLS_SSL_IS_SERVER;
1817 session->ciphersuite = 0xabcd;
Ronald Cron18b92a12024-03-26 10:15:08 +01001818
1819#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Yanray Wange6afd912022-10-27 12:11:18 +08001820 session->ticket_age_add = 0x87654321;
1821 session->ticket_flags = 0x7;
Yanray Wange6afd912022-10-27 12:11:18 +08001822 session->resumption_key_len = 32;
1823 memset(session->resumption_key, 0x99, sizeof(session->resumption_key));
Yanray Wange6afd912022-10-27 12:11:18 +08001824#endif
1825
Ronald Cron18b92a12024-03-26 10:15:08 +01001826#if defined(MBEDTLS_SSL_SRV_C)
1827 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
1828#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1829#if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN)
1830 int ret = mbedtls_ssl_session_set_ticket_alpn(session, "ALPNExample");
1831 if (ret != 0) {
1832 return -1;
1833 }
1834#endif
1835#if defined(MBEDTLS_HAVE_TIME)
1836 session->ticket_creation_time = mbedtls_ms_time() - 42;
1837#endif
1838#endif /* MBEDTLS_SSL_SESSION_TICKETS */
1839 }
1840#endif /* MBEDTLS_SSL_SRV_C */
1841
Yanray Wange6afd912022-10-27 12:11:18 +08001842#if defined(MBEDTLS_SSL_CLI_C)
1843 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Ronald Cron18b92a12024-03-26 10:15:08 +01001844#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Yanray Wange6afd912022-10-27 12:11:18 +08001845#if defined(MBEDTLS_HAVE_TIME)
Jerry Yu342a5552023-11-10 14:23:39 +08001846 session->ticket_reception_time = mbedtls_ms_time() - 40;
Yanray Wange6afd912022-10-27 12:11:18 +08001847#endif
1848 session->ticket_lifetime = 0xfedcba98;
1849
1850 session->ticket_len = ticket_len;
1851 if (ticket_len != 0) {
1852 session->ticket = mbedtls_calloc(1, ticket_len);
1853 if (session->ticket == NULL) {
1854 return -1;
1855 }
1856 memset(session->ticket, 33, ticket_len);
1857 }
Ronald Cron8d15e012024-03-27 09:30:13 +01001858#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1859 char hostname[] = "hostname example";
1860 session->hostname = mbedtls_calloc(1, sizeof(hostname));
1861 if (session->hostname == NULL) {
1862 return -1;
1863 }
1864 memcpy(session->hostname, hostname, sizeof(hostname));
1865#endif
Ronald Cron18b92a12024-03-26 10:15:08 +01001866#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Yanray Wange6afd912022-10-27 12:11:18 +08001867 }
1868#endif /* MBEDTLS_SSL_CLI_C */
1869
Ronald Cron18b92a12024-03-26 10:15:08 +01001870#if defined(MBEDTLS_SSL_EARLY_DATA)
1871 session->max_early_data_size = 0x87654321;
1872#endif /* MBEDTLS_SSL_EARLY_DATA */
1873
Waleed Elmelegy049cd302023-12-20 17:28:31 +00001874#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
1875 session->record_size_limit = 2048;
1876#endif
1877
Yanray Wange6afd912022-10-27 12:11:18 +08001878 return 0;
1879}
1880#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1881
Yanray Wangb088bfc2023-03-16 12:15:49 +08001882int mbedtls_test_ssl_exchange_data(
1883 mbedtls_ssl_context *ssl_1,
1884 int msg_len_1, const int expected_fragments_1,
1885 mbedtls_ssl_context *ssl_2,
1886 int msg_len_2, const int expected_fragments_2)
Yanray Wange6afd912022-10-27 12:11:18 +08001887{
1888 unsigned char *msg_buf_1 = malloc(msg_len_1);
1889 unsigned char *msg_buf_2 = malloc(msg_len_2);
1890 unsigned char *in_buf_1 = malloc(msg_len_2);
1891 unsigned char *in_buf_2 = malloc(msg_len_1);
1892 int msg_type, ret = -1;
1893
1894 /* Perform this test with two message types. At first use a message
1895 * consisting of only 0x00 for the client and only 0xFF for the server.
1896 * At the second time use message with generated data */
1897 for (msg_type = 0; msg_type < 2; msg_type++) {
1898 int written_1 = 0;
1899 int written_2 = 0;
1900 int read_1 = 0;
1901 int read_2 = 0;
1902 int fragments_1 = 0;
1903 int fragments_2 = 0;
1904
1905 if (msg_type == 0) {
1906 memset(msg_buf_1, 0x00, msg_len_1);
1907 memset(msg_buf_2, 0xff, msg_len_2);
1908 } else {
1909 int i, j = 0;
1910 for (i = 0; i < msg_len_1; i++) {
1911 msg_buf_1[i] = j++ & 0xFF;
1912 }
1913 for (i = 0; i < msg_len_2; i++) {
1914 msg_buf_2[i] = (j -= 5) & 0xFF;
1915 }
1916 }
1917
1918 while (read_1 < msg_len_2 || read_2 < msg_len_1) {
1919 /* ssl_1 sending */
1920 if (msg_len_1 > written_1) {
1921 ret = mbedtls_ssl_write_fragment(ssl_1, msg_buf_1,
1922 msg_len_1, &written_1,
1923 expected_fragments_1);
1924 if (expected_fragments_1 == 0) {
1925 /* This error is expected when the message is too large and
1926 * cannot be fragmented */
Gilles Peskine353eb332025-05-14 17:42:53 +02001927 TEST_EQUAL(ret, MBEDTLS_ERR_SSL_BAD_INPUT_DATA);
Yanray Wange6afd912022-10-27 12:11:18 +08001928 msg_len_1 = 0;
1929 } else {
Gilles Peskine353eb332025-05-14 17:42:53 +02001930 TEST_EQUAL(ret, 0);
Yanray Wange6afd912022-10-27 12:11:18 +08001931 }
1932 }
1933
1934 /* ssl_2 sending */
1935 if (msg_len_2 > written_2) {
1936 ret = mbedtls_ssl_write_fragment(ssl_2, msg_buf_2,
1937 msg_len_2, &written_2,
1938 expected_fragments_2);
1939 if (expected_fragments_2 == 0) {
1940 /* This error is expected when the message is too large and
1941 * cannot be fragmented */
Gilles Peskine353eb332025-05-14 17:42:53 +02001942 TEST_EQUAL(ret, MBEDTLS_ERR_SSL_BAD_INPUT_DATA);
Yanray Wange6afd912022-10-27 12:11:18 +08001943 msg_len_2 = 0;
1944 } else {
Gilles Peskine353eb332025-05-14 17:42:53 +02001945 TEST_EQUAL(ret, 0);
Yanray Wange6afd912022-10-27 12:11:18 +08001946 }
1947 }
1948
1949 /* ssl_1 reading */
1950 if (read_1 < msg_len_2) {
1951 ret = mbedtls_ssl_read_fragment(ssl_1, in_buf_1,
1952 msg_len_2, &read_1,
1953 &fragments_2,
1954 expected_fragments_2);
Gilles Peskine353eb332025-05-14 17:42:53 +02001955 TEST_EQUAL(ret, 0);
Yanray Wange6afd912022-10-27 12:11:18 +08001956 }
1957
1958 /* ssl_2 reading */
1959 if (read_2 < msg_len_1) {
1960 ret = mbedtls_ssl_read_fragment(ssl_2, in_buf_2,
1961 msg_len_1, &read_2,
1962 &fragments_1,
1963 expected_fragments_1);
Gilles Peskine353eb332025-05-14 17:42:53 +02001964 TEST_EQUAL(ret, 0);
Yanray Wange6afd912022-10-27 12:11:18 +08001965 }
1966 }
1967
1968 ret = -1;
Gilles Peskine353eb332025-05-14 17:42:53 +02001969 TEST_EQUAL(0, memcmp(msg_buf_1, in_buf_2, msg_len_1));
1970 TEST_EQUAL(0, memcmp(msg_buf_2, in_buf_1, msg_len_2));
1971 TEST_EQUAL(fragments_1, expected_fragments_1);
1972 TEST_EQUAL(fragments_2, expected_fragments_2);
Yanray Wange6afd912022-10-27 12:11:18 +08001973 }
1974
1975 ret = 0;
1976
1977exit:
1978 free(msg_buf_1);
1979 free(in_buf_1);
1980 free(msg_buf_2);
1981 free(in_buf_2);
1982
1983 return ret;
1984}
1985
1986/*
1987 * Perform data exchanging between \p ssl_1 and \p ssl_2. Both of endpoints
1988 * must be initialized and connected beforehand.
1989 *
1990 * \retval 0 on success, otherwise error code.
1991 */
Yanray Wangead70c82023-03-16 12:04:49 +08001992#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) && \
1993 (defined(MBEDTLS_SSL_RENEGOTIATION) || \
1994 defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH))
1995static int exchange_data(mbedtls_ssl_context *ssl_1,
1996 mbedtls_ssl_context *ssl_2)
Yanray Wange6afd912022-10-27 12:11:18 +08001997{
Yanray Wangb088bfc2023-03-16 12:15:49 +08001998 return mbedtls_test_ssl_exchange_data(ssl_1, 256, 1,
1999 ssl_2, 256, 1);
Yanray Wange6afd912022-10-27 12:11:18 +08002000}
Yanray Wangead70c82023-03-16 12:04:49 +08002001#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED &&
2002 (MBEDTLS_SSL_RENEGOTIATION ||
2003 MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) */
Yanray Wange6afd912022-10-27 12:11:18 +08002004
2005#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
2006static int check_ssl_version(
2007 mbedtls_ssl_protocol_version expected_negotiated_version,
2008 const mbedtls_ssl_context *ssl)
2009{
2010 const char *version_string = mbedtls_ssl_get_version(ssl);
2011 mbedtls_ssl_protocol_version version_number =
2012 mbedtls_ssl_get_version_number(ssl);
2013
2014 TEST_EQUAL(ssl->tls_version, expected_negotiated_version);
2015
2016 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
2017 TEST_EQUAL(version_string[0], 'D');
2018 ++version_string;
2019 }
2020
2021 switch (expected_negotiated_version) {
2022 case MBEDTLS_SSL_VERSION_TLS1_2:
2023 TEST_EQUAL(version_number, MBEDTLS_SSL_VERSION_TLS1_2);
Gilles Peskine353eb332025-05-14 17:42:53 +02002024 TEST_EQUAL(strcmp(version_string, "TLSv1.2"), 0);
Yanray Wange6afd912022-10-27 12:11:18 +08002025 break;
2026
2027 case MBEDTLS_SSL_VERSION_TLS1_3:
2028 TEST_EQUAL(version_number, MBEDTLS_SSL_VERSION_TLS1_3);
Gilles Peskine353eb332025-05-14 17:42:53 +02002029 TEST_EQUAL(strcmp(version_string, "TLSv1.3"), 0);
Yanray Wange6afd912022-10-27 12:11:18 +08002030 break;
2031
2032 default:
Agathiyan Bragadeeshdc28a5a2023-07-18 11:45:28 +01002033 TEST_FAIL(
Agathiyan Bragadeeshebb40bc2023-07-14 17:28:27 +01002034 "Version check not implemented for this protocol version");
Yanray Wange6afd912022-10-27 12:11:18 +08002035 }
2036
2037 return 1;
2038
2039exit:
2040 return 0;
2041}
2042#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
2043
Yanray Wange6afd912022-10-27 12:11:18 +08002044#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Max Fillingercf007ca2024-10-29 16:57:09 +01002045int mbedtls_test_ssl_do_handshake_with_endpoints(
2046 mbedtls_test_ssl_endpoint *server_ep,
2047 mbedtls_test_ssl_endpoint *client_ep,
Max Fillinger8f12e312024-10-30 00:29:37 +01002048 mbedtls_test_handshake_test_options *options,
Max Fillingercf007ca2024-10-29 16:57:09 +01002049 mbedtls_ssl_protocol_version proto)
2050{
2051 enum { BUFFSIZE = 1024 };
2052
2053 int ret = -1;
Max Fillingercf007ca2024-10-29 16:57:09 +01002054
Max Fillingeree467aa2024-11-08 22:17:33 +01002055 mbedtls_platform_zeroize(server_ep, sizeof(mbedtls_test_ssl_endpoint));
2056 mbedtls_platform_zeroize(client_ep, sizeof(mbedtls_test_ssl_endpoint));
2057
Max Fillinger8f12e312024-10-30 00:29:37 +01002058 mbedtls_test_init_handshake_options(options);
2059 options->server_min_version = proto;
2060 options->client_min_version = proto;
2061 options->server_max_version = proto;
2062 options->client_max_version = proto;
Max Fillingercf007ca2024-10-29 16:57:09 +01002063
Max Fillinger8f12e312024-10-30 00:29:37 +01002064 ret = mbedtls_test_ssl_endpoint_init(client_ep, MBEDTLS_SSL_IS_CLIENT, options,
Max Fillingercf007ca2024-10-29 16:57:09 +01002065 NULL, NULL, NULL);
2066 if (ret != 0) {
2067 return ret;
2068 }
Max Fillinger8f12e312024-10-30 00:29:37 +01002069 ret = mbedtls_test_ssl_endpoint_init(server_ep, MBEDTLS_SSL_IS_SERVER, options,
Max Fillingercf007ca2024-10-29 16:57:09 +01002070 NULL, NULL, NULL);
2071 if (ret != 0) {
2072 return ret;
2073 }
2074
2075 ret = mbedtls_test_mock_socket_connect(&client_ep->socket, &server_ep->socket, BUFFSIZE);
2076 if (ret != 0) {
2077 return ret;
2078 }
2079
Max Fillingerea1e7772024-10-30 00:49:10 +01002080 ret = mbedtls_test_move_handshake_to_state(&server_ep->ssl,
2081 &client_ep->ssl,
2082 MBEDTLS_SSL_HANDSHAKE_OVER);
Max Fillingercf007ca2024-10-29 16:57:09 +01002083 if (ret != 0 && ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
2084 return ret;
2085 }
Max Fillingerea1e7772024-10-30 00:49:10 +01002086 ret = mbedtls_test_move_handshake_to_state(&client_ep->ssl,
2087 &server_ep->ssl,
2088 MBEDTLS_SSL_HANDSHAKE_OVER);
Max Fillingercf007ca2024-10-29 16:57:09 +01002089 if (ret != 0 && ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
2090 return ret;
2091 }
Max Fillingerea1e7772024-10-30 00:49:10 +01002092 if (!mbedtls_ssl_is_handshake_over(&client_ep->ssl) ||
2093 !mbedtls_ssl_is_handshake_over(&server_ep->ssl)) {
Max Fillingercf007ca2024-10-29 16:57:09 +01002094 return -1;
2095 }
2096
2097 return 0;
2098}
2099#endif /* defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) */
2100
2101#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Yanray Wange6afd912022-10-27 12:11:18 +08002102void mbedtls_test_ssl_perform_handshake(
2103 mbedtls_test_handshake_test_options *options)
2104{
2105 /* forced_ciphersuite needs to last until the end of the handshake */
2106 int forced_ciphersuite[2];
2107 enum { BUFFSIZE = 17000 };
2108 mbedtls_test_ssl_endpoint client, server;
2109#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
2110 const char *psk_identity = "foo";
2111#endif
2112#if defined(MBEDTLS_TIMING_C)
2113 mbedtls_timing_delay_context timer_client, timer_server;
2114#endif
2115#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
2116 unsigned char *context_buf = NULL;
2117 size_t context_buf_len;
2118#endif
2119#if defined(MBEDTLS_SSL_RENEGOTIATION)
2120 int ret = -1;
2121#endif
2122 int expected_handshake_result = options->expected_handshake_result;
2123
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +01002124 MD_OR_USE_PSA_INIT();
Yanray Wange6afd912022-10-27 12:11:18 +08002125 mbedtls_platform_zeroize(&client, sizeof(client));
2126 mbedtls_platform_zeroize(&server, sizeof(server));
2127 mbedtls_test_ssl_message_queue server_queue, client_queue;
2128 mbedtls_test_message_socket_context server_context, client_context;
2129 mbedtls_test_message_socket_init(&server_context);
2130 mbedtls_test_message_socket_init(&client_context);
2131
Ronald Cronec3408d2024-01-16 17:50:40 +01002132#if defined(MBEDTLS_DEBUG_C)
2133 if (options->cli_log_fun || options->srv_log_fun) {
2134 mbedtls_debug_set_threshold(4);
2135 }
2136#endif
2137
Yanray Wange6afd912022-10-27 12:11:18 +08002138 /* Client side */
2139 if (options->dtls != 0) {
Gilles Peskine353eb332025-05-14 17:42:53 +02002140 TEST_EQUAL(mbedtls_test_ssl_endpoint_init(&client,
2141 MBEDTLS_SSL_IS_CLIENT,
2142 options, &client_context,
2143 &client_queue,
2144 &server_queue), 0);
Yanray Wange6afd912022-10-27 12:11:18 +08002145#if defined(MBEDTLS_TIMING_C)
2146 mbedtls_ssl_set_timer_cb(&client.ssl, &timer_client,
2147 mbedtls_timing_set_delay,
2148 mbedtls_timing_get_delay);
2149#endif
2150 } else {
Gilles Peskine353eb332025-05-14 17:42:53 +02002151 TEST_EQUAL(mbedtls_test_ssl_endpoint_init(&client,
2152 MBEDTLS_SSL_IS_CLIENT,
2153 options, NULL, NULL,
2154 NULL), 0);
Yanray Wange6afd912022-10-27 12:11:18 +08002155 }
2156
Yanray Wange6afd912022-10-27 12:11:18 +08002157 if (strlen(options->cipher) > 0) {
2158 set_ciphersuite(&client.conf, options->cipher, forced_ciphersuite);
2159 }
2160
Yanray Wange6afd912022-10-27 12:11:18 +08002161 /* Server side */
2162 if (options->dtls != 0) {
Gilles Peskine353eb332025-05-14 17:42:53 +02002163 TEST_EQUAL(mbedtls_test_ssl_endpoint_init(&server,
2164 MBEDTLS_SSL_IS_SERVER,
2165 options, &server_context,
2166 &server_queue,
2167 &client_queue), 0);
Yanray Wange6afd912022-10-27 12:11:18 +08002168#if defined(MBEDTLS_TIMING_C)
2169 mbedtls_ssl_set_timer_cb(&server.ssl, &timer_server,
2170 mbedtls_timing_set_delay,
2171 mbedtls_timing_get_delay);
2172#endif
2173 } else {
Gilles Peskine353eb332025-05-14 17:42:53 +02002174 TEST_EQUAL(mbedtls_test_ssl_endpoint_init(&server,
2175 MBEDTLS_SSL_IS_SERVER,
2176 options, NULL, NULL,
2177 NULL), 0);
Yanray Wange6afd912022-10-27 12:11:18 +08002178 }
2179
2180 mbedtls_ssl_conf_authmode(&server.conf, options->srv_auth_mode);
2181
Yanray Wange6afd912022-10-27 12:11:18 +08002182#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine353eb332025-05-14 17:42:53 +02002183 TEST_EQUAL(mbedtls_ssl_conf_max_frag_len(&(server.conf),
2184 (unsigned char) options->mfl),
2185 0);
2186 TEST_EQUAL(mbedtls_ssl_conf_max_frag_len(&(client.conf),
2187 (unsigned char) options->mfl),
2188 0);
Yanray Wange6afd912022-10-27 12:11:18 +08002189#else
Gilles Peskine353eb332025-05-14 17:42:53 +02002190 TEST_EQUAL(MBEDTLS_SSL_MAX_FRAG_LEN_NONE, options->mfl);
Yanray Wange6afd912022-10-27 12:11:18 +08002191#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2192
2193#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
2194 if (options->psk_str != NULL && options->psk_str->len > 0) {
Gilles Peskine353eb332025-05-14 17:42:53 +02002195 TEST_EQUAL(mbedtls_ssl_conf_psk(
2196 &client.conf, options->psk_str->x,
2197 options->psk_str->len,
2198 (const unsigned char *) psk_identity,
2199 strlen(psk_identity)), 0);
Yanray Wange6afd912022-10-27 12:11:18 +08002200
Gilles Peskine353eb332025-05-14 17:42:53 +02002201 TEST_EQUAL(mbedtls_ssl_conf_psk(
2202 &server.conf, options->psk_str->x,
2203 options->psk_str->len,
2204 (const unsigned char *) psk_identity,
2205 strlen(psk_identity)), 0);
Yanray Wange6afd912022-10-27 12:11:18 +08002206#if defined(MBEDTLS_SSL_SRV_C)
2207 mbedtls_ssl_conf_psk_cb(&server.conf, psk_dummy_callback, NULL);
2208#endif
2209 }
2210#endif
2211#if defined(MBEDTLS_SSL_RENEGOTIATION)
2212 if (options->renegotiate) {
2213 mbedtls_ssl_conf_renegotiation(&(server.conf),
2214 MBEDTLS_SSL_RENEGOTIATION_ENABLED);
2215 mbedtls_ssl_conf_renegotiation(&(client.conf),
2216 MBEDTLS_SSL_RENEGOTIATION_ENABLED);
2217
2218 mbedtls_ssl_conf_legacy_renegotiation(&(server.conf),
2219 options->legacy_renegotiation);
2220 mbedtls_ssl_conf_legacy_renegotiation(&(client.conf),
2221 options->legacy_renegotiation);
2222 }
2223#endif /* MBEDTLS_SSL_RENEGOTIATION */
2224
Gilles Peskine353eb332025-05-14 17:42:53 +02002225 TEST_EQUAL(mbedtls_test_mock_socket_connect(&(client.socket),
2226 &(server.socket),
2227 BUFFSIZE), 0);
Yanray Wange6afd912022-10-27 12:11:18 +08002228
2229#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2230 if (options->resize_buffers != 0) {
2231 /* Ensure that the buffer sizes are appropriate before resizes */
Gilles Peskine353eb332025-05-14 17:42:53 +02002232 TEST_EQUAL(client.ssl.out_buf_len, MBEDTLS_SSL_OUT_BUFFER_LEN);
2233 TEST_EQUAL(client.ssl.in_buf_len, MBEDTLS_SSL_IN_BUFFER_LEN);
2234 TEST_EQUAL(server.ssl.out_buf_len, MBEDTLS_SSL_OUT_BUFFER_LEN);
2235 TEST_EQUAL(server.ssl.in_buf_len, MBEDTLS_SSL_IN_BUFFER_LEN);
Yanray Wange6afd912022-10-27 12:11:18 +08002236 }
2237#endif
2238
2239 if (options->expected_negotiated_version == MBEDTLS_SSL_VERSION_UNKNOWN) {
2240 expected_handshake_result = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
2241 }
2242
Gilles Peskine353eb332025-05-14 17:42:53 +02002243 TEST_EQUAL(mbedtls_test_move_handshake_to_state(&(client.ssl),
2244 &(server.ssl),
2245 MBEDTLS_SSL_HANDSHAKE_OVER),
2246 expected_handshake_result);
Yanray Wange6afd912022-10-27 12:11:18 +08002247
2248 if (expected_handshake_result != 0) {
2249 /* Connection will have failed by this point, skip to cleanup */
2250 goto exit;
2251 }
2252
Gilles Peskine353eb332025-05-14 17:42:53 +02002253 TEST_EQUAL(mbedtls_ssl_is_handshake_over(&client.ssl), 1);
Yanray Wange6afd912022-10-27 12:11:18 +08002254
2255 /* Make sure server state is moved to HANDSHAKE_OVER also. */
2256 TEST_EQUAL(mbedtls_test_move_handshake_to_state(&(server.ssl),
2257 &(client.ssl),
2258 MBEDTLS_SSL_HANDSHAKE_OVER),
2259 0);
2260
Gilles Peskine353eb332025-05-14 17:42:53 +02002261 TEST_EQUAL(mbedtls_ssl_is_handshake_over(&server.ssl), 1);
Yanray Wange6afd912022-10-27 12:11:18 +08002262 /* Check that both sides have negotiated the expected version. */
2263 mbedtls_test_set_step(0);
2264 if (!check_ssl_version(options->expected_negotiated_version,
2265 &client.ssl)) {
2266 goto exit;
2267 }
2268
2269 mbedtls_test_set_step(1);
2270 if (!check_ssl_version(options->expected_negotiated_version,
2271 &server.ssl)) {
2272 goto exit;
2273 }
2274
2275 if (options->expected_ciphersuite != 0) {
2276 TEST_EQUAL(server.ssl.session->ciphersuite,
2277 options->expected_ciphersuite);
2278 }
2279
2280#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2281 if (options->resize_buffers != 0) {
2282 /* A server, when using DTLS, might delay a buffer resize to happen
2283 * after it receives a message, so we force it. */
Gilles Peskine353eb332025-05-14 17:42:53 +02002284 TEST_EQUAL(exchange_data(&(client.ssl), &(server.ssl)), 0);
Yanray Wange6afd912022-10-27 12:11:18 +08002285
Gilles Peskine353eb332025-05-14 17:42:53 +02002286 TEST_EQUAL(client.ssl.out_buf_len,
2287 mbedtls_ssl_get_output_buflen(&client.ssl));
2288 TEST_EQUAL(client.ssl.in_buf_len,
2289 mbedtls_ssl_get_input_buflen(&client.ssl));
2290 TEST_EQUAL(server.ssl.out_buf_len,
2291 mbedtls_ssl_get_output_buflen(&server.ssl));
2292 TEST_EQUAL(server.ssl.in_buf_len,
2293 mbedtls_ssl_get_input_buflen(&server.ssl));
Yanray Wange6afd912022-10-27 12:11:18 +08002294 }
2295#endif
2296
2297 if (options->cli_msg_len != 0 || options->srv_msg_len != 0) {
2298 /* Start data exchanging test */
Gilles Peskine353eb332025-05-14 17:42:53 +02002299 TEST_EQUAL(mbedtls_test_ssl_exchange_data(
2300 &(client.ssl), options->cli_msg_len,
2301 options->expected_cli_fragments,
2302 &(server.ssl), options->srv_msg_len,
2303 options->expected_srv_fragments),
2304 0);
Yanray Wange6afd912022-10-27 12:11:18 +08002305 }
2306#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
2307 if (options->serialize == 1) {
Gilles Peskine353eb332025-05-14 17:42:53 +02002308 TEST_EQUAL(options->dtls, 1);
Yanray Wange6afd912022-10-27 12:11:18 +08002309
Gilles Peskine353eb332025-05-14 17:42:53 +02002310 TEST_EQUAL(mbedtls_ssl_context_save(&(server.ssl), NULL,
2311 0, &context_buf_len),
2312 MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL);
Yanray Wange6afd912022-10-27 12:11:18 +08002313
2314 context_buf = mbedtls_calloc(1, context_buf_len);
2315 TEST_ASSERT(context_buf != NULL);
2316
Gilles Peskine353eb332025-05-14 17:42:53 +02002317 TEST_EQUAL(mbedtls_ssl_context_save(&(server.ssl), context_buf,
2318 context_buf_len,
2319 &context_buf_len),
2320 0);
Yanray Wange6afd912022-10-27 12:11:18 +08002321
2322 mbedtls_ssl_free(&(server.ssl));
2323 mbedtls_ssl_init(&(server.ssl));
2324
Gilles Peskine353eb332025-05-14 17:42:53 +02002325 TEST_EQUAL(mbedtls_ssl_setup(&(server.ssl), &(server.conf)), 0);
Yanray Wange6afd912022-10-27 12:11:18 +08002326
2327 mbedtls_ssl_set_bio(&(server.ssl), &server_context,
2328 mbedtls_test_mock_tcp_send_msg,
2329 mbedtls_test_mock_tcp_recv_msg,
2330 NULL);
2331
2332 mbedtls_ssl_set_user_data_p(&server.ssl, &server);
2333
2334#if defined(MBEDTLS_TIMING_C)
2335 mbedtls_ssl_set_timer_cb(&server.ssl, &timer_server,
2336 mbedtls_timing_set_delay,
2337 mbedtls_timing_get_delay);
2338#endif
2339#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2340 if (options->resize_buffers != 0) {
2341 /* Ensure that the buffer sizes are appropriate before resizes */
Gilles Peskine353eb332025-05-14 17:42:53 +02002342 TEST_EQUAL(server.ssl.out_buf_len, MBEDTLS_SSL_OUT_BUFFER_LEN);
2343 TEST_EQUAL(server.ssl.in_buf_len, MBEDTLS_SSL_IN_BUFFER_LEN);
Yanray Wange6afd912022-10-27 12:11:18 +08002344 }
2345#endif
Gilles Peskine353eb332025-05-14 17:42:53 +02002346 TEST_EQUAL(mbedtls_ssl_context_load(&(server.ssl), context_buf,
2347 context_buf_len), 0);
Yanray Wange6afd912022-10-27 12:11:18 +08002348
2349#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2350 /* Validate buffer sizes after context deserialization */
2351 if (options->resize_buffers != 0) {
Gilles Peskine353eb332025-05-14 17:42:53 +02002352 TEST_EQUAL(server.ssl.out_buf_len,
2353 mbedtls_ssl_get_output_buflen(&server.ssl));
2354 TEST_EQUAL(server.ssl.in_buf_len,
2355 mbedtls_ssl_get_input_buflen(&server.ssl));
Yanray Wange6afd912022-10-27 12:11:18 +08002356 }
2357#endif
2358 /* Retest writing/reading */
2359 if (options->cli_msg_len != 0 || options->srv_msg_len != 0) {
Gilles Peskine353eb332025-05-14 17:42:53 +02002360 TEST_EQUAL(mbedtls_test_ssl_exchange_data(
2361 &(client.ssl), options->cli_msg_len,
2362 options->expected_cli_fragments,
2363 &(server.ssl), options->srv_msg_len,
2364 options->expected_srv_fragments),
2365 0);
Yanray Wange6afd912022-10-27 12:11:18 +08002366 }
2367 }
2368#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
2369
2370#if defined(MBEDTLS_SSL_RENEGOTIATION)
2371 if (options->renegotiate) {
2372 /* Start test with renegotiation */
Gilles Peskine353eb332025-05-14 17:42:53 +02002373 TEST_EQUAL(server.ssl.renego_status,
2374 MBEDTLS_SSL_INITIAL_HANDSHAKE);
2375 TEST_EQUAL(client.ssl.renego_status,
2376 MBEDTLS_SSL_INITIAL_HANDSHAKE);
Yanray Wange6afd912022-10-27 12:11:18 +08002377
2378 /* After calling this function for the server, it only sends a handshake
2379 * request. All renegotiation should happen during data exchanging */
Gilles Peskine353eb332025-05-14 17:42:53 +02002380 TEST_EQUAL(mbedtls_ssl_renegotiate(&(server.ssl)), 0);
2381 TEST_EQUAL(server.ssl.renego_status,
2382 MBEDTLS_SSL_RENEGOTIATION_PENDING);
2383 TEST_EQUAL(client.ssl.renego_status,
2384 MBEDTLS_SSL_INITIAL_HANDSHAKE);
Yanray Wange6afd912022-10-27 12:11:18 +08002385
Gilles Peskine353eb332025-05-14 17:42:53 +02002386 TEST_EQUAL(exchange_data(&(client.ssl), &(server.ssl)), 0);
2387 TEST_EQUAL(server.ssl.renego_status,
2388 MBEDTLS_SSL_RENEGOTIATION_DONE);
2389 TEST_EQUAL(client.ssl.renego_status,
2390 MBEDTLS_SSL_RENEGOTIATION_DONE);
Yanray Wange6afd912022-10-27 12:11:18 +08002391
2392 /* After calling mbedtls_ssl_renegotiate for the client,
2393 * all renegotiation should happen inside this function.
2394 * However in this test, we cannot perform simultaneous communication
2395 * between client and server so this function will return waiting error
2396 * on the socket. All rest of renegotiation should happen
2397 * during data exchanging */
2398 ret = mbedtls_ssl_renegotiate(&(client.ssl));
2399#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2400 if (options->resize_buffers != 0) {
2401 /* Ensure that the buffer sizes are appropriate before resizes */
Gilles Peskine353eb332025-05-14 17:42:53 +02002402 TEST_EQUAL(client.ssl.out_buf_len, MBEDTLS_SSL_OUT_BUFFER_LEN);
2403 TEST_EQUAL(client.ssl.in_buf_len, MBEDTLS_SSL_IN_BUFFER_LEN);
Yanray Wange6afd912022-10-27 12:11:18 +08002404 }
2405#endif
2406 TEST_ASSERT(ret == 0 ||
2407 ret == MBEDTLS_ERR_SSL_WANT_READ ||
2408 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
Gilles Peskine353eb332025-05-14 17:42:53 +02002409 TEST_EQUAL(server.ssl.renego_status,
2410 MBEDTLS_SSL_RENEGOTIATION_DONE);
2411 TEST_EQUAL(client.ssl.renego_status,
2412 MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS);
Yanray Wange6afd912022-10-27 12:11:18 +08002413
Gilles Peskine353eb332025-05-14 17:42:53 +02002414 TEST_EQUAL(exchange_data(&(client.ssl), &(server.ssl)), 0);
2415 TEST_EQUAL(server.ssl.renego_status,
2416 MBEDTLS_SSL_RENEGOTIATION_DONE);
2417 TEST_EQUAL(client.ssl.renego_status,
2418 MBEDTLS_SSL_RENEGOTIATION_DONE);
Yanray Wange6afd912022-10-27 12:11:18 +08002419#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2420 /* Validate buffer sizes after renegotiation */
2421 if (options->resize_buffers != 0) {
Gilles Peskine353eb332025-05-14 17:42:53 +02002422 TEST_EQUAL(client.ssl.out_buf_len,
2423 mbedtls_ssl_get_output_buflen(&client.ssl));
2424 TEST_EQUAL(client.ssl.in_buf_len,
2425 mbedtls_ssl_get_input_buflen(&client.ssl));
2426 TEST_EQUAL(server.ssl.out_buf_len,
2427 mbedtls_ssl_get_output_buflen(&server.ssl));
2428 TEST_EQUAL(server.ssl.in_buf_len,
2429 mbedtls_ssl_get_input_buflen(&server.ssl));
Yanray Wange6afd912022-10-27 12:11:18 +08002430 }
2431#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
2432 }
2433#endif /* MBEDTLS_SSL_RENEGOTIATION */
2434
2435 TEST_ASSERT(mbedtls_ssl_conf_get_user_data_p(&client.conf) == &client);
2436 TEST_ASSERT(mbedtls_ssl_get_user_data_p(&client.ssl) == &client);
2437 TEST_ASSERT(mbedtls_ssl_conf_get_user_data_p(&server.conf) == &server);
2438 TEST_ASSERT(mbedtls_ssl_get_user_data_p(&server.ssl) == &server);
2439
2440exit:
2441 mbedtls_test_ssl_endpoint_free(&client,
Yanray Wangaf727a22023-03-13 19:22:36 +08002442 options->dtls != 0 ? &client_context : NULL);
Yanray Wange6afd912022-10-27 12:11:18 +08002443 mbedtls_test_ssl_endpoint_free(&server,
Yanray Wangaf727a22023-03-13 19:22:36 +08002444 options->dtls != 0 ? &server_context : NULL);
Yanray Wange6afd912022-10-27 12:11:18 +08002445#if defined(MBEDTLS_DEBUG_C)
2446 if (options->cli_log_fun || options->srv_log_fun) {
2447 mbedtls_debug_set_threshold(0);
2448 }
2449#endif
2450#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
2451 if (context_buf != NULL) {
2452 mbedtls_free(context_buf);
2453 }
2454#endif
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +01002455 MD_OR_USE_PSA_DONE();
Yanray Wange6afd912022-10-27 12:11:18 +08002456}
2457#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
2458
2459#if defined(MBEDTLS_TEST_HOOKS)
Yanray Wangf56181a2023-03-16 12:21:33 +08002460int mbedtls_test_tweak_tls13_certificate_msg_vector_len(
Yanray Wange6afd912022-10-27 12:11:18 +08002461 unsigned char *buf, unsigned char **end, int tweak,
2462 int *expected_result, mbedtls_ssl_chk_buf_ptr_args *args)
2463{
2464/*
2465 * The definition of the tweaks assume that the certificate list contains only
2466 * one certificate.
2467 */
2468
2469/*
2470 * struct {
2471 * opaque cert_data<1..2^24-1>;
2472 * Extension extensions<0..2^16-1>;
2473 * } CertificateEntry;
2474 *
2475 * struct {
2476 * opaque certificate_request_context<0..2^8-1>;
2477 * CertificateEntry certificate_list<0..2^24-1>;
2478 * } Certificate;
2479 */
2480 unsigned char *p_certificate_request_context_len = buf;
2481 size_t certificate_request_context_len = buf[0];
2482
2483 unsigned char *p_certificate_list_len =
2484 buf + 1 + certificate_request_context_len;
2485 unsigned char *certificate_list = p_certificate_list_len + 3;
2486 size_t certificate_list_len =
2487 MBEDTLS_GET_UINT24_BE(p_certificate_list_len, 0);
2488
2489 unsigned char *p_cert_data_len = certificate_list;
2490 unsigned char *cert_data = p_cert_data_len + 3;
2491 size_t cert_data_len = MBEDTLS_GET_UINT24_BE(p_cert_data_len, 0);
2492
2493 unsigned char *p_extensions_len = cert_data + cert_data_len;
2494 unsigned char *extensions = p_extensions_len + 2;
2495 size_t extensions_len = MBEDTLS_GET_UINT16_BE(p_extensions_len, 0);
2496
2497 *expected_result = MBEDTLS_ERR_SSL_DECODE_ERROR;
2498
2499 switch (tweak) {
2500 case 1:
2501 /* Failure when checking if the certificate request context length
2502 * and certificate list length can be read
2503 */
2504 *end = buf + 3;
2505 set_chk_buf_ptr_args(args, buf, *end, 4);
2506 break;
2507
2508 case 2:
2509 /* Invalid certificate request context length.
2510 */
2511 *p_certificate_request_context_len =
Yanray Wanga8f445e2022-11-03 11:51:59 +08002512 (unsigned char) certificate_request_context_len + 1;
Yanray Wange6afd912022-10-27 12:11:18 +08002513 reset_chk_buf_ptr_args(args);
2514 break;
2515
2516 case 3:
2517 /* Failure when checking if certificate_list data can be read. */
2518 MBEDTLS_PUT_UINT24_BE(certificate_list_len + 1,
2519 p_certificate_list_len, 0);
2520 set_chk_buf_ptr_args(args, certificate_list, *end,
2521 certificate_list_len + 1);
2522 break;
2523
2524 case 4:
2525 /* Failure when checking if the cert_data length can be read. */
2526 MBEDTLS_PUT_UINT24_BE(2, p_certificate_list_len, 0);
2527 set_chk_buf_ptr_args(args, p_cert_data_len, certificate_list + 2, 3);
2528 break;
2529
2530 case 5:
2531 /* Failure when checking if cert_data data can be read. */
2532 MBEDTLS_PUT_UINT24_BE(certificate_list_len - 3 + 1,
2533 p_cert_data_len, 0);
2534 set_chk_buf_ptr_args(args, cert_data,
2535 certificate_list + certificate_list_len,
2536 certificate_list_len - 3 + 1);
2537 break;
2538
2539 case 6:
2540 /* Failure when checking if the extensions length can be read. */
2541 MBEDTLS_PUT_UINT24_BE(certificate_list_len - extensions_len - 1,
2542 p_certificate_list_len, 0);
2543 set_chk_buf_ptr_args(
2544 args, p_extensions_len,
2545 certificate_list + certificate_list_len - extensions_len - 1, 2);
2546 break;
2547
2548 case 7:
2549 /* Failure when checking if extensions data can be read. */
2550 MBEDTLS_PUT_UINT16_BE(extensions_len + 1, p_extensions_len, 0);
2551
2552 set_chk_buf_ptr_args(
2553 args, extensions,
2554 certificate_list + certificate_list_len, extensions_len + 1);
2555 break;
2556
2557 default:
2558 return -1;
2559 }
2560
2561 return 0;
2562}
2563#endif /* MBEDTLS_TEST_HOOKS */
Ronald Cron77abfe62024-01-15 11:17:31 +01002564
Ronald Cronfaf026c2024-01-31 14:32:06 +01002565/*
2566 * Functions for tests based on tickets. Implementations of the
2567 * write/parse ticket interfaces as defined by mbedtls_ssl_ticket_write/parse_t.
2568 * Basically same implementations as in ticket.c without the encryption. That
2569 * way we can tweak easily tickets characteristics to simulate misbehaving
2570 * peers.
2571 */
Ronald Cron77abfe62024-01-15 11:17:31 +01002572#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2573int mbedtls_test_ticket_write(
2574 void *p_ticket, const mbedtls_ssl_session *session,
2575 unsigned char *start, const unsigned char *end,
2576 size_t *tlen, uint32_t *lifetime)
2577{
2578 int ret;
2579 ((void) p_ticket);
2580
2581 if ((ret = mbedtls_ssl_session_save(session, start, end - start,
2582 tlen)) != 0) {
2583 return ret;
2584 }
2585
2586 /* Maximum ticket lifetime as defined in RFC 8446 */
2587 *lifetime = 7 * 24 * 3600;
2588
2589 return 0;
2590}
2591
2592int mbedtls_test_ticket_parse(void *p_ticket, mbedtls_ssl_session *session,
2593 unsigned char *buf, size_t len)
2594{
2595 ((void) p_ticket);
2596
2597 return mbedtls_ssl_session_load(session, buf, len);
2598}
2599#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Ronald Cron1f6e4e42024-01-26 16:31:33 +01002600
2601#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SRV_C) && \
2602 defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS) && \
2603 defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
2604int mbedtls_test_get_tls13_ticket(
2605 mbedtls_test_handshake_test_options *client_options,
2606 mbedtls_test_handshake_test_options *server_options,
2607 mbedtls_ssl_session *session)
2608{
2609 int ret = -1;
Gilles Peskine946bf142025-04-08 09:48:40 +02002610 int ok = 0;
Ronald Cron1f6e4e42024-01-26 16:31:33 +01002611 unsigned char buf[64];
2612 mbedtls_test_ssl_endpoint client_ep, server_ep;
2613
2614 mbedtls_platform_zeroize(&client_ep, sizeof(client_ep));
2615 mbedtls_platform_zeroize(&server_ep, sizeof(server_ep));
2616
2617 ret = mbedtls_test_ssl_endpoint_init(&client_ep, MBEDTLS_SSL_IS_CLIENT,
2618 client_options, NULL, NULL, NULL);
2619 TEST_EQUAL(ret, 0);
2620
2621 ret = mbedtls_test_ssl_endpoint_init(&server_ep, MBEDTLS_SSL_IS_SERVER,
2622 server_options, NULL, NULL, NULL);
2623 TEST_EQUAL(ret, 0);
2624
2625 mbedtls_ssl_conf_session_tickets_cb(&server_ep.conf,
2626 mbedtls_test_ticket_write,
2627 mbedtls_test_ticket_parse,
2628 NULL);
2629
2630 ret = mbedtls_test_mock_socket_connect(&(client_ep.socket),
2631 &(server_ep.socket), 1024);
2632 TEST_EQUAL(ret, 0);
2633
2634 TEST_EQUAL(mbedtls_test_move_handshake_to_state(
2635 &(server_ep.ssl), &(client_ep.ssl),
2636 MBEDTLS_SSL_HANDSHAKE_OVER), 0);
2637
2638 TEST_EQUAL(server_ep.ssl.handshake->new_session_tickets_count, 0);
2639
2640 do {
2641 ret = mbedtls_ssl_read(&(client_ep.ssl), buf, sizeof(buf));
2642 } while (ret != MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET);
2643
2644 ret = mbedtls_ssl_get_session(&(client_ep.ssl), session);
2645 TEST_EQUAL(ret, 0);
2646
Gilles Peskine946bf142025-04-08 09:48:40 +02002647 ok = 1;
2648
Ronald Cron1f6e4e42024-01-26 16:31:33 +01002649exit:
2650 mbedtls_test_ssl_endpoint_free(&client_ep, NULL);
2651 mbedtls_test_ssl_endpoint_free(&server_ep, NULL);
2652
Gilles Peskine946bf142025-04-08 09:48:40 +02002653 if (ret == 0 && !ok) {
2654 /* Exiting due to a test assertion that isn't ret == 0 */
2655 ret = -1;
2656 }
Ronald Cron1f6e4e42024-01-26 16:31:33 +01002657 return ret;
2658}
2659#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SRV_C &&
2660 MBEDTLS_SSL_PROTO_TLS1_3 && MBEDTLS_SSL_SESSION_TICKETS &&
2661 MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
2662
Yanray Wang4d07d1c2022-10-27 15:28:16 +08002663#endif /* MBEDTLS_SSL_TLS_C */