Yanray Wang | 47907a4 | 2022-10-24 14:42:01 +0800 | [diff] [blame] | 1 | /** \file ssl_helpers.h |
| 2 | * |
| 3 | * \brief This file contains 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 | #ifndef SSL_HELPERS_H |
| 24 | #define SSL_HELPERS_H |
| 25 | |
| 26 | #include <test/helpers.h> |
Yanray Wang | 55a6619 | 2022-10-26 09:57:53 +0800 | [diff] [blame] | 27 | #include <mbedtls/ssl.h> |
| 28 | |
| 29 | #if defined(MBEDTLS_SSL_CACHE_C) |
| 30 | #include "mbedtls/ssl_cache.h" |
| 31 | #endif |
| 32 | |
| 33 | typedef struct mbedtls_test_ssl_log_pattern { |
| 34 | const char *pattern; |
| 35 | size_t counter; |
| 36 | } mbedtls_test_ssl_log_pattern; |
| 37 | |
| 38 | typedef struct mbedtls_test_handshake_test_options { |
| 39 | const char *cipher; |
| 40 | mbedtls_ssl_protocol_version client_min_version; |
| 41 | mbedtls_ssl_protocol_version client_max_version; |
| 42 | mbedtls_ssl_protocol_version server_min_version; |
| 43 | mbedtls_ssl_protocol_version server_max_version; |
| 44 | mbedtls_ssl_protocol_version expected_negotiated_version; |
| 45 | int expected_handshake_result; |
| 46 | int expected_ciphersuite; |
| 47 | int pk_alg; |
| 48 | int opaque_alg; |
| 49 | int opaque_alg2; |
| 50 | int opaque_usage; |
| 51 | data_t *psk_str; |
| 52 | int dtls; |
| 53 | int srv_auth_mode; |
| 54 | int serialize; |
| 55 | int mfl; |
| 56 | int cli_msg_len; |
| 57 | int srv_msg_len; |
| 58 | int expected_cli_fragments; |
| 59 | int expected_srv_fragments; |
| 60 | int renegotiate; |
| 61 | int legacy_renegotiation; |
| 62 | void *srv_log_obj; |
| 63 | void *cli_log_obj; |
| 64 | void (*srv_log_fun)(void *, int, const char *, int, const char *); |
| 65 | void (*cli_log_fun)(void *, int, const char *, int, const char *); |
| 66 | int resize_buffers; |
| 67 | #if defined(MBEDTLS_SSL_CACHE_C) |
| 68 | mbedtls_ssl_cache_context *cache; |
| 69 | #endif |
| 70 | } mbedtls_test_handshake_test_options; |
| 71 | |
| 72 | typedef struct mbedtls_test_ssl_buffer { |
| 73 | size_t start; |
| 74 | size_t content_length; |
| 75 | size_t capacity; |
| 76 | unsigned char *buffer; |
| 77 | } mbedtls_test_ssl_buffer; |
| 78 | |
| 79 | /* |
| 80 | * Context for a message metadata queue (fifo) that is on top of the ring buffer. |
| 81 | */ |
| 82 | typedef struct mbedtls_test_ssl_message_queue { |
| 83 | size_t *messages; |
| 84 | int pos; |
| 85 | int num; |
| 86 | int capacity; |
| 87 | } mbedtls_test_ssl_message_queue; |
| 88 | |
| 89 | /* |
| 90 | * Context for the I/O callbacks simulating network connection. |
| 91 | */ |
| 92 | |
| 93 | #define MBEDTLS_MOCK_SOCKET_CONNECTED 1 |
| 94 | |
| 95 | typedef struct mbedtls_test_mock_socket { |
| 96 | int status; |
| 97 | mbedtls_test_ssl_buffer *input; |
| 98 | mbedtls_test_ssl_buffer *output; |
| 99 | struct mbedtls_test_mock_socket *peer; |
| 100 | } mbedtls_test_mock_socket; |
| 101 | |
| 102 | /* Errors used in the message socket mocks */ |
| 103 | |
| 104 | #define MBEDTLS_TEST_ERROR_CONTEXT_ERROR -55 |
| 105 | #define MBEDTLS_TEST_ERROR_SEND_FAILED -66 |
| 106 | #define MBEDTLS_TEST_ERROR_RECV_FAILED -77 |
| 107 | |
| 108 | /* |
| 109 | * Structure used as an addon, or a wrapper, around the mocked sockets. |
| 110 | * Contains an input queue, to which the other socket pushes metadata, |
| 111 | * and an output queue, to which this one pushes metadata. This context is |
| 112 | * considered as an owner of the input queue only, which is initialized and |
| 113 | * freed in the respective setup and free calls. |
| 114 | */ |
| 115 | typedef struct mbedtls_test_message_socket_context { |
| 116 | mbedtls_test_ssl_message_queue *queue_input; |
| 117 | mbedtls_test_ssl_message_queue *queue_output; |
| 118 | mbedtls_test_mock_socket *socket; |
| 119 | } mbedtls_test_message_socket_context; |
| 120 | |
| 121 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
| 122 | |
| 123 | /* |
| 124 | * Structure with endpoint's certificates for SSL communication tests. |
| 125 | */ |
| 126 | typedef struct mbedtls_test_ssl_endpoint_certificate { |
| 127 | mbedtls_x509_crt *ca_cert; |
| 128 | mbedtls_x509_crt *cert; |
| 129 | mbedtls_pk_context *pkey; |
| 130 | } mbedtls_test_ssl_endpoint_certificate; |
| 131 | |
| 132 | /* |
| 133 | * Endpoint structure for SSL communication tests. |
| 134 | */ |
| 135 | typedef struct mbedtls_test_ssl_endpoint { |
| 136 | const char *name; |
| 137 | mbedtls_ssl_context ssl; |
| 138 | mbedtls_ssl_config conf; |
| 139 | mbedtls_test_mock_socket socket; |
| 140 | mbedtls_test_ssl_endpoint_certificate cert; |
| 141 | } mbedtls_test_ssl_endpoint; |
| 142 | |
| 143 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Yanray Wang | 47907a4 | 2022-10-24 14:42:01 +0800 | [diff] [blame] | 144 | |
| 145 | #endif /* SSL_HELPERS_H */ |