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