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 |
Dave Rodgman | 16799db | 2023-11-02 19:47:20 +0000 | [diff] [blame] | 8 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Yanray Wang | 47907a4 | 2022-10-24 14:42:01 +0800 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #ifndef SSL_HELPERS_H |
| 12 | #define SSL_HELPERS_H |
| 13 | |
Yanray Wang | 4d07d1c | 2022-10-27 15:28:16 +0800 | [diff] [blame] | 14 | #include "mbedtls/build_info.h" |
| 15 | |
| 16 | #include <string.h> |
| 17 | |
Yanray Wang | 47907a4 | 2022-10-24 14:42:01 +0800 | [diff] [blame] | 18 | #include <test/helpers.h> |
Yanray Wang | 4d07d1c | 2022-10-27 15:28:16 +0800 | [diff] [blame] | 19 | #include <test/macros.h> |
| 20 | #include <test/random.h> |
| 21 | #include <test/psa_crypto_helpers.h> |
| 22 | |
| 23 | #if defined(MBEDTLS_SSL_TLS_C) |
| 24 | #include <ssl_misc.h> |
| 25 | #include <mbedtls/timing.h> |
| 26 | #include <mbedtls/debug.h> |
Yanray Wang | 4d07d1c | 2022-10-27 15:28:16 +0800 | [diff] [blame] | 27 | |
| 28 | #include "test/certs.h" |
Yanray Wang | 55a6619 | 2022-10-26 09:57:53 +0800 | [diff] [blame] | 29 | |
| 30 | #if defined(MBEDTLS_SSL_CACHE_C) |
| 31 | #include "mbedtls/ssl_cache.h" |
| 32 | #endif |
| 33 | |
Yanray Wang | 5ba709c | 2023-02-03 11:07:56 +0800 | [diff] [blame] | 34 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 35 | #define PSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \ |
| 36 | psa_to_ssl_errors, \ |
| 37 | psa_generic_status_to_mbedtls) |
| 38 | #endif |
| 39 | |
Ronald Cron | 43263c0 | 2023-03-09 16:48:10 +0100 | [diff] [blame] | 40 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Elena Uziunaite | 6121a34 | 2024-07-05 11:16:53 +0100 | [diff] [blame] | 41 | #if defined(PSA_WANT_KEY_TYPE_AES) |
Elena Uziunaite | 83a0d9d | 2024-07-05 11:41:22 +0100 | [diff] [blame] | 42 | #if defined(PSA_WANT_ALG_GCM) |
Elena Uziunaite | b476d4b | 2024-05-23 15:33:41 +0100 | [diff] [blame] | 43 | #if defined(PSA_WANT_ALG_SHA_384) |
Ronald Cron | 43263c0 | 2023-03-09 16:48:10 +0100 | [diff] [blame] | 44 | #define MBEDTLS_TEST_HAS_TLS1_3_AES_256_GCM_SHA384 |
| 45 | #endif |
Elena Uziunaite | 0916cd7 | 2024-05-23 17:01:07 +0100 | [diff] [blame] | 46 | #if defined(PSA_WANT_ALG_SHA_256) |
Ronald Cron | 43263c0 | 2023-03-09 16:48:10 +0100 | [diff] [blame] | 47 | #define MBEDTLS_TEST_HAS_TLS1_3_AES_128_GCM_SHA256 |
| 48 | #endif |
Elena Uziunaite | 83a0d9d | 2024-07-05 11:41:22 +0100 | [diff] [blame] | 49 | #endif /* PSA_WANT_ALG_GCM */ |
Elena Uziunaite | c256172 | 2024-07-05 11:37:33 +0100 | [diff] [blame] | 50 | #if defined(PSA_WANT_ALG_CCM) && defined(PSA_WANT_ALG_SHA_256) |
Ronald Cron | 43263c0 | 2023-03-09 16:48:10 +0100 | [diff] [blame] | 51 | #define MBEDTLS_TEST_HAS_TLS1_3_AES_128_CCM_SHA256 |
| 52 | #define MBEDTLS_TEST_HAS_TLS1_3_AES_128_CCM_8_SHA256 |
| 53 | #endif |
Elena Uziunaite | 6121a34 | 2024-07-05 11:16:53 +0100 | [diff] [blame] | 54 | #endif /* PSA_WANT_KEY_TYPE_AES */ |
Elena Uziunaite | 5c70c30 | 2024-07-05 11:44:44 +0100 | [diff] [blame] | 55 | #if defined(PSA_WANT_ALG_CHACHA20_POLY1305) && defined(PSA_WANT_ALG_SHA_256) |
Ronald Cron | 43263c0 | 2023-03-09 16:48:10 +0100 | [diff] [blame] | 56 | #define MBEDTLS_TEST_HAS_TLS1_3_CHACHA20_POLY1305_SHA256 |
| 57 | #endif |
| 58 | |
| 59 | #if defined(MBEDTLS_TEST_HAS_TLS1_3_AES_256_GCM_SHA384) || \ |
| 60 | defined(MBEDTLS_TEST_HAS_TLS1_3_AES_128_GCM_SHA256) || \ |
| 61 | defined(MBEDTLS_TEST_HAS_TLS1_3_AES_128_CCM_SHA256) || \ |
| 62 | defined(MBEDTLS_TEST_HAS_TLS1_3_AES_128_CCM_8_SHA256) || \ |
| 63 | defined(MBEDTLS_TEST_HAS_TLS1_3_CHACHA20_POLY1305_SHA256) |
| 64 | #define MBEDTLS_TEST_AT_LEAST_ONE_TLS1_3_CIPHERSUITE |
| 65 | #endif |
| 66 | |
| 67 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 68 | |
Valerio Setti | 8438c63 | 2025-01-22 11:56:40 +0100 | [diff] [blame] | 69 | #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \ |
Yanray Wang | 5ba709c | 2023-02-03 11:07:56 +0800 | [diff] [blame] | 70 | defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) |
| 71 | #define MBEDTLS_CAN_HANDLE_RSA_TEST_KEY |
| 72 | #endif |
Gabor Mezei | 1495093 | 2025-03-06 16:06:42 +0100 | [diff] [blame] | 73 | |
Gabor Mezei | 8829aa3 | 2025-03-07 13:21:37 +0100 | [diff] [blame] | 74 | #if defined(PSA_WANT_ALG_GCM) || \ |
| 75 | defined(PSA_WANT_ALG_CCM) || \ |
Gabor Mezei | 1495093 | 2025-03-06 16:06:42 +0100 | [diff] [blame] | 76 | defined(PSA_WANT_ALG_CHACHA20_POLY1305) |
| 77 | #define MBEDTLS_TEST_HAS_AEAD_ALG |
| 78 | #endif |
| 79 | |
Yanray Wang | 5ba709c | 2023-02-03 11:07:56 +0800 | [diff] [blame] | 80 | enum { |
| 81 | #define MBEDTLS_SSL_TLS1_3_LABEL(name, string) \ |
| 82 | tls13_label_ ## name, |
| 83 | MBEDTLS_SSL_TLS1_3_LABEL_LIST |
| 84 | #undef MBEDTLS_SSL_TLS1_3_LABEL |
| 85 | }; |
| 86 | |
Waleed Elmelegy | 4dfb0e7 | 2024-03-14 01:48:40 +0000 | [diff] [blame] | 87 | #if defined(MBEDTLS_SSL_ALPN) |
| 88 | #define MBEDTLS_TEST_MAX_ALPN_LIST_SIZE 10 |
| 89 | #endif |
| 90 | |
Yanray Wang | 55a6619 | 2022-10-26 09:57:53 +0800 | [diff] [blame] | 91 | typedef struct mbedtls_test_ssl_log_pattern { |
| 92 | const char *pattern; |
| 93 | size_t counter; |
| 94 | } mbedtls_test_ssl_log_pattern; |
| 95 | |
| 96 | typedef struct mbedtls_test_handshake_test_options { |
| 97 | const char *cipher; |
Ronald Cron | fb53647 | 2024-01-26 14:55:25 +0100 | [diff] [blame] | 98 | uint16_t *group_list; |
Yanray Wang | 55a6619 | 2022-10-26 09:57:53 +0800 | [diff] [blame] | 99 | mbedtls_ssl_protocol_version client_min_version; |
| 100 | mbedtls_ssl_protocol_version client_max_version; |
| 101 | mbedtls_ssl_protocol_version server_min_version; |
| 102 | mbedtls_ssl_protocol_version server_max_version; |
| 103 | mbedtls_ssl_protocol_version expected_negotiated_version; |
| 104 | int expected_handshake_result; |
| 105 | int expected_ciphersuite; |
| 106 | int pk_alg; |
| 107 | int opaque_alg; |
| 108 | int opaque_alg2; |
| 109 | int opaque_usage; |
| 110 | data_t *psk_str; |
| 111 | int dtls; |
| 112 | int srv_auth_mode; |
| 113 | int serialize; |
| 114 | int mfl; |
| 115 | int cli_msg_len; |
| 116 | int srv_msg_len; |
| 117 | int expected_cli_fragments; |
| 118 | int expected_srv_fragments; |
| 119 | int renegotiate; |
| 120 | int legacy_renegotiation; |
| 121 | void *srv_log_obj; |
| 122 | void *cli_log_obj; |
| 123 | void (*srv_log_fun)(void *, int, const char *, int, const char *); |
| 124 | void (*cli_log_fun)(void *, int, const char *, int, const char *); |
| 125 | int resize_buffers; |
Ronald Cron | ced99be | 2024-01-26 15:49:12 +0100 | [diff] [blame] | 126 | int early_data; |
Ronald Cron | 5d3036e | 2024-02-23 07:43:45 +0100 | [diff] [blame] | 127 | int max_early_data_size; |
Yanray Wang | 55a6619 | 2022-10-26 09:57:53 +0800 | [diff] [blame] | 128 | #if defined(MBEDTLS_SSL_CACHE_C) |
| 129 | mbedtls_ssl_cache_context *cache; |
| 130 | #endif |
Waleed Elmelegy | 4dfb0e7 | 2024-03-14 01:48:40 +0000 | [diff] [blame] | 131 | #if defined(MBEDTLS_SSL_ALPN) |
| 132 | const char *alpn_list[MBEDTLS_TEST_MAX_ALPN_LIST_SIZE]; |
| 133 | #endif |
Yanray Wang | 55a6619 | 2022-10-26 09:57:53 +0800 | [diff] [blame] | 134 | } mbedtls_test_handshake_test_options; |
| 135 | |
Yanray Wang | 25b766f | 2023-03-15 16:39:05 +0800 | [diff] [blame] | 136 | /* |
| 137 | * Buffer structure for custom I/O callbacks. |
| 138 | */ |
Yanray Wang | 55a6619 | 2022-10-26 09:57:53 +0800 | [diff] [blame] | 139 | typedef struct mbedtls_test_ssl_buffer { |
| 140 | size_t start; |
| 141 | size_t content_length; |
| 142 | size_t capacity; |
| 143 | unsigned char *buffer; |
| 144 | } mbedtls_test_ssl_buffer; |
| 145 | |
| 146 | /* |
| 147 | * Context for a message metadata queue (fifo) that is on top of the ring buffer. |
| 148 | */ |
| 149 | typedef struct mbedtls_test_ssl_message_queue { |
| 150 | size_t *messages; |
| 151 | int pos; |
| 152 | int num; |
| 153 | int capacity; |
| 154 | } mbedtls_test_ssl_message_queue; |
| 155 | |
| 156 | /* |
| 157 | * Context for the I/O callbacks simulating network connection. |
| 158 | */ |
| 159 | |
| 160 | #define MBEDTLS_MOCK_SOCKET_CONNECTED 1 |
| 161 | |
| 162 | typedef struct mbedtls_test_mock_socket { |
| 163 | int status; |
| 164 | mbedtls_test_ssl_buffer *input; |
| 165 | mbedtls_test_ssl_buffer *output; |
| 166 | struct mbedtls_test_mock_socket *peer; |
| 167 | } mbedtls_test_mock_socket; |
| 168 | |
| 169 | /* Errors used in the message socket mocks */ |
| 170 | |
| 171 | #define MBEDTLS_TEST_ERROR_CONTEXT_ERROR -55 |
| 172 | #define MBEDTLS_TEST_ERROR_SEND_FAILED -66 |
| 173 | #define MBEDTLS_TEST_ERROR_RECV_FAILED -77 |
| 174 | |
| 175 | /* |
| 176 | * Structure used as an addon, or a wrapper, around the mocked sockets. |
| 177 | * Contains an input queue, to which the other socket pushes metadata, |
| 178 | * and an output queue, to which this one pushes metadata. This context is |
| 179 | * considered as an owner of the input queue only, which is initialized and |
| 180 | * freed in the respective setup and free calls. |
| 181 | */ |
| 182 | typedef struct mbedtls_test_message_socket_context { |
| 183 | mbedtls_test_ssl_message_queue *queue_input; |
| 184 | mbedtls_test_ssl_message_queue *queue_output; |
| 185 | mbedtls_test_mock_socket *socket; |
| 186 | } mbedtls_test_message_socket_context; |
| 187 | |
| 188 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
| 189 | |
| 190 | /* |
| 191 | * Structure with endpoint's certificates for SSL communication tests. |
| 192 | */ |
| 193 | typedef struct mbedtls_test_ssl_endpoint_certificate { |
| 194 | mbedtls_x509_crt *ca_cert; |
| 195 | mbedtls_x509_crt *cert; |
| 196 | mbedtls_pk_context *pkey; |
| 197 | } mbedtls_test_ssl_endpoint_certificate; |
| 198 | |
| 199 | /* |
| 200 | * Endpoint structure for SSL communication tests. |
| 201 | */ |
| 202 | typedef struct mbedtls_test_ssl_endpoint { |
| 203 | const char *name; |
| 204 | mbedtls_ssl_context ssl; |
| 205 | mbedtls_ssl_config conf; |
| 206 | mbedtls_test_mock_socket socket; |
| 207 | mbedtls_test_ssl_endpoint_certificate cert; |
| 208 | } mbedtls_test_ssl_endpoint; |
| 209 | |
| 210 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Yanray Wang | 47907a4 | 2022-10-24 14:42:01 +0800 | [diff] [blame] | 211 | |
Yanray Wang | 4d07d1c | 2022-10-27 15:28:16 +0800 | [diff] [blame] | 212 | /* |
Ronald Cron | 10b040f | 2024-02-05 09:38:09 +0100 | [diff] [blame] | 213 | * Random number generator aimed for TLS unitary tests. Its main purpose is to |
| 214 | * simplify the set-up of a random number generator for TLS |
| 215 | * unitary tests: no need to set up a good entropy source for example. |
| 216 | */ |
| 217 | int mbedtls_test_random(void *p_rng, unsigned char *output, size_t output_len); |
Yanray Wang | f88e529 | 2023-12-01 16:39:34 +0800 | [diff] [blame] | 218 | |
Yanray Wang | 4d07d1c | 2022-10-27 15:28:16 +0800 | [diff] [blame] | 219 | /* |
| 220 | * This function can be passed to mbedtls to receive output logs from it. In |
| 221 | * this case, it will count the instances of a mbedtls_test_ssl_log_pattern |
| 222 | * in the received logged messages. |
| 223 | */ |
| 224 | void mbedtls_test_ssl_log_analyzer(void *ctx, int level, |
| 225 | const char *file, int line, |
| 226 | const char *str); |
| 227 | |
| 228 | void mbedtls_test_init_handshake_options( |
| 229 | mbedtls_test_handshake_test_options *opts); |
| 230 | |
| 231 | void mbedtls_test_free_handshake_options( |
| 232 | mbedtls_test_handshake_test_options *opts); |
| 233 | |
| 234 | /* |
| 235 | * Initialises \p buf. After calling this function it is safe to call |
| 236 | * `mbedtls_test_ssl_buffer_free()` on \p buf. |
| 237 | */ |
| 238 | void mbedtls_test_ssl_buffer_init(mbedtls_test_ssl_buffer *buf); |
| 239 | |
| 240 | /* |
| 241 | * Sets up \p buf. After calling this function it is safe to call |
| 242 | * `mbedtls_test_ssl_buffer_put()` and `mbedtls_test_ssl_buffer_get()` |
| 243 | * on \p buf. |
| 244 | */ |
| 245 | int mbedtls_test_ssl_buffer_setup(mbedtls_test_ssl_buffer *buf, |
| 246 | size_t capacity); |
| 247 | |
| 248 | void mbedtls_test_ssl_buffer_free(mbedtls_test_ssl_buffer *buf); |
| 249 | |
| 250 | /* |
Yanray Wang | 4d07d1c | 2022-10-27 15:28:16 +0800 | [diff] [blame] | 251 | * Puts \p input_len bytes from the \p input buffer into the ring buffer \p buf. |
| 252 | * |
| 253 | * \p buf must have been initialized and set up by calling |
| 254 | * `mbedtls_test_ssl_buffer_init()` and `mbedtls_test_ssl_buffer_setup()`. |
| 255 | * |
| 256 | * \retval \p input_len, if the data fits. |
| 257 | * \retval 0 <= value < \p input_len, if the data does not fit. |
| 258 | * \retval -1, if \p buf is NULL, it hasn't been set up or \p input_len is not |
| 259 | * zero and \p input is NULL. |
| 260 | */ |
| 261 | int mbedtls_test_ssl_buffer_put(mbedtls_test_ssl_buffer *buf, |
| 262 | const unsigned char *input, size_t input_len); |
| 263 | |
| 264 | /* |
| 265 | * Gets \p output_len bytes from the ring buffer \p buf into the |
| 266 | * \p output buffer. The output buffer can be NULL, in this case a part of the |
| 267 | * ring buffer will be dropped, if the requested length is available. |
| 268 | * |
| 269 | * \p buf must have been initialized and set up by calling |
| 270 | * `mbedtls_test_ssl_buffer_init()` and `mbedtls_test_ssl_buffer_setup()`. |
| 271 | * |
| 272 | * \retval \p output_len, if the data is available. |
| 273 | * \retval 0 <= value < \p output_len, if the data is not available. |
| 274 | * \retval -1, if \buf is NULL or it hasn't been set up. |
| 275 | */ |
| 276 | int mbedtls_test_ssl_buffer_get(mbedtls_test_ssl_buffer *buf, |
| 277 | unsigned char *output, size_t output_len); |
| 278 | |
| 279 | /* |
| 280 | * Errors used in the message transport mock tests |
| 281 | */ |
| 282 | #define MBEDTLS_TEST_ERROR_ARG_NULL -11 |
| 283 | #define MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED -44 |
| 284 | |
| 285 | /* |
| 286 | * Setup and free functions for the message metadata queue. |
| 287 | * |
| 288 | * \p capacity describes the number of message metadata chunks that can be held |
| 289 | * within the queue. |
| 290 | * |
| 291 | * \retval 0, if a metadata queue of a given length can be allocated. |
| 292 | * \retval MBEDTLS_ERR_SSL_ALLOC_FAILED, if allocation failed. |
| 293 | */ |
| 294 | int mbedtls_test_ssl_message_queue_setup( |
| 295 | mbedtls_test_ssl_message_queue *queue, size_t capacity); |
| 296 | |
| 297 | void mbedtls_test_ssl_message_queue_free( |
| 298 | mbedtls_test_ssl_message_queue *queue); |
| 299 | |
| 300 | /* |
| 301 | * Push message length information onto the message metadata queue. |
| 302 | * This will become the last element to leave it (fifo). |
| 303 | * |
| 304 | * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null. |
| 305 | * \retval MBEDTLS_ERR_SSL_WANT_WRITE, if the queue is full. |
| 306 | * \retval \p len, if the push was successful. |
| 307 | */ |
| 308 | int mbedtls_test_ssl_message_queue_push_info( |
| 309 | mbedtls_test_ssl_message_queue *queue, size_t len); |
| 310 | |
| 311 | /* |
| 312 | * Pop information about the next message length from the queue. This will be |
| 313 | * the oldest inserted message length(fifo). \p msg_len can be null, in which |
| 314 | * case the data will be popped from the queue but not copied anywhere. |
| 315 | * |
| 316 | * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null. |
| 317 | * \retval MBEDTLS_ERR_SSL_WANT_READ, if the queue is empty. |
| 318 | * \retval message length, if the pop was successful, up to the given |
| 319 | \p buf_len. |
| 320 | */ |
| 321 | int mbedtls_test_ssl_message_queue_pop_info( |
| 322 | mbedtls_test_ssl_message_queue *queue, size_t buf_len); |
| 323 | |
| 324 | /* |
| 325 | * Setup and teardown functions for mock sockets. |
| 326 | */ |
Yanray Wang | 5f86a42 | 2023-03-15 16:02:29 +0800 | [diff] [blame] | 327 | void mbedtls_test_mock_socket_init(mbedtls_test_mock_socket *socket); |
Yanray Wang | 4d07d1c | 2022-10-27 15:28:16 +0800 | [diff] [blame] | 328 | |
| 329 | /* |
| 330 | * Closes the socket \p socket. |
| 331 | * |
| 332 | * \p socket must have been previously initialized by calling |
Yanray Wang | 5f86a42 | 2023-03-15 16:02:29 +0800 | [diff] [blame] | 333 | * mbedtls_test_mock_socket_init(). |
Yanray Wang | 4d07d1c | 2022-10-27 15:28:16 +0800 | [diff] [blame] | 334 | * |
| 335 | * This function frees all allocated resources and both sockets are aware of the |
| 336 | * new connection state. |
| 337 | * |
| 338 | * That is, this function does not simulate half-open TCP connections and the |
| 339 | * phenomenon that when closing a UDP connection the peer is not aware of the |
| 340 | * connection having been closed. |
| 341 | */ |
| 342 | void mbedtls_test_mock_socket_close(mbedtls_test_mock_socket *socket); |
| 343 | |
| 344 | /* |
| 345 | * Establishes a connection between \p peer1 and \p peer2. |
| 346 | * |
| 347 | * \p peer1 and \p peer2 must have been previously initialized by calling |
Yanray Wang | 5f86a42 | 2023-03-15 16:02:29 +0800 | [diff] [blame] | 348 | * mbedtls_test_mock_socket_init(). |
Yanray Wang | 4d07d1c | 2022-10-27 15:28:16 +0800 | [diff] [blame] | 349 | * |
| 350 | * The capacities of the internal buffers are set to \p bufsize. Setting this to |
| 351 | * the correct value allows for simulation of MTU, sanity testing the mock |
| 352 | * implementation and mocking TCP connections with lower memory cost. |
| 353 | */ |
| 354 | int mbedtls_test_mock_socket_connect(mbedtls_test_mock_socket *peer1, |
| 355 | mbedtls_test_mock_socket *peer2, |
| 356 | size_t bufsize); |
| 357 | |
| 358 | |
| 359 | /* |
| 360 | * Callbacks for simulating blocking I/O over connection-oriented transport. |
| 361 | */ |
Yanray Wang | af727a2 | 2023-03-13 19:22:36 +0800 | [diff] [blame] | 362 | int mbedtls_test_mock_tcp_send_b(void *ctx, |
| 363 | const unsigned char *buf, size_t len); |
Yanray Wang | 4d07d1c | 2022-10-27 15:28:16 +0800 | [diff] [blame] | 364 | |
| 365 | int mbedtls_test_mock_tcp_recv_b(void *ctx, unsigned char *buf, size_t len); |
| 366 | |
| 367 | /* |
| 368 | * Callbacks for simulating non-blocking I/O over connection-oriented transport. |
| 369 | */ |
Yanray Wang | af727a2 | 2023-03-13 19:22:36 +0800 | [diff] [blame] | 370 | int mbedtls_test_mock_tcp_send_nb(void *ctx, |
| 371 | const unsigned char *buf, size_t len); |
Yanray Wang | 4d07d1c | 2022-10-27 15:28:16 +0800 | [diff] [blame] | 372 | |
| 373 | int mbedtls_test_mock_tcp_recv_nb(void *ctx, unsigned char *buf, size_t len); |
| 374 | |
| 375 | void mbedtls_test_message_socket_init( |
| 376 | mbedtls_test_message_socket_context *ctx); |
| 377 | |
| 378 | /* |
| 379 | * Setup a given message socket context including initialization of |
| 380 | * input/output queues to a chosen capacity of messages. Also set the |
| 381 | * corresponding mock socket. |
| 382 | * |
| 383 | * \retval 0, if everything succeeds. |
| 384 | * \retval MBEDTLS_ERR_SSL_ALLOC_FAILED, if allocation of a message |
| 385 | * queue failed. |
| 386 | */ |
| 387 | int mbedtls_test_message_socket_setup( |
| 388 | mbedtls_test_ssl_message_queue *queue_input, |
| 389 | mbedtls_test_ssl_message_queue *queue_output, |
Yanray Wang | d19894f | 2023-03-16 11:47:39 +0800 | [diff] [blame] | 390 | size_t queue_capacity, |
| 391 | mbedtls_test_mock_socket *socket, |
Yanray Wang | 4d07d1c | 2022-10-27 15:28:16 +0800 | [diff] [blame] | 392 | mbedtls_test_message_socket_context *ctx); |
| 393 | |
| 394 | /* |
| 395 | * Close a given message socket context, along with the socket itself. Free the |
| 396 | * memory allocated by the input queue. |
| 397 | */ |
| 398 | void mbedtls_test_message_socket_close( |
| 399 | mbedtls_test_message_socket_context *ctx); |
| 400 | |
| 401 | /* |
| 402 | * Send one message through a given message socket context. |
| 403 | * |
| 404 | * \retval \p len, if everything succeeds. |
| 405 | * \retval MBEDTLS_TEST_ERROR_CONTEXT_ERROR, if any of the needed context |
| 406 | * elements or the context itself is null. |
| 407 | * \retval MBEDTLS_TEST_ERROR_SEND_FAILED if |
| 408 | * mbedtls_test_mock_tcp_send_b failed. |
| 409 | * \retval MBEDTLS_ERR_SSL_WANT_WRITE, if the output queue is full. |
| 410 | * |
| 411 | * This function will also return any error from |
| 412 | * mbedtls_test_ssl_message_queue_push_info. |
| 413 | */ |
Yanray Wang | af727a2 | 2023-03-13 19:22:36 +0800 | [diff] [blame] | 414 | int mbedtls_test_mock_tcp_send_msg(void *ctx, |
| 415 | const unsigned char *buf, size_t len); |
Yanray Wang | 4d07d1c | 2022-10-27 15:28:16 +0800 | [diff] [blame] | 416 | |
| 417 | /* |
| 418 | * Receive one message from a given message socket context and return message |
| 419 | * length or an error. |
| 420 | * |
| 421 | * \retval message length, if everything succeeds. |
| 422 | * \retval MBEDTLS_TEST_ERROR_CONTEXT_ERROR, if any of the needed context |
| 423 | * elements or the context itself is null. |
| 424 | * \retval MBEDTLS_TEST_ERROR_RECV_FAILED if |
| 425 | * mbedtls_test_mock_tcp_recv_b failed. |
| 426 | * |
| 427 | * This function will also return any error other than |
Yanray Wang | 5e22a92 | 2023-03-16 14:57:54 +0800 | [diff] [blame] | 428 | * MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED from test_ssl_message_queue_peek_info. |
Yanray Wang | 4d07d1c | 2022-10-27 15:28:16 +0800 | [diff] [blame] | 429 | */ |
Yanray Wang | af727a2 | 2023-03-13 19:22:36 +0800 | [diff] [blame] | 430 | int mbedtls_test_mock_tcp_recv_msg(void *ctx, |
| 431 | unsigned char *buf, size_t buf_len); |
Yanray Wang | 4d07d1c | 2022-10-27 15:28:16 +0800 | [diff] [blame] | 432 | |
| 433 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
| 434 | |
| 435 | /* |
| 436 | * Initializes \p ep_cert structure and assigns it to endpoint |
| 437 | * represented by \p ep. |
| 438 | * |
| 439 | * \retval 0 on success, otherwise error code. |
| 440 | */ |
| 441 | int mbedtls_test_ssl_endpoint_certificate_init(mbedtls_test_ssl_endpoint *ep, |
| 442 | int pk_alg, |
| 443 | int opaque_alg, int opaque_alg2, |
| 444 | int opaque_usage); |
| 445 | |
| 446 | /* |
| 447 | * Initializes \p ep structure. It is important to call |
| 448 | * `mbedtls_test_ssl_endpoint_free()` after calling this function |
| 449 | * even if it fails. |
| 450 | * |
| 451 | * \p endpoint_type must be set as MBEDTLS_SSL_IS_SERVER or |
| 452 | * MBEDTLS_SSL_IS_CLIENT. |
| 453 | * \p pk_alg the algorithm to use, currently only MBEDTLS_PK_RSA and |
| 454 | * MBEDTLS_PK_ECDSA are supported. |
| 455 | * \p dtls_context - in case of DTLS - this is the context handling metadata. |
| 456 | * \p input_queue - used only in case of DTLS. |
| 457 | * \p output_queue - used only in case of DTLS. |
| 458 | * |
| 459 | * \retval 0 on success, otherwise error code. |
| 460 | */ |
| 461 | int mbedtls_test_ssl_endpoint_init( |
| 462 | mbedtls_test_ssl_endpoint *ep, int endpoint_type, |
| 463 | mbedtls_test_handshake_test_options *options, |
| 464 | mbedtls_test_message_socket_context *dtls_context, |
| 465 | mbedtls_test_ssl_message_queue *input_queue, |
Ronald Cron | fb53647 | 2024-01-26 14:55:25 +0100 | [diff] [blame] | 466 | mbedtls_test_ssl_message_queue *output_queue); |
Yanray Wang | 4d07d1c | 2022-10-27 15:28:16 +0800 | [diff] [blame] | 467 | |
| 468 | /* |
| 469 | * Deinitializes endpoint represented by \p ep. |
| 470 | */ |
| 471 | void mbedtls_test_ssl_endpoint_free( |
| 472 | mbedtls_test_ssl_endpoint *ep, |
| 473 | mbedtls_test_message_socket_context *context); |
| 474 | |
| 475 | /* |
| 476 | * This function moves ssl handshake from \p ssl to prescribed \p state. |
| 477 | * /p second_ssl is used as second endpoint and their sockets have to be |
| 478 | * connected before calling this function. |
| 479 | * |
| 480 | * \retval 0 on success, otherwise error code. |
| 481 | */ |
| 482 | int mbedtls_test_move_handshake_to_state(mbedtls_ssl_context *ssl, |
| 483 | mbedtls_ssl_context *second_ssl, |
| 484 | int state); |
| 485 | |
| 486 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
| 487 | |
Yanray Wang | 1fca4de | 2023-02-06 12:10:48 +0800 | [diff] [blame] | 488 | /* |
| 489 | * Helper function setting up inverse record transformations |
| 490 | * using given cipher, hash, EtM mode, authentication tag length, |
| 491 | * and version. |
| 492 | */ |
| 493 | #define CHK(x) \ |
| 494 | do \ |
| 495 | { \ |
| 496 | if (!(x)) \ |
| 497 | { \ |
| 498 | ret = -1; \ |
| 499 | goto cleanup; \ |
| 500 | } \ |
| 501 | } while (0) |
| 502 | |
Yanray Wang | 25b766f | 2023-03-15 16:39:05 +0800 | [diff] [blame] | 503 | #if MBEDTLS_SSL_CID_OUT_LEN_MAX > MBEDTLS_SSL_CID_IN_LEN_MAX |
| 504 | #define SSL_CID_LEN_MIN MBEDTLS_SSL_CID_IN_LEN_MAX |
| 505 | #else |
| 506 | #define SSL_CID_LEN_MIN MBEDTLS_SSL_CID_OUT_LEN_MAX |
| 507 | #endif |
| 508 | |
Yanray Wang | 4d07d1c | 2022-10-27 15:28:16 +0800 | [diff] [blame] | 509 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
Elena Uziunaite | 74342c7 | 2024-07-05 11:31:29 +0100 | [diff] [blame] | 510 | defined(PSA_WANT_ALG_CBC_NO_PADDING) && defined(PSA_WANT_KEY_TYPE_AES) |
Yanray Wang | 4d07d1c | 2022-10-27 15:28:16 +0800 | [diff] [blame] | 511 | int mbedtls_test_psa_cipher_encrypt_helper(mbedtls_ssl_transform *transform, |
| 512 | const unsigned char *iv, |
| 513 | size_t iv_len, |
| 514 | const unsigned char *input, |
| 515 | size_t ilen, |
| 516 | unsigned char *output, |
| 517 | size_t *olen); |
Elena Uziunaite | 74342c7 | 2024-07-05 11:31:29 +0100 | [diff] [blame] | 518 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 && PSA_WANT_ALG_CBC_NO_PADDING && |
Elena Uziunaite | 6121a34 | 2024-07-05 11:16:53 +0100 | [diff] [blame] | 519 | PSA_WANT_KEY_TYPE_AES */ |
Yanray Wang | 4d07d1c | 2022-10-27 15:28:16 +0800 | [diff] [blame] | 520 | |
| 521 | int mbedtls_test_ssl_build_transforms(mbedtls_ssl_transform *t_in, |
| 522 | mbedtls_ssl_transform *t_out, |
| 523 | int cipher_type, int hash_id, |
| 524 | int etm, int tag_mode, |
| 525 | mbedtls_ssl_protocol_version tls_version, |
| 526 | size_t cid0_len, |
| 527 | size_t cid1_len); |
| 528 | |
Gilles Peskine | 9099d3f | 2023-09-18 13:11:50 +0200 | [diff] [blame] | 529 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
| 530 | /** |
| 531 | * \param[in,out] record The record to prepare. |
| 532 | * It must contain the data to MAC at offset |
| 533 | * `record->data_offset`, of length |
| 534 | * `record->data_length`. |
| 535 | * On success, write the MAC immediately |
| 536 | * after the data and increment |
| 537 | * `record->data_length` accordingly. |
| 538 | * \param[in,out] transform_out The out transform, typically prepared by |
| 539 | * mbedtls_test_ssl_build_transforms(). |
| 540 | * Its HMAC context may be used. Other than that |
| 541 | * it is treated as an input parameter. |
| 542 | * |
| 543 | * \return 0 on success, an `MBEDTLS_ERR_xxx` error code |
| 544 | * or -1 on error. |
| 545 | */ |
| 546 | int mbedtls_test_ssl_prepare_record_mac(mbedtls_record *record, |
| 547 | mbedtls_ssl_transform *transform_out); |
| 548 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
| 549 | |
Yanray Wang | 4d07d1c | 2022-10-27 15:28:16 +0800 | [diff] [blame] | 550 | /* |
| 551 | * Populate a session structure for serialization tests. |
| 552 | * Choose dummy values, mostly non-0 to distinguish from the init default. |
| 553 | */ |
| 554 | int mbedtls_test_ssl_tls12_populate_session(mbedtls_ssl_session *session, |
| 555 | int ticket_len, |
Ronald Cron | 7b1921a | 2023-11-23 12:31:56 +0100 | [diff] [blame] | 556 | int endpoint_type, |
Yanray Wang | 4d07d1c | 2022-10-27 15:28:16 +0800 | [diff] [blame] | 557 | const char *crt_file); |
| 558 | |
| 559 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 560 | int mbedtls_test_ssl_tls13_populate_session(mbedtls_ssl_session *session, |
| 561 | int ticket_len, |
| 562 | int endpoint_type); |
| 563 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 564 | |
| 565 | /* |
| 566 | * Perform data exchanging between \p ssl_1 and \p ssl_2 and check if the |
| 567 | * message was sent in the correct number of fragments. |
| 568 | * |
| 569 | * /p ssl_1 and /p ssl_2 Endpoints represented by mbedtls_ssl_context. Both |
| 570 | * of them must be initialized and connected |
| 571 | * beforehand. |
| 572 | * /p msg_len_1 and /p msg_len_2 specify the size of the message to send. |
| 573 | * /p expected_fragments_1 and /p expected_fragments_2 determine in how many |
| 574 | * fragments the message should be sent. |
| 575 | * expected_fragments is 0: can be used for DTLS testing while the message |
| 576 | * size is larger than MFL. In that case the message |
| 577 | * cannot be fragmented and sent to the second |
| 578 | * endpoint. |
| 579 | * This value can be used for negative tests. |
| 580 | * expected_fragments is 1: can be used for TLS/DTLS testing while the |
| 581 | * message size is below MFL |
| 582 | * expected_fragments > 1: can be used for TLS testing while the message |
| 583 | * size is larger than MFL |
| 584 | * |
| 585 | * \retval 0 on success, otherwise error code. |
| 586 | */ |
Yanray Wang | b088bfc | 2023-03-16 12:15:49 +0800 | [diff] [blame] | 587 | int mbedtls_test_ssl_exchange_data( |
| 588 | mbedtls_ssl_context *ssl_1, |
| 589 | int msg_len_1, const int expected_fragments_1, |
| 590 | mbedtls_ssl_context *ssl_2, |
| 591 | int msg_len_2, const int expected_fragments_2); |
Yanray Wang | 4d07d1c | 2022-10-27 15:28:16 +0800 | [diff] [blame] | 592 | |
| 593 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
| 594 | void mbedtls_test_ssl_perform_handshake( |
| 595 | mbedtls_test_handshake_test_options *options); |
| 596 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
| 597 | |
| 598 | #if defined(MBEDTLS_TEST_HOOKS) |
| 599 | /* |
| 600 | * Tweak vector lengths in a TLS 1.3 Certificate message |
| 601 | * |
| 602 | * \param[in] buf Buffer containing the Certificate message to tweak |
| 603 | * \param[in]]out] end End of the buffer to parse |
| 604 | * \param tweak Tweak identifier (from 1 to the number of tweaks). |
| 605 | * \param[out] expected_result Error code expected from the parsing function |
| 606 | * \param[out] args Arguments of the MBEDTLS_SSL_CHK_BUF_READ_PTR call that |
| 607 | * is expected to fail. All zeroes if no |
| 608 | * MBEDTLS_SSL_CHK_BUF_READ_PTR failure is expected. |
| 609 | */ |
Yanray Wang | f56181a | 2023-03-16 12:21:33 +0800 | [diff] [blame] | 610 | int mbedtls_test_tweak_tls13_certificate_msg_vector_len( |
Yanray Wang | 4d07d1c | 2022-10-27 15:28:16 +0800 | [diff] [blame] | 611 | unsigned char *buf, unsigned char **end, int tweak, |
| 612 | int *expected_result, mbedtls_ssl_chk_buf_ptr_args *args); |
| 613 | #endif /* MBEDTLS_TEST_HOOKS */ |
Yanray Wang | 1db628f | 2023-02-03 11:01:29 +0800 | [diff] [blame] | 614 | |
Ronald Cron | 77abfe6 | 2024-01-15 11:17:31 +0100 | [diff] [blame] | 615 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 616 | int mbedtls_test_ticket_write( |
| 617 | void *p_ticket, const mbedtls_ssl_session *session, |
| 618 | unsigned char *start, const unsigned char *end, |
| 619 | size_t *tlen, uint32_t *ticket_lifetime); |
| 620 | |
| 621 | int mbedtls_test_ticket_parse(void *p_ticket, mbedtls_ssl_session *session, |
| 622 | unsigned char *buf, size_t len); |
| 623 | #endif /* MBEDTLS_SSL_SESSION_TICKETS */ |
| 624 | |
Ronald Cron | 1f6e4e4 | 2024-01-26 16:31:33 +0100 | [diff] [blame] | 625 | #if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SRV_C) && \ |
| 626 | defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS) && \ |
| 627 | defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
| 628 | int mbedtls_test_get_tls13_ticket( |
| 629 | mbedtls_test_handshake_test_options *client_options, |
| 630 | mbedtls_test_handshake_test_options *server_options, |
| 631 | mbedtls_ssl_session *session); |
Ronald Cron | b9a9b1f | 2024-02-14 11:28:05 +0100 | [diff] [blame] | 632 | #endif |
Ronald Cron | 1f6e4e4 | 2024-01-26 16:31:33 +0100 | [diff] [blame] | 633 | |
Yanray Wang | 1db628f | 2023-02-03 11:01:29 +0800 | [diff] [blame] | 634 | #define ECJPAKE_TEST_PWD "bla" |
| 635 | |
| 636 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 637 | #define ECJPAKE_TEST_SET_PASSWORD(exp_ret_val) \ |
| 638 | ret = (use_opaque_arg) ? \ |
| 639 | mbedtls_ssl_set_hs_ecjpake_password_opaque(&ssl, pwd_slot) : \ |
| 640 | mbedtls_ssl_set_hs_ecjpake_password(&ssl, pwd_string, pwd_len); \ |
| 641 | TEST_EQUAL(ret, exp_ret_val) |
| 642 | #else |
| 643 | #define ECJPAKE_TEST_SET_PASSWORD(exp_ret_val) \ |
| 644 | ret = mbedtls_ssl_set_hs_ecjpake_password(&ssl, \ |
| 645 | pwd_string, pwd_len); \ |
| 646 | TEST_EQUAL(ret, exp_ret_val) |
| 647 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 648 | |
Yanray Wang | 09a6f7e | 2023-02-03 11:04:38 +0800 | [diff] [blame] | 649 | #define TEST_AVAILABLE_ECC(tls_id_, group_id_, psa_family_, psa_bits_) \ |
| 650 | TEST_EQUAL(mbedtls_ssl_get_ecp_group_id_from_tls_id(tls_id_), \ |
| 651 | group_id_); \ |
| 652 | TEST_EQUAL(mbedtls_ssl_get_tls_id_from_ecp_group_id(group_id_), \ |
| 653 | tls_id_); \ |
| 654 | TEST_EQUAL(mbedtls_ssl_get_psa_curve_info_from_tls_id(tls_id_, \ |
Przemek Stekiel | da4fba6 | 2023-06-02 14:52:28 +0200 | [diff] [blame] | 655 | &psa_type, &psa_bits), PSA_SUCCESS); \ |
| 656 | TEST_EQUAL(psa_family_, PSA_KEY_TYPE_ECC_GET_FAMILY(psa_type)); \ |
Yanray Wang | 09a6f7e | 2023-02-03 11:04:38 +0800 | [diff] [blame] | 657 | TEST_EQUAL(psa_bits_, psa_bits); |
| 658 | |
| 659 | #define TEST_UNAVAILABLE_ECC(tls_id_, group_id_, psa_family_, psa_bits_) \ |
| 660 | TEST_EQUAL(mbedtls_ssl_get_ecp_group_id_from_tls_id(tls_id_), \ |
| 661 | MBEDTLS_ECP_DP_NONE); \ |
| 662 | TEST_EQUAL(mbedtls_ssl_get_tls_id_from_ecp_group_id(group_id_), \ |
| 663 | 0); \ |
| 664 | TEST_EQUAL(mbedtls_ssl_get_psa_curve_info_from_tls_id(tls_id_, \ |
Przemek Stekiel | da4fba6 | 2023-06-02 14:52:28 +0200 | [diff] [blame] | 665 | &psa_type, &psa_bits), \ |
Yanray Wang | 09a6f7e | 2023-02-03 11:04:38 +0800 | [diff] [blame] | 666 | PSA_ERROR_NOT_SUPPORTED); |
| 667 | |
Yanray Wang | 4d07d1c | 2022-10-27 15:28:16 +0800 | [diff] [blame] | 668 | #endif /* MBEDTLS_SSL_TLS_C */ |
| 669 | |
Yanray Wang | 47907a4 | 2022-10-24 14:42:01 +0800 | [diff] [blame] | 670 | #endif /* SSL_HELPERS_H */ |