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