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