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