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