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