Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 1 | /* BEGIN_HEADER */ |
Chris Jones | 84a773f | 2021-03-05 18:38:47 +0000 | [diff] [blame] | 2 | #include <ssl_misc.h> |
Andrzej Kurek | 941962e | 2020-02-07 09:20:32 -0500 | [diff] [blame] | 3 | #include <mbedtls/timing.h> |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 4 | #include <mbedtls/debug.h> |
Valerio Setti | 1b08d42 | 2023-02-13 11:33:26 +0100 | [diff] [blame] | 5 | #include <mbedtls/pk.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" |
Yanray Wang | 47907a4 | 2022-10-24 14:42:01 +0800 | [diff] [blame] | 9 | #include <test/ssl_helpers.h> |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 10 | |
Manuel Pégourié-Gonnard | 07018f9 | 2022-09-15 11:29:35 +0200 | [diff] [blame] | 11 | #include <mbedtls/legacy_or_psa.h> |
Andrzej Kurek | 21b6870 | 2022-08-17 16:26:12 -0400 | [diff] [blame] | 12 | #include "hash_info.h" |
Hanno Becker | 6667ffd | 2021-04-19 21:59:22 +0100 | [diff] [blame] | 13 | |
Gabor Mezei | 22c9a6f | 2021-10-20 12:09:35 +0200 | [diff] [blame] | 14 | #include <constant_time_internal.h> |
Manuel Pégourié-Gonnard | 9670a59 | 2020-07-10 10:21:46 +0200 | [diff] [blame] | 15 | #include <test/constant_flow.h> |
| 16 | |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 17 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 18 | #define PSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \ |
| 19 | psa_to_ssl_errors, \ |
| 20 | psa_generic_status_to_mbedtls) |
| 21 | #endif |
| 22 | |
Andrzej Kurek | 714ae65 | 2022-11-02 19:07:19 -0400 | [diff] [blame] | 23 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \ |
| 24 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \ |
| 25 | defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) |
| 26 | #define MBEDTLS_CAN_HANDLE_RSA_TEST_KEY |
| 27 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 28 | enum { |
Yanray Wang | d577a68 | 2022-10-27 11:47:54 +0800 | [diff] [blame^] | 29 | #define MBEDTLS_SSL_TLS1_3_LABEL(name, string) \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 30 | tls13_label_ ## name, |
| 31 | MBEDTLS_SSL_TLS1_3_LABEL_LIST |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 32 | #undef MBEDTLS_SSL_TLS1_3_LABEL |
Hanno Becker | 1413bd8 | 2020-09-09 12:46:09 +0100 | [diff] [blame] | 33 | }; |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 34 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 35 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 36 | static int rng_seed = 0xBEEF; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 37 | 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] | 38 | { |
| 39 | (void) p_rng; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 40 | for (size_t i = 0; i < output_len; i++) { |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 41 | output[i] = rand(); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 42 | } |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 43 | |
Andrzej Kurek | 635c2c2 | 2022-08-17 15:20:40 -0400 | [diff] [blame] | 44 | return 0; |
| 45 | } |
Andrzej Kurek | 32bd063 | 2022-08-20 14:10:36 -0400 | [diff] [blame] | 46 | #endif |
Andrzej Kurek | 635c2c2 | 2022-08-17 15:20:40 -0400 | [diff] [blame] | 47 | |
Piotr Nowicki | 438bf3b | 2020-03-10 12:59:10 +0100 | [diff] [blame] | 48 | /* |
| 49 | * This function can be passed to mbedtls to receive output logs from it. In |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 50 | * this case, it will count the instances of a mbedtls_test_ssl_log_pattern |
| 51 | * in the received logged messages. |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 52 | */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 53 | void mbedtls_test_ssl_log_analyzer(void *ctx, int level, |
| 54 | const char *file, int line, |
| 55 | const char *str) |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 56 | { |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 57 | mbedtls_test_ssl_log_pattern *p = (mbedtls_test_ssl_log_pattern *) ctx; |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 58 | |
| 59 | (void) level; |
| 60 | (void) line; |
| 61 | (void) file; |
| 62 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 63 | if (NULL != p && |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 64 | NULL != p->pattern && |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 65 | NULL != strstr(str, p->pattern)) { |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 66 | p->counter++; |
| 67 | } |
| 68 | } |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 69 | |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 70 | void mbedtls_test_init_handshake_options( |
| 71 | mbedtls_test_handshake_test_options *opts) |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 72 | { |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 73 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 74 | srand(rng_seed); |
Andrzej Kurek | 635c2c2 | 2022-08-17 15:20:40 -0400 | [diff] [blame] | 75 | rng_seed += 0xD0; |
Andrzej Kurek | 32bd063 | 2022-08-20 14:10:36 -0400 | [diff] [blame] | 76 | #endif |
Andrzej Kurek | 3d0d501 | 2022-07-04 05:20:55 -0400 | [diff] [blame] | 77 | opts->cipher = ""; |
| 78 | opts->client_min_version = MBEDTLS_SSL_VERSION_UNKNOWN; |
| 79 | opts->client_max_version = MBEDTLS_SSL_VERSION_UNKNOWN; |
| 80 | opts->server_min_version = MBEDTLS_SSL_VERSION_UNKNOWN; |
| 81 | opts->server_max_version = MBEDTLS_SSL_VERSION_UNKNOWN; |
| 82 | opts->expected_negotiated_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 83 | opts->expected_handshake_result = 0; |
| 84 | opts->expected_ciphersuite = 0; |
| 85 | opts->pk_alg = MBEDTLS_PK_RSA; |
| 86 | opts->opaque_alg = 0; |
| 87 | opts->opaque_alg2 = 0; |
| 88 | opts->opaque_usage = 0; |
| 89 | opts->psk_str = NULL; |
| 90 | opts->dtls = 0; |
| 91 | opts->srv_auth_mode = MBEDTLS_SSL_VERIFY_NONE; |
| 92 | opts->serialize = 0; |
| 93 | opts->mfl = MBEDTLS_SSL_MAX_FRAG_LEN_NONE; |
| 94 | opts->cli_msg_len = 100; |
| 95 | opts->srv_msg_len = 100; |
| 96 | opts->expected_cli_fragments = 1; |
| 97 | opts->expected_srv_fragments = 1; |
| 98 | opts->renegotiate = 0; |
| 99 | opts->legacy_renegotiation = MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION; |
| 100 | opts->srv_log_obj = NULL; |
| 101 | opts->srv_log_obj = NULL; |
| 102 | opts->srv_log_fun = NULL; |
| 103 | opts->cli_log_fun = NULL; |
| 104 | opts->resize_buffers = 1; |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 105 | #if defined(MBEDTLS_SSL_CACHE_C) |
Andrzej Kurek | 9dc4402 | 2022-07-04 05:46:15 -0400 | [diff] [blame] | 106 | opts->cache = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 107 | ASSERT_ALLOC(opts->cache, 1); |
| 108 | mbedtls_ssl_cache_init(opts->cache); |
Andrzej Kurek | 3d0d501 | 2022-07-04 05:20:55 -0400 | [diff] [blame] | 109 | exit: |
| 110 | return; |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 111 | #endif |
| 112 | } |
| 113 | |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 114 | void mbedtls_test_free_handshake_options( |
| 115 | mbedtls_test_handshake_test_options *opts) |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 116 | { |
| 117 | #if defined(MBEDTLS_SSL_CACHE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 118 | mbedtls_ssl_cache_free(opts->cache); |
| 119 | mbedtls_free(opts->cache); |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 120 | #else |
| 121 | (void) opts; |
| 122 | #endif |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 123 | } |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 124 | |
Ronald Cron | e7b9b6b | 2022-06-10 17:24:31 +0200 | [diff] [blame] | 125 | #if defined(MBEDTLS_TEST_HOOKS) |
| 126 | static void set_chk_buf_ptr_args( |
| 127 | mbedtls_ssl_chk_buf_ptr_args *args, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 128 | unsigned char *cur, unsigned char *end, size_t need) |
Ronald Cron | e7b9b6b | 2022-06-10 17:24:31 +0200 | [diff] [blame] | 129 | { |
| 130 | args->cur = cur; |
| 131 | args->end = end; |
| 132 | args->need = need; |
| 133 | } |
| 134 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 135 | static void reset_chk_buf_ptr_args(mbedtls_ssl_chk_buf_ptr_args *args) |
Ronald Cron | e7b9b6b | 2022-06-10 17:24:31 +0200 | [diff] [blame] | 136 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 137 | memset(args, 0, sizeof(*args)); |
Ronald Cron | e7b9b6b | 2022-06-10 17:24:31 +0200 | [diff] [blame] | 138 | } |
| 139 | #endif /* MBEDTLS_TEST_HOOKS */ |
| 140 | |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 141 | /* |
| 142 | * Buffer structure for custom I/O callbacks. |
| 143 | */ |
| 144 | |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 145 | /* |
| 146 | * Initialises \p buf. After calling this function it is safe to call |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 147 | * `mbedtls_test_ssl_buffer_free()` on \p buf. |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 148 | */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 149 | void mbedtls_test_ssl_buffer_init(mbedtls_test_ssl_buffer *buf) |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 150 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 151 | memset(buf, 0, sizeof(*buf)); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | /* |
| 155 | * Sets up \p buf. After calling this function it is safe to call |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 156 | * `mbedtls_test_ssl_buffer_put()` and `mbedtls_test_ssl_buffer_get()` |
| 157 | * on \p buf. |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 158 | */ |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 159 | int mbedtls_test_ssl_buffer_setup(mbedtls_test_ssl_buffer *buf, |
| 160 | size_t capacity) |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 161 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 162 | buf->buffer = (unsigned char *) mbedtls_calloc(capacity, |
| 163 | sizeof(unsigned char)); |
| 164 | if (NULL == buf->buffer) { |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 165 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 166 | } |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 167 | buf->capacity = capacity; |
| 168 | |
| 169 | return 0; |
| 170 | } |
| 171 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 172 | void mbedtls_test_ssl_buffer_free(mbedtls_test_ssl_buffer *buf) |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 173 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 174 | if (buf->buffer != NULL) { |
| 175 | mbedtls_free(buf->buffer); |
| 176 | } |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 177 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 178 | memset(buf, 0, sizeof(*buf)); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | /* |
| 182 | * Puts \p input_len bytes from the \p input buffer into the ring buffer \p buf. |
| 183 | * |
| 184 | * \p buf must have been initialized and set up by calling |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 185 | * `mbedtls_test_ssl_buffer_init()` and `mbedtls_test_ssl_buffer_setup()`. |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 186 | * |
| 187 | * \retval \p input_len, if the data fits. |
| 188 | * \retval 0 <= value < \p input_len, if the data does not fit. |
| 189 | * \retval -1, if \p buf is NULL, it hasn't been set up or \p input_len is not |
| 190 | * zero and \p input is NULL. |
| 191 | */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 192 | int mbedtls_test_ssl_buffer_put(mbedtls_test_ssl_buffer *buf, |
| 193 | const unsigned char *input, size_t input_len) |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 194 | { |
| 195 | size_t overflow = 0; |
| 196 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 197 | if ((buf == NULL) || (buf->buffer == NULL)) { |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 198 | return -1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 199 | } |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 200 | |
| 201 | /* Reduce input_len to a number that fits in the buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 202 | if ((buf->content_length + input_len) > buf->capacity) { |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 203 | input_len = buf->capacity - buf->content_length; |
| 204 | } |
| 205 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 206 | if (input == NULL) { |
| 207 | return (input_len == 0) ? 0 : -1; |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 208 | } |
| 209 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 210 | /* Check if the buffer has not come full circle and free space is not in |
| 211 | * the middle */ |
| 212 | if (buf->start + buf->content_length < buf->capacity) { |
Piotr Nowicki | fb437d7 | 2020-01-13 16:59:12 +0100 | [diff] [blame] | 213 | |
| 214 | /* Calculate the number of bytes that need to be placed at lower memory |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 215 | * address */ |
| 216 | if (buf->start + buf->content_length + input_len |
| 217 | > buf->capacity) { |
| 218 | overflow = (buf->start + buf->content_length + input_len) |
| 219 | % buf->capacity; |
Piotr Nowicki | fb437d7 | 2020-01-13 16:59:12 +0100 | [diff] [blame] | 220 | } |
| 221 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 222 | memcpy(buf->buffer + buf->start + buf->content_length, input, |
| 223 | input_len - overflow); |
| 224 | memcpy(buf->buffer, input + input_len - overflow, overflow); |
Piotr Nowicki | fb437d7 | 2020-01-13 16:59:12 +0100 | [diff] [blame] | 225 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 226 | } else { |
Piotr Nowicki | fb437d7 | 2020-01-13 16:59:12 +0100 | [diff] [blame] | 227 | /* The buffer has come full circle and free space is in the middle */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 228 | memcpy(buf->buffer + buf->start + buf->content_length - buf->capacity, |
| 229 | input, input_len); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 230 | } |
| 231 | |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 232 | buf->content_length += input_len; |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 233 | return input_len; |
| 234 | } |
| 235 | |
| 236 | /* |
Andrzej Kurek | f777414 | 2020-01-22 06:34:59 -0500 | [diff] [blame] | 237 | * Gets \p output_len bytes from the ring buffer \p buf into the |
| 238 | * \p output buffer. The output buffer can be NULL, in this case a part of the |
| 239 | * ring buffer will be dropped, if the requested length is available. |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 240 | * |
| 241 | * \p buf must have been initialized and set up by calling |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 242 | * `mbedtls_test_ssl_buffer_init()` and `mbedtls_test_ssl_buffer_setup()`. |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 243 | * |
| 244 | * \retval \p output_len, if the data is available. |
| 245 | * \retval 0 <= value < \p output_len, if the data is not available. |
Andrzej Kurek | f777414 | 2020-01-22 06:34:59 -0500 | [diff] [blame] | 246 | * \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] | 247 | */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 248 | int mbedtls_test_ssl_buffer_get(mbedtls_test_ssl_buffer *buf, |
| 249 | unsigned char *output, size_t output_len) |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 250 | { |
| 251 | size_t overflow = 0; |
| 252 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 253 | if ((buf == NULL) || (buf->buffer == NULL)) { |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 254 | return -1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 255 | } |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 256 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 257 | if (output == NULL && output_len == 0) { |
Andrzej Kurek | f777414 | 2020-01-22 06:34:59 -0500 | [diff] [blame] | 258 | return 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 259 | } |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 260 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 261 | if (buf->content_length < output_len) { |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 262 | output_len = buf->content_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 263 | } |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 264 | |
| 265 | /* Calculate the number of bytes that need to be drawn from lower memory |
| 266 | * address */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 267 | if (buf->start + output_len > buf->capacity) { |
| 268 | overflow = (buf->start + output_len) % buf->capacity; |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 271 | if (output != NULL) { |
| 272 | memcpy(output, buf->buffer + buf->start, output_len - overflow); |
| 273 | memcpy(output + output_len - overflow, buf->buffer, overflow); |
Andrzej Kurek | f777414 | 2020-01-22 06:34:59 -0500 | [diff] [blame] | 274 | } |
| 275 | |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 276 | buf->content_length -= output_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 277 | buf->start = (buf->start + output_len) % buf->capacity; |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 278 | |
| 279 | return output_len; |
| 280 | } |
| 281 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 282 | /* |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 283 | * Errors used in the message transport mock tests |
| 284 | */ |
| 285 | #define MBEDTLS_TEST_ERROR_ARG_NULL -11 |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 286 | #define MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED -44 |
| 287 | |
| 288 | /* |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 289 | * Setup and free functions for the message metadata queue. |
| 290 | * |
| 291 | * \p capacity describes the number of message metadata chunks that can be held |
| 292 | * within the queue. |
| 293 | * |
| 294 | * \retval 0, if a metadata queue of a given length can be allocated. |
| 295 | * \retval MBEDTLS_ERR_SSL_ALLOC_FAILED, if allocation failed. |
| 296 | */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 297 | int mbedtls_test_ssl_message_queue_setup(mbedtls_test_ssl_message_queue *queue, |
| 298 | size_t capacity) |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 299 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 300 | queue->messages = (size_t *) mbedtls_calloc(capacity, sizeof(size_t)); |
| 301 | if (NULL == queue->messages) { |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 302 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 303 | } |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 304 | |
| 305 | queue->capacity = capacity; |
| 306 | queue->pos = 0; |
| 307 | queue->num = 0; |
| 308 | |
| 309 | return 0; |
| 310 | } |
| 311 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 312 | void mbedtls_test_ssl_message_queue_free(mbedtls_test_ssl_message_queue *queue) |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 313 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 314 | if (queue == NULL) { |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 315 | return; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 316 | } |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 317 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 318 | if (queue->messages != NULL) { |
| 319 | mbedtls_free(queue->messages); |
| 320 | } |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 321 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 322 | memset(queue, 0, sizeof(*queue)); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | /* |
| 326 | * Push message length information onto the message metadata queue. |
| 327 | * This will become the last element to leave it (fifo). |
| 328 | * |
| 329 | * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null. |
Andrzej Kurek | f46b912 | 2020-02-07 08:19:00 -0500 | [diff] [blame] | 330 | * \retval MBEDTLS_ERR_SSL_WANT_WRITE, if the queue is full. |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 331 | * \retval \p len, if the push was successful. |
| 332 | */ |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 333 | int mbedtls_test_ssl_message_queue_push_info( |
| 334 | mbedtls_test_ssl_message_queue *queue, size_t len) |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 335 | { |
| 336 | int place; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 337 | if (queue == NULL) { |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 338 | return MBEDTLS_TEST_ERROR_ARG_NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 339 | } |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 340 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 341 | if (queue->num >= queue->capacity) { |
Andrzej Kurek | f46b912 | 2020-02-07 08:19:00 -0500 | [diff] [blame] | 342 | return MBEDTLS_ERR_SSL_WANT_WRITE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 343 | } |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 344 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 345 | place = (queue->pos + queue->num) % queue->capacity; |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 346 | queue->messages[place] = len; |
| 347 | queue->num++; |
| 348 | return len; |
| 349 | } |
| 350 | |
| 351 | /* |
| 352 | * Pop information about the next message length from the queue. This will be |
| 353 | * the oldest inserted message length(fifo). \p msg_len can be null, in which |
| 354 | * case the data will be popped from the queue but not copied anywhere. |
| 355 | * |
| 356 | * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null. |
Andrzej Kurek | f46b912 | 2020-02-07 08:19:00 -0500 | [diff] [blame] | 357 | * \retval MBEDTLS_ERR_SSL_WANT_READ, if the queue is empty. |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 358 | * \retval message length, if the pop was successful, up to the given |
| 359 | \p buf_len. |
| 360 | */ |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 361 | int mbedtls_test_ssl_message_queue_pop_info( |
| 362 | mbedtls_test_ssl_message_queue *queue, size_t buf_len) |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 363 | { |
| 364 | size_t message_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 365 | if (queue == NULL) { |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 366 | return MBEDTLS_TEST_ERROR_ARG_NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 367 | } |
| 368 | if (queue->num == 0) { |
Andrzej Kurek | f46b912 | 2020-02-07 08:19:00 -0500 | [diff] [blame] | 369 | return MBEDTLS_ERR_SSL_WANT_READ; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 370 | } |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 371 | |
| 372 | message_length = queue->messages[queue->pos]; |
| 373 | queue->messages[queue->pos] = 0; |
| 374 | queue->num--; |
| 375 | queue->pos++; |
| 376 | queue->pos %= queue->capacity; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 377 | if (queue->pos < 0) { |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 378 | queue->pos += queue->capacity; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 379 | } |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 380 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 381 | return (message_length > buf_len) ? buf_len : message_length; |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | /* |
| 385 | * Take a peek on the info about the next message length from the queue. |
| 386 | * This will be the oldest inserted message length(fifo). |
| 387 | * |
| 388 | * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null. |
Andrzej Kurek | f46b912 | 2020-02-07 08:19:00 -0500 | [diff] [blame] | 389 | * \retval MBEDTLS_ERR_SSL_WANT_READ, if the queue is empty. |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 390 | * \retval 0, if the peek was successful. |
| 391 | * \retval MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED, if the given buffer length is |
| 392 | * too small to fit the message. In this case the \p msg_len will be |
| 393 | * set to the full message length so that the |
| 394 | * caller knows what portion of the message can be dropped. |
| 395 | */ |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 396 | int mbedtls_test_message_queue_peek_info(mbedtls_test_ssl_message_queue *queue, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 397 | size_t buf_len, size_t *msg_len) |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 398 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 399 | if (queue == NULL || msg_len == NULL) { |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 400 | return MBEDTLS_TEST_ERROR_ARG_NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 401 | } |
| 402 | if (queue->num == 0) { |
Andrzej Kurek | f46b912 | 2020-02-07 08:19:00 -0500 | [diff] [blame] | 403 | return MBEDTLS_ERR_SSL_WANT_READ; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 404 | } |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 405 | |
| 406 | *msg_len = queue->messages[queue->pos]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 407 | return (*msg_len > buf_len) ? MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED : 0; |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 408 | } |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 409 | |
| 410 | /* |
| 411 | * Setup and teardown functions for mock sockets. |
| 412 | */ |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 413 | void mbedtls_mock_socket_init(mbedtls_test_mock_socket *socket) |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 414 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 415 | memset(socket, 0, sizeof(*socket)); |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | /* |
| 419 | * Closes the socket \p socket. |
| 420 | * |
| 421 | * \p socket must have been previously initialized by calling |
| 422 | * mbedtls_mock_socket_init(). |
| 423 | * |
| 424 | * This function frees all allocated resources and both sockets are aware of the |
| 425 | * new connection state. |
| 426 | * |
| 427 | * That is, this function does not simulate half-open TCP connections and the |
| 428 | * phenomenon that when closing a UDP connection the peer is not aware of the |
| 429 | * connection having been closed. |
| 430 | */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 431 | void mbedtls_test_mock_socket_close(mbedtls_test_mock_socket *socket) |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 432 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 433 | if (socket == NULL) { |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 434 | return; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 435 | } |
| 436 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 437 | if (socket->input != NULL) { |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 438 | mbedtls_test_ssl_buffer_free(socket->input); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 439 | mbedtls_free(socket->input); |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 440 | } |
| 441 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 442 | if (socket->output != NULL) { |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 443 | mbedtls_test_ssl_buffer_free(socket->output); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 444 | mbedtls_free(socket->output); |
| 445 | } |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 446 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 447 | if (socket->peer != NULL) { |
| 448 | memset(socket->peer, 0, sizeof(*socket->peer)); |
| 449 | } |
| 450 | |
| 451 | memset(socket, 0, sizeof(*socket)); |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | /* |
| 455 | * Establishes a connection between \p peer1 and \p peer2. |
| 456 | * |
| 457 | * \p peer1 and \p peer2 must have been previously initialized by calling |
| 458 | * mbedtls_mock_socket_init(). |
| 459 | * |
Tom Cosgrove | 1797b05 | 2022-12-04 17:19:59 +0000 | [diff] [blame] | 460 | * The capacities of the internal buffers are set to \p bufsize. Setting this to |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 461 | * the correct value allows for simulation of MTU, sanity testing the mock |
| 462 | * implementation and mocking TCP connections with lower memory cost. |
| 463 | */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 464 | int mbedtls_test_mock_socket_connect(mbedtls_test_mock_socket *peer1, |
| 465 | mbedtls_test_mock_socket *peer2, |
| 466 | size_t bufsize) |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 467 | { |
| 468 | int ret = -1; |
| 469 | |
Piotr Nowicki | d796e19 | 2020-01-28 12:09:47 +0100 | [diff] [blame] | 470 | peer1->output = |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 471 | (mbedtls_test_ssl_buffer *) mbedtls_calloc( |
| 472 | 1, sizeof(mbedtls_test_ssl_buffer)); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 473 | if (peer1->output == NULL) { |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 474 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 475 | goto exit; |
| 476 | } |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 477 | mbedtls_test_ssl_buffer_init(peer1->output); |
| 478 | if (0 != (ret = mbedtls_test_ssl_buffer_setup(peer1->output, bufsize))) { |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 479 | goto exit; |
| 480 | } |
| 481 | |
Piotr Nowicki | d796e19 | 2020-01-28 12:09:47 +0100 | [diff] [blame] | 482 | peer2->output = |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 483 | (mbedtls_test_ssl_buffer *) mbedtls_calloc( |
| 484 | 1, sizeof(mbedtls_test_ssl_buffer)); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 485 | if (peer2->output == NULL) { |
Piotr Nowicki | d796e19 | 2020-01-28 12:09:47 +0100 | [diff] [blame] | 486 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 487 | goto exit; |
| 488 | } |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 489 | mbedtls_test_ssl_buffer_init(peer2->output); |
| 490 | if (0 != (ret = mbedtls_test_ssl_buffer_setup(peer2->output, bufsize))) { |
Piotr Nowicki | d796e19 | 2020-01-28 12:09:47 +0100 | [diff] [blame] | 491 | goto exit; |
| 492 | } |
| 493 | |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 494 | peer1->peer = peer2; |
| 495 | peer2->peer = peer1; |
Piotr Nowicki | d796e19 | 2020-01-28 12:09:47 +0100 | [diff] [blame] | 496 | peer1->input = peer2->output; |
| 497 | peer2->input = peer1->output; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 498 | |
| 499 | peer1->status = peer2->status = MBEDTLS_MOCK_SOCKET_CONNECTED; |
| 500 | ret = 0; |
| 501 | |
| 502 | exit: |
| 503 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 504 | if (ret != 0) { |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 505 | mbedtls_test_mock_socket_close(peer1); |
| 506 | mbedtls_test_mock_socket_close(peer2); |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | return ret; |
| 510 | } |
| 511 | |
| 512 | /* |
| 513 | * Callbacks for simulating blocking I/O over connection-oriented transport. |
| 514 | */ |
| 515 | |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 516 | int mbedtls_test_mock_tcp_send_b(void *ctx, const unsigned char *buf, |
| 517 | size_t len) |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 518 | { |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 519 | mbedtls_test_mock_socket *socket = (mbedtls_test_mock_socket *) ctx; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 520 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 521 | if (socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED) { |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 522 | return -1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 523 | } |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 524 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 525 | return mbedtls_test_ssl_buffer_put(socket->output, buf, len); |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 526 | } |
| 527 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 528 | int mbedtls_test_mock_tcp_recv_b(void *ctx, unsigned char *buf, size_t len) |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 529 | { |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 530 | mbedtls_test_mock_socket *socket = (mbedtls_test_mock_socket *) ctx; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 531 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 532 | if (socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED) { |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 533 | return -1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 534 | } |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 535 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 536 | return mbedtls_test_ssl_buffer_get(socket->input, buf, len); |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | /* |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 540 | * Callbacks for simulating non-blocking I/O over connection-oriented transport. |
| 541 | */ |
| 542 | |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 543 | int mbedtls_test_mock_tcp_send_nb(void *ctx, const unsigned char *buf, |
| 544 | size_t len) |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 545 | { |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 546 | mbedtls_test_mock_socket *socket = (mbedtls_test_mock_socket *) ctx; |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 547 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 548 | if (socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED) { |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 549 | return -1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 550 | } |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 551 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 552 | if (socket->output->capacity == socket->output->content_length) { |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 553 | return MBEDTLS_ERR_SSL_WANT_WRITE; |
| 554 | } |
| 555 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 556 | return mbedtls_test_ssl_buffer_put(socket->output, buf, len); |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 557 | } |
| 558 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 559 | int mbedtls_test_mock_tcp_recv_nb(void *ctx, unsigned char *buf, size_t len) |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 560 | { |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 561 | mbedtls_test_mock_socket *socket = (mbedtls_test_mock_socket *) ctx; |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 562 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 563 | if (socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED) { |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 564 | return -1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 565 | } |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 566 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 567 | if (socket->input->content_length == 0) { |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 568 | return MBEDTLS_ERR_SSL_WANT_READ; |
| 569 | } |
| 570 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 571 | return mbedtls_test_ssl_buffer_get(socket->input, buf, len); |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 572 | } |
| 573 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 574 | void mbedtls_test_message_socket_init(mbedtls_test_message_socket_context *ctx) |
Andrzej Kurek | 45916ba | 2020-03-05 14:46:22 -0500 | [diff] [blame] | 575 | { |
| 576 | ctx->queue_input = NULL; |
| 577 | ctx->queue_output = NULL; |
| 578 | ctx->socket = NULL; |
| 579 | } |
| 580 | |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 581 | /* |
Tom Cosgrove | 1797b05 | 2022-12-04 17:19:59 +0000 | [diff] [blame] | 582 | * Setup a given message socket context including initialization of |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 583 | * input/output queues to a chosen capacity of messages. Also set the |
| 584 | * corresponding mock socket. |
| 585 | * |
| 586 | * \retval 0, if everything succeeds. |
| 587 | * \retval MBEDTLS_ERR_SSL_ALLOC_FAILED, if allocation of a message |
| 588 | * queue failed. |
| 589 | */ |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 590 | int mbedtls_test_message_socket_setup( |
| 591 | mbedtls_test_ssl_message_queue *queue_input, |
| 592 | mbedtls_test_ssl_message_queue *queue_output, |
| 593 | size_t queue_capacity, |
| 594 | mbedtls_test_mock_socket *socket, |
| 595 | mbedtls_test_message_socket_context *ctx) |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 596 | { |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 597 | int ret = mbedtls_test_ssl_message_queue_setup(queue_input, queue_capacity); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 598 | if (ret != 0) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 599 | return ret; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 600 | } |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 601 | ctx->queue_input = queue_input; |
| 602 | ctx->queue_output = queue_output; |
| 603 | ctx->socket = socket; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 604 | mbedtls_mock_socket_init(socket); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 605 | |
| 606 | return 0; |
| 607 | } |
| 608 | |
| 609 | /* |
| 610 | * Close a given message socket context, along with the socket itself. Free the |
| 611 | * memory allocated by the input queue. |
| 612 | */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 613 | void mbedtls_test_message_socket_close(mbedtls_test_message_socket_context *ctx) |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 614 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 615 | if (ctx == NULL) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 616 | return; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 617 | } |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 618 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 619 | mbedtls_test_ssl_message_queue_free(ctx->queue_input); |
| 620 | mbedtls_test_mock_socket_close(ctx->socket); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 621 | memset(ctx, 0, sizeof(*ctx)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | /* |
| 625 | * Send one message through a given message socket context. |
| 626 | * |
| 627 | * \retval \p len, if everything succeeds. |
| 628 | * \retval MBEDTLS_TEST_ERROR_CONTEXT_ERROR, if any of the needed context |
| 629 | * elements or the context itself is null. |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 630 | * \retval MBEDTLS_TEST_ERROR_SEND_FAILED if |
| 631 | * mbedtls_test_mock_tcp_send_b failed. |
Andrzej Kurek | f46b912 | 2020-02-07 08:19:00 -0500 | [diff] [blame] | 632 | * \retval MBEDTLS_ERR_SSL_WANT_WRITE, if the output queue is full. |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 633 | * |
| 634 | * This function will also return any error from |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 635 | * mbedtls_test_ssl_message_queue_push_info. |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 636 | */ |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 637 | int mbedtls_test_mock_tcp_send_msg(void *ctx, const unsigned char *buf, |
| 638 | size_t len) |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 639 | { |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 640 | mbedtls_test_ssl_message_queue *queue; |
| 641 | mbedtls_test_mock_socket *socket; |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 642 | mbedtls_test_message_socket_context *context = |
| 643 | (mbedtls_test_message_socket_context *) ctx; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 644 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 645 | if (context == NULL || context->socket == NULL |
| 646 | || context->queue_output == NULL) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 647 | return MBEDTLS_TEST_ERROR_CONTEXT_ERROR; |
| 648 | } |
| 649 | |
| 650 | queue = context->queue_output; |
| 651 | socket = context->socket; |
| 652 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 653 | if (queue->num >= queue->capacity) { |
Andrzej Kurek | f46b912 | 2020-02-07 08:19:00 -0500 | [diff] [blame] | 654 | return MBEDTLS_ERR_SSL_WANT_WRITE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 655 | } |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 656 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 657 | if (mbedtls_test_mock_tcp_send_b(socket, buf, len) != (int) len) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 658 | return MBEDTLS_TEST_ERROR_SEND_FAILED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 659 | } |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 660 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 661 | return mbedtls_test_ssl_message_queue_push_info(queue, len); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | /* |
| 665 | * Receive one message from a given message socket context and return message |
| 666 | * length or an error. |
| 667 | * |
| 668 | * \retval message length, if everything succeeds. |
| 669 | * \retval MBEDTLS_TEST_ERROR_CONTEXT_ERROR, if any of the needed context |
| 670 | * elements or the context itself is null. |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 671 | * \retval MBEDTLS_TEST_ERROR_RECV_FAILED if |
| 672 | * mbedtls_test_mock_tcp_recv_b failed. |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 673 | * |
| 674 | * This function will also return any error other than |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 675 | * MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED from |
| 676 | * mbedtls_test_message_queue_peek_info. |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 677 | */ |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 678 | int mbedtls_test_mock_tcp_recv_msg(void *ctx, unsigned char *buf, |
| 679 | size_t buf_len) |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 680 | { |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 681 | mbedtls_test_ssl_message_queue *queue; |
| 682 | mbedtls_test_mock_socket *socket; |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 683 | mbedtls_test_message_socket_context *context = |
| 684 | (mbedtls_test_message_socket_context *) ctx; |
Gilles Peskine | 19e841e | 2020-03-09 20:43:51 +0100 | [diff] [blame] | 685 | size_t drop_len = 0; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 686 | size_t msg_len; |
| 687 | int ret; |
| 688 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 689 | if (context == NULL || context->socket == NULL |
| 690 | || context->queue_input == NULL) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 691 | return MBEDTLS_TEST_ERROR_CONTEXT_ERROR; |
| 692 | } |
| 693 | |
| 694 | queue = context->queue_input; |
| 695 | socket = context->socket; |
| 696 | |
| 697 | /* Peek first, so that in case of a socket error the data remains in |
| 698 | * the queue. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 699 | ret = mbedtls_test_message_queue_peek_info(queue, buf_len, &msg_len); |
| 700 | if (ret == MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 701 | /* Calculate how much to drop */ |
| 702 | drop_len = msg_len - buf_len; |
| 703 | |
| 704 | /* Set the requested message len to be buffer length */ |
| 705 | msg_len = buf_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 706 | } else if (ret != 0) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 707 | return ret; |
| 708 | } |
| 709 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 710 | if (mbedtls_test_mock_tcp_recv_b(socket, buf, msg_len) != (int) msg_len) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 711 | return MBEDTLS_TEST_ERROR_RECV_FAILED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 712 | } |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 713 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 714 | if (ret == MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 715 | /* Drop the remaining part of the message */ |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 716 | if (mbedtls_test_mock_tcp_recv_b(socket, NULL, drop_len) |
| 717 | != (int) drop_len) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 718 | /* Inconsistent state - part of the message was read, |
| 719 | * and a part couldn't. Not much we can do here, but it should not |
| 720 | * happen in test environment, unless forced manually. */ |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 721 | } |
| 722 | } |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 723 | mbedtls_test_ssl_message_queue_pop_info(queue, buf_len); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 724 | |
| 725 | return msg_len; |
| 726 | } |
| 727 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 728 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 729 | |
| 730 | /* |
Andrzej Kurek | 0abebeb | 2022-09-30 12:54:41 -0400 | [diff] [blame] | 731 | * Deinitializes certificates from endpoint represented by \p ep. |
| 732 | */ |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 733 | void mbedtls_endpoint_certificate_free(mbedtls_test_ssl_endpoint *ep) |
Andrzej Kurek | 0abebeb | 2022-09-30 12:54:41 -0400 | [diff] [blame] | 734 | { |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 735 | mbedtls_test_ssl_endpoint_certificate *cert = &(ep->cert); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 736 | if (cert != NULL) { |
| 737 | if (cert->ca_cert != NULL) { |
| 738 | mbedtls_x509_crt_free(cert->ca_cert); |
| 739 | mbedtls_free(cert->ca_cert); |
Andrzej Kurek | 0abebeb | 2022-09-30 12:54:41 -0400 | [diff] [blame] | 740 | cert->ca_cert = NULL; |
| 741 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 742 | if (cert->cert != NULL) { |
| 743 | mbedtls_x509_crt_free(cert->cert); |
| 744 | mbedtls_free(cert->cert); |
Andrzej Kurek | 0abebeb | 2022-09-30 12:54:41 -0400 | [diff] [blame] | 745 | cert->cert = NULL; |
| 746 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 747 | if (cert->pkey != NULL) { |
Andrzej Kurek | 0abebeb | 2022-09-30 12:54:41 -0400 | [diff] [blame] | 748 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 749 | if (mbedtls_pk_get_type(cert->pkey) == MBEDTLS_PK_OPAQUE) { |
Andrzej Kurek | 0abebeb | 2022-09-30 12:54:41 -0400 | [diff] [blame] | 750 | mbedtls_svc_key_id_t *key_slot = cert->pkey->pk_ctx; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 751 | psa_destroy_key(*key_slot); |
Andrzej Kurek | 0abebeb | 2022-09-30 12:54:41 -0400 | [diff] [blame] | 752 | } |
| 753 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 754 | mbedtls_pk_free(cert->pkey); |
| 755 | mbedtls_free(cert->pkey); |
Andrzej Kurek | 0abebeb | 2022-09-30 12:54:41 -0400 | [diff] [blame] | 756 | cert->pkey = NULL; |
| 757 | } |
| 758 | } |
| 759 | } |
| 760 | |
| 761 | /* |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 762 | * Initializes \p ep_cert structure and assigns it to endpoint |
| 763 | * represented by \p ep. |
| 764 | * |
| 765 | * \retval 0 on success, otherwise error code. |
| 766 | */ |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 767 | int mbedtls_test_ssl_endpoint_certificate_init(mbedtls_test_ssl_endpoint *ep, |
| 768 | int pk_alg, |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 769 | int opaque_alg, int opaque_alg2, |
| 770 | int opaque_usage) |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 771 | { |
| 772 | int i = 0; |
| 773 | int ret = -1; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 774 | mbedtls_test_ssl_endpoint_certificate *cert = NULL; |
Neil Armstrong | 8c52ed8 | 2022-05-27 13:14:55 +0200 | [diff] [blame] | 775 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 776 | mbedtls_svc_key_id_t key_slot = MBEDTLS_SVC_KEY_ID_INIT; |
| 777 | #endif |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 778 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 779 | if (ep == NULL) { |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 780 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 781 | } |
| 782 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 783 | cert = &(ep->cert); |
| 784 | ASSERT_ALLOC(cert->ca_cert, 1); |
| 785 | ASSERT_ALLOC(cert->cert, 1); |
| 786 | ASSERT_ALLOC(cert->pkey, 1); |
Andrzej Kurek | 0abebeb | 2022-09-30 12:54:41 -0400 | [diff] [blame] | 787 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 788 | mbedtls_x509_crt_init(cert->ca_cert); |
| 789 | mbedtls_x509_crt_init(cert->cert); |
| 790 | mbedtls_pk_init(cert->pkey); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 791 | |
| 792 | /* Load the trusted CA */ |
| 793 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 794 | for (i = 0; mbedtls_test_cas_der[i] != NULL; i++) { |
Yanray Wang | d577a68 | 2022-10-27 11:47:54 +0800 | [diff] [blame^] | 795 | ret = mbedtls_x509_crt_parse_der( |
| 796 | cert->ca_cert, |
| 797 | (const unsigned char *) mbedtls_test_cas_der[i], |
| 798 | mbedtls_test_cas_der_len[i]); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 799 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | /* Load own certificate and private key */ |
| 803 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 804 | if (ep->conf.endpoint == MBEDTLS_SSL_IS_SERVER) { |
| 805 | if (pk_alg == MBEDTLS_PK_RSA) { |
Yanray Wang | d577a68 | 2022-10-27 11:47:54 +0800 | [diff] [blame^] | 806 | ret = mbedtls_x509_crt_parse( |
| 807 | cert->cert, |
| 808 | (const unsigned char *) mbedtls_test_srv_crt_rsa_sha256_der, |
| 809 | mbedtls_test_srv_crt_rsa_sha256_der_len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 810 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 811 | |
Yanray Wang | d577a68 | 2022-10-27 11:47:54 +0800 | [diff] [blame^] | 812 | ret = mbedtls_pk_parse_key( |
| 813 | cert->pkey, |
| 814 | (const unsigned char *) mbedtls_test_srv_key_rsa_der, |
| 815 | mbedtls_test_srv_key_rsa_der_len, NULL, 0, |
| 816 | mbedtls_test_rnd_std_rand, NULL); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 817 | TEST_ASSERT(ret == 0); |
| 818 | } else { |
Yanray Wang | d577a68 | 2022-10-27 11:47:54 +0800 | [diff] [blame^] | 819 | ret = mbedtls_x509_crt_parse( |
| 820 | cert->cert, |
| 821 | (const unsigned char *) mbedtls_test_srv_crt_ec_der, |
| 822 | mbedtls_test_srv_crt_ec_der_len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 823 | TEST_ASSERT(ret == 0); |
| 824 | |
Yanray Wang | d577a68 | 2022-10-27 11:47:54 +0800 | [diff] [blame^] | 825 | ret = mbedtls_pk_parse_key( |
| 826 | cert->pkey, |
| 827 | (const unsigned char *) mbedtls_test_srv_key_ec_der, |
| 828 | mbedtls_test_srv_key_ec_der_len, NULL, 0, |
| 829 | mbedtls_test_rnd_std_rand, NULL); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 830 | TEST_ASSERT(ret == 0); |
Andrzej Kurek | b298074 | 2020-02-02 19:25:26 -0500 | [diff] [blame] | 831 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 832 | } else { |
| 833 | if (pk_alg == MBEDTLS_PK_RSA) { |
Yanray Wang | d577a68 | 2022-10-27 11:47:54 +0800 | [diff] [blame^] | 834 | ret = mbedtls_x509_crt_parse( |
| 835 | cert->cert, |
| 836 | (const unsigned char *) mbedtls_test_cli_crt_rsa_der, |
| 837 | mbedtls_test_cli_crt_rsa_der_len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 838 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 839 | |
Yanray Wang | d577a68 | 2022-10-27 11:47:54 +0800 | [diff] [blame^] | 840 | ret = mbedtls_pk_parse_key( |
| 841 | cert->pkey, |
| 842 | (const unsigned char *) mbedtls_test_cli_key_rsa_der, |
| 843 | mbedtls_test_cli_key_rsa_der_len, NULL, 0, |
| 844 | mbedtls_test_rnd_std_rand, NULL); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 845 | TEST_ASSERT(ret == 0); |
| 846 | } else { |
Yanray Wang | d577a68 | 2022-10-27 11:47:54 +0800 | [diff] [blame^] | 847 | ret = mbedtls_x509_crt_parse( |
| 848 | cert->cert, |
| 849 | (const unsigned char *) mbedtls_test_cli_crt_ec_der, |
| 850 | mbedtls_test_cli_crt_ec_len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 851 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 852 | |
Yanray Wang | d577a68 | 2022-10-27 11:47:54 +0800 | [diff] [blame^] | 853 | ret = mbedtls_pk_parse_key( |
| 854 | cert->pkey, |
| 855 | (const unsigned char *) mbedtls_test_cli_key_ec_der, |
| 856 | mbedtls_test_cli_key_ec_der_len, NULL, 0, |
| 857 | mbedtls_test_rnd_std_rand, NULL); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 858 | TEST_ASSERT(ret == 0); |
Andrzej Kurek | b298074 | 2020-02-02 19:25:26 -0500 | [diff] [blame] | 859 | } |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 860 | } |
| 861 | |
Neil Armstrong | 8c52ed8 | 2022-05-27 13:14:55 +0200 | [diff] [blame] | 862 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 863 | if (opaque_alg != 0) { |
| 864 | TEST_EQUAL(mbedtls_pk_wrap_as_opaque(cert->pkey, &key_slot, |
| 865 | opaque_alg, opaque_usage, |
| 866 | opaque_alg2), 0); |
Neil Armstrong | 8c52ed8 | 2022-05-27 13:14:55 +0200 | [diff] [blame] | 867 | } |
| 868 | #else |
| 869 | (void) opaque_alg; |
| 870 | (void) opaque_alg2; |
| 871 | (void) opaque_usage; |
| 872 | #endif |
| 873 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 874 | mbedtls_ssl_conf_ca_chain(&(ep->conf), cert->ca_cert, NULL); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 875 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 876 | ret = mbedtls_ssl_conf_own_cert(&(ep->conf), cert->cert, |
| 877 | cert->pkey); |
| 878 | TEST_ASSERT(ret == 0); |
| 879 | TEST_ASSERT(ep->conf.key_cert != NULL); |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 880 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 881 | ret = mbedtls_ssl_conf_own_cert(&(ep->conf), NULL, NULL); |
| 882 | TEST_ASSERT(ret == 0); |
| 883 | TEST_ASSERT(ep->conf.key_cert == NULL); |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 884 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 885 | ret = mbedtls_ssl_conf_own_cert(&(ep->conf), cert->cert, |
| 886 | cert->pkey); |
| 887 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 888 | |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 889 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 890 | if (ret != 0) { |
| 891 | mbedtls_endpoint_certificate_free(ep); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 892 | } |
| 893 | |
| 894 | return ret; |
| 895 | } |
| 896 | |
| 897 | /* |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 898 | * Initializes \p ep structure. It is important to call |
| 899 | * `mbedtls_test_ssl_endpoint_free()` after calling this function |
| 900 | * even if it fails. |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 901 | * |
| 902 | * \p endpoint_type must be set as MBEDTLS_SSL_IS_SERVER or |
| 903 | * MBEDTLS_SSL_IS_CLIENT. |
Andrzej Kurek | 15daf50 | 2020-02-12 09:17:52 -0500 | [diff] [blame] | 904 | * \p pk_alg the algorithm to use, currently only MBEDTLS_PK_RSA and |
| 905 | * MBEDTLS_PK_ECDSA are supported. |
| 906 | * \p dtls_context - in case of DTLS - this is the context handling metadata. |
| 907 | * \p input_queue - used only in case of DTLS. |
| 908 | * \p output_queue - used only in case of DTLS. |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 909 | * |
| 910 | * \retval 0 on success, otherwise error code. |
| 911 | */ |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 912 | int mbedtls_test_ssl_endpoint_init( |
| 913 | mbedtls_test_ssl_endpoint *ep, int endpoint_type, |
| 914 | mbedtls_test_handshake_test_options *options, |
| 915 | mbedtls_test_message_socket_context *dtls_context, |
| 916 | mbedtls_test_ssl_message_queue *input_queue, |
| 917 | mbedtls_test_ssl_message_queue *output_queue, |
| 918 | uint16_t *group_list) |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 919 | { |
| 920 | int ret = -1; |
Gilles Peskine | 80dae04 | 2022-01-21 23:50:39 +0100 | [diff] [blame] | 921 | uintptr_t user_data_n; |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 922 | |
Yanray Wang | d577a68 | 2022-10-27 11:47:54 +0800 | [diff] [blame^] | 923 | if (dtls_context != NULL && |
| 924 | (input_queue == NULL || output_queue == NULL)) { |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 925 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Yanray Wang | d577a68 | 2022-10-27 11:47:54 +0800 | [diff] [blame^] | 926 | |
Andrzej Kurek | 15daf50 | 2020-02-12 09:17:52 -0500 | [diff] [blame] | 927 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 928 | |
| 929 | if (ep == NULL) { |
| 930 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 931 | } |
| 932 | |
| 933 | memset(ep, 0, sizeof(*ep)); |
| 934 | |
| 935 | ep->name = (endpoint_type == MBEDTLS_SSL_IS_SERVER) ? "Server" : "Client"; |
| 936 | |
| 937 | mbedtls_ssl_init(&(ep->ssl)); |
| 938 | mbedtls_ssl_config_init(&(ep->conf)); |
| 939 | mbedtls_ssl_conf_rng(&(ep->conf), rng_get, NULL); |
| 940 | |
| 941 | TEST_ASSERT(mbedtls_ssl_conf_get_user_data_p(&ep->conf) == NULL); |
| 942 | TEST_EQUAL(mbedtls_ssl_conf_get_user_data_n(&ep->conf), 0); |
| 943 | TEST_ASSERT(mbedtls_ssl_get_user_data_p(&ep->ssl) == NULL); |
| 944 | TEST_EQUAL(mbedtls_ssl_get_user_data_n(&ep->ssl), 0); |
| 945 | |
| 946 | (void) mbedtls_test_rnd_std_rand(NULL, |
| 947 | (void *) &user_data_n, |
| 948 | sizeof(user_data_n)); |
| 949 | mbedtls_ssl_conf_set_user_data_n(&ep->conf, user_data_n); |
| 950 | mbedtls_ssl_set_user_data_n(&ep->ssl, user_data_n); |
| 951 | |
| 952 | if (dtls_context != NULL) { |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 953 | TEST_ASSERT(mbedtls_test_message_socket_setup(input_queue, output_queue, |
| 954 | 100, &(ep->socket), |
| 955 | dtls_context) == 0); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 956 | } else { |
| 957 | mbedtls_mock_socket_init(&(ep->socket)); |
Andrzej Kurek | 15daf50 | 2020-02-12 09:17:52 -0500 | [diff] [blame] | 958 | } |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 959 | |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 960 | /* Non-blocking callbacks without timeout */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 961 | if (dtls_context != NULL) { |
| 962 | mbedtls_ssl_set_bio(&(ep->ssl), dtls_context, |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 963 | mbedtls_test_mock_tcp_send_msg, |
| 964 | mbedtls_test_mock_tcp_recv_msg, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 965 | NULL); |
| 966 | } else { |
| 967 | mbedtls_ssl_set_bio(&(ep->ssl), &(ep->socket), |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 968 | mbedtls_test_mock_tcp_send_nb, |
| 969 | mbedtls_test_mock_tcp_recv_nb, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 970 | NULL); |
Andrzej Kurek | 15daf50 | 2020-02-12 09:17:52 -0500 | [diff] [blame] | 971 | } |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 972 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 973 | ret = mbedtls_ssl_config_defaults(&(ep->conf), endpoint_type, |
| 974 | (dtls_context != NULL) ? |
| 975 | MBEDTLS_SSL_TRANSPORT_DATAGRAM : |
| 976 | MBEDTLS_SSL_TRANSPORT_STREAM, |
| 977 | MBEDTLS_SSL_PRESET_DEFAULT); |
| 978 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 979 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 980 | if (group_list != NULL) { |
| 981 | mbedtls_ssl_conf_groups(&(ep->conf), group_list); |
| 982 | } |
Andrzej Kurek | 74394a5 | 2022-03-08 06:50:12 -0500 | [diff] [blame] | 983 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 984 | mbedtls_ssl_conf_authmode(&(ep->conf), MBEDTLS_SSL_VERIFY_REQUIRED); |
Ronald Cron | bdddaef | 2022-06-07 10:34:59 +0200 | [diff] [blame] | 985 | |
Andrzej Kurek | ed58b50 | 2022-06-10 19:24:05 -0400 | [diff] [blame] | 986 | #if defined(MBEDTLS_SSL_CACHE_C) && defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 987 | if (endpoint_type == MBEDTLS_SSL_IS_SERVER && options->cache != NULL) { |
| 988 | mbedtls_ssl_conf_session_cache(&(ep->conf), options->cache, |
| 989 | mbedtls_ssl_cache_get, |
| 990 | mbedtls_ssl_cache_set); |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 991 | } |
| 992 | #endif |
| 993 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 994 | ret = mbedtls_ssl_setup(&(ep->ssl), &(ep->conf)); |
| 995 | TEST_ASSERT(ret == 0); |
Andrzej Kurek | 15daf50 | 2020-02-12 09:17:52 -0500 | [diff] [blame] | 996 | |
| 997 | #if defined(MBEDTLS_SSL_PROTO_DTLS) && defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 998 | if (endpoint_type == MBEDTLS_SSL_IS_SERVER && dtls_context != NULL) { |
| 999 | mbedtls_ssl_conf_dtls_cookies(&(ep->conf), NULL, NULL, NULL); |
| 1000 | } |
Andrzej Kurek | 15daf50 | 2020-02-12 09:17:52 -0500 | [diff] [blame] | 1001 | #endif |
| 1002 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 1003 | ret = mbedtls_test_ssl_endpoint_certificate_init(ep, options->pk_alg, |
| 1004 | options->opaque_alg, |
| 1005 | options->opaque_alg2, |
| 1006 | options->opaque_usage); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1007 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 1008 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1009 | TEST_EQUAL(mbedtls_ssl_conf_get_user_data_n(&ep->conf), user_data_n); |
| 1010 | mbedtls_ssl_conf_set_user_data_p(&ep->conf, ep); |
| 1011 | TEST_EQUAL(mbedtls_ssl_get_user_data_n(&ep->ssl), user_data_n); |
| 1012 | mbedtls_ssl_set_user_data_p(&ep->ssl, ep); |
Gilles Peskine | 80dae04 | 2022-01-21 23:50:39 +0100 | [diff] [blame] | 1013 | |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 1014 | exit: |
| 1015 | return ret; |
| 1016 | } |
| 1017 | |
| 1018 | /* |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 1019 | * Deinitializes endpoint represented by \p ep. |
| 1020 | */ |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 1021 | void mbedtls_test_ssl_endpoint_free( |
| 1022 | mbedtls_test_ssl_endpoint *ep, |
| 1023 | mbedtls_test_message_socket_context *context) |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 1024 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1025 | mbedtls_endpoint_certificate_free(ep); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 1026 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1027 | mbedtls_ssl_free(&(ep->ssl)); |
| 1028 | mbedtls_ssl_config_free(&(ep->conf)); |
Andrzej Kurek | 15daf50 | 2020-02-12 09:17:52 -0500 | [diff] [blame] | 1029 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1030 | if (context != NULL) { |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 1031 | mbedtls_test_message_socket_close(context); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1032 | } else { |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 1033 | mbedtls_test_mock_socket_close(&(ep->socket)); |
Andrzej Kurek | 15daf50 | 2020-02-12 09:17:52 -0500 | [diff] [blame] | 1034 | } |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 1035 | } |
| 1036 | |
| 1037 | /* |
| 1038 | * This function moves ssl handshake from \p ssl to prescribed \p state. |
| 1039 | * /p second_ssl is used as second endpoint and their sockets have to be |
| 1040 | * connected before calling this function. |
| 1041 | * |
| 1042 | * \retval 0 on success, otherwise error code. |
| 1043 | */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 1044 | int mbedtls_test_move_handshake_to_state(mbedtls_ssl_context *ssl, |
| 1045 | mbedtls_ssl_context *second_ssl, |
| 1046 | int state) |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 1047 | { |
| 1048 | enum { BUFFSIZE = 1024 }; |
| 1049 | int max_steps = 1000; |
| 1050 | int ret = 0; |
| 1051 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1052 | if (ssl == NULL || second_ssl == NULL) { |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 1053 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 1054 | } |
| 1055 | |
| 1056 | /* Perform communication via connected sockets */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1057 | while ((ssl->state != state) && (--max_steps >= 0)) { |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 1058 | /* If /p second_ssl ends the handshake procedure before /p ssl then |
| 1059 | * there is no need to call the next step */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1060 | if (!mbedtls_ssl_is_handshake_over(second_ssl)) { |
| 1061 | ret = mbedtls_ssl_handshake_step(second_ssl); |
| 1062 | if (ret != 0 && ret != MBEDTLS_ERR_SSL_WANT_READ && |
| 1063 | ret != MBEDTLS_ERR_SSL_WANT_WRITE) { |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 1064 | return ret; |
| 1065 | } |
| 1066 | } |
| 1067 | |
| 1068 | /* We only care about the \p ssl state and returns, so we call it last, |
| 1069 | * to leave the iteration as soon as the state is as expected. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1070 | ret = mbedtls_ssl_handshake_step(ssl); |
| 1071 | if (ret != 0 && ret != MBEDTLS_ERR_SSL_WANT_READ && |
| 1072 | ret != MBEDTLS_ERR_SSL_WANT_WRITE) { |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 1073 | return ret; |
| 1074 | } |
| 1075 | } |
| 1076 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1077 | return (max_steps >= 0) ? ret : -1; |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 1078 | } |
| 1079 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 1080 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 1081 | |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 1082 | /* |
Piotr Nowicki | 438bf3b | 2020-03-10 12:59:10 +0100 | [diff] [blame] | 1083 | * Write application data. Increase write counter if necessary. |
Piotr Nowicki | c3fca5e | 2020-01-30 15:33:42 +0100 | [diff] [blame] | 1084 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1085 | int mbedtls_ssl_write_fragment(mbedtls_ssl_context *ssl, unsigned char *buf, |
| 1086 | int buf_len, int *written, |
| 1087 | const int expected_fragments) |
Piotr Nowicki | c3fca5e | 2020-01-30 15:33:42 +0100 | [diff] [blame] | 1088 | { |
Dave Rodgman | a4e8fb0 | 2023-02-24 15:41:55 +0000 | [diff] [blame] | 1089 | /* Verify that calling mbedtls_ssl_write with a NULL buffer and zero length is |
| 1090 | * a valid no-op for TLS connections. */ |
| 1091 | if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 1092 | TEST_ASSERT(mbedtls_ssl_write(ssl, NULL, 0) == 0); |
| 1093 | } |
| 1094 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1095 | int ret = mbedtls_ssl_write(ssl, buf + *written, buf_len - *written); |
| 1096 | if (ret > 0) { |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1097 | *written += ret; |
Piotr Nowicki | c3fca5e | 2020-01-30 15:33:42 +0100 | [diff] [blame] | 1098 | } |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1099 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1100 | if (expected_fragments == 0) { |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1101 | /* Used for DTLS and the message size larger than MFL. In that case |
| 1102 | * the message can not be fragmented and the library should return |
| 1103 | * MBEDTLS_ERR_SSL_BAD_INPUT_DATA error. This error must be returned |
| 1104 | * to prevent a dead loop inside mbedtls_exchange_data(). */ |
| 1105 | return ret; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1106 | } else if (expected_fragments == 1) { |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1107 | /* Used for TLS/DTLS and the message size lower than MFL */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1108 | TEST_ASSERT(ret == buf_len || |
| 1109 | ret == MBEDTLS_ERR_SSL_WANT_READ || |
| 1110 | ret == MBEDTLS_ERR_SSL_WANT_WRITE); |
| 1111 | } else { |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1112 | /* Used for TLS and the message size larger than MFL */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1113 | TEST_ASSERT(expected_fragments > 1); |
| 1114 | TEST_ASSERT((ret >= 0 && ret <= buf_len) || |
| 1115 | ret == MBEDTLS_ERR_SSL_WANT_READ || |
| 1116 | ret == MBEDTLS_ERR_SSL_WANT_WRITE); |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1117 | } |
| 1118 | |
| 1119 | return 0; |
| 1120 | |
| 1121 | exit: |
| 1122 | /* Some of the tests failed */ |
| 1123 | return -1; |
Piotr Nowicki | c3fca5e | 2020-01-30 15:33:42 +0100 | [diff] [blame] | 1124 | } |
| 1125 | |
| 1126 | /* |
Yanray Wang | d577a68 | 2022-10-27 11:47:54 +0800 | [diff] [blame^] | 1127 | * Read application data and increase read counter and fragments counter |
| 1128 | * if necessary. |
Piotr Nowicki | c3fca5e | 2020-01-30 15:33:42 +0100 | [diff] [blame] | 1129 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1130 | int mbedtls_ssl_read_fragment(mbedtls_ssl_context *ssl, unsigned char *buf, |
| 1131 | int buf_len, int *read, |
| 1132 | int *fragments, const int expected_fragments) |
Piotr Nowicki | c3fca5e | 2020-01-30 15:33:42 +0100 | [diff] [blame] | 1133 | { |
Dave Rodgman | a4e8fb0 | 2023-02-24 15:41:55 +0000 | [diff] [blame] | 1134 | /* Verify that calling mbedtls_ssl_write with a NULL buffer and zero length is |
| 1135 | * a valid no-op for TLS connections. */ |
| 1136 | if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 1137 | TEST_ASSERT(mbedtls_ssl_read(ssl, NULL, 0) == 0); |
| 1138 | } |
| 1139 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1140 | int ret = mbedtls_ssl_read(ssl, buf + *read, buf_len - *read); |
| 1141 | if (ret > 0) { |
| 1142 | (*fragments)++; |
Piotr Nowicki | c3fca5e | 2020-01-30 15:33:42 +0100 | [diff] [blame] | 1143 | *read += ret; |
| 1144 | } |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1145 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1146 | if (expected_fragments == 0) { |
| 1147 | TEST_ASSERT(ret == 0); |
| 1148 | } else if (expected_fragments == 1) { |
| 1149 | TEST_ASSERT(ret == buf_len || |
| 1150 | ret == MBEDTLS_ERR_SSL_WANT_READ || |
| 1151 | ret == MBEDTLS_ERR_SSL_WANT_WRITE); |
| 1152 | } else { |
| 1153 | TEST_ASSERT(expected_fragments > 1); |
| 1154 | TEST_ASSERT((ret >= 0 && ret <= buf_len) || |
| 1155 | ret == MBEDTLS_ERR_SSL_WANT_READ || |
| 1156 | ret == MBEDTLS_ERR_SSL_WANT_WRITE); |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1157 | } |
| 1158 | |
| 1159 | return 0; |
| 1160 | |
| 1161 | exit: |
| 1162 | /* Some of the tests failed */ |
| 1163 | return -1; |
Piotr Nowicki | c3fca5e | 2020-01-30 15:33:42 +0100 | [diff] [blame] | 1164 | } |
| 1165 | |
| 1166 | /* |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1167 | * Helper function setting up inverse record transformations |
| 1168 | * using given cipher, hash, EtM mode, authentication tag length, |
| 1169 | * and version. |
| 1170 | */ |
| 1171 | |
Yanray Wang | d577a68 | 2022-10-27 11:47:54 +0800 | [diff] [blame^] | 1172 | #define CHK(x) \ |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1173 | do \ |
| 1174 | { \ |
Yanray Wang | d577a68 | 2022-10-27 11:47:54 +0800 | [diff] [blame^] | 1175 | if (!(x)) \ |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 1176 | { \ |
Hanno Becker | a5780f1 | 2019-04-05 09:55:37 +0100 | [diff] [blame] | 1177 | ret = -1; \ |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 1178 | goto cleanup; \ |
| 1179 | } \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1180 | } while (0) |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1181 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1182 | void set_ciphersuite(mbedtls_ssl_config *conf, const char *cipher, |
| 1183 | int *forced_ciphersuite) |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 1184 | { |
| 1185 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1186 | forced_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id(cipher); |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 1187 | forced_ciphersuite[1] = 0; |
| 1188 | |
| 1189 | ciphersuite_info = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1190 | mbedtls_ssl_ciphersuite_from_id(forced_ciphersuite[0]); |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 1191 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1192 | TEST_ASSERT(ciphersuite_info != NULL); |
| 1193 | TEST_ASSERT(ciphersuite_info->min_tls_version <= conf->max_tls_version); |
| 1194 | TEST_ASSERT(ciphersuite_info->max_tls_version >= conf->min_tls_version); |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 1195 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1196 | if (conf->max_tls_version > ciphersuite_info->max_tls_version) { |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 1197 | conf->max_tls_version = ciphersuite_info->max_tls_version; |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 1198 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1199 | if (conf->min_tls_version < ciphersuite_info->min_tls_version) { |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 1200 | conf->min_tls_version = ciphersuite_info->min_tls_version; |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 1201 | } |
| 1202 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1203 | mbedtls_ssl_conf_ciphersuites(conf, forced_ciphersuite); |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 1204 | |
| 1205 | exit: |
| 1206 | return; |
| 1207 | } |
| 1208 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1209 | int psk_dummy_callback(void *p_info, mbedtls_ssl_context *ssl, |
| 1210 | const unsigned char *name, size_t name_len) |
Andrzej Kurek | cc5169c | 2020-02-04 09:04:56 -0500 | [diff] [blame] | 1211 | { |
| 1212 | (void) p_info; |
| 1213 | (void) ssl; |
| 1214 | (void) name; |
| 1215 | (void) name_len; |
| 1216 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1217 | return 0; |
Andrzej Kurek | cc5169c | 2020-02-04 09:04:56 -0500 | [diff] [blame] | 1218 | } |
| 1219 | |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 1220 | #if MBEDTLS_SSL_CID_OUT_LEN_MAX > MBEDTLS_SSL_CID_IN_LEN_MAX |
| 1221 | #define SSL_CID_LEN_MIN MBEDTLS_SSL_CID_IN_LEN_MAX |
| 1222 | #else |
| 1223 | #define SSL_CID_LEN_MIN MBEDTLS_SSL_CID_OUT_LEN_MAX |
| 1224 | #endif |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1225 | |
Gilles Peskine | 9c656ec | 2022-02-26 19:55:58 +0100 | [diff] [blame] | 1226 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
| 1227 | defined(MBEDTLS_CIPHER_MODE_CBC) && defined(MBEDTLS_AES_C) |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 1228 | int mbedtls_test_psa_cipher_encrypt_helper(mbedtls_ssl_transform *transform, |
| 1229 | const unsigned char *iv, |
| 1230 | size_t iv_len, |
| 1231 | const unsigned char *input, |
| 1232 | size_t ilen, |
| 1233 | unsigned char *output, |
| 1234 | size_t *olen) |
Przemyslaw Stekiel | f4facef | 2022-02-02 21:31:04 +0100 | [diff] [blame] | 1235 | { |
Przemyslaw Stekiel | 5648d57 | 2022-02-03 14:09:02 +0100 | [diff] [blame] | 1236 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Przemyslaw Stekiel | d66387f | 2022-02-03 08:55:33 +0100 | [diff] [blame] | 1237 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Przemyslaw Stekiel | f4facef | 2022-02-02 21:31:04 +0100 | [diff] [blame] | 1238 | psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT; |
| 1239 | size_t part_len; |
| 1240 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1241 | status = psa_cipher_encrypt_setup(&cipher_op, |
Yanray Wang | d577a68 | 2022-10-27 11:47:54 +0800 | [diff] [blame^] | 1242 | transform->psa_key_enc, |
| 1243 | transform->psa_alg); |
Przemyslaw Stekiel | f4facef | 2022-02-02 21:31:04 +0100 | [diff] [blame] | 1244 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1245 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 1246 | return PSA_TO_MBEDTLS_ERR(status); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1247 | } |
Przemyslaw Stekiel | f4facef | 2022-02-02 21:31:04 +0100 | [diff] [blame] | 1248 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1249 | status = psa_cipher_set_iv(&cipher_op, iv, iv_len); |
Przemyslaw Stekiel | f4facef | 2022-02-02 21:31:04 +0100 | [diff] [blame] | 1250 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1251 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 1252 | return PSA_TO_MBEDTLS_ERR(status); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1253 | } |
Przemyslaw Stekiel | f4facef | 2022-02-02 21:31:04 +0100 | [diff] [blame] | 1254 | |
Yanray Wang | d577a68 | 2022-10-27 11:47:54 +0800 | [diff] [blame^] | 1255 | status = psa_cipher_update(&cipher_op, input, ilen, output, ilen, olen); |
Przemyslaw Stekiel | f4facef | 2022-02-02 21:31:04 +0100 | [diff] [blame] | 1256 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1257 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 1258 | return PSA_TO_MBEDTLS_ERR(status); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1259 | } |
Przemyslaw Stekiel | f4facef | 2022-02-02 21:31:04 +0100 | [diff] [blame] | 1260 | |
Yanray Wang | d577a68 | 2022-10-27 11:47:54 +0800 | [diff] [blame^] | 1261 | status = psa_cipher_finish(&cipher_op, output + *olen, ilen - *olen, |
| 1262 | &part_len); |
Przemyslaw Stekiel | f4facef | 2022-02-02 21:31:04 +0100 | [diff] [blame] | 1263 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1264 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 1265 | return PSA_TO_MBEDTLS_ERR(status); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1266 | } |
Przemyslaw Stekiel | f4facef | 2022-02-02 21:31:04 +0100 | [diff] [blame] | 1267 | |
| 1268 | *olen += part_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1269 | return 0; |
Przemyslaw Stekiel | 5648d57 | 2022-02-03 14:09:02 +0100 | [diff] [blame] | 1270 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1271 | return mbedtls_cipher_crypt(&transform->cipher_ctx_enc, |
| 1272 | iv, iv_len, input, ilen, output, olen); |
Przemyslaw Stekiel | f4facef | 2022-02-02 21:31:04 +0100 | [diff] [blame] | 1273 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Przemyslaw Stekiel | 5648d57 | 2022-02-03 14:09:02 +0100 | [diff] [blame] | 1274 | } |
Yanray Wang | d577a68 | 2022-10-27 11:47:54 +0800 | [diff] [blame^] | 1275 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_CIPHER_MODE_CBC && |
| 1276 | MBEDTLS_AES_C */ |
Przemyslaw Stekiel | f4facef | 2022-02-02 21:31:04 +0100 | [diff] [blame] | 1277 | |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 1278 | int mbedtls_test_ssl_build_transforms(mbedtls_ssl_transform *t_in, |
| 1279 | mbedtls_ssl_transform *t_out, |
| 1280 | int cipher_type, int hash_id, |
| 1281 | int etm, int tag_mode, |
| 1282 | mbedtls_ssl_protocol_version tls_version, |
| 1283 | size_t cid0_len, |
| 1284 | size_t cid1_len) |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1285 | { |
| 1286 | mbedtls_cipher_info_t const *cipher_info; |
Hanno Becker | a5780f1 | 2019-04-05 09:55:37 +0100 | [diff] [blame] | 1287 | int ret = 0; |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1288 | |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1289 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1290 | psa_key_type_t key_type; |
| 1291 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1292 | psa_algorithm_t alg; |
| 1293 | size_t key_bits; |
Przemyslaw Stekiel | d66387f | 2022-02-03 08:55:33 +0100 | [diff] [blame] | 1294 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1295 | #endif |
| 1296 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1297 | size_t keylen, maclen, ivlen; |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 1298 | unsigned char *key0 = NULL, *key1 = NULL; |
Paul Elliott | 6f1eda7 | 2020-06-11 20:22:00 +0100 | [diff] [blame] | 1299 | unsigned char *md0 = NULL, *md1 = NULL; |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1300 | unsigned char iv_enc[16], iv_dec[16]; |
| 1301 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1302 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1303 | unsigned char cid0[SSL_CID_LEN_MIN]; |
| 1304 | unsigned char cid1[SSL_CID_LEN_MIN]; |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 1305 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1306 | mbedtls_test_rnd_std_rand(NULL, cid0, sizeof(cid0)); |
| 1307 | mbedtls_test_rnd_std_rand(NULL, cid1, sizeof(cid1)); |
Hanno Becker | 43c24b8 | 2019-05-01 09:45:57 +0100 | [diff] [blame] | 1308 | #else |
| 1309 | ((void) cid0_len); |
| 1310 | ((void) cid1_len); |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1311 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 1312 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1313 | maclen = 0; |
| 1314 | |
| 1315 | /* Pick cipher */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1316 | cipher_info = mbedtls_cipher_info_from_type(cipher_type); |
| 1317 | CHK(cipher_info != NULL); |
| 1318 | CHK(cipher_info->iv_size <= 16); |
| 1319 | CHK(cipher_info->key_bitlen % 8 == 0); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1320 | |
| 1321 | /* Pick keys */ |
| 1322 | keylen = cipher_info->key_bitlen / 8; |
Hanno Becker | 78d1f70 | 2019-04-05 09:56:10 +0100 | [diff] [blame] | 1323 | /* Allocate `keylen + 1` bytes to ensure that we get |
| 1324 | * a non-NULL pointers from `mbedtls_calloc` even if |
| 1325 | * `keylen == 0` in the case of the NULL cipher. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1326 | CHK((key0 = mbedtls_calloc(1, keylen + 1)) != NULL); |
| 1327 | CHK((key1 = mbedtls_calloc(1, keylen + 1)) != NULL); |
| 1328 | memset(key0, 0x1, keylen); |
| 1329 | memset(key1, 0x2, keylen); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1330 | |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1331 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1332 | /* Setup cipher contexts */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1333 | CHK(mbedtls_cipher_setup(&t_in->cipher_ctx_enc, cipher_info) == 0); |
| 1334 | CHK(mbedtls_cipher_setup(&t_in->cipher_ctx_dec, cipher_info) == 0); |
| 1335 | CHK(mbedtls_cipher_setup(&t_out->cipher_ctx_enc, cipher_info) == 0); |
| 1336 | CHK(mbedtls_cipher_setup(&t_out->cipher_ctx_dec, cipher_info) == 0); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1337 | |
| 1338 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1339 | if (cipher_info->mode == MBEDTLS_MODE_CBC) { |
| 1340 | CHK(mbedtls_cipher_set_padding_mode(&t_in->cipher_ctx_enc, |
| 1341 | MBEDTLS_PADDING_NONE) == 0); |
| 1342 | CHK(mbedtls_cipher_set_padding_mode(&t_in->cipher_ctx_dec, |
| 1343 | MBEDTLS_PADDING_NONE) == 0); |
| 1344 | CHK(mbedtls_cipher_set_padding_mode(&t_out->cipher_ctx_enc, |
| 1345 | MBEDTLS_PADDING_NONE) == 0); |
| 1346 | CHK(mbedtls_cipher_set_padding_mode(&t_out->cipher_ctx_dec, |
| 1347 | MBEDTLS_PADDING_NONE) == 0); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1348 | } |
| 1349 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
| 1350 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1351 | CHK(mbedtls_cipher_setkey(&t_in->cipher_ctx_enc, key0, |
| 1352 | keylen << 3, MBEDTLS_ENCRYPT) == 0); |
| 1353 | CHK(mbedtls_cipher_setkey(&t_in->cipher_ctx_dec, key1, |
| 1354 | keylen << 3, MBEDTLS_DECRYPT) == 0); |
| 1355 | CHK(mbedtls_cipher_setkey(&t_out->cipher_ctx_enc, key1, |
| 1356 | keylen << 3, MBEDTLS_ENCRYPT) == 0); |
| 1357 | CHK(mbedtls_cipher_setkey(&t_out->cipher_ctx_dec, key0, |
| 1358 | keylen << 3, MBEDTLS_DECRYPT) == 0); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1359 | #endif |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1360 | |
| 1361 | /* Setup MAC contexts */ |
Hanno Becker | fd86ca8 | 2020-11-30 08:54:23 +0000 | [diff] [blame] | 1362 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1363 | if (cipher_info->mode == MBEDTLS_MODE_CBC || |
| 1364 | cipher_info->mode == MBEDTLS_MODE_STREAM) { |
Andrzej Kurek | 21b6870 | 2022-08-17 16:26:12 -0400 | [diff] [blame] | 1365 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1366 | mbedtls_md_info_t const *md_info = mbedtls_md_info_from_type(hash_id); |
| 1367 | CHK(md_info != NULL); |
Andrzej Kurek | 21b6870 | 2022-08-17 16:26:12 -0400 | [diff] [blame] | 1368 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1369 | maclen = mbedtls_hash_info_get_size(hash_id); |
| 1370 | CHK(maclen != 0); |
Przemek Stekiel | cc59c99 | 2022-08-01 11:51:22 +0200 | [diff] [blame] | 1371 | /* Pick hash keys */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1372 | CHK((md0 = mbedtls_calloc(1, maclen)) != NULL); |
| 1373 | CHK((md1 = mbedtls_calloc(1, maclen)) != NULL); |
| 1374 | memset(md0, 0x5, maclen); |
| 1375 | memset(md1, 0x6, maclen); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1376 | |
Neil Armstrong | f4cc062 | 2022-02-23 16:21:01 +0100 | [diff] [blame] | 1377 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1378 | alg = mbedtls_hash_info_psa_from_md(hash_id); |
Neil Armstrong | f4cc062 | 2022-02-23 16:21:01 +0100 | [diff] [blame] | 1379 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1380 | CHK(alg != 0); |
Neil Armstrong | f4cc062 | 2022-02-23 16:21:01 +0100 | [diff] [blame] | 1381 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1382 | t_out->psa_mac_alg = PSA_ALG_HMAC(alg); |
| 1383 | t_in->psa_mac_alg = PSA_ALG_HMAC(alg); |
Neil Armstrong | f4cc062 | 2022-02-23 16:21:01 +0100 | [diff] [blame] | 1384 | t_in->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT; |
| 1385 | t_out->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT; |
| 1386 | t_in->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT; |
| 1387 | t_out->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT; |
| 1388 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1389 | psa_reset_key_attributes(&attributes); |
| 1390 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE); |
| 1391 | psa_set_key_algorithm(&attributes, PSA_ALG_HMAC(alg)); |
| 1392 | psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC); |
Neil Armstrong | f4cc062 | 2022-02-23 16:21:01 +0100 | [diff] [blame] | 1393 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1394 | CHK(psa_import_key(&attributes, |
| 1395 | md0, maclen, |
| 1396 | &t_in->psa_mac_enc) == PSA_SUCCESS); |
Neil Armstrong | f4cc062 | 2022-02-23 16:21:01 +0100 | [diff] [blame] | 1397 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1398 | CHK(psa_import_key(&attributes, |
| 1399 | md1, maclen, |
| 1400 | &t_out->psa_mac_enc) == PSA_SUCCESS); |
Neil Armstrong | f4cc062 | 2022-02-23 16:21:01 +0100 | [diff] [blame] | 1401 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1402 | if (cipher_info->mode == MBEDTLS_MODE_STREAM || |
| 1403 | etm == MBEDTLS_SSL_ETM_DISABLED) { |
Neil Armstrong | 8f92bf3 | 2022-03-18 09:56:57 +0100 | [diff] [blame] | 1404 | /* mbedtls_ct_hmac() requires the key to be exportable */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1405 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT | |
| 1406 | PSA_KEY_USAGE_VERIFY_HASH); |
| 1407 | } else { |
| 1408 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 1409 | } |
Neil Armstrong | f4cc062 | 2022-02-23 16:21:01 +0100 | [diff] [blame] | 1410 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1411 | CHK(psa_import_key(&attributes, |
| 1412 | md1, maclen, |
| 1413 | &t_in->psa_mac_dec) == PSA_SUCCESS); |
Neil Armstrong | f4cc062 | 2022-02-23 16:21:01 +0100 | [diff] [blame] | 1414 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1415 | CHK(psa_import_key(&attributes, |
| 1416 | md0, maclen, |
| 1417 | &t_out->psa_mac_dec) == PSA_SUCCESS); |
Neil Armstrong | cf8841a | 2022-02-24 11:17:45 +0100 | [diff] [blame] | 1418 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1419 | CHK(mbedtls_md_setup(&t_out->md_ctx_enc, md_info, 1) == 0); |
| 1420 | CHK(mbedtls_md_setup(&t_out->md_ctx_dec, md_info, 1) == 0); |
| 1421 | CHK(mbedtls_md_setup(&t_in->md_ctx_enc, md_info, 1) == 0); |
| 1422 | CHK(mbedtls_md_setup(&t_in->md_ctx_dec, md_info, 1) == 0); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1423 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1424 | CHK(mbedtls_md_hmac_starts(&t_in->md_ctx_enc, |
| 1425 | md0, maclen) == 0); |
| 1426 | CHK(mbedtls_md_hmac_starts(&t_in->md_ctx_dec, |
| 1427 | md1, maclen) == 0); |
| 1428 | CHK(mbedtls_md_hmac_starts(&t_out->md_ctx_enc, |
| 1429 | md1, maclen) == 0); |
| 1430 | CHK(mbedtls_md_hmac_starts(&t_out->md_ctx_dec, |
| 1431 | md0, maclen) == 0); |
Neil Armstrong | cf8841a | 2022-02-24 11:17:45 +0100 | [diff] [blame] | 1432 | #endif |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1433 | } |
| 1434 | #else |
| 1435 | ((void) hash_id); |
Hanno Becker | fd86ca8 | 2020-11-30 08:54:23 +0000 | [diff] [blame] | 1436 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1437 | |
| 1438 | |
| 1439 | /* Pick IV's (regardless of whether they |
| 1440 | * are being used by the transform). */ |
| 1441 | ivlen = cipher_info->iv_size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1442 | memset(iv_enc, 0x3, sizeof(iv_enc)); |
| 1443 | memset(iv_dec, 0x4, sizeof(iv_dec)); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1444 | |
| 1445 | /* |
| 1446 | * Setup transforms |
| 1447 | */ |
| 1448 | |
Jaeden Amero | 2de07f1 | 2019-06-05 13:32:08 +0100 | [diff] [blame] | 1449 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \ |
Hanno Becker | fd86ca8 | 2020-11-30 08:54:23 +0000 | [diff] [blame] | 1450 | defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1451 | t_out->encrypt_then_mac = etm; |
| 1452 | t_in->encrypt_then_mac = etm; |
| 1453 | #else |
| 1454 | ((void) etm); |
| 1455 | #endif |
| 1456 | |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 1457 | t_out->tls_version = tls_version; |
| 1458 | t_in->tls_version = tls_version; |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1459 | t_out->ivlen = ivlen; |
| 1460 | t_in->ivlen = ivlen; |
| 1461 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1462 | switch (cipher_info->mode) { |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1463 | case MBEDTLS_MODE_GCM: |
| 1464 | case MBEDTLS_MODE_CCM: |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1465 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1466 | if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
Hanno Becker | e683287 | 2020-05-28 08:29:58 +0100 | [diff] [blame] | 1467 | t_out->fixed_ivlen = 12; |
| 1468 | t_in->fixed_ivlen = 12; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1469 | } else |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1470 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | e683287 | 2020-05-28 08:29:58 +0100 | [diff] [blame] | 1471 | { |
| 1472 | t_out->fixed_ivlen = 4; |
| 1473 | t_in->fixed_ivlen = 4; |
| 1474 | } |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1475 | t_out->maclen = 0; |
| 1476 | t_in->maclen = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1477 | switch (tag_mode) { |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1478 | case 0: /* Full tag */ |
| 1479 | t_out->taglen = 16; |
| 1480 | t_in->taglen = 16; |
| 1481 | break; |
| 1482 | case 1: /* Partial tag */ |
| 1483 | t_out->taglen = 8; |
| 1484 | t_in->taglen = 8; |
| 1485 | break; |
| 1486 | default: |
Paul Elliott | c7b5374 | 2021-02-03 13:18:33 +0000 | [diff] [blame] | 1487 | ret = 1; |
| 1488 | goto cleanup; |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1489 | } |
| 1490 | break; |
| 1491 | |
| 1492 | case MBEDTLS_MODE_CHACHAPOLY: |
| 1493 | t_out->fixed_ivlen = 12; |
| 1494 | t_in->fixed_ivlen = 12; |
| 1495 | t_out->maclen = 0; |
| 1496 | t_in->maclen = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1497 | switch (tag_mode) { |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1498 | case 0: /* Full tag */ |
| 1499 | t_out->taglen = 16; |
| 1500 | t_in->taglen = 16; |
| 1501 | break; |
| 1502 | case 1: /* Partial tag */ |
| 1503 | t_out->taglen = 8; |
| 1504 | t_in->taglen = 8; |
| 1505 | break; |
| 1506 | default: |
Paul Elliott | c7b5374 | 2021-02-03 13:18:33 +0000 | [diff] [blame] | 1507 | ret = 1; |
| 1508 | goto cleanup; |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1509 | } |
| 1510 | break; |
| 1511 | |
| 1512 | case MBEDTLS_MODE_STREAM: |
| 1513 | case MBEDTLS_MODE_CBC: |
| 1514 | t_out->fixed_ivlen = 0; /* redundant, must be 0 */ |
| 1515 | t_in->fixed_ivlen = 0; /* redundant, must be 0 */ |
| 1516 | t_out->taglen = 0; |
| 1517 | t_in->taglen = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1518 | switch (tag_mode) { |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1519 | case 0: /* Full tag */ |
| 1520 | t_out->maclen = maclen; |
| 1521 | t_in->maclen = maclen; |
| 1522 | break; |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1523 | default: |
Paul Elliott | c7b5374 | 2021-02-03 13:18:33 +0000 | [diff] [blame] | 1524 | ret = 1; |
| 1525 | goto cleanup; |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1526 | } |
| 1527 | break; |
| 1528 | default: |
Paul Elliott | c7b5374 | 2021-02-03 13:18:33 +0000 | [diff] [blame] | 1529 | ret = 1; |
| 1530 | goto cleanup; |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1531 | break; |
| 1532 | } |
| 1533 | |
| 1534 | /* Setup IV's */ |
| 1535 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1536 | memcpy(&t_in->iv_dec, iv_dec, sizeof(iv_dec)); |
| 1537 | memcpy(&t_in->iv_enc, iv_enc, sizeof(iv_enc)); |
| 1538 | memcpy(&t_out->iv_dec, iv_enc, sizeof(iv_enc)); |
| 1539 | memcpy(&t_out->iv_enc, iv_dec, sizeof(iv_dec)); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1540 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1541 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 1542 | /* Add CID */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1543 | memcpy(&t_in->in_cid, cid0, cid0_len); |
| 1544 | memcpy(&t_in->out_cid, cid1, cid1_len); |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 1545 | t_in->in_cid_len = cid0_len; |
| 1546 | t_in->out_cid_len = cid1_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1547 | memcpy(&t_out->in_cid, cid1, cid1_len); |
| 1548 | memcpy(&t_out->out_cid, cid0, cid0_len); |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 1549 | t_out->in_cid_len = cid1_len; |
| 1550 | t_out->out_cid_len = cid0_len; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1551 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 1552 | |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1553 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1554 | status = mbedtls_ssl_cipher_to_psa(cipher_type, |
| 1555 | t_in->taglen, |
| 1556 | &alg, |
| 1557 | &key_type, |
| 1558 | &key_bits); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1559 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1560 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 1561 | ret = PSA_TO_MBEDTLS_ERR(status); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1562 | goto cleanup; |
| 1563 | } |
| 1564 | |
| 1565 | t_in->psa_alg = alg; |
| 1566 | t_out->psa_alg = alg; |
| 1567 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1568 | if (alg != MBEDTLS_SSL_NULL_CIPHER) { |
| 1569 | psa_reset_key_attributes(&attributes); |
| 1570 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 1571 | psa_set_key_algorithm(&attributes, alg); |
| 1572 | psa_set_key_type(&attributes, key_type); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1573 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1574 | status = psa_import_key(&attributes, |
| 1575 | key0, |
| 1576 | PSA_BITS_TO_BYTES(key_bits), |
| 1577 | &t_in->psa_key_enc); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1578 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1579 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 1580 | ret = PSA_TO_MBEDTLS_ERR(status); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1581 | goto cleanup; |
| 1582 | } |
| 1583 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1584 | status = psa_import_key(&attributes, |
| 1585 | key1, |
| 1586 | PSA_BITS_TO_BYTES(key_bits), |
| 1587 | &t_out->psa_key_enc); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1588 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1589 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 1590 | ret = PSA_TO_MBEDTLS_ERR(status); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1591 | goto cleanup; |
| 1592 | } |
| 1593 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1594 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
Przemyslaw Stekiel | f4ca3f0 | 2022-01-25 00:25:59 +0100 | [diff] [blame] | 1595 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1596 | status = psa_import_key(&attributes, |
| 1597 | key1, |
| 1598 | PSA_BITS_TO_BYTES(key_bits), |
| 1599 | &t_in->psa_key_dec); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1600 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1601 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 1602 | ret = PSA_TO_MBEDTLS_ERR(status); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1603 | goto cleanup; |
| 1604 | } |
| 1605 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1606 | status = psa_import_key(&attributes, |
| 1607 | key0, |
| 1608 | PSA_BITS_TO_BYTES(key_bits), |
| 1609 | &t_out->psa_key_dec); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1610 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1611 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 1612 | ret = PSA_TO_MBEDTLS_ERR(status); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1613 | goto cleanup; |
| 1614 | } |
| 1615 | } |
| 1616 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1617 | |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 1618 | cleanup: |
| 1619 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1620 | mbedtls_free(key0); |
| 1621 | mbedtls_free(key1); |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 1622 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1623 | mbedtls_free(md0); |
| 1624 | mbedtls_free(md1); |
Paul Elliott | 6f1eda7 | 2020-06-11 20:22:00 +0100 | [diff] [blame] | 1625 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1626 | return ret; |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1627 | } |
| 1628 | |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1629 | /* |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 1630 | * Populate a session structure for serialization tests. |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1631 | * Choose dummy values, mostly non-0 to distinguish from the init default. |
| 1632 | */ |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 1633 | int mbedtls_test_ssl_tls12_populate_session(mbedtls_ssl_session *session, |
| 1634 | int ticket_len, |
| 1635 | const char *crt_file) |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1636 | { |
| 1637 | #if defined(MBEDTLS_HAVE_TIME) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1638 | session->start = mbedtls_time(NULL) - 42; |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1639 | #endif |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 1640 | session->tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1641 | session->ciphersuite = 0xabcd; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1642 | session->id_len = sizeof(session->id); |
| 1643 | memset(session->id, 66, session->id_len); |
| 1644 | memset(session->master, 17, sizeof(session->master)); |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1645 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 1646 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) && defined(MBEDTLS_FS_IO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1647 | if (crt_file != NULL && strlen(crt_file) != 0) { |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 1648 | mbedtls_x509_crt tmp_crt; |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1649 | int ret; |
Manuel Pégourié-Gonnard | 6b84070 | 2019-05-24 09:40:17 +0200 | [diff] [blame] | 1650 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1651 | mbedtls_x509_crt_init(&tmp_crt); |
| 1652 | ret = mbedtls_x509_crt_parse_file(&tmp_crt, crt_file); |
| 1653 | if (ret != 0) { |
| 1654 | return ret; |
| 1655 | } |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 1656 | |
| 1657 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 1658 | /* Move temporary CRT. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1659 | session->peer_cert = mbedtls_calloc(1, sizeof(*session->peer_cert)); |
| 1660 | if (session->peer_cert == NULL) { |
| 1661 | return -1; |
| 1662 | } |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 1663 | *session->peer_cert = tmp_crt; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1664 | memset(&tmp_crt, 0, sizeof(tmp_crt)); |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 1665 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 1666 | /* Calculate digest of temporary CRT. */ |
| 1667 | session->peer_cert_digest = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1668 | mbedtls_calloc(1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN); |
| 1669 | if (session->peer_cert_digest == NULL) { |
| 1670 | return -1; |
| 1671 | } |
Przemek Stekiel | cc59c99 | 2022-08-01 11:51:22 +0200 | [diff] [blame] | 1672 | |
| 1673 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1674 | psa_algorithm_t psa_alg = mbedtls_hash_info_psa_from_md( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1675 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE); |
Przemek Stekiel | cc59c99 | 2022-08-01 11:51:22 +0200 | [diff] [blame] | 1676 | size_t hash_size = 0; |
Yanray Wang | d577a68 | 2022-10-27 11:47:54 +0800 | [diff] [blame^] | 1677 | psa_status_t status = psa_hash_compute( |
| 1678 | psa_alg, tmp_crt.raw.p, |
| 1679 | tmp_crt.raw.len, |
| 1680 | session->peer_cert_digest, |
| 1681 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN, |
| 1682 | &hash_size); |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 1683 | ret = PSA_TO_MBEDTLS_ERR(status); |
Przemek Stekiel | cc59c99 | 2022-08-01 11:51:22 +0200 | [diff] [blame] | 1684 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1685 | ret = mbedtls_md(mbedtls_md_info_from_type( |
| 1686 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE), |
| 1687 | tmp_crt.raw.p, tmp_crt.raw.len, |
| 1688 | session->peer_cert_digest); |
Przemek Stekiel | cc59c99 | 2022-08-01 11:51:22 +0200 | [diff] [blame] | 1689 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1690 | if (ret != 0) { |
| 1691 | return ret; |
| 1692 | } |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 1693 | session->peer_cert_digest_type = |
| 1694 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE; |
| 1695 | session->peer_cert_digest_len = |
| 1696 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN; |
| 1697 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 1698 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1699 | mbedtls_x509_crt_free(&tmp_crt); |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1700 | } |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 1701 | #else /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED && MBEDTLS_FS_IO */ |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1702 | (void) crt_file; |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 1703 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED && MBEDTLS_FS_IO */ |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1704 | session->verify_result = 0xdeadbeef; |
| 1705 | |
| 1706 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1707 | if (ticket_len != 0) { |
| 1708 | session->ticket = mbedtls_calloc(1, ticket_len); |
| 1709 | if (session->ticket == NULL) { |
| 1710 | return -1; |
| 1711 | } |
| 1712 | memset(session->ticket, 33, ticket_len); |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1713 | } |
| 1714 | session->ticket_len = ticket_len; |
| 1715 | session->ticket_lifetime = 86401; |
| 1716 | #else |
| 1717 | (void) ticket_len; |
| 1718 | #endif |
| 1719 | |
| 1720 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 1721 | session->mfl_code = 1; |
| 1722 | #endif |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1723 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 1724 | session->encrypt_then_mac = 1; |
| 1725 | #endif |
| 1726 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1727 | return 0; |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1728 | } |
| 1729 | |
Jerry Yu | 534ff40 | 2022-07-14 16:43:43 +0800 | [diff] [blame] | 1730 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 1731 | int mbedtls_test_ssl_tls13_populate_session(mbedtls_ssl_session *session, |
| 1732 | int ticket_len, |
| 1733 | int endpoint_type) |
Jerry Yu | 534ff40 | 2022-07-14 16:43:43 +0800 | [diff] [blame] | 1734 | { |
| 1735 | ((void) ticket_len); |
| 1736 | session->tls_version = MBEDTLS_SSL_VERSION_TLS1_3; |
| 1737 | session->endpoint = endpoint_type == MBEDTLS_SSL_IS_CLIENT ? |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1738 | MBEDTLS_SSL_IS_CLIENT : MBEDTLS_SSL_IS_SERVER; |
Jerry Yu | 534ff40 | 2022-07-14 16:43:43 +0800 | [diff] [blame] | 1739 | session->ciphersuite = 0xabcd; |
| 1740 | session->ticket_age_add = 0x87654321; |
| 1741 | session->ticket_flags = 0x7; |
| 1742 | |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 1743 | session->resumption_key_len = 32; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1744 | memset(session->resumption_key, 0x99, sizeof(session->resumption_key)); |
Jerry Yu | 534ff40 | 2022-07-14 16:43:43 +0800 | [diff] [blame] | 1745 | |
| 1746 | #if defined(MBEDTLS_HAVE_TIME) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1747 | if (session->endpoint == MBEDTLS_SSL_IS_SERVER) { |
| 1748 | session->start = mbedtls_time(NULL) - 42; |
Jerry Yu | 534ff40 | 2022-07-14 16:43:43 +0800 | [diff] [blame] | 1749 | } |
| 1750 | #endif |
| 1751 | |
| 1752 | #if defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1753 | if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
Jerry Yu | 534ff40 | 2022-07-14 16:43:43 +0800 | [diff] [blame] | 1754 | #if defined(MBEDTLS_HAVE_TIME) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1755 | session->ticket_received = mbedtls_time(NULL) - 40; |
Jerry Yu | 534ff40 | 2022-07-14 16:43:43 +0800 | [diff] [blame] | 1756 | #endif |
| 1757 | session->ticket_lifetime = 0xfedcba98; |
| 1758 | |
| 1759 | session->ticket_len = ticket_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1760 | if (ticket_len != 0) { |
| 1761 | session->ticket = mbedtls_calloc(1, ticket_len); |
| 1762 | if (session->ticket == NULL) { |
| 1763 | return -1; |
| 1764 | } |
| 1765 | memset(session->ticket, 33, ticket_len); |
Jerry Yu | 534ff40 | 2022-07-14 16:43:43 +0800 | [diff] [blame] | 1766 | } |
| 1767 | } |
| 1768 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 1769 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1770 | return 0; |
Jerry Yu | 534ff40 | 2022-07-14 16:43:43 +0800 | [diff] [blame] | 1771 | } |
| 1772 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 1773 | |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1774 | /* |
| 1775 | * Perform data exchanging between \p ssl_1 and \p ssl_2 and check if the |
| 1776 | * message was sent in the correct number of fragments. |
| 1777 | * |
| 1778 | * /p ssl_1 and /p ssl_2 Endpoints represented by mbedtls_ssl_context. Both |
Yanray Wang | d577a68 | 2022-10-27 11:47:54 +0800 | [diff] [blame^] | 1779 | * of them must be initialized and connected |
| 1780 | * beforehand. |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1781 | * /p msg_len_1 and /p msg_len_2 specify the size of the message to send. |
| 1782 | * /p expected_fragments_1 and /p expected_fragments_2 determine in how many |
| 1783 | * fragments the message should be sent. |
| 1784 | * expected_fragments is 0: can be used for DTLS testing while the message |
| 1785 | * size is larger than MFL. In that case the message |
Yanray Wang | d577a68 | 2022-10-27 11:47:54 +0800 | [diff] [blame^] | 1786 | * cannot be fragmented and sent to the second |
| 1787 | * endpoint. |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1788 | * This value can be used for negative tests. |
| 1789 | * expected_fragments is 1: can be used for TLS/DTLS testing while the |
| 1790 | * message size is below MFL |
| 1791 | * expected_fragments > 1: can be used for TLS testing while the message |
| 1792 | * size is larger than MFL |
| 1793 | * |
| 1794 | * \retval 0 on success, otherwise error code. |
| 1795 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1796 | int mbedtls_exchange_data(mbedtls_ssl_context *ssl_1, |
| 1797 | int msg_len_1, const int expected_fragments_1, |
| 1798 | mbedtls_ssl_context *ssl_2, |
| 1799 | int msg_len_2, const int expected_fragments_2) |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1800 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1801 | unsigned char *msg_buf_1 = malloc(msg_len_1); |
| 1802 | unsigned char *msg_buf_2 = malloc(msg_len_2); |
| 1803 | unsigned char *in_buf_1 = malloc(msg_len_2); |
| 1804 | unsigned char *in_buf_2 = malloc(msg_len_1); |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1805 | int msg_type, ret = -1; |
| 1806 | |
| 1807 | /* Perform this test with two message types. At first use a message |
| 1808 | * consisting of only 0x00 for the client and only 0xFF for the server. |
| 1809 | * At the second time use message with generated data */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1810 | for (msg_type = 0; msg_type < 2; msg_type++) { |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1811 | int written_1 = 0; |
| 1812 | int written_2 = 0; |
| 1813 | int read_1 = 0; |
| 1814 | int read_2 = 0; |
| 1815 | int fragments_1 = 0; |
| 1816 | int fragments_2 = 0; |
| 1817 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1818 | if (msg_type == 0) { |
| 1819 | memset(msg_buf_1, 0x00, msg_len_1); |
| 1820 | memset(msg_buf_2, 0xff, msg_len_2); |
| 1821 | } else { |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1822 | int i, j = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1823 | for (i = 0; i < msg_len_1; i++) { |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1824 | msg_buf_1[i] = j++ & 0xFF; |
| 1825 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1826 | for (i = 0; i < msg_len_2; i++) { |
| 1827 | msg_buf_2[i] = (j -= 5) & 0xFF; |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1828 | } |
| 1829 | } |
| 1830 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1831 | while (read_1 < msg_len_2 || read_2 < msg_len_1) { |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1832 | /* ssl_1 sending */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1833 | if (msg_len_1 > written_1) { |
| 1834 | ret = mbedtls_ssl_write_fragment(ssl_1, msg_buf_1, |
| 1835 | msg_len_1, &written_1, |
| 1836 | expected_fragments_1); |
| 1837 | if (expected_fragments_1 == 0) { |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1838 | /* This error is expected when the message is too large and |
| 1839 | * cannot be fragmented */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1840 | TEST_ASSERT(ret == MBEDTLS_ERR_SSL_BAD_INPUT_DATA); |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1841 | msg_len_1 = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1842 | } else { |
| 1843 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1844 | } |
| 1845 | } |
| 1846 | |
| 1847 | /* ssl_2 sending */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1848 | if (msg_len_2 > written_2) { |
| 1849 | ret = mbedtls_ssl_write_fragment(ssl_2, msg_buf_2, |
| 1850 | msg_len_2, &written_2, |
| 1851 | expected_fragments_2); |
| 1852 | if (expected_fragments_2 == 0) { |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1853 | /* This error is expected when the message is too large and |
| 1854 | * cannot be fragmented */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1855 | TEST_ASSERT(ret == MBEDTLS_ERR_SSL_BAD_INPUT_DATA); |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1856 | msg_len_2 = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1857 | } else { |
| 1858 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1859 | } |
| 1860 | } |
| 1861 | |
| 1862 | /* ssl_1 reading */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1863 | if (read_1 < msg_len_2) { |
| 1864 | ret = mbedtls_ssl_read_fragment(ssl_1, in_buf_1, |
| 1865 | msg_len_2, &read_1, |
| 1866 | &fragments_2, |
| 1867 | expected_fragments_2); |
| 1868 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1869 | } |
| 1870 | |
| 1871 | /* ssl_2 reading */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1872 | if (read_2 < msg_len_1) { |
| 1873 | ret = mbedtls_ssl_read_fragment(ssl_2, in_buf_2, |
| 1874 | msg_len_1, &read_2, |
| 1875 | &fragments_1, |
| 1876 | expected_fragments_1); |
| 1877 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1878 | } |
| 1879 | } |
| 1880 | |
| 1881 | ret = -1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1882 | TEST_ASSERT(0 == memcmp(msg_buf_1, in_buf_2, msg_len_1)); |
| 1883 | TEST_ASSERT(0 == memcmp(msg_buf_2, in_buf_1, msg_len_2)); |
| 1884 | TEST_ASSERT(fragments_1 == expected_fragments_1); |
| 1885 | TEST_ASSERT(fragments_2 == expected_fragments_2); |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1886 | } |
| 1887 | |
| 1888 | ret = 0; |
| 1889 | |
| 1890 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1891 | free(msg_buf_1); |
| 1892 | free(in_buf_1); |
| 1893 | free(msg_buf_2); |
| 1894 | free(in_buf_2); |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1895 | |
| 1896 | return ret; |
| 1897 | } |
| 1898 | |
Piotr Nowicki | 95e9eb8 | 2020-02-14 11:33:34 +0100 | [diff] [blame] | 1899 | /* |
| 1900 | * Perform data exchanging between \p ssl_1 and \p ssl_2. Both of endpoints |
| 1901 | * must be initialized and connected beforehand. |
| 1902 | * |
| 1903 | * \retval 0 on success, otherwise error code. |
| 1904 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1905 | int exchange_data(mbedtls_ssl_context *ssl_1, |
| 1906 | mbedtls_ssl_context *ssl_2) |
Piotr Nowicki | 95e9eb8 | 2020-02-14 11:33:34 +0100 | [diff] [blame] | 1907 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1908 | return mbedtls_exchange_data(ssl_1, 256, 1, |
| 1909 | ssl_2, 256, 1); |
Piotr Nowicki | 95e9eb8 | 2020-02-14 11:33:34 +0100 | [diff] [blame] | 1910 | } |
| 1911 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 1912 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 1913 | static int check_ssl_version( |
| 1914 | mbedtls_ssl_protocol_version expected_negotiated_version, |
| 1915 | const mbedtls_ssl_context *ssl) |
Gilles Peskine | 1255b0d | 2022-01-13 01:08:48 +0100 | [diff] [blame] | 1916 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1917 | const char *version_string = mbedtls_ssl_get_version(ssl); |
Gilles Peskine | 1255b0d | 2022-01-13 01:08:48 +0100 | [diff] [blame] | 1918 | mbedtls_ssl_protocol_version version_number = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1919 | mbedtls_ssl_get_version_number(ssl); |
Gilles Peskine | 1255b0d | 2022-01-13 01:08:48 +0100 | [diff] [blame] | 1920 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1921 | TEST_EQUAL(ssl->tls_version, expected_negotiated_version); |
Gilles Peskine | 1255b0d | 2022-01-13 01:08:48 +0100 | [diff] [blame] | 1922 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1923 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 1924 | TEST_EQUAL(version_string[0], 'D'); |
Gilles Peskine | 1255b0d | 2022-01-13 01:08:48 +0100 | [diff] [blame] | 1925 | ++version_string; |
| 1926 | } |
| 1927 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1928 | switch (expected_negotiated_version) { |
Glenn Strauss | e3af4cb | 2022-03-15 03:23:42 -0400 | [diff] [blame] | 1929 | case MBEDTLS_SSL_VERSION_TLS1_2: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1930 | TEST_EQUAL(version_number, MBEDTLS_SSL_VERSION_TLS1_2); |
| 1931 | TEST_ASSERT(strcmp(version_string, "TLSv1.2") == 0); |
Gilles Peskine | 1255b0d | 2022-01-13 01:08:48 +0100 | [diff] [blame] | 1932 | break; |
| 1933 | |
Glenn Strauss | e3af4cb | 2022-03-15 03:23:42 -0400 | [diff] [blame] | 1934 | case MBEDTLS_SSL_VERSION_TLS1_3: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1935 | TEST_EQUAL(version_number, MBEDTLS_SSL_VERSION_TLS1_3); |
| 1936 | TEST_ASSERT(strcmp(version_string, "TLSv1.3") == 0); |
Gilles Peskine | 1255b0d | 2022-01-13 01:08:48 +0100 | [diff] [blame] | 1937 | break; |
| 1938 | |
| 1939 | default: |
Yanray Wang | d577a68 | 2022-10-27 11:47:54 +0800 | [diff] [blame^] | 1940 | TEST_ASSERT( |
| 1941 | !"Version check not implemented for this protocol version"); |
Gilles Peskine | 1255b0d | 2022-01-13 01:08:48 +0100 | [diff] [blame] | 1942 | } |
| 1943 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1944 | return 1; |
Gilles Peskine | 1255b0d | 2022-01-13 01:08:48 +0100 | [diff] [blame] | 1945 | |
| 1946 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1947 | return 0; |
Gilles Peskine | 1255b0d | 2022-01-13 01:08:48 +0100 | [diff] [blame] | 1948 | } |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 1949 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Gilles Peskine | 1255b0d | 2022-01-13 01:08:48 +0100 | [diff] [blame] | 1950 | |
| 1951 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 1952 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 1953 | void mbedtls_test_ssl_perform_handshake( |
| 1954 | mbedtls_test_handshake_test_options *options) |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1955 | { |
| 1956 | /* forced_ciphersuite needs to last until the end of the handshake */ |
| 1957 | int forced_ciphersuite[2]; |
| 1958 | enum { BUFFSIZE = 17000 }; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 1959 | mbedtls_test_ssl_endpoint client, server; |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 1960 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED) |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1961 | const char *psk_identity = "foo"; |
| 1962 | #endif |
| 1963 | #if defined(MBEDTLS_TIMING_C) |
| 1964 | mbedtls_timing_delay_context timer_client, timer_server; |
| 1965 | #endif |
| 1966 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
| 1967 | unsigned char *context_buf = NULL; |
| 1968 | size_t context_buf_len; |
| 1969 | #endif |
| 1970 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 1971 | int ret = -1; |
| 1972 | #endif |
Neil Armstrong | 8c52ed8 | 2022-05-27 13:14:55 +0200 | [diff] [blame] | 1973 | int expected_handshake_result = options->expected_handshake_result; |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1974 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1975 | USE_PSA_INIT(); |
| 1976 | mbedtls_platform_zeroize(&client, sizeof(client)); |
| 1977 | mbedtls_platform_zeroize(&server, sizeof(server)); |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 1978 | mbedtls_test_ssl_message_queue server_queue, client_queue; |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1979 | mbedtls_test_message_socket_context server_context, client_context; |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 1980 | mbedtls_test_message_socket_init(&server_context); |
| 1981 | mbedtls_test_message_socket_init(&client_context); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1982 | |
| 1983 | /* Client side */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1984 | if (options->dtls != 0) { |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 1985 | TEST_ASSERT(mbedtls_test_ssl_endpoint_init(&client, |
| 1986 | MBEDTLS_SSL_IS_CLIENT, |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 1987 | options, &client_context, |
| 1988 | &client_queue, |
| 1989 | &server_queue, NULL) == 0); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1990 | #if defined(MBEDTLS_TIMING_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1991 | mbedtls_ssl_set_timer_cb(&client.ssl, &timer_client, |
| 1992 | mbedtls_timing_set_delay, |
| 1993 | mbedtls_timing_get_delay); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1994 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1995 | } else { |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 1996 | TEST_ASSERT(mbedtls_test_ssl_endpoint_init(&client, |
| 1997 | MBEDTLS_SSL_IS_CLIENT, |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 1998 | options, NULL, NULL, |
| 1999 | NULL, NULL) == 0); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2000 | } |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 2001 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2002 | if (options->client_min_version != MBEDTLS_SSL_VERSION_UNKNOWN) { |
| 2003 | mbedtls_ssl_conf_min_tls_version(&client.conf, |
| 2004 | options->client_min_version); |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 2005 | } |
| 2006 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2007 | if (options->client_max_version != MBEDTLS_SSL_VERSION_UNKNOWN) { |
| 2008 | mbedtls_ssl_conf_max_tls_version(&client.conf, |
| 2009 | options->client_max_version); |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 2010 | } |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2011 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2012 | if (strlen(options->cipher) > 0) { |
| 2013 | set_ciphersuite(&client.conf, options->cipher, forced_ciphersuite); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2014 | } |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 2015 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2016 | #if defined(MBEDTLS_DEBUG_C) |
| 2017 | if (options->cli_log_fun) { |
| 2018 | mbedtls_debug_set_threshold(4); |
| 2019 | mbedtls_ssl_conf_dbg(&client.conf, options->cli_log_fun, |
| 2020 | options->cli_log_obj); |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 2021 | } |
| 2022 | #endif |
| 2023 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2024 | /* Server side */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2025 | if (options->dtls != 0) { |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 2026 | TEST_ASSERT(mbedtls_test_ssl_endpoint_init(&server, |
| 2027 | MBEDTLS_SSL_IS_SERVER, |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2028 | options, &server_context, |
| 2029 | &server_queue, |
| 2030 | &client_queue, NULL) == 0); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2031 | #if defined(MBEDTLS_TIMING_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2032 | mbedtls_ssl_set_timer_cb(&server.ssl, &timer_server, |
| 2033 | mbedtls_timing_set_delay, |
| 2034 | mbedtls_timing_get_delay); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2035 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2036 | } else { |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 2037 | TEST_ASSERT(mbedtls_test_ssl_endpoint_init(&server, |
| 2038 | MBEDTLS_SSL_IS_SERVER, |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2039 | options, NULL, NULL, NULL, |
| 2040 | NULL) == 0); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2041 | } |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 2042 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2043 | mbedtls_ssl_conf_authmode(&server.conf, options->srv_auth_mode); |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 2044 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2045 | if (options->server_min_version != MBEDTLS_SSL_VERSION_UNKNOWN) { |
| 2046 | mbedtls_ssl_conf_min_tls_version(&server.conf, |
| 2047 | options->server_min_version); |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 2048 | } |
| 2049 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2050 | if (options->server_max_version != MBEDTLS_SSL_VERSION_UNKNOWN) { |
| 2051 | mbedtls_ssl_conf_max_tls_version(&server.conf, |
| 2052 | options->server_max_version); |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 2053 | } |
| 2054 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2055 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2056 | TEST_ASSERT(mbedtls_ssl_conf_max_frag_len(&(server.conf), |
Yanray Wang | d577a68 | 2022-10-27 11:47:54 +0800 | [diff] [blame^] | 2057 | (unsigned char) options->mfl) |
| 2058 | == 0); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2059 | TEST_ASSERT(mbedtls_ssl_conf_max_frag_len(&(client.conf), |
Yanray Wang | d577a68 | 2022-10-27 11:47:54 +0800 | [diff] [blame^] | 2060 | (unsigned char) options->mfl) |
| 2061 | == 0); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2062 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2063 | TEST_ASSERT(MBEDTLS_SSL_MAX_FRAG_LEN_NONE == options->mfl); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2064 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 2065 | |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 2066 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2067 | if (options->psk_str != NULL && options->psk_str->len > 0) { |
Yanray Wang | d577a68 | 2022-10-27 11:47:54 +0800 | [diff] [blame^] | 2068 | TEST_ASSERT(mbedtls_ssl_conf_psk( |
| 2069 | &client.conf, options->psk_str->x, |
| 2070 | options->psk_str->len, |
| 2071 | (const unsigned char *) psk_identity, |
| 2072 | strlen(psk_identity)) == 0); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2073 | |
Yanray Wang | d577a68 | 2022-10-27 11:47:54 +0800 | [diff] [blame^] | 2074 | TEST_ASSERT(mbedtls_ssl_conf_psk( |
| 2075 | &server.conf, options->psk_str->x, |
| 2076 | options->psk_str->len, |
| 2077 | (const unsigned char *) psk_identity, |
| 2078 | strlen(psk_identity)) == 0); |
Jerry Yu | 8897c07 | 2022-08-12 13:56:53 +0800 | [diff] [blame] | 2079 | #if defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2080 | mbedtls_ssl_conf_psk_cb(&server.conf, psk_dummy_callback, NULL); |
Jerry Yu | 8897c07 | 2022-08-12 13:56:53 +0800 | [diff] [blame] | 2081 | #endif |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2082 | } |
| 2083 | #endif |
| 2084 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2085 | if (options->renegotiate) { |
| 2086 | mbedtls_ssl_conf_renegotiation(&(server.conf), |
| 2087 | MBEDTLS_SSL_RENEGOTIATION_ENABLED); |
| 2088 | mbedtls_ssl_conf_renegotiation(&(client.conf), |
| 2089 | MBEDTLS_SSL_RENEGOTIATION_ENABLED); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2090 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2091 | mbedtls_ssl_conf_legacy_renegotiation(&(server.conf), |
| 2092 | options->legacy_renegotiation); |
| 2093 | mbedtls_ssl_conf_legacy_renegotiation(&(client.conf), |
| 2094 | options->legacy_renegotiation); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2095 | } |
| 2096 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
| 2097 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2098 | #if defined(MBEDTLS_DEBUG_C) |
| 2099 | if (options->srv_log_fun) { |
| 2100 | mbedtls_debug_set_threshold(4); |
| 2101 | mbedtls_ssl_conf_dbg(&server.conf, options->srv_log_fun, |
| 2102 | options->srv_log_obj); |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 2103 | } |
| 2104 | #endif |
| 2105 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2106 | TEST_ASSERT(mbedtls_test_mock_socket_connect(&(client.socket), |
| 2107 | &(server.socket), |
| 2108 | BUFFSIZE) == 0); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2109 | |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2110 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2111 | if (options->resize_buffers != 0) { |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2112 | /* Ensure that the buffer sizes are appropriate before resizes */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2113 | TEST_ASSERT(client.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN); |
| 2114 | TEST_ASSERT(client.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN); |
| 2115 | TEST_ASSERT(server.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN); |
| 2116 | TEST_ASSERT(server.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2117 | } |
| 2118 | #endif |
| 2119 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2120 | if (options->expected_negotiated_version == MBEDTLS_SSL_VERSION_UNKNOWN) { |
Hanno Becker | bc00044 | 2021-06-24 09:18:19 +0100 | [diff] [blame] | 2121 | expected_handshake_result = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION; |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 2122 | } |
| 2123 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2124 | TEST_ASSERT(mbedtls_test_move_handshake_to_state(&(client.ssl), |
| 2125 | &(server.ssl), |
| 2126 | MBEDTLS_SSL_HANDSHAKE_OVER) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2127 | == expected_handshake_result); |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 2128 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2129 | if (expected_handshake_result != 0) { |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 2130 | /* Connection will have failed by this point, skip to cleanup */ |
| 2131 | goto exit; |
| 2132 | } |
| 2133 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2134 | TEST_ASSERT(mbedtls_ssl_is_handshake_over(&client.ssl) == 1); |
Jerry Yu | df0a71a | 2022-05-26 10:43:30 +0800 | [diff] [blame] | 2135 | |
Jerry Yu | 66adf31 | 2022-05-31 15:23:29 +0800 | [diff] [blame] | 2136 | /* Make sure server state is moved to HANDSHAKE_OVER also. */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2137 | TEST_EQUAL(mbedtls_test_move_handshake_to_state(&(server.ssl), |
| 2138 | &(client.ssl), |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 2139 | MBEDTLS_SSL_HANDSHAKE_OVER), |
| 2140 | 0); |
Jerry Yu | df0a71a | 2022-05-26 10:43:30 +0800 | [diff] [blame] | 2141 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2142 | TEST_ASSERT(mbedtls_ssl_is_handshake_over(&server.ssl) == 1); |
Gilles Peskine | 1255b0d | 2022-01-13 01:08:48 +0100 | [diff] [blame] | 2143 | /* Check that both sides have negotiated the expected version. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2144 | mbedtls_test_set_step(0); |
| 2145 | if (!check_ssl_version(options->expected_negotiated_version, |
| 2146 | &client.ssl)) { |
Gilles Peskine | 1255b0d | 2022-01-13 01:08:48 +0100 | [diff] [blame] | 2147 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2148 | } |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 2149 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2150 | mbedtls_test_set_step(1); |
| 2151 | if (!check_ssl_version(options->expected_negotiated_version, |
| 2152 | &server.ssl)) { |
Gilles Peskine | 1255b0d | 2022-01-13 01:08:48 +0100 | [diff] [blame] | 2153 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2154 | } |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 2155 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2156 | if (options->expected_ciphersuite != 0) { |
| 2157 | TEST_EQUAL(server.ssl.session->ciphersuite, |
| 2158 | options->expected_ciphersuite); |
Neil Armstrong | 8c52ed8 | 2022-05-27 13:14:55 +0200 | [diff] [blame] | 2159 | } |
| 2160 | |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2161 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2162 | if (options->resize_buffers != 0) { |
TRodziewicz | 2abf03c | 2021-06-25 14:40:09 +0200 | [diff] [blame] | 2163 | /* A server, when using DTLS, might delay a buffer resize to happen |
| 2164 | * after it receives a message, so we force it. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2165 | TEST_ASSERT(exchange_data(&(client.ssl), &(server.ssl)) == 0); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2166 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2167 | TEST_ASSERT(client.ssl.out_buf_len == |
| 2168 | mbedtls_ssl_get_output_buflen(&client.ssl)); |
| 2169 | TEST_ASSERT(client.ssl.in_buf_len == |
| 2170 | mbedtls_ssl_get_input_buflen(&client.ssl)); |
| 2171 | TEST_ASSERT(server.ssl.out_buf_len == |
| 2172 | mbedtls_ssl_get_output_buflen(&server.ssl)); |
| 2173 | TEST_ASSERT(server.ssl.in_buf_len == |
| 2174 | mbedtls_ssl_get_input_buflen(&server.ssl)); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2175 | } |
| 2176 | #endif |
| 2177 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2178 | if (options->cli_msg_len != 0 || options->srv_msg_len != 0) { |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2179 | /* Start data exchanging test */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2180 | TEST_ASSERT(mbedtls_exchange_data(&(client.ssl), options->cli_msg_len, |
| 2181 | options->expected_cli_fragments, |
| 2182 | &(server.ssl), options->srv_msg_len, |
| 2183 | options->expected_srv_fragments) |
| 2184 | == 0); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2185 | } |
| 2186 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2187 | if (options->serialize == 1) { |
| 2188 | TEST_ASSERT(options->dtls == 1); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2189 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2190 | TEST_ASSERT(mbedtls_ssl_context_save(&(server.ssl), NULL, |
| 2191 | 0, &context_buf_len) |
| 2192 | == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2193 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2194 | context_buf = mbedtls_calloc(1, context_buf_len); |
| 2195 | TEST_ASSERT(context_buf != NULL); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2196 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2197 | TEST_ASSERT(mbedtls_ssl_context_save(&(server.ssl), context_buf, |
| 2198 | context_buf_len, |
Yanray Wang | d577a68 | 2022-10-27 11:47:54 +0800 | [diff] [blame^] | 2199 | &context_buf_len) |
| 2200 | == 0); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2201 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2202 | mbedtls_ssl_free(&(server.ssl)); |
| 2203 | mbedtls_ssl_init(&(server.ssl)); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2204 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2205 | TEST_ASSERT(mbedtls_ssl_setup(&(server.ssl), &(server.conf)) == 0); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2206 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2207 | mbedtls_ssl_set_bio(&(server.ssl), &server_context, |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2208 | mbedtls_test_mock_tcp_send_msg, |
| 2209 | mbedtls_test_mock_tcp_recv_msg, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2210 | NULL); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2211 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2212 | mbedtls_ssl_set_user_data_p(&server.ssl, &server); |
Gilles Peskine | 49d7ddf | 2022-01-27 23:25:51 +0100 | [diff] [blame] | 2213 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2214 | #if defined(MBEDTLS_TIMING_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2215 | mbedtls_ssl_set_timer_cb(&server.ssl, &timer_server, |
| 2216 | mbedtls_timing_set_delay, |
| 2217 | mbedtls_timing_get_delay); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2218 | #endif |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2219 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2220 | if (options->resize_buffers != 0) { |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2221 | /* Ensure that the buffer sizes are appropriate before resizes */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2222 | TEST_ASSERT(server.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN); |
| 2223 | TEST_ASSERT(server.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2224 | } |
| 2225 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2226 | TEST_ASSERT(mbedtls_ssl_context_load(&(server.ssl), context_buf, |
| 2227 | context_buf_len) == 0); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2228 | |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2229 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 2230 | /* Validate buffer sizes after context deserialization */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2231 | if (options->resize_buffers != 0) { |
| 2232 | TEST_ASSERT(server.ssl.out_buf_len == |
| 2233 | mbedtls_ssl_get_output_buflen(&server.ssl)); |
| 2234 | TEST_ASSERT(server.ssl.in_buf_len == |
| 2235 | mbedtls_ssl_get_input_buflen(&server.ssl)); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2236 | } |
| 2237 | #endif |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2238 | /* Retest writing/reading */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2239 | if (options->cli_msg_len != 0 || options->srv_msg_len != 0) { |
Yanray Wang | d577a68 | 2022-10-27 11:47:54 +0800 | [diff] [blame^] | 2240 | TEST_ASSERT(mbedtls_exchange_data( |
| 2241 | &(client.ssl), |
| 2242 | options->cli_msg_len, |
| 2243 | options->expected_cli_fragments, |
| 2244 | &(server.ssl), |
| 2245 | options->srv_msg_len, |
| 2246 | options->expected_srv_fragments) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2247 | == 0); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2248 | } |
| 2249 | } |
| 2250 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2251 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2252 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2253 | if (options->renegotiate) { |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2254 | /* Start test with renegotiation */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2255 | TEST_ASSERT(server.ssl.renego_status == |
| 2256 | MBEDTLS_SSL_INITIAL_HANDSHAKE); |
| 2257 | TEST_ASSERT(client.ssl.renego_status == |
| 2258 | MBEDTLS_SSL_INITIAL_HANDSHAKE); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2259 | |
| 2260 | /* After calling this function for the server, it only sends a handshake |
| 2261 | * request. All renegotiation should happen during data exchanging */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2262 | TEST_ASSERT(mbedtls_ssl_renegotiate(&(server.ssl)) == 0); |
| 2263 | TEST_ASSERT(server.ssl.renego_status == |
| 2264 | MBEDTLS_SSL_RENEGOTIATION_PENDING); |
| 2265 | TEST_ASSERT(client.ssl.renego_status == |
| 2266 | MBEDTLS_SSL_INITIAL_HANDSHAKE); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2267 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2268 | TEST_ASSERT(exchange_data(&(client.ssl), &(server.ssl)) == 0); |
| 2269 | TEST_ASSERT(server.ssl.renego_status == |
| 2270 | MBEDTLS_SSL_RENEGOTIATION_DONE); |
| 2271 | TEST_ASSERT(client.ssl.renego_status == |
| 2272 | MBEDTLS_SSL_RENEGOTIATION_DONE); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2273 | |
Yanray Wang | d577a68 | 2022-10-27 11:47:54 +0800 | [diff] [blame^] | 2274 | /* After calling mbedtls_ssl_renegotiate for the client, |
| 2275 | * all renegotiation should happen inside this function. |
| 2276 | * However in this test, we cannot perform simultaneous communication |
| 2277 | * between client and server so this function will return waiting error |
| 2278 | * on the socket. All rest of renegotiation should happen |
| 2279 | * during data exchanging */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2280 | ret = mbedtls_ssl_renegotiate(&(client.ssl)); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2281 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2282 | if (options->resize_buffers != 0) { |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2283 | /* Ensure that the buffer sizes are appropriate before resizes */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2284 | TEST_ASSERT(client.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN); |
| 2285 | TEST_ASSERT(client.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2286 | } |
| 2287 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2288 | TEST_ASSERT(ret == 0 || |
| 2289 | ret == MBEDTLS_ERR_SSL_WANT_READ || |
| 2290 | ret == MBEDTLS_ERR_SSL_WANT_WRITE); |
| 2291 | TEST_ASSERT(server.ssl.renego_status == |
| 2292 | MBEDTLS_SSL_RENEGOTIATION_DONE); |
| 2293 | TEST_ASSERT(client.ssl.renego_status == |
| 2294 | MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2295 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2296 | TEST_ASSERT(exchange_data(&(client.ssl), &(server.ssl)) == 0); |
| 2297 | TEST_ASSERT(server.ssl.renego_status == |
| 2298 | MBEDTLS_SSL_RENEGOTIATION_DONE); |
| 2299 | TEST_ASSERT(client.ssl.renego_status == |
| 2300 | MBEDTLS_SSL_RENEGOTIATION_DONE); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2301 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 2302 | /* Validate buffer sizes after renegotiation */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2303 | if (options->resize_buffers != 0) { |
| 2304 | TEST_ASSERT(client.ssl.out_buf_len == |
| 2305 | mbedtls_ssl_get_output_buflen(&client.ssl)); |
| 2306 | TEST_ASSERT(client.ssl.in_buf_len == |
| 2307 | mbedtls_ssl_get_input_buflen(&client.ssl)); |
| 2308 | TEST_ASSERT(server.ssl.out_buf_len == |
| 2309 | mbedtls_ssl_get_output_buflen(&server.ssl)); |
| 2310 | TEST_ASSERT(server.ssl.in_buf_len == |
| 2311 | mbedtls_ssl_get_input_buflen(&server.ssl)); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2312 | } |
| 2313 | #endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */ |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2314 | } |
| 2315 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
| 2316 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2317 | TEST_ASSERT(mbedtls_ssl_conf_get_user_data_p(&client.conf) == &client); |
| 2318 | TEST_ASSERT(mbedtls_ssl_get_user_data_p(&client.ssl) == &client); |
| 2319 | TEST_ASSERT(mbedtls_ssl_conf_get_user_data_p(&server.conf) == &server); |
| 2320 | TEST_ASSERT(mbedtls_ssl_get_user_data_p(&server.ssl) == &server); |
Gilles Peskine | 80dae04 | 2022-01-21 23:50:39 +0100 | [diff] [blame] | 2321 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2322 | exit: |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 2323 | mbedtls_test_ssl_endpoint_free(&client, |
| 2324 | options->dtls != 0 ? |
| 2325 | &client_context : NULL); |
| 2326 | mbedtls_test_ssl_endpoint_free(&server, |
| 2327 | options->dtls != 0 ? |
| 2328 | &server_context : NULL); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2329 | #if defined(MBEDTLS_DEBUG_C) |
| 2330 | if (options->cli_log_fun || options->srv_log_fun) { |
| 2331 | mbedtls_debug_set_threshold(0); |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 2332 | } |
| 2333 | #endif |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2334 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2335 | if (context_buf != NULL) { |
| 2336 | mbedtls_free(context_buf); |
| 2337 | } |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2338 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2339 | USE_PSA_DONE(); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2340 | } |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 2341 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2342 | |
Ronald Cron | e7b9b6b | 2022-06-10 17:24:31 +0200 | [diff] [blame] | 2343 | #if defined(MBEDTLS_TEST_HOOKS) |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 2344 | /* |
| 2345 | * Tweak vector lengths in a TLS 1.3 Certificate message |
| 2346 | * |
Ronald Cron | cf600bc | 2022-06-17 15:54:16 +0200 | [diff] [blame] | 2347 | * \param[in] buf Buffer containing the Certificate message to tweak |
| 2348 | * \param[in]]out] end End of the buffer to parse |
| 2349 | * \param tweak Tweak identifier (from 1 to the number of tweaks). |
| 2350 | * \param[out] expected_result Error code expected from the parsing function |
| 2351 | * \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] | 2352 | * is expected to fail. All zeroes if no |
| 2353 | * MBEDTLS_SSL_CHK_BUF_READ_PTR failure is expected. |
| 2354 | */ |
| 2355 | int tweak_tls13_certificate_msg_vector_len( |
Ronald Cron | e7b9b6b | 2022-06-10 17:24:31 +0200 | [diff] [blame] | 2356 | unsigned char *buf, unsigned char **end, int tweak, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2357 | int *expected_result, mbedtls_ssl_chk_buf_ptr_args *args) |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 2358 | { |
| 2359 | /* |
| 2360 | * The definition of the tweaks assume that the certificate list contains only |
| 2361 | * one certificate. |
| 2362 | */ |
| 2363 | |
| 2364 | /* |
| 2365 | * struct { |
| 2366 | * opaque cert_data<1..2^24-1>; |
| 2367 | * Extension extensions<0..2^16-1>; |
| 2368 | * } CertificateEntry; |
| 2369 | * |
| 2370 | * struct { |
| 2371 | * opaque certificate_request_context<0..2^8-1>; |
| 2372 | * CertificateEntry certificate_list<0..2^24-1>; |
| 2373 | * } Certificate; |
| 2374 | */ |
| 2375 | unsigned char *p_certificate_request_context_len = buf; |
| 2376 | size_t certificate_request_context_len = buf[0]; |
| 2377 | |
Yanray Wang | d577a68 | 2022-10-27 11:47:54 +0800 | [diff] [blame^] | 2378 | unsigned char *p_certificate_list_len = |
| 2379 | buf + 1 + certificate_request_context_len; |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 2380 | unsigned char *certificate_list = p_certificate_list_len + 3; |
Yanray Wang | d577a68 | 2022-10-27 11:47:54 +0800 | [diff] [blame^] | 2381 | size_t certificate_list_len = |
| 2382 | MBEDTLS_GET_UINT24_BE(p_certificate_list_len, 0); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 2383 | |
| 2384 | unsigned char *p_cert_data_len = certificate_list; |
| 2385 | unsigned char *cert_data = p_cert_data_len + 3; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2386 | size_t cert_data_len = MBEDTLS_GET_UINT24_BE(p_cert_data_len, 0); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 2387 | |
| 2388 | unsigned char *p_extensions_len = cert_data + cert_data_len; |
| 2389 | unsigned char *extensions = p_extensions_len + 2; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2390 | size_t extensions_len = MBEDTLS_GET_UINT16_BE(p_extensions_len, 0); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 2391 | |
| 2392 | *expected_result = MBEDTLS_ERR_SSL_DECODE_ERROR; |
| 2393 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2394 | switch (tweak) { |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 2395 | case 1: |
Yanray Wang | d577a68 | 2022-10-27 11:47:54 +0800 | [diff] [blame^] | 2396 | /* Failure when checking if the certificate request context length |
| 2397 | * and certificate list length can be read |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2398 | */ |
| 2399 | *end = buf + 3; |
| 2400 | set_chk_buf_ptr_args(args, buf, *end, 4); |
| 2401 | break; |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 2402 | |
| 2403 | case 2: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2404 | /* Invalid certificate request context length. |
| 2405 | */ |
| 2406 | *p_certificate_request_context_len = |
| 2407 | certificate_request_context_len + 1; |
| 2408 | reset_chk_buf_ptr_args(args); |
| 2409 | break; |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 2410 | |
| 2411 | case 3: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2412 | /* Failure when checking if certificate_list data can be read. */ |
| 2413 | MBEDTLS_PUT_UINT24_BE(certificate_list_len + 1, |
| 2414 | p_certificate_list_len, 0); |
| 2415 | set_chk_buf_ptr_args(args, certificate_list, *end, |
| 2416 | certificate_list_len + 1); |
| 2417 | break; |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 2418 | |
| 2419 | case 4: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2420 | /* Failure when checking if the cert_data length can be read. */ |
| 2421 | MBEDTLS_PUT_UINT24_BE(2, p_certificate_list_len, 0); |
| 2422 | set_chk_buf_ptr_args(args, p_cert_data_len, certificate_list + 2, 3); |
| 2423 | break; |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 2424 | |
| 2425 | case 5: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2426 | /* Failure when checking if cert_data data can be read. */ |
| 2427 | MBEDTLS_PUT_UINT24_BE(certificate_list_len - 3 + 1, |
| 2428 | p_cert_data_len, 0); |
| 2429 | set_chk_buf_ptr_args(args, cert_data, |
| 2430 | certificate_list + certificate_list_len, |
| 2431 | certificate_list_len - 3 + 1); |
| 2432 | break; |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 2433 | |
| 2434 | case 6: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2435 | /* Failure when checking if the extensions length can be read. */ |
| 2436 | MBEDTLS_PUT_UINT24_BE(certificate_list_len - extensions_len - 1, |
| 2437 | p_certificate_list_len, 0); |
Yanray Wang | d577a68 | 2022-10-27 11:47:54 +0800 | [diff] [blame^] | 2438 | set_chk_buf_ptr_args( |
| 2439 | args, p_extensions_len, |
| 2440 | certificate_list + certificate_list_len - extensions_len - 1, 2); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2441 | break; |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 2442 | |
| 2443 | case 7: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2444 | /* Failure when checking if extensions data can be read. */ |
| 2445 | MBEDTLS_PUT_UINT16_BE(extensions_len + 1, p_extensions_len, 0); |
Ronald Cron | e7b9b6b | 2022-06-10 17:24:31 +0200 | [diff] [blame] | 2446 | |
Yanray Wang | d577a68 | 2022-10-27 11:47:54 +0800 | [diff] [blame^] | 2447 | set_chk_buf_ptr_args( |
| 2448 | args, extensions, |
| 2449 | certificate_list + certificate_list_len, extensions_len + 1); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2450 | break; |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 2451 | |
| 2452 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2453 | return -1; |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 2454 | } |
| 2455 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2456 | return 0; |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 2457 | } |
Ronald Cron | e7b9b6b | 2022-06-10 17:24:31 +0200 | [diff] [blame] | 2458 | #endif /* MBEDTLS_TEST_HOOKS */ |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 2459 | |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 2460 | #define ECJPAKE_TEST_PWD "bla" |
Valerio Setti | e7518ba | 2022-12-02 12:09:43 +0100 | [diff] [blame] | 2461 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2462 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 2463 | #define ECJPAKE_TEST_SET_PASSWORD(exp_ret_val) \ |
| 2464 | ret = (use_opaque_arg) ? \ |
| 2465 | mbedtls_ssl_set_hs_ecjpake_password_opaque(&ssl, pwd_slot) : \ |
| 2466 | mbedtls_ssl_set_hs_ecjpake_password(&ssl, pwd_string, pwd_len); \ |
| 2467 | TEST_EQUAL(ret, exp_ret_val) |
Valerio Setti | e7518ba | 2022-12-02 12:09:43 +0100 | [diff] [blame] | 2468 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2469 | #define ECJPAKE_TEST_SET_PASSWORD(exp_ret_val) \ |
| 2470 | ret = mbedtls_ssl_set_hs_ecjpake_password(&ssl, \ |
| 2471 | pwd_string, pwd_len); \ |
| 2472 | TEST_EQUAL(ret, exp_ret_val) |
Valerio Setti | e7518ba | 2022-12-02 12:09:43 +0100 | [diff] [blame] | 2473 | #endif |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 2474 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2475 | #define TEST_AVAILABLE_ECC(tls_id_, group_id_, psa_family_, psa_bits_) \ |
| 2476 | TEST_EQUAL(mbedtls_ssl_get_ecp_group_id_from_tls_id(tls_id_), \ |
| 2477 | group_id_); \ |
| 2478 | TEST_EQUAL(mbedtls_ssl_get_tls_id_from_ecp_group_id(group_id_), \ |
| 2479 | tls_id_); \ |
| 2480 | TEST_EQUAL(mbedtls_ssl_get_psa_curve_info_from_tls_id(tls_id_, \ |
| 2481 | &psa_family, &psa_bits), PSA_SUCCESS); \ |
| 2482 | TEST_EQUAL(psa_family_, psa_family); \ |
| 2483 | TEST_EQUAL(psa_bits_, psa_bits); |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 2484 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2485 | #define TEST_UNAVAILABLE_ECC(tls_id_, group_id_, psa_family_, psa_bits_) \ |
| 2486 | TEST_EQUAL(mbedtls_ssl_get_ecp_group_id_from_tls_id(tls_id_), \ |
| 2487 | MBEDTLS_ECP_DP_NONE); \ |
| 2488 | TEST_EQUAL(mbedtls_ssl_get_tls_id_from_ecp_group_id(group_id_), \ |
| 2489 | 0); \ |
| 2490 | TEST_EQUAL(mbedtls_ssl_get_psa_curve_info_from_tls_id(tls_id_, \ |
| 2491 | &psa_family, &psa_bits), \ |
| 2492 | PSA_ERROR_NOT_SUPPORTED); |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 2493 | |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 2494 | /* END_HEADER */ |
| 2495 | |
| 2496 | /* BEGIN_DEPENDENCIES |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2497 | * depends_on:MBEDTLS_SSL_TLS_C |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 2498 | * END_DEPENDENCIES |
| 2499 | */ |
| 2500 | |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2501 | /* BEGIN_CASE */ |
| 2502 | void test_callback_buffer_sanity() |
| 2503 | { |
| 2504 | enum { MSGLEN = 10 }; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 2505 | mbedtls_test_ssl_buffer buf; |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2506 | unsigned char input[MSGLEN]; |
| 2507 | unsigned char output[MSGLEN]; |
| 2508 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2509 | memset(input, 0, sizeof(input)); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2510 | |
| 2511 | /* Make sure calling put and get on NULL buffer results in error. */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2512 | TEST_ASSERT(mbedtls_test_ssl_buffer_put(NULL, input, sizeof(input)) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2513 | == -1); |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2514 | TEST_ASSERT(mbedtls_test_ssl_buffer_get(NULL, output, sizeof(output)) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2515 | == -1); |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 2516 | TEST_ASSERT(mbedtls_test_ssl_buffer_put(NULL, NULL, sizeof(input)) |
| 2517 | == -1); |
Andrzej Kurek | f777414 | 2020-01-22 06:34:59 -0500 | [diff] [blame] | 2518 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2519 | TEST_ASSERT(mbedtls_test_ssl_buffer_put(NULL, NULL, 0) == -1); |
| 2520 | TEST_ASSERT(mbedtls_test_ssl_buffer_get(NULL, NULL, 0) == -1); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2521 | |
| 2522 | /* 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] | 2523 | * in error. */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2524 | mbedtls_test_ssl_buffer_init(&buf); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2525 | |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 2526 | TEST_ASSERT(mbedtls_test_ssl_buffer_put(&buf, input, sizeof(input)) |
| 2527 | == -1); |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2528 | TEST_ASSERT(mbedtls_test_ssl_buffer_get(&buf, output, sizeof(output)) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2529 | == -1); |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 2530 | TEST_ASSERT(mbedtls_test_ssl_buffer_put(&buf, NULL, sizeof(input)) |
| 2531 | == -1); |
Andrzej Kurek | f777414 | 2020-01-22 06:34:59 -0500 | [diff] [blame] | 2532 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2533 | TEST_ASSERT(mbedtls_test_ssl_buffer_put(&buf, NULL, 0) == -1); |
| 2534 | TEST_ASSERT(mbedtls_test_ssl_buffer_get(&buf, NULL, 0) == -1); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2535 | |
Andrzej Kurek | f777414 | 2020-01-22 06:34:59 -0500 | [diff] [blame] | 2536 | /* Make sure calling put and get on NULL input only results in |
| 2537 | * error if the length is not zero, and that a NULL output is valid for data |
| 2538 | * dropping. |
| 2539 | */ |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2540 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2541 | TEST_ASSERT(mbedtls_test_ssl_buffer_setup(&buf, sizeof(input)) == 0); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2542 | |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 2543 | TEST_ASSERT(mbedtls_test_ssl_buffer_put(&buf, NULL, sizeof(input)) |
| 2544 | == -1); |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2545 | TEST_ASSERT(mbedtls_test_ssl_buffer_get(&buf, NULL, sizeof(output)) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2546 | == 0); |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2547 | TEST_ASSERT(mbedtls_test_ssl_buffer_put(&buf, NULL, 0) == 0); |
| 2548 | TEST_ASSERT(mbedtls_test_ssl_buffer_get(&buf, NULL, 0) == 0); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2549 | |
Piotr Nowicki | fb437d7 | 2020-01-13 16:59:12 +0100 | [diff] [blame] | 2550 | /* Make sure calling put several times in the row is safe */ |
| 2551 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2552 | TEST_ASSERT(mbedtls_test_ssl_buffer_put(&buf, input, sizeof(input)) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2553 | == sizeof(input)); |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2554 | TEST_ASSERT(mbedtls_test_ssl_buffer_get(&buf, output, 2) == 2); |
| 2555 | TEST_ASSERT(mbedtls_test_ssl_buffer_put(&buf, input, 1) == 1); |
| 2556 | TEST_ASSERT(mbedtls_test_ssl_buffer_put(&buf, input, 2) == 1); |
| 2557 | TEST_ASSERT(mbedtls_test_ssl_buffer_put(&buf, input, 2) == 0); |
Piotr Nowicki | fb437d7 | 2020-01-13 16:59:12 +0100 | [diff] [blame] | 2558 | |
| 2559 | |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2560 | exit: |
| 2561 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2562 | mbedtls_test_ssl_buffer_free(&buf); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2563 | } |
| 2564 | /* END_CASE */ |
| 2565 | |
| 2566 | /* |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 2567 | * Test if the implementation of `mbedtls_test_ssl_buffer` related functions is |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2568 | * correct and works as expected. |
| 2569 | * |
| 2570 | * That is |
| 2571 | * - If we try to put in \p put1 bytes then we can put in \p put1_ret bytes. |
| 2572 | * - Afterwards if we try to get \p get1 bytes then we can get \get1_ret bytes. |
| 2573 | * - Next, if we try to put in \p put1 bytes then we can put in \p put1_ret |
| 2574 | * bytes. |
| 2575 | * - Afterwards if we try to get \p get1 bytes then we can get \get1_ret bytes. |
| 2576 | * - All of the bytes we got match the bytes we put in in a FIFO manner. |
| 2577 | */ |
| 2578 | |
| 2579 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2580 | void test_callback_buffer(int size, int put1, int put1_ret, |
| 2581 | int get1, int get1_ret, int put2, int put2_ret, |
| 2582 | int get2, int get2_ret) |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2583 | { |
| 2584 | enum { ROUNDS = 2 }; |
| 2585 | size_t put[ROUNDS]; |
| 2586 | int put_ret[ROUNDS]; |
| 2587 | size_t get[ROUNDS]; |
| 2588 | int get_ret[ROUNDS]; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 2589 | mbedtls_test_ssl_buffer buf; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2590 | unsigned char *input = NULL; |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2591 | size_t input_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2592 | unsigned char *output = NULL; |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2593 | size_t output_len; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2594 | size_t i, j, written, read; |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2595 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2596 | mbedtls_test_ssl_buffer_init(&buf); |
| 2597 | TEST_ASSERT(mbedtls_test_ssl_buffer_setup(&buf, size) == 0); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2598 | |
| 2599 | /* Check the sanity of input parameters and initialise local variables. That |
| 2600 | * is, ensure that the amount of data is not negative and that we are not |
| 2601 | * expecting more to put or get than we actually asked for. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2602 | TEST_ASSERT(put1 >= 0); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2603 | put[0] = put1; |
| 2604 | put_ret[0] = put1_ret; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2605 | TEST_ASSERT(put1_ret <= put1); |
| 2606 | TEST_ASSERT(put2 >= 0); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2607 | put[1] = put2; |
| 2608 | put_ret[1] = put2_ret; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2609 | TEST_ASSERT(put2_ret <= put2); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2610 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2611 | TEST_ASSERT(get1 >= 0); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2612 | get[0] = get1; |
| 2613 | get_ret[0] = get1_ret; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2614 | TEST_ASSERT(get1_ret <= get1); |
| 2615 | TEST_ASSERT(get2 >= 0); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2616 | get[1] = get2; |
| 2617 | get_ret[1] = get2_ret; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2618 | TEST_ASSERT(get2_ret <= get2); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2619 | |
| 2620 | input_len = 0; |
| 2621 | /* Calculate actual input and output lengths */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2622 | for (j = 0; j < ROUNDS; j++) { |
| 2623 | if (put_ret[j] > 0) { |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2624 | input_len += put_ret[j]; |
| 2625 | } |
| 2626 | } |
| 2627 | /* In order to always have a valid pointer we always allocate at least 1 |
| 2628 | * byte. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2629 | if (input_len == 0) { |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2630 | input_len = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2631 | } |
| 2632 | ASSERT_ALLOC(input, input_len); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2633 | |
| 2634 | output_len = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2635 | for (j = 0; j < ROUNDS; j++) { |
| 2636 | if (get_ret[j] > 0) { |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2637 | output_len += get_ret[j]; |
| 2638 | } |
| 2639 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2640 | TEST_ASSERT(output_len <= input_len); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2641 | /* In order to always have a valid pointer we always allocate at least 1 |
| 2642 | * byte. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2643 | if (output_len == 0) { |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2644 | output_len = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2645 | } |
| 2646 | ASSERT_ALLOC(output, output_len); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2647 | |
| 2648 | /* Fill up the buffer with structured data so that unwanted changes |
| 2649 | * can be detected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2650 | for (i = 0; i < input_len; i++) { |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2651 | input[i] = i & 0xFF; |
| 2652 | } |
| 2653 | |
| 2654 | written = read = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2655 | for (j = 0; j < ROUNDS; j++) { |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2656 | TEST_ASSERT(put_ret[j] == mbedtls_test_ssl_buffer_put(&buf, |
| 2657 | input + written, put[j])); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2658 | written += put_ret[j]; |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2659 | TEST_ASSERT(get_ret[j] == mbedtls_test_ssl_buffer_get(&buf, |
| 2660 | output + read, get[j])); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2661 | read += get_ret[j]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2662 | TEST_ASSERT(read <= written); |
| 2663 | if (get_ret[j] > 0) { |
| 2664 | TEST_ASSERT(memcmp(output + read - get_ret[j], |
| 2665 | input + read - get_ret[j], get_ret[j]) |
| 2666 | == 0); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2667 | } |
| 2668 | } |
| 2669 | |
| 2670 | exit: |
| 2671 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2672 | mbedtls_free(input); |
| 2673 | mbedtls_free(output); |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2674 | mbedtls_test_ssl_buffer_free(&buf); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2675 | } |
| 2676 | /* END_CASE */ |
| 2677 | |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2678 | /* |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 2679 | * Test if the implementation of `mbedtls_test_mock_socket` related |
| 2680 | * I/O functions is correct and works as expected on unconnected sockets. |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2681 | */ |
| 2682 | |
| 2683 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2684 | void ssl_mock_sanity() |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2685 | { |
| 2686 | enum { MSGLEN = 105 }; |
Paul Elliott | 21c8fe5 | 2021-11-24 16:54:26 +0000 | [diff] [blame] | 2687 | unsigned char message[MSGLEN] = { 0 }; |
| 2688 | unsigned char received[MSGLEN] = { 0 }; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 2689 | mbedtls_test_mock_socket socket; |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2690 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2691 | mbedtls_mock_socket_init(&socket); |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2692 | TEST_ASSERT(mbedtls_test_mock_tcp_send_b(&socket, message, MSGLEN) < 0); |
| 2693 | mbedtls_test_mock_socket_close(&socket); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2694 | mbedtls_mock_socket_init(&socket); |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2695 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_b(&socket, received, MSGLEN) < 0); |
| 2696 | mbedtls_test_mock_socket_close(&socket); |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2697 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2698 | mbedtls_mock_socket_init(&socket); |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2699 | TEST_ASSERT(mbedtls_test_mock_tcp_send_nb(&socket, message, MSGLEN) < 0); |
| 2700 | mbedtls_test_mock_socket_close(&socket); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2701 | mbedtls_mock_socket_init(&socket); |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2702 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_nb(&socket, received, MSGLEN) < 0); |
| 2703 | mbedtls_test_mock_socket_close(&socket); |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2704 | |
| 2705 | exit: |
| 2706 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2707 | mbedtls_test_mock_socket_close(&socket); |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2708 | } |
| 2709 | /* END_CASE */ |
| 2710 | |
| 2711 | /* |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 2712 | * Test if the implementation of `mbedtls_test_mock_socket` related functions |
| 2713 | * can send a single message from the client to the server. |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2714 | */ |
| 2715 | |
| 2716 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2717 | void ssl_mock_tcp(int blocking) |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2718 | { |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2719 | enum { MSGLEN = 105 }; |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2720 | enum { BUFLEN = MSGLEN / 5 }; |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2721 | unsigned char message[MSGLEN]; |
| 2722 | unsigned char received[MSGLEN]; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 2723 | mbedtls_test_mock_socket client; |
| 2724 | mbedtls_test_mock_socket server; |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2725 | size_t written, read; |
| 2726 | int send_ret, recv_ret; |
| 2727 | mbedtls_ssl_send_t *send; |
| 2728 | mbedtls_ssl_recv_t *recv; |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2729 | unsigned i; |
| 2730 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2731 | if (blocking == 0) { |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2732 | send = mbedtls_test_mock_tcp_send_nb; |
| 2733 | recv = mbedtls_test_mock_tcp_recv_nb; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2734 | } else { |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2735 | send = mbedtls_test_mock_tcp_send_b; |
| 2736 | recv = mbedtls_test_mock_tcp_recv_b; |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2737 | } |
| 2738 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2739 | mbedtls_mock_socket_init(&client); |
| 2740 | mbedtls_mock_socket_init(&server); |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2741 | |
| 2742 | /* Fill up the buffer with structured data so that unwanted changes |
| 2743 | * can be detected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2744 | for (i = 0; i < MSGLEN; i++) { |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2745 | message[i] = i & 0xFF; |
| 2746 | } |
| 2747 | |
| 2748 | /* Make sure that sending a message takes a few iterations. */ |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 2749 | TEST_ASSERT(0 == mbedtls_test_mock_socket_connect(&client, &server, |
| 2750 | BUFLEN)); |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2751 | |
| 2752 | /* Send the message to the server */ |
| 2753 | send_ret = recv_ret = 1; |
| 2754 | written = read = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2755 | while (send_ret != 0 || recv_ret != 0) { |
| 2756 | send_ret = send(&client, message + written, MSGLEN - written); |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2757 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2758 | TEST_ASSERT(send_ret >= 0); |
| 2759 | TEST_ASSERT(send_ret <= BUFLEN); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2760 | written += send_ret; |
| 2761 | |
| 2762 | /* If the buffer is full we can test blocking and non-blocking send */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2763 | if (send_ret == BUFLEN) { |
| 2764 | int blocking_ret = send(&client, message, 1); |
| 2765 | if (blocking) { |
| 2766 | TEST_ASSERT(blocking_ret == 0); |
| 2767 | } else { |
| 2768 | TEST_ASSERT(blocking_ret == MBEDTLS_ERR_SSL_WANT_WRITE); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2769 | } |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2770 | } |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2771 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2772 | recv_ret = recv(&server, received + read, MSGLEN - read); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2773 | |
| 2774 | /* The result depends on whether any data was sent */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2775 | if (send_ret > 0) { |
| 2776 | TEST_ASSERT(recv_ret > 0); |
| 2777 | TEST_ASSERT(recv_ret <= BUFLEN); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2778 | read += recv_ret; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2779 | } else if (blocking) { |
| 2780 | TEST_ASSERT(recv_ret == 0); |
| 2781 | } else { |
| 2782 | TEST_ASSERT(recv_ret == MBEDTLS_ERR_SSL_WANT_READ); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2783 | recv_ret = 0; |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2784 | } |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2785 | |
| 2786 | /* If the buffer is empty we can test blocking and non-blocking read */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2787 | if (recv_ret == BUFLEN) { |
| 2788 | int blocking_ret = recv(&server, received, 1); |
| 2789 | if (blocking) { |
| 2790 | TEST_ASSERT(blocking_ret == 0); |
| 2791 | } else { |
| 2792 | TEST_ASSERT(blocking_ret == MBEDTLS_ERR_SSL_WANT_READ); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2793 | } |
| 2794 | } |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2795 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2796 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2797 | |
| 2798 | exit: |
| 2799 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2800 | mbedtls_test_mock_socket_close(&client); |
| 2801 | mbedtls_test_mock_socket_close(&server); |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2802 | } |
| 2803 | /* END_CASE */ |
| 2804 | |
| 2805 | /* |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 2806 | * Test if the implementation of `mbedtls_test_mock_socket` related functions |
| 2807 | * can send messages in both direction at the same time (with the I/O calls |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2808 | * interleaving). |
| 2809 | */ |
| 2810 | |
| 2811 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2812 | void ssl_mock_tcp_interleaving(int blocking) |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2813 | { |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2814 | enum { ROUNDS = 2 }; |
| 2815 | enum { MSGLEN = 105 }; |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2816 | enum { BUFLEN = MSGLEN / 5 }; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2817 | unsigned char message[ROUNDS][MSGLEN]; |
| 2818 | unsigned char received[ROUNDS][MSGLEN]; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 2819 | mbedtls_test_mock_socket client; |
| 2820 | mbedtls_test_mock_socket server; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2821 | size_t written[ROUNDS]; |
| 2822 | size_t read[ROUNDS]; |
| 2823 | int send_ret[ROUNDS]; |
| 2824 | int recv_ret[ROUNDS]; |
| 2825 | unsigned i, j, progress; |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 2826 | mbedtls_ssl_send_t *send; |
| 2827 | mbedtls_ssl_recv_t *recv; |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 2828 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2829 | if (blocking == 0) { |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2830 | send = mbedtls_test_mock_tcp_send_nb; |
| 2831 | recv = mbedtls_test_mock_tcp_recv_nb; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2832 | } else { |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2833 | send = mbedtls_test_mock_tcp_send_b; |
| 2834 | recv = mbedtls_test_mock_tcp_recv_b; |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 2835 | } |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2836 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2837 | mbedtls_mock_socket_init(&client); |
| 2838 | mbedtls_mock_socket_init(&server); |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2839 | |
| 2840 | /* Fill up the buffers with structured data so that unwanted changes |
| 2841 | * can be detected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2842 | for (i = 0; i < ROUNDS; i++) { |
| 2843 | for (j = 0; j < MSGLEN; j++) { |
| 2844 | message[i][j] = (i * MSGLEN + j) & 0xFF; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2845 | } |
| 2846 | } |
| 2847 | |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2848 | /* Make sure that sending a message takes a few iterations. */ |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 2849 | TEST_ASSERT(0 == mbedtls_test_mock_socket_connect(&client, &server, |
| 2850 | BUFLEN)); |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2851 | |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2852 | /* Send the message from both sides, interleaving. */ |
| 2853 | progress = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2854 | for (i = 0; i < ROUNDS; i++) { |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2855 | written[i] = 0; |
| 2856 | read[i] = 0; |
| 2857 | } |
| 2858 | /* This loop does not stop as long as there was a successful write or read |
| 2859 | * of at least one byte on either side. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2860 | while (progress != 0) { |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 2861 | mbedtls_test_mock_socket *socket; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2862 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2863 | for (i = 0; i < ROUNDS; i++) { |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2864 | /* First sending is from the client */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2865 | socket = (i % 2 == 0) ? (&client) : (&server); |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2866 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2867 | send_ret[i] = send(socket, message[i] + written[i], |
| 2868 | MSGLEN - written[i]); |
| 2869 | TEST_ASSERT(send_ret[i] >= 0); |
| 2870 | TEST_ASSERT(send_ret[i] <= BUFLEN); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2871 | written[i] += send_ret[i]; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2872 | |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2873 | /* If the buffer is full we can test blocking and non-blocking |
| 2874 | * send */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2875 | if (send_ret[i] == BUFLEN) { |
| 2876 | int blocking_ret = send(socket, message[i], 1); |
| 2877 | if (blocking) { |
| 2878 | TEST_ASSERT(blocking_ret == 0); |
| 2879 | } else { |
| 2880 | TEST_ASSERT(blocking_ret == MBEDTLS_ERR_SSL_WANT_WRITE); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2881 | } |
| 2882 | } |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 2883 | } |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2884 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2885 | for (i = 0; i < ROUNDS; i++) { |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2886 | /* First receiving is from the server */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2887 | socket = (i % 2 == 0) ? (&server) : (&client); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2888 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2889 | recv_ret[i] = recv(socket, received[i] + read[i], |
| 2890 | MSGLEN - read[i]); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2891 | |
| 2892 | /* The result depends on whether any data was sent */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2893 | if (send_ret[i] > 0) { |
| 2894 | TEST_ASSERT(recv_ret[i] > 0); |
| 2895 | TEST_ASSERT(recv_ret[i] <= BUFLEN); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2896 | read[i] += recv_ret[i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2897 | } else if (blocking) { |
| 2898 | TEST_ASSERT(recv_ret[i] == 0); |
| 2899 | } else { |
| 2900 | TEST_ASSERT(recv_ret[i] == MBEDTLS_ERR_SSL_WANT_READ); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2901 | recv_ret[i] = 0; |
| 2902 | } |
| 2903 | |
| 2904 | /* If the buffer is empty we can test blocking and non-blocking |
| 2905 | * read */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2906 | if (recv_ret[i] == BUFLEN) { |
| 2907 | int blocking_ret = recv(socket, received[i], 1); |
| 2908 | if (blocking) { |
| 2909 | TEST_ASSERT(blocking_ret == 0); |
| 2910 | } else { |
| 2911 | TEST_ASSERT(blocking_ret == MBEDTLS_ERR_SSL_WANT_READ); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2912 | } |
| 2913 | } |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 2914 | } |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2915 | |
| 2916 | progress = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2917 | for (i = 0; i < ROUNDS; i++) { |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2918 | progress += send_ret[i] + recv_ret[i]; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2919 | } |
| 2920 | } |
| 2921 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2922 | for (i = 0; i < ROUNDS; i++) { |
| 2923 | TEST_ASSERT(memcmp(message[i], received[i], MSGLEN) == 0); |
| 2924 | } |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2925 | |
| 2926 | exit: |
| 2927 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2928 | mbedtls_test_mock_socket_close(&client); |
| 2929 | mbedtls_test_mock_socket_close(&server); |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2930 | } |
| 2931 | /* END_CASE */ |
| 2932 | |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2933 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2934 | void ssl_message_queue_sanity() |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2935 | { |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 2936 | mbedtls_test_ssl_message_queue queue; |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2937 | |
| 2938 | /* Trying to push/pull to an empty queue */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2939 | TEST_ASSERT(mbedtls_test_ssl_message_queue_push_info(NULL, 1) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2940 | == MBEDTLS_TEST_ERROR_ARG_NULL); |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2941 | TEST_ASSERT(mbedtls_test_ssl_message_queue_pop_info(NULL, 1) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2942 | == MBEDTLS_TEST_ERROR_ARG_NULL); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2943 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2944 | TEST_ASSERT(mbedtls_test_ssl_message_queue_setup(&queue, 3) == 0); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2945 | TEST_ASSERT(queue.capacity == 3); |
| 2946 | TEST_ASSERT(queue.num == 0); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2947 | |
| 2948 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2949 | mbedtls_test_ssl_message_queue_free(&queue); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2950 | } |
| 2951 | /* END_CASE */ |
| 2952 | |
| 2953 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2954 | void ssl_message_queue_basic() |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2955 | { |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 2956 | mbedtls_test_ssl_message_queue queue; |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2957 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2958 | TEST_ASSERT(mbedtls_test_ssl_message_queue_setup(&queue, 3) == 0); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2959 | |
| 2960 | /* Sanity test - 3 pushes and 3 pops with sufficient space */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2961 | TEST_ASSERT(mbedtls_test_ssl_message_queue_push_info(&queue, 1) == 1); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2962 | TEST_ASSERT(queue.capacity == 3); |
| 2963 | TEST_ASSERT(queue.num == 1); |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2964 | TEST_ASSERT(mbedtls_test_ssl_message_queue_push_info(&queue, 1) == 1); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2965 | TEST_ASSERT(queue.capacity == 3); |
| 2966 | TEST_ASSERT(queue.num == 2); |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2967 | TEST_ASSERT(mbedtls_test_ssl_message_queue_push_info(&queue, 2) == 2); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2968 | TEST_ASSERT(queue.capacity == 3); |
| 2969 | TEST_ASSERT(queue.num == 3); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2970 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2971 | TEST_ASSERT(mbedtls_test_ssl_message_queue_pop_info(&queue, 1) == 1); |
| 2972 | TEST_ASSERT(mbedtls_test_ssl_message_queue_pop_info(&queue, 1) == 1); |
| 2973 | TEST_ASSERT(mbedtls_test_ssl_message_queue_pop_info(&queue, 2) == 2); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2974 | |
| 2975 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2976 | mbedtls_test_ssl_message_queue_free(&queue); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2977 | } |
| 2978 | /* END_CASE */ |
| 2979 | |
| 2980 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2981 | void ssl_message_queue_overflow_underflow() |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2982 | { |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 2983 | mbedtls_test_ssl_message_queue queue; |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2984 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2985 | TEST_ASSERT(mbedtls_test_ssl_message_queue_setup(&queue, 3) == 0); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2986 | |
| 2987 | /* 4 pushes (last one with an error), 4 pops (last one with an error) */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2988 | TEST_ASSERT(mbedtls_test_ssl_message_queue_push_info(&queue, 1) == 1); |
| 2989 | TEST_ASSERT(mbedtls_test_ssl_message_queue_push_info(&queue, 1) == 1); |
| 2990 | TEST_ASSERT(mbedtls_test_ssl_message_queue_push_info(&queue, 2) == 2); |
| 2991 | TEST_ASSERT(mbedtls_test_ssl_message_queue_push_info(&queue, 3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2992 | == MBEDTLS_ERR_SSL_WANT_WRITE); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2993 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2994 | TEST_ASSERT(mbedtls_test_ssl_message_queue_pop_info(&queue, 1) == 1); |
| 2995 | TEST_ASSERT(mbedtls_test_ssl_message_queue_pop_info(&queue, 1) == 1); |
| 2996 | TEST_ASSERT(mbedtls_test_ssl_message_queue_pop_info(&queue, 2) == 2); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2997 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2998 | TEST_ASSERT(mbedtls_test_ssl_message_queue_pop_info(&queue, 1) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2999 | == MBEDTLS_ERR_SSL_WANT_READ); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 3000 | |
| 3001 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3002 | mbedtls_test_ssl_message_queue_free(&queue); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 3003 | } |
| 3004 | /* END_CASE */ |
| 3005 | |
| 3006 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3007 | void ssl_message_queue_interleaved() |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 3008 | { |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 3009 | mbedtls_test_ssl_message_queue queue; |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 3010 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3011 | TEST_ASSERT(mbedtls_test_ssl_message_queue_setup(&queue, 3) == 0); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 3012 | |
| 3013 | /* Interleaved test - [2 pushes, 1 pop] twice, and then two pops |
| 3014 | * (to wrap around the buffer) */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3015 | TEST_ASSERT(mbedtls_test_ssl_message_queue_push_info(&queue, 1) == 1); |
| 3016 | TEST_ASSERT(mbedtls_test_ssl_message_queue_push_info(&queue, 1) == 1); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 3017 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3018 | TEST_ASSERT(mbedtls_test_ssl_message_queue_pop_info(&queue, 1) == 1); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 3019 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3020 | TEST_ASSERT(mbedtls_test_ssl_message_queue_push_info(&queue, 2) == 2); |
| 3021 | TEST_ASSERT(mbedtls_test_ssl_message_queue_push_info(&queue, 3) == 3); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 3022 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3023 | TEST_ASSERT(mbedtls_test_ssl_message_queue_pop_info(&queue, 1) == 1); |
| 3024 | TEST_ASSERT(mbedtls_test_ssl_message_queue_pop_info(&queue, 2) == 2); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 3025 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3026 | TEST_ASSERT(mbedtls_test_ssl_message_queue_push_info(&queue, 5) == 5); |
| 3027 | TEST_ASSERT(mbedtls_test_ssl_message_queue_push_info(&queue, 8) == 8); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 3028 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3029 | TEST_ASSERT(mbedtls_test_ssl_message_queue_pop_info(&queue, 3) == 3); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 3030 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3031 | TEST_ASSERT(mbedtls_test_ssl_message_queue_pop_info(&queue, 5) == 5); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 3032 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3033 | TEST_ASSERT(mbedtls_test_ssl_message_queue_pop_info(&queue, 8) == 8); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 3034 | |
| 3035 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3036 | mbedtls_test_ssl_message_queue_free(&queue); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 3037 | } |
| 3038 | /* END_CASE */ |
| 3039 | |
| 3040 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3041 | void ssl_message_queue_insufficient_buffer() |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 3042 | { |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 3043 | mbedtls_test_ssl_message_queue queue; |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 3044 | size_t message_len = 10; |
| 3045 | size_t buffer_len = 5; |
| 3046 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3047 | TEST_ASSERT(mbedtls_test_ssl_message_queue_setup(&queue, 1) == 0); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 3048 | |
| 3049 | /* Popping without a sufficient buffer */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3050 | TEST_ASSERT(mbedtls_test_ssl_message_queue_push_info(&queue, message_len) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3051 | == (int) message_len); |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3052 | TEST_ASSERT(mbedtls_test_ssl_message_queue_pop_info(&queue, buffer_len) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3053 | == (int) buffer_len); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 3054 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3055 | mbedtls_test_ssl_message_queue_free(&queue); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 3056 | } |
| 3057 | /* END_CASE */ |
| 3058 | |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3059 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3060 | void ssl_message_mock_uninitialized() |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3061 | { |
| 3062 | enum { MSGLEN = 10 }; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3063 | unsigned char message[MSGLEN] = { 0 }, received[MSGLEN]; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 3064 | mbedtls_test_mock_socket client, server; |
| 3065 | mbedtls_test_ssl_message_queue server_queue, client_queue; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3066 | mbedtls_test_message_socket_context server_context, client_context; |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3067 | mbedtls_test_message_socket_init(&server_context); |
| 3068 | mbedtls_test_message_socket_init(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3069 | |
| 3070 | /* Send with a NULL context */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3071 | TEST_ASSERT(mbedtls_test_mock_tcp_send_msg(NULL, message, MSGLEN) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3072 | == MBEDTLS_TEST_ERROR_CONTEXT_ERROR); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3073 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3074 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(NULL, message, MSGLEN) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3075 | == MBEDTLS_TEST_ERROR_CONTEXT_ERROR); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3076 | |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 3077 | TEST_ASSERT(mbedtls_test_message_socket_setup(&server_queue, |
| 3078 | &client_queue, 1, |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3079 | &server, |
| 3080 | &server_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3081 | |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 3082 | TEST_ASSERT(mbedtls_test_message_socket_setup(&client_queue, |
| 3083 | &server_queue, 1, |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3084 | &client, |
| 3085 | &client_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3086 | |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 3087 | TEST_ASSERT(mbedtls_test_mock_tcp_send_msg(&client_context, message, |
| 3088 | MSGLEN) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3089 | == MBEDTLS_TEST_ERROR_SEND_FAILED); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3090 | |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 3091 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&server_context, received, |
| 3092 | MSGLEN) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3093 | == MBEDTLS_ERR_SSL_WANT_READ); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3094 | |
| 3095 | /* Push directly to a queue to later simulate a disconnected behavior */ |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 3096 | TEST_ASSERT(mbedtls_test_ssl_message_queue_push_info(&server_queue, |
| 3097 | MSGLEN) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3098 | == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3099 | |
| 3100 | /* Test if there's an error when trying to read from a disconnected |
| 3101 | * socket */ |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 3102 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&server_context, received, |
| 3103 | MSGLEN) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3104 | == MBEDTLS_TEST_ERROR_RECV_FAILED); |
| 3105 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3106 | mbedtls_test_message_socket_close(&server_context); |
| 3107 | mbedtls_test_message_socket_close(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3108 | } |
| 3109 | /* END_CASE */ |
| 3110 | |
| 3111 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3112 | void ssl_message_mock_basic() |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3113 | { |
| 3114 | enum { MSGLEN = 10 }; |
| 3115 | unsigned char message[MSGLEN], received[MSGLEN]; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 3116 | mbedtls_test_mock_socket client, server; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3117 | unsigned i; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 3118 | mbedtls_test_ssl_message_queue server_queue, client_queue; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3119 | mbedtls_test_message_socket_context server_context, client_context; |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3120 | mbedtls_test_message_socket_init(&server_context); |
| 3121 | mbedtls_test_message_socket_init(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3122 | |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 3123 | TEST_ASSERT(mbedtls_test_message_socket_setup(&server_queue, |
| 3124 | &client_queue, 1, |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3125 | &server, |
| 3126 | &server_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3127 | |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 3128 | TEST_ASSERT(mbedtls_test_message_socket_setup(&client_queue, |
| 3129 | &server_queue, 1, |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3130 | &client, |
| 3131 | &client_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3132 | |
| 3133 | /* Fill up the buffer with structured data so that unwanted changes |
| 3134 | * can be detected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3135 | for (i = 0; i < MSGLEN; i++) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3136 | message[i] = i & 0xFF; |
| 3137 | } |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3138 | TEST_ASSERT(0 == mbedtls_test_mock_socket_connect(&client, &server, |
| 3139 | MSGLEN)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3140 | |
| 3141 | /* Send the message to the server */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3142 | TEST_ASSERT(mbedtls_test_mock_tcp_send_msg(&client_context, message, |
| 3143 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3144 | |
| 3145 | /* Read from the server */ |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 3146 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&server_context, received, |
| 3147 | MSGLEN) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3148 | == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3149 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3150 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
| 3151 | memset(received, 0, MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3152 | |
| 3153 | /* Send the message to the client */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3154 | TEST_ASSERT(mbedtls_test_mock_tcp_send_msg(&server_context, message, |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 3155 | MSGLEN) |
| 3156 | == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3157 | |
| 3158 | /* Read from the client */ |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 3159 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&client_context, received, |
| 3160 | MSGLEN) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3161 | == MSGLEN); |
| 3162 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3163 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3164 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3165 | mbedtls_test_message_socket_close(&server_context); |
| 3166 | mbedtls_test_message_socket_close(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3167 | } |
| 3168 | /* END_CASE */ |
| 3169 | |
| 3170 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3171 | void ssl_message_mock_queue_overflow_underflow() |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3172 | { |
| 3173 | enum { MSGLEN = 10 }; |
| 3174 | unsigned char message[MSGLEN], received[MSGLEN]; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 3175 | mbedtls_test_mock_socket client, server; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3176 | unsigned i; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 3177 | mbedtls_test_ssl_message_queue server_queue, client_queue; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3178 | mbedtls_test_message_socket_context server_context, client_context; |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3179 | mbedtls_test_message_socket_init(&server_context); |
| 3180 | mbedtls_test_message_socket_init(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3181 | |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 3182 | TEST_ASSERT(mbedtls_test_message_socket_setup(&server_queue, |
| 3183 | &client_queue, 2, |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3184 | &server, |
| 3185 | &server_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3186 | |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 3187 | TEST_ASSERT(mbedtls_test_message_socket_setup(&client_queue, |
| 3188 | &server_queue, 2, |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3189 | &client, |
| 3190 | &client_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3191 | |
| 3192 | /* Fill up the buffer with structured data so that unwanted changes |
| 3193 | * can be detected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3194 | for (i = 0; i < MSGLEN; i++) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3195 | message[i] = i & 0xFF; |
| 3196 | } |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3197 | TEST_ASSERT(0 == mbedtls_test_mock_socket_connect(&client, &server, |
| 3198 | MSGLEN*2)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3199 | |
| 3200 | /* Send three message to the server, last one with an error */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3201 | TEST_ASSERT(mbedtls_test_mock_tcp_send_msg(&client_context, message, |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 3202 | MSGLEN - 1) |
| 3203 | == MSGLEN - 1); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3204 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3205 | TEST_ASSERT(mbedtls_test_mock_tcp_send_msg(&client_context, message, |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 3206 | MSGLEN) |
| 3207 | == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3208 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3209 | TEST_ASSERT(mbedtls_test_mock_tcp_send_msg(&client_context, message, |
| 3210 | MSGLEN) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3211 | == MBEDTLS_ERR_SSL_WANT_WRITE); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3212 | |
| 3213 | /* Read three messages from the server, last one with an error */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3214 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&server_context, received, |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 3215 | MSGLEN - 1) |
| 3216 | == MSGLEN - 1); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3217 | |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 3218 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&server_context, received, |
| 3219 | MSGLEN) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3220 | == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3221 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3222 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3223 | |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 3224 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&server_context, received, |
| 3225 | MSGLEN) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3226 | == MBEDTLS_ERR_SSL_WANT_READ); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3227 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3228 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3229 | mbedtls_test_message_socket_close(&server_context); |
| 3230 | mbedtls_test_message_socket_close(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3231 | } |
| 3232 | /* END_CASE */ |
| 3233 | |
| 3234 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3235 | void ssl_message_mock_socket_overflow() |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3236 | { |
| 3237 | enum { MSGLEN = 10 }; |
| 3238 | unsigned char message[MSGLEN], received[MSGLEN]; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 3239 | mbedtls_test_mock_socket client, server; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3240 | unsigned i; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 3241 | mbedtls_test_ssl_message_queue server_queue, client_queue; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3242 | mbedtls_test_message_socket_context server_context, client_context; |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3243 | mbedtls_test_message_socket_init(&server_context); |
| 3244 | mbedtls_test_message_socket_init(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3245 | |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 3246 | TEST_ASSERT(mbedtls_test_message_socket_setup(&server_queue, |
| 3247 | &client_queue, 2, |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3248 | &server, |
| 3249 | &server_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3250 | |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 3251 | TEST_ASSERT(mbedtls_test_message_socket_setup(&client_queue, |
| 3252 | &server_queue, 2, |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3253 | &client, |
| 3254 | &client_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3255 | |
| 3256 | /* Fill up the buffer with structured data so that unwanted changes |
| 3257 | * can be detected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3258 | for (i = 0; i < MSGLEN; i++) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3259 | message[i] = i & 0xFF; |
| 3260 | } |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3261 | TEST_ASSERT(0 == mbedtls_test_mock_socket_connect(&client, &server, |
| 3262 | MSGLEN)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3263 | |
| 3264 | /* Send two message to the server, second one with an error */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3265 | TEST_ASSERT(mbedtls_test_mock_tcp_send_msg(&client_context, message, |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 3266 | MSGLEN) |
| 3267 | == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3268 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3269 | TEST_ASSERT(mbedtls_test_mock_tcp_send_msg(&client_context, message, |
| 3270 | MSGLEN) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3271 | == MBEDTLS_TEST_ERROR_SEND_FAILED); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3272 | |
| 3273 | /* Read the only message from the server */ |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 3274 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&server_context, received, |
| 3275 | MSGLEN) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3276 | == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3277 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3278 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3279 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3280 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3281 | mbedtls_test_message_socket_close(&server_context); |
| 3282 | mbedtls_test_message_socket_close(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3283 | } |
| 3284 | /* END_CASE */ |
| 3285 | |
| 3286 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3287 | void ssl_message_mock_truncated() |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3288 | { |
| 3289 | enum { MSGLEN = 10 }; |
| 3290 | unsigned char message[MSGLEN], received[MSGLEN]; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 3291 | mbedtls_test_mock_socket client, server; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3292 | unsigned i; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 3293 | mbedtls_test_ssl_message_queue server_queue, client_queue; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3294 | mbedtls_test_message_socket_context server_context, client_context; |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3295 | mbedtls_test_message_socket_init(&server_context); |
| 3296 | mbedtls_test_message_socket_init(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3297 | |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 3298 | TEST_ASSERT(mbedtls_test_message_socket_setup(&server_queue, |
| 3299 | &client_queue, 2, |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3300 | &server, |
| 3301 | &server_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3302 | |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 3303 | TEST_ASSERT(mbedtls_test_message_socket_setup(&client_queue, |
| 3304 | &server_queue, 2, |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3305 | &client, |
| 3306 | &client_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3307 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3308 | memset(received, 0, MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3309 | /* Fill up the buffer with structured data so that unwanted changes |
| 3310 | * can be detected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3311 | for (i = 0; i < MSGLEN; i++) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3312 | message[i] = i & 0xFF; |
| 3313 | } |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3314 | TEST_ASSERT(0 == mbedtls_test_mock_socket_connect(&client, &server, |
| 3315 | 2 * MSGLEN)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3316 | |
| 3317 | /* Send two messages to the server, the second one small enough to fit in the |
| 3318 | * receiver's buffer. */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3319 | TEST_ASSERT(mbedtls_test_mock_tcp_send_msg(&client_context, message, |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 3320 | MSGLEN) |
| 3321 | == MSGLEN); |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3322 | TEST_ASSERT(mbedtls_test_mock_tcp_send_msg(&client_context, message, |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 3323 | MSGLEN / 2) |
| 3324 | == MSGLEN / 2); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3325 | /* Read a truncated message from the server */ |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 3326 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&server_context, received, |
| 3327 | MSGLEN/2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3328 | == MSGLEN/2); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3329 | |
| 3330 | /* Test that the first half of the message is valid, and second one isn't */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3331 | TEST_ASSERT(memcmp(message, received, MSGLEN/2) == 0); |
| 3332 | TEST_ASSERT(memcmp(message + MSGLEN/2, received + MSGLEN/2, MSGLEN/2) |
| 3333 | != 0); |
| 3334 | memset(received, 0, MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3335 | |
| 3336 | /* Read a full message from the server */ |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 3337 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&server_context, received, |
| 3338 | MSGLEN/2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3339 | == MSGLEN / 2); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3340 | |
| 3341 | /* Test that the first half of the message is valid */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3342 | TEST_ASSERT(memcmp(message, received, MSGLEN/2) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3343 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3344 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3345 | mbedtls_test_message_socket_close(&server_context); |
| 3346 | mbedtls_test_message_socket_close(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3347 | } |
| 3348 | /* END_CASE */ |
| 3349 | |
| 3350 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3351 | void ssl_message_mock_socket_read_error() |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3352 | { |
| 3353 | enum { MSGLEN = 10 }; |
| 3354 | unsigned char message[MSGLEN], received[MSGLEN]; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 3355 | mbedtls_test_mock_socket client, server; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3356 | unsigned i; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 3357 | mbedtls_test_ssl_message_queue server_queue, client_queue; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3358 | mbedtls_test_message_socket_context server_context, client_context; |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3359 | mbedtls_test_message_socket_init(&server_context); |
| 3360 | mbedtls_test_message_socket_init(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3361 | |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 3362 | TEST_ASSERT(mbedtls_test_message_socket_setup(&server_queue, |
| 3363 | &client_queue, 1, |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3364 | &server, |
| 3365 | &server_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3366 | |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 3367 | TEST_ASSERT(mbedtls_test_message_socket_setup(&client_queue, |
| 3368 | &server_queue, 1, |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3369 | &client, |
| 3370 | &client_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3371 | |
| 3372 | /* Fill up the buffer with structured data so that unwanted changes |
| 3373 | * can be detected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3374 | for (i = 0; i < MSGLEN; i++) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3375 | message[i] = i & 0xFF; |
| 3376 | } |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3377 | TEST_ASSERT(0 == mbedtls_test_mock_socket_connect(&client, &server, |
| 3378 | MSGLEN)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3379 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3380 | TEST_ASSERT(mbedtls_test_mock_tcp_send_msg(&client_context, message, |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 3381 | MSGLEN) |
| 3382 | == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3383 | |
| 3384 | /* Force a read error by disconnecting the socket by hand */ |
| 3385 | server.status = 0; |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 3386 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&server_context, received, |
| 3387 | MSGLEN) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3388 | == MBEDTLS_TEST_ERROR_RECV_FAILED); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3389 | /* Return to a valid state */ |
| 3390 | server.status = MBEDTLS_MOCK_SOCKET_CONNECTED; |
| 3391 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3392 | memset(received, 0, sizeof(received)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3393 | |
| 3394 | /* Test that even though the server tried to read once disconnected, the |
| 3395 | * continuity is preserved */ |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 3396 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&server_context, received, |
| 3397 | MSGLEN) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3398 | == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3399 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3400 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3401 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3402 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3403 | mbedtls_test_message_socket_close(&server_context); |
| 3404 | mbedtls_test_message_socket_close(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3405 | } |
| 3406 | /* END_CASE */ |
| 3407 | |
| 3408 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3409 | void ssl_message_mock_interleaved_one_way() |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3410 | { |
| 3411 | enum { MSGLEN = 10 }; |
| 3412 | unsigned char message[MSGLEN], received[MSGLEN]; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 3413 | mbedtls_test_mock_socket client, server; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3414 | unsigned i; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 3415 | mbedtls_test_ssl_message_queue server_queue, client_queue; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3416 | mbedtls_test_message_socket_context server_context, client_context; |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3417 | mbedtls_test_message_socket_init(&server_context); |
| 3418 | mbedtls_test_message_socket_init(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3419 | |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 3420 | TEST_ASSERT(mbedtls_test_message_socket_setup(&server_queue, |
| 3421 | &client_queue, 3, |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3422 | &server, |
| 3423 | &server_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3424 | |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 3425 | TEST_ASSERT(mbedtls_test_message_socket_setup(&client_queue, |
| 3426 | &server_queue, 3, |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3427 | &client, |
| 3428 | &client_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3429 | |
| 3430 | /* Fill up the buffer with structured data so that unwanted changes |
| 3431 | * can be detected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3432 | for (i = 0; i < MSGLEN; i++) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3433 | message[i] = i & 0xFF; |
| 3434 | } |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3435 | TEST_ASSERT(0 == mbedtls_test_mock_socket_connect(&client, &server, |
| 3436 | MSGLEN*3)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3437 | |
| 3438 | /* Interleaved test - [2 sends, 1 read] twice, and then two reads |
| 3439 | * (to wrap around the buffer) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3440 | for (i = 0; i < 2; i++) { |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3441 | TEST_ASSERT(mbedtls_test_mock_tcp_send_msg(&client_context, message, |
| 3442 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3443 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3444 | TEST_ASSERT(mbedtls_test_mock_tcp_send_msg(&client_context, message, |
| 3445 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3446 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3447 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&server_context, received, |
| 3448 | MSGLEN) == MSGLEN); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3449 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
| 3450 | memset(received, 0, sizeof(received)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3451 | } |
| 3452 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3453 | for (i = 0; i < 2; i++) { |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3454 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&server_context, received, |
| 3455 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3456 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3457 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3458 | } |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 3459 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&server_context, received, |
| 3460 | MSGLEN) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3461 | == MBEDTLS_ERR_SSL_WANT_READ); |
| 3462 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3463 | mbedtls_test_message_socket_close(&server_context); |
| 3464 | mbedtls_test_message_socket_close(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3465 | } |
| 3466 | /* END_CASE */ |
| 3467 | |
| 3468 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3469 | void ssl_message_mock_interleaved_two_ways() |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3470 | { |
| 3471 | enum { MSGLEN = 10 }; |
| 3472 | unsigned char message[MSGLEN], received[MSGLEN]; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 3473 | mbedtls_test_mock_socket client, server; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3474 | unsigned i; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 3475 | mbedtls_test_ssl_message_queue server_queue, client_queue; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3476 | mbedtls_test_message_socket_context server_context, client_context; |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3477 | mbedtls_test_message_socket_init(&server_context); |
| 3478 | mbedtls_test_message_socket_init(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3479 | |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 3480 | TEST_ASSERT(mbedtls_test_message_socket_setup(&server_queue, |
| 3481 | &client_queue, 3, |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3482 | &server, |
| 3483 | &server_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3484 | |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 3485 | TEST_ASSERT(mbedtls_test_message_socket_setup(&client_queue, |
| 3486 | &server_queue, 3, |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3487 | &client, |
| 3488 | &client_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3489 | |
| 3490 | /* Fill up the buffer with structured data so that unwanted changes |
| 3491 | * can be detected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3492 | for (i = 0; i < MSGLEN; i++) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3493 | message[i] = i & 0xFF; |
| 3494 | } |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3495 | TEST_ASSERT(0 == mbedtls_test_mock_socket_connect(&client, &server, |
| 3496 | MSGLEN*3)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3497 | |
| 3498 | /* Interleaved test - [2 sends, 1 read] twice, both ways, and then two reads |
| 3499 | * (to wrap around the buffer) both ways. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3500 | for (i = 0; i < 2; i++) { |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3501 | TEST_ASSERT(mbedtls_test_mock_tcp_send_msg(&client_context, message, |
| 3502 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3503 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3504 | TEST_ASSERT(mbedtls_test_mock_tcp_send_msg(&client_context, message, |
| 3505 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3506 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3507 | TEST_ASSERT(mbedtls_test_mock_tcp_send_msg(&server_context, message, |
| 3508 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3509 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3510 | TEST_ASSERT(mbedtls_test_mock_tcp_send_msg(&server_context, message, |
| 3511 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3512 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3513 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&server_context, received, |
| 3514 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3515 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3516 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3517 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3518 | memset(received, 0, sizeof(received)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3519 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3520 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&client_context, received, |
| 3521 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3522 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3523 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3524 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3525 | memset(received, 0, sizeof(received)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3526 | } |
| 3527 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3528 | for (i = 0; i < 2; i++) { |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3529 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&server_context, received, |
| 3530 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3531 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3532 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
| 3533 | memset(received, 0, sizeof(received)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3534 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3535 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&client_context, received, |
| 3536 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3537 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3538 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
| 3539 | memset(received, 0, sizeof(received)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3540 | } |
| 3541 | |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 3542 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&server_context, received, |
| 3543 | MSGLEN) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3544 | == MBEDTLS_ERR_SSL_WANT_READ); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3545 | |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 3546 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&client_context, received, |
| 3547 | MSGLEN) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3548 | == MBEDTLS_ERR_SSL_WANT_READ); |
| 3549 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3550 | mbedtls_test_message_socket_close(&server_context); |
| 3551 | mbedtls_test_message_socket_close(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3552 | } |
| 3553 | /* END_CASE */ |
| 3554 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3555 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3556 | 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] | 3557 | { |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 3558 | uint32_t len = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3559 | mbedtls_ssl_context ssl; |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 3560 | mbedtls_ssl_config conf; |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3561 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3562 | mbedtls_ssl_init(&ssl); |
| 3563 | mbedtls_ssl_config_init(&conf); |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 3564 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3565 | TEST_ASSERT(mbedtls_ssl_config_defaults(&conf, |
| 3566 | MBEDTLS_SSL_IS_CLIENT, |
| 3567 | MBEDTLS_SSL_TRANSPORT_DATAGRAM, |
| 3568 | MBEDTLS_SSL_PRESET_DEFAULT) == 0); |
| 3569 | TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0); |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3570 | |
| 3571 | /* Read previous record numbers */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3572 | for (len = 0; len < prevs->len; len += 6) { |
| 3573 | memcpy(ssl.in_ctr + 2, prevs->x + len, 6); |
| 3574 | mbedtls_ssl_dtls_replay_update(&ssl); |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3575 | } |
| 3576 | |
| 3577 | /* Check new number */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3578 | memcpy(ssl.in_ctr + 2, new->x, 6); |
| 3579 | TEST_ASSERT(mbedtls_ssl_dtls_replay_check(&ssl) == ret); |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3580 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3581 | mbedtls_ssl_free(&ssl); |
| 3582 | mbedtls_ssl_config_free(&conf); |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3583 | } |
| 3584 | /* END_CASE */ |
Hanno Becker | b25c0c7 | 2017-05-05 11:24:30 +0100 | [diff] [blame] | 3585 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 3586 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3587 | void ssl_set_hostname_twice(char *hostname0, char *hostname1) |
Hanno Becker | b25c0c7 | 2017-05-05 11:24:30 +0100 | [diff] [blame] | 3588 | { |
| 3589 | mbedtls_ssl_context ssl; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3590 | mbedtls_ssl_init(&ssl); |
Hanno Becker | b25c0c7 | 2017-05-05 11:24:30 +0100 | [diff] [blame] | 3591 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3592 | TEST_ASSERT(mbedtls_ssl_set_hostname(&ssl, hostname0) == 0); |
| 3593 | TEST_ASSERT(mbedtls_ssl_set_hostname(&ssl, hostname1) == 0); |
Hanno Becker | b25c0c7 | 2017-05-05 11:24:30 +0100 | [diff] [blame] | 3594 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3595 | mbedtls_ssl_free(&ssl); |
Hanno Becker | b25c0c7 | 2017-05-05 11:24:30 +0100 | [diff] [blame] | 3596 | } |
Darryl Green | 11999bb | 2018-03-13 15:22:58 +0000 | [diff] [blame] | 3597 | /* END_CASE */ |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3598 | |
| 3599 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3600 | void ssl_crypt_record(int cipher_type, int hash_id, |
| 3601 | int etm, int tag_mode, int ver, |
| 3602 | int cid0_len, int cid1_len) |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3603 | { |
| 3604 | /* |
| 3605 | * Test several record encryptions and decryptions |
| 3606 | * with plenty of space before and after the data |
| 3607 | * within the record buffer. |
| 3608 | */ |
| 3609 | |
| 3610 | int ret; |
| 3611 | int num_records = 16; |
| 3612 | mbedtls_ssl_context ssl; /* ONLY for debugging */ |
| 3613 | |
| 3614 | mbedtls_ssl_transform t0, t1; |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 3615 | unsigned char *buf = NULL; |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3616 | size_t const buflen = 512; |
| 3617 | mbedtls_record rec, rec_backup; |
| 3618 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3619 | USE_PSA_INIT(); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 3620 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3621 | mbedtls_ssl_init(&ssl); |
| 3622 | mbedtls_ssl_transform_init(&t0); |
| 3623 | mbedtls_ssl_transform_init(&t1); |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3624 | ret = mbedtls_test_ssl_build_transforms(&t0, &t1, cipher_type, hash_id, |
| 3625 | etm, tag_mode, ver, |
| 3626 | (size_t) cid0_len, |
| 3627 | (size_t) cid1_len); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 3628 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3629 | TEST_ASSERT(ret == 0); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3630 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3631 | TEST_ASSERT((buf = mbedtls_calloc(1, buflen)) != NULL); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3632 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3633 | while (num_records-- > 0) { |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3634 | mbedtls_ssl_transform *t_dec, *t_enc; |
| 3635 | /* Take turns in who's sending and who's receiving. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3636 | if (num_records % 3 == 0) { |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3637 | t_dec = &t0; |
| 3638 | t_enc = &t1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3639 | } else { |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3640 | t_dec = &t1; |
| 3641 | t_enc = &t0; |
| 3642 | } |
| 3643 | |
| 3644 | /* |
| 3645 | * The record header affects the transformation in two ways: |
| 3646 | * 1) It determines the AEAD additional data |
| 3647 | * 2) The record counter sometimes determines the IV. |
| 3648 | * |
| 3649 | * Apart from that, the fields don't have influence. |
| 3650 | * In particular, it is currently not the responsibility |
| 3651 | * of ssl_encrypt/decrypt_buf to check if the transform |
| 3652 | * version matches the record version, or that the |
| 3653 | * type is sensible. |
| 3654 | */ |
| 3655 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3656 | memset(rec.ctr, num_records, sizeof(rec.ctr)); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3657 | rec.type = 42; |
| 3658 | rec.ver[0] = num_records; |
| 3659 | rec.ver[1] = num_records; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3660 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 3661 | rec.cid_len = 0; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3662 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3663 | |
| 3664 | rec.buf = buf; |
| 3665 | rec.buf_len = buflen; |
| 3666 | rec.data_offset = 16; |
| 3667 | /* Make sure to vary the length to exercise different |
| 3668 | * paddings. */ |
| 3669 | rec.data_len = 1 + num_records; |
| 3670 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3671 | memset(rec.buf + rec.data_offset, 42, rec.data_len); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3672 | |
| 3673 | /* Make a copy for later comparison */ |
| 3674 | rec_backup = rec; |
| 3675 | |
| 3676 | /* Encrypt record */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3677 | ret = mbedtls_ssl_encrypt_buf(&ssl, t_enc, &rec, |
| 3678 | mbedtls_test_rnd_std_rand, NULL); |
| 3679 | TEST_ASSERT(ret == 0 || ret == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL); |
| 3680 | if (ret != 0) { |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3681 | continue; |
| 3682 | } |
| 3683 | |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 3684 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3685 | if (rec.cid_len != 0) { |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 3686 | /* DTLS 1.2 + CID hides the real content type and |
| 3687 | * uses a special CID content type in the protected |
| 3688 | * record. Double-check this. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3689 | TEST_ASSERT(rec.type == MBEDTLS_SSL_MSG_CID); |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 3690 | } |
| 3691 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 3692 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3693 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3694 | if (t_enc->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 3695 | /* TLS 1.3 hides the real content type and |
| 3696 | * always uses Application Data as the content type |
| 3697 | * for protected records. Double-check this. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3698 | TEST_ASSERT(rec.type == MBEDTLS_SSL_MSG_APPLICATION_DATA); |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 3699 | } |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3700 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 3701 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3702 | /* Decrypt record with t_dec */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3703 | ret = mbedtls_ssl_decrypt_buf(&ssl, t_dec, &rec); |
| 3704 | TEST_ASSERT(ret == 0); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3705 | |
| 3706 | /* Compare results */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3707 | TEST_ASSERT(rec.type == rec_backup.type); |
| 3708 | TEST_ASSERT(memcmp(rec.ctr, rec_backup.ctr, 8) == 0); |
| 3709 | TEST_ASSERT(rec.ver[0] == rec_backup.ver[0]); |
| 3710 | TEST_ASSERT(rec.ver[1] == rec_backup.ver[1]); |
| 3711 | TEST_ASSERT(rec.data_len == rec_backup.data_len); |
| 3712 | TEST_ASSERT(rec.data_offset == rec_backup.data_offset); |
| 3713 | TEST_ASSERT(memcmp(rec.buf + rec.data_offset, |
| 3714 | rec_backup.buf + rec_backup.data_offset, |
| 3715 | rec.data_len) == 0); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3716 | } |
| 3717 | |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 3718 | exit: |
| 3719 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3720 | /* Cleanup */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3721 | mbedtls_ssl_free(&ssl); |
| 3722 | mbedtls_ssl_transform_free(&t0); |
| 3723 | mbedtls_ssl_transform_free(&t1); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3724 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3725 | mbedtls_free(buf); |
| 3726 | USE_PSA_DONE(); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3727 | } |
| 3728 | /* END_CASE */ |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3729 | |
| 3730 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3731 | void ssl_crypt_record_small(int cipher_type, int hash_id, |
| 3732 | int etm, int tag_mode, int ver, |
| 3733 | int cid0_len, int cid1_len) |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3734 | { |
| 3735 | /* |
| 3736 | * Test pairs of encryption and decryption with an increasing |
| 3737 | * amount of space in the record buffer - in more detail: |
| 3738 | * 1) Try to encrypt with 0, 1, 2, ... bytes available |
| 3739 | * in front of the plaintext, and expect the encryption |
| 3740 | * to succeed starting from some offset. Always keep |
| 3741 | * enough space in the end of the buffer. |
| 3742 | * 2) Try to encrypt with 0, 1, 2, ... bytes available |
| 3743 | * at the end of the plaintext, and expect the encryption |
| 3744 | * to succeed starting from some offset. Always keep |
| 3745 | * enough space at the beginning of the buffer. |
| 3746 | * 3) Try to encrypt with 0, 1, 2, ... bytes available |
| 3747 | * both at the front and end of the plaintext, |
| 3748 | * and expect the encryption to succeed starting from |
| 3749 | * some offset. |
| 3750 | * |
| 3751 | * If encryption succeeds, check that decryption succeeds |
| 3752 | * and yields the original record. |
| 3753 | */ |
| 3754 | |
| 3755 | mbedtls_ssl_context ssl; /* ONLY for debugging */ |
| 3756 | |
| 3757 | mbedtls_ssl_transform t0, t1; |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 3758 | unsigned char *buf = NULL; |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 3759 | size_t const buflen = 256; |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3760 | mbedtls_record rec, rec_backup; |
| 3761 | |
| 3762 | int ret; |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 3763 | int mode; /* Mode 1, 2 or 3 as explained above */ |
| 3764 | size_t offset; /* Available space at beginning/end/both */ |
| 3765 | size_t threshold = 96; /* Maximum offset to test against */ |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3766 | |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 3767 | size_t default_pre_padding = 64; /* Pre-padding to use in mode 2 */ |
| 3768 | 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] | 3769 | |
| 3770 | int seen_success; /* Indicates if in the current mode we've |
| 3771 | * already seen a successful test. */ |
| 3772 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3773 | USE_PSA_INIT(); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 3774 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3775 | mbedtls_ssl_init(&ssl); |
| 3776 | mbedtls_ssl_transform_init(&t0); |
| 3777 | mbedtls_ssl_transform_init(&t1); |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3778 | ret = mbedtls_test_ssl_build_transforms(&t0, &t1, cipher_type, hash_id, |
| 3779 | etm, tag_mode, ver, |
| 3780 | (size_t) cid0_len, |
| 3781 | (size_t) cid1_len); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 3782 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3783 | TEST_ASSERT(ret == 0); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3784 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3785 | TEST_ASSERT((buf = mbedtls_calloc(1, buflen)) != NULL); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3786 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3787 | for (mode = 1; mode <= 3; mode++) { |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3788 | seen_success = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3789 | for (offset = 0; offset <= threshold; offset++) { |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3790 | mbedtls_ssl_transform *t_dec, *t_enc; |
Hanno Becker | 6c87b3f | 2019-04-29 17:24:44 +0100 | [diff] [blame] | 3791 | t_dec = &t0; |
| 3792 | t_enc = &t1; |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3793 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3794 | memset(rec.ctr, offset, sizeof(rec.ctr)); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3795 | rec.type = 42; |
| 3796 | rec.ver[0] = offset; |
| 3797 | rec.ver[1] = offset; |
| 3798 | rec.buf = buf; |
| 3799 | rec.buf_len = buflen; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3800 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 3801 | rec.cid_len = 0; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3802 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3803 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3804 | switch (mode) { |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3805 | case 1: /* Space in the beginning */ |
| 3806 | rec.data_offset = offset; |
| 3807 | rec.data_len = buflen - offset - default_post_padding; |
| 3808 | break; |
| 3809 | |
| 3810 | case 2: /* Space in the end */ |
| 3811 | rec.data_offset = default_pre_padding; |
| 3812 | rec.data_len = buflen - default_pre_padding - offset; |
| 3813 | break; |
| 3814 | |
| 3815 | case 3: /* Space in the beginning and end */ |
| 3816 | rec.data_offset = offset; |
| 3817 | rec.data_len = buflen - 2 * offset; |
| 3818 | break; |
| 3819 | |
| 3820 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3821 | TEST_ASSERT(0); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3822 | break; |
| 3823 | } |
| 3824 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3825 | memset(rec.buf + rec.data_offset, 42, rec.data_len); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3826 | |
| 3827 | /* Make a copy for later comparison */ |
| 3828 | rec_backup = rec; |
| 3829 | |
| 3830 | /* Encrypt record */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3831 | ret = mbedtls_ssl_encrypt_buf(&ssl, t_enc, &rec, |
| 3832 | mbedtls_test_rnd_std_rand, NULL); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3833 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3834 | if ((mode == 1 || mode == 2) && seen_success) { |
| 3835 | TEST_ASSERT(ret == 0); |
| 3836 | } else { |
| 3837 | TEST_ASSERT(ret == 0 || ret == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL); |
| 3838 | if (ret == 0) { |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3839 | seen_success = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3840 | } |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3841 | } |
| 3842 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3843 | if (ret != 0) { |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3844 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3845 | } |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3846 | |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 3847 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3848 | if (rec.cid_len != 0) { |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 3849 | /* DTLS 1.2 + CID hides the real content type and |
| 3850 | * uses a special CID content type in the protected |
| 3851 | * record. Double-check this. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3852 | TEST_ASSERT(rec.type == MBEDTLS_SSL_MSG_CID); |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 3853 | } |
| 3854 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 3855 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3856 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3857 | if (t_enc->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 3858 | /* TLS 1.3 hides the real content type and |
| 3859 | * always uses Application Data as the content type |
| 3860 | * for protected records. Double-check this. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3861 | TEST_ASSERT(rec.type == MBEDTLS_SSL_MSG_APPLICATION_DATA); |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 3862 | } |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3863 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 3864 | |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3865 | /* Decrypt record with t_dec */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3866 | TEST_ASSERT(mbedtls_ssl_decrypt_buf(&ssl, t_dec, &rec) == 0); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3867 | |
| 3868 | /* Compare results */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3869 | TEST_ASSERT(rec.type == rec_backup.type); |
| 3870 | TEST_ASSERT(memcmp(rec.ctr, rec_backup.ctr, 8) == 0); |
| 3871 | TEST_ASSERT(rec.ver[0] == rec_backup.ver[0]); |
| 3872 | TEST_ASSERT(rec.ver[1] == rec_backup.ver[1]); |
| 3873 | TEST_ASSERT(rec.data_len == rec_backup.data_len); |
| 3874 | TEST_ASSERT(rec.data_offset == rec_backup.data_offset); |
| 3875 | TEST_ASSERT(memcmp(rec.buf + rec.data_offset, |
| 3876 | rec_backup.buf + rec_backup.data_offset, |
| 3877 | rec.data_len) == 0); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3878 | } |
| 3879 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3880 | TEST_ASSERT(seen_success == 1); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3881 | } |
| 3882 | |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 3883 | exit: |
| 3884 | |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3885 | /* Cleanup */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3886 | mbedtls_ssl_free(&ssl); |
| 3887 | mbedtls_ssl_transform_free(&t0); |
| 3888 | mbedtls_ssl_transform_free(&t1); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3889 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3890 | mbedtls_free(buf); |
| 3891 | USE_PSA_DONE(); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3892 | } |
| 3893 | /* END_CASE */ |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 3894 | |
Przemyslaw Stekiel | f4facef | 2022-02-02 21:31:04 +0100 | [diff] [blame] | 3895 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC:MBEDTLS_AES_C:MBEDTLS_SSL_PROTO_TLS1_2 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3896 | void ssl_decrypt_non_etm_cbc(int cipher_type, int hash_id, int trunc_hmac, |
| 3897 | int length_selector) |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3898 | { |
| 3899 | /* |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3900 | * Test record decryption for CBC without EtM, focused on the verification |
| 3901 | * of padding and MAC. |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3902 | * |
TRodziewicz | 299510e | 2021-07-09 16:55:11 +0200 | [diff] [blame] | 3903 | * Actually depends on TLS 1.2 and either AES, ARIA or Camellia, but since |
| 3904 | * the test framework doesn't support alternation in dependency statements, |
| 3905 | * just depend on AES. |
Manuel Pégourié-Gonnard | 864abbf | 2020-07-21 10:37:14 +0200 | [diff] [blame] | 3906 | * |
| 3907 | * The length_selector argument is interpreted as follows: |
| 3908 | * - if it's -1, the plaintext length is 0 and minimal padding is applied |
| 3909 | * - if it's -2, the plaintext length is 0 and maximal padding is applied |
| 3910 | * - otherwise it must be in [0, 255] and is padding_length from RFC 5246: |
| 3911 | * it's the length of the rest of the padding, that is, excluding the |
| 3912 | * byte that encodes the length. The minimal non-zero plaintext length |
| 3913 | * that gives this padding_length is automatically selected. |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3914 | */ |
| 3915 | mbedtls_ssl_context ssl; /* ONLY for debugging */ |
| 3916 | mbedtls_ssl_transform t0, t1; |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3917 | mbedtls_record rec, rec_save; |
| 3918 | unsigned char *buf = NULL, *buf_save = NULL; |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3919 | size_t buflen, olen = 0; |
Manuel Pégourié-Gonnard | 864abbf | 2020-07-21 10:37:14 +0200 | [diff] [blame] | 3920 | size_t plaintext_len, block_size, i; |
Manuel Pégourié-Gonnard | e55653f | 2020-07-22 11:42:57 +0200 | [diff] [blame] | 3921 | unsigned char padlen; /* excluding the padding_length byte */ |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3922 | unsigned char add_data[13]; |
Neil Armstrong | cf8841a | 2022-02-24 11:17:45 +0100 | [diff] [blame] | 3923 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 3924 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 3925 | size_t sign_mac_length = 0; |
Andrzej Kurek | 8c95ac4 | 2022-08-17 16:17:00 -0400 | [diff] [blame] | 3926 | unsigned char mac[PSA_HASH_MAX_SIZE]; |
| 3927 | #else |
| 3928 | unsigned char mac[MBEDTLS_MD_MAX_SIZE]; |
Neil Armstrong | cf8841a | 2022-02-24 11:17:45 +0100 | [diff] [blame] | 3929 | #endif |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3930 | int exp_ret; |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 3931 | int ret; |
Manuel Pégourié-Gonnard | 4adc04a | 2020-07-16 10:00:48 +0200 | [diff] [blame] | 3932 | const unsigned char pad_max_len = 255; /* Per the standard */ |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3933 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3934 | USE_PSA_INIT(); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 3935 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3936 | mbedtls_ssl_init(&ssl); |
| 3937 | mbedtls_ssl_transform_init(&t0); |
| 3938 | mbedtls_ssl_transform_init(&t1); |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3939 | |
| 3940 | /* Set up transforms with dummy keys */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3941 | ret = mbedtls_test_ssl_build_transforms(&t0, &t1, cipher_type, hash_id, |
| 3942 | 0, trunc_hmac, |
| 3943 | MBEDTLS_SSL_VERSION_TLS1_2, |
| 3944 | 0, 0); |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3945 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3946 | TEST_ASSERT(ret == 0); |
Przemyslaw Stekiel | 4a36dd3 | 2022-01-25 00:43:58 +0100 | [diff] [blame] | 3947 | |
Manuel Pégourié-Gonnard | 864abbf | 2020-07-21 10:37:14 +0200 | [diff] [blame] | 3948 | /* Determine padding/plaintext length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3949 | TEST_ASSERT(length_selector >= -2 && length_selector <= 255); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3950 | block_size = t0.ivlen; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3951 | if (length_selector < 0) { |
Manuel Pégourié-Gonnard | 864abbf | 2020-07-21 10:37:14 +0200 | [diff] [blame] | 3952 | plaintext_len = 0; |
| 3953 | |
Manuel Pégourié-Gonnard | e55653f | 2020-07-22 11:42:57 +0200 | [diff] [blame] | 3954 | /* Minimal padding |
| 3955 | * The +1 is for the padding_length byte, not counted in padlen. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3956 | padlen = block_size - (t0.maclen + 1) % block_size; |
Manuel Pégourié-Gonnard | 864abbf | 2020-07-21 10:37:14 +0200 | [diff] [blame] | 3957 | |
| 3958 | /* Maximal padding? */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3959 | if (length_selector == -2) { |
| 3960 | padlen += block_size * ((pad_max_len - padlen) / block_size); |
| 3961 | } |
| 3962 | } else { |
Manuel Pégourié-Gonnard | 864abbf | 2020-07-21 10:37:14 +0200 | [diff] [blame] | 3963 | padlen = length_selector; |
| 3964 | |
Manuel Pégourié-Gonnard | e55653f | 2020-07-22 11:42:57 +0200 | [diff] [blame] | 3965 | /* Minimal non-zero plaintext_length giving desired padding. |
| 3966 | * The +1 is for the padding_length byte, not counted in padlen. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3967 | plaintext_len = block_size - (padlen + t0.maclen + 1) % block_size; |
Manuel Pégourié-Gonnard | 864abbf | 2020-07-21 10:37:14 +0200 | [diff] [blame] | 3968 | } |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3969 | |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3970 | /* Prepare a buffer for record data */ |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3971 | buflen = block_size |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3972 | + plaintext_len |
| 3973 | + t0.maclen |
| 3974 | + padlen + 1; |
| 3975 | ASSERT_ALLOC(buf, buflen); |
| 3976 | ASSERT_ALLOC(buf_save, buflen); |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3977 | |
| 3978 | /* Prepare a dummy record header */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3979 | memset(rec.ctr, 0, sizeof(rec.ctr)); |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3980 | rec.type = MBEDTLS_SSL_MSG_APPLICATION_DATA; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3981 | mbedtls_ssl_write_version(rec.ver, MBEDTLS_SSL_TRANSPORT_STREAM, |
| 3982 | MBEDTLS_SSL_VERSION_TLS1_2); |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3983 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 3984 | rec.cid_len = 0; |
| 3985 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 3986 | |
| 3987 | /* Prepare dummy record content */ |
| 3988 | rec.buf = buf; |
| 3989 | rec.buf_len = buflen; |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3990 | rec.data_offset = block_size; |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3991 | rec.data_len = plaintext_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3992 | memset(rec.buf + rec.data_offset, 42, rec.data_len); |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3993 | |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3994 | /* Serialized version of record header for MAC purposes */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3995 | memcpy(add_data, rec.ctr, 8); |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3996 | add_data[8] = rec.type; |
| 3997 | add_data[9] = rec.ver[0]; |
| 3998 | add_data[10] = rec.ver[1]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3999 | add_data[11] = (rec.data_len >> 8) & 0xff; |
| 4000 | add_data[12] = (rec.data_len >> 0) & 0xff; |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 4001 | |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 4002 | /* Set dummy IV */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4003 | memset(t0.iv_enc, 0x55, t0.ivlen); |
| 4004 | memcpy(rec.buf, t0.iv_enc, t0.ivlen); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 4005 | |
| 4006 | /* |
| 4007 | * Prepare a pre-encryption record (with MAC and padding), and save it. |
| 4008 | */ |
| 4009 | |
| 4010 | /* MAC with additional data */ |
Neil Armstrong | cf8841a | 2022-02-24 11:17:45 +0100 | [diff] [blame] | 4011 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4012 | TEST_EQUAL(PSA_SUCCESS, psa_mac_sign_setup(&operation, |
| 4013 | t0.psa_mac_enc, |
| 4014 | t0.psa_mac_alg)); |
| 4015 | TEST_EQUAL(PSA_SUCCESS, psa_mac_update(&operation, add_data, 13)); |
| 4016 | TEST_EQUAL(PSA_SUCCESS, psa_mac_update(&operation, |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 4017 | rec.buf + rec.data_offset, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4018 | rec.data_len)); |
| 4019 | TEST_EQUAL(PSA_SUCCESS, psa_mac_sign_finish(&operation, |
| 4020 | mac, sizeof(mac), |
| 4021 | &sign_mac_length)); |
| 4022 | #else |
| 4023 | TEST_EQUAL(0, mbedtls_md_hmac_update(&t0.md_ctx_enc, add_data, 13)); |
| 4024 | TEST_EQUAL(0, mbedtls_md_hmac_update(&t0.md_ctx_enc, |
| 4025 | rec.buf + rec.data_offset, |
| 4026 | rec.data_len)); |
| 4027 | TEST_EQUAL(0, mbedtls_md_hmac_finish(&t0.md_ctx_enc, mac)); |
Neil Armstrong | cf8841a | 2022-02-24 11:17:45 +0100 | [diff] [blame] | 4028 | #endif |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 4029 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4030 | memcpy(rec.buf + rec.data_offset + rec.data_len, mac, t0.maclen); |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 4031 | rec.data_len += t0.maclen; |
| 4032 | |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 4033 | /* Pad */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4034 | memset(rec.buf + rec.data_offset + rec.data_len, padlen, padlen + 1); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 4035 | rec.data_len += padlen + 1; |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 4036 | |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 4037 | /* Save correct pre-encryption record */ |
| 4038 | rec_save = rec; |
| 4039 | rec_save.buf = buf_save; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4040 | memcpy(buf_save, buf, buflen); |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 4041 | |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 4042 | /* |
| 4043 | * Encrypt and decrypt the correct record, expecting success |
| 4044 | */ |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 4045 | TEST_EQUAL(0, mbedtls_test_psa_cipher_encrypt_helper( |
| 4046 | &t0, t0.iv_enc, t0.ivlen, rec.buf + rec.data_offset, |
| 4047 | rec.data_len, rec.buf + rec.data_offset, &olen)); |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 4048 | rec.data_offset -= t0.ivlen; |
| 4049 | rec.data_len += t0.ivlen; |
| 4050 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4051 | TEST_EQUAL(0, mbedtls_ssl_decrypt_buf(&ssl, &t1, &rec)); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 4052 | |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 4053 | /* |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 4054 | * Modify each byte of the pre-encryption record before encrypting and |
| 4055 | * decrypting it, expecting failure every time. |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 4056 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4057 | for (i = block_size; i < buflen; i++) { |
| 4058 | mbedtls_test_set_step(i); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 4059 | |
| 4060 | /* Restore correct pre-encryption record */ |
| 4061 | rec = rec_save; |
| 4062 | rec.buf = buf; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4063 | memcpy(buf, buf_save, buflen); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 4064 | |
Manuel Pégourié-Gonnard | b51f044 | 2020-07-21 10:40:25 +0200 | [diff] [blame] | 4065 | /* 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] | 4066 | rec.buf[i] ^= 0x01; |
| 4067 | |
| 4068 | /* Encrypt */ |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 4069 | TEST_EQUAL(0, mbedtls_test_psa_cipher_encrypt_helper( |
| 4070 | &t0, t0.iv_enc, t0.ivlen, rec.buf + rec.data_offset, |
| 4071 | rec.data_len, rec.buf + rec.data_offset, &olen)); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 4072 | rec.data_offset -= t0.ivlen; |
| 4073 | rec.data_len += t0.ivlen; |
| 4074 | |
| 4075 | /* Decrypt and expect failure */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4076 | TEST_EQUAL(MBEDTLS_ERR_SSL_INVALID_MAC, |
| 4077 | mbedtls_ssl_decrypt_buf(&ssl, &t1, &rec)); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 4078 | } |
| 4079 | |
| 4080 | /* |
| 4081 | * Use larger values of the padding bytes - with small buffers, this tests |
| 4082 | * the case where the announced padlen would be larger than the buffer |
| 4083 | * (and before that, than the buffer minus the size of the MAC), to make |
| 4084 | * sure our padding checking code does not perform any out-of-bounds reads |
| 4085 | * in this case. (With larger buffers, ie when the plaintext is long or |
| 4086 | * maximal length padding is used, this is less relevant but still doesn't |
| 4087 | * hurt to test.) |
| 4088 | * |
| 4089 | * (Start the loop with correct padding, just to double-check that record |
| 4090 | * saving did work, and that we're overwriting the correct bytes.) |
| 4091 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4092 | for (i = padlen; i <= pad_max_len; i++) { |
| 4093 | mbedtls_test_set_step(i); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 4094 | |
| 4095 | /* Restore correct pre-encryption record */ |
| 4096 | rec = rec_save; |
| 4097 | rec.buf = buf; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4098 | memcpy(buf, buf_save, buflen); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 4099 | |
| 4100 | /* Set padding bytes to new value */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4101 | memset(buf + buflen - padlen - 1, i, padlen + 1); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 4102 | |
| 4103 | /* Encrypt */ |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 4104 | TEST_EQUAL(0, mbedtls_test_psa_cipher_encrypt_helper( |
| 4105 | &t0, t0.iv_enc, t0.ivlen, rec.buf + rec.data_offset, |
| 4106 | rec.data_len, rec.buf + rec.data_offset, &olen)); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 4107 | rec.data_offset -= t0.ivlen; |
| 4108 | rec.data_len += t0.ivlen; |
| 4109 | |
| 4110 | /* Decrypt and expect failure except the first time */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4111 | exp_ret = (i == padlen) ? 0 : MBEDTLS_ERR_SSL_INVALID_MAC; |
| 4112 | TEST_EQUAL(exp_ret, mbedtls_ssl_decrypt_buf(&ssl, &t1, &rec)); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 4113 | } |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 4114 | |
| 4115 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4116 | mbedtls_ssl_free(&ssl); |
| 4117 | mbedtls_ssl_transform_free(&t0); |
| 4118 | mbedtls_ssl_transform_free(&t1); |
| 4119 | mbedtls_free(buf); |
| 4120 | mbedtls_free(buf_save); |
| 4121 | USE_PSA_DONE(); |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 4122 | } |
| 4123 | /* END_CASE */ |
| 4124 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4125 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4126 | void ssl_tls13_hkdf_expand_label(int hash_alg, |
| 4127 | data_t *secret, |
| 4128 | int label_idx, |
| 4129 | data_t *ctx, |
| 4130 | int desired_length, |
| 4131 | data_t *expected) |
Hanno Becker | 39ff492 | 2020-08-21 13:36:56 +0100 | [diff] [blame] | 4132 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4133 | unsigned char dst[100]; |
Hanno Becker | 39ff492 | 2020-08-21 13:36:56 +0100 | [diff] [blame] | 4134 | |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 4135 | unsigned char const *lbl = NULL; |
| 4136 | size_t lbl_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4137 | #define MBEDTLS_SSL_TLS1_3_LABEL(name, string) \ |
| 4138 | if (label_idx == (int) tls13_label_ ## name) \ |
Xiaofei Bai | d25fab6 | 2021-12-02 06:36:27 +0000 | [diff] [blame] | 4139 | { \ |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4140 | lbl = mbedtls_ssl_tls13_labels.name; \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4141 | lbl_len = sizeof(mbedtls_ssl_tls13_labels.name); \ |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 4142 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4143 | MBEDTLS_SSL_TLS1_3_LABEL_LIST |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 4144 | #undef MBEDTLS_SSL_TLS1_3_LABEL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4145 | TEST_ASSERT(lbl != NULL); |
Hanno Becker | 39ff492 | 2020-08-21 13:36:56 +0100 | [diff] [blame] | 4146 | |
| 4147 | /* Check sanity of test parameters. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4148 | TEST_ASSERT((size_t) desired_length <= sizeof(dst)); |
| 4149 | TEST_ASSERT((size_t) desired_length == expected->len); |
Hanno Becker | 39ff492 | 2020-08-21 13:36:56 +0100 | [diff] [blame] | 4150 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4151 | PSA_INIT(); |
Gabor Mezei | 892c4aa | 2022-03-21 12:21:43 +0100 | [diff] [blame] | 4152 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4153 | TEST_ASSERT(mbedtls_ssl_tls13_hkdf_expand_label( |
| 4154 | (psa_algorithm_t) hash_alg, |
| 4155 | secret->x, secret->len, |
| 4156 | lbl, lbl_len, |
| 4157 | ctx->x, ctx->len, |
| 4158 | dst, desired_length) == 0); |
Hanno Becker | 39ff492 | 2020-08-21 13:36:56 +0100 | [diff] [blame] | 4159 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4160 | ASSERT_COMPARE(dst, (size_t) desired_length, |
| 4161 | expected->x, (size_t) expected->len); |
Gabor Mezei | 892c4aa | 2022-03-21 12:21:43 +0100 | [diff] [blame] | 4162 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4163 | PSA_DONE(); |
Hanno Becker | 39ff492 | 2020-08-21 13:36:56 +0100 | [diff] [blame] | 4164 | } |
| 4165 | /* END_CASE */ |
| 4166 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4167 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4168 | void ssl_tls13_traffic_key_generation(int hash_alg, |
| 4169 | data_t *server_secret, |
| 4170 | data_t *client_secret, |
| 4171 | int desired_iv_len, |
| 4172 | int desired_key_len, |
| 4173 | data_t *expected_server_write_key, |
| 4174 | data_t *expected_server_write_iv, |
| 4175 | data_t *expected_client_write_key, |
| 4176 | data_t *expected_client_write_iv) |
Hanno Becker | 19498f8 | 2020-08-21 13:37:08 +0100 | [diff] [blame] | 4177 | { |
| 4178 | mbedtls_ssl_key_set keys; |
| 4179 | |
| 4180 | /* Check sanity of test parameters. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4181 | TEST_ASSERT(client_secret->len == server_secret->len); |
Yanray Wang | d577a68 | 2022-10-27 11:47:54 +0800 | [diff] [blame^] | 4182 | TEST_ASSERT( |
| 4183 | expected_client_write_iv->len == expected_server_write_iv->len && |
| 4184 | expected_client_write_iv->len == (size_t) desired_iv_len); |
| 4185 | TEST_ASSERT( |
| 4186 | expected_client_write_key->len == expected_server_write_key->len && |
| 4187 | expected_client_write_key->len == (size_t) desired_key_len); |
Hanno Becker | 19498f8 | 2020-08-21 13:37:08 +0100 | [diff] [blame] | 4188 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4189 | PSA_INIT(); |
Gabor Mezei | 892c4aa | 2022-03-21 12:21:43 +0100 | [diff] [blame] | 4190 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4191 | TEST_ASSERT(mbedtls_ssl_tls13_make_traffic_keys( |
| 4192 | (psa_algorithm_t) hash_alg, |
| 4193 | client_secret->x, |
| 4194 | server_secret->x, |
| 4195 | client_secret->len /* == server_secret->len */, |
| 4196 | desired_key_len, desired_iv_len, |
| 4197 | &keys) == 0); |
Hanno Becker | 19498f8 | 2020-08-21 13:37:08 +0100 | [diff] [blame] | 4198 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4199 | ASSERT_COMPARE(keys.client_write_key, |
| 4200 | keys.key_len, |
| 4201 | expected_client_write_key->x, |
| 4202 | (size_t) desired_key_len); |
| 4203 | ASSERT_COMPARE(keys.server_write_key, |
| 4204 | keys.key_len, |
| 4205 | expected_server_write_key->x, |
| 4206 | (size_t) desired_key_len); |
| 4207 | ASSERT_COMPARE(keys.client_write_iv, |
| 4208 | keys.iv_len, |
| 4209 | expected_client_write_iv->x, |
| 4210 | (size_t) desired_iv_len); |
| 4211 | ASSERT_COMPARE(keys.server_write_iv, |
| 4212 | keys.iv_len, |
| 4213 | expected_server_write_iv->x, |
| 4214 | (size_t) desired_iv_len); |
Gabor Mezei | 892c4aa | 2022-03-21 12:21:43 +0100 | [diff] [blame] | 4215 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4216 | PSA_DONE(); |
Hanno Becker | 19498f8 | 2020-08-21 13:37:08 +0100 | [diff] [blame] | 4217 | } |
| 4218 | /* END_CASE */ |
| 4219 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4220 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4221 | void ssl_tls13_derive_secret(int hash_alg, |
| 4222 | data_t *secret, |
| 4223 | int label_idx, |
| 4224 | data_t *ctx, |
| 4225 | int desired_length, |
| 4226 | int already_hashed, |
| 4227 | data_t *expected) |
Hanno Becker | e4849d1 | 2020-08-21 14:14:14 +0100 | [diff] [blame] | 4228 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4229 | unsigned char dst[100]; |
Hanno Becker | e4849d1 | 2020-08-21 14:14:14 +0100 | [diff] [blame] | 4230 | |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 4231 | unsigned char const *lbl = NULL; |
| 4232 | size_t lbl_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4233 | #define MBEDTLS_SSL_TLS1_3_LABEL(name, string) \ |
| 4234 | if (label_idx == (int) tls13_label_ ## name) \ |
Xiaofei Bai | d25fab6 | 2021-12-02 06:36:27 +0000 | [diff] [blame] | 4235 | { \ |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4236 | lbl = mbedtls_ssl_tls13_labels.name; \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4237 | lbl_len = sizeof(mbedtls_ssl_tls13_labels.name); \ |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 4238 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4239 | MBEDTLS_SSL_TLS1_3_LABEL_LIST |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 4240 | #undef MBEDTLS_SSL_TLS1_3_LABEL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4241 | TEST_ASSERT(lbl != NULL); |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 4242 | |
Hanno Becker | e4849d1 | 2020-08-21 14:14:14 +0100 | [diff] [blame] | 4243 | /* Check sanity of test parameters. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4244 | TEST_ASSERT((size_t) desired_length <= sizeof(dst)); |
| 4245 | TEST_ASSERT((size_t) desired_length == expected->len); |
Hanno Becker | e4849d1 | 2020-08-21 14:14:14 +0100 | [diff] [blame] | 4246 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4247 | PSA_INIT(); |
Gabor Mezei | 892c4aa | 2022-03-21 12:21:43 +0100 | [diff] [blame] | 4248 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4249 | TEST_ASSERT(mbedtls_ssl_tls13_derive_secret( |
| 4250 | (psa_algorithm_t) hash_alg, |
| 4251 | secret->x, secret->len, |
| 4252 | lbl, lbl_len, |
| 4253 | ctx->x, ctx->len, |
| 4254 | already_hashed, |
| 4255 | dst, desired_length) == 0); |
Hanno Becker | e4849d1 | 2020-08-21 14:14:14 +0100 | [diff] [blame] | 4256 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4257 | ASSERT_COMPARE(dst, desired_length, |
| 4258 | expected->x, desired_length); |
Gabor Mezei | 892c4aa | 2022-03-21 12:21:43 +0100 | [diff] [blame] | 4259 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4260 | PSA_DONE(); |
Hanno Becker | e4849d1 | 2020-08-21 14:14:14 +0100 | [diff] [blame] | 4261 | } |
| 4262 | /* END_CASE */ |
| 4263 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4264 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4265 | void ssl_tls13_derive_early_secrets(int hash_alg, |
| 4266 | data_t *secret, |
| 4267 | data_t *transcript, |
| 4268 | data_t *traffic_expected, |
| 4269 | data_t *exporter_expected) |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4270 | { |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4271 | mbedtls_ssl_tls13_early_secrets secrets; |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4272 | |
| 4273 | /* Double-check that we've passed sane parameters. */ |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 4274 | psa_algorithm_t alg = (psa_algorithm_t) hash_alg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4275 | size_t const hash_len = PSA_HASH_LENGTH(alg); |
| 4276 | TEST_ASSERT(PSA_ALG_IS_HASH(alg) && |
| 4277 | secret->len == hash_len && |
| 4278 | transcript->len == hash_len && |
| 4279 | traffic_expected->len == hash_len && |
| 4280 | exporter_expected->len == hash_len); |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4281 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4282 | PSA_INIT(); |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4283 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4284 | TEST_ASSERT(mbedtls_ssl_tls13_derive_early_secrets( |
| 4285 | alg, secret->x, transcript->x, transcript->len, |
| 4286 | &secrets) == 0); |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4287 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4288 | ASSERT_COMPARE(secrets.client_early_traffic_secret, hash_len, |
| 4289 | traffic_expected->x, traffic_expected->len); |
| 4290 | ASSERT_COMPARE(secrets.early_exporter_master_secret, hash_len, |
| 4291 | exporter_expected->x, exporter_expected->len); |
Gabor Mezei | 892c4aa | 2022-03-21 12:21:43 +0100 | [diff] [blame] | 4292 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4293 | PSA_DONE(); |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4294 | } |
| 4295 | /* END_CASE */ |
| 4296 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4297 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4298 | void ssl_tls13_derive_handshake_secrets(int hash_alg, |
| 4299 | data_t *secret, |
| 4300 | data_t *transcript, |
| 4301 | data_t *client_expected, |
| 4302 | data_t *server_expected) |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4303 | { |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4304 | mbedtls_ssl_tls13_handshake_secrets secrets; |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4305 | |
| 4306 | /* Double-check that we've passed sane parameters. */ |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 4307 | psa_algorithm_t alg = (psa_algorithm_t) hash_alg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4308 | size_t const hash_len = PSA_HASH_LENGTH(alg); |
| 4309 | TEST_ASSERT(PSA_ALG_IS_HASH(alg) && |
| 4310 | secret->len == hash_len && |
| 4311 | transcript->len == hash_len && |
| 4312 | client_expected->len == hash_len && |
| 4313 | server_expected->len == hash_len); |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4314 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4315 | PSA_INIT(); |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4316 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4317 | TEST_ASSERT(mbedtls_ssl_tls13_derive_handshake_secrets( |
| 4318 | alg, secret->x, transcript->x, transcript->len, |
| 4319 | &secrets) == 0); |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4320 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4321 | ASSERT_COMPARE(secrets.client_handshake_traffic_secret, hash_len, |
| 4322 | client_expected->x, client_expected->len); |
| 4323 | ASSERT_COMPARE(secrets.server_handshake_traffic_secret, hash_len, |
| 4324 | server_expected->x, server_expected->len); |
Gabor Mezei | 892c4aa | 2022-03-21 12:21:43 +0100 | [diff] [blame] | 4325 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4326 | PSA_DONE(); |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4327 | } |
| 4328 | /* END_CASE */ |
| 4329 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4330 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4331 | void ssl_tls13_derive_application_secrets(int hash_alg, |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4332 | data_t *secret, |
| 4333 | data_t *transcript, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4334 | data_t *client_expected, |
| 4335 | data_t *server_expected, |
| 4336 | data_t *exporter_expected) |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4337 | { |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4338 | mbedtls_ssl_tls13_application_secrets secrets; |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4339 | |
| 4340 | /* Double-check that we've passed sane parameters. */ |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 4341 | psa_algorithm_t alg = (psa_algorithm_t) hash_alg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4342 | size_t const hash_len = PSA_HASH_LENGTH(alg); |
| 4343 | TEST_ASSERT(PSA_ALG_IS_HASH(alg) && |
| 4344 | secret->len == hash_len && |
| 4345 | transcript->len == hash_len && |
| 4346 | client_expected->len == hash_len && |
| 4347 | server_expected->len == hash_len && |
| 4348 | exporter_expected->len == hash_len); |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4349 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4350 | PSA_INIT(); |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4351 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4352 | TEST_ASSERT(mbedtls_ssl_tls13_derive_application_secrets( |
| 4353 | alg, secret->x, transcript->x, transcript->len, |
| 4354 | &secrets) == 0); |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4355 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4356 | ASSERT_COMPARE(secrets.client_application_traffic_secret_N, hash_len, |
| 4357 | client_expected->x, client_expected->len); |
| 4358 | ASSERT_COMPARE(secrets.server_application_traffic_secret_N, hash_len, |
| 4359 | server_expected->x, server_expected->len); |
| 4360 | ASSERT_COMPARE(secrets.exporter_master_secret, hash_len, |
| 4361 | exporter_expected->x, exporter_expected->len); |
Gabor Mezei | 892c4aa | 2022-03-21 12:21:43 +0100 | [diff] [blame] | 4362 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4363 | PSA_DONE(); |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4364 | } |
| 4365 | /* END_CASE */ |
| 4366 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4367 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4368 | void ssl_tls13_derive_resumption_secrets(int hash_alg, |
| 4369 | data_t *secret, |
| 4370 | data_t *transcript, |
| 4371 | data_t *resumption_expected) |
Hanno Becker | 55bc2c5 | 2021-05-24 06:53:52 +0100 | [diff] [blame] | 4372 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4373 | mbedtls_ssl_tls13_application_secrets secrets; |
Hanno Becker | 55bc2c5 | 2021-05-24 06:53:52 +0100 | [diff] [blame] | 4374 | |
| 4375 | /* Double-check that we've passed sane parameters. */ |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 4376 | psa_algorithm_t alg = (psa_algorithm_t) hash_alg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4377 | size_t const hash_len = PSA_HASH_LENGTH(alg); |
| 4378 | TEST_ASSERT(PSA_ALG_IS_HASH(alg) && |
| 4379 | secret->len == hash_len && |
| 4380 | transcript->len == hash_len && |
| 4381 | resumption_expected->len == hash_len); |
Hanno Becker | 55bc2c5 | 2021-05-24 06:53:52 +0100 | [diff] [blame] | 4382 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4383 | PSA_INIT(); |
Hanno Becker | 55bc2c5 | 2021-05-24 06:53:52 +0100 | [diff] [blame] | 4384 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4385 | TEST_ASSERT(mbedtls_ssl_tls13_derive_resumption_master_secret( |
| 4386 | alg, secret->x, transcript->x, transcript->len, |
| 4387 | &secrets) == 0); |
Hanno Becker | 55bc2c5 | 2021-05-24 06:53:52 +0100 | [diff] [blame] | 4388 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4389 | ASSERT_COMPARE(secrets.resumption_master_secret, hash_len, |
| 4390 | resumption_expected->x, resumption_expected->len); |
Gabor Mezei | 892c4aa | 2022-03-21 12:21:43 +0100 | [diff] [blame] | 4391 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4392 | PSA_DONE(); |
| 4393 | } |
| 4394 | /* END_CASE */ |
| 4395 | |
| 4396 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 4397 | void ssl_tls13_create_psk_binder(int hash_alg, |
| 4398 | data_t *psk, |
| 4399 | int psk_type, |
| 4400 | data_t *transcript, |
| 4401 | data_t *binder_expected) |
| 4402 | { |
| 4403 | unsigned char binder[MBEDTLS_HASH_MAX_SIZE]; |
| 4404 | |
| 4405 | /* Double-check that we've passed sane parameters. */ |
| 4406 | psa_algorithm_t alg = (psa_algorithm_t) hash_alg; |
| 4407 | size_t const hash_len = PSA_HASH_LENGTH(alg); |
| 4408 | TEST_ASSERT(PSA_ALG_IS_HASH(alg) && |
| 4409 | transcript->len == hash_len && |
| 4410 | binder_expected->len == hash_len); |
| 4411 | |
| 4412 | PSA_INIT(); |
| 4413 | |
| 4414 | TEST_ASSERT(mbedtls_ssl_tls13_create_psk_binder( |
| 4415 | NULL, /* SSL context for debugging only */ |
| 4416 | alg, |
| 4417 | psk->x, psk->len, |
| 4418 | psk_type, |
| 4419 | transcript->x, |
| 4420 | binder) == 0); |
| 4421 | |
| 4422 | ASSERT_COMPARE(binder, hash_len, |
| 4423 | binder_expected->x, binder_expected->len); |
| 4424 | |
| 4425 | PSA_DONE(); |
Hanno Becker | 55bc2c5 | 2021-05-24 06:53:52 +0100 | [diff] [blame] | 4426 | } |
| 4427 | /* END_CASE */ |
| 4428 | |
Andrzej Kurek | daf43fb | 2022-10-12 10:46:42 -0400 | [diff] [blame] | 4429 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3:MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4430 | void ssl_tls13_record_protection(int ciphersuite, |
| 4431 | int endpoint, |
| 4432 | int ctr, |
| 4433 | int padding_used, |
| 4434 | data_t *server_write_key, |
| 4435 | data_t *server_write_iv, |
| 4436 | data_t *client_write_key, |
| 4437 | data_t *client_write_iv, |
| 4438 | data_t *plaintext, |
| 4439 | data_t *ciphertext) |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4440 | { |
| 4441 | mbedtls_ssl_key_set keys; |
| 4442 | mbedtls_ssl_transform transform_send; |
| 4443 | mbedtls_ssl_transform transform_recv; |
| 4444 | mbedtls_record rec; |
| 4445 | unsigned char *buf = NULL; |
Hanno Becker | 1f91878 | 2021-08-01 19:18:28 +0100 | [diff] [blame] | 4446 | size_t buf_len; |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4447 | int other_endpoint; |
| 4448 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4449 | USE_PSA_INIT(); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 4450 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4451 | TEST_ASSERT(endpoint == MBEDTLS_SSL_IS_CLIENT || |
| 4452 | endpoint == MBEDTLS_SSL_IS_SERVER); |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4453 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4454 | if (endpoint == MBEDTLS_SSL_IS_SERVER) { |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4455 | other_endpoint = MBEDTLS_SSL_IS_CLIENT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4456 | } |
| 4457 | if (endpoint == MBEDTLS_SSL_IS_CLIENT) { |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4458 | other_endpoint = MBEDTLS_SSL_IS_SERVER; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4459 | } |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4460 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4461 | TEST_ASSERT(server_write_key->len == client_write_key->len); |
| 4462 | TEST_ASSERT(server_write_iv->len == client_write_iv->len); |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4463 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4464 | memcpy(keys.client_write_key, |
| 4465 | client_write_key->x, client_write_key->len); |
| 4466 | memcpy(keys.client_write_iv, |
| 4467 | client_write_iv->x, client_write_iv->len); |
| 4468 | memcpy(keys.server_write_key, |
| 4469 | server_write_key->x, server_write_key->len); |
| 4470 | memcpy(keys.server_write_iv, |
| 4471 | server_write_iv->x, server_write_iv->len); |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4472 | |
| 4473 | keys.key_len = server_write_key->len; |
| 4474 | keys.iv_len = server_write_iv->len; |
| 4475 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4476 | mbedtls_ssl_transform_init(&transform_recv); |
| 4477 | mbedtls_ssl_transform_init(&transform_send); |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4478 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4479 | TEST_ASSERT(mbedtls_ssl_tls13_populate_transform( |
| 4480 | &transform_send, endpoint, |
| 4481 | ciphersuite, &keys, NULL) == 0); |
| 4482 | TEST_ASSERT(mbedtls_ssl_tls13_populate_transform( |
| 4483 | &transform_recv, other_endpoint, |
| 4484 | ciphersuite, &keys, NULL) == 0); |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4485 | |
Hanno Becker | 1f91878 | 2021-08-01 19:18:28 +0100 | [diff] [blame] | 4486 | /* Make sure we have enough space in the buffer even if |
| 4487 | * we use more padding than the KAT. */ |
| 4488 | buf_len = ciphertext->len + MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4489 | ASSERT_ALLOC(buf, buf_len); |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4490 | rec.type = MBEDTLS_SSL_MSG_APPLICATION_DATA; |
Hanno Becker | 4153745 | 2021-04-20 05:35:28 +0100 | [diff] [blame] | 4491 | |
| 4492 | /* TLS 1.3 uses the version identifier from TLS 1.2 on the wire. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4493 | mbedtls_ssl_write_version(rec.ver, |
| 4494 | MBEDTLS_SSL_TRANSPORT_STREAM, |
| 4495 | MBEDTLS_SSL_VERSION_TLS1_2); |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4496 | |
| 4497 | /* Copy plaintext into record structure */ |
| 4498 | rec.buf = buf; |
Hanno Becker | 1f91878 | 2021-08-01 19:18:28 +0100 | [diff] [blame] | 4499 | rec.buf_len = buf_len; |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4500 | rec.data_offset = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4501 | TEST_ASSERT(plaintext->len <= ciphertext->len); |
| 4502 | memcpy(rec.buf + rec.data_offset, plaintext->x, plaintext->len); |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4503 | rec.data_len = plaintext->len; |
| 4504 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 4505 | rec.cid_len = 0; |
| 4506 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 4507 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4508 | memset(&rec.ctr[0], 0, 8); |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4509 | rec.ctr[7] = ctr; |
| 4510 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4511 | TEST_ASSERT(mbedtls_ssl_encrypt_buf(NULL, &transform_send, &rec, |
| 4512 | NULL, NULL) == 0); |
Hanno Becker | 1f91878 | 2021-08-01 19:18:28 +0100 | [diff] [blame] | 4513 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4514 | if (padding_used == MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY) { |
| 4515 | ASSERT_COMPARE(rec.buf + rec.data_offset, rec.data_len, |
| 4516 | ciphertext->x, ciphertext->len); |
Hanno Becker | 1f91878 | 2021-08-01 19:18:28 +0100 | [diff] [blame] | 4517 | } |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4518 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4519 | TEST_ASSERT(mbedtls_ssl_decrypt_buf(NULL, &transform_recv, &rec) == 0); |
| 4520 | ASSERT_COMPARE(rec.buf + rec.data_offset, rec.data_len, |
| 4521 | plaintext->x, plaintext->len); |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4522 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4523 | mbedtls_free(buf); |
| 4524 | mbedtls_ssl_transform_free(&transform_send); |
| 4525 | mbedtls_ssl_transform_free(&transform_recv); |
| 4526 | USE_PSA_DONE(); |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4527 | } |
| 4528 | /* END_CASE */ |
| 4529 | |
Andrzej Kurek | 658442f | 2022-10-12 11:28:41 -0400 | [diff] [blame] | 4530 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4531 | void ssl_tls13_key_evolution(int hash_alg, |
| 4532 | data_t *secret, |
| 4533 | data_t *input, |
| 4534 | data_t *expected) |
Hanno Becker | 2d2c3eb | 2020-08-20 14:54:24 +0100 | [diff] [blame] | 4535 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4536 | unsigned char secret_new[MBEDTLS_HASH_MAX_SIZE]; |
Hanno Becker | 2d2c3eb | 2020-08-20 14:54:24 +0100 | [diff] [blame] | 4537 | |
Gabor Mezei | 5d9a1fe | 2022-03-24 17:49:14 +0100 | [diff] [blame] | 4538 | PSA_INIT(); |
Gabor Mezei | 892c4aa | 2022-03-21 12:21:43 +0100 | [diff] [blame] | 4539 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4540 | TEST_ASSERT(mbedtls_ssl_tls13_evolve_secret( |
| 4541 | (psa_algorithm_t) hash_alg, |
| 4542 | secret->len ? secret->x : NULL, |
| 4543 | input->len ? input->x : NULL, input->len, |
| 4544 | secret_new) == 0); |
Hanno Becker | 2d2c3eb | 2020-08-20 14:54:24 +0100 | [diff] [blame] | 4545 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4546 | ASSERT_COMPARE(secret_new, (size_t) expected->len, |
| 4547 | expected->x, (size_t) expected->len); |
Gabor Mezei | 892c4aa | 2022-03-21 12:21:43 +0100 | [diff] [blame] | 4548 | |
Gabor Mezei | 5d9a1fe | 2022-03-24 17:49:14 +0100 | [diff] [blame] | 4549 | PSA_DONE(); |
Hanno Becker | 2d2c3eb | 2020-08-20 14:54:24 +0100 | [diff] [blame] | 4550 | } |
| 4551 | /* END_CASE */ |
| 4552 | |
Jerry Yu | 53d23e2 | 2022-02-09 16:25:09 +0800 | [diff] [blame] | 4553 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_2 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4554 | void ssl_tls_prf(int type, data_t *secret, data_t *random, |
| 4555 | char *label, data_t *result_str, int exp_ret) |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 4556 | { |
| 4557 | unsigned char *output; |
| 4558 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4559 | output = mbedtls_calloc(1, result_str->len); |
| 4560 | if (output == NULL) { |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 4561 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4562 | } |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 4563 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4564 | USE_PSA_INIT(); |
Ron Eldor | 6b9b1b8 | 2019-05-15 17:04:33 +0300 | [diff] [blame] | 4565 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4566 | TEST_ASSERT(mbedtls_ssl_tls_prf(type, secret->x, secret->len, |
| 4567 | label, random->x, random->len, |
| 4568 | output, result_str->len) == exp_ret); |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 4569 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4570 | if (exp_ret == 0) { |
| 4571 | TEST_ASSERT(mbedtls_test_hexcmp(output, result_str->x, |
| 4572 | result_str->len, result_str->len) == 0); |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 4573 | } |
| 4574 | exit: |
| 4575 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4576 | mbedtls_free(output); |
| 4577 | USE_PSA_DONE(); |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 4578 | } |
| 4579 | /* END_CASE */ |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 4580 | |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 4581 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4582 | void ssl_serialize_session_save_load(int ticket_len, char *crt_file, |
| 4583 | int endpoint_type, int tls_version) |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 4584 | { |
| 4585 | mbedtls_ssl_session original, restored; |
| 4586 | unsigned char *buf = NULL; |
| 4587 | size_t len; |
| 4588 | |
| 4589 | /* |
| 4590 | * Test that a save-load pair is the identity |
| 4591 | */ |
| 4592 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4593 | mbedtls_ssl_session_init(&original); |
| 4594 | mbedtls_ssl_session_init(&restored); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 4595 | |
| 4596 | /* Prepare a dummy session to work on */ |
Jerry Yu | f092629 | 2022-07-15 13:05:57 +0800 | [diff] [blame] | 4597 | ((void) endpoint_type); |
| 4598 | ((void) tls_version); |
| 4599 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4600 | if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 4601 | TEST_ASSERT(mbedtls_test_ssl_tls13_populate_session( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4602 | &original, 0, endpoint_type) == 0); |
| 4603 | } else |
Jerry Yu | f092629 | 2022-07-15 13:05:57 +0800 | [diff] [blame] | 4604 | #endif |
| 4605 | { |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 4606 | TEST_ASSERT(mbedtls_test_ssl_tls12_populate_session( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4607 | &original, ticket_len, crt_file) == 0); |
Jerry Yu | f092629 | 2022-07-15 13:05:57 +0800 | [diff] [blame] | 4608 | } |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 4609 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 4610 | /* Serialize it */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4611 | TEST_ASSERT(mbedtls_ssl_session_save(&original, NULL, 0, &len) |
| 4612 | == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL); |
| 4613 | TEST_ASSERT((buf = mbedtls_calloc(1, len)) != NULL); |
| 4614 | TEST_ASSERT(mbedtls_ssl_session_save(&original, buf, len, &len) |
| 4615 | == 0); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 4616 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 4617 | /* Restore session from serialized data */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4618 | TEST_ASSERT(mbedtls_ssl_session_load(&restored, buf, len) == 0); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 4619 | |
| 4620 | /* |
| 4621 | * Make sure both session structures are identical |
| 4622 | */ |
| 4623 | #if defined(MBEDTLS_HAVE_TIME) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4624 | TEST_ASSERT(original.start == restored.start); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 4625 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4626 | TEST_ASSERT(original.tls_version == restored.tls_version); |
| 4627 | TEST_ASSERT(original.ciphersuite == restored.ciphersuite); |
Jerry Yu | f092629 | 2022-07-15 13:05:57 +0800 | [diff] [blame] | 4628 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4629 | if (tls_version == MBEDTLS_SSL_VERSION_TLS1_2) { |
| 4630 | TEST_ASSERT(original.id_len == restored.id_len); |
| 4631 | TEST_ASSERT(memcmp(original.id, |
| 4632 | restored.id, sizeof(original.id)) == 0); |
| 4633 | TEST_ASSERT(memcmp(original.master, |
| 4634 | restored.master, sizeof(original.master)) == 0); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 4635 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4636 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 4637 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4638 | TEST_ASSERT((original.peer_cert == NULL) == |
| 4639 | (restored.peer_cert == NULL)); |
| 4640 | if (original.peer_cert != NULL) { |
| 4641 | TEST_ASSERT(original.peer_cert->raw.len == |
| 4642 | restored.peer_cert->raw.len); |
| 4643 | TEST_ASSERT(memcmp(original.peer_cert->raw.p, |
| 4644 | restored.peer_cert->raw.p, |
| 4645 | original.peer_cert->raw.len) == 0); |
Jerry Yu | f092629 | 2022-07-15 13:05:57 +0800 | [diff] [blame] | 4646 | } |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 4647 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4648 | TEST_ASSERT(original.peer_cert_digest_type == |
| 4649 | restored.peer_cert_digest_type); |
| 4650 | TEST_ASSERT(original.peer_cert_digest_len == |
| 4651 | restored.peer_cert_digest_len); |
| 4652 | TEST_ASSERT((original.peer_cert_digest == NULL) == |
| 4653 | (restored.peer_cert_digest == NULL)); |
| 4654 | if (original.peer_cert_digest != NULL) { |
| 4655 | TEST_ASSERT(memcmp(original.peer_cert_digest, |
| 4656 | restored.peer_cert_digest, |
| 4657 | original.peer_cert_digest_len) == 0); |
Jerry Yu | f092629 | 2022-07-15 13:05:57 +0800 | [diff] [blame] | 4658 | } |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 4659 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4660 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4661 | TEST_ASSERT(original.verify_result == restored.verify_result); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 4662 | |
Jerry Yu | f092629 | 2022-07-15 13:05:57 +0800 | [diff] [blame] | 4663 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4664 | TEST_ASSERT(original.mfl_code == restored.mfl_code); |
Jerry Yu | f092629 | 2022-07-15 13:05:57 +0800 | [diff] [blame] | 4665 | #endif |
| 4666 | |
| 4667 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4668 | TEST_ASSERT(original.encrypt_then_mac == restored.encrypt_then_mac); |
Jerry Yu | f092629 | 2022-07-15 13:05:57 +0800 | [diff] [blame] | 4669 | #endif |
Jerry Yu | 6ac7c03 | 2022-07-21 23:11:55 +0800 | [diff] [blame] | 4670 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4671 | TEST_ASSERT(original.ticket_len == restored.ticket_len); |
| 4672 | if (original.ticket_len != 0) { |
| 4673 | TEST_ASSERT(original.ticket != NULL); |
| 4674 | TEST_ASSERT(restored.ticket != NULL); |
| 4675 | TEST_ASSERT(memcmp(original.ticket, |
| 4676 | restored.ticket, original.ticket_len) == 0); |
Jerry Yu | 6ac7c03 | 2022-07-21 23:11:55 +0800 | [diff] [blame] | 4677 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4678 | TEST_ASSERT(original.ticket_lifetime == restored.ticket_lifetime); |
Jerry Yu | 6ac7c03 | 2022-07-21 23:11:55 +0800 | [diff] [blame] | 4679 | #endif |
Jerry Yu | f092629 | 2022-07-15 13:05:57 +0800 | [diff] [blame] | 4680 | } |
Jerry Yu | 6ac7c03 | 2022-07-21 23:11:55 +0800 | [diff] [blame] | 4681 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 4682 | |
| 4683 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4684 | if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
| 4685 | TEST_ASSERT(original.endpoint == restored.endpoint); |
| 4686 | TEST_ASSERT(original.ciphersuite == restored.ciphersuite); |
| 4687 | TEST_ASSERT(original.ticket_age_add == restored.ticket_age_add); |
| 4688 | TEST_ASSERT(original.ticket_flags == restored.ticket_flags); |
| 4689 | TEST_ASSERT(original.resumption_key_len == restored.resumption_key_len); |
| 4690 | if (original.resumption_key_len != 0) { |
| 4691 | TEST_ASSERT(original.resumption_key != NULL); |
| 4692 | TEST_ASSERT(restored.resumption_key != NULL); |
| 4693 | TEST_ASSERT(memcmp(original.resumption_key, |
| 4694 | restored.resumption_key, |
| 4695 | original.resumption_key_len) == 0); |
Jerry Yu | 6ac7c03 | 2022-07-21 23:11:55 +0800 | [diff] [blame] | 4696 | } |
| 4697 | #if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4698 | if (endpoint_type == MBEDTLS_SSL_IS_SERVER) { |
| 4699 | TEST_ASSERT(original.start == restored.start); |
Jerry Yu | 6ac7c03 | 2022-07-21 23:11:55 +0800 | [diff] [blame] | 4700 | } |
Jerry Yu | f092629 | 2022-07-15 13:05:57 +0800 | [diff] [blame] | 4701 | #endif |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 4702 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4703 | if (endpoint_type == MBEDTLS_SSL_IS_CLIENT) { |
Jerry Yu | 6ac7c03 | 2022-07-21 23:11:55 +0800 | [diff] [blame] | 4704 | #if defined(MBEDTLS_HAVE_TIME) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4705 | TEST_ASSERT(original.ticket_received == restored.ticket_received); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 4706 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4707 | TEST_ASSERT(original.ticket_lifetime == restored.ticket_lifetime); |
| 4708 | TEST_ASSERT(original.ticket_len == restored.ticket_len); |
| 4709 | if (original.ticket_len != 0) { |
| 4710 | TEST_ASSERT(original.ticket != NULL); |
| 4711 | TEST_ASSERT(restored.ticket != NULL); |
| 4712 | TEST_ASSERT(memcmp(original.ticket, |
| 4713 | restored.ticket, |
| 4714 | original.ticket_len) == 0); |
Jerry Yu | 6ac7c03 | 2022-07-21 23:11:55 +0800 | [diff] [blame] | 4715 | } |
| 4716 | |
| 4717 | } |
| 4718 | #endif |
| 4719 | } |
| 4720 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 4721 | |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 4722 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4723 | mbedtls_ssl_session_free(&original); |
| 4724 | mbedtls_ssl_session_free(&restored); |
| 4725 | mbedtls_free(buf); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 4726 | } |
| 4727 | /* END_CASE */ |
| 4728 | |
Manuel Pégourié-Gonnard | aa75583 | 2019-06-03 10:53:47 +0200 | [diff] [blame] | 4729 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4730 | void ssl_serialize_session_load_save(int ticket_len, char *crt_file, |
| 4731 | int endpoint_type, int tls_version) |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 4732 | { |
| 4733 | mbedtls_ssl_session session; |
| 4734 | unsigned char *buf1 = NULL, *buf2 = NULL; |
| 4735 | size_t len0, len1, len2; |
| 4736 | |
| 4737 | /* |
| 4738 | * Test that a load-save pair is the identity |
| 4739 | */ |
| 4740 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4741 | mbedtls_ssl_session_init(&session); |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 4742 | |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 4743 | /* Prepare a dummy session to work on */ |
Jerry Yu | a180f99 | 2022-07-15 12:52:54 +0800 | [diff] [blame] | 4744 | ((void) endpoint_type); |
| 4745 | ((void) tls_version); |
| 4746 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4747 | if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 4748 | TEST_ASSERT(mbedtls_test_ssl_tls13_populate_session( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4749 | &session, 0, endpoint_type) == 0); |
| 4750 | } else |
Jerry Yu | a180f99 | 2022-07-15 12:52:54 +0800 | [diff] [blame] | 4751 | #endif |
| 4752 | { |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 4753 | TEST_ASSERT(mbedtls_test_ssl_tls12_populate_session( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4754 | &session, ticket_len, crt_file) == 0); |
Jerry Yu | a180f99 | 2022-07-15 12:52:54 +0800 | [diff] [blame] | 4755 | } |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 4756 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 4757 | /* Get desired buffer size for serializing */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4758 | TEST_ASSERT(mbedtls_ssl_session_save(&session, NULL, 0, &len0) |
| 4759 | == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL); |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 4760 | |
| 4761 | /* Allocate first buffer */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4762 | buf1 = mbedtls_calloc(1, len0); |
| 4763 | TEST_ASSERT(buf1 != NULL); |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 4764 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 4765 | /* Serialize to buffer and free live session */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4766 | TEST_ASSERT(mbedtls_ssl_session_save(&session, buf1, len0, &len1) |
| 4767 | == 0); |
| 4768 | TEST_ASSERT(len0 == len1); |
| 4769 | mbedtls_ssl_session_free(&session); |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 4770 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 4771 | /* Restore session from serialized data */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4772 | TEST_ASSERT(mbedtls_ssl_session_load(&session, buf1, len1) == 0); |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 4773 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 4774 | /* Allocate second buffer and serialize to it */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4775 | buf2 = mbedtls_calloc(1, len0); |
| 4776 | TEST_ASSERT(buf2 != NULL); |
| 4777 | TEST_ASSERT(mbedtls_ssl_session_save(&session, buf2, len0, &len2) |
| 4778 | == 0); |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 4779 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 4780 | /* Make sure both serialized versions are identical */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4781 | TEST_ASSERT(len1 == len2); |
| 4782 | TEST_ASSERT(memcmp(buf1, buf2, len1) == 0); |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 4783 | |
| 4784 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4785 | mbedtls_ssl_session_free(&session); |
| 4786 | mbedtls_free(buf1); |
| 4787 | mbedtls_free(buf2); |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 4788 | } |
| 4789 | /* END_CASE */ |
Manuel Pégourié-Gonnard | f5fa0aa | 2019-05-23 10:38:11 +0200 | [diff] [blame] | 4790 | |
| 4791 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4792 | void ssl_serialize_session_save_buf_size(int ticket_len, char *crt_file, |
| 4793 | int endpoint_type, int tls_version) |
Manuel Pégourié-Gonnard | f5fa0aa | 2019-05-23 10:38:11 +0200 | [diff] [blame] | 4794 | { |
| 4795 | mbedtls_ssl_session session; |
| 4796 | unsigned char *buf = NULL; |
| 4797 | size_t good_len, bad_len, test_len; |
| 4798 | |
| 4799 | /* |
| 4800 | * Test that session_save() fails cleanly on small buffers |
| 4801 | */ |
| 4802 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4803 | mbedtls_ssl_session_init(&session); |
Manuel Pégourié-Gonnard | f5fa0aa | 2019-05-23 10:38:11 +0200 | [diff] [blame] | 4804 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 4805 | /* Prepare dummy session and get serialized size */ |
Jerry Yu | 1ac476c | 2022-07-15 11:22:40 +0800 | [diff] [blame] | 4806 | ((void) endpoint_type); |
| 4807 | ((void) tls_version); |
| 4808 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4809 | if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 4810 | TEST_ASSERT(mbedtls_test_ssl_tls13_populate_session( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4811 | &session, 0, endpoint_type) == 0); |
| 4812 | } else |
Jerry Yu | 1ac476c | 2022-07-15 11:22:40 +0800 | [diff] [blame] | 4813 | #endif |
| 4814 | { |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 4815 | TEST_ASSERT(mbedtls_test_ssl_tls12_populate_session( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4816 | &session, ticket_len, crt_file) == 0); |
Jerry Yu | 1ac476c | 2022-07-15 11:22:40 +0800 | [diff] [blame] | 4817 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4818 | TEST_ASSERT(mbedtls_ssl_session_save(&session, NULL, 0, &good_len) |
| 4819 | == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL); |
Manuel Pégourié-Gonnard | f5fa0aa | 2019-05-23 10:38:11 +0200 | [diff] [blame] | 4820 | |
| 4821 | /* Try all possible bad lengths */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4822 | for (bad_len = 1; bad_len < good_len; bad_len++) { |
Manuel Pégourié-Gonnard | f5fa0aa | 2019-05-23 10:38:11 +0200 | [diff] [blame] | 4823 | /* Allocate exact size so that asan/valgrind can detect any overwrite */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4824 | mbedtls_free(buf); |
| 4825 | TEST_ASSERT((buf = mbedtls_calloc(1, bad_len)) != NULL); |
| 4826 | TEST_ASSERT(mbedtls_ssl_session_save(&session, buf, bad_len, |
| 4827 | &test_len) |
| 4828 | == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL); |
| 4829 | TEST_ASSERT(test_len == good_len); |
Manuel Pégourié-Gonnard | f5fa0aa | 2019-05-23 10:38:11 +0200 | [diff] [blame] | 4830 | } |
| 4831 | |
| 4832 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4833 | mbedtls_ssl_session_free(&session); |
| 4834 | mbedtls_free(buf); |
Manuel Pégourié-Gonnard | f5fa0aa | 2019-05-23 10:38:11 +0200 | [diff] [blame] | 4835 | } |
| 4836 | /* END_CASE */ |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 4837 | |
| 4838 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4839 | void ssl_serialize_session_load_buf_size(int ticket_len, char *crt_file, |
| 4840 | int endpoint_type, int tls_version) |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 4841 | { |
| 4842 | mbedtls_ssl_session session; |
| 4843 | unsigned char *good_buf = NULL, *bad_buf = NULL; |
| 4844 | size_t good_len, bad_len; |
| 4845 | |
| 4846 | /* |
| 4847 | * Test that session_load() fails cleanly on small buffers |
| 4848 | */ |
| 4849 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4850 | mbedtls_ssl_session_init(&session); |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 4851 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 4852 | /* Prepare serialized session data */ |
Jerry Yu | 6e8fec2 | 2022-07-15 10:37:02 +0800 | [diff] [blame] | 4853 | ((void) endpoint_type); |
| 4854 | ((void) tls_version); |
| 4855 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4856 | if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 4857 | TEST_ASSERT(mbedtls_test_ssl_tls13_populate_session( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4858 | &session, 0, endpoint_type) == 0); |
| 4859 | } else |
Jerry Yu | 6e8fec2 | 2022-07-15 10:37:02 +0800 | [diff] [blame] | 4860 | #endif |
| 4861 | { |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 4862 | TEST_ASSERT(mbedtls_test_ssl_tls12_populate_session( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4863 | &session, ticket_len, crt_file) == 0); |
Jerry Yu | 6e8fec2 | 2022-07-15 10:37:02 +0800 | [diff] [blame] | 4864 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4865 | TEST_ASSERT(mbedtls_ssl_session_save(&session, NULL, 0, &good_len) |
| 4866 | == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL); |
| 4867 | TEST_ASSERT((good_buf = mbedtls_calloc(1, good_len)) != NULL); |
| 4868 | TEST_ASSERT(mbedtls_ssl_session_save(&session, good_buf, good_len, |
| 4869 | &good_len) == 0); |
| 4870 | mbedtls_ssl_session_free(&session); |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 4871 | |
| 4872 | /* Try all possible bad lengths */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4873 | for (bad_len = 0; bad_len < good_len; bad_len++) { |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 4874 | /* Allocate exact size so that asan/valgrind can detect any overread */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4875 | mbedtls_free(bad_buf); |
| 4876 | bad_buf = mbedtls_calloc(1, bad_len ? bad_len : 1); |
| 4877 | TEST_ASSERT(bad_buf != NULL); |
| 4878 | memcpy(bad_buf, good_buf, bad_len); |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 4879 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4880 | TEST_ASSERT(mbedtls_ssl_session_load(&session, bad_buf, bad_len) |
| 4881 | == MBEDTLS_ERR_SSL_BAD_INPUT_DATA); |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 4882 | } |
| 4883 | |
| 4884 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4885 | mbedtls_ssl_session_free(&session); |
| 4886 | mbedtls_free(good_buf); |
| 4887 | mbedtls_free(bad_buf); |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 4888 | } |
| 4889 | /* END_CASE */ |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 4890 | |
Hanno Becker | 363b646 | 2019-05-29 12:44:28 +0100 | [diff] [blame] | 4891 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4892 | void ssl_session_serialize_version_check(int corrupt_major, |
| 4893 | int corrupt_minor, |
| 4894 | int corrupt_patch, |
| 4895 | int corrupt_config, |
| 4896 | int endpoint_type, |
| 4897 | int tls_version) |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 4898 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4899 | unsigned char serialized_session[2048]; |
Hanno Becker | 363b646 | 2019-05-29 12:44:28 +0100 | [diff] [blame] | 4900 | size_t serialized_session_len; |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 4901 | unsigned cur_byte; |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 4902 | mbedtls_ssl_session session; |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 4903 | uint8_t should_corrupt_byte[] = { corrupt_major == 1, |
| 4904 | corrupt_minor == 1, |
| 4905 | corrupt_patch == 1, |
| 4906 | corrupt_config == 1, |
| 4907 | corrupt_config == 1 }; |
| 4908 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4909 | mbedtls_ssl_session_init(&session); |
Jerry Yu | 534ff40 | 2022-07-14 16:43:43 +0800 | [diff] [blame] | 4910 | ((void) endpoint_type); |
| 4911 | ((void) tls_version); |
| 4912 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4913 | if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 4914 | TEST_ASSERT(mbedtls_test_ssl_tls13_populate_session( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4915 | &session, 0, endpoint_type) == 0); |
| 4916 | } else |
Jerry Yu | 534ff40 | 2022-07-14 16:43:43 +0800 | [diff] [blame] | 4917 | #endif |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 4918 | TEST_ASSERT(mbedtls_test_ssl_tls12_populate_session( |
| 4919 | &session, 0, NULL) == 0); |
Jerry Yu | 534ff40 | 2022-07-14 16:43:43 +0800 | [diff] [blame] | 4920 | |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 4921 | |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 4922 | /* Infer length of serialized session. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4923 | TEST_ASSERT(mbedtls_ssl_session_save(&session, |
| 4924 | serialized_session, |
| 4925 | sizeof(serialized_session), |
| 4926 | &serialized_session_len) == 0); |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 4927 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4928 | mbedtls_ssl_session_free(&session); |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 4929 | |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 4930 | /* Without any modification, we should be able to successfully |
Hanno Becker | 363b646 | 2019-05-29 12:44:28 +0100 | [diff] [blame] | 4931 | * de-serialize the session - double-check that. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4932 | TEST_ASSERT(mbedtls_ssl_session_load(&session, |
| 4933 | serialized_session, |
| 4934 | serialized_session_len) == 0); |
| 4935 | mbedtls_ssl_session_free(&session); |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 4936 | |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 4937 | /* Go through the bytes in the serialized session header and |
| 4938 | * corrupt them bit-by-bit. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4939 | for (cur_byte = 0; cur_byte < sizeof(should_corrupt_byte); cur_byte++) { |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 4940 | int cur_bit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4941 | unsigned char * const byte = &serialized_session[cur_byte]; |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 4942 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4943 | if (should_corrupt_byte[cur_byte] == 0) { |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 4944 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4945 | } |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 4946 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4947 | for (cur_bit = 0; cur_bit < CHAR_BIT; cur_bit++) { |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 4948 | unsigned char const corrupted_bit = 0x1u << cur_bit; |
| 4949 | /* Modify a single bit in the serialized session. */ |
| 4950 | *byte ^= corrupted_bit; |
| 4951 | |
| 4952 | /* Attempt to deserialize */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4953 | TEST_ASSERT(mbedtls_ssl_session_load(&session, |
| 4954 | serialized_session, |
| 4955 | serialized_session_len) == |
| 4956 | MBEDTLS_ERR_SSL_VERSION_MISMATCH); |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 4957 | |
| 4958 | /* Undo the change */ |
| 4959 | *byte ^= corrupted_bit; |
| 4960 | } |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 4961 | } |
| 4962 | |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 4963 | } |
| 4964 | /* END_CASE */ |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4965 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4966 | /* 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 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4967 | void mbedtls_endpoint_sanity(int endpoint_type) |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4968 | { |
| 4969 | enum { BUFFSIZE = 1024 }; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 4970 | mbedtls_test_ssl_endpoint ep; |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4971 | int ret = -1; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 4972 | mbedtls_test_handshake_test_options options; |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 4973 | mbedtls_test_init_handshake_options(&options); |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 4974 | options.pk_alg = MBEDTLS_PK_RSA; |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4975 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 4976 | ret = mbedtls_test_ssl_endpoint_init(NULL, endpoint_type, &options, |
| 4977 | NULL, NULL, NULL, NULL); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4978 | TEST_ASSERT(MBEDTLS_ERR_SSL_BAD_INPUT_DATA == ret); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4979 | |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 4980 | ret = mbedtls_test_ssl_endpoint_certificate_init(NULL, options.pk_alg, |
| 4981 | 0, 0, 0); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4982 | TEST_ASSERT(MBEDTLS_ERR_SSL_BAD_INPUT_DATA == ret); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4983 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 4984 | ret = mbedtls_test_ssl_endpoint_init(&ep, endpoint_type, &options, |
| 4985 | NULL, NULL, NULL, NULL); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4986 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4987 | |
| 4988 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 4989 | mbedtls_test_ssl_endpoint_free(&ep, NULL); |
| 4990 | mbedtls_test_free_handshake_options(&options); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4991 | } |
| 4992 | /* END_CASE */ |
| 4993 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4994 | /* 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] | 4995 | void move_handshake_to_state(int endpoint_type, int state, int need_pass) |
| 4996 | { |
| 4997 | enum { BUFFSIZE = 1024 }; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 4998 | mbedtls_test_ssl_endpoint base_ep, second_ep; |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4999 | int ret = -1; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 5000 | mbedtls_test_handshake_test_options options; |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 5001 | mbedtls_test_init_handshake_options(&options); |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 5002 | options.pk_alg = MBEDTLS_PK_RSA; |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 5003 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5004 | USE_PSA_INIT(); |
| 5005 | mbedtls_platform_zeroize(&base_ep, sizeof(base_ep)); |
| 5006 | mbedtls_platform_zeroize(&second_ep, sizeof(second_ep)); |
Neil Armstrong | 06baf04 | 2022-04-14 16:21:15 +0200 | [diff] [blame] | 5007 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 5008 | ret = mbedtls_test_ssl_endpoint_init(&base_ep, endpoint_type, &options, |
| 5009 | NULL, NULL, NULL, NULL); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5010 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 5011 | |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 5012 | ret = mbedtls_test_ssl_endpoint_init( |
| 5013 | &second_ep, |
| 5014 | (endpoint_type == MBEDTLS_SSL_IS_SERVER) ? |
| 5015 | MBEDTLS_SSL_IS_CLIENT : MBEDTLS_SSL_IS_SERVER, |
| 5016 | &options, NULL, NULL, NULL, NULL); |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 5017 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5018 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 5019 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 5020 | ret = mbedtls_test_mock_socket_connect(&(base_ep.socket), |
| 5021 | &(second_ep.socket), |
| 5022 | BUFFSIZE); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5023 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 5024 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 5025 | ret = mbedtls_test_move_handshake_to_state(&(base_ep.ssl), |
| 5026 | &(second_ep.ssl), |
| 5027 | state); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5028 | if (need_pass) { |
| 5029 | TEST_ASSERT(ret == 0 || |
| 5030 | ret == MBEDTLS_ERR_SSL_WANT_READ || |
| 5031 | ret == MBEDTLS_ERR_SSL_WANT_WRITE); |
| 5032 | TEST_ASSERT(base_ep.ssl.state == state); |
| 5033 | } else { |
| 5034 | TEST_ASSERT(ret != 0 && |
| 5035 | ret != MBEDTLS_ERR_SSL_WANT_READ && |
| 5036 | ret != MBEDTLS_ERR_SSL_WANT_WRITE); |
| 5037 | TEST_ASSERT(base_ep.ssl.state != state); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 5038 | } |
| 5039 | |
| 5040 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 5041 | mbedtls_test_free_handshake_options(&options); |
| 5042 | mbedtls_test_ssl_endpoint_free(&base_ep, NULL); |
| 5043 | mbedtls_test_ssl_endpoint_free(&second_ep, NULL); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5044 | USE_PSA_DONE(); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 5045 | } |
| 5046 | /* END_CASE */ |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 5047 | |
Andrzej Kurek | 9113df8 | 2023-01-19 18:26:31 -0500 | [diff] [blame] | 5048 | /* 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:MBEDTLS_CAN_HANDLE_RSA_TEST_KEY */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5049 | void handshake_version(int dtls, int client_min_version, int client_max_version, |
| 5050 | int server_min_version, int server_max_version, |
| 5051 | int expected_negotiated_version) |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 5052 | { |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 5053 | mbedtls_test_handshake_test_options options; |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 5054 | mbedtls_test_init_handshake_options(&options); |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 5055 | |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 5056 | options.client_min_version = client_min_version; |
| 5057 | options.client_max_version = client_max_version; |
| 5058 | options.server_min_version = server_min_version; |
| 5059 | options.server_max_version = server_max_version; |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 5060 | options.expected_negotiated_version = expected_negotiated_version; |
| 5061 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5062 | options.dtls = dtls; |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 5063 | mbedtls_test_ssl_perform_handshake(&options); |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 5064 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5065 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 5066 | goto exit; |
Andrzej Kurek | e11acb2 | 2022-06-27 06:11:34 -0400 | [diff] [blame] | 5067 | |
| 5068 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 5069 | mbedtls_test_free_handshake_options(&options); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5070 | } |
| 5071 | /* END_CASE */ |
Andrzej Kurek | 9e9efdc | 2020-02-26 05:25:23 -0500 | [diff] [blame] | 5072 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5073 | /* 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 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5074 | void handshake_psk_cipher(char *cipher, int pk_alg, data_t *psk_str, int dtls) |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5075 | { |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 5076 | mbedtls_test_handshake_test_options options; |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 5077 | mbedtls_test_init_handshake_options(&options); |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 5078 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5079 | options.cipher = cipher; |
| 5080 | options.dtls = dtls; |
| 5081 | options.psk_str = psk_str; |
| 5082 | options.pk_alg = pk_alg; |
Andrzej Kurek | cc5169c | 2020-02-04 09:04:56 -0500 | [diff] [blame] | 5083 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 5084 | mbedtls_test_ssl_perform_handshake(&options); |
Andrzej Kurek | 316da1f | 2020-02-26 09:03:47 -0500 | [diff] [blame] | 5085 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5086 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 5087 | goto exit; |
Andrzej Kurek | e11acb2 | 2022-06-27 06:11:34 -0400 | [diff] [blame] | 5088 | |
Andrzej Kurek | ddb8cd6 | 2022-07-04 16:07:28 -0400 | [diff] [blame] | 5089 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 5090 | mbedtls_test_free_handshake_options(&options); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5091 | } |
| 5092 | /* END_CASE */ |
Andrzej Kurek | 316da1f | 2020-02-26 09:03:47 -0500 | [diff] [blame] | 5093 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5094 | /* 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 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5095 | void handshake_cipher(char *cipher, int pk_alg, int dtls) |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5096 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5097 | test_handshake_psk_cipher(cipher, pk_alg, NULL, dtls); |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 5098 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5099 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 5100 | goto exit; |
| 5101 | } |
| 5102 | /* END_CASE */ |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 5103 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5104 | /* 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 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5105 | void handshake_ciphersuite_select(char *cipher, int pk_alg, data_t *psk_str, |
| 5106 | int psa_alg, int psa_alg2, int psa_usage, |
| 5107 | int expected_handshake_result, |
| 5108 | int expected_ciphersuite) |
Neil Armstrong | 8c52ed8 | 2022-05-27 13:14:55 +0200 | [diff] [blame] | 5109 | { |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 5110 | mbedtls_test_handshake_test_options options; |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 5111 | mbedtls_test_init_handshake_options(&options); |
Neil Armstrong | 8c52ed8 | 2022-05-27 13:14:55 +0200 | [diff] [blame] | 5112 | |
| 5113 | options.cipher = cipher; |
Neil Armstrong | db13497 | 2022-06-30 09:06:28 +0200 | [diff] [blame] | 5114 | options.psk_str = psk_str; |
Neil Armstrong | 8c52ed8 | 2022-05-27 13:14:55 +0200 | [diff] [blame] | 5115 | options.pk_alg = pk_alg; |
| 5116 | options.opaque_alg = psa_alg; |
| 5117 | options.opaque_alg2 = psa_alg2; |
| 5118 | options.opaque_usage = psa_usage; |
| 5119 | options.expected_handshake_result = expected_handshake_result; |
| 5120 | options.expected_ciphersuite = expected_ciphersuite; |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 5121 | mbedtls_test_ssl_perform_handshake(&options); |
Neil Armstrong | 8c52ed8 | 2022-05-27 13:14:55 +0200 | [diff] [blame] | 5122 | |
| 5123 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 5124 | goto exit; |
Andrzej Kurek | ddb8cd6 | 2022-07-04 16:07:28 -0400 | [diff] [blame] | 5125 | |
| 5126 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 5127 | mbedtls_test_free_handshake_options(&options); |
Neil Armstrong | 8c52ed8 | 2022-05-27 13:14:55 +0200 | [diff] [blame] | 5128 | } |
| 5129 | /* END_CASE */ |
| 5130 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5131 | /* 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 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5132 | void app_data(int mfl, int cli_msg_len, int srv_msg_len, |
| 5133 | int expected_cli_fragments, |
| 5134 | int expected_srv_fragments, int dtls) |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5135 | { |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 5136 | mbedtls_test_handshake_test_options options; |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 5137 | mbedtls_test_init_handshake_options(&options); |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 5138 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5139 | options.mfl = mfl; |
| 5140 | options.cli_msg_len = cli_msg_len; |
| 5141 | options.srv_msg_len = srv_msg_len; |
| 5142 | options.expected_cli_fragments = expected_cli_fragments; |
| 5143 | options.expected_srv_fragments = expected_srv_fragments; |
| 5144 | options.dtls = dtls; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5145 | #if !defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Ronald Cron | 21a1b2d | 2022-06-15 17:11:35 +0200 | [diff] [blame] | 5146 | options.expected_negotiated_version = MBEDTLS_SSL_VERSION_TLS1_3; |
| 5147 | #endif |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 5148 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 5149 | mbedtls_test_ssl_perform_handshake(&options); |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 5150 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5151 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 5152 | goto exit; |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 5153 | |
| 5154 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 5155 | mbedtls_test_free_handshake_options(&options); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5156 | } |
| 5157 | /* END_CASE */ |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 5158 | |
Andrzej Kurek | 714ae65 | 2022-11-02 19:07:19 -0400 | [diff] [blame] | 5159 | /* 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:MBEDTLS_CAN_HANDLE_RSA_TEST_KEY */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5160 | void app_data_tls(int mfl, int cli_msg_len, int srv_msg_len, |
| 5161 | int expected_cli_fragments, |
| 5162 | int expected_srv_fragments) |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5163 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5164 | test_app_data(mfl, cli_msg_len, srv_msg_len, expected_cli_fragments, |
| 5165 | expected_srv_fragments, 0); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5166 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 5167 | goto exit; |
| 5168 | } |
| 5169 | /* END_CASE */ |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 5170 | |
Andrzej Kurek | 9113df8 | 2023-01-19 18:26:31 -0500 | [diff] [blame] | 5171 | /* 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:MBEDTLS_CAN_HANDLE_RSA_TEST_KEY */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5172 | void app_data_dtls(int mfl, int cli_msg_len, int srv_msg_len, |
| 5173 | int expected_cli_fragments, |
| 5174 | int expected_srv_fragments) |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5175 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5176 | test_app_data(mfl, cli_msg_len, srv_msg_len, expected_cli_fragments, |
| 5177 | expected_srv_fragments, 1); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5178 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 5179 | goto exit; |
| 5180 | } |
| 5181 | /* END_CASE */ |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 5182 | |
Andrzej Kurek | 9113df8 | 2023-01-19 18:26:31 -0500 | [diff] [blame] | 5183 | /* 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:MBEDTLS_CAN_HANDLE_RSA_TEST_KEY */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5184 | void handshake_serialization() |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5185 | { |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 5186 | mbedtls_test_handshake_test_options options; |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 5187 | mbedtls_test_init_handshake_options(&options); |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 5188 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5189 | options.serialize = 1; |
| 5190 | options.dtls = 1; |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 5191 | mbedtls_test_ssl_perform_handshake(&options); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5192 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 5193 | goto exit; |
Andrzej Kurek | 6e518ab | 2022-06-11 05:08:38 -0400 | [diff] [blame] | 5194 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 5195 | mbedtls_test_free_handshake_options(&options); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5196 | } |
| 5197 | /* END_CASE */ |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 5198 | |
Andrzej Kurek | a86cef3 | 2023-01-19 18:27:57 -0500 | [diff] [blame] | 5199 | /* 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:MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5200 | void handshake_fragmentation(int mfl, |
| 5201 | int expected_srv_hs_fragmentation, |
| 5202 | int expected_cli_hs_fragmentation) |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 5203 | { |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 5204 | mbedtls_test_handshake_test_options options; |
| 5205 | mbedtls_test_ssl_log_pattern srv_pattern, cli_pattern; |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 5206 | |
| 5207 | srv_pattern.pattern = cli_pattern.pattern = "found fragmented DTLS handshake"; |
| 5208 | srv_pattern.counter = 0; |
| 5209 | cli_pattern.counter = 0; |
| 5210 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 5211 | mbedtls_test_init_handshake_options(&options); |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 5212 | options.dtls = 1; |
| 5213 | options.mfl = mfl; |
Darryl Green | aad82f9 | 2019-12-02 10:53:11 +0000 | [diff] [blame] | 5214 | /* Set cipher to one using CBC so that record splitting can be tested */ |
| 5215 | options.cipher = "TLS-DHE-RSA-WITH-AES-256-CBC-SHA256"; |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 5216 | options.srv_auth_mode = MBEDTLS_SSL_VERIFY_REQUIRED; |
| 5217 | options.srv_log_obj = &srv_pattern; |
| 5218 | options.cli_log_obj = &cli_pattern; |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 5219 | options.srv_log_fun = mbedtls_test_ssl_log_analyzer; |
| 5220 | options.cli_log_fun = mbedtls_test_ssl_log_analyzer; |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 5221 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 5222 | mbedtls_test_ssl_perform_handshake(&options); |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 5223 | |
| 5224 | /* Test if the server received a fragmented handshake */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5225 | if (expected_srv_hs_fragmentation) { |
| 5226 | TEST_ASSERT(srv_pattern.counter >= 1); |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 5227 | } |
| 5228 | /* Test if the client received a fragmented handshake */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5229 | if (expected_cli_hs_fragmentation) { |
| 5230 | TEST_ASSERT(cli_pattern.counter >= 1); |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 5231 | } |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 5232 | |
| 5233 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 5234 | mbedtls_test_free_handshake_options(&options); |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 5235 | } |
| 5236 | /* END_CASE */ |
| 5237 | |
Andrzej Kurek | 9113df8 | 2023-01-19 18:26:31 -0500 | [diff] [blame] | 5238 | /* 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:MBEDTLS_CAN_HANDLE_RSA_TEST_KEY */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5239 | void renegotiation(int legacy_renegotiation) |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5240 | { |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 5241 | mbedtls_test_handshake_test_options options; |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 5242 | mbedtls_test_init_handshake_options(&options); |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 5243 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5244 | options.renegotiate = 1; |
| 5245 | options.legacy_renegotiation = legacy_renegotiation; |
| 5246 | options.dtls = 1; |
Andrzej Kurek | 316da1f | 2020-02-26 09:03:47 -0500 | [diff] [blame] | 5247 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 5248 | mbedtls_test_ssl_perform_handshake(&options); |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 5249 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5250 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 5251 | goto exit; |
Andrzej Kurek | ddb8cd6 | 2022-07-04 16:07:28 -0400 | [diff] [blame] | 5252 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 5253 | mbedtls_test_free_handshake_options(&options); |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 5254 | } |
| 5255 | /* END_CASE */ |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 5256 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5257 | /* 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 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5258 | void resize_buffers(int mfl, int renegotiation, int legacy_renegotiation, |
| 5259 | int serialize, int dtls, char *cipher) |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 5260 | { |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 5261 | mbedtls_test_handshake_test_options options; |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 5262 | mbedtls_test_init_handshake_options(&options); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 5263 | |
| 5264 | options.mfl = mfl; |
Andrzej Kurek | 8ea6872 | 2020-04-03 06:40:47 -0400 | [diff] [blame] | 5265 | options.cipher = cipher; |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 5266 | options.renegotiate = renegotiation; |
| 5267 | options.legacy_renegotiation = legacy_renegotiation; |
| 5268 | options.serialize = serialize; |
| 5269 | options.dtls = dtls; |
| 5270 | options.resize_buffers = 1; |
| 5271 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 5272 | mbedtls_test_ssl_perform_handshake(&options); |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 5273 | |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 5274 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 5275 | goto exit; |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 5276 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 5277 | mbedtls_test_free_handshake_options(&options); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 5278 | } |
| 5279 | /* END_CASE */ |
| 5280 | |
Andrzej Kurek | 9113df8 | 2023-01-19 18:26:31 -0500 | [diff] [blame] | 5281 | /* 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:MBEDTLS_CAN_HANDLE_RSA_TEST_KEY */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5282 | void resize_buffers_serialize_mfl(int mfl) |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 5283 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5284 | test_resize_buffers(mfl, 0, MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION, 1, 1, |
| 5285 | (char *) ""); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 5286 | |
| 5287 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 5288 | goto exit; |
| 5289 | } |
| 5290 | /* END_CASE */ |
| 5291 | |
Andrzej Kurek | 9113df8 | 2023-01-19 18:26:31 -0500 | [diff] [blame] | 5292 | /* 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:MBEDTLS_CAN_HANDLE_RSA_TEST_KEY */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5293 | void resize_buffers_renegotiate_mfl(int mfl, int legacy_renegotiation, |
| 5294 | char *cipher) |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 5295 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5296 | test_resize_buffers(mfl, 1, legacy_renegotiation, 0, 1, cipher); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 5297 | |
| 5298 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 5299 | goto exit; |
| 5300 | } |
| 5301 | /* END_CASE */ |
Manuel Pégourié-Gonnard | 045f094 | 2020-07-02 11:34:02 +0200 | [diff] [blame] | 5302 | |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 5303 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */ |
Hanno Becker | 6667ffd | 2021-04-19 21:59:22 +0100 | [diff] [blame] | 5304 | void test_multiple_psks() |
| 5305 | { |
| 5306 | unsigned char psk0[10] = { 0 }; |
| 5307 | unsigned char psk0_identity[] = { 'f', 'o', 'o' }; |
| 5308 | |
| 5309 | unsigned char psk1[10] = { 0 }; |
| 5310 | unsigned char psk1_identity[] = { 'b', 'a', 'r' }; |
| 5311 | |
| 5312 | mbedtls_ssl_config conf; |
| 5313 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5314 | USE_PSA_INIT(); |
| 5315 | mbedtls_ssl_config_init(&conf); |
Hanno Becker | 6667ffd | 2021-04-19 21:59:22 +0100 | [diff] [blame] | 5316 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5317 | TEST_ASSERT(mbedtls_ssl_conf_psk(&conf, |
| 5318 | psk0, sizeof(psk0), |
| 5319 | psk0_identity, sizeof(psk0_identity)) == 0); |
| 5320 | TEST_ASSERT(mbedtls_ssl_conf_psk(&conf, |
| 5321 | psk1, sizeof(psk1), |
| 5322 | psk1_identity, sizeof(psk1_identity)) == |
| 5323 | MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE); |
Hanno Becker | 6667ffd | 2021-04-19 21:59:22 +0100 | [diff] [blame] | 5324 | |
| 5325 | exit: |
| 5326 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5327 | mbedtls_ssl_config_free(&conf); |
Neil Armstrong | 4c3b4e0 | 2022-05-03 09:24:26 +0200 | [diff] [blame] | 5328 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5329 | USE_PSA_DONE(); |
Hanno Becker | 6667ffd | 2021-04-19 21:59:22 +0100 | [diff] [blame] | 5330 | } |
| 5331 | /* END_CASE */ |
| 5332 | |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 5333 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED:MBEDTLS_USE_PSA_CRYPTO */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5334 | void test_multiple_psks_opaque(int mode) |
Hanno Becker | 6667ffd | 2021-04-19 21:59:22 +0100 | [diff] [blame] | 5335 | { |
| 5336 | /* |
| 5337 | * Mode 0: Raw PSK, then opaque PSK |
| 5338 | * Mode 1: Opaque PSK, then raw PSK |
| 5339 | * Mode 2: 2x opaque PSK |
| 5340 | */ |
| 5341 | |
| 5342 | unsigned char psk0_raw[10] = { 0 }; |
| 5343 | unsigned char psk0_raw_identity[] = { 'f', 'o', 'o' }; |
| 5344 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5345 | mbedtls_svc_key_id_t psk0_opaque = mbedtls_svc_key_id_make(0x1, (psa_key_id_t) 1); |
Andrzej Kurek | 03e0146 | 2022-01-03 12:53:24 +0100 | [diff] [blame] | 5346 | |
Hanno Becker | 6667ffd | 2021-04-19 21:59:22 +0100 | [diff] [blame] | 5347 | unsigned char psk0_opaque_identity[] = { 'f', 'o', 'o' }; |
| 5348 | |
| 5349 | unsigned char psk1_raw[10] = { 0 }; |
| 5350 | unsigned char psk1_raw_identity[] = { 'b', 'a', 'r' }; |
| 5351 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5352 | mbedtls_svc_key_id_t psk1_opaque = mbedtls_svc_key_id_make(0x1, (psa_key_id_t) 2); |
Andrzej Kurek | 03e0146 | 2022-01-03 12:53:24 +0100 | [diff] [blame] | 5353 | |
Hanno Becker | 6667ffd | 2021-04-19 21:59:22 +0100 | [diff] [blame] | 5354 | unsigned char psk1_opaque_identity[] = { 'b', 'a', 'r' }; |
| 5355 | |
| 5356 | mbedtls_ssl_config conf; |
| 5357 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5358 | USE_PSA_INIT(); |
| 5359 | mbedtls_ssl_config_init(&conf); |
Hanno Becker | 6667ffd | 2021-04-19 21:59:22 +0100 | [diff] [blame] | 5360 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5361 | switch (mode) { |
Hanno Becker | 6667ffd | 2021-04-19 21:59:22 +0100 | [diff] [blame] | 5362 | case 0: |
| 5363 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5364 | TEST_ASSERT(mbedtls_ssl_conf_psk(&conf, |
| 5365 | psk0_raw, sizeof(psk0_raw), |
| 5366 | psk0_raw_identity, sizeof(psk0_raw_identity)) |
| 5367 | == 0); |
| 5368 | TEST_ASSERT(mbedtls_ssl_conf_psk_opaque(&conf, |
| 5369 | psk1_opaque, |
| 5370 | psk1_opaque_identity, |
| 5371 | sizeof(psk1_opaque_identity)) |
| 5372 | == MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE); |
Hanno Becker | 6667ffd | 2021-04-19 21:59:22 +0100 | [diff] [blame] | 5373 | break; |
| 5374 | |
| 5375 | case 1: |
| 5376 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5377 | TEST_ASSERT(mbedtls_ssl_conf_psk_opaque(&conf, |
| 5378 | psk0_opaque, |
| 5379 | psk0_opaque_identity, |
| 5380 | sizeof(psk0_opaque_identity)) |
| 5381 | == 0); |
| 5382 | TEST_ASSERT(mbedtls_ssl_conf_psk(&conf, |
| 5383 | psk1_raw, sizeof(psk1_raw), |
| 5384 | psk1_raw_identity, sizeof(psk1_raw_identity)) |
| 5385 | == MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE); |
Hanno Becker | 6667ffd | 2021-04-19 21:59:22 +0100 | [diff] [blame] | 5386 | |
| 5387 | break; |
| 5388 | |
| 5389 | case 2: |
| 5390 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5391 | TEST_ASSERT(mbedtls_ssl_conf_psk_opaque(&conf, |
| 5392 | psk0_opaque, |
| 5393 | psk0_opaque_identity, |
| 5394 | sizeof(psk0_opaque_identity)) |
| 5395 | == 0); |
| 5396 | TEST_ASSERT(mbedtls_ssl_conf_psk_opaque(&conf, |
| 5397 | psk1_opaque, |
| 5398 | psk1_opaque_identity, |
| 5399 | sizeof(psk1_opaque_identity)) |
| 5400 | == MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE); |
Hanno Becker | 6667ffd | 2021-04-19 21:59:22 +0100 | [diff] [blame] | 5401 | |
| 5402 | break; |
| 5403 | |
| 5404 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5405 | TEST_ASSERT(0); |
Hanno Becker | 6667ffd | 2021-04-19 21:59:22 +0100 | [diff] [blame] | 5406 | break; |
| 5407 | } |
| 5408 | |
| 5409 | exit: |
| 5410 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5411 | mbedtls_ssl_config_free(&conf); |
| 5412 | USE_PSA_DONE(); |
Hanno Becker | 6667ffd | 2021-04-19 21:59:22 +0100 | [diff] [blame] | 5413 | |
| 5414 | } |
| 5415 | /* END_CASE */ |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 5416 | |
Ronald Cron | 37bdaab | 2022-03-30 16:45:51 +0200 | [diff] [blame] | 5417 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5418 | void conf_version(int endpoint, int transport, |
| 5419 | int min_tls_version, int max_tls_version, |
| 5420 | int expected_ssl_setup_result) |
Ronald Cron | 37bdaab | 2022-03-30 16:45:51 +0200 | [diff] [blame] | 5421 | { |
| 5422 | mbedtls_ssl_config conf; |
| 5423 | mbedtls_ssl_context ssl; |
| 5424 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5425 | mbedtls_ssl_config_init(&conf); |
| 5426 | mbedtls_ssl_init(&ssl); |
Ronald Cron | 37bdaab | 2022-03-30 16:45:51 +0200 | [diff] [blame] | 5427 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5428 | mbedtls_ssl_conf_endpoint(&conf, endpoint); |
| 5429 | mbedtls_ssl_conf_transport(&conf, transport); |
| 5430 | mbedtls_ssl_conf_min_tls_version(&conf, min_tls_version); |
| 5431 | mbedtls_ssl_conf_max_tls_version(&conf, max_tls_version); |
Ronald Cron | 37bdaab | 2022-03-30 16:45:51 +0200 | [diff] [blame] | 5432 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5433 | TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == expected_ssl_setup_result); |
Ronald Cron | 37bdaab | 2022-03-30 16:45:51 +0200 | [diff] [blame] | 5434 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5435 | mbedtls_ssl_free(&ssl); |
| 5436 | mbedtls_ssl_config_free(&conf); |
Ronald Cron | 37bdaab | 2022-03-30 16:45:51 +0200 | [diff] [blame] | 5437 | } |
| 5438 | /* END_CASE */ |
| 5439 | |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 5440 | /* 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 */ |
| 5441 | void conf_curve() |
| 5442 | { |
| 5443 | |
| 5444 | mbedtls_ecp_group_id curve_list[] = { MBEDTLS_ECP_DP_SECP192R1, |
| 5445 | MBEDTLS_ECP_DP_SECP224R1, |
| 5446 | MBEDTLS_ECP_DP_SECP256R1, |
| 5447 | MBEDTLS_ECP_DP_NONE }; |
Tom Cosgrove | a327b52 | 2022-08-03 08:33:06 +0100 | [diff] [blame] | 5448 | uint16_t iana_tls_group_list[] = { MBEDTLS_SSL_IANA_TLS_GROUP_SECP192R1, |
| 5449 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP224R1, |
| 5450 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, |
| 5451 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE }; |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 5452 | |
| 5453 | mbedtls_ssl_config conf; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5454 | mbedtls_ssl_config_init(&conf); |
Jerry Yu | baa4934 | 2022-02-15 10:26:40 +0800 | [diff] [blame] | 5455 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5456 | mbedtls_ssl_conf_max_tls_version(&conf, MBEDTLS_SSL_VERSION_TLS1_2); |
| 5457 | mbedtls_ssl_conf_min_tls_version(&conf, MBEDTLS_SSL_VERSION_TLS1_2); |
Jerry Yu | baa4934 | 2022-02-15 10:26:40 +0800 | [diff] [blame] | 5458 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5459 | mbedtls_ssl_conf_max_tls_version(&conf, MBEDTLS_SSL_VERSION_TLS1_3); |
| 5460 | mbedtls_ssl_conf_min_tls_version(&conf, MBEDTLS_SSL_VERSION_TLS1_3); |
Jerry Yu | baa4934 | 2022-02-15 10:26:40 +0800 | [diff] [blame] | 5461 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5462 | mbedtls_ssl_conf_curves(&conf, curve_list); |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 5463 | |
| 5464 | mbedtls_ssl_context ssl; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5465 | mbedtls_ssl_init(&ssl); |
| 5466 | TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0); |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 5467 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5468 | TEST_ASSERT(ssl.handshake != NULL && ssl.handshake->group_list != NULL); |
| 5469 | TEST_ASSERT(ssl.conf != NULL && ssl.conf->group_list == NULL); |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 5470 | |
Yanray Wang | d577a68 | 2022-10-27 11:47:54 +0800 | [diff] [blame^] | 5471 | TEST_EQUAL(ssl.handshake-> |
| 5472 | group_list[ARRAY_LENGTH(iana_tls_group_list) - 1], |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5473 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE); |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 5474 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5475 | for (size_t i = 0; i < ARRAY_LENGTH(iana_tls_group_list); i++) { |
| 5476 | TEST_EQUAL(iana_tls_group_list[i], ssl.handshake->group_list[i]); |
| 5477 | } |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 5478 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5479 | mbedtls_ssl_free(&ssl); |
| 5480 | mbedtls_ssl_config_free(&conf); |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 5481 | } |
| 5482 | /* END_CASE */ |
| 5483 | |
| 5484 | /* BEGIN_CASE depends_on:MBEDTLS_DEPRECATED_REMOVED */ |
| 5485 | void conf_group() |
| 5486 | { |
| 5487 | uint16_t iana_tls_group_list[] = { MBEDTLS_SSL_IANA_TLS_GROUP_SECP192R1, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5488 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP224R1, |
| 5489 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, |
| 5490 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE }; |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 5491 | |
| 5492 | mbedtls_ssl_config conf; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5493 | mbedtls_ssl_config_init(&conf); |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 5494 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5495 | mbedtls_ssl_conf_max_tls_version(&conf, MBEDTLS_SSL_VERSION_TLS1_2); |
| 5496 | mbedtls_ssl_conf_min_tls_version(&conf, MBEDTLS_SSL_VERSION_TLS1_2); |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 5497 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5498 | mbedtls_ssl_conf_groups(&conf, iana_tls_group_list); |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 5499 | |
| 5500 | mbedtls_ssl_context ssl; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5501 | mbedtls_ssl_init(&ssl); |
| 5502 | TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0); |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 5503 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5504 | TEST_ASSERT(ssl.conf != NULL && ssl.conf->group_list != NULL); |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 5505 | |
Yanray Wang | d577a68 | 2022-10-27 11:47:54 +0800 | [diff] [blame^] | 5506 | TEST_EQUAL(ssl.conf-> |
| 5507 | group_list[ARRAY_LENGTH(iana_tls_group_list) - 1], |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5508 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE); |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 5509 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5510 | for (size_t i = 0; i < ARRAY_LENGTH(iana_tls_group_list); i++) { |
| 5511 | TEST_EQUAL(iana_tls_group_list[i], ssl.conf->group_list[i]); |
| 5512 | } |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 5513 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5514 | mbedtls_ssl_free(&ssl); |
| 5515 | mbedtls_ssl_config_free(&conf); |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 5516 | } |
| 5517 | /* END_CASE */ |
Paul Elliott | b9af2db | 2022-03-09 15:34:37 +0000 | [diff] [blame] | 5518 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5519 | /* 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 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5520 | void force_bad_session_id_len() |
Andrzej Kurek | 514683a | 2022-06-10 10:33:05 -0400 | [diff] [blame] | 5521 | { |
| 5522 | enum { BUFFSIZE = 1024 }; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 5523 | mbedtls_test_handshake_test_options options; |
| 5524 | mbedtls_test_ssl_endpoint client, server; |
| 5525 | mbedtls_test_ssl_log_pattern srv_pattern, cli_pattern; |
Andrzej Kurek | 514683a | 2022-06-10 10:33:05 -0400 | [diff] [blame] | 5526 | mbedtls_test_message_socket_context server_context, client_context; |
| 5527 | |
| 5528 | srv_pattern.pattern = cli_pattern.pattern = "cache did not store session"; |
| 5529 | srv_pattern.counter = 0; |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 5530 | mbedtls_test_init_handshake_options(&options); |
Andrzej Kurek | 514683a | 2022-06-10 10:33:05 -0400 | [diff] [blame] | 5531 | |
| 5532 | options.srv_log_obj = &srv_pattern; |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 5533 | options.srv_log_fun = mbedtls_test_ssl_log_analyzer; |
Andrzej Kurek | 514683a | 2022-06-10 10:33:05 -0400 | [diff] [blame] | 5534 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5535 | USE_PSA_INIT(); |
| 5536 | mbedtls_platform_zeroize(&client, sizeof(client)); |
| 5537 | mbedtls_platform_zeroize(&server, sizeof(server)); |
Andrzej Kurek | 514683a | 2022-06-10 10:33:05 -0400 | [diff] [blame] | 5538 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 5539 | mbedtls_test_message_socket_init(&server_context); |
| 5540 | mbedtls_test_message_socket_init(&client_context); |
Andrzej Kurek | 514683a | 2022-06-10 10:33:05 -0400 | [diff] [blame] | 5541 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 5542 | TEST_ASSERT(mbedtls_test_ssl_endpoint_init(&client, MBEDTLS_SSL_IS_CLIENT, |
| 5543 | &options, NULL, NULL, |
| 5544 | NULL, NULL) == 0); |
Andrzej Kurek | 514683a | 2022-06-10 10:33:05 -0400 | [diff] [blame] | 5545 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 5546 | TEST_ASSERT(mbedtls_test_ssl_endpoint_init(&server, MBEDTLS_SSL_IS_SERVER, |
| 5547 | &options, NULL, NULL, NULL, |
| 5548 | NULL) == 0); |
Andrzej Kurek | 514683a | 2022-06-10 10:33:05 -0400 | [diff] [blame] | 5549 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5550 | mbedtls_debug_set_threshold(1); |
| 5551 | mbedtls_ssl_conf_dbg(&server.conf, options.srv_log_fun, |
| 5552 | options.srv_log_obj); |
Andrzej Kurek | 514683a | 2022-06-10 10:33:05 -0400 | [diff] [blame] | 5553 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 5554 | TEST_ASSERT(mbedtls_test_mock_socket_connect(&(client.socket), |
| 5555 | &(server.socket), |
| 5556 | BUFFSIZE) == 0); |
Andrzej Kurek | 514683a | 2022-06-10 10:33:05 -0400 | [diff] [blame] | 5557 | |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 5558 | TEST_ASSERT(mbedtls_test_move_handshake_to_state( |
| 5559 | &(client.ssl), &(server.ssl), MBEDTLS_SSL_HANDSHAKE_WRAPUP) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5560 | == 0); |
Andrzej Kurek | 514683a | 2022-06-10 10:33:05 -0400 | [diff] [blame] | 5561 | /* Force a bad session_id_len that will be read by the server in |
| 5562 | * mbedtls_ssl_cache_set. */ |
| 5563 | server.ssl.session_negotiate->id_len = 33; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5564 | if (options.cli_msg_len != 0 || options.srv_msg_len != 0) { |
Andrzej Kurek | 514683a | 2022-06-10 10:33:05 -0400 | [diff] [blame] | 5565 | /* Start data exchanging test */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5566 | TEST_ASSERT(mbedtls_exchange_data(&(client.ssl), options.cli_msg_len, |
| 5567 | options.expected_cli_fragments, |
| 5568 | &(server.ssl), options.srv_msg_len, |
| 5569 | options.expected_srv_fragments) |
| 5570 | == 0); |
Andrzej Kurek | 514683a | 2022-06-10 10:33:05 -0400 | [diff] [blame] | 5571 | } |
Andrzej Kurek | 514683a | 2022-06-10 10:33:05 -0400 | [diff] [blame] | 5572 | |
| 5573 | /* Make sure that the cache did not store the session */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5574 | TEST_EQUAL(srv_pattern.counter, 1); |
Andrzej Kurek | 514683a | 2022-06-10 10:33:05 -0400 | [diff] [blame] | 5575 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 5576 | mbedtls_test_ssl_endpoint_free(&client, NULL); |
| 5577 | mbedtls_test_ssl_endpoint_free(&server, NULL); |
| 5578 | mbedtls_test_free_handshake_options(&options); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5579 | mbedtls_debug_set_threshold(0); |
| 5580 | USE_PSA_DONE(); |
Andrzej Kurek | 514683a | 2022-06-10 10:33:05 -0400 | [diff] [blame] | 5581 | } |
| 5582 | /* END_CASE */ |
| 5583 | |
Andrzej Kurek | ed4d217 | 2022-06-08 11:57:57 -0400 | [diff] [blame] | 5584 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_SRV_C:MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE:MBEDTLS_TEST_HOOKS */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5585 | void cookie_parsing(data_t *cookie, int exp_ret) |
Andrzej Kurek | cfb0194 | 2022-06-06 13:08:23 -0400 | [diff] [blame] | 5586 | { |
| 5587 | mbedtls_ssl_context ssl; |
| 5588 | mbedtls_ssl_config conf; |
| 5589 | size_t len; |
| 5590 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5591 | mbedtls_ssl_init(&ssl); |
| 5592 | mbedtls_ssl_config_init(&conf); |
| 5593 | TEST_EQUAL(mbedtls_ssl_config_defaults(&conf, MBEDTLS_SSL_IS_SERVER, |
| 5594 | MBEDTLS_SSL_TRANSPORT_DATAGRAM, |
| 5595 | MBEDTLS_SSL_PRESET_DEFAULT), |
| 5596 | 0); |
Andrzej Kurek | cfb0194 | 2022-06-06 13:08:23 -0400 | [diff] [blame] | 5597 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5598 | TEST_EQUAL(mbedtls_ssl_setup(&ssl, &conf), 0); |
| 5599 | TEST_EQUAL(mbedtls_ssl_check_dtls_clihlo_cookie(&ssl, ssl.cli_id, |
| 5600 | ssl.cli_id_len, |
| 5601 | cookie->x, cookie->len, |
| 5602 | ssl.out_buf, |
| 5603 | MBEDTLS_SSL_OUT_CONTENT_LEN, |
| 5604 | &len), |
| 5605 | exp_ret); |
Andrzej Kurek | cfb0194 | 2022-06-06 13:08:23 -0400 | [diff] [blame] | 5606 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5607 | mbedtls_ssl_free(&ssl); |
| 5608 | mbedtls_ssl_config_free(&conf); |
Andrzej Kurek | cfb0194 | 2022-06-06 13:08:23 -0400 | [diff] [blame] | 5609 | } |
| 5610 | /* END_CASE */ |
| 5611 | |
Paul Elliott | b9af2db | 2022-03-09 15:34:37 +0000 | [diff] [blame] | 5612 | /* BEGIN_CASE depends_on:MBEDTLS_TIMING_C:MBEDTLS_HAVE_TIME */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5613 | void timing_final_delay_accessor() |
Paul Elliott | b9af2db | 2022-03-09 15:34:37 +0000 | [diff] [blame] | 5614 | { |
| 5615 | mbedtls_timing_delay_context delay_context; |
| 5616 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5617 | mbedtls_timing_set_delay(&delay_context, 50, 100); |
Paul Elliott | b9af2db | 2022-03-09 15:34:37 +0000 | [diff] [blame] | 5618 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5619 | TEST_ASSERT(mbedtls_timing_get_final_delay(&delay_context) == 100); |
Paul Elliott | b9af2db | 2022-03-09 15:34:37 +0000 | [diff] [blame] | 5620 | } |
| 5621 | /* END_CASE */ |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 5622 | |
| 5623 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5624 | void cid_sanity() |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 5625 | { |
| 5626 | mbedtls_ssl_context ssl; |
| 5627 | mbedtls_ssl_config conf; |
| 5628 | |
| 5629 | unsigned char own_cid[MBEDTLS_SSL_CID_IN_LEN_MAX]; |
| 5630 | unsigned char test_cid[MBEDTLS_SSL_CID_IN_LEN_MAX]; |
| 5631 | int cid_enabled; |
| 5632 | size_t own_cid_len; |
| 5633 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5634 | mbedtls_test_rnd_std_rand(NULL, own_cid, sizeof(own_cid)); |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 5635 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5636 | mbedtls_ssl_init(&ssl); |
| 5637 | mbedtls_ssl_config_init(&conf); |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 5638 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5639 | TEST_ASSERT(mbedtls_ssl_config_defaults(&conf, |
| 5640 | MBEDTLS_SSL_IS_CLIENT, |
| 5641 | MBEDTLS_SSL_TRANSPORT_STREAM, |
| 5642 | MBEDTLS_SSL_PRESET_DEFAULT) |
| 5643 | == 0); |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 5644 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5645 | TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0); |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 5646 | |
| 5647 | /* Can't use CID functions with stream transport. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5648 | TEST_ASSERT(mbedtls_ssl_set_cid(&ssl, MBEDTLS_SSL_CID_ENABLED, own_cid, |
| 5649 | sizeof(own_cid)) |
| 5650 | == MBEDTLS_ERR_SSL_BAD_INPUT_DATA); |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 5651 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5652 | TEST_ASSERT(mbedtls_ssl_get_own_cid(&ssl, &cid_enabled, test_cid, |
| 5653 | &own_cid_len) |
| 5654 | == MBEDTLS_ERR_SSL_BAD_INPUT_DATA); |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 5655 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5656 | TEST_ASSERT(mbedtls_ssl_config_defaults(&conf, |
| 5657 | MBEDTLS_SSL_IS_CLIENT, |
| 5658 | MBEDTLS_SSL_TRANSPORT_DATAGRAM, |
| 5659 | MBEDTLS_SSL_PRESET_DEFAULT) |
| 5660 | == 0); |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 5661 | |
| 5662 | /* Attempt to set config cid size too big. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5663 | TEST_ASSERT(mbedtls_ssl_conf_cid(&conf, MBEDTLS_SSL_CID_IN_LEN_MAX + 1, |
| 5664 | MBEDTLS_SSL_UNEXPECTED_CID_IGNORE) |
| 5665 | == MBEDTLS_ERR_SSL_BAD_INPUT_DATA); |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 5666 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5667 | TEST_ASSERT(mbedtls_ssl_conf_cid(&conf, sizeof(own_cid), |
| 5668 | MBEDTLS_SSL_UNEXPECTED_CID_IGNORE) |
| 5669 | == 0); |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 5670 | |
| 5671 | /* Attempt to set CID length not matching config. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5672 | TEST_ASSERT(mbedtls_ssl_set_cid(&ssl, MBEDTLS_SSL_CID_ENABLED, own_cid, |
| 5673 | MBEDTLS_SSL_CID_IN_LEN_MAX - 1) |
| 5674 | == MBEDTLS_ERR_SSL_BAD_INPUT_DATA); |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 5675 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5676 | TEST_ASSERT(mbedtls_ssl_set_cid(&ssl, MBEDTLS_SSL_CID_ENABLED, own_cid, |
| 5677 | sizeof(own_cid)) |
| 5678 | == 0); |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 5679 | |
| 5680 | /* Test we get back what we put in. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5681 | TEST_ASSERT(mbedtls_ssl_get_own_cid(&ssl, &cid_enabled, test_cid, |
| 5682 | &own_cid_len) |
| 5683 | == 0); |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 5684 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5685 | TEST_EQUAL(cid_enabled, MBEDTLS_SSL_CID_ENABLED); |
| 5686 | ASSERT_COMPARE(own_cid, own_cid_len, test_cid, own_cid_len); |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 5687 | |
| 5688 | /* Test disabling works. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5689 | TEST_ASSERT(mbedtls_ssl_set_cid(&ssl, MBEDTLS_SSL_CID_DISABLED, NULL, |
| 5690 | 0) |
| 5691 | == 0); |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 5692 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5693 | TEST_ASSERT(mbedtls_ssl_get_own_cid(&ssl, &cid_enabled, test_cid, |
| 5694 | &own_cid_len) |
| 5695 | == 0); |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 5696 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5697 | TEST_EQUAL(cid_enabled, MBEDTLS_SSL_CID_DISABLED); |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 5698 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5699 | mbedtls_ssl_free(&ssl); |
| 5700 | mbedtls_ssl_config_free(&conf); |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 5701 | } |
| 5702 | /* END_CASE */ |
| 5703 | |
Valerio Setti | 2f08147 | 2023-02-23 17:36:06 +0100 | [diff] [blame] | 5704 | /* 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_PK_CAN_ECDSA_SOME */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5705 | void raw_key_agreement_fail(int bad_server_ecdhe_key) |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 5706 | { |
| 5707 | enum { BUFFSIZE = 17000 }; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 5708 | mbedtls_test_ssl_endpoint client, server; |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 5709 | mbedtls_psa_stats_t stats; |
Andrzej Kurek | 39d88d4 | 2022-03-31 06:30:54 -0400 | [diff] [blame] | 5710 | size_t free_slots_before = -1; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 5711 | mbedtls_test_handshake_test_options options; |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 5712 | |
Andrzej Kurek | cc28e9a | 2022-03-08 18:36:35 -0500 | [diff] [blame] | 5713 | uint16_t iana_tls_group_list[] = { MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, |
| 5714 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE }; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5715 | USE_PSA_INIT(); |
| 5716 | mbedtls_platform_zeroize(&client, sizeof(client)); |
| 5717 | mbedtls_platform_zeroize(&server, sizeof(server)); |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 5718 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 5719 | mbedtls_test_init_handshake_options(&options); |
Andrzej Kurek | 626a931 | 2022-06-10 11:07:39 -0400 | [diff] [blame] | 5720 | options.pk_alg = MBEDTLS_PK_ECDSA; |
| 5721 | |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 5722 | /* Client side, force SECP256R1 to make one key bitflip fail |
Andrzej Kurek | 83e60ee | 2022-04-14 08:51:41 -0400 | [diff] [blame] | 5723 | * the raw key agreement. Flipping the first byte makes the |
| 5724 | * required 0x04 identifier invalid. */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 5725 | TEST_EQUAL(mbedtls_test_ssl_endpoint_init(&client, MBEDTLS_SSL_IS_CLIENT, |
| 5726 | &options, NULL, NULL, |
| 5727 | NULL, iana_tls_group_list), 0); |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 5728 | |
| 5729 | /* Server side */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 5730 | TEST_EQUAL(mbedtls_test_ssl_endpoint_init(&server, MBEDTLS_SSL_IS_SERVER, |
| 5731 | &options, NULL, NULL, |
| 5732 | NULL, NULL), 0); |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 5733 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 5734 | TEST_EQUAL(mbedtls_test_mock_socket_connect(&(client.socket), |
| 5735 | &(server.socket), |
| 5736 | BUFFSIZE), 0); |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 5737 | |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 5738 | TEST_EQUAL(mbedtls_test_move_handshake_to_state( |
| 5739 | &(client.ssl), &(server.ssl), |
| 5740 | MBEDTLS_SSL_CLIENT_KEY_EXCHANGE), 0); |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 5741 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5742 | mbedtls_psa_get_stats(&stats); |
Andrzej Kurek | 39d88d4 | 2022-03-31 06:30:54 -0400 | [diff] [blame] | 5743 | /* Save the number of slots in use up to this point. |
| 5744 | * With PSA, one can be used for the ECDH private key. */ |
| 5745 | free_slots_before = stats.empty_slots; |
Andrzej Kurek | cb33bc5 | 2022-03-31 07:17:18 -0400 | [diff] [blame] | 5746 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5747 | if (bad_server_ecdhe_key) { |
Gilles Peskine | b4f874d | 2022-04-08 16:48:09 -0400 | [diff] [blame] | 5748 | /* Force a simulated bitflip in the server key. to make the |
| 5749 | * raw key agreement in ssl_write_client_key_exchange fail. */ |
| 5750 | (client.ssl).handshake->ecdh_psa_peerkey[0] ^= 0x02; |
| 5751 | } |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 5752 | |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 5753 | TEST_EQUAL(mbedtls_test_move_handshake_to_state( |
| 5754 | &(client.ssl), &(server.ssl), MBEDTLS_SSL_HANDSHAKE_OVER), |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5755 | bad_server_ecdhe_key ? MBEDTLS_ERR_SSL_HW_ACCEL_FAILED : 0); |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 5756 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5757 | mbedtls_psa_get_stats(&stats); |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 5758 | |
Gilles Peskine | b4f874d | 2022-04-08 16:48:09 -0400 | [diff] [blame] | 5759 | /* Make sure that the key slot is already destroyed in case of failure, |
| 5760 | * without waiting to close the connection. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5761 | if (bad_server_ecdhe_key) { |
| 5762 | TEST_EQUAL(free_slots_before, stats.empty_slots); |
| 5763 | } |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 5764 | |
| 5765 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 5766 | mbedtls_test_ssl_endpoint_free(&client, NULL); |
| 5767 | mbedtls_test_ssl_endpoint_free(&server, NULL); |
| 5768 | mbedtls_test_free_handshake_options(&options); |
Andrzej Kurek | cb33bc5 | 2022-03-31 07:17:18 -0400 | [diff] [blame] | 5769 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5770 | USE_PSA_DONE(); |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 5771 | } |
| 5772 | /* END_CASE */ |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5773 | /* 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 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5774 | void tls13_server_certificate_msg_invalid_vector_len() |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 5775 | { |
| 5776 | int ret = -1; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 5777 | mbedtls_test_ssl_endpoint client_ep, server_ep; |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 5778 | unsigned char *buf, *end; |
| 5779 | size_t buf_len; |
| 5780 | int step = 0; |
| 5781 | int expected_result; |
Ronald Cron | e7b9b6b | 2022-06-10 17:24:31 +0200 | [diff] [blame] | 5782 | mbedtls_ssl_chk_buf_ptr_args expected_chk_buf_ptr_args; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 5783 | mbedtls_test_handshake_test_options client_options; |
| 5784 | mbedtls_test_handshake_test_options server_options; |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 5785 | |
| 5786 | /* |
| 5787 | * Test set-up |
| 5788 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5789 | USE_PSA_INIT(); |
| 5790 | mbedtls_platform_zeroize(&client_ep, sizeof(client_ep)); |
| 5791 | mbedtls_platform_zeroize(&server_ep, sizeof(server_ep)); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 5792 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 5793 | mbedtls_test_init_handshake_options(&client_options); |
Paul Elliott | 9a8d784 | 2022-07-10 12:48:57 +0100 | [diff] [blame] | 5794 | client_options.pk_alg = MBEDTLS_PK_ECDSA; |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 5795 | ret = mbedtls_test_ssl_endpoint_init(&client_ep, MBEDTLS_SSL_IS_CLIENT, |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 5796 | &client_options, NULL, NULL, NULL, |
| 5797 | NULL); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5798 | TEST_EQUAL(ret, 0); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 5799 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 5800 | mbedtls_test_init_handshake_options(&server_options); |
Paul Elliott | 9a8d784 | 2022-07-10 12:48:57 +0100 | [diff] [blame] | 5801 | server_options.pk_alg = MBEDTLS_PK_ECDSA; |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 5802 | ret = mbedtls_test_ssl_endpoint_init(&server_ep, MBEDTLS_SSL_IS_SERVER, |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 5803 | &server_options, NULL, NULL, NULL, |
| 5804 | NULL); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5805 | TEST_EQUAL(ret, 0); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 5806 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 5807 | ret = mbedtls_test_mock_socket_connect(&(client_ep.socket), |
| 5808 | &(server_ep.socket), 1024); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5809 | TEST_EQUAL(ret, 0); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 5810 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5811 | while (1) { |
| 5812 | mbedtls_test_set_step(++step); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 5813 | |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 5814 | ret = mbedtls_test_move_handshake_to_state( |
| 5815 | &(server_ep.ssl), &(client_ep.ssl), |
| 5816 | MBEDTLS_SSL_CERTIFICATE_VERIFY); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5817 | TEST_EQUAL(ret, 0); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 5818 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5819 | ret = mbedtls_ssl_flush_output(&(server_ep.ssl)); |
| 5820 | TEST_EQUAL(ret, 0); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 5821 | |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 5822 | ret = mbedtls_test_move_handshake_to_state( |
| 5823 | &(client_ep.ssl), &(server_ep.ssl), |
| 5824 | MBEDTLS_SSL_SERVER_CERTIFICATE); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5825 | TEST_EQUAL(ret, 0); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 5826 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5827 | ret = mbedtls_ssl_tls13_fetch_handshake_msg(&(client_ep.ssl), |
| 5828 | MBEDTLS_SSL_HS_CERTIFICATE, |
| 5829 | &buf, &buf_len); |
| 5830 | TEST_EQUAL(ret, 0); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 5831 | |
| 5832 | end = buf + buf_len; |
| 5833 | |
| 5834 | /* |
| 5835 | * Tweak server Certificate message and parse it. |
| 5836 | */ |
| 5837 | |
| 5838 | ret = tweak_tls13_certificate_msg_vector_len( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5839 | buf, &end, step, &expected_result, &expected_chk_buf_ptr_args); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 5840 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5841 | if (ret != 0) { |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 5842 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5843 | } |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 5844 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5845 | ret = mbedtls_ssl_tls13_parse_certificate(&(client_ep.ssl), buf, end); |
| 5846 | TEST_EQUAL(ret, expected_result); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 5847 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5848 | TEST_ASSERT(mbedtls_ssl_cmp_chk_buf_ptr_fail_args( |
| 5849 | &expected_chk_buf_ptr_args) == 0); |
Ronald Cron | e7b9b6b | 2022-06-10 17:24:31 +0200 | [diff] [blame] | 5850 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5851 | mbedtls_ssl_reset_chk_buf_ptr_fail_args(); |
Ronald Cron | e7b9b6b | 2022-06-10 17:24:31 +0200 | [diff] [blame] | 5852 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5853 | ret = mbedtls_ssl_session_reset(&(client_ep.ssl)); |
| 5854 | TEST_EQUAL(ret, 0); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 5855 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5856 | ret = mbedtls_ssl_session_reset(&(server_ep.ssl)); |
| 5857 | TEST_EQUAL(ret, 0); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 5858 | } |
| 5859 | |
| 5860 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5861 | mbedtls_ssl_reset_chk_buf_ptr_fail_args(); |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 5862 | mbedtls_test_ssl_endpoint_free(&client_ep, NULL); |
| 5863 | mbedtls_test_ssl_endpoint_free(&server_ep, NULL); |
| 5864 | mbedtls_test_free_handshake_options(&client_options); |
| 5865 | mbedtls_test_free_handshake_options(&server_options); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5866 | USE_PSA_DONE(); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 5867 | } |
| 5868 | /* END_CASE */ |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 5869 | |
| 5870 | /* BEGIN_CASE depends_on:MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5871 | void ssl_ecjpake_set_password(int use_opaque_arg) |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 5872 | { |
| 5873 | mbedtls_ssl_context ssl; |
| 5874 | mbedtls_ssl_config conf; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5875 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 5876 | mbedtls_svc_key_id_t pwd_slot = MBEDTLS_SVC_KEY_ID_INIT; |
| 5877 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 5878 | (void) use_opaque_arg; |
| 5879 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5880 | unsigned char pwd_string[sizeof(ECJPAKE_TEST_PWD)] = ""; |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 5881 | size_t pwd_len = 0; |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 5882 | int ret; |
| 5883 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5884 | USE_PSA_INIT(); |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 5885 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5886 | mbedtls_ssl_init(&ssl); |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 5887 | |
Valerio Setti | e7518ba | 2022-12-02 12:09:43 +0100 | [diff] [blame] | 5888 | /* test with uninitalized SSL context */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5889 | ECJPAKE_TEST_SET_PASSWORD(MBEDTLS_ERR_SSL_BAD_INPUT_DATA); |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 5890 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5891 | mbedtls_ssl_config_init(&conf); |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 5892 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5893 | TEST_EQUAL(mbedtls_ssl_config_defaults(&conf, |
| 5894 | MBEDTLS_SSL_IS_CLIENT, |
| 5895 | MBEDTLS_SSL_TRANSPORT_STREAM, |
| 5896 | MBEDTLS_SSL_PRESET_DEFAULT), 0); |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 5897 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5898 | TEST_EQUAL(mbedtls_ssl_setup(&ssl, &conf), 0); |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 5899 | |
Valerio Setti | ba22c9c | 2022-12-06 11:42:33 +0100 | [diff] [blame] | 5900 | /* test with empty password or unitialized password key (depending on use_opaque_arg) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5901 | ECJPAKE_TEST_SET_PASSWORD(MBEDTLS_ERR_SSL_BAD_INPUT_DATA); |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 5902 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5903 | pwd_len = strlen(ECJPAKE_TEST_PWD); |
| 5904 | memcpy(pwd_string, ECJPAKE_TEST_PWD, pwd_len); |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 5905 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5906 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 5907 | if (use_opaque_arg) { |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 5908 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Valerio Setti | 785116a | 2022-12-12 11:59:25 +0100 | [diff] [blame] | 5909 | psa_key_attributes_t check_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 5910 | |
Valerio Setti | 2a3ffb4 | 2022-12-08 16:27:46 +0100 | [diff] [blame] | 5911 | /* First try with an invalid usage */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5912 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 5913 | psa_set_key_algorithm(&attributes, PSA_ALG_JPAKE); |
| 5914 | psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD); |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 5915 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5916 | PSA_ASSERT(psa_import_key(&attributes, pwd_string, |
| 5917 | pwd_len, &pwd_slot)); |
Valerio Setti | 2a3ffb4 | 2022-12-08 16:27:46 +0100 | [diff] [blame] | 5918 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5919 | ECJPAKE_TEST_SET_PASSWORD(MBEDTLS_ERR_SSL_HW_ACCEL_FAILED); |
Valerio Setti | 2a3ffb4 | 2022-12-08 16:27:46 +0100 | [diff] [blame] | 5920 | |
Valerio Setti | 9d313df | 2022-12-09 11:38:59 +0100 | [diff] [blame] | 5921 | /* check that the opaque key is still valid after failure */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5922 | TEST_EQUAL(psa_get_key_attributes(pwd_slot, &check_attributes), |
| 5923 | PSA_SUCCESS); |
Valerio Setti | 9d313df | 2022-12-09 11:38:59 +0100 | [diff] [blame] | 5924 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5925 | psa_destroy_key(pwd_slot); |
Valerio Setti | 2a3ffb4 | 2022-12-08 16:27:46 +0100 | [diff] [blame] | 5926 | |
| 5927 | /* Then set the correct usage */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5928 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
Valerio Setti | 2a3ffb4 | 2022-12-08 16:27:46 +0100 | [diff] [blame] | 5929 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5930 | PSA_ASSERT(psa_import_key(&attributes, pwd_string, |
| 5931 | pwd_len, &pwd_slot)); |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 5932 | } |
| 5933 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 5934 | |
Valerio Setti | e7518ba | 2022-12-02 12:09:43 +0100 | [diff] [blame] | 5935 | /* final check which should work without errors */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5936 | ECJPAKE_TEST_SET_PASSWORD(0); |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 5937 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5938 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 5939 | if (use_opaque_arg) { |
| 5940 | psa_destroy_key(pwd_slot); |
Valerio Setti | 31e99bb | 2022-12-09 14:35:10 +0100 | [diff] [blame] | 5941 | } |
Valerio Setti | e7518ba | 2022-12-02 12:09:43 +0100 | [diff] [blame] | 5942 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5943 | mbedtls_ssl_free(&ssl); |
| 5944 | mbedtls_ssl_config_free(&conf); |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 5945 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5946 | USE_PSA_DONE(); |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 5947 | } |
| 5948 | /* END_CASE */ |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 5949 | |
Valerio Setti | 2c12185 | 2023-01-09 18:00:39 +0100 | [diff] [blame] | 5950 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5951 | void elliptic_curve_get_properties() |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 5952 | { |
| 5953 | psa_ecc_family_t psa_family; |
| 5954 | size_t psa_bits; |
| 5955 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5956 | USE_PSA_INIT(); |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 5957 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5958 | #if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_521) |
| 5959 | TEST_AVAILABLE_ECC(25, MBEDTLS_ECP_DP_SECP521R1, PSA_ECC_FAMILY_SECP_R1, 521); |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 5960 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5961 | TEST_UNAVAILABLE_ECC(25, MBEDTLS_ECP_DP_SECP521R1, PSA_ECC_FAMILY_SECP_R1, 521); |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 5962 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5963 | #if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512) |
| 5964 | TEST_AVAILABLE_ECC(28, MBEDTLS_ECP_DP_BP512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 512); |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 5965 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5966 | TEST_UNAVAILABLE_ECC(28, MBEDTLS_ECP_DP_BP512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 512); |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 5967 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5968 | #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_384) |
| 5969 | TEST_AVAILABLE_ECC(24, MBEDTLS_ECP_DP_SECP384R1, PSA_ECC_FAMILY_SECP_R1, 384); |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 5970 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5971 | TEST_UNAVAILABLE_ECC(24, MBEDTLS_ECP_DP_SECP384R1, PSA_ECC_FAMILY_SECP_R1, 384); |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 5972 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5973 | #if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384) |
| 5974 | TEST_AVAILABLE_ECC(27, MBEDTLS_ECP_DP_BP384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 384); |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 5975 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5976 | TEST_UNAVAILABLE_ECC(27, MBEDTLS_ECP_DP_BP384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 384); |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 5977 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5978 | #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_256) |
| 5979 | TEST_AVAILABLE_ECC(23, MBEDTLS_ECP_DP_SECP256R1, PSA_ECC_FAMILY_SECP_R1, 256); |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 5980 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5981 | TEST_UNAVAILABLE_ECC(23, MBEDTLS_ECP_DP_SECP256R1, PSA_ECC_FAMILY_SECP_R1, 256); |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 5982 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5983 | #if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_256) |
| 5984 | TEST_AVAILABLE_ECC(22, MBEDTLS_ECP_DP_SECP256K1, PSA_ECC_FAMILY_SECP_K1, 256); |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 5985 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5986 | TEST_UNAVAILABLE_ECC(22, MBEDTLS_ECP_DP_SECP256K1, PSA_ECC_FAMILY_SECP_K1, 256); |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 5987 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5988 | #if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256) |
| 5989 | TEST_AVAILABLE_ECC(26, MBEDTLS_ECP_DP_BP256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 256); |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 5990 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5991 | TEST_UNAVAILABLE_ECC(26, MBEDTLS_ECP_DP_BP256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 256); |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 5992 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5993 | #if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_224) |
| 5994 | TEST_AVAILABLE_ECC(21, MBEDTLS_ECP_DP_SECP224R1, PSA_ECC_FAMILY_SECP_R1, 224); |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 5995 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5996 | TEST_UNAVAILABLE_ECC(21, MBEDTLS_ECP_DP_SECP224R1, PSA_ECC_FAMILY_SECP_R1, 224); |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 5997 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5998 | #if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_224) |
| 5999 | TEST_AVAILABLE_ECC(20, MBEDTLS_ECP_DP_SECP224K1, PSA_ECC_FAMILY_SECP_K1, 224); |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 6000 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6001 | TEST_UNAVAILABLE_ECC(20, MBEDTLS_ECP_DP_SECP224K1, PSA_ECC_FAMILY_SECP_K1, 224); |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 6002 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6003 | #if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_192) |
| 6004 | TEST_AVAILABLE_ECC(19, MBEDTLS_ECP_DP_SECP192R1, PSA_ECC_FAMILY_SECP_R1, 192); |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 6005 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6006 | TEST_UNAVAILABLE_ECC(19, MBEDTLS_ECP_DP_SECP192R1, PSA_ECC_FAMILY_SECP_R1, 192); |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 6007 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6008 | #if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_192) |
| 6009 | TEST_AVAILABLE_ECC(18, MBEDTLS_ECP_DP_SECP192K1, PSA_ECC_FAMILY_SECP_K1, 192); |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 6010 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6011 | TEST_UNAVAILABLE_ECC(18, MBEDTLS_ECP_DP_SECP192K1, PSA_ECC_FAMILY_SECP_K1, 192); |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 6012 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6013 | #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) || defined(PSA_WANT_ECC_MONTGOMERY_255) |
| 6014 | TEST_AVAILABLE_ECC(29, MBEDTLS_ECP_DP_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY, 255); |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 6015 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6016 | TEST_UNAVAILABLE_ECC(29, MBEDTLS_ECP_DP_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY, 255); |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 6017 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6018 | #if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED) || defined(PSA_WANT_ECC_MONTGOMERY_448) |
| 6019 | TEST_AVAILABLE_ECC(30, MBEDTLS_ECP_DP_CURVE448, PSA_ECC_FAMILY_MONTGOMERY, 448); |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 6020 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6021 | TEST_UNAVAILABLE_ECC(30, MBEDTLS_ECP_DP_CURVE448, PSA_ECC_FAMILY_MONTGOMERY, 448); |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 6022 | #endif |
| 6023 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6024 | USE_PSA_DONE(); |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 6025 | } |
| 6026 | /* END_CASE */ |