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