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