Yanray Wang | 5fce145 | 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 | 7ff7965 | 2023-11-03 12:04:52 +0000 | [diff] [blame] | 8 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Yanray Wang | 5fce145 | 2022-10-24 14:42:01 +0800 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <test/ssl_helpers.h> |
Yanray Wang | bd56b03 | 2023-03-14 14:36:48 +0800 | [diff] [blame] | 12 | |
Gilles Peskine | a8cd2e6 | 2024-05-17 19:00:46 +0200 | [diff] [blame] | 13 | #include <limits.h> |
| 14 | |
Yanray Wang | 4323e45 | 2023-03-14 16:52:06 +0800 | [diff] [blame] | 15 | #if defined(MBEDTLS_SSL_TLS_C) |
| 16 | |
Yanray Wang | bd56b03 | 2023-03-14 14:36:48 +0800 | [diff] [blame] | 17 | void mbedtls_test_ssl_log_analyzer(void *ctx, int level, |
| 18 | const char *file, int line, |
| 19 | const char *str) |
| 20 | { |
| 21 | mbedtls_test_ssl_log_pattern *p = (mbedtls_test_ssl_log_pattern *) ctx; |
| 22 | |
| 23 | (void) level; |
| 24 | (void) line; |
| 25 | (void) file; |
| 26 | |
| 27 | if (NULL != p && |
| 28 | NULL != p->pattern && |
| 29 | NULL != strstr(str, p->pattern)) { |
| 30 | p->counter++; |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | void mbedtls_test_init_handshake_options( |
| 35 | mbedtls_test_handshake_test_options *opts) |
| 36 | { |
| 37 | opts->cipher = ""; |
| 38 | opts->client_min_version = TEST_SSL_MINOR_VERSION_NONE; |
| 39 | opts->client_max_version = TEST_SSL_MINOR_VERSION_NONE; |
| 40 | opts->server_min_version = TEST_SSL_MINOR_VERSION_NONE; |
| 41 | opts->server_max_version = TEST_SSL_MINOR_VERSION_NONE; |
| 42 | opts->expected_negotiated_version = MBEDTLS_SSL_MINOR_VERSION_3; |
| 43 | opts->pk_alg = MBEDTLS_PK_RSA; |
| 44 | opts->psk_str = NULL; |
| 45 | opts->dtls = 0; |
| 46 | opts->srv_auth_mode = MBEDTLS_SSL_VERIFY_NONE; |
| 47 | opts->serialize = 0; |
| 48 | opts->mfl = MBEDTLS_SSL_MAX_FRAG_LEN_NONE; |
| 49 | opts->cli_msg_len = 100; |
| 50 | opts->srv_msg_len = 100; |
| 51 | opts->expected_cli_fragments = 1; |
| 52 | opts->expected_srv_fragments = 1; |
| 53 | opts->renegotiate = 0; |
| 54 | opts->legacy_renegotiation = MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION; |
| 55 | opts->srv_log_obj = NULL; |
| 56 | opts->srv_log_obj = NULL; |
| 57 | opts->srv_log_fun = NULL; |
| 58 | opts->cli_log_fun = NULL; |
| 59 | opts->resize_buffers = 1; |
| 60 | } |
| 61 | |
Yanray Wang | bd56b03 | 2023-03-14 14:36:48 +0800 | [diff] [blame] | 62 | void mbedtls_test_ssl_buffer_init(mbedtls_test_ssl_buffer *buf) |
| 63 | { |
| 64 | memset(buf, 0, sizeof(*buf)); |
| 65 | } |
| 66 | |
Yanray Wang | bd56b03 | 2023-03-14 14:36:48 +0800 | [diff] [blame] | 67 | int mbedtls_test_ssl_buffer_setup(mbedtls_test_ssl_buffer *buf, |
| 68 | size_t capacity) |
| 69 | { |
| 70 | buf->buffer = (unsigned char *) mbedtls_calloc(capacity, |
| 71 | sizeof(unsigned char)); |
| 72 | if (NULL == buf->buffer) { |
| 73 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 74 | } |
| 75 | buf->capacity = capacity; |
| 76 | |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | void mbedtls_test_ssl_buffer_free(mbedtls_test_ssl_buffer *buf) |
| 81 | { |
| 82 | if (buf->buffer != NULL) { |
| 83 | mbedtls_free(buf->buffer); |
| 84 | } |
| 85 | |
| 86 | memset(buf, 0, sizeof(*buf)); |
| 87 | } |
| 88 | |
Yanray Wang | bd56b03 | 2023-03-14 14:36:48 +0800 | [diff] [blame] | 89 | int mbedtls_test_ssl_buffer_put(mbedtls_test_ssl_buffer *buf, |
| 90 | const unsigned char *input, size_t input_len) |
| 91 | { |
| 92 | size_t overflow = 0; |
| 93 | |
| 94 | if ((buf == NULL) || (buf->buffer == NULL)) { |
| 95 | return -1; |
| 96 | } |
| 97 | |
| 98 | /* Reduce input_len to a number that fits in the buffer. */ |
| 99 | if ((buf->content_length + input_len) > buf->capacity) { |
| 100 | input_len = buf->capacity - buf->content_length; |
| 101 | } |
| 102 | |
| 103 | if (input == NULL) { |
| 104 | return (input_len == 0) ? 0 : -1; |
| 105 | } |
| 106 | |
| 107 | /* Check if the buffer has not come full circle and free space is not in |
| 108 | * the middle */ |
| 109 | if (buf->start + buf->content_length < buf->capacity) { |
| 110 | |
| 111 | /* Calculate the number of bytes that need to be placed at lower memory |
| 112 | * address */ |
| 113 | if (buf->start + buf->content_length + input_len |
| 114 | > buf->capacity) { |
| 115 | overflow = (buf->start + buf->content_length + input_len) |
| 116 | % buf->capacity; |
| 117 | } |
| 118 | |
| 119 | memcpy(buf->buffer + buf->start + buf->content_length, input, |
| 120 | input_len - overflow); |
| 121 | memcpy(buf->buffer, input + input_len - overflow, overflow); |
| 122 | |
| 123 | } else { |
| 124 | /* The buffer has come full circle and free space is in the middle */ |
| 125 | memcpy(buf->buffer + buf->start + buf->content_length - buf->capacity, |
| 126 | input, input_len); |
| 127 | } |
| 128 | |
| 129 | buf->content_length += input_len; |
Yanray Wang | d2696f2 | 2022-11-03 11:51:59 +0800 | [diff] [blame] | 130 | return (input_len > INT_MAX) ? INT_MAX : (int) input_len; |
Yanray Wang | bd56b03 | 2023-03-14 14:36:48 +0800 | [diff] [blame] | 131 | } |
| 132 | |
Yanray Wang | bd56b03 | 2023-03-14 14:36:48 +0800 | [diff] [blame] | 133 | int mbedtls_test_ssl_buffer_get(mbedtls_test_ssl_buffer *buf, |
| 134 | unsigned char *output, size_t output_len) |
| 135 | { |
| 136 | size_t overflow = 0; |
| 137 | |
| 138 | if ((buf == NULL) || (buf->buffer == NULL)) { |
| 139 | return -1; |
| 140 | } |
| 141 | |
| 142 | if (output == NULL && output_len == 0) { |
| 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | if (buf->content_length < output_len) { |
| 147 | output_len = buf->content_length; |
| 148 | } |
| 149 | |
| 150 | /* Calculate the number of bytes that need to be drawn from lower memory |
| 151 | * address */ |
| 152 | if (buf->start + output_len > buf->capacity) { |
| 153 | overflow = (buf->start + output_len) % buf->capacity; |
| 154 | } |
| 155 | |
| 156 | if (output != NULL) { |
| 157 | memcpy(output, buf->buffer + buf->start, output_len - overflow); |
| 158 | memcpy(output + output_len - overflow, buf->buffer, overflow); |
| 159 | } |
| 160 | |
| 161 | buf->content_length -= output_len; |
| 162 | buf->start = (buf->start + output_len) % buf->capacity; |
| 163 | |
Yanray Wang | d2696f2 | 2022-11-03 11:51:59 +0800 | [diff] [blame] | 164 | return (output_len > INT_MAX) ? INT_MAX : (int) output_len; |
Yanray Wang | bd56b03 | 2023-03-14 14:36:48 +0800 | [diff] [blame] | 165 | } |
| 166 | |
Yanray Wang | bd56b03 | 2023-03-14 14:36:48 +0800 | [diff] [blame] | 167 | int mbedtls_test_ssl_message_queue_setup( |
| 168 | mbedtls_test_ssl_message_queue *queue, size_t capacity) |
| 169 | { |
| 170 | queue->messages = (size_t *) mbedtls_calloc(capacity, sizeof(size_t)); |
| 171 | if (NULL == queue->messages) { |
| 172 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 173 | } |
| 174 | |
Yanray Wang | d2696f2 | 2022-11-03 11:51:59 +0800 | [diff] [blame] | 175 | queue->capacity = (capacity > INT_MAX) ? INT_MAX : (int) capacity; |
Yanray Wang | bd56b03 | 2023-03-14 14:36:48 +0800 | [diff] [blame] | 176 | queue->pos = 0; |
| 177 | queue->num = 0; |
| 178 | |
| 179 | return 0; |
| 180 | } |
| 181 | |
| 182 | void mbedtls_test_ssl_message_queue_free( |
| 183 | mbedtls_test_ssl_message_queue *queue) |
| 184 | { |
| 185 | if (queue == NULL) { |
| 186 | return; |
| 187 | } |
| 188 | |
| 189 | if (queue->messages != NULL) { |
| 190 | mbedtls_free(queue->messages); |
| 191 | } |
| 192 | |
| 193 | memset(queue, 0, sizeof(*queue)); |
| 194 | } |
| 195 | |
Yanray Wang | bd56b03 | 2023-03-14 14:36:48 +0800 | [diff] [blame] | 196 | int mbedtls_test_ssl_message_queue_push_info( |
| 197 | mbedtls_test_ssl_message_queue *queue, size_t len) |
| 198 | { |
| 199 | int place; |
| 200 | if (queue == NULL) { |
| 201 | return MBEDTLS_TEST_ERROR_ARG_NULL; |
| 202 | } |
| 203 | |
| 204 | if (queue->num >= queue->capacity) { |
| 205 | return MBEDTLS_ERR_SSL_WANT_WRITE; |
| 206 | } |
| 207 | |
| 208 | place = (queue->pos + queue->num) % queue->capacity; |
| 209 | queue->messages[place] = len; |
| 210 | queue->num++; |
Yanray Wang | d2696f2 | 2022-11-03 11:51:59 +0800 | [diff] [blame] | 211 | return (len > INT_MAX) ? INT_MAX : (int) len; |
Yanray Wang | bd56b03 | 2023-03-14 14:36:48 +0800 | [diff] [blame] | 212 | } |
| 213 | |
Yanray Wang | bd56b03 | 2023-03-14 14:36:48 +0800 | [diff] [blame] | 214 | int mbedtls_test_ssl_message_queue_pop_info( |
| 215 | mbedtls_test_ssl_message_queue *queue, size_t buf_len) |
| 216 | { |
| 217 | size_t message_length; |
| 218 | if (queue == NULL) { |
| 219 | return MBEDTLS_TEST_ERROR_ARG_NULL; |
| 220 | } |
| 221 | if (queue->num == 0) { |
| 222 | return MBEDTLS_ERR_SSL_WANT_READ; |
| 223 | } |
| 224 | |
| 225 | message_length = queue->messages[queue->pos]; |
| 226 | queue->messages[queue->pos] = 0; |
| 227 | queue->num--; |
| 228 | queue->pos++; |
| 229 | queue->pos %= queue->capacity; |
| 230 | if (queue->pos < 0) { |
| 231 | queue->pos += queue->capacity; |
| 232 | } |
| 233 | |
Yanray Wang | d2696f2 | 2022-11-03 11:51:59 +0800 | [diff] [blame] | 234 | return (message_length > INT_MAX && buf_len > INT_MAX) ? INT_MAX : |
| 235 | (message_length > buf_len) ? (int) buf_len : (int) message_length; |
Yanray Wang | bd56b03 | 2023-03-14 14:36:48 +0800 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | /* |
| 239 | * Take a peek on the info about the next message length from the queue. |
| 240 | * This will be the oldest inserted message length(fifo). |
| 241 | * |
| 242 | * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null. |
| 243 | * \retval MBEDTLS_ERR_SSL_WANT_READ, if the queue is empty. |
| 244 | * \retval 0, if the peek was successful. |
| 245 | * \retval MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED, if the given buffer length is |
| 246 | * too small to fit the message. In this case the \p msg_len will be |
| 247 | * set to the full message length so that the |
| 248 | * caller knows what portion of the message can be dropped. |
| 249 | */ |
Yanray Wang | c4abee2 | 2023-03-16 14:57:54 +0800 | [diff] [blame] | 250 | static int test_ssl_message_queue_peek_info( |
| 251 | mbedtls_test_ssl_message_queue *queue, |
| 252 | size_t buf_len, size_t *msg_len) |
Yanray Wang | bd56b03 | 2023-03-14 14:36:48 +0800 | [diff] [blame] | 253 | { |
| 254 | if (queue == NULL || msg_len == NULL) { |
| 255 | return MBEDTLS_TEST_ERROR_ARG_NULL; |
| 256 | } |
| 257 | if (queue->num == 0) { |
| 258 | return MBEDTLS_ERR_SSL_WANT_READ; |
| 259 | } |
| 260 | |
| 261 | *msg_len = queue->messages[queue->pos]; |
| 262 | return (*msg_len > buf_len) ? MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED : 0; |
| 263 | } |
| 264 | |
Yanray Wang | d02c317 | 2023-03-15 16:02:29 +0800 | [diff] [blame] | 265 | void mbedtls_test_mock_socket_init(mbedtls_test_mock_socket *socket) |
Yanray Wang | bd56b03 | 2023-03-14 14:36:48 +0800 | [diff] [blame] | 266 | { |
| 267 | memset(socket, 0, sizeof(*socket)); |
| 268 | } |
| 269 | |
Yanray Wang | bd56b03 | 2023-03-14 14:36:48 +0800 | [diff] [blame] | 270 | void mbedtls_test_mock_socket_close(mbedtls_test_mock_socket *socket) |
| 271 | { |
| 272 | if (socket == NULL) { |
| 273 | return; |
| 274 | } |
| 275 | |
| 276 | if (socket->input != NULL) { |
| 277 | mbedtls_test_ssl_buffer_free(socket->input); |
| 278 | mbedtls_free(socket->input); |
| 279 | } |
| 280 | |
| 281 | if (socket->output != NULL) { |
| 282 | mbedtls_test_ssl_buffer_free(socket->output); |
| 283 | mbedtls_free(socket->output); |
| 284 | } |
| 285 | |
| 286 | if (socket->peer != NULL) { |
| 287 | memset(socket->peer, 0, sizeof(*socket->peer)); |
| 288 | } |
| 289 | |
| 290 | memset(socket, 0, sizeof(*socket)); |
| 291 | } |
| 292 | |
Yanray Wang | bd56b03 | 2023-03-14 14:36:48 +0800 | [diff] [blame] | 293 | int mbedtls_test_mock_socket_connect(mbedtls_test_mock_socket *peer1, |
| 294 | mbedtls_test_mock_socket *peer2, |
| 295 | size_t bufsize) |
| 296 | { |
| 297 | int ret = -1; |
| 298 | |
| 299 | peer1->output = |
| 300 | (mbedtls_test_ssl_buffer *) mbedtls_calloc( |
| 301 | 1, sizeof(mbedtls_test_ssl_buffer)); |
| 302 | if (peer1->output == NULL) { |
| 303 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 304 | goto exit; |
| 305 | } |
| 306 | mbedtls_test_ssl_buffer_init(peer1->output); |
| 307 | if (0 != (ret = mbedtls_test_ssl_buffer_setup(peer1->output, bufsize))) { |
| 308 | goto exit; |
| 309 | } |
| 310 | |
| 311 | peer2->output = |
| 312 | (mbedtls_test_ssl_buffer *) mbedtls_calloc( |
| 313 | 1, sizeof(mbedtls_test_ssl_buffer)); |
| 314 | if (peer2->output == NULL) { |
| 315 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 316 | goto exit; |
| 317 | } |
| 318 | mbedtls_test_ssl_buffer_init(peer2->output); |
| 319 | if (0 != (ret = mbedtls_test_ssl_buffer_setup(peer2->output, bufsize))) { |
| 320 | goto exit; |
| 321 | } |
| 322 | |
| 323 | peer1->peer = peer2; |
| 324 | peer2->peer = peer1; |
| 325 | peer1->input = peer2->output; |
| 326 | peer2->input = peer1->output; |
| 327 | |
| 328 | peer1->status = peer2->status = MBEDTLS_MOCK_SOCKET_CONNECTED; |
| 329 | ret = 0; |
| 330 | |
| 331 | exit: |
| 332 | |
| 333 | if (ret != 0) { |
| 334 | mbedtls_test_mock_socket_close(peer1); |
| 335 | mbedtls_test_mock_socket_close(peer2); |
| 336 | } |
| 337 | |
| 338 | return ret; |
| 339 | } |
| 340 | |
Yanray Wang | bd56b03 | 2023-03-14 14:36:48 +0800 | [diff] [blame] | 341 | int mbedtls_test_mock_tcp_send_b(void *ctx, |
| 342 | const unsigned char *buf, size_t len) |
| 343 | { |
| 344 | mbedtls_test_mock_socket *socket = (mbedtls_test_mock_socket *) ctx; |
| 345 | |
| 346 | if (socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED) { |
| 347 | return -1; |
| 348 | } |
| 349 | |
| 350 | return mbedtls_test_ssl_buffer_put(socket->output, buf, len); |
| 351 | } |
| 352 | |
| 353 | int mbedtls_test_mock_tcp_recv_b(void *ctx, unsigned char *buf, size_t len) |
| 354 | { |
| 355 | mbedtls_test_mock_socket *socket = (mbedtls_test_mock_socket *) ctx; |
| 356 | |
| 357 | if (socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED) { |
| 358 | return -1; |
| 359 | } |
| 360 | |
| 361 | return mbedtls_test_ssl_buffer_get(socket->input, buf, len); |
| 362 | } |
| 363 | |
Yanray Wang | bd56b03 | 2023-03-14 14:36:48 +0800 | [diff] [blame] | 364 | int mbedtls_test_mock_tcp_send_nb(void *ctx, |
| 365 | const unsigned char *buf, size_t len) |
| 366 | { |
| 367 | mbedtls_test_mock_socket *socket = (mbedtls_test_mock_socket *) ctx; |
| 368 | |
| 369 | if (socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED) { |
| 370 | return -1; |
| 371 | } |
| 372 | |
| 373 | if (socket->output->capacity == socket->output->content_length) { |
| 374 | return MBEDTLS_ERR_SSL_WANT_WRITE; |
| 375 | } |
| 376 | |
| 377 | return mbedtls_test_ssl_buffer_put(socket->output, buf, len); |
| 378 | } |
| 379 | |
| 380 | int mbedtls_test_mock_tcp_recv_nb(void *ctx, unsigned char *buf, size_t len) |
| 381 | { |
| 382 | mbedtls_test_mock_socket *socket = (mbedtls_test_mock_socket *) ctx; |
| 383 | |
| 384 | if (socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED) { |
| 385 | return -1; |
| 386 | } |
| 387 | |
| 388 | if (socket->input->content_length == 0) { |
| 389 | return MBEDTLS_ERR_SSL_WANT_READ; |
| 390 | } |
| 391 | |
| 392 | return mbedtls_test_ssl_buffer_get(socket->input, buf, len); |
| 393 | } |
| 394 | |
| 395 | void mbedtls_test_message_socket_init( |
| 396 | mbedtls_test_message_socket_context *ctx) |
| 397 | { |
| 398 | ctx->queue_input = NULL; |
| 399 | ctx->queue_output = NULL; |
| 400 | ctx->socket = NULL; |
| 401 | } |
| 402 | |
Yanray Wang | bd56b03 | 2023-03-14 14:36:48 +0800 | [diff] [blame] | 403 | int mbedtls_test_message_socket_setup( |
| 404 | mbedtls_test_ssl_message_queue *queue_input, |
| 405 | mbedtls_test_ssl_message_queue *queue_output, |
| 406 | size_t queue_capacity, |
| 407 | mbedtls_test_mock_socket *socket, |
| 408 | mbedtls_test_message_socket_context *ctx) |
| 409 | { |
| 410 | int ret = mbedtls_test_ssl_message_queue_setup(queue_input, queue_capacity); |
| 411 | if (ret != 0) { |
| 412 | return ret; |
| 413 | } |
| 414 | ctx->queue_input = queue_input; |
| 415 | ctx->queue_output = queue_output; |
| 416 | ctx->socket = socket; |
Yanray Wang | d02c317 | 2023-03-15 16:02:29 +0800 | [diff] [blame] | 417 | mbedtls_test_mock_socket_init(socket); |
Yanray Wang | bd56b03 | 2023-03-14 14:36:48 +0800 | [diff] [blame] | 418 | |
| 419 | return 0; |
| 420 | } |
| 421 | |
Yanray Wang | bd56b03 | 2023-03-14 14:36:48 +0800 | [diff] [blame] | 422 | void mbedtls_test_message_socket_close( |
| 423 | mbedtls_test_message_socket_context *ctx) |
| 424 | { |
| 425 | if (ctx == NULL) { |
| 426 | return; |
| 427 | } |
| 428 | |
| 429 | mbedtls_test_ssl_message_queue_free(ctx->queue_input); |
| 430 | mbedtls_test_mock_socket_close(ctx->socket); |
| 431 | memset(ctx, 0, sizeof(*ctx)); |
| 432 | } |
| 433 | |
Yanray Wang | bd56b03 | 2023-03-14 14:36:48 +0800 | [diff] [blame] | 434 | int mbedtls_test_mock_tcp_send_msg(void *ctx, |
| 435 | const unsigned char *buf, size_t len) |
| 436 | { |
| 437 | mbedtls_test_ssl_message_queue *queue; |
| 438 | mbedtls_test_mock_socket *socket; |
| 439 | mbedtls_test_message_socket_context *context = |
| 440 | (mbedtls_test_message_socket_context *) ctx; |
| 441 | |
| 442 | if (context == NULL || context->socket == NULL |
| 443 | || context->queue_output == NULL) { |
| 444 | return MBEDTLS_TEST_ERROR_CONTEXT_ERROR; |
| 445 | } |
| 446 | |
| 447 | queue = context->queue_output; |
| 448 | socket = context->socket; |
| 449 | |
| 450 | if (queue->num >= queue->capacity) { |
| 451 | return MBEDTLS_ERR_SSL_WANT_WRITE; |
| 452 | } |
| 453 | |
| 454 | if (mbedtls_test_mock_tcp_send_b(socket, buf, len) != (int) len) { |
| 455 | return MBEDTLS_TEST_ERROR_SEND_FAILED; |
| 456 | } |
| 457 | |
| 458 | return mbedtls_test_ssl_message_queue_push_info(queue, len); |
| 459 | } |
| 460 | |
Yanray Wang | bd56b03 | 2023-03-14 14:36:48 +0800 | [diff] [blame] | 461 | int mbedtls_test_mock_tcp_recv_msg(void *ctx, |
| 462 | unsigned char *buf, size_t buf_len) |
| 463 | { |
| 464 | mbedtls_test_ssl_message_queue *queue; |
| 465 | mbedtls_test_mock_socket *socket; |
| 466 | mbedtls_test_message_socket_context *context = |
| 467 | (mbedtls_test_message_socket_context *) ctx; |
| 468 | size_t drop_len = 0; |
| 469 | size_t msg_len; |
| 470 | int ret; |
| 471 | |
| 472 | if (context == NULL || context->socket == NULL |
| 473 | || context->queue_input == NULL) { |
| 474 | return MBEDTLS_TEST_ERROR_CONTEXT_ERROR; |
| 475 | } |
| 476 | |
| 477 | queue = context->queue_input; |
| 478 | socket = context->socket; |
| 479 | |
| 480 | /* Peek first, so that in case of a socket error the data remains in |
| 481 | * the queue. */ |
Yanray Wang | c4abee2 | 2023-03-16 14:57:54 +0800 | [diff] [blame] | 482 | ret = test_ssl_message_queue_peek_info(queue, buf_len, &msg_len); |
Yanray Wang | bd56b03 | 2023-03-14 14:36:48 +0800 | [diff] [blame] | 483 | if (ret == MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED) { |
| 484 | /* Calculate how much to drop */ |
| 485 | drop_len = msg_len - buf_len; |
| 486 | |
| 487 | /* Set the requested message len to be buffer length */ |
| 488 | msg_len = buf_len; |
| 489 | } else if (ret != 0) { |
| 490 | return ret; |
| 491 | } |
| 492 | |
| 493 | if (mbedtls_test_mock_tcp_recv_b(socket, buf, msg_len) != (int) msg_len) { |
| 494 | return MBEDTLS_TEST_ERROR_RECV_FAILED; |
| 495 | } |
| 496 | |
| 497 | if (ret == MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED) { |
| 498 | /* Drop the remaining part of the message */ |
| 499 | if (mbedtls_test_mock_tcp_recv_b(socket, NULL, drop_len) != |
| 500 | (int) drop_len) { |
| 501 | /* Inconsistent state - part of the message was read, |
| 502 | * and a part couldn't. Not much we can do here, but it should not |
| 503 | * happen in test environment, unless forced manually. */ |
| 504 | } |
| 505 | } |
Tomás González | a526528 | 2024-02-01 11:12:20 +0000 | [diff] [blame] | 506 | ret = mbedtls_test_ssl_message_queue_pop_info(queue, buf_len); |
| 507 | if (ret < 0) { |
| 508 | return ret; |
| 509 | } |
Yanray Wang | bd56b03 | 2023-03-14 14:36:48 +0800 | [diff] [blame] | 510 | |
Yanray Wang | d2696f2 | 2022-11-03 11:51:59 +0800 | [diff] [blame] | 511 | return (msg_len > INT_MAX) ? INT_MAX : (int) msg_len; |
Yanray Wang | bd56b03 | 2023-03-14 14:36:48 +0800 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) && \ |
| 515 | defined(MBEDTLS_CERTS_C) && \ |
| 516 | defined(MBEDTLS_ENTROPY_C) && \ |
| 517 | defined(MBEDTLS_CTR_DRBG_C) |
| 518 | |
| 519 | /* |
| 520 | * Deinitializes certificates from endpoint represented by \p ep. |
| 521 | */ |
Yanray Wang | cd23aff | 2023-03-15 16:05:14 +0800 | [diff] [blame] | 522 | static void test_ssl_endpoint_certificate_free(mbedtls_test_ssl_endpoint *ep) |
Yanray Wang | bd56b03 | 2023-03-14 14:36:48 +0800 | [diff] [blame] | 523 | { |
| 524 | mbedtls_test_ssl_endpoint_certificate *cert = &(ep->cert); |
| 525 | if (cert != NULL) { |
| 526 | if (cert->ca_cert != NULL) { |
| 527 | mbedtls_x509_crt_free(cert->ca_cert); |
| 528 | mbedtls_free(cert->ca_cert); |
| 529 | cert->ca_cert = NULL; |
| 530 | } |
| 531 | if (cert->cert != NULL) { |
| 532 | mbedtls_x509_crt_free(cert->cert); |
| 533 | mbedtls_free(cert->cert); |
| 534 | cert->cert = NULL; |
| 535 | } |
| 536 | if (cert->pkey != NULL) { |
| 537 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 538 | if (mbedtls_pk_get_type(cert->pkey) == MBEDTLS_PK_OPAQUE) { |
| 539 | mbedtls_svc_key_id_t *key_slot = cert->pkey->pk_ctx; |
| 540 | psa_destroy_key(*key_slot); |
| 541 | } |
| 542 | #endif |
| 543 | mbedtls_pk_free(cert->pkey); |
| 544 | mbedtls_free(cert->pkey); |
| 545 | cert->pkey = NULL; |
| 546 | } |
| 547 | } |
| 548 | } |
| 549 | |
Yanray Wang | bd56b03 | 2023-03-14 14:36:48 +0800 | [diff] [blame] | 550 | int mbedtls_test_ssl_endpoint_certificate_init(mbedtls_test_ssl_endpoint *ep, |
| 551 | int pk_alg) |
| 552 | { |
| 553 | int i = 0; |
| 554 | int ret = -1; |
| 555 | mbedtls_test_ssl_endpoint_certificate *cert = NULL; |
| 556 | |
| 557 | if (ep == NULL) { |
| 558 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 559 | } |
| 560 | |
| 561 | cert = &(ep->cert); |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 562 | TEST_CALLOC(cert->ca_cert, 1); |
| 563 | TEST_CALLOC(cert->cert, 1); |
| 564 | TEST_CALLOC(cert->pkey, 1); |
Yanray Wang | bd56b03 | 2023-03-14 14:36:48 +0800 | [diff] [blame] | 565 | |
| 566 | mbedtls_x509_crt_init(cert->ca_cert); |
| 567 | mbedtls_x509_crt_init(cert->cert); |
| 568 | mbedtls_pk_init(cert->pkey); |
| 569 | |
| 570 | /* Load the trusted CA */ |
| 571 | |
| 572 | for (i = 0; mbedtls_test_cas_der[i] != NULL; i++) { |
| 573 | ret = mbedtls_x509_crt_parse_der( |
| 574 | cert->ca_cert, |
| 575 | (const unsigned char *) mbedtls_test_cas_der[i], |
| 576 | mbedtls_test_cas_der_len[i]); |
| 577 | TEST_ASSERT(ret == 0); |
| 578 | } |
| 579 | |
| 580 | /* Load own certificate and private key */ |
| 581 | |
| 582 | if (ep->conf.endpoint == MBEDTLS_SSL_IS_SERVER) { |
| 583 | if (pk_alg == MBEDTLS_PK_RSA) { |
| 584 | ret = mbedtls_x509_crt_parse( |
| 585 | cert->cert, |
| 586 | (const unsigned char *) mbedtls_test_srv_crt_rsa_sha256_der, |
| 587 | mbedtls_test_srv_crt_rsa_sha256_der_len); |
| 588 | TEST_ASSERT(ret == 0); |
| 589 | |
| 590 | ret = mbedtls_pk_parse_key( |
| 591 | cert->pkey, |
| 592 | (const unsigned char *) mbedtls_test_srv_key_rsa_der, |
| 593 | mbedtls_test_srv_key_rsa_der_len, NULL, 0); |
| 594 | TEST_ASSERT(ret == 0); |
| 595 | } else { |
| 596 | ret = mbedtls_x509_crt_parse( |
| 597 | cert->cert, |
| 598 | (const unsigned char *) mbedtls_test_srv_crt_ec_der, |
| 599 | mbedtls_test_srv_crt_ec_der_len); |
| 600 | TEST_ASSERT(ret == 0); |
| 601 | |
| 602 | ret = mbedtls_pk_parse_key( |
| 603 | cert->pkey, |
| 604 | (const unsigned char *) mbedtls_test_srv_key_ec_der, |
| 605 | mbedtls_test_srv_key_ec_der_len, NULL, 0); |
| 606 | TEST_ASSERT(ret == 0); |
| 607 | } |
| 608 | } else { |
| 609 | if (pk_alg == MBEDTLS_PK_RSA) { |
| 610 | ret = mbedtls_x509_crt_parse( |
| 611 | cert->cert, |
| 612 | (const unsigned char *) mbedtls_test_cli_crt_rsa_der, |
| 613 | mbedtls_test_cli_crt_rsa_der_len); |
| 614 | TEST_ASSERT(ret == 0); |
| 615 | |
| 616 | ret = mbedtls_pk_parse_key( |
| 617 | cert->pkey, |
| 618 | (const unsigned char *) mbedtls_test_cli_key_rsa_der, |
| 619 | mbedtls_test_cli_key_rsa_der_len, NULL, 0); |
| 620 | TEST_ASSERT(ret == 0); |
| 621 | } else { |
| 622 | ret = mbedtls_x509_crt_parse( |
| 623 | cert->cert, |
| 624 | (const unsigned char *) mbedtls_test_cli_crt_ec_der, |
| 625 | mbedtls_test_cli_crt_ec_len); |
| 626 | TEST_ASSERT(ret == 0); |
| 627 | |
| 628 | ret = mbedtls_pk_parse_key( |
| 629 | cert->pkey, |
| 630 | (const unsigned char *) mbedtls_test_cli_key_ec_der, |
| 631 | mbedtls_test_cli_key_ec_der_len, NULL, 0); |
| 632 | TEST_ASSERT(ret == 0); |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | mbedtls_ssl_conf_ca_chain(&(ep->conf), cert->ca_cert, NULL); |
| 637 | |
| 638 | ret = mbedtls_ssl_conf_own_cert(&(ep->conf), cert->cert, |
| 639 | cert->pkey); |
| 640 | TEST_ASSERT(ret == 0); |
| 641 | |
| 642 | exit: |
| 643 | if (ret != 0) { |
Yanray Wang | cd23aff | 2023-03-15 16:05:14 +0800 | [diff] [blame] | 644 | test_ssl_endpoint_certificate_free(ep); |
Yanray Wang | bd56b03 | 2023-03-14 14:36:48 +0800 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | return ret; |
| 648 | } |
| 649 | |
Yanray Wang | bd56b03 | 2023-03-14 14:36:48 +0800 | [diff] [blame] | 650 | int mbedtls_test_ssl_endpoint_init( |
| 651 | mbedtls_test_ssl_endpoint *ep, int endpoint_type, int pk_alg, |
| 652 | mbedtls_test_message_socket_context *dtls_context, |
| 653 | mbedtls_test_ssl_message_queue *input_queue, |
| 654 | mbedtls_test_ssl_message_queue *output_queue, |
| 655 | const mbedtls_ecp_group_id *curves) |
| 656 | { |
| 657 | int ret = -1; |
| 658 | |
| 659 | if (dtls_context != NULL && |
| 660 | (input_queue == NULL || output_queue == NULL)) { |
| 661 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 662 | } |
| 663 | |
| 664 | if (ep == NULL) { |
| 665 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 666 | } |
| 667 | |
| 668 | memset(ep, 0, sizeof(*ep)); |
| 669 | |
| 670 | ep->name = (endpoint_type == MBEDTLS_SSL_IS_SERVER) ? "Server" : "Client"; |
| 671 | |
| 672 | mbedtls_ssl_init(&(ep->ssl)); |
| 673 | mbedtls_ssl_config_init(&(ep->conf)); |
| 674 | mbedtls_ctr_drbg_init(&(ep->ctr_drbg)); |
| 675 | mbedtls_ssl_conf_rng(&(ep->conf), |
| 676 | mbedtls_ctr_drbg_random, |
| 677 | &(ep->ctr_drbg)); |
| 678 | mbedtls_entropy_init(&(ep->entropy)); |
| 679 | if (dtls_context != NULL) { |
| 680 | TEST_ASSERT(mbedtls_test_message_socket_setup(input_queue, output_queue, |
| 681 | 100, &(ep->socket), |
| 682 | dtls_context) == 0); |
| 683 | } else { |
Yanray Wang | d02c317 | 2023-03-15 16:02:29 +0800 | [diff] [blame] | 684 | mbedtls_test_mock_socket_init(&(ep->socket)); |
Yanray Wang | bd56b03 | 2023-03-14 14:36:48 +0800 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | ret = mbedtls_ctr_drbg_seed(&(ep->ctr_drbg), mbedtls_entropy_func, |
| 688 | &(ep->entropy), |
| 689 | (const unsigned char *) (ep->name), |
| 690 | strlen(ep->name)); |
| 691 | TEST_ASSERT(ret == 0); |
| 692 | |
| 693 | /* Non-blocking callbacks without timeout */ |
| 694 | if (dtls_context != NULL) { |
| 695 | mbedtls_ssl_set_bio(&(ep->ssl), dtls_context, |
| 696 | mbedtls_test_mock_tcp_send_msg, |
| 697 | mbedtls_test_mock_tcp_recv_msg, |
| 698 | NULL); |
| 699 | } else { |
| 700 | mbedtls_ssl_set_bio(&(ep->ssl), &(ep->socket), |
| 701 | mbedtls_test_mock_tcp_send_nb, |
| 702 | mbedtls_test_mock_tcp_recv_nb, |
| 703 | NULL); |
| 704 | } |
| 705 | |
| 706 | ret = mbedtls_ssl_config_defaults(&(ep->conf), endpoint_type, |
| 707 | (dtls_context != NULL) ? |
| 708 | MBEDTLS_SSL_TRANSPORT_DATAGRAM : |
| 709 | MBEDTLS_SSL_TRANSPORT_STREAM, |
| 710 | MBEDTLS_SSL_PRESET_DEFAULT); |
| 711 | TEST_ASSERT(ret == 0); |
| 712 | |
| 713 | #if defined(MBEDTLS_ECP_C) |
| 714 | if (curves != NULL) { |
| 715 | mbedtls_ssl_conf_curves(&(ep->conf), curves); |
| 716 | } |
| 717 | #else |
| 718 | (void) curves; |
| 719 | #endif |
| 720 | |
| 721 | ret = mbedtls_ssl_setup(&(ep->ssl), &(ep->conf)); |
| 722 | TEST_ASSERT(ret == 0); |
| 723 | |
| 724 | #if defined(MBEDTLS_SSL_PROTO_DTLS) && defined(MBEDTLS_SSL_SRV_C) |
| 725 | if (endpoint_type == MBEDTLS_SSL_IS_SERVER && dtls_context != NULL) { |
| 726 | mbedtls_ssl_conf_dtls_cookies(&(ep->conf), NULL, NULL, NULL); |
| 727 | } |
| 728 | #endif |
| 729 | |
| 730 | ret = mbedtls_test_ssl_endpoint_certificate_init(ep, pk_alg); |
| 731 | TEST_ASSERT(ret == 0); |
| 732 | |
| 733 | exit: |
| 734 | return ret; |
| 735 | } |
| 736 | |
Yanray Wang | bd56b03 | 2023-03-14 14:36:48 +0800 | [diff] [blame] | 737 | void mbedtls_test_ssl_endpoint_free( |
| 738 | mbedtls_test_ssl_endpoint *ep, |
| 739 | mbedtls_test_message_socket_context *context) |
| 740 | { |
Yanray Wang | cd23aff | 2023-03-15 16:05:14 +0800 | [diff] [blame] | 741 | test_ssl_endpoint_certificate_free(ep); |
Yanray Wang | bd56b03 | 2023-03-14 14:36:48 +0800 | [diff] [blame] | 742 | |
| 743 | mbedtls_ssl_free(&(ep->ssl)); |
| 744 | mbedtls_ssl_config_free(&(ep->conf)); |
| 745 | mbedtls_ctr_drbg_free(&(ep->ctr_drbg)); |
| 746 | mbedtls_entropy_free(&(ep->entropy)); |
| 747 | |
| 748 | if (context != NULL) { |
| 749 | mbedtls_test_message_socket_close(context); |
| 750 | } else { |
| 751 | mbedtls_test_mock_socket_close(&(ep->socket)); |
| 752 | } |
| 753 | } |
| 754 | |
Yanray Wang | bd56b03 | 2023-03-14 14:36:48 +0800 | [diff] [blame] | 755 | int mbedtls_test_move_handshake_to_state(mbedtls_ssl_context *ssl, |
| 756 | mbedtls_ssl_context *second_ssl, |
| 757 | int state) |
| 758 | { |
| 759 | enum { BUFFSIZE = 1024 }; |
| 760 | int max_steps = 1000; |
| 761 | int ret = 0; |
| 762 | |
| 763 | if (ssl == NULL || second_ssl == NULL) { |
| 764 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 765 | } |
| 766 | |
| 767 | /* Perform communication via connected sockets */ |
| 768 | while ((ssl->state != state) && (--max_steps >= 0)) { |
| 769 | /* If /p second_ssl ends the handshake procedure before /p ssl then |
| 770 | * there is no need to call the next step */ |
| 771 | if (second_ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) { |
| 772 | ret = mbedtls_ssl_handshake_step(second_ssl); |
| 773 | if (ret != 0 && ret != MBEDTLS_ERR_SSL_WANT_READ && |
| 774 | ret != MBEDTLS_ERR_SSL_WANT_WRITE) { |
| 775 | return ret; |
| 776 | } |
| 777 | } |
| 778 | |
| 779 | /* We only care about the \p ssl state and returns, so we call it last, |
| 780 | * to leave the iteration as soon as the state is as expected. */ |
| 781 | ret = mbedtls_ssl_handshake_step(ssl); |
| 782 | if (ret != 0 && ret != MBEDTLS_ERR_SSL_WANT_READ && |
| 783 | ret != MBEDTLS_ERR_SSL_WANT_WRITE) { |
| 784 | return ret; |
| 785 | } |
| 786 | } |
| 787 | |
| 788 | return (max_steps >= 0) ? ret : -1; |
| 789 | } |
| 790 | |
Yanray Wang | 1ef77c0 | 2023-03-14 16:59:00 +0800 | [diff] [blame] | 791 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED && MBEDTLS_CERTS_C && |
| 792 | MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */ |
Yanray Wang | bd56b03 | 2023-03-14 14:36:48 +0800 | [diff] [blame] | 793 | |
| 794 | /* |
| 795 | * Write application data. Increase write counter if necessary. |
| 796 | */ |
| 797 | int mbedtls_ssl_write_fragment(mbedtls_ssl_context *ssl, |
| 798 | unsigned char *buf, int buf_len, |
| 799 | int *written, |
| 800 | const int expected_fragments) |
| 801 | { |
Agathiyan Bragadeesh | 7b0ee1e | 2023-07-27 15:51:46 +0100 | [diff] [blame] | 802 | int ret; |
Agathiyan Bragadeesh | e7eb805 | 2023-07-31 16:16:38 +0100 | [diff] [blame] | 803 | |
Yanray Wang | bd56b03 | 2023-03-14 14:36:48 +0800 | [diff] [blame] | 804 | /* Verify that calling mbedtls_ssl_write with a NULL buffer and zero length is |
| 805 | * a valid no-op for TLS connections. */ |
| 806 | if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 807 | TEST_ASSERT(mbedtls_ssl_write(ssl, NULL, 0) == 0); |
| 808 | } |
| 809 | |
Agathiyan Bragadeesh | 7b0ee1e | 2023-07-27 15:51:46 +0100 | [diff] [blame] | 810 | ret = mbedtls_ssl_write(ssl, buf + *written, buf_len - *written); |
Yanray Wang | bd56b03 | 2023-03-14 14:36:48 +0800 | [diff] [blame] | 811 | if (ret > 0) { |
| 812 | *written += ret; |
| 813 | } |
| 814 | |
| 815 | if (expected_fragments == 0) { |
| 816 | /* Used for DTLS and the message size larger than MFL. In that case |
| 817 | * the message can not be fragmented and the library should return |
| 818 | * MBEDTLS_ERR_SSL_BAD_INPUT_DATA error. This error must be returned |
Yanray Wang | 74df201 | 2023-03-16 12:15:49 +0800 | [diff] [blame] | 819 | * to prevent a dead loop inside mbedtls_test_ssl_exchange_data(). */ |
Yanray Wang | bd56b03 | 2023-03-14 14:36:48 +0800 | [diff] [blame] | 820 | return ret; |
| 821 | } else if (expected_fragments == 1) { |
| 822 | /* Used for TLS/DTLS and the message size lower than MFL */ |
| 823 | TEST_ASSERT(ret == buf_len || |
| 824 | ret == MBEDTLS_ERR_SSL_WANT_READ || |
| 825 | ret == MBEDTLS_ERR_SSL_WANT_WRITE); |
| 826 | } else { |
| 827 | /* Used for TLS and the message size larger than MFL */ |
| 828 | TEST_ASSERT(expected_fragments > 1); |
| 829 | TEST_ASSERT((ret >= 0 && ret <= buf_len) || |
| 830 | ret == MBEDTLS_ERR_SSL_WANT_READ || |
| 831 | ret == MBEDTLS_ERR_SSL_WANT_WRITE); |
| 832 | } |
| 833 | |
| 834 | return 0; |
| 835 | |
| 836 | exit: |
| 837 | /* Some of the tests failed */ |
| 838 | return -1; |
| 839 | } |
| 840 | |
| 841 | /* |
| 842 | * Read application data and increase read counter and fragments counter |
| 843 | * if necessary. |
| 844 | */ |
| 845 | int mbedtls_ssl_read_fragment(mbedtls_ssl_context *ssl, |
| 846 | unsigned char *buf, int buf_len, |
| 847 | int *read, int *fragments, |
| 848 | const int expected_fragments) |
| 849 | { |
Agathiyan Bragadeesh | 7b0ee1e | 2023-07-27 15:51:46 +0100 | [diff] [blame] | 850 | int ret; |
Agathiyan Bragadeesh | e7eb805 | 2023-07-31 16:16:38 +0100 | [diff] [blame] | 851 | |
Yanray Wang | bd56b03 | 2023-03-14 14:36:48 +0800 | [diff] [blame] | 852 | /* Verify that calling mbedtls_ssl_write with a NULL buffer and zero length is |
| 853 | * a valid no-op for TLS connections. */ |
| 854 | if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 855 | TEST_ASSERT(mbedtls_ssl_read(ssl, NULL, 0) == 0); |
| 856 | } |
| 857 | |
Agathiyan Bragadeesh | 7b0ee1e | 2023-07-27 15:51:46 +0100 | [diff] [blame] | 858 | ret = mbedtls_ssl_read(ssl, buf + *read, buf_len - *read); |
Yanray Wang | bd56b03 | 2023-03-14 14:36:48 +0800 | [diff] [blame] | 859 | if (ret > 0) { |
| 860 | (*fragments)++; |
| 861 | *read += ret; |
| 862 | } |
| 863 | |
| 864 | if (expected_fragments == 0) { |
| 865 | TEST_ASSERT(ret == 0); |
| 866 | } else if (expected_fragments == 1) { |
| 867 | TEST_ASSERT(ret == buf_len || |
| 868 | ret == MBEDTLS_ERR_SSL_WANT_READ || |
| 869 | ret == MBEDTLS_ERR_SSL_WANT_WRITE); |
| 870 | } else { |
| 871 | TEST_ASSERT(expected_fragments > 1); |
| 872 | TEST_ASSERT((ret >= 0 && ret <= buf_len) || |
| 873 | ret == MBEDTLS_ERR_SSL_WANT_READ || |
| 874 | ret == MBEDTLS_ERR_SSL_WANT_WRITE); |
| 875 | } |
| 876 | |
| 877 | return 0; |
| 878 | |
| 879 | exit: |
| 880 | /* Some of the tests failed */ |
| 881 | return -1; |
| 882 | } |
| 883 | |
Yanray Wang | b4ef9a2 | 2023-03-16 12:04:49 +0800 | [diff] [blame] | 884 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) && \ |
| 885 | defined(MBEDTLS_CERTS_C) && \ |
| 886 | defined(MBEDTLS_ENTROPY_C) && \ |
| 887 | defined(MBEDTLS_CTR_DRBG_C) |
| 888 | static void set_ciphersuite(mbedtls_ssl_config *conf, const char *cipher, |
| 889 | int *forced_ciphersuite) |
Yanray Wang | bd56b03 | 2023-03-14 14:36:48 +0800 | [diff] [blame] | 890 | { |
| 891 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info; |
| 892 | forced_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id(cipher); |
| 893 | forced_ciphersuite[1] = 0; |
| 894 | |
| 895 | ciphersuite_info = |
| 896 | mbedtls_ssl_ciphersuite_from_id(forced_ciphersuite[0]); |
| 897 | |
| 898 | TEST_ASSERT(ciphersuite_info != NULL); |
| 899 | TEST_ASSERT(ciphersuite_info->min_minor_ver <= conf->max_minor_ver); |
| 900 | TEST_ASSERT(ciphersuite_info->max_minor_ver >= conf->min_minor_ver); |
| 901 | |
| 902 | if (conf->max_minor_ver > ciphersuite_info->max_minor_ver) { |
| 903 | conf->max_minor_ver = ciphersuite_info->max_minor_ver; |
| 904 | } |
| 905 | if (conf->min_minor_ver < ciphersuite_info->min_minor_ver) { |
| 906 | conf->min_minor_ver = ciphersuite_info->min_minor_ver; |
| 907 | } |
| 908 | |
| 909 | mbedtls_ssl_conf_ciphersuites(conf, forced_ciphersuite); |
| 910 | |
| 911 | exit: |
| 912 | return; |
| 913 | } |
Yanray Wang | b4ef9a2 | 2023-03-16 12:04:49 +0800 | [diff] [blame] | 914 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED && MBEDTLS_CERTS_C && |
| 915 | MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */ |
Yanray Wang | bd56b03 | 2023-03-14 14:36:48 +0800 | [diff] [blame] | 916 | |
Yanray Wang | b4ef9a2 | 2023-03-16 12:04:49 +0800 | [diff] [blame] | 917 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) && \ |
| 918 | defined(MBEDTLS_CERTS_C) && \ |
| 919 | defined(MBEDTLS_ENTROPY_C) && \ |
| 920 | defined(MBEDTLS_CTR_DRBG_C) && \ |
| 921 | defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
| 922 | static int psk_dummy_callback(void *p_info, mbedtls_ssl_context *ssl, |
| 923 | const unsigned char *name, size_t name_len) |
Yanray Wang | bd56b03 | 2023-03-14 14:36:48 +0800 | [diff] [blame] | 924 | { |
| 925 | (void) p_info; |
| 926 | (void) ssl; |
| 927 | (void) name; |
| 928 | (void) name_len; |
| 929 | |
| 930 | return 0; |
| 931 | } |
Yanray Wang | b4ef9a2 | 2023-03-16 12:04:49 +0800 | [diff] [blame] | 932 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED && MBEDTLS_CERTS_C && |
| 933 | MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C && |
| 934 | MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
Yanray Wang | bd56b03 | 2023-03-14 14:36:48 +0800 | [diff] [blame] | 935 | |
Yanray Wang | bd56b03 | 2023-03-14 14:36:48 +0800 | [diff] [blame] | 936 | int mbedtls_test_ssl_build_transforms(mbedtls_ssl_transform *t_in, |
| 937 | mbedtls_ssl_transform *t_out, |
| 938 | int cipher_type, int hash_id, |
| 939 | int etm, int tag_mode, int ver, |
| 940 | size_t cid0_len, |
| 941 | size_t cid1_len) |
| 942 | { |
| 943 | mbedtls_cipher_info_t const *cipher_info; |
| 944 | int ret = 0; |
| 945 | |
| 946 | size_t keylen, maclen, ivlen; |
| 947 | unsigned char *key0 = NULL, *key1 = NULL; |
| 948 | unsigned char *md0 = NULL, *md1 = NULL; |
| 949 | unsigned char iv_enc[16], iv_dec[16]; |
| 950 | |
| 951 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 952 | unsigned char cid0[SSL_CID_LEN_MIN]; |
| 953 | unsigned char cid1[SSL_CID_LEN_MIN]; |
| 954 | |
| 955 | mbedtls_test_rnd_std_rand(NULL, cid0, sizeof(cid0)); |
| 956 | mbedtls_test_rnd_std_rand(NULL, cid1, sizeof(cid1)); |
| 957 | #else |
| 958 | ((void) cid0_len); |
| 959 | ((void) cid1_len); |
| 960 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 961 | |
| 962 | maclen = 0; |
| 963 | |
| 964 | /* Pick cipher */ |
| 965 | cipher_info = mbedtls_cipher_info_from_type(cipher_type); |
| 966 | CHK(cipher_info != NULL); |
| 967 | CHK(cipher_info->iv_size <= 16); |
| 968 | CHK(cipher_info->key_bitlen % 8 == 0); |
| 969 | |
| 970 | /* Pick keys */ |
| 971 | keylen = cipher_info->key_bitlen / 8; |
| 972 | /* Allocate `keylen + 1` bytes to ensure that we get |
| 973 | * a non-NULL pointers from `mbedtls_calloc` even if |
| 974 | * `keylen == 0` in the case of the NULL cipher. */ |
| 975 | CHK((key0 = mbedtls_calloc(1, keylen + 1)) != NULL); |
| 976 | CHK((key1 = mbedtls_calloc(1, keylen + 1)) != NULL); |
| 977 | memset(key0, 0x1, keylen); |
| 978 | memset(key1, 0x2, keylen); |
| 979 | |
| 980 | /* Setup cipher contexts */ |
| 981 | CHK(mbedtls_cipher_setup(&t_in->cipher_ctx_enc, cipher_info) == 0); |
| 982 | CHK(mbedtls_cipher_setup(&t_in->cipher_ctx_dec, cipher_info) == 0); |
| 983 | CHK(mbedtls_cipher_setup(&t_out->cipher_ctx_enc, cipher_info) == 0); |
| 984 | CHK(mbedtls_cipher_setup(&t_out->cipher_ctx_dec, cipher_info) == 0); |
| 985 | |
| 986 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
| 987 | if (cipher_info->mode == MBEDTLS_MODE_CBC) { |
| 988 | CHK(mbedtls_cipher_set_padding_mode(&t_in->cipher_ctx_enc, |
| 989 | MBEDTLS_PADDING_NONE) == 0); |
| 990 | CHK(mbedtls_cipher_set_padding_mode(&t_in->cipher_ctx_dec, |
| 991 | MBEDTLS_PADDING_NONE) == 0); |
| 992 | CHK(mbedtls_cipher_set_padding_mode(&t_out->cipher_ctx_enc, |
| 993 | MBEDTLS_PADDING_NONE) == 0); |
| 994 | CHK(mbedtls_cipher_set_padding_mode(&t_out->cipher_ctx_dec, |
| 995 | MBEDTLS_PADDING_NONE) == 0); |
| 996 | } |
| 997 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
| 998 | |
| 999 | CHK(mbedtls_cipher_setkey(&t_in->cipher_ctx_enc, key0, |
Yanray Wang | d2696f2 | 2022-11-03 11:51:59 +0800 | [diff] [blame] | 1000 | (keylen << 3 > INT_MAX) ? INT_MAX : (int) keylen << 3, |
| 1001 | MBEDTLS_ENCRYPT) |
| 1002 | == 0); |
Yanray Wang | bd56b03 | 2023-03-14 14:36:48 +0800 | [diff] [blame] | 1003 | CHK(mbedtls_cipher_setkey(&t_in->cipher_ctx_dec, key1, |
Yanray Wang | d2696f2 | 2022-11-03 11:51:59 +0800 | [diff] [blame] | 1004 | (keylen << 3 > INT_MAX) ? INT_MAX : (int) keylen << 3, |
| 1005 | MBEDTLS_DECRYPT) |
| 1006 | == 0); |
Yanray Wang | bd56b03 | 2023-03-14 14:36:48 +0800 | [diff] [blame] | 1007 | CHK(mbedtls_cipher_setkey(&t_out->cipher_ctx_enc, key1, |
Yanray Wang | d2696f2 | 2022-11-03 11:51:59 +0800 | [diff] [blame] | 1008 | (keylen << 3 > INT_MAX) ? INT_MAX : (int) keylen << 3, |
| 1009 | MBEDTLS_ENCRYPT) |
| 1010 | == 0); |
Yanray Wang | bd56b03 | 2023-03-14 14:36:48 +0800 | [diff] [blame] | 1011 | CHK(mbedtls_cipher_setkey(&t_out->cipher_ctx_dec, key0, |
Yanray Wang | d2696f2 | 2022-11-03 11:51:59 +0800 | [diff] [blame] | 1012 | (keylen << 3 > INT_MAX) ? INT_MAX : (int) keylen << 3, |
| 1013 | MBEDTLS_DECRYPT) |
| 1014 | == 0); |
Yanray Wang | bd56b03 | 2023-03-14 14:36:48 +0800 | [diff] [blame] | 1015 | |
| 1016 | /* Setup MAC contexts */ |
| 1017 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
| 1018 | if (cipher_info->mode == MBEDTLS_MODE_CBC || |
| 1019 | cipher_info->mode == MBEDTLS_MODE_STREAM) { |
| 1020 | mbedtls_md_info_t const *md_info; |
| 1021 | |
| 1022 | /* Pick hash */ |
| 1023 | md_info = mbedtls_md_info_from_type(hash_id); |
| 1024 | CHK(md_info != NULL); |
| 1025 | |
| 1026 | /* Pick hash keys */ |
| 1027 | maclen = mbedtls_md_get_size(md_info); |
| 1028 | CHK((md0 = mbedtls_calloc(1, maclen)) != NULL); |
| 1029 | CHK((md1 = mbedtls_calloc(1, maclen)) != NULL); |
| 1030 | memset(md0, 0x5, maclen); |
| 1031 | memset(md1, 0x6, maclen); |
| 1032 | |
| 1033 | CHK(mbedtls_md_setup(&t_out->md_ctx_enc, md_info, 1) == 0); |
| 1034 | CHK(mbedtls_md_setup(&t_out->md_ctx_dec, md_info, 1) == 0); |
| 1035 | CHK(mbedtls_md_setup(&t_in->md_ctx_enc, md_info, 1) == 0); |
| 1036 | CHK(mbedtls_md_setup(&t_in->md_ctx_dec, md_info, 1) == 0); |
| 1037 | |
| 1038 | if (ver > MBEDTLS_SSL_MINOR_VERSION_0) { |
| 1039 | CHK(mbedtls_md_hmac_starts(&t_in->md_ctx_enc, |
| 1040 | md0, maclen) == 0); |
| 1041 | CHK(mbedtls_md_hmac_starts(&t_in->md_ctx_dec, |
| 1042 | md1, maclen) == 0); |
| 1043 | CHK(mbedtls_md_hmac_starts(&t_out->md_ctx_enc, |
| 1044 | md1, maclen) == 0); |
| 1045 | CHK(mbedtls_md_hmac_starts(&t_out->md_ctx_dec, |
| 1046 | md0, maclen) == 0); |
| 1047 | } |
| 1048 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
| 1049 | else { |
| 1050 | memcpy(&t_in->mac_enc, md0, maclen); |
| 1051 | memcpy(&t_in->mac_dec, md1, maclen); |
| 1052 | memcpy(&t_out->mac_enc, md1, maclen); |
| 1053 | memcpy(&t_out->mac_dec, md0, maclen); |
| 1054 | } |
| 1055 | #endif |
| 1056 | } |
| 1057 | #else |
| 1058 | ((void) hash_id); |
| 1059 | #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ |
| 1060 | |
| 1061 | |
| 1062 | /* Pick IV's (regardless of whether they |
| 1063 | * are being used by the transform). */ |
| 1064 | ivlen = cipher_info->iv_size; |
| 1065 | memset(iv_enc, 0x3, sizeof(iv_enc)); |
| 1066 | memset(iv_dec, 0x4, sizeof(iv_dec)); |
| 1067 | |
| 1068 | /* |
| 1069 | * Setup transforms |
| 1070 | */ |
| 1071 | |
| 1072 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \ |
| 1073 | defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
| 1074 | t_out->encrypt_then_mac = etm; |
| 1075 | t_in->encrypt_then_mac = etm; |
| 1076 | #else |
| 1077 | ((void) etm); |
| 1078 | #endif |
| 1079 | |
| 1080 | t_out->minor_ver = ver; |
| 1081 | t_in->minor_ver = ver; |
| 1082 | t_out->ivlen = ivlen; |
| 1083 | t_in->ivlen = ivlen; |
| 1084 | |
| 1085 | switch (cipher_info->mode) { |
| 1086 | case MBEDTLS_MODE_GCM: |
| 1087 | case MBEDTLS_MODE_CCM: |
| 1088 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL) |
| 1089 | if (ver == MBEDTLS_SSL_MINOR_VERSION_4) { |
| 1090 | t_out->fixed_ivlen = 12; |
| 1091 | t_in->fixed_ivlen = 12; |
| 1092 | } else |
| 1093 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ |
| 1094 | { |
| 1095 | t_out->fixed_ivlen = 4; |
| 1096 | t_in->fixed_ivlen = 4; |
| 1097 | } |
| 1098 | t_out->maclen = 0; |
| 1099 | t_in->maclen = 0; |
| 1100 | switch (tag_mode) { |
| 1101 | case 0: /* Full tag */ |
| 1102 | t_out->taglen = 16; |
| 1103 | t_in->taglen = 16; |
| 1104 | break; |
| 1105 | case 1: /* Partial tag */ |
| 1106 | t_out->taglen = 8; |
| 1107 | t_in->taglen = 8; |
| 1108 | break; |
| 1109 | default: |
| 1110 | ret = 1; |
| 1111 | goto cleanup; |
| 1112 | } |
| 1113 | break; |
| 1114 | |
| 1115 | case MBEDTLS_MODE_CHACHAPOLY: |
| 1116 | t_out->fixed_ivlen = 12; |
| 1117 | t_in->fixed_ivlen = 12; |
| 1118 | t_out->maclen = 0; |
| 1119 | t_in->maclen = 0; |
| 1120 | switch (tag_mode) { |
| 1121 | case 0: /* Full tag */ |
| 1122 | t_out->taglen = 16; |
| 1123 | t_in->taglen = 16; |
| 1124 | break; |
| 1125 | case 1: /* Partial tag */ |
| 1126 | t_out->taglen = 8; |
| 1127 | t_in->taglen = 8; |
| 1128 | break; |
| 1129 | default: |
| 1130 | ret = 1; |
| 1131 | goto cleanup; |
| 1132 | } |
| 1133 | break; |
| 1134 | |
| 1135 | case MBEDTLS_MODE_STREAM: |
| 1136 | case MBEDTLS_MODE_CBC: |
| 1137 | t_out->fixed_ivlen = 0; /* redundant, must be 0 */ |
| 1138 | t_in->fixed_ivlen = 0; /* redundant, must be 0 */ |
| 1139 | t_out->taglen = 0; |
| 1140 | t_in->taglen = 0; |
| 1141 | switch (tag_mode) { |
| 1142 | case 0: /* Full tag */ |
| 1143 | t_out->maclen = maclen; |
| 1144 | t_in->maclen = maclen; |
| 1145 | break; |
| 1146 | case 1: /* Partial tag */ |
| 1147 | t_out->maclen = 10; |
| 1148 | t_in->maclen = 10; |
| 1149 | break; |
| 1150 | default: |
| 1151 | ret = 1; |
| 1152 | goto cleanup; |
| 1153 | } |
| 1154 | break; |
| 1155 | default: |
| 1156 | ret = 1; |
| 1157 | goto cleanup; |
| 1158 | break; |
| 1159 | } |
| 1160 | |
| 1161 | /* Setup IV's */ |
| 1162 | |
| 1163 | memcpy(&t_in->iv_dec, iv_dec, sizeof(iv_dec)); |
| 1164 | memcpy(&t_in->iv_enc, iv_enc, sizeof(iv_enc)); |
| 1165 | memcpy(&t_out->iv_dec, iv_enc, sizeof(iv_enc)); |
| 1166 | memcpy(&t_out->iv_enc, iv_dec, sizeof(iv_dec)); |
| 1167 | |
| 1168 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 1169 | /* Add CID */ |
| 1170 | memcpy(&t_in->in_cid, cid0, cid0_len); |
| 1171 | memcpy(&t_in->out_cid, cid1, cid1_len); |
Yanray Wang | d2696f2 | 2022-11-03 11:51:59 +0800 | [diff] [blame] | 1172 | t_in->in_cid_len = (uint8_t) cid0_len; |
| 1173 | t_in->out_cid_len = (uint8_t) cid1_len; |
Yanray Wang | bd56b03 | 2023-03-14 14:36:48 +0800 | [diff] [blame] | 1174 | memcpy(&t_out->in_cid, cid1, cid1_len); |
| 1175 | memcpy(&t_out->out_cid, cid0, cid0_len); |
Yanray Wang | d2696f2 | 2022-11-03 11:51:59 +0800 | [diff] [blame] | 1176 | t_out->in_cid_len = (uint8_t) cid1_len; |
| 1177 | t_out->out_cid_len = (uint8_t) cid0_len; |
Yanray Wang | bd56b03 | 2023-03-14 14:36:48 +0800 | [diff] [blame] | 1178 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 1179 | |
| 1180 | cleanup: |
| 1181 | |
| 1182 | mbedtls_free(key0); |
| 1183 | mbedtls_free(key1); |
| 1184 | |
| 1185 | mbedtls_free(md0); |
| 1186 | mbedtls_free(md1); |
| 1187 | |
| 1188 | return ret; |
| 1189 | } |
| 1190 | |
Gilles Peskine | 2198cc5 | 2023-09-18 13:11:50 +0200 | [diff] [blame] | 1191 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
| 1192 | int mbedtls_test_ssl_prepare_record_mac(mbedtls_record *record, |
| 1193 | mbedtls_ssl_transform *transform_out) |
| 1194 | { |
| 1195 | /* Serialized version of record header for MAC purposes */ |
| 1196 | unsigned char add_data[13]; |
| 1197 | memcpy(add_data, record->ctr, 8); |
| 1198 | add_data[8] = record->type; |
| 1199 | add_data[9] = record->ver[0]; |
| 1200 | add_data[10] = record->ver[1]; |
| 1201 | add_data[11] = (record->data_len >> 8) & 0xff; |
| 1202 | add_data[12] = (record->data_len >> 0) & 0xff; |
| 1203 | |
| 1204 | /* MAC with additional data */ |
| 1205 | TEST_EQUAL(0, mbedtls_md_hmac_update(&transform_out->md_ctx_enc, add_data, 13)); |
| 1206 | TEST_EQUAL(0, mbedtls_md_hmac_update(&transform_out->md_ctx_enc, |
| 1207 | record->buf + record->data_offset, |
| 1208 | record->data_len)); |
| 1209 | /* Use a temporary buffer for the MAC, because with the truncated HMAC |
| 1210 | * extension, there might not be enough room in the record for the |
| 1211 | * full-length MAC. */ |
| 1212 | unsigned char mac[MBEDTLS_MD_MAX_SIZE]; |
| 1213 | TEST_EQUAL(0, mbedtls_md_hmac_finish(&transform_out->md_ctx_enc, mac)); |
| 1214 | memcpy(record->buf + record->data_offset + record->data_len, mac, transform_out->maclen); |
| 1215 | record->data_len += transform_out->maclen; |
| 1216 | |
| 1217 | return 0; |
| 1218 | |
| 1219 | exit: |
| 1220 | return -1; |
| 1221 | } |
| 1222 | #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ |
| 1223 | |
Yanray Wang | bd56b03 | 2023-03-14 14:36:48 +0800 | [diff] [blame] | 1224 | int mbedtls_test_ssl_populate_session(mbedtls_ssl_session *session, |
| 1225 | int ticket_len, |
| 1226 | const char *crt_file) |
| 1227 | { |
| 1228 | #if defined(MBEDTLS_HAVE_TIME) |
| 1229 | session->start = mbedtls_time(NULL) - 42; |
| 1230 | #endif |
| 1231 | session->ciphersuite = 0xabcd; |
| 1232 | session->compression = 1; |
| 1233 | session->id_len = sizeof(session->id); |
| 1234 | memset(session->id, 66, session->id_len); |
| 1235 | memset(session->master, 17, sizeof(session->master)); |
| 1236 | |
| 1237 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) && \ |
| 1238 | defined(MBEDTLS_CERTS_C) && \ |
| 1239 | defined(MBEDTLS_FS_IO) |
| 1240 | if (strlen(crt_file) != 0) { |
| 1241 | mbedtls_x509_crt tmp_crt; |
| 1242 | int ret; |
| 1243 | |
| 1244 | mbedtls_x509_crt_init(&tmp_crt); |
| 1245 | ret = mbedtls_x509_crt_parse_file(&tmp_crt, crt_file); |
| 1246 | if (ret != 0) { |
| 1247 | return ret; |
| 1248 | } |
| 1249 | |
| 1250 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 1251 | /* Move temporary CRT. */ |
| 1252 | session->peer_cert = mbedtls_calloc(1, sizeof(*session->peer_cert)); |
| 1253 | if (session->peer_cert == NULL) { |
| 1254 | return -1; |
| 1255 | } |
| 1256 | *session->peer_cert = tmp_crt; |
| 1257 | memset(&tmp_crt, 0, sizeof(tmp_crt)); |
| 1258 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 1259 | /* Calculate digest of temporary CRT. */ |
| 1260 | session->peer_cert_digest = |
| 1261 | mbedtls_calloc(1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN); |
| 1262 | if (session->peer_cert_digest == NULL) { |
| 1263 | return -1; |
| 1264 | } |
| 1265 | ret = mbedtls_md(mbedtls_md_info_from_type( |
| 1266 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE), |
| 1267 | tmp_crt.raw.p, tmp_crt.raw.len, |
| 1268 | session->peer_cert_digest); |
| 1269 | if (ret != 0) { |
| 1270 | return ret; |
| 1271 | } |
| 1272 | session->peer_cert_digest_type = |
| 1273 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE; |
| 1274 | session->peer_cert_digest_len = |
| 1275 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN; |
| 1276 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 1277 | |
| 1278 | mbedtls_x509_crt_free(&tmp_crt); |
| 1279 | } |
| 1280 | #else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED && MBEDTLS_CERTS_C && MBEDTLS_FS_IO */ |
| 1281 | (void) crt_file; |
| 1282 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED && MBEDTLS_CERTS_C && MBEDTLS_FS_IO */ |
| 1283 | session->verify_result = 0xdeadbeef; |
| 1284 | |
| 1285 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 1286 | if (ticket_len != 0) { |
| 1287 | session->ticket = mbedtls_calloc(1, ticket_len); |
| 1288 | if (session->ticket == NULL) { |
| 1289 | return -1; |
| 1290 | } |
| 1291 | memset(session->ticket, 33, ticket_len); |
| 1292 | } |
| 1293 | session->ticket_len = ticket_len; |
| 1294 | session->ticket_lifetime = 86401; |
| 1295 | #else |
| 1296 | (void) ticket_len; |
| 1297 | #endif |
| 1298 | |
| 1299 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 1300 | session->mfl_code = 1; |
| 1301 | #endif |
| 1302 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
| 1303 | session->trunc_hmac = 1; |
| 1304 | #endif |
| 1305 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 1306 | session->encrypt_then_mac = 1; |
| 1307 | #endif |
| 1308 | |
| 1309 | return 0; |
| 1310 | } |
| 1311 | |
Yanray Wang | 74df201 | 2023-03-16 12:15:49 +0800 | [diff] [blame] | 1312 | int mbedtls_test_ssl_exchange_data( |
| 1313 | mbedtls_ssl_context *ssl_1, |
| 1314 | int msg_len_1, const int expected_fragments_1, |
| 1315 | mbedtls_ssl_context *ssl_2, |
| 1316 | int msg_len_2, const int expected_fragments_2) |
Yanray Wang | bd56b03 | 2023-03-14 14:36:48 +0800 | [diff] [blame] | 1317 | { |
| 1318 | unsigned char *msg_buf_1 = malloc(msg_len_1); |
| 1319 | unsigned char *msg_buf_2 = malloc(msg_len_2); |
| 1320 | unsigned char *in_buf_1 = malloc(msg_len_2); |
| 1321 | unsigned char *in_buf_2 = malloc(msg_len_1); |
| 1322 | int msg_type, ret = -1; |
| 1323 | |
| 1324 | /* Perform this test with two message types. At first use a message |
| 1325 | * consisting of only 0x00 for the client and only 0xFF for the server. |
| 1326 | * At the second time use message with generated data */ |
| 1327 | for (msg_type = 0; msg_type < 2; msg_type++) { |
| 1328 | int written_1 = 0; |
| 1329 | int written_2 = 0; |
| 1330 | int read_1 = 0; |
| 1331 | int read_2 = 0; |
| 1332 | int fragments_1 = 0; |
| 1333 | int fragments_2 = 0; |
| 1334 | |
| 1335 | if (msg_type == 0) { |
| 1336 | memset(msg_buf_1, 0x00, msg_len_1); |
| 1337 | memset(msg_buf_2, 0xff, msg_len_2); |
| 1338 | } else { |
| 1339 | int i, j = 0; |
| 1340 | for (i = 0; i < msg_len_1; i++) { |
| 1341 | msg_buf_1[i] = j++ & 0xFF; |
| 1342 | } |
| 1343 | for (i = 0; i < msg_len_2; i++) { |
| 1344 | msg_buf_2[i] = (j -= 5) & 0xFF; |
| 1345 | } |
| 1346 | } |
| 1347 | |
| 1348 | while (read_1 < msg_len_2 || read_2 < msg_len_1) { |
| 1349 | /* ssl_1 sending */ |
| 1350 | if (msg_len_1 > written_1) { |
| 1351 | ret = mbedtls_ssl_write_fragment(ssl_1, msg_buf_1, |
| 1352 | msg_len_1, &written_1, |
| 1353 | expected_fragments_1); |
| 1354 | if (expected_fragments_1 == 0) { |
| 1355 | /* This error is expected when the message is too large and |
| 1356 | * cannot be fragmented */ |
| 1357 | TEST_ASSERT(ret == MBEDTLS_ERR_SSL_BAD_INPUT_DATA); |
| 1358 | msg_len_1 = 0; |
| 1359 | } else { |
| 1360 | TEST_ASSERT(ret == 0); |
| 1361 | } |
| 1362 | } |
| 1363 | |
| 1364 | /* ssl_2 sending */ |
| 1365 | if (msg_len_2 > written_2) { |
| 1366 | ret = mbedtls_ssl_write_fragment(ssl_2, msg_buf_2, |
| 1367 | msg_len_2, &written_2, |
| 1368 | expected_fragments_2); |
| 1369 | if (expected_fragments_2 == 0) { |
| 1370 | /* This error is expected when the message is too large and |
| 1371 | * cannot be fragmented */ |
| 1372 | TEST_ASSERT(ret == MBEDTLS_ERR_SSL_BAD_INPUT_DATA); |
| 1373 | msg_len_2 = 0; |
| 1374 | } else { |
| 1375 | TEST_ASSERT(ret == 0); |
| 1376 | } |
| 1377 | } |
| 1378 | |
| 1379 | /* ssl_1 reading */ |
| 1380 | if (read_1 < msg_len_2) { |
| 1381 | ret = mbedtls_ssl_read_fragment(ssl_1, in_buf_1, |
| 1382 | msg_len_2, &read_1, |
| 1383 | &fragments_2, |
| 1384 | expected_fragments_2); |
| 1385 | TEST_ASSERT(ret == 0); |
| 1386 | } |
| 1387 | |
| 1388 | /* ssl_2 reading */ |
| 1389 | if (read_2 < msg_len_1) { |
| 1390 | ret = mbedtls_ssl_read_fragment(ssl_2, in_buf_2, |
| 1391 | msg_len_1, &read_2, |
| 1392 | &fragments_1, |
| 1393 | expected_fragments_1); |
| 1394 | TEST_ASSERT(ret == 0); |
| 1395 | } |
| 1396 | } |
| 1397 | |
| 1398 | ret = -1; |
| 1399 | TEST_ASSERT(0 == memcmp(msg_buf_1, in_buf_2, msg_len_1)); |
| 1400 | TEST_ASSERT(0 == memcmp(msg_buf_2, in_buf_1, msg_len_2)); |
| 1401 | TEST_ASSERT(fragments_1 == expected_fragments_1); |
| 1402 | TEST_ASSERT(fragments_2 == expected_fragments_2); |
| 1403 | } |
| 1404 | |
| 1405 | ret = 0; |
| 1406 | |
| 1407 | exit: |
| 1408 | free(msg_buf_1); |
| 1409 | free(in_buf_1); |
| 1410 | free(msg_buf_2); |
| 1411 | free(in_buf_2); |
| 1412 | |
| 1413 | return ret; |
| 1414 | } |
| 1415 | |
| 1416 | /* |
| 1417 | * Perform data exchanging between \p ssl_1 and \p ssl_2. Both of endpoints |
| 1418 | * must be initialized and connected beforehand. |
| 1419 | * |
| 1420 | * \retval 0 on success, otherwise error code. |
| 1421 | */ |
Yanray Wang | b4ef9a2 | 2023-03-16 12:04:49 +0800 | [diff] [blame] | 1422 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) && \ |
| 1423 | defined(MBEDTLS_CERTS_C) && \ |
| 1424 | defined(MBEDTLS_ENTROPY_C) && \ |
| 1425 | defined(MBEDTLS_CTR_DRBG_C) && \ |
| 1426 | (defined(MBEDTLS_SSL_RENEGOTIATION) || \ |
| 1427 | defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)) |
| 1428 | static int exchange_data(mbedtls_ssl_context *ssl_1, |
| 1429 | mbedtls_ssl_context *ssl_2) |
Yanray Wang | bd56b03 | 2023-03-14 14:36:48 +0800 | [diff] [blame] | 1430 | { |
Yanray Wang | 74df201 | 2023-03-16 12:15:49 +0800 | [diff] [blame] | 1431 | return mbedtls_test_ssl_exchange_data(ssl_1, 256, 1, |
| 1432 | ssl_2, 256, 1); |
Yanray Wang | bd56b03 | 2023-03-14 14:36:48 +0800 | [diff] [blame] | 1433 | } |
Yanray Wang | b4ef9a2 | 2023-03-16 12:04:49 +0800 | [diff] [blame] | 1434 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED && MBEDTLS_CERTS_C && |
| 1435 | MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C && |
| 1436 | (MBEDTLS_SSL_RENEGOTIATION || |
| 1437 | MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) */ |
Yanray Wang | bd56b03 | 2023-03-14 14:36:48 +0800 | [diff] [blame] | 1438 | |
| 1439 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) && \ |
Yanray Wang | b4ef9a2 | 2023-03-16 12:04:49 +0800 | [diff] [blame] | 1440 | defined(MBEDTLS_CERTS_C) && \ |
| 1441 | defined(MBEDTLS_ENTROPY_C) && \ |
Yanray Wang | bd56b03 | 2023-03-14 14:36:48 +0800 | [diff] [blame] | 1442 | defined(MBEDTLS_CTR_DRBG_C) |
| 1443 | void mbedtls_test_ssl_perform_handshake( |
| 1444 | mbedtls_test_handshake_test_options *options) |
| 1445 | { |
| 1446 | /* forced_ciphersuite needs to last until the end of the handshake */ |
| 1447 | int forced_ciphersuite[2]; |
| 1448 | enum { BUFFSIZE = 17000 }; |
| 1449 | mbedtls_test_ssl_endpoint client, server; |
| 1450 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
| 1451 | const char *psk_identity = "foo"; |
| 1452 | #endif |
| 1453 | #if defined(MBEDTLS_TIMING_C) |
| 1454 | mbedtls_timing_delay_context timer_client, timer_server; |
| 1455 | #endif |
| 1456 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
| 1457 | unsigned char *context_buf = NULL; |
| 1458 | size_t context_buf_len; |
| 1459 | #endif |
| 1460 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 1461 | int ret = -1; |
| 1462 | #endif |
| 1463 | int expected_handshake_result = 0; |
| 1464 | |
| 1465 | USE_PSA_INIT(); |
| 1466 | mbedtls_platform_zeroize(&client, sizeof(client)); |
| 1467 | mbedtls_platform_zeroize(&server, sizeof(server)); |
| 1468 | |
| 1469 | mbedtls_test_ssl_message_queue server_queue, client_queue; |
| 1470 | mbedtls_test_message_socket_context server_context, client_context; |
| 1471 | mbedtls_test_message_socket_init(&server_context); |
| 1472 | mbedtls_test_message_socket_init(&client_context); |
| 1473 | |
| 1474 | /* Client side */ |
| 1475 | if (options->dtls != 0) { |
| 1476 | TEST_ASSERT(mbedtls_test_ssl_endpoint_init(&client, |
| 1477 | MBEDTLS_SSL_IS_CLIENT, |
| 1478 | options->pk_alg, |
| 1479 | &client_context, |
| 1480 | &client_queue, |
| 1481 | &server_queue, NULL) == 0); |
| 1482 | #if defined(MBEDTLS_TIMING_C) |
| 1483 | mbedtls_ssl_set_timer_cb(&client.ssl, &timer_client, |
| 1484 | mbedtls_timing_set_delay, |
| 1485 | mbedtls_timing_get_delay); |
| 1486 | #endif |
| 1487 | } else { |
| 1488 | TEST_ASSERT(mbedtls_test_ssl_endpoint_init(&client, |
| 1489 | MBEDTLS_SSL_IS_CLIENT, |
| 1490 | options->pk_alg, NULL, NULL, |
| 1491 | NULL, NULL) == 0); |
| 1492 | } |
| 1493 | |
| 1494 | if (options->client_min_version != TEST_SSL_MINOR_VERSION_NONE) { |
| 1495 | mbedtls_ssl_conf_min_version(&client.conf, MBEDTLS_SSL_MAJOR_VERSION_3, |
| 1496 | options->client_min_version); |
| 1497 | } |
| 1498 | |
| 1499 | if (options->client_max_version != TEST_SSL_MINOR_VERSION_NONE) { |
| 1500 | mbedtls_ssl_conf_max_version(&client.conf, MBEDTLS_SSL_MAJOR_VERSION_3, |
| 1501 | options->client_max_version); |
| 1502 | } |
| 1503 | |
| 1504 | if (strlen(options->cipher) > 0) { |
| 1505 | set_ciphersuite(&client.conf, options->cipher, forced_ciphersuite); |
| 1506 | } |
| 1507 | |
| 1508 | #if defined(MBEDTLS_DEBUG_C) |
| 1509 | if (options->cli_log_fun) { |
| 1510 | mbedtls_debug_set_threshold(4); |
| 1511 | mbedtls_ssl_conf_dbg(&client.conf, options->cli_log_fun, |
| 1512 | options->cli_log_obj); |
| 1513 | } |
| 1514 | #endif |
| 1515 | |
| 1516 | /* Server side */ |
| 1517 | if (options->dtls != 0) { |
| 1518 | TEST_ASSERT(mbedtls_test_ssl_endpoint_init(&server, |
| 1519 | MBEDTLS_SSL_IS_SERVER, |
| 1520 | options->pk_alg, |
| 1521 | &server_context, |
| 1522 | &server_queue, |
| 1523 | &client_queue, NULL) == 0); |
| 1524 | #if defined(MBEDTLS_TIMING_C) |
| 1525 | mbedtls_ssl_set_timer_cb(&server.ssl, &timer_server, |
| 1526 | mbedtls_timing_set_delay, |
| 1527 | mbedtls_timing_get_delay); |
| 1528 | #endif |
| 1529 | } else { |
| 1530 | TEST_ASSERT(mbedtls_test_ssl_endpoint_init(&server, |
| 1531 | MBEDTLS_SSL_IS_SERVER, |
| 1532 | options->pk_alg, NULL, NULL, |
| 1533 | NULL, NULL) == 0); |
| 1534 | } |
| 1535 | |
| 1536 | mbedtls_ssl_conf_authmode(&server.conf, options->srv_auth_mode); |
| 1537 | |
| 1538 | if (options->server_min_version != TEST_SSL_MINOR_VERSION_NONE) { |
| 1539 | mbedtls_ssl_conf_min_version(&server.conf, MBEDTLS_SSL_MAJOR_VERSION_3, |
| 1540 | options->server_min_version); |
| 1541 | } |
| 1542 | |
| 1543 | if (options->server_max_version != TEST_SSL_MINOR_VERSION_NONE) { |
| 1544 | mbedtls_ssl_conf_max_version(&server.conf, MBEDTLS_SSL_MAJOR_VERSION_3, |
| 1545 | options->server_max_version); |
| 1546 | } |
| 1547 | |
| 1548 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 1549 | TEST_ASSERT(mbedtls_ssl_conf_max_frag_len(&(server.conf), |
| 1550 | (unsigned char) options->mfl) |
| 1551 | == 0); |
| 1552 | TEST_ASSERT(mbedtls_ssl_conf_max_frag_len(&(client.conf), |
| 1553 | (unsigned char) options->mfl) |
| 1554 | == 0); |
| 1555 | #else |
| 1556 | TEST_ASSERT(MBEDTLS_SSL_MAX_FRAG_LEN_NONE == options->mfl); |
| 1557 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 1558 | |
| 1559 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
| 1560 | if (options->psk_str != NULL && options->psk_str->len > 0) { |
| 1561 | TEST_ASSERT(mbedtls_ssl_conf_psk( |
| 1562 | &client.conf, options->psk_str->x, |
| 1563 | options->psk_str->len, |
| 1564 | (const unsigned char *) psk_identity, |
| 1565 | strlen(psk_identity)) == 0); |
| 1566 | |
| 1567 | TEST_ASSERT(mbedtls_ssl_conf_psk( |
| 1568 | &server.conf, options->psk_str->x, |
| 1569 | options->psk_str->len, |
| 1570 | (const unsigned char *) psk_identity, |
| 1571 | strlen(psk_identity)) == 0); |
| 1572 | |
| 1573 | mbedtls_ssl_conf_psk_cb(&server.conf, psk_dummy_callback, NULL); |
| 1574 | } |
| 1575 | #endif |
| 1576 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 1577 | if (options->renegotiate) { |
| 1578 | mbedtls_ssl_conf_renegotiation(&(server.conf), |
| 1579 | MBEDTLS_SSL_RENEGOTIATION_ENABLED); |
| 1580 | mbedtls_ssl_conf_renegotiation(&(client.conf), |
| 1581 | MBEDTLS_SSL_RENEGOTIATION_ENABLED); |
| 1582 | |
| 1583 | mbedtls_ssl_conf_legacy_renegotiation(&(server.conf), |
| 1584 | options->legacy_renegotiation); |
| 1585 | mbedtls_ssl_conf_legacy_renegotiation(&(client.conf), |
| 1586 | options->legacy_renegotiation); |
| 1587 | } |
| 1588 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
| 1589 | |
| 1590 | #if defined(MBEDTLS_DEBUG_C) |
| 1591 | if (options->srv_log_fun) { |
| 1592 | mbedtls_debug_set_threshold(4); |
| 1593 | mbedtls_ssl_conf_dbg(&server.conf, options->srv_log_fun, |
| 1594 | options->srv_log_obj); |
| 1595 | } |
| 1596 | #endif |
| 1597 | |
| 1598 | TEST_ASSERT(mbedtls_test_mock_socket_connect(&(client.socket), |
| 1599 | &(server.socket), |
| 1600 | BUFFSIZE) == 0); |
| 1601 | |
| 1602 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1603 | if (options->resize_buffers != 0) { |
| 1604 | /* Ensure that the buffer sizes are appropriate before resizes */ |
| 1605 | TEST_ASSERT(client.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN); |
| 1606 | TEST_ASSERT(client.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN); |
| 1607 | TEST_ASSERT(server.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN); |
| 1608 | TEST_ASSERT(server.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN); |
| 1609 | } |
| 1610 | #endif |
| 1611 | |
| 1612 | if (options->expected_negotiated_version == TEST_SSL_MINOR_VERSION_NONE) { |
| 1613 | expected_handshake_result = MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION; |
| 1614 | } |
| 1615 | |
| 1616 | TEST_ASSERT(mbedtls_test_move_handshake_to_state( |
| 1617 | &(client.ssl), &(server.ssl), MBEDTLS_SSL_HANDSHAKE_OVER) |
| 1618 | == expected_handshake_result); |
| 1619 | |
| 1620 | if (expected_handshake_result != 0) { |
| 1621 | /* Connection will have failed by this point, skip to cleanup */ |
| 1622 | goto exit; |
| 1623 | } |
| 1624 | |
| 1625 | TEST_ASSERT(client.ssl.state == MBEDTLS_SSL_HANDSHAKE_OVER); |
| 1626 | TEST_ASSERT(server.ssl.state == MBEDTLS_SSL_HANDSHAKE_OVER); |
| 1627 | |
| 1628 | /* Check that we agree on the version... */ |
| 1629 | TEST_ASSERT(client.ssl.minor_ver == server.ssl.minor_ver); |
| 1630 | |
| 1631 | /* And check that the version negotiated is the expected one. */ |
| 1632 | TEST_EQUAL(client.ssl.minor_ver, options->expected_negotiated_version); |
| 1633 | |
| 1634 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1635 | if (options->resize_buffers != 0) { |
| 1636 | if (options->expected_negotiated_version != MBEDTLS_SSL_MINOR_VERSION_0 && |
| 1637 | options->expected_negotiated_version != MBEDTLS_SSL_MINOR_VERSION_1) { |
| 1638 | /* A server, when using DTLS, might delay a buffer resize to happen |
| 1639 | * after it receives a message, so we force it. */ |
| 1640 | TEST_ASSERT(exchange_data(&(client.ssl), &(server.ssl)) == 0); |
| 1641 | |
| 1642 | TEST_ASSERT(client.ssl.out_buf_len == |
| 1643 | mbedtls_ssl_get_output_buflen(&client.ssl)); |
| 1644 | TEST_ASSERT(client.ssl.in_buf_len == |
| 1645 | mbedtls_ssl_get_input_buflen(&client.ssl)); |
| 1646 | TEST_ASSERT(server.ssl.out_buf_len == |
| 1647 | mbedtls_ssl_get_output_buflen(&server.ssl)); |
| 1648 | TEST_ASSERT(server.ssl.in_buf_len == |
| 1649 | mbedtls_ssl_get_input_buflen(&server.ssl)); |
| 1650 | } |
| 1651 | } |
| 1652 | #endif |
| 1653 | |
| 1654 | if (options->cli_msg_len != 0 || options->srv_msg_len != 0) { |
| 1655 | /* Start data exchanging test */ |
Yanray Wang | 74df201 | 2023-03-16 12:15:49 +0800 | [diff] [blame] | 1656 | TEST_ASSERT(mbedtls_test_ssl_exchange_data( |
| 1657 | &(client.ssl), options->cli_msg_len, |
| 1658 | options->expected_cli_fragments, |
| 1659 | &(server.ssl), options->srv_msg_len, |
| 1660 | options->expected_srv_fragments) |
Yanray Wang | bd56b03 | 2023-03-14 14:36:48 +0800 | [diff] [blame] | 1661 | == 0); |
| 1662 | } |
| 1663 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
| 1664 | if (options->serialize == 1) { |
| 1665 | TEST_ASSERT(options->dtls == 1); |
| 1666 | |
| 1667 | TEST_ASSERT(mbedtls_ssl_context_save(&(server.ssl), NULL, |
| 1668 | 0, &context_buf_len) |
| 1669 | == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL); |
| 1670 | |
| 1671 | context_buf = mbedtls_calloc(1, context_buf_len); |
| 1672 | TEST_ASSERT(context_buf != NULL); |
| 1673 | |
| 1674 | TEST_ASSERT(mbedtls_ssl_context_save(&(server.ssl), context_buf, |
| 1675 | context_buf_len, |
| 1676 | &context_buf_len) |
| 1677 | == 0); |
| 1678 | |
| 1679 | mbedtls_ssl_free(&(server.ssl)); |
| 1680 | mbedtls_ssl_init(&(server.ssl)); |
| 1681 | |
| 1682 | TEST_ASSERT(mbedtls_ssl_setup(&(server.ssl), &(server.conf)) == 0); |
| 1683 | |
| 1684 | mbedtls_ssl_set_bio(&(server.ssl), &server_context, |
| 1685 | mbedtls_test_mock_tcp_send_msg, |
| 1686 | mbedtls_test_mock_tcp_recv_msg, |
| 1687 | NULL); |
| 1688 | |
| 1689 | #if defined(MBEDTLS_TIMING_C) |
| 1690 | mbedtls_ssl_set_timer_cb(&server.ssl, &timer_server, |
| 1691 | mbedtls_timing_set_delay, |
| 1692 | mbedtls_timing_get_delay); |
| 1693 | #endif |
| 1694 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1695 | if (options->resize_buffers != 0) { |
| 1696 | /* Ensure that the buffer sizes are appropriate before resizes */ |
| 1697 | TEST_ASSERT(server.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN); |
| 1698 | TEST_ASSERT(server.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN); |
| 1699 | } |
| 1700 | #endif |
| 1701 | TEST_ASSERT(mbedtls_ssl_context_load(&(server.ssl), context_buf, |
| 1702 | context_buf_len) == 0); |
| 1703 | |
| 1704 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1705 | /* Validate buffer sizes after context deserialization */ |
| 1706 | if (options->resize_buffers != 0) { |
| 1707 | TEST_ASSERT(server.ssl.out_buf_len == |
| 1708 | mbedtls_ssl_get_output_buflen(&server.ssl)); |
| 1709 | TEST_ASSERT(server.ssl.in_buf_len == |
| 1710 | mbedtls_ssl_get_input_buflen(&server.ssl)); |
| 1711 | } |
| 1712 | #endif |
| 1713 | /* Retest writing/reading */ |
| 1714 | if (options->cli_msg_len != 0 || options->srv_msg_len != 0) { |
Yanray Wang | 74df201 | 2023-03-16 12:15:49 +0800 | [diff] [blame] | 1715 | TEST_ASSERT(mbedtls_test_ssl_exchange_data( |
| 1716 | &(client.ssl), options->cli_msg_len, |
Yanray Wang | bd56b03 | 2023-03-14 14:36:48 +0800 | [diff] [blame] | 1717 | options->expected_cli_fragments, |
Yanray Wang | 74df201 | 2023-03-16 12:15:49 +0800 | [diff] [blame] | 1718 | &(server.ssl), options->srv_msg_len, |
Yanray Wang | bd56b03 | 2023-03-14 14:36:48 +0800 | [diff] [blame] | 1719 | options->expected_srv_fragments) |
| 1720 | == 0); |
| 1721 | } |
| 1722 | } |
| 1723 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ |
| 1724 | |
| 1725 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 1726 | if (options->renegotiate) { |
| 1727 | /* Start test with renegotiation */ |
| 1728 | TEST_ASSERT(server.ssl.renego_status == |
| 1729 | MBEDTLS_SSL_INITIAL_HANDSHAKE); |
| 1730 | TEST_ASSERT(client.ssl.renego_status == |
| 1731 | MBEDTLS_SSL_INITIAL_HANDSHAKE); |
| 1732 | |
| 1733 | /* After calling this function for the server, it only sends a handshake |
| 1734 | * request. All renegotiation should happen during data exchanging */ |
| 1735 | TEST_ASSERT(mbedtls_ssl_renegotiate(&(server.ssl)) == 0); |
| 1736 | TEST_ASSERT(server.ssl.renego_status == |
| 1737 | MBEDTLS_SSL_RENEGOTIATION_PENDING); |
| 1738 | TEST_ASSERT(client.ssl.renego_status == |
| 1739 | MBEDTLS_SSL_INITIAL_HANDSHAKE); |
| 1740 | |
| 1741 | TEST_ASSERT(exchange_data(&(client.ssl), &(server.ssl)) == 0); |
| 1742 | TEST_ASSERT(server.ssl.renego_status == |
| 1743 | MBEDTLS_SSL_RENEGOTIATION_DONE); |
| 1744 | TEST_ASSERT(client.ssl.renego_status == |
| 1745 | MBEDTLS_SSL_RENEGOTIATION_DONE); |
| 1746 | |
| 1747 | /* After calling mbedtls_ssl_renegotiate for the client, |
| 1748 | * all renegotiation should happen inside this function. |
| 1749 | * However in this test, we cannot perform simultaneous communication |
| 1750 | * between client and server so this function will return waiting error |
| 1751 | * on the socket. All rest of renegotiation should happen |
| 1752 | * during data exchanging */ |
| 1753 | ret = mbedtls_ssl_renegotiate(&(client.ssl)); |
| 1754 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1755 | if (options->resize_buffers != 0) { |
| 1756 | /* Ensure that the buffer sizes are appropriate before resizes */ |
| 1757 | TEST_ASSERT(client.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN); |
| 1758 | TEST_ASSERT(client.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN); |
| 1759 | } |
| 1760 | #endif |
| 1761 | TEST_ASSERT(ret == 0 || |
| 1762 | ret == MBEDTLS_ERR_SSL_WANT_READ || |
| 1763 | ret == MBEDTLS_ERR_SSL_WANT_WRITE); |
| 1764 | TEST_ASSERT(server.ssl.renego_status == |
| 1765 | MBEDTLS_SSL_RENEGOTIATION_DONE); |
| 1766 | TEST_ASSERT(client.ssl.renego_status == |
| 1767 | MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS); |
| 1768 | |
| 1769 | TEST_ASSERT(exchange_data(&(client.ssl), &(server.ssl)) == 0); |
| 1770 | TEST_ASSERT(server.ssl.renego_status == |
| 1771 | MBEDTLS_SSL_RENEGOTIATION_DONE); |
| 1772 | TEST_ASSERT(client.ssl.renego_status == |
| 1773 | MBEDTLS_SSL_RENEGOTIATION_DONE); |
| 1774 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1775 | /* Validate buffer sizes after renegotiation */ |
| 1776 | if (options->resize_buffers != 0) { |
| 1777 | TEST_ASSERT(client.ssl.out_buf_len == |
| 1778 | mbedtls_ssl_get_output_buflen(&client.ssl)); |
| 1779 | TEST_ASSERT(client.ssl.in_buf_len == |
| 1780 | mbedtls_ssl_get_input_buflen(&client.ssl)); |
| 1781 | TEST_ASSERT(server.ssl.out_buf_len == |
| 1782 | mbedtls_ssl_get_output_buflen(&server.ssl)); |
| 1783 | TEST_ASSERT(server.ssl.in_buf_len == |
| 1784 | mbedtls_ssl_get_input_buflen(&server.ssl)); |
| 1785 | } |
| 1786 | #endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */ |
| 1787 | } |
| 1788 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
| 1789 | |
| 1790 | exit: |
| 1791 | mbedtls_test_ssl_endpoint_free(&client, |
| 1792 | options->dtls != 0 ? &client_context : NULL); |
| 1793 | mbedtls_test_ssl_endpoint_free(&server, |
| 1794 | options->dtls != 0 ? &server_context : NULL); |
| 1795 | #if defined(MBEDTLS_DEBUG_C) |
| 1796 | if (options->cli_log_fun || options->srv_log_fun) { |
| 1797 | mbedtls_debug_set_threshold(0); |
| 1798 | } |
| 1799 | #endif |
| 1800 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
| 1801 | if (context_buf != NULL) { |
| 1802 | mbedtls_free(context_buf); |
| 1803 | } |
| 1804 | #endif |
| 1805 | } |
Yanray Wang | 1ef77c0 | 2023-03-14 16:59:00 +0800 | [diff] [blame] | 1806 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED && MBEDTLS_CERTS_C && |
| 1807 | MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */ |
Yanray Wang | bd56b03 | 2023-03-14 14:36:48 +0800 | [diff] [blame] | 1808 | |
Yanray Wang | 4323e45 | 2023-03-14 16:52:06 +0800 | [diff] [blame] | 1809 | #endif /* MBEDTLS_SSL_TLS_C */ |