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