Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 1 | /* BEGIN_HEADER */ |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 2 | #include <mbedtls/ssl.h> |
Chris Jones | 84a773f | 2021-03-05 18:38:47 +0000 | [diff] [blame] | 3 | #include <ssl_misc.h> |
Andrzej Kurek | 941962e | 2020-02-07 09:20:32 -0500 | [diff] [blame] | 4 | #include <mbedtls/timing.h> |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 5 | #include <mbedtls/debug.h> |
Hanno Becker | 73c825a | 2020-09-08 10:52:58 +0100 | [diff] [blame] | 6 | #include <ssl_tls13_keys.h> |
Gabor Mezei | b35759d | 2022-02-09 16:59:11 +0100 | [diff] [blame] | 7 | #include <ssl_tls13_invasive.h> |
Mateusz Starzyk | 1aec646 | 2021-02-08 15:34:42 +0100 | [diff] [blame] | 8 | #include "test/certs.h" |
Valerio Setti | d928aeb | 2023-02-08 14:46:15 +0100 | [diff] [blame^] | 9 | #include "pk_wrap.h" |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 10 | |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 11 | #if defined(MBEDTLS_SSL_CACHE_C) |
| 12 | #include "mbedtls/ssl_cache.h" |
| 13 | #endif |
| 14 | |
Manuel Pégourié-Gonnard | 07018f9 | 2022-09-15 11:29:35 +0200 | [diff] [blame] | 15 | #include <mbedtls/legacy_or_psa.h> |
Andrzej Kurek | 21b6870 | 2022-08-17 16:26:12 -0400 | [diff] [blame] | 16 | #include "hash_info.h" |
Hanno Becker | 6667ffd | 2021-04-19 21:59:22 +0100 | [diff] [blame] | 17 | |
Gabor Mezei | 22c9a6f | 2021-10-20 12:09:35 +0200 | [diff] [blame] | 18 | #include <constant_time_internal.h> |
Manuel Pégourié-Gonnard | 9670a59 | 2020-07-10 10:21:46 +0200 | [diff] [blame] | 19 | #include <test/constant_flow.h> |
| 20 | |
Andrzej Kurek | 714ae65 | 2022-11-02 19:07:19 -0400 | [diff] [blame] | 21 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \ |
| 22 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \ |
| 23 | defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) |
| 24 | #define MBEDTLS_CAN_HANDLE_RSA_TEST_KEY |
| 25 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 26 | enum { |
| 27 | #define MBEDTLS_SSL_TLS1_3_LABEL(name, string) \ |
| 28 | tls13_label_ ## name, |
| 29 | MBEDTLS_SSL_TLS1_3_LABEL_LIST |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 30 | #undef MBEDTLS_SSL_TLS1_3_LABEL |
Hanno Becker | 1413bd8 | 2020-09-09 12:46:09 +0100 | [diff] [blame] | 31 | }; |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 32 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 33 | typedef struct log_pattern { |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 34 | const char *pattern; |
| 35 | size_t counter; |
| 36 | } log_pattern; |
| 37 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 38 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 39 | static int rng_seed = 0xBEEF; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 40 | 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] | 41 | { |
| 42 | (void) p_rng; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 43 | for (size_t i = 0; i < output_len; i++) { |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 44 | output[i] = rand(); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 45 | } |
Andrzej Kurek | cccb044 | 2022-08-19 03:42:11 -0400 | [diff] [blame] | 46 | |
Andrzej Kurek | 635c2c2 | 2022-08-17 15:20:40 -0400 | [diff] [blame] | 47 | return 0; |
| 48 | } |
Andrzej Kurek | 32bd063 | 2022-08-20 14:10:36 -0400 | [diff] [blame] | 49 | #endif |
Andrzej Kurek | 635c2c2 | 2022-08-17 15:20:40 -0400 | [diff] [blame] | 50 | |
Piotr Nowicki | 438bf3b | 2020-03-10 12:59:10 +0100 | [diff] [blame] | 51 | /* |
| 52 | * This function can be passed to mbedtls to receive output logs from it. In |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 53 | * this case, it will count the instances of a log_pattern in the received |
| 54 | * logged messages. |
| 55 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 56 | void log_analyzer(void *ctx, int level, |
| 57 | const char *file, int line, |
| 58 | const char *str) |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 59 | { |
| 60 | log_pattern *p = (log_pattern *) ctx; |
| 61 | |
| 62 | (void) level; |
| 63 | (void) line; |
| 64 | (void) file; |
| 65 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 66 | if (NULL != p && |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 67 | NULL != p->pattern && |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 68 | NULL != strstr(str, p->pattern)) { |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 69 | p->counter++; |
| 70 | } |
| 71 | } |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 72 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 73 | typedef struct handshake_test_options { |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 74 | const char *cipher; |
Glenn Strauss | 39e624c | 2022-04-11 13:33:16 -0400 | [diff] [blame] | 75 | mbedtls_ssl_protocol_version client_min_version; |
| 76 | mbedtls_ssl_protocol_version client_max_version; |
| 77 | mbedtls_ssl_protocol_version server_min_version; |
| 78 | mbedtls_ssl_protocol_version server_max_version; |
| 79 | mbedtls_ssl_protocol_version expected_negotiated_version; |
Neil Armstrong | 8c52ed8 | 2022-05-27 13:14:55 +0200 | [diff] [blame] | 80 | int expected_handshake_result; |
| 81 | int expected_ciphersuite; |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 82 | int pk_alg; |
Neil Armstrong | 8c52ed8 | 2022-05-27 13:14:55 +0200 | [diff] [blame] | 83 | int opaque_alg; |
| 84 | int opaque_alg2; |
| 85 | int opaque_usage; |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 86 | data_t *psk_str; |
| 87 | int dtls; |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 88 | int srv_auth_mode; |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 89 | int serialize; |
| 90 | int mfl; |
| 91 | int cli_msg_len; |
| 92 | int srv_msg_len; |
| 93 | int expected_cli_fragments; |
| 94 | int expected_srv_fragments; |
| 95 | int renegotiate; |
| 96 | int legacy_renegotiation; |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 97 | void *srv_log_obj; |
| 98 | void *cli_log_obj; |
| 99 | void (*srv_log_fun)(void *, int, const char *, int, const char *); |
| 100 | void (*cli_log_fun)(void *, int, const char *, int, const char *); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 101 | int resize_buffers; |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 102 | #if defined(MBEDTLS_SSL_CACHE_C) |
Andrzej Kurek | 92d7417 | 2022-06-28 10:29:42 -0400 | [diff] [blame] | 103 | mbedtls_ssl_cache_context *cache; |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 104 | #endif |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 105 | } handshake_test_options; |
| 106 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 107 | void init_handshake_options(handshake_test_options *opts) |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 108 | { |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 109 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 110 | srand(rng_seed); |
Andrzej Kurek | 635c2c2 | 2022-08-17 15:20:40 -0400 | [diff] [blame] | 111 | rng_seed += 0xD0; |
Andrzej Kurek | 32bd063 | 2022-08-20 14:10:36 -0400 | [diff] [blame] | 112 | #endif |
Andrzej Kurek | 3d0d501 | 2022-07-04 05:20:55 -0400 | [diff] [blame] | 113 | opts->cipher = ""; |
| 114 | opts->client_min_version = MBEDTLS_SSL_VERSION_UNKNOWN; |
| 115 | opts->client_max_version = MBEDTLS_SSL_VERSION_UNKNOWN; |
| 116 | opts->server_min_version = MBEDTLS_SSL_VERSION_UNKNOWN; |
| 117 | opts->server_max_version = MBEDTLS_SSL_VERSION_UNKNOWN; |
| 118 | opts->expected_negotiated_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 119 | opts->expected_handshake_result = 0; |
| 120 | opts->expected_ciphersuite = 0; |
| 121 | opts->pk_alg = MBEDTLS_PK_RSA; |
| 122 | opts->opaque_alg = 0; |
| 123 | opts->opaque_alg2 = 0; |
| 124 | opts->opaque_usage = 0; |
| 125 | opts->psk_str = NULL; |
| 126 | opts->dtls = 0; |
| 127 | opts->srv_auth_mode = MBEDTLS_SSL_VERIFY_NONE; |
| 128 | opts->serialize = 0; |
| 129 | opts->mfl = MBEDTLS_SSL_MAX_FRAG_LEN_NONE; |
| 130 | opts->cli_msg_len = 100; |
| 131 | opts->srv_msg_len = 100; |
| 132 | opts->expected_cli_fragments = 1; |
| 133 | opts->expected_srv_fragments = 1; |
| 134 | opts->renegotiate = 0; |
| 135 | opts->legacy_renegotiation = MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION; |
| 136 | opts->srv_log_obj = NULL; |
| 137 | opts->srv_log_obj = NULL; |
| 138 | opts->srv_log_fun = NULL; |
| 139 | opts->cli_log_fun = NULL; |
| 140 | opts->resize_buffers = 1; |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 141 | #if defined(MBEDTLS_SSL_CACHE_C) |
Andrzej Kurek | 9dc4402 | 2022-07-04 05:46:15 -0400 | [diff] [blame] | 142 | opts->cache = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 143 | ASSERT_ALLOC(opts->cache, 1); |
| 144 | mbedtls_ssl_cache_init(opts->cache); |
Andrzej Kurek | 3d0d501 | 2022-07-04 05:20:55 -0400 | [diff] [blame] | 145 | exit: |
| 146 | return; |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 147 | #endif |
| 148 | } |
| 149 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 150 | void free_handshake_options(handshake_test_options *opts) |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 151 | { |
| 152 | #if defined(MBEDTLS_SSL_CACHE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 153 | mbedtls_ssl_cache_free(opts->cache); |
| 154 | mbedtls_free(opts->cache); |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 155 | #else |
| 156 | (void) opts; |
| 157 | #endif |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 158 | } |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 159 | |
Ronald Cron | e7b9b6b | 2022-06-10 17:24:31 +0200 | [diff] [blame] | 160 | #if defined(MBEDTLS_TEST_HOOKS) |
| 161 | static void set_chk_buf_ptr_args( |
| 162 | mbedtls_ssl_chk_buf_ptr_args *args, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 163 | unsigned char *cur, unsigned char *end, size_t need) |
Ronald Cron | e7b9b6b | 2022-06-10 17:24:31 +0200 | [diff] [blame] | 164 | { |
| 165 | args->cur = cur; |
| 166 | args->end = end; |
| 167 | args->need = need; |
| 168 | } |
| 169 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 170 | 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] | 171 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 172 | memset(args, 0, sizeof(*args)); |
Ronald Cron | e7b9b6b | 2022-06-10 17:24:31 +0200 | [diff] [blame] | 173 | } |
| 174 | #endif /* MBEDTLS_TEST_HOOKS */ |
| 175 | |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 176 | /* |
| 177 | * Buffer structure for custom I/O callbacks. |
| 178 | */ |
| 179 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 180 | typedef struct mbedtls_test_buffer { |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 181 | size_t start; |
| 182 | size_t content_length; |
| 183 | size_t capacity; |
| 184 | unsigned char *buffer; |
| 185 | } mbedtls_test_buffer; |
| 186 | |
| 187 | /* |
| 188 | * Initialises \p buf. After calling this function it is safe to call |
| 189 | * `mbedtls_test_buffer_free()` on \p buf. |
| 190 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 191 | void mbedtls_test_buffer_init(mbedtls_test_buffer *buf) |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 192 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 193 | memset(buf, 0, sizeof(*buf)); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | /* |
| 197 | * Sets up \p buf. After calling this function it is safe to call |
| 198 | * `mbedtls_test_buffer_put()` and `mbedtls_test_buffer_get()` on \p buf. |
| 199 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 200 | int mbedtls_test_buffer_setup(mbedtls_test_buffer *buf, size_t capacity) |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 201 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 202 | buf->buffer = (unsigned char *) mbedtls_calloc(capacity, |
| 203 | sizeof(unsigned char)); |
| 204 | if (NULL == buf->buffer) { |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 205 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 206 | } |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 207 | buf->capacity = capacity; |
| 208 | |
| 209 | return 0; |
| 210 | } |
| 211 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 212 | void mbedtls_test_buffer_free(mbedtls_test_buffer *buf) |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 213 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 214 | if (buf->buffer != NULL) { |
| 215 | mbedtls_free(buf->buffer); |
| 216 | } |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 217 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 218 | memset(buf, 0, sizeof(*buf)); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | /* |
| 222 | * Puts \p input_len bytes from the \p input buffer into the ring buffer \p buf. |
| 223 | * |
| 224 | * \p buf must have been initialized and set up by calling |
| 225 | * `mbedtls_test_buffer_init()` and `mbedtls_test_buffer_setup()`. |
| 226 | * |
| 227 | * \retval \p input_len, if the data fits. |
| 228 | * \retval 0 <= value < \p input_len, if the data does not fit. |
| 229 | * \retval -1, if \p buf is NULL, it hasn't been set up or \p input_len is not |
| 230 | * zero and \p input is NULL. |
| 231 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 232 | int mbedtls_test_buffer_put(mbedtls_test_buffer *buf, |
| 233 | const unsigned char *input, size_t input_len) |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 234 | { |
| 235 | size_t overflow = 0; |
| 236 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 237 | if ((buf == NULL) || (buf->buffer == NULL)) { |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 238 | return -1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 239 | } |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 240 | |
| 241 | /* Reduce input_len to a number that fits in the buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 242 | if ((buf->content_length + input_len) > buf->capacity) { |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 243 | input_len = buf->capacity - buf->content_length; |
| 244 | } |
| 245 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 246 | if (input == NULL) { |
| 247 | return (input_len == 0) ? 0 : -1; |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 248 | } |
| 249 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 250 | /* Check if the buffer has not come full circle and free space is not in |
| 251 | * the middle */ |
| 252 | if (buf->start + buf->content_length < buf->capacity) { |
Piotr Nowicki | fb437d7 | 2020-01-13 16:59:12 +0100 | [diff] [blame] | 253 | |
| 254 | /* 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] | 255 | * address */ |
| 256 | if (buf->start + buf->content_length + input_len |
| 257 | > buf->capacity) { |
| 258 | overflow = (buf->start + buf->content_length + input_len) |
| 259 | % buf->capacity; |
Piotr Nowicki | fb437d7 | 2020-01-13 16:59:12 +0100 | [diff] [blame] | 260 | } |
| 261 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 262 | memcpy(buf->buffer + buf->start + buf->content_length, input, |
| 263 | input_len - overflow); |
| 264 | memcpy(buf->buffer, input + input_len - overflow, overflow); |
Piotr Nowicki | fb437d7 | 2020-01-13 16:59:12 +0100 | [diff] [blame] | 265 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 266 | } else { |
Piotr Nowicki | fb437d7 | 2020-01-13 16:59:12 +0100 | [diff] [blame] | 267 | /* 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] | 268 | memcpy(buf->buffer + buf->start + buf->content_length - buf->capacity, |
| 269 | input, input_len); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 270 | } |
| 271 | |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 272 | buf->content_length += input_len; |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 273 | return input_len; |
| 274 | } |
| 275 | |
| 276 | /* |
Andrzej Kurek | f777414 | 2020-01-22 06:34:59 -0500 | [diff] [blame] | 277 | * Gets \p output_len bytes from the ring buffer \p buf into the |
| 278 | * \p output buffer. The output buffer can be NULL, in this case a part of the |
| 279 | * ring buffer will be dropped, if the requested length is available. |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 280 | * |
| 281 | * \p buf must have been initialized and set up by calling |
| 282 | * `mbedtls_test_buffer_init()` and `mbedtls_test_buffer_setup()`. |
| 283 | * |
| 284 | * \retval \p output_len, if the data is available. |
| 285 | * \retval 0 <= value < \p output_len, if the data is not available. |
Andrzej Kurek | f777414 | 2020-01-22 06:34:59 -0500 | [diff] [blame] | 286 | * \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] | 287 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 288 | int mbedtls_test_buffer_get(mbedtls_test_buffer *buf, |
| 289 | unsigned char *output, size_t output_len) |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 290 | { |
| 291 | size_t overflow = 0; |
| 292 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 293 | if ((buf == NULL) || (buf->buffer == NULL)) { |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 294 | return -1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 295 | } |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 296 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 297 | if (output == NULL && output_len == 0) { |
Andrzej Kurek | f777414 | 2020-01-22 06:34:59 -0500 | [diff] [blame] | 298 | return 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 299 | } |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 300 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 301 | if (buf->content_length < output_len) { |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 302 | output_len = buf->content_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 303 | } |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 304 | |
| 305 | /* Calculate the number of bytes that need to be drawn from lower memory |
| 306 | * address */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 307 | if (buf->start + output_len > buf->capacity) { |
| 308 | overflow = (buf->start + output_len) % buf->capacity; |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 309 | } |
| 310 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 311 | if (output != NULL) { |
| 312 | memcpy(output, buf->buffer + buf->start, output_len - overflow); |
| 313 | memcpy(output + output_len - overflow, buf->buffer, overflow); |
Andrzej Kurek | f777414 | 2020-01-22 06:34:59 -0500 | [diff] [blame] | 314 | } |
| 315 | |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 316 | buf->content_length -= output_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 317 | buf->start = (buf->start + output_len) % buf->capacity; |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 318 | |
| 319 | return output_len; |
| 320 | } |
| 321 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 322 | /* |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 323 | * Errors used in the message transport mock tests |
| 324 | */ |
| 325 | #define MBEDTLS_TEST_ERROR_ARG_NULL -11 |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 326 | #define MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED -44 |
| 327 | |
| 328 | /* |
| 329 | * Context for a message metadata queue (fifo) that is on top of the ring buffer. |
| 330 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 331 | typedef struct mbedtls_test_message_queue { |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 332 | size_t *messages; |
| 333 | int pos; |
| 334 | int num; |
| 335 | int capacity; |
| 336 | } mbedtls_test_message_queue; |
| 337 | |
| 338 | /* |
| 339 | * Setup and free functions for the message metadata queue. |
| 340 | * |
| 341 | * \p capacity describes the number of message metadata chunks that can be held |
| 342 | * within the queue. |
| 343 | * |
| 344 | * \retval 0, if a metadata queue of a given length can be allocated. |
| 345 | * \retval MBEDTLS_ERR_SSL_ALLOC_FAILED, if allocation failed. |
| 346 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 347 | int mbedtls_test_message_queue_setup(mbedtls_test_message_queue *queue, |
| 348 | size_t capacity) |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 349 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 350 | queue->messages = (size_t *) mbedtls_calloc(capacity, sizeof(size_t)); |
| 351 | if (NULL == queue->messages) { |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 352 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 353 | } |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 354 | |
| 355 | queue->capacity = capacity; |
| 356 | queue->pos = 0; |
| 357 | queue->num = 0; |
| 358 | |
| 359 | return 0; |
| 360 | } |
| 361 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 362 | void mbedtls_test_message_queue_free(mbedtls_test_message_queue *queue) |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 363 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 364 | if (queue == NULL) { |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 365 | return; |
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 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 368 | if (queue->messages != NULL) { |
| 369 | mbedtls_free(queue->messages); |
| 370 | } |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 371 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 372 | memset(queue, 0, sizeof(*queue)); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | /* |
| 376 | * Push message length information onto the message metadata queue. |
| 377 | * This will become the last element to leave it (fifo). |
| 378 | * |
| 379 | * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null. |
Andrzej Kurek | f46b912 | 2020-02-07 08:19:00 -0500 | [diff] [blame] | 380 | * \retval MBEDTLS_ERR_SSL_WANT_WRITE, if the queue is full. |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 381 | * \retval \p len, if the push was successful. |
| 382 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 383 | int mbedtls_test_message_queue_push_info(mbedtls_test_message_queue *queue, |
| 384 | size_t len) |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 385 | { |
| 386 | int place; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 387 | if (queue == NULL) { |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 388 | return MBEDTLS_TEST_ERROR_ARG_NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 389 | } |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 390 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 391 | if (queue->num >= queue->capacity) { |
Andrzej Kurek | f46b912 | 2020-02-07 08:19:00 -0500 | [diff] [blame] | 392 | return MBEDTLS_ERR_SSL_WANT_WRITE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 393 | } |
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 | place = (queue->pos + queue->num) % queue->capacity; |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 396 | queue->messages[place] = len; |
| 397 | queue->num++; |
| 398 | return len; |
| 399 | } |
| 400 | |
| 401 | /* |
| 402 | * Pop information about the next message length from the queue. This will be |
| 403 | * the oldest inserted message length(fifo). \p msg_len can be null, in which |
| 404 | * case the data will be popped from the queue but not copied anywhere. |
| 405 | * |
| 406 | * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null. |
Andrzej Kurek | f46b912 | 2020-02-07 08:19:00 -0500 | [diff] [blame] | 407 | * \retval MBEDTLS_ERR_SSL_WANT_READ, if the queue is empty. |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 408 | * \retval message length, if the pop was successful, up to the given |
| 409 | \p buf_len. |
| 410 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 411 | int mbedtls_test_message_queue_pop_info(mbedtls_test_message_queue *queue, |
| 412 | size_t buf_len) |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 413 | { |
| 414 | size_t message_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 415 | if (queue == NULL) { |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 416 | return MBEDTLS_TEST_ERROR_ARG_NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 417 | } |
| 418 | if (queue->num == 0) { |
Andrzej Kurek | f46b912 | 2020-02-07 08:19:00 -0500 | [diff] [blame] | 419 | return MBEDTLS_ERR_SSL_WANT_READ; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 420 | } |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 421 | |
| 422 | message_length = queue->messages[queue->pos]; |
| 423 | queue->messages[queue->pos] = 0; |
| 424 | queue->num--; |
| 425 | queue->pos++; |
| 426 | queue->pos %= queue->capacity; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 427 | if (queue->pos < 0) { |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 428 | queue->pos += queue->capacity; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 429 | } |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 430 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 431 | return (message_length > buf_len) ? buf_len : message_length; |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | /* |
| 435 | * Take a peek on the info about the next message length from the queue. |
| 436 | * This will be the oldest inserted message length(fifo). |
| 437 | * |
| 438 | * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null. |
Andrzej Kurek | f46b912 | 2020-02-07 08:19:00 -0500 | [diff] [blame] | 439 | * \retval MBEDTLS_ERR_SSL_WANT_READ, if the queue is empty. |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 440 | * \retval 0, if the peek was successful. |
| 441 | * \retval MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED, if the given buffer length is |
| 442 | * too small to fit the message. In this case the \p msg_len will be |
| 443 | * set to the full message length so that the |
| 444 | * caller knows what portion of the message can be dropped. |
| 445 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 446 | int mbedtls_test_message_queue_peek_info(mbedtls_test_message_queue *queue, |
| 447 | size_t buf_len, size_t *msg_len) |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 448 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 449 | if (queue == NULL || msg_len == NULL) { |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 450 | return MBEDTLS_TEST_ERROR_ARG_NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 451 | } |
| 452 | if (queue->num == 0) { |
Andrzej Kurek | f46b912 | 2020-02-07 08:19:00 -0500 | [diff] [blame] | 453 | return MBEDTLS_ERR_SSL_WANT_READ; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 454 | } |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 455 | |
| 456 | *msg_len = queue->messages[queue->pos]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 457 | return (*msg_len > buf_len) ? MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED : 0; |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 458 | } |
| 459 | /* |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 460 | * Context for the I/O callbacks simulating network connection. |
| 461 | */ |
| 462 | |
| 463 | #define MBEDTLS_MOCK_SOCKET_CONNECTED 1 |
| 464 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 465 | typedef struct mbedtls_mock_socket { |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 466 | int status; |
| 467 | mbedtls_test_buffer *input; |
| 468 | mbedtls_test_buffer *output; |
| 469 | struct mbedtls_mock_socket *peer; |
| 470 | } mbedtls_mock_socket; |
| 471 | |
| 472 | /* |
| 473 | * Setup and teardown functions for mock sockets. |
| 474 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 475 | void mbedtls_mock_socket_init(mbedtls_mock_socket *socket) |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 476 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 477 | memset(socket, 0, sizeof(*socket)); |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | /* |
| 481 | * Closes the socket \p socket. |
| 482 | * |
| 483 | * \p socket must have been previously initialized by calling |
| 484 | * mbedtls_mock_socket_init(). |
| 485 | * |
| 486 | * This function frees all allocated resources and both sockets are aware of the |
| 487 | * new connection state. |
| 488 | * |
| 489 | * That is, this function does not simulate half-open TCP connections and the |
| 490 | * phenomenon that when closing a UDP connection the peer is not aware of the |
| 491 | * connection having been closed. |
| 492 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 493 | void mbedtls_mock_socket_close(mbedtls_mock_socket *socket) |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 494 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 495 | if (socket == NULL) { |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 496 | return; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 497 | } |
| 498 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 499 | if (socket->input != NULL) { |
| 500 | mbedtls_test_buffer_free(socket->input); |
| 501 | mbedtls_free(socket->input); |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 502 | } |
| 503 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 504 | if (socket->output != NULL) { |
| 505 | mbedtls_test_buffer_free(socket->output); |
| 506 | mbedtls_free(socket->output); |
| 507 | } |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 508 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 509 | if (socket->peer != NULL) { |
| 510 | memset(socket->peer, 0, sizeof(*socket->peer)); |
| 511 | } |
| 512 | |
| 513 | memset(socket, 0, sizeof(*socket)); |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | /* |
| 517 | * Establishes a connection between \p peer1 and \p peer2. |
| 518 | * |
| 519 | * \p peer1 and \p peer2 must have been previously initialized by calling |
| 520 | * mbedtls_mock_socket_init(). |
| 521 | * |
Tom Cosgrove | 1797b05 | 2022-12-04 17:19:59 +0000 | [diff] [blame] | 522 | * 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] | 523 | * the correct value allows for simulation of MTU, sanity testing the mock |
| 524 | * implementation and mocking TCP connections with lower memory cost. |
| 525 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 526 | int mbedtls_mock_socket_connect(mbedtls_mock_socket *peer1, |
| 527 | mbedtls_mock_socket *peer2, |
| 528 | size_t bufsize) |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 529 | { |
| 530 | int ret = -1; |
| 531 | |
Piotr Nowicki | d796e19 | 2020-01-28 12:09:47 +0100 | [diff] [blame] | 532 | peer1->output = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 533 | (mbedtls_test_buffer *) mbedtls_calloc(1, sizeof(mbedtls_test_buffer)); |
| 534 | if (peer1->output == NULL) { |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 535 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 536 | goto exit; |
| 537 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 538 | mbedtls_test_buffer_init(peer1->output); |
| 539 | if (0 != (ret = mbedtls_test_buffer_setup(peer1->output, bufsize))) { |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 540 | goto exit; |
| 541 | } |
| 542 | |
Piotr Nowicki | d796e19 | 2020-01-28 12:09:47 +0100 | [diff] [blame] | 543 | peer2->output = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 544 | (mbedtls_test_buffer *) mbedtls_calloc(1, sizeof(mbedtls_test_buffer)); |
| 545 | if (peer2->output == NULL) { |
Piotr Nowicki | d796e19 | 2020-01-28 12:09:47 +0100 | [diff] [blame] | 546 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 547 | goto exit; |
| 548 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 549 | mbedtls_test_buffer_init(peer2->output); |
| 550 | if (0 != (ret = mbedtls_test_buffer_setup(peer2->output, bufsize))) { |
Piotr Nowicki | d796e19 | 2020-01-28 12:09:47 +0100 | [diff] [blame] | 551 | goto exit; |
| 552 | } |
| 553 | |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 554 | peer1->peer = peer2; |
| 555 | peer2->peer = peer1; |
Piotr Nowicki | d796e19 | 2020-01-28 12:09:47 +0100 | [diff] [blame] | 556 | peer1->input = peer2->output; |
| 557 | peer2->input = peer1->output; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 558 | |
| 559 | peer1->status = peer2->status = MBEDTLS_MOCK_SOCKET_CONNECTED; |
| 560 | ret = 0; |
| 561 | |
| 562 | exit: |
| 563 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 564 | if (ret != 0) { |
| 565 | mbedtls_mock_socket_close(peer1); |
| 566 | mbedtls_mock_socket_close(peer2); |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | return ret; |
| 570 | } |
| 571 | |
| 572 | /* |
| 573 | * Callbacks for simulating blocking I/O over connection-oriented transport. |
| 574 | */ |
| 575 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 576 | int mbedtls_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] | 577 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 578 | mbedtls_mock_socket *socket = (mbedtls_mock_socket *) ctx; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 579 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 580 | if (socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED) { |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 581 | return -1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 582 | } |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 583 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 584 | return mbedtls_test_buffer_put(socket->output, buf, len); |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 585 | } |
| 586 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 587 | int mbedtls_mock_tcp_recv_b(void *ctx, unsigned char *buf, size_t len) |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 588 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 589 | mbedtls_mock_socket *socket = (mbedtls_mock_socket *) ctx; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 590 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 591 | if (socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED) { |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 592 | return -1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 593 | } |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 594 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 595 | return mbedtls_test_buffer_get(socket->input, buf, len); |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | /* |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 599 | * Callbacks for simulating non-blocking I/O over connection-oriented transport. |
| 600 | */ |
| 601 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 602 | int mbedtls_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] | 603 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 604 | mbedtls_mock_socket *socket = (mbedtls_mock_socket *) ctx; |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 605 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 606 | if (socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED) { |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 607 | return -1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 608 | } |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 609 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 610 | if (socket->output->capacity == socket->output->content_length) { |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 611 | return MBEDTLS_ERR_SSL_WANT_WRITE; |
| 612 | } |
| 613 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 614 | return mbedtls_test_buffer_put(socket->output, buf, len); |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 615 | } |
| 616 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 617 | int mbedtls_mock_tcp_recv_nb(void *ctx, unsigned char *buf, size_t len) |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 618 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 619 | mbedtls_mock_socket *socket = (mbedtls_mock_socket *) ctx; |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 620 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 621 | if (socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED) { |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 622 | return -1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 623 | } |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 624 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 625 | if (socket->input->content_length == 0) { |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 626 | return MBEDTLS_ERR_SSL_WANT_READ; |
| 627 | } |
| 628 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 629 | return mbedtls_test_buffer_get(socket->input, buf, len); |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 630 | } |
| 631 | |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 632 | /* Errors used in the message socket mocks */ |
| 633 | |
| 634 | #define MBEDTLS_TEST_ERROR_CONTEXT_ERROR -55 |
| 635 | #define MBEDTLS_TEST_ERROR_SEND_FAILED -66 |
| 636 | #define MBEDTLS_TEST_ERROR_RECV_FAILED -77 |
| 637 | |
| 638 | /* |
| 639 | * Structure used as an addon, or a wrapper, around the mocked sockets. |
| 640 | * Contains an input queue, to which the other socket pushes metadata, |
| 641 | * and an output queue, to which this one pushes metadata. This context is |
| 642 | * considered as an owner of the input queue only, which is initialized and |
| 643 | * freed in the respective setup and free calls. |
| 644 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 645 | typedef struct mbedtls_test_message_socket_context { |
| 646 | mbedtls_test_message_queue *queue_input; |
| 647 | mbedtls_test_message_queue *queue_output; |
| 648 | mbedtls_mock_socket *socket; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 649 | } mbedtls_test_message_socket_context; |
| 650 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 651 | void mbedtls_message_socket_init(mbedtls_test_message_socket_context *ctx) |
Andrzej Kurek | 45916ba | 2020-03-05 14:46:22 -0500 | [diff] [blame] | 652 | { |
| 653 | ctx->queue_input = NULL; |
| 654 | ctx->queue_output = NULL; |
| 655 | ctx->socket = NULL; |
| 656 | } |
| 657 | |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 658 | /* |
Tom Cosgrove | 1797b05 | 2022-12-04 17:19:59 +0000 | [diff] [blame] | 659 | * Setup a given message socket context including initialization of |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 660 | * input/output queues to a chosen capacity of messages. Also set the |
| 661 | * corresponding mock socket. |
| 662 | * |
| 663 | * \retval 0, if everything succeeds. |
| 664 | * \retval MBEDTLS_ERR_SSL_ALLOC_FAILED, if allocation of a message |
| 665 | * queue failed. |
| 666 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 667 | int mbedtls_message_socket_setup(mbedtls_test_message_queue *queue_input, |
| 668 | mbedtls_test_message_queue *queue_output, |
| 669 | size_t queue_capacity, |
| 670 | mbedtls_mock_socket *socket, |
| 671 | mbedtls_test_message_socket_context *ctx) |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 672 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 673 | int ret = mbedtls_test_message_queue_setup(queue_input, queue_capacity); |
| 674 | if (ret != 0) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 675 | return ret; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 676 | } |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 677 | ctx->queue_input = queue_input; |
| 678 | ctx->queue_output = queue_output; |
| 679 | ctx->socket = socket; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 680 | mbedtls_mock_socket_init(socket); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 681 | |
| 682 | return 0; |
| 683 | } |
| 684 | |
| 685 | /* |
| 686 | * Close a given message socket context, along with the socket itself. Free the |
| 687 | * memory allocated by the input queue. |
| 688 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 689 | void mbedtls_message_socket_close(mbedtls_test_message_socket_context *ctx) |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 690 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 691 | if (ctx == NULL) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 692 | return; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 693 | } |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 694 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 695 | mbedtls_test_message_queue_free(ctx->queue_input); |
| 696 | mbedtls_mock_socket_close(ctx->socket); |
| 697 | memset(ctx, 0, sizeof(*ctx)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | /* |
| 701 | * Send one message through a given message socket context. |
| 702 | * |
| 703 | * \retval \p len, if everything succeeds. |
| 704 | * \retval MBEDTLS_TEST_ERROR_CONTEXT_ERROR, if any of the needed context |
| 705 | * elements or the context itself is null. |
| 706 | * \retval MBEDTLS_TEST_ERROR_SEND_FAILED if mbedtls_mock_tcp_send_b failed. |
Andrzej Kurek | f46b912 | 2020-02-07 08:19:00 -0500 | [diff] [blame] | 707 | * \retval MBEDTLS_ERR_SSL_WANT_WRITE, if the output queue is full. |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 708 | * |
| 709 | * This function will also return any error from |
| 710 | * mbedtls_test_message_queue_push_info. |
| 711 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 712 | int mbedtls_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] | 713 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 714 | mbedtls_test_message_queue *queue; |
| 715 | mbedtls_mock_socket *socket; |
| 716 | mbedtls_test_message_socket_context *context = (mbedtls_test_message_socket_context *) ctx; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 717 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 718 | if (context == NULL || context->socket == NULL |
| 719 | || context->queue_output == NULL) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 720 | return MBEDTLS_TEST_ERROR_CONTEXT_ERROR; |
| 721 | } |
| 722 | |
| 723 | queue = context->queue_output; |
| 724 | socket = context->socket; |
| 725 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 726 | if (queue->num >= queue->capacity) { |
Andrzej Kurek | f46b912 | 2020-02-07 08:19:00 -0500 | [diff] [blame] | 727 | return MBEDTLS_ERR_SSL_WANT_WRITE; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 728 | } |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 729 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 730 | if (mbedtls_mock_tcp_send_b(socket, buf, len) != (int) len) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 731 | return MBEDTLS_TEST_ERROR_SEND_FAILED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 732 | } |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 733 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 734 | return mbedtls_test_message_queue_push_info(queue, len); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | /* |
| 738 | * Receive one message from a given message socket context and return message |
| 739 | * length or an error. |
| 740 | * |
| 741 | * \retval message length, if everything succeeds. |
| 742 | * \retval MBEDTLS_TEST_ERROR_CONTEXT_ERROR, if any of the needed context |
| 743 | * elements or the context itself is null. |
| 744 | * \retval MBEDTLS_TEST_ERROR_RECV_FAILED if mbedtls_mock_tcp_recv_b failed. |
| 745 | * |
| 746 | * This function will also return any error other than |
| 747 | * MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED from mbedtls_test_message_queue_peek_info. |
| 748 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 749 | int mbedtls_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] | 750 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 751 | mbedtls_test_message_queue *queue; |
| 752 | mbedtls_mock_socket *socket; |
| 753 | mbedtls_test_message_socket_context *context = (mbedtls_test_message_socket_context *) ctx; |
Gilles Peskine | 19e841e | 2020-03-09 20:43:51 +0100 | [diff] [blame] | 754 | size_t drop_len = 0; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 755 | size_t msg_len; |
| 756 | int ret; |
| 757 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 758 | if (context == NULL || context->socket == NULL |
| 759 | || context->queue_input == NULL) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 760 | return MBEDTLS_TEST_ERROR_CONTEXT_ERROR; |
| 761 | } |
| 762 | |
| 763 | queue = context->queue_input; |
| 764 | socket = context->socket; |
| 765 | |
| 766 | /* Peek first, so that in case of a socket error the data remains in |
| 767 | * the queue. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 768 | ret = mbedtls_test_message_queue_peek_info(queue, buf_len, &msg_len); |
| 769 | if (ret == MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 770 | /* Calculate how much to drop */ |
| 771 | drop_len = msg_len - buf_len; |
| 772 | |
| 773 | /* Set the requested message len to be buffer length */ |
| 774 | msg_len = buf_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 775 | } else if (ret != 0) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 776 | return ret; |
| 777 | } |
| 778 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 779 | if (mbedtls_mock_tcp_recv_b(socket, buf, msg_len) != (int) msg_len) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 780 | return MBEDTLS_TEST_ERROR_RECV_FAILED; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 781 | } |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 782 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 783 | if (ret == MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 784 | /* Drop the remaining part of the message */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 785 | if (mbedtls_mock_tcp_recv_b(socket, NULL, drop_len) != (int) drop_len) { |
| 786 | /* Inconsistent state - part of the message was read, |
| 787 | * and a part couldn't. Not much we can do here, but it should not |
| 788 | * happen in test environment, unless forced manually. */ |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 789 | } |
| 790 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 791 | mbedtls_test_message_queue_pop_info(queue, buf_len); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 792 | |
| 793 | return msg_len; |
| 794 | } |
| 795 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 796 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 797 | |
| 798 | /* |
| 799 | * Structure with endpoint's certificates for SSL communication tests. |
| 800 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 801 | typedef struct mbedtls_endpoint_certificate { |
| 802 | mbedtls_x509_crt *ca_cert; |
| 803 | mbedtls_x509_crt *cert; |
| 804 | mbedtls_pk_context *pkey; |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 805 | } mbedtls_endpoint_certificate; |
| 806 | |
| 807 | /* |
| 808 | * Endpoint structure for SSL communication tests. |
| 809 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 810 | typedef struct mbedtls_endpoint { |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 811 | const char *name; |
| 812 | mbedtls_ssl_context ssl; |
| 813 | mbedtls_ssl_config conf; |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 814 | mbedtls_mock_socket socket; |
| 815 | mbedtls_endpoint_certificate cert; |
| 816 | } mbedtls_endpoint; |
| 817 | |
| 818 | /* |
Andrzej Kurek | 0abebeb | 2022-09-30 12:54:41 -0400 | [diff] [blame] | 819 | * Deinitializes certificates from endpoint represented by \p ep. |
| 820 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 821 | void mbedtls_endpoint_certificate_free(mbedtls_endpoint *ep) |
Andrzej Kurek | 0abebeb | 2022-09-30 12:54:41 -0400 | [diff] [blame] | 822 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 823 | mbedtls_endpoint_certificate *cert = &(ep->cert); |
| 824 | if (cert != NULL) { |
| 825 | if (cert->ca_cert != NULL) { |
| 826 | mbedtls_x509_crt_free(cert->ca_cert); |
| 827 | mbedtls_free(cert->ca_cert); |
Andrzej Kurek | 0abebeb | 2022-09-30 12:54:41 -0400 | [diff] [blame] | 828 | cert->ca_cert = NULL; |
| 829 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 830 | if (cert->cert != NULL) { |
| 831 | mbedtls_x509_crt_free(cert->cert); |
| 832 | mbedtls_free(cert->cert); |
Andrzej Kurek | 0abebeb | 2022-09-30 12:54:41 -0400 | [diff] [blame] | 833 | cert->cert = NULL; |
| 834 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 835 | if (cert->pkey != NULL) { |
Andrzej Kurek | 0abebeb | 2022-09-30 12:54:41 -0400 | [diff] [blame] | 836 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 837 | if (mbedtls_pk_get_type(cert->pkey) == MBEDTLS_PK_OPAQUE) { |
Andrzej Kurek | 0abebeb | 2022-09-30 12:54:41 -0400 | [diff] [blame] | 838 | mbedtls_svc_key_id_t *key_slot = cert->pkey->pk_ctx; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 839 | psa_destroy_key(*key_slot); |
Andrzej Kurek | 0abebeb | 2022-09-30 12:54:41 -0400 | [diff] [blame] | 840 | } |
| 841 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 842 | mbedtls_pk_free(cert->pkey); |
| 843 | mbedtls_free(cert->pkey); |
Andrzej Kurek | 0abebeb | 2022-09-30 12:54:41 -0400 | [diff] [blame] | 844 | cert->pkey = NULL; |
| 845 | } |
| 846 | } |
| 847 | } |
| 848 | |
| 849 | /* |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 850 | * Initializes \p ep_cert structure and assigns it to endpoint |
| 851 | * represented by \p ep. |
| 852 | * |
| 853 | * \retval 0 on success, otherwise error code. |
| 854 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 855 | int mbedtls_endpoint_certificate_init(mbedtls_endpoint *ep, int pk_alg, |
| 856 | int opaque_alg, int opaque_alg2, |
| 857 | int opaque_usage) |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 858 | { |
| 859 | int i = 0; |
| 860 | int ret = -1; |
Andrzej Kurek | 0abebeb | 2022-09-30 12:54:41 -0400 | [diff] [blame] | 861 | mbedtls_endpoint_certificate *cert = NULL; |
Neil Armstrong | 8c52ed8 | 2022-05-27 13:14:55 +0200 | [diff] [blame] | 862 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 863 | mbedtls_svc_key_id_t key_slot = MBEDTLS_SVC_KEY_ID_INIT; |
| 864 | #endif |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 865 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 866 | if (ep == NULL) { |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 867 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 868 | } |
| 869 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 870 | cert = &(ep->cert); |
| 871 | ASSERT_ALLOC(cert->ca_cert, 1); |
| 872 | ASSERT_ALLOC(cert->cert, 1); |
| 873 | ASSERT_ALLOC(cert->pkey, 1); |
Andrzej Kurek | 0abebeb | 2022-09-30 12:54:41 -0400 | [diff] [blame] | 874 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 875 | mbedtls_x509_crt_init(cert->ca_cert); |
| 876 | mbedtls_x509_crt_init(cert->cert); |
| 877 | mbedtls_pk_init(cert->pkey); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 878 | |
| 879 | /* Load the trusted CA */ |
| 880 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 881 | for (i = 0; mbedtls_test_cas_der[i] != NULL; i++) { |
| 882 | ret = mbedtls_x509_crt_parse_der(cert->ca_cert, |
| 883 | (const unsigned char *) mbedtls_test_cas_der[i], |
| 884 | mbedtls_test_cas_der_len[i]); |
| 885 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 886 | } |
| 887 | |
| 888 | /* Load own certificate and private key */ |
| 889 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 890 | if (ep->conf.endpoint == MBEDTLS_SSL_IS_SERVER) { |
| 891 | if (pk_alg == MBEDTLS_PK_RSA) { |
| 892 | ret = mbedtls_x509_crt_parse(cert->cert, |
| 893 | (const unsigned char *) mbedtls_test_srv_crt_rsa_sha256_der, |
| 894 | mbedtls_test_srv_crt_rsa_sha256_der_len); |
| 895 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 896 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 897 | ret = mbedtls_pk_parse_key(cert->pkey, |
| 898 | (const unsigned char *) mbedtls_test_srv_key_rsa_der, |
| 899 | mbedtls_test_srv_key_rsa_der_len, NULL, 0, |
| 900 | mbedtls_test_rnd_std_rand, NULL); |
| 901 | TEST_ASSERT(ret == 0); |
| 902 | } else { |
| 903 | ret = mbedtls_x509_crt_parse(cert->cert, |
| 904 | (const unsigned char *) mbedtls_test_srv_crt_ec_der, |
| 905 | mbedtls_test_srv_crt_ec_der_len); |
| 906 | TEST_ASSERT(ret == 0); |
| 907 | |
| 908 | ret = mbedtls_pk_parse_key(cert->pkey, |
| 909 | (const unsigned char *) mbedtls_test_srv_key_ec_der, |
| 910 | mbedtls_test_srv_key_ec_der_len, NULL, 0, |
| 911 | mbedtls_test_rnd_std_rand, NULL); |
| 912 | TEST_ASSERT(ret == 0); |
Andrzej Kurek | b298074 | 2020-02-02 19:25:26 -0500 | [diff] [blame] | 913 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 914 | } else { |
| 915 | if (pk_alg == MBEDTLS_PK_RSA) { |
| 916 | ret = mbedtls_x509_crt_parse(cert->cert, |
| 917 | (const unsigned char *) mbedtls_test_cli_crt_rsa_der, |
| 918 | mbedtls_test_cli_crt_rsa_der_len); |
| 919 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 920 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 921 | ret = mbedtls_pk_parse_key(cert->pkey, |
| 922 | (const unsigned char *) mbedtls_test_cli_key_rsa_der, |
| 923 | mbedtls_test_cli_key_rsa_der_len, NULL, 0, |
| 924 | mbedtls_test_rnd_std_rand, NULL); |
| 925 | TEST_ASSERT(ret == 0); |
| 926 | } else { |
| 927 | ret = mbedtls_x509_crt_parse(cert->cert, |
| 928 | (const unsigned char *) mbedtls_test_cli_crt_ec_der, |
| 929 | mbedtls_test_cli_crt_ec_len); |
| 930 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 931 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 932 | ret = mbedtls_pk_parse_key(cert->pkey, |
| 933 | (const unsigned char *) mbedtls_test_cli_key_ec_der, |
| 934 | mbedtls_test_cli_key_ec_der_len, NULL, 0, |
| 935 | mbedtls_test_rnd_std_rand, NULL); |
| 936 | TEST_ASSERT(ret == 0); |
Andrzej Kurek | b298074 | 2020-02-02 19:25:26 -0500 | [diff] [blame] | 937 | } |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 938 | } |
| 939 | |
Neil Armstrong | 8c52ed8 | 2022-05-27 13:14:55 +0200 | [diff] [blame] | 940 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 941 | if (opaque_alg != 0) { |
| 942 | TEST_EQUAL(mbedtls_pk_wrap_as_opaque(cert->pkey, &key_slot, |
| 943 | opaque_alg, opaque_usage, |
| 944 | opaque_alg2), 0); |
Neil Armstrong | 8c52ed8 | 2022-05-27 13:14:55 +0200 | [diff] [blame] | 945 | } |
| 946 | #else |
| 947 | (void) opaque_alg; |
| 948 | (void) opaque_alg2; |
| 949 | (void) opaque_usage; |
| 950 | #endif |
| 951 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 952 | mbedtls_ssl_conf_ca_chain(&(ep->conf), cert->ca_cert, NULL); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 953 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 954 | ret = mbedtls_ssl_conf_own_cert(&(ep->conf), cert->cert, |
| 955 | cert->pkey); |
| 956 | TEST_ASSERT(ret == 0); |
| 957 | TEST_ASSERT(ep->conf.key_cert != NULL); |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 958 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 959 | ret = mbedtls_ssl_conf_own_cert(&(ep->conf), NULL, NULL); |
| 960 | TEST_ASSERT(ret == 0); |
| 961 | TEST_ASSERT(ep->conf.key_cert == NULL); |
Glenn Strauss | 36872db | 2022-01-22 05:06:31 -0500 | [diff] [blame] | 962 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 963 | ret = mbedtls_ssl_conf_own_cert(&(ep->conf), cert->cert, |
| 964 | cert->pkey); |
| 965 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 966 | |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 967 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 968 | if (ret != 0) { |
| 969 | mbedtls_endpoint_certificate_free(ep); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 970 | } |
| 971 | |
| 972 | return ret; |
| 973 | } |
| 974 | |
| 975 | /* |
| 976 | * Initializes \p ep structure. It is important to call `mbedtls_endpoint_free()` |
| 977 | * after calling this function even if it fails. |
| 978 | * |
| 979 | * \p endpoint_type must be set as MBEDTLS_SSL_IS_SERVER or |
| 980 | * MBEDTLS_SSL_IS_CLIENT. |
Andrzej Kurek | 15daf50 | 2020-02-12 09:17:52 -0500 | [diff] [blame] | 981 | * \p pk_alg the algorithm to use, currently only MBEDTLS_PK_RSA and |
| 982 | * MBEDTLS_PK_ECDSA are supported. |
| 983 | * \p dtls_context - in case of DTLS - this is the context handling metadata. |
| 984 | * \p input_queue - used only in case of DTLS. |
| 985 | * \p output_queue - used only in case of DTLS. |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 986 | * |
| 987 | * \retval 0 on success, otherwise error code. |
| 988 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 989 | int mbedtls_endpoint_init(mbedtls_endpoint *ep, int endpoint_type, |
| 990 | handshake_test_options *options, |
| 991 | mbedtls_test_message_socket_context *dtls_context, |
| 992 | mbedtls_test_message_queue *input_queue, |
| 993 | mbedtls_test_message_queue *output_queue, |
| 994 | uint16_t *group_list) |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 995 | { |
| 996 | int ret = -1; |
Gilles Peskine | 80dae04 | 2022-01-21 23:50:39 +0100 | [diff] [blame] | 997 | uintptr_t user_data_n; |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 998 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 999 | if (dtls_context != NULL && (input_queue == NULL || output_queue == NULL)) { |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 1000 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Andrzej Kurek | 15daf50 | 2020-02-12 09:17:52 -0500 | [diff] [blame] | 1001 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1002 | |
| 1003 | if (ep == NULL) { |
| 1004 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 1005 | } |
| 1006 | |
| 1007 | memset(ep, 0, sizeof(*ep)); |
| 1008 | |
| 1009 | ep->name = (endpoint_type == MBEDTLS_SSL_IS_SERVER) ? "Server" : "Client"; |
| 1010 | |
| 1011 | mbedtls_ssl_init(&(ep->ssl)); |
| 1012 | mbedtls_ssl_config_init(&(ep->conf)); |
| 1013 | mbedtls_ssl_conf_rng(&(ep->conf), rng_get, NULL); |
| 1014 | |
| 1015 | TEST_ASSERT(mbedtls_ssl_conf_get_user_data_p(&ep->conf) == NULL); |
| 1016 | TEST_EQUAL(mbedtls_ssl_conf_get_user_data_n(&ep->conf), 0); |
| 1017 | TEST_ASSERT(mbedtls_ssl_get_user_data_p(&ep->ssl) == NULL); |
| 1018 | TEST_EQUAL(mbedtls_ssl_get_user_data_n(&ep->ssl), 0); |
| 1019 | |
| 1020 | (void) mbedtls_test_rnd_std_rand(NULL, |
| 1021 | (void *) &user_data_n, |
| 1022 | sizeof(user_data_n)); |
| 1023 | mbedtls_ssl_conf_set_user_data_n(&ep->conf, user_data_n); |
| 1024 | mbedtls_ssl_set_user_data_n(&ep->ssl, user_data_n); |
| 1025 | |
| 1026 | if (dtls_context != NULL) { |
| 1027 | TEST_ASSERT(mbedtls_message_socket_setup(input_queue, output_queue, |
| 1028 | 100, &(ep->socket), |
| 1029 | dtls_context) == 0); |
| 1030 | } else { |
| 1031 | mbedtls_mock_socket_init(&(ep->socket)); |
Andrzej Kurek | 15daf50 | 2020-02-12 09:17:52 -0500 | [diff] [blame] | 1032 | } |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 1033 | |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 1034 | /* Non-blocking callbacks without timeout */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1035 | if (dtls_context != NULL) { |
| 1036 | mbedtls_ssl_set_bio(&(ep->ssl), dtls_context, |
| 1037 | mbedtls_mock_tcp_send_msg, |
| 1038 | mbedtls_mock_tcp_recv_msg, |
| 1039 | NULL); |
| 1040 | } else { |
| 1041 | mbedtls_ssl_set_bio(&(ep->ssl), &(ep->socket), |
| 1042 | mbedtls_mock_tcp_send_nb, |
| 1043 | mbedtls_mock_tcp_recv_nb, |
| 1044 | NULL); |
Andrzej Kurek | 15daf50 | 2020-02-12 09:17:52 -0500 | [diff] [blame] | 1045 | } |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 1046 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1047 | ret = mbedtls_ssl_config_defaults(&(ep->conf), endpoint_type, |
| 1048 | (dtls_context != NULL) ? |
| 1049 | MBEDTLS_SSL_TRANSPORT_DATAGRAM : |
| 1050 | MBEDTLS_SSL_TRANSPORT_STREAM, |
| 1051 | MBEDTLS_SSL_PRESET_DEFAULT); |
| 1052 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 1053 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1054 | if (group_list != NULL) { |
| 1055 | mbedtls_ssl_conf_groups(&(ep->conf), group_list); |
| 1056 | } |
Andrzej Kurek | 74394a5 | 2022-03-08 06:50:12 -0500 | [diff] [blame] | 1057 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1058 | mbedtls_ssl_conf_authmode(&(ep->conf), MBEDTLS_SSL_VERIFY_REQUIRED); |
Ronald Cron | bdddaef | 2022-06-07 10:34:59 +0200 | [diff] [blame] | 1059 | |
Andrzej Kurek | ed58b50 | 2022-06-10 19:24:05 -0400 | [diff] [blame] | 1060 | #if defined(MBEDTLS_SSL_CACHE_C) && defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1061 | if (endpoint_type == MBEDTLS_SSL_IS_SERVER && options->cache != NULL) { |
| 1062 | mbedtls_ssl_conf_session_cache(&(ep->conf), options->cache, |
| 1063 | mbedtls_ssl_cache_get, |
| 1064 | mbedtls_ssl_cache_set); |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 1065 | } |
| 1066 | #endif |
| 1067 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1068 | ret = mbedtls_ssl_setup(&(ep->ssl), &(ep->conf)); |
| 1069 | TEST_ASSERT(ret == 0); |
Andrzej Kurek | 15daf50 | 2020-02-12 09:17:52 -0500 | [diff] [blame] | 1070 | |
| 1071 | #if defined(MBEDTLS_SSL_PROTO_DTLS) && defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1072 | if (endpoint_type == MBEDTLS_SSL_IS_SERVER && dtls_context != NULL) { |
| 1073 | mbedtls_ssl_conf_dtls_cookies(&(ep->conf), NULL, NULL, NULL); |
| 1074 | } |
Andrzej Kurek | 15daf50 | 2020-02-12 09:17:52 -0500 | [diff] [blame] | 1075 | #endif |
| 1076 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1077 | ret = mbedtls_endpoint_certificate_init(ep, options->pk_alg, |
| 1078 | options->opaque_alg, |
| 1079 | options->opaque_alg2, |
| 1080 | options->opaque_usage); |
| 1081 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 1082 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1083 | TEST_EQUAL(mbedtls_ssl_conf_get_user_data_n(&ep->conf), user_data_n); |
| 1084 | mbedtls_ssl_conf_set_user_data_p(&ep->conf, ep); |
| 1085 | TEST_EQUAL(mbedtls_ssl_get_user_data_n(&ep->ssl), user_data_n); |
| 1086 | mbedtls_ssl_set_user_data_p(&ep->ssl, ep); |
Gilles Peskine | 80dae04 | 2022-01-21 23:50:39 +0100 | [diff] [blame] | 1087 | |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 1088 | exit: |
| 1089 | return ret; |
| 1090 | } |
| 1091 | |
| 1092 | /* |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 1093 | * Deinitializes endpoint represented by \p ep. |
| 1094 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1095 | void mbedtls_endpoint_free(mbedtls_endpoint *ep, |
| 1096 | mbedtls_test_message_socket_context *context) |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 1097 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1098 | mbedtls_endpoint_certificate_free(ep); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 1099 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1100 | mbedtls_ssl_free(&(ep->ssl)); |
| 1101 | mbedtls_ssl_config_free(&(ep->conf)); |
Andrzej Kurek | 15daf50 | 2020-02-12 09:17:52 -0500 | [diff] [blame] | 1102 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1103 | if (context != NULL) { |
| 1104 | mbedtls_message_socket_close(context); |
| 1105 | } else { |
| 1106 | mbedtls_mock_socket_close(&(ep->socket)); |
Andrzej Kurek | 15daf50 | 2020-02-12 09:17:52 -0500 | [diff] [blame] | 1107 | } |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 1108 | } |
| 1109 | |
| 1110 | /* |
| 1111 | * This function moves ssl handshake from \p ssl to prescribed \p state. |
| 1112 | * /p second_ssl is used as second endpoint and their sockets have to be |
| 1113 | * connected before calling this function. |
| 1114 | * |
| 1115 | * \retval 0 on success, otherwise error code. |
| 1116 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1117 | int mbedtls_move_handshake_to_state(mbedtls_ssl_context *ssl, |
| 1118 | mbedtls_ssl_context *second_ssl, |
| 1119 | int state) |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 1120 | { |
| 1121 | enum { BUFFSIZE = 1024 }; |
| 1122 | int max_steps = 1000; |
| 1123 | int ret = 0; |
| 1124 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1125 | if (ssl == NULL || second_ssl == NULL) { |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 1126 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 1127 | } |
| 1128 | |
| 1129 | /* Perform communication via connected sockets */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1130 | while ((ssl->state != state) && (--max_steps >= 0)) { |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 1131 | /* If /p second_ssl ends the handshake procedure before /p ssl then |
| 1132 | * there is no need to call the next step */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1133 | if (!mbedtls_ssl_is_handshake_over(second_ssl)) { |
| 1134 | ret = mbedtls_ssl_handshake_step(second_ssl); |
| 1135 | if (ret != 0 && ret != MBEDTLS_ERR_SSL_WANT_READ && |
| 1136 | ret != MBEDTLS_ERR_SSL_WANT_WRITE) { |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 1137 | return ret; |
| 1138 | } |
| 1139 | } |
| 1140 | |
| 1141 | /* We only care about the \p ssl state and returns, so we call it last, |
| 1142 | * to leave the iteration as soon as the state is as expected. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1143 | ret = mbedtls_ssl_handshake_step(ssl); |
| 1144 | if (ret != 0 && ret != MBEDTLS_ERR_SSL_WANT_READ && |
| 1145 | ret != MBEDTLS_ERR_SSL_WANT_WRITE) { |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 1146 | return ret; |
| 1147 | } |
| 1148 | } |
| 1149 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1150 | return (max_steps >= 0) ? ret : -1; |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 1151 | } |
| 1152 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 1153 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 1154 | |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 1155 | /* |
Piotr Nowicki | 438bf3b | 2020-03-10 12:59:10 +0100 | [diff] [blame] | 1156 | * Write application data. Increase write counter if necessary. |
Piotr Nowicki | c3fca5e | 2020-01-30 15:33:42 +0100 | [diff] [blame] | 1157 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1158 | int mbedtls_ssl_write_fragment(mbedtls_ssl_context *ssl, unsigned char *buf, |
| 1159 | int buf_len, int *written, |
| 1160 | const int expected_fragments) |
Piotr Nowicki | c3fca5e | 2020-01-30 15:33:42 +0100 | [diff] [blame] | 1161 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1162 | int ret = mbedtls_ssl_write(ssl, buf + *written, buf_len - *written); |
| 1163 | if (ret > 0) { |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1164 | *written += ret; |
Piotr Nowicki | c3fca5e | 2020-01-30 15:33:42 +0100 | [diff] [blame] | 1165 | } |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1166 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1167 | if (expected_fragments == 0) { |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1168 | /* Used for DTLS and the message size larger than MFL. In that case |
| 1169 | * the message can not be fragmented and the library should return |
| 1170 | * MBEDTLS_ERR_SSL_BAD_INPUT_DATA error. This error must be returned |
| 1171 | * to prevent a dead loop inside mbedtls_exchange_data(). */ |
| 1172 | return ret; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1173 | } else if (expected_fragments == 1) { |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1174 | /* Used for TLS/DTLS and the message size lower than MFL */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1175 | TEST_ASSERT(ret == buf_len || |
| 1176 | ret == MBEDTLS_ERR_SSL_WANT_READ || |
| 1177 | ret == MBEDTLS_ERR_SSL_WANT_WRITE); |
| 1178 | } else { |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1179 | /* Used for TLS and the message size larger than MFL */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1180 | TEST_ASSERT(expected_fragments > 1); |
| 1181 | TEST_ASSERT((ret >= 0 && ret <= buf_len) || |
| 1182 | ret == MBEDTLS_ERR_SSL_WANT_READ || |
| 1183 | ret == MBEDTLS_ERR_SSL_WANT_WRITE); |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1184 | } |
| 1185 | |
| 1186 | return 0; |
| 1187 | |
| 1188 | exit: |
| 1189 | /* Some of the tests failed */ |
| 1190 | return -1; |
Piotr Nowicki | c3fca5e | 2020-01-30 15:33:42 +0100 | [diff] [blame] | 1191 | } |
| 1192 | |
| 1193 | /* |
Piotr Nowicki | 438bf3b | 2020-03-10 12:59:10 +0100 | [diff] [blame] | 1194 | * Read application data and increase read counter and fragments counter if necessary. |
Piotr Nowicki | c3fca5e | 2020-01-30 15:33:42 +0100 | [diff] [blame] | 1195 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1196 | int mbedtls_ssl_read_fragment(mbedtls_ssl_context *ssl, unsigned char *buf, |
| 1197 | int buf_len, int *read, |
| 1198 | int *fragments, const int expected_fragments) |
Piotr Nowicki | c3fca5e | 2020-01-30 15:33:42 +0100 | [diff] [blame] | 1199 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1200 | int ret = mbedtls_ssl_read(ssl, buf + *read, buf_len - *read); |
| 1201 | if (ret > 0) { |
| 1202 | (*fragments)++; |
Piotr Nowicki | c3fca5e | 2020-01-30 15:33:42 +0100 | [diff] [blame] | 1203 | *read += ret; |
| 1204 | } |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1205 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1206 | if (expected_fragments == 0) { |
| 1207 | TEST_ASSERT(ret == 0); |
| 1208 | } else if (expected_fragments == 1) { |
| 1209 | TEST_ASSERT(ret == buf_len || |
| 1210 | ret == MBEDTLS_ERR_SSL_WANT_READ || |
| 1211 | ret == MBEDTLS_ERR_SSL_WANT_WRITE); |
| 1212 | } else { |
| 1213 | TEST_ASSERT(expected_fragments > 1); |
| 1214 | TEST_ASSERT((ret >= 0 && ret <= buf_len) || |
| 1215 | ret == MBEDTLS_ERR_SSL_WANT_READ || |
| 1216 | ret == MBEDTLS_ERR_SSL_WANT_WRITE); |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1217 | } |
| 1218 | |
| 1219 | return 0; |
| 1220 | |
| 1221 | exit: |
| 1222 | /* Some of the tests failed */ |
| 1223 | return -1; |
Piotr Nowicki | c3fca5e | 2020-01-30 15:33:42 +0100 | [diff] [blame] | 1224 | } |
| 1225 | |
| 1226 | /* |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1227 | * Helper function setting up inverse record transformations |
| 1228 | * using given cipher, hash, EtM mode, authentication tag length, |
| 1229 | * and version. |
| 1230 | */ |
| 1231 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1232 | #define CHK(x) \ |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1233 | do \ |
| 1234 | { \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1235 | if (!(x)) \ |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 1236 | { \ |
Hanno Becker | a5780f1 | 2019-04-05 09:55:37 +0100 | [diff] [blame] | 1237 | ret = -1; \ |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 1238 | goto cleanup; \ |
| 1239 | } \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1240 | } while (0) |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1241 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1242 | void set_ciphersuite(mbedtls_ssl_config *conf, const char *cipher, |
| 1243 | int *forced_ciphersuite) |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 1244 | { |
| 1245 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1246 | forced_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id(cipher); |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 1247 | forced_ciphersuite[1] = 0; |
| 1248 | |
| 1249 | ciphersuite_info = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1250 | mbedtls_ssl_ciphersuite_from_id(forced_ciphersuite[0]); |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 1251 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1252 | TEST_ASSERT(ciphersuite_info != NULL); |
| 1253 | TEST_ASSERT(ciphersuite_info->min_tls_version <= conf->max_tls_version); |
| 1254 | TEST_ASSERT(ciphersuite_info->max_tls_version >= conf->min_tls_version); |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 1255 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1256 | if (conf->max_tls_version > ciphersuite_info->max_tls_version) { |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 1257 | conf->max_tls_version = ciphersuite_info->max_tls_version; |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 1258 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1259 | if (conf->min_tls_version < ciphersuite_info->min_tls_version) { |
Glenn Strauss | 60bfe60 | 2022-03-14 19:04:24 -0400 | [diff] [blame] | 1260 | conf->min_tls_version = ciphersuite_info->min_tls_version; |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 1261 | } |
| 1262 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1263 | mbedtls_ssl_conf_ciphersuites(conf, forced_ciphersuite); |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 1264 | |
| 1265 | exit: |
| 1266 | return; |
| 1267 | } |
| 1268 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1269 | int psk_dummy_callback(void *p_info, mbedtls_ssl_context *ssl, |
| 1270 | const unsigned char *name, size_t name_len) |
Andrzej Kurek | cc5169c | 2020-02-04 09:04:56 -0500 | [diff] [blame] | 1271 | { |
| 1272 | (void) p_info; |
| 1273 | (void) ssl; |
| 1274 | (void) name; |
| 1275 | (void) name_len; |
| 1276 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1277 | return 0; |
Andrzej Kurek | cc5169c | 2020-02-04 09:04:56 -0500 | [diff] [blame] | 1278 | } |
| 1279 | |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 1280 | #if MBEDTLS_SSL_CID_OUT_LEN_MAX > MBEDTLS_SSL_CID_IN_LEN_MAX |
| 1281 | #define SSL_CID_LEN_MIN MBEDTLS_SSL_CID_IN_LEN_MAX |
| 1282 | #else |
| 1283 | #define SSL_CID_LEN_MIN MBEDTLS_SSL_CID_OUT_LEN_MAX |
| 1284 | #endif |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1285 | |
Gilles Peskine | 9c656ec | 2022-02-26 19:55:58 +0100 | [diff] [blame] | 1286 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \ |
| 1287 | defined(MBEDTLS_CIPHER_MODE_CBC) && defined(MBEDTLS_AES_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1288 | static int psa_cipher_encrypt_helper(mbedtls_ssl_transform *transform, |
| 1289 | const unsigned char *iv, size_t iv_len, |
| 1290 | const unsigned char *input, size_t ilen, |
| 1291 | unsigned char *output, size_t *olen) |
Przemyslaw Stekiel | f4facef | 2022-02-02 21:31:04 +0100 | [diff] [blame] | 1292 | { |
Przemyslaw Stekiel | 5648d57 | 2022-02-03 14:09:02 +0100 | [diff] [blame] | 1293 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Przemyslaw Stekiel | d66387f | 2022-02-03 08:55:33 +0100 | [diff] [blame] | 1294 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Przemyslaw Stekiel | f4facef | 2022-02-02 21:31:04 +0100 | [diff] [blame] | 1295 | psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT; |
| 1296 | size_t part_len; |
| 1297 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1298 | status = psa_cipher_encrypt_setup(&cipher_op, |
| 1299 | transform->psa_key_enc, transform->psa_alg); |
Przemyslaw Stekiel | f4facef | 2022-02-02 21:31:04 +0100 | [diff] [blame] | 1300 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1301 | if (status != PSA_SUCCESS) { |
| 1302 | return psa_ssl_status_to_mbedtls(status); |
| 1303 | } |
Przemyslaw Stekiel | f4facef | 2022-02-02 21:31:04 +0100 | [diff] [blame] | 1304 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1305 | status = psa_cipher_set_iv(&cipher_op, iv, iv_len); |
Przemyslaw Stekiel | f4facef | 2022-02-02 21:31:04 +0100 | [diff] [blame] | 1306 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1307 | if (status != PSA_SUCCESS) { |
| 1308 | return psa_ssl_status_to_mbedtls(status); |
| 1309 | } |
Przemyslaw Stekiel | f4facef | 2022-02-02 21:31:04 +0100 | [diff] [blame] | 1310 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1311 | status = psa_cipher_update(&cipher_op, |
| 1312 | input, ilen, output, ilen, olen); |
Przemyslaw Stekiel | f4facef | 2022-02-02 21:31:04 +0100 | [diff] [blame] | 1313 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1314 | if (status != PSA_SUCCESS) { |
| 1315 | return psa_ssl_status_to_mbedtls(status); |
| 1316 | } |
Przemyslaw Stekiel | f4facef | 2022-02-02 21:31:04 +0100 | [diff] [blame] | 1317 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1318 | status = psa_cipher_finish(&cipher_op, |
| 1319 | output + *olen, ilen - *olen, &part_len); |
Przemyslaw Stekiel | f4facef | 2022-02-02 21:31:04 +0100 | [diff] [blame] | 1320 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1321 | if (status != PSA_SUCCESS) { |
| 1322 | return psa_ssl_status_to_mbedtls(status); |
| 1323 | } |
Przemyslaw Stekiel | f4facef | 2022-02-02 21:31:04 +0100 | [diff] [blame] | 1324 | |
| 1325 | *olen += part_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1326 | return 0; |
Przemyslaw Stekiel | 5648d57 | 2022-02-03 14:09:02 +0100 | [diff] [blame] | 1327 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1328 | return mbedtls_cipher_crypt(&transform->cipher_ctx_enc, |
| 1329 | iv, iv_len, input, ilen, output, olen); |
Przemyslaw Stekiel | f4facef | 2022-02-02 21:31:04 +0100 | [diff] [blame] | 1330 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Przemyslaw Stekiel | 5648d57 | 2022-02-03 14:09:02 +0100 | [diff] [blame] | 1331 | } |
Gilles Peskine | 9c656ec | 2022-02-26 19:55:58 +0100 | [diff] [blame] | 1332 | #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] | 1333 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1334 | static int build_transforms(mbedtls_ssl_transform *t_in, |
| 1335 | mbedtls_ssl_transform *t_out, |
| 1336 | int cipher_type, int hash_id, |
| 1337 | int etm, int tag_mode, |
| 1338 | mbedtls_ssl_protocol_version tls_version, |
| 1339 | size_t cid0_len, |
| 1340 | size_t cid1_len) |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1341 | { |
| 1342 | mbedtls_cipher_info_t const *cipher_info; |
Hanno Becker | a5780f1 | 2019-04-05 09:55:37 +0100 | [diff] [blame] | 1343 | int ret = 0; |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1344 | |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1345 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1346 | psa_key_type_t key_type; |
| 1347 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1348 | psa_algorithm_t alg; |
| 1349 | size_t key_bits; |
Przemyslaw Stekiel | d66387f | 2022-02-03 08:55:33 +0100 | [diff] [blame] | 1350 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1351 | #endif |
| 1352 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1353 | size_t keylen, maclen, ivlen; |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 1354 | unsigned char *key0 = NULL, *key1 = NULL; |
Paul Elliott | 6f1eda7 | 2020-06-11 20:22:00 +0100 | [diff] [blame] | 1355 | unsigned char *md0 = NULL, *md1 = NULL; |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1356 | unsigned char iv_enc[16], iv_dec[16]; |
| 1357 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1358 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1359 | unsigned char cid0[SSL_CID_LEN_MIN]; |
| 1360 | unsigned char cid1[SSL_CID_LEN_MIN]; |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 1361 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1362 | mbedtls_test_rnd_std_rand(NULL, cid0, sizeof(cid0)); |
| 1363 | mbedtls_test_rnd_std_rand(NULL, cid1, sizeof(cid1)); |
Hanno Becker | 43c24b8 | 2019-05-01 09:45:57 +0100 | [diff] [blame] | 1364 | #else |
| 1365 | ((void) cid0_len); |
| 1366 | ((void) cid1_len); |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1367 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 1368 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1369 | maclen = 0; |
| 1370 | |
| 1371 | /* Pick cipher */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1372 | cipher_info = mbedtls_cipher_info_from_type(cipher_type); |
| 1373 | CHK(cipher_info != NULL); |
| 1374 | CHK(cipher_info->iv_size <= 16); |
| 1375 | CHK(cipher_info->key_bitlen % 8 == 0); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1376 | |
| 1377 | /* Pick keys */ |
| 1378 | keylen = cipher_info->key_bitlen / 8; |
Hanno Becker | 78d1f70 | 2019-04-05 09:56:10 +0100 | [diff] [blame] | 1379 | /* Allocate `keylen + 1` bytes to ensure that we get |
| 1380 | * a non-NULL pointers from `mbedtls_calloc` even if |
| 1381 | * `keylen == 0` in the case of the NULL cipher. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1382 | CHK((key0 = mbedtls_calloc(1, keylen + 1)) != NULL); |
| 1383 | CHK((key1 = mbedtls_calloc(1, keylen + 1)) != NULL); |
| 1384 | memset(key0, 0x1, keylen); |
| 1385 | memset(key1, 0x2, keylen); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1386 | |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1387 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1388 | /* Setup cipher contexts */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1389 | CHK(mbedtls_cipher_setup(&t_in->cipher_ctx_enc, cipher_info) == 0); |
| 1390 | CHK(mbedtls_cipher_setup(&t_in->cipher_ctx_dec, cipher_info) == 0); |
| 1391 | CHK(mbedtls_cipher_setup(&t_out->cipher_ctx_enc, cipher_info) == 0); |
| 1392 | CHK(mbedtls_cipher_setup(&t_out->cipher_ctx_dec, cipher_info) == 0); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1393 | |
| 1394 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1395 | if (cipher_info->mode == MBEDTLS_MODE_CBC) { |
| 1396 | CHK(mbedtls_cipher_set_padding_mode(&t_in->cipher_ctx_enc, |
| 1397 | MBEDTLS_PADDING_NONE) == 0); |
| 1398 | CHK(mbedtls_cipher_set_padding_mode(&t_in->cipher_ctx_dec, |
| 1399 | MBEDTLS_PADDING_NONE) == 0); |
| 1400 | CHK(mbedtls_cipher_set_padding_mode(&t_out->cipher_ctx_enc, |
| 1401 | MBEDTLS_PADDING_NONE) == 0); |
| 1402 | CHK(mbedtls_cipher_set_padding_mode(&t_out->cipher_ctx_dec, |
| 1403 | MBEDTLS_PADDING_NONE) == 0); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1404 | } |
| 1405 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
| 1406 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1407 | CHK(mbedtls_cipher_setkey(&t_in->cipher_ctx_enc, key0, |
| 1408 | keylen << 3, MBEDTLS_ENCRYPT) == 0); |
| 1409 | CHK(mbedtls_cipher_setkey(&t_in->cipher_ctx_dec, key1, |
| 1410 | keylen << 3, MBEDTLS_DECRYPT) == 0); |
| 1411 | CHK(mbedtls_cipher_setkey(&t_out->cipher_ctx_enc, key1, |
| 1412 | keylen << 3, MBEDTLS_ENCRYPT) == 0); |
| 1413 | CHK(mbedtls_cipher_setkey(&t_out->cipher_ctx_dec, key0, |
| 1414 | keylen << 3, MBEDTLS_DECRYPT) == 0); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1415 | #endif |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1416 | |
| 1417 | /* Setup MAC contexts */ |
Hanno Becker | fd86ca8 | 2020-11-30 08:54:23 +0000 | [diff] [blame] | 1418 | #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1419 | if (cipher_info->mode == MBEDTLS_MODE_CBC || |
| 1420 | cipher_info->mode == MBEDTLS_MODE_STREAM) { |
Andrzej Kurek | 21b6870 | 2022-08-17 16:26:12 -0400 | [diff] [blame] | 1421 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1422 | mbedtls_md_info_t const *md_info = mbedtls_md_info_from_type(hash_id); |
| 1423 | CHK(md_info != NULL); |
Andrzej Kurek | 21b6870 | 2022-08-17 16:26:12 -0400 | [diff] [blame] | 1424 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1425 | maclen = mbedtls_hash_info_get_size(hash_id); |
| 1426 | CHK(maclen != 0); |
Przemek Stekiel | cc59c99 | 2022-08-01 11:51:22 +0200 | [diff] [blame] | 1427 | /* Pick hash keys */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1428 | CHK((md0 = mbedtls_calloc(1, maclen)) != NULL); |
| 1429 | CHK((md1 = mbedtls_calloc(1, maclen)) != NULL); |
| 1430 | memset(md0, 0x5, maclen); |
| 1431 | memset(md1, 0x6, maclen); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1432 | |
Neil Armstrong | f4cc062 | 2022-02-23 16:21:01 +0100 | [diff] [blame] | 1433 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1434 | alg = mbedtls_hash_info_psa_from_md(hash_id); |
Neil Armstrong | f4cc062 | 2022-02-23 16:21:01 +0100 | [diff] [blame] | 1435 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1436 | CHK(alg != 0); |
Neil Armstrong | f4cc062 | 2022-02-23 16:21:01 +0100 | [diff] [blame] | 1437 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1438 | t_out->psa_mac_alg = PSA_ALG_HMAC(alg); |
| 1439 | t_in->psa_mac_alg = PSA_ALG_HMAC(alg); |
Neil Armstrong | f4cc062 | 2022-02-23 16:21:01 +0100 | [diff] [blame] | 1440 | t_in->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT; |
| 1441 | t_out->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT; |
| 1442 | t_in->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT; |
| 1443 | t_out->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT; |
| 1444 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1445 | psa_reset_key_attributes(&attributes); |
| 1446 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE); |
| 1447 | psa_set_key_algorithm(&attributes, PSA_ALG_HMAC(alg)); |
| 1448 | psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC); |
Neil Armstrong | f4cc062 | 2022-02-23 16:21:01 +0100 | [diff] [blame] | 1449 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1450 | CHK(psa_import_key(&attributes, |
| 1451 | md0, maclen, |
| 1452 | &t_in->psa_mac_enc) == PSA_SUCCESS); |
Neil Armstrong | f4cc062 | 2022-02-23 16:21:01 +0100 | [diff] [blame] | 1453 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1454 | CHK(psa_import_key(&attributes, |
| 1455 | md1, maclen, |
| 1456 | &t_out->psa_mac_enc) == PSA_SUCCESS); |
Neil Armstrong | f4cc062 | 2022-02-23 16:21:01 +0100 | [diff] [blame] | 1457 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1458 | if (cipher_info->mode == MBEDTLS_MODE_STREAM || |
| 1459 | etm == MBEDTLS_SSL_ETM_DISABLED) { |
Neil Armstrong | 8f92bf3 | 2022-03-18 09:56:57 +0100 | [diff] [blame] | 1460 | /* mbedtls_ct_hmac() requires the key to be exportable */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1461 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT | |
| 1462 | PSA_KEY_USAGE_VERIFY_HASH); |
| 1463 | } else { |
| 1464 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
| 1465 | } |
Neil Armstrong | f4cc062 | 2022-02-23 16:21:01 +0100 | [diff] [blame] | 1466 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1467 | CHK(psa_import_key(&attributes, |
| 1468 | md1, maclen, |
| 1469 | &t_in->psa_mac_dec) == PSA_SUCCESS); |
Neil Armstrong | f4cc062 | 2022-02-23 16:21:01 +0100 | [diff] [blame] | 1470 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1471 | CHK(psa_import_key(&attributes, |
| 1472 | md0, maclen, |
| 1473 | &t_out->psa_mac_dec) == PSA_SUCCESS); |
Neil Armstrong | cf8841a | 2022-02-24 11:17:45 +0100 | [diff] [blame] | 1474 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1475 | CHK(mbedtls_md_setup(&t_out->md_ctx_enc, md_info, 1) == 0); |
| 1476 | CHK(mbedtls_md_setup(&t_out->md_ctx_dec, md_info, 1) == 0); |
| 1477 | CHK(mbedtls_md_setup(&t_in->md_ctx_enc, md_info, 1) == 0); |
| 1478 | 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] | 1479 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1480 | CHK(mbedtls_md_hmac_starts(&t_in->md_ctx_enc, |
| 1481 | md0, maclen) == 0); |
| 1482 | CHK(mbedtls_md_hmac_starts(&t_in->md_ctx_dec, |
| 1483 | md1, maclen) == 0); |
| 1484 | CHK(mbedtls_md_hmac_starts(&t_out->md_ctx_enc, |
| 1485 | md1, maclen) == 0); |
| 1486 | CHK(mbedtls_md_hmac_starts(&t_out->md_ctx_dec, |
| 1487 | md0, maclen) == 0); |
Neil Armstrong | cf8841a | 2022-02-24 11:17:45 +0100 | [diff] [blame] | 1488 | #endif |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1489 | } |
| 1490 | #else |
| 1491 | ((void) hash_id); |
Hanno Becker | fd86ca8 | 2020-11-30 08:54:23 +0000 | [diff] [blame] | 1492 | #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */ |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1493 | |
| 1494 | |
| 1495 | /* Pick IV's (regardless of whether they |
| 1496 | * are being used by the transform). */ |
| 1497 | ivlen = cipher_info->iv_size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1498 | memset(iv_enc, 0x3, sizeof(iv_enc)); |
| 1499 | memset(iv_dec, 0x4, sizeof(iv_dec)); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1500 | |
| 1501 | /* |
| 1502 | * Setup transforms |
| 1503 | */ |
| 1504 | |
Jaeden Amero | 2de07f1 | 2019-06-05 13:32:08 +0100 | [diff] [blame] | 1505 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \ |
Hanno Becker | fd86ca8 | 2020-11-30 08:54:23 +0000 | [diff] [blame] | 1506 | defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1507 | t_out->encrypt_then_mac = etm; |
| 1508 | t_in->encrypt_then_mac = etm; |
| 1509 | #else |
| 1510 | ((void) etm); |
| 1511 | #endif |
| 1512 | |
Glenn Strauss | 07c6416 | 2022-03-14 12:34:51 -0400 | [diff] [blame] | 1513 | t_out->tls_version = tls_version; |
| 1514 | t_in->tls_version = tls_version; |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1515 | t_out->ivlen = ivlen; |
| 1516 | t_in->ivlen = ivlen; |
| 1517 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1518 | switch (cipher_info->mode) { |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1519 | case MBEDTLS_MODE_GCM: |
| 1520 | case MBEDTLS_MODE_CCM: |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1521 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1522 | if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
Hanno Becker | e683287 | 2020-05-28 08:29:58 +0100 | [diff] [blame] | 1523 | t_out->fixed_ivlen = 12; |
| 1524 | t_in->fixed_ivlen = 12; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1525 | } else |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1526 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | e683287 | 2020-05-28 08:29:58 +0100 | [diff] [blame] | 1527 | { |
| 1528 | t_out->fixed_ivlen = 4; |
| 1529 | t_in->fixed_ivlen = 4; |
| 1530 | } |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1531 | t_out->maclen = 0; |
| 1532 | t_in->maclen = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1533 | switch (tag_mode) { |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1534 | case 0: /* Full tag */ |
| 1535 | t_out->taglen = 16; |
| 1536 | t_in->taglen = 16; |
| 1537 | break; |
| 1538 | case 1: /* Partial tag */ |
| 1539 | t_out->taglen = 8; |
| 1540 | t_in->taglen = 8; |
| 1541 | break; |
| 1542 | default: |
Paul Elliott | c7b5374 | 2021-02-03 13:18:33 +0000 | [diff] [blame] | 1543 | ret = 1; |
| 1544 | goto cleanup; |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1545 | } |
| 1546 | break; |
| 1547 | |
| 1548 | case MBEDTLS_MODE_CHACHAPOLY: |
| 1549 | t_out->fixed_ivlen = 12; |
| 1550 | t_in->fixed_ivlen = 12; |
| 1551 | t_out->maclen = 0; |
| 1552 | t_in->maclen = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1553 | switch (tag_mode) { |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1554 | case 0: /* Full tag */ |
| 1555 | t_out->taglen = 16; |
| 1556 | t_in->taglen = 16; |
| 1557 | break; |
| 1558 | case 1: /* Partial tag */ |
| 1559 | t_out->taglen = 8; |
| 1560 | t_in->taglen = 8; |
| 1561 | break; |
| 1562 | default: |
Paul Elliott | c7b5374 | 2021-02-03 13:18:33 +0000 | [diff] [blame] | 1563 | ret = 1; |
| 1564 | goto cleanup; |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1565 | } |
| 1566 | break; |
| 1567 | |
| 1568 | case MBEDTLS_MODE_STREAM: |
| 1569 | case MBEDTLS_MODE_CBC: |
| 1570 | t_out->fixed_ivlen = 0; /* redundant, must be 0 */ |
| 1571 | t_in->fixed_ivlen = 0; /* redundant, must be 0 */ |
| 1572 | t_out->taglen = 0; |
| 1573 | t_in->taglen = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1574 | switch (tag_mode) { |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1575 | case 0: /* Full tag */ |
| 1576 | t_out->maclen = maclen; |
| 1577 | t_in->maclen = maclen; |
| 1578 | break; |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1579 | default: |
Paul Elliott | c7b5374 | 2021-02-03 13:18:33 +0000 | [diff] [blame] | 1580 | ret = 1; |
| 1581 | goto cleanup; |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1582 | } |
| 1583 | break; |
| 1584 | default: |
Paul Elliott | c7b5374 | 2021-02-03 13:18:33 +0000 | [diff] [blame] | 1585 | ret = 1; |
| 1586 | goto cleanup; |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1587 | break; |
| 1588 | } |
| 1589 | |
| 1590 | /* Setup IV's */ |
| 1591 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1592 | memcpy(&t_in->iv_dec, iv_dec, sizeof(iv_dec)); |
| 1593 | memcpy(&t_in->iv_enc, iv_enc, sizeof(iv_enc)); |
| 1594 | memcpy(&t_out->iv_dec, iv_enc, sizeof(iv_enc)); |
| 1595 | memcpy(&t_out->iv_enc, iv_dec, sizeof(iv_dec)); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1596 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1597 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 1598 | /* Add CID */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1599 | memcpy(&t_in->in_cid, cid0, cid0_len); |
| 1600 | memcpy(&t_in->out_cid, cid1, cid1_len); |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 1601 | t_in->in_cid_len = cid0_len; |
| 1602 | t_in->out_cid_len = cid1_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1603 | memcpy(&t_out->in_cid, cid1, cid1_len); |
| 1604 | memcpy(&t_out->out_cid, cid0, cid0_len); |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 1605 | t_out->in_cid_len = cid1_len; |
| 1606 | t_out->out_cid_len = cid0_len; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1607 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 1608 | |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1609 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1610 | status = mbedtls_ssl_cipher_to_psa(cipher_type, |
| 1611 | t_in->taglen, |
| 1612 | &alg, |
| 1613 | &key_type, |
| 1614 | &key_bits); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1615 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1616 | if (status != PSA_SUCCESS) { |
| 1617 | ret = psa_ssl_status_to_mbedtls(status); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1618 | goto cleanup; |
| 1619 | } |
| 1620 | |
| 1621 | t_in->psa_alg = alg; |
| 1622 | t_out->psa_alg = alg; |
| 1623 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1624 | if (alg != MBEDTLS_SSL_NULL_CIPHER) { |
| 1625 | psa_reset_key_attributes(&attributes); |
| 1626 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
| 1627 | psa_set_key_algorithm(&attributes, alg); |
| 1628 | psa_set_key_type(&attributes, key_type); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1629 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1630 | status = psa_import_key(&attributes, |
| 1631 | key0, |
| 1632 | PSA_BITS_TO_BYTES(key_bits), |
| 1633 | &t_in->psa_key_enc); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1634 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1635 | if (status != PSA_SUCCESS) { |
| 1636 | ret = psa_ssl_status_to_mbedtls(status); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1637 | goto cleanup; |
| 1638 | } |
| 1639 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1640 | status = psa_import_key(&attributes, |
| 1641 | key1, |
| 1642 | PSA_BITS_TO_BYTES(key_bits), |
| 1643 | &t_out->psa_key_enc); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1644 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1645 | if (status != PSA_SUCCESS) { |
| 1646 | ret = psa_ssl_status_to_mbedtls(status); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1647 | goto cleanup; |
| 1648 | } |
| 1649 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1650 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
Przemyslaw Stekiel | f4ca3f0 | 2022-01-25 00:25:59 +0100 | [diff] [blame] | 1651 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1652 | status = psa_import_key(&attributes, |
| 1653 | key1, |
| 1654 | PSA_BITS_TO_BYTES(key_bits), |
| 1655 | &t_in->psa_key_dec); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1656 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1657 | if (status != PSA_SUCCESS) { |
| 1658 | ret = psa_ssl_status_to_mbedtls(status); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1659 | goto cleanup; |
| 1660 | } |
| 1661 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1662 | status = psa_import_key(&attributes, |
| 1663 | key0, |
| 1664 | PSA_BITS_TO_BYTES(key_bits), |
| 1665 | &t_out->psa_key_dec); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1666 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1667 | if (status != PSA_SUCCESS) { |
| 1668 | ret = psa_ssl_status_to_mbedtls(status); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1669 | goto cleanup; |
| 1670 | } |
| 1671 | } |
| 1672 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 1673 | |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 1674 | cleanup: |
| 1675 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1676 | mbedtls_free(key0); |
| 1677 | mbedtls_free(key1); |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 1678 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1679 | mbedtls_free(md0); |
| 1680 | mbedtls_free(md1); |
Paul Elliott | 6f1eda7 | 2020-06-11 20:22:00 +0100 | [diff] [blame] | 1681 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1682 | return ret; |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1683 | } |
| 1684 | |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1685 | /* |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 1686 | * Populate a session structure for serialization tests. |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1687 | * Choose dummy values, mostly non-0 to distinguish from the init default. |
| 1688 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1689 | static int ssl_tls12_populate_session(mbedtls_ssl_session *session, |
| 1690 | int ticket_len, |
| 1691 | const char *crt_file) |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1692 | { |
| 1693 | #if defined(MBEDTLS_HAVE_TIME) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1694 | session->start = mbedtls_time(NULL) - 42; |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1695 | #endif |
Glenn Strauss | da7851c | 2022-03-14 13:29:48 -0400 | [diff] [blame] | 1696 | session->tls_version = MBEDTLS_SSL_VERSION_TLS1_2; |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1697 | session->ciphersuite = 0xabcd; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1698 | session->id_len = sizeof(session->id); |
| 1699 | memset(session->id, 66, session->id_len); |
| 1700 | memset(session->master, 17, sizeof(session->master)); |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1701 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 1702 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) && defined(MBEDTLS_FS_IO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1703 | if (crt_file != NULL && strlen(crt_file) != 0) { |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 1704 | mbedtls_x509_crt tmp_crt; |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1705 | int ret; |
Manuel Pégourié-Gonnard | 6b84070 | 2019-05-24 09:40:17 +0200 | [diff] [blame] | 1706 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1707 | mbedtls_x509_crt_init(&tmp_crt); |
| 1708 | ret = mbedtls_x509_crt_parse_file(&tmp_crt, crt_file); |
| 1709 | if (ret != 0) { |
| 1710 | return ret; |
| 1711 | } |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 1712 | |
| 1713 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 1714 | /* Move temporary CRT. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1715 | session->peer_cert = mbedtls_calloc(1, sizeof(*session->peer_cert)); |
| 1716 | if (session->peer_cert == NULL) { |
| 1717 | return -1; |
| 1718 | } |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 1719 | *session->peer_cert = tmp_crt; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1720 | memset(&tmp_crt, 0, sizeof(tmp_crt)); |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 1721 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 1722 | /* Calculate digest of temporary CRT. */ |
| 1723 | session->peer_cert_digest = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1724 | mbedtls_calloc(1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN); |
| 1725 | if (session->peer_cert_digest == NULL) { |
| 1726 | return -1; |
| 1727 | } |
Przemek Stekiel | cc59c99 | 2022-08-01 11:51:22 +0200 | [diff] [blame] | 1728 | |
| 1729 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 1730 | psa_algorithm_t psa_alg = mbedtls_hash_info_psa_from_md( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1731 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE); |
Przemek Stekiel | cc59c99 | 2022-08-01 11:51:22 +0200 | [diff] [blame] | 1732 | size_t hash_size = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1733 | psa_status_t status = psa_hash_compute(psa_alg, tmp_crt.raw.p, |
| 1734 | tmp_crt.raw.len, |
| 1735 | session->peer_cert_digest, |
| 1736 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN, |
| 1737 | &hash_size); |
| 1738 | ret = psa_ssl_status_to_mbedtls(status); |
Przemek Stekiel | cc59c99 | 2022-08-01 11:51:22 +0200 | [diff] [blame] | 1739 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1740 | ret = mbedtls_md(mbedtls_md_info_from_type( |
| 1741 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE), |
| 1742 | tmp_crt.raw.p, tmp_crt.raw.len, |
| 1743 | session->peer_cert_digest); |
Przemek Stekiel | cc59c99 | 2022-08-01 11:51:22 +0200 | [diff] [blame] | 1744 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1745 | if (ret != 0) { |
| 1746 | return ret; |
| 1747 | } |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 1748 | session->peer_cert_digest_type = |
| 1749 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE; |
| 1750 | session->peer_cert_digest_len = |
| 1751 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN; |
| 1752 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 1753 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1754 | mbedtls_x509_crt_free(&tmp_crt); |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1755 | } |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 1756 | #else /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED && MBEDTLS_FS_IO */ |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1757 | (void) crt_file; |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 1758 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED && MBEDTLS_FS_IO */ |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1759 | session->verify_result = 0xdeadbeef; |
| 1760 | |
| 1761 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1762 | if (ticket_len != 0) { |
| 1763 | session->ticket = mbedtls_calloc(1, ticket_len); |
| 1764 | if (session->ticket == NULL) { |
| 1765 | return -1; |
| 1766 | } |
| 1767 | memset(session->ticket, 33, ticket_len); |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1768 | } |
| 1769 | session->ticket_len = ticket_len; |
| 1770 | session->ticket_lifetime = 86401; |
| 1771 | #else |
| 1772 | (void) ticket_len; |
| 1773 | #endif |
| 1774 | |
| 1775 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 1776 | session->mfl_code = 1; |
| 1777 | #endif |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1778 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 1779 | session->encrypt_then_mac = 1; |
| 1780 | #endif |
| 1781 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1782 | return 0; |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1783 | } |
| 1784 | |
Jerry Yu | 534ff40 | 2022-07-14 16:43:43 +0800 | [diff] [blame] | 1785 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1786 | static int ssl_tls13_populate_session(mbedtls_ssl_session *session, |
| 1787 | int ticket_len, |
| 1788 | int endpoint_type) |
Jerry Yu | 534ff40 | 2022-07-14 16:43:43 +0800 | [diff] [blame] | 1789 | { |
| 1790 | ((void) ticket_len); |
| 1791 | session->tls_version = MBEDTLS_SSL_VERSION_TLS1_3; |
| 1792 | session->endpoint = endpoint_type == MBEDTLS_SSL_IS_CLIENT ? |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1793 | MBEDTLS_SSL_IS_CLIENT : MBEDTLS_SSL_IS_SERVER; |
Jerry Yu | 534ff40 | 2022-07-14 16:43:43 +0800 | [diff] [blame] | 1794 | session->ciphersuite = 0xabcd; |
| 1795 | session->ticket_age_add = 0x87654321; |
| 1796 | session->ticket_flags = 0x7; |
| 1797 | |
Jerry Yu | bc7c1a4 | 2022-07-21 22:57:37 +0800 | [diff] [blame] | 1798 | session->resumption_key_len = 32; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1799 | memset(session->resumption_key, 0x99, sizeof(session->resumption_key)); |
Jerry Yu | 534ff40 | 2022-07-14 16:43:43 +0800 | [diff] [blame] | 1800 | |
| 1801 | #if defined(MBEDTLS_HAVE_TIME) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1802 | if (session->endpoint == MBEDTLS_SSL_IS_SERVER) { |
| 1803 | session->start = mbedtls_time(NULL) - 42; |
Jerry Yu | 534ff40 | 2022-07-14 16:43:43 +0800 | [diff] [blame] | 1804 | } |
| 1805 | #endif |
| 1806 | |
| 1807 | #if defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1808 | if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) { |
Jerry Yu | 534ff40 | 2022-07-14 16:43:43 +0800 | [diff] [blame] | 1809 | #if defined(MBEDTLS_HAVE_TIME) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1810 | session->ticket_received = mbedtls_time(NULL) - 40; |
Jerry Yu | 534ff40 | 2022-07-14 16:43:43 +0800 | [diff] [blame] | 1811 | #endif |
| 1812 | session->ticket_lifetime = 0xfedcba98; |
| 1813 | |
| 1814 | session->ticket_len = ticket_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1815 | if (ticket_len != 0) { |
| 1816 | session->ticket = mbedtls_calloc(1, ticket_len); |
| 1817 | if (session->ticket == NULL) { |
| 1818 | return -1; |
| 1819 | } |
| 1820 | memset(session->ticket, 33, ticket_len); |
Jerry Yu | 534ff40 | 2022-07-14 16:43:43 +0800 | [diff] [blame] | 1821 | } |
| 1822 | } |
| 1823 | #endif /* MBEDTLS_SSL_CLI_C */ |
| 1824 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1825 | return 0; |
Jerry Yu | 534ff40 | 2022-07-14 16:43:43 +0800 | [diff] [blame] | 1826 | } |
| 1827 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 1828 | |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1829 | /* |
| 1830 | * Perform data exchanging between \p ssl_1 and \p ssl_2 and check if the |
| 1831 | * message was sent in the correct number of fragments. |
| 1832 | * |
| 1833 | * /p ssl_1 and /p ssl_2 Endpoints represented by mbedtls_ssl_context. Both |
| 1834 | * of them must be initialized and connected beforehand. |
| 1835 | * /p msg_len_1 and /p msg_len_2 specify the size of the message to send. |
| 1836 | * /p expected_fragments_1 and /p expected_fragments_2 determine in how many |
| 1837 | * fragments the message should be sent. |
| 1838 | * expected_fragments is 0: can be used for DTLS testing while the message |
| 1839 | * size is larger than MFL. In that case the message |
| 1840 | * cannot be fragmented and sent to the second endpoint. |
| 1841 | * This value can be used for negative tests. |
| 1842 | * expected_fragments is 1: can be used for TLS/DTLS testing while the |
| 1843 | * message size is below MFL |
| 1844 | * expected_fragments > 1: can be used for TLS testing while the message |
| 1845 | * size is larger than MFL |
| 1846 | * |
| 1847 | * \retval 0 on success, otherwise error code. |
| 1848 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1849 | int mbedtls_exchange_data(mbedtls_ssl_context *ssl_1, |
| 1850 | int msg_len_1, const int expected_fragments_1, |
| 1851 | mbedtls_ssl_context *ssl_2, |
| 1852 | int msg_len_2, const int expected_fragments_2) |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1853 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1854 | unsigned char *msg_buf_1 = malloc(msg_len_1); |
| 1855 | unsigned char *msg_buf_2 = malloc(msg_len_2); |
| 1856 | unsigned char *in_buf_1 = malloc(msg_len_2); |
| 1857 | unsigned char *in_buf_2 = malloc(msg_len_1); |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1858 | int msg_type, ret = -1; |
| 1859 | |
| 1860 | /* Perform this test with two message types. At first use a message |
| 1861 | * consisting of only 0x00 for the client and only 0xFF for the server. |
| 1862 | * At the second time use message with generated data */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1863 | for (msg_type = 0; msg_type < 2; msg_type++) { |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1864 | int written_1 = 0; |
| 1865 | int written_2 = 0; |
| 1866 | int read_1 = 0; |
| 1867 | int read_2 = 0; |
| 1868 | int fragments_1 = 0; |
| 1869 | int fragments_2 = 0; |
| 1870 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1871 | if (msg_type == 0) { |
| 1872 | memset(msg_buf_1, 0x00, msg_len_1); |
| 1873 | memset(msg_buf_2, 0xff, msg_len_2); |
| 1874 | } else { |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1875 | int i, j = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1876 | for (i = 0; i < msg_len_1; i++) { |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1877 | msg_buf_1[i] = j++ & 0xFF; |
| 1878 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1879 | for (i = 0; i < msg_len_2; i++) { |
| 1880 | msg_buf_2[i] = (j -= 5) & 0xFF; |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1881 | } |
| 1882 | } |
| 1883 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1884 | while (read_1 < msg_len_2 || read_2 < msg_len_1) { |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1885 | /* ssl_1 sending */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1886 | if (msg_len_1 > written_1) { |
| 1887 | ret = mbedtls_ssl_write_fragment(ssl_1, msg_buf_1, |
| 1888 | msg_len_1, &written_1, |
| 1889 | expected_fragments_1); |
| 1890 | if (expected_fragments_1 == 0) { |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1891 | /* This error is expected when the message is too large and |
| 1892 | * cannot be fragmented */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1893 | TEST_ASSERT(ret == MBEDTLS_ERR_SSL_BAD_INPUT_DATA); |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1894 | msg_len_1 = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1895 | } else { |
| 1896 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1897 | } |
| 1898 | } |
| 1899 | |
| 1900 | /* ssl_2 sending */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1901 | if (msg_len_2 > written_2) { |
| 1902 | ret = mbedtls_ssl_write_fragment(ssl_2, msg_buf_2, |
| 1903 | msg_len_2, &written_2, |
| 1904 | expected_fragments_2); |
| 1905 | if (expected_fragments_2 == 0) { |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1906 | /* This error is expected when the message is too large and |
| 1907 | * cannot be fragmented */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1908 | TEST_ASSERT(ret == MBEDTLS_ERR_SSL_BAD_INPUT_DATA); |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1909 | msg_len_2 = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1910 | } else { |
| 1911 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1912 | } |
| 1913 | } |
| 1914 | |
| 1915 | /* ssl_1 reading */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1916 | if (read_1 < msg_len_2) { |
| 1917 | ret = mbedtls_ssl_read_fragment(ssl_1, in_buf_1, |
| 1918 | msg_len_2, &read_1, |
| 1919 | &fragments_2, |
| 1920 | expected_fragments_2); |
| 1921 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1922 | } |
| 1923 | |
| 1924 | /* ssl_2 reading */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1925 | if (read_2 < msg_len_1) { |
| 1926 | ret = mbedtls_ssl_read_fragment(ssl_2, in_buf_2, |
| 1927 | msg_len_1, &read_2, |
| 1928 | &fragments_1, |
| 1929 | expected_fragments_1); |
| 1930 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1931 | } |
| 1932 | } |
| 1933 | |
| 1934 | ret = -1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1935 | TEST_ASSERT(0 == memcmp(msg_buf_1, in_buf_2, msg_len_1)); |
| 1936 | TEST_ASSERT(0 == memcmp(msg_buf_2, in_buf_1, msg_len_2)); |
| 1937 | TEST_ASSERT(fragments_1 == expected_fragments_1); |
| 1938 | TEST_ASSERT(fragments_2 == expected_fragments_2); |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1939 | } |
| 1940 | |
| 1941 | ret = 0; |
| 1942 | |
| 1943 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1944 | free(msg_buf_1); |
| 1945 | free(in_buf_1); |
| 1946 | free(msg_buf_2); |
| 1947 | free(in_buf_2); |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1948 | |
| 1949 | return ret; |
| 1950 | } |
| 1951 | |
Piotr Nowicki | 95e9eb8 | 2020-02-14 11:33:34 +0100 | [diff] [blame] | 1952 | /* |
| 1953 | * Perform data exchanging between \p ssl_1 and \p ssl_2. Both of endpoints |
| 1954 | * must be initialized and connected beforehand. |
| 1955 | * |
| 1956 | * \retval 0 on success, otherwise error code. |
| 1957 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1958 | int exchange_data(mbedtls_ssl_context *ssl_1, |
| 1959 | mbedtls_ssl_context *ssl_2) |
Piotr Nowicki | 95e9eb8 | 2020-02-14 11:33:34 +0100 | [diff] [blame] | 1960 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1961 | return mbedtls_exchange_data(ssl_1, 256, 1, |
| 1962 | ssl_2, 256, 1); |
Piotr Nowicki | 95e9eb8 | 2020-02-14 11:33:34 +0100 | [diff] [blame] | 1963 | } |
| 1964 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 1965 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1966 | static int check_ssl_version(mbedtls_ssl_protocol_version expected_negotiated_version, |
| 1967 | const mbedtls_ssl_context *ssl) |
Gilles Peskine | 1255b0d | 2022-01-13 01:08:48 +0100 | [diff] [blame] | 1968 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1969 | const char *version_string = mbedtls_ssl_get_version(ssl); |
Gilles Peskine | 1255b0d | 2022-01-13 01:08:48 +0100 | [diff] [blame] | 1970 | mbedtls_ssl_protocol_version version_number = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1971 | mbedtls_ssl_get_version_number(ssl); |
Gilles Peskine | 1255b0d | 2022-01-13 01:08:48 +0100 | [diff] [blame] | 1972 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1973 | TEST_EQUAL(ssl->tls_version, expected_negotiated_version); |
Gilles Peskine | 1255b0d | 2022-01-13 01:08:48 +0100 | [diff] [blame] | 1974 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1975 | if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 1976 | TEST_EQUAL(version_string[0], 'D'); |
Gilles Peskine | 1255b0d | 2022-01-13 01:08:48 +0100 | [diff] [blame] | 1977 | ++version_string; |
| 1978 | } |
| 1979 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1980 | switch (expected_negotiated_version) { |
Glenn Strauss | e3af4cb | 2022-03-15 03:23:42 -0400 | [diff] [blame] | 1981 | case MBEDTLS_SSL_VERSION_TLS1_2: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1982 | TEST_EQUAL(version_number, MBEDTLS_SSL_VERSION_TLS1_2); |
| 1983 | TEST_ASSERT(strcmp(version_string, "TLSv1.2") == 0); |
Gilles Peskine | 1255b0d | 2022-01-13 01:08:48 +0100 | [diff] [blame] | 1984 | break; |
| 1985 | |
Glenn Strauss | e3af4cb | 2022-03-15 03:23:42 -0400 | [diff] [blame] | 1986 | case MBEDTLS_SSL_VERSION_TLS1_3: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1987 | TEST_EQUAL(version_number, MBEDTLS_SSL_VERSION_TLS1_3); |
| 1988 | TEST_ASSERT(strcmp(version_string, "TLSv1.3") == 0); |
Gilles Peskine | 1255b0d | 2022-01-13 01:08:48 +0100 | [diff] [blame] | 1989 | break; |
| 1990 | |
| 1991 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1992 | TEST_ASSERT(!"Version check not implemented for this protocol version"); |
Gilles Peskine | 1255b0d | 2022-01-13 01:08:48 +0100 | [diff] [blame] | 1993 | } |
| 1994 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1995 | return 1; |
Gilles Peskine | 1255b0d | 2022-01-13 01:08:48 +0100 | [diff] [blame] | 1996 | |
| 1997 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1998 | return 0; |
Gilles Peskine | 1255b0d | 2022-01-13 01:08:48 +0100 | [diff] [blame] | 1999 | } |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 2000 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Gilles Peskine | 1255b0d | 2022-01-13 01:08:48 +0100 | [diff] [blame] | 2001 | |
| 2002 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 2003 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2004 | void perform_handshake(handshake_test_options *options) |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2005 | { |
| 2006 | /* forced_ciphersuite needs to last until the end of the handshake */ |
| 2007 | int forced_ciphersuite[2]; |
| 2008 | enum { BUFFSIZE = 17000 }; |
| 2009 | mbedtls_endpoint client, server; |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 2010 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED) |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2011 | const char *psk_identity = "foo"; |
| 2012 | #endif |
| 2013 | #if defined(MBEDTLS_TIMING_C) |
| 2014 | mbedtls_timing_delay_context timer_client, timer_server; |
| 2015 | #endif |
| 2016 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
| 2017 | unsigned char *context_buf = NULL; |
| 2018 | size_t context_buf_len; |
| 2019 | #endif |
| 2020 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 2021 | int ret = -1; |
| 2022 | #endif |
Neil Armstrong | 8c52ed8 | 2022-05-27 13:14:55 +0200 | [diff] [blame] | 2023 | int expected_handshake_result = options->expected_handshake_result; |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2024 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2025 | USE_PSA_INIT(); |
| 2026 | mbedtls_platform_zeroize(&client, sizeof(client)); |
| 2027 | mbedtls_platform_zeroize(&server, sizeof(server)); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2028 | mbedtls_test_message_queue server_queue, client_queue; |
| 2029 | mbedtls_test_message_socket_context server_context, client_context; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2030 | mbedtls_message_socket_init(&server_context); |
| 2031 | mbedtls_message_socket_init(&client_context); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2032 | |
| 2033 | /* Client side */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2034 | if (options->dtls != 0) { |
| 2035 | TEST_ASSERT(mbedtls_endpoint_init(&client, MBEDTLS_SSL_IS_CLIENT, |
| 2036 | options, &client_context, |
| 2037 | &client_queue, |
| 2038 | &server_queue, NULL) == 0); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2039 | #if defined(MBEDTLS_TIMING_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2040 | mbedtls_ssl_set_timer_cb(&client.ssl, &timer_client, |
| 2041 | mbedtls_timing_set_delay, |
| 2042 | mbedtls_timing_get_delay); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2043 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2044 | } else { |
| 2045 | TEST_ASSERT(mbedtls_endpoint_init(&client, MBEDTLS_SSL_IS_CLIENT, |
| 2046 | options, NULL, NULL, |
| 2047 | NULL, NULL) == 0); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2048 | } |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 2049 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2050 | if (options->client_min_version != MBEDTLS_SSL_VERSION_UNKNOWN) { |
| 2051 | mbedtls_ssl_conf_min_tls_version(&client.conf, |
| 2052 | options->client_min_version); |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 2053 | } |
| 2054 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2055 | if (options->client_max_version != MBEDTLS_SSL_VERSION_UNKNOWN) { |
| 2056 | mbedtls_ssl_conf_max_tls_version(&client.conf, |
| 2057 | options->client_max_version); |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 2058 | } |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2059 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2060 | if (strlen(options->cipher) > 0) { |
| 2061 | set_ciphersuite(&client.conf, options->cipher, forced_ciphersuite); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2062 | } |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 2063 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2064 | #if defined(MBEDTLS_DEBUG_C) |
| 2065 | if (options->cli_log_fun) { |
| 2066 | mbedtls_debug_set_threshold(4); |
| 2067 | mbedtls_ssl_conf_dbg(&client.conf, options->cli_log_fun, |
| 2068 | options->cli_log_obj); |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 2069 | } |
| 2070 | #endif |
| 2071 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2072 | /* Server side */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2073 | if (options->dtls != 0) { |
| 2074 | TEST_ASSERT(mbedtls_endpoint_init(&server, MBEDTLS_SSL_IS_SERVER, |
| 2075 | options, &server_context, |
| 2076 | &server_queue, |
| 2077 | &client_queue, NULL) == 0); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2078 | #if defined(MBEDTLS_TIMING_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2079 | mbedtls_ssl_set_timer_cb(&server.ssl, &timer_server, |
| 2080 | mbedtls_timing_set_delay, |
| 2081 | mbedtls_timing_get_delay); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2082 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2083 | } else { |
| 2084 | TEST_ASSERT(mbedtls_endpoint_init(&server, MBEDTLS_SSL_IS_SERVER, |
| 2085 | options, NULL, NULL, NULL, |
| 2086 | NULL) == 0); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2087 | } |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 2088 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2089 | mbedtls_ssl_conf_authmode(&server.conf, options->srv_auth_mode); |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 2090 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2091 | if (options->server_min_version != MBEDTLS_SSL_VERSION_UNKNOWN) { |
| 2092 | mbedtls_ssl_conf_min_tls_version(&server.conf, |
| 2093 | options->server_min_version); |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 2094 | } |
| 2095 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2096 | if (options->server_max_version != MBEDTLS_SSL_VERSION_UNKNOWN) { |
| 2097 | mbedtls_ssl_conf_max_tls_version(&server.conf, |
| 2098 | options->server_max_version); |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 2099 | } |
| 2100 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2101 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2102 | TEST_ASSERT(mbedtls_ssl_conf_max_frag_len(&(server.conf), |
| 2103 | (unsigned char) options->mfl) == 0); |
| 2104 | TEST_ASSERT(mbedtls_ssl_conf_max_frag_len(&(client.conf), |
| 2105 | (unsigned char) options->mfl) == 0); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2106 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2107 | TEST_ASSERT(MBEDTLS_SSL_MAX_FRAG_LEN_NONE == options->mfl); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2108 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 2109 | |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 2110 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2111 | if (options->psk_str != NULL && options->psk_str->len > 0) { |
| 2112 | TEST_ASSERT(mbedtls_ssl_conf_psk(&client.conf, options->psk_str->x, |
| 2113 | options->psk_str->len, |
| 2114 | (const unsigned char *) psk_identity, |
| 2115 | strlen(psk_identity)) == 0); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2116 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2117 | TEST_ASSERT(mbedtls_ssl_conf_psk(&server.conf, options->psk_str->x, |
| 2118 | options->psk_str->len, |
| 2119 | (const unsigned char *) psk_identity, |
| 2120 | strlen(psk_identity)) == 0); |
Jerry Yu | 8897c07 | 2022-08-12 13:56:53 +0800 | [diff] [blame] | 2121 | #if defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2122 | mbedtls_ssl_conf_psk_cb(&server.conf, psk_dummy_callback, NULL); |
Jerry Yu | 8897c07 | 2022-08-12 13:56:53 +0800 | [diff] [blame] | 2123 | #endif |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2124 | } |
| 2125 | #endif |
| 2126 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2127 | if (options->renegotiate) { |
| 2128 | mbedtls_ssl_conf_renegotiation(&(server.conf), |
| 2129 | MBEDTLS_SSL_RENEGOTIATION_ENABLED); |
| 2130 | mbedtls_ssl_conf_renegotiation(&(client.conf), |
| 2131 | MBEDTLS_SSL_RENEGOTIATION_ENABLED); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2132 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2133 | mbedtls_ssl_conf_legacy_renegotiation(&(server.conf), |
| 2134 | options->legacy_renegotiation); |
| 2135 | mbedtls_ssl_conf_legacy_renegotiation(&(client.conf), |
| 2136 | options->legacy_renegotiation); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2137 | } |
| 2138 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
| 2139 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2140 | #if defined(MBEDTLS_DEBUG_C) |
| 2141 | if (options->srv_log_fun) { |
| 2142 | mbedtls_debug_set_threshold(4); |
| 2143 | mbedtls_ssl_conf_dbg(&server.conf, options->srv_log_fun, |
| 2144 | options->srv_log_obj); |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 2145 | } |
| 2146 | #endif |
| 2147 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2148 | TEST_ASSERT(mbedtls_mock_socket_connect(&(client.socket), |
| 2149 | &(server.socket), |
| 2150 | BUFFSIZE) == 0); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2151 | |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2152 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2153 | if (options->resize_buffers != 0) { |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2154 | /* Ensure that the buffer sizes are appropriate before resizes */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2155 | TEST_ASSERT(client.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN); |
| 2156 | TEST_ASSERT(client.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN); |
| 2157 | TEST_ASSERT(server.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN); |
| 2158 | TEST_ASSERT(server.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2159 | } |
| 2160 | #endif |
| 2161 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2162 | if (options->expected_negotiated_version == MBEDTLS_SSL_VERSION_UNKNOWN) { |
Hanno Becker | bc00044 | 2021-06-24 09:18:19 +0100 | [diff] [blame] | 2163 | expected_handshake_result = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION; |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 2164 | } |
| 2165 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2166 | TEST_ASSERT(mbedtls_move_handshake_to_state(&(client.ssl), |
| 2167 | &(server.ssl), |
| 2168 | MBEDTLS_SSL_HANDSHAKE_OVER) |
| 2169 | == expected_handshake_result); |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 2170 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2171 | if (expected_handshake_result != 0) { |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 2172 | /* Connection will have failed by this point, skip to cleanup */ |
| 2173 | goto exit; |
| 2174 | } |
| 2175 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2176 | TEST_ASSERT(mbedtls_ssl_is_handshake_over(&client.ssl) == 1); |
Jerry Yu | df0a71a | 2022-05-26 10:43:30 +0800 | [diff] [blame] | 2177 | |
Jerry Yu | 66adf31 | 2022-05-31 15:23:29 +0800 | [diff] [blame] | 2178 | /* Make sure server state is moved to HANDSHAKE_OVER also. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2179 | TEST_EQUAL(mbedtls_move_handshake_to_state(&(server.ssl), |
| 2180 | &(client.ssl), |
| 2181 | MBEDTLS_SSL_HANDSHAKE_OVER), 0); |
Jerry Yu | df0a71a | 2022-05-26 10:43:30 +0800 | [diff] [blame] | 2182 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2183 | TEST_ASSERT(mbedtls_ssl_is_handshake_over(&server.ssl) == 1); |
Gilles Peskine | 1255b0d | 2022-01-13 01:08:48 +0100 | [diff] [blame] | 2184 | /* Check that both sides have negotiated the expected version. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2185 | mbedtls_test_set_step(0); |
| 2186 | if (!check_ssl_version(options->expected_negotiated_version, |
| 2187 | &client.ssl)) { |
Gilles Peskine | 1255b0d | 2022-01-13 01:08:48 +0100 | [diff] [blame] | 2188 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2189 | } |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 2190 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2191 | mbedtls_test_set_step(1); |
| 2192 | if (!check_ssl_version(options->expected_negotiated_version, |
| 2193 | &server.ssl)) { |
Gilles Peskine | 1255b0d | 2022-01-13 01:08:48 +0100 | [diff] [blame] | 2194 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2195 | } |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 2196 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2197 | if (options->expected_ciphersuite != 0) { |
| 2198 | TEST_EQUAL(server.ssl.session->ciphersuite, |
| 2199 | options->expected_ciphersuite); |
Neil Armstrong | 8c52ed8 | 2022-05-27 13:14:55 +0200 | [diff] [blame] | 2200 | } |
| 2201 | |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2202 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2203 | if (options->resize_buffers != 0) { |
TRodziewicz | 2abf03c | 2021-06-25 14:40:09 +0200 | [diff] [blame] | 2204 | /* A server, when using DTLS, might delay a buffer resize to happen |
| 2205 | * after it receives a message, so we force it. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2206 | TEST_ASSERT(exchange_data(&(client.ssl), &(server.ssl)) == 0); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2207 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2208 | TEST_ASSERT(client.ssl.out_buf_len == |
| 2209 | mbedtls_ssl_get_output_buflen(&client.ssl)); |
| 2210 | TEST_ASSERT(client.ssl.in_buf_len == |
| 2211 | mbedtls_ssl_get_input_buflen(&client.ssl)); |
| 2212 | TEST_ASSERT(server.ssl.out_buf_len == |
| 2213 | mbedtls_ssl_get_output_buflen(&server.ssl)); |
| 2214 | TEST_ASSERT(server.ssl.in_buf_len == |
| 2215 | mbedtls_ssl_get_input_buflen(&server.ssl)); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2216 | } |
| 2217 | #endif |
| 2218 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2219 | if (options->cli_msg_len != 0 || options->srv_msg_len != 0) { |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2220 | /* Start data exchanging test */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2221 | TEST_ASSERT(mbedtls_exchange_data(&(client.ssl), options->cli_msg_len, |
| 2222 | options->expected_cli_fragments, |
| 2223 | &(server.ssl), options->srv_msg_len, |
| 2224 | options->expected_srv_fragments) |
| 2225 | == 0); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2226 | } |
| 2227 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2228 | if (options->serialize == 1) { |
| 2229 | TEST_ASSERT(options->dtls == 1); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2230 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2231 | TEST_ASSERT(mbedtls_ssl_context_save(&(server.ssl), NULL, |
| 2232 | 0, &context_buf_len) |
| 2233 | == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2234 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2235 | context_buf = mbedtls_calloc(1, context_buf_len); |
| 2236 | TEST_ASSERT(context_buf != NULL); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2237 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2238 | TEST_ASSERT(mbedtls_ssl_context_save(&(server.ssl), context_buf, |
| 2239 | context_buf_len, |
| 2240 | &context_buf_len) == 0); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2241 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2242 | mbedtls_ssl_free(&(server.ssl)); |
| 2243 | mbedtls_ssl_init(&(server.ssl)); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2244 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2245 | TEST_ASSERT(mbedtls_ssl_setup(&(server.ssl), &(server.conf)) == 0); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2246 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2247 | mbedtls_ssl_set_bio(&(server.ssl), &server_context, |
| 2248 | mbedtls_mock_tcp_send_msg, |
| 2249 | mbedtls_mock_tcp_recv_msg, |
| 2250 | NULL); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2251 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2252 | mbedtls_ssl_set_user_data_p(&server.ssl, &server); |
Gilles Peskine | 49d7ddf | 2022-01-27 23:25:51 +0100 | [diff] [blame] | 2253 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2254 | #if defined(MBEDTLS_TIMING_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2255 | mbedtls_ssl_set_timer_cb(&server.ssl, &timer_server, |
| 2256 | mbedtls_timing_set_delay, |
| 2257 | mbedtls_timing_get_delay); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2258 | #endif |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2259 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2260 | if (options->resize_buffers != 0) { |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2261 | /* Ensure that the buffer sizes are appropriate before resizes */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2262 | TEST_ASSERT(server.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN); |
| 2263 | TEST_ASSERT(server.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2264 | } |
| 2265 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2266 | TEST_ASSERT(mbedtls_ssl_context_load(&(server.ssl), context_buf, |
| 2267 | context_buf_len) == 0); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2268 | |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2269 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 2270 | /* Validate buffer sizes after context deserialization */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2271 | if (options->resize_buffers != 0) { |
| 2272 | TEST_ASSERT(server.ssl.out_buf_len == |
| 2273 | mbedtls_ssl_get_output_buflen(&server.ssl)); |
| 2274 | TEST_ASSERT(server.ssl.in_buf_len == |
| 2275 | mbedtls_ssl_get_input_buflen(&server.ssl)); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2276 | } |
| 2277 | #endif |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2278 | /* Retest writing/reading */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2279 | if (options->cli_msg_len != 0 || options->srv_msg_len != 0) { |
| 2280 | TEST_ASSERT(mbedtls_exchange_data(&(client.ssl), |
| 2281 | options->cli_msg_len, |
| 2282 | options->expected_cli_fragments, |
| 2283 | &(server.ssl), |
| 2284 | options->srv_msg_len, |
| 2285 | options->expected_srv_fragments) |
| 2286 | == 0); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2287 | } |
| 2288 | } |
| 2289 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2290 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2291 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2292 | if (options->renegotiate) { |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2293 | /* Start test with renegotiation */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2294 | TEST_ASSERT(server.ssl.renego_status == |
| 2295 | MBEDTLS_SSL_INITIAL_HANDSHAKE); |
| 2296 | TEST_ASSERT(client.ssl.renego_status == |
| 2297 | MBEDTLS_SSL_INITIAL_HANDSHAKE); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2298 | |
| 2299 | /* After calling this function for the server, it only sends a handshake |
| 2300 | * request. All renegotiation should happen during data exchanging */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2301 | TEST_ASSERT(mbedtls_ssl_renegotiate(&(server.ssl)) == 0); |
| 2302 | TEST_ASSERT(server.ssl.renego_status == |
| 2303 | MBEDTLS_SSL_RENEGOTIATION_PENDING); |
| 2304 | TEST_ASSERT(client.ssl.renego_status == |
| 2305 | MBEDTLS_SSL_INITIAL_HANDSHAKE); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2306 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2307 | TEST_ASSERT(exchange_data(&(client.ssl), &(server.ssl)) == 0); |
| 2308 | TEST_ASSERT(server.ssl.renego_status == |
| 2309 | MBEDTLS_SSL_RENEGOTIATION_DONE); |
| 2310 | TEST_ASSERT(client.ssl.renego_status == |
| 2311 | MBEDTLS_SSL_RENEGOTIATION_DONE); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2312 | |
| 2313 | /* After calling mbedtls_ssl_renegotiate for the client all renegotiation |
| 2314 | * should happen inside this function. However in this test, we cannot |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 2315 | * perform simultaneous communication between client and server so this |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2316 | * function will return waiting error on the socket. All rest of |
| 2317 | * renegotiation should happen during data exchanging */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2318 | ret = mbedtls_ssl_renegotiate(&(client.ssl)); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2319 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2320 | if (options->resize_buffers != 0) { |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2321 | /* Ensure that the buffer sizes are appropriate before resizes */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2322 | TEST_ASSERT(client.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN); |
| 2323 | TEST_ASSERT(client.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2324 | } |
| 2325 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2326 | TEST_ASSERT(ret == 0 || |
| 2327 | ret == MBEDTLS_ERR_SSL_WANT_READ || |
| 2328 | ret == MBEDTLS_ERR_SSL_WANT_WRITE); |
| 2329 | TEST_ASSERT(server.ssl.renego_status == |
| 2330 | MBEDTLS_SSL_RENEGOTIATION_DONE); |
| 2331 | TEST_ASSERT(client.ssl.renego_status == |
| 2332 | MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2333 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2334 | TEST_ASSERT(exchange_data(&(client.ssl), &(server.ssl)) == 0); |
| 2335 | TEST_ASSERT(server.ssl.renego_status == |
| 2336 | MBEDTLS_SSL_RENEGOTIATION_DONE); |
| 2337 | TEST_ASSERT(client.ssl.renego_status == |
| 2338 | MBEDTLS_SSL_RENEGOTIATION_DONE); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2339 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 2340 | /* Validate buffer sizes after renegotiation */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2341 | if (options->resize_buffers != 0) { |
| 2342 | TEST_ASSERT(client.ssl.out_buf_len == |
| 2343 | mbedtls_ssl_get_output_buflen(&client.ssl)); |
| 2344 | TEST_ASSERT(client.ssl.in_buf_len == |
| 2345 | mbedtls_ssl_get_input_buflen(&client.ssl)); |
| 2346 | TEST_ASSERT(server.ssl.out_buf_len == |
| 2347 | mbedtls_ssl_get_output_buflen(&server.ssl)); |
| 2348 | TEST_ASSERT(server.ssl.in_buf_len == |
| 2349 | mbedtls_ssl_get_input_buflen(&server.ssl)); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2350 | } |
| 2351 | #endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */ |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2352 | } |
| 2353 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
| 2354 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2355 | TEST_ASSERT(mbedtls_ssl_conf_get_user_data_p(&client.conf) == &client); |
| 2356 | TEST_ASSERT(mbedtls_ssl_get_user_data_p(&client.ssl) == &client); |
| 2357 | TEST_ASSERT(mbedtls_ssl_conf_get_user_data_p(&server.conf) == &server); |
| 2358 | TEST_ASSERT(mbedtls_ssl_get_user_data_p(&server.ssl) == &server); |
Gilles Peskine | 80dae04 | 2022-01-21 23:50:39 +0100 | [diff] [blame] | 2359 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2360 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2361 | mbedtls_endpoint_free(&client, options->dtls != 0 ? &client_context : NULL); |
| 2362 | mbedtls_endpoint_free(&server, options->dtls != 0 ? &server_context : NULL); |
| 2363 | #if defined(MBEDTLS_DEBUG_C) |
| 2364 | if (options->cli_log_fun || options->srv_log_fun) { |
| 2365 | mbedtls_debug_set_threshold(0); |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 2366 | } |
| 2367 | #endif |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2368 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2369 | if (context_buf != NULL) { |
| 2370 | mbedtls_free(context_buf); |
| 2371 | } |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2372 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2373 | USE_PSA_DONE(); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2374 | } |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 2375 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2376 | |
Ronald Cron | e7b9b6b | 2022-06-10 17:24:31 +0200 | [diff] [blame] | 2377 | #if defined(MBEDTLS_TEST_HOOKS) |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 2378 | /* |
| 2379 | * Tweak vector lengths in a TLS 1.3 Certificate message |
| 2380 | * |
Ronald Cron | cf600bc | 2022-06-17 15:54:16 +0200 | [diff] [blame] | 2381 | * \param[in] buf Buffer containing the Certificate message to tweak |
| 2382 | * \param[in]]out] end End of the buffer to parse |
| 2383 | * \param tweak Tweak identifier (from 1 to the number of tweaks). |
| 2384 | * \param[out] expected_result Error code expected from the parsing function |
| 2385 | * \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] | 2386 | * is expected to fail. All zeroes if no |
| 2387 | * MBEDTLS_SSL_CHK_BUF_READ_PTR failure is expected. |
| 2388 | */ |
| 2389 | int tweak_tls13_certificate_msg_vector_len( |
Ronald Cron | e7b9b6b | 2022-06-10 17:24:31 +0200 | [diff] [blame] | 2390 | unsigned char *buf, unsigned char **end, int tweak, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2391 | int *expected_result, mbedtls_ssl_chk_buf_ptr_args *args) |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 2392 | { |
| 2393 | /* |
| 2394 | * The definition of the tweaks assume that the certificate list contains only |
| 2395 | * one certificate. |
| 2396 | */ |
| 2397 | |
| 2398 | /* |
| 2399 | * struct { |
| 2400 | * opaque cert_data<1..2^24-1>; |
| 2401 | * Extension extensions<0..2^16-1>; |
| 2402 | * } CertificateEntry; |
| 2403 | * |
| 2404 | * struct { |
| 2405 | * opaque certificate_request_context<0..2^8-1>; |
| 2406 | * CertificateEntry certificate_list<0..2^24-1>; |
| 2407 | * } Certificate; |
| 2408 | */ |
| 2409 | unsigned char *p_certificate_request_context_len = buf; |
| 2410 | size_t certificate_request_context_len = buf[0]; |
| 2411 | |
| 2412 | unsigned char *p_certificate_list_len = buf + 1 + certificate_request_context_len; |
| 2413 | unsigned char *certificate_list = p_certificate_list_len + 3; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2414 | 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] | 2415 | |
| 2416 | unsigned char *p_cert_data_len = certificate_list; |
| 2417 | unsigned char *cert_data = p_cert_data_len + 3; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2418 | 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] | 2419 | |
| 2420 | unsigned char *p_extensions_len = cert_data + cert_data_len; |
| 2421 | unsigned char *extensions = p_extensions_len + 2; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2422 | size_t extensions_len = MBEDTLS_GET_UINT16_BE(p_extensions_len, 0); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 2423 | |
| 2424 | *expected_result = MBEDTLS_ERR_SSL_DECODE_ERROR; |
| 2425 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2426 | switch (tweak) { |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 2427 | case 1: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2428 | /* Failure when checking if the certificate request context length and |
| 2429 | * certificate list length can be read |
| 2430 | */ |
| 2431 | *end = buf + 3; |
| 2432 | set_chk_buf_ptr_args(args, buf, *end, 4); |
| 2433 | break; |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 2434 | |
| 2435 | case 2: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2436 | /* Invalid certificate request context length. |
| 2437 | */ |
| 2438 | *p_certificate_request_context_len = |
| 2439 | certificate_request_context_len + 1; |
| 2440 | reset_chk_buf_ptr_args(args); |
| 2441 | break; |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 2442 | |
| 2443 | case 3: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2444 | /* Failure when checking if certificate_list data can be read. */ |
| 2445 | MBEDTLS_PUT_UINT24_BE(certificate_list_len + 1, |
| 2446 | p_certificate_list_len, 0); |
| 2447 | set_chk_buf_ptr_args(args, certificate_list, *end, |
| 2448 | certificate_list_len + 1); |
| 2449 | break; |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 2450 | |
| 2451 | case 4: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2452 | /* Failure when checking if the cert_data length can be read. */ |
| 2453 | MBEDTLS_PUT_UINT24_BE(2, p_certificate_list_len, 0); |
| 2454 | set_chk_buf_ptr_args(args, p_cert_data_len, certificate_list + 2, 3); |
| 2455 | break; |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 2456 | |
| 2457 | case 5: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2458 | /* Failure when checking if cert_data data can be read. */ |
| 2459 | MBEDTLS_PUT_UINT24_BE(certificate_list_len - 3 + 1, |
| 2460 | p_cert_data_len, 0); |
| 2461 | set_chk_buf_ptr_args(args, cert_data, |
| 2462 | certificate_list + certificate_list_len, |
| 2463 | certificate_list_len - 3 + 1); |
| 2464 | break; |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 2465 | |
| 2466 | case 6: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2467 | /* Failure when checking if the extensions length can be read. */ |
| 2468 | MBEDTLS_PUT_UINT24_BE(certificate_list_len - extensions_len - 1, |
| 2469 | p_certificate_list_len, 0); |
| 2470 | set_chk_buf_ptr_args(args, p_extensions_len, |
| 2471 | certificate_list + certificate_list_len - extensions_len - 1, 2); |
| 2472 | break; |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 2473 | |
| 2474 | case 7: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2475 | /* Failure when checking if extensions data can be read. */ |
| 2476 | MBEDTLS_PUT_UINT16_BE(extensions_len + 1, p_extensions_len, 0); |
Ronald Cron | e7b9b6b | 2022-06-10 17:24:31 +0200 | [diff] [blame] | 2477 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2478 | set_chk_buf_ptr_args(args, extensions, |
| 2479 | certificate_list + certificate_list_len, extensions_len + 1); |
| 2480 | break; |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 2481 | |
| 2482 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2483 | return -1; |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 2484 | } |
| 2485 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2486 | return 0; |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 2487 | } |
Ronald Cron | e7b9b6b | 2022-06-10 17:24:31 +0200 | [diff] [blame] | 2488 | #endif /* MBEDTLS_TEST_HOOKS */ |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 2489 | |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 2490 | #define ECJPAKE_TEST_PWD "bla" |
Valerio Setti | e7518ba | 2022-12-02 12:09:43 +0100 | [diff] [blame] | 2491 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2492 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 2493 | #define ECJPAKE_TEST_SET_PASSWORD(exp_ret_val) \ |
| 2494 | ret = (use_opaque_arg) ? \ |
| 2495 | mbedtls_ssl_set_hs_ecjpake_password_opaque(&ssl, pwd_slot) : \ |
| 2496 | mbedtls_ssl_set_hs_ecjpake_password(&ssl, pwd_string, pwd_len); \ |
| 2497 | TEST_EQUAL(ret, exp_ret_val) |
Valerio Setti | e7518ba | 2022-12-02 12:09:43 +0100 | [diff] [blame] | 2498 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2499 | #define ECJPAKE_TEST_SET_PASSWORD(exp_ret_val) \ |
| 2500 | ret = mbedtls_ssl_set_hs_ecjpake_password(&ssl, \ |
| 2501 | pwd_string, pwd_len); \ |
| 2502 | TEST_EQUAL(ret, exp_ret_val) |
Valerio Setti | e7518ba | 2022-12-02 12:09:43 +0100 | [diff] [blame] | 2503 | #endif |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 2504 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2505 | #define TEST_AVAILABLE_ECC(tls_id_, group_id_, psa_family_, psa_bits_) \ |
| 2506 | TEST_EQUAL(mbedtls_ssl_get_ecp_group_id_from_tls_id(tls_id_), \ |
| 2507 | group_id_); \ |
| 2508 | TEST_EQUAL(mbedtls_ssl_get_tls_id_from_ecp_group_id(group_id_), \ |
| 2509 | tls_id_); \ |
| 2510 | TEST_EQUAL(mbedtls_ssl_get_psa_curve_info_from_tls_id(tls_id_, \ |
| 2511 | &psa_family, &psa_bits), PSA_SUCCESS); \ |
| 2512 | TEST_EQUAL(psa_family_, psa_family); \ |
| 2513 | TEST_EQUAL(psa_bits_, psa_bits); |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 2514 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2515 | #define TEST_UNAVAILABLE_ECC(tls_id_, group_id_, psa_family_, psa_bits_) \ |
| 2516 | TEST_EQUAL(mbedtls_ssl_get_ecp_group_id_from_tls_id(tls_id_), \ |
| 2517 | MBEDTLS_ECP_DP_NONE); \ |
| 2518 | TEST_EQUAL(mbedtls_ssl_get_tls_id_from_ecp_group_id(group_id_), \ |
| 2519 | 0); \ |
| 2520 | TEST_EQUAL(mbedtls_ssl_get_psa_curve_info_from_tls_id(tls_id_, \ |
| 2521 | &psa_family, &psa_bits), \ |
| 2522 | PSA_ERROR_NOT_SUPPORTED); |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 2523 | |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 2524 | /* END_HEADER */ |
| 2525 | |
| 2526 | /* BEGIN_DEPENDENCIES |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2527 | * depends_on:MBEDTLS_SSL_TLS_C |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 2528 | * END_DEPENDENCIES |
| 2529 | */ |
| 2530 | |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2531 | /* BEGIN_CASE */ |
| 2532 | void test_callback_buffer_sanity() |
| 2533 | { |
| 2534 | enum { MSGLEN = 10 }; |
| 2535 | mbedtls_test_buffer buf; |
| 2536 | unsigned char input[MSGLEN]; |
| 2537 | unsigned char output[MSGLEN]; |
| 2538 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2539 | memset(input, 0, sizeof(input)); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2540 | |
| 2541 | /* Make sure calling put and get on NULL buffer results in error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2542 | TEST_ASSERT(mbedtls_test_buffer_put(NULL, input, sizeof(input)) |
| 2543 | == -1); |
| 2544 | TEST_ASSERT(mbedtls_test_buffer_get(NULL, output, sizeof(output)) |
| 2545 | == -1); |
| 2546 | TEST_ASSERT(mbedtls_test_buffer_put(NULL, NULL, sizeof(input)) == -1); |
Andrzej Kurek | f777414 | 2020-01-22 06:34:59 -0500 | [diff] [blame] | 2547 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2548 | TEST_ASSERT(mbedtls_test_buffer_put(NULL, NULL, 0) == -1); |
| 2549 | TEST_ASSERT(mbedtls_test_buffer_get(NULL, NULL, 0) == -1); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2550 | |
| 2551 | /* 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] | 2552 | * in error. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2553 | mbedtls_test_buffer_init(&buf); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2554 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2555 | TEST_ASSERT(mbedtls_test_buffer_put(&buf, input, sizeof(input)) == -1); |
| 2556 | TEST_ASSERT(mbedtls_test_buffer_get(&buf, output, sizeof(output)) |
| 2557 | == -1); |
| 2558 | TEST_ASSERT(mbedtls_test_buffer_put(&buf, NULL, sizeof(input)) == -1); |
Andrzej Kurek | f777414 | 2020-01-22 06:34:59 -0500 | [diff] [blame] | 2559 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2560 | TEST_ASSERT(mbedtls_test_buffer_put(&buf, NULL, 0) == -1); |
| 2561 | TEST_ASSERT(mbedtls_test_buffer_get(&buf, NULL, 0) == -1); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2562 | |
Andrzej Kurek | f777414 | 2020-01-22 06:34:59 -0500 | [diff] [blame] | 2563 | /* Make sure calling put and get on NULL input only results in |
| 2564 | * error if the length is not zero, and that a NULL output is valid for data |
| 2565 | * dropping. |
| 2566 | */ |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2567 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2568 | TEST_ASSERT(mbedtls_test_buffer_setup(&buf, sizeof(input)) == 0); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2569 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2570 | TEST_ASSERT(mbedtls_test_buffer_put(&buf, NULL, sizeof(input)) == -1); |
| 2571 | TEST_ASSERT(mbedtls_test_buffer_get(&buf, NULL, sizeof(output)) |
| 2572 | == 0); |
| 2573 | TEST_ASSERT(mbedtls_test_buffer_put(&buf, NULL, 0) == 0); |
| 2574 | TEST_ASSERT(mbedtls_test_buffer_get(&buf, NULL, 0) == 0); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2575 | |
Piotr Nowicki | fb437d7 | 2020-01-13 16:59:12 +0100 | [diff] [blame] | 2576 | /* Make sure calling put several times in the row is safe */ |
| 2577 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2578 | TEST_ASSERT(mbedtls_test_buffer_put(&buf, input, sizeof(input)) |
| 2579 | == sizeof(input)); |
| 2580 | TEST_ASSERT(mbedtls_test_buffer_get(&buf, output, 2) == 2); |
| 2581 | TEST_ASSERT(mbedtls_test_buffer_put(&buf, input, 1) == 1); |
| 2582 | TEST_ASSERT(mbedtls_test_buffer_put(&buf, input, 2) == 1); |
| 2583 | TEST_ASSERT(mbedtls_test_buffer_put(&buf, input, 2) == 0); |
Piotr Nowicki | fb437d7 | 2020-01-13 16:59:12 +0100 | [diff] [blame] | 2584 | |
| 2585 | |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2586 | exit: |
| 2587 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2588 | mbedtls_test_buffer_free(&buf); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2589 | } |
| 2590 | /* END_CASE */ |
| 2591 | |
| 2592 | /* |
| 2593 | * Test if the implementation of `mbedtls_test_buffer` related functions is |
| 2594 | * correct and works as expected. |
| 2595 | * |
| 2596 | * That is |
| 2597 | * - If we try to put in \p put1 bytes then we can put in \p put1_ret bytes. |
| 2598 | * - Afterwards if we try to get \p get1 bytes then we can get \get1_ret bytes. |
| 2599 | * - Next, if we try to put in \p put1 bytes then we can put in \p put1_ret |
| 2600 | * bytes. |
| 2601 | * - Afterwards if we try to get \p get1 bytes then we can get \get1_ret bytes. |
| 2602 | * - All of the bytes we got match the bytes we put in in a FIFO manner. |
| 2603 | */ |
| 2604 | |
| 2605 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2606 | void test_callback_buffer(int size, int put1, int put1_ret, |
| 2607 | int get1, int get1_ret, int put2, int put2_ret, |
| 2608 | int get2, int get2_ret) |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2609 | { |
| 2610 | enum { ROUNDS = 2 }; |
| 2611 | size_t put[ROUNDS]; |
| 2612 | int put_ret[ROUNDS]; |
| 2613 | size_t get[ROUNDS]; |
| 2614 | int get_ret[ROUNDS]; |
| 2615 | mbedtls_test_buffer buf; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2616 | unsigned char *input = NULL; |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2617 | size_t input_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2618 | unsigned char *output = NULL; |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2619 | size_t output_len; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2620 | size_t i, j, written, read; |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2621 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2622 | mbedtls_test_buffer_init(&buf); |
| 2623 | TEST_ASSERT(mbedtls_test_buffer_setup(&buf, size) == 0); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2624 | |
| 2625 | /* Check the sanity of input parameters and initialise local variables. That |
| 2626 | * is, ensure that the amount of data is not negative and that we are not |
| 2627 | * expecting more to put or get than we actually asked for. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2628 | TEST_ASSERT(put1 >= 0); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2629 | put[0] = put1; |
| 2630 | put_ret[0] = put1_ret; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2631 | TEST_ASSERT(put1_ret <= put1); |
| 2632 | TEST_ASSERT(put2 >= 0); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2633 | put[1] = put2; |
| 2634 | put_ret[1] = put2_ret; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2635 | TEST_ASSERT(put2_ret <= put2); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2636 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2637 | TEST_ASSERT(get1 >= 0); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2638 | get[0] = get1; |
| 2639 | get_ret[0] = get1_ret; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2640 | TEST_ASSERT(get1_ret <= get1); |
| 2641 | TEST_ASSERT(get2 >= 0); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2642 | get[1] = get2; |
| 2643 | get_ret[1] = get2_ret; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2644 | TEST_ASSERT(get2_ret <= get2); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2645 | |
| 2646 | input_len = 0; |
| 2647 | /* Calculate actual input and output lengths */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2648 | for (j = 0; j < ROUNDS; j++) { |
| 2649 | if (put_ret[j] > 0) { |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2650 | input_len += put_ret[j]; |
| 2651 | } |
| 2652 | } |
| 2653 | /* In order to always have a valid pointer we always allocate at least 1 |
| 2654 | * byte. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2655 | if (input_len == 0) { |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2656 | input_len = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2657 | } |
| 2658 | ASSERT_ALLOC(input, input_len); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2659 | |
| 2660 | output_len = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2661 | for (j = 0; j < ROUNDS; j++) { |
| 2662 | if (get_ret[j] > 0) { |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2663 | output_len += get_ret[j]; |
| 2664 | } |
| 2665 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2666 | TEST_ASSERT(output_len <= input_len); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2667 | /* In order to always have a valid pointer we always allocate at least 1 |
| 2668 | * byte. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2669 | if (output_len == 0) { |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2670 | output_len = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2671 | } |
| 2672 | ASSERT_ALLOC(output, output_len); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2673 | |
| 2674 | /* Fill up the buffer with structured data so that unwanted changes |
| 2675 | * can be detected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2676 | for (i = 0; i < input_len; i++) { |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2677 | input[i] = i & 0xFF; |
| 2678 | } |
| 2679 | |
| 2680 | written = read = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2681 | for (j = 0; j < ROUNDS; j++) { |
| 2682 | TEST_ASSERT(put_ret[j] == mbedtls_test_buffer_put(&buf, |
| 2683 | input + written, put[j])); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2684 | written += put_ret[j]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2685 | TEST_ASSERT(get_ret[j] == mbedtls_test_buffer_get(&buf, |
| 2686 | output + read, get[j])); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2687 | read += get_ret[j]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2688 | TEST_ASSERT(read <= written); |
| 2689 | if (get_ret[j] > 0) { |
| 2690 | TEST_ASSERT(memcmp(output + read - get_ret[j], |
| 2691 | input + read - get_ret[j], get_ret[j]) |
| 2692 | == 0); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2693 | } |
| 2694 | } |
| 2695 | |
| 2696 | exit: |
| 2697 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2698 | mbedtls_free(input); |
| 2699 | mbedtls_free(output); |
| 2700 | mbedtls_test_buffer_free(&buf); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2701 | } |
| 2702 | /* END_CASE */ |
| 2703 | |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2704 | /* |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2705 | * Test if the implementation of `mbedtls_mock_socket` related I/O functions is |
| 2706 | * correct and works as expected on unconnected sockets. |
| 2707 | */ |
| 2708 | |
| 2709 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2710 | void ssl_mock_sanity() |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2711 | { |
| 2712 | enum { MSGLEN = 105 }; |
Paul Elliott | 21c8fe5 | 2021-11-24 16:54:26 +0000 | [diff] [blame] | 2713 | unsigned char message[MSGLEN] = { 0 }; |
| 2714 | unsigned char received[MSGLEN] = { 0 }; |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2715 | mbedtls_mock_socket socket; |
| 2716 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2717 | mbedtls_mock_socket_init(&socket); |
| 2718 | TEST_ASSERT(mbedtls_mock_tcp_send_b(&socket, message, MSGLEN) < 0); |
| 2719 | mbedtls_mock_socket_close(&socket); |
| 2720 | mbedtls_mock_socket_init(&socket); |
| 2721 | TEST_ASSERT(mbedtls_mock_tcp_recv_b(&socket, received, MSGLEN) < 0); |
| 2722 | mbedtls_mock_socket_close(&socket); |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2723 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2724 | mbedtls_mock_socket_init(&socket); |
| 2725 | TEST_ASSERT(mbedtls_mock_tcp_send_nb(&socket, message, MSGLEN) < 0); |
| 2726 | mbedtls_mock_socket_close(&socket); |
| 2727 | mbedtls_mock_socket_init(&socket); |
| 2728 | TEST_ASSERT(mbedtls_mock_tcp_recv_nb(&socket, received, MSGLEN) < 0); |
| 2729 | mbedtls_mock_socket_close(&socket); |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2730 | |
| 2731 | exit: |
| 2732 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2733 | mbedtls_mock_socket_close(&socket); |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2734 | } |
| 2735 | /* END_CASE */ |
| 2736 | |
| 2737 | /* |
| 2738 | * Test if the implementation of `mbedtls_mock_socket` related functions can |
| 2739 | * send a single message from the client to the server. |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2740 | */ |
| 2741 | |
| 2742 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2743 | void ssl_mock_tcp(int blocking) |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2744 | { |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2745 | enum { MSGLEN = 105 }; |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2746 | enum { BUFLEN = MSGLEN / 5 }; |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2747 | unsigned char message[MSGLEN]; |
| 2748 | unsigned char received[MSGLEN]; |
| 2749 | mbedtls_mock_socket client; |
| 2750 | mbedtls_mock_socket server; |
| 2751 | size_t written, read; |
| 2752 | int send_ret, recv_ret; |
| 2753 | mbedtls_ssl_send_t *send; |
| 2754 | mbedtls_ssl_recv_t *recv; |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2755 | unsigned i; |
| 2756 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2757 | if (blocking == 0) { |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2758 | send = mbedtls_mock_tcp_send_nb; |
| 2759 | recv = mbedtls_mock_tcp_recv_nb; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2760 | } else { |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2761 | send = mbedtls_mock_tcp_send_b; |
| 2762 | recv = mbedtls_mock_tcp_recv_b; |
| 2763 | } |
| 2764 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2765 | mbedtls_mock_socket_init(&client); |
| 2766 | mbedtls_mock_socket_init(&server); |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2767 | |
| 2768 | /* Fill up the buffer with structured data so that unwanted changes |
| 2769 | * can be detected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2770 | for (i = 0; i < MSGLEN; i++) { |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2771 | message[i] = i & 0xFF; |
| 2772 | } |
| 2773 | |
| 2774 | /* Make sure that sending a message takes a few iterations. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2775 | TEST_ASSERT(0 == mbedtls_mock_socket_connect(&client, &server, BUFLEN)); |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2776 | |
| 2777 | /* Send the message to the server */ |
| 2778 | send_ret = recv_ret = 1; |
| 2779 | written = read = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2780 | while (send_ret != 0 || recv_ret != 0) { |
| 2781 | send_ret = send(&client, message + written, MSGLEN - written); |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2782 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2783 | TEST_ASSERT(send_ret >= 0); |
| 2784 | TEST_ASSERT(send_ret <= BUFLEN); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2785 | written += send_ret; |
| 2786 | |
| 2787 | /* 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] | 2788 | if (send_ret == BUFLEN) { |
| 2789 | int blocking_ret = send(&client, message, 1); |
| 2790 | if (blocking) { |
| 2791 | TEST_ASSERT(blocking_ret == 0); |
| 2792 | } else { |
| 2793 | TEST_ASSERT(blocking_ret == MBEDTLS_ERR_SSL_WANT_WRITE); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2794 | } |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2795 | } |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2796 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2797 | recv_ret = recv(&server, received + read, MSGLEN - read); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2798 | |
| 2799 | /* The result depends on whether any data was sent */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2800 | if (send_ret > 0) { |
| 2801 | TEST_ASSERT(recv_ret > 0); |
| 2802 | TEST_ASSERT(recv_ret <= BUFLEN); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2803 | read += recv_ret; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2804 | } else if (blocking) { |
| 2805 | TEST_ASSERT(recv_ret == 0); |
| 2806 | } else { |
| 2807 | TEST_ASSERT(recv_ret == MBEDTLS_ERR_SSL_WANT_READ); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2808 | recv_ret = 0; |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2809 | } |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2810 | |
| 2811 | /* 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] | 2812 | if (recv_ret == BUFLEN) { |
| 2813 | int blocking_ret = recv(&server, received, 1); |
| 2814 | if (blocking) { |
| 2815 | TEST_ASSERT(blocking_ret == 0); |
| 2816 | } else { |
| 2817 | TEST_ASSERT(blocking_ret == MBEDTLS_ERR_SSL_WANT_READ); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2818 | } |
| 2819 | } |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2820 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2821 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2822 | |
| 2823 | exit: |
| 2824 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2825 | mbedtls_mock_socket_close(&client); |
| 2826 | mbedtls_mock_socket_close(&server); |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2827 | } |
| 2828 | /* END_CASE */ |
| 2829 | |
| 2830 | /* |
| 2831 | * Test if the implementation of `mbedtls_mock_socket` related functions can |
| 2832 | * send messages in both direction at the same time (with the I/O calls |
| 2833 | * interleaving). |
| 2834 | */ |
| 2835 | |
| 2836 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2837 | void ssl_mock_tcp_interleaving(int blocking) |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2838 | { |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2839 | enum { ROUNDS = 2 }; |
| 2840 | enum { MSGLEN = 105 }; |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2841 | enum { BUFLEN = MSGLEN / 5 }; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2842 | unsigned char message[ROUNDS][MSGLEN]; |
| 2843 | unsigned char received[ROUNDS][MSGLEN]; |
| 2844 | mbedtls_mock_socket client; |
| 2845 | mbedtls_mock_socket server; |
| 2846 | size_t written[ROUNDS]; |
| 2847 | size_t read[ROUNDS]; |
| 2848 | int send_ret[ROUNDS]; |
| 2849 | int recv_ret[ROUNDS]; |
| 2850 | unsigned i, j, progress; |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 2851 | mbedtls_ssl_send_t *send; |
| 2852 | mbedtls_ssl_recv_t *recv; |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 2853 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2854 | if (blocking == 0) { |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 2855 | send = mbedtls_mock_tcp_send_nb; |
| 2856 | recv = mbedtls_mock_tcp_recv_nb; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2857 | } else { |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 2858 | send = mbedtls_mock_tcp_send_b; |
| 2859 | recv = mbedtls_mock_tcp_recv_b; |
| 2860 | } |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2861 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2862 | mbedtls_mock_socket_init(&client); |
| 2863 | mbedtls_mock_socket_init(&server); |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2864 | |
| 2865 | /* Fill up the buffers with structured data so that unwanted changes |
| 2866 | * can be detected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2867 | for (i = 0; i < ROUNDS; i++) { |
| 2868 | for (j = 0; j < MSGLEN; j++) { |
| 2869 | message[i][j] = (i * MSGLEN + j) & 0xFF; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2870 | } |
| 2871 | } |
| 2872 | |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2873 | /* Make sure that sending a message takes a few iterations. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2874 | TEST_ASSERT(0 == mbedtls_mock_socket_connect(&client, &server, BUFLEN)); |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2875 | |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2876 | /* Send the message from both sides, interleaving. */ |
| 2877 | progress = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2878 | for (i = 0; i < ROUNDS; i++) { |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2879 | written[i] = 0; |
| 2880 | read[i] = 0; |
| 2881 | } |
| 2882 | /* This loop does not stop as long as there was a successful write or read |
| 2883 | * of at least one byte on either side. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2884 | while (progress != 0) { |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2885 | mbedtls_mock_socket *socket; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2886 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2887 | for (i = 0; i < ROUNDS; i++) { |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2888 | /* First sending is from the client */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2889 | socket = (i % 2 == 0) ? (&client) : (&server); |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2890 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2891 | send_ret[i] = send(socket, message[i] + written[i], |
| 2892 | MSGLEN - written[i]); |
| 2893 | TEST_ASSERT(send_ret[i] >= 0); |
| 2894 | TEST_ASSERT(send_ret[i] <= BUFLEN); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2895 | written[i] += send_ret[i]; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2896 | |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2897 | /* If the buffer is full we can test blocking and non-blocking |
| 2898 | * send */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2899 | if (send_ret[i] == BUFLEN) { |
| 2900 | int blocking_ret = send(socket, message[i], 1); |
| 2901 | if (blocking) { |
| 2902 | TEST_ASSERT(blocking_ret == 0); |
| 2903 | } else { |
| 2904 | TEST_ASSERT(blocking_ret == MBEDTLS_ERR_SSL_WANT_WRITE); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2905 | } |
| 2906 | } |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 2907 | } |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2908 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2909 | for (i = 0; i < ROUNDS; i++) { |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2910 | /* First receiving is from the server */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2911 | socket = (i % 2 == 0) ? (&server) : (&client); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2912 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2913 | recv_ret[i] = recv(socket, received[i] + read[i], |
| 2914 | MSGLEN - read[i]); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2915 | |
| 2916 | /* The result depends on whether any data was sent */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2917 | if (send_ret[i] > 0) { |
| 2918 | TEST_ASSERT(recv_ret[i] > 0); |
| 2919 | TEST_ASSERT(recv_ret[i] <= BUFLEN); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2920 | read[i] += recv_ret[i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2921 | } else if (blocking) { |
| 2922 | TEST_ASSERT(recv_ret[i] == 0); |
| 2923 | } else { |
| 2924 | TEST_ASSERT(recv_ret[i] == MBEDTLS_ERR_SSL_WANT_READ); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2925 | recv_ret[i] = 0; |
| 2926 | } |
| 2927 | |
| 2928 | /* If the buffer is empty we can test blocking and non-blocking |
| 2929 | * read */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2930 | if (recv_ret[i] == BUFLEN) { |
| 2931 | int blocking_ret = recv(socket, received[i], 1); |
| 2932 | if (blocking) { |
| 2933 | TEST_ASSERT(blocking_ret == 0); |
| 2934 | } else { |
| 2935 | TEST_ASSERT(blocking_ret == MBEDTLS_ERR_SSL_WANT_READ); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2936 | } |
| 2937 | } |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 2938 | } |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2939 | |
| 2940 | progress = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2941 | for (i = 0; i < ROUNDS; i++) { |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2942 | progress += send_ret[i] + recv_ret[i]; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2943 | } |
| 2944 | } |
| 2945 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2946 | for (i = 0; i < ROUNDS; i++) { |
| 2947 | TEST_ASSERT(memcmp(message[i], received[i], MSGLEN) == 0); |
| 2948 | } |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2949 | |
| 2950 | exit: |
| 2951 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2952 | mbedtls_mock_socket_close(&client); |
| 2953 | mbedtls_mock_socket_close(&server); |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2954 | } |
| 2955 | /* END_CASE */ |
| 2956 | |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2957 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2958 | void ssl_message_queue_sanity() |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2959 | { |
| 2960 | mbedtls_test_message_queue queue; |
| 2961 | |
| 2962 | /* Trying to push/pull to an empty queue */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2963 | TEST_ASSERT(mbedtls_test_message_queue_push_info(NULL, 1) |
| 2964 | == MBEDTLS_TEST_ERROR_ARG_NULL); |
| 2965 | TEST_ASSERT(mbedtls_test_message_queue_pop_info(NULL, 1) |
| 2966 | == MBEDTLS_TEST_ERROR_ARG_NULL); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2967 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2968 | TEST_ASSERT(mbedtls_test_message_queue_setup(&queue, 3) == 0); |
| 2969 | TEST_ASSERT(queue.capacity == 3); |
| 2970 | TEST_ASSERT(queue.num == 0); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2971 | |
| 2972 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2973 | mbedtls_test_message_queue_free(&queue); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2974 | } |
| 2975 | /* END_CASE */ |
| 2976 | |
| 2977 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2978 | void ssl_message_queue_basic() |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2979 | { |
| 2980 | mbedtls_test_message_queue queue; |
| 2981 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2982 | TEST_ASSERT(mbedtls_test_message_queue_setup(&queue, 3) == 0); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2983 | |
| 2984 | /* Sanity test - 3 pushes and 3 pops with sufficient space */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2985 | TEST_ASSERT(mbedtls_test_message_queue_push_info(&queue, 1) == 1); |
| 2986 | TEST_ASSERT(queue.capacity == 3); |
| 2987 | TEST_ASSERT(queue.num == 1); |
| 2988 | TEST_ASSERT(mbedtls_test_message_queue_push_info(&queue, 1) == 1); |
| 2989 | TEST_ASSERT(queue.capacity == 3); |
| 2990 | TEST_ASSERT(queue.num == 2); |
| 2991 | TEST_ASSERT(mbedtls_test_message_queue_push_info(&queue, 2) == 2); |
| 2992 | TEST_ASSERT(queue.capacity == 3); |
| 2993 | TEST_ASSERT(queue.num == 3); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2994 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2995 | TEST_ASSERT(mbedtls_test_message_queue_pop_info(&queue, 1) == 1); |
| 2996 | TEST_ASSERT(mbedtls_test_message_queue_pop_info(&queue, 1) == 1); |
| 2997 | TEST_ASSERT(mbedtls_test_message_queue_pop_info(&queue, 2) == 2); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2998 | |
| 2999 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3000 | mbedtls_test_message_queue_free(&queue); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 3001 | } |
| 3002 | /* END_CASE */ |
| 3003 | |
| 3004 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3005 | void ssl_message_queue_overflow_underflow() |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 3006 | { |
| 3007 | mbedtls_test_message_queue queue; |
| 3008 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3009 | TEST_ASSERT(mbedtls_test_message_queue_setup(&queue, 3) == 0); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 3010 | |
| 3011 | /* 4 pushes (last one with an error), 4 pops (last one with an error) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3012 | TEST_ASSERT(mbedtls_test_message_queue_push_info(&queue, 1) == 1); |
| 3013 | TEST_ASSERT(mbedtls_test_message_queue_push_info(&queue, 1) == 1); |
| 3014 | TEST_ASSERT(mbedtls_test_message_queue_push_info(&queue, 2) == 2); |
| 3015 | TEST_ASSERT(mbedtls_test_message_queue_push_info(&queue, 3) |
| 3016 | == MBEDTLS_ERR_SSL_WANT_WRITE); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 3017 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3018 | TEST_ASSERT(mbedtls_test_message_queue_pop_info(&queue, 1) == 1); |
| 3019 | TEST_ASSERT(mbedtls_test_message_queue_pop_info(&queue, 1) == 1); |
| 3020 | TEST_ASSERT(mbedtls_test_message_queue_pop_info(&queue, 2) == 2); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 3021 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3022 | TEST_ASSERT(mbedtls_test_message_queue_pop_info(&queue, 1) |
| 3023 | == MBEDTLS_ERR_SSL_WANT_READ); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 3024 | |
| 3025 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3026 | mbedtls_test_message_queue_free(&queue); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 3027 | } |
| 3028 | /* END_CASE */ |
| 3029 | |
| 3030 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3031 | void ssl_message_queue_interleaved() |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 3032 | { |
| 3033 | mbedtls_test_message_queue queue; |
| 3034 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3035 | TEST_ASSERT(mbedtls_test_message_queue_setup(&queue, 3) == 0); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 3036 | |
| 3037 | /* Interleaved test - [2 pushes, 1 pop] twice, and then two pops |
| 3038 | * (to wrap around the buffer) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3039 | TEST_ASSERT(mbedtls_test_message_queue_push_info(&queue, 1) == 1); |
| 3040 | TEST_ASSERT(mbedtls_test_message_queue_push_info(&queue, 1) == 1); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 3041 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3042 | TEST_ASSERT(mbedtls_test_message_queue_pop_info(&queue, 1) == 1); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 3043 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3044 | TEST_ASSERT(mbedtls_test_message_queue_push_info(&queue, 2) == 2); |
| 3045 | TEST_ASSERT(mbedtls_test_message_queue_push_info(&queue, 3) == 3); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 3046 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3047 | TEST_ASSERT(mbedtls_test_message_queue_pop_info(&queue, 1) == 1); |
| 3048 | TEST_ASSERT(mbedtls_test_message_queue_pop_info(&queue, 2) == 2); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 3049 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3050 | TEST_ASSERT(mbedtls_test_message_queue_push_info(&queue, 5) == 5); |
| 3051 | TEST_ASSERT(mbedtls_test_message_queue_push_info(&queue, 8) == 8); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 3052 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3053 | TEST_ASSERT(mbedtls_test_message_queue_pop_info(&queue, 3) == 3); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 3054 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3055 | TEST_ASSERT(mbedtls_test_message_queue_pop_info(&queue, 5) == 5); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 3056 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3057 | TEST_ASSERT(mbedtls_test_message_queue_pop_info(&queue, 8) == 8); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 3058 | |
| 3059 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3060 | mbedtls_test_message_queue_free(&queue); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 3061 | } |
| 3062 | /* END_CASE */ |
| 3063 | |
| 3064 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3065 | void ssl_message_queue_insufficient_buffer() |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 3066 | { |
| 3067 | mbedtls_test_message_queue queue; |
| 3068 | size_t message_len = 10; |
| 3069 | size_t buffer_len = 5; |
| 3070 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3071 | TEST_ASSERT(mbedtls_test_message_queue_setup(&queue, 1) == 0); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 3072 | |
| 3073 | /* Popping without a sufficient buffer */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3074 | TEST_ASSERT(mbedtls_test_message_queue_push_info(&queue, message_len) |
| 3075 | == (int) message_len); |
| 3076 | TEST_ASSERT(mbedtls_test_message_queue_pop_info(&queue, buffer_len) |
| 3077 | == (int) buffer_len); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 3078 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3079 | mbedtls_test_message_queue_free(&queue); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 3080 | } |
| 3081 | /* END_CASE */ |
| 3082 | |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3083 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3084 | void ssl_message_mock_uninitialized() |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3085 | { |
| 3086 | enum { MSGLEN = 10 }; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3087 | unsigned char message[MSGLEN] = { 0 }, received[MSGLEN]; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3088 | mbedtls_mock_socket client, server; |
| 3089 | mbedtls_test_message_queue server_queue, client_queue; |
| 3090 | mbedtls_test_message_socket_context server_context, client_context; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3091 | mbedtls_message_socket_init(&server_context); |
| 3092 | mbedtls_message_socket_init(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3093 | |
| 3094 | /* Send with a NULL context */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3095 | TEST_ASSERT(mbedtls_mock_tcp_send_msg(NULL, message, MSGLEN) |
| 3096 | == MBEDTLS_TEST_ERROR_CONTEXT_ERROR); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3097 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3098 | TEST_ASSERT(mbedtls_mock_tcp_recv_msg(NULL, message, MSGLEN) |
| 3099 | == MBEDTLS_TEST_ERROR_CONTEXT_ERROR); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3100 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3101 | TEST_ASSERT(mbedtls_message_socket_setup(&server_queue, &client_queue, 1, |
| 3102 | &server, |
| 3103 | &server_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3104 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3105 | TEST_ASSERT(mbedtls_message_socket_setup(&client_queue, &server_queue, 1, |
| 3106 | &client, |
| 3107 | &client_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3108 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3109 | TEST_ASSERT(mbedtls_mock_tcp_send_msg(&client_context, message, MSGLEN) |
| 3110 | == MBEDTLS_TEST_ERROR_SEND_FAILED); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3111 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3112 | TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&server_context, received, MSGLEN) |
| 3113 | == MBEDTLS_ERR_SSL_WANT_READ); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3114 | |
| 3115 | /* Push directly to a queue to later simulate a disconnected behavior */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3116 | TEST_ASSERT(mbedtls_test_message_queue_push_info(&server_queue, MSGLEN) |
| 3117 | == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3118 | |
| 3119 | /* Test if there's an error when trying to read from a disconnected |
| 3120 | * socket */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3121 | TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&server_context, received, MSGLEN) |
| 3122 | == MBEDTLS_TEST_ERROR_RECV_FAILED); |
| 3123 | exit: |
| 3124 | mbedtls_message_socket_close(&server_context); |
| 3125 | mbedtls_message_socket_close(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3126 | } |
| 3127 | /* END_CASE */ |
| 3128 | |
| 3129 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3130 | void ssl_message_mock_basic() |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3131 | { |
| 3132 | enum { MSGLEN = 10 }; |
| 3133 | unsigned char message[MSGLEN], received[MSGLEN]; |
| 3134 | mbedtls_mock_socket client, server; |
| 3135 | unsigned i; |
| 3136 | mbedtls_test_message_queue server_queue, client_queue; |
| 3137 | mbedtls_test_message_socket_context server_context, client_context; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3138 | mbedtls_message_socket_init(&server_context); |
| 3139 | mbedtls_message_socket_init(&client_context); |
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 | TEST_ASSERT(mbedtls_message_socket_setup(&server_queue, &client_queue, 1, |
| 3142 | &server, |
| 3143 | &server_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3144 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3145 | TEST_ASSERT(mbedtls_message_socket_setup(&client_queue, &server_queue, 1, |
| 3146 | &client, |
| 3147 | &client_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3148 | |
| 3149 | /* Fill up the buffer with structured data so that unwanted changes |
| 3150 | * can be detected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3151 | for (i = 0; i < MSGLEN; i++) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3152 | message[i] = i & 0xFF; |
| 3153 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3154 | TEST_ASSERT(0 == mbedtls_mock_socket_connect(&client, &server, |
| 3155 | MSGLEN)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3156 | |
| 3157 | /* Send the message to the server */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3158 | TEST_ASSERT(mbedtls_mock_tcp_send_msg(&client_context, message, |
| 3159 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3160 | |
| 3161 | /* Read from the server */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3162 | TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&server_context, received, MSGLEN) |
| 3163 | == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3164 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3165 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
| 3166 | memset(received, 0, MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3167 | |
| 3168 | /* Send the message to the client */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3169 | TEST_ASSERT(mbedtls_mock_tcp_send_msg(&server_context, message, |
| 3170 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3171 | |
| 3172 | /* Read from the client */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3173 | TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&client_context, received, MSGLEN) |
| 3174 | == MSGLEN); |
| 3175 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3176 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3177 | exit: |
| 3178 | mbedtls_message_socket_close(&server_context); |
| 3179 | mbedtls_message_socket_close(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3180 | } |
| 3181 | /* END_CASE */ |
| 3182 | |
| 3183 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3184 | void ssl_message_mock_queue_overflow_underflow() |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3185 | { |
| 3186 | enum { MSGLEN = 10 }; |
| 3187 | unsigned char message[MSGLEN], received[MSGLEN]; |
| 3188 | mbedtls_mock_socket client, server; |
| 3189 | unsigned i; |
| 3190 | mbedtls_test_message_queue server_queue, client_queue; |
| 3191 | mbedtls_test_message_socket_context server_context, client_context; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3192 | mbedtls_message_socket_init(&server_context); |
| 3193 | mbedtls_message_socket_init(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3194 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3195 | TEST_ASSERT(mbedtls_message_socket_setup(&server_queue, &client_queue, 2, |
| 3196 | &server, |
| 3197 | &server_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3198 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3199 | TEST_ASSERT(mbedtls_message_socket_setup(&client_queue, &server_queue, 2, |
| 3200 | &client, |
| 3201 | &client_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3202 | |
| 3203 | /* Fill up the buffer with structured data so that unwanted changes |
| 3204 | * can be detected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3205 | for (i = 0; i < MSGLEN; i++) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3206 | message[i] = i & 0xFF; |
| 3207 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3208 | TEST_ASSERT(0 == mbedtls_mock_socket_connect(&client, &server, |
| 3209 | MSGLEN*2)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3210 | |
| 3211 | /* Send three message to the server, last one with an error */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3212 | TEST_ASSERT(mbedtls_mock_tcp_send_msg(&client_context, message, |
| 3213 | MSGLEN - 1) == MSGLEN - 1); |
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 | TEST_ASSERT(mbedtls_mock_tcp_send_msg(&client_context, message, |
| 3216 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3217 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3218 | TEST_ASSERT(mbedtls_mock_tcp_send_msg(&client_context, message, |
| 3219 | MSGLEN) |
| 3220 | == MBEDTLS_ERR_SSL_WANT_WRITE); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3221 | |
| 3222 | /* Read three messages from the server, last one with an error */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3223 | TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&server_context, received, |
| 3224 | MSGLEN - 1) == MSGLEN - 1); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3225 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3226 | TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&server_context, received, MSGLEN) |
| 3227 | == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3228 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3229 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3230 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3231 | TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&server_context, received, MSGLEN) |
| 3232 | == MBEDTLS_ERR_SSL_WANT_READ); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3233 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3234 | exit: |
| 3235 | mbedtls_message_socket_close(&server_context); |
| 3236 | mbedtls_message_socket_close(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3237 | } |
| 3238 | /* END_CASE */ |
| 3239 | |
| 3240 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3241 | void ssl_message_mock_socket_overflow() |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3242 | { |
| 3243 | enum { MSGLEN = 10 }; |
| 3244 | unsigned char message[MSGLEN], received[MSGLEN]; |
| 3245 | mbedtls_mock_socket client, server; |
| 3246 | unsigned i; |
| 3247 | mbedtls_test_message_queue server_queue, client_queue; |
| 3248 | mbedtls_test_message_socket_context server_context, client_context; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3249 | mbedtls_message_socket_init(&server_context); |
| 3250 | mbedtls_message_socket_init(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3251 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3252 | TEST_ASSERT(mbedtls_message_socket_setup(&server_queue, &client_queue, 2, |
| 3253 | &server, |
| 3254 | &server_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3255 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3256 | TEST_ASSERT(mbedtls_message_socket_setup(&client_queue, &server_queue, 2, |
| 3257 | &client, |
| 3258 | &client_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3259 | |
| 3260 | /* Fill up the buffer with structured data so that unwanted changes |
| 3261 | * can be detected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3262 | for (i = 0; i < MSGLEN; i++) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3263 | message[i] = i & 0xFF; |
| 3264 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3265 | TEST_ASSERT(0 == mbedtls_mock_socket_connect(&client, &server, |
| 3266 | MSGLEN)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3267 | |
| 3268 | /* Send two message to the server, second one with an error */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3269 | TEST_ASSERT(mbedtls_mock_tcp_send_msg(&client_context, message, |
| 3270 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3271 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3272 | TEST_ASSERT(mbedtls_mock_tcp_send_msg(&client_context, message, |
| 3273 | MSGLEN) |
| 3274 | == MBEDTLS_TEST_ERROR_SEND_FAILED); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3275 | |
| 3276 | /* Read the only message from the server */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3277 | TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&server_context, received, MSGLEN) |
| 3278 | == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3279 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3280 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3281 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3282 | exit: |
| 3283 | mbedtls_message_socket_close(&server_context); |
| 3284 | mbedtls_message_socket_close(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3285 | } |
| 3286 | /* END_CASE */ |
| 3287 | |
| 3288 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3289 | void ssl_message_mock_truncated() |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3290 | { |
| 3291 | enum { MSGLEN = 10 }; |
| 3292 | unsigned char message[MSGLEN], received[MSGLEN]; |
| 3293 | mbedtls_mock_socket client, server; |
| 3294 | unsigned i; |
| 3295 | mbedtls_test_message_queue server_queue, client_queue; |
| 3296 | mbedtls_test_message_socket_context server_context, client_context; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3297 | mbedtls_message_socket_init(&server_context); |
| 3298 | mbedtls_message_socket_init(&client_context); |
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 | TEST_ASSERT(mbedtls_message_socket_setup(&server_queue, &client_queue, 2, |
| 3301 | &server, |
| 3302 | &server_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3303 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3304 | TEST_ASSERT(mbedtls_message_socket_setup(&client_queue, &server_queue, 2, |
| 3305 | &client, |
| 3306 | &client_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3307 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3308 | memset(received, 0, MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3309 | /* Fill up the buffer with structured data so that unwanted changes |
| 3310 | * can be detected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3311 | for (i = 0; i < MSGLEN; i++) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3312 | message[i] = i & 0xFF; |
| 3313 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3314 | TEST_ASSERT(0 == mbedtls_mock_socket_connect(&client, &server, |
| 3315 | 2 * MSGLEN)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3316 | |
| 3317 | /* Send two messages to the server, the second one small enough to fit in the |
| 3318 | * receiver's buffer. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3319 | TEST_ASSERT(mbedtls_mock_tcp_send_msg(&client_context, message, |
| 3320 | MSGLEN) == MSGLEN); |
| 3321 | TEST_ASSERT(mbedtls_mock_tcp_send_msg(&client_context, message, |
| 3322 | MSGLEN / 2) == MSGLEN / 2); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3323 | /* Read a truncated message from the server */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3324 | TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&server_context, received, MSGLEN/2) |
| 3325 | == MSGLEN/2); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3326 | |
| 3327 | /* 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] | 3328 | TEST_ASSERT(memcmp(message, received, MSGLEN/2) == 0); |
| 3329 | TEST_ASSERT(memcmp(message + MSGLEN/2, received + MSGLEN/2, MSGLEN/2) |
| 3330 | != 0); |
| 3331 | memset(received, 0, MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3332 | |
| 3333 | /* Read a full message from the server */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3334 | TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&server_context, received, MSGLEN/2) |
| 3335 | == MSGLEN / 2); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3336 | |
| 3337 | /* Test that the first half of the message is valid */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3338 | TEST_ASSERT(memcmp(message, received, MSGLEN/2) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3339 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3340 | exit: |
| 3341 | mbedtls_message_socket_close(&server_context); |
| 3342 | mbedtls_message_socket_close(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3343 | } |
| 3344 | /* END_CASE */ |
| 3345 | |
| 3346 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3347 | void ssl_message_mock_socket_read_error() |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3348 | { |
| 3349 | enum { MSGLEN = 10 }; |
| 3350 | unsigned char message[MSGLEN], received[MSGLEN]; |
| 3351 | mbedtls_mock_socket client, server; |
| 3352 | unsigned i; |
| 3353 | mbedtls_test_message_queue server_queue, client_queue; |
| 3354 | mbedtls_test_message_socket_context server_context, client_context; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3355 | mbedtls_message_socket_init(&server_context); |
| 3356 | mbedtls_message_socket_init(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3357 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3358 | TEST_ASSERT(mbedtls_message_socket_setup(&server_queue, &client_queue, 1, |
| 3359 | &server, |
| 3360 | &server_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3361 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3362 | TEST_ASSERT(mbedtls_message_socket_setup(&client_queue, &server_queue, 1, |
| 3363 | &client, |
| 3364 | &client_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3365 | |
| 3366 | /* Fill up the buffer with structured data so that unwanted changes |
| 3367 | * can be detected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3368 | for (i = 0; i < MSGLEN; i++) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3369 | message[i] = i & 0xFF; |
| 3370 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3371 | TEST_ASSERT(0 == mbedtls_mock_socket_connect(&client, &server, |
| 3372 | MSGLEN)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3373 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3374 | TEST_ASSERT(mbedtls_mock_tcp_send_msg(&client_context, message, |
| 3375 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3376 | |
| 3377 | /* Force a read error by disconnecting the socket by hand */ |
| 3378 | server.status = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3379 | TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&server_context, received, MSGLEN) |
| 3380 | == MBEDTLS_TEST_ERROR_RECV_FAILED); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3381 | /* Return to a valid state */ |
| 3382 | server.status = MBEDTLS_MOCK_SOCKET_CONNECTED; |
| 3383 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3384 | memset(received, 0, sizeof(received)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3385 | |
| 3386 | /* Test that even though the server tried to read once disconnected, the |
| 3387 | * continuity is preserved */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3388 | TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&server_context, received, MSGLEN) |
| 3389 | == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3390 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3391 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3392 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3393 | exit: |
| 3394 | mbedtls_message_socket_close(&server_context); |
| 3395 | mbedtls_message_socket_close(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3396 | } |
| 3397 | /* END_CASE */ |
| 3398 | |
| 3399 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3400 | void ssl_message_mock_interleaved_one_way() |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3401 | { |
| 3402 | enum { MSGLEN = 10 }; |
| 3403 | unsigned char message[MSGLEN], received[MSGLEN]; |
| 3404 | mbedtls_mock_socket client, server; |
| 3405 | unsigned i; |
| 3406 | mbedtls_test_message_queue server_queue, client_queue; |
| 3407 | mbedtls_test_message_socket_context server_context, client_context; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3408 | mbedtls_message_socket_init(&server_context); |
| 3409 | mbedtls_message_socket_init(&client_context); |
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 | TEST_ASSERT(mbedtls_message_socket_setup(&server_queue, &client_queue, 3, |
| 3412 | &server, |
| 3413 | &server_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3414 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3415 | TEST_ASSERT(mbedtls_message_socket_setup(&client_queue, &server_queue, 3, |
| 3416 | &client, |
| 3417 | &client_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3418 | |
| 3419 | /* Fill up the buffer with structured data so that unwanted changes |
| 3420 | * can be detected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3421 | for (i = 0; i < MSGLEN; i++) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3422 | message[i] = i & 0xFF; |
| 3423 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3424 | TEST_ASSERT(0 == mbedtls_mock_socket_connect(&client, &server, |
| 3425 | MSGLEN*3)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3426 | |
| 3427 | /* Interleaved test - [2 sends, 1 read] twice, and then two reads |
| 3428 | * (to wrap around the buffer) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3429 | for (i = 0; i < 2; i++) { |
| 3430 | TEST_ASSERT(mbedtls_mock_tcp_send_msg(&client_context, message, |
| 3431 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3432 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3433 | TEST_ASSERT(mbedtls_mock_tcp_send_msg(&client_context, message, |
| 3434 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3435 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3436 | TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&server_context, received, |
| 3437 | MSGLEN) == MSGLEN); |
| 3438 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
| 3439 | memset(received, 0, sizeof(received)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3440 | } |
| 3441 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3442 | for (i = 0; i < 2; i++) { |
| 3443 | TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&server_context, received, |
| 3444 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3445 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3446 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3447 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3448 | TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&server_context, received, MSGLEN) |
| 3449 | == MBEDTLS_ERR_SSL_WANT_READ); |
| 3450 | exit: |
| 3451 | mbedtls_message_socket_close(&server_context); |
| 3452 | mbedtls_message_socket_close(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3453 | } |
| 3454 | /* END_CASE */ |
| 3455 | |
| 3456 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3457 | void ssl_message_mock_interleaved_two_ways() |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3458 | { |
| 3459 | enum { MSGLEN = 10 }; |
| 3460 | unsigned char message[MSGLEN], received[MSGLEN]; |
| 3461 | mbedtls_mock_socket client, server; |
| 3462 | unsigned i; |
| 3463 | mbedtls_test_message_queue server_queue, client_queue; |
| 3464 | mbedtls_test_message_socket_context server_context, client_context; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3465 | mbedtls_message_socket_init(&server_context); |
| 3466 | mbedtls_message_socket_init(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3467 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3468 | TEST_ASSERT(mbedtls_message_socket_setup(&server_queue, &client_queue, 3, |
| 3469 | &server, |
| 3470 | &server_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3471 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3472 | TEST_ASSERT(mbedtls_message_socket_setup(&client_queue, &server_queue, 3, |
| 3473 | &client, |
| 3474 | &client_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3475 | |
| 3476 | /* Fill up the buffer with structured data so that unwanted changes |
| 3477 | * can be detected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3478 | for (i = 0; i < MSGLEN; i++) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3479 | message[i] = i & 0xFF; |
| 3480 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3481 | TEST_ASSERT(0 == mbedtls_mock_socket_connect(&client, &server, |
| 3482 | MSGLEN*3)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3483 | |
| 3484 | /* Interleaved test - [2 sends, 1 read] twice, both ways, and then two reads |
| 3485 | * (to wrap around the buffer) both ways. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3486 | for (i = 0; i < 2; i++) { |
| 3487 | TEST_ASSERT(mbedtls_mock_tcp_send_msg(&client_context, message, |
| 3488 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3489 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3490 | TEST_ASSERT(mbedtls_mock_tcp_send_msg(&client_context, message, |
| 3491 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3492 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3493 | TEST_ASSERT(mbedtls_mock_tcp_send_msg(&server_context, message, |
| 3494 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3495 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3496 | TEST_ASSERT(mbedtls_mock_tcp_send_msg(&server_context, message, |
| 3497 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3498 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3499 | TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&server_context, received, |
| 3500 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3501 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3502 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3503 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3504 | memset(received, 0, sizeof(received)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3505 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3506 | TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&client_context, received, |
| 3507 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3508 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3509 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3510 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3511 | memset(received, 0, sizeof(received)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3512 | } |
| 3513 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3514 | for (i = 0; i < 2; i++) { |
| 3515 | TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&server_context, received, |
| 3516 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3517 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3518 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
| 3519 | memset(received, 0, sizeof(received)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3520 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3521 | TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&client_context, received, |
| 3522 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3523 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3524 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
| 3525 | memset(received, 0, sizeof(received)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3526 | } |
| 3527 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3528 | TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&server_context, received, MSGLEN) |
| 3529 | == MBEDTLS_ERR_SSL_WANT_READ); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3530 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3531 | TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&client_context, received, MSGLEN) |
| 3532 | == MBEDTLS_ERR_SSL_WANT_READ); |
| 3533 | exit: |
| 3534 | mbedtls_message_socket_close(&server_context); |
| 3535 | mbedtls_message_socket_close(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3536 | } |
| 3537 | /* END_CASE */ |
| 3538 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3539 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3540 | 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] | 3541 | { |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 3542 | uint32_t len = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3543 | mbedtls_ssl_context ssl; |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 3544 | mbedtls_ssl_config conf; |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3545 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3546 | mbedtls_ssl_init(&ssl); |
| 3547 | mbedtls_ssl_config_init(&conf); |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 3548 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3549 | TEST_ASSERT(mbedtls_ssl_config_defaults(&conf, |
| 3550 | MBEDTLS_SSL_IS_CLIENT, |
| 3551 | MBEDTLS_SSL_TRANSPORT_DATAGRAM, |
| 3552 | MBEDTLS_SSL_PRESET_DEFAULT) == 0); |
| 3553 | TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0); |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3554 | |
| 3555 | /* Read previous record numbers */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3556 | for (len = 0; len < prevs->len; len += 6) { |
| 3557 | memcpy(ssl.in_ctr + 2, prevs->x + len, 6); |
| 3558 | mbedtls_ssl_dtls_replay_update(&ssl); |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3559 | } |
| 3560 | |
| 3561 | /* Check new number */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3562 | memcpy(ssl.in_ctr + 2, new->x, 6); |
| 3563 | TEST_ASSERT(mbedtls_ssl_dtls_replay_check(&ssl) == ret); |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3564 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3565 | mbedtls_ssl_free(&ssl); |
| 3566 | mbedtls_ssl_config_free(&conf); |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3567 | } |
| 3568 | /* END_CASE */ |
Hanno Becker | b25c0c7 | 2017-05-05 11:24:30 +0100 | [diff] [blame] | 3569 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 3570 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3571 | void ssl_set_hostname_twice(char *hostname0, char *hostname1) |
Hanno Becker | b25c0c7 | 2017-05-05 11:24:30 +0100 | [diff] [blame] | 3572 | { |
| 3573 | mbedtls_ssl_context ssl; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3574 | mbedtls_ssl_init(&ssl); |
Hanno Becker | b25c0c7 | 2017-05-05 11:24:30 +0100 | [diff] [blame] | 3575 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3576 | TEST_ASSERT(mbedtls_ssl_set_hostname(&ssl, hostname0) == 0); |
| 3577 | TEST_ASSERT(mbedtls_ssl_set_hostname(&ssl, hostname1) == 0); |
Hanno Becker | b25c0c7 | 2017-05-05 11:24:30 +0100 | [diff] [blame] | 3578 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3579 | mbedtls_ssl_free(&ssl); |
Hanno Becker | b25c0c7 | 2017-05-05 11:24:30 +0100 | [diff] [blame] | 3580 | } |
Darryl Green | 11999bb | 2018-03-13 15:22:58 +0000 | [diff] [blame] | 3581 | /* END_CASE */ |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3582 | |
| 3583 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3584 | void ssl_crypt_record(int cipher_type, int hash_id, |
| 3585 | int etm, int tag_mode, int ver, |
| 3586 | int cid0_len, int cid1_len) |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3587 | { |
| 3588 | /* |
| 3589 | * Test several record encryptions and decryptions |
| 3590 | * with plenty of space before and after the data |
| 3591 | * within the record buffer. |
| 3592 | */ |
| 3593 | |
| 3594 | int ret; |
| 3595 | int num_records = 16; |
| 3596 | mbedtls_ssl_context ssl; /* ONLY for debugging */ |
| 3597 | |
| 3598 | mbedtls_ssl_transform t0, t1; |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 3599 | unsigned char *buf = NULL; |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3600 | size_t const buflen = 512; |
| 3601 | mbedtls_record rec, rec_backup; |
| 3602 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3603 | USE_PSA_INIT(); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 3604 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3605 | mbedtls_ssl_init(&ssl); |
| 3606 | mbedtls_ssl_transform_init(&t0); |
| 3607 | mbedtls_ssl_transform_init(&t1); |
| 3608 | ret = build_transforms(&t0, &t1, cipher_type, hash_id, |
| 3609 | etm, tag_mode, ver, |
| 3610 | (size_t) cid0_len, |
| 3611 | (size_t) cid1_len); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 3612 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3613 | TEST_ASSERT(ret == 0); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3614 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3615 | TEST_ASSERT((buf = mbedtls_calloc(1, buflen)) != NULL); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3616 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3617 | while (num_records-- > 0) { |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3618 | mbedtls_ssl_transform *t_dec, *t_enc; |
| 3619 | /* Take turns in who's sending and who's receiving. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3620 | if (num_records % 3 == 0) { |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3621 | t_dec = &t0; |
| 3622 | t_enc = &t1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3623 | } else { |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3624 | t_dec = &t1; |
| 3625 | t_enc = &t0; |
| 3626 | } |
| 3627 | |
| 3628 | /* |
| 3629 | * The record header affects the transformation in two ways: |
| 3630 | * 1) It determines the AEAD additional data |
| 3631 | * 2) The record counter sometimes determines the IV. |
| 3632 | * |
| 3633 | * Apart from that, the fields don't have influence. |
| 3634 | * In particular, it is currently not the responsibility |
| 3635 | * of ssl_encrypt/decrypt_buf to check if the transform |
| 3636 | * version matches the record version, or that the |
| 3637 | * type is sensible. |
| 3638 | */ |
| 3639 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3640 | memset(rec.ctr, num_records, sizeof(rec.ctr)); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3641 | rec.type = 42; |
| 3642 | rec.ver[0] = num_records; |
| 3643 | rec.ver[1] = num_records; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3644 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 3645 | rec.cid_len = 0; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3646 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3647 | |
| 3648 | rec.buf = buf; |
| 3649 | rec.buf_len = buflen; |
| 3650 | rec.data_offset = 16; |
| 3651 | /* Make sure to vary the length to exercise different |
| 3652 | * paddings. */ |
| 3653 | rec.data_len = 1 + num_records; |
| 3654 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3655 | memset(rec.buf + rec.data_offset, 42, rec.data_len); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3656 | |
| 3657 | /* Make a copy for later comparison */ |
| 3658 | rec_backup = rec; |
| 3659 | |
| 3660 | /* Encrypt record */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3661 | ret = mbedtls_ssl_encrypt_buf(&ssl, t_enc, &rec, |
| 3662 | mbedtls_test_rnd_std_rand, NULL); |
| 3663 | TEST_ASSERT(ret == 0 || ret == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL); |
| 3664 | if (ret != 0) { |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3665 | continue; |
| 3666 | } |
| 3667 | |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 3668 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3669 | if (rec.cid_len != 0) { |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 3670 | /* DTLS 1.2 + CID hides the real content type and |
| 3671 | * uses a special CID content type in the protected |
| 3672 | * record. Double-check this. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3673 | TEST_ASSERT(rec.type == MBEDTLS_SSL_MSG_CID); |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 3674 | } |
| 3675 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 3676 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3677 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3678 | if (t_enc->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 3679 | /* TLS 1.3 hides the real content type and |
| 3680 | * always uses Application Data as the content type |
| 3681 | * for protected records. Double-check this. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3682 | TEST_ASSERT(rec.type == MBEDTLS_SSL_MSG_APPLICATION_DATA); |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 3683 | } |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3684 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 3685 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3686 | /* Decrypt record with t_dec */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3687 | ret = mbedtls_ssl_decrypt_buf(&ssl, t_dec, &rec); |
| 3688 | TEST_ASSERT(ret == 0); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3689 | |
| 3690 | /* Compare results */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3691 | TEST_ASSERT(rec.type == rec_backup.type); |
| 3692 | TEST_ASSERT(memcmp(rec.ctr, rec_backup.ctr, 8) == 0); |
| 3693 | TEST_ASSERT(rec.ver[0] == rec_backup.ver[0]); |
| 3694 | TEST_ASSERT(rec.ver[1] == rec_backup.ver[1]); |
| 3695 | TEST_ASSERT(rec.data_len == rec_backup.data_len); |
| 3696 | TEST_ASSERT(rec.data_offset == rec_backup.data_offset); |
| 3697 | TEST_ASSERT(memcmp(rec.buf + rec.data_offset, |
| 3698 | rec_backup.buf + rec_backup.data_offset, |
| 3699 | rec.data_len) == 0); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3700 | } |
| 3701 | |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 3702 | exit: |
| 3703 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3704 | /* Cleanup */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3705 | mbedtls_ssl_free(&ssl); |
| 3706 | mbedtls_ssl_transform_free(&t0); |
| 3707 | mbedtls_ssl_transform_free(&t1); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3708 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3709 | mbedtls_free(buf); |
| 3710 | USE_PSA_DONE(); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3711 | } |
| 3712 | /* END_CASE */ |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3713 | |
| 3714 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3715 | void ssl_crypt_record_small(int cipher_type, int hash_id, |
| 3716 | int etm, int tag_mode, int ver, |
| 3717 | int cid0_len, int cid1_len) |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3718 | { |
| 3719 | /* |
| 3720 | * Test pairs of encryption and decryption with an increasing |
| 3721 | * amount of space in the record buffer - in more detail: |
| 3722 | * 1) Try to encrypt with 0, 1, 2, ... bytes available |
| 3723 | * in front of the plaintext, and expect the encryption |
| 3724 | * to succeed starting from some offset. Always keep |
| 3725 | * enough space in the end of the buffer. |
| 3726 | * 2) Try to encrypt with 0, 1, 2, ... bytes available |
| 3727 | * at the end of the plaintext, and expect the encryption |
| 3728 | * to succeed starting from some offset. Always keep |
| 3729 | * enough space at the beginning of the buffer. |
| 3730 | * 3) Try to encrypt with 0, 1, 2, ... bytes available |
| 3731 | * both at the front and end of the plaintext, |
| 3732 | * and expect the encryption to succeed starting from |
| 3733 | * some offset. |
| 3734 | * |
| 3735 | * If encryption succeeds, check that decryption succeeds |
| 3736 | * and yields the original record. |
| 3737 | */ |
| 3738 | |
| 3739 | mbedtls_ssl_context ssl; /* ONLY for debugging */ |
| 3740 | |
| 3741 | mbedtls_ssl_transform t0, t1; |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 3742 | unsigned char *buf = NULL; |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 3743 | size_t const buflen = 256; |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3744 | mbedtls_record rec, rec_backup; |
| 3745 | |
| 3746 | int ret; |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 3747 | int mode; /* Mode 1, 2 or 3 as explained above */ |
| 3748 | size_t offset; /* Available space at beginning/end/both */ |
| 3749 | size_t threshold = 96; /* Maximum offset to test against */ |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3750 | |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 3751 | size_t default_pre_padding = 64; /* Pre-padding to use in mode 2 */ |
| 3752 | 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] | 3753 | |
| 3754 | int seen_success; /* Indicates if in the current mode we've |
| 3755 | * already seen a successful test. */ |
| 3756 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3757 | USE_PSA_INIT(); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 3758 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3759 | mbedtls_ssl_init(&ssl); |
| 3760 | mbedtls_ssl_transform_init(&t0); |
| 3761 | mbedtls_ssl_transform_init(&t1); |
| 3762 | ret = build_transforms(&t0, &t1, cipher_type, hash_id, |
| 3763 | etm, tag_mode, ver, |
| 3764 | (size_t) cid0_len, |
| 3765 | (size_t) cid1_len); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 3766 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3767 | TEST_ASSERT(ret == 0); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3768 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3769 | TEST_ASSERT((buf = mbedtls_calloc(1, buflen)) != NULL); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3770 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3771 | for (mode = 1; mode <= 3; mode++) { |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3772 | seen_success = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3773 | for (offset = 0; offset <= threshold; offset++) { |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3774 | mbedtls_ssl_transform *t_dec, *t_enc; |
Hanno Becker | 6c87b3f | 2019-04-29 17:24:44 +0100 | [diff] [blame] | 3775 | t_dec = &t0; |
| 3776 | t_enc = &t1; |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3777 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3778 | memset(rec.ctr, offset, sizeof(rec.ctr)); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3779 | rec.type = 42; |
| 3780 | rec.ver[0] = offset; |
| 3781 | rec.ver[1] = offset; |
| 3782 | rec.buf = buf; |
| 3783 | rec.buf_len = buflen; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3784 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 3785 | rec.cid_len = 0; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3786 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3787 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3788 | switch (mode) { |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3789 | case 1: /* Space in the beginning */ |
| 3790 | rec.data_offset = offset; |
| 3791 | rec.data_len = buflen - offset - default_post_padding; |
| 3792 | break; |
| 3793 | |
| 3794 | case 2: /* Space in the end */ |
| 3795 | rec.data_offset = default_pre_padding; |
| 3796 | rec.data_len = buflen - default_pre_padding - offset; |
| 3797 | break; |
| 3798 | |
| 3799 | case 3: /* Space in the beginning and end */ |
| 3800 | rec.data_offset = offset; |
| 3801 | rec.data_len = buflen - 2 * offset; |
| 3802 | break; |
| 3803 | |
| 3804 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3805 | TEST_ASSERT(0); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3806 | break; |
| 3807 | } |
| 3808 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3809 | memset(rec.buf + rec.data_offset, 42, rec.data_len); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3810 | |
| 3811 | /* Make a copy for later comparison */ |
| 3812 | rec_backup = rec; |
| 3813 | |
| 3814 | /* Encrypt record */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3815 | ret = mbedtls_ssl_encrypt_buf(&ssl, t_enc, &rec, |
| 3816 | mbedtls_test_rnd_std_rand, NULL); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3817 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3818 | if ((mode == 1 || mode == 2) && seen_success) { |
| 3819 | TEST_ASSERT(ret == 0); |
| 3820 | } else { |
| 3821 | TEST_ASSERT(ret == 0 || ret == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL); |
| 3822 | if (ret == 0) { |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3823 | seen_success = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3824 | } |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3825 | } |
| 3826 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3827 | if (ret != 0) { |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3828 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3829 | } |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3830 | |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 3831 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3832 | if (rec.cid_len != 0) { |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 3833 | /* DTLS 1.2 + CID hides the real content type and |
| 3834 | * uses a special CID content type in the protected |
| 3835 | * record. Double-check this. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3836 | TEST_ASSERT(rec.type == MBEDTLS_SSL_MSG_CID); |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 3837 | } |
| 3838 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 3839 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3840 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3841 | if (t_enc->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 3842 | /* TLS 1.3 hides the real content type and |
| 3843 | * always uses Application Data as the content type |
| 3844 | * for protected records. Double-check this. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3845 | TEST_ASSERT(rec.type == MBEDTLS_SSL_MSG_APPLICATION_DATA); |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 3846 | } |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 3847 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 3848 | |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3849 | /* Decrypt record with t_dec */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3850 | TEST_ASSERT(mbedtls_ssl_decrypt_buf(&ssl, t_dec, &rec) == 0); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3851 | |
| 3852 | /* Compare results */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3853 | TEST_ASSERT(rec.type == rec_backup.type); |
| 3854 | TEST_ASSERT(memcmp(rec.ctr, rec_backup.ctr, 8) == 0); |
| 3855 | TEST_ASSERT(rec.ver[0] == rec_backup.ver[0]); |
| 3856 | TEST_ASSERT(rec.ver[1] == rec_backup.ver[1]); |
| 3857 | TEST_ASSERT(rec.data_len == rec_backup.data_len); |
| 3858 | TEST_ASSERT(rec.data_offset == rec_backup.data_offset); |
| 3859 | TEST_ASSERT(memcmp(rec.buf + rec.data_offset, |
| 3860 | rec_backup.buf + rec_backup.data_offset, |
| 3861 | rec.data_len) == 0); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3862 | } |
| 3863 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3864 | TEST_ASSERT(seen_success == 1); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3865 | } |
| 3866 | |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 3867 | exit: |
| 3868 | |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3869 | /* Cleanup */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3870 | mbedtls_ssl_free(&ssl); |
| 3871 | mbedtls_ssl_transform_free(&t0); |
| 3872 | mbedtls_ssl_transform_free(&t1); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3873 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3874 | mbedtls_free(buf); |
| 3875 | USE_PSA_DONE(); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3876 | } |
| 3877 | /* END_CASE */ |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 3878 | |
Przemyslaw Stekiel | f4facef | 2022-02-02 21:31:04 +0100 | [diff] [blame] | 3879 | /* 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] | 3880 | void ssl_decrypt_non_etm_cbc(int cipher_type, int hash_id, int trunc_hmac, |
| 3881 | int length_selector) |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3882 | { |
| 3883 | /* |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3884 | * Test record decryption for CBC without EtM, focused on the verification |
| 3885 | * of padding and MAC. |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3886 | * |
TRodziewicz | 299510e | 2021-07-09 16:55:11 +0200 | [diff] [blame] | 3887 | * Actually depends on TLS 1.2 and either AES, ARIA or Camellia, but since |
| 3888 | * the test framework doesn't support alternation in dependency statements, |
| 3889 | * just depend on AES. |
Manuel Pégourié-Gonnard | 864abbf | 2020-07-21 10:37:14 +0200 | [diff] [blame] | 3890 | * |
| 3891 | * The length_selector argument is interpreted as follows: |
| 3892 | * - if it's -1, the plaintext length is 0 and minimal padding is applied |
| 3893 | * - if it's -2, the plaintext length is 0 and maximal padding is applied |
| 3894 | * - otherwise it must be in [0, 255] and is padding_length from RFC 5246: |
| 3895 | * it's the length of the rest of the padding, that is, excluding the |
| 3896 | * byte that encodes the length. The minimal non-zero plaintext length |
| 3897 | * that gives this padding_length is automatically selected. |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3898 | */ |
| 3899 | mbedtls_ssl_context ssl; /* ONLY for debugging */ |
| 3900 | mbedtls_ssl_transform t0, t1; |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3901 | mbedtls_record rec, rec_save; |
| 3902 | unsigned char *buf = NULL, *buf_save = NULL; |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3903 | size_t buflen, olen = 0; |
Manuel Pégourié-Gonnard | 864abbf | 2020-07-21 10:37:14 +0200 | [diff] [blame] | 3904 | size_t plaintext_len, block_size, i; |
Manuel Pégourié-Gonnard | e55653f | 2020-07-22 11:42:57 +0200 | [diff] [blame] | 3905 | unsigned char padlen; /* excluding the padding_length byte */ |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3906 | unsigned char add_data[13]; |
Neil Armstrong | cf8841a | 2022-02-24 11:17:45 +0100 | [diff] [blame] | 3907 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 3908 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 3909 | size_t sign_mac_length = 0; |
Andrzej Kurek | 8c95ac4 | 2022-08-17 16:17:00 -0400 | [diff] [blame] | 3910 | unsigned char mac[PSA_HASH_MAX_SIZE]; |
| 3911 | #else |
| 3912 | unsigned char mac[MBEDTLS_MD_MAX_SIZE]; |
Neil Armstrong | cf8841a | 2022-02-24 11:17:45 +0100 | [diff] [blame] | 3913 | #endif |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3914 | int exp_ret; |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 3915 | int ret; |
Manuel Pégourié-Gonnard | 4adc04a | 2020-07-16 10:00:48 +0200 | [diff] [blame] | 3916 | const unsigned char pad_max_len = 255; /* Per the standard */ |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3917 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3918 | USE_PSA_INIT(); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 3919 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3920 | mbedtls_ssl_init(&ssl); |
| 3921 | mbedtls_ssl_transform_init(&t0); |
| 3922 | mbedtls_ssl_transform_init(&t1); |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3923 | |
| 3924 | /* Set up transforms with dummy keys */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3925 | ret = build_transforms(&t0, &t1, cipher_type, hash_id, |
| 3926 | 0, trunc_hmac, |
| 3927 | MBEDTLS_SSL_VERSION_TLS1_2, |
| 3928 | 0, 0); |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3929 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3930 | TEST_ASSERT(ret == 0); |
Przemyslaw Stekiel | 4a36dd3 | 2022-01-25 00:43:58 +0100 | [diff] [blame] | 3931 | |
Manuel Pégourié-Gonnard | 864abbf | 2020-07-21 10:37:14 +0200 | [diff] [blame] | 3932 | /* Determine padding/plaintext length */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3933 | TEST_ASSERT(length_selector >= -2 && length_selector <= 255); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3934 | block_size = t0.ivlen; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3935 | if (length_selector < 0) { |
Manuel Pégourié-Gonnard | 864abbf | 2020-07-21 10:37:14 +0200 | [diff] [blame] | 3936 | plaintext_len = 0; |
| 3937 | |
Manuel Pégourié-Gonnard | e55653f | 2020-07-22 11:42:57 +0200 | [diff] [blame] | 3938 | /* Minimal padding |
| 3939 | * The +1 is for the padding_length byte, not counted in padlen. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3940 | padlen = block_size - (t0.maclen + 1) % block_size; |
Manuel Pégourié-Gonnard | 864abbf | 2020-07-21 10:37:14 +0200 | [diff] [blame] | 3941 | |
| 3942 | /* Maximal padding? */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3943 | if (length_selector == -2) { |
| 3944 | padlen += block_size * ((pad_max_len - padlen) / block_size); |
| 3945 | } |
| 3946 | } else { |
Manuel Pégourié-Gonnard | 864abbf | 2020-07-21 10:37:14 +0200 | [diff] [blame] | 3947 | padlen = length_selector; |
| 3948 | |
Manuel Pégourié-Gonnard | e55653f | 2020-07-22 11:42:57 +0200 | [diff] [blame] | 3949 | /* Minimal non-zero plaintext_length giving desired padding. |
| 3950 | * The +1 is for the padding_length byte, not counted in padlen. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3951 | plaintext_len = block_size - (padlen + t0.maclen + 1) % block_size; |
Manuel Pégourié-Gonnard | 864abbf | 2020-07-21 10:37:14 +0200 | [diff] [blame] | 3952 | } |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3953 | |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3954 | /* Prepare a buffer for record data */ |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3955 | buflen = block_size |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3956 | + plaintext_len |
| 3957 | + t0.maclen |
| 3958 | + padlen + 1; |
| 3959 | ASSERT_ALLOC(buf, buflen); |
| 3960 | ASSERT_ALLOC(buf_save, buflen); |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3961 | |
| 3962 | /* Prepare a dummy record header */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3963 | memset(rec.ctr, 0, sizeof(rec.ctr)); |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3964 | rec.type = MBEDTLS_SSL_MSG_APPLICATION_DATA; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3965 | mbedtls_ssl_write_version(rec.ver, MBEDTLS_SSL_TRANSPORT_STREAM, |
| 3966 | MBEDTLS_SSL_VERSION_TLS1_2); |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3967 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 3968 | rec.cid_len = 0; |
| 3969 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 3970 | |
| 3971 | /* Prepare dummy record content */ |
| 3972 | rec.buf = buf; |
| 3973 | rec.buf_len = buflen; |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3974 | rec.data_offset = block_size; |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3975 | rec.data_len = plaintext_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3976 | memset(rec.buf + rec.data_offset, 42, rec.data_len); |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3977 | |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3978 | /* Serialized version of record header for MAC purposes */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3979 | memcpy(add_data, rec.ctr, 8); |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3980 | add_data[8] = rec.type; |
| 3981 | add_data[9] = rec.ver[0]; |
| 3982 | add_data[10] = rec.ver[1]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3983 | add_data[11] = (rec.data_len >> 8) & 0xff; |
| 3984 | add_data[12] = (rec.data_len >> 0) & 0xff; |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3985 | |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3986 | /* Set dummy IV */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3987 | memset(t0.iv_enc, 0x55, t0.ivlen); |
| 3988 | memcpy(rec.buf, t0.iv_enc, t0.ivlen); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3989 | |
| 3990 | /* |
| 3991 | * Prepare a pre-encryption record (with MAC and padding), and save it. |
| 3992 | */ |
| 3993 | |
| 3994 | /* MAC with additional data */ |
Neil Armstrong | cf8841a | 2022-02-24 11:17:45 +0100 | [diff] [blame] | 3995 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3996 | TEST_EQUAL(PSA_SUCCESS, psa_mac_sign_setup(&operation, |
| 3997 | t0.psa_mac_enc, |
| 3998 | t0.psa_mac_alg)); |
| 3999 | TEST_EQUAL(PSA_SUCCESS, psa_mac_update(&operation, add_data, 13)); |
| 4000 | TEST_EQUAL(PSA_SUCCESS, psa_mac_update(&operation, |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 4001 | rec.buf + rec.data_offset, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4002 | rec.data_len)); |
| 4003 | TEST_EQUAL(PSA_SUCCESS, psa_mac_sign_finish(&operation, |
| 4004 | mac, sizeof(mac), |
| 4005 | &sign_mac_length)); |
| 4006 | #else |
| 4007 | TEST_EQUAL(0, mbedtls_md_hmac_update(&t0.md_ctx_enc, add_data, 13)); |
| 4008 | TEST_EQUAL(0, mbedtls_md_hmac_update(&t0.md_ctx_enc, |
| 4009 | rec.buf + rec.data_offset, |
| 4010 | rec.data_len)); |
| 4011 | TEST_EQUAL(0, mbedtls_md_hmac_finish(&t0.md_ctx_enc, mac)); |
Neil Armstrong | cf8841a | 2022-02-24 11:17:45 +0100 | [diff] [blame] | 4012 | #endif |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 4013 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4014 | 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] | 4015 | rec.data_len += t0.maclen; |
| 4016 | |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 4017 | /* Pad */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4018 | 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] | 4019 | rec.data_len += padlen + 1; |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 4020 | |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 4021 | /* Save correct pre-encryption record */ |
| 4022 | rec_save = rec; |
| 4023 | rec_save.buf = buf_save; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4024 | memcpy(buf_save, buf, buflen); |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 4025 | |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 4026 | /* |
| 4027 | * Encrypt and decrypt the correct record, expecting success |
| 4028 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4029 | TEST_EQUAL(0, psa_cipher_encrypt_helper(&t0, t0.iv_enc, t0.ivlen, |
| 4030 | rec.buf + rec.data_offset, rec.data_len, |
| 4031 | rec.buf + rec.data_offset, &olen)); |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 4032 | rec.data_offset -= t0.ivlen; |
| 4033 | rec.data_len += t0.ivlen; |
| 4034 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4035 | TEST_EQUAL(0, mbedtls_ssl_decrypt_buf(&ssl, &t1, &rec)); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 4036 | |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 4037 | /* |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 4038 | * Modify each byte of the pre-encryption record before encrypting and |
| 4039 | * decrypting it, expecting failure every time. |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 4040 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4041 | for (i = block_size; i < buflen; i++) { |
| 4042 | mbedtls_test_set_step(i); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 4043 | |
| 4044 | /* Restore correct pre-encryption record */ |
| 4045 | rec = rec_save; |
| 4046 | rec.buf = buf; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4047 | memcpy(buf, buf_save, buflen); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 4048 | |
Manuel Pégourié-Gonnard | b51f044 | 2020-07-21 10:40:25 +0200 | [diff] [blame] | 4049 | /* 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] | 4050 | rec.buf[i] ^= 0x01; |
| 4051 | |
| 4052 | /* Encrypt */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4053 | TEST_EQUAL(0, psa_cipher_encrypt_helper(&t0, t0.iv_enc, t0.ivlen, |
| 4054 | rec.buf + rec.data_offset, rec.data_len, |
| 4055 | rec.buf + rec.data_offset, &olen)); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 4056 | rec.data_offset -= t0.ivlen; |
| 4057 | rec.data_len += t0.ivlen; |
| 4058 | |
| 4059 | /* Decrypt and expect failure */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4060 | TEST_EQUAL(MBEDTLS_ERR_SSL_INVALID_MAC, |
| 4061 | mbedtls_ssl_decrypt_buf(&ssl, &t1, &rec)); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 4062 | } |
| 4063 | |
| 4064 | /* |
| 4065 | * Use larger values of the padding bytes - with small buffers, this tests |
| 4066 | * the case where the announced padlen would be larger than the buffer |
| 4067 | * (and before that, than the buffer minus the size of the MAC), to make |
| 4068 | * sure our padding checking code does not perform any out-of-bounds reads |
| 4069 | * in this case. (With larger buffers, ie when the plaintext is long or |
| 4070 | * maximal length padding is used, this is less relevant but still doesn't |
| 4071 | * hurt to test.) |
| 4072 | * |
| 4073 | * (Start the loop with correct padding, just to double-check that record |
| 4074 | * saving did work, and that we're overwriting the correct bytes.) |
| 4075 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4076 | for (i = padlen; i <= pad_max_len; i++) { |
| 4077 | mbedtls_test_set_step(i); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 4078 | |
| 4079 | /* Restore correct pre-encryption record */ |
| 4080 | rec = rec_save; |
| 4081 | rec.buf = buf; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4082 | memcpy(buf, buf_save, buflen); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 4083 | |
| 4084 | /* Set padding bytes to new value */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4085 | memset(buf + buflen - padlen - 1, i, padlen + 1); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 4086 | |
| 4087 | /* Encrypt */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4088 | TEST_EQUAL(0, psa_cipher_encrypt_helper(&t0, t0.iv_enc, t0.ivlen, |
| 4089 | rec.buf + rec.data_offset, rec.data_len, |
| 4090 | rec.buf + rec.data_offset, &olen)); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 4091 | rec.data_offset -= t0.ivlen; |
| 4092 | rec.data_len += t0.ivlen; |
| 4093 | |
| 4094 | /* Decrypt and expect failure except the first time */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4095 | exp_ret = (i == padlen) ? 0 : MBEDTLS_ERR_SSL_INVALID_MAC; |
| 4096 | 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] | 4097 | } |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 4098 | |
| 4099 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4100 | mbedtls_ssl_free(&ssl); |
| 4101 | mbedtls_ssl_transform_free(&t0); |
| 4102 | mbedtls_ssl_transform_free(&t1); |
| 4103 | mbedtls_free(buf); |
| 4104 | mbedtls_free(buf_save); |
| 4105 | USE_PSA_DONE(); |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 4106 | } |
| 4107 | /* END_CASE */ |
| 4108 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4109 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4110 | void ssl_tls13_hkdf_expand_label(int hash_alg, |
| 4111 | data_t *secret, |
| 4112 | int label_idx, |
| 4113 | data_t *ctx, |
| 4114 | int desired_length, |
| 4115 | data_t *expected) |
Hanno Becker | 39ff492 | 2020-08-21 13:36:56 +0100 | [diff] [blame] | 4116 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4117 | unsigned char dst[100]; |
Hanno Becker | 39ff492 | 2020-08-21 13:36:56 +0100 | [diff] [blame] | 4118 | |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 4119 | unsigned char const *lbl = NULL; |
| 4120 | size_t lbl_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4121 | #define MBEDTLS_SSL_TLS1_3_LABEL(name, string) \ |
| 4122 | if (label_idx == (int) tls13_label_ ## name) \ |
Xiaofei Bai | d25fab6 | 2021-12-02 06:36:27 +0000 | [diff] [blame] | 4123 | { \ |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4124 | lbl = mbedtls_ssl_tls13_labels.name; \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4125 | lbl_len = sizeof(mbedtls_ssl_tls13_labels.name); \ |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 4126 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4127 | MBEDTLS_SSL_TLS1_3_LABEL_LIST |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 4128 | #undef MBEDTLS_SSL_TLS1_3_LABEL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4129 | TEST_ASSERT(lbl != NULL); |
Hanno Becker | 39ff492 | 2020-08-21 13:36:56 +0100 | [diff] [blame] | 4130 | |
| 4131 | /* Check sanity of test parameters. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4132 | TEST_ASSERT((size_t) desired_length <= sizeof(dst)); |
| 4133 | TEST_ASSERT((size_t) desired_length == expected->len); |
Hanno Becker | 39ff492 | 2020-08-21 13:36:56 +0100 | [diff] [blame] | 4134 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4135 | PSA_INIT(); |
Gabor Mezei | 892c4aa | 2022-03-21 12:21:43 +0100 | [diff] [blame] | 4136 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4137 | TEST_ASSERT(mbedtls_ssl_tls13_hkdf_expand_label( |
| 4138 | (psa_algorithm_t) hash_alg, |
| 4139 | secret->x, secret->len, |
| 4140 | lbl, lbl_len, |
| 4141 | ctx->x, ctx->len, |
| 4142 | dst, desired_length) == 0); |
Hanno Becker | 39ff492 | 2020-08-21 13:36:56 +0100 | [diff] [blame] | 4143 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4144 | ASSERT_COMPARE(dst, (size_t) desired_length, |
| 4145 | expected->x, (size_t) expected->len); |
Gabor Mezei | 892c4aa | 2022-03-21 12:21:43 +0100 | [diff] [blame] | 4146 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4147 | PSA_DONE(); |
Hanno Becker | 39ff492 | 2020-08-21 13:36:56 +0100 | [diff] [blame] | 4148 | } |
| 4149 | /* END_CASE */ |
| 4150 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4151 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4152 | void ssl_tls13_traffic_key_generation(int hash_alg, |
| 4153 | data_t *server_secret, |
| 4154 | data_t *client_secret, |
| 4155 | int desired_iv_len, |
| 4156 | int desired_key_len, |
| 4157 | data_t *expected_server_write_key, |
| 4158 | data_t *expected_server_write_iv, |
| 4159 | data_t *expected_client_write_key, |
| 4160 | data_t *expected_client_write_iv) |
Hanno Becker | 19498f8 | 2020-08-21 13:37:08 +0100 | [diff] [blame] | 4161 | { |
| 4162 | mbedtls_ssl_key_set keys; |
| 4163 | |
| 4164 | /* Check sanity of test parameters. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4165 | TEST_ASSERT(client_secret->len == server_secret->len); |
| 4166 | TEST_ASSERT(expected_client_write_iv->len == expected_server_write_iv->len && |
| 4167 | expected_client_write_iv->len == (size_t) desired_iv_len); |
| 4168 | TEST_ASSERT(expected_client_write_key->len == expected_server_write_key->len && |
| 4169 | expected_client_write_key->len == (size_t) desired_key_len); |
Hanno Becker | 19498f8 | 2020-08-21 13:37:08 +0100 | [diff] [blame] | 4170 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4171 | PSA_INIT(); |
Gabor Mezei | 892c4aa | 2022-03-21 12:21:43 +0100 | [diff] [blame] | 4172 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4173 | TEST_ASSERT(mbedtls_ssl_tls13_make_traffic_keys( |
| 4174 | (psa_algorithm_t) hash_alg, |
| 4175 | client_secret->x, |
| 4176 | server_secret->x, |
| 4177 | client_secret->len /* == server_secret->len */, |
| 4178 | desired_key_len, desired_iv_len, |
| 4179 | &keys) == 0); |
Hanno Becker | 19498f8 | 2020-08-21 13:37:08 +0100 | [diff] [blame] | 4180 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4181 | ASSERT_COMPARE(keys.client_write_key, |
| 4182 | keys.key_len, |
| 4183 | expected_client_write_key->x, |
| 4184 | (size_t) desired_key_len); |
| 4185 | ASSERT_COMPARE(keys.server_write_key, |
| 4186 | keys.key_len, |
| 4187 | expected_server_write_key->x, |
| 4188 | (size_t) desired_key_len); |
| 4189 | ASSERT_COMPARE(keys.client_write_iv, |
| 4190 | keys.iv_len, |
| 4191 | expected_client_write_iv->x, |
| 4192 | (size_t) desired_iv_len); |
| 4193 | ASSERT_COMPARE(keys.server_write_iv, |
| 4194 | keys.iv_len, |
| 4195 | expected_server_write_iv->x, |
| 4196 | (size_t) desired_iv_len); |
Gabor Mezei | 892c4aa | 2022-03-21 12:21:43 +0100 | [diff] [blame] | 4197 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4198 | PSA_DONE(); |
Hanno Becker | 19498f8 | 2020-08-21 13:37:08 +0100 | [diff] [blame] | 4199 | } |
| 4200 | /* END_CASE */ |
| 4201 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4202 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4203 | void ssl_tls13_derive_secret(int hash_alg, |
| 4204 | data_t *secret, |
| 4205 | int label_idx, |
| 4206 | data_t *ctx, |
| 4207 | int desired_length, |
| 4208 | int already_hashed, |
| 4209 | data_t *expected) |
Hanno Becker | e4849d1 | 2020-08-21 14:14:14 +0100 | [diff] [blame] | 4210 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4211 | unsigned char dst[100]; |
Hanno Becker | e4849d1 | 2020-08-21 14:14:14 +0100 | [diff] [blame] | 4212 | |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 4213 | unsigned char const *lbl = NULL; |
| 4214 | size_t lbl_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4215 | #define MBEDTLS_SSL_TLS1_3_LABEL(name, string) \ |
| 4216 | if (label_idx == (int) tls13_label_ ## name) \ |
Xiaofei Bai | d25fab6 | 2021-12-02 06:36:27 +0000 | [diff] [blame] | 4217 | { \ |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4218 | lbl = mbedtls_ssl_tls13_labels.name; \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4219 | lbl_len = sizeof(mbedtls_ssl_tls13_labels.name); \ |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 4220 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4221 | MBEDTLS_SSL_TLS1_3_LABEL_LIST |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 4222 | #undef MBEDTLS_SSL_TLS1_3_LABEL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4223 | TEST_ASSERT(lbl != NULL); |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 4224 | |
Hanno Becker | e4849d1 | 2020-08-21 14:14:14 +0100 | [diff] [blame] | 4225 | /* Check sanity of test parameters. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4226 | TEST_ASSERT((size_t) desired_length <= sizeof(dst)); |
| 4227 | TEST_ASSERT((size_t) desired_length == expected->len); |
Hanno Becker | e4849d1 | 2020-08-21 14:14:14 +0100 | [diff] [blame] | 4228 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4229 | PSA_INIT(); |
Gabor Mezei | 892c4aa | 2022-03-21 12:21:43 +0100 | [diff] [blame] | 4230 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4231 | TEST_ASSERT(mbedtls_ssl_tls13_derive_secret( |
| 4232 | (psa_algorithm_t) hash_alg, |
| 4233 | secret->x, secret->len, |
| 4234 | lbl, lbl_len, |
| 4235 | ctx->x, ctx->len, |
| 4236 | already_hashed, |
| 4237 | dst, desired_length) == 0); |
Hanno Becker | e4849d1 | 2020-08-21 14:14:14 +0100 | [diff] [blame] | 4238 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4239 | ASSERT_COMPARE(dst, desired_length, |
| 4240 | expected->x, desired_length); |
Gabor Mezei | 892c4aa | 2022-03-21 12:21:43 +0100 | [diff] [blame] | 4241 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4242 | PSA_DONE(); |
Hanno Becker | e4849d1 | 2020-08-21 14:14:14 +0100 | [diff] [blame] | 4243 | } |
| 4244 | /* END_CASE */ |
| 4245 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4246 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4247 | void ssl_tls13_derive_early_secrets(int hash_alg, |
| 4248 | data_t *secret, |
| 4249 | data_t *transcript, |
| 4250 | data_t *traffic_expected, |
| 4251 | data_t *exporter_expected) |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4252 | { |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4253 | mbedtls_ssl_tls13_early_secrets secrets; |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4254 | |
| 4255 | /* Double-check that we've passed sane parameters. */ |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 4256 | psa_algorithm_t alg = (psa_algorithm_t) hash_alg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4257 | size_t const hash_len = PSA_HASH_LENGTH(alg); |
| 4258 | TEST_ASSERT(PSA_ALG_IS_HASH(alg) && |
| 4259 | secret->len == hash_len && |
| 4260 | transcript->len == hash_len && |
| 4261 | traffic_expected->len == hash_len && |
| 4262 | exporter_expected->len == hash_len); |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4263 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4264 | PSA_INIT(); |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4265 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4266 | TEST_ASSERT(mbedtls_ssl_tls13_derive_early_secrets( |
| 4267 | alg, secret->x, transcript->x, transcript->len, |
| 4268 | &secrets) == 0); |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4269 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4270 | ASSERT_COMPARE(secrets.client_early_traffic_secret, hash_len, |
| 4271 | traffic_expected->x, traffic_expected->len); |
| 4272 | ASSERT_COMPARE(secrets.early_exporter_master_secret, hash_len, |
| 4273 | exporter_expected->x, exporter_expected->len); |
Gabor Mezei | 892c4aa | 2022-03-21 12:21:43 +0100 | [diff] [blame] | 4274 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4275 | PSA_DONE(); |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4276 | } |
| 4277 | /* END_CASE */ |
| 4278 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4279 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4280 | void ssl_tls13_derive_handshake_secrets(int hash_alg, |
| 4281 | data_t *secret, |
| 4282 | data_t *transcript, |
| 4283 | data_t *client_expected, |
| 4284 | data_t *server_expected) |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4285 | { |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4286 | mbedtls_ssl_tls13_handshake_secrets secrets; |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4287 | |
| 4288 | /* Double-check that we've passed sane parameters. */ |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 4289 | psa_algorithm_t alg = (psa_algorithm_t) hash_alg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4290 | size_t const hash_len = PSA_HASH_LENGTH(alg); |
| 4291 | TEST_ASSERT(PSA_ALG_IS_HASH(alg) && |
| 4292 | secret->len == hash_len && |
| 4293 | transcript->len == hash_len && |
| 4294 | client_expected->len == hash_len && |
| 4295 | server_expected->len == hash_len); |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4296 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4297 | PSA_INIT(); |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4298 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4299 | TEST_ASSERT(mbedtls_ssl_tls13_derive_handshake_secrets( |
| 4300 | alg, secret->x, transcript->x, transcript->len, |
| 4301 | &secrets) == 0); |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4302 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4303 | ASSERT_COMPARE(secrets.client_handshake_traffic_secret, hash_len, |
| 4304 | client_expected->x, client_expected->len); |
| 4305 | ASSERT_COMPARE(secrets.server_handshake_traffic_secret, hash_len, |
| 4306 | server_expected->x, server_expected->len); |
Gabor Mezei | 892c4aa | 2022-03-21 12:21:43 +0100 | [diff] [blame] | 4307 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4308 | PSA_DONE(); |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4309 | } |
| 4310 | /* END_CASE */ |
| 4311 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4312 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4313 | void ssl_tls13_derive_application_secrets(int hash_alg, |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4314 | data_t *secret, |
| 4315 | data_t *transcript, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4316 | data_t *client_expected, |
| 4317 | data_t *server_expected, |
| 4318 | data_t *exporter_expected) |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4319 | { |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 4320 | mbedtls_ssl_tls13_application_secrets secrets; |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4321 | |
| 4322 | /* Double-check that we've passed sane parameters. */ |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 4323 | psa_algorithm_t alg = (psa_algorithm_t) hash_alg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4324 | size_t const hash_len = PSA_HASH_LENGTH(alg); |
| 4325 | TEST_ASSERT(PSA_ALG_IS_HASH(alg) && |
| 4326 | secret->len == hash_len && |
| 4327 | transcript->len == hash_len && |
| 4328 | client_expected->len == hash_len && |
| 4329 | server_expected->len == hash_len && |
| 4330 | exporter_expected->len == hash_len); |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4331 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4332 | PSA_INIT(); |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4333 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4334 | TEST_ASSERT(mbedtls_ssl_tls13_derive_application_secrets( |
| 4335 | alg, secret->x, transcript->x, transcript->len, |
| 4336 | &secrets) == 0); |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4337 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4338 | ASSERT_COMPARE(secrets.client_application_traffic_secret_N, hash_len, |
| 4339 | client_expected->x, client_expected->len); |
| 4340 | ASSERT_COMPARE(secrets.server_application_traffic_secret_N, hash_len, |
| 4341 | server_expected->x, server_expected->len); |
| 4342 | ASSERT_COMPARE(secrets.exporter_master_secret, hash_len, |
| 4343 | exporter_expected->x, exporter_expected->len); |
Gabor Mezei | 892c4aa | 2022-03-21 12:21:43 +0100 | [diff] [blame] | 4344 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4345 | PSA_DONE(); |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 4346 | } |
| 4347 | /* END_CASE */ |
| 4348 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 4349 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4350 | void ssl_tls13_derive_resumption_secrets(int hash_alg, |
| 4351 | data_t *secret, |
| 4352 | data_t *transcript, |
| 4353 | data_t *resumption_expected) |
Hanno Becker | 55bc2c5 | 2021-05-24 06:53:52 +0100 | [diff] [blame] | 4354 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4355 | mbedtls_ssl_tls13_application_secrets secrets; |
Hanno Becker | 55bc2c5 | 2021-05-24 06:53:52 +0100 | [diff] [blame] | 4356 | |
| 4357 | /* Double-check that we've passed sane parameters. */ |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 4358 | psa_algorithm_t alg = (psa_algorithm_t) hash_alg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4359 | size_t const hash_len = PSA_HASH_LENGTH(alg); |
| 4360 | TEST_ASSERT(PSA_ALG_IS_HASH(alg) && |
| 4361 | secret->len == hash_len && |
| 4362 | transcript->len == hash_len && |
| 4363 | resumption_expected->len == hash_len); |
Hanno Becker | 55bc2c5 | 2021-05-24 06:53:52 +0100 | [diff] [blame] | 4364 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4365 | PSA_INIT(); |
Hanno Becker | 55bc2c5 | 2021-05-24 06:53:52 +0100 | [diff] [blame] | 4366 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4367 | TEST_ASSERT(mbedtls_ssl_tls13_derive_resumption_master_secret( |
| 4368 | alg, secret->x, transcript->x, transcript->len, |
| 4369 | &secrets) == 0); |
Hanno Becker | 55bc2c5 | 2021-05-24 06:53:52 +0100 | [diff] [blame] | 4370 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4371 | ASSERT_COMPARE(secrets.resumption_master_secret, hash_len, |
| 4372 | resumption_expected->x, resumption_expected->len); |
Gabor Mezei | 892c4aa | 2022-03-21 12:21:43 +0100 | [diff] [blame] | 4373 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4374 | PSA_DONE(); |
| 4375 | } |
| 4376 | /* END_CASE */ |
| 4377 | |
| 4378 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 4379 | void ssl_tls13_create_psk_binder(int hash_alg, |
| 4380 | data_t *psk, |
| 4381 | int psk_type, |
| 4382 | data_t *transcript, |
| 4383 | data_t *binder_expected) |
| 4384 | { |
| 4385 | unsigned char binder[MBEDTLS_HASH_MAX_SIZE]; |
| 4386 | |
| 4387 | /* Double-check that we've passed sane parameters. */ |
| 4388 | psa_algorithm_t alg = (psa_algorithm_t) hash_alg; |
| 4389 | size_t const hash_len = PSA_HASH_LENGTH(alg); |
| 4390 | TEST_ASSERT(PSA_ALG_IS_HASH(alg) && |
| 4391 | transcript->len == hash_len && |
| 4392 | binder_expected->len == hash_len); |
| 4393 | |
| 4394 | PSA_INIT(); |
| 4395 | |
| 4396 | TEST_ASSERT(mbedtls_ssl_tls13_create_psk_binder( |
| 4397 | NULL, /* SSL context for debugging only */ |
| 4398 | alg, |
| 4399 | psk->x, psk->len, |
| 4400 | psk_type, |
| 4401 | transcript->x, |
| 4402 | binder) == 0); |
| 4403 | |
| 4404 | ASSERT_COMPARE(binder, hash_len, |
| 4405 | binder_expected->x, binder_expected->len); |
| 4406 | |
| 4407 | PSA_DONE(); |
Hanno Becker | 55bc2c5 | 2021-05-24 06:53:52 +0100 | [diff] [blame] | 4408 | } |
| 4409 | /* END_CASE */ |
| 4410 | |
Andrzej Kurek | daf43fb | 2022-10-12 10:46:42 -0400 | [diff] [blame] | 4411 | /* 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] | 4412 | void ssl_tls13_record_protection(int ciphersuite, |
| 4413 | int endpoint, |
| 4414 | int ctr, |
| 4415 | int padding_used, |
| 4416 | data_t *server_write_key, |
| 4417 | data_t *server_write_iv, |
| 4418 | data_t *client_write_key, |
| 4419 | data_t *client_write_iv, |
| 4420 | data_t *plaintext, |
| 4421 | data_t *ciphertext) |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4422 | { |
| 4423 | mbedtls_ssl_key_set keys; |
| 4424 | mbedtls_ssl_transform transform_send; |
| 4425 | mbedtls_ssl_transform transform_recv; |
| 4426 | mbedtls_record rec; |
| 4427 | unsigned char *buf = NULL; |
Hanno Becker | 1f91878 | 2021-08-01 19:18:28 +0100 | [diff] [blame] | 4428 | size_t buf_len; |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4429 | int other_endpoint; |
| 4430 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4431 | USE_PSA_INIT(); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 4432 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4433 | TEST_ASSERT(endpoint == MBEDTLS_SSL_IS_CLIENT || |
| 4434 | endpoint == MBEDTLS_SSL_IS_SERVER); |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4435 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4436 | if (endpoint == MBEDTLS_SSL_IS_SERVER) { |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4437 | other_endpoint = MBEDTLS_SSL_IS_CLIENT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4438 | } |
| 4439 | if (endpoint == MBEDTLS_SSL_IS_CLIENT) { |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4440 | other_endpoint = MBEDTLS_SSL_IS_SERVER; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4441 | } |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4442 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4443 | TEST_ASSERT(server_write_key->len == client_write_key->len); |
| 4444 | TEST_ASSERT(server_write_iv->len == client_write_iv->len); |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4445 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4446 | memcpy(keys.client_write_key, |
| 4447 | client_write_key->x, client_write_key->len); |
| 4448 | memcpy(keys.client_write_iv, |
| 4449 | client_write_iv->x, client_write_iv->len); |
| 4450 | memcpy(keys.server_write_key, |
| 4451 | server_write_key->x, server_write_key->len); |
| 4452 | memcpy(keys.server_write_iv, |
| 4453 | server_write_iv->x, server_write_iv->len); |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4454 | |
| 4455 | keys.key_len = server_write_key->len; |
| 4456 | keys.iv_len = server_write_iv->len; |
| 4457 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4458 | mbedtls_ssl_transform_init(&transform_recv); |
| 4459 | mbedtls_ssl_transform_init(&transform_send); |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4460 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4461 | TEST_ASSERT(mbedtls_ssl_tls13_populate_transform( |
| 4462 | &transform_send, endpoint, |
| 4463 | ciphersuite, &keys, NULL) == 0); |
| 4464 | TEST_ASSERT(mbedtls_ssl_tls13_populate_transform( |
| 4465 | &transform_recv, other_endpoint, |
| 4466 | ciphersuite, &keys, NULL) == 0); |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4467 | |
Hanno Becker | 1f91878 | 2021-08-01 19:18:28 +0100 | [diff] [blame] | 4468 | /* Make sure we have enough space in the buffer even if |
| 4469 | * we use more padding than the KAT. */ |
| 4470 | buf_len = ciphertext->len + MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4471 | ASSERT_ALLOC(buf, buf_len); |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4472 | rec.type = MBEDTLS_SSL_MSG_APPLICATION_DATA; |
Hanno Becker | 4153745 | 2021-04-20 05:35:28 +0100 | [diff] [blame] | 4473 | |
| 4474 | /* 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] | 4475 | mbedtls_ssl_write_version(rec.ver, |
| 4476 | MBEDTLS_SSL_TRANSPORT_STREAM, |
| 4477 | MBEDTLS_SSL_VERSION_TLS1_2); |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4478 | |
| 4479 | /* Copy plaintext into record structure */ |
| 4480 | rec.buf = buf; |
Hanno Becker | 1f91878 | 2021-08-01 19:18:28 +0100 | [diff] [blame] | 4481 | rec.buf_len = buf_len; |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4482 | rec.data_offset = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4483 | TEST_ASSERT(plaintext->len <= ciphertext->len); |
| 4484 | memcpy(rec.buf + rec.data_offset, plaintext->x, plaintext->len); |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4485 | rec.data_len = plaintext->len; |
| 4486 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 4487 | rec.cid_len = 0; |
| 4488 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 4489 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4490 | memset(&rec.ctr[0], 0, 8); |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4491 | rec.ctr[7] = ctr; |
| 4492 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4493 | TEST_ASSERT(mbedtls_ssl_encrypt_buf(NULL, &transform_send, &rec, |
| 4494 | NULL, NULL) == 0); |
Hanno Becker | 1f91878 | 2021-08-01 19:18:28 +0100 | [diff] [blame] | 4495 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4496 | if (padding_used == MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY) { |
| 4497 | ASSERT_COMPARE(rec.buf + rec.data_offset, rec.data_len, |
| 4498 | ciphertext->x, ciphertext->len); |
Hanno Becker | 1f91878 | 2021-08-01 19:18:28 +0100 | [diff] [blame] | 4499 | } |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4500 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4501 | TEST_ASSERT(mbedtls_ssl_decrypt_buf(NULL, &transform_recv, &rec) == 0); |
| 4502 | ASSERT_COMPARE(rec.buf + rec.data_offset, rec.data_len, |
| 4503 | plaintext->x, plaintext->len); |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4504 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4505 | mbedtls_free(buf); |
| 4506 | mbedtls_ssl_transform_free(&transform_send); |
| 4507 | mbedtls_ssl_transform_free(&transform_recv); |
| 4508 | USE_PSA_DONE(); |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 4509 | } |
| 4510 | /* END_CASE */ |
| 4511 | |
Andrzej Kurek | 658442f | 2022-10-12 11:28:41 -0400 | [diff] [blame] | 4512 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4513 | void ssl_tls13_key_evolution(int hash_alg, |
| 4514 | data_t *secret, |
| 4515 | data_t *input, |
| 4516 | data_t *expected) |
Hanno Becker | 2d2c3eb | 2020-08-20 14:54:24 +0100 | [diff] [blame] | 4517 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4518 | unsigned char secret_new[MBEDTLS_HASH_MAX_SIZE]; |
Hanno Becker | 2d2c3eb | 2020-08-20 14:54:24 +0100 | [diff] [blame] | 4519 | |
Gabor Mezei | 5d9a1fe | 2022-03-24 17:49:14 +0100 | [diff] [blame] | 4520 | PSA_INIT(); |
Gabor Mezei | 892c4aa | 2022-03-21 12:21:43 +0100 | [diff] [blame] | 4521 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4522 | TEST_ASSERT(mbedtls_ssl_tls13_evolve_secret( |
| 4523 | (psa_algorithm_t) hash_alg, |
| 4524 | secret->len ? secret->x : NULL, |
| 4525 | input->len ? input->x : NULL, input->len, |
| 4526 | secret_new) == 0); |
Hanno Becker | 2d2c3eb | 2020-08-20 14:54:24 +0100 | [diff] [blame] | 4527 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4528 | ASSERT_COMPARE(secret_new, (size_t) expected->len, |
| 4529 | expected->x, (size_t) expected->len); |
Gabor Mezei | 892c4aa | 2022-03-21 12:21:43 +0100 | [diff] [blame] | 4530 | |
Gabor Mezei | 5d9a1fe | 2022-03-24 17:49:14 +0100 | [diff] [blame] | 4531 | PSA_DONE(); |
Hanno Becker | 2d2c3eb | 2020-08-20 14:54:24 +0100 | [diff] [blame] | 4532 | } |
| 4533 | /* END_CASE */ |
| 4534 | |
Jerry Yu | 53d23e2 | 2022-02-09 16:25:09 +0800 | [diff] [blame] | 4535 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_2 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4536 | void ssl_tls_prf(int type, data_t *secret, data_t *random, |
| 4537 | char *label, data_t *result_str, int exp_ret) |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 4538 | { |
| 4539 | unsigned char *output; |
| 4540 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4541 | output = mbedtls_calloc(1, result_str->len); |
| 4542 | if (output == NULL) { |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 4543 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4544 | } |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 4545 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4546 | USE_PSA_INIT(); |
Ron Eldor | 6b9b1b8 | 2019-05-15 17:04:33 +0300 | [diff] [blame] | 4547 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4548 | TEST_ASSERT(mbedtls_ssl_tls_prf(type, secret->x, secret->len, |
| 4549 | label, random->x, random->len, |
| 4550 | output, result_str->len) == exp_ret); |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 4551 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4552 | if (exp_ret == 0) { |
| 4553 | TEST_ASSERT(mbedtls_test_hexcmp(output, result_str->x, |
| 4554 | result_str->len, result_str->len) == 0); |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 4555 | } |
| 4556 | exit: |
| 4557 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4558 | mbedtls_free(output); |
| 4559 | USE_PSA_DONE(); |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 4560 | } |
| 4561 | /* END_CASE */ |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 4562 | |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 4563 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4564 | void ssl_serialize_session_save_load(int ticket_len, char *crt_file, |
| 4565 | int endpoint_type, int tls_version) |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 4566 | { |
| 4567 | mbedtls_ssl_session original, restored; |
| 4568 | unsigned char *buf = NULL; |
| 4569 | size_t len; |
| 4570 | |
| 4571 | /* |
| 4572 | * Test that a save-load pair is the identity |
| 4573 | */ |
| 4574 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4575 | mbedtls_ssl_session_init(&original); |
| 4576 | mbedtls_ssl_session_init(&restored); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 4577 | |
| 4578 | /* Prepare a dummy session to work on */ |
Jerry Yu | f092629 | 2022-07-15 13:05:57 +0800 | [diff] [blame] | 4579 | ((void) endpoint_type); |
| 4580 | ((void) tls_version); |
| 4581 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4582 | if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
| 4583 | TEST_ASSERT(ssl_tls13_populate_session( |
| 4584 | &original, 0, endpoint_type) == 0); |
| 4585 | } else |
Jerry Yu | f092629 | 2022-07-15 13:05:57 +0800 | [diff] [blame] | 4586 | #endif |
| 4587 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4588 | TEST_ASSERT(ssl_tls12_populate_session( |
| 4589 | &original, ticket_len, crt_file) == 0); |
Jerry Yu | f092629 | 2022-07-15 13:05:57 +0800 | [diff] [blame] | 4590 | } |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 4591 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 4592 | /* Serialize it */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4593 | TEST_ASSERT(mbedtls_ssl_session_save(&original, NULL, 0, &len) |
| 4594 | == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL); |
| 4595 | TEST_ASSERT((buf = mbedtls_calloc(1, len)) != NULL); |
| 4596 | TEST_ASSERT(mbedtls_ssl_session_save(&original, buf, len, &len) |
| 4597 | == 0); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 4598 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 4599 | /* Restore session from serialized data */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4600 | TEST_ASSERT(mbedtls_ssl_session_load(&restored, buf, len) == 0); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 4601 | |
| 4602 | /* |
| 4603 | * Make sure both session structures are identical |
| 4604 | */ |
| 4605 | #if defined(MBEDTLS_HAVE_TIME) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4606 | TEST_ASSERT(original.start == restored.start); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 4607 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4608 | TEST_ASSERT(original.tls_version == restored.tls_version); |
| 4609 | TEST_ASSERT(original.ciphersuite == restored.ciphersuite); |
Jerry Yu | f092629 | 2022-07-15 13:05:57 +0800 | [diff] [blame] | 4610 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4611 | if (tls_version == MBEDTLS_SSL_VERSION_TLS1_2) { |
| 4612 | TEST_ASSERT(original.id_len == restored.id_len); |
| 4613 | TEST_ASSERT(memcmp(original.id, |
| 4614 | restored.id, sizeof(original.id)) == 0); |
| 4615 | TEST_ASSERT(memcmp(original.master, |
| 4616 | restored.master, sizeof(original.master)) == 0); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 4617 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4618 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 4619 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4620 | TEST_ASSERT((original.peer_cert == NULL) == |
| 4621 | (restored.peer_cert == NULL)); |
| 4622 | if (original.peer_cert != NULL) { |
| 4623 | TEST_ASSERT(original.peer_cert->raw.len == |
| 4624 | restored.peer_cert->raw.len); |
| 4625 | TEST_ASSERT(memcmp(original.peer_cert->raw.p, |
| 4626 | restored.peer_cert->raw.p, |
| 4627 | original.peer_cert->raw.len) == 0); |
Jerry Yu | f092629 | 2022-07-15 13:05:57 +0800 | [diff] [blame] | 4628 | } |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 4629 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4630 | TEST_ASSERT(original.peer_cert_digest_type == |
| 4631 | restored.peer_cert_digest_type); |
| 4632 | TEST_ASSERT(original.peer_cert_digest_len == |
| 4633 | restored.peer_cert_digest_len); |
| 4634 | TEST_ASSERT((original.peer_cert_digest == NULL) == |
| 4635 | (restored.peer_cert_digest == NULL)); |
| 4636 | if (original.peer_cert_digest != NULL) { |
| 4637 | TEST_ASSERT(memcmp(original.peer_cert_digest, |
| 4638 | restored.peer_cert_digest, |
| 4639 | original.peer_cert_digest_len) == 0); |
Jerry Yu | f092629 | 2022-07-15 13:05:57 +0800 | [diff] [blame] | 4640 | } |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 4641 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4642 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4643 | TEST_ASSERT(original.verify_result == restored.verify_result); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 4644 | |
Jerry Yu | f092629 | 2022-07-15 13:05:57 +0800 | [diff] [blame] | 4645 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4646 | TEST_ASSERT(original.mfl_code == restored.mfl_code); |
Jerry Yu | f092629 | 2022-07-15 13:05:57 +0800 | [diff] [blame] | 4647 | #endif |
| 4648 | |
| 4649 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4650 | TEST_ASSERT(original.encrypt_then_mac == restored.encrypt_then_mac); |
Jerry Yu | f092629 | 2022-07-15 13:05:57 +0800 | [diff] [blame] | 4651 | #endif |
Jerry Yu | 6ac7c03 | 2022-07-21 23:11:55 +0800 | [diff] [blame] | 4652 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4653 | TEST_ASSERT(original.ticket_len == restored.ticket_len); |
| 4654 | if (original.ticket_len != 0) { |
| 4655 | TEST_ASSERT(original.ticket != NULL); |
| 4656 | TEST_ASSERT(restored.ticket != NULL); |
| 4657 | TEST_ASSERT(memcmp(original.ticket, |
| 4658 | restored.ticket, original.ticket_len) == 0); |
Jerry Yu | 6ac7c03 | 2022-07-21 23:11:55 +0800 | [diff] [blame] | 4659 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4660 | TEST_ASSERT(original.ticket_lifetime == restored.ticket_lifetime); |
Jerry Yu | 6ac7c03 | 2022-07-21 23:11:55 +0800 | [diff] [blame] | 4661 | #endif |
Jerry Yu | f092629 | 2022-07-15 13:05:57 +0800 | [diff] [blame] | 4662 | } |
Jerry Yu | 6ac7c03 | 2022-07-21 23:11:55 +0800 | [diff] [blame] | 4663 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 4664 | |
| 4665 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4666 | if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
| 4667 | TEST_ASSERT(original.endpoint == restored.endpoint); |
| 4668 | TEST_ASSERT(original.ciphersuite == restored.ciphersuite); |
| 4669 | TEST_ASSERT(original.ticket_age_add == restored.ticket_age_add); |
| 4670 | TEST_ASSERT(original.ticket_flags == restored.ticket_flags); |
| 4671 | TEST_ASSERT(original.resumption_key_len == restored.resumption_key_len); |
| 4672 | if (original.resumption_key_len != 0) { |
| 4673 | TEST_ASSERT(original.resumption_key != NULL); |
| 4674 | TEST_ASSERT(restored.resumption_key != NULL); |
| 4675 | TEST_ASSERT(memcmp(original.resumption_key, |
| 4676 | restored.resumption_key, |
| 4677 | original.resumption_key_len) == 0); |
Jerry Yu | 6ac7c03 | 2022-07-21 23:11:55 +0800 | [diff] [blame] | 4678 | } |
| 4679 | #if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4680 | if (endpoint_type == MBEDTLS_SSL_IS_SERVER) { |
| 4681 | TEST_ASSERT(original.start == restored.start); |
Jerry Yu | 6ac7c03 | 2022-07-21 23:11:55 +0800 | [diff] [blame] | 4682 | } |
Jerry Yu | f092629 | 2022-07-15 13:05:57 +0800 | [diff] [blame] | 4683 | #endif |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 4684 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4685 | if (endpoint_type == MBEDTLS_SSL_IS_CLIENT) { |
Jerry Yu | 6ac7c03 | 2022-07-21 23:11:55 +0800 | [diff] [blame] | 4686 | #if defined(MBEDTLS_HAVE_TIME) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4687 | TEST_ASSERT(original.ticket_received == restored.ticket_received); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 4688 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4689 | TEST_ASSERT(original.ticket_lifetime == restored.ticket_lifetime); |
| 4690 | TEST_ASSERT(original.ticket_len == restored.ticket_len); |
| 4691 | if (original.ticket_len != 0) { |
| 4692 | TEST_ASSERT(original.ticket != NULL); |
| 4693 | TEST_ASSERT(restored.ticket != NULL); |
| 4694 | TEST_ASSERT(memcmp(original.ticket, |
| 4695 | restored.ticket, |
| 4696 | original.ticket_len) == 0); |
Jerry Yu | 6ac7c03 | 2022-07-21 23:11:55 +0800 | [diff] [blame] | 4697 | } |
| 4698 | |
| 4699 | } |
| 4700 | #endif |
| 4701 | } |
| 4702 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 4703 | |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 4704 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4705 | mbedtls_ssl_session_free(&original); |
| 4706 | mbedtls_ssl_session_free(&restored); |
| 4707 | mbedtls_free(buf); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 4708 | } |
| 4709 | /* END_CASE */ |
| 4710 | |
Manuel Pégourié-Gonnard | aa75583 | 2019-06-03 10:53:47 +0200 | [diff] [blame] | 4711 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4712 | void ssl_serialize_session_load_save(int ticket_len, char *crt_file, |
| 4713 | int endpoint_type, int tls_version) |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 4714 | { |
| 4715 | mbedtls_ssl_session session; |
| 4716 | unsigned char *buf1 = NULL, *buf2 = NULL; |
| 4717 | size_t len0, len1, len2; |
| 4718 | |
| 4719 | /* |
| 4720 | * Test that a load-save pair is the identity |
| 4721 | */ |
| 4722 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4723 | mbedtls_ssl_session_init(&session); |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 4724 | |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 4725 | /* Prepare a dummy session to work on */ |
Jerry Yu | a180f99 | 2022-07-15 12:52:54 +0800 | [diff] [blame] | 4726 | ((void) endpoint_type); |
| 4727 | ((void) tls_version); |
| 4728 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4729 | if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
| 4730 | TEST_ASSERT(ssl_tls13_populate_session( |
| 4731 | &session, 0, endpoint_type) == 0); |
| 4732 | } else |
Jerry Yu | a180f99 | 2022-07-15 12:52:54 +0800 | [diff] [blame] | 4733 | #endif |
| 4734 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4735 | TEST_ASSERT(ssl_tls12_populate_session( |
| 4736 | &session, ticket_len, crt_file) == 0); |
Jerry Yu | a180f99 | 2022-07-15 12:52:54 +0800 | [diff] [blame] | 4737 | } |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 4738 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 4739 | /* Get desired buffer size for serializing */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4740 | TEST_ASSERT(mbedtls_ssl_session_save(&session, NULL, 0, &len0) |
| 4741 | == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL); |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 4742 | |
| 4743 | /* Allocate first buffer */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4744 | buf1 = mbedtls_calloc(1, len0); |
| 4745 | TEST_ASSERT(buf1 != NULL); |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 4746 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 4747 | /* Serialize to buffer and free live session */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4748 | TEST_ASSERT(mbedtls_ssl_session_save(&session, buf1, len0, &len1) |
| 4749 | == 0); |
| 4750 | TEST_ASSERT(len0 == len1); |
| 4751 | mbedtls_ssl_session_free(&session); |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 4752 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 4753 | /* Restore session from serialized data */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4754 | TEST_ASSERT(mbedtls_ssl_session_load(&session, buf1, len1) == 0); |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 4755 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 4756 | /* Allocate second buffer and serialize to it */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4757 | buf2 = mbedtls_calloc(1, len0); |
| 4758 | TEST_ASSERT(buf2 != NULL); |
| 4759 | TEST_ASSERT(mbedtls_ssl_session_save(&session, buf2, len0, &len2) |
| 4760 | == 0); |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 4761 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 4762 | /* Make sure both serialized versions are identical */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4763 | TEST_ASSERT(len1 == len2); |
| 4764 | TEST_ASSERT(memcmp(buf1, buf2, len1) == 0); |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 4765 | |
| 4766 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4767 | mbedtls_ssl_session_free(&session); |
| 4768 | mbedtls_free(buf1); |
| 4769 | mbedtls_free(buf2); |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 4770 | } |
| 4771 | /* END_CASE */ |
Manuel Pégourié-Gonnard | f5fa0aa | 2019-05-23 10:38:11 +0200 | [diff] [blame] | 4772 | |
| 4773 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4774 | void ssl_serialize_session_save_buf_size(int ticket_len, char *crt_file, |
| 4775 | int endpoint_type, int tls_version) |
Manuel Pégourié-Gonnard | f5fa0aa | 2019-05-23 10:38:11 +0200 | [diff] [blame] | 4776 | { |
| 4777 | mbedtls_ssl_session session; |
| 4778 | unsigned char *buf = NULL; |
| 4779 | size_t good_len, bad_len, test_len; |
| 4780 | |
| 4781 | /* |
| 4782 | * Test that session_save() fails cleanly on small buffers |
| 4783 | */ |
| 4784 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4785 | mbedtls_ssl_session_init(&session); |
Manuel Pégourié-Gonnard | f5fa0aa | 2019-05-23 10:38:11 +0200 | [diff] [blame] | 4786 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 4787 | /* Prepare dummy session and get serialized size */ |
Jerry Yu | 1ac476c | 2022-07-15 11:22:40 +0800 | [diff] [blame] | 4788 | ((void) endpoint_type); |
| 4789 | ((void) tls_version); |
| 4790 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4791 | if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
| 4792 | TEST_ASSERT(ssl_tls13_populate_session( |
| 4793 | &session, 0, endpoint_type) == 0); |
| 4794 | } else |
Jerry Yu | 1ac476c | 2022-07-15 11:22:40 +0800 | [diff] [blame] | 4795 | #endif |
| 4796 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4797 | TEST_ASSERT(ssl_tls12_populate_session( |
| 4798 | &session, ticket_len, crt_file) == 0); |
Jerry Yu | 1ac476c | 2022-07-15 11:22:40 +0800 | [diff] [blame] | 4799 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4800 | TEST_ASSERT(mbedtls_ssl_session_save(&session, NULL, 0, &good_len) |
| 4801 | == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL); |
Manuel Pégourié-Gonnard | f5fa0aa | 2019-05-23 10:38:11 +0200 | [diff] [blame] | 4802 | |
| 4803 | /* Try all possible bad lengths */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4804 | for (bad_len = 1; bad_len < good_len; bad_len++) { |
Manuel Pégourié-Gonnard | f5fa0aa | 2019-05-23 10:38:11 +0200 | [diff] [blame] | 4805 | /* Allocate exact size so that asan/valgrind can detect any overwrite */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4806 | mbedtls_free(buf); |
| 4807 | TEST_ASSERT((buf = mbedtls_calloc(1, bad_len)) != NULL); |
| 4808 | TEST_ASSERT(mbedtls_ssl_session_save(&session, buf, bad_len, |
| 4809 | &test_len) |
| 4810 | == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL); |
| 4811 | TEST_ASSERT(test_len == good_len); |
Manuel Pégourié-Gonnard | f5fa0aa | 2019-05-23 10:38:11 +0200 | [diff] [blame] | 4812 | } |
| 4813 | |
| 4814 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4815 | mbedtls_ssl_session_free(&session); |
| 4816 | mbedtls_free(buf); |
Manuel Pégourié-Gonnard | f5fa0aa | 2019-05-23 10:38:11 +0200 | [diff] [blame] | 4817 | } |
| 4818 | /* END_CASE */ |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 4819 | |
| 4820 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4821 | void ssl_serialize_session_load_buf_size(int ticket_len, char *crt_file, |
| 4822 | int endpoint_type, int tls_version) |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 4823 | { |
| 4824 | mbedtls_ssl_session session; |
| 4825 | unsigned char *good_buf = NULL, *bad_buf = NULL; |
| 4826 | size_t good_len, bad_len; |
| 4827 | |
| 4828 | /* |
| 4829 | * Test that session_load() fails cleanly on small buffers |
| 4830 | */ |
| 4831 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4832 | mbedtls_ssl_session_init(&session); |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 4833 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 4834 | /* Prepare serialized session data */ |
Jerry Yu | 6e8fec2 | 2022-07-15 10:37:02 +0800 | [diff] [blame] | 4835 | ((void) endpoint_type); |
| 4836 | ((void) tls_version); |
| 4837 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4838 | if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
| 4839 | TEST_ASSERT(ssl_tls13_populate_session( |
| 4840 | &session, 0, endpoint_type) == 0); |
| 4841 | } else |
Jerry Yu | 6e8fec2 | 2022-07-15 10:37:02 +0800 | [diff] [blame] | 4842 | #endif |
| 4843 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4844 | TEST_ASSERT(ssl_tls12_populate_session( |
| 4845 | &session, ticket_len, crt_file) == 0); |
Jerry Yu | 6e8fec2 | 2022-07-15 10:37:02 +0800 | [diff] [blame] | 4846 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4847 | TEST_ASSERT(mbedtls_ssl_session_save(&session, NULL, 0, &good_len) |
| 4848 | == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL); |
| 4849 | TEST_ASSERT((good_buf = mbedtls_calloc(1, good_len)) != NULL); |
| 4850 | TEST_ASSERT(mbedtls_ssl_session_save(&session, good_buf, good_len, |
| 4851 | &good_len) == 0); |
| 4852 | mbedtls_ssl_session_free(&session); |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 4853 | |
| 4854 | /* Try all possible bad lengths */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4855 | for (bad_len = 0; bad_len < good_len; bad_len++) { |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 4856 | /* Allocate exact size so that asan/valgrind can detect any overread */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4857 | mbedtls_free(bad_buf); |
| 4858 | bad_buf = mbedtls_calloc(1, bad_len ? bad_len : 1); |
| 4859 | TEST_ASSERT(bad_buf != NULL); |
| 4860 | memcpy(bad_buf, good_buf, bad_len); |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 4861 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4862 | TEST_ASSERT(mbedtls_ssl_session_load(&session, bad_buf, bad_len) |
| 4863 | == MBEDTLS_ERR_SSL_BAD_INPUT_DATA); |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 4864 | } |
| 4865 | |
| 4866 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4867 | mbedtls_ssl_session_free(&session); |
| 4868 | mbedtls_free(good_buf); |
| 4869 | mbedtls_free(bad_buf); |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 4870 | } |
| 4871 | /* END_CASE */ |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 4872 | |
Hanno Becker | 363b646 | 2019-05-29 12:44:28 +0100 | [diff] [blame] | 4873 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4874 | void ssl_session_serialize_version_check(int corrupt_major, |
| 4875 | int corrupt_minor, |
| 4876 | int corrupt_patch, |
| 4877 | int corrupt_config, |
| 4878 | int endpoint_type, |
| 4879 | int tls_version) |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 4880 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4881 | unsigned char serialized_session[2048]; |
Hanno Becker | 363b646 | 2019-05-29 12:44:28 +0100 | [diff] [blame] | 4882 | size_t serialized_session_len; |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 4883 | unsigned cur_byte; |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 4884 | mbedtls_ssl_session session; |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 4885 | uint8_t should_corrupt_byte[] = { corrupt_major == 1, |
| 4886 | corrupt_minor == 1, |
| 4887 | corrupt_patch == 1, |
| 4888 | corrupt_config == 1, |
| 4889 | corrupt_config == 1 }; |
| 4890 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4891 | mbedtls_ssl_session_init(&session); |
Jerry Yu | 534ff40 | 2022-07-14 16:43:43 +0800 | [diff] [blame] | 4892 | ((void) endpoint_type); |
| 4893 | ((void) tls_version); |
| 4894 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4895 | if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
| 4896 | TEST_ASSERT(ssl_tls13_populate_session( |
| 4897 | &session, 0, endpoint_type) == 0); |
| 4898 | } else |
Jerry Yu | 534ff40 | 2022-07-14 16:43:43 +0800 | [diff] [blame] | 4899 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4900 | TEST_ASSERT(ssl_tls12_populate_session(&session, 0, NULL) == 0); |
Jerry Yu | 534ff40 | 2022-07-14 16:43:43 +0800 | [diff] [blame] | 4901 | |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 4902 | |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 4903 | /* Infer length of serialized session. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4904 | TEST_ASSERT(mbedtls_ssl_session_save(&session, |
| 4905 | serialized_session, |
| 4906 | sizeof(serialized_session), |
| 4907 | &serialized_session_len) == 0); |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 4908 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4909 | mbedtls_ssl_session_free(&session); |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 4910 | |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 4911 | /* Without any modification, we should be able to successfully |
Hanno Becker | 363b646 | 2019-05-29 12:44:28 +0100 | [diff] [blame] | 4912 | * de-serialize the session - double-check that. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4913 | TEST_ASSERT(mbedtls_ssl_session_load(&session, |
| 4914 | serialized_session, |
| 4915 | serialized_session_len) == 0); |
| 4916 | mbedtls_ssl_session_free(&session); |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 4917 | |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 4918 | /* Go through the bytes in the serialized session header and |
| 4919 | * corrupt them bit-by-bit. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4920 | for (cur_byte = 0; cur_byte < sizeof(should_corrupt_byte); cur_byte++) { |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 4921 | int cur_bit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4922 | unsigned char * const byte = &serialized_session[cur_byte]; |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 4923 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4924 | if (should_corrupt_byte[cur_byte] == 0) { |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 4925 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4926 | } |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 4927 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4928 | for (cur_bit = 0; cur_bit < CHAR_BIT; cur_bit++) { |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 4929 | unsigned char const corrupted_bit = 0x1u << cur_bit; |
| 4930 | /* Modify a single bit in the serialized session. */ |
| 4931 | *byte ^= corrupted_bit; |
| 4932 | |
| 4933 | /* Attempt to deserialize */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4934 | TEST_ASSERT(mbedtls_ssl_session_load(&session, |
| 4935 | serialized_session, |
| 4936 | serialized_session_len) == |
| 4937 | MBEDTLS_ERR_SSL_VERSION_MISMATCH); |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 4938 | |
| 4939 | /* Undo the change */ |
| 4940 | *byte ^= corrupted_bit; |
| 4941 | } |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 4942 | } |
| 4943 | |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 4944 | } |
| 4945 | /* END_CASE */ |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4946 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4947 | /* 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] | 4948 | void mbedtls_endpoint_sanity(int endpoint_type) |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4949 | { |
| 4950 | enum { BUFFSIZE = 1024 }; |
| 4951 | mbedtls_endpoint ep; |
| 4952 | int ret = -1; |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 4953 | handshake_test_options options; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4954 | init_handshake_options(&options); |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 4955 | options.pk_alg = MBEDTLS_PK_RSA; |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4956 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4957 | ret = mbedtls_endpoint_init(NULL, endpoint_type, &options, |
| 4958 | NULL, NULL, NULL, NULL); |
| 4959 | TEST_ASSERT(MBEDTLS_ERR_SSL_BAD_INPUT_DATA == ret); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4960 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4961 | ret = mbedtls_endpoint_certificate_init(NULL, options.pk_alg, 0, 0, 0); |
| 4962 | TEST_ASSERT(MBEDTLS_ERR_SSL_BAD_INPUT_DATA == ret); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4963 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4964 | ret = mbedtls_endpoint_init(&ep, endpoint_type, &options, |
| 4965 | NULL, NULL, NULL, NULL); |
| 4966 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4967 | |
| 4968 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4969 | mbedtls_endpoint_free(&ep, NULL); |
| 4970 | free_handshake_options(&options); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4971 | } |
| 4972 | /* END_CASE */ |
| 4973 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 4974 | /* 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] | 4975 | void move_handshake_to_state(int endpoint_type, int state, int need_pass) |
| 4976 | { |
| 4977 | enum { BUFFSIZE = 1024 }; |
| 4978 | mbedtls_endpoint base_ep, second_ep; |
| 4979 | int ret = -1; |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 4980 | handshake_test_options options; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4981 | init_handshake_options(&options); |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 4982 | options.pk_alg = MBEDTLS_PK_RSA; |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4983 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4984 | USE_PSA_INIT(); |
| 4985 | mbedtls_platform_zeroize(&base_ep, sizeof(base_ep)); |
| 4986 | mbedtls_platform_zeroize(&second_ep, sizeof(second_ep)); |
Neil Armstrong | 06baf04 | 2022-04-14 16:21:15 +0200 | [diff] [blame] | 4987 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4988 | ret = mbedtls_endpoint_init(&base_ep, endpoint_type, &options, |
| 4989 | NULL, NULL, NULL, NULL); |
| 4990 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4991 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4992 | ret = mbedtls_endpoint_init(&second_ep, |
| 4993 | (endpoint_type == MBEDTLS_SSL_IS_SERVER) ? |
| 4994 | MBEDTLS_SSL_IS_CLIENT : MBEDTLS_SSL_IS_SERVER, |
| 4995 | &options, NULL, NULL, NULL, NULL); |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 4996 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4997 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4998 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 4999 | ret = mbedtls_mock_socket_connect(&(base_ep.socket), |
| 5000 | &(second_ep.socket), |
| 5001 | BUFFSIZE); |
| 5002 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 5003 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5004 | ret = mbedtls_move_handshake_to_state(&(base_ep.ssl), |
| 5005 | &(second_ep.ssl), |
| 5006 | state); |
| 5007 | if (need_pass) { |
| 5008 | TEST_ASSERT(ret == 0 || |
| 5009 | ret == MBEDTLS_ERR_SSL_WANT_READ || |
| 5010 | ret == MBEDTLS_ERR_SSL_WANT_WRITE); |
| 5011 | TEST_ASSERT(base_ep.ssl.state == state); |
| 5012 | } else { |
| 5013 | TEST_ASSERT(ret != 0 && |
| 5014 | ret != MBEDTLS_ERR_SSL_WANT_READ && |
| 5015 | ret != MBEDTLS_ERR_SSL_WANT_WRITE); |
| 5016 | TEST_ASSERT(base_ep.ssl.state != state); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 5017 | } |
| 5018 | |
| 5019 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5020 | free_handshake_options(&options); |
| 5021 | mbedtls_endpoint_free(&base_ep, NULL); |
| 5022 | mbedtls_endpoint_free(&second_ep, NULL); |
| 5023 | USE_PSA_DONE(); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 5024 | } |
| 5025 | /* END_CASE */ |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 5026 | |
Andrzej Kurek | 9113df8 | 2023-01-19 18:26:31 -0500 | [diff] [blame] | 5027 | /* 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] | 5028 | void handshake_version(int dtls, int client_min_version, int client_max_version, |
| 5029 | int server_min_version, int server_max_version, |
| 5030 | int expected_negotiated_version) |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 5031 | { |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5032 | handshake_test_options options; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5033 | init_handshake_options(&options); |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 5034 | |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 5035 | options.client_min_version = client_min_version; |
| 5036 | options.client_max_version = client_max_version; |
| 5037 | options.server_min_version = server_min_version; |
| 5038 | options.server_max_version = server_max_version; |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 5039 | options.expected_negotiated_version = expected_negotiated_version; |
| 5040 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5041 | options.dtls = dtls; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5042 | perform_handshake(&options); |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 5043 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5044 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 5045 | goto exit; |
Andrzej Kurek | e11acb2 | 2022-06-27 06:11:34 -0400 | [diff] [blame] | 5046 | |
| 5047 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5048 | free_handshake_options(&options); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5049 | } |
| 5050 | /* END_CASE */ |
Andrzej Kurek | 9e9efdc | 2020-02-26 05:25:23 -0500 | [diff] [blame] | 5051 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5052 | /* 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] | 5053 | 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] | 5054 | { |
| 5055 | handshake_test_options options; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5056 | init_handshake_options(&options); |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 5057 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5058 | options.cipher = cipher; |
| 5059 | options.dtls = dtls; |
| 5060 | options.psk_str = psk_str; |
| 5061 | options.pk_alg = pk_alg; |
Andrzej Kurek | cc5169c | 2020-02-04 09:04:56 -0500 | [diff] [blame] | 5062 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5063 | perform_handshake(&options); |
Andrzej Kurek | 316da1f | 2020-02-26 09:03:47 -0500 | [diff] [blame] | 5064 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5065 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 5066 | goto exit; |
Andrzej Kurek | e11acb2 | 2022-06-27 06:11:34 -0400 | [diff] [blame] | 5067 | |
Andrzej Kurek | ddb8cd6 | 2022-07-04 16:07:28 -0400 | [diff] [blame] | 5068 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5069 | free_handshake_options(&options); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5070 | } |
| 5071 | /* END_CASE */ |
Andrzej Kurek | 316da1f | 2020-02-26 09:03:47 -0500 | [diff] [blame] | 5072 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5073 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5074 | void handshake_cipher(char *cipher, int pk_alg, int dtls) |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5075 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5076 | test_handshake_psk_cipher(cipher, pk_alg, NULL, dtls); |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 5077 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5078 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 5079 | goto exit; |
| 5080 | } |
| 5081 | /* END_CASE */ |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 5082 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5083 | /* 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] | 5084 | void handshake_ciphersuite_select(char *cipher, int pk_alg, data_t *psk_str, |
| 5085 | int psa_alg, int psa_alg2, int psa_usage, |
| 5086 | int expected_handshake_result, |
| 5087 | int expected_ciphersuite) |
Neil Armstrong | 8c52ed8 | 2022-05-27 13:14:55 +0200 | [diff] [blame] | 5088 | { |
| 5089 | handshake_test_options options; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5090 | init_handshake_options(&options); |
Neil Armstrong | 8c52ed8 | 2022-05-27 13:14:55 +0200 | [diff] [blame] | 5091 | |
| 5092 | options.cipher = cipher; |
Neil Armstrong | db13497 | 2022-06-30 09:06:28 +0200 | [diff] [blame] | 5093 | options.psk_str = psk_str; |
Neil Armstrong | 8c52ed8 | 2022-05-27 13:14:55 +0200 | [diff] [blame] | 5094 | options.pk_alg = pk_alg; |
| 5095 | options.opaque_alg = psa_alg; |
| 5096 | options.opaque_alg2 = psa_alg2; |
| 5097 | options.opaque_usage = psa_usage; |
| 5098 | options.expected_handshake_result = expected_handshake_result; |
| 5099 | options.expected_ciphersuite = expected_ciphersuite; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5100 | perform_handshake(&options); |
Neil Armstrong | 8c52ed8 | 2022-05-27 13:14:55 +0200 | [diff] [blame] | 5101 | |
| 5102 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 5103 | goto exit; |
Andrzej Kurek | ddb8cd6 | 2022-07-04 16:07:28 -0400 | [diff] [blame] | 5104 | |
| 5105 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5106 | free_handshake_options(&options); |
Neil Armstrong | 8c52ed8 | 2022-05-27 13:14:55 +0200 | [diff] [blame] | 5107 | } |
| 5108 | /* END_CASE */ |
| 5109 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5110 | /* 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] | 5111 | void app_data(int mfl, int cli_msg_len, int srv_msg_len, |
| 5112 | int expected_cli_fragments, |
| 5113 | int expected_srv_fragments, int dtls) |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5114 | { |
| 5115 | handshake_test_options options; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5116 | init_handshake_options(&options); |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 5117 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5118 | options.mfl = mfl; |
| 5119 | options.cli_msg_len = cli_msg_len; |
| 5120 | options.srv_msg_len = srv_msg_len; |
| 5121 | options.expected_cli_fragments = expected_cli_fragments; |
| 5122 | options.expected_srv_fragments = expected_srv_fragments; |
| 5123 | options.dtls = dtls; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5124 | #if !defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Ronald Cron | 21a1b2d | 2022-06-15 17:11:35 +0200 | [diff] [blame] | 5125 | options.expected_negotiated_version = MBEDTLS_SSL_VERSION_TLS1_3; |
| 5126 | #endif |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 5127 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5128 | perform_handshake(&options); |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 5129 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5130 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 5131 | goto exit; |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 5132 | |
| 5133 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5134 | free_handshake_options(&options); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5135 | } |
| 5136 | /* END_CASE */ |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 5137 | |
Andrzej Kurek | 714ae65 | 2022-11-02 19:07:19 -0400 | [diff] [blame] | 5138 | /* 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] | 5139 | void app_data_tls(int mfl, int cli_msg_len, int srv_msg_len, |
| 5140 | int expected_cli_fragments, |
| 5141 | int expected_srv_fragments) |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5142 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5143 | test_app_data(mfl, cli_msg_len, srv_msg_len, expected_cli_fragments, |
| 5144 | expected_srv_fragments, 0); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5145 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 5146 | goto exit; |
| 5147 | } |
| 5148 | /* END_CASE */ |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 5149 | |
Andrzej Kurek | 9113df8 | 2023-01-19 18:26:31 -0500 | [diff] [blame] | 5150 | /* 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] | 5151 | void app_data_dtls(int mfl, int cli_msg_len, int srv_msg_len, |
| 5152 | int expected_cli_fragments, |
| 5153 | int expected_srv_fragments) |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5154 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5155 | test_app_data(mfl, cli_msg_len, srv_msg_len, expected_cli_fragments, |
| 5156 | expected_srv_fragments, 1); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5157 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 5158 | goto exit; |
| 5159 | } |
| 5160 | /* END_CASE */ |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 5161 | |
Andrzej Kurek | 9113df8 | 2023-01-19 18:26:31 -0500 | [diff] [blame] | 5162 | /* 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] | 5163 | void handshake_serialization() |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5164 | { |
| 5165 | handshake_test_options options; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5166 | init_handshake_options(&options); |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 5167 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5168 | options.serialize = 1; |
| 5169 | options.dtls = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5170 | perform_handshake(&options); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5171 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 5172 | goto exit; |
Andrzej Kurek | 6e518ab | 2022-06-11 05:08:38 -0400 | [diff] [blame] | 5173 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5174 | free_handshake_options(&options); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5175 | } |
| 5176 | /* END_CASE */ |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 5177 | |
Andrzej Kurek | a86cef3 | 2023-01-19 18:27:57 -0500 | [diff] [blame] | 5178 | /* 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] | 5179 | void handshake_fragmentation(int mfl, |
| 5180 | int expected_srv_hs_fragmentation, |
| 5181 | int expected_cli_hs_fragmentation) |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 5182 | { |
| 5183 | handshake_test_options options; |
| 5184 | log_pattern srv_pattern, cli_pattern; |
| 5185 | |
| 5186 | srv_pattern.pattern = cli_pattern.pattern = "found fragmented DTLS handshake"; |
| 5187 | srv_pattern.counter = 0; |
| 5188 | cli_pattern.counter = 0; |
| 5189 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5190 | init_handshake_options(&options); |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 5191 | options.dtls = 1; |
| 5192 | options.mfl = mfl; |
Darryl Green | aad82f9 | 2019-12-02 10:53:11 +0000 | [diff] [blame] | 5193 | /* Set cipher to one using CBC so that record splitting can be tested */ |
| 5194 | options.cipher = "TLS-DHE-RSA-WITH-AES-256-CBC-SHA256"; |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 5195 | options.srv_auth_mode = MBEDTLS_SSL_VERIFY_REQUIRED; |
| 5196 | options.srv_log_obj = &srv_pattern; |
| 5197 | options.cli_log_obj = &cli_pattern; |
| 5198 | options.srv_log_fun = log_analyzer; |
| 5199 | options.cli_log_fun = log_analyzer; |
| 5200 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5201 | perform_handshake(&options); |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 5202 | |
| 5203 | /* Test if the server received a fragmented handshake */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5204 | if (expected_srv_hs_fragmentation) { |
| 5205 | TEST_ASSERT(srv_pattern.counter >= 1); |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 5206 | } |
| 5207 | /* Test if the client received a fragmented handshake */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5208 | if (expected_cli_hs_fragmentation) { |
| 5209 | TEST_ASSERT(cli_pattern.counter >= 1); |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 5210 | } |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 5211 | |
| 5212 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5213 | free_handshake_options(&options); |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 5214 | } |
| 5215 | /* END_CASE */ |
| 5216 | |
Andrzej Kurek | 9113df8 | 2023-01-19 18:26:31 -0500 | [diff] [blame] | 5217 | /* 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] | 5218 | void renegotiation(int legacy_renegotiation) |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5219 | { |
| 5220 | handshake_test_options options; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5221 | init_handshake_options(&options); |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 5222 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5223 | options.renegotiate = 1; |
| 5224 | options.legacy_renegotiation = legacy_renegotiation; |
| 5225 | options.dtls = 1; |
Andrzej Kurek | 316da1f | 2020-02-26 09:03:47 -0500 | [diff] [blame] | 5226 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5227 | perform_handshake(&options); |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 5228 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 5229 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 5230 | goto exit; |
Andrzej Kurek | ddb8cd6 | 2022-07-04 16:07:28 -0400 | [diff] [blame] | 5231 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5232 | free_handshake_options(&options); |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 5233 | } |
| 5234 | /* END_CASE */ |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 5235 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5236 | /* 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] | 5237 | void resize_buffers(int mfl, int renegotiation, int legacy_renegotiation, |
| 5238 | int serialize, int dtls, char *cipher) |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 5239 | { |
| 5240 | handshake_test_options options; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5241 | init_handshake_options(&options); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 5242 | |
| 5243 | options.mfl = mfl; |
Andrzej Kurek | 8ea6872 | 2020-04-03 06:40:47 -0400 | [diff] [blame] | 5244 | options.cipher = cipher; |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 5245 | options.renegotiate = renegotiation; |
| 5246 | options.legacy_renegotiation = legacy_renegotiation; |
| 5247 | options.serialize = serialize; |
| 5248 | options.dtls = dtls; |
| 5249 | options.resize_buffers = 1; |
| 5250 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5251 | perform_handshake(&options); |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 5252 | |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 5253 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 5254 | goto exit; |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 5255 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5256 | free_handshake_options(&options); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 5257 | } |
| 5258 | /* END_CASE */ |
| 5259 | |
Andrzej Kurek | 9113df8 | 2023-01-19 18:26:31 -0500 | [diff] [blame] | 5260 | /* 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] | 5261 | void resize_buffers_serialize_mfl(int mfl) |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 5262 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5263 | test_resize_buffers(mfl, 0, MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION, 1, 1, |
| 5264 | (char *) ""); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 5265 | |
| 5266 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 5267 | goto exit; |
| 5268 | } |
| 5269 | /* END_CASE */ |
| 5270 | |
Andrzej Kurek | 9113df8 | 2023-01-19 18:26:31 -0500 | [diff] [blame] | 5271 | /* 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] | 5272 | void resize_buffers_renegotiate_mfl(int mfl, int legacy_renegotiation, |
| 5273 | char *cipher) |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 5274 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5275 | test_resize_buffers(mfl, 1, legacy_renegotiation, 0, 1, cipher); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 5276 | |
| 5277 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 5278 | goto exit; |
| 5279 | } |
| 5280 | /* END_CASE */ |
Manuel Pégourié-Gonnard | 045f094 | 2020-07-02 11:34:02 +0200 | [diff] [blame] | 5281 | |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 5282 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */ |
Hanno Becker | 6667ffd | 2021-04-19 21:59:22 +0100 | [diff] [blame] | 5283 | void test_multiple_psks() |
| 5284 | { |
| 5285 | unsigned char psk0[10] = { 0 }; |
| 5286 | unsigned char psk0_identity[] = { 'f', 'o', 'o' }; |
| 5287 | |
| 5288 | unsigned char psk1[10] = { 0 }; |
| 5289 | unsigned char psk1_identity[] = { 'b', 'a', 'r' }; |
| 5290 | |
| 5291 | mbedtls_ssl_config conf; |
| 5292 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5293 | USE_PSA_INIT(); |
| 5294 | mbedtls_ssl_config_init(&conf); |
Hanno Becker | 6667ffd | 2021-04-19 21:59:22 +0100 | [diff] [blame] | 5295 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5296 | TEST_ASSERT(mbedtls_ssl_conf_psk(&conf, |
| 5297 | psk0, sizeof(psk0), |
| 5298 | psk0_identity, sizeof(psk0_identity)) == 0); |
| 5299 | TEST_ASSERT(mbedtls_ssl_conf_psk(&conf, |
| 5300 | psk1, sizeof(psk1), |
| 5301 | psk1_identity, sizeof(psk1_identity)) == |
| 5302 | MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE); |
Hanno Becker | 6667ffd | 2021-04-19 21:59:22 +0100 | [diff] [blame] | 5303 | |
| 5304 | exit: |
| 5305 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5306 | mbedtls_ssl_config_free(&conf); |
Neil Armstrong | 4c3b4e0 | 2022-05-03 09:24:26 +0200 | [diff] [blame] | 5307 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5308 | USE_PSA_DONE(); |
Hanno Becker | 6667ffd | 2021-04-19 21:59:22 +0100 | [diff] [blame] | 5309 | } |
| 5310 | /* END_CASE */ |
| 5311 | |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 5312 | /* 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] | 5313 | void test_multiple_psks_opaque(int mode) |
Hanno Becker | 6667ffd | 2021-04-19 21:59:22 +0100 | [diff] [blame] | 5314 | { |
| 5315 | /* |
| 5316 | * Mode 0: Raw PSK, then opaque PSK |
| 5317 | * Mode 1: Opaque PSK, then raw PSK |
| 5318 | * Mode 2: 2x opaque PSK |
| 5319 | */ |
| 5320 | |
| 5321 | unsigned char psk0_raw[10] = { 0 }; |
| 5322 | unsigned char psk0_raw_identity[] = { 'f', 'o', 'o' }; |
| 5323 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5324 | 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] | 5325 | |
Hanno Becker | 6667ffd | 2021-04-19 21:59:22 +0100 | [diff] [blame] | 5326 | unsigned char psk0_opaque_identity[] = { 'f', 'o', 'o' }; |
| 5327 | |
| 5328 | unsigned char psk1_raw[10] = { 0 }; |
| 5329 | unsigned char psk1_raw_identity[] = { 'b', 'a', 'r' }; |
| 5330 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5331 | 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] | 5332 | |
Hanno Becker | 6667ffd | 2021-04-19 21:59:22 +0100 | [diff] [blame] | 5333 | unsigned char psk1_opaque_identity[] = { 'b', 'a', 'r' }; |
| 5334 | |
| 5335 | mbedtls_ssl_config conf; |
| 5336 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5337 | USE_PSA_INIT(); |
| 5338 | mbedtls_ssl_config_init(&conf); |
Hanno Becker | 6667ffd | 2021-04-19 21:59:22 +0100 | [diff] [blame] | 5339 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5340 | switch (mode) { |
Hanno Becker | 6667ffd | 2021-04-19 21:59:22 +0100 | [diff] [blame] | 5341 | case 0: |
| 5342 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5343 | TEST_ASSERT(mbedtls_ssl_conf_psk(&conf, |
| 5344 | psk0_raw, sizeof(psk0_raw), |
| 5345 | psk0_raw_identity, sizeof(psk0_raw_identity)) |
| 5346 | == 0); |
| 5347 | TEST_ASSERT(mbedtls_ssl_conf_psk_opaque(&conf, |
| 5348 | psk1_opaque, |
| 5349 | psk1_opaque_identity, |
| 5350 | sizeof(psk1_opaque_identity)) |
| 5351 | == MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE); |
Hanno Becker | 6667ffd | 2021-04-19 21:59:22 +0100 | [diff] [blame] | 5352 | break; |
| 5353 | |
| 5354 | case 1: |
| 5355 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5356 | TEST_ASSERT(mbedtls_ssl_conf_psk_opaque(&conf, |
| 5357 | psk0_opaque, |
| 5358 | psk0_opaque_identity, |
| 5359 | sizeof(psk0_opaque_identity)) |
| 5360 | == 0); |
| 5361 | TEST_ASSERT(mbedtls_ssl_conf_psk(&conf, |
| 5362 | psk1_raw, sizeof(psk1_raw), |
| 5363 | psk1_raw_identity, sizeof(psk1_raw_identity)) |
| 5364 | == MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE); |
Hanno Becker | 6667ffd | 2021-04-19 21:59:22 +0100 | [diff] [blame] | 5365 | |
| 5366 | break; |
| 5367 | |
| 5368 | case 2: |
| 5369 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5370 | TEST_ASSERT(mbedtls_ssl_conf_psk_opaque(&conf, |
| 5371 | psk0_opaque, |
| 5372 | psk0_opaque_identity, |
| 5373 | sizeof(psk0_opaque_identity)) |
| 5374 | == 0); |
| 5375 | TEST_ASSERT(mbedtls_ssl_conf_psk_opaque(&conf, |
| 5376 | psk1_opaque, |
| 5377 | psk1_opaque_identity, |
| 5378 | sizeof(psk1_opaque_identity)) |
| 5379 | == MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE); |
Hanno Becker | 6667ffd | 2021-04-19 21:59:22 +0100 | [diff] [blame] | 5380 | |
| 5381 | break; |
| 5382 | |
| 5383 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5384 | TEST_ASSERT(0); |
Hanno Becker | 6667ffd | 2021-04-19 21:59:22 +0100 | [diff] [blame] | 5385 | break; |
| 5386 | } |
| 5387 | |
| 5388 | exit: |
| 5389 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5390 | mbedtls_ssl_config_free(&conf); |
| 5391 | USE_PSA_DONE(); |
Hanno Becker | 6667ffd | 2021-04-19 21:59:22 +0100 | [diff] [blame] | 5392 | |
| 5393 | } |
| 5394 | /* END_CASE */ |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 5395 | |
Ronald Cron | 37bdaab | 2022-03-30 16:45:51 +0200 | [diff] [blame] | 5396 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5397 | void conf_version(int endpoint, int transport, |
| 5398 | int min_tls_version, int max_tls_version, |
| 5399 | int expected_ssl_setup_result) |
Ronald Cron | 37bdaab | 2022-03-30 16:45:51 +0200 | [diff] [blame] | 5400 | { |
| 5401 | mbedtls_ssl_config conf; |
| 5402 | mbedtls_ssl_context ssl; |
| 5403 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5404 | mbedtls_ssl_config_init(&conf); |
| 5405 | mbedtls_ssl_init(&ssl); |
Ronald Cron | 37bdaab | 2022-03-30 16:45:51 +0200 | [diff] [blame] | 5406 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5407 | mbedtls_ssl_conf_endpoint(&conf, endpoint); |
| 5408 | mbedtls_ssl_conf_transport(&conf, transport); |
| 5409 | mbedtls_ssl_conf_min_tls_version(&conf, min_tls_version); |
| 5410 | mbedtls_ssl_conf_max_tls_version(&conf, max_tls_version); |
Ronald Cron | 37bdaab | 2022-03-30 16:45:51 +0200 | [diff] [blame] | 5411 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5412 | TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == expected_ssl_setup_result); |
Ronald Cron | 37bdaab | 2022-03-30 16:45:51 +0200 | [diff] [blame] | 5413 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5414 | mbedtls_ssl_free(&ssl); |
| 5415 | mbedtls_ssl_config_free(&conf); |
Ronald Cron | 37bdaab | 2022-03-30 16:45:51 +0200 | [diff] [blame] | 5416 | } |
| 5417 | /* END_CASE */ |
| 5418 | |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 5419 | /* 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 */ |
| 5420 | void conf_curve() |
| 5421 | { |
| 5422 | |
| 5423 | mbedtls_ecp_group_id curve_list[] = { MBEDTLS_ECP_DP_SECP192R1, |
| 5424 | MBEDTLS_ECP_DP_SECP224R1, |
| 5425 | MBEDTLS_ECP_DP_SECP256R1, |
| 5426 | MBEDTLS_ECP_DP_NONE }; |
Tom Cosgrove | a327b52 | 2022-08-03 08:33:06 +0100 | [diff] [blame] | 5427 | uint16_t iana_tls_group_list[] = { MBEDTLS_SSL_IANA_TLS_GROUP_SECP192R1, |
| 5428 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP224R1, |
| 5429 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, |
| 5430 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE }; |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 5431 | |
| 5432 | mbedtls_ssl_config conf; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5433 | mbedtls_ssl_config_init(&conf); |
Jerry Yu | baa4934 | 2022-02-15 10:26:40 +0800 | [diff] [blame] | 5434 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5435 | mbedtls_ssl_conf_max_tls_version(&conf, MBEDTLS_SSL_VERSION_TLS1_2); |
| 5436 | mbedtls_ssl_conf_min_tls_version(&conf, MBEDTLS_SSL_VERSION_TLS1_2); |
Jerry Yu | baa4934 | 2022-02-15 10:26:40 +0800 | [diff] [blame] | 5437 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5438 | mbedtls_ssl_conf_max_tls_version(&conf, MBEDTLS_SSL_VERSION_TLS1_3); |
| 5439 | mbedtls_ssl_conf_min_tls_version(&conf, MBEDTLS_SSL_VERSION_TLS1_3); |
Jerry Yu | baa4934 | 2022-02-15 10:26:40 +0800 | [diff] [blame] | 5440 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5441 | mbedtls_ssl_conf_curves(&conf, curve_list); |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 5442 | |
| 5443 | mbedtls_ssl_context ssl; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5444 | mbedtls_ssl_init(&ssl); |
| 5445 | TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0); |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 5446 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5447 | TEST_ASSERT(ssl.handshake != NULL && ssl.handshake->group_list != NULL); |
| 5448 | TEST_ASSERT(ssl.conf != NULL && ssl.conf->group_list == NULL); |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 5449 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5450 | TEST_EQUAL(ssl.handshake->group_list[ARRAY_LENGTH(iana_tls_group_list) - 1], |
| 5451 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE); |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 5452 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5453 | for (size_t i = 0; i < ARRAY_LENGTH(iana_tls_group_list); i++) { |
| 5454 | TEST_EQUAL(iana_tls_group_list[i], ssl.handshake->group_list[i]); |
| 5455 | } |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 5456 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5457 | mbedtls_ssl_free(&ssl); |
| 5458 | mbedtls_ssl_config_free(&conf); |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 5459 | } |
| 5460 | /* END_CASE */ |
| 5461 | |
| 5462 | /* BEGIN_CASE depends_on:MBEDTLS_DEPRECATED_REMOVED */ |
| 5463 | void conf_group() |
| 5464 | { |
| 5465 | uint16_t iana_tls_group_list[] = { MBEDTLS_SSL_IANA_TLS_GROUP_SECP192R1, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5466 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP224R1, |
| 5467 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, |
| 5468 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE }; |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 5469 | |
| 5470 | mbedtls_ssl_config conf; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5471 | mbedtls_ssl_config_init(&conf); |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 5472 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5473 | mbedtls_ssl_conf_max_tls_version(&conf, MBEDTLS_SSL_VERSION_TLS1_2); |
| 5474 | mbedtls_ssl_conf_min_tls_version(&conf, MBEDTLS_SSL_VERSION_TLS1_2); |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 5475 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5476 | mbedtls_ssl_conf_groups(&conf, iana_tls_group_list); |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 5477 | |
| 5478 | mbedtls_ssl_context ssl; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5479 | mbedtls_ssl_init(&ssl); |
| 5480 | TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0); |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 5481 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5482 | TEST_ASSERT(ssl.conf != NULL && ssl.conf->group_list != NULL); |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 5483 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5484 | TEST_EQUAL(ssl.conf->group_list[ARRAY_LENGTH(iana_tls_group_list) - 1], |
| 5485 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE); |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 5486 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5487 | for (size_t i = 0; i < ARRAY_LENGTH(iana_tls_group_list); i++) { |
| 5488 | TEST_EQUAL(iana_tls_group_list[i], ssl.conf->group_list[i]); |
| 5489 | } |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 5490 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5491 | mbedtls_ssl_free(&ssl); |
| 5492 | mbedtls_ssl_config_free(&conf); |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 5493 | } |
| 5494 | /* END_CASE */ |
Paul Elliott | b9af2db | 2022-03-09 15:34:37 +0000 | [diff] [blame] | 5495 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5496 | /* 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] | 5497 | void force_bad_session_id_len() |
Andrzej Kurek | 514683a | 2022-06-10 10:33:05 -0400 | [diff] [blame] | 5498 | { |
| 5499 | enum { BUFFSIZE = 1024 }; |
| 5500 | handshake_test_options options; |
| 5501 | mbedtls_endpoint client, server; |
| 5502 | log_pattern srv_pattern, cli_pattern; |
| 5503 | mbedtls_test_message_socket_context server_context, client_context; |
| 5504 | |
| 5505 | srv_pattern.pattern = cli_pattern.pattern = "cache did not store session"; |
| 5506 | srv_pattern.counter = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5507 | init_handshake_options(&options); |
Andrzej Kurek | 514683a | 2022-06-10 10:33:05 -0400 | [diff] [blame] | 5508 | |
| 5509 | options.srv_log_obj = &srv_pattern; |
| 5510 | options.srv_log_fun = log_analyzer; |
| 5511 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5512 | USE_PSA_INIT(); |
| 5513 | mbedtls_platform_zeroize(&client, sizeof(client)); |
| 5514 | mbedtls_platform_zeroize(&server, sizeof(server)); |
Andrzej Kurek | 514683a | 2022-06-10 10:33:05 -0400 | [diff] [blame] | 5515 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5516 | mbedtls_message_socket_init(&server_context); |
| 5517 | mbedtls_message_socket_init(&client_context); |
Andrzej Kurek | 514683a | 2022-06-10 10:33:05 -0400 | [diff] [blame] | 5518 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5519 | TEST_ASSERT(mbedtls_endpoint_init(&client, MBEDTLS_SSL_IS_CLIENT, |
| 5520 | &options, NULL, NULL, |
| 5521 | NULL, NULL) == 0); |
Andrzej Kurek | 514683a | 2022-06-10 10:33:05 -0400 | [diff] [blame] | 5522 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5523 | TEST_ASSERT(mbedtls_endpoint_init(&server, MBEDTLS_SSL_IS_SERVER, |
| 5524 | &options, NULL, NULL, NULL, |
| 5525 | NULL) == 0); |
Andrzej Kurek | 514683a | 2022-06-10 10:33:05 -0400 | [diff] [blame] | 5526 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5527 | mbedtls_debug_set_threshold(1); |
| 5528 | mbedtls_ssl_conf_dbg(&server.conf, options.srv_log_fun, |
| 5529 | options.srv_log_obj); |
Andrzej Kurek | 514683a | 2022-06-10 10:33:05 -0400 | [diff] [blame] | 5530 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5531 | TEST_ASSERT(mbedtls_mock_socket_connect(&(client.socket), |
| 5532 | &(server.socket), |
| 5533 | BUFFSIZE) == 0); |
Andrzej Kurek | 514683a | 2022-06-10 10:33:05 -0400 | [diff] [blame] | 5534 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5535 | TEST_ASSERT(mbedtls_move_handshake_to_state(&(client.ssl), |
| 5536 | &(server.ssl), |
| 5537 | MBEDTLS_SSL_HANDSHAKE_WRAPUP) |
| 5538 | == 0); |
Andrzej Kurek | 514683a | 2022-06-10 10:33:05 -0400 | [diff] [blame] | 5539 | /* Force a bad session_id_len that will be read by the server in |
| 5540 | * mbedtls_ssl_cache_set. */ |
| 5541 | server.ssl.session_negotiate->id_len = 33; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5542 | if (options.cli_msg_len != 0 || options.srv_msg_len != 0) { |
Andrzej Kurek | 514683a | 2022-06-10 10:33:05 -0400 | [diff] [blame] | 5543 | /* Start data exchanging test */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5544 | TEST_ASSERT(mbedtls_exchange_data(&(client.ssl), options.cli_msg_len, |
| 5545 | options.expected_cli_fragments, |
| 5546 | &(server.ssl), options.srv_msg_len, |
| 5547 | options.expected_srv_fragments) |
| 5548 | == 0); |
Andrzej Kurek | 514683a | 2022-06-10 10:33:05 -0400 | [diff] [blame] | 5549 | } |
Andrzej Kurek | 514683a | 2022-06-10 10:33:05 -0400 | [diff] [blame] | 5550 | |
| 5551 | /* Make sure that the cache did not store the session */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5552 | TEST_EQUAL(srv_pattern.counter, 1); |
Andrzej Kurek | 514683a | 2022-06-10 10:33:05 -0400 | [diff] [blame] | 5553 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5554 | mbedtls_endpoint_free(&client, NULL); |
| 5555 | mbedtls_endpoint_free(&server, NULL); |
| 5556 | free_handshake_options(&options); |
| 5557 | mbedtls_debug_set_threshold(0); |
| 5558 | USE_PSA_DONE(); |
Andrzej Kurek | 514683a | 2022-06-10 10:33:05 -0400 | [diff] [blame] | 5559 | } |
| 5560 | /* END_CASE */ |
| 5561 | |
Andrzej Kurek | ed4d217 | 2022-06-08 11:57:57 -0400 | [diff] [blame] | 5562 | /* 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] | 5563 | void cookie_parsing(data_t *cookie, int exp_ret) |
Andrzej Kurek | cfb0194 | 2022-06-06 13:08:23 -0400 | [diff] [blame] | 5564 | { |
| 5565 | mbedtls_ssl_context ssl; |
| 5566 | mbedtls_ssl_config conf; |
| 5567 | size_t len; |
| 5568 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5569 | mbedtls_ssl_init(&ssl); |
| 5570 | mbedtls_ssl_config_init(&conf); |
| 5571 | TEST_EQUAL(mbedtls_ssl_config_defaults(&conf, MBEDTLS_SSL_IS_SERVER, |
| 5572 | MBEDTLS_SSL_TRANSPORT_DATAGRAM, |
| 5573 | MBEDTLS_SSL_PRESET_DEFAULT), |
| 5574 | 0); |
Andrzej Kurek | cfb0194 | 2022-06-06 13:08:23 -0400 | [diff] [blame] | 5575 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5576 | TEST_EQUAL(mbedtls_ssl_setup(&ssl, &conf), 0); |
| 5577 | TEST_EQUAL(mbedtls_ssl_check_dtls_clihlo_cookie(&ssl, ssl.cli_id, |
| 5578 | ssl.cli_id_len, |
| 5579 | cookie->x, cookie->len, |
| 5580 | ssl.out_buf, |
| 5581 | MBEDTLS_SSL_OUT_CONTENT_LEN, |
| 5582 | &len), |
| 5583 | exp_ret); |
Andrzej Kurek | cfb0194 | 2022-06-06 13:08:23 -0400 | [diff] [blame] | 5584 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5585 | mbedtls_ssl_free(&ssl); |
| 5586 | mbedtls_ssl_config_free(&conf); |
Andrzej Kurek | cfb0194 | 2022-06-06 13:08:23 -0400 | [diff] [blame] | 5587 | } |
| 5588 | /* END_CASE */ |
| 5589 | |
Paul Elliott | b9af2db | 2022-03-09 15:34:37 +0000 | [diff] [blame] | 5590 | /* BEGIN_CASE depends_on:MBEDTLS_TIMING_C:MBEDTLS_HAVE_TIME */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5591 | void timing_final_delay_accessor() |
Paul Elliott | b9af2db | 2022-03-09 15:34:37 +0000 | [diff] [blame] | 5592 | { |
| 5593 | mbedtls_timing_delay_context delay_context; |
| 5594 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5595 | mbedtls_timing_set_delay(&delay_context, 50, 100); |
Paul Elliott | b9af2db | 2022-03-09 15:34:37 +0000 | [diff] [blame] | 5596 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5597 | TEST_ASSERT(mbedtls_timing_get_final_delay(&delay_context) == 100); |
Paul Elliott | b9af2db | 2022-03-09 15:34:37 +0000 | [diff] [blame] | 5598 | } |
| 5599 | /* END_CASE */ |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 5600 | |
| 5601 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5602 | void cid_sanity() |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 5603 | { |
| 5604 | mbedtls_ssl_context ssl; |
| 5605 | mbedtls_ssl_config conf; |
| 5606 | |
| 5607 | unsigned char own_cid[MBEDTLS_SSL_CID_IN_LEN_MAX]; |
| 5608 | unsigned char test_cid[MBEDTLS_SSL_CID_IN_LEN_MAX]; |
| 5609 | int cid_enabled; |
| 5610 | size_t own_cid_len; |
| 5611 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5612 | mbedtls_test_rnd_std_rand(NULL, own_cid, sizeof(own_cid)); |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 5613 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5614 | mbedtls_ssl_init(&ssl); |
| 5615 | mbedtls_ssl_config_init(&conf); |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 5616 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5617 | TEST_ASSERT(mbedtls_ssl_config_defaults(&conf, |
| 5618 | MBEDTLS_SSL_IS_CLIENT, |
| 5619 | MBEDTLS_SSL_TRANSPORT_STREAM, |
| 5620 | MBEDTLS_SSL_PRESET_DEFAULT) |
| 5621 | == 0); |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 5622 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5623 | TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0); |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 5624 | |
| 5625 | /* Can't use CID functions with stream transport. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5626 | TEST_ASSERT(mbedtls_ssl_set_cid(&ssl, MBEDTLS_SSL_CID_ENABLED, own_cid, |
| 5627 | sizeof(own_cid)) |
| 5628 | == MBEDTLS_ERR_SSL_BAD_INPUT_DATA); |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 5629 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5630 | TEST_ASSERT(mbedtls_ssl_get_own_cid(&ssl, &cid_enabled, test_cid, |
| 5631 | &own_cid_len) |
| 5632 | == MBEDTLS_ERR_SSL_BAD_INPUT_DATA); |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 5633 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5634 | TEST_ASSERT(mbedtls_ssl_config_defaults(&conf, |
| 5635 | MBEDTLS_SSL_IS_CLIENT, |
| 5636 | MBEDTLS_SSL_TRANSPORT_DATAGRAM, |
| 5637 | MBEDTLS_SSL_PRESET_DEFAULT) |
| 5638 | == 0); |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 5639 | |
| 5640 | /* Attempt to set config cid size too big. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5641 | TEST_ASSERT(mbedtls_ssl_conf_cid(&conf, MBEDTLS_SSL_CID_IN_LEN_MAX + 1, |
| 5642 | MBEDTLS_SSL_UNEXPECTED_CID_IGNORE) |
| 5643 | == MBEDTLS_ERR_SSL_BAD_INPUT_DATA); |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 5644 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5645 | TEST_ASSERT(mbedtls_ssl_conf_cid(&conf, sizeof(own_cid), |
| 5646 | MBEDTLS_SSL_UNEXPECTED_CID_IGNORE) |
| 5647 | == 0); |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 5648 | |
| 5649 | /* Attempt to set CID length not matching config. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5650 | TEST_ASSERT(mbedtls_ssl_set_cid(&ssl, MBEDTLS_SSL_CID_ENABLED, own_cid, |
| 5651 | MBEDTLS_SSL_CID_IN_LEN_MAX - 1) |
| 5652 | == MBEDTLS_ERR_SSL_BAD_INPUT_DATA); |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 5653 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5654 | TEST_ASSERT(mbedtls_ssl_set_cid(&ssl, MBEDTLS_SSL_CID_ENABLED, own_cid, |
| 5655 | sizeof(own_cid)) |
| 5656 | == 0); |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 5657 | |
| 5658 | /* Test we get back what we put in. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5659 | TEST_ASSERT(mbedtls_ssl_get_own_cid(&ssl, &cid_enabled, test_cid, |
| 5660 | &own_cid_len) |
| 5661 | == 0); |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 5662 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5663 | TEST_EQUAL(cid_enabled, MBEDTLS_SSL_CID_ENABLED); |
| 5664 | ASSERT_COMPARE(own_cid, own_cid_len, test_cid, own_cid_len); |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 5665 | |
| 5666 | /* Test disabling works. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5667 | TEST_ASSERT(mbedtls_ssl_set_cid(&ssl, MBEDTLS_SSL_CID_DISABLED, NULL, |
| 5668 | 0) |
| 5669 | == 0); |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 5670 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5671 | TEST_ASSERT(mbedtls_ssl_get_own_cid(&ssl, &cid_enabled, test_cid, |
| 5672 | &own_cid_len) |
| 5673 | == 0); |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 5674 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5675 | TEST_EQUAL(cid_enabled, MBEDTLS_SSL_CID_DISABLED); |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 5676 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5677 | mbedtls_ssl_free(&ssl); |
| 5678 | mbedtls_ssl_config_free(&conf); |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 5679 | } |
| 5680 | /* END_CASE */ |
| 5681 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5682 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED:MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_ECP_DP_SECP256R1_ENABLED:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_ECDSA_C */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5683 | void raw_key_agreement_fail(int bad_server_ecdhe_key) |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 5684 | { |
| 5685 | enum { BUFFSIZE = 17000 }; |
| 5686 | mbedtls_endpoint client, server; |
| 5687 | mbedtls_psa_stats_t stats; |
Andrzej Kurek | 39d88d4 | 2022-03-31 06:30:54 -0400 | [diff] [blame] | 5688 | size_t free_slots_before = -1; |
Andrzej Kurek | 626a931 | 2022-06-10 11:07:39 -0400 | [diff] [blame] | 5689 | handshake_test_options options; |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 5690 | |
Andrzej Kurek | cc28e9a | 2022-03-08 18:36:35 -0500 | [diff] [blame] | 5691 | uint16_t iana_tls_group_list[] = { MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, |
| 5692 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE }; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5693 | USE_PSA_INIT(); |
| 5694 | mbedtls_platform_zeroize(&client, sizeof(client)); |
| 5695 | mbedtls_platform_zeroize(&server, sizeof(server)); |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 5696 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5697 | init_handshake_options(&options); |
Andrzej Kurek | 626a931 | 2022-06-10 11:07:39 -0400 | [diff] [blame] | 5698 | options.pk_alg = MBEDTLS_PK_ECDSA; |
| 5699 | |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 5700 | /* Client side, force SECP256R1 to make one key bitflip fail |
Andrzej Kurek | 83e60ee | 2022-04-14 08:51:41 -0400 | [diff] [blame] | 5701 | * the raw key agreement. Flipping the first byte makes the |
| 5702 | * required 0x04 identifier invalid. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5703 | TEST_EQUAL(mbedtls_endpoint_init(&client, MBEDTLS_SSL_IS_CLIENT, |
| 5704 | &options, NULL, NULL, |
| 5705 | NULL, iana_tls_group_list), 0); |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 5706 | |
| 5707 | /* Server side */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5708 | TEST_EQUAL(mbedtls_endpoint_init(&server, MBEDTLS_SSL_IS_SERVER, |
| 5709 | &options, NULL, NULL, |
| 5710 | NULL, NULL), 0); |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 5711 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5712 | TEST_EQUAL(mbedtls_mock_socket_connect(&(client.socket), |
| 5713 | &(server.socket), |
| 5714 | BUFFSIZE), 0); |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 5715 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5716 | TEST_EQUAL(mbedtls_move_handshake_to_state(&(client.ssl), |
| 5717 | &(server.ssl), |
| 5718 | MBEDTLS_SSL_CLIENT_KEY_EXCHANGE) |
| 5719 | , 0); |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 5720 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5721 | mbedtls_psa_get_stats(&stats); |
Andrzej Kurek | 39d88d4 | 2022-03-31 06:30:54 -0400 | [diff] [blame] | 5722 | /* Save the number of slots in use up to this point. |
| 5723 | * With PSA, one can be used for the ECDH private key. */ |
| 5724 | free_slots_before = stats.empty_slots; |
Andrzej Kurek | cb33bc5 | 2022-03-31 07:17:18 -0400 | [diff] [blame] | 5725 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5726 | if (bad_server_ecdhe_key) { |
Gilles Peskine | b4f874d | 2022-04-08 16:48:09 -0400 | [diff] [blame] | 5727 | /* Force a simulated bitflip in the server key. to make the |
| 5728 | * raw key agreement in ssl_write_client_key_exchange fail. */ |
| 5729 | (client.ssl).handshake->ecdh_psa_peerkey[0] ^= 0x02; |
| 5730 | } |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 5731 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5732 | TEST_EQUAL(mbedtls_move_handshake_to_state(&(client.ssl), |
| 5733 | &(server.ssl), |
| 5734 | MBEDTLS_SSL_HANDSHAKE_OVER), |
| 5735 | bad_server_ecdhe_key ? MBEDTLS_ERR_SSL_HW_ACCEL_FAILED : 0); |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 5736 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5737 | mbedtls_psa_get_stats(&stats); |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 5738 | |
Gilles Peskine | b4f874d | 2022-04-08 16:48:09 -0400 | [diff] [blame] | 5739 | /* Make sure that the key slot is already destroyed in case of failure, |
| 5740 | * without waiting to close the connection. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5741 | if (bad_server_ecdhe_key) { |
| 5742 | TEST_EQUAL(free_slots_before, stats.empty_slots); |
| 5743 | } |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 5744 | |
| 5745 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5746 | mbedtls_endpoint_free(&client, NULL); |
| 5747 | mbedtls_endpoint_free(&server, NULL); |
| 5748 | free_handshake_options(&options); |
Andrzej Kurek | cb33bc5 | 2022-03-31 07:17:18 -0400 | [diff] [blame] | 5749 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5750 | USE_PSA_DONE(); |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 5751 | } |
| 5752 | /* END_CASE */ |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 5753 | /* 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] | 5754 | void tls13_server_certificate_msg_invalid_vector_len() |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 5755 | { |
| 5756 | int ret = -1; |
| 5757 | mbedtls_endpoint client_ep, server_ep; |
| 5758 | unsigned char *buf, *end; |
| 5759 | size_t buf_len; |
| 5760 | int step = 0; |
| 5761 | int expected_result; |
Ronald Cron | e7b9b6b | 2022-06-10 17:24:31 +0200 | [diff] [blame] | 5762 | mbedtls_ssl_chk_buf_ptr_args expected_chk_buf_ptr_args; |
Paul Elliott | 9a8d784 | 2022-07-10 12:48:57 +0100 | [diff] [blame] | 5763 | handshake_test_options client_options; |
| 5764 | handshake_test_options server_options; |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 5765 | |
| 5766 | /* |
| 5767 | * Test set-up |
| 5768 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5769 | USE_PSA_INIT(); |
| 5770 | mbedtls_platform_zeroize(&client_ep, sizeof(client_ep)); |
| 5771 | mbedtls_platform_zeroize(&server_ep, sizeof(server_ep)); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 5772 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5773 | init_handshake_options(&client_options); |
Paul Elliott | 9a8d784 | 2022-07-10 12:48:57 +0100 | [diff] [blame] | 5774 | client_options.pk_alg = MBEDTLS_PK_ECDSA; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5775 | ret = mbedtls_endpoint_init(&client_ep, MBEDTLS_SSL_IS_CLIENT, |
| 5776 | &client_options, NULL, NULL, NULL, NULL); |
| 5777 | TEST_EQUAL(ret, 0); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 5778 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5779 | init_handshake_options(&server_options); |
Paul Elliott | 9a8d784 | 2022-07-10 12:48:57 +0100 | [diff] [blame] | 5780 | server_options.pk_alg = MBEDTLS_PK_ECDSA; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5781 | ret = mbedtls_endpoint_init(&server_ep, MBEDTLS_SSL_IS_SERVER, |
| 5782 | &server_options, NULL, NULL, NULL, NULL); |
| 5783 | TEST_EQUAL(ret, 0); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 5784 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5785 | ret = mbedtls_mock_socket_connect(&(client_ep.socket), |
| 5786 | &(server_ep.socket), 1024); |
| 5787 | TEST_EQUAL(ret, 0); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 5788 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5789 | while (1) { |
| 5790 | mbedtls_test_set_step(++step); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 5791 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5792 | ret = mbedtls_move_handshake_to_state(&(server_ep.ssl), |
| 5793 | &(client_ep.ssl), |
| 5794 | MBEDTLS_SSL_CERTIFICATE_VERIFY); |
| 5795 | TEST_EQUAL(ret, 0); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 5796 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5797 | ret = mbedtls_ssl_flush_output(&(server_ep.ssl)); |
| 5798 | TEST_EQUAL(ret, 0); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 5799 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5800 | ret = mbedtls_move_handshake_to_state(&(client_ep.ssl), |
| 5801 | &(server_ep.ssl), |
| 5802 | MBEDTLS_SSL_SERVER_CERTIFICATE); |
| 5803 | TEST_EQUAL(ret, 0); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 5804 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5805 | ret = mbedtls_ssl_tls13_fetch_handshake_msg(&(client_ep.ssl), |
| 5806 | MBEDTLS_SSL_HS_CERTIFICATE, |
| 5807 | &buf, &buf_len); |
| 5808 | TEST_EQUAL(ret, 0); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 5809 | |
| 5810 | end = buf + buf_len; |
| 5811 | |
| 5812 | /* |
| 5813 | * Tweak server Certificate message and parse it. |
| 5814 | */ |
| 5815 | |
| 5816 | ret = tweak_tls13_certificate_msg_vector_len( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5817 | buf, &end, step, &expected_result, &expected_chk_buf_ptr_args); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 5818 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5819 | if (ret != 0) { |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 5820 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5821 | } |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 5822 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5823 | ret = mbedtls_ssl_tls13_parse_certificate(&(client_ep.ssl), buf, end); |
| 5824 | TEST_EQUAL(ret, expected_result); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 5825 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5826 | TEST_ASSERT(mbedtls_ssl_cmp_chk_buf_ptr_fail_args( |
| 5827 | &expected_chk_buf_ptr_args) == 0); |
Ronald Cron | e7b9b6b | 2022-06-10 17:24:31 +0200 | [diff] [blame] | 5828 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5829 | mbedtls_ssl_reset_chk_buf_ptr_fail_args(); |
Ronald Cron | e7b9b6b | 2022-06-10 17:24:31 +0200 | [diff] [blame] | 5830 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5831 | ret = mbedtls_ssl_session_reset(&(client_ep.ssl)); |
| 5832 | TEST_EQUAL(ret, 0); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 5833 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5834 | ret = mbedtls_ssl_session_reset(&(server_ep.ssl)); |
| 5835 | TEST_EQUAL(ret, 0); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 5836 | } |
| 5837 | |
| 5838 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5839 | mbedtls_ssl_reset_chk_buf_ptr_fail_args(); |
| 5840 | mbedtls_endpoint_free(&client_ep, NULL); |
| 5841 | mbedtls_endpoint_free(&server_ep, NULL); |
| 5842 | free_handshake_options(&client_options); |
| 5843 | free_handshake_options(&server_options); |
| 5844 | USE_PSA_DONE(); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 5845 | } |
| 5846 | /* END_CASE */ |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 5847 | |
| 5848 | /* BEGIN_CASE depends_on:MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5849 | void ssl_ecjpake_set_password(int use_opaque_arg) |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 5850 | { |
| 5851 | mbedtls_ssl_context ssl; |
| 5852 | mbedtls_ssl_config conf; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5853 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 5854 | mbedtls_svc_key_id_t pwd_slot = MBEDTLS_SVC_KEY_ID_INIT; |
| 5855 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 5856 | (void) use_opaque_arg; |
| 5857 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5858 | unsigned char pwd_string[sizeof(ECJPAKE_TEST_PWD)] = ""; |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 5859 | size_t pwd_len = 0; |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 5860 | int ret; |
| 5861 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5862 | USE_PSA_INIT(); |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 5863 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5864 | mbedtls_ssl_init(&ssl); |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 5865 | |
Valerio Setti | e7518ba | 2022-12-02 12:09:43 +0100 | [diff] [blame] | 5866 | /* test with uninitalized SSL context */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5867 | ECJPAKE_TEST_SET_PASSWORD(MBEDTLS_ERR_SSL_BAD_INPUT_DATA); |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 5868 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5869 | mbedtls_ssl_config_init(&conf); |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 5870 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5871 | TEST_EQUAL(mbedtls_ssl_config_defaults(&conf, |
| 5872 | MBEDTLS_SSL_IS_CLIENT, |
| 5873 | MBEDTLS_SSL_TRANSPORT_STREAM, |
| 5874 | MBEDTLS_SSL_PRESET_DEFAULT), 0); |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 5875 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5876 | TEST_EQUAL(mbedtls_ssl_setup(&ssl, &conf), 0); |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 5877 | |
Valerio Setti | ba22c9c | 2022-12-06 11:42:33 +0100 | [diff] [blame] | 5878 | /* 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] | 5879 | ECJPAKE_TEST_SET_PASSWORD(MBEDTLS_ERR_SSL_BAD_INPUT_DATA); |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 5880 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5881 | pwd_len = strlen(ECJPAKE_TEST_PWD); |
| 5882 | memcpy(pwd_string, ECJPAKE_TEST_PWD, pwd_len); |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 5883 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5884 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 5885 | if (use_opaque_arg) { |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 5886 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Valerio Setti | 785116a | 2022-12-12 11:59:25 +0100 | [diff] [blame] | 5887 | psa_key_attributes_t check_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 5888 | |
Valerio Setti | 2a3ffb4 | 2022-12-08 16:27:46 +0100 | [diff] [blame] | 5889 | /* First try with an invalid usage */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5890 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 5891 | psa_set_key_algorithm(&attributes, PSA_ALG_JPAKE); |
| 5892 | psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD); |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 5893 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5894 | PSA_ASSERT(psa_import_key(&attributes, pwd_string, |
| 5895 | pwd_len, &pwd_slot)); |
Valerio Setti | 2a3ffb4 | 2022-12-08 16:27:46 +0100 | [diff] [blame] | 5896 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5897 | ECJPAKE_TEST_SET_PASSWORD(MBEDTLS_ERR_SSL_HW_ACCEL_FAILED); |
Valerio Setti | 2a3ffb4 | 2022-12-08 16:27:46 +0100 | [diff] [blame] | 5898 | |
Valerio Setti | 9d313df | 2022-12-09 11:38:59 +0100 | [diff] [blame] | 5899 | /* check that the opaque key is still valid after failure */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5900 | TEST_EQUAL(psa_get_key_attributes(pwd_slot, &check_attributes), |
| 5901 | PSA_SUCCESS); |
Valerio Setti | 9d313df | 2022-12-09 11:38:59 +0100 | [diff] [blame] | 5902 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5903 | psa_destroy_key(pwd_slot); |
Valerio Setti | 2a3ffb4 | 2022-12-08 16:27:46 +0100 | [diff] [blame] | 5904 | |
| 5905 | /* Then set the correct usage */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5906 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
Valerio Setti | 2a3ffb4 | 2022-12-08 16:27:46 +0100 | [diff] [blame] | 5907 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5908 | PSA_ASSERT(psa_import_key(&attributes, pwd_string, |
| 5909 | pwd_len, &pwd_slot)); |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 5910 | } |
| 5911 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 5912 | |
Valerio Setti | e7518ba | 2022-12-02 12:09:43 +0100 | [diff] [blame] | 5913 | /* final check which should work without errors */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5914 | ECJPAKE_TEST_SET_PASSWORD(0); |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 5915 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5916 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 5917 | if (use_opaque_arg) { |
| 5918 | psa_destroy_key(pwd_slot); |
Valerio Setti | 31e99bb | 2022-12-09 14:35:10 +0100 | [diff] [blame] | 5919 | } |
Valerio Setti | e7518ba | 2022-12-02 12:09:43 +0100 | [diff] [blame] | 5920 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5921 | mbedtls_ssl_free(&ssl); |
| 5922 | mbedtls_ssl_config_free(&conf); |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 5923 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5924 | USE_PSA_DONE(); |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 5925 | } |
| 5926 | /* END_CASE */ |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 5927 | |
Valerio Setti | 2c12185 | 2023-01-09 18:00:39 +0100 | [diff] [blame] | 5928 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5929 | void elliptic_curve_get_properties() |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 5930 | { |
| 5931 | psa_ecc_family_t psa_family; |
| 5932 | size_t psa_bits; |
| 5933 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5934 | USE_PSA_INIT(); |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 5935 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5936 | #if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_521) |
| 5937 | 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] | 5938 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5939 | 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] | 5940 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5941 | #if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512) |
| 5942 | 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] | 5943 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5944 | 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] | 5945 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5946 | #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_384) |
| 5947 | 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] | 5948 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5949 | 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] | 5950 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5951 | #if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384) |
| 5952 | 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] | 5953 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5954 | 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] | 5955 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5956 | #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_256) |
| 5957 | 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] | 5958 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5959 | 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] | 5960 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5961 | #if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_256) |
| 5962 | 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] | 5963 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5964 | 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] | 5965 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5966 | #if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256) |
| 5967 | 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] | 5968 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5969 | 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] | 5970 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5971 | #if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_224) |
| 5972 | 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] | 5973 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5974 | 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] | 5975 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5976 | #if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_224) |
| 5977 | 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] | 5978 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5979 | 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] | 5980 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5981 | #if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_192) |
| 5982 | 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] | 5983 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5984 | 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] | 5985 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5986 | #if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_192) |
| 5987 | 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] | 5988 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5989 | 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] | 5990 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5991 | #if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) || defined(PSA_WANT_ECC_MONTGOMERY_255) |
| 5992 | 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] | 5993 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5994 | 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] | 5995 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5996 | #if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED) || defined(PSA_WANT_ECC_MONTGOMERY_448) |
| 5997 | 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] | 5998 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 5999 | 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] | 6000 | #endif |
| 6001 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 6002 | USE_PSA_DONE(); |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 6003 | } |
| 6004 | /* END_CASE */ |