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