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 | |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1199 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1200 | psa_key_type_t key_type; |
| 1201 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1202 | psa_algorithm_t alg; |
| 1203 | size_t key_bits; |
| 1204 | psa_status_t status; |
| 1205 | #endif |
| 1206 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1207 | size_t keylen, maclen, ivlen; |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 1208 | unsigned char *key0 = NULL, *key1 = NULL; |
Paul Elliott | 6f1eda7 | 2020-06-11 20:22:00 +0100 | [diff] [blame] | 1209 | unsigned char *md0 = NULL, *md1 = NULL; |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1210 | unsigned char iv_enc[16], iv_dec[16]; |
| 1211 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1212 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 1213 | unsigned char cid0[ SSL_CID_LEN_MIN ]; |
| 1214 | unsigned char cid1[ SSL_CID_LEN_MIN ]; |
| 1215 | |
Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 1216 | mbedtls_test_rnd_std_rand( NULL, cid0, sizeof( cid0 ) ); |
| 1217 | mbedtls_test_rnd_std_rand( NULL, cid1, sizeof( cid1 ) ); |
Hanno Becker | 43c24b8 | 2019-05-01 09:45:57 +0100 | [diff] [blame] | 1218 | #else |
| 1219 | ((void) cid0_len); |
| 1220 | ((void) cid1_len); |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1221 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 1222 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1223 | maclen = 0; |
| 1224 | |
| 1225 | /* Pick cipher */ |
| 1226 | cipher_info = mbedtls_cipher_info_from_type( cipher_type ); |
| 1227 | CHK( cipher_info != NULL ); |
| 1228 | CHK( cipher_info->iv_size <= 16 ); |
| 1229 | CHK( cipher_info->key_bitlen % 8 == 0 ); |
| 1230 | |
| 1231 | /* Pick keys */ |
| 1232 | keylen = cipher_info->key_bitlen / 8; |
Hanno Becker | 78d1f70 | 2019-04-05 09:56:10 +0100 | [diff] [blame] | 1233 | /* Allocate `keylen + 1` bytes to ensure that we get |
| 1234 | * a non-NULL pointers from `mbedtls_calloc` even if |
| 1235 | * `keylen == 0` in the case of the NULL cipher. */ |
| 1236 | CHK( ( key0 = mbedtls_calloc( 1, keylen + 1 ) ) != NULL ); |
| 1237 | CHK( ( key1 = mbedtls_calloc( 1, keylen + 1 ) ) != NULL ); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1238 | memset( key0, 0x1, keylen ); |
| 1239 | memset( key1, 0x2, keylen ); |
| 1240 | |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1241 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1242 | /* Setup cipher contexts */ |
| 1243 | CHK( mbedtls_cipher_setup( &t_in->cipher_ctx_enc, cipher_info ) == 0 ); |
| 1244 | CHK( mbedtls_cipher_setup( &t_in->cipher_ctx_dec, cipher_info ) == 0 ); |
| 1245 | CHK( mbedtls_cipher_setup( &t_out->cipher_ctx_enc, cipher_info ) == 0 ); |
| 1246 | CHK( mbedtls_cipher_setup( &t_out->cipher_ctx_dec, cipher_info ) == 0 ); |
| 1247 | |
| 1248 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
| 1249 | if( cipher_info->mode == MBEDTLS_MODE_CBC ) |
| 1250 | { |
| 1251 | CHK( mbedtls_cipher_set_padding_mode( &t_in->cipher_ctx_enc, |
| 1252 | MBEDTLS_PADDING_NONE ) == 0 ); |
| 1253 | CHK( mbedtls_cipher_set_padding_mode( &t_in->cipher_ctx_dec, |
| 1254 | MBEDTLS_PADDING_NONE ) == 0 ); |
| 1255 | CHK( mbedtls_cipher_set_padding_mode( &t_out->cipher_ctx_enc, |
| 1256 | MBEDTLS_PADDING_NONE ) == 0 ); |
| 1257 | CHK( mbedtls_cipher_set_padding_mode( &t_out->cipher_ctx_dec, |
| 1258 | MBEDTLS_PADDING_NONE ) == 0 ); |
| 1259 | } |
| 1260 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
| 1261 | |
| 1262 | CHK( mbedtls_cipher_setkey( &t_in->cipher_ctx_enc, key0, |
| 1263 | keylen << 3, MBEDTLS_ENCRYPT ) == 0 ); |
| 1264 | CHK( mbedtls_cipher_setkey( &t_in->cipher_ctx_dec, key1, |
| 1265 | keylen << 3, MBEDTLS_DECRYPT ) == 0 ); |
| 1266 | CHK( mbedtls_cipher_setkey( &t_out->cipher_ctx_enc, key1, |
| 1267 | keylen << 3, MBEDTLS_ENCRYPT ) == 0 ); |
| 1268 | CHK( mbedtls_cipher_setkey( &t_out->cipher_ctx_dec, key0, |
| 1269 | keylen << 3, MBEDTLS_DECRYPT ) == 0 ); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1270 | #endif |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1271 | |
| 1272 | /* Setup MAC contexts */ |
Hanno Becker | fd86ca8 | 2020-11-30 08:54:23 +0000 | [diff] [blame] | 1273 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1274 | if( cipher_info->mode == MBEDTLS_MODE_CBC || |
| 1275 | cipher_info->mode == MBEDTLS_MODE_STREAM ) |
| 1276 | { |
| 1277 | mbedtls_md_info_t const *md_info; |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1278 | |
| 1279 | /* Pick hash */ |
| 1280 | md_info = mbedtls_md_info_from_type( hash_id ); |
| 1281 | CHK( md_info != NULL ); |
| 1282 | |
| 1283 | /* Pick hash keys */ |
| 1284 | maclen = mbedtls_md_get_size( md_info ); |
Hanno Becker | 3ee5421 | 2019-04-04 16:31:26 +0100 | [diff] [blame] | 1285 | CHK( ( md0 = mbedtls_calloc( 1, maclen ) ) != NULL ); |
| 1286 | CHK( ( md1 = mbedtls_calloc( 1, maclen ) ) != NULL ); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1287 | memset( md0, 0x5, maclen ); |
| 1288 | memset( md1, 0x6, maclen ); |
| 1289 | |
| 1290 | CHK( mbedtls_md_setup( &t_out->md_ctx_enc, md_info, 1 ) == 0 ); |
| 1291 | CHK( mbedtls_md_setup( &t_out->md_ctx_dec, md_info, 1 ) == 0 ); |
| 1292 | CHK( mbedtls_md_setup( &t_in->md_ctx_enc, md_info, 1 ) == 0 ); |
| 1293 | CHK( mbedtls_md_setup( &t_in->md_ctx_dec, md_info, 1 ) == 0 ); |
| 1294 | |
Mateusz Starzyk | 06b07fb | 2021-02-18 13:55:21 +0100 | [diff] [blame] | 1295 | CHK( mbedtls_md_hmac_starts( &t_in->md_ctx_enc, |
| 1296 | md0, maclen ) == 0 ); |
| 1297 | CHK( mbedtls_md_hmac_starts( &t_in->md_ctx_dec, |
| 1298 | md1, maclen ) == 0 ); |
| 1299 | CHK( mbedtls_md_hmac_starts( &t_out->md_ctx_enc, |
| 1300 | md1, maclen ) == 0 ); |
| 1301 | CHK( mbedtls_md_hmac_starts( &t_out->md_ctx_dec, |
| 1302 | md0, maclen ) == 0 ); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1303 | } |
| 1304 | #else |
| 1305 | ((void) hash_id); |
Hanno Becker | fd86ca8 | 2020-11-30 08:54:23 +0000 | [diff] [blame] | 1306 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1307 | |
| 1308 | |
| 1309 | /* Pick IV's (regardless of whether they |
| 1310 | * are being used by the transform). */ |
| 1311 | ivlen = cipher_info->iv_size; |
| 1312 | memset( iv_enc, 0x3, sizeof( iv_enc ) ); |
| 1313 | memset( iv_dec, 0x4, sizeof( iv_dec ) ); |
| 1314 | |
| 1315 | /* |
| 1316 | * Setup transforms |
| 1317 | */ |
| 1318 | |
Jaeden Amero | 2de07f1 | 2019-06-05 13:32:08 +0100 | [diff] [blame] | 1319 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \ |
Hanno Becker | fd86ca8 | 2020-11-30 08:54:23 +0000 | [diff] [blame] | 1320 | defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1321 | t_out->encrypt_then_mac = etm; |
| 1322 | t_in->encrypt_then_mac = etm; |
| 1323 | #else |
| 1324 | ((void) etm); |
| 1325 | #endif |
| 1326 | |
| 1327 | t_out->minor_ver = ver; |
| 1328 | t_in->minor_ver = ver; |
| 1329 | t_out->ivlen = ivlen; |
| 1330 | t_in->ivlen = ivlen; |
| 1331 | |
| 1332 | switch( cipher_info->mode ) |
| 1333 | { |
| 1334 | case MBEDTLS_MODE_GCM: |
| 1335 | case MBEDTLS_MODE_CCM: |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1336 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Hanno Becker | e683287 | 2020-05-28 08:29:58 +0100 | [diff] [blame] | 1337 | if( ver == MBEDTLS_SSL_MINOR_VERSION_4 ) |
| 1338 | { |
| 1339 | t_out->fixed_ivlen = 12; |
| 1340 | t_in->fixed_ivlen = 12; |
| 1341 | } |
| 1342 | else |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1343 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | e683287 | 2020-05-28 08:29:58 +0100 | [diff] [blame] | 1344 | { |
| 1345 | t_out->fixed_ivlen = 4; |
| 1346 | t_in->fixed_ivlen = 4; |
| 1347 | } |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1348 | t_out->maclen = 0; |
| 1349 | t_in->maclen = 0; |
| 1350 | switch( tag_mode ) |
| 1351 | { |
| 1352 | case 0: /* Full tag */ |
| 1353 | t_out->taglen = 16; |
| 1354 | t_in->taglen = 16; |
| 1355 | break; |
| 1356 | case 1: /* Partial tag */ |
| 1357 | t_out->taglen = 8; |
| 1358 | t_in->taglen = 8; |
| 1359 | break; |
| 1360 | default: |
Paul Elliott | c7b5374 | 2021-02-03 13:18:33 +0000 | [diff] [blame] | 1361 | ret = 1; |
| 1362 | goto cleanup; |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1363 | } |
| 1364 | break; |
| 1365 | |
| 1366 | case MBEDTLS_MODE_CHACHAPOLY: |
| 1367 | t_out->fixed_ivlen = 12; |
| 1368 | t_in->fixed_ivlen = 12; |
| 1369 | t_out->maclen = 0; |
| 1370 | t_in->maclen = 0; |
| 1371 | switch( tag_mode ) |
| 1372 | { |
| 1373 | case 0: /* Full tag */ |
| 1374 | t_out->taglen = 16; |
| 1375 | t_in->taglen = 16; |
| 1376 | break; |
| 1377 | case 1: /* Partial tag */ |
| 1378 | t_out->taglen = 8; |
| 1379 | t_in->taglen = 8; |
| 1380 | break; |
| 1381 | default: |
Paul Elliott | c7b5374 | 2021-02-03 13:18:33 +0000 | [diff] [blame] | 1382 | ret = 1; |
| 1383 | goto cleanup; |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1384 | } |
| 1385 | break; |
| 1386 | |
| 1387 | case MBEDTLS_MODE_STREAM: |
| 1388 | case MBEDTLS_MODE_CBC: |
| 1389 | t_out->fixed_ivlen = 0; /* redundant, must be 0 */ |
| 1390 | t_in->fixed_ivlen = 0; /* redundant, must be 0 */ |
| 1391 | t_out->taglen = 0; |
| 1392 | t_in->taglen = 0; |
| 1393 | switch( tag_mode ) |
| 1394 | { |
| 1395 | case 0: /* Full tag */ |
| 1396 | t_out->maclen = maclen; |
| 1397 | t_in->maclen = maclen; |
| 1398 | break; |
| 1399 | case 1: /* Partial tag */ |
| 1400 | t_out->maclen = 10; |
| 1401 | t_in->maclen = 10; |
| 1402 | break; |
| 1403 | default: |
Paul Elliott | c7b5374 | 2021-02-03 13:18:33 +0000 | [diff] [blame] | 1404 | ret = 1; |
| 1405 | goto cleanup; |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1406 | } |
| 1407 | break; |
| 1408 | default: |
Paul Elliott | c7b5374 | 2021-02-03 13:18:33 +0000 | [diff] [blame] | 1409 | ret = 1; |
| 1410 | goto cleanup; |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1411 | break; |
| 1412 | } |
| 1413 | |
| 1414 | /* Setup IV's */ |
| 1415 | |
| 1416 | memcpy( &t_in->iv_dec, iv_dec, sizeof( iv_dec ) ); |
| 1417 | memcpy( &t_in->iv_enc, iv_enc, sizeof( iv_enc ) ); |
| 1418 | memcpy( &t_out->iv_dec, iv_enc, sizeof( iv_enc ) ); |
| 1419 | memcpy( &t_out->iv_enc, iv_dec, sizeof( iv_dec ) ); |
| 1420 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1421 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 1422 | /* Add CID */ |
| 1423 | memcpy( &t_in->in_cid, cid0, cid0_len ); |
| 1424 | memcpy( &t_in->out_cid, cid1, cid1_len ); |
| 1425 | t_in->in_cid_len = cid0_len; |
| 1426 | t_in->out_cid_len = cid1_len; |
| 1427 | memcpy( &t_out->in_cid, cid1, cid1_len ); |
| 1428 | memcpy( &t_out->out_cid, cid0, cid0_len ); |
| 1429 | t_out->in_cid_len = cid1_len; |
| 1430 | t_out->out_cid_len = cid0_len; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1431 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 1432 | |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1433 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Przemyslaw Stekiel | f57b456 | 2022-01-25 00:04:18 +0100 | [diff] [blame] | 1434 | status = mbedtls_ssl_cipher_to_psa( cipher_type, |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1435 | t_in->taglen, |
| 1436 | &alg, |
| 1437 | &key_type, |
| 1438 | &key_bits ); |
| 1439 | |
| 1440 | if ( status != PSA_SUCCESS) |
| 1441 | { |
| 1442 | ret = psa_status_to_mbedtls( status ); |
| 1443 | goto cleanup; |
| 1444 | } |
| 1445 | |
| 1446 | t_in->psa_alg = alg; |
| 1447 | t_out->psa_alg = alg; |
| 1448 | |
| 1449 | if ( alg != MBEDTLS_SSL_NULL_CIPHER ) |
| 1450 | { |
Przemyslaw Stekiel | f4ca3f0 | 2022-01-25 00:25:59 +0100 | [diff] [blame] | 1451 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1452 | psa_set_key_algorithm( &attributes, alg ); |
| 1453 | psa_set_key_type( &attributes, key_type ); |
| 1454 | |
| 1455 | status = psa_import_key( &attributes, |
| 1456 | key0, |
| 1457 | PSA_BITS_TO_BYTES( key_bits ), |
| 1458 | &t_in->psa_key_enc ); |
| 1459 | |
| 1460 | if ( status != PSA_SUCCESS) |
| 1461 | { |
| 1462 | ret = psa_status_to_mbedtls( status ); |
| 1463 | goto cleanup; |
| 1464 | } |
| 1465 | |
| 1466 | status = psa_import_key( &attributes, |
| 1467 | key1, |
| 1468 | PSA_BITS_TO_BYTES( key_bits ), |
Przemyslaw Stekiel | f4ca3f0 | 2022-01-25 00:25:59 +0100 | [diff] [blame] | 1469 | &t_out->psa_key_enc ); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1470 | |
| 1471 | if ( status != PSA_SUCCESS) |
| 1472 | { |
| 1473 | ret = psa_status_to_mbedtls( status ); |
| 1474 | goto cleanup; |
| 1475 | } |
| 1476 | |
Przemyslaw Stekiel | f4ca3f0 | 2022-01-25 00:25:59 +0100 | [diff] [blame] | 1477 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 1478 | |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1479 | status = psa_import_key( &attributes, |
| 1480 | key1, |
| 1481 | PSA_BITS_TO_BYTES( key_bits ), |
Przemyslaw Stekiel | f4ca3f0 | 2022-01-25 00:25:59 +0100 | [diff] [blame] | 1482 | &t_in->psa_key_dec ); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1483 | |
| 1484 | if ( status != PSA_SUCCESS) |
| 1485 | { |
| 1486 | ret = psa_status_to_mbedtls( status ); |
| 1487 | goto cleanup; |
| 1488 | } |
| 1489 | |
| 1490 | status = psa_import_key( &attributes, |
| 1491 | key0, |
| 1492 | PSA_BITS_TO_BYTES( key_bits ), |
| 1493 | &t_out->psa_key_dec ); |
| 1494 | |
| 1495 | if ( status != PSA_SUCCESS) |
| 1496 | { |
| 1497 | ret = psa_status_to_mbedtls( status ); |
| 1498 | goto cleanup; |
| 1499 | } |
| 1500 | } |
| 1501 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1502 | |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 1503 | cleanup: |
| 1504 | |
Hanno Becker | 3ee5421 | 2019-04-04 16:31:26 +0100 | [diff] [blame] | 1505 | mbedtls_free( key0 ); |
| 1506 | mbedtls_free( key1 ); |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 1507 | |
Paul Elliott | 6f1eda7 | 2020-06-11 20:22:00 +0100 | [diff] [blame] | 1508 | mbedtls_free( md0 ); |
| 1509 | mbedtls_free( md1 ); |
| 1510 | |
Hanno Becker | a5780f1 | 2019-04-05 09:55:37 +0100 | [diff] [blame] | 1511 | return( ret ); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1512 | } |
| 1513 | |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1514 | /* |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 1515 | * Populate a session structure for serialization tests. |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1516 | * Choose dummy values, mostly non-0 to distinguish from the init default. |
| 1517 | */ |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 1518 | static int ssl_populate_session_tls12( mbedtls_ssl_session *session, |
| 1519 | int ticket_len, |
| 1520 | const char *crt_file ) |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1521 | { |
| 1522 | #if defined(MBEDTLS_HAVE_TIME) |
| 1523 | session->start = mbedtls_time( NULL ) - 42; |
| 1524 | #endif |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 1525 | session->minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1526 | session->ciphersuite = 0xabcd; |
| 1527 | session->compression = 1; |
| 1528 | session->id_len = sizeof( session->id ); |
| 1529 | memset( session->id, 66, session->id_len ); |
Manuel Pégourié-Gonnard | 220403b | 2019-05-24 09:54:21 +0200 | [diff] [blame] | 1530 | memset( session->master, 17, sizeof( session->master ) ); |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1531 | |
Manuel Pégourié-Gonnard | 1f6033a | 2019-05-24 10:17:52 +0200 | [diff] [blame] | 1532 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(MBEDTLS_FS_IO) |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 1533 | if( crt_file != NULL && strlen( crt_file ) != 0 ) |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1534 | { |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 1535 | mbedtls_x509_crt tmp_crt; |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1536 | int ret; |
Manuel Pégourié-Gonnard | 6b84070 | 2019-05-24 09:40:17 +0200 | [diff] [blame] | 1537 | |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 1538 | mbedtls_x509_crt_init( &tmp_crt ); |
| 1539 | ret = mbedtls_x509_crt_parse_file( &tmp_crt, crt_file ); |
| 1540 | if( ret != 0 ) |
| 1541 | return( ret ); |
| 1542 | |
| 1543 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 1544 | /* Move temporary CRT. */ |
Manuel Pégourié-Gonnard | 6b84070 | 2019-05-24 09:40:17 +0200 | [diff] [blame] | 1545 | session->peer_cert = mbedtls_calloc( 1, sizeof( *session->peer_cert ) ); |
| 1546 | if( session->peer_cert == NULL ) |
| 1547 | return( -1 ); |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 1548 | *session->peer_cert = tmp_crt; |
| 1549 | memset( &tmp_crt, 0, sizeof( tmp_crt ) ); |
| 1550 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 1551 | /* Calculate digest of temporary CRT. */ |
| 1552 | session->peer_cert_digest = |
| 1553 | mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ); |
| 1554 | if( session->peer_cert_digest == NULL ) |
| 1555 | return( -1 ); |
| 1556 | ret = mbedtls_md( mbedtls_md_info_from_type( |
| 1557 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ), |
| 1558 | tmp_crt.raw.p, tmp_crt.raw.len, |
| 1559 | session->peer_cert_digest ); |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1560 | if( ret != 0 ) |
| 1561 | return( ret ); |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 1562 | session->peer_cert_digest_type = |
| 1563 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE; |
| 1564 | session->peer_cert_digest_len = |
| 1565 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN; |
| 1566 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 1567 | |
| 1568 | mbedtls_x509_crt_free( &tmp_crt ); |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1569 | } |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 1570 | #else /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_FS_IO */ |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1571 | (void) crt_file; |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 1572 | #endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_FS_IO */ |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1573 | session->verify_result = 0xdeadbeef; |
| 1574 | |
| 1575 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 1576 | if( ticket_len != 0 ) |
| 1577 | { |
| 1578 | session->ticket = mbedtls_calloc( 1, ticket_len ); |
Manuel Pégourié-Gonnard | 220403b | 2019-05-24 09:54:21 +0200 | [diff] [blame] | 1579 | if( session->ticket == NULL ) |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1580 | return( -1 ); |
| 1581 | memset( session->ticket, 33, ticket_len ); |
| 1582 | } |
| 1583 | session->ticket_len = ticket_len; |
| 1584 | session->ticket_lifetime = 86401; |
| 1585 | #else |
| 1586 | (void) ticket_len; |
| 1587 | #endif |
| 1588 | |
| 1589 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 1590 | session->mfl_code = 1; |
| 1591 | #endif |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1592 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 1593 | session->encrypt_then_mac = 1; |
| 1594 | #endif |
| 1595 | |
| 1596 | return( 0 ); |
| 1597 | } |
| 1598 | |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1599 | /* |
| 1600 | * Perform data exchanging between \p ssl_1 and \p ssl_2 and check if the |
| 1601 | * message was sent in the correct number of fragments. |
| 1602 | * |
| 1603 | * /p ssl_1 and /p ssl_2 Endpoints represented by mbedtls_ssl_context. Both |
| 1604 | * of them must be initialized and connected beforehand. |
| 1605 | * /p msg_len_1 and /p msg_len_2 specify the size of the message to send. |
| 1606 | * /p expected_fragments_1 and /p expected_fragments_2 determine in how many |
| 1607 | * fragments the message should be sent. |
| 1608 | * expected_fragments is 0: can be used for DTLS testing while the message |
| 1609 | * size is larger than MFL. In that case the message |
| 1610 | * cannot be fragmented and sent to the second endpoint. |
| 1611 | * This value can be used for negative tests. |
| 1612 | * expected_fragments is 1: can be used for TLS/DTLS testing while the |
| 1613 | * message size is below MFL |
| 1614 | * expected_fragments > 1: can be used for TLS testing while the message |
| 1615 | * size is larger than MFL |
| 1616 | * |
| 1617 | * \retval 0 on success, otherwise error code. |
| 1618 | */ |
| 1619 | int mbedtls_exchange_data( mbedtls_ssl_context *ssl_1, |
| 1620 | int msg_len_1, const int expected_fragments_1, |
| 1621 | mbedtls_ssl_context *ssl_2, |
| 1622 | int msg_len_2, const int expected_fragments_2 ) |
| 1623 | { |
| 1624 | unsigned char *msg_buf_1 = malloc( msg_len_1 ); |
| 1625 | unsigned char *msg_buf_2 = malloc( msg_len_2 ); |
| 1626 | unsigned char *in_buf_1 = malloc( msg_len_2 ); |
| 1627 | unsigned char *in_buf_2 = malloc( msg_len_1 ); |
| 1628 | int msg_type, ret = -1; |
| 1629 | |
| 1630 | /* Perform this test with two message types. At first use a message |
| 1631 | * consisting of only 0x00 for the client and only 0xFF for the server. |
| 1632 | * At the second time use message with generated data */ |
| 1633 | for( msg_type = 0; msg_type < 2; msg_type++ ) |
| 1634 | { |
| 1635 | int written_1 = 0; |
| 1636 | int written_2 = 0; |
| 1637 | int read_1 = 0; |
| 1638 | int read_2 = 0; |
| 1639 | int fragments_1 = 0; |
| 1640 | int fragments_2 = 0; |
| 1641 | |
| 1642 | if( msg_type == 0 ) |
| 1643 | { |
| 1644 | memset( msg_buf_1, 0x00, msg_len_1 ); |
| 1645 | memset( msg_buf_2, 0xff, msg_len_2 ); |
| 1646 | } |
| 1647 | else |
| 1648 | { |
| 1649 | int i, j = 0; |
| 1650 | for( i = 0; i < msg_len_1; i++ ) |
| 1651 | { |
| 1652 | msg_buf_1[i] = j++ & 0xFF; |
| 1653 | } |
| 1654 | for( i = 0; i < msg_len_2; i++ ) |
| 1655 | { |
| 1656 | msg_buf_2[i] = ( j -= 5 ) & 0xFF; |
| 1657 | } |
| 1658 | } |
| 1659 | |
| 1660 | while( read_1 < msg_len_2 || read_2 < msg_len_1 ) |
| 1661 | { |
| 1662 | /* ssl_1 sending */ |
| 1663 | if( msg_len_1 > written_1 ) |
| 1664 | { |
| 1665 | ret = mbedtls_ssl_write_fragment( ssl_1, msg_buf_1, |
| 1666 | msg_len_1, &written_1, |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1667 | expected_fragments_1 ); |
| 1668 | if( expected_fragments_1 == 0 ) |
| 1669 | { |
| 1670 | /* This error is expected when the message is too large and |
| 1671 | * cannot be fragmented */ |
| 1672 | TEST_ASSERT( ret == MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1673 | msg_len_1 = 0; |
| 1674 | } |
| 1675 | else |
| 1676 | { |
| 1677 | TEST_ASSERT( ret == 0 ); |
| 1678 | } |
| 1679 | } |
| 1680 | |
| 1681 | /* ssl_2 sending */ |
| 1682 | if( msg_len_2 > written_2 ) |
| 1683 | { |
| 1684 | ret = mbedtls_ssl_write_fragment( ssl_2, msg_buf_2, |
| 1685 | msg_len_2, &written_2, |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1686 | expected_fragments_2 ); |
| 1687 | if( expected_fragments_2 == 0 ) |
| 1688 | { |
| 1689 | /* This error is expected when the message is too large and |
| 1690 | * cannot be fragmented */ |
| 1691 | TEST_ASSERT( ret == MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 1692 | msg_len_2 = 0; |
| 1693 | } |
| 1694 | else |
| 1695 | { |
| 1696 | TEST_ASSERT( ret == 0 ); |
| 1697 | } |
| 1698 | } |
| 1699 | |
| 1700 | /* ssl_1 reading */ |
| 1701 | if( read_1 < msg_len_2 ) |
| 1702 | { |
| 1703 | ret = mbedtls_ssl_read_fragment( ssl_1, in_buf_1, |
| 1704 | msg_len_2, &read_1, |
Piotr Nowicki | 438bf3b | 2020-03-10 12:59:10 +0100 | [diff] [blame] | 1705 | &fragments_2, |
| 1706 | expected_fragments_2 ); |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1707 | TEST_ASSERT( ret == 0 ); |
| 1708 | } |
| 1709 | |
| 1710 | /* ssl_2 reading */ |
| 1711 | if( read_2 < msg_len_1 ) |
| 1712 | { |
| 1713 | ret = mbedtls_ssl_read_fragment( ssl_2, in_buf_2, |
| 1714 | msg_len_1, &read_2, |
Piotr Nowicki | 438bf3b | 2020-03-10 12:59:10 +0100 | [diff] [blame] | 1715 | &fragments_1, |
| 1716 | expected_fragments_1 ); |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1717 | TEST_ASSERT( ret == 0 ); |
| 1718 | } |
| 1719 | } |
| 1720 | |
| 1721 | ret = -1; |
| 1722 | TEST_ASSERT( 0 == memcmp( msg_buf_1, in_buf_2, msg_len_1 ) ); |
| 1723 | TEST_ASSERT( 0 == memcmp( msg_buf_2, in_buf_1, msg_len_2 ) ); |
| 1724 | TEST_ASSERT( fragments_1 == expected_fragments_1 ); |
| 1725 | TEST_ASSERT( fragments_2 == expected_fragments_2 ); |
| 1726 | } |
| 1727 | |
| 1728 | ret = 0; |
| 1729 | |
| 1730 | exit: |
| 1731 | free( msg_buf_1 ); |
| 1732 | free( in_buf_1 ); |
| 1733 | free( msg_buf_2 ); |
| 1734 | free( in_buf_2 ); |
| 1735 | |
| 1736 | return ret; |
| 1737 | } |
| 1738 | |
Piotr Nowicki | 95e9eb8 | 2020-02-14 11:33:34 +0100 | [diff] [blame] | 1739 | /* |
| 1740 | * Perform data exchanging between \p ssl_1 and \p ssl_2. Both of endpoints |
| 1741 | * must be initialized and connected beforehand. |
| 1742 | * |
| 1743 | * \retval 0 on success, otherwise error code. |
| 1744 | */ |
| 1745 | int exchange_data( mbedtls_ssl_context *ssl_1, |
| 1746 | mbedtls_ssl_context *ssl_2 ) |
| 1747 | { |
| 1748 | return mbedtls_exchange_data( ssl_1, 256, 1, |
| 1749 | ssl_2, 256, 1 ); |
| 1750 | } |
| 1751 | |
Manuel Pégourié-Gonnard | d12402f | 2020-05-20 10:34:25 +0200 | [diff] [blame] | 1752 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && \ |
| 1753 | defined(MBEDTLS_ENTROPY_C) && \ |
| 1754 | defined(MBEDTLS_CTR_DRBG_C) |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1755 | void perform_handshake( handshake_test_options* options ) |
| 1756 | { |
| 1757 | /* forced_ciphersuite needs to last until the end of the handshake */ |
| 1758 | int forced_ciphersuite[2]; |
| 1759 | enum { BUFFSIZE = 17000 }; |
| 1760 | mbedtls_endpoint client, server; |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 1761 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1762 | const char *psk_identity = "foo"; |
| 1763 | #endif |
| 1764 | #if defined(MBEDTLS_TIMING_C) |
| 1765 | mbedtls_timing_delay_context timer_client, timer_server; |
| 1766 | #endif |
| 1767 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
| 1768 | unsigned char *context_buf = NULL; |
| 1769 | size_t context_buf_len; |
| 1770 | #endif |
| 1771 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 1772 | int ret = -1; |
| 1773 | #endif |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 1774 | int expected_handshake_result = 0; |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1775 | |
| 1776 | mbedtls_test_message_queue server_queue, client_queue; |
| 1777 | mbedtls_test_message_socket_context server_context, client_context; |
Andrzej Kurek | 45916ba | 2020-03-05 14:46:22 -0500 | [diff] [blame] | 1778 | mbedtls_message_socket_init( &server_context ); |
| 1779 | mbedtls_message_socket_init( &client_context ); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1780 | |
| 1781 | /* Client side */ |
| 1782 | if( options->dtls != 0 ) |
| 1783 | { |
| 1784 | TEST_ASSERT( mbedtls_endpoint_init( &client, MBEDTLS_SSL_IS_CLIENT, |
| 1785 | options->pk_alg, &client_context, |
| 1786 | &client_queue, |
| 1787 | &server_queue ) == 0 ); |
| 1788 | #if defined(MBEDTLS_TIMING_C) |
| 1789 | mbedtls_ssl_set_timer_cb( &client.ssl, &timer_client, |
| 1790 | mbedtls_timing_set_delay, |
| 1791 | mbedtls_timing_get_delay ); |
| 1792 | #endif |
| 1793 | } |
| 1794 | else |
| 1795 | { |
| 1796 | TEST_ASSERT( mbedtls_endpoint_init( &client, MBEDTLS_SSL_IS_CLIENT, |
| 1797 | options->pk_alg, NULL, NULL, |
| 1798 | NULL ) == 0 ); |
| 1799 | } |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 1800 | |
| 1801 | if( options->client_min_version != TEST_SSL_MINOR_VERSION_NONE ) |
| 1802 | { |
| 1803 | mbedtls_ssl_conf_min_version( &client.conf, MBEDTLS_SSL_MAJOR_VERSION_3, |
| 1804 | options->client_min_version ); |
| 1805 | } |
| 1806 | |
| 1807 | if( options->client_max_version != TEST_SSL_MINOR_VERSION_NONE ) |
| 1808 | { |
| 1809 | mbedtls_ssl_conf_max_version( &client.conf, MBEDTLS_SSL_MAJOR_VERSION_3, |
| 1810 | options->client_max_version ); |
| 1811 | } |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1812 | |
| 1813 | if( strlen( options->cipher ) > 0 ) |
| 1814 | { |
| 1815 | set_ciphersuite( &client.conf, options->cipher, forced_ciphersuite ); |
| 1816 | } |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 1817 | |
| 1818 | #if defined (MBEDTLS_DEBUG_C) |
| 1819 | if( options->cli_log_fun ) |
| 1820 | { |
| 1821 | mbedtls_debug_set_threshold( 4 ); |
| 1822 | mbedtls_ssl_conf_dbg( &client.conf, options->cli_log_fun, |
| 1823 | options->cli_log_obj ); |
| 1824 | } |
| 1825 | #endif |
| 1826 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1827 | /* Server side */ |
| 1828 | if( options->dtls != 0 ) |
| 1829 | { |
| 1830 | TEST_ASSERT( mbedtls_endpoint_init( &server, MBEDTLS_SSL_IS_SERVER, |
| 1831 | options->pk_alg, &server_context, |
| 1832 | &server_queue, |
| 1833 | &client_queue) == 0 ); |
| 1834 | #if defined(MBEDTLS_TIMING_C) |
| 1835 | mbedtls_ssl_set_timer_cb( &server.ssl, &timer_server, |
| 1836 | mbedtls_timing_set_delay, |
| 1837 | mbedtls_timing_get_delay ); |
| 1838 | #endif |
| 1839 | } |
| 1840 | else |
| 1841 | { |
| 1842 | TEST_ASSERT( mbedtls_endpoint_init( &server, MBEDTLS_SSL_IS_SERVER, |
| 1843 | options->pk_alg, NULL, NULL, NULL ) == 0 ); |
| 1844 | } |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 1845 | |
| 1846 | mbedtls_ssl_conf_authmode( &server.conf, options->srv_auth_mode ); |
| 1847 | |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 1848 | if( options->server_min_version != TEST_SSL_MINOR_VERSION_NONE ) |
| 1849 | { |
| 1850 | mbedtls_ssl_conf_min_version( &server.conf, MBEDTLS_SSL_MAJOR_VERSION_3, |
| 1851 | options->server_min_version ); |
| 1852 | } |
| 1853 | |
| 1854 | if( options->server_max_version != TEST_SSL_MINOR_VERSION_NONE ) |
| 1855 | { |
| 1856 | mbedtls_ssl_conf_max_version( &server.conf, MBEDTLS_SSL_MAJOR_VERSION_3, |
| 1857 | options->server_max_version ); |
| 1858 | } |
| 1859 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1860 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 1861 | TEST_ASSERT( mbedtls_ssl_conf_max_frag_len( &(server.conf), |
| 1862 | (unsigned char) options->mfl ) == 0 ); |
| 1863 | TEST_ASSERT( mbedtls_ssl_conf_max_frag_len( &(client.conf), |
| 1864 | (unsigned char) options->mfl ) == 0 ); |
| 1865 | #else |
| 1866 | TEST_ASSERT( MBEDTLS_SSL_MAX_FRAG_LEN_NONE == options->mfl ); |
| 1867 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 1868 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 1869 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1870 | if( options->psk_str != NULL && options->psk_str->len > 0 ) |
| 1871 | { |
| 1872 | TEST_ASSERT( mbedtls_ssl_conf_psk( &client.conf, options->psk_str->x, |
| 1873 | options->psk_str->len, |
| 1874 | (const unsigned char *) psk_identity, |
| 1875 | strlen( psk_identity ) ) == 0 ); |
| 1876 | |
| 1877 | TEST_ASSERT( mbedtls_ssl_conf_psk( &server.conf, options->psk_str->x, |
| 1878 | options->psk_str->len, |
| 1879 | (const unsigned char *) psk_identity, |
| 1880 | strlen( psk_identity ) ) == 0 ); |
| 1881 | |
| 1882 | mbedtls_ssl_conf_psk_cb( &server.conf, psk_dummy_callback, NULL ); |
| 1883 | } |
| 1884 | #endif |
| 1885 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 1886 | if( options->renegotiate ) |
| 1887 | { |
| 1888 | mbedtls_ssl_conf_renegotiation( &(server.conf), |
| 1889 | MBEDTLS_SSL_RENEGOTIATION_ENABLED ); |
| 1890 | mbedtls_ssl_conf_renegotiation( &(client.conf), |
| 1891 | MBEDTLS_SSL_RENEGOTIATION_ENABLED ); |
| 1892 | |
| 1893 | mbedtls_ssl_conf_legacy_renegotiation( &(server.conf), |
| 1894 | options->legacy_renegotiation ); |
| 1895 | mbedtls_ssl_conf_legacy_renegotiation( &(client.conf), |
| 1896 | options->legacy_renegotiation ); |
| 1897 | } |
| 1898 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
| 1899 | |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 1900 | #if defined (MBEDTLS_DEBUG_C) |
| 1901 | if( options->srv_log_fun ) |
| 1902 | { |
| 1903 | mbedtls_debug_set_threshold( 4 ); |
| 1904 | mbedtls_ssl_conf_dbg( &server.conf, options->srv_log_fun, |
| 1905 | options->srv_log_obj ); |
| 1906 | } |
| 1907 | #endif |
| 1908 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1909 | TEST_ASSERT( mbedtls_mock_socket_connect( &(client.socket), |
| 1910 | &(server.socket), |
| 1911 | BUFFSIZE ) == 0 ); |
| 1912 | |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 1913 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1914 | if( options->resize_buffers != 0 ) |
| 1915 | { |
| 1916 | /* Ensure that the buffer sizes are appropriate before resizes */ |
| 1917 | TEST_ASSERT( client.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN ); |
| 1918 | TEST_ASSERT( client.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN ); |
| 1919 | TEST_ASSERT( server.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN ); |
| 1920 | TEST_ASSERT( server.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN ); |
| 1921 | } |
| 1922 | #endif |
| 1923 | |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 1924 | if( options->expected_negotiated_version == TEST_SSL_MINOR_VERSION_NONE ) |
| 1925 | { |
Hanno Becker | bc00044 | 2021-06-24 09:18:19 +0100 | [diff] [blame] | 1926 | expected_handshake_result = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION; |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 1927 | } |
| 1928 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1929 | TEST_ASSERT( mbedtls_move_handshake_to_state( &(client.ssl), |
| 1930 | &(server.ssl), |
| 1931 | MBEDTLS_SSL_HANDSHAKE_OVER ) |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 1932 | == expected_handshake_result ); |
| 1933 | |
| 1934 | if( expected_handshake_result != 0 ) |
| 1935 | { |
| 1936 | /* Connection will have failed by this point, skip to cleanup */ |
| 1937 | goto exit; |
| 1938 | } |
| 1939 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1940 | TEST_ASSERT( client.ssl.state == MBEDTLS_SSL_HANDSHAKE_OVER ); |
| 1941 | TEST_ASSERT( server.ssl.state == MBEDTLS_SSL_HANDSHAKE_OVER ); |
| 1942 | |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 1943 | /* Check that we agree on the version... */ |
| 1944 | TEST_ASSERT( client.ssl.minor_ver == server.ssl.minor_ver ); |
| 1945 | |
| 1946 | /* And check that the version negotiated is the expected one. */ |
| 1947 | TEST_EQUAL( client.ssl.minor_ver, options->expected_negotiated_version ); |
| 1948 | |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 1949 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1950 | if( options->resize_buffers != 0 ) |
| 1951 | { |
TRodziewicz | 2abf03c | 2021-06-25 14:40:09 +0200 | [diff] [blame] | 1952 | /* A server, when using DTLS, might delay a buffer resize to happen |
| 1953 | * after it receives a message, so we force it. */ |
| 1954 | TEST_ASSERT( exchange_data( &(client.ssl), &(server.ssl) ) == 0 ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 1955 | |
TRodziewicz | 2abf03c | 2021-06-25 14:40:09 +0200 | [diff] [blame] | 1956 | TEST_ASSERT( client.ssl.out_buf_len == |
| 1957 | mbedtls_ssl_get_output_buflen( &client.ssl ) ); |
| 1958 | TEST_ASSERT( client.ssl.in_buf_len == |
| 1959 | mbedtls_ssl_get_input_buflen( &client.ssl ) ); |
| 1960 | TEST_ASSERT( server.ssl.out_buf_len == |
| 1961 | mbedtls_ssl_get_output_buflen( &server.ssl ) ); |
| 1962 | TEST_ASSERT( server.ssl.in_buf_len == |
| 1963 | mbedtls_ssl_get_input_buflen( &server.ssl ) ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 1964 | } |
| 1965 | #endif |
| 1966 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1967 | if( options->cli_msg_len != 0 || options->srv_msg_len != 0 ) |
| 1968 | { |
| 1969 | /* Start data exchanging test */ |
| 1970 | TEST_ASSERT( mbedtls_exchange_data( &(client.ssl), options->cli_msg_len, |
| 1971 | options->expected_cli_fragments, |
| 1972 | &(server.ssl), options->srv_msg_len, |
| 1973 | options->expected_srv_fragments ) |
| 1974 | == 0 ); |
| 1975 | } |
| 1976 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
| 1977 | if( options->serialize == 1 ) |
| 1978 | { |
| 1979 | TEST_ASSERT( options->dtls == 1 ); |
| 1980 | |
| 1981 | TEST_ASSERT( mbedtls_ssl_context_save( &(server.ssl), NULL, |
| 1982 | 0, &context_buf_len ) |
| 1983 | == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 1984 | |
| 1985 | context_buf = mbedtls_calloc( 1, context_buf_len ); |
| 1986 | TEST_ASSERT( context_buf != NULL ); |
| 1987 | |
| 1988 | TEST_ASSERT( mbedtls_ssl_context_save( &(server.ssl), context_buf, |
| 1989 | context_buf_len, |
| 1990 | &context_buf_len ) == 0 ); |
| 1991 | |
| 1992 | mbedtls_ssl_free( &(server.ssl) ); |
| 1993 | mbedtls_ssl_init( &(server.ssl) ); |
| 1994 | |
| 1995 | TEST_ASSERT( mbedtls_ssl_setup( &(server.ssl), &(server.conf) ) == 0 ); |
| 1996 | |
| 1997 | mbedtls_ssl_set_bio( &( server.ssl ), &server_context, |
| 1998 | mbedtls_mock_tcp_send_msg, |
| 1999 | mbedtls_mock_tcp_recv_msg, |
| 2000 | NULL ); |
| 2001 | |
| 2002 | #if defined(MBEDTLS_TIMING_C) |
| 2003 | mbedtls_ssl_set_timer_cb( &server.ssl, &timer_server, |
| 2004 | mbedtls_timing_set_delay, |
| 2005 | mbedtls_timing_get_delay ); |
| 2006 | #endif |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2007 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 2008 | if( options->resize_buffers != 0 ) |
| 2009 | { |
| 2010 | /* Ensure that the buffer sizes are appropriate before resizes */ |
| 2011 | TEST_ASSERT( server.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN ); |
| 2012 | TEST_ASSERT( server.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN ); |
| 2013 | } |
| 2014 | #endif |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2015 | TEST_ASSERT( mbedtls_ssl_context_load( &( server.ssl ), context_buf, |
| 2016 | context_buf_len ) == 0 ); |
| 2017 | |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2018 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 2019 | /* Validate buffer sizes after context deserialization */ |
| 2020 | if( options->resize_buffers != 0 ) |
| 2021 | { |
| 2022 | TEST_ASSERT( server.ssl.out_buf_len == |
| 2023 | mbedtls_ssl_get_output_buflen( &server.ssl ) ); |
| 2024 | TEST_ASSERT( server.ssl.in_buf_len == |
| 2025 | mbedtls_ssl_get_input_buflen( &server.ssl ) ); |
| 2026 | } |
| 2027 | #endif |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2028 | /* Retest writing/reading */ |
| 2029 | if( options->cli_msg_len != 0 || options->srv_msg_len != 0 ) |
| 2030 | { |
| 2031 | TEST_ASSERT( mbedtls_exchange_data( &(client.ssl), |
| 2032 | options->cli_msg_len, |
| 2033 | options->expected_cli_fragments, |
| 2034 | &(server.ssl), |
| 2035 | options->srv_msg_len, |
| 2036 | options->expected_srv_fragments ) |
| 2037 | == 0 ); |
| 2038 | } |
| 2039 | } |
| 2040 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2041 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2042 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 2043 | if( options->renegotiate ) |
| 2044 | { |
| 2045 | /* Start test with renegotiation */ |
| 2046 | TEST_ASSERT( server.ssl.renego_status == |
| 2047 | MBEDTLS_SSL_INITIAL_HANDSHAKE ); |
| 2048 | TEST_ASSERT( client.ssl.renego_status == |
| 2049 | MBEDTLS_SSL_INITIAL_HANDSHAKE ); |
| 2050 | |
| 2051 | /* After calling this function for the server, it only sends a handshake |
| 2052 | * request. All renegotiation should happen during data exchanging */ |
| 2053 | TEST_ASSERT( mbedtls_ssl_renegotiate( &(server.ssl) ) == 0 ); |
| 2054 | TEST_ASSERT( server.ssl.renego_status == |
| 2055 | MBEDTLS_SSL_RENEGOTIATION_PENDING ); |
| 2056 | TEST_ASSERT( client.ssl.renego_status == |
| 2057 | MBEDTLS_SSL_INITIAL_HANDSHAKE ); |
| 2058 | |
| 2059 | TEST_ASSERT( exchange_data( &(client.ssl), &(server.ssl) ) == 0 ); |
| 2060 | TEST_ASSERT( server.ssl.renego_status == |
| 2061 | MBEDTLS_SSL_RENEGOTIATION_DONE ); |
| 2062 | TEST_ASSERT( client.ssl.renego_status == |
| 2063 | MBEDTLS_SSL_RENEGOTIATION_DONE ); |
| 2064 | |
| 2065 | /* After calling mbedtls_ssl_renegotiate for the client all renegotiation |
| 2066 | * should happen inside this function. However in this test, we cannot |
| 2067 | * perform simultaneous communication betwen client and server so this |
| 2068 | * function will return waiting error on the socket. All rest of |
| 2069 | * renegotiation should happen during data exchanging */ |
| 2070 | ret = mbedtls_ssl_renegotiate( &(client.ssl) ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2071 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 2072 | if( options->resize_buffers != 0 ) |
| 2073 | { |
| 2074 | /* Ensure that the buffer sizes are appropriate before resizes */ |
| 2075 | TEST_ASSERT( client.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN ); |
| 2076 | TEST_ASSERT( client.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN ); |
| 2077 | } |
| 2078 | #endif |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2079 | TEST_ASSERT( ret == 0 || |
| 2080 | ret == MBEDTLS_ERR_SSL_WANT_READ || |
| 2081 | ret == MBEDTLS_ERR_SSL_WANT_WRITE ); |
| 2082 | TEST_ASSERT( server.ssl.renego_status == |
| 2083 | MBEDTLS_SSL_RENEGOTIATION_DONE ); |
| 2084 | TEST_ASSERT( client.ssl.renego_status == |
| 2085 | MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS ); |
| 2086 | |
| 2087 | TEST_ASSERT( exchange_data( &(client.ssl), &(server.ssl) ) == 0 ); |
| 2088 | TEST_ASSERT( server.ssl.renego_status == |
| 2089 | MBEDTLS_SSL_RENEGOTIATION_DONE ); |
| 2090 | TEST_ASSERT( client.ssl.renego_status == |
| 2091 | MBEDTLS_SSL_RENEGOTIATION_DONE ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2092 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 2093 | /* Validate buffer sizes after renegotiation */ |
| 2094 | if( options->resize_buffers != 0 ) |
| 2095 | { |
| 2096 | TEST_ASSERT( client.ssl.out_buf_len == |
| 2097 | mbedtls_ssl_get_output_buflen( &client.ssl ) ); |
| 2098 | TEST_ASSERT( client.ssl.in_buf_len == |
| 2099 | mbedtls_ssl_get_input_buflen( &client.ssl ) ); |
| 2100 | TEST_ASSERT( server.ssl.out_buf_len == |
| 2101 | mbedtls_ssl_get_output_buflen( &server.ssl ) ); |
| 2102 | TEST_ASSERT( server.ssl.in_buf_len == |
| 2103 | mbedtls_ssl_get_input_buflen( &server.ssl ) ); |
| 2104 | } |
| 2105 | #endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */ |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2106 | } |
| 2107 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
| 2108 | |
| 2109 | exit: |
| 2110 | mbedtls_endpoint_free( &client, options->dtls != 0 ? &client_context : NULL ); |
| 2111 | mbedtls_endpoint_free( &server, options->dtls != 0 ? &server_context : NULL ); |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 2112 | #if defined (MBEDTLS_DEBUG_C) |
| 2113 | if( options->cli_log_fun || options->srv_log_fun ) |
| 2114 | { |
| 2115 | mbedtls_debug_set_threshold( 0 ); |
| 2116 | } |
| 2117 | #endif |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2118 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
| 2119 | if( context_buf != NULL ) |
| 2120 | mbedtls_free( context_buf ); |
| 2121 | #endif |
| 2122 | } |
Manuel Pégourié-Gonnard | d12402f | 2020-05-20 10:34:25 +0200 | [diff] [blame] | 2123 | #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] | 2124 | |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 2125 | /* END_HEADER */ |
| 2126 | |
| 2127 | /* BEGIN_DEPENDENCIES |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2128 | * depends_on:MBEDTLS_SSL_TLS_C |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 2129 | * END_DEPENDENCIES |
| 2130 | */ |
| 2131 | |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2132 | /* BEGIN_CASE */ |
| 2133 | void test_callback_buffer_sanity() |
| 2134 | { |
| 2135 | enum { MSGLEN = 10 }; |
| 2136 | mbedtls_test_buffer buf; |
| 2137 | unsigned char input[MSGLEN]; |
| 2138 | unsigned char output[MSGLEN]; |
| 2139 | |
| 2140 | memset( input, 0, sizeof(input) ); |
| 2141 | |
| 2142 | /* Make sure calling put and get on NULL buffer results in error. */ |
| 2143 | TEST_ASSERT( mbedtls_test_buffer_put( NULL, input, sizeof( input ) ) |
| 2144 | == -1 ); |
| 2145 | TEST_ASSERT( mbedtls_test_buffer_get( NULL, output, sizeof( output ) ) |
| 2146 | == -1 ); |
| 2147 | TEST_ASSERT( mbedtls_test_buffer_put( NULL, NULL, sizeof( input ) ) == -1 ); |
Andrzej Kurek | f777414 | 2020-01-22 06:34:59 -0500 | [diff] [blame] | 2148 | |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2149 | TEST_ASSERT( mbedtls_test_buffer_put( NULL, NULL, 0 ) == -1 ); |
| 2150 | TEST_ASSERT( mbedtls_test_buffer_get( NULL, NULL, 0 ) == -1 ); |
| 2151 | |
| 2152 | /* Make sure calling put and get on a buffer that hasn't been set up results |
| 2153 | * in eror. */ |
| 2154 | mbedtls_test_buffer_init( &buf ); |
| 2155 | |
| 2156 | TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, sizeof( input ) ) == -1 ); |
| 2157 | TEST_ASSERT( mbedtls_test_buffer_get( &buf, output, sizeof( output ) ) |
| 2158 | == -1 ); |
| 2159 | TEST_ASSERT( mbedtls_test_buffer_put( &buf, NULL, sizeof( input ) ) == -1 ); |
Andrzej Kurek | f777414 | 2020-01-22 06:34:59 -0500 | [diff] [blame] | 2160 | |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2161 | TEST_ASSERT( mbedtls_test_buffer_put( &buf, NULL, 0 ) == -1 ); |
| 2162 | TEST_ASSERT( mbedtls_test_buffer_get( &buf, NULL, 0 ) == -1 ); |
| 2163 | |
Andrzej Kurek | f777414 | 2020-01-22 06:34:59 -0500 | [diff] [blame] | 2164 | /* Make sure calling put and get on NULL input only results in |
| 2165 | * error if the length is not zero, and that a NULL output is valid for data |
| 2166 | * dropping. |
| 2167 | */ |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2168 | |
| 2169 | TEST_ASSERT( mbedtls_test_buffer_setup( &buf, sizeof( input ) ) == 0 ); |
| 2170 | |
| 2171 | TEST_ASSERT( mbedtls_test_buffer_put( &buf, NULL, sizeof( input ) ) == -1 ); |
| 2172 | TEST_ASSERT( mbedtls_test_buffer_get( &buf, NULL, sizeof( output ) ) |
Andrzej Kurek | f777414 | 2020-01-22 06:34:59 -0500 | [diff] [blame] | 2173 | == 0 ); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2174 | TEST_ASSERT( mbedtls_test_buffer_put( &buf, NULL, 0 ) == 0 ); |
| 2175 | TEST_ASSERT( mbedtls_test_buffer_get( &buf, NULL, 0 ) == 0 ); |
| 2176 | |
Piotr Nowicki | fb437d7 | 2020-01-13 16:59:12 +0100 | [diff] [blame] | 2177 | /* Make sure calling put several times in the row is safe */ |
| 2178 | |
| 2179 | TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, sizeof( input ) ) |
| 2180 | == sizeof( input ) ); |
| 2181 | TEST_ASSERT( mbedtls_test_buffer_get( &buf, output, 2 ) == 2 ); |
| 2182 | TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, 1 ) == 1 ); |
| 2183 | TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, 2 ) == 1 ); |
| 2184 | TEST_ASSERT( mbedtls_test_buffer_put( &buf, input, 2 ) == 0 ); |
| 2185 | |
| 2186 | |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2187 | exit: |
| 2188 | |
| 2189 | mbedtls_test_buffer_free( &buf ); |
| 2190 | } |
| 2191 | /* END_CASE */ |
| 2192 | |
| 2193 | /* |
| 2194 | * Test if the implementation of `mbedtls_test_buffer` related functions is |
| 2195 | * correct and works as expected. |
| 2196 | * |
| 2197 | * That is |
| 2198 | * - If we try to put in \p put1 bytes then we can put in \p put1_ret bytes. |
| 2199 | * - Afterwards if we try to get \p get1 bytes then we can get \get1_ret bytes. |
| 2200 | * - Next, if we try to put in \p put1 bytes then we can put in \p put1_ret |
| 2201 | * bytes. |
| 2202 | * - Afterwards if we try to get \p get1 bytes then we can get \get1_ret bytes. |
| 2203 | * - All of the bytes we got match the bytes we put in in a FIFO manner. |
| 2204 | */ |
| 2205 | |
| 2206 | /* BEGIN_CASE */ |
| 2207 | void test_callback_buffer( int size, int put1, int put1_ret, |
| 2208 | int get1, int get1_ret, int put2, int put2_ret, |
| 2209 | int get2, int get2_ret ) |
| 2210 | { |
| 2211 | enum { ROUNDS = 2 }; |
| 2212 | size_t put[ROUNDS]; |
| 2213 | int put_ret[ROUNDS]; |
| 2214 | size_t get[ROUNDS]; |
| 2215 | int get_ret[ROUNDS]; |
| 2216 | mbedtls_test_buffer buf; |
| 2217 | unsigned char* input = NULL; |
| 2218 | size_t input_len; |
| 2219 | unsigned char* output = NULL; |
| 2220 | size_t output_len; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2221 | size_t i, j, written, read; |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2222 | |
| 2223 | mbedtls_test_buffer_init( &buf ); |
| 2224 | TEST_ASSERT( mbedtls_test_buffer_setup( &buf, size ) == 0 ); |
| 2225 | |
| 2226 | /* Check the sanity of input parameters and initialise local variables. That |
| 2227 | * is, ensure that the amount of data is not negative and that we are not |
| 2228 | * expecting more to put or get than we actually asked for. */ |
| 2229 | TEST_ASSERT( put1 >= 0 ); |
| 2230 | put[0] = put1; |
| 2231 | put_ret[0] = put1_ret; |
| 2232 | TEST_ASSERT( put1_ret <= put1 ); |
| 2233 | TEST_ASSERT( put2 >= 0 ); |
| 2234 | put[1] = put2; |
| 2235 | put_ret[1] = put2_ret; |
| 2236 | TEST_ASSERT( put2_ret <= put2 ); |
| 2237 | |
| 2238 | TEST_ASSERT( get1 >= 0 ); |
| 2239 | get[0] = get1; |
| 2240 | get_ret[0] = get1_ret; |
| 2241 | TEST_ASSERT( get1_ret <= get1 ); |
| 2242 | TEST_ASSERT( get2 >= 0 ); |
| 2243 | get[1] = get2; |
| 2244 | get_ret[1] = get2_ret; |
| 2245 | TEST_ASSERT( get2_ret <= get2 ); |
| 2246 | |
| 2247 | input_len = 0; |
| 2248 | /* Calculate actual input and output lengths */ |
| 2249 | for( j = 0; j < ROUNDS; j++ ) |
| 2250 | { |
| 2251 | if( put_ret[j] > 0 ) |
| 2252 | { |
| 2253 | input_len += put_ret[j]; |
| 2254 | } |
| 2255 | } |
| 2256 | /* In order to always have a valid pointer we always allocate at least 1 |
| 2257 | * byte. */ |
| 2258 | if( input_len == 0 ) |
| 2259 | input_len = 1; |
| 2260 | ASSERT_ALLOC( input, input_len ); |
| 2261 | |
| 2262 | output_len = 0; |
| 2263 | for( j = 0; j < ROUNDS; j++ ) |
| 2264 | { |
| 2265 | if( get_ret[j] > 0 ) |
| 2266 | { |
| 2267 | output_len += get_ret[j]; |
| 2268 | } |
| 2269 | } |
| 2270 | TEST_ASSERT( output_len <= input_len ); |
| 2271 | /* In order to always have a valid pointer we always allocate at least 1 |
| 2272 | * byte. */ |
| 2273 | if( output_len == 0 ) |
| 2274 | output_len = 1; |
| 2275 | ASSERT_ALLOC( output, output_len ); |
| 2276 | |
| 2277 | /* Fill up the buffer with structured data so that unwanted changes |
| 2278 | * can be detected */ |
| 2279 | for( i = 0; i < input_len; i++ ) |
| 2280 | { |
| 2281 | input[i] = i & 0xFF; |
| 2282 | } |
| 2283 | |
| 2284 | written = read = 0; |
| 2285 | for( j = 0; j < ROUNDS; j++ ) |
| 2286 | { |
| 2287 | TEST_ASSERT( put_ret[j] == mbedtls_test_buffer_put( &buf, |
| 2288 | input + written, put[j] ) ); |
| 2289 | written += put_ret[j]; |
| 2290 | TEST_ASSERT( get_ret[j] == mbedtls_test_buffer_get( &buf, |
| 2291 | output + read, get[j] ) ); |
| 2292 | read += get_ret[j]; |
| 2293 | TEST_ASSERT( read <= written ); |
| 2294 | if( get_ret[j] > 0 ) |
| 2295 | { |
| 2296 | TEST_ASSERT( memcmp( output + read - get_ret[j], |
| 2297 | input + read - get_ret[j], get_ret[j] ) |
| 2298 | == 0 ); |
| 2299 | } |
| 2300 | } |
| 2301 | |
| 2302 | exit: |
| 2303 | |
| 2304 | mbedtls_free( input ); |
| 2305 | mbedtls_free( output ); |
| 2306 | mbedtls_test_buffer_free( &buf ); |
| 2307 | } |
| 2308 | /* END_CASE */ |
| 2309 | |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2310 | /* |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2311 | * Test if the implementation of `mbedtls_mock_socket` related I/O functions is |
| 2312 | * correct and works as expected on unconnected sockets. |
| 2313 | */ |
| 2314 | |
| 2315 | /* BEGIN_CASE */ |
| 2316 | void ssl_mock_sanity( ) |
| 2317 | { |
| 2318 | enum { MSGLEN = 105 }; |
Paul Elliott | 21c8fe5 | 2021-11-24 16:54:26 +0000 | [diff] [blame] | 2319 | unsigned char message[MSGLEN] = { 0 }; |
| 2320 | unsigned char received[MSGLEN] = { 0 }; |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2321 | mbedtls_mock_socket socket; |
| 2322 | |
| 2323 | mbedtls_mock_socket_init( &socket ); |
| 2324 | TEST_ASSERT( mbedtls_mock_tcp_send_b( &socket, message, MSGLEN ) < 0 ); |
| 2325 | mbedtls_mock_socket_close( &socket ); |
| 2326 | mbedtls_mock_socket_init( &socket ); |
| 2327 | TEST_ASSERT( mbedtls_mock_tcp_recv_b( &socket, received, MSGLEN ) < 0 ); |
| 2328 | mbedtls_mock_socket_close( &socket ); |
| 2329 | |
| 2330 | mbedtls_mock_socket_init( &socket ); |
| 2331 | TEST_ASSERT( mbedtls_mock_tcp_send_nb( &socket, message, MSGLEN ) < 0 ); |
| 2332 | mbedtls_mock_socket_close( &socket ); |
| 2333 | mbedtls_mock_socket_init( &socket ); |
| 2334 | TEST_ASSERT( mbedtls_mock_tcp_recv_nb( &socket, received, MSGLEN ) < 0 ); |
| 2335 | mbedtls_mock_socket_close( &socket ); |
| 2336 | |
| 2337 | exit: |
| 2338 | |
| 2339 | mbedtls_mock_socket_close( &socket ); |
| 2340 | } |
| 2341 | /* END_CASE */ |
| 2342 | |
| 2343 | /* |
| 2344 | * Test if the implementation of `mbedtls_mock_socket` related functions can |
| 2345 | * send a single message from the client to the server. |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2346 | */ |
| 2347 | |
| 2348 | /* BEGIN_CASE */ |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2349 | void ssl_mock_tcp( int blocking ) |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2350 | { |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2351 | enum { MSGLEN = 105 }; |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2352 | enum { BUFLEN = MSGLEN / 5 }; |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2353 | unsigned char message[MSGLEN]; |
| 2354 | unsigned char received[MSGLEN]; |
| 2355 | mbedtls_mock_socket client; |
| 2356 | mbedtls_mock_socket server; |
| 2357 | size_t written, read; |
| 2358 | int send_ret, recv_ret; |
| 2359 | mbedtls_ssl_send_t *send; |
| 2360 | mbedtls_ssl_recv_t *recv; |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2361 | unsigned i; |
| 2362 | |
| 2363 | if( blocking == 0 ) |
| 2364 | { |
| 2365 | send = mbedtls_mock_tcp_send_nb; |
| 2366 | recv = mbedtls_mock_tcp_recv_nb; |
| 2367 | } |
| 2368 | else |
| 2369 | { |
| 2370 | send = mbedtls_mock_tcp_send_b; |
| 2371 | recv = mbedtls_mock_tcp_recv_b; |
| 2372 | } |
| 2373 | |
| 2374 | mbedtls_mock_socket_init( &client ); |
| 2375 | mbedtls_mock_socket_init( &server ); |
| 2376 | |
| 2377 | /* Fill up the buffer with structured data so that unwanted changes |
| 2378 | * can be detected */ |
| 2379 | for( i = 0; i < MSGLEN; i++ ) |
| 2380 | { |
| 2381 | message[i] = i & 0xFF; |
| 2382 | } |
| 2383 | |
| 2384 | /* Make sure that sending a message takes a few iterations. */ |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2385 | TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, BUFLEN ) ); |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2386 | |
| 2387 | /* Send the message to the server */ |
| 2388 | send_ret = recv_ret = 1; |
| 2389 | written = read = 0; |
| 2390 | while( send_ret != 0 || recv_ret != 0 ) |
| 2391 | { |
| 2392 | send_ret = send( &client, message + written, MSGLEN - written ); |
| 2393 | |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2394 | TEST_ASSERT( send_ret >= 0 ); |
| 2395 | TEST_ASSERT( send_ret <= BUFLEN ); |
| 2396 | written += send_ret; |
| 2397 | |
| 2398 | /* If the buffer is full we can test blocking and non-blocking send */ |
| 2399 | if ( send_ret == BUFLEN ) |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2400 | { |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2401 | int blocking_ret = send( &client, message , 1 ); |
| 2402 | if ( blocking ) |
| 2403 | { |
| 2404 | TEST_ASSERT( blocking_ret == 0 ); |
| 2405 | } |
| 2406 | else |
| 2407 | { |
| 2408 | TEST_ASSERT( blocking_ret == MBEDTLS_ERR_SSL_WANT_WRITE ); |
| 2409 | } |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2410 | } |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2411 | |
| 2412 | recv_ret = recv( &server, received + read, MSGLEN - read ); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2413 | |
| 2414 | /* The result depends on whether any data was sent */ |
| 2415 | if ( send_ret > 0 ) |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2416 | { |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2417 | TEST_ASSERT( recv_ret > 0 ); |
| 2418 | TEST_ASSERT( recv_ret <= BUFLEN ); |
| 2419 | read += recv_ret; |
| 2420 | } |
| 2421 | else if( blocking ) |
| 2422 | { |
| 2423 | TEST_ASSERT( recv_ret == 0 ); |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2424 | } |
| 2425 | else |
| 2426 | { |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2427 | TEST_ASSERT( recv_ret == MBEDTLS_ERR_SSL_WANT_READ ); |
| 2428 | recv_ret = 0; |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2429 | } |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2430 | |
| 2431 | /* If the buffer is empty we can test blocking and non-blocking read */ |
| 2432 | if ( recv_ret == BUFLEN ) |
| 2433 | { |
| 2434 | int blocking_ret = recv( &server, received, 1 ); |
| 2435 | if ( blocking ) |
| 2436 | { |
| 2437 | TEST_ASSERT( blocking_ret == 0 ); |
| 2438 | } |
| 2439 | else |
| 2440 | { |
| 2441 | TEST_ASSERT( blocking_ret == MBEDTLS_ERR_SSL_WANT_READ ); |
| 2442 | } |
| 2443 | } |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2444 | } |
| 2445 | TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 ); |
| 2446 | |
| 2447 | exit: |
| 2448 | |
| 2449 | mbedtls_mock_socket_close( &client ); |
| 2450 | mbedtls_mock_socket_close( &server ); |
| 2451 | } |
| 2452 | /* END_CASE */ |
| 2453 | |
| 2454 | /* |
| 2455 | * Test if the implementation of `mbedtls_mock_socket` related functions can |
| 2456 | * send messages in both direction at the same time (with the I/O calls |
| 2457 | * interleaving). |
| 2458 | */ |
| 2459 | |
| 2460 | /* BEGIN_CASE */ |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2461 | void ssl_mock_tcp_interleaving( int blocking ) |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2462 | { |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2463 | enum { ROUNDS = 2 }; |
| 2464 | enum { MSGLEN = 105 }; |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2465 | enum { BUFLEN = MSGLEN / 5 }; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2466 | unsigned char message[ROUNDS][MSGLEN]; |
| 2467 | unsigned char received[ROUNDS][MSGLEN]; |
| 2468 | mbedtls_mock_socket client; |
| 2469 | mbedtls_mock_socket server; |
| 2470 | size_t written[ROUNDS]; |
| 2471 | size_t read[ROUNDS]; |
| 2472 | int send_ret[ROUNDS]; |
| 2473 | int recv_ret[ROUNDS]; |
| 2474 | unsigned i, j, progress; |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 2475 | mbedtls_ssl_send_t *send; |
| 2476 | mbedtls_ssl_recv_t *recv; |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 2477 | |
| 2478 | if( blocking == 0 ) |
| 2479 | { |
| 2480 | send = mbedtls_mock_tcp_send_nb; |
| 2481 | recv = mbedtls_mock_tcp_recv_nb; |
| 2482 | } |
| 2483 | else |
| 2484 | { |
| 2485 | send = mbedtls_mock_tcp_send_b; |
| 2486 | recv = mbedtls_mock_tcp_recv_b; |
| 2487 | } |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2488 | |
| 2489 | mbedtls_mock_socket_init( &client ); |
| 2490 | mbedtls_mock_socket_init( &server ); |
| 2491 | |
| 2492 | /* Fill up the buffers with structured data so that unwanted changes |
| 2493 | * can be detected */ |
| 2494 | for( i = 0; i < ROUNDS; i++ ) |
| 2495 | { |
| 2496 | for( j = 0; j < MSGLEN; j++ ) |
| 2497 | { |
| 2498 | message[i][j] = ( i * MSGLEN + j ) & 0xFF; |
| 2499 | } |
| 2500 | } |
| 2501 | |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2502 | /* Make sure that sending a message takes a few iterations. */ |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2503 | TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, BUFLEN ) ); |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2504 | |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2505 | /* Send the message from both sides, interleaving. */ |
| 2506 | progress = 1; |
| 2507 | for( i = 0; i < ROUNDS; i++ ) |
| 2508 | { |
| 2509 | written[i] = 0; |
| 2510 | read[i] = 0; |
| 2511 | } |
| 2512 | /* This loop does not stop as long as there was a successful write or read |
| 2513 | * of at least one byte on either side. */ |
| 2514 | while( progress != 0 ) |
| 2515 | { |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2516 | mbedtls_mock_socket *socket; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2517 | |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2518 | for( i = 0; i < ROUNDS; i++ ) |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 2519 | { |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2520 | /* First sending is from the client */ |
| 2521 | socket = ( i % 2 == 0 ) ? ( &client ) : ( &server ); |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2522 | |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2523 | send_ret[i] = send( socket, message[i] + written[i], |
| 2524 | MSGLEN - written[i] ); |
| 2525 | TEST_ASSERT( send_ret[i] >= 0 ); |
| 2526 | TEST_ASSERT( send_ret[i] <= BUFLEN ); |
| 2527 | written[i] += send_ret[i]; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2528 | |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2529 | /* If the buffer is full we can test blocking and non-blocking |
| 2530 | * send */ |
| 2531 | if ( send_ret[i] == BUFLEN ) |
| 2532 | { |
| 2533 | int blocking_ret = send( socket, message[i] , 1 ); |
| 2534 | if ( blocking ) |
| 2535 | { |
| 2536 | TEST_ASSERT( blocking_ret == 0 ); |
| 2537 | } |
| 2538 | else |
| 2539 | { |
| 2540 | TEST_ASSERT( blocking_ret == MBEDTLS_ERR_SSL_WANT_WRITE ); |
| 2541 | } |
| 2542 | } |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 2543 | } |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2544 | |
| 2545 | for( i = 0; i < ROUNDS; i++ ) |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 2546 | { |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2547 | /* First receiving is from the server */ |
| 2548 | socket = ( i % 2 == 0 ) ? ( &server ) : ( &client ); |
| 2549 | |
| 2550 | recv_ret[i] = recv( socket, received[i] + read[i], |
| 2551 | MSGLEN - read[i] ); |
| 2552 | |
| 2553 | /* The result depends on whether any data was sent */ |
| 2554 | if ( send_ret[i] > 0 ) |
| 2555 | { |
| 2556 | TEST_ASSERT( recv_ret[i] > 0 ); |
| 2557 | TEST_ASSERT( recv_ret[i] <= BUFLEN ); |
| 2558 | read[i] += recv_ret[i]; |
| 2559 | } |
| 2560 | else if( blocking ) |
| 2561 | { |
| 2562 | TEST_ASSERT( recv_ret[i] == 0 ); |
| 2563 | } |
| 2564 | else |
| 2565 | { |
| 2566 | TEST_ASSERT( recv_ret[i] == MBEDTLS_ERR_SSL_WANT_READ ); |
| 2567 | recv_ret[i] = 0; |
| 2568 | } |
| 2569 | |
| 2570 | /* If the buffer is empty we can test blocking and non-blocking |
| 2571 | * read */ |
| 2572 | if ( recv_ret[i] == BUFLEN ) |
| 2573 | { |
| 2574 | int blocking_ret = recv( socket, received[i], 1 ); |
| 2575 | if ( blocking ) |
| 2576 | { |
| 2577 | TEST_ASSERT( blocking_ret == 0 ); |
| 2578 | } |
| 2579 | else |
| 2580 | { |
| 2581 | TEST_ASSERT( blocking_ret == MBEDTLS_ERR_SSL_WANT_READ ); |
| 2582 | } |
| 2583 | } |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 2584 | } |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2585 | |
| 2586 | progress = 0; |
| 2587 | for( i = 0; i < ROUNDS; i++ ) |
| 2588 | { |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2589 | progress += send_ret[i] + recv_ret[i]; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2590 | } |
| 2591 | } |
| 2592 | |
| 2593 | for( i = 0; i < ROUNDS; i++ ) |
| 2594 | TEST_ASSERT( memcmp( message[i], received[i], MSGLEN ) == 0 ); |
| 2595 | |
| 2596 | exit: |
| 2597 | |
| 2598 | mbedtls_mock_socket_close( &client ); |
| 2599 | mbedtls_mock_socket_close( &server ); |
| 2600 | } |
| 2601 | /* END_CASE */ |
| 2602 | |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2603 | /* BEGIN_CASE */ |
| 2604 | void ssl_message_queue_sanity( ) |
| 2605 | { |
| 2606 | mbedtls_test_message_queue queue; |
| 2607 | |
| 2608 | /* Trying to push/pull to an empty queue */ |
| 2609 | TEST_ASSERT( mbedtls_test_message_queue_push_info( NULL, 1 ) |
| 2610 | == MBEDTLS_TEST_ERROR_ARG_NULL ); |
| 2611 | TEST_ASSERT( mbedtls_test_message_queue_pop_info( NULL, 1 ) |
| 2612 | == MBEDTLS_TEST_ERROR_ARG_NULL ); |
| 2613 | |
Andrzej Kurek | 89bdc58 | 2020-03-09 06:29:43 -0400 | [diff] [blame] | 2614 | TEST_ASSERT( mbedtls_test_message_queue_setup( &queue, 3 ) == 0 ); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2615 | TEST_ASSERT( queue.capacity == 3 ); |
| 2616 | TEST_ASSERT( queue.num == 0 ); |
| 2617 | |
| 2618 | exit: |
| 2619 | mbedtls_test_message_queue_free( &queue ); |
| 2620 | } |
| 2621 | /* END_CASE */ |
| 2622 | |
| 2623 | /* BEGIN_CASE */ |
| 2624 | void ssl_message_queue_basic( ) |
| 2625 | { |
| 2626 | mbedtls_test_message_queue queue; |
| 2627 | |
Andrzej Kurek | 89bdc58 | 2020-03-09 06:29:43 -0400 | [diff] [blame] | 2628 | TEST_ASSERT( mbedtls_test_message_queue_setup( &queue, 3 ) == 0 ); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2629 | |
| 2630 | /* Sanity test - 3 pushes and 3 pops with sufficient space */ |
| 2631 | TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 ); |
| 2632 | TEST_ASSERT( queue.capacity == 3 ); |
| 2633 | TEST_ASSERT( queue.num == 1 ); |
| 2634 | TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 ); |
| 2635 | TEST_ASSERT( queue.capacity == 3 ); |
| 2636 | TEST_ASSERT( queue.num == 2 ); |
| 2637 | TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 2 ) == 2 ); |
| 2638 | TEST_ASSERT( queue.capacity == 3 ); |
| 2639 | TEST_ASSERT( queue.num == 3 ); |
| 2640 | |
| 2641 | TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 ); |
| 2642 | TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 ); |
| 2643 | TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 2 ) == 2 ); |
| 2644 | |
| 2645 | exit: |
| 2646 | mbedtls_test_message_queue_free( &queue ); |
| 2647 | } |
| 2648 | /* END_CASE */ |
| 2649 | |
| 2650 | /* BEGIN_CASE */ |
| 2651 | void ssl_message_queue_overflow_underflow( ) |
| 2652 | { |
| 2653 | mbedtls_test_message_queue queue; |
| 2654 | |
Andrzej Kurek | 89bdc58 | 2020-03-09 06:29:43 -0400 | [diff] [blame] | 2655 | TEST_ASSERT( mbedtls_test_message_queue_setup( &queue, 3 ) == 0 ); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2656 | |
| 2657 | /* 4 pushes (last one with an error), 4 pops (last one with an error) */ |
| 2658 | TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 ); |
| 2659 | TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 ); |
| 2660 | TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 2 ) == 2 ); |
| 2661 | TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 3 ) |
Andrzej Kurek | f46b912 | 2020-02-07 08:19:00 -0500 | [diff] [blame] | 2662 | == MBEDTLS_ERR_SSL_WANT_WRITE ); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2663 | |
| 2664 | TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 ); |
| 2665 | TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 ); |
| 2666 | TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 2 ) == 2 ); |
| 2667 | |
| 2668 | TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) |
Andrzej Kurek | f46b912 | 2020-02-07 08:19:00 -0500 | [diff] [blame] | 2669 | == MBEDTLS_ERR_SSL_WANT_READ ); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2670 | |
| 2671 | exit: |
| 2672 | mbedtls_test_message_queue_free( &queue ); |
| 2673 | } |
| 2674 | /* END_CASE */ |
| 2675 | |
| 2676 | /* BEGIN_CASE */ |
| 2677 | void ssl_message_queue_interleaved( ) |
| 2678 | { |
| 2679 | mbedtls_test_message_queue queue; |
| 2680 | |
Andrzej Kurek | 89bdc58 | 2020-03-09 06:29:43 -0400 | [diff] [blame] | 2681 | TEST_ASSERT( mbedtls_test_message_queue_setup( &queue, 3 ) == 0 ); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2682 | |
| 2683 | /* Interleaved test - [2 pushes, 1 pop] twice, and then two pops |
| 2684 | * (to wrap around the buffer) */ |
| 2685 | TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 ); |
| 2686 | TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 1 ) == 1 ); |
| 2687 | |
| 2688 | TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 ); |
| 2689 | |
| 2690 | TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 2 ) == 2 ); |
| 2691 | TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 3 ) == 3 ); |
| 2692 | |
| 2693 | TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 1 ) == 1 ); |
| 2694 | TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 2 ) == 2 ); |
| 2695 | |
| 2696 | TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 5 ) == 5 ); |
| 2697 | TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, 8 ) == 8 ); |
| 2698 | |
| 2699 | TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 3 ) == 3 ); |
| 2700 | |
| 2701 | TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 5 ) == 5 ); |
| 2702 | |
| 2703 | TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, 8 ) == 8 ); |
| 2704 | |
| 2705 | exit: |
| 2706 | mbedtls_test_message_queue_free( &queue ); |
| 2707 | } |
| 2708 | /* END_CASE */ |
| 2709 | |
| 2710 | /* BEGIN_CASE */ |
| 2711 | void ssl_message_queue_insufficient_buffer( ) |
| 2712 | { |
| 2713 | mbedtls_test_message_queue queue; |
| 2714 | size_t message_len = 10; |
| 2715 | size_t buffer_len = 5; |
| 2716 | |
Andrzej Kurek | 89bdc58 | 2020-03-09 06:29:43 -0400 | [diff] [blame] | 2717 | TEST_ASSERT( mbedtls_test_message_queue_setup( &queue, 1 ) == 0 ); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2718 | |
| 2719 | /* Popping without a sufficient buffer */ |
| 2720 | TEST_ASSERT( mbedtls_test_message_queue_push_info( &queue, message_len ) |
| 2721 | == (int) message_len ); |
| 2722 | TEST_ASSERT( mbedtls_test_message_queue_pop_info( &queue, buffer_len ) |
| 2723 | == (int) buffer_len ); |
| 2724 | exit: |
| 2725 | mbedtls_test_message_queue_free( &queue ); |
| 2726 | } |
| 2727 | /* END_CASE */ |
| 2728 | |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2729 | /* BEGIN_CASE */ |
| 2730 | void ssl_message_mock_uninitialized( ) |
| 2731 | { |
| 2732 | enum { MSGLEN = 10 }; |
Shawn Carey | 03092f5 | 2021-05-13 10:38:32 -0400 | [diff] [blame] | 2733 | unsigned char message[MSGLEN] = {0}, received[MSGLEN]; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2734 | mbedtls_mock_socket client, server; |
| 2735 | mbedtls_test_message_queue server_queue, client_queue; |
| 2736 | mbedtls_test_message_socket_context server_context, client_context; |
Andrzej Kurek | 45916ba | 2020-03-05 14:46:22 -0500 | [diff] [blame] | 2737 | mbedtls_message_socket_init( &server_context ); |
| 2738 | mbedtls_message_socket_init( &client_context ); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2739 | |
| 2740 | /* Send with a NULL context */ |
| 2741 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( NULL, message, MSGLEN ) |
| 2742 | == MBEDTLS_TEST_ERROR_CONTEXT_ERROR ); |
| 2743 | |
| 2744 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( NULL, message, MSGLEN ) |
| 2745 | == MBEDTLS_TEST_ERROR_CONTEXT_ERROR ); |
| 2746 | |
| 2747 | TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 1, |
| 2748 | &server, |
| 2749 | &server_context ) == 0 ); |
| 2750 | |
| 2751 | TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 1, |
| 2752 | &client, |
| 2753 | &client_context ) == 0 ); |
| 2754 | |
| 2755 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, MSGLEN ) |
| 2756 | == MBEDTLS_TEST_ERROR_SEND_FAILED ); |
| 2757 | |
| 2758 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN ) |
Andrzej Kurek | f46b912 | 2020-02-07 08:19:00 -0500 | [diff] [blame] | 2759 | == MBEDTLS_ERR_SSL_WANT_READ ); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2760 | |
| 2761 | /* Push directly to a queue to later simulate a disconnected behavior */ |
| 2762 | TEST_ASSERT( mbedtls_test_message_queue_push_info( &server_queue, MSGLEN ) |
| 2763 | == MSGLEN ); |
| 2764 | |
| 2765 | /* Test if there's an error when trying to read from a disconnected |
| 2766 | * socket */ |
| 2767 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN ) |
| 2768 | == MBEDTLS_TEST_ERROR_RECV_FAILED ); |
| 2769 | exit: |
| 2770 | mbedtls_message_socket_close( &server_context ); |
| 2771 | mbedtls_message_socket_close( &client_context ); |
| 2772 | } |
| 2773 | /* END_CASE */ |
| 2774 | |
| 2775 | /* BEGIN_CASE */ |
| 2776 | void ssl_message_mock_basic( ) |
| 2777 | { |
| 2778 | enum { MSGLEN = 10 }; |
| 2779 | unsigned char message[MSGLEN], received[MSGLEN]; |
| 2780 | mbedtls_mock_socket client, server; |
| 2781 | unsigned i; |
| 2782 | mbedtls_test_message_queue server_queue, client_queue; |
| 2783 | mbedtls_test_message_socket_context server_context, client_context; |
Andrzej Kurek | 45916ba | 2020-03-05 14:46:22 -0500 | [diff] [blame] | 2784 | mbedtls_message_socket_init( &server_context ); |
| 2785 | mbedtls_message_socket_init( &client_context ); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2786 | |
| 2787 | TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 1, |
| 2788 | &server, |
| 2789 | &server_context ) == 0 ); |
| 2790 | |
| 2791 | TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 1, |
| 2792 | &client, |
| 2793 | &client_context ) == 0 ); |
| 2794 | |
| 2795 | /* Fill up the buffer with structured data so that unwanted changes |
| 2796 | * can be detected */ |
| 2797 | for( i = 0; i < MSGLEN; i++ ) |
| 2798 | { |
| 2799 | message[i] = i & 0xFF; |
| 2800 | } |
| 2801 | TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, |
| 2802 | MSGLEN ) ); |
| 2803 | |
| 2804 | /* Send the message to the server */ |
| 2805 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, |
| 2806 | MSGLEN ) == MSGLEN ); |
| 2807 | |
| 2808 | /* Read from the server */ |
| 2809 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN ) |
| 2810 | == MSGLEN ); |
| 2811 | |
| 2812 | TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 ); |
| 2813 | memset( received, 0, MSGLEN ); |
| 2814 | |
| 2815 | /* Send the message to the client */ |
| 2816 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &server_context, message, |
| 2817 | MSGLEN ) == MSGLEN ); |
| 2818 | |
| 2819 | /* Read from the client */ |
| 2820 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &client_context, received, MSGLEN ) |
| 2821 | == MSGLEN ); |
| 2822 | TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 ); |
| 2823 | |
| 2824 | exit: |
| 2825 | mbedtls_message_socket_close( &server_context ); |
| 2826 | mbedtls_message_socket_close( &client_context ); |
| 2827 | } |
| 2828 | /* END_CASE */ |
| 2829 | |
| 2830 | /* BEGIN_CASE */ |
| 2831 | void ssl_message_mock_queue_overflow_underflow( ) |
| 2832 | { |
| 2833 | enum { MSGLEN = 10 }; |
| 2834 | unsigned char message[MSGLEN], received[MSGLEN]; |
| 2835 | mbedtls_mock_socket client, server; |
| 2836 | unsigned i; |
| 2837 | mbedtls_test_message_queue server_queue, client_queue; |
| 2838 | mbedtls_test_message_socket_context server_context, client_context; |
Andrzej Kurek | 45916ba | 2020-03-05 14:46:22 -0500 | [diff] [blame] | 2839 | mbedtls_message_socket_init( &server_context ); |
| 2840 | mbedtls_message_socket_init( &client_context ); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2841 | |
| 2842 | TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 2, |
| 2843 | &server, |
| 2844 | &server_context ) == 0 ); |
| 2845 | |
| 2846 | TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 2, |
| 2847 | &client, |
| 2848 | &client_context ) == 0 ); |
| 2849 | |
| 2850 | /* Fill up the buffer with structured data so that unwanted changes |
| 2851 | * can be detected */ |
| 2852 | for( i = 0; i < MSGLEN; i++ ) |
| 2853 | { |
| 2854 | message[i] = i & 0xFF; |
| 2855 | } |
| 2856 | TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, |
| 2857 | MSGLEN*2 ) ); |
| 2858 | |
| 2859 | /* Send three message to the server, last one with an error */ |
| 2860 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, |
| 2861 | MSGLEN - 1 ) == MSGLEN - 1 ); |
| 2862 | |
| 2863 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, |
| 2864 | MSGLEN ) == MSGLEN ); |
| 2865 | |
| 2866 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, |
| 2867 | MSGLEN ) |
Andrzej Kurek | f46b912 | 2020-02-07 08:19:00 -0500 | [diff] [blame] | 2868 | == MBEDTLS_ERR_SSL_WANT_WRITE ); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2869 | |
| 2870 | /* Read three messages from the server, last one with an error */ |
| 2871 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, |
| 2872 | MSGLEN - 1 ) == MSGLEN - 1 ); |
| 2873 | |
| 2874 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN ) |
| 2875 | == MSGLEN ); |
| 2876 | |
| 2877 | TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 ); |
| 2878 | |
| 2879 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN ) |
Andrzej Kurek | f46b912 | 2020-02-07 08:19:00 -0500 | [diff] [blame] | 2880 | == MBEDTLS_ERR_SSL_WANT_READ ); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2881 | |
| 2882 | exit: |
| 2883 | mbedtls_message_socket_close( &server_context ); |
| 2884 | mbedtls_message_socket_close( &client_context ); |
| 2885 | } |
| 2886 | /* END_CASE */ |
| 2887 | |
| 2888 | /* BEGIN_CASE */ |
| 2889 | void ssl_message_mock_socket_overflow( ) |
| 2890 | { |
| 2891 | enum { MSGLEN = 10 }; |
| 2892 | unsigned char message[MSGLEN], received[MSGLEN]; |
| 2893 | mbedtls_mock_socket client, server; |
| 2894 | unsigned i; |
| 2895 | mbedtls_test_message_queue server_queue, client_queue; |
| 2896 | mbedtls_test_message_socket_context server_context, client_context; |
Andrzej Kurek | 45916ba | 2020-03-05 14:46:22 -0500 | [diff] [blame] | 2897 | mbedtls_message_socket_init( &server_context ); |
| 2898 | mbedtls_message_socket_init( &client_context ); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2899 | |
| 2900 | TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 2, |
| 2901 | &server, |
| 2902 | &server_context ) == 0 ); |
| 2903 | |
| 2904 | TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 2, |
| 2905 | &client, |
| 2906 | &client_context ) == 0 ); |
| 2907 | |
| 2908 | /* Fill up the buffer with structured data so that unwanted changes |
| 2909 | * can be detected */ |
| 2910 | for( i = 0; i < MSGLEN; i++ ) |
| 2911 | { |
| 2912 | message[i] = i & 0xFF; |
| 2913 | } |
| 2914 | TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, |
| 2915 | MSGLEN ) ); |
| 2916 | |
| 2917 | /* Send two message to the server, second one with an error */ |
| 2918 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, |
| 2919 | MSGLEN ) == MSGLEN ); |
| 2920 | |
| 2921 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, |
| 2922 | MSGLEN ) |
| 2923 | == MBEDTLS_TEST_ERROR_SEND_FAILED ); |
| 2924 | |
| 2925 | /* Read the only message from the server */ |
| 2926 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN ) |
| 2927 | == MSGLEN ); |
| 2928 | |
| 2929 | TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 ); |
| 2930 | |
| 2931 | exit: |
| 2932 | mbedtls_message_socket_close( &server_context ); |
| 2933 | mbedtls_message_socket_close( &client_context ); |
| 2934 | } |
| 2935 | /* END_CASE */ |
| 2936 | |
| 2937 | /* BEGIN_CASE */ |
| 2938 | void ssl_message_mock_truncated( ) |
| 2939 | { |
| 2940 | enum { MSGLEN = 10 }; |
| 2941 | unsigned char message[MSGLEN], received[MSGLEN]; |
| 2942 | mbedtls_mock_socket client, server; |
| 2943 | unsigned i; |
| 2944 | mbedtls_test_message_queue server_queue, client_queue; |
| 2945 | mbedtls_test_message_socket_context server_context, client_context; |
Andrzej Kurek | 45916ba | 2020-03-05 14:46:22 -0500 | [diff] [blame] | 2946 | mbedtls_message_socket_init( &server_context ); |
| 2947 | mbedtls_message_socket_init( &client_context ); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2948 | |
| 2949 | TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 2, |
| 2950 | &server, |
| 2951 | &server_context ) == 0 ); |
| 2952 | |
| 2953 | TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 2, |
| 2954 | &client, |
| 2955 | &client_context ) == 0 ); |
| 2956 | |
| 2957 | memset( received, 0, MSGLEN ); |
| 2958 | /* Fill up the buffer with structured data so that unwanted changes |
| 2959 | * can be detected */ |
| 2960 | for( i = 0; i < MSGLEN; i++ ) |
| 2961 | { |
| 2962 | message[i] = i & 0xFF; |
| 2963 | } |
| 2964 | TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, |
| 2965 | 2 * MSGLEN ) ); |
| 2966 | |
| 2967 | /* Send two messages to the server, the second one small enough to fit in the |
| 2968 | * receiver's buffer. */ |
| 2969 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, |
| 2970 | MSGLEN ) == MSGLEN ); |
| 2971 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, |
| 2972 | MSGLEN / 2 ) == MSGLEN / 2 ); |
| 2973 | /* Read a truncated message from the server */ |
| 2974 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN/2 ) |
| 2975 | == MSGLEN/2 ); |
| 2976 | |
| 2977 | /* Test that the first half of the message is valid, and second one isn't */ |
| 2978 | TEST_ASSERT( memcmp( message, received, MSGLEN/2 ) == 0 ); |
| 2979 | TEST_ASSERT( memcmp( message + MSGLEN/2, received + MSGLEN/2, MSGLEN/2 ) |
| 2980 | != 0 ); |
| 2981 | memset( received, 0, MSGLEN ); |
| 2982 | |
| 2983 | /* Read a full message from the server */ |
| 2984 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN/2 ) |
| 2985 | == MSGLEN / 2 ); |
| 2986 | |
| 2987 | /* Test that the first half of the message is valid */ |
| 2988 | TEST_ASSERT( memcmp( message, received, MSGLEN/2 ) == 0 ); |
| 2989 | |
| 2990 | exit: |
| 2991 | mbedtls_message_socket_close( &server_context ); |
| 2992 | mbedtls_message_socket_close( &client_context ); |
| 2993 | } |
| 2994 | /* END_CASE */ |
| 2995 | |
| 2996 | /* BEGIN_CASE */ |
| 2997 | void ssl_message_mock_socket_read_error( ) |
| 2998 | { |
| 2999 | enum { MSGLEN = 10 }; |
| 3000 | unsigned char message[MSGLEN], received[MSGLEN]; |
| 3001 | mbedtls_mock_socket client, server; |
| 3002 | unsigned i; |
| 3003 | mbedtls_test_message_queue server_queue, client_queue; |
| 3004 | mbedtls_test_message_socket_context server_context, client_context; |
Andrzej Kurek | 45916ba | 2020-03-05 14:46:22 -0500 | [diff] [blame] | 3005 | mbedtls_message_socket_init( &server_context ); |
| 3006 | mbedtls_message_socket_init( &client_context ); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3007 | |
| 3008 | TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 1, |
| 3009 | &server, |
| 3010 | &server_context ) == 0 ); |
| 3011 | |
| 3012 | TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 1, |
| 3013 | &client, |
| 3014 | &client_context ) == 0 ); |
| 3015 | |
| 3016 | /* Fill up the buffer with structured data so that unwanted changes |
| 3017 | * can be detected */ |
| 3018 | for( i = 0; i < MSGLEN; i++ ) |
| 3019 | { |
| 3020 | message[i] = i & 0xFF; |
| 3021 | } |
| 3022 | TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, |
| 3023 | MSGLEN ) ); |
| 3024 | |
| 3025 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, |
| 3026 | MSGLEN ) == MSGLEN ); |
| 3027 | |
| 3028 | /* Force a read error by disconnecting the socket by hand */ |
| 3029 | server.status = 0; |
| 3030 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN ) |
| 3031 | == MBEDTLS_TEST_ERROR_RECV_FAILED ); |
| 3032 | /* Return to a valid state */ |
| 3033 | server.status = MBEDTLS_MOCK_SOCKET_CONNECTED; |
| 3034 | |
| 3035 | memset( received, 0, sizeof( received ) ); |
| 3036 | |
| 3037 | /* Test that even though the server tried to read once disconnected, the |
| 3038 | * continuity is preserved */ |
| 3039 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN ) |
| 3040 | == MSGLEN ); |
| 3041 | |
| 3042 | TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 ); |
| 3043 | |
| 3044 | exit: |
| 3045 | mbedtls_message_socket_close( &server_context ); |
| 3046 | mbedtls_message_socket_close( &client_context ); |
| 3047 | } |
| 3048 | /* END_CASE */ |
| 3049 | |
| 3050 | /* BEGIN_CASE */ |
| 3051 | void ssl_message_mock_interleaved_one_way( ) |
| 3052 | { |
| 3053 | enum { MSGLEN = 10 }; |
| 3054 | unsigned char message[MSGLEN], received[MSGLEN]; |
| 3055 | mbedtls_mock_socket client, server; |
| 3056 | unsigned i; |
| 3057 | mbedtls_test_message_queue server_queue, client_queue; |
| 3058 | mbedtls_test_message_socket_context server_context, client_context; |
Andrzej Kurek | 45916ba | 2020-03-05 14:46:22 -0500 | [diff] [blame] | 3059 | mbedtls_message_socket_init( &server_context ); |
| 3060 | mbedtls_message_socket_init( &client_context ); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3061 | |
| 3062 | TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 3, |
| 3063 | &server, |
| 3064 | &server_context ) == 0 ); |
| 3065 | |
| 3066 | TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 3, |
| 3067 | &client, |
| 3068 | &client_context ) == 0 ); |
| 3069 | |
| 3070 | /* Fill up the buffer with structured data so that unwanted changes |
| 3071 | * can be detected */ |
| 3072 | for( i = 0; i < MSGLEN; i++ ) |
| 3073 | { |
| 3074 | message[i] = i & 0xFF; |
| 3075 | } |
| 3076 | TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, |
| 3077 | MSGLEN*3 ) ); |
| 3078 | |
| 3079 | /* Interleaved test - [2 sends, 1 read] twice, and then two reads |
| 3080 | * (to wrap around the buffer) */ |
| 3081 | for( i = 0; i < 2; i++ ) |
| 3082 | { |
| 3083 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, |
| 3084 | MSGLEN ) == MSGLEN ); |
| 3085 | |
| 3086 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, |
| 3087 | MSGLEN ) == MSGLEN ); |
| 3088 | |
| 3089 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, |
| 3090 | MSGLEN ) == MSGLEN ); |
| 3091 | TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 ); |
| 3092 | memset( received, 0, sizeof( received ) ); |
| 3093 | } |
| 3094 | |
| 3095 | for( i = 0; i < 2; i++ ) |
| 3096 | { |
| 3097 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, |
| 3098 | MSGLEN ) == MSGLEN ); |
| 3099 | |
| 3100 | TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 ); |
| 3101 | } |
| 3102 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN ) |
Andrzej Kurek | f46b912 | 2020-02-07 08:19:00 -0500 | [diff] [blame] | 3103 | == MBEDTLS_ERR_SSL_WANT_READ ); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3104 | exit: |
| 3105 | mbedtls_message_socket_close( &server_context ); |
| 3106 | mbedtls_message_socket_close( &client_context ); |
| 3107 | } |
| 3108 | /* END_CASE */ |
| 3109 | |
| 3110 | /* BEGIN_CASE */ |
| 3111 | void ssl_message_mock_interleaved_two_ways( ) |
| 3112 | { |
| 3113 | enum { MSGLEN = 10 }; |
| 3114 | unsigned char message[MSGLEN], received[MSGLEN]; |
| 3115 | mbedtls_mock_socket client, server; |
| 3116 | unsigned i; |
| 3117 | mbedtls_test_message_queue server_queue, client_queue; |
| 3118 | mbedtls_test_message_socket_context server_context, client_context; |
Andrzej Kurek | 45916ba | 2020-03-05 14:46:22 -0500 | [diff] [blame] | 3119 | mbedtls_message_socket_init( &server_context ); |
| 3120 | mbedtls_message_socket_init( &client_context ); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3121 | |
| 3122 | TEST_ASSERT( mbedtls_message_socket_setup( &server_queue, &client_queue, 3, |
| 3123 | &server, |
| 3124 | &server_context ) == 0 ); |
| 3125 | |
| 3126 | TEST_ASSERT( mbedtls_message_socket_setup( &client_queue, &server_queue, 3, |
| 3127 | &client, |
| 3128 | &client_context ) == 0 ); |
| 3129 | |
| 3130 | /* Fill up the buffer with structured data so that unwanted changes |
| 3131 | * can be detected */ |
| 3132 | for( i = 0; i < MSGLEN; i++ ) |
| 3133 | { |
| 3134 | message[i] = i & 0xFF; |
| 3135 | } |
| 3136 | TEST_ASSERT( 0 == mbedtls_mock_socket_connect( &client, &server, |
| 3137 | MSGLEN*3 ) ); |
| 3138 | |
| 3139 | /* Interleaved test - [2 sends, 1 read] twice, both ways, and then two reads |
| 3140 | * (to wrap around the buffer) both ways. */ |
| 3141 | for( i = 0; i < 2; i++ ) |
| 3142 | { |
| 3143 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, |
| 3144 | MSGLEN ) == MSGLEN ); |
| 3145 | |
| 3146 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &client_context, message, |
| 3147 | MSGLEN ) == MSGLEN ); |
| 3148 | |
| 3149 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &server_context, message, |
| 3150 | MSGLEN ) == MSGLEN ); |
| 3151 | |
| 3152 | TEST_ASSERT( mbedtls_mock_tcp_send_msg( &server_context, message, |
| 3153 | MSGLEN ) == MSGLEN ); |
| 3154 | |
| 3155 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, |
| 3156 | MSGLEN ) == MSGLEN ); |
| 3157 | |
| 3158 | TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 ); |
| 3159 | |
| 3160 | memset( received, 0, sizeof( received ) ); |
| 3161 | |
| 3162 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &client_context, received, |
| 3163 | MSGLEN ) == MSGLEN ); |
| 3164 | |
| 3165 | TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 ); |
| 3166 | |
| 3167 | memset( received, 0, sizeof( received ) ); |
| 3168 | } |
| 3169 | |
| 3170 | for( i = 0; i < 2; i++ ) |
| 3171 | { |
| 3172 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, |
| 3173 | MSGLEN ) == MSGLEN ); |
| 3174 | |
| 3175 | TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 ); |
| 3176 | memset( received, 0, sizeof( received ) ); |
| 3177 | |
| 3178 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &client_context, received, |
| 3179 | MSGLEN ) == MSGLEN ); |
| 3180 | |
| 3181 | TEST_ASSERT( memcmp( message, received, MSGLEN ) == 0 ); |
| 3182 | memset( received, 0, sizeof( received ) ); |
| 3183 | } |
| 3184 | |
| 3185 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &server_context, received, MSGLEN ) |
Andrzej Kurek | f46b912 | 2020-02-07 08:19:00 -0500 | [diff] [blame] | 3186 | == MBEDTLS_ERR_SSL_WANT_READ ); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3187 | |
| 3188 | TEST_ASSERT( mbedtls_mock_tcp_recv_msg( &client_context, received, MSGLEN ) |
Andrzej Kurek | f46b912 | 2020-02-07 08:19:00 -0500 | [diff] [blame] | 3189 | == MBEDTLS_ERR_SSL_WANT_READ ); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3190 | exit: |
| 3191 | mbedtls_message_socket_close( &server_context ); |
| 3192 | mbedtls_message_socket_close( &client_context ); |
| 3193 | } |
| 3194 | /* END_CASE */ |
| 3195 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3196 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
Azim Khan | 5fcca46 | 2018-06-29 11:05:32 +0100 | [diff] [blame] | 3197 | 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] | 3198 | { |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 3199 | uint32_t len = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3200 | mbedtls_ssl_context ssl; |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 3201 | mbedtls_ssl_config conf; |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3202 | |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 3203 | mbedtls_ssl_init( &ssl ); |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 3204 | mbedtls_ssl_config_init( &conf ); |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 3205 | |
Manuel Pégourié-Gonnard | 419d5ae | 2015-05-04 19:32:36 +0200 | [diff] [blame] | 3206 | TEST_ASSERT( mbedtls_ssl_config_defaults( &conf, |
| 3207 | MBEDTLS_SSL_IS_CLIENT, |
Manuel Pégourié-Gonnard | b31c5f6 | 2015-06-17 13:53:47 +0200 | [diff] [blame] | 3208 | MBEDTLS_SSL_TRANSPORT_DATAGRAM, |
| 3209 | MBEDTLS_SSL_PRESET_DEFAULT ) == 0 ); |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 3210 | TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 ); |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3211 | |
| 3212 | /* Read previous record numbers */ |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 3213 | for( len = 0; len < prevs->len; len += 6 ) |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3214 | { |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 3215 | memcpy( ssl.in_ctr + 2, prevs->x + len, 6 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3216 | mbedtls_ssl_dtls_replay_update( &ssl ); |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3217 | } |
| 3218 | |
| 3219 | /* Check new number */ |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 3220 | memcpy( ssl.in_ctr + 2, new->x, 6 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3221 | TEST_ASSERT( mbedtls_ssl_dtls_replay_check( &ssl ) == ret ); |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3222 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3223 | mbedtls_ssl_free( &ssl ); |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 3224 | mbedtls_ssl_config_free( &conf ); |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3225 | } |
| 3226 | /* END_CASE */ |
Hanno Becker | b25c0c7 | 2017-05-05 11:24:30 +0100 | [diff] [blame] | 3227 | |
| 3228 | /* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */ |
| 3229 | void ssl_set_hostname_twice( char *hostname0, char *hostname1 ) |
| 3230 | { |
| 3231 | mbedtls_ssl_context ssl; |
| 3232 | mbedtls_ssl_init( &ssl ); |
| 3233 | |
| 3234 | TEST_ASSERT( mbedtls_ssl_set_hostname( &ssl, hostname0 ) == 0 ); |
| 3235 | TEST_ASSERT( mbedtls_ssl_set_hostname( &ssl, hostname1 ) == 0 ); |
| 3236 | |
| 3237 | mbedtls_ssl_free( &ssl ); |
| 3238 | } |
Darryl Green | 11999bb | 2018-03-13 15:22:58 +0000 | [diff] [blame] | 3239 | /* END_CASE */ |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3240 | |
| 3241 | /* BEGIN_CASE */ |
| 3242 | void ssl_crypt_record( int cipher_type, int hash_id, |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 3243 | int etm, int tag_mode, int ver, |
| 3244 | int cid0_len, int cid1_len ) |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3245 | { |
| 3246 | /* |
| 3247 | * Test several record encryptions and decryptions |
| 3248 | * with plenty of space before and after the data |
| 3249 | * within the record buffer. |
| 3250 | */ |
| 3251 | |
| 3252 | int ret; |
| 3253 | int num_records = 16; |
| 3254 | mbedtls_ssl_context ssl; /* ONLY for debugging */ |
| 3255 | |
| 3256 | mbedtls_ssl_transform t0, t1; |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 3257 | unsigned char *buf = NULL; |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3258 | size_t const buflen = 512; |
| 3259 | mbedtls_record rec, rec_backup; |
| 3260 | |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 3261 | USE_PSA_INIT( ); |
| 3262 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3263 | mbedtls_ssl_init( &ssl ); |
| 3264 | mbedtls_ssl_transform_init( &t0 ); |
| 3265 | mbedtls_ssl_transform_init( &t1 ); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 3266 | ret = build_transforms( &t0, &t1, cipher_type, hash_id, |
| 3267 | etm, tag_mode, ver, |
| 3268 | (size_t) cid0_len, |
| 3269 | (size_t) cid1_len ); |
| 3270 | |
| 3271 | TEST_ASSERT( ret == 0 ); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3272 | |
Hanno Becker | 3ee5421 | 2019-04-04 16:31:26 +0100 | [diff] [blame] | 3273 | TEST_ASSERT( ( buf = mbedtls_calloc( 1, buflen ) ) != NULL ); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3274 | |
| 3275 | while( num_records-- > 0 ) |
| 3276 | { |
| 3277 | mbedtls_ssl_transform *t_dec, *t_enc; |
| 3278 | /* Take turns in who's sending and who's receiving. */ |
| 3279 | if( num_records % 3 == 0 ) |
| 3280 | { |
| 3281 | t_dec = &t0; |
| 3282 | t_enc = &t1; |
| 3283 | } |
| 3284 | else |
| 3285 | { |
| 3286 | t_dec = &t1; |
| 3287 | t_enc = &t0; |
| 3288 | } |
| 3289 | |
| 3290 | /* |
| 3291 | * The record header affects the transformation in two ways: |
| 3292 | * 1) It determines the AEAD additional data |
| 3293 | * 2) The record counter sometimes determines the IV. |
| 3294 | * |
| 3295 | * Apart from that, the fields don't have influence. |
| 3296 | * In particular, it is currently not the responsibility |
| 3297 | * of ssl_encrypt/decrypt_buf to check if the transform |
| 3298 | * version matches the record version, or that the |
| 3299 | * type is sensible. |
| 3300 | */ |
| 3301 | |
| 3302 | memset( rec.ctr, num_records, sizeof( rec.ctr ) ); |
| 3303 | rec.type = 42; |
| 3304 | rec.ver[0] = num_records; |
| 3305 | rec.ver[1] = num_records; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3306 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 3307 | rec.cid_len = 0; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3308 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3309 | |
| 3310 | rec.buf = buf; |
| 3311 | rec.buf_len = buflen; |
| 3312 | rec.data_offset = 16; |
| 3313 | /* Make sure to vary the length to exercise different |
| 3314 | * paddings. */ |
| 3315 | rec.data_len = 1 + num_records; |
| 3316 | |
| 3317 | memset( rec.buf + rec.data_offset, 42, rec.data_len ); |
| 3318 | |
| 3319 | /* Make a copy for later comparison */ |
| 3320 | rec_backup = rec; |
| 3321 | |
| 3322 | /* Encrypt record */ |
| 3323 | ret = mbedtls_ssl_encrypt_buf( &ssl, t_enc, &rec, |
Ronald Cron | 351f0ee | 2020-06-10 12:12:18 +0200 | [diff] [blame] | 3324 | mbedtls_test_rnd_std_rand, NULL ); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3325 | TEST_ASSERT( ret == 0 || ret == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 3326 | if( ret != 0 ) |
| 3327 | { |
| 3328 | continue; |
| 3329 | } |
| 3330 | |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 3331 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 3332 | if( rec.cid_len != 0 ) |
| 3333 | { |
| 3334 | /* DTLS 1.2 + CID hides the real content type and |
| 3335 | * uses a special CID content type in the protected |
| 3336 | * record. Double-check this. */ |
| 3337 | TEST_ASSERT( rec.type == MBEDTLS_SSL_MSG_CID ); |
| 3338 | } |
| 3339 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 3340 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3341 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 3342 | if( t_enc->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 ) |
| 3343 | { |
| 3344 | /* TLS 1.3 hides the real content type and |
| 3345 | * always uses Application Data as the content type |
| 3346 | * for protected records. Double-check this. */ |
| 3347 | TEST_ASSERT( rec.type == MBEDTLS_SSL_MSG_APPLICATION_DATA ); |
| 3348 | } |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3349 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 3350 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3351 | /* Decrypt record with t_dec */ |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 3352 | ret = mbedtls_ssl_decrypt_buf( &ssl, t_dec, &rec ); |
| 3353 | TEST_ASSERT( ret == 0 ); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3354 | |
| 3355 | /* Compare results */ |
| 3356 | TEST_ASSERT( rec.type == rec_backup.type ); |
| 3357 | TEST_ASSERT( memcmp( rec.ctr, rec_backup.ctr, 8 ) == 0 ); |
| 3358 | TEST_ASSERT( rec.ver[0] == rec_backup.ver[0] ); |
| 3359 | TEST_ASSERT( rec.ver[1] == rec_backup.ver[1] ); |
| 3360 | TEST_ASSERT( rec.data_len == rec_backup.data_len ); |
| 3361 | TEST_ASSERT( rec.data_offset == rec_backup.data_offset ); |
| 3362 | TEST_ASSERT( memcmp( rec.buf + rec.data_offset, |
| 3363 | rec_backup.buf + rec_backup.data_offset, |
| 3364 | rec.data_len ) == 0 ); |
| 3365 | } |
| 3366 | |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 3367 | exit: |
| 3368 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3369 | /* Cleanup */ |
| 3370 | mbedtls_ssl_free( &ssl ); |
| 3371 | mbedtls_ssl_transform_free( &t0 ); |
| 3372 | mbedtls_ssl_transform_free( &t1 ); |
| 3373 | |
Hanno Becker | 3ee5421 | 2019-04-04 16:31:26 +0100 | [diff] [blame] | 3374 | mbedtls_free( buf ); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 3375 | USE_PSA_DONE( ); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3376 | } |
| 3377 | /* END_CASE */ |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3378 | |
| 3379 | /* BEGIN_CASE */ |
| 3380 | void ssl_crypt_record_small( int cipher_type, int hash_id, |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 3381 | int etm, int tag_mode, int ver, |
| 3382 | int cid0_len, int cid1_len ) |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3383 | { |
| 3384 | /* |
| 3385 | * Test pairs of encryption and decryption with an increasing |
| 3386 | * amount of space in the record buffer - in more detail: |
| 3387 | * 1) Try to encrypt with 0, 1, 2, ... bytes available |
| 3388 | * in front of the plaintext, and expect the encryption |
| 3389 | * to succeed starting from some offset. Always keep |
| 3390 | * enough space in the end of the buffer. |
| 3391 | * 2) Try to encrypt with 0, 1, 2, ... bytes available |
| 3392 | * at the end of the plaintext, and expect the encryption |
| 3393 | * to succeed starting from some offset. Always keep |
| 3394 | * enough space at the beginning of the buffer. |
| 3395 | * 3) Try to encrypt with 0, 1, 2, ... bytes available |
| 3396 | * both at the front and end of the plaintext, |
| 3397 | * and expect the encryption to succeed starting from |
| 3398 | * some offset. |
| 3399 | * |
| 3400 | * If encryption succeeds, check that decryption succeeds |
| 3401 | * and yields the original record. |
| 3402 | */ |
| 3403 | |
| 3404 | mbedtls_ssl_context ssl; /* ONLY for debugging */ |
| 3405 | |
| 3406 | mbedtls_ssl_transform t0, t1; |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 3407 | unsigned char *buf = NULL; |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 3408 | size_t const buflen = 256; |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3409 | mbedtls_record rec, rec_backup; |
| 3410 | |
| 3411 | int ret; |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 3412 | int mode; /* Mode 1, 2 or 3 as explained above */ |
| 3413 | size_t offset; /* Available space at beginning/end/both */ |
| 3414 | size_t threshold = 96; /* Maximum offset to test against */ |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3415 | |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 3416 | size_t default_pre_padding = 64; /* Pre-padding to use in mode 2 */ |
| 3417 | 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] | 3418 | |
| 3419 | int seen_success; /* Indicates if in the current mode we've |
| 3420 | * already seen a successful test. */ |
| 3421 | |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 3422 | USE_PSA_INIT( ); |
| 3423 | |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3424 | mbedtls_ssl_init( &ssl ); |
| 3425 | mbedtls_ssl_transform_init( &t0 ); |
| 3426 | mbedtls_ssl_transform_init( &t1 ); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 3427 | ret = build_transforms( &t0, &t1, cipher_type, hash_id, |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 3428 | etm, tag_mode, ver, |
| 3429 | (size_t) cid0_len, |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 3430 | (size_t) cid1_len ); |
| 3431 | |
| 3432 | TEST_ASSERT( ret == 0 ); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3433 | |
Hanno Becker | 3ee5421 | 2019-04-04 16:31:26 +0100 | [diff] [blame] | 3434 | TEST_ASSERT( ( buf = mbedtls_calloc( 1, buflen ) ) != NULL ); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3435 | |
| 3436 | for( mode=1; mode <= 3; mode++ ) |
| 3437 | { |
| 3438 | seen_success = 0; |
| 3439 | for( offset=0; offset <= threshold; offset++ ) |
| 3440 | { |
| 3441 | mbedtls_ssl_transform *t_dec, *t_enc; |
Hanno Becker | 6c87b3f | 2019-04-29 17:24:44 +0100 | [diff] [blame] | 3442 | t_dec = &t0; |
| 3443 | t_enc = &t1; |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3444 | |
| 3445 | memset( rec.ctr, offset, sizeof( rec.ctr ) ); |
| 3446 | rec.type = 42; |
| 3447 | rec.ver[0] = offset; |
| 3448 | rec.ver[1] = offset; |
| 3449 | rec.buf = buf; |
| 3450 | rec.buf_len = buflen; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3451 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 3452 | rec.cid_len = 0; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3453 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3454 | |
| 3455 | switch( mode ) |
| 3456 | { |
| 3457 | case 1: /* Space in the beginning */ |
| 3458 | rec.data_offset = offset; |
| 3459 | rec.data_len = buflen - offset - default_post_padding; |
| 3460 | break; |
| 3461 | |
| 3462 | case 2: /* Space in the end */ |
| 3463 | rec.data_offset = default_pre_padding; |
| 3464 | rec.data_len = buflen - default_pre_padding - offset; |
| 3465 | break; |
| 3466 | |
| 3467 | case 3: /* Space in the beginning and end */ |
| 3468 | rec.data_offset = offset; |
| 3469 | rec.data_len = buflen - 2 * offset; |
| 3470 | break; |
| 3471 | |
| 3472 | default: |
| 3473 | TEST_ASSERT( 0 ); |
| 3474 | break; |
| 3475 | } |
| 3476 | |
| 3477 | memset( rec.buf + rec.data_offset, 42, rec.data_len ); |
| 3478 | |
| 3479 | /* Make a copy for later comparison */ |
| 3480 | rec_backup = rec; |
| 3481 | |
| 3482 | /* Encrypt record */ |
Ronald Cron | 6c5bd7f | 2020-06-10 14:08:26 +0200 | [diff] [blame] | 3483 | ret = mbedtls_ssl_encrypt_buf( &ssl, t_enc, &rec, |
| 3484 | mbedtls_test_rnd_std_rand, NULL ); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3485 | |
| 3486 | if( ( mode == 1 || mode == 2 ) && seen_success ) |
| 3487 | { |
| 3488 | TEST_ASSERT( ret == 0 ); |
| 3489 | } |
| 3490 | else |
| 3491 | { |
| 3492 | TEST_ASSERT( ret == 0 || ret == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 3493 | if( ret == 0 ) |
| 3494 | seen_success = 1; |
| 3495 | } |
| 3496 | |
| 3497 | if( ret != 0 ) |
| 3498 | continue; |
| 3499 | |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 3500 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 3501 | if( rec.cid_len != 0 ) |
| 3502 | { |
| 3503 | /* DTLS 1.2 + CID hides the real content type and |
| 3504 | * uses a special CID content type in the protected |
| 3505 | * record. Double-check this. */ |
| 3506 | TEST_ASSERT( rec.type == MBEDTLS_SSL_MSG_CID ); |
| 3507 | } |
| 3508 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 3509 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3510 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 3511 | if( t_enc->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 ) |
| 3512 | { |
| 3513 | /* TLS 1.3 hides the real content type and |
| 3514 | * always uses Application Data as the content type |
| 3515 | * for protected records. Double-check this. */ |
| 3516 | TEST_ASSERT( rec.type == MBEDTLS_SSL_MSG_APPLICATION_DATA ); |
| 3517 | } |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3518 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 3519 | |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3520 | /* Decrypt record with t_dec */ |
| 3521 | TEST_ASSERT( mbedtls_ssl_decrypt_buf( &ssl, t_dec, &rec ) == 0 ); |
| 3522 | |
| 3523 | /* Compare results */ |
| 3524 | TEST_ASSERT( rec.type == rec_backup.type ); |
| 3525 | TEST_ASSERT( memcmp( rec.ctr, rec_backup.ctr, 8 ) == 0 ); |
| 3526 | TEST_ASSERT( rec.ver[0] == rec_backup.ver[0] ); |
| 3527 | TEST_ASSERT( rec.ver[1] == rec_backup.ver[1] ); |
| 3528 | TEST_ASSERT( rec.data_len == rec_backup.data_len ); |
| 3529 | TEST_ASSERT( rec.data_offset == rec_backup.data_offset ); |
| 3530 | TEST_ASSERT( memcmp( rec.buf + rec.data_offset, |
| 3531 | rec_backup.buf + rec_backup.data_offset, |
| 3532 | rec.data_len ) == 0 ); |
| 3533 | } |
| 3534 | |
| 3535 | TEST_ASSERT( seen_success == 1 ); |
| 3536 | } |
| 3537 | |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 3538 | exit: |
| 3539 | |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3540 | /* Cleanup */ |
| 3541 | mbedtls_ssl_free( &ssl ); |
| 3542 | mbedtls_ssl_transform_free( &t0 ); |
| 3543 | mbedtls_ssl_transform_free( &t1 ); |
| 3544 | |
Hanno Becker | 3ee5421 | 2019-04-04 16:31:26 +0100 | [diff] [blame] | 3545 | mbedtls_free( buf ); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 3546 | USE_PSA_DONE( ); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3547 | } |
| 3548 | /* END_CASE */ |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 3549 | |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 3550 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC:MBEDTLS_AES_C:MBEDTLS_SSL_PROTO_TLS1_2:!MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3551 | 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] | 3552 | int length_selector ) |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3553 | { |
| 3554 | /* |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3555 | * Test record decryption for CBC without EtM, focused on the verification |
| 3556 | * of padding and MAC. |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3557 | * |
TRodziewicz | 299510e | 2021-07-09 16:55:11 +0200 | [diff] [blame] | 3558 | * Actually depends on TLS 1.2 and either AES, ARIA or Camellia, but since |
| 3559 | * the test framework doesn't support alternation in dependency statements, |
| 3560 | * just depend on AES. |
Manuel Pégourié-Gonnard | 864abbf | 2020-07-21 10:37:14 +0200 | [diff] [blame] | 3561 | * |
| 3562 | * The length_selector argument is interpreted as follows: |
| 3563 | * - if it's -1, the plaintext length is 0 and minimal padding is applied |
| 3564 | * - if it's -2, the plaintext length is 0 and maximal padding is applied |
| 3565 | * - otherwise it must be in [0, 255] and is padding_length from RFC 5246: |
| 3566 | * it's the length of the rest of the padding, that is, excluding the |
| 3567 | * byte that encodes the length. The minimal non-zero plaintext length |
| 3568 | * that gives this padding_length is automatically selected. |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3569 | */ |
| 3570 | mbedtls_ssl_context ssl; /* ONLY for debugging */ |
| 3571 | mbedtls_ssl_transform t0, t1; |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3572 | mbedtls_record rec, rec_save; |
| 3573 | unsigned char *buf = NULL, *buf_save = NULL; |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3574 | size_t buflen, olen = 0; |
Manuel Pégourié-Gonnard | 864abbf | 2020-07-21 10:37:14 +0200 | [diff] [blame] | 3575 | size_t plaintext_len, block_size, i; |
Manuel Pégourié-Gonnard | e55653f | 2020-07-22 11:42:57 +0200 | [diff] [blame] | 3576 | unsigned char padlen; /* excluding the padding_length byte */ |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3577 | unsigned char add_data[13]; |
| 3578 | unsigned char mac[MBEDTLS_MD_MAX_SIZE]; |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3579 | int exp_ret; |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 3580 | int ret; |
Manuel Pégourié-Gonnard | 4adc04a | 2020-07-16 10:00:48 +0200 | [diff] [blame] | 3581 | const unsigned char pad_max_len = 255; /* Per the standard */ |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3582 | |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 3583 | USE_PSA_INIT( ); |
| 3584 | |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3585 | mbedtls_ssl_init( &ssl ); |
| 3586 | mbedtls_ssl_transform_init( &t0 ); |
| 3587 | mbedtls_ssl_transform_init( &t1 ); |
| 3588 | |
| 3589 | /* Set up transforms with dummy keys */ |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 3590 | ret = build_transforms( &t0, &t1, cipher_type, hash_id, |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3591 | 0, trunc_hmac, |
| 3592 | MBEDTLS_SSL_MINOR_VERSION_3, |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 3593 | 0 , 0 ); |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3594 | |
Przemyslaw Stekiel | 4a36dd3 | 2022-01-25 00:43:58 +0100 | [diff] [blame^] | 3595 | TEST_ASSERT( ret == 0 ); |
| 3596 | |
Manuel Pégourié-Gonnard | 864abbf | 2020-07-21 10:37:14 +0200 | [diff] [blame] | 3597 | /* Determine padding/plaintext length */ |
| 3598 | TEST_ASSERT( length_selector >= -2 && length_selector <= 255 ); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3599 | block_size = t0.ivlen; |
Manuel Pégourié-Gonnard | 864abbf | 2020-07-21 10:37:14 +0200 | [diff] [blame] | 3600 | if( length_selector < 0 ) |
| 3601 | { |
| 3602 | plaintext_len = 0; |
| 3603 | |
Manuel Pégourié-Gonnard | e55653f | 2020-07-22 11:42:57 +0200 | [diff] [blame] | 3604 | /* Minimal padding |
| 3605 | * 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] | 3606 | padlen = block_size - ( t0.maclen + 1 ) % block_size; |
| 3607 | |
| 3608 | /* Maximal padding? */ |
| 3609 | if( length_selector == -2 ) |
| 3610 | padlen += block_size * ( ( pad_max_len - padlen ) / block_size ); |
| 3611 | } |
| 3612 | else |
| 3613 | { |
| 3614 | padlen = length_selector; |
| 3615 | |
Manuel Pégourié-Gonnard | e55653f | 2020-07-22 11:42:57 +0200 | [diff] [blame] | 3616 | /* Minimal non-zero plaintext_length giving desired padding. |
| 3617 | * 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] | 3618 | plaintext_len = block_size - ( padlen + t0.maclen + 1 ) % block_size; |
| 3619 | } |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3620 | |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3621 | /* Prepare a buffer for record data */ |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3622 | buflen = block_size |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3623 | + plaintext_len |
| 3624 | + t0.maclen |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3625 | + padlen + 1; |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3626 | ASSERT_ALLOC( buf, buflen ); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3627 | ASSERT_ALLOC( buf_save, buflen ); |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3628 | |
| 3629 | /* Prepare a dummy record header */ |
| 3630 | memset( rec.ctr, 0, sizeof( rec.ctr ) ); |
| 3631 | rec.type = MBEDTLS_SSL_MSG_APPLICATION_DATA; |
| 3632 | rec.ver[0] = MBEDTLS_SSL_MAJOR_VERSION_3; |
| 3633 | rec.ver[1] = MBEDTLS_SSL_MINOR_VERSION_3; |
| 3634 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 3635 | rec.cid_len = 0; |
| 3636 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 3637 | |
| 3638 | /* Prepare dummy record content */ |
| 3639 | rec.buf = buf; |
| 3640 | rec.buf_len = buflen; |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3641 | rec.data_offset = block_size; |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3642 | rec.data_len = plaintext_len; |
| 3643 | memset( rec.buf + rec.data_offset, 42, rec.data_len ); |
| 3644 | |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3645 | /* Serialized version of record header for MAC purposes */ |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3646 | memcpy( add_data, rec.ctr, 8 ); |
| 3647 | add_data[8] = rec.type; |
| 3648 | add_data[9] = rec.ver[0]; |
| 3649 | add_data[10] = rec.ver[1]; |
| 3650 | add_data[11] = ( rec.data_len >> 8 ) & 0xff; |
| 3651 | add_data[12] = ( rec.data_len >> 0 ) & 0xff; |
| 3652 | |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3653 | /* Set dummy IV */ |
| 3654 | memset( t0.iv_enc, 0x55, t0.ivlen ); |
| 3655 | memcpy( rec.buf, t0.iv_enc, t0.ivlen ); |
| 3656 | |
| 3657 | /* |
| 3658 | * Prepare a pre-encryption record (with MAC and padding), and save it. |
| 3659 | */ |
| 3660 | |
| 3661 | /* MAC with additional data */ |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3662 | TEST_EQUAL( 0, mbedtls_md_hmac_update( &t0.md_ctx_enc, add_data, 13 ) ); |
| 3663 | TEST_EQUAL( 0, mbedtls_md_hmac_update( &t0.md_ctx_enc, |
| 3664 | rec.buf + rec.data_offset, |
| 3665 | rec.data_len ) ); |
| 3666 | TEST_EQUAL( 0, mbedtls_md_hmac_finish( &t0.md_ctx_enc, mac ) ); |
| 3667 | |
| 3668 | memcpy( rec.buf + rec.data_offset + rec.data_len, mac, t0.maclen ); |
| 3669 | rec.data_len += t0.maclen; |
| 3670 | |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3671 | /* Pad */ |
| 3672 | memset( rec.buf + rec.data_offset + rec.data_len, padlen, padlen + 1 ); |
| 3673 | rec.data_len += padlen + 1; |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3674 | |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3675 | /* Save correct pre-encryption record */ |
| 3676 | rec_save = rec; |
| 3677 | rec_save.buf = buf_save; |
| 3678 | memcpy( buf_save, buf, buflen ); |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3679 | |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3680 | /* |
| 3681 | * Encrypt and decrypt the correct record, expecting success |
| 3682 | */ |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3683 | TEST_EQUAL( 0, mbedtls_cipher_crypt( &t0.cipher_ctx_enc, |
| 3684 | t0.iv_enc, t0.ivlen, |
| 3685 | rec.buf + rec.data_offset, rec.data_len, |
| 3686 | rec.buf + rec.data_offset, &olen ) ); |
| 3687 | rec.data_offset -= t0.ivlen; |
| 3688 | rec.data_len += t0.ivlen; |
| 3689 | |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3690 | TEST_EQUAL( 0, mbedtls_ssl_decrypt_buf( &ssl, &t1, &rec ) ); |
| 3691 | |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3692 | /* |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3693 | * Modify each byte of the pre-encryption record before encrypting and |
| 3694 | * decrypting it, expecting failure every time. |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3695 | */ |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3696 | for( i = block_size; i < buflen; i++ ) |
| 3697 | { |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 3698 | mbedtls_test_set_step( i ); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3699 | |
| 3700 | /* Restore correct pre-encryption record */ |
| 3701 | rec = rec_save; |
| 3702 | rec.buf = buf; |
| 3703 | memcpy( buf, buf_save, buflen ); |
| 3704 | |
Manuel Pégourié-Gonnard | b51f044 | 2020-07-21 10:40:25 +0200 | [diff] [blame] | 3705 | /* 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] | 3706 | rec.buf[i] ^= 0x01; |
| 3707 | |
| 3708 | /* Encrypt */ |
| 3709 | TEST_EQUAL( 0, mbedtls_cipher_crypt( &t0.cipher_ctx_enc, |
| 3710 | t0.iv_enc, t0.ivlen, |
| 3711 | rec.buf + rec.data_offset, rec.data_len, |
| 3712 | rec.buf + rec.data_offset, &olen ) ); |
| 3713 | rec.data_offset -= t0.ivlen; |
| 3714 | rec.data_len += t0.ivlen; |
| 3715 | |
| 3716 | /* Decrypt and expect failure */ |
| 3717 | TEST_EQUAL( MBEDTLS_ERR_SSL_INVALID_MAC, |
| 3718 | mbedtls_ssl_decrypt_buf( &ssl, &t1, &rec ) ); |
| 3719 | } |
| 3720 | |
| 3721 | /* |
| 3722 | * Use larger values of the padding bytes - with small buffers, this tests |
| 3723 | * the case where the announced padlen would be larger than the buffer |
| 3724 | * (and before that, than the buffer minus the size of the MAC), to make |
| 3725 | * sure our padding checking code does not perform any out-of-bounds reads |
| 3726 | * in this case. (With larger buffers, ie when the plaintext is long or |
| 3727 | * maximal length padding is used, this is less relevant but still doesn't |
| 3728 | * hurt to test.) |
| 3729 | * |
| 3730 | * (Start the loop with correct padding, just to double-check that record |
| 3731 | * saving did work, and that we're overwriting the correct bytes.) |
| 3732 | */ |
Manuel Pégourié-Gonnard | 4adc04a | 2020-07-16 10:00:48 +0200 | [diff] [blame] | 3733 | for( i = padlen; i <= pad_max_len; i++ ) |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3734 | { |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 3735 | mbedtls_test_set_step( i ); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3736 | |
| 3737 | /* Restore correct pre-encryption record */ |
| 3738 | rec = rec_save; |
| 3739 | rec.buf = buf; |
| 3740 | memcpy( buf, buf_save, buflen ); |
| 3741 | |
| 3742 | /* Set padding bytes to new value */ |
| 3743 | memset( buf + buflen - padlen - 1, i, padlen + 1 ); |
| 3744 | |
| 3745 | /* Encrypt */ |
| 3746 | TEST_EQUAL( 0, mbedtls_cipher_crypt( &t0.cipher_ctx_enc, |
| 3747 | t0.iv_enc, t0.ivlen, |
| 3748 | rec.buf + rec.data_offset, rec.data_len, |
| 3749 | rec.buf + rec.data_offset, &olen ) ); |
| 3750 | rec.data_offset -= t0.ivlen; |
| 3751 | rec.data_len += t0.ivlen; |
| 3752 | |
| 3753 | /* Decrypt and expect failure except the first time */ |
| 3754 | exp_ret = ( i == padlen ) ? 0 : MBEDTLS_ERR_SSL_INVALID_MAC; |
| 3755 | TEST_EQUAL( exp_ret, mbedtls_ssl_decrypt_buf( &ssl, &t1, &rec ) ); |
| 3756 | } |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3757 | |
| 3758 | exit: |
| 3759 | mbedtls_ssl_free( &ssl ); |
| 3760 | mbedtls_ssl_transform_free( &t0 ); |
| 3761 | mbedtls_ssl_transform_free( &t1 ); |
| 3762 | mbedtls_free( buf ); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3763 | mbedtls_free( buf_save ); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 3764 | USE_PSA_DONE( ); |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3765 | } |
| 3766 | /* END_CASE */ |
| 3767 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3768 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */ |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 3769 | void ssl_tls13_hkdf_expand_label( int hash_alg, |
| 3770 | data_t *secret, |
| 3771 | int label_idx, |
| 3772 | data_t *ctx, |
| 3773 | int desired_length, |
| 3774 | data_t *expected ) |
Hanno Becker | 39ff492 | 2020-08-21 13:36:56 +0100 | [diff] [blame] | 3775 | { |
| 3776 | unsigned char dst[ 100 ]; |
| 3777 | |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 3778 | unsigned char const *lbl = NULL; |
| 3779 | size_t lbl_len; |
Xiaofei Bai | d25fab6 | 2021-12-02 06:36:27 +0000 | [diff] [blame] | 3780 | #define MBEDTLS_SSL_TLS1_3_LABEL( name, string ) \ |
| 3781 | if( label_idx == (int) tls13_label_ ## name ) \ |
| 3782 | { \ |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 3783 | lbl = mbedtls_ssl_tls13_labels.name; \ |
| 3784 | lbl_len = sizeof( mbedtls_ssl_tls13_labels.name ); \ |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 3785 | } |
| 3786 | MBEDTLS_SSL_TLS1_3_LABEL_LIST |
| 3787 | #undef MBEDTLS_SSL_TLS1_3_LABEL |
| 3788 | TEST_ASSERT( lbl != NULL ); |
Hanno Becker | 39ff492 | 2020-08-21 13:36:56 +0100 | [diff] [blame] | 3789 | |
| 3790 | /* Check sanity of test parameters. */ |
| 3791 | TEST_ASSERT( (size_t) desired_length <= sizeof(dst) ); |
| 3792 | TEST_ASSERT( (size_t) desired_length == expected->len ); |
| 3793 | |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 3794 | TEST_ASSERT( mbedtls_ssl_tls13_hkdf_expand_label( |
Hanno Becker | 39ff492 | 2020-08-21 13:36:56 +0100 | [diff] [blame] | 3795 | (mbedtls_md_type_t) hash_alg, |
| 3796 | secret->x, secret->len, |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 3797 | lbl, lbl_len, |
Hanno Becker | 39ff492 | 2020-08-21 13:36:56 +0100 | [diff] [blame] | 3798 | ctx->x, ctx->len, |
| 3799 | dst, desired_length ) == 0 ); |
| 3800 | |
Hanno Becker | fb08096 | 2020-09-08 10:58:42 +0100 | [diff] [blame] | 3801 | ASSERT_COMPARE( dst, (size_t) desired_length, |
| 3802 | expected->x, (size_t) expected->len ); |
Hanno Becker | 39ff492 | 2020-08-21 13:36:56 +0100 | [diff] [blame] | 3803 | } |
| 3804 | /* END_CASE */ |
| 3805 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3806 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */ |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 3807 | void ssl_tls13_traffic_key_generation( int hash_alg, |
| 3808 | data_t *server_secret, |
| 3809 | data_t *client_secret, |
| 3810 | int desired_iv_len, |
| 3811 | int desired_key_len, |
| 3812 | data_t *expected_server_write_key, |
| 3813 | data_t *expected_server_write_iv, |
| 3814 | data_t *expected_client_write_key, |
| 3815 | data_t *expected_client_write_iv ) |
Hanno Becker | 19498f8 | 2020-08-21 13:37:08 +0100 | [diff] [blame] | 3816 | { |
| 3817 | mbedtls_ssl_key_set keys; |
| 3818 | |
| 3819 | /* Check sanity of test parameters. */ |
| 3820 | TEST_ASSERT( client_secret->len == server_secret->len ); |
| 3821 | TEST_ASSERT( expected_client_write_iv->len == expected_server_write_iv->len && |
| 3822 | expected_client_write_iv->len == (size_t) desired_iv_len ); |
| 3823 | TEST_ASSERT( expected_client_write_key->len == expected_server_write_key->len && |
| 3824 | expected_client_write_key->len == (size_t) desired_key_len ); |
| 3825 | |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 3826 | TEST_ASSERT( mbedtls_ssl_tls13_make_traffic_keys( |
Hanno Becker | 19498f8 | 2020-08-21 13:37:08 +0100 | [diff] [blame] | 3827 | (mbedtls_md_type_t) hash_alg, |
| 3828 | client_secret->x, |
| 3829 | server_secret->x, |
| 3830 | client_secret->len /* == server_secret->len */, |
| 3831 | desired_key_len, desired_iv_len, |
| 3832 | &keys ) == 0 ); |
| 3833 | |
Hanno Becker | fb08096 | 2020-09-08 10:58:42 +0100 | [diff] [blame] | 3834 | ASSERT_COMPARE( keys.client_write_key, |
Hanno Becker | 493ea7f | 2020-09-08 11:01:00 +0100 | [diff] [blame] | 3835 | keys.key_len, |
Hanno Becker | fb08096 | 2020-09-08 10:58:42 +0100 | [diff] [blame] | 3836 | expected_client_write_key->x, |
| 3837 | (size_t) desired_key_len ); |
| 3838 | ASSERT_COMPARE( keys.server_write_key, |
Hanno Becker | 493ea7f | 2020-09-08 11:01:00 +0100 | [diff] [blame] | 3839 | keys.key_len, |
Hanno Becker | fb08096 | 2020-09-08 10:58:42 +0100 | [diff] [blame] | 3840 | expected_server_write_key->x, |
| 3841 | (size_t) desired_key_len ); |
| 3842 | ASSERT_COMPARE( keys.client_write_iv, |
Hanno Becker | 493ea7f | 2020-09-08 11:01:00 +0100 | [diff] [blame] | 3843 | keys.iv_len, |
Hanno Becker | fb08096 | 2020-09-08 10:58:42 +0100 | [diff] [blame] | 3844 | expected_client_write_iv->x, |
| 3845 | (size_t) desired_iv_len ); |
| 3846 | ASSERT_COMPARE( keys.server_write_iv, |
Hanno Becker | 493ea7f | 2020-09-08 11:01:00 +0100 | [diff] [blame] | 3847 | keys.iv_len, |
Hanno Becker | fb08096 | 2020-09-08 10:58:42 +0100 | [diff] [blame] | 3848 | expected_server_write_iv->x, |
| 3849 | (size_t) desired_iv_len ); |
Hanno Becker | 19498f8 | 2020-08-21 13:37:08 +0100 | [diff] [blame] | 3850 | } |
| 3851 | /* END_CASE */ |
| 3852 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3853 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */ |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 3854 | void ssl_tls13_derive_secret( int hash_alg, |
| 3855 | data_t *secret, |
| 3856 | int label_idx, |
| 3857 | data_t *ctx, |
| 3858 | int desired_length, |
| 3859 | int already_hashed, |
| 3860 | data_t *expected ) |
Hanno Becker | e4849d1 | 2020-08-21 14:14:14 +0100 | [diff] [blame] | 3861 | { |
| 3862 | unsigned char dst[ 100 ]; |
| 3863 | |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 3864 | unsigned char const *lbl = NULL; |
| 3865 | size_t lbl_len; |
Xiaofei Bai | d25fab6 | 2021-12-02 06:36:27 +0000 | [diff] [blame] | 3866 | #define MBEDTLS_SSL_TLS1_3_LABEL( name, string ) \ |
| 3867 | if( label_idx == (int) tls13_label_ ## name ) \ |
| 3868 | { \ |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 3869 | lbl = mbedtls_ssl_tls13_labels.name; \ |
| 3870 | lbl_len = sizeof( mbedtls_ssl_tls13_labels.name ); \ |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 3871 | } |
| 3872 | MBEDTLS_SSL_TLS1_3_LABEL_LIST |
| 3873 | #undef MBEDTLS_SSL_TLS1_3_LABEL |
| 3874 | TEST_ASSERT( lbl != NULL ); |
| 3875 | |
Hanno Becker | e4849d1 | 2020-08-21 14:14:14 +0100 | [diff] [blame] | 3876 | /* Check sanity of test parameters. */ |
| 3877 | TEST_ASSERT( (size_t) desired_length <= sizeof(dst) ); |
| 3878 | TEST_ASSERT( (size_t) desired_length == expected->len ); |
| 3879 | |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 3880 | TEST_ASSERT( mbedtls_ssl_tls13_derive_secret( |
Hanno Becker | e4849d1 | 2020-08-21 14:14:14 +0100 | [diff] [blame] | 3881 | (mbedtls_md_type_t) hash_alg, |
| 3882 | secret->x, secret->len, |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 3883 | lbl, lbl_len, |
Hanno Becker | e4849d1 | 2020-08-21 14:14:14 +0100 | [diff] [blame] | 3884 | ctx->x, ctx->len, |
| 3885 | already_hashed, |
| 3886 | dst, desired_length ) == 0 ); |
| 3887 | |
Hanno Becker | fb08096 | 2020-09-08 10:58:42 +0100 | [diff] [blame] | 3888 | ASSERT_COMPARE( dst, desired_length, |
| 3889 | expected->x, desired_length ); |
Hanno Becker | e4849d1 | 2020-08-21 14:14:14 +0100 | [diff] [blame] | 3890 | } |
| 3891 | /* END_CASE */ |
| 3892 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3893 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */ |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 3894 | void ssl_tls13_derive_early_secrets( int hash_alg, |
| 3895 | data_t *secret, |
| 3896 | data_t *transcript, |
| 3897 | data_t *traffic_expected, |
| 3898 | data_t *exporter_expected ) |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 3899 | { |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 3900 | mbedtls_ssl_tls13_early_secrets secrets; |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 3901 | |
| 3902 | /* Double-check that we've passed sane parameters. */ |
| 3903 | mbedtls_md_type_t md_type = (mbedtls_md_type_t) hash_alg; |
| 3904 | mbedtls_md_info_t const * const md_info = mbedtls_md_info_from_type( md_type ); |
| 3905 | size_t const md_size = mbedtls_md_get_size( md_info ); |
| 3906 | TEST_ASSERT( md_info != 0 && |
| 3907 | secret->len == md_size && |
| 3908 | transcript->len == md_size && |
| 3909 | traffic_expected->len == md_size && |
| 3910 | exporter_expected->len == md_size ); |
| 3911 | |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 3912 | TEST_ASSERT( mbedtls_ssl_tls13_derive_early_secrets( |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 3913 | md_type, secret->x, transcript->x, transcript->len, |
| 3914 | &secrets ) == 0 ); |
| 3915 | |
| 3916 | ASSERT_COMPARE( secrets.client_early_traffic_secret, md_size, |
| 3917 | traffic_expected->x, traffic_expected->len ); |
| 3918 | ASSERT_COMPARE( secrets.early_exporter_master_secret, md_size, |
| 3919 | exporter_expected->x, exporter_expected->len ); |
| 3920 | } |
| 3921 | /* END_CASE */ |
| 3922 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3923 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */ |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 3924 | void ssl_tls13_derive_handshake_secrets( int hash_alg, |
| 3925 | data_t *secret, |
| 3926 | data_t *transcript, |
| 3927 | data_t *client_expected, |
| 3928 | data_t *server_expected ) |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 3929 | { |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 3930 | mbedtls_ssl_tls13_handshake_secrets secrets; |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 3931 | |
| 3932 | /* Double-check that we've passed sane parameters. */ |
| 3933 | mbedtls_md_type_t md_type = (mbedtls_md_type_t) hash_alg; |
| 3934 | mbedtls_md_info_t const * const md_info = mbedtls_md_info_from_type( md_type ); |
| 3935 | size_t const md_size = mbedtls_md_get_size( md_info ); |
| 3936 | TEST_ASSERT( md_info != 0 && |
| 3937 | secret->len == md_size && |
| 3938 | transcript->len == md_size && |
| 3939 | client_expected->len == md_size && |
| 3940 | server_expected->len == md_size ); |
| 3941 | |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 3942 | TEST_ASSERT( mbedtls_ssl_tls13_derive_handshake_secrets( |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 3943 | md_type, secret->x, transcript->x, transcript->len, |
| 3944 | &secrets ) == 0 ); |
| 3945 | |
| 3946 | ASSERT_COMPARE( secrets.client_handshake_traffic_secret, md_size, |
| 3947 | client_expected->x, client_expected->len ); |
| 3948 | ASSERT_COMPARE( secrets.server_handshake_traffic_secret, md_size, |
| 3949 | server_expected->x, server_expected->len ); |
| 3950 | } |
| 3951 | /* END_CASE */ |
| 3952 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3953 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */ |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 3954 | void ssl_tls13_derive_application_secrets( int hash_alg, |
| 3955 | data_t *secret, |
| 3956 | data_t *transcript, |
| 3957 | data_t *client_expected, |
| 3958 | data_t *server_expected, |
| 3959 | data_t *exporter_expected ) |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 3960 | { |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 3961 | mbedtls_ssl_tls13_application_secrets secrets; |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 3962 | |
| 3963 | /* Double-check that we've passed sane parameters. */ |
| 3964 | mbedtls_md_type_t md_type = (mbedtls_md_type_t) hash_alg; |
| 3965 | mbedtls_md_info_t const * const md_info = mbedtls_md_info_from_type( md_type ); |
| 3966 | size_t const md_size = mbedtls_md_get_size( md_info ); |
| 3967 | TEST_ASSERT( md_info != 0 && |
| 3968 | secret->len == md_size && |
| 3969 | transcript->len == md_size && |
| 3970 | client_expected->len == md_size && |
| 3971 | server_expected->len == md_size && |
| 3972 | exporter_expected->len == md_size ); |
| 3973 | |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 3974 | TEST_ASSERT( mbedtls_ssl_tls13_derive_application_secrets( |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 3975 | md_type, secret->x, transcript->x, transcript->len, |
| 3976 | &secrets ) == 0 ); |
| 3977 | |
| 3978 | ASSERT_COMPARE( secrets.client_application_traffic_secret_N, md_size, |
| 3979 | client_expected->x, client_expected->len ); |
| 3980 | ASSERT_COMPARE( secrets.server_application_traffic_secret_N, md_size, |
| 3981 | server_expected->x, server_expected->len ); |
| 3982 | ASSERT_COMPARE( secrets.exporter_master_secret, md_size, |
| 3983 | exporter_expected->x, exporter_expected->len ); |
| 3984 | } |
| 3985 | /* END_CASE */ |
| 3986 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3987 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */ |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 3988 | void ssl_tls13_derive_resumption_secrets( int hash_alg, |
| 3989 | data_t *secret, |
| 3990 | data_t *transcript, |
| 3991 | data_t *resumption_expected ) |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 3992 | { |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 3993 | mbedtls_ssl_tls13_application_secrets secrets; |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 3994 | |
| 3995 | /* Double-check that we've passed sane parameters. */ |
| 3996 | mbedtls_md_type_t md_type = (mbedtls_md_type_t) hash_alg; |
| 3997 | mbedtls_md_info_t const * const md_info = mbedtls_md_info_from_type( md_type ); |
| 3998 | size_t const md_size = mbedtls_md_get_size( md_info ); |
| 3999 | TEST_ASSERT( md_info != 0 && |
| 4000 | secret->len == md_size && |
| 4001 | transcript->len == md_size && |
| 4002 | resumption_expected->len == md_size ); |
| 4003 | |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4004 | TEST_ASSERT( mbedtls_ssl_tls13_derive_resumption_master_secret( |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4005 | md_type, secret->x, transcript->x, transcript->len, |
| 4006 | &secrets ) == 0 ); |
| 4007 | |
| 4008 | ASSERT_COMPARE( secrets.resumption_master_secret, md_size, |
| 4009 | resumption_expected->x, resumption_expected->len ); |
| 4010 | } |
| 4011 | /* END_CASE */ |
| 4012 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4013 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */ |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4014 | void ssl_tls13_create_psk_binder( int hash_alg, |
| 4015 | data_t *psk, |
| 4016 | int psk_type, |
| 4017 | data_t *transcript, |
| 4018 | data_t *binder_expected ) |
Hanno Becker | 55bc2c5 | 2021-05-24 06:53:52 +0100 | [diff] [blame] | 4019 | { |
| 4020 | unsigned char binder[ MBEDTLS_MD_MAX_SIZE ]; |
| 4021 | |
| 4022 | /* Double-check that we've passed sane parameters. */ |
| 4023 | mbedtls_md_type_t md_type = (mbedtls_md_type_t) hash_alg; |
| 4024 | mbedtls_md_info_t const * const md_info = mbedtls_md_info_from_type( md_type ); |
| 4025 | size_t const md_size = mbedtls_md_get_size( md_info ); |
| 4026 | TEST_ASSERT( md_info != 0 && |
| 4027 | transcript->len == md_size && |
| 4028 | binder_expected->len == md_size ); |
| 4029 | |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4030 | TEST_ASSERT( mbedtls_ssl_tls13_create_psk_binder( |
Hanno Becker | 55bc2c5 | 2021-05-24 06:53:52 +0100 | [diff] [blame] | 4031 | NULL, /* SSL context for debugging only */ |
| 4032 | md_type, |
| 4033 | psk->x, psk->len, |
| 4034 | psk_type, |
| 4035 | transcript->x, |
| 4036 | binder ) == 0 ); |
| 4037 | |
| 4038 | ASSERT_COMPARE( binder, md_size, |
| 4039 | binder_expected->x, binder_expected->len ); |
| 4040 | } |
| 4041 | /* END_CASE */ |
| 4042 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4043 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */ |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4044 | void ssl_tls13_record_protection( int ciphersuite, |
| 4045 | int endpoint, |
| 4046 | int ctr, |
| 4047 | int padding_used, |
| 4048 | data_t *server_write_key, |
| 4049 | data_t *server_write_iv, |
| 4050 | data_t *client_write_key, |
| 4051 | data_t *client_write_iv, |
| 4052 | data_t *plaintext, |
| 4053 | data_t *ciphertext ) |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4054 | { |
| 4055 | mbedtls_ssl_key_set keys; |
| 4056 | mbedtls_ssl_transform transform_send; |
| 4057 | mbedtls_ssl_transform transform_recv; |
| 4058 | mbedtls_record rec; |
| 4059 | unsigned char *buf = NULL; |
Hanno Becker | 1f91878 | 2021-08-01 19:18:28 +0100 | [diff] [blame] | 4060 | size_t buf_len; |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4061 | int other_endpoint; |
| 4062 | |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 4063 | USE_PSA_INIT( ); |
| 4064 | |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4065 | TEST_ASSERT( endpoint == MBEDTLS_SSL_IS_CLIENT || |
| 4066 | endpoint == MBEDTLS_SSL_IS_SERVER ); |
| 4067 | |
| 4068 | if( endpoint == MBEDTLS_SSL_IS_SERVER ) |
| 4069 | other_endpoint = MBEDTLS_SSL_IS_CLIENT; |
| 4070 | if( endpoint == MBEDTLS_SSL_IS_CLIENT ) |
| 4071 | other_endpoint = MBEDTLS_SSL_IS_SERVER; |
| 4072 | |
| 4073 | TEST_ASSERT( server_write_key->len == client_write_key->len ); |
| 4074 | TEST_ASSERT( server_write_iv->len == client_write_iv->len ); |
| 4075 | |
| 4076 | memcpy( keys.client_write_key, |
| 4077 | client_write_key->x, client_write_key->len ); |
| 4078 | memcpy( keys.client_write_iv, |
| 4079 | client_write_iv->x, client_write_iv->len ); |
| 4080 | memcpy( keys.server_write_key, |
| 4081 | server_write_key->x, server_write_key->len ); |
| 4082 | memcpy( keys.server_write_iv, |
| 4083 | server_write_iv->x, server_write_iv->len ); |
| 4084 | |
| 4085 | keys.key_len = server_write_key->len; |
| 4086 | keys.iv_len = server_write_iv->len; |
| 4087 | |
| 4088 | mbedtls_ssl_transform_init( &transform_recv ); |
| 4089 | mbedtls_ssl_transform_init( &transform_send ); |
| 4090 | |
| 4091 | TEST_ASSERT( mbedtls_ssl_tls13_populate_transform( |
| 4092 | &transform_send, endpoint, |
| 4093 | ciphersuite, &keys, NULL ) == 0 ); |
| 4094 | TEST_ASSERT( mbedtls_ssl_tls13_populate_transform( |
| 4095 | &transform_recv, other_endpoint, |
| 4096 | ciphersuite, &keys, NULL ) == 0 ); |
| 4097 | |
Hanno Becker | 1f91878 | 2021-08-01 19:18:28 +0100 | [diff] [blame] | 4098 | /* Make sure we have enough space in the buffer even if |
| 4099 | * we use more padding than the KAT. */ |
| 4100 | buf_len = ciphertext->len + MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY; |
| 4101 | ASSERT_ALLOC( buf, buf_len ); |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4102 | rec.type = MBEDTLS_SSL_MSG_APPLICATION_DATA; |
Hanno Becker | 4153745 | 2021-04-20 05:35:28 +0100 | [diff] [blame] | 4103 | |
| 4104 | /* 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] | 4105 | mbedtls_ssl_write_version( MBEDTLS_SSL_MAJOR_VERSION_3, |
| 4106 | MBEDTLS_SSL_MINOR_VERSION_3, |
| 4107 | MBEDTLS_SSL_TRANSPORT_STREAM, |
| 4108 | rec.ver ); |
| 4109 | |
| 4110 | /* Copy plaintext into record structure */ |
| 4111 | rec.buf = buf; |
Hanno Becker | 1f91878 | 2021-08-01 19:18:28 +0100 | [diff] [blame] | 4112 | rec.buf_len = buf_len; |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4113 | rec.data_offset = 0; |
| 4114 | TEST_ASSERT( plaintext->len <= ciphertext->len ); |
| 4115 | memcpy( rec.buf + rec.data_offset, plaintext->x, plaintext->len ); |
| 4116 | rec.data_len = plaintext->len; |
| 4117 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 4118 | rec.cid_len = 0; |
| 4119 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 4120 | |
| 4121 | memset( &rec.ctr[0], 0, 8 ); |
| 4122 | rec.ctr[7] = ctr; |
| 4123 | |
| 4124 | TEST_ASSERT( mbedtls_ssl_encrypt_buf( NULL, &transform_send, &rec, |
| 4125 | NULL, NULL ) == 0 ); |
Hanno Becker | 1f91878 | 2021-08-01 19:18:28 +0100 | [diff] [blame] | 4126 | |
| 4127 | if( padding_used == MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY ) |
| 4128 | { |
| 4129 | ASSERT_COMPARE( rec.buf + rec.data_offset, rec.data_len, |
| 4130 | ciphertext->x, ciphertext->len ); |
| 4131 | } |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4132 | |
| 4133 | TEST_ASSERT( mbedtls_ssl_decrypt_buf( NULL, &transform_recv, &rec ) == 0 ); |
| 4134 | ASSERT_COMPARE( rec.buf + rec.data_offset, rec.data_len, |
| 4135 | plaintext->x, plaintext->len ); |
| 4136 | |
Hanno Becker | 80e760e | 2021-03-23 06:00:21 +0000 | [diff] [blame] | 4137 | mbedtls_free( buf ); |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4138 | mbedtls_ssl_transform_free( &transform_send ); |
| 4139 | mbedtls_ssl_transform_free( &transform_recv ); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 4140 | USE_PSA_DONE( ); |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4141 | } |
| 4142 | /* END_CASE */ |
| 4143 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4144 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */ |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4145 | void ssl_tls13_key_evolution( int hash_alg, |
| 4146 | data_t *secret, |
| 4147 | data_t *input, |
| 4148 | data_t *expected ) |
Hanno Becker | 2d2c3eb | 2020-08-20 14:54:24 +0100 | [diff] [blame] | 4149 | { |
| 4150 | unsigned char secret_new[ MBEDTLS_MD_MAX_SIZE ]; |
| 4151 | |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4152 | TEST_ASSERT( mbedtls_ssl_tls13_evolve_secret( |
Hanno Becker | 2d2c3eb | 2020-08-20 14:54:24 +0100 | [diff] [blame] | 4153 | (mbedtls_md_type_t) hash_alg, |
| 4154 | secret->len ? secret->x : NULL, |
| 4155 | input->len ? input->x : NULL, input->len, |
| 4156 | secret_new ) == 0 ); |
| 4157 | |
Hanno Becker | fb08096 | 2020-09-08 10:58:42 +0100 | [diff] [blame] | 4158 | ASSERT_COMPARE( secret_new, (size_t) expected->len, |
| 4159 | expected->x, (size_t) expected->len ); |
Hanno Becker | 2d2c3eb | 2020-08-20 14:54:24 +0100 | [diff] [blame] | 4160 | } |
| 4161 | /* END_CASE */ |
| 4162 | |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 4163 | /* BEGIN_CASE */ |
| 4164 | void ssl_tls_prf( int type, data_t * secret, data_t * random, |
Ronald Cron | ac6ae35 | 2020-06-26 14:33:03 +0200 | [diff] [blame] | 4165 | char *label, data_t *result_str, int exp_ret ) |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 4166 | { |
| 4167 | unsigned char *output; |
| 4168 | |
Ronald Cron | ac6ae35 | 2020-06-26 14:33:03 +0200 | [diff] [blame] | 4169 | output = mbedtls_calloc( 1, result_str->len ); |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 4170 | if( output == NULL ) |
| 4171 | goto exit; |
| 4172 | |
Gilles Peskine | 9de97e2 | 2021-02-02 21:00:11 +0100 | [diff] [blame] | 4173 | USE_PSA_INIT( ); |
Ron Eldor | 6b9b1b8 | 2019-05-15 17:04:33 +0300 | [diff] [blame] | 4174 | |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 4175 | TEST_ASSERT( mbedtls_ssl_tls_prf( type, secret->x, secret->len, |
| 4176 | label, random->x, random->len, |
Ronald Cron | ac6ae35 | 2020-06-26 14:33:03 +0200 | [diff] [blame] | 4177 | output, result_str->len ) == exp_ret ); |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 4178 | |
| 4179 | if( exp_ret == 0 ) |
| 4180 | { |
Ronald Cron | ac6ae35 | 2020-06-26 14:33:03 +0200 | [diff] [blame] | 4181 | TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x, |
| 4182 | result_str->len, result_str->len ) == 0 ); |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 4183 | } |
| 4184 | exit: |
| 4185 | |
| 4186 | mbedtls_free( output ); |
Gilles Peskine | 1f186ff | 2021-02-02 21:04:06 +0100 | [diff] [blame] | 4187 | USE_PSA_DONE( ); |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 4188 | } |
| 4189 | /* END_CASE */ |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 4190 | |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 4191 | /* BEGIN_CASE */ |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 4192 | 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] | 4193 | { |
| 4194 | mbedtls_ssl_session original, restored; |
| 4195 | unsigned char *buf = NULL; |
| 4196 | size_t len; |
| 4197 | |
| 4198 | /* |
| 4199 | * Test that a save-load pair is the identity |
| 4200 | */ |
| 4201 | |
| 4202 | mbedtls_ssl_session_init( &original ); |
| 4203 | mbedtls_ssl_session_init( &restored ); |
| 4204 | |
| 4205 | /* Prepare a dummy session to work on */ |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4206 | 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] | 4207 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 4208 | /* Serialize it */ |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 4209 | TEST_ASSERT( mbedtls_ssl_session_save( &original, NULL, 0, &len ) |
| 4210 | == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 4211 | TEST_ASSERT( ( buf = mbedtls_calloc( 1, len ) ) != NULL ); |
| 4212 | TEST_ASSERT( mbedtls_ssl_session_save( &original, buf, len, &len ) |
| 4213 | == 0 ); |
| 4214 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 4215 | /* Restore session from serialized data */ |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 4216 | TEST_ASSERT( mbedtls_ssl_session_load( &restored, buf, len) == 0 ); |
| 4217 | |
| 4218 | /* |
| 4219 | * Make sure both session structures are identical |
| 4220 | */ |
| 4221 | #if defined(MBEDTLS_HAVE_TIME) |
| 4222 | TEST_ASSERT( original.start == restored.start ); |
| 4223 | #endif |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4224 | TEST_ASSERT( original.minor_ver == restored.minor_ver ); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 4225 | TEST_ASSERT( original.ciphersuite == restored.ciphersuite ); |
| 4226 | TEST_ASSERT( original.compression == restored.compression ); |
| 4227 | TEST_ASSERT( original.id_len == restored.id_len ); |
| 4228 | TEST_ASSERT( memcmp( original.id, |
| 4229 | restored.id, sizeof( original.id ) ) == 0 ); |
| 4230 | TEST_ASSERT( memcmp( original.master, |
| 4231 | restored.master, sizeof( original.master ) ) == 0 ); |
| 4232 | |
| 4233 | #if defined(MBEDTLS_X509_CRT_PARSE_C) |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 4234 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 4235 | TEST_ASSERT( ( original.peer_cert == NULL ) == |
| 4236 | ( restored.peer_cert == NULL ) ); |
| 4237 | if( original.peer_cert != NULL ) |
| 4238 | { |
| 4239 | TEST_ASSERT( original.peer_cert->raw.len == |
| 4240 | restored.peer_cert->raw.len ); |
| 4241 | TEST_ASSERT( memcmp( original.peer_cert->raw.p, |
| 4242 | restored.peer_cert->raw.p, |
| 4243 | original.peer_cert->raw.len ) == 0 ); |
| 4244 | } |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 4245 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 4246 | TEST_ASSERT( original.peer_cert_digest_type == |
| 4247 | restored.peer_cert_digest_type ); |
| 4248 | TEST_ASSERT( original.peer_cert_digest_len == |
| 4249 | restored.peer_cert_digest_len ); |
| 4250 | TEST_ASSERT( ( original.peer_cert_digest == NULL ) == |
| 4251 | ( restored.peer_cert_digest == NULL ) ); |
| 4252 | if( original.peer_cert_digest != NULL ) |
| 4253 | { |
| 4254 | TEST_ASSERT( memcmp( original.peer_cert_digest, |
| 4255 | restored.peer_cert_digest, |
| 4256 | original.peer_cert_digest_len ) == 0 ); |
| 4257 | } |
| 4258 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 4259 | #endif /* MBEDTLS_X509_CRT_PARSE_C */ |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 4260 | TEST_ASSERT( original.verify_result == restored.verify_result ); |
| 4261 | |
| 4262 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
| 4263 | TEST_ASSERT( original.ticket_len == restored.ticket_len ); |
| 4264 | if( original.ticket_len != 0 ) |
| 4265 | { |
| 4266 | TEST_ASSERT( original.ticket != NULL ); |
| 4267 | TEST_ASSERT( restored.ticket != NULL ); |
| 4268 | TEST_ASSERT( memcmp( original.ticket, |
| 4269 | restored.ticket, original.ticket_len ) == 0 ); |
| 4270 | } |
| 4271 | TEST_ASSERT( original.ticket_lifetime == restored.ticket_lifetime ); |
| 4272 | #endif |
| 4273 | |
| 4274 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 4275 | TEST_ASSERT( original.mfl_code == restored.mfl_code ); |
| 4276 | #endif |
| 4277 | |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 4278 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 4279 | TEST_ASSERT( original.encrypt_then_mac == restored.encrypt_then_mac ); |
| 4280 | #endif |
| 4281 | |
| 4282 | exit: |
| 4283 | mbedtls_ssl_session_free( &original ); |
| 4284 | mbedtls_ssl_session_free( &restored ); |
| 4285 | mbedtls_free( buf ); |
| 4286 | } |
| 4287 | /* END_CASE */ |
| 4288 | |
Manuel Pégourié-Gonnard | aa75583 | 2019-06-03 10:53:47 +0200 | [diff] [blame] | 4289 | /* BEGIN_CASE */ |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 4290 | 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] | 4291 | { |
| 4292 | mbedtls_ssl_session session; |
| 4293 | unsigned char *buf1 = NULL, *buf2 = NULL; |
| 4294 | size_t len0, len1, len2; |
| 4295 | |
| 4296 | /* |
| 4297 | * Test that a load-save pair is the identity |
| 4298 | */ |
| 4299 | |
| 4300 | mbedtls_ssl_session_init( &session ); |
| 4301 | |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 4302 | /* Prepare a dummy session to work on */ |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4303 | 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] | 4304 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 4305 | /* Get desired buffer size for serializing */ |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 4306 | TEST_ASSERT( mbedtls_ssl_session_save( &session, NULL, 0, &len0 ) |
| 4307 | == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 4308 | |
| 4309 | /* Allocate first buffer */ |
| 4310 | buf1 = mbedtls_calloc( 1, len0 ); |
| 4311 | TEST_ASSERT( buf1 != NULL ); |
| 4312 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 4313 | /* Serialize to buffer and free live session */ |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 4314 | TEST_ASSERT( mbedtls_ssl_session_save( &session, buf1, len0, &len1 ) |
| 4315 | == 0 ); |
| 4316 | TEST_ASSERT( len0 == len1 ); |
| 4317 | mbedtls_ssl_session_free( &session ); |
| 4318 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 4319 | /* Restore session from serialized data */ |
Manuel Pégourié-Gonnard | 220403b | 2019-05-24 09:54:21 +0200 | [diff] [blame] | 4320 | TEST_ASSERT( mbedtls_ssl_session_load( &session, buf1, len1 ) == 0 ); |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 4321 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 4322 | /* Allocate second buffer and serialize to it */ |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 4323 | buf2 = mbedtls_calloc( 1, len0 ); |
Manuel Pégourié-Gonnard | b407990 | 2019-05-24 09:52:10 +0200 | [diff] [blame] | 4324 | TEST_ASSERT( buf2 != NULL ); |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 4325 | TEST_ASSERT( mbedtls_ssl_session_save( &session, buf2, len0, &len2 ) |
| 4326 | == 0 ); |
| 4327 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 4328 | /* Make sure both serialized versions are identical */ |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 4329 | TEST_ASSERT( len1 == len2 ); |
| 4330 | TEST_ASSERT( memcmp( buf1, buf2, len1 ) == 0 ); |
| 4331 | |
| 4332 | exit: |
| 4333 | mbedtls_ssl_session_free( &session ); |
| 4334 | mbedtls_free( buf1 ); |
| 4335 | mbedtls_free( buf2 ); |
| 4336 | } |
| 4337 | /* END_CASE */ |
Manuel Pégourié-Gonnard | f5fa0aa | 2019-05-23 10:38:11 +0200 | [diff] [blame] | 4338 | |
| 4339 | /* BEGIN_CASE */ |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 4340 | 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] | 4341 | { |
| 4342 | mbedtls_ssl_session session; |
| 4343 | unsigned char *buf = NULL; |
| 4344 | size_t good_len, bad_len, test_len; |
| 4345 | |
| 4346 | /* |
| 4347 | * Test that session_save() fails cleanly on small buffers |
| 4348 | */ |
| 4349 | |
| 4350 | mbedtls_ssl_session_init( &session ); |
| 4351 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 4352 | /* Prepare dummy session and get serialized size */ |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4353 | 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] | 4354 | TEST_ASSERT( mbedtls_ssl_session_save( &session, NULL, 0, &good_len ) |
| 4355 | == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 4356 | |
| 4357 | /* Try all possible bad lengths */ |
| 4358 | for( bad_len = 1; bad_len < good_len; bad_len++ ) |
| 4359 | { |
| 4360 | /* Allocate exact size so that asan/valgrind can detect any overwrite */ |
| 4361 | mbedtls_free( buf ); |
| 4362 | TEST_ASSERT( ( buf = mbedtls_calloc( 1, bad_len ) ) != NULL ); |
| 4363 | TEST_ASSERT( mbedtls_ssl_session_save( &session, buf, bad_len, |
| 4364 | &test_len ) |
| 4365 | == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 4366 | TEST_ASSERT( test_len == good_len ); |
| 4367 | } |
| 4368 | |
| 4369 | exit: |
| 4370 | mbedtls_ssl_session_free( &session ); |
| 4371 | mbedtls_free( buf ); |
| 4372 | } |
| 4373 | /* END_CASE */ |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 4374 | |
| 4375 | /* BEGIN_CASE */ |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 4376 | 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] | 4377 | { |
| 4378 | mbedtls_ssl_session session; |
| 4379 | unsigned char *good_buf = NULL, *bad_buf = NULL; |
| 4380 | size_t good_len, bad_len; |
| 4381 | |
| 4382 | /* |
| 4383 | * Test that session_load() fails cleanly on small buffers |
| 4384 | */ |
| 4385 | |
| 4386 | mbedtls_ssl_session_init( &session ); |
| 4387 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 4388 | /* Prepare serialized session data */ |
Hanno Becker | fadbdbb | 2021-07-23 06:25:48 +0100 | [diff] [blame] | 4389 | 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] | 4390 | TEST_ASSERT( mbedtls_ssl_session_save( &session, NULL, 0, &good_len ) |
| 4391 | == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL ); |
| 4392 | TEST_ASSERT( ( good_buf = mbedtls_calloc( 1, good_len ) ) != NULL ); |
| 4393 | TEST_ASSERT( mbedtls_ssl_session_save( &session, good_buf, good_len, |
| 4394 | &good_len ) == 0 ); |
| 4395 | mbedtls_ssl_session_free( &session ); |
| 4396 | |
| 4397 | /* Try all possible bad lengths */ |
| 4398 | for( bad_len = 0; bad_len < good_len; bad_len++ ) |
| 4399 | { |
| 4400 | /* Allocate exact size so that asan/valgrind can detect any overread */ |
| 4401 | mbedtls_free( bad_buf ); |
| 4402 | bad_buf = mbedtls_calloc( 1, bad_len ? bad_len : 1 ); |
| 4403 | TEST_ASSERT( bad_buf != NULL ); |
| 4404 | memcpy( bad_buf, good_buf, bad_len ); |
| 4405 | |
| 4406 | TEST_ASSERT( mbedtls_ssl_session_load( &session, bad_buf, bad_len ) |
| 4407 | == MBEDTLS_ERR_SSL_BAD_INPUT_DATA ); |
| 4408 | } |
| 4409 | |
| 4410 | exit: |
| 4411 | mbedtls_ssl_session_free( &session ); |
| 4412 | mbedtls_free( good_buf ); |
| 4413 | mbedtls_free( bad_buf ); |
| 4414 | } |
| 4415 | /* END_CASE */ |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 4416 | |
Hanno Becker | 363b646 | 2019-05-29 12:44:28 +0100 | [diff] [blame] | 4417 | /* BEGIN_CASE */ |
| 4418 | void ssl_session_serialize_version_check( int corrupt_major, |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 4419 | int corrupt_minor, |
| 4420 | int corrupt_patch, |
| 4421 | int corrupt_config ) |
| 4422 | { |
Hanno Becker | 363b646 | 2019-05-29 12:44:28 +0100 | [diff] [blame] | 4423 | unsigned char serialized_session[ 2048 ]; |
| 4424 | size_t serialized_session_len; |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 4425 | unsigned cur_byte; |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 4426 | mbedtls_ssl_session session; |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 4427 | uint8_t should_corrupt_byte[] = { corrupt_major == 1, |
| 4428 | corrupt_minor == 1, |
| 4429 | corrupt_patch == 1, |
| 4430 | corrupt_config == 1, |
| 4431 | corrupt_config == 1 }; |
| 4432 | |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 4433 | mbedtls_ssl_session_init( &session ); |
Paul Elliott | 46a6c20 | 2021-12-09 18:16:13 +0000 | [diff] [blame] | 4434 | TEST_ASSERT( ssl_populate_session_tls12( &session, 0, NULL ) == 0 ); |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 4435 | |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 4436 | /* Infer length of serialized session. */ |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 4437 | TEST_ASSERT( mbedtls_ssl_session_save( &session, |
Hanno Becker | 363b646 | 2019-05-29 12:44:28 +0100 | [diff] [blame] | 4438 | serialized_session, |
| 4439 | sizeof( serialized_session ), |
| 4440 | &serialized_session_len ) == 0 ); |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 4441 | |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 4442 | mbedtls_ssl_session_free( &session ); |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 4443 | |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 4444 | /* Without any modification, we should be able to successfully |
Hanno Becker | 363b646 | 2019-05-29 12:44:28 +0100 | [diff] [blame] | 4445 | * de-serialize the session - double-check that. */ |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 4446 | TEST_ASSERT( mbedtls_ssl_session_load( &session, |
Hanno Becker | 363b646 | 2019-05-29 12:44:28 +0100 | [diff] [blame] | 4447 | serialized_session, |
| 4448 | serialized_session_len ) == 0 ); |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 4449 | mbedtls_ssl_session_free( &session ); |
| 4450 | |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 4451 | /* Go through the bytes in the serialized session header and |
| 4452 | * corrupt them bit-by-bit. */ |
| 4453 | for( cur_byte = 0; cur_byte < sizeof( should_corrupt_byte ); cur_byte++ ) |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 4454 | { |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 4455 | int cur_bit; |
| 4456 | unsigned char * const byte = &serialized_session[ cur_byte ]; |
| 4457 | |
| 4458 | if( should_corrupt_byte[ cur_byte ] == 0 ) |
| 4459 | continue; |
| 4460 | |
| 4461 | for( cur_bit = 0; cur_bit < CHAR_BIT; cur_bit++ ) |
| 4462 | { |
| 4463 | unsigned char const corrupted_bit = 0x1u << cur_bit; |
| 4464 | /* Modify a single bit in the serialized session. */ |
| 4465 | *byte ^= corrupted_bit; |
| 4466 | |
| 4467 | /* Attempt to deserialize */ |
| 4468 | TEST_ASSERT( mbedtls_ssl_session_load( &session, |
| 4469 | serialized_session, |
| 4470 | serialized_session_len ) == |
Hanno Becker | f9b3303 | 2019-06-03 12:58:39 +0100 | [diff] [blame] | 4471 | MBEDTLS_ERR_SSL_VERSION_MISMATCH ); |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 4472 | |
| 4473 | /* Undo the change */ |
| 4474 | *byte ^= corrupted_bit; |
| 4475 | } |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 4476 | } |
| 4477 | |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 4478 | } |
| 4479 | /* END_CASE */ |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4480 | |
Manuel Pégourié-Gonnard | d12402f | 2020-05-20 10:34:25 +0200 | [diff] [blame] | 4481 | /* 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] | 4482 | void mbedtls_endpoint_sanity( int endpoint_type ) |
| 4483 | { |
| 4484 | enum { BUFFSIZE = 1024 }; |
| 4485 | mbedtls_endpoint ep; |
| 4486 | int ret = -1; |
| 4487 | |
Andrzej Kurek | 15daf50 | 2020-02-12 09:17:52 -0500 | [diff] [blame] | 4488 | ret = mbedtls_endpoint_init( NULL, endpoint_type, MBEDTLS_PK_RSA, |
| 4489 | NULL, NULL, NULL ); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4490 | TEST_ASSERT( MBEDTLS_ERR_SSL_BAD_INPUT_DATA == ret ); |
| 4491 | |
Andrzej Kurek | b298074 | 2020-02-02 19:25:26 -0500 | [diff] [blame] | 4492 | ret = mbedtls_endpoint_certificate_init( NULL, MBEDTLS_PK_RSA ); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4493 | TEST_ASSERT( MBEDTLS_ERR_SSL_BAD_INPUT_DATA == ret ); |
| 4494 | |
Andrzej Kurek | 15daf50 | 2020-02-12 09:17:52 -0500 | [diff] [blame] | 4495 | ret = mbedtls_endpoint_init( &ep, endpoint_type, MBEDTLS_PK_RSA, |
| 4496 | NULL, NULL, NULL ); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4497 | TEST_ASSERT( ret == 0 ); |
| 4498 | |
| 4499 | exit: |
Andrzej Kurek | 15daf50 | 2020-02-12 09:17:52 -0500 | [diff] [blame] | 4500 | mbedtls_endpoint_free( &ep, NULL ); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4501 | } |
| 4502 | /* END_CASE */ |
| 4503 | |
Manuel Pégourié-Gonnard | d12402f | 2020-05-20 10:34:25 +0200 | [diff] [blame] | 4504 | /* 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] | 4505 | void move_handshake_to_state(int endpoint_type, int state, int need_pass) |
| 4506 | { |
| 4507 | enum { BUFFSIZE = 1024 }; |
| 4508 | mbedtls_endpoint base_ep, second_ep; |
| 4509 | int ret = -1; |
| 4510 | |
Andrzej Kurek | 15daf50 | 2020-02-12 09:17:52 -0500 | [diff] [blame] | 4511 | ret = mbedtls_endpoint_init( &base_ep, endpoint_type, MBEDTLS_PK_RSA, |
| 4512 | NULL, NULL, NULL ); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4513 | TEST_ASSERT( ret == 0 ); |
| 4514 | |
| 4515 | ret = mbedtls_endpoint_init( &second_ep, |
| 4516 | ( endpoint_type == MBEDTLS_SSL_IS_SERVER ) ? |
Andrzej Kurek | b298074 | 2020-02-02 19:25:26 -0500 | [diff] [blame] | 4517 | MBEDTLS_SSL_IS_CLIENT : MBEDTLS_SSL_IS_SERVER, |
Andrzej Kurek | 15daf50 | 2020-02-12 09:17:52 -0500 | [diff] [blame] | 4518 | MBEDTLS_PK_RSA, NULL, NULL, NULL ); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4519 | TEST_ASSERT( ret == 0 ); |
| 4520 | |
| 4521 | ret = mbedtls_mock_socket_connect( &(base_ep.socket), |
| 4522 | &(second_ep.socket), |
| 4523 | BUFFSIZE ); |
| 4524 | TEST_ASSERT( ret == 0 ); |
| 4525 | |
| 4526 | ret = mbedtls_move_handshake_to_state( &(base_ep.ssl), |
| 4527 | &(second_ep.ssl), |
| 4528 | state ); |
| 4529 | if( need_pass ) |
| 4530 | { |
| 4531 | TEST_ASSERT( ret == 0 ); |
| 4532 | TEST_ASSERT( base_ep.ssl.state == state ); |
| 4533 | } |
| 4534 | else |
| 4535 | { |
| 4536 | TEST_ASSERT( ret != 0 ); |
| 4537 | TEST_ASSERT( base_ep.ssl.state != state ); |
| 4538 | } |
| 4539 | |
| 4540 | exit: |
Andrzej Kurek | 15daf50 | 2020-02-12 09:17:52 -0500 | [diff] [blame] | 4541 | mbedtls_endpoint_free( &base_ep, NULL ); |
| 4542 | mbedtls_endpoint_free( &second_ep, NULL ); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4543 | } |
| 4544 | /* END_CASE */ |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 4545 | |
Manuel Pégourié-Gonnard | d12402f | 2020-05-20 10:34:25 +0200 | [diff] [blame] | 4546 | /* 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] | 4547 | void handshake_version( int dtls, int client_min_version, int client_max_version, |
| 4548 | int server_min_version, int server_max_version, |
| 4549 | int expected_negotiated_version ) |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 4550 | { |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 4551 | handshake_test_options options; |
| 4552 | init_handshake_options( &options ); |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 4553 | |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 4554 | options.client_min_version = client_min_version; |
| 4555 | options.client_max_version = client_max_version; |
| 4556 | options.server_min_version = server_min_version; |
| 4557 | options.server_max_version = server_max_version; |
| 4558 | |
| 4559 | options.expected_negotiated_version = expected_negotiated_version; |
| 4560 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 4561 | options.dtls = dtls; |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 4562 | perform_handshake( &options ); |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 4563 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 4564 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 4565 | goto exit; |
| 4566 | } |
| 4567 | /* END_CASE */ |
Andrzej Kurek | 9e9efdc | 2020-02-26 05:25:23 -0500 | [diff] [blame] | 4568 | |
Manuel Pégourié-Gonnard | d12402f | 2020-05-20 10:34:25 +0200 | [diff] [blame] | 4569 | /* 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] | 4570 | void handshake_psk_cipher( char* cipher, int pk_alg, data_t *psk_str, int dtls ) |
| 4571 | { |
| 4572 | handshake_test_options options; |
| 4573 | init_handshake_options( &options ); |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 4574 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 4575 | options.cipher = cipher; |
| 4576 | options.dtls = dtls; |
| 4577 | options.psk_str = psk_str; |
| 4578 | options.pk_alg = pk_alg; |
Andrzej Kurek | cc5169c | 2020-02-04 09:04:56 -0500 | [diff] [blame] | 4579 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 4580 | perform_handshake( &options ); |
Andrzej Kurek | 316da1f | 2020-02-26 09:03:47 -0500 | [diff] [blame] | 4581 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 4582 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 4583 | goto exit; |
| 4584 | } |
| 4585 | /* END_CASE */ |
Andrzej Kurek | 316da1f | 2020-02-26 09:03:47 -0500 | [diff] [blame] | 4586 | |
Manuel Pégourié-Gonnard | d12402f | 2020-05-20 10:34:25 +0200 | [diff] [blame] | 4587 | /* 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] | 4588 | void handshake_cipher( char* cipher, int pk_alg, int dtls ) |
| 4589 | { |
| 4590 | test_handshake_psk_cipher( cipher, pk_alg, NULL, dtls ); |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 4591 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 4592 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 4593 | goto exit; |
| 4594 | } |
| 4595 | /* END_CASE */ |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 4596 | |
Manuel Pégourié-Gonnard | d12402f | 2020-05-20 10:34:25 +0200 | [diff] [blame] | 4597 | /* 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] | 4598 | void app_data( int mfl, int cli_msg_len, int srv_msg_len, |
| 4599 | int expected_cli_fragments, |
| 4600 | int expected_srv_fragments, int dtls ) |
| 4601 | { |
| 4602 | handshake_test_options options; |
| 4603 | init_handshake_options( &options ); |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 4604 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 4605 | options.mfl = mfl; |
| 4606 | options.cli_msg_len = cli_msg_len; |
| 4607 | options.srv_msg_len = srv_msg_len; |
| 4608 | options.expected_cli_fragments = expected_cli_fragments; |
| 4609 | options.expected_srv_fragments = expected_srv_fragments; |
| 4610 | options.dtls = dtls; |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 4611 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 4612 | perform_handshake( &options ); |
| 4613 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 4614 | goto exit; |
| 4615 | } |
| 4616 | /* END_CASE */ |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 4617 | |
Manuel Pégourié-Gonnard | d12402f | 2020-05-20 10:34:25 +0200 | [diff] [blame] | 4618 | /* 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] | 4619 | void app_data_tls( int mfl, int cli_msg_len, int srv_msg_len, |
| 4620 | int expected_cli_fragments, |
| 4621 | int expected_srv_fragments ) |
| 4622 | { |
| 4623 | test_app_data( mfl, cli_msg_len, srv_msg_len, expected_cli_fragments, |
| 4624 | expected_srv_fragments, 0 ); |
| 4625 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 4626 | goto exit; |
| 4627 | } |
| 4628 | /* END_CASE */ |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 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_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] | 4631 | void app_data_dtls( int mfl, int cli_msg_len, int srv_msg_len, |
| 4632 | int expected_cli_fragments, |
| 4633 | int expected_srv_fragments ) |
| 4634 | { |
| 4635 | test_app_data( mfl, cli_msg_len, srv_msg_len, expected_cli_fragments, |
| 4636 | expected_srv_fragments, 1 ); |
| 4637 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 4638 | goto exit; |
| 4639 | } |
| 4640 | /* END_CASE */ |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 4641 | |
Manuel Pégourié-Gonnard | d12402f | 2020-05-20 10:34:25 +0200 | [diff] [blame] | 4642 | /* 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] | 4643 | void handshake_serialization( ) |
| 4644 | { |
| 4645 | handshake_test_options options; |
| 4646 | init_handshake_options( &options ); |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 4647 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 4648 | options.serialize = 1; |
| 4649 | options.dtls = 1; |
| 4650 | perform_handshake( &options ); |
| 4651 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 4652 | goto exit; |
| 4653 | } |
| 4654 | /* END_CASE */ |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 4655 | |
Manuel Pégourié-Gonnard | d12402f | 2020-05-20 10:34:25 +0200 | [diff] [blame] | 4656 | /* 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] | 4657 | void handshake_fragmentation( int mfl, int expected_srv_hs_fragmentation, int expected_cli_hs_fragmentation) |
| 4658 | { |
| 4659 | handshake_test_options options; |
| 4660 | log_pattern srv_pattern, cli_pattern; |
| 4661 | |
| 4662 | srv_pattern.pattern = cli_pattern.pattern = "found fragmented DTLS handshake"; |
| 4663 | srv_pattern.counter = 0; |
| 4664 | cli_pattern.counter = 0; |
| 4665 | |
| 4666 | init_handshake_options( &options ); |
| 4667 | options.dtls = 1; |
| 4668 | options.mfl = mfl; |
Darryl Green | aad82f9 | 2019-12-02 10:53:11 +0000 | [diff] [blame] | 4669 | /* Set cipher to one using CBC so that record splitting can be tested */ |
| 4670 | options.cipher = "TLS-DHE-RSA-WITH-AES-256-CBC-SHA256"; |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 4671 | options.srv_auth_mode = MBEDTLS_SSL_VERIFY_REQUIRED; |
| 4672 | options.srv_log_obj = &srv_pattern; |
| 4673 | options.cli_log_obj = &cli_pattern; |
| 4674 | options.srv_log_fun = log_analyzer; |
| 4675 | options.cli_log_fun = log_analyzer; |
| 4676 | |
| 4677 | perform_handshake( &options ); |
| 4678 | |
| 4679 | /* Test if the server received a fragmented handshake */ |
| 4680 | if( expected_srv_hs_fragmentation ) |
| 4681 | { |
| 4682 | TEST_ASSERT( srv_pattern.counter >= 1 ); |
| 4683 | } |
| 4684 | /* Test if the client received a fragmented handshake */ |
| 4685 | if( expected_cli_hs_fragmentation ) |
| 4686 | { |
| 4687 | TEST_ASSERT( cli_pattern.counter >= 1 ); |
| 4688 | } |
| 4689 | } |
| 4690 | /* END_CASE */ |
| 4691 | |
Manuel Pégourié-Gonnard | d12402f | 2020-05-20 10:34:25 +0200 | [diff] [blame] | 4692 | /* 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] | 4693 | void renegotiation( int legacy_renegotiation ) |
| 4694 | { |
| 4695 | handshake_test_options options; |
| 4696 | init_handshake_options( &options ); |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 4697 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 4698 | options.renegotiate = 1; |
| 4699 | options.legacy_renegotiation = legacy_renegotiation; |
| 4700 | options.dtls = 1; |
Andrzej Kurek | 316da1f | 2020-02-26 09:03:47 -0500 | [diff] [blame] | 4701 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 4702 | perform_handshake( &options ); |
| 4703 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 4704 | goto exit; |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 4705 | } |
| 4706 | /* END_CASE */ |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 4707 | |
Manuel Pégourié-Gonnard | d12402f | 2020-05-20 10:34:25 +0200 | [diff] [blame] | 4708 | /* 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] | 4709 | void resize_buffers( int mfl, int renegotiation, int legacy_renegotiation, |
Andrzej Kurek | 8ea6872 | 2020-04-03 06:40:47 -0400 | [diff] [blame] | 4710 | int serialize, int dtls, char *cipher ) |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 4711 | { |
| 4712 | handshake_test_options options; |
| 4713 | init_handshake_options( &options ); |
| 4714 | |
| 4715 | options.mfl = mfl; |
Andrzej Kurek | 8ea6872 | 2020-04-03 06:40:47 -0400 | [diff] [blame] | 4716 | options.cipher = cipher; |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 4717 | options.renegotiate = renegotiation; |
| 4718 | options.legacy_renegotiation = legacy_renegotiation; |
| 4719 | options.serialize = serialize; |
| 4720 | options.dtls = dtls; |
| 4721 | options.resize_buffers = 1; |
| 4722 | |
| 4723 | perform_handshake( &options ); |
| 4724 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 4725 | goto exit; |
| 4726 | } |
| 4727 | /* END_CASE */ |
| 4728 | |
Manuel Pégourié-Gonnard | d12402f | 2020-05-20 10:34:25 +0200 | [diff] [blame] | 4729 | /* 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] | 4730 | void resize_buffers_serialize_mfl( int mfl ) |
| 4731 | { |
Andrzej Kurek | 8ea6872 | 2020-04-03 06:40:47 -0400 | [diff] [blame] | 4732 | test_resize_buffers( mfl, 0, MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION, 1, 1, |
| 4733 | (char *) "" ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 4734 | |
| 4735 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 4736 | goto exit; |
| 4737 | } |
| 4738 | /* END_CASE */ |
| 4739 | |
Manuel Pégourié-Gonnard | d12402f | 2020-05-20 10:34:25 +0200 | [diff] [blame] | 4740 | /* 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] | 4741 | void resize_buffers_renegotiate_mfl( int mfl, int legacy_renegotiation, |
| 4742 | char *cipher ) |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 4743 | { |
Andrzej Kurek | 8ea6872 | 2020-04-03 06:40:47 -0400 | [diff] [blame] | 4744 | test_resize_buffers( mfl, 1, legacy_renegotiation, 0, 1, cipher ); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 4745 | |
| 4746 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 4747 | goto exit; |
| 4748 | } |
| 4749 | /* END_CASE */ |
Manuel Pégourié-Gonnard | 045f094 | 2020-07-02 11:34:02 +0200 | [diff] [blame] | 4750 | |
Manuel Pégourié-Gonnard | ed0e864 | 2020-07-21 11:20:30 +0200 | [diff] [blame] | 4751 | /* 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] | 4752 | void ssl_cf_hmac( int hash ) |
| 4753 | { |
| 4754 | /* |
Gabor Mezei | 90437e3 | 2021-10-20 11:59:27 +0200 | [diff] [blame] | 4755 | * Test the function mbedtls_ct_hmac() against a reference |
Manuel Pégourié-Gonnard | 045f094 | 2020-07-02 11:34:02 +0200 | [diff] [blame] | 4756 | * implementation. |
Manuel Pégourié-Gonnard | 045f094 | 2020-07-02 11:34:02 +0200 | [diff] [blame] | 4757 | */ |
| 4758 | mbedtls_md_context_t ctx, ref_ctx; |
| 4759 | const mbedtls_md_info_t *md_info; |
| 4760 | size_t out_len, block_size; |
| 4761 | size_t min_in_len, in_len, max_in_len, i; |
| 4762 | /* TLS additional data is 13 bytes (hence the "lucky 13" name) */ |
| 4763 | unsigned char add_data[13]; |
| 4764 | unsigned char ref_out[MBEDTLS_MD_MAX_SIZE]; |
| 4765 | unsigned char *data = NULL; |
| 4766 | unsigned char *out = NULL; |
| 4767 | unsigned char rec_num = 0; |
| 4768 | |
| 4769 | mbedtls_md_init( &ctx ); |
| 4770 | mbedtls_md_init( &ref_ctx ); |
| 4771 | |
| 4772 | md_info = mbedtls_md_info_from_type( hash ); |
| 4773 | TEST_ASSERT( md_info != NULL ); |
| 4774 | out_len = mbedtls_md_get_size( md_info ); |
| 4775 | TEST_ASSERT( out_len != 0 ); |
| 4776 | block_size = hash == MBEDTLS_MD_SHA384 ? 128 : 64; |
| 4777 | |
| 4778 | /* Use allocated out buffer to catch overwrites */ |
| 4779 | ASSERT_ALLOC( out, out_len ); |
| 4780 | |
| 4781 | /* Set up contexts with the given hash and a dummy key */ |
| 4782 | TEST_EQUAL( 0, mbedtls_md_setup( &ctx, md_info, 1 ) ); |
| 4783 | TEST_EQUAL( 0, mbedtls_md_setup( &ref_ctx, md_info, 1 ) ); |
| 4784 | memset( ref_out, 42, sizeof( ref_out ) ); |
| 4785 | TEST_EQUAL( 0, mbedtls_md_hmac_starts( &ctx, ref_out, out_len ) ); |
| 4786 | TEST_EQUAL( 0, mbedtls_md_hmac_starts( &ref_ctx, ref_out, out_len ) ); |
| 4787 | memset( ref_out, 0, sizeof( ref_out ) ); |
| 4788 | |
| 4789 | /* |
| 4790 | * Test all possible lengths up to a point. The difference between |
| 4791 | * max_in_len and min_in_len is at most 255, and make sure they both vary |
| 4792 | * by at least one block size. |
| 4793 | */ |
| 4794 | for( max_in_len = 0; max_in_len <= 255 + block_size; max_in_len++ ) |
| 4795 | { |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 4796 | mbedtls_test_set_step( max_in_len * 10000 ); |
Manuel Pégourié-Gonnard | ca8287c | 2020-07-22 10:29:39 +0200 | [diff] [blame] | 4797 | |
Manuel Pégourié-Gonnard | 045f094 | 2020-07-02 11:34:02 +0200 | [diff] [blame] | 4798 | /* Use allocated in buffer to catch overreads */ |
Manuel Pégourié-Gonnard | c321900 | 2020-07-22 10:32:52 +0200 | [diff] [blame] | 4799 | ASSERT_ALLOC( data, max_in_len ); |
Manuel Pégourié-Gonnard | 045f094 | 2020-07-02 11:34:02 +0200 | [diff] [blame] | 4800 | |
| 4801 | min_in_len = max_in_len > 255 ? max_in_len - 255 : 0; |
| 4802 | for( in_len = min_in_len; in_len <= max_in_len; in_len++ ) |
| 4803 | { |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 4804 | mbedtls_test_set_step( max_in_len * 10000 + in_len ); |
Manuel Pégourié-Gonnard | ca8287c | 2020-07-22 10:29:39 +0200 | [diff] [blame] | 4805 | |
Manuel Pégourié-Gonnard | 045f094 | 2020-07-02 11:34:02 +0200 | [diff] [blame] | 4806 | /* Set up dummy data and add_data */ |
| 4807 | rec_num++; |
| 4808 | memset( add_data, rec_num, sizeof( add_data ) ); |
| 4809 | for( i = 0; i < in_len; i++ ) |
| 4810 | data[i] = ( i & 0xff ) ^ rec_num; |
| 4811 | |
| 4812 | /* Get the function's result */ |
Manuel Pégourié-Gonnard | 9670a59 | 2020-07-10 10:21:46 +0200 | [diff] [blame] | 4813 | TEST_CF_SECRET( &in_len, sizeof( in_len ) ); |
Gabor Mezei | 90437e3 | 2021-10-20 11:59:27 +0200 | [diff] [blame] | 4814 | 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] | 4815 | data, in_len, |
| 4816 | min_in_len, max_in_len, |
| 4817 | out ) ); |
Manuel Pégourié-Gonnard | 9670a59 | 2020-07-10 10:21:46 +0200 | [diff] [blame] | 4818 | TEST_CF_PUBLIC( &in_len, sizeof( in_len ) ); |
| 4819 | TEST_CF_PUBLIC( out, out_len ); |
Manuel Pégourié-Gonnard | 045f094 | 2020-07-02 11:34:02 +0200 | [diff] [blame] | 4820 | |
| 4821 | /* Compute the reference result */ |
| 4822 | TEST_EQUAL( 0, mbedtls_md_hmac_update( &ref_ctx, add_data, |
| 4823 | sizeof( add_data ) ) ); |
| 4824 | TEST_EQUAL( 0, mbedtls_md_hmac_update( &ref_ctx, data, in_len ) ); |
| 4825 | TEST_EQUAL( 0, mbedtls_md_hmac_finish( &ref_ctx, ref_out ) ); |
| 4826 | TEST_EQUAL( 0, mbedtls_md_hmac_reset( &ref_ctx ) ); |
| 4827 | |
| 4828 | /* Compare */ |
| 4829 | ASSERT_COMPARE( out, out_len, ref_out, out_len ); |
| 4830 | } |
| 4831 | |
| 4832 | mbedtls_free( data ); |
| 4833 | data = NULL; |
| 4834 | } |
| 4835 | |
| 4836 | exit: |
| 4837 | mbedtls_md_free( &ref_ctx ); |
| 4838 | mbedtls_md_free( &ctx ); |
| 4839 | |
| 4840 | mbedtls_free( data ); |
| 4841 | mbedtls_free( out ); |
| 4842 | } |
| 4843 | /* END_CASE */ |
Manuel Pégourié-Gonnard | 7fe2c5f | 2020-08-18 12:02:54 +0200 | [diff] [blame] | 4844 | |
| 4845 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_SOME_SUITES_USE_TLS_CBC:MBEDTLS_TEST_HOOKS */ |
| 4846 | void ssl_cf_memcpy_offset( int offset_min, int offset_max, int len ) |
| 4847 | { |
| 4848 | unsigned char *dst = NULL; |
| 4849 | unsigned char *src = NULL; |
| 4850 | size_t src_len = offset_max + len; |
| 4851 | size_t secret; |
| 4852 | |
| 4853 | ASSERT_ALLOC( dst, len ); |
| 4854 | ASSERT_ALLOC( src, src_len ); |
| 4855 | |
| 4856 | /* Fill src in a way that we can detect if we copied the right bytes */ |
| 4857 | mbedtls_test_rnd_std_rand( NULL, src, src_len ); |
| 4858 | |
| 4859 | for( secret = offset_min; secret <= (size_t) offset_max; secret++ ) |
| 4860 | { |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 4861 | mbedtls_test_set_step( (int) secret ); |
Manuel Pégourié-Gonnard | 7fe2c5f | 2020-08-18 12:02:54 +0200 | [diff] [blame] | 4862 | |
| 4863 | TEST_CF_SECRET( &secret, sizeof( secret ) ); |
Gabor Mezei | 90437e3 | 2021-10-20 11:59:27 +0200 | [diff] [blame] | 4864 | mbedtls_ct_memcpy_offset( dst, src, secret, |
gabor-mezei-arm | 9fa43ce | 2021-09-28 16:14:47 +0200 | [diff] [blame] | 4865 | offset_min, offset_max, len ); |
Manuel Pégourié-Gonnard | 7fe2c5f | 2020-08-18 12:02:54 +0200 | [diff] [blame] | 4866 | TEST_CF_PUBLIC( &secret, sizeof( secret ) ); |
| 4867 | TEST_CF_PUBLIC( dst, len ); |
| 4868 | |
| 4869 | ASSERT_COMPARE( dst, len, src + secret, len ); |
| 4870 | } |
| 4871 | |
| 4872 | exit: |
| 4873 | mbedtls_free( dst ); |
| 4874 | mbedtls_free( src ); |
| 4875 | } |
| 4876 | /* END_CASE */ |
Hanno Becker | 6667ffd | 2021-04-19 21:59:22 +0100 | [diff] [blame] | 4877 | |
| 4878 | /* BEGIN_CASE depends_on:MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */ |
| 4879 | void test_multiple_psks() |
| 4880 | { |
| 4881 | unsigned char psk0[10] = { 0 }; |
| 4882 | unsigned char psk0_identity[] = { 'f', 'o', 'o' }; |
| 4883 | |
| 4884 | unsigned char psk1[10] = { 0 }; |
| 4885 | unsigned char psk1_identity[] = { 'b', 'a', 'r' }; |
| 4886 | |
| 4887 | mbedtls_ssl_config conf; |
| 4888 | |
| 4889 | mbedtls_ssl_config_init( &conf ); |
| 4890 | |
| 4891 | TEST_ASSERT( mbedtls_ssl_conf_psk( &conf, |
| 4892 | psk0, sizeof( psk0 ), |
| 4893 | psk0_identity, sizeof( psk0_identity ) ) == 0 ); |
| 4894 | TEST_ASSERT( mbedtls_ssl_conf_psk( &conf, |
| 4895 | psk1, sizeof( psk1 ), |
| 4896 | psk1_identity, sizeof( psk1_identity ) ) == |
| 4897 | MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 4898 | |
| 4899 | exit: |
| 4900 | |
| 4901 | mbedtls_ssl_config_free( &conf ); |
| 4902 | } |
| 4903 | /* END_CASE */ |
| 4904 | |
| 4905 | /* BEGIN_CASE depends_on:MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED:MBEDTLS_USE_PSA_CRYPTO */ |
| 4906 | void test_multiple_psks_opaque( int mode ) |
| 4907 | { |
| 4908 | /* |
| 4909 | * Mode 0: Raw PSK, then opaque PSK |
| 4910 | * Mode 1: Opaque PSK, then raw PSK |
| 4911 | * Mode 2: 2x opaque PSK |
| 4912 | */ |
| 4913 | |
| 4914 | unsigned char psk0_raw[10] = { 0 }; |
| 4915 | unsigned char psk0_raw_identity[] = { 'f', 'o', 'o' }; |
| 4916 | |
Andrzej Kurek | 03e0146 | 2022-01-03 12:53:24 +0100 | [diff] [blame] | 4917 | mbedtls_svc_key_id_t psk0_opaque = mbedtls_svc_key_id_make( 0x1, (psa_key_id_t) 1 ); |
| 4918 | |
Hanno Becker | 6667ffd | 2021-04-19 21:59:22 +0100 | [diff] [blame] | 4919 | unsigned char psk0_opaque_identity[] = { 'f', 'o', 'o' }; |
| 4920 | |
| 4921 | unsigned char psk1_raw[10] = { 0 }; |
| 4922 | unsigned char psk1_raw_identity[] = { 'b', 'a', 'r' }; |
| 4923 | |
Andrzej Kurek | 03e0146 | 2022-01-03 12:53:24 +0100 | [diff] [blame] | 4924 | mbedtls_svc_key_id_t psk1_opaque = mbedtls_svc_key_id_make( 0x1, (psa_key_id_t) 2 ); |
| 4925 | |
Hanno Becker | 6667ffd | 2021-04-19 21:59:22 +0100 | [diff] [blame] | 4926 | unsigned char psk1_opaque_identity[] = { 'b', 'a', 'r' }; |
| 4927 | |
| 4928 | mbedtls_ssl_config conf; |
| 4929 | |
| 4930 | USE_PSA_INIT( ); |
| 4931 | mbedtls_ssl_config_init( &conf ); |
| 4932 | |
| 4933 | switch( mode ) |
| 4934 | { |
| 4935 | case 0: |
| 4936 | |
| 4937 | TEST_ASSERT( mbedtls_ssl_conf_psk( &conf, |
| 4938 | psk0_raw, sizeof( psk0_raw ), |
| 4939 | psk0_raw_identity, sizeof( psk0_raw_identity ) ) |
| 4940 | == 0 ); |
| 4941 | TEST_ASSERT( mbedtls_ssl_conf_psk_opaque( &conf, |
| 4942 | psk1_opaque, |
| 4943 | psk1_opaque_identity, sizeof( psk1_opaque_identity ) ) |
| 4944 | == MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 4945 | break; |
| 4946 | |
| 4947 | case 1: |
| 4948 | |
| 4949 | TEST_ASSERT( mbedtls_ssl_conf_psk_opaque( &conf, |
| 4950 | psk0_opaque, |
| 4951 | psk0_opaque_identity, sizeof( psk0_opaque_identity ) ) |
| 4952 | == 0 ); |
| 4953 | TEST_ASSERT( mbedtls_ssl_conf_psk( &conf, |
| 4954 | psk1_raw, sizeof( psk1_raw ), |
| 4955 | psk1_raw_identity, sizeof( psk1_raw_identity ) ) |
| 4956 | == MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 4957 | |
| 4958 | break; |
| 4959 | |
| 4960 | case 2: |
| 4961 | |
| 4962 | TEST_ASSERT( mbedtls_ssl_conf_psk_opaque( &conf, |
| 4963 | psk0_opaque, |
| 4964 | psk0_opaque_identity, sizeof( psk0_opaque_identity ) ) |
| 4965 | == 0 ); |
| 4966 | TEST_ASSERT( mbedtls_ssl_conf_psk_opaque( &conf, |
| 4967 | psk1_opaque, |
| 4968 | psk1_opaque_identity, sizeof( psk1_opaque_identity ) ) |
| 4969 | == MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE ); |
| 4970 | |
| 4971 | break; |
| 4972 | |
| 4973 | default: |
| 4974 | TEST_ASSERT( 0 ); |
| 4975 | break; |
| 4976 | } |
| 4977 | |
| 4978 | exit: |
| 4979 | |
| 4980 | mbedtls_ssl_config_free( &conf ); |
| 4981 | USE_PSA_DONE( ); |
| 4982 | |
| 4983 | } |
| 4984 | /* END_CASE */ |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 4985 | |
| 4986 | /* 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 */ |
| 4987 | void conf_curve() |
| 4988 | { |
| 4989 | |
| 4990 | mbedtls_ecp_group_id curve_list[] = { MBEDTLS_ECP_DP_SECP192R1, |
| 4991 | MBEDTLS_ECP_DP_SECP224R1, |
| 4992 | MBEDTLS_ECP_DP_SECP256R1, |
| 4993 | MBEDTLS_ECP_DP_NONE }; |
| 4994 | mbedtls_ecp_group_id iana_tls_group_list[] = { MBEDTLS_SSL_IANA_TLS_GROUP_SECP192R1, |
| 4995 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP224R1, |
| 4996 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, |
| 4997 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE }; |
| 4998 | |
| 4999 | mbedtls_ssl_config conf; |
| 5000 | mbedtls_ssl_config_init( &conf ); |
| 5001 | |
| 5002 | mbedtls_ssl_conf_max_version( &conf, 3, 3 ); |
| 5003 | mbedtls_ssl_conf_min_version( &conf, 3, 3 ); |
| 5004 | mbedtls_ssl_conf_curves( &conf, curve_list ); |
| 5005 | |
| 5006 | mbedtls_ssl_context ssl; |
| 5007 | mbedtls_ssl_init( &ssl ); |
Paul Elliott | 46a6c20 | 2021-12-09 18:16:13 +0000 | [diff] [blame] | 5008 | TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 ); |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 5009 | |
| 5010 | TEST_ASSERT( ssl.handshake != NULL && ssl.handshake->group_list != NULL ); |
| 5011 | TEST_ASSERT( ssl.conf != NULL && ssl.conf->group_list == NULL ); |
| 5012 | |
| 5013 | TEST_EQUAL( ssl.handshake->group_list[ARRAY_LENGTH( iana_tls_group_list ) - 1], MBEDTLS_SSL_IANA_TLS_GROUP_NONE ); |
| 5014 | |
| 5015 | for( size_t i = 0; i < ARRAY_LENGTH( iana_tls_group_list ); i++ ) |
| 5016 | TEST_EQUAL( iana_tls_group_list[i], ssl.handshake->group_list[i] ); |
| 5017 | |
| 5018 | mbedtls_ssl_free( &ssl ); |
| 5019 | mbedtls_ssl_config_free( &conf ); |
| 5020 | } |
| 5021 | /* END_CASE */ |
| 5022 | |
| 5023 | /* BEGIN_CASE depends_on:MBEDTLS_DEPRECATED_REMOVED */ |
| 5024 | void conf_group() |
| 5025 | { |
| 5026 | uint16_t iana_tls_group_list[] = { MBEDTLS_SSL_IANA_TLS_GROUP_SECP192R1, |
| 5027 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP224R1, |
| 5028 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, |
| 5029 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE }; |
| 5030 | |
| 5031 | mbedtls_ssl_config conf; |
| 5032 | mbedtls_ssl_config_init( &conf ); |
| 5033 | |
| 5034 | mbedtls_ssl_conf_max_version( &conf, 3, 3 ); |
| 5035 | mbedtls_ssl_conf_min_version( &conf, 3, 3 ); |
| 5036 | |
| 5037 | mbedtls_ssl_conf_groups( &conf, iana_tls_group_list ); |
| 5038 | |
| 5039 | mbedtls_ssl_context ssl; |
| 5040 | mbedtls_ssl_init( &ssl ); |
Paul Elliott | 46a6c20 | 2021-12-09 18:16:13 +0000 | [diff] [blame] | 5041 | TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 ); |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 5042 | |
| 5043 | TEST_ASSERT( ssl.conf != NULL && ssl.conf->group_list != NULL ); |
| 5044 | |
| 5045 | TEST_EQUAL( ssl.conf->group_list[ARRAY_LENGTH( iana_tls_group_list ) - 1], MBEDTLS_SSL_IANA_TLS_GROUP_NONE ); |
| 5046 | |
| 5047 | for( size_t i = 0; i < ARRAY_LENGTH( iana_tls_group_list ); i++ ) |
| 5048 | TEST_EQUAL( iana_tls_group_list[i], ssl.conf->group_list[i] ); |
| 5049 | |
| 5050 | mbedtls_ssl_free( &ssl ); |
| 5051 | mbedtls_ssl_config_free( &conf ); |
| 5052 | } |
| 5053 | /* END_CASE */ |