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