blob: 2fea014ac951ca833f222eba0ebc9e0a4095ed93 [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
Yanray Wangbd56b032023-03-14 14:36:48 +08001198int mbedtls_test_ssl_populate_session(mbedtls_ssl_session *session,
1199 int ticket_len,
1200 const char *crt_file)
1201{
1202#if defined(MBEDTLS_HAVE_TIME)
1203 session->start = mbedtls_time(NULL) - 42;
1204#endif
1205 session->ciphersuite = 0xabcd;
1206 session->compression = 1;
1207 session->id_len = sizeof(session->id);
1208 memset(session->id, 66, session->id_len);
1209 memset(session->master, 17, sizeof(session->master));
1210
1211#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) && \
1212 defined(MBEDTLS_CERTS_C) && \
1213 defined(MBEDTLS_FS_IO)
1214 if (strlen(crt_file) != 0) {
1215 mbedtls_x509_crt tmp_crt;
1216 int ret;
1217
1218 mbedtls_x509_crt_init(&tmp_crt);
1219 ret = mbedtls_x509_crt_parse_file(&tmp_crt, crt_file);
1220 if (ret != 0) {
1221 return ret;
1222 }
1223
1224#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1225 /* Move temporary CRT. */
1226 session->peer_cert = mbedtls_calloc(1, sizeof(*session->peer_cert));
1227 if (session->peer_cert == NULL) {
1228 return -1;
1229 }
1230 *session->peer_cert = tmp_crt;
1231 memset(&tmp_crt, 0, sizeof(tmp_crt));
1232#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
1233 /* Calculate digest of temporary CRT. */
1234 session->peer_cert_digest =
1235 mbedtls_calloc(1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN);
1236 if (session->peer_cert_digest == NULL) {
1237 return -1;
1238 }
1239 ret = mbedtls_md(mbedtls_md_info_from_type(
1240 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE),
1241 tmp_crt.raw.p, tmp_crt.raw.len,
1242 session->peer_cert_digest);
1243 if (ret != 0) {
1244 return ret;
1245 }
1246 session->peer_cert_digest_type =
1247 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
1248 session->peer_cert_digest_len =
1249 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
1250#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
1251
1252 mbedtls_x509_crt_free(&tmp_crt);
1253 }
1254#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED && MBEDTLS_CERTS_C && MBEDTLS_FS_IO */
1255 (void) crt_file;
1256#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED && MBEDTLS_CERTS_C && MBEDTLS_FS_IO */
1257 session->verify_result = 0xdeadbeef;
1258
1259#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
1260 if (ticket_len != 0) {
1261 session->ticket = mbedtls_calloc(1, ticket_len);
1262 if (session->ticket == NULL) {
1263 return -1;
1264 }
1265 memset(session->ticket, 33, ticket_len);
1266 }
1267 session->ticket_len = ticket_len;
1268 session->ticket_lifetime = 86401;
1269#else
1270 (void) ticket_len;
1271#endif
1272
1273#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1274 session->mfl_code = 1;
1275#endif
1276#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1277 session->trunc_hmac = 1;
1278#endif
1279#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1280 session->encrypt_then_mac = 1;
1281#endif
1282
1283 return 0;
1284}
1285
Yanray Wang74df2012023-03-16 12:15:49 +08001286int mbedtls_test_ssl_exchange_data(
1287 mbedtls_ssl_context *ssl_1,
1288 int msg_len_1, const int expected_fragments_1,
1289 mbedtls_ssl_context *ssl_2,
1290 int msg_len_2, const int expected_fragments_2)
Yanray Wangbd56b032023-03-14 14:36:48 +08001291{
1292 unsigned char *msg_buf_1 = malloc(msg_len_1);
1293 unsigned char *msg_buf_2 = malloc(msg_len_2);
1294 unsigned char *in_buf_1 = malloc(msg_len_2);
1295 unsigned char *in_buf_2 = malloc(msg_len_1);
1296 int msg_type, ret = -1;
1297
1298 /* Perform this test with two message types. At first use a message
1299 * consisting of only 0x00 for the client and only 0xFF for the server.
1300 * At the second time use message with generated data */
1301 for (msg_type = 0; msg_type < 2; msg_type++) {
1302 int written_1 = 0;
1303 int written_2 = 0;
1304 int read_1 = 0;
1305 int read_2 = 0;
1306 int fragments_1 = 0;
1307 int fragments_2 = 0;
1308
1309 if (msg_type == 0) {
1310 memset(msg_buf_1, 0x00, msg_len_1);
1311 memset(msg_buf_2, 0xff, msg_len_2);
1312 } else {
1313 int i, j = 0;
1314 for (i = 0; i < msg_len_1; i++) {
1315 msg_buf_1[i] = j++ & 0xFF;
1316 }
1317 for (i = 0; i < msg_len_2; i++) {
1318 msg_buf_2[i] = (j -= 5) & 0xFF;
1319 }
1320 }
1321
1322 while (read_1 < msg_len_2 || read_2 < msg_len_1) {
1323 /* ssl_1 sending */
1324 if (msg_len_1 > written_1) {
1325 ret = mbedtls_ssl_write_fragment(ssl_1, msg_buf_1,
1326 msg_len_1, &written_1,
1327 expected_fragments_1);
1328 if (expected_fragments_1 == 0) {
1329 /* This error is expected when the message is too large and
1330 * cannot be fragmented */
1331 TEST_ASSERT(ret == MBEDTLS_ERR_SSL_BAD_INPUT_DATA);
1332 msg_len_1 = 0;
1333 } else {
1334 TEST_ASSERT(ret == 0);
1335 }
1336 }
1337
1338 /* ssl_2 sending */
1339 if (msg_len_2 > written_2) {
1340 ret = mbedtls_ssl_write_fragment(ssl_2, msg_buf_2,
1341 msg_len_2, &written_2,
1342 expected_fragments_2);
1343 if (expected_fragments_2 == 0) {
1344 /* This error is expected when the message is too large and
1345 * cannot be fragmented */
1346 TEST_ASSERT(ret == MBEDTLS_ERR_SSL_BAD_INPUT_DATA);
1347 msg_len_2 = 0;
1348 } else {
1349 TEST_ASSERT(ret == 0);
1350 }
1351 }
1352
1353 /* ssl_1 reading */
1354 if (read_1 < msg_len_2) {
1355 ret = mbedtls_ssl_read_fragment(ssl_1, in_buf_1,
1356 msg_len_2, &read_1,
1357 &fragments_2,
1358 expected_fragments_2);
1359 TEST_ASSERT(ret == 0);
1360 }
1361
1362 /* ssl_2 reading */
1363 if (read_2 < msg_len_1) {
1364 ret = mbedtls_ssl_read_fragment(ssl_2, in_buf_2,
1365 msg_len_1, &read_2,
1366 &fragments_1,
1367 expected_fragments_1);
1368 TEST_ASSERT(ret == 0);
1369 }
1370 }
1371
1372 ret = -1;
1373 TEST_ASSERT(0 == memcmp(msg_buf_1, in_buf_2, msg_len_1));
1374 TEST_ASSERT(0 == memcmp(msg_buf_2, in_buf_1, msg_len_2));
1375 TEST_ASSERT(fragments_1 == expected_fragments_1);
1376 TEST_ASSERT(fragments_2 == expected_fragments_2);
1377 }
1378
1379 ret = 0;
1380
1381exit:
1382 free(msg_buf_1);
1383 free(in_buf_1);
1384 free(msg_buf_2);
1385 free(in_buf_2);
1386
1387 return ret;
1388}
1389
1390/*
1391 * Perform data exchanging between \p ssl_1 and \p ssl_2. Both of endpoints
1392 * must be initialized and connected beforehand.
1393 *
1394 * \retval 0 on success, otherwise error code.
1395 */
Yanray Wangb4ef9a22023-03-16 12:04:49 +08001396#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) && \
1397 defined(MBEDTLS_CERTS_C) && \
1398 defined(MBEDTLS_ENTROPY_C) && \
1399 defined(MBEDTLS_CTR_DRBG_C) && \
1400 (defined(MBEDTLS_SSL_RENEGOTIATION) || \
1401 defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH))
1402static int exchange_data(mbedtls_ssl_context *ssl_1,
1403 mbedtls_ssl_context *ssl_2)
Yanray Wangbd56b032023-03-14 14:36:48 +08001404{
Yanray Wang74df2012023-03-16 12:15:49 +08001405 return mbedtls_test_ssl_exchange_data(ssl_1, 256, 1,
1406 ssl_2, 256, 1);
Yanray Wangbd56b032023-03-14 14:36:48 +08001407}
Yanray Wangb4ef9a22023-03-16 12:04:49 +08001408#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED && MBEDTLS_CERTS_C &&
1409 MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C &&
1410 (MBEDTLS_SSL_RENEGOTIATION ||
1411 MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) */
Yanray Wangbd56b032023-03-14 14:36:48 +08001412
1413#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) && \
Yanray Wangb4ef9a22023-03-16 12:04:49 +08001414 defined(MBEDTLS_CERTS_C) && \
1415 defined(MBEDTLS_ENTROPY_C) && \
Yanray Wangbd56b032023-03-14 14:36:48 +08001416 defined(MBEDTLS_CTR_DRBG_C)
1417void mbedtls_test_ssl_perform_handshake(
1418 mbedtls_test_handshake_test_options *options)
1419{
1420 /* forced_ciphersuite needs to last until the end of the handshake */
1421 int forced_ciphersuite[2];
1422 enum { BUFFSIZE = 17000 };
1423 mbedtls_test_ssl_endpoint client, server;
1424#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
1425 const char *psk_identity = "foo";
1426#endif
1427#if defined(MBEDTLS_TIMING_C)
1428 mbedtls_timing_delay_context timer_client, timer_server;
1429#endif
1430#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1431 unsigned char *context_buf = NULL;
1432 size_t context_buf_len;
1433#endif
1434#if defined(MBEDTLS_SSL_RENEGOTIATION)
1435 int ret = -1;
1436#endif
1437 int expected_handshake_result = 0;
1438
1439 USE_PSA_INIT();
1440 mbedtls_platform_zeroize(&client, sizeof(client));
1441 mbedtls_platform_zeroize(&server, sizeof(server));
1442
1443 mbedtls_test_ssl_message_queue server_queue, client_queue;
1444 mbedtls_test_message_socket_context server_context, client_context;
1445 mbedtls_test_message_socket_init(&server_context);
1446 mbedtls_test_message_socket_init(&client_context);
1447
1448 /* Client side */
1449 if (options->dtls != 0) {
1450 TEST_ASSERT(mbedtls_test_ssl_endpoint_init(&client,
1451 MBEDTLS_SSL_IS_CLIENT,
1452 options->pk_alg,
1453 &client_context,
1454 &client_queue,
1455 &server_queue, NULL) == 0);
1456#if defined(MBEDTLS_TIMING_C)
1457 mbedtls_ssl_set_timer_cb(&client.ssl, &timer_client,
1458 mbedtls_timing_set_delay,
1459 mbedtls_timing_get_delay);
1460#endif
1461 } else {
1462 TEST_ASSERT(mbedtls_test_ssl_endpoint_init(&client,
1463 MBEDTLS_SSL_IS_CLIENT,
1464 options->pk_alg, NULL, NULL,
1465 NULL, NULL) == 0);
1466 }
1467
1468 if (options->client_min_version != TEST_SSL_MINOR_VERSION_NONE) {
1469 mbedtls_ssl_conf_min_version(&client.conf, MBEDTLS_SSL_MAJOR_VERSION_3,
1470 options->client_min_version);
1471 }
1472
1473 if (options->client_max_version != TEST_SSL_MINOR_VERSION_NONE) {
1474 mbedtls_ssl_conf_max_version(&client.conf, MBEDTLS_SSL_MAJOR_VERSION_3,
1475 options->client_max_version);
1476 }
1477
1478 if (strlen(options->cipher) > 0) {
1479 set_ciphersuite(&client.conf, options->cipher, forced_ciphersuite);
1480 }
1481
1482#if defined(MBEDTLS_DEBUG_C)
1483 if (options->cli_log_fun) {
1484 mbedtls_debug_set_threshold(4);
1485 mbedtls_ssl_conf_dbg(&client.conf, options->cli_log_fun,
1486 options->cli_log_obj);
1487 }
1488#endif
1489
1490 /* Server side */
1491 if (options->dtls != 0) {
1492 TEST_ASSERT(mbedtls_test_ssl_endpoint_init(&server,
1493 MBEDTLS_SSL_IS_SERVER,
1494 options->pk_alg,
1495 &server_context,
1496 &server_queue,
1497 &client_queue, NULL) == 0);
1498#if defined(MBEDTLS_TIMING_C)
1499 mbedtls_ssl_set_timer_cb(&server.ssl, &timer_server,
1500 mbedtls_timing_set_delay,
1501 mbedtls_timing_get_delay);
1502#endif
1503 } else {
1504 TEST_ASSERT(mbedtls_test_ssl_endpoint_init(&server,
1505 MBEDTLS_SSL_IS_SERVER,
1506 options->pk_alg, NULL, NULL,
1507 NULL, NULL) == 0);
1508 }
1509
1510 mbedtls_ssl_conf_authmode(&server.conf, options->srv_auth_mode);
1511
1512 if (options->server_min_version != TEST_SSL_MINOR_VERSION_NONE) {
1513 mbedtls_ssl_conf_min_version(&server.conf, MBEDTLS_SSL_MAJOR_VERSION_3,
1514 options->server_min_version);
1515 }
1516
1517 if (options->server_max_version != TEST_SSL_MINOR_VERSION_NONE) {
1518 mbedtls_ssl_conf_max_version(&server.conf, MBEDTLS_SSL_MAJOR_VERSION_3,
1519 options->server_max_version);
1520 }
1521
1522#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1523 TEST_ASSERT(mbedtls_ssl_conf_max_frag_len(&(server.conf),
1524 (unsigned char) options->mfl)
1525 == 0);
1526 TEST_ASSERT(mbedtls_ssl_conf_max_frag_len(&(client.conf),
1527 (unsigned char) options->mfl)
1528 == 0);
1529#else
1530 TEST_ASSERT(MBEDTLS_SSL_MAX_FRAG_LEN_NONE == options->mfl);
1531#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
1532
1533#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
1534 if (options->psk_str != NULL && options->psk_str->len > 0) {
1535 TEST_ASSERT(mbedtls_ssl_conf_psk(
1536 &client.conf, options->psk_str->x,
1537 options->psk_str->len,
1538 (const unsigned char *) psk_identity,
1539 strlen(psk_identity)) == 0);
1540
1541 TEST_ASSERT(mbedtls_ssl_conf_psk(
1542 &server.conf, options->psk_str->x,
1543 options->psk_str->len,
1544 (const unsigned char *) psk_identity,
1545 strlen(psk_identity)) == 0);
1546
1547 mbedtls_ssl_conf_psk_cb(&server.conf, psk_dummy_callback, NULL);
1548 }
1549#endif
1550#if defined(MBEDTLS_SSL_RENEGOTIATION)
1551 if (options->renegotiate) {
1552 mbedtls_ssl_conf_renegotiation(&(server.conf),
1553 MBEDTLS_SSL_RENEGOTIATION_ENABLED);
1554 mbedtls_ssl_conf_renegotiation(&(client.conf),
1555 MBEDTLS_SSL_RENEGOTIATION_ENABLED);
1556
1557 mbedtls_ssl_conf_legacy_renegotiation(&(server.conf),
1558 options->legacy_renegotiation);
1559 mbedtls_ssl_conf_legacy_renegotiation(&(client.conf),
1560 options->legacy_renegotiation);
1561 }
1562#endif /* MBEDTLS_SSL_RENEGOTIATION */
1563
1564#if defined(MBEDTLS_DEBUG_C)
1565 if (options->srv_log_fun) {
1566 mbedtls_debug_set_threshold(4);
1567 mbedtls_ssl_conf_dbg(&server.conf, options->srv_log_fun,
1568 options->srv_log_obj);
1569 }
1570#endif
1571
1572 TEST_ASSERT(mbedtls_test_mock_socket_connect(&(client.socket),
1573 &(server.socket),
1574 BUFFSIZE) == 0);
1575
1576#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1577 if (options->resize_buffers != 0) {
1578 /* Ensure that the buffer sizes are appropriate before resizes */
1579 TEST_ASSERT(client.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN);
1580 TEST_ASSERT(client.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN);
1581 TEST_ASSERT(server.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN);
1582 TEST_ASSERT(server.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN);
1583 }
1584#endif
1585
1586 if (options->expected_negotiated_version == TEST_SSL_MINOR_VERSION_NONE) {
1587 expected_handshake_result = MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION;
1588 }
1589
1590 TEST_ASSERT(mbedtls_test_move_handshake_to_state(
1591 &(client.ssl), &(server.ssl), MBEDTLS_SSL_HANDSHAKE_OVER)
1592 == expected_handshake_result);
1593
1594 if (expected_handshake_result != 0) {
1595 /* Connection will have failed by this point, skip to cleanup */
1596 goto exit;
1597 }
1598
1599 TEST_ASSERT(client.ssl.state == MBEDTLS_SSL_HANDSHAKE_OVER);
1600 TEST_ASSERT(server.ssl.state == MBEDTLS_SSL_HANDSHAKE_OVER);
1601
1602 /* Check that we agree on the version... */
1603 TEST_ASSERT(client.ssl.minor_ver == server.ssl.minor_ver);
1604
1605 /* And check that the version negotiated is the expected one. */
1606 TEST_EQUAL(client.ssl.minor_ver, options->expected_negotiated_version);
1607
1608#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1609 if (options->resize_buffers != 0) {
1610 if (options->expected_negotiated_version != MBEDTLS_SSL_MINOR_VERSION_0 &&
1611 options->expected_negotiated_version != MBEDTLS_SSL_MINOR_VERSION_1) {
1612 /* A server, when using DTLS, might delay a buffer resize to happen
1613 * after it receives a message, so we force it. */
1614 TEST_ASSERT(exchange_data(&(client.ssl), &(server.ssl)) == 0);
1615
1616 TEST_ASSERT(client.ssl.out_buf_len ==
1617 mbedtls_ssl_get_output_buflen(&client.ssl));
1618 TEST_ASSERT(client.ssl.in_buf_len ==
1619 mbedtls_ssl_get_input_buflen(&client.ssl));
1620 TEST_ASSERT(server.ssl.out_buf_len ==
1621 mbedtls_ssl_get_output_buflen(&server.ssl));
1622 TEST_ASSERT(server.ssl.in_buf_len ==
1623 mbedtls_ssl_get_input_buflen(&server.ssl));
1624 }
1625 }
1626#endif
1627
1628 if (options->cli_msg_len != 0 || options->srv_msg_len != 0) {
1629 /* Start data exchanging test */
Yanray Wang74df2012023-03-16 12:15:49 +08001630 TEST_ASSERT(mbedtls_test_ssl_exchange_data(
1631 &(client.ssl), options->cli_msg_len,
1632 options->expected_cli_fragments,
1633 &(server.ssl), options->srv_msg_len,
1634 options->expected_srv_fragments)
Yanray Wangbd56b032023-03-14 14:36:48 +08001635 == 0);
1636 }
1637#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1638 if (options->serialize == 1) {
1639 TEST_ASSERT(options->dtls == 1);
1640
1641 TEST_ASSERT(mbedtls_ssl_context_save(&(server.ssl), NULL,
1642 0, &context_buf_len)
1643 == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL);
1644
1645 context_buf = mbedtls_calloc(1, context_buf_len);
1646 TEST_ASSERT(context_buf != NULL);
1647
1648 TEST_ASSERT(mbedtls_ssl_context_save(&(server.ssl), context_buf,
1649 context_buf_len,
1650 &context_buf_len)
1651 == 0);
1652
1653 mbedtls_ssl_free(&(server.ssl));
1654 mbedtls_ssl_init(&(server.ssl));
1655
1656 TEST_ASSERT(mbedtls_ssl_setup(&(server.ssl), &(server.conf)) == 0);
1657
1658 mbedtls_ssl_set_bio(&(server.ssl), &server_context,
1659 mbedtls_test_mock_tcp_send_msg,
1660 mbedtls_test_mock_tcp_recv_msg,
1661 NULL);
1662
1663#if defined(MBEDTLS_TIMING_C)
1664 mbedtls_ssl_set_timer_cb(&server.ssl, &timer_server,
1665 mbedtls_timing_set_delay,
1666 mbedtls_timing_get_delay);
1667#endif
1668#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1669 if (options->resize_buffers != 0) {
1670 /* Ensure that the buffer sizes are appropriate before resizes */
1671 TEST_ASSERT(server.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN);
1672 TEST_ASSERT(server.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN);
1673 }
1674#endif
1675 TEST_ASSERT(mbedtls_ssl_context_load(&(server.ssl), context_buf,
1676 context_buf_len) == 0);
1677
1678#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1679 /* Validate buffer sizes after context deserialization */
1680 if (options->resize_buffers != 0) {
1681 TEST_ASSERT(server.ssl.out_buf_len ==
1682 mbedtls_ssl_get_output_buflen(&server.ssl));
1683 TEST_ASSERT(server.ssl.in_buf_len ==
1684 mbedtls_ssl_get_input_buflen(&server.ssl));
1685 }
1686#endif
1687 /* Retest writing/reading */
1688 if (options->cli_msg_len != 0 || options->srv_msg_len != 0) {
Yanray Wang74df2012023-03-16 12:15:49 +08001689 TEST_ASSERT(mbedtls_test_ssl_exchange_data(
1690 &(client.ssl), options->cli_msg_len,
Yanray Wangbd56b032023-03-14 14:36:48 +08001691 options->expected_cli_fragments,
Yanray Wang74df2012023-03-16 12:15:49 +08001692 &(server.ssl), options->srv_msg_len,
Yanray Wangbd56b032023-03-14 14:36:48 +08001693 options->expected_srv_fragments)
1694 == 0);
1695 }
1696 }
1697#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
1698
1699#if defined(MBEDTLS_SSL_RENEGOTIATION)
1700 if (options->renegotiate) {
1701 /* Start test with renegotiation */
1702 TEST_ASSERT(server.ssl.renego_status ==
1703 MBEDTLS_SSL_INITIAL_HANDSHAKE);
1704 TEST_ASSERT(client.ssl.renego_status ==
1705 MBEDTLS_SSL_INITIAL_HANDSHAKE);
1706
1707 /* After calling this function for the server, it only sends a handshake
1708 * request. All renegotiation should happen during data exchanging */
1709 TEST_ASSERT(mbedtls_ssl_renegotiate(&(server.ssl)) == 0);
1710 TEST_ASSERT(server.ssl.renego_status ==
1711 MBEDTLS_SSL_RENEGOTIATION_PENDING);
1712 TEST_ASSERT(client.ssl.renego_status ==
1713 MBEDTLS_SSL_INITIAL_HANDSHAKE);
1714
1715 TEST_ASSERT(exchange_data(&(client.ssl), &(server.ssl)) == 0);
1716 TEST_ASSERT(server.ssl.renego_status ==
1717 MBEDTLS_SSL_RENEGOTIATION_DONE);
1718 TEST_ASSERT(client.ssl.renego_status ==
1719 MBEDTLS_SSL_RENEGOTIATION_DONE);
1720
1721 /* After calling mbedtls_ssl_renegotiate for the client,
1722 * all renegotiation should happen inside this function.
1723 * However in this test, we cannot perform simultaneous communication
1724 * between client and server so this function will return waiting error
1725 * on the socket. All rest of renegotiation should happen
1726 * during data exchanging */
1727 ret = mbedtls_ssl_renegotiate(&(client.ssl));
1728#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1729 if (options->resize_buffers != 0) {
1730 /* Ensure that the buffer sizes are appropriate before resizes */
1731 TEST_ASSERT(client.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN);
1732 TEST_ASSERT(client.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN);
1733 }
1734#endif
1735 TEST_ASSERT(ret == 0 ||
1736 ret == MBEDTLS_ERR_SSL_WANT_READ ||
1737 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
1738 TEST_ASSERT(server.ssl.renego_status ==
1739 MBEDTLS_SSL_RENEGOTIATION_DONE);
1740 TEST_ASSERT(client.ssl.renego_status ==
1741 MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS);
1742
1743 TEST_ASSERT(exchange_data(&(client.ssl), &(server.ssl)) == 0);
1744 TEST_ASSERT(server.ssl.renego_status ==
1745 MBEDTLS_SSL_RENEGOTIATION_DONE);
1746 TEST_ASSERT(client.ssl.renego_status ==
1747 MBEDTLS_SSL_RENEGOTIATION_DONE);
1748#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1749 /* Validate buffer sizes after renegotiation */
1750 if (options->resize_buffers != 0) {
1751 TEST_ASSERT(client.ssl.out_buf_len ==
1752 mbedtls_ssl_get_output_buflen(&client.ssl));
1753 TEST_ASSERT(client.ssl.in_buf_len ==
1754 mbedtls_ssl_get_input_buflen(&client.ssl));
1755 TEST_ASSERT(server.ssl.out_buf_len ==
1756 mbedtls_ssl_get_output_buflen(&server.ssl));
1757 TEST_ASSERT(server.ssl.in_buf_len ==
1758 mbedtls_ssl_get_input_buflen(&server.ssl));
1759 }
1760#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
1761 }
1762#endif /* MBEDTLS_SSL_RENEGOTIATION */
1763
1764exit:
1765 mbedtls_test_ssl_endpoint_free(&client,
1766 options->dtls != 0 ? &client_context : NULL);
1767 mbedtls_test_ssl_endpoint_free(&server,
1768 options->dtls != 0 ? &server_context : NULL);
1769#if defined(MBEDTLS_DEBUG_C)
1770 if (options->cli_log_fun || options->srv_log_fun) {
1771 mbedtls_debug_set_threshold(0);
1772 }
1773#endif
1774#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1775 if (context_buf != NULL) {
1776 mbedtls_free(context_buf);
1777 }
1778#endif
1779}
Yanray Wang1ef77c02023-03-14 16:59:00 +08001780#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED && MBEDTLS_CERTS_C &&
1781 MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */
Yanray Wangbd56b032023-03-14 14:36:48 +08001782
Yanray Wang4323e452023-03-14 16:52:06 +08001783#endif /* MBEDTLS_SSL_TLS_C */