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 { |
| 29 | #define MBEDTLS_SSL_TLS1_3_LABEL(name, string) \ |
| 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 | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 50 | * this case, it will count the instances of a mbedtls_test_ssl_log_pattern in the received |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 51 | * logged messages. |
| 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 | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 70 | void mbedtls_test_init_handshake_options(mbedtls_test_handshake_test_options *opts) |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 71 | { |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 72 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 73 | srand(rng_seed); |
Andrzej Kurek | 635c2c2 | 2022-08-17 15:20:40 -0400 | [diff] [blame] | 74 | rng_seed += 0xD0; |
Andrzej Kurek | 32bd063 | 2022-08-20 14:10:36 -0400 | [diff] [blame] | 75 | #endif |
Andrzej Kurek | 3d0d501 | 2022-07-04 05:20:55 -0400 | [diff] [blame] | 76 | opts->cipher = ""; |
| 77 | opts->client_min_version = MBEDTLS_SSL_VERSION_UNKNOWN; |
| 78 | opts->client_max_version = MBEDTLS_SSL_VERSION_UNKNOWN; |
| 79 | opts->server_min_version = MBEDTLS_SSL_VERSION_UNKNOWN; |
| 80 | opts->server_max_version = MBEDTLS_SSL_VERSION_UNKNOWN; |
| 81 | opts->expected_negotiated_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 82 | opts->expected_handshake_result = 0; |
| 83 | opts->expected_ciphersuite = 0; |
| 84 | opts->pk_alg = MBEDTLS_PK_RSA; |
| 85 | opts->opaque_alg = 0; |
| 86 | opts->opaque_alg2 = 0; |
| 87 | opts->opaque_usage = 0; |
| 88 | opts->psk_str = NULL; |
| 89 | opts->dtls = 0; |
| 90 | opts->srv_auth_mode = MBEDTLS_SSL_VERIFY_NONE; |
| 91 | opts->serialize = 0; |
| 92 | opts->mfl = MBEDTLS_SSL_MAX_FRAG_LEN_NONE; |
| 93 | opts->cli_msg_len = 100; |
| 94 | opts->srv_msg_len = 100; |
| 95 | opts->expected_cli_fragments = 1; |
| 96 | opts->expected_srv_fragments = 1; |
| 97 | opts->renegotiate = 0; |
| 98 | opts->legacy_renegotiation = MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION; |
| 99 | opts->srv_log_obj = NULL; |
| 100 | opts->srv_log_obj = NULL; |
| 101 | opts->srv_log_fun = NULL; |
| 102 | opts->cli_log_fun = NULL; |
| 103 | opts->resize_buffers = 1; |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 104 | #if defined(MBEDTLS_SSL_CACHE_C) |
Andrzej Kurek | 9dc4402 | 2022-07-04 05:46:15 -0400 | [diff] [blame] | 105 | opts->cache = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 106 | ASSERT_ALLOC(opts->cache, 1); |
| 107 | mbedtls_ssl_cache_init(opts->cache); |
Andrzej Kurek | 3d0d501 | 2022-07-04 05:20:55 -0400 | [diff] [blame] | 108 | exit: |
| 109 | return; |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 110 | #endif |
| 111 | } |
| 112 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 113 | void mbedtls_test_free_handshake_options(mbedtls_test_handshake_test_options *opts) |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 114 | { |
| 115 | #if defined(MBEDTLS_SSL_CACHE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 116 | mbedtls_ssl_cache_free(opts->cache); |
| 117 | mbedtls_free(opts->cache); |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 118 | #else |
| 119 | (void) opts; |
| 120 | #endif |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 121 | } |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 122 | |
Ronald Cron | e7b9b6b | 2022-06-10 17:24:31 +0200 | [diff] [blame] | 123 | #if defined(MBEDTLS_TEST_HOOKS) |
| 124 | static void set_chk_buf_ptr_args( |
| 125 | mbedtls_ssl_chk_buf_ptr_args *args, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 126 | unsigned char *cur, unsigned char *end, size_t need) |
Ronald Cron | e7b9b6b | 2022-06-10 17:24:31 +0200 | [diff] [blame] | 127 | { |
| 128 | args->cur = cur; |
| 129 | args->end = end; |
| 130 | args->need = need; |
| 131 | } |
| 132 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 133 | 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] | 134 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 135 | memset(args, 0, sizeof(*args)); |
Ronald Cron | e7b9b6b | 2022-06-10 17:24:31 +0200 | [diff] [blame] | 136 | } |
| 137 | #endif /* MBEDTLS_TEST_HOOKS */ |
| 138 | |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 139 | /* |
| 140 | * Buffer structure for custom I/O callbacks. |
| 141 | */ |
| 142 | |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 143 | /* |
| 144 | * Initialises \p buf. After calling this function it is safe to call |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 145 | * `mbedtls_test_ssl_buffer_free()` on \p buf. |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 146 | */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 147 | void mbedtls_test_ssl_buffer_init(mbedtls_test_ssl_buffer *buf) |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 148 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 149 | memset(buf, 0, sizeof(*buf)); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | /* |
| 153 | * Sets up \p buf. After calling this function it is safe to call |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 154 | * `mbedtls_test_ssl_buffer_put()` and `mbedtls_test_ssl_buffer_get()` on \p buf. |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 155 | */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 156 | int mbedtls_test_ssl_buffer_setup(mbedtls_test_ssl_buffer *buf, size_t capacity) |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 157 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 158 | buf->buffer = (unsigned char *) mbedtls_calloc(capacity, |
| 159 | sizeof(unsigned char)); |
| 160 | if (NULL == buf->buffer) { |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 161 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 162 | } |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 163 | buf->capacity = capacity; |
| 164 | |
| 165 | return 0; |
| 166 | } |
| 167 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 168 | void mbedtls_test_ssl_buffer_free(mbedtls_test_ssl_buffer *buf) |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 169 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 170 | if (buf->buffer != NULL) { |
| 171 | mbedtls_free(buf->buffer); |
| 172 | } |
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 | memset(buf, 0, sizeof(*buf)); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | /* |
| 178 | * Puts \p input_len bytes from the \p input buffer into the ring buffer \p buf. |
| 179 | * |
| 180 | * \p buf must have been initialized and set up by calling |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 181 | * `mbedtls_test_ssl_buffer_init()` and `mbedtls_test_ssl_buffer_setup()`. |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 182 | * |
| 183 | * \retval \p input_len, if the data fits. |
| 184 | * \retval 0 <= value < \p input_len, if the data does not fit. |
| 185 | * \retval -1, if \p buf is NULL, it hasn't been set up or \p input_len is not |
| 186 | * zero and \p input is NULL. |
| 187 | */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 188 | int mbedtls_test_ssl_buffer_put(mbedtls_test_ssl_buffer *buf, |
| 189 | const unsigned char *input, size_t input_len) |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 190 | { |
| 191 | size_t overflow = 0; |
| 192 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 193 | if ((buf == NULL) || (buf->buffer == NULL)) { |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 194 | return -1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 195 | } |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 196 | |
| 197 | /* Reduce input_len to a number that fits in the buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 198 | if ((buf->content_length + input_len) > buf->capacity) { |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 199 | input_len = buf->capacity - buf->content_length; |
| 200 | } |
| 201 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 202 | if (input == NULL) { |
| 203 | return (input_len == 0) ? 0 : -1; |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 206 | /* Check if the buffer has not come full circle and free space is not in |
| 207 | * the middle */ |
| 208 | if (buf->start + buf->content_length < buf->capacity) { |
Piotr Nowicki | fb437d7 | 2020-01-13 16:59:12 +0100 | [diff] [blame] | 209 | |
| 210 | /* 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] | 211 | * address */ |
| 212 | if (buf->start + buf->content_length + input_len |
| 213 | > buf->capacity) { |
| 214 | overflow = (buf->start + buf->content_length + input_len) |
| 215 | % buf->capacity; |
Piotr Nowicki | fb437d7 | 2020-01-13 16:59:12 +0100 | [diff] [blame] | 216 | } |
| 217 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 218 | memcpy(buf->buffer + buf->start + buf->content_length, input, |
| 219 | input_len - overflow); |
| 220 | memcpy(buf->buffer, input + input_len - overflow, overflow); |
Piotr Nowicki | fb437d7 | 2020-01-13 16:59:12 +0100 | [diff] [blame] | 221 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 222 | } else { |
Piotr Nowicki | fb437d7 | 2020-01-13 16:59:12 +0100 | [diff] [blame] | 223 | /* 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] | 224 | memcpy(buf->buffer + buf->start + buf->content_length - buf->capacity, |
| 225 | input, input_len); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 228 | buf->content_length += input_len; |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 229 | return input_len; |
| 230 | } |
| 231 | |
| 232 | /* |
Andrzej Kurek | f777414 | 2020-01-22 06:34:59 -0500 | [diff] [blame] | 233 | * Gets \p output_len bytes from the ring buffer \p buf into the |
| 234 | * \p output buffer. The output buffer can be NULL, in this case a part of the |
| 235 | * ring buffer will be dropped, if the requested length is available. |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 236 | * |
| 237 | * \p buf must have been initialized and set up by calling |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 238 | * `mbedtls_test_ssl_buffer_init()` and `mbedtls_test_ssl_buffer_setup()`. |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 239 | * |
| 240 | * \retval \p output_len, if the data is available. |
| 241 | * \retval 0 <= value < \p output_len, if the data is not available. |
Andrzej Kurek | f777414 | 2020-01-22 06:34:59 -0500 | [diff] [blame] | 242 | * \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] | 243 | */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 244 | int mbedtls_test_ssl_buffer_get(mbedtls_test_ssl_buffer *buf, |
| 245 | unsigned char *output, size_t output_len) |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 246 | { |
| 247 | size_t overflow = 0; |
| 248 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 249 | if ((buf == NULL) || (buf->buffer == NULL)) { |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 250 | return -1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 251 | } |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 252 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 253 | if (output == NULL && output_len == 0) { |
Andrzej Kurek | f777414 | 2020-01-22 06:34:59 -0500 | [diff] [blame] | 254 | return 0; |
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 (buf->content_length < output_len) { |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 258 | output_len = buf->content_length; |
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 | |
| 261 | /* Calculate the number of bytes that need to be drawn from lower memory |
| 262 | * address */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 263 | if (buf->start + output_len > buf->capacity) { |
| 264 | overflow = (buf->start + output_len) % buf->capacity; |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 265 | } |
| 266 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 267 | if (output != NULL) { |
| 268 | memcpy(output, buf->buffer + buf->start, output_len - overflow); |
| 269 | memcpy(output + output_len - overflow, buf->buffer, overflow); |
Andrzej Kurek | f777414 | 2020-01-22 06:34:59 -0500 | [diff] [blame] | 270 | } |
| 271 | |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 272 | buf->content_length -= output_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 273 | buf->start = (buf->start + output_len) % buf->capacity; |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 274 | |
| 275 | return output_len; |
| 276 | } |
| 277 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 278 | /* |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 279 | * Errors used in the message transport mock tests |
| 280 | */ |
| 281 | #define MBEDTLS_TEST_ERROR_ARG_NULL -11 |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 282 | #define MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED -44 |
| 283 | |
| 284 | /* |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 285 | * Setup and free functions for the message metadata queue. |
| 286 | * |
| 287 | * \p capacity describes the number of message metadata chunks that can be held |
| 288 | * within the queue. |
| 289 | * |
| 290 | * \retval 0, if a metadata queue of a given length can be allocated. |
| 291 | * \retval MBEDTLS_ERR_SSL_ALLOC_FAILED, if allocation failed. |
| 292 | */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 293 | int mbedtls_test_ssl_message_queue_setup(mbedtls_test_ssl_message_queue *queue, |
| 294 | size_t capacity) |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 295 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 296 | queue->messages = (size_t *) mbedtls_calloc(capacity, sizeof(size_t)); |
| 297 | if (NULL == queue->messages) { |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 298 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 299 | } |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 300 | |
| 301 | queue->capacity = capacity; |
| 302 | queue->pos = 0; |
| 303 | queue->num = 0; |
| 304 | |
| 305 | return 0; |
| 306 | } |
| 307 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 308 | 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] | 309 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 310 | if (queue == NULL) { |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 311 | return; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 312 | } |
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->messages != NULL) { |
| 315 | mbedtls_free(queue->messages); |
| 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 | memset(queue, 0, sizeof(*queue)); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | /* |
| 322 | * Push message length information onto the message metadata queue. |
| 323 | * This will become the last element to leave it (fifo). |
| 324 | * |
| 325 | * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null. |
Andrzej Kurek | f46b912 | 2020-02-07 08:19:00 -0500 | [diff] [blame] | 326 | * \retval MBEDTLS_ERR_SSL_WANT_WRITE, if the queue is full. |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 327 | * \retval \p len, if the push was successful. |
| 328 | */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 329 | int mbedtls_test_ssl_message_queue_push_info(mbedtls_test_ssl_message_queue *queue, |
| 330 | size_t len) |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 331 | { |
| 332 | int place; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 333 | if (queue == NULL) { |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 334 | return MBEDTLS_TEST_ERROR_ARG_NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 335 | } |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 336 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 337 | if (queue->num >= queue->capacity) { |
Andrzej Kurek | f46b912 | 2020-02-07 08:19:00 -0500 | [diff] [blame] | 338 | return MBEDTLS_ERR_SSL_WANT_WRITE; |
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 | place = (queue->pos + queue->num) % queue->capacity; |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 342 | queue->messages[place] = len; |
| 343 | queue->num++; |
| 344 | return len; |
| 345 | } |
| 346 | |
| 347 | /* |
| 348 | * Pop information about the next message length from the queue. This will be |
| 349 | * the oldest inserted message length(fifo). \p msg_len can be null, in which |
| 350 | * case the data will be popped from the queue but not copied anywhere. |
| 351 | * |
| 352 | * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null. |
Andrzej Kurek | f46b912 | 2020-02-07 08:19:00 -0500 | [diff] [blame] | 353 | * \retval MBEDTLS_ERR_SSL_WANT_READ, if the queue is empty. |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 354 | * \retval message length, if the pop was successful, up to the given |
| 355 | \p buf_len. |
| 356 | */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 357 | int mbedtls_test_ssl_message_queue_pop_info(mbedtls_test_ssl_message_queue *queue, |
| 358 | size_t buf_len) |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 359 | { |
| 360 | size_t message_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 361 | if (queue == NULL) { |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 362 | return MBEDTLS_TEST_ERROR_ARG_NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 363 | } |
| 364 | if (queue->num == 0) { |
Andrzej Kurek | f46b912 | 2020-02-07 08:19:00 -0500 | [diff] [blame] | 365 | return MBEDTLS_ERR_SSL_WANT_READ; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 366 | } |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 367 | |
| 368 | message_length = queue->messages[queue->pos]; |
| 369 | queue->messages[queue->pos] = 0; |
| 370 | queue->num--; |
| 371 | queue->pos++; |
| 372 | queue->pos %= queue->capacity; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 373 | if (queue->pos < 0) { |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 374 | queue->pos += queue->capacity; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 375 | } |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 376 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 377 | return (message_length > buf_len) ? buf_len : message_length; |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | /* |
| 381 | * Take a peek on the info about the next message length from the queue. |
| 382 | * This will be the oldest inserted message length(fifo). |
| 383 | * |
| 384 | * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null. |
Andrzej Kurek | f46b912 | 2020-02-07 08:19:00 -0500 | [diff] [blame] | 385 | * \retval MBEDTLS_ERR_SSL_WANT_READ, if the queue is empty. |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 386 | * \retval 0, if the peek was successful. |
| 387 | * \retval MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED, if the given buffer length is |
| 388 | * too small to fit the message. In this case the \p msg_len will be |
| 389 | * set to the full message length so that the |
| 390 | * caller knows what portion of the message can be dropped. |
| 391 | */ |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 392 | 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] | 393 | size_t buf_len, size_t *msg_len) |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 394 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 395 | if (queue == NULL || msg_len == NULL) { |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 396 | return MBEDTLS_TEST_ERROR_ARG_NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 397 | } |
| 398 | if (queue->num == 0) { |
Andrzej Kurek | f46b912 | 2020-02-07 08:19:00 -0500 | [diff] [blame] | 399 | return MBEDTLS_ERR_SSL_WANT_READ; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 400 | } |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 401 | |
| 402 | *msg_len = queue->messages[queue->pos]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 403 | return (*msg_len > buf_len) ? MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED : 0; |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 404 | } |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 405 | |
| 406 | /* |
| 407 | * Setup and teardown functions for mock sockets. |
| 408 | */ |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 409 | void mbedtls_mock_socket_init(mbedtls_test_mock_socket *socket) |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 410 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 411 | memset(socket, 0, sizeof(*socket)); |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | /* |
| 415 | * Closes the socket \p socket. |
| 416 | * |
| 417 | * \p socket must have been previously initialized by calling |
| 418 | * mbedtls_mock_socket_init(). |
| 419 | * |
| 420 | * This function frees all allocated resources and both sockets are aware of the |
| 421 | * new connection state. |
| 422 | * |
| 423 | * That is, this function does not simulate half-open TCP connections and the |
| 424 | * phenomenon that when closing a UDP connection the peer is not aware of the |
| 425 | * connection having been closed. |
| 426 | */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 427 | void mbedtls_test_mock_socket_close(mbedtls_test_mock_socket *socket) |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 428 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 429 | if (socket == NULL) { |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 430 | return; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 431 | } |
| 432 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 433 | if (socket->input != NULL) { |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 434 | mbedtls_test_ssl_buffer_free(socket->input); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 435 | mbedtls_free(socket->input); |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 436 | } |
| 437 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 438 | if (socket->output != NULL) { |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 439 | mbedtls_test_ssl_buffer_free(socket->output); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 440 | mbedtls_free(socket->output); |
| 441 | } |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 442 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 443 | if (socket->peer != NULL) { |
| 444 | memset(socket->peer, 0, sizeof(*socket->peer)); |
| 445 | } |
| 446 | |
| 447 | memset(socket, 0, sizeof(*socket)); |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | /* |
| 451 | * Establishes a connection between \p peer1 and \p peer2. |
| 452 | * |
| 453 | * \p peer1 and \p peer2 must have been previously initialized by calling |
| 454 | * mbedtls_mock_socket_init(). |
| 455 | * |
Tom Cosgrove | 1797b05 | 2022-12-04 17:19:59 +0000 | [diff] [blame] | 456 | * 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] | 457 | * the correct value allows for simulation of MTU, sanity testing the mock |
| 458 | * implementation and mocking TCP connections with lower memory cost. |
| 459 | */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 460 | int mbedtls_test_mock_socket_connect(mbedtls_test_mock_socket *peer1, |
| 461 | mbedtls_test_mock_socket *peer2, |
| 462 | size_t bufsize) |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 463 | { |
| 464 | int ret = -1; |
| 465 | |
Piotr Nowicki | d796e19 | 2020-01-28 12:09:47 +0100 | [diff] [blame] | 466 | peer1->output = |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 467 | (mbedtls_test_ssl_buffer *) mbedtls_calloc(1, sizeof(mbedtls_test_ssl_buffer)); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 468 | if (peer1->output == NULL) { |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 469 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 470 | goto exit; |
| 471 | } |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 472 | mbedtls_test_ssl_buffer_init(peer1->output); |
| 473 | if (0 != (ret = mbedtls_test_ssl_buffer_setup(peer1->output, bufsize))) { |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 474 | goto exit; |
| 475 | } |
| 476 | |
Piotr Nowicki | d796e19 | 2020-01-28 12:09:47 +0100 | [diff] [blame] | 477 | peer2->output = |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 478 | (mbedtls_test_ssl_buffer *) mbedtls_calloc(1, sizeof(mbedtls_test_ssl_buffer)); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 479 | if (peer2->output == NULL) { |
Piotr Nowicki | d796e19 | 2020-01-28 12:09:47 +0100 | [diff] [blame] | 480 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 481 | goto exit; |
| 482 | } |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 483 | mbedtls_test_ssl_buffer_init(peer2->output); |
| 484 | if (0 != (ret = mbedtls_test_ssl_buffer_setup(peer2->output, bufsize))) { |
Piotr Nowicki | d796e19 | 2020-01-28 12:09:47 +0100 | [diff] [blame] | 485 | goto exit; |
| 486 | } |
| 487 | |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 488 | peer1->peer = peer2; |
| 489 | peer2->peer = peer1; |
Piotr Nowicki | d796e19 | 2020-01-28 12:09:47 +0100 | [diff] [blame] | 490 | peer1->input = peer2->output; |
| 491 | peer2->input = peer1->output; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 492 | |
| 493 | peer1->status = peer2->status = MBEDTLS_MOCK_SOCKET_CONNECTED; |
| 494 | ret = 0; |
| 495 | |
| 496 | exit: |
| 497 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 498 | if (ret != 0) { |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 499 | mbedtls_test_mock_socket_close(peer1); |
| 500 | mbedtls_test_mock_socket_close(peer2); |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | return ret; |
| 504 | } |
| 505 | |
| 506 | /* |
| 507 | * Callbacks for simulating blocking I/O over connection-oriented transport. |
| 508 | */ |
| 509 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 510 | int mbedtls_test_mock_tcp_send_b(void *ctx, const unsigned char *buf, size_t len) |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 511 | { |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 512 | mbedtls_test_mock_socket *socket = (mbedtls_test_mock_socket *) ctx; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 513 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 514 | if (socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED) { |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 515 | return -1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 516 | } |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 517 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 518 | return mbedtls_test_ssl_buffer_put(socket->output, buf, len); |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 519 | } |
| 520 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 521 | 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] | 522 | { |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 523 | mbedtls_test_mock_socket *socket = (mbedtls_test_mock_socket *) ctx; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 524 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 525 | if (socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED) { |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 526 | return -1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 527 | } |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 528 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 529 | return mbedtls_test_ssl_buffer_get(socket->input, buf, len); |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | /* |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 533 | * Callbacks for simulating non-blocking I/O over connection-oriented transport. |
| 534 | */ |
| 535 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 536 | int mbedtls_test_mock_tcp_send_nb(void *ctx, const unsigned char *buf, size_t len) |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 537 | { |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 538 | mbedtls_test_mock_socket *socket = (mbedtls_test_mock_socket *) ctx; |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 539 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 540 | if (socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED) { |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 541 | return -1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 542 | } |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 543 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 544 | if (socket->output->capacity == socket->output->content_length) { |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 545 | return MBEDTLS_ERR_SSL_WANT_WRITE; |
| 546 | } |
| 547 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 548 | return mbedtls_test_ssl_buffer_put(socket->output, buf, len); |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 549 | } |
| 550 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 551 | 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] | 552 | { |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 553 | mbedtls_test_mock_socket *socket = (mbedtls_test_mock_socket *) ctx; |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 554 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 555 | if (socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED) { |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 556 | return -1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 557 | } |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 558 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 559 | if (socket->input->content_length == 0) { |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 560 | return MBEDTLS_ERR_SSL_WANT_READ; |
| 561 | } |
| 562 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 563 | return mbedtls_test_ssl_buffer_get(socket->input, buf, len); |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 564 | } |
| 565 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 566 | void mbedtls_test_message_socket_init(mbedtls_test_message_socket_context *ctx) |
Andrzej Kurek | 45916ba | 2020-03-05 14:46:22 -0500 | [diff] [blame] | 567 | { |
| 568 | ctx->queue_input = NULL; |
| 569 | ctx->queue_output = NULL; |
| 570 | ctx->socket = NULL; |
| 571 | } |
| 572 | |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 573 | /* |
Tom Cosgrove | 1797b05 | 2022-12-04 17:19:59 +0000 | [diff] [blame] | 574 | * Setup a given message socket context including initialization of |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 575 | * input/output queues to a chosen capacity of messages. Also set the |
| 576 | * corresponding mock socket. |
| 577 | * |
| 578 | * \retval 0, if everything succeeds. |
| 579 | * \retval MBEDTLS_ERR_SSL_ALLOC_FAILED, if allocation of a message |
| 580 | * queue failed. |
| 581 | */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 582 | int mbedtls_test_message_socket_setup(mbedtls_test_ssl_message_queue *queue_input, |
| 583 | mbedtls_test_ssl_message_queue *queue_output, |
| 584 | size_t queue_capacity, |
| 585 | mbedtls_test_mock_socket *socket, |
| 586 | mbedtls_test_message_socket_context *ctx) |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 587 | { |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 588 | int ret = mbedtls_test_ssl_message_queue_setup(queue_input, queue_capacity); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 589 | if (ret != 0) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 590 | return ret; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 591 | } |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 592 | ctx->queue_input = queue_input; |
| 593 | ctx->queue_output = queue_output; |
| 594 | ctx->socket = socket; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 595 | mbedtls_mock_socket_init(socket); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 596 | |
| 597 | return 0; |
| 598 | } |
| 599 | |
| 600 | /* |
| 601 | * Close a given message socket context, along with the socket itself. Free the |
| 602 | * memory allocated by the input queue. |
| 603 | */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 604 | void mbedtls_test_message_socket_close(mbedtls_test_message_socket_context *ctx) |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 605 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 606 | if (ctx == NULL) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 607 | return; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 608 | } |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 609 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 610 | mbedtls_test_ssl_message_queue_free(ctx->queue_input); |
| 611 | mbedtls_test_mock_socket_close(ctx->socket); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 612 | memset(ctx, 0, sizeof(*ctx)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | /* |
| 616 | * Send one message through a given message socket context. |
| 617 | * |
| 618 | * \retval \p len, if everything succeeds. |
| 619 | * \retval MBEDTLS_TEST_ERROR_CONTEXT_ERROR, if any of the needed context |
| 620 | * elements or the context itself is null. |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 621 | * \retval MBEDTLS_TEST_ERROR_SEND_FAILED if mbedtls_test_mock_tcp_send_b failed. |
Andrzej Kurek | f46b912 | 2020-02-07 08:19:00 -0500 | [diff] [blame] | 622 | * \retval MBEDTLS_ERR_SSL_WANT_WRITE, if the output queue is full. |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 623 | * |
| 624 | * This function will also return any error from |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 625 | * mbedtls_test_ssl_message_queue_push_info. |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 626 | */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 627 | int mbedtls_test_mock_tcp_send_msg(void *ctx, const unsigned char *buf, size_t len) |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 628 | { |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 629 | mbedtls_test_ssl_message_queue *queue; |
| 630 | mbedtls_test_mock_socket *socket; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 631 | mbedtls_test_message_socket_context *context = (mbedtls_test_message_socket_context *) ctx; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 632 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 633 | if (context == NULL || context->socket == NULL |
| 634 | || context->queue_output == NULL) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 635 | return MBEDTLS_TEST_ERROR_CONTEXT_ERROR; |
| 636 | } |
| 637 | |
| 638 | queue = context->queue_output; |
| 639 | socket = context->socket; |
| 640 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 641 | if (queue->num >= queue->capacity) { |
Andrzej Kurek | f46b912 | 2020-02-07 08:19:00 -0500 | [diff] [blame] | 642 | return MBEDTLS_ERR_SSL_WANT_WRITE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 643 | } |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 644 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 645 | if (mbedtls_test_mock_tcp_send_b(socket, buf, len) != (int) len) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 646 | return MBEDTLS_TEST_ERROR_SEND_FAILED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 647 | } |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 648 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 649 | return mbedtls_test_ssl_message_queue_push_info(queue, len); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | /* |
| 653 | * Receive one message from a given message socket context and return message |
| 654 | * length or an error. |
| 655 | * |
| 656 | * \retval message length, if everything succeeds. |
| 657 | * \retval MBEDTLS_TEST_ERROR_CONTEXT_ERROR, if any of the needed context |
| 658 | * elements or the context itself is null. |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 659 | * \retval MBEDTLS_TEST_ERROR_RECV_FAILED if mbedtls_test_mock_tcp_recv_b failed. |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 660 | * |
| 661 | * This function will also return any error other than |
| 662 | * MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED from mbedtls_test_message_queue_peek_info. |
| 663 | */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 664 | int mbedtls_test_mock_tcp_recv_msg(void *ctx, unsigned char *buf, size_t buf_len) |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 665 | { |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 666 | mbedtls_test_ssl_message_queue *queue; |
| 667 | mbedtls_test_mock_socket *socket; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 668 | mbedtls_test_message_socket_context *context = (mbedtls_test_message_socket_context *) ctx; |
Gilles Peskine | 19e841e | 2020-03-09 20:43:51 +0100 | [diff] [blame] | 669 | size_t drop_len = 0; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 670 | size_t msg_len; |
| 671 | int ret; |
| 672 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 673 | if (context == NULL || context->socket == NULL |
| 674 | || context->queue_input == NULL) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 675 | return MBEDTLS_TEST_ERROR_CONTEXT_ERROR; |
| 676 | } |
| 677 | |
| 678 | queue = context->queue_input; |
| 679 | socket = context->socket; |
| 680 | |
| 681 | /* Peek first, so that in case of a socket error the data remains in |
| 682 | * the queue. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 683 | ret = mbedtls_test_message_queue_peek_info(queue, buf_len, &msg_len); |
| 684 | if (ret == MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 685 | /* Calculate how much to drop */ |
| 686 | drop_len = msg_len - buf_len; |
| 687 | |
| 688 | /* Set the requested message len to be buffer length */ |
| 689 | msg_len = buf_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 690 | } else if (ret != 0) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 691 | return ret; |
| 692 | } |
| 693 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 694 | 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] | 695 | return MBEDTLS_TEST_ERROR_RECV_FAILED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 696 | } |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 697 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 698 | if (ret == MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 699 | /* Drop the remaining part of the message */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 700 | if (mbedtls_test_mock_tcp_recv_b(socket, NULL, drop_len) != (int) drop_len) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 701 | /* Inconsistent state - part of the message was read, |
| 702 | * and a part couldn't. Not much we can do here, but it should not |
| 703 | * happen in test environment, unless forced manually. */ |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 704 | } |
| 705 | } |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 706 | mbedtls_test_ssl_message_queue_pop_info(queue, buf_len); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 707 | |
| 708 | return msg_len; |
| 709 | } |
| 710 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 711 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 712 | |
| 713 | /* |
Andrzej Kurek | 0abebeb | 2022-09-30 12:54:41 -0400 | [diff] [blame] | 714 | * Deinitializes certificates from endpoint represented by \p ep. |
| 715 | */ |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 716 | void mbedtls_endpoint_certificate_free(mbedtls_test_ssl_endpoint *ep) |
Andrzej Kurek | 0abebeb | 2022-09-30 12:54:41 -0400 | [diff] [blame] | 717 | { |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 718 | mbedtls_test_ssl_endpoint_certificate *cert = &(ep->cert); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 719 | if (cert != NULL) { |
| 720 | if (cert->ca_cert != NULL) { |
| 721 | mbedtls_x509_crt_free(cert->ca_cert); |
| 722 | mbedtls_free(cert->ca_cert); |
Andrzej Kurek | 0abebeb | 2022-09-30 12:54:41 -0400 | [diff] [blame] | 723 | cert->ca_cert = NULL; |
| 724 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 725 | if (cert->cert != NULL) { |
| 726 | mbedtls_x509_crt_free(cert->cert); |
| 727 | mbedtls_free(cert->cert); |
Andrzej Kurek | 0abebeb | 2022-09-30 12:54:41 -0400 | [diff] [blame] | 728 | cert->cert = NULL; |
| 729 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 730 | if (cert->pkey != NULL) { |
Andrzej Kurek | 0abebeb | 2022-09-30 12:54:41 -0400 | [diff] [blame] | 731 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 732 | if (mbedtls_pk_get_type(cert->pkey) == MBEDTLS_PK_OPAQUE) { |
Andrzej Kurek | 0abebeb | 2022-09-30 12:54:41 -0400 | [diff] [blame] | 733 | mbedtls_svc_key_id_t *key_slot = cert->pkey->pk_ctx; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 734 | psa_destroy_key(*key_slot); |
Andrzej Kurek | 0abebeb | 2022-09-30 12:54:41 -0400 | [diff] [blame] | 735 | } |
| 736 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 737 | mbedtls_pk_free(cert->pkey); |
| 738 | mbedtls_free(cert->pkey); |
Andrzej Kurek | 0abebeb | 2022-09-30 12:54:41 -0400 | [diff] [blame] | 739 | cert->pkey = NULL; |
| 740 | } |
| 741 | } |
| 742 | } |
| 743 | |
| 744 | /* |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 745 | * Initializes \p ep_cert structure and assigns it to endpoint |
| 746 | * represented by \p ep. |
| 747 | * |
| 748 | * \retval 0 on success, otherwise error code. |
| 749 | */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 750 | int mbedtls_test_ssl_endpoint_certificate_init(mbedtls_test_ssl_endpoint *ep, int pk_alg, |
| 751 | int opaque_alg, int opaque_alg2, |
| 752 | int opaque_usage) |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 753 | { |
| 754 | int i = 0; |
| 755 | int ret = -1; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 756 | mbedtls_test_ssl_endpoint_certificate *cert = NULL; |
Neil Armstrong | 8c52ed8 | 2022-05-27 13:14:55 +0200 | [diff] [blame] | 757 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 758 | mbedtls_svc_key_id_t key_slot = MBEDTLS_SVC_KEY_ID_INIT; |
| 759 | #endif |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 760 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 761 | if (ep == NULL) { |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 762 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 763 | } |
| 764 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 765 | cert = &(ep->cert); |
| 766 | ASSERT_ALLOC(cert->ca_cert, 1); |
| 767 | ASSERT_ALLOC(cert->cert, 1); |
| 768 | ASSERT_ALLOC(cert->pkey, 1); |
Andrzej Kurek | 0abebeb | 2022-09-30 12:54:41 -0400 | [diff] [blame] | 769 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 770 | mbedtls_x509_crt_init(cert->ca_cert); |
| 771 | mbedtls_x509_crt_init(cert->cert); |
| 772 | mbedtls_pk_init(cert->pkey); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 773 | |
| 774 | /* Load the trusted CA */ |
| 775 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 776 | for (i = 0; mbedtls_test_cas_der[i] != NULL; i++) { |
| 777 | ret = mbedtls_x509_crt_parse_der(cert->ca_cert, |
| 778 | (const unsigned char *) mbedtls_test_cas_der[i], |
| 779 | mbedtls_test_cas_der_len[i]); |
| 780 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | /* Load own certificate and private key */ |
| 784 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 785 | if (ep->conf.endpoint == MBEDTLS_SSL_IS_SERVER) { |
| 786 | if (pk_alg == MBEDTLS_PK_RSA) { |
| 787 | ret = mbedtls_x509_crt_parse(cert->cert, |
| 788 | (const unsigned char *) mbedtls_test_srv_crt_rsa_sha256_der, |
| 789 | mbedtls_test_srv_crt_rsa_sha256_der_len); |
| 790 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 791 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 792 | ret = mbedtls_pk_parse_key(cert->pkey, |
| 793 | (const unsigned char *) mbedtls_test_srv_key_rsa_der, |
| 794 | mbedtls_test_srv_key_rsa_der_len, NULL, 0, |
| 795 | mbedtls_test_rnd_std_rand, NULL); |
| 796 | TEST_ASSERT(ret == 0); |
| 797 | } else { |
| 798 | ret = mbedtls_x509_crt_parse(cert->cert, |
| 799 | (const unsigned char *) mbedtls_test_srv_crt_ec_der, |
| 800 | mbedtls_test_srv_crt_ec_der_len); |
| 801 | TEST_ASSERT(ret == 0); |
| 802 | |
| 803 | ret = mbedtls_pk_parse_key(cert->pkey, |
| 804 | (const unsigned char *) mbedtls_test_srv_key_ec_der, |
| 805 | mbedtls_test_srv_key_ec_der_len, NULL, 0, |
| 806 | mbedtls_test_rnd_std_rand, NULL); |
| 807 | TEST_ASSERT(ret == 0); |
Andrzej Kurek | b298074 | 2020-02-02 19:25:26 -0500 | [diff] [blame] | 808 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 809 | } else { |
| 810 | if (pk_alg == MBEDTLS_PK_RSA) { |
| 811 | ret = mbedtls_x509_crt_parse(cert->cert, |
| 812 | (const unsigned char *) mbedtls_test_cli_crt_rsa_der, |
| 813 | mbedtls_test_cli_crt_rsa_der_len); |
| 814 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 815 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 816 | ret = mbedtls_pk_parse_key(cert->pkey, |
| 817 | (const unsigned char *) mbedtls_test_cli_key_rsa_der, |
| 818 | mbedtls_test_cli_key_rsa_der_len, NULL, 0, |
| 819 | mbedtls_test_rnd_std_rand, NULL); |
| 820 | TEST_ASSERT(ret == 0); |
| 821 | } else { |
| 822 | ret = mbedtls_x509_crt_parse(cert->cert, |
| 823 | (const unsigned char *) mbedtls_test_cli_crt_ec_der, |
| 824 | mbedtls_test_cli_crt_ec_len); |
| 825 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 826 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 827 | ret = mbedtls_pk_parse_key(cert->pkey, |
| 828 | (const unsigned char *) mbedtls_test_cli_key_ec_der, |
| 829 | mbedtls_test_cli_key_ec_der_len, NULL, 0, |
| 830 | mbedtls_test_rnd_std_rand, NULL); |
| 831 | TEST_ASSERT(ret == 0); |
Andrzej Kurek | b298074 | 2020-02-02 19:25:26 -0500 | [diff] [blame] | 832 | } |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 833 | } |
| 834 | |
Neil Armstrong | 8c52ed8 | 2022-05-27 13:14:55 +0200 | [diff] [blame] | 835 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 836 | if (opaque_alg != 0) { |
| 837 | TEST_EQUAL(mbedtls_pk_wrap_as_opaque(cert->pkey, &key_slot, |
| 838 | opaque_alg, opaque_usage, |
| 839 | opaque_alg2), 0); |
Neil Armstrong | 8c52ed8 | 2022-05-27 13:14:55 +0200 | [diff] [blame] | 840 | } |
| 841 | #else |
| 842 | (void) opaque_alg; |
| 843 | (void) opaque_alg2; |
| 844 | (void) opaque_usage; |
| 845 | #endif |
| 846 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 847 | mbedtls_ssl_conf_ca_chain(&(ep->conf), cert->ca_cert, NULL); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 848 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 849 | ret = mbedtls_ssl_conf_own_cert(&(ep->conf), cert->cert, |
| 850 | cert->pkey); |
| 851 | TEST_ASSERT(ret == 0); |
| 852 | TEST_ASSERT(ep->conf.key_cert != NULL); |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 853 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 854 | ret = mbedtls_ssl_conf_own_cert(&(ep->conf), NULL, NULL); |
| 855 | TEST_ASSERT(ret == 0); |
| 856 | TEST_ASSERT(ep->conf.key_cert == NULL); |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 857 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 858 | ret = mbedtls_ssl_conf_own_cert(&(ep->conf), cert->cert, |
| 859 | cert->pkey); |
| 860 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 861 | |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 862 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 863 | if (ret != 0) { |
| 864 | mbedtls_endpoint_certificate_free(ep); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 865 | } |
| 866 | |
| 867 | return ret; |
| 868 | } |
| 869 | |
| 870 | /* |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 871 | * Initializes \p ep structure. It is important to call `mbedtls_test_ssl_endpoint_free()` |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 872 | * after calling this function even if it fails. |
| 873 | * |
| 874 | * \p endpoint_type must be set as MBEDTLS_SSL_IS_SERVER or |
| 875 | * MBEDTLS_SSL_IS_CLIENT. |
Andrzej Kurek | 15daf50 | 2020-02-12 09:17:52 -0500 | [diff] [blame] | 876 | * \p pk_alg the algorithm to use, currently only MBEDTLS_PK_RSA and |
| 877 | * MBEDTLS_PK_ECDSA are supported. |
| 878 | * \p dtls_context - in case of DTLS - this is the context handling metadata. |
| 879 | * \p input_queue - used only in case of DTLS. |
| 880 | * \p output_queue - used only in case of DTLS. |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 881 | * |
| 882 | * \retval 0 on success, otherwise error code. |
| 883 | */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 884 | int mbedtls_test_ssl_endpoint_init(mbedtls_test_ssl_endpoint *ep, int endpoint_type, |
| 885 | mbedtls_test_handshake_test_options *options, |
| 886 | mbedtls_test_message_socket_context *dtls_context, |
| 887 | mbedtls_test_ssl_message_queue *input_queue, |
| 888 | mbedtls_test_ssl_message_queue *output_queue, |
| 889 | uint16_t *group_list) |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 890 | { |
| 891 | int ret = -1; |
Gilles Peskine | 80dae04 | 2022-01-21 23:50:39 +0100 | [diff] [blame] | 892 | uintptr_t user_data_n; |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 893 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 894 | if (dtls_context != NULL && (input_queue == NULL || output_queue == NULL)) { |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 895 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Andrzej Kurek | 15daf50 | 2020-02-12 09:17:52 -0500 | [diff] [blame] | 896 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 897 | |
| 898 | if (ep == NULL) { |
| 899 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 900 | } |
| 901 | |
| 902 | memset(ep, 0, sizeof(*ep)); |
| 903 | |
| 904 | ep->name = (endpoint_type == MBEDTLS_SSL_IS_SERVER) ? "Server" : "Client"; |
| 905 | |
| 906 | mbedtls_ssl_init(&(ep->ssl)); |
| 907 | mbedtls_ssl_config_init(&(ep->conf)); |
| 908 | mbedtls_ssl_conf_rng(&(ep->conf), rng_get, NULL); |
| 909 | |
| 910 | TEST_ASSERT(mbedtls_ssl_conf_get_user_data_p(&ep->conf) == NULL); |
| 911 | TEST_EQUAL(mbedtls_ssl_conf_get_user_data_n(&ep->conf), 0); |
| 912 | TEST_ASSERT(mbedtls_ssl_get_user_data_p(&ep->ssl) == NULL); |
| 913 | TEST_EQUAL(mbedtls_ssl_get_user_data_n(&ep->ssl), 0); |
| 914 | |
| 915 | (void) mbedtls_test_rnd_std_rand(NULL, |
| 916 | (void *) &user_data_n, |
| 917 | sizeof(user_data_n)); |
| 918 | mbedtls_ssl_conf_set_user_data_n(&ep->conf, user_data_n); |
| 919 | mbedtls_ssl_set_user_data_n(&ep->ssl, user_data_n); |
| 920 | |
| 921 | if (dtls_context != NULL) { |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 922 | TEST_ASSERT(mbedtls_test_message_socket_setup(input_queue, output_queue, |
| 923 | 100, &(ep->socket), |
| 924 | dtls_context) == 0); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 925 | } else { |
| 926 | mbedtls_mock_socket_init(&(ep->socket)); |
Andrzej Kurek | 15daf50 | 2020-02-12 09:17:52 -0500 | [diff] [blame] | 927 | } |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 928 | |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 929 | /* Non-blocking callbacks without timeout */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 930 | if (dtls_context != NULL) { |
| 931 | mbedtls_ssl_set_bio(&(ep->ssl), dtls_context, |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 932 | mbedtls_test_mock_tcp_send_msg, |
| 933 | mbedtls_test_mock_tcp_recv_msg, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 934 | NULL); |
| 935 | } else { |
| 936 | mbedtls_ssl_set_bio(&(ep->ssl), &(ep->socket), |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 937 | mbedtls_test_mock_tcp_send_nb, |
| 938 | mbedtls_test_mock_tcp_recv_nb, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 939 | NULL); |
Andrzej Kurek | 15daf50 | 2020-02-12 09:17:52 -0500 | [diff] [blame] | 940 | } |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 941 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 942 | ret = mbedtls_ssl_config_defaults(&(ep->conf), endpoint_type, |
| 943 | (dtls_context != NULL) ? |
| 944 | MBEDTLS_SSL_TRANSPORT_DATAGRAM : |
| 945 | MBEDTLS_SSL_TRANSPORT_STREAM, |
| 946 | MBEDTLS_SSL_PRESET_DEFAULT); |
| 947 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 948 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 949 | if (group_list != NULL) { |
| 950 | mbedtls_ssl_conf_groups(&(ep->conf), group_list); |
| 951 | } |
Andrzej Kurek | 74394a5 | 2022-03-08 06:50:12 -0500 | [diff] [blame] | 952 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 953 | mbedtls_ssl_conf_authmode(&(ep->conf), MBEDTLS_SSL_VERIFY_REQUIRED); |
Ronald Cron | bdddaef | 2022-06-07 10:34:59 +0200 | [diff] [blame] | 954 | |
Andrzej Kurek | ed58b50 | 2022-06-10 19:24:05 -0400 | [diff] [blame] | 955 | #if defined(MBEDTLS_SSL_CACHE_C) && defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 956 | if (endpoint_type == MBEDTLS_SSL_IS_SERVER && options->cache != NULL) { |
| 957 | mbedtls_ssl_conf_session_cache(&(ep->conf), options->cache, |
| 958 | mbedtls_ssl_cache_get, |
| 959 | mbedtls_ssl_cache_set); |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 960 | } |
| 961 | #endif |
| 962 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 963 | ret = mbedtls_ssl_setup(&(ep->ssl), &(ep->conf)); |
| 964 | TEST_ASSERT(ret == 0); |
Andrzej Kurek | 15daf50 | 2020-02-12 09:17:52 -0500 | [diff] [blame] | 965 | |
| 966 | #if defined(MBEDTLS_SSL_PROTO_DTLS) && defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 967 | if (endpoint_type == MBEDTLS_SSL_IS_SERVER && dtls_context != NULL) { |
| 968 | mbedtls_ssl_conf_dtls_cookies(&(ep->conf), NULL, NULL, NULL); |
| 969 | } |
Andrzej Kurek | 15daf50 | 2020-02-12 09:17:52 -0500 | [diff] [blame] | 970 | #endif |
| 971 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 972 | ret = mbedtls_test_ssl_endpoint_certificate_init(ep, options->pk_alg, |
| 973 | options->opaque_alg, |
| 974 | options->opaque_alg2, |
| 975 | options->opaque_usage); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 976 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 977 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 978 | TEST_EQUAL(mbedtls_ssl_conf_get_user_data_n(&ep->conf), user_data_n); |
| 979 | mbedtls_ssl_conf_set_user_data_p(&ep->conf, ep); |
| 980 | TEST_EQUAL(mbedtls_ssl_get_user_data_n(&ep->ssl), user_data_n); |
| 981 | mbedtls_ssl_set_user_data_p(&ep->ssl, ep); |
Gilles Peskine | 80dae04 | 2022-01-21 23:50:39 +0100 | [diff] [blame] | 982 | |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 983 | exit: |
| 984 | return ret; |
| 985 | } |
| 986 | |
| 987 | /* |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 988 | * Deinitializes endpoint represented by \p ep. |
| 989 | */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 990 | void mbedtls_test_ssl_endpoint_free(mbedtls_test_ssl_endpoint *ep, |
| 991 | mbedtls_test_message_socket_context *context) |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 992 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 993 | mbedtls_endpoint_certificate_free(ep); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 994 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 995 | mbedtls_ssl_free(&(ep->ssl)); |
| 996 | mbedtls_ssl_config_free(&(ep->conf)); |
Andrzej Kurek | 15daf50 | 2020-02-12 09:17:52 -0500 | [diff] [blame] | 997 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 998 | if (context != NULL) { |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 999 | mbedtls_test_message_socket_close(context); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1000 | } else { |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 1001 | mbedtls_test_mock_socket_close(&(ep->socket)); |
Andrzej Kurek | 15daf50 | 2020-02-12 09:17:52 -0500 | [diff] [blame] | 1002 | } |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 1003 | } |
| 1004 | |
| 1005 | /* |
| 1006 | * This function moves ssl handshake from \p ssl to prescribed \p state. |
| 1007 | * /p second_ssl is used as second endpoint and their sockets have to be |
| 1008 | * connected before calling this function. |
| 1009 | * |
| 1010 | * \retval 0 on success, otherwise error code. |
| 1011 | */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 1012 | int mbedtls_test_move_handshake_to_state(mbedtls_ssl_context *ssl, |
| 1013 | mbedtls_ssl_context *second_ssl, |
| 1014 | int state) |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 1015 | { |
| 1016 | enum { BUFFSIZE = 1024 }; |
| 1017 | int max_steps = 1000; |
| 1018 | int ret = 0; |
| 1019 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1020 | if (ssl == NULL || second_ssl == NULL) { |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 1021 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 1022 | } |
| 1023 | |
| 1024 | /* Perform communication via connected sockets */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1025 | while ((ssl->state != state) && (--max_steps >= 0)) { |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 1026 | /* If /p second_ssl ends the handshake procedure before /p ssl then |
| 1027 | * there is no need to call the next step */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1028 | if (!mbedtls_ssl_is_handshake_over(second_ssl)) { |
| 1029 | ret = mbedtls_ssl_handshake_step(second_ssl); |
| 1030 | if (ret != 0 && ret != MBEDTLS_ERR_SSL_WANT_READ && |
| 1031 | ret != MBEDTLS_ERR_SSL_WANT_WRITE) { |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 1032 | return ret; |
| 1033 | } |
| 1034 | } |
| 1035 | |
| 1036 | /* We only care about the \p ssl state and returns, so we call it last, |
| 1037 | * to leave the iteration as soon as the state is as expected. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1038 | ret = mbedtls_ssl_handshake_step(ssl); |
| 1039 | if (ret != 0 && ret != MBEDTLS_ERR_SSL_WANT_READ && |
| 1040 | ret != MBEDTLS_ERR_SSL_WANT_WRITE) { |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 1041 | return ret; |
| 1042 | } |
| 1043 | } |
| 1044 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1045 | return (max_steps >= 0) ? ret : -1; |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 1046 | } |
| 1047 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 1048 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 1049 | |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 1050 | /* |
Piotr Nowicki | 438bf3b | 2020-03-10 12:59:10 +0100 | [diff] [blame] | 1051 | * Write application data. Increase write counter if necessary. |
Piotr Nowicki | c3fca5e | 2020-01-30 15:33:42 +0100 | [diff] [blame] | 1052 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1053 | int mbedtls_ssl_write_fragment(mbedtls_ssl_context *ssl, unsigned char *buf, |
| 1054 | int buf_len, int *written, |
| 1055 | const int expected_fragments) |
Piotr Nowicki | c3fca5e | 2020-01-30 15:33:42 +0100 | [diff] [blame] | 1056 | { |
Dave Rodgman | a4e8fb0 | 2023-02-24 15:41:55 +0000 | [diff] [blame] | 1057 | /* Verify that calling mbedtls_ssl_write with a NULL buffer and zero length is |
| 1058 | * a valid no-op for TLS connections. */ |
| 1059 | if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 1060 | TEST_ASSERT(mbedtls_ssl_write(ssl, NULL, 0) == 0); |
| 1061 | } |
| 1062 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1063 | int ret = mbedtls_ssl_write(ssl, buf + *written, buf_len - *written); |
| 1064 | if (ret > 0) { |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1065 | *written += ret; |
Piotr Nowicki | c3fca5e | 2020-01-30 15:33:42 +0100 | [diff] [blame] | 1066 | } |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1067 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1068 | if (expected_fragments == 0) { |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1069 | /* Used for DTLS and the message size larger than MFL. In that case |
| 1070 | * the message can not be fragmented and the library should return |
| 1071 | * MBEDTLS_ERR_SSL_BAD_INPUT_DATA error. This error must be returned |
| 1072 | * to prevent a dead loop inside mbedtls_exchange_data(). */ |
| 1073 | return ret; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1074 | } else if (expected_fragments == 1) { |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1075 | /* Used for TLS/DTLS and the message size lower than MFL */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1076 | TEST_ASSERT(ret == buf_len || |
| 1077 | ret == MBEDTLS_ERR_SSL_WANT_READ || |
| 1078 | ret == MBEDTLS_ERR_SSL_WANT_WRITE); |
| 1079 | } else { |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1080 | /* Used for TLS and the message size larger than MFL */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1081 | TEST_ASSERT(expected_fragments > 1); |
| 1082 | TEST_ASSERT((ret >= 0 && ret <= buf_len) || |
| 1083 | ret == MBEDTLS_ERR_SSL_WANT_READ || |
| 1084 | ret == MBEDTLS_ERR_SSL_WANT_WRITE); |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1085 | } |
| 1086 | |
| 1087 | return 0; |
| 1088 | |
| 1089 | exit: |
| 1090 | /* Some of the tests failed */ |
| 1091 | return -1; |
Piotr Nowicki | c3fca5e | 2020-01-30 15:33:42 +0100 | [diff] [blame] | 1092 | } |
| 1093 | |
| 1094 | /* |
Piotr Nowicki | 438bf3b | 2020-03-10 12:59:10 +0100 | [diff] [blame] | 1095 | * Read application data and increase read counter and fragments counter if necessary. |
Piotr Nowicki | c3fca5e | 2020-01-30 15:33:42 +0100 | [diff] [blame] | 1096 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1097 | int mbedtls_ssl_read_fragment(mbedtls_ssl_context *ssl, unsigned char *buf, |
| 1098 | int buf_len, int *read, |
| 1099 | int *fragments, const int expected_fragments) |
Piotr Nowicki | c3fca5e | 2020-01-30 15:33:42 +0100 | [diff] [blame] | 1100 | { |
Dave Rodgman | a4e8fb0 | 2023-02-24 15:41:55 +0000 | [diff] [blame] | 1101 | /* Verify that calling mbedtls_ssl_write with a NULL buffer and zero length is |
| 1102 | * a valid no-op for TLS connections. */ |
| 1103 | if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 1104 | TEST_ASSERT(mbedtls_ssl_read(ssl, NULL, 0) == 0); |
| 1105 | } |
| 1106 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1107 | int ret = mbedtls_ssl_read(ssl, buf + *read, buf_len - *read); |
| 1108 | if (ret > 0) { |
| 1109 | (*fragments)++; |
Piotr Nowicki | c3fca5e | 2020-01-30 15:33:42 +0100 | [diff] [blame] | 1110 | *read += ret; |
| 1111 | } |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1112 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1113 | if (expected_fragments == 0) { |
| 1114 | TEST_ASSERT(ret == 0); |
| 1115 | } else if (expected_fragments == 1) { |
| 1116 | TEST_ASSERT(ret == buf_len || |
| 1117 | ret == MBEDTLS_ERR_SSL_WANT_READ || |
| 1118 | ret == MBEDTLS_ERR_SSL_WANT_WRITE); |
| 1119 | } else { |
| 1120 | TEST_ASSERT(expected_fragments > 1); |
| 1121 | TEST_ASSERT((ret >= 0 && ret <= buf_len) || |
| 1122 | ret == MBEDTLS_ERR_SSL_WANT_READ || |
| 1123 | ret == MBEDTLS_ERR_SSL_WANT_WRITE); |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1124 | } |
| 1125 | |
| 1126 | return 0; |
| 1127 | |
| 1128 | exit: |
| 1129 | /* Some of the tests failed */ |
| 1130 | return -1; |
Piotr Nowicki | c3fca5e | 2020-01-30 15:33:42 +0100 | [diff] [blame] | 1131 | } |
| 1132 | |
| 1133 | /* |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1134 | * Helper function setting up inverse record transformations |
| 1135 | * using given cipher, hash, EtM mode, authentication tag length, |
| 1136 | * and version. |
| 1137 | */ |
| 1138 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1139 | #define CHK(x) \ |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1140 | do \ |
| 1141 | { \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1142 | if (!(x)) \ |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 1143 | { \ |
Hanno Becker | a5780f1 | 2019-04-05 09:55:37 +0100 | [diff] [blame] | 1144 | ret = -1; \ |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 1145 | goto cleanup; \ |
| 1146 | } \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1147 | } while (0) |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1148 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1149 | void set_ciphersuite(mbedtls_ssl_config *conf, const char *cipher, |
| 1150 | int *forced_ciphersuite) |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 1151 | { |
| 1152 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1153 | forced_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id(cipher); |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 1154 | forced_ciphersuite[1] = 0; |
| 1155 | |
| 1156 | ciphersuite_info = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1157 | mbedtls_ssl_ciphersuite_from_id(forced_ciphersuite[0]); |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 1158 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1159 | TEST_ASSERT(ciphersuite_info != NULL); |
| 1160 | TEST_ASSERT(ciphersuite_info->min_tls_version <= conf->max_tls_version); |
| 1161 | TEST_ASSERT(ciphersuite_info->max_tls_version >= conf->min_tls_version); |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 1162 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1163 | if (conf->max_tls_version > ciphersuite_info->max_tls_version) { |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 1164 | conf->max_tls_version = ciphersuite_info->max_tls_version; |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 1165 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1166 | if (conf->min_tls_version < ciphersuite_info->min_tls_version) { |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 1167 | conf->min_tls_version = ciphersuite_info->min_tls_version; |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 1168 | } |
| 1169 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1170 | mbedtls_ssl_conf_ciphersuites(conf, forced_ciphersuite); |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 1171 | |
| 1172 | exit: |
| 1173 | return; |
| 1174 | } |
| 1175 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1176 | int psk_dummy_callback(void *p_info, mbedtls_ssl_context *ssl, |
| 1177 | const unsigned char *name, size_t name_len) |
Andrzej Kurek | cc5169c | 2020-02-04 09:04:56 -0500 | [diff] [blame] | 1178 | { |
| 1179 | (void) p_info; |
| 1180 | (void) ssl; |
| 1181 | (void) name; |
| 1182 | (void) name_len; |
| 1183 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1184 | return 0; |
Andrzej Kurek | cc5169c | 2020-02-04 09:04:56 -0500 | [diff] [blame] | 1185 | } |
| 1186 | |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 1187 | #if MBEDTLS_SSL_CID_OUT_LEN_MAX > MBEDTLS_SSL_CID_IN_LEN_MAX |
| 1188 | #define SSL_CID_LEN_MIN MBEDTLS_SSL_CID_IN_LEN_MAX |
| 1189 | #else |
| 1190 | #define SSL_CID_LEN_MIN MBEDTLS_SSL_CID_OUT_LEN_MAX |
| 1191 | #endif |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1192 | |
Gilles Peskine | 9c656ec | 2022-02-26 19:55:58 +0100 | [diff] [blame] | 1193 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
| 1194 | defined(MBEDTLS_CIPHER_MODE_CBC) && defined(MBEDTLS_AES_C) |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 1195 | static int mbedtls_test_psa_cipher_encrypt_helper(mbedtls_ssl_transform *transform, |
| 1196 | const unsigned char *iv, size_t iv_len, |
| 1197 | const unsigned char *input, size_t ilen, |
| 1198 | unsigned char *output, size_t *olen) |
Przemyslaw Stekiel | f4facef | 2022-02-02 21:31:04 +0100 | [diff] [blame] | 1199 | { |
Przemyslaw Stekiel | 5648d57 | 2022-02-03 14:09:02 +0100 | [diff] [blame] | 1200 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Przemyslaw Stekiel | d66387f | 2022-02-03 08:55:33 +0100 | [diff] [blame] | 1201 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Przemyslaw Stekiel | f4facef | 2022-02-02 21:31:04 +0100 | [diff] [blame] | 1202 | psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT; |
| 1203 | size_t part_len; |
| 1204 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1205 | status = psa_cipher_encrypt_setup(&cipher_op, |
| 1206 | transform->psa_key_enc, transform->psa_alg); |
Przemyslaw Stekiel | f4facef | 2022-02-02 21:31:04 +0100 | [diff] [blame] | 1207 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1208 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 1209 | return PSA_TO_MBEDTLS_ERR(status); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1210 | } |
Przemyslaw Stekiel | f4facef | 2022-02-02 21:31:04 +0100 | [diff] [blame] | 1211 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1212 | status = psa_cipher_set_iv(&cipher_op, iv, iv_len); |
Przemyslaw Stekiel | f4facef | 2022-02-02 21:31:04 +0100 | [diff] [blame] | 1213 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1214 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 1215 | return PSA_TO_MBEDTLS_ERR(status); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1216 | } |
Przemyslaw Stekiel | f4facef | 2022-02-02 21:31:04 +0100 | [diff] [blame] | 1217 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1218 | status = psa_cipher_update(&cipher_op, |
| 1219 | input, ilen, output, ilen, olen); |
Przemyslaw Stekiel | f4facef | 2022-02-02 21:31:04 +0100 | [diff] [blame] | 1220 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1221 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 1222 | return PSA_TO_MBEDTLS_ERR(status); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1223 | } |
Przemyslaw Stekiel | f4facef | 2022-02-02 21:31:04 +0100 | [diff] [blame] | 1224 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1225 | status = psa_cipher_finish(&cipher_op, |
| 1226 | output + *olen, ilen - *olen, &part_len); |
Przemyslaw Stekiel | f4facef | 2022-02-02 21:31:04 +0100 | [diff] [blame] | 1227 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1228 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 1229 | return PSA_TO_MBEDTLS_ERR(status); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1230 | } |
Przemyslaw Stekiel | f4facef | 2022-02-02 21:31:04 +0100 | [diff] [blame] | 1231 | |
| 1232 | *olen += part_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1233 | return 0; |
Przemyslaw Stekiel | 5648d57 | 2022-02-03 14:09:02 +0100 | [diff] [blame] | 1234 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1235 | return mbedtls_cipher_crypt(&transform->cipher_ctx_enc, |
| 1236 | iv, iv_len, input, ilen, output, olen); |
Przemyslaw Stekiel | f4facef | 2022-02-02 21:31:04 +0100 | [diff] [blame] | 1237 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Przemyslaw Stekiel | 5648d57 | 2022-02-03 14:09:02 +0100 | [diff] [blame] | 1238 | } |
Gilles Peskine | 9c656ec | 2022-02-26 19:55:58 +0100 | [diff] [blame] | 1239 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_CIPHER_MODE_CBC && MBEDTLS_AES_C */ |
Przemyslaw Stekiel | f4facef | 2022-02-02 21:31:04 +0100 | [diff] [blame] | 1240 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 1241 | static int mbedtls_test_ssl_build_transforms(mbedtls_ssl_transform *t_in, |
| 1242 | mbedtls_ssl_transform *t_out, |
| 1243 | int cipher_type, int hash_id, |
| 1244 | int etm, int tag_mode, |
| 1245 | mbedtls_ssl_protocol_version tls_version, |
| 1246 | size_t cid0_len, |
| 1247 | size_t cid1_len) |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1248 | { |
| 1249 | mbedtls_cipher_info_t const *cipher_info; |
Hanno Becker | a5780f1 | 2019-04-05 09:55:37 +0100 | [diff] [blame] | 1250 | int ret = 0; |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1251 | |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1252 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1253 | psa_key_type_t key_type; |
| 1254 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1255 | psa_algorithm_t alg; |
| 1256 | size_t key_bits; |
Przemyslaw Stekiel | d66387f | 2022-02-03 08:55:33 +0100 | [diff] [blame] | 1257 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1258 | #endif |
| 1259 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1260 | size_t keylen, maclen, ivlen; |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 1261 | unsigned char *key0 = NULL, *key1 = NULL; |
Paul Elliott | 6f1eda7 | 2020-06-11 20:22:00 +0100 | [diff] [blame] | 1262 | unsigned char *md0 = NULL, *md1 = NULL; |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1263 | unsigned char iv_enc[16], iv_dec[16]; |
| 1264 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1265 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1266 | unsigned char cid0[SSL_CID_LEN_MIN]; |
| 1267 | unsigned char cid1[SSL_CID_LEN_MIN]; |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 1268 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1269 | mbedtls_test_rnd_std_rand(NULL, cid0, sizeof(cid0)); |
| 1270 | mbedtls_test_rnd_std_rand(NULL, cid1, sizeof(cid1)); |
Hanno Becker | 43c24b8 | 2019-05-01 09:45:57 +0100 | [diff] [blame] | 1271 | #else |
| 1272 | ((void) cid0_len); |
| 1273 | ((void) cid1_len); |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1274 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 1275 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1276 | maclen = 0; |
| 1277 | |
| 1278 | /* Pick cipher */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1279 | cipher_info = mbedtls_cipher_info_from_type(cipher_type); |
| 1280 | CHK(cipher_info != NULL); |
| 1281 | CHK(cipher_info->iv_size <= 16); |
| 1282 | CHK(cipher_info->key_bitlen % 8 == 0); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1283 | |
| 1284 | /* Pick keys */ |
| 1285 | keylen = cipher_info->key_bitlen / 8; |
Hanno Becker | 78d1f70 | 2019-04-05 09:56:10 +0100 | [diff] [blame] | 1286 | /* Allocate `keylen + 1` bytes to ensure that we get |
| 1287 | * a non-NULL pointers from `mbedtls_calloc` even if |
| 1288 | * `keylen == 0` in the case of the NULL cipher. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1289 | CHK((key0 = mbedtls_calloc(1, keylen + 1)) != NULL); |
| 1290 | CHK((key1 = mbedtls_calloc(1, keylen + 1)) != NULL); |
| 1291 | memset(key0, 0x1, keylen); |
| 1292 | memset(key1, 0x2, keylen); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1293 | |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1294 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1295 | /* Setup cipher contexts */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1296 | CHK(mbedtls_cipher_setup(&t_in->cipher_ctx_enc, cipher_info) == 0); |
| 1297 | CHK(mbedtls_cipher_setup(&t_in->cipher_ctx_dec, cipher_info) == 0); |
| 1298 | CHK(mbedtls_cipher_setup(&t_out->cipher_ctx_enc, cipher_info) == 0); |
| 1299 | CHK(mbedtls_cipher_setup(&t_out->cipher_ctx_dec, cipher_info) == 0); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1300 | |
| 1301 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1302 | if (cipher_info->mode == MBEDTLS_MODE_CBC) { |
| 1303 | CHK(mbedtls_cipher_set_padding_mode(&t_in->cipher_ctx_enc, |
| 1304 | MBEDTLS_PADDING_NONE) == 0); |
| 1305 | CHK(mbedtls_cipher_set_padding_mode(&t_in->cipher_ctx_dec, |
| 1306 | MBEDTLS_PADDING_NONE) == 0); |
| 1307 | CHK(mbedtls_cipher_set_padding_mode(&t_out->cipher_ctx_enc, |
| 1308 | MBEDTLS_PADDING_NONE) == 0); |
| 1309 | CHK(mbedtls_cipher_set_padding_mode(&t_out->cipher_ctx_dec, |
| 1310 | MBEDTLS_PADDING_NONE) == 0); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1311 | } |
| 1312 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
| 1313 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1314 | CHK(mbedtls_cipher_setkey(&t_in->cipher_ctx_enc, key0, |
| 1315 | keylen << 3, MBEDTLS_ENCRYPT) == 0); |
| 1316 | CHK(mbedtls_cipher_setkey(&t_in->cipher_ctx_dec, key1, |
| 1317 | keylen << 3, MBEDTLS_DECRYPT) == 0); |
| 1318 | CHK(mbedtls_cipher_setkey(&t_out->cipher_ctx_enc, key1, |
| 1319 | keylen << 3, MBEDTLS_ENCRYPT) == 0); |
| 1320 | CHK(mbedtls_cipher_setkey(&t_out->cipher_ctx_dec, key0, |
| 1321 | keylen << 3, MBEDTLS_DECRYPT) == 0); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1322 | #endif |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1323 | |
| 1324 | /* Setup MAC contexts */ |
Hanno Becker | fd86ca8 | 2020-11-30 08:54:23 +0000 | [diff] [blame] | 1325 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1326 | if (cipher_info->mode == MBEDTLS_MODE_CBC || |
| 1327 | cipher_info->mode == MBEDTLS_MODE_STREAM) { |
Andrzej Kurek | 21b6870 | 2022-08-17 16:26:12 -0400 | [diff] [blame] | 1328 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1329 | mbedtls_md_info_t const *md_info = mbedtls_md_info_from_type(hash_id); |
| 1330 | CHK(md_info != NULL); |
Andrzej Kurek | 21b6870 | 2022-08-17 16:26:12 -0400 | [diff] [blame] | 1331 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1332 | maclen = mbedtls_hash_info_get_size(hash_id); |
| 1333 | CHK(maclen != 0); |
Przemek Stekiel | cc59c99 | 2022-08-01 11:51:22 +0200 | [diff] [blame] | 1334 | /* Pick hash keys */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1335 | CHK((md0 = mbedtls_calloc(1, maclen)) != NULL); |
| 1336 | CHK((md1 = mbedtls_calloc(1, maclen)) != NULL); |
| 1337 | memset(md0, 0x5, maclen); |
| 1338 | memset(md1, 0x6, maclen); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1339 | |
Neil Armstrong | f4cc062 | 2022-02-23 16:21:01 +0100 | [diff] [blame] | 1340 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1341 | alg = mbedtls_hash_info_psa_from_md(hash_id); |
Neil Armstrong | f4cc062 | 2022-02-23 16:21:01 +0100 | [diff] [blame] | 1342 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1343 | CHK(alg != 0); |
Neil Armstrong | f4cc062 | 2022-02-23 16:21:01 +0100 | [diff] [blame] | 1344 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1345 | t_out->psa_mac_alg = PSA_ALG_HMAC(alg); |
| 1346 | t_in->psa_mac_alg = PSA_ALG_HMAC(alg); |
Neil Armstrong | f4cc062 | 2022-02-23 16:21:01 +0100 | [diff] [blame] | 1347 | t_in->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT; |
| 1348 | t_out->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT; |
| 1349 | t_in->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT; |
| 1350 | t_out->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT; |
| 1351 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1352 | psa_reset_key_attributes(&attributes); |
| 1353 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE); |
| 1354 | psa_set_key_algorithm(&attributes, PSA_ALG_HMAC(alg)); |
| 1355 | psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC); |
Neil Armstrong | f4cc062 | 2022-02-23 16:21:01 +0100 | [diff] [blame] | 1356 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1357 | CHK(psa_import_key(&attributes, |
| 1358 | md0, maclen, |
| 1359 | &t_in->psa_mac_enc) == PSA_SUCCESS); |
Neil Armstrong | f4cc062 | 2022-02-23 16:21:01 +0100 | [diff] [blame] | 1360 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1361 | CHK(psa_import_key(&attributes, |
| 1362 | md1, maclen, |
| 1363 | &t_out->psa_mac_enc) == PSA_SUCCESS); |
Neil Armstrong | f4cc062 | 2022-02-23 16:21:01 +0100 | [diff] [blame] | 1364 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1365 | if (cipher_info->mode == MBEDTLS_MODE_STREAM || |
| 1366 | etm == MBEDTLS_SSL_ETM_DISABLED) { |
Neil Armstrong | 8f92bf3 | 2022-03-18 09:56:57 +0100 | [diff] [blame] | 1367 | /* mbedtls_ct_hmac() requires the key to be exportable */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1368 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT | |
| 1369 | PSA_KEY_USAGE_VERIFY_HASH); |
| 1370 | } else { |
| 1371 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 1372 | } |
Neil Armstrong | f4cc062 | 2022-02-23 16:21:01 +0100 | [diff] [blame] | 1373 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1374 | CHK(psa_import_key(&attributes, |
| 1375 | md1, maclen, |
| 1376 | &t_in->psa_mac_dec) == PSA_SUCCESS); |
Neil Armstrong | f4cc062 | 2022-02-23 16:21:01 +0100 | [diff] [blame] | 1377 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1378 | CHK(psa_import_key(&attributes, |
| 1379 | md0, maclen, |
| 1380 | &t_out->psa_mac_dec) == PSA_SUCCESS); |
Neil Armstrong | cf8841a | 2022-02-24 11:17:45 +0100 | [diff] [blame] | 1381 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1382 | CHK(mbedtls_md_setup(&t_out->md_ctx_enc, md_info, 1) == 0); |
| 1383 | CHK(mbedtls_md_setup(&t_out->md_ctx_dec, md_info, 1) == 0); |
| 1384 | CHK(mbedtls_md_setup(&t_in->md_ctx_enc, md_info, 1) == 0); |
| 1385 | 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] | 1386 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1387 | CHK(mbedtls_md_hmac_starts(&t_in->md_ctx_enc, |
| 1388 | md0, maclen) == 0); |
| 1389 | CHK(mbedtls_md_hmac_starts(&t_in->md_ctx_dec, |
| 1390 | md1, maclen) == 0); |
| 1391 | CHK(mbedtls_md_hmac_starts(&t_out->md_ctx_enc, |
| 1392 | md1, maclen) == 0); |
| 1393 | CHK(mbedtls_md_hmac_starts(&t_out->md_ctx_dec, |
| 1394 | md0, maclen) == 0); |
Neil Armstrong | cf8841a | 2022-02-24 11:17:45 +0100 | [diff] [blame] | 1395 | #endif |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1396 | } |
| 1397 | #else |
| 1398 | ((void) hash_id); |
Hanno Becker | fd86ca8 | 2020-11-30 08:54:23 +0000 | [diff] [blame] | 1399 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1400 | |
| 1401 | |
| 1402 | /* Pick IV's (regardless of whether they |
| 1403 | * are being used by the transform). */ |
| 1404 | ivlen = cipher_info->iv_size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1405 | memset(iv_enc, 0x3, sizeof(iv_enc)); |
| 1406 | memset(iv_dec, 0x4, sizeof(iv_dec)); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1407 | |
| 1408 | /* |
| 1409 | * Setup transforms |
| 1410 | */ |
| 1411 | |
Jaeden Amero | 2de07f1 | 2019-06-05 13:32:08 +0100 | [diff] [blame] | 1412 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \ |
Hanno Becker | fd86ca8 | 2020-11-30 08:54:23 +0000 | [diff] [blame] | 1413 | defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1414 | t_out->encrypt_then_mac = etm; |
| 1415 | t_in->encrypt_then_mac = etm; |
| 1416 | #else |
| 1417 | ((void) etm); |
| 1418 | #endif |
| 1419 | |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 1420 | t_out->tls_version = tls_version; |
| 1421 | t_in->tls_version = tls_version; |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1422 | t_out->ivlen = ivlen; |
| 1423 | t_in->ivlen = ivlen; |
| 1424 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1425 | switch (cipher_info->mode) { |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1426 | case MBEDTLS_MODE_GCM: |
| 1427 | case MBEDTLS_MODE_CCM: |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1428 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1429 | if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
Hanno Becker | e683287 | 2020-05-28 08:29:58 +0100 | [diff] [blame] | 1430 | t_out->fixed_ivlen = 12; |
| 1431 | t_in->fixed_ivlen = 12; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1432 | } else |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1433 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | e683287 | 2020-05-28 08:29:58 +0100 | [diff] [blame] | 1434 | { |
| 1435 | t_out->fixed_ivlen = 4; |
| 1436 | t_in->fixed_ivlen = 4; |
| 1437 | } |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1438 | t_out->maclen = 0; |
| 1439 | t_in->maclen = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1440 | switch (tag_mode) { |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1441 | case 0: /* Full tag */ |
| 1442 | t_out->taglen = 16; |
| 1443 | t_in->taglen = 16; |
| 1444 | break; |
| 1445 | case 1: /* Partial tag */ |
| 1446 | t_out->taglen = 8; |
| 1447 | t_in->taglen = 8; |
| 1448 | break; |
| 1449 | default: |
Paul Elliott | c7b5374 | 2021-02-03 13:18:33 +0000 | [diff] [blame] | 1450 | ret = 1; |
| 1451 | goto cleanup; |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1452 | } |
| 1453 | break; |
| 1454 | |
| 1455 | case MBEDTLS_MODE_CHACHAPOLY: |
| 1456 | t_out->fixed_ivlen = 12; |
| 1457 | t_in->fixed_ivlen = 12; |
| 1458 | t_out->maclen = 0; |
| 1459 | t_in->maclen = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1460 | switch (tag_mode) { |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1461 | case 0: /* Full tag */ |
| 1462 | t_out->taglen = 16; |
| 1463 | t_in->taglen = 16; |
| 1464 | break; |
| 1465 | case 1: /* Partial tag */ |
| 1466 | t_out->taglen = 8; |
| 1467 | t_in->taglen = 8; |
| 1468 | break; |
| 1469 | default: |
Paul Elliott | c7b5374 | 2021-02-03 13:18:33 +0000 | [diff] [blame] | 1470 | ret = 1; |
| 1471 | goto cleanup; |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1472 | } |
| 1473 | break; |
| 1474 | |
| 1475 | case MBEDTLS_MODE_STREAM: |
| 1476 | case MBEDTLS_MODE_CBC: |
| 1477 | t_out->fixed_ivlen = 0; /* redundant, must be 0 */ |
| 1478 | t_in->fixed_ivlen = 0; /* redundant, must be 0 */ |
| 1479 | t_out->taglen = 0; |
| 1480 | t_in->taglen = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1481 | switch (tag_mode) { |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1482 | case 0: /* Full tag */ |
| 1483 | t_out->maclen = maclen; |
| 1484 | t_in->maclen = maclen; |
| 1485 | break; |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 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 | default: |
Paul Elliott | c7b5374 | 2021-02-03 13:18:33 +0000 | [diff] [blame] | 1492 | ret = 1; |
| 1493 | goto cleanup; |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1494 | break; |
| 1495 | } |
| 1496 | |
| 1497 | /* Setup IV's */ |
| 1498 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1499 | memcpy(&t_in->iv_dec, iv_dec, sizeof(iv_dec)); |
| 1500 | memcpy(&t_in->iv_enc, iv_enc, sizeof(iv_enc)); |
| 1501 | memcpy(&t_out->iv_dec, iv_enc, sizeof(iv_enc)); |
| 1502 | memcpy(&t_out->iv_enc, iv_dec, sizeof(iv_dec)); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1503 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1504 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 1505 | /* Add CID */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1506 | memcpy(&t_in->in_cid, cid0, cid0_len); |
| 1507 | memcpy(&t_in->out_cid, cid1, cid1_len); |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 1508 | t_in->in_cid_len = cid0_len; |
| 1509 | t_in->out_cid_len = cid1_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1510 | memcpy(&t_out->in_cid, cid1, cid1_len); |
| 1511 | memcpy(&t_out->out_cid, cid0, cid0_len); |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 1512 | t_out->in_cid_len = cid1_len; |
| 1513 | t_out->out_cid_len = cid0_len; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1514 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 1515 | |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1516 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1517 | status = mbedtls_ssl_cipher_to_psa(cipher_type, |
| 1518 | t_in->taglen, |
| 1519 | &alg, |
| 1520 | &key_type, |
| 1521 | &key_bits); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1522 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1523 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 1524 | ret = PSA_TO_MBEDTLS_ERR(status); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1525 | goto cleanup; |
| 1526 | } |
| 1527 | |
| 1528 | t_in->psa_alg = alg; |
| 1529 | t_out->psa_alg = alg; |
| 1530 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1531 | if (alg != MBEDTLS_SSL_NULL_CIPHER) { |
| 1532 | psa_reset_key_attributes(&attributes); |
| 1533 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 1534 | psa_set_key_algorithm(&attributes, alg); |
| 1535 | psa_set_key_type(&attributes, key_type); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1536 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1537 | status = psa_import_key(&attributes, |
| 1538 | key0, |
| 1539 | PSA_BITS_TO_BYTES(key_bits), |
| 1540 | &t_in->psa_key_enc); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1541 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1542 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 1543 | ret = PSA_TO_MBEDTLS_ERR(status); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1544 | goto cleanup; |
| 1545 | } |
| 1546 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1547 | status = psa_import_key(&attributes, |
| 1548 | key1, |
| 1549 | PSA_BITS_TO_BYTES(key_bits), |
| 1550 | &t_out->psa_key_enc); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1551 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1552 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 1553 | ret = PSA_TO_MBEDTLS_ERR(status); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1554 | goto cleanup; |
| 1555 | } |
| 1556 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1557 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
Przemyslaw Stekiel | f4ca3f0 | 2022-01-25 00:25:59 +0100 | [diff] [blame] | 1558 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1559 | status = psa_import_key(&attributes, |
| 1560 | key1, |
| 1561 | PSA_BITS_TO_BYTES(key_bits), |
| 1562 | &t_in->psa_key_dec); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1563 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1564 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 1565 | ret = PSA_TO_MBEDTLS_ERR(status); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1566 | goto cleanup; |
| 1567 | } |
| 1568 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1569 | status = psa_import_key(&attributes, |
| 1570 | key0, |
| 1571 | PSA_BITS_TO_BYTES(key_bits), |
| 1572 | &t_out->psa_key_dec); |
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 | if (status != PSA_SUCCESS) { |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 1575 | ret = PSA_TO_MBEDTLS_ERR(status); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1576 | goto cleanup; |
| 1577 | } |
| 1578 | } |
| 1579 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1580 | |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 1581 | cleanup: |
| 1582 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1583 | mbedtls_free(key0); |
| 1584 | mbedtls_free(key1); |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 1585 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1586 | mbedtls_free(md0); |
| 1587 | mbedtls_free(md1); |
Paul Elliott | 6f1eda7 | 2020-06-11 20:22:00 +0100 | [diff] [blame] | 1588 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1589 | return ret; |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1590 | } |
| 1591 | |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1592 | /* |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 1593 | * Populate a session structure for serialization tests. |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1594 | * Choose dummy values, mostly non-0 to distinguish from the init default. |
| 1595 | */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 1596 | static int mbedtls_test_ssl_tls12_populate_session(mbedtls_ssl_session *session, |
| 1597 | int ticket_len, |
| 1598 | const char *crt_file) |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1599 | { |
| 1600 | #if defined(MBEDTLS_HAVE_TIME) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1601 | session->start = mbedtls_time(NULL) - 42; |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1602 | #endif |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 1603 | session->tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1604 | session->ciphersuite = 0xabcd; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1605 | session->id_len = sizeof(session->id); |
| 1606 | memset(session->id, 66, session->id_len); |
| 1607 | memset(session->master, 17, sizeof(session->master)); |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1608 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 1609 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) && defined(MBEDTLS_FS_IO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1610 | if (crt_file != NULL && strlen(crt_file) != 0) { |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 1611 | mbedtls_x509_crt tmp_crt; |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1612 | int ret; |
Manuel Pégourié-Gonnard | 6b84070 | 2019-05-24 09:40:17 +0200 | [diff] [blame] | 1613 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1614 | mbedtls_x509_crt_init(&tmp_crt); |
| 1615 | ret = mbedtls_x509_crt_parse_file(&tmp_crt, crt_file); |
| 1616 | if (ret != 0) { |
| 1617 | return ret; |
| 1618 | } |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 1619 | |
| 1620 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 1621 | /* Move temporary CRT. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1622 | session->peer_cert = mbedtls_calloc(1, sizeof(*session->peer_cert)); |
| 1623 | if (session->peer_cert == NULL) { |
| 1624 | return -1; |
| 1625 | } |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 1626 | *session->peer_cert = tmp_crt; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1627 | memset(&tmp_crt, 0, sizeof(tmp_crt)); |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 1628 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 1629 | /* Calculate digest of temporary CRT. */ |
| 1630 | session->peer_cert_digest = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1631 | mbedtls_calloc(1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN); |
| 1632 | if (session->peer_cert_digest == NULL) { |
| 1633 | return -1; |
| 1634 | } |
Przemek Stekiel | cc59c99 | 2022-08-01 11:51:22 +0200 | [diff] [blame] | 1635 | |
| 1636 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1637 | psa_algorithm_t psa_alg = mbedtls_hash_info_psa_from_md( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1638 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE); |
Przemek Stekiel | cc59c99 | 2022-08-01 11:51:22 +0200 | [diff] [blame] | 1639 | size_t hash_size = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1640 | psa_status_t status = psa_hash_compute(psa_alg, tmp_crt.raw.p, |
| 1641 | tmp_crt.raw.len, |
| 1642 | session->peer_cert_digest, |
| 1643 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN, |
| 1644 | &hash_size); |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 1645 | ret = PSA_TO_MBEDTLS_ERR(status); |
Przemek Stekiel | cc59c99 | 2022-08-01 11:51:22 +0200 | [diff] [blame] | 1646 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1647 | ret = mbedtls_md(mbedtls_md_info_from_type( |
| 1648 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE), |
| 1649 | tmp_crt.raw.p, tmp_crt.raw.len, |
| 1650 | session->peer_cert_digest); |
Przemek Stekiel | cc59c99 | 2022-08-01 11:51:22 +0200 | [diff] [blame] | 1651 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1652 | if (ret != 0) { |
| 1653 | return ret; |
| 1654 | } |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 1655 | session->peer_cert_digest_type = |
| 1656 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE; |
| 1657 | session->peer_cert_digest_len = |
| 1658 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN; |
| 1659 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 1660 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1661 | mbedtls_x509_crt_free(&tmp_crt); |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1662 | } |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 1663 | #else /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED && MBEDTLS_FS_IO */ |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1664 | (void) crt_file; |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 1665 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED && MBEDTLS_FS_IO */ |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1666 | session->verify_result = 0xdeadbeef; |
| 1667 | |
| 1668 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1669 | if (ticket_len != 0) { |
| 1670 | session->ticket = mbedtls_calloc(1, ticket_len); |
| 1671 | if (session->ticket == NULL) { |
| 1672 | return -1; |
| 1673 | } |
| 1674 | memset(session->ticket, 33, ticket_len); |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1675 | } |
| 1676 | session->ticket_len = ticket_len; |
| 1677 | session->ticket_lifetime = 86401; |
| 1678 | #else |
| 1679 | (void) ticket_len; |
| 1680 | #endif |
| 1681 | |
| 1682 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 1683 | session->mfl_code = 1; |
| 1684 | #endif |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1685 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 1686 | session->encrypt_then_mac = 1; |
| 1687 | #endif |
| 1688 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1689 | return 0; |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1690 | } |
| 1691 | |
Jerry Yu | 534ff40 | 2022-07-14 16:43:43 +0800 | [diff] [blame] | 1692 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 1693 | static int mbedtls_test_ssl_tls13_populate_session(mbedtls_ssl_session *session, |
| 1694 | int ticket_len, |
| 1695 | int endpoint_type) |
Jerry Yu | 534ff40 | 2022-07-14 16:43:43 +0800 | [diff] [blame] | 1696 | { |
| 1697 | ((void) ticket_len); |
| 1698 | session->tls_version = MBEDTLS_SSL_VERSION_TLS1_3; |
| 1699 | session->endpoint = endpoint_type == MBEDTLS_SSL_IS_CLIENT ? |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1700 | MBEDTLS_SSL_IS_CLIENT : MBEDTLS_SSL_IS_SERVER; |
Jerry Yu | 534ff40 | 2022-07-14 16:43:43 +0800 | [diff] [blame] | 1701 | session->ciphersuite = 0xabcd; |
| 1702 | session->ticket_age_add = 0x87654321; |
| 1703 | session->ticket_flags = 0x7; |
| 1704 | |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 1705 | session->resumption_key_len = 32; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1706 | memset(session->resumption_key, 0x99, sizeof(session->resumption_key)); |
Jerry Yu | 534ff40 | 2022-07-14 16:43:43 +0800 | [diff] [blame] | 1707 | |
| 1708 | #if defined(MBEDTLS_HAVE_TIME) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1709 | if (session->endpoint == MBEDTLS_SSL_IS_SERVER) { |
| 1710 | session->start = mbedtls_time(NULL) - 42; |
Jerry Yu | 534ff40 | 2022-07-14 16:43:43 +0800 | [diff] [blame] | 1711 | } |
| 1712 | #endif |
| 1713 | |
| 1714 | #if defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1715 | if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
Jerry Yu | 534ff40 | 2022-07-14 16:43:43 +0800 | [diff] [blame] | 1716 | #if defined(MBEDTLS_HAVE_TIME) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1717 | session->ticket_received = mbedtls_time(NULL) - 40; |
Jerry Yu | 534ff40 | 2022-07-14 16:43:43 +0800 | [diff] [blame] | 1718 | #endif |
| 1719 | session->ticket_lifetime = 0xfedcba98; |
| 1720 | |
| 1721 | session->ticket_len = ticket_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1722 | if (ticket_len != 0) { |
| 1723 | session->ticket = mbedtls_calloc(1, ticket_len); |
| 1724 | if (session->ticket == NULL) { |
| 1725 | return -1; |
| 1726 | } |
| 1727 | memset(session->ticket, 33, ticket_len); |
Jerry Yu | 534ff40 | 2022-07-14 16:43:43 +0800 | [diff] [blame] | 1728 | } |
| 1729 | } |
| 1730 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 1731 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1732 | return 0; |
Jerry Yu | 534ff40 | 2022-07-14 16:43:43 +0800 | [diff] [blame] | 1733 | } |
| 1734 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 1735 | |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1736 | /* |
| 1737 | * Perform data exchanging between \p ssl_1 and \p ssl_2 and check if the |
| 1738 | * message was sent in the correct number of fragments. |
| 1739 | * |
| 1740 | * /p ssl_1 and /p ssl_2 Endpoints represented by mbedtls_ssl_context. Both |
| 1741 | * of them must be initialized and connected beforehand. |
| 1742 | * /p msg_len_1 and /p msg_len_2 specify the size of the message to send. |
| 1743 | * /p expected_fragments_1 and /p expected_fragments_2 determine in how many |
| 1744 | * fragments the message should be sent. |
| 1745 | * expected_fragments is 0: can be used for DTLS testing while the message |
| 1746 | * size is larger than MFL. In that case the message |
| 1747 | * cannot be fragmented and sent to the second endpoint. |
| 1748 | * This value can be used for negative tests. |
| 1749 | * expected_fragments is 1: can be used for TLS/DTLS testing while the |
| 1750 | * message size is below MFL |
| 1751 | * expected_fragments > 1: can be used for TLS testing while the message |
| 1752 | * size is larger than MFL |
| 1753 | * |
| 1754 | * \retval 0 on success, otherwise error code. |
| 1755 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1756 | int mbedtls_exchange_data(mbedtls_ssl_context *ssl_1, |
| 1757 | int msg_len_1, const int expected_fragments_1, |
| 1758 | mbedtls_ssl_context *ssl_2, |
| 1759 | int msg_len_2, const int expected_fragments_2) |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1760 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1761 | unsigned char *msg_buf_1 = malloc(msg_len_1); |
| 1762 | unsigned char *msg_buf_2 = malloc(msg_len_2); |
| 1763 | unsigned char *in_buf_1 = malloc(msg_len_2); |
| 1764 | unsigned char *in_buf_2 = malloc(msg_len_1); |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1765 | int msg_type, ret = -1; |
| 1766 | |
| 1767 | /* Perform this test with two message types. At first use a message |
| 1768 | * consisting of only 0x00 for the client and only 0xFF for the server. |
| 1769 | * At the second time use message with generated data */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1770 | for (msg_type = 0; msg_type < 2; msg_type++) { |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1771 | int written_1 = 0; |
| 1772 | int written_2 = 0; |
| 1773 | int read_1 = 0; |
| 1774 | int read_2 = 0; |
| 1775 | int fragments_1 = 0; |
| 1776 | int fragments_2 = 0; |
| 1777 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1778 | if (msg_type == 0) { |
| 1779 | memset(msg_buf_1, 0x00, msg_len_1); |
| 1780 | memset(msg_buf_2, 0xff, msg_len_2); |
| 1781 | } else { |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1782 | int i, j = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1783 | for (i = 0; i < msg_len_1; i++) { |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1784 | msg_buf_1[i] = j++ & 0xFF; |
| 1785 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1786 | for (i = 0; i < msg_len_2; i++) { |
| 1787 | msg_buf_2[i] = (j -= 5) & 0xFF; |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1788 | } |
| 1789 | } |
| 1790 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1791 | while (read_1 < msg_len_2 || read_2 < msg_len_1) { |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1792 | /* ssl_1 sending */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1793 | if (msg_len_1 > written_1) { |
| 1794 | ret = mbedtls_ssl_write_fragment(ssl_1, msg_buf_1, |
| 1795 | msg_len_1, &written_1, |
| 1796 | expected_fragments_1); |
| 1797 | if (expected_fragments_1 == 0) { |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1798 | /* This error is expected when the message is too large and |
| 1799 | * cannot be fragmented */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1800 | TEST_ASSERT(ret == MBEDTLS_ERR_SSL_BAD_INPUT_DATA); |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1801 | msg_len_1 = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1802 | } else { |
| 1803 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1804 | } |
| 1805 | } |
| 1806 | |
| 1807 | /* ssl_2 sending */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1808 | if (msg_len_2 > written_2) { |
| 1809 | ret = mbedtls_ssl_write_fragment(ssl_2, msg_buf_2, |
| 1810 | msg_len_2, &written_2, |
| 1811 | expected_fragments_2); |
| 1812 | if (expected_fragments_2 == 0) { |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1813 | /* This error is expected when the message is too large and |
| 1814 | * cannot be fragmented */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1815 | TEST_ASSERT(ret == MBEDTLS_ERR_SSL_BAD_INPUT_DATA); |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1816 | msg_len_2 = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1817 | } else { |
| 1818 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1819 | } |
| 1820 | } |
| 1821 | |
| 1822 | /* ssl_1 reading */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1823 | if (read_1 < msg_len_2) { |
| 1824 | ret = mbedtls_ssl_read_fragment(ssl_1, in_buf_1, |
| 1825 | msg_len_2, &read_1, |
| 1826 | &fragments_2, |
| 1827 | expected_fragments_2); |
| 1828 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1829 | } |
| 1830 | |
| 1831 | /* ssl_2 reading */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1832 | if (read_2 < msg_len_1) { |
| 1833 | ret = mbedtls_ssl_read_fragment(ssl_2, in_buf_2, |
| 1834 | msg_len_1, &read_2, |
| 1835 | &fragments_1, |
| 1836 | expected_fragments_1); |
| 1837 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1838 | } |
| 1839 | } |
| 1840 | |
| 1841 | ret = -1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1842 | TEST_ASSERT(0 == memcmp(msg_buf_1, in_buf_2, msg_len_1)); |
| 1843 | TEST_ASSERT(0 == memcmp(msg_buf_2, in_buf_1, msg_len_2)); |
| 1844 | TEST_ASSERT(fragments_1 == expected_fragments_1); |
| 1845 | TEST_ASSERT(fragments_2 == expected_fragments_2); |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1846 | } |
| 1847 | |
| 1848 | ret = 0; |
| 1849 | |
| 1850 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1851 | free(msg_buf_1); |
| 1852 | free(in_buf_1); |
| 1853 | free(msg_buf_2); |
| 1854 | free(in_buf_2); |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1855 | |
| 1856 | return ret; |
| 1857 | } |
| 1858 | |
Piotr Nowicki | 95e9eb8 | 2020-02-14 11:33:34 +0100 | [diff] [blame] | 1859 | /* |
| 1860 | * Perform data exchanging between \p ssl_1 and \p ssl_2. Both of endpoints |
| 1861 | * must be initialized and connected beforehand. |
| 1862 | * |
| 1863 | * \retval 0 on success, otherwise error code. |
| 1864 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1865 | int exchange_data(mbedtls_ssl_context *ssl_1, |
| 1866 | mbedtls_ssl_context *ssl_2) |
Piotr Nowicki | 95e9eb8 | 2020-02-14 11:33:34 +0100 | [diff] [blame] | 1867 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1868 | return mbedtls_exchange_data(ssl_1, 256, 1, |
| 1869 | ssl_2, 256, 1); |
Piotr Nowicki | 95e9eb8 | 2020-02-14 11:33:34 +0100 | [diff] [blame] | 1870 | } |
| 1871 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 1872 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1873 | static int check_ssl_version(mbedtls_ssl_protocol_version expected_negotiated_version, |
| 1874 | const mbedtls_ssl_context *ssl) |
Gilles Peskine | 1255b0d | 2022-01-13 01:08:48 +0100 | [diff] [blame] | 1875 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1876 | const char *version_string = mbedtls_ssl_get_version(ssl); |
Gilles Peskine | 1255b0d | 2022-01-13 01:08:48 +0100 | [diff] [blame] | 1877 | mbedtls_ssl_protocol_version version_number = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1878 | mbedtls_ssl_get_version_number(ssl); |
Gilles Peskine | 1255b0d | 2022-01-13 01:08:48 +0100 | [diff] [blame] | 1879 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1880 | TEST_EQUAL(ssl->tls_version, expected_negotiated_version); |
Gilles Peskine | 1255b0d | 2022-01-13 01:08:48 +0100 | [diff] [blame] | 1881 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1882 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 1883 | TEST_EQUAL(version_string[0], 'D'); |
Gilles Peskine | 1255b0d | 2022-01-13 01:08:48 +0100 | [diff] [blame] | 1884 | ++version_string; |
| 1885 | } |
| 1886 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1887 | switch (expected_negotiated_version) { |
Glenn Strauss | e3af4cb | 2022-03-15 03:23:42 -0400 | [diff] [blame] | 1888 | case MBEDTLS_SSL_VERSION_TLS1_2: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1889 | TEST_EQUAL(version_number, MBEDTLS_SSL_VERSION_TLS1_2); |
| 1890 | TEST_ASSERT(strcmp(version_string, "TLSv1.2") == 0); |
Gilles Peskine | 1255b0d | 2022-01-13 01:08:48 +0100 | [diff] [blame] | 1891 | break; |
| 1892 | |
Glenn Strauss | e3af4cb | 2022-03-15 03:23:42 -0400 | [diff] [blame] | 1893 | case MBEDTLS_SSL_VERSION_TLS1_3: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1894 | TEST_EQUAL(version_number, MBEDTLS_SSL_VERSION_TLS1_3); |
| 1895 | TEST_ASSERT(strcmp(version_string, "TLSv1.3") == 0); |
Gilles Peskine | 1255b0d | 2022-01-13 01:08:48 +0100 | [diff] [blame] | 1896 | break; |
| 1897 | |
| 1898 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1899 | TEST_ASSERT(!"Version check not implemented for this protocol version"); |
Gilles Peskine | 1255b0d | 2022-01-13 01:08:48 +0100 | [diff] [blame] | 1900 | } |
| 1901 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1902 | return 1; |
Gilles Peskine | 1255b0d | 2022-01-13 01:08:48 +0100 | [diff] [blame] | 1903 | |
| 1904 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1905 | return 0; |
Gilles Peskine | 1255b0d | 2022-01-13 01:08:48 +0100 | [diff] [blame] | 1906 | } |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 1907 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Gilles Peskine | 1255b0d | 2022-01-13 01:08:48 +0100 | [diff] [blame] | 1908 | |
| 1909 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 1910 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 1911 | void mbedtls_test_ssl_perform_handshake(mbedtls_test_handshake_test_options *options) |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1912 | { |
| 1913 | /* forced_ciphersuite needs to last until the end of the handshake */ |
| 1914 | int forced_ciphersuite[2]; |
| 1915 | enum { BUFFSIZE = 17000 }; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 1916 | mbedtls_test_ssl_endpoint client, server; |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 1917 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED) |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1918 | const char *psk_identity = "foo"; |
| 1919 | #endif |
| 1920 | #if defined(MBEDTLS_TIMING_C) |
| 1921 | mbedtls_timing_delay_context timer_client, timer_server; |
| 1922 | #endif |
| 1923 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
| 1924 | unsigned char *context_buf = NULL; |
| 1925 | size_t context_buf_len; |
| 1926 | #endif |
| 1927 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 1928 | int ret = -1; |
| 1929 | #endif |
Neil Armstrong | 8c52ed8 | 2022-05-27 13:14:55 +0200 | [diff] [blame] | 1930 | int expected_handshake_result = options->expected_handshake_result; |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1931 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1932 | USE_PSA_INIT(); |
| 1933 | mbedtls_platform_zeroize(&client, sizeof(client)); |
| 1934 | mbedtls_platform_zeroize(&server, sizeof(server)); |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 1935 | mbedtls_test_ssl_message_queue server_queue, client_queue; |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1936 | mbedtls_test_message_socket_context server_context, client_context; |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 1937 | mbedtls_test_message_socket_init(&server_context); |
| 1938 | mbedtls_test_message_socket_init(&client_context); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1939 | |
| 1940 | /* Client side */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1941 | if (options->dtls != 0) { |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 1942 | TEST_ASSERT(mbedtls_test_ssl_endpoint_init(&client, MBEDTLS_SSL_IS_CLIENT, |
| 1943 | options, &client_context, |
| 1944 | &client_queue, |
| 1945 | &server_queue, NULL) == 0); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1946 | #if defined(MBEDTLS_TIMING_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1947 | mbedtls_ssl_set_timer_cb(&client.ssl, &timer_client, |
| 1948 | mbedtls_timing_set_delay, |
| 1949 | mbedtls_timing_get_delay); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1950 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1951 | } else { |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 1952 | TEST_ASSERT(mbedtls_test_ssl_endpoint_init(&client, MBEDTLS_SSL_IS_CLIENT, |
| 1953 | options, NULL, NULL, |
| 1954 | NULL, NULL) == 0); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1955 | } |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 1956 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1957 | if (options->client_min_version != MBEDTLS_SSL_VERSION_UNKNOWN) { |
| 1958 | mbedtls_ssl_conf_min_tls_version(&client.conf, |
| 1959 | options->client_min_version); |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 1960 | } |
| 1961 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1962 | if (options->client_max_version != MBEDTLS_SSL_VERSION_UNKNOWN) { |
| 1963 | mbedtls_ssl_conf_max_tls_version(&client.conf, |
| 1964 | options->client_max_version); |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 1965 | } |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1966 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1967 | if (strlen(options->cipher) > 0) { |
| 1968 | set_ciphersuite(&client.conf, options->cipher, forced_ciphersuite); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1969 | } |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 1970 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1971 | #if defined(MBEDTLS_DEBUG_C) |
| 1972 | if (options->cli_log_fun) { |
| 1973 | mbedtls_debug_set_threshold(4); |
| 1974 | mbedtls_ssl_conf_dbg(&client.conf, options->cli_log_fun, |
| 1975 | options->cli_log_obj); |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 1976 | } |
| 1977 | #endif |
| 1978 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1979 | /* Server side */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1980 | if (options->dtls != 0) { |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 1981 | TEST_ASSERT(mbedtls_test_ssl_endpoint_init(&server, MBEDTLS_SSL_IS_SERVER, |
| 1982 | options, &server_context, |
| 1983 | &server_queue, |
| 1984 | &client_queue, NULL) == 0); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1985 | #if defined(MBEDTLS_TIMING_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1986 | mbedtls_ssl_set_timer_cb(&server.ssl, &timer_server, |
| 1987 | mbedtls_timing_set_delay, |
| 1988 | mbedtls_timing_get_delay); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1989 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1990 | } else { |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 1991 | TEST_ASSERT(mbedtls_test_ssl_endpoint_init(&server, MBEDTLS_SSL_IS_SERVER, |
| 1992 | options, NULL, NULL, NULL, |
| 1993 | NULL) == 0); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1994 | } |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 1995 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1996 | mbedtls_ssl_conf_authmode(&server.conf, options->srv_auth_mode); |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 1997 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1998 | if (options->server_min_version != MBEDTLS_SSL_VERSION_UNKNOWN) { |
| 1999 | mbedtls_ssl_conf_min_tls_version(&server.conf, |
| 2000 | options->server_min_version); |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 2001 | } |
| 2002 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2003 | if (options->server_max_version != MBEDTLS_SSL_VERSION_UNKNOWN) { |
| 2004 | mbedtls_ssl_conf_max_tls_version(&server.conf, |
| 2005 | options->server_max_version); |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 2006 | } |
| 2007 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2008 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2009 | TEST_ASSERT(mbedtls_ssl_conf_max_frag_len(&(server.conf), |
| 2010 | (unsigned char) options->mfl) == 0); |
| 2011 | TEST_ASSERT(mbedtls_ssl_conf_max_frag_len(&(client.conf), |
| 2012 | (unsigned char) options->mfl) == 0); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2013 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2014 | TEST_ASSERT(MBEDTLS_SSL_MAX_FRAG_LEN_NONE == options->mfl); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2015 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 2016 | |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 2017 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2018 | if (options->psk_str != NULL && options->psk_str->len > 0) { |
| 2019 | TEST_ASSERT(mbedtls_ssl_conf_psk(&client.conf, options->psk_str->x, |
| 2020 | options->psk_str->len, |
| 2021 | (const unsigned char *) psk_identity, |
| 2022 | strlen(psk_identity)) == 0); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2023 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2024 | TEST_ASSERT(mbedtls_ssl_conf_psk(&server.conf, options->psk_str->x, |
| 2025 | options->psk_str->len, |
| 2026 | (const unsigned char *) psk_identity, |
| 2027 | strlen(psk_identity)) == 0); |
Jerry Yu | 8897c07 | 2022-08-12 13:56:53 +0800 | [diff] [blame] | 2028 | #if defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2029 | mbedtls_ssl_conf_psk_cb(&server.conf, psk_dummy_callback, NULL); |
Jerry Yu | 8897c07 | 2022-08-12 13:56:53 +0800 | [diff] [blame] | 2030 | #endif |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2031 | } |
| 2032 | #endif |
| 2033 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2034 | if (options->renegotiate) { |
| 2035 | mbedtls_ssl_conf_renegotiation(&(server.conf), |
| 2036 | MBEDTLS_SSL_RENEGOTIATION_ENABLED); |
| 2037 | mbedtls_ssl_conf_renegotiation(&(client.conf), |
| 2038 | MBEDTLS_SSL_RENEGOTIATION_ENABLED); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2039 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2040 | mbedtls_ssl_conf_legacy_renegotiation(&(server.conf), |
| 2041 | options->legacy_renegotiation); |
| 2042 | mbedtls_ssl_conf_legacy_renegotiation(&(client.conf), |
| 2043 | options->legacy_renegotiation); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2044 | } |
| 2045 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
| 2046 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2047 | #if defined(MBEDTLS_DEBUG_C) |
| 2048 | if (options->srv_log_fun) { |
| 2049 | mbedtls_debug_set_threshold(4); |
| 2050 | mbedtls_ssl_conf_dbg(&server.conf, options->srv_log_fun, |
| 2051 | options->srv_log_obj); |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 2052 | } |
| 2053 | #endif |
| 2054 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 2055 | TEST_ASSERT(mbedtls_test_mock_socket_connect(&(client.socket), |
| 2056 | &(server.socket), |
| 2057 | BUFFSIZE) == 0); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2058 | |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2059 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2060 | if (options->resize_buffers != 0) { |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2061 | /* Ensure that the buffer sizes are appropriate before resizes */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2062 | TEST_ASSERT(client.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN); |
| 2063 | TEST_ASSERT(client.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN); |
| 2064 | TEST_ASSERT(server.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN); |
| 2065 | TEST_ASSERT(server.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2066 | } |
| 2067 | #endif |
| 2068 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2069 | if (options->expected_negotiated_version == MBEDTLS_SSL_VERSION_UNKNOWN) { |
Hanno Becker | bc00044 | 2021-06-24 09:18:19 +0100 | [diff] [blame] | 2070 | expected_handshake_result = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION; |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 2071 | } |
| 2072 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 2073 | TEST_ASSERT(mbedtls_test_move_handshake_to_state(&(client.ssl), |
| 2074 | &(server.ssl), |
| 2075 | MBEDTLS_SSL_HANDSHAKE_OVER) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2076 | == expected_handshake_result); |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 2077 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2078 | if (expected_handshake_result != 0) { |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 2079 | /* Connection will have failed by this point, skip to cleanup */ |
| 2080 | goto exit; |
| 2081 | } |
| 2082 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2083 | TEST_ASSERT(mbedtls_ssl_is_handshake_over(&client.ssl) == 1); |
Jerry Yu | df0a71a | 2022-05-26 10:43:30 +0800 | [diff] [blame] | 2084 | |
Jerry Yu | 66adf31 | 2022-05-31 15:23:29 +0800 | [diff] [blame] | 2085 | /* Make sure server state is moved to HANDSHAKE_OVER also. */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 2086 | TEST_EQUAL(mbedtls_test_move_handshake_to_state(&(server.ssl), |
| 2087 | &(client.ssl), |
| 2088 | MBEDTLS_SSL_HANDSHAKE_OVER), 0); |
Jerry Yu | df0a71a | 2022-05-26 10:43:30 +0800 | [diff] [blame] | 2089 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2090 | TEST_ASSERT(mbedtls_ssl_is_handshake_over(&server.ssl) == 1); |
Gilles Peskine | 1255b0d | 2022-01-13 01:08:48 +0100 | [diff] [blame] | 2091 | /* Check that both sides have negotiated the expected version. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2092 | mbedtls_test_set_step(0); |
| 2093 | if (!check_ssl_version(options->expected_negotiated_version, |
| 2094 | &client.ssl)) { |
Gilles Peskine | 1255b0d | 2022-01-13 01:08:48 +0100 | [diff] [blame] | 2095 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2096 | } |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 2097 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2098 | mbedtls_test_set_step(1); |
| 2099 | if (!check_ssl_version(options->expected_negotiated_version, |
| 2100 | &server.ssl)) { |
Gilles Peskine | 1255b0d | 2022-01-13 01:08:48 +0100 | [diff] [blame] | 2101 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2102 | } |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 2103 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2104 | if (options->expected_ciphersuite != 0) { |
| 2105 | TEST_EQUAL(server.ssl.session->ciphersuite, |
| 2106 | options->expected_ciphersuite); |
Neil Armstrong | 8c52ed8 | 2022-05-27 13:14:55 +0200 | [diff] [blame] | 2107 | } |
| 2108 | |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2109 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2110 | if (options->resize_buffers != 0) { |
TRodziewicz | 2abf03c | 2021-06-25 14:40:09 +0200 | [diff] [blame] | 2111 | /* A server, when using DTLS, might delay a buffer resize to happen |
| 2112 | * after it receives a message, so we force it. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2113 | TEST_ASSERT(exchange_data(&(client.ssl), &(server.ssl)) == 0); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2114 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2115 | TEST_ASSERT(client.ssl.out_buf_len == |
| 2116 | mbedtls_ssl_get_output_buflen(&client.ssl)); |
| 2117 | TEST_ASSERT(client.ssl.in_buf_len == |
| 2118 | mbedtls_ssl_get_input_buflen(&client.ssl)); |
| 2119 | TEST_ASSERT(server.ssl.out_buf_len == |
| 2120 | mbedtls_ssl_get_output_buflen(&server.ssl)); |
| 2121 | TEST_ASSERT(server.ssl.in_buf_len == |
| 2122 | mbedtls_ssl_get_input_buflen(&server.ssl)); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2123 | } |
| 2124 | #endif |
| 2125 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2126 | if (options->cli_msg_len != 0 || options->srv_msg_len != 0) { |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2127 | /* Start data exchanging test */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2128 | TEST_ASSERT(mbedtls_exchange_data(&(client.ssl), options->cli_msg_len, |
| 2129 | options->expected_cli_fragments, |
| 2130 | &(server.ssl), options->srv_msg_len, |
| 2131 | options->expected_srv_fragments) |
| 2132 | == 0); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2133 | } |
| 2134 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2135 | if (options->serialize == 1) { |
| 2136 | TEST_ASSERT(options->dtls == 1); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2137 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2138 | TEST_ASSERT(mbedtls_ssl_context_save(&(server.ssl), NULL, |
| 2139 | 0, &context_buf_len) |
| 2140 | == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2141 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2142 | context_buf = mbedtls_calloc(1, context_buf_len); |
| 2143 | TEST_ASSERT(context_buf != NULL); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2144 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2145 | TEST_ASSERT(mbedtls_ssl_context_save(&(server.ssl), context_buf, |
| 2146 | context_buf_len, |
| 2147 | &context_buf_len) == 0); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2148 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2149 | mbedtls_ssl_free(&(server.ssl)); |
| 2150 | mbedtls_ssl_init(&(server.ssl)); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2151 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2152 | TEST_ASSERT(mbedtls_ssl_setup(&(server.ssl), &(server.conf)) == 0); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2153 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2154 | mbedtls_ssl_set_bio(&(server.ssl), &server_context, |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 2155 | mbedtls_test_mock_tcp_send_msg, |
| 2156 | mbedtls_test_mock_tcp_recv_msg, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2157 | NULL); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2158 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2159 | mbedtls_ssl_set_user_data_p(&server.ssl, &server); |
Gilles Peskine | 49d7ddf | 2022-01-27 23:25:51 +0100 | [diff] [blame] | 2160 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2161 | #if defined(MBEDTLS_TIMING_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2162 | mbedtls_ssl_set_timer_cb(&server.ssl, &timer_server, |
| 2163 | mbedtls_timing_set_delay, |
| 2164 | mbedtls_timing_get_delay); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2165 | #endif |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2166 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2167 | if (options->resize_buffers != 0) { |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2168 | /* Ensure that the buffer sizes are appropriate before resizes */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2169 | TEST_ASSERT(server.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN); |
| 2170 | TEST_ASSERT(server.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2171 | } |
| 2172 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2173 | TEST_ASSERT(mbedtls_ssl_context_load(&(server.ssl), context_buf, |
| 2174 | context_buf_len) == 0); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2175 | |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2176 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 2177 | /* Validate buffer sizes after context deserialization */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2178 | if (options->resize_buffers != 0) { |
| 2179 | TEST_ASSERT(server.ssl.out_buf_len == |
| 2180 | mbedtls_ssl_get_output_buflen(&server.ssl)); |
| 2181 | TEST_ASSERT(server.ssl.in_buf_len == |
| 2182 | mbedtls_ssl_get_input_buflen(&server.ssl)); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2183 | } |
| 2184 | #endif |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2185 | /* Retest writing/reading */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2186 | if (options->cli_msg_len != 0 || options->srv_msg_len != 0) { |
| 2187 | TEST_ASSERT(mbedtls_exchange_data(&(client.ssl), |
| 2188 | options->cli_msg_len, |
| 2189 | options->expected_cli_fragments, |
| 2190 | &(server.ssl), |
| 2191 | options->srv_msg_len, |
| 2192 | options->expected_srv_fragments) |
| 2193 | == 0); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2194 | } |
| 2195 | } |
| 2196 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2197 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2198 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2199 | if (options->renegotiate) { |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2200 | /* Start test with renegotiation */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2201 | TEST_ASSERT(server.ssl.renego_status == |
| 2202 | MBEDTLS_SSL_INITIAL_HANDSHAKE); |
| 2203 | TEST_ASSERT(client.ssl.renego_status == |
| 2204 | MBEDTLS_SSL_INITIAL_HANDSHAKE); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2205 | |
| 2206 | /* After calling this function for the server, it only sends a handshake |
| 2207 | * request. All renegotiation should happen during data exchanging */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2208 | TEST_ASSERT(mbedtls_ssl_renegotiate(&(server.ssl)) == 0); |
| 2209 | TEST_ASSERT(server.ssl.renego_status == |
| 2210 | MBEDTLS_SSL_RENEGOTIATION_PENDING); |
| 2211 | TEST_ASSERT(client.ssl.renego_status == |
| 2212 | MBEDTLS_SSL_INITIAL_HANDSHAKE); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2213 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2214 | TEST_ASSERT(exchange_data(&(client.ssl), &(server.ssl)) == 0); |
| 2215 | TEST_ASSERT(server.ssl.renego_status == |
| 2216 | MBEDTLS_SSL_RENEGOTIATION_DONE); |
| 2217 | TEST_ASSERT(client.ssl.renego_status == |
| 2218 | MBEDTLS_SSL_RENEGOTIATION_DONE); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2219 | |
| 2220 | /* After calling mbedtls_ssl_renegotiate for the client all renegotiation |
| 2221 | * should happen inside this function. However in this test, we cannot |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 2222 | * perform simultaneous communication between client and server so this |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2223 | * function will return waiting error on the socket. All rest of |
| 2224 | * renegotiation should happen during data exchanging */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2225 | ret = mbedtls_ssl_renegotiate(&(client.ssl)); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2226 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2227 | if (options->resize_buffers != 0) { |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2228 | /* Ensure that the buffer sizes are appropriate before resizes */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2229 | TEST_ASSERT(client.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN); |
| 2230 | TEST_ASSERT(client.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2231 | } |
| 2232 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2233 | TEST_ASSERT(ret == 0 || |
| 2234 | ret == MBEDTLS_ERR_SSL_WANT_READ || |
| 2235 | ret == MBEDTLS_ERR_SSL_WANT_WRITE); |
| 2236 | TEST_ASSERT(server.ssl.renego_status == |
| 2237 | MBEDTLS_SSL_RENEGOTIATION_DONE); |
| 2238 | TEST_ASSERT(client.ssl.renego_status == |
| 2239 | MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2240 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2241 | TEST_ASSERT(exchange_data(&(client.ssl), &(server.ssl)) == 0); |
| 2242 | TEST_ASSERT(server.ssl.renego_status == |
| 2243 | MBEDTLS_SSL_RENEGOTIATION_DONE); |
| 2244 | TEST_ASSERT(client.ssl.renego_status == |
| 2245 | MBEDTLS_SSL_RENEGOTIATION_DONE); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2246 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 2247 | /* Validate buffer sizes after renegotiation */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2248 | if (options->resize_buffers != 0) { |
| 2249 | TEST_ASSERT(client.ssl.out_buf_len == |
| 2250 | mbedtls_ssl_get_output_buflen(&client.ssl)); |
| 2251 | TEST_ASSERT(client.ssl.in_buf_len == |
| 2252 | mbedtls_ssl_get_input_buflen(&client.ssl)); |
| 2253 | TEST_ASSERT(server.ssl.out_buf_len == |
| 2254 | mbedtls_ssl_get_output_buflen(&server.ssl)); |
| 2255 | TEST_ASSERT(server.ssl.in_buf_len == |
| 2256 | mbedtls_ssl_get_input_buflen(&server.ssl)); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2257 | } |
| 2258 | #endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */ |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2259 | } |
| 2260 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
| 2261 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2262 | TEST_ASSERT(mbedtls_ssl_conf_get_user_data_p(&client.conf) == &client); |
| 2263 | TEST_ASSERT(mbedtls_ssl_get_user_data_p(&client.ssl) == &client); |
| 2264 | TEST_ASSERT(mbedtls_ssl_conf_get_user_data_p(&server.conf) == &server); |
| 2265 | TEST_ASSERT(mbedtls_ssl_get_user_data_p(&server.ssl) == &server); |
Gilles Peskine | 80dae04 | 2022-01-21 23:50:39 +0100 | [diff] [blame] | 2266 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2267 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 2268 | mbedtls_test_ssl_endpoint_free(&client, options->dtls != 0 ? &client_context : NULL); |
| 2269 | mbedtls_test_ssl_endpoint_free(&server, options->dtls != 0 ? &server_context : NULL); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2270 | #if defined(MBEDTLS_DEBUG_C) |
| 2271 | if (options->cli_log_fun || options->srv_log_fun) { |
| 2272 | mbedtls_debug_set_threshold(0); |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 2273 | } |
| 2274 | #endif |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2275 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2276 | if (context_buf != NULL) { |
| 2277 | mbedtls_free(context_buf); |
| 2278 | } |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2279 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2280 | USE_PSA_DONE(); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2281 | } |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 2282 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2283 | |
Ronald Cron | e7b9b6b | 2022-06-10 17:24:31 +0200 | [diff] [blame] | 2284 | #if defined(MBEDTLS_TEST_HOOKS) |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 2285 | /* |
| 2286 | * Tweak vector lengths in a TLS 1.3 Certificate message |
| 2287 | * |
Ronald Cron | cf600bc | 2022-06-17 15:54:16 +0200 | [diff] [blame] | 2288 | * \param[in] buf Buffer containing the Certificate message to tweak |
| 2289 | * \param[in]]out] end End of the buffer to parse |
| 2290 | * \param tweak Tweak identifier (from 1 to the number of tweaks). |
| 2291 | * \param[out] expected_result Error code expected from the parsing function |
| 2292 | * \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] | 2293 | * is expected to fail. All zeroes if no |
| 2294 | * MBEDTLS_SSL_CHK_BUF_READ_PTR failure is expected. |
| 2295 | */ |
| 2296 | int tweak_tls13_certificate_msg_vector_len( |
Ronald Cron | e7b9b6b | 2022-06-10 17:24:31 +0200 | [diff] [blame] | 2297 | unsigned char *buf, unsigned char **end, int tweak, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2298 | int *expected_result, mbedtls_ssl_chk_buf_ptr_args *args) |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 2299 | { |
| 2300 | /* |
| 2301 | * The definition of the tweaks assume that the certificate list contains only |
| 2302 | * one certificate. |
| 2303 | */ |
| 2304 | |
| 2305 | /* |
| 2306 | * struct { |
| 2307 | * opaque cert_data<1..2^24-1>; |
| 2308 | * Extension extensions<0..2^16-1>; |
| 2309 | * } CertificateEntry; |
| 2310 | * |
| 2311 | * struct { |
| 2312 | * opaque certificate_request_context<0..2^8-1>; |
| 2313 | * CertificateEntry certificate_list<0..2^24-1>; |
| 2314 | * } Certificate; |
| 2315 | */ |
| 2316 | unsigned char *p_certificate_request_context_len = buf; |
| 2317 | size_t certificate_request_context_len = buf[0]; |
| 2318 | |
| 2319 | unsigned char *p_certificate_list_len = buf + 1 + certificate_request_context_len; |
| 2320 | unsigned char *certificate_list = p_certificate_list_len + 3; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2321 | size_t certificate_list_len = MBEDTLS_GET_UINT24_BE(p_certificate_list_len, 0); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 2322 | |
| 2323 | unsigned char *p_cert_data_len = certificate_list; |
| 2324 | unsigned char *cert_data = p_cert_data_len + 3; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2325 | 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] | 2326 | |
| 2327 | unsigned char *p_extensions_len = cert_data + cert_data_len; |
| 2328 | unsigned char *extensions = p_extensions_len + 2; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2329 | size_t extensions_len = MBEDTLS_GET_UINT16_BE(p_extensions_len, 0); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 2330 | |
| 2331 | *expected_result = MBEDTLS_ERR_SSL_DECODE_ERROR; |
| 2332 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2333 | switch (tweak) { |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 2334 | case 1: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2335 | /* Failure when checking if the certificate request context length and |
| 2336 | * certificate list length can be read |
| 2337 | */ |
| 2338 | *end = buf + 3; |
| 2339 | set_chk_buf_ptr_args(args, buf, *end, 4); |
| 2340 | break; |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 2341 | |
| 2342 | case 2: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2343 | /* Invalid certificate request context length. |
| 2344 | */ |
| 2345 | *p_certificate_request_context_len = |
| 2346 | certificate_request_context_len + 1; |
| 2347 | reset_chk_buf_ptr_args(args); |
| 2348 | break; |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 2349 | |
| 2350 | case 3: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2351 | /* Failure when checking if certificate_list data can be read. */ |
| 2352 | MBEDTLS_PUT_UINT24_BE(certificate_list_len + 1, |
| 2353 | p_certificate_list_len, 0); |
| 2354 | set_chk_buf_ptr_args(args, certificate_list, *end, |
| 2355 | certificate_list_len + 1); |
| 2356 | break; |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 2357 | |
| 2358 | case 4: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2359 | /* Failure when checking if the cert_data length can be read. */ |
| 2360 | MBEDTLS_PUT_UINT24_BE(2, p_certificate_list_len, 0); |
| 2361 | set_chk_buf_ptr_args(args, p_cert_data_len, certificate_list + 2, 3); |
| 2362 | break; |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 2363 | |
| 2364 | case 5: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2365 | /* Failure when checking if cert_data data can be read. */ |
| 2366 | MBEDTLS_PUT_UINT24_BE(certificate_list_len - 3 + 1, |
| 2367 | p_cert_data_len, 0); |
| 2368 | set_chk_buf_ptr_args(args, cert_data, |
| 2369 | certificate_list + certificate_list_len, |
| 2370 | certificate_list_len - 3 + 1); |
| 2371 | break; |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 2372 | |
| 2373 | case 6: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2374 | /* Failure when checking if the extensions length can be read. */ |
| 2375 | MBEDTLS_PUT_UINT24_BE(certificate_list_len - extensions_len - 1, |
| 2376 | p_certificate_list_len, 0); |
| 2377 | set_chk_buf_ptr_args(args, p_extensions_len, |
| 2378 | certificate_list + certificate_list_len - extensions_len - 1, 2); |
| 2379 | break; |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 2380 | |
| 2381 | case 7: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2382 | /* Failure when checking if extensions data can be read. */ |
| 2383 | MBEDTLS_PUT_UINT16_BE(extensions_len + 1, p_extensions_len, 0); |
Ronald Cron | e7b9b6b | 2022-06-10 17:24:31 +0200 | [diff] [blame] | 2384 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2385 | set_chk_buf_ptr_args(args, extensions, |
| 2386 | certificate_list + certificate_list_len, extensions_len + 1); |
| 2387 | break; |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 2388 | |
| 2389 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2390 | return -1; |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 2391 | } |
| 2392 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2393 | return 0; |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 2394 | } |
Ronald Cron | e7b9b6b | 2022-06-10 17:24:31 +0200 | [diff] [blame] | 2395 | #endif /* MBEDTLS_TEST_HOOKS */ |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 2396 | |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 2397 | #define ECJPAKE_TEST_PWD "bla" |
Valerio Setti | e7518ba | 2022-12-02 12:09:43 +0100 | [diff] [blame] | 2398 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2399 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 2400 | #define ECJPAKE_TEST_SET_PASSWORD(exp_ret_val) \ |
| 2401 | ret = (use_opaque_arg) ? \ |
| 2402 | mbedtls_ssl_set_hs_ecjpake_password_opaque(&ssl, pwd_slot) : \ |
| 2403 | mbedtls_ssl_set_hs_ecjpake_password(&ssl, pwd_string, pwd_len); \ |
| 2404 | TEST_EQUAL(ret, exp_ret_val) |
Valerio Setti | e7518ba | 2022-12-02 12:09:43 +0100 | [diff] [blame] | 2405 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2406 | #define ECJPAKE_TEST_SET_PASSWORD(exp_ret_val) \ |
| 2407 | ret = mbedtls_ssl_set_hs_ecjpake_password(&ssl, \ |
| 2408 | pwd_string, pwd_len); \ |
| 2409 | TEST_EQUAL(ret, exp_ret_val) |
Valerio Setti | e7518ba | 2022-12-02 12:09:43 +0100 | [diff] [blame] | 2410 | #endif |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 2411 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2412 | #define TEST_AVAILABLE_ECC(tls_id_, group_id_, psa_family_, psa_bits_) \ |
| 2413 | TEST_EQUAL(mbedtls_ssl_get_ecp_group_id_from_tls_id(tls_id_), \ |
| 2414 | group_id_); \ |
| 2415 | TEST_EQUAL(mbedtls_ssl_get_tls_id_from_ecp_group_id(group_id_), \ |
| 2416 | tls_id_); \ |
| 2417 | TEST_EQUAL(mbedtls_ssl_get_psa_curve_info_from_tls_id(tls_id_, \ |
| 2418 | &psa_family, &psa_bits), PSA_SUCCESS); \ |
| 2419 | TEST_EQUAL(psa_family_, psa_family); \ |
| 2420 | TEST_EQUAL(psa_bits_, psa_bits); |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 2421 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2422 | #define TEST_UNAVAILABLE_ECC(tls_id_, group_id_, psa_family_, psa_bits_) \ |
| 2423 | TEST_EQUAL(mbedtls_ssl_get_ecp_group_id_from_tls_id(tls_id_), \ |
| 2424 | MBEDTLS_ECP_DP_NONE); \ |
| 2425 | TEST_EQUAL(mbedtls_ssl_get_tls_id_from_ecp_group_id(group_id_), \ |
| 2426 | 0); \ |
| 2427 | TEST_EQUAL(mbedtls_ssl_get_psa_curve_info_from_tls_id(tls_id_, \ |
| 2428 | &psa_family, &psa_bits), \ |
| 2429 | PSA_ERROR_NOT_SUPPORTED); |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 2430 | |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 2431 | /* END_HEADER */ |
| 2432 | |
| 2433 | /* BEGIN_DEPENDENCIES |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2434 | * depends_on:MBEDTLS_SSL_TLS_C |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 2435 | * END_DEPENDENCIES |
| 2436 | */ |
| 2437 | |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2438 | /* BEGIN_CASE */ |
| 2439 | void test_callback_buffer_sanity() |
| 2440 | { |
| 2441 | enum { MSGLEN = 10 }; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 2442 | mbedtls_test_ssl_buffer buf; |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2443 | unsigned char input[MSGLEN]; |
| 2444 | unsigned char output[MSGLEN]; |
| 2445 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2446 | memset(input, 0, sizeof(input)); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2447 | |
| 2448 | /* Make sure calling put and get on NULL buffer results in error. */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 2449 | TEST_ASSERT(mbedtls_test_ssl_buffer_put(NULL, input, sizeof(input)) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2450 | == -1); |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 2451 | TEST_ASSERT(mbedtls_test_ssl_buffer_get(NULL, output, sizeof(output)) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2452 | == -1); |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 2453 | TEST_ASSERT(mbedtls_test_ssl_buffer_put(NULL, NULL, sizeof(input)) == -1); |
Andrzej Kurek | f777414 | 2020-01-22 06:34:59 -0500 | [diff] [blame] | 2454 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 2455 | TEST_ASSERT(mbedtls_test_ssl_buffer_put(NULL, NULL, 0) == -1); |
| 2456 | TEST_ASSERT(mbedtls_test_ssl_buffer_get(NULL, NULL, 0) == -1); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2457 | |
| 2458 | /* 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] | 2459 | * in error. */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 2460 | mbedtls_test_ssl_buffer_init(&buf); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2461 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 2462 | TEST_ASSERT(mbedtls_test_ssl_buffer_put(&buf, input, sizeof(input)) == -1); |
| 2463 | TEST_ASSERT(mbedtls_test_ssl_buffer_get(&buf, output, sizeof(output)) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2464 | == -1); |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 2465 | TEST_ASSERT(mbedtls_test_ssl_buffer_put(&buf, NULL, sizeof(input)) == -1); |
Andrzej Kurek | f777414 | 2020-01-22 06:34:59 -0500 | [diff] [blame] | 2466 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 2467 | TEST_ASSERT(mbedtls_test_ssl_buffer_put(&buf, NULL, 0) == -1); |
| 2468 | TEST_ASSERT(mbedtls_test_ssl_buffer_get(&buf, NULL, 0) == -1); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2469 | |
Andrzej Kurek | f777414 | 2020-01-22 06:34:59 -0500 | [diff] [blame] | 2470 | /* Make sure calling put and get on NULL input only results in |
| 2471 | * error if the length is not zero, and that a NULL output is valid for data |
| 2472 | * dropping. |
| 2473 | */ |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2474 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 2475 | TEST_ASSERT(mbedtls_test_ssl_buffer_setup(&buf, sizeof(input)) == 0); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2476 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 2477 | TEST_ASSERT(mbedtls_test_ssl_buffer_put(&buf, NULL, sizeof(input)) == -1); |
| 2478 | TEST_ASSERT(mbedtls_test_ssl_buffer_get(&buf, NULL, sizeof(output)) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2479 | == 0); |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 2480 | TEST_ASSERT(mbedtls_test_ssl_buffer_put(&buf, NULL, 0) == 0); |
| 2481 | TEST_ASSERT(mbedtls_test_ssl_buffer_get(&buf, NULL, 0) == 0); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2482 | |
Piotr Nowicki | fb437d7 | 2020-01-13 16:59:12 +0100 | [diff] [blame] | 2483 | /* Make sure calling put several times in the row is safe */ |
| 2484 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 2485 | TEST_ASSERT(mbedtls_test_ssl_buffer_put(&buf, input, sizeof(input)) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2486 | == sizeof(input)); |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 2487 | TEST_ASSERT(mbedtls_test_ssl_buffer_get(&buf, output, 2) == 2); |
| 2488 | TEST_ASSERT(mbedtls_test_ssl_buffer_put(&buf, input, 1) == 1); |
| 2489 | TEST_ASSERT(mbedtls_test_ssl_buffer_put(&buf, input, 2) == 1); |
| 2490 | TEST_ASSERT(mbedtls_test_ssl_buffer_put(&buf, input, 2) == 0); |
Piotr Nowicki | fb437d7 | 2020-01-13 16:59:12 +0100 | [diff] [blame] | 2491 | |
| 2492 | |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2493 | exit: |
| 2494 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 2495 | mbedtls_test_ssl_buffer_free(&buf); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2496 | } |
| 2497 | /* END_CASE */ |
| 2498 | |
| 2499 | /* |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 2500 | * Test if the implementation of `mbedtls_test_ssl_buffer` related functions is |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2501 | * correct and works as expected. |
| 2502 | * |
| 2503 | * That is |
| 2504 | * - If we try to put in \p put1 bytes then we can put in \p put1_ret bytes. |
| 2505 | * - Afterwards if we try to get \p get1 bytes then we can get \get1_ret bytes. |
| 2506 | * - Next, if we try to put in \p put1 bytes then we can put in \p put1_ret |
| 2507 | * bytes. |
| 2508 | * - Afterwards if we try to get \p get1 bytes then we can get \get1_ret bytes. |
| 2509 | * - All of the bytes we got match the bytes we put in in a FIFO manner. |
| 2510 | */ |
| 2511 | |
| 2512 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2513 | void test_callback_buffer(int size, int put1, int put1_ret, |
| 2514 | int get1, int get1_ret, int put2, int put2_ret, |
| 2515 | int get2, int get2_ret) |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2516 | { |
| 2517 | enum { ROUNDS = 2 }; |
| 2518 | size_t put[ROUNDS]; |
| 2519 | int put_ret[ROUNDS]; |
| 2520 | size_t get[ROUNDS]; |
| 2521 | int get_ret[ROUNDS]; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 2522 | mbedtls_test_ssl_buffer buf; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2523 | unsigned char *input = NULL; |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2524 | size_t input_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2525 | unsigned char *output = NULL; |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2526 | size_t output_len; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2527 | size_t i, j, written, read; |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2528 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 2529 | mbedtls_test_ssl_buffer_init(&buf); |
| 2530 | TEST_ASSERT(mbedtls_test_ssl_buffer_setup(&buf, size) == 0); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2531 | |
| 2532 | /* Check the sanity of input parameters and initialise local variables. That |
| 2533 | * is, ensure that the amount of data is not negative and that we are not |
| 2534 | * expecting more to put or get than we actually asked for. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2535 | TEST_ASSERT(put1 >= 0); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2536 | put[0] = put1; |
| 2537 | put_ret[0] = put1_ret; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2538 | TEST_ASSERT(put1_ret <= put1); |
| 2539 | TEST_ASSERT(put2 >= 0); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2540 | put[1] = put2; |
| 2541 | put_ret[1] = put2_ret; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2542 | TEST_ASSERT(put2_ret <= put2); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2543 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2544 | TEST_ASSERT(get1 >= 0); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2545 | get[0] = get1; |
| 2546 | get_ret[0] = get1_ret; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2547 | TEST_ASSERT(get1_ret <= get1); |
| 2548 | TEST_ASSERT(get2 >= 0); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2549 | get[1] = get2; |
| 2550 | get_ret[1] = get2_ret; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2551 | TEST_ASSERT(get2_ret <= get2); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2552 | |
| 2553 | input_len = 0; |
| 2554 | /* Calculate actual input and output lengths */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2555 | for (j = 0; j < ROUNDS; j++) { |
| 2556 | if (put_ret[j] > 0) { |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2557 | input_len += put_ret[j]; |
| 2558 | } |
| 2559 | } |
| 2560 | /* In order to always have a valid pointer we always allocate at least 1 |
| 2561 | * byte. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2562 | if (input_len == 0) { |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2563 | input_len = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2564 | } |
| 2565 | ASSERT_ALLOC(input, input_len); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2566 | |
| 2567 | output_len = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2568 | for (j = 0; j < ROUNDS; j++) { |
| 2569 | if (get_ret[j] > 0) { |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2570 | output_len += get_ret[j]; |
| 2571 | } |
| 2572 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2573 | TEST_ASSERT(output_len <= input_len); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2574 | /* In order to always have a valid pointer we always allocate at least 1 |
| 2575 | * byte. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2576 | if (output_len == 0) { |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2577 | output_len = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2578 | } |
| 2579 | ASSERT_ALLOC(output, output_len); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2580 | |
| 2581 | /* Fill up the buffer with structured data so that unwanted changes |
| 2582 | * can be detected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2583 | for (i = 0; i < input_len; i++) { |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2584 | input[i] = i & 0xFF; |
| 2585 | } |
| 2586 | |
| 2587 | written = read = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2588 | for (j = 0; j < ROUNDS; j++) { |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 2589 | TEST_ASSERT(put_ret[j] == mbedtls_test_ssl_buffer_put(&buf, |
| 2590 | input + written, put[j])); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2591 | written += put_ret[j]; |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 2592 | TEST_ASSERT(get_ret[j] == mbedtls_test_ssl_buffer_get(&buf, |
| 2593 | output + read, get[j])); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2594 | read += get_ret[j]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2595 | TEST_ASSERT(read <= written); |
| 2596 | if (get_ret[j] > 0) { |
| 2597 | TEST_ASSERT(memcmp(output + read - get_ret[j], |
| 2598 | input + read - get_ret[j], get_ret[j]) |
| 2599 | == 0); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2600 | } |
| 2601 | } |
| 2602 | |
| 2603 | exit: |
| 2604 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2605 | mbedtls_free(input); |
| 2606 | mbedtls_free(output); |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 2607 | mbedtls_test_ssl_buffer_free(&buf); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2608 | } |
| 2609 | /* END_CASE */ |
| 2610 | |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2611 | /* |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 2612 | * Test if the implementation of `mbedtls_test_mock_socket` related I/O functions is |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2613 | * correct and works as expected on unconnected sockets. |
| 2614 | */ |
| 2615 | |
| 2616 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2617 | void ssl_mock_sanity() |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2618 | { |
| 2619 | enum { MSGLEN = 105 }; |
Paul Elliott | 21c8fe5 | 2021-11-24 16:54:26 +0000 | [diff] [blame] | 2620 | unsigned char message[MSGLEN] = { 0 }; |
| 2621 | unsigned char received[MSGLEN] = { 0 }; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 2622 | mbedtls_test_mock_socket socket; |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2623 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2624 | mbedtls_mock_socket_init(&socket); |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 2625 | TEST_ASSERT(mbedtls_test_mock_tcp_send_b(&socket, message, MSGLEN) < 0); |
| 2626 | mbedtls_test_mock_socket_close(&socket); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2627 | mbedtls_mock_socket_init(&socket); |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 2628 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_b(&socket, received, MSGLEN) < 0); |
| 2629 | mbedtls_test_mock_socket_close(&socket); |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2630 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2631 | mbedtls_mock_socket_init(&socket); |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 2632 | TEST_ASSERT(mbedtls_test_mock_tcp_send_nb(&socket, message, MSGLEN) < 0); |
| 2633 | mbedtls_test_mock_socket_close(&socket); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2634 | mbedtls_mock_socket_init(&socket); |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 2635 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_nb(&socket, received, MSGLEN) < 0); |
| 2636 | mbedtls_test_mock_socket_close(&socket); |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2637 | |
| 2638 | exit: |
| 2639 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 2640 | mbedtls_test_mock_socket_close(&socket); |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2641 | } |
| 2642 | /* END_CASE */ |
| 2643 | |
| 2644 | /* |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 2645 | * Test if the implementation of `mbedtls_test_mock_socket` related functions can |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2646 | * send a single message from the client to the server. |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2647 | */ |
| 2648 | |
| 2649 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2650 | void ssl_mock_tcp(int blocking) |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2651 | { |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2652 | enum { MSGLEN = 105 }; |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2653 | enum { BUFLEN = MSGLEN / 5 }; |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2654 | unsigned char message[MSGLEN]; |
| 2655 | unsigned char received[MSGLEN]; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 2656 | mbedtls_test_mock_socket client; |
| 2657 | mbedtls_test_mock_socket server; |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2658 | size_t written, read; |
| 2659 | int send_ret, recv_ret; |
| 2660 | mbedtls_ssl_send_t *send; |
| 2661 | mbedtls_ssl_recv_t *recv; |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2662 | unsigned i; |
| 2663 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2664 | if (blocking == 0) { |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 2665 | send = mbedtls_test_mock_tcp_send_nb; |
| 2666 | recv = mbedtls_test_mock_tcp_recv_nb; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2667 | } else { |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 2668 | send = mbedtls_test_mock_tcp_send_b; |
| 2669 | recv = mbedtls_test_mock_tcp_recv_b; |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2670 | } |
| 2671 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2672 | mbedtls_mock_socket_init(&client); |
| 2673 | mbedtls_mock_socket_init(&server); |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2674 | |
| 2675 | /* Fill up the buffer with structured data so that unwanted changes |
| 2676 | * can be detected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2677 | for (i = 0; i < MSGLEN; i++) { |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2678 | message[i] = i & 0xFF; |
| 2679 | } |
| 2680 | |
| 2681 | /* Make sure that sending a message takes a few iterations. */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 2682 | TEST_ASSERT(0 == mbedtls_test_mock_socket_connect(&client, &server, BUFLEN)); |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2683 | |
| 2684 | /* Send the message to the server */ |
| 2685 | send_ret = recv_ret = 1; |
| 2686 | written = read = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2687 | while (send_ret != 0 || recv_ret != 0) { |
| 2688 | send_ret = send(&client, message + written, MSGLEN - written); |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2689 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2690 | TEST_ASSERT(send_ret >= 0); |
| 2691 | TEST_ASSERT(send_ret <= BUFLEN); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2692 | written += send_ret; |
| 2693 | |
| 2694 | /* 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] | 2695 | if (send_ret == BUFLEN) { |
| 2696 | int blocking_ret = send(&client, message, 1); |
| 2697 | if (blocking) { |
| 2698 | TEST_ASSERT(blocking_ret == 0); |
| 2699 | } else { |
| 2700 | TEST_ASSERT(blocking_ret == MBEDTLS_ERR_SSL_WANT_WRITE); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2701 | } |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2702 | } |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2703 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2704 | recv_ret = recv(&server, received + read, MSGLEN - read); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2705 | |
| 2706 | /* The result depends on whether any data was sent */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2707 | if (send_ret > 0) { |
| 2708 | TEST_ASSERT(recv_ret > 0); |
| 2709 | TEST_ASSERT(recv_ret <= BUFLEN); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2710 | read += recv_ret; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2711 | } else if (blocking) { |
| 2712 | TEST_ASSERT(recv_ret == 0); |
| 2713 | } else { |
| 2714 | TEST_ASSERT(recv_ret == MBEDTLS_ERR_SSL_WANT_READ); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2715 | recv_ret = 0; |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2716 | } |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2717 | |
| 2718 | /* 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] | 2719 | if (recv_ret == BUFLEN) { |
| 2720 | int blocking_ret = recv(&server, received, 1); |
| 2721 | if (blocking) { |
| 2722 | TEST_ASSERT(blocking_ret == 0); |
| 2723 | } else { |
| 2724 | TEST_ASSERT(blocking_ret == MBEDTLS_ERR_SSL_WANT_READ); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2725 | } |
| 2726 | } |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2727 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2728 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2729 | |
| 2730 | exit: |
| 2731 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 2732 | mbedtls_test_mock_socket_close(&client); |
| 2733 | mbedtls_test_mock_socket_close(&server); |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2734 | } |
| 2735 | /* END_CASE */ |
| 2736 | |
| 2737 | /* |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 2738 | * Test if the implementation of `mbedtls_test_mock_socket` related functions can |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2739 | * send messages in both direction at the same time (with the I/O calls |
| 2740 | * interleaving). |
| 2741 | */ |
| 2742 | |
| 2743 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2744 | void ssl_mock_tcp_interleaving(int blocking) |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2745 | { |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2746 | enum { ROUNDS = 2 }; |
| 2747 | enum { MSGLEN = 105 }; |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2748 | enum { BUFLEN = MSGLEN / 5 }; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2749 | unsigned char message[ROUNDS][MSGLEN]; |
| 2750 | unsigned char received[ROUNDS][MSGLEN]; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 2751 | mbedtls_test_mock_socket client; |
| 2752 | mbedtls_test_mock_socket server; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2753 | size_t written[ROUNDS]; |
| 2754 | size_t read[ROUNDS]; |
| 2755 | int send_ret[ROUNDS]; |
| 2756 | int recv_ret[ROUNDS]; |
| 2757 | unsigned i, j, progress; |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 2758 | mbedtls_ssl_send_t *send; |
| 2759 | mbedtls_ssl_recv_t *recv; |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 2760 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2761 | if (blocking == 0) { |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 2762 | send = mbedtls_test_mock_tcp_send_nb; |
| 2763 | recv = mbedtls_test_mock_tcp_recv_nb; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2764 | } else { |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 2765 | send = mbedtls_test_mock_tcp_send_b; |
| 2766 | recv = mbedtls_test_mock_tcp_recv_b; |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 2767 | } |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2768 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2769 | mbedtls_mock_socket_init(&client); |
| 2770 | mbedtls_mock_socket_init(&server); |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2771 | |
| 2772 | /* Fill up the buffers with structured data so that unwanted changes |
| 2773 | * can be detected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2774 | for (i = 0; i < ROUNDS; i++) { |
| 2775 | for (j = 0; j < MSGLEN; j++) { |
| 2776 | message[i][j] = (i * MSGLEN + j) & 0xFF; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2777 | } |
| 2778 | } |
| 2779 | |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2780 | /* Make sure that sending a message takes a few iterations. */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 2781 | TEST_ASSERT(0 == mbedtls_test_mock_socket_connect(&client, &server, BUFLEN)); |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2782 | |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2783 | /* Send the message from both sides, interleaving. */ |
| 2784 | progress = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2785 | for (i = 0; i < ROUNDS; i++) { |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2786 | written[i] = 0; |
| 2787 | read[i] = 0; |
| 2788 | } |
| 2789 | /* This loop does not stop as long as there was a successful write or read |
| 2790 | * of at least one byte on either side. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2791 | while (progress != 0) { |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 2792 | mbedtls_test_mock_socket *socket; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2793 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2794 | for (i = 0; i < ROUNDS; i++) { |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2795 | /* First sending is from the client */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2796 | socket = (i % 2 == 0) ? (&client) : (&server); |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2797 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2798 | send_ret[i] = send(socket, message[i] + written[i], |
| 2799 | MSGLEN - written[i]); |
| 2800 | TEST_ASSERT(send_ret[i] >= 0); |
| 2801 | TEST_ASSERT(send_ret[i] <= BUFLEN); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2802 | written[i] += send_ret[i]; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2803 | |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2804 | /* If the buffer is full we can test blocking and non-blocking |
| 2805 | * send */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2806 | if (send_ret[i] == BUFLEN) { |
| 2807 | int blocking_ret = send(socket, message[i], 1); |
| 2808 | if (blocking) { |
| 2809 | TEST_ASSERT(blocking_ret == 0); |
| 2810 | } else { |
| 2811 | TEST_ASSERT(blocking_ret == MBEDTLS_ERR_SSL_WANT_WRITE); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2812 | } |
| 2813 | } |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 2814 | } |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2815 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2816 | for (i = 0; i < ROUNDS; i++) { |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2817 | /* First receiving is from the server */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2818 | socket = (i % 2 == 0) ? (&server) : (&client); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2819 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2820 | recv_ret[i] = recv(socket, received[i] + read[i], |
| 2821 | MSGLEN - read[i]); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2822 | |
| 2823 | /* The result depends on whether any data was sent */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2824 | if (send_ret[i] > 0) { |
| 2825 | TEST_ASSERT(recv_ret[i] > 0); |
| 2826 | TEST_ASSERT(recv_ret[i] <= BUFLEN); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2827 | read[i] += recv_ret[i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2828 | } else if (blocking) { |
| 2829 | TEST_ASSERT(recv_ret[i] == 0); |
| 2830 | } else { |
| 2831 | TEST_ASSERT(recv_ret[i] == MBEDTLS_ERR_SSL_WANT_READ); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2832 | recv_ret[i] = 0; |
| 2833 | } |
| 2834 | |
| 2835 | /* If the buffer is empty we can test blocking and non-blocking |
| 2836 | * read */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2837 | if (recv_ret[i] == BUFLEN) { |
| 2838 | int blocking_ret = recv(socket, received[i], 1); |
| 2839 | if (blocking) { |
| 2840 | TEST_ASSERT(blocking_ret == 0); |
| 2841 | } else { |
| 2842 | TEST_ASSERT(blocking_ret == MBEDTLS_ERR_SSL_WANT_READ); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2843 | } |
| 2844 | } |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 2845 | } |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2846 | |
| 2847 | progress = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2848 | for (i = 0; i < ROUNDS; i++) { |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2849 | progress += send_ret[i] + recv_ret[i]; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2850 | } |
| 2851 | } |
| 2852 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2853 | for (i = 0; i < ROUNDS; i++) { |
| 2854 | TEST_ASSERT(memcmp(message[i], received[i], MSGLEN) == 0); |
| 2855 | } |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2856 | |
| 2857 | exit: |
| 2858 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 2859 | mbedtls_test_mock_socket_close(&client); |
| 2860 | mbedtls_test_mock_socket_close(&server); |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2861 | } |
| 2862 | /* END_CASE */ |
| 2863 | |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2864 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2865 | void ssl_message_queue_sanity() |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2866 | { |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 2867 | mbedtls_test_ssl_message_queue queue; |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2868 | |
| 2869 | /* Trying to push/pull to an empty queue */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 2870 | TEST_ASSERT(mbedtls_test_ssl_message_queue_push_info(NULL, 1) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2871 | == MBEDTLS_TEST_ERROR_ARG_NULL); |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 2872 | TEST_ASSERT(mbedtls_test_ssl_message_queue_pop_info(NULL, 1) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2873 | == MBEDTLS_TEST_ERROR_ARG_NULL); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2874 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 2875 | TEST_ASSERT(mbedtls_test_ssl_message_queue_setup(&queue, 3) == 0); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2876 | TEST_ASSERT(queue.capacity == 3); |
| 2877 | TEST_ASSERT(queue.num == 0); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2878 | |
| 2879 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 2880 | mbedtls_test_ssl_message_queue_free(&queue); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2881 | } |
| 2882 | /* END_CASE */ |
| 2883 | |
| 2884 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2885 | void ssl_message_queue_basic() |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2886 | { |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 2887 | mbedtls_test_ssl_message_queue queue; |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2888 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 2889 | TEST_ASSERT(mbedtls_test_ssl_message_queue_setup(&queue, 3) == 0); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2890 | |
| 2891 | /* Sanity test - 3 pushes and 3 pops with sufficient space */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 2892 | TEST_ASSERT(mbedtls_test_ssl_message_queue_push_info(&queue, 1) == 1); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2893 | TEST_ASSERT(queue.capacity == 3); |
| 2894 | TEST_ASSERT(queue.num == 1); |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 2895 | TEST_ASSERT(mbedtls_test_ssl_message_queue_push_info(&queue, 1) == 1); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2896 | TEST_ASSERT(queue.capacity == 3); |
| 2897 | TEST_ASSERT(queue.num == 2); |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 2898 | TEST_ASSERT(mbedtls_test_ssl_message_queue_push_info(&queue, 2) == 2); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2899 | TEST_ASSERT(queue.capacity == 3); |
| 2900 | TEST_ASSERT(queue.num == 3); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2901 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 2902 | TEST_ASSERT(mbedtls_test_ssl_message_queue_pop_info(&queue, 1) == 1); |
| 2903 | TEST_ASSERT(mbedtls_test_ssl_message_queue_pop_info(&queue, 1) == 1); |
| 2904 | TEST_ASSERT(mbedtls_test_ssl_message_queue_pop_info(&queue, 2) == 2); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2905 | |
| 2906 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 2907 | mbedtls_test_ssl_message_queue_free(&queue); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2908 | } |
| 2909 | /* END_CASE */ |
| 2910 | |
| 2911 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2912 | void ssl_message_queue_overflow_underflow() |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2913 | { |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 2914 | mbedtls_test_ssl_message_queue queue; |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2915 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 2916 | TEST_ASSERT(mbedtls_test_ssl_message_queue_setup(&queue, 3) == 0); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2917 | |
| 2918 | /* 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^] | 2919 | TEST_ASSERT(mbedtls_test_ssl_message_queue_push_info(&queue, 1) == 1); |
| 2920 | TEST_ASSERT(mbedtls_test_ssl_message_queue_push_info(&queue, 1) == 1); |
| 2921 | TEST_ASSERT(mbedtls_test_ssl_message_queue_push_info(&queue, 2) == 2); |
| 2922 | TEST_ASSERT(mbedtls_test_ssl_message_queue_push_info(&queue, 3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2923 | == MBEDTLS_ERR_SSL_WANT_WRITE); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2924 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 2925 | TEST_ASSERT(mbedtls_test_ssl_message_queue_pop_info(&queue, 1) == 1); |
| 2926 | TEST_ASSERT(mbedtls_test_ssl_message_queue_pop_info(&queue, 1) == 1); |
| 2927 | TEST_ASSERT(mbedtls_test_ssl_message_queue_pop_info(&queue, 2) == 2); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2928 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 2929 | TEST_ASSERT(mbedtls_test_ssl_message_queue_pop_info(&queue, 1) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2930 | == MBEDTLS_ERR_SSL_WANT_READ); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2931 | |
| 2932 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 2933 | mbedtls_test_ssl_message_queue_free(&queue); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2934 | } |
| 2935 | /* END_CASE */ |
| 2936 | |
| 2937 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2938 | void ssl_message_queue_interleaved() |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2939 | { |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 2940 | mbedtls_test_ssl_message_queue queue; |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2941 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 2942 | TEST_ASSERT(mbedtls_test_ssl_message_queue_setup(&queue, 3) == 0); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2943 | |
| 2944 | /* Interleaved test - [2 pushes, 1 pop] twice, and then two pops |
| 2945 | * (to wrap around the buffer) */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 2946 | TEST_ASSERT(mbedtls_test_ssl_message_queue_push_info(&queue, 1) == 1); |
| 2947 | TEST_ASSERT(mbedtls_test_ssl_message_queue_push_info(&queue, 1) == 1); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2948 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 2949 | TEST_ASSERT(mbedtls_test_ssl_message_queue_pop_info(&queue, 1) == 1); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2950 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 2951 | TEST_ASSERT(mbedtls_test_ssl_message_queue_push_info(&queue, 2) == 2); |
| 2952 | TEST_ASSERT(mbedtls_test_ssl_message_queue_push_info(&queue, 3) == 3); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2953 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 2954 | TEST_ASSERT(mbedtls_test_ssl_message_queue_pop_info(&queue, 1) == 1); |
| 2955 | TEST_ASSERT(mbedtls_test_ssl_message_queue_pop_info(&queue, 2) == 2); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2956 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 2957 | TEST_ASSERT(mbedtls_test_ssl_message_queue_push_info(&queue, 5) == 5); |
| 2958 | TEST_ASSERT(mbedtls_test_ssl_message_queue_push_info(&queue, 8) == 8); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2959 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 2960 | TEST_ASSERT(mbedtls_test_ssl_message_queue_pop_info(&queue, 3) == 3); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2961 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 2962 | TEST_ASSERT(mbedtls_test_ssl_message_queue_pop_info(&queue, 5) == 5); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2963 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 2964 | TEST_ASSERT(mbedtls_test_ssl_message_queue_pop_info(&queue, 8) == 8); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2965 | |
| 2966 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 2967 | mbedtls_test_ssl_message_queue_free(&queue); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2968 | } |
| 2969 | /* END_CASE */ |
| 2970 | |
| 2971 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2972 | void ssl_message_queue_insufficient_buffer() |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2973 | { |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 2974 | mbedtls_test_ssl_message_queue queue; |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2975 | size_t message_len = 10; |
| 2976 | size_t buffer_len = 5; |
| 2977 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 2978 | TEST_ASSERT(mbedtls_test_ssl_message_queue_setup(&queue, 1) == 0); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2979 | |
| 2980 | /* Popping without a sufficient buffer */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 2981 | TEST_ASSERT(mbedtls_test_ssl_message_queue_push_info(&queue, message_len) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2982 | == (int) message_len); |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 2983 | TEST_ASSERT(mbedtls_test_ssl_message_queue_pop_info(&queue, buffer_len) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2984 | == (int) buffer_len); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2985 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 2986 | mbedtls_test_ssl_message_queue_free(&queue); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2987 | } |
| 2988 | /* END_CASE */ |
| 2989 | |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2990 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2991 | void ssl_message_mock_uninitialized() |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2992 | { |
| 2993 | enum { MSGLEN = 10 }; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2994 | unsigned char message[MSGLEN] = { 0 }, received[MSGLEN]; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 2995 | mbedtls_test_mock_socket client, server; |
| 2996 | mbedtls_test_ssl_message_queue server_queue, client_queue; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2997 | mbedtls_test_message_socket_context server_context, client_context; |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 2998 | mbedtls_test_message_socket_init(&server_context); |
| 2999 | mbedtls_test_message_socket_init(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3000 | |
| 3001 | /* Send with a NULL context */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3002 | TEST_ASSERT(mbedtls_test_mock_tcp_send_msg(NULL, message, MSGLEN) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3003 | == MBEDTLS_TEST_ERROR_CONTEXT_ERROR); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3004 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3005 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(NULL, message, MSGLEN) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3006 | == MBEDTLS_TEST_ERROR_CONTEXT_ERROR); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3007 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3008 | TEST_ASSERT(mbedtls_test_message_socket_setup(&server_queue, &client_queue, 1, |
| 3009 | &server, |
| 3010 | &server_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3011 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3012 | TEST_ASSERT(mbedtls_test_message_socket_setup(&client_queue, &server_queue, 1, |
| 3013 | &client, |
| 3014 | &client_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3015 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3016 | TEST_ASSERT(mbedtls_test_mock_tcp_send_msg(&client_context, message, MSGLEN) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3017 | == MBEDTLS_TEST_ERROR_SEND_FAILED); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3018 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3019 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&server_context, received, MSGLEN) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3020 | == MBEDTLS_ERR_SSL_WANT_READ); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3021 | |
| 3022 | /* Push directly to a queue to later simulate a disconnected behavior */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3023 | TEST_ASSERT(mbedtls_test_ssl_message_queue_push_info(&server_queue, MSGLEN) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3024 | == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3025 | |
| 3026 | /* Test if there's an error when trying to read from a disconnected |
| 3027 | * socket */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3028 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&server_context, received, MSGLEN) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3029 | == MBEDTLS_TEST_ERROR_RECV_FAILED); |
| 3030 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3031 | mbedtls_test_message_socket_close(&server_context); |
| 3032 | mbedtls_test_message_socket_close(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3033 | } |
| 3034 | /* END_CASE */ |
| 3035 | |
| 3036 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3037 | void ssl_message_mock_basic() |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3038 | { |
| 3039 | enum { MSGLEN = 10 }; |
| 3040 | unsigned char message[MSGLEN], received[MSGLEN]; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 3041 | mbedtls_test_mock_socket client, server; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3042 | unsigned i; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 3043 | mbedtls_test_ssl_message_queue server_queue, client_queue; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3044 | mbedtls_test_message_socket_context server_context, client_context; |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3045 | mbedtls_test_message_socket_init(&server_context); |
| 3046 | mbedtls_test_message_socket_init(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3047 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3048 | TEST_ASSERT(mbedtls_test_message_socket_setup(&server_queue, &client_queue, 1, |
| 3049 | &server, |
| 3050 | &server_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3051 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3052 | TEST_ASSERT(mbedtls_test_message_socket_setup(&client_queue, &server_queue, 1, |
| 3053 | &client, |
| 3054 | &client_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3055 | |
| 3056 | /* Fill up the buffer with structured data so that unwanted changes |
| 3057 | * can be detected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3058 | for (i = 0; i < MSGLEN; i++) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3059 | message[i] = i & 0xFF; |
| 3060 | } |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3061 | TEST_ASSERT(0 == mbedtls_test_mock_socket_connect(&client, &server, |
| 3062 | MSGLEN)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3063 | |
| 3064 | /* Send the message to the server */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3065 | TEST_ASSERT(mbedtls_test_mock_tcp_send_msg(&client_context, message, |
| 3066 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3067 | |
| 3068 | /* Read from the server */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3069 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&server_context, received, MSGLEN) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3070 | == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3071 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3072 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
| 3073 | memset(received, 0, MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3074 | |
| 3075 | /* Send the message to the client */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3076 | TEST_ASSERT(mbedtls_test_mock_tcp_send_msg(&server_context, message, |
| 3077 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3078 | |
| 3079 | /* Read from the client */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3080 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&client_context, received, MSGLEN) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3081 | == MSGLEN); |
| 3082 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3083 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3084 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3085 | mbedtls_test_message_socket_close(&server_context); |
| 3086 | mbedtls_test_message_socket_close(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3087 | } |
| 3088 | /* END_CASE */ |
| 3089 | |
| 3090 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3091 | void ssl_message_mock_queue_overflow_underflow() |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3092 | { |
| 3093 | enum { MSGLEN = 10 }; |
| 3094 | unsigned char message[MSGLEN], received[MSGLEN]; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 3095 | mbedtls_test_mock_socket client, server; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3096 | unsigned i; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 3097 | mbedtls_test_ssl_message_queue server_queue, client_queue; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3098 | mbedtls_test_message_socket_context server_context, client_context; |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3099 | mbedtls_test_message_socket_init(&server_context); |
| 3100 | mbedtls_test_message_socket_init(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3101 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3102 | TEST_ASSERT(mbedtls_test_message_socket_setup(&server_queue, &client_queue, 2, |
| 3103 | &server, |
| 3104 | &server_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3105 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3106 | TEST_ASSERT(mbedtls_test_message_socket_setup(&client_queue, &server_queue, 2, |
| 3107 | &client, |
| 3108 | &client_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3109 | |
| 3110 | /* Fill up the buffer with structured data so that unwanted changes |
| 3111 | * can be detected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3112 | for (i = 0; i < MSGLEN; i++) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3113 | message[i] = i & 0xFF; |
| 3114 | } |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3115 | TEST_ASSERT(0 == mbedtls_test_mock_socket_connect(&client, &server, |
| 3116 | MSGLEN*2)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3117 | |
| 3118 | /* Send three message to the server, last one with an error */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3119 | TEST_ASSERT(mbedtls_test_mock_tcp_send_msg(&client_context, message, |
| 3120 | MSGLEN - 1) == MSGLEN - 1); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3121 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3122 | TEST_ASSERT(mbedtls_test_mock_tcp_send_msg(&client_context, message, |
| 3123 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3124 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3125 | TEST_ASSERT(mbedtls_test_mock_tcp_send_msg(&client_context, message, |
| 3126 | MSGLEN) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3127 | == MBEDTLS_ERR_SSL_WANT_WRITE); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3128 | |
| 3129 | /* Read three messages from the server, last one with an error */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3130 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&server_context, received, |
| 3131 | MSGLEN - 1) == MSGLEN - 1); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3132 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3133 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&server_context, received, MSGLEN) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3134 | == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3135 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3136 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3137 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3138 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&server_context, received, MSGLEN) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3139 | == MBEDTLS_ERR_SSL_WANT_READ); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3140 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3141 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3142 | mbedtls_test_message_socket_close(&server_context); |
| 3143 | mbedtls_test_message_socket_close(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3144 | } |
| 3145 | /* END_CASE */ |
| 3146 | |
| 3147 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3148 | void ssl_message_mock_socket_overflow() |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3149 | { |
| 3150 | enum { MSGLEN = 10 }; |
| 3151 | unsigned char message[MSGLEN], received[MSGLEN]; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 3152 | mbedtls_test_mock_socket client, server; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3153 | unsigned i; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 3154 | mbedtls_test_ssl_message_queue server_queue, client_queue; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3155 | mbedtls_test_message_socket_context server_context, client_context; |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3156 | mbedtls_test_message_socket_init(&server_context); |
| 3157 | mbedtls_test_message_socket_init(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3158 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3159 | TEST_ASSERT(mbedtls_test_message_socket_setup(&server_queue, &client_queue, 2, |
| 3160 | &server, |
| 3161 | &server_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3162 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3163 | TEST_ASSERT(mbedtls_test_message_socket_setup(&client_queue, &server_queue, 2, |
| 3164 | &client, |
| 3165 | &client_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3166 | |
| 3167 | /* Fill up the buffer with structured data so that unwanted changes |
| 3168 | * can be detected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3169 | for (i = 0; i < MSGLEN; i++) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3170 | message[i] = i & 0xFF; |
| 3171 | } |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3172 | TEST_ASSERT(0 == mbedtls_test_mock_socket_connect(&client, &server, |
| 3173 | MSGLEN)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3174 | |
| 3175 | /* Send two message to the server, second one with an error */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3176 | TEST_ASSERT(mbedtls_test_mock_tcp_send_msg(&client_context, message, |
| 3177 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3178 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3179 | TEST_ASSERT(mbedtls_test_mock_tcp_send_msg(&client_context, message, |
| 3180 | MSGLEN) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3181 | == MBEDTLS_TEST_ERROR_SEND_FAILED); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3182 | |
| 3183 | /* Read the only message from the server */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3184 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&server_context, received, MSGLEN) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3185 | == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3186 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3187 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3188 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3189 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3190 | mbedtls_test_message_socket_close(&server_context); |
| 3191 | mbedtls_test_message_socket_close(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3192 | } |
| 3193 | /* END_CASE */ |
| 3194 | |
| 3195 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3196 | void ssl_message_mock_truncated() |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3197 | { |
| 3198 | enum { MSGLEN = 10 }; |
| 3199 | unsigned char message[MSGLEN], received[MSGLEN]; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 3200 | mbedtls_test_mock_socket client, server; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3201 | unsigned i; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 3202 | mbedtls_test_ssl_message_queue server_queue, client_queue; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3203 | mbedtls_test_message_socket_context server_context, client_context; |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3204 | mbedtls_test_message_socket_init(&server_context); |
| 3205 | mbedtls_test_message_socket_init(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3206 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3207 | TEST_ASSERT(mbedtls_test_message_socket_setup(&server_queue, &client_queue, 2, |
| 3208 | &server, |
| 3209 | &server_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3210 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3211 | TEST_ASSERT(mbedtls_test_message_socket_setup(&client_queue, &server_queue, 2, |
| 3212 | &client, |
| 3213 | &client_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3214 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3215 | memset(received, 0, MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3216 | /* Fill up the buffer with structured data so that unwanted changes |
| 3217 | * can be detected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3218 | for (i = 0; i < MSGLEN; i++) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3219 | message[i] = i & 0xFF; |
| 3220 | } |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3221 | TEST_ASSERT(0 == mbedtls_test_mock_socket_connect(&client, &server, |
| 3222 | 2 * MSGLEN)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3223 | |
| 3224 | /* Send two messages to the server, the second one small enough to fit in the |
| 3225 | * receiver's buffer. */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3226 | TEST_ASSERT(mbedtls_test_mock_tcp_send_msg(&client_context, message, |
| 3227 | MSGLEN) == MSGLEN); |
| 3228 | TEST_ASSERT(mbedtls_test_mock_tcp_send_msg(&client_context, message, |
| 3229 | MSGLEN / 2) == MSGLEN / 2); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3230 | /* Read a truncated message from the server */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3231 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&server_context, received, MSGLEN/2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3232 | == MSGLEN/2); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3233 | |
| 3234 | /* 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] | 3235 | TEST_ASSERT(memcmp(message, received, MSGLEN/2) == 0); |
| 3236 | TEST_ASSERT(memcmp(message + MSGLEN/2, received + MSGLEN/2, MSGLEN/2) |
| 3237 | != 0); |
| 3238 | memset(received, 0, MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3239 | |
| 3240 | /* Read a full message from the server */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3241 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&server_context, received, MSGLEN/2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3242 | == MSGLEN / 2); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3243 | |
| 3244 | /* Test that the first half of the message is valid */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3245 | TEST_ASSERT(memcmp(message, received, MSGLEN/2) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3246 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3247 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3248 | mbedtls_test_message_socket_close(&server_context); |
| 3249 | mbedtls_test_message_socket_close(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3250 | } |
| 3251 | /* END_CASE */ |
| 3252 | |
| 3253 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3254 | void ssl_message_mock_socket_read_error() |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3255 | { |
| 3256 | enum { MSGLEN = 10 }; |
| 3257 | unsigned char message[MSGLEN], received[MSGLEN]; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 3258 | mbedtls_test_mock_socket client, server; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3259 | unsigned i; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 3260 | mbedtls_test_ssl_message_queue server_queue, client_queue; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3261 | mbedtls_test_message_socket_context server_context, client_context; |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3262 | mbedtls_test_message_socket_init(&server_context); |
| 3263 | mbedtls_test_message_socket_init(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3264 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3265 | TEST_ASSERT(mbedtls_test_message_socket_setup(&server_queue, &client_queue, 1, |
| 3266 | &server, |
| 3267 | &server_context) == 0); |
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_message_socket_setup(&client_queue, &server_queue, 1, |
| 3270 | &client, |
| 3271 | &client_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3272 | |
| 3273 | /* Fill up the buffer with structured data so that unwanted changes |
| 3274 | * can be detected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3275 | for (i = 0; i < MSGLEN; i++) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3276 | message[i] = i & 0xFF; |
| 3277 | } |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3278 | TEST_ASSERT(0 == mbedtls_test_mock_socket_connect(&client, &server, |
| 3279 | MSGLEN)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3280 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3281 | TEST_ASSERT(mbedtls_test_mock_tcp_send_msg(&client_context, message, |
| 3282 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3283 | |
| 3284 | /* Force a read error by disconnecting the socket by hand */ |
| 3285 | server.status = 0; |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3286 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&server_context, received, MSGLEN) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3287 | == MBEDTLS_TEST_ERROR_RECV_FAILED); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3288 | /* Return to a valid state */ |
| 3289 | server.status = MBEDTLS_MOCK_SOCKET_CONNECTED; |
| 3290 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3291 | memset(received, 0, sizeof(received)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3292 | |
| 3293 | /* Test that even though the server tried to read once disconnected, the |
| 3294 | * continuity is preserved */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3295 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&server_context, received, MSGLEN) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3296 | == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3297 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3298 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3299 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3300 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3301 | mbedtls_test_message_socket_close(&server_context); |
| 3302 | mbedtls_test_message_socket_close(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3303 | } |
| 3304 | /* END_CASE */ |
| 3305 | |
| 3306 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3307 | void ssl_message_mock_interleaved_one_way() |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3308 | { |
| 3309 | enum { MSGLEN = 10 }; |
| 3310 | unsigned char message[MSGLEN], received[MSGLEN]; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 3311 | mbedtls_test_mock_socket client, server; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3312 | unsigned i; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 3313 | mbedtls_test_ssl_message_queue server_queue, client_queue; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3314 | mbedtls_test_message_socket_context server_context, client_context; |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3315 | mbedtls_test_message_socket_init(&server_context); |
| 3316 | mbedtls_test_message_socket_init(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3317 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3318 | TEST_ASSERT(mbedtls_test_message_socket_setup(&server_queue, &client_queue, 3, |
| 3319 | &server, |
| 3320 | &server_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3321 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3322 | TEST_ASSERT(mbedtls_test_message_socket_setup(&client_queue, &server_queue, 3, |
| 3323 | &client, |
| 3324 | &client_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3325 | |
| 3326 | /* Fill up the buffer with structured data so that unwanted changes |
| 3327 | * can be detected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3328 | for (i = 0; i < MSGLEN; i++) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3329 | message[i] = i & 0xFF; |
| 3330 | } |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3331 | TEST_ASSERT(0 == mbedtls_test_mock_socket_connect(&client, &server, |
| 3332 | MSGLEN*3)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3333 | |
| 3334 | /* Interleaved test - [2 sends, 1 read] twice, and then two reads |
| 3335 | * (to wrap around the buffer) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3336 | for (i = 0; i < 2; i++) { |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3337 | TEST_ASSERT(mbedtls_test_mock_tcp_send_msg(&client_context, message, |
| 3338 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3339 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3340 | TEST_ASSERT(mbedtls_test_mock_tcp_send_msg(&client_context, message, |
| 3341 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3342 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3343 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&server_context, received, |
| 3344 | MSGLEN) == MSGLEN); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3345 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
| 3346 | memset(received, 0, sizeof(received)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3347 | } |
| 3348 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3349 | for (i = 0; i < 2; i++) { |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3350 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&server_context, received, |
| 3351 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3352 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3353 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3354 | } |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3355 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&server_context, received, MSGLEN) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3356 | == MBEDTLS_ERR_SSL_WANT_READ); |
| 3357 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3358 | mbedtls_test_message_socket_close(&server_context); |
| 3359 | mbedtls_test_message_socket_close(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3360 | } |
| 3361 | /* END_CASE */ |
| 3362 | |
| 3363 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3364 | void ssl_message_mock_interleaved_two_ways() |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3365 | { |
| 3366 | enum { MSGLEN = 10 }; |
| 3367 | unsigned char message[MSGLEN], received[MSGLEN]; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 3368 | mbedtls_test_mock_socket client, server; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3369 | unsigned i; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 3370 | mbedtls_test_ssl_message_queue server_queue, client_queue; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3371 | mbedtls_test_message_socket_context server_context, client_context; |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3372 | mbedtls_test_message_socket_init(&server_context); |
| 3373 | mbedtls_test_message_socket_init(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3374 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3375 | TEST_ASSERT(mbedtls_test_message_socket_setup(&server_queue, &client_queue, 3, |
| 3376 | &server, |
| 3377 | &server_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3378 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3379 | TEST_ASSERT(mbedtls_test_message_socket_setup(&client_queue, &server_queue, 3, |
| 3380 | &client, |
| 3381 | &client_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3382 | |
| 3383 | /* Fill up the buffer with structured data so that unwanted changes |
| 3384 | * can be detected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3385 | for (i = 0; i < MSGLEN; i++) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3386 | message[i] = i & 0xFF; |
| 3387 | } |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3388 | TEST_ASSERT(0 == mbedtls_test_mock_socket_connect(&client, &server, |
| 3389 | MSGLEN*3)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3390 | |
| 3391 | /* Interleaved test - [2 sends, 1 read] twice, both ways, and then two reads |
| 3392 | * (to wrap around the buffer) both ways. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3393 | for (i = 0; i < 2; i++) { |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3394 | TEST_ASSERT(mbedtls_test_mock_tcp_send_msg(&client_context, message, |
| 3395 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3396 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3397 | TEST_ASSERT(mbedtls_test_mock_tcp_send_msg(&client_context, message, |
| 3398 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3399 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3400 | TEST_ASSERT(mbedtls_test_mock_tcp_send_msg(&server_context, message, |
| 3401 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3402 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3403 | TEST_ASSERT(mbedtls_test_mock_tcp_send_msg(&server_context, message, |
| 3404 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3405 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3406 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&server_context, received, |
| 3407 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3408 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3409 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3410 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3411 | memset(received, 0, sizeof(received)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3412 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3413 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&client_context, received, |
| 3414 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3415 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3416 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3417 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3418 | memset(received, 0, sizeof(received)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3419 | } |
| 3420 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3421 | for (i = 0; i < 2; i++) { |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3422 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&server_context, received, |
| 3423 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3424 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3425 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
| 3426 | memset(received, 0, sizeof(received)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3427 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3428 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&client_context, received, |
| 3429 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3430 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3431 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
| 3432 | memset(received, 0, sizeof(received)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3433 | } |
| 3434 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3435 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&server_context, received, MSGLEN) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3436 | == MBEDTLS_ERR_SSL_WANT_READ); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3437 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3438 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&client_context, received, MSGLEN) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3439 | == MBEDTLS_ERR_SSL_WANT_READ); |
| 3440 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3441 | mbedtls_test_message_socket_close(&server_context); |
| 3442 | mbedtls_test_message_socket_close(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3443 | } |
| 3444 | /* END_CASE */ |
| 3445 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3446 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3447 | 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] | 3448 | { |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 3449 | uint32_t len = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3450 | mbedtls_ssl_context ssl; |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 3451 | mbedtls_ssl_config conf; |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3452 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3453 | mbedtls_ssl_init(&ssl); |
| 3454 | mbedtls_ssl_config_init(&conf); |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 3455 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3456 | TEST_ASSERT(mbedtls_ssl_config_defaults(&conf, |
| 3457 | MBEDTLS_SSL_IS_CLIENT, |
| 3458 | MBEDTLS_SSL_TRANSPORT_DATAGRAM, |
| 3459 | MBEDTLS_SSL_PRESET_DEFAULT) == 0); |
| 3460 | TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0); |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3461 | |
| 3462 | /* Read previous record numbers */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3463 | for (len = 0; len < prevs->len; len += 6) { |
| 3464 | memcpy(ssl.in_ctr + 2, prevs->x + len, 6); |
| 3465 | mbedtls_ssl_dtls_replay_update(&ssl); |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3466 | } |
| 3467 | |
| 3468 | /* Check new number */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3469 | memcpy(ssl.in_ctr + 2, new->x, 6); |
| 3470 | TEST_ASSERT(mbedtls_ssl_dtls_replay_check(&ssl) == ret); |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3471 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3472 | mbedtls_ssl_free(&ssl); |
| 3473 | mbedtls_ssl_config_free(&conf); |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3474 | } |
| 3475 | /* END_CASE */ |
Hanno Becker | b25c0c7 | 2017-05-05 11:24:30 +0100 | [diff] [blame] | 3476 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 3477 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3478 | void ssl_set_hostname_twice(char *hostname0, char *hostname1) |
Hanno Becker | b25c0c7 | 2017-05-05 11:24:30 +0100 | [diff] [blame] | 3479 | { |
| 3480 | mbedtls_ssl_context ssl; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3481 | mbedtls_ssl_init(&ssl); |
Hanno Becker | b25c0c7 | 2017-05-05 11:24:30 +0100 | [diff] [blame] | 3482 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3483 | TEST_ASSERT(mbedtls_ssl_set_hostname(&ssl, hostname0) == 0); |
| 3484 | TEST_ASSERT(mbedtls_ssl_set_hostname(&ssl, hostname1) == 0); |
Hanno Becker | b25c0c7 | 2017-05-05 11:24:30 +0100 | [diff] [blame] | 3485 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3486 | mbedtls_ssl_free(&ssl); |
Hanno Becker | b25c0c7 | 2017-05-05 11:24:30 +0100 | [diff] [blame] | 3487 | } |
Darryl Green | 11999bb | 2018-03-13 15:22:58 +0000 | [diff] [blame] | 3488 | /* END_CASE */ |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3489 | |
| 3490 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3491 | void ssl_crypt_record(int cipher_type, int hash_id, |
| 3492 | int etm, int tag_mode, int ver, |
| 3493 | int cid0_len, int cid1_len) |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3494 | { |
| 3495 | /* |
| 3496 | * Test several record encryptions and decryptions |
| 3497 | * with plenty of space before and after the data |
| 3498 | * within the record buffer. |
| 3499 | */ |
| 3500 | |
| 3501 | int ret; |
| 3502 | int num_records = 16; |
| 3503 | mbedtls_ssl_context ssl; /* ONLY for debugging */ |
| 3504 | |
| 3505 | mbedtls_ssl_transform t0, t1; |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 3506 | unsigned char *buf = NULL; |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3507 | size_t const buflen = 512; |
| 3508 | mbedtls_record rec, rec_backup; |
| 3509 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3510 | USE_PSA_INIT(); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 3511 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3512 | mbedtls_ssl_init(&ssl); |
| 3513 | mbedtls_ssl_transform_init(&t0); |
| 3514 | mbedtls_ssl_transform_init(&t1); |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3515 | ret = mbedtls_test_ssl_build_transforms(&t0, &t1, cipher_type, hash_id, |
| 3516 | etm, tag_mode, ver, |
| 3517 | (size_t) cid0_len, |
| 3518 | (size_t) cid1_len); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 3519 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3520 | TEST_ASSERT(ret == 0); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3521 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3522 | TEST_ASSERT((buf = mbedtls_calloc(1, buflen)) != NULL); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3523 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3524 | while (num_records-- > 0) { |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3525 | mbedtls_ssl_transform *t_dec, *t_enc; |
| 3526 | /* Take turns in who's sending and who's receiving. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3527 | if (num_records % 3 == 0) { |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3528 | t_dec = &t0; |
| 3529 | t_enc = &t1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3530 | } else { |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3531 | t_dec = &t1; |
| 3532 | t_enc = &t0; |
| 3533 | } |
| 3534 | |
| 3535 | /* |
| 3536 | * The record header affects the transformation in two ways: |
| 3537 | * 1) It determines the AEAD additional data |
| 3538 | * 2) The record counter sometimes determines the IV. |
| 3539 | * |
| 3540 | * Apart from that, the fields don't have influence. |
| 3541 | * In particular, it is currently not the responsibility |
| 3542 | * of ssl_encrypt/decrypt_buf to check if the transform |
| 3543 | * version matches the record version, or that the |
| 3544 | * type is sensible. |
| 3545 | */ |
| 3546 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3547 | memset(rec.ctr, num_records, sizeof(rec.ctr)); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3548 | rec.type = 42; |
| 3549 | rec.ver[0] = num_records; |
| 3550 | rec.ver[1] = num_records; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3551 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 3552 | rec.cid_len = 0; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3553 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3554 | |
| 3555 | rec.buf = buf; |
| 3556 | rec.buf_len = buflen; |
| 3557 | rec.data_offset = 16; |
| 3558 | /* Make sure to vary the length to exercise different |
| 3559 | * paddings. */ |
| 3560 | rec.data_len = 1 + num_records; |
| 3561 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3562 | memset(rec.buf + rec.data_offset, 42, rec.data_len); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3563 | |
| 3564 | /* Make a copy for later comparison */ |
| 3565 | rec_backup = rec; |
| 3566 | |
| 3567 | /* Encrypt record */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3568 | ret = mbedtls_ssl_encrypt_buf(&ssl, t_enc, &rec, |
| 3569 | mbedtls_test_rnd_std_rand, NULL); |
| 3570 | TEST_ASSERT(ret == 0 || ret == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL); |
| 3571 | if (ret != 0) { |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3572 | continue; |
| 3573 | } |
| 3574 | |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 3575 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3576 | if (rec.cid_len != 0) { |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 3577 | /* DTLS 1.2 + CID hides the real content type and |
| 3578 | * uses a special CID content type in the protected |
| 3579 | * record. Double-check this. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3580 | TEST_ASSERT(rec.type == MBEDTLS_SSL_MSG_CID); |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 3581 | } |
| 3582 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 3583 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3584 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3585 | if (t_enc->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 3586 | /* TLS 1.3 hides the real content type and |
| 3587 | * always uses Application Data as the content type |
| 3588 | * for protected records. Double-check this. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3589 | TEST_ASSERT(rec.type == MBEDTLS_SSL_MSG_APPLICATION_DATA); |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 3590 | } |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3591 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 3592 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3593 | /* Decrypt record with t_dec */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3594 | ret = mbedtls_ssl_decrypt_buf(&ssl, t_dec, &rec); |
| 3595 | TEST_ASSERT(ret == 0); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3596 | |
| 3597 | /* Compare results */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3598 | TEST_ASSERT(rec.type == rec_backup.type); |
| 3599 | TEST_ASSERT(memcmp(rec.ctr, rec_backup.ctr, 8) == 0); |
| 3600 | TEST_ASSERT(rec.ver[0] == rec_backup.ver[0]); |
| 3601 | TEST_ASSERT(rec.ver[1] == rec_backup.ver[1]); |
| 3602 | TEST_ASSERT(rec.data_len == rec_backup.data_len); |
| 3603 | TEST_ASSERT(rec.data_offset == rec_backup.data_offset); |
| 3604 | TEST_ASSERT(memcmp(rec.buf + rec.data_offset, |
| 3605 | rec_backup.buf + rec_backup.data_offset, |
| 3606 | rec.data_len) == 0); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3607 | } |
| 3608 | |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 3609 | exit: |
| 3610 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3611 | /* Cleanup */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3612 | mbedtls_ssl_free(&ssl); |
| 3613 | mbedtls_ssl_transform_free(&t0); |
| 3614 | mbedtls_ssl_transform_free(&t1); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3615 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3616 | mbedtls_free(buf); |
| 3617 | USE_PSA_DONE(); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3618 | } |
| 3619 | /* END_CASE */ |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3620 | |
| 3621 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3622 | void ssl_crypt_record_small(int cipher_type, int hash_id, |
| 3623 | int etm, int tag_mode, int ver, |
| 3624 | int cid0_len, int cid1_len) |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3625 | { |
| 3626 | /* |
| 3627 | * Test pairs of encryption and decryption with an increasing |
| 3628 | * amount of space in the record buffer - in more detail: |
| 3629 | * 1) Try to encrypt with 0, 1, 2, ... bytes available |
| 3630 | * in front of the plaintext, and expect the encryption |
| 3631 | * to succeed starting from some offset. Always keep |
| 3632 | * enough space in the end of the buffer. |
| 3633 | * 2) Try to encrypt with 0, 1, 2, ... bytes available |
| 3634 | * at the end of the plaintext, and expect the encryption |
| 3635 | * to succeed starting from some offset. Always keep |
| 3636 | * enough space at the beginning of the buffer. |
| 3637 | * 3) Try to encrypt with 0, 1, 2, ... bytes available |
| 3638 | * both at the front and end of the plaintext, |
| 3639 | * and expect the encryption to succeed starting from |
| 3640 | * some offset. |
| 3641 | * |
| 3642 | * If encryption succeeds, check that decryption succeeds |
| 3643 | * and yields the original record. |
| 3644 | */ |
| 3645 | |
| 3646 | mbedtls_ssl_context ssl; /* ONLY for debugging */ |
| 3647 | |
| 3648 | mbedtls_ssl_transform t0, t1; |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 3649 | unsigned char *buf = NULL; |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 3650 | size_t const buflen = 256; |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3651 | mbedtls_record rec, rec_backup; |
| 3652 | |
| 3653 | int ret; |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 3654 | int mode; /* Mode 1, 2 or 3 as explained above */ |
| 3655 | size_t offset; /* Available space at beginning/end/both */ |
| 3656 | size_t threshold = 96; /* Maximum offset to test against */ |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3657 | |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 3658 | size_t default_pre_padding = 64; /* Pre-padding to use in mode 2 */ |
| 3659 | 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] | 3660 | |
| 3661 | int seen_success; /* Indicates if in the current mode we've |
| 3662 | * already seen a successful test. */ |
| 3663 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3664 | USE_PSA_INIT(); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 3665 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3666 | mbedtls_ssl_init(&ssl); |
| 3667 | mbedtls_ssl_transform_init(&t0); |
| 3668 | mbedtls_ssl_transform_init(&t1); |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3669 | ret = mbedtls_test_ssl_build_transforms(&t0, &t1, cipher_type, hash_id, |
| 3670 | etm, tag_mode, ver, |
| 3671 | (size_t) cid0_len, |
| 3672 | (size_t) cid1_len); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 3673 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3674 | TEST_ASSERT(ret == 0); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3675 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3676 | TEST_ASSERT((buf = mbedtls_calloc(1, buflen)) != NULL); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3677 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3678 | for (mode = 1; mode <= 3; mode++) { |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3679 | seen_success = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3680 | for (offset = 0; offset <= threshold; offset++) { |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3681 | mbedtls_ssl_transform *t_dec, *t_enc; |
Hanno Becker | 6c87b3f | 2019-04-29 17:24:44 +0100 | [diff] [blame] | 3682 | t_dec = &t0; |
| 3683 | t_enc = &t1; |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3684 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3685 | memset(rec.ctr, offset, sizeof(rec.ctr)); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3686 | rec.type = 42; |
| 3687 | rec.ver[0] = offset; |
| 3688 | rec.ver[1] = offset; |
| 3689 | rec.buf = buf; |
| 3690 | rec.buf_len = buflen; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3691 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 3692 | rec.cid_len = 0; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3693 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3694 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3695 | switch (mode) { |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3696 | case 1: /* Space in the beginning */ |
| 3697 | rec.data_offset = offset; |
| 3698 | rec.data_len = buflen - offset - default_post_padding; |
| 3699 | break; |
| 3700 | |
| 3701 | case 2: /* Space in the end */ |
| 3702 | rec.data_offset = default_pre_padding; |
| 3703 | rec.data_len = buflen - default_pre_padding - offset; |
| 3704 | break; |
| 3705 | |
| 3706 | case 3: /* Space in the beginning and end */ |
| 3707 | rec.data_offset = offset; |
| 3708 | rec.data_len = buflen - 2 * offset; |
| 3709 | break; |
| 3710 | |
| 3711 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3712 | TEST_ASSERT(0); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3713 | break; |
| 3714 | } |
| 3715 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3716 | memset(rec.buf + rec.data_offset, 42, rec.data_len); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3717 | |
| 3718 | /* Make a copy for later comparison */ |
| 3719 | rec_backup = rec; |
| 3720 | |
| 3721 | /* Encrypt record */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3722 | ret = mbedtls_ssl_encrypt_buf(&ssl, t_enc, &rec, |
| 3723 | mbedtls_test_rnd_std_rand, NULL); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3724 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3725 | if ((mode == 1 || mode == 2) && seen_success) { |
| 3726 | TEST_ASSERT(ret == 0); |
| 3727 | } else { |
| 3728 | TEST_ASSERT(ret == 0 || ret == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL); |
| 3729 | if (ret == 0) { |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3730 | seen_success = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3731 | } |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3732 | } |
| 3733 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3734 | if (ret != 0) { |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3735 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3736 | } |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3737 | |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 3738 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3739 | if (rec.cid_len != 0) { |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 3740 | /* DTLS 1.2 + CID hides the real content type and |
| 3741 | * uses a special CID content type in the protected |
| 3742 | * record. Double-check this. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3743 | TEST_ASSERT(rec.type == MBEDTLS_SSL_MSG_CID); |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 3744 | } |
| 3745 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 3746 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3747 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3748 | if (t_enc->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 3749 | /* TLS 1.3 hides the real content type and |
| 3750 | * always uses Application Data as the content type |
| 3751 | * for protected records. Double-check this. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3752 | TEST_ASSERT(rec.type == MBEDTLS_SSL_MSG_APPLICATION_DATA); |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 3753 | } |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3754 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 3755 | |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3756 | /* Decrypt record with t_dec */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3757 | TEST_ASSERT(mbedtls_ssl_decrypt_buf(&ssl, t_dec, &rec) == 0); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3758 | |
| 3759 | /* Compare results */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3760 | TEST_ASSERT(rec.type == rec_backup.type); |
| 3761 | TEST_ASSERT(memcmp(rec.ctr, rec_backup.ctr, 8) == 0); |
| 3762 | TEST_ASSERT(rec.ver[0] == rec_backup.ver[0]); |
| 3763 | TEST_ASSERT(rec.ver[1] == rec_backup.ver[1]); |
| 3764 | TEST_ASSERT(rec.data_len == rec_backup.data_len); |
| 3765 | TEST_ASSERT(rec.data_offset == rec_backup.data_offset); |
| 3766 | TEST_ASSERT(memcmp(rec.buf + rec.data_offset, |
| 3767 | rec_backup.buf + rec_backup.data_offset, |
| 3768 | rec.data_len) == 0); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3769 | } |
| 3770 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3771 | TEST_ASSERT(seen_success == 1); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3772 | } |
| 3773 | |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 3774 | exit: |
| 3775 | |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3776 | /* Cleanup */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3777 | mbedtls_ssl_free(&ssl); |
| 3778 | mbedtls_ssl_transform_free(&t0); |
| 3779 | mbedtls_ssl_transform_free(&t1); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3780 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3781 | mbedtls_free(buf); |
| 3782 | USE_PSA_DONE(); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3783 | } |
| 3784 | /* END_CASE */ |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 3785 | |
Przemyslaw Stekiel | f4facef | 2022-02-02 21:31:04 +0100 | [diff] [blame] | 3786 | /* 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] | 3787 | void ssl_decrypt_non_etm_cbc(int cipher_type, int hash_id, int trunc_hmac, |
| 3788 | int length_selector) |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3789 | { |
| 3790 | /* |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3791 | * Test record decryption for CBC without EtM, focused on the verification |
| 3792 | * of padding and MAC. |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3793 | * |
TRodziewicz | 299510e | 2021-07-09 16:55:11 +0200 | [diff] [blame] | 3794 | * Actually depends on TLS 1.2 and either AES, ARIA or Camellia, but since |
| 3795 | * the test framework doesn't support alternation in dependency statements, |
| 3796 | * just depend on AES. |
Manuel Pégourié-Gonnard | 864abbf | 2020-07-21 10:37:14 +0200 | [diff] [blame] | 3797 | * |
| 3798 | * The length_selector argument is interpreted as follows: |
| 3799 | * - if it's -1, the plaintext length is 0 and minimal padding is applied |
| 3800 | * - if it's -2, the plaintext length is 0 and maximal padding is applied |
| 3801 | * - otherwise it must be in [0, 255] and is padding_length from RFC 5246: |
| 3802 | * it's the length of the rest of the padding, that is, excluding the |
| 3803 | * byte that encodes the length. The minimal non-zero plaintext length |
| 3804 | * that gives this padding_length is automatically selected. |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3805 | */ |
| 3806 | mbedtls_ssl_context ssl; /* ONLY for debugging */ |
| 3807 | mbedtls_ssl_transform t0, t1; |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3808 | mbedtls_record rec, rec_save; |
| 3809 | unsigned char *buf = NULL, *buf_save = NULL; |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3810 | size_t buflen, olen = 0; |
Manuel Pégourié-Gonnard | 864abbf | 2020-07-21 10:37:14 +0200 | [diff] [blame] | 3811 | size_t plaintext_len, block_size, i; |
Manuel Pégourié-Gonnard | e55653f | 2020-07-22 11:42:57 +0200 | [diff] [blame] | 3812 | unsigned char padlen; /* excluding the padding_length byte */ |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3813 | unsigned char add_data[13]; |
Neil Armstrong | cf8841a | 2022-02-24 11:17:45 +0100 | [diff] [blame] | 3814 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 3815 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 3816 | size_t sign_mac_length = 0; |
Andrzej Kurek | 8c95ac4 | 2022-08-17 16:17:00 -0400 | [diff] [blame] | 3817 | unsigned char mac[PSA_HASH_MAX_SIZE]; |
| 3818 | #else |
| 3819 | unsigned char mac[MBEDTLS_MD_MAX_SIZE]; |
Neil Armstrong | cf8841a | 2022-02-24 11:17:45 +0100 | [diff] [blame] | 3820 | #endif |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3821 | int exp_ret; |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 3822 | int ret; |
Manuel Pégourié-Gonnard | 4adc04a | 2020-07-16 10:00:48 +0200 | [diff] [blame] | 3823 | const unsigned char pad_max_len = 255; /* Per the standard */ |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3824 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3825 | USE_PSA_INIT(); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 3826 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3827 | mbedtls_ssl_init(&ssl); |
| 3828 | mbedtls_ssl_transform_init(&t0); |
| 3829 | mbedtls_ssl_transform_init(&t1); |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3830 | |
| 3831 | /* Set up transforms with dummy keys */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3832 | ret = mbedtls_test_ssl_build_transforms(&t0, &t1, cipher_type, hash_id, |
| 3833 | 0, trunc_hmac, |
| 3834 | MBEDTLS_SSL_VERSION_TLS1_2, |
| 3835 | 0, 0); |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3836 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3837 | TEST_ASSERT(ret == 0); |
Przemyslaw Stekiel | 4a36dd3 | 2022-01-25 00:43:58 +0100 | [diff] [blame] | 3838 | |
Manuel Pégourié-Gonnard | 864abbf | 2020-07-21 10:37:14 +0200 | [diff] [blame] | 3839 | /* Determine padding/plaintext length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3840 | TEST_ASSERT(length_selector >= -2 && length_selector <= 255); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3841 | block_size = t0.ivlen; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3842 | if (length_selector < 0) { |
Manuel Pégourié-Gonnard | 864abbf | 2020-07-21 10:37:14 +0200 | [diff] [blame] | 3843 | plaintext_len = 0; |
| 3844 | |
Manuel Pégourié-Gonnard | e55653f | 2020-07-22 11:42:57 +0200 | [diff] [blame] | 3845 | /* Minimal padding |
| 3846 | * The +1 is for the padding_length byte, not counted in padlen. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3847 | padlen = block_size - (t0.maclen + 1) % block_size; |
Manuel Pégourié-Gonnard | 864abbf | 2020-07-21 10:37:14 +0200 | [diff] [blame] | 3848 | |
| 3849 | /* Maximal padding? */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3850 | if (length_selector == -2) { |
| 3851 | padlen += block_size * ((pad_max_len - padlen) / block_size); |
| 3852 | } |
| 3853 | } else { |
Manuel Pégourié-Gonnard | 864abbf | 2020-07-21 10:37:14 +0200 | [diff] [blame] | 3854 | padlen = length_selector; |
| 3855 | |
Manuel Pégourié-Gonnard | e55653f | 2020-07-22 11:42:57 +0200 | [diff] [blame] | 3856 | /* Minimal non-zero plaintext_length giving desired padding. |
| 3857 | * The +1 is for the padding_length byte, not counted in padlen. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3858 | plaintext_len = block_size - (padlen + t0.maclen + 1) % block_size; |
Manuel Pégourié-Gonnard | 864abbf | 2020-07-21 10:37:14 +0200 | [diff] [blame] | 3859 | } |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3860 | |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3861 | /* Prepare a buffer for record data */ |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3862 | buflen = block_size |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3863 | + plaintext_len |
| 3864 | + t0.maclen |
| 3865 | + padlen + 1; |
| 3866 | ASSERT_ALLOC(buf, buflen); |
| 3867 | ASSERT_ALLOC(buf_save, buflen); |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3868 | |
| 3869 | /* Prepare a dummy record header */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3870 | memset(rec.ctr, 0, sizeof(rec.ctr)); |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3871 | rec.type = MBEDTLS_SSL_MSG_APPLICATION_DATA; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3872 | mbedtls_ssl_write_version(rec.ver, MBEDTLS_SSL_TRANSPORT_STREAM, |
| 3873 | MBEDTLS_SSL_VERSION_TLS1_2); |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3874 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 3875 | rec.cid_len = 0; |
| 3876 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 3877 | |
| 3878 | /* Prepare dummy record content */ |
| 3879 | rec.buf = buf; |
| 3880 | rec.buf_len = buflen; |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3881 | rec.data_offset = block_size; |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3882 | rec.data_len = plaintext_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3883 | memset(rec.buf + rec.data_offset, 42, rec.data_len); |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3884 | |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3885 | /* Serialized version of record header for MAC purposes */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3886 | memcpy(add_data, rec.ctr, 8); |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3887 | add_data[8] = rec.type; |
| 3888 | add_data[9] = rec.ver[0]; |
| 3889 | add_data[10] = rec.ver[1]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3890 | add_data[11] = (rec.data_len >> 8) & 0xff; |
| 3891 | add_data[12] = (rec.data_len >> 0) & 0xff; |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3892 | |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3893 | /* Set dummy IV */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3894 | memset(t0.iv_enc, 0x55, t0.ivlen); |
| 3895 | memcpy(rec.buf, t0.iv_enc, t0.ivlen); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3896 | |
| 3897 | /* |
| 3898 | * Prepare a pre-encryption record (with MAC and padding), and save it. |
| 3899 | */ |
| 3900 | |
| 3901 | /* MAC with additional data */ |
Neil Armstrong | cf8841a | 2022-02-24 11:17:45 +0100 | [diff] [blame] | 3902 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3903 | TEST_EQUAL(PSA_SUCCESS, psa_mac_sign_setup(&operation, |
| 3904 | t0.psa_mac_enc, |
| 3905 | t0.psa_mac_alg)); |
| 3906 | TEST_EQUAL(PSA_SUCCESS, psa_mac_update(&operation, add_data, 13)); |
| 3907 | TEST_EQUAL(PSA_SUCCESS, psa_mac_update(&operation, |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3908 | rec.buf + rec.data_offset, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3909 | rec.data_len)); |
| 3910 | TEST_EQUAL(PSA_SUCCESS, psa_mac_sign_finish(&operation, |
| 3911 | mac, sizeof(mac), |
| 3912 | &sign_mac_length)); |
| 3913 | #else |
| 3914 | TEST_EQUAL(0, mbedtls_md_hmac_update(&t0.md_ctx_enc, add_data, 13)); |
| 3915 | TEST_EQUAL(0, mbedtls_md_hmac_update(&t0.md_ctx_enc, |
| 3916 | rec.buf + rec.data_offset, |
| 3917 | rec.data_len)); |
| 3918 | TEST_EQUAL(0, mbedtls_md_hmac_finish(&t0.md_ctx_enc, mac)); |
Neil Armstrong | cf8841a | 2022-02-24 11:17:45 +0100 | [diff] [blame] | 3919 | #endif |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3920 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3921 | 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] | 3922 | rec.data_len += t0.maclen; |
| 3923 | |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3924 | /* Pad */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3925 | 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] | 3926 | rec.data_len += padlen + 1; |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3927 | |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3928 | /* Save correct pre-encryption record */ |
| 3929 | rec_save = rec; |
| 3930 | rec_save.buf = buf_save; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3931 | memcpy(buf_save, buf, buflen); |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3932 | |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3933 | /* |
| 3934 | * Encrypt and decrypt the correct record, expecting success |
| 3935 | */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3936 | TEST_EQUAL(0, mbedtls_test_psa_cipher_encrypt_helper(&t0, t0.iv_enc, t0.ivlen, |
| 3937 | rec.buf + rec.data_offset, rec.data_len, |
| 3938 | rec.buf + rec.data_offset, &olen)); |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3939 | rec.data_offset -= t0.ivlen; |
| 3940 | rec.data_len += t0.ivlen; |
| 3941 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3942 | TEST_EQUAL(0, mbedtls_ssl_decrypt_buf(&ssl, &t1, &rec)); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3943 | |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3944 | /* |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3945 | * Modify each byte of the pre-encryption record before encrypting and |
| 3946 | * decrypting it, expecting failure every time. |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3947 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3948 | for (i = block_size; i < buflen; i++) { |
| 3949 | mbedtls_test_set_step(i); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3950 | |
| 3951 | /* Restore correct pre-encryption record */ |
| 3952 | rec = rec_save; |
| 3953 | rec.buf = buf; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3954 | memcpy(buf, buf_save, buflen); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3955 | |
Manuel Pégourié-Gonnard | b51f044 | 2020-07-21 10:40:25 +0200 | [diff] [blame] | 3956 | /* 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] | 3957 | rec.buf[i] ^= 0x01; |
| 3958 | |
| 3959 | /* Encrypt */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3960 | TEST_EQUAL(0, mbedtls_test_psa_cipher_encrypt_helper(&t0, t0.iv_enc, t0.ivlen, |
| 3961 | rec.buf + rec.data_offset, |
| 3962 | rec.data_len, |
| 3963 | rec.buf + rec.data_offset, &olen)); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3964 | rec.data_offset -= t0.ivlen; |
| 3965 | rec.data_len += t0.ivlen; |
| 3966 | |
| 3967 | /* Decrypt and expect failure */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3968 | TEST_EQUAL(MBEDTLS_ERR_SSL_INVALID_MAC, |
| 3969 | mbedtls_ssl_decrypt_buf(&ssl, &t1, &rec)); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3970 | } |
| 3971 | |
| 3972 | /* |
| 3973 | * Use larger values of the padding bytes - with small buffers, this tests |
| 3974 | * the case where the announced padlen would be larger than the buffer |
| 3975 | * (and before that, than the buffer minus the size of the MAC), to make |
| 3976 | * sure our padding checking code does not perform any out-of-bounds reads |
| 3977 | * in this case. (With larger buffers, ie when the plaintext is long or |
| 3978 | * maximal length padding is used, this is less relevant but still doesn't |
| 3979 | * hurt to test.) |
| 3980 | * |
| 3981 | * (Start the loop with correct padding, just to double-check that record |
| 3982 | * saving did work, and that we're overwriting the correct bytes.) |
| 3983 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3984 | for (i = padlen; i <= pad_max_len; i++) { |
| 3985 | mbedtls_test_set_step(i); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3986 | |
| 3987 | /* Restore correct pre-encryption record */ |
| 3988 | rec = rec_save; |
| 3989 | rec.buf = buf; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3990 | memcpy(buf, buf_save, buflen); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3991 | |
| 3992 | /* Set padding bytes to new value */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3993 | memset(buf + buflen - padlen - 1, i, padlen + 1); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3994 | |
| 3995 | /* Encrypt */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 3996 | TEST_EQUAL(0, mbedtls_test_psa_cipher_encrypt_helper(&t0, t0.iv_enc, t0.ivlen, |
| 3997 | rec.buf + rec.data_offset, |
| 3998 | rec.data_len, |
| 3999 | rec.buf + rec.data_offset, &olen)); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 4000 | rec.data_offset -= t0.ivlen; |
| 4001 | rec.data_len += t0.ivlen; |
| 4002 | |
| 4003 | /* Decrypt and expect failure except the first time */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4004 | exp_ret = (i == padlen) ? 0 : MBEDTLS_ERR_SSL_INVALID_MAC; |
| 4005 | 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] | 4006 | } |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 4007 | |
| 4008 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4009 | mbedtls_ssl_free(&ssl); |
| 4010 | mbedtls_ssl_transform_free(&t0); |
| 4011 | mbedtls_ssl_transform_free(&t1); |
| 4012 | mbedtls_free(buf); |
| 4013 | mbedtls_free(buf_save); |
| 4014 | USE_PSA_DONE(); |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 4015 | } |
| 4016 | /* END_CASE */ |
| 4017 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4018 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4019 | void ssl_tls13_hkdf_expand_label(int hash_alg, |
| 4020 | data_t *secret, |
| 4021 | int label_idx, |
| 4022 | data_t *ctx, |
| 4023 | int desired_length, |
| 4024 | data_t *expected) |
Hanno Becker | 39ff492 | 2020-08-21 13:36:56 +0100 | [diff] [blame] | 4025 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4026 | unsigned char dst[100]; |
Hanno Becker | 39ff492 | 2020-08-21 13:36:56 +0100 | [diff] [blame] | 4027 | |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 4028 | unsigned char const *lbl = NULL; |
| 4029 | size_t lbl_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4030 | #define MBEDTLS_SSL_TLS1_3_LABEL(name, string) \ |
| 4031 | if (label_idx == (int) tls13_label_ ## name) \ |
Xiaofei Bai | d25fab6 | 2021-12-02 06:36:27 +0000 | [diff] [blame] | 4032 | { \ |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4033 | lbl = mbedtls_ssl_tls13_labels.name; \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4034 | lbl_len = sizeof(mbedtls_ssl_tls13_labels.name); \ |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 4035 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4036 | MBEDTLS_SSL_TLS1_3_LABEL_LIST |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 4037 | #undef MBEDTLS_SSL_TLS1_3_LABEL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4038 | TEST_ASSERT(lbl != NULL); |
Hanno Becker | 39ff492 | 2020-08-21 13:36:56 +0100 | [diff] [blame] | 4039 | |
| 4040 | /* Check sanity of test parameters. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4041 | TEST_ASSERT((size_t) desired_length <= sizeof(dst)); |
| 4042 | TEST_ASSERT((size_t) desired_length == expected->len); |
Hanno Becker | 39ff492 | 2020-08-21 13:36:56 +0100 | [diff] [blame] | 4043 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4044 | PSA_INIT(); |
Gabor Mezei | 892c4aa | 2022-03-21 12:21:43 +0100 | [diff] [blame] | 4045 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4046 | TEST_ASSERT(mbedtls_ssl_tls13_hkdf_expand_label( |
| 4047 | (psa_algorithm_t) hash_alg, |
| 4048 | secret->x, secret->len, |
| 4049 | lbl, lbl_len, |
| 4050 | ctx->x, ctx->len, |
| 4051 | dst, desired_length) == 0); |
Hanno Becker | 39ff492 | 2020-08-21 13:36:56 +0100 | [diff] [blame] | 4052 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4053 | ASSERT_COMPARE(dst, (size_t) desired_length, |
| 4054 | expected->x, (size_t) expected->len); |
Gabor Mezei | 892c4aa | 2022-03-21 12:21:43 +0100 | [diff] [blame] | 4055 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4056 | PSA_DONE(); |
Hanno Becker | 39ff492 | 2020-08-21 13:36:56 +0100 | [diff] [blame] | 4057 | } |
| 4058 | /* END_CASE */ |
| 4059 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4060 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4061 | void ssl_tls13_traffic_key_generation(int hash_alg, |
| 4062 | data_t *server_secret, |
| 4063 | data_t *client_secret, |
| 4064 | int desired_iv_len, |
| 4065 | int desired_key_len, |
| 4066 | data_t *expected_server_write_key, |
| 4067 | data_t *expected_server_write_iv, |
| 4068 | data_t *expected_client_write_key, |
| 4069 | data_t *expected_client_write_iv) |
Hanno Becker | 19498f8 | 2020-08-21 13:37:08 +0100 | [diff] [blame] | 4070 | { |
| 4071 | mbedtls_ssl_key_set keys; |
| 4072 | |
| 4073 | /* Check sanity of test parameters. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4074 | TEST_ASSERT(client_secret->len == server_secret->len); |
| 4075 | TEST_ASSERT(expected_client_write_iv->len == expected_server_write_iv->len && |
| 4076 | expected_client_write_iv->len == (size_t) desired_iv_len); |
| 4077 | TEST_ASSERT(expected_client_write_key->len == expected_server_write_key->len && |
| 4078 | expected_client_write_key->len == (size_t) desired_key_len); |
Hanno Becker | 19498f8 | 2020-08-21 13:37:08 +0100 | [diff] [blame] | 4079 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4080 | PSA_INIT(); |
Gabor Mezei | 892c4aa | 2022-03-21 12:21:43 +0100 | [diff] [blame] | 4081 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4082 | TEST_ASSERT(mbedtls_ssl_tls13_make_traffic_keys( |
| 4083 | (psa_algorithm_t) hash_alg, |
| 4084 | client_secret->x, |
| 4085 | server_secret->x, |
| 4086 | client_secret->len /* == server_secret->len */, |
| 4087 | desired_key_len, desired_iv_len, |
| 4088 | &keys) == 0); |
Hanno Becker | 19498f8 | 2020-08-21 13:37:08 +0100 | [diff] [blame] | 4089 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4090 | ASSERT_COMPARE(keys.client_write_key, |
| 4091 | keys.key_len, |
| 4092 | expected_client_write_key->x, |
| 4093 | (size_t) desired_key_len); |
| 4094 | ASSERT_COMPARE(keys.server_write_key, |
| 4095 | keys.key_len, |
| 4096 | expected_server_write_key->x, |
| 4097 | (size_t) desired_key_len); |
| 4098 | ASSERT_COMPARE(keys.client_write_iv, |
| 4099 | keys.iv_len, |
| 4100 | expected_client_write_iv->x, |
| 4101 | (size_t) desired_iv_len); |
| 4102 | ASSERT_COMPARE(keys.server_write_iv, |
| 4103 | keys.iv_len, |
| 4104 | expected_server_write_iv->x, |
| 4105 | (size_t) desired_iv_len); |
Gabor Mezei | 892c4aa | 2022-03-21 12:21:43 +0100 | [diff] [blame] | 4106 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4107 | PSA_DONE(); |
Hanno Becker | 19498f8 | 2020-08-21 13:37:08 +0100 | [diff] [blame] | 4108 | } |
| 4109 | /* END_CASE */ |
| 4110 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4111 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4112 | void ssl_tls13_derive_secret(int hash_alg, |
| 4113 | data_t *secret, |
| 4114 | int label_idx, |
| 4115 | data_t *ctx, |
| 4116 | int desired_length, |
| 4117 | int already_hashed, |
| 4118 | data_t *expected) |
Hanno Becker | e4849d1 | 2020-08-21 14:14:14 +0100 | [diff] [blame] | 4119 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4120 | unsigned char dst[100]; |
Hanno Becker | e4849d1 | 2020-08-21 14:14:14 +0100 | [diff] [blame] | 4121 | |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 4122 | unsigned char const *lbl = NULL; |
| 4123 | size_t lbl_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4124 | #define MBEDTLS_SSL_TLS1_3_LABEL(name, string) \ |
| 4125 | if (label_idx == (int) tls13_label_ ## name) \ |
Xiaofei Bai | d25fab6 | 2021-12-02 06:36:27 +0000 | [diff] [blame] | 4126 | { \ |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4127 | lbl = mbedtls_ssl_tls13_labels.name; \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4128 | lbl_len = sizeof(mbedtls_ssl_tls13_labels.name); \ |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 4129 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4130 | MBEDTLS_SSL_TLS1_3_LABEL_LIST |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 4131 | #undef MBEDTLS_SSL_TLS1_3_LABEL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4132 | TEST_ASSERT(lbl != NULL); |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 4133 | |
Hanno Becker | e4849d1 | 2020-08-21 14:14:14 +0100 | [diff] [blame] | 4134 | /* Check sanity of test parameters. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4135 | TEST_ASSERT((size_t) desired_length <= sizeof(dst)); |
| 4136 | TEST_ASSERT((size_t) desired_length == expected->len); |
Hanno Becker | e4849d1 | 2020-08-21 14:14:14 +0100 | [diff] [blame] | 4137 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4138 | PSA_INIT(); |
Gabor Mezei | 892c4aa | 2022-03-21 12:21:43 +0100 | [diff] [blame] | 4139 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4140 | TEST_ASSERT(mbedtls_ssl_tls13_derive_secret( |
| 4141 | (psa_algorithm_t) hash_alg, |
| 4142 | secret->x, secret->len, |
| 4143 | lbl, lbl_len, |
| 4144 | ctx->x, ctx->len, |
| 4145 | already_hashed, |
| 4146 | dst, desired_length) == 0); |
Hanno Becker | e4849d1 | 2020-08-21 14:14:14 +0100 | [diff] [blame] | 4147 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4148 | ASSERT_COMPARE(dst, desired_length, |
| 4149 | expected->x, desired_length); |
Gabor Mezei | 892c4aa | 2022-03-21 12:21:43 +0100 | [diff] [blame] | 4150 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4151 | PSA_DONE(); |
Hanno Becker | e4849d1 | 2020-08-21 14:14:14 +0100 | [diff] [blame] | 4152 | } |
| 4153 | /* END_CASE */ |
| 4154 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4155 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4156 | void ssl_tls13_derive_early_secrets(int hash_alg, |
| 4157 | data_t *secret, |
| 4158 | data_t *transcript, |
| 4159 | data_t *traffic_expected, |
| 4160 | data_t *exporter_expected) |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4161 | { |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4162 | mbedtls_ssl_tls13_early_secrets secrets; |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4163 | |
| 4164 | /* Double-check that we've passed sane parameters. */ |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 4165 | psa_algorithm_t alg = (psa_algorithm_t) hash_alg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4166 | size_t const hash_len = PSA_HASH_LENGTH(alg); |
| 4167 | TEST_ASSERT(PSA_ALG_IS_HASH(alg) && |
| 4168 | secret->len == hash_len && |
| 4169 | transcript->len == hash_len && |
| 4170 | traffic_expected->len == hash_len && |
| 4171 | exporter_expected->len == hash_len); |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4172 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4173 | PSA_INIT(); |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4174 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4175 | TEST_ASSERT(mbedtls_ssl_tls13_derive_early_secrets( |
| 4176 | alg, secret->x, transcript->x, transcript->len, |
| 4177 | &secrets) == 0); |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4178 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4179 | ASSERT_COMPARE(secrets.client_early_traffic_secret, hash_len, |
| 4180 | traffic_expected->x, traffic_expected->len); |
| 4181 | ASSERT_COMPARE(secrets.early_exporter_master_secret, hash_len, |
| 4182 | exporter_expected->x, exporter_expected->len); |
Gabor Mezei | 892c4aa | 2022-03-21 12:21:43 +0100 | [diff] [blame] | 4183 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4184 | PSA_DONE(); |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4185 | } |
| 4186 | /* END_CASE */ |
| 4187 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4188 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4189 | void ssl_tls13_derive_handshake_secrets(int hash_alg, |
| 4190 | data_t *secret, |
| 4191 | data_t *transcript, |
| 4192 | data_t *client_expected, |
| 4193 | data_t *server_expected) |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4194 | { |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4195 | mbedtls_ssl_tls13_handshake_secrets secrets; |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4196 | |
| 4197 | /* Double-check that we've passed sane parameters. */ |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 4198 | psa_algorithm_t alg = (psa_algorithm_t) hash_alg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4199 | size_t const hash_len = PSA_HASH_LENGTH(alg); |
| 4200 | TEST_ASSERT(PSA_ALG_IS_HASH(alg) && |
| 4201 | secret->len == hash_len && |
| 4202 | transcript->len == hash_len && |
| 4203 | client_expected->len == hash_len && |
| 4204 | server_expected->len == hash_len); |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4205 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4206 | PSA_INIT(); |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4207 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4208 | TEST_ASSERT(mbedtls_ssl_tls13_derive_handshake_secrets( |
| 4209 | alg, secret->x, transcript->x, transcript->len, |
| 4210 | &secrets) == 0); |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4211 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4212 | ASSERT_COMPARE(secrets.client_handshake_traffic_secret, hash_len, |
| 4213 | client_expected->x, client_expected->len); |
| 4214 | ASSERT_COMPARE(secrets.server_handshake_traffic_secret, hash_len, |
| 4215 | server_expected->x, server_expected->len); |
Gabor Mezei | 892c4aa | 2022-03-21 12:21:43 +0100 | [diff] [blame] | 4216 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4217 | PSA_DONE(); |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4218 | } |
| 4219 | /* END_CASE */ |
| 4220 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4221 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4222 | void ssl_tls13_derive_application_secrets(int hash_alg, |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4223 | data_t *secret, |
| 4224 | data_t *transcript, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4225 | data_t *client_expected, |
| 4226 | data_t *server_expected, |
| 4227 | data_t *exporter_expected) |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4228 | { |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4229 | mbedtls_ssl_tls13_application_secrets secrets; |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4230 | |
| 4231 | /* Double-check that we've passed sane parameters. */ |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 4232 | psa_algorithm_t alg = (psa_algorithm_t) hash_alg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4233 | size_t const hash_len = PSA_HASH_LENGTH(alg); |
| 4234 | TEST_ASSERT(PSA_ALG_IS_HASH(alg) && |
| 4235 | secret->len == hash_len && |
| 4236 | transcript->len == hash_len && |
| 4237 | client_expected->len == hash_len && |
| 4238 | server_expected->len == hash_len && |
| 4239 | exporter_expected->len == hash_len); |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4240 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4241 | PSA_INIT(); |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4242 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4243 | TEST_ASSERT(mbedtls_ssl_tls13_derive_application_secrets( |
| 4244 | alg, secret->x, transcript->x, transcript->len, |
| 4245 | &secrets) == 0); |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4246 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4247 | ASSERT_COMPARE(secrets.client_application_traffic_secret_N, hash_len, |
| 4248 | client_expected->x, client_expected->len); |
| 4249 | ASSERT_COMPARE(secrets.server_application_traffic_secret_N, hash_len, |
| 4250 | server_expected->x, server_expected->len); |
| 4251 | ASSERT_COMPARE(secrets.exporter_master_secret, hash_len, |
| 4252 | exporter_expected->x, exporter_expected->len); |
Gabor Mezei | 892c4aa | 2022-03-21 12:21:43 +0100 | [diff] [blame] | 4253 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4254 | PSA_DONE(); |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4255 | } |
| 4256 | /* END_CASE */ |
| 4257 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4258 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4259 | void ssl_tls13_derive_resumption_secrets(int hash_alg, |
| 4260 | data_t *secret, |
| 4261 | data_t *transcript, |
| 4262 | data_t *resumption_expected) |
Hanno Becker | 55bc2c5 | 2021-05-24 06:53:52 +0100 | [diff] [blame] | 4263 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4264 | mbedtls_ssl_tls13_application_secrets secrets; |
Hanno Becker | 55bc2c5 | 2021-05-24 06:53:52 +0100 | [diff] [blame] | 4265 | |
| 4266 | /* Double-check that we've passed sane parameters. */ |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 4267 | psa_algorithm_t alg = (psa_algorithm_t) hash_alg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4268 | size_t const hash_len = PSA_HASH_LENGTH(alg); |
| 4269 | TEST_ASSERT(PSA_ALG_IS_HASH(alg) && |
| 4270 | secret->len == hash_len && |
| 4271 | transcript->len == hash_len && |
| 4272 | resumption_expected->len == hash_len); |
Hanno Becker | 55bc2c5 | 2021-05-24 06:53:52 +0100 | [diff] [blame] | 4273 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4274 | PSA_INIT(); |
Hanno Becker | 55bc2c5 | 2021-05-24 06:53:52 +0100 | [diff] [blame] | 4275 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4276 | TEST_ASSERT(mbedtls_ssl_tls13_derive_resumption_master_secret( |
| 4277 | alg, secret->x, transcript->x, transcript->len, |
| 4278 | &secrets) == 0); |
Hanno Becker | 55bc2c5 | 2021-05-24 06:53:52 +0100 | [diff] [blame] | 4279 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4280 | ASSERT_COMPARE(secrets.resumption_master_secret, hash_len, |
| 4281 | resumption_expected->x, resumption_expected->len); |
Gabor Mezei | 892c4aa | 2022-03-21 12:21:43 +0100 | [diff] [blame] | 4282 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4283 | PSA_DONE(); |
| 4284 | } |
| 4285 | /* END_CASE */ |
| 4286 | |
| 4287 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 4288 | void ssl_tls13_create_psk_binder(int hash_alg, |
| 4289 | data_t *psk, |
| 4290 | int psk_type, |
| 4291 | data_t *transcript, |
| 4292 | data_t *binder_expected) |
| 4293 | { |
| 4294 | unsigned char binder[MBEDTLS_HASH_MAX_SIZE]; |
| 4295 | |
| 4296 | /* Double-check that we've passed sane parameters. */ |
| 4297 | psa_algorithm_t alg = (psa_algorithm_t) hash_alg; |
| 4298 | size_t const hash_len = PSA_HASH_LENGTH(alg); |
| 4299 | TEST_ASSERT(PSA_ALG_IS_HASH(alg) && |
| 4300 | transcript->len == hash_len && |
| 4301 | binder_expected->len == hash_len); |
| 4302 | |
| 4303 | PSA_INIT(); |
| 4304 | |
| 4305 | TEST_ASSERT(mbedtls_ssl_tls13_create_psk_binder( |
| 4306 | NULL, /* SSL context for debugging only */ |
| 4307 | alg, |
| 4308 | psk->x, psk->len, |
| 4309 | psk_type, |
| 4310 | transcript->x, |
| 4311 | binder) == 0); |
| 4312 | |
| 4313 | ASSERT_COMPARE(binder, hash_len, |
| 4314 | binder_expected->x, binder_expected->len); |
| 4315 | |
| 4316 | PSA_DONE(); |
Hanno Becker | 55bc2c5 | 2021-05-24 06:53:52 +0100 | [diff] [blame] | 4317 | } |
| 4318 | /* END_CASE */ |
| 4319 | |
Andrzej Kurek | daf43fb | 2022-10-12 10:46:42 -0400 | [diff] [blame] | 4320 | /* 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] | 4321 | void ssl_tls13_record_protection(int ciphersuite, |
| 4322 | int endpoint, |
| 4323 | int ctr, |
| 4324 | int padding_used, |
| 4325 | data_t *server_write_key, |
| 4326 | data_t *server_write_iv, |
| 4327 | data_t *client_write_key, |
| 4328 | data_t *client_write_iv, |
| 4329 | data_t *plaintext, |
| 4330 | data_t *ciphertext) |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4331 | { |
| 4332 | mbedtls_ssl_key_set keys; |
| 4333 | mbedtls_ssl_transform transform_send; |
| 4334 | mbedtls_ssl_transform transform_recv; |
| 4335 | mbedtls_record rec; |
| 4336 | unsigned char *buf = NULL; |
Hanno Becker | 1f91878 | 2021-08-01 19:18:28 +0100 | [diff] [blame] | 4337 | size_t buf_len; |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4338 | int other_endpoint; |
| 4339 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4340 | USE_PSA_INIT(); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 4341 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4342 | TEST_ASSERT(endpoint == MBEDTLS_SSL_IS_CLIENT || |
| 4343 | endpoint == MBEDTLS_SSL_IS_SERVER); |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4344 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4345 | if (endpoint == MBEDTLS_SSL_IS_SERVER) { |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4346 | other_endpoint = MBEDTLS_SSL_IS_CLIENT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4347 | } |
| 4348 | if (endpoint == MBEDTLS_SSL_IS_CLIENT) { |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4349 | other_endpoint = MBEDTLS_SSL_IS_SERVER; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4350 | } |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4351 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4352 | TEST_ASSERT(server_write_key->len == client_write_key->len); |
| 4353 | TEST_ASSERT(server_write_iv->len == client_write_iv->len); |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4354 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4355 | memcpy(keys.client_write_key, |
| 4356 | client_write_key->x, client_write_key->len); |
| 4357 | memcpy(keys.client_write_iv, |
| 4358 | client_write_iv->x, client_write_iv->len); |
| 4359 | memcpy(keys.server_write_key, |
| 4360 | server_write_key->x, server_write_key->len); |
| 4361 | memcpy(keys.server_write_iv, |
| 4362 | server_write_iv->x, server_write_iv->len); |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4363 | |
| 4364 | keys.key_len = server_write_key->len; |
| 4365 | keys.iv_len = server_write_iv->len; |
| 4366 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4367 | mbedtls_ssl_transform_init(&transform_recv); |
| 4368 | mbedtls_ssl_transform_init(&transform_send); |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4369 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4370 | TEST_ASSERT(mbedtls_ssl_tls13_populate_transform( |
| 4371 | &transform_send, endpoint, |
| 4372 | ciphersuite, &keys, NULL) == 0); |
| 4373 | TEST_ASSERT(mbedtls_ssl_tls13_populate_transform( |
| 4374 | &transform_recv, other_endpoint, |
| 4375 | ciphersuite, &keys, NULL) == 0); |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4376 | |
Hanno Becker | 1f91878 | 2021-08-01 19:18:28 +0100 | [diff] [blame] | 4377 | /* Make sure we have enough space in the buffer even if |
| 4378 | * we use more padding than the KAT. */ |
| 4379 | buf_len = ciphertext->len + MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4380 | ASSERT_ALLOC(buf, buf_len); |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4381 | rec.type = MBEDTLS_SSL_MSG_APPLICATION_DATA; |
Hanno Becker | 4153745 | 2021-04-20 05:35:28 +0100 | [diff] [blame] | 4382 | |
| 4383 | /* 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] | 4384 | mbedtls_ssl_write_version(rec.ver, |
| 4385 | MBEDTLS_SSL_TRANSPORT_STREAM, |
| 4386 | MBEDTLS_SSL_VERSION_TLS1_2); |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4387 | |
| 4388 | /* Copy plaintext into record structure */ |
| 4389 | rec.buf = buf; |
Hanno Becker | 1f91878 | 2021-08-01 19:18:28 +0100 | [diff] [blame] | 4390 | rec.buf_len = buf_len; |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4391 | rec.data_offset = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4392 | TEST_ASSERT(plaintext->len <= ciphertext->len); |
| 4393 | memcpy(rec.buf + rec.data_offset, plaintext->x, plaintext->len); |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4394 | rec.data_len = plaintext->len; |
| 4395 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 4396 | rec.cid_len = 0; |
| 4397 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 4398 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4399 | memset(&rec.ctr[0], 0, 8); |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4400 | rec.ctr[7] = ctr; |
| 4401 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4402 | TEST_ASSERT(mbedtls_ssl_encrypt_buf(NULL, &transform_send, &rec, |
| 4403 | NULL, NULL) == 0); |
Hanno Becker | 1f91878 | 2021-08-01 19:18:28 +0100 | [diff] [blame] | 4404 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4405 | if (padding_used == MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY) { |
| 4406 | ASSERT_COMPARE(rec.buf + rec.data_offset, rec.data_len, |
| 4407 | ciphertext->x, ciphertext->len); |
Hanno Becker | 1f91878 | 2021-08-01 19:18:28 +0100 | [diff] [blame] | 4408 | } |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4409 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4410 | TEST_ASSERT(mbedtls_ssl_decrypt_buf(NULL, &transform_recv, &rec) == 0); |
| 4411 | ASSERT_COMPARE(rec.buf + rec.data_offset, rec.data_len, |
| 4412 | plaintext->x, plaintext->len); |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4413 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4414 | mbedtls_free(buf); |
| 4415 | mbedtls_ssl_transform_free(&transform_send); |
| 4416 | mbedtls_ssl_transform_free(&transform_recv); |
| 4417 | USE_PSA_DONE(); |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4418 | } |
| 4419 | /* END_CASE */ |
| 4420 | |
Andrzej Kurek | 658442f | 2022-10-12 11:28:41 -0400 | [diff] [blame] | 4421 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4422 | void ssl_tls13_key_evolution(int hash_alg, |
| 4423 | data_t *secret, |
| 4424 | data_t *input, |
| 4425 | data_t *expected) |
Hanno Becker | 2d2c3eb | 2020-08-20 14:54:24 +0100 | [diff] [blame] | 4426 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4427 | unsigned char secret_new[MBEDTLS_HASH_MAX_SIZE]; |
Hanno Becker | 2d2c3eb | 2020-08-20 14:54:24 +0100 | [diff] [blame] | 4428 | |
Gabor Mezei | 5d9a1fe | 2022-03-24 17:49:14 +0100 | [diff] [blame] | 4429 | PSA_INIT(); |
Gabor Mezei | 892c4aa | 2022-03-21 12:21:43 +0100 | [diff] [blame] | 4430 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4431 | TEST_ASSERT(mbedtls_ssl_tls13_evolve_secret( |
| 4432 | (psa_algorithm_t) hash_alg, |
| 4433 | secret->len ? secret->x : NULL, |
| 4434 | input->len ? input->x : NULL, input->len, |
| 4435 | secret_new) == 0); |
Hanno Becker | 2d2c3eb | 2020-08-20 14:54:24 +0100 | [diff] [blame] | 4436 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4437 | ASSERT_COMPARE(secret_new, (size_t) expected->len, |
| 4438 | expected->x, (size_t) expected->len); |
Gabor Mezei | 892c4aa | 2022-03-21 12:21:43 +0100 | [diff] [blame] | 4439 | |
Gabor Mezei | 5d9a1fe | 2022-03-24 17:49:14 +0100 | [diff] [blame] | 4440 | PSA_DONE(); |
Hanno Becker | 2d2c3eb | 2020-08-20 14:54:24 +0100 | [diff] [blame] | 4441 | } |
| 4442 | /* END_CASE */ |
| 4443 | |
Jerry Yu | 53d23e2 | 2022-02-09 16:25:09 +0800 | [diff] [blame] | 4444 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_2 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4445 | void ssl_tls_prf(int type, data_t *secret, data_t *random, |
| 4446 | char *label, data_t *result_str, int exp_ret) |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 4447 | { |
| 4448 | unsigned char *output; |
| 4449 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4450 | output = mbedtls_calloc(1, result_str->len); |
| 4451 | if (output == NULL) { |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 4452 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4453 | } |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 4454 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4455 | USE_PSA_INIT(); |
Ron Eldor | 6b9b1b8 | 2019-05-15 17:04:33 +0300 | [diff] [blame] | 4456 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4457 | TEST_ASSERT(mbedtls_ssl_tls_prf(type, secret->x, secret->len, |
| 4458 | label, random->x, random->len, |
| 4459 | output, result_str->len) == exp_ret); |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 4460 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4461 | if (exp_ret == 0) { |
| 4462 | TEST_ASSERT(mbedtls_test_hexcmp(output, result_str->x, |
| 4463 | result_str->len, result_str->len) == 0); |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 4464 | } |
| 4465 | exit: |
| 4466 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4467 | mbedtls_free(output); |
| 4468 | USE_PSA_DONE(); |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 4469 | } |
| 4470 | /* END_CASE */ |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 4471 | |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 4472 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4473 | void ssl_serialize_session_save_load(int ticket_len, char *crt_file, |
| 4474 | int endpoint_type, int tls_version) |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 4475 | { |
| 4476 | mbedtls_ssl_session original, restored; |
| 4477 | unsigned char *buf = NULL; |
| 4478 | size_t len; |
| 4479 | |
| 4480 | /* |
| 4481 | * Test that a save-load pair is the identity |
| 4482 | */ |
| 4483 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4484 | mbedtls_ssl_session_init(&original); |
| 4485 | mbedtls_ssl_session_init(&restored); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 4486 | |
| 4487 | /* Prepare a dummy session to work on */ |
Jerry Yu | f092629 | 2022-07-15 13:05:57 +0800 | [diff] [blame] | 4488 | ((void) endpoint_type); |
| 4489 | ((void) tls_version); |
| 4490 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4491 | if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 4492 | TEST_ASSERT(mbedtls_test_ssl_tls13_populate_session( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4493 | &original, 0, endpoint_type) == 0); |
| 4494 | } else |
Jerry Yu | f092629 | 2022-07-15 13:05:57 +0800 | [diff] [blame] | 4495 | #endif |
| 4496 | { |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 4497 | TEST_ASSERT(mbedtls_test_ssl_tls12_populate_session( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4498 | &original, ticket_len, crt_file) == 0); |
Jerry Yu | f092629 | 2022-07-15 13:05:57 +0800 | [diff] [blame] | 4499 | } |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 4500 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 4501 | /* Serialize it */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4502 | TEST_ASSERT(mbedtls_ssl_session_save(&original, NULL, 0, &len) |
| 4503 | == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL); |
| 4504 | TEST_ASSERT((buf = mbedtls_calloc(1, len)) != NULL); |
| 4505 | TEST_ASSERT(mbedtls_ssl_session_save(&original, buf, len, &len) |
| 4506 | == 0); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 4507 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 4508 | /* Restore session from serialized data */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4509 | TEST_ASSERT(mbedtls_ssl_session_load(&restored, buf, len) == 0); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 4510 | |
| 4511 | /* |
| 4512 | * Make sure both session structures are identical |
| 4513 | */ |
| 4514 | #if defined(MBEDTLS_HAVE_TIME) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4515 | TEST_ASSERT(original.start == restored.start); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 4516 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4517 | TEST_ASSERT(original.tls_version == restored.tls_version); |
| 4518 | TEST_ASSERT(original.ciphersuite == restored.ciphersuite); |
Jerry Yu | f092629 | 2022-07-15 13:05:57 +0800 | [diff] [blame] | 4519 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4520 | if (tls_version == MBEDTLS_SSL_VERSION_TLS1_2) { |
| 4521 | TEST_ASSERT(original.id_len == restored.id_len); |
| 4522 | TEST_ASSERT(memcmp(original.id, |
| 4523 | restored.id, sizeof(original.id)) == 0); |
| 4524 | TEST_ASSERT(memcmp(original.master, |
| 4525 | restored.master, sizeof(original.master)) == 0); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 4526 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4527 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 4528 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4529 | TEST_ASSERT((original.peer_cert == NULL) == |
| 4530 | (restored.peer_cert == NULL)); |
| 4531 | if (original.peer_cert != NULL) { |
| 4532 | TEST_ASSERT(original.peer_cert->raw.len == |
| 4533 | restored.peer_cert->raw.len); |
| 4534 | TEST_ASSERT(memcmp(original.peer_cert->raw.p, |
| 4535 | restored.peer_cert->raw.p, |
| 4536 | original.peer_cert->raw.len) == 0); |
Jerry Yu | f092629 | 2022-07-15 13:05:57 +0800 | [diff] [blame] | 4537 | } |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 4538 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4539 | TEST_ASSERT(original.peer_cert_digest_type == |
| 4540 | restored.peer_cert_digest_type); |
| 4541 | TEST_ASSERT(original.peer_cert_digest_len == |
| 4542 | restored.peer_cert_digest_len); |
| 4543 | TEST_ASSERT((original.peer_cert_digest == NULL) == |
| 4544 | (restored.peer_cert_digest == NULL)); |
| 4545 | if (original.peer_cert_digest != NULL) { |
| 4546 | TEST_ASSERT(memcmp(original.peer_cert_digest, |
| 4547 | restored.peer_cert_digest, |
| 4548 | original.peer_cert_digest_len) == 0); |
Jerry Yu | f092629 | 2022-07-15 13:05:57 +0800 | [diff] [blame] | 4549 | } |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 4550 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4551 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4552 | TEST_ASSERT(original.verify_result == restored.verify_result); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 4553 | |
Jerry Yu | f092629 | 2022-07-15 13:05:57 +0800 | [diff] [blame] | 4554 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4555 | TEST_ASSERT(original.mfl_code == restored.mfl_code); |
Jerry Yu | f092629 | 2022-07-15 13:05:57 +0800 | [diff] [blame] | 4556 | #endif |
| 4557 | |
| 4558 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4559 | TEST_ASSERT(original.encrypt_then_mac == restored.encrypt_then_mac); |
Jerry Yu | f092629 | 2022-07-15 13:05:57 +0800 | [diff] [blame] | 4560 | #endif |
Jerry Yu | 6ac7c03 | 2022-07-21 23:11:55 +0800 | [diff] [blame] | 4561 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4562 | TEST_ASSERT(original.ticket_len == restored.ticket_len); |
| 4563 | if (original.ticket_len != 0) { |
| 4564 | TEST_ASSERT(original.ticket != NULL); |
| 4565 | TEST_ASSERT(restored.ticket != NULL); |
| 4566 | TEST_ASSERT(memcmp(original.ticket, |
| 4567 | restored.ticket, original.ticket_len) == 0); |
Jerry Yu | 6ac7c03 | 2022-07-21 23:11:55 +0800 | [diff] [blame] | 4568 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4569 | TEST_ASSERT(original.ticket_lifetime == restored.ticket_lifetime); |
Jerry Yu | 6ac7c03 | 2022-07-21 23:11:55 +0800 | [diff] [blame] | 4570 | #endif |
Jerry Yu | f092629 | 2022-07-15 13:05:57 +0800 | [diff] [blame] | 4571 | } |
Jerry Yu | 6ac7c03 | 2022-07-21 23:11:55 +0800 | [diff] [blame] | 4572 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 4573 | |
| 4574 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4575 | if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
| 4576 | TEST_ASSERT(original.endpoint == restored.endpoint); |
| 4577 | TEST_ASSERT(original.ciphersuite == restored.ciphersuite); |
| 4578 | TEST_ASSERT(original.ticket_age_add == restored.ticket_age_add); |
| 4579 | TEST_ASSERT(original.ticket_flags == restored.ticket_flags); |
| 4580 | TEST_ASSERT(original.resumption_key_len == restored.resumption_key_len); |
| 4581 | if (original.resumption_key_len != 0) { |
| 4582 | TEST_ASSERT(original.resumption_key != NULL); |
| 4583 | TEST_ASSERT(restored.resumption_key != NULL); |
| 4584 | TEST_ASSERT(memcmp(original.resumption_key, |
| 4585 | restored.resumption_key, |
| 4586 | original.resumption_key_len) == 0); |
Jerry Yu | 6ac7c03 | 2022-07-21 23:11:55 +0800 | [diff] [blame] | 4587 | } |
| 4588 | #if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4589 | if (endpoint_type == MBEDTLS_SSL_IS_SERVER) { |
| 4590 | TEST_ASSERT(original.start == restored.start); |
Jerry Yu | 6ac7c03 | 2022-07-21 23:11:55 +0800 | [diff] [blame] | 4591 | } |
Jerry Yu | f092629 | 2022-07-15 13:05:57 +0800 | [diff] [blame] | 4592 | #endif |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 4593 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4594 | if (endpoint_type == MBEDTLS_SSL_IS_CLIENT) { |
Jerry Yu | 6ac7c03 | 2022-07-21 23:11:55 +0800 | [diff] [blame] | 4595 | #if defined(MBEDTLS_HAVE_TIME) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4596 | TEST_ASSERT(original.ticket_received == restored.ticket_received); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 4597 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4598 | TEST_ASSERT(original.ticket_lifetime == restored.ticket_lifetime); |
| 4599 | TEST_ASSERT(original.ticket_len == restored.ticket_len); |
| 4600 | if (original.ticket_len != 0) { |
| 4601 | TEST_ASSERT(original.ticket != NULL); |
| 4602 | TEST_ASSERT(restored.ticket != NULL); |
| 4603 | TEST_ASSERT(memcmp(original.ticket, |
| 4604 | restored.ticket, |
| 4605 | original.ticket_len) == 0); |
Jerry Yu | 6ac7c03 | 2022-07-21 23:11:55 +0800 | [diff] [blame] | 4606 | } |
| 4607 | |
| 4608 | } |
| 4609 | #endif |
| 4610 | } |
| 4611 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 4612 | |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 4613 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4614 | mbedtls_ssl_session_free(&original); |
| 4615 | mbedtls_ssl_session_free(&restored); |
| 4616 | mbedtls_free(buf); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 4617 | } |
| 4618 | /* END_CASE */ |
| 4619 | |
Manuel Pégourié-Gonnard | aa75583 | 2019-06-03 10:53:47 +0200 | [diff] [blame] | 4620 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4621 | void ssl_serialize_session_load_save(int ticket_len, char *crt_file, |
| 4622 | int endpoint_type, int tls_version) |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 4623 | { |
| 4624 | mbedtls_ssl_session session; |
| 4625 | unsigned char *buf1 = NULL, *buf2 = NULL; |
| 4626 | size_t len0, len1, len2; |
| 4627 | |
| 4628 | /* |
| 4629 | * Test that a load-save pair is the identity |
| 4630 | */ |
| 4631 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4632 | mbedtls_ssl_session_init(&session); |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 4633 | |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 4634 | /* Prepare a dummy session to work on */ |
Jerry Yu | a180f99 | 2022-07-15 12:52:54 +0800 | [diff] [blame] | 4635 | ((void) endpoint_type); |
| 4636 | ((void) tls_version); |
| 4637 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4638 | if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 4639 | TEST_ASSERT(mbedtls_test_ssl_tls13_populate_session( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4640 | &session, 0, endpoint_type) == 0); |
| 4641 | } else |
Jerry Yu | a180f99 | 2022-07-15 12:52:54 +0800 | [diff] [blame] | 4642 | #endif |
| 4643 | { |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 4644 | TEST_ASSERT(mbedtls_test_ssl_tls12_populate_session( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4645 | &session, ticket_len, crt_file) == 0); |
Jerry Yu | a180f99 | 2022-07-15 12:52:54 +0800 | [diff] [blame] | 4646 | } |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 4647 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 4648 | /* Get desired buffer size for serializing */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4649 | TEST_ASSERT(mbedtls_ssl_session_save(&session, NULL, 0, &len0) |
| 4650 | == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL); |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 4651 | |
| 4652 | /* Allocate first buffer */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4653 | buf1 = mbedtls_calloc(1, len0); |
| 4654 | TEST_ASSERT(buf1 != NULL); |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 4655 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 4656 | /* Serialize to buffer and free live session */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4657 | TEST_ASSERT(mbedtls_ssl_session_save(&session, buf1, len0, &len1) |
| 4658 | == 0); |
| 4659 | TEST_ASSERT(len0 == len1); |
| 4660 | mbedtls_ssl_session_free(&session); |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 4661 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 4662 | /* Restore session from serialized data */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4663 | TEST_ASSERT(mbedtls_ssl_session_load(&session, buf1, len1) == 0); |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 4664 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 4665 | /* Allocate second buffer and serialize to it */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4666 | buf2 = mbedtls_calloc(1, len0); |
| 4667 | TEST_ASSERT(buf2 != NULL); |
| 4668 | TEST_ASSERT(mbedtls_ssl_session_save(&session, buf2, len0, &len2) |
| 4669 | == 0); |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 4670 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 4671 | /* Make sure both serialized versions are identical */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4672 | TEST_ASSERT(len1 == len2); |
| 4673 | TEST_ASSERT(memcmp(buf1, buf2, len1) == 0); |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 4674 | |
| 4675 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4676 | mbedtls_ssl_session_free(&session); |
| 4677 | mbedtls_free(buf1); |
| 4678 | mbedtls_free(buf2); |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 4679 | } |
| 4680 | /* END_CASE */ |
Manuel Pégourié-Gonnard | f5fa0aa | 2019-05-23 10:38:11 +0200 | [diff] [blame] | 4681 | |
| 4682 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4683 | void ssl_serialize_session_save_buf_size(int ticket_len, char *crt_file, |
| 4684 | int endpoint_type, int tls_version) |
Manuel Pégourié-Gonnard | f5fa0aa | 2019-05-23 10:38:11 +0200 | [diff] [blame] | 4685 | { |
| 4686 | mbedtls_ssl_session session; |
| 4687 | unsigned char *buf = NULL; |
| 4688 | size_t good_len, bad_len, test_len; |
| 4689 | |
| 4690 | /* |
| 4691 | * Test that session_save() fails cleanly on small buffers |
| 4692 | */ |
| 4693 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4694 | mbedtls_ssl_session_init(&session); |
Manuel Pégourié-Gonnard | f5fa0aa | 2019-05-23 10:38:11 +0200 | [diff] [blame] | 4695 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 4696 | /* Prepare dummy session and get serialized size */ |
Jerry Yu | 1ac476c | 2022-07-15 11:22:40 +0800 | [diff] [blame] | 4697 | ((void) endpoint_type); |
| 4698 | ((void) tls_version); |
| 4699 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4700 | if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 4701 | TEST_ASSERT(mbedtls_test_ssl_tls13_populate_session( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4702 | &session, 0, endpoint_type) == 0); |
| 4703 | } else |
Jerry Yu | 1ac476c | 2022-07-15 11:22:40 +0800 | [diff] [blame] | 4704 | #endif |
| 4705 | { |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 4706 | TEST_ASSERT(mbedtls_test_ssl_tls12_populate_session( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4707 | &session, ticket_len, crt_file) == 0); |
Jerry Yu | 1ac476c | 2022-07-15 11:22:40 +0800 | [diff] [blame] | 4708 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4709 | TEST_ASSERT(mbedtls_ssl_session_save(&session, NULL, 0, &good_len) |
| 4710 | == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL); |
Manuel Pégourié-Gonnard | f5fa0aa | 2019-05-23 10:38:11 +0200 | [diff] [blame] | 4711 | |
| 4712 | /* Try all possible bad lengths */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4713 | for (bad_len = 1; bad_len < good_len; bad_len++) { |
Manuel Pégourié-Gonnard | f5fa0aa | 2019-05-23 10:38:11 +0200 | [diff] [blame] | 4714 | /* Allocate exact size so that asan/valgrind can detect any overwrite */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4715 | mbedtls_free(buf); |
| 4716 | TEST_ASSERT((buf = mbedtls_calloc(1, bad_len)) != NULL); |
| 4717 | TEST_ASSERT(mbedtls_ssl_session_save(&session, buf, bad_len, |
| 4718 | &test_len) |
| 4719 | == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL); |
| 4720 | TEST_ASSERT(test_len == good_len); |
Manuel Pégourié-Gonnard | f5fa0aa | 2019-05-23 10:38:11 +0200 | [diff] [blame] | 4721 | } |
| 4722 | |
| 4723 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4724 | mbedtls_ssl_session_free(&session); |
| 4725 | mbedtls_free(buf); |
Manuel Pégourié-Gonnard | f5fa0aa | 2019-05-23 10:38:11 +0200 | [diff] [blame] | 4726 | } |
| 4727 | /* END_CASE */ |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 4728 | |
| 4729 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4730 | void ssl_serialize_session_load_buf_size(int ticket_len, char *crt_file, |
| 4731 | int endpoint_type, int tls_version) |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 4732 | { |
| 4733 | mbedtls_ssl_session session; |
| 4734 | unsigned char *good_buf = NULL, *bad_buf = NULL; |
| 4735 | size_t good_len, bad_len; |
| 4736 | |
| 4737 | /* |
| 4738 | * Test that session_load() fails cleanly on small buffers |
| 4739 | */ |
| 4740 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4741 | mbedtls_ssl_session_init(&session); |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 4742 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 4743 | /* Prepare serialized session data */ |
Jerry Yu | 6e8fec2 | 2022-07-15 10:37:02 +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 | 6e8fec2 | 2022-07-15 10:37:02 +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 | 6e8fec2 | 2022-07-15 10:37:02 +0800 | [diff] [blame] | 4755 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4756 | TEST_ASSERT(mbedtls_ssl_session_save(&session, NULL, 0, &good_len) |
| 4757 | == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL); |
| 4758 | TEST_ASSERT((good_buf = mbedtls_calloc(1, good_len)) != NULL); |
| 4759 | TEST_ASSERT(mbedtls_ssl_session_save(&session, good_buf, good_len, |
| 4760 | &good_len) == 0); |
| 4761 | mbedtls_ssl_session_free(&session); |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 4762 | |
| 4763 | /* Try all possible bad lengths */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4764 | for (bad_len = 0; bad_len < good_len; bad_len++) { |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 4765 | /* Allocate exact size so that asan/valgrind can detect any overread */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4766 | mbedtls_free(bad_buf); |
| 4767 | bad_buf = mbedtls_calloc(1, bad_len ? bad_len : 1); |
| 4768 | TEST_ASSERT(bad_buf != NULL); |
| 4769 | memcpy(bad_buf, good_buf, bad_len); |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 4770 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4771 | TEST_ASSERT(mbedtls_ssl_session_load(&session, bad_buf, bad_len) |
| 4772 | == MBEDTLS_ERR_SSL_BAD_INPUT_DATA); |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 4773 | } |
| 4774 | |
| 4775 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4776 | mbedtls_ssl_session_free(&session); |
| 4777 | mbedtls_free(good_buf); |
| 4778 | mbedtls_free(bad_buf); |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 4779 | } |
| 4780 | /* END_CASE */ |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 4781 | |
Hanno Becker | 363b646 | 2019-05-29 12:44:28 +0100 | [diff] [blame] | 4782 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4783 | void ssl_session_serialize_version_check(int corrupt_major, |
| 4784 | int corrupt_minor, |
| 4785 | int corrupt_patch, |
| 4786 | int corrupt_config, |
| 4787 | int endpoint_type, |
| 4788 | int tls_version) |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 4789 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4790 | unsigned char serialized_session[2048]; |
Hanno Becker | 363b646 | 2019-05-29 12:44:28 +0100 | [diff] [blame] | 4791 | size_t serialized_session_len; |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 4792 | unsigned cur_byte; |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 4793 | mbedtls_ssl_session session; |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 4794 | uint8_t should_corrupt_byte[] = { corrupt_major == 1, |
| 4795 | corrupt_minor == 1, |
| 4796 | corrupt_patch == 1, |
| 4797 | corrupt_config == 1, |
| 4798 | corrupt_config == 1 }; |
| 4799 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4800 | mbedtls_ssl_session_init(&session); |
Jerry Yu | 534ff40 | 2022-07-14 16:43:43 +0800 | [diff] [blame] | 4801 | ((void) endpoint_type); |
| 4802 | ((void) tls_version); |
| 4803 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4804 | if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 4805 | TEST_ASSERT(mbedtls_test_ssl_tls13_populate_session( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4806 | &session, 0, endpoint_type) == 0); |
| 4807 | } else |
Jerry Yu | 534ff40 | 2022-07-14 16:43:43 +0800 | [diff] [blame] | 4808 | #endif |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 4809 | TEST_ASSERT(mbedtls_test_ssl_tls12_populate_session(&session, 0, NULL) == 0); |
Jerry Yu | 534ff40 | 2022-07-14 16:43:43 +0800 | [diff] [blame] | 4810 | |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 4811 | |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 4812 | /* Infer length of serialized session. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4813 | TEST_ASSERT(mbedtls_ssl_session_save(&session, |
| 4814 | serialized_session, |
| 4815 | sizeof(serialized_session), |
| 4816 | &serialized_session_len) == 0); |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 4817 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4818 | mbedtls_ssl_session_free(&session); |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 4819 | |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 4820 | /* Without any modification, we should be able to successfully |
Hanno Becker | 363b646 | 2019-05-29 12:44:28 +0100 | [diff] [blame] | 4821 | * de-serialize the session - double-check that. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4822 | TEST_ASSERT(mbedtls_ssl_session_load(&session, |
| 4823 | serialized_session, |
| 4824 | serialized_session_len) == 0); |
| 4825 | mbedtls_ssl_session_free(&session); |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 4826 | |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 4827 | /* Go through the bytes in the serialized session header and |
| 4828 | * corrupt them bit-by-bit. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4829 | for (cur_byte = 0; cur_byte < sizeof(should_corrupt_byte); cur_byte++) { |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 4830 | int cur_bit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4831 | unsigned char * const byte = &serialized_session[cur_byte]; |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 4832 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4833 | if (should_corrupt_byte[cur_byte] == 0) { |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 4834 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4835 | } |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 4836 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4837 | for (cur_bit = 0; cur_bit < CHAR_BIT; cur_bit++) { |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 4838 | unsigned char const corrupted_bit = 0x1u << cur_bit; |
| 4839 | /* Modify a single bit in the serialized session. */ |
| 4840 | *byte ^= corrupted_bit; |
| 4841 | |
| 4842 | /* Attempt to deserialize */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4843 | TEST_ASSERT(mbedtls_ssl_session_load(&session, |
| 4844 | serialized_session, |
| 4845 | serialized_session_len) == |
| 4846 | MBEDTLS_ERR_SSL_VERSION_MISMATCH); |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 4847 | |
| 4848 | /* Undo the change */ |
| 4849 | *byte ^= corrupted_bit; |
| 4850 | } |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 4851 | } |
| 4852 | |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 4853 | } |
| 4854 | /* END_CASE */ |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4855 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4856 | /* 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] | 4857 | void mbedtls_endpoint_sanity(int endpoint_type) |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4858 | { |
| 4859 | enum { BUFFSIZE = 1024 }; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 4860 | mbedtls_test_ssl_endpoint ep; |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4861 | int ret = -1; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 4862 | mbedtls_test_handshake_test_options options; |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 4863 | mbedtls_test_init_handshake_options(&options); |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 4864 | options.pk_alg = MBEDTLS_PK_RSA; |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4865 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 4866 | ret = mbedtls_test_ssl_endpoint_init(NULL, endpoint_type, &options, |
| 4867 | NULL, NULL, NULL, NULL); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4868 | TEST_ASSERT(MBEDTLS_ERR_SSL_BAD_INPUT_DATA == ret); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4869 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 4870 | ret = mbedtls_test_ssl_endpoint_certificate_init(NULL, options.pk_alg, 0, 0, 0); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4871 | TEST_ASSERT(MBEDTLS_ERR_SSL_BAD_INPUT_DATA == ret); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4872 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 4873 | ret = mbedtls_test_ssl_endpoint_init(&ep, endpoint_type, &options, |
| 4874 | NULL, NULL, NULL, NULL); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4875 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4876 | |
| 4877 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 4878 | mbedtls_test_ssl_endpoint_free(&ep, NULL); |
| 4879 | mbedtls_test_free_handshake_options(&options); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4880 | } |
| 4881 | /* END_CASE */ |
| 4882 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4883 | /* 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] | 4884 | void move_handshake_to_state(int endpoint_type, int state, int need_pass) |
| 4885 | { |
| 4886 | enum { BUFFSIZE = 1024 }; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 4887 | mbedtls_test_ssl_endpoint base_ep, second_ep; |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4888 | int ret = -1; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 4889 | mbedtls_test_handshake_test_options options; |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 4890 | mbedtls_test_init_handshake_options(&options); |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 4891 | options.pk_alg = MBEDTLS_PK_RSA; |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4892 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4893 | USE_PSA_INIT(); |
| 4894 | mbedtls_platform_zeroize(&base_ep, sizeof(base_ep)); |
| 4895 | mbedtls_platform_zeroize(&second_ep, sizeof(second_ep)); |
Neil Armstrong | 06baf04 | 2022-04-14 16:21:15 +0200 | [diff] [blame] | 4896 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 4897 | ret = mbedtls_test_ssl_endpoint_init(&base_ep, endpoint_type, &options, |
| 4898 | NULL, NULL, NULL, NULL); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4899 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4900 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 4901 | ret = mbedtls_test_ssl_endpoint_init(&second_ep, |
| 4902 | (endpoint_type == MBEDTLS_SSL_IS_SERVER) ? |
| 4903 | MBEDTLS_SSL_IS_CLIENT : MBEDTLS_SSL_IS_SERVER, |
| 4904 | &options, NULL, NULL, NULL, NULL); |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 4905 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4906 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4907 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 4908 | ret = mbedtls_test_mock_socket_connect(&(base_ep.socket), |
| 4909 | &(second_ep.socket), |
| 4910 | BUFFSIZE); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4911 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4912 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 4913 | ret = mbedtls_test_move_handshake_to_state(&(base_ep.ssl), |
| 4914 | &(second_ep.ssl), |
| 4915 | state); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4916 | if (need_pass) { |
| 4917 | TEST_ASSERT(ret == 0 || |
| 4918 | ret == MBEDTLS_ERR_SSL_WANT_READ || |
| 4919 | ret == MBEDTLS_ERR_SSL_WANT_WRITE); |
| 4920 | TEST_ASSERT(base_ep.ssl.state == state); |
| 4921 | } else { |
| 4922 | TEST_ASSERT(ret != 0 && |
| 4923 | ret != MBEDTLS_ERR_SSL_WANT_READ && |
| 4924 | ret != MBEDTLS_ERR_SSL_WANT_WRITE); |
| 4925 | TEST_ASSERT(base_ep.ssl.state != state); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4926 | } |
| 4927 | |
| 4928 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 4929 | mbedtls_test_free_handshake_options(&options); |
| 4930 | mbedtls_test_ssl_endpoint_free(&base_ep, NULL); |
| 4931 | mbedtls_test_ssl_endpoint_free(&second_ep, NULL); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4932 | USE_PSA_DONE(); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4933 | } |
| 4934 | /* END_CASE */ |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 4935 | |
Andrzej Kurek | 9113df8 | 2023-01-19 18:26:31 -0500 | [diff] [blame] | 4936 | /* 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] | 4937 | void handshake_version(int dtls, int client_min_version, int client_max_version, |
| 4938 | int server_min_version, int server_max_version, |
| 4939 | int expected_negotiated_version) |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 4940 | { |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 4941 | mbedtls_test_handshake_test_options options; |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 4942 | mbedtls_test_init_handshake_options(&options); |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 4943 | |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 4944 | options.client_min_version = client_min_version; |
| 4945 | options.client_max_version = client_max_version; |
| 4946 | options.server_min_version = server_min_version; |
| 4947 | options.server_max_version = server_max_version; |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 4948 | options.expected_negotiated_version = expected_negotiated_version; |
| 4949 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 4950 | options.dtls = dtls; |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 4951 | mbedtls_test_ssl_perform_handshake(&options); |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 4952 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 4953 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 4954 | goto exit; |
Andrzej Kurek | e11acb2 | 2022-06-27 06:11:34 -0400 | [diff] [blame] | 4955 | |
| 4956 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 4957 | mbedtls_test_free_handshake_options(&options); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 4958 | } |
| 4959 | /* END_CASE */ |
Andrzej Kurek | 9e9efdc | 2020-02-26 05:25:23 -0500 | [diff] [blame] | 4960 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4961 | /* 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] | 4962 | 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] | 4963 | { |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 4964 | mbedtls_test_handshake_test_options options; |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 4965 | mbedtls_test_init_handshake_options(&options); |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 4966 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 4967 | options.cipher = cipher; |
| 4968 | options.dtls = dtls; |
| 4969 | options.psk_str = psk_str; |
| 4970 | options.pk_alg = pk_alg; |
Andrzej Kurek | cc5169c | 2020-02-04 09:04:56 -0500 | [diff] [blame] | 4971 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 4972 | mbedtls_test_ssl_perform_handshake(&options); |
Andrzej Kurek | 316da1f | 2020-02-26 09:03:47 -0500 | [diff] [blame] | 4973 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 4974 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 4975 | goto exit; |
Andrzej Kurek | e11acb2 | 2022-06-27 06:11:34 -0400 | [diff] [blame] | 4976 | |
Andrzej Kurek | ddb8cd6 | 2022-07-04 16:07:28 -0400 | [diff] [blame] | 4977 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 4978 | mbedtls_test_free_handshake_options(&options); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 4979 | } |
| 4980 | /* END_CASE */ |
Andrzej Kurek | 316da1f | 2020-02-26 09:03:47 -0500 | [diff] [blame] | 4981 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4982 | /* 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] | 4983 | void handshake_cipher(char *cipher, int pk_alg, int dtls) |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 4984 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4985 | test_handshake_psk_cipher(cipher, pk_alg, NULL, dtls); |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 4986 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 4987 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 4988 | goto exit; |
| 4989 | } |
| 4990 | /* END_CASE */ |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 4991 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4992 | /* 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] | 4993 | void handshake_ciphersuite_select(char *cipher, int pk_alg, data_t *psk_str, |
| 4994 | int psa_alg, int psa_alg2, int psa_usage, |
| 4995 | int expected_handshake_result, |
| 4996 | int expected_ciphersuite) |
Neil Armstrong | 8c52ed8 | 2022-05-27 13:14:55 +0200 | [diff] [blame] | 4997 | { |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 4998 | mbedtls_test_handshake_test_options options; |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 4999 | mbedtls_test_init_handshake_options(&options); |
Neil Armstrong | 8c52ed8 | 2022-05-27 13:14:55 +0200 | [diff] [blame] | 5000 | |
| 5001 | options.cipher = cipher; |
Neil Armstrong | db13497 | 2022-06-30 09:06:28 +0200 | [diff] [blame] | 5002 | options.psk_str = psk_str; |
Neil Armstrong | 8c52ed8 | 2022-05-27 13:14:55 +0200 | [diff] [blame] | 5003 | options.pk_alg = pk_alg; |
| 5004 | options.opaque_alg = psa_alg; |
| 5005 | options.opaque_alg2 = psa_alg2; |
| 5006 | options.opaque_usage = psa_usage; |
| 5007 | options.expected_handshake_result = expected_handshake_result; |
| 5008 | options.expected_ciphersuite = expected_ciphersuite; |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 5009 | mbedtls_test_ssl_perform_handshake(&options); |
Neil Armstrong | 8c52ed8 | 2022-05-27 13:14:55 +0200 | [diff] [blame] | 5010 | |
| 5011 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 5012 | goto exit; |
Andrzej Kurek | ddb8cd6 | 2022-07-04 16:07:28 -0400 | [diff] [blame] | 5013 | |
| 5014 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 5015 | mbedtls_test_free_handshake_options(&options); |
Neil Armstrong | 8c52ed8 | 2022-05-27 13:14:55 +0200 | [diff] [blame] | 5016 | } |
| 5017 | /* END_CASE */ |
| 5018 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5019 | /* 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] | 5020 | void app_data(int mfl, int cli_msg_len, int srv_msg_len, |
| 5021 | int expected_cli_fragments, |
| 5022 | int expected_srv_fragments, int dtls) |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5023 | { |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 5024 | mbedtls_test_handshake_test_options options; |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 5025 | mbedtls_test_init_handshake_options(&options); |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 5026 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5027 | options.mfl = mfl; |
| 5028 | options.cli_msg_len = cli_msg_len; |
| 5029 | options.srv_msg_len = srv_msg_len; |
| 5030 | options.expected_cli_fragments = expected_cli_fragments; |
| 5031 | options.expected_srv_fragments = expected_srv_fragments; |
| 5032 | options.dtls = dtls; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5033 | #if !defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Ronald Cron | 21a1b2d | 2022-06-15 17:11:35 +0200 | [diff] [blame] | 5034 | options.expected_negotiated_version = MBEDTLS_SSL_VERSION_TLS1_3; |
| 5035 | #endif |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 5036 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 5037 | mbedtls_test_ssl_perform_handshake(&options); |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 5038 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5039 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 5040 | goto exit; |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 5041 | |
| 5042 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 5043 | mbedtls_test_free_handshake_options(&options); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5044 | } |
| 5045 | /* END_CASE */ |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 5046 | |
Andrzej Kurek | 714ae65 | 2022-11-02 19:07:19 -0400 | [diff] [blame] | 5047 | /* 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] | 5048 | void app_data_tls(int mfl, int cli_msg_len, int srv_msg_len, |
| 5049 | int expected_cli_fragments, |
| 5050 | int expected_srv_fragments) |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5051 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5052 | test_app_data(mfl, cli_msg_len, srv_msg_len, expected_cli_fragments, |
| 5053 | expected_srv_fragments, 0); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5054 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 5055 | goto exit; |
| 5056 | } |
| 5057 | /* END_CASE */ |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 5058 | |
Andrzej Kurek | 9113df8 | 2023-01-19 18:26:31 -0500 | [diff] [blame] | 5059 | /* 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] | 5060 | void app_data_dtls(int mfl, int cli_msg_len, int srv_msg_len, |
| 5061 | int expected_cli_fragments, |
| 5062 | int expected_srv_fragments) |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5063 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5064 | test_app_data(mfl, cli_msg_len, srv_msg_len, expected_cli_fragments, |
| 5065 | expected_srv_fragments, 1); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5066 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 5067 | goto exit; |
| 5068 | } |
| 5069 | /* END_CASE */ |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 5070 | |
Andrzej Kurek | 9113df8 | 2023-01-19 18:26:31 -0500 | [diff] [blame] | 5071 | /* 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] | 5072 | void handshake_serialization() |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5073 | { |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 5074 | mbedtls_test_handshake_test_options options; |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 5075 | mbedtls_test_init_handshake_options(&options); |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 5076 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5077 | options.serialize = 1; |
| 5078 | options.dtls = 1; |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 5079 | mbedtls_test_ssl_perform_handshake(&options); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5080 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 5081 | goto exit; |
Andrzej Kurek | 6e518ab | 2022-06-11 05:08:38 -0400 | [diff] [blame] | 5082 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 5083 | mbedtls_test_free_handshake_options(&options); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5084 | } |
| 5085 | /* END_CASE */ |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 5086 | |
Andrzej Kurek | a86cef3 | 2023-01-19 18:27:57 -0500 | [diff] [blame] | 5087 | /* 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] | 5088 | void handshake_fragmentation(int mfl, |
| 5089 | int expected_srv_hs_fragmentation, |
| 5090 | int expected_cli_hs_fragmentation) |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 5091 | { |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 5092 | mbedtls_test_handshake_test_options options; |
| 5093 | mbedtls_test_ssl_log_pattern srv_pattern, cli_pattern; |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 5094 | |
| 5095 | srv_pattern.pattern = cli_pattern.pattern = "found fragmented DTLS handshake"; |
| 5096 | srv_pattern.counter = 0; |
| 5097 | cli_pattern.counter = 0; |
| 5098 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 5099 | mbedtls_test_init_handshake_options(&options); |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 5100 | options.dtls = 1; |
| 5101 | options.mfl = mfl; |
Darryl Green | aad82f9 | 2019-12-02 10:53:11 +0000 | [diff] [blame] | 5102 | /* Set cipher to one using CBC so that record splitting can be tested */ |
| 5103 | options.cipher = "TLS-DHE-RSA-WITH-AES-256-CBC-SHA256"; |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 5104 | options.srv_auth_mode = MBEDTLS_SSL_VERIFY_REQUIRED; |
| 5105 | options.srv_log_obj = &srv_pattern; |
| 5106 | options.cli_log_obj = &cli_pattern; |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 5107 | options.srv_log_fun = mbedtls_test_ssl_log_analyzer; |
| 5108 | options.cli_log_fun = mbedtls_test_ssl_log_analyzer; |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 5109 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 5110 | mbedtls_test_ssl_perform_handshake(&options); |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 5111 | |
| 5112 | /* Test if the server received a fragmented handshake */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5113 | if (expected_srv_hs_fragmentation) { |
| 5114 | TEST_ASSERT(srv_pattern.counter >= 1); |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 5115 | } |
| 5116 | /* Test if the client received a fragmented handshake */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5117 | if (expected_cli_hs_fragmentation) { |
| 5118 | TEST_ASSERT(cli_pattern.counter >= 1); |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 5119 | } |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 5120 | |
| 5121 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 5122 | mbedtls_test_free_handshake_options(&options); |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 5123 | } |
| 5124 | /* END_CASE */ |
| 5125 | |
Andrzej Kurek | 9113df8 | 2023-01-19 18:26:31 -0500 | [diff] [blame] | 5126 | /* 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] | 5127 | void renegotiation(int legacy_renegotiation) |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5128 | { |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 5129 | mbedtls_test_handshake_test_options options; |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 5130 | mbedtls_test_init_handshake_options(&options); |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 5131 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5132 | options.renegotiate = 1; |
| 5133 | options.legacy_renegotiation = legacy_renegotiation; |
| 5134 | options.dtls = 1; |
Andrzej Kurek | 316da1f | 2020-02-26 09:03:47 -0500 | [diff] [blame] | 5135 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 5136 | mbedtls_test_ssl_perform_handshake(&options); |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 5137 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5138 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 5139 | goto exit; |
Andrzej Kurek | ddb8cd6 | 2022-07-04 16:07:28 -0400 | [diff] [blame] | 5140 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 5141 | mbedtls_test_free_handshake_options(&options); |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 5142 | } |
| 5143 | /* END_CASE */ |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 5144 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5145 | /* 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] | 5146 | void resize_buffers(int mfl, int renegotiation, int legacy_renegotiation, |
| 5147 | int serialize, int dtls, char *cipher) |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 5148 | { |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 5149 | mbedtls_test_handshake_test_options options; |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 5150 | mbedtls_test_init_handshake_options(&options); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 5151 | |
| 5152 | options.mfl = mfl; |
Andrzej Kurek | 8ea6872 | 2020-04-03 06:40:47 -0400 | [diff] [blame] | 5153 | options.cipher = cipher; |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 5154 | options.renegotiate = renegotiation; |
| 5155 | options.legacy_renegotiation = legacy_renegotiation; |
| 5156 | options.serialize = serialize; |
| 5157 | options.dtls = dtls; |
| 5158 | options.resize_buffers = 1; |
| 5159 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 5160 | mbedtls_test_ssl_perform_handshake(&options); |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 5161 | |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 5162 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 5163 | goto exit; |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 5164 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 5165 | mbedtls_test_free_handshake_options(&options); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 5166 | } |
| 5167 | /* END_CASE */ |
| 5168 | |
Andrzej Kurek | 9113df8 | 2023-01-19 18:26:31 -0500 | [diff] [blame] | 5169 | /* 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] | 5170 | void resize_buffers_serialize_mfl(int mfl) |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 5171 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5172 | test_resize_buffers(mfl, 0, MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION, 1, 1, |
| 5173 | (char *) ""); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 5174 | |
| 5175 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 5176 | goto exit; |
| 5177 | } |
| 5178 | /* END_CASE */ |
| 5179 | |
Andrzej Kurek | 9113df8 | 2023-01-19 18:26:31 -0500 | [diff] [blame] | 5180 | /* 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] | 5181 | void resize_buffers_renegotiate_mfl(int mfl, int legacy_renegotiation, |
| 5182 | char *cipher) |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 5183 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5184 | test_resize_buffers(mfl, 1, legacy_renegotiation, 0, 1, cipher); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 5185 | |
| 5186 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 5187 | goto exit; |
| 5188 | } |
| 5189 | /* END_CASE */ |
Manuel Pégourié-Gonnard | 045f094 | 2020-07-02 11:34:02 +0200 | [diff] [blame] | 5190 | |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 5191 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */ |
Hanno Becker | 6667ffd | 2021-04-19 21:59:22 +0100 | [diff] [blame] | 5192 | void test_multiple_psks() |
| 5193 | { |
| 5194 | unsigned char psk0[10] = { 0 }; |
| 5195 | unsigned char psk0_identity[] = { 'f', 'o', 'o' }; |
| 5196 | |
| 5197 | unsigned char psk1[10] = { 0 }; |
| 5198 | unsigned char psk1_identity[] = { 'b', 'a', 'r' }; |
| 5199 | |
| 5200 | mbedtls_ssl_config conf; |
| 5201 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5202 | USE_PSA_INIT(); |
| 5203 | mbedtls_ssl_config_init(&conf); |
Hanno Becker | 6667ffd | 2021-04-19 21:59:22 +0100 | [diff] [blame] | 5204 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5205 | TEST_ASSERT(mbedtls_ssl_conf_psk(&conf, |
| 5206 | psk0, sizeof(psk0), |
| 5207 | psk0_identity, sizeof(psk0_identity)) == 0); |
| 5208 | TEST_ASSERT(mbedtls_ssl_conf_psk(&conf, |
| 5209 | psk1, sizeof(psk1), |
| 5210 | psk1_identity, sizeof(psk1_identity)) == |
| 5211 | MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE); |
Hanno Becker | 6667ffd | 2021-04-19 21:59:22 +0100 | [diff] [blame] | 5212 | |
| 5213 | exit: |
| 5214 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5215 | mbedtls_ssl_config_free(&conf); |
Neil Armstrong | 4c3b4e0 | 2022-05-03 09:24:26 +0200 | [diff] [blame] | 5216 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5217 | USE_PSA_DONE(); |
Hanno Becker | 6667ffd | 2021-04-19 21:59:22 +0100 | [diff] [blame] | 5218 | } |
| 5219 | /* END_CASE */ |
| 5220 | |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 5221 | /* 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] | 5222 | void test_multiple_psks_opaque(int mode) |
Hanno Becker | 6667ffd | 2021-04-19 21:59:22 +0100 | [diff] [blame] | 5223 | { |
| 5224 | /* |
| 5225 | * Mode 0: Raw PSK, then opaque PSK |
| 5226 | * Mode 1: Opaque PSK, then raw PSK |
| 5227 | * Mode 2: 2x opaque PSK |
| 5228 | */ |
| 5229 | |
| 5230 | unsigned char psk0_raw[10] = { 0 }; |
| 5231 | unsigned char psk0_raw_identity[] = { 'f', 'o', 'o' }; |
| 5232 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5233 | 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] | 5234 | |
Hanno Becker | 6667ffd | 2021-04-19 21:59:22 +0100 | [diff] [blame] | 5235 | unsigned char psk0_opaque_identity[] = { 'f', 'o', 'o' }; |
| 5236 | |
| 5237 | unsigned char psk1_raw[10] = { 0 }; |
| 5238 | unsigned char psk1_raw_identity[] = { 'b', 'a', 'r' }; |
| 5239 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5240 | 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] | 5241 | |
Hanno Becker | 6667ffd | 2021-04-19 21:59:22 +0100 | [diff] [blame] | 5242 | unsigned char psk1_opaque_identity[] = { 'b', 'a', 'r' }; |
| 5243 | |
| 5244 | mbedtls_ssl_config conf; |
| 5245 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5246 | USE_PSA_INIT(); |
| 5247 | mbedtls_ssl_config_init(&conf); |
Hanno Becker | 6667ffd | 2021-04-19 21:59:22 +0100 | [diff] [blame] | 5248 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5249 | switch (mode) { |
Hanno Becker | 6667ffd | 2021-04-19 21:59:22 +0100 | [diff] [blame] | 5250 | case 0: |
| 5251 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5252 | TEST_ASSERT(mbedtls_ssl_conf_psk(&conf, |
| 5253 | psk0_raw, sizeof(psk0_raw), |
| 5254 | psk0_raw_identity, sizeof(psk0_raw_identity)) |
| 5255 | == 0); |
| 5256 | TEST_ASSERT(mbedtls_ssl_conf_psk_opaque(&conf, |
| 5257 | psk1_opaque, |
| 5258 | psk1_opaque_identity, |
| 5259 | sizeof(psk1_opaque_identity)) |
| 5260 | == MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE); |
Hanno Becker | 6667ffd | 2021-04-19 21:59:22 +0100 | [diff] [blame] | 5261 | break; |
| 5262 | |
| 5263 | case 1: |
| 5264 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5265 | TEST_ASSERT(mbedtls_ssl_conf_psk_opaque(&conf, |
| 5266 | psk0_opaque, |
| 5267 | psk0_opaque_identity, |
| 5268 | sizeof(psk0_opaque_identity)) |
| 5269 | == 0); |
| 5270 | TEST_ASSERT(mbedtls_ssl_conf_psk(&conf, |
| 5271 | psk1_raw, sizeof(psk1_raw), |
| 5272 | psk1_raw_identity, sizeof(psk1_raw_identity)) |
| 5273 | == MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE); |
Hanno Becker | 6667ffd | 2021-04-19 21:59:22 +0100 | [diff] [blame] | 5274 | |
| 5275 | break; |
| 5276 | |
| 5277 | case 2: |
| 5278 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5279 | TEST_ASSERT(mbedtls_ssl_conf_psk_opaque(&conf, |
| 5280 | psk0_opaque, |
| 5281 | psk0_opaque_identity, |
| 5282 | sizeof(psk0_opaque_identity)) |
| 5283 | == 0); |
| 5284 | TEST_ASSERT(mbedtls_ssl_conf_psk_opaque(&conf, |
| 5285 | psk1_opaque, |
| 5286 | psk1_opaque_identity, |
| 5287 | sizeof(psk1_opaque_identity)) |
| 5288 | == MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE); |
Hanno Becker | 6667ffd | 2021-04-19 21:59:22 +0100 | [diff] [blame] | 5289 | |
| 5290 | break; |
| 5291 | |
| 5292 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5293 | TEST_ASSERT(0); |
Hanno Becker | 6667ffd | 2021-04-19 21:59:22 +0100 | [diff] [blame] | 5294 | break; |
| 5295 | } |
| 5296 | |
| 5297 | exit: |
| 5298 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5299 | mbedtls_ssl_config_free(&conf); |
| 5300 | USE_PSA_DONE(); |
Hanno Becker | 6667ffd | 2021-04-19 21:59:22 +0100 | [diff] [blame] | 5301 | |
| 5302 | } |
| 5303 | /* END_CASE */ |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 5304 | |
Ronald Cron | 37bdaab | 2022-03-30 16:45:51 +0200 | [diff] [blame] | 5305 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5306 | void conf_version(int endpoint, int transport, |
| 5307 | int min_tls_version, int max_tls_version, |
| 5308 | int expected_ssl_setup_result) |
Ronald Cron | 37bdaab | 2022-03-30 16:45:51 +0200 | [diff] [blame] | 5309 | { |
| 5310 | mbedtls_ssl_config conf; |
| 5311 | mbedtls_ssl_context ssl; |
| 5312 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5313 | mbedtls_ssl_config_init(&conf); |
| 5314 | mbedtls_ssl_init(&ssl); |
Ronald Cron | 37bdaab | 2022-03-30 16:45:51 +0200 | [diff] [blame] | 5315 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5316 | mbedtls_ssl_conf_endpoint(&conf, endpoint); |
| 5317 | mbedtls_ssl_conf_transport(&conf, transport); |
| 5318 | mbedtls_ssl_conf_min_tls_version(&conf, min_tls_version); |
| 5319 | mbedtls_ssl_conf_max_tls_version(&conf, max_tls_version); |
Ronald Cron | 37bdaab | 2022-03-30 16:45:51 +0200 | [diff] [blame] | 5320 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5321 | TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == expected_ssl_setup_result); |
Ronald Cron | 37bdaab | 2022-03-30 16:45:51 +0200 | [diff] [blame] | 5322 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5323 | mbedtls_ssl_free(&ssl); |
| 5324 | mbedtls_ssl_config_free(&conf); |
Ronald Cron | 37bdaab | 2022-03-30 16:45:51 +0200 | [diff] [blame] | 5325 | } |
| 5326 | /* END_CASE */ |
| 5327 | |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 5328 | /* 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 */ |
| 5329 | void conf_curve() |
| 5330 | { |
| 5331 | |
| 5332 | mbedtls_ecp_group_id curve_list[] = { MBEDTLS_ECP_DP_SECP192R1, |
| 5333 | MBEDTLS_ECP_DP_SECP224R1, |
| 5334 | MBEDTLS_ECP_DP_SECP256R1, |
| 5335 | MBEDTLS_ECP_DP_NONE }; |
Tom Cosgrove | a327b52 | 2022-08-03 08:33:06 +0100 | [diff] [blame] | 5336 | uint16_t iana_tls_group_list[] = { MBEDTLS_SSL_IANA_TLS_GROUP_SECP192R1, |
| 5337 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP224R1, |
| 5338 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, |
| 5339 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE }; |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 5340 | |
| 5341 | mbedtls_ssl_config conf; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5342 | mbedtls_ssl_config_init(&conf); |
Jerry Yu | baa4934 | 2022-02-15 10:26:40 +0800 | [diff] [blame] | 5343 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5344 | mbedtls_ssl_conf_max_tls_version(&conf, MBEDTLS_SSL_VERSION_TLS1_2); |
| 5345 | mbedtls_ssl_conf_min_tls_version(&conf, MBEDTLS_SSL_VERSION_TLS1_2); |
Jerry Yu | baa4934 | 2022-02-15 10:26:40 +0800 | [diff] [blame] | 5346 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5347 | mbedtls_ssl_conf_max_tls_version(&conf, MBEDTLS_SSL_VERSION_TLS1_3); |
| 5348 | mbedtls_ssl_conf_min_tls_version(&conf, MBEDTLS_SSL_VERSION_TLS1_3); |
Jerry Yu | baa4934 | 2022-02-15 10:26:40 +0800 | [diff] [blame] | 5349 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5350 | mbedtls_ssl_conf_curves(&conf, curve_list); |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 5351 | |
| 5352 | mbedtls_ssl_context ssl; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5353 | mbedtls_ssl_init(&ssl); |
| 5354 | TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0); |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 5355 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5356 | TEST_ASSERT(ssl.handshake != NULL && ssl.handshake->group_list != NULL); |
| 5357 | TEST_ASSERT(ssl.conf != NULL && ssl.conf->group_list == NULL); |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 5358 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5359 | TEST_EQUAL(ssl.handshake->group_list[ARRAY_LENGTH(iana_tls_group_list) - 1], |
| 5360 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE); |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 5361 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5362 | for (size_t i = 0; i < ARRAY_LENGTH(iana_tls_group_list); i++) { |
| 5363 | TEST_EQUAL(iana_tls_group_list[i], ssl.handshake->group_list[i]); |
| 5364 | } |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 5365 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5366 | mbedtls_ssl_free(&ssl); |
| 5367 | mbedtls_ssl_config_free(&conf); |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 5368 | } |
| 5369 | /* END_CASE */ |
| 5370 | |
| 5371 | /* BEGIN_CASE depends_on:MBEDTLS_DEPRECATED_REMOVED */ |
| 5372 | void conf_group() |
| 5373 | { |
| 5374 | uint16_t iana_tls_group_list[] = { MBEDTLS_SSL_IANA_TLS_GROUP_SECP192R1, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5375 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP224R1, |
| 5376 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, |
| 5377 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE }; |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 5378 | |
| 5379 | mbedtls_ssl_config conf; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5380 | mbedtls_ssl_config_init(&conf); |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 5381 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5382 | mbedtls_ssl_conf_max_tls_version(&conf, MBEDTLS_SSL_VERSION_TLS1_2); |
| 5383 | mbedtls_ssl_conf_min_tls_version(&conf, MBEDTLS_SSL_VERSION_TLS1_2); |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 5384 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5385 | mbedtls_ssl_conf_groups(&conf, iana_tls_group_list); |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 5386 | |
| 5387 | mbedtls_ssl_context ssl; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5388 | mbedtls_ssl_init(&ssl); |
| 5389 | TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0); |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 5390 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5391 | TEST_ASSERT(ssl.conf != NULL && ssl.conf->group_list != NULL); |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 5392 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5393 | TEST_EQUAL(ssl.conf->group_list[ARRAY_LENGTH(iana_tls_group_list) - 1], |
| 5394 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE); |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 5395 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5396 | for (size_t i = 0; i < ARRAY_LENGTH(iana_tls_group_list); i++) { |
| 5397 | TEST_EQUAL(iana_tls_group_list[i], ssl.conf->group_list[i]); |
| 5398 | } |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 5399 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5400 | mbedtls_ssl_free(&ssl); |
| 5401 | mbedtls_ssl_config_free(&conf); |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 5402 | } |
| 5403 | /* END_CASE */ |
Paul Elliott | b9af2db | 2022-03-09 15:34:37 +0000 | [diff] [blame] | 5404 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5405 | /* 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] | 5406 | void force_bad_session_id_len() |
Andrzej Kurek | 514683a | 2022-06-10 10:33:05 -0400 | [diff] [blame] | 5407 | { |
| 5408 | enum { BUFFSIZE = 1024 }; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 5409 | mbedtls_test_handshake_test_options options; |
| 5410 | mbedtls_test_ssl_endpoint client, server; |
| 5411 | mbedtls_test_ssl_log_pattern srv_pattern, cli_pattern; |
Andrzej Kurek | 514683a | 2022-06-10 10:33:05 -0400 | [diff] [blame] | 5412 | mbedtls_test_message_socket_context server_context, client_context; |
| 5413 | |
| 5414 | srv_pattern.pattern = cli_pattern.pattern = "cache did not store session"; |
| 5415 | srv_pattern.counter = 0; |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 5416 | mbedtls_test_init_handshake_options(&options); |
Andrzej Kurek | 514683a | 2022-06-10 10:33:05 -0400 | [diff] [blame] | 5417 | |
| 5418 | options.srv_log_obj = &srv_pattern; |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 5419 | options.srv_log_fun = mbedtls_test_ssl_log_analyzer; |
Andrzej Kurek | 514683a | 2022-06-10 10:33:05 -0400 | [diff] [blame] | 5420 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5421 | USE_PSA_INIT(); |
| 5422 | mbedtls_platform_zeroize(&client, sizeof(client)); |
| 5423 | mbedtls_platform_zeroize(&server, sizeof(server)); |
Andrzej Kurek | 514683a | 2022-06-10 10:33:05 -0400 | [diff] [blame] | 5424 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 5425 | mbedtls_test_message_socket_init(&server_context); |
| 5426 | mbedtls_test_message_socket_init(&client_context); |
Andrzej Kurek | 514683a | 2022-06-10 10:33:05 -0400 | [diff] [blame] | 5427 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 5428 | TEST_ASSERT(mbedtls_test_ssl_endpoint_init(&client, MBEDTLS_SSL_IS_CLIENT, |
| 5429 | &options, NULL, NULL, |
| 5430 | NULL, NULL) == 0); |
Andrzej Kurek | 514683a | 2022-06-10 10:33:05 -0400 | [diff] [blame] | 5431 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 5432 | TEST_ASSERT(mbedtls_test_ssl_endpoint_init(&server, MBEDTLS_SSL_IS_SERVER, |
| 5433 | &options, NULL, NULL, NULL, |
| 5434 | NULL) == 0); |
Andrzej Kurek | 514683a | 2022-06-10 10:33:05 -0400 | [diff] [blame] | 5435 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5436 | mbedtls_debug_set_threshold(1); |
| 5437 | mbedtls_ssl_conf_dbg(&server.conf, options.srv_log_fun, |
| 5438 | options.srv_log_obj); |
Andrzej Kurek | 514683a | 2022-06-10 10:33:05 -0400 | [diff] [blame] | 5439 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 5440 | TEST_ASSERT(mbedtls_test_mock_socket_connect(&(client.socket), |
| 5441 | &(server.socket), |
| 5442 | BUFFSIZE) == 0); |
Andrzej Kurek | 514683a | 2022-06-10 10:33:05 -0400 | [diff] [blame] | 5443 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 5444 | TEST_ASSERT(mbedtls_test_move_handshake_to_state(&(client.ssl), |
| 5445 | &(server.ssl), |
| 5446 | MBEDTLS_SSL_HANDSHAKE_WRAPUP) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5447 | == 0); |
Andrzej Kurek | 514683a | 2022-06-10 10:33:05 -0400 | [diff] [blame] | 5448 | /* Force a bad session_id_len that will be read by the server in |
| 5449 | * mbedtls_ssl_cache_set. */ |
| 5450 | server.ssl.session_negotiate->id_len = 33; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5451 | if (options.cli_msg_len != 0 || options.srv_msg_len != 0) { |
Andrzej Kurek | 514683a | 2022-06-10 10:33:05 -0400 | [diff] [blame] | 5452 | /* Start data exchanging test */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5453 | TEST_ASSERT(mbedtls_exchange_data(&(client.ssl), options.cli_msg_len, |
| 5454 | options.expected_cli_fragments, |
| 5455 | &(server.ssl), options.srv_msg_len, |
| 5456 | options.expected_srv_fragments) |
| 5457 | == 0); |
Andrzej Kurek | 514683a | 2022-06-10 10:33:05 -0400 | [diff] [blame] | 5458 | } |
Andrzej Kurek | 514683a | 2022-06-10 10:33:05 -0400 | [diff] [blame] | 5459 | |
| 5460 | /* Make sure that the cache did not store the session */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5461 | TEST_EQUAL(srv_pattern.counter, 1); |
Andrzej Kurek | 514683a | 2022-06-10 10:33:05 -0400 | [diff] [blame] | 5462 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 5463 | mbedtls_test_ssl_endpoint_free(&client, NULL); |
| 5464 | mbedtls_test_ssl_endpoint_free(&server, NULL); |
| 5465 | mbedtls_test_free_handshake_options(&options); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5466 | mbedtls_debug_set_threshold(0); |
| 5467 | USE_PSA_DONE(); |
Andrzej Kurek | 514683a | 2022-06-10 10:33:05 -0400 | [diff] [blame] | 5468 | } |
| 5469 | /* END_CASE */ |
| 5470 | |
Andrzej Kurek | ed4d217 | 2022-06-08 11:57:57 -0400 | [diff] [blame] | 5471 | /* 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] | 5472 | void cookie_parsing(data_t *cookie, int exp_ret) |
Andrzej Kurek | cfb0194 | 2022-06-06 13:08:23 -0400 | [diff] [blame] | 5473 | { |
| 5474 | mbedtls_ssl_context ssl; |
| 5475 | mbedtls_ssl_config conf; |
| 5476 | size_t len; |
| 5477 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5478 | mbedtls_ssl_init(&ssl); |
| 5479 | mbedtls_ssl_config_init(&conf); |
| 5480 | TEST_EQUAL(mbedtls_ssl_config_defaults(&conf, MBEDTLS_SSL_IS_SERVER, |
| 5481 | MBEDTLS_SSL_TRANSPORT_DATAGRAM, |
| 5482 | MBEDTLS_SSL_PRESET_DEFAULT), |
| 5483 | 0); |
Andrzej Kurek | cfb0194 | 2022-06-06 13:08:23 -0400 | [diff] [blame] | 5484 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5485 | TEST_EQUAL(mbedtls_ssl_setup(&ssl, &conf), 0); |
| 5486 | TEST_EQUAL(mbedtls_ssl_check_dtls_clihlo_cookie(&ssl, ssl.cli_id, |
| 5487 | ssl.cli_id_len, |
| 5488 | cookie->x, cookie->len, |
| 5489 | ssl.out_buf, |
| 5490 | MBEDTLS_SSL_OUT_CONTENT_LEN, |
| 5491 | &len), |
| 5492 | exp_ret); |
Andrzej Kurek | cfb0194 | 2022-06-06 13:08:23 -0400 | [diff] [blame] | 5493 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5494 | mbedtls_ssl_free(&ssl); |
| 5495 | mbedtls_ssl_config_free(&conf); |
Andrzej Kurek | cfb0194 | 2022-06-06 13:08:23 -0400 | [diff] [blame] | 5496 | } |
| 5497 | /* END_CASE */ |
| 5498 | |
Paul Elliott | b9af2db | 2022-03-09 15:34:37 +0000 | [diff] [blame] | 5499 | /* BEGIN_CASE depends_on:MBEDTLS_TIMING_C:MBEDTLS_HAVE_TIME */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5500 | void timing_final_delay_accessor() |
Paul Elliott | b9af2db | 2022-03-09 15:34:37 +0000 | [diff] [blame] | 5501 | { |
| 5502 | mbedtls_timing_delay_context delay_context; |
| 5503 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5504 | mbedtls_timing_set_delay(&delay_context, 50, 100); |
Paul Elliott | b9af2db | 2022-03-09 15:34:37 +0000 | [diff] [blame] | 5505 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5506 | TEST_ASSERT(mbedtls_timing_get_final_delay(&delay_context) == 100); |
Paul Elliott | b9af2db | 2022-03-09 15:34:37 +0000 | [diff] [blame] | 5507 | } |
| 5508 | /* END_CASE */ |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 5509 | |
| 5510 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5511 | void cid_sanity() |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 5512 | { |
| 5513 | mbedtls_ssl_context ssl; |
| 5514 | mbedtls_ssl_config conf; |
| 5515 | |
| 5516 | unsigned char own_cid[MBEDTLS_SSL_CID_IN_LEN_MAX]; |
| 5517 | unsigned char test_cid[MBEDTLS_SSL_CID_IN_LEN_MAX]; |
| 5518 | int cid_enabled; |
| 5519 | size_t own_cid_len; |
| 5520 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5521 | mbedtls_test_rnd_std_rand(NULL, own_cid, sizeof(own_cid)); |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 5522 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5523 | mbedtls_ssl_init(&ssl); |
| 5524 | mbedtls_ssl_config_init(&conf); |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 5525 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5526 | TEST_ASSERT(mbedtls_ssl_config_defaults(&conf, |
| 5527 | MBEDTLS_SSL_IS_CLIENT, |
| 5528 | MBEDTLS_SSL_TRANSPORT_STREAM, |
| 5529 | MBEDTLS_SSL_PRESET_DEFAULT) |
| 5530 | == 0); |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 5531 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5532 | TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0); |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 5533 | |
| 5534 | /* Can't use CID functions with stream transport. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5535 | TEST_ASSERT(mbedtls_ssl_set_cid(&ssl, MBEDTLS_SSL_CID_ENABLED, own_cid, |
| 5536 | sizeof(own_cid)) |
| 5537 | == MBEDTLS_ERR_SSL_BAD_INPUT_DATA); |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 5538 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5539 | TEST_ASSERT(mbedtls_ssl_get_own_cid(&ssl, &cid_enabled, test_cid, |
| 5540 | &own_cid_len) |
| 5541 | == MBEDTLS_ERR_SSL_BAD_INPUT_DATA); |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 5542 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5543 | TEST_ASSERT(mbedtls_ssl_config_defaults(&conf, |
| 5544 | MBEDTLS_SSL_IS_CLIENT, |
| 5545 | MBEDTLS_SSL_TRANSPORT_DATAGRAM, |
| 5546 | MBEDTLS_SSL_PRESET_DEFAULT) |
| 5547 | == 0); |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 5548 | |
| 5549 | /* Attempt to set config cid size too big. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5550 | TEST_ASSERT(mbedtls_ssl_conf_cid(&conf, MBEDTLS_SSL_CID_IN_LEN_MAX + 1, |
| 5551 | MBEDTLS_SSL_UNEXPECTED_CID_IGNORE) |
| 5552 | == MBEDTLS_ERR_SSL_BAD_INPUT_DATA); |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 5553 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5554 | TEST_ASSERT(mbedtls_ssl_conf_cid(&conf, sizeof(own_cid), |
| 5555 | MBEDTLS_SSL_UNEXPECTED_CID_IGNORE) |
| 5556 | == 0); |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 5557 | |
| 5558 | /* Attempt to set CID length not matching config. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5559 | TEST_ASSERT(mbedtls_ssl_set_cid(&ssl, MBEDTLS_SSL_CID_ENABLED, own_cid, |
| 5560 | MBEDTLS_SSL_CID_IN_LEN_MAX - 1) |
| 5561 | == MBEDTLS_ERR_SSL_BAD_INPUT_DATA); |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 5562 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5563 | TEST_ASSERT(mbedtls_ssl_set_cid(&ssl, MBEDTLS_SSL_CID_ENABLED, own_cid, |
| 5564 | sizeof(own_cid)) |
| 5565 | == 0); |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 5566 | |
| 5567 | /* Test we get back what we put in. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5568 | TEST_ASSERT(mbedtls_ssl_get_own_cid(&ssl, &cid_enabled, test_cid, |
| 5569 | &own_cid_len) |
| 5570 | == 0); |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 5571 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5572 | TEST_EQUAL(cid_enabled, MBEDTLS_SSL_CID_ENABLED); |
| 5573 | ASSERT_COMPARE(own_cid, own_cid_len, test_cid, own_cid_len); |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 5574 | |
| 5575 | /* Test disabling works. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5576 | TEST_ASSERT(mbedtls_ssl_set_cid(&ssl, MBEDTLS_SSL_CID_DISABLED, NULL, |
| 5577 | 0) |
| 5578 | == 0); |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 5579 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5580 | TEST_ASSERT(mbedtls_ssl_get_own_cid(&ssl, &cid_enabled, test_cid, |
| 5581 | &own_cid_len) |
| 5582 | == 0); |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 5583 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5584 | TEST_EQUAL(cid_enabled, MBEDTLS_SSL_CID_DISABLED); |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 5585 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5586 | mbedtls_ssl_free(&ssl); |
| 5587 | mbedtls_ssl_config_free(&conf); |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 5588 | } |
| 5589 | /* END_CASE */ |
| 5590 | |
Valerio Setti | 2f08147 | 2023-02-23 17:36:06 +0100 | [diff] [blame] | 5591 | /* 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] | 5592 | void raw_key_agreement_fail(int bad_server_ecdhe_key) |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 5593 | { |
| 5594 | enum { BUFFSIZE = 17000 }; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 5595 | mbedtls_test_ssl_endpoint client, server; |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 5596 | mbedtls_psa_stats_t stats; |
Andrzej Kurek | 39d88d4 | 2022-03-31 06:30:54 -0400 | [diff] [blame] | 5597 | size_t free_slots_before = -1; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 5598 | mbedtls_test_handshake_test_options options; |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 5599 | |
Andrzej Kurek | cc28e9a | 2022-03-08 18:36:35 -0500 | [diff] [blame] | 5600 | uint16_t iana_tls_group_list[] = { MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, |
| 5601 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE }; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5602 | USE_PSA_INIT(); |
| 5603 | mbedtls_platform_zeroize(&client, sizeof(client)); |
| 5604 | mbedtls_platform_zeroize(&server, sizeof(server)); |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 5605 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 5606 | mbedtls_test_init_handshake_options(&options); |
Andrzej Kurek | 626a931 | 2022-06-10 11:07:39 -0400 | [diff] [blame] | 5607 | options.pk_alg = MBEDTLS_PK_ECDSA; |
| 5608 | |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 5609 | /* Client side, force SECP256R1 to make one key bitflip fail |
Andrzej Kurek | 83e60ee | 2022-04-14 08:51:41 -0400 | [diff] [blame] | 5610 | * the raw key agreement. Flipping the first byte makes the |
| 5611 | * required 0x04 identifier invalid. */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 5612 | TEST_EQUAL(mbedtls_test_ssl_endpoint_init(&client, MBEDTLS_SSL_IS_CLIENT, |
| 5613 | &options, NULL, NULL, |
| 5614 | NULL, iana_tls_group_list), 0); |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 5615 | |
| 5616 | /* Server side */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 5617 | TEST_EQUAL(mbedtls_test_ssl_endpoint_init(&server, MBEDTLS_SSL_IS_SERVER, |
| 5618 | &options, NULL, NULL, |
| 5619 | NULL, NULL), 0); |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 5620 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 5621 | TEST_EQUAL(mbedtls_test_mock_socket_connect(&(client.socket), |
| 5622 | &(server.socket), |
| 5623 | BUFFSIZE), 0); |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 5624 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 5625 | TEST_EQUAL(mbedtls_test_move_handshake_to_state(&(client.ssl), |
| 5626 | &(server.ssl), |
| 5627 | MBEDTLS_SSL_CLIENT_KEY_EXCHANGE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5628 | , 0); |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 5629 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5630 | mbedtls_psa_get_stats(&stats); |
Andrzej Kurek | 39d88d4 | 2022-03-31 06:30:54 -0400 | [diff] [blame] | 5631 | /* Save the number of slots in use up to this point. |
| 5632 | * With PSA, one can be used for the ECDH private key. */ |
| 5633 | free_slots_before = stats.empty_slots; |
Andrzej Kurek | cb33bc5 | 2022-03-31 07:17:18 -0400 | [diff] [blame] | 5634 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5635 | if (bad_server_ecdhe_key) { |
Gilles Peskine | b4f874d | 2022-04-08 16:48:09 -0400 | [diff] [blame] | 5636 | /* Force a simulated bitflip in the server key. to make the |
| 5637 | * raw key agreement in ssl_write_client_key_exchange fail. */ |
| 5638 | (client.ssl).handshake->ecdh_psa_peerkey[0] ^= 0x02; |
| 5639 | } |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 5640 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 5641 | TEST_EQUAL(mbedtls_test_move_handshake_to_state(&(client.ssl), |
| 5642 | &(server.ssl), |
| 5643 | MBEDTLS_SSL_HANDSHAKE_OVER), |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5644 | bad_server_ecdhe_key ? MBEDTLS_ERR_SSL_HW_ACCEL_FAILED : 0); |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 5645 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5646 | mbedtls_psa_get_stats(&stats); |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 5647 | |
Gilles Peskine | b4f874d | 2022-04-08 16:48:09 -0400 | [diff] [blame] | 5648 | /* Make sure that the key slot is already destroyed in case of failure, |
| 5649 | * without waiting to close the connection. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5650 | if (bad_server_ecdhe_key) { |
| 5651 | TEST_EQUAL(free_slots_before, stats.empty_slots); |
| 5652 | } |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 5653 | |
| 5654 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 5655 | mbedtls_test_ssl_endpoint_free(&client, NULL); |
| 5656 | mbedtls_test_ssl_endpoint_free(&server, NULL); |
| 5657 | mbedtls_test_free_handshake_options(&options); |
Andrzej Kurek | cb33bc5 | 2022-03-31 07:17:18 -0400 | [diff] [blame] | 5658 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5659 | USE_PSA_DONE(); |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 5660 | } |
| 5661 | /* END_CASE */ |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5662 | /* 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] | 5663 | void tls13_server_certificate_msg_invalid_vector_len() |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 5664 | { |
| 5665 | int ret = -1; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 5666 | mbedtls_test_ssl_endpoint client_ep, server_ep; |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 5667 | unsigned char *buf, *end; |
| 5668 | size_t buf_len; |
| 5669 | int step = 0; |
| 5670 | int expected_result; |
Ronald Cron | e7b9b6b | 2022-06-10 17:24:31 +0200 | [diff] [blame] | 5671 | mbedtls_ssl_chk_buf_ptr_args expected_chk_buf_ptr_args; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 5672 | mbedtls_test_handshake_test_options client_options; |
| 5673 | mbedtls_test_handshake_test_options server_options; |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 5674 | |
| 5675 | /* |
| 5676 | * Test set-up |
| 5677 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5678 | USE_PSA_INIT(); |
| 5679 | mbedtls_platform_zeroize(&client_ep, sizeof(client_ep)); |
| 5680 | mbedtls_platform_zeroize(&server_ep, sizeof(server_ep)); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 5681 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 5682 | mbedtls_test_init_handshake_options(&client_options); |
Paul Elliott | 9a8d784 | 2022-07-10 12:48:57 +0100 | [diff] [blame] | 5683 | client_options.pk_alg = MBEDTLS_PK_ECDSA; |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 5684 | ret = mbedtls_test_ssl_endpoint_init(&client_ep, MBEDTLS_SSL_IS_CLIENT, |
| 5685 | &client_options, NULL, NULL, NULL, NULL); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5686 | TEST_EQUAL(ret, 0); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 5687 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 5688 | mbedtls_test_init_handshake_options(&server_options); |
Paul Elliott | 9a8d784 | 2022-07-10 12:48:57 +0100 | [diff] [blame] | 5689 | server_options.pk_alg = MBEDTLS_PK_ECDSA; |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 5690 | ret = mbedtls_test_ssl_endpoint_init(&server_ep, MBEDTLS_SSL_IS_SERVER, |
| 5691 | &server_options, NULL, NULL, NULL, NULL); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5692 | TEST_EQUAL(ret, 0); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 5693 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 5694 | ret = mbedtls_test_mock_socket_connect(&(client_ep.socket), |
| 5695 | &(server_ep.socket), 1024); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5696 | TEST_EQUAL(ret, 0); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 5697 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5698 | while (1) { |
| 5699 | mbedtls_test_set_step(++step); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 5700 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 5701 | ret = mbedtls_test_move_handshake_to_state(&(server_ep.ssl), |
| 5702 | &(client_ep.ssl), |
| 5703 | MBEDTLS_SSL_CERTIFICATE_VERIFY); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5704 | TEST_EQUAL(ret, 0); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 5705 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5706 | ret = mbedtls_ssl_flush_output(&(server_ep.ssl)); |
| 5707 | TEST_EQUAL(ret, 0); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 5708 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 5709 | ret = mbedtls_test_move_handshake_to_state(&(client_ep.ssl), |
| 5710 | &(server_ep.ssl), |
| 5711 | MBEDTLS_SSL_SERVER_CERTIFICATE); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5712 | TEST_EQUAL(ret, 0); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 5713 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5714 | ret = mbedtls_ssl_tls13_fetch_handshake_msg(&(client_ep.ssl), |
| 5715 | MBEDTLS_SSL_HS_CERTIFICATE, |
| 5716 | &buf, &buf_len); |
| 5717 | TEST_EQUAL(ret, 0); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 5718 | |
| 5719 | end = buf + buf_len; |
| 5720 | |
| 5721 | /* |
| 5722 | * Tweak server Certificate message and parse it. |
| 5723 | */ |
| 5724 | |
| 5725 | ret = tweak_tls13_certificate_msg_vector_len( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5726 | buf, &end, step, &expected_result, &expected_chk_buf_ptr_args); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 5727 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5728 | if (ret != 0) { |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 5729 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5730 | } |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 5731 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5732 | ret = mbedtls_ssl_tls13_parse_certificate(&(client_ep.ssl), buf, end); |
| 5733 | TEST_EQUAL(ret, expected_result); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 5734 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5735 | TEST_ASSERT(mbedtls_ssl_cmp_chk_buf_ptr_fail_args( |
| 5736 | &expected_chk_buf_ptr_args) == 0); |
Ronald Cron | e7b9b6b | 2022-06-10 17:24:31 +0200 | [diff] [blame] | 5737 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5738 | mbedtls_ssl_reset_chk_buf_ptr_fail_args(); |
Ronald Cron | e7b9b6b | 2022-06-10 17:24:31 +0200 | [diff] [blame] | 5739 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5740 | ret = mbedtls_ssl_session_reset(&(client_ep.ssl)); |
| 5741 | TEST_EQUAL(ret, 0); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 5742 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5743 | ret = mbedtls_ssl_session_reset(&(server_ep.ssl)); |
| 5744 | TEST_EQUAL(ret, 0); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 5745 | } |
| 5746 | |
| 5747 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5748 | mbedtls_ssl_reset_chk_buf_ptr_fail_args(); |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame^] | 5749 | mbedtls_test_ssl_endpoint_free(&client_ep, NULL); |
| 5750 | mbedtls_test_ssl_endpoint_free(&server_ep, NULL); |
| 5751 | mbedtls_test_free_handshake_options(&client_options); |
| 5752 | mbedtls_test_free_handshake_options(&server_options); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5753 | USE_PSA_DONE(); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 5754 | } |
| 5755 | /* END_CASE */ |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 5756 | |
| 5757 | /* BEGIN_CASE depends_on:MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5758 | void ssl_ecjpake_set_password(int use_opaque_arg) |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 5759 | { |
| 5760 | mbedtls_ssl_context ssl; |
| 5761 | mbedtls_ssl_config conf; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5762 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 5763 | mbedtls_svc_key_id_t pwd_slot = MBEDTLS_SVC_KEY_ID_INIT; |
| 5764 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 5765 | (void) use_opaque_arg; |
| 5766 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5767 | unsigned char pwd_string[sizeof(ECJPAKE_TEST_PWD)] = ""; |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 5768 | size_t pwd_len = 0; |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 5769 | int ret; |
| 5770 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5771 | USE_PSA_INIT(); |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 5772 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5773 | mbedtls_ssl_init(&ssl); |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 5774 | |
Valerio Setti | e7518ba | 2022-12-02 12:09:43 +0100 | [diff] [blame] | 5775 | /* test with uninitalized SSL context */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5776 | ECJPAKE_TEST_SET_PASSWORD(MBEDTLS_ERR_SSL_BAD_INPUT_DATA); |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 5777 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5778 | mbedtls_ssl_config_init(&conf); |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 5779 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5780 | TEST_EQUAL(mbedtls_ssl_config_defaults(&conf, |
| 5781 | MBEDTLS_SSL_IS_CLIENT, |
| 5782 | MBEDTLS_SSL_TRANSPORT_STREAM, |
| 5783 | MBEDTLS_SSL_PRESET_DEFAULT), 0); |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 5784 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5785 | TEST_EQUAL(mbedtls_ssl_setup(&ssl, &conf), 0); |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 5786 | |
Valerio Setti | ba22c9c | 2022-12-06 11:42:33 +0100 | [diff] [blame] | 5787 | /* 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] | 5788 | ECJPAKE_TEST_SET_PASSWORD(MBEDTLS_ERR_SSL_BAD_INPUT_DATA); |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 5789 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5790 | pwd_len = strlen(ECJPAKE_TEST_PWD); |
| 5791 | memcpy(pwd_string, ECJPAKE_TEST_PWD, pwd_len); |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 5792 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5793 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 5794 | if (use_opaque_arg) { |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 5795 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Valerio Setti | 785116a | 2022-12-12 11:59:25 +0100 | [diff] [blame] | 5796 | psa_key_attributes_t check_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 5797 | |
Valerio Setti | 2a3ffb4 | 2022-12-08 16:27:46 +0100 | [diff] [blame] | 5798 | /* First try with an invalid usage */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5799 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 5800 | psa_set_key_algorithm(&attributes, PSA_ALG_JPAKE); |
| 5801 | psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD); |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 5802 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5803 | PSA_ASSERT(psa_import_key(&attributes, pwd_string, |
| 5804 | pwd_len, &pwd_slot)); |
Valerio Setti | 2a3ffb4 | 2022-12-08 16:27:46 +0100 | [diff] [blame] | 5805 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5806 | ECJPAKE_TEST_SET_PASSWORD(MBEDTLS_ERR_SSL_HW_ACCEL_FAILED); |
Valerio Setti | 2a3ffb4 | 2022-12-08 16:27:46 +0100 | [diff] [blame] | 5807 | |
Valerio Setti | 9d313df | 2022-12-09 11:38:59 +0100 | [diff] [blame] | 5808 | /* check that the opaque key is still valid after failure */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5809 | TEST_EQUAL(psa_get_key_attributes(pwd_slot, &check_attributes), |
| 5810 | PSA_SUCCESS); |
Valerio Setti | 9d313df | 2022-12-09 11:38:59 +0100 | [diff] [blame] | 5811 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5812 | psa_destroy_key(pwd_slot); |
Valerio Setti | 2a3ffb4 | 2022-12-08 16:27:46 +0100 | [diff] [blame] | 5813 | |
| 5814 | /* Then set the correct usage */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5815 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
Valerio Setti | 2a3ffb4 | 2022-12-08 16:27:46 +0100 | [diff] [blame] | 5816 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5817 | PSA_ASSERT(psa_import_key(&attributes, pwd_string, |
| 5818 | pwd_len, &pwd_slot)); |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 5819 | } |
| 5820 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 5821 | |
Valerio Setti | e7518ba | 2022-12-02 12:09:43 +0100 | [diff] [blame] | 5822 | /* final check which should work without errors */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5823 | ECJPAKE_TEST_SET_PASSWORD(0); |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 5824 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5825 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 5826 | if (use_opaque_arg) { |
| 5827 | psa_destroy_key(pwd_slot); |
Valerio Setti | 31e99bb | 2022-12-09 14:35:10 +0100 | [diff] [blame] | 5828 | } |
Valerio Setti | e7518ba | 2022-12-02 12:09:43 +0100 | [diff] [blame] | 5829 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5830 | mbedtls_ssl_free(&ssl); |
| 5831 | mbedtls_ssl_config_free(&conf); |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 5832 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5833 | USE_PSA_DONE(); |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 5834 | } |
| 5835 | /* END_CASE */ |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 5836 | |
Valerio Setti | 2c12185 | 2023-01-09 18:00:39 +0100 | [diff] [blame] | 5837 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5838 | void elliptic_curve_get_properties() |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 5839 | { |
| 5840 | psa_ecc_family_t psa_family; |
| 5841 | size_t psa_bits; |
| 5842 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5843 | USE_PSA_INIT(); |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 5844 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5845 | #if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_521) |
| 5846 | 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] | 5847 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5848 | 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] | 5849 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5850 | #if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512) |
| 5851 | 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] | 5852 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5853 | 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] | 5854 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5855 | #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_384) |
| 5856 | 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] | 5857 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5858 | 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] | 5859 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5860 | #if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384) |
| 5861 | 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] | 5862 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5863 | 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] | 5864 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5865 | #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_256) |
| 5866 | 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] | 5867 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5868 | 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] | 5869 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5870 | #if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_256) |
| 5871 | 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] | 5872 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5873 | 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] | 5874 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5875 | #if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256) |
| 5876 | 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] | 5877 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5878 | 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] | 5879 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5880 | #if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_224) |
| 5881 | 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] | 5882 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5883 | 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] | 5884 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5885 | #if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_224) |
| 5886 | 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] | 5887 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5888 | 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] | 5889 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5890 | #if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_192) |
| 5891 | 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] | 5892 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5893 | 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] | 5894 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5895 | #if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_192) |
| 5896 | 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] | 5897 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5898 | 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] | 5899 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5900 | #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) || defined(PSA_WANT_ECC_MONTGOMERY_255) |
| 5901 | 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] | 5902 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5903 | 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] | 5904 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5905 | #if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED) || defined(PSA_WANT_ECC_MONTGOMERY_448) |
| 5906 | 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] | 5907 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5908 | 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] | 5909 | #endif |
| 5910 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5911 | USE_PSA_DONE(); |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 5912 | } |
| 5913 | /* END_CASE */ |