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