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> |
Manuel Pégourié-Gonnard | 5e94dde | 2015-05-26 11:57:05 +0200 | [diff] [blame] | 3 | #include <mbedtls/ssl_internal.h> |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4 | #include <mbedtls/ctr_drbg.h> |
| 5 | #include <mbedtls/entropy.h> |
| 6 | #include <mbedtls/certs.h> |
Andrzej Kurek | 941962e | 2020-02-07 09:20:32 -0500 | [diff] [blame] | 7 | #include <mbedtls/timing.h> |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 8 | #include <mbedtls/debug.h> |
Hanno Becker | 73c825a | 2020-09-08 10:52:58 +0100 | [diff] [blame] | 9 | #include <ssl_tls13_keys.h> |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 10 | |
Gabor Mezei | c0ae1cf | 2021-10-20 12:09:35 +0200 | [diff] [blame] | 11 | #include <constant_time_internal.h> |
Manuel Pégourié-Gonnard | 045f094 | 2020-07-02 11:34:02 +0200 | [diff] [blame] | 12 | |
Manuel Pégourié-Gonnard | 9670a59 | 2020-07-10 10:21:46 +0200 | [diff] [blame] | 13 | #include <test/constant_flow.h> |
| 14 | |
Andrzej Kurek | 01bdab3 | 2023-01-17 11:12:11 -0500 | [diff] [blame] | 15 | #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \ |
| 16 | defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \ |
| 17 | defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED) |
| 18 | #define MBEDTLS_CAN_HANDLE_RSA_TEST_KEY |
| 19 | #endif |
| 20 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 21 | enum { |
| 22 | #define MBEDTLS_SSL_TLS1_3_LABEL(name, string) \ |
| 23 | tls1_3_label_ ## name, |
| 24 | MBEDTLS_SSL_TLS1_3_LABEL_LIST |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 25 | #undef MBEDTLS_SSL_TLS1_3_LABEL |
Hanno Becker | 1413bd8 | 2020-09-09 12:46:09 +0100 | [diff] [blame] | 26 | }; |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 27 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 28 | typedef struct log_pattern { |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 29 | const char *pattern; |
| 30 | size_t counter; |
| 31 | } log_pattern; |
| 32 | |
Piotr Nowicki | 438bf3b | 2020-03-10 12:59:10 +0100 | [diff] [blame] | 33 | /* |
| 34 | * 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] | 35 | * this case, it will count the instances of a log_pattern in the received |
| 36 | * logged messages. |
| 37 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 38 | void log_analyzer(void *ctx, int level, |
| 39 | const char *file, int line, |
| 40 | const char *str) |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 41 | { |
| 42 | log_pattern *p = (log_pattern *) ctx; |
| 43 | |
| 44 | (void) level; |
| 45 | (void) line; |
| 46 | (void) file; |
| 47 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 48 | if (NULL != p && |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 49 | NULL != p->pattern && |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 50 | NULL != strstr(str, p->pattern)) { |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 51 | p->counter++; |
| 52 | } |
| 53 | } |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 54 | |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 55 | /* Invalid minor version used when not specifying a min/max version or expecting a test to fail */ |
| 56 | #define TEST_SSL_MINOR_VERSION_NONE -1 |
| 57 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 58 | typedef struct handshake_test_options { |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 59 | const char *cipher; |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 60 | int client_min_version; |
| 61 | int client_max_version; |
| 62 | int server_min_version; |
| 63 | int server_max_version; |
| 64 | int expected_negotiated_version; |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 65 | int pk_alg; |
| 66 | data_t *psk_str; |
| 67 | int dtls; |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 68 | int srv_auth_mode; |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 69 | int serialize; |
| 70 | int mfl; |
| 71 | int cli_msg_len; |
| 72 | int srv_msg_len; |
| 73 | int expected_cli_fragments; |
| 74 | int expected_srv_fragments; |
| 75 | int renegotiate; |
| 76 | int legacy_renegotiation; |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 77 | void *srv_log_obj; |
| 78 | void *cli_log_obj; |
| 79 | void (*srv_log_fun)(void *, int, const char *, int, const char *); |
| 80 | void (*cli_log_fun)(void *, int, const char *, int, const char *); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 81 | int resize_buffers; |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 82 | } handshake_test_options; |
| 83 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 84 | void init_handshake_options(handshake_test_options *opts) |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 85 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 86 | opts->cipher = ""; |
| 87 | opts->client_min_version = TEST_SSL_MINOR_VERSION_NONE; |
| 88 | opts->client_max_version = TEST_SSL_MINOR_VERSION_NONE; |
| 89 | opts->server_min_version = TEST_SSL_MINOR_VERSION_NONE; |
| 90 | opts->server_max_version = TEST_SSL_MINOR_VERSION_NONE; |
| 91 | opts->expected_negotiated_version = MBEDTLS_SSL_MINOR_VERSION_3; |
| 92 | opts->pk_alg = MBEDTLS_PK_RSA; |
| 93 | opts->psk_str = NULL; |
| 94 | opts->dtls = 0; |
| 95 | opts->srv_auth_mode = MBEDTLS_SSL_VERIFY_NONE; |
| 96 | opts->serialize = 0; |
| 97 | opts->mfl = MBEDTLS_SSL_MAX_FRAG_LEN_NONE; |
| 98 | opts->cli_msg_len = 100; |
| 99 | opts->srv_msg_len = 100; |
| 100 | opts->expected_cli_fragments = 1; |
| 101 | opts->expected_srv_fragments = 1; |
| 102 | opts->renegotiate = 0; |
| 103 | opts->legacy_renegotiation = MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION; |
| 104 | opts->srv_log_obj = NULL; |
| 105 | opts->srv_log_obj = NULL; |
| 106 | opts->srv_log_fun = NULL; |
| 107 | opts->cli_log_fun = NULL; |
| 108 | opts->resize_buffers = 1; |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 109 | } |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 110 | /* |
| 111 | * Buffer structure for custom I/O callbacks. |
| 112 | */ |
| 113 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 114 | typedef struct mbedtls_test_buffer { |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 115 | size_t start; |
| 116 | size_t content_length; |
| 117 | size_t capacity; |
| 118 | unsigned char *buffer; |
| 119 | } mbedtls_test_buffer; |
| 120 | |
| 121 | /* |
| 122 | * Initialises \p buf. After calling this function it is safe to call |
| 123 | * `mbedtls_test_buffer_free()` on \p buf. |
| 124 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 125 | void mbedtls_test_buffer_init(mbedtls_test_buffer *buf) |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 126 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 127 | memset(buf, 0, sizeof(*buf)); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | /* |
| 131 | * Sets up \p buf. After calling this function it is safe to call |
| 132 | * `mbedtls_test_buffer_put()` and `mbedtls_test_buffer_get()` on \p buf. |
| 133 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 134 | int mbedtls_test_buffer_setup(mbedtls_test_buffer *buf, size_t capacity) |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 135 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 136 | buf->buffer = (unsigned char *) mbedtls_calloc(capacity, |
| 137 | sizeof(unsigned char)); |
| 138 | if (NULL == buf->buffer) { |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 139 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 140 | } |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 141 | buf->capacity = capacity; |
| 142 | |
| 143 | return 0; |
| 144 | } |
| 145 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 146 | void mbedtls_test_buffer_free(mbedtls_test_buffer *buf) |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 147 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 148 | if (buf->buffer != NULL) { |
| 149 | mbedtls_free(buf->buffer); |
| 150 | } |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 151 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 152 | memset(buf, 0, sizeof(*buf)); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | /* |
| 156 | * Puts \p input_len bytes from the \p input buffer into the ring buffer \p buf. |
| 157 | * |
| 158 | * \p buf must have been initialized and set up by calling |
| 159 | * `mbedtls_test_buffer_init()` and `mbedtls_test_buffer_setup()`. |
| 160 | * |
| 161 | * \retval \p input_len, if the data fits. |
| 162 | * \retval 0 <= value < \p input_len, if the data does not fit. |
| 163 | * \retval -1, if \p buf is NULL, it hasn't been set up or \p input_len is not |
| 164 | * zero and \p input is NULL. |
| 165 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 166 | int mbedtls_test_buffer_put(mbedtls_test_buffer *buf, |
| 167 | const unsigned char *input, size_t input_len) |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 168 | { |
| 169 | size_t overflow = 0; |
| 170 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 171 | if ((buf == NULL) || (buf->buffer == NULL)) { |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 172 | return -1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 173 | } |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 174 | |
| 175 | /* Reduce input_len to a number that fits in the buffer. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 176 | if ((buf->content_length + input_len) > buf->capacity) { |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 177 | input_len = buf->capacity - buf->content_length; |
| 178 | } |
| 179 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 180 | if (input == NULL) { |
| 181 | return (input_len == 0) ? 0 : -1; |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 184 | /* Check if the buffer has not come full circle and free space is not in |
| 185 | * the middle */ |
| 186 | if (buf->start + buf->content_length < buf->capacity) { |
Piotr Nowicki | fb437d7 | 2020-01-13 16:59:12 +0100 | [diff] [blame] | 187 | |
| 188 | /* Calculate the number of bytes that need to be placed at lower memory |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 189 | * address */ |
| 190 | if (buf->start + buf->content_length + input_len |
| 191 | > buf->capacity) { |
| 192 | overflow = (buf->start + buf->content_length + input_len) |
| 193 | % buf->capacity; |
Piotr Nowicki | fb437d7 | 2020-01-13 16:59:12 +0100 | [diff] [blame] | 194 | } |
| 195 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 196 | memcpy(buf->buffer + buf->start + buf->content_length, input, |
| 197 | input_len - overflow); |
| 198 | memcpy(buf->buffer, input + input_len - overflow, overflow); |
Piotr Nowicki | fb437d7 | 2020-01-13 16:59:12 +0100 | [diff] [blame] | 199 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 200 | } else { |
Piotr Nowicki | fb437d7 | 2020-01-13 16:59:12 +0100 | [diff] [blame] | 201 | /* The buffer has come full circle and free space is in the middle */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 202 | memcpy(buf->buffer + buf->start + buf->content_length - buf->capacity, |
| 203 | input, input_len); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 206 | buf->content_length += input_len; |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 207 | return input_len; |
| 208 | } |
| 209 | |
| 210 | /* |
Andrzej Kurek | f777414 | 2020-01-22 06:34:59 -0500 | [diff] [blame] | 211 | * Gets \p output_len bytes from the ring buffer \p buf into the |
| 212 | * \p output buffer. The output buffer can be NULL, in this case a part of the |
| 213 | * ring buffer will be dropped, if the requested length is available. |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 214 | * |
| 215 | * \p buf must have been initialized and set up by calling |
| 216 | * `mbedtls_test_buffer_init()` and `mbedtls_test_buffer_setup()`. |
| 217 | * |
| 218 | * \retval \p output_len, if the data is available. |
| 219 | * \retval 0 <= value < \p output_len, if the data is not available. |
Andrzej Kurek | f777414 | 2020-01-22 06:34:59 -0500 | [diff] [blame] | 220 | * \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] | 221 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 222 | int mbedtls_test_buffer_get(mbedtls_test_buffer *buf, |
| 223 | unsigned char *output, size_t output_len) |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 224 | { |
| 225 | size_t overflow = 0; |
| 226 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 227 | if ((buf == NULL) || (buf->buffer == NULL)) { |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 228 | return -1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 229 | } |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 230 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 231 | if (output == NULL && output_len == 0) { |
Andrzej Kurek | f777414 | 2020-01-22 06:34:59 -0500 | [diff] [blame] | 232 | return 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 233 | } |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 234 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 235 | if (buf->content_length < output_len) { |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 236 | output_len = buf->content_length; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 237 | } |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 238 | |
| 239 | /* Calculate the number of bytes that need to be drawn from lower memory |
| 240 | * address */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 241 | if (buf->start + output_len > buf->capacity) { |
| 242 | overflow = (buf->start + output_len) % buf->capacity; |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 243 | } |
| 244 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 245 | if (output != NULL) { |
| 246 | memcpy(output, buf->buffer + buf->start, output_len - overflow); |
| 247 | memcpy(output + output_len - overflow, buf->buffer, overflow); |
Andrzej Kurek | f777414 | 2020-01-22 06:34:59 -0500 | [diff] [blame] | 248 | } |
| 249 | |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 250 | buf->content_length -= output_len; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 251 | buf->start = (buf->start + output_len) % buf->capacity; |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 252 | |
| 253 | return output_len; |
| 254 | } |
| 255 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 256 | /* |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 257 | * Errors used in the message transport mock tests |
| 258 | */ |
| 259 | #define MBEDTLS_TEST_ERROR_ARG_NULL -11 |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 260 | #define MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED -44 |
| 261 | |
| 262 | /* |
| 263 | * Context for a message metadata queue (fifo) that is on top of the ring buffer. |
| 264 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 265 | typedef struct mbedtls_test_message_queue { |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 266 | size_t *messages; |
| 267 | int pos; |
| 268 | int num; |
| 269 | int capacity; |
| 270 | } mbedtls_test_message_queue; |
| 271 | |
| 272 | /* |
| 273 | * Setup and free functions for the message metadata queue. |
| 274 | * |
| 275 | * \p capacity describes the number of message metadata chunks that can be held |
| 276 | * within the queue. |
| 277 | * |
| 278 | * \retval 0, if a metadata queue of a given length can be allocated. |
| 279 | * \retval MBEDTLS_ERR_SSL_ALLOC_FAILED, if allocation failed. |
| 280 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 281 | int mbedtls_test_message_queue_setup(mbedtls_test_message_queue *queue, |
| 282 | size_t capacity) |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 283 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 284 | queue->messages = (size_t *) mbedtls_calloc(capacity, sizeof(size_t)); |
| 285 | if (NULL == queue->messages) { |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 286 | return MBEDTLS_ERR_SSL_ALLOC_FAILED; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 287 | } |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 288 | |
| 289 | queue->capacity = capacity; |
| 290 | queue->pos = 0; |
| 291 | queue->num = 0; |
| 292 | |
| 293 | return 0; |
| 294 | } |
| 295 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 296 | void mbedtls_test_message_queue_free(mbedtls_test_message_queue *queue) |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 297 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 298 | if (queue == NULL) { |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 299 | return; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 300 | } |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 301 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 302 | if (queue->messages != NULL) { |
| 303 | mbedtls_free(queue->messages); |
| 304 | } |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 305 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 306 | memset(queue, 0, sizeof(*queue)); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | /* |
| 310 | * Push message length information onto the message metadata queue. |
| 311 | * This will become the last element to leave it (fifo). |
| 312 | * |
| 313 | * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null. |
Andrzej Kurek | f46b912 | 2020-02-07 08:19:00 -0500 | [diff] [blame] | 314 | * \retval MBEDTLS_ERR_SSL_WANT_WRITE, if the queue is full. |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 315 | * \retval \p len, if the push was successful. |
| 316 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 317 | int mbedtls_test_message_queue_push_info(mbedtls_test_message_queue *queue, |
| 318 | size_t len) |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 319 | { |
| 320 | int place; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 321 | if (queue == NULL) { |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 322 | return MBEDTLS_TEST_ERROR_ARG_NULL; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 323 | } |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 324 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 325 | if (queue->num >= queue->capacity) { |
Andrzej Kurek | f46b912 | 2020-02-07 08:19:00 -0500 | [diff] [blame] | 326 | return MBEDTLS_ERR_SSL_WANT_WRITE; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 327 | } |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 328 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 329 | place = (queue->pos + queue->num) % queue->capacity; |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 330 | queue->messages[place] = len; |
| 331 | queue->num++; |
| 332 | return len; |
| 333 | } |
| 334 | |
| 335 | /* |
| 336 | * Pop information about the next message length from the queue. This will be |
| 337 | * the oldest inserted message length(fifo). \p msg_len can be null, in which |
| 338 | * case the data will be popped from the queue but not copied anywhere. |
| 339 | * |
| 340 | * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null. |
Andrzej Kurek | f46b912 | 2020-02-07 08:19:00 -0500 | [diff] [blame] | 341 | * \retval MBEDTLS_ERR_SSL_WANT_READ, if the queue is empty. |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 342 | * \retval message length, if the pop was successful, up to the given |
| 343 | \p buf_len. |
| 344 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 345 | int mbedtls_test_message_queue_pop_info(mbedtls_test_message_queue *queue, |
| 346 | size_t buf_len) |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 347 | { |
| 348 | size_t message_length; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 349 | if (queue == NULL) { |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 350 | return MBEDTLS_TEST_ERROR_ARG_NULL; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 351 | } |
| 352 | if (queue->num == 0) { |
Andrzej Kurek | f46b912 | 2020-02-07 08:19:00 -0500 | [diff] [blame] | 353 | return MBEDTLS_ERR_SSL_WANT_READ; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 354 | } |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 355 | |
| 356 | message_length = queue->messages[queue->pos]; |
| 357 | queue->messages[queue->pos] = 0; |
| 358 | queue->num--; |
| 359 | queue->pos++; |
| 360 | queue->pos %= queue->capacity; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 361 | if (queue->pos < 0) { |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 362 | queue->pos += queue->capacity; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 363 | } |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 364 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 365 | return (message_length > buf_len) ? buf_len : message_length; |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | /* |
| 369 | * Take a peek on the info about the next message length from the queue. |
| 370 | * This will be the oldest inserted message length(fifo). |
| 371 | * |
| 372 | * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null. |
Andrzej Kurek | f46b912 | 2020-02-07 08:19:00 -0500 | [diff] [blame] | 373 | * \retval MBEDTLS_ERR_SSL_WANT_READ, if the queue is empty. |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 374 | * \retval 0, if the peek was successful. |
| 375 | * \retval MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED, if the given buffer length is |
| 376 | * too small to fit the message. In this case the \p msg_len will be |
| 377 | * set to the full message length so that the |
| 378 | * caller knows what portion of the message can be dropped. |
| 379 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 380 | int mbedtls_test_message_queue_peek_info(mbedtls_test_message_queue *queue, |
| 381 | size_t buf_len, size_t *msg_len) |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 382 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 383 | if (queue == NULL || msg_len == NULL) { |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 384 | return MBEDTLS_TEST_ERROR_ARG_NULL; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 385 | } |
| 386 | if (queue->num == 0) { |
Andrzej Kurek | f46b912 | 2020-02-07 08:19:00 -0500 | [diff] [blame] | 387 | return MBEDTLS_ERR_SSL_WANT_READ; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 388 | } |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 389 | |
| 390 | *msg_len = queue->messages[queue->pos]; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 391 | return (*msg_len > buf_len) ? MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED : 0; |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 392 | } |
| 393 | /* |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 394 | * Context for the I/O callbacks simulating network connection. |
| 395 | */ |
| 396 | |
| 397 | #define MBEDTLS_MOCK_SOCKET_CONNECTED 1 |
| 398 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 399 | typedef struct mbedtls_mock_socket { |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 400 | int status; |
| 401 | mbedtls_test_buffer *input; |
| 402 | mbedtls_test_buffer *output; |
| 403 | struct mbedtls_mock_socket *peer; |
| 404 | } mbedtls_mock_socket; |
| 405 | |
| 406 | /* |
| 407 | * Setup and teardown functions for mock sockets. |
| 408 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 409 | void mbedtls_mock_socket_init(mbedtls_mock_socket *socket) |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 410 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 411 | memset(socket, 0, sizeof(*socket)); |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | /* |
| 415 | * Closes the socket \p socket. |
| 416 | * |
| 417 | * \p socket must have been previously initialized by calling |
| 418 | * mbedtls_mock_socket_init(). |
| 419 | * |
| 420 | * This function frees all allocated resources and both sockets are aware of the |
| 421 | * new connection state. |
| 422 | * |
| 423 | * That is, this function does not simulate half-open TCP connections and the |
| 424 | * phenomenon that when closing a UDP connection the peer is not aware of the |
| 425 | * connection having been closed. |
| 426 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 427 | void mbedtls_mock_socket_close(mbedtls_mock_socket *socket) |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 428 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 429 | if (socket == NULL) { |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 430 | return; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 431 | } |
| 432 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 433 | if (socket->input != NULL) { |
| 434 | mbedtls_test_buffer_free(socket->input); |
| 435 | mbedtls_free(socket->input); |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 436 | } |
| 437 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 438 | if (socket->output != NULL) { |
| 439 | mbedtls_test_buffer_free(socket->output); |
| 440 | mbedtls_free(socket->output); |
| 441 | } |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 442 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 443 | if (socket->peer != NULL) { |
| 444 | memset(socket->peer, 0, sizeof(*socket->peer)); |
| 445 | } |
| 446 | |
| 447 | memset(socket, 0, sizeof(*socket)); |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | /* |
| 451 | * Establishes a connection between \p peer1 and \p peer2. |
| 452 | * |
| 453 | * \p peer1 and \p peer2 must have been previously initialized by calling |
| 454 | * mbedtls_mock_socket_init(). |
| 455 | * |
Tom Cosgrove | 49f99bc | 2022-12-04 16:44:21 +0000 | [diff] [blame] | 456 | * The capacities of the internal buffers are set to \p bufsize. Setting this to |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 457 | * the correct value allows for simulation of MTU, sanity testing the mock |
| 458 | * implementation and mocking TCP connections with lower memory cost. |
| 459 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 460 | int mbedtls_mock_socket_connect(mbedtls_mock_socket *peer1, |
| 461 | mbedtls_mock_socket *peer2, |
| 462 | size_t bufsize) |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 463 | { |
| 464 | int ret = -1; |
| 465 | |
Piotr Nowicki | d796e19 | 2020-01-28 12:09:47 +0100 | [diff] [blame] | 466 | peer1->output = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 467 | (mbedtls_test_buffer *) mbedtls_calloc(1, sizeof(mbedtls_test_buffer)); |
| 468 | if (peer1->output == NULL) { |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 469 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 470 | goto exit; |
| 471 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 472 | mbedtls_test_buffer_init(peer1->output); |
| 473 | if (0 != (ret = mbedtls_test_buffer_setup(peer1->output, bufsize))) { |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 474 | goto exit; |
| 475 | } |
| 476 | |
Piotr Nowicki | d796e19 | 2020-01-28 12:09:47 +0100 | [diff] [blame] | 477 | peer2->output = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 478 | (mbedtls_test_buffer *) mbedtls_calloc(1, sizeof(mbedtls_test_buffer)); |
| 479 | if (peer2->output == NULL) { |
Piotr Nowicki | d796e19 | 2020-01-28 12:09:47 +0100 | [diff] [blame] | 480 | ret = MBEDTLS_ERR_SSL_ALLOC_FAILED; |
| 481 | goto exit; |
| 482 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 483 | mbedtls_test_buffer_init(peer2->output); |
| 484 | if (0 != (ret = mbedtls_test_buffer_setup(peer2->output, bufsize))) { |
Piotr Nowicki | d796e19 | 2020-01-28 12:09:47 +0100 | [diff] [blame] | 485 | goto exit; |
| 486 | } |
| 487 | |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 488 | peer1->peer = peer2; |
| 489 | peer2->peer = peer1; |
Piotr Nowicki | d796e19 | 2020-01-28 12:09:47 +0100 | [diff] [blame] | 490 | peer1->input = peer2->output; |
| 491 | peer2->input = peer1->output; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 492 | |
| 493 | peer1->status = peer2->status = MBEDTLS_MOCK_SOCKET_CONNECTED; |
| 494 | ret = 0; |
| 495 | |
| 496 | exit: |
| 497 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 498 | if (ret != 0) { |
| 499 | mbedtls_mock_socket_close(peer1); |
| 500 | mbedtls_mock_socket_close(peer2); |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | return ret; |
| 504 | } |
| 505 | |
| 506 | /* |
| 507 | * Callbacks for simulating blocking I/O over connection-oriented transport. |
| 508 | */ |
| 509 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 510 | 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] | 511 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 512 | mbedtls_mock_socket *socket = (mbedtls_mock_socket *) ctx; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 513 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 514 | if (socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED) { |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 515 | return -1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 516 | } |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 517 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 518 | return mbedtls_test_buffer_put(socket->output, buf, len); |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 519 | } |
| 520 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 521 | 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] | 522 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 523 | mbedtls_mock_socket *socket = (mbedtls_mock_socket *) ctx; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 524 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 525 | if (socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED) { |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 526 | return -1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 527 | } |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 528 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 529 | return mbedtls_test_buffer_get(socket->input, buf, len); |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | /* |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 533 | * Callbacks for simulating non-blocking I/O over connection-oriented transport. |
| 534 | */ |
| 535 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 536 | 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] | 537 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 538 | mbedtls_mock_socket *socket = (mbedtls_mock_socket *) ctx; |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 539 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 540 | if (socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED) { |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 541 | return -1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 542 | } |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 543 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 544 | if (socket->output->capacity == socket->output->content_length) { |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 545 | return MBEDTLS_ERR_SSL_WANT_WRITE; |
| 546 | } |
| 547 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 548 | return mbedtls_test_buffer_put(socket->output, buf, len); |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 549 | } |
| 550 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 551 | 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] | 552 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 553 | mbedtls_mock_socket *socket = (mbedtls_mock_socket *) ctx; |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 554 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 555 | if (socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED) { |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 556 | return -1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 557 | } |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 558 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 559 | if (socket->input->content_length == 0) { |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 560 | return MBEDTLS_ERR_SSL_WANT_READ; |
| 561 | } |
| 562 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 563 | return mbedtls_test_buffer_get(socket->input, buf, len); |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 564 | } |
| 565 | |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 566 | /* Errors used in the message socket mocks */ |
| 567 | |
| 568 | #define MBEDTLS_TEST_ERROR_CONTEXT_ERROR -55 |
| 569 | #define MBEDTLS_TEST_ERROR_SEND_FAILED -66 |
| 570 | #define MBEDTLS_TEST_ERROR_RECV_FAILED -77 |
| 571 | |
| 572 | /* |
| 573 | * Structure used as an addon, or a wrapper, around the mocked sockets. |
| 574 | * Contains an input queue, to which the other socket pushes metadata, |
| 575 | * and an output queue, to which this one pushes metadata. This context is |
| 576 | * considered as an owner of the input queue only, which is initialized and |
| 577 | * freed in the respective setup and free calls. |
| 578 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 579 | typedef struct mbedtls_test_message_socket_context { |
| 580 | mbedtls_test_message_queue *queue_input; |
| 581 | mbedtls_test_message_queue *queue_output; |
| 582 | mbedtls_mock_socket *socket; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 583 | } mbedtls_test_message_socket_context; |
| 584 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 585 | void mbedtls_message_socket_init(mbedtls_test_message_socket_context *ctx) |
Andrzej Kurek | 45916ba | 2020-03-05 14:46:22 -0500 | [diff] [blame] | 586 | { |
| 587 | ctx->queue_input = NULL; |
| 588 | ctx->queue_output = NULL; |
| 589 | ctx->socket = NULL; |
| 590 | } |
| 591 | |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 592 | /* |
Tom Cosgrove | 49f99bc | 2022-12-04 16:44:21 +0000 | [diff] [blame] | 593 | * Setup a given message socket context including initialization of |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 594 | * input/output queues to a chosen capacity of messages. Also set the |
| 595 | * corresponding mock socket. |
| 596 | * |
| 597 | * \retval 0, if everything succeeds. |
| 598 | * \retval MBEDTLS_ERR_SSL_ALLOC_FAILED, if allocation of a message |
| 599 | * queue failed. |
| 600 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 601 | int mbedtls_message_socket_setup(mbedtls_test_message_queue *queue_input, |
| 602 | mbedtls_test_message_queue *queue_output, |
| 603 | size_t queue_capacity, |
| 604 | mbedtls_mock_socket *socket, |
| 605 | mbedtls_test_message_socket_context *ctx) |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 606 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 607 | int ret = mbedtls_test_message_queue_setup(queue_input, queue_capacity); |
| 608 | if (ret != 0) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 609 | return ret; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 610 | } |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 611 | ctx->queue_input = queue_input; |
| 612 | ctx->queue_output = queue_output; |
| 613 | ctx->socket = socket; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 614 | mbedtls_mock_socket_init(socket); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 615 | |
| 616 | return 0; |
| 617 | } |
| 618 | |
| 619 | /* |
| 620 | * Close a given message socket context, along with the socket itself. Free the |
| 621 | * memory allocated by the input queue. |
| 622 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 623 | void mbedtls_message_socket_close(mbedtls_test_message_socket_context *ctx) |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 624 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 625 | if (ctx == NULL) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 626 | return; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 627 | } |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 628 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 629 | mbedtls_test_message_queue_free(ctx->queue_input); |
| 630 | mbedtls_mock_socket_close(ctx->socket); |
| 631 | memset(ctx, 0, sizeof(*ctx)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | /* |
| 635 | * Send one message through a given message socket context. |
| 636 | * |
| 637 | * \retval \p len, if everything succeeds. |
| 638 | * \retval MBEDTLS_TEST_ERROR_CONTEXT_ERROR, if any of the needed context |
| 639 | * elements or the context itself is null. |
| 640 | * \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] | 641 | * \retval MBEDTLS_ERR_SSL_WANT_WRITE, if the output queue is full. |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 642 | * |
| 643 | * This function will also return any error from |
| 644 | * mbedtls_test_message_queue_push_info. |
| 645 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 646 | 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] | 647 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 648 | mbedtls_test_message_queue *queue; |
| 649 | mbedtls_mock_socket *socket; |
| 650 | mbedtls_test_message_socket_context *context = (mbedtls_test_message_socket_context *) ctx; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 651 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 652 | if (context == NULL || context->socket == NULL |
| 653 | || context->queue_output == NULL) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 654 | return MBEDTLS_TEST_ERROR_CONTEXT_ERROR; |
| 655 | } |
| 656 | |
| 657 | queue = context->queue_output; |
| 658 | socket = context->socket; |
| 659 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 660 | if (queue->num >= queue->capacity) { |
Andrzej Kurek | f46b912 | 2020-02-07 08:19:00 -0500 | [diff] [blame] | 661 | return MBEDTLS_ERR_SSL_WANT_WRITE; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 662 | } |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 663 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 664 | if (mbedtls_mock_tcp_send_b(socket, buf, len) != (int) len) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 665 | return MBEDTLS_TEST_ERROR_SEND_FAILED; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 666 | } |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 667 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 668 | return mbedtls_test_message_queue_push_info(queue, len); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | /* |
| 672 | * Receive one message from a given message socket context and return message |
| 673 | * length or an error. |
| 674 | * |
| 675 | * \retval message length, if everything succeeds. |
| 676 | * \retval MBEDTLS_TEST_ERROR_CONTEXT_ERROR, if any of the needed context |
| 677 | * elements or the context itself is null. |
| 678 | * \retval MBEDTLS_TEST_ERROR_RECV_FAILED if mbedtls_mock_tcp_recv_b failed. |
| 679 | * |
| 680 | * This function will also return any error other than |
| 681 | * MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED from mbedtls_test_message_queue_peek_info. |
| 682 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 683 | 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] | 684 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 685 | mbedtls_test_message_queue *queue; |
| 686 | mbedtls_mock_socket *socket; |
| 687 | mbedtls_test_message_socket_context *context = (mbedtls_test_message_socket_context *) ctx; |
Gilles Peskine | 19e841e | 2020-03-09 20:43:51 +0100 | [diff] [blame] | 688 | size_t drop_len = 0; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 689 | size_t msg_len; |
| 690 | int ret; |
| 691 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 692 | if (context == NULL || context->socket == NULL |
| 693 | || context->queue_input == NULL) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 694 | return MBEDTLS_TEST_ERROR_CONTEXT_ERROR; |
| 695 | } |
| 696 | |
| 697 | queue = context->queue_input; |
| 698 | socket = context->socket; |
| 699 | |
| 700 | /* Peek first, so that in case of a socket error the data remains in |
| 701 | * the queue. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 702 | ret = mbedtls_test_message_queue_peek_info(queue, buf_len, &msg_len); |
| 703 | if (ret == MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 704 | /* Calculate how much to drop */ |
| 705 | drop_len = msg_len - buf_len; |
| 706 | |
| 707 | /* Set the requested message len to be buffer length */ |
| 708 | msg_len = buf_len; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 709 | } else if (ret != 0) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 710 | return ret; |
| 711 | } |
| 712 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 713 | 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] | 714 | return MBEDTLS_TEST_ERROR_RECV_FAILED; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 715 | } |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 716 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 717 | if (ret == MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 718 | /* Drop the remaining part of the message */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 719 | if (mbedtls_mock_tcp_recv_b(socket, NULL, drop_len) != (int) drop_len) { |
| 720 | /* Inconsistent state - part of the message was read, |
| 721 | * and a part couldn't. Not much we can do here, but it should not |
| 722 | * happen in test environment, unless forced manually. */ |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 723 | } |
| 724 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 725 | mbedtls_test_message_queue_pop_info(queue, buf_len); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 726 | |
| 727 | return msg_len; |
| 728 | } |
| 729 | |
Andrzej Kurek | 9155e7f | 2022-10-18 09:36:19 -0400 | [diff] [blame] | 730 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) && \ |
Gilles Peskine | 33d03fe | 2023-02-01 18:37:49 +0100 | [diff] [blame^] | 731 | defined(MBEDTLS_CERTS_C) && \ |
Manuel Pégourié-Gonnard | d12402f | 2020-05-20 10:34:25 +0200 | [diff] [blame] | 732 | defined(MBEDTLS_ENTROPY_C) && \ |
| 733 | defined(MBEDTLS_CTR_DRBG_C) |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 734 | |
| 735 | /* |
| 736 | * Structure with endpoint's certificates for SSL communication tests. |
| 737 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 738 | typedef struct mbedtls_endpoint_certificate { |
| 739 | mbedtls_x509_crt *ca_cert; |
| 740 | mbedtls_x509_crt *cert; |
| 741 | mbedtls_pk_context *pkey; |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 742 | } mbedtls_endpoint_certificate; |
| 743 | |
| 744 | /* |
| 745 | * Endpoint structure for SSL communication tests. |
| 746 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 747 | typedef struct mbedtls_endpoint { |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 748 | const char *name; |
| 749 | mbedtls_ssl_context ssl; |
| 750 | mbedtls_ssl_config conf; |
| 751 | mbedtls_ctr_drbg_context ctr_drbg; |
| 752 | mbedtls_entropy_context entropy; |
| 753 | mbedtls_mock_socket socket; |
| 754 | mbedtls_endpoint_certificate cert; |
| 755 | } mbedtls_endpoint; |
| 756 | |
| 757 | /* |
Andrzej Kurek | 0d2982b | 2022-10-18 07:55:46 -0400 | [diff] [blame] | 758 | * Deinitializes certificates from endpoint represented by \p ep. |
| 759 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 760 | void mbedtls_endpoint_certificate_free(mbedtls_endpoint *ep) |
Andrzej Kurek | 0d2982b | 2022-10-18 07:55:46 -0400 | [diff] [blame] | 761 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 762 | mbedtls_endpoint_certificate *cert = &(ep->cert); |
| 763 | if (cert != NULL) { |
| 764 | if (cert->ca_cert != NULL) { |
| 765 | mbedtls_x509_crt_free(cert->ca_cert); |
| 766 | mbedtls_free(cert->ca_cert); |
Andrzej Kurek | 0d2982b | 2022-10-18 07:55:46 -0400 | [diff] [blame] | 767 | cert->ca_cert = NULL; |
| 768 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 769 | if (cert->cert != NULL) { |
| 770 | mbedtls_x509_crt_free(cert->cert); |
| 771 | mbedtls_free(cert->cert); |
Andrzej Kurek | 0d2982b | 2022-10-18 07:55:46 -0400 | [diff] [blame] | 772 | cert->cert = NULL; |
| 773 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 774 | if (cert->pkey != NULL) { |
Andrzej Kurek | 0d2982b | 2022-10-18 07:55:46 -0400 | [diff] [blame] | 775 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 776 | if (mbedtls_pk_get_type(cert->pkey) == MBEDTLS_PK_OPAQUE) { |
Andrzej Kurek | 0d2982b | 2022-10-18 07:55:46 -0400 | [diff] [blame] | 777 | mbedtls_svc_key_id_t *key_slot = cert->pkey->pk_ctx; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 778 | psa_destroy_key(*key_slot); |
Andrzej Kurek | 0d2982b | 2022-10-18 07:55:46 -0400 | [diff] [blame] | 779 | } |
| 780 | #endif |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 781 | mbedtls_pk_free(cert->pkey); |
| 782 | mbedtls_free(cert->pkey); |
Andrzej Kurek | 0d2982b | 2022-10-18 07:55:46 -0400 | [diff] [blame] | 783 | cert->pkey = NULL; |
| 784 | } |
| 785 | } |
| 786 | } |
| 787 | |
| 788 | /* |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 789 | * Initializes \p ep_cert structure and assigns it to endpoint |
| 790 | * represented by \p ep. |
| 791 | * |
| 792 | * \retval 0 on success, otherwise error code. |
| 793 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 794 | int mbedtls_endpoint_certificate_init(mbedtls_endpoint *ep, int pk_alg) |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 795 | { |
| 796 | int i = 0; |
| 797 | int ret = -1; |
Andrzej Kurek | 0d2982b | 2022-10-18 07:55:46 -0400 | [diff] [blame] | 798 | mbedtls_endpoint_certificate *cert = NULL; |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 799 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 800 | if (ep == NULL) { |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 801 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 802 | } |
| 803 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 804 | cert = &(ep->cert); |
| 805 | ASSERT_ALLOC(cert->ca_cert, 1); |
| 806 | ASSERT_ALLOC(cert->cert, 1); |
| 807 | ASSERT_ALLOC(cert->pkey, 1); |
Andrzej Kurek | 0d2982b | 2022-10-18 07:55:46 -0400 | [diff] [blame] | 808 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 809 | mbedtls_x509_crt_init(cert->ca_cert); |
| 810 | mbedtls_x509_crt_init(cert->cert); |
| 811 | mbedtls_pk_init(cert->pkey); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 812 | |
| 813 | /* Load the trusted CA */ |
| 814 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 815 | for (i = 0; mbedtls_test_cas_der[i] != NULL; i++) { |
| 816 | ret = mbedtls_x509_crt_parse_der(cert->ca_cert, |
| 817 | (const unsigned char *) mbedtls_test_cas_der[i], |
| 818 | mbedtls_test_cas_der_len[i]); |
| 819 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 820 | } |
| 821 | |
| 822 | /* Load own certificate and private key */ |
| 823 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 824 | if (ep->conf.endpoint == MBEDTLS_SSL_IS_SERVER) { |
| 825 | if (pk_alg == MBEDTLS_PK_RSA) { |
| 826 | ret = mbedtls_x509_crt_parse(cert->cert, |
| 827 | (const unsigned char *) mbedtls_test_srv_crt_rsa_sha256_der, |
| 828 | mbedtls_test_srv_crt_rsa_sha256_der_len); |
| 829 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 830 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 831 | ret = mbedtls_pk_parse_key(cert->pkey, |
| 832 | (const unsigned char *) mbedtls_test_srv_key_rsa_der, |
| 833 | mbedtls_test_srv_key_rsa_der_len, NULL, 0); |
| 834 | TEST_ASSERT(ret == 0); |
| 835 | } else { |
| 836 | ret = mbedtls_x509_crt_parse(cert->cert, |
| 837 | (const unsigned char *) mbedtls_test_srv_crt_ec_der, |
| 838 | mbedtls_test_srv_crt_ec_der_len); |
| 839 | TEST_ASSERT(ret == 0); |
| 840 | |
| 841 | ret = mbedtls_pk_parse_key(cert->pkey, |
| 842 | (const unsigned char *) mbedtls_test_srv_key_ec_der, |
| 843 | mbedtls_test_srv_key_ec_der_len, NULL, 0); |
| 844 | TEST_ASSERT(ret == 0); |
Andrzej Kurek | b298074 | 2020-02-02 19:25:26 -0500 | [diff] [blame] | 845 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 846 | } else { |
| 847 | if (pk_alg == MBEDTLS_PK_RSA) { |
| 848 | ret = mbedtls_x509_crt_parse(cert->cert, |
| 849 | (const unsigned char *) mbedtls_test_cli_crt_rsa_der, |
| 850 | mbedtls_test_cli_crt_rsa_der_len); |
| 851 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 852 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 853 | ret = mbedtls_pk_parse_key(cert->pkey, |
| 854 | (const unsigned char *) mbedtls_test_cli_key_rsa_der, |
| 855 | mbedtls_test_cli_key_rsa_der_len, NULL, 0); |
| 856 | TEST_ASSERT(ret == 0); |
| 857 | } else { |
| 858 | ret = mbedtls_x509_crt_parse(cert->cert, |
| 859 | (const unsigned char *) mbedtls_test_cli_crt_ec_der, |
| 860 | mbedtls_test_cli_crt_ec_len); |
| 861 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 862 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 863 | ret = mbedtls_pk_parse_key(cert->pkey, |
| 864 | (const unsigned char *) mbedtls_test_cli_key_ec_der, |
| 865 | mbedtls_test_cli_key_ec_der_len, NULL, 0); |
| 866 | TEST_ASSERT(ret == 0); |
Andrzej Kurek | b298074 | 2020-02-02 19:25:26 -0500 | [diff] [blame] | 867 | } |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 868 | } |
| 869 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 870 | mbedtls_ssl_conf_ca_chain(&(ep->conf), cert->ca_cert, NULL); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 871 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 872 | ret = mbedtls_ssl_conf_own_cert(&(ep->conf), cert->cert, |
| 873 | cert->pkey); |
| 874 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 875 | |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 876 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 877 | if (ret != 0) { |
| 878 | mbedtls_endpoint_certificate_free(ep); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 879 | } |
| 880 | |
| 881 | return ret; |
| 882 | } |
| 883 | |
| 884 | /* |
| 885 | * Initializes \p ep structure. It is important to call `mbedtls_endpoint_free()` |
| 886 | * after calling this function even if it fails. |
| 887 | * |
| 888 | * \p endpoint_type must be set as MBEDTLS_SSL_IS_SERVER or |
| 889 | * MBEDTLS_SSL_IS_CLIENT. |
Andrzej Kurek | 15daf50 | 2020-02-12 09:17:52 -0500 | [diff] [blame] | 890 | * \p pk_alg the algorithm to use, currently only MBEDTLS_PK_RSA and |
| 891 | * MBEDTLS_PK_ECDSA are supported. |
| 892 | * \p dtls_context - in case of DTLS - this is the context handling metadata. |
| 893 | * \p input_queue - used only in case of DTLS. |
| 894 | * \p output_queue - used only in case of DTLS. |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 895 | * |
| 896 | * \retval 0 on success, otherwise error code. |
| 897 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 898 | int mbedtls_endpoint_init(mbedtls_endpoint *ep, int endpoint_type, int pk_alg, |
| 899 | mbedtls_test_message_socket_context *dtls_context, |
| 900 | mbedtls_test_message_queue *input_queue, |
| 901 | mbedtls_test_message_queue *output_queue, |
| 902 | const mbedtls_ecp_group_id *curves) |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 903 | { |
| 904 | int ret = -1; |
| 905 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 906 | if (dtls_context != NULL && (input_queue == NULL || output_queue == NULL)) { |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 907 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
Andrzej Kurek | 15daf50 | 2020-02-12 09:17:52 -0500 | [diff] [blame] | 908 | } |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 909 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 910 | if (ep == NULL) { |
| 911 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 912 | } |
| 913 | |
| 914 | memset(ep, 0, sizeof(*ep)); |
| 915 | |
| 916 | ep->name = (endpoint_type == MBEDTLS_SSL_IS_SERVER) ? "Server" : "Client"; |
| 917 | |
| 918 | mbedtls_ssl_init(&(ep->ssl)); |
| 919 | mbedtls_ssl_config_init(&(ep->conf)); |
| 920 | mbedtls_ctr_drbg_init(&(ep->ctr_drbg)); |
| 921 | mbedtls_ssl_conf_rng(&(ep->conf), |
| 922 | mbedtls_ctr_drbg_random, |
| 923 | &(ep->ctr_drbg)); |
| 924 | mbedtls_entropy_init(&(ep->entropy)); |
| 925 | if (dtls_context != NULL) { |
| 926 | TEST_ASSERT(mbedtls_message_socket_setup(input_queue, output_queue, |
| 927 | 100, &(ep->socket), |
| 928 | dtls_context) == 0); |
| 929 | } else { |
| 930 | mbedtls_mock_socket_init(&(ep->socket)); |
| 931 | } |
| 932 | |
| 933 | ret = mbedtls_ctr_drbg_seed(&(ep->ctr_drbg), mbedtls_entropy_func, |
| 934 | &(ep->entropy), (const unsigned char *) (ep->name), |
| 935 | strlen(ep->name)); |
| 936 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 937 | |
| 938 | /* Non-blocking callbacks without timeout */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 939 | if (dtls_context != NULL) { |
| 940 | mbedtls_ssl_set_bio(&(ep->ssl), dtls_context, |
| 941 | mbedtls_mock_tcp_send_msg, |
| 942 | mbedtls_mock_tcp_recv_msg, |
| 943 | NULL); |
| 944 | } else { |
| 945 | mbedtls_ssl_set_bio(&(ep->ssl), &(ep->socket), |
| 946 | mbedtls_mock_tcp_send_nb, |
| 947 | mbedtls_mock_tcp_recv_nb, |
| 948 | NULL); |
Andrzej Kurek | 15daf50 | 2020-02-12 09:17:52 -0500 | [diff] [blame] | 949 | } |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 950 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 951 | ret = mbedtls_ssl_config_defaults(&(ep->conf), endpoint_type, |
| 952 | (dtls_context != NULL) ? |
| 953 | MBEDTLS_SSL_TRANSPORT_DATAGRAM : |
| 954 | MBEDTLS_SSL_TRANSPORT_STREAM, |
| 955 | MBEDTLS_SSL_PRESET_DEFAULT); |
| 956 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 957 | |
Andrzej Kurek | 96bf3d1 | 2022-04-15 07:35:16 -0400 | [diff] [blame] | 958 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 959 | if (curves != NULL) { |
| 960 | mbedtls_ssl_conf_curves(&(ep->conf), curves); |
| 961 | } |
Andrzej Kurek | 96bf3d1 | 2022-04-15 07:35:16 -0400 | [diff] [blame] | 962 | #else |
| 963 | (void) curves; |
| 964 | #endif |
Andrzej Kurek | 535cd17 | 2022-03-08 06:50:12 -0500 | [diff] [blame] | 965 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 966 | ret = mbedtls_ssl_setup(&(ep->ssl), &(ep->conf)); |
| 967 | TEST_ASSERT(ret == 0); |
Andrzej Kurek | 15daf50 | 2020-02-12 09:17:52 -0500 | [diff] [blame] | 968 | |
| 969 | #if defined(MBEDTLS_SSL_PROTO_DTLS) && defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 970 | if (endpoint_type == MBEDTLS_SSL_IS_SERVER && dtls_context != NULL) { |
| 971 | mbedtls_ssl_conf_dtls_cookies(&(ep->conf), NULL, NULL, NULL); |
| 972 | } |
Andrzej Kurek | 15daf50 | 2020-02-12 09:17:52 -0500 | [diff] [blame] | 973 | #endif |
| 974 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 975 | ret = mbedtls_endpoint_certificate_init(ep, pk_alg); |
| 976 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 977 | |
| 978 | exit: |
| 979 | return ret; |
| 980 | } |
| 981 | |
| 982 | /* |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 983 | * Deinitializes endpoint represented by \p ep. |
| 984 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 985 | void mbedtls_endpoint_free(mbedtls_endpoint *ep, |
| 986 | mbedtls_test_message_socket_context *context) |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 987 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 988 | mbedtls_endpoint_certificate_free(ep); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 989 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 990 | mbedtls_ssl_free(&(ep->ssl)); |
| 991 | mbedtls_ssl_config_free(&(ep->conf)); |
| 992 | mbedtls_ctr_drbg_free(&(ep->ctr_drbg)); |
| 993 | mbedtls_entropy_free(&(ep->entropy)); |
Andrzej Kurek | 15daf50 | 2020-02-12 09:17:52 -0500 | [diff] [blame] | 994 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 995 | if (context != NULL) { |
| 996 | mbedtls_message_socket_close(context); |
| 997 | } else { |
| 998 | mbedtls_mock_socket_close(&(ep->socket)); |
Andrzej Kurek | 15daf50 | 2020-02-12 09:17:52 -0500 | [diff] [blame] | 999 | } |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 1000 | } |
| 1001 | |
| 1002 | /* |
| 1003 | * This function moves ssl handshake from \p ssl to prescribed \p state. |
| 1004 | * /p second_ssl is used as second endpoint and their sockets have to be |
| 1005 | * connected before calling this function. |
| 1006 | * |
| 1007 | * \retval 0 on success, otherwise error code. |
| 1008 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1009 | int mbedtls_move_handshake_to_state(mbedtls_ssl_context *ssl, |
| 1010 | mbedtls_ssl_context *second_ssl, |
| 1011 | int state) |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 1012 | { |
| 1013 | enum { BUFFSIZE = 1024 }; |
| 1014 | int max_steps = 1000; |
| 1015 | int ret = 0; |
| 1016 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1017 | if (ssl == NULL || second_ssl == NULL) { |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 1018 | return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; |
| 1019 | } |
| 1020 | |
| 1021 | /* Perform communication via connected sockets */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1022 | while ((ssl->state != state) && (--max_steps >= 0)) { |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 1023 | /* If /p second_ssl ends the handshake procedure before /p ssl then |
| 1024 | * there is no need to call the next step */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1025 | if (second_ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) { |
| 1026 | ret = mbedtls_ssl_handshake_step(second_ssl); |
| 1027 | if (ret != 0 && ret != MBEDTLS_ERR_SSL_WANT_READ && |
| 1028 | ret != MBEDTLS_ERR_SSL_WANT_WRITE) { |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 1029 | return ret; |
| 1030 | } |
| 1031 | } |
| 1032 | |
| 1033 | /* We only care about the \p ssl state and returns, so we call it last, |
| 1034 | * to leave the iteration as soon as the state is as expected. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1035 | ret = mbedtls_ssl_handshake_step(ssl); |
| 1036 | if (ret != 0 && ret != MBEDTLS_ERR_SSL_WANT_READ && |
| 1037 | ret != MBEDTLS_ERR_SSL_WANT_WRITE) { |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 1038 | return ret; |
| 1039 | } |
| 1040 | } |
| 1041 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1042 | return (max_steps >= 0) ? ret : -1; |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 1043 | } |
| 1044 | |
Gilles Peskine | 33d03fe | 2023-02-01 18:37:49 +0100 | [diff] [blame^] | 1045 | #endif \ |
| 1046 | /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED && MBEDTLS_CERTS_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */ |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 1047 | |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 1048 | /* |
Piotr Nowicki | 438bf3b | 2020-03-10 12:59:10 +0100 | [diff] [blame] | 1049 | * Write application data. Increase write counter if necessary. |
Piotr Nowicki | c3fca5e | 2020-01-30 15:33:42 +0100 | [diff] [blame] | 1050 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1051 | int mbedtls_ssl_write_fragment(mbedtls_ssl_context *ssl, unsigned char *buf, |
| 1052 | int buf_len, int *written, |
| 1053 | const int expected_fragments) |
Piotr Nowicki | c3fca5e | 2020-01-30 15:33:42 +0100 | [diff] [blame] | 1054 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1055 | int ret = mbedtls_ssl_write(ssl, buf + *written, buf_len - *written); |
| 1056 | if (ret > 0) { |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1057 | *written += ret; |
Piotr Nowicki | c3fca5e | 2020-01-30 15:33:42 +0100 | [diff] [blame] | 1058 | } |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1059 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1060 | if (expected_fragments == 0) { |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1061 | /* Used for DTLS and the message size larger than MFL. In that case |
| 1062 | * the message can not be fragmented and the library should return |
| 1063 | * MBEDTLS_ERR_SSL_BAD_INPUT_DATA error. This error must be returned |
| 1064 | * to prevent a dead loop inside mbedtls_exchange_data(). */ |
| 1065 | return ret; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1066 | } else if (expected_fragments == 1) { |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1067 | /* Used for TLS/DTLS and the message size lower than MFL */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1068 | TEST_ASSERT(ret == buf_len || |
| 1069 | ret == MBEDTLS_ERR_SSL_WANT_READ || |
| 1070 | ret == MBEDTLS_ERR_SSL_WANT_WRITE); |
| 1071 | } else { |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1072 | /* Used for TLS and the message size larger than MFL */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1073 | TEST_ASSERT(expected_fragments > 1); |
| 1074 | TEST_ASSERT((ret >= 0 && ret <= buf_len) || |
| 1075 | ret == MBEDTLS_ERR_SSL_WANT_READ || |
| 1076 | ret == MBEDTLS_ERR_SSL_WANT_WRITE); |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1077 | } |
| 1078 | |
| 1079 | return 0; |
| 1080 | |
| 1081 | exit: |
| 1082 | /* Some of the tests failed */ |
| 1083 | return -1; |
Piotr Nowicki | c3fca5e | 2020-01-30 15:33:42 +0100 | [diff] [blame] | 1084 | } |
| 1085 | |
| 1086 | /* |
Piotr Nowicki | 438bf3b | 2020-03-10 12:59:10 +0100 | [diff] [blame] | 1087 | * Read application data and increase read counter and fragments counter if necessary. |
Piotr Nowicki | c3fca5e | 2020-01-30 15:33:42 +0100 | [diff] [blame] | 1088 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1089 | int mbedtls_ssl_read_fragment(mbedtls_ssl_context *ssl, unsigned char *buf, |
| 1090 | int buf_len, int *read, |
| 1091 | int *fragments, const int expected_fragments) |
Piotr Nowicki | c3fca5e | 2020-01-30 15:33:42 +0100 | [diff] [blame] | 1092 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1093 | int ret = mbedtls_ssl_read(ssl, buf + *read, buf_len - *read); |
| 1094 | if (ret > 0) { |
| 1095 | (*fragments)++; |
Piotr Nowicki | c3fca5e | 2020-01-30 15:33:42 +0100 | [diff] [blame] | 1096 | *read += ret; |
| 1097 | } |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1098 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1099 | if (expected_fragments == 0) { |
| 1100 | TEST_ASSERT(ret == 0); |
| 1101 | } else if (expected_fragments == 1) { |
| 1102 | TEST_ASSERT(ret == buf_len || |
| 1103 | ret == MBEDTLS_ERR_SSL_WANT_READ || |
| 1104 | ret == MBEDTLS_ERR_SSL_WANT_WRITE); |
| 1105 | } else { |
| 1106 | TEST_ASSERT(expected_fragments > 1); |
| 1107 | TEST_ASSERT((ret >= 0 && ret <= buf_len) || |
| 1108 | ret == MBEDTLS_ERR_SSL_WANT_READ || |
| 1109 | ret == MBEDTLS_ERR_SSL_WANT_WRITE); |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1110 | } |
| 1111 | |
| 1112 | return 0; |
| 1113 | |
| 1114 | exit: |
| 1115 | /* Some of the tests failed */ |
| 1116 | return -1; |
Piotr Nowicki | c3fca5e | 2020-01-30 15:33:42 +0100 | [diff] [blame] | 1117 | } |
| 1118 | |
| 1119 | /* |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1120 | * Helper function setting up inverse record transformations |
| 1121 | * using given cipher, hash, EtM mode, authentication tag length, |
| 1122 | * and version. |
| 1123 | */ |
| 1124 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1125 | #define CHK(x) \ |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1126 | do \ |
| 1127 | { \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1128 | if (!(x)) \ |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 1129 | { \ |
Hanno Becker | a5780f1 | 2019-04-05 09:55:37 +0100 | [diff] [blame] | 1130 | ret = -1; \ |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 1131 | goto cleanup; \ |
| 1132 | } \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1133 | } while (0) |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1134 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1135 | void set_ciphersuite(mbedtls_ssl_config *conf, const char *cipher, |
| 1136 | int *forced_ciphersuite) |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 1137 | { |
| 1138 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1139 | forced_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id(cipher); |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 1140 | forced_ciphersuite[1] = 0; |
| 1141 | |
| 1142 | ciphersuite_info = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1143 | mbedtls_ssl_ciphersuite_from_id(forced_ciphersuite[0]); |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 1144 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1145 | TEST_ASSERT(ciphersuite_info != NULL); |
| 1146 | TEST_ASSERT(ciphersuite_info->min_minor_ver <= conf->max_minor_ver); |
| 1147 | TEST_ASSERT(ciphersuite_info->max_minor_ver >= conf->min_minor_ver); |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 1148 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1149 | if (conf->max_minor_ver > ciphersuite_info->max_minor_ver) { |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 1150 | conf->max_minor_ver = ciphersuite_info->max_minor_ver; |
| 1151 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1152 | if (conf->min_minor_ver < ciphersuite_info->min_minor_ver) { |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 1153 | conf->min_minor_ver = ciphersuite_info->min_minor_ver; |
| 1154 | } |
| 1155 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1156 | mbedtls_ssl_conf_ciphersuites(conf, forced_ciphersuite); |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 1157 | |
| 1158 | exit: |
| 1159 | return; |
| 1160 | } |
| 1161 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1162 | int psk_dummy_callback(void *p_info, mbedtls_ssl_context *ssl, |
| 1163 | const unsigned char *name, size_t name_len) |
Andrzej Kurek | cc5169c | 2020-02-04 09:04:56 -0500 | [diff] [blame] | 1164 | { |
| 1165 | (void) p_info; |
| 1166 | (void) ssl; |
| 1167 | (void) name; |
| 1168 | (void) name_len; |
| 1169 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1170 | return 0; |
Andrzej Kurek | cc5169c | 2020-02-04 09:04:56 -0500 | [diff] [blame] | 1171 | } |
| 1172 | |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 1173 | #if MBEDTLS_SSL_CID_OUT_LEN_MAX > MBEDTLS_SSL_CID_IN_LEN_MAX |
| 1174 | #define SSL_CID_LEN_MIN MBEDTLS_SSL_CID_IN_LEN_MAX |
| 1175 | #else |
| 1176 | #define SSL_CID_LEN_MIN MBEDTLS_SSL_CID_OUT_LEN_MAX |
| 1177 | #endif |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1178 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1179 | static int build_transforms(mbedtls_ssl_transform *t_in, |
| 1180 | mbedtls_ssl_transform *t_out, |
| 1181 | int cipher_type, int hash_id, |
| 1182 | int etm, int tag_mode, int ver, |
| 1183 | size_t cid0_len, |
| 1184 | size_t cid1_len) |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1185 | { |
| 1186 | mbedtls_cipher_info_t const *cipher_info; |
Hanno Becker | a5780f1 | 2019-04-05 09:55:37 +0100 | [diff] [blame] | 1187 | int ret = 0; |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1188 | |
| 1189 | size_t keylen, maclen, ivlen; |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 1190 | unsigned char *key0 = NULL, *key1 = NULL; |
Paul Elliott | 6f1eda7 | 2020-06-11 20:22:00 +0100 | [diff] [blame] | 1191 | unsigned char *md0 = NULL, *md1 = NULL; |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1192 | unsigned char iv_enc[16], iv_dec[16]; |
| 1193 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1194 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1195 | unsigned char cid0[SSL_CID_LEN_MIN]; |
| 1196 | unsigned char cid1[SSL_CID_LEN_MIN]; |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 1197 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1198 | mbedtls_test_rnd_std_rand(NULL, cid0, sizeof(cid0)); |
| 1199 | mbedtls_test_rnd_std_rand(NULL, cid1, sizeof(cid1)); |
Hanno Becker | 43c24b8 | 2019-05-01 09:45:57 +0100 | [diff] [blame] | 1200 | #else |
| 1201 | ((void) cid0_len); |
| 1202 | ((void) cid1_len); |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1203 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 1204 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1205 | maclen = 0; |
| 1206 | |
| 1207 | /* Pick cipher */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1208 | cipher_info = mbedtls_cipher_info_from_type(cipher_type); |
| 1209 | CHK(cipher_info != NULL); |
| 1210 | CHK(cipher_info->iv_size <= 16); |
| 1211 | CHK(cipher_info->key_bitlen % 8 == 0); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1212 | |
| 1213 | /* Pick keys */ |
| 1214 | keylen = cipher_info->key_bitlen / 8; |
Hanno Becker | 78d1f70 | 2019-04-05 09:56:10 +0100 | [diff] [blame] | 1215 | /* Allocate `keylen + 1` bytes to ensure that we get |
| 1216 | * a non-NULL pointers from `mbedtls_calloc` even if |
| 1217 | * `keylen == 0` in the case of the NULL cipher. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1218 | CHK((key0 = mbedtls_calloc(1, keylen + 1)) != NULL); |
| 1219 | CHK((key1 = mbedtls_calloc(1, keylen + 1)) != NULL); |
| 1220 | memset(key0, 0x1, keylen); |
| 1221 | memset(key1, 0x2, keylen); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1222 | |
| 1223 | /* Setup cipher contexts */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1224 | CHK(mbedtls_cipher_setup(&t_in->cipher_ctx_enc, cipher_info) == 0); |
| 1225 | CHK(mbedtls_cipher_setup(&t_in->cipher_ctx_dec, cipher_info) == 0); |
| 1226 | CHK(mbedtls_cipher_setup(&t_out->cipher_ctx_enc, cipher_info) == 0); |
| 1227 | CHK(mbedtls_cipher_setup(&t_out->cipher_ctx_dec, cipher_info) == 0); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1228 | |
| 1229 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1230 | if (cipher_info->mode == MBEDTLS_MODE_CBC) { |
| 1231 | CHK(mbedtls_cipher_set_padding_mode(&t_in->cipher_ctx_enc, |
| 1232 | MBEDTLS_PADDING_NONE) == 0); |
| 1233 | CHK(mbedtls_cipher_set_padding_mode(&t_in->cipher_ctx_dec, |
| 1234 | MBEDTLS_PADDING_NONE) == 0); |
| 1235 | CHK(mbedtls_cipher_set_padding_mode(&t_out->cipher_ctx_enc, |
| 1236 | MBEDTLS_PADDING_NONE) == 0); |
| 1237 | CHK(mbedtls_cipher_set_padding_mode(&t_out->cipher_ctx_dec, |
| 1238 | MBEDTLS_PADDING_NONE) == 0); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1239 | } |
| 1240 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
| 1241 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1242 | CHK(mbedtls_cipher_setkey(&t_in->cipher_ctx_enc, key0, |
| 1243 | keylen << 3, MBEDTLS_ENCRYPT) == 0); |
| 1244 | CHK(mbedtls_cipher_setkey(&t_in->cipher_ctx_dec, key1, |
| 1245 | keylen << 3, MBEDTLS_DECRYPT) == 0); |
| 1246 | CHK(mbedtls_cipher_setkey(&t_out->cipher_ctx_enc, key1, |
| 1247 | keylen << 3, MBEDTLS_ENCRYPT) == 0); |
| 1248 | CHK(mbedtls_cipher_setkey(&t_out->cipher_ctx_dec, key0, |
| 1249 | keylen << 3, MBEDTLS_DECRYPT) == 0); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1250 | |
| 1251 | /* Setup MAC contexts */ |
| 1252 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1253 | if (cipher_info->mode == MBEDTLS_MODE_CBC || |
| 1254 | cipher_info->mode == MBEDTLS_MODE_STREAM) { |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1255 | mbedtls_md_info_t const *md_info; |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1256 | |
| 1257 | /* Pick hash */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1258 | md_info = mbedtls_md_info_from_type(hash_id); |
| 1259 | CHK(md_info != NULL); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1260 | |
| 1261 | /* Pick hash keys */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1262 | maclen = mbedtls_md_get_size(md_info); |
| 1263 | CHK((md0 = mbedtls_calloc(1, maclen)) != NULL); |
| 1264 | CHK((md1 = mbedtls_calloc(1, maclen)) != NULL); |
| 1265 | memset(md0, 0x5, maclen); |
| 1266 | memset(md1, 0x6, maclen); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1267 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1268 | CHK(mbedtls_md_setup(&t_out->md_ctx_enc, md_info, 1) == 0); |
| 1269 | CHK(mbedtls_md_setup(&t_out->md_ctx_dec, md_info, 1) == 0); |
| 1270 | CHK(mbedtls_md_setup(&t_in->md_ctx_enc, md_info, 1) == 0); |
| 1271 | 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] | 1272 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1273 | if (ver > MBEDTLS_SSL_MINOR_VERSION_0) { |
| 1274 | CHK(mbedtls_md_hmac_starts(&t_in->md_ctx_enc, |
| 1275 | md0, maclen) == 0); |
| 1276 | CHK(mbedtls_md_hmac_starts(&t_in->md_ctx_dec, |
| 1277 | md1, maclen) == 0); |
| 1278 | CHK(mbedtls_md_hmac_starts(&t_out->md_ctx_enc, |
| 1279 | md1, maclen) == 0); |
| 1280 | CHK(mbedtls_md_hmac_starts(&t_out->md_ctx_dec, |
| 1281 | md0, maclen) == 0); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1282 | } |
| 1283 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1284 | else { |
| 1285 | memcpy(&t_in->mac_enc, md0, maclen); |
| 1286 | memcpy(&t_in->mac_dec, md1, maclen); |
| 1287 | memcpy(&t_out->mac_enc, md1, maclen); |
| 1288 | memcpy(&t_out->mac_dec, md0, maclen); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1289 | } |
| 1290 | #endif |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1291 | } |
| 1292 | #else |
| 1293 | ((void) hash_id); |
| 1294 | #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ |
| 1295 | |
| 1296 | |
| 1297 | /* Pick IV's (regardless of whether they |
| 1298 | * are being used by the transform). */ |
| 1299 | ivlen = cipher_info->iv_size; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1300 | memset(iv_enc, 0x3, sizeof(iv_enc)); |
| 1301 | memset(iv_dec, 0x4, sizeof(iv_dec)); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1302 | |
| 1303 | /* |
| 1304 | * Setup transforms |
| 1305 | */ |
| 1306 | |
Jaeden Amero | 2de07f1 | 2019-06-05 13:32:08 +0100 | [diff] [blame] | 1307 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \ |
| 1308 | defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1309 | t_out->encrypt_then_mac = etm; |
| 1310 | t_in->encrypt_then_mac = etm; |
| 1311 | #else |
| 1312 | ((void) etm); |
| 1313 | #endif |
| 1314 | |
| 1315 | t_out->minor_ver = ver; |
| 1316 | t_in->minor_ver = ver; |
| 1317 | t_out->ivlen = ivlen; |
| 1318 | t_in->ivlen = ivlen; |
| 1319 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1320 | switch (cipher_info->mode) { |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1321 | case MBEDTLS_MODE_GCM: |
| 1322 | case MBEDTLS_MODE_CCM: |
Hanno Becker | e683287 | 2020-05-28 08:29:58 +0100 | [diff] [blame] | 1323 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1324 | if (ver == MBEDTLS_SSL_MINOR_VERSION_4) { |
Hanno Becker | e683287 | 2020-05-28 08:29:58 +0100 | [diff] [blame] | 1325 | t_out->fixed_ivlen = 12; |
| 1326 | t_in->fixed_ivlen = 12; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1327 | } else |
Hanno Becker | e683287 | 2020-05-28 08:29:58 +0100 | [diff] [blame] | 1328 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ |
| 1329 | { |
| 1330 | t_out->fixed_ivlen = 4; |
| 1331 | t_in->fixed_ivlen = 4; |
| 1332 | } |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1333 | t_out->maclen = 0; |
| 1334 | t_in->maclen = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1335 | switch (tag_mode) { |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1336 | case 0: /* Full tag */ |
| 1337 | t_out->taglen = 16; |
| 1338 | t_in->taglen = 16; |
| 1339 | break; |
| 1340 | case 1: /* Partial tag */ |
| 1341 | t_out->taglen = 8; |
| 1342 | t_in->taglen = 8; |
| 1343 | break; |
| 1344 | default: |
Paul Elliott | c7b5374 | 2021-02-03 13:18:33 +0000 | [diff] [blame] | 1345 | ret = 1; |
| 1346 | goto cleanup; |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1347 | } |
| 1348 | break; |
| 1349 | |
| 1350 | case MBEDTLS_MODE_CHACHAPOLY: |
| 1351 | t_out->fixed_ivlen = 12; |
| 1352 | t_in->fixed_ivlen = 12; |
| 1353 | t_out->maclen = 0; |
| 1354 | t_in->maclen = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1355 | switch (tag_mode) { |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1356 | case 0: /* Full tag */ |
| 1357 | t_out->taglen = 16; |
| 1358 | t_in->taglen = 16; |
| 1359 | break; |
| 1360 | case 1: /* Partial tag */ |
| 1361 | t_out->taglen = 8; |
| 1362 | t_in->taglen = 8; |
| 1363 | break; |
| 1364 | default: |
Paul Elliott | c7b5374 | 2021-02-03 13:18:33 +0000 | [diff] [blame] | 1365 | ret = 1; |
| 1366 | goto cleanup; |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1367 | } |
| 1368 | break; |
| 1369 | |
| 1370 | case MBEDTLS_MODE_STREAM: |
| 1371 | case MBEDTLS_MODE_CBC: |
| 1372 | t_out->fixed_ivlen = 0; /* redundant, must be 0 */ |
| 1373 | t_in->fixed_ivlen = 0; /* redundant, must be 0 */ |
| 1374 | t_out->taglen = 0; |
| 1375 | t_in->taglen = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1376 | switch (tag_mode) { |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1377 | case 0: /* Full tag */ |
| 1378 | t_out->maclen = maclen; |
| 1379 | t_in->maclen = maclen; |
| 1380 | break; |
| 1381 | case 1: /* Partial tag */ |
| 1382 | t_out->maclen = 10; |
| 1383 | t_in->maclen = 10; |
| 1384 | break; |
| 1385 | default: |
Paul Elliott | c7b5374 | 2021-02-03 13:18:33 +0000 | [diff] [blame] | 1386 | ret = 1; |
| 1387 | goto cleanup; |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1388 | } |
| 1389 | break; |
| 1390 | default: |
Paul Elliott | c7b5374 | 2021-02-03 13:18:33 +0000 | [diff] [blame] | 1391 | ret = 1; |
| 1392 | goto cleanup; |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1393 | break; |
| 1394 | } |
| 1395 | |
| 1396 | /* Setup IV's */ |
| 1397 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1398 | memcpy(&t_in->iv_dec, iv_dec, sizeof(iv_dec)); |
| 1399 | memcpy(&t_in->iv_enc, iv_enc, sizeof(iv_enc)); |
| 1400 | memcpy(&t_out->iv_dec, iv_enc, sizeof(iv_enc)); |
| 1401 | memcpy(&t_out->iv_enc, iv_dec, sizeof(iv_dec)); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1402 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1403 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 1404 | /* Add CID */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1405 | memcpy(&t_in->in_cid, cid0, cid0_len); |
| 1406 | memcpy(&t_in->out_cid, cid1, cid1_len); |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 1407 | t_in->in_cid_len = cid0_len; |
| 1408 | t_in->out_cid_len = cid1_len; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1409 | memcpy(&t_out->in_cid, cid1, cid1_len); |
| 1410 | memcpy(&t_out->out_cid, cid0, cid0_len); |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 1411 | t_out->in_cid_len = cid1_len; |
| 1412 | t_out->out_cid_len = cid0_len; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1413 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 1414 | |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 1415 | cleanup: |
| 1416 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1417 | mbedtls_free(key0); |
| 1418 | mbedtls_free(key1); |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 1419 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1420 | mbedtls_free(md0); |
| 1421 | mbedtls_free(md1); |
Paul Elliott | 6f1eda7 | 2020-06-11 20:22:00 +0100 | [diff] [blame] | 1422 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1423 | return ret; |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1424 | } |
| 1425 | |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1426 | /* |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 1427 | * Populate a session structure for serialization tests. |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1428 | * Choose dummy values, mostly non-0 to distinguish from the init default. |
| 1429 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1430 | static int ssl_populate_session(mbedtls_ssl_session *session, |
| 1431 | int ticket_len, |
| 1432 | const char *crt_file) |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1433 | { |
| 1434 | #if defined(MBEDTLS_HAVE_TIME) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1435 | session->start = mbedtls_time(NULL) - 42; |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1436 | #endif |
| 1437 | session->ciphersuite = 0xabcd; |
| 1438 | session->compression = 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1439 | session->id_len = sizeof(session->id); |
| 1440 | memset(session->id, 66, session->id_len); |
| 1441 | memset(session->master, 17, sizeof(session->master)); |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1442 | |
Gilles Peskine | 33d03fe | 2023-02-01 18:37:49 +0100 | [diff] [blame^] | 1443 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) && \ |
| 1444 | defined(MBEDTLS_CERTS_C) && \ |
| 1445 | defined(MBEDTLS_FS_IO) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1446 | if (strlen(crt_file) != 0) { |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 1447 | mbedtls_x509_crt tmp_crt; |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1448 | int ret; |
Manuel Pégourié-Gonnard | 6b84070 | 2019-05-24 09:40:17 +0200 | [diff] [blame] | 1449 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1450 | mbedtls_x509_crt_init(&tmp_crt); |
| 1451 | ret = mbedtls_x509_crt_parse_file(&tmp_crt, crt_file); |
| 1452 | if (ret != 0) { |
| 1453 | return ret; |
| 1454 | } |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 1455 | |
| 1456 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 1457 | /* Move temporary CRT. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1458 | session->peer_cert = mbedtls_calloc(1, sizeof(*session->peer_cert)); |
| 1459 | if (session->peer_cert == NULL) { |
| 1460 | return -1; |
| 1461 | } |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 1462 | *session->peer_cert = tmp_crt; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1463 | memset(&tmp_crt, 0, sizeof(tmp_crt)); |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 1464 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 1465 | /* Calculate digest of temporary CRT. */ |
| 1466 | session->peer_cert_digest = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1467 | mbedtls_calloc(1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN); |
| 1468 | if (session->peer_cert_digest == NULL) { |
| 1469 | return -1; |
| 1470 | } |
| 1471 | ret = mbedtls_md(mbedtls_md_info_from_type( |
| 1472 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE), |
| 1473 | tmp_crt.raw.p, tmp_crt.raw.len, |
| 1474 | session->peer_cert_digest); |
| 1475 | if (ret != 0) { |
| 1476 | return ret; |
| 1477 | } |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 1478 | session->peer_cert_digest_type = |
| 1479 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE; |
| 1480 | session->peer_cert_digest_len = |
| 1481 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN; |
| 1482 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 1483 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1484 | mbedtls_x509_crt_free(&tmp_crt); |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1485 | } |
Gilles Peskine | 33d03fe | 2023-02-01 18:37:49 +0100 | [diff] [blame^] | 1486 | #else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED && MBEDTLS_CERTS_C && MBEDTLS_FS_IO */ |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1487 | (void) crt_file; |
Gilles Peskine | 33d03fe | 2023-02-01 18:37:49 +0100 | [diff] [blame^] | 1488 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED && MBEDTLS_CERTS_C && MBEDTLS_FS_IO */ |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1489 | session->verify_result = 0xdeadbeef; |
| 1490 | |
| 1491 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1492 | if (ticket_len != 0) { |
| 1493 | session->ticket = mbedtls_calloc(1, ticket_len); |
| 1494 | if (session->ticket == NULL) { |
| 1495 | return -1; |
| 1496 | } |
| 1497 | memset(session->ticket, 33, ticket_len); |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1498 | } |
| 1499 | session->ticket_len = ticket_len; |
| 1500 | session->ticket_lifetime = 86401; |
| 1501 | #else |
| 1502 | (void) ticket_len; |
| 1503 | #endif |
| 1504 | |
| 1505 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 1506 | session->mfl_code = 1; |
| 1507 | #endif |
| 1508 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
| 1509 | session->trunc_hmac = 1; |
| 1510 | #endif |
| 1511 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 1512 | session->encrypt_then_mac = 1; |
| 1513 | #endif |
| 1514 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1515 | return 0; |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1516 | } |
| 1517 | |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1518 | /* |
| 1519 | * Perform data exchanging between \p ssl_1 and \p ssl_2 and check if the |
| 1520 | * message was sent in the correct number of fragments. |
| 1521 | * |
| 1522 | * /p ssl_1 and /p ssl_2 Endpoints represented by mbedtls_ssl_context. Both |
| 1523 | * of them must be initialized and connected beforehand. |
| 1524 | * /p msg_len_1 and /p msg_len_2 specify the size of the message to send. |
| 1525 | * /p expected_fragments_1 and /p expected_fragments_2 determine in how many |
| 1526 | * fragments the message should be sent. |
| 1527 | * expected_fragments is 0: can be used for DTLS testing while the message |
| 1528 | * size is larger than MFL. In that case the message |
| 1529 | * cannot be fragmented and sent to the second endpoint. |
| 1530 | * This value can be used for negative tests. |
| 1531 | * expected_fragments is 1: can be used for TLS/DTLS testing while the |
| 1532 | * message size is below MFL |
| 1533 | * expected_fragments > 1: can be used for TLS testing while the message |
| 1534 | * size is larger than MFL |
| 1535 | * |
| 1536 | * \retval 0 on success, otherwise error code. |
| 1537 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1538 | int mbedtls_exchange_data(mbedtls_ssl_context *ssl_1, |
| 1539 | int msg_len_1, const int expected_fragments_1, |
| 1540 | mbedtls_ssl_context *ssl_2, |
| 1541 | int msg_len_2, const int expected_fragments_2) |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1542 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1543 | unsigned char *msg_buf_1 = malloc(msg_len_1); |
| 1544 | unsigned char *msg_buf_2 = malloc(msg_len_2); |
| 1545 | unsigned char *in_buf_1 = malloc(msg_len_2); |
| 1546 | unsigned char *in_buf_2 = malloc(msg_len_1); |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1547 | int msg_type, ret = -1; |
| 1548 | |
| 1549 | /* Perform this test with two message types. At first use a message |
| 1550 | * consisting of only 0x00 for the client and only 0xFF for the server. |
| 1551 | * At the second time use message with generated data */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1552 | for (msg_type = 0; msg_type < 2; msg_type++) { |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1553 | int written_1 = 0; |
| 1554 | int written_2 = 0; |
| 1555 | int read_1 = 0; |
| 1556 | int read_2 = 0; |
| 1557 | int fragments_1 = 0; |
| 1558 | int fragments_2 = 0; |
| 1559 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1560 | if (msg_type == 0) { |
| 1561 | memset(msg_buf_1, 0x00, msg_len_1); |
| 1562 | memset(msg_buf_2, 0xff, msg_len_2); |
| 1563 | } else { |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1564 | int i, j = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1565 | for (i = 0; i < msg_len_1; i++) { |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1566 | msg_buf_1[i] = j++ & 0xFF; |
| 1567 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1568 | for (i = 0; i < msg_len_2; i++) { |
| 1569 | msg_buf_2[i] = (j -= 5) & 0xFF; |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1570 | } |
| 1571 | } |
| 1572 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1573 | while (read_1 < msg_len_2 || read_2 < msg_len_1) { |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1574 | /* ssl_1 sending */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1575 | if (msg_len_1 > written_1) { |
| 1576 | ret = mbedtls_ssl_write_fragment(ssl_1, msg_buf_1, |
| 1577 | msg_len_1, &written_1, |
| 1578 | expected_fragments_1); |
| 1579 | if (expected_fragments_1 == 0) { |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1580 | /* This error is expected when the message is too large and |
| 1581 | * cannot be fragmented */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1582 | TEST_ASSERT(ret == MBEDTLS_ERR_SSL_BAD_INPUT_DATA); |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1583 | msg_len_1 = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1584 | } else { |
| 1585 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1586 | } |
| 1587 | } |
| 1588 | |
| 1589 | /* ssl_2 sending */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1590 | if (msg_len_2 > written_2) { |
| 1591 | ret = mbedtls_ssl_write_fragment(ssl_2, msg_buf_2, |
| 1592 | msg_len_2, &written_2, |
| 1593 | expected_fragments_2); |
| 1594 | if (expected_fragments_2 == 0) { |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1595 | /* This error is expected when the message is too large and |
| 1596 | * cannot be fragmented */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1597 | TEST_ASSERT(ret == MBEDTLS_ERR_SSL_BAD_INPUT_DATA); |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1598 | msg_len_2 = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1599 | } else { |
| 1600 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1601 | } |
| 1602 | } |
| 1603 | |
| 1604 | /* ssl_1 reading */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1605 | if (read_1 < msg_len_2) { |
| 1606 | ret = mbedtls_ssl_read_fragment(ssl_1, in_buf_1, |
| 1607 | msg_len_2, &read_1, |
| 1608 | &fragments_2, |
| 1609 | expected_fragments_2); |
| 1610 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1611 | } |
| 1612 | |
| 1613 | /* ssl_2 reading */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1614 | if (read_2 < msg_len_1) { |
| 1615 | ret = mbedtls_ssl_read_fragment(ssl_2, in_buf_2, |
| 1616 | msg_len_1, &read_2, |
| 1617 | &fragments_1, |
| 1618 | expected_fragments_1); |
| 1619 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1620 | } |
| 1621 | } |
| 1622 | |
| 1623 | ret = -1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1624 | TEST_ASSERT(0 == memcmp(msg_buf_1, in_buf_2, msg_len_1)); |
| 1625 | TEST_ASSERT(0 == memcmp(msg_buf_2, in_buf_1, msg_len_2)); |
| 1626 | TEST_ASSERT(fragments_1 == expected_fragments_1); |
| 1627 | TEST_ASSERT(fragments_2 == expected_fragments_2); |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1628 | } |
| 1629 | |
| 1630 | ret = 0; |
| 1631 | |
| 1632 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1633 | free(msg_buf_1); |
| 1634 | free(in_buf_1); |
| 1635 | free(msg_buf_2); |
| 1636 | free(in_buf_2); |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1637 | |
| 1638 | return ret; |
| 1639 | } |
| 1640 | |
Piotr Nowicki | 95e9eb8 | 2020-02-14 11:33:34 +0100 | [diff] [blame] | 1641 | /* |
| 1642 | * Perform data exchanging between \p ssl_1 and \p ssl_2. Both of endpoints |
| 1643 | * must be initialized and connected beforehand. |
| 1644 | * |
| 1645 | * \retval 0 on success, otherwise error code. |
| 1646 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1647 | int exchange_data(mbedtls_ssl_context *ssl_1, |
| 1648 | mbedtls_ssl_context *ssl_2) |
Piotr Nowicki | 95e9eb8 | 2020-02-14 11:33:34 +0100 | [diff] [blame] | 1649 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1650 | return mbedtls_exchange_data(ssl_1, 256, 1, |
| 1651 | ssl_2, 256, 1); |
Piotr Nowicki | 95e9eb8 | 2020-02-14 11:33:34 +0100 | [diff] [blame] | 1652 | } |
| 1653 | |
Andrzej Kurek | 9155e7f | 2022-10-18 09:36:19 -0400 | [diff] [blame] | 1654 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) && \ |
Gilles Peskine | 33d03fe | 2023-02-01 18:37:49 +0100 | [diff] [blame^] | 1655 | defined(MBEDTLS_CERTS_C) && \ |
Manuel Pégourié-Gonnard | d12402f | 2020-05-20 10:34:25 +0200 | [diff] [blame] | 1656 | defined(MBEDTLS_ENTROPY_C) && \ |
| 1657 | defined(MBEDTLS_CTR_DRBG_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1658 | void perform_handshake(handshake_test_options *options) |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1659 | { |
| 1660 | /* forced_ciphersuite needs to last until the end of the handshake */ |
| 1661 | int forced_ciphersuite[2]; |
| 1662 | enum { BUFFSIZE = 17000 }; |
| 1663 | mbedtls_endpoint client, server; |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 1664 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1665 | const char *psk_identity = "foo"; |
| 1666 | #endif |
| 1667 | #if defined(MBEDTLS_TIMING_C) |
| 1668 | mbedtls_timing_delay_context timer_client, timer_server; |
| 1669 | #endif |
| 1670 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
| 1671 | unsigned char *context_buf = NULL; |
| 1672 | size_t context_buf_len; |
| 1673 | #endif |
| 1674 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 1675 | int ret = -1; |
| 1676 | #endif |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 1677 | int expected_handshake_result = 0; |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1678 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1679 | USE_PSA_INIT(); |
| 1680 | mbedtls_platform_zeroize(&client, sizeof(client)); |
| 1681 | mbedtls_platform_zeroize(&server, sizeof(server)); |
Andrzej Kurek | 0d2982b | 2022-10-18 07:55:46 -0400 | [diff] [blame] | 1682 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1683 | mbedtls_test_message_queue server_queue, client_queue; |
| 1684 | mbedtls_test_message_socket_context server_context, client_context; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1685 | mbedtls_message_socket_init(&server_context); |
| 1686 | mbedtls_message_socket_init(&client_context); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1687 | |
| 1688 | /* Client side */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1689 | if (options->dtls != 0) { |
| 1690 | TEST_ASSERT(mbedtls_endpoint_init(&client, MBEDTLS_SSL_IS_CLIENT, |
| 1691 | options->pk_alg, &client_context, |
| 1692 | &client_queue, |
| 1693 | &server_queue, NULL) == 0); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1694 | #if defined(MBEDTLS_TIMING_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1695 | mbedtls_ssl_set_timer_cb(&client.ssl, &timer_client, |
| 1696 | mbedtls_timing_set_delay, |
| 1697 | mbedtls_timing_get_delay); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1698 | #endif |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1699 | } else { |
| 1700 | TEST_ASSERT(mbedtls_endpoint_init(&client, MBEDTLS_SSL_IS_CLIENT, |
| 1701 | options->pk_alg, NULL, NULL, |
| 1702 | NULL, NULL) == 0); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1703 | } |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 1704 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1705 | if (options->client_min_version != TEST_SSL_MINOR_VERSION_NONE) { |
| 1706 | mbedtls_ssl_conf_min_version(&client.conf, MBEDTLS_SSL_MAJOR_VERSION_3, |
| 1707 | options->client_min_version); |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 1708 | } |
| 1709 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1710 | if (options->client_max_version != TEST_SSL_MINOR_VERSION_NONE) { |
| 1711 | mbedtls_ssl_conf_max_version(&client.conf, MBEDTLS_SSL_MAJOR_VERSION_3, |
| 1712 | options->client_max_version); |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 1713 | } |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1714 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1715 | if (strlen(options->cipher) > 0) { |
| 1716 | set_ciphersuite(&client.conf, options->cipher, forced_ciphersuite); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1717 | } |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 1718 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1719 | #if defined(MBEDTLS_DEBUG_C) |
| 1720 | if (options->cli_log_fun) { |
| 1721 | mbedtls_debug_set_threshold(4); |
| 1722 | mbedtls_ssl_conf_dbg(&client.conf, options->cli_log_fun, |
| 1723 | options->cli_log_obj); |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 1724 | } |
| 1725 | #endif |
| 1726 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1727 | /* Server side */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1728 | if (options->dtls != 0) { |
| 1729 | TEST_ASSERT(mbedtls_endpoint_init(&server, MBEDTLS_SSL_IS_SERVER, |
| 1730 | options->pk_alg, &server_context, |
| 1731 | &server_queue, |
| 1732 | &client_queue, NULL) == 0); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1733 | #if defined(MBEDTLS_TIMING_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1734 | mbedtls_ssl_set_timer_cb(&server.ssl, &timer_server, |
| 1735 | mbedtls_timing_set_delay, |
| 1736 | mbedtls_timing_get_delay); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1737 | #endif |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1738 | } else { |
| 1739 | TEST_ASSERT(mbedtls_endpoint_init(&server, MBEDTLS_SSL_IS_SERVER, |
| 1740 | options->pk_alg, NULL, NULL, |
| 1741 | NULL, NULL) == 0); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1742 | } |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 1743 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1744 | mbedtls_ssl_conf_authmode(&server.conf, options->srv_auth_mode); |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 1745 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1746 | if (options->server_min_version != TEST_SSL_MINOR_VERSION_NONE) { |
| 1747 | mbedtls_ssl_conf_min_version(&server.conf, MBEDTLS_SSL_MAJOR_VERSION_3, |
| 1748 | options->server_min_version); |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 1749 | } |
| 1750 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1751 | if (options->server_max_version != TEST_SSL_MINOR_VERSION_NONE) { |
| 1752 | mbedtls_ssl_conf_max_version(&server.conf, MBEDTLS_SSL_MAJOR_VERSION_3, |
| 1753 | options->server_max_version); |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 1754 | } |
| 1755 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1756 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1757 | TEST_ASSERT(mbedtls_ssl_conf_max_frag_len(&(server.conf), |
| 1758 | (unsigned char) options->mfl) == 0); |
| 1759 | TEST_ASSERT(mbedtls_ssl_conf_max_frag_len(&(client.conf), |
| 1760 | (unsigned char) options->mfl) == 0); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1761 | #else |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1762 | TEST_ASSERT(MBEDTLS_SSL_MAX_FRAG_LEN_NONE == options->mfl); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1763 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 1764 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 1765 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1766 | if (options->psk_str != NULL && options->psk_str->len > 0) { |
| 1767 | TEST_ASSERT(mbedtls_ssl_conf_psk(&client.conf, options->psk_str->x, |
| 1768 | options->psk_str->len, |
| 1769 | (const unsigned char *) psk_identity, |
| 1770 | strlen(psk_identity)) == 0); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1771 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1772 | TEST_ASSERT(mbedtls_ssl_conf_psk(&server.conf, options->psk_str->x, |
| 1773 | options->psk_str->len, |
| 1774 | (const unsigned char *) psk_identity, |
| 1775 | strlen(psk_identity)) == 0); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1776 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1777 | mbedtls_ssl_conf_psk_cb(&server.conf, psk_dummy_callback, NULL); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1778 | } |
| 1779 | #endif |
| 1780 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1781 | if (options->renegotiate) { |
| 1782 | mbedtls_ssl_conf_renegotiation(&(server.conf), |
| 1783 | MBEDTLS_SSL_RENEGOTIATION_ENABLED); |
| 1784 | mbedtls_ssl_conf_renegotiation(&(client.conf), |
| 1785 | MBEDTLS_SSL_RENEGOTIATION_ENABLED); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1786 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1787 | mbedtls_ssl_conf_legacy_renegotiation(&(server.conf), |
| 1788 | options->legacy_renegotiation); |
| 1789 | mbedtls_ssl_conf_legacy_renegotiation(&(client.conf), |
| 1790 | options->legacy_renegotiation); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1791 | } |
| 1792 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
| 1793 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1794 | #if defined(MBEDTLS_DEBUG_C) |
| 1795 | if (options->srv_log_fun) { |
| 1796 | mbedtls_debug_set_threshold(4); |
| 1797 | mbedtls_ssl_conf_dbg(&server.conf, options->srv_log_fun, |
| 1798 | options->srv_log_obj); |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 1799 | } |
| 1800 | #endif |
| 1801 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1802 | TEST_ASSERT(mbedtls_mock_socket_connect(&(client.socket), |
| 1803 | &(server.socket), |
| 1804 | BUFFSIZE) == 0); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1805 | |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 1806 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1807 | if (options->resize_buffers != 0) { |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 1808 | /* Ensure that the buffer sizes are appropriate before resizes */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1809 | TEST_ASSERT(client.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN); |
| 1810 | TEST_ASSERT(client.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN); |
| 1811 | TEST_ASSERT(server.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN); |
| 1812 | TEST_ASSERT(server.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 1813 | } |
| 1814 | #endif |
| 1815 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1816 | if (options->expected_negotiated_version == TEST_SSL_MINOR_VERSION_NONE) { |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 1817 | expected_handshake_result = MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION; |
| 1818 | } |
| 1819 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1820 | TEST_ASSERT(mbedtls_move_handshake_to_state(&(client.ssl), |
| 1821 | &(server.ssl), |
| 1822 | MBEDTLS_SSL_HANDSHAKE_OVER) |
| 1823 | == expected_handshake_result); |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 1824 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1825 | if (expected_handshake_result != 0) { |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 1826 | /* Connection will have failed by this point, skip to cleanup */ |
| 1827 | goto exit; |
| 1828 | } |
| 1829 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1830 | TEST_ASSERT(client.ssl.state == MBEDTLS_SSL_HANDSHAKE_OVER); |
| 1831 | TEST_ASSERT(server.ssl.state == MBEDTLS_SSL_HANDSHAKE_OVER); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1832 | |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 1833 | /* Check that we agree on the version... */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1834 | TEST_ASSERT(client.ssl.minor_ver == server.ssl.minor_ver); |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 1835 | |
| 1836 | /* And check that the version negotiated is the expected one. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1837 | TEST_EQUAL(client.ssl.minor_ver, options->expected_negotiated_version); |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 1838 | |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 1839 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1840 | if (options->resize_buffers != 0) { |
| 1841 | if (options->expected_negotiated_version != MBEDTLS_SSL_MINOR_VERSION_0 && |
| 1842 | options->expected_negotiated_version != MBEDTLS_SSL_MINOR_VERSION_1) { |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 1843 | /* A server, when using DTLS, might delay a buffer resize to happen |
| 1844 | * after it receives a message, so we force it. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1845 | TEST_ASSERT(exchange_data(&(client.ssl), &(server.ssl)) == 0); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 1846 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1847 | TEST_ASSERT(client.ssl.out_buf_len == |
| 1848 | mbedtls_ssl_get_output_buflen(&client.ssl)); |
| 1849 | TEST_ASSERT(client.ssl.in_buf_len == |
| 1850 | mbedtls_ssl_get_input_buflen(&client.ssl)); |
| 1851 | TEST_ASSERT(server.ssl.out_buf_len == |
| 1852 | mbedtls_ssl_get_output_buflen(&server.ssl)); |
| 1853 | TEST_ASSERT(server.ssl.in_buf_len == |
| 1854 | mbedtls_ssl_get_input_buflen(&server.ssl)); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 1855 | } |
| 1856 | } |
| 1857 | #endif |
| 1858 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1859 | if (options->cli_msg_len != 0 || options->srv_msg_len != 0) { |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1860 | /* Start data exchanging test */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1861 | TEST_ASSERT(mbedtls_exchange_data(&(client.ssl), options->cli_msg_len, |
| 1862 | options->expected_cli_fragments, |
| 1863 | &(server.ssl), options->srv_msg_len, |
| 1864 | options->expected_srv_fragments) |
| 1865 | == 0); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1866 | } |
| 1867 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1868 | if (options->serialize == 1) { |
| 1869 | TEST_ASSERT(options->dtls == 1); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1870 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1871 | TEST_ASSERT(mbedtls_ssl_context_save(&(server.ssl), NULL, |
| 1872 | 0, &context_buf_len) |
| 1873 | == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1874 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1875 | context_buf = mbedtls_calloc(1, context_buf_len); |
| 1876 | TEST_ASSERT(context_buf != NULL); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1877 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1878 | TEST_ASSERT(mbedtls_ssl_context_save(&(server.ssl), context_buf, |
| 1879 | context_buf_len, |
| 1880 | &context_buf_len) == 0); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1881 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1882 | mbedtls_ssl_free(&(server.ssl)); |
| 1883 | mbedtls_ssl_init(&(server.ssl)); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1884 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1885 | TEST_ASSERT(mbedtls_ssl_setup(&(server.ssl), &(server.conf)) == 0); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1886 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1887 | mbedtls_ssl_set_bio(&(server.ssl), &server_context, |
| 1888 | mbedtls_mock_tcp_send_msg, |
| 1889 | mbedtls_mock_tcp_recv_msg, |
| 1890 | NULL); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1891 | |
| 1892 | #if defined(MBEDTLS_TIMING_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1893 | mbedtls_ssl_set_timer_cb(&server.ssl, &timer_server, |
| 1894 | mbedtls_timing_set_delay, |
| 1895 | mbedtls_timing_get_delay); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1896 | #endif |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 1897 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1898 | if (options->resize_buffers != 0) { |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 1899 | /* Ensure that the buffer sizes are appropriate before resizes */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1900 | TEST_ASSERT(server.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN); |
| 1901 | TEST_ASSERT(server.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 1902 | } |
| 1903 | #endif |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1904 | TEST_ASSERT(mbedtls_ssl_context_load(&(server.ssl), context_buf, |
| 1905 | context_buf_len) == 0); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1906 | |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 1907 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1908 | /* Validate buffer sizes after context deserialization */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1909 | if (options->resize_buffers != 0) { |
| 1910 | TEST_ASSERT(server.ssl.out_buf_len == |
| 1911 | mbedtls_ssl_get_output_buflen(&server.ssl)); |
| 1912 | TEST_ASSERT(server.ssl.in_buf_len == |
| 1913 | mbedtls_ssl_get_input_buflen(&server.ssl)); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 1914 | } |
| 1915 | #endif |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1916 | /* Retest writing/reading */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1917 | if (options->cli_msg_len != 0 || options->srv_msg_len != 0) { |
| 1918 | TEST_ASSERT(mbedtls_exchange_data(&(client.ssl), |
| 1919 | options->cli_msg_len, |
| 1920 | options->expected_cli_fragments, |
| 1921 | &(server.ssl), |
| 1922 | options->srv_msg_len, |
| 1923 | options->expected_srv_fragments) |
| 1924 | == 0); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1925 | } |
| 1926 | } |
| 1927 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 1928 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1929 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1930 | if (options->renegotiate) { |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1931 | /* Start test with renegotiation */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1932 | TEST_ASSERT(server.ssl.renego_status == |
| 1933 | MBEDTLS_SSL_INITIAL_HANDSHAKE); |
| 1934 | TEST_ASSERT(client.ssl.renego_status == |
| 1935 | MBEDTLS_SSL_INITIAL_HANDSHAKE); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1936 | |
| 1937 | /* After calling this function for the server, it only sends a handshake |
| 1938 | * request. All renegotiation should happen during data exchanging */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1939 | TEST_ASSERT(mbedtls_ssl_renegotiate(&(server.ssl)) == 0); |
| 1940 | TEST_ASSERT(server.ssl.renego_status == |
| 1941 | MBEDTLS_SSL_RENEGOTIATION_PENDING); |
| 1942 | TEST_ASSERT(client.ssl.renego_status == |
| 1943 | MBEDTLS_SSL_INITIAL_HANDSHAKE); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1944 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1945 | TEST_ASSERT(exchange_data(&(client.ssl), &(server.ssl)) == 0); |
| 1946 | TEST_ASSERT(server.ssl.renego_status == |
| 1947 | MBEDTLS_SSL_RENEGOTIATION_DONE); |
| 1948 | TEST_ASSERT(client.ssl.renego_status == |
| 1949 | MBEDTLS_SSL_RENEGOTIATION_DONE); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1950 | |
| 1951 | /* After calling mbedtls_ssl_renegotiate for the client all renegotiation |
| 1952 | * should happen inside this function. However in this test, we cannot |
Shaun Case | 0e7791f | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 1953 | * perform simultaneous communication between client and server so this |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1954 | * function will return waiting error on the socket. All rest of |
| 1955 | * renegotiation should happen during data exchanging */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1956 | ret = mbedtls_ssl_renegotiate(&(client.ssl)); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 1957 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1958 | if (options->resize_buffers != 0) { |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 1959 | /* Ensure that the buffer sizes are appropriate before resizes */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1960 | TEST_ASSERT(client.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN); |
| 1961 | TEST_ASSERT(client.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 1962 | } |
| 1963 | #endif |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1964 | TEST_ASSERT(ret == 0 || |
| 1965 | ret == MBEDTLS_ERR_SSL_WANT_READ || |
| 1966 | ret == MBEDTLS_ERR_SSL_WANT_WRITE); |
| 1967 | TEST_ASSERT(server.ssl.renego_status == |
| 1968 | MBEDTLS_SSL_RENEGOTIATION_DONE); |
| 1969 | TEST_ASSERT(client.ssl.renego_status == |
| 1970 | MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1971 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1972 | TEST_ASSERT(exchange_data(&(client.ssl), &(server.ssl)) == 0); |
| 1973 | TEST_ASSERT(server.ssl.renego_status == |
| 1974 | MBEDTLS_SSL_RENEGOTIATION_DONE); |
| 1975 | TEST_ASSERT(client.ssl.renego_status == |
| 1976 | MBEDTLS_SSL_RENEGOTIATION_DONE); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 1977 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1978 | /* Validate buffer sizes after renegotiation */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1979 | if (options->resize_buffers != 0) { |
| 1980 | TEST_ASSERT(client.ssl.out_buf_len == |
| 1981 | mbedtls_ssl_get_output_buflen(&client.ssl)); |
| 1982 | TEST_ASSERT(client.ssl.in_buf_len == |
| 1983 | mbedtls_ssl_get_input_buflen(&client.ssl)); |
| 1984 | TEST_ASSERT(server.ssl.out_buf_len == |
| 1985 | mbedtls_ssl_get_output_buflen(&server.ssl)); |
| 1986 | TEST_ASSERT(server.ssl.in_buf_len == |
| 1987 | mbedtls_ssl_get_input_buflen(&server.ssl)); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 1988 | } |
| 1989 | #endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */ |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1990 | } |
| 1991 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
| 1992 | |
| 1993 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1994 | mbedtls_endpoint_free(&client, options->dtls != 0 ? &client_context : NULL); |
| 1995 | mbedtls_endpoint_free(&server, options->dtls != 0 ? &server_context : NULL); |
| 1996 | #if defined(MBEDTLS_DEBUG_C) |
| 1997 | if (options->cli_log_fun || options->srv_log_fun) { |
| 1998 | mbedtls_debug_set_threshold(0); |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 1999 | } |
| 2000 | #endif |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2001 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2002 | if (context_buf != NULL) { |
| 2003 | mbedtls_free(context_buf); |
| 2004 | } |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2005 | #endif |
| 2006 | } |
Gilles Peskine | 33d03fe | 2023-02-01 18:37:49 +0100 | [diff] [blame^] | 2007 | #endif \ |
| 2008 | /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED && MBEDTLS_CERTS_C && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */ |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2009 | |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 2010 | /* END_HEADER */ |
| 2011 | |
| 2012 | /* BEGIN_DEPENDENCIES |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2013 | * depends_on:MBEDTLS_SSL_TLS_C |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 2014 | * END_DEPENDENCIES |
| 2015 | */ |
| 2016 | |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2017 | /* BEGIN_CASE */ |
| 2018 | void test_callback_buffer_sanity() |
| 2019 | { |
| 2020 | enum { MSGLEN = 10 }; |
| 2021 | mbedtls_test_buffer buf; |
| 2022 | unsigned char input[MSGLEN]; |
| 2023 | unsigned char output[MSGLEN]; |
| 2024 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2025 | memset(input, 0, sizeof(input)); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2026 | |
| 2027 | /* Make sure calling put and get on NULL buffer results in error. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2028 | TEST_ASSERT(mbedtls_test_buffer_put(NULL, input, sizeof(input)) |
| 2029 | == -1); |
| 2030 | TEST_ASSERT(mbedtls_test_buffer_get(NULL, output, sizeof(output)) |
| 2031 | == -1); |
| 2032 | TEST_ASSERT(mbedtls_test_buffer_put(NULL, NULL, sizeof(input)) == -1); |
Andrzej Kurek | f777414 | 2020-01-22 06:34:59 -0500 | [diff] [blame] | 2033 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2034 | TEST_ASSERT(mbedtls_test_buffer_put(NULL, NULL, 0) == -1); |
| 2035 | TEST_ASSERT(mbedtls_test_buffer_get(NULL, NULL, 0) == -1); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2036 | |
| 2037 | /* Make sure calling put and get on a buffer that hasn't been set up results |
Shaun Case | 0e7791f | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 2038 | * in error. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2039 | mbedtls_test_buffer_init(&buf); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2040 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2041 | TEST_ASSERT(mbedtls_test_buffer_put(&buf, input, sizeof(input)) == -1); |
| 2042 | TEST_ASSERT(mbedtls_test_buffer_get(&buf, output, sizeof(output)) |
| 2043 | == -1); |
| 2044 | TEST_ASSERT(mbedtls_test_buffer_put(&buf, NULL, sizeof(input)) == -1); |
Andrzej Kurek | f777414 | 2020-01-22 06:34:59 -0500 | [diff] [blame] | 2045 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2046 | TEST_ASSERT(mbedtls_test_buffer_put(&buf, NULL, 0) == -1); |
| 2047 | TEST_ASSERT(mbedtls_test_buffer_get(&buf, NULL, 0) == -1); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2048 | |
Andrzej Kurek | f777414 | 2020-01-22 06:34:59 -0500 | [diff] [blame] | 2049 | /* Make sure calling put and get on NULL input only results in |
| 2050 | * error if the length is not zero, and that a NULL output is valid for data |
| 2051 | * dropping. |
| 2052 | */ |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2053 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2054 | TEST_ASSERT(mbedtls_test_buffer_setup(&buf, sizeof(input)) == 0); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2055 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2056 | TEST_ASSERT(mbedtls_test_buffer_put(&buf, NULL, sizeof(input)) == -1); |
| 2057 | TEST_ASSERT(mbedtls_test_buffer_get(&buf, NULL, sizeof(output)) |
| 2058 | == 0); |
| 2059 | TEST_ASSERT(mbedtls_test_buffer_put(&buf, NULL, 0) == 0); |
| 2060 | TEST_ASSERT(mbedtls_test_buffer_get(&buf, NULL, 0) == 0); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2061 | |
Piotr Nowicki | fb437d7 | 2020-01-13 16:59:12 +0100 | [diff] [blame] | 2062 | /* Make sure calling put several times in the row is safe */ |
| 2063 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2064 | TEST_ASSERT(mbedtls_test_buffer_put(&buf, input, sizeof(input)) |
| 2065 | == sizeof(input)); |
| 2066 | TEST_ASSERT(mbedtls_test_buffer_get(&buf, output, 2) == 2); |
| 2067 | TEST_ASSERT(mbedtls_test_buffer_put(&buf, input, 1) == 1); |
| 2068 | TEST_ASSERT(mbedtls_test_buffer_put(&buf, input, 2) == 1); |
| 2069 | TEST_ASSERT(mbedtls_test_buffer_put(&buf, input, 2) == 0); |
Piotr Nowicki | fb437d7 | 2020-01-13 16:59:12 +0100 | [diff] [blame] | 2070 | |
| 2071 | |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2072 | exit: |
| 2073 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2074 | mbedtls_test_buffer_free(&buf); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2075 | } |
| 2076 | /* END_CASE */ |
| 2077 | |
| 2078 | /* |
| 2079 | * Test if the implementation of `mbedtls_test_buffer` related functions is |
| 2080 | * correct and works as expected. |
| 2081 | * |
| 2082 | * That is |
| 2083 | * - If we try to put in \p put1 bytes then we can put in \p put1_ret bytes. |
| 2084 | * - Afterwards if we try to get \p get1 bytes then we can get \get1_ret bytes. |
| 2085 | * - Next, if we try to put in \p put1 bytes then we can put in \p put1_ret |
| 2086 | * bytes. |
| 2087 | * - Afterwards if we try to get \p get1 bytes then we can get \get1_ret bytes. |
| 2088 | * - All of the bytes we got match the bytes we put in in a FIFO manner. |
| 2089 | */ |
| 2090 | |
| 2091 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2092 | void test_callback_buffer(int size, int put1, int put1_ret, |
| 2093 | int get1, int get1_ret, int put2, int put2_ret, |
| 2094 | int get2, int get2_ret) |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2095 | { |
| 2096 | enum { ROUNDS = 2 }; |
| 2097 | size_t put[ROUNDS]; |
| 2098 | int put_ret[ROUNDS]; |
| 2099 | size_t get[ROUNDS]; |
| 2100 | int get_ret[ROUNDS]; |
| 2101 | mbedtls_test_buffer buf; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2102 | unsigned char *input = NULL; |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2103 | size_t input_len; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2104 | unsigned char *output = NULL; |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2105 | size_t output_len; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2106 | size_t i, j, written, read; |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2107 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2108 | mbedtls_test_buffer_init(&buf); |
| 2109 | TEST_ASSERT(mbedtls_test_buffer_setup(&buf, size) == 0); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2110 | |
| 2111 | /* Check the sanity of input parameters and initialise local variables. That |
| 2112 | * is, ensure that the amount of data is not negative and that we are not |
| 2113 | * expecting more to put or get than we actually asked for. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2114 | TEST_ASSERT(put1 >= 0); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2115 | put[0] = put1; |
| 2116 | put_ret[0] = put1_ret; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2117 | TEST_ASSERT(put1_ret <= put1); |
| 2118 | TEST_ASSERT(put2 >= 0); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2119 | put[1] = put2; |
| 2120 | put_ret[1] = put2_ret; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2121 | TEST_ASSERT(put2_ret <= put2); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2122 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2123 | TEST_ASSERT(get1 >= 0); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2124 | get[0] = get1; |
| 2125 | get_ret[0] = get1_ret; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2126 | TEST_ASSERT(get1_ret <= get1); |
| 2127 | TEST_ASSERT(get2 >= 0); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2128 | get[1] = get2; |
| 2129 | get_ret[1] = get2_ret; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2130 | TEST_ASSERT(get2_ret <= get2); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2131 | |
| 2132 | input_len = 0; |
| 2133 | /* Calculate actual input and output lengths */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2134 | for (j = 0; j < ROUNDS; j++) { |
| 2135 | if (put_ret[j] > 0) { |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2136 | input_len += put_ret[j]; |
| 2137 | } |
| 2138 | } |
| 2139 | /* In order to always have a valid pointer we always allocate at least 1 |
| 2140 | * byte. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2141 | if (input_len == 0) { |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2142 | input_len = 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2143 | } |
| 2144 | ASSERT_ALLOC(input, input_len); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2145 | |
| 2146 | output_len = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2147 | for (j = 0; j < ROUNDS; j++) { |
| 2148 | if (get_ret[j] > 0) { |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2149 | output_len += get_ret[j]; |
| 2150 | } |
| 2151 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2152 | TEST_ASSERT(output_len <= input_len); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2153 | /* In order to always have a valid pointer we always allocate at least 1 |
| 2154 | * byte. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2155 | if (output_len == 0) { |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2156 | output_len = 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2157 | } |
| 2158 | ASSERT_ALLOC(output, output_len); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2159 | |
| 2160 | /* Fill up the buffer with structured data so that unwanted changes |
| 2161 | * can be detected */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2162 | for (i = 0; i < input_len; i++) { |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2163 | input[i] = i & 0xFF; |
| 2164 | } |
| 2165 | |
| 2166 | written = read = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2167 | for (j = 0; j < ROUNDS; j++) { |
| 2168 | TEST_ASSERT(put_ret[j] == mbedtls_test_buffer_put(&buf, |
| 2169 | input + written, put[j])); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2170 | written += put_ret[j]; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2171 | TEST_ASSERT(get_ret[j] == mbedtls_test_buffer_get(&buf, |
| 2172 | output + read, get[j])); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2173 | read += get_ret[j]; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2174 | TEST_ASSERT(read <= written); |
| 2175 | if (get_ret[j] > 0) { |
| 2176 | TEST_ASSERT(memcmp(output + read - get_ret[j], |
| 2177 | input + read - get_ret[j], get_ret[j]) |
| 2178 | == 0); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2179 | } |
| 2180 | } |
| 2181 | |
| 2182 | exit: |
| 2183 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2184 | mbedtls_free(input); |
| 2185 | mbedtls_free(output); |
| 2186 | mbedtls_test_buffer_free(&buf); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2187 | } |
| 2188 | /* END_CASE */ |
| 2189 | |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2190 | /* |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2191 | * Test if the implementation of `mbedtls_mock_socket` related I/O functions is |
| 2192 | * correct and works as expected on unconnected sockets. |
| 2193 | */ |
| 2194 | |
| 2195 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2196 | void ssl_mock_sanity() |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2197 | { |
| 2198 | enum { MSGLEN = 105 }; |
Paul Elliott | 9545786 | 2021-11-24 16:54:26 +0000 | [diff] [blame] | 2199 | unsigned char message[MSGLEN] = { 0 }; |
| 2200 | unsigned char received[MSGLEN] = { 0 }; |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2201 | mbedtls_mock_socket socket; |
| 2202 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2203 | mbedtls_mock_socket_init(&socket); |
| 2204 | TEST_ASSERT(mbedtls_mock_tcp_send_b(&socket, message, MSGLEN) < 0); |
| 2205 | mbedtls_mock_socket_close(&socket); |
| 2206 | mbedtls_mock_socket_init(&socket); |
| 2207 | TEST_ASSERT(mbedtls_mock_tcp_recv_b(&socket, received, MSGLEN) < 0); |
| 2208 | mbedtls_mock_socket_close(&socket); |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2209 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2210 | mbedtls_mock_socket_init(&socket); |
| 2211 | TEST_ASSERT(mbedtls_mock_tcp_send_nb(&socket, message, MSGLEN) < 0); |
| 2212 | mbedtls_mock_socket_close(&socket); |
| 2213 | mbedtls_mock_socket_init(&socket); |
| 2214 | TEST_ASSERT(mbedtls_mock_tcp_recv_nb(&socket, received, MSGLEN) < 0); |
| 2215 | mbedtls_mock_socket_close(&socket); |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2216 | |
| 2217 | exit: |
| 2218 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2219 | mbedtls_mock_socket_close(&socket); |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2220 | } |
| 2221 | /* END_CASE */ |
| 2222 | |
| 2223 | /* |
| 2224 | * Test if the implementation of `mbedtls_mock_socket` related functions can |
| 2225 | * send a single message from the client to the server. |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2226 | */ |
| 2227 | |
| 2228 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2229 | void ssl_mock_tcp(int blocking) |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2230 | { |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2231 | enum { MSGLEN = 105 }; |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2232 | enum { BUFLEN = MSGLEN / 5 }; |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2233 | unsigned char message[MSGLEN]; |
| 2234 | unsigned char received[MSGLEN]; |
| 2235 | mbedtls_mock_socket client; |
| 2236 | mbedtls_mock_socket server; |
| 2237 | size_t written, read; |
| 2238 | int send_ret, recv_ret; |
| 2239 | mbedtls_ssl_send_t *send; |
| 2240 | mbedtls_ssl_recv_t *recv; |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2241 | unsigned i; |
| 2242 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2243 | if (blocking == 0) { |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2244 | send = mbedtls_mock_tcp_send_nb; |
| 2245 | recv = mbedtls_mock_tcp_recv_nb; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2246 | } else { |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2247 | send = mbedtls_mock_tcp_send_b; |
| 2248 | recv = mbedtls_mock_tcp_recv_b; |
| 2249 | } |
| 2250 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2251 | mbedtls_mock_socket_init(&client); |
| 2252 | mbedtls_mock_socket_init(&server); |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2253 | |
| 2254 | /* Fill up the buffer with structured data so that unwanted changes |
| 2255 | * can be detected */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2256 | for (i = 0; i < MSGLEN; i++) { |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2257 | message[i] = i & 0xFF; |
| 2258 | } |
| 2259 | |
| 2260 | /* Make sure that sending a message takes a few iterations. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2261 | TEST_ASSERT(0 == mbedtls_mock_socket_connect(&client, &server, BUFLEN)); |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2262 | |
| 2263 | /* Send the message to the server */ |
| 2264 | send_ret = recv_ret = 1; |
| 2265 | written = read = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2266 | while (send_ret != 0 || recv_ret != 0) { |
| 2267 | send_ret = send(&client, message + written, MSGLEN - written); |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2268 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2269 | TEST_ASSERT(send_ret >= 0); |
| 2270 | TEST_ASSERT(send_ret <= BUFLEN); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2271 | written += send_ret; |
| 2272 | |
| 2273 | /* If the buffer is full we can test blocking and non-blocking send */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2274 | if (send_ret == BUFLEN) { |
| 2275 | int blocking_ret = send(&client, message, 1); |
| 2276 | if (blocking) { |
| 2277 | TEST_ASSERT(blocking_ret == 0); |
| 2278 | } else { |
| 2279 | TEST_ASSERT(blocking_ret == MBEDTLS_ERR_SSL_WANT_WRITE); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2280 | } |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2281 | } |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2282 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2283 | recv_ret = recv(&server, received + read, MSGLEN - read); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2284 | |
| 2285 | /* The result depends on whether any data was sent */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2286 | if (send_ret > 0) { |
| 2287 | TEST_ASSERT(recv_ret > 0); |
| 2288 | TEST_ASSERT(recv_ret <= BUFLEN); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2289 | read += recv_ret; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2290 | } else if (blocking) { |
| 2291 | TEST_ASSERT(recv_ret == 0); |
| 2292 | } else { |
| 2293 | TEST_ASSERT(recv_ret == MBEDTLS_ERR_SSL_WANT_READ); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2294 | recv_ret = 0; |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2295 | } |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2296 | |
| 2297 | /* If the buffer is empty we can test blocking and non-blocking read */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2298 | if (recv_ret == BUFLEN) { |
| 2299 | int blocking_ret = recv(&server, received, 1); |
| 2300 | if (blocking) { |
| 2301 | TEST_ASSERT(blocking_ret == 0); |
| 2302 | } else { |
| 2303 | TEST_ASSERT(blocking_ret == MBEDTLS_ERR_SSL_WANT_READ); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2304 | } |
| 2305 | } |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2306 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2307 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2308 | |
| 2309 | exit: |
| 2310 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2311 | mbedtls_mock_socket_close(&client); |
| 2312 | mbedtls_mock_socket_close(&server); |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2313 | } |
| 2314 | /* END_CASE */ |
| 2315 | |
| 2316 | /* |
| 2317 | * Test if the implementation of `mbedtls_mock_socket` related functions can |
| 2318 | * send messages in both direction at the same time (with the I/O calls |
| 2319 | * interleaving). |
| 2320 | */ |
| 2321 | |
| 2322 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2323 | void ssl_mock_tcp_interleaving(int blocking) |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2324 | { |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2325 | enum { ROUNDS = 2 }; |
| 2326 | enum { MSGLEN = 105 }; |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2327 | enum { BUFLEN = MSGLEN / 5 }; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2328 | unsigned char message[ROUNDS][MSGLEN]; |
| 2329 | unsigned char received[ROUNDS][MSGLEN]; |
| 2330 | mbedtls_mock_socket client; |
| 2331 | mbedtls_mock_socket server; |
| 2332 | size_t written[ROUNDS]; |
| 2333 | size_t read[ROUNDS]; |
| 2334 | int send_ret[ROUNDS]; |
| 2335 | int recv_ret[ROUNDS]; |
| 2336 | unsigned i, j, progress; |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 2337 | mbedtls_ssl_send_t *send; |
| 2338 | mbedtls_ssl_recv_t *recv; |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 2339 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2340 | if (blocking == 0) { |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 2341 | send = mbedtls_mock_tcp_send_nb; |
| 2342 | recv = mbedtls_mock_tcp_recv_nb; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2343 | } else { |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 2344 | send = mbedtls_mock_tcp_send_b; |
| 2345 | recv = mbedtls_mock_tcp_recv_b; |
| 2346 | } |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2347 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2348 | mbedtls_mock_socket_init(&client); |
| 2349 | mbedtls_mock_socket_init(&server); |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2350 | |
| 2351 | /* Fill up the buffers with structured data so that unwanted changes |
| 2352 | * can be detected */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2353 | for (i = 0; i < ROUNDS; i++) { |
| 2354 | for (j = 0; j < MSGLEN; j++) { |
| 2355 | message[i][j] = (i * MSGLEN + j) & 0xFF; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2356 | } |
| 2357 | } |
| 2358 | |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2359 | /* Make sure that sending a message takes a few iterations. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2360 | TEST_ASSERT(0 == mbedtls_mock_socket_connect(&client, &server, BUFLEN)); |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2361 | |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2362 | /* Send the message from both sides, interleaving. */ |
| 2363 | progress = 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2364 | for (i = 0; i < ROUNDS; i++) { |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2365 | written[i] = 0; |
| 2366 | read[i] = 0; |
| 2367 | } |
| 2368 | /* This loop does not stop as long as there was a successful write or read |
| 2369 | * of at least one byte on either side. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2370 | while (progress != 0) { |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2371 | mbedtls_mock_socket *socket; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2372 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2373 | for (i = 0; i < ROUNDS; i++) { |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2374 | /* First sending is from the client */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2375 | socket = (i % 2 == 0) ? (&client) : (&server); |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2376 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2377 | send_ret[i] = send(socket, message[i] + written[i], |
| 2378 | MSGLEN - written[i]); |
| 2379 | TEST_ASSERT(send_ret[i] >= 0); |
| 2380 | TEST_ASSERT(send_ret[i] <= BUFLEN); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2381 | written[i] += send_ret[i]; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2382 | |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2383 | /* If the buffer is full we can test blocking and non-blocking |
| 2384 | * send */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2385 | if (send_ret[i] == BUFLEN) { |
| 2386 | int blocking_ret = send(socket, message[i], 1); |
| 2387 | if (blocking) { |
| 2388 | TEST_ASSERT(blocking_ret == 0); |
| 2389 | } else { |
| 2390 | TEST_ASSERT(blocking_ret == MBEDTLS_ERR_SSL_WANT_WRITE); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2391 | } |
| 2392 | } |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 2393 | } |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2394 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2395 | for (i = 0; i < ROUNDS; i++) { |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2396 | /* First receiving is from the server */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2397 | socket = (i % 2 == 0) ? (&server) : (&client); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2398 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2399 | recv_ret[i] = recv(socket, received[i] + read[i], |
| 2400 | MSGLEN - read[i]); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2401 | |
| 2402 | /* The result depends on whether any data was sent */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2403 | if (send_ret[i] > 0) { |
| 2404 | TEST_ASSERT(recv_ret[i] > 0); |
| 2405 | TEST_ASSERT(recv_ret[i] <= BUFLEN); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2406 | read[i] += recv_ret[i]; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2407 | } else if (blocking) { |
| 2408 | TEST_ASSERT(recv_ret[i] == 0); |
| 2409 | } else { |
| 2410 | TEST_ASSERT(recv_ret[i] == MBEDTLS_ERR_SSL_WANT_READ); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2411 | recv_ret[i] = 0; |
| 2412 | } |
| 2413 | |
| 2414 | /* If the buffer is empty we can test blocking and non-blocking |
| 2415 | * read */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2416 | if (recv_ret[i] == BUFLEN) { |
| 2417 | int blocking_ret = recv(socket, received[i], 1); |
| 2418 | if (blocking) { |
| 2419 | TEST_ASSERT(blocking_ret == 0); |
| 2420 | } else { |
| 2421 | TEST_ASSERT(blocking_ret == MBEDTLS_ERR_SSL_WANT_READ); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2422 | } |
| 2423 | } |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 2424 | } |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2425 | |
| 2426 | progress = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2427 | for (i = 0; i < ROUNDS; i++) { |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2428 | progress += send_ret[i] + recv_ret[i]; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2429 | } |
| 2430 | } |
| 2431 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2432 | for (i = 0; i < ROUNDS; i++) { |
| 2433 | TEST_ASSERT(memcmp(message[i], received[i], MSGLEN) == 0); |
| 2434 | } |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2435 | |
| 2436 | exit: |
| 2437 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2438 | mbedtls_mock_socket_close(&client); |
| 2439 | mbedtls_mock_socket_close(&server); |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2440 | } |
| 2441 | /* END_CASE */ |
| 2442 | |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2443 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2444 | void ssl_message_queue_sanity() |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2445 | { |
| 2446 | mbedtls_test_message_queue queue; |
| 2447 | |
| 2448 | /* Trying to push/pull to an empty queue */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2449 | TEST_ASSERT(mbedtls_test_message_queue_push_info(NULL, 1) |
| 2450 | == MBEDTLS_TEST_ERROR_ARG_NULL); |
| 2451 | TEST_ASSERT(mbedtls_test_message_queue_pop_info(NULL, 1) |
| 2452 | == MBEDTLS_TEST_ERROR_ARG_NULL); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2453 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2454 | TEST_ASSERT(mbedtls_test_message_queue_setup(&queue, 3) == 0); |
| 2455 | TEST_ASSERT(queue.capacity == 3); |
| 2456 | TEST_ASSERT(queue.num == 0); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2457 | |
| 2458 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2459 | mbedtls_test_message_queue_free(&queue); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2460 | } |
| 2461 | /* END_CASE */ |
| 2462 | |
| 2463 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2464 | void ssl_message_queue_basic() |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2465 | { |
| 2466 | mbedtls_test_message_queue queue; |
| 2467 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2468 | TEST_ASSERT(mbedtls_test_message_queue_setup(&queue, 3) == 0); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2469 | |
| 2470 | /* Sanity test - 3 pushes and 3 pops with sufficient space */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2471 | TEST_ASSERT(mbedtls_test_message_queue_push_info(&queue, 1) == 1); |
| 2472 | TEST_ASSERT(queue.capacity == 3); |
| 2473 | TEST_ASSERT(queue.num == 1); |
| 2474 | TEST_ASSERT(mbedtls_test_message_queue_push_info(&queue, 1) == 1); |
| 2475 | TEST_ASSERT(queue.capacity == 3); |
| 2476 | TEST_ASSERT(queue.num == 2); |
| 2477 | TEST_ASSERT(mbedtls_test_message_queue_push_info(&queue, 2) == 2); |
| 2478 | TEST_ASSERT(queue.capacity == 3); |
| 2479 | TEST_ASSERT(queue.num == 3); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2480 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2481 | TEST_ASSERT(mbedtls_test_message_queue_pop_info(&queue, 1) == 1); |
| 2482 | TEST_ASSERT(mbedtls_test_message_queue_pop_info(&queue, 1) == 1); |
| 2483 | TEST_ASSERT(mbedtls_test_message_queue_pop_info(&queue, 2) == 2); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2484 | |
| 2485 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2486 | mbedtls_test_message_queue_free(&queue); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2487 | } |
| 2488 | /* END_CASE */ |
| 2489 | |
| 2490 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2491 | void ssl_message_queue_overflow_underflow() |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2492 | { |
| 2493 | mbedtls_test_message_queue queue; |
| 2494 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2495 | TEST_ASSERT(mbedtls_test_message_queue_setup(&queue, 3) == 0); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2496 | |
| 2497 | /* 4 pushes (last one with an error), 4 pops (last one with an error) */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2498 | TEST_ASSERT(mbedtls_test_message_queue_push_info(&queue, 1) == 1); |
| 2499 | TEST_ASSERT(mbedtls_test_message_queue_push_info(&queue, 1) == 1); |
| 2500 | TEST_ASSERT(mbedtls_test_message_queue_push_info(&queue, 2) == 2); |
| 2501 | TEST_ASSERT(mbedtls_test_message_queue_push_info(&queue, 3) |
| 2502 | == MBEDTLS_ERR_SSL_WANT_WRITE); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2503 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2504 | TEST_ASSERT(mbedtls_test_message_queue_pop_info(&queue, 1) == 1); |
| 2505 | TEST_ASSERT(mbedtls_test_message_queue_pop_info(&queue, 1) == 1); |
| 2506 | TEST_ASSERT(mbedtls_test_message_queue_pop_info(&queue, 2) == 2); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2507 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2508 | TEST_ASSERT(mbedtls_test_message_queue_pop_info(&queue, 1) |
| 2509 | == MBEDTLS_ERR_SSL_WANT_READ); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2510 | |
| 2511 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2512 | mbedtls_test_message_queue_free(&queue); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2513 | } |
| 2514 | /* END_CASE */ |
| 2515 | |
| 2516 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2517 | void ssl_message_queue_interleaved() |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2518 | { |
| 2519 | mbedtls_test_message_queue queue; |
| 2520 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2521 | TEST_ASSERT(mbedtls_test_message_queue_setup(&queue, 3) == 0); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2522 | |
| 2523 | /* Interleaved test - [2 pushes, 1 pop] twice, and then two pops |
| 2524 | * (to wrap around the buffer) */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2525 | TEST_ASSERT(mbedtls_test_message_queue_push_info(&queue, 1) == 1); |
| 2526 | TEST_ASSERT(mbedtls_test_message_queue_push_info(&queue, 1) == 1); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2527 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2528 | TEST_ASSERT(mbedtls_test_message_queue_pop_info(&queue, 1) == 1); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2529 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2530 | TEST_ASSERT(mbedtls_test_message_queue_push_info(&queue, 2) == 2); |
| 2531 | TEST_ASSERT(mbedtls_test_message_queue_push_info(&queue, 3) == 3); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2532 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2533 | TEST_ASSERT(mbedtls_test_message_queue_pop_info(&queue, 1) == 1); |
| 2534 | TEST_ASSERT(mbedtls_test_message_queue_pop_info(&queue, 2) == 2); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2535 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2536 | TEST_ASSERT(mbedtls_test_message_queue_push_info(&queue, 5) == 5); |
| 2537 | TEST_ASSERT(mbedtls_test_message_queue_push_info(&queue, 8) == 8); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2538 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2539 | TEST_ASSERT(mbedtls_test_message_queue_pop_info(&queue, 3) == 3); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2540 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2541 | TEST_ASSERT(mbedtls_test_message_queue_pop_info(&queue, 5) == 5); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2542 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2543 | TEST_ASSERT(mbedtls_test_message_queue_pop_info(&queue, 8) == 8); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2544 | |
| 2545 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2546 | mbedtls_test_message_queue_free(&queue); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2547 | } |
| 2548 | /* END_CASE */ |
| 2549 | |
| 2550 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2551 | void ssl_message_queue_insufficient_buffer() |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2552 | { |
| 2553 | mbedtls_test_message_queue queue; |
| 2554 | size_t message_len = 10; |
| 2555 | size_t buffer_len = 5; |
| 2556 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2557 | TEST_ASSERT(mbedtls_test_message_queue_setup(&queue, 1) == 0); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2558 | |
| 2559 | /* Popping without a sufficient buffer */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2560 | TEST_ASSERT(mbedtls_test_message_queue_push_info(&queue, message_len) |
| 2561 | == (int) message_len); |
| 2562 | TEST_ASSERT(mbedtls_test_message_queue_pop_info(&queue, buffer_len) |
| 2563 | == (int) buffer_len); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2564 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2565 | mbedtls_test_message_queue_free(&queue); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2566 | } |
| 2567 | /* END_CASE */ |
| 2568 | |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2569 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2570 | void ssl_message_mock_uninitialized() |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2571 | { |
| 2572 | enum { MSGLEN = 10 }; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2573 | unsigned char message[MSGLEN] = { 0 }, received[MSGLEN]; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2574 | mbedtls_mock_socket client, server; |
| 2575 | mbedtls_test_message_queue server_queue, client_queue; |
| 2576 | mbedtls_test_message_socket_context server_context, client_context; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2577 | mbedtls_message_socket_init(&server_context); |
| 2578 | mbedtls_message_socket_init(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2579 | |
| 2580 | /* Send with a NULL context */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2581 | TEST_ASSERT(mbedtls_mock_tcp_send_msg(NULL, message, MSGLEN) |
| 2582 | == MBEDTLS_TEST_ERROR_CONTEXT_ERROR); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2583 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2584 | TEST_ASSERT(mbedtls_mock_tcp_recv_msg(NULL, message, MSGLEN) |
| 2585 | == MBEDTLS_TEST_ERROR_CONTEXT_ERROR); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2586 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2587 | TEST_ASSERT(mbedtls_message_socket_setup(&server_queue, &client_queue, 1, |
| 2588 | &server, |
| 2589 | &server_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2590 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2591 | TEST_ASSERT(mbedtls_message_socket_setup(&client_queue, &server_queue, 1, |
| 2592 | &client, |
| 2593 | &client_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2594 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2595 | TEST_ASSERT(mbedtls_mock_tcp_send_msg(&client_context, message, MSGLEN) |
| 2596 | == MBEDTLS_TEST_ERROR_SEND_FAILED); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2597 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2598 | TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&server_context, received, MSGLEN) |
| 2599 | == MBEDTLS_ERR_SSL_WANT_READ); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2600 | |
| 2601 | /* Push directly to a queue to later simulate a disconnected behavior */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2602 | TEST_ASSERT(mbedtls_test_message_queue_push_info(&server_queue, MSGLEN) |
| 2603 | == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2604 | |
| 2605 | /* Test if there's an error when trying to read from a disconnected |
| 2606 | * socket */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2607 | TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&server_context, received, MSGLEN) |
| 2608 | == MBEDTLS_TEST_ERROR_RECV_FAILED); |
| 2609 | exit: |
| 2610 | mbedtls_message_socket_close(&server_context); |
| 2611 | mbedtls_message_socket_close(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2612 | } |
| 2613 | /* END_CASE */ |
| 2614 | |
| 2615 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2616 | void ssl_message_mock_basic() |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2617 | { |
| 2618 | enum { MSGLEN = 10 }; |
| 2619 | unsigned char message[MSGLEN], received[MSGLEN]; |
| 2620 | mbedtls_mock_socket client, server; |
| 2621 | unsigned i; |
| 2622 | mbedtls_test_message_queue server_queue, client_queue; |
| 2623 | mbedtls_test_message_socket_context server_context, client_context; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2624 | mbedtls_message_socket_init(&server_context); |
| 2625 | mbedtls_message_socket_init(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2626 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2627 | TEST_ASSERT(mbedtls_message_socket_setup(&server_queue, &client_queue, 1, |
| 2628 | &server, |
| 2629 | &server_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2630 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2631 | TEST_ASSERT(mbedtls_message_socket_setup(&client_queue, &server_queue, 1, |
| 2632 | &client, |
| 2633 | &client_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2634 | |
| 2635 | /* Fill up the buffer with structured data so that unwanted changes |
| 2636 | * can be detected */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2637 | for (i = 0; i < MSGLEN; i++) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2638 | message[i] = i & 0xFF; |
| 2639 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2640 | TEST_ASSERT(0 == mbedtls_mock_socket_connect(&client, &server, |
| 2641 | MSGLEN)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2642 | |
| 2643 | /* Send the message to the server */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2644 | TEST_ASSERT(mbedtls_mock_tcp_send_msg(&client_context, message, |
| 2645 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2646 | |
| 2647 | /* Read from the server */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2648 | TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&server_context, received, MSGLEN) |
| 2649 | == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2650 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2651 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
| 2652 | memset(received, 0, MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2653 | |
| 2654 | /* Send the message to the client */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2655 | TEST_ASSERT(mbedtls_mock_tcp_send_msg(&server_context, message, |
| 2656 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2657 | |
| 2658 | /* Read from the client */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2659 | TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&client_context, received, MSGLEN) |
| 2660 | == MSGLEN); |
| 2661 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2662 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2663 | exit: |
| 2664 | mbedtls_message_socket_close(&server_context); |
| 2665 | mbedtls_message_socket_close(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2666 | } |
| 2667 | /* END_CASE */ |
| 2668 | |
| 2669 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2670 | void ssl_message_mock_queue_overflow_underflow() |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2671 | { |
| 2672 | enum { MSGLEN = 10 }; |
| 2673 | unsigned char message[MSGLEN], received[MSGLEN]; |
| 2674 | mbedtls_mock_socket client, server; |
| 2675 | unsigned i; |
| 2676 | mbedtls_test_message_queue server_queue, client_queue; |
| 2677 | mbedtls_test_message_socket_context server_context, client_context; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2678 | mbedtls_message_socket_init(&server_context); |
| 2679 | mbedtls_message_socket_init(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2680 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2681 | TEST_ASSERT(mbedtls_message_socket_setup(&server_queue, &client_queue, 2, |
| 2682 | &server, |
| 2683 | &server_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2684 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2685 | TEST_ASSERT(mbedtls_message_socket_setup(&client_queue, &server_queue, 2, |
| 2686 | &client, |
| 2687 | &client_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2688 | |
| 2689 | /* Fill up the buffer with structured data so that unwanted changes |
| 2690 | * can be detected */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2691 | for (i = 0; i < MSGLEN; i++) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2692 | message[i] = i & 0xFF; |
| 2693 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2694 | TEST_ASSERT(0 == mbedtls_mock_socket_connect(&client, &server, |
| 2695 | MSGLEN*2)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2696 | |
| 2697 | /* Send three message to the server, last one with an error */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2698 | TEST_ASSERT(mbedtls_mock_tcp_send_msg(&client_context, message, |
| 2699 | MSGLEN - 1) == MSGLEN - 1); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2700 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2701 | TEST_ASSERT(mbedtls_mock_tcp_send_msg(&client_context, message, |
| 2702 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2703 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2704 | TEST_ASSERT(mbedtls_mock_tcp_send_msg(&client_context, message, |
| 2705 | MSGLEN) |
| 2706 | == MBEDTLS_ERR_SSL_WANT_WRITE); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2707 | |
| 2708 | /* Read three messages from the server, last one with an error */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2709 | TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&server_context, received, |
| 2710 | MSGLEN - 1) == MSGLEN - 1); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2711 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2712 | TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&server_context, received, MSGLEN) |
| 2713 | == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2714 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2715 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2716 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2717 | TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&server_context, received, MSGLEN) |
| 2718 | == MBEDTLS_ERR_SSL_WANT_READ); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2719 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2720 | exit: |
| 2721 | mbedtls_message_socket_close(&server_context); |
| 2722 | mbedtls_message_socket_close(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2723 | } |
| 2724 | /* END_CASE */ |
| 2725 | |
| 2726 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2727 | void ssl_message_mock_socket_overflow() |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2728 | { |
| 2729 | enum { MSGLEN = 10 }; |
| 2730 | unsigned char message[MSGLEN], received[MSGLEN]; |
| 2731 | mbedtls_mock_socket client, server; |
| 2732 | unsigned i; |
| 2733 | mbedtls_test_message_queue server_queue, client_queue; |
| 2734 | mbedtls_test_message_socket_context server_context, client_context; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2735 | mbedtls_message_socket_init(&server_context); |
| 2736 | mbedtls_message_socket_init(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2737 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2738 | TEST_ASSERT(mbedtls_message_socket_setup(&server_queue, &client_queue, 2, |
| 2739 | &server, |
| 2740 | &server_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2741 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2742 | TEST_ASSERT(mbedtls_message_socket_setup(&client_queue, &server_queue, 2, |
| 2743 | &client, |
| 2744 | &client_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2745 | |
| 2746 | /* Fill up the buffer with structured data so that unwanted changes |
| 2747 | * can be detected */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2748 | for (i = 0; i < MSGLEN; i++) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2749 | message[i] = i & 0xFF; |
| 2750 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2751 | TEST_ASSERT(0 == mbedtls_mock_socket_connect(&client, &server, |
| 2752 | MSGLEN)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2753 | |
| 2754 | /* Send two message to the server, second one with an error */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2755 | TEST_ASSERT(mbedtls_mock_tcp_send_msg(&client_context, message, |
| 2756 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2757 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2758 | TEST_ASSERT(mbedtls_mock_tcp_send_msg(&client_context, message, |
| 2759 | MSGLEN) |
| 2760 | == MBEDTLS_TEST_ERROR_SEND_FAILED); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2761 | |
| 2762 | /* Read the only message from the server */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2763 | TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&server_context, received, MSGLEN) |
| 2764 | == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2765 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2766 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2767 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2768 | exit: |
| 2769 | mbedtls_message_socket_close(&server_context); |
| 2770 | mbedtls_message_socket_close(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2771 | } |
| 2772 | /* END_CASE */ |
| 2773 | |
| 2774 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2775 | void ssl_message_mock_truncated() |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2776 | { |
| 2777 | enum { MSGLEN = 10 }; |
| 2778 | unsigned char message[MSGLEN], received[MSGLEN]; |
| 2779 | mbedtls_mock_socket client, server; |
| 2780 | unsigned i; |
| 2781 | mbedtls_test_message_queue server_queue, client_queue; |
| 2782 | mbedtls_test_message_socket_context server_context, client_context; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2783 | mbedtls_message_socket_init(&server_context); |
| 2784 | mbedtls_message_socket_init(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2785 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2786 | TEST_ASSERT(mbedtls_message_socket_setup(&server_queue, &client_queue, 2, |
| 2787 | &server, |
| 2788 | &server_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2789 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2790 | TEST_ASSERT(mbedtls_message_socket_setup(&client_queue, &server_queue, 2, |
| 2791 | &client, |
| 2792 | &client_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2793 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2794 | memset(received, 0, MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2795 | /* Fill up the buffer with structured data so that unwanted changes |
| 2796 | * can be detected */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2797 | for (i = 0; i < MSGLEN; i++) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2798 | message[i] = i & 0xFF; |
| 2799 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2800 | TEST_ASSERT(0 == mbedtls_mock_socket_connect(&client, &server, |
| 2801 | 2 * MSGLEN)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2802 | |
| 2803 | /* Send two messages to the server, the second one small enough to fit in the |
| 2804 | * receiver's buffer. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2805 | TEST_ASSERT(mbedtls_mock_tcp_send_msg(&client_context, message, |
| 2806 | MSGLEN) == MSGLEN); |
| 2807 | TEST_ASSERT(mbedtls_mock_tcp_send_msg(&client_context, message, |
| 2808 | MSGLEN / 2) == MSGLEN / 2); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2809 | /* Read a truncated message from the server */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2810 | TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&server_context, received, MSGLEN/2) |
| 2811 | == MSGLEN/2); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2812 | |
| 2813 | /* Test that the first half of the message is valid, and second one isn't */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2814 | TEST_ASSERT(memcmp(message, received, MSGLEN/2) == 0); |
| 2815 | TEST_ASSERT(memcmp(message + MSGLEN/2, received + MSGLEN/2, MSGLEN/2) |
| 2816 | != 0); |
| 2817 | memset(received, 0, MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2818 | |
| 2819 | /* Read a full message from the server */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2820 | TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&server_context, received, MSGLEN/2) |
| 2821 | == MSGLEN / 2); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2822 | |
| 2823 | /* Test that the first half of the message is valid */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2824 | TEST_ASSERT(memcmp(message, received, MSGLEN/2) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2825 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2826 | exit: |
| 2827 | mbedtls_message_socket_close(&server_context); |
| 2828 | mbedtls_message_socket_close(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2829 | } |
| 2830 | /* END_CASE */ |
| 2831 | |
| 2832 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2833 | void ssl_message_mock_socket_read_error() |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2834 | { |
| 2835 | enum { MSGLEN = 10 }; |
| 2836 | unsigned char message[MSGLEN], received[MSGLEN]; |
| 2837 | mbedtls_mock_socket client, server; |
| 2838 | unsigned i; |
| 2839 | mbedtls_test_message_queue server_queue, client_queue; |
| 2840 | mbedtls_test_message_socket_context server_context, client_context; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2841 | mbedtls_message_socket_init(&server_context); |
| 2842 | mbedtls_message_socket_init(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2843 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2844 | TEST_ASSERT(mbedtls_message_socket_setup(&server_queue, &client_queue, 1, |
| 2845 | &server, |
| 2846 | &server_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2847 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2848 | TEST_ASSERT(mbedtls_message_socket_setup(&client_queue, &server_queue, 1, |
| 2849 | &client, |
| 2850 | &client_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2851 | |
| 2852 | /* Fill up the buffer with structured data so that unwanted changes |
| 2853 | * can be detected */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2854 | for (i = 0; i < MSGLEN; i++) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2855 | message[i] = i & 0xFF; |
| 2856 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2857 | TEST_ASSERT(0 == mbedtls_mock_socket_connect(&client, &server, |
| 2858 | MSGLEN)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2859 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2860 | TEST_ASSERT(mbedtls_mock_tcp_send_msg(&client_context, message, |
| 2861 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2862 | |
| 2863 | /* Force a read error by disconnecting the socket by hand */ |
| 2864 | server.status = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2865 | TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&server_context, received, MSGLEN) |
| 2866 | == MBEDTLS_TEST_ERROR_RECV_FAILED); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2867 | /* Return to a valid state */ |
| 2868 | server.status = MBEDTLS_MOCK_SOCKET_CONNECTED; |
| 2869 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2870 | memset(received, 0, sizeof(received)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2871 | |
| 2872 | /* Test that even though the server tried to read once disconnected, the |
| 2873 | * continuity is preserved */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2874 | TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&server_context, received, MSGLEN) |
| 2875 | == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2876 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2877 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2878 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2879 | exit: |
| 2880 | mbedtls_message_socket_close(&server_context); |
| 2881 | mbedtls_message_socket_close(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2882 | } |
| 2883 | /* END_CASE */ |
| 2884 | |
| 2885 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2886 | void ssl_message_mock_interleaved_one_way() |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2887 | { |
| 2888 | enum { MSGLEN = 10 }; |
| 2889 | unsigned char message[MSGLEN], received[MSGLEN]; |
| 2890 | mbedtls_mock_socket client, server; |
| 2891 | unsigned i; |
| 2892 | mbedtls_test_message_queue server_queue, client_queue; |
| 2893 | mbedtls_test_message_socket_context server_context, client_context; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2894 | mbedtls_message_socket_init(&server_context); |
| 2895 | mbedtls_message_socket_init(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2896 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2897 | TEST_ASSERT(mbedtls_message_socket_setup(&server_queue, &client_queue, 3, |
| 2898 | &server, |
| 2899 | &server_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2900 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2901 | TEST_ASSERT(mbedtls_message_socket_setup(&client_queue, &server_queue, 3, |
| 2902 | &client, |
| 2903 | &client_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2904 | |
| 2905 | /* Fill up the buffer with structured data so that unwanted changes |
| 2906 | * can be detected */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2907 | for (i = 0; i < MSGLEN; i++) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2908 | message[i] = i & 0xFF; |
| 2909 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2910 | TEST_ASSERT(0 == mbedtls_mock_socket_connect(&client, &server, |
| 2911 | MSGLEN*3)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2912 | |
| 2913 | /* Interleaved test - [2 sends, 1 read] twice, and then two reads |
| 2914 | * (to wrap around the buffer) */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2915 | for (i = 0; i < 2; i++) { |
| 2916 | TEST_ASSERT(mbedtls_mock_tcp_send_msg(&client_context, message, |
| 2917 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2918 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2919 | TEST_ASSERT(mbedtls_mock_tcp_send_msg(&client_context, message, |
| 2920 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2921 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2922 | TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&server_context, received, |
| 2923 | MSGLEN) == MSGLEN); |
| 2924 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
| 2925 | memset(received, 0, sizeof(received)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2926 | } |
| 2927 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2928 | for (i = 0; i < 2; i++) { |
| 2929 | TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&server_context, received, |
| 2930 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2931 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2932 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2933 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2934 | TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&server_context, received, MSGLEN) |
| 2935 | == MBEDTLS_ERR_SSL_WANT_READ); |
| 2936 | exit: |
| 2937 | mbedtls_message_socket_close(&server_context); |
| 2938 | mbedtls_message_socket_close(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2939 | } |
| 2940 | /* END_CASE */ |
| 2941 | |
| 2942 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2943 | void ssl_message_mock_interleaved_two_ways() |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2944 | { |
| 2945 | enum { MSGLEN = 10 }; |
| 2946 | unsigned char message[MSGLEN], received[MSGLEN]; |
| 2947 | mbedtls_mock_socket client, server; |
| 2948 | unsigned i; |
| 2949 | mbedtls_test_message_queue server_queue, client_queue; |
| 2950 | mbedtls_test_message_socket_context server_context, client_context; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2951 | mbedtls_message_socket_init(&server_context); |
| 2952 | mbedtls_message_socket_init(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2953 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2954 | TEST_ASSERT(mbedtls_message_socket_setup(&server_queue, &client_queue, 3, |
| 2955 | &server, |
| 2956 | &server_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2957 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2958 | TEST_ASSERT(mbedtls_message_socket_setup(&client_queue, &server_queue, 3, |
| 2959 | &client, |
| 2960 | &client_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2961 | |
| 2962 | /* Fill up the buffer with structured data so that unwanted changes |
| 2963 | * can be detected */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2964 | for (i = 0; i < MSGLEN; i++) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2965 | message[i] = i & 0xFF; |
| 2966 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2967 | TEST_ASSERT(0 == mbedtls_mock_socket_connect(&client, &server, |
| 2968 | MSGLEN*3)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2969 | |
| 2970 | /* Interleaved test - [2 sends, 1 read] twice, both ways, and then two reads |
| 2971 | * (to wrap around the buffer) both ways. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2972 | for (i = 0; i < 2; i++) { |
| 2973 | TEST_ASSERT(mbedtls_mock_tcp_send_msg(&client_context, message, |
| 2974 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2975 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2976 | TEST_ASSERT(mbedtls_mock_tcp_send_msg(&client_context, message, |
| 2977 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2978 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2979 | TEST_ASSERT(mbedtls_mock_tcp_send_msg(&server_context, message, |
| 2980 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2981 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2982 | TEST_ASSERT(mbedtls_mock_tcp_send_msg(&server_context, message, |
| 2983 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2984 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2985 | TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&server_context, received, |
| 2986 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2987 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2988 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2989 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2990 | memset(received, 0, sizeof(received)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2991 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2992 | TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&client_context, received, |
| 2993 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2994 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2995 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2996 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2997 | memset(received, 0, sizeof(received)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2998 | } |
| 2999 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3000 | for (i = 0; i < 2; i++) { |
| 3001 | TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&server_context, received, |
| 3002 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3003 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3004 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
| 3005 | memset(received, 0, sizeof(received)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3006 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3007 | TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&client_context, received, |
| 3008 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3009 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3010 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
| 3011 | memset(received, 0, sizeof(received)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3012 | } |
| 3013 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3014 | TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&server_context, received, MSGLEN) |
| 3015 | == MBEDTLS_ERR_SSL_WANT_READ); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3016 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3017 | TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&client_context, received, MSGLEN) |
| 3018 | == MBEDTLS_ERR_SSL_WANT_READ); |
| 3019 | exit: |
| 3020 | mbedtls_message_socket_close(&server_context); |
| 3021 | mbedtls_message_socket_close(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3022 | } |
| 3023 | /* END_CASE */ |
| 3024 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3025 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3026 | 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] | 3027 | { |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 3028 | uint32_t len = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3029 | mbedtls_ssl_context ssl; |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 3030 | mbedtls_ssl_config conf; |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3031 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3032 | mbedtls_ssl_init(&ssl); |
| 3033 | mbedtls_ssl_config_init(&conf); |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 3034 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3035 | TEST_ASSERT(mbedtls_ssl_config_defaults(&conf, |
| 3036 | MBEDTLS_SSL_IS_CLIENT, |
| 3037 | MBEDTLS_SSL_TRANSPORT_DATAGRAM, |
| 3038 | MBEDTLS_SSL_PRESET_DEFAULT) == 0); |
| 3039 | TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0); |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3040 | |
| 3041 | /* Read previous record numbers */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3042 | for (len = 0; len < prevs->len; len += 6) { |
| 3043 | memcpy(ssl.in_ctr + 2, prevs->x + len, 6); |
| 3044 | mbedtls_ssl_dtls_replay_update(&ssl); |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3045 | } |
| 3046 | |
| 3047 | /* Check new number */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3048 | memcpy(ssl.in_ctr + 2, new->x, 6); |
| 3049 | TEST_ASSERT(mbedtls_ssl_dtls_replay_check(&ssl) == ret); |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3050 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3051 | mbedtls_ssl_free(&ssl); |
| 3052 | mbedtls_ssl_config_free(&conf); |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3053 | } |
| 3054 | /* END_CASE */ |
Hanno Becker | b25c0c7 | 2017-05-05 11:24:30 +0100 | [diff] [blame] | 3055 | |
Gilles Peskine | 33d03fe | 2023-02-01 18:37:49 +0100 | [diff] [blame^] | 3056 | /* BEGIN_CASE depends_on:MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED:MBEDTLS_CERTS_C */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3057 | void ssl_set_hostname_twice(char *hostname0, char *hostname1) |
Hanno Becker | b25c0c7 | 2017-05-05 11:24:30 +0100 | [diff] [blame] | 3058 | { |
| 3059 | mbedtls_ssl_context ssl; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3060 | mbedtls_ssl_init(&ssl); |
Hanno Becker | b25c0c7 | 2017-05-05 11:24:30 +0100 | [diff] [blame] | 3061 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3062 | TEST_ASSERT(mbedtls_ssl_set_hostname(&ssl, hostname0) == 0); |
| 3063 | TEST_ASSERT(mbedtls_ssl_set_hostname(&ssl, hostname1) == 0); |
Hanno Becker | b25c0c7 | 2017-05-05 11:24:30 +0100 | [diff] [blame] | 3064 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3065 | mbedtls_ssl_free(&ssl); |
Hanno Becker | b25c0c7 | 2017-05-05 11:24:30 +0100 | [diff] [blame] | 3066 | } |
Darryl Green | 11999bb | 2018-03-13 15:22:58 +0000 | [diff] [blame] | 3067 | /* END_CASE */ |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3068 | |
| 3069 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3070 | void ssl_crypt_record(int cipher_type, int hash_id, |
| 3071 | int etm, int tag_mode, int ver, |
| 3072 | int cid0_len, int cid1_len) |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3073 | { |
| 3074 | /* |
| 3075 | * Test several record encryptions and decryptions |
| 3076 | * with plenty of space before and after the data |
| 3077 | * within the record buffer. |
| 3078 | */ |
| 3079 | |
| 3080 | int ret; |
| 3081 | int num_records = 16; |
| 3082 | mbedtls_ssl_context ssl; /* ONLY for debugging */ |
| 3083 | |
| 3084 | mbedtls_ssl_transform t0, t1; |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 3085 | unsigned char *buf = NULL; |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3086 | size_t const buflen = 512; |
| 3087 | mbedtls_record rec, rec_backup; |
| 3088 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3089 | mbedtls_ssl_init(&ssl); |
| 3090 | mbedtls_ssl_transform_init(&t0); |
| 3091 | mbedtls_ssl_transform_init(&t1); |
| 3092 | TEST_ASSERT(build_transforms(&t0, &t1, cipher_type, hash_id, |
| 3093 | etm, tag_mode, ver, |
| 3094 | (size_t) cid0_len, |
| 3095 | (size_t) cid1_len) == 0); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3096 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3097 | TEST_ASSERT((buf = mbedtls_calloc(1, buflen)) != NULL); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3098 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3099 | while (num_records-- > 0) { |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3100 | mbedtls_ssl_transform *t_dec, *t_enc; |
| 3101 | /* Take turns in who's sending and who's receiving. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3102 | if (num_records % 3 == 0) { |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3103 | t_dec = &t0; |
| 3104 | t_enc = &t1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3105 | } else { |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3106 | t_dec = &t1; |
| 3107 | t_enc = &t0; |
| 3108 | } |
| 3109 | |
| 3110 | /* |
| 3111 | * The record header affects the transformation in two ways: |
| 3112 | * 1) It determines the AEAD additional data |
| 3113 | * 2) The record counter sometimes determines the IV. |
| 3114 | * |
| 3115 | * Apart from that, the fields don't have influence. |
| 3116 | * In particular, it is currently not the responsibility |
| 3117 | * of ssl_encrypt/decrypt_buf to check if the transform |
| 3118 | * version matches the record version, or that the |
| 3119 | * type is sensible. |
| 3120 | */ |
| 3121 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3122 | memset(rec.ctr, num_records, sizeof(rec.ctr)); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3123 | rec.type = 42; |
| 3124 | rec.ver[0] = num_records; |
| 3125 | rec.ver[1] = num_records; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3126 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 3127 | rec.cid_len = 0; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3128 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3129 | |
| 3130 | rec.buf = buf; |
| 3131 | rec.buf_len = buflen; |
| 3132 | rec.data_offset = 16; |
| 3133 | /* Make sure to vary the length to exercise different |
| 3134 | * paddings. */ |
| 3135 | rec.data_len = 1 + num_records; |
| 3136 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3137 | memset(rec.buf + rec.data_offset, 42, rec.data_len); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3138 | |
| 3139 | /* Make a copy for later comparison */ |
| 3140 | rec_backup = rec; |
| 3141 | |
| 3142 | /* Encrypt record */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3143 | ret = mbedtls_ssl_encrypt_buf(&ssl, t_enc, &rec, |
| 3144 | mbedtls_test_rnd_std_rand, NULL); |
| 3145 | TEST_ASSERT(ret == 0 || ret == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL); |
| 3146 | if (ret != 0) { |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3147 | continue; |
| 3148 | } |
| 3149 | |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 3150 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3151 | if (rec.cid_len != 0) { |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 3152 | /* DTLS 1.2 + CID hides the real content type and |
| 3153 | * uses a special CID content type in the protected |
| 3154 | * record. Double-check this. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3155 | TEST_ASSERT(rec.type == MBEDTLS_SSL_MSG_CID); |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 3156 | } |
| 3157 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 3158 | |
| 3159 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3160 | if (t_enc->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4) { |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 3161 | /* TLS 1.3 hides the real content type and |
| 3162 | * always uses Application Data as the content type |
| 3163 | * for protected records. Double-check this. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3164 | TEST_ASSERT(rec.type == MBEDTLS_SSL_MSG_APPLICATION_DATA); |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 3165 | } |
| 3166 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ |
| 3167 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3168 | /* Decrypt record with t_dec */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3169 | ret = mbedtls_ssl_decrypt_buf(&ssl, t_dec, &rec); |
| 3170 | TEST_ASSERT(ret == 0); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3171 | |
| 3172 | /* Compare results */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3173 | TEST_ASSERT(rec.type == rec_backup.type); |
| 3174 | TEST_ASSERT(memcmp(rec.ctr, rec_backup.ctr, 8) == 0); |
| 3175 | TEST_ASSERT(rec.ver[0] == rec_backup.ver[0]); |
| 3176 | TEST_ASSERT(rec.ver[1] == rec_backup.ver[1]); |
| 3177 | TEST_ASSERT(rec.data_len == rec_backup.data_len); |
| 3178 | TEST_ASSERT(rec.data_offset == rec_backup.data_offset); |
| 3179 | TEST_ASSERT(memcmp(rec.buf + rec.data_offset, |
| 3180 | rec_backup.buf + rec_backup.data_offset, |
| 3181 | rec.data_len) == 0); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3182 | } |
| 3183 | |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 3184 | exit: |
| 3185 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3186 | /* Cleanup */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3187 | mbedtls_ssl_free(&ssl); |
| 3188 | mbedtls_ssl_transform_free(&t0); |
| 3189 | mbedtls_ssl_transform_free(&t1); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3190 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3191 | mbedtls_free(buf); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3192 | } |
| 3193 | /* END_CASE */ |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3194 | |
| 3195 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3196 | void ssl_crypt_record_small(int cipher_type, int hash_id, |
| 3197 | int etm, int tag_mode, int ver, |
| 3198 | int cid0_len, int cid1_len) |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3199 | { |
| 3200 | /* |
| 3201 | * Test pairs of encryption and decryption with an increasing |
| 3202 | * amount of space in the record buffer - in more detail: |
| 3203 | * 1) Try to encrypt with 0, 1, 2, ... bytes available |
| 3204 | * in front of the plaintext, and expect the encryption |
| 3205 | * to succeed starting from some offset. Always keep |
| 3206 | * enough space in the end of the buffer. |
| 3207 | * 2) Try to encrypt with 0, 1, 2, ... bytes available |
| 3208 | * at the end of the plaintext, and expect the encryption |
| 3209 | * to succeed starting from some offset. Always keep |
| 3210 | * enough space at the beginning of the buffer. |
| 3211 | * 3) Try to encrypt with 0, 1, 2, ... bytes available |
| 3212 | * both at the front and end of the plaintext, |
| 3213 | * and expect the encryption to succeed starting from |
| 3214 | * some offset. |
| 3215 | * |
| 3216 | * If encryption succeeds, check that decryption succeeds |
| 3217 | * and yields the original record. |
| 3218 | */ |
| 3219 | |
| 3220 | mbedtls_ssl_context ssl; /* ONLY for debugging */ |
| 3221 | |
| 3222 | mbedtls_ssl_transform t0, t1; |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 3223 | unsigned char *buf = NULL; |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 3224 | size_t const buflen = 256; |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3225 | mbedtls_record rec, rec_backup; |
| 3226 | |
| 3227 | int ret; |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 3228 | int mode; /* Mode 1, 2 or 3 as explained above */ |
| 3229 | size_t offset; /* Available space at beginning/end/both */ |
| 3230 | size_t threshold = 96; /* Maximum offset to test against */ |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3231 | |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 3232 | size_t default_pre_padding = 64; /* Pre-padding to use in mode 2 */ |
| 3233 | 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] | 3234 | |
| 3235 | int seen_success; /* Indicates if in the current mode we've |
| 3236 | * already seen a successful test. */ |
| 3237 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3238 | mbedtls_ssl_init(&ssl); |
| 3239 | mbedtls_ssl_transform_init(&t0); |
| 3240 | mbedtls_ssl_transform_init(&t1); |
| 3241 | TEST_ASSERT(build_transforms(&t0, &t1, cipher_type, hash_id, |
| 3242 | etm, tag_mode, ver, |
| 3243 | (size_t) cid0_len, |
| 3244 | (size_t) cid1_len) == 0); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3245 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3246 | TEST_ASSERT((buf = mbedtls_calloc(1, buflen)) != NULL); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3247 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3248 | for (mode = 1; mode <= 3; mode++) { |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3249 | seen_success = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3250 | for (offset = 0; offset <= threshold; offset++) { |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3251 | mbedtls_ssl_transform *t_dec, *t_enc; |
Hanno Becker | 6c87b3f | 2019-04-29 17:24:44 +0100 | [diff] [blame] | 3252 | t_dec = &t0; |
| 3253 | t_enc = &t1; |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3254 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3255 | memset(rec.ctr, offset, sizeof(rec.ctr)); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3256 | rec.type = 42; |
| 3257 | rec.ver[0] = offset; |
| 3258 | rec.ver[1] = offset; |
| 3259 | rec.buf = buf; |
| 3260 | rec.buf_len = buflen; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3261 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 3262 | rec.cid_len = 0; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3263 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3264 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3265 | switch (mode) { |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3266 | case 1: /* Space in the beginning */ |
| 3267 | rec.data_offset = offset; |
| 3268 | rec.data_len = buflen - offset - default_post_padding; |
| 3269 | break; |
| 3270 | |
| 3271 | case 2: /* Space in the end */ |
| 3272 | rec.data_offset = default_pre_padding; |
| 3273 | rec.data_len = buflen - default_pre_padding - offset; |
| 3274 | break; |
| 3275 | |
| 3276 | case 3: /* Space in the beginning and end */ |
| 3277 | rec.data_offset = offset; |
| 3278 | rec.data_len = buflen - 2 * offset; |
| 3279 | break; |
| 3280 | |
| 3281 | default: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3282 | TEST_ASSERT(0); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3283 | break; |
| 3284 | } |
| 3285 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3286 | memset(rec.buf + rec.data_offset, 42, rec.data_len); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3287 | |
| 3288 | /* Make a copy for later comparison */ |
| 3289 | rec_backup = rec; |
| 3290 | |
| 3291 | /* Encrypt record */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3292 | ret = mbedtls_ssl_encrypt_buf(&ssl, t_enc, &rec, |
| 3293 | mbedtls_test_rnd_std_rand, NULL); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3294 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3295 | if ((mode == 1 || mode == 2) && seen_success) { |
| 3296 | TEST_ASSERT(ret == 0); |
| 3297 | } else { |
| 3298 | TEST_ASSERT(ret == 0 || ret == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL); |
| 3299 | if (ret == 0) { |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3300 | seen_success = 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3301 | } |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3302 | } |
| 3303 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3304 | if (ret != 0) { |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3305 | continue; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3306 | } |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3307 | |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 3308 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3309 | if (rec.cid_len != 0) { |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 3310 | /* DTLS 1.2 + CID hides the real content type and |
| 3311 | * uses a special CID content type in the protected |
| 3312 | * record. Double-check this. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3313 | TEST_ASSERT(rec.type == MBEDTLS_SSL_MSG_CID); |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 3314 | } |
| 3315 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 3316 | |
| 3317 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3318 | if (t_enc->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4) { |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 3319 | /* TLS 1.3 hides the real content type and |
| 3320 | * always uses Application Data as the content type |
| 3321 | * for protected records. Double-check this. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3322 | TEST_ASSERT(rec.type == MBEDTLS_SSL_MSG_APPLICATION_DATA); |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 3323 | } |
| 3324 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ |
| 3325 | |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3326 | /* Decrypt record with t_dec */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3327 | TEST_ASSERT(mbedtls_ssl_decrypt_buf(&ssl, t_dec, &rec) == 0); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3328 | |
| 3329 | /* Compare results */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3330 | TEST_ASSERT(rec.type == rec_backup.type); |
| 3331 | TEST_ASSERT(memcmp(rec.ctr, rec_backup.ctr, 8) == 0); |
| 3332 | TEST_ASSERT(rec.ver[0] == rec_backup.ver[0]); |
| 3333 | TEST_ASSERT(rec.ver[1] == rec_backup.ver[1]); |
| 3334 | TEST_ASSERT(rec.data_len == rec_backup.data_len); |
| 3335 | TEST_ASSERT(rec.data_offset == rec_backup.data_offset); |
| 3336 | TEST_ASSERT(memcmp(rec.buf + rec.data_offset, |
| 3337 | rec_backup.buf + rec_backup.data_offset, |
| 3338 | rec.data_len) == 0); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3339 | } |
| 3340 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3341 | TEST_ASSERT(seen_success == 1); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3342 | } |
| 3343 | |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 3344 | exit: |
| 3345 | |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3346 | /* Cleanup */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3347 | mbedtls_ssl_free(&ssl); |
| 3348 | mbedtls_ssl_transform_free(&t0); |
| 3349 | mbedtls_ssl_transform_free(&t1); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3350 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3351 | mbedtls_free(buf); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3352 | } |
| 3353 | /* END_CASE */ |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 3354 | |
Manuel Pégourié-Gonnard | 913a204 | 2020-07-09 10:02:41 +0200 | [diff] [blame] | 3355 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC:MBEDTLS_AES_C:MBEDTLS_SSL_PROTO_TLS1_2 */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3356 | void ssl_decrypt_non_etm_cbc(int cipher_type, int hash_id, int trunc_hmac, |
| 3357 | int length_selector) |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3358 | { |
| 3359 | /* |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3360 | * Test record decryption for CBC without EtM, focused on the verification |
| 3361 | * of padding and MAC. |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3362 | * |
| 3363 | * Actually depends on TLS >= 1.0 (SSL 3.0 computes the MAC differently), |
Manuel Pégourié-Gonnard | 913a204 | 2020-07-09 10:02:41 +0200 | [diff] [blame] | 3364 | * and either AES, ARIA, Camellia or DES, but since the test framework |
| 3365 | * doesn't support alternation in dependency statements, just depend on |
| 3366 | * TLS 1.2 and AES. |
Manuel Pégourié-Gonnard | 864abbf | 2020-07-21 10:37:14 +0200 | [diff] [blame] | 3367 | * |
| 3368 | * The length_selector argument is interpreted as follows: |
| 3369 | * - if it's -1, the plaintext length is 0 and minimal padding is applied |
| 3370 | * - if it's -2, the plaintext length is 0 and maximal padding is applied |
| 3371 | * - otherwise it must be in [0, 255] and is padding_length from RFC 5246: |
| 3372 | * it's the length of the rest of the padding, that is, excluding the |
| 3373 | * byte that encodes the length. The minimal non-zero plaintext length |
| 3374 | * that gives this padding_length is automatically selected. |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3375 | */ |
| 3376 | mbedtls_ssl_context ssl; /* ONLY for debugging */ |
| 3377 | mbedtls_ssl_transform t0, t1; |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3378 | mbedtls_record rec, rec_save; |
| 3379 | unsigned char *buf = NULL, *buf_save = NULL; |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3380 | size_t buflen, olen = 0; |
Manuel Pégourié-Gonnard | 864abbf | 2020-07-21 10:37:14 +0200 | [diff] [blame] | 3381 | size_t plaintext_len, block_size, i; |
Manuel Pégourié-Gonnard | e55653f | 2020-07-22 11:42:57 +0200 | [diff] [blame] | 3382 | unsigned char padlen; /* excluding the padding_length byte */ |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3383 | unsigned char add_data[13]; |
| 3384 | unsigned char mac[MBEDTLS_MD_MAX_SIZE]; |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3385 | int exp_ret; |
Manuel Pégourié-Gonnard | 4adc04a | 2020-07-16 10:00:48 +0200 | [diff] [blame] | 3386 | const unsigned char pad_max_len = 255; /* Per the standard */ |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3387 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3388 | mbedtls_ssl_init(&ssl); |
| 3389 | mbedtls_ssl_transform_init(&t0); |
| 3390 | mbedtls_ssl_transform_init(&t1); |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3391 | |
| 3392 | /* Set up transforms with dummy keys */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3393 | TEST_ASSERT(build_transforms(&t0, &t1, cipher_type, hash_id, |
| 3394 | 0, trunc_hmac, |
| 3395 | MBEDTLS_SSL_MINOR_VERSION_3, |
| 3396 | 0, 0) == 0); |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3397 | |
Manuel Pégourié-Gonnard | 864abbf | 2020-07-21 10:37:14 +0200 | [diff] [blame] | 3398 | /* Determine padding/plaintext length */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3399 | TEST_ASSERT(length_selector >= -2 && length_selector <= 255); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3400 | block_size = t0.ivlen; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3401 | if (length_selector < 0) { |
Manuel Pégourié-Gonnard | 864abbf | 2020-07-21 10:37:14 +0200 | [diff] [blame] | 3402 | plaintext_len = 0; |
| 3403 | |
Manuel Pégourié-Gonnard | e55653f | 2020-07-22 11:42:57 +0200 | [diff] [blame] | 3404 | /* Minimal padding |
| 3405 | * The +1 is for the padding_length byte, not counted in padlen. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3406 | padlen = block_size - (t0.maclen + 1) % block_size; |
Manuel Pégourié-Gonnard | 864abbf | 2020-07-21 10:37:14 +0200 | [diff] [blame] | 3407 | |
| 3408 | /* Maximal padding? */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3409 | if (length_selector == -2) { |
| 3410 | padlen += block_size * ((pad_max_len - padlen) / block_size); |
| 3411 | } |
| 3412 | } else { |
Manuel Pégourié-Gonnard | 864abbf | 2020-07-21 10:37:14 +0200 | [diff] [blame] | 3413 | padlen = length_selector; |
| 3414 | |
Manuel Pégourié-Gonnard | e55653f | 2020-07-22 11:42:57 +0200 | [diff] [blame] | 3415 | /* Minimal non-zero plaintext_length giving desired padding. |
| 3416 | * The +1 is for the padding_length byte, not counted in padlen. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3417 | plaintext_len = block_size - (padlen + t0.maclen + 1) % block_size; |
Manuel Pégourié-Gonnard | 864abbf | 2020-07-21 10:37:14 +0200 | [diff] [blame] | 3418 | } |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3419 | |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3420 | /* Prepare a buffer for record data */ |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3421 | buflen = block_size |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3422 | + plaintext_len |
| 3423 | + t0.maclen |
| 3424 | + padlen + 1; |
| 3425 | ASSERT_ALLOC(buf, buflen); |
| 3426 | ASSERT_ALLOC(buf_save, buflen); |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3427 | |
| 3428 | /* Prepare a dummy record header */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3429 | memset(rec.ctr, 0, sizeof(rec.ctr)); |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3430 | rec.type = MBEDTLS_SSL_MSG_APPLICATION_DATA; |
| 3431 | rec.ver[0] = MBEDTLS_SSL_MAJOR_VERSION_3; |
| 3432 | rec.ver[1] = MBEDTLS_SSL_MINOR_VERSION_3; |
| 3433 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 3434 | rec.cid_len = 0; |
| 3435 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 3436 | |
| 3437 | /* Prepare dummy record content */ |
| 3438 | rec.buf = buf; |
| 3439 | rec.buf_len = buflen; |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3440 | rec.data_offset = block_size; |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3441 | rec.data_len = plaintext_len; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3442 | memset(rec.buf + rec.data_offset, 42, rec.data_len); |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3443 | |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3444 | /* Serialized version of record header for MAC purposes */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3445 | memcpy(add_data, rec.ctr, 8); |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3446 | add_data[8] = rec.type; |
| 3447 | add_data[9] = rec.ver[0]; |
| 3448 | add_data[10] = rec.ver[1]; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3449 | add_data[11] = (rec.data_len >> 8) & 0xff; |
| 3450 | add_data[12] = (rec.data_len >> 0) & 0xff; |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3451 | |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3452 | /* Set dummy IV */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3453 | memset(t0.iv_enc, 0x55, t0.ivlen); |
| 3454 | memcpy(rec.buf, t0.iv_enc, t0.ivlen); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3455 | |
| 3456 | /* |
| 3457 | * Prepare a pre-encryption record (with MAC and padding), and save it. |
| 3458 | */ |
| 3459 | |
| 3460 | /* MAC with additional data */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3461 | TEST_EQUAL(0, mbedtls_md_hmac_update(&t0.md_ctx_enc, add_data, 13)); |
| 3462 | TEST_EQUAL(0, mbedtls_md_hmac_update(&t0.md_ctx_enc, |
| 3463 | rec.buf + rec.data_offset, |
| 3464 | rec.data_len)); |
| 3465 | TEST_EQUAL(0, mbedtls_md_hmac_finish(&t0.md_ctx_enc, mac)); |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3466 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3467 | 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] | 3468 | rec.data_len += t0.maclen; |
| 3469 | |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3470 | /* Pad */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3471 | 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] | 3472 | rec.data_len += padlen + 1; |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3473 | |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3474 | /* Save correct pre-encryption record */ |
| 3475 | rec_save = rec; |
| 3476 | rec_save.buf = buf_save; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3477 | memcpy(buf_save, buf, buflen); |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3478 | |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3479 | /* |
| 3480 | * Encrypt and decrypt the correct record, expecting success |
| 3481 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3482 | TEST_EQUAL(0, mbedtls_cipher_crypt(&t0.cipher_ctx_enc, |
| 3483 | t0.iv_enc, t0.ivlen, |
| 3484 | rec.buf + rec.data_offset, rec.data_len, |
| 3485 | rec.buf + rec.data_offset, &olen)); |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3486 | rec.data_offset -= t0.ivlen; |
| 3487 | rec.data_len += t0.ivlen; |
| 3488 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3489 | TEST_EQUAL(0, mbedtls_ssl_decrypt_buf(&ssl, &t1, &rec)); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3490 | |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3491 | /* |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3492 | * Modify each byte of the pre-encryption record before encrypting and |
| 3493 | * decrypting it, expecting failure every time. |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3494 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3495 | for (i = block_size; i < buflen; i++) { |
| 3496 | mbedtls_test_set_step(i); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3497 | |
| 3498 | /* Restore correct pre-encryption record */ |
| 3499 | rec = rec_save; |
| 3500 | rec.buf = buf; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3501 | memcpy(buf, buf_save, buflen); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3502 | |
Manuel Pégourié-Gonnard | b51f044 | 2020-07-21 10:40:25 +0200 | [diff] [blame] | 3503 | /* 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] | 3504 | rec.buf[i] ^= 0x01; |
| 3505 | |
| 3506 | /* Encrypt */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3507 | TEST_EQUAL(0, mbedtls_cipher_crypt(&t0.cipher_ctx_enc, |
| 3508 | t0.iv_enc, t0.ivlen, |
| 3509 | rec.buf + rec.data_offset, rec.data_len, |
| 3510 | rec.buf + rec.data_offset, &olen)); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3511 | rec.data_offset -= t0.ivlen; |
| 3512 | rec.data_len += t0.ivlen; |
| 3513 | |
| 3514 | /* Decrypt and expect failure */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3515 | TEST_EQUAL(MBEDTLS_ERR_SSL_INVALID_MAC, |
| 3516 | mbedtls_ssl_decrypt_buf(&ssl, &t1, &rec)); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3517 | } |
| 3518 | |
| 3519 | /* |
| 3520 | * Use larger values of the padding bytes - with small buffers, this tests |
| 3521 | * the case where the announced padlen would be larger than the buffer |
| 3522 | * (and before that, than the buffer minus the size of the MAC), to make |
| 3523 | * sure our padding checking code does not perform any out-of-bounds reads |
| 3524 | * in this case. (With larger buffers, ie when the plaintext is long or |
| 3525 | * maximal length padding is used, this is less relevant but still doesn't |
| 3526 | * hurt to test.) |
| 3527 | * |
| 3528 | * (Start the loop with correct padding, just to double-check that record |
| 3529 | * saving did work, and that we're overwriting the correct bytes.) |
| 3530 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3531 | for (i = padlen; i <= pad_max_len; i++) { |
| 3532 | mbedtls_test_set_step(i); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3533 | |
| 3534 | /* Restore correct pre-encryption record */ |
| 3535 | rec = rec_save; |
| 3536 | rec.buf = buf; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3537 | memcpy(buf, buf_save, buflen); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3538 | |
| 3539 | /* Set padding bytes to new value */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3540 | memset(buf + buflen - padlen - 1, i, padlen + 1); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3541 | |
| 3542 | /* Encrypt */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3543 | TEST_EQUAL(0, mbedtls_cipher_crypt(&t0.cipher_ctx_enc, |
| 3544 | t0.iv_enc, t0.ivlen, |
| 3545 | rec.buf + rec.data_offset, rec.data_len, |
| 3546 | rec.buf + rec.data_offset, &olen)); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3547 | rec.data_offset -= t0.ivlen; |
| 3548 | rec.data_len += t0.ivlen; |
| 3549 | |
| 3550 | /* Decrypt and expect failure except the first time */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3551 | exp_ret = (i == padlen) ? 0 : MBEDTLS_ERR_SSL_INVALID_MAC; |
| 3552 | 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] | 3553 | } |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3554 | |
| 3555 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3556 | mbedtls_ssl_free(&ssl); |
| 3557 | mbedtls_ssl_transform_free(&t0); |
| 3558 | mbedtls_ssl_transform_free(&t1); |
| 3559 | mbedtls_free(buf); |
| 3560 | mbedtls_free(buf_save); |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3561 | } |
| 3562 | /* END_CASE */ |
| 3563 | |
Hanno Becker | 39ff492 | 2020-08-21 13:36:56 +0100 | [diff] [blame] | 3564 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3565 | void ssl_tls1_3_hkdf_expand_label(int hash_alg, |
| 3566 | data_t *secret, |
| 3567 | int label_idx, |
| 3568 | data_t *ctx, |
| 3569 | int desired_length, |
| 3570 | data_t *expected) |
Hanno Becker | 39ff492 | 2020-08-21 13:36:56 +0100 | [diff] [blame] | 3571 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3572 | unsigned char dst[100]; |
Hanno Becker | 39ff492 | 2020-08-21 13:36:56 +0100 | [diff] [blame] | 3573 | |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 3574 | unsigned char const *lbl = NULL; |
| 3575 | size_t lbl_len; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3576 | #define MBEDTLS_SSL_TLS1_3_LABEL(name, string) \ |
| 3577 | if (label_idx == (int) tls1_3_label_ ## name) \ |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 3578 | { \ |
| 3579 | lbl = mbedtls_ssl_tls1_3_labels.name; \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3580 | lbl_len = sizeof(mbedtls_ssl_tls1_3_labels.name); \ |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 3581 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3582 | MBEDTLS_SSL_TLS1_3_LABEL_LIST |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 3583 | #undef MBEDTLS_SSL_TLS1_3_LABEL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3584 | TEST_ASSERT(lbl != NULL); |
Hanno Becker | 39ff492 | 2020-08-21 13:36:56 +0100 | [diff] [blame] | 3585 | |
| 3586 | /* Check sanity of test parameters. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3587 | TEST_ASSERT((size_t) desired_length <= sizeof(dst)); |
| 3588 | TEST_ASSERT((size_t) desired_length == expected->len); |
Hanno Becker | 39ff492 | 2020-08-21 13:36:56 +0100 | [diff] [blame] | 3589 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3590 | TEST_ASSERT(mbedtls_ssl_tls1_3_hkdf_expand_label( |
| 3591 | (mbedtls_md_type_t) hash_alg, |
| 3592 | secret->x, secret->len, |
| 3593 | lbl, lbl_len, |
| 3594 | ctx->x, ctx->len, |
| 3595 | dst, desired_length) == 0); |
Hanno Becker | 39ff492 | 2020-08-21 13:36:56 +0100 | [diff] [blame] | 3596 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3597 | ASSERT_COMPARE(dst, (size_t) desired_length, |
| 3598 | expected->x, (size_t) expected->len); |
Hanno Becker | 39ff492 | 2020-08-21 13:36:56 +0100 | [diff] [blame] | 3599 | } |
| 3600 | /* END_CASE */ |
| 3601 | |
Hanno Becker | 19498f8 | 2020-08-21 13:37:08 +0100 | [diff] [blame] | 3602 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3603 | void ssl_tls1_3_traffic_key_generation(int hash_alg, |
| 3604 | data_t *server_secret, |
| 3605 | data_t *client_secret, |
| 3606 | int desired_iv_len, |
| 3607 | int desired_key_len, |
| 3608 | data_t *expected_server_write_key, |
| 3609 | data_t *expected_server_write_iv, |
| 3610 | data_t *expected_client_write_key, |
| 3611 | data_t *expected_client_write_iv) |
Hanno Becker | 19498f8 | 2020-08-21 13:37:08 +0100 | [diff] [blame] | 3612 | { |
| 3613 | mbedtls_ssl_key_set keys; |
| 3614 | |
| 3615 | /* Check sanity of test parameters. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3616 | TEST_ASSERT(client_secret->len == server_secret->len); |
| 3617 | TEST_ASSERT(expected_client_write_iv->len == expected_server_write_iv->len && |
| 3618 | expected_client_write_iv->len == (size_t) desired_iv_len); |
| 3619 | TEST_ASSERT(expected_client_write_key->len == expected_server_write_key->len && |
| 3620 | expected_client_write_key->len == (size_t) desired_key_len); |
Hanno Becker | 19498f8 | 2020-08-21 13:37:08 +0100 | [diff] [blame] | 3621 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3622 | TEST_ASSERT(mbedtls_ssl_tls1_3_make_traffic_keys( |
| 3623 | (mbedtls_md_type_t) hash_alg, |
| 3624 | client_secret->x, |
| 3625 | server_secret->x, |
| 3626 | client_secret->len /* == server_secret->len */, |
| 3627 | desired_key_len, desired_iv_len, |
| 3628 | &keys) == 0); |
Hanno Becker | 19498f8 | 2020-08-21 13:37:08 +0100 | [diff] [blame] | 3629 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3630 | ASSERT_COMPARE(keys.client_write_key, |
| 3631 | keys.key_len, |
| 3632 | expected_client_write_key->x, |
| 3633 | (size_t) desired_key_len); |
| 3634 | ASSERT_COMPARE(keys.server_write_key, |
| 3635 | keys.key_len, |
| 3636 | expected_server_write_key->x, |
| 3637 | (size_t) desired_key_len); |
| 3638 | ASSERT_COMPARE(keys.client_write_iv, |
| 3639 | keys.iv_len, |
| 3640 | expected_client_write_iv->x, |
| 3641 | (size_t) desired_iv_len); |
| 3642 | ASSERT_COMPARE(keys.server_write_iv, |
| 3643 | keys.iv_len, |
| 3644 | expected_server_write_iv->x, |
| 3645 | (size_t) desired_iv_len); |
Hanno Becker | 19498f8 | 2020-08-21 13:37:08 +0100 | [diff] [blame] | 3646 | } |
| 3647 | /* END_CASE */ |
| 3648 | |
Hanno Becker | e4849d1 | 2020-08-21 14:14:14 +0100 | [diff] [blame] | 3649 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3650 | void ssl_tls1_3_derive_secret(int hash_alg, |
| 3651 | data_t *secret, |
| 3652 | int label_idx, |
| 3653 | data_t *ctx, |
| 3654 | int desired_length, |
| 3655 | int already_hashed, |
| 3656 | data_t *expected) |
Hanno Becker | e4849d1 | 2020-08-21 14:14:14 +0100 | [diff] [blame] | 3657 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3658 | unsigned char dst[100]; |
Hanno Becker | e4849d1 | 2020-08-21 14:14:14 +0100 | [diff] [blame] | 3659 | |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 3660 | unsigned char const *lbl = NULL; |
| 3661 | size_t lbl_len; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3662 | #define MBEDTLS_SSL_TLS1_3_LABEL(name, string) \ |
| 3663 | if (label_idx == (int) tls1_3_label_ ## name) \ |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 3664 | { \ |
| 3665 | lbl = mbedtls_ssl_tls1_3_labels.name; \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3666 | lbl_len = sizeof(mbedtls_ssl_tls1_3_labels.name); \ |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 3667 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3668 | MBEDTLS_SSL_TLS1_3_LABEL_LIST |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 3669 | #undef MBEDTLS_SSL_TLS1_3_LABEL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3670 | TEST_ASSERT(lbl != NULL); |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 3671 | |
Hanno Becker | e4849d1 | 2020-08-21 14:14:14 +0100 | [diff] [blame] | 3672 | /* Check sanity of test parameters. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3673 | TEST_ASSERT((size_t) desired_length <= sizeof(dst)); |
| 3674 | TEST_ASSERT((size_t) desired_length == expected->len); |
Hanno Becker | e4849d1 | 2020-08-21 14:14:14 +0100 | [diff] [blame] | 3675 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3676 | TEST_ASSERT(mbedtls_ssl_tls1_3_derive_secret( |
| 3677 | (mbedtls_md_type_t) hash_alg, |
| 3678 | secret->x, secret->len, |
| 3679 | lbl, lbl_len, |
| 3680 | ctx->x, ctx->len, |
| 3681 | already_hashed, |
| 3682 | dst, desired_length) == 0); |
Hanno Becker | e4849d1 | 2020-08-21 14:14:14 +0100 | [diff] [blame] | 3683 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3684 | ASSERT_COMPARE(dst, desired_length, |
| 3685 | expected->x, desired_length); |
Hanno Becker | e4849d1 | 2020-08-21 14:14:14 +0100 | [diff] [blame] | 3686 | } |
| 3687 | /* END_CASE */ |
| 3688 | |
Hanno Becker | 2d2c3eb | 2020-08-20 14:54:24 +0100 | [diff] [blame] | 3689 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3690 | void ssl_tls1_3_key_evolution(int hash_alg, |
| 3691 | data_t *secret, |
| 3692 | data_t *input, |
| 3693 | data_t *expected) |
Hanno Becker | 2d2c3eb | 2020-08-20 14:54:24 +0100 | [diff] [blame] | 3694 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3695 | unsigned char secret_new[MBEDTLS_MD_MAX_SIZE]; |
Hanno Becker | 2d2c3eb | 2020-08-20 14:54:24 +0100 | [diff] [blame] | 3696 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3697 | TEST_ASSERT(mbedtls_ssl_tls1_3_evolve_secret( |
| 3698 | (mbedtls_md_type_t) hash_alg, |
| 3699 | secret->len ? secret->x : NULL, |
| 3700 | input->len ? input->x : NULL, input->len, |
| 3701 | secret_new) == 0); |
Hanno Becker | 2d2c3eb | 2020-08-20 14:54:24 +0100 | [diff] [blame] | 3702 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3703 | ASSERT_COMPARE(secret_new, (size_t) expected->len, |
| 3704 | expected->x, (size_t) expected->len); |
Hanno Becker | 2d2c3eb | 2020-08-20 14:54:24 +0100 | [diff] [blame] | 3705 | } |
| 3706 | /* END_CASE */ |
| 3707 | |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 3708 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3709 | void ssl_tls_prf(int type, data_t *secret, data_t *random, |
| 3710 | char *label, data_t *result_str, int exp_ret) |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 3711 | { |
| 3712 | unsigned char *output; |
| 3713 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3714 | output = mbedtls_calloc(1, result_str->len); |
| 3715 | if (output == NULL) { |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 3716 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3717 | } |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 3718 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3719 | USE_PSA_INIT(); |
Ron Eldor | 6b9b1b8 | 2019-05-15 17:04:33 +0300 | [diff] [blame] | 3720 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3721 | TEST_ASSERT(mbedtls_ssl_tls_prf(type, secret->x, secret->len, |
| 3722 | label, random->x, random->len, |
| 3723 | output, result_str->len) == exp_ret); |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 3724 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3725 | if (exp_ret == 0) { |
| 3726 | TEST_ASSERT(mbedtls_test_hexcmp(output, result_str->x, |
| 3727 | result_str->len, result_str->len) == 0); |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 3728 | } |
| 3729 | exit: |
| 3730 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3731 | mbedtls_free(output); |
| 3732 | USE_PSA_DONE(); |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 3733 | } |
| 3734 | /* END_CASE */ |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 3735 | |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 3736 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3737 | void ssl_serialize_session_save_load(int ticket_len, char *crt_file) |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 3738 | { |
| 3739 | mbedtls_ssl_session original, restored; |
| 3740 | unsigned char *buf = NULL; |
| 3741 | size_t len; |
| 3742 | |
| 3743 | /* |
| 3744 | * Test that a save-load pair is the identity |
| 3745 | */ |
| 3746 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3747 | mbedtls_ssl_session_init(&original); |
| 3748 | mbedtls_ssl_session_init(&restored); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 3749 | |
| 3750 | /* Prepare a dummy session to work on */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3751 | TEST_ASSERT(ssl_populate_session(&original, ticket_len, crt_file) == 0); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 3752 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 3753 | /* Serialize it */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3754 | TEST_ASSERT(mbedtls_ssl_session_save(&original, NULL, 0, &len) |
| 3755 | == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL); |
| 3756 | TEST_ASSERT((buf = mbedtls_calloc(1, len)) != NULL); |
| 3757 | TEST_ASSERT(mbedtls_ssl_session_save(&original, buf, len, &len) |
| 3758 | == 0); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 3759 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 3760 | /* Restore session from serialized data */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3761 | TEST_ASSERT(mbedtls_ssl_session_load(&restored, buf, len) == 0); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 3762 | |
| 3763 | /* |
| 3764 | * Make sure both session structures are identical |
| 3765 | */ |
| 3766 | #if defined(MBEDTLS_HAVE_TIME) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3767 | TEST_ASSERT(original.start == restored.start); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 3768 | #endif |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3769 | TEST_ASSERT(original.ciphersuite == restored.ciphersuite); |
| 3770 | TEST_ASSERT(original.compression == restored.compression); |
| 3771 | TEST_ASSERT(original.id_len == restored.id_len); |
| 3772 | TEST_ASSERT(memcmp(original.id, |
| 3773 | restored.id, sizeof(original.id)) == 0); |
| 3774 | TEST_ASSERT(memcmp(original.master, |
| 3775 | restored.master, sizeof(original.master)) == 0); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 3776 | |
Gilles Peskine | 33d03fe | 2023-02-01 18:37:49 +0100 | [diff] [blame^] | 3777 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) && \ |
| 3778 | defined(MBEDTLS_CERTS_C) |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 3779 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3780 | TEST_ASSERT((original.peer_cert == NULL) == |
| 3781 | (restored.peer_cert == NULL)); |
| 3782 | if (original.peer_cert != NULL) { |
| 3783 | TEST_ASSERT(original.peer_cert->raw.len == |
| 3784 | restored.peer_cert->raw.len); |
| 3785 | TEST_ASSERT(memcmp(original.peer_cert->raw.p, |
| 3786 | restored.peer_cert->raw.p, |
| 3787 | original.peer_cert->raw.len) == 0); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 3788 | } |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 3789 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3790 | TEST_ASSERT(original.peer_cert_digest_type == |
| 3791 | restored.peer_cert_digest_type); |
| 3792 | TEST_ASSERT(original.peer_cert_digest_len == |
| 3793 | restored.peer_cert_digest_len); |
| 3794 | TEST_ASSERT((original.peer_cert_digest == NULL) == |
| 3795 | (restored.peer_cert_digest == NULL)); |
| 3796 | if (original.peer_cert_digest != NULL) { |
| 3797 | TEST_ASSERT(memcmp(original.peer_cert_digest, |
| 3798 | restored.peer_cert_digest, |
| 3799 | original.peer_cert_digest_len) == 0); |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 3800 | } |
| 3801 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Gilles Peskine | 33d03fe | 2023-02-01 18:37:49 +0100 | [diff] [blame^] | 3802 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED && MBEDTLS_CERTS_C */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3803 | TEST_ASSERT(original.verify_result == restored.verify_result); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 3804 | |
| 3805 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3806 | TEST_ASSERT(original.ticket_len == restored.ticket_len); |
| 3807 | if (original.ticket_len != 0) { |
| 3808 | TEST_ASSERT(original.ticket != NULL); |
| 3809 | TEST_ASSERT(restored.ticket != NULL); |
| 3810 | TEST_ASSERT(memcmp(original.ticket, |
| 3811 | restored.ticket, original.ticket_len) == 0); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 3812 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3813 | TEST_ASSERT(original.ticket_lifetime == restored.ticket_lifetime); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 3814 | #endif |
| 3815 | |
| 3816 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3817 | TEST_ASSERT(original.mfl_code == restored.mfl_code); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 3818 | #endif |
| 3819 | |
| 3820 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3821 | TEST_ASSERT(original.trunc_hmac == restored.trunc_hmac); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 3822 | #endif |
| 3823 | |
| 3824 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3825 | TEST_ASSERT(original.encrypt_then_mac == restored.encrypt_then_mac); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 3826 | #endif |
| 3827 | |
| 3828 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3829 | mbedtls_ssl_session_free(&original); |
| 3830 | mbedtls_ssl_session_free(&restored); |
| 3831 | mbedtls_free(buf); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 3832 | } |
| 3833 | /* END_CASE */ |
| 3834 | |
Manuel Pégourié-Gonnard | aa75583 | 2019-06-03 10:53:47 +0200 | [diff] [blame] | 3835 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3836 | void ssl_serialize_session_load_save(int ticket_len, char *crt_file) |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 3837 | { |
| 3838 | mbedtls_ssl_session session; |
| 3839 | unsigned char *buf1 = NULL, *buf2 = NULL; |
| 3840 | size_t len0, len1, len2; |
| 3841 | |
| 3842 | /* |
| 3843 | * Test that a load-save pair is the identity |
| 3844 | */ |
| 3845 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3846 | mbedtls_ssl_session_init(&session); |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 3847 | |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 3848 | /* Prepare a dummy session to work on */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3849 | TEST_ASSERT(ssl_populate_session(&session, ticket_len, crt_file) == 0); |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 3850 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 3851 | /* Get desired buffer size for serializing */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3852 | TEST_ASSERT(mbedtls_ssl_session_save(&session, NULL, 0, &len0) |
| 3853 | == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL); |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 3854 | |
| 3855 | /* Allocate first buffer */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3856 | buf1 = mbedtls_calloc(1, len0); |
| 3857 | TEST_ASSERT(buf1 != NULL); |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 3858 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 3859 | /* Serialize to buffer and free live session */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3860 | TEST_ASSERT(mbedtls_ssl_session_save(&session, buf1, len0, &len1) |
| 3861 | == 0); |
| 3862 | TEST_ASSERT(len0 == len1); |
| 3863 | mbedtls_ssl_session_free(&session); |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 3864 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 3865 | /* Restore session from serialized data */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3866 | TEST_ASSERT(mbedtls_ssl_session_load(&session, buf1, len1) == 0); |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 3867 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 3868 | /* Allocate second buffer and serialize to it */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3869 | buf2 = mbedtls_calloc(1, len0); |
| 3870 | TEST_ASSERT(buf2 != NULL); |
| 3871 | TEST_ASSERT(mbedtls_ssl_session_save(&session, buf2, len0, &len2) |
| 3872 | == 0); |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 3873 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 3874 | /* Make sure both serialized versions are identical */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3875 | TEST_ASSERT(len1 == len2); |
| 3876 | TEST_ASSERT(memcmp(buf1, buf2, len1) == 0); |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 3877 | |
| 3878 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3879 | mbedtls_ssl_session_free(&session); |
| 3880 | mbedtls_free(buf1); |
| 3881 | mbedtls_free(buf2); |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 3882 | } |
| 3883 | /* END_CASE */ |
Manuel Pégourié-Gonnard | f5fa0aa | 2019-05-23 10:38:11 +0200 | [diff] [blame] | 3884 | |
| 3885 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3886 | void ssl_serialize_session_save_buf_size(int ticket_len, char *crt_file) |
Manuel Pégourié-Gonnard | f5fa0aa | 2019-05-23 10:38:11 +0200 | [diff] [blame] | 3887 | { |
| 3888 | mbedtls_ssl_session session; |
| 3889 | unsigned char *buf = NULL; |
| 3890 | size_t good_len, bad_len, test_len; |
| 3891 | |
| 3892 | /* |
| 3893 | * Test that session_save() fails cleanly on small buffers |
| 3894 | */ |
| 3895 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3896 | mbedtls_ssl_session_init(&session); |
Manuel Pégourié-Gonnard | f5fa0aa | 2019-05-23 10:38:11 +0200 | [diff] [blame] | 3897 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 3898 | /* Prepare dummy session and get serialized size */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3899 | TEST_ASSERT(ssl_populate_session(&session, ticket_len, crt_file) == 0); |
| 3900 | TEST_ASSERT(mbedtls_ssl_session_save(&session, NULL, 0, &good_len) |
| 3901 | == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL); |
Manuel Pégourié-Gonnard | f5fa0aa | 2019-05-23 10:38:11 +0200 | [diff] [blame] | 3902 | |
| 3903 | /* Try all possible bad lengths */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3904 | for (bad_len = 1; bad_len < good_len; bad_len++) { |
Manuel Pégourié-Gonnard | f5fa0aa | 2019-05-23 10:38:11 +0200 | [diff] [blame] | 3905 | /* Allocate exact size so that asan/valgrind can detect any overwrite */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3906 | mbedtls_free(buf); |
| 3907 | TEST_ASSERT((buf = mbedtls_calloc(1, bad_len)) != NULL); |
| 3908 | TEST_ASSERT(mbedtls_ssl_session_save(&session, buf, bad_len, |
| 3909 | &test_len) |
| 3910 | == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL); |
| 3911 | TEST_ASSERT(test_len == good_len); |
Manuel Pégourié-Gonnard | f5fa0aa | 2019-05-23 10:38:11 +0200 | [diff] [blame] | 3912 | } |
| 3913 | |
| 3914 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3915 | mbedtls_ssl_session_free(&session); |
| 3916 | mbedtls_free(buf); |
Manuel Pégourié-Gonnard | f5fa0aa | 2019-05-23 10:38:11 +0200 | [diff] [blame] | 3917 | } |
| 3918 | /* END_CASE */ |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 3919 | |
| 3920 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3921 | void ssl_serialize_session_load_buf_size(int ticket_len, char *crt_file) |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 3922 | { |
| 3923 | mbedtls_ssl_session session; |
| 3924 | unsigned char *good_buf = NULL, *bad_buf = NULL; |
| 3925 | size_t good_len, bad_len; |
| 3926 | |
| 3927 | /* |
| 3928 | * Test that session_load() fails cleanly on small buffers |
| 3929 | */ |
| 3930 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3931 | mbedtls_ssl_session_init(&session); |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 3932 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 3933 | /* Prepare serialized session data */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3934 | TEST_ASSERT(ssl_populate_session(&session, ticket_len, crt_file) == 0); |
| 3935 | TEST_ASSERT(mbedtls_ssl_session_save(&session, NULL, 0, &good_len) |
| 3936 | == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL); |
| 3937 | TEST_ASSERT((good_buf = mbedtls_calloc(1, good_len)) != NULL); |
| 3938 | TEST_ASSERT(mbedtls_ssl_session_save(&session, good_buf, good_len, |
| 3939 | &good_len) == 0); |
| 3940 | mbedtls_ssl_session_free(&session); |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 3941 | |
| 3942 | /* Try all possible bad lengths */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3943 | for (bad_len = 0; bad_len < good_len; bad_len++) { |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 3944 | /* Allocate exact size so that asan/valgrind can detect any overread */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3945 | mbedtls_free(bad_buf); |
| 3946 | bad_buf = mbedtls_calloc(1, bad_len ? bad_len : 1); |
| 3947 | TEST_ASSERT(bad_buf != NULL); |
| 3948 | memcpy(bad_buf, good_buf, bad_len); |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 3949 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3950 | TEST_ASSERT(mbedtls_ssl_session_load(&session, bad_buf, bad_len) |
| 3951 | == MBEDTLS_ERR_SSL_BAD_INPUT_DATA); |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 3952 | } |
| 3953 | |
| 3954 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3955 | mbedtls_ssl_session_free(&session); |
| 3956 | mbedtls_free(good_buf); |
| 3957 | mbedtls_free(bad_buf); |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 3958 | } |
| 3959 | /* END_CASE */ |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 3960 | |
Hanno Becker | 363b646 | 2019-05-29 12:44:28 +0100 | [diff] [blame] | 3961 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3962 | void ssl_session_serialize_version_check(int corrupt_major, |
| 3963 | int corrupt_minor, |
| 3964 | int corrupt_patch, |
| 3965 | int corrupt_config) |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 3966 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3967 | unsigned char serialized_session[2048]; |
Hanno Becker | 363b646 | 2019-05-29 12:44:28 +0100 | [diff] [blame] | 3968 | size_t serialized_session_len; |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 3969 | unsigned cur_byte; |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 3970 | mbedtls_ssl_session session; |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 3971 | uint8_t should_corrupt_byte[] = { corrupt_major == 1, |
| 3972 | corrupt_minor == 1, |
| 3973 | corrupt_patch == 1, |
| 3974 | corrupt_config == 1, |
| 3975 | corrupt_config == 1 }; |
| 3976 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3977 | mbedtls_ssl_session_init(&session); |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 3978 | |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 3979 | /* Infer length of serialized session. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3980 | TEST_ASSERT(mbedtls_ssl_session_save(&session, |
| 3981 | serialized_session, |
| 3982 | sizeof(serialized_session), |
| 3983 | &serialized_session_len) == 0); |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 3984 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3985 | mbedtls_ssl_session_free(&session); |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 3986 | |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 3987 | /* Without any modification, we should be able to successfully |
Hanno Becker | 363b646 | 2019-05-29 12:44:28 +0100 | [diff] [blame] | 3988 | * de-serialize the session - double-check that. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3989 | TEST_ASSERT(mbedtls_ssl_session_load(&session, |
| 3990 | serialized_session, |
| 3991 | serialized_session_len) == 0); |
| 3992 | mbedtls_ssl_session_free(&session); |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 3993 | |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 3994 | /* Go through the bytes in the serialized session header and |
| 3995 | * corrupt them bit-by-bit. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3996 | for (cur_byte = 0; cur_byte < sizeof(should_corrupt_byte); cur_byte++) { |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 3997 | int cur_bit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3998 | unsigned char * const byte = &serialized_session[cur_byte]; |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 3999 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4000 | if (should_corrupt_byte[cur_byte] == 0) { |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 4001 | continue; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4002 | } |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 4003 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4004 | for (cur_bit = 0; cur_bit < CHAR_BIT; cur_bit++) { |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 4005 | unsigned char const corrupted_bit = 0x1u << cur_bit; |
| 4006 | /* Modify a single bit in the serialized session. */ |
| 4007 | *byte ^= corrupted_bit; |
| 4008 | |
| 4009 | /* Attempt to deserialize */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4010 | TEST_ASSERT(mbedtls_ssl_session_load(&session, |
| 4011 | serialized_session, |
| 4012 | serialized_session_len) == |
| 4013 | MBEDTLS_ERR_SSL_VERSION_MISMATCH); |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 4014 | |
| 4015 | /* Undo the change */ |
| 4016 | *byte ^= corrupted_bit; |
| 4017 | } |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 4018 | } |
| 4019 | |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 4020 | } |
| 4021 | /* END_CASE */ |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4022 | |
Gilles Peskine | 33d03fe | 2023-02-01 18:37:49 +0100 | [diff] [blame^] | 4023 | /* BEGIN_CASE depends_on:MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED:MBEDTLS_CERTS_C:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_ENTROPY_C:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C:MBEDTLS_SHA256_C */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4024 | void mbedtls_endpoint_sanity(int endpoint_type) |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4025 | { |
| 4026 | enum { BUFFSIZE = 1024 }; |
| 4027 | mbedtls_endpoint ep; |
| 4028 | int ret = -1; |
| 4029 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4030 | ret = mbedtls_endpoint_init(NULL, endpoint_type, MBEDTLS_PK_RSA, |
| 4031 | NULL, NULL, NULL, NULL); |
| 4032 | TEST_ASSERT(MBEDTLS_ERR_SSL_BAD_INPUT_DATA == ret); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4033 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4034 | ret = mbedtls_endpoint_certificate_init(NULL, MBEDTLS_PK_RSA); |
| 4035 | TEST_ASSERT(MBEDTLS_ERR_SSL_BAD_INPUT_DATA == ret); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4036 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4037 | ret = mbedtls_endpoint_init(&ep, endpoint_type, MBEDTLS_PK_RSA, |
| 4038 | NULL, NULL, NULL, NULL); |
| 4039 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4040 | |
| 4041 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4042 | mbedtls_endpoint_free(&ep, NULL); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4043 | } |
| 4044 | /* END_CASE */ |
| 4045 | |
Gilles Peskine | 33d03fe | 2023-02-01 18:37:49 +0100 | [diff] [blame^] | 4046 | /* BEGIN_CASE depends_on:MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED:MBEDTLS_CERTS_C:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_ENTROPY_C:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C:MBEDTLS_ECP_C:MBEDTLS_SHA256_C */ |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4047 | void move_handshake_to_state(int endpoint_type, int state, int need_pass) |
| 4048 | { |
| 4049 | enum { BUFFSIZE = 1024 }; |
| 4050 | mbedtls_endpoint base_ep, second_ep; |
| 4051 | int ret = -1; |
| 4052 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4053 | mbedtls_platform_zeroize(&base_ep, sizeof(base_ep)); |
| 4054 | mbedtls_platform_zeroize(&second_ep, sizeof(second_ep)); |
Andrzej Kurek | 0d2982b | 2022-10-18 07:55:46 -0400 | [diff] [blame] | 4055 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4056 | ret = mbedtls_endpoint_init(&base_ep, endpoint_type, MBEDTLS_PK_RSA, |
| 4057 | NULL, NULL, NULL, NULL); |
| 4058 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4059 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4060 | ret = mbedtls_endpoint_init(&second_ep, |
| 4061 | (endpoint_type == MBEDTLS_SSL_IS_SERVER) ? |
| 4062 | MBEDTLS_SSL_IS_CLIENT : MBEDTLS_SSL_IS_SERVER, |
| 4063 | MBEDTLS_PK_RSA, NULL, NULL, NULL, NULL); |
| 4064 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4065 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4066 | ret = mbedtls_mock_socket_connect(&(base_ep.socket), |
| 4067 | &(second_ep.socket), |
| 4068 | BUFFSIZE); |
| 4069 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4070 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4071 | ret = mbedtls_move_handshake_to_state(&(base_ep.ssl), |
| 4072 | &(second_ep.ssl), |
| 4073 | state); |
| 4074 | if (need_pass) { |
| 4075 | TEST_ASSERT(ret == 0); |
| 4076 | TEST_ASSERT(base_ep.ssl.state == state); |
| 4077 | } else { |
| 4078 | TEST_ASSERT(ret != 0); |
| 4079 | TEST_ASSERT(base_ep.ssl.state != state); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4080 | } |
| 4081 | |
| 4082 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4083 | mbedtls_endpoint_free(&base_ep, NULL); |
| 4084 | mbedtls_endpoint_free(&second_ep, NULL); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4085 | } |
| 4086 | /* END_CASE */ |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 4087 | |
Gilles Peskine | 33d03fe | 2023-02-01 18:37:49 +0100 | [diff] [blame^] | 4088 | /* BEGIN_CASE depends_on:MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED:MBEDTLS_CERTS_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C:MBEDTLS_ECP_C:MBEDTLS_SHA256_C:MBEDTLS_CAN_HANDLE_RSA_TEST_KEY */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4089 | void handshake_version(int dtls, int client_min_version, int client_max_version, |
| 4090 | int server_min_version, int server_max_version, |
| 4091 | int expected_negotiated_version) |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 4092 | { |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 4093 | handshake_test_options options; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4094 | init_handshake_options(&options); |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 4095 | |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 4096 | options.client_min_version = client_min_version; |
| 4097 | options.client_max_version = client_max_version; |
| 4098 | options.server_min_version = server_min_version; |
| 4099 | options.server_max_version = server_max_version; |
| 4100 | |
| 4101 | options.expected_negotiated_version = expected_negotiated_version; |
| 4102 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 4103 | options.dtls = dtls; |
Piotr Nowicki | 438bf3b | 2020-03-10 12:59:10 +0100 | [diff] [blame] | 4104 | /* By default, SSLv3.0 and TLSv1.0 use 1/n-1 splitting when sending data, so |
| 4105 | * the number of fragments will be twice as big. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4106 | if (expected_negotiated_version == MBEDTLS_SSL_MINOR_VERSION_0 || |
| 4107 | expected_negotiated_version == MBEDTLS_SSL_MINOR_VERSION_1) { |
Piotr Nowicki | 438bf3b | 2020-03-10 12:59:10 +0100 | [diff] [blame] | 4108 | options.expected_cli_fragments = 2; |
| 4109 | options.expected_srv_fragments = 2; |
Andrzej Kurek | 941962e | 2020-02-07 09:20:32 -0500 | [diff] [blame] | 4110 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4111 | perform_handshake(&options); |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 4112 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 4113 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 4114 | goto exit; |
| 4115 | } |
| 4116 | /* END_CASE */ |
Andrzej Kurek | 9e9efdc | 2020-02-26 05:25:23 -0500 | [diff] [blame] | 4117 | |
Gilles Peskine | 33d03fe | 2023-02-01 18:37:49 +0100 | [diff] [blame^] | 4118 | /* BEGIN_CASE depends_on:MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED:MBEDTLS_CERTS_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C:MBEDTLS_SHA256_C */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4119 | 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] | 4120 | { |
| 4121 | handshake_test_options options; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4122 | init_handshake_options(&options); |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 4123 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 4124 | options.cipher = cipher; |
| 4125 | options.dtls = dtls; |
| 4126 | options.psk_str = psk_str; |
| 4127 | options.pk_alg = pk_alg; |
Andrzej Kurek | cc5169c | 2020-02-04 09:04:56 -0500 | [diff] [blame] | 4128 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4129 | perform_handshake(&options); |
Andrzej Kurek | 316da1f | 2020-02-26 09:03:47 -0500 | [diff] [blame] | 4130 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 4131 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 4132 | goto exit; |
| 4133 | } |
| 4134 | /* END_CASE */ |
Andrzej Kurek | 316da1f | 2020-02-26 09:03:47 -0500 | [diff] [blame] | 4135 | |
Gilles Peskine | 33d03fe | 2023-02-01 18:37:49 +0100 | [diff] [blame^] | 4136 | /* BEGIN_CASE depends_on:MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED:MBEDTLS_CERTS_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C:MBEDTLS_SHA256_C */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4137 | void handshake_cipher(char *cipher, int pk_alg, int dtls) |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 4138 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4139 | test_handshake_psk_cipher(cipher, pk_alg, NULL, dtls); |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 4140 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 4141 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 4142 | goto exit; |
| 4143 | } |
| 4144 | /* END_CASE */ |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 4145 | |
Gilles Peskine | 33d03fe | 2023-02-01 18:37:49 +0100 | [diff] [blame^] | 4146 | /* BEGIN_CASE depends_on:MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED:MBEDTLS_CERTS_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C:MBEDTLS_SHA256_C */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4147 | void app_data(int mfl, int cli_msg_len, int srv_msg_len, |
| 4148 | int expected_cli_fragments, |
| 4149 | int expected_srv_fragments, int dtls) |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 4150 | { |
| 4151 | handshake_test_options options; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4152 | init_handshake_options(&options); |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 4153 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 4154 | options.mfl = mfl; |
| 4155 | options.cli_msg_len = cli_msg_len; |
| 4156 | options.srv_msg_len = srv_msg_len; |
| 4157 | options.expected_cli_fragments = expected_cli_fragments; |
| 4158 | options.expected_srv_fragments = expected_srv_fragments; |
| 4159 | options.dtls = dtls; |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 4160 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4161 | perform_handshake(&options); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 4162 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 4163 | goto exit; |
| 4164 | } |
| 4165 | /* END_CASE */ |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 4166 | |
Gilles Peskine | 33d03fe | 2023-02-01 18:37:49 +0100 | [diff] [blame^] | 4167 | /* BEGIN_CASE depends_on:MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED:MBEDTLS_CERTS_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C:MBEDTLS_ECP_C:MBEDTLS_SHA256_C:MBEDTLS_CAN_HANDLE_RSA_TEST_KEY */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4168 | void app_data_tls(int mfl, int cli_msg_len, int srv_msg_len, |
| 4169 | int expected_cli_fragments, |
| 4170 | int expected_srv_fragments) |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 4171 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4172 | test_app_data(mfl, cli_msg_len, srv_msg_len, expected_cli_fragments, |
| 4173 | expected_srv_fragments, 0); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 4174 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 4175 | goto exit; |
| 4176 | } |
| 4177 | /* END_CASE */ |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 4178 | |
Gilles Peskine | 33d03fe | 2023-02-01 18:37:49 +0100 | [diff] [blame^] | 4179 | /* BEGIN_CASE depends_on:MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED:MBEDTLS_CERTS_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_SSL_PROTO_DTLS:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C:MBEDTLS_SHA256_C:MBEDTLS_CAN_HANDLE_RSA_TEST_KEY */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4180 | void app_data_dtls(int mfl, int cli_msg_len, int srv_msg_len, |
| 4181 | int expected_cli_fragments, |
| 4182 | int expected_srv_fragments) |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 4183 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4184 | test_app_data(mfl, cli_msg_len, srv_msg_len, expected_cli_fragments, |
| 4185 | expected_srv_fragments, 1); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 4186 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 4187 | goto exit; |
| 4188 | } |
| 4189 | /* END_CASE */ |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 4190 | |
Gilles Peskine | 33d03fe | 2023-02-01 18:37:49 +0100 | [diff] [blame^] | 4191 | /* BEGIN_CASE depends_on:MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED:MBEDTLS_CERTS_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_SSL_PROTO_DTLS:MBEDTLS_SSL_RENEGOTIATION:MBEDTLS_SSL_CONTEXT_SERIALIZATION:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C:MBEDTLS_SHA256_C:MBEDTLS_CAN_HANDLE_RSA_TEST_KEY */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4192 | void handshake_serialization() |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 4193 | { |
| 4194 | handshake_test_options options; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4195 | init_handshake_options(&options); |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 4196 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 4197 | options.serialize = 1; |
| 4198 | options.dtls = 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4199 | perform_handshake(&options); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 4200 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 4201 | goto exit; |
| 4202 | } |
| 4203 | /* END_CASE */ |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 4204 | |
Gilles Peskine | 33d03fe | 2023-02-01 18:37:49 +0100 | [diff] [blame^] | 4205 | /* BEGIN_CASE depends_on:MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED:MBEDTLS_CERTS_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_DEBUG_C:MBEDTLS_SSL_MAX_FRAGMENT_LENGTH:MBEDTLS_CIPHER_MODE_CBC:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C:MBEDTLS_SHA256_C:MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED*/ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4206 | void handshake_fragmentation(int mfl, |
| 4207 | int expected_srv_hs_fragmentation, |
| 4208 | int expected_cli_hs_fragmentation) |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 4209 | { |
| 4210 | handshake_test_options options; |
| 4211 | log_pattern srv_pattern, cli_pattern; |
| 4212 | |
| 4213 | srv_pattern.pattern = cli_pattern.pattern = "found fragmented DTLS handshake"; |
| 4214 | srv_pattern.counter = 0; |
| 4215 | cli_pattern.counter = 0; |
| 4216 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4217 | init_handshake_options(&options); |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 4218 | options.dtls = 1; |
| 4219 | options.mfl = mfl; |
Darryl Green | aad82f9 | 2019-12-02 10:53:11 +0000 | [diff] [blame] | 4220 | /* Set cipher to one using CBC so that record splitting can be tested */ |
| 4221 | options.cipher = "TLS-DHE-RSA-WITH-AES-256-CBC-SHA256"; |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 4222 | options.srv_auth_mode = MBEDTLS_SSL_VERIFY_REQUIRED; |
| 4223 | options.srv_log_obj = &srv_pattern; |
| 4224 | options.cli_log_obj = &cli_pattern; |
| 4225 | options.srv_log_fun = log_analyzer; |
| 4226 | options.cli_log_fun = log_analyzer; |
| 4227 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4228 | perform_handshake(&options); |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 4229 | |
| 4230 | /* Test if the server received a fragmented handshake */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4231 | if (expected_srv_hs_fragmentation) { |
| 4232 | TEST_ASSERT(srv_pattern.counter >= 1); |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 4233 | } |
| 4234 | /* Test if the client received a fragmented handshake */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4235 | if (expected_cli_hs_fragmentation) { |
| 4236 | TEST_ASSERT(cli_pattern.counter >= 1); |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 4237 | } |
| 4238 | } |
| 4239 | /* END_CASE */ |
| 4240 | |
Gilles Peskine | 33d03fe | 2023-02-01 18:37:49 +0100 | [diff] [blame^] | 4241 | /* BEGIN_CASE depends_on:MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED:MBEDTLS_CERTS_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_SSL_PROTO_DTLS:MBEDTLS_SSL_RENEGOTIATION:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C:MBEDTLS_SHA256_C:MBEDTLS_CAN_HANDLE_RSA_TEST_KEY */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4242 | void renegotiation(int legacy_renegotiation) |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 4243 | { |
| 4244 | handshake_test_options options; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4245 | init_handshake_options(&options); |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 4246 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 4247 | options.renegotiate = 1; |
| 4248 | options.legacy_renegotiation = legacy_renegotiation; |
| 4249 | options.dtls = 1; |
Andrzej Kurek | 316da1f | 2020-02-26 09:03:47 -0500 | [diff] [blame] | 4250 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4251 | perform_handshake(&options); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 4252 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 4253 | goto exit; |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 4254 | } |
| 4255 | /* END_CASE */ |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 4256 | |
Gilles Peskine | 33d03fe | 2023-02-01 18:37:49 +0100 | [diff] [blame^] | 4257 | /* BEGIN_CASE depends_on:MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED:MBEDTLS_CERTS_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C:MBEDTLS_SHA256_C */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4258 | void resize_buffers(int mfl, int renegotiation, int legacy_renegotiation, |
| 4259 | int serialize, int dtls, char *cipher) |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 4260 | { |
| 4261 | handshake_test_options options; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4262 | init_handshake_options(&options); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 4263 | |
| 4264 | options.mfl = mfl; |
Andrzej Kurek | 8ea6872 | 2020-04-03 06:40:47 -0400 | [diff] [blame] | 4265 | options.cipher = cipher; |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 4266 | options.renegotiate = renegotiation; |
| 4267 | options.legacy_renegotiation = legacy_renegotiation; |
| 4268 | options.serialize = serialize; |
| 4269 | options.dtls = dtls; |
| 4270 | options.resize_buffers = 1; |
| 4271 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4272 | perform_handshake(&options); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 4273 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 4274 | goto exit; |
| 4275 | } |
| 4276 | /* END_CASE */ |
| 4277 | |
Gilles Peskine | 33d03fe | 2023-02-01 18:37:49 +0100 | [diff] [blame^] | 4278 | /* BEGIN_CASE depends_on:MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED:MBEDTLS_CERTS_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH:MBEDTLS_SSL_CONTEXT_SERIALIZATION:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_SSL_PROTO_DTLS:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C:MBEDTLS_SHA256_C:MBEDTLS_CAN_HANDLE_RSA_TEST_KEY */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4279 | void resize_buffers_serialize_mfl(int mfl) |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 4280 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4281 | test_resize_buffers(mfl, 0, MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION, 1, 1, |
| 4282 | (char *) ""); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 4283 | |
| 4284 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 4285 | goto exit; |
| 4286 | } |
| 4287 | /* END_CASE */ |
| 4288 | |
Gilles Peskine | 33d03fe | 2023-02-01 18:37:49 +0100 | [diff] [blame^] | 4289 | /* BEGIN_CASE depends_on:MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED:MBEDTLS_CERTS_C:!MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C:MBEDTLS_SHA256_C:MBEDTLS_SSL_RENEGOTIATION:MBEDTLS_CAN_HANDLE_RSA_TEST_KEY */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4290 | void resize_buffers_renegotiate_mfl(int mfl, int legacy_renegotiation, |
| 4291 | char *cipher) |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 4292 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4293 | test_resize_buffers(mfl, 1, legacy_renegotiation, 0, 1, cipher); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 4294 | |
| 4295 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 4296 | goto exit; |
| 4297 | } |
| 4298 | /* END_CASE */ |
Manuel Pégourié-Gonnard | 045f094 | 2020-07-02 11:34:02 +0200 | [diff] [blame] | 4299 | |
Gilles Peskine | 33d03fe | 2023-02-01 18:37:49 +0100 | [diff] [blame^] | 4300 | /* BEGIN_CASE depends_on:MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED:MBEDTLS_CERTS_C:MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_ECP_DP_SECP256R1_ENABLED:MBEDTLS_ENTROPY_C:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_CTR_DRBG_C:MBEDTLS_ECP_C:MBEDTLS_ECDSA_C */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4301 | void raw_key_agreement_fail(int bad_server_ecdhe_key) |
Andrzej Kurek | b4eedf7 | 2022-04-15 05:41:14 -0400 | [diff] [blame] | 4302 | { |
| 4303 | enum { BUFFSIZE = 17000 }; |
| 4304 | mbedtls_endpoint client, server; |
| 4305 | mbedtls_psa_stats_t stats; |
Andrzej Kurek | 2582ba3 | 2022-03-31 06:30:54 -0400 | [diff] [blame] | 4306 | size_t free_slots_before = -1; |
Andrzej Kurek | b4eedf7 | 2022-04-15 05:41:14 -0400 | [diff] [blame] | 4307 | |
Andrzej Kurek | b4eedf7 | 2022-04-15 05:41:14 -0400 | [diff] [blame] | 4308 | mbedtls_ecp_group_id curve_list[] = { MBEDTLS_ECP_DP_SECP256R1, |
| 4309 | MBEDTLS_ECP_DP_NONE }; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4310 | USE_PSA_INIT(); |
| 4311 | mbedtls_platform_zeroize(&client, sizeof(client)); |
| 4312 | mbedtls_platform_zeroize(&server, sizeof(server)); |
Andrzej Kurek | b4eedf7 | 2022-04-15 05:41:14 -0400 | [diff] [blame] | 4313 | |
| 4314 | /* Client side, force SECP256R1 to make one key bitflip fail |
Andrzej Kurek | 9cb14d4 | 2022-04-14 08:51:41 -0400 | [diff] [blame] | 4315 | * the raw key agreement. Flipping the first byte makes the |
| 4316 | * required 0x04 identifier invalid. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4317 | TEST_EQUAL(mbedtls_endpoint_init(&client, MBEDTLS_SSL_IS_CLIENT, |
| 4318 | MBEDTLS_PK_ECDSA, NULL, NULL, |
| 4319 | NULL, curve_list), 0); |
Andrzej Kurek | b4eedf7 | 2022-04-15 05:41:14 -0400 | [diff] [blame] | 4320 | |
| 4321 | /* Server side */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4322 | TEST_EQUAL(mbedtls_endpoint_init(&server, MBEDTLS_SSL_IS_SERVER, |
| 4323 | MBEDTLS_PK_ECDSA, NULL, NULL, |
| 4324 | NULL, NULL), 0); |
Andrzej Kurek | b4eedf7 | 2022-04-15 05:41:14 -0400 | [diff] [blame] | 4325 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4326 | TEST_EQUAL(mbedtls_mock_socket_connect(&(client.socket), |
| 4327 | &(server.socket), |
| 4328 | BUFFSIZE), 0); |
Andrzej Kurek | b4eedf7 | 2022-04-15 05:41:14 -0400 | [diff] [blame] | 4329 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4330 | TEST_EQUAL(mbedtls_move_handshake_to_state(&(client.ssl), |
| 4331 | &(server.ssl), |
| 4332 | MBEDTLS_SSL_CLIENT_KEY_EXCHANGE) |
| 4333 | , 0); |
Andrzej Kurek | b4eedf7 | 2022-04-15 05:41:14 -0400 | [diff] [blame] | 4334 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4335 | mbedtls_psa_get_stats(&stats); |
Andrzej Kurek | 2582ba3 | 2022-03-31 06:30:54 -0400 | [diff] [blame] | 4336 | /* Save the number of slots in use up to this point. |
| 4337 | * With PSA, one can be used for the ECDH private key. */ |
| 4338 | free_slots_before = stats.empty_slots; |
Andrzej Kurek | 99f6778 | 2022-03-31 07:17:18 -0400 | [diff] [blame] | 4339 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4340 | if (bad_server_ecdhe_key) { |
Gilles Peskine | 6dd489c | 2022-04-15 05:54:40 -0400 | [diff] [blame] | 4341 | /* Force a simulated bitflip in the server key. to make the |
| 4342 | * raw key agreement in ssl_write_client_key_exchange fail. */ |
| 4343 | (client.ssl).handshake->ecdh_psa_peerkey[0] ^= 0x02; |
| 4344 | } |
Andrzej Kurek | b4eedf7 | 2022-04-15 05:41:14 -0400 | [diff] [blame] | 4345 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4346 | TEST_EQUAL(mbedtls_move_handshake_to_state(&(client.ssl), |
| 4347 | &(server.ssl), |
| 4348 | MBEDTLS_SSL_HANDSHAKE_OVER), |
| 4349 | bad_server_ecdhe_key ? MBEDTLS_ERR_SSL_HW_ACCEL_FAILED : 0); |
Andrzej Kurek | b4eedf7 | 2022-04-15 05:41:14 -0400 | [diff] [blame] | 4350 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4351 | mbedtls_psa_get_stats(&stats); |
Andrzej Kurek | b4eedf7 | 2022-04-15 05:41:14 -0400 | [diff] [blame] | 4352 | |
Gilles Peskine | 6dd489c | 2022-04-15 05:54:40 -0400 | [diff] [blame] | 4353 | /* Make sure that the key slot is already destroyed in case of failure, |
| 4354 | * without waiting to close the connection. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4355 | if (bad_server_ecdhe_key) { |
| 4356 | TEST_EQUAL(free_slots_before, stats.empty_slots); |
| 4357 | } |
Andrzej Kurek | b4eedf7 | 2022-04-15 05:41:14 -0400 | [diff] [blame] | 4358 | |
| 4359 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4360 | mbedtls_endpoint_free(&client, NULL); |
| 4361 | mbedtls_endpoint_free(&server, NULL); |
Andrzej Kurek | 99f6778 | 2022-03-31 07:17:18 -0400 | [diff] [blame] | 4362 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4363 | USE_PSA_DONE(); |
Andrzej Kurek | b4eedf7 | 2022-04-15 05:41:14 -0400 | [diff] [blame] | 4364 | } |
| 4365 | /* END_CASE */ |
Andrzej Kurek | 862acb8 | 2022-06-06 13:08:23 -0400 | [diff] [blame] | 4366 | |
Andrzej Kurek | 3c036f5 | 2022-06-08 11:57:57 -0400 | [diff] [blame] | 4367 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_SRV_C:MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE:MBEDTLS_TEST_HOOKS */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4368 | void cookie_parsing(data_t *cookie, int exp_ret) |
Andrzej Kurek | 862acb8 | 2022-06-06 13:08:23 -0400 | [diff] [blame] | 4369 | { |
| 4370 | mbedtls_ssl_context ssl; |
| 4371 | mbedtls_ssl_config conf; |
| 4372 | size_t len; |
| 4373 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4374 | mbedtls_ssl_init(&ssl); |
| 4375 | mbedtls_ssl_config_init(&conf); |
| 4376 | TEST_EQUAL(mbedtls_ssl_config_defaults(&conf, MBEDTLS_SSL_IS_SERVER, |
| 4377 | MBEDTLS_SSL_TRANSPORT_DATAGRAM, |
| 4378 | MBEDTLS_SSL_PRESET_DEFAULT), |
| 4379 | 0); |
Andrzej Kurek | 862acb8 | 2022-06-06 13:08:23 -0400 | [diff] [blame] | 4380 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4381 | TEST_EQUAL(mbedtls_ssl_setup(&ssl, &conf), 0); |
| 4382 | TEST_EQUAL(mbedtls_ssl_check_dtls_clihlo_cookie(&ssl, ssl.cli_id, |
| 4383 | ssl.cli_id_len, |
| 4384 | cookie->x, cookie->len, |
| 4385 | ssl.out_buf, |
| 4386 | MBEDTLS_SSL_OUT_CONTENT_LEN, |
| 4387 | &len), |
| 4388 | exp_ret); |
Andrzej Kurek | 862acb8 | 2022-06-06 13:08:23 -0400 | [diff] [blame] | 4389 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4390 | mbedtls_ssl_free(&ssl); |
| 4391 | mbedtls_ssl_config_free(&conf); |
Andrzej Kurek | 862acb8 | 2022-06-06 13:08:23 -0400 | [diff] [blame] | 4392 | } |
| 4393 | /* END_CASE */ |