blob: 6fbbe35ad968f3947d176caaa7a2a65482a127b0 [file] [log] [blame]
Yanray Wang5fce1452022-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>
Yanray Wangbd56b032023-03-14 14:36:48 +080024
Yanray Wang4323e452023-03-14 16:52:06 +080025#if defined(MBEDTLS_SSL_TLS_C)
26
Yanray Wangbd56b032023-03-14 14:36:48 +080027void mbedtls_test_ssl_log_analyzer(void *ctx, int level,
28 const char *file, int line,
29 const char *str)
30{
31 mbedtls_test_ssl_log_pattern *p = (mbedtls_test_ssl_log_pattern *) ctx;
32
33 (void) level;
34 (void) line;
35 (void) file;
36
37 if (NULL != p &&
38 NULL != p->pattern &&
39 NULL != strstr(str, p->pattern)) {
40 p->counter++;
41 }
42}
43
44void mbedtls_test_init_handshake_options(
45 mbedtls_test_handshake_test_options *opts)
46{
47 opts->cipher = "";
48 opts->client_min_version = TEST_SSL_MINOR_VERSION_NONE;
49 opts->client_max_version = TEST_SSL_MINOR_VERSION_NONE;
50 opts->server_min_version = TEST_SSL_MINOR_VERSION_NONE;
51 opts->server_max_version = TEST_SSL_MINOR_VERSION_NONE;
52 opts->expected_negotiated_version = MBEDTLS_SSL_MINOR_VERSION_3;
53 opts->pk_alg = MBEDTLS_PK_RSA;
54 opts->psk_str = NULL;
55 opts->dtls = 0;
56 opts->srv_auth_mode = MBEDTLS_SSL_VERIFY_NONE;
57 opts->serialize = 0;
58 opts->mfl = MBEDTLS_SSL_MAX_FRAG_LEN_NONE;
59 opts->cli_msg_len = 100;
60 opts->srv_msg_len = 100;
61 opts->expected_cli_fragments = 1;
62 opts->expected_srv_fragments = 1;
63 opts->renegotiate = 0;
64 opts->legacy_renegotiation = MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION;
65 opts->srv_log_obj = NULL;
66 opts->srv_log_obj = NULL;
67 opts->srv_log_fun = NULL;
68 opts->cli_log_fun = NULL;
69 opts->resize_buffers = 1;
70}
71
Yanray Wangbd56b032023-03-14 14:36:48 +080072void mbedtls_test_ssl_buffer_init(mbedtls_test_ssl_buffer *buf)
73{
74 memset(buf, 0, sizeof(*buf));
75}
76
Yanray Wangbd56b032023-03-14 14:36:48 +080077int mbedtls_test_ssl_buffer_setup(mbedtls_test_ssl_buffer *buf,
78 size_t capacity)
79{
80 buf->buffer = (unsigned char *) mbedtls_calloc(capacity,
81 sizeof(unsigned char));
82 if (NULL == buf->buffer) {
83 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
84 }
85 buf->capacity = capacity;
86
87 return 0;
88}
89
90void mbedtls_test_ssl_buffer_free(mbedtls_test_ssl_buffer *buf)
91{
92 if (buf->buffer != NULL) {
93 mbedtls_free(buf->buffer);
94 }
95
96 memset(buf, 0, sizeof(*buf));
97}
98
Yanray Wangbd56b032023-03-14 14:36:48 +080099int mbedtls_test_ssl_buffer_put(mbedtls_test_ssl_buffer *buf,
100 const unsigned char *input, size_t input_len)
101{
102 size_t overflow = 0;
103
104 if ((buf == NULL) || (buf->buffer == NULL)) {
105 return -1;
106 }
107
108 /* Reduce input_len to a number that fits in the buffer. */
109 if ((buf->content_length + input_len) > buf->capacity) {
110 input_len = buf->capacity - buf->content_length;
111 }
112
113 if (input == NULL) {
114 return (input_len == 0) ? 0 : -1;
115 }
116
117 /* Check if the buffer has not come full circle and free space is not in
118 * the middle */
119 if (buf->start + buf->content_length < buf->capacity) {
120
121 /* Calculate the number of bytes that need to be placed at lower memory
122 * address */
123 if (buf->start + buf->content_length + input_len
124 > buf->capacity) {
125 overflow = (buf->start + buf->content_length + input_len)
126 % buf->capacity;
127 }
128
129 memcpy(buf->buffer + buf->start + buf->content_length, input,
130 input_len - overflow);
131 memcpy(buf->buffer, input + input_len - overflow, overflow);
132
133 } else {
134 /* The buffer has come full circle and free space is in the middle */
135 memcpy(buf->buffer + buf->start + buf->content_length - buf->capacity,
136 input, input_len);
137 }
138
139 buf->content_length += input_len;
Yanray Wangd2696f22022-11-03 11:51:59 +0800140 return (input_len > INT_MAX) ? INT_MAX : (int) input_len;
Yanray Wangbd56b032023-03-14 14:36:48 +0800141}
142
Yanray Wangbd56b032023-03-14 14:36:48 +0800143int mbedtls_test_ssl_buffer_get(mbedtls_test_ssl_buffer *buf,
144 unsigned char *output, size_t output_len)
145{
146 size_t overflow = 0;
147
148 if ((buf == NULL) || (buf->buffer == NULL)) {
149 return -1;
150 }
151
152 if (output == NULL && output_len == 0) {
153 return 0;
154 }
155
156 if (buf->content_length < output_len) {
157 output_len = buf->content_length;
158 }
159
160 /* Calculate the number of bytes that need to be drawn from lower memory
161 * address */
162 if (buf->start + output_len > buf->capacity) {
163 overflow = (buf->start + output_len) % buf->capacity;
164 }
165
166 if (output != NULL) {
167 memcpy(output, buf->buffer + buf->start, output_len - overflow);
168 memcpy(output + output_len - overflow, buf->buffer, overflow);
169 }
170
171 buf->content_length -= output_len;
172 buf->start = (buf->start + output_len) % buf->capacity;
173
Yanray Wangd2696f22022-11-03 11:51:59 +0800174 return (output_len > INT_MAX) ? INT_MAX : (int) output_len;
Yanray Wangbd56b032023-03-14 14:36:48 +0800175}
176
Yanray Wangbd56b032023-03-14 14:36:48 +0800177int mbedtls_test_ssl_message_queue_setup(
178 mbedtls_test_ssl_message_queue *queue, size_t capacity)
179{
180 queue->messages = (size_t *) mbedtls_calloc(capacity, sizeof(size_t));
181 if (NULL == queue->messages) {
182 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
183 }
184
Yanray Wangd2696f22022-11-03 11:51:59 +0800185 queue->capacity = (capacity > INT_MAX) ? INT_MAX : (int) capacity;
Yanray Wangbd56b032023-03-14 14:36:48 +0800186 queue->pos = 0;
187 queue->num = 0;
188
189 return 0;
190}
191
192void mbedtls_test_ssl_message_queue_free(
193 mbedtls_test_ssl_message_queue *queue)
194{
195 if (queue == NULL) {
196 return;
197 }
198
199 if (queue->messages != NULL) {
200 mbedtls_free(queue->messages);
201 }
202
203 memset(queue, 0, sizeof(*queue));
204}
205
Yanray Wangbd56b032023-03-14 14:36:48 +0800206int mbedtls_test_ssl_message_queue_push_info(
207 mbedtls_test_ssl_message_queue *queue, size_t len)
208{
209 int place;
210 if (queue == NULL) {
211 return MBEDTLS_TEST_ERROR_ARG_NULL;
212 }
213
214 if (queue->num >= queue->capacity) {
215 return MBEDTLS_ERR_SSL_WANT_WRITE;
216 }
217
218 place = (queue->pos + queue->num) % queue->capacity;
219 queue->messages[place] = len;
220 queue->num++;
Yanray Wangd2696f22022-11-03 11:51:59 +0800221 return (len > INT_MAX) ? INT_MAX : (int) len;
Yanray Wangbd56b032023-03-14 14:36:48 +0800222}
223
Yanray Wangbd56b032023-03-14 14:36:48 +0800224int mbedtls_test_ssl_message_queue_pop_info(
225 mbedtls_test_ssl_message_queue *queue, size_t buf_len)
226{
227 size_t message_length;
228 if (queue == NULL) {
229 return MBEDTLS_TEST_ERROR_ARG_NULL;
230 }
231 if (queue->num == 0) {
232 return MBEDTLS_ERR_SSL_WANT_READ;
233 }
234
235 message_length = queue->messages[queue->pos];
236 queue->messages[queue->pos] = 0;
237 queue->num--;
238 queue->pos++;
239 queue->pos %= queue->capacity;
240 if (queue->pos < 0) {
241 queue->pos += queue->capacity;
242 }
243
Yanray Wangd2696f22022-11-03 11:51:59 +0800244 return (message_length > INT_MAX && buf_len > INT_MAX) ? INT_MAX :
245 (message_length > buf_len) ? (int) buf_len : (int) message_length;
Yanray Wangbd56b032023-03-14 14:36:48 +0800246}
247
248/*
249 * Take a peek on the info about the next message length from the queue.
250 * This will be the oldest inserted message length(fifo).
251 *
252 * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null.
253 * \retval MBEDTLS_ERR_SSL_WANT_READ, if the queue is empty.
254 * \retval 0, if the peek was successful.
255 * \retval MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED, if the given buffer length is
256 * too small to fit the message. In this case the \p msg_len will be
257 * set to the full message length so that the
258 * caller knows what portion of the message can be dropped.
259 */
Yanray Wangc4abee22023-03-16 14:57:54 +0800260static int test_ssl_message_queue_peek_info(
261 mbedtls_test_ssl_message_queue *queue,
262 size_t buf_len, size_t *msg_len)
Yanray Wangbd56b032023-03-14 14:36:48 +0800263{
264 if (queue == NULL || msg_len == NULL) {
265 return MBEDTLS_TEST_ERROR_ARG_NULL;
266 }
267 if (queue->num == 0) {
268 return MBEDTLS_ERR_SSL_WANT_READ;
269 }
270
271 *msg_len = queue->messages[queue->pos];
272 return (*msg_len > buf_len) ? MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED : 0;
273}
274
Yanray Wangd02c3172023-03-15 16:02:29 +0800275void mbedtls_test_mock_socket_init(mbedtls_test_mock_socket *socket)
Yanray Wangbd56b032023-03-14 14:36:48 +0800276{
277 memset(socket, 0, sizeof(*socket));
278}
279
Yanray Wangbd56b032023-03-14 14:36:48 +0800280void mbedtls_test_mock_socket_close(mbedtls_test_mock_socket *socket)
281{
282 if (socket == NULL) {
283 return;
284 }
285
286 if (socket->input != NULL) {
287 mbedtls_test_ssl_buffer_free(socket->input);
288 mbedtls_free(socket->input);
289 }
290
291 if (socket->output != NULL) {
292 mbedtls_test_ssl_buffer_free(socket->output);
293 mbedtls_free(socket->output);
294 }
295
296 if (socket->peer != NULL) {
297 memset(socket->peer, 0, sizeof(*socket->peer));
298 }
299
300 memset(socket, 0, sizeof(*socket));
301}
302
Yanray Wangbd56b032023-03-14 14:36:48 +0800303int mbedtls_test_mock_socket_connect(mbedtls_test_mock_socket *peer1,
304 mbedtls_test_mock_socket *peer2,
305 size_t bufsize)
306{
307 int ret = -1;
308
309 peer1->output =
310 (mbedtls_test_ssl_buffer *) mbedtls_calloc(
311 1, sizeof(mbedtls_test_ssl_buffer));
312 if (peer1->output == NULL) {
313 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
314 goto exit;
315 }
316 mbedtls_test_ssl_buffer_init(peer1->output);
317 if (0 != (ret = mbedtls_test_ssl_buffer_setup(peer1->output, bufsize))) {
318 goto exit;
319 }
320
321 peer2->output =
322 (mbedtls_test_ssl_buffer *) mbedtls_calloc(
323 1, sizeof(mbedtls_test_ssl_buffer));
324 if (peer2->output == NULL) {
325 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
326 goto exit;
327 }
328 mbedtls_test_ssl_buffer_init(peer2->output);
329 if (0 != (ret = mbedtls_test_ssl_buffer_setup(peer2->output, bufsize))) {
330 goto exit;
331 }
332
333 peer1->peer = peer2;
334 peer2->peer = peer1;
335 peer1->input = peer2->output;
336 peer2->input = peer1->output;
337
338 peer1->status = peer2->status = MBEDTLS_MOCK_SOCKET_CONNECTED;
339 ret = 0;
340
341exit:
342
343 if (ret != 0) {
344 mbedtls_test_mock_socket_close(peer1);
345 mbedtls_test_mock_socket_close(peer2);
346 }
347
348 return ret;
349}
350
Yanray Wangbd56b032023-03-14 14:36:48 +0800351int mbedtls_test_mock_tcp_send_b(void *ctx,
352 const unsigned char *buf, size_t len)
353{
354 mbedtls_test_mock_socket *socket = (mbedtls_test_mock_socket *) ctx;
355
356 if (socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED) {
357 return -1;
358 }
359
360 return mbedtls_test_ssl_buffer_put(socket->output, buf, len);
361}
362
363int mbedtls_test_mock_tcp_recv_b(void *ctx, unsigned char *buf, size_t len)
364{
365 mbedtls_test_mock_socket *socket = (mbedtls_test_mock_socket *) ctx;
366
367 if (socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED) {
368 return -1;
369 }
370
371 return mbedtls_test_ssl_buffer_get(socket->input, buf, len);
372}
373
Yanray Wangbd56b032023-03-14 14:36:48 +0800374int mbedtls_test_mock_tcp_send_nb(void *ctx,
375 const unsigned char *buf, size_t len)
376{
377 mbedtls_test_mock_socket *socket = (mbedtls_test_mock_socket *) ctx;
378
379 if (socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED) {
380 return -1;
381 }
382
383 if (socket->output->capacity == socket->output->content_length) {
384 return MBEDTLS_ERR_SSL_WANT_WRITE;
385 }
386
387 return mbedtls_test_ssl_buffer_put(socket->output, buf, len);
388}
389
390int mbedtls_test_mock_tcp_recv_nb(void *ctx, unsigned char *buf, size_t len)
391{
392 mbedtls_test_mock_socket *socket = (mbedtls_test_mock_socket *) ctx;
393
394 if (socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED) {
395 return -1;
396 }
397
398 if (socket->input->content_length == 0) {
399 return MBEDTLS_ERR_SSL_WANT_READ;
400 }
401
402 return mbedtls_test_ssl_buffer_get(socket->input, buf, len);
403}
404
405void mbedtls_test_message_socket_init(
406 mbedtls_test_message_socket_context *ctx)
407{
408 ctx->queue_input = NULL;
409 ctx->queue_output = NULL;
410 ctx->socket = NULL;
411}
412
Yanray Wangbd56b032023-03-14 14:36:48 +0800413int mbedtls_test_message_socket_setup(
414 mbedtls_test_ssl_message_queue *queue_input,
415 mbedtls_test_ssl_message_queue *queue_output,
416 size_t queue_capacity,
417 mbedtls_test_mock_socket *socket,
418 mbedtls_test_message_socket_context *ctx)
419{
420 int ret = mbedtls_test_ssl_message_queue_setup(queue_input, queue_capacity);
421 if (ret != 0) {
422 return ret;
423 }
424 ctx->queue_input = queue_input;
425 ctx->queue_output = queue_output;
426 ctx->socket = socket;
Yanray Wangd02c3172023-03-15 16:02:29 +0800427 mbedtls_test_mock_socket_init(socket);
Yanray Wangbd56b032023-03-14 14:36:48 +0800428
429 return 0;
430}
431
Yanray Wangbd56b032023-03-14 14:36:48 +0800432void mbedtls_test_message_socket_close(
433 mbedtls_test_message_socket_context *ctx)
434{
435 if (ctx == NULL) {
436 return;
437 }
438
439 mbedtls_test_ssl_message_queue_free(ctx->queue_input);
440 mbedtls_test_mock_socket_close(ctx->socket);
441 memset(ctx, 0, sizeof(*ctx));
442}
443
Yanray Wangbd56b032023-03-14 14:36:48 +0800444int mbedtls_test_mock_tcp_send_msg(void *ctx,
445 const unsigned char *buf, size_t len)
446{
447 mbedtls_test_ssl_message_queue *queue;
448 mbedtls_test_mock_socket *socket;
449 mbedtls_test_message_socket_context *context =
450 (mbedtls_test_message_socket_context *) ctx;
451
452 if (context == NULL || context->socket == NULL
453 || context->queue_output == NULL) {
454 return MBEDTLS_TEST_ERROR_CONTEXT_ERROR;
455 }
456
457 queue = context->queue_output;
458 socket = context->socket;
459
460 if (queue->num >= queue->capacity) {
461 return MBEDTLS_ERR_SSL_WANT_WRITE;
462 }
463
464 if (mbedtls_test_mock_tcp_send_b(socket, buf, len) != (int) len) {
465 return MBEDTLS_TEST_ERROR_SEND_FAILED;
466 }
467
468 return mbedtls_test_ssl_message_queue_push_info(queue, len);
469}
470
Yanray Wangbd56b032023-03-14 14:36:48 +0800471int mbedtls_test_mock_tcp_recv_msg(void *ctx,
472 unsigned char *buf, size_t buf_len)
473{
474 mbedtls_test_ssl_message_queue *queue;
475 mbedtls_test_mock_socket *socket;
476 mbedtls_test_message_socket_context *context =
477 (mbedtls_test_message_socket_context *) ctx;
478 size_t drop_len = 0;
479 size_t msg_len;
480 int ret;
481
482 if (context == NULL || context->socket == NULL
483 || context->queue_input == NULL) {
484 return MBEDTLS_TEST_ERROR_CONTEXT_ERROR;
485 }
486
487 queue = context->queue_input;
488 socket = context->socket;
489
490 /* Peek first, so that in case of a socket error the data remains in
491 * the queue. */
Yanray Wangc4abee22023-03-16 14:57:54 +0800492 ret = test_ssl_message_queue_peek_info(queue, buf_len, &msg_len);
Yanray Wangbd56b032023-03-14 14:36:48 +0800493 if (ret == MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED) {
494 /* Calculate how much to drop */
495 drop_len = msg_len - buf_len;
496
497 /* Set the requested message len to be buffer length */
498 msg_len = buf_len;
499 } else if (ret != 0) {
500 return ret;
501 }
502
503 if (mbedtls_test_mock_tcp_recv_b(socket, buf, msg_len) != (int) msg_len) {
504 return MBEDTLS_TEST_ERROR_RECV_FAILED;
505 }
506
507 if (ret == MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED) {
508 /* Drop the remaining part of the message */
509 if (mbedtls_test_mock_tcp_recv_b(socket, NULL, drop_len) !=
510 (int) drop_len) {
511 /* Inconsistent state - part of the message was read,
512 * and a part couldn't. Not much we can do here, but it should not
513 * happen in test environment, unless forced manually. */
514 }
515 }
516 mbedtls_test_ssl_message_queue_pop_info(queue, buf_len);
517
Yanray Wangd2696f22022-11-03 11:51:59 +0800518 return (msg_len > INT_MAX) ? INT_MAX : (int) msg_len;
Yanray Wangbd56b032023-03-14 14:36:48 +0800519}
520
521#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) && \
522 defined(MBEDTLS_CERTS_C) && \
523 defined(MBEDTLS_ENTROPY_C) && \
524 defined(MBEDTLS_CTR_DRBG_C)
525
526/*
527 * Deinitializes certificates from endpoint represented by \p ep.
528 */
Yanray Wangcd23aff2023-03-15 16:05:14 +0800529static void test_ssl_endpoint_certificate_free(mbedtls_test_ssl_endpoint *ep)
Yanray Wangbd56b032023-03-14 14:36:48 +0800530{
531 mbedtls_test_ssl_endpoint_certificate *cert = &(ep->cert);
532 if (cert != NULL) {
533 if (cert->ca_cert != NULL) {
534 mbedtls_x509_crt_free(cert->ca_cert);
535 mbedtls_free(cert->ca_cert);
536 cert->ca_cert = NULL;
537 }
538 if (cert->cert != NULL) {
539 mbedtls_x509_crt_free(cert->cert);
540 mbedtls_free(cert->cert);
541 cert->cert = NULL;
542 }
543 if (cert->pkey != NULL) {
544#if defined(MBEDTLS_USE_PSA_CRYPTO)
545 if (mbedtls_pk_get_type(cert->pkey) == MBEDTLS_PK_OPAQUE) {
546 mbedtls_svc_key_id_t *key_slot = cert->pkey->pk_ctx;
547 psa_destroy_key(*key_slot);
548 }
549#endif
550 mbedtls_pk_free(cert->pkey);
551 mbedtls_free(cert->pkey);
552 cert->pkey = NULL;
553 }
554 }
555}
556
Yanray Wangbd56b032023-03-14 14:36:48 +0800557int mbedtls_test_ssl_endpoint_certificate_init(mbedtls_test_ssl_endpoint *ep,
558 int pk_alg)
559{
560 int i = 0;
561 int ret = -1;
562 mbedtls_test_ssl_endpoint_certificate *cert = NULL;
563
564 if (ep == NULL) {
565 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
566 }
567
568 cert = &(ep->cert);
Tom Cosgrove30ceb232023-09-04 11:20:19 +0100569 TEST_CALLOC(cert->ca_cert, 1);
570 TEST_CALLOC(cert->cert, 1);
571 TEST_CALLOC(cert->pkey, 1);
Yanray Wangbd56b032023-03-14 14:36:48 +0800572
573 mbedtls_x509_crt_init(cert->ca_cert);
574 mbedtls_x509_crt_init(cert->cert);
575 mbedtls_pk_init(cert->pkey);
576
577 /* Load the trusted CA */
578
579 for (i = 0; mbedtls_test_cas_der[i] != NULL; i++) {
580 ret = mbedtls_x509_crt_parse_der(
581 cert->ca_cert,
582 (const unsigned char *) mbedtls_test_cas_der[i],
583 mbedtls_test_cas_der_len[i]);
584 TEST_ASSERT(ret == 0);
585 }
586
587 /* Load own certificate and private key */
588
589 if (ep->conf.endpoint == MBEDTLS_SSL_IS_SERVER) {
590 if (pk_alg == MBEDTLS_PK_RSA) {
591 ret = mbedtls_x509_crt_parse(
592 cert->cert,
593 (const unsigned char *) mbedtls_test_srv_crt_rsa_sha256_der,
594 mbedtls_test_srv_crt_rsa_sha256_der_len);
595 TEST_ASSERT(ret == 0);
596
597 ret = mbedtls_pk_parse_key(
598 cert->pkey,
599 (const unsigned char *) mbedtls_test_srv_key_rsa_der,
600 mbedtls_test_srv_key_rsa_der_len, NULL, 0);
601 TEST_ASSERT(ret == 0);
602 } else {
603 ret = mbedtls_x509_crt_parse(
604 cert->cert,
605 (const unsigned char *) mbedtls_test_srv_crt_ec_der,
606 mbedtls_test_srv_crt_ec_der_len);
607 TEST_ASSERT(ret == 0);
608
609 ret = mbedtls_pk_parse_key(
610 cert->pkey,
611 (const unsigned char *) mbedtls_test_srv_key_ec_der,
612 mbedtls_test_srv_key_ec_der_len, NULL, 0);
613 TEST_ASSERT(ret == 0);
614 }
615 } else {
616 if (pk_alg == MBEDTLS_PK_RSA) {
617 ret = mbedtls_x509_crt_parse(
618 cert->cert,
619 (const unsigned char *) mbedtls_test_cli_crt_rsa_der,
620 mbedtls_test_cli_crt_rsa_der_len);
621 TEST_ASSERT(ret == 0);
622
623 ret = mbedtls_pk_parse_key(
624 cert->pkey,
625 (const unsigned char *) mbedtls_test_cli_key_rsa_der,
626 mbedtls_test_cli_key_rsa_der_len, NULL, 0);
627 TEST_ASSERT(ret == 0);
628 } else {
629 ret = mbedtls_x509_crt_parse(
630 cert->cert,
631 (const unsigned char *) mbedtls_test_cli_crt_ec_der,
632 mbedtls_test_cli_crt_ec_len);
633 TEST_ASSERT(ret == 0);
634
635 ret = mbedtls_pk_parse_key(
636 cert->pkey,
637 (const unsigned char *) mbedtls_test_cli_key_ec_der,
638 mbedtls_test_cli_key_ec_der_len, NULL, 0);
639 TEST_ASSERT(ret == 0);
640 }
641 }
642
643 mbedtls_ssl_conf_ca_chain(&(ep->conf), cert->ca_cert, NULL);
644
645 ret = mbedtls_ssl_conf_own_cert(&(ep->conf), cert->cert,
646 cert->pkey);
647 TEST_ASSERT(ret == 0);
648
649exit:
650 if (ret != 0) {
Yanray Wangcd23aff2023-03-15 16:05:14 +0800651 test_ssl_endpoint_certificate_free(ep);
Yanray Wangbd56b032023-03-14 14:36:48 +0800652 }
653
654 return ret;
655}
656
Yanray Wangbd56b032023-03-14 14:36:48 +0800657int mbedtls_test_ssl_endpoint_init(
658 mbedtls_test_ssl_endpoint *ep, int endpoint_type, int pk_alg,
659 mbedtls_test_message_socket_context *dtls_context,
660 mbedtls_test_ssl_message_queue *input_queue,
661 mbedtls_test_ssl_message_queue *output_queue,
662 const mbedtls_ecp_group_id *curves)
663{
664 int ret = -1;
665
666 if (dtls_context != NULL &&
667 (input_queue == NULL || output_queue == NULL)) {
668 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
669 }
670
671 if (ep == NULL) {
672 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
673 }
674
675 memset(ep, 0, sizeof(*ep));
676
677 ep->name = (endpoint_type == MBEDTLS_SSL_IS_SERVER) ? "Server" : "Client";
678
679 mbedtls_ssl_init(&(ep->ssl));
680 mbedtls_ssl_config_init(&(ep->conf));
681 mbedtls_ctr_drbg_init(&(ep->ctr_drbg));
682 mbedtls_ssl_conf_rng(&(ep->conf),
683 mbedtls_ctr_drbg_random,
684 &(ep->ctr_drbg));
685 mbedtls_entropy_init(&(ep->entropy));
686 if (dtls_context != NULL) {
687 TEST_ASSERT(mbedtls_test_message_socket_setup(input_queue, output_queue,
688 100, &(ep->socket),
689 dtls_context) == 0);
690 } else {
Yanray Wangd02c3172023-03-15 16:02:29 +0800691 mbedtls_test_mock_socket_init(&(ep->socket));
Yanray Wangbd56b032023-03-14 14:36:48 +0800692 }
693
694 ret = mbedtls_ctr_drbg_seed(&(ep->ctr_drbg), mbedtls_entropy_func,
695 &(ep->entropy),
696 (const unsigned char *) (ep->name),
697 strlen(ep->name));
698 TEST_ASSERT(ret == 0);
699
700 /* Non-blocking callbacks without timeout */
701 if (dtls_context != NULL) {
702 mbedtls_ssl_set_bio(&(ep->ssl), dtls_context,
703 mbedtls_test_mock_tcp_send_msg,
704 mbedtls_test_mock_tcp_recv_msg,
705 NULL);
706 } else {
707 mbedtls_ssl_set_bio(&(ep->ssl), &(ep->socket),
708 mbedtls_test_mock_tcp_send_nb,
709 mbedtls_test_mock_tcp_recv_nb,
710 NULL);
711 }
712
713 ret = mbedtls_ssl_config_defaults(&(ep->conf), endpoint_type,
714 (dtls_context != NULL) ?
715 MBEDTLS_SSL_TRANSPORT_DATAGRAM :
716 MBEDTLS_SSL_TRANSPORT_STREAM,
717 MBEDTLS_SSL_PRESET_DEFAULT);
718 TEST_ASSERT(ret == 0);
719
720#if defined(MBEDTLS_ECP_C)
721 if (curves != NULL) {
722 mbedtls_ssl_conf_curves(&(ep->conf), curves);
723 }
724#else
725 (void) curves;
726#endif
727
728 ret = mbedtls_ssl_setup(&(ep->ssl), &(ep->conf));
729 TEST_ASSERT(ret == 0);
730
731#if defined(MBEDTLS_SSL_PROTO_DTLS) && defined(MBEDTLS_SSL_SRV_C)
732 if (endpoint_type == MBEDTLS_SSL_IS_SERVER && dtls_context != NULL) {
733 mbedtls_ssl_conf_dtls_cookies(&(ep->conf), NULL, NULL, NULL);
734 }
735#endif
736
737 ret = mbedtls_test_ssl_endpoint_certificate_init(ep, pk_alg);
738 TEST_ASSERT(ret == 0);
739
740exit:
741 return ret;
742}
743
Yanray Wangbd56b032023-03-14 14:36:48 +0800744void mbedtls_test_ssl_endpoint_free(
745 mbedtls_test_ssl_endpoint *ep,
746 mbedtls_test_message_socket_context *context)
747{
Yanray Wangcd23aff2023-03-15 16:05:14 +0800748 test_ssl_endpoint_certificate_free(ep);
Yanray Wangbd56b032023-03-14 14:36:48 +0800749
750 mbedtls_ssl_free(&(ep->ssl));
751 mbedtls_ssl_config_free(&(ep->conf));
752 mbedtls_ctr_drbg_free(&(ep->ctr_drbg));
753 mbedtls_entropy_free(&(ep->entropy));
754
755 if (context != NULL) {
756 mbedtls_test_message_socket_close(context);
757 } else {
758 mbedtls_test_mock_socket_close(&(ep->socket));
759 }
760}
761
Yanray Wangbd56b032023-03-14 14:36:48 +0800762int mbedtls_test_move_handshake_to_state(mbedtls_ssl_context *ssl,
763 mbedtls_ssl_context *second_ssl,
764 int state)
765{
766 enum { BUFFSIZE = 1024 };
767 int max_steps = 1000;
768 int ret = 0;
769
770 if (ssl == NULL || second_ssl == NULL) {
771 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
772 }
773
774 /* Perform communication via connected sockets */
775 while ((ssl->state != state) && (--max_steps >= 0)) {
776 /* If /p second_ssl ends the handshake procedure before /p ssl then
777 * there is no need to call the next step */
778 if (second_ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
779 ret = mbedtls_ssl_handshake_step(second_ssl);
780 if (ret != 0 && ret != MBEDTLS_ERR_SSL_WANT_READ &&
781 ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
782 return ret;
783 }
784 }
785
786 /* We only care about the \p ssl state and returns, so we call it last,
787 * to leave the iteration as soon as the state is as expected. */
788 ret = mbedtls_ssl_handshake_step(ssl);
789 if (ret != 0 && ret != MBEDTLS_ERR_SSL_WANT_READ &&
790 ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
791 return ret;
792 }
793 }
794
795 return (max_steps >= 0) ? ret : -1;
796}
797
Yanray Wang1ef77c02023-03-14 16:59:00 +0800798#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED && MBEDTLS_CERTS_C &&
799 MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */
Yanray Wangbd56b032023-03-14 14:36:48 +0800800
801/*
802 * Write application data. Increase write counter if necessary.
803 */
804int mbedtls_ssl_write_fragment(mbedtls_ssl_context *ssl,
805 unsigned char *buf, int buf_len,
806 int *written,
807 const int expected_fragments)
808{
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +0100809 int ret;
Agathiyan Bragadeeshe7eb8052023-07-31 16:16:38 +0100810
Yanray Wangbd56b032023-03-14 14:36:48 +0800811 /* Verify that calling mbedtls_ssl_write with a NULL buffer and zero length is
812 * a valid no-op for TLS connections. */
813 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
814 TEST_ASSERT(mbedtls_ssl_write(ssl, NULL, 0) == 0);
815 }
816
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +0100817 ret = mbedtls_ssl_write(ssl, buf + *written, buf_len - *written);
Yanray Wangbd56b032023-03-14 14:36:48 +0800818 if (ret > 0) {
819 *written += ret;
820 }
821
822 if (expected_fragments == 0) {
823 /* Used for DTLS and the message size larger than MFL. In that case
824 * the message can not be fragmented and the library should return
825 * MBEDTLS_ERR_SSL_BAD_INPUT_DATA error. This error must be returned
Yanray Wang74df2012023-03-16 12:15:49 +0800826 * to prevent a dead loop inside mbedtls_test_ssl_exchange_data(). */
Yanray Wangbd56b032023-03-14 14:36:48 +0800827 return ret;
828 } else if (expected_fragments == 1) {
829 /* Used for TLS/DTLS and the message size lower than MFL */
830 TEST_ASSERT(ret == buf_len ||
831 ret == MBEDTLS_ERR_SSL_WANT_READ ||
832 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
833 } else {
834 /* Used for TLS and the message size larger than MFL */
835 TEST_ASSERT(expected_fragments > 1);
836 TEST_ASSERT((ret >= 0 && ret <= buf_len) ||
837 ret == MBEDTLS_ERR_SSL_WANT_READ ||
838 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
839 }
840
841 return 0;
842
843exit:
844 /* Some of the tests failed */
845 return -1;
846}
847
848/*
849 * Read application data and increase read counter and fragments counter
850 * if necessary.
851 */
852int mbedtls_ssl_read_fragment(mbedtls_ssl_context *ssl,
853 unsigned char *buf, int buf_len,
854 int *read, int *fragments,
855 const int expected_fragments)
856{
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +0100857 int ret;
Agathiyan Bragadeeshe7eb8052023-07-31 16:16:38 +0100858
Yanray Wangbd56b032023-03-14 14:36:48 +0800859 /* Verify that calling mbedtls_ssl_write with a NULL buffer and zero length is
860 * a valid no-op for TLS connections. */
861 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
862 TEST_ASSERT(mbedtls_ssl_read(ssl, NULL, 0) == 0);
863 }
864
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +0100865 ret = mbedtls_ssl_read(ssl, buf + *read, buf_len - *read);
Yanray Wangbd56b032023-03-14 14:36:48 +0800866 if (ret > 0) {
867 (*fragments)++;
868 *read += ret;
869 }
870
871 if (expected_fragments == 0) {
872 TEST_ASSERT(ret == 0);
873 } else if (expected_fragments == 1) {
874 TEST_ASSERT(ret == buf_len ||
875 ret == MBEDTLS_ERR_SSL_WANT_READ ||
876 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
877 } else {
878 TEST_ASSERT(expected_fragments > 1);
879 TEST_ASSERT((ret >= 0 && ret <= buf_len) ||
880 ret == MBEDTLS_ERR_SSL_WANT_READ ||
881 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
882 }
883
884 return 0;
885
886exit:
887 /* Some of the tests failed */
888 return -1;
889}
890
Yanray Wangb4ef9a22023-03-16 12:04:49 +0800891#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) && \
892 defined(MBEDTLS_CERTS_C) && \
893 defined(MBEDTLS_ENTROPY_C) && \
894 defined(MBEDTLS_CTR_DRBG_C)
895static void set_ciphersuite(mbedtls_ssl_config *conf, const char *cipher,
896 int *forced_ciphersuite)
Yanray Wangbd56b032023-03-14 14:36:48 +0800897{
898 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
899 forced_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id(cipher);
900 forced_ciphersuite[1] = 0;
901
902 ciphersuite_info =
903 mbedtls_ssl_ciphersuite_from_id(forced_ciphersuite[0]);
904
905 TEST_ASSERT(ciphersuite_info != NULL);
906 TEST_ASSERT(ciphersuite_info->min_minor_ver <= conf->max_minor_ver);
907 TEST_ASSERT(ciphersuite_info->max_minor_ver >= conf->min_minor_ver);
908
909 if (conf->max_minor_ver > ciphersuite_info->max_minor_ver) {
910 conf->max_minor_ver = ciphersuite_info->max_minor_ver;
911 }
912 if (conf->min_minor_ver < ciphersuite_info->min_minor_ver) {
913 conf->min_minor_ver = ciphersuite_info->min_minor_ver;
914 }
915
916 mbedtls_ssl_conf_ciphersuites(conf, forced_ciphersuite);
917
918exit:
919 return;
920}
Yanray Wangb4ef9a22023-03-16 12:04:49 +0800921#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED && MBEDTLS_CERTS_C &&
922 MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */
Yanray Wangbd56b032023-03-14 14:36:48 +0800923
Yanray Wangb4ef9a22023-03-16 12:04:49 +0800924#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) && \
925 defined(MBEDTLS_CERTS_C) && \
926 defined(MBEDTLS_ENTROPY_C) && \
927 defined(MBEDTLS_CTR_DRBG_C) && \
928 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
929static int psk_dummy_callback(void *p_info, mbedtls_ssl_context *ssl,
930 const unsigned char *name, size_t name_len)
Yanray Wangbd56b032023-03-14 14:36:48 +0800931{
932 (void) p_info;
933 (void) ssl;
934 (void) name;
935 (void) name_len;
936
937 return 0;
938}
Yanray Wangb4ef9a22023-03-16 12:04:49 +0800939#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED && MBEDTLS_CERTS_C &&
940 MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C &&
941 MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Yanray Wangbd56b032023-03-14 14:36:48 +0800942
Yanray Wangbd56b032023-03-14 14:36:48 +0800943int mbedtls_test_ssl_build_transforms(mbedtls_ssl_transform *t_in,
944 mbedtls_ssl_transform *t_out,
945 int cipher_type, int hash_id,
946 int etm, int tag_mode, int ver,
947 size_t cid0_len,
948 size_t cid1_len)
949{
950 mbedtls_cipher_info_t const *cipher_info;
951 int ret = 0;
952
953 size_t keylen, maclen, ivlen;
954 unsigned char *key0 = NULL, *key1 = NULL;
955 unsigned char *md0 = NULL, *md1 = NULL;
956 unsigned char iv_enc[16], iv_dec[16];
957
958#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
959 unsigned char cid0[SSL_CID_LEN_MIN];
960 unsigned char cid1[SSL_CID_LEN_MIN];
961
962 mbedtls_test_rnd_std_rand(NULL, cid0, sizeof(cid0));
963 mbedtls_test_rnd_std_rand(NULL, cid1, sizeof(cid1));
964#else
965 ((void) cid0_len);
966 ((void) cid1_len);
967#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
968
969 maclen = 0;
970
971 /* Pick cipher */
972 cipher_info = mbedtls_cipher_info_from_type(cipher_type);
973 CHK(cipher_info != NULL);
974 CHK(cipher_info->iv_size <= 16);
975 CHK(cipher_info->key_bitlen % 8 == 0);
976
977 /* Pick keys */
978 keylen = cipher_info->key_bitlen / 8;
979 /* Allocate `keylen + 1` bytes to ensure that we get
980 * a non-NULL pointers from `mbedtls_calloc` even if
981 * `keylen == 0` in the case of the NULL cipher. */
982 CHK((key0 = mbedtls_calloc(1, keylen + 1)) != NULL);
983 CHK((key1 = mbedtls_calloc(1, keylen + 1)) != NULL);
984 memset(key0, 0x1, keylen);
985 memset(key1, 0x2, keylen);
986
987 /* Setup cipher contexts */
988 CHK(mbedtls_cipher_setup(&t_in->cipher_ctx_enc, cipher_info) == 0);
989 CHK(mbedtls_cipher_setup(&t_in->cipher_ctx_dec, cipher_info) == 0);
990 CHK(mbedtls_cipher_setup(&t_out->cipher_ctx_enc, cipher_info) == 0);
991 CHK(mbedtls_cipher_setup(&t_out->cipher_ctx_dec, cipher_info) == 0);
992
993#if defined(MBEDTLS_CIPHER_MODE_CBC)
994 if (cipher_info->mode == MBEDTLS_MODE_CBC) {
995 CHK(mbedtls_cipher_set_padding_mode(&t_in->cipher_ctx_enc,
996 MBEDTLS_PADDING_NONE) == 0);
997 CHK(mbedtls_cipher_set_padding_mode(&t_in->cipher_ctx_dec,
998 MBEDTLS_PADDING_NONE) == 0);
999 CHK(mbedtls_cipher_set_padding_mode(&t_out->cipher_ctx_enc,
1000 MBEDTLS_PADDING_NONE) == 0);
1001 CHK(mbedtls_cipher_set_padding_mode(&t_out->cipher_ctx_dec,
1002 MBEDTLS_PADDING_NONE) == 0);
1003 }
1004#endif /* MBEDTLS_CIPHER_MODE_CBC */
1005
1006 CHK(mbedtls_cipher_setkey(&t_in->cipher_ctx_enc, key0,
Yanray Wangd2696f22022-11-03 11:51:59 +08001007 (keylen << 3 > INT_MAX) ? INT_MAX : (int) keylen << 3,
1008 MBEDTLS_ENCRYPT)
1009 == 0);
Yanray Wangbd56b032023-03-14 14:36:48 +08001010 CHK(mbedtls_cipher_setkey(&t_in->cipher_ctx_dec, key1,
Yanray Wangd2696f22022-11-03 11:51:59 +08001011 (keylen << 3 > INT_MAX) ? INT_MAX : (int) keylen << 3,
1012 MBEDTLS_DECRYPT)
1013 == 0);
Yanray Wangbd56b032023-03-14 14:36:48 +08001014 CHK(mbedtls_cipher_setkey(&t_out->cipher_ctx_enc, key1,
Yanray Wangd2696f22022-11-03 11:51:59 +08001015 (keylen << 3 > INT_MAX) ? INT_MAX : (int) keylen << 3,
1016 MBEDTLS_ENCRYPT)
1017 == 0);
Yanray Wangbd56b032023-03-14 14:36:48 +08001018 CHK(mbedtls_cipher_setkey(&t_out->cipher_ctx_dec, key0,
Yanray Wangd2696f22022-11-03 11:51:59 +08001019 (keylen << 3 > INT_MAX) ? INT_MAX : (int) keylen << 3,
1020 MBEDTLS_DECRYPT)
1021 == 0);
Yanray Wangbd56b032023-03-14 14:36:48 +08001022
1023 /* Setup MAC contexts */
1024#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1025 if (cipher_info->mode == MBEDTLS_MODE_CBC ||
1026 cipher_info->mode == MBEDTLS_MODE_STREAM) {
1027 mbedtls_md_info_t const *md_info;
1028
1029 /* Pick hash */
1030 md_info = mbedtls_md_info_from_type(hash_id);
1031 CHK(md_info != NULL);
1032
1033 /* Pick hash keys */
1034 maclen = mbedtls_md_get_size(md_info);
1035 CHK((md0 = mbedtls_calloc(1, maclen)) != NULL);
1036 CHK((md1 = mbedtls_calloc(1, maclen)) != NULL);
1037 memset(md0, 0x5, maclen);
1038 memset(md1, 0x6, maclen);
1039
1040 CHK(mbedtls_md_setup(&t_out->md_ctx_enc, md_info, 1) == 0);
1041 CHK(mbedtls_md_setup(&t_out->md_ctx_dec, md_info, 1) == 0);
1042 CHK(mbedtls_md_setup(&t_in->md_ctx_enc, md_info, 1) == 0);
1043 CHK(mbedtls_md_setup(&t_in->md_ctx_dec, md_info, 1) == 0);
1044
1045 if (ver > MBEDTLS_SSL_MINOR_VERSION_0) {
1046 CHK(mbedtls_md_hmac_starts(&t_in->md_ctx_enc,
1047 md0, maclen) == 0);
1048 CHK(mbedtls_md_hmac_starts(&t_in->md_ctx_dec,
1049 md1, maclen) == 0);
1050 CHK(mbedtls_md_hmac_starts(&t_out->md_ctx_enc,
1051 md1, maclen) == 0);
1052 CHK(mbedtls_md_hmac_starts(&t_out->md_ctx_dec,
1053 md0, maclen) == 0);
1054 }
1055#if defined(MBEDTLS_SSL_PROTO_SSL3)
1056 else {
1057 memcpy(&t_in->mac_enc, md0, maclen);
1058 memcpy(&t_in->mac_dec, md1, maclen);
1059 memcpy(&t_out->mac_enc, md1, maclen);
1060 memcpy(&t_out->mac_dec, md0, maclen);
1061 }
1062#endif
1063 }
1064#else
1065 ((void) hash_id);
1066#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1067
1068
1069 /* Pick IV's (regardless of whether they
1070 * are being used by the transform). */
1071 ivlen = cipher_info->iv_size;
1072 memset(iv_enc, 0x3, sizeof(iv_enc));
1073 memset(iv_dec, 0x4, sizeof(iv_dec));
1074
1075 /*
1076 * Setup transforms
1077 */
1078
1079#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
1080 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1081 t_out->encrypt_then_mac = etm;
1082 t_in->encrypt_then_mac = etm;
1083#else
1084 ((void) etm);
1085#endif
1086
1087 t_out->minor_ver = ver;
1088 t_in->minor_ver = ver;
1089 t_out->ivlen = ivlen;
1090 t_in->ivlen = ivlen;
1091
1092 switch (cipher_info->mode) {
1093 case MBEDTLS_MODE_GCM:
1094 case MBEDTLS_MODE_CCM:
1095#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
1096 if (ver == MBEDTLS_SSL_MINOR_VERSION_4) {
1097 t_out->fixed_ivlen = 12;
1098 t_in->fixed_ivlen = 12;
1099 } else
1100#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
1101 {
1102 t_out->fixed_ivlen = 4;
1103 t_in->fixed_ivlen = 4;
1104 }
1105 t_out->maclen = 0;
1106 t_in->maclen = 0;
1107 switch (tag_mode) {
1108 case 0: /* Full tag */
1109 t_out->taglen = 16;
1110 t_in->taglen = 16;
1111 break;
1112 case 1: /* Partial tag */
1113 t_out->taglen = 8;
1114 t_in->taglen = 8;
1115 break;
1116 default:
1117 ret = 1;
1118 goto cleanup;
1119 }
1120 break;
1121
1122 case MBEDTLS_MODE_CHACHAPOLY:
1123 t_out->fixed_ivlen = 12;
1124 t_in->fixed_ivlen = 12;
1125 t_out->maclen = 0;
1126 t_in->maclen = 0;
1127 switch (tag_mode) {
1128 case 0: /* Full tag */
1129 t_out->taglen = 16;
1130 t_in->taglen = 16;
1131 break;
1132 case 1: /* Partial tag */
1133 t_out->taglen = 8;
1134 t_in->taglen = 8;
1135 break;
1136 default:
1137 ret = 1;
1138 goto cleanup;
1139 }
1140 break;
1141
1142 case MBEDTLS_MODE_STREAM:
1143 case MBEDTLS_MODE_CBC:
1144 t_out->fixed_ivlen = 0; /* redundant, must be 0 */
1145 t_in->fixed_ivlen = 0; /* redundant, must be 0 */
1146 t_out->taglen = 0;
1147 t_in->taglen = 0;
1148 switch (tag_mode) {
1149 case 0: /* Full tag */
1150 t_out->maclen = maclen;
1151 t_in->maclen = maclen;
1152 break;
1153 case 1: /* Partial tag */
1154 t_out->maclen = 10;
1155 t_in->maclen = 10;
1156 break;
1157 default:
1158 ret = 1;
1159 goto cleanup;
1160 }
1161 break;
1162 default:
1163 ret = 1;
1164 goto cleanup;
1165 break;
1166 }
1167
1168 /* Setup IV's */
1169
1170 memcpy(&t_in->iv_dec, iv_dec, sizeof(iv_dec));
1171 memcpy(&t_in->iv_enc, iv_enc, sizeof(iv_enc));
1172 memcpy(&t_out->iv_dec, iv_enc, sizeof(iv_enc));
1173 memcpy(&t_out->iv_enc, iv_dec, sizeof(iv_dec));
1174
1175#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
1176 /* Add CID */
1177 memcpy(&t_in->in_cid, cid0, cid0_len);
1178 memcpy(&t_in->out_cid, cid1, cid1_len);
Yanray Wangd2696f22022-11-03 11:51:59 +08001179 t_in->in_cid_len = (uint8_t) cid0_len;
1180 t_in->out_cid_len = (uint8_t) cid1_len;
Yanray Wangbd56b032023-03-14 14:36:48 +08001181 memcpy(&t_out->in_cid, cid1, cid1_len);
1182 memcpy(&t_out->out_cid, cid0, cid0_len);
Yanray Wangd2696f22022-11-03 11:51:59 +08001183 t_out->in_cid_len = (uint8_t) cid1_len;
1184 t_out->out_cid_len = (uint8_t) cid0_len;
Yanray Wangbd56b032023-03-14 14:36:48 +08001185#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
1186
1187cleanup:
1188
1189 mbedtls_free(key0);
1190 mbedtls_free(key1);
1191
1192 mbedtls_free(md0);
1193 mbedtls_free(md1);
1194
1195 return ret;
1196}
1197
Gilles Peskine2198cc52023-09-18 13:11:50 +02001198#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1199int mbedtls_test_ssl_prepare_record_mac(mbedtls_record *record,
1200 mbedtls_ssl_transform *transform_out)
1201{
1202 /* Serialized version of record header for MAC purposes */
1203 unsigned char add_data[13];
1204 memcpy(add_data, record->ctr, 8);
1205 add_data[8] = record->type;
1206 add_data[9] = record->ver[0];
1207 add_data[10] = record->ver[1];
1208 add_data[11] = (record->data_len >> 8) & 0xff;
1209 add_data[12] = (record->data_len >> 0) & 0xff;
1210
1211 /* MAC with additional data */
1212 TEST_EQUAL(0, mbedtls_md_hmac_update(&transform_out->md_ctx_enc, add_data, 13));
1213 TEST_EQUAL(0, mbedtls_md_hmac_update(&transform_out->md_ctx_enc,
1214 record->buf + record->data_offset,
1215 record->data_len));
1216 /* Use a temporary buffer for the MAC, because with the truncated HMAC
1217 * extension, there might not be enough room in the record for the
1218 * full-length MAC. */
1219 unsigned char mac[MBEDTLS_MD_MAX_SIZE];
1220 TEST_EQUAL(0, mbedtls_md_hmac_finish(&transform_out->md_ctx_enc, mac));
1221 memcpy(record->buf + record->data_offset + record->data_len, mac, transform_out->maclen);
1222 record->data_len += transform_out->maclen;
1223
1224 return 0;
1225
1226exit:
1227 return -1;
1228}
1229#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1230
Yanray Wangbd56b032023-03-14 14:36:48 +08001231int mbedtls_test_ssl_populate_session(mbedtls_ssl_session *session,
1232 int ticket_len,
1233 const char *crt_file)
1234{
1235#if defined(MBEDTLS_HAVE_TIME)
1236 session->start = mbedtls_time(NULL) - 42;
1237#endif
1238 session->ciphersuite = 0xabcd;
1239 session->compression = 1;
1240 session->id_len = sizeof(session->id);
1241 memset(session->id, 66, session->id_len);
1242 memset(session->master, 17, sizeof(session->master));
1243
1244#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) && \
1245 defined(MBEDTLS_CERTS_C) && \
1246 defined(MBEDTLS_FS_IO)
1247 if (strlen(crt_file) != 0) {
1248 mbedtls_x509_crt tmp_crt;
1249 int ret;
1250
1251 mbedtls_x509_crt_init(&tmp_crt);
1252 ret = mbedtls_x509_crt_parse_file(&tmp_crt, crt_file);
1253 if (ret != 0) {
1254 return ret;
1255 }
1256
1257#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1258 /* Move temporary CRT. */
1259 session->peer_cert = mbedtls_calloc(1, sizeof(*session->peer_cert));
1260 if (session->peer_cert == NULL) {
1261 return -1;
1262 }
1263 *session->peer_cert = tmp_crt;
1264 memset(&tmp_crt, 0, sizeof(tmp_crt));
1265#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
1266 /* Calculate digest of temporary CRT. */
1267 session->peer_cert_digest =
1268 mbedtls_calloc(1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN);
1269 if (session->peer_cert_digest == NULL) {
1270 return -1;
1271 }
1272 ret = mbedtls_md(mbedtls_md_info_from_type(
1273 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE),
1274 tmp_crt.raw.p, tmp_crt.raw.len,
1275 session->peer_cert_digest);
1276 if (ret != 0) {
1277 return ret;
1278 }
1279 session->peer_cert_digest_type =
1280 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
1281 session->peer_cert_digest_len =
1282 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
1283#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
1284
1285 mbedtls_x509_crt_free(&tmp_crt);
1286 }
1287#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED && MBEDTLS_CERTS_C && MBEDTLS_FS_IO */
1288 (void) crt_file;
1289#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED && MBEDTLS_CERTS_C && MBEDTLS_FS_IO */
1290 session->verify_result = 0xdeadbeef;
1291
1292#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
1293 if (ticket_len != 0) {
1294 session->ticket = mbedtls_calloc(1, ticket_len);
1295 if (session->ticket == NULL) {
1296 return -1;
1297 }
1298 memset(session->ticket, 33, ticket_len);
1299 }
1300 session->ticket_len = ticket_len;
1301 session->ticket_lifetime = 86401;
1302#else
1303 (void) ticket_len;
1304#endif
1305
1306#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1307 session->mfl_code = 1;
1308#endif
1309#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1310 session->trunc_hmac = 1;
1311#endif
1312#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1313 session->encrypt_then_mac = 1;
1314#endif
1315
1316 return 0;
1317}
1318
Yanray Wang74df2012023-03-16 12:15:49 +08001319int mbedtls_test_ssl_exchange_data(
1320 mbedtls_ssl_context *ssl_1,
1321 int msg_len_1, const int expected_fragments_1,
1322 mbedtls_ssl_context *ssl_2,
1323 int msg_len_2, const int expected_fragments_2)
Yanray Wangbd56b032023-03-14 14:36:48 +08001324{
1325 unsigned char *msg_buf_1 = malloc(msg_len_1);
1326 unsigned char *msg_buf_2 = malloc(msg_len_2);
1327 unsigned char *in_buf_1 = malloc(msg_len_2);
1328 unsigned char *in_buf_2 = malloc(msg_len_1);
1329 int msg_type, ret = -1;
1330
1331 /* Perform this test with two message types. At first use a message
1332 * consisting of only 0x00 for the client and only 0xFF for the server.
1333 * At the second time use message with generated data */
1334 for (msg_type = 0; msg_type < 2; msg_type++) {
1335 int written_1 = 0;
1336 int written_2 = 0;
1337 int read_1 = 0;
1338 int read_2 = 0;
1339 int fragments_1 = 0;
1340 int fragments_2 = 0;
1341
1342 if (msg_type == 0) {
1343 memset(msg_buf_1, 0x00, msg_len_1);
1344 memset(msg_buf_2, 0xff, msg_len_2);
1345 } else {
1346 int i, j = 0;
1347 for (i = 0; i < msg_len_1; i++) {
1348 msg_buf_1[i] = j++ & 0xFF;
1349 }
1350 for (i = 0; i < msg_len_2; i++) {
1351 msg_buf_2[i] = (j -= 5) & 0xFF;
1352 }
1353 }
1354
1355 while (read_1 < msg_len_2 || read_2 < msg_len_1) {
1356 /* ssl_1 sending */
1357 if (msg_len_1 > written_1) {
1358 ret = mbedtls_ssl_write_fragment(ssl_1, msg_buf_1,
1359 msg_len_1, &written_1,
1360 expected_fragments_1);
1361 if (expected_fragments_1 == 0) {
1362 /* This error is expected when the message is too large and
1363 * cannot be fragmented */
1364 TEST_ASSERT(ret == MBEDTLS_ERR_SSL_BAD_INPUT_DATA);
1365 msg_len_1 = 0;
1366 } else {
1367 TEST_ASSERT(ret == 0);
1368 }
1369 }
1370
1371 /* ssl_2 sending */
1372 if (msg_len_2 > written_2) {
1373 ret = mbedtls_ssl_write_fragment(ssl_2, msg_buf_2,
1374 msg_len_2, &written_2,
1375 expected_fragments_2);
1376 if (expected_fragments_2 == 0) {
1377 /* This error is expected when the message is too large and
1378 * cannot be fragmented */
1379 TEST_ASSERT(ret == MBEDTLS_ERR_SSL_BAD_INPUT_DATA);
1380 msg_len_2 = 0;
1381 } else {
1382 TEST_ASSERT(ret == 0);
1383 }
1384 }
1385
1386 /* ssl_1 reading */
1387 if (read_1 < msg_len_2) {
1388 ret = mbedtls_ssl_read_fragment(ssl_1, in_buf_1,
1389 msg_len_2, &read_1,
1390 &fragments_2,
1391 expected_fragments_2);
1392 TEST_ASSERT(ret == 0);
1393 }
1394
1395 /* ssl_2 reading */
1396 if (read_2 < msg_len_1) {
1397 ret = mbedtls_ssl_read_fragment(ssl_2, in_buf_2,
1398 msg_len_1, &read_2,
1399 &fragments_1,
1400 expected_fragments_1);
1401 TEST_ASSERT(ret == 0);
1402 }
1403 }
1404
1405 ret = -1;
1406 TEST_ASSERT(0 == memcmp(msg_buf_1, in_buf_2, msg_len_1));
1407 TEST_ASSERT(0 == memcmp(msg_buf_2, in_buf_1, msg_len_2));
1408 TEST_ASSERT(fragments_1 == expected_fragments_1);
1409 TEST_ASSERT(fragments_2 == expected_fragments_2);
1410 }
1411
1412 ret = 0;
1413
1414exit:
1415 free(msg_buf_1);
1416 free(in_buf_1);
1417 free(msg_buf_2);
1418 free(in_buf_2);
1419
1420 return ret;
1421}
1422
1423/*
1424 * Perform data exchanging between \p ssl_1 and \p ssl_2. Both of endpoints
1425 * must be initialized and connected beforehand.
1426 *
1427 * \retval 0 on success, otherwise error code.
1428 */
Yanray Wangb4ef9a22023-03-16 12:04:49 +08001429#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) && \
1430 defined(MBEDTLS_CERTS_C) && \
1431 defined(MBEDTLS_ENTROPY_C) && \
1432 defined(MBEDTLS_CTR_DRBG_C) && \
1433 (defined(MBEDTLS_SSL_RENEGOTIATION) || \
1434 defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH))
1435static int exchange_data(mbedtls_ssl_context *ssl_1,
1436 mbedtls_ssl_context *ssl_2)
Yanray Wangbd56b032023-03-14 14:36:48 +08001437{
Yanray Wang74df2012023-03-16 12:15:49 +08001438 return mbedtls_test_ssl_exchange_data(ssl_1, 256, 1,
1439 ssl_2, 256, 1);
Yanray Wangbd56b032023-03-14 14:36:48 +08001440}
Yanray Wangb4ef9a22023-03-16 12:04:49 +08001441#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED && MBEDTLS_CERTS_C &&
1442 MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C &&
1443 (MBEDTLS_SSL_RENEGOTIATION ||
1444 MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) */
Yanray Wangbd56b032023-03-14 14:36:48 +08001445
1446#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) && \
Yanray Wangb4ef9a22023-03-16 12:04:49 +08001447 defined(MBEDTLS_CERTS_C) && \
1448 defined(MBEDTLS_ENTROPY_C) && \
Yanray Wangbd56b032023-03-14 14:36:48 +08001449 defined(MBEDTLS_CTR_DRBG_C)
1450void mbedtls_test_ssl_perform_handshake(
1451 mbedtls_test_handshake_test_options *options)
1452{
1453 /* forced_ciphersuite needs to last until the end of the handshake */
1454 int forced_ciphersuite[2];
1455 enum { BUFFSIZE = 17000 };
1456 mbedtls_test_ssl_endpoint client, server;
1457#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
1458 const char *psk_identity = "foo";
1459#endif
1460#if defined(MBEDTLS_TIMING_C)
1461 mbedtls_timing_delay_context timer_client, timer_server;
1462#endif
1463#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1464 unsigned char *context_buf = NULL;
1465 size_t context_buf_len;
1466#endif
1467#if defined(MBEDTLS_SSL_RENEGOTIATION)
1468 int ret = -1;
1469#endif
1470 int expected_handshake_result = 0;
1471
1472 USE_PSA_INIT();
1473 mbedtls_platform_zeroize(&client, sizeof(client));
1474 mbedtls_platform_zeroize(&server, sizeof(server));
1475
1476 mbedtls_test_ssl_message_queue server_queue, client_queue;
1477 mbedtls_test_message_socket_context server_context, client_context;
1478 mbedtls_test_message_socket_init(&server_context);
1479 mbedtls_test_message_socket_init(&client_context);
1480
1481 /* Client side */
1482 if (options->dtls != 0) {
1483 TEST_ASSERT(mbedtls_test_ssl_endpoint_init(&client,
1484 MBEDTLS_SSL_IS_CLIENT,
1485 options->pk_alg,
1486 &client_context,
1487 &client_queue,
1488 &server_queue, NULL) == 0);
1489#if defined(MBEDTLS_TIMING_C)
1490 mbedtls_ssl_set_timer_cb(&client.ssl, &timer_client,
1491 mbedtls_timing_set_delay,
1492 mbedtls_timing_get_delay);
1493#endif
1494 } else {
1495 TEST_ASSERT(mbedtls_test_ssl_endpoint_init(&client,
1496 MBEDTLS_SSL_IS_CLIENT,
1497 options->pk_alg, NULL, NULL,
1498 NULL, NULL) == 0);
1499 }
1500
1501 if (options->client_min_version != TEST_SSL_MINOR_VERSION_NONE) {
1502 mbedtls_ssl_conf_min_version(&client.conf, MBEDTLS_SSL_MAJOR_VERSION_3,
1503 options->client_min_version);
1504 }
1505
1506 if (options->client_max_version != TEST_SSL_MINOR_VERSION_NONE) {
1507 mbedtls_ssl_conf_max_version(&client.conf, MBEDTLS_SSL_MAJOR_VERSION_3,
1508 options->client_max_version);
1509 }
1510
1511 if (strlen(options->cipher) > 0) {
1512 set_ciphersuite(&client.conf, options->cipher, forced_ciphersuite);
1513 }
1514
1515#if defined(MBEDTLS_DEBUG_C)
1516 if (options->cli_log_fun) {
1517 mbedtls_debug_set_threshold(4);
1518 mbedtls_ssl_conf_dbg(&client.conf, options->cli_log_fun,
1519 options->cli_log_obj);
1520 }
1521#endif
1522
1523 /* Server side */
1524 if (options->dtls != 0) {
1525 TEST_ASSERT(mbedtls_test_ssl_endpoint_init(&server,
1526 MBEDTLS_SSL_IS_SERVER,
1527 options->pk_alg,
1528 &server_context,
1529 &server_queue,
1530 &client_queue, NULL) == 0);
1531#if defined(MBEDTLS_TIMING_C)
1532 mbedtls_ssl_set_timer_cb(&server.ssl, &timer_server,
1533 mbedtls_timing_set_delay,
1534 mbedtls_timing_get_delay);
1535#endif
1536 } else {
1537 TEST_ASSERT(mbedtls_test_ssl_endpoint_init(&server,
1538 MBEDTLS_SSL_IS_SERVER,
1539 options->pk_alg, NULL, NULL,
1540 NULL, NULL) == 0);
1541 }
1542
1543 mbedtls_ssl_conf_authmode(&server.conf, options->srv_auth_mode);
1544
1545 if (options->server_min_version != TEST_SSL_MINOR_VERSION_NONE) {
1546 mbedtls_ssl_conf_min_version(&server.conf, MBEDTLS_SSL_MAJOR_VERSION_3,
1547 options->server_min_version);
1548 }
1549
1550 if (options->server_max_version != TEST_SSL_MINOR_VERSION_NONE) {
1551 mbedtls_ssl_conf_max_version(&server.conf, MBEDTLS_SSL_MAJOR_VERSION_3,
1552 options->server_max_version);
1553 }
1554
1555#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1556 TEST_ASSERT(mbedtls_ssl_conf_max_frag_len(&(server.conf),
1557 (unsigned char) options->mfl)
1558 == 0);
1559 TEST_ASSERT(mbedtls_ssl_conf_max_frag_len(&(client.conf),
1560 (unsigned char) options->mfl)
1561 == 0);
1562#else
1563 TEST_ASSERT(MBEDTLS_SSL_MAX_FRAG_LEN_NONE == options->mfl);
1564#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
1565
1566#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
1567 if (options->psk_str != NULL && options->psk_str->len > 0) {
1568 TEST_ASSERT(mbedtls_ssl_conf_psk(
1569 &client.conf, options->psk_str->x,
1570 options->psk_str->len,
1571 (const unsigned char *) psk_identity,
1572 strlen(psk_identity)) == 0);
1573
1574 TEST_ASSERT(mbedtls_ssl_conf_psk(
1575 &server.conf, options->psk_str->x,
1576 options->psk_str->len,
1577 (const unsigned char *) psk_identity,
1578 strlen(psk_identity)) == 0);
1579
1580 mbedtls_ssl_conf_psk_cb(&server.conf, psk_dummy_callback, NULL);
1581 }
1582#endif
1583#if defined(MBEDTLS_SSL_RENEGOTIATION)
1584 if (options->renegotiate) {
1585 mbedtls_ssl_conf_renegotiation(&(server.conf),
1586 MBEDTLS_SSL_RENEGOTIATION_ENABLED);
1587 mbedtls_ssl_conf_renegotiation(&(client.conf),
1588 MBEDTLS_SSL_RENEGOTIATION_ENABLED);
1589
1590 mbedtls_ssl_conf_legacy_renegotiation(&(server.conf),
1591 options->legacy_renegotiation);
1592 mbedtls_ssl_conf_legacy_renegotiation(&(client.conf),
1593 options->legacy_renegotiation);
1594 }
1595#endif /* MBEDTLS_SSL_RENEGOTIATION */
1596
1597#if defined(MBEDTLS_DEBUG_C)
1598 if (options->srv_log_fun) {
1599 mbedtls_debug_set_threshold(4);
1600 mbedtls_ssl_conf_dbg(&server.conf, options->srv_log_fun,
1601 options->srv_log_obj);
1602 }
1603#endif
1604
1605 TEST_ASSERT(mbedtls_test_mock_socket_connect(&(client.socket),
1606 &(server.socket),
1607 BUFFSIZE) == 0);
1608
1609#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1610 if (options->resize_buffers != 0) {
1611 /* Ensure that the buffer sizes are appropriate before resizes */
1612 TEST_ASSERT(client.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN);
1613 TEST_ASSERT(client.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN);
1614 TEST_ASSERT(server.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN);
1615 TEST_ASSERT(server.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN);
1616 }
1617#endif
1618
1619 if (options->expected_negotiated_version == TEST_SSL_MINOR_VERSION_NONE) {
1620 expected_handshake_result = MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION;
1621 }
1622
1623 TEST_ASSERT(mbedtls_test_move_handshake_to_state(
1624 &(client.ssl), &(server.ssl), MBEDTLS_SSL_HANDSHAKE_OVER)
1625 == expected_handshake_result);
1626
1627 if (expected_handshake_result != 0) {
1628 /* Connection will have failed by this point, skip to cleanup */
1629 goto exit;
1630 }
1631
1632 TEST_ASSERT(client.ssl.state == MBEDTLS_SSL_HANDSHAKE_OVER);
1633 TEST_ASSERT(server.ssl.state == MBEDTLS_SSL_HANDSHAKE_OVER);
1634
1635 /* Check that we agree on the version... */
1636 TEST_ASSERT(client.ssl.minor_ver == server.ssl.minor_ver);
1637
1638 /* And check that the version negotiated is the expected one. */
1639 TEST_EQUAL(client.ssl.minor_ver, options->expected_negotiated_version);
1640
1641#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1642 if (options->resize_buffers != 0) {
1643 if (options->expected_negotiated_version != MBEDTLS_SSL_MINOR_VERSION_0 &&
1644 options->expected_negotiated_version != MBEDTLS_SSL_MINOR_VERSION_1) {
1645 /* A server, when using DTLS, might delay a buffer resize to happen
1646 * after it receives a message, so we force it. */
1647 TEST_ASSERT(exchange_data(&(client.ssl), &(server.ssl)) == 0);
1648
1649 TEST_ASSERT(client.ssl.out_buf_len ==
1650 mbedtls_ssl_get_output_buflen(&client.ssl));
1651 TEST_ASSERT(client.ssl.in_buf_len ==
1652 mbedtls_ssl_get_input_buflen(&client.ssl));
1653 TEST_ASSERT(server.ssl.out_buf_len ==
1654 mbedtls_ssl_get_output_buflen(&server.ssl));
1655 TEST_ASSERT(server.ssl.in_buf_len ==
1656 mbedtls_ssl_get_input_buflen(&server.ssl));
1657 }
1658 }
1659#endif
1660
1661 if (options->cli_msg_len != 0 || options->srv_msg_len != 0) {
1662 /* Start data exchanging test */
Yanray Wang74df2012023-03-16 12:15:49 +08001663 TEST_ASSERT(mbedtls_test_ssl_exchange_data(
1664 &(client.ssl), options->cli_msg_len,
1665 options->expected_cli_fragments,
1666 &(server.ssl), options->srv_msg_len,
1667 options->expected_srv_fragments)
Yanray Wangbd56b032023-03-14 14:36:48 +08001668 == 0);
1669 }
1670#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1671 if (options->serialize == 1) {
1672 TEST_ASSERT(options->dtls == 1);
1673
1674 TEST_ASSERT(mbedtls_ssl_context_save(&(server.ssl), NULL,
1675 0, &context_buf_len)
1676 == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL);
1677
1678 context_buf = mbedtls_calloc(1, context_buf_len);
1679 TEST_ASSERT(context_buf != NULL);
1680
1681 TEST_ASSERT(mbedtls_ssl_context_save(&(server.ssl), context_buf,
1682 context_buf_len,
1683 &context_buf_len)
1684 == 0);
1685
1686 mbedtls_ssl_free(&(server.ssl));
1687 mbedtls_ssl_init(&(server.ssl));
1688
1689 TEST_ASSERT(mbedtls_ssl_setup(&(server.ssl), &(server.conf)) == 0);
1690
1691 mbedtls_ssl_set_bio(&(server.ssl), &server_context,
1692 mbedtls_test_mock_tcp_send_msg,
1693 mbedtls_test_mock_tcp_recv_msg,
1694 NULL);
1695
1696#if defined(MBEDTLS_TIMING_C)
1697 mbedtls_ssl_set_timer_cb(&server.ssl, &timer_server,
1698 mbedtls_timing_set_delay,
1699 mbedtls_timing_get_delay);
1700#endif
1701#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1702 if (options->resize_buffers != 0) {
1703 /* Ensure that the buffer sizes are appropriate before resizes */
1704 TEST_ASSERT(server.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN);
1705 TEST_ASSERT(server.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN);
1706 }
1707#endif
1708 TEST_ASSERT(mbedtls_ssl_context_load(&(server.ssl), context_buf,
1709 context_buf_len) == 0);
1710
1711#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1712 /* Validate buffer sizes after context deserialization */
1713 if (options->resize_buffers != 0) {
1714 TEST_ASSERT(server.ssl.out_buf_len ==
1715 mbedtls_ssl_get_output_buflen(&server.ssl));
1716 TEST_ASSERT(server.ssl.in_buf_len ==
1717 mbedtls_ssl_get_input_buflen(&server.ssl));
1718 }
1719#endif
1720 /* Retest writing/reading */
1721 if (options->cli_msg_len != 0 || options->srv_msg_len != 0) {
Yanray Wang74df2012023-03-16 12:15:49 +08001722 TEST_ASSERT(mbedtls_test_ssl_exchange_data(
1723 &(client.ssl), options->cli_msg_len,
Yanray Wangbd56b032023-03-14 14:36:48 +08001724 options->expected_cli_fragments,
Yanray Wang74df2012023-03-16 12:15:49 +08001725 &(server.ssl), options->srv_msg_len,
Yanray Wangbd56b032023-03-14 14:36:48 +08001726 options->expected_srv_fragments)
1727 == 0);
1728 }
1729 }
1730#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
1731
1732#if defined(MBEDTLS_SSL_RENEGOTIATION)
1733 if (options->renegotiate) {
1734 /* Start test with renegotiation */
1735 TEST_ASSERT(server.ssl.renego_status ==
1736 MBEDTLS_SSL_INITIAL_HANDSHAKE);
1737 TEST_ASSERT(client.ssl.renego_status ==
1738 MBEDTLS_SSL_INITIAL_HANDSHAKE);
1739
1740 /* After calling this function for the server, it only sends a handshake
1741 * request. All renegotiation should happen during data exchanging */
1742 TEST_ASSERT(mbedtls_ssl_renegotiate(&(server.ssl)) == 0);
1743 TEST_ASSERT(server.ssl.renego_status ==
1744 MBEDTLS_SSL_RENEGOTIATION_PENDING);
1745 TEST_ASSERT(client.ssl.renego_status ==
1746 MBEDTLS_SSL_INITIAL_HANDSHAKE);
1747
1748 TEST_ASSERT(exchange_data(&(client.ssl), &(server.ssl)) == 0);
1749 TEST_ASSERT(server.ssl.renego_status ==
1750 MBEDTLS_SSL_RENEGOTIATION_DONE);
1751 TEST_ASSERT(client.ssl.renego_status ==
1752 MBEDTLS_SSL_RENEGOTIATION_DONE);
1753
1754 /* After calling mbedtls_ssl_renegotiate for the client,
1755 * all renegotiation should happen inside this function.
1756 * However in this test, we cannot perform simultaneous communication
1757 * between client and server so this function will return waiting error
1758 * on the socket. All rest of renegotiation should happen
1759 * during data exchanging */
1760 ret = mbedtls_ssl_renegotiate(&(client.ssl));
1761#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1762 if (options->resize_buffers != 0) {
1763 /* Ensure that the buffer sizes are appropriate before resizes */
1764 TEST_ASSERT(client.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN);
1765 TEST_ASSERT(client.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN);
1766 }
1767#endif
1768 TEST_ASSERT(ret == 0 ||
1769 ret == MBEDTLS_ERR_SSL_WANT_READ ||
1770 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
1771 TEST_ASSERT(server.ssl.renego_status ==
1772 MBEDTLS_SSL_RENEGOTIATION_DONE);
1773 TEST_ASSERT(client.ssl.renego_status ==
1774 MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS);
1775
1776 TEST_ASSERT(exchange_data(&(client.ssl), &(server.ssl)) == 0);
1777 TEST_ASSERT(server.ssl.renego_status ==
1778 MBEDTLS_SSL_RENEGOTIATION_DONE);
1779 TEST_ASSERT(client.ssl.renego_status ==
1780 MBEDTLS_SSL_RENEGOTIATION_DONE);
1781#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1782 /* Validate buffer sizes after renegotiation */
1783 if (options->resize_buffers != 0) {
1784 TEST_ASSERT(client.ssl.out_buf_len ==
1785 mbedtls_ssl_get_output_buflen(&client.ssl));
1786 TEST_ASSERT(client.ssl.in_buf_len ==
1787 mbedtls_ssl_get_input_buflen(&client.ssl));
1788 TEST_ASSERT(server.ssl.out_buf_len ==
1789 mbedtls_ssl_get_output_buflen(&server.ssl));
1790 TEST_ASSERT(server.ssl.in_buf_len ==
1791 mbedtls_ssl_get_input_buflen(&server.ssl));
1792 }
1793#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
1794 }
1795#endif /* MBEDTLS_SSL_RENEGOTIATION */
1796
1797exit:
1798 mbedtls_test_ssl_endpoint_free(&client,
1799 options->dtls != 0 ? &client_context : NULL);
1800 mbedtls_test_ssl_endpoint_free(&server,
1801 options->dtls != 0 ? &server_context : NULL);
1802#if defined(MBEDTLS_DEBUG_C)
1803 if (options->cli_log_fun || options->srv_log_fun) {
1804 mbedtls_debug_set_threshold(0);
1805 }
1806#endif
1807#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1808 if (context_buf != NULL) {
1809 mbedtls_free(context_buf);
1810 }
1811#endif
1812}
Yanray Wang1ef77c02023-03-14 16:59:00 +08001813#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED && MBEDTLS_CERTS_C &&
1814 MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */
Yanray Wangbd56b032023-03-14 14:36:48 +08001815
Yanray Wang4323e452023-03-14 16:52:06 +08001816#endif /* MBEDTLS_SSL_TLS_C */