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