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