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 | */ |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 1727 | static int ssl_populate_session_tls12( mbedtls_ssl_session *session, |
| 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 | |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1808 | /* |
| 1809 | * Perform data exchanging between \p ssl_1 and \p ssl_2 and check if the |
| 1810 | * message was sent in the correct number of fragments. |
| 1811 | * |
| 1812 | * /p ssl_1 and /p ssl_2 Endpoints represented by mbedtls_ssl_context. Both |
| 1813 | * of them must be initialized and connected beforehand. |
| 1814 | * /p msg_len_1 and /p msg_len_2 specify the size of the message to send. |
| 1815 | * /p expected_fragments_1 and /p expected_fragments_2 determine in how many |
| 1816 | * fragments the message should be sent. |
| 1817 | * expected_fragments is 0: can be used for DTLS testing while the message |
| 1818 | * size is larger than MFL. In that case the message |
| 1819 | * cannot be fragmented and sent to the second endpoint. |
| 1820 | * This value can be used for negative tests. |
| 1821 | * expected_fragments is 1: can be used for TLS/DTLS testing while the |
| 1822 | * message size is below MFL |
| 1823 | * expected_fragments > 1: can be used for TLS testing while the message |
| 1824 | * size is larger than MFL |
| 1825 | * |
| 1826 | * \retval 0 on success, otherwise error code. |
| 1827 | */ |
| 1828 | int mbedtls_exchange_data( mbedtls_ssl_context *ssl_1, |
| 1829 | int msg_len_1, const int expected_fragments_1, |
| 1830 | mbedtls_ssl_context *ssl_2, |
| 1831 | int msg_len_2, const int expected_fragments_2 ) |
| 1832 | { |
| 1833 | unsigned char *msg_buf_1 = malloc( msg_len_1 ); |
| 1834 | unsigned char *msg_buf_2 = malloc( msg_len_2 ); |
| 1835 | unsigned char *in_buf_1 = malloc( msg_len_2 ); |
| 1836 | unsigned char *in_buf_2 = malloc( msg_len_1 ); |
| 1837 | int msg_type, ret = -1; |
| 1838 | |
| 1839 | /* Perform this test with two message types. At first use a message |
| 1840 | * consisting of only 0x00 for the client and only 0xFF for the server. |
| 1841 | * At the second time use message with generated data */ |
| 1842 | for( msg_type = 0; msg_type < 2; msg_type++ ) |
| 1843 | { |
| 1844 | int written_1 = 0; |
| 1845 | int written_2 = 0; |
| 1846 | int read_1 = 0; |
| 1847 | int read_2 = 0; |
| 1848 | int fragments_1 = 0; |
| 1849 | int fragments_2 = 0; |
| 1850 | |
| 1851 | if( msg_type == 0 ) |
| 1852 | { |
| 1853 | memset( msg_buf_1, 0x00, msg_len_1 ); |
| 1854 | memset( msg_buf_2, 0xff, msg_len_2 ); |
| 1855 | } |
| 1856 | else |
| 1857 | { |
| 1858 | int i, j = 0; |
| 1859 | for( i = 0; i < msg_len_1; i++ ) |
| 1860 | { |
| 1861 | msg_buf_1[i] = j++ & 0xFF; |
| 1862 | } |
| 1863 | for( i = 0; i < msg_len_2; i++ ) |
| 1864 | { |
| 1865 | msg_buf_2[i] = ( j -= 5 ) & 0xFF; |
| 1866 | } |
| 1867 | } |
| 1868 | |
| 1869 | while( read_1 < msg_len_2 || read_2 < msg_len_1 ) |
| 1870 | { |
| 1871 | /* ssl_1 sending */ |
| 1872 | if( msg_len_1 > written_1 ) |
| 1873 | { |
| 1874 | ret = mbedtls_ssl_write_fragment( ssl_1, msg_buf_1, |
| 1875 | msg_len_1, &written_1, |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1876 | expected_fragments_1 ); |
| 1877 | if( expected_fragments_1 == 0 ) |
| 1878 | { |
| 1879 | /* This error is expected when the message is too large and |
| 1880 | * cannot be fragmented */ |
| 1881 | TEST_ASSERT( ret == MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1882 | msg_len_1 = 0; |
| 1883 | } |
| 1884 | else |
| 1885 | { |
| 1886 | TEST_ASSERT( ret == 0 ); |
| 1887 | } |
| 1888 | } |
| 1889 | |
| 1890 | /* ssl_2 sending */ |
| 1891 | if( msg_len_2 > written_2 ) |
| 1892 | { |
| 1893 | ret = mbedtls_ssl_write_fragment( ssl_2, msg_buf_2, |
| 1894 | msg_len_2, &written_2, |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1895 | expected_fragments_2 ); |
| 1896 | if( expected_fragments_2 == 0 ) |
| 1897 | { |
| 1898 | /* This error is expected when the message is too large and |
| 1899 | * cannot be fragmented */ |
| 1900 | TEST_ASSERT( ret == MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1901 | msg_len_2 = 0; |
| 1902 | } |
| 1903 | else |
| 1904 | { |
| 1905 | TEST_ASSERT( ret == 0 ); |
| 1906 | } |
| 1907 | } |
| 1908 | |
| 1909 | /* ssl_1 reading */ |
| 1910 | if( read_1 < msg_len_2 ) |
| 1911 | { |
| 1912 | ret = mbedtls_ssl_read_fragment( ssl_1, in_buf_1, |
| 1913 | msg_len_2, &read_1, |
Piotr Nowicki | 438bf3b | 2020-03-10 12:59:10 +0100 | [diff] [blame] | 1914 | &fragments_2, |
| 1915 | expected_fragments_2 ); |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1916 | TEST_ASSERT( ret == 0 ); |
| 1917 | } |
| 1918 | |
| 1919 | /* ssl_2 reading */ |
| 1920 | if( read_2 < msg_len_1 ) |
| 1921 | { |
| 1922 | ret = mbedtls_ssl_read_fragment( ssl_2, in_buf_2, |
| 1923 | msg_len_1, &read_2, |
Piotr Nowicki | 438bf3b | 2020-03-10 12:59:10 +0100 | [diff] [blame] | 1924 | &fragments_1, |
| 1925 | expected_fragments_1 ); |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1926 | TEST_ASSERT( ret == 0 ); |
| 1927 | } |
| 1928 | } |
| 1929 | |
| 1930 | ret = -1; |
| 1931 | TEST_ASSERT( 0 == memcmp( msg_buf_1, in_buf_2, msg_len_1 ) ); |
| 1932 | TEST_ASSERT( 0 == memcmp( msg_buf_2, in_buf_1, msg_len_2 ) ); |
| 1933 | TEST_ASSERT( fragments_1 == expected_fragments_1 ); |
| 1934 | TEST_ASSERT( fragments_2 == expected_fragments_2 ); |
| 1935 | } |
| 1936 | |
| 1937 | ret = 0; |
| 1938 | |
| 1939 | exit: |
| 1940 | free( msg_buf_1 ); |
| 1941 | free( in_buf_1 ); |
| 1942 | free( msg_buf_2 ); |
| 1943 | free( in_buf_2 ); |
| 1944 | |
| 1945 | return ret; |
| 1946 | } |
| 1947 | |
Piotr Nowicki | 95e9eb8 | 2020-02-14 11:33:34 +0100 | [diff] [blame] | 1948 | /* |
| 1949 | * Perform data exchanging between \p ssl_1 and \p ssl_2. Both of endpoints |
| 1950 | * must be initialized and connected beforehand. |
| 1951 | * |
| 1952 | * \retval 0 on success, otherwise error code. |
| 1953 | */ |
| 1954 | int exchange_data( mbedtls_ssl_context *ssl_1, |
| 1955 | mbedtls_ssl_context *ssl_2 ) |
| 1956 | { |
| 1957 | return mbedtls_exchange_data( ssl_1, 256, 1, |
| 1958 | ssl_2, 256, 1 ); |
| 1959 | } |
| 1960 | |
Glenn Strauss | 39e624c | 2022-04-11 13:33:16 -0400 | [diff] [blame] | 1961 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 1962 | defined(MBEDTLS_ENTROPY_C) && \ |
| 1963 | defined(MBEDTLS_CTR_DRBG_C) |
| 1964 | static int check_ssl_version( mbedtls_ssl_protocol_version expected_negotiated_version, |
| 1965 | const mbedtls_ssl_context *ssl ) |
Gilles Peskine | 1255b0d | 2022-01-13 01:08:48 +0100 | [diff] [blame] | 1966 | { |
| 1967 | const char *version_string = mbedtls_ssl_get_version( ssl ); |
| 1968 | mbedtls_ssl_protocol_version version_number = |
| 1969 | mbedtls_ssl_get_version_number( ssl ); |
| 1970 | |
Glenn Strauss | e3af4cb | 2022-03-15 03:23:42 -0400 | [diff] [blame] | 1971 | TEST_EQUAL( ssl->tls_version, expected_negotiated_version ); |
Gilles Peskine | 1255b0d | 2022-01-13 01:08:48 +0100 | [diff] [blame] | 1972 | |
| 1973 | if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM ) |
| 1974 | { |
| 1975 | TEST_EQUAL( version_string[0], 'D' ); |
| 1976 | ++version_string; |
| 1977 | } |
| 1978 | |
| 1979 | switch( expected_negotiated_version ) |
| 1980 | { |
Glenn Strauss | e3af4cb | 2022-03-15 03:23:42 -0400 | [diff] [blame] | 1981 | case MBEDTLS_SSL_VERSION_TLS1_2: |
Glenn Strauss | dff8462 | 2022-03-14 11:12:57 -0400 | [diff] [blame] | 1982 | TEST_EQUAL( version_number, MBEDTLS_SSL_VERSION_TLS1_2 ); |
Gilles Peskine | 1255b0d | 2022-01-13 01:08:48 +0100 | [diff] [blame] | 1983 | TEST_ASSERT( strcmp( version_string, "TLSv1.2" ) == 0 ); |
| 1984 | break; |
| 1985 | |
Glenn Strauss | e3af4cb | 2022-03-15 03:23:42 -0400 | [diff] [blame] | 1986 | case MBEDTLS_SSL_VERSION_TLS1_3: |
Glenn Strauss | dff8462 | 2022-03-14 11:12:57 -0400 | [diff] [blame] | 1987 | TEST_EQUAL( version_number, MBEDTLS_SSL_VERSION_TLS1_3 ); |
Gilles Peskine | 1255b0d | 2022-01-13 01:08:48 +0100 | [diff] [blame] | 1988 | TEST_ASSERT( strcmp( version_string, "TLSv1.3" ) == 0 ); |
| 1989 | break; |
| 1990 | |
| 1991 | default: |
| 1992 | TEST_ASSERT( ! "Version check not implemented for this protocol version" ); |
| 1993 | } |
| 1994 | |
| 1995 | return( 1 ); |
| 1996 | |
| 1997 | exit: |
| 1998 | return( 0 ); |
| 1999 | } |
Glenn Strauss | 39e624c | 2022-04-11 13:33:16 -0400 | [diff] [blame] | 2000 | #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] | 2001 | |
| 2002 | |
Manuel Pégourié-Gonnard | d12402f | 2020-05-20 10:34:25 +0200 | [diff] [blame] | 2003 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 2004 | defined(MBEDTLS_ENTROPY_C) && \ |
| 2005 | defined(MBEDTLS_CTR_DRBG_C) |
Andrzej Kurek | 92d7417 | 2022-06-28 10:29:42 -0400 | [diff] [blame] | 2006 | void perform_handshake( handshake_test_options *options ) |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2007 | { |
| 2008 | /* forced_ciphersuite needs to last until the end of the handshake */ |
| 2009 | int forced_ciphersuite[2]; |
| 2010 | enum { BUFFSIZE = 17000 }; |
| 2011 | mbedtls_endpoint client, server; |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2012 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2013 | const char *psk_identity = "foo"; |
| 2014 | #endif |
| 2015 | #if defined(MBEDTLS_TIMING_C) |
| 2016 | mbedtls_timing_delay_context timer_client, timer_server; |
| 2017 | #endif |
| 2018 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
| 2019 | unsigned char *context_buf = NULL; |
| 2020 | size_t context_buf_len; |
| 2021 | #endif |
| 2022 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 2023 | int ret = -1; |
| 2024 | #endif |
Neil Armstrong | 8c52ed8 | 2022-05-27 13:14:55 +0200 | [diff] [blame] | 2025 | int expected_handshake_result = options->expected_handshake_result; |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2026 | |
Neil Armstrong | 46a1760 | 2022-02-23 15:11:16 +0100 | [diff] [blame] | 2027 | USE_PSA_INIT( ); |
| 2028 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2029 | mbedtls_test_message_queue server_queue, client_queue; |
| 2030 | mbedtls_test_message_socket_context server_context, client_context; |
Andrzej Kurek | 45916ba | 2020-03-05 14:46:22 -0500 | [diff] [blame] | 2031 | mbedtls_message_socket_init( &server_context ); |
| 2032 | mbedtls_message_socket_init( &client_context ); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2033 | |
| 2034 | /* Client side */ |
| 2035 | if( options->dtls != 0 ) |
| 2036 | { |
| 2037 | TEST_ASSERT( mbedtls_endpoint_init( &client, MBEDTLS_SSL_IS_CLIENT, |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 2038 | options, &client_context, |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2039 | &client_queue, |
Andrzej Kurek | 74394a5 | 2022-03-08 06:50:12 -0500 | [diff] [blame] | 2040 | &server_queue, NULL ) == 0 ); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2041 | #if defined(MBEDTLS_TIMING_C) |
| 2042 | mbedtls_ssl_set_timer_cb( &client.ssl, &timer_client, |
| 2043 | mbedtls_timing_set_delay, |
| 2044 | mbedtls_timing_get_delay ); |
| 2045 | #endif |
| 2046 | } |
| 2047 | else |
| 2048 | { |
| 2049 | TEST_ASSERT( mbedtls_endpoint_init( &client, MBEDTLS_SSL_IS_CLIENT, |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 2050 | options, NULL, NULL, |
Andrzej Kurek | 74394a5 | 2022-03-08 06:50:12 -0500 | [diff] [blame] | 2051 | NULL, NULL ) == 0 ); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2052 | } |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 2053 | |
Glenn Strauss | 39e624c | 2022-04-11 13:33:16 -0400 | [diff] [blame] | 2054 | if( options->client_min_version != MBEDTLS_SSL_VERSION_UNKNOWN ) |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 2055 | { |
Glenn Strauss | e3af4cb | 2022-03-15 03:23:42 -0400 | [diff] [blame] | 2056 | mbedtls_ssl_conf_min_tls_version( &client.conf, |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 2057 | options->client_min_version ); |
| 2058 | } |
| 2059 | |
Glenn Strauss | 39e624c | 2022-04-11 13:33:16 -0400 | [diff] [blame] | 2060 | if( options->client_max_version != MBEDTLS_SSL_VERSION_UNKNOWN ) |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 2061 | { |
Glenn Strauss | e3af4cb | 2022-03-15 03:23:42 -0400 | [diff] [blame] | 2062 | mbedtls_ssl_conf_max_tls_version( &client.conf, |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 2063 | options->client_max_version ); |
| 2064 | } |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2065 | |
| 2066 | if( strlen( options->cipher ) > 0 ) |
| 2067 | { |
| 2068 | set_ciphersuite( &client.conf, options->cipher, forced_ciphersuite ); |
| 2069 | } |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 2070 | |
| 2071 | #if defined (MBEDTLS_DEBUG_C) |
| 2072 | if( options->cli_log_fun ) |
| 2073 | { |
| 2074 | mbedtls_debug_set_threshold( 4 ); |
| 2075 | mbedtls_ssl_conf_dbg( &client.conf, options->cli_log_fun, |
| 2076 | options->cli_log_obj ); |
| 2077 | } |
| 2078 | #endif |
| 2079 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2080 | /* Server side */ |
| 2081 | if( options->dtls != 0 ) |
| 2082 | { |
| 2083 | TEST_ASSERT( mbedtls_endpoint_init( &server, MBEDTLS_SSL_IS_SERVER, |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 2084 | options, &server_context, |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2085 | &server_queue, |
Andrzej Kurek | 74394a5 | 2022-03-08 06:50:12 -0500 | [diff] [blame] | 2086 | &client_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( &server.ssl, &timer_server, |
| 2089 | mbedtls_timing_set_delay, |
| 2090 | mbedtls_timing_get_delay ); |
| 2091 | #endif |
| 2092 | } |
| 2093 | else |
| 2094 | { |
| 2095 | TEST_ASSERT( mbedtls_endpoint_init( &server, MBEDTLS_SSL_IS_SERVER, |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 2096 | options, NULL, NULL, NULL, |
| 2097 | NULL ) == 0 ); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2098 | } |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 2099 | |
| 2100 | mbedtls_ssl_conf_authmode( &server.conf, options->srv_auth_mode ); |
| 2101 | |
Glenn Strauss | 39e624c | 2022-04-11 13:33:16 -0400 | [diff] [blame] | 2102 | if( options->server_min_version != MBEDTLS_SSL_VERSION_UNKNOWN ) |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 2103 | { |
Glenn Strauss | e3af4cb | 2022-03-15 03:23:42 -0400 | [diff] [blame] | 2104 | mbedtls_ssl_conf_min_tls_version( &server.conf, |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 2105 | options->server_min_version ); |
| 2106 | } |
| 2107 | |
Glenn Strauss | 39e624c | 2022-04-11 13:33:16 -0400 | [diff] [blame] | 2108 | if( options->server_max_version != MBEDTLS_SSL_VERSION_UNKNOWN ) |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 2109 | { |
Glenn Strauss | e3af4cb | 2022-03-15 03:23:42 -0400 | [diff] [blame] | 2110 | mbedtls_ssl_conf_max_tls_version( &server.conf, |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 2111 | options->server_max_version ); |
| 2112 | } |
| 2113 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2114 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 2115 | TEST_ASSERT( mbedtls_ssl_conf_max_frag_len( &(server.conf), |
| 2116 | (unsigned char) options->mfl ) == 0 ); |
| 2117 | TEST_ASSERT( mbedtls_ssl_conf_max_frag_len( &(client.conf), |
| 2118 | (unsigned char) options->mfl ) == 0 ); |
| 2119 | #else |
| 2120 | TEST_ASSERT( MBEDTLS_SSL_MAX_FRAG_LEN_NONE == options->mfl ); |
| 2121 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 2122 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 2123 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2124 | if( options->psk_str != NULL && options->psk_str->len > 0 ) |
| 2125 | { |
| 2126 | TEST_ASSERT( mbedtls_ssl_conf_psk( &client.conf, options->psk_str->x, |
| 2127 | options->psk_str->len, |
| 2128 | (const unsigned char *) psk_identity, |
| 2129 | strlen( psk_identity ) ) == 0 ); |
| 2130 | |
| 2131 | TEST_ASSERT( mbedtls_ssl_conf_psk( &server.conf, options->psk_str->x, |
| 2132 | options->psk_str->len, |
| 2133 | (const unsigned char *) psk_identity, |
| 2134 | strlen( psk_identity ) ) == 0 ); |
| 2135 | |
| 2136 | mbedtls_ssl_conf_psk_cb( &server.conf, psk_dummy_callback, NULL ); |
| 2137 | } |
| 2138 | #endif |
| 2139 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 2140 | if( options->renegotiate ) |
| 2141 | { |
| 2142 | mbedtls_ssl_conf_renegotiation( &(server.conf), |
| 2143 | MBEDTLS_SSL_RENEGOTIATION_ENABLED ); |
| 2144 | mbedtls_ssl_conf_renegotiation( &(client.conf), |
| 2145 | MBEDTLS_SSL_RENEGOTIATION_ENABLED ); |
| 2146 | |
| 2147 | mbedtls_ssl_conf_legacy_renegotiation( &(server.conf), |
| 2148 | options->legacy_renegotiation ); |
| 2149 | mbedtls_ssl_conf_legacy_renegotiation( &(client.conf), |
| 2150 | options->legacy_renegotiation ); |
| 2151 | } |
| 2152 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
| 2153 | |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 2154 | #if defined (MBEDTLS_DEBUG_C) |
| 2155 | if( options->srv_log_fun ) |
| 2156 | { |
| 2157 | mbedtls_debug_set_threshold( 4 ); |
| 2158 | mbedtls_ssl_conf_dbg( &server.conf, options->srv_log_fun, |
| 2159 | options->srv_log_obj ); |
| 2160 | } |
| 2161 | #endif |
| 2162 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2163 | TEST_ASSERT( mbedtls_mock_socket_connect( &(client.socket), |
| 2164 | &(server.socket), |
| 2165 | BUFFSIZE ) == 0 ); |
| 2166 | |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2167 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 2168 | if( options->resize_buffers != 0 ) |
| 2169 | { |
| 2170 | /* Ensure that the buffer sizes are appropriate before resizes */ |
| 2171 | TEST_ASSERT( client.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN ); |
| 2172 | TEST_ASSERT( client.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN ); |
| 2173 | TEST_ASSERT( server.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN ); |
| 2174 | TEST_ASSERT( server.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN ); |
| 2175 | } |
| 2176 | #endif |
| 2177 | |
Glenn Strauss | 39e624c | 2022-04-11 13:33:16 -0400 | [diff] [blame] | 2178 | if( options->expected_negotiated_version == MBEDTLS_SSL_VERSION_UNKNOWN ) |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 2179 | { |
Hanno Becker | bc00044 | 2021-06-24 09:18:19 +0100 | [diff] [blame] | 2180 | expected_handshake_result = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION; |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 2181 | } |
| 2182 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2183 | TEST_ASSERT( mbedtls_move_handshake_to_state( &(client.ssl), |
| 2184 | &(server.ssl), |
| 2185 | MBEDTLS_SSL_HANDSHAKE_OVER ) |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 2186 | == expected_handshake_result ); |
| 2187 | |
| 2188 | if( expected_handshake_result != 0 ) |
| 2189 | { |
| 2190 | /* Connection will have failed by this point, skip to cleanup */ |
| 2191 | goto exit; |
| 2192 | } |
| 2193 | |
Paul Elliott | 27b0d94 | 2022-03-18 21:55:32 +0000 | [diff] [blame] | 2194 | TEST_ASSERT( mbedtls_ssl_is_handshake_over( &client.ssl ) == 1 ); |
Jerry Yu | df0a71a | 2022-05-26 10:43:30 +0800 | [diff] [blame] | 2195 | |
Jerry Yu | 66adf31 | 2022-05-31 15:23:29 +0800 | [diff] [blame] | 2196 | /* Make sure server state is moved to HANDSHAKE_OVER also. */ |
Ronald Cron | 585cd70 | 2022-06-10 15:02:05 +0200 | [diff] [blame] | 2197 | TEST_EQUAL( mbedtls_move_handshake_to_state( &(server.ssl), |
| 2198 | &(client.ssl), |
| 2199 | MBEDTLS_SSL_HANDSHAKE_OVER ), 0 ); |
Jerry Yu | df0a71a | 2022-05-26 10:43:30 +0800 | [diff] [blame] | 2200 | |
Paul Elliott | 27b0d94 | 2022-03-18 21:55:32 +0000 | [diff] [blame] | 2201 | TEST_ASSERT( mbedtls_ssl_is_handshake_over( &server.ssl ) == 1 ); |
Gilles Peskine | 1255b0d | 2022-01-13 01:08:48 +0100 | [diff] [blame] | 2202 | /* Check that both sides have negotiated the expected version. */ |
| 2203 | mbedtls_test_set_step( 0 ); |
| 2204 | if( ! check_ssl_version( options->expected_negotiated_version, |
| 2205 | &client.ssl ) ) |
| 2206 | goto exit; |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 2207 | |
Gilles Peskine | 1255b0d | 2022-01-13 01:08:48 +0100 | [diff] [blame] | 2208 | mbedtls_test_set_step( 1 ); |
| 2209 | if( ! check_ssl_version( options->expected_negotiated_version, |
| 2210 | &server.ssl ) ) |
| 2211 | goto exit; |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 2212 | |
Neil Armstrong | 8c52ed8 | 2022-05-27 13:14:55 +0200 | [diff] [blame] | 2213 | if( options->expected_ciphersuite != 0 ) |
| 2214 | { |
| 2215 | TEST_EQUAL( server.ssl.session->ciphersuite, |
| 2216 | options->expected_ciphersuite ); |
| 2217 | } |
| 2218 | |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2219 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 2220 | if( options->resize_buffers != 0 ) |
| 2221 | { |
TRodziewicz | 2abf03c | 2021-06-25 14:40:09 +0200 | [diff] [blame] | 2222 | /* A server, when using DTLS, might delay a buffer resize to happen |
| 2223 | * after it receives a message, so we force it. */ |
| 2224 | TEST_ASSERT( exchange_data( &(client.ssl), &(server.ssl) ) == 0 ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2225 | |
TRodziewicz | 2abf03c | 2021-06-25 14:40:09 +0200 | [diff] [blame] | 2226 | TEST_ASSERT( client.ssl.out_buf_len == |
| 2227 | mbedtls_ssl_get_output_buflen( &client.ssl ) ); |
| 2228 | TEST_ASSERT( client.ssl.in_buf_len == |
| 2229 | mbedtls_ssl_get_input_buflen( &client.ssl ) ); |
| 2230 | TEST_ASSERT( server.ssl.out_buf_len == |
| 2231 | mbedtls_ssl_get_output_buflen( &server.ssl ) ); |
| 2232 | TEST_ASSERT( server.ssl.in_buf_len == |
| 2233 | mbedtls_ssl_get_input_buflen( &server.ssl ) ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2234 | } |
| 2235 | #endif |
| 2236 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2237 | if( options->cli_msg_len != 0 || options->srv_msg_len != 0 ) |
| 2238 | { |
| 2239 | /* Start data exchanging test */ |
| 2240 | TEST_ASSERT( mbedtls_exchange_data( &(client.ssl), options->cli_msg_len, |
| 2241 | options->expected_cli_fragments, |
| 2242 | &(server.ssl), options->srv_msg_len, |
| 2243 | options->expected_srv_fragments ) |
| 2244 | == 0 ); |
| 2245 | } |
| 2246 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
| 2247 | if( options->serialize == 1 ) |
| 2248 | { |
| 2249 | TEST_ASSERT( options->dtls == 1 ); |
| 2250 | |
| 2251 | TEST_ASSERT( mbedtls_ssl_context_save( &(server.ssl), NULL, |
| 2252 | 0, &context_buf_len ) |
| 2253 | == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 2254 | |
| 2255 | context_buf = mbedtls_calloc( 1, context_buf_len ); |
| 2256 | TEST_ASSERT( context_buf != NULL ); |
| 2257 | |
| 2258 | TEST_ASSERT( mbedtls_ssl_context_save( &(server.ssl), context_buf, |
| 2259 | context_buf_len, |
| 2260 | &context_buf_len ) == 0 ); |
| 2261 | |
| 2262 | mbedtls_ssl_free( &(server.ssl) ); |
| 2263 | mbedtls_ssl_init( &(server.ssl) ); |
| 2264 | |
| 2265 | TEST_ASSERT( mbedtls_ssl_setup( &(server.ssl), &(server.conf) ) == 0 ); |
| 2266 | |
| 2267 | mbedtls_ssl_set_bio( &( server.ssl ), &server_context, |
| 2268 | mbedtls_mock_tcp_send_msg, |
| 2269 | mbedtls_mock_tcp_recv_msg, |
| 2270 | NULL ); |
| 2271 | |
Gilles Peskine | 49d7ddf | 2022-01-27 23:25:51 +0100 | [diff] [blame] | 2272 | mbedtls_ssl_set_user_data_p( &server.ssl, &server ); |
| 2273 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2274 | #if defined(MBEDTLS_TIMING_C) |
| 2275 | mbedtls_ssl_set_timer_cb( &server.ssl, &timer_server, |
| 2276 | mbedtls_timing_set_delay, |
| 2277 | mbedtls_timing_get_delay ); |
| 2278 | #endif |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2279 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 2280 | if( options->resize_buffers != 0 ) |
| 2281 | { |
| 2282 | /* Ensure that the buffer sizes are appropriate before resizes */ |
| 2283 | TEST_ASSERT( server.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN ); |
| 2284 | TEST_ASSERT( server.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN ); |
| 2285 | } |
| 2286 | #endif |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2287 | TEST_ASSERT( mbedtls_ssl_context_load( &( server.ssl ), context_buf, |
| 2288 | context_buf_len ) == 0 ); |
| 2289 | |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2290 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 2291 | /* Validate buffer sizes after context deserialization */ |
| 2292 | if( options->resize_buffers != 0 ) |
| 2293 | { |
| 2294 | TEST_ASSERT( server.ssl.out_buf_len == |
| 2295 | mbedtls_ssl_get_output_buflen( &server.ssl ) ); |
| 2296 | TEST_ASSERT( server.ssl.in_buf_len == |
| 2297 | mbedtls_ssl_get_input_buflen( &server.ssl ) ); |
| 2298 | } |
| 2299 | #endif |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2300 | /* Retest writing/reading */ |
| 2301 | if( options->cli_msg_len != 0 || options->srv_msg_len != 0 ) |
| 2302 | { |
| 2303 | TEST_ASSERT( mbedtls_exchange_data( &(client.ssl), |
| 2304 | options->cli_msg_len, |
| 2305 | options->expected_cli_fragments, |
| 2306 | &(server.ssl), |
| 2307 | options->srv_msg_len, |
| 2308 | options->expected_srv_fragments ) |
| 2309 | == 0 ); |
| 2310 | } |
| 2311 | } |
| 2312 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2313 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2314 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 2315 | if( options->renegotiate ) |
| 2316 | { |
| 2317 | /* Start test with renegotiation */ |
| 2318 | TEST_ASSERT( server.ssl.renego_status == |
| 2319 | MBEDTLS_SSL_INITIAL_HANDSHAKE ); |
| 2320 | TEST_ASSERT( client.ssl.renego_status == |
| 2321 | MBEDTLS_SSL_INITIAL_HANDSHAKE ); |
| 2322 | |
| 2323 | /* After calling this function for the server, it only sends a handshake |
| 2324 | * request. All renegotiation should happen during data exchanging */ |
| 2325 | TEST_ASSERT( mbedtls_ssl_renegotiate( &(server.ssl) ) == 0 ); |
| 2326 | TEST_ASSERT( server.ssl.renego_status == |
| 2327 | MBEDTLS_SSL_RENEGOTIATION_PENDING ); |
| 2328 | TEST_ASSERT( client.ssl.renego_status == |
| 2329 | MBEDTLS_SSL_INITIAL_HANDSHAKE ); |
| 2330 | |
| 2331 | TEST_ASSERT( exchange_data( &(client.ssl), &(server.ssl) ) == 0 ); |
| 2332 | TEST_ASSERT( server.ssl.renego_status == |
| 2333 | MBEDTLS_SSL_RENEGOTIATION_DONE ); |
| 2334 | TEST_ASSERT( client.ssl.renego_status == |
| 2335 | MBEDTLS_SSL_RENEGOTIATION_DONE ); |
| 2336 | |
| 2337 | /* After calling mbedtls_ssl_renegotiate for the client all renegotiation |
| 2338 | * should happen inside this function. However in this test, we cannot |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 2339 | * perform simultaneous communication between client and server so this |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2340 | * function will return waiting error on the socket. All rest of |
| 2341 | * renegotiation should happen during data exchanging */ |
| 2342 | ret = mbedtls_ssl_renegotiate( &(client.ssl) ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2343 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 2344 | if( options->resize_buffers != 0 ) |
| 2345 | { |
| 2346 | /* Ensure that the buffer sizes are appropriate before resizes */ |
| 2347 | TEST_ASSERT( client.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN ); |
| 2348 | TEST_ASSERT( client.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN ); |
| 2349 | } |
| 2350 | #endif |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2351 | TEST_ASSERT( ret == 0 || |
| 2352 | ret == MBEDTLS_ERR_SSL_WANT_READ || |
| 2353 | ret == MBEDTLS_ERR_SSL_WANT_WRITE ); |
| 2354 | TEST_ASSERT( server.ssl.renego_status == |
| 2355 | MBEDTLS_SSL_RENEGOTIATION_DONE ); |
| 2356 | TEST_ASSERT( client.ssl.renego_status == |
| 2357 | MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ); |
| 2358 | |
| 2359 | TEST_ASSERT( exchange_data( &(client.ssl), &(server.ssl) ) == 0 ); |
| 2360 | TEST_ASSERT( server.ssl.renego_status == |
| 2361 | MBEDTLS_SSL_RENEGOTIATION_DONE ); |
| 2362 | TEST_ASSERT( client.ssl.renego_status == |
| 2363 | MBEDTLS_SSL_RENEGOTIATION_DONE ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2364 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 2365 | /* Validate buffer sizes after renegotiation */ |
| 2366 | if( options->resize_buffers != 0 ) |
| 2367 | { |
| 2368 | TEST_ASSERT( client.ssl.out_buf_len == |
| 2369 | mbedtls_ssl_get_output_buflen( &client.ssl ) ); |
| 2370 | TEST_ASSERT( client.ssl.in_buf_len == |
| 2371 | mbedtls_ssl_get_input_buflen( &client.ssl ) ); |
| 2372 | TEST_ASSERT( server.ssl.out_buf_len == |
| 2373 | mbedtls_ssl_get_output_buflen( &server.ssl ) ); |
| 2374 | TEST_ASSERT( server.ssl.in_buf_len == |
| 2375 | mbedtls_ssl_get_input_buflen( &server.ssl ) ); |
| 2376 | } |
| 2377 | #endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */ |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2378 | } |
| 2379 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
| 2380 | |
Gilles Peskine | 80dae04 | 2022-01-21 23:50:39 +0100 | [diff] [blame] | 2381 | TEST_ASSERT( mbedtls_ssl_conf_get_user_data_p( &client.conf ) == &client ); |
| 2382 | TEST_ASSERT( mbedtls_ssl_get_user_data_p( &client.ssl ) == &client ); |
| 2383 | TEST_ASSERT( mbedtls_ssl_conf_get_user_data_p( &server.conf ) == &server ); |
| 2384 | TEST_ASSERT( mbedtls_ssl_get_user_data_p( &server.ssl ) == &server ); |
| 2385 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2386 | exit: |
| 2387 | mbedtls_endpoint_free( &client, options->dtls != 0 ? &client_context : NULL ); |
| 2388 | mbedtls_endpoint_free( &server, options->dtls != 0 ? &server_context : NULL ); |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 2389 | #if defined (MBEDTLS_DEBUG_C) |
| 2390 | if( options->cli_log_fun || options->srv_log_fun ) |
| 2391 | { |
| 2392 | mbedtls_debug_set_threshold( 0 ); |
| 2393 | } |
| 2394 | #endif |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2395 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
| 2396 | if( context_buf != NULL ) |
| 2397 | mbedtls_free( context_buf ); |
| 2398 | #endif |
Neil Armstrong | 46a1760 | 2022-02-23 15:11:16 +0100 | [diff] [blame] | 2399 | USE_PSA_DONE( ); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2400 | } |
Manuel Pégourié-Gonnard | d12402f | 2020-05-20 10:34:25 +0200 | [diff] [blame] | 2401 | #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] | 2402 | |
Ronald Cron | e7b9b6b | 2022-06-10 17:24:31 +0200 | [diff] [blame] | 2403 | #if defined(MBEDTLS_TEST_HOOKS) |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 2404 | /* |
| 2405 | * Tweak vector lengths in a TLS 1.3 Certificate message |
| 2406 | * |
Ronald Cron | cf600bc | 2022-06-17 15:54:16 +0200 | [diff] [blame] | 2407 | * \param[in] buf Buffer containing the Certificate message to tweak |
| 2408 | * \param[in]]out] end End of the buffer to parse |
| 2409 | * \param tweak Tweak identifier (from 1 to the number of tweaks). |
| 2410 | * \param[out] expected_result Error code expected from the parsing function |
| 2411 | * \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] | 2412 | * is expected to fail. All zeroes if no |
| 2413 | * MBEDTLS_SSL_CHK_BUF_READ_PTR failure is expected. |
| 2414 | */ |
| 2415 | int tweak_tls13_certificate_msg_vector_len( |
Ronald Cron | e7b9b6b | 2022-06-10 17:24:31 +0200 | [diff] [blame] | 2416 | unsigned char *buf, unsigned char **end, int tweak, |
| 2417 | int *expected_result, mbedtls_ssl_chk_buf_ptr_args *args ) |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 2418 | { |
| 2419 | /* |
| 2420 | * The definition of the tweaks assume that the certificate list contains only |
| 2421 | * one certificate. |
| 2422 | */ |
| 2423 | |
| 2424 | /* |
| 2425 | * struct { |
| 2426 | * opaque cert_data<1..2^24-1>; |
| 2427 | * Extension extensions<0..2^16-1>; |
| 2428 | * } CertificateEntry; |
| 2429 | * |
| 2430 | * struct { |
| 2431 | * opaque certificate_request_context<0..2^8-1>; |
| 2432 | * CertificateEntry certificate_list<0..2^24-1>; |
| 2433 | * } Certificate; |
| 2434 | */ |
| 2435 | unsigned char *p_certificate_request_context_len = buf; |
| 2436 | size_t certificate_request_context_len = buf[0]; |
| 2437 | |
| 2438 | unsigned char *p_certificate_list_len = buf + 1 + certificate_request_context_len; |
| 2439 | unsigned char *certificate_list = p_certificate_list_len + 3; |
| 2440 | size_t certificate_list_len = MBEDTLS_GET_UINT24_BE( p_certificate_list_len, 0 ); |
| 2441 | |
| 2442 | unsigned char *p_cert_data_len = certificate_list; |
| 2443 | unsigned char *cert_data = p_cert_data_len + 3; |
| 2444 | size_t cert_data_len = MBEDTLS_GET_UINT24_BE( p_cert_data_len, 0 ); |
| 2445 | |
| 2446 | unsigned char *p_extensions_len = cert_data + cert_data_len; |
| 2447 | unsigned char *extensions = p_extensions_len + 2; |
| 2448 | size_t extensions_len = MBEDTLS_GET_UINT16_BE( p_extensions_len, 0 ); |
| 2449 | |
| 2450 | *expected_result = MBEDTLS_ERR_SSL_DECODE_ERROR; |
| 2451 | |
| 2452 | switch( tweak ) |
| 2453 | { |
| 2454 | case 1: |
| 2455 | /* Failure when checking if the certificate request context length and |
| 2456 | * certificate list length can be read |
| 2457 | */ |
| 2458 | *end = buf + 3; |
Ronald Cron | e7b9b6b | 2022-06-10 17:24:31 +0200 | [diff] [blame] | 2459 | set_chk_buf_ptr_args( args, buf, *end, 4 ); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 2460 | break; |
| 2461 | |
| 2462 | case 2: |
| 2463 | /* Invalid certificate request context length. |
| 2464 | */ |
| 2465 | *p_certificate_request_context_len = |
| 2466 | certificate_request_context_len + 1; |
Ronald Cron | e7b9b6b | 2022-06-10 17:24:31 +0200 | [diff] [blame] | 2467 | reset_chk_buf_ptr_args( args ); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 2468 | break; |
| 2469 | |
| 2470 | case 3: |
| 2471 | /* Failure when checking if certificate_list data can be read. */ |
| 2472 | MBEDTLS_PUT_UINT24_BE( certificate_list_len + 1, |
| 2473 | p_certificate_list_len, 0 ); |
Ronald Cron | e7b9b6b | 2022-06-10 17:24:31 +0200 | [diff] [blame] | 2474 | set_chk_buf_ptr_args( args, certificate_list, *end, |
| 2475 | certificate_list_len + 1 ); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 2476 | break; |
| 2477 | |
| 2478 | case 4: |
| 2479 | /* Failure when checking if the cert_data length can be read. */ |
| 2480 | MBEDTLS_PUT_UINT24_BE( 2, p_certificate_list_len, 0 ); |
Ronald Cron | e7b9b6b | 2022-06-10 17:24:31 +0200 | [diff] [blame] | 2481 | 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] | 2482 | break; |
| 2483 | |
| 2484 | case 5: |
| 2485 | /* Failure when checking if cert_data data can be read. */ |
| 2486 | MBEDTLS_PUT_UINT24_BE( certificate_list_len - 3 + 1, |
| 2487 | p_cert_data_len, 0 ); |
Ronald Cron | e7b9b6b | 2022-06-10 17:24:31 +0200 | [diff] [blame] | 2488 | set_chk_buf_ptr_args( args, cert_data, |
| 2489 | certificate_list + certificate_list_len, |
| 2490 | certificate_list_len - 3 + 1 ); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 2491 | break; |
| 2492 | |
| 2493 | case 6: |
| 2494 | /* Failure when checking if the extensions length can be read. */ |
| 2495 | MBEDTLS_PUT_UINT24_BE( certificate_list_len - extensions_len - 1, |
| 2496 | p_certificate_list_len, 0 ); |
Ronald Cron | e7b9b6b | 2022-06-10 17:24:31 +0200 | [diff] [blame] | 2497 | set_chk_buf_ptr_args( args, p_extensions_len, |
| 2498 | certificate_list + certificate_list_len - extensions_len - 1, 2 ); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 2499 | break; |
| 2500 | |
| 2501 | case 7: |
| 2502 | /* Failure when checking if extensions data can be read. */ |
| 2503 | MBEDTLS_PUT_UINT16_BE( extensions_len + 1, p_extensions_len, 0 ); |
Ronald Cron | e7b9b6b | 2022-06-10 17:24:31 +0200 | [diff] [blame] | 2504 | |
| 2505 | set_chk_buf_ptr_args( args, extensions, |
| 2506 | certificate_list + certificate_list_len, extensions_len + 1 ); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 2507 | break; |
| 2508 | |
| 2509 | default: |
| 2510 | return( -1 ); |
| 2511 | } |
| 2512 | |
| 2513 | return( 0 ); |
| 2514 | } |
Ronald Cron | e7b9b6b | 2022-06-10 17:24:31 +0200 | [diff] [blame] | 2515 | #endif /* MBEDTLS_TEST_HOOKS */ |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 2516 | /* END_HEADER */ |
| 2517 | |
| 2518 | /* BEGIN_DEPENDENCIES |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2519 | * depends_on:MBEDTLS_SSL_TLS_C |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 2520 | * END_DEPENDENCIES |
| 2521 | */ |
| 2522 | |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2523 | /* BEGIN_CASE */ |
| 2524 | void test_callback_buffer_sanity() |
| 2525 | { |
| 2526 | enum { MSGLEN = 10 }; |
| 2527 | mbedtls_test_buffer buf; |
| 2528 | unsigned char input[MSGLEN]; |
| 2529 | unsigned char output[MSGLEN]; |
| 2530 | |
| 2531 | memset( input, 0, sizeof(input) ); |
| 2532 | |
| 2533 | /* Make sure calling put and get on NULL buffer results in error. */ |
| 2534 | TEST_ASSERT( mbedtls_test_buffer_put( NULL, input, sizeof( input ) ) |
| 2535 | == -1 ); |
| 2536 | TEST_ASSERT( mbedtls_test_buffer_get( NULL, output, sizeof( output ) ) |
| 2537 | == -1 ); |
| 2538 | TEST_ASSERT( mbedtls_test_buffer_put( NULL, NULL, sizeof( input ) ) == -1 ); |
Andrzej Kurek | f777414 | 2020-01-22 06:34:59 -0500 | [diff] [blame] | 2539 | |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2540 | TEST_ASSERT( mbedtls_test_buffer_put( NULL, NULL, 0 ) == -1 ); |
| 2541 | TEST_ASSERT( mbedtls_test_buffer_get( NULL, NULL, 0 ) == -1 ); |
| 2542 | |
| 2543 | /* 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] | 2544 | * in error. */ |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2545 | mbedtls_test_buffer_init( &buf ); |
| 2546 | |
| 2547 | TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, sizeof( input ) ) == -1 ); |
| 2548 | TEST_ASSERT( mbedtls_test_buffer_get( &buf, output, sizeof( output ) ) |
| 2549 | == -1 ); |
| 2550 | TEST_ASSERT( mbedtls_test_buffer_put( &buf, NULL, sizeof( input ) ) == -1 ); |
Andrzej Kurek | f777414 | 2020-01-22 06:34:59 -0500 | [diff] [blame] | 2551 | |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2552 | TEST_ASSERT( mbedtls_test_buffer_put( &buf, NULL, 0 ) == -1 ); |
| 2553 | TEST_ASSERT( mbedtls_test_buffer_get( &buf, NULL, 0 ) == -1 ); |
| 2554 | |
Andrzej Kurek | f777414 | 2020-01-22 06:34:59 -0500 | [diff] [blame] | 2555 | /* Make sure calling put and get on NULL input only results in |
| 2556 | * error if the length is not zero, and that a NULL output is valid for data |
| 2557 | * dropping. |
| 2558 | */ |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2559 | |
| 2560 | TEST_ASSERT( mbedtls_test_buffer_setup( &buf, sizeof( input ) ) == 0 ); |
| 2561 | |
| 2562 | TEST_ASSERT( mbedtls_test_buffer_put( &buf, NULL, sizeof( input ) ) == -1 ); |
| 2563 | TEST_ASSERT( mbedtls_test_buffer_get( &buf, NULL, sizeof( output ) ) |
Andrzej Kurek | f777414 | 2020-01-22 06:34:59 -0500 | [diff] [blame] | 2564 | == 0 ); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2565 | TEST_ASSERT( mbedtls_test_buffer_put( &buf, NULL, 0 ) == 0 ); |
| 2566 | TEST_ASSERT( mbedtls_test_buffer_get( &buf, NULL, 0 ) == 0 ); |
| 2567 | |
Piotr Nowicki | fb437d7 | 2020-01-13 16:59:12 +0100 | [diff] [blame] | 2568 | /* Make sure calling put several times in the row is safe */ |
| 2569 | |
| 2570 | TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, sizeof( input ) ) |
| 2571 | == sizeof( input ) ); |
| 2572 | TEST_ASSERT( mbedtls_test_buffer_get( &buf, output, 2 ) == 2 ); |
| 2573 | TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, 1 ) == 1 ); |
| 2574 | TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, 2 ) == 1 ); |
| 2575 | TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, 2 ) == 0 ); |
| 2576 | |
| 2577 | |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2578 | exit: |
| 2579 | |
| 2580 | mbedtls_test_buffer_free( &buf ); |
| 2581 | } |
| 2582 | /* END_CASE */ |
| 2583 | |
| 2584 | /* |
| 2585 | * Test if the implementation of `mbedtls_test_buffer` related functions is |
| 2586 | * correct and works as expected. |
| 2587 | * |
| 2588 | * That is |
| 2589 | * - If we try to put in \p put1 bytes then we can put in \p put1_ret bytes. |
| 2590 | * - Afterwards if we try to get \p get1 bytes then we can get \get1_ret bytes. |
| 2591 | * - Next, if we try to put in \p put1 bytes then we can put in \p put1_ret |
| 2592 | * bytes. |
| 2593 | * - Afterwards if we try to get \p get1 bytes then we can get \get1_ret bytes. |
| 2594 | * - All of the bytes we got match the bytes we put in in a FIFO manner. |
| 2595 | */ |
| 2596 | |
| 2597 | /* BEGIN_CASE */ |
| 2598 | void test_callback_buffer( int size, int put1, int put1_ret, |
| 2599 | int get1, int get1_ret, int put2, int put2_ret, |
| 2600 | int get2, int get2_ret ) |
| 2601 | { |
| 2602 | enum { ROUNDS = 2 }; |
| 2603 | size_t put[ROUNDS]; |
| 2604 | int put_ret[ROUNDS]; |
| 2605 | size_t get[ROUNDS]; |
| 2606 | int get_ret[ROUNDS]; |
| 2607 | mbedtls_test_buffer buf; |
| 2608 | unsigned char* input = NULL; |
| 2609 | size_t input_len; |
| 2610 | unsigned char* output = NULL; |
| 2611 | size_t output_len; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2612 | size_t i, j, written, read; |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2613 | |
| 2614 | mbedtls_test_buffer_init( &buf ); |
| 2615 | TEST_ASSERT( mbedtls_test_buffer_setup( &buf, size ) == 0 ); |
| 2616 | |
| 2617 | /* Check the sanity of input parameters and initialise local variables. That |
| 2618 | * is, ensure that the amount of data is not negative and that we are not |
| 2619 | * expecting more to put or get than we actually asked for. */ |
| 2620 | TEST_ASSERT( put1 >= 0 ); |
| 2621 | put[0] = put1; |
| 2622 | put_ret[0] = put1_ret; |
| 2623 | TEST_ASSERT( put1_ret <= put1 ); |
| 2624 | TEST_ASSERT( put2 >= 0 ); |
| 2625 | put[1] = put2; |
| 2626 | put_ret[1] = put2_ret; |
| 2627 | TEST_ASSERT( put2_ret <= put2 ); |
| 2628 | |
| 2629 | TEST_ASSERT( get1 >= 0 ); |
| 2630 | get[0] = get1; |
| 2631 | get_ret[0] = get1_ret; |
| 2632 | TEST_ASSERT( get1_ret <= get1 ); |
| 2633 | TEST_ASSERT( get2 >= 0 ); |
| 2634 | get[1] = get2; |
| 2635 | get_ret[1] = get2_ret; |
| 2636 | TEST_ASSERT( get2_ret <= get2 ); |
| 2637 | |
| 2638 | input_len = 0; |
| 2639 | /* Calculate actual input and output lengths */ |
| 2640 | for( j = 0; j < ROUNDS; j++ ) |
| 2641 | { |
| 2642 | if( put_ret[j] > 0 ) |
| 2643 | { |
| 2644 | input_len += put_ret[j]; |
| 2645 | } |
| 2646 | } |
| 2647 | /* In order to always have a valid pointer we always allocate at least 1 |
| 2648 | * byte. */ |
| 2649 | if( input_len == 0 ) |
| 2650 | input_len = 1; |
| 2651 | ASSERT_ALLOC( input, input_len ); |
| 2652 | |
| 2653 | output_len = 0; |
| 2654 | for( j = 0; j < ROUNDS; j++ ) |
| 2655 | { |
| 2656 | if( get_ret[j] > 0 ) |
| 2657 | { |
| 2658 | output_len += get_ret[j]; |
| 2659 | } |
| 2660 | } |
| 2661 | TEST_ASSERT( output_len <= input_len ); |
| 2662 | /* In order to always have a valid pointer we always allocate at least 1 |
| 2663 | * byte. */ |
| 2664 | if( output_len == 0 ) |
| 2665 | output_len = 1; |
| 2666 | ASSERT_ALLOC( output, output_len ); |
| 2667 | |
| 2668 | /* Fill up the buffer with structured data so that unwanted changes |
| 2669 | * can be detected */ |
| 2670 | for( i = 0; i < input_len; i++ ) |
| 2671 | { |
| 2672 | input[i] = i & 0xFF; |
| 2673 | } |
| 2674 | |
| 2675 | written = read = 0; |
| 2676 | for( j = 0; j < ROUNDS; j++ ) |
| 2677 | { |
| 2678 | TEST_ASSERT( put_ret[j] == mbedtls_test_buffer_put( &buf, |
| 2679 | input + written, put[j] ) ); |
| 2680 | written += put_ret[j]; |
| 2681 | TEST_ASSERT( get_ret[j] == mbedtls_test_buffer_get( &buf, |
| 2682 | output + read, get[j] ) ); |
| 2683 | read += get_ret[j]; |
| 2684 | TEST_ASSERT( read <= written ); |
| 2685 | if( get_ret[j] > 0 ) |
| 2686 | { |
| 2687 | TEST_ASSERT( memcmp( output + read - get_ret[j], |
| 2688 | input + read - get_ret[j], get_ret[j] ) |
| 2689 | == 0 ); |
| 2690 | } |
| 2691 | } |
| 2692 | |
| 2693 | exit: |
| 2694 | |
| 2695 | mbedtls_free( input ); |
| 2696 | mbedtls_free( output ); |
| 2697 | mbedtls_test_buffer_free( &buf ); |
| 2698 | } |
| 2699 | /* END_CASE */ |
| 2700 | |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2701 | /* |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2702 | * Test if the implementation of `mbedtls_mock_socket` related I/O functions is |
| 2703 | * correct and works as expected on unconnected sockets. |
| 2704 | */ |
| 2705 | |
| 2706 | /* BEGIN_CASE */ |
| 2707 | void ssl_mock_sanity( ) |
| 2708 | { |
| 2709 | enum { MSGLEN = 105 }; |
Paul Elliott | 21c8fe5 | 2021-11-24 16:54:26 +0000 | [diff] [blame] | 2710 | unsigned char message[MSGLEN] = { 0 }; |
| 2711 | unsigned char received[MSGLEN] = { 0 }; |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2712 | mbedtls_mock_socket socket; |
| 2713 | |
| 2714 | mbedtls_mock_socket_init( &socket ); |
| 2715 | TEST_ASSERT( mbedtls_mock_tcp_send_b( &socket, message, MSGLEN ) < 0 ); |
| 2716 | mbedtls_mock_socket_close( &socket ); |
| 2717 | mbedtls_mock_socket_init( &socket ); |
| 2718 | TEST_ASSERT( mbedtls_mock_tcp_recv_b( &socket, received, MSGLEN ) < 0 ); |
| 2719 | mbedtls_mock_socket_close( &socket ); |
| 2720 | |
| 2721 | mbedtls_mock_socket_init( &socket ); |
| 2722 | TEST_ASSERT( mbedtls_mock_tcp_send_nb( &socket, message, MSGLEN ) < 0 ); |
| 2723 | mbedtls_mock_socket_close( &socket ); |
| 2724 | mbedtls_mock_socket_init( &socket ); |
| 2725 | TEST_ASSERT( mbedtls_mock_tcp_recv_nb( &socket, received, MSGLEN ) < 0 ); |
| 2726 | mbedtls_mock_socket_close( &socket ); |
| 2727 | |
| 2728 | exit: |
| 2729 | |
| 2730 | mbedtls_mock_socket_close( &socket ); |
| 2731 | } |
| 2732 | /* END_CASE */ |
| 2733 | |
| 2734 | /* |
| 2735 | * Test if the implementation of `mbedtls_mock_socket` related functions can |
| 2736 | * send a single message from the client to the server. |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2737 | */ |
| 2738 | |
| 2739 | /* BEGIN_CASE */ |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2740 | void ssl_mock_tcp( int blocking ) |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2741 | { |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2742 | enum { MSGLEN = 105 }; |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2743 | enum { BUFLEN = MSGLEN / 5 }; |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2744 | unsigned char message[MSGLEN]; |
| 2745 | unsigned char received[MSGLEN]; |
| 2746 | mbedtls_mock_socket client; |
| 2747 | mbedtls_mock_socket server; |
| 2748 | size_t written, read; |
| 2749 | int send_ret, recv_ret; |
| 2750 | mbedtls_ssl_send_t *send; |
| 2751 | mbedtls_ssl_recv_t *recv; |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2752 | unsigned i; |
| 2753 | |
| 2754 | if( blocking == 0 ) |
| 2755 | { |
| 2756 | send = mbedtls_mock_tcp_send_nb; |
| 2757 | recv = mbedtls_mock_tcp_recv_nb; |
| 2758 | } |
| 2759 | else |
| 2760 | { |
| 2761 | send = mbedtls_mock_tcp_send_b; |
| 2762 | recv = mbedtls_mock_tcp_recv_b; |
| 2763 | } |
| 2764 | |
| 2765 | mbedtls_mock_socket_init( &client ); |
| 2766 | mbedtls_mock_socket_init( &server ); |
| 2767 | |
| 2768 | /* Fill up the buffer with structured data so that unwanted changes |
| 2769 | * can be detected */ |
| 2770 | for( i = 0; i < MSGLEN; i++ ) |
| 2771 | { |
| 2772 | message[i] = i & 0xFF; |
| 2773 | } |
| 2774 | |
| 2775 | /* Make sure that sending a message takes a few iterations. */ |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2776 | TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, BUFLEN ) ); |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2777 | |
| 2778 | /* Send the message to the server */ |
| 2779 | send_ret = recv_ret = 1; |
| 2780 | written = read = 0; |
| 2781 | while( send_ret != 0 || recv_ret != 0 ) |
| 2782 | { |
| 2783 | send_ret = send( &client, message + written, MSGLEN - written ); |
| 2784 | |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2785 | TEST_ASSERT( send_ret >= 0 ); |
| 2786 | TEST_ASSERT( send_ret <= BUFLEN ); |
| 2787 | written += send_ret; |
| 2788 | |
| 2789 | /* If the buffer is full we can test blocking and non-blocking send */ |
| 2790 | if ( send_ret == BUFLEN ) |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2791 | { |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2792 | int blocking_ret = send( &client, message , 1 ); |
| 2793 | if ( blocking ) |
| 2794 | { |
| 2795 | TEST_ASSERT( blocking_ret == 0 ); |
| 2796 | } |
| 2797 | else |
| 2798 | { |
| 2799 | TEST_ASSERT( blocking_ret == MBEDTLS_ERR_SSL_WANT_WRITE ); |
| 2800 | } |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2801 | } |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2802 | |
| 2803 | recv_ret = recv( &server, received + read, MSGLEN - read ); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2804 | |
| 2805 | /* The result depends on whether any data was sent */ |
| 2806 | if ( send_ret > 0 ) |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2807 | { |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2808 | TEST_ASSERT( recv_ret > 0 ); |
| 2809 | TEST_ASSERT( recv_ret <= BUFLEN ); |
| 2810 | read += recv_ret; |
| 2811 | } |
| 2812 | else if( blocking ) |
| 2813 | { |
| 2814 | TEST_ASSERT( recv_ret == 0 ); |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2815 | } |
| 2816 | else |
| 2817 | { |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2818 | TEST_ASSERT( recv_ret == MBEDTLS_ERR_SSL_WANT_READ ); |
| 2819 | recv_ret = 0; |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2820 | } |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2821 | |
| 2822 | /* If the buffer is empty we can test blocking and non-blocking read */ |
| 2823 | if ( recv_ret == BUFLEN ) |
| 2824 | { |
| 2825 | int blocking_ret = recv( &server, received, 1 ); |
| 2826 | if ( blocking ) |
| 2827 | { |
| 2828 | TEST_ASSERT( blocking_ret == 0 ); |
| 2829 | } |
| 2830 | else |
| 2831 | { |
| 2832 | TEST_ASSERT( blocking_ret == MBEDTLS_ERR_SSL_WANT_READ ); |
| 2833 | } |
| 2834 | } |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2835 | } |
| 2836 | TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 ); |
| 2837 | |
| 2838 | exit: |
| 2839 | |
| 2840 | mbedtls_mock_socket_close( &client ); |
| 2841 | mbedtls_mock_socket_close( &server ); |
| 2842 | } |
| 2843 | /* END_CASE */ |
| 2844 | |
| 2845 | /* |
| 2846 | * Test if the implementation of `mbedtls_mock_socket` related functions can |
| 2847 | * send messages in both direction at the same time (with the I/O calls |
| 2848 | * interleaving). |
| 2849 | */ |
| 2850 | |
| 2851 | /* BEGIN_CASE */ |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2852 | void ssl_mock_tcp_interleaving( int blocking ) |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2853 | { |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2854 | enum { ROUNDS = 2 }; |
| 2855 | enum { MSGLEN = 105 }; |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2856 | enum { BUFLEN = MSGLEN / 5 }; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2857 | unsigned char message[ROUNDS][MSGLEN]; |
| 2858 | unsigned char received[ROUNDS][MSGLEN]; |
| 2859 | mbedtls_mock_socket client; |
| 2860 | mbedtls_mock_socket server; |
| 2861 | size_t written[ROUNDS]; |
| 2862 | size_t read[ROUNDS]; |
| 2863 | int send_ret[ROUNDS]; |
| 2864 | int recv_ret[ROUNDS]; |
| 2865 | unsigned i, j, progress; |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 2866 | mbedtls_ssl_send_t *send; |
| 2867 | mbedtls_ssl_recv_t *recv; |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 2868 | |
| 2869 | if( blocking == 0 ) |
| 2870 | { |
| 2871 | send = mbedtls_mock_tcp_send_nb; |
| 2872 | recv = mbedtls_mock_tcp_recv_nb; |
| 2873 | } |
| 2874 | else |
| 2875 | { |
| 2876 | send = mbedtls_mock_tcp_send_b; |
| 2877 | recv = mbedtls_mock_tcp_recv_b; |
| 2878 | } |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2879 | |
| 2880 | mbedtls_mock_socket_init( &client ); |
| 2881 | mbedtls_mock_socket_init( &server ); |
| 2882 | |
| 2883 | /* Fill up the buffers with structured data so that unwanted changes |
| 2884 | * can be detected */ |
| 2885 | for( i = 0; i < ROUNDS; i++ ) |
| 2886 | { |
| 2887 | for( j = 0; j < MSGLEN; j++ ) |
| 2888 | { |
| 2889 | message[i][j] = ( i * MSGLEN + j ) & 0xFF; |
| 2890 | } |
| 2891 | } |
| 2892 | |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2893 | /* Make sure that sending a message takes a few iterations. */ |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2894 | TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, BUFLEN ) ); |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2895 | |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2896 | /* Send the message from both sides, interleaving. */ |
| 2897 | progress = 1; |
| 2898 | for( i = 0; i < ROUNDS; i++ ) |
| 2899 | { |
| 2900 | written[i] = 0; |
| 2901 | read[i] = 0; |
| 2902 | } |
| 2903 | /* This loop does not stop as long as there was a successful write or read |
| 2904 | * of at least one byte on either side. */ |
| 2905 | while( progress != 0 ) |
| 2906 | { |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2907 | mbedtls_mock_socket *socket; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2908 | |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2909 | for( i = 0; i < ROUNDS; i++ ) |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 2910 | { |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2911 | /* First sending is from the client */ |
| 2912 | socket = ( i % 2 == 0 ) ? ( &client ) : ( &server ); |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2913 | |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2914 | send_ret[i] = send( socket, message[i] + written[i], |
| 2915 | MSGLEN - written[i] ); |
| 2916 | TEST_ASSERT( send_ret[i] >= 0 ); |
| 2917 | TEST_ASSERT( send_ret[i] <= BUFLEN ); |
| 2918 | written[i] += send_ret[i]; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2919 | |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2920 | /* If the buffer is full we can test blocking and non-blocking |
| 2921 | * send */ |
| 2922 | if ( send_ret[i] == BUFLEN ) |
| 2923 | { |
| 2924 | int blocking_ret = send( socket, message[i] , 1 ); |
| 2925 | if ( blocking ) |
| 2926 | { |
| 2927 | TEST_ASSERT( blocking_ret == 0 ); |
| 2928 | } |
| 2929 | else |
| 2930 | { |
| 2931 | TEST_ASSERT( blocking_ret == MBEDTLS_ERR_SSL_WANT_WRITE ); |
| 2932 | } |
| 2933 | } |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 2934 | } |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2935 | |
| 2936 | for( i = 0; i < ROUNDS; i++ ) |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 2937 | { |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2938 | /* First receiving is from the server */ |
| 2939 | socket = ( i % 2 == 0 ) ? ( &server ) : ( &client ); |
| 2940 | |
| 2941 | recv_ret[i] = recv( socket, received[i] + read[i], |
| 2942 | MSGLEN - read[i] ); |
| 2943 | |
| 2944 | /* The result depends on whether any data was sent */ |
| 2945 | if ( send_ret[i] > 0 ) |
| 2946 | { |
| 2947 | TEST_ASSERT( recv_ret[i] > 0 ); |
| 2948 | TEST_ASSERT( recv_ret[i] <= BUFLEN ); |
| 2949 | read[i] += recv_ret[i]; |
| 2950 | } |
| 2951 | else if( blocking ) |
| 2952 | { |
| 2953 | TEST_ASSERT( recv_ret[i] == 0 ); |
| 2954 | } |
| 2955 | else |
| 2956 | { |
| 2957 | TEST_ASSERT( recv_ret[i] == MBEDTLS_ERR_SSL_WANT_READ ); |
| 2958 | recv_ret[i] = 0; |
| 2959 | } |
| 2960 | |
| 2961 | /* If the buffer is empty we can test blocking and non-blocking |
| 2962 | * read */ |
| 2963 | if ( recv_ret[i] == BUFLEN ) |
| 2964 | { |
| 2965 | int blocking_ret = recv( socket, received[i], 1 ); |
| 2966 | if ( blocking ) |
| 2967 | { |
| 2968 | TEST_ASSERT( blocking_ret == 0 ); |
| 2969 | } |
| 2970 | else |
| 2971 | { |
| 2972 | TEST_ASSERT( blocking_ret == MBEDTLS_ERR_SSL_WANT_READ ); |
| 2973 | } |
| 2974 | } |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 2975 | } |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2976 | |
| 2977 | progress = 0; |
| 2978 | for( i = 0; i < ROUNDS; i++ ) |
| 2979 | { |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2980 | progress += send_ret[i] + recv_ret[i]; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2981 | } |
| 2982 | } |
| 2983 | |
| 2984 | for( i = 0; i < ROUNDS; i++ ) |
| 2985 | TEST_ASSERT( memcmp( message[i], received[i], MSGLEN ) == 0 ); |
| 2986 | |
| 2987 | exit: |
| 2988 | |
| 2989 | mbedtls_mock_socket_close( &client ); |
| 2990 | mbedtls_mock_socket_close( &server ); |
| 2991 | } |
| 2992 | /* END_CASE */ |
| 2993 | |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2994 | /* BEGIN_CASE */ |
| 2995 | void ssl_message_queue_sanity( ) |
| 2996 | { |
| 2997 | mbedtls_test_message_queue queue; |
| 2998 | |
| 2999 | /* Trying to push/pull to an empty queue */ |
| 3000 | TEST_ASSERT( mbedtls_test_message_queue_push_info( NULL, 1 ) |
| 3001 | == MBEDTLS_TEST_ERROR_ARG_NULL ); |
| 3002 | TEST_ASSERT( mbedtls_test_message_queue_pop_info( NULL, 1 ) |
| 3003 | == MBEDTLS_TEST_ERROR_ARG_NULL ); |
| 3004 | |
Andrzej Kurek | 89bdc58 | 2020-03-09 06:29:43 -0400 | [diff] [blame] | 3005 | TEST_ASSERT( mbedtls_test_message_queue_setup( &queue, 3 ) == 0 ); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 3006 | TEST_ASSERT( queue.capacity == 3 ); |
| 3007 | TEST_ASSERT( queue.num == 0 ); |
| 3008 | |
| 3009 | exit: |
| 3010 | mbedtls_test_message_queue_free( &queue ); |
| 3011 | } |
| 3012 | /* END_CASE */ |
| 3013 | |
| 3014 | /* BEGIN_CASE */ |
| 3015 | void ssl_message_queue_basic( ) |
| 3016 | { |
| 3017 | mbedtls_test_message_queue queue; |
| 3018 | |
Andrzej Kurek | 89bdc58 | 2020-03-09 06:29:43 -0400 | [diff] [blame] | 3019 | TEST_ASSERT( mbedtls_test_message_queue_setup( &queue, 3 ) == 0 ); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 3020 | |
| 3021 | /* Sanity test - 3 pushes and 3 pops with sufficient space */ |
| 3022 | TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 ); |
| 3023 | TEST_ASSERT( queue.capacity == 3 ); |
| 3024 | TEST_ASSERT( queue.num == 1 ); |
| 3025 | TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 ); |
| 3026 | TEST_ASSERT( queue.capacity == 3 ); |
| 3027 | TEST_ASSERT( queue.num == 2 ); |
| 3028 | TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 2 ) == 2 ); |
| 3029 | TEST_ASSERT( queue.capacity == 3 ); |
| 3030 | TEST_ASSERT( queue.num == 3 ); |
| 3031 | |
| 3032 | TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 ); |
| 3033 | TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 ); |
| 3034 | TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 2 ) == 2 ); |
| 3035 | |
| 3036 | exit: |
| 3037 | mbedtls_test_message_queue_free( &queue ); |
| 3038 | } |
| 3039 | /* END_CASE */ |
| 3040 | |
| 3041 | /* BEGIN_CASE */ |
| 3042 | void ssl_message_queue_overflow_underflow( ) |
| 3043 | { |
| 3044 | mbedtls_test_message_queue queue; |
| 3045 | |
Andrzej Kurek | 89bdc58 | 2020-03-09 06:29:43 -0400 | [diff] [blame] | 3046 | TEST_ASSERT( mbedtls_test_message_queue_setup( &queue, 3 ) == 0 ); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 3047 | |
| 3048 | /* 4 pushes (last one with an error), 4 pops (last one with an error) */ |
| 3049 | TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 ); |
| 3050 | TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 ); |
| 3051 | TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 2 ) == 2 ); |
| 3052 | TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 3 ) |
Andrzej Kurek | f46b912 | 2020-02-07 08:19:00 -0500 | [diff] [blame] | 3053 | == MBEDTLS_ERR_SSL_WANT_WRITE ); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 3054 | |
| 3055 | TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 ); |
| 3056 | TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 ); |
| 3057 | TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 2 ) == 2 ); |
| 3058 | |
| 3059 | TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) |
Andrzej Kurek | f46b912 | 2020-02-07 08:19:00 -0500 | [diff] [blame] | 3060 | == MBEDTLS_ERR_SSL_WANT_READ ); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 3061 | |
| 3062 | exit: |
| 3063 | mbedtls_test_message_queue_free( &queue ); |
| 3064 | } |
| 3065 | /* END_CASE */ |
| 3066 | |
| 3067 | /* BEGIN_CASE */ |
| 3068 | void ssl_message_queue_interleaved( ) |
| 3069 | { |
| 3070 | mbedtls_test_message_queue queue; |
| 3071 | |
Andrzej Kurek | 89bdc58 | 2020-03-09 06:29:43 -0400 | [diff] [blame] | 3072 | TEST_ASSERT( mbedtls_test_message_queue_setup( &queue, 3 ) == 0 ); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 3073 | |
| 3074 | /* Interleaved test - [2 pushes, 1 pop] twice, and then two pops |
| 3075 | * (to wrap around the buffer) */ |
| 3076 | TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 ); |
| 3077 | TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 ); |
| 3078 | |
| 3079 | TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 ); |
| 3080 | |
| 3081 | TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 2 ) == 2 ); |
| 3082 | TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 3 ) == 3 ); |
| 3083 | |
| 3084 | TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 ); |
| 3085 | TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 2 ) == 2 ); |
| 3086 | |
| 3087 | TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 5 ) == 5 ); |
| 3088 | TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 8 ) == 8 ); |
| 3089 | |
| 3090 | TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 3 ) == 3 ); |
| 3091 | |
| 3092 | TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 5 ) == 5 ); |
| 3093 | |
| 3094 | TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 8 ) == 8 ); |
| 3095 | |
| 3096 | exit: |
| 3097 | mbedtls_test_message_queue_free( &queue ); |
| 3098 | } |
| 3099 | /* END_CASE */ |
| 3100 | |
| 3101 | /* BEGIN_CASE */ |
| 3102 | void ssl_message_queue_insufficient_buffer( ) |
| 3103 | { |
| 3104 | mbedtls_test_message_queue queue; |
| 3105 | size_t message_len = 10; |
| 3106 | size_t buffer_len = 5; |
| 3107 | |
Andrzej Kurek | 89bdc58 | 2020-03-09 06:29:43 -0400 | [diff] [blame] | 3108 | TEST_ASSERT( mbedtls_test_message_queue_setup( &queue, 1 ) == 0 ); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 3109 | |
| 3110 | /* Popping without a sufficient buffer */ |
| 3111 | TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, message_len ) |
| 3112 | == (int) message_len ); |
| 3113 | TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, buffer_len ) |
| 3114 | == (int) buffer_len ); |
| 3115 | exit: |
| 3116 | mbedtls_test_message_queue_free( &queue ); |
| 3117 | } |
| 3118 | /* END_CASE */ |
| 3119 | |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3120 | /* BEGIN_CASE */ |
| 3121 | void ssl_message_mock_uninitialized( ) |
| 3122 | { |
| 3123 | enum { MSGLEN = 10 }; |
Shawn Carey | 03092f5 | 2021-05-13 10:38:32 -0400 | [diff] [blame] | 3124 | unsigned char message[MSGLEN] = {0}, received[MSGLEN]; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3125 | mbedtls_mock_socket client, server; |
| 3126 | mbedtls_test_message_queue server_queue, client_queue; |
| 3127 | mbedtls_test_message_socket_context server_context, client_context; |
Andrzej Kurek | 45916ba | 2020-03-05 14:46:22 -0500 | [diff] [blame] | 3128 | mbedtls_message_socket_init( &server_context ); |
| 3129 | mbedtls_message_socket_init( &client_context ); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3130 | |
| 3131 | /* Send with a NULL context */ |
| 3132 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( NULL, message, MSGLEN ) |
| 3133 | == MBEDTLS_TEST_ERROR_CONTEXT_ERROR ); |
| 3134 | |
| 3135 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( NULL, message, MSGLEN ) |
| 3136 | == MBEDTLS_TEST_ERROR_CONTEXT_ERROR ); |
| 3137 | |
| 3138 | TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 1, |
| 3139 | &server, |
| 3140 | &server_context ) == 0 ); |
| 3141 | |
| 3142 | TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 1, |
| 3143 | &client, |
| 3144 | &client_context ) == 0 ); |
| 3145 | |
| 3146 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, MSGLEN ) |
| 3147 | == MBEDTLS_TEST_ERROR_SEND_FAILED ); |
| 3148 | |
| 3149 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN ) |
Andrzej Kurek | f46b912 | 2020-02-07 08:19:00 -0500 | [diff] [blame] | 3150 | == MBEDTLS_ERR_SSL_WANT_READ ); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3151 | |
| 3152 | /* Push directly to a queue to later simulate a disconnected behavior */ |
| 3153 | TEST_ASSERT( mbedtls_test_message_queue_push_info( &server_queue, MSGLEN ) |
| 3154 | == MSGLEN ); |
| 3155 | |
| 3156 | /* Test if there's an error when trying to read from a disconnected |
| 3157 | * socket */ |
| 3158 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN ) |
| 3159 | == MBEDTLS_TEST_ERROR_RECV_FAILED ); |
| 3160 | exit: |
| 3161 | mbedtls_message_socket_close( &server_context ); |
| 3162 | mbedtls_message_socket_close( &client_context ); |
| 3163 | } |
| 3164 | /* END_CASE */ |
| 3165 | |
| 3166 | /* BEGIN_CASE */ |
| 3167 | void ssl_message_mock_basic( ) |
| 3168 | { |
| 3169 | enum { MSGLEN = 10 }; |
| 3170 | unsigned char message[MSGLEN], received[MSGLEN]; |
| 3171 | mbedtls_mock_socket client, server; |
| 3172 | unsigned i; |
| 3173 | mbedtls_test_message_queue server_queue, client_queue; |
| 3174 | mbedtls_test_message_socket_context server_context, client_context; |
Andrzej Kurek | 45916ba | 2020-03-05 14:46:22 -0500 | [diff] [blame] | 3175 | mbedtls_message_socket_init( &server_context ); |
| 3176 | mbedtls_message_socket_init( &client_context ); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3177 | |
| 3178 | TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 1, |
| 3179 | &server, |
| 3180 | &server_context ) == 0 ); |
| 3181 | |
| 3182 | TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 1, |
| 3183 | &client, |
| 3184 | &client_context ) == 0 ); |
| 3185 | |
| 3186 | /* Fill up the buffer with structured data so that unwanted changes |
| 3187 | * can be detected */ |
| 3188 | for( i = 0; i < MSGLEN; i++ ) |
| 3189 | { |
| 3190 | message[i] = i & 0xFF; |
| 3191 | } |
| 3192 | TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, |
| 3193 | MSGLEN ) ); |
| 3194 | |
| 3195 | /* Send the message to the server */ |
| 3196 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, |
| 3197 | MSGLEN ) == MSGLEN ); |
| 3198 | |
| 3199 | /* Read from the server */ |
| 3200 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN ) |
| 3201 | == MSGLEN ); |
| 3202 | |
| 3203 | TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 ); |
| 3204 | memset( received, 0, MSGLEN ); |
| 3205 | |
| 3206 | /* Send the message to the client */ |
| 3207 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &server_context, message, |
| 3208 | MSGLEN ) == MSGLEN ); |
| 3209 | |
| 3210 | /* Read from the client */ |
| 3211 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &client_context, received, MSGLEN ) |
| 3212 | == MSGLEN ); |
| 3213 | TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 ); |
| 3214 | |
| 3215 | exit: |
| 3216 | mbedtls_message_socket_close( &server_context ); |
| 3217 | mbedtls_message_socket_close( &client_context ); |
| 3218 | } |
| 3219 | /* END_CASE */ |
| 3220 | |
| 3221 | /* BEGIN_CASE */ |
| 3222 | void ssl_message_mock_queue_overflow_underflow( ) |
| 3223 | { |
| 3224 | enum { MSGLEN = 10 }; |
| 3225 | unsigned char message[MSGLEN], received[MSGLEN]; |
| 3226 | mbedtls_mock_socket client, server; |
| 3227 | unsigned i; |
| 3228 | mbedtls_test_message_queue server_queue, client_queue; |
| 3229 | mbedtls_test_message_socket_context server_context, client_context; |
Andrzej Kurek | 45916ba | 2020-03-05 14:46:22 -0500 | [diff] [blame] | 3230 | mbedtls_message_socket_init( &server_context ); |
| 3231 | mbedtls_message_socket_init( &client_context ); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3232 | |
| 3233 | TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 2, |
| 3234 | &server, |
| 3235 | &server_context ) == 0 ); |
| 3236 | |
| 3237 | TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 2, |
| 3238 | &client, |
| 3239 | &client_context ) == 0 ); |
| 3240 | |
| 3241 | /* Fill up the buffer with structured data so that unwanted changes |
| 3242 | * can be detected */ |
| 3243 | for( i = 0; i < MSGLEN; i++ ) |
| 3244 | { |
| 3245 | message[i] = i & 0xFF; |
| 3246 | } |
| 3247 | TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, |
| 3248 | MSGLEN*2 ) ); |
| 3249 | |
| 3250 | /* Send three message to the server, last one with an error */ |
| 3251 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, |
| 3252 | MSGLEN - 1 ) == MSGLEN - 1 ); |
| 3253 | |
| 3254 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, |
| 3255 | MSGLEN ) == MSGLEN ); |
| 3256 | |
| 3257 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, |
| 3258 | MSGLEN ) |
Andrzej Kurek | f46b912 | 2020-02-07 08:19:00 -0500 | [diff] [blame] | 3259 | == MBEDTLS_ERR_SSL_WANT_WRITE ); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3260 | |
| 3261 | /* Read three messages from the server, last one with an error */ |
| 3262 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, |
| 3263 | MSGLEN - 1 ) == MSGLEN - 1 ); |
| 3264 | |
| 3265 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN ) |
| 3266 | == MSGLEN ); |
| 3267 | |
| 3268 | TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 ); |
| 3269 | |
| 3270 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN ) |
Andrzej Kurek | f46b912 | 2020-02-07 08:19:00 -0500 | [diff] [blame] | 3271 | == MBEDTLS_ERR_SSL_WANT_READ ); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3272 | |
| 3273 | exit: |
| 3274 | mbedtls_message_socket_close( &server_context ); |
| 3275 | mbedtls_message_socket_close( &client_context ); |
| 3276 | } |
| 3277 | /* END_CASE */ |
| 3278 | |
| 3279 | /* BEGIN_CASE */ |
| 3280 | void ssl_message_mock_socket_overflow( ) |
| 3281 | { |
| 3282 | enum { MSGLEN = 10 }; |
| 3283 | unsigned char message[MSGLEN], received[MSGLEN]; |
| 3284 | mbedtls_mock_socket client, server; |
| 3285 | unsigned i; |
| 3286 | mbedtls_test_message_queue server_queue, client_queue; |
| 3287 | mbedtls_test_message_socket_context server_context, client_context; |
Andrzej Kurek | 45916ba | 2020-03-05 14:46:22 -0500 | [diff] [blame] | 3288 | mbedtls_message_socket_init( &server_context ); |
| 3289 | mbedtls_message_socket_init( &client_context ); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3290 | |
| 3291 | TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 2, |
| 3292 | &server, |
| 3293 | &server_context ) == 0 ); |
| 3294 | |
| 3295 | TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 2, |
| 3296 | &client, |
| 3297 | &client_context ) == 0 ); |
| 3298 | |
| 3299 | /* Fill up the buffer with structured data so that unwanted changes |
| 3300 | * can be detected */ |
| 3301 | for( i = 0; i < MSGLEN; i++ ) |
| 3302 | { |
| 3303 | message[i] = i & 0xFF; |
| 3304 | } |
| 3305 | TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, |
| 3306 | MSGLEN ) ); |
| 3307 | |
| 3308 | /* Send two message to the server, second one with an error */ |
| 3309 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, |
| 3310 | MSGLEN ) == MSGLEN ); |
| 3311 | |
| 3312 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, |
| 3313 | MSGLEN ) |
| 3314 | == MBEDTLS_TEST_ERROR_SEND_FAILED ); |
| 3315 | |
| 3316 | /* Read the only message from the server */ |
| 3317 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN ) |
| 3318 | == MSGLEN ); |
| 3319 | |
| 3320 | TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 ); |
| 3321 | |
| 3322 | exit: |
| 3323 | mbedtls_message_socket_close( &server_context ); |
| 3324 | mbedtls_message_socket_close( &client_context ); |
| 3325 | } |
| 3326 | /* END_CASE */ |
| 3327 | |
| 3328 | /* BEGIN_CASE */ |
| 3329 | void ssl_message_mock_truncated( ) |
| 3330 | { |
| 3331 | enum { MSGLEN = 10 }; |
| 3332 | unsigned char message[MSGLEN], received[MSGLEN]; |
| 3333 | mbedtls_mock_socket client, server; |
| 3334 | unsigned i; |
| 3335 | mbedtls_test_message_queue server_queue, client_queue; |
| 3336 | mbedtls_test_message_socket_context server_context, client_context; |
Andrzej Kurek | 45916ba | 2020-03-05 14:46:22 -0500 | [diff] [blame] | 3337 | mbedtls_message_socket_init( &server_context ); |
| 3338 | mbedtls_message_socket_init( &client_context ); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3339 | |
| 3340 | TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 2, |
| 3341 | &server, |
| 3342 | &server_context ) == 0 ); |
| 3343 | |
| 3344 | TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 2, |
| 3345 | &client, |
| 3346 | &client_context ) == 0 ); |
| 3347 | |
| 3348 | memset( received, 0, MSGLEN ); |
| 3349 | /* Fill up the buffer with structured data so that unwanted changes |
| 3350 | * can be detected */ |
| 3351 | for( i = 0; i < MSGLEN; i++ ) |
| 3352 | { |
| 3353 | message[i] = i & 0xFF; |
| 3354 | } |
| 3355 | TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, |
| 3356 | 2 * MSGLEN ) ); |
| 3357 | |
| 3358 | /* Send two messages to the server, the second one small enough to fit in the |
| 3359 | * receiver's buffer. */ |
| 3360 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, |
| 3361 | MSGLEN ) == MSGLEN ); |
| 3362 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, |
| 3363 | MSGLEN / 2 ) == MSGLEN / 2 ); |
| 3364 | /* Read a truncated message from the server */ |
| 3365 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN/2 ) |
| 3366 | == MSGLEN/2 ); |
| 3367 | |
| 3368 | /* Test that the first half of the message is valid, and second one isn't */ |
| 3369 | TEST_ASSERT( memcmp( message, received, MSGLEN/2 ) == 0 ); |
| 3370 | TEST_ASSERT( memcmp( message + MSGLEN/2, received + MSGLEN/2, MSGLEN/2 ) |
| 3371 | != 0 ); |
| 3372 | memset( received, 0, MSGLEN ); |
| 3373 | |
| 3374 | /* Read a full message from the server */ |
| 3375 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN/2 ) |
| 3376 | == MSGLEN / 2 ); |
| 3377 | |
| 3378 | /* Test that the first half of the message is valid */ |
| 3379 | TEST_ASSERT( memcmp( message, received, MSGLEN/2 ) == 0 ); |
| 3380 | |
| 3381 | exit: |
| 3382 | mbedtls_message_socket_close( &server_context ); |
| 3383 | mbedtls_message_socket_close( &client_context ); |
| 3384 | } |
| 3385 | /* END_CASE */ |
| 3386 | |
| 3387 | /* BEGIN_CASE */ |
| 3388 | void ssl_message_mock_socket_read_error( ) |
| 3389 | { |
| 3390 | enum { MSGLEN = 10 }; |
| 3391 | unsigned char message[MSGLEN], received[MSGLEN]; |
| 3392 | mbedtls_mock_socket client, server; |
| 3393 | unsigned i; |
| 3394 | mbedtls_test_message_queue server_queue, client_queue; |
| 3395 | mbedtls_test_message_socket_context server_context, client_context; |
Andrzej Kurek | 45916ba | 2020-03-05 14:46:22 -0500 | [diff] [blame] | 3396 | mbedtls_message_socket_init( &server_context ); |
| 3397 | mbedtls_message_socket_init( &client_context ); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3398 | |
| 3399 | TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 1, |
| 3400 | &server, |
| 3401 | &server_context ) == 0 ); |
| 3402 | |
| 3403 | TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 1, |
| 3404 | &client, |
| 3405 | &client_context ) == 0 ); |
| 3406 | |
| 3407 | /* Fill up the buffer with structured data so that unwanted changes |
| 3408 | * can be detected */ |
| 3409 | for( i = 0; i < MSGLEN; i++ ) |
| 3410 | { |
| 3411 | message[i] = i & 0xFF; |
| 3412 | } |
| 3413 | TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, |
| 3414 | MSGLEN ) ); |
| 3415 | |
| 3416 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, |
| 3417 | MSGLEN ) == MSGLEN ); |
| 3418 | |
| 3419 | /* Force a read error by disconnecting the socket by hand */ |
| 3420 | server.status = 0; |
| 3421 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN ) |
| 3422 | == MBEDTLS_TEST_ERROR_RECV_FAILED ); |
| 3423 | /* Return to a valid state */ |
| 3424 | server.status = MBEDTLS_MOCK_SOCKET_CONNECTED; |
| 3425 | |
| 3426 | memset( received, 0, sizeof( received ) ); |
| 3427 | |
| 3428 | /* Test that even though the server tried to read once disconnected, the |
| 3429 | * continuity is preserved */ |
| 3430 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN ) |
| 3431 | == MSGLEN ); |
| 3432 | |
| 3433 | TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 ); |
| 3434 | |
| 3435 | exit: |
| 3436 | mbedtls_message_socket_close( &server_context ); |
| 3437 | mbedtls_message_socket_close( &client_context ); |
| 3438 | } |
| 3439 | /* END_CASE */ |
| 3440 | |
| 3441 | /* BEGIN_CASE */ |
| 3442 | void ssl_message_mock_interleaved_one_way( ) |
| 3443 | { |
| 3444 | enum { MSGLEN = 10 }; |
| 3445 | unsigned char message[MSGLEN], received[MSGLEN]; |
| 3446 | mbedtls_mock_socket client, server; |
| 3447 | unsigned i; |
| 3448 | mbedtls_test_message_queue server_queue, client_queue; |
| 3449 | mbedtls_test_message_socket_context server_context, client_context; |
Andrzej Kurek | 45916ba | 2020-03-05 14:46:22 -0500 | [diff] [blame] | 3450 | mbedtls_message_socket_init( &server_context ); |
| 3451 | mbedtls_message_socket_init( &client_context ); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3452 | |
| 3453 | TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 3, |
| 3454 | &server, |
| 3455 | &server_context ) == 0 ); |
| 3456 | |
| 3457 | TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 3, |
| 3458 | &client, |
| 3459 | &client_context ) == 0 ); |
| 3460 | |
| 3461 | /* Fill up the buffer with structured data so that unwanted changes |
| 3462 | * can be detected */ |
| 3463 | for( i = 0; i < MSGLEN; i++ ) |
| 3464 | { |
| 3465 | message[i] = i & 0xFF; |
| 3466 | } |
| 3467 | TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, |
| 3468 | MSGLEN*3 ) ); |
| 3469 | |
| 3470 | /* Interleaved test - [2 sends, 1 read] twice, and then two reads |
| 3471 | * (to wrap around the buffer) */ |
| 3472 | for( i = 0; i < 2; i++ ) |
| 3473 | { |
| 3474 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, |
| 3475 | MSGLEN ) == MSGLEN ); |
| 3476 | |
| 3477 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, |
| 3478 | MSGLEN ) == MSGLEN ); |
| 3479 | |
| 3480 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, |
| 3481 | MSGLEN ) == MSGLEN ); |
| 3482 | TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 ); |
| 3483 | memset( received, 0, sizeof( received ) ); |
| 3484 | } |
| 3485 | |
| 3486 | for( i = 0; i < 2; i++ ) |
| 3487 | { |
| 3488 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, |
| 3489 | MSGLEN ) == MSGLEN ); |
| 3490 | |
| 3491 | TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 ); |
| 3492 | } |
| 3493 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN ) |
Andrzej Kurek | f46b912 | 2020-02-07 08:19:00 -0500 | [diff] [blame] | 3494 | == MBEDTLS_ERR_SSL_WANT_READ ); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3495 | exit: |
| 3496 | mbedtls_message_socket_close( &server_context ); |
| 3497 | mbedtls_message_socket_close( &client_context ); |
| 3498 | } |
| 3499 | /* END_CASE */ |
| 3500 | |
| 3501 | /* BEGIN_CASE */ |
| 3502 | void ssl_message_mock_interleaved_two_ways( ) |
| 3503 | { |
| 3504 | enum { MSGLEN = 10 }; |
| 3505 | unsigned char message[MSGLEN], received[MSGLEN]; |
| 3506 | mbedtls_mock_socket client, server; |
| 3507 | unsigned i; |
| 3508 | mbedtls_test_message_queue server_queue, client_queue; |
| 3509 | mbedtls_test_message_socket_context server_context, client_context; |
Andrzej Kurek | 45916ba | 2020-03-05 14:46:22 -0500 | [diff] [blame] | 3510 | mbedtls_message_socket_init( &server_context ); |
| 3511 | mbedtls_message_socket_init( &client_context ); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3512 | |
| 3513 | TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 3, |
| 3514 | &server, |
| 3515 | &server_context ) == 0 ); |
| 3516 | |
| 3517 | TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 3, |
| 3518 | &client, |
| 3519 | &client_context ) == 0 ); |
| 3520 | |
| 3521 | /* Fill up the buffer with structured data so that unwanted changes |
| 3522 | * can be detected */ |
| 3523 | for( i = 0; i < MSGLEN; i++ ) |
| 3524 | { |
| 3525 | message[i] = i & 0xFF; |
| 3526 | } |
| 3527 | TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, |
| 3528 | MSGLEN*3 ) ); |
| 3529 | |
| 3530 | /* Interleaved test - [2 sends, 1 read] twice, both ways, and then two reads |
| 3531 | * (to wrap around the buffer) both ways. */ |
| 3532 | for( i = 0; i < 2; i++ ) |
| 3533 | { |
| 3534 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, |
| 3535 | MSGLEN ) == MSGLEN ); |
| 3536 | |
| 3537 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, |
| 3538 | MSGLEN ) == MSGLEN ); |
| 3539 | |
| 3540 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &server_context, message, |
| 3541 | MSGLEN ) == MSGLEN ); |
| 3542 | |
| 3543 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &server_context, message, |
| 3544 | MSGLEN ) == MSGLEN ); |
| 3545 | |
| 3546 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, |
| 3547 | MSGLEN ) == MSGLEN ); |
| 3548 | |
| 3549 | TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 ); |
| 3550 | |
| 3551 | memset( received, 0, sizeof( received ) ); |
| 3552 | |
| 3553 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &client_context, received, |
| 3554 | MSGLEN ) == MSGLEN ); |
| 3555 | |
| 3556 | TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 ); |
| 3557 | |
| 3558 | memset( received, 0, sizeof( received ) ); |
| 3559 | } |
| 3560 | |
| 3561 | for( i = 0; i < 2; i++ ) |
| 3562 | { |
| 3563 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, |
| 3564 | MSGLEN ) == MSGLEN ); |
| 3565 | |
| 3566 | TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 ); |
| 3567 | memset( received, 0, sizeof( received ) ); |
| 3568 | |
| 3569 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &client_context, received, |
| 3570 | MSGLEN ) == MSGLEN ); |
| 3571 | |
| 3572 | TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 ); |
| 3573 | memset( received, 0, sizeof( received ) ); |
| 3574 | } |
| 3575 | |
| 3576 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN ) |
Andrzej Kurek | f46b912 | 2020-02-07 08:19:00 -0500 | [diff] [blame] | 3577 | == MBEDTLS_ERR_SSL_WANT_READ ); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3578 | |
| 3579 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &client_context, received, MSGLEN ) |
Andrzej Kurek | f46b912 | 2020-02-07 08:19:00 -0500 | [diff] [blame] | 3580 | == MBEDTLS_ERR_SSL_WANT_READ ); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3581 | exit: |
| 3582 | mbedtls_message_socket_close( &server_context ); |
| 3583 | mbedtls_message_socket_close( &client_context ); |
| 3584 | } |
| 3585 | /* END_CASE */ |
| 3586 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3587 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
Azim Khan | 5fcca46 | 2018-06-29 11:05:32 +0100 | [diff] [blame] | 3588 | 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] | 3589 | { |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 3590 | uint32_t len = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3591 | mbedtls_ssl_context ssl; |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 3592 | mbedtls_ssl_config conf; |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3593 | |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 3594 | mbedtls_ssl_init( &ssl ); |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 3595 | mbedtls_ssl_config_init( &conf ); |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 3596 | |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 3597 | TEST_ASSERT( mbedtls_ssl_config_defaults( &conf, |
| 3598 | MBEDTLS_SSL_IS_CLIENT, |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 3599 | MBEDTLS_SSL_TRANSPORT_DATAGRAM, |
| 3600 | MBEDTLS_SSL_PRESET_DEFAULT ) == 0 ); |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 3601 | TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 ); |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3602 | |
| 3603 | /* Read previous record numbers */ |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 3604 | for( len = 0; len < prevs->len; len += 6 ) |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3605 | { |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 3606 | memcpy( ssl.in_ctr + 2, prevs->x + len, 6 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3607 | mbedtls_ssl_dtls_replay_update( &ssl ); |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3608 | } |
| 3609 | |
| 3610 | /* Check new number */ |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 3611 | memcpy( ssl.in_ctr + 2, new->x, 6 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3612 | TEST_ASSERT( mbedtls_ssl_dtls_replay_check( &ssl ) == ret ); |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3613 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3614 | mbedtls_ssl_free( &ssl ); |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 3615 | mbedtls_ssl_config_free( &conf ); |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3616 | } |
| 3617 | /* END_CASE */ |
Hanno Becker | b25c0c7 | 2017-05-05 11:24:30 +0100 | [diff] [blame] | 3618 | |
| 3619 | /* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */ |
| 3620 | void ssl_set_hostname_twice( char *hostname0, char *hostname1 ) |
| 3621 | { |
| 3622 | mbedtls_ssl_context ssl; |
| 3623 | mbedtls_ssl_init( &ssl ); |
| 3624 | |
| 3625 | TEST_ASSERT( mbedtls_ssl_set_hostname( &ssl, hostname0 ) == 0 ); |
| 3626 | TEST_ASSERT( mbedtls_ssl_set_hostname( &ssl, hostname1 ) == 0 ); |
| 3627 | |
| 3628 | mbedtls_ssl_free( &ssl ); |
| 3629 | } |
Darryl Green | 11999bb | 2018-03-13 15:22:58 +0000 | [diff] [blame] | 3630 | /* END_CASE */ |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3631 | |
| 3632 | /* BEGIN_CASE */ |
| 3633 | void ssl_crypt_record( int cipher_type, int hash_id, |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 3634 | int etm, int tag_mode, int ver, |
| 3635 | int cid0_len, int cid1_len ) |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3636 | { |
| 3637 | /* |
| 3638 | * Test several record encryptions and decryptions |
| 3639 | * with plenty of space before and after the data |
| 3640 | * within the record buffer. |
| 3641 | */ |
| 3642 | |
| 3643 | int ret; |
| 3644 | int num_records = 16; |
| 3645 | mbedtls_ssl_context ssl; /* ONLY for debugging */ |
| 3646 | |
| 3647 | mbedtls_ssl_transform t0, t1; |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 3648 | unsigned char *buf = NULL; |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3649 | size_t const buflen = 512; |
| 3650 | mbedtls_record rec, rec_backup; |
| 3651 | |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 3652 | USE_PSA_INIT( ); |
| 3653 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3654 | mbedtls_ssl_init( &ssl ); |
| 3655 | mbedtls_ssl_transform_init( &t0 ); |
| 3656 | mbedtls_ssl_transform_init( &t1 ); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 3657 | ret = build_transforms( &t0, &t1, cipher_type, hash_id, |
| 3658 | etm, tag_mode, ver, |
| 3659 | (size_t) cid0_len, |
| 3660 | (size_t) cid1_len ); |
| 3661 | |
Przemyslaw Stekiel | 8c010eb | 2022-02-03 10:44:02 +0100 | [diff] [blame] | 3662 | TEST_ASSERT( ret == 0 ); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3663 | |
Hanno Becker | 3ee5421 | 2019-04-04 16:31:26 +0100 | [diff] [blame] | 3664 | TEST_ASSERT( ( buf = mbedtls_calloc( 1, buflen ) ) != NULL ); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3665 | |
| 3666 | while( num_records-- > 0 ) |
| 3667 | { |
| 3668 | mbedtls_ssl_transform *t_dec, *t_enc; |
| 3669 | /* Take turns in who's sending and who's receiving. */ |
| 3670 | if( num_records % 3 == 0 ) |
| 3671 | { |
| 3672 | t_dec = &t0; |
| 3673 | t_enc = &t1; |
| 3674 | } |
| 3675 | else |
| 3676 | { |
| 3677 | t_dec = &t1; |
| 3678 | t_enc = &t0; |
| 3679 | } |
| 3680 | |
| 3681 | /* |
| 3682 | * The record header affects the transformation in two ways: |
| 3683 | * 1) It determines the AEAD additional data |
| 3684 | * 2) The record counter sometimes determines the IV. |
| 3685 | * |
| 3686 | * Apart from that, the fields don't have influence. |
| 3687 | * In particular, it is currently not the responsibility |
| 3688 | * of ssl_encrypt/decrypt_buf to check if the transform |
| 3689 | * version matches the record version, or that the |
| 3690 | * type is sensible. |
| 3691 | */ |
| 3692 | |
| 3693 | memset( rec.ctr, num_records, sizeof( rec.ctr ) ); |
| 3694 | rec.type = 42; |
| 3695 | rec.ver[0] = num_records; |
| 3696 | rec.ver[1] = num_records; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3697 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 3698 | rec.cid_len = 0; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3699 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3700 | |
| 3701 | rec.buf = buf; |
| 3702 | rec.buf_len = buflen; |
| 3703 | rec.data_offset = 16; |
| 3704 | /* Make sure to vary the length to exercise different |
| 3705 | * paddings. */ |
| 3706 | rec.data_len = 1 + num_records; |
| 3707 | |
| 3708 | memset( rec.buf + rec.data_offset, 42, rec.data_len ); |
| 3709 | |
| 3710 | /* Make a copy for later comparison */ |
| 3711 | rec_backup = rec; |
| 3712 | |
| 3713 | /* Encrypt record */ |
| 3714 | ret = mbedtls_ssl_encrypt_buf( &ssl, t_enc, &rec, |
Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 3715 | mbedtls_test_rnd_std_rand, NULL ); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3716 | TEST_ASSERT( ret == 0 || ret == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 3717 | if( ret != 0 ) |
| 3718 | { |
| 3719 | continue; |
| 3720 | } |
| 3721 | |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 3722 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 3723 | if( rec.cid_len != 0 ) |
| 3724 | { |
| 3725 | /* DTLS 1.2 + CID hides the real content type and |
| 3726 | * uses a special CID content type in the protected |
| 3727 | * record. Double-check this. */ |
| 3728 | TEST_ASSERT( rec.type == MBEDTLS_SSL_MSG_CID ); |
| 3729 | } |
| 3730 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 3731 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3732 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 3733 | if( t_enc->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 ) |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 3734 | { |
| 3735 | /* TLS 1.3 hides the real content type and |
| 3736 | * always uses Application Data as the content type |
| 3737 | * for protected records. Double-check this. */ |
| 3738 | TEST_ASSERT( rec.type == MBEDTLS_SSL_MSG_APPLICATION_DATA ); |
| 3739 | } |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3740 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 3741 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3742 | /* Decrypt record with t_dec */ |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 3743 | ret = mbedtls_ssl_decrypt_buf( &ssl, t_dec, &rec ); |
| 3744 | TEST_ASSERT( ret == 0 ); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3745 | |
| 3746 | /* Compare results */ |
| 3747 | TEST_ASSERT( rec.type == rec_backup.type ); |
| 3748 | TEST_ASSERT( memcmp( rec.ctr, rec_backup.ctr, 8 ) == 0 ); |
| 3749 | TEST_ASSERT( rec.ver[0] == rec_backup.ver[0] ); |
| 3750 | TEST_ASSERT( rec.ver[1] == rec_backup.ver[1] ); |
| 3751 | TEST_ASSERT( rec.data_len == rec_backup.data_len ); |
| 3752 | TEST_ASSERT( rec.data_offset == rec_backup.data_offset ); |
| 3753 | TEST_ASSERT( memcmp( rec.buf + rec.data_offset, |
| 3754 | rec_backup.buf + rec_backup.data_offset, |
| 3755 | rec.data_len ) == 0 ); |
| 3756 | } |
| 3757 | |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 3758 | exit: |
| 3759 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3760 | /* Cleanup */ |
| 3761 | mbedtls_ssl_free( &ssl ); |
| 3762 | mbedtls_ssl_transform_free( &t0 ); |
| 3763 | mbedtls_ssl_transform_free( &t1 ); |
| 3764 | |
Hanno Becker | 3ee5421 | 2019-04-04 16:31:26 +0100 | [diff] [blame] | 3765 | mbedtls_free( buf ); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 3766 | USE_PSA_DONE( ); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3767 | } |
| 3768 | /* END_CASE */ |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3769 | |
| 3770 | /* BEGIN_CASE */ |
| 3771 | void ssl_crypt_record_small( int cipher_type, int hash_id, |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 3772 | int etm, int tag_mode, int ver, |
| 3773 | int cid0_len, int cid1_len ) |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3774 | { |
| 3775 | /* |
| 3776 | * Test pairs of encryption and decryption with an increasing |
| 3777 | * amount of space in the record buffer - in more detail: |
| 3778 | * 1) Try to encrypt with 0, 1, 2, ... bytes available |
| 3779 | * in front of the plaintext, and expect the encryption |
| 3780 | * to succeed starting from some offset. Always keep |
| 3781 | * enough space in the end of the buffer. |
| 3782 | * 2) Try to encrypt with 0, 1, 2, ... bytes available |
| 3783 | * at the end of the plaintext, and expect the encryption |
| 3784 | * to succeed starting from some offset. Always keep |
| 3785 | * enough space at the beginning of the buffer. |
| 3786 | * 3) Try to encrypt with 0, 1, 2, ... bytes available |
| 3787 | * both at the front and end of the plaintext, |
| 3788 | * and expect the encryption to succeed starting from |
| 3789 | * some offset. |
| 3790 | * |
| 3791 | * If encryption succeeds, check that decryption succeeds |
| 3792 | * and yields the original record. |
| 3793 | */ |
| 3794 | |
| 3795 | mbedtls_ssl_context ssl; /* ONLY for debugging */ |
| 3796 | |
| 3797 | mbedtls_ssl_transform t0, t1; |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 3798 | unsigned char *buf = NULL; |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 3799 | size_t const buflen = 256; |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3800 | mbedtls_record rec, rec_backup; |
| 3801 | |
| 3802 | int ret; |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 3803 | int mode; /* Mode 1, 2 or 3 as explained above */ |
| 3804 | size_t offset; /* Available space at beginning/end/both */ |
| 3805 | size_t threshold = 96; /* Maximum offset to test against */ |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3806 | |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 3807 | size_t default_pre_padding = 64; /* Pre-padding to use in mode 2 */ |
| 3808 | 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] | 3809 | |
| 3810 | int seen_success; /* Indicates if in the current mode we've |
| 3811 | * already seen a successful test. */ |
| 3812 | |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 3813 | USE_PSA_INIT( ); |
| 3814 | |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3815 | mbedtls_ssl_init( &ssl ); |
| 3816 | mbedtls_ssl_transform_init( &t0 ); |
| 3817 | mbedtls_ssl_transform_init( &t1 ); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 3818 | ret = build_transforms( &t0, &t1, cipher_type, hash_id, |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 3819 | etm, tag_mode, ver, |
| 3820 | (size_t) cid0_len, |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 3821 | (size_t) cid1_len ); |
| 3822 | |
Przemyslaw Stekiel | 8c010eb | 2022-02-03 10:44:02 +0100 | [diff] [blame] | 3823 | TEST_ASSERT( ret == 0 ); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3824 | |
Hanno Becker | 3ee5421 | 2019-04-04 16:31:26 +0100 | [diff] [blame] | 3825 | TEST_ASSERT( ( buf = mbedtls_calloc( 1, buflen ) ) != NULL ); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3826 | |
| 3827 | for( mode=1; mode <= 3; mode++ ) |
| 3828 | { |
| 3829 | seen_success = 0; |
| 3830 | for( offset=0; offset <= threshold; offset++ ) |
| 3831 | { |
| 3832 | mbedtls_ssl_transform *t_dec, *t_enc; |
Hanno Becker | 6c87b3f | 2019-04-29 17:24:44 +0100 | [diff] [blame] | 3833 | t_dec = &t0; |
| 3834 | t_enc = &t1; |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3835 | |
| 3836 | memset( rec.ctr, offset, sizeof( rec.ctr ) ); |
| 3837 | rec.type = 42; |
| 3838 | rec.ver[0] = offset; |
| 3839 | rec.ver[1] = offset; |
| 3840 | rec.buf = buf; |
| 3841 | rec.buf_len = buflen; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3842 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 3843 | rec.cid_len = 0; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3844 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3845 | |
| 3846 | switch( mode ) |
| 3847 | { |
| 3848 | case 1: /* Space in the beginning */ |
| 3849 | rec.data_offset = offset; |
| 3850 | rec.data_len = buflen - offset - default_post_padding; |
| 3851 | break; |
| 3852 | |
| 3853 | case 2: /* Space in the end */ |
| 3854 | rec.data_offset = default_pre_padding; |
| 3855 | rec.data_len = buflen - default_pre_padding - offset; |
| 3856 | break; |
| 3857 | |
| 3858 | case 3: /* Space in the beginning and end */ |
| 3859 | rec.data_offset = offset; |
| 3860 | rec.data_len = buflen - 2 * offset; |
| 3861 | break; |
| 3862 | |
| 3863 | default: |
| 3864 | TEST_ASSERT( 0 ); |
| 3865 | break; |
| 3866 | } |
| 3867 | |
| 3868 | memset( rec.buf + rec.data_offset, 42, rec.data_len ); |
| 3869 | |
| 3870 | /* Make a copy for later comparison */ |
| 3871 | rec_backup = rec; |
| 3872 | |
| 3873 | /* Encrypt record */ |
Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 3874 | ret = mbedtls_ssl_encrypt_buf( &ssl, t_enc, &rec, |
| 3875 | mbedtls_test_rnd_std_rand, NULL ); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3876 | |
| 3877 | if( ( mode == 1 || mode == 2 ) && seen_success ) |
| 3878 | { |
| 3879 | TEST_ASSERT( ret == 0 ); |
| 3880 | } |
| 3881 | else |
| 3882 | { |
| 3883 | TEST_ASSERT( ret == 0 || ret == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 3884 | if( ret == 0 ) |
| 3885 | seen_success = 1; |
| 3886 | } |
| 3887 | |
| 3888 | if( ret != 0 ) |
| 3889 | continue; |
| 3890 | |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 3891 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 3892 | if( rec.cid_len != 0 ) |
| 3893 | { |
| 3894 | /* DTLS 1.2 + CID hides the real content type and |
| 3895 | * uses a special CID content type in the protected |
| 3896 | * record. Double-check this. */ |
| 3897 | TEST_ASSERT( rec.type == MBEDTLS_SSL_MSG_CID ); |
| 3898 | } |
| 3899 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 3900 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3901 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 3902 | if( t_enc->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 ) |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 3903 | { |
| 3904 | /* TLS 1.3 hides the real content type and |
| 3905 | * always uses Application Data as the content type |
| 3906 | * for protected records. Double-check this. */ |
| 3907 | TEST_ASSERT( rec.type == MBEDTLS_SSL_MSG_APPLICATION_DATA ); |
| 3908 | } |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3909 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 3910 | |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3911 | /* Decrypt record with t_dec */ |
| 3912 | TEST_ASSERT( mbedtls_ssl_decrypt_buf( &ssl, t_dec, &rec ) == 0 ); |
| 3913 | |
| 3914 | /* Compare results */ |
| 3915 | TEST_ASSERT( rec.type == rec_backup.type ); |
| 3916 | TEST_ASSERT( memcmp( rec.ctr, rec_backup.ctr, 8 ) == 0 ); |
| 3917 | TEST_ASSERT( rec.ver[0] == rec_backup.ver[0] ); |
| 3918 | TEST_ASSERT( rec.ver[1] == rec_backup.ver[1] ); |
| 3919 | TEST_ASSERT( rec.data_len == rec_backup.data_len ); |
| 3920 | TEST_ASSERT( rec.data_offset == rec_backup.data_offset ); |
| 3921 | TEST_ASSERT( memcmp( rec.buf + rec.data_offset, |
| 3922 | rec_backup.buf + rec_backup.data_offset, |
| 3923 | rec.data_len ) == 0 ); |
| 3924 | } |
| 3925 | |
| 3926 | TEST_ASSERT( seen_success == 1 ); |
| 3927 | } |
| 3928 | |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 3929 | exit: |
| 3930 | |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3931 | /* Cleanup */ |
| 3932 | mbedtls_ssl_free( &ssl ); |
| 3933 | mbedtls_ssl_transform_free( &t0 ); |
| 3934 | mbedtls_ssl_transform_free( &t1 ); |
| 3935 | |
Hanno Becker | 3ee5421 | 2019-04-04 16:31:26 +0100 | [diff] [blame] | 3936 | mbedtls_free( buf ); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 3937 | USE_PSA_DONE( ); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3938 | } |
| 3939 | /* END_CASE */ |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 3940 | |
Przemyslaw Stekiel | f4facef | 2022-02-02 21:31:04 +0100 | [diff] [blame] | 3941 | /* 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] | 3942 | 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] | 3943 | int length_selector ) |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3944 | { |
| 3945 | /* |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3946 | * Test record decryption for CBC without EtM, focused on the verification |
| 3947 | * of padding and MAC. |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3948 | * |
TRodziewicz | 299510e | 2021-07-09 16:55:11 +0200 | [diff] [blame] | 3949 | * Actually depends on TLS 1.2 and either AES, ARIA or Camellia, but since |
| 3950 | * the test framework doesn't support alternation in dependency statements, |
| 3951 | * just depend on AES. |
Manuel Pégourié-Gonnard | 864abbf | 2020-07-21 10:37:14 +0200 | [diff] [blame] | 3952 | * |
| 3953 | * The length_selector argument is interpreted as follows: |
| 3954 | * - if it's -1, the plaintext length is 0 and minimal padding is applied |
| 3955 | * - if it's -2, the plaintext length is 0 and maximal padding is applied |
| 3956 | * - otherwise it must be in [0, 255] and is padding_length from RFC 5246: |
| 3957 | * it's the length of the rest of the padding, that is, excluding the |
| 3958 | * byte that encodes the length. The minimal non-zero plaintext length |
| 3959 | * that gives this padding_length is automatically selected. |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3960 | */ |
| 3961 | mbedtls_ssl_context ssl; /* ONLY for debugging */ |
| 3962 | mbedtls_ssl_transform t0, t1; |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3963 | mbedtls_record rec, rec_save; |
| 3964 | unsigned char *buf = NULL, *buf_save = NULL; |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3965 | size_t buflen, olen = 0; |
Manuel Pégourié-Gonnard | 864abbf | 2020-07-21 10:37:14 +0200 | [diff] [blame] | 3966 | size_t plaintext_len, block_size, i; |
Manuel Pégourié-Gonnard | e55653f | 2020-07-22 11:42:57 +0200 | [diff] [blame] | 3967 | unsigned char padlen; /* excluding the padding_length byte */ |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3968 | unsigned char add_data[13]; |
| 3969 | unsigned char mac[MBEDTLS_MD_MAX_SIZE]; |
Neil Armstrong | cf8841a | 2022-02-24 11:17:45 +0100 | [diff] [blame] | 3970 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 3971 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 3972 | size_t sign_mac_length = 0; |
| 3973 | #endif |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3974 | int exp_ret; |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 3975 | int ret; |
Manuel Pégourié-Gonnard | 4adc04a | 2020-07-16 10:00:48 +0200 | [diff] [blame] | 3976 | const unsigned char pad_max_len = 255; /* Per the standard */ |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3977 | |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 3978 | USE_PSA_INIT( ); |
| 3979 | |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3980 | mbedtls_ssl_init( &ssl ); |
| 3981 | mbedtls_ssl_transform_init( &t0 ); |
| 3982 | mbedtls_ssl_transform_init( &t1 ); |
| 3983 | |
| 3984 | /* Set up transforms with dummy keys */ |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 3985 | ret = build_transforms( &t0, &t1, cipher_type, hash_id, |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3986 | 0, trunc_hmac, |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 3987 | MBEDTLS_SSL_VERSION_TLS1_2, |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 3988 | 0 , 0 ); |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3989 | |
Przemyslaw Stekiel | 4a36dd3 | 2022-01-25 00:43:58 +0100 | [diff] [blame] | 3990 | TEST_ASSERT( ret == 0 ); |
| 3991 | |
Manuel Pégourié-Gonnard | 864abbf | 2020-07-21 10:37:14 +0200 | [diff] [blame] | 3992 | /* Determine padding/plaintext length */ |
| 3993 | TEST_ASSERT( length_selector >= -2 && length_selector <= 255 ); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3994 | block_size = t0.ivlen; |
Manuel Pégourié-Gonnard | 864abbf | 2020-07-21 10:37:14 +0200 | [diff] [blame] | 3995 | if( length_selector < 0 ) |
| 3996 | { |
| 3997 | plaintext_len = 0; |
| 3998 | |
Manuel Pégourié-Gonnard | e55653f | 2020-07-22 11:42:57 +0200 | [diff] [blame] | 3999 | /* Minimal padding |
| 4000 | * 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] | 4001 | padlen = block_size - ( t0.maclen + 1 ) % block_size; |
| 4002 | |
| 4003 | /* Maximal padding? */ |
| 4004 | if( length_selector == -2 ) |
| 4005 | padlen += block_size * ( ( pad_max_len - padlen ) / block_size ); |
| 4006 | } |
| 4007 | else |
| 4008 | { |
| 4009 | padlen = length_selector; |
| 4010 | |
Manuel Pégourié-Gonnard | e55653f | 2020-07-22 11:42:57 +0200 | [diff] [blame] | 4011 | /* Minimal non-zero plaintext_length giving desired padding. |
| 4012 | * 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] | 4013 | plaintext_len = block_size - ( padlen + t0.maclen + 1 ) % block_size; |
| 4014 | } |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 4015 | |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 4016 | /* Prepare a buffer for record data */ |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 4017 | buflen = block_size |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 4018 | + plaintext_len |
| 4019 | + t0.maclen |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 4020 | + padlen + 1; |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 4021 | ASSERT_ALLOC( buf, buflen ); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 4022 | ASSERT_ALLOC( buf_save, buflen ); |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 4023 | |
| 4024 | /* Prepare a dummy record header */ |
| 4025 | memset( rec.ctr, 0, sizeof( rec.ctr ) ); |
| 4026 | rec.type = MBEDTLS_SSL_MSG_APPLICATION_DATA; |
Glenn Strauss | e3af4cb | 2022-03-15 03:23:42 -0400 | [diff] [blame] | 4027 | mbedtls_ssl_write_version( rec.ver, MBEDTLS_SSL_TRANSPORT_STREAM, |
| 4028 | MBEDTLS_SSL_VERSION_TLS1_2 ); |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 4029 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 4030 | rec.cid_len = 0; |
| 4031 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 4032 | |
| 4033 | /* Prepare dummy record content */ |
| 4034 | rec.buf = buf; |
| 4035 | rec.buf_len = buflen; |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 4036 | rec.data_offset = block_size; |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 4037 | rec.data_len = plaintext_len; |
| 4038 | memset( rec.buf + rec.data_offset, 42, rec.data_len ); |
| 4039 | |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 4040 | /* Serialized version of record header for MAC purposes */ |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 4041 | memcpy( add_data, rec.ctr, 8 ); |
| 4042 | add_data[8] = rec.type; |
| 4043 | add_data[9] = rec.ver[0]; |
| 4044 | add_data[10] = rec.ver[1]; |
| 4045 | add_data[11] = ( rec.data_len >> 8 ) & 0xff; |
| 4046 | add_data[12] = ( rec.data_len >> 0 ) & 0xff; |
| 4047 | |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 4048 | /* Set dummy IV */ |
| 4049 | memset( t0.iv_enc, 0x55, t0.ivlen ); |
| 4050 | memcpy( rec.buf, t0.iv_enc, t0.ivlen ); |
| 4051 | |
| 4052 | /* |
| 4053 | * Prepare a pre-encryption record (with MAC and padding), and save it. |
| 4054 | */ |
| 4055 | |
| 4056 | /* MAC with additional data */ |
Neil Armstrong | cf8841a | 2022-02-24 11:17:45 +0100 | [diff] [blame] | 4057 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 4058 | TEST_EQUAL( PSA_SUCCESS, psa_mac_sign_setup( &operation, |
| 4059 | t0.psa_mac_enc, |
| 4060 | t0.psa_mac_alg ) ); |
| 4061 | TEST_EQUAL( PSA_SUCCESS, psa_mac_update( &operation, add_data, 13 ) ); |
| 4062 | TEST_EQUAL( PSA_SUCCESS, psa_mac_update( &operation, |
| 4063 | rec.buf + rec.data_offset, |
| 4064 | rec.data_len ) ); |
| 4065 | TEST_EQUAL( PSA_SUCCESS, psa_mac_sign_finish( &operation, |
| 4066 | mac, MBEDTLS_MD_MAX_SIZE, |
| 4067 | &sign_mac_length ) ); |
| 4068 | #else |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 4069 | TEST_EQUAL( 0, mbedtls_md_hmac_update( &t0.md_ctx_enc, add_data, 13 ) ); |
| 4070 | TEST_EQUAL( 0, mbedtls_md_hmac_update( &t0.md_ctx_enc, |
| 4071 | rec.buf + rec.data_offset, |
| 4072 | rec.data_len ) ); |
| 4073 | TEST_EQUAL( 0, mbedtls_md_hmac_finish( &t0.md_ctx_enc, mac ) ); |
Neil Armstrong | cf8841a | 2022-02-24 11:17:45 +0100 | [diff] [blame] | 4074 | #endif |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 4075 | |
| 4076 | memcpy( rec.buf + rec.data_offset + rec.data_len, mac, t0.maclen ); |
| 4077 | rec.data_len += t0.maclen; |
| 4078 | |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 4079 | /* Pad */ |
| 4080 | memset( rec.buf + rec.data_offset + rec.data_len, padlen, padlen + 1 ); |
| 4081 | rec.data_len += padlen + 1; |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 4082 | |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 4083 | /* Save correct pre-encryption record */ |
| 4084 | rec_save = rec; |
| 4085 | rec_save.buf = buf_save; |
| 4086 | memcpy( buf_save, buf, buflen ); |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 4087 | |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 4088 | /* |
| 4089 | * Encrypt and decrypt the correct record, expecting success |
| 4090 | */ |
Przemyslaw Stekiel | 8c010eb | 2022-02-03 10:44:02 +0100 | [diff] [blame] | 4091 | 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] | 4092 | rec.buf + rec.data_offset, rec.data_len, |
| 4093 | rec.buf + rec.data_offset, &olen ) ); |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 4094 | rec.data_offset -= t0.ivlen; |
| 4095 | rec.data_len += t0.ivlen; |
| 4096 | |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 4097 | TEST_EQUAL( 0, mbedtls_ssl_decrypt_buf( &ssl, &t1, &rec ) ); |
| 4098 | |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 4099 | /* |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 4100 | * Modify each byte of the pre-encryption record before encrypting and |
| 4101 | * decrypting it, expecting failure every time. |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 4102 | */ |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 4103 | for( i = block_size; i < buflen; i++ ) |
| 4104 | { |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 4105 | mbedtls_test_set_step( i ); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 4106 | |
| 4107 | /* Restore correct pre-encryption record */ |
| 4108 | rec = rec_save; |
| 4109 | rec.buf = buf; |
| 4110 | memcpy( buf, buf_save, buflen ); |
| 4111 | |
Manuel Pégourié-Gonnard | b51f044 | 2020-07-21 10:40:25 +0200 | [diff] [blame] | 4112 | /* 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] | 4113 | rec.buf[i] ^= 0x01; |
| 4114 | |
| 4115 | /* Encrypt */ |
Przemyslaw Stekiel | 8c010eb | 2022-02-03 10:44:02 +0100 | [diff] [blame] | 4116 | 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] | 4117 | rec.buf + rec.data_offset, rec.data_len, |
| 4118 | rec.buf + rec.data_offset, &olen ) ); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 4119 | rec.data_offset -= t0.ivlen; |
| 4120 | rec.data_len += t0.ivlen; |
| 4121 | |
| 4122 | /* Decrypt and expect failure */ |
| 4123 | TEST_EQUAL( MBEDTLS_ERR_SSL_INVALID_MAC, |
| 4124 | mbedtls_ssl_decrypt_buf( &ssl, &t1, &rec ) ); |
| 4125 | } |
| 4126 | |
| 4127 | /* |
| 4128 | * Use larger values of the padding bytes - with small buffers, this tests |
| 4129 | * the case where the announced padlen would be larger than the buffer |
| 4130 | * (and before that, than the buffer minus the size of the MAC), to make |
| 4131 | * sure our padding checking code does not perform any out-of-bounds reads |
| 4132 | * in this case. (With larger buffers, ie when the plaintext is long or |
| 4133 | * maximal length padding is used, this is less relevant but still doesn't |
| 4134 | * hurt to test.) |
| 4135 | * |
| 4136 | * (Start the loop with correct padding, just to double-check that record |
| 4137 | * saving did work, and that we're overwriting the correct bytes.) |
| 4138 | */ |
Manuel Pégourié-Gonnard | 4adc04a | 2020-07-16 10:00:48 +0200 | [diff] [blame] | 4139 | for( i = padlen; i <= pad_max_len; i++ ) |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 4140 | { |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 4141 | mbedtls_test_set_step( i ); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 4142 | |
| 4143 | /* Restore correct pre-encryption record */ |
| 4144 | rec = rec_save; |
| 4145 | rec.buf = buf; |
| 4146 | memcpy( buf, buf_save, buflen ); |
| 4147 | |
| 4148 | /* Set padding bytes to new value */ |
| 4149 | memset( buf + buflen - padlen - 1, i, padlen + 1 ); |
| 4150 | |
| 4151 | /* Encrypt */ |
Przemyslaw Stekiel | 8c010eb | 2022-02-03 10:44:02 +0100 | [diff] [blame] | 4152 | 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] | 4153 | rec.buf + rec.data_offset, rec.data_len, |
| 4154 | rec.buf + rec.data_offset, &olen ) ); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 4155 | rec.data_offset -= t0.ivlen; |
| 4156 | rec.data_len += t0.ivlen; |
| 4157 | |
| 4158 | /* Decrypt and expect failure except the first time */ |
| 4159 | exp_ret = ( i == padlen ) ? 0 : MBEDTLS_ERR_SSL_INVALID_MAC; |
| 4160 | TEST_EQUAL( exp_ret, mbedtls_ssl_decrypt_buf( &ssl, &t1, &rec ) ); |
| 4161 | } |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 4162 | |
| 4163 | exit: |
| 4164 | mbedtls_ssl_free( &ssl ); |
| 4165 | mbedtls_ssl_transform_free( &t0 ); |
| 4166 | mbedtls_ssl_transform_free( &t1 ); |
| 4167 | mbedtls_free( buf ); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 4168 | mbedtls_free( buf_save ); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 4169 | USE_PSA_DONE( ); |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 4170 | } |
| 4171 | /* END_CASE */ |
| 4172 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4173 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */ |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4174 | void ssl_tls13_hkdf_expand_label( int hash_alg, |
| 4175 | data_t *secret, |
| 4176 | int label_idx, |
| 4177 | data_t *ctx, |
| 4178 | int desired_length, |
| 4179 | data_t *expected ) |
Hanno Becker | 39ff492 | 2020-08-21 13:36:56 +0100 | [diff] [blame] | 4180 | { |
| 4181 | unsigned char dst[ 100 ]; |
| 4182 | |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 4183 | unsigned char const *lbl = NULL; |
| 4184 | size_t lbl_len; |
Xiaofei Bai | d25fab6 | 2021-12-02 06:36:27 +0000 | [diff] [blame] | 4185 | #define MBEDTLS_SSL_TLS1_3_LABEL( name, string ) \ |
| 4186 | if( label_idx == (int) tls13_label_ ## name ) \ |
| 4187 | { \ |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4188 | lbl = mbedtls_ssl_tls13_labels.name; \ |
| 4189 | lbl_len = sizeof( mbedtls_ssl_tls13_labels.name ); \ |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 4190 | } |
| 4191 | MBEDTLS_SSL_TLS1_3_LABEL_LIST |
| 4192 | #undef MBEDTLS_SSL_TLS1_3_LABEL |
| 4193 | TEST_ASSERT( lbl != NULL ); |
Hanno Becker | 39ff492 | 2020-08-21 13:36:56 +0100 | [diff] [blame] | 4194 | |
| 4195 | /* Check sanity of test parameters. */ |
| 4196 | TEST_ASSERT( (size_t) desired_length <= sizeof(dst) ); |
| 4197 | TEST_ASSERT( (size_t) desired_length == expected->len ); |
| 4198 | |
Gabor Mezei | 5d9a1fe | 2022-03-24 17:49:14 +0100 | [diff] [blame] | 4199 | PSA_INIT( ); |
Gabor Mezei | 892c4aa | 2022-03-21 12:21:43 +0100 | [diff] [blame] | 4200 | |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4201 | TEST_ASSERT( mbedtls_ssl_tls13_hkdf_expand_label( |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 4202 | (psa_algorithm_t) hash_alg, |
Hanno Becker | 39ff492 | 2020-08-21 13:36:56 +0100 | [diff] [blame] | 4203 | secret->x, secret->len, |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 4204 | lbl, lbl_len, |
Hanno Becker | 39ff492 | 2020-08-21 13:36:56 +0100 | [diff] [blame] | 4205 | ctx->x, ctx->len, |
| 4206 | dst, desired_length ) == 0 ); |
| 4207 | |
Hanno Becker | fb08096 | 2020-09-08 10:58:42 +0100 | [diff] [blame] | 4208 | ASSERT_COMPARE( dst, (size_t) desired_length, |
| 4209 | expected->x, (size_t) expected->len ); |
Gabor Mezei | 892c4aa | 2022-03-21 12:21:43 +0100 | [diff] [blame] | 4210 | |
Gabor Mezei | 5d9a1fe | 2022-03-24 17:49:14 +0100 | [diff] [blame] | 4211 | PSA_DONE( ); |
Hanno Becker | 39ff492 | 2020-08-21 13:36:56 +0100 | [diff] [blame] | 4212 | } |
| 4213 | /* END_CASE */ |
| 4214 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4215 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */ |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4216 | void ssl_tls13_traffic_key_generation( int hash_alg, |
| 4217 | data_t *server_secret, |
| 4218 | data_t *client_secret, |
| 4219 | int desired_iv_len, |
| 4220 | int desired_key_len, |
| 4221 | data_t *expected_server_write_key, |
| 4222 | data_t *expected_server_write_iv, |
| 4223 | data_t *expected_client_write_key, |
| 4224 | data_t *expected_client_write_iv ) |
Hanno Becker | 19498f8 | 2020-08-21 13:37:08 +0100 | [diff] [blame] | 4225 | { |
| 4226 | mbedtls_ssl_key_set keys; |
| 4227 | |
| 4228 | /* Check sanity of test parameters. */ |
| 4229 | TEST_ASSERT( client_secret->len == server_secret->len ); |
| 4230 | TEST_ASSERT( expected_client_write_iv->len == expected_server_write_iv->len && |
| 4231 | expected_client_write_iv->len == (size_t) desired_iv_len ); |
| 4232 | TEST_ASSERT( expected_client_write_key->len == expected_server_write_key->len && |
| 4233 | expected_client_write_key->len == (size_t) desired_key_len ); |
| 4234 | |
Gabor Mezei | 5d9a1fe | 2022-03-24 17:49:14 +0100 | [diff] [blame] | 4235 | PSA_INIT( ); |
Gabor Mezei | 892c4aa | 2022-03-21 12:21:43 +0100 | [diff] [blame] | 4236 | |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4237 | TEST_ASSERT( mbedtls_ssl_tls13_make_traffic_keys( |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 4238 | (psa_algorithm_t) hash_alg, |
Hanno Becker | 19498f8 | 2020-08-21 13:37:08 +0100 | [diff] [blame] | 4239 | client_secret->x, |
| 4240 | server_secret->x, |
| 4241 | client_secret->len /* == server_secret->len */, |
| 4242 | desired_key_len, desired_iv_len, |
| 4243 | &keys ) == 0 ); |
| 4244 | |
Hanno Becker | fb08096 | 2020-09-08 10:58:42 +0100 | [diff] [blame] | 4245 | ASSERT_COMPARE( keys.client_write_key, |
Hanno Becker | 493ea7f | 2020-09-08 11:01:00 +0100 | [diff] [blame] | 4246 | keys.key_len, |
Hanno Becker | fb08096 | 2020-09-08 10:58:42 +0100 | [diff] [blame] | 4247 | expected_client_write_key->x, |
| 4248 | (size_t) desired_key_len ); |
| 4249 | ASSERT_COMPARE( keys.server_write_key, |
Hanno Becker | 493ea7f | 2020-09-08 11:01:00 +0100 | [diff] [blame] | 4250 | keys.key_len, |
Hanno Becker | fb08096 | 2020-09-08 10:58:42 +0100 | [diff] [blame] | 4251 | expected_server_write_key->x, |
| 4252 | (size_t) desired_key_len ); |
| 4253 | ASSERT_COMPARE( keys.client_write_iv, |
Hanno Becker | 493ea7f | 2020-09-08 11:01:00 +0100 | [diff] [blame] | 4254 | keys.iv_len, |
Hanno Becker | fb08096 | 2020-09-08 10:58:42 +0100 | [diff] [blame] | 4255 | expected_client_write_iv->x, |
| 4256 | (size_t) desired_iv_len ); |
| 4257 | ASSERT_COMPARE( keys.server_write_iv, |
Hanno Becker | 493ea7f | 2020-09-08 11:01:00 +0100 | [diff] [blame] | 4258 | keys.iv_len, |
Hanno Becker | fb08096 | 2020-09-08 10:58:42 +0100 | [diff] [blame] | 4259 | expected_server_write_iv->x, |
| 4260 | (size_t) desired_iv_len ); |
Gabor Mezei | 892c4aa | 2022-03-21 12:21:43 +0100 | [diff] [blame] | 4261 | |
Gabor Mezei | 5d9a1fe | 2022-03-24 17:49:14 +0100 | [diff] [blame] | 4262 | PSA_DONE( ); |
Hanno Becker | 19498f8 | 2020-08-21 13:37:08 +0100 | [diff] [blame] | 4263 | } |
| 4264 | /* END_CASE */ |
| 4265 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4266 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */ |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4267 | void ssl_tls13_derive_secret( int hash_alg, |
| 4268 | data_t *secret, |
| 4269 | int label_idx, |
| 4270 | data_t *ctx, |
| 4271 | int desired_length, |
| 4272 | int already_hashed, |
| 4273 | data_t *expected ) |
Hanno Becker | e4849d1 | 2020-08-21 14:14:14 +0100 | [diff] [blame] | 4274 | { |
| 4275 | unsigned char dst[ 100 ]; |
| 4276 | |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 4277 | unsigned char const *lbl = NULL; |
| 4278 | size_t lbl_len; |
Xiaofei Bai | d25fab6 | 2021-12-02 06:36:27 +0000 | [diff] [blame] | 4279 | #define MBEDTLS_SSL_TLS1_3_LABEL( name, string ) \ |
| 4280 | if( label_idx == (int) tls13_label_ ## name ) \ |
| 4281 | { \ |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4282 | lbl = mbedtls_ssl_tls13_labels.name; \ |
| 4283 | lbl_len = sizeof( mbedtls_ssl_tls13_labels.name ); \ |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 4284 | } |
| 4285 | MBEDTLS_SSL_TLS1_3_LABEL_LIST |
| 4286 | #undef MBEDTLS_SSL_TLS1_3_LABEL |
| 4287 | TEST_ASSERT( lbl != NULL ); |
| 4288 | |
Hanno Becker | e4849d1 | 2020-08-21 14:14:14 +0100 | [diff] [blame] | 4289 | /* Check sanity of test parameters. */ |
| 4290 | TEST_ASSERT( (size_t) desired_length <= sizeof(dst) ); |
| 4291 | TEST_ASSERT( (size_t) desired_length == expected->len ); |
| 4292 | |
Gabor Mezei | 5d9a1fe | 2022-03-24 17:49:14 +0100 | [diff] [blame] | 4293 | PSA_INIT( ); |
Gabor Mezei | 892c4aa | 2022-03-21 12:21:43 +0100 | [diff] [blame] | 4294 | |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4295 | TEST_ASSERT( mbedtls_ssl_tls13_derive_secret( |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 4296 | (psa_algorithm_t) hash_alg, |
Hanno Becker | e4849d1 | 2020-08-21 14:14:14 +0100 | [diff] [blame] | 4297 | secret->x, secret->len, |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 4298 | lbl, lbl_len, |
Hanno Becker | e4849d1 | 2020-08-21 14:14:14 +0100 | [diff] [blame] | 4299 | ctx->x, ctx->len, |
| 4300 | already_hashed, |
| 4301 | dst, desired_length ) == 0 ); |
| 4302 | |
Hanno Becker | fb08096 | 2020-09-08 10:58:42 +0100 | [diff] [blame] | 4303 | ASSERT_COMPARE( dst, desired_length, |
| 4304 | expected->x, desired_length ); |
Gabor Mezei | 892c4aa | 2022-03-21 12:21:43 +0100 | [diff] [blame] | 4305 | |
Gabor Mezei | 5d9a1fe | 2022-03-24 17:49:14 +0100 | [diff] [blame] | 4306 | PSA_DONE( ); |
Hanno Becker | e4849d1 | 2020-08-21 14:14:14 +0100 | [diff] [blame] | 4307 | } |
| 4308 | /* END_CASE */ |
| 4309 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4310 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */ |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4311 | void ssl_tls13_derive_early_secrets( int hash_alg, |
| 4312 | data_t *secret, |
| 4313 | data_t *transcript, |
| 4314 | data_t *traffic_expected, |
| 4315 | data_t *exporter_expected ) |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4316 | { |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4317 | mbedtls_ssl_tls13_early_secrets secrets; |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4318 | |
| 4319 | /* Double-check that we've passed sane parameters. */ |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 4320 | psa_algorithm_t alg = (psa_algorithm_t) hash_alg; |
| 4321 | size_t const hash_len = PSA_HASH_LENGTH( alg ); |
| 4322 | TEST_ASSERT( PSA_ALG_IS_HASH( alg ) && |
| 4323 | secret->len == hash_len && |
| 4324 | transcript->len == hash_len && |
| 4325 | traffic_expected->len == hash_len && |
| 4326 | exporter_expected->len == hash_len ); |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4327 | |
Gabor Mezei | 5d9a1fe | 2022-03-24 17:49:14 +0100 | [diff] [blame] | 4328 | PSA_INIT( ); |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4329 | |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4330 | TEST_ASSERT( mbedtls_ssl_tls13_derive_early_secrets( |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 4331 | alg, secret->x, transcript->x, transcript->len, |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4332 | &secrets ) == 0 ); |
| 4333 | |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 4334 | ASSERT_COMPARE( secrets.client_early_traffic_secret, hash_len, |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4335 | traffic_expected->x, traffic_expected->len ); |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 4336 | ASSERT_COMPARE( secrets.early_exporter_master_secret, hash_len, |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4337 | exporter_expected->x, exporter_expected->len ); |
Gabor Mezei | 892c4aa | 2022-03-21 12:21:43 +0100 | [diff] [blame] | 4338 | |
Gabor Mezei | 5d9a1fe | 2022-03-24 17:49:14 +0100 | [diff] [blame] | 4339 | PSA_DONE( ); |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4340 | } |
| 4341 | /* END_CASE */ |
| 4342 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4343 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */ |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4344 | void ssl_tls13_derive_handshake_secrets( int hash_alg, |
| 4345 | data_t *secret, |
| 4346 | data_t *transcript, |
| 4347 | data_t *client_expected, |
| 4348 | data_t *server_expected ) |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4349 | { |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4350 | mbedtls_ssl_tls13_handshake_secrets secrets; |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4351 | |
| 4352 | /* Double-check that we've passed sane parameters. */ |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 4353 | psa_algorithm_t alg = (psa_algorithm_t) hash_alg; |
| 4354 | size_t const hash_len = PSA_HASH_LENGTH( alg ); |
| 4355 | TEST_ASSERT( PSA_ALG_IS_HASH( alg ) && |
| 4356 | secret->len == hash_len && |
| 4357 | transcript->len == hash_len && |
| 4358 | client_expected->len == hash_len && |
| 4359 | server_expected->len == hash_len ); |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4360 | |
Gabor Mezei | 5d9a1fe | 2022-03-24 17:49:14 +0100 | [diff] [blame] | 4361 | PSA_INIT( ); |
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 | TEST_ASSERT( mbedtls_ssl_tls13_derive_handshake_secrets( |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 4364 | alg, secret->x, transcript->x, transcript->len, |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4365 | &secrets ) == 0 ); |
| 4366 | |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 4367 | ASSERT_COMPARE( secrets.client_handshake_traffic_secret, hash_len, |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4368 | client_expected->x, client_expected->len ); |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 4369 | ASSERT_COMPARE( secrets.server_handshake_traffic_secret, hash_len, |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4370 | server_expected->x, server_expected->len ); |
Gabor Mezei | 892c4aa | 2022-03-21 12:21:43 +0100 | [diff] [blame] | 4371 | |
Gabor Mezei | 5d9a1fe | 2022-03-24 17:49:14 +0100 | [diff] [blame] | 4372 | PSA_DONE( ); |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4373 | } |
| 4374 | /* END_CASE */ |
| 4375 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4376 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */ |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4377 | void ssl_tls13_derive_application_secrets( int hash_alg, |
| 4378 | data_t *secret, |
| 4379 | data_t *transcript, |
| 4380 | data_t *client_expected, |
| 4381 | data_t *server_expected, |
| 4382 | data_t *exporter_expected ) |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4383 | { |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4384 | mbedtls_ssl_tls13_application_secrets secrets; |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4385 | |
| 4386 | /* Double-check that we've passed sane parameters. */ |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 4387 | psa_algorithm_t alg = (psa_algorithm_t) hash_alg; |
| 4388 | size_t const hash_len = PSA_HASH_LENGTH( alg ); |
| 4389 | TEST_ASSERT( PSA_ALG_IS_HASH( alg ) && |
| 4390 | secret->len == hash_len && |
| 4391 | transcript->len == hash_len && |
| 4392 | client_expected->len == hash_len && |
| 4393 | server_expected->len == hash_len && |
| 4394 | exporter_expected->len == hash_len ); |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4395 | |
Gabor Mezei | 5d9a1fe | 2022-03-24 17:49:14 +0100 | [diff] [blame] | 4396 | PSA_INIT( ); |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4397 | |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4398 | TEST_ASSERT( mbedtls_ssl_tls13_derive_application_secrets( |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 4399 | alg, secret->x, transcript->x, transcript->len, |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4400 | &secrets ) == 0 ); |
| 4401 | |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 4402 | ASSERT_COMPARE( secrets.client_application_traffic_secret_N, hash_len, |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4403 | client_expected->x, client_expected->len ); |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 4404 | ASSERT_COMPARE( secrets.server_application_traffic_secret_N, hash_len, |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4405 | server_expected->x, server_expected->len ); |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 4406 | ASSERT_COMPARE( secrets.exporter_master_secret, hash_len, |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4407 | exporter_expected->x, exporter_expected->len ); |
Gabor Mezei | 892c4aa | 2022-03-21 12:21:43 +0100 | [diff] [blame] | 4408 | |
Gabor Mezei | 5d9a1fe | 2022-03-24 17:49:14 +0100 | [diff] [blame] | 4409 | PSA_DONE( ); |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4410 | } |
| 4411 | /* END_CASE */ |
| 4412 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4413 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */ |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4414 | void ssl_tls13_derive_resumption_secrets( int hash_alg, |
| 4415 | data_t *secret, |
| 4416 | data_t *transcript, |
| 4417 | data_t *resumption_expected ) |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4418 | { |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4419 | mbedtls_ssl_tls13_application_secrets secrets; |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4420 | |
| 4421 | /* Double-check that we've passed sane parameters. */ |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 4422 | psa_algorithm_t alg = (psa_algorithm_t) hash_alg; |
| 4423 | size_t const hash_len = PSA_HASH_LENGTH( alg ); |
| 4424 | TEST_ASSERT( PSA_ALG_IS_HASH( alg ) && |
| 4425 | secret->len == hash_len && |
| 4426 | transcript->len == hash_len && |
| 4427 | resumption_expected->len == hash_len ); |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4428 | |
Gabor Mezei | 5d9a1fe | 2022-03-24 17:49:14 +0100 | [diff] [blame] | 4429 | PSA_INIT( ); |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4430 | |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4431 | TEST_ASSERT( mbedtls_ssl_tls13_derive_resumption_master_secret( |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 4432 | alg, secret->x, transcript->x, transcript->len, |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4433 | &secrets ) == 0 ); |
| 4434 | |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 4435 | ASSERT_COMPARE( secrets.resumption_master_secret, hash_len, |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4436 | resumption_expected->x, resumption_expected->len ); |
Gabor Mezei | 892c4aa | 2022-03-21 12:21:43 +0100 | [diff] [blame] | 4437 | |
Gabor Mezei | 5d9a1fe | 2022-03-24 17:49:14 +0100 | [diff] [blame] | 4438 | PSA_DONE( ); |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4439 | } |
| 4440 | /* END_CASE */ |
| 4441 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4442 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */ |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4443 | void ssl_tls13_create_psk_binder( int hash_alg, |
| 4444 | data_t *psk, |
| 4445 | int psk_type, |
| 4446 | data_t *transcript, |
| 4447 | data_t *binder_expected ) |
Hanno Becker | 55bc2c5 | 2021-05-24 06:53:52 +0100 | [diff] [blame] | 4448 | { |
| 4449 | unsigned char binder[ MBEDTLS_MD_MAX_SIZE ]; |
| 4450 | |
| 4451 | /* Double-check that we've passed sane parameters. */ |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 4452 | psa_algorithm_t alg = (psa_algorithm_t) hash_alg; |
| 4453 | size_t const hash_len = PSA_HASH_LENGTH( alg ); |
| 4454 | TEST_ASSERT( PSA_ALG_IS_HASH( alg ) && |
| 4455 | transcript->len == hash_len && |
| 4456 | binder_expected->len == hash_len ); |
Hanno Becker | 55bc2c5 | 2021-05-24 06:53:52 +0100 | [diff] [blame] | 4457 | |
Gabor Mezei | 5d9a1fe | 2022-03-24 17:49:14 +0100 | [diff] [blame] | 4458 | PSA_INIT( ); |
Hanno Becker | 55bc2c5 | 2021-05-24 06:53:52 +0100 | [diff] [blame] | 4459 | |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4460 | TEST_ASSERT( mbedtls_ssl_tls13_create_psk_binder( |
Hanno Becker | 55bc2c5 | 2021-05-24 06:53:52 +0100 | [diff] [blame] | 4461 | NULL, /* SSL context for debugging only */ |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 4462 | alg, |
Hanno Becker | 55bc2c5 | 2021-05-24 06:53:52 +0100 | [diff] [blame] | 4463 | psk->x, psk->len, |
| 4464 | psk_type, |
| 4465 | transcript->x, |
| 4466 | binder ) == 0 ); |
| 4467 | |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 4468 | ASSERT_COMPARE( binder, hash_len, |
Hanno Becker | 55bc2c5 | 2021-05-24 06:53:52 +0100 | [diff] [blame] | 4469 | binder_expected->x, binder_expected->len ); |
Gabor Mezei | 892c4aa | 2022-03-21 12:21:43 +0100 | [diff] [blame] | 4470 | |
Gabor Mezei | 5d9a1fe | 2022-03-24 17:49:14 +0100 | [diff] [blame] | 4471 | PSA_DONE( ); |
Hanno Becker | 55bc2c5 | 2021-05-24 06:53:52 +0100 | [diff] [blame] | 4472 | } |
| 4473 | /* END_CASE */ |
| 4474 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4475 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */ |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4476 | void ssl_tls13_record_protection( int ciphersuite, |
| 4477 | int endpoint, |
| 4478 | int ctr, |
| 4479 | int padding_used, |
| 4480 | data_t *server_write_key, |
| 4481 | data_t *server_write_iv, |
| 4482 | data_t *client_write_key, |
| 4483 | data_t *client_write_iv, |
| 4484 | data_t *plaintext, |
| 4485 | data_t *ciphertext ) |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4486 | { |
| 4487 | mbedtls_ssl_key_set keys; |
| 4488 | mbedtls_ssl_transform transform_send; |
| 4489 | mbedtls_ssl_transform transform_recv; |
| 4490 | mbedtls_record rec; |
| 4491 | unsigned char *buf = NULL; |
Hanno Becker | 1f91878 | 2021-08-01 19:18:28 +0100 | [diff] [blame] | 4492 | size_t buf_len; |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4493 | int other_endpoint; |
| 4494 | |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 4495 | USE_PSA_INIT( ); |
| 4496 | |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4497 | TEST_ASSERT( endpoint == MBEDTLS_SSL_IS_CLIENT || |
| 4498 | endpoint == MBEDTLS_SSL_IS_SERVER ); |
| 4499 | |
| 4500 | if( endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 4501 | other_endpoint = MBEDTLS_SSL_IS_CLIENT; |
| 4502 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 4503 | other_endpoint = MBEDTLS_SSL_IS_SERVER; |
| 4504 | |
| 4505 | TEST_ASSERT( server_write_key->len == client_write_key->len ); |
| 4506 | TEST_ASSERT( server_write_iv->len == client_write_iv->len ); |
| 4507 | |
| 4508 | memcpy( keys.client_write_key, |
| 4509 | client_write_key->x, client_write_key->len ); |
| 4510 | memcpy( keys.client_write_iv, |
| 4511 | client_write_iv->x, client_write_iv->len ); |
| 4512 | memcpy( keys.server_write_key, |
| 4513 | server_write_key->x, server_write_key->len ); |
| 4514 | memcpy( keys.server_write_iv, |
| 4515 | server_write_iv->x, server_write_iv->len ); |
| 4516 | |
| 4517 | keys.key_len = server_write_key->len; |
| 4518 | keys.iv_len = server_write_iv->len; |
| 4519 | |
| 4520 | mbedtls_ssl_transform_init( &transform_recv ); |
| 4521 | mbedtls_ssl_transform_init( &transform_send ); |
| 4522 | |
| 4523 | TEST_ASSERT( mbedtls_ssl_tls13_populate_transform( |
| 4524 | &transform_send, endpoint, |
| 4525 | ciphersuite, &keys, NULL ) == 0 ); |
| 4526 | TEST_ASSERT( mbedtls_ssl_tls13_populate_transform( |
| 4527 | &transform_recv, other_endpoint, |
| 4528 | ciphersuite, &keys, NULL ) == 0 ); |
| 4529 | |
Hanno Becker | 1f91878 | 2021-08-01 19:18:28 +0100 | [diff] [blame] | 4530 | /* Make sure we have enough space in the buffer even if |
| 4531 | * we use more padding than the KAT. */ |
| 4532 | buf_len = ciphertext->len + MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY; |
| 4533 | ASSERT_ALLOC( buf, buf_len ); |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4534 | rec.type = MBEDTLS_SSL_MSG_APPLICATION_DATA; |
Hanno Becker | 4153745 | 2021-04-20 05:35:28 +0100 | [diff] [blame] | 4535 | |
| 4536 | /* 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] | 4537 | mbedtls_ssl_write_version( rec.ver, |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4538 | MBEDTLS_SSL_TRANSPORT_STREAM, |
Glenn Strauss | e3af4cb | 2022-03-15 03:23:42 -0400 | [diff] [blame] | 4539 | MBEDTLS_SSL_VERSION_TLS1_2 ); |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4540 | |
| 4541 | /* Copy plaintext into record structure */ |
| 4542 | rec.buf = buf; |
Hanno Becker | 1f91878 | 2021-08-01 19:18:28 +0100 | [diff] [blame] | 4543 | rec.buf_len = buf_len; |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4544 | rec.data_offset = 0; |
| 4545 | TEST_ASSERT( plaintext->len <= ciphertext->len ); |
| 4546 | memcpy( rec.buf + rec.data_offset, plaintext->x, plaintext->len ); |
| 4547 | rec.data_len = plaintext->len; |
| 4548 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 4549 | rec.cid_len = 0; |
| 4550 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 4551 | |
| 4552 | memset( &rec.ctr[0], 0, 8 ); |
| 4553 | rec.ctr[7] = ctr; |
| 4554 | |
| 4555 | TEST_ASSERT( mbedtls_ssl_encrypt_buf( NULL, &transform_send, &rec, |
| 4556 | NULL, NULL ) == 0 ); |
Hanno Becker | 1f91878 | 2021-08-01 19:18:28 +0100 | [diff] [blame] | 4557 | |
| 4558 | if( padding_used == MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY ) |
| 4559 | { |
| 4560 | ASSERT_COMPARE( rec.buf + rec.data_offset, rec.data_len, |
| 4561 | ciphertext->x, ciphertext->len ); |
| 4562 | } |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4563 | |
| 4564 | TEST_ASSERT( mbedtls_ssl_decrypt_buf( NULL, &transform_recv, &rec ) == 0 ); |
| 4565 | ASSERT_COMPARE( rec.buf + rec.data_offset, rec.data_len, |
| 4566 | plaintext->x, plaintext->len ); |
| 4567 | |
Hanno Becker | 80e760e | 2021-03-23 06:00:21 +0000 | [diff] [blame] | 4568 | mbedtls_free( buf ); |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4569 | mbedtls_ssl_transform_free( &transform_send ); |
| 4570 | mbedtls_ssl_transform_free( &transform_recv ); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 4571 | USE_PSA_DONE( ); |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4572 | } |
| 4573 | /* END_CASE */ |
| 4574 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4575 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */ |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4576 | void ssl_tls13_key_evolution( int hash_alg, |
| 4577 | data_t *secret, |
| 4578 | data_t *input, |
| 4579 | data_t *expected ) |
Hanno Becker | 2d2c3eb | 2020-08-20 14:54:24 +0100 | [diff] [blame] | 4580 | { |
| 4581 | unsigned char secret_new[ MBEDTLS_MD_MAX_SIZE ]; |
| 4582 | |
Gabor Mezei | 5d9a1fe | 2022-03-24 17:49:14 +0100 | [diff] [blame] | 4583 | PSA_INIT(); |
Gabor Mezei | 892c4aa | 2022-03-21 12:21:43 +0100 | [diff] [blame] | 4584 | |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4585 | TEST_ASSERT( mbedtls_ssl_tls13_evolve_secret( |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 4586 | (psa_algorithm_t) hash_alg, |
Hanno Becker | 2d2c3eb | 2020-08-20 14:54:24 +0100 | [diff] [blame] | 4587 | secret->len ? secret->x : NULL, |
| 4588 | input->len ? input->x : NULL, input->len, |
| 4589 | secret_new ) == 0 ); |
| 4590 | |
Hanno Becker | fb08096 | 2020-09-08 10:58:42 +0100 | [diff] [blame] | 4591 | ASSERT_COMPARE( secret_new, (size_t) expected->len, |
| 4592 | expected->x, (size_t) expected->len ); |
Gabor Mezei | 892c4aa | 2022-03-21 12:21:43 +0100 | [diff] [blame] | 4593 | |
Gabor Mezei | 5d9a1fe | 2022-03-24 17:49:14 +0100 | [diff] [blame] | 4594 | PSA_DONE(); |
Hanno Becker | 2d2c3eb | 2020-08-20 14:54:24 +0100 | [diff] [blame] | 4595 | } |
| 4596 | /* END_CASE */ |
| 4597 | |
Jerry Yu | 53d23e2 | 2022-02-09 16:25:09 +0800 | [diff] [blame] | 4598 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_2 */ |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 4599 | void ssl_tls_prf( int type, data_t * secret, data_t * random, |
Ronald Cron | ac6ae35 | 2020-06-26 14:33:03 +0200 | [diff] [blame] | 4600 | char *label, data_t *result_str, int exp_ret ) |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 4601 | { |
| 4602 | unsigned char *output; |
| 4603 | |
Ronald Cron | ac6ae35 | 2020-06-26 14:33:03 +0200 | [diff] [blame] | 4604 | output = mbedtls_calloc( 1, result_str->len ); |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 4605 | if( output == NULL ) |
| 4606 | goto exit; |
| 4607 | |
Gilles Peskine | 9de97e2 | 2021-02-02 21:00:11 +0100 | [diff] [blame] | 4608 | USE_PSA_INIT( ); |
Ron Eldor | 6b9b1b8 | 2019-05-15 17:04:33 +0300 | [diff] [blame] | 4609 | |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 4610 | TEST_ASSERT( mbedtls_ssl_tls_prf( type, secret->x, secret->len, |
| 4611 | label, random->x, random->len, |
Ronald Cron | ac6ae35 | 2020-06-26 14:33:03 +0200 | [diff] [blame] | 4612 | output, result_str->len ) == exp_ret ); |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 4613 | |
| 4614 | if( exp_ret == 0 ) |
| 4615 | { |
Ronald Cron | ac6ae35 | 2020-06-26 14:33:03 +0200 | [diff] [blame] | 4616 | TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x, |
| 4617 | result_str->len, result_str->len ) == 0 ); |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 4618 | } |
| 4619 | exit: |
| 4620 | |
| 4621 | mbedtls_free( output ); |
Gilles Peskine | 1f186ff | 2021-02-02 21:04:06 +0100 | [diff] [blame] | 4622 | USE_PSA_DONE( ); |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 4623 | } |
| 4624 | /* END_CASE */ |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 4625 | |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 4626 | /* BEGIN_CASE */ |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 4627 | 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] | 4628 | { |
| 4629 | mbedtls_ssl_session original, restored; |
| 4630 | unsigned char *buf = NULL; |
| 4631 | size_t len; |
| 4632 | |
| 4633 | /* |
| 4634 | * Test that a save-load pair is the identity |
| 4635 | */ |
| 4636 | |
| 4637 | mbedtls_ssl_session_init( &original ); |
| 4638 | mbedtls_ssl_session_init( &restored ); |
| 4639 | |
| 4640 | /* Prepare a dummy session to work on */ |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4641 | TEST_ASSERT( ssl_populate_session_tls12( &original, ticket_len, crt_file ) == 0 ); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 4642 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 4643 | /* Serialize it */ |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 4644 | TEST_ASSERT( mbedtls_ssl_session_save( &original, NULL, 0, &len ) |
| 4645 | == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 4646 | TEST_ASSERT( ( buf = mbedtls_calloc( 1, len ) ) != NULL ); |
| 4647 | TEST_ASSERT( mbedtls_ssl_session_save( &original, buf, len, &len ) |
| 4648 | == 0 ); |
| 4649 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 4650 | /* Restore session from serialized data */ |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 4651 | TEST_ASSERT( mbedtls_ssl_session_load( &restored, buf, len) == 0 ); |
| 4652 | |
| 4653 | /* |
| 4654 | * Make sure both session structures are identical |
| 4655 | */ |
| 4656 | #if defined(MBEDTLS_HAVE_TIME) |
| 4657 | TEST_ASSERT( original.start == restored.start ); |
| 4658 | #endif |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 4659 | TEST_ASSERT( original.tls_version == restored.tls_version ); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 4660 | TEST_ASSERT( original.ciphersuite == restored.ciphersuite ); |
| 4661 | TEST_ASSERT( original.compression == restored.compression ); |
| 4662 | TEST_ASSERT( original.id_len == restored.id_len ); |
| 4663 | TEST_ASSERT( memcmp( original.id, |
| 4664 | restored.id, sizeof( original.id ) ) == 0 ); |
| 4665 | TEST_ASSERT( memcmp( original.master, |
| 4666 | restored.master, sizeof( original.master ) ) == 0 ); |
| 4667 | |
| 4668 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 4669 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 4670 | TEST_ASSERT( ( original.peer_cert == NULL ) == |
| 4671 | ( restored.peer_cert == NULL ) ); |
| 4672 | if( original.peer_cert != NULL ) |
| 4673 | { |
| 4674 | TEST_ASSERT( original.peer_cert->raw.len == |
| 4675 | restored.peer_cert->raw.len ); |
| 4676 | TEST_ASSERT( memcmp( original.peer_cert->raw.p, |
| 4677 | restored.peer_cert->raw.p, |
| 4678 | original.peer_cert->raw.len ) == 0 ); |
| 4679 | } |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 4680 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 4681 | TEST_ASSERT( original.peer_cert_digest_type == |
| 4682 | restored.peer_cert_digest_type ); |
| 4683 | TEST_ASSERT( original.peer_cert_digest_len == |
| 4684 | restored.peer_cert_digest_len ); |
| 4685 | TEST_ASSERT( ( original.peer_cert_digest == NULL ) == |
| 4686 | ( restored.peer_cert_digest == NULL ) ); |
| 4687 | if( original.peer_cert_digest != NULL ) |
| 4688 | { |
| 4689 | TEST_ASSERT( memcmp( original.peer_cert_digest, |
| 4690 | restored.peer_cert_digest, |
| 4691 | original.peer_cert_digest_len ) == 0 ); |
| 4692 | } |
| 4693 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 4694 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 4695 | TEST_ASSERT( original.verify_result == restored.verify_result ); |
| 4696 | |
| 4697 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 4698 | TEST_ASSERT( original.ticket_len == restored.ticket_len ); |
| 4699 | if( original.ticket_len != 0 ) |
| 4700 | { |
| 4701 | TEST_ASSERT( original.ticket != NULL ); |
| 4702 | TEST_ASSERT( restored.ticket != NULL ); |
| 4703 | TEST_ASSERT( memcmp( original.ticket, |
| 4704 | restored.ticket, original.ticket_len ) == 0 ); |
| 4705 | } |
| 4706 | TEST_ASSERT( original.ticket_lifetime == restored.ticket_lifetime ); |
| 4707 | #endif |
| 4708 | |
| 4709 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 4710 | TEST_ASSERT( original.mfl_code == restored.mfl_code ); |
| 4711 | #endif |
| 4712 | |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 4713 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 4714 | TEST_ASSERT( original.encrypt_then_mac == restored.encrypt_then_mac ); |
| 4715 | #endif |
| 4716 | |
| 4717 | exit: |
| 4718 | mbedtls_ssl_session_free( &original ); |
| 4719 | mbedtls_ssl_session_free( &restored ); |
| 4720 | mbedtls_free( buf ); |
| 4721 | } |
| 4722 | /* END_CASE */ |
| 4723 | |
Manuel Pégourié-Gonnard | aa75583 | 2019-06-03 10:53:47 +0200 | [diff] [blame] | 4724 | /* BEGIN_CASE */ |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 4725 | 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] | 4726 | { |
| 4727 | mbedtls_ssl_session session; |
| 4728 | unsigned char *buf1 = NULL, *buf2 = NULL; |
| 4729 | size_t len0, len1, len2; |
| 4730 | |
| 4731 | /* |
| 4732 | * Test that a load-save pair is the identity |
| 4733 | */ |
| 4734 | |
| 4735 | mbedtls_ssl_session_init( &session ); |
| 4736 | |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 4737 | /* Prepare a dummy session to work on */ |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4738 | TEST_ASSERT( ssl_populate_session_tls12( &session, ticket_len, crt_file ) == 0 ); |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 4739 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 4740 | /* Get desired buffer size for serializing */ |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 4741 | TEST_ASSERT( mbedtls_ssl_session_save( &session, NULL, 0, &len0 ) |
| 4742 | == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 4743 | |
| 4744 | /* Allocate first buffer */ |
| 4745 | buf1 = mbedtls_calloc( 1, len0 ); |
| 4746 | TEST_ASSERT( buf1 != NULL ); |
| 4747 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 4748 | /* Serialize to buffer and free live session */ |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 4749 | TEST_ASSERT( mbedtls_ssl_session_save( &session, buf1, len0, &len1 ) |
| 4750 | == 0 ); |
| 4751 | TEST_ASSERT( len0 == len1 ); |
| 4752 | mbedtls_ssl_session_free( &session ); |
| 4753 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 4754 | /* Restore session from serialized data */ |
Manuel Pégourié-Gonnard | 220403b | 2019-05-24 09:54:21 +0200 | [diff] [blame] | 4755 | TEST_ASSERT( mbedtls_ssl_session_load( &session, buf1, len1 ) == 0 ); |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 4756 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 4757 | /* Allocate second buffer and serialize to it */ |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 4758 | buf2 = mbedtls_calloc( 1, len0 ); |
Manuel Pégourié-Gonnard | b407990 | 2019-05-24 09:52:10 +0200 | [diff] [blame] | 4759 | TEST_ASSERT( buf2 != NULL ); |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 4760 | TEST_ASSERT( mbedtls_ssl_session_save( &session, buf2, len0, &len2 ) |
| 4761 | == 0 ); |
| 4762 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 4763 | /* Make sure both serialized versions are identical */ |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 4764 | TEST_ASSERT( len1 == len2 ); |
| 4765 | TEST_ASSERT( memcmp( buf1, buf2, len1 ) == 0 ); |
| 4766 | |
| 4767 | exit: |
| 4768 | mbedtls_ssl_session_free( &session ); |
| 4769 | mbedtls_free( buf1 ); |
| 4770 | mbedtls_free( buf2 ); |
| 4771 | } |
| 4772 | /* END_CASE */ |
Manuel Pégourié-Gonnard | f5fa0aa | 2019-05-23 10:38:11 +0200 | [diff] [blame] | 4773 | |
| 4774 | /* BEGIN_CASE */ |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 4775 | 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] | 4776 | { |
| 4777 | mbedtls_ssl_session session; |
| 4778 | unsigned char *buf = NULL; |
| 4779 | size_t good_len, bad_len, test_len; |
| 4780 | |
| 4781 | /* |
| 4782 | * Test that session_save() fails cleanly on small buffers |
| 4783 | */ |
| 4784 | |
| 4785 | mbedtls_ssl_session_init( &session ); |
| 4786 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 4787 | /* Prepare dummy session and get serialized size */ |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4788 | TEST_ASSERT( ssl_populate_session_tls12( &session, ticket_len, crt_file ) == 0 ); |
Manuel Pégourié-Gonnard | f5fa0aa | 2019-05-23 10:38:11 +0200 | [diff] [blame] | 4789 | TEST_ASSERT( mbedtls_ssl_session_save( &session, NULL, 0, &good_len ) |
| 4790 | == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 4791 | |
| 4792 | /* Try all possible bad lengths */ |
| 4793 | for( bad_len = 1; bad_len < good_len; bad_len++ ) |
| 4794 | { |
| 4795 | /* Allocate exact size so that asan/valgrind can detect any overwrite */ |
| 4796 | mbedtls_free( buf ); |
| 4797 | TEST_ASSERT( ( buf = mbedtls_calloc( 1, bad_len ) ) != NULL ); |
| 4798 | TEST_ASSERT( mbedtls_ssl_session_save( &session, buf, bad_len, |
| 4799 | &test_len ) |
| 4800 | == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 4801 | TEST_ASSERT( test_len == good_len ); |
| 4802 | } |
| 4803 | |
| 4804 | exit: |
| 4805 | mbedtls_ssl_session_free( &session ); |
| 4806 | mbedtls_free( buf ); |
| 4807 | } |
| 4808 | /* END_CASE */ |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 4809 | |
| 4810 | /* BEGIN_CASE */ |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 4811 | void ssl_serialize_session_load_buf_size( int ticket_len, char *crt_file ) |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 4812 | { |
| 4813 | mbedtls_ssl_session session; |
| 4814 | unsigned char *good_buf = NULL, *bad_buf = NULL; |
| 4815 | size_t good_len, bad_len; |
| 4816 | |
| 4817 | /* |
| 4818 | * Test that session_load() fails cleanly on small buffers |
| 4819 | */ |
| 4820 | |
| 4821 | mbedtls_ssl_session_init( &session ); |
| 4822 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 4823 | /* Prepare serialized session data */ |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4824 | TEST_ASSERT( ssl_populate_session_tls12( &session, ticket_len, crt_file ) == 0 ); |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 4825 | TEST_ASSERT( mbedtls_ssl_session_save( &session, NULL, 0, &good_len ) |
| 4826 | == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 4827 | TEST_ASSERT( ( good_buf = mbedtls_calloc( 1, good_len ) ) != NULL ); |
| 4828 | TEST_ASSERT( mbedtls_ssl_session_save( &session, good_buf, good_len, |
| 4829 | &good_len ) == 0 ); |
| 4830 | mbedtls_ssl_session_free( &session ); |
| 4831 | |
| 4832 | /* Try all possible bad lengths */ |
| 4833 | for( bad_len = 0; bad_len < good_len; bad_len++ ) |
| 4834 | { |
| 4835 | /* Allocate exact size so that asan/valgrind can detect any overread */ |
| 4836 | mbedtls_free( bad_buf ); |
| 4837 | bad_buf = mbedtls_calloc( 1, bad_len ? bad_len : 1 ); |
| 4838 | TEST_ASSERT( bad_buf != NULL ); |
| 4839 | memcpy( bad_buf, good_buf, bad_len ); |
| 4840 | |
| 4841 | TEST_ASSERT( mbedtls_ssl_session_load( &session, bad_buf, bad_len ) |
| 4842 | == MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4843 | } |
| 4844 | |
| 4845 | exit: |
| 4846 | mbedtls_ssl_session_free( &session ); |
| 4847 | mbedtls_free( good_buf ); |
| 4848 | mbedtls_free( bad_buf ); |
| 4849 | } |
| 4850 | /* END_CASE */ |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 4851 | |
Hanno Becker | 363b646 | 2019-05-29 12:44:28 +0100 | [diff] [blame] | 4852 | /* BEGIN_CASE */ |
| 4853 | void ssl_session_serialize_version_check( int corrupt_major, |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 4854 | int corrupt_minor, |
| 4855 | int corrupt_patch, |
| 4856 | int corrupt_config ) |
| 4857 | { |
Hanno Becker | 363b646 | 2019-05-29 12:44:28 +0100 | [diff] [blame] | 4858 | unsigned char serialized_session[ 2048 ]; |
| 4859 | size_t serialized_session_len; |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 4860 | unsigned cur_byte; |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 4861 | mbedtls_ssl_session session; |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 4862 | uint8_t should_corrupt_byte[] = { corrupt_major == 1, |
| 4863 | corrupt_minor == 1, |
| 4864 | corrupt_patch == 1, |
| 4865 | corrupt_config == 1, |
| 4866 | corrupt_config == 1 }; |
| 4867 | |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 4868 | mbedtls_ssl_session_init( &session ); |
Paul Elliott | 46a6c20 | 2021-12-09 18:16:13 +0000 | [diff] [blame] | 4869 | TEST_ASSERT( ssl_populate_session_tls12( &session, 0, NULL ) == 0 ); |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 4870 | |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 4871 | /* Infer length of serialized session. */ |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 4872 | TEST_ASSERT( mbedtls_ssl_session_save( &session, |
Hanno Becker | 363b646 | 2019-05-29 12:44:28 +0100 | [diff] [blame] | 4873 | serialized_session, |
| 4874 | sizeof( serialized_session ), |
| 4875 | &serialized_session_len ) == 0 ); |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 4876 | |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 4877 | mbedtls_ssl_session_free( &session ); |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 4878 | |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 4879 | /* Without any modification, we should be able to successfully |
Hanno Becker | 363b646 | 2019-05-29 12:44:28 +0100 | [diff] [blame] | 4880 | * de-serialize the session - double-check that. */ |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 4881 | TEST_ASSERT( mbedtls_ssl_session_load( &session, |
Hanno Becker | 363b646 | 2019-05-29 12:44:28 +0100 | [diff] [blame] | 4882 | serialized_session, |
| 4883 | serialized_session_len ) == 0 ); |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 4884 | mbedtls_ssl_session_free( &session ); |
| 4885 | |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 4886 | /* Go through the bytes in the serialized session header and |
| 4887 | * corrupt them bit-by-bit. */ |
| 4888 | for( cur_byte = 0; cur_byte < sizeof( should_corrupt_byte ); cur_byte++ ) |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 4889 | { |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 4890 | int cur_bit; |
| 4891 | unsigned char * const byte = &serialized_session[ cur_byte ]; |
| 4892 | |
| 4893 | if( should_corrupt_byte[ cur_byte ] == 0 ) |
| 4894 | continue; |
| 4895 | |
| 4896 | for( cur_bit = 0; cur_bit < CHAR_BIT; cur_bit++ ) |
| 4897 | { |
| 4898 | unsigned char const corrupted_bit = 0x1u << cur_bit; |
| 4899 | /* Modify a single bit in the serialized session. */ |
| 4900 | *byte ^= corrupted_bit; |
| 4901 | |
| 4902 | /* Attempt to deserialize */ |
| 4903 | TEST_ASSERT( mbedtls_ssl_session_load( &session, |
| 4904 | serialized_session, |
| 4905 | serialized_session_len ) == |
Hanno Becker | f9b3303 | 2019-06-03 12:58:39 +0100 | [diff] [blame] | 4906 | MBEDTLS_ERR_SSL_VERSION_MISMATCH ); |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 4907 | |
| 4908 | /* Undo the change */ |
| 4909 | *byte ^= corrupted_bit; |
| 4910 | } |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 4911 | } |
| 4912 | |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 4913 | } |
| 4914 | /* END_CASE */ |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4915 | |
Neil Armstrong | 0739336 | 2022-04-14 15:36:17 +0200 | [diff] [blame] | 4916 | /* 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] | 4917 | void mbedtls_endpoint_sanity( int endpoint_type ) |
| 4918 | { |
| 4919 | enum { BUFFSIZE = 1024 }; |
| 4920 | mbedtls_endpoint ep; |
| 4921 | int ret = -1; |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 4922 | handshake_test_options options; |
| 4923 | init_handshake_options( &options ); |
| 4924 | options.pk_alg = MBEDTLS_PK_RSA; |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4925 | |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 4926 | ret = mbedtls_endpoint_init( NULL, endpoint_type, &options, |
| 4927 | NULL, NULL, NULL, NULL ); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4928 | TEST_ASSERT( MBEDTLS_ERR_SSL_BAD_INPUT_DATA == ret ); |
| 4929 | |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 4930 | ret = mbedtls_endpoint_certificate_init( NULL, options.pk_alg, 0, 0, 0 ); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4931 | TEST_ASSERT( MBEDTLS_ERR_SSL_BAD_INPUT_DATA == ret ); |
| 4932 | |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 4933 | ret = mbedtls_endpoint_init( &ep, endpoint_type, &options, |
Andrzej Kurek | 74394a5 | 2022-03-08 06:50:12 -0500 | [diff] [blame] | 4934 | NULL, NULL, NULL, NULL ); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4935 | TEST_ASSERT( ret == 0 ); |
| 4936 | |
| 4937 | exit: |
Andrzej Kurek | 15daf50 | 2020-02-12 09:17:52 -0500 | [diff] [blame] | 4938 | mbedtls_endpoint_free( &ep, NULL ); |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 4939 | free_handshake_options( &options ); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4940 | } |
| 4941 | /* END_CASE */ |
| 4942 | |
Jerry Yu | 023ff7a | 2022-04-29 17:00:19 +0800 | [diff] [blame] | 4943 | /* 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] | 4944 | void move_handshake_to_state(int endpoint_type, int state, int need_pass) |
| 4945 | { |
| 4946 | enum { BUFFSIZE = 1024 }; |
| 4947 | mbedtls_endpoint base_ep, second_ep; |
| 4948 | int ret = -1; |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 4949 | handshake_test_options options; |
| 4950 | init_handshake_options( &options ); |
| 4951 | options.pk_alg = MBEDTLS_PK_RSA; |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4952 | |
Neil Armstrong | 06baf04 | 2022-04-14 16:21:15 +0200 | [diff] [blame] | 4953 | USE_PSA_INIT( ); |
| 4954 | |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 4955 | ret = mbedtls_endpoint_init( &base_ep, endpoint_type, &options, |
| 4956 | NULL, NULL, NULL, NULL ); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4957 | TEST_ASSERT( ret == 0 ); |
| 4958 | |
| 4959 | ret = mbedtls_endpoint_init( &second_ep, |
| 4960 | ( endpoint_type == MBEDTLS_SSL_IS_SERVER ) ? |
Andrzej Kurek | b298074 | 2020-02-02 19:25:26 -0500 | [diff] [blame] | 4961 | MBEDTLS_SSL_IS_CLIENT : MBEDTLS_SSL_IS_SERVER, |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 4962 | &options, NULL, NULL, NULL, NULL ); |
| 4963 | |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4964 | TEST_ASSERT( ret == 0 ); |
| 4965 | |
| 4966 | ret = mbedtls_mock_socket_connect( &(base_ep.socket), |
| 4967 | &(second_ep.socket), |
| 4968 | BUFFSIZE ); |
| 4969 | TEST_ASSERT( ret == 0 ); |
| 4970 | |
| 4971 | ret = mbedtls_move_handshake_to_state( &(base_ep.ssl), |
| 4972 | &(second_ep.ssl), |
| 4973 | state ); |
| 4974 | if( need_pass ) |
| 4975 | { |
Jerry Yu | 1443537 | 2022-06-01 15:53:31 +0800 | [diff] [blame] | 4976 | TEST_ASSERT( ret == 0 || |
| 4977 | ret == MBEDTLS_ERR_SSL_WANT_READ || |
| 4978 | ret == MBEDTLS_ERR_SSL_WANT_WRITE ); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4979 | TEST_ASSERT( base_ep.ssl.state == state ); |
| 4980 | } |
| 4981 | else |
| 4982 | { |
Jerry Yu | 1443537 | 2022-06-01 15:53:31 +0800 | [diff] [blame] | 4983 | TEST_ASSERT( ret != 0 && |
| 4984 | ret != MBEDTLS_ERR_SSL_WANT_READ && |
| 4985 | ret != MBEDTLS_ERR_SSL_WANT_WRITE ); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4986 | TEST_ASSERT( base_ep.ssl.state != state ); |
| 4987 | } |
| 4988 | |
| 4989 | exit: |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 4990 | free_handshake_options( &options ); |
Andrzej Kurek | 15daf50 | 2020-02-12 09:17:52 -0500 | [diff] [blame] | 4991 | mbedtls_endpoint_free( &base_ep, NULL ); |
| 4992 | mbedtls_endpoint_free( &second_ep, NULL ); |
Neil Armstrong | 06baf04 | 2022-04-14 16:21:15 +0200 | [diff] [blame] | 4993 | USE_PSA_DONE( ); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4994 | } |
| 4995 | /* END_CASE */ |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 4996 | |
Neil Armstrong | 46a1760 | 2022-02-23 15:11:16 +0100 | [diff] [blame] | 4997 | /* 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] | 4998 | void handshake_version( int dtls, int client_min_version, int client_max_version, |
| 4999 | int server_min_version, int server_max_version, |
| 5000 | int expected_negotiated_version ) |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 5001 | { |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5002 | handshake_test_options options; |
| 5003 | init_handshake_options( &options ); |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 5004 | |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 5005 | options.client_min_version = client_min_version; |
| 5006 | options.client_max_version = client_max_version; |
| 5007 | options.server_min_version = server_min_version; |
| 5008 | options.server_max_version = server_max_version; |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 5009 | options.expected_negotiated_version = expected_negotiated_version; |
| 5010 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5011 | options.dtls = dtls; |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5012 | perform_handshake( &options ); |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 5013 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5014 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 5015 | goto exit; |
Andrzej Kurek | e11acb2 | 2022-06-27 06:11:34 -0400 | [diff] [blame] | 5016 | |
| 5017 | exit: |
| 5018 | free_handshake_options( &options ); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5019 | } |
| 5020 | /* END_CASE */ |
Andrzej Kurek | 9e9efdc | 2020-02-26 05:25:23 -0500 | [diff] [blame] | 5021 | |
Neil Armstrong | 46a1760 | 2022-02-23 15:11:16 +0100 | [diff] [blame] | 5022 | /* 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] | 5023 | void handshake_psk_cipher( char* cipher, int pk_alg, data_t *psk_str, int dtls ) |
| 5024 | { |
| 5025 | handshake_test_options options; |
| 5026 | init_handshake_options( &options ); |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 5027 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5028 | options.cipher = cipher; |
| 5029 | options.dtls = dtls; |
| 5030 | options.psk_str = psk_str; |
| 5031 | options.pk_alg = pk_alg; |
Andrzej Kurek | cc5169c | 2020-02-04 09:04:56 -0500 | [diff] [blame] | 5032 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5033 | perform_handshake( &options ); |
Andrzej Kurek | 316da1f | 2020-02-26 09:03:47 -0500 | [diff] [blame] | 5034 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5035 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 5036 | goto exit; |
Andrzej Kurek | e11acb2 | 2022-06-27 06:11:34 -0400 | [diff] [blame] | 5037 | |
Andrzej Kurek | ddb8cd6 | 2022-07-04 16:07:28 -0400 | [diff] [blame] | 5038 | exit: |
Andrzej Kurek | e11acb2 | 2022-06-27 06:11:34 -0400 | [diff] [blame] | 5039 | free_handshake_options( &options ); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5040 | } |
| 5041 | /* END_CASE */ |
Andrzej Kurek | 316da1f | 2020-02-26 09:03:47 -0500 | [diff] [blame] | 5042 | |
Neil Armstrong | 46a1760 | 2022-02-23 15:11:16 +0100 | [diff] [blame] | 5043 | /* 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] | 5044 | void handshake_cipher( char* cipher, int pk_alg, int dtls ) |
| 5045 | { |
| 5046 | test_handshake_psk_cipher( cipher, pk_alg, NULL, dtls ); |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 5047 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5048 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 5049 | goto exit; |
| 5050 | } |
| 5051 | /* END_CASE */ |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 5052 | |
Neil Armstrong | 8c52ed8 | 2022-05-27 13:14:55 +0200 | [diff] [blame] | 5053 | /* 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] | 5054 | void handshake_ciphersuite_select( char* cipher, int pk_alg, data_t *psk_str, |
| 5055 | int psa_alg, int psa_alg2, int psa_usage, |
Neil Armstrong | 8c52ed8 | 2022-05-27 13:14:55 +0200 | [diff] [blame] | 5056 | int expected_handshake_result, |
Neil Armstrong | db13497 | 2022-06-30 09:06:28 +0200 | [diff] [blame] | 5057 | int expected_ciphersuite ) |
Neil Armstrong | 8c52ed8 | 2022-05-27 13:14:55 +0200 | [diff] [blame] | 5058 | { |
| 5059 | handshake_test_options options; |
| 5060 | init_handshake_options( &options ); |
| 5061 | |
| 5062 | options.cipher = cipher; |
Neil Armstrong | db13497 | 2022-06-30 09:06:28 +0200 | [diff] [blame] | 5063 | options.psk_str = psk_str; |
Neil Armstrong | 8c52ed8 | 2022-05-27 13:14:55 +0200 | [diff] [blame] | 5064 | options.pk_alg = pk_alg; |
| 5065 | options.opaque_alg = psa_alg; |
| 5066 | options.opaque_alg2 = psa_alg2; |
| 5067 | options.opaque_usage = psa_usage; |
| 5068 | options.expected_handshake_result = expected_handshake_result; |
| 5069 | options.expected_ciphersuite = expected_ciphersuite; |
| 5070 | perform_handshake( &options ); |
| 5071 | |
| 5072 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 5073 | goto exit; |
Andrzej Kurek | ddb8cd6 | 2022-07-04 16:07:28 -0400 | [diff] [blame] | 5074 | |
| 5075 | exit: |
| 5076 | free_handshake_options( &options ); |
Neil Armstrong | 8c52ed8 | 2022-05-27 13:14:55 +0200 | [diff] [blame] | 5077 | } |
| 5078 | /* END_CASE */ |
| 5079 | |
Ronald Cron | 21a1b2d | 2022-06-15 17:11:35 +0200 | [diff] [blame] | 5080 | /* 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] | 5081 | void app_data( int mfl, int cli_msg_len, int srv_msg_len, |
| 5082 | int expected_cli_fragments, |
| 5083 | int expected_srv_fragments, int dtls ) |
| 5084 | { |
| 5085 | handshake_test_options options; |
| 5086 | init_handshake_options( &options ); |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 5087 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5088 | options.mfl = mfl; |
| 5089 | options.cli_msg_len = cli_msg_len; |
| 5090 | options.srv_msg_len = srv_msg_len; |
| 5091 | options.expected_cli_fragments = expected_cli_fragments; |
| 5092 | options.expected_srv_fragments = expected_srv_fragments; |
| 5093 | options.dtls = dtls; |
Ronald Cron | 21a1b2d | 2022-06-15 17:11:35 +0200 | [diff] [blame] | 5094 | #if ! defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 5095 | options.expected_negotiated_version = MBEDTLS_SSL_VERSION_TLS1_3; |
| 5096 | #endif |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 5097 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5098 | perform_handshake( &options ); |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 5099 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5100 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 5101 | goto exit; |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 5102 | |
| 5103 | exit: |
| 5104 | free_handshake_options( &options ); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5105 | } |
| 5106 | /* END_CASE */ |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 5107 | |
Ronald Cron | 21a1b2d | 2022-06-15 17:11:35 +0200 | [diff] [blame] | 5108 | /* 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] | 5109 | void app_data_tls( int mfl, int cli_msg_len, int srv_msg_len, |
| 5110 | int expected_cli_fragments, |
| 5111 | int expected_srv_fragments ) |
| 5112 | { |
| 5113 | test_app_data( mfl, cli_msg_len, srv_msg_len, expected_cli_fragments, |
| 5114 | expected_srv_fragments, 0 ); |
| 5115 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 5116 | goto exit; |
| 5117 | } |
| 5118 | /* END_CASE */ |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 5119 | |
Neil Armstrong | 993eea3 | 2022-04-14 15:37:23 +0200 | [diff] [blame] | 5120 | /* 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] | 5121 | void app_data_dtls( int mfl, int cli_msg_len, int srv_msg_len, |
| 5122 | int expected_cli_fragments, |
| 5123 | int expected_srv_fragments ) |
| 5124 | { |
| 5125 | test_app_data( mfl, cli_msg_len, srv_msg_len, expected_cli_fragments, |
| 5126 | expected_srv_fragments, 1 ); |
| 5127 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 5128 | goto exit; |
| 5129 | } |
| 5130 | /* END_CASE */ |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 5131 | |
Neil Armstrong | 181fe69 | 2022-04-14 15:38:01 +0200 | [diff] [blame] | 5132 | /* 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] | 5133 | void handshake_serialization( ) |
| 5134 | { |
| 5135 | handshake_test_options options; |
| 5136 | init_handshake_options( &options ); |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 5137 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5138 | options.serialize = 1; |
| 5139 | options.dtls = 1; |
| 5140 | perform_handshake( &options ); |
| 5141 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 5142 | goto exit; |
Andrzej Kurek | 6e518ab | 2022-06-11 05:08:38 -0400 | [diff] [blame] | 5143 | exit: |
| 5144 | free_handshake_options( &options ); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5145 | } |
| 5146 | /* END_CASE */ |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 5147 | |
Neil Armstrong | 181fe69 | 2022-04-14 15:38:01 +0200 | [diff] [blame] | 5148 | /* 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] | 5149 | void handshake_fragmentation( int mfl, int expected_srv_hs_fragmentation, int expected_cli_hs_fragmentation) |
| 5150 | { |
| 5151 | handshake_test_options options; |
| 5152 | log_pattern srv_pattern, cli_pattern; |
| 5153 | |
| 5154 | srv_pattern.pattern = cli_pattern.pattern = "found fragmented DTLS handshake"; |
| 5155 | srv_pattern.counter = 0; |
| 5156 | cli_pattern.counter = 0; |
| 5157 | |
| 5158 | init_handshake_options( &options ); |
| 5159 | options.dtls = 1; |
| 5160 | options.mfl = mfl; |
Darryl Green | aad82f9 | 2019-12-02 10:53:11 +0000 | [diff] [blame] | 5161 | /* Set cipher to one using CBC so that record splitting can be tested */ |
| 5162 | options.cipher = "TLS-DHE-RSA-WITH-AES-256-CBC-SHA256"; |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 5163 | options.srv_auth_mode = MBEDTLS_SSL_VERIFY_REQUIRED; |
| 5164 | options.srv_log_obj = &srv_pattern; |
| 5165 | options.cli_log_obj = &cli_pattern; |
| 5166 | options.srv_log_fun = log_analyzer; |
| 5167 | options.cli_log_fun = log_analyzer; |
| 5168 | |
| 5169 | perform_handshake( &options ); |
| 5170 | |
| 5171 | /* Test if the server received a fragmented handshake */ |
| 5172 | if( expected_srv_hs_fragmentation ) |
| 5173 | { |
| 5174 | TEST_ASSERT( srv_pattern.counter >= 1 ); |
| 5175 | } |
| 5176 | /* Test if the client received a fragmented handshake */ |
| 5177 | if( expected_cli_hs_fragmentation ) |
| 5178 | { |
| 5179 | TEST_ASSERT( cli_pattern.counter >= 1 ); |
| 5180 | } |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 5181 | |
| 5182 | exit: |
| 5183 | free_handshake_options( &options ); |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 5184 | } |
| 5185 | /* END_CASE */ |
| 5186 | |
Neil Armstrong | 537e915 | 2022-04-14 15:40:26 +0200 | [diff] [blame] | 5187 | /* 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] | 5188 | void renegotiation( int legacy_renegotiation ) |
| 5189 | { |
| 5190 | handshake_test_options options; |
| 5191 | init_handshake_options( &options ); |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 5192 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5193 | options.renegotiate = 1; |
| 5194 | options.legacy_renegotiation = legacy_renegotiation; |
| 5195 | options.dtls = 1; |
Andrzej Kurek | 316da1f | 2020-02-26 09:03:47 -0500 | [diff] [blame] | 5196 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5197 | perform_handshake( &options ); |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 5198 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5199 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 5200 | goto exit; |
Andrzej Kurek | ddb8cd6 | 2022-07-04 16:07:28 -0400 | [diff] [blame] | 5201 | exit: |
| 5202 | free_handshake_options( &options ); |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 5203 | } |
| 5204 | /* END_CASE */ |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 5205 | |
Neil Armstrong | 537e915 | 2022-04-14 15:40:26 +0200 | [diff] [blame] | 5206 | /* 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] | 5207 | void resize_buffers( int mfl, int renegotiation, int legacy_renegotiation, |
Andrzej Kurek | 8ea6872 | 2020-04-03 06:40:47 -0400 | [diff] [blame] | 5208 | int serialize, int dtls, char *cipher ) |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 5209 | { |
| 5210 | handshake_test_options options; |
| 5211 | init_handshake_options( &options ); |
| 5212 | |
| 5213 | options.mfl = mfl; |
Andrzej Kurek | 8ea6872 | 2020-04-03 06:40:47 -0400 | [diff] [blame] | 5214 | options.cipher = cipher; |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 5215 | options.renegotiate = renegotiation; |
| 5216 | options.legacy_renegotiation = legacy_renegotiation; |
| 5217 | options.serialize = serialize; |
| 5218 | options.dtls = dtls; |
| 5219 | options.resize_buffers = 1; |
| 5220 | |
| 5221 | perform_handshake( &options ); |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 5222 | |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 5223 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 5224 | goto exit; |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 5225 | exit: |
| 5226 | free_handshake_options( &options ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 5227 | } |
| 5228 | /* END_CASE */ |
| 5229 | |
Neil Armstrong | 537e915 | 2022-04-14 15:40:26 +0200 | [diff] [blame] | 5230 | /* 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] | 5231 | void resize_buffers_serialize_mfl( int mfl ) |
| 5232 | { |
Andrzej Kurek | 8ea6872 | 2020-04-03 06:40:47 -0400 | [diff] [blame] | 5233 | test_resize_buffers( mfl, 0, MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION, 1, 1, |
| 5234 | (char *) "" ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 5235 | |
| 5236 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 5237 | goto exit; |
| 5238 | } |
| 5239 | /* END_CASE */ |
| 5240 | |
Neil Armstrong | 537e915 | 2022-04-14 15:40:26 +0200 | [diff] [blame] | 5241 | /* 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] | 5242 | void resize_buffers_renegotiate_mfl( int mfl, int legacy_renegotiation, |
| 5243 | char *cipher ) |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 5244 | { |
Andrzej Kurek | 8ea6872 | 2020-04-03 06:40:47 -0400 | [diff] [blame] | 5245 | test_resize_buffers( mfl, 1, legacy_renegotiation, 0, 1, cipher ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 5246 | |
| 5247 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 5248 | goto exit; |
| 5249 | } |
| 5250 | /* END_CASE */ |
Manuel Pégourié-Gonnard | 045f094 | 2020-07-02 11:34:02 +0200 | [diff] [blame] | 5251 | |
Manuel Pégourié-Gonnard | ed0e864 | 2020-07-21 11:20:30 +0200 | [diff] [blame] | 5252 | /* 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] | 5253 | void ssl_cf_hmac( int hash ) |
| 5254 | { |
| 5255 | /* |
Gabor Mezei | 90437e3 | 2021-10-20 11:59:27 +0200 | [diff] [blame] | 5256 | * Test the function mbedtls_ct_hmac() against a reference |
Manuel Pégourié-Gonnard | 045f094 | 2020-07-02 11:34:02 +0200 | [diff] [blame] | 5257 | * implementation. |
Manuel Pégourié-Gonnard | 045f094 | 2020-07-02 11:34:02 +0200 | [diff] [blame] | 5258 | */ |
Neil Armstrong | 2968d30 | 2022-02-25 15:09:36 +0100 | [diff] [blame] | 5259 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 5260 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5261 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5262 | psa_algorithm_t alg; |
| 5263 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 5264 | #else |
Manuel Pégourié-Gonnard | 045f094 | 2020-07-02 11:34:02 +0200 | [diff] [blame] | 5265 | mbedtls_md_context_t ctx, ref_ctx; |
| 5266 | const mbedtls_md_info_t *md_info; |
Neil Armstrong | 2968d30 | 2022-02-25 15:09:36 +0100 | [diff] [blame] | 5267 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 045f094 | 2020-07-02 11:34:02 +0200 | [diff] [blame] | 5268 | size_t out_len, block_size; |
| 5269 | size_t min_in_len, in_len, max_in_len, i; |
| 5270 | /* TLS additional data is 13 bytes (hence the "lucky 13" name) */ |
| 5271 | unsigned char add_data[13]; |
| 5272 | unsigned char ref_out[MBEDTLS_MD_MAX_SIZE]; |
| 5273 | unsigned char *data = NULL; |
| 5274 | unsigned char *out = NULL; |
| 5275 | unsigned char rec_num = 0; |
| 5276 | |
Neil Armstrong | 2968d30 | 2022-02-25 15:09:36 +0100 | [diff] [blame] | 5277 | USE_PSA_INIT( ); |
| 5278 | |
| 5279 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 5280 | alg = PSA_ALG_HMAC( mbedtls_psa_translate_md( hash ) ); |
| 5281 | |
| 5282 | out_len = PSA_HASH_LENGTH( alg ); |
| 5283 | block_size = PSA_HASH_BLOCK_LENGTH( alg ); |
| 5284 | |
| 5285 | /* mbedtls_ct_hmac() requires the key to be exportable */ |
| 5286 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT | |
| 5287 | PSA_KEY_USAGE_VERIFY_HASH ); |
| 5288 | psa_set_key_algorithm( &attributes, PSA_ALG_HMAC( alg ) ); |
| 5289 | psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC ); |
| 5290 | #else |
Manuel Pégourié-Gonnard | 045f094 | 2020-07-02 11:34:02 +0200 | [diff] [blame] | 5291 | mbedtls_md_init( &ctx ); |
| 5292 | mbedtls_md_init( &ref_ctx ); |
| 5293 | |
| 5294 | md_info = mbedtls_md_info_from_type( hash ); |
| 5295 | TEST_ASSERT( md_info != NULL ); |
| 5296 | out_len = mbedtls_md_get_size( md_info ); |
| 5297 | TEST_ASSERT( out_len != 0 ); |
| 5298 | block_size = hash == MBEDTLS_MD_SHA384 ? 128 : 64; |
Neil Armstrong | 2968d30 | 2022-02-25 15:09:36 +0100 | [diff] [blame] | 5299 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 045f094 | 2020-07-02 11:34:02 +0200 | [diff] [blame] | 5300 | |
| 5301 | /* Use allocated out buffer to catch overwrites */ |
| 5302 | ASSERT_ALLOC( out, out_len ); |
| 5303 | |
Neil Armstrong | 2968d30 | 2022-02-25 15:09:36 +0100 | [diff] [blame] | 5304 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 5305 | /* Set up dummy key */ |
| 5306 | memset( ref_out, 42, sizeof( ref_out ) ); |
| 5307 | TEST_EQUAL( PSA_SUCCESS, psa_import_key( &attributes, |
| 5308 | ref_out, out_len, |
| 5309 | &key ) ); |
| 5310 | #else |
Manuel Pégourié-Gonnard | 045f094 | 2020-07-02 11:34:02 +0200 | [diff] [blame] | 5311 | /* Set up contexts with the given hash and a dummy key */ |
| 5312 | TEST_EQUAL( 0, mbedtls_md_setup( &ctx, md_info, 1 ) ); |
| 5313 | TEST_EQUAL( 0, mbedtls_md_setup( &ref_ctx, md_info, 1 ) ); |
| 5314 | memset( ref_out, 42, sizeof( ref_out ) ); |
| 5315 | TEST_EQUAL( 0, mbedtls_md_hmac_starts( &ctx, ref_out, out_len ) ); |
| 5316 | TEST_EQUAL( 0, mbedtls_md_hmac_starts( &ref_ctx, ref_out, out_len ) ); |
| 5317 | memset( ref_out, 0, sizeof( ref_out ) ); |
Neil Armstrong | 2968d30 | 2022-02-25 15:09:36 +0100 | [diff] [blame] | 5318 | #endif |
Manuel Pégourié-Gonnard | 045f094 | 2020-07-02 11:34:02 +0200 | [diff] [blame] | 5319 | |
| 5320 | /* |
| 5321 | * Test all possible lengths up to a point. The difference between |
| 5322 | * max_in_len and min_in_len is at most 255, and make sure they both vary |
| 5323 | * by at least one block size. |
| 5324 | */ |
| 5325 | for( max_in_len = 0; max_in_len <= 255 + block_size; max_in_len++ ) |
| 5326 | { |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 5327 | mbedtls_test_set_step( max_in_len * 10000 ); |
Manuel Pégourié-Gonnard | ca8287c | 2020-07-22 10:29:39 +0200 | [diff] [blame] | 5328 | |
Manuel Pégourié-Gonnard | 045f094 | 2020-07-02 11:34:02 +0200 | [diff] [blame] | 5329 | /* Use allocated in buffer to catch overreads */ |
Manuel Pégourié-Gonnard | c321900 | 2020-07-22 10:32:52 +0200 | [diff] [blame] | 5330 | ASSERT_ALLOC( data, max_in_len ); |
Manuel Pégourié-Gonnard | 045f094 | 2020-07-02 11:34:02 +0200 | [diff] [blame] | 5331 | |
| 5332 | min_in_len = max_in_len > 255 ? max_in_len - 255 : 0; |
| 5333 | for( in_len = min_in_len; in_len <= max_in_len; in_len++ ) |
| 5334 | { |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 5335 | mbedtls_test_set_step( max_in_len * 10000 + in_len ); |
Manuel Pégourié-Gonnard | ca8287c | 2020-07-22 10:29:39 +0200 | [diff] [blame] | 5336 | |
Manuel Pégourié-Gonnard | 045f094 | 2020-07-02 11:34:02 +0200 | [diff] [blame] | 5337 | /* Set up dummy data and add_data */ |
| 5338 | rec_num++; |
| 5339 | memset( add_data, rec_num, sizeof( add_data ) ); |
| 5340 | for( i = 0; i < in_len; i++ ) |
| 5341 | data[i] = ( i & 0xff ) ^ rec_num; |
| 5342 | |
| 5343 | /* Get the function's result */ |
Manuel Pégourié-Gonnard | 9670a59 | 2020-07-10 10:21:46 +0200 | [diff] [blame] | 5344 | TEST_CF_SECRET( &in_len, sizeof( in_len ) ); |
Neil Armstrong | 2968d30 | 2022-02-25 15:09:36 +0100 | [diff] [blame] | 5345 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 5346 | TEST_EQUAL( 0, mbedtls_ct_hmac( key, PSA_ALG_HMAC( alg ), |
| 5347 | add_data, sizeof( add_data ), |
| 5348 | data, in_len, |
| 5349 | min_in_len, max_in_len, |
| 5350 | out ) ); |
| 5351 | #else |
Gabor Mezei | 90437e3 | 2021-10-20 11:59:27 +0200 | [diff] [blame] | 5352 | 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] | 5353 | data, in_len, |
| 5354 | min_in_len, max_in_len, |
| 5355 | out ) ); |
Neil Armstrong | 2968d30 | 2022-02-25 15:09:36 +0100 | [diff] [blame] | 5356 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 9670a59 | 2020-07-10 10:21:46 +0200 | [diff] [blame] | 5357 | TEST_CF_PUBLIC( &in_len, sizeof( in_len ) ); |
| 5358 | TEST_CF_PUBLIC( out, out_len ); |
Manuel Pégourié-Gonnard | 045f094 | 2020-07-02 11:34:02 +0200 | [diff] [blame] | 5359 | |
Neil Armstrong | 2968d30 | 2022-02-25 15:09:36 +0100 | [diff] [blame] | 5360 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 5361 | TEST_EQUAL( PSA_SUCCESS, psa_mac_verify_setup( &operation, |
| 5362 | key, alg ) ); |
| 5363 | TEST_EQUAL( PSA_SUCCESS, psa_mac_update( &operation, add_data, |
| 5364 | sizeof( add_data ) ) ); |
| 5365 | TEST_EQUAL( PSA_SUCCESS, psa_mac_update( &operation, |
| 5366 | data, in_len ) ); |
| 5367 | TEST_EQUAL( PSA_SUCCESS, psa_mac_verify_finish( &operation, |
| 5368 | out, out_len ) ); |
| 5369 | #else |
Manuel Pégourié-Gonnard | 045f094 | 2020-07-02 11:34:02 +0200 | [diff] [blame] | 5370 | /* Compute the reference result */ |
| 5371 | TEST_EQUAL( 0, mbedtls_md_hmac_update( &ref_ctx, add_data, |
| 5372 | sizeof( add_data ) ) ); |
| 5373 | TEST_EQUAL( 0, mbedtls_md_hmac_update( &ref_ctx, data, in_len ) ); |
| 5374 | TEST_EQUAL( 0, mbedtls_md_hmac_finish( &ref_ctx, ref_out ) ); |
| 5375 | TEST_EQUAL( 0, mbedtls_md_hmac_reset( &ref_ctx ) ); |
| 5376 | |
| 5377 | /* Compare */ |
| 5378 | ASSERT_COMPARE( out, out_len, ref_out, out_len ); |
Neil Armstrong | 2968d30 | 2022-02-25 15:09:36 +0100 | [diff] [blame] | 5379 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 045f094 | 2020-07-02 11:34:02 +0200 | [diff] [blame] | 5380 | } |
| 5381 | |
| 5382 | mbedtls_free( data ); |
| 5383 | data = NULL; |
| 5384 | } |
| 5385 | |
| 5386 | exit: |
Neil Armstrong | 2968d30 | 2022-02-25 15:09:36 +0100 | [diff] [blame] | 5387 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 5388 | psa_mac_abort( &operation ); |
| 5389 | psa_destroy_key( key ); |
| 5390 | #else |
Manuel Pégourié-Gonnard | 045f094 | 2020-07-02 11:34:02 +0200 | [diff] [blame] | 5391 | mbedtls_md_free( &ref_ctx ); |
| 5392 | mbedtls_md_free( &ctx ); |
Neil Armstrong | 2968d30 | 2022-02-25 15:09:36 +0100 | [diff] [blame] | 5393 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 045f094 | 2020-07-02 11:34:02 +0200 | [diff] [blame] | 5394 | |
| 5395 | mbedtls_free( data ); |
| 5396 | mbedtls_free( out ); |
Neil Armstrong | 2968d30 | 2022-02-25 15:09:36 +0100 | [diff] [blame] | 5397 | |
| 5398 | USE_PSA_DONE( ); |
Manuel Pégourié-Gonnard | 045f094 | 2020-07-02 11:34:02 +0200 | [diff] [blame] | 5399 | } |
| 5400 | /* END_CASE */ |
Manuel Pégourié-Gonnard | 7fe2c5f | 2020-08-18 12:02:54 +0200 | [diff] [blame] | 5401 | |
| 5402 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC:MBEDTLS_TEST_HOOKS */ |
| 5403 | void ssl_cf_memcpy_offset( int offset_min, int offset_max, int len ) |
| 5404 | { |
| 5405 | unsigned char *dst = NULL; |
| 5406 | unsigned char *src = NULL; |
| 5407 | size_t src_len = offset_max + len; |
| 5408 | size_t secret; |
| 5409 | |
| 5410 | ASSERT_ALLOC( dst, len ); |
| 5411 | ASSERT_ALLOC( src, src_len ); |
| 5412 | |
| 5413 | /* Fill src in a way that we can detect if we copied the right bytes */ |
| 5414 | mbedtls_test_rnd_std_rand( NULL, src, src_len ); |
| 5415 | |
| 5416 | for( secret = offset_min; secret <= (size_t) offset_max; secret++ ) |
| 5417 | { |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 5418 | mbedtls_test_set_step( (int) secret ); |
Manuel Pégourié-Gonnard | 7fe2c5f | 2020-08-18 12:02:54 +0200 | [diff] [blame] | 5419 | |
| 5420 | TEST_CF_SECRET( &secret, sizeof( secret ) ); |
Gabor Mezei | 90437e3 | 2021-10-20 11:59:27 +0200 | [diff] [blame] | 5421 | mbedtls_ct_memcpy_offset( dst, src, secret, |
gabor-mezei-arm | 9fa43ce | 2021-09-28 16:14:47 +0200 | [diff] [blame] | 5422 | offset_min, offset_max, len ); |
Manuel Pégourié-Gonnard | 7fe2c5f | 2020-08-18 12:02:54 +0200 | [diff] [blame] | 5423 | TEST_CF_PUBLIC( &secret, sizeof( secret ) ); |
| 5424 | TEST_CF_PUBLIC( dst, len ); |
| 5425 | |
| 5426 | ASSERT_COMPARE( dst, len, src + secret, len ); |
| 5427 | } |
| 5428 | |
| 5429 | exit: |
| 5430 | mbedtls_free( dst ); |
| 5431 | mbedtls_free( src ); |
| 5432 | } |
| 5433 | /* END_CASE */ |
Hanno Becker | 6667ffd | 2021-04-19 21:59:22 +0100 | [diff] [blame] | 5434 | |
| 5435 | /* BEGIN_CASE depends_on:MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
| 5436 | void test_multiple_psks() |
| 5437 | { |
| 5438 | unsigned char psk0[10] = { 0 }; |
| 5439 | unsigned char psk0_identity[] = { 'f', 'o', 'o' }; |
| 5440 | |
| 5441 | unsigned char psk1[10] = { 0 }; |
| 5442 | unsigned char psk1_identity[] = { 'b', 'a', 'r' }; |
| 5443 | |
| 5444 | mbedtls_ssl_config conf; |
| 5445 | |
Neil Armstrong | 4c3b4e0 | 2022-05-03 09:24:26 +0200 | [diff] [blame] | 5446 | USE_PSA_INIT( ); |
Hanno Becker | 6667ffd | 2021-04-19 21:59:22 +0100 | [diff] [blame] | 5447 | mbedtls_ssl_config_init( &conf ); |
| 5448 | |
| 5449 | TEST_ASSERT( mbedtls_ssl_conf_psk( &conf, |
| 5450 | psk0, sizeof( psk0 ), |
| 5451 | psk0_identity, sizeof( psk0_identity ) ) == 0 ); |
| 5452 | TEST_ASSERT( mbedtls_ssl_conf_psk( &conf, |
| 5453 | psk1, sizeof( psk1 ), |
| 5454 | psk1_identity, sizeof( psk1_identity ) ) == |
| 5455 | MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 5456 | |
| 5457 | exit: |
| 5458 | |
| 5459 | mbedtls_ssl_config_free( &conf ); |
Neil Armstrong | 4c3b4e0 | 2022-05-03 09:24:26 +0200 | [diff] [blame] | 5460 | |
| 5461 | USE_PSA_DONE( ); |
Hanno Becker | 6667ffd | 2021-04-19 21:59:22 +0100 | [diff] [blame] | 5462 | } |
| 5463 | /* END_CASE */ |
| 5464 | |
| 5465 | /* BEGIN_CASE depends_on:MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED:MBEDTLS_USE_PSA_CRYPTO */ |
| 5466 | void test_multiple_psks_opaque( int mode ) |
| 5467 | { |
| 5468 | /* |
| 5469 | * Mode 0: Raw PSK, then opaque PSK |
| 5470 | * Mode 1: Opaque PSK, then raw PSK |
| 5471 | * Mode 2: 2x opaque PSK |
| 5472 | */ |
| 5473 | |
| 5474 | unsigned char psk0_raw[10] = { 0 }; |
| 5475 | unsigned char psk0_raw_identity[] = { 'f', 'o', 'o' }; |
| 5476 | |
Andrzej Kurek | 03e0146 | 2022-01-03 12:53:24 +0100 | [diff] [blame] | 5477 | mbedtls_svc_key_id_t psk0_opaque = mbedtls_svc_key_id_make( 0x1, (psa_key_id_t) 1 ); |
| 5478 | |
Hanno Becker | 6667ffd | 2021-04-19 21:59:22 +0100 | [diff] [blame] | 5479 | unsigned char psk0_opaque_identity[] = { 'f', 'o', 'o' }; |
| 5480 | |
| 5481 | unsigned char psk1_raw[10] = { 0 }; |
| 5482 | unsigned char psk1_raw_identity[] = { 'b', 'a', 'r' }; |
| 5483 | |
Andrzej Kurek | 03e0146 | 2022-01-03 12:53:24 +0100 | [diff] [blame] | 5484 | mbedtls_svc_key_id_t psk1_opaque = mbedtls_svc_key_id_make( 0x1, (psa_key_id_t) 2 ); |
| 5485 | |
Hanno Becker | 6667ffd | 2021-04-19 21:59:22 +0100 | [diff] [blame] | 5486 | unsigned char psk1_opaque_identity[] = { 'b', 'a', 'r' }; |
| 5487 | |
| 5488 | mbedtls_ssl_config conf; |
| 5489 | |
| 5490 | USE_PSA_INIT( ); |
| 5491 | mbedtls_ssl_config_init( &conf ); |
| 5492 | |
| 5493 | switch( mode ) |
| 5494 | { |
| 5495 | case 0: |
| 5496 | |
| 5497 | TEST_ASSERT( mbedtls_ssl_conf_psk( &conf, |
| 5498 | psk0_raw, sizeof( psk0_raw ), |
| 5499 | psk0_raw_identity, sizeof( psk0_raw_identity ) ) |
| 5500 | == 0 ); |
| 5501 | TEST_ASSERT( mbedtls_ssl_conf_psk_opaque( &conf, |
| 5502 | psk1_opaque, |
| 5503 | psk1_opaque_identity, sizeof( psk1_opaque_identity ) ) |
| 5504 | == MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 5505 | break; |
| 5506 | |
| 5507 | case 1: |
| 5508 | |
| 5509 | TEST_ASSERT( mbedtls_ssl_conf_psk_opaque( &conf, |
| 5510 | psk0_opaque, |
| 5511 | psk0_opaque_identity, sizeof( psk0_opaque_identity ) ) |
| 5512 | == 0 ); |
| 5513 | TEST_ASSERT( mbedtls_ssl_conf_psk( &conf, |
| 5514 | psk1_raw, sizeof( psk1_raw ), |
| 5515 | psk1_raw_identity, sizeof( psk1_raw_identity ) ) |
| 5516 | == MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 5517 | |
| 5518 | break; |
| 5519 | |
| 5520 | case 2: |
| 5521 | |
| 5522 | TEST_ASSERT( mbedtls_ssl_conf_psk_opaque( &conf, |
| 5523 | psk0_opaque, |
| 5524 | psk0_opaque_identity, sizeof( psk0_opaque_identity ) ) |
| 5525 | == 0 ); |
| 5526 | TEST_ASSERT( mbedtls_ssl_conf_psk_opaque( &conf, |
| 5527 | psk1_opaque, |
| 5528 | psk1_opaque_identity, sizeof( psk1_opaque_identity ) ) |
| 5529 | == MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 5530 | |
| 5531 | break; |
| 5532 | |
| 5533 | default: |
| 5534 | TEST_ASSERT( 0 ); |
| 5535 | break; |
| 5536 | } |
| 5537 | |
| 5538 | exit: |
| 5539 | |
| 5540 | mbedtls_ssl_config_free( &conf ); |
| 5541 | USE_PSA_DONE( ); |
| 5542 | |
| 5543 | } |
| 5544 | /* END_CASE */ |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 5545 | |
Ronald Cron | 37bdaab | 2022-03-30 16:45:51 +0200 | [diff] [blame] | 5546 | /* BEGIN_CASE */ |
| 5547 | void conf_version( int endpoint, int transport, |
Glenn Strauss | 59b0d54 | 2022-04-12 07:10:06 -0400 | [diff] [blame] | 5548 | int min_tls_version, int max_tls_version, |
Ronald Cron | 37bdaab | 2022-03-30 16:45:51 +0200 | [diff] [blame] | 5549 | int expected_ssl_setup_result ) |
| 5550 | { |
| 5551 | mbedtls_ssl_config conf; |
| 5552 | mbedtls_ssl_context ssl; |
| 5553 | |
| 5554 | mbedtls_ssl_config_init( &conf ); |
| 5555 | mbedtls_ssl_init( &ssl ); |
| 5556 | |
| 5557 | mbedtls_ssl_conf_endpoint( &conf, endpoint ); |
| 5558 | mbedtls_ssl_conf_transport( &conf, transport ); |
Glenn Strauss | 59b0d54 | 2022-04-12 07:10:06 -0400 | [diff] [blame] | 5559 | mbedtls_ssl_conf_min_tls_version( &conf, min_tls_version ); |
| 5560 | mbedtls_ssl_conf_max_tls_version( &conf, max_tls_version ); |
Ronald Cron | 37bdaab | 2022-03-30 16:45:51 +0200 | [diff] [blame] | 5561 | |
| 5562 | TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == expected_ssl_setup_result ); |
| 5563 | |
| 5564 | mbedtls_ssl_free( &ssl ); |
| 5565 | mbedtls_ssl_config_free( &conf ); |
| 5566 | } |
| 5567 | /* END_CASE */ |
| 5568 | |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 5569 | /* 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 */ |
| 5570 | void conf_curve() |
| 5571 | { |
| 5572 | |
| 5573 | mbedtls_ecp_group_id curve_list[] = { MBEDTLS_ECP_DP_SECP192R1, |
| 5574 | MBEDTLS_ECP_DP_SECP224R1, |
| 5575 | MBEDTLS_ECP_DP_SECP256R1, |
| 5576 | MBEDTLS_ECP_DP_NONE }; |
| 5577 | mbedtls_ecp_group_id iana_tls_group_list[] = { MBEDTLS_SSL_IANA_TLS_GROUP_SECP192R1, |
| 5578 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP224R1, |
| 5579 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, |
| 5580 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE }; |
| 5581 | |
| 5582 | mbedtls_ssl_config conf; |
| 5583 | mbedtls_ssl_config_init( &conf ); |
Jerry Yu | baa4934 | 2022-02-15 10:26:40 +0800 | [diff] [blame] | 5584 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Glenn Strauss | e3af4cb | 2022-03-15 03:23:42 -0400 | [diff] [blame] | 5585 | mbedtls_ssl_conf_max_tls_version( &conf, MBEDTLS_SSL_VERSION_TLS1_2 ); |
| 5586 | mbedtls_ssl_conf_min_tls_version( &conf, MBEDTLS_SSL_VERSION_TLS1_2 ); |
Jerry Yu | baa4934 | 2022-02-15 10:26:40 +0800 | [diff] [blame] | 5587 | #else |
Glenn Strauss | e3af4cb | 2022-03-15 03:23:42 -0400 | [diff] [blame] | 5588 | mbedtls_ssl_conf_max_tls_version( &conf, MBEDTLS_SSL_VERSION_TLS1_3 ); |
| 5589 | mbedtls_ssl_conf_min_tls_version( &conf, MBEDTLS_SSL_VERSION_TLS1_3 ); |
Jerry Yu | baa4934 | 2022-02-15 10:26:40 +0800 | [diff] [blame] | 5590 | #endif |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 5591 | mbedtls_ssl_conf_curves( &conf, curve_list ); |
| 5592 | |
| 5593 | mbedtls_ssl_context ssl; |
| 5594 | mbedtls_ssl_init( &ssl ); |
Paul Elliott | 46a6c20 | 2021-12-09 18:16:13 +0000 | [diff] [blame] | 5595 | TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 ); |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 5596 | |
| 5597 | TEST_ASSERT( ssl.handshake != NULL && ssl.handshake->group_list != NULL ); |
| 5598 | TEST_ASSERT( ssl.conf != NULL && ssl.conf->group_list == NULL ); |
| 5599 | |
| 5600 | TEST_EQUAL( ssl.handshake->group_list[ARRAY_LENGTH( iana_tls_group_list ) - 1], MBEDTLS_SSL_IANA_TLS_GROUP_NONE ); |
| 5601 | |
| 5602 | for( size_t i = 0; i < ARRAY_LENGTH( iana_tls_group_list ); i++ ) |
| 5603 | TEST_EQUAL( iana_tls_group_list[i], ssl.handshake->group_list[i] ); |
| 5604 | |
| 5605 | mbedtls_ssl_free( &ssl ); |
| 5606 | mbedtls_ssl_config_free( &conf ); |
| 5607 | } |
| 5608 | /* END_CASE */ |
| 5609 | |
| 5610 | /* BEGIN_CASE depends_on:MBEDTLS_DEPRECATED_REMOVED */ |
| 5611 | void conf_group() |
| 5612 | { |
| 5613 | uint16_t iana_tls_group_list[] = { MBEDTLS_SSL_IANA_TLS_GROUP_SECP192R1, |
| 5614 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP224R1, |
| 5615 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, |
| 5616 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE }; |
| 5617 | |
| 5618 | mbedtls_ssl_config conf; |
| 5619 | mbedtls_ssl_config_init( &conf ); |
| 5620 | |
Glenn Strauss | e3af4cb | 2022-03-15 03:23:42 -0400 | [diff] [blame] | 5621 | mbedtls_ssl_conf_max_tls_version( &conf, MBEDTLS_SSL_VERSION_TLS1_2 ); |
| 5622 | mbedtls_ssl_conf_min_tls_version( &conf, MBEDTLS_SSL_VERSION_TLS1_2 ); |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 5623 | |
| 5624 | mbedtls_ssl_conf_groups( &conf, iana_tls_group_list ); |
| 5625 | |
| 5626 | mbedtls_ssl_context ssl; |
| 5627 | mbedtls_ssl_init( &ssl ); |
Paul Elliott | 46a6c20 | 2021-12-09 18:16:13 +0000 | [diff] [blame] | 5628 | TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 ); |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 5629 | |
| 5630 | TEST_ASSERT( ssl.conf != NULL && ssl.conf->group_list != NULL ); |
| 5631 | |
| 5632 | TEST_EQUAL( ssl.conf->group_list[ARRAY_LENGTH( iana_tls_group_list ) - 1], MBEDTLS_SSL_IANA_TLS_GROUP_NONE ); |
| 5633 | |
| 5634 | for( size_t i = 0; i < ARRAY_LENGTH( iana_tls_group_list ); i++ ) |
| 5635 | TEST_EQUAL( iana_tls_group_list[i], ssl.conf->group_list[i] ); |
| 5636 | |
| 5637 | mbedtls_ssl_free( &ssl ); |
| 5638 | mbedtls_ssl_config_free( &conf ); |
| 5639 | } |
| 5640 | /* END_CASE */ |
Paul Elliott | b9af2db | 2022-03-09 15:34:37 +0000 | [diff] [blame] | 5641 | |
Andrzej Kurek | e8ad0d7 | 2022-06-11 09:43:45 -0400 | [diff] [blame] | 5642 | /* 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] | 5643 | void force_bad_session_id_len( ) |
| 5644 | { |
| 5645 | enum { BUFFSIZE = 1024 }; |
| 5646 | handshake_test_options options; |
| 5647 | mbedtls_endpoint client, server; |
| 5648 | log_pattern srv_pattern, cli_pattern; |
| 5649 | mbedtls_test_message_socket_context server_context, client_context; |
| 5650 | |
| 5651 | srv_pattern.pattern = cli_pattern.pattern = "cache did not store session"; |
| 5652 | srv_pattern.counter = 0; |
| 5653 | init_handshake_options( &options ); |
| 5654 | |
| 5655 | options.srv_log_obj = &srv_pattern; |
| 5656 | options.srv_log_fun = log_analyzer; |
| 5657 | |
| 5658 | USE_PSA_INIT( ); |
| 5659 | |
| 5660 | mbedtls_message_socket_init( &server_context ); |
| 5661 | mbedtls_message_socket_init( &client_context ); |
| 5662 | |
| 5663 | TEST_ASSERT( mbedtls_endpoint_init( &client, MBEDTLS_SSL_IS_CLIENT, |
| 5664 | &options, NULL, NULL, |
Andrzej Kurek | 626a931 | 2022-06-10 11:07:39 -0400 | [diff] [blame] | 5665 | NULL, NULL ) == 0 ); |
Andrzej Kurek | 514683a | 2022-06-10 10:33:05 -0400 | [diff] [blame] | 5666 | |
| 5667 | TEST_ASSERT( mbedtls_endpoint_init( &server, MBEDTLS_SSL_IS_SERVER, |
Andrzej Kurek | 626a931 | 2022-06-10 11:07:39 -0400 | [diff] [blame] | 5668 | &options, NULL, NULL, NULL, |
| 5669 | NULL ) == 0 ); |
Andrzej Kurek | 514683a | 2022-06-10 10:33:05 -0400 | [diff] [blame] | 5670 | |
| 5671 | mbedtls_debug_set_threshold( 1 ); |
| 5672 | mbedtls_ssl_conf_dbg( &server.conf, options.srv_log_fun, |
| 5673 | options.srv_log_obj ); |
| 5674 | |
| 5675 | TEST_ASSERT( mbedtls_mock_socket_connect( &(client.socket), |
| 5676 | &(server.socket), |
| 5677 | BUFFSIZE ) == 0 ); |
| 5678 | |
| 5679 | TEST_ASSERT( mbedtls_move_handshake_to_state( &(client.ssl), |
| 5680 | &(server.ssl), |
| 5681 | MBEDTLS_SSL_HANDSHAKE_WRAPUP ) |
| 5682 | == 0 ); |
| 5683 | /* Force a bad session_id_len that will be read by the server in |
| 5684 | * mbedtls_ssl_cache_set. */ |
| 5685 | server.ssl.session_negotiate->id_len = 33; |
| 5686 | if( options.cli_msg_len != 0 || options.srv_msg_len != 0 ) |
| 5687 | { |
| 5688 | /* Start data exchanging test */ |
| 5689 | TEST_ASSERT( mbedtls_exchange_data( &(client.ssl), options.cli_msg_len, |
| 5690 | options.expected_cli_fragments, |
| 5691 | &(server.ssl), options.srv_msg_len, |
| 5692 | options.expected_srv_fragments ) |
| 5693 | == 0 ); |
| 5694 | } |
Andrzej Kurek | 514683a | 2022-06-10 10:33:05 -0400 | [diff] [blame] | 5695 | |
| 5696 | /* Make sure that the cache did not store the session */ |
| 5697 | TEST_EQUAL( srv_pattern.counter, 1 ); |
| 5698 | exit: |
| 5699 | mbedtls_endpoint_free( &client, NULL ); |
| 5700 | mbedtls_endpoint_free( &server, NULL ); |
| 5701 | free_handshake_options( &options ); |
| 5702 | mbedtls_debug_set_threshold( 0 ); |
| 5703 | USE_PSA_DONE( ); |
| 5704 | } |
| 5705 | /* END_CASE */ |
| 5706 | |
Andrzej Kurek | ed4d217 | 2022-06-08 11:57:57 -0400 | [diff] [blame] | 5707 | /* 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] | 5708 | void cookie_parsing( data_t *cookie, int exp_ret ) |
| 5709 | { |
| 5710 | mbedtls_ssl_context ssl; |
| 5711 | mbedtls_ssl_config conf; |
| 5712 | size_t len; |
| 5713 | |
| 5714 | mbedtls_ssl_init( &ssl ); |
| 5715 | mbedtls_ssl_config_init( &conf ); |
| 5716 | TEST_EQUAL( mbedtls_ssl_config_defaults( &conf, MBEDTLS_SSL_IS_SERVER, |
| 5717 | MBEDTLS_SSL_TRANSPORT_DATAGRAM, |
| 5718 | MBEDTLS_SSL_PRESET_DEFAULT ), |
| 5719 | 0 ); |
| 5720 | |
| 5721 | TEST_EQUAL( mbedtls_ssl_setup( &ssl, &conf ), 0 ); |
Andrzej Kurek | 078e9bc | 2022-06-08 11:47:33 -0400 | [diff] [blame] | 5722 | TEST_EQUAL( mbedtls_ssl_check_dtls_clihlo_cookie( &ssl, ssl.cli_id, |
| 5723 | ssl.cli_id_len, |
| 5724 | cookie->x, cookie->len, |
| 5725 | ssl.out_buf, |
| 5726 | MBEDTLS_SSL_OUT_CONTENT_LEN, |
| 5727 | &len ), |
Andrzej Kurek | cfb0194 | 2022-06-06 13:08:23 -0400 | [diff] [blame] | 5728 | exp_ret ); |
| 5729 | |
| 5730 | mbedtls_ssl_free( &ssl ); |
| 5731 | mbedtls_ssl_config_free( &conf ); |
| 5732 | } |
| 5733 | /* END_CASE */ |
| 5734 | |
Paul Elliott | b9af2db | 2022-03-09 15:34:37 +0000 | [diff] [blame] | 5735 | /* BEGIN_CASE depends_on:MBEDTLS_TIMING_C:MBEDTLS_HAVE_TIME */ |
Paul Elliott | 42d5e51 | 2022-03-24 19:41:28 +0000 | [diff] [blame] | 5736 | void timing_final_delay_accessor( ) |
Paul Elliott | b9af2db | 2022-03-09 15:34:37 +0000 | [diff] [blame] | 5737 | { |
| 5738 | mbedtls_timing_delay_context delay_context; |
| 5739 | |
| 5740 | mbedtls_timing_set_delay( &delay_context, 50, 100 ); |
| 5741 | |
| 5742 | TEST_ASSERT( mbedtls_timing_get_final_delay( &delay_context ) == 100 ); |
| 5743 | } |
| 5744 | /* END_CASE */ |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 5745 | |
| 5746 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 5747 | void cid_sanity( ) |
| 5748 | { |
| 5749 | mbedtls_ssl_context ssl; |
| 5750 | mbedtls_ssl_config conf; |
| 5751 | |
| 5752 | unsigned char own_cid[MBEDTLS_SSL_CID_IN_LEN_MAX]; |
| 5753 | unsigned char test_cid[MBEDTLS_SSL_CID_IN_LEN_MAX]; |
| 5754 | int cid_enabled; |
| 5755 | size_t own_cid_len; |
| 5756 | |
| 5757 | mbedtls_test_rnd_std_rand( NULL, own_cid, sizeof( own_cid ) ); |
| 5758 | |
| 5759 | mbedtls_ssl_init( &ssl ); |
| 5760 | mbedtls_ssl_config_init( &conf ); |
| 5761 | |
| 5762 | TEST_ASSERT( mbedtls_ssl_config_defaults( &conf, |
| 5763 | MBEDTLS_SSL_IS_CLIENT, |
| 5764 | MBEDTLS_SSL_TRANSPORT_STREAM, |
| 5765 | MBEDTLS_SSL_PRESET_DEFAULT ) |
| 5766 | == 0 ); |
| 5767 | |
| 5768 | TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 ); |
| 5769 | |
| 5770 | /* Can't use CID functions with stream transport. */ |
| 5771 | TEST_ASSERT( mbedtls_ssl_set_cid( &ssl, MBEDTLS_SSL_CID_ENABLED, own_cid, |
| 5772 | sizeof( own_cid ) ) |
| 5773 | == MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5774 | |
| 5775 | TEST_ASSERT( mbedtls_ssl_get_own_cid( &ssl, &cid_enabled, test_cid, |
| 5776 | &own_cid_len ) |
| 5777 | == MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5778 | |
| 5779 | TEST_ASSERT( mbedtls_ssl_config_defaults( &conf, |
| 5780 | MBEDTLS_SSL_IS_CLIENT, |
| 5781 | MBEDTLS_SSL_TRANSPORT_DATAGRAM, |
| 5782 | MBEDTLS_SSL_PRESET_DEFAULT ) |
| 5783 | == 0 ); |
| 5784 | |
| 5785 | /* Attempt to set config cid size too big. */ |
| 5786 | TEST_ASSERT( mbedtls_ssl_conf_cid( &conf, MBEDTLS_SSL_CID_IN_LEN_MAX + 1, |
| 5787 | MBEDTLS_SSL_UNEXPECTED_CID_IGNORE ) |
| 5788 | == MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5789 | |
| 5790 | TEST_ASSERT( mbedtls_ssl_conf_cid( &conf, sizeof( own_cid ), |
| 5791 | MBEDTLS_SSL_UNEXPECTED_CID_IGNORE ) |
| 5792 | == 0 ); |
| 5793 | |
| 5794 | /* Attempt to set CID length not matching config. */ |
| 5795 | TEST_ASSERT( mbedtls_ssl_set_cid( &ssl, MBEDTLS_SSL_CID_ENABLED, own_cid, |
| 5796 | MBEDTLS_SSL_CID_IN_LEN_MAX - 1 ) |
| 5797 | == MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 5798 | |
| 5799 | TEST_ASSERT( mbedtls_ssl_set_cid( &ssl, MBEDTLS_SSL_CID_ENABLED, own_cid, |
| 5800 | sizeof( own_cid ) ) |
| 5801 | == 0 ); |
| 5802 | |
| 5803 | /* Test we get back what we put in. */ |
| 5804 | TEST_ASSERT( mbedtls_ssl_get_own_cid( &ssl, &cid_enabled, test_cid, |
| 5805 | &own_cid_len ) |
| 5806 | == 0 ); |
| 5807 | |
| 5808 | TEST_EQUAL( cid_enabled, MBEDTLS_SSL_CID_ENABLED ); |
| 5809 | ASSERT_COMPARE( own_cid, own_cid_len, test_cid, own_cid_len ); |
| 5810 | |
| 5811 | /* Test disabling works. */ |
| 5812 | TEST_ASSERT( mbedtls_ssl_set_cid( &ssl, MBEDTLS_SSL_CID_DISABLED, NULL, |
| 5813 | 0 ) |
| 5814 | == 0 ); |
| 5815 | |
| 5816 | TEST_ASSERT( mbedtls_ssl_get_own_cid( &ssl, &cid_enabled, test_cid, |
| 5817 | &own_cid_len ) |
| 5818 | == 0 ); |
| 5819 | |
| 5820 | TEST_EQUAL( cid_enabled, MBEDTLS_SSL_CID_DISABLED ); |
| 5821 | |
| 5822 | mbedtls_ssl_free( &ssl ); |
| 5823 | mbedtls_ssl_config_free( &conf ); |
| 5824 | } |
| 5825 | /* END_CASE */ |
| 5826 | |
Andrzej Kurek | 28f883e | 2022-04-08 07:55:27 -0400 | [diff] [blame] | 5827 | /* 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] | 5828 | void raw_key_agreement_fail( int bad_server_ecdhe_key ) |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 5829 | { |
| 5830 | enum { BUFFSIZE = 17000 }; |
| 5831 | mbedtls_endpoint client, server; |
| 5832 | mbedtls_psa_stats_t stats; |
Andrzej Kurek | 39d88d4 | 2022-03-31 06:30:54 -0400 | [diff] [blame] | 5833 | size_t free_slots_before = -1; |
Andrzej Kurek | 626a931 | 2022-06-10 11:07:39 -0400 | [diff] [blame] | 5834 | handshake_test_options options; |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 5835 | |
Andrzej Kurek | cc28e9a | 2022-03-08 18:36:35 -0500 | [diff] [blame] | 5836 | uint16_t iana_tls_group_list[] = { MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, |
| 5837 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE }; |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 5838 | USE_PSA_INIT( ); |
| 5839 | |
Andrzej Kurek | 626a931 | 2022-06-10 11:07:39 -0400 | [diff] [blame] | 5840 | init_handshake_options( &options ); |
| 5841 | options.pk_alg = MBEDTLS_PK_ECDSA; |
| 5842 | |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 5843 | /* Client side, force SECP256R1 to make one key bitflip fail |
Andrzej Kurek | 83e60ee | 2022-04-14 08:51:41 -0400 | [diff] [blame] | 5844 | * the raw key agreement. Flipping the first byte makes the |
| 5845 | * required 0x04 identifier invalid. */ |
Andrzej Kurek | 57f58b0 | 2022-04-08 08:10:53 -0400 | [diff] [blame] | 5846 | TEST_EQUAL( mbedtls_endpoint_init( &client, MBEDTLS_SSL_IS_CLIENT, |
Andrzej Kurek | 626a931 | 2022-06-10 11:07:39 -0400 | [diff] [blame] | 5847 | &options, NULL, NULL, |
Andrzej Kurek | 57f58b0 | 2022-04-08 08:10:53 -0400 | [diff] [blame] | 5848 | NULL, iana_tls_group_list ), 0 ); |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 5849 | |
| 5850 | /* Server side */ |
Andrzej Kurek | 57f58b0 | 2022-04-08 08:10:53 -0400 | [diff] [blame] | 5851 | TEST_EQUAL( mbedtls_endpoint_init( &server, MBEDTLS_SSL_IS_SERVER, |
Andrzej Kurek | 626a931 | 2022-06-10 11:07:39 -0400 | [diff] [blame] | 5852 | &options, NULL, NULL, |
| 5853 | NULL, NULL ), 0 ); |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 5854 | |
Andrzej Kurek | 57f58b0 | 2022-04-08 08:10:53 -0400 | [diff] [blame] | 5855 | TEST_EQUAL( mbedtls_mock_socket_connect( &(client.socket), |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 5856 | &(server.socket), |
Andrzej Kurek | 57f58b0 | 2022-04-08 08:10:53 -0400 | [diff] [blame] | 5857 | BUFFSIZE ), 0 ); |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 5858 | |
Andrzej Kurek | 57f58b0 | 2022-04-08 08:10:53 -0400 | [diff] [blame] | 5859 | TEST_EQUAL( mbedtls_move_handshake_to_state( &(client.ssl), |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 5860 | &(server.ssl), |
| 5861 | MBEDTLS_SSL_CLIENT_KEY_EXCHANGE ) |
Andrzej Kurek | 57f58b0 | 2022-04-08 08:10:53 -0400 | [diff] [blame] | 5862 | , 0 ); |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 5863 | |
Andrzej Kurek | 39d88d4 | 2022-03-31 06:30:54 -0400 | [diff] [blame] | 5864 | mbedtls_psa_get_stats( &stats ); |
| 5865 | /* Save the number of slots in use up to this point. |
| 5866 | * With PSA, one can be used for the ECDH private key. */ |
| 5867 | free_slots_before = stats.empty_slots; |
Andrzej Kurek | cb33bc5 | 2022-03-31 07:17:18 -0400 | [diff] [blame] | 5868 | |
Gilles Peskine | b4f874d | 2022-04-08 16:48:09 -0400 | [diff] [blame] | 5869 | if( bad_server_ecdhe_key ) |
| 5870 | { |
| 5871 | /* Force a simulated bitflip in the server key. to make the |
| 5872 | * raw key agreement in ssl_write_client_key_exchange fail. */ |
| 5873 | (client.ssl).handshake->ecdh_psa_peerkey[0] ^= 0x02; |
| 5874 | } |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 5875 | |
Gilles Peskine | b4f874d | 2022-04-08 16:48:09 -0400 | [diff] [blame] | 5876 | TEST_EQUAL( mbedtls_move_handshake_to_state( &(client.ssl), |
| 5877 | &(server.ssl), |
| 5878 | MBEDTLS_SSL_HANDSHAKE_OVER ), |
| 5879 | bad_server_ecdhe_key ? MBEDTLS_ERR_SSL_HW_ACCEL_FAILED : 0 ); |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 5880 | |
| 5881 | mbedtls_psa_get_stats( &stats ); |
| 5882 | |
Gilles Peskine | b4f874d | 2022-04-08 16:48:09 -0400 | [diff] [blame] | 5883 | /* Make sure that the key slot is already destroyed in case of failure, |
| 5884 | * without waiting to close the connection. */ |
| 5885 | if( bad_server_ecdhe_key ) |
| 5886 | TEST_EQUAL( free_slots_before, stats.empty_slots ); |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 5887 | |
| 5888 | exit: |
Andrzej Kurek | 28f883e | 2022-04-08 07:55:27 -0400 | [diff] [blame] | 5889 | mbedtls_endpoint_free( &client, NULL ); |
| 5890 | mbedtls_endpoint_free( &server, NULL ); |
Andrzej Kurek | 626a931 | 2022-06-10 11:07:39 -0400 | [diff] [blame] | 5891 | free_handshake_options( &options ); |
Andrzej Kurek | cb33bc5 | 2022-03-31 07:17:18 -0400 | [diff] [blame] | 5892 | |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 5893 | USE_PSA_DONE( ); |
| 5894 | } |
| 5895 | /* END_CASE */ |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 5896 | /* 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 */ |
| 5897 | void tls13_server_certificate_msg_invalid_vector_len( ) |
| 5898 | { |
| 5899 | int ret = -1; |
| 5900 | mbedtls_endpoint client_ep, server_ep; |
| 5901 | unsigned char *buf, *end; |
| 5902 | size_t buf_len; |
| 5903 | int step = 0; |
| 5904 | int expected_result; |
Ronald Cron | e7b9b6b | 2022-06-10 17:24:31 +0200 | [diff] [blame] | 5905 | mbedtls_ssl_chk_buf_ptr_args expected_chk_buf_ptr_args; |
Paul Elliott | 9a8d784 | 2022-07-10 12:48:57 +0100 | [diff] [blame^] | 5906 | handshake_test_options client_options; |
| 5907 | handshake_test_options server_options; |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 5908 | |
| 5909 | /* |
| 5910 | * Test set-up |
| 5911 | */ |
| 5912 | USE_PSA_INIT( ); |
| 5913 | |
Paul Elliott | 9a8d784 | 2022-07-10 12:48:57 +0100 | [diff] [blame^] | 5914 | init_handshake_options( &client_options ); |
| 5915 | client_options.pk_alg = MBEDTLS_PK_ECDSA; |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 5916 | ret = mbedtls_endpoint_init( &client_ep, MBEDTLS_SSL_IS_CLIENT, |
Paul Elliott | 9a8d784 | 2022-07-10 12:48:57 +0100 | [diff] [blame^] | 5917 | &client_options, NULL, NULL, NULL, NULL ); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 5918 | TEST_EQUAL( ret, 0 ); |
| 5919 | |
Paul Elliott | 9a8d784 | 2022-07-10 12:48:57 +0100 | [diff] [blame^] | 5920 | init_handshake_options( &server_options ); |
| 5921 | server_options.pk_alg = MBEDTLS_PK_ECDSA; |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 5922 | ret = mbedtls_endpoint_init( &server_ep, MBEDTLS_SSL_IS_SERVER, |
Paul Elliott | 9a8d784 | 2022-07-10 12:48:57 +0100 | [diff] [blame^] | 5923 | &server_options, NULL, NULL, NULL, NULL ); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 5924 | TEST_EQUAL( ret, 0 ); |
| 5925 | |
| 5926 | ret = mbedtls_mock_socket_connect( &(client_ep.socket), |
| 5927 | &(server_ep.socket), 1024 ); |
| 5928 | TEST_EQUAL( ret, 0 ); |
| 5929 | |
| 5930 | while( 1 ) |
| 5931 | { |
| 5932 | mbedtls_test_set_step( ++step ); |
| 5933 | |
| 5934 | ret = mbedtls_move_handshake_to_state( &(server_ep.ssl), |
| 5935 | &(client_ep.ssl), |
| 5936 | MBEDTLS_SSL_CERTIFICATE_VERIFY ); |
| 5937 | TEST_EQUAL( ret, 0 ); |
| 5938 | |
| 5939 | ret = mbedtls_ssl_flush_output( &(server_ep.ssl) ); |
| 5940 | TEST_EQUAL( ret, 0 ); |
| 5941 | |
| 5942 | ret = mbedtls_move_handshake_to_state( &(client_ep.ssl), |
| 5943 | &(server_ep.ssl), |
| 5944 | MBEDTLS_SSL_SERVER_CERTIFICATE ); |
| 5945 | TEST_EQUAL( ret, 0 ); |
| 5946 | |
| 5947 | ret = mbedtls_ssl_tls13_fetch_handshake_msg( &(client_ep.ssl), |
| 5948 | MBEDTLS_SSL_HS_CERTIFICATE, |
| 5949 | &buf, &buf_len ); |
| 5950 | TEST_EQUAL( ret, 0 ); |
| 5951 | |
| 5952 | end = buf + buf_len; |
| 5953 | |
| 5954 | /* |
| 5955 | * Tweak server Certificate message and parse it. |
| 5956 | */ |
| 5957 | |
| 5958 | ret = tweak_tls13_certificate_msg_vector_len( |
Ronald Cron | e7b9b6b | 2022-06-10 17:24:31 +0200 | [diff] [blame] | 5959 | buf, &end, step, &expected_result, &expected_chk_buf_ptr_args ); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 5960 | |
| 5961 | if( ret != 0 ) |
| 5962 | break; |
| 5963 | |
| 5964 | ret = mbedtls_ssl_tls13_parse_certificate( &(client_ep.ssl), buf, end ); |
| 5965 | TEST_EQUAL( ret, expected_result ); |
| 5966 | |
Ronald Cron | e7b9b6b | 2022-06-10 17:24:31 +0200 | [diff] [blame] | 5967 | TEST_ASSERT( mbedtls_ssl_cmp_chk_buf_ptr_fail_args( |
| 5968 | &expected_chk_buf_ptr_args ) == 0 ); |
| 5969 | |
| 5970 | mbedtls_ssl_reset_chk_buf_ptr_fail_args( ); |
| 5971 | |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 5972 | ret = mbedtls_ssl_session_reset( &(client_ep.ssl) ); |
| 5973 | TEST_EQUAL( ret, 0 ); |
| 5974 | |
| 5975 | ret = mbedtls_ssl_session_reset( &(server_ep.ssl) ); |
| 5976 | TEST_EQUAL( ret, 0 ); |
| 5977 | } |
| 5978 | |
| 5979 | exit: |
Ronald Cron | e7b9b6b | 2022-06-10 17:24:31 +0200 | [diff] [blame] | 5980 | mbedtls_ssl_reset_chk_buf_ptr_fail_args( ); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 5981 | mbedtls_endpoint_free( &client_ep, NULL ); |
| 5982 | mbedtls_endpoint_free( &server_ep, NULL ); |
Paul Elliott | 9a8d784 | 2022-07-10 12:48:57 +0100 | [diff] [blame^] | 5983 | free_handshake_options( &client_options ); |
| 5984 | free_handshake_options( &server_options ); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 5985 | USE_PSA_DONE( ); |
| 5986 | } |
| 5987 | /* END_CASE */ |