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