blob: 12218634e7ca12154a1370c435eda179786010b1 [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{
929 /* Verify that calling mbedtls_ssl_write with a NULL buffer and zero length is
930 * a valid no-op for TLS connections. */
931 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
932 TEST_ASSERT(mbedtls_ssl_write(ssl, NULL, 0) == 0);
933 }
934
935 int ret = mbedtls_ssl_write(ssl, buf + *written, buf_len - *written);
936 if (ret > 0) {
937 *written += ret;
938 }
939
940 if (expected_fragments == 0) {
941 /* Used for DTLS and the message size larger than MFL. In that case
942 * the message can not be fragmented and the library should return
943 * MBEDTLS_ERR_SSL_BAD_INPUT_DATA error. This error must be returned
Yanray Wangb088bfc2023-03-16 12:15:49 +0800944 * to prevent a dead loop inside mbedtls_test_ssl_exchange_data(). */
Yanray Wange6afd912022-10-27 12:11:18 +0800945 return ret;
946 } else if (expected_fragments == 1) {
947 /* Used for TLS/DTLS and the message size lower than MFL */
948 TEST_ASSERT(ret == buf_len ||
949 ret == MBEDTLS_ERR_SSL_WANT_READ ||
950 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
951 } else {
952 /* Used for TLS and the message size larger than MFL */
953 TEST_ASSERT(expected_fragments > 1);
954 TEST_ASSERT((ret >= 0 && ret <= buf_len) ||
955 ret == MBEDTLS_ERR_SSL_WANT_READ ||
956 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
957 }
958
959 return 0;
960
961exit:
962 /* Some of the tests failed */
963 return -1;
964}
965
966/*
967 * Read application data and increase read counter and fragments counter
968 * if necessary.
969 */
Yanray Wang1fca4de2023-02-06 12:10:48 +0800970int mbedtls_ssl_read_fragment(mbedtls_ssl_context *ssl,
971 unsigned char *buf, int buf_len,
972 int *read, int *fragments,
973 const int expected_fragments)
Yanray Wange6afd912022-10-27 12:11:18 +0800974{
975 /* Verify that calling mbedtls_ssl_write with a NULL buffer and zero length is
976 * a valid no-op for TLS connections. */
977 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
978 TEST_ASSERT(mbedtls_ssl_read(ssl, NULL, 0) == 0);
979 }
980
981 int ret = mbedtls_ssl_read(ssl, buf + *read, buf_len - *read);
982 if (ret > 0) {
983 (*fragments)++;
984 *read += ret;
985 }
986
987 if (expected_fragments == 0) {
988 TEST_ASSERT(ret == 0);
989 } else if (expected_fragments == 1) {
990 TEST_ASSERT(ret == buf_len ||
991 ret == MBEDTLS_ERR_SSL_WANT_READ ||
992 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
993 } else {
994 TEST_ASSERT(expected_fragments > 1);
995 TEST_ASSERT((ret >= 0 && ret <= buf_len) ||
996 ret == MBEDTLS_ERR_SSL_WANT_READ ||
997 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
998 }
999
1000 return 0;
1001
1002exit:
1003 /* Some of the tests failed */
1004 return -1;
1005}
1006
Yanray Wangead70c82023-03-16 12:04:49 +08001007#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
1008static void set_ciphersuite(mbedtls_ssl_config *conf, const char *cipher,
1009 int *forced_ciphersuite)
Yanray Wange6afd912022-10-27 12:11:18 +08001010{
1011 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
1012 forced_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id(cipher);
1013 forced_ciphersuite[1] = 0;
1014
1015 ciphersuite_info =
1016 mbedtls_ssl_ciphersuite_from_id(forced_ciphersuite[0]);
1017
1018 TEST_ASSERT(ciphersuite_info != NULL);
1019 TEST_ASSERT(ciphersuite_info->min_tls_version <= conf->max_tls_version);
1020 TEST_ASSERT(ciphersuite_info->max_tls_version >= conf->min_tls_version);
1021
1022 if (conf->max_tls_version > ciphersuite_info->max_tls_version) {
Agathiyan Bragadeesh2f017a82023-07-12 11:21:54 +01001023 conf->max_tls_version = (mbedtls_ssl_protocol_version) ciphersuite_info->max_tls_version;
Yanray Wange6afd912022-10-27 12:11:18 +08001024 }
1025 if (conf->min_tls_version < ciphersuite_info->min_tls_version) {
Agathiyan Bragadeesh2f017a82023-07-12 11:21:54 +01001026 conf->min_tls_version = (mbedtls_ssl_protocol_version) ciphersuite_info->min_tls_version;
Yanray Wange6afd912022-10-27 12:11:18 +08001027 }
1028
1029 mbedtls_ssl_conf_ciphersuites(conf, forced_ciphersuite);
1030
1031exit:
1032 return;
1033}
Yanray Wangead70c82023-03-16 12:04:49 +08001034#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Yanray Wange6afd912022-10-27 12:11:18 +08001035
Yanray Wangead70c82023-03-16 12:04:49 +08001036#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) && \
1037 defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED) && \
1038 defined(MBEDTLS_SSL_SRV_C)
1039static int psk_dummy_callback(void *p_info, mbedtls_ssl_context *ssl,
1040 const unsigned char *name, size_t name_len)
Yanray Wange6afd912022-10-27 12:11:18 +08001041{
1042 (void) p_info;
1043 (void) ssl;
1044 (void) name;
1045 (void) name_len;
1046
1047 return 0;
1048}
Yanray Wangead70c82023-03-16 12:04:49 +08001049#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED &&
1050 MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED &&
1051 MBEDTLS_SSL_SRV_C */
Yanray Wange6afd912022-10-27 12:11:18 +08001052
1053#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
1054 defined(MBEDTLS_CIPHER_MODE_CBC) && defined(MBEDTLS_AES_C)
1055int mbedtls_test_psa_cipher_encrypt_helper(mbedtls_ssl_transform *transform,
1056 const unsigned char *iv,
1057 size_t iv_len,
1058 const unsigned char *input,
1059 size_t ilen,
1060 unsigned char *output,
1061 size_t *olen)
1062{
1063#if defined(MBEDTLS_USE_PSA_CRYPTO)
1064 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1065 psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT;
1066 size_t part_len;
1067
1068 status = psa_cipher_encrypt_setup(&cipher_op,
1069 transform->psa_key_enc,
1070 transform->psa_alg);
1071
1072 if (status != PSA_SUCCESS) {
1073 return PSA_TO_MBEDTLS_ERR(status);
1074 }
1075
1076 status = psa_cipher_set_iv(&cipher_op, iv, iv_len);
1077
1078 if (status != PSA_SUCCESS) {
1079 return PSA_TO_MBEDTLS_ERR(status);
1080 }
1081
1082 status = psa_cipher_update(&cipher_op, input, ilen, output, ilen, olen);
1083
1084 if (status != PSA_SUCCESS) {
1085 return PSA_TO_MBEDTLS_ERR(status);
1086 }
1087
1088 status = psa_cipher_finish(&cipher_op, output + *olen, ilen - *olen,
1089 &part_len);
1090
1091 if (status != PSA_SUCCESS) {
1092 return PSA_TO_MBEDTLS_ERR(status);
1093 }
1094
1095 *olen += part_len;
1096 return 0;
1097#else
1098 return mbedtls_cipher_crypt(&transform->cipher_ctx_enc,
1099 iv, iv_len, input, ilen, output, olen);
1100#endif /* MBEDTLS_USE_PSA_CRYPTO */
1101}
1102#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_CIPHER_MODE_CBC &&
1103 MBEDTLS_AES_C */
1104
1105int mbedtls_test_ssl_build_transforms(mbedtls_ssl_transform *t_in,
1106 mbedtls_ssl_transform *t_out,
1107 int cipher_type, int hash_id,
1108 int etm, int tag_mode,
1109 mbedtls_ssl_protocol_version tls_version,
1110 size_t cid0_len,
1111 size_t cid1_len)
1112{
1113 mbedtls_cipher_info_t const *cipher_info;
1114 int ret = 0;
1115
1116#if defined(MBEDTLS_USE_PSA_CRYPTO)
1117 psa_key_type_t key_type;
1118 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1119 psa_algorithm_t alg;
1120 size_t key_bits;
1121 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1122#endif
1123
1124 size_t keylen, maclen, ivlen;
1125 unsigned char *key0 = NULL, *key1 = NULL;
1126 unsigned char *md0 = NULL, *md1 = NULL;
1127 unsigned char iv_enc[16], iv_dec[16];
1128
1129#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
1130 unsigned char cid0[SSL_CID_LEN_MIN];
1131 unsigned char cid1[SSL_CID_LEN_MIN];
1132
1133 mbedtls_test_rnd_std_rand(NULL, cid0, sizeof(cid0));
1134 mbedtls_test_rnd_std_rand(NULL, cid1, sizeof(cid1));
1135#else
1136 ((void) cid0_len);
1137 ((void) cid1_len);
1138#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
1139
1140 maclen = 0;
1141
1142 /* Pick cipher */
Agathiyan Bragadeesh2f017a82023-07-12 11:21:54 +01001143 cipher_info = mbedtls_cipher_info_from_type((mbedtls_cipher_type_t) cipher_type);
Yanray Wange6afd912022-10-27 12:11:18 +08001144 CHK(cipher_info != NULL);
1145 CHK(cipher_info->iv_size <= 16);
1146 CHK(cipher_info->key_bitlen % 8 == 0);
1147
1148 /* Pick keys */
1149 keylen = cipher_info->key_bitlen / 8;
1150 /* Allocate `keylen + 1` bytes to ensure that we get
1151 * a non-NULL pointers from `mbedtls_calloc` even if
1152 * `keylen == 0` in the case of the NULL cipher. */
1153 CHK((key0 = mbedtls_calloc(1, keylen + 1)) != NULL);
1154 CHK((key1 = mbedtls_calloc(1, keylen + 1)) != NULL);
1155 memset(key0, 0x1, keylen);
1156 memset(key1, 0x2, keylen);
1157
1158#if !defined(MBEDTLS_USE_PSA_CRYPTO)
1159 /* Setup cipher contexts */
1160 CHK(mbedtls_cipher_setup(&t_in->cipher_ctx_enc, cipher_info) == 0);
1161 CHK(mbedtls_cipher_setup(&t_in->cipher_ctx_dec, cipher_info) == 0);
1162 CHK(mbedtls_cipher_setup(&t_out->cipher_ctx_enc, cipher_info) == 0);
1163 CHK(mbedtls_cipher_setup(&t_out->cipher_ctx_dec, cipher_info) == 0);
1164
1165#if defined(MBEDTLS_CIPHER_MODE_CBC)
1166 if (cipher_info->mode == MBEDTLS_MODE_CBC) {
1167 CHK(mbedtls_cipher_set_padding_mode(&t_in->cipher_ctx_enc,
1168 MBEDTLS_PADDING_NONE) == 0);
1169 CHK(mbedtls_cipher_set_padding_mode(&t_in->cipher_ctx_dec,
1170 MBEDTLS_PADDING_NONE) == 0);
1171 CHK(mbedtls_cipher_set_padding_mode(&t_out->cipher_ctx_enc,
1172 MBEDTLS_PADDING_NONE) == 0);
1173 CHK(mbedtls_cipher_set_padding_mode(&t_out->cipher_ctx_dec,
1174 MBEDTLS_PADDING_NONE) == 0);
1175 }
1176#endif /* MBEDTLS_CIPHER_MODE_CBC */
1177
1178 CHK(mbedtls_cipher_setkey(&t_in->cipher_ctx_enc, key0,
Yanray Wanga8f445e2022-11-03 11:51:59 +08001179 (keylen << 3 > INT_MAX) ? INT_MAX : (int) keylen << 3,
1180 MBEDTLS_ENCRYPT)
1181 == 0);
Yanray Wange6afd912022-10-27 12:11:18 +08001182 CHK(mbedtls_cipher_setkey(&t_in->cipher_ctx_dec, key1,
Yanray Wanga8f445e2022-11-03 11:51:59 +08001183 (keylen << 3 > INT_MAX) ? INT_MAX : (int) keylen << 3,
1184 MBEDTLS_DECRYPT)
1185 == 0);
Yanray Wange6afd912022-10-27 12:11:18 +08001186 CHK(mbedtls_cipher_setkey(&t_out->cipher_ctx_enc, key1,
Yanray Wanga8f445e2022-11-03 11:51:59 +08001187 (keylen << 3 > INT_MAX) ? INT_MAX : (int) keylen << 3,
1188 MBEDTLS_ENCRYPT)
1189 == 0);
Yanray Wange6afd912022-10-27 12:11:18 +08001190 CHK(mbedtls_cipher_setkey(&t_out->cipher_ctx_dec, key0,
Yanray Wanga8f445e2022-11-03 11:51:59 +08001191 (keylen << 3 > INT_MAX) ? INT_MAX : (int) keylen << 3,
1192 MBEDTLS_DECRYPT)
1193 == 0);
Yanray Wange6afd912022-10-27 12:11:18 +08001194#endif
1195
1196 /* Setup MAC contexts */
1197#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
1198 if (cipher_info->mode == MBEDTLS_MODE_CBC ||
1199 cipher_info->mode == MBEDTLS_MODE_STREAM) {
1200#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Agathiyan Bragadeesh2f017a82023-07-12 11:21:54 +01001201 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 +08001202 CHK(md_info != NULL);
1203#endif
Agathiyan Bragadeesh2f017a82023-07-12 11:21:54 +01001204 maclen = mbedtls_md_get_size_from_type((mbedtls_md_type_t) hash_id);
Yanray Wange6afd912022-10-27 12:11:18 +08001205 CHK(maclen != 0);
1206 /* Pick hash keys */
1207 CHK((md0 = mbedtls_calloc(1, maclen)) != NULL);
1208 CHK((md1 = mbedtls_calloc(1, maclen)) != NULL);
1209 memset(md0, 0x5, maclen);
1210 memset(md1, 0x6, maclen);
1211
1212#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02001213 alg = mbedtls_md_psa_alg_from_type(hash_id);
Yanray Wange6afd912022-10-27 12:11:18 +08001214
1215 CHK(alg != 0);
1216
1217 t_out->psa_mac_alg = PSA_ALG_HMAC(alg);
1218 t_in->psa_mac_alg = PSA_ALG_HMAC(alg);
1219 t_in->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
1220 t_out->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
1221 t_in->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
1222 t_out->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
1223
1224 psa_reset_key_attributes(&attributes);
1225 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
1226 psa_set_key_algorithm(&attributes, PSA_ALG_HMAC(alg));
1227 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
1228
1229 CHK(psa_import_key(&attributes,
1230 md0, maclen,
1231 &t_in->psa_mac_enc) == PSA_SUCCESS);
1232
1233 CHK(psa_import_key(&attributes,
1234 md1, maclen,
1235 &t_out->psa_mac_enc) == PSA_SUCCESS);
1236
1237 if (cipher_info->mode == MBEDTLS_MODE_STREAM ||
1238 etm == MBEDTLS_SSL_ETM_DISABLED) {
1239 /* mbedtls_ct_hmac() requires the key to be exportable */
1240 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT |
1241 PSA_KEY_USAGE_VERIFY_HASH);
1242 } else {
1243 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
1244 }
1245
1246 CHK(psa_import_key(&attributes,
1247 md1, maclen,
1248 &t_in->psa_mac_dec) == PSA_SUCCESS);
1249
1250 CHK(psa_import_key(&attributes,
1251 md0, maclen,
1252 &t_out->psa_mac_dec) == PSA_SUCCESS);
1253#else
1254 CHK(mbedtls_md_setup(&t_out->md_ctx_enc, md_info, 1) == 0);
1255 CHK(mbedtls_md_setup(&t_out->md_ctx_dec, md_info, 1) == 0);
1256 CHK(mbedtls_md_setup(&t_in->md_ctx_enc, md_info, 1) == 0);
1257 CHK(mbedtls_md_setup(&t_in->md_ctx_dec, md_info, 1) == 0);
1258
1259 CHK(mbedtls_md_hmac_starts(&t_in->md_ctx_enc,
1260 md0, maclen) == 0);
1261 CHK(mbedtls_md_hmac_starts(&t_in->md_ctx_dec,
1262 md1, maclen) == 0);
1263 CHK(mbedtls_md_hmac_starts(&t_out->md_ctx_enc,
1264 md1, maclen) == 0);
1265 CHK(mbedtls_md_hmac_starts(&t_out->md_ctx_dec,
1266 md0, maclen) == 0);
1267#endif
1268 }
1269#else
1270 ((void) hash_id);
1271#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
1272
1273
1274 /* Pick IV's (regardless of whether they
1275 * are being used by the transform). */
1276 ivlen = cipher_info->iv_size;
1277 memset(iv_enc, 0x3, sizeof(iv_enc));
1278 memset(iv_dec, 0x4, sizeof(iv_dec));
1279
1280 /*
1281 * Setup transforms
1282 */
1283
1284#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
1285 defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
1286 t_out->encrypt_then_mac = etm;
1287 t_in->encrypt_then_mac = etm;
1288#else
1289 ((void) etm);
1290#endif
1291
1292 t_out->tls_version = tls_version;
1293 t_in->tls_version = tls_version;
1294 t_out->ivlen = ivlen;
1295 t_in->ivlen = ivlen;
1296
1297 switch (cipher_info->mode) {
1298 case MBEDTLS_MODE_GCM:
1299 case MBEDTLS_MODE_CCM:
1300#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1301 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
1302 t_out->fixed_ivlen = 12;
1303 t_in->fixed_ivlen = 12;
1304 } else
1305#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1306 {
1307 t_out->fixed_ivlen = 4;
1308 t_in->fixed_ivlen = 4;
1309 }
1310 t_out->maclen = 0;
1311 t_in->maclen = 0;
1312 switch (tag_mode) {
1313 case 0: /* Full tag */
1314 t_out->taglen = 16;
1315 t_in->taglen = 16;
1316 break;
1317 case 1: /* Partial tag */
1318 t_out->taglen = 8;
1319 t_in->taglen = 8;
1320 break;
1321 default:
1322 ret = 1;
1323 goto cleanup;
1324 }
1325 break;
1326
1327 case MBEDTLS_MODE_CHACHAPOLY:
1328 t_out->fixed_ivlen = 12;
1329 t_in->fixed_ivlen = 12;
1330 t_out->maclen = 0;
1331 t_in->maclen = 0;
1332 switch (tag_mode) {
1333 case 0: /* Full tag */
1334 t_out->taglen = 16;
1335 t_in->taglen = 16;
1336 break;
1337 case 1: /* Partial tag */
1338 t_out->taglen = 8;
1339 t_in->taglen = 8;
1340 break;
1341 default:
1342 ret = 1;
1343 goto cleanup;
1344 }
1345 break;
1346
1347 case MBEDTLS_MODE_STREAM:
1348 case MBEDTLS_MODE_CBC:
1349 t_out->fixed_ivlen = 0; /* redundant, must be 0 */
1350 t_in->fixed_ivlen = 0; /* redundant, must be 0 */
1351 t_out->taglen = 0;
1352 t_in->taglen = 0;
1353 switch (tag_mode) {
1354 case 0: /* Full tag */
1355 t_out->maclen = maclen;
1356 t_in->maclen = maclen;
1357 break;
1358 default:
1359 ret = 1;
1360 goto cleanup;
1361 }
1362 break;
1363 default:
1364 ret = 1;
1365 goto cleanup;
1366 break;
1367 }
1368
1369 /* Setup IV's */
1370
1371 memcpy(&t_in->iv_dec, iv_dec, sizeof(iv_dec));
1372 memcpy(&t_in->iv_enc, iv_enc, sizeof(iv_enc));
1373 memcpy(&t_out->iv_dec, iv_enc, sizeof(iv_enc));
1374 memcpy(&t_out->iv_enc, iv_dec, sizeof(iv_dec));
1375
1376#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
1377 /* Add CID */
1378 memcpy(&t_in->in_cid, cid0, cid0_len);
1379 memcpy(&t_in->out_cid, cid1, cid1_len);
Yanray Wanga8f445e2022-11-03 11:51:59 +08001380 t_in->in_cid_len = (uint8_t) cid0_len;
1381 t_in->out_cid_len = (uint8_t) cid1_len;
Yanray Wange6afd912022-10-27 12:11:18 +08001382 memcpy(&t_out->in_cid, cid1, cid1_len);
1383 memcpy(&t_out->out_cid, cid0, cid0_len);
Yanray Wanga8f445e2022-11-03 11:51:59 +08001384 t_out->in_cid_len = (uint8_t) cid1_len;
1385 t_out->out_cid_len = (uint8_t) cid0_len;
Yanray Wange6afd912022-10-27 12:11:18 +08001386#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
1387
1388#if defined(MBEDTLS_USE_PSA_CRYPTO)
1389 status = mbedtls_ssl_cipher_to_psa(cipher_type,
1390 t_in->taglen,
1391 &alg,
1392 &key_type,
1393 &key_bits);
1394
1395 if (status != PSA_SUCCESS) {
1396 ret = PSA_TO_MBEDTLS_ERR(status);
1397 goto cleanup;
1398 }
1399
1400 t_in->psa_alg = alg;
1401 t_out->psa_alg = alg;
1402
1403 if (alg != MBEDTLS_SSL_NULL_CIPHER) {
1404 psa_reset_key_attributes(&attributes);
1405 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
1406 psa_set_key_algorithm(&attributes, alg);
1407 psa_set_key_type(&attributes, key_type);
1408
1409 status = psa_import_key(&attributes,
1410 key0,
1411 PSA_BITS_TO_BYTES(key_bits),
1412 &t_in->psa_key_enc);
1413
1414 if (status != PSA_SUCCESS) {
1415 ret = PSA_TO_MBEDTLS_ERR(status);
1416 goto cleanup;
1417 }
1418
1419 status = psa_import_key(&attributes,
1420 key1,
1421 PSA_BITS_TO_BYTES(key_bits),
1422 &t_out->psa_key_enc);
1423
1424 if (status != PSA_SUCCESS) {
1425 ret = PSA_TO_MBEDTLS_ERR(status);
1426 goto cleanup;
1427 }
1428
1429 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
1430
1431 status = psa_import_key(&attributes,
1432 key1,
1433 PSA_BITS_TO_BYTES(key_bits),
1434 &t_in->psa_key_dec);
1435
1436 if (status != PSA_SUCCESS) {
1437 ret = PSA_TO_MBEDTLS_ERR(status);
1438 goto cleanup;
1439 }
1440
1441 status = psa_import_key(&attributes,
1442 key0,
1443 PSA_BITS_TO_BYTES(key_bits),
1444 &t_out->psa_key_dec);
1445
1446 if (status != PSA_SUCCESS) {
1447 ret = PSA_TO_MBEDTLS_ERR(status);
1448 goto cleanup;
1449 }
1450 }
1451#endif /* MBEDTLS_USE_PSA_CRYPTO */
1452
1453cleanup:
1454
1455 mbedtls_free(key0);
1456 mbedtls_free(key1);
1457
1458 mbedtls_free(md0);
1459 mbedtls_free(md1);
1460
1461 return ret;
1462}
1463
Yanray Wange6afd912022-10-27 12:11:18 +08001464int mbedtls_test_ssl_tls12_populate_session(mbedtls_ssl_session *session,
1465 int ticket_len,
1466 const char *crt_file)
1467{
1468#if defined(MBEDTLS_HAVE_TIME)
1469 session->start = mbedtls_time(NULL) - 42;
1470#endif
1471 session->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
1472 session->ciphersuite = 0xabcd;
1473 session->id_len = sizeof(session->id);
1474 memset(session->id, 66, session->id_len);
1475 memset(session->master, 17, sizeof(session->master));
1476
1477#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) && defined(MBEDTLS_FS_IO)
1478 if (crt_file != NULL && strlen(crt_file) != 0) {
1479 mbedtls_x509_crt tmp_crt;
1480 int ret;
1481
1482 mbedtls_x509_crt_init(&tmp_crt);
1483 ret = mbedtls_x509_crt_parse_file(&tmp_crt, crt_file);
1484 if (ret != 0) {
1485 return ret;
1486 }
1487
1488#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1489 /* Move temporary CRT. */
1490 session->peer_cert = mbedtls_calloc(1, sizeof(*session->peer_cert));
1491 if (session->peer_cert == NULL) {
1492 return -1;
1493 }
1494 *session->peer_cert = tmp_crt;
1495 memset(&tmp_crt, 0, sizeof(tmp_crt));
1496#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
1497 /* Calculate digest of temporary CRT. */
1498 session->peer_cert_digest =
1499 mbedtls_calloc(1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN);
1500 if (session->peer_cert_digest == NULL) {
1501 return -1;
1502 }
1503
1504#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02001505 psa_algorithm_t psa_alg = mbedtls_md_psa_alg_from_type(
Yanray Wange6afd912022-10-27 12:11:18 +08001506 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE);
1507 size_t hash_size = 0;
1508 psa_status_t status = psa_hash_compute(
1509 psa_alg, tmp_crt.raw.p,
1510 tmp_crt.raw.len,
1511 session->peer_cert_digest,
1512 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN,
1513 &hash_size);
1514 ret = PSA_TO_MBEDTLS_ERR(status);
1515#else
1516 ret = mbedtls_md(mbedtls_md_info_from_type(
1517 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE),
1518 tmp_crt.raw.p, tmp_crt.raw.len,
1519 session->peer_cert_digest);
1520#endif /* MBEDTLS_USE_PSA_CRYPTO */
1521 if (ret != 0) {
1522 return ret;
1523 }
1524 session->peer_cert_digest_type =
1525 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
1526 session->peer_cert_digest_len =
1527 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
1528#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
1529
1530 mbedtls_x509_crt_free(&tmp_crt);
1531 }
1532#else /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED && MBEDTLS_FS_IO */
1533 (void) crt_file;
1534#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED && MBEDTLS_FS_IO */
1535 session->verify_result = 0xdeadbeef;
1536
1537#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
1538 if (ticket_len != 0) {
1539 session->ticket = mbedtls_calloc(1, ticket_len);
1540 if (session->ticket == NULL) {
1541 return -1;
1542 }
1543 memset(session->ticket, 33, ticket_len);
1544 }
1545 session->ticket_len = ticket_len;
1546 session->ticket_lifetime = 86401;
1547#else
1548 (void) ticket_len;
1549#endif
1550
1551#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1552 session->mfl_code = 1;
1553#endif
1554#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1555 session->encrypt_then_mac = 1;
1556#endif
1557
1558 return 0;
1559}
1560
1561#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1562int mbedtls_test_ssl_tls13_populate_session(mbedtls_ssl_session *session,
1563 int ticket_len,
1564 int endpoint_type)
1565{
1566 ((void) ticket_len);
1567 session->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
1568 session->endpoint = endpoint_type == MBEDTLS_SSL_IS_CLIENT ?
1569 MBEDTLS_SSL_IS_CLIENT : MBEDTLS_SSL_IS_SERVER;
1570 session->ciphersuite = 0xabcd;
1571 session->ticket_age_add = 0x87654321;
1572 session->ticket_flags = 0x7;
1573
1574 session->resumption_key_len = 32;
1575 memset(session->resumption_key, 0x99, sizeof(session->resumption_key));
1576
1577#if defined(MBEDTLS_HAVE_TIME)
1578 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
1579 session->start = mbedtls_time(NULL) - 42;
1580 }
1581#endif
1582
1583#if defined(MBEDTLS_SSL_CLI_C)
1584 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
1585#if defined(MBEDTLS_HAVE_TIME)
1586 session->ticket_received = mbedtls_time(NULL) - 40;
1587#endif
1588 session->ticket_lifetime = 0xfedcba98;
1589
1590 session->ticket_len = ticket_len;
1591 if (ticket_len != 0) {
1592 session->ticket = mbedtls_calloc(1, ticket_len);
1593 if (session->ticket == NULL) {
1594 return -1;
1595 }
1596 memset(session->ticket, 33, ticket_len);
1597 }
1598 }
1599#endif /* MBEDTLS_SSL_CLI_C */
1600
1601 return 0;
1602}
1603#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1604
Yanray Wangb088bfc2023-03-16 12:15:49 +08001605int mbedtls_test_ssl_exchange_data(
1606 mbedtls_ssl_context *ssl_1,
1607 int msg_len_1, const int expected_fragments_1,
1608 mbedtls_ssl_context *ssl_2,
1609 int msg_len_2, const int expected_fragments_2)
Yanray Wange6afd912022-10-27 12:11:18 +08001610{
1611 unsigned char *msg_buf_1 = malloc(msg_len_1);
1612 unsigned char *msg_buf_2 = malloc(msg_len_2);
1613 unsigned char *in_buf_1 = malloc(msg_len_2);
1614 unsigned char *in_buf_2 = malloc(msg_len_1);
1615 int msg_type, ret = -1;
1616
1617 /* Perform this test with two message types. At first use a message
1618 * consisting of only 0x00 for the client and only 0xFF for the server.
1619 * At the second time use message with generated data */
1620 for (msg_type = 0; msg_type < 2; msg_type++) {
1621 int written_1 = 0;
1622 int written_2 = 0;
1623 int read_1 = 0;
1624 int read_2 = 0;
1625 int fragments_1 = 0;
1626 int fragments_2 = 0;
1627
1628 if (msg_type == 0) {
1629 memset(msg_buf_1, 0x00, msg_len_1);
1630 memset(msg_buf_2, 0xff, msg_len_2);
1631 } else {
1632 int i, j = 0;
1633 for (i = 0; i < msg_len_1; i++) {
1634 msg_buf_1[i] = j++ & 0xFF;
1635 }
1636 for (i = 0; i < msg_len_2; i++) {
1637 msg_buf_2[i] = (j -= 5) & 0xFF;
1638 }
1639 }
1640
1641 while (read_1 < msg_len_2 || read_2 < msg_len_1) {
1642 /* ssl_1 sending */
1643 if (msg_len_1 > written_1) {
1644 ret = mbedtls_ssl_write_fragment(ssl_1, msg_buf_1,
1645 msg_len_1, &written_1,
1646 expected_fragments_1);
1647 if (expected_fragments_1 == 0) {
1648 /* This error is expected when the message is too large and
1649 * cannot be fragmented */
1650 TEST_ASSERT(ret == MBEDTLS_ERR_SSL_BAD_INPUT_DATA);
1651 msg_len_1 = 0;
1652 } else {
1653 TEST_ASSERT(ret == 0);
1654 }
1655 }
1656
1657 /* ssl_2 sending */
1658 if (msg_len_2 > written_2) {
1659 ret = mbedtls_ssl_write_fragment(ssl_2, msg_buf_2,
1660 msg_len_2, &written_2,
1661 expected_fragments_2);
1662 if (expected_fragments_2 == 0) {
1663 /* This error is expected when the message is too large and
1664 * cannot be fragmented */
1665 TEST_ASSERT(ret == MBEDTLS_ERR_SSL_BAD_INPUT_DATA);
1666 msg_len_2 = 0;
1667 } else {
1668 TEST_ASSERT(ret == 0);
1669 }
1670 }
1671
1672 /* ssl_1 reading */
1673 if (read_1 < msg_len_2) {
1674 ret = mbedtls_ssl_read_fragment(ssl_1, in_buf_1,
1675 msg_len_2, &read_1,
1676 &fragments_2,
1677 expected_fragments_2);
1678 TEST_ASSERT(ret == 0);
1679 }
1680
1681 /* ssl_2 reading */
1682 if (read_2 < msg_len_1) {
1683 ret = mbedtls_ssl_read_fragment(ssl_2, in_buf_2,
1684 msg_len_1, &read_2,
1685 &fragments_1,
1686 expected_fragments_1);
1687 TEST_ASSERT(ret == 0);
1688 }
1689 }
1690
1691 ret = -1;
1692 TEST_ASSERT(0 == memcmp(msg_buf_1, in_buf_2, msg_len_1));
1693 TEST_ASSERT(0 == memcmp(msg_buf_2, in_buf_1, msg_len_2));
1694 TEST_ASSERT(fragments_1 == expected_fragments_1);
1695 TEST_ASSERT(fragments_2 == expected_fragments_2);
1696 }
1697
1698 ret = 0;
1699
1700exit:
1701 free(msg_buf_1);
1702 free(in_buf_1);
1703 free(msg_buf_2);
1704 free(in_buf_2);
1705
1706 return ret;
1707}
1708
1709/*
1710 * Perform data exchanging between \p ssl_1 and \p ssl_2. Both of endpoints
1711 * must be initialized and connected beforehand.
1712 *
1713 * \retval 0 on success, otherwise error code.
1714 */
Yanray Wangead70c82023-03-16 12:04:49 +08001715#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) && \
1716 (defined(MBEDTLS_SSL_RENEGOTIATION) || \
1717 defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH))
1718static int exchange_data(mbedtls_ssl_context *ssl_1,
1719 mbedtls_ssl_context *ssl_2)
Yanray Wange6afd912022-10-27 12:11:18 +08001720{
Yanray Wangb088bfc2023-03-16 12:15:49 +08001721 return mbedtls_test_ssl_exchange_data(ssl_1, 256, 1,
1722 ssl_2, 256, 1);
Yanray Wange6afd912022-10-27 12:11:18 +08001723}
Yanray Wangead70c82023-03-16 12:04:49 +08001724#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED &&
1725 (MBEDTLS_SSL_RENEGOTIATION ||
1726 MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) */
Yanray Wange6afd912022-10-27 12:11:18 +08001727
1728#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
1729static int check_ssl_version(
1730 mbedtls_ssl_protocol_version expected_negotiated_version,
1731 const mbedtls_ssl_context *ssl)
1732{
1733 const char *version_string = mbedtls_ssl_get_version(ssl);
1734 mbedtls_ssl_protocol_version version_number =
1735 mbedtls_ssl_get_version_number(ssl);
1736
1737 TEST_EQUAL(ssl->tls_version, expected_negotiated_version);
1738
1739 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1740 TEST_EQUAL(version_string[0], 'D');
1741 ++version_string;
1742 }
1743
1744 switch (expected_negotiated_version) {
1745 case MBEDTLS_SSL_VERSION_TLS1_2:
1746 TEST_EQUAL(version_number, MBEDTLS_SSL_VERSION_TLS1_2);
1747 TEST_ASSERT(strcmp(version_string, "TLSv1.2") == 0);
1748 break;
1749
1750 case MBEDTLS_SSL_VERSION_TLS1_3:
1751 TEST_EQUAL(version_number, MBEDTLS_SSL_VERSION_TLS1_3);
1752 TEST_ASSERT(strcmp(version_string, "TLSv1.3") == 0);
1753 break;
1754
1755 default:
1756 TEST_ASSERT(
1757 !"Version check not implemented for this protocol version");
1758 }
1759
1760 return 1;
1761
1762exit:
1763 return 0;
1764}
1765#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
1766
Yanray Wange6afd912022-10-27 12:11:18 +08001767#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
1768void mbedtls_test_ssl_perform_handshake(
1769 mbedtls_test_handshake_test_options *options)
1770{
1771 /* forced_ciphersuite needs to last until the end of the handshake */
1772 int forced_ciphersuite[2];
1773 enum { BUFFSIZE = 17000 };
1774 mbedtls_test_ssl_endpoint client, server;
1775#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
1776 const char *psk_identity = "foo";
1777#endif
1778#if defined(MBEDTLS_TIMING_C)
1779 mbedtls_timing_delay_context timer_client, timer_server;
1780#endif
1781#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1782 unsigned char *context_buf = NULL;
1783 size_t context_buf_len;
1784#endif
1785#if defined(MBEDTLS_SSL_RENEGOTIATION)
1786 int ret = -1;
1787#endif
1788 int expected_handshake_result = options->expected_handshake_result;
1789
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +01001790 MD_OR_USE_PSA_INIT();
Yanray Wange6afd912022-10-27 12:11:18 +08001791 mbedtls_platform_zeroize(&client, sizeof(client));
1792 mbedtls_platform_zeroize(&server, sizeof(server));
1793 mbedtls_test_ssl_message_queue server_queue, client_queue;
1794 mbedtls_test_message_socket_context server_context, client_context;
1795 mbedtls_test_message_socket_init(&server_context);
1796 mbedtls_test_message_socket_init(&client_context);
1797
1798 /* Client side */
1799 if (options->dtls != 0) {
1800 TEST_ASSERT(mbedtls_test_ssl_endpoint_init(&client,
1801 MBEDTLS_SSL_IS_CLIENT,
1802 options, &client_context,
1803 &client_queue,
1804 &server_queue, NULL) == 0);
1805#if defined(MBEDTLS_TIMING_C)
1806 mbedtls_ssl_set_timer_cb(&client.ssl, &timer_client,
1807 mbedtls_timing_set_delay,
1808 mbedtls_timing_get_delay);
1809#endif
1810 } else {
1811 TEST_ASSERT(mbedtls_test_ssl_endpoint_init(&client,
1812 MBEDTLS_SSL_IS_CLIENT,
1813 options, NULL, NULL,
1814 NULL, NULL) == 0);
1815 }
1816
Yanray Wange6afd912022-10-27 12:11:18 +08001817 if (strlen(options->cipher) > 0) {
1818 set_ciphersuite(&client.conf, options->cipher, forced_ciphersuite);
1819 }
1820
1821#if defined(MBEDTLS_DEBUG_C)
1822 if (options->cli_log_fun) {
1823 mbedtls_debug_set_threshold(4);
1824 mbedtls_ssl_conf_dbg(&client.conf, options->cli_log_fun,
1825 options->cli_log_obj);
1826 }
1827#endif
1828
1829 /* Server side */
1830 if (options->dtls != 0) {
1831 TEST_ASSERT(mbedtls_test_ssl_endpoint_init(&server,
1832 MBEDTLS_SSL_IS_SERVER,
1833 options, &server_context,
1834 &server_queue,
1835 &client_queue, NULL) == 0);
1836#if defined(MBEDTLS_TIMING_C)
1837 mbedtls_ssl_set_timer_cb(&server.ssl, &timer_server,
1838 mbedtls_timing_set_delay,
1839 mbedtls_timing_get_delay);
1840#endif
1841 } else {
1842 TEST_ASSERT(mbedtls_test_ssl_endpoint_init(&server,
1843 MBEDTLS_SSL_IS_SERVER,
1844 options, NULL, NULL, NULL,
1845 NULL) == 0);
1846 }
1847
1848 mbedtls_ssl_conf_authmode(&server.conf, options->srv_auth_mode);
1849
Yanray Wange6afd912022-10-27 12:11:18 +08001850#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1851 TEST_ASSERT(mbedtls_ssl_conf_max_frag_len(&(server.conf),
1852 (unsigned char) options->mfl)
1853 == 0);
1854 TEST_ASSERT(mbedtls_ssl_conf_max_frag_len(&(client.conf),
1855 (unsigned char) options->mfl)
1856 == 0);
1857#else
1858 TEST_ASSERT(MBEDTLS_SSL_MAX_FRAG_LEN_NONE == options->mfl);
1859#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
1860
1861#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
1862 if (options->psk_str != NULL && options->psk_str->len > 0) {
1863 TEST_ASSERT(mbedtls_ssl_conf_psk(
1864 &client.conf, options->psk_str->x,
1865 options->psk_str->len,
1866 (const unsigned char *) psk_identity,
1867 strlen(psk_identity)) == 0);
1868
1869 TEST_ASSERT(mbedtls_ssl_conf_psk(
1870 &server.conf, options->psk_str->x,
1871 options->psk_str->len,
1872 (const unsigned char *) psk_identity,
1873 strlen(psk_identity)) == 0);
1874#if defined(MBEDTLS_SSL_SRV_C)
1875 mbedtls_ssl_conf_psk_cb(&server.conf, psk_dummy_callback, NULL);
1876#endif
1877 }
1878#endif
1879#if defined(MBEDTLS_SSL_RENEGOTIATION)
1880 if (options->renegotiate) {
1881 mbedtls_ssl_conf_renegotiation(&(server.conf),
1882 MBEDTLS_SSL_RENEGOTIATION_ENABLED);
1883 mbedtls_ssl_conf_renegotiation(&(client.conf),
1884 MBEDTLS_SSL_RENEGOTIATION_ENABLED);
1885
1886 mbedtls_ssl_conf_legacy_renegotiation(&(server.conf),
1887 options->legacy_renegotiation);
1888 mbedtls_ssl_conf_legacy_renegotiation(&(client.conf),
1889 options->legacy_renegotiation);
1890 }
1891#endif /* MBEDTLS_SSL_RENEGOTIATION */
1892
1893#if defined(MBEDTLS_DEBUG_C)
1894 if (options->srv_log_fun) {
1895 mbedtls_debug_set_threshold(4);
1896 mbedtls_ssl_conf_dbg(&server.conf, options->srv_log_fun,
1897 options->srv_log_obj);
1898 }
1899#endif
1900
1901 TEST_ASSERT(mbedtls_test_mock_socket_connect(&(client.socket),
1902 &(server.socket),
1903 BUFFSIZE) == 0);
1904
1905#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1906 if (options->resize_buffers != 0) {
1907 /* Ensure that the buffer sizes are appropriate before resizes */
1908 TEST_ASSERT(client.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN);
1909 TEST_ASSERT(client.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN);
1910 TEST_ASSERT(server.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN);
1911 TEST_ASSERT(server.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN);
1912 }
1913#endif
1914
1915 if (options->expected_negotiated_version == MBEDTLS_SSL_VERSION_UNKNOWN) {
1916 expected_handshake_result = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
1917 }
1918
1919 TEST_ASSERT(mbedtls_test_move_handshake_to_state(&(client.ssl),
1920 &(server.ssl),
1921 MBEDTLS_SSL_HANDSHAKE_OVER)
1922 == expected_handshake_result);
1923
1924 if (expected_handshake_result != 0) {
1925 /* Connection will have failed by this point, skip to cleanup */
1926 goto exit;
1927 }
1928
1929 TEST_ASSERT(mbedtls_ssl_is_handshake_over(&client.ssl) == 1);
1930
1931 /* Make sure server state is moved to HANDSHAKE_OVER also. */
1932 TEST_EQUAL(mbedtls_test_move_handshake_to_state(&(server.ssl),
1933 &(client.ssl),
1934 MBEDTLS_SSL_HANDSHAKE_OVER),
1935 0);
1936
1937 TEST_ASSERT(mbedtls_ssl_is_handshake_over(&server.ssl) == 1);
1938 /* Check that both sides have negotiated the expected version. */
1939 mbedtls_test_set_step(0);
1940 if (!check_ssl_version(options->expected_negotiated_version,
1941 &client.ssl)) {
1942 goto exit;
1943 }
1944
1945 mbedtls_test_set_step(1);
1946 if (!check_ssl_version(options->expected_negotiated_version,
1947 &server.ssl)) {
1948 goto exit;
1949 }
1950
1951 if (options->expected_ciphersuite != 0) {
1952 TEST_EQUAL(server.ssl.session->ciphersuite,
1953 options->expected_ciphersuite);
1954 }
1955
1956#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1957 if (options->resize_buffers != 0) {
1958 /* A server, when using DTLS, might delay a buffer resize to happen
1959 * after it receives a message, so we force it. */
1960 TEST_ASSERT(exchange_data(&(client.ssl), &(server.ssl)) == 0);
1961
1962 TEST_ASSERT(client.ssl.out_buf_len ==
1963 mbedtls_ssl_get_output_buflen(&client.ssl));
1964 TEST_ASSERT(client.ssl.in_buf_len ==
1965 mbedtls_ssl_get_input_buflen(&client.ssl));
1966 TEST_ASSERT(server.ssl.out_buf_len ==
1967 mbedtls_ssl_get_output_buflen(&server.ssl));
1968 TEST_ASSERT(server.ssl.in_buf_len ==
1969 mbedtls_ssl_get_input_buflen(&server.ssl));
1970 }
1971#endif
1972
1973 if (options->cli_msg_len != 0 || options->srv_msg_len != 0) {
1974 /* Start data exchanging test */
Yanray Wangb088bfc2023-03-16 12:15:49 +08001975 TEST_ASSERT(mbedtls_test_ssl_exchange_data(
1976 &(client.ssl), options->cli_msg_len,
1977 options->expected_cli_fragments,
1978 &(server.ssl), options->srv_msg_len,
1979 options->expected_srv_fragments)
Yanray Wange6afd912022-10-27 12:11:18 +08001980 == 0);
1981 }
1982#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1983 if (options->serialize == 1) {
1984 TEST_ASSERT(options->dtls == 1);
1985
1986 TEST_ASSERT(mbedtls_ssl_context_save(&(server.ssl), NULL,
1987 0, &context_buf_len)
1988 == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL);
1989
1990 context_buf = mbedtls_calloc(1, context_buf_len);
1991 TEST_ASSERT(context_buf != NULL);
1992
1993 TEST_ASSERT(mbedtls_ssl_context_save(&(server.ssl), context_buf,
1994 context_buf_len,
1995 &context_buf_len)
1996 == 0);
1997
1998 mbedtls_ssl_free(&(server.ssl));
1999 mbedtls_ssl_init(&(server.ssl));
2000
2001 TEST_ASSERT(mbedtls_ssl_setup(&(server.ssl), &(server.conf)) == 0);
2002
2003 mbedtls_ssl_set_bio(&(server.ssl), &server_context,
2004 mbedtls_test_mock_tcp_send_msg,
2005 mbedtls_test_mock_tcp_recv_msg,
2006 NULL);
2007
2008 mbedtls_ssl_set_user_data_p(&server.ssl, &server);
2009
2010#if defined(MBEDTLS_TIMING_C)
2011 mbedtls_ssl_set_timer_cb(&server.ssl, &timer_server,
2012 mbedtls_timing_set_delay,
2013 mbedtls_timing_get_delay);
2014#endif
2015#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2016 if (options->resize_buffers != 0) {
2017 /* Ensure that the buffer sizes are appropriate before resizes */
2018 TEST_ASSERT(server.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN);
2019 TEST_ASSERT(server.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN);
2020 }
2021#endif
2022 TEST_ASSERT(mbedtls_ssl_context_load(&(server.ssl), context_buf,
2023 context_buf_len) == 0);
2024
2025#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2026 /* Validate buffer sizes after context deserialization */
2027 if (options->resize_buffers != 0) {
2028 TEST_ASSERT(server.ssl.out_buf_len ==
2029 mbedtls_ssl_get_output_buflen(&server.ssl));
2030 TEST_ASSERT(server.ssl.in_buf_len ==
2031 mbedtls_ssl_get_input_buflen(&server.ssl));
2032 }
2033#endif
2034 /* Retest writing/reading */
2035 if (options->cli_msg_len != 0 || options->srv_msg_len != 0) {
Yanray Wangb088bfc2023-03-16 12:15:49 +08002036 TEST_ASSERT(mbedtls_test_ssl_exchange_data(
2037 &(client.ssl), options->cli_msg_len,
Yanray Wange6afd912022-10-27 12:11:18 +08002038 options->expected_cli_fragments,
Yanray Wangb088bfc2023-03-16 12:15:49 +08002039 &(server.ssl), options->srv_msg_len,
Yanray Wange6afd912022-10-27 12:11:18 +08002040 options->expected_srv_fragments)
2041 == 0);
2042 }
2043 }
2044#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
2045
2046#if defined(MBEDTLS_SSL_RENEGOTIATION)
2047 if (options->renegotiate) {
2048 /* Start test with renegotiation */
2049 TEST_ASSERT(server.ssl.renego_status ==
2050 MBEDTLS_SSL_INITIAL_HANDSHAKE);
2051 TEST_ASSERT(client.ssl.renego_status ==
2052 MBEDTLS_SSL_INITIAL_HANDSHAKE);
2053
2054 /* After calling this function for the server, it only sends a handshake
2055 * request. All renegotiation should happen during data exchanging */
2056 TEST_ASSERT(mbedtls_ssl_renegotiate(&(server.ssl)) == 0);
2057 TEST_ASSERT(server.ssl.renego_status ==
2058 MBEDTLS_SSL_RENEGOTIATION_PENDING);
2059 TEST_ASSERT(client.ssl.renego_status ==
2060 MBEDTLS_SSL_INITIAL_HANDSHAKE);
2061
2062 TEST_ASSERT(exchange_data(&(client.ssl), &(server.ssl)) == 0);
2063 TEST_ASSERT(server.ssl.renego_status ==
2064 MBEDTLS_SSL_RENEGOTIATION_DONE);
2065 TEST_ASSERT(client.ssl.renego_status ==
2066 MBEDTLS_SSL_RENEGOTIATION_DONE);
2067
2068 /* After calling mbedtls_ssl_renegotiate for the client,
2069 * all renegotiation should happen inside this function.
2070 * However in this test, we cannot perform simultaneous communication
2071 * between client and server so this function will return waiting error
2072 * on the socket. All rest of renegotiation should happen
2073 * during data exchanging */
2074 ret = mbedtls_ssl_renegotiate(&(client.ssl));
2075#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2076 if (options->resize_buffers != 0) {
2077 /* Ensure that the buffer sizes are appropriate before resizes */
2078 TEST_ASSERT(client.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN);
2079 TEST_ASSERT(client.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN);
2080 }
2081#endif
2082 TEST_ASSERT(ret == 0 ||
2083 ret == MBEDTLS_ERR_SSL_WANT_READ ||
2084 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
2085 TEST_ASSERT(server.ssl.renego_status ==
2086 MBEDTLS_SSL_RENEGOTIATION_DONE);
2087 TEST_ASSERT(client.ssl.renego_status ==
2088 MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS);
2089
2090 TEST_ASSERT(exchange_data(&(client.ssl), &(server.ssl)) == 0);
2091 TEST_ASSERT(server.ssl.renego_status ==
2092 MBEDTLS_SSL_RENEGOTIATION_DONE);
2093 TEST_ASSERT(client.ssl.renego_status ==
2094 MBEDTLS_SSL_RENEGOTIATION_DONE);
2095#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2096 /* Validate buffer sizes after renegotiation */
2097 if (options->resize_buffers != 0) {
2098 TEST_ASSERT(client.ssl.out_buf_len ==
2099 mbedtls_ssl_get_output_buflen(&client.ssl));
2100 TEST_ASSERT(client.ssl.in_buf_len ==
2101 mbedtls_ssl_get_input_buflen(&client.ssl));
2102 TEST_ASSERT(server.ssl.out_buf_len ==
2103 mbedtls_ssl_get_output_buflen(&server.ssl));
2104 TEST_ASSERT(server.ssl.in_buf_len ==
2105 mbedtls_ssl_get_input_buflen(&server.ssl));
2106 }
2107#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
2108 }
2109#endif /* MBEDTLS_SSL_RENEGOTIATION */
2110
2111 TEST_ASSERT(mbedtls_ssl_conf_get_user_data_p(&client.conf) == &client);
2112 TEST_ASSERT(mbedtls_ssl_get_user_data_p(&client.ssl) == &client);
2113 TEST_ASSERT(mbedtls_ssl_conf_get_user_data_p(&server.conf) == &server);
2114 TEST_ASSERT(mbedtls_ssl_get_user_data_p(&server.ssl) == &server);
2115
2116exit:
2117 mbedtls_test_ssl_endpoint_free(&client,
Yanray Wangaf727a22023-03-13 19:22:36 +08002118 options->dtls != 0 ? &client_context : NULL);
Yanray Wange6afd912022-10-27 12:11:18 +08002119 mbedtls_test_ssl_endpoint_free(&server,
Yanray Wangaf727a22023-03-13 19:22:36 +08002120 options->dtls != 0 ? &server_context : NULL);
Yanray Wange6afd912022-10-27 12:11:18 +08002121#if defined(MBEDTLS_DEBUG_C)
2122 if (options->cli_log_fun || options->srv_log_fun) {
2123 mbedtls_debug_set_threshold(0);
2124 }
2125#endif
2126#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
2127 if (context_buf != NULL) {
2128 mbedtls_free(context_buf);
2129 }
2130#endif
Manuel Pégourié-Gonnard23fc4372023-03-17 13:34:11 +01002131 MD_OR_USE_PSA_DONE();
Yanray Wange6afd912022-10-27 12:11:18 +08002132}
2133#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
2134
2135#if defined(MBEDTLS_TEST_HOOKS)
Yanray Wangf56181a2023-03-16 12:21:33 +08002136int mbedtls_test_tweak_tls13_certificate_msg_vector_len(
Yanray Wange6afd912022-10-27 12:11:18 +08002137 unsigned char *buf, unsigned char **end, int tweak,
2138 int *expected_result, mbedtls_ssl_chk_buf_ptr_args *args)
2139{
2140/*
2141 * The definition of the tweaks assume that the certificate list contains only
2142 * one certificate.
2143 */
2144
2145/*
2146 * struct {
2147 * opaque cert_data<1..2^24-1>;
2148 * Extension extensions<0..2^16-1>;
2149 * } CertificateEntry;
2150 *
2151 * struct {
2152 * opaque certificate_request_context<0..2^8-1>;
2153 * CertificateEntry certificate_list<0..2^24-1>;
2154 * } Certificate;
2155 */
2156 unsigned char *p_certificate_request_context_len = buf;
2157 size_t certificate_request_context_len = buf[0];
2158
2159 unsigned char *p_certificate_list_len =
2160 buf + 1 + certificate_request_context_len;
2161 unsigned char *certificate_list = p_certificate_list_len + 3;
2162 size_t certificate_list_len =
2163 MBEDTLS_GET_UINT24_BE(p_certificate_list_len, 0);
2164
2165 unsigned char *p_cert_data_len = certificate_list;
2166 unsigned char *cert_data = p_cert_data_len + 3;
2167 size_t cert_data_len = MBEDTLS_GET_UINT24_BE(p_cert_data_len, 0);
2168
2169 unsigned char *p_extensions_len = cert_data + cert_data_len;
2170 unsigned char *extensions = p_extensions_len + 2;
2171 size_t extensions_len = MBEDTLS_GET_UINT16_BE(p_extensions_len, 0);
2172
2173 *expected_result = MBEDTLS_ERR_SSL_DECODE_ERROR;
2174
2175 switch (tweak) {
2176 case 1:
2177 /* Failure when checking if the certificate request context length
2178 * and certificate list length can be read
2179 */
2180 *end = buf + 3;
2181 set_chk_buf_ptr_args(args, buf, *end, 4);
2182 break;
2183
2184 case 2:
2185 /* Invalid certificate request context length.
2186 */
2187 *p_certificate_request_context_len =
Yanray Wanga8f445e2022-11-03 11:51:59 +08002188 (unsigned char) certificate_request_context_len + 1;
Yanray Wange6afd912022-10-27 12:11:18 +08002189 reset_chk_buf_ptr_args(args);
2190 break;
2191
2192 case 3:
2193 /* Failure when checking if certificate_list data can be read. */
2194 MBEDTLS_PUT_UINT24_BE(certificate_list_len + 1,
2195 p_certificate_list_len, 0);
2196 set_chk_buf_ptr_args(args, certificate_list, *end,
2197 certificate_list_len + 1);
2198 break;
2199
2200 case 4:
2201 /* Failure when checking if the cert_data length can be read. */
2202 MBEDTLS_PUT_UINT24_BE(2, p_certificate_list_len, 0);
2203 set_chk_buf_ptr_args(args, p_cert_data_len, certificate_list + 2, 3);
2204 break;
2205
2206 case 5:
2207 /* Failure when checking if cert_data data can be read. */
2208 MBEDTLS_PUT_UINT24_BE(certificate_list_len - 3 + 1,
2209 p_cert_data_len, 0);
2210 set_chk_buf_ptr_args(args, cert_data,
2211 certificate_list + certificate_list_len,
2212 certificate_list_len - 3 + 1);
2213 break;
2214
2215 case 6:
2216 /* Failure when checking if the extensions length can be read. */
2217 MBEDTLS_PUT_UINT24_BE(certificate_list_len - extensions_len - 1,
2218 p_certificate_list_len, 0);
2219 set_chk_buf_ptr_args(
2220 args, p_extensions_len,
2221 certificate_list + certificate_list_len - extensions_len - 1, 2);
2222 break;
2223
2224 case 7:
2225 /* Failure when checking if extensions data can be read. */
2226 MBEDTLS_PUT_UINT16_BE(extensions_len + 1, p_extensions_len, 0);
2227
2228 set_chk_buf_ptr_args(
2229 args, extensions,
2230 certificate_list + certificate_list_len, extensions_len + 1);
2231 break;
2232
2233 default:
2234 return -1;
2235 }
2236
2237 return 0;
2238}
2239#endif /* MBEDTLS_TEST_HOOKS */
Yanray Wang4d07d1c2022-10-27 15:28:16 +08002240#endif /* MBEDTLS_SSL_TLS_C */