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