blob: 1f8ba1e42ad909c6747944f1874e7d3eea5bb719 [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 */
260int mbedtls_test_message_queue_peek_info(mbedtls_test_ssl_message_queue *queue,
261 size_t buf_len, size_t *msg_len)
262{
263 if (queue == NULL || msg_len == NULL) {
264 return MBEDTLS_TEST_ERROR_ARG_NULL;
265 }
266 if (queue->num == 0) {
267 return MBEDTLS_ERR_SSL_WANT_READ;
268 }
269
270 *msg_len = queue->messages[queue->pos];
271 return (*msg_len > buf_len) ? MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED : 0;
272}
273
Yanray Wangbd56b032023-03-14 14:36:48 +0800274void mbedtls_mock_socket_init(mbedtls_test_mock_socket *socket)
275{
276 memset(socket, 0, sizeof(*socket));
277}
278
Yanray Wangbd56b032023-03-14 14:36:48 +0800279void mbedtls_test_mock_socket_close(mbedtls_test_mock_socket *socket)
280{
281 if (socket == NULL) {
282 return;
283 }
284
285 if (socket->input != NULL) {
286 mbedtls_test_ssl_buffer_free(socket->input);
287 mbedtls_free(socket->input);
288 }
289
290 if (socket->output != NULL) {
291 mbedtls_test_ssl_buffer_free(socket->output);
292 mbedtls_free(socket->output);
293 }
294
295 if (socket->peer != NULL) {
296 memset(socket->peer, 0, sizeof(*socket->peer));
297 }
298
299 memset(socket, 0, sizeof(*socket));
300}
301
Yanray Wangbd56b032023-03-14 14:36:48 +0800302int mbedtls_test_mock_socket_connect(mbedtls_test_mock_socket *peer1,
303 mbedtls_test_mock_socket *peer2,
304 size_t bufsize)
305{
306 int ret = -1;
307
308 peer1->output =
309 (mbedtls_test_ssl_buffer *) mbedtls_calloc(
310 1, sizeof(mbedtls_test_ssl_buffer));
311 if (peer1->output == NULL) {
312 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
313 goto exit;
314 }
315 mbedtls_test_ssl_buffer_init(peer1->output);
316 if (0 != (ret = mbedtls_test_ssl_buffer_setup(peer1->output, bufsize))) {
317 goto exit;
318 }
319
320 peer2->output =
321 (mbedtls_test_ssl_buffer *) mbedtls_calloc(
322 1, sizeof(mbedtls_test_ssl_buffer));
323 if (peer2->output == NULL) {
324 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
325 goto exit;
326 }
327 mbedtls_test_ssl_buffer_init(peer2->output);
328 if (0 != (ret = mbedtls_test_ssl_buffer_setup(peer2->output, bufsize))) {
329 goto exit;
330 }
331
332 peer1->peer = peer2;
333 peer2->peer = peer1;
334 peer1->input = peer2->output;
335 peer2->input = peer1->output;
336
337 peer1->status = peer2->status = MBEDTLS_MOCK_SOCKET_CONNECTED;
338 ret = 0;
339
340exit:
341
342 if (ret != 0) {
343 mbedtls_test_mock_socket_close(peer1);
344 mbedtls_test_mock_socket_close(peer2);
345 }
346
347 return ret;
348}
349
Yanray Wangbd56b032023-03-14 14:36:48 +0800350int mbedtls_test_mock_tcp_send_b(void *ctx,
351 const unsigned char *buf, size_t len)
352{
353 mbedtls_test_mock_socket *socket = (mbedtls_test_mock_socket *) ctx;
354
355 if (socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED) {
356 return -1;
357 }
358
359 return mbedtls_test_ssl_buffer_put(socket->output, buf, len);
360}
361
362int mbedtls_test_mock_tcp_recv_b(void *ctx, unsigned char *buf, size_t len)
363{
364 mbedtls_test_mock_socket *socket = (mbedtls_test_mock_socket *) ctx;
365
366 if (socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED) {
367 return -1;
368 }
369
370 return mbedtls_test_ssl_buffer_get(socket->input, buf, len);
371}
372
Yanray Wangbd56b032023-03-14 14:36:48 +0800373int mbedtls_test_mock_tcp_send_nb(void *ctx,
374 const unsigned char *buf, size_t len)
375{
376 mbedtls_test_mock_socket *socket = (mbedtls_test_mock_socket *) ctx;
377
378 if (socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED) {
379 return -1;
380 }
381
382 if (socket->output->capacity == socket->output->content_length) {
383 return MBEDTLS_ERR_SSL_WANT_WRITE;
384 }
385
386 return mbedtls_test_ssl_buffer_put(socket->output, buf, len);
387}
388
389int mbedtls_test_mock_tcp_recv_nb(void *ctx, unsigned char *buf, size_t len)
390{
391 mbedtls_test_mock_socket *socket = (mbedtls_test_mock_socket *) ctx;
392
393 if (socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED) {
394 return -1;
395 }
396
397 if (socket->input->content_length == 0) {
398 return MBEDTLS_ERR_SSL_WANT_READ;
399 }
400
401 return mbedtls_test_ssl_buffer_get(socket->input, buf, len);
402}
403
404void mbedtls_test_message_socket_init(
405 mbedtls_test_message_socket_context *ctx)
406{
407 ctx->queue_input = NULL;
408 ctx->queue_output = NULL;
409 ctx->socket = NULL;
410}
411
Yanray Wangbd56b032023-03-14 14:36:48 +0800412int mbedtls_test_message_socket_setup(
413 mbedtls_test_ssl_message_queue *queue_input,
414 mbedtls_test_ssl_message_queue *queue_output,
415 size_t queue_capacity,
416 mbedtls_test_mock_socket *socket,
417 mbedtls_test_message_socket_context *ctx)
418{
419 int ret = mbedtls_test_ssl_message_queue_setup(queue_input, queue_capacity);
420 if (ret != 0) {
421 return ret;
422 }
423 ctx->queue_input = queue_input;
424 ctx->queue_output = queue_output;
425 ctx->socket = socket;
426 mbedtls_mock_socket_init(socket);
427
428 return 0;
429}
430
Yanray Wangbd56b032023-03-14 14:36:48 +0800431void mbedtls_test_message_socket_close(
432 mbedtls_test_message_socket_context *ctx)
433{
434 if (ctx == NULL) {
435 return;
436 }
437
438 mbedtls_test_ssl_message_queue_free(ctx->queue_input);
439 mbedtls_test_mock_socket_close(ctx->socket);
440 memset(ctx, 0, sizeof(*ctx));
441}
442
Yanray Wangbd56b032023-03-14 14:36:48 +0800443int mbedtls_test_mock_tcp_send_msg(void *ctx,
444 const unsigned char *buf, size_t len)
445{
446 mbedtls_test_ssl_message_queue *queue;
447 mbedtls_test_mock_socket *socket;
448 mbedtls_test_message_socket_context *context =
449 (mbedtls_test_message_socket_context *) ctx;
450
451 if (context == NULL || context->socket == NULL
452 || context->queue_output == NULL) {
453 return MBEDTLS_TEST_ERROR_CONTEXT_ERROR;
454 }
455
456 queue = context->queue_output;
457 socket = context->socket;
458
459 if (queue->num >= queue->capacity) {
460 return MBEDTLS_ERR_SSL_WANT_WRITE;
461 }
462
463 if (mbedtls_test_mock_tcp_send_b(socket, buf, len) != (int) len) {
464 return MBEDTLS_TEST_ERROR_SEND_FAILED;
465 }
466
467 return mbedtls_test_ssl_message_queue_push_info(queue, len);
468}
469
Yanray Wangbd56b032023-03-14 14:36:48 +0800470int mbedtls_test_mock_tcp_recv_msg(void *ctx,
471 unsigned char *buf, size_t buf_len)
472{
473 mbedtls_test_ssl_message_queue *queue;
474 mbedtls_test_mock_socket *socket;
475 mbedtls_test_message_socket_context *context =
476 (mbedtls_test_message_socket_context *) ctx;
477 size_t drop_len = 0;
478 size_t msg_len;
479 int ret;
480
481 if (context == NULL || context->socket == NULL
482 || context->queue_input == NULL) {
483 return MBEDTLS_TEST_ERROR_CONTEXT_ERROR;
484 }
485
486 queue = context->queue_input;
487 socket = context->socket;
488
489 /* Peek first, so that in case of a socket error the data remains in
490 * the queue. */
491 ret = mbedtls_test_message_queue_peek_info(queue, buf_len, &msg_len);
492 if (ret == MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED) {
493 /* Calculate how much to drop */
494 drop_len = msg_len - buf_len;
495
496 /* Set the requested message len to be buffer length */
497 msg_len = buf_len;
498 } else if (ret != 0) {
499 return ret;
500 }
501
502 if (mbedtls_test_mock_tcp_recv_b(socket, buf, msg_len) != (int) msg_len) {
503 return MBEDTLS_TEST_ERROR_RECV_FAILED;
504 }
505
506 if (ret == MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED) {
507 /* Drop the remaining part of the message */
508 if (mbedtls_test_mock_tcp_recv_b(socket, NULL, drop_len) !=
509 (int) drop_len) {
510 /* Inconsistent state - part of the message was read,
511 * and a part couldn't. Not much we can do here, but it should not
512 * happen in test environment, unless forced manually. */
513 }
514 }
515 mbedtls_test_ssl_message_queue_pop_info(queue, buf_len);
516
Yanray Wangd2696f22022-11-03 11:51:59 +0800517 return (msg_len > INT_MAX) ? INT_MAX : (int) msg_len;
Yanray Wangbd56b032023-03-14 14:36:48 +0800518}
519
520#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) && \
521 defined(MBEDTLS_CERTS_C) && \
522 defined(MBEDTLS_ENTROPY_C) && \
523 defined(MBEDTLS_CTR_DRBG_C)
524
525/*
526 * Deinitializes certificates from endpoint represented by \p ep.
527 */
528void mbedtls_endpoint_certificate_free(mbedtls_test_ssl_endpoint *ep)
529{
530 mbedtls_test_ssl_endpoint_certificate *cert = &(ep->cert);
531 if (cert != NULL) {
532 if (cert->ca_cert != NULL) {
533 mbedtls_x509_crt_free(cert->ca_cert);
534 mbedtls_free(cert->ca_cert);
535 cert->ca_cert = NULL;
536 }
537 if (cert->cert != NULL) {
538 mbedtls_x509_crt_free(cert->cert);
539 mbedtls_free(cert->cert);
540 cert->cert = NULL;
541 }
542 if (cert->pkey != NULL) {
543#if defined(MBEDTLS_USE_PSA_CRYPTO)
544 if (mbedtls_pk_get_type(cert->pkey) == MBEDTLS_PK_OPAQUE) {
545 mbedtls_svc_key_id_t *key_slot = cert->pkey->pk_ctx;
546 psa_destroy_key(*key_slot);
547 }
548#endif
549 mbedtls_pk_free(cert->pkey);
550 mbedtls_free(cert->pkey);
551 cert->pkey = NULL;
552 }
553 }
554}
555
Yanray Wangbd56b032023-03-14 14:36:48 +0800556int mbedtls_test_ssl_endpoint_certificate_init(mbedtls_test_ssl_endpoint *ep,
557 int pk_alg)
558{
559 int i = 0;
560 int ret = -1;
561 mbedtls_test_ssl_endpoint_certificate *cert = NULL;
562
563 if (ep == NULL) {
564 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
565 }
566
567 cert = &(ep->cert);
568 ASSERT_ALLOC(cert->ca_cert, 1);
569 ASSERT_ALLOC(cert->cert, 1);
570 ASSERT_ALLOC(cert->pkey, 1);
571
572 mbedtls_x509_crt_init(cert->ca_cert);
573 mbedtls_x509_crt_init(cert->cert);
574 mbedtls_pk_init(cert->pkey);
575
576 /* Load the trusted CA */
577
578 for (i = 0; mbedtls_test_cas_der[i] != NULL; i++) {
579 ret = mbedtls_x509_crt_parse_der(
580 cert->ca_cert,
581 (const unsigned char *) mbedtls_test_cas_der[i],
582 mbedtls_test_cas_der_len[i]);
583 TEST_ASSERT(ret == 0);
584 }
585
586 /* Load own certificate and private key */
587
588 if (ep->conf.endpoint == MBEDTLS_SSL_IS_SERVER) {
589 if (pk_alg == MBEDTLS_PK_RSA) {
590 ret = mbedtls_x509_crt_parse(
591 cert->cert,
592 (const unsigned char *) mbedtls_test_srv_crt_rsa_sha256_der,
593 mbedtls_test_srv_crt_rsa_sha256_der_len);
594 TEST_ASSERT(ret == 0);
595
596 ret = mbedtls_pk_parse_key(
597 cert->pkey,
598 (const unsigned char *) mbedtls_test_srv_key_rsa_der,
599 mbedtls_test_srv_key_rsa_der_len, NULL, 0);
600 TEST_ASSERT(ret == 0);
601 } else {
602 ret = mbedtls_x509_crt_parse(
603 cert->cert,
604 (const unsigned char *) mbedtls_test_srv_crt_ec_der,
605 mbedtls_test_srv_crt_ec_der_len);
606 TEST_ASSERT(ret == 0);
607
608 ret = mbedtls_pk_parse_key(
609 cert->pkey,
610 (const unsigned char *) mbedtls_test_srv_key_ec_der,
611 mbedtls_test_srv_key_ec_der_len, NULL, 0);
612 TEST_ASSERT(ret == 0);
613 }
614 } else {
615 if (pk_alg == MBEDTLS_PK_RSA) {
616 ret = mbedtls_x509_crt_parse(
617 cert->cert,
618 (const unsigned char *) mbedtls_test_cli_crt_rsa_der,
619 mbedtls_test_cli_crt_rsa_der_len);
620 TEST_ASSERT(ret == 0);
621
622 ret = mbedtls_pk_parse_key(
623 cert->pkey,
624 (const unsigned char *) mbedtls_test_cli_key_rsa_der,
625 mbedtls_test_cli_key_rsa_der_len, NULL, 0);
626 TEST_ASSERT(ret == 0);
627 } else {
628 ret = mbedtls_x509_crt_parse(
629 cert->cert,
630 (const unsigned char *) mbedtls_test_cli_crt_ec_der,
631 mbedtls_test_cli_crt_ec_len);
632 TEST_ASSERT(ret == 0);
633
634 ret = mbedtls_pk_parse_key(
635 cert->pkey,
636 (const unsigned char *) mbedtls_test_cli_key_ec_der,
637 mbedtls_test_cli_key_ec_der_len, NULL, 0);
638 TEST_ASSERT(ret == 0);
639 }
640 }
641
642 mbedtls_ssl_conf_ca_chain(&(ep->conf), cert->ca_cert, NULL);
643
644 ret = mbedtls_ssl_conf_own_cert(&(ep->conf), cert->cert,
645 cert->pkey);
646 TEST_ASSERT(ret == 0);
647
648exit:
649 if (ret != 0) {
650 mbedtls_endpoint_certificate_free(ep);
651 }
652
653 return ret;
654}
655
Yanray Wangbd56b032023-03-14 14:36:48 +0800656int mbedtls_test_ssl_endpoint_init(
657 mbedtls_test_ssl_endpoint *ep, int endpoint_type, int pk_alg,
658 mbedtls_test_message_socket_context *dtls_context,
659 mbedtls_test_ssl_message_queue *input_queue,
660 mbedtls_test_ssl_message_queue *output_queue,
661 const mbedtls_ecp_group_id *curves)
662{
663 int ret = -1;
664
665 if (dtls_context != NULL &&
666 (input_queue == NULL || output_queue == NULL)) {
667 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
668 }
669
670 if (ep == NULL) {
671 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
672 }
673
674 memset(ep, 0, sizeof(*ep));
675
676 ep->name = (endpoint_type == MBEDTLS_SSL_IS_SERVER) ? "Server" : "Client";
677
678 mbedtls_ssl_init(&(ep->ssl));
679 mbedtls_ssl_config_init(&(ep->conf));
680 mbedtls_ctr_drbg_init(&(ep->ctr_drbg));
681 mbedtls_ssl_conf_rng(&(ep->conf),
682 mbedtls_ctr_drbg_random,
683 &(ep->ctr_drbg));
684 mbedtls_entropy_init(&(ep->entropy));
685 if (dtls_context != NULL) {
686 TEST_ASSERT(mbedtls_test_message_socket_setup(input_queue, output_queue,
687 100, &(ep->socket),
688 dtls_context) == 0);
689 } else {
690 mbedtls_mock_socket_init(&(ep->socket));
691 }
692
693 ret = mbedtls_ctr_drbg_seed(&(ep->ctr_drbg), mbedtls_entropy_func,
694 &(ep->entropy),
695 (const unsigned char *) (ep->name),
696 strlen(ep->name));
697 TEST_ASSERT(ret == 0);
698
699 /* Non-blocking callbacks without timeout */
700 if (dtls_context != NULL) {
701 mbedtls_ssl_set_bio(&(ep->ssl), dtls_context,
702 mbedtls_test_mock_tcp_send_msg,
703 mbedtls_test_mock_tcp_recv_msg,
704 NULL);
705 } else {
706 mbedtls_ssl_set_bio(&(ep->ssl), &(ep->socket),
707 mbedtls_test_mock_tcp_send_nb,
708 mbedtls_test_mock_tcp_recv_nb,
709 NULL);
710 }
711
712 ret = mbedtls_ssl_config_defaults(&(ep->conf), endpoint_type,
713 (dtls_context != NULL) ?
714 MBEDTLS_SSL_TRANSPORT_DATAGRAM :
715 MBEDTLS_SSL_TRANSPORT_STREAM,
716 MBEDTLS_SSL_PRESET_DEFAULT);
717 TEST_ASSERT(ret == 0);
718
719#if defined(MBEDTLS_ECP_C)
720 if (curves != NULL) {
721 mbedtls_ssl_conf_curves(&(ep->conf), curves);
722 }
723#else
724 (void) curves;
725#endif
726
727 ret = mbedtls_ssl_setup(&(ep->ssl), &(ep->conf));
728 TEST_ASSERT(ret == 0);
729
730#if defined(MBEDTLS_SSL_PROTO_DTLS) && defined(MBEDTLS_SSL_SRV_C)
731 if (endpoint_type == MBEDTLS_SSL_IS_SERVER && dtls_context != NULL) {
732 mbedtls_ssl_conf_dtls_cookies(&(ep->conf), NULL, NULL, NULL);
733 }
734#endif
735
736 ret = mbedtls_test_ssl_endpoint_certificate_init(ep, pk_alg);
737 TEST_ASSERT(ret == 0);
738
739exit:
740 return ret;
741}
742
Yanray Wangbd56b032023-03-14 14:36:48 +0800743void mbedtls_test_ssl_endpoint_free(
744 mbedtls_test_ssl_endpoint *ep,
745 mbedtls_test_message_socket_context *context)
746{
747 mbedtls_endpoint_certificate_free(ep);
748
749 mbedtls_ssl_free(&(ep->ssl));
750 mbedtls_ssl_config_free(&(ep->conf));
751 mbedtls_ctr_drbg_free(&(ep->ctr_drbg));
752 mbedtls_entropy_free(&(ep->entropy));
753
754 if (context != NULL) {
755 mbedtls_test_message_socket_close(context);
756 } else {
757 mbedtls_test_mock_socket_close(&(ep->socket));
758 }
759}
760
Yanray Wangbd56b032023-03-14 14:36:48 +0800761int mbedtls_test_move_handshake_to_state(mbedtls_ssl_context *ssl,
762 mbedtls_ssl_context *second_ssl,
763 int state)
764{
765 enum { BUFFSIZE = 1024 };
766 int max_steps = 1000;
767 int ret = 0;
768
769 if (ssl == NULL || second_ssl == NULL) {
770 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
771 }
772
773 /* Perform communication via connected sockets */
774 while ((ssl->state != state) && (--max_steps >= 0)) {
775 /* If /p second_ssl ends the handshake procedure before /p ssl then
776 * there is no need to call the next step */
777 if (second_ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
778 ret = mbedtls_ssl_handshake_step(second_ssl);
779 if (ret != 0 && ret != MBEDTLS_ERR_SSL_WANT_READ &&
780 ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
781 return ret;
782 }
783 }
784
785 /* We only care about the \p ssl state and returns, so we call it last,
786 * to leave the iteration as soon as the state is as expected. */
787 ret = mbedtls_ssl_handshake_step(ssl);
788 if (ret != 0 && ret != MBEDTLS_ERR_SSL_WANT_READ &&
789 ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
790 return ret;
791 }
792 }
793
794 return (max_steps >= 0) ? ret : -1;
795}
796
Yanray Wang1ef77c02023-03-14 16:59:00 +0800797#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED && MBEDTLS_CERTS_C &&
798 MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */
Yanray Wangbd56b032023-03-14 14:36:48 +0800799
800/*
801 * Write application data. Increase write counter if necessary.
802 */
803int mbedtls_ssl_write_fragment(mbedtls_ssl_context *ssl,
804 unsigned char *buf, int buf_len,
805 int *written,
806 const int expected_fragments)
807{
808 /* Verify that calling mbedtls_ssl_write with a NULL buffer and zero length is
809 * a valid no-op for TLS connections. */
810 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
811 TEST_ASSERT(mbedtls_ssl_write(ssl, NULL, 0) == 0);
812 }
813
814 int ret = mbedtls_ssl_write(ssl, buf + *written, buf_len - *written);
815 if (ret > 0) {
816 *written += ret;
817 }
818
819 if (expected_fragments == 0) {
820 /* Used for DTLS and the message size larger than MFL. In that case
821 * the message can not be fragmented and the library should return
822 * MBEDTLS_ERR_SSL_BAD_INPUT_DATA error. This error must be returned
823 * to prevent a dead loop inside mbedtls_exchange_data(). */
824 return ret;
825 } else if (expected_fragments == 1) {
826 /* Used for TLS/DTLS and the message size lower than MFL */
827 TEST_ASSERT(ret == buf_len ||
828 ret == MBEDTLS_ERR_SSL_WANT_READ ||
829 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
830 } else {
831 /* Used for TLS and the message size larger than MFL */
832 TEST_ASSERT(expected_fragments > 1);
833 TEST_ASSERT((ret >= 0 && ret <= buf_len) ||
834 ret == MBEDTLS_ERR_SSL_WANT_READ ||
835 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
836 }
837
838 return 0;
839
840exit:
841 /* Some of the tests failed */
842 return -1;
843}
844
845/*
846 * Read application data and increase read counter and fragments counter
847 * if necessary.
848 */
849int mbedtls_ssl_read_fragment(mbedtls_ssl_context *ssl,
850 unsigned char *buf, int buf_len,
851 int *read, int *fragments,
852 const int expected_fragments)
853{
854 /* Verify that calling mbedtls_ssl_write with a NULL buffer and zero length is
855 * a valid no-op for TLS connections. */
856 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
857 TEST_ASSERT(mbedtls_ssl_read(ssl, NULL, 0) == 0);
858 }
859
860 int ret = mbedtls_ssl_read(ssl, buf + *read, buf_len - *read);
861 if (ret > 0) {
862 (*fragments)++;
863 *read += ret;
864 }
865
866 if (expected_fragments == 0) {
867 TEST_ASSERT(ret == 0);
868 } else if (expected_fragments == 1) {
869 TEST_ASSERT(ret == buf_len ||
870 ret == MBEDTLS_ERR_SSL_WANT_READ ||
871 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
872 } else {
873 TEST_ASSERT(expected_fragments > 1);
874 TEST_ASSERT((ret >= 0 && ret <= buf_len) ||
875 ret == MBEDTLS_ERR_SSL_WANT_READ ||
876 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
877 }
878
879 return 0;
880
881exit:
882 /* Some of the tests failed */
883 return -1;
884}
885
Yanray Wangbd56b032023-03-14 14:36:48 +0800886void set_ciphersuite(mbedtls_ssl_config *conf, const char *cipher,
887 int *forced_ciphersuite)
888{
889 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
890 forced_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id(cipher);
891 forced_ciphersuite[1] = 0;
892
893 ciphersuite_info =
894 mbedtls_ssl_ciphersuite_from_id(forced_ciphersuite[0]);
895
896 TEST_ASSERT(ciphersuite_info != NULL);
897 TEST_ASSERT(ciphersuite_info->min_minor_ver <= conf->max_minor_ver);
898 TEST_ASSERT(ciphersuite_info->max_minor_ver >= conf->min_minor_ver);
899
900 if (conf->max_minor_ver > ciphersuite_info->max_minor_ver) {
901 conf->max_minor_ver = ciphersuite_info->max_minor_ver;
902 }
903 if (conf->min_minor_ver < ciphersuite_info->min_minor_ver) {
904 conf->min_minor_ver = ciphersuite_info->min_minor_ver;
905 }
906
907 mbedtls_ssl_conf_ciphersuites(conf, forced_ciphersuite);
908
909exit:
910 return;
911}
912
913int psk_dummy_callback(void *p_info, mbedtls_ssl_context *ssl,
914 const unsigned char *name, size_t name_len)
915{
916 (void) p_info;
917 (void) ssl;
918 (void) name;
919 (void) name_len;
920
921 return 0;
922}
923
Yanray Wangbd56b032023-03-14 14:36:48 +0800924int mbedtls_test_ssl_build_transforms(mbedtls_ssl_transform *t_in,
925 mbedtls_ssl_transform *t_out,
926 int cipher_type, int hash_id,
927 int etm, int tag_mode, int ver,
928 size_t cid0_len,
929 size_t cid1_len)
930{
931 mbedtls_cipher_info_t const *cipher_info;
932 int ret = 0;
933
934 size_t keylen, maclen, ivlen;
935 unsigned char *key0 = NULL, *key1 = NULL;
936 unsigned char *md0 = NULL, *md1 = NULL;
937 unsigned char iv_enc[16], iv_dec[16];
938
939#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
940 unsigned char cid0[SSL_CID_LEN_MIN];
941 unsigned char cid1[SSL_CID_LEN_MIN];
942
943 mbedtls_test_rnd_std_rand(NULL, cid0, sizeof(cid0));
944 mbedtls_test_rnd_std_rand(NULL, cid1, sizeof(cid1));
945#else
946 ((void) cid0_len);
947 ((void) cid1_len);
948#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
949
950 maclen = 0;
951
952 /* Pick cipher */
953 cipher_info = mbedtls_cipher_info_from_type(cipher_type);
954 CHK(cipher_info != NULL);
955 CHK(cipher_info->iv_size <= 16);
956 CHK(cipher_info->key_bitlen % 8 == 0);
957
958 /* Pick keys */
959 keylen = cipher_info->key_bitlen / 8;
960 /* Allocate `keylen + 1` bytes to ensure that we get
961 * a non-NULL pointers from `mbedtls_calloc` even if
962 * `keylen == 0` in the case of the NULL cipher. */
963 CHK((key0 = mbedtls_calloc(1, keylen + 1)) != NULL);
964 CHK((key1 = mbedtls_calloc(1, keylen + 1)) != NULL);
965 memset(key0, 0x1, keylen);
966 memset(key1, 0x2, keylen);
967
968 /* Setup cipher contexts */
969 CHK(mbedtls_cipher_setup(&t_in->cipher_ctx_enc, cipher_info) == 0);
970 CHK(mbedtls_cipher_setup(&t_in->cipher_ctx_dec, cipher_info) == 0);
971 CHK(mbedtls_cipher_setup(&t_out->cipher_ctx_enc, cipher_info) == 0);
972 CHK(mbedtls_cipher_setup(&t_out->cipher_ctx_dec, cipher_info) == 0);
973
974#if defined(MBEDTLS_CIPHER_MODE_CBC)
975 if (cipher_info->mode == MBEDTLS_MODE_CBC) {
976 CHK(mbedtls_cipher_set_padding_mode(&t_in->cipher_ctx_enc,
977 MBEDTLS_PADDING_NONE) == 0);
978 CHK(mbedtls_cipher_set_padding_mode(&t_in->cipher_ctx_dec,
979 MBEDTLS_PADDING_NONE) == 0);
980 CHK(mbedtls_cipher_set_padding_mode(&t_out->cipher_ctx_enc,
981 MBEDTLS_PADDING_NONE) == 0);
982 CHK(mbedtls_cipher_set_padding_mode(&t_out->cipher_ctx_dec,
983 MBEDTLS_PADDING_NONE) == 0);
984 }
985#endif /* MBEDTLS_CIPHER_MODE_CBC */
986
987 CHK(mbedtls_cipher_setkey(&t_in->cipher_ctx_enc, key0,
Yanray Wangd2696f22022-11-03 11:51:59 +0800988 (keylen << 3 > INT_MAX) ? INT_MAX : (int) keylen << 3,
989 MBEDTLS_ENCRYPT)
990 == 0);
Yanray Wangbd56b032023-03-14 14:36:48 +0800991 CHK(mbedtls_cipher_setkey(&t_in->cipher_ctx_dec, key1,
Yanray Wangd2696f22022-11-03 11:51:59 +0800992 (keylen << 3 > INT_MAX) ? INT_MAX : (int) keylen << 3,
993 MBEDTLS_DECRYPT)
994 == 0);
Yanray Wangbd56b032023-03-14 14:36:48 +0800995 CHK(mbedtls_cipher_setkey(&t_out->cipher_ctx_enc, key1,
Yanray Wangd2696f22022-11-03 11:51:59 +0800996 (keylen << 3 > INT_MAX) ? INT_MAX : (int) keylen << 3,
997 MBEDTLS_ENCRYPT)
998 == 0);
Yanray Wangbd56b032023-03-14 14:36:48 +0800999 CHK(mbedtls_cipher_setkey(&t_out->cipher_ctx_dec, key0,
Yanray Wangd2696f22022-11-03 11:51:59 +08001000 (keylen << 3 > INT_MAX) ? INT_MAX : (int) keylen << 3,
1001 MBEDTLS_DECRYPT)
1002 == 0);
Yanray Wangbd56b032023-03-14 14:36:48 +08001003
1004 /* Setup MAC contexts */
1005#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1006 if (cipher_info->mode == MBEDTLS_MODE_CBC ||
1007 cipher_info->mode == MBEDTLS_MODE_STREAM) {
1008 mbedtls_md_info_t const *md_info;
1009
1010 /* Pick hash */
1011 md_info = mbedtls_md_info_from_type(hash_id);
1012 CHK(md_info != NULL);
1013
1014 /* Pick hash keys */
1015 maclen = mbedtls_md_get_size(md_info);
1016 CHK((md0 = mbedtls_calloc(1, maclen)) != NULL);
1017 CHK((md1 = mbedtls_calloc(1, maclen)) != NULL);
1018 memset(md0, 0x5, maclen);
1019 memset(md1, 0x6, maclen);
1020
1021 CHK(mbedtls_md_setup(&t_out->md_ctx_enc, md_info, 1) == 0);
1022 CHK(mbedtls_md_setup(&t_out->md_ctx_dec, md_info, 1) == 0);
1023 CHK(mbedtls_md_setup(&t_in->md_ctx_enc, md_info, 1) == 0);
1024 CHK(mbedtls_md_setup(&t_in->md_ctx_dec, md_info, 1) == 0);
1025
1026 if (ver > MBEDTLS_SSL_MINOR_VERSION_0) {
1027 CHK(mbedtls_md_hmac_starts(&t_in->md_ctx_enc,
1028 md0, maclen) == 0);
1029 CHK(mbedtls_md_hmac_starts(&t_in->md_ctx_dec,
1030 md1, maclen) == 0);
1031 CHK(mbedtls_md_hmac_starts(&t_out->md_ctx_enc,
1032 md1, maclen) == 0);
1033 CHK(mbedtls_md_hmac_starts(&t_out->md_ctx_dec,
1034 md0, maclen) == 0);
1035 }
1036#if defined(MBEDTLS_SSL_PROTO_SSL3)
1037 else {
1038 memcpy(&t_in->mac_enc, md0, maclen);
1039 memcpy(&t_in->mac_dec, md1, maclen);
1040 memcpy(&t_out->mac_enc, md1, maclen);
1041 memcpy(&t_out->mac_dec, md0, maclen);
1042 }
1043#endif
1044 }
1045#else
1046 ((void) hash_id);
1047#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1048
1049
1050 /* Pick IV's (regardless of whether they
1051 * are being used by the transform). */
1052 ivlen = cipher_info->iv_size;
1053 memset(iv_enc, 0x3, sizeof(iv_enc));
1054 memset(iv_dec, 0x4, sizeof(iv_dec));
1055
1056 /*
1057 * Setup transforms
1058 */
1059
1060#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
1061 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1062 t_out->encrypt_then_mac = etm;
1063 t_in->encrypt_then_mac = etm;
1064#else
1065 ((void) etm);
1066#endif
1067
1068 t_out->minor_ver = ver;
1069 t_in->minor_ver = ver;
1070 t_out->ivlen = ivlen;
1071 t_in->ivlen = ivlen;
1072
1073 switch (cipher_info->mode) {
1074 case MBEDTLS_MODE_GCM:
1075 case MBEDTLS_MODE_CCM:
1076#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
1077 if (ver == MBEDTLS_SSL_MINOR_VERSION_4) {
1078 t_out->fixed_ivlen = 12;
1079 t_in->fixed_ivlen = 12;
1080 } else
1081#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
1082 {
1083 t_out->fixed_ivlen = 4;
1084 t_in->fixed_ivlen = 4;
1085 }
1086 t_out->maclen = 0;
1087 t_in->maclen = 0;
1088 switch (tag_mode) {
1089 case 0: /* Full tag */
1090 t_out->taglen = 16;
1091 t_in->taglen = 16;
1092 break;
1093 case 1: /* Partial tag */
1094 t_out->taglen = 8;
1095 t_in->taglen = 8;
1096 break;
1097 default:
1098 ret = 1;
1099 goto cleanup;
1100 }
1101 break;
1102
1103 case MBEDTLS_MODE_CHACHAPOLY:
1104 t_out->fixed_ivlen = 12;
1105 t_in->fixed_ivlen = 12;
1106 t_out->maclen = 0;
1107 t_in->maclen = 0;
1108 switch (tag_mode) {
1109 case 0: /* Full tag */
1110 t_out->taglen = 16;
1111 t_in->taglen = 16;
1112 break;
1113 case 1: /* Partial tag */
1114 t_out->taglen = 8;
1115 t_in->taglen = 8;
1116 break;
1117 default:
1118 ret = 1;
1119 goto cleanup;
1120 }
1121 break;
1122
1123 case MBEDTLS_MODE_STREAM:
1124 case MBEDTLS_MODE_CBC:
1125 t_out->fixed_ivlen = 0; /* redundant, must be 0 */
1126 t_in->fixed_ivlen = 0; /* redundant, must be 0 */
1127 t_out->taglen = 0;
1128 t_in->taglen = 0;
1129 switch (tag_mode) {
1130 case 0: /* Full tag */
1131 t_out->maclen = maclen;
1132 t_in->maclen = maclen;
1133 break;
1134 case 1: /* Partial tag */
1135 t_out->maclen = 10;
1136 t_in->maclen = 10;
1137 break;
1138 default:
1139 ret = 1;
1140 goto cleanup;
1141 }
1142 break;
1143 default:
1144 ret = 1;
1145 goto cleanup;
1146 break;
1147 }
1148
1149 /* Setup IV's */
1150
1151 memcpy(&t_in->iv_dec, iv_dec, sizeof(iv_dec));
1152 memcpy(&t_in->iv_enc, iv_enc, sizeof(iv_enc));
1153 memcpy(&t_out->iv_dec, iv_enc, sizeof(iv_enc));
1154 memcpy(&t_out->iv_enc, iv_dec, sizeof(iv_dec));
1155
1156#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
1157 /* Add CID */
1158 memcpy(&t_in->in_cid, cid0, cid0_len);
1159 memcpy(&t_in->out_cid, cid1, cid1_len);
Yanray Wangd2696f22022-11-03 11:51:59 +08001160 t_in->in_cid_len = (uint8_t) cid0_len;
1161 t_in->out_cid_len = (uint8_t) cid1_len;
Yanray Wangbd56b032023-03-14 14:36:48 +08001162 memcpy(&t_out->in_cid, cid1, cid1_len);
1163 memcpy(&t_out->out_cid, cid0, cid0_len);
Yanray Wangd2696f22022-11-03 11:51:59 +08001164 t_out->in_cid_len = (uint8_t) cid1_len;
1165 t_out->out_cid_len = (uint8_t) cid0_len;
Yanray Wangbd56b032023-03-14 14:36:48 +08001166#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
1167
1168cleanup:
1169
1170 mbedtls_free(key0);
1171 mbedtls_free(key1);
1172
1173 mbedtls_free(md0);
1174 mbedtls_free(md1);
1175
1176 return ret;
1177}
1178
Yanray Wangbd56b032023-03-14 14:36:48 +08001179int mbedtls_test_ssl_populate_session(mbedtls_ssl_session *session,
1180 int ticket_len,
1181 const char *crt_file)
1182{
1183#if defined(MBEDTLS_HAVE_TIME)
1184 session->start = mbedtls_time(NULL) - 42;
1185#endif
1186 session->ciphersuite = 0xabcd;
1187 session->compression = 1;
1188 session->id_len = sizeof(session->id);
1189 memset(session->id, 66, session->id_len);
1190 memset(session->master, 17, sizeof(session->master));
1191
1192#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) && \
1193 defined(MBEDTLS_CERTS_C) && \
1194 defined(MBEDTLS_FS_IO)
1195 if (strlen(crt_file) != 0) {
1196 mbedtls_x509_crt tmp_crt;
1197 int ret;
1198
1199 mbedtls_x509_crt_init(&tmp_crt);
1200 ret = mbedtls_x509_crt_parse_file(&tmp_crt, crt_file);
1201 if (ret != 0) {
1202 return ret;
1203 }
1204
1205#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1206 /* Move temporary CRT. */
1207 session->peer_cert = mbedtls_calloc(1, sizeof(*session->peer_cert));
1208 if (session->peer_cert == NULL) {
1209 return -1;
1210 }
1211 *session->peer_cert = tmp_crt;
1212 memset(&tmp_crt, 0, sizeof(tmp_crt));
1213#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
1214 /* Calculate digest of temporary CRT. */
1215 session->peer_cert_digest =
1216 mbedtls_calloc(1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN);
1217 if (session->peer_cert_digest == NULL) {
1218 return -1;
1219 }
1220 ret = mbedtls_md(mbedtls_md_info_from_type(
1221 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE),
1222 tmp_crt.raw.p, tmp_crt.raw.len,
1223 session->peer_cert_digest);
1224 if (ret != 0) {
1225 return ret;
1226 }
1227 session->peer_cert_digest_type =
1228 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
1229 session->peer_cert_digest_len =
1230 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
1231#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
1232
1233 mbedtls_x509_crt_free(&tmp_crt);
1234 }
1235#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED && MBEDTLS_CERTS_C && MBEDTLS_FS_IO */
1236 (void) crt_file;
1237#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED && MBEDTLS_CERTS_C && MBEDTLS_FS_IO */
1238 session->verify_result = 0xdeadbeef;
1239
1240#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
1241 if (ticket_len != 0) {
1242 session->ticket = mbedtls_calloc(1, ticket_len);
1243 if (session->ticket == NULL) {
1244 return -1;
1245 }
1246 memset(session->ticket, 33, ticket_len);
1247 }
1248 session->ticket_len = ticket_len;
1249 session->ticket_lifetime = 86401;
1250#else
1251 (void) ticket_len;
1252#endif
1253
1254#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1255 session->mfl_code = 1;
1256#endif
1257#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1258 session->trunc_hmac = 1;
1259#endif
1260#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1261 session->encrypt_then_mac = 1;
1262#endif
1263
1264 return 0;
1265}
1266
Yanray Wangbd56b032023-03-14 14:36:48 +08001267int mbedtls_exchange_data(mbedtls_ssl_context *ssl_1,
1268 int msg_len_1, const int expected_fragments_1,
1269 mbedtls_ssl_context *ssl_2,
1270 int msg_len_2, const int expected_fragments_2)
1271{
1272 unsigned char *msg_buf_1 = malloc(msg_len_1);
1273 unsigned char *msg_buf_2 = malloc(msg_len_2);
1274 unsigned char *in_buf_1 = malloc(msg_len_2);
1275 unsigned char *in_buf_2 = malloc(msg_len_1);
1276 int msg_type, ret = -1;
1277
1278 /* Perform this test with two message types. At first use a message
1279 * consisting of only 0x00 for the client and only 0xFF for the server.
1280 * At the second time use message with generated data */
1281 for (msg_type = 0; msg_type < 2; msg_type++) {
1282 int written_1 = 0;
1283 int written_2 = 0;
1284 int read_1 = 0;
1285 int read_2 = 0;
1286 int fragments_1 = 0;
1287 int fragments_2 = 0;
1288
1289 if (msg_type == 0) {
1290 memset(msg_buf_1, 0x00, msg_len_1);
1291 memset(msg_buf_2, 0xff, msg_len_2);
1292 } else {
1293 int i, j = 0;
1294 for (i = 0; i < msg_len_1; i++) {
1295 msg_buf_1[i] = j++ & 0xFF;
1296 }
1297 for (i = 0; i < msg_len_2; i++) {
1298 msg_buf_2[i] = (j -= 5) & 0xFF;
1299 }
1300 }
1301
1302 while (read_1 < msg_len_2 || read_2 < msg_len_1) {
1303 /* ssl_1 sending */
1304 if (msg_len_1 > written_1) {
1305 ret = mbedtls_ssl_write_fragment(ssl_1, msg_buf_1,
1306 msg_len_1, &written_1,
1307 expected_fragments_1);
1308 if (expected_fragments_1 == 0) {
1309 /* This error is expected when the message is too large and
1310 * cannot be fragmented */
1311 TEST_ASSERT(ret == MBEDTLS_ERR_SSL_BAD_INPUT_DATA);
1312 msg_len_1 = 0;
1313 } else {
1314 TEST_ASSERT(ret == 0);
1315 }
1316 }
1317
1318 /* ssl_2 sending */
1319 if (msg_len_2 > written_2) {
1320 ret = mbedtls_ssl_write_fragment(ssl_2, msg_buf_2,
1321 msg_len_2, &written_2,
1322 expected_fragments_2);
1323 if (expected_fragments_2 == 0) {
1324 /* This error is expected when the message is too large and
1325 * cannot be fragmented */
1326 TEST_ASSERT(ret == MBEDTLS_ERR_SSL_BAD_INPUT_DATA);
1327 msg_len_2 = 0;
1328 } else {
1329 TEST_ASSERT(ret == 0);
1330 }
1331 }
1332
1333 /* ssl_1 reading */
1334 if (read_1 < msg_len_2) {
1335 ret = mbedtls_ssl_read_fragment(ssl_1, in_buf_1,
1336 msg_len_2, &read_1,
1337 &fragments_2,
1338 expected_fragments_2);
1339 TEST_ASSERT(ret == 0);
1340 }
1341
1342 /* ssl_2 reading */
1343 if (read_2 < msg_len_1) {
1344 ret = mbedtls_ssl_read_fragment(ssl_2, in_buf_2,
1345 msg_len_1, &read_2,
1346 &fragments_1,
1347 expected_fragments_1);
1348 TEST_ASSERT(ret == 0);
1349 }
1350 }
1351
1352 ret = -1;
1353 TEST_ASSERT(0 == memcmp(msg_buf_1, in_buf_2, msg_len_1));
1354 TEST_ASSERT(0 == memcmp(msg_buf_2, in_buf_1, msg_len_2));
1355 TEST_ASSERT(fragments_1 == expected_fragments_1);
1356 TEST_ASSERT(fragments_2 == expected_fragments_2);
1357 }
1358
1359 ret = 0;
1360
1361exit:
1362 free(msg_buf_1);
1363 free(in_buf_1);
1364 free(msg_buf_2);
1365 free(in_buf_2);
1366
1367 return ret;
1368}
1369
1370/*
1371 * Perform data exchanging between \p ssl_1 and \p ssl_2. Both of endpoints
1372 * must be initialized and connected beforehand.
1373 *
1374 * \retval 0 on success, otherwise error code.
1375 */
1376int exchange_data(mbedtls_ssl_context *ssl_1,
1377 mbedtls_ssl_context *ssl_2)
1378{
1379 return mbedtls_exchange_data(ssl_1, 256, 1,
1380 ssl_2, 256, 1);
1381}
1382
1383#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) && \
1384 defined(MBEDTLS_CERTS_C) && \
1385 defined(MBEDTLS_ENTROPY_C) && \
1386 defined(MBEDTLS_CTR_DRBG_C)
1387void mbedtls_test_ssl_perform_handshake(
1388 mbedtls_test_handshake_test_options *options)
1389{
1390 /* forced_ciphersuite needs to last until the end of the handshake */
1391 int forced_ciphersuite[2];
1392 enum { BUFFSIZE = 17000 };
1393 mbedtls_test_ssl_endpoint client, server;
1394#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
1395 const char *psk_identity = "foo";
1396#endif
1397#if defined(MBEDTLS_TIMING_C)
1398 mbedtls_timing_delay_context timer_client, timer_server;
1399#endif
1400#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1401 unsigned char *context_buf = NULL;
1402 size_t context_buf_len;
1403#endif
1404#if defined(MBEDTLS_SSL_RENEGOTIATION)
1405 int ret = -1;
1406#endif
1407 int expected_handshake_result = 0;
1408
1409 USE_PSA_INIT();
1410 mbedtls_platform_zeroize(&client, sizeof(client));
1411 mbedtls_platform_zeroize(&server, sizeof(server));
1412
1413 mbedtls_test_ssl_message_queue server_queue, client_queue;
1414 mbedtls_test_message_socket_context server_context, client_context;
1415 mbedtls_test_message_socket_init(&server_context);
1416 mbedtls_test_message_socket_init(&client_context);
1417
1418 /* Client side */
1419 if (options->dtls != 0) {
1420 TEST_ASSERT(mbedtls_test_ssl_endpoint_init(&client,
1421 MBEDTLS_SSL_IS_CLIENT,
1422 options->pk_alg,
1423 &client_context,
1424 &client_queue,
1425 &server_queue, NULL) == 0);
1426#if defined(MBEDTLS_TIMING_C)
1427 mbedtls_ssl_set_timer_cb(&client.ssl, &timer_client,
1428 mbedtls_timing_set_delay,
1429 mbedtls_timing_get_delay);
1430#endif
1431 } else {
1432 TEST_ASSERT(mbedtls_test_ssl_endpoint_init(&client,
1433 MBEDTLS_SSL_IS_CLIENT,
1434 options->pk_alg, NULL, NULL,
1435 NULL, NULL) == 0);
1436 }
1437
1438 if (options->client_min_version != TEST_SSL_MINOR_VERSION_NONE) {
1439 mbedtls_ssl_conf_min_version(&client.conf, MBEDTLS_SSL_MAJOR_VERSION_3,
1440 options->client_min_version);
1441 }
1442
1443 if (options->client_max_version != TEST_SSL_MINOR_VERSION_NONE) {
1444 mbedtls_ssl_conf_max_version(&client.conf, MBEDTLS_SSL_MAJOR_VERSION_3,
1445 options->client_max_version);
1446 }
1447
1448 if (strlen(options->cipher) > 0) {
1449 set_ciphersuite(&client.conf, options->cipher, forced_ciphersuite);
1450 }
1451
1452#if defined(MBEDTLS_DEBUG_C)
1453 if (options->cli_log_fun) {
1454 mbedtls_debug_set_threshold(4);
1455 mbedtls_ssl_conf_dbg(&client.conf, options->cli_log_fun,
1456 options->cli_log_obj);
1457 }
1458#endif
1459
1460 /* Server side */
1461 if (options->dtls != 0) {
1462 TEST_ASSERT(mbedtls_test_ssl_endpoint_init(&server,
1463 MBEDTLS_SSL_IS_SERVER,
1464 options->pk_alg,
1465 &server_context,
1466 &server_queue,
1467 &client_queue, NULL) == 0);
1468#if defined(MBEDTLS_TIMING_C)
1469 mbedtls_ssl_set_timer_cb(&server.ssl, &timer_server,
1470 mbedtls_timing_set_delay,
1471 mbedtls_timing_get_delay);
1472#endif
1473 } else {
1474 TEST_ASSERT(mbedtls_test_ssl_endpoint_init(&server,
1475 MBEDTLS_SSL_IS_SERVER,
1476 options->pk_alg, NULL, NULL,
1477 NULL, NULL) == 0);
1478 }
1479
1480 mbedtls_ssl_conf_authmode(&server.conf, options->srv_auth_mode);
1481
1482 if (options->server_min_version != TEST_SSL_MINOR_VERSION_NONE) {
1483 mbedtls_ssl_conf_min_version(&server.conf, MBEDTLS_SSL_MAJOR_VERSION_3,
1484 options->server_min_version);
1485 }
1486
1487 if (options->server_max_version != TEST_SSL_MINOR_VERSION_NONE) {
1488 mbedtls_ssl_conf_max_version(&server.conf, MBEDTLS_SSL_MAJOR_VERSION_3,
1489 options->server_max_version);
1490 }
1491
1492#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1493 TEST_ASSERT(mbedtls_ssl_conf_max_frag_len(&(server.conf),
1494 (unsigned char) options->mfl)
1495 == 0);
1496 TEST_ASSERT(mbedtls_ssl_conf_max_frag_len(&(client.conf),
1497 (unsigned char) options->mfl)
1498 == 0);
1499#else
1500 TEST_ASSERT(MBEDTLS_SSL_MAX_FRAG_LEN_NONE == options->mfl);
1501#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
1502
1503#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
1504 if (options->psk_str != NULL && options->psk_str->len > 0) {
1505 TEST_ASSERT(mbedtls_ssl_conf_psk(
1506 &client.conf, options->psk_str->x,
1507 options->psk_str->len,
1508 (const unsigned char *) psk_identity,
1509 strlen(psk_identity)) == 0);
1510
1511 TEST_ASSERT(mbedtls_ssl_conf_psk(
1512 &server.conf, options->psk_str->x,
1513 options->psk_str->len,
1514 (const unsigned char *) psk_identity,
1515 strlen(psk_identity)) == 0);
1516
1517 mbedtls_ssl_conf_psk_cb(&server.conf, psk_dummy_callback, NULL);
1518 }
1519#endif
1520#if defined(MBEDTLS_SSL_RENEGOTIATION)
1521 if (options->renegotiate) {
1522 mbedtls_ssl_conf_renegotiation(&(server.conf),
1523 MBEDTLS_SSL_RENEGOTIATION_ENABLED);
1524 mbedtls_ssl_conf_renegotiation(&(client.conf),
1525 MBEDTLS_SSL_RENEGOTIATION_ENABLED);
1526
1527 mbedtls_ssl_conf_legacy_renegotiation(&(server.conf),
1528 options->legacy_renegotiation);
1529 mbedtls_ssl_conf_legacy_renegotiation(&(client.conf),
1530 options->legacy_renegotiation);
1531 }
1532#endif /* MBEDTLS_SSL_RENEGOTIATION */
1533
1534#if defined(MBEDTLS_DEBUG_C)
1535 if (options->srv_log_fun) {
1536 mbedtls_debug_set_threshold(4);
1537 mbedtls_ssl_conf_dbg(&server.conf, options->srv_log_fun,
1538 options->srv_log_obj);
1539 }
1540#endif
1541
1542 TEST_ASSERT(mbedtls_test_mock_socket_connect(&(client.socket),
1543 &(server.socket),
1544 BUFFSIZE) == 0);
1545
1546#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1547 if (options->resize_buffers != 0) {
1548 /* Ensure that the buffer sizes are appropriate before resizes */
1549 TEST_ASSERT(client.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN);
1550 TEST_ASSERT(client.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN);
1551 TEST_ASSERT(server.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN);
1552 TEST_ASSERT(server.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN);
1553 }
1554#endif
1555
1556 if (options->expected_negotiated_version == TEST_SSL_MINOR_VERSION_NONE) {
1557 expected_handshake_result = MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION;
1558 }
1559
1560 TEST_ASSERT(mbedtls_test_move_handshake_to_state(
1561 &(client.ssl), &(server.ssl), MBEDTLS_SSL_HANDSHAKE_OVER)
1562 == expected_handshake_result);
1563
1564 if (expected_handshake_result != 0) {
1565 /* Connection will have failed by this point, skip to cleanup */
1566 goto exit;
1567 }
1568
1569 TEST_ASSERT(client.ssl.state == MBEDTLS_SSL_HANDSHAKE_OVER);
1570 TEST_ASSERT(server.ssl.state == MBEDTLS_SSL_HANDSHAKE_OVER);
1571
1572 /* Check that we agree on the version... */
1573 TEST_ASSERT(client.ssl.minor_ver == server.ssl.minor_ver);
1574
1575 /* And check that the version negotiated is the expected one. */
1576 TEST_EQUAL(client.ssl.minor_ver, options->expected_negotiated_version);
1577
1578#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1579 if (options->resize_buffers != 0) {
1580 if (options->expected_negotiated_version != MBEDTLS_SSL_MINOR_VERSION_0 &&
1581 options->expected_negotiated_version != MBEDTLS_SSL_MINOR_VERSION_1) {
1582 /* A server, when using DTLS, might delay a buffer resize to happen
1583 * after it receives a message, so we force it. */
1584 TEST_ASSERT(exchange_data(&(client.ssl), &(server.ssl)) == 0);
1585
1586 TEST_ASSERT(client.ssl.out_buf_len ==
1587 mbedtls_ssl_get_output_buflen(&client.ssl));
1588 TEST_ASSERT(client.ssl.in_buf_len ==
1589 mbedtls_ssl_get_input_buflen(&client.ssl));
1590 TEST_ASSERT(server.ssl.out_buf_len ==
1591 mbedtls_ssl_get_output_buflen(&server.ssl));
1592 TEST_ASSERT(server.ssl.in_buf_len ==
1593 mbedtls_ssl_get_input_buflen(&server.ssl));
1594 }
1595 }
1596#endif
1597
1598 if (options->cli_msg_len != 0 || options->srv_msg_len != 0) {
1599 /* Start data exchanging test */
1600 TEST_ASSERT(mbedtls_exchange_data(&(client.ssl), options->cli_msg_len,
1601 options->expected_cli_fragments,
1602 &(server.ssl), options->srv_msg_len,
1603 options->expected_srv_fragments)
1604 == 0);
1605 }
1606#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1607 if (options->serialize == 1) {
1608 TEST_ASSERT(options->dtls == 1);
1609
1610 TEST_ASSERT(mbedtls_ssl_context_save(&(server.ssl), NULL,
1611 0, &context_buf_len)
1612 == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL);
1613
1614 context_buf = mbedtls_calloc(1, context_buf_len);
1615 TEST_ASSERT(context_buf != NULL);
1616
1617 TEST_ASSERT(mbedtls_ssl_context_save(&(server.ssl), context_buf,
1618 context_buf_len,
1619 &context_buf_len)
1620 == 0);
1621
1622 mbedtls_ssl_free(&(server.ssl));
1623 mbedtls_ssl_init(&(server.ssl));
1624
1625 TEST_ASSERT(mbedtls_ssl_setup(&(server.ssl), &(server.conf)) == 0);
1626
1627 mbedtls_ssl_set_bio(&(server.ssl), &server_context,
1628 mbedtls_test_mock_tcp_send_msg,
1629 mbedtls_test_mock_tcp_recv_msg,
1630 NULL);
1631
1632#if defined(MBEDTLS_TIMING_C)
1633 mbedtls_ssl_set_timer_cb(&server.ssl, &timer_server,
1634 mbedtls_timing_set_delay,
1635 mbedtls_timing_get_delay);
1636#endif
1637#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1638 if (options->resize_buffers != 0) {
1639 /* Ensure that the buffer sizes are appropriate before resizes */
1640 TEST_ASSERT(server.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN);
1641 TEST_ASSERT(server.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN);
1642 }
1643#endif
1644 TEST_ASSERT(mbedtls_ssl_context_load(&(server.ssl), context_buf,
1645 context_buf_len) == 0);
1646
1647#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1648 /* Validate buffer sizes after context deserialization */
1649 if (options->resize_buffers != 0) {
1650 TEST_ASSERT(server.ssl.out_buf_len ==
1651 mbedtls_ssl_get_output_buflen(&server.ssl));
1652 TEST_ASSERT(server.ssl.in_buf_len ==
1653 mbedtls_ssl_get_input_buflen(&server.ssl));
1654 }
1655#endif
1656 /* Retest writing/reading */
1657 if (options->cli_msg_len != 0 || options->srv_msg_len != 0) {
1658 TEST_ASSERT(mbedtls_exchange_data(
1659 &(client.ssl),
1660 options->cli_msg_len,
1661 options->expected_cli_fragments,
1662 &(server.ssl),
1663 options->srv_msg_len,
1664 options->expected_srv_fragments)
1665 == 0);
1666 }
1667 }
1668#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
1669
1670#if defined(MBEDTLS_SSL_RENEGOTIATION)
1671 if (options->renegotiate) {
1672 /* Start test with renegotiation */
1673 TEST_ASSERT(server.ssl.renego_status ==
1674 MBEDTLS_SSL_INITIAL_HANDSHAKE);
1675 TEST_ASSERT(client.ssl.renego_status ==
1676 MBEDTLS_SSL_INITIAL_HANDSHAKE);
1677
1678 /* After calling this function for the server, it only sends a handshake
1679 * request. All renegotiation should happen during data exchanging */
1680 TEST_ASSERT(mbedtls_ssl_renegotiate(&(server.ssl)) == 0);
1681 TEST_ASSERT(server.ssl.renego_status ==
1682 MBEDTLS_SSL_RENEGOTIATION_PENDING);
1683 TEST_ASSERT(client.ssl.renego_status ==
1684 MBEDTLS_SSL_INITIAL_HANDSHAKE);
1685
1686 TEST_ASSERT(exchange_data(&(client.ssl), &(server.ssl)) == 0);
1687 TEST_ASSERT(server.ssl.renego_status ==
1688 MBEDTLS_SSL_RENEGOTIATION_DONE);
1689 TEST_ASSERT(client.ssl.renego_status ==
1690 MBEDTLS_SSL_RENEGOTIATION_DONE);
1691
1692 /* After calling mbedtls_ssl_renegotiate for the client,
1693 * all renegotiation should happen inside this function.
1694 * However in this test, we cannot perform simultaneous communication
1695 * between client and server so this function will return waiting error
1696 * on the socket. All rest of renegotiation should happen
1697 * during data exchanging */
1698 ret = mbedtls_ssl_renegotiate(&(client.ssl));
1699#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1700 if (options->resize_buffers != 0) {
1701 /* Ensure that the buffer sizes are appropriate before resizes */
1702 TEST_ASSERT(client.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN);
1703 TEST_ASSERT(client.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN);
1704 }
1705#endif
1706 TEST_ASSERT(ret == 0 ||
1707 ret == MBEDTLS_ERR_SSL_WANT_READ ||
1708 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
1709 TEST_ASSERT(server.ssl.renego_status ==
1710 MBEDTLS_SSL_RENEGOTIATION_DONE);
1711 TEST_ASSERT(client.ssl.renego_status ==
1712 MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS);
1713
1714 TEST_ASSERT(exchange_data(&(client.ssl), &(server.ssl)) == 0);
1715 TEST_ASSERT(server.ssl.renego_status ==
1716 MBEDTLS_SSL_RENEGOTIATION_DONE);
1717 TEST_ASSERT(client.ssl.renego_status ==
1718 MBEDTLS_SSL_RENEGOTIATION_DONE);
1719#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1720 /* Validate buffer sizes after renegotiation */
1721 if (options->resize_buffers != 0) {
1722 TEST_ASSERT(client.ssl.out_buf_len ==
1723 mbedtls_ssl_get_output_buflen(&client.ssl));
1724 TEST_ASSERT(client.ssl.in_buf_len ==
1725 mbedtls_ssl_get_input_buflen(&client.ssl));
1726 TEST_ASSERT(server.ssl.out_buf_len ==
1727 mbedtls_ssl_get_output_buflen(&server.ssl));
1728 TEST_ASSERT(server.ssl.in_buf_len ==
1729 mbedtls_ssl_get_input_buflen(&server.ssl));
1730 }
1731#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
1732 }
1733#endif /* MBEDTLS_SSL_RENEGOTIATION */
1734
1735exit:
1736 mbedtls_test_ssl_endpoint_free(&client,
1737 options->dtls != 0 ? &client_context : NULL);
1738 mbedtls_test_ssl_endpoint_free(&server,
1739 options->dtls != 0 ? &server_context : NULL);
1740#if defined(MBEDTLS_DEBUG_C)
1741 if (options->cli_log_fun || options->srv_log_fun) {
1742 mbedtls_debug_set_threshold(0);
1743 }
1744#endif
1745#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1746 if (context_buf != NULL) {
1747 mbedtls_free(context_buf);
1748 }
1749#endif
1750}
Yanray Wang1ef77c02023-03-14 16:59:00 +08001751#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED && MBEDTLS_CERTS_C &&
1752 MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */
Yanray Wangbd56b032023-03-14 14:36:48 +08001753
Yanray Wang4323e452023-03-14 16:52:06 +08001754#endif /* MBEDTLS_SSL_TLS_C */