blob: 52759aac9f28c7e455d0200a1dc57d6f400263fe [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);
569 ASSERT_ALLOC(cert->ca_cert, 1);
570 ASSERT_ALLOC(cert->cert, 1);
571 ASSERT_ALLOC(cert->pkey, 1);
572
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;
Yanray Wangbd56b032023-03-14 14:36:48 +0800810 /* Verify that calling mbedtls_ssl_write with a NULL buffer and zero length is
811 * a valid no-op for TLS connections. */
812 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
813 TEST_ASSERT(mbedtls_ssl_write(ssl, NULL, 0) == 0);
814 }
815
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +0100816 ret = mbedtls_ssl_write(ssl, buf + *written, buf_len - *written);
Yanray Wangbd56b032023-03-14 14:36:48 +0800817 if (ret > 0) {
818 *written += ret;
819 }
820
821 if (expected_fragments == 0) {
822 /* Used for DTLS and the message size larger than MFL. In that case
823 * the message can not be fragmented and the library should return
824 * MBEDTLS_ERR_SSL_BAD_INPUT_DATA error. This error must be returned
Yanray Wang74df2012023-03-16 12:15:49 +0800825 * to prevent a dead loop inside mbedtls_test_ssl_exchange_data(). */
Yanray Wangbd56b032023-03-14 14:36:48 +0800826 return ret;
827 } else if (expected_fragments == 1) {
828 /* Used for TLS/DTLS and the message size lower than MFL */
829 TEST_ASSERT(ret == buf_len ||
830 ret == MBEDTLS_ERR_SSL_WANT_READ ||
831 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
832 } else {
833 /* Used for TLS and the message size larger than MFL */
834 TEST_ASSERT(expected_fragments > 1);
835 TEST_ASSERT((ret >= 0 && ret <= buf_len) ||
836 ret == MBEDTLS_ERR_SSL_WANT_READ ||
837 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
838 }
839
840 return 0;
841
842exit:
843 /* Some of the tests failed */
844 return -1;
845}
846
847/*
848 * Read application data and increase read counter and fragments counter
849 * if necessary.
850 */
851int mbedtls_ssl_read_fragment(mbedtls_ssl_context *ssl,
852 unsigned char *buf, int buf_len,
853 int *read, int *fragments,
854 const int expected_fragments)
855{
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +0100856 int ret;
Yanray Wangbd56b032023-03-14 14:36:48 +0800857 /* Verify that calling mbedtls_ssl_write with a NULL buffer and zero length is
858 * a valid no-op for TLS connections. */
859 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
860 TEST_ASSERT(mbedtls_ssl_read(ssl, NULL, 0) == 0);
861 }
862
Agathiyan Bragadeesh7b0ee1e2023-07-27 15:51:46 +0100863 ret = mbedtls_ssl_read(ssl, buf + *read, buf_len - *read);
Yanray Wangbd56b032023-03-14 14:36:48 +0800864 if (ret > 0) {
865 (*fragments)++;
866 *read += ret;
867 }
868
869 if (expected_fragments == 0) {
870 TEST_ASSERT(ret == 0);
871 } else if (expected_fragments == 1) {
872 TEST_ASSERT(ret == buf_len ||
873 ret == MBEDTLS_ERR_SSL_WANT_READ ||
874 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
875 } else {
876 TEST_ASSERT(expected_fragments > 1);
877 TEST_ASSERT((ret >= 0 && ret <= buf_len) ||
878 ret == MBEDTLS_ERR_SSL_WANT_READ ||
879 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
880 }
881
882 return 0;
883
884exit:
885 /* Some of the tests failed */
886 return -1;
887}
888
Yanray Wangb4ef9a22023-03-16 12:04:49 +0800889#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) && \
890 defined(MBEDTLS_CERTS_C) && \
891 defined(MBEDTLS_ENTROPY_C) && \
892 defined(MBEDTLS_CTR_DRBG_C)
893static void set_ciphersuite(mbedtls_ssl_config *conf, const char *cipher,
894 int *forced_ciphersuite)
Yanray Wangbd56b032023-03-14 14:36:48 +0800895{
896 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
897 forced_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id(cipher);
898 forced_ciphersuite[1] = 0;
899
900 ciphersuite_info =
901 mbedtls_ssl_ciphersuite_from_id(forced_ciphersuite[0]);
902
903 TEST_ASSERT(ciphersuite_info != NULL);
904 TEST_ASSERT(ciphersuite_info->min_minor_ver <= conf->max_minor_ver);
905 TEST_ASSERT(ciphersuite_info->max_minor_ver >= conf->min_minor_ver);
906
907 if (conf->max_minor_ver > ciphersuite_info->max_minor_ver) {
908 conf->max_minor_ver = ciphersuite_info->max_minor_ver;
909 }
910 if (conf->min_minor_ver < ciphersuite_info->min_minor_ver) {
911 conf->min_minor_ver = ciphersuite_info->min_minor_ver;
912 }
913
914 mbedtls_ssl_conf_ciphersuites(conf, forced_ciphersuite);
915
916exit:
917 return;
918}
Yanray Wangb4ef9a22023-03-16 12:04:49 +0800919#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED && MBEDTLS_CERTS_C &&
920 MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */
Yanray Wangbd56b032023-03-14 14:36:48 +0800921
Yanray Wangb4ef9a22023-03-16 12:04:49 +0800922#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) && \
923 defined(MBEDTLS_CERTS_C) && \
924 defined(MBEDTLS_ENTROPY_C) && \
925 defined(MBEDTLS_CTR_DRBG_C) && \
926 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
927static int psk_dummy_callback(void *p_info, mbedtls_ssl_context *ssl,
928 const unsigned char *name, size_t name_len)
Yanray Wangbd56b032023-03-14 14:36:48 +0800929{
930 (void) p_info;
931 (void) ssl;
932 (void) name;
933 (void) name_len;
934
935 return 0;
936}
Yanray Wangb4ef9a22023-03-16 12:04:49 +0800937#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED && MBEDTLS_CERTS_C &&
938 MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C &&
939 MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Yanray Wangbd56b032023-03-14 14:36:48 +0800940
Yanray Wangbd56b032023-03-14 14:36:48 +0800941int mbedtls_test_ssl_build_transforms(mbedtls_ssl_transform *t_in,
942 mbedtls_ssl_transform *t_out,
943 int cipher_type, int hash_id,
944 int etm, int tag_mode, int ver,
945 size_t cid0_len,
946 size_t cid1_len)
947{
948 mbedtls_cipher_info_t const *cipher_info;
949 int ret = 0;
950
951 size_t keylen, maclen, ivlen;
952 unsigned char *key0 = NULL, *key1 = NULL;
953 unsigned char *md0 = NULL, *md1 = NULL;
954 unsigned char iv_enc[16], iv_dec[16];
955
956#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
957 unsigned char cid0[SSL_CID_LEN_MIN];
958 unsigned char cid1[SSL_CID_LEN_MIN];
959
960 mbedtls_test_rnd_std_rand(NULL, cid0, sizeof(cid0));
961 mbedtls_test_rnd_std_rand(NULL, cid1, sizeof(cid1));
962#else
963 ((void) cid0_len);
964 ((void) cid1_len);
965#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
966
967 maclen = 0;
968
969 /* Pick cipher */
970 cipher_info = mbedtls_cipher_info_from_type(cipher_type);
971 CHK(cipher_info != NULL);
972 CHK(cipher_info->iv_size <= 16);
973 CHK(cipher_info->key_bitlen % 8 == 0);
974
975 /* Pick keys */
976 keylen = cipher_info->key_bitlen / 8;
977 /* Allocate `keylen + 1` bytes to ensure that we get
978 * a non-NULL pointers from `mbedtls_calloc` even if
979 * `keylen == 0` in the case of the NULL cipher. */
980 CHK((key0 = mbedtls_calloc(1, keylen + 1)) != NULL);
981 CHK((key1 = mbedtls_calloc(1, keylen + 1)) != NULL);
982 memset(key0, 0x1, keylen);
983 memset(key1, 0x2, keylen);
984
985 /* Setup cipher contexts */
986 CHK(mbedtls_cipher_setup(&t_in->cipher_ctx_enc, cipher_info) == 0);
987 CHK(mbedtls_cipher_setup(&t_in->cipher_ctx_dec, cipher_info) == 0);
988 CHK(mbedtls_cipher_setup(&t_out->cipher_ctx_enc, cipher_info) == 0);
989 CHK(mbedtls_cipher_setup(&t_out->cipher_ctx_dec, cipher_info) == 0);
990
991#if defined(MBEDTLS_CIPHER_MODE_CBC)
992 if (cipher_info->mode == MBEDTLS_MODE_CBC) {
993 CHK(mbedtls_cipher_set_padding_mode(&t_in->cipher_ctx_enc,
994 MBEDTLS_PADDING_NONE) == 0);
995 CHK(mbedtls_cipher_set_padding_mode(&t_in->cipher_ctx_dec,
996 MBEDTLS_PADDING_NONE) == 0);
997 CHK(mbedtls_cipher_set_padding_mode(&t_out->cipher_ctx_enc,
998 MBEDTLS_PADDING_NONE) == 0);
999 CHK(mbedtls_cipher_set_padding_mode(&t_out->cipher_ctx_dec,
1000 MBEDTLS_PADDING_NONE) == 0);
1001 }
1002#endif /* MBEDTLS_CIPHER_MODE_CBC */
1003
1004 CHK(mbedtls_cipher_setkey(&t_in->cipher_ctx_enc, key0,
Yanray Wangd2696f22022-11-03 11:51:59 +08001005 (keylen << 3 > INT_MAX) ? INT_MAX : (int) keylen << 3,
1006 MBEDTLS_ENCRYPT)
1007 == 0);
Yanray Wangbd56b032023-03-14 14:36:48 +08001008 CHK(mbedtls_cipher_setkey(&t_in->cipher_ctx_dec, key1,
Yanray Wangd2696f22022-11-03 11:51:59 +08001009 (keylen << 3 > INT_MAX) ? INT_MAX : (int) keylen << 3,
1010 MBEDTLS_DECRYPT)
1011 == 0);
Yanray Wangbd56b032023-03-14 14:36:48 +08001012 CHK(mbedtls_cipher_setkey(&t_out->cipher_ctx_enc, key1,
Yanray Wangd2696f22022-11-03 11:51:59 +08001013 (keylen << 3 > INT_MAX) ? INT_MAX : (int) keylen << 3,
1014 MBEDTLS_ENCRYPT)
1015 == 0);
Yanray Wangbd56b032023-03-14 14:36:48 +08001016 CHK(mbedtls_cipher_setkey(&t_out->cipher_ctx_dec, key0,
Yanray Wangd2696f22022-11-03 11:51:59 +08001017 (keylen << 3 > INT_MAX) ? INT_MAX : (int) keylen << 3,
1018 MBEDTLS_DECRYPT)
1019 == 0);
Yanray Wangbd56b032023-03-14 14:36:48 +08001020
1021 /* Setup MAC contexts */
1022#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1023 if (cipher_info->mode == MBEDTLS_MODE_CBC ||
1024 cipher_info->mode == MBEDTLS_MODE_STREAM) {
1025 mbedtls_md_info_t const *md_info;
1026
1027 /* Pick hash */
1028 md_info = mbedtls_md_info_from_type(hash_id);
1029 CHK(md_info != NULL);
1030
1031 /* Pick hash keys */
1032 maclen = mbedtls_md_get_size(md_info);
1033 CHK((md0 = mbedtls_calloc(1, maclen)) != NULL);
1034 CHK((md1 = mbedtls_calloc(1, maclen)) != NULL);
1035 memset(md0, 0x5, maclen);
1036 memset(md1, 0x6, maclen);
1037
1038 CHK(mbedtls_md_setup(&t_out->md_ctx_enc, md_info, 1) == 0);
1039 CHK(mbedtls_md_setup(&t_out->md_ctx_dec, md_info, 1) == 0);
1040 CHK(mbedtls_md_setup(&t_in->md_ctx_enc, md_info, 1) == 0);
1041 CHK(mbedtls_md_setup(&t_in->md_ctx_dec, md_info, 1) == 0);
1042
1043 if (ver > MBEDTLS_SSL_MINOR_VERSION_0) {
1044 CHK(mbedtls_md_hmac_starts(&t_in->md_ctx_enc,
1045 md0, maclen) == 0);
1046 CHK(mbedtls_md_hmac_starts(&t_in->md_ctx_dec,
1047 md1, maclen) == 0);
1048 CHK(mbedtls_md_hmac_starts(&t_out->md_ctx_enc,
1049 md1, maclen) == 0);
1050 CHK(mbedtls_md_hmac_starts(&t_out->md_ctx_dec,
1051 md0, maclen) == 0);
1052 }
1053#if defined(MBEDTLS_SSL_PROTO_SSL3)
1054 else {
1055 memcpy(&t_in->mac_enc, md0, maclen);
1056 memcpy(&t_in->mac_dec, md1, maclen);
1057 memcpy(&t_out->mac_enc, md1, maclen);
1058 memcpy(&t_out->mac_dec, md0, maclen);
1059 }
1060#endif
1061 }
1062#else
1063 ((void) hash_id);
1064#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1065
1066
1067 /* Pick IV's (regardless of whether they
1068 * are being used by the transform). */
1069 ivlen = cipher_info->iv_size;
1070 memset(iv_enc, 0x3, sizeof(iv_enc));
1071 memset(iv_dec, 0x4, sizeof(iv_dec));
1072
1073 /*
1074 * Setup transforms
1075 */
1076
1077#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
1078 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1079 t_out->encrypt_then_mac = etm;
1080 t_in->encrypt_then_mac = etm;
1081#else
1082 ((void) etm);
1083#endif
1084
1085 t_out->minor_ver = ver;
1086 t_in->minor_ver = ver;
1087 t_out->ivlen = ivlen;
1088 t_in->ivlen = ivlen;
1089
1090 switch (cipher_info->mode) {
1091 case MBEDTLS_MODE_GCM:
1092 case MBEDTLS_MODE_CCM:
1093#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
1094 if (ver == MBEDTLS_SSL_MINOR_VERSION_4) {
1095 t_out->fixed_ivlen = 12;
1096 t_in->fixed_ivlen = 12;
1097 } else
1098#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
1099 {
1100 t_out->fixed_ivlen = 4;
1101 t_in->fixed_ivlen = 4;
1102 }
1103 t_out->maclen = 0;
1104 t_in->maclen = 0;
1105 switch (tag_mode) {
1106 case 0: /* Full tag */
1107 t_out->taglen = 16;
1108 t_in->taglen = 16;
1109 break;
1110 case 1: /* Partial tag */
1111 t_out->taglen = 8;
1112 t_in->taglen = 8;
1113 break;
1114 default:
1115 ret = 1;
1116 goto cleanup;
1117 }
1118 break;
1119
1120 case MBEDTLS_MODE_CHACHAPOLY:
1121 t_out->fixed_ivlen = 12;
1122 t_in->fixed_ivlen = 12;
1123 t_out->maclen = 0;
1124 t_in->maclen = 0;
1125 switch (tag_mode) {
1126 case 0: /* Full tag */
1127 t_out->taglen = 16;
1128 t_in->taglen = 16;
1129 break;
1130 case 1: /* Partial tag */
1131 t_out->taglen = 8;
1132 t_in->taglen = 8;
1133 break;
1134 default:
1135 ret = 1;
1136 goto cleanup;
1137 }
1138 break;
1139
1140 case MBEDTLS_MODE_STREAM:
1141 case MBEDTLS_MODE_CBC:
1142 t_out->fixed_ivlen = 0; /* redundant, must be 0 */
1143 t_in->fixed_ivlen = 0; /* redundant, must be 0 */
1144 t_out->taglen = 0;
1145 t_in->taglen = 0;
1146 switch (tag_mode) {
1147 case 0: /* Full tag */
1148 t_out->maclen = maclen;
1149 t_in->maclen = maclen;
1150 break;
1151 case 1: /* Partial tag */
1152 t_out->maclen = 10;
1153 t_in->maclen = 10;
1154 break;
1155 default:
1156 ret = 1;
1157 goto cleanup;
1158 }
1159 break;
1160 default:
1161 ret = 1;
1162 goto cleanup;
1163 break;
1164 }
1165
1166 /* Setup IV's */
1167
1168 memcpy(&t_in->iv_dec, iv_dec, sizeof(iv_dec));
1169 memcpy(&t_in->iv_enc, iv_enc, sizeof(iv_enc));
1170 memcpy(&t_out->iv_dec, iv_enc, sizeof(iv_enc));
1171 memcpy(&t_out->iv_enc, iv_dec, sizeof(iv_dec));
1172
1173#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
1174 /* Add CID */
1175 memcpy(&t_in->in_cid, cid0, cid0_len);
1176 memcpy(&t_in->out_cid, cid1, cid1_len);
Yanray Wangd2696f22022-11-03 11:51:59 +08001177 t_in->in_cid_len = (uint8_t) cid0_len;
1178 t_in->out_cid_len = (uint8_t) cid1_len;
Yanray Wangbd56b032023-03-14 14:36:48 +08001179 memcpy(&t_out->in_cid, cid1, cid1_len);
1180 memcpy(&t_out->out_cid, cid0, cid0_len);
Yanray Wangd2696f22022-11-03 11:51:59 +08001181 t_out->in_cid_len = (uint8_t) cid1_len;
1182 t_out->out_cid_len = (uint8_t) cid0_len;
Yanray Wangbd56b032023-03-14 14:36:48 +08001183#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
1184
1185cleanup:
1186
1187 mbedtls_free(key0);
1188 mbedtls_free(key1);
1189
1190 mbedtls_free(md0);
1191 mbedtls_free(md1);
1192
1193 return ret;
1194}
1195
Yanray Wangbd56b032023-03-14 14:36:48 +08001196int mbedtls_test_ssl_populate_session(mbedtls_ssl_session *session,
1197 int ticket_len,
1198 const char *crt_file)
1199{
1200#if defined(MBEDTLS_HAVE_TIME)
1201 session->start = mbedtls_time(NULL) - 42;
1202#endif
1203 session->ciphersuite = 0xabcd;
1204 session->compression = 1;
1205 session->id_len = sizeof(session->id);
1206 memset(session->id, 66, session->id_len);
1207 memset(session->master, 17, sizeof(session->master));
1208
1209#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) && \
1210 defined(MBEDTLS_CERTS_C) && \
1211 defined(MBEDTLS_FS_IO)
1212 if (strlen(crt_file) != 0) {
1213 mbedtls_x509_crt tmp_crt;
1214 int ret;
1215
1216 mbedtls_x509_crt_init(&tmp_crt);
1217 ret = mbedtls_x509_crt_parse_file(&tmp_crt, crt_file);
1218 if (ret != 0) {
1219 return ret;
1220 }
1221
1222#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1223 /* Move temporary CRT. */
1224 session->peer_cert = mbedtls_calloc(1, sizeof(*session->peer_cert));
1225 if (session->peer_cert == NULL) {
1226 return -1;
1227 }
1228 *session->peer_cert = tmp_crt;
1229 memset(&tmp_crt, 0, sizeof(tmp_crt));
1230#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
1231 /* Calculate digest of temporary CRT. */
1232 session->peer_cert_digest =
1233 mbedtls_calloc(1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN);
1234 if (session->peer_cert_digest == NULL) {
1235 return -1;
1236 }
1237 ret = mbedtls_md(mbedtls_md_info_from_type(
1238 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE),
1239 tmp_crt.raw.p, tmp_crt.raw.len,
1240 session->peer_cert_digest);
1241 if (ret != 0) {
1242 return ret;
1243 }
1244 session->peer_cert_digest_type =
1245 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
1246 session->peer_cert_digest_len =
1247 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
1248#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
1249
1250 mbedtls_x509_crt_free(&tmp_crt);
1251 }
1252#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED && MBEDTLS_CERTS_C && MBEDTLS_FS_IO */
1253 (void) crt_file;
1254#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED && MBEDTLS_CERTS_C && MBEDTLS_FS_IO */
1255 session->verify_result = 0xdeadbeef;
1256
1257#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
1258 if (ticket_len != 0) {
1259 session->ticket = mbedtls_calloc(1, ticket_len);
1260 if (session->ticket == NULL) {
1261 return -1;
1262 }
1263 memset(session->ticket, 33, ticket_len);
1264 }
1265 session->ticket_len = ticket_len;
1266 session->ticket_lifetime = 86401;
1267#else
1268 (void) ticket_len;
1269#endif
1270
1271#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1272 session->mfl_code = 1;
1273#endif
1274#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1275 session->trunc_hmac = 1;
1276#endif
1277#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1278 session->encrypt_then_mac = 1;
1279#endif
1280
1281 return 0;
1282}
1283
Yanray Wang74df2012023-03-16 12:15:49 +08001284int mbedtls_test_ssl_exchange_data(
1285 mbedtls_ssl_context *ssl_1,
1286 int msg_len_1, const int expected_fragments_1,
1287 mbedtls_ssl_context *ssl_2,
1288 int msg_len_2, const int expected_fragments_2)
Yanray Wangbd56b032023-03-14 14:36:48 +08001289{
1290 unsigned char *msg_buf_1 = malloc(msg_len_1);
1291 unsigned char *msg_buf_2 = malloc(msg_len_2);
1292 unsigned char *in_buf_1 = malloc(msg_len_2);
1293 unsigned char *in_buf_2 = malloc(msg_len_1);
1294 int msg_type, ret = -1;
1295
1296 /* Perform this test with two message types. At first use a message
1297 * consisting of only 0x00 for the client and only 0xFF for the server.
1298 * At the second time use message with generated data */
1299 for (msg_type = 0; msg_type < 2; msg_type++) {
1300 int written_1 = 0;
1301 int written_2 = 0;
1302 int read_1 = 0;
1303 int read_2 = 0;
1304 int fragments_1 = 0;
1305 int fragments_2 = 0;
1306
1307 if (msg_type == 0) {
1308 memset(msg_buf_1, 0x00, msg_len_1);
1309 memset(msg_buf_2, 0xff, msg_len_2);
1310 } else {
1311 int i, j = 0;
1312 for (i = 0; i < msg_len_1; i++) {
1313 msg_buf_1[i] = j++ & 0xFF;
1314 }
1315 for (i = 0; i < msg_len_2; i++) {
1316 msg_buf_2[i] = (j -= 5) & 0xFF;
1317 }
1318 }
1319
1320 while (read_1 < msg_len_2 || read_2 < msg_len_1) {
1321 /* ssl_1 sending */
1322 if (msg_len_1 > written_1) {
1323 ret = mbedtls_ssl_write_fragment(ssl_1, msg_buf_1,
1324 msg_len_1, &written_1,
1325 expected_fragments_1);
1326 if (expected_fragments_1 == 0) {
1327 /* This error is expected when the message is too large and
1328 * cannot be fragmented */
1329 TEST_ASSERT(ret == MBEDTLS_ERR_SSL_BAD_INPUT_DATA);
1330 msg_len_1 = 0;
1331 } else {
1332 TEST_ASSERT(ret == 0);
1333 }
1334 }
1335
1336 /* ssl_2 sending */
1337 if (msg_len_2 > written_2) {
1338 ret = mbedtls_ssl_write_fragment(ssl_2, msg_buf_2,
1339 msg_len_2, &written_2,
1340 expected_fragments_2);
1341 if (expected_fragments_2 == 0) {
1342 /* This error is expected when the message is too large and
1343 * cannot be fragmented */
1344 TEST_ASSERT(ret == MBEDTLS_ERR_SSL_BAD_INPUT_DATA);
1345 msg_len_2 = 0;
1346 } else {
1347 TEST_ASSERT(ret == 0);
1348 }
1349 }
1350
1351 /* ssl_1 reading */
1352 if (read_1 < msg_len_2) {
1353 ret = mbedtls_ssl_read_fragment(ssl_1, in_buf_1,
1354 msg_len_2, &read_1,
1355 &fragments_2,
1356 expected_fragments_2);
1357 TEST_ASSERT(ret == 0);
1358 }
1359
1360 /* ssl_2 reading */
1361 if (read_2 < msg_len_1) {
1362 ret = mbedtls_ssl_read_fragment(ssl_2, in_buf_2,
1363 msg_len_1, &read_2,
1364 &fragments_1,
1365 expected_fragments_1);
1366 TEST_ASSERT(ret == 0);
1367 }
1368 }
1369
1370 ret = -1;
1371 TEST_ASSERT(0 == memcmp(msg_buf_1, in_buf_2, msg_len_1));
1372 TEST_ASSERT(0 == memcmp(msg_buf_2, in_buf_1, msg_len_2));
1373 TEST_ASSERT(fragments_1 == expected_fragments_1);
1374 TEST_ASSERT(fragments_2 == expected_fragments_2);
1375 }
1376
1377 ret = 0;
1378
1379exit:
1380 free(msg_buf_1);
1381 free(in_buf_1);
1382 free(msg_buf_2);
1383 free(in_buf_2);
1384
1385 return ret;
1386}
1387
1388/*
1389 * Perform data exchanging between \p ssl_1 and \p ssl_2. Both of endpoints
1390 * must be initialized and connected beforehand.
1391 *
1392 * \retval 0 on success, otherwise error code.
1393 */
Yanray Wangb4ef9a22023-03-16 12:04:49 +08001394#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) && \
1395 defined(MBEDTLS_CERTS_C) && \
1396 defined(MBEDTLS_ENTROPY_C) && \
1397 defined(MBEDTLS_CTR_DRBG_C) && \
1398 (defined(MBEDTLS_SSL_RENEGOTIATION) || \
1399 defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH))
1400static int exchange_data(mbedtls_ssl_context *ssl_1,
1401 mbedtls_ssl_context *ssl_2)
Yanray Wangbd56b032023-03-14 14:36:48 +08001402{
Yanray Wang74df2012023-03-16 12:15:49 +08001403 return mbedtls_test_ssl_exchange_data(ssl_1, 256, 1,
1404 ssl_2, 256, 1);
Yanray Wangbd56b032023-03-14 14:36:48 +08001405}
Yanray Wangb4ef9a22023-03-16 12:04:49 +08001406#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED && MBEDTLS_CERTS_C &&
1407 MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C &&
1408 (MBEDTLS_SSL_RENEGOTIATION ||
1409 MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) */
Yanray Wangbd56b032023-03-14 14:36:48 +08001410
1411#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) && \
Yanray Wangb4ef9a22023-03-16 12:04:49 +08001412 defined(MBEDTLS_CERTS_C) && \
1413 defined(MBEDTLS_ENTROPY_C) && \
Yanray Wangbd56b032023-03-14 14:36:48 +08001414 defined(MBEDTLS_CTR_DRBG_C)
1415void mbedtls_test_ssl_perform_handshake(
1416 mbedtls_test_handshake_test_options *options)
1417{
1418 /* forced_ciphersuite needs to last until the end of the handshake */
1419 int forced_ciphersuite[2];
1420 enum { BUFFSIZE = 17000 };
1421 mbedtls_test_ssl_endpoint client, server;
1422#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
1423 const char *psk_identity = "foo";
1424#endif
1425#if defined(MBEDTLS_TIMING_C)
1426 mbedtls_timing_delay_context timer_client, timer_server;
1427#endif
1428#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1429 unsigned char *context_buf = NULL;
1430 size_t context_buf_len;
1431#endif
1432#if defined(MBEDTLS_SSL_RENEGOTIATION)
1433 int ret = -1;
1434#endif
1435 int expected_handshake_result = 0;
1436
1437 USE_PSA_INIT();
1438 mbedtls_platform_zeroize(&client, sizeof(client));
1439 mbedtls_platform_zeroize(&server, sizeof(server));
1440
1441 mbedtls_test_ssl_message_queue server_queue, client_queue;
1442 mbedtls_test_message_socket_context server_context, client_context;
1443 mbedtls_test_message_socket_init(&server_context);
1444 mbedtls_test_message_socket_init(&client_context);
1445
1446 /* Client side */
1447 if (options->dtls != 0) {
1448 TEST_ASSERT(mbedtls_test_ssl_endpoint_init(&client,
1449 MBEDTLS_SSL_IS_CLIENT,
1450 options->pk_alg,
1451 &client_context,
1452 &client_queue,
1453 &server_queue, NULL) == 0);
1454#if defined(MBEDTLS_TIMING_C)
1455 mbedtls_ssl_set_timer_cb(&client.ssl, &timer_client,
1456 mbedtls_timing_set_delay,
1457 mbedtls_timing_get_delay);
1458#endif
1459 } else {
1460 TEST_ASSERT(mbedtls_test_ssl_endpoint_init(&client,
1461 MBEDTLS_SSL_IS_CLIENT,
1462 options->pk_alg, NULL, NULL,
1463 NULL, NULL) == 0);
1464 }
1465
1466 if (options->client_min_version != TEST_SSL_MINOR_VERSION_NONE) {
1467 mbedtls_ssl_conf_min_version(&client.conf, MBEDTLS_SSL_MAJOR_VERSION_3,
1468 options->client_min_version);
1469 }
1470
1471 if (options->client_max_version != TEST_SSL_MINOR_VERSION_NONE) {
1472 mbedtls_ssl_conf_max_version(&client.conf, MBEDTLS_SSL_MAJOR_VERSION_3,
1473 options->client_max_version);
1474 }
1475
1476 if (strlen(options->cipher) > 0) {
1477 set_ciphersuite(&client.conf, options->cipher, forced_ciphersuite);
1478 }
1479
1480#if defined(MBEDTLS_DEBUG_C)
1481 if (options->cli_log_fun) {
1482 mbedtls_debug_set_threshold(4);
1483 mbedtls_ssl_conf_dbg(&client.conf, options->cli_log_fun,
1484 options->cli_log_obj);
1485 }
1486#endif
1487
1488 /* Server side */
1489 if (options->dtls != 0) {
1490 TEST_ASSERT(mbedtls_test_ssl_endpoint_init(&server,
1491 MBEDTLS_SSL_IS_SERVER,
1492 options->pk_alg,
1493 &server_context,
1494 &server_queue,
1495 &client_queue, NULL) == 0);
1496#if defined(MBEDTLS_TIMING_C)
1497 mbedtls_ssl_set_timer_cb(&server.ssl, &timer_server,
1498 mbedtls_timing_set_delay,
1499 mbedtls_timing_get_delay);
1500#endif
1501 } else {
1502 TEST_ASSERT(mbedtls_test_ssl_endpoint_init(&server,
1503 MBEDTLS_SSL_IS_SERVER,
1504 options->pk_alg, NULL, NULL,
1505 NULL, NULL) == 0);
1506 }
1507
1508 mbedtls_ssl_conf_authmode(&server.conf, options->srv_auth_mode);
1509
1510 if (options->server_min_version != TEST_SSL_MINOR_VERSION_NONE) {
1511 mbedtls_ssl_conf_min_version(&server.conf, MBEDTLS_SSL_MAJOR_VERSION_3,
1512 options->server_min_version);
1513 }
1514
1515 if (options->server_max_version != TEST_SSL_MINOR_VERSION_NONE) {
1516 mbedtls_ssl_conf_max_version(&server.conf, MBEDTLS_SSL_MAJOR_VERSION_3,
1517 options->server_max_version);
1518 }
1519
1520#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1521 TEST_ASSERT(mbedtls_ssl_conf_max_frag_len(&(server.conf),
1522 (unsigned char) options->mfl)
1523 == 0);
1524 TEST_ASSERT(mbedtls_ssl_conf_max_frag_len(&(client.conf),
1525 (unsigned char) options->mfl)
1526 == 0);
1527#else
1528 TEST_ASSERT(MBEDTLS_SSL_MAX_FRAG_LEN_NONE == options->mfl);
1529#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
1530
1531#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
1532 if (options->psk_str != NULL && options->psk_str->len > 0) {
1533 TEST_ASSERT(mbedtls_ssl_conf_psk(
1534 &client.conf, options->psk_str->x,
1535 options->psk_str->len,
1536 (const unsigned char *) psk_identity,
1537 strlen(psk_identity)) == 0);
1538
1539 TEST_ASSERT(mbedtls_ssl_conf_psk(
1540 &server.conf, options->psk_str->x,
1541 options->psk_str->len,
1542 (const unsigned char *) psk_identity,
1543 strlen(psk_identity)) == 0);
1544
1545 mbedtls_ssl_conf_psk_cb(&server.conf, psk_dummy_callback, NULL);
1546 }
1547#endif
1548#if defined(MBEDTLS_SSL_RENEGOTIATION)
1549 if (options->renegotiate) {
1550 mbedtls_ssl_conf_renegotiation(&(server.conf),
1551 MBEDTLS_SSL_RENEGOTIATION_ENABLED);
1552 mbedtls_ssl_conf_renegotiation(&(client.conf),
1553 MBEDTLS_SSL_RENEGOTIATION_ENABLED);
1554
1555 mbedtls_ssl_conf_legacy_renegotiation(&(server.conf),
1556 options->legacy_renegotiation);
1557 mbedtls_ssl_conf_legacy_renegotiation(&(client.conf),
1558 options->legacy_renegotiation);
1559 }
1560#endif /* MBEDTLS_SSL_RENEGOTIATION */
1561
1562#if defined(MBEDTLS_DEBUG_C)
1563 if (options->srv_log_fun) {
1564 mbedtls_debug_set_threshold(4);
1565 mbedtls_ssl_conf_dbg(&server.conf, options->srv_log_fun,
1566 options->srv_log_obj);
1567 }
1568#endif
1569
1570 TEST_ASSERT(mbedtls_test_mock_socket_connect(&(client.socket),
1571 &(server.socket),
1572 BUFFSIZE) == 0);
1573
1574#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1575 if (options->resize_buffers != 0) {
1576 /* Ensure that the buffer sizes are appropriate before resizes */
1577 TEST_ASSERT(client.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN);
1578 TEST_ASSERT(client.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN);
1579 TEST_ASSERT(server.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN);
1580 TEST_ASSERT(server.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN);
1581 }
1582#endif
1583
1584 if (options->expected_negotiated_version == TEST_SSL_MINOR_VERSION_NONE) {
1585 expected_handshake_result = MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION;
1586 }
1587
1588 TEST_ASSERT(mbedtls_test_move_handshake_to_state(
1589 &(client.ssl), &(server.ssl), MBEDTLS_SSL_HANDSHAKE_OVER)
1590 == expected_handshake_result);
1591
1592 if (expected_handshake_result != 0) {
1593 /* Connection will have failed by this point, skip to cleanup */
1594 goto exit;
1595 }
1596
1597 TEST_ASSERT(client.ssl.state == MBEDTLS_SSL_HANDSHAKE_OVER);
1598 TEST_ASSERT(server.ssl.state == MBEDTLS_SSL_HANDSHAKE_OVER);
1599
1600 /* Check that we agree on the version... */
1601 TEST_ASSERT(client.ssl.minor_ver == server.ssl.minor_ver);
1602
1603 /* And check that the version negotiated is the expected one. */
1604 TEST_EQUAL(client.ssl.minor_ver, options->expected_negotiated_version);
1605
1606#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1607 if (options->resize_buffers != 0) {
1608 if (options->expected_negotiated_version != MBEDTLS_SSL_MINOR_VERSION_0 &&
1609 options->expected_negotiated_version != MBEDTLS_SSL_MINOR_VERSION_1) {
1610 /* A server, when using DTLS, might delay a buffer resize to happen
1611 * after it receives a message, so we force it. */
1612 TEST_ASSERT(exchange_data(&(client.ssl), &(server.ssl)) == 0);
1613
1614 TEST_ASSERT(client.ssl.out_buf_len ==
1615 mbedtls_ssl_get_output_buflen(&client.ssl));
1616 TEST_ASSERT(client.ssl.in_buf_len ==
1617 mbedtls_ssl_get_input_buflen(&client.ssl));
1618 TEST_ASSERT(server.ssl.out_buf_len ==
1619 mbedtls_ssl_get_output_buflen(&server.ssl));
1620 TEST_ASSERT(server.ssl.in_buf_len ==
1621 mbedtls_ssl_get_input_buflen(&server.ssl));
1622 }
1623 }
1624#endif
1625
1626 if (options->cli_msg_len != 0 || options->srv_msg_len != 0) {
1627 /* Start data exchanging test */
Yanray Wang74df2012023-03-16 12:15:49 +08001628 TEST_ASSERT(mbedtls_test_ssl_exchange_data(
1629 &(client.ssl), options->cli_msg_len,
1630 options->expected_cli_fragments,
1631 &(server.ssl), options->srv_msg_len,
1632 options->expected_srv_fragments)
Yanray Wangbd56b032023-03-14 14:36:48 +08001633 == 0);
1634 }
1635#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1636 if (options->serialize == 1) {
1637 TEST_ASSERT(options->dtls == 1);
1638
1639 TEST_ASSERT(mbedtls_ssl_context_save(&(server.ssl), NULL,
1640 0, &context_buf_len)
1641 == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL);
1642
1643 context_buf = mbedtls_calloc(1, context_buf_len);
1644 TEST_ASSERT(context_buf != NULL);
1645
1646 TEST_ASSERT(mbedtls_ssl_context_save(&(server.ssl), context_buf,
1647 context_buf_len,
1648 &context_buf_len)
1649 == 0);
1650
1651 mbedtls_ssl_free(&(server.ssl));
1652 mbedtls_ssl_init(&(server.ssl));
1653
1654 TEST_ASSERT(mbedtls_ssl_setup(&(server.ssl), &(server.conf)) == 0);
1655
1656 mbedtls_ssl_set_bio(&(server.ssl), &server_context,
1657 mbedtls_test_mock_tcp_send_msg,
1658 mbedtls_test_mock_tcp_recv_msg,
1659 NULL);
1660
1661#if defined(MBEDTLS_TIMING_C)
1662 mbedtls_ssl_set_timer_cb(&server.ssl, &timer_server,
1663 mbedtls_timing_set_delay,
1664 mbedtls_timing_get_delay);
1665#endif
1666#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1667 if (options->resize_buffers != 0) {
1668 /* Ensure that the buffer sizes are appropriate before resizes */
1669 TEST_ASSERT(server.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN);
1670 TEST_ASSERT(server.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN);
1671 }
1672#endif
1673 TEST_ASSERT(mbedtls_ssl_context_load(&(server.ssl), context_buf,
1674 context_buf_len) == 0);
1675
1676#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1677 /* Validate buffer sizes after context deserialization */
1678 if (options->resize_buffers != 0) {
1679 TEST_ASSERT(server.ssl.out_buf_len ==
1680 mbedtls_ssl_get_output_buflen(&server.ssl));
1681 TEST_ASSERT(server.ssl.in_buf_len ==
1682 mbedtls_ssl_get_input_buflen(&server.ssl));
1683 }
1684#endif
1685 /* Retest writing/reading */
1686 if (options->cli_msg_len != 0 || options->srv_msg_len != 0) {
Yanray Wang74df2012023-03-16 12:15:49 +08001687 TEST_ASSERT(mbedtls_test_ssl_exchange_data(
1688 &(client.ssl), options->cli_msg_len,
Yanray Wangbd56b032023-03-14 14:36:48 +08001689 options->expected_cli_fragments,
Yanray Wang74df2012023-03-16 12:15:49 +08001690 &(server.ssl), options->srv_msg_len,
Yanray Wangbd56b032023-03-14 14:36:48 +08001691 options->expected_srv_fragments)
1692 == 0);
1693 }
1694 }
1695#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
1696
1697#if defined(MBEDTLS_SSL_RENEGOTIATION)
1698 if (options->renegotiate) {
1699 /* Start test with renegotiation */
1700 TEST_ASSERT(server.ssl.renego_status ==
1701 MBEDTLS_SSL_INITIAL_HANDSHAKE);
1702 TEST_ASSERT(client.ssl.renego_status ==
1703 MBEDTLS_SSL_INITIAL_HANDSHAKE);
1704
1705 /* After calling this function for the server, it only sends a handshake
1706 * request. All renegotiation should happen during data exchanging */
1707 TEST_ASSERT(mbedtls_ssl_renegotiate(&(server.ssl)) == 0);
1708 TEST_ASSERT(server.ssl.renego_status ==
1709 MBEDTLS_SSL_RENEGOTIATION_PENDING);
1710 TEST_ASSERT(client.ssl.renego_status ==
1711 MBEDTLS_SSL_INITIAL_HANDSHAKE);
1712
1713 TEST_ASSERT(exchange_data(&(client.ssl), &(server.ssl)) == 0);
1714 TEST_ASSERT(server.ssl.renego_status ==
1715 MBEDTLS_SSL_RENEGOTIATION_DONE);
1716 TEST_ASSERT(client.ssl.renego_status ==
1717 MBEDTLS_SSL_RENEGOTIATION_DONE);
1718
1719 /* After calling mbedtls_ssl_renegotiate for the client,
1720 * all renegotiation should happen inside this function.
1721 * However in this test, we cannot perform simultaneous communication
1722 * between client and server so this function will return waiting error
1723 * on the socket. All rest of renegotiation should happen
1724 * during data exchanging */
1725 ret = mbedtls_ssl_renegotiate(&(client.ssl));
1726#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1727 if (options->resize_buffers != 0) {
1728 /* Ensure that the buffer sizes are appropriate before resizes */
1729 TEST_ASSERT(client.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN);
1730 TEST_ASSERT(client.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN);
1731 }
1732#endif
1733 TEST_ASSERT(ret == 0 ||
1734 ret == MBEDTLS_ERR_SSL_WANT_READ ||
1735 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
1736 TEST_ASSERT(server.ssl.renego_status ==
1737 MBEDTLS_SSL_RENEGOTIATION_DONE);
1738 TEST_ASSERT(client.ssl.renego_status ==
1739 MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS);
1740
1741 TEST_ASSERT(exchange_data(&(client.ssl), &(server.ssl)) == 0);
1742 TEST_ASSERT(server.ssl.renego_status ==
1743 MBEDTLS_SSL_RENEGOTIATION_DONE);
1744 TEST_ASSERT(client.ssl.renego_status ==
1745 MBEDTLS_SSL_RENEGOTIATION_DONE);
1746#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1747 /* Validate buffer sizes after renegotiation */
1748 if (options->resize_buffers != 0) {
1749 TEST_ASSERT(client.ssl.out_buf_len ==
1750 mbedtls_ssl_get_output_buflen(&client.ssl));
1751 TEST_ASSERT(client.ssl.in_buf_len ==
1752 mbedtls_ssl_get_input_buflen(&client.ssl));
1753 TEST_ASSERT(server.ssl.out_buf_len ==
1754 mbedtls_ssl_get_output_buflen(&server.ssl));
1755 TEST_ASSERT(server.ssl.in_buf_len ==
1756 mbedtls_ssl_get_input_buflen(&server.ssl));
1757 }
1758#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
1759 }
1760#endif /* MBEDTLS_SSL_RENEGOTIATION */
1761
1762exit:
1763 mbedtls_test_ssl_endpoint_free(&client,
1764 options->dtls != 0 ? &client_context : NULL);
1765 mbedtls_test_ssl_endpoint_free(&server,
1766 options->dtls != 0 ? &server_context : NULL);
1767#if defined(MBEDTLS_DEBUG_C)
1768 if (options->cli_log_fun || options->srv_log_fun) {
1769 mbedtls_debug_set_threshold(0);
1770 }
1771#endif
1772#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1773 if (context_buf != NULL) {
1774 mbedtls_free(context_buf);
1775 }
1776#endif
1777}
Yanray Wang1ef77c02023-03-14 16:59:00 +08001778#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED && MBEDTLS_CERTS_C &&
1779 MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */
Yanray Wangbd56b032023-03-14 14:36:48 +08001780
Yanray Wang4323e452023-03-14 16:52:06 +08001781#endif /* MBEDTLS_SSL_TLS_C */