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