blob: 91efd1c813eee9c9d3d5ac350390c9962cc2d216 [file] [log] [blame]
Yanray Wang47907a42022-10-24 14:42:01 +08001/** \file ssl_helpers.c
2 *
3 * \brief Helper functions to set up a TLS connection.
4 */
5
6/*
7 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00008 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Yanray Wang47907a42022-10-24 14:42:01 +08009 */
10
11#include <test/ssl_helpers.h>
Valerio Setti384fbde2024-01-02 13:26:40 +010012#include "mbedtls/psa_util.h"
Yanray Wange6afd912022-10-27 12:11:18 +080013
Yanray Wang4d07d1c2022-10-27 15:28:16 +080014#if defined(MBEDTLS_SSL_TLS_C)
Ronald Cron10b040f2024-02-05 09:38:09 +010015int mbedtls_test_random(void *p_rng, unsigned char *output, size_t output_len)
Yanray Wange6afd912022-10-27 12:11:18 +080016{
17 (void) p_rng;
18 for (size_t i = 0; i < output_len; i++) {
19 output[i] = rand();
20 }
21
22 return 0;
23}
Yanray Wange6afd912022-10-27 12:11:18 +080024
Yanray Wange6afd912022-10-27 12:11:18 +080025void mbedtls_test_ssl_log_analyzer(void *ctx, int level,
26 const char *file, int line,
27 const char *str)
28{
29 mbedtls_test_ssl_log_pattern *p = (mbedtls_test_ssl_log_pattern *) ctx;
30
Manuel Pégourié-Gonnard6637ef72025-02-11 13:19:45 +010031/* Change 0 to 1 for debugging of test cases that use this function. */
32#if 0
33 const char *q, *basename;
34 /* Extract basename from file */
35 for (q = basename = file; *q != '\0'; q++) {
36 if (*q == '/' || *q == '\\') {
37 basename = q + 1;
38 }
39 }
40 printf("%s:%04d: |%d| %s",
41 basename, line, level, str);
42#else
Yanray Wange6afd912022-10-27 12:11:18 +080043 (void) level;
44 (void) line;
45 (void) file;
Manuel Pégourié-Gonnard6637ef72025-02-11 13:19:45 +010046#endif
Yanray Wange6afd912022-10-27 12:11:18 +080047
48 if (NULL != p &&
49 NULL != p->pattern &&
50 NULL != strstr(str, p->pattern)) {
51 p->counter++;
52 }
53}
54
55void mbedtls_test_init_handshake_options(
56 mbedtls_test_handshake_test_options *opts)
57{
58#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Ronald Cron987cf892024-03-04 10:24:27 +010059 static int rng_seed = 0xBEEF;
Yanray Wanga72bc9a2023-12-01 23:34:27 +080060
Yanray Wange6afd912022-10-27 12:11:18 +080061 srand(rng_seed);
62 rng_seed += 0xD0;
63#endif
Ronald Cronb4ad3e72024-01-26 14:57:53 +010064
65 memset(opts, 0, sizeof(*opts));
66
Yanray Wange6afd912022-10-27 12:11:18 +080067 opts->cipher = "";
68 opts->client_min_version = MBEDTLS_SSL_VERSION_UNKNOWN;
69 opts->client_max_version = MBEDTLS_SSL_VERSION_UNKNOWN;
70 opts->server_min_version = MBEDTLS_SSL_VERSION_UNKNOWN;
71 opts->server_max_version = MBEDTLS_SSL_VERSION_UNKNOWN;
Ronald Cron097ba142023-03-08 16:18:00 +010072 opts->expected_negotiated_version = MBEDTLS_SSL_VERSION_TLS1_3;
Yanray Wange6afd912022-10-27 12:11:18 +080073 opts->pk_alg = MBEDTLS_PK_RSA;
Yanray Wange6afd912022-10-27 12:11:18 +080074 opts->srv_auth_mode = MBEDTLS_SSL_VERIFY_NONE;
Yanray Wange6afd912022-10-27 12:11:18 +080075 opts->mfl = MBEDTLS_SSL_MAX_FRAG_LEN_NONE;
76 opts->cli_msg_len = 100;
77 opts->srv_msg_len = 100;
78 opts->expected_cli_fragments = 1;
79 opts->expected_srv_fragments = 1;
Yanray Wange6afd912022-10-27 12:11:18 +080080 opts->legacy_renegotiation = MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION;
Yanray Wange6afd912022-10-27 12:11:18 +080081 opts->resize_buffers = 1;
Ronald Cronced99be2024-01-26 15:49:12 +010082 opts->early_data = MBEDTLS_SSL_EARLY_DATA_DISABLED;
Ronald Cron5d3036e2024-02-23 07:43:45 +010083 opts->max_early_data_size = -1;
Yanray Wange6afd912022-10-27 12:11:18 +080084#if defined(MBEDTLS_SSL_CACHE_C)
Tom Cosgrove05b2a872023-07-21 11:31:13 +010085 TEST_CALLOC(opts->cache, 1);
Yanray Wange6afd912022-10-27 12:11:18 +080086 mbedtls_ssl_cache_init(opts->cache);
Pengyu Lv5cbb93e2023-07-10 11:09:40 +080087#if defined(MBEDTLS_HAVE_TIME)
88 TEST_EQUAL(mbedtls_ssl_cache_get_timeout(opts->cache),
89 MBEDTLS_SSL_CACHE_DEFAULT_TIMEOUT);
90#endif
Yanray Wange6afd912022-10-27 12:11:18 +080091exit:
92 return;
93#endif
94}
95
96void mbedtls_test_free_handshake_options(
97 mbedtls_test_handshake_test_options *opts)
98{
99#if defined(MBEDTLS_SSL_CACHE_C)
100 mbedtls_ssl_cache_free(opts->cache);
101 mbedtls_free(opts->cache);
102#else
103 (void) opts;
104#endif
105}
106
107#if defined(MBEDTLS_TEST_HOOKS)
108static void set_chk_buf_ptr_args(
109 mbedtls_ssl_chk_buf_ptr_args *args,
110 unsigned char *cur, unsigned char *end, size_t need)
111{
112 args->cur = cur;
113 args->end = end;
114 args->need = need;
115}
116
117static void reset_chk_buf_ptr_args(mbedtls_ssl_chk_buf_ptr_args *args)
118{
119 memset(args, 0, sizeof(*args));
120}
121#endif /* MBEDTLS_TEST_HOOKS */
122
Yanray Wange6afd912022-10-27 12:11:18 +0800123void mbedtls_test_ssl_buffer_init(mbedtls_test_ssl_buffer *buf)
124{
125 memset(buf, 0, sizeof(*buf));
126}
127
Yanray Wange6afd912022-10-27 12:11:18 +0800128int mbedtls_test_ssl_buffer_setup(mbedtls_test_ssl_buffer *buf,
129 size_t capacity)
130{
131 buf->buffer = (unsigned char *) mbedtls_calloc(capacity,
132 sizeof(unsigned char));
133 if (NULL == buf->buffer) {
134 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
135 }
136 buf->capacity = capacity;
137
138 return 0;
139}
140
141void mbedtls_test_ssl_buffer_free(mbedtls_test_ssl_buffer *buf)
142{
143 if (buf->buffer != NULL) {
144 mbedtls_free(buf->buffer);
145 }
146
147 memset(buf, 0, sizeof(*buf));
148}
149
Yanray Wange6afd912022-10-27 12:11:18 +0800150int mbedtls_test_ssl_buffer_put(mbedtls_test_ssl_buffer *buf,
151 const unsigned char *input, size_t input_len)
152{
153 size_t overflow = 0;
154
155 if ((buf == NULL) || (buf->buffer == NULL)) {
156 return -1;
157 }
158
159 /* Reduce input_len to a number that fits in the buffer. */
160 if ((buf->content_length + input_len) > buf->capacity) {
161 input_len = buf->capacity - buf->content_length;
162 }
163
164 if (input == NULL) {
165 return (input_len == 0) ? 0 : -1;
166 }
167
168 /* Check if the buffer has not come full circle and free space is not in
169 * the middle */
170 if (buf->start + buf->content_length < buf->capacity) {
171
172 /* Calculate the number of bytes that need to be placed at lower memory
173 * address */
174 if (buf->start + buf->content_length + input_len
175 > buf->capacity) {
176 overflow = (buf->start + buf->content_length + input_len)
177 % buf->capacity;
178 }
179
180 memcpy(buf->buffer + buf->start + buf->content_length, input,
181 input_len - overflow);
182 memcpy(buf->buffer, input + input_len - overflow, overflow);
183
184 } else {
185 /* The buffer has come full circle and free space is in the middle */
186 memcpy(buf->buffer + buf->start + buf->content_length - buf->capacity,
187 input, input_len);
188 }
189
190 buf->content_length += input_len;
Yanray Wanga8f445e2022-11-03 11:51:59 +0800191 return (input_len > INT_MAX) ? INT_MAX : (int) input_len;
Yanray Wange6afd912022-10-27 12:11:18 +0800192}
193
Yanray Wange6afd912022-10-27 12:11:18 +0800194int mbedtls_test_ssl_buffer_get(mbedtls_test_ssl_buffer *buf,
195 unsigned char *output, size_t output_len)
196{
197 size_t overflow = 0;
198
199 if ((buf == NULL) || (buf->buffer == NULL)) {
200 return -1;
201 }
202
203 if (output == NULL && output_len == 0) {
204 return 0;
205 }
206
207 if (buf->content_length < output_len) {
208 output_len = buf->content_length;
209 }
210
211 /* Calculate the number of bytes that need to be drawn from lower memory
212 * address */
213 if (buf->start + output_len > buf->capacity) {
214 overflow = (buf->start + output_len) % buf->capacity;
215 }
216
217 if (output != NULL) {
218 memcpy(output, buf->buffer + buf->start, output_len - overflow);
219 memcpy(output + output_len - overflow, buf->buffer, overflow);
220 }
221
222 buf->content_length -= output_len;
223 buf->start = (buf->start + output_len) % buf->capacity;
224
Yanray Wanga8f445e2022-11-03 11:51:59 +0800225 return (output_len > INT_MAX) ? INT_MAX : (int) output_len;
Yanray Wange6afd912022-10-27 12:11:18 +0800226}
227
Yanray Wangd19894f2023-03-16 11:47:39 +0800228int mbedtls_test_ssl_message_queue_setup(
229 mbedtls_test_ssl_message_queue *queue, size_t capacity)
Yanray Wange6afd912022-10-27 12:11:18 +0800230{
231 queue->messages = (size_t *) mbedtls_calloc(capacity, sizeof(size_t));
232 if (NULL == queue->messages) {
233 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
234 }
235
Yanray Wanga8f445e2022-11-03 11:51:59 +0800236 queue->capacity = (capacity > INT_MAX) ? INT_MAX : (int) capacity;
Yanray Wange6afd912022-10-27 12:11:18 +0800237 queue->pos = 0;
238 queue->num = 0;
239
240 return 0;
241}
242
Yanray Wangd19894f2023-03-16 11:47:39 +0800243void mbedtls_test_ssl_message_queue_free(
244 mbedtls_test_ssl_message_queue *queue)
Yanray Wange6afd912022-10-27 12:11:18 +0800245{
246 if (queue == NULL) {
247 return;
248 }
249
250 if (queue->messages != NULL) {
251 mbedtls_free(queue->messages);
252 }
253
254 memset(queue, 0, sizeof(*queue));
255}
256
Yanray Wange6afd912022-10-27 12:11:18 +0800257int mbedtls_test_ssl_message_queue_push_info(
258 mbedtls_test_ssl_message_queue *queue, size_t len)
259{
260 int place;
261 if (queue == NULL) {
262 return MBEDTLS_TEST_ERROR_ARG_NULL;
263 }
264
265 if (queue->num >= queue->capacity) {
266 return MBEDTLS_ERR_SSL_WANT_WRITE;
267 }
268
269 place = (queue->pos + queue->num) % queue->capacity;
270 queue->messages[place] = len;
271 queue->num++;
Yanray Wanga8f445e2022-11-03 11:51:59 +0800272 return (len > INT_MAX) ? INT_MAX : (int) len;
Yanray Wange6afd912022-10-27 12:11:18 +0800273}
274
Yanray Wange6afd912022-10-27 12:11:18 +0800275int mbedtls_test_ssl_message_queue_pop_info(
276 mbedtls_test_ssl_message_queue *queue, size_t buf_len)
277{
278 size_t message_length;
279 if (queue == NULL) {
280 return MBEDTLS_TEST_ERROR_ARG_NULL;
281 }
282 if (queue->num == 0) {
283 return MBEDTLS_ERR_SSL_WANT_READ;
284 }
285
286 message_length = queue->messages[queue->pos];
287 queue->messages[queue->pos] = 0;
288 queue->num--;
289 queue->pos++;
290 queue->pos %= queue->capacity;
291 if (queue->pos < 0) {
292 queue->pos += queue->capacity;
293 }
294
Yanray Wanga8f445e2022-11-03 11:51:59 +0800295 return (message_length > INT_MAX && buf_len > INT_MAX) ? INT_MAX :
296 (message_length > buf_len) ? (int) buf_len : (int) message_length;
Yanray Wange6afd912022-10-27 12:11:18 +0800297}
298
299/*
300 * Take a peek on the info about the next message length from the queue.
301 * This will be the oldest inserted message length(fifo).
302 *
303 * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null.
304 * \retval MBEDTLS_ERR_SSL_WANT_READ, if the queue is empty.
305 * \retval 0, if the peek was successful.
306 * \retval MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED, if the given buffer length is
307 * too small to fit the message. In this case the \p msg_len will be
308 * set to the full message length so that the
309 * caller knows what portion of the message can be dropped.
310 */
Yanray Wang5e22a922023-03-16 14:57:54 +0800311static int test_ssl_message_queue_peek_info(
312 mbedtls_test_ssl_message_queue *queue,
313 size_t buf_len, size_t *msg_len)
Yanray Wange6afd912022-10-27 12:11:18 +0800314{
315 if (queue == NULL || msg_len == NULL) {
316 return MBEDTLS_TEST_ERROR_ARG_NULL;
317 }
318 if (queue->num == 0) {
319 return MBEDTLS_ERR_SSL_WANT_READ;
320 }
321
322 *msg_len = queue->messages[queue->pos];
323 return (*msg_len > buf_len) ? MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED : 0;
324}
325
Yanray Wang5f86a422023-03-15 16:02:29 +0800326void mbedtls_test_mock_socket_init(mbedtls_test_mock_socket *socket)
Yanray Wange6afd912022-10-27 12:11:18 +0800327{
328 memset(socket, 0, sizeof(*socket));
329}
330
Yanray Wange6afd912022-10-27 12:11:18 +0800331void mbedtls_test_mock_socket_close(mbedtls_test_mock_socket *socket)
332{
333 if (socket == NULL) {
334 return;
335 }
336
337 if (socket->input != NULL) {
338 mbedtls_test_ssl_buffer_free(socket->input);
339 mbedtls_free(socket->input);
340 }
341
342 if (socket->output != NULL) {
343 mbedtls_test_ssl_buffer_free(socket->output);
344 mbedtls_free(socket->output);
345 }
346
347 if (socket->peer != NULL) {
348 memset(socket->peer, 0, sizeof(*socket->peer));
349 }
350
351 memset(socket, 0, sizeof(*socket));
352}
353
Yanray Wange6afd912022-10-27 12:11:18 +0800354int mbedtls_test_mock_socket_connect(mbedtls_test_mock_socket *peer1,
355 mbedtls_test_mock_socket *peer2,
356 size_t bufsize)
357{
358 int ret = -1;
359
360 peer1->output =
361 (mbedtls_test_ssl_buffer *) mbedtls_calloc(
362 1, sizeof(mbedtls_test_ssl_buffer));
363 if (peer1->output == NULL) {
364 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
365 goto exit;
366 }
367 mbedtls_test_ssl_buffer_init(peer1->output);
368 if (0 != (ret = mbedtls_test_ssl_buffer_setup(peer1->output, bufsize))) {
369 goto exit;
370 }
371
372 peer2->output =
373 (mbedtls_test_ssl_buffer *) mbedtls_calloc(
374 1, sizeof(mbedtls_test_ssl_buffer));
375 if (peer2->output == NULL) {
376 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
377 goto exit;
378 }
379 mbedtls_test_ssl_buffer_init(peer2->output);
380 if (0 != (ret = mbedtls_test_ssl_buffer_setup(peer2->output, bufsize))) {
381 goto exit;
382 }
383
384 peer1->peer = peer2;
385 peer2->peer = peer1;
386 peer1->input = peer2->output;
387 peer2->input = peer1->output;
388
389 peer1->status = peer2->status = MBEDTLS_MOCK_SOCKET_CONNECTED;
390 ret = 0;
391
392exit:
393
394 if (ret != 0) {
395 mbedtls_test_mock_socket_close(peer1);
396 mbedtls_test_mock_socket_close(peer2);
397 }
398
399 return ret;
400}
401
Yanray Wangaf727a22023-03-13 19:22:36 +0800402int mbedtls_test_mock_tcp_send_b(void *ctx,
403 const unsigned char *buf, size_t len)
Yanray Wange6afd912022-10-27 12:11:18 +0800404{
405 mbedtls_test_mock_socket *socket = (mbedtls_test_mock_socket *) ctx;
406
407 if (socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED) {
408 return -1;
409 }
410
411 return mbedtls_test_ssl_buffer_put(socket->output, buf, len);
412}
413
414int mbedtls_test_mock_tcp_recv_b(void *ctx, unsigned char *buf, size_t len)
415{
416 mbedtls_test_mock_socket *socket = (mbedtls_test_mock_socket *) ctx;
417
418 if (socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED) {
419 return -1;
420 }
421
422 return mbedtls_test_ssl_buffer_get(socket->input, buf, len);
423}
424
Yanray Wang34634352023-02-14 17:56:51 +0800425int mbedtls_test_mock_tcp_send_nb(void *ctx,
426 const unsigned char *buf, size_t len)
Yanray Wange6afd912022-10-27 12:11:18 +0800427{
428 mbedtls_test_mock_socket *socket = (mbedtls_test_mock_socket *) ctx;
429
430 if (socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED) {
431 return -1;
432 }
433
434 if (socket->output->capacity == socket->output->content_length) {
435 return MBEDTLS_ERR_SSL_WANT_WRITE;
436 }
437
438 return mbedtls_test_ssl_buffer_put(socket->output, buf, len);
439}
440
441int mbedtls_test_mock_tcp_recv_nb(void *ctx, unsigned char *buf, size_t len)
442{
443 mbedtls_test_mock_socket *socket = (mbedtls_test_mock_socket *) ctx;
444
445 if (socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED) {
446 return -1;
447 }
448
449 if (socket->input->content_length == 0) {
450 return MBEDTLS_ERR_SSL_WANT_READ;
451 }
452
453 return mbedtls_test_ssl_buffer_get(socket->input, buf, len);
454}
455
Yanray Wangd19894f2023-03-16 11:47:39 +0800456void mbedtls_test_message_socket_init(
457 mbedtls_test_message_socket_context *ctx)
Yanray Wange6afd912022-10-27 12:11:18 +0800458{
459 ctx->queue_input = NULL;
460 ctx->queue_output = NULL;
461 ctx->socket = NULL;
462}
463
Yanray Wange6afd912022-10-27 12:11:18 +0800464int mbedtls_test_message_socket_setup(
465 mbedtls_test_ssl_message_queue *queue_input,
466 mbedtls_test_ssl_message_queue *queue_output,
467 size_t queue_capacity,
468 mbedtls_test_mock_socket *socket,
469 mbedtls_test_message_socket_context *ctx)
470{
471 int ret = mbedtls_test_ssl_message_queue_setup(queue_input, queue_capacity);
472 if (ret != 0) {
473 return ret;
474 }
475 ctx->queue_input = queue_input;
476 ctx->queue_output = queue_output;
477 ctx->socket = socket;
Yanray Wang5f86a422023-03-15 16:02:29 +0800478 mbedtls_test_mock_socket_init(socket);
Yanray Wange6afd912022-10-27 12:11:18 +0800479
480 return 0;
481}
482
Yanray Wangd19894f2023-03-16 11:47:39 +0800483void mbedtls_test_message_socket_close(
484 mbedtls_test_message_socket_context *ctx)
Yanray Wange6afd912022-10-27 12:11:18 +0800485{
486 if (ctx == NULL) {
487 return;
488 }
489
490 mbedtls_test_ssl_message_queue_free(ctx->queue_input);
491 mbedtls_test_mock_socket_close(ctx->socket);
492 memset(ctx, 0, sizeof(*ctx));
493}
494
Yanray Wang34634352023-02-14 17:56:51 +0800495int mbedtls_test_mock_tcp_send_msg(void *ctx,
496 const unsigned char *buf, size_t len)
Yanray Wange6afd912022-10-27 12:11:18 +0800497{
498 mbedtls_test_ssl_message_queue *queue;
499 mbedtls_test_mock_socket *socket;
500 mbedtls_test_message_socket_context *context =
501 (mbedtls_test_message_socket_context *) ctx;
502
503 if (context == NULL || context->socket == NULL
504 || context->queue_output == NULL) {
505 return MBEDTLS_TEST_ERROR_CONTEXT_ERROR;
506 }
507
508 queue = context->queue_output;
509 socket = context->socket;
510
511 if (queue->num >= queue->capacity) {
512 return MBEDTLS_ERR_SSL_WANT_WRITE;
513 }
514
515 if (mbedtls_test_mock_tcp_send_b(socket, buf, len) != (int) len) {
516 return MBEDTLS_TEST_ERROR_SEND_FAILED;
517 }
518
519 return mbedtls_test_ssl_message_queue_push_info(queue, len);
520}
521
Yanray Wang34634352023-02-14 17:56:51 +0800522int mbedtls_test_mock_tcp_recv_msg(void *ctx,
523 unsigned char *buf, size_t buf_len)
Yanray Wange6afd912022-10-27 12:11:18 +0800524{
525 mbedtls_test_ssl_message_queue *queue;
526 mbedtls_test_mock_socket *socket;
527 mbedtls_test_message_socket_context *context =
528 (mbedtls_test_message_socket_context *) ctx;
529 size_t drop_len = 0;
530 size_t msg_len;
531 int ret;
532
533 if (context == NULL || context->socket == NULL
534 || context->queue_input == NULL) {
535 return MBEDTLS_TEST_ERROR_CONTEXT_ERROR;
536 }
537
538 queue = context->queue_input;
539 socket = context->socket;
540
541 /* Peek first, so that in case of a socket error the data remains in
542 * the queue. */
Yanray Wang5e22a922023-03-16 14:57:54 +0800543 ret = test_ssl_message_queue_peek_info(queue, buf_len, &msg_len);
Yanray Wange6afd912022-10-27 12:11:18 +0800544 if (ret == MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED) {
545 /* Calculate how much to drop */
546 drop_len = msg_len - buf_len;
547
548 /* Set the requested message len to be buffer length */
549 msg_len = buf_len;
550 } else if (ret != 0) {
551 return ret;
552 }
553
554 if (mbedtls_test_mock_tcp_recv_b(socket, buf, msg_len) != (int) msg_len) {
555 return MBEDTLS_TEST_ERROR_RECV_FAILED;
556 }
557
558 if (ret == MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED) {
559 /* Drop the remaining part of the message */
Yanray Wangaf727a22023-03-13 19:22:36 +0800560 if (mbedtls_test_mock_tcp_recv_b(socket, NULL, drop_len) !=
561 (int) drop_len) {
Yanray Wange6afd912022-10-27 12:11:18 +0800562 /* Inconsistent state - part of the message was read,
563 * and a part couldn't. Not much we can do here, but it should not
564 * happen in test environment, unless forced manually. */
565 }
566 }
Tomás Gonzáleza9b90de2024-02-01 11:12:20 +0000567 ret = mbedtls_test_ssl_message_queue_pop_info(queue, buf_len);
568 if (ret < 0) {
569 return ret;
570 }
Yanray Wange6afd912022-10-27 12:11:18 +0800571
Yanray Wanga8f445e2022-11-03 11:51:59 +0800572 return (msg_len > INT_MAX) ? INT_MAX : (int) msg_len;
Yanray Wange6afd912022-10-27 12:11:18 +0800573}
574
575#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
576
577/*
578 * Deinitializes certificates from endpoint represented by \p ep.
579 */
Yanray Wangf6f71902023-03-15 16:05:14 +0800580static void test_ssl_endpoint_certificate_free(mbedtls_test_ssl_endpoint *ep)
Yanray Wange6afd912022-10-27 12:11:18 +0800581{
582 mbedtls_test_ssl_endpoint_certificate *cert = &(ep->cert);
583 if (cert != NULL) {
584 if (cert->ca_cert != NULL) {
585 mbedtls_x509_crt_free(cert->ca_cert);
586 mbedtls_free(cert->ca_cert);
587 cert->ca_cert = NULL;
588 }
589 if (cert->cert != NULL) {
590 mbedtls_x509_crt_free(cert->cert);
591 mbedtls_free(cert->cert);
592 cert->cert = NULL;
593 }
594 if (cert->pkey != NULL) {
595#if defined(MBEDTLS_USE_PSA_CRYPTO)
596 if (mbedtls_pk_get_type(cert->pkey) == MBEDTLS_PK_OPAQUE) {
Valerio Setti4f387ef2023-05-02 14:15:59 +0200597 psa_destroy_key(cert->pkey->priv_id);
Yanray Wange6afd912022-10-27 12:11:18 +0800598 }
599#endif
600 mbedtls_pk_free(cert->pkey);
601 mbedtls_free(cert->pkey);
602 cert->pkey = NULL;
603 }
604 }
605}
606
Yanray Wange6afd912022-10-27 12:11:18 +0800607int mbedtls_test_ssl_endpoint_certificate_init(mbedtls_test_ssl_endpoint *ep,
608 int pk_alg,
609 int opaque_alg, int opaque_alg2,
610 int opaque_usage)
611{
612 int i = 0;
613 int ret = -1;
614 mbedtls_test_ssl_endpoint_certificate *cert = NULL;
615#if defined(MBEDTLS_USE_PSA_CRYPTO)
616 mbedtls_svc_key_id_t key_slot = MBEDTLS_SVC_KEY_ID_INIT;
617#endif
618
619 if (ep == NULL) {
620 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
621 }
622
623 cert = &(ep->cert);
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100624 TEST_CALLOC(cert->ca_cert, 1);
625 TEST_CALLOC(cert->cert, 1);
626 TEST_CALLOC(cert->pkey, 1);
Yanray Wange6afd912022-10-27 12:11:18 +0800627
628 mbedtls_x509_crt_init(cert->ca_cert);
629 mbedtls_x509_crt_init(cert->cert);
630 mbedtls_pk_init(cert->pkey);
631
632 /* Load the trusted CA */
633
634 for (i = 0; mbedtls_test_cas_der[i] != NULL; i++) {
635 ret = mbedtls_x509_crt_parse_der(
636 cert->ca_cert,
637 (const unsigned char *) mbedtls_test_cas_der[i],
638 mbedtls_test_cas_der_len[i]);
639 TEST_ASSERT(ret == 0);
640 }
641
642 /* Load own certificate and private key */
643
644 if (ep->conf.endpoint == MBEDTLS_SSL_IS_SERVER) {
645 if (pk_alg == MBEDTLS_PK_RSA) {
646 ret = mbedtls_x509_crt_parse(
647 cert->cert,
648 (const unsigned char *) mbedtls_test_srv_crt_rsa_sha256_der,
649 mbedtls_test_srv_crt_rsa_sha256_der_len);
650 TEST_ASSERT(ret == 0);
651
652 ret = mbedtls_pk_parse_key(
653 cert->pkey,
654 (const unsigned char *) mbedtls_test_srv_key_rsa_der,
Ben Taylor440cb2a2025-03-05 09:40:08 +0000655 mbedtls_test_srv_key_rsa_der_len, NULL, 0);
Yanray Wange6afd912022-10-27 12:11:18 +0800656 TEST_ASSERT(ret == 0);
657 } else {
658 ret = mbedtls_x509_crt_parse(
659 cert->cert,
660 (const unsigned char *) mbedtls_test_srv_crt_ec_der,
661 mbedtls_test_srv_crt_ec_der_len);
662 TEST_ASSERT(ret == 0);
663
664 ret = mbedtls_pk_parse_key(
665 cert->pkey,
666 (const unsigned char *) mbedtls_test_srv_key_ec_der,
Ben Taylor440cb2a2025-03-05 09:40:08 +0000667 mbedtls_test_srv_key_ec_der_len, NULL, 0);
Yanray Wange6afd912022-10-27 12:11:18 +0800668 TEST_ASSERT(ret == 0);
669 }
670 } else {
671 if (pk_alg == MBEDTLS_PK_RSA) {
672 ret = mbedtls_x509_crt_parse(
673 cert->cert,
674 (const unsigned char *) mbedtls_test_cli_crt_rsa_der,
675 mbedtls_test_cli_crt_rsa_der_len);
676 TEST_ASSERT(ret == 0);
677
678 ret = mbedtls_pk_parse_key(
679 cert->pkey,
680 (const unsigned char *) mbedtls_test_cli_key_rsa_der,
Ben Taylor440cb2a2025-03-05 09:40:08 +0000681 mbedtls_test_cli_key_rsa_der_len, NULL, 0);
Yanray Wange6afd912022-10-27 12:11:18 +0800682 TEST_ASSERT(ret == 0);
683 } else {
684 ret = mbedtls_x509_crt_parse(
685 cert->cert,
686 (const unsigned char *) mbedtls_test_cli_crt_ec_der,
687 mbedtls_test_cli_crt_ec_len);
688 TEST_ASSERT(ret == 0);
689
690 ret = mbedtls_pk_parse_key(
691 cert->pkey,
692 (const unsigned char *) mbedtls_test_cli_key_ec_der,
Ben Taylor440cb2a2025-03-05 09:40:08 +0000693 mbedtls_test_cli_key_ec_der_len, NULL, 0);
Yanray Wange6afd912022-10-27 12:11:18 +0800694 TEST_ASSERT(ret == 0);
695 }
696 }
697
698#if defined(MBEDTLS_USE_PSA_CRYPTO)
699 if (opaque_alg != 0) {
Valerio Setti1fa2f6e2024-02-27 08:11:25 +0100700 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
701 /* Use a fake key usage to get a successful initial guess for the PSA attributes. */
Valerio Settia9de9442024-02-27 13:56:09 +0100702 TEST_EQUAL(mbedtls_pk_get_psa_attributes(cert->pkey, PSA_KEY_USAGE_SIGN_HASH,
Valerio Setti1fa2f6e2024-02-27 08:11:25 +0100703 &key_attr), 0);
Valerio Settia9de9442024-02-27 13:56:09 +0100704 /* Then manually usage, alg and alg2 as requested by the test. */
Valerio Setti1fa2f6e2024-02-27 08:11:25 +0100705 psa_set_key_usage_flags(&key_attr, opaque_usage);
706 psa_set_key_algorithm(&key_attr, opaque_alg);
707 if (opaque_alg2 != PSA_ALG_NONE) {
708 psa_set_key_enrollment_algorithm(&key_attr, opaque_alg2);
709 }
710 TEST_EQUAL(mbedtls_pk_import_into_psa(cert->pkey, &key_attr, &key_slot), 0);
711 mbedtls_pk_free(cert->pkey);
712 mbedtls_pk_init(cert->pkey);
713 TEST_EQUAL(mbedtls_pk_setup_opaque(cert->pkey, key_slot), 0);
Yanray Wange6afd912022-10-27 12:11:18 +0800714 }
715#else
716 (void) opaque_alg;
717 (void) opaque_alg2;
718 (void) opaque_usage;
719#endif
720
721 mbedtls_ssl_conf_ca_chain(&(ep->conf), cert->ca_cert, NULL);
722
723 ret = mbedtls_ssl_conf_own_cert(&(ep->conf), cert->cert,
724 cert->pkey);
725 TEST_ASSERT(ret == 0);
726 TEST_ASSERT(ep->conf.key_cert != NULL);
727
728 ret = mbedtls_ssl_conf_own_cert(&(ep->conf), NULL, NULL);
729 TEST_ASSERT(ret == 0);
730 TEST_ASSERT(ep->conf.key_cert == NULL);
731
732 ret = mbedtls_ssl_conf_own_cert(&(ep->conf), cert->cert,
733 cert->pkey);
734 TEST_ASSERT(ret == 0);
735
736exit:
737 if (ret != 0) {
Yanray Wangf6f71902023-03-15 16:05:14 +0800738 test_ssl_endpoint_certificate_free(ep);
Yanray Wange6afd912022-10-27 12:11:18 +0800739 }
740
741 return ret;
742}
743
Yanray Wange6afd912022-10-27 12:11:18 +0800744int mbedtls_test_ssl_endpoint_init(
745 mbedtls_test_ssl_endpoint *ep, int endpoint_type,
746 mbedtls_test_handshake_test_options *options,
747 mbedtls_test_message_socket_context *dtls_context,
748 mbedtls_test_ssl_message_queue *input_queue,
Ronald Cronfb536472024-01-26 14:55:25 +0100749 mbedtls_test_ssl_message_queue *output_queue)
Yanray Wange6afd912022-10-27 12:11:18 +0800750{
751 int ret = -1;
752 uintptr_t user_data_n;
753
754 if (dtls_context != NULL &&
755 (input_queue == NULL || output_queue == NULL)) {
756 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
757
758 }
759
760 if (ep == NULL) {
761 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
762 }
763
764 memset(ep, 0, sizeof(*ep));
765
766 ep->name = (endpoint_type == MBEDTLS_SSL_IS_SERVER) ? "Server" : "Client";
767
768 mbedtls_ssl_init(&(ep->ssl));
769 mbedtls_ssl_config_init(&(ep->conf));
Yanray Wange6afd912022-10-27 12:11:18 +0800770
771 TEST_ASSERT(mbedtls_ssl_conf_get_user_data_p(&ep->conf) == NULL);
772 TEST_EQUAL(mbedtls_ssl_conf_get_user_data_n(&ep->conf), 0);
773 TEST_ASSERT(mbedtls_ssl_get_user_data_p(&ep->ssl) == NULL);
774 TEST_EQUAL(mbedtls_ssl_get_user_data_n(&ep->ssl), 0);
775
776 (void) mbedtls_test_rnd_std_rand(NULL,
777 (void *) &user_data_n,
778 sizeof(user_data_n));
779 mbedtls_ssl_conf_set_user_data_n(&ep->conf, user_data_n);
780 mbedtls_ssl_set_user_data_n(&ep->ssl, user_data_n);
781
782 if (dtls_context != NULL) {
783 TEST_ASSERT(mbedtls_test_message_socket_setup(input_queue, output_queue,
784 100, &(ep->socket),
785 dtls_context) == 0);
786 } else {
Yanray Wang5f86a422023-03-15 16:02:29 +0800787 mbedtls_test_mock_socket_init(&(ep->socket));
Yanray Wange6afd912022-10-27 12:11:18 +0800788 }
789
790 /* Non-blocking callbacks without timeout */
791 if (dtls_context != NULL) {
792 mbedtls_ssl_set_bio(&(ep->ssl), dtls_context,
793 mbedtls_test_mock_tcp_send_msg,
794 mbedtls_test_mock_tcp_recv_msg,
795 NULL);
796 } else {
797 mbedtls_ssl_set_bio(&(ep->ssl), &(ep->socket),
798 mbedtls_test_mock_tcp_send_nb,
799 mbedtls_test_mock_tcp_recv_nb,
800 NULL);
801 }
802
803 ret = mbedtls_ssl_config_defaults(&(ep->conf), endpoint_type,
804 (dtls_context != NULL) ?
805 MBEDTLS_SSL_TRANSPORT_DATAGRAM :
806 MBEDTLS_SSL_TRANSPORT_STREAM,
807 MBEDTLS_SSL_PRESET_DEFAULT);
808 TEST_ASSERT(ret == 0);
809
Ronald Crona697a712023-03-09 17:47:42 +0100810 if (MBEDTLS_SSL_IS_CLIENT == endpoint_type) {
811 if (options->client_min_version != MBEDTLS_SSL_VERSION_UNKNOWN) {
812 mbedtls_ssl_conf_min_tls_version(&(ep->conf),
813 options->client_min_version);
814 }
815
816 if (options->client_max_version != MBEDTLS_SSL_VERSION_UNKNOWN) {
817 mbedtls_ssl_conf_max_tls_version(&(ep->conf),
818 options->client_max_version);
819 }
820 } else {
821 if (options->server_min_version != MBEDTLS_SSL_VERSION_UNKNOWN) {
822 mbedtls_ssl_conf_min_tls_version(&(ep->conf),
823 options->server_min_version);
824 }
825
826 if (options->server_max_version != MBEDTLS_SSL_VERSION_UNKNOWN) {
827 mbedtls_ssl_conf_max_tls_version(&(ep->conf),
828 options->server_max_version);
829 }
830 }
831
Ronald Cronfb536472024-01-26 14:55:25 +0100832 if (options->group_list != NULL) {
833 mbedtls_ssl_conf_groups(&(ep->conf), options->group_list);
Yanray Wange6afd912022-10-27 12:11:18 +0800834 }
835
836 mbedtls_ssl_conf_authmode(&(ep->conf), MBEDTLS_SSL_VERIFY_REQUIRED);
837
Ronald Cronced99be2024-01-26 15:49:12 +0100838#if defined(MBEDTLS_SSL_EARLY_DATA)
839 mbedtls_ssl_conf_early_data(&(ep->conf), options->early_data);
Ronald Cron5d3036e2024-02-23 07:43:45 +0100840#if defined(MBEDTLS_SSL_SRV_C)
841 if (endpoint_type == MBEDTLS_SSL_IS_SERVER &&
842 (options->max_early_data_size >= 0)) {
843 mbedtls_ssl_conf_max_early_data_size(&(ep->conf),
844 options->max_early_data_size);
845 }
846#endif
Waleed Elmelegy4dfb0e72024-03-14 01:48:40 +0000847#if defined(MBEDTLS_SSL_ALPN)
848 /* check that alpn_list contains at least one valid entry */
849 if (options->alpn_list[0] != NULL) {
850 mbedtls_ssl_conf_alpn_protocols(&(ep->conf), options->alpn_list);
851 }
852#endif
Ronald Cronced99be2024-01-26 15:49:12 +0100853#endif
854
Yanray Wange6afd912022-10-27 12:11:18 +0800855#if defined(MBEDTLS_SSL_CACHE_C) && defined(MBEDTLS_SSL_SRV_C)
856 if (endpoint_type == MBEDTLS_SSL_IS_SERVER && options->cache != NULL) {
857 mbedtls_ssl_conf_session_cache(&(ep->conf), options->cache,
858 mbedtls_ssl_cache_get,
859 mbedtls_ssl_cache_set);
860 }
861#endif
862
863 ret = mbedtls_ssl_setup(&(ep->ssl), &(ep->conf));
864 TEST_ASSERT(ret == 0);
865
Gilles Peskine856a3702025-02-13 17:28:49 +0100866 if (MBEDTLS_SSL_IS_CLIENT == endpoint_type) {
867 ret = mbedtls_ssl_set_hostname(&(ep->ssl), "localhost");
868 }
869
Yanray Wange6afd912022-10-27 12:11:18 +0800870#if defined(MBEDTLS_SSL_PROTO_DTLS) && defined(MBEDTLS_SSL_SRV_C)
871 if (endpoint_type == MBEDTLS_SSL_IS_SERVER && dtls_context != NULL) {
872 mbedtls_ssl_conf_dtls_cookies(&(ep->conf), NULL, NULL, NULL);
873 }
874#endif
875
Ronald Cronec3408d2024-01-16 17:50:40 +0100876#if defined(MBEDTLS_DEBUG_C)
877#if defined(MBEDTLS_SSL_SRV_C)
878 if (endpoint_type == MBEDTLS_SSL_IS_SERVER &&
879 options->srv_log_fun != NULL) {
880 mbedtls_ssl_conf_dbg(&(ep->conf), options->srv_log_fun,
881 options->srv_log_obj);
882 }
883#endif
884#if defined(MBEDTLS_SSL_CLI_C)
885 if (endpoint_type == MBEDTLS_SSL_IS_CLIENT &&
886 options->cli_log_fun != NULL) {
887 mbedtls_ssl_conf_dbg(&(ep->conf), options->cli_log_fun,
888 options->cli_log_obj);
889 }
890#endif
891#endif /* MBEDTLS_DEBUG_C */
892
Yanray Wange6afd912022-10-27 12:11:18 +0800893 ret = mbedtls_test_ssl_endpoint_certificate_init(ep, options->pk_alg,
894 options->opaque_alg,
895 options->opaque_alg2,
896 options->opaque_usage);
897 TEST_ASSERT(ret == 0);
898
899 TEST_EQUAL(mbedtls_ssl_conf_get_user_data_n(&ep->conf), user_data_n);
900 mbedtls_ssl_conf_set_user_data_p(&ep->conf, ep);
901 TEST_EQUAL(mbedtls_ssl_get_user_data_n(&ep->ssl), user_data_n);
902 mbedtls_ssl_set_user_data_p(&ep->ssl, ep);
903
904exit:
905 return ret;
906}
907
Yanray Wange6afd912022-10-27 12:11:18 +0800908void mbedtls_test_ssl_endpoint_free(
909 mbedtls_test_ssl_endpoint *ep,
910 mbedtls_test_message_socket_context *context)
911{
Yanray Wangf6f71902023-03-15 16:05:14 +0800912 test_ssl_endpoint_certificate_free(ep);
Yanray Wange6afd912022-10-27 12:11:18 +0800913
914 mbedtls_ssl_free(&(ep->ssl));
915 mbedtls_ssl_config_free(&(ep->conf));
916
917 if (context != NULL) {
918 mbedtls_test_message_socket_close(context);
919 } else {
920 mbedtls_test_mock_socket_close(&(ep->socket));
921 }
922}
923
Yanray Wange6afd912022-10-27 12:11:18 +0800924int mbedtls_test_move_handshake_to_state(mbedtls_ssl_context *ssl,
925 mbedtls_ssl_context *second_ssl,
926 int state)
927{
928 enum { BUFFSIZE = 1024 };
929 int max_steps = 1000;
930 int ret = 0;
931
932 if (ssl == NULL || second_ssl == NULL) {
933 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
934 }
935
936 /* Perform communication via connected sockets */
937 while ((ssl->state != state) && (--max_steps >= 0)) {
938 /* If /p second_ssl ends the handshake procedure before /p ssl then
939 * there is no need to call the next step */
940 if (!mbedtls_ssl_is_handshake_over(second_ssl)) {
941 ret = mbedtls_ssl_handshake_step(second_ssl);
942 if (ret != 0 && ret != MBEDTLS_ERR_SSL_WANT_READ &&
943 ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
944 return ret;
945 }
946 }
947
948 /* We only care about the \p ssl state and returns, so we call it last,
949 * to leave the iteration as soon as the state is as expected. */
950 ret = mbedtls_ssl_handshake_step(ssl);
951 if (ret != 0 && ret != MBEDTLS_ERR_SSL_WANT_READ &&
952 ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
953 return ret;
954 }
955 }
956
957 return (max_steps >= 0) ? ret : -1;
958}
959
960#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
961
962/*
963 * Write application data. Increase write counter if necessary.
964 */
Michael Schuster54300d42024-06-04 02:30:22 +0200965static int mbedtls_ssl_write_fragment(mbedtls_ssl_context *ssl,
Michael Schusterbd89b792024-06-04 02:41:10 +0200966 unsigned char *buf, int buf_len,
967 int *written,
968 const int expected_fragments)
Yanray Wange6afd912022-10-27 12:11:18 +0800969{
Agathiyan Bragadeesh93212652023-07-12 11:22:59 +0100970 int ret;
Yanray Wange6afd912022-10-27 12:11:18 +0800971 /* Verify that calling mbedtls_ssl_write with a NULL buffer and zero length is
972 * a valid no-op for TLS connections. */
973 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
974 TEST_ASSERT(mbedtls_ssl_write(ssl, NULL, 0) == 0);
975 }
976
Agathiyan Bragadeesh93212652023-07-12 11:22:59 +0100977 ret = mbedtls_ssl_write(ssl, buf + *written, buf_len - *written);
Yanray Wange6afd912022-10-27 12:11:18 +0800978 if (ret > 0) {
979 *written += ret;
980 }
981
982 if (expected_fragments == 0) {
983 /* Used for DTLS and the message size larger than MFL. In that case
984 * the message can not be fragmented and the library should return
985 * MBEDTLS_ERR_SSL_BAD_INPUT_DATA error. This error must be returned
Yanray Wangb088bfc2023-03-16 12:15:49 +0800986 * to prevent a dead loop inside mbedtls_test_ssl_exchange_data(). */
Yanray Wange6afd912022-10-27 12:11:18 +0800987 return ret;
988 } else if (expected_fragments == 1) {
989 /* Used for TLS/DTLS and the message size lower than MFL */
990 TEST_ASSERT(ret == buf_len ||
991 ret == MBEDTLS_ERR_SSL_WANT_READ ||
992 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
993 } else {
994 /* Used for TLS and the message size larger than MFL */
995 TEST_ASSERT(expected_fragments > 1);
996 TEST_ASSERT((ret >= 0 && ret <= buf_len) ||
997 ret == MBEDTLS_ERR_SSL_WANT_READ ||
998 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
999 }
1000
1001 return 0;
1002
1003exit:
1004 /* Some of the tests failed */
1005 return -1;
1006}
1007
1008/*
1009 * Read application data and increase read counter and fragments counter
1010 * if necessary.
1011 */
Michael Schuster54300d42024-06-04 02:30:22 +02001012static int mbedtls_ssl_read_fragment(mbedtls_ssl_context *ssl,
Michael Schusterbd89b792024-06-04 02:41:10 +02001013 unsigned char *buf, int buf_len,
1014 int *read, int *fragments,
1015 const int expected_fragments)
Yanray Wange6afd912022-10-27 12:11:18 +08001016{
Agathiyan Bragadeesh93212652023-07-12 11:22:59 +01001017 int ret;
Yanray Wange6afd912022-10-27 12:11:18 +08001018 /* Verify that calling mbedtls_ssl_write with a NULL buffer and zero length is
1019 * a valid no-op for TLS connections. */
1020 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1021 TEST_ASSERT(mbedtls_ssl_read(ssl, NULL, 0) == 0);
1022 }
1023
Agathiyan Bragadeesh93212652023-07-12 11:22:59 +01001024 ret = mbedtls_ssl_read(ssl, buf + *read, buf_len - *read);
Yanray Wange6afd912022-10-27 12:11:18 +08001025 if (ret > 0) {
1026 (*fragments)++;
1027 *read += ret;
1028 }
1029
1030 if (expected_fragments == 0) {
1031 TEST_ASSERT(ret == 0);
1032 } else if (expected_fragments == 1) {
1033 TEST_ASSERT(ret == buf_len ||
1034 ret == MBEDTLS_ERR_SSL_WANT_READ ||
1035 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
1036 } else {
1037 TEST_ASSERT(expected_fragments > 1);
1038 TEST_ASSERT((ret >= 0 && ret <= buf_len) ||
1039 ret == MBEDTLS_ERR_SSL_WANT_READ ||
1040 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
1041 }
1042
1043 return 0;
1044
1045exit:
1046 /* Some of the tests failed */
1047 return -1;
1048}
1049
Yanray Wangead70c82023-03-16 12:04:49 +08001050#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
1051static void set_ciphersuite(mbedtls_ssl_config *conf, const char *cipher,
1052 int *forced_ciphersuite)
Yanray Wange6afd912022-10-27 12:11:18 +08001053{
1054 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
1055 forced_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id(cipher);
1056 forced_ciphersuite[1] = 0;
1057
1058 ciphersuite_info =
1059 mbedtls_ssl_ciphersuite_from_id(forced_ciphersuite[0]);
1060
1061 TEST_ASSERT(ciphersuite_info != NULL);
1062 TEST_ASSERT(ciphersuite_info->min_tls_version <= conf->max_tls_version);
1063 TEST_ASSERT(ciphersuite_info->max_tls_version >= conf->min_tls_version);
1064
1065 if (conf->max_tls_version > ciphersuite_info->max_tls_version) {
Agathiyan Bragadeesh2f017a82023-07-12 11:21:54 +01001066 conf->max_tls_version = (mbedtls_ssl_protocol_version) ciphersuite_info->max_tls_version;
Yanray Wange6afd912022-10-27 12:11:18 +08001067 }
1068 if (conf->min_tls_version < ciphersuite_info->min_tls_version) {
Agathiyan Bragadeesh2f017a82023-07-12 11:21:54 +01001069 conf->min_tls_version = (mbedtls_ssl_protocol_version) ciphersuite_info->min_tls_version;
Yanray Wange6afd912022-10-27 12:11:18 +08001070 }
1071
1072 mbedtls_ssl_conf_ciphersuites(conf, forced_ciphersuite);
1073
1074exit:
1075 return;
1076}
Yanray Wangead70c82023-03-16 12:04:49 +08001077#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Yanray Wange6afd912022-10-27 12:11:18 +08001078
Yanray Wangead70c82023-03-16 12:04:49 +08001079#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) && \
1080 defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED) && \
1081 defined(MBEDTLS_SSL_SRV_C)
1082static int psk_dummy_callback(void *p_info, mbedtls_ssl_context *ssl,
1083 const unsigned char *name, size_t name_len)
Yanray Wange6afd912022-10-27 12:11:18 +08001084{
1085 (void) p_info;
1086 (void) ssl;
1087 (void) name;
1088 (void) name_len;
1089
1090 return 0;
1091}
Yanray Wangead70c82023-03-16 12:04:49 +08001092#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED &&
1093 MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED &&
1094 MBEDTLS_SSL_SRV_C */
Yanray Wange6afd912022-10-27 12:11:18 +08001095
1096#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Elena Uziunaite74342c72024-07-05 11:31:29 +01001097 defined(PSA_WANT_ALG_CBC_NO_PADDING) && defined(PSA_WANT_KEY_TYPE_AES)
Yanray Wange6afd912022-10-27 12:11:18 +08001098int mbedtls_test_psa_cipher_encrypt_helper(mbedtls_ssl_transform *transform,
1099 const unsigned char *iv,
1100 size_t iv_len,
1101 const unsigned char *input,
1102 size_t ilen,
1103 unsigned char *output,
1104 size_t *olen)
1105{
1106#if defined(MBEDTLS_USE_PSA_CRYPTO)
1107 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1108 psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT;
1109 size_t part_len;
1110
1111 status = psa_cipher_encrypt_setup(&cipher_op,
1112 transform->psa_key_enc,
1113 transform->psa_alg);
1114
1115 if (status != PSA_SUCCESS) {
1116 return PSA_TO_MBEDTLS_ERR(status);
1117 }
1118
1119 status = psa_cipher_set_iv(&cipher_op, iv, iv_len);
1120
1121 if (status != PSA_SUCCESS) {
1122 return PSA_TO_MBEDTLS_ERR(status);
1123 }
1124
1125 status = psa_cipher_update(&cipher_op, input, ilen, output, ilen, olen);
1126
1127 if (status != PSA_SUCCESS) {
1128 return PSA_TO_MBEDTLS_ERR(status);
1129 }
1130
1131 status = psa_cipher_finish(&cipher_op, output + *olen, ilen - *olen,
1132 &part_len);
1133
1134 if (status != PSA_SUCCESS) {
1135 return PSA_TO_MBEDTLS_ERR(status);
1136 }
1137
1138 *olen += part_len;
1139 return 0;
1140#else
1141 return mbedtls_cipher_crypt(&transform->cipher_ctx_enc,
1142 iv, iv_len, input, ilen, output, olen);
1143#endif /* MBEDTLS_USE_PSA_CRYPTO */
1144}
Elena Uziunaite74342c72024-07-05 11:31:29 +01001145#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && PSA_WANT_ALG_CBC_NO_PADDING &&
Elena Uziunaite6121a342024-07-05 11:16:53 +01001146 PSA_WANT_KEY_TYPE_AES */
Yanray Wange6afd912022-10-27 12:11:18 +08001147
Valerio Setti31ad3a12023-10-27 11:55:02 +02001148static void mbedtls_test_ssl_cipher_info_from_type(mbedtls_cipher_type_t cipher_type,
1149 mbedtls_cipher_mode_t *cipher_mode,
1150 size_t *key_bits, size_t *iv_len)
1151{
1152 switch (cipher_type) {
1153 case MBEDTLS_CIPHER_AES_128_CBC:
1154 *cipher_mode = MBEDTLS_MODE_CBC;
1155 *key_bits = 128;
1156 *iv_len = 16;
1157 break;
1158 case MBEDTLS_CIPHER_AES_256_CBC:
1159 *cipher_mode = MBEDTLS_MODE_CBC;
1160 *key_bits = 256;
1161 *iv_len = 16;
1162 break;
1163 case MBEDTLS_CIPHER_ARIA_128_CBC:
1164 *cipher_mode = MBEDTLS_MODE_CBC;
1165 *key_bits = 128;
1166 *iv_len = 16;
1167 break;
1168 case MBEDTLS_CIPHER_ARIA_256_CBC:
1169 *cipher_mode = MBEDTLS_MODE_CBC;
1170 *key_bits = 256;
1171 *iv_len = 16;
1172 break;
1173 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
1174 *cipher_mode = MBEDTLS_MODE_CBC;
1175 *key_bits = 128;
1176 *iv_len = 16;
1177 break;
1178 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
1179 *cipher_mode = MBEDTLS_MODE_CBC;
1180 *key_bits = 256;
1181 *iv_len = 16;
1182 break;
1183
1184 case MBEDTLS_CIPHER_AES_128_CCM:
1185 *cipher_mode = MBEDTLS_MODE_CCM;
1186 *key_bits = 128;
1187 *iv_len = 12;
1188 break;
1189 case MBEDTLS_CIPHER_AES_192_CCM:
1190 *cipher_mode = MBEDTLS_MODE_CCM;
1191 *key_bits = 192;
1192 *iv_len = 12;
1193 break;
1194 case MBEDTLS_CIPHER_AES_256_CCM:
1195 *cipher_mode = MBEDTLS_MODE_CCM;
1196 *key_bits = 256;
1197 *iv_len = 12;
1198 break;
1199 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
1200 *cipher_mode = MBEDTLS_MODE_CCM;
1201 *key_bits = 128;
1202 *iv_len = 12;
1203 break;
1204 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
1205 *cipher_mode = MBEDTLS_MODE_CCM;
1206 *key_bits = 192;
1207 *iv_len = 12;
1208 break;
1209 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
1210 *cipher_mode = MBEDTLS_MODE_CCM;
1211 *key_bits = 256;
1212 *iv_len = 12;
1213 break;
1214
1215 case MBEDTLS_CIPHER_AES_128_GCM:
1216 *cipher_mode = MBEDTLS_MODE_GCM;
1217 *key_bits = 128;
1218 *iv_len = 12;
1219 break;
1220 case MBEDTLS_CIPHER_AES_192_GCM:
1221 *cipher_mode = MBEDTLS_MODE_GCM;
1222 *key_bits = 192;
1223 *iv_len = 12;
1224 break;
1225 case MBEDTLS_CIPHER_AES_256_GCM:
1226 *cipher_mode = MBEDTLS_MODE_GCM;
1227 *key_bits = 256;
1228 *iv_len = 12;
1229 break;
1230 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
1231 *cipher_mode = MBEDTLS_MODE_GCM;
1232 *key_bits = 128;
1233 *iv_len = 12;
1234 break;
1235 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
1236 *cipher_mode = MBEDTLS_MODE_GCM;
1237 *key_bits = 192;
1238 *iv_len = 12;
1239 break;
1240 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
1241 *cipher_mode = MBEDTLS_MODE_GCM;
1242 *key_bits = 256;
1243 *iv_len = 12;
1244 break;
1245
1246 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
1247 *cipher_mode = MBEDTLS_MODE_CHACHAPOLY;
1248 *key_bits = 256;
1249 *iv_len = 12;
1250 break;
1251
1252 case MBEDTLS_CIPHER_NULL:
1253 *cipher_mode = MBEDTLS_MODE_STREAM;
1254 *key_bits = 0;
1255 *iv_len = 0;
1256 break;
1257
1258 default:
1259 *cipher_mode = MBEDTLS_MODE_NONE;
1260 *key_bits = 0;
1261 *iv_len = 0;
1262 }
1263}
1264
Yanray Wange6afd912022-10-27 12:11:18 +08001265int mbedtls_test_ssl_build_transforms(mbedtls_ssl_transform *t_in,
1266 mbedtls_ssl_transform *t_out,
1267 int cipher_type, int hash_id,
1268 int etm, int tag_mode,
1269 mbedtls_ssl_protocol_version tls_version,
1270 size_t cid0_len,
1271 size_t cid1_len)
1272{
Valerio Setti31ad3a12023-10-27 11:55:02 +02001273 mbedtls_cipher_mode_t cipher_mode = MBEDTLS_MODE_NONE;
1274 size_t key_bits = 0;
Yanray Wange6afd912022-10-27 12:11:18 +08001275 int ret = 0;
1276
1277#if defined(MBEDTLS_USE_PSA_CRYPTO)
1278 psa_key_type_t key_type;
1279 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1280 psa_algorithm_t alg;
Yanray Wange6afd912022-10-27 12:11:18 +08001281 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Valerio Setti3d59ebe2023-10-30 11:56:56 +01001282#else
Valerio Setti31ad3a12023-10-27 11:55:02 +02001283 mbedtls_cipher_info_t const *cipher_info;
Yanray Wange6afd912022-10-27 12:11:18 +08001284#endif
1285
Valerio Setti31ad3a12023-10-27 11:55:02 +02001286 size_t keylen, maclen, ivlen = 0;
Yanray Wange6afd912022-10-27 12:11:18 +08001287 unsigned char *key0 = NULL, *key1 = NULL;
1288 unsigned char *md0 = NULL, *md1 = NULL;
1289 unsigned char iv_enc[16], iv_dec[16];
1290
1291#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
1292 unsigned char cid0[SSL_CID_LEN_MIN];
1293 unsigned char cid1[SSL_CID_LEN_MIN];
1294
1295 mbedtls_test_rnd_std_rand(NULL, cid0, sizeof(cid0));
1296 mbedtls_test_rnd_std_rand(NULL, cid1, sizeof(cid1));
1297#else
1298 ((void) cid0_len);
1299 ((void) cid1_len);
1300#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
1301
1302 maclen = 0;
Valerio Setti31ad3a12023-10-27 11:55:02 +02001303 mbedtls_test_ssl_cipher_info_from_type((mbedtls_cipher_type_t) cipher_type,
1304 &cipher_mode, &key_bits, &ivlen);
Yanray Wange6afd912022-10-27 12:11:18 +08001305
1306 /* Pick keys */
Valerio Setti31ad3a12023-10-27 11:55:02 +02001307 keylen = key_bits / 8;
Yanray Wange6afd912022-10-27 12:11:18 +08001308 /* Allocate `keylen + 1` bytes to ensure that we get
1309 * a non-NULL pointers from `mbedtls_calloc` even if
1310 * `keylen == 0` in the case of the NULL cipher. */
1311 CHK((key0 = mbedtls_calloc(1, keylen + 1)) != NULL);
1312 CHK((key1 = mbedtls_calloc(1, keylen + 1)) != NULL);
1313 memset(key0, 0x1, keylen);
1314 memset(key1, 0x2, keylen);
1315
1316#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti31ad3a12023-10-27 11:55:02 +02001317 /* Pick cipher */
1318 cipher_info = mbedtls_cipher_info_from_type((mbedtls_cipher_type_t) cipher_type);
1319 CHK(cipher_info != NULL);
1320 CHK(mbedtls_cipher_info_get_iv_size(cipher_info) <= 16);
1321 CHK(mbedtls_cipher_info_get_key_bitlen(cipher_info) % 8 == 0);
Valerio Setti3d59ebe2023-10-30 11:56:56 +01001322
Yanray Wange6afd912022-10-27 12:11:18 +08001323 /* Setup cipher contexts */
1324 CHK(mbedtls_cipher_setup(&t_in->cipher_ctx_enc, cipher_info) == 0);
1325 CHK(mbedtls_cipher_setup(&t_in->cipher_ctx_dec, cipher_info) == 0);
1326 CHK(mbedtls_cipher_setup(&t_out->cipher_ctx_enc, cipher_info) == 0);
1327 CHK(mbedtls_cipher_setup(&t_out->cipher_ctx_dec, cipher_info) == 0);
1328
1329#if defined(MBEDTLS_CIPHER_MODE_CBC)
Valerio Setti31ad3a12023-10-27 11:55:02 +02001330 if (cipher_mode == MBEDTLS_MODE_CBC) {
Yanray Wange6afd912022-10-27 12:11:18 +08001331 CHK(mbedtls_cipher_set_padding_mode(&t_in->cipher_ctx_enc,
1332 MBEDTLS_PADDING_NONE) == 0);
1333 CHK(mbedtls_cipher_set_padding_mode(&t_in->cipher_ctx_dec,
1334 MBEDTLS_PADDING_NONE) == 0);
1335 CHK(mbedtls_cipher_set_padding_mode(&t_out->cipher_ctx_enc,
1336 MBEDTLS_PADDING_NONE) == 0);
1337 CHK(mbedtls_cipher_set_padding_mode(&t_out->cipher_ctx_dec,
1338 MBEDTLS_PADDING_NONE) == 0);
1339 }
1340#endif /* MBEDTLS_CIPHER_MODE_CBC */
1341
1342 CHK(mbedtls_cipher_setkey(&t_in->cipher_ctx_enc, key0,
Yanray Wanga8f445e2022-11-03 11:51:59 +08001343 (keylen << 3 > INT_MAX) ? INT_MAX : (int) keylen << 3,
1344 MBEDTLS_ENCRYPT)
1345 == 0);
Yanray Wange6afd912022-10-27 12:11:18 +08001346 CHK(mbedtls_cipher_setkey(&t_in->cipher_ctx_dec, key1,
Yanray Wanga8f445e2022-11-03 11:51:59 +08001347 (keylen << 3 > INT_MAX) ? INT_MAX : (int) keylen << 3,
1348 MBEDTLS_DECRYPT)
1349 == 0);
Yanray Wange6afd912022-10-27 12:11:18 +08001350 CHK(mbedtls_cipher_setkey(&t_out->cipher_ctx_enc, key1,
Yanray Wanga8f445e2022-11-03 11:51:59 +08001351 (keylen << 3 > INT_MAX) ? INT_MAX : (int) keylen << 3,
1352 MBEDTLS_ENCRYPT)
1353 == 0);
Yanray Wange6afd912022-10-27 12:11:18 +08001354 CHK(mbedtls_cipher_setkey(&t_out->cipher_ctx_dec, key0,
Yanray Wanga8f445e2022-11-03 11:51:59 +08001355 (keylen << 3 > INT_MAX) ? INT_MAX : (int) keylen << 3,
1356 MBEDTLS_DECRYPT)
1357 == 0);
Valerio Setti31ad3a12023-10-27 11:55:02 +02001358#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Yanray Wange6afd912022-10-27 12:11:18 +08001359
1360 /* Setup MAC contexts */
1361#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Valerio Setti31ad3a12023-10-27 11:55:02 +02001362 if (cipher_mode == MBEDTLS_MODE_CBC ||
1363 cipher_mode == MBEDTLS_MODE_STREAM) {
Yanray Wange6afd912022-10-27 12:11:18 +08001364#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Agathiyan Bragadeesh2f017a82023-07-12 11:21:54 +01001365 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 +08001366 CHK(md_info != NULL);
1367#endif
Agathiyan Bragadeesh2f017a82023-07-12 11:21:54 +01001368 maclen = mbedtls_md_get_size_from_type((mbedtls_md_type_t) hash_id);
Yanray Wange6afd912022-10-27 12:11:18 +08001369 CHK(maclen != 0);
1370 /* Pick hash keys */
1371 CHK((md0 = mbedtls_calloc(1, maclen)) != NULL);
1372 CHK((md1 = mbedtls_calloc(1, maclen)) != NULL);
1373 memset(md0, 0x5, maclen);
1374 memset(md1, 0x6, maclen);
1375
1376#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02001377 alg = mbedtls_md_psa_alg_from_type(hash_id);
Yanray Wange6afd912022-10-27 12:11:18 +08001378
1379 CHK(alg != 0);
1380
1381 t_out->psa_mac_alg = PSA_ALG_HMAC(alg);
1382 t_in->psa_mac_alg = PSA_ALG_HMAC(alg);
1383 t_in->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
1384 t_out->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
1385 t_in->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
1386 t_out->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
1387
1388 psa_reset_key_attributes(&attributes);
1389 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
1390 psa_set_key_algorithm(&attributes, PSA_ALG_HMAC(alg));
1391 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
1392
1393 CHK(psa_import_key(&attributes,
1394 md0, maclen,
1395 &t_in->psa_mac_enc) == PSA_SUCCESS);
1396
1397 CHK(psa_import_key(&attributes,
1398 md1, maclen,
1399 &t_out->psa_mac_enc) == PSA_SUCCESS);
1400
Valerio Setti31ad3a12023-10-27 11:55:02 +02001401 if (cipher_mode == MBEDTLS_MODE_STREAM ||
Yanray Wange6afd912022-10-27 12:11:18 +08001402 etm == MBEDTLS_SSL_ETM_DISABLED) {
1403 /* mbedtls_ct_hmac() requires the key to be exportable */
1404 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT |
1405 PSA_KEY_USAGE_VERIFY_HASH);
1406 } else {
1407 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
1408 }
1409
1410 CHK(psa_import_key(&attributes,
1411 md1, maclen,
1412 &t_in->psa_mac_dec) == PSA_SUCCESS);
1413
1414 CHK(psa_import_key(&attributes,
1415 md0, maclen,
1416 &t_out->psa_mac_dec) == PSA_SUCCESS);
1417#else
1418 CHK(mbedtls_md_setup(&t_out->md_ctx_enc, md_info, 1) == 0);
1419 CHK(mbedtls_md_setup(&t_out->md_ctx_dec, md_info, 1) == 0);
1420 CHK(mbedtls_md_setup(&t_in->md_ctx_enc, md_info, 1) == 0);
1421 CHK(mbedtls_md_setup(&t_in->md_ctx_dec, md_info, 1) == 0);
1422
1423 CHK(mbedtls_md_hmac_starts(&t_in->md_ctx_enc,
1424 md0, maclen) == 0);
1425 CHK(mbedtls_md_hmac_starts(&t_in->md_ctx_dec,
1426 md1, maclen) == 0);
1427 CHK(mbedtls_md_hmac_starts(&t_out->md_ctx_enc,
1428 md1, maclen) == 0);
1429 CHK(mbedtls_md_hmac_starts(&t_out->md_ctx_dec,
1430 md0, maclen) == 0);
1431#endif
1432 }
1433#else
1434 ((void) hash_id);
1435#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
1436
1437
1438 /* Pick IV's (regardless of whether they
1439 * are being used by the transform). */
Yanray Wange6afd912022-10-27 12:11:18 +08001440 memset(iv_enc, 0x3, sizeof(iv_enc));
1441 memset(iv_dec, 0x4, sizeof(iv_dec));
1442
1443 /*
1444 * Setup transforms
1445 */
1446
1447#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
1448 defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
1449 t_out->encrypt_then_mac = etm;
1450 t_in->encrypt_then_mac = etm;
1451#else
1452 ((void) etm);
1453#endif
1454
1455 t_out->tls_version = tls_version;
1456 t_in->tls_version = tls_version;
1457 t_out->ivlen = ivlen;
1458 t_in->ivlen = ivlen;
1459
Valerio Setti31ad3a12023-10-27 11:55:02 +02001460 switch (cipher_mode) {
Yanray Wange6afd912022-10-27 12:11:18 +08001461 case MBEDTLS_MODE_GCM:
1462 case MBEDTLS_MODE_CCM:
1463#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1464 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
1465 t_out->fixed_ivlen = 12;
1466 t_in->fixed_ivlen = 12;
1467 } else
1468#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1469 {
1470 t_out->fixed_ivlen = 4;
1471 t_in->fixed_ivlen = 4;
1472 }
1473 t_out->maclen = 0;
1474 t_in->maclen = 0;
1475 switch (tag_mode) {
1476 case 0: /* Full tag */
1477 t_out->taglen = 16;
1478 t_in->taglen = 16;
1479 break;
1480 case 1: /* Partial tag */
1481 t_out->taglen = 8;
1482 t_in->taglen = 8;
1483 break;
1484 default:
1485 ret = 1;
1486 goto cleanup;
1487 }
1488 break;
1489
1490 case MBEDTLS_MODE_CHACHAPOLY:
1491 t_out->fixed_ivlen = 12;
1492 t_in->fixed_ivlen = 12;
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_STREAM:
1511 case MBEDTLS_MODE_CBC:
1512 t_out->fixed_ivlen = 0; /* redundant, must be 0 */
1513 t_in->fixed_ivlen = 0; /* redundant, must be 0 */
1514 t_out->taglen = 0;
1515 t_in->taglen = 0;
1516 switch (tag_mode) {
1517 case 0: /* Full tag */
1518 t_out->maclen = maclen;
1519 t_in->maclen = maclen;
1520 break;
1521 default:
1522 ret = 1;
1523 goto cleanup;
1524 }
1525 break;
1526 default:
1527 ret = 1;
1528 goto cleanup;
1529 break;
1530 }
1531
1532 /* Setup IV's */
1533
1534 memcpy(&t_in->iv_dec, iv_dec, sizeof(iv_dec));
1535 memcpy(&t_in->iv_enc, iv_enc, sizeof(iv_enc));
1536 memcpy(&t_out->iv_dec, iv_enc, sizeof(iv_enc));
1537 memcpy(&t_out->iv_enc, iv_dec, sizeof(iv_dec));
1538
1539#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
1540 /* Add CID */
1541 memcpy(&t_in->in_cid, cid0, cid0_len);
1542 memcpy(&t_in->out_cid, cid1, cid1_len);
Yanray Wanga8f445e2022-11-03 11:51:59 +08001543 t_in->in_cid_len = (uint8_t) cid0_len;
1544 t_in->out_cid_len = (uint8_t) cid1_len;
Yanray Wange6afd912022-10-27 12:11:18 +08001545 memcpy(&t_out->in_cid, cid1, cid1_len);
1546 memcpy(&t_out->out_cid, cid0, cid0_len);
Yanray Wanga8f445e2022-11-03 11:51:59 +08001547 t_out->in_cid_len = (uint8_t) cid1_len;
1548 t_out->out_cid_len = (uint8_t) cid0_len;
Yanray Wange6afd912022-10-27 12:11:18 +08001549#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
1550
1551#if defined(MBEDTLS_USE_PSA_CRYPTO)
1552 status = mbedtls_ssl_cipher_to_psa(cipher_type,
1553 t_in->taglen,
1554 &alg,
1555 &key_type,
1556 &key_bits);
1557
1558 if (status != PSA_SUCCESS) {
1559 ret = PSA_TO_MBEDTLS_ERR(status);
1560 goto cleanup;
1561 }
1562
1563 t_in->psa_alg = alg;
1564 t_out->psa_alg = alg;
1565
1566 if (alg != MBEDTLS_SSL_NULL_CIPHER) {
1567 psa_reset_key_attributes(&attributes);
1568 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
1569 psa_set_key_algorithm(&attributes, alg);
1570 psa_set_key_type(&attributes, key_type);
1571
1572 status = psa_import_key(&attributes,
1573 key0,
1574 PSA_BITS_TO_BYTES(key_bits),
1575 &t_in->psa_key_enc);
1576
1577 if (status != PSA_SUCCESS) {
1578 ret = PSA_TO_MBEDTLS_ERR(status);
1579 goto cleanup;
1580 }
1581
1582 status = psa_import_key(&attributes,
1583 key1,
1584 PSA_BITS_TO_BYTES(key_bits),
1585 &t_out->psa_key_enc);
1586
1587 if (status != PSA_SUCCESS) {
1588 ret = PSA_TO_MBEDTLS_ERR(status);
1589 goto cleanup;
1590 }
1591
1592 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
1593
1594 status = psa_import_key(&attributes,
1595 key1,
1596 PSA_BITS_TO_BYTES(key_bits),
1597 &t_in->psa_key_dec);
1598
1599 if (status != PSA_SUCCESS) {
1600 ret = PSA_TO_MBEDTLS_ERR(status);
1601 goto cleanup;
1602 }
1603
1604 status = psa_import_key(&attributes,
1605 key0,
1606 PSA_BITS_TO_BYTES(key_bits),
1607 &t_out->psa_key_dec);
1608
1609 if (status != PSA_SUCCESS) {
1610 ret = PSA_TO_MBEDTLS_ERR(status);
1611 goto cleanup;
1612 }
1613 }
1614#endif /* MBEDTLS_USE_PSA_CRYPTO */
1615
1616cleanup:
1617
1618 mbedtls_free(key0);
1619 mbedtls_free(key1);
1620
1621 mbedtls_free(md0);
1622 mbedtls_free(md1);
1623
1624 return ret;
1625}
1626
Gilles Peskine9099d3f2023-09-18 13:11:50 +02001627#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
1628int mbedtls_test_ssl_prepare_record_mac(mbedtls_record *record,
1629 mbedtls_ssl_transform *transform_out)
1630{
1631#if defined(MBEDTLS_USE_PSA_CRYPTO)
1632 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
1633#endif
1634
1635 /* Serialized version of record header for MAC purposes */
1636 unsigned char add_data[13];
1637 memcpy(add_data, record->ctr, 8);
1638 add_data[8] = record->type;
1639 add_data[9] = record->ver[0];
1640 add_data[10] = record->ver[1];
1641 add_data[11] = (record->data_len >> 8) & 0xff;
1642 add_data[12] = (record->data_len >> 0) & 0xff;
1643
1644 /* MAC with additional data */
1645#if defined(MBEDTLS_USE_PSA_CRYPTO)
1646 size_t sign_mac_length = 0;
1647 TEST_EQUAL(PSA_SUCCESS, psa_mac_sign_setup(&operation,
1648 transform_out->psa_mac_enc,
1649 transform_out->psa_mac_alg));
1650 TEST_EQUAL(PSA_SUCCESS, psa_mac_update(&operation, add_data, 13));
1651 TEST_EQUAL(PSA_SUCCESS, psa_mac_update(&operation,
1652 record->buf + record->data_offset,
1653 record->data_len));
1654 /* Use a temporary buffer for the MAC, because with the truncated HMAC
1655 * extension, there might not be enough room in the record for the
1656 * full-length MAC. */
1657 unsigned char mac[PSA_HASH_MAX_SIZE];
1658 TEST_EQUAL(PSA_SUCCESS, psa_mac_sign_finish(&operation,
1659 mac, sizeof(mac),
1660 &sign_mac_length));
1661#else
1662 TEST_EQUAL(0, mbedtls_md_hmac_update(&transform_out->md_ctx_enc, add_data, 13));
1663 TEST_EQUAL(0, mbedtls_md_hmac_update(&transform_out->md_ctx_enc,
1664 record->buf + record->data_offset,
1665 record->data_len));
1666 /* Use a temporary buffer for the MAC, because with the truncated HMAC
1667 * extension, there might not be enough room in the record for the
1668 * full-length MAC. */
1669 unsigned char mac[MBEDTLS_MD_MAX_SIZE];
1670 TEST_EQUAL(0, mbedtls_md_hmac_finish(&transform_out->md_ctx_enc, mac));
1671#endif
1672 memcpy(record->buf + record->data_offset + record->data_len, mac, transform_out->maclen);
1673 record->data_len += transform_out->maclen;
1674
1675 return 0;
1676
1677exit:
1678#if defined(MBEDTLS_USE_PSA_CRYPTO)
1679 psa_mac_abort(&operation);
1680#endif
1681 return -1;
1682}
1683#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
1684
Jerry Yu28547c42023-10-31 14:42:50 +08001685#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Yanray Wange6afd912022-10-27 12:11:18 +08001686int mbedtls_test_ssl_tls12_populate_session(mbedtls_ssl_session *session,
1687 int ticket_len,
Ronald Cron7b1921a2023-11-23 12:31:56 +01001688 int endpoint_type,
Yanray Wange6afd912022-10-27 12:11:18 +08001689 const char *crt_file)
1690{
Ronald Cronc57f86e2023-11-22 09:50:01 +01001691 (void) ticket_len;
1692
Yanray Wange6afd912022-10-27 12:11:18 +08001693#if defined(MBEDTLS_HAVE_TIME)
1694 session->start = mbedtls_time(NULL) - 42;
1695#endif
1696 session->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Ronald Cronc7fa82e2024-02-09 09:33:09 +01001697
1698 TEST_ASSERT(endpoint_type == MBEDTLS_SSL_IS_CLIENT ||
1699 endpoint_type == MBEDTLS_SSL_IS_SERVER);
1700
1701 session->endpoint = endpoint_type;
Yanray Wange6afd912022-10-27 12:11:18 +08001702 session->ciphersuite = 0xabcd;
1703 session->id_len = sizeof(session->id);
1704 memset(session->id, 66, session->id_len);
1705 memset(session->master, 17, sizeof(session->master));
1706
1707#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) && defined(MBEDTLS_FS_IO)
1708 if (crt_file != NULL && strlen(crt_file) != 0) {
1709 mbedtls_x509_crt tmp_crt;
1710 int ret;
1711
1712 mbedtls_x509_crt_init(&tmp_crt);
1713 ret = mbedtls_x509_crt_parse_file(&tmp_crt, crt_file);
1714 if (ret != 0) {
1715 return ret;
1716 }
1717
1718#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1719 /* Move temporary CRT. */
1720 session->peer_cert = mbedtls_calloc(1, sizeof(*session->peer_cert));
1721 if (session->peer_cert == NULL) {
1722 return -1;
1723 }
1724 *session->peer_cert = tmp_crt;
1725 memset(&tmp_crt, 0, sizeof(tmp_crt));
1726#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
1727 /* Calculate digest of temporary CRT. */
1728 session->peer_cert_digest =
1729 mbedtls_calloc(1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN);
1730 if (session->peer_cert_digest == NULL) {
1731 return -1;
1732 }
1733
1734#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02001735 psa_algorithm_t psa_alg = mbedtls_md_psa_alg_from_type(
Yanray Wange6afd912022-10-27 12:11:18 +08001736 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE);
1737 size_t hash_size = 0;
1738 psa_status_t status = psa_hash_compute(
1739 psa_alg, tmp_crt.raw.p,
1740 tmp_crt.raw.len,
1741 session->peer_cert_digest,
1742 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN,
1743 &hash_size);
1744 ret = PSA_TO_MBEDTLS_ERR(status);
1745#else
1746 ret = mbedtls_md(mbedtls_md_info_from_type(
1747 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE),
1748 tmp_crt.raw.p, tmp_crt.raw.len,
1749 session->peer_cert_digest);
1750#endif /* MBEDTLS_USE_PSA_CRYPTO */
1751 if (ret != 0) {
1752 return ret;
1753 }
1754 session->peer_cert_digest_type =
1755 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
1756 session->peer_cert_digest_len =
1757 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
1758#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
1759
1760 mbedtls_x509_crt_free(&tmp_crt);
1761 }
1762#else /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED && MBEDTLS_FS_IO */
1763 (void) crt_file;
1764#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED && MBEDTLS_FS_IO */
1765 session->verify_result = 0xdeadbeef;
1766
Ronald Cronc57f86e2023-11-22 09:50:01 +01001767#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1768#if defined(MBEDTLS_SSL_CLI_C)
Yanray Wange6afd912022-10-27 12:11:18 +08001769 if (ticket_len != 0) {
1770 session->ticket = mbedtls_calloc(1, ticket_len);
1771 if (session->ticket == NULL) {
1772 return -1;
1773 }
1774 memset(session->ticket, 33, ticket_len);
1775 }
1776 session->ticket_len = ticket_len;
1777 session->ticket_lifetime = 86401;
Ronald Cronc57f86e2023-11-22 09:50:01 +01001778#endif /* MBEDTLS_SSL_CLI_C */
1779
1780#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_HAVE_TIME)
1781 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
1782 session->ticket_creation_time = mbedtls_ms_time() - 42;
1783 }
Yanray Wange6afd912022-10-27 12:11:18 +08001784#endif
Ronald Cronc57f86e2023-11-22 09:50:01 +01001785#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Yanray Wange6afd912022-10-27 12:11:18 +08001786
1787#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1788 session->mfl_code = 1;
1789#endif
1790#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1791 session->encrypt_then_mac = 1;
1792#endif
1793
Ronald Cronc7fa82e2024-02-09 09:33:09 +01001794exit:
Yanray Wange6afd912022-10-27 12:11:18 +08001795 return 0;
1796}
Jerry Yu28547c42023-10-31 14:42:50 +08001797#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Yanray Wange6afd912022-10-27 12:11:18 +08001798
1799#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1800int mbedtls_test_ssl_tls13_populate_session(mbedtls_ssl_session *session,
1801 int ticket_len,
1802 int endpoint_type)
1803{
1804 ((void) ticket_len);
1805 session->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
1806 session->endpoint = endpoint_type == MBEDTLS_SSL_IS_CLIENT ?
1807 MBEDTLS_SSL_IS_CLIENT : MBEDTLS_SSL_IS_SERVER;
1808 session->ciphersuite = 0xabcd;
Ronald Cron18b92a12024-03-26 10:15:08 +01001809
1810#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Yanray Wange6afd912022-10-27 12:11:18 +08001811 session->ticket_age_add = 0x87654321;
1812 session->ticket_flags = 0x7;
Yanray Wange6afd912022-10-27 12:11:18 +08001813 session->resumption_key_len = 32;
1814 memset(session->resumption_key, 0x99, sizeof(session->resumption_key));
Yanray Wange6afd912022-10-27 12:11:18 +08001815#endif
1816
Ronald Cron18b92a12024-03-26 10:15:08 +01001817#if defined(MBEDTLS_SSL_SRV_C)
1818 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
1819#if defined(MBEDTLS_SSL_SESSION_TICKETS)
1820#if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN)
1821 int ret = mbedtls_ssl_session_set_ticket_alpn(session, "ALPNExample");
1822 if (ret != 0) {
1823 return -1;
1824 }
1825#endif
1826#if defined(MBEDTLS_HAVE_TIME)
1827 session->ticket_creation_time = mbedtls_ms_time() - 42;
1828#endif
1829#endif /* MBEDTLS_SSL_SESSION_TICKETS */
1830 }
1831#endif /* MBEDTLS_SSL_SRV_C */
1832
Yanray Wange6afd912022-10-27 12:11:18 +08001833#if defined(MBEDTLS_SSL_CLI_C)
1834 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Ronald Cron18b92a12024-03-26 10:15:08 +01001835#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Yanray Wange6afd912022-10-27 12:11:18 +08001836#if defined(MBEDTLS_HAVE_TIME)
Jerry Yu342a5552023-11-10 14:23:39 +08001837 session->ticket_reception_time = mbedtls_ms_time() - 40;
Yanray Wange6afd912022-10-27 12:11:18 +08001838#endif
1839 session->ticket_lifetime = 0xfedcba98;
1840
1841 session->ticket_len = ticket_len;
1842 if (ticket_len != 0) {
1843 session->ticket = mbedtls_calloc(1, ticket_len);
1844 if (session->ticket == NULL) {
1845 return -1;
1846 }
1847 memset(session->ticket, 33, ticket_len);
1848 }
Ronald Cron8d15e012024-03-27 09:30:13 +01001849#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1850 char hostname[] = "hostname example";
1851 session->hostname = mbedtls_calloc(1, sizeof(hostname));
1852 if (session->hostname == NULL) {
1853 return -1;
1854 }
1855 memcpy(session->hostname, hostname, sizeof(hostname));
1856#endif
Ronald Cron18b92a12024-03-26 10:15:08 +01001857#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Yanray Wange6afd912022-10-27 12:11:18 +08001858 }
1859#endif /* MBEDTLS_SSL_CLI_C */
1860
Ronald Cron18b92a12024-03-26 10:15:08 +01001861#if defined(MBEDTLS_SSL_EARLY_DATA)
1862 session->max_early_data_size = 0x87654321;
1863#endif /* MBEDTLS_SSL_EARLY_DATA */
1864
Waleed Elmelegy049cd302023-12-20 17:28:31 +00001865#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
1866 session->record_size_limit = 2048;
1867#endif
1868
Yanray Wange6afd912022-10-27 12:11:18 +08001869 return 0;
1870}
1871#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1872
Yanray Wangb088bfc2023-03-16 12:15:49 +08001873int mbedtls_test_ssl_exchange_data(
1874 mbedtls_ssl_context *ssl_1,
1875 int msg_len_1, const int expected_fragments_1,
1876 mbedtls_ssl_context *ssl_2,
1877 int msg_len_2, const int expected_fragments_2)
Yanray Wange6afd912022-10-27 12:11:18 +08001878{
1879 unsigned char *msg_buf_1 = malloc(msg_len_1);
1880 unsigned char *msg_buf_2 = malloc(msg_len_2);
1881 unsigned char *in_buf_1 = malloc(msg_len_2);
1882 unsigned char *in_buf_2 = malloc(msg_len_1);
1883 int msg_type, ret = -1;
1884
1885 /* Perform this test with two message types. At first use a message
1886 * consisting of only 0x00 for the client and only 0xFF for the server.
1887 * At the second time use message with generated data */
1888 for (msg_type = 0; msg_type < 2; msg_type++) {
1889 int written_1 = 0;
1890 int written_2 = 0;
1891 int read_1 = 0;
1892 int read_2 = 0;
1893 int fragments_1 = 0;
1894 int fragments_2 = 0;
1895
1896 if (msg_type == 0) {
1897 memset(msg_buf_1, 0x00, msg_len_1);
1898 memset(msg_buf_2, 0xff, msg_len_2);
1899 } else {
1900 int i, j = 0;
1901 for (i = 0; i < msg_len_1; i++) {
1902 msg_buf_1[i] = j++ & 0xFF;
1903 }
1904 for (i = 0; i < msg_len_2; i++) {
1905 msg_buf_2[i] = (j -= 5) & 0xFF;
1906 }
1907 }
1908
1909 while (read_1 < msg_len_2 || read_2 < msg_len_1) {
1910 /* ssl_1 sending */
1911 if (msg_len_1 > written_1) {
1912 ret = mbedtls_ssl_write_fragment(ssl_1, msg_buf_1,
1913 msg_len_1, &written_1,
1914 expected_fragments_1);
1915 if (expected_fragments_1 == 0) {
1916 /* This error is expected when the message is too large and
1917 * cannot be fragmented */
1918 TEST_ASSERT(ret == MBEDTLS_ERR_SSL_BAD_INPUT_DATA);
1919 msg_len_1 = 0;
1920 } else {
1921 TEST_ASSERT(ret == 0);
1922 }
1923 }
1924
1925 /* ssl_2 sending */
1926 if (msg_len_2 > written_2) {
1927 ret = mbedtls_ssl_write_fragment(ssl_2, msg_buf_2,
1928 msg_len_2, &written_2,
1929 expected_fragments_2);
1930 if (expected_fragments_2 == 0) {
1931 /* This error is expected when the message is too large and
1932 * cannot be fragmented */
1933 TEST_ASSERT(ret == MBEDTLS_ERR_SSL_BAD_INPUT_DATA);
1934 msg_len_2 = 0;
1935 } else {
1936 TEST_ASSERT(ret == 0);
1937 }
1938 }
1939
1940 /* ssl_1 reading */
1941 if (read_1 < msg_len_2) {
1942 ret = mbedtls_ssl_read_fragment(ssl_1, in_buf_1,
1943 msg_len_2, &read_1,
1944 &fragments_2,
1945 expected_fragments_2);
1946 TEST_ASSERT(ret == 0);
1947 }
1948
1949 /* ssl_2 reading */
1950 if (read_2 < msg_len_1) {
1951 ret = mbedtls_ssl_read_fragment(ssl_2, in_buf_2,
1952 msg_len_1, &read_2,
1953 &fragments_1,
1954 expected_fragments_1);
1955 TEST_ASSERT(ret == 0);
1956 }
1957 }
1958
1959 ret = -1;
1960 TEST_ASSERT(0 == memcmp(msg_buf_1, in_buf_2, msg_len_1));
1961 TEST_ASSERT(0 == memcmp(msg_buf_2, in_buf_1, msg_len_2));
1962 TEST_ASSERT(fragments_1 == expected_fragments_1);
1963 TEST_ASSERT(fragments_2 == expected_fragments_2);
1964 }
1965
1966 ret = 0;
1967
1968exit:
1969 free(msg_buf_1);
1970 free(in_buf_1);
1971 free(msg_buf_2);
1972 free(in_buf_2);
1973
1974 return ret;
1975}
1976
1977/*
1978 * Perform data exchanging between \p ssl_1 and \p ssl_2. Both of endpoints
1979 * must be initialized and connected beforehand.
1980 *
1981 * \retval 0 on success, otherwise error code.
1982 */
Yanray Wangead70c82023-03-16 12:04:49 +08001983#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) && \
1984 (defined(MBEDTLS_SSL_RENEGOTIATION) || \
1985 defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH))
1986static int exchange_data(mbedtls_ssl_context *ssl_1,
1987 mbedtls_ssl_context *ssl_2)
Yanray Wange6afd912022-10-27 12:11:18 +08001988{
Yanray Wangb088bfc2023-03-16 12:15:49 +08001989 return mbedtls_test_ssl_exchange_data(ssl_1, 256, 1,
1990 ssl_2, 256, 1);
Yanray Wange6afd912022-10-27 12:11:18 +08001991}
Yanray Wangead70c82023-03-16 12:04:49 +08001992#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED &&
1993 (MBEDTLS_SSL_RENEGOTIATION ||
1994 MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) */
Yanray Wange6afd912022-10-27 12:11:18 +08001995
1996#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
1997static int check_ssl_version(
1998 mbedtls_ssl_protocol_version expected_negotiated_version,
1999 const mbedtls_ssl_context *ssl)
2000{
2001 const char *version_string = mbedtls_ssl_get_version(ssl);
2002 mbedtls_ssl_protocol_version version_number =
2003 mbedtls_ssl_get_version_number(ssl);
2004
2005 TEST_EQUAL(ssl->tls_version, expected_negotiated_version);
2006
2007 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
2008 TEST_EQUAL(version_string[0], 'D');
2009 ++version_string;
2010 }
2011
2012 switch (expected_negotiated_version) {
2013 case MBEDTLS_SSL_VERSION_TLS1_2:
2014 TEST_EQUAL(version_number, MBEDTLS_SSL_VERSION_TLS1_2);
2015 TEST_ASSERT(strcmp(version_string, "TLSv1.2") == 0);
2016 break;
2017
2018 case MBEDTLS_SSL_VERSION_TLS1_3:
2019 TEST_EQUAL(version_number, MBEDTLS_SSL_VERSION_TLS1_3);
2020 TEST_ASSERT(strcmp(version_string, "TLSv1.3") == 0);
2021 break;
2022
2023 default:
Agathiyan Bragadeeshdc28a5a2023-07-18 11:45:28 +01002024 TEST_FAIL(
Agathiyan Bragadeeshebb40bc2023-07-14 17:28:27 +01002025 "Version check not implemented for this protocol version");
Yanray Wange6afd912022-10-27 12:11:18 +08002026 }
2027
2028 return 1;
2029
2030exit:
2031 return 0;
2032}
2033#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
2034
Yanray Wange6afd912022-10-27 12:11:18 +08002035#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
2036void mbedtls_test_ssl_perform_handshake(
2037 mbedtls_test_handshake_test_options *options)
2038{
2039 /* forced_ciphersuite needs to last until the end of the handshake */
2040 int forced_ciphersuite[2];
2041 enum { BUFFSIZE = 17000 };
2042 mbedtls_test_ssl_endpoint client, server;
2043#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
2044 const char *psk_identity = "foo";
2045#endif
2046#if defined(MBEDTLS_TIMING_C)
2047 mbedtls_timing_delay_context timer_client, timer_server;
2048#endif
2049#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
2050 unsigned char *context_buf = NULL;
2051 size_t context_buf_len;
2052#endif
2053#if defined(MBEDTLS_SSL_RENEGOTIATION)
2054 int ret = -1;
2055#endif
2056 int expected_handshake_result = options->expected_handshake_result;
2057
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +01002058 MD_OR_USE_PSA_INIT();
Yanray Wange6afd912022-10-27 12:11:18 +08002059 mbedtls_platform_zeroize(&client, sizeof(client));
2060 mbedtls_platform_zeroize(&server, sizeof(server));
2061 mbedtls_test_ssl_message_queue server_queue, client_queue;
2062 mbedtls_test_message_socket_context server_context, client_context;
2063 mbedtls_test_message_socket_init(&server_context);
2064 mbedtls_test_message_socket_init(&client_context);
2065
Ronald Cronec3408d2024-01-16 17:50:40 +01002066#if defined(MBEDTLS_DEBUG_C)
2067 if (options->cli_log_fun || options->srv_log_fun) {
2068 mbedtls_debug_set_threshold(4);
2069 }
2070#endif
2071
Yanray Wange6afd912022-10-27 12:11:18 +08002072 /* Client side */
2073 if (options->dtls != 0) {
2074 TEST_ASSERT(mbedtls_test_ssl_endpoint_init(&client,
2075 MBEDTLS_SSL_IS_CLIENT,
2076 options, &client_context,
2077 &client_queue,
Ronald Cronfb536472024-01-26 14:55:25 +01002078 &server_queue) == 0);
Yanray Wange6afd912022-10-27 12:11:18 +08002079#if defined(MBEDTLS_TIMING_C)
2080 mbedtls_ssl_set_timer_cb(&client.ssl, &timer_client,
2081 mbedtls_timing_set_delay,
2082 mbedtls_timing_get_delay);
2083#endif
2084 } else {
2085 TEST_ASSERT(mbedtls_test_ssl_endpoint_init(&client,
2086 MBEDTLS_SSL_IS_CLIENT,
2087 options, NULL, NULL,
Ronald Cronfb536472024-01-26 14:55:25 +01002088 NULL) == 0);
Yanray Wange6afd912022-10-27 12:11:18 +08002089 }
2090
Yanray Wange6afd912022-10-27 12:11:18 +08002091 if (strlen(options->cipher) > 0) {
2092 set_ciphersuite(&client.conf, options->cipher, forced_ciphersuite);
2093 }
2094
Yanray Wange6afd912022-10-27 12:11:18 +08002095 /* Server side */
2096 if (options->dtls != 0) {
2097 TEST_ASSERT(mbedtls_test_ssl_endpoint_init(&server,
2098 MBEDTLS_SSL_IS_SERVER,
2099 options, &server_context,
2100 &server_queue,
Ronald Cronfb536472024-01-26 14:55:25 +01002101 &client_queue) == 0);
Yanray Wange6afd912022-10-27 12:11:18 +08002102#if defined(MBEDTLS_TIMING_C)
2103 mbedtls_ssl_set_timer_cb(&server.ssl, &timer_server,
2104 mbedtls_timing_set_delay,
2105 mbedtls_timing_get_delay);
2106#endif
2107 } else {
2108 TEST_ASSERT(mbedtls_test_ssl_endpoint_init(&server,
2109 MBEDTLS_SSL_IS_SERVER,
Ronald Cronfb536472024-01-26 14:55:25 +01002110 options, NULL, NULL,
Yanray Wange6afd912022-10-27 12:11:18 +08002111 NULL) == 0);
2112 }
2113
2114 mbedtls_ssl_conf_authmode(&server.conf, options->srv_auth_mode);
2115
Yanray Wange6afd912022-10-27 12:11:18 +08002116#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2117 TEST_ASSERT(mbedtls_ssl_conf_max_frag_len(&(server.conf),
2118 (unsigned char) options->mfl)
2119 == 0);
2120 TEST_ASSERT(mbedtls_ssl_conf_max_frag_len(&(client.conf),
2121 (unsigned char) options->mfl)
2122 == 0);
2123#else
2124 TEST_ASSERT(MBEDTLS_SSL_MAX_FRAG_LEN_NONE == options->mfl);
2125#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2126
2127#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
2128 if (options->psk_str != NULL && options->psk_str->len > 0) {
2129 TEST_ASSERT(mbedtls_ssl_conf_psk(
2130 &client.conf, options->psk_str->x,
2131 options->psk_str->len,
2132 (const unsigned char *) psk_identity,
2133 strlen(psk_identity)) == 0);
2134
2135 TEST_ASSERT(mbedtls_ssl_conf_psk(
2136 &server.conf, options->psk_str->x,
2137 options->psk_str->len,
2138 (const unsigned char *) psk_identity,
2139 strlen(psk_identity)) == 0);
2140#if defined(MBEDTLS_SSL_SRV_C)
2141 mbedtls_ssl_conf_psk_cb(&server.conf, psk_dummy_callback, NULL);
2142#endif
2143 }
2144#endif
2145#if defined(MBEDTLS_SSL_RENEGOTIATION)
2146 if (options->renegotiate) {
2147 mbedtls_ssl_conf_renegotiation(&(server.conf),
2148 MBEDTLS_SSL_RENEGOTIATION_ENABLED);
2149 mbedtls_ssl_conf_renegotiation(&(client.conf),
2150 MBEDTLS_SSL_RENEGOTIATION_ENABLED);
2151
2152 mbedtls_ssl_conf_legacy_renegotiation(&(server.conf),
2153 options->legacy_renegotiation);
2154 mbedtls_ssl_conf_legacy_renegotiation(&(client.conf),
2155 options->legacy_renegotiation);
2156 }
2157#endif /* MBEDTLS_SSL_RENEGOTIATION */
2158
Yanray Wange6afd912022-10-27 12:11:18 +08002159 TEST_ASSERT(mbedtls_test_mock_socket_connect(&(client.socket),
2160 &(server.socket),
2161 BUFFSIZE) == 0);
2162
2163#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2164 if (options->resize_buffers != 0) {
2165 /* Ensure that the buffer sizes are appropriate before resizes */
2166 TEST_ASSERT(client.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN);
2167 TEST_ASSERT(client.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN);
2168 TEST_ASSERT(server.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN);
2169 TEST_ASSERT(server.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN);
2170 }
2171#endif
2172
2173 if (options->expected_negotiated_version == MBEDTLS_SSL_VERSION_UNKNOWN) {
2174 expected_handshake_result = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
2175 }
2176
2177 TEST_ASSERT(mbedtls_test_move_handshake_to_state(&(client.ssl),
2178 &(server.ssl),
2179 MBEDTLS_SSL_HANDSHAKE_OVER)
2180 == expected_handshake_result);
2181
2182 if (expected_handshake_result != 0) {
2183 /* Connection will have failed by this point, skip to cleanup */
2184 goto exit;
2185 }
2186
2187 TEST_ASSERT(mbedtls_ssl_is_handshake_over(&client.ssl) == 1);
2188
2189 /* Make sure server state is moved to HANDSHAKE_OVER also. */
2190 TEST_EQUAL(mbedtls_test_move_handshake_to_state(&(server.ssl),
2191 &(client.ssl),
2192 MBEDTLS_SSL_HANDSHAKE_OVER),
2193 0);
2194
2195 TEST_ASSERT(mbedtls_ssl_is_handshake_over(&server.ssl) == 1);
2196 /* Check that both sides have negotiated the expected version. */
2197 mbedtls_test_set_step(0);
2198 if (!check_ssl_version(options->expected_negotiated_version,
2199 &client.ssl)) {
2200 goto exit;
2201 }
2202
2203 mbedtls_test_set_step(1);
2204 if (!check_ssl_version(options->expected_negotiated_version,
2205 &server.ssl)) {
2206 goto exit;
2207 }
2208
2209 if (options->expected_ciphersuite != 0) {
2210 TEST_EQUAL(server.ssl.session->ciphersuite,
2211 options->expected_ciphersuite);
2212 }
2213
2214#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2215 if (options->resize_buffers != 0) {
2216 /* A server, when using DTLS, might delay a buffer resize to happen
2217 * after it receives a message, so we force it. */
2218 TEST_ASSERT(exchange_data(&(client.ssl), &(server.ssl)) == 0);
2219
2220 TEST_ASSERT(client.ssl.out_buf_len ==
2221 mbedtls_ssl_get_output_buflen(&client.ssl));
2222 TEST_ASSERT(client.ssl.in_buf_len ==
2223 mbedtls_ssl_get_input_buflen(&client.ssl));
2224 TEST_ASSERT(server.ssl.out_buf_len ==
2225 mbedtls_ssl_get_output_buflen(&server.ssl));
2226 TEST_ASSERT(server.ssl.in_buf_len ==
2227 mbedtls_ssl_get_input_buflen(&server.ssl));
2228 }
2229#endif
2230
2231 if (options->cli_msg_len != 0 || options->srv_msg_len != 0) {
2232 /* Start data exchanging test */
Yanray Wangb088bfc2023-03-16 12:15:49 +08002233 TEST_ASSERT(mbedtls_test_ssl_exchange_data(
2234 &(client.ssl), options->cli_msg_len,
2235 options->expected_cli_fragments,
2236 &(server.ssl), options->srv_msg_len,
2237 options->expected_srv_fragments)
Yanray Wange6afd912022-10-27 12:11:18 +08002238 == 0);
2239 }
2240#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
2241 if (options->serialize == 1) {
2242 TEST_ASSERT(options->dtls == 1);
2243
2244 TEST_ASSERT(mbedtls_ssl_context_save(&(server.ssl), NULL,
2245 0, &context_buf_len)
2246 == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL);
2247
2248 context_buf = mbedtls_calloc(1, context_buf_len);
2249 TEST_ASSERT(context_buf != NULL);
2250
2251 TEST_ASSERT(mbedtls_ssl_context_save(&(server.ssl), context_buf,
2252 context_buf_len,
2253 &context_buf_len)
2254 == 0);
2255
2256 mbedtls_ssl_free(&(server.ssl));
2257 mbedtls_ssl_init(&(server.ssl));
2258
2259 TEST_ASSERT(mbedtls_ssl_setup(&(server.ssl), &(server.conf)) == 0);
2260
2261 mbedtls_ssl_set_bio(&(server.ssl), &server_context,
2262 mbedtls_test_mock_tcp_send_msg,
2263 mbedtls_test_mock_tcp_recv_msg,
2264 NULL);
2265
2266 mbedtls_ssl_set_user_data_p(&server.ssl, &server);
2267
2268#if defined(MBEDTLS_TIMING_C)
2269 mbedtls_ssl_set_timer_cb(&server.ssl, &timer_server,
2270 mbedtls_timing_set_delay,
2271 mbedtls_timing_get_delay);
2272#endif
2273#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2274 if (options->resize_buffers != 0) {
2275 /* Ensure that the buffer sizes are appropriate before resizes */
2276 TEST_ASSERT(server.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN);
2277 TEST_ASSERT(server.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN);
2278 }
2279#endif
2280 TEST_ASSERT(mbedtls_ssl_context_load(&(server.ssl), context_buf,
2281 context_buf_len) == 0);
2282
2283#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2284 /* Validate buffer sizes after context deserialization */
2285 if (options->resize_buffers != 0) {
2286 TEST_ASSERT(server.ssl.out_buf_len ==
2287 mbedtls_ssl_get_output_buflen(&server.ssl));
2288 TEST_ASSERT(server.ssl.in_buf_len ==
2289 mbedtls_ssl_get_input_buflen(&server.ssl));
2290 }
2291#endif
2292 /* Retest writing/reading */
2293 if (options->cli_msg_len != 0 || options->srv_msg_len != 0) {
Yanray Wangb088bfc2023-03-16 12:15:49 +08002294 TEST_ASSERT(mbedtls_test_ssl_exchange_data(
2295 &(client.ssl), options->cli_msg_len,
Yanray Wange6afd912022-10-27 12:11:18 +08002296 options->expected_cli_fragments,
Yanray Wangb088bfc2023-03-16 12:15:49 +08002297 &(server.ssl), options->srv_msg_len,
Yanray Wange6afd912022-10-27 12:11:18 +08002298 options->expected_srv_fragments)
2299 == 0);
2300 }
2301 }
2302#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
2303
2304#if defined(MBEDTLS_SSL_RENEGOTIATION)
2305 if (options->renegotiate) {
2306 /* Start test with renegotiation */
2307 TEST_ASSERT(server.ssl.renego_status ==
2308 MBEDTLS_SSL_INITIAL_HANDSHAKE);
2309 TEST_ASSERT(client.ssl.renego_status ==
2310 MBEDTLS_SSL_INITIAL_HANDSHAKE);
2311
2312 /* After calling this function for the server, it only sends a handshake
2313 * request. All renegotiation should happen during data exchanging */
2314 TEST_ASSERT(mbedtls_ssl_renegotiate(&(server.ssl)) == 0);
2315 TEST_ASSERT(server.ssl.renego_status ==
2316 MBEDTLS_SSL_RENEGOTIATION_PENDING);
2317 TEST_ASSERT(client.ssl.renego_status ==
2318 MBEDTLS_SSL_INITIAL_HANDSHAKE);
2319
2320 TEST_ASSERT(exchange_data(&(client.ssl), &(server.ssl)) == 0);
2321 TEST_ASSERT(server.ssl.renego_status ==
2322 MBEDTLS_SSL_RENEGOTIATION_DONE);
2323 TEST_ASSERT(client.ssl.renego_status ==
2324 MBEDTLS_SSL_RENEGOTIATION_DONE);
2325
2326 /* After calling mbedtls_ssl_renegotiate for the client,
2327 * all renegotiation should happen inside this function.
2328 * However in this test, we cannot perform simultaneous communication
2329 * between client and server so this function will return waiting error
2330 * on the socket. All rest of renegotiation should happen
2331 * during data exchanging */
2332 ret = mbedtls_ssl_renegotiate(&(client.ssl));
2333#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2334 if (options->resize_buffers != 0) {
2335 /* Ensure that the buffer sizes are appropriate before resizes */
2336 TEST_ASSERT(client.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN);
2337 TEST_ASSERT(client.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN);
2338 }
2339#endif
2340 TEST_ASSERT(ret == 0 ||
2341 ret == MBEDTLS_ERR_SSL_WANT_READ ||
2342 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
2343 TEST_ASSERT(server.ssl.renego_status ==
2344 MBEDTLS_SSL_RENEGOTIATION_DONE);
2345 TEST_ASSERT(client.ssl.renego_status ==
2346 MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS);
2347
2348 TEST_ASSERT(exchange_data(&(client.ssl), &(server.ssl)) == 0);
2349 TEST_ASSERT(server.ssl.renego_status ==
2350 MBEDTLS_SSL_RENEGOTIATION_DONE);
2351 TEST_ASSERT(client.ssl.renego_status ==
2352 MBEDTLS_SSL_RENEGOTIATION_DONE);
2353#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2354 /* Validate buffer sizes after renegotiation */
2355 if (options->resize_buffers != 0) {
2356 TEST_ASSERT(client.ssl.out_buf_len ==
2357 mbedtls_ssl_get_output_buflen(&client.ssl));
2358 TEST_ASSERT(client.ssl.in_buf_len ==
2359 mbedtls_ssl_get_input_buflen(&client.ssl));
2360 TEST_ASSERT(server.ssl.out_buf_len ==
2361 mbedtls_ssl_get_output_buflen(&server.ssl));
2362 TEST_ASSERT(server.ssl.in_buf_len ==
2363 mbedtls_ssl_get_input_buflen(&server.ssl));
2364 }
2365#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
2366 }
2367#endif /* MBEDTLS_SSL_RENEGOTIATION */
2368
2369 TEST_ASSERT(mbedtls_ssl_conf_get_user_data_p(&client.conf) == &client);
2370 TEST_ASSERT(mbedtls_ssl_get_user_data_p(&client.ssl) == &client);
2371 TEST_ASSERT(mbedtls_ssl_conf_get_user_data_p(&server.conf) == &server);
2372 TEST_ASSERT(mbedtls_ssl_get_user_data_p(&server.ssl) == &server);
2373
2374exit:
2375 mbedtls_test_ssl_endpoint_free(&client,
Yanray Wangaf727a22023-03-13 19:22:36 +08002376 options->dtls != 0 ? &client_context : NULL);
Yanray Wange6afd912022-10-27 12:11:18 +08002377 mbedtls_test_ssl_endpoint_free(&server,
Yanray Wangaf727a22023-03-13 19:22:36 +08002378 options->dtls != 0 ? &server_context : NULL);
Yanray Wange6afd912022-10-27 12:11:18 +08002379#if defined(MBEDTLS_DEBUG_C)
2380 if (options->cli_log_fun || options->srv_log_fun) {
2381 mbedtls_debug_set_threshold(0);
2382 }
2383#endif
2384#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
2385 if (context_buf != NULL) {
2386 mbedtls_free(context_buf);
2387 }
2388#endif
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +01002389 MD_OR_USE_PSA_DONE();
Yanray Wange6afd912022-10-27 12:11:18 +08002390}
2391#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
2392
2393#if defined(MBEDTLS_TEST_HOOKS)
Yanray Wangf56181a2023-03-16 12:21:33 +08002394int mbedtls_test_tweak_tls13_certificate_msg_vector_len(
Yanray Wange6afd912022-10-27 12:11:18 +08002395 unsigned char *buf, unsigned char **end, int tweak,
2396 int *expected_result, mbedtls_ssl_chk_buf_ptr_args *args)
2397{
2398/*
2399 * The definition of the tweaks assume that the certificate list contains only
2400 * one certificate.
2401 */
2402
2403/*
2404 * struct {
2405 * opaque cert_data<1..2^24-1>;
2406 * Extension extensions<0..2^16-1>;
2407 * } CertificateEntry;
2408 *
2409 * struct {
2410 * opaque certificate_request_context<0..2^8-1>;
2411 * CertificateEntry certificate_list<0..2^24-1>;
2412 * } Certificate;
2413 */
2414 unsigned char *p_certificate_request_context_len = buf;
2415 size_t certificate_request_context_len = buf[0];
2416
2417 unsigned char *p_certificate_list_len =
2418 buf + 1 + certificate_request_context_len;
2419 unsigned char *certificate_list = p_certificate_list_len + 3;
2420 size_t certificate_list_len =
2421 MBEDTLS_GET_UINT24_BE(p_certificate_list_len, 0);
2422
2423 unsigned char *p_cert_data_len = certificate_list;
2424 unsigned char *cert_data = p_cert_data_len + 3;
2425 size_t cert_data_len = MBEDTLS_GET_UINT24_BE(p_cert_data_len, 0);
2426
2427 unsigned char *p_extensions_len = cert_data + cert_data_len;
2428 unsigned char *extensions = p_extensions_len + 2;
2429 size_t extensions_len = MBEDTLS_GET_UINT16_BE(p_extensions_len, 0);
2430
2431 *expected_result = MBEDTLS_ERR_SSL_DECODE_ERROR;
2432
2433 switch (tweak) {
2434 case 1:
2435 /* Failure when checking if the certificate request context length
2436 * and certificate list length can be read
2437 */
2438 *end = buf + 3;
2439 set_chk_buf_ptr_args(args, buf, *end, 4);
2440 break;
2441
2442 case 2:
2443 /* Invalid certificate request context length.
2444 */
2445 *p_certificate_request_context_len =
Yanray Wanga8f445e2022-11-03 11:51:59 +08002446 (unsigned char) certificate_request_context_len + 1;
Yanray Wange6afd912022-10-27 12:11:18 +08002447 reset_chk_buf_ptr_args(args);
2448 break;
2449
2450 case 3:
2451 /* Failure when checking if certificate_list data can be read. */
2452 MBEDTLS_PUT_UINT24_BE(certificate_list_len + 1,
2453 p_certificate_list_len, 0);
2454 set_chk_buf_ptr_args(args, certificate_list, *end,
2455 certificate_list_len + 1);
2456 break;
2457
2458 case 4:
2459 /* Failure when checking if the cert_data length can be read. */
2460 MBEDTLS_PUT_UINT24_BE(2, p_certificate_list_len, 0);
2461 set_chk_buf_ptr_args(args, p_cert_data_len, certificate_list + 2, 3);
2462 break;
2463
2464 case 5:
2465 /* Failure when checking if cert_data data can be read. */
2466 MBEDTLS_PUT_UINT24_BE(certificate_list_len - 3 + 1,
2467 p_cert_data_len, 0);
2468 set_chk_buf_ptr_args(args, cert_data,
2469 certificate_list + certificate_list_len,
2470 certificate_list_len - 3 + 1);
2471 break;
2472
2473 case 6:
2474 /* Failure when checking if the extensions length can be read. */
2475 MBEDTLS_PUT_UINT24_BE(certificate_list_len - extensions_len - 1,
2476 p_certificate_list_len, 0);
2477 set_chk_buf_ptr_args(
2478 args, p_extensions_len,
2479 certificate_list + certificate_list_len - extensions_len - 1, 2);
2480 break;
2481
2482 case 7:
2483 /* Failure when checking if extensions data can be read. */
2484 MBEDTLS_PUT_UINT16_BE(extensions_len + 1, p_extensions_len, 0);
2485
2486 set_chk_buf_ptr_args(
2487 args, extensions,
2488 certificate_list + certificate_list_len, extensions_len + 1);
2489 break;
2490
2491 default:
2492 return -1;
2493 }
2494
2495 return 0;
2496}
2497#endif /* MBEDTLS_TEST_HOOKS */
Ronald Cron77abfe62024-01-15 11:17:31 +01002498
Ronald Cronfaf026c2024-01-31 14:32:06 +01002499/*
2500 * Functions for tests based on tickets. Implementations of the
2501 * write/parse ticket interfaces as defined by mbedtls_ssl_ticket_write/parse_t.
2502 * Basically same implementations as in ticket.c without the encryption. That
2503 * way we can tweak easily tickets characteristics to simulate misbehaving
2504 * peers.
2505 */
Ronald Cron77abfe62024-01-15 11:17:31 +01002506#if defined(MBEDTLS_SSL_SESSION_TICKETS)
2507int mbedtls_test_ticket_write(
2508 void *p_ticket, const mbedtls_ssl_session *session,
2509 unsigned char *start, const unsigned char *end,
2510 size_t *tlen, uint32_t *lifetime)
2511{
2512 int ret;
2513 ((void) p_ticket);
2514
2515 if ((ret = mbedtls_ssl_session_save(session, start, end - start,
2516 tlen)) != 0) {
2517 return ret;
2518 }
2519
2520 /* Maximum ticket lifetime as defined in RFC 8446 */
2521 *lifetime = 7 * 24 * 3600;
2522
2523 return 0;
2524}
2525
2526int mbedtls_test_ticket_parse(void *p_ticket, mbedtls_ssl_session *session,
2527 unsigned char *buf, size_t len)
2528{
2529 ((void) p_ticket);
2530
2531 return mbedtls_ssl_session_load(session, buf, len);
2532}
2533#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Ronald Cron1f6e4e42024-01-26 16:31:33 +01002534
2535#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SRV_C) && \
2536 defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS) && \
2537 defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
2538int mbedtls_test_get_tls13_ticket(
2539 mbedtls_test_handshake_test_options *client_options,
2540 mbedtls_test_handshake_test_options *server_options,
2541 mbedtls_ssl_session *session)
2542{
2543 int ret = -1;
2544 unsigned char buf[64];
2545 mbedtls_test_ssl_endpoint client_ep, server_ep;
2546
2547 mbedtls_platform_zeroize(&client_ep, sizeof(client_ep));
2548 mbedtls_platform_zeroize(&server_ep, sizeof(server_ep));
2549
2550 ret = mbedtls_test_ssl_endpoint_init(&client_ep, MBEDTLS_SSL_IS_CLIENT,
2551 client_options, NULL, NULL, NULL);
2552 TEST_EQUAL(ret, 0);
2553
2554 ret = mbedtls_test_ssl_endpoint_init(&server_ep, MBEDTLS_SSL_IS_SERVER,
2555 server_options, NULL, NULL, NULL);
2556 TEST_EQUAL(ret, 0);
2557
2558 mbedtls_ssl_conf_session_tickets_cb(&server_ep.conf,
2559 mbedtls_test_ticket_write,
2560 mbedtls_test_ticket_parse,
2561 NULL);
2562
2563 ret = mbedtls_test_mock_socket_connect(&(client_ep.socket),
2564 &(server_ep.socket), 1024);
2565 TEST_EQUAL(ret, 0);
2566
2567 TEST_EQUAL(mbedtls_test_move_handshake_to_state(
2568 &(server_ep.ssl), &(client_ep.ssl),
2569 MBEDTLS_SSL_HANDSHAKE_OVER), 0);
2570
2571 TEST_EQUAL(server_ep.ssl.handshake->new_session_tickets_count, 0);
2572
2573 do {
2574 ret = mbedtls_ssl_read(&(client_ep.ssl), buf, sizeof(buf));
2575 } while (ret != MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET);
2576
2577 ret = mbedtls_ssl_get_session(&(client_ep.ssl), session);
2578 TEST_EQUAL(ret, 0);
2579
2580exit:
2581 mbedtls_test_ssl_endpoint_free(&client_ep, NULL);
2582 mbedtls_test_ssl_endpoint_free(&server_ep, NULL);
2583
2584 return ret;
2585}
2586#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SRV_C &&
2587 MBEDTLS_SSL_PROTO_TLS1_3 && MBEDTLS_SSL_SESSION_TICKETS &&
2588 MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
2589
Yanray Wang4d07d1c2022-10-27 15:28:16 +08002590#endif /* MBEDTLS_SSL_TLS_C */