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