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