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