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