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