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