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