blob: 793eed6056a1d24718e9e5e85c6a1f4405e906d5 [file] [log] [blame]
Yanray Wang47907a42022-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 Wange6afd912022-10-27 12:11:18 +080024
Yanray Wang4d07d1c2022-10-27 15:28:16 +080025#if defined(MBEDTLS_SSL_TLS_C)
Yanray Wange6afd912022-10-27 12:11:18 +080026#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
27static int rng_seed = 0xBEEF;
28static int rng_get(void *p_rng, unsigned char *output, size_t output_len)
29{
30 (void) p_rng;
31 for (size_t i = 0; i < output_len; i++) {
32 output[i] = rand();
33 }
34
35 return 0;
36}
37#endif
38
39/*
40 * This function can be passed to mbedtls to receive output logs from it. In
41 * this case, it will count the instances of a mbedtls_test_ssl_log_pattern
42 * in the received logged messages.
43 */
44void mbedtls_test_ssl_log_analyzer(void *ctx, int level,
45 const char *file, int line,
46 const char *str)
47{
48 mbedtls_test_ssl_log_pattern *p = (mbedtls_test_ssl_log_pattern *) ctx;
49
50 (void) level;
51 (void) line;
52 (void) file;
53
54 if (NULL != p &&
55 NULL != p->pattern &&
56 NULL != strstr(str, p->pattern)) {
57 p->counter++;
58 }
59}
60
61void mbedtls_test_init_handshake_options(
62 mbedtls_test_handshake_test_options *opts)
63{
64#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
65 srand(rng_seed);
66 rng_seed += 0xD0;
67#endif
68 opts->cipher = "";
69 opts->client_min_version = MBEDTLS_SSL_VERSION_UNKNOWN;
70 opts->client_max_version = MBEDTLS_SSL_VERSION_UNKNOWN;
71 opts->server_min_version = MBEDTLS_SSL_VERSION_UNKNOWN;
72 opts->server_max_version = MBEDTLS_SSL_VERSION_UNKNOWN;
73 opts->expected_negotiated_version = MBEDTLS_SSL_VERSION_TLS1_2;
74 opts->expected_handshake_result = 0;
75 opts->expected_ciphersuite = 0;
76 opts->pk_alg = MBEDTLS_PK_RSA;
77 opts->opaque_alg = 0;
78 opts->opaque_alg2 = 0;
79 opts->opaque_usage = 0;
80 opts->psk_str = NULL;
81 opts->dtls = 0;
82 opts->srv_auth_mode = MBEDTLS_SSL_VERIFY_NONE;
83 opts->serialize = 0;
84 opts->mfl = MBEDTLS_SSL_MAX_FRAG_LEN_NONE;
85 opts->cli_msg_len = 100;
86 opts->srv_msg_len = 100;
87 opts->expected_cli_fragments = 1;
88 opts->expected_srv_fragments = 1;
89 opts->renegotiate = 0;
90 opts->legacy_renegotiation = MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION;
91 opts->srv_log_obj = NULL;
92 opts->srv_log_obj = NULL;
93 opts->srv_log_fun = NULL;
94 opts->cli_log_fun = NULL;
95 opts->resize_buffers = 1;
96#if defined(MBEDTLS_SSL_CACHE_C)
97 opts->cache = NULL;
98 ASSERT_ALLOC(opts->cache, 1);
99 mbedtls_ssl_cache_init(opts->cache);
100exit:
101 return;
102#endif
103}
104
105void mbedtls_test_free_handshake_options(
106 mbedtls_test_handshake_test_options *opts)
107{
108#if defined(MBEDTLS_SSL_CACHE_C)
109 mbedtls_ssl_cache_free(opts->cache);
110 mbedtls_free(opts->cache);
111#else
112 (void) opts;
113#endif
114}
115
116#if defined(MBEDTLS_TEST_HOOKS)
117static void set_chk_buf_ptr_args(
118 mbedtls_ssl_chk_buf_ptr_args *args,
119 unsigned char *cur, unsigned char *end, size_t need)
120{
121 args->cur = cur;
122 args->end = end;
123 args->need = need;
124}
125
126static void reset_chk_buf_ptr_args(mbedtls_ssl_chk_buf_ptr_args *args)
127{
128 memset(args, 0, sizeof(*args));
129}
130#endif /* MBEDTLS_TEST_HOOKS */
131
132/*
133 * Buffer structure for custom I/O callbacks.
134 */
135
136/*
137 * Initialises \p buf. After calling this function it is safe to call
138 * `mbedtls_test_ssl_buffer_free()` on \p buf.
139 */
140void mbedtls_test_ssl_buffer_init(mbedtls_test_ssl_buffer *buf)
141{
142 memset(buf, 0, sizeof(*buf));
143}
144
145/*
146 * Sets up \p buf. After calling this function it is safe to call
147 * `mbedtls_test_ssl_buffer_put()` and `mbedtls_test_ssl_buffer_get()`
148 * on \p buf.
149 */
150int mbedtls_test_ssl_buffer_setup(mbedtls_test_ssl_buffer *buf,
151 size_t capacity)
152{
153 buf->buffer = (unsigned char *) mbedtls_calloc(capacity,
154 sizeof(unsigned char));
155 if (NULL == buf->buffer) {
156 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
157 }
158 buf->capacity = capacity;
159
160 return 0;
161}
162
163void mbedtls_test_ssl_buffer_free(mbedtls_test_ssl_buffer *buf)
164{
165 if (buf->buffer != NULL) {
166 mbedtls_free(buf->buffer);
167 }
168
169 memset(buf, 0, sizeof(*buf));
170}
171
172/*
173 * Puts \p input_len bytes from the \p input buffer into the ring buffer \p buf.
174 *
175 * \p buf must have been initialized and set up by calling
176 * `mbedtls_test_ssl_buffer_init()` and `mbedtls_test_ssl_buffer_setup()`.
177 *
178 * \retval \p input_len, if the data fits.
179 * \retval 0 <= value < \p input_len, if the data does not fit.
180 * \retval -1, if \p buf is NULL, it hasn't been set up or \p input_len is not
181 * zero and \p input is NULL.
182 */
183int mbedtls_test_ssl_buffer_put(mbedtls_test_ssl_buffer *buf,
184 const unsigned char *input, size_t input_len)
185{
186 size_t overflow = 0;
187
188 if ((buf == NULL) || (buf->buffer == NULL)) {
189 return -1;
190 }
191
192 /* Reduce input_len to a number that fits in the buffer. */
193 if ((buf->content_length + input_len) > buf->capacity) {
194 input_len = buf->capacity - buf->content_length;
195 }
196
197 if (input == NULL) {
198 return (input_len == 0) ? 0 : -1;
199 }
200
201 /* Check if the buffer has not come full circle and free space is not in
202 * the middle */
203 if (buf->start + buf->content_length < buf->capacity) {
204
205 /* Calculate the number of bytes that need to be placed at lower memory
206 * address */
207 if (buf->start + buf->content_length + input_len
208 > buf->capacity) {
209 overflow = (buf->start + buf->content_length + input_len)
210 % buf->capacity;
211 }
212
213 memcpy(buf->buffer + buf->start + buf->content_length, input,
214 input_len - overflow);
215 memcpy(buf->buffer, input + input_len - overflow, overflow);
216
217 } else {
218 /* The buffer has come full circle and free space is in the middle */
219 memcpy(buf->buffer + buf->start + buf->content_length - buf->capacity,
220 input, input_len);
221 }
222
223 buf->content_length += input_len;
224 return input_len;
225}
226
227/*
228 * Gets \p output_len bytes from the ring buffer \p buf into the
229 * \p output buffer. The output buffer can be NULL, in this case a part of the
230 * ring buffer will be dropped, if the requested length is available.
231 *
232 * \p buf must have been initialized and set up by calling
233 * `mbedtls_test_ssl_buffer_init()` and `mbedtls_test_ssl_buffer_setup()`.
234 *
235 * \retval \p output_len, if the data is available.
236 * \retval 0 <= value < \p output_len, if the data is not available.
237 * \retval -1, if \buf is NULL or it hasn't been set up.
238 */
239int mbedtls_test_ssl_buffer_get(mbedtls_test_ssl_buffer *buf,
240 unsigned char *output, size_t output_len)
241{
242 size_t overflow = 0;
243
244 if ((buf == NULL) || (buf->buffer == NULL)) {
245 return -1;
246 }
247
248 if (output == NULL && output_len == 0) {
249 return 0;
250 }
251
252 if (buf->content_length < output_len) {
253 output_len = buf->content_length;
254 }
255
256 /* Calculate the number of bytes that need to be drawn from lower memory
257 * address */
258 if (buf->start + output_len > buf->capacity) {
259 overflow = (buf->start + output_len) % buf->capacity;
260 }
261
262 if (output != NULL) {
263 memcpy(output, buf->buffer + buf->start, output_len - overflow);
264 memcpy(output + output_len - overflow, buf->buffer, overflow);
265 }
266
267 buf->content_length -= output_len;
268 buf->start = (buf->start + output_len) % buf->capacity;
269
270 return output_len;
271}
272
273/*
Yanray Wange6afd912022-10-27 12:11:18 +0800274 * Setup and free functions for the message metadata queue.
275 *
276 * \p capacity describes the number of message metadata chunks that can be held
277 * within the queue.
278 *
279 * \retval 0, if a metadata queue of a given length can be allocated.
280 * \retval MBEDTLS_ERR_SSL_ALLOC_FAILED, if allocation failed.
281 */
282int mbedtls_test_ssl_message_queue_setup(mbedtls_test_ssl_message_queue *queue,
283 size_t capacity)
284{
285 queue->messages = (size_t *) mbedtls_calloc(capacity, sizeof(size_t));
286 if (NULL == queue->messages) {
287 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
288 }
289
290 queue->capacity = capacity;
291 queue->pos = 0;
292 queue->num = 0;
293
294 return 0;
295}
296
297void mbedtls_test_ssl_message_queue_free(mbedtls_test_ssl_message_queue *queue)
298{
299 if (queue == NULL) {
300 return;
301 }
302
303 if (queue->messages != NULL) {
304 mbedtls_free(queue->messages);
305 }
306
307 memset(queue, 0, sizeof(*queue));
308}
309
310/*
311 * Push message length information onto the message metadata queue.
312 * This will become the last element to leave it (fifo).
313 *
314 * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null.
315 * \retval MBEDTLS_ERR_SSL_WANT_WRITE, if the queue is full.
316 * \retval \p len, if the push was successful.
317 */
318int mbedtls_test_ssl_message_queue_push_info(
319 mbedtls_test_ssl_message_queue *queue, size_t len)
320{
321 int place;
322 if (queue == NULL) {
323 return MBEDTLS_TEST_ERROR_ARG_NULL;
324 }
325
326 if (queue->num >= queue->capacity) {
327 return MBEDTLS_ERR_SSL_WANT_WRITE;
328 }
329
330 place = (queue->pos + queue->num) % queue->capacity;
331 queue->messages[place] = len;
332 queue->num++;
333 return len;
334}
335
336/*
337 * Pop information about the next message length from the queue. This will be
338 * the oldest inserted message length(fifo). \p msg_len can be null, in which
339 * case the data will be popped from the queue but not copied anywhere.
340 *
341 * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null.
342 * \retval MBEDTLS_ERR_SSL_WANT_READ, if the queue is empty.
343 * \retval message length, if the pop was successful, up to the given
344 \p buf_len.
345 */
346int mbedtls_test_ssl_message_queue_pop_info(
347 mbedtls_test_ssl_message_queue *queue, size_t buf_len)
348{
349 size_t message_length;
350 if (queue == NULL) {
351 return MBEDTLS_TEST_ERROR_ARG_NULL;
352 }
353 if (queue->num == 0) {
354 return MBEDTLS_ERR_SSL_WANT_READ;
355 }
356
357 message_length = queue->messages[queue->pos];
358 queue->messages[queue->pos] = 0;
359 queue->num--;
360 queue->pos++;
361 queue->pos %= queue->capacity;
362 if (queue->pos < 0) {
363 queue->pos += queue->capacity;
364 }
365
366 return (message_length > buf_len) ? buf_len : message_length;
367}
368
369/*
370 * Take a peek on the info about the next message length from the queue.
371 * This will be the oldest inserted message length(fifo).
372 *
373 * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null.
374 * \retval MBEDTLS_ERR_SSL_WANT_READ, if the queue is empty.
375 * \retval 0, if the peek was successful.
376 * \retval MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED, if the given buffer length is
377 * too small to fit the message. In this case the \p msg_len will be
378 * set to the full message length so that the
379 * caller knows what portion of the message can be dropped.
380 */
381int mbedtls_test_message_queue_peek_info(mbedtls_test_ssl_message_queue *queue,
382 size_t buf_len, size_t *msg_len)
383{
384 if (queue == NULL || msg_len == NULL) {
385 return MBEDTLS_TEST_ERROR_ARG_NULL;
386 }
387 if (queue->num == 0) {
388 return MBEDTLS_ERR_SSL_WANT_READ;
389 }
390
391 *msg_len = queue->messages[queue->pos];
392 return (*msg_len > buf_len) ? MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED : 0;
393}
394
395/*
396 * Setup and teardown functions for mock sockets.
397 */
398void mbedtls_mock_socket_init(mbedtls_test_mock_socket *socket)
399{
400 memset(socket, 0, sizeof(*socket));
401}
402
403/*
404 * Closes the socket \p socket.
405 *
406 * \p socket must have been previously initialized by calling
407 * mbedtls_mock_socket_init().
408 *
409 * This function frees all allocated resources and both sockets are aware of the
410 * new connection state.
411 *
412 * That is, this function does not simulate half-open TCP connections and the
413 * phenomenon that when closing a UDP connection the peer is not aware of the
414 * connection having been closed.
415 */
416void mbedtls_test_mock_socket_close(mbedtls_test_mock_socket *socket)
417{
418 if (socket == NULL) {
419 return;
420 }
421
422 if (socket->input != NULL) {
423 mbedtls_test_ssl_buffer_free(socket->input);
424 mbedtls_free(socket->input);
425 }
426
427 if (socket->output != NULL) {
428 mbedtls_test_ssl_buffer_free(socket->output);
429 mbedtls_free(socket->output);
430 }
431
432 if (socket->peer != NULL) {
433 memset(socket->peer, 0, sizeof(*socket->peer));
434 }
435
436 memset(socket, 0, sizeof(*socket));
437}
438
439/*
440 * Establishes a connection between \p peer1 and \p peer2.
441 *
442 * \p peer1 and \p peer2 must have been previously initialized by calling
443 * mbedtls_mock_socket_init().
444 *
445 * The capacities of the internal buffers are set to \p bufsize. Setting this to
446 * the correct value allows for simulation of MTU, sanity testing the mock
447 * implementation and mocking TCP connections with lower memory cost.
448 */
449int mbedtls_test_mock_socket_connect(mbedtls_test_mock_socket *peer1,
450 mbedtls_test_mock_socket *peer2,
451 size_t bufsize)
452{
453 int ret = -1;
454
455 peer1->output =
456 (mbedtls_test_ssl_buffer *) mbedtls_calloc(
457 1, sizeof(mbedtls_test_ssl_buffer));
458 if (peer1->output == NULL) {
459 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
460 goto exit;
461 }
462 mbedtls_test_ssl_buffer_init(peer1->output);
463 if (0 != (ret = mbedtls_test_ssl_buffer_setup(peer1->output, bufsize))) {
464 goto exit;
465 }
466
467 peer2->output =
468 (mbedtls_test_ssl_buffer *) mbedtls_calloc(
469 1, sizeof(mbedtls_test_ssl_buffer));
470 if (peer2->output == NULL) {
471 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
472 goto exit;
473 }
474 mbedtls_test_ssl_buffer_init(peer2->output);
475 if (0 != (ret = mbedtls_test_ssl_buffer_setup(peer2->output, bufsize))) {
476 goto exit;
477 }
478
479 peer1->peer = peer2;
480 peer2->peer = peer1;
481 peer1->input = peer2->output;
482 peer2->input = peer1->output;
483
484 peer1->status = peer2->status = MBEDTLS_MOCK_SOCKET_CONNECTED;
485 ret = 0;
486
487exit:
488
489 if (ret != 0) {
490 mbedtls_test_mock_socket_close(peer1);
491 mbedtls_test_mock_socket_close(peer2);
492 }
493
494 return ret;
495}
496
497/*
498 * Callbacks for simulating blocking I/O over connection-oriented transport.
499 */
500
501int mbedtls_test_mock_tcp_send_b(void *ctx, const unsigned char *buf,
502 size_t len)
503{
504 mbedtls_test_mock_socket *socket = (mbedtls_test_mock_socket *) ctx;
505
506 if (socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED) {
507 return -1;
508 }
509
510 return mbedtls_test_ssl_buffer_put(socket->output, buf, len);
511}
512
513int mbedtls_test_mock_tcp_recv_b(void *ctx, unsigned char *buf, size_t len)
514{
515 mbedtls_test_mock_socket *socket = (mbedtls_test_mock_socket *) ctx;
516
517 if (socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED) {
518 return -1;
519 }
520
521 return mbedtls_test_ssl_buffer_get(socket->input, buf, len);
522}
523
524/*
525 * Callbacks for simulating non-blocking I/O over connection-oriented transport.
526 */
527
528int mbedtls_test_mock_tcp_send_nb(void *ctx, const unsigned char *buf,
529 size_t len)
530{
531 mbedtls_test_mock_socket *socket = (mbedtls_test_mock_socket *) ctx;
532
533 if (socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED) {
534 return -1;
535 }
536
537 if (socket->output->capacity == socket->output->content_length) {
538 return MBEDTLS_ERR_SSL_WANT_WRITE;
539 }
540
541 return mbedtls_test_ssl_buffer_put(socket->output, buf, len);
542}
543
544int mbedtls_test_mock_tcp_recv_nb(void *ctx, unsigned char *buf, size_t len)
545{
546 mbedtls_test_mock_socket *socket = (mbedtls_test_mock_socket *) ctx;
547
548 if (socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED) {
549 return -1;
550 }
551
552 if (socket->input->content_length == 0) {
553 return MBEDTLS_ERR_SSL_WANT_READ;
554 }
555
556 return mbedtls_test_ssl_buffer_get(socket->input, buf, len);
557}
558
559void mbedtls_test_message_socket_init(mbedtls_test_message_socket_context *ctx)
560{
561 ctx->queue_input = NULL;
562 ctx->queue_output = NULL;
563 ctx->socket = NULL;
564}
565
566/*
567 * Setup a given message socket context including initialization of
568 * input/output queues to a chosen capacity of messages. Also set the
569 * corresponding mock socket.
570 *
571 * \retval 0, if everything succeeds.
572 * \retval MBEDTLS_ERR_SSL_ALLOC_FAILED, if allocation of a message
573 * queue failed.
574 */
575int mbedtls_test_message_socket_setup(
576 mbedtls_test_ssl_message_queue *queue_input,
577 mbedtls_test_ssl_message_queue *queue_output,
578 size_t queue_capacity,
579 mbedtls_test_mock_socket *socket,
580 mbedtls_test_message_socket_context *ctx)
581{
582 int ret = mbedtls_test_ssl_message_queue_setup(queue_input, queue_capacity);
583 if (ret != 0) {
584 return ret;
585 }
586 ctx->queue_input = queue_input;
587 ctx->queue_output = queue_output;
588 ctx->socket = socket;
589 mbedtls_mock_socket_init(socket);
590
591 return 0;
592}
593
594/*
595 * Close a given message socket context, along with the socket itself. Free the
596 * memory allocated by the input queue.
597 */
598void mbedtls_test_message_socket_close(mbedtls_test_message_socket_context *ctx)
599{
600 if (ctx == NULL) {
601 return;
602 }
603
604 mbedtls_test_ssl_message_queue_free(ctx->queue_input);
605 mbedtls_test_mock_socket_close(ctx->socket);
606 memset(ctx, 0, sizeof(*ctx));
607}
608
609/*
610 * Send one message through a given message socket context.
611 *
612 * \retval \p len, if everything succeeds.
613 * \retval MBEDTLS_TEST_ERROR_CONTEXT_ERROR, if any of the needed context
614 * elements or the context itself is null.
615 * \retval MBEDTLS_TEST_ERROR_SEND_FAILED if
616 * mbedtls_test_mock_tcp_send_b failed.
617 * \retval MBEDTLS_ERR_SSL_WANT_WRITE, if the output queue is full.
618 *
619 * This function will also return any error from
620 * mbedtls_test_ssl_message_queue_push_info.
621 */
622int mbedtls_test_mock_tcp_send_msg(void *ctx, const unsigned char *buf,
623 size_t len)
624{
625 mbedtls_test_ssl_message_queue *queue;
626 mbedtls_test_mock_socket *socket;
627 mbedtls_test_message_socket_context *context =
628 (mbedtls_test_message_socket_context *) ctx;
629
630 if (context == NULL || context->socket == NULL
631 || context->queue_output == NULL) {
632 return MBEDTLS_TEST_ERROR_CONTEXT_ERROR;
633 }
634
635 queue = context->queue_output;
636 socket = context->socket;
637
638 if (queue->num >= queue->capacity) {
639 return MBEDTLS_ERR_SSL_WANT_WRITE;
640 }
641
642 if (mbedtls_test_mock_tcp_send_b(socket, buf, len) != (int) len) {
643 return MBEDTLS_TEST_ERROR_SEND_FAILED;
644 }
645
646 return mbedtls_test_ssl_message_queue_push_info(queue, len);
647}
648
649/*
650 * Receive one message from a given message socket context and return message
651 * length or an error.
652 *
653 * \retval message length, if everything succeeds.
654 * \retval MBEDTLS_TEST_ERROR_CONTEXT_ERROR, if any of the needed context
655 * elements or the context itself is null.
656 * \retval MBEDTLS_TEST_ERROR_RECV_FAILED if
657 * mbedtls_test_mock_tcp_recv_b failed.
658 *
659 * This function will also return any error other than
660 * MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED from
661 * mbedtls_test_message_queue_peek_info.
662 */
663int mbedtls_test_mock_tcp_recv_msg(void *ctx, unsigned char *buf,
664 size_t buf_len)
665{
666 mbedtls_test_ssl_message_queue *queue;
667 mbedtls_test_mock_socket *socket;
668 mbedtls_test_message_socket_context *context =
669 (mbedtls_test_message_socket_context *) ctx;
670 size_t drop_len = 0;
671 size_t msg_len;
672 int ret;
673
674 if (context == NULL || context->socket == NULL
675 || context->queue_input == NULL) {
676 return MBEDTLS_TEST_ERROR_CONTEXT_ERROR;
677 }
678
679 queue = context->queue_input;
680 socket = context->socket;
681
682 /* Peek first, so that in case of a socket error the data remains in
683 * the queue. */
684 ret = mbedtls_test_message_queue_peek_info(queue, buf_len, &msg_len);
685 if (ret == MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED) {
686 /* Calculate how much to drop */
687 drop_len = msg_len - buf_len;
688
689 /* Set the requested message len to be buffer length */
690 msg_len = buf_len;
691 } else if (ret != 0) {
692 return ret;
693 }
694
695 if (mbedtls_test_mock_tcp_recv_b(socket, buf, msg_len) != (int) msg_len) {
696 return MBEDTLS_TEST_ERROR_RECV_FAILED;
697 }
698
699 if (ret == MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED) {
700 /* Drop the remaining part of the message */
701 if (mbedtls_test_mock_tcp_recv_b(socket, NULL, drop_len)
702 != (int) drop_len) {
703 /* Inconsistent state - part of the message was read,
704 * and a part couldn't. Not much we can do here, but it should not
705 * happen in test environment, unless forced manually. */
706 }
707 }
708 mbedtls_test_ssl_message_queue_pop_info(queue, buf_len);
709
710 return msg_len;
711}
712
713#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
714
715/*
716 * Deinitializes certificates from endpoint represented by \p ep.
717 */
718void mbedtls_endpoint_certificate_free(mbedtls_test_ssl_endpoint *ep)
719{
720 mbedtls_test_ssl_endpoint_certificate *cert = &(ep->cert);
721 if (cert != NULL) {
722 if (cert->ca_cert != NULL) {
723 mbedtls_x509_crt_free(cert->ca_cert);
724 mbedtls_free(cert->ca_cert);
725 cert->ca_cert = NULL;
726 }
727 if (cert->cert != NULL) {
728 mbedtls_x509_crt_free(cert->cert);
729 mbedtls_free(cert->cert);
730 cert->cert = NULL;
731 }
732 if (cert->pkey != NULL) {
733#if defined(MBEDTLS_USE_PSA_CRYPTO)
734 if (mbedtls_pk_get_type(cert->pkey) == MBEDTLS_PK_OPAQUE) {
735 mbedtls_svc_key_id_t *key_slot = cert->pkey->pk_ctx;
736 psa_destroy_key(*key_slot);
737 }
738#endif
739 mbedtls_pk_free(cert->pkey);
740 mbedtls_free(cert->pkey);
741 cert->pkey = NULL;
742 }
743 }
744}
745
746/*
747 * Initializes \p ep_cert structure and assigns it to endpoint
748 * represented by \p ep.
749 *
750 * \retval 0 on success, otherwise error code.
751 */
752int mbedtls_test_ssl_endpoint_certificate_init(mbedtls_test_ssl_endpoint *ep,
753 int pk_alg,
754 int opaque_alg, int opaque_alg2,
755 int opaque_usage)
756{
757 int i = 0;
758 int ret = -1;
759 mbedtls_test_ssl_endpoint_certificate *cert = NULL;
760#if defined(MBEDTLS_USE_PSA_CRYPTO)
761 mbedtls_svc_key_id_t key_slot = MBEDTLS_SVC_KEY_ID_INIT;
762#endif
763
764 if (ep == NULL) {
765 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
766 }
767
768 cert = &(ep->cert);
769 ASSERT_ALLOC(cert->ca_cert, 1);
770 ASSERT_ALLOC(cert->cert, 1);
771 ASSERT_ALLOC(cert->pkey, 1);
772
773 mbedtls_x509_crt_init(cert->ca_cert);
774 mbedtls_x509_crt_init(cert->cert);
775 mbedtls_pk_init(cert->pkey);
776
777 /* Load the trusted CA */
778
779 for (i = 0; mbedtls_test_cas_der[i] != NULL; i++) {
780 ret = mbedtls_x509_crt_parse_der(
781 cert->ca_cert,
782 (const unsigned char *) mbedtls_test_cas_der[i],
783 mbedtls_test_cas_der_len[i]);
784 TEST_ASSERT(ret == 0);
785 }
786
787 /* Load own certificate and private key */
788
789 if (ep->conf.endpoint == MBEDTLS_SSL_IS_SERVER) {
790 if (pk_alg == MBEDTLS_PK_RSA) {
791 ret = mbedtls_x509_crt_parse(
792 cert->cert,
793 (const unsigned char *) mbedtls_test_srv_crt_rsa_sha256_der,
794 mbedtls_test_srv_crt_rsa_sha256_der_len);
795 TEST_ASSERT(ret == 0);
796
797 ret = mbedtls_pk_parse_key(
798 cert->pkey,
799 (const unsigned char *) mbedtls_test_srv_key_rsa_der,
800 mbedtls_test_srv_key_rsa_der_len, NULL, 0,
801 mbedtls_test_rnd_std_rand, NULL);
802 TEST_ASSERT(ret == 0);
803 } else {
804 ret = mbedtls_x509_crt_parse(
805 cert->cert,
806 (const unsigned char *) mbedtls_test_srv_crt_ec_der,
807 mbedtls_test_srv_crt_ec_der_len);
808 TEST_ASSERT(ret == 0);
809
810 ret = mbedtls_pk_parse_key(
811 cert->pkey,
812 (const unsigned char *) mbedtls_test_srv_key_ec_der,
813 mbedtls_test_srv_key_ec_der_len, NULL, 0,
814 mbedtls_test_rnd_std_rand, NULL);
815 TEST_ASSERT(ret == 0);
816 }
817 } else {
818 if (pk_alg == MBEDTLS_PK_RSA) {
819 ret = mbedtls_x509_crt_parse(
820 cert->cert,
821 (const unsigned char *) mbedtls_test_cli_crt_rsa_der,
822 mbedtls_test_cli_crt_rsa_der_len);
823 TEST_ASSERT(ret == 0);
824
825 ret = mbedtls_pk_parse_key(
826 cert->pkey,
827 (const unsigned char *) mbedtls_test_cli_key_rsa_der,
828 mbedtls_test_cli_key_rsa_der_len, NULL, 0,
829 mbedtls_test_rnd_std_rand, NULL);
830 TEST_ASSERT(ret == 0);
831 } else {
832 ret = mbedtls_x509_crt_parse(
833 cert->cert,
834 (const unsigned char *) mbedtls_test_cli_crt_ec_der,
835 mbedtls_test_cli_crt_ec_len);
836 TEST_ASSERT(ret == 0);
837
838 ret = mbedtls_pk_parse_key(
839 cert->pkey,
840 (const unsigned char *) mbedtls_test_cli_key_ec_der,
841 mbedtls_test_cli_key_ec_der_len, NULL, 0,
842 mbedtls_test_rnd_std_rand, NULL);
843 TEST_ASSERT(ret == 0);
844 }
845 }
846
847#if defined(MBEDTLS_USE_PSA_CRYPTO)
848 if (opaque_alg != 0) {
849 TEST_EQUAL(mbedtls_pk_wrap_as_opaque(cert->pkey, &key_slot,
850 opaque_alg, opaque_usage,
851 opaque_alg2), 0);
852 }
853#else
854 (void) opaque_alg;
855 (void) opaque_alg2;
856 (void) opaque_usage;
857#endif
858
859 mbedtls_ssl_conf_ca_chain(&(ep->conf), cert->ca_cert, NULL);
860
861 ret = mbedtls_ssl_conf_own_cert(&(ep->conf), cert->cert,
862 cert->pkey);
863 TEST_ASSERT(ret == 0);
864 TEST_ASSERT(ep->conf.key_cert != NULL);
865
866 ret = mbedtls_ssl_conf_own_cert(&(ep->conf), NULL, NULL);
867 TEST_ASSERT(ret == 0);
868 TEST_ASSERT(ep->conf.key_cert == NULL);
869
870 ret = mbedtls_ssl_conf_own_cert(&(ep->conf), cert->cert,
871 cert->pkey);
872 TEST_ASSERT(ret == 0);
873
874exit:
875 if (ret != 0) {
876 mbedtls_endpoint_certificate_free(ep);
877 }
878
879 return ret;
880}
881
882/*
883 * Initializes \p ep structure. It is important to call
884 * `mbedtls_test_ssl_endpoint_free()` after calling this function
885 * even if it fails.
886 *
887 * \p endpoint_type must be set as MBEDTLS_SSL_IS_SERVER or
888 * MBEDTLS_SSL_IS_CLIENT.
889 * \p pk_alg the algorithm to use, currently only MBEDTLS_PK_RSA and
890 * MBEDTLS_PK_ECDSA are supported.
891 * \p dtls_context - in case of DTLS - this is the context handling metadata.
892 * \p input_queue - used only in case of DTLS.
893 * \p output_queue - used only in case of DTLS.
894 *
895 * \retval 0 on success, otherwise error code.
896 */
897int mbedtls_test_ssl_endpoint_init(
898 mbedtls_test_ssl_endpoint *ep, int endpoint_type,
899 mbedtls_test_handshake_test_options *options,
900 mbedtls_test_message_socket_context *dtls_context,
901 mbedtls_test_ssl_message_queue *input_queue,
902 mbedtls_test_ssl_message_queue *output_queue,
903 uint16_t *group_list)
904{
905 int ret = -1;
906 uintptr_t user_data_n;
907
908 if (dtls_context != NULL &&
909 (input_queue == NULL || output_queue == NULL)) {
910 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
911
912 }
913
914 if (ep == NULL) {
915 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
916 }
917
918 memset(ep, 0, sizeof(*ep));
919
920 ep->name = (endpoint_type == MBEDTLS_SSL_IS_SERVER) ? "Server" : "Client";
921
922 mbedtls_ssl_init(&(ep->ssl));
923 mbedtls_ssl_config_init(&(ep->conf));
924 mbedtls_ssl_conf_rng(&(ep->conf), rng_get, NULL);
925
926 TEST_ASSERT(mbedtls_ssl_conf_get_user_data_p(&ep->conf) == NULL);
927 TEST_EQUAL(mbedtls_ssl_conf_get_user_data_n(&ep->conf), 0);
928 TEST_ASSERT(mbedtls_ssl_get_user_data_p(&ep->ssl) == NULL);
929 TEST_EQUAL(mbedtls_ssl_get_user_data_n(&ep->ssl), 0);
930
931 (void) mbedtls_test_rnd_std_rand(NULL,
932 (void *) &user_data_n,
933 sizeof(user_data_n));
934 mbedtls_ssl_conf_set_user_data_n(&ep->conf, user_data_n);
935 mbedtls_ssl_set_user_data_n(&ep->ssl, user_data_n);
936
937 if (dtls_context != NULL) {
938 TEST_ASSERT(mbedtls_test_message_socket_setup(input_queue, output_queue,
939 100, &(ep->socket),
940 dtls_context) == 0);
941 } else {
942 mbedtls_mock_socket_init(&(ep->socket));
943 }
944
945 /* Non-blocking callbacks without timeout */
946 if (dtls_context != NULL) {
947 mbedtls_ssl_set_bio(&(ep->ssl), dtls_context,
948 mbedtls_test_mock_tcp_send_msg,
949 mbedtls_test_mock_tcp_recv_msg,
950 NULL);
951 } else {
952 mbedtls_ssl_set_bio(&(ep->ssl), &(ep->socket),
953 mbedtls_test_mock_tcp_send_nb,
954 mbedtls_test_mock_tcp_recv_nb,
955 NULL);
956 }
957
958 ret = mbedtls_ssl_config_defaults(&(ep->conf), endpoint_type,
959 (dtls_context != NULL) ?
960 MBEDTLS_SSL_TRANSPORT_DATAGRAM :
961 MBEDTLS_SSL_TRANSPORT_STREAM,
962 MBEDTLS_SSL_PRESET_DEFAULT);
963 TEST_ASSERT(ret == 0);
964
965 if (group_list != NULL) {
966 mbedtls_ssl_conf_groups(&(ep->conf), group_list);
967 }
968
969 mbedtls_ssl_conf_authmode(&(ep->conf), MBEDTLS_SSL_VERIFY_REQUIRED);
970
971#if defined(MBEDTLS_SSL_CACHE_C) && defined(MBEDTLS_SSL_SRV_C)
972 if (endpoint_type == MBEDTLS_SSL_IS_SERVER && options->cache != NULL) {
973 mbedtls_ssl_conf_session_cache(&(ep->conf), options->cache,
974 mbedtls_ssl_cache_get,
975 mbedtls_ssl_cache_set);
976 }
977#endif
978
979 ret = mbedtls_ssl_setup(&(ep->ssl), &(ep->conf));
980 TEST_ASSERT(ret == 0);
981
982#if defined(MBEDTLS_SSL_PROTO_DTLS) && defined(MBEDTLS_SSL_SRV_C)
983 if (endpoint_type == MBEDTLS_SSL_IS_SERVER && dtls_context != NULL) {
984 mbedtls_ssl_conf_dtls_cookies(&(ep->conf), NULL, NULL, NULL);
985 }
986#endif
987
988 ret = mbedtls_test_ssl_endpoint_certificate_init(ep, options->pk_alg,
989 options->opaque_alg,
990 options->opaque_alg2,
991 options->opaque_usage);
992 TEST_ASSERT(ret == 0);
993
994 TEST_EQUAL(mbedtls_ssl_conf_get_user_data_n(&ep->conf), user_data_n);
995 mbedtls_ssl_conf_set_user_data_p(&ep->conf, ep);
996 TEST_EQUAL(mbedtls_ssl_get_user_data_n(&ep->ssl), user_data_n);
997 mbedtls_ssl_set_user_data_p(&ep->ssl, ep);
998
999exit:
1000 return ret;
1001}
1002
1003/*
1004 * Deinitializes endpoint represented by \p ep.
1005 */
1006void mbedtls_test_ssl_endpoint_free(
1007 mbedtls_test_ssl_endpoint *ep,
1008 mbedtls_test_message_socket_context *context)
1009{
1010 mbedtls_endpoint_certificate_free(ep);
1011
1012 mbedtls_ssl_free(&(ep->ssl));
1013 mbedtls_ssl_config_free(&(ep->conf));
1014
1015 if (context != NULL) {
1016 mbedtls_test_message_socket_close(context);
1017 } else {
1018 mbedtls_test_mock_socket_close(&(ep->socket));
1019 }
1020}
1021
1022/*
1023 * This function moves ssl handshake from \p ssl to prescribed \p state.
1024 * /p second_ssl is used as second endpoint and their sockets have to be
1025 * connected before calling this function.
1026 *
1027 * \retval 0 on success, otherwise error code.
1028 */
1029int mbedtls_test_move_handshake_to_state(mbedtls_ssl_context *ssl,
1030 mbedtls_ssl_context *second_ssl,
1031 int state)
1032{
1033 enum { BUFFSIZE = 1024 };
1034 int max_steps = 1000;
1035 int ret = 0;
1036
1037 if (ssl == NULL || second_ssl == NULL) {
1038 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
1039 }
1040
1041 /* Perform communication via connected sockets */
1042 while ((ssl->state != state) && (--max_steps >= 0)) {
1043 /* If /p second_ssl ends the handshake procedure before /p ssl then
1044 * there is no need to call the next step */
1045 if (!mbedtls_ssl_is_handshake_over(second_ssl)) {
1046 ret = mbedtls_ssl_handshake_step(second_ssl);
1047 if (ret != 0 && ret != MBEDTLS_ERR_SSL_WANT_READ &&
1048 ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
1049 return ret;
1050 }
1051 }
1052
1053 /* We only care about the \p ssl state and returns, so we call it last,
1054 * to leave the iteration as soon as the state is as expected. */
1055 ret = mbedtls_ssl_handshake_step(ssl);
1056 if (ret != 0 && ret != MBEDTLS_ERR_SSL_WANT_READ &&
1057 ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
1058 return ret;
1059 }
1060 }
1061
1062 return (max_steps >= 0) ? ret : -1;
1063}
1064
1065#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
1066
1067/*
1068 * Write application data. Increase write counter if necessary.
1069 */
1070int mbedtls_ssl_write_fragment(mbedtls_ssl_context *ssl, unsigned char *buf,
1071 int buf_len, int *written,
1072 const int expected_fragments)
1073{
1074 /* Verify that calling mbedtls_ssl_write with a NULL buffer and zero length is
1075 * a valid no-op for TLS connections. */
1076 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1077 TEST_ASSERT(mbedtls_ssl_write(ssl, NULL, 0) == 0);
1078 }
1079
1080 int ret = mbedtls_ssl_write(ssl, buf + *written, buf_len - *written);
1081 if (ret > 0) {
1082 *written += ret;
1083 }
1084
1085 if (expected_fragments == 0) {
1086 /* Used for DTLS and the message size larger than MFL. In that case
1087 * the message can not be fragmented and the library should return
1088 * MBEDTLS_ERR_SSL_BAD_INPUT_DATA error. This error must be returned
1089 * to prevent a dead loop inside mbedtls_exchange_data(). */
1090 return ret;
1091 } else if (expected_fragments == 1) {
1092 /* Used for TLS/DTLS and the message size lower than MFL */
1093 TEST_ASSERT(ret == buf_len ||
1094 ret == MBEDTLS_ERR_SSL_WANT_READ ||
1095 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
1096 } else {
1097 /* Used for TLS and the message size larger than MFL */
1098 TEST_ASSERT(expected_fragments > 1);
1099 TEST_ASSERT((ret >= 0 && ret <= buf_len) ||
1100 ret == MBEDTLS_ERR_SSL_WANT_READ ||
1101 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
1102 }
1103
1104 return 0;
1105
1106exit:
1107 /* Some of the tests failed */
1108 return -1;
1109}
1110
1111/*
1112 * Read application data and increase read counter and fragments counter
1113 * if necessary.
1114 */
1115int mbedtls_ssl_read_fragment(mbedtls_ssl_context *ssl, unsigned char *buf,
1116 int buf_len, int *read,
1117 int *fragments, const int expected_fragments)
1118{
1119 /* Verify that calling mbedtls_ssl_write with a NULL buffer and zero length is
1120 * a valid no-op for TLS connections. */
1121 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1122 TEST_ASSERT(mbedtls_ssl_read(ssl, NULL, 0) == 0);
1123 }
1124
1125 int ret = mbedtls_ssl_read(ssl, buf + *read, buf_len - *read);
1126 if (ret > 0) {
1127 (*fragments)++;
1128 *read += ret;
1129 }
1130
1131 if (expected_fragments == 0) {
1132 TEST_ASSERT(ret == 0);
1133 } else if (expected_fragments == 1) {
1134 TEST_ASSERT(ret == buf_len ||
1135 ret == MBEDTLS_ERR_SSL_WANT_READ ||
1136 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
1137 } else {
1138 TEST_ASSERT(expected_fragments > 1);
1139 TEST_ASSERT((ret >= 0 && ret <= buf_len) ||
1140 ret == MBEDTLS_ERR_SSL_WANT_READ ||
1141 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
1142 }
1143
1144 return 0;
1145
1146exit:
1147 /* Some of the tests failed */
1148 return -1;
1149}
1150
1151/*
1152 * Helper function setting up inverse record transformations
1153 * using given cipher, hash, EtM mode, authentication tag length,
1154 * and version.
1155 */
1156
1157#define CHK(x) \
1158 do \
1159 { \
1160 if (!(x)) \
1161 { \
1162 ret = -1; \
1163 goto cleanup; \
1164 } \
1165 } while (0)
1166
1167void set_ciphersuite(mbedtls_ssl_config *conf, const char *cipher,
1168 int *forced_ciphersuite)
1169{
1170 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
1171 forced_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id(cipher);
1172 forced_ciphersuite[1] = 0;
1173
1174 ciphersuite_info =
1175 mbedtls_ssl_ciphersuite_from_id(forced_ciphersuite[0]);
1176
1177 TEST_ASSERT(ciphersuite_info != NULL);
1178 TEST_ASSERT(ciphersuite_info->min_tls_version <= conf->max_tls_version);
1179 TEST_ASSERT(ciphersuite_info->max_tls_version >= conf->min_tls_version);
1180
1181 if (conf->max_tls_version > ciphersuite_info->max_tls_version) {
1182 conf->max_tls_version = ciphersuite_info->max_tls_version;
1183 }
1184 if (conf->min_tls_version < ciphersuite_info->min_tls_version) {
1185 conf->min_tls_version = ciphersuite_info->min_tls_version;
1186 }
1187
1188 mbedtls_ssl_conf_ciphersuites(conf, forced_ciphersuite);
1189
1190exit:
1191 return;
1192}
1193
1194int psk_dummy_callback(void *p_info, mbedtls_ssl_context *ssl,
1195 const unsigned char *name, size_t name_len)
1196{
1197 (void) p_info;
1198 (void) ssl;
1199 (void) name;
1200 (void) name_len;
1201
1202 return 0;
1203}
1204
1205#if MBEDTLS_SSL_CID_OUT_LEN_MAX > MBEDTLS_SSL_CID_IN_LEN_MAX
1206#define SSL_CID_LEN_MIN MBEDTLS_SSL_CID_IN_LEN_MAX
1207#else
1208#define SSL_CID_LEN_MIN MBEDTLS_SSL_CID_OUT_LEN_MAX
1209#endif
1210
1211#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
1212 defined(MBEDTLS_CIPHER_MODE_CBC) && defined(MBEDTLS_AES_C)
1213int mbedtls_test_psa_cipher_encrypt_helper(mbedtls_ssl_transform *transform,
1214 const unsigned char *iv,
1215 size_t iv_len,
1216 const unsigned char *input,
1217 size_t ilen,
1218 unsigned char *output,
1219 size_t *olen)
1220{
1221#if defined(MBEDTLS_USE_PSA_CRYPTO)
1222 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1223 psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT;
1224 size_t part_len;
1225
1226 status = psa_cipher_encrypt_setup(&cipher_op,
1227 transform->psa_key_enc,
1228 transform->psa_alg);
1229
1230 if (status != PSA_SUCCESS) {
1231 return PSA_TO_MBEDTLS_ERR(status);
1232 }
1233
1234 status = psa_cipher_set_iv(&cipher_op, iv, iv_len);
1235
1236 if (status != PSA_SUCCESS) {
1237 return PSA_TO_MBEDTLS_ERR(status);
1238 }
1239
1240 status = psa_cipher_update(&cipher_op, input, ilen, output, ilen, olen);
1241
1242 if (status != PSA_SUCCESS) {
1243 return PSA_TO_MBEDTLS_ERR(status);
1244 }
1245
1246 status = psa_cipher_finish(&cipher_op, output + *olen, ilen - *olen,
1247 &part_len);
1248
1249 if (status != PSA_SUCCESS) {
1250 return PSA_TO_MBEDTLS_ERR(status);
1251 }
1252
1253 *olen += part_len;
1254 return 0;
1255#else
1256 return mbedtls_cipher_crypt(&transform->cipher_ctx_enc,
1257 iv, iv_len, input, ilen, output, olen);
1258#endif /* MBEDTLS_USE_PSA_CRYPTO */
1259}
1260#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_CIPHER_MODE_CBC &&
1261 MBEDTLS_AES_C */
1262
1263int mbedtls_test_ssl_build_transforms(mbedtls_ssl_transform *t_in,
1264 mbedtls_ssl_transform *t_out,
1265 int cipher_type, int hash_id,
1266 int etm, int tag_mode,
1267 mbedtls_ssl_protocol_version tls_version,
1268 size_t cid0_len,
1269 size_t cid1_len)
1270{
1271 mbedtls_cipher_info_t const *cipher_info;
1272 int ret = 0;
1273
1274#if defined(MBEDTLS_USE_PSA_CRYPTO)
1275 psa_key_type_t key_type;
1276 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1277 psa_algorithm_t alg;
1278 size_t key_bits;
1279 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1280#endif
1281
1282 size_t keylen, maclen, ivlen;
1283 unsigned char *key0 = NULL, *key1 = NULL;
1284 unsigned char *md0 = NULL, *md1 = NULL;
1285 unsigned char iv_enc[16], iv_dec[16];
1286
1287#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
1288 unsigned char cid0[SSL_CID_LEN_MIN];
1289 unsigned char cid1[SSL_CID_LEN_MIN];
1290
1291 mbedtls_test_rnd_std_rand(NULL, cid0, sizeof(cid0));
1292 mbedtls_test_rnd_std_rand(NULL, cid1, sizeof(cid1));
1293#else
1294 ((void) cid0_len);
1295 ((void) cid1_len);
1296#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
1297
1298 maclen = 0;
1299
1300 /* Pick cipher */
1301 cipher_info = mbedtls_cipher_info_from_type(cipher_type);
1302 CHK(cipher_info != NULL);
1303 CHK(cipher_info->iv_size <= 16);
1304 CHK(cipher_info->key_bitlen % 8 == 0);
1305
1306 /* Pick keys */
1307 keylen = cipher_info->key_bitlen / 8;
1308 /* Allocate `keylen + 1` bytes to ensure that we get
1309 * a non-NULL pointers from `mbedtls_calloc` even if
1310 * `keylen == 0` in the case of the NULL cipher. */
1311 CHK((key0 = mbedtls_calloc(1, keylen + 1)) != NULL);
1312 CHK((key1 = mbedtls_calloc(1, keylen + 1)) != NULL);
1313 memset(key0, 0x1, keylen);
1314 memset(key1, 0x2, keylen);
1315
1316#if !defined(MBEDTLS_USE_PSA_CRYPTO)
1317 /* Setup cipher contexts */
1318 CHK(mbedtls_cipher_setup(&t_in->cipher_ctx_enc, cipher_info) == 0);
1319 CHK(mbedtls_cipher_setup(&t_in->cipher_ctx_dec, cipher_info) == 0);
1320 CHK(mbedtls_cipher_setup(&t_out->cipher_ctx_enc, cipher_info) == 0);
1321 CHK(mbedtls_cipher_setup(&t_out->cipher_ctx_dec, cipher_info) == 0);
1322
1323#if defined(MBEDTLS_CIPHER_MODE_CBC)
1324 if (cipher_info->mode == MBEDTLS_MODE_CBC) {
1325 CHK(mbedtls_cipher_set_padding_mode(&t_in->cipher_ctx_enc,
1326 MBEDTLS_PADDING_NONE) == 0);
1327 CHK(mbedtls_cipher_set_padding_mode(&t_in->cipher_ctx_dec,
1328 MBEDTLS_PADDING_NONE) == 0);
1329 CHK(mbedtls_cipher_set_padding_mode(&t_out->cipher_ctx_enc,
1330 MBEDTLS_PADDING_NONE) == 0);
1331 CHK(mbedtls_cipher_set_padding_mode(&t_out->cipher_ctx_dec,
1332 MBEDTLS_PADDING_NONE) == 0);
1333 }
1334#endif /* MBEDTLS_CIPHER_MODE_CBC */
1335
1336 CHK(mbedtls_cipher_setkey(&t_in->cipher_ctx_enc, key0,
1337 keylen << 3, MBEDTLS_ENCRYPT) == 0);
1338 CHK(mbedtls_cipher_setkey(&t_in->cipher_ctx_dec, key1,
1339 keylen << 3, MBEDTLS_DECRYPT) == 0);
1340 CHK(mbedtls_cipher_setkey(&t_out->cipher_ctx_enc, key1,
1341 keylen << 3, MBEDTLS_ENCRYPT) == 0);
1342 CHK(mbedtls_cipher_setkey(&t_out->cipher_ctx_dec, key0,
1343 keylen << 3, MBEDTLS_DECRYPT) == 0);
1344#endif
1345
1346 /* Setup MAC contexts */
1347#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
1348 if (cipher_info->mode == MBEDTLS_MODE_CBC ||
1349 cipher_info->mode == MBEDTLS_MODE_STREAM) {
1350#if !defined(MBEDTLS_USE_PSA_CRYPTO)
1351 mbedtls_md_info_t const *md_info = mbedtls_md_info_from_type(hash_id);
1352 CHK(md_info != NULL);
1353#endif
1354 maclen = mbedtls_hash_info_get_size(hash_id);
1355 CHK(maclen != 0);
1356 /* Pick hash keys */
1357 CHK((md0 = mbedtls_calloc(1, maclen)) != NULL);
1358 CHK((md1 = mbedtls_calloc(1, maclen)) != NULL);
1359 memset(md0, 0x5, maclen);
1360 memset(md1, 0x6, maclen);
1361
1362#if defined(MBEDTLS_USE_PSA_CRYPTO)
1363 alg = mbedtls_hash_info_psa_from_md(hash_id);
1364
1365 CHK(alg != 0);
1366
1367 t_out->psa_mac_alg = PSA_ALG_HMAC(alg);
1368 t_in->psa_mac_alg = PSA_ALG_HMAC(alg);
1369 t_in->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
1370 t_out->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
1371 t_in->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
1372 t_out->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
1373
1374 psa_reset_key_attributes(&attributes);
1375 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
1376 psa_set_key_algorithm(&attributes, PSA_ALG_HMAC(alg));
1377 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
1378
1379 CHK(psa_import_key(&attributes,
1380 md0, maclen,
1381 &t_in->psa_mac_enc) == PSA_SUCCESS);
1382
1383 CHK(psa_import_key(&attributes,
1384 md1, maclen,
1385 &t_out->psa_mac_enc) == PSA_SUCCESS);
1386
1387 if (cipher_info->mode == MBEDTLS_MODE_STREAM ||
1388 etm == MBEDTLS_SSL_ETM_DISABLED) {
1389 /* mbedtls_ct_hmac() requires the key to be exportable */
1390 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT |
1391 PSA_KEY_USAGE_VERIFY_HASH);
1392 } else {
1393 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
1394 }
1395
1396 CHK(psa_import_key(&attributes,
1397 md1, maclen,
1398 &t_in->psa_mac_dec) == PSA_SUCCESS);
1399
1400 CHK(psa_import_key(&attributes,
1401 md0, maclen,
1402 &t_out->psa_mac_dec) == PSA_SUCCESS);
1403#else
1404 CHK(mbedtls_md_setup(&t_out->md_ctx_enc, md_info, 1) == 0);
1405 CHK(mbedtls_md_setup(&t_out->md_ctx_dec, md_info, 1) == 0);
1406 CHK(mbedtls_md_setup(&t_in->md_ctx_enc, md_info, 1) == 0);
1407 CHK(mbedtls_md_setup(&t_in->md_ctx_dec, md_info, 1) == 0);
1408
1409 CHK(mbedtls_md_hmac_starts(&t_in->md_ctx_enc,
1410 md0, maclen) == 0);
1411 CHK(mbedtls_md_hmac_starts(&t_in->md_ctx_dec,
1412 md1, maclen) == 0);
1413 CHK(mbedtls_md_hmac_starts(&t_out->md_ctx_enc,
1414 md1, maclen) == 0);
1415 CHK(mbedtls_md_hmac_starts(&t_out->md_ctx_dec,
1416 md0, maclen) == 0);
1417#endif
1418 }
1419#else
1420 ((void) hash_id);
1421#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
1422
1423
1424 /* Pick IV's (regardless of whether they
1425 * are being used by the transform). */
1426 ivlen = cipher_info->iv_size;
1427 memset(iv_enc, 0x3, sizeof(iv_enc));
1428 memset(iv_dec, 0x4, sizeof(iv_dec));
1429
1430 /*
1431 * Setup transforms
1432 */
1433
1434#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
1435 defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
1436 t_out->encrypt_then_mac = etm;
1437 t_in->encrypt_then_mac = etm;
1438#else
1439 ((void) etm);
1440#endif
1441
1442 t_out->tls_version = tls_version;
1443 t_in->tls_version = tls_version;
1444 t_out->ivlen = ivlen;
1445 t_in->ivlen = ivlen;
1446
1447 switch (cipher_info->mode) {
1448 case MBEDTLS_MODE_GCM:
1449 case MBEDTLS_MODE_CCM:
1450#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1451 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
1452 t_out->fixed_ivlen = 12;
1453 t_in->fixed_ivlen = 12;
1454 } else
1455#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1456 {
1457 t_out->fixed_ivlen = 4;
1458 t_in->fixed_ivlen = 4;
1459 }
1460 t_out->maclen = 0;
1461 t_in->maclen = 0;
1462 switch (tag_mode) {
1463 case 0: /* Full tag */
1464 t_out->taglen = 16;
1465 t_in->taglen = 16;
1466 break;
1467 case 1: /* Partial tag */
1468 t_out->taglen = 8;
1469 t_in->taglen = 8;
1470 break;
1471 default:
1472 ret = 1;
1473 goto cleanup;
1474 }
1475 break;
1476
1477 case MBEDTLS_MODE_CHACHAPOLY:
1478 t_out->fixed_ivlen = 12;
1479 t_in->fixed_ivlen = 12;
1480 t_out->maclen = 0;
1481 t_in->maclen = 0;
1482 switch (tag_mode) {
1483 case 0: /* Full tag */
1484 t_out->taglen = 16;
1485 t_in->taglen = 16;
1486 break;
1487 case 1: /* Partial tag */
1488 t_out->taglen = 8;
1489 t_in->taglen = 8;
1490 break;
1491 default:
1492 ret = 1;
1493 goto cleanup;
1494 }
1495 break;
1496
1497 case MBEDTLS_MODE_STREAM:
1498 case MBEDTLS_MODE_CBC:
1499 t_out->fixed_ivlen = 0; /* redundant, must be 0 */
1500 t_in->fixed_ivlen = 0; /* redundant, must be 0 */
1501 t_out->taglen = 0;
1502 t_in->taglen = 0;
1503 switch (tag_mode) {
1504 case 0: /* Full tag */
1505 t_out->maclen = maclen;
1506 t_in->maclen = maclen;
1507 break;
1508 default:
1509 ret = 1;
1510 goto cleanup;
1511 }
1512 break;
1513 default:
1514 ret = 1;
1515 goto cleanup;
1516 break;
1517 }
1518
1519 /* Setup IV's */
1520
1521 memcpy(&t_in->iv_dec, iv_dec, sizeof(iv_dec));
1522 memcpy(&t_in->iv_enc, iv_enc, sizeof(iv_enc));
1523 memcpy(&t_out->iv_dec, iv_enc, sizeof(iv_enc));
1524 memcpy(&t_out->iv_enc, iv_dec, sizeof(iv_dec));
1525
1526#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
1527 /* Add CID */
1528 memcpy(&t_in->in_cid, cid0, cid0_len);
1529 memcpy(&t_in->out_cid, cid1, cid1_len);
1530 t_in->in_cid_len = cid0_len;
1531 t_in->out_cid_len = cid1_len;
1532 memcpy(&t_out->in_cid, cid1, cid1_len);
1533 memcpy(&t_out->out_cid, cid0, cid0_len);
1534 t_out->in_cid_len = cid1_len;
1535 t_out->out_cid_len = cid0_len;
1536#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
1537
1538#if defined(MBEDTLS_USE_PSA_CRYPTO)
1539 status = mbedtls_ssl_cipher_to_psa(cipher_type,
1540 t_in->taglen,
1541 &alg,
1542 &key_type,
1543 &key_bits);
1544
1545 if (status != PSA_SUCCESS) {
1546 ret = PSA_TO_MBEDTLS_ERR(status);
1547 goto cleanup;
1548 }
1549
1550 t_in->psa_alg = alg;
1551 t_out->psa_alg = alg;
1552
1553 if (alg != MBEDTLS_SSL_NULL_CIPHER) {
1554 psa_reset_key_attributes(&attributes);
1555 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
1556 psa_set_key_algorithm(&attributes, alg);
1557 psa_set_key_type(&attributes, key_type);
1558
1559 status = psa_import_key(&attributes,
1560 key0,
1561 PSA_BITS_TO_BYTES(key_bits),
1562 &t_in->psa_key_enc);
1563
1564 if (status != PSA_SUCCESS) {
1565 ret = PSA_TO_MBEDTLS_ERR(status);
1566 goto cleanup;
1567 }
1568
1569 status = psa_import_key(&attributes,
1570 key1,
1571 PSA_BITS_TO_BYTES(key_bits),
1572 &t_out->psa_key_enc);
1573
1574 if (status != PSA_SUCCESS) {
1575 ret = PSA_TO_MBEDTLS_ERR(status);
1576 goto cleanup;
1577 }
1578
1579 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
1580
1581 status = psa_import_key(&attributes,
1582 key1,
1583 PSA_BITS_TO_BYTES(key_bits),
1584 &t_in->psa_key_dec);
1585
1586 if (status != PSA_SUCCESS) {
1587 ret = PSA_TO_MBEDTLS_ERR(status);
1588 goto cleanup;
1589 }
1590
1591 status = psa_import_key(&attributes,
1592 key0,
1593 PSA_BITS_TO_BYTES(key_bits),
1594 &t_out->psa_key_dec);
1595
1596 if (status != PSA_SUCCESS) {
1597 ret = PSA_TO_MBEDTLS_ERR(status);
1598 goto cleanup;
1599 }
1600 }
1601#endif /* MBEDTLS_USE_PSA_CRYPTO */
1602
1603cleanup:
1604
1605 mbedtls_free(key0);
1606 mbedtls_free(key1);
1607
1608 mbedtls_free(md0);
1609 mbedtls_free(md1);
1610
1611 return ret;
1612}
1613
1614/*
1615 * Populate a session structure for serialization tests.
1616 * Choose dummy values, mostly non-0 to distinguish from the init default.
1617 */
1618int mbedtls_test_ssl_tls12_populate_session(mbedtls_ssl_session *session,
1619 int ticket_len,
1620 const char *crt_file)
1621{
1622#if defined(MBEDTLS_HAVE_TIME)
1623 session->start = mbedtls_time(NULL) - 42;
1624#endif
1625 session->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
1626 session->ciphersuite = 0xabcd;
1627 session->id_len = sizeof(session->id);
1628 memset(session->id, 66, session->id_len);
1629 memset(session->master, 17, sizeof(session->master));
1630
1631#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) && defined(MBEDTLS_FS_IO)
1632 if (crt_file != NULL && strlen(crt_file) != 0) {
1633 mbedtls_x509_crt tmp_crt;
1634 int ret;
1635
1636 mbedtls_x509_crt_init(&tmp_crt);
1637 ret = mbedtls_x509_crt_parse_file(&tmp_crt, crt_file);
1638 if (ret != 0) {
1639 return ret;
1640 }
1641
1642#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1643 /* Move temporary CRT. */
1644 session->peer_cert = mbedtls_calloc(1, sizeof(*session->peer_cert));
1645 if (session->peer_cert == NULL) {
1646 return -1;
1647 }
1648 *session->peer_cert = tmp_crt;
1649 memset(&tmp_crt, 0, sizeof(tmp_crt));
1650#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
1651 /* Calculate digest of temporary CRT. */
1652 session->peer_cert_digest =
1653 mbedtls_calloc(1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN);
1654 if (session->peer_cert_digest == NULL) {
1655 return -1;
1656 }
1657
1658#if defined(MBEDTLS_USE_PSA_CRYPTO)
1659 psa_algorithm_t psa_alg = mbedtls_hash_info_psa_from_md(
1660 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE);
1661 size_t hash_size = 0;
1662 psa_status_t status = psa_hash_compute(
1663 psa_alg, tmp_crt.raw.p,
1664 tmp_crt.raw.len,
1665 session->peer_cert_digest,
1666 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN,
1667 &hash_size);
1668 ret = PSA_TO_MBEDTLS_ERR(status);
1669#else
1670 ret = mbedtls_md(mbedtls_md_info_from_type(
1671 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE),
1672 tmp_crt.raw.p, tmp_crt.raw.len,
1673 session->peer_cert_digest);
1674#endif /* MBEDTLS_USE_PSA_CRYPTO */
1675 if (ret != 0) {
1676 return ret;
1677 }
1678 session->peer_cert_digest_type =
1679 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
1680 session->peer_cert_digest_len =
1681 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
1682#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
1683
1684 mbedtls_x509_crt_free(&tmp_crt);
1685 }
1686#else /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED && MBEDTLS_FS_IO */
1687 (void) crt_file;
1688#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED && MBEDTLS_FS_IO */
1689 session->verify_result = 0xdeadbeef;
1690
1691#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
1692 if (ticket_len != 0) {
1693 session->ticket = mbedtls_calloc(1, ticket_len);
1694 if (session->ticket == NULL) {
1695 return -1;
1696 }
1697 memset(session->ticket, 33, ticket_len);
1698 }
1699 session->ticket_len = ticket_len;
1700 session->ticket_lifetime = 86401;
1701#else
1702 (void) ticket_len;
1703#endif
1704
1705#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1706 session->mfl_code = 1;
1707#endif
1708#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1709 session->encrypt_then_mac = 1;
1710#endif
1711
1712 return 0;
1713}
1714
1715#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1716int mbedtls_test_ssl_tls13_populate_session(mbedtls_ssl_session *session,
1717 int ticket_len,
1718 int endpoint_type)
1719{
1720 ((void) ticket_len);
1721 session->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
1722 session->endpoint = endpoint_type == MBEDTLS_SSL_IS_CLIENT ?
1723 MBEDTLS_SSL_IS_CLIENT : MBEDTLS_SSL_IS_SERVER;
1724 session->ciphersuite = 0xabcd;
1725 session->ticket_age_add = 0x87654321;
1726 session->ticket_flags = 0x7;
1727
1728 session->resumption_key_len = 32;
1729 memset(session->resumption_key, 0x99, sizeof(session->resumption_key));
1730
1731#if defined(MBEDTLS_HAVE_TIME)
1732 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
1733 session->start = mbedtls_time(NULL) - 42;
1734 }
1735#endif
1736
1737#if defined(MBEDTLS_SSL_CLI_C)
1738 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
1739#if defined(MBEDTLS_HAVE_TIME)
1740 session->ticket_received = mbedtls_time(NULL) - 40;
1741#endif
1742 session->ticket_lifetime = 0xfedcba98;
1743
1744 session->ticket_len = ticket_len;
1745 if (ticket_len != 0) {
1746 session->ticket = mbedtls_calloc(1, ticket_len);
1747 if (session->ticket == NULL) {
1748 return -1;
1749 }
1750 memset(session->ticket, 33, ticket_len);
1751 }
1752 }
1753#endif /* MBEDTLS_SSL_CLI_C */
1754
1755 return 0;
1756}
1757#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1758
1759/*
1760 * Perform data exchanging between \p ssl_1 and \p ssl_2 and check if the
1761 * message was sent in the correct number of fragments.
1762 *
1763 * /p ssl_1 and /p ssl_2 Endpoints represented by mbedtls_ssl_context. Both
1764 * of them must be initialized and connected
1765 * beforehand.
1766 * /p msg_len_1 and /p msg_len_2 specify the size of the message to send.
1767 * /p expected_fragments_1 and /p expected_fragments_2 determine in how many
1768 * fragments the message should be sent.
1769 * expected_fragments is 0: can be used for DTLS testing while the message
1770 * size is larger than MFL. In that case the message
1771 * cannot be fragmented and sent to the second
1772 * endpoint.
1773 * This value can be used for negative tests.
1774 * expected_fragments is 1: can be used for TLS/DTLS testing while the
1775 * message size is below MFL
1776 * expected_fragments > 1: can be used for TLS testing while the message
1777 * size is larger than MFL
1778 *
1779 * \retval 0 on success, otherwise error code.
1780 */
1781int mbedtls_exchange_data(mbedtls_ssl_context *ssl_1,
1782 int msg_len_1, const int expected_fragments_1,
1783 mbedtls_ssl_context *ssl_2,
1784 int msg_len_2, const int expected_fragments_2)
1785{
1786 unsigned char *msg_buf_1 = malloc(msg_len_1);
1787 unsigned char *msg_buf_2 = malloc(msg_len_2);
1788 unsigned char *in_buf_1 = malloc(msg_len_2);
1789 unsigned char *in_buf_2 = malloc(msg_len_1);
1790 int msg_type, ret = -1;
1791
1792 /* Perform this test with two message types. At first use a message
1793 * consisting of only 0x00 for the client and only 0xFF for the server.
1794 * At the second time use message with generated data */
1795 for (msg_type = 0; msg_type < 2; msg_type++) {
1796 int written_1 = 0;
1797 int written_2 = 0;
1798 int read_1 = 0;
1799 int read_2 = 0;
1800 int fragments_1 = 0;
1801 int fragments_2 = 0;
1802
1803 if (msg_type == 0) {
1804 memset(msg_buf_1, 0x00, msg_len_1);
1805 memset(msg_buf_2, 0xff, msg_len_2);
1806 } else {
1807 int i, j = 0;
1808 for (i = 0; i < msg_len_1; i++) {
1809 msg_buf_1[i] = j++ & 0xFF;
1810 }
1811 for (i = 0; i < msg_len_2; i++) {
1812 msg_buf_2[i] = (j -= 5) & 0xFF;
1813 }
1814 }
1815
1816 while (read_1 < msg_len_2 || read_2 < msg_len_1) {
1817 /* ssl_1 sending */
1818 if (msg_len_1 > written_1) {
1819 ret = mbedtls_ssl_write_fragment(ssl_1, msg_buf_1,
1820 msg_len_1, &written_1,
1821 expected_fragments_1);
1822 if (expected_fragments_1 == 0) {
1823 /* This error is expected when the message is too large and
1824 * cannot be fragmented */
1825 TEST_ASSERT(ret == MBEDTLS_ERR_SSL_BAD_INPUT_DATA);
1826 msg_len_1 = 0;
1827 } else {
1828 TEST_ASSERT(ret == 0);
1829 }
1830 }
1831
1832 /* ssl_2 sending */
1833 if (msg_len_2 > written_2) {
1834 ret = mbedtls_ssl_write_fragment(ssl_2, msg_buf_2,
1835 msg_len_2, &written_2,
1836 expected_fragments_2);
1837 if (expected_fragments_2 == 0) {
1838 /* This error is expected when the message is too large and
1839 * cannot be fragmented */
1840 TEST_ASSERT(ret == MBEDTLS_ERR_SSL_BAD_INPUT_DATA);
1841 msg_len_2 = 0;
1842 } else {
1843 TEST_ASSERT(ret == 0);
1844 }
1845 }
1846
1847 /* ssl_1 reading */
1848 if (read_1 < msg_len_2) {
1849 ret = mbedtls_ssl_read_fragment(ssl_1, in_buf_1,
1850 msg_len_2, &read_1,
1851 &fragments_2,
1852 expected_fragments_2);
1853 TEST_ASSERT(ret == 0);
1854 }
1855
1856 /* ssl_2 reading */
1857 if (read_2 < msg_len_1) {
1858 ret = mbedtls_ssl_read_fragment(ssl_2, in_buf_2,
1859 msg_len_1, &read_2,
1860 &fragments_1,
1861 expected_fragments_1);
1862 TEST_ASSERT(ret == 0);
1863 }
1864 }
1865
1866 ret = -1;
1867 TEST_ASSERT(0 == memcmp(msg_buf_1, in_buf_2, msg_len_1));
1868 TEST_ASSERT(0 == memcmp(msg_buf_2, in_buf_1, msg_len_2));
1869 TEST_ASSERT(fragments_1 == expected_fragments_1);
1870 TEST_ASSERT(fragments_2 == expected_fragments_2);
1871 }
1872
1873 ret = 0;
1874
1875exit:
1876 free(msg_buf_1);
1877 free(in_buf_1);
1878 free(msg_buf_2);
1879 free(in_buf_2);
1880
1881 return ret;
1882}
1883
1884/*
1885 * Perform data exchanging between \p ssl_1 and \p ssl_2. Both of endpoints
1886 * must be initialized and connected beforehand.
1887 *
1888 * \retval 0 on success, otherwise error code.
1889 */
1890int exchange_data(mbedtls_ssl_context *ssl_1,
1891 mbedtls_ssl_context *ssl_2)
1892{
1893 return mbedtls_exchange_data(ssl_1, 256, 1,
1894 ssl_2, 256, 1);
1895}
1896
1897#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
1898static int check_ssl_version(
1899 mbedtls_ssl_protocol_version expected_negotiated_version,
1900 const mbedtls_ssl_context *ssl)
1901{
1902 const char *version_string = mbedtls_ssl_get_version(ssl);
1903 mbedtls_ssl_protocol_version version_number =
1904 mbedtls_ssl_get_version_number(ssl);
1905
1906 TEST_EQUAL(ssl->tls_version, expected_negotiated_version);
1907
1908 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1909 TEST_EQUAL(version_string[0], 'D');
1910 ++version_string;
1911 }
1912
1913 switch (expected_negotiated_version) {
1914 case MBEDTLS_SSL_VERSION_TLS1_2:
1915 TEST_EQUAL(version_number, MBEDTLS_SSL_VERSION_TLS1_2);
1916 TEST_ASSERT(strcmp(version_string, "TLSv1.2") == 0);
1917 break;
1918
1919 case MBEDTLS_SSL_VERSION_TLS1_3:
1920 TEST_EQUAL(version_number, MBEDTLS_SSL_VERSION_TLS1_3);
1921 TEST_ASSERT(strcmp(version_string, "TLSv1.3") == 0);
1922 break;
1923
1924 default:
1925 TEST_ASSERT(
1926 !"Version check not implemented for this protocol version");
1927 }
1928
1929 return 1;
1930
1931exit:
1932 return 0;
1933}
1934#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
1935
1936
1937#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
1938void mbedtls_test_ssl_perform_handshake(
1939 mbedtls_test_handshake_test_options *options)
1940{
1941 /* forced_ciphersuite needs to last until the end of the handshake */
1942 int forced_ciphersuite[2];
1943 enum { BUFFSIZE = 17000 };
1944 mbedtls_test_ssl_endpoint client, server;
1945#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
1946 const char *psk_identity = "foo";
1947#endif
1948#if defined(MBEDTLS_TIMING_C)
1949 mbedtls_timing_delay_context timer_client, timer_server;
1950#endif
1951#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
1952 unsigned char *context_buf = NULL;
1953 size_t context_buf_len;
1954#endif
1955#if defined(MBEDTLS_SSL_RENEGOTIATION)
1956 int ret = -1;
1957#endif
1958 int expected_handshake_result = options->expected_handshake_result;
1959
1960 USE_PSA_INIT();
1961 mbedtls_platform_zeroize(&client, sizeof(client));
1962 mbedtls_platform_zeroize(&server, sizeof(server));
1963 mbedtls_test_ssl_message_queue server_queue, client_queue;
1964 mbedtls_test_message_socket_context server_context, client_context;
1965 mbedtls_test_message_socket_init(&server_context);
1966 mbedtls_test_message_socket_init(&client_context);
1967
1968 /* Client side */
1969 if (options->dtls != 0) {
1970 TEST_ASSERT(mbedtls_test_ssl_endpoint_init(&client,
1971 MBEDTLS_SSL_IS_CLIENT,
1972 options, &client_context,
1973 &client_queue,
1974 &server_queue, NULL) == 0);
1975#if defined(MBEDTLS_TIMING_C)
1976 mbedtls_ssl_set_timer_cb(&client.ssl, &timer_client,
1977 mbedtls_timing_set_delay,
1978 mbedtls_timing_get_delay);
1979#endif
1980 } else {
1981 TEST_ASSERT(mbedtls_test_ssl_endpoint_init(&client,
1982 MBEDTLS_SSL_IS_CLIENT,
1983 options, NULL, NULL,
1984 NULL, NULL) == 0);
1985 }
1986
1987 if (options->client_min_version != MBEDTLS_SSL_VERSION_UNKNOWN) {
1988 mbedtls_ssl_conf_min_tls_version(&client.conf,
1989 options->client_min_version);
1990 }
1991
1992 if (options->client_max_version != MBEDTLS_SSL_VERSION_UNKNOWN) {
1993 mbedtls_ssl_conf_max_tls_version(&client.conf,
1994 options->client_max_version);
1995 }
1996
1997 if (strlen(options->cipher) > 0) {
1998 set_ciphersuite(&client.conf, options->cipher, forced_ciphersuite);
1999 }
2000
2001#if defined(MBEDTLS_DEBUG_C)
2002 if (options->cli_log_fun) {
2003 mbedtls_debug_set_threshold(4);
2004 mbedtls_ssl_conf_dbg(&client.conf, options->cli_log_fun,
2005 options->cli_log_obj);
2006 }
2007#endif
2008
2009 /* Server side */
2010 if (options->dtls != 0) {
2011 TEST_ASSERT(mbedtls_test_ssl_endpoint_init(&server,
2012 MBEDTLS_SSL_IS_SERVER,
2013 options, &server_context,
2014 &server_queue,
2015 &client_queue, NULL) == 0);
2016#if defined(MBEDTLS_TIMING_C)
2017 mbedtls_ssl_set_timer_cb(&server.ssl, &timer_server,
2018 mbedtls_timing_set_delay,
2019 mbedtls_timing_get_delay);
2020#endif
2021 } else {
2022 TEST_ASSERT(mbedtls_test_ssl_endpoint_init(&server,
2023 MBEDTLS_SSL_IS_SERVER,
2024 options, NULL, NULL, NULL,
2025 NULL) == 0);
2026 }
2027
2028 mbedtls_ssl_conf_authmode(&server.conf, options->srv_auth_mode);
2029
2030 if (options->server_min_version != MBEDTLS_SSL_VERSION_UNKNOWN) {
2031 mbedtls_ssl_conf_min_tls_version(&server.conf,
2032 options->server_min_version);
2033 }
2034
2035 if (options->server_max_version != MBEDTLS_SSL_VERSION_UNKNOWN) {
2036 mbedtls_ssl_conf_max_tls_version(&server.conf,
2037 options->server_max_version);
2038 }
2039
2040#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
2041 TEST_ASSERT(mbedtls_ssl_conf_max_frag_len(&(server.conf),
2042 (unsigned char) options->mfl)
2043 == 0);
2044 TEST_ASSERT(mbedtls_ssl_conf_max_frag_len(&(client.conf),
2045 (unsigned char) options->mfl)
2046 == 0);
2047#else
2048 TEST_ASSERT(MBEDTLS_SSL_MAX_FRAG_LEN_NONE == options->mfl);
2049#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2050
2051#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
2052 if (options->psk_str != NULL && options->psk_str->len > 0) {
2053 TEST_ASSERT(mbedtls_ssl_conf_psk(
2054 &client.conf, options->psk_str->x,
2055 options->psk_str->len,
2056 (const unsigned char *) psk_identity,
2057 strlen(psk_identity)) == 0);
2058
2059 TEST_ASSERT(mbedtls_ssl_conf_psk(
2060 &server.conf, options->psk_str->x,
2061 options->psk_str->len,
2062 (const unsigned char *) psk_identity,
2063 strlen(psk_identity)) == 0);
2064#if defined(MBEDTLS_SSL_SRV_C)
2065 mbedtls_ssl_conf_psk_cb(&server.conf, psk_dummy_callback, NULL);
2066#endif
2067 }
2068#endif
2069#if defined(MBEDTLS_SSL_RENEGOTIATION)
2070 if (options->renegotiate) {
2071 mbedtls_ssl_conf_renegotiation(&(server.conf),
2072 MBEDTLS_SSL_RENEGOTIATION_ENABLED);
2073 mbedtls_ssl_conf_renegotiation(&(client.conf),
2074 MBEDTLS_SSL_RENEGOTIATION_ENABLED);
2075
2076 mbedtls_ssl_conf_legacy_renegotiation(&(server.conf),
2077 options->legacy_renegotiation);
2078 mbedtls_ssl_conf_legacy_renegotiation(&(client.conf),
2079 options->legacy_renegotiation);
2080 }
2081#endif /* MBEDTLS_SSL_RENEGOTIATION */
2082
2083#if defined(MBEDTLS_DEBUG_C)
2084 if (options->srv_log_fun) {
2085 mbedtls_debug_set_threshold(4);
2086 mbedtls_ssl_conf_dbg(&server.conf, options->srv_log_fun,
2087 options->srv_log_obj);
2088 }
2089#endif
2090
2091 TEST_ASSERT(mbedtls_test_mock_socket_connect(&(client.socket),
2092 &(server.socket),
2093 BUFFSIZE) == 0);
2094
2095#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2096 if (options->resize_buffers != 0) {
2097 /* Ensure that the buffer sizes are appropriate before resizes */
2098 TEST_ASSERT(client.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN);
2099 TEST_ASSERT(client.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN);
2100 TEST_ASSERT(server.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN);
2101 TEST_ASSERT(server.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN);
2102 }
2103#endif
2104
2105 if (options->expected_negotiated_version == MBEDTLS_SSL_VERSION_UNKNOWN) {
2106 expected_handshake_result = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
2107 }
2108
2109 TEST_ASSERT(mbedtls_test_move_handshake_to_state(&(client.ssl),
2110 &(server.ssl),
2111 MBEDTLS_SSL_HANDSHAKE_OVER)
2112 == expected_handshake_result);
2113
2114 if (expected_handshake_result != 0) {
2115 /* Connection will have failed by this point, skip to cleanup */
2116 goto exit;
2117 }
2118
2119 TEST_ASSERT(mbedtls_ssl_is_handshake_over(&client.ssl) == 1);
2120
2121 /* Make sure server state is moved to HANDSHAKE_OVER also. */
2122 TEST_EQUAL(mbedtls_test_move_handshake_to_state(&(server.ssl),
2123 &(client.ssl),
2124 MBEDTLS_SSL_HANDSHAKE_OVER),
2125 0);
2126
2127 TEST_ASSERT(mbedtls_ssl_is_handshake_over(&server.ssl) == 1);
2128 /* Check that both sides have negotiated the expected version. */
2129 mbedtls_test_set_step(0);
2130 if (!check_ssl_version(options->expected_negotiated_version,
2131 &client.ssl)) {
2132 goto exit;
2133 }
2134
2135 mbedtls_test_set_step(1);
2136 if (!check_ssl_version(options->expected_negotiated_version,
2137 &server.ssl)) {
2138 goto exit;
2139 }
2140
2141 if (options->expected_ciphersuite != 0) {
2142 TEST_EQUAL(server.ssl.session->ciphersuite,
2143 options->expected_ciphersuite);
2144 }
2145
2146#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2147 if (options->resize_buffers != 0) {
2148 /* A server, when using DTLS, might delay a buffer resize to happen
2149 * after it receives a message, so we force it. */
2150 TEST_ASSERT(exchange_data(&(client.ssl), &(server.ssl)) == 0);
2151
2152 TEST_ASSERT(client.ssl.out_buf_len ==
2153 mbedtls_ssl_get_output_buflen(&client.ssl));
2154 TEST_ASSERT(client.ssl.in_buf_len ==
2155 mbedtls_ssl_get_input_buflen(&client.ssl));
2156 TEST_ASSERT(server.ssl.out_buf_len ==
2157 mbedtls_ssl_get_output_buflen(&server.ssl));
2158 TEST_ASSERT(server.ssl.in_buf_len ==
2159 mbedtls_ssl_get_input_buflen(&server.ssl));
2160 }
2161#endif
2162
2163 if (options->cli_msg_len != 0 || options->srv_msg_len != 0) {
2164 /* Start data exchanging test */
2165 TEST_ASSERT(mbedtls_exchange_data(&(client.ssl), options->cli_msg_len,
2166 options->expected_cli_fragments,
2167 &(server.ssl), options->srv_msg_len,
2168 options->expected_srv_fragments)
2169 == 0);
2170 }
2171#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
2172 if (options->serialize == 1) {
2173 TEST_ASSERT(options->dtls == 1);
2174
2175 TEST_ASSERT(mbedtls_ssl_context_save(&(server.ssl), NULL,
2176 0, &context_buf_len)
2177 == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL);
2178
2179 context_buf = mbedtls_calloc(1, context_buf_len);
2180 TEST_ASSERT(context_buf != NULL);
2181
2182 TEST_ASSERT(mbedtls_ssl_context_save(&(server.ssl), context_buf,
2183 context_buf_len,
2184 &context_buf_len)
2185 == 0);
2186
2187 mbedtls_ssl_free(&(server.ssl));
2188 mbedtls_ssl_init(&(server.ssl));
2189
2190 TEST_ASSERT(mbedtls_ssl_setup(&(server.ssl), &(server.conf)) == 0);
2191
2192 mbedtls_ssl_set_bio(&(server.ssl), &server_context,
2193 mbedtls_test_mock_tcp_send_msg,
2194 mbedtls_test_mock_tcp_recv_msg,
2195 NULL);
2196
2197 mbedtls_ssl_set_user_data_p(&server.ssl, &server);
2198
2199#if defined(MBEDTLS_TIMING_C)
2200 mbedtls_ssl_set_timer_cb(&server.ssl, &timer_server,
2201 mbedtls_timing_set_delay,
2202 mbedtls_timing_get_delay);
2203#endif
2204#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2205 if (options->resize_buffers != 0) {
2206 /* Ensure that the buffer sizes are appropriate before resizes */
2207 TEST_ASSERT(server.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN);
2208 TEST_ASSERT(server.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN);
2209 }
2210#endif
2211 TEST_ASSERT(mbedtls_ssl_context_load(&(server.ssl), context_buf,
2212 context_buf_len) == 0);
2213
2214#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2215 /* Validate buffer sizes after context deserialization */
2216 if (options->resize_buffers != 0) {
2217 TEST_ASSERT(server.ssl.out_buf_len ==
2218 mbedtls_ssl_get_output_buflen(&server.ssl));
2219 TEST_ASSERT(server.ssl.in_buf_len ==
2220 mbedtls_ssl_get_input_buflen(&server.ssl));
2221 }
2222#endif
2223 /* Retest writing/reading */
2224 if (options->cli_msg_len != 0 || options->srv_msg_len != 0) {
2225 TEST_ASSERT(mbedtls_exchange_data(
2226 &(client.ssl),
2227 options->cli_msg_len,
2228 options->expected_cli_fragments,
2229 &(server.ssl),
2230 options->srv_msg_len,
2231 options->expected_srv_fragments)
2232 == 0);
2233 }
2234 }
2235#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
2236
2237#if defined(MBEDTLS_SSL_RENEGOTIATION)
2238 if (options->renegotiate) {
2239 /* Start test with renegotiation */
2240 TEST_ASSERT(server.ssl.renego_status ==
2241 MBEDTLS_SSL_INITIAL_HANDSHAKE);
2242 TEST_ASSERT(client.ssl.renego_status ==
2243 MBEDTLS_SSL_INITIAL_HANDSHAKE);
2244
2245 /* After calling this function for the server, it only sends a handshake
2246 * request. All renegotiation should happen during data exchanging */
2247 TEST_ASSERT(mbedtls_ssl_renegotiate(&(server.ssl)) == 0);
2248 TEST_ASSERT(server.ssl.renego_status ==
2249 MBEDTLS_SSL_RENEGOTIATION_PENDING);
2250 TEST_ASSERT(client.ssl.renego_status ==
2251 MBEDTLS_SSL_INITIAL_HANDSHAKE);
2252
2253 TEST_ASSERT(exchange_data(&(client.ssl), &(server.ssl)) == 0);
2254 TEST_ASSERT(server.ssl.renego_status ==
2255 MBEDTLS_SSL_RENEGOTIATION_DONE);
2256 TEST_ASSERT(client.ssl.renego_status ==
2257 MBEDTLS_SSL_RENEGOTIATION_DONE);
2258
2259 /* After calling mbedtls_ssl_renegotiate for the client,
2260 * all renegotiation should happen inside this function.
2261 * However in this test, we cannot perform simultaneous communication
2262 * between client and server so this function will return waiting error
2263 * on the socket. All rest of renegotiation should happen
2264 * during data exchanging */
2265 ret = mbedtls_ssl_renegotiate(&(client.ssl));
2266#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2267 if (options->resize_buffers != 0) {
2268 /* Ensure that the buffer sizes are appropriate before resizes */
2269 TEST_ASSERT(client.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN);
2270 TEST_ASSERT(client.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN);
2271 }
2272#endif
2273 TEST_ASSERT(ret == 0 ||
2274 ret == MBEDTLS_ERR_SSL_WANT_READ ||
2275 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
2276 TEST_ASSERT(server.ssl.renego_status ==
2277 MBEDTLS_SSL_RENEGOTIATION_DONE);
2278 TEST_ASSERT(client.ssl.renego_status ==
2279 MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS);
2280
2281 TEST_ASSERT(exchange_data(&(client.ssl), &(server.ssl)) == 0);
2282 TEST_ASSERT(server.ssl.renego_status ==
2283 MBEDTLS_SSL_RENEGOTIATION_DONE);
2284 TEST_ASSERT(client.ssl.renego_status ==
2285 MBEDTLS_SSL_RENEGOTIATION_DONE);
2286#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2287 /* Validate buffer sizes after renegotiation */
2288 if (options->resize_buffers != 0) {
2289 TEST_ASSERT(client.ssl.out_buf_len ==
2290 mbedtls_ssl_get_output_buflen(&client.ssl));
2291 TEST_ASSERT(client.ssl.in_buf_len ==
2292 mbedtls_ssl_get_input_buflen(&client.ssl));
2293 TEST_ASSERT(server.ssl.out_buf_len ==
2294 mbedtls_ssl_get_output_buflen(&server.ssl));
2295 TEST_ASSERT(server.ssl.in_buf_len ==
2296 mbedtls_ssl_get_input_buflen(&server.ssl));
2297 }
2298#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
2299 }
2300#endif /* MBEDTLS_SSL_RENEGOTIATION */
2301
2302 TEST_ASSERT(mbedtls_ssl_conf_get_user_data_p(&client.conf) == &client);
2303 TEST_ASSERT(mbedtls_ssl_get_user_data_p(&client.ssl) == &client);
2304 TEST_ASSERT(mbedtls_ssl_conf_get_user_data_p(&server.conf) == &server);
2305 TEST_ASSERT(mbedtls_ssl_get_user_data_p(&server.ssl) == &server);
2306
2307exit:
2308 mbedtls_test_ssl_endpoint_free(&client,
2309 options->dtls != 0 ?
2310 &client_context : NULL);
2311 mbedtls_test_ssl_endpoint_free(&server,
2312 options->dtls != 0 ?
2313 &server_context : NULL);
2314#if defined(MBEDTLS_DEBUG_C)
2315 if (options->cli_log_fun || options->srv_log_fun) {
2316 mbedtls_debug_set_threshold(0);
2317 }
2318#endif
2319#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
2320 if (context_buf != NULL) {
2321 mbedtls_free(context_buf);
2322 }
2323#endif
2324 USE_PSA_DONE();
2325}
2326#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
2327
2328#if defined(MBEDTLS_TEST_HOOKS)
2329/*
2330 * Tweak vector lengths in a TLS 1.3 Certificate message
2331 *
2332 * \param[in] buf Buffer containing the Certificate message to tweak
2333 * \param[in]]out] end End of the buffer to parse
2334 * \param tweak Tweak identifier (from 1 to the number of tweaks).
2335 * \param[out] expected_result Error code expected from the parsing function
2336 * \param[out] args Arguments of the MBEDTLS_SSL_CHK_BUF_READ_PTR call that
2337 * is expected to fail. All zeroes if no
2338 * MBEDTLS_SSL_CHK_BUF_READ_PTR failure is expected.
2339 */
2340int tweak_tls13_certificate_msg_vector_len(
2341 unsigned char *buf, unsigned char **end, int tweak,
2342 int *expected_result, mbedtls_ssl_chk_buf_ptr_args *args)
2343{
2344/*
2345 * The definition of the tweaks assume that the certificate list contains only
2346 * one certificate.
2347 */
2348
2349/*
2350 * struct {
2351 * opaque cert_data<1..2^24-1>;
2352 * Extension extensions<0..2^16-1>;
2353 * } CertificateEntry;
2354 *
2355 * struct {
2356 * opaque certificate_request_context<0..2^8-1>;
2357 * CertificateEntry certificate_list<0..2^24-1>;
2358 * } Certificate;
2359 */
2360 unsigned char *p_certificate_request_context_len = buf;
2361 size_t certificate_request_context_len = buf[0];
2362
2363 unsigned char *p_certificate_list_len =
2364 buf + 1 + certificate_request_context_len;
2365 unsigned char *certificate_list = p_certificate_list_len + 3;
2366 size_t certificate_list_len =
2367 MBEDTLS_GET_UINT24_BE(p_certificate_list_len, 0);
2368
2369 unsigned char *p_cert_data_len = certificate_list;
2370 unsigned char *cert_data = p_cert_data_len + 3;
2371 size_t cert_data_len = MBEDTLS_GET_UINT24_BE(p_cert_data_len, 0);
2372
2373 unsigned char *p_extensions_len = cert_data + cert_data_len;
2374 unsigned char *extensions = p_extensions_len + 2;
2375 size_t extensions_len = MBEDTLS_GET_UINT16_BE(p_extensions_len, 0);
2376
2377 *expected_result = MBEDTLS_ERR_SSL_DECODE_ERROR;
2378
2379 switch (tweak) {
2380 case 1:
2381 /* Failure when checking if the certificate request context length
2382 * and certificate list length can be read
2383 */
2384 *end = buf + 3;
2385 set_chk_buf_ptr_args(args, buf, *end, 4);
2386 break;
2387
2388 case 2:
2389 /* Invalid certificate request context length.
2390 */
2391 *p_certificate_request_context_len =
2392 certificate_request_context_len + 1;
2393 reset_chk_buf_ptr_args(args);
2394 break;
2395
2396 case 3:
2397 /* Failure when checking if certificate_list data can be read. */
2398 MBEDTLS_PUT_UINT24_BE(certificate_list_len + 1,
2399 p_certificate_list_len, 0);
2400 set_chk_buf_ptr_args(args, certificate_list, *end,
2401 certificate_list_len + 1);
2402 break;
2403
2404 case 4:
2405 /* Failure when checking if the cert_data length can be read. */
2406 MBEDTLS_PUT_UINT24_BE(2, p_certificate_list_len, 0);
2407 set_chk_buf_ptr_args(args, p_cert_data_len, certificate_list + 2, 3);
2408 break;
2409
2410 case 5:
2411 /* Failure when checking if cert_data data can be read. */
2412 MBEDTLS_PUT_UINT24_BE(certificate_list_len - 3 + 1,
2413 p_cert_data_len, 0);
2414 set_chk_buf_ptr_args(args, cert_data,
2415 certificate_list + certificate_list_len,
2416 certificate_list_len - 3 + 1);
2417 break;
2418
2419 case 6:
2420 /* Failure when checking if the extensions length can be read. */
2421 MBEDTLS_PUT_UINT24_BE(certificate_list_len - extensions_len - 1,
2422 p_certificate_list_len, 0);
2423 set_chk_buf_ptr_args(
2424 args, p_extensions_len,
2425 certificate_list + certificate_list_len - extensions_len - 1, 2);
2426 break;
2427
2428 case 7:
2429 /* Failure when checking if extensions data can be read. */
2430 MBEDTLS_PUT_UINT16_BE(extensions_len + 1, p_extensions_len, 0);
2431
2432 set_chk_buf_ptr_args(
2433 args, extensions,
2434 certificate_list + certificate_list_len, extensions_len + 1);
2435 break;
2436
2437 default:
2438 return -1;
2439 }
2440
2441 return 0;
2442}
2443#endif /* MBEDTLS_TEST_HOOKS */
Yanray Wang4d07d1c2022-10-27 15:28:16 +08002444#endif /* MBEDTLS_SSL_TLS_C */