blob: 756e600d97cccd631e32b40e76eb73dbe49e1031 [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
8 * SPDX-License-Identifier: Apache-2.0
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License"); you may
11 * not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 */
22
23#include <test/ssl_helpers.h>
Manuel Pégourié-Gonnard02b10d82023-03-28 12:33:20 +020024#include "md_psa.h"
Yanray Wange6afd912022-10-27 12:11:18 +080025
Yanray Wang4d07d1c2022-10-27 15:28:16 +080026#if defined(MBEDTLS_SSL_TLS_C)
Yanray Wange6afd912022-10-27 12:11:18 +080027#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
28static int rng_seed = 0xBEEF;
29static int rng_get(void *p_rng, unsigned char *output, size_t output_len)
30{
31 (void) p_rng;
32 for (size_t i = 0; i < output_len; i++) {
33 output[i] = rand();
34 }
35
36 return 0;
37}
38#endif
39
Yanray Wange6afd912022-10-27 12:11:18 +080040void mbedtls_test_ssl_log_analyzer(void *ctx, int level,
41 const char *file, int line,
42 const char *str)
43{
44 mbedtls_test_ssl_log_pattern *p = (mbedtls_test_ssl_log_pattern *) ctx;
45
46 (void) level;
47 (void) line;
48 (void) file;
49
50 if (NULL != p &&
51 NULL != p->pattern &&
52 NULL != strstr(str, p->pattern)) {
53 p->counter++;
54 }
55}
56
57void mbedtls_test_init_handshake_options(
58 mbedtls_test_handshake_test_options *opts)
59{
60#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
61 srand(rng_seed);
62 rng_seed += 0xD0;
63#endif
64 opts->cipher = "";
65 opts->client_min_version = MBEDTLS_SSL_VERSION_UNKNOWN;
66 opts->client_max_version = MBEDTLS_SSL_VERSION_UNKNOWN;
67 opts->server_min_version = MBEDTLS_SSL_VERSION_UNKNOWN;
68 opts->server_max_version = MBEDTLS_SSL_VERSION_UNKNOWN;
Ronald Cron097ba142023-03-08 16:18:00 +010069 opts->expected_negotiated_version = MBEDTLS_SSL_VERSION_TLS1_3;
Yanray Wange6afd912022-10-27 12:11:18 +080070 opts->expected_handshake_result = 0;
71 opts->expected_ciphersuite = 0;
72 opts->pk_alg = MBEDTLS_PK_RSA;
73 opts->opaque_alg = 0;
74 opts->opaque_alg2 = 0;
75 opts->opaque_usage = 0;
76 opts->psk_str = NULL;
77 opts->dtls = 0;
78 opts->srv_auth_mode = MBEDTLS_SSL_VERIFY_NONE;
79 opts->serialize = 0;
80 opts->mfl = MBEDTLS_SSL_MAX_FRAG_LEN_NONE;
81 opts->cli_msg_len = 100;
82 opts->srv_msg_len = 100;
83 opts->expected_cli_fragments = 1;
84 opts->expected_srv_fragments = 1;
85 opts->renegotiate = 0;
86 opts->legacy_renegotiation = MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION;
87 opts->srv_log_obj = NULL;
88 opts->srv_log_obj = NULL;
89 opts->srv_log_fun = NULL;
90 opts->cli_log_fun = NULL;
91 opts->resize_buffers = 1;
92#if defined(MBEDTLS_SSL_CACHE_C)
93 opts->cache = NULL;
94 ASSERT_ALLOC(opts->cache, 1);
95 mbedtls_ssl_cache_init(opts->cache);
96exit:
97 return;
98#endif
99}
100
101void mbedtls_test_free_handshake_options(
102 mbedtls_test_handshake_test_options *opts)
103{
104#if defined(MBEDTLS_SSL_CACHE_C)
105 mbedtls_ssl_cache_free(opts->cache);
106 mbedtls_free(opts->cache);
107#else
108 (void) opts;
109#endif
110}
111
112#if defined(MBEDTLS_TEST_HOOKS)
113static void set_chk_buf_ptr_args(
114 mbedtls_ssl_chk_buf_ptr_args *args,
115 unsigned char *cur, unsigned char *end, size_t need)
116{
117 args->cur = cur;
118 args->end = end;
119 args->need = need;
120}
121
122static void reset_chk_buf_ptr_args(mbedtls_ssl_chk_buf_ptr_args *args)
123{
124 memset(args, 0, sizeof(*args));
125}
126#endif /* MBEDTLS_TEST_HOOKS */
127
Yanray Wange6afd912022-10-27 12:11:18 +0800128void mbedtls_test_ssl_buffer_init(mbedtls_test_ssl_buffer *buf)
129{
130 memset(buf, 0, sizeof(*buf));
131}
132
Yanray Wange6afd912022-10-27 12:11:18 +0800133int mbedtls_test_ssl_buffer_setup(mbedtls_test_ssl_buffer *buf,
134 size_t capacity)
135{
136 buf->buffer = (unsigned char *) mbedtls_calloc(capacity,
137 sizeof(unsigned char));
138 if (NULL == buf->buffer) {
139 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
140 }
141 buf->capacity = capacity;
142
143 return 0;
144}
145
146void mbedtls_test_ssl_buffer_free(mbedtls_test_ssl_buffer *buf)
147{
148 if (buf->buffer != NULL) {
149 mbedtls_free(buf->buffer);
150 }
151
152 memset(buf, 0, sizeof(*buf));
153}
154
Yanray Wange6afd912022-10-27 12:11:18 +0800155int mbedtls_test_ssl_buffer_put(mbedtls_test_ssl_buffer *buf,
156 const unsigned char *input, size_t input_len)
157{
158 size_t overflow = 0;
159
160 if ((buf == NULL) || (buf->buffer == NULL)) {
161 return -1;
162 }
163
164 /* Reduce input_len to a number that fits in the buffer. */
165 if ((buf->content_length + input_len) > buf->capacity) {
166 input_len = buf->capacity - buf->content_length;
167 }
168
169 if (input == NULL) {
170 return (input_len == 0) ? 0 : -1;
171 }
172
173 /* Check if the buffer has not come full circle and free space is not in
174 * the middle */
175 if (buf->start + buf->content_length < buf->capacity) {
176
177 /* Calculate the number of bytes that need to be placed at lower memory
178 * address */
179 if (buf->start + buf->content_length + input_len
180 > buf->capacity) {
181 overflow = (buf->start + buf->content_length + input_len)
182 % buf->capacity;
183 }
184
185 memcpy(buf->buffer + buf->start + buf->content_length, input,
186 input_len - overflow);
187 memcpy(buf->buffer, input + input_len - overflow, overflow);
188
189 } else {
190 /* The buffer has come full circle and free space is in the middle */
191 memcpy(buf->buffer + buf->start + buf->content_length - buf->capacity,
192 input, input_len);
193 }
194
195 buf->content_length += input_len;
Yanray Wanga8f445e2022-11-03 11:51:59 +0800196 return (input_len > INT_MAX) ? INT_MAX : (int) input_len;
Yanray Wange6afd912022-10-27 12:11:18 +0800197}
198
Yanray Wange6afd912022-10-27 12:11:18 +0800199int mbedtls_test_ssl_buffer_get(mbedtls_test_ssl_buffer *buf,
200 unsigned char *output, size_t output_len)
201{
202 size_t overflow = 0;
203
204 if ((buf == NULL) || (buf->buffer == NULL)) {
205 return -1;
206 }
207
208 if (output == NULL && output_len == 0) {
209 return 0;
210 }
211
212 if (buf->content_length < output_len) {
213 output_len = buf->content_length;
214 }
215
216 /* Calculate the number of bytes that need to be drawn from lower memory
217 * address */
218 if (buf->start + output_len > buf->capacity) {
219 overflow = (buf->start + output_len) % buf->capacity;
220 }
221
222 if (output != NULL) {
223 memcpy(output, buf->buffer + buf->start, output_len - overflow);
224 memcpy(output + output_len - overflow, buf->buffer, overflow);
225 }
226
227 buf->content_length -= output_len;
228 buf->start = (buf->start + output_len) % buf->capacity;
229
Yanray Wanga8f445e2022-11-03 11:51:59 +0800230 return (output_len > INT_MAX) ? INT_MAX : (int) output_len;
Yanray Wange6afd912022-10-27 12:11:18 +0800231}
232
Yanray Wangd19894f2023-03-16 11:47:39 +0800233int mbedtls_test_ssl_message_queue_setup(
234 mbedtls_test_ssl_message_queue *queue, size_t capacity)
Yanray Wange6afd912022-10-27 12:11:18 +0800235{
236 queue->messages = (size_t *) mbedtls_calloc(capacity, sizeof(size_t));
237 if (NULL == queue->messages) {
238 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
239 }
240
Yanray Wanga8f445e2022-11-03 11:51:59 +0800241 queue->capacity = (capacity > INT_MAX) ? INT_MAX : (int) capacity;
Yanray Wange6afd912022-10-27 12:11:18 +0800242 queue->pos = 0;
243 queue->num = 0;
244
245 return 0;
246}
247
Yanray Wangd19894f2023-03-16 11:47:39 +0800248void mbedtls_test_ssl_message_queue_free(
249 mbedtls_test_ssl_message_queue *queue)
Yanray Wange6afd912022-10-27 12:11:18 +0800250{
251 if (queue == NULL) {
252 return;
253 }
254
255 if (queue->messages != NULL) {
256 mbedtls_free(queue->messages);
257 }
258
259 memset(queue, 0, sizeof(*queue));
260}
261
Yanray Wange6afd912022-10-27 12:11:18 +0800262int mbedtls_test_ssl_message_queue_push_info(
263 mbedtls_test_ssl_message_queue *queue, size_t len)
264{
265 int place;
266 if (queue == NULL) {
267 return MBEDTLS_TEST_ERROR_ARG_NULL;
268 }
269
270 if (queue->num >= queue->capacity) {
271 return MBEDTLS_ERR_SSL_WANT_WRITE;
272 }
273
274 place = (queue->pos + queue->num) % queue->capacity;
275 queue->messages[place] = len;
276 queue->num++;
Yanray Wanga8f445e2022-11-03 11:51:59 +0800277 return (len > INT_MAX) ? INT_MAX : (int) len;
Yanray Wange6afd912022-10-27 12:11:18 +0800278}
279
Yanray Wange6afd912022-10-27 12:11:18 +0800280int mbedtls_test_ssl_message_queue_pop_info(
281 mbedtls_test_ssl_message_queue *queue, size_t buf_len)
282{
283 size_t message_length;
284 if (queue == NULL) {
285 return MBEDTLS_TEST_ERROR_ARG_NULL;
286 }
287 if (queue->num == 0) {
288 return MBEDTLS_ERR_SSL_WANT_READ;
289 }
290
291 message_length = queue->messages[queue->pos];
292 queue->messages[queue->pos] = 0;
293 queue->num--;
294 queue->pos++;
295 queue->pos %= queue->capacity;
296 if (queue->pos < 0) {
297 queue->pos += queue->capacity;
298 }
299
Yanray Wanga8f445e2022-11-03 11:51:59 +0800300 return (message_length > INT_MAX && buf_len > INT_MAX) ? INT_MAX :
301 (message_length > buf_len) ? (int) buf_len : (int) message_length;
Yanray Wange6afd912022-10-27 12:11:18 +0800302}
303
304/*
305 * Take a peek on the info about the next message length from the queue.
306 * This will be the oldest inserted message length(fifo).
307 *
308 * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null.
309 * \retval MBEDTLS_ERR_SSL_WANT_READ, if the queue is empty.
310 * \retval 0, if the peek was successful.
311 * \retval MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED, if the given buffer length is
312 * too small to fit the message. In this case the \p msg_len will be
313 * set to the full message length so that the
314 * caller knows what portion of the message can be dropped.
315 */
Yanray Wang5e22a922023-03-16 14:57:54 +0800316static int test_ssl_message_queue_peek_info(
317 mbedtls_test_ssl_message_queue *queue,
318 size_t buf_len, size_t *msg_len)
Yanray Wange6afd912022-10-27 12:11:18 +0800319{
320 if (queue == NULL || msg_len == NULL) {
321 return MBEDTLS_TEST_ERROR_ARG_NULL;
322 }
323 if (queue->num == 0) {
324 return MBEDTLS_ERR_SSL_WANT_READ;
325 }
326
327 *msg_len = queue->messages[queue->pos];
328 return (*msg_len > buf_len) ? MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED : 0;
329}
330
Yanray Wang5f86a422023-03-15 16:02:29 +0800331void mbedtls_test_mock_socket_init(mbedtls_test_mock_socket *socket)
Yanray Wange6afd912022-10-27 12:11:18 +0800332{
333 memset(socket, 0, sizeof(*socket));
334}
335
Yanray Wange6afd912022-10-27 12:11:18 +0800336void mbedtls_test_mock_socket_close(mbedtls_test_mock_socket *socket)
337{
338 if (socket == NULL) {
339 return;
340 }
341
342 if (socket->input != NULL) {
343 mbedtls_test_ssl_buffer_free(socket->input);
344 mbedtls_free(socket->input);
345 }
346
347 if (socket->output != NULL) {
348 mbedtls_test_ssl_buffer_free(socket->output);
349 mbedtls_free(socket->output);
350 }
351
352 if (socket->peer != NULL) {
353 memset(socket->peer, 0, sizeof(*socket->peer));
354 }
355
356 memset(socket, 0, sizeof(*socket));
357}
358
Yanray Wange6afd912022-10-27 12:11:18 +0800359int mbedtls_test_mock_socket_connect(mbedtls_test_mock_socket *peer1,
360 mbedtls_test_mock_socket *peer2,
361 size_t bufsize)
362{
363 int ret = -1;
364
365 peer1->output =
366 (mbedtls_test_ssl_buffer *) mbedtls_calloc(
367 1, sizeof(mbedtls_test_ssl_buffer));
368 if (peer1->output == NULL) {
369 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
370 goto exit;
371 }
372 mbedtls_test_ssl_buffer_init(peer1->output);
373 if (0 != (ret = mbedtls_test_ssl_buffer_setup(peer1->output, bufsize))) {
374 goto exit;
375 }
376
377 peer2->output =
378 (mbedtls_test_ssl_buffer *) mbedtls_calloc(
379 1, sizeof(mbedtls_test_ssl_buffer));
380 if (peer2->output == NULL) {
381 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
382 goto exit;
383 }
384 mbedtls_test_ssl_buffer_init(peer2->output);
385 if (0 != (ret = mbedtls_test_ssl_buffer_setup(peer2->output, bufsize))) {
386 goto exit;
387 }
388
389 peer1->peer = peer2;
390 peer2->peer = peer1;
391 peer1->input = peer2->output;
392 peer2->input = peer1->output;
393
394 peer1->status = peer2->status = MBEDTLS_MOCK_SOCKET_CONNECTED;
395 ret = 0;
396
397exit:
398
399 if (ret != 0) {
400 mbedtls_test_mock_socket_close(peer1);
401 mbedtls_test_mock_socket_close(peer2);
402 }
403
404 return ret;
405}
406
Yanray Wangaf727a22023-03-13 19:22:36 +0800407int mbedtls_test_mock_tcp_send_b(void *ctx,
408 const unsigned char *buf, size_t len)
Yanray Wange6afd912022-10-27 12:11:18 +0800409{
410 mbedtls_test_mock_socket *socket = (mbedtls_test_mock_socket *) ctx;
411
412 if (socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED) {
413 return -1;
414 }
415
416 return mbedtls_test_ssl_buffer_put(socket->output, buf, len);
417}
418
419int mbedtls_test_mock_tcp_recv_b(void *ctx, unsigned char *buf, size_t len)
420{
421 mbedtls_test_mock_socket *socket = (mbedtls_test_mock_socket *) ctx;
422
423 if (socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED) {
424 return -1;
425 }
426
427 return mbedtls_test_ssl_buffer_get(socket->input, buf, len);
428}
429
Yanray Wang34634352023-02-14 17:56:51 +0800430int mbedtls_test_mock_tcp_send_nb(void *ctx,
431 const unsigned char *buf, size_t len)
Yanray Wange6afd912022-10-27 12:11:18 +0800432{
433 mbedtls_test_mock_socket *socket = (mbedtls_test_mock_socket *) ctx;
434
435 if (socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED) {
436 return -1;
437 }
438
439 if (socket->output->capacity == socket->output->content_length) {
440 return MBEDTLS_ERR_SSL_WANT_WRITE;
441 }
442
443 return mbedtls_test_ssl_buffer_put(socket->output, buf, len);
444}
445
446int mbedtls_test_mock_tcp_recv_nb(void *ctx, unsigned char *buf, size_t len)
447{
448 mbedtls_test_mock_socket *socket = (mbedtls_test_mock_socket *) ctx;
449
450 if (socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED) {
451 return -1;
452 }
453
454 if (socket->input->content_length == 0) {
455 return MBEDTLS_ERR_SSL_WANT_READ;
456 }
457
458 return mbedtls_test_ssl_buffer_get(socket->input, buf, len);
459}
460
Yanray Wangd19894f2023-03-16 11:47:39 +0800461void mbedtls_test_message_socket_init(
462 mbedtls_test_message_socket_context *ctx)
Yanray Wange6afd912022-10-27 12:11:18 +0800463{
464 ctx->queue_input = NULL;
465 ctx->queue_output = NULL;
466 ctx->socket = NULL;
467}
468
Yanray Wange6afd912022-10-27 12:11:18 +0800469int mbedtls_test_message_socket_setup(
470 mbedtls_test_ssl_message_queue *queue_input,
471 mbedtls_test_ssl_message_queue *queue_output,
472 size_t queue_capacity,
473 mbedtls_test_mock_socket *socket,
474 mbedtls_test_message_socket_context *ctx)
475{
476 int ret = mbedtls_test_ssl_message_queue_setup(queue_input, queue_capacity);
477 if (ret != 0) {
478 return ret;
479 }
480 ctx->queue_input = queue_input;
481 ctx->queue_output = queue_output;
482 ctx->socket = socket;
Yanray Wang5f86a422023-03-15 16:02:29 +0800483 mbedtls_test_mock_socket_init(socket);
Yanray Wange6afd912022-10-27 12:11:18 +0800484
485 return 0;
486}
487
Yanray Wangd19894f2023-03-16 11:47:39 +0800488void mbedtls_test_message_socket_close(
489 mbedtls_test_message_socket_context *ctx)
Yanray Wange6afd912022-10-27 12:11:18 +0800490{
491 if (ctx == NULL) {
492 return;
493 }
494
495 mbedtls_test_ssl_message_queue_free(ctx->queue_input);
496 mbedtls_test_mock_socket_close(ctx->socket);
497 memset(ctx, 0, sizeof(*ctx));
498}
499
Yanray Wang34634352023-02-14 17:56:51 +0800500int mbedtls_test_mock_tcp_send_msg(void *ctx,
501 const unsigned char *buf, size_t len)
Yanray Wange6afd912022-10-27 12:11:18 +0800502{
503 mbedtls_test_ssl_message_queue *queue;
504 mbedtls_test_mock_socket *socket;
505 mbedtls_test_message_socket_context *context =
506 (mbedtls_test_message_socket_context *) ctx;
507
508 if (context == NULL || context->socket == NULL
509 || context->queue_output == NULL) {
510 return MBEDTLS_TEST_ERROR_CONTEXT_ERROR;
511 }
512
513 queue = context->queue_output;
514 socket = context->socket;
515
516 if (queue->num >= queue->capacity) {
517 return MBEDTLS_ERR_SSL_WANT_WRITE;
518 }
519
520 if (mbedtls_test_mock_tcp_send_b(socket, buf, len) != (int) len) {
521 return MBEDTLS_TEST_ERROR_SEND_FAILED;
522 }
523
524 return mbedtls_test_ssl_message_queue_push_info(queue, len);
525}
526
Yanray Wang34634352023-02-14 17:56:51 +0800527int mbedtls_test_mock_tcp_recv_msg(void *ctx,
528 unsigned char *buf, size_t buf_len)
Yanray Wange6afd912022-10-27 12:11:18 +0800529{
530 mbedtls_test_ssl_message_queue *queue;
531 mbedtls_test_mock_socket *socket;
532 mbedtls_test_message_socket_context *context =
533 (mbedtls_test_message_socket_context *) ctx;
534 size_t drop_len = 0;
535 size_t msg_len;
536 int ret;
537
538 if (context == NULL || context->socket == NULL
539 || context->queue_input == NULL) {
540 return MBEDTLS_TEST_ERROR_CONTEXT_ERROR;
541 }
542
543 queue = context->queue_input;
544 socket = context->socket;
545
546 /* Peek first, so that in case of a socket error the data remains in
547 * the queue. */
Yanray Wang5e22a922023-03-16 14:57:54 +0800548 ret = test_ssl_message_queue_peek_info(queue, buf_len, &msg_len);
Yanray Wange6afd912022-10-27 12:11:18 +0800549 if (ret == MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED) {
550 /* Calculate how much to drop */
551 drop_len = msg_len - buf_len;
552
553 /* Set the requested message len to be buffer length */
554 msg_len = buf_len;
555 } else if (ret != 0) {
556 return ret;
557 }
558
559 if (mbedtls_test_mock_tcp_recv_b(socket, buf, msg_len) != (int) msg_len) {
560 return MBEDTLS_TEST_ERROR_RECV_FAILED;
561 }
562
563 if (ret == MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED) {
564 /* Drop the remaining part of the message */
Yanray Wangaf727a22023-03-13 19:22:36 +0800565 if (mbedtls_test_mock_tcp_recv_b(socket, NULL, drop_len) !=
566 (int) drop_len) {
Yanray Wange6afd912022-10-27 12:11:18 +0800567 /* Inconsistent state - part of the message was read,
568 * and a part couldn't. Not much we can do here, but it should not
569 * happen in test environment, unless forced manually. */
570 }
571 }
572 mbedtls_test_ssl_message_queue_pop_info(queue, buf_len);
573
Yanray Wanga8f445e2022-11-03 11:51:59 +0800574 return (msg_len > INT_MAX) ? INT_MAX : (int) msg_len;
Yanray Wange6afd912022-10-27 12:11:18 +0800575}
576
577#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
578
579/*
580 * Deinitializes certificates from endpoint represented by \p ep.
581 */
Yanray Wangf6f71902023-03-15 16:05:14 +0800582static void test_ssl_endpoint_certificate_free(mbedtls_test_ssl_endpoint *ep)
Yanray Wange6afd912022-10-27 12:11:18 +0800583{
584 mbedtls_test_ssl_endpoint_certificate *cert = &(ep->cert);
585 if (cert != NULL) {
586 if (cert->ca_cert != NULL) {
587 mbedtls_x509_crt_free(cert->ca_cert);
588 mbedtls_free(cert->ca_cert);
589 cert->ca_cert = NULL;
590 }
591 if (cert->cert != NULL) {
592 mbedtls_x509_crt_free(cert->cert);
593 mbedtls_free(cert->cert);
594 cert->cert = NULL;
595 }
596 if (cert->pkey != NULL) {
597#if defined(MBEDTLS_USE_PSA_CRYPTO)
598 if (mbedtls_pk_get_type(cert->pkey) == MBEDTLS_PK_OPAQUE) {
Valerio Setti4f387ef2023-05-02 14:15:59 +0200599 psa_destroy_key(cert->pkey->priv_id);
Yanray Wange6afd912022-10-27 12:11:18 +0800600 }
601#endif
602 mbedtls_pk_free(cert->pkey);
603 mbedtls_free(cert->pkey);
604 cert->pkey = NULL;
605 }
606 }
607}
608
Yanray Wange6afd912022-10-27 12:11:18 +0800609int mbedtls_test_ssl_endpoint_certificate_init(mbedtls_test_ssl_endpoint *ep,
610 int pk_alg,
611 int opaque_alg, int opaque_alg2,
612 int opaque_usage)
613{
614 int i = 0;
615 int ret = -1;
616 mbedtls_test_ssl_endpoint_certificate *cert = NULL;
617#if defined(MBEDTLS_USE_PSA_CRYPTO)
618 mbedtls_svc_key_id_t key_slot = MBEDTLS_SVC_KEY_ID_INIT;
619#endif
620
621 if (ep == NULL) {
622 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
623 }
624
625 cert = &(ep->cert);
626 ASSERT_ALLOC(cert->ca_cert, 1);
627 ASSERT_ALLOC(cert->cert, 1);
628 ASSERT_ALLOC(cert->pkey, 1);
629
630 mbedtls_x509_crt_init(cert->ca_cert);
631 mbedtls_x509_crt_init(cert->cert);
632 mbedtls_pk_init(cert->pkey);
633
634 /* Load the trusted CA */
635
636 for (i = 0; mbedtls_test_cas_der[i] != NULL; i++) {
637 ret = mbedtls_x509_crt_parse_der(
638 cert->ca_cert,
639 (const unsigned char *) mbedtls_test_cas_der[i],
640 mbedtls_test_cas_der_len[i]);
641 TEST_ASSERT(ret == 0);
642 }
643
644 /* Load own certificate and private key */
645
646 if (ep->conf.endpoint == MBEDTLS_SSL_IS_SERVER) {
647 if (pk_alg == MBEDTLS_PK_RSA) {
648 ret = mbedtls_x509_crt_parse(
649 cert->cert,
650 (const unsigned char *) mbedtls_test_srv_crt_rsa_sha256_der,
651 mbedtls_test_srv_crt_rsa_sha256_der_len);
652 TEST_ASSERT(ret == 0);
653
654 ret = mbedtls_pk_parse_key(
655 cert->pkey,
656 (const unsigned char *) mbedtls_test_srv_key_rsa_der,
657 mbedtls_test_srv_key_rsa_der_len, NULL, 0,
658 mbedtls_test_rnd_std_rand, NULL);
659 TEST_ASSERT(ret == 0);
660 } else {
661 ret = mbedtls_x509_crt_parse(
662 cert->cert,
663 (const unsigned char *) mbedtls_test_srv_crt_ec_der,
664 mbedtls_test_srv_crt_ec_der_len);
665 TEST_ASSERT(ret == 0);
666
667 ret = mbedtls_pk_parse_key(
668 cert->pkey,
669 (const unsigned char *) mbedtls_test_srv_key_ec_der,
670 mbedtls_test_srv_key_ec_der_len, NULL, 0,
671 mbedtls_test_rnd_std_rand, NULL);
672 TEST_ASSERT(ret == 0);
673 }
674 } else {
675 if (pk_alg == MBEDTLS_PK_RSA) {
676 ret = mbedtls_x509_crt_parse(
677 cert->cert,
678 (const unsigned char *) mbedtls_test_cli_crt_rsa_der,
679 mbedtls_test_cli_crt_rsa_der_len);
680 TEST_ASSERT(ret == 0);
681
682 ret = mbedtls_pk_parse_key(
683 cert->pkey,
684 (const unsigned char *) mbedtls_test_cli_key_rsa_der,
685 mbedtls_test_cli_key_rsa_der_len, NULL, 0,
686 mbedtls_test_rnd_std_rand, NULL);
687 TEST_ASSERT(ret == 0);
688 } else {
689 ret = mbedtls_x509_crt_parse(
690 cert->cert,
691 (const unsigned char *) mbedtls_test_cli_crt_ec_der,
692 mbedtls_test_cli_crt_ec_len);
693 TEST_ASSERT(ret == 0);
694
695 ret = mbedtls_pk_parse_key(
696 cert->pkey,
697 (const unsigned char *) mbedtls_test_cli_key_ec_der,
698 mbedtls_test_cli_key_ec_der_len, NULL, 0,
699 mbedtls_test_rnd_std_rand, NULL);
700 TEST_ASSERT(ret == 0);
701 }
702 }
703
704#if defined(MBEDTLS_USE_PSA_CRYPTO)
705 if (opaque_alg != 0) {
706 TEST_EQUAL(mbedtls_pk_wrap_as_opaque(cert->pkey, &key_slot,
707 opaque_alg, opaque_usage,
708 opaque_alg2), 0);
709 }
710#else
711 (void) opaque_alg;
712 (void) opaque_alg2;
713 (void) opaque_usage;
714#endif
715
716 mbedtls_ssl_conf_ca_chain(&(ep->conf), cert->ca_cert, NULL);
717
718 ret = mbedtls_ssl_conf_own_cert(&(ep->conf), cert->cert,
719 cert->pkey);
720 TEST_ASSERT(ret == 0);
721 TEST_ASSERT(ep->conf.key_cert != NULL);
722
723 ret = mbedtls_ssl_conf_own_cert(&(ep->conf), NULL, NULL);
724 TEST_ASSERT(ret == 0);
725 TEST_ASSERT(ep->conf.key_cert == NULL);
726
727 ret = mbedtls_ssl_conf_own_cert(&(ep->conf), cert->cert,
728 cert->pkey);
729 TEST_ASSERT(ret == 0);
730
731exit:
732 if (ret != 0) {
Yanray Wangf6f71902023-03-15 16:05:14 +0800733 test_ssl_endpoint_certificate_free(ep);
Yanray Wange6afd912022-10-27 12:11:18 +0800734 }
735
736 return ret;
737}
738
Yanray Wange6afd912022-10-27 12:11:18 +0800739int mbedtls_test_ssl_endpoint_init(
740 mbedtls_test_ssl_endpoint *ep, int endpoint_type,
741 mbedtls_test_handshake_test_options *options,
742 mbedtls_test_message_socket_context *dtls_context,
743 mbedtls_test_ssl_message_queue *input_queue,
744 mbedtls_test_ssl_message_queue *output_queue,
745 uint16_t *group_list)
746{
747 int ret = -1;
748 uintptr_t user_data_n;
749
750 if (dtls_context != NULL &&
751 (input_queue == NULL || output_queue == NULL)) {
752 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
753
754 }
755
756 if (ep == NULL) {
757 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
758 }
759
760 memset(ep, 0, sizeof(*ep));
761
762 ep->name = (endpoint_type == MBEDTLS_SSL_IS_SERVER) ? "Server" : "Client";
763
764 mbedtls_ssl_init(&(ep->ssl));
765 mbedtls_ssl_config_init(&(ep->conf));
766 mbedtls_ssl_conf_rng(&(ep->conf), rng_get, NULL);
767
768 TEST_ASSERT(mbedtls_ssl_conf_get_user_data_p(&ep->conf) == NULL);
769 TEST_EQUAL(mbedtls_ssl_conf_get_user_data_n(&ep->conf), 0);
770 TEST_ASSERT(mbedtls_ssl_get_user_data_p(&ep->ssl) == NULL);
771 TEST_EQUAL(mbedtls_ssl_get_user_data_n(&ep->ssl), 0);
772
773 (void) mbedtls_test_rnd_std_rand(NULL,
774 (void *) &user_data_n,
775 sizeof(user_data_n));
776 mbedtls_ssl_conf_set_user_data_n(&ep->conf, user_data_n);
777 mbedtls_ssl_set_user_data_n(&ep->ssl, user_data_n);
778
779 if (dtls_context != NULL) {
780 TEST_ASSERT(mbedtls_test_message_socket_setup(input_queue, output_queue,
781 100, &(ep->socket),
782 dtls_context) == 0);
783 } else {
Yanray Wang5f86a422023-03-15 16:02:29 +0800784 mbedtls_test_mock_socket_init(&(ep->socket));
Yanray Wange6afd912022-10-27 12:11:18 +0800785 }
786
787 /* Non-blocking callbacks without timeout */
788 if (dtls_context != NULL) {
789 mbedtls_ssl_set_bio(&(ep->ssl), dtls_context,
790 mbedtls_test_mock_tcp_send_msg,
791 mbedtls_test_mock_tcp_recv_msg,
792 NULL);
793 } else {
794 mbedtls_ssl_set_bio(&(ep->ssl), &(ep->socket),
795 mbedtls_test_mock_tcp_send_nb,
796 mbedtls_test_mock_tcp_recv_nb,
797 NULL);
798 }
799
800 ret = mbedtls_ssl_config_defaults(&(ep->conf), endpoint_type,
801 (dtls_context != NULL) ?
802 MBEDTLS_SSL_TRANSPORT_DATAGRAM :
803 MBEDTLS_SSL_TRANSPORT_STREAM,
804 MBEDTLS_SSL_PRESET_DEFAULT);
805 TEST_ASSERT(ret == 0);
806
Ronald Crona697a712023-03-09 17:47:42 +0100807 if (MBEDTLS_SSL_IS_CLIENT == endpoint_type) {
808 if (options->client_min_version != MBEDTLS_SSL_VERSION_UNKNOWN) {
809 mbedtls_ssl_conf_min_tls_version(&(ep->conf),
810 options->client_min_version);
811 }
812
813 if (options->client_max_version != MBEDTLS_SSL_VERSION_UNKNOWN) {
814 mbedtls_ssl_conf_max_tls_version(&(ep->conf),
815 options->client_max_version);
816 }
817 } else {
818 if (options->server_min_version != MBEDTLS_SSL_VERSION_UNKNOWN) {
819 mbedtls_ssl_conf_min_tls_version(&(ep->conf),
820 options->server_min_version);
821 }
822
823 if (options->server_max_version != MBEDTLS_SSL_VERSION_UNKNOWN) {
824 mbedtls_ssl_conf_max_tls_version(&(ep->conf),
825 options->server_max_version);
826 }
827 }
828
Yanray Wange6afd912022-10-27 12:11:18 +0800829 if (group_list != NULL) {
830 mbedtls_ssl_conf_groups(&(ep->conf), group_list);
831 }
832
833 mbedtls_ssl_conf_authmode(&(ep->conf), MBEDTLS_SSL_VERIFY_REQUIRED);
834
835#if defined(MBEDTLS_SSL_CACHE_C) && defined(MBEDTLS_SSL_SRV_C)
836 if (endpoint_type == MBEDTLS_SSL_IS_SERVER && options->cache != NULL) {
837 mbedtls_ssl_conf_session_cache(&(ep->conf), options->cache,
838 mbedtls_ssl_cache_get,
839 mbedtls_ssl_cache_set);
840 }
841#endif
842
843 ret = mbedtls_ssl_setup(&(ep->ssl), &(ep->conf));
844 TEST_ASSERT(ret == 0);
845
846#if defined(MBEDTLS_SSL_PROTO_DTLS) && defined(MBEDTLS_SSL_SRV_C)
847 if (endpoint_type == MBEDTLS_SSL_IS_SERVER && dtls_context != NULL) {
848 mbedtls_ssl_conf_dtls_cookies(&(ep->conf), NULL, NULL, NULL);
849 }
850#endif
851
852 ret = mbedtls_test_ssl_endpoint_certificate_init(ep, options->pk_alg,
853 options->opaque_alg,
854 options->opaque_alg2,
855 options->opaque_usage);
856 TEST_ASSERT(ret == 0);
857
858 TEST_EQUAL(mbedtls_ssl_conf_get_user_data_n(&ep->conf), user_data_n);
859 mbedtls_ssl_conf_set_user_data_p(&ep->conf, ep);
860 TEST_EQUAL(mbedtls_ssl_get_user_data_n(&ep->ssl), user_data_n);
861 mbedtls_ssl_set_user_data_p(&ep->ssl, ep);
862
863exit:
864 return ret;
865}
866
Yanray Wange6afd912022-10-27 12:11:18 +0800867void mbedtls_test_ssl_endpoint_free(
868 mbedtls_test_ssl_endpoint *ep,
869 mbedtls_test_message_socket_context *context)
870{
Yanray Wangf6f71902023-03-15 16:05:14 +0800871 test_ssl_endpoint_certificate_free(ep);
Yanray Wange6afd912022-10-27 12:11:18 +0800872
873 mbedtls_ssl_free(&(ep->ssl));
874 mbedtls_ssl_config_free(&(ep->conf));
875
876 if (context != NULL) {
877 mbedtls_test_message_socket_close(context);
878 } else {
879 mbedtls_test_mock_socket_close(&(ep->socket));
880 }
881}
882
Yanray Wange6afd912022-10-27 12:11:18 +0800883int mbedtls_test_move_handshake_to_state(mbedtls_ssl_context *ssl,
884 mbedtls_ssl_context *second_ssl,
885 int state)
886{
887 enum { BUFFSIZE = 1024 };
888 int max_steps = 1000;
889 int ret = 0;
890
891 if (ssl == NULL || second_ssl == NULL) {
892 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
893 }
894
895 /* Perform communication via connected sockets */
896 while ((ssl->state != state) && (--max_steps >= 0)) {
897 /* If /p second_ssl ends the handshake procedure before /p ssl then
898 * there is no need to call the next step */
899 if (!mbedtls_ssl_is_handshake_over(second_ssl)) {
900 ret = mbedtls_ssl_handshake_step(second_ssl);
901 if (ret != 0 && ret != MBEDTLS_ERR_SSL_WANT_READ &&
902 ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
903 return ret;
904 }
905 }
906
907 /* We only care about the \p ssl state and returns, so we call it last,
908 * to leave the iteration as soon as the state is as expected. */
909 ret = mbedtls_ssl_handshake_step(ssl);
910 if (ret != 0 && ret != MBEDTLS_ERR_SSL_WANT_READ &&
911 ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
912 return ret;
913 }
914 }
915
916 return (max_steps >= 0) ? ret : -1;
917}
918
919#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
920
921/*
922 * Write application data. Increase write counter if necessary.
923 */
Yanray Wang1fca4de2023-02-06 12:10:48 +0800924int mbedtls_ssl_write_fragment(mbedtls_ssl_context *ssl,
925 unsigned char *buf, int buf_len,
926 int *written,
Yanray Wange6afd912022-10-27 12:11:18 +0800927 const int expected_fragments)
928{
Agathiyan Bragadeesh93212652023-07-12 11:22:59 +0100929 int ret;
Yanray Wange6afd912022-10-27 12:11:18 +0800930 /* Verify that calling mbedtls_ssl_write with a NULL buffer and zero length is
931 * a valid no-op for TLS connections. */
932 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
933 TEST_ASSERT(mbedtls_ssl_write(ssl, NULL, 0) == 0);
934 }
935
Agathiyan Bragadeesh93212652023-07-12 11:22:59 +0100936 ret = mbedtls_ssl_write(ssl, buf + *written, buf_len - *written);
Yanray Wange6afd912022-10-27 12:11:18 +0800937 if (ret > 0) {
938 *written += ret;
939 }
940
941 if (expected_fragments == 0) {
942 /* Used for DTLS and the message size larger than MFL. In that case
943 * the message can not be fragmented and the library should return
944 * MBEDTLS_ERR_SSL_BAD_INPUT_DATA error. This error must be returned
Yanray Wangb088bfc2023-03-16 12:15:49 +0800945 * to prevent a dead loop inside mbedtls_test_ssl_exchange_data(). */
Yanray Wange6afd912022-10-27 12:11:18 +0800946 return ret;
947 } else if (expected_fragments == 1) {
948 /* Used for TLS/DTLS and the message size lower than MFL */
949 TEST_ASSERT(ret == buf_len ||
950 ret == MBEDTLS_ERR_SSL_WANT_READ ||
951 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
952 } else {
953 /* Used for TLS and the message size larger than MFL */
954 TEST_ASSERT(expected_fragments > 1);
955 TEST_ASSERT((ret >= 0 && ret <= buf_len) ||
956 ret == MBEDTLS_ERR_SSL_WANT_READ ||
957 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
958 }
959
960 return 0;
961
962exit:
963 /* Some of the tests failed */
964 return -1;
965}
966
967/*
968 * Read application data and increase read counter and fragments counter
969 * if necessary.
970 */
Yanray Wang1fca4de2023-02-06 12:10:48 +0800971int mbedtls_ssl_read_fragment(mbedtls_ssl_context *ssl,
972 unsigned char *buf, int buf_len,
973 int *read, int *fragments,
974 const int expected_fragments)
Yanray Wange6afd912022-10-27 12:11:18 +0800975{
Agathiyan Bragadeesh93212652023-07-12 11:22:59 +0100976 int ret;
Yanray Wange6afd912022-10-27 12:11:18 +0800977 /* Verify that calling mbedtls_ssl_write with a NULL buffer and zero length is
978 * a valid no-op for TLS connections. */
979 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
980 TEST_ASSERT(mbedtls_ssl_read(ssl, NULL, 0) == 0);
981 }
982
Agathiyan Bragadeesh93212652023-07-12 11:22:59 +0100983 ret = mbedtls_ssl_read(ssl, buf + *read, buf_len - *read);
Yanray Wange6afd912022-10-27 12:11:18 +0800984 if (ret > 0) {
985 (*fragments)++;
986 *read += ret;
987 }
988
989 if (expected_fragments == 0) {
990 TEST_ASSERT(ret == 0);
991 } else if (expected_fragments == 1) {
992 TEST_ASSERT(ret == buf_len ||
993 ret == MBEDTLS_ERR_SSL_WANT_READ ||
994 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
995 } else {
996 TEST_ASSERT(expected_fragments > 1);
997 TEST_ASSERT((ret >= 0 && ret <= buf_len) ||
998 ret == MBEDTLS_ERR_SSL_WANT_READ ||
999 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
1000 }
1001
1002 return 0;
1003
1004exit:
1005 /* Some of the tests failed */
1006 return -1;
1007}
1008
Yanray Wangead70c82023-03-16 12:04:49 +08001009#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
1010static void set_ciphersuite(mbedtls_ssl_config *conf, const char *cipher,
1011 int *forced_ciphersuite)
Yanray Wange6afd912022-10-27 12:11:18 +08001012{
1013 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
1014 forced_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id(cipher);
1015 forced_ciphersuite[1] = 0;
1016
1017 ciphersuite_info =
1018 mbedtls_ssl_ciphersuite_from_id(forced_ciphersuite[0]);
1019
1020 TEST_ASSERT(ciphersuite_info != NULL);
1021 TEST_ASSERT(ciphersuite_info->min_tls_version <= conf->max_tls_version);
1022 TEST_ASSERT(ciphersuite_info->max_tls_version >= conf->min_tls_version);
1023
1024 if (conf->max_tls_version > ciphersuite_info->max_tls_version) {
1025 conf->max_tls_version = ciphersuite_info->max_tls_version;
1026 }
1027 if (conf->min_tls_version < ciphersuite_info->min_tls_version) {
1028 conf->min_tls_version = ciphersuite_info->min_tls_version;
1029 }
1030
1031 mbedtls_ssl_conf_ciphersuites(conf, forced_ciphersuite);
1032
1033exit:
1034 return;
1035}
Yanray Wangead70c82023-03-16 12:04:49 +08001036#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Yanray Wange6afd912022-10-27 12:11:18 +08001037
Yanray Wangead70c82023-03-16 12:04:49 +08001038#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) && \
1039 defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED) && \
1040 defined(MBEDTLS_SSL_SRV_C)
1041static int psk_dummy_callback(void *p_info, mbedtls_ssl_context *ssl,
1042 const unsigned char *name, size_t name_len)
Yanray Wange6afd912022-10-27 12:11:18 +08001043{
1044 (void) p_info;
1045 (void) ssl;
1046 (void) name;
1047 (void) name_len;
1048
1049 return 0;
1050}
Yanray Wangead70c82023-03-16 12:04:49 +08001051#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED &&
1052 MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED &&
1053 MBEDTLS_SSL_SRV_C */
Yanray Wange6afd912022-10-27 12:11:18 +08001054
1055#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
1056 defined(MBEDTLS_CIPHER_MODE_CBC) && defined(MBEDTLS_AES_C)
1057int mbedtls_test_psa_cipher_encrypt_helper(mbedtls_ssl_transform *transform,
1058 const unsigned char *iv,
1059 size_t iv_len,
1060 const unsigned char *input,
1061 size_t ilen,
1062 unsigned char *output,
1063 size_t *olen)
1064{
1065#if defined(MBEDTLS_USE_PSA_CRYPTO)
1066 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1067 psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT;
1068 size_t part_len;
1069
1070 status = psa_cipher_encrypt_setup(&cipher_op,
1071 transform->psa_key_enc,
1072 transform->psa_alg);
1073
1074 if (status != PSA_SUCCESS) {
1075 return PSA_TO_MBEDTLS_ERR(status);
1076 }
1077
1078 status = psa_cipher_set_iv(&cipher_op, iv, iv_len);
1079
1080 if (status != PSA_SUCCESS) {
1081 return PSA_TO_MBEDTLS_ERR(status);
1082 }
1083
1084 status = psa_cipher_update(&cipher_op, input, ilen, output, ilen, olen);
1085
1086 if (status != PSA_SUCCESS) {
1087 return PSA_TO_MBEDTLS_ERR(status);
1088 }
1089
1090 status = psa_cipher_finish(&cipher_op, output + *olen, ilen - *olen,
1091 &part_len);
1092
1093 if (status != PSA_SUCCESS) {
1094 return PSA_TO_MBEDTLS_ERR(status);
1095 }
1096
1097 *olen += part_len;
1098 return 0;
1099#else
1100 return mbedtls_cipher_crypt(&transform->cipher_ctx_enc,
1101 iv, iv_len, input, ilen, output, olen);
1102#endif /* MBEDTLS_USE_PSA_CRYPTO */
1103}
1104#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_CIPHER_MODE_CBC &&
1105 MBEDTLS_AES_C */
1106
1107int mbedtls_test_ssl_build_transforms(mbedtls_ssl_transform *t_in,
1108 mbedtls_ssl_transform *t_out,
1109 int cipher_type, int hash_id,
1110 int etm, int tag_mode,
1111 mbedtls_ssl_protocol_version tls_version,
1112 size_t cid0_len,
1113 size_t cid1_len)
1114{
1115 mbedtls_cipher_info_t const *cipher_info;
1116 int ret = 0;
1117
1118#if defined(MBEDTLS_USE_PSA_CRYPTO)
1119 psa_key_type_t key_type;
1120 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1121 psa_algorithm_t alg;
1122 size_t key_bits;
1123 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1124#endif
1125
1126 size_t keylen, maclen, ivlen;
1127 unsigned char *key0 = NULL, *key1 = NULL;
1128 unsigned char *md0 = NULL, *md1 = NULL;
1129 unsigned char iv_enc[16], iv_dec[16];
1130
1131#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
1132 unsigned char cid0[SSL_CID_LEN_MIN];
1133 unsigned char cid1[SSL_CID_LEN_MIN];
1134
1135 mbedtls_test_rnd_std_rand(NULL, cid0, sizeof(cid0));
1136 mbedtls_test_rnd_std_rand(NULL, cid1, sizeof(cid1));
1137#else
1138 ((void) cid0_len);
1139 ((void) cid1_len);
1140#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
1141
1142 maclen = 0;
1143
1144 /* Pick cipher */
1145 cipher_info = mbedtls_cipher_info_from_type(cipher_type);
1146 CHK(cipher_info != NULL);
1147 CHK(cipher_info->iv_size <= 16);
1148 CHK(cipher_info->key_bitlen % 8 == 0);
1149
1150 /* Pick keys */
1151 keylen = cipher_info->key_bitlen / 8;
1152 /* Allocate `keylen + 1` bytes to ensure that we get
1153 * a non-NULL pointers from `mbedtls_calloc` even if
1154 * `keylen == 0` in the case of the NULL cipher. */
1155 CHK((key0 = mbedtls_calloc(1, keylen + 1)) != NULL);
1156 CHK((key1 = mbedtls_calloc(1, keylen + 1)) != NULL);
1157 memset(key0, 0x1, keylen);
1158 memset(key1, 0x2, keylen);
1159
1160#if !defined(MBEDTLS_USE_PSA_CRYPTO)
1161 /* Setup cipher contexts */
1162 CHK(mbedtls_cipher_setup(&t_in->cipher_ctx_enc, cipher_info) == 0);
1163 CHK(mbedtls_cipher_setup(&t_in->cipher_ctx_dec, cipher_info) == 0);
1164 CHK(mbedtls_cipher_setup(&t_out->cipher_ctx_enc, cipher_info) == 0);
1165 CHK(mbedtls_cipher_setup(&t_out->cipher_ctx_dec, cipher_info) == 0);
1166
1167#if defined(MBEDTLS_CIPHER_MODE_CBC)
1168 if (cipher_info->mode == MBEDTLS_MODE_CBC) {
1169 CHK(mbedtls_cipher_set_padding_mode(&t_in->cipher_ctx_enc,
1170 MBEDTLS_PADDING_NONE) == 0);
1171 CHK(mbedtls_cipher_set_padding_mode(&t_in->cipher_ctx_dec,
1172 MBEDTLS_PADDING_NONE) == 0);
1173 CHK(mbedtls_cipher_set_padding_mode(&t_out->cipher_ctx_enc,
1174 MBEDTLS_PADDING_NONE) == 0);
1175 CHK(mbedtls_cipher_set_padding_mode(&t_out->cipher_ctx_dec,
1176 MBEDTLS_PADDING_NONE) == 0);
1177 }
1178#endif /* MBEDTLS_CIPHER_MODE_CBC */
1179
1180 CHK(mbedtls_cipher_setkey(&t_in->cipher_ctx_enc, key0,
Yanray Wanga8f445e2022-11-03 11:51:59 +08001181 (keylen << 3 > INT_MAX) ? INT_MAX : (int) keylen << 3,
1182 MBEDTLS_ENCRYPT)
1183 == 0);
Yanray Wange6afd912022-10-27 12:11:18 +08001184 CHK(mbedtls_cipher_setkey(&t_in->cipher_ctx_dec, key1,
Yanray Wanga8f445e2022-11-03 11:51:59 +08001185 (keylen << 3 > INT_MAX) ? INT_MAX : (int) keylen << 3,
1186 MBEDTLS_DECRYPT)
1187 == 0);
Yanray Wange6afd912022-10-27 12:11:18 +08001188 CHK(mbedtls_cipher_setkey(&t_out->cipher_ctx_enc, key1,
Yanray Wanga8f445e2022-11-03 11:51:59 +08001189 (keylen << 3 > INT_MAX) ? INT_MAX : (int) keylen << 3,
1190 MBEDTLS_ENCRYPT)
1191 == 0);
Yanray Wange6afd912022-10-27 12:11:18 +08001192 CHK(mbedtls_cipher_setkey(&t_out->cipher_ctx_dec, key0,
Yanray Wanga8f445e2022-11-03 11:51:59 +08001193 (keylen << 3 > INT_MAX) ? INT_MAX : (int) keylen << 3,
1194 MBEDTLS_DECRYPT)
1195 == 0);
Yanray Wange6afd912022-10-27 12:11:18 +08001196#endif
1197
1198 /* Setup MAC contexts */
1199#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
1200 if (cipher_info->mode == MBEDTLS_MODE_CBC ||
1201 cipher_info->mode == MBEDTLS_MODE_STREAM) {
1202#if !defined(MBEDTLS_USE_PSA_CRYPTO)
1203 mbedtls_md_info_t const *md_info = mbedtls_md_info_from_type(hash_id);
1204 CHK(md_info != NULL);
1205#endif
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001206 maclen = mbedtls_md_get_size_from_type(hash_id);
Yanray Wange6afd912022-10-27 12:11:18 +08001207 CHK(maclen != 0);
1208 /* Pick hash keys */
1209 CHK((md0 = mbedtls_calloc(1, maclen)) != NULL);
1210 CHK((md1 = mbedtls_calloc(1, maclen)) != NULL);
1211 memset(md0, 0x5, maclen);
1212 memset(md1, 0x6, maclen);
1213
1214#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02001215 alg = mbedtls_md_psa_alg_from_type(hash_id);
Yanray Wange6afd912022-10-27 12:11:18 +08001216
1217 CHK(alg != 0);
1218
1219 t_out->psa_mac_alg = PSA_ALG_HMAC(alg);
1220 t_in->psa_mac_alg = PSA_ALG_HMAC(alg);
1221 t_in->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
1222 t_out->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
1223 t_in->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
1224 t_out->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
1225
1226 psa_reset_key_attributes(&attributes);
1227 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
1228 psa_set_key_algorithm(&attributes, PSA_ALG_HMAC(alg));
1229 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
1230
1231 CHK(psa_import_key(&attributes,
1232 md0, maclen,
1233 &t_in->psa_mac_enc) == PSA_SUCCESS);
1234
1235 CHK(psa_import_key(&attributes,
1236 md1, maclen,
1237 &t_out->psa_mac_enc) == PSA_SUCCESS);
1238
1239 if (cipher_info->mode == MBEDTLS_MODE_STREAM ||
1240 etm == MBEDTLS_SSL_ETM_DISABLED) {
1241 /* mbedtls_ct_hmac() requires the key to be exportable */
1242 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT |
1243 PSA_KEY_USAGE_VERIFY_HASH);
1244 } else {
1245 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
1246 }
1247
1248 CHK(psa_import_key(&attributes,
1249 md1, maclen,
1250 &t_in->psa_mac_dec) == PSA_SUCCESS);
1251
1252 CHK(psa_import_key(&attributes,
1253 md0, maclen,
1254 &t_out->psa_mac_dec) == PSA_SUCCESS);
1255#else
1256 CHK(mbedtls_md_setup(&t_out->md_ctx_enc, md_info, 1) == 0);
1257 CHK(mbedtls_md_setup(&t_out->md_ctx_dec, md_info, 1) == 0);
1258 CHK(mbedtls_md_setup(&t_in->md_ctx_enc, md_info, 1) == 0);
1259 CHK(mbedtls_md_setup(&t_in->md_ctx_dec, md_info, 1) == 0);
1260
1261 CHK(mbedtls_md_hmac_starts(&t_in->md_ctx_enc,
1262 md0, maclen) == 0);
1263 CHK(mbedtls_md_hmac_starts(&t_in->md_ctx_dec,
1264 md1, maclen) == 0);
1265 CHK(mbedtls_md_hmac_starts(&t_out->md_ctx_enc,
1266 md1, maclen) == 0);
1267 CHK(mbedtls_md_hmac_starts(&t_out->md_ctx_dec,
1268 md0, maclen) == 0);
1269#endif
1270 }
1271#else
1272 ((void) hash_id);
1273#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
1274
1275
1276 /* Pick IV's (regardless of whether they
1277 * are being used by the transform). */
1278 ivlen = cipher_info->iv_size;
1279 memset(iv_enc, 0x3, sizeof(iv_enc));
1280 memset(iv_dec, 0x4, sizeof(iv_dec));
1281
1282 /*
1283 * Setup transforms
1284 */
1285
1286#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
1287 defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
1288 t_out->encrypt_then_mac = etm;
1289 t_in->encrypt_then_mac = etm;
1290#else
1291 ((void) etm);
1292#endif
1293
1294 t_out->tls_version = tls_version;
1295 t_in->tls_version = tls_version;
1296 t_out->ivlen = ivlen;
1297 t_in->ivlen = ivlen;
1298
1299 switch (cipher_info->mode) {
1300 case MBEDTLS_MODE_GCM:
1301 case MBEDTLS_MODE_CCM:
1302#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1303 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
1304 t_out->fixed_ivlen = 12;
1305 t_in->fixed_ivlen = 12;
1306 } else
1307#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1308 {
1309 t_out->fixed_ivlen = 4;
1310 t_in->fixed_ivlen = 4;
1311 }
1312 t_out->maclen = 0;
1313 t_in->maclen = 0;
1314 switch (tag_mode) {
1315 case 0: /* Full tag */
1316 t_out->taglen = 16;
1317 t_in->taglen = 16;
1318 break;
1319 case 1: /* Partial tag */
1320 t_out->taglen = 8;
1321 t_in->taglen = 8;
1322 break;
1323 default:
1324 ret = 1;
1325 goto cleanup;
1326 }
1327 break;
1328
1329 case MBEDTLS_MODE_CHACHAPOLY:
1330 t_out->fixed_ivlen = 12;
1331 t_in->fixed_ivlen = 12;
1332 t_out->maclen = 0;
1333 t_in->maclen = 0;
1334 switch (tag_mode) {
1335 case 0: /* Full tag */
1336 t_out->taglen = 16;
1337 t_in->taglen = 16;
1338 break;
1339 case 1: /* Partial tag */
1340 t_out->taglen = 8;
1341 t_in->taglen = 8;
1342 break;
1343 default:
1344 ret = 1;
1345 goto cleanup;
1346 }
1347 break;
1348
1349 case MBEDTLS_MODE_STREAM:
1350 case MBEDTLS_MODE_CBC:
1351 t_out->fixed_ivlen = 0; /* redundant, must be 0 */
1352 t_in->fixed_ivlen = 0; /* redundant, must be 0 */
1353 t_out->taglen = 0;
1354 t_in->taglen = 0;
1355 switch (tag_mode) {
1356 case 0: /* Full tag */
1357 t_out->maclen = maclen;
1358 t_in->maclen = maclen;
1359 break;
1360 default:
1361 ret = 1;
1362 goto cleanup;
1363 }
1364 break;
1365 default:
1366 ret = 1;
1367 goto cleanup;
1368 break;
1369 }
1370
1371 /* Setup IV's */
1372
1373 memcpy(&t_in->iv_dec, iv_dec, sizeof(iv_dec));
1374 memcpy(&t_in->iv_enc, iv_enc, sizeof(iv_enc));
1375 memcpy(&t_out->iv_dec, iv_enc, sizeof(iv_enc));
1376 memcpy(&t_out->iv_enc, iv_dec, sizeof(iv_dec));
1377
1378#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
1379 /* Add CID */
1380 memcpy(&t_in->in_cid, cid0, cid0_len);
1381 memcpy(&t_in->out_cid, cid1, cid1_len);
Yanray Wanga8f445e2022-11-03 11:51:59 +08001382 t_in->in_cid_len = (uint8_t) cid0_len;
1383 t_in->out_cid_len = (uint8_t) cid1_len;
Yanray Wange6afd912022-10-27 12:11:18 +08001384 memcpy(&t_out->in_cid, cid1, cid1_len);
1385 memcpy(&t_out->out_cid, cid0, cid0_len);
Yanray Wanga8f445e2022-11-03 11:51:59 +08001386 t_out->in_cid_len = (uint8_t) cid1_len;
1387 t_out->out_cid_len = (uint8_t) cid0_len;
Yanray Wange6afd912022-10-27 12:11:18 +08001388#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
1389
1390#if defined(MBEDTLS_USE_PSA_CRYPTO)
1391 status = mbedtls_ssl_cipher_to_psa(cipher_type,
1392 t_in->taglen,
1393 &alg,
1394 &key_type,
1395 &key_bits);
1396
1397 if (status != PSA_SUCCESS) {
1398 ret = PSA_TO_MBEDTLS_ERR(status);
1399 goto cleanup;
1400 }
1401
1402 t_in->psa_alg = alg;
1403 t_out->psa_alg = alg;
1404
1405 if (alg != MBEDTLS_SSL_NULL_CIPHER) {
1406 psa_reset_key_attributes(&attributes);
1407 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
1408 psa_set_key_algorithm(&attributes, alg);
1409 psa_set_key_type(&attributes, key_type);
1410
1411 status = psa_import_key(&attributes,
1412 key0,
1413 PSA_BITS_TO_BYTES(key_bits),
1414 &t_in->psa_key_enc);
1415
1416 if (status != PSA_SUCCESS) {
1417 ret = PSA_TO_MBEDTLS_ERR(status);
1418 goto cleanup;
1419 }
1420
1421 status = psa_import_key(&attributes,
1422 key1,
1423 PSA_BITS_TO_BYTES(key_bits),
1424 &t_out->psa_key_enc);
1425
1426 if (status != PSA_SUCCESS) {
1427 ret = PSA_TO_MBEDTLS_ERR(status);
1428 goto cleanup;
1429 }
1430
1431 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
1432
1433 status = psa_import_key(&attributes,
1434 key1,
1435 PSA_BITS_TO_BYTES(key_bits),
1436 &t_in->psa_key_dec);
1437
1438 if (status != PSA_SUCCESS) {
1439 ret = PSA_TO_MBEDTLS_ERR(status);
1440 goto cleanup;
1441 }
1442
1443 status = psa_import_key(&attributes,
1444 key0,
1445 PSA_BITS_TO_BYTES(key_bits),
1446 &t_out->psa_key_dec);
1447
1448 if (status != PSA_SUCCESS) {
1449 ret = PSA_TO_MBEDTLS_ERR(status);
1450 goto cleanup;
1451 }
1452 }
1453#endif /* MBEDTLS_USE_PSA_CRYPTO */
1454
1455cleanup:
1456
1457 mbedtls_free(key0);
1458 mbedtls_free(key1);
1459
1460 mbedtls_free(md0);
1461 mbedtls_free(md1);
1462
1463 return ret;
1464}
1465
Yanray Wange6afd912022-10-27 12:11:18 +08001466int mbedtls_test_ssl_tls12_populate_session(mbedtls_ssl_session *session,
1467 int ticket_len,
1468 const char *crt_file)
1469{
1470#if defined(MBEDTLS_HAVE_TIME)
1471 session->start = mbedtls_time(NULL) - 42;
1472#endif
1473 session->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
1474 session->ciphersuite = 0xabcd;
1475 session->id_len = sizeof(session->id);
1476 memset(session->id, 66, session->id_len);
1477 memset(session->master, 17, sizeof(session->master));
1478
1479#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) && defined(MBEDTLS_FS_IO)
1480 if (crt_file != NULL && strlen(crt_file) != 0) {
1481 mbedtls_x509_crt tmp_crt;
1482 int ret;
1483
1484 mbedtls_x509_crt_init(&tmp_crt);
1485 ret = mbedtls_x509_crt_parse_file(&tmp_crt, crt_file);
1486 if (ret != 0) {
1487 return ret;
1488 }
1489
1490#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1491 /* Move temporary CRT. */
1492 session->peer_cert = mbedtls_calloc(1, sizeof(*session->peer_cert));
1493 if (session->peer_cert == NULL) {
1494 return -1;
1495 }
1496 *session->peer_cert = tmp_crt;
1497 memset(&tmp_crt, 0, sizeof(tmp_crt));
1498#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
1499 /* Calculate digest of temporary CRT. */
1500 session->peer_cert_digest =
1501 mbedtls_calloc(1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN);
1502 if (session->peer_cert_digest == NULL) {
1503 return -1;
1504 }
1505
1506#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02001507 psa_algorithm_t psa_alg = mbedtls_md_psa_alg_from_type(
Yanray Wange6afd912022-10-27 12:11:18 +08001508 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE);
1509 size_t hash_size = 0;
1510 psa_status_t status = psa_hash_compute(
1511 psa_alg, tmp_crt.raw.p,
1512 tmp_crt.raw.len,
1513 session->peer_cert_digest,
1514 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN,
1515 &hash_size);
1516 ret = PSA_TO_MBEDTLS_ERR(status);
1517#else
1518 ret = mbedtls_md(mbedtls_md_info_from_type(
1519 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE),
1520 tmp_crt.raw.p, tmp_crt.raw.len,
1521 session->peer_cert_digest);
1522#endif /* MBEDTLS_USE_PSA_CRYPTO */
1523 if (ret != 0) {
1524 return ret;
1525 }
1526 session->peer_cert_digest_type =
1527 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
1528 session->peer_cert_digest_len =
1529 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
1530#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
1531
1532 mbedtls_x509_crt_free(&tmp_crt);
1533 }
1534#else /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED && MBEDTLS_FS_IO */
1535 (void) crt_file;
1536#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED && MBEDTLS_FS_IO */
1537 session->verify_result = 0xdeadbeef;
1538
1539#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
1540 if (ticket_len != 0) {
1541 session->ticket = mbedtls_calloc(1, ticket_len);
1542 if (session->ticket == NULL) {
1543 return -1;
1544 }
1545 memset(session->ticket, 33, ticket_len);
1546 }
1547 session->ticket_len = ticket_len;
1548 session->ticket_lifetime = 86401;
1549#else
1550 (void) ticket_len;
1551#endif
1552
1553#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1554 session->mfl_code = 1;
1555#endif
1556#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1557 session->encrypt_then_mac = 1;
1558#endif
1559
1560 return 0;
1561}
1562
1563#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1564int mbedtls_test_ssl_tls13_populate_session(mbedtls_ssl_session *session,
1565 int ticket_len,
1566 int endpoint_type)
1567{
1568 ((void) ticket_len);
1569 session->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
1570 session->endpoint = endpoint_type == MBEDTLS_SSL_IS_CLIENT ?
1571 MBEDTLS_SSL_IS_CLIENT : MBEDTLS_SSL_IS_SERVER;
1572 session->ciphersuite = 0xabcd;
1573 session->ticket_age_add = 0x87654321;
1574 session->ticket_flags = 0x7;
1575
1576 session->resumption_key_len = 32;
1577 memset(session->resumption_key, 0x99, sizeof(session->resumption_key));
1578
1579#if defined(MBEDTLS_HAVE_TIME)
1580 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
1581 session->start = mbedtls_time(NULL) - 42;
1582 }
1583#endif
1584
1585#if defined(MBEDTLS_SSL_CLI_C)
1586 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
1587#if defined(MBEDTLS_HAVE_TIME)
1588 session->ticket_received = mbedtls_time(NULL) - 40;
1589#endif
1590 session->ticket_lifetime = 0xfedcba98;
1591
1592 session->ticket_len = ticket_len;
1593 if (ticket_len != 0) {
1594 session->ticket = mbedtls_calloc(1, ticket_len);
1595 if (session->ticket == NULL) {
1596 return -1;
1597 }
1598 memset(session->ticket, 33, ticket_len);
1599 }
1600 }
1601#endif /* MBEDTLS_SSL_CLI_C */
1602
1603 return 0;
1604}
1605#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1606
Yanray Wangb088bfc2023-03-16 12:15:49 +08001607int mbedtls_test_ssl_exchange_data(
1608 mbedtls_ssl_context *ssl_1,
1609 int msg_len_1, const int expected_fragments_1,
1610 mbedtls_ssl_context *ssl_2,
1611 int msg_len_2, const int expected_fragments_2)
Yanray Wange6afd912022-10-27 12:11:18 +08001612{
1613 unsigned char *msg_buf_1 = malloc(msg_len_1);
1614 unsigned char *msg_buf_2 = malloc(msg_len_2);
1615 unsigned char *in_buf_1 = malloc(msg_len_2);
1616 unsigned char *in_buf_2 = malloc(msg_len_1);
1617 int msg_type, ret = -1;
1618
1619 /* Perform this test with two message types. At first use a message
1620 * consisting of only 0x00 for the client and only 0xFF for the server.
1621 * At the second time use message with generated data */
1622 for (msg_type = 0; msg_type < 2; msg_type++) {
1623 int written_1 = 0;
1624 int written_2 = 0;
1625 int read_1 = 0;
1626 int read_2 = 0;
1627 int fragments_1 = 0;
1628 int fragments_2 = 0;
1629
1630 if (msg_type == 0) {
1631 memset(msg_buf_1, 0x00, msg_len_1);
1632 memset(msg_buf_2, 0xff, msg_len_2);
1633 } else {
1634 int i, j = 0;
1635 for (i = 0; i < msg_len_1; i++) {
1636 msg_buf_1[i] = j++ & 0xFF;
1637 }
1638 for (i = 0; i < msg_len_2; i++) {
1639 msg_buf_2[i] = (j -= 5) & 0xFF;
1640 }
1641 }
1642
1643 while (read_1 < msg_len_2 || read_2 < msg_len_1) {
1644 /* ssl_1 sending */
1645 if (msg_len_1 > written_1) {
1646 ret = mbedtls_ssl_write_fragment(ssl_1, msg_buf_1,
1647 msg_len_1, &written_1,
1648 expected_fragments_1);
1649 if (expected_fragments_1 == 0) {
1650 /* This error is expected when the message is too large and
1651 * cannot be fragmented */
1652 TEST_ASSERT(ret == MBEDTLS_ERR_SSL_BAD_INPUT_DATA);
1653 msg_len_1 = 0;
1654 } else {
1655 TEST_ASSERT(ret == 0);
1656 }
1657 }
1658
1659 /* ssl_2 sending */
1660 if (msg_len_2 > written_2) {
1661 ret = mbedtls_ssl_write_fragment(ssl_2, msg_buf_2,
1662 msg_len_2, &written_2,
1663 expected_fragments_2);
1664 if (expected_fragments_2 == 0) {
1665 /* This error is expected when the message is too large and
1666 * cannot be fragmented */
1667 TEST_ASSERT(ret == MBEDTLS_ERR_SSL_BAD_INPUT_DATA);
1668 msg_len_2 = 0;
1669 } else {
1670 TEST_ASSERT(ret == 0);
1671 }
1672 }
1673
1674 /* ssl_1 reading */
1675 if (read_1 < msg_len_2) {
1676 ret = mbedtls_ssl_read_fragment(ssl_1, in_buf_1,
1677 msg_len_2, &read_1,
1678 &fragments_2,
1679 expected_fragments_2);
1680 TEST_ASSERT(ret == 0);
1681 }
1682
1683 /* ssl_2 reading */
1684 if (read_2 < msg_len_1) {
1685 ret = mbedtls_ssl_read_fragment(ssl_2, in_buf_2,
1686 msg_len_1, &read_2,
1687 &fragments_1,
1688 expected_fragments_1);
1689 TEST_ASSERT(ret == 0);
1690 }
1691 }
1692
1693 ret = -1;
1694 TEST_ASSERT(0 == memcmp(msg_buf_1, in_buf_2, msg_len_1));
1695 TEST_ASSERT(0 == memcmp(msg_buf_2, in_buf_1, msg_len_2));
1696 TEST_ASSERT(fragments_1 == expected_fragments_1);
1697 TEST_ASSERT(fragments_2 == expected_fragments_2);
1698 }
1699
1700 ret = 0;
1701
1702exit:
1703 free(msg_buf_1);
1704 free(in_buf_1);
1705 free(msg_buf_2);
1706 free(in_buf_2);
1707
1708 return ret;
1709}
1710
1711/*
1712 * Perform data exchanging between \p ssl_1 and \p ssl_2. Both of endpoints
1713 * must be initialized and connected beforehand.
1714 *
1715 * \retval 0 on success, otherwise error code.
1716 */
Yanray Wangead70c82023-03-16 12:04:49 +08001717#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) && \
1718 (defined(MBEDTLS_SSL_RENEGOTIATION) || \
1719 defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH))
1720static int exchange_data(mbedtls_ssl_context *ssl_1,
1721 mbedtls_ssl_context *ssl_2)
Yanray Wange6afd912022-10-27 12:11:18 +08001722{
Yanray Wangb088bfc2023-03-16 12:15:49 +08001723 return mbedtls_test_ssl_exchange_data(ssl_1, 256, 1,
1724 ssl_2, 256, 1);
Yanray Wange6afd912022-10-27 12:11:18 +08001725}
Yanray Wangead70c82023-03-16 12:04:49 +08001726#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED &&
1727 (MBEDTLS_SSL_RENEGOTIATION ||
1728 MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) */
Yanray Wange6afd912022-10-27 12:11:18 +08001729
1730#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
1731static int check_ssl_version(
1732 mbedtls_ssl_protocol_version expected_negotiated_version,
1733 const mbedtls_ssl_context *ssl)
1734{
1735 const char *version_string = mbedtls_ssl_get_version(ssl);
1736 mbedtls_ssl_protocol_version version_number =
1737 mbedtls_ssl_get_version_number(ssl);
1738
1739 TEST_EQUAL(ssl->tls_version, expected_negotiated_version);
1740
1741 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1742 TEST_EQUAL(version_string[0], 'D');
1743 ++version_string;
1744 }
1745
1746 switch (expected_negotiated_version) {
1747 case MBEDTLS_SSL_VERSION_TLS1_2:
1748 TEST_EQUAL(version_number, MBEDTLS_SSL_VERSION_TLS1_2);
1749 TEST_ASSERT(strcmp(version_string, "TLSv1.2") == 0);
1750 break;
1751
1752 case MBEDTLS_SSL_VERSION_TLS1_3:
1753 TEST_EQUAL(version_number, MBEDTLS_SSL_VERSION_TLS1_3);
1754 TEST_ASSERT(strcmp(version_string, "TLSv1.3") == 0);
1755 break;
1756
1757 default:
1758 TEST_ASSERT(
1759 !"Version check not implemented for this protocol version");
1760 }
1761
1762 return 1;
1763
1764exit:
1765 return 0;
1766}
1767#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
1768
Yanray Wange6afd912022-10-27 12:11:18 +08001769#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
1770void mbedtls_test_ssl_perform_handshake(
1771 mbedtls_test_handshake_test_options *options)
1772{
1773 /* forced_ciphersuite needs to last until the end of the handshake */
1774 int forced_ciphersuite[2];
1775 enum { BUFFSIZE = 17000 };
1776 mbedtls_test_ssl_endpoint client, server;
1777#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
1778 const char *psk_identity = "foo";
1779#endif
1780#if defined(MBEDTLS_TIMING_C)
1781 mbedtls_timing_delay_context timer_client, timer_server;
1782#endif
1783#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1784 unsigned char *context_buf = NULL;
1785 size_t context_buf_len;
1786#endif
1787#if defined(MBEDTLS_SSL_RENEGOTIATION)
1788 int ret = -1;
1789#endif
1790 int expected_handshake_result = options->expected_handshake_result;
1791
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +01001792 MD_OR_USE_PSA_INIT();
Yanray Wange6afd912022-10-27 12:11:18 +08001793 mbedtls_platform_zeroize(&client, sizeof(client));
1794 mbedtls_platform_zeroize(&server, sizeof(server));
1795 mbedtls_test_ssl_message_queue server_queue, client_queue;
1796 mbedtls_test_message_socket_context server_context, client_context;
1797 mbedtls_test_message_socket_init(&server_context);
1798 mbedtls_test_message_socket_init(&client_context);
1799
1800 /* Client side */
1801 if (options->dtls != 0) {
1802 TEST_ASSERT(mbedtls_test_ssl_endpoint_init(&client,
1803 MBEDTLS_SSL_IS_CLIENT,
1804 options, &client_context,
1805 &client_queue,
1806 &server_queue, NULL) == 0);
1807#if defined(MBEDTLS_TIMING_C)
1808 mbedtls_ssl_set_timer_cb(&client.ssl, &timer_client,
1809 mbedtls_timing_set_delay,
1810 mbedtls_timing_get_delay);
1811#endif
1812 } else {
1813 TEST_ASSERT(mbedtls_test_ssl_endpoint_init(&client,
1814 MBEDTLS_SSL_IS_CLIENT,
1815 options, NULL, NULL,
1816 NULL, NULL) == 0);
1817 }
1818
Yanray Wange6afd912022-10-27 12:11:18 +08001819 if (strlen(options->cipher) > 0) {
1820 set_ciphersuite(&client.conf, options->cipher, forced_ciphersuite);
1821 }
1822
1823#if defined(MBEDTLS_DEBUG_C)
1824 if (options->cli_log_fun) {
1825 mbedtls_debug_set_threshold(4);
1826 mbedtls_ssl_conf_dbg(&client.conf, options->cli_log_fun,
1827 options->cli_log_obj);
1828 }
1829#endif
1830
1831 /* Server side */
1832 if (options->dtls != 0) {
1833 TEST_ASSERT(mbedtls_test_ssl_endpoint_init(&server,
1834 MBEDTLS_SSL_IS_SERVER,
1835 options, &server_context,
1836 &server_queue,
1837 &client_queue, NULL) == 0);
1838#if defined(MBEDTLS_TIMING_C)
1839 mbedtls_ssl_set_timer_cb(&server.ssl, &timer_server,
1840 mbedtls_timing_set_delay,
1841 mbedtls_timing_get_delay);
1842#endif
1843 } else {
1844 TEST_ASSERT(mbedtls_test_ssl_endpoint_init(&server,
1845 MBEDTLS_SSL_IS_SERVER,
1846 options, NULL, NULL, NULL,
1847 NULL) == 0);
1848 }
1849
1850 mbedtls_ssl_conf_authmode(&server.conf, options->srv_auth_mode);
1851
Yanray Wange6afd912022-10-27 12:11:18 +08001852#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1853 TEST_ASSERT(mbedtls_ssl_conf_max_frag_len(&(server.conf),
1854 (unsigned char) options->mfl)
1855 == 0);
1856 TEST_ASSERT(mbedtls_ssl_conf_max_frag_len(&(client.conf),
1857 (unsigned char) options->mfl)
1858 == 0);
1859#else
1860 TEST_ASSERT(MBEDTLS_SSL_MAX_FRAG_LEN_NONE == options->mfl);
1861#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
1862
1863#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
1864 if (options->psk_str != NULL && options->psk_str->len > 0) {
1865 TEST_ASSERT(mbedtls_ssl_conf_psk(
1866 &client.conf, options->psk_str->x,
1867 options->psk_str->len,
1868 (const unsigned char *) psk_identity,
1869 strlen(psk_identity)) == 0);
1870
1871 TEST_ASSERT(mbedtls_ssl_conf_psk(
1872 &server.conf, options->psk_str->x,
1873 options->psk_str->len,
1874 (const unsigned char *) psk_identity,
1875 strlen(psk_identity)) == 0);
1876#if defined(MBEDTLS_SSL_SRV_C)
1877 mbedtls_ssl_conf_psk_cb(&server.conf, psk_dummy_callback, NULL);
1878#endif
1879 }
1880#endif
1881#if defined(MBEDTLS_SSL_RENEGOTIATION)
1882 if (options->renegotiate) {
1883 mbedtls_ssl_conf_renegotiation(&(server.conf),
1884 MBEDTLS_SSL_RENEGOTIATION_ENABLED);
1885 mbedtls_ssl_conf_renegotiation(&(client.conf),
1886 MBEDTLS_SSL_RENEGOTIATION_ENABLED);
1887
1888 mbedtls_ssl_conf_legacy_renegotiation(&(server.conf),
1889 options->legacy_renegotiation);
1890 mbedtls_ssl_conf_legacy_renegotiation(&(client.conf),
1891 options->legacy_renegotiation);
1892 }
1893#endif /* MBEDTLS_SSL_RENEGOTIATION */
1894
1895#if defined(MBEDTLS_DEBUG_C)
1896 if (options->srv_log_fun) {
1897 mbedtls_debug_set_threshold(4);
1898 mbedtls_ssl_conf_dbg(&server.conf, options->srv_log_fun,
1899 options->srv_log_obj);
1900 }
1901#endif
1902
1903 TEST_ASSERT(mbedtls_test_mock_socket_connect(&(client.socket),
1904 &(server.socket),
1905 BUFFSIZE) == 0);
1906
1907#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1908 if (options->resize_buffers != 0) {
1909 /* Ensure that the buffer sizes are appropriate before resizes */
1910 TEST_ASSERT(client.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN);
1911 TEST_ASSERT(client.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN);
1912 TEST_ASSERT(server.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN);
1913 TEST_ASSERT(server.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN);
1914 }
1915#endif
1916
1917 if (options->expected_negotiated_version == MBEDTLS_SSL_VERSION_UNKNOWN) {
1918 expected_handshake_result = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
1919 }
1920
1921 TEST_ASSERT(mbedtls_test_move_handshake_to_state(&(client.ssl),
1922 &(server.ssl),
1923 MBEDTLS_SSL_HANDSHAKE_OVER)
1924 == expected_handshake_result);
1925
1926 if (expected_handshake_result != 0) {
1927 /* Connection will have failed by this point, skip to cleanup */
1928 goto exit;
1929 }
1930
1931 TEST_ASSERT(mbedtls_ssl_is_handshake_over(&client.ssl) == 1);
1932
1933 /* Make sure server state is moved to HANDSHAKE_OVER also. */
1934 TEST_EQUAL(mbedtls_test_move_handshake_to_state(&(server.ssl),
1935 &(client.ssl),
1936 MBEDTLS_SSL_HANDSHAKE_OVER),
1937 0);
1938
1939 TEST_ASSERT(mbedtls_ssl_is_handshake_over(&server.ssl) == 1);
1940 /* Check that both sides have negotiated the expected version. */
1941 mbedtls_test_set_step(0);
1942 if (!check_ssl_version(options->expected_negotiated_version,
1943 &client.ssl)) {
1944 goto exit;
1945 }
1946
1947 mbedtls_test_set_step(1);
1948 if (!check_ssl_version(options->expected_negotiated_version,
1949 &server.ssl)) {
1950 goto exit;
1951 }
1952
1953 if (options->expected_ciphersuite != 0) {
1954 TEST_EQUAL(server.ssl.session->ciphersuite,
1955 options->expected_ciphersuite);
1956 }
1957
1958#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1959 if (options->resize_buffers != 0) {
1960 /* A server, when using DTLS, might delay a buffer resize to happen
1961 * after it receives a message, so we force it. */
1962 TEST_ASSERT(exchange_data(&(client.ssl), &(server.ssl)) == 0);
1963
1964 TEST_ASSERT(client.ssl.out_buf_len ==
1965 mbedtls_ssl_get_output_buflen(&client.ssl));
1966 TEST_ASSERT(client.ssl.in_buf_len ==
1967 mbedtls_ssl_get_input_buflen(&client.ssl));
1968 TEST_ASSERT(server.ssl.out_buf_len ==
1969 mbedtls_ssl_get_output_buflen(&server.ssl));
1970 TEST_ASSERT(server.ssl.in_buf_len ==
1971 mbedtls_ssl_get_input_buflen(&server.ssl));
1972 }
1973#endif
1974
1975 if (options->cli_msg_len != 0 || options->srv_msg_len != 0) {
1976 /* Start data exchanging test */
Yanray Wangb088bfc2023-03-16 12:15:49 +08001977 TEST_ASSERT(mbedtls_test_ssl_exchange_data(
1978 &(client.ssl), options->cli_msg_len,
1979 options->expected_cli_fragments,
1980 &(server.ssl), options->srv_msg_len,
1981 options->expected_srv_fragments)
Yanray Wange6afd912022-10-27 12:11:18 +08001982 == 0);
1983 }
1984#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1985 if (options->serialize == 1) {
1986 TEST_ASSERT(options->dtls == 1);
1987
1988 TEST_ASSERT(mbedtls_ssl_context_save(&(server.ssl), NULL,
1989 0, &context_buf_len)
1990 == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL);
1991
1992 context_buf = mbedtls_calloc(1, context_buf_len);
1993 TEST_ASSERT(context_buf != NULL);
1994
1995 TEST_ASSERT(mbedtls_ssl_context_save(&(server.ssl), context_buf,
1996 context_buf_len,
1997 &context_buf_len)
1998 == 0);
1999
2000 mbedtls_ssl_free(&(server.ssl));
2001 mbedtls_ssl_init(&(server.ssl));
2002
2003 TEST_ASSERT(mbedtls_ssl_setup(&(server.ssl), &(server.conf)) == 0);
2004
2005 mbedtls_ssl_set_bio(&(server.ssl), &server_context,
2006 mbedtls_test_mock_tcp_send_msg,
2007 mbedtls_test_mock_tcp_recv_msg,
2008 NULL);
2009
2010 mbedtls_ssl_set_user_data_p(&server.ssl, &server);
2011
2012#if defined(MBEDTLS_TIMING_C)
2013 mbedtls_ssl_set_timer_cb(&server.ssl, &timer_server,
2014 mbedtls_timing_set_delay,
2015 mbedtls_timing_get_delay);
2016#endif
2017#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2018 if (options->resize_buffers != 0) {
2019 /* Ensure that the buffer sizes are appropriate before resizes */
2020 TEST_ASSERT(server.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN);
2021 TEST_ASSERT(server.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN);
2022 }
2023#endif
2024 TEST_ASSERT(mbedtls_ssl_context_load(&(server.ssl), context_buf,
2025 context_buf_len) == 0);
2026
2027#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2028 /* Validate buffer sizes after context deserialization */
2029 if (options->resize_buffers != 0) {
2030 TEST_ASSERT(server.ssl.out_buf_len ==
2031 mbedtls_ssl_get_output_buflen(&server.ssl));
2032 TEST_ASSERT(server.ssl.in_buf_len ==
2033 mbedtls_ssl_get_input_buflen(&server.ssl));
2034 }
2035#endif
2036 /* Retest writing/reading */
2037 if (options->cli_msg_len != 0 || options->srv_msg_len != 0) {
Yanray Wangb088bfc2023-03-16 12:15:49 +08002038 TEST_ASSERT(mbedtls_test_ssl_exchange_data(
2039 &(client.ssl), options->cli_msg_len,
Yanray Wange6afd912022-10-27 12:11:18 +08002040 options->expected_cli_fragments,
Yanray Wangb088bfc2023-03-16 12:15:49 +08002041 &(server.ssl), options->srv_msg_len,
Yanray Wange6afd912022-10-27 12:11:18 +08002042 options->expected_srv_fragments)
2043 == 0);
2044 }
2045 }
2046#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
2047
2048#if defined(MBEDTLS_SSL_RENEGOTIATION)
2049 if (options->renegotiate) {
2050 /* Start test with renegotiation */
2051 TEST_ASSERT(server.ssl.renego_status ==
2052 MBEDTLS_SSL_INITIAL_HANDSHAKE);
2053 TEST_ASSERT(client.ssl.renego_status ==
2054 MBEDTLS_SSL_INITIAL_HANDSHAKE);
2055
2056 /* After calling this function for the server, it only sends a handshake
2057 * request. All renegotiation should happen during data exchanging */
2058 TEST_ASSERT(mbedtls_ssl_renegotiate(&(server.ssl)) == 0);
2059 TEST_ASSERT(server.ssl.renego_status ==
2060 MBEDTLS_SSL_RENEGOTIATION_PENDING);
2061 TEST_ASSERT(client.ssl.renego_status ==
2062 MBEDTLS_SSL_INITIAL_HANDSHAKE);
2063
2064 TEST_ASSERT(exchange_data(&(client.ssl), &(server.ssl)) == 0);
2065 TEST_ASSERT(server.ssl.renego_status ==
2066 MBEDTLS_SSL_RENEGOTIATION_DONE);
2067 TEST_ASSERT(client.ssl.renego_status ==
2068 MBEDTLS_SSL_RENEGOTIATION_DONE);
2069
2070 /* After calling mbedtls_ssl_renegotiate for the client,
2071 * all renegotiation should happen inside this function.
2072 * However in this test, we cannot perform simultaneous communication
2073 * between client and server so this function will return waiting error
2074 * on the socket. All rest of renegotiation should happen
2075 * during data exchanging */
2076 ret = mbedtls_ssl_renegotiate(&(client.ssl));
2077#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2078 if (options->resize_buffers != 0) {
2079 /* Ensure that the buffer sizes are appropriate before resizes */
2080 TEST_ASSERT(client.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN);
2081 TEST_ASSERT(client.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN);
2082 }
2083#endif
2084 TEST_ASSERT(ret == 0 ||
2085 ret == MBEDTLS_ERR_SSL_WANT_READ ||
2086 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
2087 TEST_ASSERT(server.ssl.renego_status ==
2088 MBEDTLS_SSL_RENEGOTIATION_DONE);
2089 TEST_ASSERT(client.ssl.renego_status ==
2090 MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS);
2091
2092 TEST_ASSERT(exchange_data(&(client.ssl), &(server.ssl)) == 0);
2093 TEST_ASSERT(server.ssl.renego_status ==
2094 MBEDTLS_SSL_RENEGOTIATION_DONE);
2095 TEST_ASSERT(client.ssl.renego_status ==
2096 MBEDTLS_SSL_RENEGOTIATION_DONE);
2097#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2098 /* Validate buffer sizes after renegotiation */
2099 if (options->resize_buffers != 0) {
2100 TEST_ASSERT(client.ssl.out_buf_len ==
2101 mbedtls_ssl_get_output_buflen(&client.ssl));
2102 TEST_ASSERT(client.ssl.in_buf_len ==
2103 mbedtls_ssl_get_input_buflen(&client.ssl));
2104 TEST_ASSERT(server.ssl.out_buf_len ==
2105 mbedtls_ssl_get_output_buflen(&server.ssl));
2106 TEST_ASSERT(server.ssl.in_buf_len ==
2107 mbedtls_ssl_get_input_buflen(&server.ssl));
2108 }
2109#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
2110 }
2111#endif /* MBEDTLS_SSL_RENEGOTIATION */
2112
2113 TEST_ASSERT(mbedtls_ssl_conf_get_user_data_p(&client.conf) == &client);
2114 TEST_ASSERT(mbedtls_ssl_get_user_data_p(&client.ssl) == &client);
2115 TEST_ASSERT(mbedtls_ssl_conf_get_user_data_p(&server.conf) == &server);
2116 TEST_ASSERT(mbedtls_ssl_get_user_data_p(&server.ssl) == &server);
2117
2118exit:
2119 mbedtls_test_ssl_endpoint_free(&client,
Yanray Wangaf727a22023-03-13 19:22:36 +08002120 options->dtls != 0 ? &client_context : NULL);
Yanray Wange6afd912022-10-27 12:11:18 +08002121 mbedtls_test_ssl_endpoint_free(&server,
Yanray Wangaf727a22023-03-13 19:22:36 +08002122 options->dtls != 0 ? &server_context : NULL);
Yanray Wange6afd912022-10-27 12:11:18 +08002123#if defined(MBEDTLS_DEBUG_C)
2124 if (options->cli_log_fun || options->srv_log_fun) {
2125 mbedtls_debug_set_threshold(0);
2126 }
2127#endif
2128#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
2129 if (context_buf != NULL) {
2130 mbedtls_free(context_buf);
2131 }
2132#endif
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +01002133 MD_OR_USE_PSA_DONE();
Yanray Wange6afd912022-10-27 12:11:18 +08002134}
2135#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
2136
2137#if defined(MBEDTLS_TEST_HOOKS)
Yanray Wangf56181a2023-03-16 12:21:33 +08002138int mbedtls_test_tweak_tls13_certificate_msg_vector_len(
Yanray Wange6afd912022-10-27 12:11:18 +08002139 unsigned char *buf, unsigned char **end, int tweak,
2140 int *expected_result, mbedtls_ssl_chk_buf_ptr_args *args)
2141{
2142/*
2143 * The definition of the tweaks assume that the certificate list contains only
2144 * one certificate.
2145 */
2146
2147/*
2148 * struct {
2149 * opaque cert_data<1..2^24-1>;
2150 * Extension extensions<0..2^16-1>;
2151 * } CertificateEntry;
2152 *
2153 * struct {
2154 * opaque certificate_request_context<0..2^8-1>;
2155 * CertificateEntry certificate_list<0..2^24-1>;
2156 * } Certificate;
2157 */
2158 unsigned char *p_certificate_request_context_len = buf;
2159 size_t certificate_request_context_len = buf[0];
2160
2161 unsigned char *p_certificate_list_len =
2162 buf + 1 + certificate_request_context_len;
2163 unsigned char *certificate_list = p_certificate_list_len + 3;
2164 size_t certificate_list_len =
2165 MBEDTLS_GET_UINT24_BE(p_certificate_list_len, 0);
2166
2167 unsigned char *p_cert_data_len = certificate_list;
2168 unsigned char *cert_data = p_cert_data_len + 3;
2169 size_t cert_data_len = MBEDTLS_GET_UINT24_BE(p_cert_data_len, 0);
2170
2171 unsigned char *p_extensions_len = cert_data + cert_data_len;
2172 unsigned char *extensions = p_extensions_len + 2;
2173 size_t extensions_len = MBEDTLS_GET_UINT16_BE(p_extensions_len, 0);
2174
2175 *expected_result = MBEDTLS_ERR_SSL_DECODE_ERROR;
2176
2177 switch (tweak) {
2178 case 1:
2179 /* Failure when checking if the certificate request context length
2180 * and certificate list length can be read
2181 */
2182 *end = buf + 3;
2183 set_chk_buf_ptr_args(args, buf, *end, 4);
2184 break;
2185
2186 case 2:
2187 /* Invalid certificate request context length.
2188 */
2189 *p_certificate_request_context_len =
Yanray Wanga8f445e2022-11-03 11:51:59 +08002190 (unsigned char) certificate_request_context_len + 1;
Yanray Wange6afd912022-10-27 12:11:18 +08002191 reset_chk_buf_ptr_args(args);
2192 break;
2193
2194 case 3:
2195 /* Failure when checking if certificate_list data can be read. */
2196 MBEDTLS_PUT_UINT24_BE(certificate_list_len + 1,
2197 p_certificate_list_len, 0);
2198 set_chk_buf_ptr_args(args, certificate_list, *end,
2199 certificate_list_len + 1);
2200 break;
2201
2202 case 4:
2203 /* Failure when checking if the cert_data length can be read. */
2204 MBEDTLS_PUT_UINT24_BE(2, p_certificate_list_len, 0);
2205 set_chk_buf_ptr_args(args, p_cert_data_len, certificate_list + 2, 3);
2206 break;
2207
2208 case 5:
2209 /* Failure when checking if cert_data data can be read. */
2210 MBEDTLS_PUT_UINT24_BE(certificate_list_len - 3 + 1,
2211 p_cert_data_len, 0);
2212 set_chk_buf_ptr_args(args, cert_data,
2213 certificate_list + certificate_list_len,
2214 certificate_list_len - 3 + 1);
2215 break;
2216
2217 case 6:
2218 /* Failure when checking if the extensions length can be read. */
2219 MBEDTLS_PUT_UINT24_BE(certificate_list_len - extensions_len - 1,
2220 p_certificate_list_len, 0);
2221 set_chk_buf_ptr_args(
2222 args, p_extensions_len,
2223 certificate_list + certificate_list_len - extensions_len - 1, 2);
2224 break;
2225
2226 case 7:
2227 /* Failure when checking if extensions data can be read. */
2228 MBEDTLS_PUT_UINT16_BE(extensions_len + 1, p_extensions_len, 0);
2229
2230 set_chk_buf_ptr_args(
2231 args, extensions,
2232 certificate_list + certificate_list_len, extensions_len + 1);
2233 break;
2234
2235 default:
2236 return -1;
2237 }
2238
2239 return 0;
2240}
2241#endif /* MBEDTLS_TEST_HOOKS */
Yanray Wang4d07d1c2022-10-27 15:28:16 +08002242#endif /* MBEDTLS_SSL_TLS_C */