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