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