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