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