blob: 184c0cd05bdc94d3b8f06ce18f53ea0f0433edcf [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
Gilles Peskine946bf142025-04-08 09:48:40 +0200723 ok = 1;
724
Yanray Wange6afd912022-10-27 12:11:18 +0800725exit:
Gilles Peskine946bf142025-04-08 09:48:40 +0200726 if (ret == 0 && !ok) {
727 /* Exiting due to a test assertion that isn't ret == 0 */
728 ret = -1;
729 }
Yanray Wange6afd912022-10-27 12:11:18 +0800730 if (ret != 0) {
Yanray Wangf6f71902023-03-15 16:05:14 +0800731 test_ssl_endpoint_certificate_free(ep);
Yanray Wange6afd912022-10-27 12:11:18 +0800732 }
733
734 return ret;
735}
736
Yanray Wange6afd912022-10-27 12:11:18 +0800737int mbedtls_test_ssl_endpoint_init(
738 mbedtls_test_ssl_endpoint *ep, int endpoint_type,
Gilles Peskineca8a9ac2025-05-27 20:52:24 +0200739 const mbedtls_test_handshake_test_options *options)
Yanray Wange6afd912022-10-27 12:11:18 +0800740{
741 int ret = -1;
742 uintptr_t user_data_n;
743
Yanray Wange6afd912022-10-27 12:11:18 +0800744 if (ep == NULL) {
745 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
746 }
747
748 memset(ep, 0, sizeof(*ep));
749
750 ep->name = (endpoint_type == MBEDTLS_SSL_IS_SERVER) ? "Server" : "Client";
751
752 mbedtls_ssl_init(&(ep->ssl));
753 mbedtls_ssl_config_init(&(ep->conf));
Gilles Peskine29969592025-05-27 19:24:28 +0200754 mbedtls_test_message_socket_init(&ep->dtls_context);
Yanray Wange6afd912022-10-27 12:11:18 +0800755
756 TEST_ASSERT(mbedtls_ssl_conf_get_user_data_p(&ep->conf) == NULL);
757 TEST_EQUAL(mbedtls_ssl_conf_get_user_data_n(&ep->conf), 0);
758 TEST_ASSERT(mbedtls_ssl_get_user_data_p(&ep->ssl) == NULL);
759 TEST_EQUAL(mbedtls_ssl_get_user_data_n(&ep->ssl), 0);
760
761 (void) mbedtls_test_rnd_std_rand(NULL,
762 (void *) &user_data_n,
763 sizeof(user_data_n));
764 mbedtls_ssl_conf_set_user_data_n(&ep->conf, user_data_n);
765 mbedtls_ssl_set_user_data_n(&ep->ssl, user_data_n);
766
Gilles Peskine6c154e72025-05-27 20:23:52 +0200767 mbedtls_test_mock_socket_init(&(ep->socket));
Yanray Wange6afd912022-10-27 12:11:18 +0800768
769 /* Non-blocking callbacks without timeout */
Gilles Peskine29969592025-05-27 19:24:28 +0200770 if (options->dtls) {
771 mbedtls_ssl_set_bio(&(ep->ssl), &ep->dtls_context,
Yanray Wange6afd912022-10-27 12:11:18 +0800772 mbedtls_test_mock_tcp_send_msg,
773 mbedtls_test_mock_tcp_recv_msg,
774 NULL);
Gilles Peskine0677e022025-05-27 18:05:20 +0200775#if defined(MBEDTLS_TIMING_C)
776 mbedtls_ssl_set_timer_cb(&ep->ssl, &ep->timer,
777 mbedtls_timing_set_delay,
778 mbedtls_timing_get_delay);
779#endif
Yanray Wange6afd912022-10-27 12:11:18 +0800780 } else {
781 mbedtls_ssl_set_bio(&(ep->ssl), &(ep->socket),
782 mbedtls_test_mock_tcp_send_nb,
783 mbedtls_test_mock_tcp_recv_nb,
784 NULL);
785 }
786
787 ret = mbedtls_ssl_config_defaults(&(ep->conf), endpoint_type,
Gilles Peskine29969592025-05-27 19:24:28 +0200788 options->dtls ?
Yanray Wange6afd912022-10-27 12:11:18 +0800789 MBEDTLS_SSL_TRANSPORT_DATAGRAM :
790 MBEDTLS_SSL_TRANSPORT_STREAM,
791 MBEDTLS_SSL_PRESET_DEFAULT);
Gilles Peskine353eb332025-05-14 17:42:53 +0200792 TEST_EQUAL(ret, 0);
Yanray Wange6afd912022-10-27 12:11:18 +0800793
Ronald Crona697a712023-03-09 17:47:42 +0100794 if (MBEDTLS_SSL_IS_CLIENT == endpoint_type) {
795 if (options->client_min_version != MBEDTLS_SSL_VERSION_UNKNOWN) {
796 mbedtls_ssl_conf_min_tls_version(&(ep->conf),
797 options->client_min_version);
798 }
799
800 if (options->client_max_version != MBEDTLS_SSL_VERSION_UNKNOWN) {
801 mbedtls_ssl_conf_max_tls_version(&(ep->conf),
802 options->client_max_version);
803 }
804 } else {
805 if (options->server_min_version != MBEDTLS_SSL_VERSION_UNKNOWN) {
806 mbedtls_ssl_conf_min_tls_version(&(ep->conf),
807 options->server_min_version);
808 }
809
810 if (options->server_max_version != MBEDTLS_SSL_VERSION_UNKNOWN) {
811 mbedtls_ssl_conf_max_tls_version(&(ep->conf),
812 options->server_max_version);
813 }
814 }
815
Ronald Cronfb536472024-01-26 14:55:25 +0100816 if (options->group_list != NULL) {
817 mbedtls_ssl_conf_groups(&(ep->conf), options->group_list);
Yanray Wange6afd912022-10-27 12:11:18 +0800818 }
819
820 mbedtls_ssl_conf_authmode(&(ep->conf), MBEDTLS_SSL_VERIFY_REQUIRED);
821
Ronald Cronced99be2024-01-26 15:49:12 +0100822#if defined(MBEDTLS_SSL_EARLY_DATA)
823 mbedtls_ssl_conf_early_data(&(ep->conf), options->early_data);
Ronald Cron5d3036e2024-02-23 07:43:45 +0100824#if defined(MBEDTLS_SSL_SRV_C)
825 if (endpoint_type == MBEDTLS_SSL_IS_SERVER &&
826 (options->max_early_data_size >= 0)) {
827 mbedtls_ssl_conf_max_early_data_size(&(ep->conf),
828 options->max_early_data_size);
829 }
830#endif
Waleed Elmelegy4dfb0e72024-03-14 01:48:40 +0000831#if defined(MBEDTLS_SSL_ALPN)
832 /* check that alpn_list contains at least one valid entry */
833 if (options->alpn_list[0] != NULL) {
834 mbedtls_ssl_conf_alpn_protocols(&(ep->conf), options->alpn_list);
835 }
836#endif
Ronald Cronced99be2024-01-26 15:49:12 +0100837#endif
838
Yanray Wange6afd912022-10-27 12:11:18 +0800839#if defined(MBEDTLS_SSL_CACHE_C) && defined(MBEDTLS_SSL_SRV_C)
840 if (endpoint_type == MBEDTLS_SSL_IS_SERVER && options->cache != NULL) {
841 mbedtls_ssl_conf_session_cache(&(ep->conf), options->cache,
842 mbedtls_ssl_cache_get,
843 mbedtls_ssl_cache_set);
844 }
845#endif
846
847 ret = mbedtls_ssl_setup(&(ep->ssl), &(ep->conf));
Gilles Peskine353eb332025-05-14 17:42:53 +0200848 TEST_EQUAL(ret, 0);
Yanray Wange6afd912022-10-27 12:11:18 +0800849
Gilles Peskine856a3702025-02-13 17:28:49 +0100850 if (MBEDTLS_SSL_IS_CLIENT == endpoint_type) {
851 ret = mbedtls_ssl_set_hostname(&(ep->ssl), "localhost");
Gilles Peskine55b8bb42025-04-08 09:44:34 +0200852 TEST_EQUAL(ret, 0);
Gilles Peskine856a3702025-02-13 17:28:49 +0100853 }
854
Yanray Wange6afd912022-10-27 12:11:18 +0800855#if defined(MBEDTLS_SSL_PROTO_DTLS) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine29969592025-05-27 19:24:28 +0200856 if (endpoint_type == MBEDTLS_SSL_IS_SERVER && options->dtls) {
Yanray Wange6afd912022-10-27 12:11:18 +0800857 mbedtls_ssl_conf_dtls_cookies(&(ep->conf), NULL, NULL, NULL);
858 }
859#endif
860
Ronald Cronec3408d2024-01-16 17:50:40 +0100861#if defined(MBEDTLS_DEBUG_C)
862#if defined(MBEDTLS_SSL_SRV_C)
863 if (endpoint_type == MBEDTLS_SSL_IS_SERVER &&
864 options->srv_log_fun != NULL) {
865 mbedtls_ssl_conf_dbg(&(ep->conf), options->srv_log_fun,
866 options->srv_log_obj);
867 }
868#endif
869#if defined(MBEDTLS_SSL_CLI_C)
870 if (endpoint_type == MBEDTLS_SSL_IS_CLIENT &&
871 options->cli_log_fun != NULL) {
872 mbedtls_ssl_conf_dbg(&(ep->conf), options->cli_log_fun,
873 options->cli_log_obj);
874 }
875#endif
876#endif /* MBEDTLS_DEBUG_C */
877
Yanray Wange6afd912022-10-27 12:11:18 +0800878 ret = mbedtls_test_ssl_endpoint_certificate_init(ep, options->pk_alg,
879 options->opaque_alg,
880 options->opaque_alg2,
881 options->opaque_usage);
Gilles Peskine353eb332025-05-14 17:42:53 +0200882 TEST_EQUAL(ret, 0);
Yanray Wange6afd912022-10-27 12:11:18 +0800883
884 TEST_EQUAL(mbedtls_ssl_conf_get_user_data_n(&ep->conf), user_data_n);
885 mbedtls_ssl_conf_set_user_data_p(&ep->conf, ep);
886 TEST_EQUAL(mbedtls_ssl_get_user_data_n(&ep->ssl), user_data_n);
887 mbedtls_ssl_set_user_data_p(&ep->ssl, ep);
888
Gilles Peskine946bf142025-04-08 09:48:40 +0200889 return 0;
890
Yanray Wange6afd912022-10-27 12:11:18 +0800891exit:
Gilles Peskine946bf142025-04-08 09:48:40 +0200892 if (ret == 0) {
893 /* Exiting due to a test assertion that isn't ret == 0 */
894 ret = -1;
895 }
Yanray Wange6afd912022-10-27 12:11:18 +0800896 return ret;
897}
898
Yanray Wange6afd912022-10-27 12:11:18 +0800899void mbedtls_test_ssl_endpoint_free(
Gilles Peskineca8a9ac2025-05-27 20:52:24 +0200900 mbedtls_test_ssl_endpoint *ep)
Yanray Wange6afd912022-10-27 12:11:18 +0800901{
Yanray Wange6afd912022-10-27 12:11:18 +0800902 mbedtls_ssl_free(&(ep->ssl));
903 mbedtls_ssl_config_free(&(ep->conf));
904
Gilles Peskine2744a432025-05-27 13:27:22 +0200905 mbedtls_free(ep->ciphersuites);
906 ep->ciphersuites = NULL;
907 test_ssl_endpoint_certificate_free(ep);
908
Gilles Peskine29969592025-05-27 19:24:28 +0200909 if (ep->dtls_context.socket != NULL) {
910 mbedtls_test_message_socket_close(&ep->dtls_context);
Yanray Wange6afd912022-10-27 12:11:18 +0800911 } else {
912 mbedtls_test_mock_socket_close(&(ep->socket));
913 }
914}
915
Gilles Peskineb092e782025-05-27 20:15:03 +0200916int mbedtls_test_ssl_dtls_join_endpoints(mbedtls_test_ssl_endpoint *client,
917 mbedtls_test_ssl_endpoint *server)
918{
919 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
920
Gilles Peskine6c154e72025-05-27 20:23:52 +0200921 ret = mbedtls_test_message_socket_setup(&client->queue_input,
922 &server->queue_input,
923 100, &(client->socket),
924 &client->dtls_context);
925 TEST_EQUAL(ret, 0);
Gilles Peskineb092e782025-05-27 20:15:03 +0200926
Gilles Peskine6c154e72025-05-27 20:23:52 +0200927 ret = mbedtls_test_message_socket_setup(&server->queue_input,
928 &client->queue_input,
929 100, &(server->socket),
930 &server->dtls_context);
931 TEST_EQUAL(ret, 0);
932
933exit:
Gilles Peskineb092e782025-05-27 20:15:03 +0200934 return ret;
935}
936
Yanray Wange6afd912022-10-27 12:11:18 +0800937int mbedtls_test_move_handshake_to_state(mbedtls_ssl_context *ssl,
938 mbedtls_ssl_context *second_ssl,
939 int state)
940{
941 enum { BUFFSIZE = 1024 };
942 int max_steps = 1000;
943 int ret = 0;
944
945 if (ssl == NULL || second_ssl == NULL) {
946 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
947 }
948
949 /* Perform communication via connected sockets */
950 while ((ssl->state != state) && (--max_steps >= 0)) {
951 /* If /p second_ssl ends the handshake procedure before /p ssl then
952 * there is no need to call the next step */
953 if (!mbedtls_ssl_is_handshake_over(second_ssl)) {
954 ret = mbedtls_ssl_handshake_step(second_ssl);
955 if (ret != 0 && ret != MBEDTLS_ERR_SSL_WANT_READ &&
956 ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
957 return ret;
958 }
959 }
960
961 /* We only care about the \p ssl state and returns, so we call it last,
962 * to leave the iteration as soon as the state is as expected. */
963 ret = mbedtls_ssl_handshake_step(ssl);
964 if (ret != 0 && ret != MBEDTLS_ERR_SSL_WANT_READ &&
965 ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
966 return ret;
967 }
968 }
969
970 return (max_steps >= 0) ? ret : -1;
971}
972
973#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
974
975/*
976 * Write application data. Increase write counter if necessary.
977 */
Michael Schuster54300d42024-06-04 02:30:22 +0200978static int mbedtls_ssl_write_fragment(mbedtls_ssl_context *ssl,
Michael Schusterbd89b792024-06-04 02:41:10 +0200979 unsigned char *buf, int buf_len,
980 int *written,
981 const int expected_fragments)
Yanray Wange6afd912022-10-27 12:11:18 +0800982{
Agathiyan Bragadeesh93212652023-07-12 11:22:59 +0100983 int ret;
Yanray Wange6afd912022-10-27 12:11:18 +0800984 /* Verify that calling mbedtls_ssl_write with a NULL buffer and zero length is
985 * a valid no-op for TLS connections. */
986 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Gilles Peskine353eb332025-05-14 17:42:53 +0200987 TEST_EQUAL(mbedtls_ssl_write(ssl, NULL, 0), 0);
Yanray Wange6afd912022-10-27 12:11:18 +0800988 }
989
Agathiyan Bragadeesh93212652023-07-12 11:22:59 +0100990 ret = mbedtls_ssl_write(ssl, buf + *written, buf_len - *written);
Yanray Wange6afd912022-10-27 12:11:18 +0800991 if (ret > 0) {
992 *written += ret;
993 }
994
995 if (expected_fragments == 0) {
996 /* Used for DTLS and the message size larger than MFL. In that case
997 * the message can not be fragmented and the library should return
998 * MBEDTLS_ERR_SSL_BAD_INPUT_DATA error. This error must be returned
Yanray Wangb088bfc2023-03-16 12:15:49 +0800999 * to prevent a dead loop inside mbedtls_test_ssl_exchange_data(). */
Yanray Wange6afd912022-10-27 12:11:18 +08001000 return ret;
1001 } else if (expected_fragments == 1) {
1002 /* Used for TLS/DTLS and the message size lower than MFL */
1003 TEST_ASSERT(ret == buf_len ||
1004 ret == MBEDTLS_ERR_SSL_WANT_READ ||
1005 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
1006 } else {
1007 /* Used for TLS and the message size larger than MFL */
1008 TEST_ASSERT(expected_fragments > 1);
1009 TEST_ASSERT((ret >= 0 && ret <= buf_len) ||
1010 ret == MBEDTLS_ERR_SSL_WANT_READ ||
1011 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
1012 }
1013
1014 return 0;
1015
1016exit:
1017 /* Some of the tests failed */
1018 return -1;
1019}
1020
1021/*
1022 * Read application data and increase read counter and fragments counter
1023 * if necessary.
1024 */
Michael Schuster54300d42024-06-04 02:30:22 +02001025static int mbedtls_ssl_read_fragment(mbedtls_ssl_context *ssl,
Michael Schusterbd89b792024-06-04 02:41:10 +02001026 unsigned char *buf, int buf_len,
1027 int *read, int *fragments,
1028 const int expected_fragments)
Yanray Wange6afd912022-10-27 12:11:18 +08001029{
Agathiyan Bragadeesh93212652023-07-12 11:22:59 +01001030 int ret;
Yanray Wange6afd912022-10-27 12:11:18 +08001031 /* Verify that calling mbedtls_ssl_write with a NULL buffer and zero length is
1032 * a valid no-op for TLS connections. */
1033 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Gilles Peskine353eb332025-05-14 17:42:53 +02001034 TEST_EQUAL(mbedtls_ssl_read(ssl, NULL, 0), 0);
Yanray Wange6afd912022-10-27 12:11:18 +08001035 }
1036
Agathiyan Bragadeesh93212652023-07-12 11:22:59 +01001037 ret = mbedtls_ssl_read(ssl, buf + *read, buf_len - *read);
Yanray Wange6afd912022-10-27 12:11:18 +08001038 if (ret > 0) {
1039 (*fragments)++;
1040 *read += ret;
1041 }
1042
1043 if (expected_fragments == 0) {
Gilles Peskine353eb332025-05-14 17:42:53 +02001044 TEST_EQUAL(ret, 0);
Yanray Wange6afd912022-10-27 12:11:18 +08001045 } else if (expected_fragments == 1) {
1046 TEST_ASSERT(ret == buf_len ||
1047 ret == MBEDTLS_ERR_SSL_WANT_READ ||
1048 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
1049 } else {
1050 TEST_ASSERT(expected_fragments > 1);
1051 TEST_ASSERT((ret >= 0 && ret <= buf_len) ||
1052 ret == MBEDTLS_ERR_SSL_WANT_READ ||
1053 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
1054 }
1055
1056 return 0;
1057
1058exit:
1059 /* Some of the tests failed */
1060 return -1;
1061}
1062
Yanray Wangead70c82023-03-16 12:04:49 +08001063#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gilles Peskine2744a432025-05-27 13:27:22 +02001064static int set_ciphersuite(mbedtls_test_ssl_endpoint *ep,
1065 const char *cipher)
Yanray Wange6afd912022-10-27 12:11:18 +08001066{
Gilles Peskine2744a432025-05-27 13:27:22 +02001067 if (cipher == NULL || cipher[0] == 0) {
1068 return 1;
1069 }
Yanray Wange6afd912022-10-27 12:11:18 +08001070
Gilles Peskine2744a432025-05-27 13:27:22 +02001071 int ok = 0;
1072
1073 TEST_CALLOC(ep->ciphersuites, 2);
1074 ep->ciphersuites[0] = mbedtls_ssl_get_ciphersuite_id(cipher);
1075 ep->ciphersuites[1] = 0;
1076
1077 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
1078 mbedtls_ssl_ciphersuite_from_id(ep->ciphersuites[0]);
Yanray Wange6afd912022-10-27 12:11:18 +08001079
1080 TEST_ASSERT(ciphersuite_info != NULL);
Gilles Peskine2744a432025-05-27 13:27:22 +02001081 TEST_ASSERT(ciphersuite_info->min_tls_version <= ep->conf.max_tls_version);
1082 TEST_ASSERT(ciphersuite_info->max_tls_version >= ep->conf.min_tls_version);
Yanray Wange6afd912022-10-27 12:11:18 +08001083
Gilles Peskine2744a432025-05-27 13:27:22 +02001084 if (ep->conf.max_tls_version > ciphersuite_info->max_tls_version) {
1085 ep->conf.max_tls_version = (mbedtls_ssl_protocol_version) ciphersuite_info->max_tls_version;
Yanray Wange6afd912022-10-27 12:11:18 +08001086 }
Gilles Peskine2744a432025-05-27 13:27:22 +02001087 if (ep->conf.min_tls_version < ciphersuite_info->min_tls_version) {
1088 ep->conf.min_tls_version = (mbedtls_ssl_protocol_version) ciphersuite_info->min_tls_version;
Yanray Wange6afd912022-10-27 12:11:18 +08001089 }
1090
Gilles Peskine2744a432025-05-27 13:27:22 +02001091 mbedtls_ssl_conf_ciphersuites(&ep->conf, ep->ciphersuites);
1092 ok = 1;
Yanray Wange6afd912022-10-27 12:11:18 +08001093
1094exit:
Gilles Peskine2744a432025-05-27 13:27:22 +02001095 return ok;
Yanray Wange6afd912022-10-27 12:11:18 +08001096}
Yanray Wangead70c82023-03-16 12:04:49 +08001097#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Yanray Wange6afd912022-10-27 12:11:18 +08001098
Yanray Wangead70c82023-03-16 12:04:49 +08001099#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) && \
1100 defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED) && \
1101 defined(MBEDTLS_SSL_SRV_C)
1102static int psk_dummy_callback(void *p_info, mbedtls_ssl_context *ssl,
1103 const unsigned char *name, size_t name_len)
Yanray Wange6afd912022-10-27 12:11:18 +08001104{
1105 (void) p_info;
1106 (void) ssl;
1107 (void) name;
1108 (void) name_len;
1109
1110 return 0;
1111}
Yanray Wangead70c82023-03-16 12:04:49 +08001112#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED &&
1113 MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED &&
1114 MBEDTLS_SSL_SRV_C */
Yanray Wange6afd912022-10-27 12:11:18 +08001115
1116#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Elena Uziunaite74342c72024-07-05 11:31:29 +01001117 defined(PSA_WANT_ALG_CBC_NO_PADDING) && defined(PSA_WANT_KEY_TYPE_AES)
Yanray Wange6afd912022-10-27 12:11:18 +08001118int mbedtls_test_psa_cipher_encrypt_helper(mbedtls_ssl_transform *transform,
1119 const unsigned char *iv,
1120 size_t iv_len,
1121 const unsigned char *input,
1122 size_t ilen,
1123 unsigned char *output,
1124 size_t *olen)
1125{
1126#if defined(MBEDTLS_USE_PSA_CRYPTO)
1127 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1128 psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT;
1129 size_t part_len;
1130
1131 status = psa_cipher_encrypt_setup(&cipher_op,
1132 transform->psa_key_enc,
1133 transform->psa_alg);
1134
1135 if (status != PSA_SUCCESS) {
1136 return PSA_TO_MBEDTLS_ERR(status);
1137 }
1138
1139 status = psa_cipher_set_iv(&cipher_op, iv, iv_len);
1140
1141 if (status != PSA_SUCCESS) {
1142 return PSA_TO_MBEDTLS_ERR(status);
1143 }
1144
1145 status = psa_cipher_update(&cipher_op, input, ilen, output, ilen, olen);
1146
1147 if (status != PSA_SUCCESS) {
1148 return PSA_TO_MBEDTLS_ERR(status);
1149 }
1150
1151 status = psa_cipher_finish(&cipher_op, output + *olen, ilen - *olen,
1152 &part_len);
1153
1154 if (status != PSA_SUCCESS) {
1155 return PSA_TO_MBEDTLS_ERR(status);
1156 }
1157
1158 *olen += part_len;
1159 return 0;
1160#else
1161 return mbedtls_cipher_crypt(&transform->cipher_ctx_enc,
1162 iv, iv_len, input, ilen, output, olen);
1163#endif /* MBEDTLS_USE_PSA_CRYPTO */
1164}
Elena Uziunaite74342c72024-07-05 11:31:29 +01001165#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && PSA_WANT_ALG_CBC_NO_PADDING &&
Elena Uziunaite6121a342024-07-05 11:16:53 +01001166 PSA_WANT_KEY_TYPE_AES */
Yanray Wange6afd912022-10-27 12:11:18 +08001167
Valerio Setti31ad3a12023-10-27 11:55:02 +02001168static void mbedtls_test_ssl_cipher_info_from_type(mbedtls_cipher_type_t cipher_type,
1169 mbedtls_cipher_mode_t *cipher_mode,
1170 size_t *key_bits, size_t *iv_len)
1171{
1172 switch (cipher_type) {
1173 case MBEDTLS_CIPHER_AES_128_CBC:
1174 *cipher_mode = MBEDTLS_MODE_CBC;
1175 *key_bits = 128;
1176 *iv_len = 16;
1177 break;
1178 case MBEDTLS_CIPHER_AES_256_CBC:
1179 *cipher_mode = MBEDTLS_MODE_CBC;
1180 *key_bits = 256;
1181 *iv_len = 16;
1182 break;
1183 case MBEDTLS_CIPHER_ARIA_128_CBC:
1184 *cipher_mode = MBEDTLS_MODE_CBC;
1185 *key_bits = 128;
1186 *iv_len = 16;
1187 break;
1188 case MBEDTLS_CIPHER_ARIA_256_CBC:
1189 *cipher_mode = MBEDTLS_MODE_CBC;
1190 *key_bits = 256;
1191 *iv_len = 16;
1192 break;
1193 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
1194 *cipher_mode = MBEDTLS_MODE_CBC;
1195 *key_bits = 128;
1196 *iv_len = 16;
1197 break;
1198 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
1199 *cipher_mode = MBEDTLS_MODE_CBC;
1200 *key_bits = 256;
1201 *iv_len = 16;
1202 break;
1203
1204 case MBEDTLS_CIPHER_AES_128_CCM:
1205 *cipher_mode = MBEDTLS_MODE_CCM;
1206 *key_bits = 128;
1207 *iv_len = 12;
1208 break;
1209 case MBEDTLS_CIPHER_AES_192_CCM:
1210 *cipher_mode = MBEDTLS_MODE_CCM;
1211 *key_bits = 192;
1212 *iv_len = 12;
1213 break;
1214 case MBEDTLS_CIPHER_AES_256_CCM:
1215 *cipher_mode = MBEDTLS_MODE_CCM;
1216 *key_bits = 256;
1217 *iv_len = 12;
1218 break;
1219 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
1220 *cipher_mode = MBEDTLS_MODE_CCM;
1221 *key_bits = 128;
1222 *iv_len = 12;
1223 break;
1224 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
1225 *cipher_mode = MBEDTLS_MODE_CCM;
1226 *key_bits = 192;
1227 *iv_len = 12;
1228 break;
1229 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
1230 *cipher_mode = MBEDTLS_MODE_CCM;
1231 *key_bits = 256;
1232 *iv_len = 12;
1233 break;
1234
1235 case MBEDTLS_CIPHER_AES_128_GCM:
1236 *cipher_mode = MBEDTLS_MODE_GCM;
1237 *key_bits = 128;
1238 *iv_len = 12;
1239 break;
1240 case MBEDTLS_CIPHER_AES_192_GCM:
1241 *cipher_mode = MBEDTLS_MODE_GCM;
1242 *key_bits = 192;
1243 *iv_len = 12;
1244 break;
1245 case MBEDTLS_CIPHER_AES_256_GCM:
1246 *cipher_mode = MBEDTLS_MODE_GCM;
1247 *key_bits = 256;
1248 *iv_len = 12;
1249 break;
1250 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
1251 *cipher_mode = MBEDTLS_MODE_GCM;
1252 *key_bits = 128;
1253 *iv_len = 12;
1254 break;
1255 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
1256 *cipher_mode = MBEDTLS_MODE_GCM;
1257 *key_bits = 192;
1258 *iv_len = 12;
1259 break;
1260 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
1261 *cipher_mode = MBEDTLS_MODE_GCM;
1262 *key_bits = 256;
1263 *iv_len = 12;
1264 break;
1265
1266 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
1267 *cipher_mode = MBEDTLS_MODE_CHACHAPOLY;
1268 *key_bits = 256;
1269 *iv_len = 12;
1270 break;
1271
1272 case MBEDTLS_CIPHER_NULL:
1273 *cipher_mode = MBEDTLS_MODE_STREAM;
1274 *key_bits = 0;
1275 *iv_len = 0;
1276 break;
1277
1278 default:
1279 *cipher_mode = MBEDTLS_MODE_NONE;
1280 *key_bits = 0;
1281 *iv_len = 0;
1282 }
1283}
1284
Yanray Wange6afd912022-10-27 12:11:18 +08001285int mbedtls_test_ssl_build_transforms(mbedtls_ssl_transform *t_in,
1286 mbedtls_ssl_transform *t_out,
1287 int cipher_type, int hash_id,
1288 int etm, int tag_mode,
1289 mbedtls_ssl_protocol_version tls_version,
1290 size_t cid0_len,
1291 size_t cid1_len)
1292{
Valerio Setti31ad3a12023-10-27 11:55:02 +02001293 mbedtls_cipher_mode_t cipher_mode = MBEDTLS_MODE_NONE;
1294 size_t key_bits = 0;
Yanray Wange6afd912022-10-27 12:11:18 +08001295 int ret = 0;
1296
1297#if defined(MBEDTLS_USE_PSA_CRYPTO)
1298 psa_key_type_t key_type;
1299 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1300 psa_algorithm_t alg;
Yanray Wange6afd912022-10-27 12:11:18 +08001301 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Valerio Setti3d59ebe2023-10-30 11:56:56 +01001302#else
Valerio Setti31ad3a12023-10-27 11:55:02 +02001303 mbedtls_cipher_info_t const *cipher_info;
Yanray Wange6afd912022-10-27 12:11:18 +08001304#endif
1305
Valerio Setti31ad3a12023-10-27 11:55:02 +02001306 size_t keylen, maclen, ivlen = 0;
Yanray Wange6afd912022-10-27 12:11:18 +08001307 unsigned char *key0 = NULL, *key1 = NULL;
1308 unsigned char *md0 = NULL, *md1 = NULL;
1309 unsigned char iv_enc[16], iv_dec[16];
1310
1311#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
1312 unsigned char cid0[SSL_CID_LEN_MIN];
1313 unsigned char cid1[SSL_CID_LEN_MIN];
1314
1315 mbedtls_test_rnd_std_rand(NULL, cid0, sizeof(cid0));
1316 mbedtls_test_rnd_std_rand(NULL, cid1, sizeof(cid1));
1317#else
1318 ((void) cid0_len);
1319 ((void) cid1_len);
1320#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
1321
1322 maclen = 0;
Valerio Setti31ad3a12023-10-27 11:55:02 +02001323 mbedtls_test_ssl_cipher_info_from_type((mbedtls_cipher_type_t) cipher_type,
1324 &cipher_mode, &key_bits, &ivlen);
Yanray Wange6afd912022-10-27 12:11:18 +08001325
1326 /* Pick keys */
Valerio Setti31ad3a12023-10-27 11:55:02 +02001327 keylen = key_bits / 8;
Yanray Wange6afd912022-10-27 12:11:18 +08001328 /* Allocate `keylen + 1` bytes to ensure that we get
1329 * a non-NULL pointers from `mbedtls_calloc` even if
1330 * `keylen == 0` in the case of the NULL cipher. */
1331 CHK((key0 = mbedtls_calloc(1, keylen + 1)) != NULL);
1332 CHK((key1 = mbedtls_calloc(1, keylen + 1)) != NULL);
1333 memset(key0, 0x1, keylen);
1334 memset(key1, 0x2, keylen);
1335
1336#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti31ad3a12023-10-27 11:55:02 +02001337 /* Pick cipher */
1338 cipher_info = mbedtls_cipher_info_from_type((mbedtls_cipher_type_t) cipher_type);
1339 CHK(cipher_info != NULL);
1340 CHK(mbedtls_cipher_info_get_iv_size(cipher_info) <= 16);
1341 CHK(mbedtls_cipher_info_get_key_bitlen(cipher_info) % 8 == 0);
Valerio Setti3d59ebe2023-10-30 11:56:56 +01001342
Yanray Wange6afd912022-10-27 12:11:18 +08001343 /* Setup cipher contexts */
1344 CHK(mbedtls_cipher_setup(&t_in->cipher_ctx_enc, cipher_info) == 0);
1345 CHK(mbedtls_cipher_setup(&t_in->cipher_ctx_dec, cipher_info) == 0);
1346 CHK(mbedtls_cipher_setup(&t_out->cipher_ctx_enc, cipher_info) == 0);
1347 CHK(mbedtls_cipher_setup(&t_out->cipher_ctx_dec, cipher_info) == 0);
1348
1349#if defined(MBEDTLS_CIPHER_MODE_CBC)
Valerio Setti31ad3a12023-10-27 11:55:02 +02001350 if (cipher_mode == MBEDTLS_MODE_CBC) {
Yanray Wange6afd912022-10-27 12:11:18 +08001351 CHK(mbedtls_cipher_set_padding_mode(&t_in->cipher_ctx_enc,
1352 MBEDTLS_PADDING_NONE) == 0);
1353 CHK(mbedtls_cipher_set_padding_mode(&t_in->cipher_ctx_dec,
1354 MBEDTLS_PADDING_NONE) == 0);
1355 CHK(mbedtls_cipher_set_padding_mode(&t_out->cipher_ctx_enc,
1356 MBEDTLS_PADDING_NONE) == 0);
1357 CHK(mbedtls_cipher_set_padding_mode(&t_out->cipher_ctx_dec,
1358 MBEDTLS_PADDING_NONE) == 0);
1359 }
1360#endif /* MBEDTLS_CIPHER_MODE_CBC */
1361
1362 CHK(mbedtls_cipher_setkey(&t_in->cipher_ctx_enc, key0,
Yanray Wanga8f445e2022-11-03 11:51:59 +08001363 (keylen << 3 > INT_MAX) ? INT_MAX : (int) keylen << 3,
1364 MBEDTLS_ENCRYPT)
1365 == 0);
Yanray Wange6afd912022-10-27 12:11:18 +08001366 CHK(mbedtls_cipher_setkey(&t_in->cipher_ctx_dec, key1,
Yanray Wanga8f445e2022-11-03 11:51:59 +08001367 (keylen << 3 > INT_MAX) ? INT_MAX : (int) keylen << 3,
1368 MBEDTLS_DECRYPT)
1369 == 0);
Yanray Wange6afd912022-10-27 12:11:18 +08001370 CHK(mbedtls_cipher_setkey(&t_out->cipher_ctx_enc, key1,
Yanray Wanga8f445e2022-11-03 11:51:59 +08001371 (keylen << 3 > INT_MAX) ? INT_MAX : (int) keylen << 3,
1372 MBEDTLS_ENCRYPT)
1373 == 0);
Yanray Wange6afd912022-10-27 12:11:18 +08001374 CHK(mbedtls_cipher_setkey(&t_out->cipher_ctx_dec, key0,
Yanray Wanga8f445e2022-11-03 11:51:59 +08001375 (keylen << 3 > INT_MAX) ? INT_MAX : (int) keylen << 3,
1376 MBEDTLS_DECRYPT)
1377 == 0);
Valerio Setti31ad3a12023-10-27 11:55:02 +02001378#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Yanray Wange6afd912022-10-27 12:11:18 +08001379
1380 /* Setup MAC contexts */
1381#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Valerio Setti31ad3a12023-10-27 11:55:02 +02001382 if (cipher_mode == MBEDTLS_MODE_CBC ||
1383 cipher_mode == MBEDTLS_MODE_STREAM) {
Yanray Wange6afd912022-10-27 12:11:18 +08001384#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Agathiyan Bragadeesh2f017a82023-07-12 11:21:54 +01001385 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 +08001386 CHK(md_info != NULL);
1387#endif
Agathiyan Bragadeesh2f017a82023-07-12 11:21:54 +01001388 maclen = mbedtls_md_get_size_from_type((mbedtls_md_type_t) hash_id);
Yanray Wange6afd912022-10-27 12:11:18 +08001389 CHK(maclen != 0);
1390 /* Pick hash keys */
1391 CHK((md0 = mbedtls_calloc(1, maclen)) != NULL);
1392 CHK((md1 = mbedtls_calloc(1, maclen)) != NULL);
1393 memset(md0, 0x5, maclen);
1394 memset(md1, 0x6, maclen);
1395
1396#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02001397 alg = mbedtls_md_psa_alg_from_type(hash_id);
Yanray Wange6afd912022-10-27 12:11:18 +08001398
1399 CHK(alg != 0);
1400
1401 t_out->psa_mac_alg = PSA_ALG_HMAC(alg);
1402 t_in->psa_mac_alg = PSA_ALG_HMAC(alg);
1403 t_in->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
1404 t_out->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
1405 t_in->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
1406 t_out->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
1407
1408 psa_reset_key_attributes(&attributes);
1409 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
1410 psa_set_key_algorithm(&attributes, PSA_ALG_HMAC(alg));
1411 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
1412
1413 CHK(psa_import_key(&attributes,
1414 md0, maclen,
1415 &t_in->psa_mac_enc) == PSA_SUCCESS);
1416
1417 CHK(psa_import_key(&attributes,
1418 md1, maclen,
1419 &t_out->psa_mac_enc) == PSA_SUCCESS);
1420
Valerio Setti31ad3a12023-10-27 11:55:02 +02001421 if (cipher_mode == MBEDTLS_MODE_STREAM ||
Yanray Wange6afd912022-10-27 12:11:18 +08001422 etm == MBEDTLS_SSL_ETM_DISABLED) {
1423 /* mbedtls_ct_hmac() requires the key to be exportable */
1424 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT |
1425 PSA_KEY_USAGE_VERIFY_HASH);
1426 } else {
1427 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
1428 }
1429
1430 CHK(psa_import_key(&attributes,
1431 md1, maclen,
1432 &t_in->psa_mac_dec) == PSA_SUCCESS);
1433
1434 CHK(psa_import_key(&attributes,
1435 md0, maclen,
1436 &t_out->psa_mac_dec) == PSA_SUCCESS);
1437#else
1438 CHK(mbedtls_md_setup(&t_out->md_ctx_enc, md_info, 1) == 0);
1439 CHK(mbedtls_md_setup(&t_out->md_ctx_dec, md_info, 1) == 0);
1440 CHK(mbedtls_md_setup(&t_in->md_ctx_enc, md_info, 1) == 0);
1441 CHK(mbedtls_md_setup(&t_in->md_ctx_dec, md_info, 1) == 0);
1442
1443 CHK(mbedtls_md_hmac_starts(&t_in->md_ctx_enc,
1444 md0, maclen) == 0);
1445 CHK(mbedtls_md_hmac_starts(&t_in->md_ctx_dec,
1446 md1, maclen) == 0);
1447 CHK(mbedtls_md_hmac_starts(&t_out->md_ctx_enc,
1448 md1, maclen) == 0);
1449 CHK(mbedtls_md_hmac_starts(&t_out->md_ctx_dec,
1450 md0, maclen) == 0);
1451#endif
1452 }
1453#else
1454 ((void) hash_id);
1455#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
1456
1457
1458 /* Pick IV's (regardless of whether they
1459 * are being used by the transform). */
Yanray Wange6afd912022-10-27 12:11:18 +08001460 memset(iv_enc, 0x3, sizeof(iv_enc));
1461 memset(iv_dec, 0x4, sizeof(iv_dec));
1462
1463 /*
1464 * Setup transforms
1465 */
1466
1467#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
1468 defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
1469 t_out->encrypt_then_mac = etm;
1470 t_in->encrypt_then_mac = etm;
1471#else
1472 ((void) etm);
1473#endif
1474
1475 t_out->tls_version = tls_version;
1476 t_in->tls_version = tls_version;
1477 t_out->ivlen = ivlen;
1478 t_in->ivlen = ivlen;
1479
Valerio Setti31ad3a12023-10-27 11:55:02 +02001480 switch (cipher_mode) {
Yanray Wange6afd912022-10-27 12:11:18 +08001481 case MBEDTLS_MODE_GCM:
1482 case MBEDTLS_MODE_CCM:
1483#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1484 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
1485 t_out->fixed_ivlen = 12;
1486 t_in->fixed_ivlen = 12;
1487 } else
1488#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1489 {
1490 t_out->fixed_ivlen = 4;
1491 t_in->fixed_ivlen = 4;
1492 }
1493 t_out->maclen = 0;
1494 t_in->maclen = 0;
1495 switch (tag_mode) {
1496 case 0: /* Full tag */
1497 t_out->taglen = 16;
1498 t_in->taglen = 16;
1499 break;
1500 case 1: /* Partial tag */
1501 t_out->taglen = 8;
1502 t_in->taglen = 8;
1503 break;
1504 default:
1505 ret = 1;
1506 goto cleanup;
1507 }
1508 break;
1509
1510 case MBEDTLS_MODE_CHACHAPOLY:
1511 t_out->fixed_ivlen = 12;
1512 t_in->fixed_ivlen = 12;
1513 t_out->maclen = 0;
1514 t_in->maclen = 0;
1515 switch (tag_mode) {
1516 case 0: /* Full tag */
1517 t_out->taglen = 16;
1518 t_in->taglen = 16;
1519 break;
1520 case 1: /* Partial tag */
1521 t_out->taglen = 8;
1522 t_in->taglen = 8;
1523 break;
1524 default:
1525 ret = 1;
1526 goto cleanup;
1527 }
1528 break;
1529
1530 case MBEDTLS_MODE_STREAM:
1531 case MBEDTLS_MODE_CBC:
1532 t_out->fixed_ivlen = 0; /* redundant, must be 0 */
1533 t_in->fixed_ivlen = 0; /* redundant, must be 0 */
1534 t_out->taglen = 0;
1535 t_in->taglen = 0;
1536 switch (tag_mode) {
1537 case 0: /* Full tag */
1538 t_out->maclen = maclen;
1539 t_in->maclen = maclen;
1540 break;
1541 default:
1542 ret = 1;
1543 goto cleanup;
1544 }
1545 break;
1546 default:
1547 ret = 1;
1548 goto cleanup;
1549 break;
1550 }
1551
1552 /* Setup IV's */
1553
1554 memcpy(&t_in->iv_dec, iv_dec, sizeof(iv_dec));
1555 memcpy(&t_in->iv_enc, iv_enc, sizeof(iv_enc));
1556 memcpy(&t_out->iv_dec, iv_enc, sizeof(iv_enc));
1557 memcpy(&t_out->iv_enc, iv_dec, sizeof(iv_dec));
1558
1559#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
1560 /* Add CID */
1561 memcpy(&t_in->in_cid, cid0, cid0_len);
1562 memcpy(&t_in->out_cid, cid1, cid1_len);
Yanray Wanga8f445e2022-11-03 11:51:59 +08001563 t_in->in_cid_len = (uint8_t) cid0_len;
1564 t_in->out_cid_len = (uint8_t) cid1_len;
Yanray Wange6afd912022-10-27 12:11:18 +08001565 memcpy(&t_out->in_cid, cid1, cid1_len);
1566 memcpy(&t_out->out_cid, cid0, cid0_len);
Yanray Wanga8f445e2022-11-03 11:51:59 +08001567 t_out->in_cid_len = (uint8_t) cid1_len;
1568 t_out->out_cid_len = (uint8_t) cid0_len;
Yanray Wange6afd912022-10-27 12:11:18 +08001569#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
1570
1571#if defined(MBEDTLS_USE_PSA_CRYPTO)
1572 status = mbedtls_ssl_cipher_to_psa(cipher_type,
1573 t_in->taglen,
1574 &alg,
1575 &key_type,
1576 &key_bits);
1577
1578 if (status != PSA_SUCCESS) {
1579 ret = PSA_TO_MBEDTLS_ERR(status);
1580 goto cleanup;
1581 }
1582
1583 t_in->psa_alg = alg;
1584 t_out->psa_alg = alg;
1585
1586 if (alg != MBEDTLS_SSL_NULL_CIPHER) {
1587 psa_reset_key_attributes(&attributes);
1588 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
1589 psa_set_key_algorithm(&attributes, alg);
1590 psa_set_key_type(&attributes, key_type);
1591
1592 status = psa_import_key(&attributes,
1593 key0,
1594 PSA_BITS_TO_BYTES(key_bits),
1595 &t_in->psa_key_enc);
1596
1597 if (status != PSA_SUCCESS) {
1598 ret = PSA_TO_MBEDTLS_ERR(status);
1599 goto cleanup;
1600 }
1601
1602 status = psa_import_key(&attributes,
1603 key1,
1604 PSA_BITS_TO_BYTES(key_bits),
1605 &t_out->psa_key_enc);
1606
1607 if (status != PSA_SUCCESS) {
1608 ret = PSA_TO_MBEDTLS_ERR(status);
1609 goto cleanup;
1610 }
1611
1612 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
1613
1614 status = psa_import_key(&attributes,
1615 key1,
1616 PSA_BITS_TO_BYTES(key_bits),
1617 &t_in->psa_key_dec);
1618
1619 if (status != PSA_SUCCESS) {
1620 ret = PSA_TO_MBEDTLS_ERR(status);
1621 goto cleanup;
1622 }
1623
1624 status = psa_import_key(&attributes,
1625 key0,
1626 PSA_BITS_TO_BYTES(key_bits),
1627 &t_out->psa_key_dec);
1628
1629 if (status != PSA_SUCCESS) {
1630 ret = PSA_TO_MBEDTLS_ERR(status);
1631 goto cleanup;
1632 }
1633 }
1634#endif /* MBEDTLS_USE_PSA_CRYPTO */
1635
1636cleanup:
1637
1638 mbedtls_free(key0);
1639 mbedtls_free(key1);
1640
1641 mbedtls_free(md0);
1642 mbedtls_free(md1);
1643
1644 return ret;
1645}
1646
Gilles Peskine9099d3f2023-09-18 13:11:50 +02001647#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
1648int mbedtls_test_ssl_prepare_record_mac(mbedtls_record *record,
1649 mbedtls_ssl_transform *transform_out)
1650{
1651#if defined(MBEDTLS_USE_PSA_CRYPTO)
1652 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1653#endif
1654
1655 /* Serialized version of record header for MAC purposes */
1656 unsigned char add_data[13];
1657 memcpy(add_data, record->ctr, 8);
1658 add_data[8] = record->type;
1659 add_data[9] = record->ver[0];
1660 add_data[10] = record->ver[1];
1661 add_data[11] = (record->data_len >> 8) & 0xff;
1662 add_data[12] = (record->data_len >> 0) & 0xff;
1663
1664 /* MAC with additional data */
1665#if defined(MBEDTLS_USE_PSA_CRYPTO)
1666 size_t sign_mac_length = 0;
1667 TEST_EQUAL(PSA_SUCCESS, psa_mac_sign_setup(&operation,
1668 transform_out->psa_mac_enc,
1669 transform_out->psa_mac_alg));
1670 TEST_EQUAL(PSA_SUCCESS, psa_mac_update(&operation, add_data, 13));
1671 TEST_EQUAL(PSA_SUCCESS, psa_mac_update(&operation,
1672 record->buf + record->data_offset,
1673 record->data_len));
1674 /* Use a temporary buffer for the MAC, because with the truncated HMAC
1675 * extension, there might not be enough room in the record for the
1676 * full-length MAC. */
1677 unsigned char mac[PSA_HASH_MAX_SIZE];
1678 TEST_EQUAL(PSA_SUCCESS, psa_mac_sign_finish(&operation,
1679 mac, sizeof(mac),
1680 &sign_mac_length));
1681#else
1682 TEST_EQUAL(0, mbedtls_md_hmac_update(&transform_out->md_ctx_enc, add_data, 13));
1683 TEST_EQUAL(0, mbedtls_md_hmac_update(&transform_out->md_ctx_enc,
1684 record->buf + record->data_offset,
1685 record->data_len));
1686 /* Use a temporary buffer for the MAC, because with the truncated HMAC
1687 * extension, there might not be enough room in the record for the
1688 * full-length MAC. */
1689 unsigned char mac[MBEDTLS_MD_MAX_SIZE];
1690 TEST_EQUAL(0, mbedtls_md_hmac_finish(&transform_out->md_ctx_enc, mac));
1691#endif
1692 memcpy(record->buf + record->data_offset + record->data_len, mac, transform_out->maclen);
1693 record->data_len += transform_out->maclen;
1694
1695 return 0;
1696
1697exit:
1698#if defined(MBEDTLS_USE_PSA_CRYPTO)
1699 psa_mac_abort(&operation);
1700#endif
1701 return -1;
1702}
1703#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
1704
Jerry Yu28547c42023-10-31 14:42:50 +08001705#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Yanray Wange6afd912022-10-27 12:11:18 +08001706int mbedtls_test_ssl_tls12_populate_session(mbedtls_ssl_session *session,
1707 int ticket_len,
Ronald Cron7b1921a2023-11-23 12:31:56 +01001708 int endpoint_type,
Yanray Wange6afd912022-10-27 12:11:18 +08001709 const char *crt_file)
1710{
Ronald Cronc57f86e2023-11-22 09:50:01 +01001711 (void) ticket_len;
1712
Yanray Wange6afd912022-10-27 12:11:18 +08001713#if defined(MBEDTLS_HAVE_TIME)
1714 session->start = mbedtls_time(NULL) - 42;
1715#endif
1716 session->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Ronald Cronc7fa82e2024-02-09 09:33:09 +01001717
1718 TEST_ASSERT(endpoint_type == MBEDTLS_SSL_IS_CLIENT ||
1719 endpoint_type == MBEDTLS_SSL_IS_SERVER);
1720
1721 session->endpoint = endpoint_type;
Yanray Wange6afd912022-10-27 12:11:18 +08001722 session->ciphersuite = 0xabcd;
1723 session->id_len = sizeof(session->id);
1724 memset(session->id, 66, session->id_len);
1725 memset(session->master, 17, sizeof(session->master));
1726
1727#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) && defined(MBEDTLS_FS_IO)
1728 if (crt_file != NULL && strlen(crt_file) != 0) {
1729 mbedtls_x509_crt tmp_crt;
1730 int ret;
1731
1732 mbedtls_x509_crt_init(&tmp_crt);
1733 ret = mbedtls_x509_crt_parse_file(&tmp_crt, crt_file);
1734 if (ret != 0) {
1735 return ret;
1736 }
1737
1738#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1739 /* Move temporary CRT. */
1740 session->peer_cert = mbedtls_calloc(1, sizeof(*session->peer_cert));
1741 if (session->peer_cert == NULL) {
1742 return -1;
1743 }
1744 *session->peer_cert = tmp_crt;
1745 memset(&tmp_crt, 0, sizeof(tmp_crt));
1746#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
1747 /* Calculate digest of temporary CRT. */
1748 session->peer_cert_digest =
1749 mbedtls_calloc(1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN);
1750 if (session->peer_cert_digest == NULL) {
1751 return -1;
1752 }
1753
1754#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02001755 psa_algorithm_t psa_alg = mbedtls_md_psa_alg_from_type(
Yanray Wange6afd912022-10-27 12:11:18 +08001756 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE);
1757 size_t hash_size = 0;
1758 psa_status_t status = psa_hash_compute(
1759 psa_alg, tmp_crt.raw.p,
1760 tmp_crt.raw.len,
1761 session->peer_cert_digest,
1762 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN,
1763 &hash_size);
1764 ret = PSA_TO_MBEDTLS_ERR(status);
1765#else
1766 ret = mbedtls_md(mbedtls_md_info_from_type(
1767 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE),
1768 tmp_crt.raw.p, tmp_crt.raw.len,
1769 session->peer_cert_digest);
1770#endif /* MBEDTLS_USE_PSA_CRYPTO */
1771 if (ret != 0) {
1772 return ret;
1773 }
1774 session->peer_cert_digest_type =
1775 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
1776 session->peer_cert_digest_len =
1777 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
1778#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
1779
1780 mbedtls_x509_crt_free(&tmp_crt);
1781 }
1782#else /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED && MBEDTLS_FS_IO */
1783 (void) crt_file;
1784#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED && MBEDTLS_FS_IO */
1785 session->verify_result = 0xdeadbeef;
1786
Ronald Cronc57f86e2023-11-22 09:50:01 +01001787#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1788#if defined(MBEDTLS_SSL_CLI_C)
Yanray Wange6afd912022-10-27 12:11:18 +08001789 if (ticket_len != 0) {
1790 session->ticket = mbedtls_calloc(1, ticket_len);
1791 if (session->ticket == NULL) {
1792 return -1;
1793 }
1794 memset(session->ticket, 33, ticket_len);
1795 }
1796 session->ticket_len = ticket_len;
1797 session->ticket_lifetime = 86401;
Ronald Cronc57f86e2023-11-22 09:50:01 +01001798#endif /* MBEDTLS_SSL_CLI_C */
1799
1800#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_HAVE_TIME)
1801 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
1802 session->ticket_creation_time = mbedtls_ms_time() - 42;
1803 }
Yanray Wange6afd912022-10-27 12:11:18 +08001804#endif
Ronald Cronc57f86e2023-11-22 09:50:01 +01001805#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Yanray Wange6afd912022-10-27 12:11:18 +08001806
1807#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1808 session->mfl_code = 1;
1809#endif
1810#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1811 session->encrypt_then_mac = 1;
1812#endif
1813
Ronald Cronc7fa82e2024-02-09 09:33:09 +01001814exit:
Yanray Wange6afd912022-10-27 12:11:18 +08001815 return 0;
1816}
Jerry Yu28547c42023-10-31 14:42:50 +08001817#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Yanray Wange6afd912022-10-27 12:11:18 +08001818
1819#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1820int mbedtls_test_ssl_tls13_populate_session(mbedtls_ssl_session *session,
1821 int ticket_len,
1822 int endpoint_type)
1823{
1824 ((void) ticket_len);
1825 session->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
1826 session->endpoint = endpoint_type == MBEDTLS_SSL_IS_CLIENT ?
1827 MBEDTLS_SSL_IS_CLIENT : MBEDTLS_SSL_IS_SERVER;
1828 session->ciphersuite = 0xabcd;
Ronald Cron18b92a12024-03-26 10:15:08 +01001829
1830#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Yanray Wange6afd912022-10-27 12:11:18 +08001831 session->ticket_age_add = 0x87654321;
1832 session->ticket_flags = 0x7;
Yanray Wange6afd912022-10-27 12:11:18 +08001833 session->resumption_key_len = 32;
1834 memset(session->resumption_key, 0x99, sizeof(session->resumption_key));
Yanray Wange6afd912022-10-27 12:11:18 +08001835#endif
1836
Ronald Cron18b92a12024-03-26 10:15:08 +01001837#if defined(MBEDTLS_SSL_SRV_C)
1838 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
1839#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1840#if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN)
1841 int ret = mbedtls_ssl_session_set_ticket_alpn(session, "ALPNExample");
1842 if (ret != 0) {
1843 return -1;
1844 }
1845#endif
1846#if defined(MBEDTLS_HAVE_TIME)
1847 session->ticket_creation_time = mbedtls_ms_time() - 42;
1848#endif
1849#endif /* MBEDTLS_SSL_SESSION_TICKETS */
1850 }
1851#endif /* MBEDTLS_SSL_SRV_C */
1852
Yanray Wange6afd912022-10-27 12:11:18 +08001853#if defined(MBEDTLS_SSL_CLI_C)
1854 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Ronald Cron18b92a12024-03-26 10:15:08 +01001855#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Yanray Wange6afd912022-10-27 12:11:18 +08001856#if defined(MBEDTLS_HAVE_TIME)
Jerry Yu342a5552023-11-10 14:23:39 +08001857 session->ticket_reception_time = mbedtls_ms_time() - 40;
Yanray Wange6afd912022-10-27 12:11:18 +08001858#endif
1859 session->ticket_lifetime = 0xfedcba98;
1860
1861 session->ticket_len = ticket_len;
1862 if (ticket_len != 0) {
1863 session->ticket = mbedtls_calloc(1, ticket_len);
1864 if (session->ticket == NULL) {
1865 return -1;
1866 }
1867 memset(session->ticket, 33, ticket_len);
1868 }
Ronald Cron8d15e012024-03-27 09:30:13 +01001869#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1870 char hostname[] = "hostname example";
1871 session->hostname = mbedtls_calloc(1, sizeof(hostname));
1872 if (session->hostname == NULL) {
1873 return -1;
1874 }
1875 memcpy(session->hostname, hostname, sizeof(hostname));
1876#endif
Ronald Cron18b92a12024-03-26 10:15:08 +01001877#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Yanray Wange6afd912022-10-27 12:11:18 +08001878 }
1879#endif /* MBEDTLS_SSL_CLI_C */
1880
Ronald Cron18b92a12024-03-26 10:15:08 +01001881#if defined(MBEDTLS_SSL_EARLY_DATA)
1882 session->max_early_data_size = 0x87654321;
1883#endif /* MBEDTLS_SSL_EARLY_DATA */
1884
Waleed Elmelegy049cd302023-12-20 17:28:31 +00001885#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
1886 session->record_size_limit = 2048;
1887#endif
1888
Yanray Wange6afd912022-10-27 12:11:18 +08001889 return 0;
1890}
1891#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1892
Yanray Wangb088bfc2023-03-16 12:15:49 +08001893int mbedtls_test_ssl_exchange_data(
1894 mbedtls_ssl_context *ssl_1,
1895 int msg_len_1, const int expected_fragments_1,
1896 mbedtls_ssl_context *ssl_2,
1897 int msg_len_2, const int expected_fragments_2)
Yanray Wange6afd912022-10-27 12:11:18 +08001898{
1899 unsigned char *msg_buf_1 = malloc(msg_len_1);
1900 unsigned char *msg_buf_2 = malloc(msg_len_2);
1901 unsigned char *in_buf_1 = malloc(msg_len_2);
1902 unsigned char *in_buf_2 = malloc(msg_len_1);
1903 int msg_type, ret = -1;
1904
1905 /* Perform this test with two message types. At first use a message
1906 * consisting of only 0x00 for the client and only 0xFF for the server.
1907 * At the second time use message with generated data */
1908 for (msg_type = 0; msg_type < 2; msg_type++) {
1909 int written_1 = 0;
1910 int written_2 = 0;
1911 int read_1 = 0;
1912 int read_2 = 0;
1913 int fragments_1 = 0;
1914 int fragments_2 = 0;
1915
1916 if (msg_type == 0) {
1917 memset(msg_buf_1, 0x00, msg_len_1);
1918 memset(msg_buf_2, 0xff, msg_len_2);
1919 } else {
1920 int i, j = 0;
1921 for (i = 0; i < msg_len_1; i++) {
1922 msg_buf_1[i] = j++ & 0xFF;
1923 }
1924 for (i = 0; i < msg_len_2; i++) {
1925 msg_buf_2[i] = (j -= 5) & 0xFF;
1926 }
1927 }
1928
1929 while (read_1 < msg_len_2 || read_2 < msg_len_1) {
1930 /* ssl_1 sending */
1931 if (msg_len_1 > written_1) {
1932 ret = mbedtls_ssl_write_fragment(ssl_1, msg_buf_1,
1933 msg_len_1, &written_1,
1934 expected_fragments_1);
1935 if (expected_fragments_1 == 0) {
1936 /* This error is expected when the message is too large and
1937 * cannot be fragmented */
Gilles Peskine353eb332025-05-14 17:42:53 +02001938 TEST_EQUAL(ret, MBEDTLS_ERR_SSL_BAD_INPUT_DATA);
Yanray Wange6afd912022-10-27 12:11:18 +08001939 msg_len_1 = 0;
1940 } else {
Gilles Peskine353eb332025-05-14 17:42:53 +02001941 TEST_EQUAL(ret, 0);
Yanray Wange6afd912022-10-27 12:11:18 +08001942 }
1943 }
1944
1945 /* ssl_2 sending */
1946 if (msg_len_2 > written_2) {
1947 ret = mbedtls_ssl_write_fragment(ssl_2, msg_buf_2,
1948 msg_len_2, &written_2,
1949 expected_fragments_2);
1950 if (expected_fragments_2 == 0) {
1951 /* This error is expected when the message is too large and
1952 * cannot be fragmented */
Gilles Peskine353eb332025-05-14 17:42:53 +02001953 TEST_EQUAL(ret, MBEDTLS_ERR_SSL_BAD_INPUT_DATA);
Yanray Wange6afd912022-10-27 12:11:18 +08001954 msg_len_2 = 0;
1955 } else {
Gilles Peskine353eb332025-05-14 17:42:53 +02001956 TEST_EQUAL(ret, 0);
Yanray Wange6afd912022-10-27 12:11:18 +08001957 }
1958 }
1959
1960 /* ssl_1 reading */
1961 if (read_1 < msg_len_2) {
1962 ret = mbedtls_ssl_read_fragment(ssl_1, in_buf_1,
1963 msg_len_2, &read_1,
1964 &fragments_2,
1965 expected_fragments_2);
Gilles Peskine353eb332025-05-14 17:42:53 +02001966 TEST_EQUAL(ret, 0);
Yanray Wange6afd912022-10-27 12:11:18 +08001967 }
1968
1969 /* ssl_2 reading */
1970 if (read_2 < msg_len_1) {
1971 ret = mbedtls_ssl_read_fragment(ssl_2, in_buf_2,
1972 msg_len_1, &read_2,
1973 &fragments_1,
1974 expected_fragments_1);
Gilles Peskine353eb332025-05-14 17:42:53 +02001975 TEST_EQUAL(ret, 0);
Yanray Wange6afd912022-10-27 12:11:18 +08001976 }
1977 }
1978
1979 ret = -1;
Gilles Peskine353eb332025-05-14 17:42:53 +02001980 TEST_EQUAL(0, memcmp(msg_buf_1, in_buf_2, msg_len_1));
1981 TEST_EQUAL(0, memcmp(msg_buf_2, in_buf_1, msg_len_2));
1982 TEST_EQUAL(fragments_1, expected_fragments_1);
1983 TEST_EQUAL(fragments_2, expected_fragments_2);
Yanray Wange6afd912022-10-27 12:11:18 +08001984 }
1985
1986 ret = 0;
1987
1988exit:
1989 free(msg_buf_1);
1990 free(in_buf_1);
1991 free(msg_buf_2);
1992 free(in_buf_2);
1993
1994 return ret;
1995}
1996
1997/*
1998 * Perform data exchanging between \p ssl_1 and \p ssl_2. Both of endpoints
1999 * must be initialized and connected beforehand.
2000 *
2001 * \retval 0 on success, otherwise error code.
2002 */
Yanray Wangead70c82023-03-16 12:04:49 +08002003#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) && \
2004 (defined(MBEDTLS_SSL_RENEGOTIATION) || \
2005 defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH))
2006static int exchange_data(mbedtls_ssl_context *ssl_1,
2007 mbedtls_ssl_context *ssl_2)
Yanray Wange6afd912022-10-27 12:11:18 +08002008{
Yanray Wangb088bfc2023-03-16 12:15:49 +08002009 return mbedtls_test_ssl_exchange_data(ssl_1, 256, 1,
2010 ssl_2, 256, 1);
Yanray Wange6afd912022-10-27 12:11:18 +08002011}
Yanray Wangead70c82023-03-16 12:04:49 +08002012#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED &&
2013 (MBEDTLS_SSL_RENEGOTIATION ||
2014 MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) */
Yanray Wange6afd912022-10-27 12:11:18 +08002015
2016#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
2017static int check_ssl_version(
2018 mbedtls_ssl_protocol_version expected_negotiated_version,
2019 const mbedtls_ssl_context *ssl)
2020{
2021 const char *version_string = mbedtls_ssl_get_version(ssl);
2022 mbedtls_ssl_protocol_version version_number =
2023 mbedtls_ssl_get_version_number(ssl);
2024
2025 TEST_EQUAL(ssl->tls_version, expected_negotiated_version);
2026
2027 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
2028 TEST_EQUAL(version_string[0], 'D');
2029 ++version_string;
2030 }
2031
2032 switch (expected_negotiated_version) {
2033 case MBEDTLS_SSL_VERSION_TLS1_2:
2034 TEST_EQUAL(version_number, MBEDTLS_SSL_VERSION_TLS1_2);
Gilles Peskine353eb332025-05-14 17:42:53 +02002035 TEST_EQUAL(strcmp(version_string, "TLSv1.2"), 0);
Yanray Wange6afd912022-10-27 12:11:18 +08002036 break;
2037
2038 case MBEDTLS_SSL_VERSION_TLS1_3:
2039 TEST_EQUAL(version_number, MBEDTLS_SSL_VERSION_TLS1_3);
Gilles Peskine353eb332025-05-14 17:42:53 +02002040 TEST_EQUAL(strcmp(version_string, "TLSv1.3"), 0);
Yanray Wange6afd912022-10-27 12:11:18 +08002041 break;
2042
2043 default:
Agathiyan Bragadeeshdc28a5a2023-07-18 11:45:28 +01002044 TEST_FAIL(
Agathiyan Bragadeeshebb40bc2023-07-14 17:28:27 +01002045 "Version check not implemented for this protocol version");
Yanray Wange6afd912022-10-27 12:11:18 +08002046 }
2047
2048 return 1;
2049
2050exit:
2051 return 0;
2052}
2053#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
2054
Yanray Wange6afd912022-10-27 12:11:18 +08002055#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Max Fillingercf007ca2024-10-29 16:57:09 +01002056int mbedtls_test_ssl_do_handshake_with_endpoints(
2057 mbedtls_test_ssl_endpoint *server_ep,
2058 mbedtls_test_ssl_endpoint *client_ep,
Max Fillinger8f12e312024-10-30 00:29:37 +01002059 mbedtls_test_handshake_test_options *options,
Max Fillingercf007ca2024-10-29 16:57:09 +01002060 mbedtls_ssl_protocol_version proto)
2061{
2062 enum { BUFFSIZE = 1024 };
2063
2064 int ret = -1;
Max Fillingercf007ca2024-10-29 16:57:09 +01002065
Max Fillingeree467aa2024-11-08 22:17:33 +01002066 mbedtls_platform_zeroize(server_ep, sizeof(mbedtls_test_ssl_endpoint));
2067 mbedtls_platform_zeroize(client_ep, sizeof(mbedtls_test_ssl_endpoint));
2068
Max Fillinger8f12e312024-10-30 00:29:37 +01002069 mbedtls_test_init_handshake_options(options);
2070 options->server_min_version = proto;
2071 options->client_min_version = proto;
2072 options->server_max_version = proto;
2073 options->client_max_version = proto;
Max Fillingercf007ca2024-10-29 16:57:09 +01002074
Gilles Peskineca8a9ac2025-05-27 20:52:24 +02002075 ret = mbedtls_test_ssl_endpoint_init(client_ep, MBEDTLS_SSL_IS_CLIENT, options);
Max Fillingercf007ca2024-10-29 16:57:09 +01002076 if (ret != 0) {
2077 return ret;
2078 }
Gilles Peskineca8a9ac2025-05-27 20:52:24 +02002079 ret = mbedtls_test_ssl_endpoint_init(server_ep, MBEDTLS_SSL_IS_SERVER, options);
Max Fillingercf007ca2024-10-29 16:57:09 +01002080 if (ret != 0) {
2081 return ret;
2082 }
2083
2084 ret = mbedtls_test_mock_socket_connect(&client_ep->socket, &server_ep->socket, BUFFSIZE);
2085 if (ret != 0) {
2086 return ret;
2087 }
2088
Max Fillingerea1e7772024-10-30 00:49:10 +01002089 ret = mbedtls_test_move_handshake_to_state(&server_ep->ssl,
2090 &client_ep->ssl,
2091 MBEDTLS_SSL_HANDSHAKE_OVER);
Max Fillingercf007ca2024-10-29 16:57:09 +01002092 if (ret != 0 && ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
2093 return ret;
2094 }
Max Fillingerea1e7772024-10-30 00:49:10 +01002095 ret = mbedtls_test_move_handshake_to_state(&client_ep->ssl,
2096 &server_ep->ssl,
2097 MBEDTLS_SSL_HANDSHAKE_OVER);
Max Fillingercf007ca2024-10-29 16:57:09 +01002098 if (ret != 0 && ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
2099 return ret;
2100 }
Max Fillingerea1e7772024-10-30 00:49:10 +01002101 if (!mbedtls_ssl_is_handshake_over(&client_ep->ssl) ||
2102 !mbedtls_ssl_is_handshake_over(&server_ep->ssl)) {
Max Fillingercf007ca2024-10-29 16:57:09 +01002103 return -1;
2104 }
2105
2106 return 0;
2107}
2108#endif /* defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) */
2109
2110#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Yanray Wange6afd912022-10-27 12:11:18 +08002111void mbedtls_test_ssl_perform_handshake(
Gilles Peskine9b993682025-05-27 18:44:12 +02002112 const mbedtls_test_handshake_test_options *options)
Yanray Wange6afd912022-10-27 12:11:18 +08002113{
Yanray Wange6afd912022-10-27 12:11:18 +08002114 enum { BUFFSIZE = 17000 };
2115 mbedtls_test_ssl_endpoint client, server;
2116#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
2117 const char *psk_identity = "foo";
2118#endif
Yanray Wange6afd912022-10-27 12:11:18 +08002119#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
2120 unsigned char *context_buf = NULL;
2121 size_t context_buf_len;
2122#endif
2123#if defined(MBEDTLS_SSL_RENEGOTIATION)
2124 int ret = -1;
2125#endif
2126 int expected_handshake_result = options->expected_handshake_result;
2127
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +01002128 MD_OR_USE_PSA_INIT();
Yanray Wange6afd912022-10-27 12:11:18 +08002129 mbedtls_platform_zeroize(&client, sizeof(client));
2130 mbedtls_platform_zeroize(&server, sizeof(server));
Yanray Wange6afd912022-10-27 12:11:18 +08002131
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 */
Gilles Peskine07432b92025-05-27 21:07:44 +02002139 TEST_EQUAL(mbedtls_test_ssl_endpoint_init(&client,
2140 MBEDTLS_SSL_IS_CLIENT,
2141 options), 0);
Gilles Peskine2744a432025-05-27 13:27:22 +02002142 TEST_ASSERT(set_ciphersuite(&client, options->cipher));
Yanray Wange6afd912022-10-27 12:11:18 +08002143
Yanray Wange6afd912022-10-27 12:11:18 +08002144 /* Server side */
Gilles Peskine07432b92025-05-27 21:07:44 +02002145 TEST_EQUAL(mbedtls_test_ssl_endpoint_init(&server,
2146 MBEDTLS_SSL_IS_SERVER,
2147 options), 0);
Yanray Wange6afd912022-10-27 12:11:18 +08002148 mbedtls_ssl_conf_authmode(&server.conf, options->srv_auth_mode);
2149
Gilles Peskineb092e782025-05-27 20:15:03 +02002150 if (options->dtls) {
2151 TEST_EQUAL(mbedtls_test_ssl_dtls_join_endpoints(&client, &server), 0);
2152 }
2153
Yanray Wange6afd912022-10-27 12:11:18 +08002154#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine353eb332025-05-14 17:42:53 +02002155 TEST_EQUAL(mbedtls_ssl_conf_max_frag_len(&(server.conf),
2156 (unsigned char) options->mfl),
2157 0);
2158 TEST_EQUAL(mbedtls_ssl_conf_max_frag_len(&(client.conf),
2159 (unsigned char) options->mfl),
2160 0);
Yanray Wange6afd912022-10-27 12:11:18 +08002161#else
Gilles Peskine353eb332025-05-14 17:42:53 +02002162 TEST_EQUAL(MBEDTLS_SSL_MAX_FRAG_LEN_NONE, options->mfl);
Yanray Wange6afd912022-10-27 12:11:18 +08002163#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2164
2165#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
2166 if (options->psk_str != NULL && options->psk_str->len > 0) {
Gilles Peskine353eb332025-05-14 17:42:53 +02002167 TEST_EQUAL(mbedtls_ssl_conf_psk(
2168 &client.conf, options->psk_str->x,
2169 options->psk_str->len,
2170 (const unsigned char *) psk_identity,
2171 strlen(psk_identity)), 0);
Yanray Wange6afd912022-10-27 12:11:18 +08002172
Gilles Peskine353eb332025-05-14 17:42:53 +02002173 TEST_EQUAL(mbedtls_ssl_conf_psk(
2174 &server.conf, options->psk_str->x,
2175 options->psk_str->len,
2176 (const unsigned char *) psk_identity,
2177 strlen(psk_identity)), 0);
Yanray Wange6afd912022-10-27 12:11:18 +08002178#if defined(MBEDTLS_SSL_SRV_C)
2179 mbedtls_ssl_conf_psk_cb(&server.conf, psk_dummy_callback, NULL);
2180#endif
2181 }
2182#endif
2183#if defined(MBEDTLS_SSL_RENEGOTIATION)
2184 if (options->renegotiate) {
2185 mbedtls_ssl_conf_renegotiation(&(server.conf),
2186 MBEDTLS_SSL_RENEGOTIATION_ENABLED);
2187 mbedtls_ssl_conf_renegotiation(&(client.conf),
2188 MBEDTLS_SSL_RENEGOTIATION_ENABLED);
2189
2190 mbedtls_ssl_conf_legacy_renegotiation(&(server.conf),
2191 options->legacy_renegotiation);
2192 mbedtls_ssl_conf_legacy_renegotiation(&(client.conf),
2193 options->legacy_renegotiation);
2194 }
2195#endif /* MBEDTLS_SSL_RENEGOTIATION */
2196
Gilles Peskine353eb332025-05-14 17:42:53 +02002197 TEST_EQUAL(mbedtls_test_mock_socket_connect(&(client.socket),
2198 &(server.socket),
2199 BUFFSIZE), 0);
Yanray Wange6afd912022-10-27 12:11:18 +08002200
2201#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2202 if (options->resize_buffers != 0) {
2203 /* Ensure that the buffer sizes are appropriate before resizes */
Gilles Peskine353eb332025-05-14 17:42:53 +02002204 TEST_EQUAL(client.ssl.out_buf_len, MBEDTLS_SSL_OUT_BUFFER_LEN);
2205 TEST_EQUAL(client.ssl.in_buf_len, MBEDTLS_SSL_IN_BUFFER_LEN);
2206 TEST_EQUAL(server.ssl.out_buf_len, MBEDTLS_SSL_OUT_BUFFER_LEN);
2207 TEST_EQUAL(server.ssl.in_buf_len, MBEDTLS_SSL_IN_BUFFER_LEN);
Yanray Wange6afd912022-10-27 12:11:18 +08002208 }
2209#endif
2210
2211 if (options->expected_negotiated_version == MBEDTLS_SSL_VERSION_UNKNOWN) {
2212 expected_handshake_result = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
2213 }
2214
Gilles Peskine353eb332025-05-14 17:42:53 +02002215 TEST_EQUAL(mbedtls_test_move_handshake_to_state(&(client.ssl),
2216 &(server.ssl),
2217 MBEDTLS_SSL_HANDSHAKE_OVER),
2218 expected_handshake_result);
Yanray Wange6afd912022-10-27 12:11:18 +08002219
2220 if (expected_handshake_result != 0) {
2221 /* Connection will have failed by this point, skip to cleanup */
2222 goto exit;
2223 }
2224
Gilles Peskine353eb332025-05-14 17:42:53 +02002225 TEST_EQUAL(mbedtls_ssl_is_handshake_over(&client.ssl), 1);
Yanray Wange6afd912022-10-27 12:11:18 +08002226
2227 /* Make sure server state is moved to HANDSHAKE_OVER also. */
2228 TEST_EQUAL(mbedtls_test_move_handshake_to_state(&(server.ssl),
2229 &(client.ssl),
2230 MBEDTLS_SSL_HANDSHAKE_OVER),
2231 0);
2232
Gilles Peskine353eb332025-05-14 17:42:53 +02002233 TEST_EQUAL(mbedtls_ssl_is_handshake_over(&server.ssl), 1);
Yanray Wange6afd912022-10-27 12:11:18 +08002234 /* Check that both sides have negotiated the expected version. */
2235 mbedtls_test_set_step(0);
2236 if (!check_ssl_version(options->expected_negotiated_version,
2237 &client.ssl)) {
2238 goto exit;
2239 }
2240
2241 mbedtls_test_set_step(1);
2242 if (!check_ssl_version(options->expected_negotiated_version,
2243 &server.ssl)) {
2244 goto exit;
2245 }
2246
2247 if (options->expected_ciphersuite != 0) {
2248 TEST_EQUAL(server.ssl.session->ciphersuite,
2249 options->expected_ciphersuite);
2250 }
2251
2252#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2253 if (options->resize_buffers != 0) {
2254 /* A server, when using DTLS, might delay a buffer resize to happen
2255 * after it receives a message, so we force it. */
Gilles Peskine353eb332025-05-14 17:42:53 +02002256 TEST_EQUAL(exchange_data(&(client.ssl), &(server.ssl)), 0);
Yanray Wange6afd912022-10-27 12:11:18 +08002257
Gilles Peskine353eb332025-05-14 17:42:53 +02002258 TEST_EQUAL(client.ssl.out_buf_len,
2259 mbedtls_ssl_get_output_buflen(&client.ssl));
2260 TEST_EQUAL(client.ssl.in_buf_len,
2261 mbedtls_ssl_get_input_buflen(&client.ssl));
2262 TEST_EQUAL(server.ssl.out_buf_len,
2263 mbedtls_ssl_get_output_buflen(&server.ssl));
2264 TEST_EQUAL(server.ssl.in_buf_len,
2265 mbedtls_ssl_get_input_buflen(&server.ssl));
Yanray Wange6afd912022-10-27 12:11:18 +08002266 }
2267#endif
2268
2269 if (options->cli_msg_len != 0 || options->srv_msg_len != 0) {
2270 /* Start data exchanging test */
Gilles Peskine353eb332025-05-14 17:42:53 +02002271 TEST_EQUAL(mbedtls_test_ssl_exchange_data(
2272 &(client.ssl), options->cli_msg_len,
2273 options->expected_cli_fragments,
2274 &(server.ssl), options->srv_msg_len,
2275 options->expected_srv_fragments),
2276 0);
Yanray Wange6afd912022-10-27 12:11:18 +08002277 }
2278#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
2279 if (options->serialize == 1) {
Gilles Peskine353eb332025-05-14 17:42:53 +02002280 TEST_EQUAL(options->dtls, 1);
Yanray Wange6afd912022-10-27 12:11:18 +08002281
Gilles Peskine353eb332025-05-14 17:42:53 +02002282 TEST_EQUAL(mbedtls_ssl_context_save(&(server.ssl), NULL,
2283 0, &context_buf_len),
2284 MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL);
Yanray Wange6afd912022-10-27 12:11:18 +08002285
2286 context_buf = mbedtls_calloc(1, context_buf_len);
2287 TEST_ASSERT(context_buf != NULL);
2288
Gilles Peskine353eb332025-05-14 17:42:53 +02002289 TEST_EQUAL(mbedtls_ssl_context_save(&(server.ssl), context_buf,
2290 context_buf_len,
2291 &context_buf_len),
2292 0);
Yanray Wange6afd912022-10-27 12:11:18 +08002293
2294 mbedtls_ssl_free(&(server.ssl));
2295 mbedtls_ssl_init(&(server.ssl));
2296
Gilles Peskine353eb332025-05-14 17:42:53 +02002297 TEST_EQUAL(mbedtls_ssl_setup(&(server.ssl), &(server.conf)), 0);
Yanray Wange6afd912022-10-27 12:11:18 +08002298
Gilles Peskine29969592025-05-27 19:24:28 +02002299 mbedtls_ssl_set_bio(&(server.ssl), &server.dtls_context,
Yanray Wange6afd912022-10-27 12:11:18 +08002300 mbedtls_test_mock_tcp_send_msg,
2301 mbedtls_test_mock_tcp_recv_msg,
2302 NULL);
2303
2304 mbedtls_ssl_set_user_data_p(&server.ssl, &server);
2305
2306#if defined(MBEDTLS_TIMING_C)
Gilles Peskine0677e022025-05-27 18:05:20 +02002307 mbedtls_ssl_set_timer_cb(&server.ssl, &server.timer,
Yanray Wange6afd912022-10-27 12:11:18 +08002308 mbedtls_timing_set_delay,
2309 mbedtls_timing_get_delay);
2310#endif
2311#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2312 if (options->resize_buffers != 0) {
2313 /* Ensure that the buffer sizes are appropriate before resizes */
Gilles Peskine353eb332025-05-14 17:42:53 +02002314 TEST_EQUAL(server.ssl.out_buf_len, MBEDTLS_SSL_OUT_BUFFER_LEN);
2315 TEST_EQUAL(server.ssl.in_buf_len, MBEDTLS_SSL_IN_BUFFER_LEN);
Yanray Wange6afd912022-10-27 12:11:18 +08002316 }
2317#endif
Gilles Peskine353eb332025-05-14 17:42:53 +02002318 TEST_EQUAL(mbedtls_ssl_context_load(&(server.ssl), context_buf,
2319 context_buf_len), 0);
Yanray Wange6afd912022-10-27 12:11:18 +08002320
2321#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2322 /* Validate buffer sizes after context deserialization */
2323 if (options->resize_buffers != 0) {
Gilles Peskine353eb332025-05-14 17:42:53 +02002324 TEST_EQUAL(server.ssl.out_buf_len,
2325 mbedtls_ssl_get_output_buflen(&server.ssl));
2326 TEST_EQUAL(server.ssl.in_buf_len,
2327 mbedtls_ssl_get_input_buflen(&server.ssl));
Yanray Wange6afd912022-10-27 12:11:18 +08002328 }
2329#endif
2330 /* Retest writing/reading */
2331 if (options->cli_msg_len != 0 || options->srv_msg_len != 0) {
Gilles Peskine353eb332025-05-14 17:42:53 +02002332 TEST_EQUAL(mbedtls_test_ssl_exchange_data(
2333 &(client.ssl), options->cli_msg_len,
2334 options->expected_cli_fragments,
2335 &(server.ssl), options->srv_msg_len,
2336 options->expected_srv_fragments),
2337 0);
Yanray Wange6afd912022-10-27 12:11:18 +08002338 }
2339 }
2340#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
2341
2342#if defined(MBEDTLS_SSL_RENEGOTIATION)
2343 if (options->renegotiate) {
2344 /* Start test with renegotiation */
Gilles Peskine353eb332025-05-14 17:42:53 +02002345 TEST_EQUAL(server.ssl.renego_status,
2346 MBEDTLS_SSL_INITIAL_HANDSHAKE);
2347 TEST_EQUAL(client.ssl.renego_status,
2348 MBEDTLS_SSL_INITIAL_HANDSHAKE);
Yanray Wange6afd912022-10-27 12:11:18 +08002349
2350 /* After calling this function for the server, it only sends a handshake
2351 * request. All renegotiation should happen during data exchanging */
Gilles Peskine353eb332025-05-14 17:42:53 +02002352 TEST_EQUAL(mbedtls_ssl_renegotiate(&(server.ssl)), 0);
2353 TEST_EQUAL(server.ssl.renego_status,
2354 MBEDTLS_SSL_RENEGOTIATION_PENDING);
2355 TEST_EQUAL(client.ssl.renego_status,
2356 MBEDTLS_SSL_INITIAL_HANDSHAKE);
Yanray Wange6afd912022-10-27 12:11:18 +08002357
Gilles Peskine353eb332025-05-14 17:42:53 +02002358 TEST_EQUAL(exchange_data(&(client.ssl), &(server.ssl)), 0);
2359 TEST_EQUAL(server.ssl.renego_status,
2360 MBEDTLS_SSL_RENEGOTIATION_DONE);
2361 TEST_EQUAL(client.ssl.renego_status,
2362 MBEDTLS_SSL_RENEGOTIATION_DONE);
Yanray Wange6afd912022-10-27 12:11:18 +08002363
2364 /* After calling mbedtls_ssl_renegotiate for the client,
2365 * all renegotiation should happen inside this function.
2366 * However in this test, we cannot perform simultaneous communication
2367 * between client and server so this function will return waiting error
2368 * on the socket. All rest of renegotiation should happen
2369 * during data exchanging */
2370 ret = mbedtls_ssl_renegotiate(&(client.ssl));
2371#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2372 if (options->resize_buffers != 0) {
2373 /* Ensure that the buffer sizes are appropriate before resizes */
Gilles Peskine353eb332025-05-14 17:42:53 +02002374 TEST_EQUAL(client.ssl.out_buf_len, MBEDTLS_SSL_OUT_BUFFER_LEN);
2375 TEST_EQUAL(client.ssl.in_buf_len, MBEDTLS_SSL_IN_BUFFER_LEN);
Yanray Wange6afd912022-10-27 12:11:18 +08002376 }
2377#endif
2378 TEST_ASSERT(ret == 0 ||
2379 ret == MBEDTLS_ERR_SSL_WANT_READ ||
2380 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
Gilles Peskine353eb332025-05-14 17:42:53 +02002381 TEST_EQUAL(server.ssl.renego_status,
2382 MBEDTLS_SSL_RENEGOTIATION_DONE);
2383 TEST_EQUAL(client.ssl.renego_status,
2384 MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS);
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#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2392 /* Validate buffer sizes after renegotiation */
2393 if (options->resize_buffers != 0) {
Gilles Peskine353eb332025-05-14 17:42:53 +02002394 TEST_EQUAL(client.ssl.out_buf_len,
2395 mbedtls_ssl_get_output_buflen(&client.ssl));
2396 TEST_EQUAL(client.ssl.in_buf_len,
2397 mbedtls_ssl_get_input_buflen(&client.ssl));
2398 TEST_EQUAL(server.ssl.out_buf_len,
2399 mbedtls_ssl_get_output_buflen(&server.ssl));
2400 TEST_EQUAL(server.ssl.in_buf_len,
2401 mbedtls_ssl_get_input_buflen(&server.ssl));
Yanray Wange6afd912022-10-27 12:11:18 +08002402 }
2403#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
2404 }
2405#endif /* MBEDTLS_SSL_RENEGOTIATION */
2406
2407 TEST_ASSERT(mbedtls_ssl_conf_get_user_data_p(&client.conf) == &client);
2408 TEST_ASSERT(mbedtls_ssl_get_user_data_p(&client.ssl) == &client);
2409 TEST_ASSERT(mbedtls_ssl_conf_get_user_data_p(&server.conf) == &server);
2410 TEST_ASSERT(mbedtls_ssl_get_user_data_p(&server.ssl) == &server);
2411
2412exit:
Gilles Peskineca8a9ac2025-05-27 20:52:24 +02002413 mbedtls_test_ssl_endpoint_free(&client);
2414 mbedtls_test_ssl_endpoint_free(&server);
Yanray Wange6afd912022-10-27 12:11:18 +08002415#if defined(MBEDTLS_DEBUG_C)
2416 if (options->cli_log_fun || options->srv_log_fun) {
2417 mbedtls_debug_set_threshold(0);
2418 }
2419#endif
2420#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
2421 if (context_buf != NULL) {
2422 mbedtls_free(context_buf);
2423 }
2424#endif
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +01002425 MD_OR_USE_PSA_DONE();
Yanray Wange6afd912022-10-27 12:11:18 +08002426}
2427#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
2428
2429#if defined(MBEDTLS_TEST_HOOKS)
Yanray Wangf56181a2023-03-16 12:21:33 +08002430int mbedtls_test_tweak_tls13_certificate_msg_vector_len(
Yanray Wange6afd912022-10-27 12:11:18 +08002431 unsigned char *buf, unsigned char **end, int tweak,
2432 int *expected_result, mbedtls_ssl_chk_buf_ptr_args *args)
2433{
2434/*
2435 * The definition of the tweaks assume that the certificate list contains only
2436 * one certificate.
2437 */
2438
2439/*
2440 * struct {
2441 * opaque cert_data<1..2^24-1>;
2442 * Extension extensions<0..2^16-1>;
2443 * } CertificateEntry;
2444 *
2445 * struct {
2446 * opaque certificate_request_context<0..2^8-1>;
2447 * CertificateEntry certificate_list<0..2^24-1>;
2448 * } Certificate;
2449 */
2450 unsigned char *p_certificate_request_context_len = buf;
2451 size_t certificate_request_context_len = buf[0];
2452
2453 unsigned char *p_certificate_list_len =
2454 buf + 1 + certificate_request_context_len;
2455 unsigned char *certificate_list = p_certificate_list_len + 3;
2456 size_t certificate_list_len =
2457 MBEDTLS_GET_UINT24_BE(p_certificate_list_len, 0);
2458
2459 unsigned char *p_cert_data_len = certificate_list;
2460 unsigned char *cert_data = p_cert_data_len + 3;
2461 size_t cert_data_len = MBEDTLS_GET_UINT24_BE(p_cert_data_len, 0);
2462
2463 unsigned char *p_extensions_len = cert_data + cert_data_len;
2464 unsigned char *extensions = p_extensions_len + 2;
2465 size_t extensions_len = MBEDTLS_GET_UINT16_BE(p_extensions_len, 0);
2466
2467 *expected_result = MBEDTLS_ERR_SSL_DECODE_ERROR;
2468
2469 switch (tweak) {
2470 case 1:
2471 /* Failure when checking if the certificate request context length
2472 * and certificate list length can be read
2473 */
2474 *end = buf + 3;
2475 set_chk_buf_ptr_args(args, buf, *end, 4);
2476 break;
2477
2478 case 2:
2479 /* Invalid certificate request context length.
2480 */
2481 *p_certificate_request_context_len =
Yanray Wanga8f445e2022-11-03 11:51:59 +08002482 (unsigned char) certificate_request_context_len + 1;
Yanray Wange6afd912022-10-27 12:11:18 +08002483 reset_chk_buf_ptr_args(args);
2484 break;
2485
2486 case 3:
2487 /* Failure when checking if certificate_list data can be read. */
2488 MBEDTLS_PUT_UINT24_BE(certificate_list_len + 1,
2489 p_certificate_list_len, 0);
2490 set_chk_buf_ptr_args(args, certificate_list, *end,
2491 certificate_list_len + 1);
2492 break;
2493
2494 case 4:
2495 /* Failure when checking if the cert_data length can be read. */
2496 MBEDTLS_PUT_UINT24_BE(2, p_certificate_list_len, 0);
2497 set_chk_buf_ptr_args(args, p_cert_data_len, certificate_list + 2, 3);
2498 break;
2499
2500 case 5:
2501 /* Failure when checking if cert_data data can be read. */
2502 MBEDTLS_PUT_UINT24_BE(certificate_list_len - 3 + 1,
2503 p_cert_data_len, 0);
2504 set_chk_buf_ptr_args(args, cert_data,
2505 certificate_list + certificate_list_len,
2506 certificate_list_len - 3 + 1);
2507 break;
2508
2509 case 6:
2510 /* Failure when checking if the extensions length can be read. */
2511 MBEDTLS_PUT_UINT24_BE(certificate_list_len - extensions_len - 1,
2512 p_certificate_list_len, 0);
2513 set_chk_buf_ptr_args(
2514 args, p_extensions_len,
2515 certificate_list + certificate_list_len - extensions_len - 1, 2);
2516 break;
2517
2518 case 7:
2519 /* Failure when checking if extensions data can be read. */
2520 MBEDTLS_PUT_UINT16_BE(extensions_len + 1, p_extensions_len, 0);
2521
2522 set_chk_buf_ptr_args(
2523 args, extensions,
2524 certificate_list + certificate_list_len, extensions_len + 1);
2525 break;
2526
2527 default:
2528 return -1;
2529 }
2530
2531 return 0;
2532}
2533#endif /* MBEDTLS_TEST_HOOKS */
Ronald Cron77abfe62024-01-15 11:17:31 +01002534
Ronald Cronfaf026c2024-01-31 14:32:06 +01002535/*
2536 * Functions for tests based on tickets. Implementations of the
2537 * write/parse ticket interfaces as defined by mbedtls_ssl_ticket_write/parse_t.
2538 * Basically same implementations as in ticket.c without the encryption. That
2539 * way we can tweak easily tickets characteristics to simulate misbehaving
2540 * peers.
2541 */
Ronald Cron77abfe62024-01-15 11:17:31 +01002542#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2543int mbedtls_test_ticket_write(
2544 void *p_ticket, const mbedtls_ssl_session *session,
2545 unsigned char *start, const unsigned char *end,
2546 size_t *tlen, uint32_t *lifetime)
2547{
2548 int ret;
2549 ((void) p_ticket);
2550
2551 if ((ret = mbedtls_ssl_session_save(session, start, end - start,
2552 tlen)) != 0) {
2553 return ret;
2554 }
2555
2556 /* Maximum ticket lifetime as defined in RFC 8446 */
2557 *lifetime = 7 * 24 * 3600;
2558
2559 return 0;
2560}
2561
2562int mbedtls_test_ticket_parse(void *p_ticket, mbedtls_ssl_session *session,
2563 unsigned char *buf, size_t len)
2564{
2565 ((void) p_ticket);
2566
2567 return mbedtls_ssl_session_load(session, buf, len);
2568}
2569#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Ronald Cron1f6e4e42024-01-26 16:31:33 +01002570
2571#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SRV_C) && \
2572 defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS) && \
2573 defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
2574int mbedtls_test_get_tls13_ticket(
2575 mbedtls_test_handshake_test_options *client_options,
2576 mbedtls_test_handshake_test_options *server_options,
2577 mbedtls_ssl_session *session)
2578{
2579 int ret = -1;
Gilles Peskine946bf142025-04-08 09:48:40 +02002580 int ok = 0;
Ronald Cron1f6e4e42024-01-26 16:31:33 +01002581 unsigned char buf[64];
2582 mbedtls_test_ssl_endpoint client_ep, server_ep;
2583
2584 mbedtls_platform_zeroize(&client_ep, sizeof(client_ep));
2585 mbedtls_platform_zeroize(&server_ep, sizeof(server_ep));
2586
2587 ret = mbedtls_test_ssl_endpoint_init(&client_ep, MBEDTLS_SSL_IS_CLIENT,
Gilles Peskineca8a9ac2025-05-27 20:52:24 +02002588 client_options);
Ronald Cron1f6e4e42024-01-26 16:31:33 +01002589 TEST_EQUAL(ret, 0);
2590
2591 ret = mbedtls_test_ssl_endpoint_init(&server_ep, MBEDTLS_SSL_IS_SERVER,
Gilles Peskineca8a9ac2025-05-27 20:52:24 +02002592 server_options);
Ronald Cron1f6e4e42024-01-26 16:31:33 +01002593 TEST_EQUAL(ret, 0);
2594
2595 mbedtls_ssl_conf_session_tickets_cb(&server_ep.conf,
2596 mbedtls_test_ticket_write,
2597 mbedtls_test_ticket_parse,
2598 NULL);
2599
2600 ret = mbedtls_test_mock_socket_connect(&(client_ep.socket),
2601 &(server_ep.socket), 1024);
2602 TEST_EQUAL(ret, 0);
2603
2604 TEST_EQUAL(mbedtls_test_move_handshake_to_state(
2605 &(server_ep.ssl), &(client_ep.ssl),
2606 MBEDTLS_SSL_HANDSHAKE_OVER), 0);
2607
2608 TEST_EQUAL(server_ep.ssl.handshake->new_session_tickets_count, 0);
2609
2610 do {
2611 ret = mbedtls_ssl_read(&(client_ep.ssl), buf, sizeof(buf));
2612 } while (ret != MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET);
2613
2614 ret = mbedtls_ssl_get_session(&(client_ep.ssl), session);
2615 TEST_EQUAL(ret, 0);
2616
Gilles Peskine946bf142025-04-08 09:48:40 +02002617 ok = 1;
2618
Ronald Cron1f6e4e42024-01-26 16:31:33 +01002619exit:
Gilles Peskineca8a9ac2025-05-27 20:52:24 +02002620 mbedtls_test_ssl_endpoint_free(&client_ep);
2621 mbedtls_test_ssl_endpoint_free(&server_ep);
Ronald Cron1f6e4e42024-01-26 16:31:33 +01002622
Gilles Peskine946bf142025-04-08 09:48:40 +02002623 if (ret == 0 && !ok) {
2624 /* Exiting due to a test assertion that isn't ret == 0 */
2625 ret = -1;
2626 }
Ronald Cron1f6e4e42024-01-26 16:31:33 +01002627 return ret;
2628}
2629#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SRV_C &&
2630 MBEDTLS_SSL_PROTO_TLS1_3 && MBEDTLS_SSL_SESSION_TICKETS &&
2631 MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
2632
Yanray Wang4d07d1c2022-10-27 15:28:16 +08002633#endif /* MBEDTLS_SSL_TLS_C */