Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 1 | /* BEGIN_HEADER */ |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 2 | #include <mbedtls/ssl.h> |
Chris Jones | 84a773f | 2021-03-05 18:38:47 +0000 | [diff] [blame] | 3 | #include <ssl_misc.h> |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4 | #include <mbedtls/ctr_drbg.h> |
| 5 | #include <mbedtls/entropy.h> |
Andrzej Kurek | 941962e | 2020-02-07 09:20:32 -0500 | [diff] [blame] | 6 | #include <mbedtls/timing.h> |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 7 | #include <mbedtls/debug.h> |
Hanno Becker | 73c825a | 2020-09-08 10:52:58 +0100 | [diff] [blame] | 8 | #include <ssl_tls13_keys.h> |
Gabor Mezei | b35759d | 2022-02-09 16:59:11 +0100 | [diff] [blame] | 9 | #include <ssl_tls13_invasive.h> |
Mateusz Starzyk | 1aec646 | 2021-02-08 15:34:42 +0100 | [diff] [blame] | 10 | #include "test/certs.h" |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 11 | |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 12 | #if defined(MBEDTLS_SSL_CACHE_C) |
| 13 | #include "mbedtls/ssl_cache.h" |
| 14 | #endif |
| 15 | |
Hanno Becker | 6667ffd | 2021-04-19 21:59:22 +0100 | [diff] [blame] | 16 | #include <psa/crypto.h> |
| 17 | |
Gabor Mezei | 22c9a6f | 2021-10-20 12:09:35 +0200 | [diff] [blame] | 18 | #include <constant_time_internal.h> |
Manuel Pégourié-Gonnard | 045f094 | 2020-07-02 11:34:02 +0200 | [diff] [blame] | 19 | |
Manuel Pégourié-Gonnard | 9670a59 | 2020-07-10 10:21:46 +0200 | [diff] [blame] | 20 | #include <test/constant_flow.h> |
| 21 | |
Hanno Becker | 1413bd8 | 2020-09-09 12:46:09 +0100 | [diff] [blame] | 22 | enum |
| 23 | { |
| 24 | #define MBEDTLS_SSL_TLS1_3_LABEL( name, string ) \ |
Xiaofei Bai | d25fab6 | 2021-12-02 06:36:27 +0000 | [diff] [blame] | 25 | tls13_label_ ## name, |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 26 | MBEDTLS_SSL_TLS1_3_LABEL_LIST |
| 27 | #undef MBEDTLS_SSL_TLS1_3_LABEL |
Hanno Becker | 1413bd8 | 2020-09-09 12:46:09 +0100 | [diff] [blame] | 28 | }; |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 29 | |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 30 | typedef struct log_pattern |
| 31 | { |
| 32 | const char *pattern; |
| 33 | size_t counter; |
| 34 | } log_pattern; |
| 35 | |
Piotr Nowicki | 438bf3b | 2020-03-10 12:59:10 +0100 | [diff] [blame] | 36 | /* |
| 37 | * This function can be passed to mbedtls to receive output logs from it. In |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 38 | * this case, it will count the instances of a log_pattern in the received |
| 39 | * logged messages. |
| 40 | */ |
| 41 | void log_analyzer( void *ctx, int level, |
| 42 | const char *file, int line, |
| 43 | const char *str ) |
| 44 | { |
| 45 | log_pattern *p = (log_pattern *) ctx; |
| 46 | |
| 47 | (void) level; |
| 48 | (void) line; |
| 49 | (void) file; |
| 50 | |
| 51 | if( NULL != p && |
| 52 | NULL != p->pattern && |
| 53 | NULL != strstr( str, p->pattern ) ) |
| 54 | { |
| 55 | p->counter++; |
| 56 | } |
| 57 | } |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 58 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 59 | typedef struct handshake_test_options |
| 60 | { |
| 61 | const char *cipher; |
Glenn Strauss | 39e624c | 2022-04-11 13:33:16 -0400 | [diff] [blame] | 62 | mbedtls_ssl_protocol_version client_min_version; |
| 63 | mbedtls_ssl_protocol_version client_max_version; |
| 64 | mbedtls_ssl_protocol_version server_min_version; |
| 65 | mbedtls_ssl_protocol_version server_max_version; |
| 66 | mbedtls_ssl_protocol_version expected_negotiated_version; |
Neil Armstrong | 8c52ed8 | 2022-05-27 13:14:55 +0200 | [diff] [blame] | 67 | int expected_handshake_result; |
| 68 | int expected_ciphersuite; |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 69 | int pk_alg; |
Neil Armstrong | 8c52ed8 | 2022-05-27 13:14:55 +0200 | [diff] [blame] | 70 | int opaque_alg; |
| 71 | int opaque_alg2; |
| 72 | int opaque_usage; |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 73 | data_t *psk_str; |
| 74 | int dtls; |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 75 | int srv_auth_mode; |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 76 | int serialize; |
| 77 | int mfl; |
| 78 | int cli_msg_len; |
| 79 | int srv_msg_len; |
| 80 | int expected_cli_fragments; |
| 81 | int expected_srv_fragments; |
| 82 | int renegotiate; |
| 83 | int legacy_renegotiation; |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 84 | void *srv_log_obj; |
| 85 | void *cli_log_obj; |
| 86 | void (*srv_log_fun)(void *, int, const char *, int, const char *); |
| 87 | void (*cli_log_fun)(void *, int, const char *, int, const char *); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 88 | int resize_buffers; |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 89 | #if defined(MBEDTLS_SSL_CACHE_C) |
Andrzej Kurek | 92d7417 | 2022-06-28 10:29:42 -0400 | [diff] [blame] | 90 | mbedtls_ssl_cache_context *cache; |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 91 | #endif |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 92 | } handshake_test_options; |
| 93 | |
| 94 | void init_handshake_options( handshake_test_options *opts ) |
| 95 | { |
Andrzej Kurek | 3d0d501 | 2022-07-04 05:20:55 -0400 | [diff] [blame] | 96 | opts->cipher = ""; |
| 97 | opts->client_min_version = MBEDTLS_SSL_VERSION_UNKNOWN; |
| 98 | opts->client_max_version = MBEDTLS_SSL_VERSION_UNKNOWN; |
| 99 | opts->server_min_version = MBEDTLS_SSL_VERSION_UNKNOWN; |
| 100 | opts->server_max_version = MBEDTLS_SSL_VERSION_UNKNOWN; |
| 101 | opts->expected_negotiated_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 102 | opts->expected_handshake_result = 0; |
| 103 | opts->expected_ciphersuite = 0; |
| 104 | opts->pk_alg = MBEDTLS_PK_RSA; |
| 105 | opts->opaque_alg = 0; |
| 106 | opts->opaque_alg2 = 0; |
| 107 | opts->opaque_usage = 0; |
| 108 | opts->psk_str = NULL; |
| 109 | opts->dtls = 0; |
| 110 | opts->srv_auth_mode = MBEDTLS_SSL_VERIFY_NONE; |
| 111 | opts->serialize = 0; |
| 112 | opts->mfl = MBEDTLS_SSL_MAX_FRAG_LEN_NONE; |
| 113 | opts->cli_msg_len = 100; |
| 114 | opts->srv_msg_len = 100; |
| 115 | opts->expected_cli_fragments = 1; |
| 116 | opts->expected_srv_fragments = 1; |
| 117 | opts->renegotiate = 0; |
| 118 | opts->legacy_renegotiation = MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION; |
| 119 | opts->srv_log_obj = NULL; |
| 120 | opts->srv_log_obj = NULL; |
| 121 | opts->srv_log_fun = NULL; |
| 122 | opts->cli_log_fun = NULL; |
| 123 | opts->resize_buffers = 1; |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 124 | #if defined(MBEDTLS_SSL_CACHE_C) |
Andrzej Kurek | 9dc4402 | 2022-07-04 05:46:15 -0400 | [diff] [blame] | 125 | opts->cache = NULL; |
Andrzej Kurek | 3d0d501 | 2022-07-04 05:20:55 -0400 | [diff] [blame] | 126 | ASSERT_ALLOC( opts->cache, sizeof( mbedtls_ssl_cache_context ) ); |
| 127 | mbedtls_ssl_cache_init( opts->cache ); |
| 128 | exit: |
| 129 | return; |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 130 | #endif |
| 131 | } |
| 132 | |
| 133 | void free_handshake_options( handshake_test_options *opts ) |
| 134 | { |
| 135 | #if defined(MBEDTLS_SSL_CACHE_C) |
| 136 | mbedtls_ssl_cache_free( opts->cache ); |
Andrzej Kurek | 3d0d501 | 2022-07-04 05:20:55 -0400 | [diff] [blame] | 137 | mbedtls_free( opts->cache ); |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 138 | #else |
| 139 | (void) opts; |
| 140 | #endif |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 141 | } |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 142 | |
Ronald Cron | e7b9b6b | 2022-06-10 17:24:31 +0200 | [diff] [blame] | 143 | #if defined(MBEDTLS_TEST_HOOKS) |
| 144 | static void set_chk_buf_ptr_args( |
| 145 | mbedtls_ssl_chk_buf_ptr_args *args, |
| 146 | unsigned char *cur, unsigned char *end, size_t need ) |
| 147 | { |
| 148 | args->cur = cur; |
| 149 | args->end = end; |
| 150 | args->need = need; |
| 151 | } |
| 152 | |
| 153 | static void reset_chk_buf_ptr_args( mbedtls_ssl_chk_buf_ptr_args *args ) |
| 154 | { |
| 155 | memset( args, 0, sizeof( *args ) ); |
| 156 | } |
| 157 | #endif /* MBEDTLS_TEST_HOOKS */ |
| 158 | |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 159 | /* |
| 160 | * Buffer structure for custom I/O callbacks. |
| 161 | */ |
| 162 | |
| 163 | typedef struct mbedtls_test_buffer |
| 164 | { |
| 165 | size_t start; |
| 166 | size_t content_length; |
| 167 | size_t capacity; |
| 168 | unsigned char *buffer; |
| 169 | } mbedtls_test_buffer; |
| 170 | |
| 171 | /* |
| 172 | * Initialises \p buf. After calling this function it is safe to call |
| 173 | * `mbedtls_test_buffer_free()` on \p buf. |
| 174 | */ |
| 175 | void mbedtls_test_buffer_init( mbedtls_test_buffer *buf ) |
| 176 | { |
| 177 | memset( buf, 0, sizeof( *buf ) ); |
| 178 | } |
| 179 | |
| 180 | /* |
| 181 | * Sets up \p buf. After calling this function it is safe to call |
| 182 | * `mbedtls_test_buffer_put()` and `mbedtls_test_buffer_get()` on \p buf. |
| 183 | */ |
| 184 | int mbedtls_test_buffer_setup( mbedtls_test_buffer *buf, size_t capacity ) |
| 185 | { |
| 186 | buf->buffer = (unsigned char*) mbedtls_calloc( capacity, |
| 187 | sizeof(unsigned char) ); |
| 188 | if( NULL == buf->buffer ) |
| 189 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 190 | buf->capacity = capacity; |
| 191 | |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | void mbedtls_test_buffer_free( mbedtls_test_buffer *buf ) |
| 196 | { |
| 197 | if( buf->buffer != NULL ) |
| 198 | mbedtls_free( buf->buffer ); |
| 199 | |
| 200 | memset( buf, 0, sizeof( *buf ) ); |
| 201 | } |
| 202 | |
| 203 | /* |
| 204 | * Puts \p input_len bytes from the \p input buffer into the ring buffer \p buf. |
| 205 | * |
| 206 | * \p buf must have been initialized and set up by calling |
| 207 | * `mbedtls_test_buffer_init()` and `mbedtls_test_buffer_setup()`. |
| 208 | * |
| 209 | * \retval \p input_len, if the data fits. |
| 210 | * \retval 0 <= value < \p input_len, if the data does not fit. |
| 211 | * \retval -1, if \p buf is NULL, it hasn't been set up or \p input_len is not |
| 212 | * zero and \p input is NULL. |
| 213 | */ |
| 214 | int mbedtls_test_buffer_put( mbedtls_test_buffer *buf, |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 215 | const unsigned char *input, size_t input_len ) |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 216 | { |
| 217 | size_t overflow = 0; |
| 218 | |
| 219 | if( ( buf == NULL ) || ( buf->buffer == NULL ) ) |
| 220 | return -1; |
| 221 | |
| 222 | /* Reduce input_len to a number that fits in the buffer. */ |
| 223 | if ( ( buf->content_length + input_len ) > buf->capacity ) |
| 224 | { |
| 225 | input_len = buf->capacity - buf->content_length; |
| 226 | } |
| 227 | |
| 228 | if( input == NULL ) |
| 229 | { |
| 230 | return ( input_len == 0 ) ? 0 : -1; |
| 231 | } |
| 232 | |
Piotr Nowicki | fb437d7 | 2020-01-13 16:59:12 +0100 | [diff] [blame] | 233 | /* Check if the buffer has not come full circle and free space is not in |
| 234 | * the middle */ |
| 235 | if( buf->start + buf->content_length < buf->capacity ) |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 236 | { |
Piotr Nowicki | fb437d7 | 2020-01-13 16:59:12 +0100 | [diff] [blame] | 237 | |
| 238 | /* Calculate the number of bytes that need to be placed at lower memory |
| 239 | * address */ |
| 240 | if( buf->start + buf->content_length + input_len |
| 241 | > buf->capacity ) |
| 242 | { |
| 243 | overflow = ( buf->start + buf->content_length + input_len ) |
| 244 | % buf->capacity; |
| 245 | } |
| 246 | |
| 247 | memcpy( buf->buffer + buf->start + buf->content_length, input, |
| 248 | input_len - overflow ); |
| 249 | memcpy( buf->buffer, input + input_len - overflow, overflow ); |
| 250 | |
| 251 | } |
| 252 | else |
| 253 | { |
| 254 | /* The buffer has come full circle and free space is in the middle */ |
| 255 | memcpy( buf->buffer + buf->start + buf->content_length - buf->capacity, |
| 256 | input, input_len ); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 259 | buf->content_length += input_len; |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 260 | return input_len; |
| 261 | } |
| 262 | |
| 263 | /* |
Andrzej Kurek | f777414 | 2020-01-22 06:34:59 -0500 | [diff] [blame] | 264 | * Gets \p output_len bytes from the ring buffer \p buf into the |
| 265 | * \p output buffer. The output buffer can be NULL, in this case a part of the |
| 266 | * ring buffer will be dropped, if the requested length is available. |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 267 | * |
| 268 | * \p buf must have been initialized and set up by calling |
| 269 | * `mbedtls_test_buffer_init()` and `mbedtls_test_buffer_setup()`. |
| 270 | * |
| 271 | * \retval \p output_len, if the data is available. |
| 272 | * \retval 0 <= value < \p output_len, if the data is not available. |
Andrzej Kurek | f777414 | 2020-01-22 06:34:59 -0500 | [diff] [blame] | 273 | * \retval -1, if \buf is NULL or it hasn't been set up. |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 274 | */ |
| 275 | int mbedtls_test_buffer_get( mbedtls_test_buffer *buf, |
| 276 | unsigned char* output, size_t output_len ) |
| 277 | { |
| 278 | size_t overflow = 0; |
| 279 | |
| 280 | if( ( buf == NULL ) || ( buf->buffer == NULL ) ) |
| 281 | return -1; |
| 282 | |
Andrzej Kurek | f777414 | 2020-01-22 06:34:59 -0500 | [diff] [blame] | 283 | if( output == NULL && output_len == 0 ) |
| 284 | return 0; |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 285 | |
| 286 | if( buf->content_length < output_len ) |
| 287 | output_len = buf->content_length; |
| 288 | |
| 289 | /* Calculate the number of bytes that need to be drawn from lower memory |
| 290 | * address */ |
| 291 | if( buf->start + output_len > buf->capacity ) |
| 292 | { |
| 293 | overflow = ( buf->start + output_len ) % buf->capacity; |
| 294 | } |
| 295 | |
Andrzej Kurek | f777414 | 2020-01-22 06:34:59 -0500 | [diff] [blame] | 296 | if( output != NULL ) |
| 297 | { |
| 298 | memcpy( output, buf->buffer + buf->start, output_len - overflow ); |
| 299 | memcpy( output + output_len - overflow, buf->buffer, overflow ); |
| 300 | } |
| 301 | |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 302 | buf->content_length -= output_len; |
| 303 | buf->start = ( buf->start + output_len ) % buf->capacity; |
| 304 | |
| 305 | return output_len; |
| 306 | } |
| 307 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 308 | /* |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 309 | * Errors used in the message transport mock tests |
| 310 | */ |
| 311 | #define MBEDTLS_TEST_ERROR_ARG_NULL -11 |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 312 | #define MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED -44 |
| 313 | |
| 314 | /* |
| 315 | * Context for a message metadata queue (fifo) that is on top of the ring buffer. |
| 316 | */ |
| 317 | typedef struct mbedtls_test_message_queue |
| 318 | { |
| 319 | size_t *messages; |
| 320 | int pos; |
| 321 | int num; |
| 322 | int capacity; |
| 323 | } mbedtls_test_message_queue; |
| 324 | |
| 325 | /* |
| 326 | * Setup and free functions for the message metadata queue. |
| 327 | * |
| 328 | * \p capacity describes the number of message metadata chunks that can be held |
| 329 | * within the queue. |
| 330 | * |
| 331 | * \retval 0, if a metadata queue of a given length can be allocated. |
| 332 | * \retval MBEDTLS_ERR_SSL_ALLOC_FAILED, if allocation failed. |
| 333 | */ |
| 334 | int mbedtls_test_message_queue_setup( mbedtls_test_message_queue *queue, |
| 335 | size_t capacity ) |
| 336 | { |
| 337 | queue->messages = (size_t*) mbedtls_calloc( capacity, sizeof(size_t) ); |
| 338 | if( NULL == queue->messages ) |
| 339 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 340 | |
| 341 | queue->capacity = capacity; |
| 342 | queue->pos = 0; |
| 343 | queue->num = 0; |
| 344 | |
| 345 | return 0; |
| 346 | } |
| 347 | |
| 348 | void mbedtls_test_message_queue_free( mbedtls_test_message_queue *queue ) |
| 349 | { |
| 350 | if( queue == NULL ) |
| 351 | return; |
| 352 | |
| 353 | if( queue->messages != NULL ) |
| 354 | mbedtls_free( queue->messages ); |
| 355 | |
| 356 | memset( queue, 0, sizeof( *queue ) ); |
| 357 | } |
| 358 | |
| 359 | /* |
| 360 | * Push message length information onto the message metadata queue. |
| 361 | * This will become the last element to leave it (fifo). |
| 362 | * |
| 363 | * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null. |
Andrzej Kurek | f46b912 | 2020-02-07 08:19:00 -0500 | [diff] [blame] | 364 | * \retval MBEDTLS_ERR_SSL_WANT_WRITE, if the queue is full. |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 365 | * \retval \p len, if the push was successful. |
| 366 | */ |
| 367 | int mbedtls_test_message_queue_push_info( mbedtls_test_message_queue *queue, |
| 368 | size_t len ) |
| 369 | { |
| 370 | int place; |
| 371 | if( queue == NULL ) |
| 372 | return MBEDTLS_TEST_ERROR_ARG_NULL; |
| 373 | |
| 374 | if( queue->num >= queue->capacity ) |
Andrzej Kurek | f46b912 | 2020-02-07 08:19:00 -0500 | [diff] [blame] | 375 | return MBEDTLS_ERR_SSL_WANT_WRITE; |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 376 | |
| 377 | place = ( queue->pos + queue->num ) % queue->capacity; |
| 378 | queue->messages[place] = len; |
| 379 | queue->num++; |
| 380 | return len; |
| 381 | } |
| 382 | |
| 383 | /* |
| 384 | * Pop information about the next message length from the queue. This will be |
| 385 | * the oldest inserted message length(fifo). \p msg_len can be null, in which |
| 386 | * case the data will be popped from the queue but not copied anywhere. |
| 387 | * |
| 388 | * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null. |
Andrzej Kurek | f46b912 | 2020-02-07 08:19:00 -0500 | [diff] [blame] | 389 | * \retval MBEDTLS_ERR_SSL_WANT_READ, if the queue is empty. |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 390 | * \retval message length, if the pop was successful, up to the given |
| 391 | \p buf_len. |
| 392 | */ |
| 393 | int mbedtls_test_message_queue_pop_info( mbedtls_test_message_queue *queue, |
| 394 | size_t buf_len ) |
| 395 | { |
| 396 | size_t message_length; |
| 397 | if( queue == NULL ) |
| 398 | return MBEDTLS_TEST_ERROR_ARG_NULL; |
| 399 | if( queue->num == 0 ) |
Andrzej Kurek | f46b912 | 2020-02-07 08:19:00 -0500 | [diff] [blame] | 400 | return MBEDTLS_ERR_SSL_WANT_READ; |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 401 | |
| 402 | message_length = queue->messages[queue->pos]; |
| 403 | queue->messages[queue->pos] = 0; |
| 404 | queue->num--; |
| 405 | queue->pos++; |
| 406 | queue->pos %= queue->capacity; |
| 407 | if( queue->pos < 0 ) |
| 408 | queue->pos += queue->capacity; |
| 409 | |
| 410 | return ( message_length > buf_len ) ? buf_len : message_length; |
| 411 | } |
| 412 | |
| 413 | /* |
| 414 | * Take a peek on the info about the next message length from the queue. |
| 415 | * This will be the oldest inserted message length(fifo). |
| 416 | * |
| 417 | * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null. |
Andrzej Kurek | f46b912 | 2020-02-07 08:19:00 -0500 | [diff] [blame] | 418 | * \retval MBEDTLS_ERR_SSL_WANT_READ, if the queue is empty. |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 419 | * \retval 0, if the peek was successful. |
| 420 | * \retval MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED, if the given buffer length is |
| 421 | * too small to fit the message. In this case the \p msg_len will be |
| 422 | * set to the full message length so that the |
| 423 | * caller knows what portion of the message can be dropped. |
| 424 | */ |
| 425 | int mbedtls_test_message_queue_peek_info( mbedtls_test_message_queue *queue, |
| 426 | size_t buf_len, size_t* msg_len ) |
| 427 | { |
| 428 | if( queue == NULL || msg_len == NULL ) |
| 429 | return MBEDTLS_TEST_ERROR_ARG_NULL; |
| 430 | if( queue->num == 0 ) |
Andrzej Kurek | f46b912 | 2020-02-07 08:19:00 -0500 | [diff] [blame] | 431 | return MBEDTLS_ERR_SSL_WANT_READ; |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 432 | |
| 433 | *msg_len = queue->messages[queue->pos]; |
| 434 | return ( *msg_len > buf_len ) ? MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED : 0; |
| 435 | } |
| 436 | /* |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 437 | * Context for the I/O callbacks simulating network connection. |
| 438 | */ |
| 439 | |
| 440 | #define MBEDTLS_MOCK_SOCKET_CONNECTED 1 |
| 441 | |
| 442 | typedef struct mbedtls_mock_socket |
| 443 | { |
| 444 | int status; |
| 445 | mbedtls_test_buffer *input; |
| 446 | mbedtls_test_buffer *output; |
| 447 | struct mbedtls_mock_socket *peer; |
| 448 | } mbedtls_mock_socket; |
| 449 | |
| 450 | /* |
| 451 | * Setup and teardown functions for mock sockets. |
| 452 | */ |
| 453 | void mbedtls_mock_socket_init( mbedtls_mock_socket *socket ) |
| 454 | { |
| 455 | memset( socket, 0, sizeof( *socket ) ); |
| 456 | } |
| 457 | |
| 458 | /* |
| 459 | * Closes the socket \p socket. |
| 460 | * |
| 461 | * \p socket must have been previously initialized by calling |
| 462 | * mbedtls_mock_socket_init(). |
| 463 | * |
| 464 | * This function frees all allocated resources and both sockets are aware of the |
| 465 | * new connection state. |
| 466 | * |
| 467 | * That is, this function does not simulate half-open TCP connections and the |
| 468 | * phenomenon that when closing a UDP connection the peer is not aware of the |
| 469 | * connection having been closed. |
| 470 | */ |
| 471 | void mbedtls_mock_socket_close( mbedtls_mock_socket* socket ) |
| 472 | { |
| 473 | if( socket == NULL ) |
| 474 | return; |
| 475 | |
| 476 | if( socket->input != NULL ) |
| 477 | { |
| 478 | mbedtls_test_buffer_free( socket->input ); |
| 479 | mbedtls_free( socket->input ); |
| 480 | } |
| 481 | |
| 482 | if( socket->output != NULL ) |
| 483 | { |
| 484 | mbedtls_test_buffer_free( socket->output ); |
| 485 | mbedtls_free( socket->output ); |
| 486 | } |
| 487 | |
| 488 | if( socket->peer != NULL ) |
| 489 | memset( socket->peer, 0, sizeof( *socket->peer ) ); |
| 490 | |
| 491 | memset( socket, 0, sizeof( *socket ) ); |
| 492 | } |
| 493 | |
| 494 | /* |
| 495 | * Establishes a connection between \p peer1 and \p peer2. |
| 496 | * |
| 497 | * \p peer1 and \p peer2 must have been previously initialized by calling |
| 498 | * mbedtls_mock_socket_init(). |
| 499 | * |
| 500 | * The capacites of the internal buffers are set to \p bufsize. Setting this to |
| 501 | * the correct value allows for simulation of MTU, sanity testing the mock |
| 502 | * implementation and mocking TCP connections with lower memory cost. |
| 503 | */ |
| 504 | int mbedtls_mock_socket_connect( mbedtls_mock_socket* peer1, |
| 505 | mbedtls_mock_socket* peer2, |
| 506 | size_t bufsize ) |
| 507 | { |
| 508 | int ret = -1; |
| 509 | |
Piotr Nowicki | d796e19 | 2020-01-28 12:09:47 +0100 | [diff] [blame] | 510 | peer1->output = |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 511 | (mbedtls_test_buffer*) mbedtls_calloc( 1, sizeof(mbedtls_test_buffer) ); |
| 512 | if( peer1->output == NULL ) |
| 513 | { |
| 514 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 515 | goto exit; |
| 516 | } |
| 517 | mbedtls_test_buffer_init( peer1->output ); |
| 518 | if( 0 != ( ret = mbedtls_test_buffer_setup( peer1->output, bufsize ) ) ) |
| 519 | { |
| 520 | goto exit; |
| 521 | } |
| 522 | |
Piotr Nowicki | d796e19 | 2020-01-28 12:09:47 +0100 | [diff] [blame] | 523 | peer2->output = |
| 524 | (mbedtls_test_buffer*) mbedtls_calloc( 1, sizeof(mbedtls_test_buffer) ); |
| 525 | if( peer2->output == NULL ) |
| 526 | { |
| 527 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 528 | goto exit; |
| 529 | } |
| 530 | mbedtls_test_buffer_init( peer2->output ); |
| 531 | if( 0 != ( ret = mbedtls_test_buffer_setup( peer2->output, bufsize ) ) ) |
| 532 | { |
| 533 | goto exit; |
| 534 | } |
| 535 | |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 536 | peer1->peer = peer2; |
| 537 | peer2->peer = peer1; |
Piotr Nowicki | d796e19 | 2020-01-28 12:09:47 +0100 | [diff] [blame] | 538 | peer1->input = peer2->output; |
| 539 | peer2->input = peer1->output; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 540 | |
| 541 | peer1->status = peer2->status = MBEDTLS_MOCK_SOCKET_CONNECTED; |
| 542 | ret = 0; |
| 543 | |
| 544 | exit: |
| 545 | |
| 546 | if( ret != 0 ) |
| 547 | { |
| 548 | mbedtls_mock_socket_close( peer1 ); |
| 549 | mbedtls_mock_socket_close( peer2 ); |
| 550 | } |
| 551 | |
| 552 | return ret; |
| 553 | } |
| 554 | |
| 555 | /* |
| 556 | * Callbacks for simulating blocking I/O over connection-oriented transport. |
| 557 | */ |
| 558 | |
| 559 | int mbedtls_mock_tcp_send_b( void *ctx, const unsigned char *buf, size_t len ) |
| 560 | { |
| 561 | mbedtls_mock_socket *socket = (mbedtls_mock_socket*) ctx; |
| 562 | |
| 563 | if( socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED ) |
| 564 | return -1; |
| 565 | |
| 566 | return mbedtls_test_buffer_put( socket->output, buf, len ); |
| 567 | } |
| 568 | |
| 569 | int mbedtls_mock_tcp_recv_b( void *ctx, unsigned char *buf, size_t len ) |
| 570 | { |
| 571 | mbedtls_mock_socket *socket = (mbedtls_mock_socket*) ctx; |
| 572 | |
| 573 | if( socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED ) |
| 574 | return -1; |
| 575 | |
| 576 | return mbedtls_test_buffer_get( socket->input, buf, len ); |
| 577 | } |
| 578 | |
| 579 | /* |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 580 | * Callbacks for simulating non-blocking I/O over connection-oriented transport. |
| 581 | */ |
| 582 | |
| 583 | int mbedtls_mock_tcp_send_nb( void *ctx, const unsigned char *buf, size_t len ) |
| 584 | { |
| 585 | mbedtls_mock_socket *socket = (mbedtls_mock_socket*) ctx; |
| 586 | |
| 587 | if( socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED ) |
| 588 | return -1; |
| 589 | |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 590 | if( socket->output->capacity == socket->output->content_length ) |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 591 | { |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 592 | return MBEDTLS_ERR_SSL_WANT_WRITE; |
| 593 | } |
| 594 | |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 595 | return mbedtls_test_buffer_put( socket->output, buf, len ); |
| 596 | } |
| 597 | |
| 598 | int mbedtls_mock_tcp_recv_nb( void *ctx, unsigned char *buf, size_t len ) |
| 599 | { |
| 600 | mbedtls_mock_socket *socket = (mbedtls_mock_socket*) ctx; |
| 601 | |
| 602 | if( socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED ) |
| 603 | return -1; |
| 604 | |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 605 | if( socket->input->content_length == 0 ) |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 606 | { |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 607 | return MBEDTLS_ERR_SSL_WANT_READ; |
| 608 | } |
| 609 | |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 610 | return mbedtls_test_buffer_get( socket->input, buf, len ); |
| 611 | } |
| 612 | |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 613 | /* Errors used in the message socket mocks */ |
| 614 | |
| 615 | #define MBEDTLS_TEST_ERROR_CONTEXT_ERROR -55 |
| 616 | #define MBEDTLS_TEST_ERROR_SEND_FAILED -66 |
| 617 | #define MBEDTLS_TEST_ERROR_RECV_FAILED -77 |
| 618 | |
| 619 | /* |
| 620 | * Structure used as an addon, or a wrapper, around the mocked sockets. |
| 621 | * Contains an input queue, to which the other socket pushes metadata, |
| 622 | * and an output queue, to which this one pushes metadata. This context is |
| 623 | * considered as an owner of the input queue only, which is initialized and |
| 624 | * freed in the respective setup and free calls. |
| 625 | */ |
| 626 | typedef struct mbedtls_test_message_socket_context |
| 627 | { |
| 628 | mbedtls_test_message_queue* queue_input; |
| 629 | mbedtls_test_message_queue* queue_output; |
| 630 | mbedtls_mock_socket* socket; |
| 631 | } mbedtls_test_message_socket_context; |
| 632 | |
Andrzej Kurek | 45916ba | 2020-03-05 14:46:22 -0500 | [diff] [blame] | 633 | void mbedtls_message_socket_init( mbedtls_test_message_socket_context *ctx ) |
| 634 | { |
| 635 | ctx->queue_input = NULL; |
| 636 | ctx->queue_output = NULL; |
| 637 | ctx->socket = NULL; |
| 638 | } |
| 639 | |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 640 | /* |
| 641 | * Setup a given mesasge socket context including initialization of |
| 642 | * input/output queues to a chosen capacity of messages. Also set the |
| 643 | * corresponding mock socket. |
| 644 | * |
| 645 | * \retval 0, if everything succeeds. |
| 646 | * \retval MBEDTLS_ERR_SSL_ALLOC_FAILED, if allocation of a message |
| 647 | * queue failed. |
| 648 | */ |
| 649 | int mbedtls_message_socket_setup( mbedtls_test_message_queue* queue_input, |
| 650 | mbedtls_test_message_queue* queue_output, |
| 651 | size_t queue_capacity, |
| 652 | mbedtls_mock_socket* socket, |
| 653 | mbedtls_test_message_socket_context* ctx ) |
| 654 | { |
| 655 | int ret = mbedtls_test_message_queue_setup( queue_input, queue_capacity ); |
| 656 | if( ret != 0 ) |
| 657 | return ret; |
| 658 | ctx->queue_input = queue_input; |
| 659 | ctx->queue_output = queue_output; |
| 660 | ctx->socket = socket; |
| 661 | mbedtls_mock_socket_init( socket ); |
| 662 | |
| 663 | return 0; |
| 664 | } |
| 665 | |
| 666 | /* |
| 667 | * Close a given message socket context, along with the socket itself. Free the |
| 668 | * memory allocated by the input queue. |
| 669 | */ |
| 670 | void mbedtls_message_socket_close( mbedtls_test_message_socket_context* ctx ) |
| 671 | { |
| 672 | if( ctx == NULL ) |
| 673 | return; |
| 674 | |
| 675 | mbedtls_test_message_queue_free( ctx->queue_input ); |
| 676 | mbedtls_mock_socket_close( ctx->socket ); |
| 677 | memset( ctx, 0, sizeof( *ctx ) ); |
| 678 | } |
| 679 | |
| 680 | /* |
| 681 | * Send one message through a given message socket context. |
| 682 | * |
| 683 | * \retval \p len, if everything succeeds. |
| 684 | * \retval MBEDTLS_TEST_ERROR_CONTEXT_ERROR, if any of the needed context |
| 685 | * elements or the context itself is null. |
| 686 | * \retval MBEDTLS_TEST_ERROR_SEND_FAILED if mbedtls_mock_tcp_send_b failed. |
Andrzej Kurek | f46b912 | 2020-02-07 08:19:00 -0500 | [diff] [blame] | 687 | * \retval MBEDTLS_ERR_SSL_WANT_WRITE, if the output queue is full. |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 688 | * |
| 689 | * This function will also return any error from |
| 690 | * mbedtls_test_message_queue_push_info. |
| 691 | */ |
| 692 | int mbedtls_mock_tcp_send_msg( void *ctx, const unsigned char *buf, size_t len ) |
| 693 | { |
| 694 | mbedtls_test_message_queue* queue; |
| 695 | mbedtls_mock_socket* socket; |
| 696 | mbedtls_test_message_socket_context *context = (mbedtls_test_message_socket_context*) ctx; |
| 697 | |
| 698 | if( context == NULL || context->socket == NULL |
| 699 | || context->queue_output == NULL ) |
| 700 | { |
| 701 | return MBEDTLS_TEST_ERROR_CONTEXT_ERROR; |
| 702 | } |
| 703 | |
| 704 | queue = context->queue_output; |
| 705 | socket = context->socket; |
| 706 | |
| 707 | if( queue->num >= queue->capacity ) |
Andrzej Kurek | f46b912 | 2020-02-07 08:19:00 -0500 | [diff] [blame] | 708 | return MBEDTLS_ERR_SSL_WANT_WRITE; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 709 | |
| 710 | if( mbedtls_mock_tcp_send_b( socket, buf, len ) != (int) len ) |
| 711 | return MBEDTLS_TEST_ERROR_SEND_FAILED; |
| 712 | |
| 713 | return mbedtls_test_message_queue_push_info( queue, len ); |
| 714 | } |
| 715 | |
| 716 | /* |
| 717 | * Receive one message from a given message socket context and return message |
| 718 | * length or an error. |
| 719 | * |
| 720 | * \retval message length, if everything succeeds. |
| 721 | * \retval MBEDTLS_TEST_ERROR_CONTEXT_ERROR, if any of the needed context |
| 722 | * elements or the context itself is null. |
| 723 | * \retval MBEDTLS_TEST_ERROR_RECV_FAILED if mbedtls_mock_tcp_recv_b failed. |
| 724 | * |
| 725 | * This function will also return any error other than |
| 726 | * MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED from mbedtls_test_message_queue_peek_info. |
| 727 | */ |
| 728 | int mbedtls_mock_tcp_recv_msg( void *ctx, unsigned char *buf, size_t buf_len ) |
| 729 | { |
| 730 | mbedtls_test_message_queue* queue; |
| 731 | mbedtls_mock_socket* socket; |
| 732 | mbedtls_test_message_socket_context *context = (mbedtls_test_message_socket_context*) ctx; |
Gilles Peskine | 19e841e | 2020-03-09 20:43:51 +0100 | [diff] [blame] | 733 | size_t drop_len = 0; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 734 | size_t msg_len; |
| 735 | int ret; |
| 736 | |
| 737 | if( context == NULL || context->socket == NULL |
| 738 | || context->queue_input == NULL ) |
| 739 | { |
| 740 | return MBEDTLS_TEST_ERROR_CONTEXT_ERROR; |
| 741 | } |
| 742 | |
| 743 | queue = context->queue_input; |
| 744 | socket = context->socket; |
| 745 | |
| 746 | /* Peek first, so that in case of a socket error the data remains in |
| 747 | * the queue. */ |
| 748 | ret = mbedtls_test_message_queue_peek_info( queue, buf_len, &msg_len ); |
| 749 | if( ret == MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED ) |
| 750 | { |
| 751 | /* Calculate how much to drop */ |
| 752 | drop_len = msg_len - buf_len; |
| 753 | |
| 754 | /* Set the requested message len to be buffer length */ |
| 755 | msg_len = buf_len; |
| 756 | } else if( ret != 0 ) |
| 757 | { |
| 758 | return ret; |
| 759 | } |
| 760 | |
| 761 | if( mbedtls_mock_tcp_recv_b( socket, buf, msg_len ) != (int) msg_len ) |
| 762 | return MBEDTLS_TEST_ERROR_RECV_FAILED; |
| 763 | |
| 764 | if( ret == MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED ) |
| 765 | { |
| 766 | /* Drop the remaining part of the message */ |
| 767 | if( mbedtls_mock_tcp_recv_b( socket, NULL, drop_len ) != (int) drop_len ) |
| 768 | { |
| 769 | /* Inconsistent state - part of the message was read, |
| 770 | * and a part couldn't. Not much we can do here, but it should not |
| 771 | * happen in test environment, unless forced manually. */ |
| 772 | } |
| 773 | } |
| 774 | mbedtls_test_message_queue_pop_info( queue, buf_len ); |
| 775 | |
| 776 | return msg_len; |
| 777 | } |
| 778 | |
Manuel Pégourié-Gonnard | d12402f | 2020-05-20 10:34:25 +0200 | [diff] [blame] | 779 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 780 | defined(MBEDTLS_ENTROPY_C) && \ |
| 781 | defined(MBEDTLS_CTR_DRBG_C) |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 782 | |
| 783 | /* |
| 784 | * Structure with endpoint's certificates for SSL communication tests. |
| 785 | */ |
| 786 | typedef struct mbedtls_endpoint_certificate |
| 787 | { |
| 788 | mbedtls_x509_crt ca_cert; |
| 789 | mbedtls_x509_crt cert; |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 790 | mbedtls_pk_context pkey; |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 791 | } mbedtls_endpoint_certificate; |
| 792 | |
| 793 | /* |
| 794 | * Endpoint structure for SSL communication tests. |
| 795 | */ |
| 796 | typedef struct mbedtls_endpoint |
| 797 | { |
| 798 | const char *name; |
| 799 | mbedtls_ssl_context ssl; |
| 800 | mbedtls_ssl_config conf; |
| 801 | mbedtls_ctr_drbg_context ctr_drbg; |
| 802 | mbedtls_entropy_context entropy; |
| 803 | mbedtls_mock_socket socket; |
| 804 | mbedtls_endpoint_certificate cert; |
| 805 | } mbedtls_endpoint; |
| 806 | |
| 807 | /* |
| 808 | * Initializes \p ep_cert structure and assigns it to endpoint |
| 809 | * represented by \p ep. |
| 810 | * |
| 811 | * \retval 0 on success, otherwise error code. |
| 812 | */ |
Neil Armstrong | 8c52ed8 | 2022-05-27 13:14:55 +0200 | [diff] [blame] | 813 | int mbedtls_endpoint_certificate_init( mbedtls_endpoint *ep, int pk_alg, |
| 814 | int opaque_alg, int opaque_alg2, |
| 815 | int opaque_usage ) |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 816 | { |
| 817 | int i = 0; |
| 818 | int ret = -1; |
| 819 | mbedtls_endpoint_certificate *cert; |
Neil Armstrong | 8c52ed8 | 2022-05-27 13:14:55 +0200 | [diff] [blame] | 820 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 821 | mbedtls_svc_key_id_t key_slot = MBEDTLS_SVC_KEY_ID_INIT; |
| 822 | #endif |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 823 | |
| 824 | if( ep == NULL ) |
| 825 | { |
| 826 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 827 | } |
| 828 | |
| 829 | cert = &( ep->cert ); |
| 830 | mbedtls_x509_crt_init( &( cert->ca_cert ) ); |
| 831 | mbedtls_x509_crt_init( &( cert->cert ) ); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 832 | mbedtls_pk_init( &( cert->pkey ) ); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 833 | |
| 834 | /* Load the trusted CA */ |
| 835 | |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 836 | for( i = 0; mbedtls_test_cas_der[i] != NULL; i++ ) |
| 837 | { |
| 838 | ret = mbedtls_x509_crt_parse_der( &( cert->ca_cert ), |
| 839 | (const unsigned char *) mbedtls_test_cas_der[i], |
| 840 | mbedtls_test_cas_der_len[i] ); |
| 841 | TEST_ASSERT( ret == 0 ); |
| 842 | } |
| 843 | |
| 844 | /* Load own certificate and private key */ |
| 845 | |
| 846 | if( ep->conf.endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 847 | { |
Andrzej Kurek | b298074 | 2020-02-02 19:25:26 -0500 | [diff] [blame] | 848 | if( pk_alg == MBEDTLS_PK_RSA ) |
| 849 | { |
| 850 | ret = mbedtls_x509_crt_parse( &( cert->cert ), |
| 851 | (const unsigned char*) mbedtls_test_srv_crt_rsa_sha256_der, |
| 852 | mbedtls_test_srv_crt_rsa_sha256_der_len ); |
| 853 | TEST_ASSERT( ret == 0 ); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 854 | |
Andrzej Kurek | b298074 | 2020-02-02 19:25:26 -0500 | [diff] [blame] | 855 | ret = mbedtls_pk_parse_key( &( cert->pkey ), |
| 856 | (const unsigned char*) mbedtls_test_srv_key_rsa_der, |
Manuel Pégourié-Gonnard | 84dea01 | 2021-06-15 11:29:26 +0200 | [diff] [blame] | 857 | mbedtls_test_srv_key_rsa_der_len, NULL, 0, |
| 858 | mbedtls_test_rnd_std_rand, NULL ); |
Andrzej Kurek | b298074 | 2020-02-02 19:25:26 -0500 | [diff] [blame] | 859 | TEST_ASSERT( ret == 0 ); |
| 860 | } |
| 861 | else |
| 862 | { |
| 863 | ret = mbedtls_x509_crt_parse( &( cert->cert ), |
| 864 | (const unsigned char*) mbedtls_test_srv_crt_ec_der, |
| 865 | mbedtls_test_srv_crt_ec_der_len ); |
| 866 | TEST_ASSERT( ret == 0 ); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 867 | |
Andrzej Kurek | b298074 | 2020-02-02 19:25:26 -0500 | [diff] [blame] | 868 | ret = mbedtls_pk_parse_key( &( cert->pkey ), |
| 869 | (const unsigned char*) mbedtls_test_srv_key_ec_der, |
Manuel Pégourié-Gonnard | 84dea01 | 2021-06-15 11:29:26 +0200 | [diff] [blame] | 870 | mbedtls_test_srv_key_ec_der_len, NULL, 0, |
| 871 | mbedtls_test_rnd_std_rand, NULL ); |
Andrzej Kurek | b298074 | 2020-02-02 19:25:26 -0500 | [diff] [blame] | 872 | TEST_ASSERT( ret == 0 ); |
| 873 | } |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 874 | } |
| 875 | else |
| 876 | { |
Andrzej Kurek | b298074 | 2020-02-02 19:25:26 -0500 | [diff] [blame] | 877 | if( pk_alg == MBEDTLS_PK_RSA ) |
| 878 | { |
| 879 | ret = mbedtls_x509_crt_parse( &( cert->cert ), |
| 880 | (const unsigned char *) mbedtls_test_cli_crt_rsa_der, |
| 881 | mbedtls_test_cli_crt_rsa_der_len ); |
| 882 | TEST_ASSERT( ret == 0 ); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 883 | |
Andrzej Kurek | b298074 | 2020-02-02 19:25:26 -0500 | [diff] [blame] | 884 | ret = mbedtls_pk_parse_key( &( cert->pkey ), |
| 885 | (const unsigned char *) mbedtls_test_cli_key_rsa_der, |
Manuel Pégourié-Gonnard | 84dea01 | 2021-06-15 11:29:26 +0200 | [diff] [blame] | 886 | mbedtls_test_cli_key_rsa_der_len, NULL, 0, |
| 887 | mbedtls_test_rnd_std_rand, NULL ); |
Andrzej Kurek | b298074 | 2020-02-02 19:25:26 -0500 | [diff] [blame] | 888 | TEST_ASSERT( ret == 0 ); |
| 889 | } |
| 890 | else |
| 891 | { |
| 892 | ret = mbedtls_x509_crt_parse( &( cert->cert ), |
| 893 | (const unsigned char *) mbedtls_test_cli_crt_ec_der, |
| 894 | mbedtls_test_cli_crt_ec_len ); |
| 895 | TEST_ASSERT( ret == 0 ); |
| 896 | |
| 897 | ret = mbedtls_pk_parse_key( &( cert->pkey ), |
| 898 | (const unsigned char *) mbedtls_test_cli_key_ec_der, |
Manuel Pégourié-Gonnard | 84dea01 | 2021-06-15 11:29:26 +0200 | [diff] [blame] | 899 | mbedtls_test_cli_key_ec_der_len, NULL, 0, |
| 900 | mbedtls_test_rnd_std_rand, NULL ); |
Andrzej Kurek | b298074 | 2020-02-02 19:25:26 -0500 | [diff] [blame] | 901 | TEST_ASSERT( ret == 0 ); |
| 902 | } |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 903 | } |
| 904 | |
Neil Armstrong | 8c52ed8 | 2022-05-27 13:14:55 +0200 | [diff] [blame] | 905 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 906 | if( opaque_alg != 0 ) |
| 907 | { |
| 908 | TEST_EQUAL( mbedtls_pk_wrap_as_opaque( &( cert->pkey ), &key_slot, |
| 909 | opaque_alg, opaque_usage, |
| 910 | opaque_alg2 ), 0 ); |
| 911 | } |
| 912 | #else |
| 913 | (void) opaque_alg; |
| 914 | (void) opaque_alg2; |
| 915 | (void) opaque_usage; |
| 916 | #endif |
| 917 | |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 918 | mbedtls_ssl_conf_ca_chain( &( ep->conf ), &( cert->ca_cert ), NULL ); |
| 919 | |
Andrzej Kurek | b298074 | 2020-02-02 19:25:26 -0500 | [diff] [blame] | 920 | ret = mbedtls_ssl_conf_own_cert( &( ep->conf ), &( cert->cert ), |
| 921 | &( cert->pkey ) ); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 922 | TEST_ASSERT( ret == 0 ); |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 923 | TEST_ASSERT( ep->conf.key_cert != NULL ); |
| 924 | |
| 925 | ret = mbedtls_ssl_conf_own_cert( &( ep->conf ), NULL, NULL ); |
| 926 | TEST_ASSERT( ret == 0 ); |
| 927 | TEST_ASSERT( ep->conf.key_cert == NULL ); |
| 928 | |
| 929 | ret = mbedtls_ssl_conf_own_cert( &( ep->conf ), &( cert->cert ), |
| 930 | &( cert->pkey ) ); |
| 931 | TEST_ASSERT( ret == 0 ); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 932 | |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 933 | exit: |
| 934 | if( ret != 0 ) |
| 935 | { |
| 936 | mbedtls_x509_crt_free( &( cert->ca_cert ) ); |
| 937 | mbedtls_x509_crt_free( &( cert->cert ) ); |
Neil Armstrong | 8c52ed8 | 2022-05-27 13:14:55 +0200 | [diff] [blame] | 938 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 939 | if( opaque_alg != 0 ) |
| 940 | psa_destroy_key( key_slot ); |
| 941 | #endif |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 942 | mbedtls_pk_free( &( cert->pkey ) ); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 943 | } |
| 944 | |
| 945 | return ret; |
| 946 | } |
| 947 | |
| 948 | /* |
| 949 | * Initializes \p ep structure. It is important to call `mbedtls_endpoint_free()` |
| 950 | * after calling this function even if it fails. |
| 951 | * |
| 952 | * \p endpoint_type must be set as MBEDTLS_SSL_IS_SERVER or |
| 953 | * MBEDTLS_SSL_IS_CLIENT. |
Andrzej Kurek | 15daf50 | 2020-02-12 09:17:52 -0500 | [diff] [blame] | 954 | * \p pk_alg the algorithm to use, currently only MBEDTLS_PK_RSA and |
| 955 | * MBEDTLS_PK_ECDSA are supported. |
| 956 | * \p dtls_context - in case of DTLS - this is the context handling metadata. |
| 957 | * \p input_queue - used only in case of DTLS. |
| 958 | * \p output_queue - used only in case of DTLS. |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 959 | * |
| 960 | * \retval 0 on success, otherwise error code. |
| 961 | */ |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 962 | int mbedtls_endpoint_init( mbedtls_endpoint *ep, int endpoint_type, |
Andrzej Kurek | 92d7417 | 2022-06-28 10:29:42 -0400 | [diff] [blame] | 963 | handshake_test_options *options, |
Andrzej Kurek | 15daf50 | 2020-02-12 09:17:52 -0500 | [diff] [blame] | 964 | mbedtls_test_message_socket_context *dtls_context, |
| 965 | mbedtls_test_message_queue *input_queue, |
Andrzej Kurek | 74394a5 | 2022-03-08 06:50:12 -0500 | [diff] [blame] | 966 | mbedtls_test_message_queue *output_queue, |
| 967 | uint16_t* group_list ) |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 968 | { |
| 969 | int ret = -1; |
Gilles Peskine | 80dae04 | 2022-01-21 23:50:39 +0100 | [diff] [blame] | 970 | uintptr_t user_data_n; |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 971 | |
Andrzej Kurek | 15daf50 | 2020-02-12 09:17:52 -0500 | [diff] [blame] | 972 | if( dtls_context != NULL && ( input_queue == NULL || output_queue == NULL ) ) |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 973 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Andrzej Kurek | 15daf50 | 2020-02-12 09:17:52 -0500 | [diff] [blame] | 974 | |
| 975 | if( ep == NULL ) |
| 976 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 977 | |
| 978 | memset( ep, 0, sizeof( *ep ) ); |
| 979 | |
| 980 | ep->name = ( endpoint_type == MBEDTLS_SSL_IS_SERVER ) ? "Server" : "Client"; |
| 981 | |
| 982 | mbedtls_ssl_init( &( ep->ssl ) ); |
| 983 | mbedtls_ssl_config_init( &( ep->conf ) ); |
| 984 | mbedtls_ctr_drbg_init( &( ep->ctr_drbg ) ); |
| 985 | mbedtls_ssl_conf_rng( &( ep->conf ), |
| 986 | mbedtls_ctr_drbg_random, |
| 987 | &( ep->ctr_drbg ) ); |
| 988 | mbedtls_entropy_init( &( ep->entropy ) ); |
Gilles Peskine | 80dae04 | 2022-01-21 23:50:39 +0100 | [diff] [blame] | 989 | |
| 990 | TEST_ASSERT( mbedtls_ssl_conf_get_user_data_p( &ep->conf ) == NULL ); |
| 991 | TEST_EQUAL( mbedtls_ssl_conf_get_user_data_n( &ep->conf ), 0 ); |
| 992 | TEST_ASSERT( mbedtls_ssl_get_user_data_p( &ep->ssl ) == NULL ); |
| 993 | TEST_EQUAL( mbedtls_ssl_get_user_data_n( &ep->ssl ), 0 ); |
| 994 | |
| 995 | (void) mbedtls_test_rnd_std_rand( NULL, |
| 996 | (void*) &user_data_n, |
| 997 | sizeof( user_data_n ) ); |
| 998 | mbedtls_ssl_conf_set_user_data_n( &ep->conf, user_data_n ); |
| 999 | mbedtls_ssl_set_user_data_n( &ep->ssl, user_data_n ); |
| 1000 | |
Andrzej Kurek | 15daf50 | 2020-02-12 09:17:52 -0500 | [diff] [blame] | 1001 | if( dtls_context != NULL ) |
| 1002 | { |
| 1003 | TEST_ASSERT( mbedtls_message_socket_setup( input_queue, output_queue, |
| 1004 | 100, &( ep->socket ), |
| 1005 | dtls_context ) == 0 ); |
| 1006 | } |
| 1007 | else |
| 1008 | { |
| 1009 | mbedtls_mock_socket_init( &( ep->socket ) ); |
| 1010 | } |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 1011 | |
| 1012 | ret = mbedtls_ctr_drbg_seed( &( ep->ctr_drbg ), mbedtls_entropy_func, |
| 1013 | &( ep->entropy ), (const unsigned char *) ( ep->name ), |
| 1014 | strlen( ep->name ) ); |
| 1015 | TEST_ASSERT( ret == 0 ); |
| 1016 | |
| 1017 | /* Non-blocking callbacks without timeout */ |
Andrzej Kurek | 15daf50 | 2020-02-12 09:17:52 -0500 | [diff] [blame] | 1018 | if( dtls_context != NULL ) |
| 1019 | { |
| 1020 | mbedtls_ssl_set_bio( &( ep->ssl ), dtls_context, |
| 1021 | mbedtls_mock_tcp_send_msg, |
| 1022 | mbedtls_mock_tcp_recv_msg, |
| 1023 | NULL ); |
| 1024 | } |
| 1025 | else |
| 1026 | { |
| 1027 | mbedtls_ssl_set_bio( &( ep->ssl ), &( ep->socket ), |
| 1028 | mbedtls_mock_tcp_send_nb, |
| 1029 | mbedtls_mock_tcp_recv_nb, |
| 1030 | NULL ); |
| 1031 | } |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 1032 | |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 1033 | ret = mbedtls_ssl_config_defaults( &( ep->conf ), endpoint_type, |
Andrzej Kurek | 15daf50 | 2020-02-12 09:17:52 -0500 | [diff] [blame] | 1034 | ( dtls_context != NULL ) ? |
| 1035 | MBEDTLS_SSL_TRANSPORT_DATAGRAM : |
| 1036 | MBEDTLS_SSL_TRANSPORT_STREAM, |
| 1037 | MBEDTLS_SSL_PRESET_DEFAULT ); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 1038 | TEST_ASSERT( ret == 0 ); |
| 1039 | |
Andrzej Kurek | 74394a5 | 2022-03-08 06:50:12 -0500 | [diff] [blame] | 1040 | if( group_list != NULL ) |
| 1041 | mbedtls_ssl_conf_groups( &(ep->conf), group_list ); |
| 1042 | |
Ronald Cron | bdddaef | 2022-06-07 10:34:59 +0200 | [diff] [blame] | 1043 | mbedtls_ssl_conf_authmode( &( ep->conf ), MBEDTLS_SSL_VERIFY_REQUIRED ); |
| 1044 | |
Andrzej Kurek | ed58b50 | 2022-06-10 19:24:05 -0400 | [diff] [blame] | 1045 | #if defined(MBEDTLS_SSL_CACHE_C) && defined(MBEDTLS_SSL_SRV_C) |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 1046 | if( endpoint_type == MBEDTLS_SSL_IS_SERVER && options->cache != NULL ) |
| 1047 | { |
| 1048 | mbedtls_ssl_conf_session_cache( &( ep->conf ), options->cache, |
| 1049 | mbedtls_ssl_cache_get, |
| 1050 | mbedtls_ssl_cache_set ); |
| 1051 | } |
| 1052 | #endif |
| 1053 | |
Andrzej Kurek | 1a44a15 | 2020-02-07 08:21:32 -0500 | [diff] [blame] | 1054 | ret = mbedtls_ssl_setup( &( ep->ssl ), &( ep->conf ) ); |
| 1055 | TEST_ASSERT( ret == 0 ); |
Andrzej Kurek | 15daf50 | 2020-02-12 09:17:52 -0500 | [diff] [blame] | 1056 | |
| 1057 | #if defined(MBEDTLS_SSL_PROTO_DTLS) && defined(MBEDTLS_SSL_SRV_C) |
| 1058 | if( endpoint_type == MBEDTLS_SSL_IS_SERVER && dtls_context != NULL ) |
| 1059 | mbedtls_ssl_conf_dtls_cookies( &( ep->conf ), NULL, NULL, NULL ); |
| 1060 | #endif |
| 1061 | |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 1062 | ret = mbedtls_endpoint_certificate_init( ep, options->pk_alg, |
| 1063 | options->opaque_alg, |
| 1064 | options->opaque_alg2, |
| 1065 | options->opaque_usage ); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 1066 | TEST_ASSERT( ret == 0 ); |
| 1067 | |
Gilles Peskine | 80dae04 | 2022-01-21 23:50:39 +0100 | [diff] [blame] | 1068 | TEST_EQUAL( mbedtls_ssl_conf_get_user_data_n( &ep->conf ), user_data_n ); |
| 1069 | mbedtls_ssl_conf_set_user_data_p( &ep->conf, ep ); |
| 1070 | TEST_EQUAL( mbedtls_ssl_get_user_data_n( &ep->ssl ), user_data_n ); |
| 1071 | mbedtls_ssl_set_user_data_p( &ep->ssl, ep ); |
| 1072 | |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 1073 | exit: |
| 1074 | return ret; |
| 1075 | } |
| 1076 | |
| 1077 | /* |
| 1078 | * Deinitializes certificates from endpoint represented by \p ep. |
| 1079 | */ |
| 1080 | void mbedtls_endpoint_certificate_free( mbedtls_endpoint *ep ) |
| 1081 | { |
| 1082 | mbedtls_endpoint_certificate *cert = &( ep->cert ); |
| 1083 | mbedtls_x509_crt_free( &( cert->ca_cert ) ); |
| 1084 | mbedtls_x509_crt_free( &( cert->cert ) ); |
Neil Armstrong | 8c52ed8 | 2022-05-27 13:14:55 +0200 | [diff] [blame] | 1085 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1086 | if( mbedtls_pk_get_type( &( cert->pkey ) ) == MBEDTLS_PK_OPAQUE ) |
| 1087 | { |
| 1088 | mbedtls_svc_key_id_t *key_slot = cert->pkey.pk_ctx; |
| 1089 | |
| 1090 | psa_destroy_key( *key_slot ); |
| 1091 | } |
| 1092 | #endif |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 1093 | mbedtls_pk_free( &( cert->pkey ) ); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 1094 | } |
| 1095 | |
| 1096 | /* |
| 1097 | * Deinitializes endpoint represented by \p ep. |
| 1098 | */ |
Andrzej Kurek | 15daf50 | 2020-02-12 09:17:52 -0500 | [diff] [blame] | 1099 | void mbedtls_endpoint_free( mbedtls_endpoint *ep, |
| 1100 | mbedtls_test_message_socket_context *context ) |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 1101 | { |
| 1102 | mbedtls_endpoint_certificate_free( ep ); |
| 1103 | |
| 1104 | mbedtls_ssl_free( &( ep->ssl ) ); |
| 1105 | mbedtls_ssl_config_free( &( ep->conf ) ); |
| 1106 | mbedtls_ctr_drbg_free( &( ep->ctr_drbg ) ); |
| 1107 | mbedtls_entropy_free( &( ep->entropy ) ); |
Andrzej Kurek | 15daf50 | 2020-02-12 09:17:52 -0500 | [diff] [blame] | 1108 | |
| 1109 | if( context != NULL ) |
| 1110 | { |
| 1111 | mbedtls_message_socket_close( context ); |
| 1112 | } |
| 1113 | else |
| 1114 | { |
| 1115 | mbedtls_mock_socket_close( &( ep->socket ) ); |
| 1116 | } |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 1117 | } |
| 1118 | |
| 1119 | /* |
| 1120 | * This function moves ssl handshake from \p ssl to prescribed \p state. |
| 1121 | * /p second_ssl is used as second endpoint and their sockets have to be |
| 1122 | * connected before calling this function. |
| 1123 | * |
| 1124 | * \retval 0 on success, otherwise error code. |
| 1125 | */ |
| 1126 | int mbedtls_move_handshake_to_state( mbedtls_ssl_context *ssl, |
| 1127 | mbedtls_ssl_context *second_ssl, |
| 1128 | int state ) |
| 1129 | { |
| 1130 | enum { BUFFSIZE = 1024 }; |
| 1131 | int max_steps = 1000; |
| 1132 | int ret = 0; |
| 1133 | |
| 1134 | if( ssl == NULL || second_ssl == NULL ) |
| 1135 | { |
| 1136 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 1137 | } |
| 1138 | |
| 1139 | /* Perform communication via connected sockets */ |
| 1140 | while( ( ssl->state != state ) && ( --max_steps >= 0 ) ) |
| 1141 | { |
| 1142 | /* If /p second_ssl ends the handshake procedure before /p ssl then |
| 1143 | * there is no need to call the next step */ |
Paul Elliott | 27b0d94 | 2022-03-18 21:55:32 +0000 | [diff] [blame] | 1144 | if( !mbedtls_ssl_is_handshake_over( second_ssl ) ) |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 1145 | { |
| 1146 | ret = mbedtls_ssl_handshake_step( second_ssl ); |
| 1147 | if( ret != 0 && ret != MBEDTLS_ERR_SSL_WANT_READ && |
| 1148 | ret != MBEDTLS_ERR_SSL_WANT_WRITE ) |
| 1149 | { |
| 1150 | return ret; |
| 1151 | } |
| 1152 | } |
| 1153 | |
| 1154 | /* We only care about the \p ssl state and returns, so we call it last, |
| 1155 | * to leave the iteration as soon as the state is as expected. */ |
| 1156 | ret = mbedtls_ssl_handshake_step( ssl ); |
| 1157 | if( ret != 0 && ret != MBEDTLS_ERR_SSL_WANT_READ && |
| 1158 | ret != MBEDTLS_ERR_SSL_WANT_WRITE ) |
| 1159 | { |
| 1160 | return ret; |
| 1161 | } |
| 1162 | } |
| 1163 | |
| 1164 | return ( max_steps >= 0 ) ? ret : -1; |
| 1165 | } |
| 1166 | |
Manuel Pégourié-Gonnard | d12402f | 2020-05-20 10:34:25 +0200 | [diff] [blame] | 1167 | #endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */ |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 1168 | |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 1169 | /* |
Piotr Nowicki | 438bf3b | 2020-03-10 12:59:10 +0100 | [diff] [blame] | 1170 | * Write application data. Increase write counter if necessary. |
Piotr Nowicki | c3fca5e | 2020-01-30 15:33:42 +0100 | [diff] [blame] | 1171 | */ |
| 1172 | int mbedtls_ssl_write_fragment( mbedtls_ssl_context *ssl, unsigned char *buf, |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1173 | int buf_len, int *written, |
Piotr Nowicki | 438bf3b | 2020-03-10 12:59:10 +0100 | [diff] [blame] | 1174 | const int expected_fragments ) |
Piotr Nowicki | c3fca5e | 2020-01-30 15:33:42 +0100 | [diff] [blame] | 1175 | { |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1176 | int ret = mbedtls_ssl_write( ssl, buf + *written, buf_len - *written ); |
| 1177 | if( ret > 0 ) |
Piotr Nowicki | c3fca5e | 2020-01-30 15:33:42 +0100 | [diff] [blame] | 1178 | { |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1179 | *written += ret; |
Piotr Nowicki | c3fca5e | 2020-01-30 15:33:42 +0100 | [diff] [blame] | 1180 | } |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1181 | |
| 1182 | if( expected_fragments == 0 ) |
| 1183 | { |
| 1184 | /* Used for DTLS and the message size larger than MFL. In that case |
| 1185 | * the message can not be fragmented and the library should return |
| 1186 | * MBEDTLS_ERR_SSL_BAD_INPUT_DATA error. This error must be returned |
| 1187 | * to prevent a dead loop inside mbedtls_exchange_data(). */ |
| 1188 | return ret; |
| 1189 | } |
| 1190 | else if( expected_fragments == 1 ) |
| 1191 | { |
| 1192 | /* Used for TLS/DTLS and the message size lower than MFL */ |
| 1193 | TEST_ASSERT( ret == buf_len || |
| 1194 | ret == MBEDTLS_ERR_SSL_WANT_READ || |
| 1195 | ret == MBEDTLS_ERR_SSL_WANT_WRITE ); |
| 1196 | } |
| 1197 | else |
| 1198 | { |
| 1199 | /* Used for TLS and the message size larger than MFL */ |
| 1200 | TEST_ASSERT( expected_fragments > 1 ); |
| 1201 | TEST_ASSERT( ( ret >= 0 && ret <= buf_len ) || |
| 1202 | ret == MBEDTLS_ERR_SSL_WANT_READ || |
| 1203 | ret == MBEDTLS_ERR_SSL_WANT_WRITE ); |
| 1204 | } |
| 1205 | |
| 1206 | return 0; |
| 1207 | |
| 1208 | exit: |
| 1209 | /* Some of the tests failed */ |
| 1210 | return -1; |
Piotr Nowicki | c3fca5e | 2020-01-30 15:33:42 +0100 | [diff] [blame] | 1211 | } |
| 1212 | |
| 1213 | /* |
Piotr Nowicki | 438bf3b | 2020-03-10 12:59:10 +0100 | [diff] [blame] | 1214 | * Read application data and increase read counter and fragments counter if necessary. |
Piotr Nowicki | c3fca5e | 2020-01-30 15:33:42 +0100 | [diff] [blame] | 1215 | */ |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1216 | int mbedtls_ssl_read_fragment( mbedtls_ssl_context *ssl, unsigned char *buf, |
| 1217 | int buf_len, int *read, |
Piotr Nowicki | 438bf3b | 2020-03-10 12:59:10 +0100 | [diff] [blame] | 1218 | int *fragments, const int expected_fragments ) |
Piotr Nowicki | c3fca5e | 2020-01-30 15:33:42 +0100 | [diff] [blame] | 1219 | { |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1220 | int ret = mbedtls_ssl_read( ssl, buf + *read, buf_len - *read ); |
| 1221 | if( ret > 0 ) |
Piotr Nowicki | c3fca5e | 2020-01-30 15:33:42 +0100 | [diff] [blame] | 1222 | { |
Piotr Nowicki | 438bf3b | 2020-03-10 12:59:10 +0100 | [diff] [blame] | 1223 | ( *fragments )++; |
Piotr Nowicki | c3fca5e | 2020-01-30 15:33:42 +0100 | [diff] [blame] | 1224 | *read += ret; |
| 1225 | } |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1226 | |
| 1227 | if( expected_fragments == 0 ) |
| 1228 | { |
| 1229 | TEST_ASSERT( ret == 0 ); |
| 1230 | } |
| 1231 | else if( expected_fragments == 1 ) |
| 1232 | { |
| 1233 | TEST_ASSERT( ret == buf_len || |
| 1234 | ret == MBEDTLS_ERR_SSL_WANT_READ || |
| 1235 | ret == MBEDTLS_ERR_SSL_WANT_WRITE ); |
| 1236 | } |
| 1237 | else |
| 1238 | { |
| 1239 | TEST_ASSERT( expected_fragments > 1 ); |
| 1240 | TEST_ASSERT( ( ret >= 0 && ret <= buf_len ) || |
| 1241 | ret == MBEDTLS_ERR_SSL_WANT_READ || |
| 1242 | ret == MBEDTLS_ERR_SSL_WANT_WRITE ); |
| 1243 | } |
| 1244 | |
| 1245 | return 0; |
| 1246 | |
| 1247 | exit: |
| 1248 | /* Some of the tests failed */ |
| 1249 | return -1; |
Piotr Nowicki | c3fca5e | 2020-01-30 15:33:42 +0100 | [diff] [blame] | 1250 | } |
| 1251 | |
| 1252 | /* |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1253 | * Helper function setting up inverse record transformations |
| 1254 | * using given cipher, hash, EtM mode, authentication tag length, |
| 1255 | * and version. |
| 1256 | */ |
| 1257 | |
| 1258 | #define CHK( x ) \ |
| 1259 | do \ |
| 1260 | { \ |
| 1261 | if( !( x ) ) \ |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 1262 | { \ |
Hanno Becker | a5780f1 | 2019-04-05 09:55:37 +0100 | [diff] [blame] | 1263 | ret = -1; \ |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 1264 | goto cleanup; \ |
| 1265 | } \ |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1266 | } while( 0 ) |
| 1267 | |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 1268 | void set_ciphersuite( mbedtls_ssl_config *conf, const char *cipher, |
| 1269 | int* forced_ciphersuite ) |
| 1270 | { |
| 1271 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info; |
| 1272 | forced_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id( cipher ); |
| 1273 | forced_ciphersuite[1] = 0; |
| 1274 | |
| 1275 | ciphersuite_info = |
| 1276 | mbedtls_ssl_ciphersuite_from_id( forced_ciphersuite[0] ); |
| 1277 | |
| 1278 | TEST_ASSERT( ciphersuite_info != NULL ); |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 1279 | TEST_ASSERT( ciphersuite_info->min_tls_version <= conf->max_tls_version ); |
| 1280 | TEST_ASSERT( ciphersuite_info->max_tls_version >= conf->min_tls_version ); |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 1281 | |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 1282 | if( conf->max_tls_version > ciphersuite_info->max_tls_version ) |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 1283 | { |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 1284 | conf->max_tls_version = ciphersuite_info->max_tls_version; |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 1285 | } |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 1286 | if( conf->min_tls_version < ciphersuite_info->min_tls_version ) |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 1287 | { |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 1288 | conf->min_tls_version = ciphersuite_info->min_tls_version; |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 1289 | } |
| 1290 | |
| 1291 | mbedtls_ssl_conf_ciphersuites( conf, forced_ciphersuite ); |
| 1292 | |
| 1293 | exit: |
| 1294 | return; |
| 1295 | } |
| 1296 | |
Andrzej Kurek | cc5169c | 2020-02-04 09:04:56 -0500 | [diff] [blame] | 1297 | int psk_dummy_callback( void *p_info, mbedtls_ssl_context *ssl, |
| 1298 | const unsigned char *name, size_t name_len ) |
| 1299 | { |
| 1300 | (void) p_info; |
| 1301 | (void) ssl; |
| 1302 | (void) name; |
| 1303 | (void) name_len; |
| 1304 | |
| 1305 | return ( 0 ); |
| 1306 | } |
| 1307 | |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 1308 | #if MBEDTLS_SSL_CID_OUT_LEN_MAX > MBEDTLS_SSL_CID_IN_LEN_MAX |
| 1309 | #define SSL_CID_LEN_MIN MBEDTLS_SSL_CID_IN_LEN_MAX |
| 1310 | #else |
| 1311 | #define SSL_CID_LEN_MIN MBEDTLS_SSL_CID_OUT_LEN_MAX |
| 1312 | #endif |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1313 | |
Gilles Peskine | 9c656ec | 2022-02-26 19:55:58 +0100 | [diff] [blame] | 1314 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
| 1315 | defined(MBEDTLS_CIPHER_MODE_CBC) && defined(MBEDTLS_AES_C) |
Przemyslaw Stekiel | f4facef | 2022-02-02 21:31:04 +0100 | [diff] [blame] | 1316 | static int psa_cipher_encrypt_helper( mbedtls_ssl_transform *transform, |
| 1317 | const unsigned char *iv, size_t iv_len, |
| 1318 | const unsigned char *input, size_t ilen, |
| 1319 | unsigned char *output, size_t *olen ) |
| 1320 | { |
Przemyslaw Stekiel | 5648d57 | 2022-02-03 14:09:02 +0100 | [diff] [blame] | 1321 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Przemyslaw Stekiel | d66387f | 2022-02-03 08:55:33 +0100 | [diff] [blame] | 1322 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Przemyslaw Stekiel | f4facef | 2022-02-02 21:31:04 +0100 | [diff] [blame] | 1323 | psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT; |
| 1324 | size_t part_len; |
| 1325 | |
| 1326 | status = psa_cipher_encrypt_setup( &cipher_op, |
| 1327 | transform->psa_key_enc, transform->psa_alg ); |
| 1328 | |
| 1329 | if( status != PSA_SUCCESS ) |
| 1330 | return( psa_ssl_status_to_mbedtls( status ) ); |
| 1331 | |
| 1332 | status = psa_cipher_set_iv( &cipher_op, iv, iv_len ); |
| 1333 | |
| 1334 | if( status != PSA_SUCCESS ) |
| 1335 | return( psa_ssl_status_to_mbedtls( status ) ); |
| 1336 | |
| 1337 | status = psa_cipher_update( &cipher_op, |
| 1338 | input, ilen, output, ilen, olen ); |
| 1339 | |
| 1340 | if( status != PSA_SUCCESS ) |
| 1341 | return( psa_ssl_status_to_mbedtls( status ) ); |
| 1342 | |
| 1343 | status = psa_cipher_finish( &cipher_op, |
| 1344 | output + *olen, ilen - *olen, &part_len ); |
| 1345 | |
| 1346 | if( status != PSA_SUCCESS ) |
| 1347 | return( psa_ssl_status_to_mbedtls( status ) ); |
| 1348 | |
| 1349 | *olen += part_len; |
| 1350 | return( 0 ); |
Przemyslaw Stekiel | 5648d57 | 2022-02-03 14:09:02 +0100 | [diff] [blame] | 1351 | #else |
| 1352 | return mbedtls_cipher_crypt( &transform->cipher_ctx_enc, |
| 1353 | iv, iv_len, input, ilen, output, olen ); |
Przemyslaw Stekiel | f4facef | 2022-02-02 21:31:04 +0100 | [diff] [blame] | 1354 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Przemyslaw Stekiel | 5648d57 | 2022-02-03 14:09:02 +0100 | [diff] [blame] | 1355 | } |
Gilles Peskine | 9c656ec | 2022-02-26 19:55:58 +0100 | [diff] [blame] | 1356 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_CIPHER_MODE_CBC && MBEDTLS_AES_C */ |
Przemyslaw Stekiel | f4facef | 2022-02-02 21:31:04 +0100 | [diff] [blame] | 1357 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1358 | static int build_transforms( mbedtls_ssl_transform *t_in, |
| 1359 | mbedtls_ssl_transform *t_out, |
| 1360 | int cipher_type, int hash_id, |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 1361 | int etm, int tag_mode, |
| 1362 | mbedtls_ssl_protocol_version tls_version, |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 1363 | size_t cid0_len, |
| 1364 | size_t cid1_len ) |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1365 | { |
| 1366 | mbedtls_cipher_info_t const *cipher_info; |
Hanno Becker | a5780f1 | 2019-04-05 09:55:37 +0100 | [diff] [blame] | 1367 | int ret = 0; |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1368 | |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1369 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1370 | psa_key_type_t key_type; |
| 1371 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1372 | psa_algorithm_t alg; |
| 1373 | size_t key_bits; |
Przemyslaw Stekiel | d66387f | 2022-02-03 08:55:33 +0100 | [diff] [blame] | 1374 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1375 | #endif |
| 1376 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1377 | size_t keylen, maclen, ivlen; |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 1378 | unsigned char *key0 = NULL, *key1 = NULL; |
Paul Elliott | 6f1eda7 | 2020-06-11 20:22:00 +0100 | [diff] [blame] | 1379 | unsigned char *md0 = NULL, *md1 = NULL; |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1380 | unsigned char iv_enc[16], iv_dec[16]; |
| 1381 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1382 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 1383 | unsigned char cid0[ SSL_CID_LEN_MIN ]; |
| 1384 | unsigned char cid1[ SSL_CID_LEN_MIN ]; |
| 1385 | |
Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 1386 | mbedtls_test_rnd_std_rand( NULL, cid0, sizeof( cid0 ) ); |
| 1387 | mbedtls_test_rnd_std_rand( NULL, cid1, sizeof( cid1 ) ); |
Hanno Becker | 43c24b8 | 2019-05-01 09:45:57 +0100 | [diff] [blame] | 1388 | #else |
| 1389 | ((void) cid0_len); |
| 1390 | ((void) cid1_len); |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1391 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 1392 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1393 | maclen = 0; |
| 1394 | |
| 1395 | /* Pick cipher */ |
| 1396 | cipher_info = mbedtls_cipher_info_from_type( cipher_type ); |
| 1397 | CHK( cipher_info != NULL ); |
| 1398 | CHK( cipher_info->iv_size <= 16 ); |
| 1399 | CHK( cipher_info->key_bitlen % 8 == 0 ); |
| 1400 | |
| 1401 | /* Pick keys */ |
| 1402 | keylen = cipher_info->key_bitlen / 8; |
Hanno Becker | 78d1f70 | 2019-04-05 09:56:10 +0100 | [diff] [blame] | 1403 | /* Allocate `keylen + 1` bytes to ensure that we get |
| 1404 | * a non-NULL pointers from `mbedtls_calloc` even if |
| 1405 | * `keylen == 0` in the case of the NULL cipher. */ |
| 1406 | CHK( ( key0 = mbedtls_calloc( 1, keylen + 1 ) ) != NULL ); |
| 1407 | CHK( ( key1 = mbedtls_calloc( 1, keylen + 1 ) ) != NULL ); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1408 | memset( key0, 0x1, keylen ); |
| 1409 | memset( key1, 0x2, keylen ); |
| 1410 | |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1411 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1412 | /* Setup cipher contexts */ |
| 1413 | CHK( mbedtls_cipher_setup( &t_in->cipher_ctx_enc, cipher_info ) == 0 ); |
| 1414 | CHK( mbedtls_cipher_setup( &t_in->cipher_ctx_dec, cipher_info ) == 0 ); |
| 1415 | CHK( mbedtls_cipher_setup( &t_out->cipher_ctx_enc, cipher_info ) == 0 ); |
| 1416 | CHK( mbedtls_cipher_setup( &t_out->cipher_ctx_dec, cipher_info ) == 0 ); |
| 1417 | |
| 1418 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
| 1419 | if( cipher_info->mode == MBEDTLS_MODE_CBC ) |
| 1420 | { |
| 1421 | CHK( mbedtls_cipher_set_padding_mode( &t_in->cipher_ctx_enc, |
| 1422 | MBEDTLS_PADDING_NONE ) == 0 ); |
| 1423 | CHK( mbedtls_cipher_set_padding_mode( &t_in->cipher_ctx_dec, |
| 1424 | MBEDTLS_PADDING_NONE ) == 0 ); |
| 1425 | CHK( mbedtls_cipher_set_padding_mode( &t_out->cipher_ctx_enc, |
| 1426 | MBEDTLS_PADDING_NONE ) == 0 ); |
| 1427 | CHK( mbedtls_cipher_set_padding_mode( &t_out->cipher_ctx_dec, |
| 1428 | MBEDTLS_PADDING_NONE ) == 0 ); |
| 1429 | } |
| 1430 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
| 1431 | |
| 1432 | CHK( mbedtls_cipher_setkey( &t_in->cipher_ctx_enc, key0, |
| 1433 | keylen << 3, MBEDTLS_ENCRYPT ) == 0 ); |
| 1434 | CHK( mbedtls_cipher_setkey( &t_in->cipher_ctx_dec, key1, |
| 1435 | keylen << 3, MBEDTLS_DECRYPT ) == 0 ); |
| 1436 | CHK( mbedtls_cipher_setkey( &t_out->cipher_ctx_enc, key1, |
| 1437 | keylen << 3, MBEDTLS_ENCRYPT ) == 0 ); |
| 1438 | CHK( mbedtls_cipher_setkey( &t_out->cipher_ctx_dec, key0, |
| 1439 | keylen << 3, MBEDTLS_DECRYPT ) == 0 ); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1440 | #endif |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1441 | |
| 1442 | /* Setup MAC contexts */ |
Hanno Becker | fd86ca8 | 2020-11-30 08:54:23 +0000 | [diff] [blame] | 1443 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1444 | if( cipher_info->mode == MBEDTLS_MODE_CBC || |
| 1445 | cipher_info->mode == MBEDTLS_MODE_STREAM ) |
| 1446 | { |
| 1447 | mbedtls_md_info_t const *md_info; |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1448 | |
| 1449 | /* Pick hash */ |
| 1450 | md_info = mbedtls_md_info_from_type( hash_id ); |
| 1451 | CHK( md_info != NULL ); |
| 1452 | |
| 1453 | /* Pick hash keys */ |
| 1454 | maclen = mbedtls_md_get_size( md_info ); |
Hanno Becker | 3ee5421 | 2019-04-04 16:31:26 +0100 | [diff] [blame] | 1455 | CHK( ( md0 = mbedtls_calloc( 1, maclen ) ) != NULL ); |
| 1456 | CHK( ( md1 = mbedtls_calloc( 1, maclen ) ) != NULL ); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1457 | memset( md0, 0x5, maclen ); |
| 1458 | memset( md1, 0x6, maclen ); |
| 1459 | |
Neil Armstrong | f4cc062 | 2022-02-23 16:21:01 +0100 | [diff] [blame] | 1460 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1461 | alg = mbedtls_psa_translate_md( mbedtls_md_get_type( md_info ) ); |
| 1462 | |
| 1463 | CHK( alg != 0 ); |
| 1464 | |
| 1465 | t_out->psa_mac_alg = PSA_ALG_HMAC( alg ); |
| 1466 | t_in->psa_mac_alg = PSA_ALG_HMAC( alg ); |
| 1467 | t_in->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT; |
| 1468 | t_out->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT; |
| 1469 | t_in->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT; |
| 1470 | t_out->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT; |
| 1471 | |
| 1472 | psa_reset_key_attributes( &attributes ); |
| 1473 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE ); |
| 1474 | psa_set_key_algorithm( &attributes, PSA_ALG_HMAC( alg ) ); |
| 1475 | psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC ); |
| 1476 | |
| 1477 | CHK( psa_import_key( &attributes, |
| 1478 | md0, maclen, |
| 1479 | &t_in->psa_mac_enc ) == PSA_SUCCESS ); |
| 1480 | |
| 1481 | CHK( psa_import_key( &attributes, |
| 1482 | md1, maclen, |
| 1483 | &t_out->psa_mac_enc ) == PSA_SUCCESS ); |
| 1484 | |
Neil Armstrong | 8f92bf3 | 2022-03-18 09:56:57 +0100 | [diff] [blame] | 1485 | if( cipher_info->mode == MBEDTLS_MODE_STREAM || |
| 1486 | etm == MBEDTLS_SSL_ETM_DISABLED ) |
| 1487 | /* mbedtls_ct_hmac() requires the key to be exportable */ |
| 1488 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT | |
| 1489 | PSA_KEY_USAGE_VERIFY_HASH ); |
| 1490 | else |
| 1491 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Neil Armstrong | f4cc062 | 2022-02-23 16:21:01 +0100 | [diff] [blame] | 1492 | |
| 1493 | CHK( psa_import_key( &attributes, |
| 1494 | md1, maclen, |
| 1495 | &t_in->psa_mac_dec ) == PSA_SUCCESS ); |
| 1496 | |
| 1497 | CHK( psa_import_key( &attributes, |
| 1498 | md0, maclen, |
| 1499 | &t_out->psa_mac_dec ) == PSA_SUCCESS ); |
Neil Armstrong | cf8841a | 2022-02-24 11:17:45 +0100 | [diff] [blame] | 1500 | #else |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1501 | CHK( mbedtls_md_setup( &t_out->md_ctx_enc, md_info, 1 ) == 0 ); |
| 1502 | CHK( mbedtls_md_setup( &t_out->md_ctx_dec, md_info, 1 ) == 0 ); |
| 1503 | CHK( mbedtls_md_setup( &t_in->md_ctx_enc, md_info, 1 ) == 0 ); |
| 1504 | CHK( mbedtls_md_setup( &t_in->md_ctx_dec, md_info, 1 ) == 0 ); |
| 1505 | |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 1506 | CHK( mbedtls_md_hmac_starts( &t_in->md_ctx_enc, |
| 1507 | md0, maclen ) == 0 ); |
| 1508 | CHK( mbedtls_md_hmac_starts( &t_in->md_ctx_dec, |
| 1509 | md1, maclen ) == 0 ); |
| 1510 | CHK( mbedtls_md_hmac_starts( &t_out->md_ctx_enc, |
| 1511 | md1, maclen ) == 0 ); |
| 1512 | CHK( mbedtls_md_hmac_starts( &t_out->md_ctx_dec, |
| 1513 | md0, maclen ) == 0 ); |
Neil Armstrong | cf8841a | 2022-02-24 11:17:45 +0100 | [diff] [blame] | 1514 | #endif |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1515 | } |
| 1516 | #else |
| 1517 | ((void) hash_id); |
Hanno Becker | fd86ca8 | 2020-11-30 08:54:23 +0000 | [diff] [blame] | 1518 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1519 | |
| 1520 | |
| 1521 | /* Pick IV's (regardless of whether they |
| 1522 | * are being used by the transform). */ |
| 1523 | ivlen = cipher_info->iv_size; |
| 1524 | memset( iv_enc, 0x3, sizeof( iv_enc ) ); |
| 1525 | memset( iv_dec, 0x4, sizeof( iv_dec ) ); |
| 1526 | |
| 1527 | /* |
| 1528 | * Setup transforms |
| 1529 | */ |
| 1530 | |
Jaeden Amero | 2de07f1 | 2019-06-05 13:32:08 +0100 | [diff] [blame] | 1531 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \ |
Hanno Becker | fd86ca8 | 2020-11-30 08:54:23 +0000 | [diff] [blame] | 1532 | defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1533 | t_out->encrypt_then_mac = etm; |
| 1534 | t_in->encrypt_then_mac = etm; |
| 1535 | #else |
| 1536 | ((void) etm); |
| 1537 | #endif |
| 1538 | |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 1539 | t_out->tls_version = tls_version; |
| 1540 | t_in->tls_version = tls_version; |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1541 | t_out->ivlen = ivlen; |
| 1542 | t_in->ivlen = ivlen; |
| 1543 | |
| 1544 | switch( cipher_info->mode ) |
| 1545 | { |
| 1546 | case MBEDTLS_MODE_GCM: |
| 1547 | case MBEDTLS_MODE_CCM: |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1548 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 1549 | if( tls_version == MBEDTLS_SSL_VERSION_TLS1_3 ) |
Hanno Becker | e683287 | 2020-05-28 08:29:58 +0100 | [diff] [blame] | 1550 | { |
| 1551 | t_out->fixed_ivlen = 12; |
| 1552 | t_in->fixed_ivlen = 12; |
| 1553 | } |
| 1554 | else |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1555 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | e683287 | 2020-05-28 08:29:58 +0100 | [diff] [blame] | 1556 | { |
| 1557 | t_out->fixed_ivlen = 4; |
| 1558 | t_in->fixed_ivlen = 4; |
| 1559 | } |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1560 | t_out->maclen = 0; |
| 1561 | t_in->maclen = 0; |
| 1562 | switch( tag_mode ) |
| 1563 | { |
| 1564 | case 0: /* Full tag */ |
| 1565 | t_out->taglen = 16; |
| 1566 | t_in->taglen = 16; |
| 1567 | break; |
| 1568 | case 1: /* Partial tag */ |
| 1569 | t_out->taglen = 8; |
| 1570 | t_in->taglen = 8; |
| 1571 | break; |
| 1572 | default: |
Paul Elliott | c7b5374 | 2021-02-03 13:18:33 +0000 | [diff] [blame] | 1573 | ret = 1; |
| 1574 | goto cleanup; |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1575 | } |
| 1576 | break; |
| 1577 | |
| 1578 | case MBEDTLS_MODE_CHACHAPOLY: |
| 1579 | t_out->fixed_ivlen = 12; |
| 1580 | t_in->fixed_ivlen = 12; |
| 1581 | t_out->maclen = 0; |
| 1582 | t_in->maclen = 0; |
| 1583 | switch( tag_mode ) |
| 1584 | { |
| 1585 | case 0: /* Full tag */ |
| 1586 | t_out->taglen = 16; |
| 1587 | t_in->taglen = 16; |
| 1588 | break; |
| 1589 | case 1: /* Partial tag */ |
| 1590 | t_out->taglen = 8; |
| 1591 | t_in->taglen = 8; |
| 1592 | break; |
| 1593 | default: |
Paul Elliott | c7b5374 | 2021-02-03 13:18:33 +0000 | [diff] [blame] | 1594 | ret = 1; |
| 1595 | goto cleanup; |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1596 | } |
| 1597 | break; |
| 1598 | |
| 1599 | case MBEDTLS_MODE_STREAM: |
| 1600 | case MBEDTLS_MODE_CBC: |
| 1601 | t_out->fixed_ivlen = 0; /* redundant, must be 0 */ |
| 1602 | t_in->fixed_ivlen = 0; /* redundant, must be 0 */ |
| 1603 | t_out->taglen = 0; |
| 1604 | t_in->taglen = 0; |
| 1605 | switch( tag_mode ) |
| 1606 | { |
| 1607 | case 0: /* Full tag */ |
| 1608 | t_out->maclen = maclen; |
| 1609 | t_in->maclen = maclen; |
| 1610 | break; |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1611 | default: |
Paul Elliott | c7b5374 | 2021-02-03 13:18:33 +0000 | [diff] [blame] | 1612 | ret = 1; |
| 1613 | goto cleanup; |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1614 | } |
| 1615 | break; |
| 1616 | default: |
Paul Elliott | c7b5374 | 2021-02-03 13:18:33 +0000 | [diff] [blame] | 1617 | ret = 1; |
| 1618 | goto cleanup; |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1619 | break; |
| 1620 | } |
| 1621 | |
| 1622 | /* Setup IV's */ |
| 1623 | |
| 1624 | memcpy( &t_in->iv_dec, iv_dec, sizeof( iv_dec ) ); |
| 1625 | memcpy( &t_in->iv_enc, iv_enc, sizeof( iv_enc ) ); |
| 1626 | memcpy( &t_out->iv_dec, iv_enc, sizeof( iv_enc ) ); |
| 1627 | memcpy( &t_out->iv_enc, iv_dec, sizeof( iv_dec ) ); |
| 1628 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1629 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 1630 | /* Add CID */ |
| 1631 | memcpy( &t_in->in_cid, cid0, cid0_len ); |
| 1632 | memcpy( &t_in->out_cid, cid1, cid1_len ); |
| 1633 | t_in->in_cid_len = cid0_len; |
| 1634 | t_in->out_cid_len = cid1_len; |
| 1635 | memcpy( &t_out->in_cid, cid1, cid1_len ); |
| 1636 | memcpy( &t_out->out_cid, cid0, cid0_len ); |
| 1637 | t_out->in_cid_len = cid1_len; |
| 1638 | t_out->out_cid_len = cid0_len; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1639 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 1640 | |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1641 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Przemyslaw Stekiel | f57b456 | 2022-01-25 00:04:18 +0100 | [diff] [blame] | 1642 | status = mbedtls_ssl_cipher_to_psa( cipher_type, |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1643 | t_in->taglen, |
| 1644 | &alg, |
| 1645 | &key_type, |
| 1646 | &key_bits ); |
| 1647 | |
Przemyslaw Stekiel | 8c010eb | 2022-02-03 10:44:02 +0100 | [diff] [blame] | 1648 | if ( status != PSA_SUCCESS ) |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1649 | { |
Przemyslaw Stekiel | 77aec8d | 2022-01-31 20:22:53 +0100 | [diff] [blame] | 1650 | ret = psa_ssl_status_to_mbedtls( status ); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1651 | goto cleanup; |
| 1652 | } |
| 1653 | |
| 1654 | t_in->psa_alg = alg; |
| 1655 | t_out->psa_alg = alg; |
| 1656 | |
| 1657 | if ( alg != MBEDTLS_SSL_NULL_CIPHER ) |
| 1658 | { |
Neil Armstrong | f4cc062 | 2022-02-23 16:21:01 +0100 | [diff] [blame] | 1659 | psa_reset_key_attributes( &attributes ); |
Przemyslaw Stekiel | f4ca3f0 | 2022-01-25 00:25:59 +0100 | [diff] [blame] | 1660 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1661 | psa_set_key_algorithm( &attributes, alg ); |
| 1662 | psa_set_key_type( &attributes, key_type ); |
| 1663 | |
| 1664 | status = psa_import_key( &attributes, |
| 1665 | key0, |
| 1666 | PSA_BITS_TO_BYTES( key_bits ), |
| 1667 | &t_in->psa_key_enc ); |
| 1668 | |
Przemyslaw Stekiel | 8c010eb | 2022-02-03 10:44:02 +0100 | [diff] [blame] | 1669 | if ( status != PSA_SUCCESS ) |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1670 | { |
Przemyslaw Stekiel | 77aec8d | 2022-01-31 20:22:53 +0100 | [diff] [blame] | 1671 | ret = psa_ssl_status_to_mbedtls( status ); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1672 | goto cleanup; |
| 1673 | } |
| 1674 | |
| 1675 | status = psa_import_key( &attributes, |
| 1676 | key1, |
| 1677 | PSA_BITS_TO_BYTES( key_bits ), |
Przemyslaw Stekiel | f4ca3f0 | 2022-01-25 00:25:59 +0100 | [diff] [blame] | 1678 | &t_out->psa_key_enc ); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1679 | |
Przemyslaw Stekiel | 8c010eb | 2022-02-03 10:44:02 +0100 | [diff] [blame] | 1680 | if ( status != PSA_SUCCESS ) |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1681 | { |
Przemyslaw Stekiel | 77aec8d | 2022-01-31 20:22:53 +0100 | [diff] [blame] | 1682 | ret = psa_ssl_status_to_mbedtls( status ); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1683 | goto cleanup; |
| 1684 | } |
| 1685 | |
Przemyslaw Stekiel | f4ca3f0 | 2022-01-25 00:25:59 +0100 | [diff] [blame] | 1686 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 1687 | |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1688 | status = psa_import_key( &attributes, |
| 1689 | key1, |
| 1690 | PSA_BITS_TO_BYTES( key_bits ), |
Przemyslaw Stekiel | f4ca3f0 | 2022-01-25 00:25:59 +0100 | [diff] [blame] | 1691 | &t_in->psa_key_dec ); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1692 | |
Przemyslaw Stekiel | 8c010eb | 2022-02-03 10:44:02 +0100 | [diff] [blame] | 1693 | if ( status != PSA_SUCCESS ) |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1694 | { |
Przemyslaw Stekiel | 77aec8d | 2022-01-31 20:22:53 +0100 | [diff] [blame] | 1695 | ret = psa_ssl_status_to_mbedtls( status ); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1696 | goto cleanup; |
| 1697 | } |
| 1698 | |
| 1699 | status = psa_import_key( &attributes, |
| 1700 | key0, |
| 1701 | PSA_BITS_TO_BYTES( key_bits ), |
| 1702 | &t_out->psa_key_dec ); |
| 1703 | |
Przemyslaw Stekiel | 8c010eb | 2022-02-03 10:44:02 +0100 | [diff] [blame] | 1704 | if ( status != PSA_SUCCESS ) |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1705 | { |
Przemyslaw Stekiel | 77aec8d | 2022-01-31 20:22:53 +0100 | [diff] [blame] | 1706 | ret = psa_ssl_status_to_mbedtls( status ); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1707 | goto cleanup; |
| 1708 | } |
| 1709 | } |
| 1710 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1711 | |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 1712 | cleanup: |
| 1713 | |
Hanno Becker | 3ee5421 | 2019-04-04 16:31:26 +0100 | [diff] [blame] | 1714 | mbedtls_free( key0 ); |
| 1715 | mbedtls_free( key1 ); |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 1716 | |
Paul Elliott | 6f1eda7 | 2020-06-11 20:22:00 +0100 | [diff] [blame] | 1717 | mbedtls_free( md0 ); |
| 1718 | mbedtls_free( md1 ); |
| 1719 | |
Hanno Becker | a5780f1 | 2019-04-05 09:55:37 +0100 | [diff] [blame] | 1720 | return( ret ); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1721 | } |
| 1722 | |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1723 | /* |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 1724 | * Populate a session structure for serialization tests. |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1725 | * Choose dummy values, mostly non-0 to distinguish from the init default. |
| 1726 | */ |
Jerry Yu | 534ff40 | 2022-07-14 16:43:43 +0800 | [diff] [blame] | 1727 | static int ssl_tls12_populate_session( mbedtls_ssl_session *session, |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 1728 | int ticket_len, |
| 1729 | const char *crt_file ) |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1730 | { |
| 1731 | #if defined(MBEDTLS_HAVE_TIME) |
| 1732 | session->start = mbedtls_time( NULL ) - 42; |
| 1733 | #endif |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 1734 | session->tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1735 | session->ciphersuite = 0xabcd; |
| 1736 | session->compression = 1; |
| 1737 | session->id_len = sizeof( session->id ); |
| 1738 | memset( session->id, 66, session->id_len ); |
Manuel Pégourié-Gonnard | 220403b | 2019-05-24 09:54:21 +0200 | [diff] [blame] | 1739 | memset( session->master, 17, sizeof( session->master ) ); |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1740 | |
Manuel Pégourié-Gonnard | 1f6033a | 2019-05-24 10:17:52 +0200 | [diff] [blame] | 1741 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_FS_IO) |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 1742 | if( crt_file != NULL && strlen( crt_file ) != 0 ) |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1743 | { |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 1744 | mbedtls_x509_crt tmp_crt; |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1745 | int ret; |
Manuel Pégourié-Gonnard | 6b84070 | 2019-05-24 09:40:17 +0200 | [diff] [blame] | 1746 | |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 1747 | mbedtls_x509_crt_init( &tmp_crt ); |
| 1748 | ret = mbedtls_x509_crt_parse_file( &tmp_crt, crt_file ); |
| 1749 | if( ret != 0 ) |
| 1750 | return( ret ); |
| 1751 | |
| 1752 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 1753 | /* Move temporary CRT. */ |
Manuel Pégourié-Gonnard | 6b84070 | 2019-05-24 09:40:17 +0200 | [diff] [blame] | 1754 | session->peer_cert = mbedtls_calloc( 1, sizeof( *session->peer_cert ) ); |
| 1755 | if( session->peer_cert == NULL ) |
| 1756 | return( -1 ); |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 1757 | *session->peer_cert = tmp_crt; |
| 1758 | memset( &tmp_crt, 0, sizeof( tmp_crt ) ); |
| 1759 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 1760 | /* Calculate digest of temporary CRT. */ |
| 1761 | session->peer_cert_digest = |
| 1762 | mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ); |
| 1763 | if( session->peer_cert_digest == NULL ) |
| 1764 | return( -1 ); |
| 1765 | ret = mbedtls_md( mbedtls_md_info_from_type( |
| 1766 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ), |
| 1767 | tmp_crt.raw.p, tmp_crt.raw.len, |
| 1768 | session->peer_cert_digest ); |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1769 | if( ret != 0 ) |
| 1770 | return( ret ); |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 1771 | session->peer_cert_digest_type = |
| 1772 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE; |
| 1773 | session->peer_cert_digest_len = |
| 1774 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN; |
| 1775 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 1776 | |
| 1777 | mbedtls_x509_crt_free( &tmp_crt ); |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1778 | } |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 1779 | #else /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_FS_IO */ |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1780 | (void) crt_file; |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 1781 | #endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_FS_IO */ |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1782 | session->verify_result = 0xdeadbeef; |
| 1783 | |
| 1784 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 1785 | if( ticket_len != 0 ) |
| 1786 | { |
| 1787 | session->ticket = mbedtls_calloc( 1, ticket_len ); |
Manuel Pégourié-Gonnard | 220403b | 2019-05-24 09:54:21 +0200 | [diff] [blame] | 1788 | if( session->ticket == NULL ) |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1789 | return( -1 ); |
| 1790 | memset( session->ticket, 33, ticket_len ); |
| 1791 | } |
| 1792 | session->ticket_len = ticket_len; |
| 1793 | session->ticket_lifetime = 86401; |
| 1794 | #else |
| 1795 | (void) ticket_len; |
| 1796 | #endif |
| 1797 | |
| 1798 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 1799 | session->mfl_code = 1; |
| 1800 | #endif |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1801 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 1802 | session->encrypt_then_mac = 1; |
| 1803 | #endif |
| 1804 | |
| 1805 | return( 0 ); |
| 1806 | } |
| 1807 | |
Jerry Yu | 534ff40 | 2022-07-14 16:43:43 +0800 | [diff] [blame] | 1808 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 1809 | static int ssl_tls13_populate_session( mbedtls_ssl_session *session, |
| 1810 | int ticket_len, |
| 1811 | int endpoint_type ) |
| 1812 | { |
| 1813 | ((void) ticket_len); |
| 1814 | session->tls_version = MBEDTLS_SSL_VERSION_TLS1_3; |
| 1815 | session->endpoint = endpoint_type == MBEDTLS_SSL_IS_CLIENT ? |
| 1816 | MBEDTLS_SSL_IS_CLIENT : MBEDTLS_SSL_IS_SERVER; |
| 1817 | session->ciphersuite = 0xabcd; |
| 1818 | session->ticket_age_add = 0x87654321; |
| 1819 | session->ticket_flags = 0x7; |
| 1820 | |
| 1821 | session->key_len = 32; |
| 1822 | memset( session->key, 0x99, sizeof( session->key ) ); |
| 1823 | |
| 1824 | #if defined(MBEDTLS_HAVE_TIME) |
| 1825 | if( session->endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 1826 | { |
| 1827 | session->start = mbedtls_time( NULL ) - 42; |
| 1828 | } |
| 1829 | #endif |
| 1830 | |
| 1831 | #if defined(MBEDTLS_SSL_CLI_C) |
| 1832 | if( session->endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 1833 | { |
| 1834 | #if defined(MBEDTLS_HAVE_TIME) |
| 1835 | session->ticket_received = mbedtls_time( NULL ) - 40; |
| 1836 | #endif |
| 1837 | session->ticket_lifetime = 0xfedcba98; |
| 1838 | |
| 1839 | session->ticket_len = ticket_len; |
| 1840 | if( ticket_len != 0 ) |
| 1841 | { |
| 1842 | session->ticket = mbedtls_calloc( 1, ticket_len ); |
| 1843 | if( session->ticket == NULL ) |
| 1844 | return( -1 ); |
| 1845 | memset( session->ticket, 33, ticket_len ); |
| 1846 | } |
| 1847 | } |
| 1848 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 1849 | |
| 1850 | return( 0 ); |
| 1851 | } |
| 1852 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 1853 | |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1854 | /* |
| 1855 | * Perform data exchanging between \p ssl_1 and \p ssl_2 and check if the |
| 1856 | * message was sent in the correct number of fragments. |
| 1857 | * |
| 1858 | * /p ssl_1 and /p ssl_2 Endpoints represented by mbedtls_ssl_context. Both |
| 1859 | * of them must be initialized and connected beforehand. |
| 1860 | * /p msg_len_1 and /p msg_len_2 specify the size of the message to send. |
| 1861 | * /p expected_fragments_1 and /p expected_fragments_2 determine in how many |
| 1862 | * fragments the message should be sent. |
| 1863 | * expected_fragments is 0: can be used for DTLS testing while the message |
| 1864 | * size is larger than MFL. In that case the message |
| 1865 | * cannot be fragmented and sent to the second endpoint. |
| 1866 | * This value can be used for negative tests. |
| 1867 | * expected_fragments is 1: can be used for TLS/DTLS testing while the |
| 1868 | * message size is below MFL |
| 1869 | * expected_fragments > 1: can be used for TLS testing while the message |
| 1870 | * size is larger than MFL |
| 1871 | * |
| 1872 | * \retval 0 on success, otherwise error code. |
| 1873 | */ |
| 1874 | int mbedtls_exchange_data( mbedtls_ssl_context *ssl_1, |
| 1875 | int msg_len_1, const int expected_fragments_1, |
| 1876 | mbedtls_ssl_context *ssl_2, |
| 1877 | int msg_len_2, const int expected_fragments_2 ) |
| 1878 | { |
| 1879 | unsigned char *msg_buf_1 = malloc( msg_len_1 ); |
| 1880 | unsigned char *msg_buf_2 = malloc( msg_len_2 ); |
| 1881 | unsigned char *in_buf_1 = malloc( msg_len_2 ); |
| 1882 | unsigned char *in_buf_2 = malloc( msg_len_1 ); |
| 1883 | int msg_type, ret = -1; |
| 1884 | |
| 1885 | /* Perform this test with two message types. At first use a message |
| 1886 | * consisting of only 0x00 for the client and only 0xFF for the server. |
| 1887 | * At the second time use message with generated data */ |
| 1888 | for( msg_type = 0; msg_type < 2; msg_type++ ) |
| 1889 | { |
| 1890 | int written_1 = 0; |
| 1891 | int written_2 = 0; |
| 1892 | int read_1 = 0; |
| 1893 | int read_2 = 0; |
| 1894 | int fragments_1 = 0; |
| 1895 | int fragments_2 = 0; |
| 1896 | |
| 1897 | if( msg_type == 0 ) |
| 1898 | { |
| 1899 | memset( msg_buf_1, 0x00, msg_len_1 ); |
| 1900 | memset( msg_buf_2, 0xff, msg_len_2 ); |
| 1901 | } |
| 1902 | else |
| 1903 | { |
| 1904 | int i, j = 0; |
| 1905 | for( i = 0; i < msg_len_1; i++ ) |
| 1906 | { |
| 1907 | msg_buf_1[i] = j++ & 0xFF; |
| 1908 | } |
| 1909 | for( i = 0; i < msg_len_2; i++ ) |
| 1910 | { |
| 1911 | msg_buf_2[i] = ( j -= 5 ) & 0xFF; |
| 1912 | } |
| 1913 | } |
| 1914 | |
| 1915 | while( read_1 < msg_len_2 || read_2 < msg_len_1 ) |
| 1916 | { |
| 1917 | /* ssl_1 sending */ |
| 1918 | if( msg_len_1 > written_1 ) |
| 1919 | { |
| 1920 | ret = mbedtls_ssl_write_fragment( ssl_1, msg_buf_1, |
| 1921 | msg_len_1, &written_1, |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1922 | expected_fragments_1 ); |
| 1923 | if( expected_fragments_1 == 0 ) |
| 1924 | { |
| 1925 | /* This error is expected when the message is too large and |
| 1926 | * cannot be fragmented */ |
| 1927 | TEST_ASSERT( ret == MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1928 | msg_len_1 = 0; |
| 1929 | } |
| 1930 | else |
| 1931 | { |
| 1932 | TEST_ASSERT( ret == 0 ); |
| 1933 | } |
| 1934 | } |
| 1935 | |
| 1936 | /* ssl_2 sending */ |
| 1937 | if( msg_len_2 > written_2 ) |
| 1938 | { |
| 1939 | ret = mbedtls_ssl_write_fragment( ssl_2, msg_buf_2, |
| 1940 | msg_len_2, &written_2, |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1941 | expected_fragments_2 ); |
| 1942 | if( expected_fragments_2 == 0 ) |
| 1943 | { |
| 1944 | /* This error is expected when the message is too large and |
| 1945 | * cannot be fragmented */ |
| 1946 | TEST_ASSERT( ret == MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1947 | msg_len_2 = 0; |
| 1948 | } |
| 1949 | else |
| 1950 | { |
| 1951 | TEST_ASSERT( ret == 0 ); |
| 1952 | } |
| 1953 | } |
| 1954 | |
| 1955 | /* ssl_1 reading */ |
| 1956 | if( read_1 < msg_len_2 ) |
| 1957 | { |
| 1958 | ret = mbedtls_ssl_read_fragment( ssl_1, in_buf_1, |
| 1959 | msg_len_2, &read_1, |
Piotr Nowicki | 438bf3b | 2020-03-10 12:59:10 +0100 | [diff] [blame] | 1960 | &fragments_2, |
| 1961 | expected_fragments_2 ); |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1962 | TEST_ASSERT( ret == 0 ); |
| 1963 | } |
| 1964 | |
| 1965 | /* ssl_2 reading */ |
| 1966 | if( read_2 < msg_len_1 ) |
| 1967 | { |
| 1968 | ret = mbedtls_ssl_read_fragment( ssl_2, in_buf_2, |
| 1969 | msg_len_1, &read_2, |
Piotr Nowicki | 438bf3b | 2020-03-10 12:59:10 +0100 | [diff] [blame] | 1970 | &fragments_1, |
| 1971 | expected_fragments_1 ); |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1972 | TEST_ASSERT( ret == 0 ); |
| 1973 | } |
| 1974 | } |
| 1975 | |
| 1976 | ret = -1; |
| 1977 | TEST_ASSERT( 0 == memcmp( msg_buf_1, in_buf_2, msg_len_1 ) ); |
| 1978 | TEST_ASSERT( 0 == memcmp( msg_buf_2, in_buf_1, msg_len_2 ) ); |
| 1979 | TEST_ASSERT( fragments_1 == expected_fragments_1 ); |
| 1980 | TEST_ASSERT( fragments_2 == expected_fragments_2 ); |
| 1981 | } |
| 1982 | |
| 1983 | ret = 0; |
| 1984 | |
| 1985 | exit: |
| 1986 | free( msg_buf_1 ); |
| 1987 | free( in_buf_1 ); |
| 1988 | free( msg_buf_2 ); |
| 1989 | free( in_buf_2 ); |
| 1990 | |
| 1991 | return ret; |
| 1992 | } |
| 1993 | |
Piotr Nowicki | 95e9eb8 | 2020-02-14 11:33:34 +0100 | [diff] [blame] | 1994 | /* |
| 1995 | * Perform data exchanging between \p ssl_1 and \p ssl_2. Both of endpoints |
| 1996 | * must be initialized and connected beforehand. |
| 1997 | * |
| 1998 | * \retval 0 on success, otherwise error code. |
| 1999 | */ |
| 2000 | int exchange_data( mbedtls_ssl_context *ssl_1, |
| 2001 | mbedtls_ssl_context *ssl_2 ) |
| 2002 | { |
| 2003 | return mbedtls_exchange_data( ssl_1, 256, 1, |
| 2004 | ssl_2, 256, 1 ); |
| 2005 | } |
| 2006 | |
Glenn Strauss | 39e624c | 2022-04-11 13:33:16 -0400 | [diff] [blame] | 2007 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 2008 | defined(MBEDTLS_ENTROPY_C) && \ |
| 2009 | defined(MBEDTLS_CTR_DRBG_C) |
| 2010 | static int check_ssl_version( mbedtls_ssl_protocol_version expected_negotiated_version, |
| 2011 | const mbedtls_ssl_context *ssl ) |
Gilles Peskine | 1255b0d | 2022-01-13 01:08:48 +0100 | [diff] [blame] | 2012 | { |
| 2013 | const char *version_string = mbedtls_ssl_get_version( ssl ); |
| 2014 | mbedtls_ssl_protocol_version version_number = |
| 2015 | mbedtls_ssl_get_version_number( ssl ); |
| 2016 | |
Glenn Strauss | e3af4cb | 2022-03-15 03:23:42 -0400 | [diff] [blame] | 2017 | TEST_EQUAL( ssl->tls_version, expected_negotiated_version ); |
Gilles Peskine | 1255b0d | 2022-01-13 01:08:48 +0100 | [diff] [blame] | 2018 | |
| 2019 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 2020 | { |
| 2021 | TEST_EQUAL( version_string[0], 'D' ); |
| 2022 | ++version_string; |
| 2023 | } |
| 2024 | |
| 2025 | switch( expected_negotiated_version ) |
| 2026 | { |
Glenn Strauss | e3af4cb | 2022-03-15 03:23:42 -0400 | [diff] [blame] | 2027 | case MBEDTLS_SSL_VERSION_TLS1_2: |
Glenn Strauss | dff8462 | 2022-03-14 11:12:57 -0400 | [diff] [blame] | 2028 | TEST_EQUAL( version_number, MBEDTLS_SSL_VERSION_TLS1_2 ); |
Gilles Peskine | 1255b0d | 2022-01-13 01:08:48 +0100 | [diff] [blame] | 2029 | TEST_ASSERT( strcmp( version_string, "TLSv1.2" ) == 0 ); |
| 2030 | break; |
| 2031 | |
Glenn Strauss | e3af4cb | 2022-03-15 03:23:42 -0400 | [diff] [blame] | 2032 | case MBEDTLS_SSL_VERSION_TLS1_3: |
Glenn Strauss | dff8462 | 2022-03-14 11:12:57 -0400 | [diff] [blame] | 2033 | TEST_EQUAL( version_number, MBEDTLS_SSL_VERSION_TLS1_3 ); |
Gilles Peskine | 1255b0d | 2022-01-13 01:08:48 +0100 | [diff] [blame] | 2034 | TEST_ASSERT( strcmp( version_string, "TLSv1.3" ) == 0 ); |
| 2035 | break; |
| 2036 | |
| 2037 | default: |
| 2038 | TEST_ASSERT( ! "Version check not implemented for this protocol version" ); |
| 2039 | } |
| 2040 | |
| 2041 | return( 1 ); |
| 2042 | |
| 2043 | exit: |
| 2044 | return( 0 ); |
| 2045 | } |
Glenn Strauss | 39e624c | 2022-04-11 13:33:16 -0400 | [diff] [blame] | 2046 | #endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */ |
Gilles Peskine | 1255b0d | 2022-01-13 01:08:48 +0100 | [diff] [blame] | 2047 | |
| 2048 | |
Manuel Pégourié-Gonnard | d12402f | 2020-05-20 10:34:25 +0200 | [diff] [blame] | 2049 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 2050 | defined(MBEDTLS_ENTROPY_C) && \ |
| 2051 | defined(MBEDTLS_CTR_DRBG_C) |
Andrzej Kurek | 92d7417 | 2022-06-28 10:29:42 -0400 | [diff] [blame] | 2052 | void perform_handshake( handshake_test_options *options ) |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2053 | { |
| 2054 | /* forced_ciphersuite needs to last until the end of the handshake */ |
| 2055 | int forced_ciphersuite[2]; |
| 2056 | enum { BUFFSIZE = 17000 }; |
| 2057 | mbedtls_endpoint client, server; |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2058 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2059 | const char *psk_identity = "foo"; |
| 2060 | #endif |
| 2061 | #if defined(MBEDTLS_TIMING_C) |
| 2062 | mbedtls_timing_delay_context timer_client, timer_server; |
| 2063 | #endif |
| 2064 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
| 2065 | unsigned char *context_buf = NULL; |
| 2066 | size_t context_buf_len; |
| 2067 | #endif |
| 2068 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 2069 | int ret = -1; |
| 2070 | #endif |
Neil Armstrong | 8c52ed8 | 2022-05-27 13:14:55 +0200 | [diff] [blame] | 2071 | int expected_handshake_result = options->expected_handshake_result; |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2072 | |
Neil Armstrong | 46a1760 | 2022-02-23 15:11:16 +0100 | [diff] [blame] | 2073 | USE_PSA_INIT( ); |
| 2074 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2075 | mbedtls_test_message_queue server_queue, client_queue; |
| 2076 | mbedtls_test_message_socket_context server_context, client_context; |
Andrzej Kurek | 45916ba | 2020-03-05 14:46:22 -0500 | [diff] [blame] | 2077 | mbedtls_message_socket_init( &server_context ); |
| 2078 | mbedtls_message_socket_init( &client_context ); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2079 | |
| 2080 | /* Client side */ |
| 2081 | if( options->dtls != 0 ) |
| 2082 | { |
| 2083 | TEST_ASSERT( mbedtls_endpoint_init( &client, MBEDTLS_SSL_IS_CLIENT, |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 2084 | options, &client_context, |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2085 | &client_queue, |
Andrzej Kurek | 74394a5 | 2022-03-08 06:50:12 -0500 | [diff] [blame] | 2086 | &server_queue, NULL ) == 0 ); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2087 | #if defined(MBEDTLS_TIMING_C) |
| 2088 | mbedtls_ssl_set_timer_cb( &client.ssl, &timer_client, |
| 2089 | mbedtls_timing_set_delay, |
| 2090 | mbedtls_timing_get_delay ); |
| 2091 | #endif |
| 2092 | } |
| 2093 | else |
| 2094 | { |
| 2095 | TEST_ASSERT( mbedtls_endpoint_init( &client, MBEDTLS_SSL_IS_CLIENT, |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 2096 | options, NULL, NULL, |
Andrzej Kurek | 74394a5 | 2022-03-08 06:50:12 -0500 | [diff] [blame] | 2097 | NULL, NULL ) == 0 ); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2098 | } |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 2099 | |
Glenn Strauss | 39e624c | 2022-04-11 13:33:16 -0400 | [diff] [blame] | 2100 | if( options->client_min_version != MBEDTLS_SSL_VERSION_UNKNOWN ) |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 2101 | { |
Glenn Strauss | e3af4cb | 2022-03-15 03:23:42 -0400 | [diff] [blame] | 2102 | mbedtls_ssl_conf_min_tls_version( &client.conf, |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 2103 | options->client_min_version ); |
| 2104 | } |
| 2105 | |
Glenn Strauss | 39e624c | 2022-04-11 13:33:16 -0400 | [diff] [blame] | 2106 | if( options->client_max_version != MBEDTLS_SSL_VERSION_UNKNOWN ) |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 2107 | { |
Glenn Strauss | e3af4cb | 2022-03-15 03:23:42 -0400 | [diff] [blame] | 2108 | mbedtls_ssl_conf_max_tls_version( &client.conf, |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 2109 | options->client_max_version ); |
| 2110 | } |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2111 | |
| 2112 | if( strlen( options->cipher ) > 0 ) |
| 2113 | { |
| 2114 | set_ciphersuite( &client.conf, options->cipher, forced_ciphersuite ); |
| 2115 | } |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 2116 | |
| 2117 | #if defined (MBEDTLS_DEBUG_C) |
| 2118 | if( options->cli_log_fun ) |
| 2119 | { |
| 2120 | mbedtls_debug_set_threshold( 4 ); |
| 2121 | mbedtls_ssl_conf_dbg( &client.conf, options->cli_log_fun, |
| 2122 | options->cli_log_obj ); |
| 2123 | } |
| 2124 | #endif |
| 2125 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2126 | /* Server side */ |
| 2127 | if( options->dtls != 0 ) |
| 2128 | { |
| 2129 | TEST_ASSERT( mbedtls_endpoint_init( &server, MBEDTLS_SSL_IS_SERVER, |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 2130 | options, &server_context, |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2131 | &server_queue, |
Andrzej Kurek | 74394a5 | 2022-03-08 06:50:12 -0500 | [diff] [blame] | 2132 | &client_queue, NULL ) == 0 ); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2133 | #if defined(MBEDTLS_TIMING_C) |
| 2134 | mbedtls_ssl_set_timer_cb( &server.ssl, &timer_server, |
| 2135 | mbedtls_timing_set_delay, |
| 2136 | mbedtls_timing_get_delay ); |
| 2137 | #endif |
| 2138 | } |
| 2139 | else |
| 2140 | { |
| 2141 | TEST_ASSERT( mbedtls_endpoint_init( &server, MBEDTLS_SSL_IS_SERVER, |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 2142 | options, NULL, NULL, NULL, |
| 2143 | NULL ) == 0 ); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2144 | } |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 2145 | |
| 2146 | mbedtls_ssl_conf_authmode( &server.conf, options->srv_auth_mode ); |
| 2147 | |
Glenn Strauss | 39e624c | 2022-04-11 13:33:16 -0400 | [diff] [blame] | 2148 | if( options->server_min_version != MBEDTLS_SSL_VERSION_UNKNOWN ) |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 2149 | { |
Glenn Strauss | e3af4cb | 2022-03-15 03:23:42 -0400 | [diff] [blame] | 2150 | mbedtls_ssl_conf_min_tls_version( &server.conf, |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 2151 | options->server_min_version ); |
| 2152 | } |
| 2153 | |
Glenn Strauss | 39e624c | 2022-04-11 13:33:16 -0400 | [diff] [blame] | 2154 | if( options->server_max_version != MBEDTLS_SSL_VERSION_UNKNOWN ) |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 2155 | { |
Glenn Strauss | e3af4cb | 2022-03-15 03:23:42 -0400 | [diff] [blame] | 2156 | mbedtls_ssl_conf_max_tls_version( &server.conf, |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 2157 | options->server_max_version ); |
| 2158 | } |
| 2159 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2160 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 2161 | TEST_ASSERT( mbedtls_ssl_conf_max_frag_len( &(server.conf), |
| 2162 | (unsigned char) options->mfl ) == 0 ); |
| 2163 | TEST_ASSERT( mbedtls_ssl_conf_max_frag_len( &(client.conf), |
| 2164 | (unsigned char) options->mfl ) == 0 ); |
| 2165 | #else |
| 2166 | TEST_ASSERT( MBEDTLS_SSL_MAX_FRAG_LEN_NONE == options->mfl ); |
| 2167 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 2168 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2169 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2170 | if( options->psk_str != NULL && options->psk_str->len > 0 ) |
| 2171 | { |
| 2172 | TEST_ASSERT( mbedtls_ssl_conf_psk( &client.conf, options->psk_str->x, |
| 2173 | options->psk_str->len, |
| 2174 | (const unsigned char *) psk_identity, |
| 2175 | strlen( psk_identity ) ) == 0 ); |
| 2176 | |
| 2177 | TEST_ASSERT( mbedtls_ssl_conf_psk( &server.conf, options->psk_str->x, |
| 2178 | options->psk_str->len, |
| 2179 | (const unsigned char *) psk_identity, |
| 2180 | strlen( psk_identity ) ) == 0 ); |
| 2181 | |
| 2182 | mbedtls_ssl_conf_psk_cb( &server.conf, psk_dummy_callback, NULL ); |
| 2183 | } |
| 2184 | #endif |
| 2185 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 2186 | if( options->renegotiate ) |
| 2187 | { |
| 2188 | mbedtls_ssl_conf_renegotiation( &(server.conf), |
| 2189 | MBEDTLS_SSL_RENEGOTIATION_ENABLED ); |
| 2190 | mbedtls_ssl_conf_renegotiation( &(client.conf), |
| 2191 | MBEDTLS_SSL_RENEGOTIATION_ENABLED ); |
| 2192 | |
| 2193 | mbedtls_ssl_conf_legacy_renegotiation( &(server.conf), |
| 2194 | options->legacy_renegotiation ); |
| 2195 | mbedtls_ssl_conf_legacy_renegotiation( &(client.conf), |
| 2196 | options->legacy_renegotiation ); |
| 2197 | } |
| 2198 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
| 2199 | |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 2200 | #if defined (MBEDTLS_DEBUG_C) |
| 2201 | if( options->srv_log_fun ) |
| 2202 | { |
| 2203 | mbedtls_debug_set_threshold( 4 ); |
| 2204 | mbedtls_ssl_conf_dbg( &server.conf, options->srv_log_fun, |
| 2205 | options->srv_log_obj ); |
| 2206 | } |
| 2207 | #endif |
| 2208 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2209 | TEST_ASSERT( mbedtls_mock_socket_connect( &(client.socket), |
| 2210 | &(server.socket), |
| 2211 | BUFFSIZE ) == 0 ); |
| 2212 | |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2213 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 2214 | if( options->resize_buffers != 0 ) |
| 2215 | { |
| 2216 | /* Ensure that the buffer sizes are appropriate before resizes */ |
| 2217 | TEST_ASSERT( client.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN ); |
| 2218 | TEST_ASSERT( client.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN ); |
| 2219 | TEST_ASSERT( server.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN ); |
| 2220 | TEST_ASSERT( server.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN ); |
| 2221 | } |
| 2222 | #endif |
| 2223 | |
Glenn Strauss | 39e624c | 2022-04-11 13:33:16 -0400 | [diff] [blame] | 2224 | if( options->expected_negotiated_version == MBEDTLS_SSL_VERSION_UNKNOWN ) |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 2225 | { |
Hanno Becker | bc00044 | 2021-06-24 09:18:19 +0100 | [diff] [blame] | 2226 | expected_handshake_result = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION; |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 2227 | } |
| 2228 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2229 | TEST_ASSERT( mbedtls_move_handshake_to_state( &(client.ssl), |
| 2230 | &(server.ssl), |
| 2231 | MBEDTLS_SSL_HANDSHAKE_OVER ) |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 2232 | == expected_handshake_result ); |
| 2233 | |
| 2234 | if( expected_handshake_result != 0 ) |
| 2235 | { |
| 2236 | /* Connection will have failed by this point, skip to cleanup */ |
| 2237 | goto exit; |
| 2238 | } |
| 2239 | |
Paul Elliott | 27b0d94 | 2022-03-18 21:55:32 +0000 | [diff] [blame] | 2240 | TEST_ASSERT( mbedtls_ssl_is_handshake_over( &client.ssl ) == 1 ); |
Jerry Yu | df0a71a | 2022-05-26 10:43:30 +0800 | [diff] [blame] | 2241 | |
Jerry Yu | 66adf31 | 2022-05-31 15:23:29 +0800 | [diff] [blame] | 2242 | /* Make sure server state is moved to HANDSHAKE_OVER also. */ |
Ronald Cron | 585cd70 | 2022-06-10 15:02:05 +0200 | [diff] [blame] | 2243 | TEST_EQUAL( mbedtls_move_handshake_to_state( &(server.ssl), |
| 2244 | &(client.ssl), |
| 2245 | MBEDTLS_SSL_HANDSHAKE_OVER ), 0 ); |
Jerry Yu | df0a71a | 2022-05-26 10:43:30 +0800 | [diff] [blame] | 2246 | |
Paul Elliott | 27b0d94 | 2022-03-18 21:55:32 +0000 | [diff] [blame] | 2247 | TEST_ASSERT( mbedtls_ssl_is_handshake_over( &server.ssl ) == 1 ); |
Gilles Peskine | 1255b0d | 2022-01-13 01:08:48 +0100 | [diff] [blame] | 2248 | /* Check that both sides have negotiated the expected version. */ |
| 2249 | mbedtls_test_set_step( 0 ); |
| 2250 | if( ! check_ssl_version( options->expected_negotiated_version, |
| 2251 | &client.ssl ) ) |
| 2252 | goto exit; |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 2253 | |
Gilles Peskine | 1255b0d | 2022-01-13 01:08:48 +0100 | [diff] [blame] | 2254 | mbedtls_test_set_step( 1 ); |
| 2255 | if( ! check_ssl_version( options->expected_negotiated_version, |
| 2256 | &server.ssl ) ) |
| 2257 | goto exit; |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 2258 | |
Neil Armstrong | 8c52ed8 | 2022-05-27 13:14:55 +0200 | [diff] [blame] | 2259 | if( options->expected_ciphersuite != 0 ) |
| 2260 | { |
| 2261 | TEST_EQUAL( server.ssl.session->ciphersuite, |
| 2262 | options->expected_ciphersuite ); |
| 2263 | } |
| 2264 | |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2265 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 2266 | if( options->resize_buffers != 0 ) |
| 2267 | { |
TRodziewicz | 2abf03c | 2021-06-25 14:40:09 +0200 | [diff] [blame] | 2268 | /* A server, when using DTLS, might delay a buffer resize to happen |
| 2269 | * after it receives a message, so we force it. */ |
| 2270 | TEST_ASSERT( exchange_data( &(client.ssl), &(server.ssl) ) == 0 ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2271 | |
TRodziewicz | 2abf03c | 2021-06-25 14:40:09 +0200 | [diff] [blame] | 2272 | TEST_ASSERT( client.ssl.out_buf_len == |
| 2273 | mbedtls_ssl_get_output_buflen( &client.ssl ) ); |
| 2274 | TEST_ASSERT( client.ssl.in_buf_len == |
| 2275 | mbedtls_ssl_get_input_buflen( &client.ssl ) ); |
| 2276 | TEST_ASSERT( server.ssl.out_buf_len == |
| 2277 | mbedtls_ssl_get_output_buflen( &server.ssl ) ); |
| 2278 | TEST_ASSERT( server.ssl.in_buf_len == |
| 2279 | mbedtls_ssl_get_input_buflen( &server.ssl ) ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2280 | } |
| 2281 | #endif |
| 2282 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2283 | if( options->cli_msg_len != 0 || options->srv_msg_len != 0 ) |
| 2284 | { |
| 2285 | /* Start data exchanging test */ |
| 2286 | TEST_ASSERT( mbedtls_exchange_data( &(client.ssl), options->cli_msg_len, |
| 2287 | options->expected_cli_fragments, |
| 2288 | &(server.ssl), options->srv_msg_len, |
| 2289 | options->expected_srv_fragments ) |
| 2290 | == 0 ); |
| 2291 | } |
| 2292 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
| 2293 | if( options->serialize == 1 ) |
| 2294 | { |
| 2295 | TEST_ASSERT( options->dtls == 1 ); |
| 2296 | |
| 2297 | TEST_ASSERT( mbedtls_ssl_context_save( &(server.ssl), NULL, |
| 2298 | 0, &context_buf_len ) |
| 2299 | == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 2300 | |
| 2301 | context_buf = mbedtls_calloc( 1, context_buf_len ); |
| 2302 | TEST_ASSERT( context_buf != NULL ); |
| 2303 | |
| 2304 | TEST_ASSERT( mbedtls_ssl_context_save( &(server.ssl), context_buf, |
| 2305 | context_buf_len, |
| 2306 | &context_buf_len ) == 0 ); |
| 2307 | |
| 2308 | mbedtls_ssl_free( &(server.ssl) ); |
| 2309 | mbedtls_ssl_init( &(server.ssl) ); |
| 2310 | |
| 2311 | TEST_ASSERT( mbedtls_ssl_setup( &(server.ssl), &(server.conf) ) == 0 ); |
| 2312 | |
| 2313 | mbedtls_ssl_set_bio( &( server.ssl ), &server_context, |
| 2314 | mbedtls_mock_tcp_send_msg, |
| 2315 | mbedtls_mock_tcp_recv_msg, |
| 2316 | NULL ); |
| 2317 | |
Gilles Peskine | 49d7ddf | 2022-01-27 23:25:51 +0100 | [diff] [blame] | 2318 | mbedtls_ssl_set_user_data_p( &server.ssl, &server ); |
| 2319 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2320 | #if defined(MBEDTLS_TIMING_C) |
| 2321 | mbedtls_ssl_set_timer_cb( &server.ssl, &timer_server, |
| 2322 | mbedtls_timing_set_delay, |
| 2323 | mbedtls_timing_get_delay ); |
| 2324 | #endif |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2325 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 2326 | if( options->resize_buffers != 0 ) |
| 2327 | { |
| 2328 | /* Ensure that the buffer sizes are appropriate before resizes */ |
| 2329 | TEST_ASSERT( server.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN ); |
| 2330 | TEST_ASSERT( server.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN ); |
| 2331 | } |
| 2332 | #endif |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2333 | TEST_ASSERT( mbedtls_ssl_context_load( &( server.ssl ), context_buf, |
| 2334 | context_buf_len ) == 0 ); |
| 2335 | |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2336 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 2337 | /* Validate buffer sizes after context deserialization */ |
| 2338 | if( options->resize_buffers != 0 ) |
| 2339 | { |
| 2340 | TEST_ASSERT( server.ssl.out_buf_len == |
| 2341 | mbedtls_ssl_get_output_buflen( &server.ssl ) ); |
| 2342 | TEST_ASSERT( server.ssl.in_buf_len == |
| 2343 | mbedtls_ssl_get_input_buflen( &server.ssl ) ); |
| 2344 | } |
| 2345 | #endif |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2346 | /* Retest writing/reading */ |
| 2347 | if( options->cli_msg_len != 0 || options->srv_msg_len != 0 ) |
| 2348 | { |
| 2349 | TEST_ASSERT( mbedtls_exchange_data( &(client.ssl), |
| 2350 | options->cli_msg_len, |
| 2351 | options->expected_cli_fragments, |
| 2352 | &(server.ssl), |
| 2353 | options->srv_msg_len, |
| 2354 | options->expected_srv_fragments ) |
| 2355 | == 0 ); |
| 2356 | } |
| 2357 | } |
| 2358 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2359 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2360 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 2361 | if( options->renegotiate ) |
| 2362 | { |
| 2363 | /* Start test with renegotiation */ |
| 2364 | TEST_ASSERT( server.ssl.renego_status == |
| 2365 | MBEDTLS_SSL_INITIAL_HANDSHAKE ); |
| 2366 | TEST_ASSERT( client.ssl.renego_status == |
| 2367 | MBEDTLS_SSL_INITIAL_HANDSHAKE ); |
| 2368 | |
| 2369 | /* After calling this function for the server, it only sends a handshake |
| 2370 | * request. All renegotiation should happen during data exchanging */ |
| 2371 | TEST_ASSERT( mbedtls_ssl_renegotiate( &(server.ssl) ) == 0 ); |
| 2372 | TEST_ASSERT( server.ssl.renego_status == |
| 2373 | MBEDTLS_SSL_RENEGOTIATION_PENDING ); |
| 2374 | TEST_ASSERT( client.ssl.renego_status == |
| 2375 | MBEDTLS_SSL_INITIAL_HANDSHAKE ); |
| 2376 | |
| 2377 | TEST_ASSERT( exchange_data( &(client.ssl), &(server.ssl) ) == 0 ); |
| 2378 | TEST_ASSERT( server.ssl.renego_status == |
| 2379 | MBEDTLS_SSL_RENEGOTIATION_DONE ); |
| 2380 | TEST_ASSERT( client.ssl.renego_status == |
| 2381 | MBEDTLS_SSL_RENEGOTIATION_DONE ); |
| 2382 | |
| 2383 | /* After calling mbedtls_ssl_renegotiate for the client all renegotiation |
| 2384 | * should happen inside this function. However in this test, we cannot |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 2385 | * perform simultaneous communication between client and server so this |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2386 | * function will return waiting error on the socket. All rest of |
| 2387 | * renegotiation should happen during data exchanging */ |
| 2388 | ret = mbedtls_ssl_renegotiate( &(client.ssl) ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2389 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 2390 | if( options->resize_buffers != 0 ) |
| 2391 | { |
| 2392 | /* Ensure that the buffer sizes are appropriate before resizes */ |
| 2393 | TEST_ASSERT( client.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN ); |
| 2394 | TEST_ASSERT( client.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN ); |
| 2395 | } |
| 2396 | #endif |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2397 | TEST_ASSERT( ret == 0 || |
| 2398 | ret == MBEDTLS_ERR_SSL_WANT_READ || |
| 2399 | ret == MBEDTLS_ERR_SSL_WANT_WRITE ); |
| 2400 | TEST_ASSERT( server.ssl.renego_status == |
| 2401 | MBEDTLS_SSL_RENEGOTIATION_DONE ); |
| 2402 | TEST_ASSERT( client.ssl.renego_status == |
| 2403 | MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ); |
| 2404 | |
| 2405 | TEST_ASSERT( exchange_data( &(client.ssl), &(server.ssl) ) == 0 ); |
| 2406 | TEST_ASSERT( server.ssl.renego_status == |
| 2407 | MBEDTLS_SSL_RENEGOTIATION_DONE ); |
| 2408 | TEST_ASSERT( client.ssl.renego_status == |
| 2409 | MBEDTLS_SSL_RENEGOTIATION_DONE ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2410 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 2411 | /* Validate buffer sizes after renegotiation */ |
| 2412 | if( options->resize_buffers != 0 ) |
| 2413 | { |
| 2414 | TEST_ASSERT( client.ssl.out_buf_len == |
| 2415 | mbedtls_ssl_get_output_buflen( &client.ssl ) ); |
| 2416 | TEST_ASSERT( client.ssl.in_buf_len == |
| 2417 | mbedtls_ssl_get_input_buflen( &client.ssl ) ); |
| 2418 | TEST_ASSERT( server.ssl.out_buf_len == |
| 2419 | mbedtls_ssl_get_output_buflen( &server.ssl ) ); |
| 2420 | TEST_ASSERT( server.ssl.in_buf_len == |
| 2421 | mbedtls_ssl_get_input_buflen( &server.ssl ) ); |
| 2422 | } |
| 2423 | #endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */ |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2424 | } |
| 2425 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
| 2426 | |
Gilles Peskine | 80dae04 | 2022-01-21 23:50:39 +0100 | [diff] [blame] | 2427 | TEST_ASSERT( mbedtls_ssl_conf_get_user_data_p( &client.conf ) == &client ); |
| 2428 | TEST_ASSERT( mbedtls_ssl_get_user_data_p( &client.ssl ) == &client ); |
| 2429 | TEST_ASSERT( mbedtls_ssl_conf_get_user_data_p( &server.conf ) == &server ); |
| 2430 | TEST_ASSERT( mbedtls_ssl_get_user_data_p( &server.ssl ) == &server ); |
| 2431 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2432 | exit: |
| 2433 | mbedtls_endpoint_free( &client, options->dtls != 0 ? &client_context : NULL ); |
| 2434 | mbedtls_endpoint_free( &server, options->dtls != 0 ? &server_context : NULL ); |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 2435 | #if defined (MBEDTLS_DEBUG_C) |
| 2436 | if( options->cli_log_fun || options->srv_log_fun ) |
| 2437 | { |
| 2438 | mbedtls_debug_set_threshold( 0 ); |
| 2439 | } |
| 2440 | #endif |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2441 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
| 2442 | if( context_buf != NULL ) |
| 2443 | mbedtls_free( context_buf ); |
| 2444 | #endif |
Neil Armstrong | 46a1760 | 2022-02-23 15:11:16 +0100 | [diff] [blame] | 2445 | USE_PSA_DONE( ); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2446 | } |
Manuel Pégourié-Gonnard | d12402f | 2020-05-20 10:34:25 +0200 | [diff] [blame] | 2447 | #endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */ |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2448 | |
Ronald Cron | e7b9b6b | 2022-06-10 17:24:31 +0200 | [diff] [blame] | 2449 | #if defined(MBEDTLS_TEST_HOOKS) |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 2450 | /* |
| 2451 | * Tweak vector lengths in a TLS 1.3 Certificate message |
| 2452 | * |
Ronald Cron | cf600bc | 2022-06-17 15:54:16 +0200 | [diff] [blame] | 2453 | * \param[in] buf Buffer containing the Certificate message to tweak |
| 2454 | * \param[in]]out] end End of the buffer to parse |
| 2455 | * \param tweak Tweak identifier (from 1 to the number of tweaks). |
| 2456 | * \param[out] expected_result Error code expected from the parsing function |
| 2457 | * \param[out] args Arguments of the MBEDTLS_SSL_CHK_BUF_READ_PTR call that |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 2458 | * is expected to fail. All zeroes if no |
| 2459 | * MBEDTLS_SSL_CHK_BUF_READ_PTR failure is expected. |
| 2460 | */ |
| 2461 | int tweak_tls13_certificate_msg_vector_len( |
Ronald Cron | e7b9b6b | 2022-06-10 17:24:31 +0200 | [diff] [blame] | 2462 | unsigned char *buf, unsigned char **end, int tweak, |
| 2463 | int *expected_result, mbedtls_ssl_chk_buf_ptr_args *args ) |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 2464 | { |
| 2465 | /* |
| 2466 | * The definition of the tweaks assume that the certificate list contains only |
| 2467 | * one certificate. |
| 2468 | */ |
| 2469 | |
| 2470 | /* |
| 2471 | * struct { |
| 2472 | * opaque cert_data<1..2^24-1>; |
| 2473 | * Extension extensions<0..2^16-1>; |
| 2474 | * } CertificateEntry; |
| 2475 | * |
| 2476 | * struct { |
| 2477 | * opaque certificate_request_context<0..2^8-1>; |
| 2478 | * CertificateEntry certificate_list<0..2^24-1>; |
| 2479 | * } Certificate; |
| 2480 | */ |
| 2481 | unsigned char *p_certificate_request_context_len = buf; |
| 2482 | size_t certificate_request_context_len = buf[0]; |
| 2483 | |
| 2484 | unsigned char *p_certificate_list_len = buf + 1 + certificate_request_context_len; |
| 2485 | unsigned char *certificate_list = p_certificate_list_len + 3; |
| 2486 | size_t certificate_list_len = MBEDTLS_GET_UINT24_BE( p_certificate_list_len, 0 ); |
| 2487 | |
| 2488 | unsigned char *p_cert_data_len = certificate_list; |
| 2489 | unsigned char *cert_data = p_cert_data_len + 3; |
| 2490 | size_t cert_data_len = MBEDTLS_GET_UINT24_BE( p_cert_data_len, 0 ); |
| 2491 | |
| 2492 | unsigned char *p_extensions_len = cert_data + cert_data_len; |
| 2493 | unsigned char *extensions = p_extensions_len + 2; |
| 2494 | size_t extensions_len = MBEDTLS_GET_UINT16_BE( p_extensions_len, 0 ); |
| 2495 | |
| 2496 | *expected_result = MBEDTLS_ERR_SSL_DECODE_ERROR; |
| 2497 | |
| 2498 | switch( tweak ) |
| 2499 | { |
| 2500 | case 1: |
| 2501 | /* Failure when checking if the certificate request context length and |
| 2502 | * certificate list length can be read |
| 2503 | */ |
| 2504 | *end = buf + 3; |
Ronald Cron | e7b9b6b | 2022-06-10 17:24:31 +0200 | [diff] [blame] | 2505 | set_chk_buf_ptr_args( args, buf, *end, 4 ); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 2506 | break; |
| 2507 | |
| 2508 | case 2: |
| 2509 | /* Invalid certificate request context length. |
| 2510 | */ |
| 2511 | *p_certificate_request_context_len = |
| 2512 | certificate_request_context_len + 1; |
Ronald Cron | e7b9b6b | 2022-06-10 17:24:31 +0200 | [diff] [blame] | 2513 | reset_chk_buf_ptr_args( args ); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 2514 | break; |
| 2515 | |
| 2516 | case 3: |
| 2517 | /* Failure when checking if certificate_list data can be read. */ |
| 2518 | MBEDTLS_PUT_UINT24_BE( certificate_list_len + 1, |
| 2519 | p_certificate_list_len, 0 ); |
Ronald Cron | e7b9b6b | 2022-06-10 17:24:31 +0200 | [diff] [blame] | 2520 | set_chk_buf_ptr_args( args, certificate_list, *end, |
| 2521 | certificate_list_len + 1 ); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 2522 | break; |
| 2523 | |
| 2524 | case 4: |
| 2525 | /* Failure when checking if the cert_data length can be read. */ |
| 2526 | MBEDTLS_PUT_UINT24_BE( 2, p_certificate_list_len, 0 ); |
Ronald Cron | e7b9b6b | 2022-06-10 17:24:31 +0200 | [diff] [blame] | 2527 | set_chk_buf_ptr_args( args, p_cert_data_len, certificate_list + 2, 3 ); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 2528 | break; |
| 2529 | |
| 2530 | case 5: |
| 2531 | /* Failure when checking if cert_data data can be read. */ |
| 2532 | MBEDTLS_PUT_UINT24_BE( certificate_list_len - 3 + 1, |
| 2533 | p_cert_data_len, 0 ); |
Ronald Cron | e7b9b6b | 2022-06-10 17:24:31 +0200 | [diff] [blame] | 2534 | set_chk_buf_ptr_args( args, cert_data, |
| 2535 | certificate_list + certificate_list_len, |
| 2536 | certificate_list_len - 3 + 1 ); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 2537 | break; |
| 2538 | |
| 2539 | case 6: |
| 2540 | /* Failure when checking if the extensions length can be read. */ |
| 2541 | MBEDTLS_PUT_UINT24_BE( certificate_list_len - extensions_len - 1, |
| 2542 | p_certificate_list_len, 0 ); |
Ronald Cron | e7b9b6b | 2022-06-10 17:24:31 +0200 | [diff] [blame] | 2543 | set_chk_buf_ptr_args( args, p_extensions_len, |
| 2544 | certificate_list + certificate_list_len - extensions_len - 1, 2 ); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 2545 | break; |
| 2546 | |
| 2547 | case 7: |
| 2548 | /* Failure when checking if extensions data can be read. */ |
| 2549 | MBEDTLS_PUT_UINT16_BE( extensions_len + 1, p_extensions_len, 0 ); |
Ronald Cron | e7b9b6b | 2022-06-10 17:24:31 +0200 | [diff] [blame] | 2550 | |
| 2551 | set_chk_buf_ptr_args( args, extensions, |
| 2552 | certificate_list + certificate_list_len, extensions_len + 1 ); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 2553 | break; |
| 2554 | |
| 2555 | default: |
| 2556 | return( -1 ); |
| 2557 | } |
| 2558 | |
| 2559 | return( 0 ); |
| 2560 | } |
Ronald Cron | e7b9b6b | 2022-06-10 17:24:31 +0200 | [diff] [blame] | 2561 | #endif /* MBEDTLS_TEST_HOOKS */ |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 2562 | /* END_HEADER */ |
| 2563 | |
| 2564 | /* BEGIN_DEPENDENCIES |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2565 | * depends_on:MBEDTLS_SSL_TLS_C |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 2566 | * END_DEPENDENCIES |
| 2567 | */ |
| 2568 | |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2569 | /* BEGIN_CASE */ |
| 2570 | void test_callback_buffer_sanity() |
| 2571 | { |
| 2572 | enum { MSGLEN = 10 }; |
| 2573 | mbedtls_test_buffer buf; |
| 2574 | unsigned char input[MSGLEN]; |
| 2575 | unsigned char output[MSGLEN]; |
| 2576 | |
| 2577 | memset( input, 0, sizeof(input) ); |
| 2578 | |
| 2579 | /* Make sure calling put and get on NULL buffer results in error. */ |
| 2580 | TEST_ASSERT( mbedtls_test_buffer_put( NULL, input, sizeof( input ) ) |
| 2581 | == -1 ); |
| 2582 | TEST_ASSERT( mbedtls_test_buffer_get( NULL, output, sizeof( output ) ) |
| 2583 | == -1 ); |
| 2584 | TEST_ASSERT( mbedtls_test_buffer_put( NULL, NULL, sizeof( input ) ) == -1 ); |
Andrzej Kurek | f777414 | 2020-01-22 06:34:59 -0500 | [diff] [blame] | 2585 | |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2586 | TEST_ASSERT( mbedtls_test_buffer_put( NULL, NULL, 0 ) == -1 ); |
| 2587 | TEST_ASSERT( mbedtls_test_buffer_get( NULL, NULL, 0 ) == -1 ); |
| 2588 | |
| 2589 | /* Make sure calling put and get on a buffer that hasn't been set up results |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 2590 | * in error. */ |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2591 | mbedtls_test_buffer_init( &buf ); |
| 2592 | |
| 2593 | TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, sizeof( input ) ) == -1 ); |
| 2594 | TEST_ASSERT( mbedtls_test_buffer_get( &buf, output, sizeof( output ) ) |
| 2595 | == -1 ); |
| 2596 | TEST_ASSERT( mbedtls_test_buffer_put( &buf, NULL, sizeof( input ) ) == -1 ); |
Andrzej Kurek | f777414 | 2020-01-22 06:34:59 -0500 | [diff] [blame] | 2597 | |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2598 | TEST_ASSERT( mbedtls_test_buffer_put( &buf, NULL, 0 ) == -1 ); |
| 2599 | TEST_ASSERT( mbedtls_test_buffer_get( &buf, NULL, 0 ) == -1 ); |
| 2600 | |
Andrzej Kurek | f777414 | 2020-01-22 06:34:59 -0500 | [diff] [blame] | 2601 | /* Make sure calling put and get on NULL input only results in |
| 2602 | * error if the length is not zero, and that a NULL output is valid for data |
| 2603 | * dropping. |
| 2604 | */ |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2605 | |
| 2606 | TEST_ASSERT( mbedtls_test_buffer_setup( &buf, sizeof( input ) ) == 0 ); |
| 2607 | |
| 2608 | TEST_ASSERT( mbedtls_test_buffer_put( &buf, NULL, sizeof( input ) ) == -1 ); |
| 2609 | TEST_ASSERT( mbedtls_test_buffer_get( &buf, NULL, sizeof( output ) ) |
Andrzej Kurek | f777414 | 2020-01-22 06:34:59 -0500 | [diff] [blame] | 2610 | == 0 ); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2611 | TEST_ASSERT( mbedtls_test_buffer_put( &buf, NULL, 0 ) == 0 ); |
| 2612 | TEST_ASSERT( mbedtls_test_buffer_get( &buf, NULL, 0 ) == 0 ); |
| 2613 | |
Piotr Nowicki | fb437d7 | 2020-01-13 16:59:12 +0100 | [diff] [blame] | 2614 | /* Make sure calling put several times in the row is safe */ |
| 2615 | |
| 2616 | TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, sizeof( input ) ) |
| 2617 | == sizeof( input ) ); |
| 2618 | TEST_ASSERT( mbedtls_test_buffer_get( &buf, output, 2 ) == 2 ); |
| 2619 | TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, 1 ) == 1 ); |
| 2620 | TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, 2 ) == 1 ); |
| 2621 | TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, 2 ) == 0 ); |
| 2622 | |
| 2623 | |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2624 | exit: |
| 2625 | |
| 2626 | mbedtls_test_buffer_free( &buf ); |
| 2627 | } |
| 2628 | /* END_CASE */ |
| 2629 | |
| 2630 | /* |
| 2631 | * Test if the implementation of `mbedtls_test_buffer` related functions is |
| 2632 | * correct and works as expected. |
| 2633 | * |
| 2634 | * That is |
| 2635 | * - If we try to put in \p put1 bytes then we can put in \p put1_ret bytes. |
| 2636 | * - Afterwards if we try to get \p get1 bytes then we can get \get1_ret bytes. |
| 2637 | * - Next, if we try to put in \p put1 bytes then we can put in \p put1_ret |
| 2638 | * bytes. |
| 2639 | * - Afterwards if we try to get \p get1 bytes then we can get \get1_ret bytes. |
| 2640 | * - All of the bytes we got match the bytes we put in in a FIFO manner. |
| 2641 | */ |
| 2642 | |
| 2643 | /* BEGIN_CASE */ |
| 2644 | void test_callback_buffer( int size, int put1, int put1_ret, |
| 2645 | int get1, int get1_ret, int put2, int put2_ret, |
| 2646 | int get2, int get2_ret ) |
| 2647 | { |
| 2648 | enum { ROUNDS = 2 }; |
| 2649 | size_t put[ROUNDS]; |
| 2650 | int put_ret[ROUNDS]; |
| 2651 | size_t get[ROUNDS]; |
| 2652 | int get_ret[ROUNDS]; |
| 2653 | mbedtls_test_buffer buf; |
| 2654 | unsigned char* input = NULL; |
| 2655 | size_t input_len; |
| 2656 | unsigned char* output = NULL; |
| 2657 | size_t output_len; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2658 | size_t i, j, written, read; |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2659 | |
| 2660 | mbedtls_test_buffer_init( &buf ); |
| 2661 | TEST_ASSERT( mbedtls_test_buffer_setup( &buf, size ) == 0 ); |
| 2662 | |
| 2663 | /* Check the sanity of input parameters and initialise local variables. That |
| 2664 | * is, ensure that the amount of data is not negative and that we are not |
| 2665 | * expecting more to put or get than we actually asked for. */ |
| 2666 | TEST_ASSERT( put1 >= 0 ); |
| 2667 | put[0] = put1; |
| 2668 | put_ret[0] = put1_ret; |
| 2669 | TEST_ASSERT( put1_ret <= put1 ); |
| 2670 | TEST_ASSERT( put2 >= 0 ); |
| 2671 | put[1] = put2; |
| 2672 | put_ret[1] = put2_ret; |
| 2673 | TEST_ASSERT( put2_ret <= put2 ); |
| 2674 | |
| 2675 | TEST_ASSERT( get1 >= 0 ); |
| 2676 | get[0] = get1; |
| 2677 | get_ret[0] = get1_ret; |
| 2678 | TEST_ASSERT( get1_ret <= get1 ); |
| 2679 | TEST_ASSERT( get2 >= 0 ); |
| 2680 | get[1] = get2; |
| 2681 | get_ret[1] = get2_ret; |
| 2682 | TEST_ASSERT( get2_ret <= get2 ); |
| 2683 | |
| 2684 | input_len = 0; |
| 2685 | /* Calculate actual input and output lengths */ |
| 2686 | for( j = 0; j < ROUNDS; j++ ) |
| 2687 | { |
| 2688 | if( put_ret[j] > 0 ) |
| 2689 | { |
| 2690 | input_len += put_ret[j]; |
| 2691 | } |
| 2692 | } |
| 2693 | /* In order to always have a valid pointer we always allocate at least 1 |
| 2694 | * byte. */ |
| 2695 | if( input_len == 0 ) |
| 2696 | input_len = 1; |
| 2697 | ASSERT_ALLOC( input, input_len ); |
| 2698 | |
| 2699 | output_len = 0; |
| 2700 | for( j = 0; j < ROUNDS; j++ ) |
| 2701 | { |
| 2702 | if( get_ret[j] > 0 ) |
| 2703 | { |
| 2704 | output_len += get_ret[j]; |
| 2705 | } |
| 2706 | } |
| 2707 | TEST_ASSERT( output_len <= input_len ); |
| 2708 | /* In order to always have a valid pointer we always allocate at least 1 |
| 2709 | * byte. */ |
| 2710 | if( output_len == 0 ) |
| 2711 | output_len = 1; |
| 2712 | ASSERT_ALLOC( output, output_len ); |
| 2713 | |
| 2714 | /* Fill up the buffer with structured data so that unwanted changes |
| 2715 | * can be detected */ |
| 2716 | for( i = 0; i < input_len; i++ ) |
| 2717 | { |
| 2718 | input[i] = i & 0xFF; |
| 2719 | } |
| 2720 | |
| 2721 | written = read = 0; |
| 2722 | for( j = 0; j < ROUNDS; j++ ) |
| 2723 | { |
| 2724 | TEST_ASSERT( put_ret[j] == mbedtls_test_buffer_put( &buf, |
| 2725 | input + written, put[j] ) ); |
| 2726 | written += put_ret[j]; |
| 2727 | TEST_ASSERT( get_ret[j] == mbedtls_test_buffer_get( &buf, |
| 2728 | output + read, get[j] ) ); |
| 2729 | read += get_ret[j]; |
| 2730 | TEST_ASSERT( read <= written ); |
| 2731 | if( get_ret[j] > 0 ) |
| 2732 | { |
| 2733 | TEST_ASSERT( memcmp( output + read - get_ret[j], |
| 2734 | input + read - get_ret[j], get_ret[j] ) |
| 2735 | == 0 ); |
| 2736 | } |
| 2737 | } |
| 2738 | |
| 2739 | exit: |
| 2740 | |
| 2741 | mbedtls_free( input ); |
| 2742 | mbedtls_free( output ); |
| 2743 | mbedtls_test_buffer_free( &buf ); |
| 2744 | } |
| 2745 | /* END_CASE */ |
| 2746 | |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2747 | /* |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2748 | * Test if the implementation of `mbedtls_mock_socket` related I/O functions is |
| 2749 | * correct and works as expected on unconnected sockets. |
| 2750 | */ |
| 2751 | |
| 2752 | /* BEGIN_CASE */ |
| 2753 | void ssl_mock_sanity( ) |
| 2754 | { |
| 2755 | enum { MSGLEN = 105 }; |
Paul Elliott | 21c8fe5 | 2021-11-24 16:54:26 +0000 | [diff] [blame] | 2756 | unsigned char message[MSGLEN] = { 0 }; |
| 2757 | unsigned char received[MSGLEN] = { 0 }; |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2758 | mbedtls_mock_socket socket; |
| 2759 | |
| 2760 | mbedtls_mock_socket_init( &socket ); |
| 2761 | TEST_ASSERT( mbedtls_mock_tcp_send_b( &socket, message, MSGLEN ) < 0 ); |
| 2762 | mbedtls_mock_socket_close( &socket ); |
| 2763 | mbedtls_mock_socket_init( &socket ); |
| 2764 | TEST_ASSERT( mbedtls_mock_tcp_recv_b( &socket, received, MSGLEN ) < 0 ); |
| 2765 | mbedtls_mock_socket_close( &socket ); |
| 2766 | |
| 2767 | mbedtls_mock_socket_init( &socket ); |
| 2768 | TEST_ASSERT( mbedtls_mock_tcp_send_nb( &socket, message, MSGLEN ) < 0 ); |
| 2769 | mbedtls_mock_socket_close( &socket ); |
| 2770 | mbedtls_mock_socket_init( &socket ); |
| 2771 | TEST_ASSERT( mbedtls_mock_tcp_recv_nb( &socket, received, MSGLEN ) < 0 ); |
| 2772 | mbedtls_mock_socket_close( &socket ); |
| 2773 | |
| 2774 | exit: |
| 2775 | |
| 2776 | mbedtls_mock_socket_close( &socket ); |
| 2777 | } |
| 2778 | /* END_CASE */ |
| 2779 | |
| 2780 | /* |
| 2781 | * Test if the implementation of `mbedtls_mock_socket` related functions can |
| 2782 | * send a single message from the client to the server. |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2783 | */ |
| 2784 | |
| 2785 | /* BEGIN_CASE */ |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2786 | void ssl_mock_tcp( int blocking ) |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2787 | { |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2788 | enum { MSGLEN = 105 }; |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2789 | enum { BUFLEN = MSGLEN / 5 }; |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2790 | unsigned char message[MSGLEN]; |
| 2791 | unsigned char received[MSGLEN]; |
| 2792 | mbedtls_mock_socket client; |
| 2793 | mbedtls_mock_socket server; |
| 2794 | size_t written, read; |
| 2795 | int send_ret, recv_ret; |
| 2796 | mbedtls_ssl_send_t *send; |
| 2797 | mbedtls_ssl_recv_t *recv; |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2798 | unsigned i; |
| 2799 | |
| 2800 | if( blocking == 0 ) |
| 2801 | { |
| 2802 | send = mbedtls_mock_tcp_send_nb; |
| 2803 | recv = mbedtls_mock_tcp_recv_nb; |
| 2804 | } |
| 2805 | else |
| 2806 | { |
| 2807 | send = mbedtls_mock_tcp_send_b; |
| 2808 | recv = mbedtls_mock_tcp_recv_b; |
| 2809 | } |
| 2810 | |
| 2811 | mbedtls_mock_socket_init( &client ); |
| 2812 | mbedtls_mock_socket_init( &server ); |
| 2813 | |
| 2814 | /* Fill up the buffer with structured data so that unwanted changes |
| 2815 | * can be detected */ |
| 2816 | for( i = 0; i < MSGLEN; i++ ) |
| 2817 | { |
| 2818 | message[i] = i & 0xFF; |
| 2819 | } |
| 2820 | |
| 2821 | /* Make sure that sending a message takes a few iterations. */ |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2822 | TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, BUFLEN ) ); |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2823 | |
| 2824 | /* Send the message to the server */ |
| 2825 | send_ret = recv_ret = 1; |
| 2826 | written = read = 0; |
| 2827 | while( send_ret != 0 || recv_ret != 0 ) |
| 2828 | { |
| 2829 | send_ret = send( &client, message + written, MSGLEN - written ); |
| 2830 | |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2831 | TEST_ASSERT( send_ret >= 0 ); |
| 2832 | TEST_ASSERT( send_ret <= BUFLEN ); |
| 2833 | written += send_ret; |
| 2834 | |
| 2835 | /* If the buffer is full we can test blocking and non-blocking send */ |
| 2836 | if ( send_ret == BUFLEN ) |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2837 | { |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2838 | int blocking_ret = send( &client, message , 1 ); |
| 2839 | if ( blocking ) |
| 2840 | { |
| 2841 | TEST_ASSERT( blocking_ret == 0 ); |
| 2842 | } |
| 2843 | else |
| 2844 | { |
| 2845 | TEST_ASSERT( blocking_ret == MBEDTLS_ERR_SSL_WANT_WRITE ); |
| 2846 | } |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2847 | } |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2848 | |
| 2849 | recv_ret = recv( &server, received + read, MSGLEN - read ); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2850 | |
| 2851 | /* The result depends on whether any data was sent */ |
| 2852 | if ( send_ret > 0 ) |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2853 | { |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2854 | TEST_ASSERT( recv_ret > 0 ); |
| 2855 | TEST_ASSERT( recv_ret <= BUFLEN ); |
| 2856 | read += recv_ret; |
| 2857 | } |
| 2858 | else if( blocking ) |
| 2859 | { |
| 2860 | TEST_ASSERT( recv_ret == 0 ); |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2861 | } |
| 2862 | else |
| 2863 | { |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2864 | TEST_ASSERT( recv_ret == MBEDTLS_ERR_SSL_WANT_READ ); |
| 2865 | recv_ret = 0; |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2866 | } |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2867 | |
| 2868 | /* If the buffer is empty we can test blocking and non-blocking read */ |
| 2869 | if ( recv_ret == BUFLEN ) |
| 2870 | { |
| 2871 | int blocking_ret = recv( &server, received, 1 ); |
| 2872 | if ( blocking ) |
| 2873 | { |
| 2874 | TEST_ASSERT( blocking_ret == 0 ); |
| 2875 | } |
| 2876 | else |
| 2877 | { |
| 2878 | TEST_ASSERT( blocking_ret == MBEDTLS_ERR_SSL_WANT_READ ); |
| 2879 | } |
| 2880 | } |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2881 | } |
| 2882 | TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 ); |
| 2883 | |
| 2884 | exit: |
| 2885 | |
| 2886 | mbedtls_mock_socket_close( &client ); |
| 2887 | mbedtls_mock_socket_close( &server ); |
| 2888 | } |
| 2889 | /* END_CASE */ |
| 2890 | |
| 2891 | /* |
| 2892 | * Test if the implementation of `mbedtls_mock_socket` related functions can |
| 2893 | * send messages in both direction at the same time (with the I/O calls |
| 2894 | * interleaving). |
| 2895 | */ |
| 2896 | |
| 2897 | /* BEGIN_CASE */ |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2898 | void ssl_mock_tcp_interleaving( int blocking ) |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2899 | { |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2900 | enum { ROUNDS = 2 }; |
| 2901 | enum { MSGLEN = 105 }; |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2902 | enum { BUFLEN = MSGLEN / 5 }; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2903 | unsigned char message[ROUNDS][MSGLEN]; |
| 2904 | unsigned char received[ROUNDS][MSGLEN]; |
| 2905 | mbedtls_mock_socket client; |
| 2906 | mbedtls_mock_socket server; |
| 2907 | size_t written[ROUNDS]; |
| 2908 | size_t read[ROUNDS]; |
| 2909 | int send_ret[ROUNDS]; |
| 2910 | int recv_ret[ROUNDS]; |
| 2911 | unsigned i, j, progress; |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 2912 | mbedtls_ssl_send_t *send; |
| 2913 | mbedtls_ssl_recv_t *recv; |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 2914 | |
| 2915 | if( blocking == 0 ) |
| 2916 | { |
| 2917 | send = mbedtls_mock_tcp_send_nb; |
| 2918 | recv = mbedtls_mock_tcp_recv_nb; |
| 2919 | } |
| 2920 | else |
| 2921 | { |
| 2922 | send = mbedtls_mock_tcp_send_b; |
| 2923 | recv = mbedtls_mock_tcp_recv_b; |
| 2924 | } |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2925 | |
| 2926 | mbedtls_mock_socket_init( &client ); |
| 2927 | mbedtls_mock_socket_init( &server ); |
| 2928 | |
| 2929 | /* Fill up the buffers with structured data so that unwanted changes |
| 2930 | * can be detected */ |
| 2931 | for( i = 0; i < ROUNDS; i++ ) |
| 2932 | { |
| 2933 | for( j = 0; j < MSGLEN; j++ ) |
| 2934 | { |
| 2935 | message[i][j] = ( i * MSGLEN + j ) & 0xFF; |
| 2936 | } |
| 2937 | } |
| 2938 | |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2939 | /* Make sure that sending a message takes a few iterations. */ |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2940 | TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, BUFLEN ) ); |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2941 | |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2942 | /* Send the message from both sides, interleaving. */ |
| 2943 | progress = 1; |
| 2944 | for( i = 0; i < ROUNDS; i++ ) |
| 2945 | { |
| 2946 | written[i] = 0; |
| 2947 | read[i] = 0; |
| 2948 | } |
| 2949 | /* This loop does not stop as long as there was a successful write or read |
| 2950 | * of at least one byte on either side. */ |
| 2951 | while( progress != 0 ) |
| 2952 | { |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2953 | mbedtls_mock_socket *socket; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2954 | |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2955 | for( i = 0; i < ROUNDS; i++ ) |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 2956 | { |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2957 | /* First sending is from the client */ |
| 2958 | socket = ( i % 2 == 0 ) ? ( &client ) : ( &server ); |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2959 | |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2960 | send_ret[i] = send( socket, message[i] + written[i], |
| 2961 | MSGLEN - written[i] ); |
| 2962 | TEST_ASSERT( send_ret[i] >= 0 ); |
| 2963 | TEST_ASSERT( send_ret[i] <= BUFLEN ); |
| 2964 | written[i] += send_ret[i]; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2965 | |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2966 | /* If the buffer is full we can test blocking and non-blocking |
| 2967 | * send */ |
| 2968 | if ( send_ret[i] == BUFLEN ) |
| 2969 | { |
| 2970 | int blocking_ret = send( socket, message[i] , 1 ); |
| 2971 | if ( blocking ) |
| 2972 | { |
| 2973 | TEST_ASSERT( blocking_ret == 0 ); |
| 2974 | } |
| 2975 | else |
| 2976 | { |
| 2977 | TEST_ASSERT( blocking_ret == MBEDTLS_ERR_SSL_WANT_WRITE ); |
| 2978 | } |
| 2979 | } |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 2980 | } |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2981 | |
| 2982 | for( i = 0; i < ROUNDS; i++ ) |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 2983 | { |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2984 | /* First receiving is from the server */ |
| 2985 | socket = ( i % 2 == 0 ) ? ( &server ) : ( &client ); |
| 2986 | |
| 2987 | recv_ret[i] = recv( socket, received[i] + read[i], |
| 2988 | MSGLEN - read[i] ); |
| 2989 | |
| 2990 | /* The result depends on whether any data was sent */ |
| 2991 | if ( send_ret[i] > 0 ) |
| 2992 | { |
| 2993 | TEST_ASSERT( recv_ret[i] > 0 ); |
| 2994 | TEST_ASSERT( recv_ret[i] <= BUFLEN ); |
| 2995 | read[i] += recv_ret[i]; |
| 2996 | } |
| 2997 | else if( blocking ) |
| 2998 | { |
| 2999 | TEST_ASSERT( recv_ret[i] == 0 ); |
| 3000 | } |
| 3001 | else |
| 3002 | { |
| 3003 | TEST_ASSERT( recv_ret[i] == MBEDTLS_ERR_SSL_WANT_READ ); |
| 3004 | recv_ret[i] = 0; |
| 3005 | } |
| 3006 | |
| 3007 | /* If the buffer is empty we can test blocking and non-blocking |
| 3008 | * read */ |
| 3009 | if ( recv_ret[i] == BUFLEN ) |
| 3010 | { |
| 3011 | int blocking_ret = recv( socket, received[i], 1 ); |
| 3012 | if ( blocking ) |
| 3013 | { |
| 3014 | TEST_ASSERT( blocking_ret == 0 ); |
| 3015 | } |
| 3016 | else |
| 3017 | { |
| 3018 | TEST_ASSERT( blocking_ret == MBEDTLS_ERR_SSL_WANT_READ ); |
| 3019 | } |
| 3020 | } |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 3021 | } |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 3022 | |
| 3023 | progress = 0; |
| 3024 | for( i = 0; i < ROUNDS; i++ ) |
| 3025 | { |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 3026 | progress += send_ret[i] + recv_ret[i]; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 3027 | } |
| 3028 | } |
| 3029 | |
| 3030 | for( i = 0; i < ROUNDS; i++ ) |
| 3031 | TEST_ASSERT( memcmp( message[i], received[i], MSGLEN ) == 0 ); |
| 3032 | |
| 3033 | exit: |
| 3034 | |
| 3035 | mbedtls_mock_socket_close( &client ); |
| 3036 | mbedtls_mock_socket_close( &server ); |
| 3037 | } |
| 3038 | /* END_CASE */ |
| 3039 | |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 3040 | /* BEGIN_CASE */ |
| 3041 | void ssl_message_queue_sanity( ) |
| 3042 | { |
| 3043 | mbedtls_test_message_queue queue; |
| 3044 | |
| 3045 | /* Trying to push/pull to an empty queue */ |
| 3046 | TEST_ASSERT( mbedtls_test_message_queue_push_info( NULL, 1 ) |
| 3047 | == MBEDTLS_TEST_ERROR_ARG_NULL ); |
| 3048 | TEST_ASSERT( mbedtls_test_message_queue_pop_info( NULL, 1 ) |
| 3049 | == MBEDTLS_TEST_ERROR_ARG_NULL ); |
| 3050 | |
Andrzej Kurek | 89bdc58 | 2020-03-09 06:29:43 -0400 | [diff] [blame] | 3051 | TEST_ASSERT( mbedtls_test_message_queue_setup( &queue, 3 ) == 0 ); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 3052 | TEST_ASSERT( queue.capacity == 3 ); |
| 3053 | TEST_ASSERT( queue.num == 0 ); |
| 3054 | |
| 3055 | exit: |
| 3056 | mbedtls_test_message_queue_free( &queue ); |
| 3057 | } |
| 3058 | /* END_CASE */ |
| 3059 | |
| 3060 | /* BEGIN_CASE */ |
| 3061 | void ssl_message_queue_basic( ) |
| 3062 | { |
| 3063 | mbedtls_test_message_queue queue; |
| 3064 | |
Andrzej Kurek | 89bdc58 | 2020-03-09 06:29:43 -0400 | [diff] [blame] | 3065 | TEST_ASSERT( mbedtls_test_message_queue_setup( &queue, 3 ) == 0 ); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 3066 | |
| 3067 | /* Sanity test - 3 pushes and 3 pops with sufficient space */ |
| 3068 | TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 ); |
| 3069 | TEST_ASSERT( queue.capacity == 3 ); |
| 3070 | TEST_ASSERT( queue.num == 1 ); |
| 3071 | TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 ); |
| 3072 | TEST_ASSERT( queue.capacity == 3 ); |
| 3073 | TEST_ASSERT( queue.num == 2 ); |
| 3074 | TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 2 ) == 2 ); |
| 3075 | TEST_ASSERT( queue.capacity == 3 ); |
| 3076 | TEST_ASSERT( queue.num == 3 ); |
| 3077 | |
| 3078 | TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 ); |
| 3079 | TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 ); |
| 3080 | TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 2 ) == 2 ); |
| 3081 | |
| 3082 | exit: |
| 3083 | mbedtls_test_message_queue_free( &queue ); |
| 3084 | } |
| 3085 | /* END_CASE */ |
| 3086 | |
| 3087 | /* BEGIN_CASE */ |
| 3088 | void ssl_message_queue_overflow_underflow( ) |
| 3089 | { |
| 3090 | mbedtls_test_message_queue queue; |
| 3091 | |
Andrzej Kurek | 89bdc58 | 2020-03-09 06:29:43 -0400 | [diff] [blame] | 3092 | TEST_ASSERT( mbedtls_test_message_queue_setup( &queue, 3 ) == 0 ); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 3093 | |
| 3094 | /* 4 pushes (last one with an error), 4 pops (last one with an error) */ |
| 3095 | TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 ); |
| 3096 | TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 ); |
| 3097 | TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 2 ) == 2 ); |
| 3098 | TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 3 ) |
Andrzej Kurek | f46b912 | 2020-02-07 08:19:00 -0500 | [diff] [blame] | 3099 | == MBEDTLS_ERR_SSL_WANT_WRITE ); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 3100 | |
| 3101 | TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 ); |
| 3102 | TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 ); |
| 3103 | TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 2 ) == 2 ); |
| 3104 | |
| 3105 | TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) |
Andrzej Kurek | f46b912 | 2020-02-07 08:19:00 -0500 | [diff] [blame] | 3106 | == MBEDTLS_ERR_SSL_WANT_READ ); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 3107 | |
| 3108 | exit: |
| 3109 | mbedtls_test_message_queue_free( &queue ); |
| 3110 | } |
| 3111 | /* END_CASE */ |
| 3112 | |
| 3113 | /* BEGIN_CASE */ |
| 3114 | void ssl_message_queue_interleaved( ) |
| 3115 | { |
| 3116 | mbedtls_test_message_queue queue; |
| 3117 | |
Andrzej Kurek | 89bdc58 | 2020-03-09 06:29:43 -0400 | [diff] [blame] | 3118 | TEST_ASSERT( mbedtls_test_message_queue_setup( &queue, 3 ) == 0 ); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 3119 | |
| 3120 | /* Interleaved test - [2 pushes, 1 pop] twice, and then two pops |
| 3121 | * (to wrap around the buffer) */ |
| 3122 | TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 ); |
| 3123 | TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 ); |
| 3124 | |
| 3125 | TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 ); |
| 3126 | |
| 3127 | TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 2 ) == 2 ); |
| 3128 | TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 3 ) == 3 ); |
| 3129 | |
| 3130 | TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 ); |
| 3131 | TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 2 ) == 2 ); |
| 3132 | |
| 3133 | TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 5 ) == 5 ); |
| 3134 | TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 8 ) == 8 ); |
| 3135 | |
| 3136 | TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 3 ) == 3 ); |
| 3137 | |
| 3138 | TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 5 ) == 5 ); |
| 3139 | |
| 3140 | TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 8 ) == 8 ); |
| 3141 | |
| 3142 | exit: |
| 3143 | mbedtls_test_message_queue_free( &queue ); |
| 3144 | } |
| 3145 | /* END_CASE */ |
| 3146 | |
| 3147 | /* BEGIN_CASE */ |
| 3148 | void ssl_message_queue_insufficient_buffer( ) |
| 3149 | { |
| 3150 | mbedtls_test_message_queue queue; |
| 3151 | size_t message_len = 10; |
| 3152 | size_t buffer_len = 5; |
| 3153 | |
Andrzej Kurek | 89bdc58 | 2020-03-09 06:29:43 -0400 | [diff] [blame] | 3154 | TEST_ASSERT( mbedtls_test_message_queue_setup( &queue, 1 ) == 0 ); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 3155 | |
| 3156 | /* Popping without a sufficient buffer */ |
| 3157 | TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, message_len ) |
| 3158 | == (int) message_len ); |
| 3159 | TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, buffer_len ) |
| 3160 | == (int) buffer_len ); |
| 3161 | exit: |
| 3162 | mbedtls_test_message_queue_free( &queue ); |
| 3163 | } |
| 3164 | /* END_CASE */ |
| 3165 | |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3166 | /* BEGIN_CASE */ |
| 3167 | void ssl_message_mock_uninitialized( ) |
| 3168 | { |
| 3169 | enum { MSGLEN = 10 }; |
Shawn Carey | 03092f5 | 2021-05-13 10:38:32 -0400 | [diff] [blame] | 3170 | unsigned char message[MSGLEN] = {0}, received[MSGLEN]; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3171 | mbedtls_mock_socket client, server; |
| 3172 | mbedtls_test_message_queue server_queue, client_queue; |
| 3173 | mbedtls_test_message_socket_context server_context, client_context; |
Andrzej Kurek | 45916ba | 2020-03-05 14:46:22 -0500 | [diff] [blame] | 3174 | mbedtls_message_socket_init( &server_context ); |
| 3175 | mbedtls_message_socket_init( &client_context ); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3176 | |
| 3177 | /* Send with a NULL context */ |
| 3178 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( NULL, message, MSGLEN ) |
| 3179 | == MBEDTLS_TEST_ERROR_CONTEXT_ERROR ); |
| 3180 | |
| 3181 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( NULL, message, MSGLEN ) |
| 3182 | == MBEDTLS_TEST_ERROR_CONTEXT_ERROR ); |
| 3183 | |
| 3184 | TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 1, |
| 3185 | &server, |
| 3186 | &server_context ) == 0 ); |
| 3187 | |
| 3188 | TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 1, |
| 3189 | &client, |
| 3190 | &client_context ) == 0 ); |
| 3191 | |
| 3192 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, MSGLEN ) |
| 3193 | == MBEDTLS_TEST_ERROR_SEND_FAILED ); |
| 3194 | |
| 3195 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN ) |
Andrzej Kurek | f46b912 | 2020-02-07 08:19:00 -0500 | [diff] [blame] | 3196 | == MBEDTLS_ERR_SSL_WANT_READ ); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3197 | |
| 3198 | /* Push directly to a queue to later simulate a disconnected behavior */ |
| 3199 | TEST_ASSERT( mbedtls_test_message_queue_push_info( &server_queue, MSGLEN ) |
| 3200 | == MSGLEN ); |
| 3201 | |
| 3202 | /* Test if there's an error when trying to read from a disconnected |
| 3203 | * socket */ |
| 3204 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN ) |
| 3205 | == MBEDTLS_TEST_ERROR_RECV_FAILED ); |
| 3206 | exit: |
| 3207 | mbedtls_message_socket_close( &server_context ); |
| 3208 | mbedtls_message_socket_close( &client_context ); |
| 3209 | } |
| 3210 | /* END_CASE */ |
| 3211 | |
| 3212 | /* BEGIN_CASE */ |
| 3213 | void ssl_message_mock_basic( ) |
| 3214 | { |
| 3215 | enum { MSGLEN = 10 }; |
| 3216 | unsigned char message[MSGLEN], received[MSGLEN]; |
| 3217 | mbedtls_mock_socket client, server; |
| 3218 | unsigned i; |
| 3219 | mbedtls_test_message_queue server_queue, client_queue; |
| 3220 | mbedtls_test_message_socket_context server_context, client_context; |
Andrzej Kurek | 45916ba | 2020-03-05 14:46:22 -0500 | [diff] [blame] | 3221 | mbedtls_message_socket_init( &server_context ); |
| 3222 | mbedtls_message_socket_init( &client_context ); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3223 | |
| 3224 | TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 1, |
| 3225 | &server, |
| 3226 | &server_context ) == 0 ); |
| 3227 | |
| 3228 | TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 1, |
| 3229 | &client, |
| 3230 | &client_context ) == 0 ); |
| 3231 | |
| 3232 | /* Fill up the buffer with structured data so that unwanted changes |
| 3233 | * can be detected */ |
| 3234 | for( i = 0; i < MSGLEN; i++ ) |
| 3235 | { |
| 3236 | message[i] = i & 0xFF; |
| 3237 | } |
| 3238 | TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, |
| 3239 | MSGLEN ) ); |
| 3240 | |
| 3241 | /* Send the message to the server */ |
| 3242 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, |
| 3243 | MSGLEN ) == MSGLEN ); |
| 3244 | |
| 3245 | /* Read from the server */ |
| 3246 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN ) |
| 3247 | == MSGLEN ); |
| 3248 | |
| 3249 | TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 ); |
| 3250 | memset( received, 0, MSGLEN ); |
| 3251 | |
| 3252 | /* Send the message to the client */ |
| 3253 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &server_context, message, |
| 3254 | MSGLEN ) == MSGLEN ); |
| 3255 | |
| 3256 | /* Read from the client */ |
| 3257 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &client_context, received, MSGLEN ) |
| 3258 | == MSGLEN ); |
| 3259 | TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 ); |
| 3260 | |
| 3261 | exit: |
| 3262 | mbedtls_message_socket_close( &server_context ); |
| 3263 | mbedtls_message_socket_close( &client_context ); |
| 3264 | } |
| 3265 | /* END_CASE */ |
| 3266 | |
| 3267 | /* BEGIN_CASE */ |
| 3268 | void ssl_message_mock_queue_overflow_underflow( ) |
| 3269 | { |
| 3270 | enum { MSGLEN = 10 }; |
| 3271 | unsigned char message[MSGLEN], received[MSGLEN]; |
| 3272 | mbedtls_mock_socket client, server; |
| 3273 | unsigned i; |
| 3274 | mbedtls_test_message_queue server_queue, client_queue; |
| 3275 | mbedtls_test_message_socket_context server_context, client_context; |
Andrzej Kurek | 45916ba | 2020-03-05 14:46:22 -0500 | [diff] [blame] | 3276 | mbedtls_message_socket_init( &server_context ); |
| 3277 | mbedtls_message_socket_init( &client_context ); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3278 | |
| 3279 | TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 2, |
| 3280 | &server, |
| 3281 | &server_context ) == 0 ); |
| 3282 | |
| 3283 | TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 2, |
| 3284 | &client, |
| 3285 | &client_context ) == 0 ); |
| 3286 | |
| 3287 | /* Fill up the buffer with structured data so that unwanted changes |
| 3288 | * can be detected */ |
| 3289 | for( i = 0; i < MSGLEN; i++ ) |
| 3290 | { |
| 3291 | message[i] = i & 0xFF; |
| 3292 | } |
| 3293 | TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, |
| 3294 | MSGLEN*2 ) ); |
| 3295 | |
| 3296 | /* Send three message to the server, last one with an error */ |
| 3297 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, |
| 3298 | MSGLEN - 1 ) == MSGLEN - 1 ); |
| 3299 | |
| 3300 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, |
| 3301 | MSGLEN ) == MSGLEN ); |
| 3302 | |
| 3303 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, |
| 3304 | MSGLEN ) |
Andrzej Kurek | f46b912 | 2020-02-07 08:19:00 -0500 | [diff] [blame] | 3305 | == MBEDTLS_ERR_SSL_WANT_WRITE ); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3306 | |
| 3307 | /* Read three messages from the server, last one with an error */ |
| 3308 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, |
| 3309 | MSGLEN - 1 ) == MSGLEN - 1 ); |
| 3310 | |
| 3311 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN ) |
| 3312 | == MSGLEN ); |
| 3313 | |
| 3314 | TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 ); |
| 3315 | |
| 3316 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN ) |
Andrzej Kurek | f46b912 | 2020-02-07 08:19:00 -0500 | [diff] [blame] | 3317 | == MBEDTLS_ERR_SSL_WANT_READ ); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3318 | |
| 3319 | exit: |
| 3320 | mbedtls_message_socket_close( &server_context ); |
| 3321 | mbedtls_message_socket_close( &client_context ); |
| 3322 | } |
| 3323 | /* END_CASE */ |
| 3324 | |
| 3325 | /* BEGIN_CASE */ |
| 3326 | void ssl_message_mock_socket_overflow( ) |
| 3327 | { |
| 3328 | enum { MSGLEN = 10 }; |
| 3329 | unsigned char message[MSGLEN], received[MSGLEN]; |
| 3330 | mbedtls_mock_socket client, server; |
| 3331 | unsigned i; |
| 3332 | mbedtls_test_message_queue server_queue, client_queue; |
| 3333 | mbedtls_test_message_socket_context server_context, client_context; |
Andrzej Kurek | 45916ba | 2020-03-05 14:46:22 -0500 | [diff] [blame] | 3334 | mbedtls_message_socket_init( &server_context ); |
| 3335 | mbedtls_message_socket_init( &client_context ); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3336 | |
| 3337 | TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 2, |
| 3338 | &server, |
| 3339 | &server_context ) == 0 ); |
| 3340 | |
| 3341 | TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 2, |
| 3342 | &client, |
| 3343 | &client_context ) == 0 ); |
| 3344 | |
| 3345 | /* Fill up the buffer with structured data so that unwanted changes |
| 3346 | * can be detected */ |
| 3347 | for( i = 0; i < MSGLEN; i++ ) |
| 3348 | { |
| 3349 | message[i] = i & 0xFF; |
| 3350 | } |
| 3351 | TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, |
| 3352 | MSGLEN ) ); |
| 3353 | |
| 3354 | /* Send two message to the server, second one with an error */ |
| 3355 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, |
| 3356 | MSGLEN ) == MSGLEN ); |
| 3357 | |
| 3358 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, |
| 3359 | MSGLEN ) |
| 3360 | == MBEDTLS_TEST_ERROR_SEND_FAILED ); |
| 3361 | |
| 3362 | /* Read the only message from the server */ |
| 3363 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN ) |
| 3364 | == MSGLEN ); |
| 3365 | |
| 3366 | TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 ); |
| 3367 | |
| 3368 | exit: |
| 3369 | mbedtls_message_socket_close( &server_context ); |
| 3370 | mbedtls_message_socket_close( &client_context ); |
| 3371 | } |
| 3372 | /* END_CASE */ |
| 3373 | |
| 3374 | /* BEGIN_CASE */ |
| 3375 | void ssl_message_mock_truncated( ) |
| 3376 | { |
| 3377 | enum { MSGLEN = 10 }; |
| 3378 | unsigned char message[MSGLEN], received[MSGLEN]; |
| 3379 | mbedtls_mock_socket client, server; |
| 3380 | unsigned i; |
| 3381 | mbedtls_test_message_queue server_queue, client_queue; |
| 3382 | mbedtls_test_message_socket_context server_context, client_context; |
Andrzej Kurek | 45916ba | 2020-03-05 14:46:22 -0500 | [diff] [blame] | 3383 | mbedtls_message_socket_init( &server_context ); |
| 3384 | mbedtls_message_socket_init( &client_context ); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3385 | |
| 3386 | TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 2, |
| 3387 | &server, |
| 3388 | &server_context ) == 0 ); |
| 3389 | |
| 3390 | TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 2, |
| 3391 | &client, |
| 3392 | &client_context ) == 0 ); |
| 3393 | |
| 3394 | memset( received, 0, MSGLEN ); |
| 3395 | /* Fill up the buffer with structured data so that unwanted changes |
| 3396 | * can be detected */ |
| 3397 | for( i = 0; i < MSGLEN; i++ ) |
| 3398 | { |
| 3399 | message[i] = i & 0xFF; |
| 3400 | } |
| 3401 | TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, |
| 3402 | 2 * MSGLEN ) ); |
| 3403 | |
| 3404 | /* Send two messages to the server, the second one small enough to fit in the |
| 3405 | * receiver's buffer. */ |
| 3406 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, |
| 3407 | MSGLEN ) == MSGLEN ); |
| 3408 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, |
| 3409 | MSGLEN / 2 ) == MSGLEN / 2 ); |
| 3410 | /* Read a truncated message from the server */ |
| 3411 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN/2 ) |
| 3412 | == MSGLEN/2 ); |
| 3413 | |
| 3414 | /* Test that the first half of the message is valid, and second one isn't */ |
| 3415 | TEST_ASSERT( memcmp( message, received, MSGLEN/2 ) == 0 ); |
| 3416 | TEST_ASSERT( memcmp( message + MSGLEN/2, received + MSGLEN/2, MSGLEN/2 ) |
| 3417 | != 0 ); |
| 3418 | memset( received, 0, MSGLEN ); |
| 3419 | |
| 3420 | /* Read a full message from the server */ |
| 3421 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN/2 ) |
| 3422 | == MSGLEN / 2 ); |
| 3423 | |
| 3424 | /* Test that the first half of the message is valid */ |
| 3425 | TEST_ASSERT( memcmp( message, received, MSGLEN/2 ) == 0 ); |
| 3426 | |
| 3427 | exit: |
| 3428 | mbedtls_message_socket_close( &server_context ); |
| 3429 | mbedtls_message_socket_close( &client_context ); |
| 3430 | } |
| 3431 | /* END_CASE */ |
| 3432 | |
| 3433 | /* BEGIN_CASE */ |
| 3434 | void ssl_message_mock_socket_read_error( ) |
| 3435 | { |
| 3436 | enum { MSGLEN = 10 }; |
| 3437 | unsigned char message[MSGLEN], received[MSGLEN]; |
| 3438 | mbedtls_mock_socket client, server; |
| 3439 | unsigned i; |
| 3440 | mbedtls_test_message_queue server_queue, client_queue; |
| 3441 | mbedtls_test_message_socket_context server_context, client_context; |
Andrzej Kurek | 45916ba | 2020-03-05 14:46:22 -0500 | [diff] [blame] | 3442 | mbedtls_message_socket_init( &server_context ); |
| 3443 | mbedtls_message_socket_init( &client_context ); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3444 | |
| 3445 | TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 1, |
| 3446 | &server, |
| 3447 | &server_context ) == 0 ); |
| 3448 | |
| 3449 | TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 1, |
| 3450 | &client, |
| 3451 | &client_context ) == 0 ); |
| 3452 | |
| 3453 | /* Fill up the buffer with structured data so that unwanted changes |
| 3454 | * can be detected */ |
| 3455 | for( i = 0; i < MSGLEN; i++ ) |
| 3456 | { |
| 3457 | message[i] = i & 0xFF; |
| 3458 | } |
| 3459 | TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, |
| 3460 | MSGLEN ) ); |
| 3461 | |
| 3462 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, |
| 3463 | MSGLEN ) == MSGLEN ); |
| 3464 | |
| 3465 | /* Force a read error by disconnecting the socket by hand */ |
| 3466 | server.status = 0; |
| 3467 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN ) |
| 3468 | == MBEDTLS_TEST_ERROR_RECV_FAILED ); |
| 3469 | /* Return to a valid state */ |
| 3470 | server.status = MBEDTLS_MOCK_SOCKET_CONNECTED; |
| 3471 | |
| 3472 | memset( received, 0, sizeof( received ) ); |
| 3473 | |
| 3474 | /* Test that even though the server tried to read once disconnected, the |
| 3475 | * continuity is preserved */ |
| 3476 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN ) |
| 3477 | == MSGLEN ); |
| 3478 | |
| 3479 | TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 ); |
| 3480 | |
| 3481 | exit: |
| 3482 | mbedtls_message_socket_close( &server_context ); |
| 3483 | mbedtls_message_socket_close( &client_context ); |
| 3484 | } |
| 3485 | /* END_CASE */ |
| 3486 | |
| 3487 | /* BEGIN_CASE */ |
| 3488 | void ssl_message_mock_interleaved_one_way( ) |
| 3489 | { |
| 3490 | enum { MSGLEN = 10 }; |
| 3491 | unsigned char message[MSGLEN], received[MSGLEN]; |
| 3492 | mbedtls_mock_socket client, server; |
| 3493 | unsigned i; |
| 3494 | mbedtls_test_message_queue server_queue, client_queue; |
| 3495 | mbedtls_test_message_socket_context server_context, client_context; |
Andrzej Kurek | 45916ba | 2020-03-05 14:46:22 -0500 | [diff] [blame] | 3496 | mbedtls_message_socket_init( &server_context ); |
| 3497 | mbedtls_message_socket_init( &client_context ); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3498 | |
| 3499 | TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 3, |
| 3500 | &server, |
| 3501 | &server_context ) == 0 ); |
| 3502 | |
| 3503 | TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 3, |
| 3504 | &client, |
| 3505 | &client_context ) == 0 ); |
| 3506 | |
| 3507 | /* Fill up the buffer with structured data so that unwanted changes |
| 3508 | * can be detected */ |
| 3509 | for( i = 0; i < MSGLEN; i++ ) |
| 3510 | { |
| 3511 | message[i] = i & 0xFF; |
| 3512 | } |
| 3513 | TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, |
| 3514 | MSGLEN*3 ) ); |
| 3515 | |
| 3516 | /* Interleaved test - [2 sends, 1 read] twice, and then two reads |
| 3517 | * (to wrap around the buffer) */ |
| 3518 | for( i = 0; i < 2; i++ ) |
| 3519 | { |
| 3520 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, |
| 3521 | MSGLEN ) == MSGLEN ); |
| 3522 | |
| 3523 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, |
| 3524 | MSGLEN ) == MSGLEN ); |
| 3525 | |
| 3526 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, |
| 3527 | MSGLEN ) == MSGLEN ); |
| 3528 | TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 ); |
| 3529 | memset( received, 0, sizeof( received ) ); |
| 3530 | } |
| 3531 | |
| 3532 | for( i = 0; i < 2; i++ ) |
| 3533 | { |
| 3534 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, |
| 3535 | MSGLEN ) == MSGLEN ); |
| 3536 | |
| 3537 | TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 ); |
| 3538 | } |
| 3539 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN ) |
Andrzej Kurek | f46b912 | 2020-02-07 08:19:00 -0500 | [diff] [blame] | 3540 | == MBEDTLS_ERR_SSL_WANT_READ ); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3541 | exit: |
| 3542 | mbedtls_message_socket_close( &server_context ); |
| 3543 | mbedtls_message_socket_close( &client_context ); |
| 3544 | } |
| 3545 | /* END_CASE */ |
| 3546 | |
| 3547 | /* BEGIN_CASE */ |
| 3548 | void ssl_message_mock_interleaved_two_ways( ) |
| 3549 | { |
| 3550 | enum { MSGLEN = 10 }; |
| 3551 | unsigned char message[MSGLEN], received[MSGLEN]; |
| 3552 | mbedtls_mock_socket client, server; |
| 3553 | unsigned i; |
| 3554 | mbedtls_test_message_queue server_queue, client_queue; |
| 3555 | mbedtls_test_message_socket_context server_context, client_context; |
Andrzej Kurek | 45916ba | 2020-03-05 14:46:22 -0500 | [diff] [blame] | 3556 | mbedtls_message_socket_init( &server_context ); |
| 3557 | mbedtls_message_socket_init( &client_context ); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3558 | |
| 3559 | TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 3, |
| 3560 | &server, |
| 3561 | &server_context ) == 0 ); |
| 3562 | |
| 3563 | TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 3, |
| 3564 | &client, |
| 3565 | &client_context ) == 0 ); |
| 3566 | |
| 3567 | /* Fill up the buffer with structured data so that unwanted changes |
| 3568 | * can be detected */ |
| 3569 | for( i = 0; i < MSGLEN; i++ ) |
| 3570 | { |
| 3571 | message[i] = i & 0xFF; |
| 3572 | } |
| 3573 | TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, |
| 3574 | MSGLEN*3 ) ); |
| 3575 | |
| 3576 | /* Interleaved test - [2 sends, 1 read] twice, both ways, and then two reads |
| 3577 | * (to wrap around the buffer) both ways. */ |
| 3578 | for( i = 0; i < 2; i++ ) |
| 3579 | { |
| 3580 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, |
| 3581 | MSGLEN ) == MSGLEN ); |
| 3582 | |
| 3583 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, |
| 3584 | MSGLEN ) == MSGLEN ); |
| 3585 | |
| 3586 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &server_context, message, |
| 3587 | MSGLEN ) == MSGLEN ); |
| 3588 | |
| 3589 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &server_context, message, |
| 3590 | MSGLEN ) == MSGLEN ); |
| 3591 | |
| 3592 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, |
| 3593 | MSGLEN ) == MSGLEN ); |
| 3594 | |
| 3595 | TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 ); |
| 3596 | |
| 3597 | memset( received, 0, sizeof( received ) ); |
| 3598 | |
| 3599 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &client_context, received, |
| 3600 | MSGLEN ) == MSGLEN ); |
| 3601 | |
| 3602 | TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 ); |
| 3603 | |
| 3604 | memset( received, 0, sizeof( received ) ); |
| 3605 | } |
| 3606 | |
| 3607 | for( i = 0; i < 2; i++ ) |
| 3608 | { |
| 3609 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, |
| 3610 | MSGLEN ) == MSGLEN ); |
| 3611 | |
| 3612 | TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 ); |
| 3613 | memset( received, 0, sizeof( received ) ); |
| 3614 | |
| 3615 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &client_context, received, |
| 3616 | MSGLEN ) == MSGLEN ); |
| 3617 | |
| 3618 | TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 ); |
| 3619 | memset( received, 0, sizeof( received ) ); |
| 3620 | } |
| 3621 | |
| 3622 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN ) |
Andrzej Kurek | f46b912 | 2020-02-07 08:19:00 -0500 | [diff] [blame] | 3623 | == MBEDTLS_ERR_SSL_WANT_READ ); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3624 | |
| 3625 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &client_context, received, MSGLEN ) |
Andrzej Kurek | f46b912 | 2020-02-07 08:19:00 -0500 | [diff] [blame] | 3626 | == MBEDTLS_ERR_SSL_WANT_READ ); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3627 | exit: |
| 3628 | mbedtls_message_socket_close( &server_context ); |
| 3629 | mbedtls_message_socket_close( &client_context ); |
| 3630 | } |
| 3631 | /* END_CASE */ |
| 3632 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3633 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
Azim Khan | 5fcca46 | 2018-06-29 11:05:32 +0100 | [diff] [blame] | 3634 | void ssl_dtls_replay( data_t * prevs, data_t * new, int ret ) |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3635 | { |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 3636 | uint32_t len = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3637 | mbedtls_ssl_context ssl; |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 3638 | mbedtls_ssl_config conf; |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3639 | |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 3640 | mbedtls_ssl_init( &ssl ); |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 3641 | mbedtls_ssl_config_init( &conf ); |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 3642 | |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 3643 | TEST_ASSERT( mbedtls_ssl_config_defaults( &conf, |
| 3644 | MBEDTLS_SSL_IS_CLIENT, |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 3645 | MBEDTLS_SSL_TRANSPORT_DATAGRAM, |
| 3646 | MBEDTLS_SSL_PRESET_DEFAULT ) == 0 ); |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 3647 | TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 ); |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3648 | |
| 3649 | /* Read previous record numbers */ |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 3650 | for( len = 0; len < prevs->len; len += 6 ) |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3651 | { |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 3652 | memcpy( ssl.in_ctr + 2, prevs->x + len, 6 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3653 | mbedtls_ssl_dtls_replay_update( &ssl ); |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3654 | } |
| 3655 | |
| 3656 | /* Check new number */ |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 3657 | memcpy( ssl.in_ctr + 2, new->x, 6 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3658 | TEST_ASSERT( mbedtls_ssl_dtls_replay_check( &ssl ) == ret ); |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3659 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3660 | mbedtls_ssl_free( &ssl ); |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 3661 | mbedtls_ssl_config_free( &conf ); |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3662 | } |
| 3663 | /* END_CASE */ |
Hanno Becker | b25c0c7 | 2017-05-05 11:24:30 +0100 | [diff] [blame] | 3664 | |
| 3665 | /* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */ |
| 3666 | void ssl_set_hostname_twice( char *hostname0, char *hostname1 ) |
| 3667 | { |
| 3668 | mbedtls_ssl_context ssl; |
| 3669 | mbedtls_ssl_init( &ssl ); |
| 3670 | |
| 3671 | TEST_ASSERT( mbedtls_ssl_set_hostname( &ssl, hostname0 ) == 0 ); |
| 3672 | TEST_ASSERT( mbedtls_ssl_set_hostname( &ssl, hostname1 ) == 0 ); |
| 3673 | |
| 3674 | mbedtls_ssl_free( &ssl ); |
| 3675 | } |
Darryl Green | 11999bb | 2018-03-13 15:22:58 +0000 | [diff] [blame] | 3676 | /* END_CASE */ |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3677 | |
| 3678 | /* BEGIN_CASE */ |
| 3679 | void ssl_crypt_record( int cipher_type, int hash_id, |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 3680 | int etm, int tag_mode, int ver, |
| 3681 | int cid0_len, int cid1_len ) |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3682 | { |
| 3683 | /* |
| 3684 | * Test several record encryptions and decryptions |
| 3685 | * with plenty of space before and after the data |
| 3686 | * within the record buffer. |
| 3687 | */ |
| 3688 | |
| 3689 | int ret; |
| 3690 | int num_records = 16; |
| 3691 | mbedtls_ssl_context ssl; /* ONLY for debugging */ |
| 3692 | |
| 3693 | mbedtls_ssl_transform t0, t1; |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 3694 | unsigned char *buf = NULL; |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3695 | size_t const buflen = 512; |
| 3696 | mbedtls_record rec, rec_backup; |
| 3697 | |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 3698 | USE_PSA_INIT( ); |
| 3699 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3700 | mbedtls_ssl_init( &ssl ); |
| 3701 | mbedtls_ssl_transform_init( &t0 ); |
| 3702 | mbedtls_ssl_transform_init( &t1 ); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 3703 | ret = build_transforms( &t0, &t1, cipher_type, hash_id, |
| 3704 | etm, tag_mode, ver, |
| 3705 | (size_t) cid0_len, |
| 3706 | (size_t) cid1_len ); |
| 3707 | |
Przemyslaw Stekiel | 8c010eb | 2022-02-03 10:44:02 +0100 | [diff] [blame] | 3708 | TEST_ASSERT( ret == 0 ); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3709 | |
Hanno Becker | 3ee5421 | 2019-04-04 16:31:26 +0100 | [diff] [blame] | 3710 | TEST_ASSERT( ( buf = mbedtls_calloc( 1, buflen ) ) != NULL ); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3711 | |
| 3712 | while( num_records-- > 0 ) |
| 3713 | { |
| 3714 | mbedtls_ssl_transform *t_dec, *t_enc; |
| 3715 | /* Take turns in who's sending and who's receiving. */ |
| 3716 | if( num_records % 3 == 0 ) |
| 3717 | { |
| 3718 | t_dec = &t0; |
| 3719 | t_enc = &t1; |
| 3720 | } |
| 3721 | else |
| 3722 | { |
| 3723 | t_dec = &t1; |
| 3724 | t_enc = &t0; |
| 3725 | } |
| 3726 | |
| 3727 | /* |
| 3728 | * The record header affects the transformation in two ways: |
| 3729 | * 1) It determines the AEAD additional data |
| 3730 | * 2) The record counter sometimes determines the IV. |
| 3731 | * |
| 3732 | * Apart from that, the fields don't have influence. |
| 3733 | * In particular, it is currently not the responsibility |
| 3734 | * of ssl_encrypt/decrypt_buf to check if the transform |
| 3735 | * version matches the record version, or that the |
| 3736 | * type is sensible. |
| 3737 | */ |
| 3738 | |
| 3739 | memset( rec.ctr, num_records, sizeof( rec.ctr ) ); |
| 3740 | rec.type = 42; |
| 3741 | rec.ver[0] = num_records; |
| 3742 | rec.ver[1] = num_records; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3743 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 3744 | rec.cid_len = 0; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3745 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3746 | |
| 3747 | rec.buf = buf; |
| 3748 | rec.buf_len = buflen; |
| 3749 | rec.data_offset = 16; |
| 3750 | /* Make sure to vary the length to exercise different |
| 3751 | * paddings. */ |
| 3752 | rec.data_len = 1 + num_records; |
| 3753 | |
| 3754 | memset( rec.buf + rec.data_offset, 42, rec.data_len ); |
| 3755 | |
| 3756 | /* Make a copy for later comparison */ |
| 3757 | rec_backup = rec; |
| 3758 | |
| 3759 | /* Encrypt record */ |
| 3760 | ret = mbedtls_ssl_encrypt_buf( &ssl, t_enc, &rec, |
Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 3761 | mbedtls_test_rnd_std_rand, NULL ); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3762 | TEST_ASSERT( ret == 0 || ret == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 3763 | if( ret != 0 ) |
| 3764 | { |
| 3765 | continue; |
| 3766 | } |
| 3767 | |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 3768 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 3769 | if( rec.cid_len != 0 ) |
| 3770 | { |
| 3771 | /* DTLS 1.2 + CID hides the real content type and |
| 3772 | * uses a special CID content type in the protected |
| 3773 | * record. Double-check this. */ |
| 3774 | TEST_ASSERT( rec.type == MBEDTLS_SSL_MSG_CID ); |
| 3775 | } |
| 3776 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 3777 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3778 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 3779 | if( t_enc->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 ) |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 3780 | { |
| 3781 | /* TLS 1.3 hides the real content type and |
| 3782 | * always uses Application Data as the content type |
| 3783 | * for protected records. Double-check this. */ |
| 3784 | TEST_ASSERT( rec.type == MBEDTLS_SSL_MSG_APPLICATION_DATA ); |
| 3785 | } |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3786 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 3787 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3788 | /* Decrypt record with t_dec */ |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 3789 | ret = mbedtls_ssl_decrypt_buf( &ssl, t_dec, &rec ); |
| 3790 | TEST_ASSERT( ret == 0 ); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3791 | |
| 3792 | /* Compare results */ |
| 3793 | TEST_ASSERT( rec.type == rec_backup.type ); |
| 3794 | TEST_ASSERT( memcmp( rec.ctr, rec_backup.ctr, 8 ) == 0 ); |
| 3795 | TEST_ASSERT( rec.ver[0] == rec_backup.ver[0] ); |
| 3796 | TEST_ASSERT( rec.ver[1] == rec_backup.ver[1] ); |
| 3797 | TEST_ASSERT( rec.data_len == rec_backup.data_len ); |
| 3798 | TEST_ASSERT( rec.data_offset == rec_backup.data_offset ); |
| 3799 | TEST_ASSERT( memcmp( rec.buf + rec.data_offset, |
| 3800 | rec_backup.buf + rec_backup.data_offset, |
| 3801 | rec.data_len ) == 0 ); |
| 3802 | } |
| 3803 | |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 3804 | exit: |
| 3805 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3806 | /* Cleanup */ |
| 3807 | mbedtls_ssl_free( &ssl ); |
| 3808 | mbedtls_ssl_transform_free( &t0 ); |
| 3809 | mbedtls_ssl_transform_free( &t1 ); |
| 3810 | |
Hanno Becker | 3ee5421 | 2019-04-04 16:31:26 +0100 | [diff] [blame] | 3811 | mbedtls_free( buf ); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 3812 | USE_PSA_DONE( ); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3813 | } |
| 3814 | /* END_CASE */ |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3815 | |
| 3816 | /* BEGIN_CASE */ |
| 3817 | void ssl_crypt_record_small( int cipher_type, int hash_id, |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 3818 | int etm, int tag_mode, int ver, |
| 3819 | int cid0_len, int cid1_len ) |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3820 | { |
| 3821 | /* |
| 3822 | * Test pairs of encryption and decryption with an increasing |
| 3823 | * amount of space in the record buffer - in more detail: |
| 3824 | * 1) Try to encrypt with 0, 1, 2, ... bytes available |
| 3825 | * in front of the plaintext, and expect the encryption |
| 3826 | * to succeed starting from some offset. Always keep |
| 3827 | * enough space in the end of the buffer. |
| 3828 | * 2) Try to encrypt with 0, 1, 2, ... bytes available |
| 3829 | * at the end of the plaintext, and expect the encryption |
| 3830 | * to succeed starting from some offset. Always keep |
| 3831 | * enough space at the beginning of the buffer. |
| 3832 | * 3) Try to encrypt with 0, 1, 2, ... bytes available |
| 3833 | * both at the front and end of the plaintext, |
| 3834 | * and expect the encryption to succeed starting from |
| 3835 | * some offset. |
| 3836 | * |
| 3837 | * If encryption succeeds, check that decryption succeeds |
| 3838 | * and yields the original record. |
| 3839 | */ |
| 3840 | |
| 3841 | mbedtls_ssl_context ssl; /* ONLY for debugging */ |
| 3842 | |
| 3843 | mbedtls_ssl_transform t0, t1; |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 3844 | unsigned char *buf = NULL; |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 3845 | size_t const buflen = 256; |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3846 | mbedtls_record rec, rec_backup; |
| 3847 | |
| 3848 | int ret; |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 3849 | int mode; /* Mode 1, 2 or 3 as explained above */ |
| 3850 | size_t offset; /* Available space at beginning/end/both */ |
| 3851 | size_t threshold = 96; /* Maximum offset to test against */ |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3852 | |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 3853 | size_t default_pre_padding = 64; /* Pre-padding to use in mode 2 */ |
| 3854 | size_t default_post_padding = 128; /* Post-padding to use in mode 1 */ |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3855 | |
| 3856 | int seen_success; /* Indicates if in the current mode we've |
| 3857 | * already seen a successful test. */ |
| 3858 | |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 3859 | USE_PSA_INIT( ); |
| 3860 | |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3861 | mbedtls_ssl_init( &ssl ); |
| 3862 | mbedtls_ssl_transform_init( &t0 ); |
| 3863 | mbedtls_ssl_transform_init( &t1 ); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 3864 | ret = build_transforms( &t0, &t1, cipher_type, hash_id, |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 3865 | etm, tag_mode, ver, |
| 3866 | (size_t) cid0_len, |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 3867 | (size_t) cid1_len ); |
| 3868 | |
Przemyslaw Stekiel | 8c010eb | 2022-02-03 10:44:02 +0100 | [diff] [blame] | 3869 | TEST_ASSERT( ret == 0 ); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3870 | |
Hanno Becker | 3ee5421 | 2019-04-04 16:31:26 +0100 | [diff] [blame] | 3871 | TEST_ASSERT( ( buf = mbedtls_calloc( 1, buflen ) ) != NULL ); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3872 | |
| 3873 | for( mode=1; mode <= 3; mode++ ) |
| 3874 | { |
| 3875 | seen_success = 0; |
| 3876 | for( offset=0; offset <= threshold; offset++ ) |
| 3877 | { |
| 3878 | mbedtls_ssl_transform *t_dec, *t_enc; |
Hanno Becker | 6c87b3f | 2019-04-29 17:24:44 +0100 | [diff] [blame] | 3879 | t_dec = &t0; |
| 3880 | t_enc = &t1; |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3881 | |
| 3882 | memset( rec.ctr, offset, sizeof( rec.ctr ) ); |
| 3883 | rec.type = 42; |
| 3884 | rec.ver[0] = offset; |
| 3885 | rec.ver[1] = offset; |
| 3886 | rec.buf = buf; |
| 3887 | rec.buf_len = buflen; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3888 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 3889 | rec.cid_len = 0; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3890 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3891 | |
| 3892 | switch( mode ) |
| 3893 | { |
| 3894 | case 1: /* Space in the beginning */ |
| 3895 | rec.data_offset = offset; |
| 3896 | rec.data_len = buflen - offset - default_post_padding; |
| 3897 | break; |
| 3898 | |
| 3899 | case 2: /* Space in the end */ |
| 3900 | rec.data_offset = default_pre_padding; |
| 3901 | rec.data_len = buflen - default_pre_padding - offset; |
| 3902 | break; |
| 3903 | |
| 3904 | case 3: /* Space in the beginning and end */ |
| 3905 | rec.data_offset = offset; |
| 3906 | rec.data_len = buflen - 2 * offset; |
| 3907 | break; |
| 3908 | |
| 3909 | default: |
| 3910 | TEST_ASSERT( 0 ); |
| 3911 | break; |
| 3912 | } |
| 3913 | |
| 3914 | memset( rec.buf + rec.data_offset, 42, rec.data_len ); |
| 3915 | |
| 3916 | /* Make a copy for later comparison */ |
| 3917 | rec_backup = rec; |
| 3918 | |
| 3919 | /* Encrypt record */ |
Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 3920 | ret = mbedtls_ssl_encrypt_buf( &ssl, t_enc, &rec, |
| 3921 | mbedtls_test_rnd_std_rand, NULL ); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3922 | |
| 3923 | if( ( mode == 1 || mode == 2 ) && seen_success ) |
| 3924 | { |
| 3925 | TEST_ASSERT( ret == 0 ); |
| 3926 | } |
| 3927 | else |
| 3928 | { |
| 3929 | TEST_ASSERT( ret == 0 || ret == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 3930 | if( ret == 0 ) |
| 3931 | seen_success = 1; |
| 3932 | } |
| 3933 | |
| 3934 | if( ret != 0 ) |
| 3935 | continue; |
| 3936 | |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 3937 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 3938 | if( rec.cid_len != 0 ) |
| 3939 | { |
| 3940 | /* DTLS 1.2 + CID hides the real content type and |
| 3941 | * uses a special CID content type in the protected |
| 3942 | * record. Double-check this. */ |
| 3943 | TEST_ASSERT( rec.type == MBEDTLS_SSL_MSG_CID ); |
| 3944 | } |
| 3945 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 3946 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3947 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 3948 | if( t_enc->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 ) |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 3949 | { |
| 3950 | /* TLS 1.3 hides the real content type and |
| 3951 | * always uses Application Data as the content type |
| 3952 | * for protected records. Double-check this. */ |
| 3953 | TEST_ASSERT( rec.type == MBEDTLS_SSL_MSG_APPLICATION_DATA ); |
| 3954 | } |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3955 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 3956 | |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3957 | /* Decrypt record with t_dec */ |
| 3958 | TEST_ASSERT( mbedtls_ssl_decrypt_buf( &ssl, t_dec, &rec ) == 0 ); |
| 3959 | |
| 3960 | /* Compare results */ |
| 3961 | TEST_ASSERT( rec.type == rec_backup.type ); |
| 3962 | TEST_ASSERT( memcmp( rec.ctr, rec_backup.ctr, 8 ) == 0 ); |
| 3963 | TEST_ASSERT( rec.ver[0] == rec_backup.ver[0] ); |
| 3964 | TEST_ASSERT( rec.ver[1] == rec_backup.ver[1] ); |
| 3965 | TEST_ASSERT( rec.data_len == rec_backup.data_len ); |
| 3966 | TEST_ASSERT( rec.data_offset == rec_backup.data_offset ); |
| 3967 | TEST_ASSERT( memcmp( rec.buf + rec.data_offset, |
| 3968 | rec_backup.buf + rec_backup.data_offset, |
| 3969 | rec.data_len ) == 0 ); |
| 3970 | } |
| 3971 | |
| 3972 | TEST_ASSERT( seen_success == 1 ); |
| 3973 | } |
| 3974 | |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 3975 | exit: |
| 3976 | |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3977 | /* Cleanup */ |
| 3978 | mbedtls_ssl_free( &ssl ); |
| 3979 | mbedtls_ssl_transform_free( &t0 ); |
| 3980 | mbedtls_ssl_transform_free( &t1 ); |
| 3981 | |
Hanno Becker | 3ee5421 | 2019-04-04 16:31:26 +0100 | [diff] [blame] | 3982 | mbedtls_free( buf ); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 3983 | USE_PSA_DONE( ); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3984 | } |
| 3985 | /* END_CASE */ |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 3986 | |
Przemyslaw Stekiel | f4facef | 2022-02-02 21:31:04 +0100 | [diff] [blame] | 3987 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC:MBEDTLS_AES_C:MBEDTLS_SSL_PROTO_TLS1_2 */ |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3988 | void ssl_decrypt_non_etm_cbc( int cipher_type, int hash_id, int trunc_hmac, |
Manuel Pégourié-Gonnard | 864abbf | 2020-07-21 10:37:14 +0200 | [diff] [blame] | 3989 | int length_selector ) |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3990 | { |
| 3991 | /* |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3992 | * Test record decryption for CBC without EtM, focused on the verification |
| 3993 | * of padding and MAC. |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3994 | * |
TRodziewicz | 299510e | 2021-07-09 16:55:11 +0200 | [diff] [blame] | 3995 | * Actually depends on TLS 1.2 and either AES, ARIA or Camellia, but since |
| 3996 | * the test framework doesn't support alternation in dependency statements, |
| 3997 | * just depend on AES. |
Manuel Pégourié-Gonnard | 864abbf | 2020-07-21 10:37:14 +0200 | [diff] [blame] | 3998 | * |
| 3999 | * The length_selector argument is interpreted as follows: |
| 4000 | * - if it's -1, the plaintext length is 0 and minimal padding is applied |
| 4001 | * - if it's -2, the plaintext length is 0 and maximal padding is applied |
| 4002 | * - otherwise it must be in [0, 255] and is padding_length from RFC 5246: |
| 4003 | * it's the length of the rest of the padding, that is, excluding the |
| 4004 | * byte that encodes the length. The minimal non-zero plaintext length |
| 4005 | * that gives this padding_length is automatically selected. |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 4006 | */ |
| 4007 | mbedtls_ssl_context ssl; /* ONLY for debugging */ |
| 4008 | mbedtls_ssl_transform t0, t1; |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 4009 | mbedtls_record rec, rec_save; |
| 4010 | unsigned char *buf = NULL, *buf_save = NULL; |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 4011 | size_t buflen, olen = 0; |
Manuel Pégourié-Gonnard | 864abbf | 2020-07-21 10:37:14 +0200 | [diff] [blame] | 4012 | size_t plaintext_len, block_size, i; |
Manuel Pégourié-Gonnard | e55653f | 2020-07-22 11:42:57 +0200 | [diff] [blame] | 4013 | unsigned char padlen; /* excluding the padding_length byte */ |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 4014 | unsigned char add_data[13]; |
| 4015 | unsigned char mac[MBEDTLS_MD_MAX_SIZE]; |
Neil Armstrong | cf8841a | 2022-02-24 11:17:45 +0100 | [diff] [blame] | 4016 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 4017 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 4018 | size_t sign_mac_length = 0; |
| 4019 | #endif |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 4020 | int exp_ret; |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 4021 | int ret; |
Manuel Pégourié-Gonnard | 4adc04a | 2020-07-16 10:00:48 +0200 | [diff] [blame] | 4022 | const unsigned char pad_max_len = 255; /* Per the standard */ |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 4023 | |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 4024 | USE_PSA_INIT( ); |
| 4025 | |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 4026 | mbedtls_ssl_init( &ssl ); |
| 4027 | mbedtls_ssl_transform_init( &t0 ); |
| 4028 | mbedtls_ssl_transform_init( &t1 ); |
| 4029 | |
| 4030 | /* Set up transforms with dummy keys */ |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 4031 | ret = build_transforms( &t0, &t1, cipher_type, hash_id, |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 4032 | 0, trunc_hmac, |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 4033 | MBEDTLS_SSL_VERSION_TLS1_2, |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 4034 | 0 , 0 ); |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 4035 | |
Przemyslaw Stekiel | 4a36dd3 | 2022-01-25 00:43:58 +0100 | [diff] [blame] | 4036 | TEST_ASSERT( ret == 0 ); |
| 4037 | |
Manuel Pégourié-Gonnard | 864abbf | 2020-07-21 10:37:14 +0200 | [diff] [blame] | 4038 | /* Determine padding/plaintext length */ |
| 4039 | TEST_ASSERT( length_selector >= -2 && length_selector <= 255 ); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 4040 | block_size = t0.ivlen; |
Manuel Pégourié-Gonnard | 864abbf | 2020-07-21 10:37:14 +0200 | [diff] [blame] | 4041 | if( length_selector < 0 ) |
| 4042 | { |
| 4043 | plaintext_len = 0; |
| 4044 | |
Manuel Pégourié-Gonnard | e55653f | 2020-07-22 11:42:57 +0200 | [diff] [blame] | 4045 | /* Minimal padding |
| 4046 | * The +1 is for the padding_length byte, not counted in padlen. */ |
Manuel Pégourié-Gonnard | 864abbf | 2020-07-21 10:37:14 +0200 | [diff] [blame] | 4047 | padlen = block_size - ( t0.maclen + 1 ) % block_size; |
| 4048 | |
| 4049 | /* Maximal padding? */ |
| 4050 | if( length_selector == -2 ) |
| 4051 | padlen += block_size * ( ( pad_max_len - padlen ) / block_size ); |
| 4052 | } |
| 4053 | else |
| 4054 | { |
| 4055 | padlen = length_selector; |
| 4056 | |
Manuel Pégourié-Gonnard | e55653f | 2020-07-22 11:42:57 +0200 | [diff] [blame] | 4057 | /* Minimal non-zero plaintext_length giving desired padding. |
| 4058 | * The +1 is for the padding_length byte, not counted in padlen. */ |
Manuel Pégourié-Gonnard | 864abbf | 2020-07-21 10:37:14 +0200 | [diff] [blame] | 4059 | plaintext_len = block_size - ( padlen + t0.maclen + 1 ) % block_size; |
| 4060 | } |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 4061 | |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 4062 | /* Prepare a buffer for record data */ |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 4063 | buflen = block_size |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 4064 | + plaintext_len |
| 4065 | + t0.maclen |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 4066 | + padlen + 1; |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 4067 | ASSERT_ALLOC( buf, buflen ); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 4068 | ASSERT_ALLOC( buf_save, buflen ); |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 4069 | |
| 4070 | /* Prepare a dummy record header */ |
| 4071 | memset( rec.ctr, 0, sizeof( rec.ctr ) ); |
| 4072 | rec.type = MBEDTLS_SSL_MSG_APPLICATION_DATA; |
Glenn Strauss | e3af4cb | 2022-03-15 03:23:42 -0400 | [diff] [blame] | 4073 | mbedtls_ssl_write_version( rec.ver, MBEDTLS_SSL_TRANSPORT_STREAM, |
| 4074 | MBEDTLS_SSL_VERSION_TLS1_2 ); |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 4075 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 4076 | rec.cid_len = 0; |
| 4077 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 4078 | |
| 4079 | /* Prepare dummy record content */ |
| 4080 | rec.buf = buf; |
| 4081 | rec.buf_len = buflen; |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 4082 | rec.data_offset = block_size; |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 4083 | rec.data_len = plaintext_len; |
| 4084 | memset( rec.buf + rec.data_offset, 42, rec.data_len ); |
| 4085 | |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 4086 | /* Serialized version of record header for MAC purposes */ |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 4087 | memcpy( add_data, rec.ctr, 8 ); |
| 4088 | add_data[8] = rec.type; |
| 4089 | add_data[9] = rec.ver[0]; |
| 4090 | add_data[10] = rec.ver[1]; |
| 4091 | add_data[11] = ( rec.data_len >> 8 ) & 0xff; |
| 4092 | add_data[12] = ( rec.data_len >> 0 ) & 0xff; |
| 4093 | |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 4094 | /* Set dummy IV */ |
| 4095 | memset( t0.iv_enc, 0x55, t0.ivlen ); |
| 4096 | memcpy( rec.buf, t0.iv_enc, t0.ivlen ); |
| 4097 | |
| 4098 | /* |
| 4099 | * Prepare a pre-encryption record (with MAC and padding), and save it. |
| 4100 | */ |
| 4101 | |
| 4102 | /* MAC with additional data */ |
Neil Armstrong | cf8841a | 2022-02-24 11:17:45 +0100 | [diff] [blame] | 4103 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 4104 | TEST_EQUAL( PSA_SUCCESS, psa_mac_sign_setup( &operation, |
| 4105 | t0.psa_mac_enc, |
| 4106 | t0.psa_mac_alg ) ); |
| 4107 | TEST_EQUAL( PSA_SUCCESS, psa_mac_update( &operation, add_data, 13 ) ); |
| 4108 | TEST_EQUAL( PSA_SUCCESS, psa_mac_update( &operation, |
| 4109 | rec.buf + rec.data_offset, |
| 4110 | rec.data_len ) ); |
| 4111 | TEST_EQUAL( PSA_SUCCESS, psa_mac_sign_finish( &operation, |
| 4112 | mac, MBEDTLS_MD_MAX_SIZE, |
| 4113 | &sign_mac_length ) ); |
| 4114 | #else |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 4115 | TEST_EQUAL( 0, mbedtls_md_hmac_update( &t0.md_ctx_enc, add_data, 13 ) ); |
| 4116 | TEST_EQUAL( 0, mbedtls_md_hmac_update( &t0.md_ctx_enc, |
| 4117 | rec.buf + rec.data_offset, |
| 4118 | rec.data_len ) ); |
| 4119 | TEST_EQUAL( 0, mbedtls_md_hmac_finish( &t0.md_ctx_enc, mac ) ); |
Neil Armstrong | cf8841a | 2022-02-24 11:17:45 +0100 | [diff] [blame] | 4120 | #endif |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 4121 | |
| 4122 | memcpy( rec.buf + rec.data_offset + rec.data_len, mac, t0.maclen ); |
| 4123 | rec.data_len += t0.maclen; |
| 4124 | |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 4125 | /* Pad */ |
| 4126 | memset( rec.buf + rec.data_offset + rec.data_len, padlen, padlen + 1 ); |
| 4127 | rec.data_len += padlen + 1; |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 4128 | |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 4129 | /* Save correct pre-encryption record */ |
| 4130 | rec_save = rec; |
| 4131 | rec_save.buf = buf_save; |
| 4132 | memcpy( buf_save, buf, buflen ); |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 4133 | |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 4134 | /* |
| 4135 | * Encrypt and decrypt the correct record, expecting success |
| 4136 | */ |
Przemyslaw Stekiel | 8c010eb | 2022-02-03 10:44:02 +0100 | [diff] [blame] | 4137 | TEST_EQUAL( 0, psa_cipher_encrypt_helper(&t0, t0.iv_enc, t0.ivlen, |
Przemyslaw Stekiel | f4facef | 2022-02-02 21:31:04 +0100 | [diff] [blame] | 4138 | rec.buf + rec.data_offset, rec.data_len, |
| 4139 | rec.buf + rec.data_offset, &olen ) ); |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 4140 | rec.data_offset -= t0.ivlen; |
| 4141 | rec.data_len += t0.ivlen; |
| 4142 | |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 4143 | TEST_EQUAL( 0, mbedtls_ssl_decrypt_buf( &ssl, &t1, &rec ) ); |
| 4144 | |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 4145 | /* |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 4146 | * Modify each byte of the pre-encryption record before encrypting and |
| 4147 | * decrypting it, expecting failure every time. |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 4148 | */ |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 4149 | for( i = block_size; i < buflen; i++ ) |
| 4150 | { |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 4151 | mbedtls_test_set_step( i ); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 4152 | |
| 4153 | /* Restore correct pre-encryption record */ |
| 4154 | rec = rec_save; |
| 4155 | rec.buf = buf; |
| 4156 | memcpy( buf, buf_save, buflen ); |
| 4157 | |
Manuel Pégourié-Gonnard | b51f044 | 2020-07-21 10:40:25 +0200 | [diff] [blame] | 4158 | /* Corrupt one byte of the data (could be plaintext, MAC or padding) */ |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 4159 | rec.buf[i] ^= 0x01; |
| 4160 | |
| 4161 | /* Encrypt */ |
Przemyslaw Stekiel | 8c010eb | 2022-02-03 10:44:02 +0100 | [diff] [blame] | 4162 | TEST_EQUAL( 0, psa_cipher_encrypt_helper(&t0, t0.iv_enc, t0.ivlen, |
Przemyslaw Stekiel | f4facef | 2022-02-02 21:31:04 +0100 | [diff] [blame] | 4163 | rec.buf + rec.data_offset, rec.data_len, |
| 4164 | rec.buf + rec.data_offset, &olen ) ); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 4165 | rec.data_offset -= t0.ivlen; |
| 4166 | rec.data_len += t0.ivlen; |
| 4167 | |
| 4168 | /* Decrypt and expect failure */ |
| 4169 | TEST_EQUAL( MBEDTLS_ERR_SSL_INVALID_MAC, |
| 4170 | mbedtls_ssl_decrypt_buf( &ssl, &t1, &rec ) ); |
| 4171 | } |
| 4172 | |
| 4173 | /* |
| 4174 | * Use larger values of the padding bytes - with small buffers, this tests |
| 4175 | * the case where the announced padlen would be larger than the buffer |
| 4176 | * (and before that, than the buffer minus the size of the MAC), to make |
| 4177 | * sure our padding checking code does not perform any out-of-bounds reads |
| 4178 | * in this case. (With larger buffers, ie when the plaintext is long or |
| 4179 | * maximal length padding is used, this is less relevant but still doesn't |
| 4180 | * hurt to test.) |
| 4181 | * |
| 4182 | * (Start the loop with correct padding, just to double-check that record |
| 4183 | * saving did work, and that we're overwriting the correct bytes.) |
| 4184 | */ |
Manuel Pégourié-Gonnard | 4adc04a | 2020-07-16 10:00:48 +0200 | [diff] [blame] | 4185 | for( i = padlen; i <= pad_max_len; i++ ) |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 4186 | { |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 4187 | mbedtls_test_set_step( i ); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 4188 | |
| 4189 | /* Restore correct pre-encryption record */ |
| 4190 | rec = rec_save; |
| 4191 | rec.buf = buf; |
| 4192 | memcpy( buf, buf_save, buflen ); |
| 4193 | |
| 4194 | /* Set padding bytes to new value */ |
| 4195 | memset( buf + buflen - padlen - 1, i, padlen + 1 ); |
| 4196 | |
| 4197 | /* Encrypt */ |
Przemyslaw Stekiel | 8c010eb | 2022-02-03 10:44:02 +0100 | [diff] [blame] | 4198 | TEST_EQUAL( 0, psa_cipher_encrypt_helper(&t0, t0.iv_enc, t0.ivlen, |
Przemyslaw Stekiel | f4facef | 2022-02-02 21:31:04 +0100 | [diff] [blame] | 4199 | rec.buf + rec.data_offset, rec.data_len, |
| 4200 | rec.buf + rec.data_offset, &olen ) ); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 4201 | rec.data_offset -= t0.ivlen; |
| 4202 | rec.data_len += t0.ivlen; |
| 4203 | |
| 4204 | /* Decrypt and expect failure except the first time */ |
| 4205 | exp_ret = ( i == padlen ) ? 0 : MBEDTLS_ERR_SSL_INVALID_MAC; |
| 4206 | TEST_EQUAL( exp_ret, mbedtls_ssl_decrypt_buf( &ssl, &t1, &rec ) ); |
| 4207 | } |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 4208 | |
| 4209 | exit: |
| 4210 | mbedtls_ssl_free( &ssl ); |
| 4211 | mbedtls_ssl_transform_free( &t0 ); |
| 4212 | mbedtls_ssl_transform_free( &t1 ); |
| 4213 | mbedtls_free( buf ); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 4214 | mbedtls_free( buf_save ); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 4215 | USE_PSA_DONE( ); |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 4216 | } |
| 4217 | /* END_CASE */ |
| 4218 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4219 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */ |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4220 | void ssl_tls13_hkdf_expand_label( int hash_alg, |
| 4221 | data_t *secret, |
| 4222 | int label_idx, |
| 4223 | data_t *ctx, |
| 4224 | int desired_length, |
| 4225 | data_t *expected ) |
Hanno Becker | 39ff492 | 2020-08-21 13:36:56 +0100 | [diff] [blame] | 4226 | { |
| 4227 | unsigned char dst[ 100 ]; |
| 4228 | |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 4229 | unsigned char const *lbl = NULL; |
| 4230 | size_t lbl_len; |
Xiaofei Bai | d25fab6 | 2021-12-02 06:36:27 +0000 | [diff] [blame] | 4231 | #define MBEDTLS_SSL_TLS1_3_LABEL( name, string ) \ |
| 4232 | if( label_idx == (int) tls13_label_ ## name ) \ |
| 4233 | { \ |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4234 | lbl = mbedtls_ssl_tls13_labels.name; \ |
| 4235 | lbl_len = sizeof( mbedtls_ssl_tls13_labels.name ); \ |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 4236 | } |
| 4237 | MBEDTLS_SSL_TLS1_3_LABEL_LIST |
| 4238 | #undef MBEDTLS_SSL_TLS1_3_LABEL |
| 4239 | TEST_ASSERT( lbl != NULL ); |
Hanno Becker | 39ff492 | 2020-08-21 13:36:56 +0100 | [diff] [blame] | 4240 | |
| 4241 | /* Check sanity of test parameters. */ |
| 4242 | TEST_ASSERT( (size_t) desired_length <= sizeof(dst) ); |
| 4243 | TEST_ASSERT( (size_t) desired_length == expected->len ); |
| 4244 | |
Gabor Mezei | 5d9a1fe | 2022-03-24 17:49:14 +0100 | [diff] [blame] | 4245 | PSA_INIT( ); |
Gabor Mezei | 892c4aa | 2022-03-21 12:21:43 +0100 | [diff] [blame] | 4246 | |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4247 | TEST_ASSERT( mbedtls_ssl_tls13_hkdf_expand_label( |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 4248 | (psa_algorithm_t) hash_alg, |
Hanno Becker | 39ff492 | 2020-08-21 13:36:56 +0100 | [diff] [blame] | 4249 | secret->x, secret->len, |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 4250 | lbl, lbl_len, |
Hanno Becker | 39ff492 | 2020-08-21 13:36:56 +0100 | [diff] [blame] | 4251 | ctx->x, ctx->len, |
| 4252 | dst, desired_length ) == 0 ); |
| 4253 | |
Hanno Becker | fb08096 | 2020-09-08 10:58:42 +0100 | [diff] [blame] | 4254 | ASSERT_COMPARE( dst, (size_t) desired_length, |
| 4255 | expected->x, (size_t) expected->len ); |
Gabor Mezei | 892c4aa | 2022-03-21 12:21:43 +0100 | [diff] [blame] | 4256 | |
Gabor Mezei | 5d9a1fe | 2022-03-24 17:49:14 +0100 | [diff] [blame] | 4257 | PSA_DONE( ); |
Hanno Becker | 39ff492 | 2020-08-21 13:36:56 +0100 | [diff] [blame] | 4258 | } |
| 4259 | /* END_CASE */ |
| 4260 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4261 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */ |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4262 | void ssl_tls13_traffic_key_generation( int hash_alg, |
| 4263 | data_t *server_secret, |
| 4264 | data_t *client_secret, |
| 4265 | int desired_iv_len, |
| 4266 | int desired_key_len, |
| 4267 | data_t *expected_server_write_key, |
| 4268 | data_t *expected_server_write_iv, |
| 4269 | data_t *expected_client_write_key, |
| 4270 | data_t *expected_client_write_iv ) |
Hanno Becker | 19498f8 | 2020-08-21 13:37:08 +0100 | [diff] [blame] | 4271 | { |
| 4272 | mbedtls_ssl_key_set keys; |
| 4273 | |
| 4274 | /* Check sanity of test parameters. */ |
| 4275 | TEST_ASSERT( client_secret->len == server_secret->len ); |
| 4276 | TEST_ASSERT( expected_client_write_iv->len == expected_server_write_iv->len && |
| 4277 | expected_client_write_iv->len == (size_t) desired_iv_len ); |
| 4278 | TEST_ASSERT( expected_client_write_key->len == expected_server_write_key->len && |
| 4279 | expected_client_write_key->len == (size_t) desired_key_len ); |
| 4280 | |
Gabor Mezei | 5d9a1fe | 2022-03-24 17:49:14 +0100 | [diff] [blame] | 4281 | PSA_INIT( ); |
Gabor Mezei | 892c4aa | 2022-03-21 12:21:43 +0100 | [diff] [blame] | 4282 | |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4283 | TEST_ASSERT( mbedtls_ssl_tls13_make_traffic_keys( |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 4284 | (psa_algorithm_t) hash_alg, |
Hanno Becker | 19498f8 | 2020-08-21 13:37:08 +0100 | [diff] [blame] | 4285 | client_secret->x, |
| 4286 | server_secret->x, |
| 4287 | client_secret->len /* == server_secret->len */, |
| 4288 | desired_key_len, desired_iv_len, |
| 4289 | &keys ) == 0 ); |
| 4290 | |
Hanno Becker | fb08096 | 2020-09-08 10:58:42 +0100 | [diff] [blame] | 4291 | ASSERT_COMPARE( keys.client_write_key, |
Hanno Becker | 493ea7f | 2020-09-08 11:01:00 +0100 | [diff] [blame] | 4292 | keys.key_len, |
Hanno Becker | fb08096 | 2020-09-08 10:58:42 +0100 | [diff] [blame] | 4293 | expected_client_write_key->x, |
| 4294 | (size_t) desired_key_len ); |
| 4295 | ASSERT_COMPARE( keys.server_write_key, |
Hanno Becker | 493ea7f | 2020-09-08 11:01:00 +0100 | [diff] [blame] | 4296 | keys.key_len, |
Hanno Becker | fb08096 | 2020-09-08 10:58:42 +0100 | [diff] [blame] | 4297 | expected_server_write_key->x, |
| 4298 | (size_t) desired_key_len ); |
| 4299 | ASSERT_COMPARE( keys.client_write_iv, |
Hanno Becker | 493ea7f | 2020-09-08 11:01:00 +0100 | [diff] [blame] | 4300 | keys.iv_len, |
Hanno Becker | fb08096 | 2020-09-08 10:58:42 +0100 | [diff] [blame] | 4301 | expected_client_write_iv->x, |
| 4302 | (size_t) desired_iv_len ); |
| 4303 | ASSERT_COMPARE( keys.server_write_iv, |
Hanno Becker | 493ea7f | 2020-09-08 11:01:00 +0100 | [diff] [blame] | 4304 | keys.iv_len, |
Hanno Becker | fb08096 | 2020-09-08 10:58:42 +0100 | [diff] [blame] | 4305 | expected_server_write_iv->x, |
| 4306 | (size_t) desired_iv_len ); |
Gabor Mezei | 892c4aa | 2022-03-21 12:21:43 +0100 | [diff] [blame] | 4307 | |
Gabor Mezei | 5d9a1fe | 2022-03-24 17:49:14 +0100 | [diff] [blame] | 4308 | PSA_DONE( ); |
Hanno Becker | 19498f8 | 2020-08-21 13:37:08 +0100 | [diff] [blame] | 4309 | } |
| 4310 | /* END_CASE */ |
| 4311 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4312 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */ |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4313 | void ssl_tls13_derive_secret( int hash_alg, |
| 4314 | data_t *secret, |
| 4315 | int label_idx, |
| 4316 | data_t *ctx, |
| 4317 | int desired_length, |
| 4318 | int already_hashed, |
| 4319 | data_t *expected ) |
Hanno Becker | e4849d1 | 2020-08-21 14:14:14 +0100 | [diff] [blame] | 4320 | { |
| 4321 | unsigned char dst[ 100 ]; |
| 4322 | |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 4323 | unsigned char const *lbl = NULL; |
| 4324 | size_t lbl_len; |
Xiaofei Bai | d25fab6 | 2021-12-02 06:36:27 +0000 | [diff] [blame] | 4325 | #define MBEDTLS_SSL_TLS1_3_LABEL( name, string ) \ |
| 4326 | if( label_idx == (int) tls13_label_ ## name ) \ |
| 4327 | { \ |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4328 | lbl = mbedtls_ssl_tls13_labels.name; \ |
| 4329 | lbl_len = sizeof( mbedtls_ssl_tls13_labels.name ); \ |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 4330 | } |
| 4331 | MBEDTLS_SSL_TLS1_3_LABEL_LIST |
| 4332 | #undef MBEDTLS_SSL_TLS1_3_LABEL |
| 4333 | TEST_ASSERT( lbl != NULL ); |
| 4334 | |
Hanno Becker | e4849d1 | 2020-08-21 14:14:14 +0100 | [diff] [blame] | 4335 | /* Check sanity of test parameters. */ |
| 4336 | TEST_ASSERT( (size_t) desired_length <= sizeof(dst) ); |
| 4337 | TEST_ASSERT( (size_t) desired_length == expected->len ); |
| 4338 | |
Gabor Mezei | 5d9a1fe | 2022-03-24 17:49:14 +0100 | [diff] [blame] | 4339 | PSA_INIT( ); |
Gabor Mezei | 892c4aa | 2022-03-21 12:21:43 +0100 | [diff] [blame] | 4340 | |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4341 | TEST_ASSERT( mbedtls_ssl_tls13_derive_secret( |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 4342 | (psa_algorithm_t) hash_alg, |
Hanno Becker | e4849d1 | 2020-08-21 14:14:14 +0100 | [diff] [blame] | 4343 | secret->x, secret->len, |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 4344 | lbl, lbl_len, |
Hanno Becker | e4849d1 | 2020-08-21 14:14:14 +0100 | [diff] [blame] | 4345 | ctx->x, ctx->len, |
| 4346 | already_hashed, |
| 4347 | dst, desired_length ) == 0 ); |
| 4348 | |
Hanno Becker | fb08096 | 2020-09-08 10:58:42 +0100 | [diff] [blame] | 4349 | ASSERT_COMPARE( dst, desired_length, |
| 4350 | expected->x, desired_length ); |
Gabor Mezei | 892c4aa | 2022-03-21 12:21:43 +0100 | [diff] [blame] | 4351 | |
Gabor Mezei | 5d9a1fe | 2022-03-24 17:49:14 +0100 | [diff] [blame] | 4352 | PSA_DONE( ); |
Hanno Becker | e4849d1 | 2020-08-21 14:14:14 +0100 | [diff] [blame] | 4353 | } |
| 4354 | /* END_CASE */ |
| 4355 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4356 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */ |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4357 | void ssl_tls13_derive_early_secrets( int hash_alg, |
| 4358 | data_t *secret, |
| 4359 | data_t *transcript, |
| 4360 | data_t *traffic_expected, |
| 4361 | data_t *exporter_expected ) |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4362 | { |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4363 | mbedtls_ssl_tls13_early_secrets secrets; |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4364 | |
| 4365 | /* Double-check that we've passed sane parameters. */ |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 4366 | psa_algorithm_t alg = (psa_algorithm_t) hash_alg; |
| 4367 | size_t const hash_len = PSA_HASH_LENGTH( alg ); |
| 4368 | TEST_ASSERT( PSA_ALG_IS_HASH( alg ) && |
| 4369 | secret->len == hash_len && |
| 4370 | transcript->len == hash_len && |
| 4371 | traffic_expected->len == hash_len && |
| 4372 | exporter_expected->len == hash_len ); |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4373 | |
Gabor Mezei | 5d9a1fe | 2022-03-24 17:49:14 +0100 | [diff] [blame] | 4374 | PSA_INIT( ); |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4375 | |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4376 | TEST_ASSERT( mbedtls_ssl_tls13_derive_early_secrets( |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 4377 | alg, secret->x, transcript->x, transcript->len, |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4378 | &secrets ) == 0 ); |
| 4379 | |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 4380 | ASSERT_COMPARE( secrets.client_early_traffic_secret, hash_len, |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4381 | traffic_expected->x, traffic_expected->len ); |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 4382 | ASSERT_COMPARE( secrets.early_exporter_master_secret, hash_len, |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4383 | exporter_expected->x, exporter_expected->len ); |
Gabor Mezei | 892c4aa | 2022-03-21 12:21:43 +0100 | [diff] [blame] | 4384 | |
Gabor Mezei | 5d9a1fe | 2022-03-24 17:49:14 +0100 | [diff] [blame] | 4385 | PSA_DONE( ); |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4386 | } |
| 4387 | /* END_CASE */ |
| 4388 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4389 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */ |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4390 | void ssl_tls13_derive_handshake_secrets( int hash_alg, |
| 4391 | data_t *secret, |
| 4392 | data_t *transcript, |
| 4393 | data_t *client_expected, |
| 4394 | data_t *server_expected ) |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4395 | { |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4396 | mbedtls_ssl_tls13_handshake_secrets secrets; |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4397 | |
| 4398 | /* Double-check that we've passed sane parameters. */ |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 4399 | psa_algorithm_t alg = (psa_algorithm_t) hash_alg; |
| 4400 | size_t const hash_len = PSA_HASH_LENGTH( alg ); |
| 4401 | TEST_ASSERT( PSA_ALG_IS_HASH( alg ) && |
| 4402 | secret->len == hash_len && |
| 4403 | transcript->len == hash_len && |
| 4404 | client_expected->len == hash_len && |
| 4405 | server_expected->len == hash_len ); |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4406 | |
Gabor Mezei | 5d9a1fe | 2022-03-24 17:49:14 +0100 | [diff] [blame] | 4407 | PSA_INIT( ); |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4408 | |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4409 | TEST_ASSERT( mbedtls_ssl_tls13_derive_handshake_secrets( |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 4410 | alg, secret->x, transcript->x, transcript->len, |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4411 | &secrets ) == 0 ); |
| 4412 | |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 4413 | ASSERT_COMPARE( secrets.client_handshake_traffic_secret, hash_len, |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4414 | client_expected->x, client_expected->len ); |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 4415 | ASSERT_COMPARE( secrets.server_handshake_traffic_secret, hash_len, |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4416 | server_expected->x, server_expected->len ); |
Gabor Mezei | 892c4aa | 2022-03-21 12:21:43 +0100 | [diff] [blame] | 4417 | |
Gabor Mezei | 5d9a1fe | 2022-03-24 17:49:14 +0100 | [diff] [blame] | 4418 | PSA_DONE( ); |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4419 | } |
| 4420 | /* END_CASE */ |
| 4421 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4422 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */ |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4423 | void ssl_tls13_derive_application_secrets( int hash_alg, |
| 4424 | data_t *secret, |
| 4425 | data_t *transcript, |
| 4426 | data_t *client_expected, |
| 4427 | data_t *server_expected, |
| 4428 | data_t *exporter_expected ) |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4429 | { |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4430 | mbedtls_ssl_tls13_application_secrets secrets; |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4431 | |
| 4432 | /* Double-check that we've passed sane parameters. */ |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 4433 | psa_algorithm_t alg = (psa_algorithm_t) hash_alg; |
| 4434 | size_t const hash_len = PSA_HASH_LENGTH( alg ); |
| 4435 | TEST_ASSERT( PSA_ALG_IS_HASH( alg ) && |
| 4436 | secret->len == hash_len && |
| 4437 | transcript->len == hash_len && |
| 4438 | client_expected->len == hash_len && |
| 4439 | server_expected->len == hash_len && |
| 4440 | exporter_expected->len == hash_len ); |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4441 | |
Gabor Mezei | 5d9a1fe | 2022-03-24 17:49:14 +0100 | [diff] [blame] | 4442 | PSA_INIT( ); |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4443 | |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4444 | TEST_ASSERT( mbedtls_ssl_tls13_derive_application_secrets( |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 4445 | alg, secret->x, transcript->x, transcript->len, |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4446 | &secrets ) == 0 ); |
| 4447 | |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 4448 | ASSERT_COMPARE( secrets.client_application_traffic_secret_N, hash_len, |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4449 | client_expected->x, client_expected->len ); |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 4450 | ASSERT_COMPARE( secrets.server_application_traffic_secret_N, hash_len, |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4451 | server_expected->x, server_expected->len ); |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 4452 | ASSERT_COMPARE( secrets.exporter_master_secret, hash_len, |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4453 | exporter_expected->x, exporter_expected->len ); |
Gabor Mezei | 892c4aa | 2022-03-21 12:21:43 +0100 | [diff] [blame] | 4454 | |
Gabor Mezei | 5d9a1fe | 2022-03-24 17:49:14 +0100 | [diff] [blame] | 4455 | PSA_DONE( ); |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4456 | } |
| 4457 | /* END_CASE */ |
| 4458 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4459 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */ |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4460 | void ssl_tls13_derive_resumption_secrets( int hash_alg, |
| 4461 | data_t *secret, |
| 4462 | data_t *transcript, |
| 4463 | data_t *resumption_expected ) |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4464 | { |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4465 | mbedtls_ssl_tls13_application_secrets secrets; |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4466 | |
| 4467 | /* Double-check that we've passed sane parameters. */ |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 4468 | psa_algorithm_t alg = (psa_algorithm_t) hash_alg; |
| 4469 | size_t const hash_len = PSA_HASH_LENGTH( alg ); |
| 4470 | TEST_ASSERT( PSA_ALG_IS_HASH( alg ) && |
| 4471 | secret->len == hash_len && |
| 4472 | transcript->len == hash_len && |
| 4473 | resumption_expected->len == hash_len ); |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4474 | |
Gabor Mezei | 5d9a1fe | 2022-03-24 17:49:14 +0100 | [diff] [blame] | 4475 | PSA_INIT( ); |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4476 | |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4477 | TEST_ASSERT( mbedtls_ssl_tls13_derive_resumption_master_secret( |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 4478 | alg, secret->x, transcript->x, transcript->len, |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4479 | &secrets ) == 0 ); |
| 4480 | |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 4481 | ASSERT_COMPARE( secrets.resumption_master_secret, hash_len, |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4482 | resumption_expected->x, resumption_expected->len ); |
Gabor Mezei | 892c4aa | 2022-03-21 12:21:43 +0100 | [diff] [blame] | 4483 | |
Gabor Mezei | 5d9a1fe | 2022-03-24 17:49:14 +0100 | [diff] [blame] | 4484 | PSA_DONE( ); |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4485 | } |
| 4486 | /* END_CASE */ |
| 4487 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4488 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */ |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4489 | void ssl_tls13_create_psk_binder( int hash_alg, |
| 4490 | data_t *psk, |
| 4491 | int psk_type, |
| 4492 | data_t *transcript, |
| 4493 | data_t *binder_expected ) |
Hanno Becker | 55bc2c5 | 2021-05-24 06:53:52 +0100 | [diff] [blame] | 4494 | { |
| 4495 | unsigned char binder[ MBEDTLS_MD_MAX_SIZE ]; |
| 4496 | |
| 4497 | /* Double-check that we've passed sane parameters. */ |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 4498 | psa_algorithm_t alg = (psa_algorithm_t) hash_alg; |
| 4499 | size_t const hash_len = PSA_HASH_LENGTH( alg ); |
| 4500 | TEST_ASSERT( PSA_ALG_IS_HASH( alg ) && |
| 4501 | transcript->len == hash_len && |
| 4502 | binder_expected->len == hash_len ); |
Hanno Becker | 55bc2c5 | 2021-05-24 06:53:52 +0100 | [diff] [blame] | 4503 | |
Gabor Mezei | 5d9a1fe | 2022-03-24 17:49:14 +0100 | [diff] [blame] | 4504 | PSA_INIT( ); |
Hanno Becker | 55bc2c5 | 2021-05-24 06:53:52 +0100 | [diff] [blame] | 4505 | |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4506 | TEST_ASSERT( mbedtls_ssl_tls13_create_psk_binder( |
Hanno Becker | 55bc2c5 | 2021-05-24 06:53:52 +0100 | [diff] [blame] | 4507 | NULL, /* SSL context for debugging only */ |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 4508 | alg, |
Hanno Becker | 55bc2c5 | 2021-05-24 06:53:52 +0100 | [diff] [blame] | 4509 | psk->x, psk->len, |
| 4510 | psk_type, |
| 4511 | transcript->x, |
| 4512 | binder ) == 0 ); |
| 4513 | |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 4514 | ASSERT_COMPARE( binder, hash_len, |
Hanno Becker | 55bc2c5 | 2021-05-24 06:53:52 +0100 | [diff] [blame] | 4515 | binder_expected->x, binder_expected->len ); |
Gabor Mezei | 892c4aa | 2022-03-21 12:21:43 +0100 | [diff] [blame] | 4516 | |
Gabor Mezei | 5d9a1fe | 2022-03-24 17:49:14 +0100 | [diff] [blame] | 4517 | PSA_DONE( ); |
Hanno Becker | 55bc2c5 | 2021-05-24 06:53:52 +0100 | [diff] [blame] | 4518 | } |
| 4519 | /* END_CASE */ |
| 4520 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4521 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */ |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4522 | void ssl_tls13_record_protection( int ciphersuite, |
| 4523 | int endpoint, |
| 4524 | int ctr, |
| 4525 | int padding_used, |
| 4526 | data_t *server_write_key, |
| 4527 | data_t *server_write_iv, |
| 4528 | data_t *client_write_key, |
| 4529 | data_t *client_write_iv, |
| 4530 | data_t *plaintext, |
| 4531 | data_t *ciphertext ) |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4532 | { |
| 4533 | mbedtls_ssl_key_set keys; |
| 4534 | mbedtls_ssl_transform transform_send; |
| 4535 | mbedtls_ssl_transform transform_recv; |
| 4536 | mbedtls_record rec; |
| 4537 | unsigned char *buf = NULL; |
Hanno Becker | 1f91878 | 2021-08-01 19:18:28 +0100 | [diff] [blame] | 4538 | size_t buf_len; |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4539 | int other_endpoint; |
| 4540 | |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 4541 | USE_PSA_INIT( ); |
| 4542 | |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4543 | TEST_ASSERT( endpoint == MBEDTLS_SSL_IS_CLIENT || |
| 4544 | endpoint == MBEDTLS_SSL_IS_SERVER ); |
| 4545 | |
| 4546 | if( endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 4547 | other_endpoint = MBEDTLS_SSL_IS_CLIENT; |
| 4548 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 4549 | other_endpoint = MBEDTLS_SSL_IS_SERVER; |
| 4550 | |
| 4551 | TEST_ASSERT( server_write_key->len == client_write_key->len ); |
| 4552 | TEST_ASSERT( server_write_iv->len == client_write_iv->len ); |
| 4553 | |
| 4554 | memcpy( keys.client_write_key, |
| 4555 | client_write_key->x, client_write_key->len ); |
| 4556 | memcpy( keys.client_write_iv, |
| 4557 | client_write_iv->x, client_write_iv->len ); |
| 4558 | memcpy( keys.server_write_key, |
| 4559 | server_write_key->x, server_write_key->len ); |
| 4560 | memcpy( keys.server_write_iv, |
| 4561 | server_write_iv->x, server_write_iv->len ); |
| 4562 | |
| 4563 | keys.key_len = server_write_key->len; |
| 4564 | keys.iv_len = server_write_iv->len; |
| 4565 | |
| 4566 | mbedtls_ssl_transform_init( &transform_recv ); |
| 4567 | mbedtls_ssl_transform_init( &transform_send ); |
| 4568 | |
| 4569 | TEST_ASSERT( mbedtls_ssl_tls13_populate_transform( |
| 4570 | &transform_send, endpoint, |
| 4571 | ciphersuite, &keys, NULL ) == 0 ); |
| 4572 | TEST_ASSERT( mbedtls_ssl_tls13_populate_transform( |
| 4573 | &transform_recv, other_endpoint, |
| 4574 | ciphersuite, &keys, NULL ) == 0 ); |
| 4575 | |
Hanno Becker | 1f91878 | 2021-08-01 19:18:28 +0100 | [diff] [blame] | 4576 | /* Make sure we have enough space in the buffer even if |
| 4577 | * we use more padding than the KAT. */ |
| 4578 | buf_len = ciphertext->len + MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY; |
| 4579 | ASSERT_ALLOC( buf, buf_len ); |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4580 | rec.type = MBEDTLS_SSL_MSG_APPLICATION_DATA; |
Hanno Becker | 4153745 | 2021-04-20 05:35:28 +0100 | [diff] [blame] | 4581 | |
| 4582 | /* TLS 1.3 uses the version identifier from TLS 1.2 on the wire. */ |
Glenn Strauss | e3af4cb | 2022-03-15 03:23:42 -0400 | [diff] [blame] | 4583 | mbedtls_ssl_write_version( rec.ver, |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4584 | MBEDTLS_SSL_TRANSPORT_STREAM, |
Glenn Strauss | e3af4cb | 2022-03-15 03:23:42 -0400 | [diff] [blame] | 4585 | MBEDTLS_SSL_VERSION_TLS1_2 ); |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4586 | |
| 4587 | /* Copy plaintext into record structure */ |
| 4588 | rec.buf = buf; |
Hanno Becker | 1f91878 | 2021-08-01 19:18:28 +0100 | [diff] [blame] | 4589 | rec.buf_len = buf_len; |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4590 | rec.data_offset = 0; |
| 4591 | TEST_ASSERT( plaintext->len <= ciphertext->len ); |
| 4592 | memcpy( rec.buf + rec.data_offset, plaintext->x, plaintext->len ); |
| 4593 | rec.data_len = plaintext->len; |
| 4594 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 4595 | rec.cid_len = 0; |
| 4596 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 4597 | |
| 4598 | memset( &rec.ctr[0], 0, 8 ); |
| 4599 | rec.ctr[7] = ctr; |
| 4600 | |
| 4601 | TEST_ASSERT( mbedtls_ssl_encrypt_buf( NULL, &transform_send, &rec, |
| 4602 | NULL, NULL ) == 0 ); |
Hanno Becker | 1f91878 | 2021-08-01 19:18:28 +0100 | [diff] [blame] | 4603 | |
| 4604 | if( padding_used == MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY ) |
| 4605 | { |
| 4606 | ASSERT_COMPARE( rec.buf + rec.data_offset, rec.data_len, |
| 4607 | ciphertext->x, ciphertext->len ); |
| 4608 | } |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4609 | |
| 4610 | TEST_ASSERT( mbedtls_ssl_decrypt_buf( NULL, &transform_recv, &rec ) == 0 ); |
| 4611 | ASSERT_COMPARE( rec.buf + rec.data_offset, rec.data_len, |
| 4612 | plaintext->x, plaintext->len ); |
| 4613 | |
Hanno Becker | 80e760e | 2021-03-23 06:00:21 +0000 | [diff] [blame] | 4614 | mbedtls_free( buf ); |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4615 | mbedtls_ssl_transform_free( &transform_send ); |
| 4616 | mbedtls_ssl_transform_free( &transform_recv ); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 4617 | USE_PSA_DONE( ); |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4618 | } |
| 4619 | /* END_CASE */ |
| 4620 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4621 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */ |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4622 | void ssl_tls13_key_evolution( int hash_alg, |
| 4623 | data_t *secret, |
| 4624 | data_t *input, |
| 4625 | data_t *expected ) |
Hanno Becker | 2d2c3eb | 2020-08-20 14:54:24 +0100 | [diff] [blame] | 4626 | { |
| 4627 | unsigned char secret_new[ MBEDTLS_MD_MAX_SIZE ]; |
| 4628 | |
Gabor Mezei | 5d9a1fe | 2022-03-24 17:49:14 +0100 | [diff] [blame] | 4629 | PSA_INIT(); |
Gabor Mezei | 892c4aa | 2022-03-21 12:21:43 +0100 | [diff] [blame] | 4630 | |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4631 | TEST_ASSERT( mbedtls_ssl_tls13_evolve_secret( |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 4632 | (psa_algorithm_t) hash_alg, |
Hanno Becker | 2d2c3eb | 2020-08-20 14:54:24 +0100 | [diff] [blame] | 4633 | secret->len ? secret->x : NULL, |
| 4634 | input->len ? input->x : NULL, input->len, |
| 4635 | secret_new ) == 0 ); |
| 4636 | |
Hanno Becker | fb08096 | 2020-09-08 10:58:42 +0100 | [diff] [blame] | 4637 | ASSERT_COMPARE( secret_new, (size_t) expected->len, |
| 4638 | expected->x, (size_t) expected->len ); |
Gabor Mezei | 892c4aa | 2022-03-21 12:21:43 +0100 | [diff] [blame] | 4639 | |
Gabor Mezei | 5d9a1fe | 2022-03-24 17:49:14 +0100 | [diff] [blame] | 4640 | PSA_DONE(); |
Hanno Becker | 2d2c3eb | 2020-08-20 14:54:24 +0100 | [diff] [blame] | 4641 | } |
| 4642 | /* END_CASE */ |
| 4643 | |
Jerry Yu | 53d23e2 | 2022-02-09 16:25:09 +0800 | [diff] [blame] | 4644 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_2 */ |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 4645 | void ssl_tls_prf( int type, data_t * secret, data_t * random, |
Ronald Cron | ac6ae35 | 2020-06-26 14:33:03 +0200 | [diff] [blame] | 4646 | char *label, data_t *result_str, int exp_ret ) |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 4647 | { |
| 4648 | unsigned char *output; |
| 4649 | |
Ronald Cron | ac6ae35 | 2020-06-26 14:33:03 +0200 | [diff] [blame] | 4650 | output = mbedtls_calloc( 1, result_str->len ); |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 4651 | if( output == NULL ) |
| 4652 | goto exit; |
| 4653 | |
Gilles Peskine | 9de97e2 | 2021-02-02 21:00:11 +0100 | [diff] [blame] | 4654 | USE_PSA_INIT( ); |
Ron Eldor | 6b9b1b8 | 2019-05-15 17:04:33 +0300 | [diff] [blame] | 4655 | |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 4656 | TEST_ASSERT( mbedtls_ssl_tls_prf( type, secret->x, secret->len, |
| 4657 | label, random->x, random->len, |
Ronald Cron | ac6ae35 | 2020-06-26 14:33:03 +0200 | [diff] [blame] | 4658 | output, result_str->len ) == exp_ret ); |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 4659 | |
| 4660 | if( exp_ret == 0 ) |
| 4661 | { |
Ronald Cron | ac6ae35 | 2020-06-26 14:33:03 +0200 | [diff] [blame] | 4662 | TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x, |
| 4663 | result_str->len, result_str->len ) == 0 ); |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 4664 | } |
| 4665 | exit: |
| 4666 | |
| 4667 | mbedtls_free( output ); |
Gilles Peskine | 1f186ff | 2021-02-02 21:04:06 +0100 | [diff] [blame] | 4668 | USE_PSA_DONE( ); |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 4669 | } |
| 4670 | /* END_CASE */ |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 4671 | |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 4672 | /* BEGIN_CASE */ |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 4673 | void ssl_serialize_session_save_load( int ticket_len, char *crt_file ) |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 4674 | { |
| 4675 | mbedtls_ssl_session original, restored; |
| 4676 | unsigned char *buf = NULL; |
| 4677 | size_t len; |
| 4678 | |
| 4679 | /* |
| 4680 | * Test that a save-load pair is the identity |
| 4681 | */ |
| 4682 | |
| 4683 | mbedtls_ssl_session_init( &original ); |
| 4684 | mbedtls_ssl_session_init( &restored ); |
| 4685 | |
| 4686 | /* Prepare a dummy session to work on */ |
Jerry Yu | 534ff40 | 2022-07-14 16:43:43 +0800 | [diff] [blame] | 4687 | TEST_ASSERT( ssl_tls12_populate_session( &original, ticket_len, crt_file ) == 0 ); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 4688 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 4689 | /* Serialize it */ |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 4690 | TEST_ASSERT( mbedtls_ssl_session_save( &original, NULL, 0, &len ) |
| 4691 | == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 4692 | TEST_ASSERT( ( buf = mbedtls_calloc( 1, len ) ) != NULL ); |
| 4693 | TEST_ASSERT( mbedtls_ssl_session_save( &original, buf, len, &len ) |
| 4694 | == 0 ); |
| 4695 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 4696 | /* Restore session from serialized data */ |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 4697 | TEST_ASSERT( mbedtls_ssl_session_load( &restored, buf, len) == 0 ); |
| 4698 | |
| 4699 | /* |
| 4700 | * Make sure both session structures are identical |
| 4701 | */ |
| 4702 | #if defined(MBEDTLS_HAVE_TIME) |
| 4703 | TEST_ASSERT( original.start == restored.start ); |
| 4704 | #endif |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 4705 | TEST_ASSERT( original.tls_version == restored.tls_version ); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 4706 | TEST_ASSERT( original.ciphersuite == restored.ciphersuite ); |
| 4707 | TEST_ASSERT( original.compression == restored.compression ); |
| 4708 | TEST_ASSERT( original.id_len == restored.id_len ); |
| 4709 | TEST_ASSERT( memcmp( original.id, |
| 4710 | restored.id, sizeof( original.id ) ) == 0 ); |
| 4711 | TEST_ASSERT( memcmp( original.master, |
| 4712 | restored.master, sizeof( original.master ) ) == 0 ); |
| 4713 | |
| 4714 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 4715 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 4716 | TEST_ASSERT( ( original.peer_cert == NULL ) == |
| 4717 | ( restored.peer_cert == NULL ) ); |
| 4718 | if( original.peer_cert != NULL ) |
| 4719 | { |
| 4720 | TEST_ASSERT( original.peer_cert->raw.len == |
| 4721 | restored.peer_cert->raw.len ); |
| 4722 | TEST_ASSERT( memcmp( original.peer_cert->raw.p, |
| 4723 | restored.peer_cert->raw.p, |
| 4724 | original.peer_cert->raw.len ) == 0 ); |
| 4725 | } |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 4726 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 4727 | TEST_ASSERT( original.peer_cert_digest_type == |
| 4728 | restored.peer_cert_digest_type ); |
| 4729 | TEST_ASSERT( original.peer_cert_digest_len == |
| 4730 | restored.peer_cert_digest_len ); |
| 4731 | TEST_ASSERT( ( original.peer_cert_digest == NULL ) == |
| 4732 | ( restored.peer_cert_digest == NULL ) ); |
| 4733 | if( original.peer_cert_digest != NULL ) |
| 4734 | { |
| 4735 | TEST_ASSERT( memcmp( original.peer_cert_digest, |
| 4736 | restored.peer_cert_digest, |
| 4737 | original.peer_cert_digest_len ) == 0 ); |
| 4738 | } |
| 4739 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 4740 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 4741 | TEST_ASSERT( original.verify_result == restored.verify_result ); |
| 4742 | |
| 4743 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 4744 | TEST_ASSERT( original.ticket_len == restored.ticket_len ); |
| 4745 | if( original.ticket_len != 0 ) |
| 4746 | { |
| 4747 | TEST_ASSERT( original.ticket != NULL ); |
| 4748 | TEST_ASSERT( restored.ticket != NULL ); |
| 4749 | TEST_ASSERT( memcmp( original.ticket, |
| 4750 | restored.ticket, original.ticket_len ) == 0 ); |
| 4751 | } |
| 4752 | TEST_ASSERT( original.ticket_lifetime == restored.ticket_lifetime ); |
| 4753 | #endif |
| 4754 | |
| 4755 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 4756 | TEST_ASSERT( original.mfl_code == restored.mfl_code ); |
| 4757 | #endif |
| 4758 | |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 4759 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 4760 | TEST_ASSERT( original.encrypt_then_mac == restored.encrypt_then_mac ); |
| 4761 | #endif |
| 4762 | |
| 4763 | exit: |
| 4764 | mbedtls_ssl_session_free( &original ); |
| 4765 | mbedtls_ssl_session_free( &restored ); |
| 4766 | mbedtls_free( buf ); |
| 4767 | } |
| 4768 | /* END_CASE */ |
| 4769 | |
Manuel Pégourié-Gonnard | aa75583 | 2019-06-03 10:53:47 +0200 | [diff] [blame] | 4770 | /* BEGIN_CASE */ |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 4771 | void ssl_serialize_session_load_save( int ticket_len, char *crt_file ) |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 4772 | { |
| 4773 | mbedtls_ssl_session session; |
| 4774 | unsigned char *buf1 = NULL, *buf2 = NULL; |
| 4775 | size_t len0, len1, len2; |
| 4776 | |
| 4777 | /* |
| 4778 | * Test that a load-save pair is the identity |
| 4779 | */ |
| 4780 | |
| 4781 | mbedtls_ssl_session_init( &session ); |
| 4782 | |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 4783 | /* Prepare a dummy session to work on */ |
Jerry Yu | 534ff40 | 2022-07-14 16:43:43 +0800 | [diff] [blame] | 4784 | TEST_ASSERT( ssl_tls12_populate_session( &session, ticket_len, crt_file ) == 0 ); |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 4785 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 4786 | /* Get desired buffer size for serializing */ |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 4787 | TEST_ASSERT( mbedtls_ssl_session_save( &session, NULL, 0, &len0 ) |
| 4788 | == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 4789 | |
| 4790 | /* Allocate first buffer */ |
| 4791 | buf1 = mbedtls_calloc( 1, len0 ); |
| 4792 | TEST_ASSERT( buf1 != NULL ); |
| 4793 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 4794 | /* Serialize to buffer and free live session */ |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 4795 | TEST_ASSERT( mbedtls_ssl_session_save( &session, buf1, len0, &len1 ) |
| 4796 | == 0 ); |
| 4797 | TEST_ASSERT( len0 == len1 ); |
| 4798 | mbedtls_ssl_session_free( &session ); |
| 4799 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 4800 | /* Restore session from serialized data */ |
Manuel Pégourié-Gonnard | 220403b | 2019-05-24 09:54:21 +0200 | [diff] [blame] | 4801 | TEST_ASSERT( mbedtls_ssl_session_load( &session, buf1, len1 ) == 0 ); |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 4802 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 4803 | /* Allocate second buffer and serialize to it */ |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 4804 | buf2 = mbedtls_calloc( 1, len0 ); |
Manuel Pégourié-Gonnard | b407990 | 2019-05-24 09:52:10 +0200 | [diff] [blame] | 4805 | TEST_ASSERT( buf2 != NULL ); |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 4806 | TEST_ASSERT( mbedtls_ssl_session_save( &session, buf2, len0, &len2 ) |
| 4807 | == 0 ); |
| 4808 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 4809 | /* Make sure both serialized versions are identical */ |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 4810 | TEST_ASSERT( len1 == len2 ); |
| 4811 | TEST_ASSERT( memcmp( buf1, buf2, len1 ) == 0 ); |
| 4812 | |
| 4813 | exit: |
| 4814 | mbedtls_ssl_session_free( &session ); |
| 4815 | mbedtls_free( buf1 ); |
| 4816 | mbedtls_free( buf2 ); |
| 4817 | } |
| 4818 | /* END_CASE */ |
Manuel Pégourié-Gonnard | f5fa0aa | 2019-05-23 10:38:11 +0200 | [diff] [blame] | 4819 | |
| 4820 | /* BEGIN_CASE */ |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 4821 | void ssl_serialize_session_save_buf_size( int ticket_len, char *crt_file ) |
Manuel Pégourié-Gonnard | f5fa0aa | 2019-05-23 10:38:11 +0200 | [diff] [blame] | 4822 | { |
| 4823 | mbedtls_ssl_session session; |
| 4824 | unsigned char *buf = NULL; |
| 4825 | size_t good_len, bad_len, test_len; |
| 4826 | |
| 4827 | /* |
| 4828 | * Test that session_save() fails cleanly on small buffers |
| 4829 | */ |
| 4830 | |
| 4831 | mbedtls_ssl_session_init( &session ); |
| 4832 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 4833 | /* Prepare dummy session and get serialized size */ |
Jerry Yu | 534ff40 | 2022-07-14 16:43:43 +0800 | [diff] [blame] | 4834 | TEST_ASSERT( ssl_tls12_populate_session( &session, ticket_len, crt_file ) == 0 ); |
Manuel Pégourié-Gonnard | f5fa0aa | 2019-05-23 10:38:11 +0200 | [diff] [blame] | 4835 | TEST_ASSERT( mbedtls_ssl_session_save( &session, NULL, 0, &good_len ) |
| 4836 | == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 4837 | |
| 4838 | /* Try all possible bad lengths */ |
| 4839 | for( bad_len = 1; bad_len < good_len; bad_len++ ) |
| 4840 | { |
| 4841 | /* Allocate exact size so that asan/valgrind can detect any overwrite */ |
| 4842 | mbedtls_free( buf ); |
| 4843 | TEST_ASSERT( ( buf = mbedtls_calloc( 1, bad_len ) ) != NULL ); |
| 4844 | TEST_ASSERT( mbedtls_ssl_session_save( &session, buf, bad_len, |
| 4845 | &test_len ) |
| 4846 | == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 4847 | TEST_ASSERT( test_len == good_len ); |
| 4848 | } |
| 4849 | |
| 4850 | exit: |
| 4851 | mbedtls_ssl_session_free( &session ); |
| 4852 | mbedtls_free( buf ); |
| 4853 | } |
| 4854 | /* END_CASE */ |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 4855 | |
| 4856 | /* BEGIN_CASE */ |
Jerry Yu | 6e8fec2 | 2022-07-15 10:37:02 +0800 | [diff] [blame^] | 4857 | void ssl_serialize_session_load_buf_size( int ticket_len, char *crt_file, |
| 4858 | int endpoint_type, int tls_version ) |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 4859 | { |
| 4860 | mbedtls_ssl_session session; |
| 4861 | unsigned char *good_buf = NULL, *bad_buf = NULL; |
| 4862 | size_t good_len, bad_len; |
| 4863 | |
| 4864 | /* |
| 4865 | * Test that session_load() fails cleanly on small buffers |
| 4866 | */ |
| 4867 | |
| 4868 | mbedtls_ssl_session_init( &session ); |
| 4869 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 4870 | /* Prepare serialized session data */ |
Jerry Yu | 6e8fec2 | 2022-07-15 10:37:02 +0800 | [diff] [blame^] | 4871 | ((void) endpoint_type); |
| 4872 | ((void) tls_version); |
| 4873 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 4874 | if(tls_version == MBEDTLS_SSL_VERSION_TLS1_3) |
| 4875 | { |
| 4876 | TEST_ASSERT( ssl_tls13_populate_session( |
| 4877 | &session, 0, endpoint_type ) == 0 ); |
| 4878 | } |
| 4879 | else |
| 4880 | #endif |
| 4881 | { |
| 4882 | TEST_ASSERT( ssl_tls12_populate_session( |
| 4883 | &session, ticket_len, crt_file ) == 0 ); |
| 4884 | } |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 4885 | TEST_ASSERT( mbedtls_ssl_session_save( &session, NULL, 0, &good_len ) |
| 4886 | == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 4887 | TEST_ASSERT( ( good_buf = mbedtls_calloc( 1, good_len ) ) != NULL ); |
| 4888 | TEST_ASSERT( mbedtls_ssl_session_save( &session, good_buf, good_len, |
| 4889 | &good_len ) == 0 ); |
| 4890 | mbedtls_ssl_session_free( &session ); |
| 4891 | |
| 4892 | /* Try all possible bad lengths */ |
| 4893 | for( bad_len = 0; bad_len < good_len; bad_len++ ) |
| 4894 | { |
| 4895 | /* Allocate exact size so that asan/valgrind can detect any overread */ |
| 4896 | mbedtls_free( bad_buf ); |
| 4897 | bad_buf = mbedtls_calloc( 1, bad_len ? bad_len : 1 ); |
| 4898 | TEST_ASSERT( bad_buf != NULL ); |
| 4899 | memcpy( bad_buf, good_buf, bad_len ); |
| 4900 | |
| 4901 | TEST_ASSERT( mbedtls_ssl_session_load( &session, bad_buf, bad_len ) |
| 4902 | == MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4903 | } |
| 4904 | |
| 4905 | exit: |
| 4906 | mbedtls_ssl_session_free( &session ); |
| 4907 | mbedtls_free( good_buf ); |
| 4908 | mbedtls_free( bad_buf ); |
| 4909 | } |
| 4910 | /* END_CASE */ |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 4911 | |
Hanno Becker | 363b646 | 2019-05-29 12:44:28 +0100 | [diff] [blame] | 4912 | /* BEGIN_CASE */ |
| 4913 | void ssl_session_serialize_version_check( int corrupt_major, |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 4914 | int corrupt_minor, |
| 4915 | int corrupt_patch, |
Jerry Yu | 534ff40 | 2022-07-14 16:43:43 +0800 | [diff] [blame] | 4916 | int corrupt_config, |
| 4917 | int endpoint_type, |
| 4918 | int tls_version ) |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 4919 | { |
Hanno Becker | 363b646 | 2019-05-29 12:44:28 +0100 | [diff] [blame] | 4920 | unsigned char serialized_session[ 2048 ]; |
| 4921 | size_t serialized_session_len; |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 4922 | unsigned cur_byte; |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 4923 | mbedtls_ssl_session session; |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 4924 | uint8_t should_corrupt_byte[] = { corrupt_major == 1, |
| 4925 | corrupt_minor == 1, |
| 4926 | corrupt_patch == 1, |
| 4927 | corrupt_config == 1, |
| 4928 | corrupt_config == 1 }; |
| 4929 | |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 4930 | mbedtls_ssl_session_init( &session ); |
Jerry Yu | 534ff40 | 2022-07-14 16:43:43 +0800 | [diff] [blame] | 4931 | ((void) endpoint_type); |
| 4932 | ((void) tls_version); |
| 4933 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
| 4934 | if(tls_version == MBEDTLS_SSL_VERSION_TLS1_3) |
| 4935 | { |
| 4936 | TEST_ASSERT( ssl_tls13_populate_session( |
| 4937 | &session, 0, endpoint_type ) == 0 ); |
| 4938 | } |
| 4939 | else |
| 4940 | #endif |
| 4941 | TEST_ASSERT( ssl_tls12_populate_session( &session, 0, NULL ) == 0 ); |
| 4942 | |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 4943 | |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 4944 | /* Infer length of serialized session. */ |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 4945 | TEST_ASSERT( mbedtls_ssl_session_save( &session, |
Hanno Becker | 363b646 | 2019-05-29 12:44:28 +0100 | [diff] [blame] | 4946 | serialized_session, |
| 4947 | sizeof( serialized_session ), |
| 4948 | &serialized_session_len ) == 0 ); |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 4949 | |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 4950 | mbedtls_ssl_session_free( &session ); |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 4951 | |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 4952 | /* Without any modification, we should be able to successfully |
Hanno Becker | 363b646 | 2019-05-29 12:44:28 +0100 | [diff] [blame] | 4953 | * de-serialize the session - double-check that. */ |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 4954 | TEST_ASSERT( mbedtls_ssl_session_load( &session, |
Hanno Becker | 363b646 | 2019-05-29 12:44:28 +0100 | [diff] [blame] | 4955 | serialized_session, |
| 4956 | serialized_session_len ) == 0 ); |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 4957 | mbedtls_ssl_session_free( &session ); |
| 4958 | |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 4959 | /* Go through the bytes in the serialized session header and |
| 4960 | * corrupt them bit-by-bit. */ |
| 4961 | for( cur_byte = 0; cur_byte < sizeof( should_corrupt_byte ); cur_byte++ ) |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 4962 | { |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 4963 | int cur_bit; |
| 4964 | unsigned char * const byte = &serialized_session[ cur_byte ]; |
| 4965 | |
| 4966 | if( should_corrupt_byte[ cur_byte ] == 0 ) |
| 4967 | continue; |
| 4968 | |
| 4969 | for( cur_bit = 0; cur_bit < CHAR_BIT; cur_bit++ ) |
| 4970 | { |
| 4971 | unsigned char const corrupted_bit = 0x1u << cur_bit; |
| 4972 | /* Modify a single bit in the serialized session. */ |
| 4973 | *byte ^= corrupted_bit; |
| 4974 | |
| 4975 | /* Attempt to deserialize */ |
| 4976 | TEST_ASSERT( mbedtls_ssl_session_load( &session, |
| 4977 | serialized_session, |
| 4978 | serialized_session_len ) == |
Hanno Becker | f9b3303 | 2019-06-03 12:58:39 +0100 | [diff] [blame] | 4979 | MBEDTLS_ERR_SSL_VERSION_MISMATCH ); |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 4980 | |
| 4981 | /* Undo the change */ |
| 4982 | *byte ^= corrupted_bit; |
| 4983 | } |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 4984 | } |
| 4985 | |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 4986 | } |
| 4987 | /* END_CASE */ |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4988 | |
Neil Armstrong | 0739336 | 2022-04-14 15:36:17 +0200 | [diff] [blame] | 4989 | /* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:!MBEDTLS_SSL_PROTO_TLS1_3:MBEDTLS_PKCS1_V15:MBEDTLS_ENTROPY_C:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */ |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4990 | void mbedtls_endpoint_sanity( int endpoint_type ) |
| 4991 | { |
| 4992 | enum { BUFFSIZE = 1024 }; |
| 4993 | mbedtls_endpoint ep; |
| 4994 | int ret = -1; |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 4995 | handshake_test_options options; |
| 4996 | init_handshake_options( &options ); |
| 4997 | options.pk_alg = MBEDTLS_PK_RSA; |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4998 | |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 4999 | ret = mbedtls_endpoint_init( NULL, endpoint_type, &options, |
| 5000 | NULL, NULL, NULL, NULL ); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 5001 | TEST_ASSERT( MBEDTLS_ERR_SSL_BAD_INPUT_DATA == ret ); |
| 5002 | |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 5003 | ret = mbedtls_endpoint_certificate_init( NULL, options.pk_alg, 0, 0, 0 ); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 5004 | TEST_ASSERT( MBEDTLS_ERR_SSL_BAD_INPUT_DATA == ret ); |
| 5005 | |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 5006 | ret = mbedtls_endpoint_init( &ep, endpoint_type, &options, |
Andrzej Kurek | 74394a5 | 2022-03-08 06:50:12 -0500 | [diff] [blame] | 5007 | NULL, NULL, NULL, NULL ); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 5008 | TEST_ASSERT( ret == 0 ); |
| 5009 | |
| 5010 | exit: |
Andrzej Kurek | 15daf50 | 2020-02-12 09:17:52 -0500 | [diff] [blame] | 5011 | mbedtls_endpoint_free( &ep, NULL ); |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 5012 | free_handshake_options( &options ); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 5013 | } |
| 5014 | /* END_CASE */ |
| 5015 | |
Jerry Yu | 023ff7a | 2022-04-29 17:00:19 +0800 | [diff] [blame] | 5016 | /* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_PKCS1_V15:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */ |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 5017 | void move_handshake_to_state(int endpoint_type, int state, int need_pass) |
| 5018 | { |
| 5019 | enum { BUFFSIZE = 1024 }; |
| 5020 | mbedtls_endpoint base_ep, second_ep; |
| 5021 | int ret = -1; |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 5022 | handshake_test_options options; |
| 5023 | init_handshake_options( &options ); |
| 5024 | options.pk_alg = MBEDTLS_PK_RSA; |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 5025 | |
Neil Armstrong | 06baf04 | 2022-04-14 16:21:15 +0200 | [diff] [blame] | 5026 | USE_PSA_INIT( ); |
| 5027 | |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 5028 | ret = mbedtls_endpoint_init( &base_ep, endpoint_type, &options, |
| 5029 | NULL, NULL, NULL, NULL ); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 5030 | TEST_ASSERT( ret == 0 ); |
| 5031 | |
| 5032 | ret = mbedtls_endpoint_init( &second_ep, |
| 5033 | ( endpoint_type == MBEDTLS_SSL_IS_SERVER ) ? |
Andrzej Kurek | b298074 | 2020-02-02 19:25:26 -0500 | [diff] [blame] | 5034 | MBEDTLS_SSL_IS_CLIENT : MBEDTLS_SSL_IS_SERVER, |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 5035 | &options, NULL, NULL, NULL, NULL ); |
| 5036 | |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 5037 | TEST_ASSERT( ret == 0 ); |
| 5038 | |
| 5039 | ret = mbedtls_mock_socket_connect( &(base_ep.socket), |
| 5040 | &(second_ep.socket), |
| 5041 | BUFFSIZE ); |
| 5042 | TEST_ASSERT( ret == 0 ); |
| 5043 | |
| 5044 | ret = mbedtls_move_handshake_to_state( &(base_ep.ssl), |
| 5045 | &(second_ep.ssl), |
| 5046 | state ); |
| 5047 | if( need_pass ) |
| 5048 | { |
Jerry Yu | 1443537 | 2022-06-01 15:53:31 +0800 | [diff] [blame] | 5049 | TEST_ASSERT( ret == 0 || |
| 5050 | ret == MBEDTLS_ERR_SSL_WANT_READ || |
| 5051 | ret == MBEDTLS_ERR_SSL_WANT_WRITE ); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 5052 | TEST_ASSERT( base_ep.ssl.state == state ); |
| 5053 | } |
| 5054 | else |
| 5055 | { |
Jerry Yu | 1443537 | 2022-06-01 15:53:31 +0800 | [diff] [blame] | 5056 | TEST_ASSERT( ret != 0 && |
| 5057 | ret != MBEDTLS_ERR_SSL_WANT_READ && |
| 5058 | ret != MBEDTLS_ERR_SSL_WANT_WRITE ); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 5059 | TEST_ASSERT( base_ep.ssl.state != state ); |
| 5060 | } |
| 5061 | |
| 5062 | exit: |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 5063 | free_handshake_options( &options ); |
Andrzej Kurek | 15daf50 | 2020-02-12 09:17:52 -0500 | [diff] [blame] | 5064 | mbedtls_endpoint_free( &base_ep, NULL ); |
| 5065 | mbedtls_endpoint_free( &second_ep, NULL ); |
Neil Armstrong | 06baf04 | 2022-04-14 16:21:15 +0200 | [diff] [blame] | 5066 | USE_PSA_DONE( ); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 5067 | } |
| 5068 | /* END_CASE */ |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 5069 | |
Neil Armstrong | 46a1760 | 2022-02-23 15:11:16 +0100 | [diff] [blame] | 5070 | /* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_PKCS1_V15:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */ |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 5071 | void handshake_version( int dtls, int client_min_version, int client_max_version, |
| 5072 | int server_min_version, int server_max_version, |
| 5073 | int expected_negotiated_version ) |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 5074 | { |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5075 | handshake_test_options options; |
| 5076 | init_handshake_options( &options ); |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 5077 | |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 5078 | options.client_min_version = client_min_version; |
| 5079 | options.client_max_version = client_max_version; |
| 5080 | options.server_min_version = server_min_version; |
| 5081 | options.server_max_version = server_max_version; |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 5082 | options.expected_negotiated_version = expected_negotiated_version; |
| 5083 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5084 | options.dtls = dtls; |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5085 | perform_handshake( &options ); |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 5086 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5087 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 5088 | goto exit; |
Andrzej Kurek | e11acb2 | 2022-06-27 06:11:34 -0400 | [diff] [blame] | 5089 | |
| 5090 | exit: |
| 5091 | free_handshake_options( &options ); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5092 | } |
| 5093 | /* END_CASE */ |
Andrzej Kurek | 9e9efdc | 2020-02-26 05:25:23 -0500 | [diff] [blame] | 5094 | |
Neil Armstrong | 46a1760 | 2022-02-23 15:11:16 +0100 | [diff] [blame] | 5095 | /* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */ |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5096 | void handshake_psk_cipher( char* cipher, int pk_alg, data_t *psk_str, int dtls ) |
| 5097 | { |
| 5098 | handshake_test_options options; |
| 5099 | init_handshake_options( &options ); |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 5100 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5101 | options.cipher = cipher; |
| 5102 | options.dtls = dtls; |
| 5103 | options.psk_str = psk_str; |
| 5104 | options.pk_alg = pk_alg; |
Andrzej Kurek | cc5169c | 2020-02-04 09:04:56 -0500 | [diff] [blame] | 5105 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5106 | perform_handshake( &options ); |
Andrzej Kurek | 316da1f | 2020-02-26 09:03:47 -0500 | [diff] [blame] | 5107 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5108 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 5109 | goto exit; |
Andrzej Kurek | e11acb2 | 2022-06-27 06:11:34 -0400 | [diff] [blame] | 5110 | |
Andrzej Kurek | ddb8cd6 | 2022-07-04 16:07:28 -0400 | [diff] [blame] | 5111 | exit: |
Andrzej Kurek | e11acb2 | 2022-06-27 06:11:34 -0400 | [diff] [blame] | 5112 | free_handshake_options( &options ); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5113 | } |
| 5114 | /* END_CASE */ |
Andrzej Kurek | 316da1f | 2020-02-26 09:03:47 -0500 | [diff] [blame] | 5115 | |
Neil Armstrong | 46a1760 | 2022-02-23 15:11:16 +0100 | [diff] [blame] | 5116 | /* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */ |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5117 | void handshake_cipher( char* cipher, int pk_alg, int dtls ) |
| 5118 | { |
| 5119 | test_handshake_psk_cipher( cipher, pk_alg, NULL, dtls ); |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 5120 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5121 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 5122 | goto exit; |
| 5123 | } |
| 5124 | /* END_CASE */ |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 5125 | |
Neil Armstrong | 8c52ed8 | 2022-05-27 13:14:55 +0200 | [diff] [blame] | 5126 | /* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */ |
Neil Armstrong | db13497 | 2022-06-30 09:06:28 +0200 | [diff] [blame] | 5127 | void handshake_ciphersuite_select( char* cipher, int pk_alg, data_t *psk_str, |
| 5128 | int psa_alg, int psa_alg2, int psa_usage, |
Neil Armstrong | 8c52ed8 | 2022-05-27 13:14:55 +0200 | [diff] [blame] | 5129 | int expected_handshake_result, |
Neil Armstrong | db13497 | 2022-06-30 09:06:28 +0200 | [diff] [blame] | 5130 | int expected_ciphersuite ) |
Neil Armstrong | 8c52ed8 | 2022-05-27 13:14:55 +0200 | [diff] [blame] | 5131 | { |
| 5132 | handshake_test_options options; |
| 5133 | init_handshake_options( &options ); |
| 5134 | |
| 5135 | options.cipher = cipher; |
Neil Armstrong | db13497 | 2022-06-30 09:06:28 +0200 | [diff] [blame] | 5136 | options.psk_str = psk_str; |
Neil Armstrong | 8c52ed8 | 2022-05-27 13:14:55 +0200 | [diff] [blame] | 5137 | options.pk_alg = pk_alg; |
| 5138 | options.opaque_alg = psa_alg; |
| 5139 | options.opaque_alg2 = psa_alg2; |
| 5140 | options.opaque_usage = psa_usage; |
| 5141 | options.expected_handshake_result = expected_handshake_result; |
| 5142 | options.expected_ciphersuite = expected_ciphersuite; |
| 5143 | perform_handshake( &options ); |
| 5144 | |
| 5145 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 5146 | goto exit; |
Andrzej Kurek | ddb8cd6 | 2022-07-04 16:07:28 -0400 | [diff] [blame] | 5147 | |
| 5148 | exit: |
| 5149 | free_handshake_options( &options ); |
Neil Armstrong | 8c52ed8 | 2022-05-27 13:14:55 +0200 | [diff] [blame] | 5150 | } |
| 5151 | /* END_CASE */ |
| 5152 | |
Ronald Cron | 21a1b2d | 2022-06-15 17:11:35 +0200 | [diff] [blame] | 5153 | /* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_PKCS1_V15:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */ |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5154 | void app_data( int mfl, int cli_msg_len, int srv_msg_len, |
| 5155 | int expected_cli_fragments, |
| 5156 | int expected_srv_fragments, int dtls ) |
| 5157 | { |
| 5158 | handshake_test_options options; |
| 5159 | init_handshake_options( &options ); |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 5160 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5161 | options.mfl = mfl; |
| 5162 | options.cli_msg_len = cli_msg_len; |
| 5163 | options.srv_msg_len = srv_msg_len; |
| 5164 | options.expected_cli_fragments = expected_cli_fragments; |
| 5165 | options.expected_srv_fragments = expected_srv_fragments; |
| 5166 | options.dtls = dtls; |
Ronald Cron | 21a1b2d | 2022-06-15 17:11:35 +0200 | [diff] [blame] | 5167 | #if ! defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 5168 | options.expected_negotiated_version = MBEDTLS_SSL_VERSION_TLS1_3; |
| 5169 | #endif |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 5170 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5171 | perform_handshake( &options ); |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 5172 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5173 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 5174 | goto exit; |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 5175 | |
| 5176 | exit: |
| 5177 | free_handshake_options( &options ); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5178 | } |
| 5179 | /* END_CASE */ |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 5180 | |
Ronald Cron | 21a1b2d | 2022-06-15 17:11:35 +0200 | [diff] [blame] | 5181 | /* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_PKCS1_V15:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */ |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5182 | void app_data_tls( int mfl, int cli_msg_len, int srv_msg_len, |
| 5183 | int expected_cli_fragments, |
| 5184 | int expected_srv_fragments ) |
| 5185 | { |
| 5186 | test_app_data( mfl, cli_msg_len, srv_msg_len, expected_cli_fragments, |
| 5187 | expected_srv_fragments, 0 ); |
| 5188 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 5189 | goto exit; |
| 5190 | } |
| 5191 | /* END_CASE */ |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 5192 | |
Neil Armstrong | 993eea3 | 2022-04-14 15:37:23 +0200 | [diff] [blame] | 5193 | /* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_SSL_PROTO_TLS1_3:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_SSL_PROTO_DTLS:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */ |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5194 | void app_data_dtls( int mfl, int cli_msg_len, int srv_msg_len, |
| 5195 | int expected_cli_fragments, |
| 5196 | int expected_srv_fragments ) |
| 5197 | { |
| 5198 | test_app_data( mfl, cli_msg_len, srv_msg_len, expected_cli_fragments, |
| 5199 | expected_srv_fragments, 1 ); |
| 5200 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 5201 | goto exit; |
| 5202 | } |
| 5203 | /* END_CASE */ |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 5204 | |
Neil Armstrong | 181fe69 | 2022-04-14 15:38:01 +0200 | [diff] [blame] | 5205 | /* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_SSL_PROTO_TLS1_3:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_SSL_PROTO_DTLS:MBEDTLS_SSL_RENEGOTIATION:MBEDTLS_SSL_CONTEXT_SERIALIZATION:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */ |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5206 | void handshake_serialization( ) |
| 5207 | { |
| 5208 | handshake_test_options options; |
| 5209 | init_handshake_options( &options ); |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 5210 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5211 | options.serialize = 1; |
| 5212 | options.dtls = 1; |
| 5213 | perform_handshake( &options ); |
| 5214 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 5215 | goto exit; |
Andrzej Kurek | 6e518ab | 2022-06-11 05:08:38 -0400 | [diff] [blame] | 5216 | exit: |
| 5217 | free_handshake_options( &options ); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5218 | } |
| 5219 | /* END_CASE */ |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 5220 | |
Neil Armstrong | 181fe69 | 2022-04-14 15:38:01 +0200 | [diff] [blame] | 5221 | /* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_SSL_PROTO_TLS1_3:MBEDTLS_PKCS1_V15:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_DEBUG_C:MBEDTLS_SSL_MAX_FRAGMENT_LENGTH:MBEDTLS_CIPHER_MODE_CBC:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */ |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 5222 | void handshake_fragmentation( int mfl, int expected_srv_hs_fragmentation, int expected_cli_hs_fragmentation) |
| 5223 | { |
| 5224 | handshake_test_options options; |
| 5225 | log_pattern srv_pattern, cli_pattern; |
| 5226 | |
| 5227 | srv_pattern.pattern = cli_pattern.pattern = "found fragmented DTLS handshake"; |
| 5228 | srv_pattern.counter = 0; |
| 5229 | cli_pattern.counter = 0; |
| 5230 | |
| 5231 | init_handshake_options( &options ); |
| 5232 | options.dtls = 1; |
| 5233 | options.mfl = mfl; |
Darryl Green | aad82f9 | 2019-12-02 10:53:11 +0000 | [diff] [blame] | 5234 | /* Set cipher to one using CBC so that record splitting can be tested */ |
| 5235 | options.cipher = "TLS-DHE-RSA-WITH-AES-256-CBC-SHA256"; |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 5236 | options.srv_auth_mode = MBEDTLS_SSL_VERIFY_REQUIRED; |
| 5237 | options.srv_log_obj = &srv_pattern; |
| 5238 | options.cli_log_obj = &cli_pattern; |
| 5239 | options.srv_log_fun = log_analyzer; |
| 5240 | options.cli_log_fun = log_analyzer; |
| 5241 | |
| 5242 | perform_handshake( &options ); |
| 5243 | |
| 5244 | /* Test if the server received a fragmented handshake */ |
| 5245 | if( expected_srv_hs_fragmentation ) |
| 5246 | { |
| 5247 | TEST_ASSERT( srv_pattern.counter >= 1 ); |
| 5248 | } |
| 5249 | /* Test if the client received a fragmented handshake */ |
| 5250 | if( expected_cli_hs_fragmentation ) |
| 5251 | { |
| 5252 | TEST_ASSERT( cli_pattern.counter >= 1 ); |
| 5253 | } |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 5254 | |
| 5255 | exit: |
| 5256 | free_handshake_options( &options ); |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 5257 | } |
| 5258 | /* END_CASE */ |
| 5259 | |
Neil Armstrong | 537e915 | 2022-04-14 15:40:26 +0200 | [diff] [blame] | 5260 | /* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_SSL_PROTO_TLS1_3:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_SSL_PROTO_DTLS:MBEDTLS_SSL_RENEGOTIATION:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */ |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5261 | void renegotiation( int legacy_renegotiation ) |
| 5262 | { |
| 5263 | handshake_test_options options; |
| 5264 | init_handshake_options( &options ); |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 5265 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5266 | options.renegotiate = 1; |
| 5267 | options.legacy_renegotiation = legacy_renegotiation; |
| 5268 | options.dtls = 1; |
Andrzej Kurek | 316da1f | 2020-02-26 09:03:47 -0500 | [diff] [blame] | 5269 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5270 | perform_handshake( &options ); |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 5271 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5272 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 5273 | goto exit; |
Andrzej Kurek | ddb8cd6 | 2022-07-04 16:07:28 -0400 | [diff] [blame] | 5274 | exit: |
| 5275 | free_handshake_options( &options ); |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 5276 | } |
| 5277 | /* END_CASE */ |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 5278 | |
Neil Armstrong | 537e915 | 2022-04-14 15:40:26 +0200 | [diff] [blame] | 5279 | /* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_SSL_PROTO_TLS1_3:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */ |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 5280 | void resize_buffers( int mfl, int renegotiation, int legacy_renegotiation, |
Andrzej Kurek | 8ea6872 | 2020-04-03 06:40:47 -0400 | [diff] [blame] | 5281 | int serialize, int dtls, char *cipher ) |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 5282 | { |
| 5283 | handshake_test_options options; |
| 5284 | init_handshake_options( &options ); |
| 5285 | |
| 5286 | options.mfl = mfl; |
Andrzej Kurek | 8ea6872 | 2020-04-03 06:40:47 -0400 | [diff] [blame] | 5287 | options.cipher = cipher; |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 5288 | options.renegotiate = renegotiation; |
| 5289 | options.legacy_renegotiation = legacy_renegotiation; |
| 5290 | options.serialize = serialize; |
| 5291 | options.dtls = dtls; |
| 5292 | options.resize_buffers = 1; |
| 5293 | |
| 5294 | perform_handshake( &options ); |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 5295 | |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 5296 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 5297 | goto exit; |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 5298 | exit: |
| 5299 | free_handshake_options( &options ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 5300 | } |
| 5301 | /* END_CASE */ |
| 5302 | |
Neil Armstrong | 537e915 | 2022-04-14 15:40:26 +0200 | [diff] [blame] | 5303 | /* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_SSL_PROTO_TLS1_3:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH:MBEDTLS_SSL_CONTEXT_SERIALIZATION:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_SSL_PROTO_DTLS:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */ |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 5304 | void resize_buffers_serialize_mfl( int mfl ) |
| 5305 | { |
Andrzej Kurek | 8ea6872 | 2020-04-03 06:40:47 -0400 | [diff] [blame] | 5306 | test_resize_buffers( mfl, 0, MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION, 1, 1, |
| 5307 | (char *) "" ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 5308 | |
| 5309 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 5310 | goto exit; |
| 5311 | } |
| 5312 | /* END_CASE */ |
| 5313 | |
Neil Armstrong | 537e915 | 2022-04-14 15:40:26 +0200 | [diff] [blame] | 5314 | /* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_SSL_PROTO_TLS1_3:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH:MBEDTLS_SSL_RENEGOTIATION:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */ |
Andrzej Kurek | 8ea6872 | 2020-04-03 06:40:47 -0400 | [diff] [blame] | 5315 | void resize_buffers_renegotiate_mfl( int mfl, int legacy_renegotiation, |
| 5316 | char *cipher ) |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 5317 | { |
Andrzej Kurek | 8ea6872 | 2020-04-03 06:40:47 -0400 | [diff] [blame] | 5318 | test_resize_buffers( mfl, 1, legacy_renegotiation, 0, 1, cipher ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 5319 | |
| 5320 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 5321 | goto exit; |
| 5322 | } |
| 5323 | /* END_CASE */ |
Manuel Pégourié-Gonnard | 045f094 | 2020-07-02 11:34:02 +0200 | [diff] [blame] | 5324 | |
Manuel Pégourié-Gonnard | ed0e864 | 2020-07-21 11:20:30 +0200 | [diff] [blame] | 5325 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC:MBEDTLS_TEST_HOOKS */ |
Manuel Pégourié-Gonnard | 045f094 | 2020-07-02 11:34:02 +0200 | [diff] [blame] | 5326 | void ssl_cf_hmac( int hash ) |
| 5327 | { |
| 5328 | /* |
Gabor Mezei | 90437e3 | 2021-10-20 11:59:27 +0200 | [diff] [blame] | 5329 | * Test the function mbedtls_ct_hmac() against a reference |
Manuel Pégourié-Gonnard | 045f094 | 2020-07-02 11:34:02 +0200 | [diff] [blame] | 5330 | * implementation. |
Manuel Pégourié-Gonnard | 045f094 | 2020-07-02 11:34:02 +0200 | [diff] [blame] | 5331 | */ |
Neil Armstrong | 2968d30 | 2022-02-25 15:09:36 +0100 | [diff] [blame] | 5332 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 5333 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5334 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5335 | psa_algorithm_t alg; |
| 5336 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 5337 | #else |
Manuel Pégourié-Gonnard | 045f094 | 2020-07-02 11:34:02 +0200 | [diff] [blame] | 5338 | mbedtls_md_context_t ctx, ref_ctx; |
| 5339 | const mbedtls_md_info_t *md_info; |
Neil Armstrong | 2968d30 | 2022-02-25 15:09:36 +0100 | [diff] [blame] | 5340 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 045f094 | 2020-07-02 11:34:02 +0200 | [diff] [blame] | 5341 | size_t out_len, block_size; |
| 5342 | size_t min_in_len, in_len, max_in_len, i; |
| 5343 | /* TLS additional data is 13 bytes (hence the "lucky 13" name) */ |
| 5344 | unsigned char add_data[13]; |
| 5345 | unsigned char ref_out[MBEDTLS_MD_MAX_SIZE]; |
| 5346 | unsigned char *data = NULL; |
| 5347 | unsigned char *out = NULL; |
| 5348 | unsigned char rec_num = 0; |
| 5349 | |
Neil Armstrong | 2968d30 | 2022-02-25 15:09:36 +0100 | [diff] [blame] | 5350 | USE_PSA_INIT( ); |
| 5351 | |
| 5352 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 5353 | alg = PSA_ALG_HMAC( mbedtls_psa_translate_md( hash ) ); |
| 5354 | |
| 5355 | out_len = PSA_HASH_LENGTH( alg ); |
| 5356 | block_size = PSA_HASH_BLOCK_LENGTH( alg ); |
| 5357 | |
| 5358 | /* mbedtls_ct_hmac() requires the key to be exportable */ |
| 5359 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT | |
| 5360 | PSA_KEY_USAGE_VERIFY_HASH ); |
| 5361 | psa_set_key_algorithm( &attributes, PSA_ALG_HMAC( alg ) ); |
| 5362 | psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC ); |
| 5363 | #else |
Manuel Pégourié-Gonnard | 045f094 | 2020-07-02 11:34:02 +0200 | [diff] [blame] | 5364 | mbedtls_md_init( &ctx ); |
| 5365 | mbedtls_md_init( &ref_ctx ); |
| 5366 | |
| 5367 | md_info = mbedtls_md_info_from_type( hash ); |
| 5368 | TEST_ASSERT( md_info != NULL ); |
| 5369 | out_len = mbedtls_md_get_size( md_info ); |
| 5370 | TEST_ASSERT( out_len != 0 ); |
| 5371 | block_size = hash == MBEDTLS_MD_SHA384 ? 128 : 64; |
Neil Armstrong | 2968d30 | 2022-02-25 15:09:36 +0100 | [diff] [blame] | 5372 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 045f094 | 2020-07-02 11:34:02 +0200 | [diff] [blame] | 5373 | |
| 5374 | /* Use allocated out buffer to catch overwrites */ |
| 5375 | ASSERT_ALLOC( out, out_len ); |
| 5376 | |
Neil Armstrong | 2968d30 | 2022-02-25 15:09:36 +0100 | [diff] [blame] | 5377 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 5378 | /* Set up dummy key */ |
| 5379 | memset( ref_out, 42, sizeof( ref_out ) ); |
| 5380 | TEST_EQUAL( PSA_SUCCESS, psa_import_key( &attributes, |
| 5381 | ref_out, out_len, |
| 5382 | &key ) ); |
| 5383 | #else |
Manuel Pégourié-Gonnard | 045f094 | 2020-07-02 11:34:02 +0200 | [diff] [blame] | 5384 | /* Set up contexts with the given hash and a dummy key */ |
| 5385 | TEST_EQUAL( 0, mbedtls_md_setup( &ctx, md_info, 1 ) ); |
| 5386 | TEST_EQUAL( 0, mbedtls_md_setup( &ref_ctx, md_info, 1 ) ); |
| 5387 | memset( ref_out, 42, sizeof( ref_out ) ); |
| 5388 | TEST_EQUAL( 0, mbedtls_md_hmac_starts( &ctx, ref_out, out_len ) ); |
| 5389 | TEST_EQUAL( 0, mbedtls_md_hmac_starts( &ref_ctx, ref_out, out_len ) ); |
| 5390 | memset( ref_out, 0, sizeof( ref_out ) ); |
Neil Armstrong | 2968d30 | 2022-02-25 15:09:36 +0100 | [diff] [blame] | 5391 | #endif |
Manuel Pégourié-Gonnard | 045f094 | 2020-07-02 11:34:02 +0200 | [diff] [blame] | 5392 | |
| 5393 | /* |
| 5394 | * Test all possible lengths up to a point. The difference between |
| 5395 | * max_in_len and min_in_len is at most 255, and make sure they both vary |
| 5396 | * by at least one block size. |
| 5397 | */ |
| 5398 | for( max_in_len = 0; max_in_len <= 255 + block_size; max_in_len++ ) |
| 5399 | { |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 5400 | mbedtls_test_set_step( max_in_len * 10000 ); |
Manuel Pégourié-Gonnard | ca8287c | 2020-07-22 10:29:39 +0200 | [diff] [blame] | 5401 | |
Manuel Pégourié-Gonnard | 045f094 | 2020-07-02 11:34:02 +0200 | [diff] [blame] | 5402 | /* Use allocated in buffer to catch overreads */ |
Manuel Pégourié-Gonnard | c321900 | 2020-07-22 10:32:52 +0200 | [diff] [blame] | 5403 | ASSERT_ALLOC( data, max_in_len ); |
Manuel Pégourié-Gonnard | 045f094 | 2020-07-02 11:34:02 +0200 | [diff] [blame] | 5404 | |
| 5405 | min_in_len = max_in_len > 255 ? max_in_len - 255 : 0; |
| 5406 | for( in_len = min_in_len; in_len <= max_in_len; in_len++ ) |
| 5407 | { |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 5408 | mbedtls_test_set_step( max_in_len * 10000 + in_len ); |
Manuel Pégourié-Gonnard | ca8287c | 2020-07-22 10:29:39 +0200 | [diff] [blame] | 5409 | |
Manuel Pégourié-Gonnard | 045f094 | 2020-07-02 11:34:02 +0200 | [diff] [blame] | 5410 | /* Set up dummy data and add_data */ |
| 5411 | rec_num++; |
| 5412 | memset( add_data, rec_num, sizeof( add_data ) ); |
| 5413 | for( i = 0; i < in_len; i++ ) |
| 5414 | data[i] = ( i & 0xff ) ^ rec_num; |
| 5415 | |
| 5416 | /* Get the function's result */ |
Manuel Pégourié-Gonnard | 9670a59 | 2020-07-10 10:21:46 +0200 | [diff] [blame] | 5417 | TEST_CF_SECRET( &in_len, sizeof( in_len ) ); |
Neil Armstrong | 2968d30 | 2022-02-25 15:09:36 +0100 | [diff] [blame] | 5418 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 5419 | TEST_EQUAL( 0, mbedtls_ct_hmac( key, PSA_ALG_HMAC( alg ), |
| 5420 | add_data, sizeof( add_data ), |
| 5421 | data, in_len, |
| 5422 | min_in_len, max_in_len, |
| 5423 | out ) ); |
| 5424 | #else |
Gabor Mezei | 90437e3 | 2021-10-20 11:59:27 +0200 | [diff] [blame] | 5425 | TEST_EQUAL( 0, mbedtls_ct_hmac( &ctx, add_data, sizeof( add_data ), |
gabor-mezei-arm | 9fa43ce | 2021-09-28 16:14:47 +0200 | [diff] [blame] | 5426 | data, in_len, |
| 5427 | min_in_len, max_in_len, |
| 5428 | out ) ); |
Neil Armstrong | 2968d30 | 2022-02-25 15:09:36 +0100 | [diff] [blame] | 5429 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 9670a59 | 2020-07-10 10:21:46 +0200 | [diff] [blame] | 5430 | TEST_CF_PUBLIC( &in_len, sizeof( in_len ) ); |
| 5431 | TEST_CF_PUBLIC( out, out_len ); |
Manuel Pégourié-Gonnard | 045f094 | 2020-07-02 11:34:02 +0200 | [diff] [blame] | 5432 | |
Neil Armstrong | 2968d30 | 2022-02-25 15:09:36 +0100 | [diff] [blame] | 5433 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 5434 | TEST_EQUAL( PSA_SUCCESS, psa_mac_verify_setup( &operation, |
| 5435 | key, alg ) ); |
| 5436 | TEST_EQUAL( PSA_SUCCESS, psa_mac_update( &operation, add_data, |
| 5437 | sizeof( add_data ) ) ); |
| 5438 | TEST_EQUAL( PSA_SUCCESS, psa_mac_update( &operation, |
| 5439 | data, in_len ) ); |
| 5440 | TEST_EQUAL( PSA_SUCCESS, psa_mac_verify_finish( &operation, |
| 5441 | out, out_len ) ); |
| 5442 | #else |
Manuel Pégourié-Gonnard | 045f094 | 2020-07-02 11:34:02 +0200 | [diff] [blame] | 5443 | /* Compute the reference result */ |
| 5444 | TEST_EQUAL( 0, mbedtls_md_hmac_update( &ref_ctx, add_data, |
| 5445 | sizeof( add_data ) ) ); |
| 5446 | TEST_EQUAL( 0, mbedtls_md_hmac_update( &ref_ctx, data, in_len ) ); |
| 5447 | TEST_EQUAL( 0, mbedtls_md_hmac_finish( &ref_ctx, ref_out ) ); |
| 5448 | TEST_EQUAL( 0, mbedtls_md_hmac_reset( &ref_ctx ) ); |
| 5449 | |
| 5450 | /* Compare */ |
| 5451 | ASSERT_COMPARE( out, out_len, ref_out, out_len ); |
Neil Armstrong | 2968d30 | 2022-02-25 15:09:36 +0100 | [diff] [blame] | 5452 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 045f094 | 2020-07-02 11:34:02 +0200 | [diff] [blame] | 5453 | } |
| 5454 | |
| 5455 | mbedtls_free( data ); |
| 5456 | data = NULL; |
| 5457 | } |
| 5458 | |
| 5459 | exit: |
Neil Armstrong | 2968d30 | 2022-02-25 15:09:36 +0100 | [diff] [blame] | 5460 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 5461 | psa_mac_abort( &operation ); |
| 5462 | psa_destroy_key( key ); |
| 5463 | #else |
Manuel Pégourié-Gonnard | 045f094 | 2020-07-02 11:34:02 +0200 | [diff] [blame] | 5464 | mbedtls_md_free( &ref_ctx ); |
| 5465 | mbedtls_md_free( &ctx ); |
Neil Armstrong | 2968d30 | 2022-02-25 15:09:36 +0100 | [diff] [blame] | 5466 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 045f094 | 2020-07-02 11:34:02 +0200 | [diff] [blame] | 5467 | |
| 5468 | mbedtls_free( data ); |
| 5469 | mbedtls_free( out ); |
Neil Armstrong | 2968d30 | 2022-02-25 15:09:36 +0100 | [diff] [blame] | 5470 | |
| 5471 | USE_PSA_DONE( ); |
Manuel Pégourié-Gonnard | 045f094 | 2020-07-02 11:34:02 +0200 | [diff] [blame] | 5472 | } |
| 5473 | /* END_CASE */ |
Manuel Pégourié-Gonnard | 7fe2c5f | 2020-08-18 12:02:54 +0200 | [diff] [blame] | 5474 | |
| 5475 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC:MBEDTLS_TEST_HOOKS */ |
| 5476 | void ssl_cf_memcpy_offset( int offset_min, int offset_max, int len ) |
| 5477 | { |
| 5478 | unsigned char *dst = NULL; |
| 5479 | unsigned char *src = NULL; |
| 5480 | size_t src_len = offset_max + len; |
| 5481 | size_t secret; |
| 5482 | |
| 5483 | ASSERT_ALLOC( dst, len ); |
| 5484 | ASSERT_ALLOC( src, src_len ); |
| 5485 | |
| 5486 | /* Fill src in a way that we can detect if we copied the right bytes */ |
| 5487 | mbedtls_test_rnd_std_rand( NULL, src, src_len ); |
| 5488 | |
| 5489 | for( secret = offset_min; secret <= (size_t) offset_max; secret++ ) |
| 5490 | { |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 5491 | mbedtls_test_set_step( (int) secret ); |
Manuel Pégourié-Gonnard | 7fe2c5f | 2020-08-18 12:02:54 +0200 | [diff] [blame] | 5492 | |
| 5493 | TEST_CF_SECRET( &secret, sizeof( secret ) ); |
Gabor Mezei | 90437e3 | 2021-10-20 11:59:27 +0200 | [diff] [blame] | 5494 | mbedtls_ct_memcpy_offset( dst, src, secret, |
gabor-mezei-arm | 9fa43ce | 2021-09-28 16:14:47 +0200 | [diff] [blame] | 5495 | offset_min, offset_max, len ); |
Manuel Pégourié-Gonnard | 7fe2c5f | 2020-08-18 12:02:54 +0200 | [diff] [blame] | 5496 | TEST_CF_PUBLIC( &secret, sizeof( secret ) ); |
| 5497 | TEST_CF_PUBLIC( dst, len ); |
| 5498 | |
| 5499 | ASSERT_COMPARE( dst, len, src + secret, len ); |
| 5500 | } |
| 5501 | |
| 5502 | exit: |
| 5503 | mbedtls_free( dst ); |
| 5504 | mbedtls_free( src ); |
| 5505 | } |
| 5506 | /* END_CASE */ |
Hanno Becker | 6667ffd | 2021-04-19 21:59:22 +0100 | [diff] [blame] | 5507 | |
| 5508 | /* BEGIN_CASE depends_on:MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
| 5509 | void test_multiple_psks() |
| 5510 | { |
| 5511 | unsigned char psk0[10] = { 0 }; |
| 5512 | unsigned char psk0_identity[] = { 'f', 'o', 'o' }; |
| 5513 | |
| 5514 | unsigned char psk1[10] = { 0 }; |
| 5515 | unsigned char psk1_identity[] = { 'b', 'a', 'r' }; |
| 5516 | |
| 5517 | mbedtls_ssl_config conf; |
| 5518 | |
Neil Armstrong | 4c3b4e0 | 2022-05-03 09:24:26 +0200 | [diff] [blame] | 5519 | USE_PSA_INIT( ); |
Hanno Becker | 6667ffd | 2021-04-19 21:59:22 +0100 | [diff] [blame] | 5520 | mbedtls_ssl_config_init( &conf ); |
| 5521 | |
| 5522 | TEST_ASSERT( mbedtls_ssl_conf_psk( &conf, |
| 5523 | psk0, sizeof( psk0 ), |
| 5524 | psk0_identity, sizeof( psk0_identity ) ) == 0 ); |
| 5525 | TEST_ASSERT( mbedtls_ssl_conf_psk( &conf, |
| 5526 | psk1, sizeof( psk1 ), |
| 5527 | psk1_identity, sizeof( psk1_identity ) ) == |
| 5528 | MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 5529 | |
| 5530 | exit: |
| 5531 | |
| 5532 | mbedtls_ssl_config_free( &conf ); |
Neil Armstrong | 4c3b4e0 | 2022-05-03 09:24:26 +0200 | [diff] [blame] | 5533 | |
| 5534 | USE_PSA_DONE( ); |
Hanno Becker | 6667ffd | 2021-04-19 21:59:22 +0100 | [diff] [blame] | 5535 | } |
| 5536 | /* END_CASE */ |
| 5537 | |
| 5538 | /* BEGIN_CASE depends_on:MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED:MBEDTLS_USE_PSA_CRYPTO */ |
| 5539 | void test_multiple_psks_opaque( int mode ) |
| 5540 | { |
| 5541 | /* |
| 5542 | * Mode 0: Raw PSK, then opaque PSK |
| 5543 | * Mode 1: Opaque PSK, then raw PSK |
| 5544 | * Mode 2: 2x opaque PSK |
| 5545 | */ |
| 5546 | |
| 5547 | unsigned char psk0_raw[10] = { 0 }; |
| 5548 | unsigned char psk0_raw_identity[] = { 'f', 'o', 'o' }; |
| 5549 | |
Andrzej Kurek | 03e0146 | 2022-01-03 12:53:24 +0100 | [diff] [blame] | 5550 | mbedtls_svc_key_id_t psk0_opaque = mbedtls_svc_key_id_make( 0x1, (psa_key_id_t) 1 ); |
| 5551 | |
Hanno Becker | 6667ffd | 2021-04-19 21:59:22 +0100 | [diff] [blame] | 5552 | unsigned char psk0_opaque_identity[] = { 'f', 'o', 'o' }; |
| 5553 | |
| 5554 | unsigned char psk1_raw[10] = { 0 }; |
| 5555 | unsigned char psk1_raw_identity[] = { 'b', 'a', 'r' }; |
| 5556 | |
Andrzej Kurek | 03e0146 | 2022-01-03 12:53:24 +0100 | [diff] [blame] | 5557 | mbedtls_svc_key_id_t psk1_opaque = mbedtls_svc_key_id_make( 0x1, (psa_key_id_t) 2 ); |
| 5558 | |
Hanno Becker | 6667ffd | 2021-04-19 21:59:22 +0100 | [diff] [blame] | 5559 | unsigned char psk1_opaque_identity[] = { 'b', 'a', 'r' }; |
| 5560 | |
| 5561 | mbedtls_ssl_config conf; |
| 5562 | |
| 5563 | USE_PSA_INIT( ); |
| 5564 | mbedtls_ssl_config_init( &conf ); |
| 5565 | |
| 5566 | switch( mode ) |
| 5567 | { |
| 5568 | case 0: |
| 5569 | |
| 5570 | TEST_ASSERT( mbedtls_ssl_conf_psk( &conf, |
| 5571 | psk0_raw, sizeof( psk0_raw ), |
| 5572 | psk0_raw_identity, sizeof( psk0_raw_identity ) ) |
| 5573 | == 0 ); |
| 5574 | TEST_ASSERT( mbedtls_ssl_conf_psk_opaque( &conf, |
| 5575 | psk1_opaque, |
| 5576 | psk1_opaque_identity, sizeof( psk1_opaque_identity ) ) |
| 5577 | == MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 5578 | break; |
| 5579 | |
| 5580 | case 1: |
| 5581 | |
| 5582 | TEST_ASSERT( mbedtls_ssl_conf_psk_opaque( &conf, |
| 5583 | psk0_opaque, |
| 5584 | psk0_opaque_identity, sizeof( psk0_opaque_identity ) ) |
| 5585 | == 0 ); |
| 5586 | TEST_ASSERT( mbedtls_ssl_conf_psk( &conf, |
| 5587 | psk1_raw, sizeof( psk1_raw ), |
| 5588 | psk1_raw_identity, sizeof( psk1_raw_identity ) ) |
| 5589 | == MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 5590 | |
| 5591 | break; |
| 5592 | |
| 5593 | case 2: |
| 5594 | |
| 5595 | TEST_ASSERT( mbedtls_ssl_conf_psk_opaque( &conf, |
| 5596 | psk0_opaque, |
| 5597 | psk0_opaque_identity, sizeof( psk0_opaque_identity ) ) |
| 5598 | == 0 ); |
| 5599 | TEST_ASSERT( mbedtls_ssl_conf_psk_opaque( &conf, |
| 5600 | psk1_opaque, |
| 5601 | psk1_opaque_identity, sizeof( psk1_opaque_identity ) ) |
| 5602 | == MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 5603 | |
| 5604 | break; |
| 5605 | |
| 5606 | default: |
| 5607 | TEST_ASSERT( 0 ); |
| 5608 | break; |
| 5609 | } |
| 5610 | |
| 5611 | exit: |
| 5612 | |
| 5613 | mbedtls_ssl_config_free( &conf ); |
| 5614 | USE_PSA_DONE( ); |
| 5615 | |
| 5616 | } |
| 5617 | /* END_CASE */ |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 5618 | |
Ronald Cron | 37bdaab | 2022-03-30 16:45:51 +0200 | [diff] [blame] | 5619 | /* BEGIN_CASE */ |
| 5620 | void conf_version( int endpoint, int transport, |
Glenn Strauss | 59b0d54 | 2022-04-12 07:10:06 -0400 | [diff] [blame] | 5621 | int min_tls_version, int max_tls_version, |
Ronald Cron | 37bdaab | 2022-03-30 16:45:51 +0200 | [diff] [blame] | 5622 | int expected_ssl_setup_result ) |
| 5623 | { |
| 5624 | mbedtls_ssl_config conf; |
| 5625 | mbedtls_ssl_context ssl; |
| 5626 | |
| 5627 | mbedtls_ssl_config_init( &conf ); |
| 5628 | mbedtls_ssl_init( &ssl ); |
| 5629 | |
| 5630 | mbedtls_ssl_conf_endpoint( &conf, endpoint ); |
| 5631 | mbedtls_ssl_conf_transport( &conf, transport ); |
Glenn Strauss | 59b0d54 | 2022-04-12 07:10:06 -0400 | [diff] [blame] | 5632 | mbedtls_ssl_conf_min_tls_version( &conf, min_tls_version ); |
| 5633 | mbedtls_ssl_conf_max_tls_version( &conf, max_tls_version ); |
Ronald Cron | 37bdaab | 2022-03-30 16:45:51 +0200 | [diff] [blame] | 5634 | |
| 5635 | TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == expected_ssl_setup_result ); |
| 5636 | |
| 5637 | mbedtls_ssl_free( &ssl ); |
| 5638 | mbedtls_ssl_config_free( &conf ); |
| 5639 | } |
| 5640 | /* END_CASE */ |
| 5641 | |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 5642 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_C:!MBEDTLS_DEPRECATED_REMOVED:!MBEDTLS_DEPRECATED_WARNING:MBEDTLS_ECP_DP_SECP192R1_ENABLED:MBEDTLS_ECP_DP_SECP224R1_ENABLED:MBEDTLS_ECP_DP_SECP256R1_ENABLED */ |
| 5643 | void conf_curve() |
| 5644 | { |
| 5645 | |
| 5646 | mbedtls_ecp_group_id curve_list[] = { MBEDTLS_ECP_DP_SECP192R1, |
| 5647 | MBEDTLS_ECP_DP_SECP224R1, |
| 5648 | MBEDTLS_ECP_DP_SECP256R1, |
| 5649 | MBEDTLS_ECP_DP_NONE }; |
| 5650 | mbedtls_ecp_group_id iana_tls_group_list[] = { MBEDTLS_SSL_IANA_TLS_GROUP_SECP192R1, |
| 5651 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP224R1, |
| 5652 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, |
| 5653 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE }; |
| 5654 | |
| 5655 | mbedtls_ssl_config conf; |
| 5656 | mbedtls_ssl_config_init( &conf ); |
Jerry Yu | baa4934 | 2022-02-15 10:26:40 +0800 | [diff] [blame] | 5657 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Glenn Strauss | e3af4cb | 2022-03-15 03:23:42 -0400 | [diff] [blame] | 5658 | mbedtls_ssl_conf_max_tls_version( &conf, MBEDTLS_SSL_VERSION_TLS1_2 ); |
| 5659 | mbedtls_ssl_conf_min_tls_version( &conf, MBEDTLS_SSL_VERSION_TLS1_2 ); |
Jerry Yu | baa4934 | 2022-02-15 10:26:40 +0800 | [diff] [blame] | 5660 | #else |
Glenn Strauss | e3af4cb | 2022-03-15 03:23:42 -0400 | [diff] [blame] | 5661 | mbedtls_ssl_conf_max_tls_version( &conf, MBEDTLS_SSL_VERSION_TLS1_3 ); |
| 5662 | mbedtls_ssl_conf_min_tls_version( &conf, MBEDTLS_SSL_VERSION_TLS1_3 ); |
Jerry Yu | baa4934 | 2022-02-15 10:26:40 +0800 | [diff] [blame] | 5663 | #endif |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 5664 | mbedtls_ssl_conf_curves( &conf, curve_list ); |
| 5665 | |
| 5666 | mbedtls_ssl_context ssl; |
| 5667 | mbedtls_ssl_init( &ssl ); |
Paul Elliott | 46a6c20 | 2021-12-09 18:16:13 +0000 | [diff] [blame] | 5668 | TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 ); |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 5669 | |
| 5670 | TEST_ASSERT( ssl.handshake != NULL && ssl.handshake->group_list != NULL ); |
| 5671 | TEST_ASSERT( ssl.conf != NULL && ssl.conf->group_list == NULL ); |
| 5672 | |
| 5673 | TEST_EQUAL( ssl.handshake->group_list[ARRAY_LENGTH( iana_tls_group_list ) - 1], MBEDTLS_SSL_IANA_TLS_GROUP_NONE ); |
| 5674 | |
| 5675 | for( size_t i = 0; i < ARRAY_LENGTH( iana_tls_group_list ); i++ ) |
| 5676 | TEST_EQUAL( iana_tls_group_list[i], ssl.handshake->group_list[i] ); |
| 5677 | |
| 5678 | mbedtls_ssl_free( &ssl ); |
| 5679 | mbedtls_ssl_config_free( &conf ); |
| 5680 | } |
| 5681 | /* END_CASE */ |
| 5682 | |
| 5683 | /* BEGIN_CASE depends_on:MBEDTLS_DEPRECATED_REMOVED */ |
| 5684 | void conf_group() |
| 5685 | { |
| 5686 | uint16_t iana_tls_group_list[] = { MBEDTLS_SSL_IANA_TLS_GROUP_SECP192R1, |
| 5687 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP224R1, |
| 5688 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, |
| 5689 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE }; |
| 5690 | |
| 5691 | mbedtls_ssl_config conf; |
| 5692 | mbedtls_ssl_config_init( &conf ); |
| 5693 | |
Glenn Strauss | e3af4cb | 2022-03-15 03:23:42 -0400 | [diff] [blame] | 5694 | mbedtls_ssl_conf_max_tls_version( &conf, MBEDTLS_SSL_VERSION_TLS1_2 ); |
| 5695 | mbedtls_ssl_conf_min_tls_version( &conf, MBEDTLS_SSL_VERSION_TLS1_2 ); |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 5696 | |
| 5697 | mbedtls_ssl_conf_groups( &conf, iana_tls_group_list ); |
| 5698 | |
| 5699 | mbedtls_ssl_context ssl; |
| 5700 | mbedtls_ssl_init( &ssl ); |
Paul Elliott | 46a6c20 | 2021-12-09 18:16:13 +0000 | [diff] [blame] | 5701 | TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 ); |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 5702 | |
| 5703 | TEST_ASSERT( ssl.conf != NULL && ssl.conf->group_list != NULL ); |
| 5704 | |
| 5705 | TEST_EQUAL( ssl.conf->group_list[ARRAY_LENGTH( iana_tls_group_list ) - 1], MBEDTLS_SSL_IANA_TLS_GROUP_NONE ); |
| 5706 | |
| 5707 | for( size_t i = 0; i < ARRAY_LENGTH( iana_tls_group_list ); i++ ) |
| 5708 | TEST_EQUAL( iana_tls_group_list[i], ssl.conf->group_list[i] ); |
| 5709 | |
| 5710 | mbedtls_ssl_free( &ssl ); |
| 5711 | mbedtls_ssl_config_free( &conf ); |
| 5712 | } |
| 5713 | /* END_CASE */ |
Paul Elliott | b9af2db | 2022-03-09 15:34:37 +0000 | [diff] [blame] | 5714 | |
Andrzej Kurek | e8ad0d7 | 2022-06-11 09:43:45 -0400 | [diff] [blame] | 5715 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_SRV_C:MBEDTLS_SSL_CACHE_C:!MBEDTLS_SSL_PROTO_TLS1_3:MBEDTLS_DEBUG_C:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_PKCS1_V15:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */ |
Andrzej Kurek | 514683a | 2022-06-10 10:33:05 -0400 | [diff] [blame] | 5716 | void force_bad_session_id_len( ) |
| 5717 | { |
| 5718 | enum { BUFFSIZE = 1024 }; |
| 5719 | handshake_test_options options; |
| 5720 | mbedtls_endpoint client, server; |
| 5721 | log_pattern srv_pattern, cli_pattern; |
| 5722 | mbedtls_test_message_socket_context server_context, client_context; |
| 5723 | |
| 5724 | srv_pattern.pattern = cli_pattern.pattern = "cache did not store session"; |
| 5725 | srv_pattern.counter = 0; |
| 5726 | init_handshake_options( &options ); |
| 5727 | |
| 5728 | options.srv_log_obj = &srv_pattern; |
| 5729 | options.srv_log_fun = log_analyzer; |
| 5730 | |
| 5731 | USE_PSA_INIT( ); |
| 5732 | |
| 5733 | mbedtls_message_socket_init( &server_context ); |
| 5734 | mbedtls_message_socket_init( &client_context ); |
| 5735 | |
| 5736 | TEST_ASSERT( mbedtls_endpoint_init( &client, MBEDTLS_SSL_IS_CLIENT, |
| 5737 | &options, NULL, NULL, |
Andrzej Kurek | 626a931 | 2022-06-10 11:07:39 -0400 | [diff] [blame] | 5738 | NULL, NULL ) == 0 ); |
Andrzej Kurek | 514683a | 2022-06-10 10:33:05 -0400 | [diff] [blame] | 5739 | |
| 5740 | TEST_ASSERT( mbedtls_endpoint_init( &server, MBEDTLS_SSL_IS_SERVER, |
Andrzej Kurek | 626a931 | 2022-06-10 11:07:39 -0400 | [diff] [blame] | 5741 | &options, NULL, NULL, NULL, |
| 5742 | NULL ) == 0 ); |
Andrzej Kurek | 514683a | 2022-06-10 10:33:05 -0400 | [diff] [blame] | 5743 | |
| 5744 | mbedtls_debug_set_threshold( 1 ); |
| 5745 | mbedtls_ssl_conf_dbg( &server.conf, options.srv_log_fun, |
| 5746 | options.srv_log_obj ); |
| 5747 | |
| 5748 | TEST_ASSERT( mbedtls_mock_socket_connect( &(client.socket), |
| 5749 | &(server.socket), |
| 5750 | BUFFSIZE ) == 0 ); |
| 5751 | |
| 5752 | TEST_ASSERT( mbedtls_move_handshake_to_state( &(client.ssl), |
| 5753 | &(server.ssl), |
| 5754 | MBEDTLS_SSL_HANDSHAKE_WRAPUP ) |
| 5755 | == 0 ); |
| 5756 | /* Force a bad session_id_len that will be read by the server in |
| 5757 | * mbedtls_ssl_cache_set. */ |
| 5758 | server.ssl.session_negotiate->id_len = 33; |
| 5759 | if( options.cli_msg_len != 0 || options.srv_msg_len != 0 ) |
| 5760 | { |
| 5761 | /* Start data exchanging test */ |
| 5762 | TEST_ASSERT( mbedtls_exchange_data( &(client.ssl), options.cli_msg_len, |
| 5763 | options.expected_cli_fragments, |
| 5764 | &(server.ssl), options.srv_msg_len, |
| 5765 | options.expected_srv_fragments ) |
| 5766 | == 0 ); |
| 5767 | } |
Andrzej Kurek | 514683a | 2022-06-10 10:33:05 -0400 | [diff] [blame] | 5768 | |
| 5769 | /* Make sure that the cache did not store the session */ |
| 5770 | TEST_EQUAL( srv_pattern.counter, 1 ); |
| 5771 | exit: |
| 5772 | mbedtls_endpoint_free( &client, NULL ); |
| 5773 | mbedtls_endpoint_free( &server, NULL ); |
| 5774 | free_handshake_options( &options ); |
| 5775 | mbedtls_debug_set_threshold( 0 ); |
| 5776 | USE_PSA_DONE( ); |
| 5777 | } |
| 5778 | /* END_CASE */ |
| 5779 | |
Andrzej Kurek | ed4d217 | 2022-06-08 11:57:57 -0400 | [diff] [blame] | 5780 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_SRV_C:MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE:MBEDTLS_TEST_HOOKS */ |
Andrzej Kurek | cfb0194 | 2022-06-06 13:08:23 -0400 | [diff] [blame] | 5781 | void cookie_parsing( data_t *cookie, int exp_ret ) |
| 5782 | { |
| 5783 | mbedtls_ssl_context ssl; |
| 5784 | mbedtls_ssl_config conf; |
| 5785 | size_t len; |
| 5786 | |
| 5787 | mbedtls_ssl_init( &ssl ); |
| 5788 | mbedtls_ssl_config_init( &conf ); |
| 5789 | TEST_EQUAL( mbedtls_ssl_config_defaults( &conf, MBEDTLS_SSL_IS_SERVER, |
| 5790 | MBEDTLS_SSL_TRANSPORT_DATAGRAM, |
| 5791 | MBEDTLS_SSL_PRESET_DEFAULT ), |
| 5792 | 0 ); |
| 5793 | |
| 5794 | TEST_EQUAL( mbedtls_ssl_setup( &ssl, &conf ), 0 ); |
Andrzej Kurek | 078e9bc | 2022-06-08 11:47:33 -0400 | [diff] [blame] | 5795 | TEST_EQUAL( mbedtls_ssl_check_dtls_clihlo_cookie( &ssl, ssl.cli_id, |
| 5796 | ssl.cli_id_len, |
| 5797 | cookie->x, cookie->len, |
| 5798 | ssl.out_buf, |
| 5799 | MBEDTLS_SSL_OUT_CONTENT_LEN, |
| 5800 | &len ), |
Andrzej Kurek | cfb0194 | 2022-06-06 13:08:23 -0400 | [diff] [blame] | 5801 | exp_ret ); |
| 5802 | |
| 5803 | mbedtls_ssl_free( &ssl ); |
| 5804 | mbedtls_ssl_config_free( &conf ); |
| 5805 | } |
| 5806 | /* END_CASE */ |
| 5807 | |
Paul Elliott | b9af2db | 2022-03-09 15:34:37 +0000 | [diff] [blame] | 5808 | /* BEGIN_CASE depends_on:MBEDTLS_TIMING_C:MBEDTLS_HAVE_TIME */ |
Paul Elliott | 42d5e51 | 2022-03-24 19:41:28 +0000 | [diff] [blame] | 5809 | void timing_final_delay_accessor( ) |
Paul Elliott | b9af2db | 2022-03-09 15:34:37 +0000 | [diff] [blame] | 5810 | { |
| 5811 | mbedtls_timing_delay_context delay_context; |
| 5812 | |
| 5813 | mbedtls_timing_set_delay( &delay_context, 50, 100 ); |
| 5814 | |
| 5815 | TEST_ASSERT( mbedtls_timing_get_final_delay( &delay_context ) == 100 ); |
| 5816 | } |
| 5817 | /* END_CASE */ |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 5818 | |
| 5819 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 5820 | void cid_sanity( ) |
| 5821 | { |
| 5822 | mbedtls_ssl_context ssl; |
| 5823 | mbedtls_ssl_config conf; |
| 5824 | |
| 5825 | unsigned char own_cid[MBEDTLS_SSL_CID_IN_LEN_MAX]; |
| 5826 | unsigned char test_cid[MBEDTLS_SSL_CID_IN_LEN_MAX]; |
| 5827 | int cid_enabled; |
| 5828 | size_t own_cid_len; |
| 5829 | |
| 5830 | mbedtls_test_rnd_std_rand( NULL, own_cid, sizeof( own_cid ) ); |
| 5831 | |
| 5832 | mbedtls_ssl_init( &ssl ); |
| 5833 | mbedtls_ssl_config_init( &conf ); |
| 5834 | |
| 5835 | TEST_ASSERT( mbedtls_ssl_config_defaults( &conf, |
| 5836 | MBEDTLS_SSL_IS_CLIENT, |
| 5837 | MBEDTLS_SSL_TRANSPORT_STREAM, |
| 5838 | MBEDTLS_SSL_PRESET_DEFAULT ) |
| 5839 | == 0 ); |
| 5840 | |
| 5841 | TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 ); |
| 5842 | |
| 5843 | /* Can't use CID functions with stream transport. */ |
| 5844 | TEST_ASSERT( mbedtls_ssl_set_cid( &ssl, MBEDTLS_SSL_CID_ENABLED, own_cid, |
| 5845 | sizeof( own_cid ) ) |
| 5846 | == MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5847 | |
| 5848 | TEST_ASSERT( mbedtls_ssl_get_own_cid( &ssl, &cid_enabled, test_cid, |
| 5849 | &own_cid_len ) |
| 5850 | == MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5851 | |
| 5852 | TEST_ASSERT( mbedtls_ssl_config_defaults( &conf, |
| 5853 | MBEDTLS_SSL_IS_CLIENT, |
| 5854 | MBEDTLS_SSL_TRANSPORT_DATAGRAM, |
| 5855 | MBEDTLS_SSL_PRESET_DEFAULT ) |
| 5856 | == 0 ); |
| 5857 | |
| 5858 | /* Attempt to set config cid size too big. */ |
| 5859 | TEST_ASSERT( mbedtls_ssl_conf_cid( &conf, MBEDTLS_SSL_CID_IN_LEN_MAX + 1, |
| 5860 | MBEDTLS_SSL_UNEXPECTED_CID_IGNORE ) |
| 5861 | == MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5862 | |
| 5863 | TEST_ASSERT( mbedtls_ssl_conf_cid( &conf, sizeof( own_cid ), |
| 5864 | MBEDTLS_SSL_UNEXPECTED_CID_IGNORE ) |
| 5865 | == 0 ); |
| 5866 | |
| 5867 | /* Attempt to set CID length not matching config. */ |
| 5868 | TEST_ASSERT( mbedtls_ssl_set_cid( &ssl, MBEDTLS_SSL_CID_ENABLED, own_cid, |
| 5869 | MBEDTLS_SSL_CID_IN_LEN_MAX - 1 ) |
| 5870 | == MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5871 | |
| 5872 | TEST_ASSERT( mbedtls_ssl_set_cid( &ssl, MBEDTLS_SSL_CID_ENABLED, own_cid, |
| 5873 | sizeof( own_cid ) ) |
| 5874 | == 0 ); |
| 5875 | |
| 5876 | /* Test we get back what we put in. */ |
| 5877 | TEST_ASSERT( mbedtls_ssl_get_own_cid( &ssl, &cid_enabled, test_cid, |
| 5878 | &own_cid_len ) |
| 5879 | == 0 ); |
| 5880 | |
| 5881 | TEST_EQUAL( cid_enabled, MBEDTLS_SSL_CID_ENABLED ); |
| 5882 | ASSERT_COMPARE( own_cid, own_cid_len, test_cid, own_cid_len ); |
| 5883 | |
| 5884 | /* Test disabling works. */ |
| 5885 | TEST_ASSERT( mbedtls_ssl_set_cid( &ssl, MBEDTLS_SSL_CID_DISABLED, NULL, |
| 5886 | 0 ) |
| 5887 | == 0 ); |
| 5888 | |
| 5889 | TEST_ASSERT( mbedtls_ssl_get_own_cid( &ssl, &cid_enabled, test_cid, |
| 5890 | &own_cid_len ) |
| 5891 | == 0 ); |
| 5892 | |
| 5893 | TEST_EQUAL( cid_enabled, MBEDTLS_SSL_CID_DISABLED ); |
| 5894 | |
| 5895 | mbedtls_ssl_free( &ssl ); |
| 5896 | mbedtls_ssl_config_free( &conf ); |
| 5897 | } |
| 5898 | /* END_CASE */ |
| 5899 | |
Andrzej Kurek | 28f883e | 2022-04-08 07:55:27 -0400 | [diff] [blame] | 5900 | /* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_ECP_DP_SECP256R1_ENABLED:MBEDTLS_ENTROPY_C:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_CTR_DRBG_C:MBEDTLS_ECP_C:MBEDTLS_ECDSA_C */ |
Gilles Peskine | b4f874d | 2022-04-08 16:48:09 -0400 | [diff] [blame] | 5901 | void raw_key_agreement_fail( int bad_server_ecdhe_key ) |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 5902 | { |
| 5903 | enum { BUFFSIZE = 17000 }; |
| 5904 | mbedtls_endpoint client, server; |
| 5905 | mbedtls_psa_stats_t stats; |
Andrzej Kurek | 39d88d4 | 2022-03-31 06:30:54 -0400 | [diff] [blame] | 5906 | size_t free_slots_before = -1; |
Andrzej Kurek | 626a931 | 2022-06-10 11:07:39 -0400 | [diff] [blame] | 5907 | handshake_test_options options; |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 5908 | |
Andrzej Kurek | cc28e9a | 2022-03-08 18:36:35 -0500 | [diff] [blame] | 5909 | uint16_t iana_tls_group_list[] = { MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, |
| 5910 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE }; |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 5911 | USE_PSA_INIT( ); |
| 5912 | |
Andrzej Kurek | 626a931 | 2022-06-10 11:07:39 -0400 | [diff] [blame] | 5913 | init_handshake_options( &options ); |
| 5914 | options.pk_alg = MBEDTLS_PK_ECDSA; |
| 5915 | |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 5916 | /* Client side, force SECP256R1 to make one key bitflip fail |
Andrzej Kurek | 83e60ee | 2022-04-14 08:51:41 -0400 | [diff] [blame] | 5917 | * the raw key agreement. Flipping the first byte makes the |
| 5918 | * required 0x04 identifier invalid. */ |
Andrzej Kurek | 57f58b0 | 2022-04-08 08:10:53 -0400 | [diff] [blame] | 5919 | TEST_EQUAL( mbedtls_endpoint_init( &client, MBEDTLS_SSL_IS_CLIENT, |
Andrzej Kurek | 626a931 | 2022-06-10 11:07:39 -0400 | [diff] [blame] | 5920 | &options, NULL, NULL, |
Andrzej Kurek | 57f58b0 | 2022-04-08 08:10:53 -0400 | [diff] [blame] | 5921 | NULL, iana_tls_group_list ), 0 ); |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 5922 | |
| 5923 | /* Server side */ |
Andrzej Kurek | 57f58b0 | 2022-04-08 08:10:53 -0400 | [diff] [blame] | 5924 | TEST_EQUAL( mbedtls_endpoint_init( &server, MBEDTLS_SSL_IS_SERVER, |
Andrzej Kurek | 626a931 | 2022-06-10 11:07:39 -0400 | [diff] [blame] | 5925 | &options, NULL, NULL, |
| 5926 | NULL, NULL ), 0 ); |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 5927 | |
Andrzej Kurek | 57f58b0 | 2022-04-08 08:10:53 -0400 | [diff] [blame] | 5928 | TEST_EQUAL( mbedtls_mock_socket_connect( &(client.socket), |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 5929 | &(server.socket), |
Andrzej Kurek | 57f58b0 | 2022-04-08 08:10:53 -0400 | [diff] [blame] | 5930 | BUFFSIZE ), 0 ); |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 5931 | |
Andrzej Kurek | 57f58b0 | 2022-04-08 08:10:53 -0400 | [diff] [blame] | 5932 | TEST_EQUAL( mbedtls_move_handshake_to_state( &(client.ssl), |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 5933 | &(server.ssl), |
| 5934 | MBEDTLS_SSL_CLIENT_KEY_EXCHANGE ) |
Andrzej Kurek | 57f58b0 | 2022-04-08 08:10:53 -0400 | [diff] [blame] | 5935 | , 0 ); |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 5936 | |
Andrzej Kurek | 39d88d4 | 2022-03-31 06:30:54 -0400 | [diff] [blame] | 5937 | mbedtls_psa_get_stats( &stats ); |
| 5938 | /* Save the number of slots in use up to this point. |
| 5939 | * With PSA, one can be used for the ECDH private key. */ |
| 5940 | free_slots_before = stats.empty_slots; |
Andrzej Kurek | cb33bc5 | 2022-03-31 07:17:18 -0400 | [diff] [blame] | 5941 | |
Gilles Peskine | b4f874d | 2022-04-08 16:48:09 -0400 | [diff] [blame] | 5942 | if( bad_server_ecdhe_key ) |
| 5943 | { |
| 5944 | /* Force a simulated bitflip in the server key. to make the |
| 5945 | * raw key agreement in ssl_write_client_key_exchange fail. */ |
| 5946 | (client.ssl).handshake->ecdh_psa_peerkey[0] ^= 0x02; |
| 5947 | } |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 5948 | |
Gilles Peskine | b4f874d | 2022-04-08 16:48:09 -0400 | [diff] [blame] | 5949 | TEST_EQUAL( mbedtls_move_handshake_to_state( &(client.ssl), |
| 5950 | &(server.ssl), |
| 5951 | MBEDTLS_SSL_HANDSHAKE_OVER ), |
| 5952 | bad_server_ecdhe_key ? MBEDTLS_ERR_SSL_HW_ACCEL_FAILED : 0 ); |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 5953 | |
| 5954 | mbedtls_psa_get_stats( &stats ); |
| 5955 | |
Gilles Peskine | b4f874d | 2022-04-08 16:48:09 -0400 | [diff] [blame] | 5956 | /* Make sure that the key slot is already destroyed in case of failure, |
| 5957 | * without waiting to close the connection. */ |
| 5958 | if( bad_server_ecdhe_key ) |
| 5959 | TEST_EQUAL( free_slots_before, stats.empty_slots ); |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 5960 | |
| 5961 | exit: |
Andrzej Kurek | 28f883e | 2022-04-08 07:55:27 -0400 | [diff] [blame] | 5962 | mbedtls_endpoint_free( &client, NULL ); |
| 5963 | mbedtls_endpoint_free( &server, NULL ); |
Andrzej Kurek | 626a931 | 2022-06-10 11:07:39 -0400 | [diff] [blame] | 5964 | free_handshake_options( &options ); |
Andrzej Kurek | cb33bc5 | 2022-03-31 07:17:18 -0400 | [diff] [blame] | 5965 | |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 5966 | USE_PSA_DONE( ); |
| 5967 | } |
| 5968 | /* END_CASE */ |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 5969 | /* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS:MBEDTLS_SSL_PROTO_TLS1_3:!MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_SSL_CLI_C:MBEDTLS_SSL_SRV_C:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED */ |
| 5970 | void tls13_server_certificate_msg_invalid_vector_len( ) |
| 5971 | { |
| 5972 | int ret = -1; |
| 5973 | mbedtls_endpoint client_ep, server_ep; |
| 5974 | unsigned char *buf, *end; |
| 5975 | size_t buf_len; |
| 5976 | int step = 0; |
| 5977 | int expected_result; |
Ronald Cron | e7b9b6b | 2022-06-10 17:24:31 +0200 | [diff] [blame] | 5978 | mbedtls_ssl_chk_buf_ptr_args expected_chk_buf_ptr_args; |
Paul Elliott | 9a8d784 | 2022-07-10 12:48:57 +0100 | [diff] [blame] | 5979 | handshake_test_options client_options; |
| 5980 | handshake_test_options server_options; |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 5981 | |
| 5982 | /* |
| 5983 | * Test set-up |
| 5984 | */ |
| 5985 | USE_PSA_INIT( ); |
| 5986 | |
Paul Elliott | 9a8d784 | 2022-07-10 12:48:57 +0100 | [diff] [blame] | 5987 | init_handshake_options( &client_options ); |
| 5988 | client_options.pk_alg = MBEDTLS_PK_ECDSA; |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 5989 | ret = mbedtls_endpoint_init( &client_ep, MBEDTLS_SSL_IS_CLIENT, |
Paul Elliott | 9a8d784 | 2022-07-10 12:48:57 +0100 | [diff] [blame] | 5990 | &client_options, NULL, NULL, NULL, NULL ); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 5991 | TEST_EQUAL( ret, 0 ); |
| 5992 | |
Paul Elliott | 9a8d784 | 2022-07-10 12:48:57 +0100 | [diff] [blame] | 5993 | init_handshake_options( &server_options ); |
| 5994 | server_options.pk_alg = MBEDTLS_PK_ECDSA; |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 5995 | ret = mbedtls_endpoint_init( &server_ep, MBEDTLS_SSL_IS_SERVER, |
Paul Elliott | 9a8d784 | 2022-07-10 12:48:57 +0100 | [diff] [blame] | 5996 | &server_options, NULL, NULL, NULL, NULL ); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 5997 | TEST_EQUAL( ret, 0 ); |
| 5998 | |
| 5999 | ret = mbedtls_mock_socket_connect( &(client_ep.socket), |
| 6000 | &(server_ep.socket), 1024 ); |
| 6001 | TEST_EQUAL( ret, 0 ); |
| 6002 | |
| 6003 | while( 1 ) |
| 6004 | { |
| 6005 | mbedtls_test_set_step( ++step ); |
| 6006 | |
| 6007 | ret = mbedtls_move_handshake_to_state( &(server_ep.ssl), |
| 6008 | &(client_ep.ssl), |
| 6009 | MBEDTLS_SSL_CERTIFICATE_VERIFY ); |
| 6010 | TEST_EQUAL( ret, 0 ); |
| 6011 | |
| 6012 | ret = mbedtls_ssl_flush_output( &(server_ep.ssl) ); |
| 6013 | TEST_EQUAL( ret, 0 ); |
| 6014 | |
| 6015 | ret = mbedtls_move_handshake_to_state( &(client_ep.ssl), |
| 6016 | &(server_ep.ssl), |
| 6017 | MBEDTLS_SSL_SERVER_CERTIFICATE ); |
| 6018 | TEST_EQUAL( ret, 0 ); |
| 6019 | |
| 6020 | ret = mbedtls_ssl_tls13_fetch_handshake_msg( &(client_ep.ssl), |
| 6021 | MBEDTLS_SSL_HS_CERTIFICATE, |
| 6022 | &buf, &buf_len ); |
| 6023 | TEST_EQUAL( ret, 0 ); |
| 6024 | |
| 6025 | end = buf + buf_len; |
| 6026 | |
| 6027 | /* |
| 6028 | * Tweak server Certificate message and parse it. |
| 6029 | */ |
| 6030 | |
| 6031 | ret = tweak_tls13_certificate_msg_vector_len( |
Ronald Cron | e7b9b6b | 2022-06-10 17:24:31 +0200 | [diff] [blame] | 6032 | buf, &end, step, &expected_result, &expected_chk_buf_ptr_args ); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 6033 | |
| 6034 | if( ret != 0 ) |
| 6035 | break; |
| 6036 | |
| 6037 | ret = mbedtls_ssl_tls13_parse_certificate( &(client_ep.ssl), buf, end ); |
| 6038 | TEST_EQUAL( ret, expected_result ); |
| 6039 | |
Ronald Cron | e7b9b6b | 2022-06-10 17:24:31 +0200 | [diff] [blame] | 6040 | TEST_ASSERT( mbedtls_ssl_cmp_chk_buf_ptr_fail_args( |
| 6041 | &expected_chk_buf_ptr_args ) == 0 ); |
| 6042 | |
| 6043 | mbedtls_ssl_reset_chk_buf_ptr_fail_args( ); |
| 6044 | |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 6045 | ret = mbedtls_ssl_session_reset( &(client_ep.ssl) ); |
| 6046 | TEST_EQUAL( ret, 0 ); |
| 6047 | |
| 6048 | ret = mbedtls_ssl_session_reset( &(server_ep.ssl) ); |
| 6049 | TEST_EQUAL( ret, 0 ); |
| 6050 | } |
| 6051 | |
| 6052 | exit: |
Ronald Cron | e7b9b6b | 2022-06-10 17:24:31 +0200 | [diff] [blame] | 6053 | mbedtls_ssl_reset_chk_buf_ptr_fail_args( ); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 6054 | mbedtls_endpoint_free( &client_ep, NULL ); |
| 6055 | mbedtls_endpoint_free( &server_ep, NULL ); |
Paul Elliott | 9a8d784 | 2022-07-10 12:48:57 +0100 | [diff] [blame] | 6056 | free_handshake_options( &client_options ); |
| 6057 | free_handshake_options( &server_options ); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 6058 | USE_PSA_DONE( ); |
| 6059 | } |
| 6060 | /* END_CASE */ |