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 | { |
Dave Rodgman | cd09d68 | 2023-02-24 15:41:55 +0000 | [diff] [blame^] | 1055 | /* Verify that calling mbedtls_ssl_write with a NULL buffer and zero length is |
| 1056 | * a valid no-op for TLS connections. */ |
| 1057 | if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 1058 | TEST_ASSERT(mbedtls_ssl_write(ssl, NULL, 0) == 0); |
| 1059 | } |
| 1060 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1061 | int ret = mbedtls_ssl_write(ssl, buf + *written, buf_len - *written); |
| 1062 | if (ret > 0) { |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1063 | *written += ret; |
Piotr Nowicki | c3fca5e | 2020-01-30 15:33:42 +0100 | [diff] [blame] | 1064 | } |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1065 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1066 | if (expected_fragments == 0) { |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1067 | /* Used for DTLS and the message size larger than MFL. In that case |
| 1068 | * the message can not be fragmented and the library should return |
| 1069 | * MBEDTLS_ERR_SSL_BAD_INPUT_DATA error. This error must be returned |
| 1070 | * to prevent a dead loop inside mbedtls_exchange_data(). */ |
| 1071 | return ret; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1072 | } else if (expected_fragments == 1) { |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1073 | /* Used for TLS/DTLS and the message size lower than MFL */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1074 | TEST_ASSERT(ret == buf_len || |
| 1075 | ret == MBEDTLS_ERR_SSL_WANT_READ || |
| 1076 | ret == MBEDTLS_ERR_SSL_WANT_WRITE); |
| 1077 | } else { |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1078 | /* Used for TLS and the message size larger than MFL */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1079 | TEST_ASSERT(expected_fragments > 1); |
| 1080 | TEST_ASSERT((ret >= 0 && ret <= buf_len) || |
| 1081 | ret == MBEDTLS_ERR_SSL_WANT_READ || |
| 1082 | ret == MBEDTLS_ERR_SSL_WANT_WRITE); |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1083 | } |
| 1084 | |
| 1085 | return 0; |
| 1086 | |
| 1087 | exit: |
| 1088 | /* Some of the tests failed */ |
| 1089 | return -1; |
Piotr Nowicki | c3fca5e | 2020-01-30 15:33:42 +0100 | [diff] [blame] | 1090 | } |
| 1091 | |
| 1092 | /* |
Piotr Nowicki | 438bf3b | 2020-03-10 12:59:10 +0100 | [diff] [blame] | 1093 | * Read application data and increase read counter and fragments counter if necessary. |
Piotr Nowicki | c3fca5e | 2020-01-30 15:33:42 +0100 | [diff] [blame] | 1094 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1095 | int mbedtls_ssl_read_fragment(mbedtls_ssl_context *ssl, unsigned char *buf, |
| 1096 | int buf_len, int *read, |
| 1097 | int *fragments, const int expected_fragments) |
Piotr Nowicki | c3fca5e | 2020-01-30 15:33:42 +0100 | [diff] [blame] | 1098 | { |
Dave Rodgman | cd09d68 | 2023-02-24 15:41:55 +0000 | [diff] [blame^] | 1099 | /* Verify that calling mbedtls_ssl_write with a NULL buffer and zero length is |
| 1100 | * a valid no-op for TLS connections. */ |
| 1101 | if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) { |
| 1102 | TEST_ASSERT(mbedtls_ssl_read(ssl, NULL, 0) == 0); |
| 1103 | } |
| 1104 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1105 | int ret = mbedtls_ssl_read(ssl, buf + *read, buf_len - *read); |
| 1106 | if (ret > 0) { |
| 1107 | (*fragments)++; |
Piotr Nowicki | c3fca5e | 2020-01-30 15:33:42 +0100 | [diff] [blame] | 1108 | *read += ret; |
| 1109 | } |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1110 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1111 | if (expected_fragments == 0) { |
| 1112 | TEST_ASSERT(ret == 0); |
| 1113 | } else if (expected_fragments == 1) { |
| 1114 | TEST_ASSERT(ret == buf_len || |
| 1115 | ret == MBEDTLS_ERR_SSL_WANT_READ || |
| 1116 | ret == MBEDTLS_ERR_SSL_WANT_WRITE); |
| 1117 | } else { |
| 1118 | TEST_ASSERT(expected_fragments > 1); |
| 1119 | TEST_ASSERT((ret >= 0 && ret <= buf_len) || |
| 1120 | ret == MBEDTLS_ERR_SSL_WANT_READ || |
| 1121 | ret == MBEDTLS_ERR_SSL_WANT_WRITE); |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1122 | } |
| 1123 | |
| 1124 | return 0; |
| 1125 | |
| 1126 | exit: |
| 1127 | /* Some of the tests failed */ |
| 1128 | return -1; |
Piotr Nowicki | c3fca5e | 2020-01-30 15:33:42 +0100 | [diff] [blame] | 1129 | } |
| 1130 | |
| 1131 | /* |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1132 | * Helper function setting up inverse record transformations |
| 1133 | * using given cipher, hash, EtM mode, authentication tag length, |
| 1134 | * and version. |
| 1135 | */ |
| 1136 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1137 | #define CHK(x) \ |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1138 | do \ |
| 1139 | { \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1140 | if (!(x)) \ |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 1141 | { \ |
Hanno Becker | a5780f1 | 2019-04-05 09:55:37 +0100 | [diff] [blame] | 1142 | ret = -1; \ |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 1143 | goto cleanup; \ |
| 1144 | } \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1145 | } while (0) |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1146 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1147 | void set_ciphersuite(mbedtls_ssl_config *conf, const char *cipher, |
| 1148 | int *forced_ciphersuite) |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 1149 | { |
| 1150 | const mbedtls_ssl_ciphersuite_t *ciphersuite_info; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1151 | forced_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id(cipher); |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 1152 | forced_ciphersuite[1] = 0; |
| 1153 | |
| 1154 | ciphersuite_info = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1155 | mbedtls_ssl_ciphersuite_from_id(forced_ciphersuite[0]); |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 1156 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1157 | TEST_ASSERT(ciphersuite_info != NULL); |
| 1158 | TEST_ASSERT(ciphersuite_info->min_minor_ver <= conf->max_minor_ver); |
| 1159 | TEST_ASSERT(ciphersuite_info->max_minor_ver >= conf->min_minor_ver); |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 1160 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1161 | if (conf->max_minor_ver > ciphersuite_info->max_minor_ver) { |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 1162 | conf->max_minor_ver = ciphersuite_info->max_minor_ver; |
| 1163 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1164 | if (conf->min_minor_ver < ciphersuite_info->min_minor_ver) { |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 1165 | conf->min_minor_ver = ciphersuite_info->min_minor_ver; |
| 1166 | } |
| 1167 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1168 | mbedtls_ssl_conf_ciphersuites(conf, forced_ciphersuite); |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 1169 | |
| 1170 | exit: |
| 1171 | return; |
| 1172 | } |
| 1173 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1174 | int psk_dummy_callback(void *p_info, mbedtls_ssl_context *ssl, |
| 1175 | const unsigned char *name, size_t name_len) |
Andrzej Kurek | cc5169c | 2020-02-04 09:04:56 -0500 | [diff] [blame] | 1176 | { |
| 1177 | (void) p_info; |
| 1178 | (void) ssl; |
| 1179 | (void) name; |
| 1180 | (void) name_len; |
| 1181 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1182 | return 0; |
Andrzej Kurek | cc5169c | 2020-02-04 09:04:56 -0500 | [diff] [blame] | 1183 | } |
| 1184 | |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 1185 | #if MBEDTLS_SSL_CID_OUT_LEN_MAX > MBEDTLS_SSL_CID_IN_LEN_MAX |
| 1186 | #define SSL_CID_LEN_MIN MBEDTLS_SSL_CID_IN_LEN_MAX |
| 1187 | #else |
| 1188 | #define SSL_CID_LEN_MIN MBEDTLS_SSL_CID_OUT_LEN_MAX |
| 1189 | #endif |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1190 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1191 | static int build_transforms(mbedtls_ssl_transform *t_in, |
| 1192 | mbedtls_ssl_transform *t_out, |
| 1193 | int cipher_type, int hash_id, |
| 1194 | int etm, int tag_mode, int ver, |
| 1195 | size_t cid0_len, |
| 1196 | size_t cid1_len) |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1197 | { |
| 1198 | mbedtls_cipher_info_t const *cipher_info; |
Hanno Becker | a5780f1 | 2019-04-05 09:55:37 +0100 | [diff] [blame] | 1199 | int ret = 0; |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1200 | |
| 1201 | size_t keylen, maclen, ivlen; |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 1202 | unsigned char *key0 = NULL, *key1 = NULL; |
Paul Elliott | 6f1eda7 | 2020-06-11 20:22:00 +0100 | [diff] [blame] | 1203 | unsigned char *md0 = NULL, *md1 = NULL; |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1204 | unsigned char iv_enc[16], iv_dec[16]; |
| 1205 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1206 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1207 | unsigned char cid0[SSL_CID_LEN_MIN]; |
| 1208 | unsigned char cid1[SSL_CID_LEN_MIN]; |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 1209 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1210 | mbedtls_test_rnd_std_rand(NULL, cid0, sizeof(cid0)); |
| 1211 | mbedtls_test_rnd_std_rand(NULL, cid1, sizeof(cid1)); |
Hanno Becker | 43c24b8 | 2019-05-01 09:45:57 +0100 | [diff] [blame] | 1212 | #else |
| 1213 | ((void) cid0_len); |
| 1214 | ((void) cid1_len); |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1215 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 1216 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1217 | maclen = 0; |
| 1218 | |
| 1219 | /* Pick cipher */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1220 | cipher_info = mbedtls_cipher_info_from_type(cipher_type); |
| 1221 | CHK(cipher_info != NULL); |
| 1222 | CHK(cipher_info->iv_size <= 16); |
| 1223 | CHK(cipher_info->key_bitlen % 8 == 0); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1224 | |
| 1225 | /* Pick keys */ |
| 1226 | keylen = cipher_info->key_bitlen / 8; |
Hanno Becker | 78d1f70 | 2019-04-05 09:56:10 +0100 | [diff] [blame] | 1227 | /* Allocate `keylen + 1` bytes to ensure that we get |
| 1228 | * a non-NULL pointers from `mbedtls_calloc` even if |
| 1229 | * `keylen == 0` in the case of the NULL cipher. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1230 | CHK((key0 = mbedtls_calloc(1, keylen + 1)) != NULL); |
| 1231 | CHK((key1 = mbedtls_calloc(1, keylen + 1)) != NULL); |
| 1232 | memset(key0, 0x1, keylen); |
| 1233 | memset(key1, 0x2, keylen); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1234 | |
| 1235 | /* Setup cipher contexts */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1236 | CHK(mbedtls_cipher_setup(&t_in->cipher_ctx_enc, cipher_info) == 0); |
| 1237 | CHK(mbedtls_cipher_setup(&t_in->cipher_ctx_dec, cipher_info) == 0); |
| 1238 | CHK(mbedtls_cipher_setup(&t_out->cipher_ctx_enc, cipher_info) == 0); |
| 1239 | CHK(mbedtls_cipher_setup(&t_out->cipher_ctx_dec, cipher_info) == 0); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1240 | |
| 1241 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1242 | if (cipher_info->mode == MBEDTLS_MODE_CBC) { |
| 1243 | CHK(mbedtls_cipher_set_padding_mode(&t_in->cipher_ctx_enc, |
| 1244 | MBEDTLS_PADDING_NONE) == 0); |
| 1245 | CHK(mbedtls_cipher_set_padding_mode(&t_in->cipher_ctx_dec, |
| 1246 | MBEDTLS_PADDING_NONE) == 0); |
| 1247 | CHK(mbedtls_cipher_set_padding_mode(&t_out->cipher_ctx_enc, |
| 1248 | MBEDTLS_PADDING_NONE) == 0); |
| 1249 | CHK(mbedtls_cipher_set_padding_mode(&t_out->cipher_ctx_dec, |
| 1250 | MBEDTLS_PADDING_NONE) == 0); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1251 | } |
| 1252 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
| 1253 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1254 | CHK(mbedtls_cipher_setkey(&t_in->cipher_ctx_enc, key0, |
| 1255 | keylen << 3, MBEDTLS_ENCRYPT) == 0); |
| 1256 | CHK(mbedtls_cipher_setkey(&t_in->cipher_ctx_dec, key1, |
| 1257 | keylen << 3, MBEDTLS_DECRYPT) == 0); |
| 1258 | CHK(mbedtls_cipher_setkey(&t_out->cipher_ctx_enc, key1, |
| 1259 | keylen << 3, MBEDTLS_ENCRYPT) == 0); |
| 1260 | CHK(mbedtls_cipher_setkey(&t_out->cipher_ctx_dec, key0, |
| 1261 | keylen << 3, MBEDTLS_DECRYPT) == 0); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1262 | |
| 1263 | /* Setup MAC contexts */ |
| 1264 | #if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1265 | if (cipher_info->mode == MBEDTLS_MODE_CBC || |
| 1266 | cipher_info->mode == MBEDTLS_MODE_STREAM) { |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1267 | mbedtls_md_info_t const *md_info; |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1268 | |
| 1269 | /* Pick hash */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1270 | md_info = mbedtls_md_info_from_type(hash_id); |
| 1271 | CHK(md_info != NULL); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1272 | |
| 1273 | /* Pick hash keys */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1274 | maclen = mbedtls_md_get_size(md_info); |
| 1275 | CHK((md0 = mbedtls_calloc(1, maclen)) != NULL); |
| 1276 | CHK((md1 = mbedtls_calloc(1, maclen)) != NULL); |
| 1277 | memset(md0, 0x5, maclen); |
| 1278 | memset(md1, 0x6, maclen); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1279 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1280 | CHK(mbedtls_md_setup(&t_out->md_ctx_enc, md_info, 1) == 0); |
| 1281 | CHK(mbedtls_md_setup(&t_out->md_ctx_dec, md_info, 1) == 0); |
| 1282 | CHK(mbedtls_md_setup(&t_in->md_ctx_enc, md_info, 1) == 0); |
| 1283 | 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] | 1284 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1285 | if (ver > MBEDTLS_SSL_MINOR_VERSION_0) { |
| 1286 | CHK(mbedtls_md_hmac_starts(&t_in->md_ctx_enc, |
| 1287 | md0, maclen) == 0); |
| 1288 | CHK(mbedtls_md_hmac_starts(&t_in->md_ctx_dec, |
| 1289 | md1, maclen) == 0); |
| 1290 | CHK(mbedtls_md_hmac_starts(&t_out->md_ctx_enc, |
| 1291 | md1, maclen) == 0); |
| 1292 | CHK(mbedtls_md_hmac_starts(&t_out->md_ctx_dec, |
| 1293 | md0, maclen) == 0); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1294 | } |
| 1295 | #if defined(MBEDTLS_SSL_PROTO_SSL3) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1296 | else { |
| 1297 | memcpy(&t_in->mac_enc, md0, maclen); |
| 1298 | memcpy(&t_in->mac_dec, md1, maclen); |
| 1299 | memcpy(&t_out->mac_enc, md1, maclen); |
| 1300 | memcpy(&t_out->mac_dec, md0, maclen); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1301 | } |
| 1302 | #endif |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1303 | } |
| 1304 | #else |
| 1305 | ((void) hash_id); |
| 1306 | #endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */ |
| 1307 | |
| 1308 | |
| 1309 | /* Pick IV's (regardless of whether they |
| 1310 | * are being used by the transform). */ |
| 1311 | ivlen = cipher_info->iv_size; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1312 | memset(iv_enc, 0x3, sizeof(iv_enc)); |
| 1313 | memset(iv_dec, 0x4, sizeof(iv_dec)); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1314 | |
| 1315 | /* |
| 1316 | * Setup transforms |
| 1317 | */ |
| 1318 | |
Jaeden Amero | 2de07f1 | 2019-06-05 13:32:08 +0100 | [diff] [blame] | 1319 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \ |
| 1320 | defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1321 | t_out->encrypt_then_mac = etm; |
| 1322 | t_in->encrypt_then_mac = etm; |
| 1323 | #else |
| 1324 | ((void) etm); |
| 1325 | #endif |
| 1326 | |
| 1327 | t_out->minor_ver = ver; |
| 1328 | t_in->minor_ver = ver; |
| 1329 | t_out->ivlen = ivlen; |
| 1330 | t_in->ivlen = ivlen; |
| 1331 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1332 | switch (cipher_info->mode) { |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1333 | case MBEDTLS_MODE_GCM: |
| 1334 | case MBEDTLS_MODE_CCM: |
Hanno Becker | e683287 | 2020-05-28 08:29:58 +0100 | [diff] [blame] | 1335 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1336 | if (ver == MBEDTLS_SSL_MINOR_VERSION_4) { |
Hanno Becker | e683287 | 2020-05-28 08:29:58 +0100 | [diff] [blame] | 1337 | t_out->fixed_ivlen = 12; |
| 1338 | t_in->fixed_ivlen = 12; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1339 | } else |
Hanno Becker | e683287 | 2020-05-28 08:29:58 +0100 | [diff] [blame] | 1340 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ |
| 1341 | { |
| 1342 | t_out->fixed_ivlen = 4; |
| 1343 | t_in->fixed_ivlen = 4; |
| 1344 | } |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1345 | t_out->maclen = 0; |
| 1346 | t_in->maclen = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1347 | switch (tag_mode) { |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1348 | case 0: /* Full tag */ |
| 1349 | t_out->taglen = 16; |
| 1350 | t_in->taglen = 16; |
| 1351 | break; |
| 1352 | case 1: /* Partial tag */ |
| 1353 | t_out->taglen = 8; |
| 1354 | t_in->taglen = 8; |
| 1355 | break; |
| 1356 | default: |
Paul Elliott | c7b5374 | 2021-02-03 13:18:33 +0000 | [diff] [blame] | 1357 | ret = 1; |
| 1358 | goto cleanup; |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1359 | } |
| 1360 | break; |
| 1361 | |
| 1362 | case MBEDTLS_MODE_CHACHAPOLY: |
| 1363 | t_out->fixed_ivlen = 12; |
| 1364 | t_in->fixed_ivlen = 12; |
| 1365 | t_out->maclen = 0; |
| 1366 | t_in->maclen = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1367 | switch (tag_mode) { |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1368 | case 0: /* Full tag */ |
| 1369 | t_out->taglen = 16; |
| 1370 | t_in->taglen = 16; |
| 1371 | break; |
| 1372 | case 1: /* Partial tag */ |
| 1373 | t_out->taglen = 8; |
| 1374 | t_in->taglen = 8; |
| 1375 | break; |
| 1376 | default: |
Paul Elliott | c7b5374 | 2021-02-03 13:18:33 +0000 | [diff] [blame] | 1377 | ret = 1; |
| 1378 | goto cleanup; |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1379 | } |
| 1380 | break; |
| 1381 | |
| 1382 | case MBEDTLS_MODE_STREAM: |
| 1383 | case MBEDTLS_MODE_CBC: |
| 1384 | t_out->fixed_ivlen = 0; /* redundant, must be 0 */ |
| 1385 | t_in->fixed_ivlen = 0; /* redundant, must be 0 */ |
| 1386 | t_out->taglen = 0; |
| 1387 | t_in->taglen = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1388 | switch (tag_mode) { |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1389 | case 0: /* Full tag */ |
| 1390 | t_out->maclen = maclen; |
| 1391 | t_in->maclen = maclen; |
| 1392 | break; |
| 1393 | case 1: /* Partial tag */ |
| 1394 | t_out->maclen = 10; |
| 1395 | t_in->maclen = 10; |
| 1396 | break; |
| 1397 | default: |
Paul Elliott | c7b5374 | 2021-02-03 13:18:33 +0000 | [diff] [blame] | 1398 | ret = 1; |
| 1399 | goto cleanup; |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1400 | } |
| 1401 | break; |
| 1402 | default: |
Paul Elliott | c7b5374 | 2021-02-03 13:18:33 +0000 | [diff] [blame] | 1403 | ret = 1; |
| 1404 | goto cleanup; |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1405 | break; |
| 1406 | } |
| 1407 | |
| 1408 | /* Setup IV's */ |
| 1409 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1410 | memcpy(&t_in->iv_dec, iv_dec, sizeof(iv_dec)); |
| 1411 | memcpy(&t_in->iv_enc, iv_enc, sizeof(iv_enc)); |
| 1412 | memcpy(&t_out->iv_dec, iv_enc, sizeof(iv_enc)); |
| 1413 | memcpy(&t_out->iv_enc, iv_dec, sizeof(iv_dec)); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1414 | |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1415 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 1416 | /* Add CID */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1417 | memcpy(&t_in->in_cid, cid0, cid0_len); |
| 1418 | memcpy(&t_in->out_cid, cid1, cid1_len); |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 1419 | t_in->in_cid_len = cid0_len; |
| 1420 | t_in->out_cid_len = cid1_len; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1421 | memcpy(&t_out->in_cid, cid1, cid1_len); |
| 1422 | memcpy(&t_out->out_cid, cid0, cid0_len); |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 1423 | t_out->in_cid_len = cid1_len; |
| 1424 | t_out->out_cid_len = cid0_len; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1425 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 1426 | |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 1427 | cleanup: |
| 1428 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1429 | mbedtls_free(key0); |
| 1430 | mbedtls_free(key1); |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 1431 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1432 | mbedtls_free(md0); |
| 1433 | mbedtls_free(md1); |
Paul Elliott | 6f1eda7 | 2020-06-11 20:22:00 +0100 | [diff] [blame] | 1434 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1435 | return ret; |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1436 | } |
| 1437 | |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1438 | /* |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 1439 | * Populate a session structure for serialization tests. |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1440 | * Choose dummy values, mostly non-0 to distinguish from the init default. |
| 1441 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1442 | static int ssl_populate_session(mbedtls_ssl_session *session, |
| 1443 | int ticket_len, |
| 1444 | const char *crt_file) |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1445 | { |
| 1446 | #if defined(MBEDTLS_HAVE_TIME) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1447 | session->start = mbedtls_time(NULL) - 42; |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1448 | #endif |
| 1449 | session->ciphersuite = 0xabcd; |
| 1450 | session->compression = 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1451 | session->id_len = sizeof(session->id); |
| 1452 | memset(session->id, 66, session->id_len); |
| 1453 | memset(session->master, 17, sizeof(session->master)); |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1454 | |
Gilles Peskine | 33d03fe | 2023-02-01 18:37:49 +0100 | [diff] [blame] | 1455 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) && \ |
| 1456 | defined(MBEDTLS_CERTS_C) && \ |
| 1457 | defined(MBEDTLS_FS_IO) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1458 | if (strlen(crt_file) != 0) { |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 1459 | mbedtls_x509_crt tmp_crt; |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1460 | int ret; |
Manuel Pégourié-Gonnard | 6b84070 | 2019-05-24 09:40:17 +0200 | [diff] [blame] | 1461 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1462 | mbedtls_x509_crt_init(&tmp_crt); |
| 1463 | ret = mbedtls_x509_crt_parse_file(&tmp_crt, crt_file); |
| 1464 | if (ret != 0) { |
| 1465 | return ret; |
| 1466 | } |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 1467 | |
| 1468 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
| 1469 | /* Move temporary CRT. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1470 | session->peer_cert = mbedtls_calloc(1, sizeof(*session->peer_cert)); |
| 1471 | if (session->peer_cert == NULL) { |
| 1472 | return -1; |
| 1473 | } |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 1474 | *session->peer_cert = tmp_crt; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1475 | memset(&tmp_crt, 0, sizeof(tmp_crt)); |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 1476 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 1477 | /* Calculate digest of temporary CRT. */ |
| 1478 | session->peer_cert_digest = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1479 | mbedtls_calloc(1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN); |
| 1480 | if (session->peer_cert_digest == NULL) { |
| 1481 | return -1; |
| 1482 | } |
| 1483 | ret = mbedtls_md(mbedtls_md_info_from_type( |
| 1484 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE), |
| 1485 | tmp_crt.raw.p, tmp_crt.raw.len, |
| 1486 | session->peer_cert_digest); |
| 1487 | if (ret != 0) { |
| 1488 | return ret; |
| 1489 | } |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 1490 | session->peer_cert_digest_type = |
| 1491 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE; |
| 1492 | session->peer_cert_digest_len = |
| 1493 | MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN; |
| 1494 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
| 1495 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1496 | mbedtls_x509_crt_free(&tmp_crt); |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1497 | } |
Gilles Peskine | 33d03fe | 2023-02-01 18:37:49 +0100 | [diff] [blame] | 1498 | #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] | 1499 | (void) crt_file; |
Gilles Peskine | 33d03fe | 2023-02-01 18:37:49 +0100 | [diff] [blame] | 1500 | #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] | 1501 | session->verify_result = 0xdeadbeef; |
| 1502 | |
| 1503 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1504 | if (ticket_len != 0) { |
| 1505 | session->ticket = mbedtls_calloc(1, ticket_len); |
| 1506 | if (session->ticket == NULL) { |
| 1507 | return -1; |
| 1508 | } |
| 1509 | memset(session->ticket, 33, ticket_len); |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1510 | } |
| 1511 | session->ticket_len = ticket_len; |
| 1512 | session->ticket_lifetime = 86401; |
| 1513 | #else |
| 1514 | (void) ticket_len; |
| 1515 | #endif |
| 1516 | |
| 1517 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
| 1518 | session->mfl_code = 1; |
| 1519 | #endif |
| 1520 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
| 1521 | session->trunc_hmac = 1; |
| 1522 | #endif |
| 1523 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
| 1524 | session->encrypt_then_mac = 1; |
| 1525 | #endif |
| 1526 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1527 | return 0; |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1528 | } |
| 1529 | |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1530 | /* |
| 1531 | * Perform data exchanging between \p ssl_1 and \p ssl_2 and check if the |
| 1532 | * message was sent in the correct number of fragments. |
| 1533 | * |
| 1534 | * /p ssl_1 and /p ssl_2 Endpoints represented by mbedtls_ssl_context. Both |
| 1535 | * of them must be initialized and connected beforehand. |
| 1536 | * /p msg_len_1 and /p msg_len_2 specify the size of the message to send. |
| 1537 | * /p expected_fragments_1 and /p expected_fragments_2 determine in how many |
| 1538 | * fragments the message should be sent. |
| 1539 | * expected_fragments is 0: can be used for DTLS testing while the message |
| 1540 | * size is larger than MFL. In that case the message |
| 1541 | * cannot be fragmented and sent to the second endpoint. |
| 1542 | * This value can be used for negative tests. |
| 1543 | * expected_fragments is 1: can be used for TLS/DTLS testing while the |
| 1544 | * message size is below MFL |
| 1545 | * expected_fragments > 1: can be used for TLS testing while the message |
| 1546 | * size is larger than MFL |
| 1547 | * |
| 1548 | * \retval 0 on success, otherwise error code. |
| 1549 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1550 | int mbedtls_exchange_data(mbedtls_ssl_context *ssl_1, |
| 1551 | int msg_len_1, const int expected_fragments_1, |
| 1552 | mbedtls_ssl_context *ssl_2, |
| 1553 | int msg_len_2, const int expected_fragments_2) |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1554 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1555 | unsigned char *msg_buf_1 = malloc(msg_len_1); |
| 1556 | unsigned char *msg_buf_2 = malloc(msg_len_2); |
| 1557 | unsigned char *in_buf_1 = malloc(msg_len_2); |
| 1558 | unsigned char *in_buf_2 = malloc(msg_len_1); |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1559 | int msg_type, ret = -1; |
| 1560 | |
| 1561 | /* Perform this test with two message types. At first use a message |
| 1562 | * consisting of only 0x00 for the client and only 0xFF for the server. |
| 1563 | * At the second time use message with generated data */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1564 | for (msg_type = 0; msg_type < 2; msg_type++) { |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1565 | int written_1 = 0; |
| 1566 | int written_2 = 0; |
| 1567 | int read_1 = 0; |
| 1568 | int read_2 = 0; |
| 1569 | int fragments_1 = 0; |
| 1570 | int fragments_2 = 0; |
| 1571 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1572 | if (msg_type == 0) { |
| 1573 | memset(msg_buf_1, 0x00, msg_len_1); |
| 1574 | memset(msg_buf_2, 0xff, msg_len_2); |
| 1575 | } else { |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1576 | int i, j = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1577 | for (i = 0; i < msg_len_1; i++) { |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1578 | msg_buf_1[i] = j++ & 0xFF; |
| 1579 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1580 | for (i = 0; i < msg_len_2; i++) { |
| 1581 | msg_buf_2[i] = (j -= 5) & 0xFF; |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1582 | } |
| 1583 | } |
| 1584 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1585 | while (read_1 < msg_len_2 || read_2 < msg_len_1) { |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1586 | /* ssl_1 sending */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1587 | if (msg_len_1 > written_1) { |
| 1588 | ret = mbedtls_ssl_write_fragment(ssl_1, msg_buf_1, |
| 1589 | msg_len_1, &written_1, |
| 1590 | expected_fragments_1); |
| 1591 | if (expected_fragments_1 == 0) { |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1592 | /* This error is expected when the message is too large and |
| 1593 | * cannot be fragmented */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1594 | TEST_ASSERT(ret == MBEDTLS_ERR_SSL_BAD_INPUT_DATA); |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1595 | msg_len_1 = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1596 | } else { |
| 1597 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1598 | } |
| 1599 | } |
| 1600 | |
| 1601 | /* ssl_2 sending */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1602 | if (msg_len_2 > written_2) { |
| 1603 | ret = mbedtls_ssl_write_fragment(ssl_2, msg_buf_2, |
| 1604 | msg_len_2, &written_2, |
| 1605 | expected_fragments_2); |
| 1606 | if (expected_fragments_2 == 0) { |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1607 | /* This error is expected when the message is too large and |
| 1608 | * cannot be fragmented */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1609 | TEST_ASSERT(ret == MBEDTLS_ERR_SSL_BAD_INPUT_DATA); |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1610 | msg_len_2 = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1611 | } else { |
| 1612 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1613 | } |
| 1614 | } |
| 1615 | |
| 1616 | /* ssl_1 reading */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1617 | if (read_1 < msg_len_2) { |
| 1618 | ret = mbedtls_ssl_read_fragment(ssl_1, in_buf_1, |
| 1619 | msg_len_2, &read_1, |
| 1620 | &fragments_2, |
| 1621 | expected_fragments_2); |
| 1622 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1623 | } |
| 1624 | |
| 1625 | /* ssl_2 reading */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1626 | if (read_2 < msg_len_1) { |
| 1627 | ret = mbedtls_ssl_read_fragment(ssl_2, in_buf_2, |
| 1628 | msg_len_1, &read_2, |
| 1629 | &fragments_1, |
| 1630 | expected_fragments_1); |
| 1631 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1632 | } |
| 1633 | } |
| 1634 | |
| 1635 | ret = -1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1636 | TEST_ASSERT(0 == memcmp(msg_buf_1, in_buf_2, msg_len_1)); |
| 1637 | TEST_ASSERT(0 == memcmp(msg_buf_2, in_buf_1, msg_len_2)); |
| 1638 | TEST_ASSERT(fragments_1 == expected_fragments_1); |
| 1639 | TEST_ASSERT(fragments_2 == expected_fragments_2); |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1640 | } |
| 1641 | |
| 1642 | ret = 0; |
| 1643 | |
| 1644 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1645 | free(msg_buf_1); |
| 1646 | free(in_buf_1); |
| 1647 | free(msg_buf_2); |
| 1648 | free(in_buf_2); |
Piotr Nowicki | 6a7f01c | 2020-02-12 13:53:36 +0100 | [diff] [blame] | 1649 | |
| 1650 | return ret; |
| 1651 | } |
| 1652 | |
Piotr Nowicki | 95e9eb8 | 2020-02-14 11:33:34 +0100 | [diff] [blame] | 1653 | /* |
| 1654 | * Perform data exchanging between \p ssl_1 and \p ssl_2. Both of endpoints |
| 1655 | * must be initialized and connected beforehand. |
| 1656 | * |
| 1657 | * \retval 0 on success, otherwise error code. |
| 1658 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1659 | int exchange_data(mbedtls_ssl_context *ssl_1, |
| 1660 | mbedtls_ssl_context *ssl_2) |
Piotr Nowicki | 95e9eb8 | 2020-02-14 11:33:34 +0100 | [diff] [blame] | 1661 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1662 | return mbedtls_exchange_data(ssl_1, 256, 1, |
| 1663 | ssl_2, 256, 1); |
Piotr Nowicki | 95e9eb8 | 2020-02-14 11:33:34 +0100 | [diff] [blame] | 1664 | } |
| 1665 | |
Andrzej Kurek | 9155e7f | 2022-10-18 09:36:19 -0400 | [diff] [blame] | 1666 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) && \ |
Gilles Peskine | 33d03fe | 2023-02-01 18:37:49 +0100 | [diff] [blame] | 1667 | defined(MBEDTLS_CERTS_C) && \ |
Manuel Pégourié-Gonnard | d12402f | 2020-05-20 10:34:25 +0200 | [diff] [blame] | 1668 | defined(MBEDTLS_ENTROPY_C) && \ |
| 1669 | defined(MBEDTLS_CTR_DRBG_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1670 | void perform_handshake(handshake_test_options *options) |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1671 | { |
| 1672 | /* forced_ciphersuite needs to last until the end of the handshake */ |
| 1673 | int forced_ciphersuite[2]; |
| 1674 | enum { BUFFSIZE = 17000 }; |
| 1675 | mbedtls_endpoint client, server; |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 1676 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1677 | const char *psk_identity = "foo"; |
| 1678 | #endif |
| 1679 | #if defined(MBEDTLS_TIMING_C) |
| 1680 | mbedtls_timing_delay_context timer_client, timer_server; |
| 1681 | #endif |
| 1682 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
| 1683 | unsigned char *context_buf = NULL; |
| 1684 | size_t context_buf_len; |
| 1685 | #endif |
| 1686 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 1687 | int ret = -1; |
| 1688 | #endif |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 1689 | int expected_handshake_result = 0; |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1690 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1691 | USE_PSA_INIT(); |
| 1692 | mbedtls_platform_zeroize(&client, sizeof(client)); |
| 1693 | mbedtls_platform_zeroize(&server, sizeof(server)); |
Andrzej Kurek | 0d2982b | 2022-10-18 07:55:46 -0400 | [diff] [blame] | 1694 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1695 | mbedtls_test_message_queue server_queue, client_queue; |
| 1696 | mbedtls_test_message_socket_context server_context, client_context; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1697 | mbedtls_message_socket_init(&server_context); |
| 1698 | mbedtls_message_socket_init(&client_context); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1699 | |
| 1700 | /* Client side */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1701 | if (options->dtls != 0) { |
| 1702 | TEST_ASSERT(mbedtls_endpoint_init(&client, MBEDTLS_SSL_IS_CLIENT, |
| 1703 | options->pk_alg, &client_context, |
| 1704 | &client_queue, |
| 1705 | &server_queue, NULL) == 0); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1706 | #if defined(MBEDTLS_TIMING_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1707 | mbedtls_ssl_set_timer_cb(&client.ssl, &timer_client, |
| 1708 | mbedtls_timing_set_delay, |
| 1709 | mbedtls_timing_get_delay); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1710 | #endif |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1711 | } else { |
| 1712 | TEST_ASSERT(mbedtls_endpoint_init(&client, MBEDTLS_SSL_IS_CLIENT, |
| 1713 | options->pk_alg, NULL, NULL, |
| 1714 | NULL, NULL) == 0); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1715 | } |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 1716 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1717 | if (options->client_min_version != TEST_SSL_MINOR_VERSION_NONE) { |
| 1718 | mbedtls_ssl_conf_min_version(&client.conf, MBEDTLS_SSL_MAJOR_VERSION_3, |
| 1719 | options->client_min_version); |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 1720 | } |
| 1721 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1722 | if (options->client_max_version != TEST_SSL_MINOR_VERSION_NONE) { |
| 1723 | mbedtls_ssl_conf_max_version(&client.conf, MBEDTLS_SSL_MAJOR_VERSION_3, |
| 1724 | options->client_max_version); |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 1725 | } |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1726 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1727 | if (strlen(options->cipher) > 0) { |
| 1728 | set_ciphersuite(&client.conf, options->cipher, forced_ciphersuite); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1729 | } |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 1730 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1731 | #if defined(MBEDTLS_DEBUG_C) |
| 1732 | if (options->cli_log_fun) { |
| 1733 | mbedtls_debug_set_threshold(4); |
| 1734 | mbedtls_ssl_conf_dbg(&client.conf, options->cli_log_fun, |
| 1735 | options->cli_log_obj); |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 1736 | } |
| 1737 | #endif |
| 1738 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1739 | /* Server side */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1740 | if (options->dtls != 0) { |
| 1741 | TEST_ASSERT(mbedtls_endpoint_init(&server, MBEDTLS_SSL_IS_SERVER, |
| 1742 | options->pk_alg, &server_context, |
| 1743 | &server_queue, |
| 1744 | &client_queue, NULL) == 0); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1745 | #if defined(MBEDTLS_TIMING_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1746 | mbedtls_ssl_set_timer_cb(&server.ssl, &timer_server, |
| 1747 | mbedtls_timing_set_delay, |
| 1748 | mbedtls_timing_get_delay); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1749 | #endif |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1750 | } else { |
| 1751 | TEST_ASSERT(mbedtls_endpoint_init(&server, MBEDTLS_SSL_IS_SERVER, |
| 1752 | options->pk_alg, NULL, NULL, |
| 1753 | NULL, NULL) == 0); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1754 | } |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 1755 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1756 | mbedtls_ssl_conf_authmode(&server.conf, options->srv_auth_mode); |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 1757 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1758 | if (options->server_min_version != TEST_SSL_MINOR_VERSION_NONE) { |
| 1759 | mbedtls_ssl_conf_min_version(&server.conf, MBEDTLS_SSL_MAJOR_VERSION_3, |
| 1760 | options->server_min_version); |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 1761 | } |
| 1762 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1763 | if (options->server_max_version != TEST_SSL_MINOR_VERSION_NONE) { |
| 1764 | mbedtls_ssl_conf_max_version(&server.conf, MBEDTLS_SSL_MAJOR_VERSION_3, |
| 1765 | options->server_max_version); |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 1766 | } |
| 1767 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1768 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1769 | TEST_ASSERT(mbedtls_ssl_conf_max_frag_len(&(server.conf), |
| 1770 | (unsigned char) options->mfl) == 0); |
| 1771 | TEST_ASSERT(mbedtls_ssl_conf_max_frag_len(&(client.conf), |
| 1772 | (unsigned char) options->mfl) == 0); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1773 | #else |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1774 | TEST_ASSERT(MBEDTLS_SSL_MAX_FRAG_LEN_NONE == options->mfl); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1775 | #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */ |
| 1776 | |
Gilles Peskine | eccd888 | 2020-03-10 12:19:08 +0100 | [diff] [blame] | 1777 | #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1778 | if (options->psk_str != NULL && options->psk_str->len > 0) { |
| 1779 | TEST_ASSERT(mbedtls_ssl_conf_psk(&client.conf, options->psk_str->x, |
| 1780 | options->psk_str->len, |
| 1781 | (const unsigned char *) psk_identity, |
| 1782 | strlen(psk_identity)) == 0); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1783 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1784 | TEST_ASSERT(mbedtls_ssl_conf_psk(&server.conf, options->psk_str->x, |
| 1785 | options->psk_str->len, |
| 1786 | (const unsigned char *) psk_identity, |
| 1787 | strlen(psk_identity)) == 0); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1788 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1789 | mbedtls_ssl_conf_psk_cb(&server.conf, psk_dummy_callback, NULL); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1790 | } |
| 1791 | #endif |
| 1792 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1793 | if (options->renegotiate) { |
| 1794 | mbedtls_ssl_conf_renegotiation(&(server.conf), |
| 1795 | MBEDTLS_SSL_RENEGOTIATION_ENABLED); |
| 1796 | mbedtls_ssl_conf_renegotiation(&(client.conf), |
| 1797 | MBEDTLS_SSL_RENEGOTIATION_ENABLED); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1798 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1799 | mbedtls_ssl_conf_legacy_renegotiation(&(server.conf), |
| 1800 | options->legacy_renegotiation); |
| 1801 | mbedtls_ssl_conf_legacy_renegotiation(&(client.conf), |
| 1802 | options->legacy_renegotiation); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1803 | } |
| 1804 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
| 1805 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1806 | #if defined(MBEDTLS_DEBUG_C) |
| 1807 | if (options->srv_log_fun) { |
| 1808 | mbedtls_debug_set_threshold(4); |
| 1809 | mbedtls_ssl_conf_dbg(&server.conf, options->srv_log_fun, |
| 1810 | options->srv_log_obj); |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 1811 | } |
| 1812 | #endif |
| 1813 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1814 | TEST_ASSERT(mbedtls_mock_socket_connect(&(client.socket), |
| 1815 | &(server.socket), |
| 1816 | BUFFSIZE) == 0); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1817 | |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 1818 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1819 | if (options->resize_buffers != 0) { |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 1820 | /* Ensure that the buffer sizes are appropriate before resizes */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1821 | TEST_ASSERT(client.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN); |
| 1822 | TEST_ASSERT(client.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN); |
| 1823 | TEST_ASSERT(server.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN); |
| 1824 | TEST_ASSERT(server.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 1825 | } |
| 1826 | #endif |
| 1827 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1828 | if (options->expected_negotiated_version == TEST_SSL_MINOR_VERSION_NONE) { |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 1829 | expected_handshake_result = MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION; |
| 1830 | } |
| 1831 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1832 | TEST_ASSERT(mbedtls_move_handshake_to_state(&(client.ssl), |
| 1833 | &(server.ssl), |
| 1834 | MBEDTLS_SSL_HANDSHAKE_OVER) |
| 1835 | == expected_handshake_result); |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 1836 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1837 | if (expected_handshake_result != 0) { |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 1838 | /* Connection will have failed by this point, skip to cleanup */ |
| 1839 | goto exit; |
| 1840 | } |
| 1841 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1842 | TEST_ASSERT(client.ssl.state == MBEDTLS_SSL_HANDSHAKE_OVER); |
| 1843 | TEST_ASSERT(server.ssl.state == MBEDTLS_SSL_HANDSHAKE_OVER); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1844 | |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 1845 | /* Check that we agree on the version... */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1846 | TEST_ASSERT(client.ssl.minor_ver == server.ssl.minor_ver); |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 1847 | |
| 1848 | /* And check that the version negotiated is the expected one. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1849 | TEST_EQUAL(client.ssl.minor_ver, options->expected_negotiated_version); |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 1850 | |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 1851 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1852 | if (options->resize_buffers != 0) { |
| 1853 | if (options->expected_negotiated_version != MBEDTLS_SSL_MINOR_VERSION_0 && |
| 1854 | options->expected_negotiated_version != MBEDTLS_SSL_MINOR_VERSION_1) { |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 1855 | /* A server, when using DTLS, might delay a buffer resize to happen |
| 1856 | * after it receives a message, so we force it. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1857 | TEST_ASSERT(exchange_data(&(client.ssl), &(server.ssl)) == 0); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 1858 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1859 | TEST_ASSERT(client.ssl.out_buf_len == |
| 1860 | mbedtls_ssl_get_output_buflen(&client.ssl)); |
| 1861 | TEST_ASSERT(client.ssl.in_buf_len == |
| 1862 | mbedtls_ssl_get_input_buflen(&client.ssl)); |
| 1863 | TEST_ASSERT(server.ssl.out_buf_len == |
| 1864 | mbedtls_ssl_get_output_buflen(&server.ssl)); |
| 1865 | TEST_ASSERT(server.ssl.in_buf_len == |
| 1866 | mbedtls_ssl_get_input_buflen(&server.ssl)); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 1867 | } |
| 1868 | } |
| 1869 | #endif |
| 1870 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1871 | if (options->cli_msg_len != 0 || options->srv_msg_len != 0) { |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1872 | /* Start data exchanging test */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1873 | TEST_ASSERT(mbedtls_exchange_data(&(client.ssl), options->cli_msg_len, |
| 1874 | options->expected_cli_fragments, |
| 1875 | &(server.ssl), options->srv_msg_len, |
| 1876 | options->expected_srv_fragments) |
| 1877 | == 0); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1878 | } |
| 1879 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1880 | if (options->serialize == 1) { |
| 1881 | TEST_ASSERT(options->dtls == 1); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1882 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1883 | TEST_ASSERT(mbedtls_ssl_context_save(&(server.ssl), NULL, |
| 1884 | 0, &context_buf_len) |
| 1885 | == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL); |
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 | context_buf = mbedtls_calloc(1, context_buf_len); |
| 1888 | TEST_ASSERT(context_buf != NULL); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1889 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1890 | TEST_ASSERT(mbedtls_ssl_context_save(&(server.ssl), context_buf, |
| 1891 | context_buf_len, |
| 1892 | &context_buf_len) == 0); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1893 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1894 | mbedtls_ssl_free(&(server.ssl)); |
| 1895 | mbedtls_ssl_init(&(server.ssl)); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1896 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1897 | TEST_ASSERT(mbedtls_ssl_setup(&(server.ssl), &(server.conf)) == 0); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1898 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1899 | mbedtls_ssl_set_bio(&(server.ssl), &server_context, |
| 1900 | mbedtls_mock_tcp_send_msg, |
| 1901 | mbedtls_mock_tcp_recv_msg, |
| 1902 | NULL); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1903 | |
| 1904 | #if defined(MBEDTLS_TIMING_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1905 | mbedtls_ssl_set_timer_cb(&server.ssl, &timer_server, |
| 1906 | mbedtls_timing_set_delay, |
| 1907 | mbedtls_timing_get_delay); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1908 | #endif |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 1909 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1910 | if (options->resize_buffers != 0) { |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 1911 | /* Ensure that the buffer sizes are appropriate before resizes */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1912 | TEST_ASSERT(server.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN); |
| 1913 | TEST_ASSERT(server.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 1914 | } |
| 1915 | #endif |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1916 | TEST_ASSERT(mbedtls_ssl_context_load(&(server.ssl), context_buf, |
| 1917 | context_buf_len) == 0); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1918 | |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 1919 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1920 | /* Validate buffer sizes after context deserialization */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1921 | if (options->resize_buffers != 0) { |
| 1922 | TEST_ASSERT(server.ssl.out_buf_len == |
| 1923 | mbedtls_ssl_get_output_buflen(&server.ssl)); |
| 1924 | TEST_ASSERT(server.ssl.in_buf_len == |
| 1925 | mbedtls_ssl_get_input_buflen(&server.ssl)); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 1926 | } |
| 1927 | #endif |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1928 | /* Retest writing/reading */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1929 | if (options->cli_msg_len != 0 || options->srv_msg_len != 0) { |
| 1930 | TEST_ASSERT(mbedtls_exchange_data(&(client.ssl), |
| 1931 | options->cli_msg_len, |
| 1932 | options->expected_cli_fragments, |
| 1933 | &(server.ssl), |
| 1934 | options->srv_msg_len, |
| 1935 | options->expected_srv_fragments) |
| 1936 | == 0); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1937 | } |
| 1938 | } |
| 1939 | #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */ |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 1940 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1941 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1942 | if (options->renegotiate) { |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1943 | /* Start test with renegotiation */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1944 | TEST_ASSERT(server.ssl.renego_status == |
| 1945 | MBEDTLS_SSL_INITIAL_HANDSHAKE); |
| 1946 | TEST_ASSERT(client.ssl.renego_status == |
| 1947 | MBEDTLS_SSL_INITIAL_HANDSHAKE); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1948 | |
| 1949 | /* After calling this function for the server, it only sends a handshake |
| 1950 | * request. All renegotiation should happen during data exchanging */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1951 | TEST_ASSERT(mbedtls_ssl_renegotiate(&(server.ssl)) == 0); |
| 1952 | TEST_ASSERT(server.ssl.renego_status == |
| 1953 | MBEDTLS_SSL_RENEGOTIATION_PENDING); |
| 1954 | TEST_ASSERT(client.ssl.renego_status == |
| 1955 | MBEDTLS_SSL_INITIAL_HANDSHAKE); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1956 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1957 | TEST_ASSERT(exchange_data(&(client.ssl), &(server.ssl)) == 0); |
| 1958 | TEST_ASSERT(server.ssl.renego_status == |
| 1959 | MBEDTLS_SSL_RENEGOTIATION_DONE); |
| 1960 | TEST_ASSERT(client.ssl.renego_status == |
| 1961 | MBEDTLS_SSL_RENEGOTIATION_DONE); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1962 | |
| 1963 | /* After calling mbedtls_ssl_renegotiate for the client all renegotiation |
| 1964 | * should happen inside this function. However in this test, we cannot |
Shaun Case | 0e7791f | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 1965 | * perform simultaneous communication between client and server so this |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1966 | * function will return waiting error on the socket. All rest of |
| 1967 | * renegotiation should happen during data exchanging */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1968 | ret = mbedtls_ssl_renegotiate(&(client.ssl)); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 1969 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1970 | if (options->resize_buffers != 0) { |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 1971 | /* Ensure that the buffer sizes are appropriate before resizes */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1972 | TEST_ASSERT(client.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN); |
| 1973 | TEST_ASSERT(client.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 1974 | } |
| 1975 | #endif |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1976 | TEST_ASSERT(ret == 0 || |
| 1977 | ret == MBEDTLS_ERR_SSL_WANT_READ || |
| 1978 | ret == MBEDTLS_ERR_SSL_WANT_WRITE); |
| 1979 | TEST_ASSERT(server.ssl.renego_status == |
| 1980 | MBEDTLS_SSL_RENEGOTIATION_DONE); |
| 1981 | TEST_ASSERT(client.ssl.renego_status == |
| 1982 | MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 1983 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1984 | TEST_ASSERT(exchange_data(&(client.ssl), &(server.ssl)) == 0); |
| 1985 | TEST_ASSERT(server.ssl.renego_status == |
| 1986 | MBEDTLS_SSL_RENEGOTIATION_DONE); |
| 1987 | TEST_ASSERT(client.ssl.renego_status == |
| 1988 | MBEDTLS_SSL_RENEGOTIATION_DONE); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 1989 | #if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH) |
| 1990 | /* Validate buffer sizes after renegotiation */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1991 | if (options->resize_buffers != 0) { |
| 1992 | TEST_ASSERT(client.ssl.out_buf_len == |
| 1993 | mbedtls_ssl_get_output_buflen(&client.ssl)); |
| 1994 | TEST_ASSERT(client.ssl.in_buf_len == |
| 1995 | mbedtls_ssl_get_input_buflen(&client.ssl)); |
| 1996 | TEST_ASSERT(server.ssl.out_buf_len == |
| 1997 | mbedtls_ssl_get_output_buflen(&server.ssl)); |
| 1998 | TEST_ASSERT(server.ssl.in_buf_len == |
| 1999 | mbedtls_ssl_get_input_buflen(&server.ssl)); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2000 | } |
| 2001 | #endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */ |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2002 | } |
| 2003 | #endif /* MBEDTLS_SSL_RENEGOTIATION */ |
| 2004 | |
| 2005 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2006 | mbedtls_endpoint_free(&client, options->dtls != 0 ? &client_context : NULL); |
| 2007 | mbedtls_endpoint_free(&server, options->dtls != 0 ? &server_context : NULL); |
| 2008 | #if defined(MBEDTLS_DEBUG_C) |
| 2009 | if (options->cli_log_fun || options->srv_log_fun) { |
| 2010 | mbedtls_debug_set_threshold(0); |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 2011 | } |
| 2012 | #endif |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2013 | #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2014 | if (context_buf != NULL) { |
| 2015 | mbedtls_free(context_buf); |
| 2016 | } |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2017 | #endif |
| 2018 | } |
Gilles Peskine | 33d03fe | 2023-02-01 18:37:49 +0100 | [diff] [blame] | 2019 | #endif \ |
| 2020 | /* 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] | 2021 | |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 2022 | /* END_HEADER */ |
| 2023 | |
| 2024 | /* BEGIN_DEPENDENCIES |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2025 | * depends_on:MBEDTLS_SSL_TLS_C |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 2026 | * END_DEPENDENCIES |
| 2027 | */ |
| 2028 | |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2029 | /* BEGIN_CASE */ |
| 2030 | void test_callback_buffer_sanity() |
| 2031 | { |
| 2032 | enum { MSGLEN = 10 }; |
| 2033 | mbedtls_test_buffer buf; |
| 2034 | unsigned char input[MSGLEN]; |
| 2035 | unsigned char output[MSGLEN]; |
| 2036 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2037 | memset(input, 0, sizeof(input)); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2038 | |
| 2039 | /* Make sure calling put and get on NULL buffer results in error. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2040 | TEST_ASSERT(mbedtls_test_buffer_put(NULL, input, sizeof(input)) |
| 2041 | == -1); |
| 2042 | TEST_ASSERT(mbedtls_test_buffer_get(NULL, output, sizeof(output)) |
| 2043 | == -1); |
| 2044 | TEST_ASSERT(mbedtls_test_buffer_put(NULL, 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(NULL, NULL, 0) == -1); |
| 2047 | TEST_ASSERT(mbedtls_test_buffer_get(NULL, NULL, 0) == -1); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2048 | |
| 2049 | /* 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] | 2050 | * in error. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2051 | mbedtls_test_buffer_init(&buf); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2052 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2053 | TEST_ASSERT(mbedtls_test_buffer_put(&buf, input, sizeof(input)) == -1); |
| 2054 | TEST_ASSERT(mbedtls_test_buffer_get(&buf, output, sizeof(output)) |
| 2055 | == -1); |
| 2056 | TEST_ASSERT(mbedtls_test_buffer_put(&buf, NULL, sizeof(input)) == -1); |
Andrzej Kurek | f777414 | 2020-01-22 06:34:59 -0500 | [diff] [blame] | 2057 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2058 | TEST_ASSERT(mbedtls_test_buffer_put(&buf, NULL, 0) == -1); |
| 2059 | TEST_ASSERT(mbedtls_test_buffer_get(&buf, NULL, 0) == -1); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2060 | |
Andrzej Kurek | f777414 | 2020-01-22 06:34:59 -0500 | [diff] [blame] | 2061 | /* Make sure calling put and get on NULL input only results in |
| 2062 | * error if the length is not zero, and that a NULL output is valid for data |
| 2063 | * dropping. |
| 2064 | */ |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2065 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2066 | TEST_ASSERT(mbedtls_test_buffer_setup(&buf, sizeof(input)) == 0); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2067 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2068 | TEST_ASSERT(mbedtls_test_buffer_put(&buf, NULL, sizeof(input)) == -1); |
| 2069 | TEST_ASSERT(mbedtls_test_buffer_get(&buf, NULL, sizeof(output)) |
| 2070 | == 0); |
| 2071 | TEST_ASSERT(mbedtls_test_buffer_put(&buf, NULL, 0) == 0); |
| 2072 | TEST_ASSERT(mbedtls_test_buffer_get(&buf, NULL, 0) == 0); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2073 | |
Piotr Nowicki | fb437d7 | 2020-01-13 16:59:12 +0100 | [diff] [blame] | 2074 | /* Make sure calling put several times in the row is safe */ |
| 2075 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2076 | TEST_ASSERT(mbedtls_test_buffer_put(&buf, input, sizeof(input)) |
| 2077 | == sizeof(input)); |
| 2078 | TEST_ASSERT(mbedtls_test_buffer_get(&buf, output, 2) == 2); |
| 2079 | TEST_ASSERT(mbedtls_test_buffer_put(&buf, input, 1) == 1); |
| 2080 | TEST_ASSERT(mbedtls_test_buffer_put(&buf, input, 2) == 1); |
| 2081 | TEST_ASSERT(mbedtls_test_buffer_put(&buf, input, 2) == 0); |
Piotr Nowicki | fb437d7 | 2020-01-13 16:59:12 +0100 | [diff] [blame] | 2082 | |
| 2083 | |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2084 | exit: |
| 2085 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2086 | mbedtls_test_buffer_free(&buf); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2087 | } |
| 2088 | /* END_CASE */ |
| 2089 | |
| 2090 | /* |
| 2091 | * Test if the implementation of `mbedtls_test_buffer` related functions is |
| 2092 | * correct and works as expected. |
| 2093 | * |
| 2094 | * That is |
| 2095 | * - If we try to put in \p put1 bytes then we can put in \p put1_ret bytes. |
| 2096 | * - Afterwards if we try to get \p get1 bytes then we can get \get1_ret bytes. |
| 2097 | * - Next, if we try to put in \p put1 bytes then we can put in \p put1_ret |
| 2098 | * bytes. |
| 2099 | * - Afterwards if we try to get \p get1 bytes then we can get \get1_ret bytes. |
| 2100 | * - All of the bytes we got match the bytes we put in in a FIFO manner. |
| 2101 | */ |
| 2102 | |
| 2103 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2104 | void test_callback_buffer(int size, int put1, int put1_ret, |
| 2105 | int get1, int get1_ret, int put2, int put2_ret, |
| 2106 | int get2, int get2_ret) |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2107 | { |
| 2108 | enum { ROUNDS = 2 }; |
| 2109 | size_t put[ROUNDS]; |
| 2110 | int put_ret[ROUNDS]; |
| 2111 | size_t get[ROUNDS]; |
| 2112 | int get_ret[ROUNDS]; |
| 2113 | mbedtls_test_buffer buf; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2114 | unsigned char *input = NULL; |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2115 | size_t input_len; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2116 | unsigned char *output = NULL; |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2117 | size_t output_len; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2118 | size_t i, j, written, read; |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2119 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2120 | mbedtls_test_buffer_init(&buf); |
| 2121 | TEST_ASSERT(mbedtls_test_buffer_setup(&buf, size) == 0); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2122 | |
| 2123 | /* Check the sanity of input parameters and initialise local variables. That |
| 2124 | * is, ensure that the amount of data is not negative and that we are not |
| 2125 | * expecting more to put or get than we actually asked for. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2126 | TEST_ASSERT(put1 >= 0); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2127 | put[0] = put1; |
| 2128 | put_ret[0] = put1_ret; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2129 | TEST_ASSERT(put1_ret <= put1); |
| 2130 | TEST_ASSERT(put2 >= 0); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2131 | put[1] = put2; |
| 2132 | put_ret[1] = put2_ret; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2133 | TEST_ASSERT(put2_ret <= put2); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2134 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2135 | TEST_ASSERT(get1 >= 0); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2136 | get[0] = get1; |
| 2137 | get_ret[0] = get1_ret; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2138 | TEST_ASSERT(get1_ret <= get1); |
| 2139 | TEST_ASSERT(get2 >= 0); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2140 | get[1] = get2; |
| 2141 | get_ret[1] = get2_ret; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2142 | TEST_ASSERT(get2_ret <= get2); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2143 | |
| 2144 | input_len = 0; |
| 2145 | /* Calculate actual input and output lengths */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2146 | for (j = 0; j < ROUNDS; j++) { |
| 2147 | if (put_ret[j] > 0) { |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2148 | input_len += put_ret[j]; |
| 2149 | } |
| 2150 | } |
| 2151 | /* In order to always have a valid pointer we always allocate at least 1 |
| 2152 | * byte. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2153 | if (input_len == 0) { |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2154 | input_len = 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2155 | } |
| 2156 | ASSERT_ALLOC(input, input_len); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2157 | |
| 2158 | output_len = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2159 | for (j = 0; j < ROUNDS; j++) { |
| 2160 | if (get_ret[j] > 0) { |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2161 | output_len += get_ret[j]; |
| 2162 | } |
| 2163 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2164 | TEST_ASSERT(output_len <= input_len); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2165 | /* In order to always have a valid pointer we always allocate at least 1 |
| 2166 | * byte. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2167 | if (output_len == 0) { |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2168 | output_len = 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2169 | } |
| 2170 | ASSERT_ALLOC(output, output_len); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2171 | |
| 2172 | /* Fill up the buffer with structured data so that unwanted changes |
| 2173 | * can be detected */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2174 | for (i = 0; i < input_len; i++) { |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2175 | input[i] = i & 0xFF; |
| 2176 | } |
| 2177 | |
| 2178 | written = read = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2179 | for (j = 0; j < ROUNDS; j++) { |
| 2180 | TEST_ASSERT(put_ret[j] == mbedtls_test_buffer_put(&buf, |
| 2181 | input + written, put[j])); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2182 | written += put_ret[j]; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2183 | TEST_ASSERT(get_ret[j] == mbedtls_test_buffer_get(&buf, |
| 2184 | output + read, get[j])); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2185 | read += get_ret[j]; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2186 | TEST_ASSERT(read <= written); |
| 2187 | if (get_ret[j] > 0) { |
| 2188 | TEST_ASSERT(memcmp(output + read - get_ret[j], |
| 2189 | input + read - get_ret[j], get_ret[j]) |
| 2190 | == 0); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2191 | } |
| 2192 | } |
| 2193 | |
| 2194 | exit: |
| 2195 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2196 | mbedtls_free(input); |
| 2197 | mbedtls_free(output); |
| 2198 | mbedtls_test_buffer_free(&buf); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 2199 | } |
| 2200 | /* END_CASE */ |
| 2201 | |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2202 | /* |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2203 | * Test if the implementation of `mbedtls_mock_socket` related I/O functions is |
| 2204 | * correct and works as expected on unconnected sockets. |
| 2205 | */ |
| 2206 | |
| 2207 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2208 | void ssl_mock_sanity() |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2209 | { |
| 2210 | enum { MSGLEN = 105 }; |
Paul Elliott | 9545786 | 2021-11-24 16:54:26 +0000 | [diff] [blame] | 2211 | unsigned char message[MSGLEN] = { 0 }; |
| 2212 | unsigned char received[MSGLEN] = { 0 }; |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2213 | mbedtls_mock_socket socket; |
| 2214 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2215 | mbedtls_mock_socket_init(&socket); |
| 2216 | TEST_ASSERT(mbedtls_mock_tcp_send_b(&socket, message, MSGLEN) < 0); |
| 2217 | mbedtls_mock_socket_close(&socket); |
| 2218 | mbedtls_mock_socket_init(&socket); |
| 2219 | TEST_ASSERT(mbedtls_mock_tcp_recv_b(&socket, received, MSGLEN) < 0); |
| 2220 | mbedtls_mock_socket_close(&socket); |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2221 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2222 | mbedtls_mock_socket_init(&socket); |
| 2223 | TEST_ASSERT(mbedtls_mock_tcp_send_nb(&socket, message, MSGLEN) < 0); |
| 2224 | mbedtls_mock_socket_close(&socket); |
| 2225 | mbedtls_mock_socket_init(&socket); |
| 2226 | TEST_ASSERT(mbedtls_mock_tcp_recv_nb(&socket, received, MSGLEN) < 0); |
| 2227 | mbedtls_mock_socket_close(&socket); |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2228 | |
| 2229 | exit: |
| 2230 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2231 | mbedtls_mock_socket_close(&socket); |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2232 | } |
| 2233 | /* END_CASE */ |
| 2234 | |
| 2235 | /* |
| 2236 | * Test if the implementation of `mbedtls_mock_socket` related functions can |
| 2237 | * send a single message from the client to the server. |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2238 | */ |
| 2239 | |
| 2240 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2241 | void ssl_mock_tcp(int blocking) |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2242 | { |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2243 | enum { MSGLEN = 105 }; |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2244 | enum { BUFLEN = MSGLEN / 5 }; |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2245 | unsigned char message[MSGLEN]; |
| 2246 | unsigned char received[MSGLEN]; |
| 2247 | mbedtls_mock_socket client; |
| 2248 | mbedtls_mock_socket server; |
| 2249 | size_t written, read; |
| 2250 | int send_ret, recv_ret; |
| 2251 | mbedtls_ssl_send_t *send; |
| 2252 | mbedtls_ssl_recv_t *recv; |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2253 | unsigned i; |
| 2254 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2255 | if (blocking == 0) { |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2256 | send = mbedtls_mock_tcp_send_nb; |
| 2257 | recv = mbedtls_mock_tcp_recv_nb; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2258 | } else { |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2259 | send = mbedtls_mock_tcp_send_b; |
| 2260 | recv = mbedtls_mock_tcp_recv_b; |
| 2261 | } |
| 2262 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2263 | mbedtls_mock_socket_init(&client); |
| 2264 | mbedtls_mock_socket_init(&server); |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2265 | |
| 2266 | /* Fill up the buffer with structured data so that unwanted changes |
| 2267 | * can be detected */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2268 | for (i = 0; i < MSGLEN; i++) { |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2269 | message[i] = i & 0xFF; |
| 2270 | } |
| 2271 | |
| 2272 | /* Make sure that sending a message takes a few iterations. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2273 | TEST_ASSERT(0 == mbedtls_mock_socket_connect(&client, &server, BUFLEN)); |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2274 | |
| 2275 | /* Send the message to the server */ |
| 2276 | send_ret = recv_ret = 1; |
| 2277 | written = read = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2278 | while (send_ret != 0 || recv_ret != 0) { |
| 2279 | send_ret = send(&client, message + written, MSGLEN - written); |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2280 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2281 | TEST_ASSERT(send_ret >= 0); |
| 2282 | TEST_ASSERT(send_ret <= BUFLEN); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2283 | written += send_ret; |
| 2284 | |
| 2285 | /* 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] | 2286 | if (send_ret == BUFLEN) { |
| 2287 | int blocking_ret = send(&client, message, 1); |
| 2288 | if (blocking) { |
| 2289 | TEST_ASSERT(blocking_ret == 0); |
| 2290 | } else { |
| 2291 | TEST_ASSERT(blocking_ret == MBEDTLS_ERR_SSL_WANT_WRITE); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2292 | } |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2293 | } |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2294 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2295 | recv_ret = recv(&server, received + read, MSGLEN - read); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2296 | |
| 2297 | /* The result depends on whether any data was sent */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2298 | if (send_ret > 0) { |
| 2299 | TEST_ASSERT(recv_ret > 0); |
| 2300 | TEST_ASSERT(recv_ret <= BUFLEN); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2301 | read += recv_ret; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2302 | } else if (blocking) { |
| 2303 | TEST_ASSERT(recv_ret == 0); |
| 2304 | } else { |
| 2305 | TEST_ASSERT(recv_ret == MBEDTLS_ERR_SSL_WANT_READ); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2306 | recv_ret = 0; |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2307 | } |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2308 | |
| 2309 | /* 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] | 2310 | if (recv_ret == BUFLEN) { |
| 2311 | int blocking_ret = recv(&server, received, 1); |
| 2312 | if (blocking) { |
| 2313 | TEST_ASSERT(blocking_ret == 0); |
| 2314 | } else { |
| 2315 | TEST_ASSERT(blocking_ret == MBEDTLS_ERR_SSL_WANT_READ); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2316 | } |
| 2317 | } |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2318 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2319 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2320 | |
| 2321 | exit: |
| 2322 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2323 | mbedtls_mock_socket_close(&client); |
| 2324 | mbedtls_mock_socket_close(&server); |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2325 | } |
| 2326 | /* END_CASE */ |
| 2327 | |
| 2328 | /* |
| 2329 | * Test if the implementation of `mbedtls_mock_socket` related functions can |
| 2330 | * send messages in both direction at the same time (with the I/O calls |
| 2331 | * interleaving). |
| 2332 | */ |
| 2333 | |
| 2334 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2335 | void ssl_mock_tcp_interleaving(int blocking) |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 2336 | { |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2337 | enum { ROUNDS = 2 }; |
| 2338 | enum { MSGLEN = 105 }; |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2339 | enum { BUFLEN = MSGLEN / 5 }; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2340 | unsigned char message[ROUNDS][MSGLEN]; |
| 2341 | unsigned char received[ROUNDS][MSGLEN]; |
| 2342 | mbedtls_mock_socket client; |
| 2343 | mbedtls_mock_socket server; |
| 2344 | size_t written[ROUNDS]; |
| 2345 | size_t read[ROUNDS]; |
| 2346 | int send_ret[ROUNDS]; |
| 2347 | int recv_ret[ROUNDS]; |
| 2348 | unsigned i, j, progress; |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 2349 | mbedtls_ssl_send_t *send; |
| 2350 | mbedtls_ssl_recv_t *recv; |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 2351 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2352 | if (blocking == 0) { |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 2353 | send = mbedtls_mock_tcp_send_nb; |
| 2354 | recv = mbedtls_mock_tcp_recv_nb; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2355 | } else { |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 2356 | send = mbedtls_mock_tcp_send_b; |
| 2357 | recv = mbedtls_mock_tcp_recv_b; |
| 2358 | } |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2359 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2360 | mbedtls_mock_socket_init(&client); |
| 2361 | mbedtls_mock_socket_init(&server); |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2362 | |
| 2363 | /* Fill up the buffers with structured data so that unwanted changes |
| 2364 | * can be detected */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2365 | for (i = 0; i < ROUNDS; i++) { |
| 2366 | for (j = 0; j < MSGLEN; j++) { |
| 2367 | message[i][j] = (i * MSGLEN + j) & 0xFF; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2368 | } |
| 2369 | } |
| 2370 | |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2371 | /* Make sure that sending a message takes a few iterations. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2372 | TEST_ASSERT(0 == mbedtls_mock_socket_connect(&client, &server, BUFLEN)); |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2373 | |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2374 | /* Send the message from both sides, interleaving. */ |
| 2375 | progress = 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2376 | for (i = 0; i < ROUNDS; i++) { |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2377 | written[i] = 0; |
| 2378 | read[i] = 0; |
| 2379 | } |
| 2380 | /* This loop does not stop as long as there was a successful write or read |
| 2381 | * of at least one byte on either side. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2382 | while (progress != 0) { |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2383 | mbedtls_mock_socket *socket; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2384 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2385 | for (i = 0; i < ROUNDS; i++) { |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2386 | /* First sending is from the client */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2387 | socket = (i % 2 == 0) ? (&client) : (&server); |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2388 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2389 | send_ret[i] = send(socket, message[i] + written[i], |
| 2390 | MSGLEN - written[i]); |
| 2391 | TEST_ASSERT(send_ret[i] >= 0); |
| 2392 | TEST_ASSERT(send_ret[i] <= BUFLEN); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2393 | written[i] += send_ret[i]; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2394 | |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2395 | /* If the buffer is full we can test blocking and non-blocking |
| 2396 | * send */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2397 | if (send_ret[i] == BUFLEN) { |
| 2398 | int blocking_ret = send(socket, message[i], 1); |
| 2399 | if (blocking) { |
| 2400 | TEST_ASSERT(blocking_ret == 0); |
| 2401 | } else { |
| 2402 | TEST_ASSERT(blocking_ret == MBEDTLS_ERR_SSL_WANT_WRITE); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2403 | } |
| 2404 | } |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 2405 | } |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2406 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2407 | for (i = 0; i < ROUNDS; i++) { |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2408 | /* First receiving is from the server */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2409 | socket = (i % 2 == 0) ? (&server) : (&client); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2410 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2411 | recv_ret[i] = recv(socket, received[i] + read[i], |
| 2412 | MSGLEN - read[i]); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2413 | |
| 2414 | /* The result depends on whether any data was sent */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2415 | if (send_ret[i] > 0) { |
| 2416 | TEST_ASSERT(recv_ret[i] > 0); |
| 2417 | TEST_ASSERT(recv_ret[i] <= BUFLEN); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2418 | read[i] += recv_ret[i]; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2419 | } else if (blocking) { |
| 2420 | TEST_ASSERT(recv_ret[i] == 0); |
| 2421 | } else { |
| 2422 | TEST_ASSERT(recv_ret[i] == MBEDTLS_ERR_SSL_WANT_READ); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2423 | recv_ret[i] = 0; |
| 2424 | } |
| 2425 | |
| 2426 | /* If the buffer is empty we can test blocking and non-blocking |
| 2427 | * read */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2428 | if (recv_ret[i] == BUFLEN) { |
| 2429 | int blocking_ret = recv(socket, received[i], 1); |
| 2430 | if (blocking) { |
| 2431 | TEST_ASSERT(blocking_ret == 0); |
| 2432 | } else { |
| 2433 | TEST_ASSERT(blocking_ret == MBEDTLS_ERR_SSL_WANT_READ); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2434 | } |
| 2435 | } |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 2436 | } |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2437 | |
| 2438 | progress = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2439 | for (i = 0; i < ROUNDS; i++) { |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 2440 | progress += send_ret[i] + recv_ret[i]; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2441 | } |
| 2442 | } |
| 2443 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2444 | for (i = 0; i < ROUNDS; i++) { |
| 2445 | TEST_ASSERT(memcmp(message[i], received[i], MSGLEN) == 0); |
| 2446 | } |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2447 | |
| 2448 | exit: |
| 2449 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2450 | mbedtls_mock_socket_close(&client); |
| 2451 | mbedtls_mock_socket_close(&server); |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 2452 | } |
| 2453 | /* END_CASE */ |
| 2454 | |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2455 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2456 | void ssl_message_queue_sanity() |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2457 | { |
| 2458 | mbedtls_test_message_queue queue; |
| 2459 | |
| 2460 | /* Trying to push/pull to an empty queue */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2461 | TEST_ASSERT(mbedtls_test_message_queue_push_info(NULL, 1) |
| 2462 | == MBEDTLS_TEST_ERROR_ARG_NULL); |
| 2463 | TEST_ASSERT(mbedtls_test_message_queue_pop_info(NULL, 1) |
| 2464 | == MBEDTLS_TEST_ERROR_ARG_NULL); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2465 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2466 | TEST_ASSERT(mbedtls_test_message_queue_setup(&queue, 3) == 0); |
| 2467 | TEST_ASSERT(queue.capacity == 3); |
| 2468 | TEST_ASSERT(queue.num == 0); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2469 | |
| 2470 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2471 | mbedtls_test_message_queue_free(&queue); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2472 | } |
| 2473 | /* END_CASE */ |
| 2474 | |
| 2475 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2476 | void ssl_message_queue_basic() |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2477 | { |
| 2478 | mbedtls_test_message_queue queue; |
| 2479 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2480 | TEST_ASSERT(mbedtls_test_message_queue_setup(&queue, 3) == 0); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2481 | |
| 2482 | /* Sanity test - 3 pushes and 3 pops with sufficient space */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2483 | TEST_ASSERT(mbedtls_test_message_queue_push_info(&queue, 1) == 1); |
| 2484 | TEST_ASSERT(queue.capacity == 3); |
| 2485 | TEST_ASSERT(queue.num == 1); |
| 2486 | TEST_ASSERT(mbedtls_test_message_queue_push_info(&queue, 1) == 1); |
| 2487 | TEST_ASSERT(queue.capacity == 3); |
| 2488 | TEST_ASSERT(queue.num == 2); |
| 2489 | TEST_ASSERT(mbedtls_test_message_queue_push_info(&queue, 2) == 2); |
| 2490 | TEST_ASSERT(queue.capacity == 3); |
| 2491 | TEST_ASSERT(queue.num == 3); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2492 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2493 | TEST_ASSERT(mbedtls_test_message_queue_pop_info(&queue, 1) == 1); |
| 2494 | TEST_ASSERT(mbedtls_test_message_queue_pop_info(&queue, 1) == 1); |
| 2495 | TEST_ASSERT(mbedtls_test_message_queue_pop_info(&queue, 2) == 2); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2496 | |
| 2497 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2498 | mbedtls_test_message_queue_free(&queue); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2499 | } |
| 2500 | /* END_CASE */ |
| 2501 | |
| 2502 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2503 | void ssl_message_queue_overflow_underflow() |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2504 | { |
| 2505 | mbedtls_test_message_queue queue; |
| 2506 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2507 | TEST_ASSERT(mbedtls_test_message_queue_setup(&queue, 3) == 0); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2508 | |
| 2509 | /* 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] | 2510 | TEST_ASSERT(mbedtls_test_message_queue_push_info(&queue, 1) == 1); |
| 2511 | TEST_ASSERT(mbedtls_test_message_queue_push_info(&queue, 1) == 1); |
| 2512 | TEST_ASSERT(mbedtls_test_message_queue_push_info(&queue, 2) == 2); |
| 2513 | TEST_ASSERT(mbedtls_test_message_queue_push_info(&queue, 3) |
| 2514 | == MBEDTLS_ERR_SSL_WANT_WRITE); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2515 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2516 | TEST_ASSERT(mbedtls_test_message_queue_pop_info(&queue, 1) == 1); |
| 2517 | TEST_ASSERT(mbedtls_test_message_queue_pop_info(&queue, 1) == 1); |
| 2518 | TEST_ASSERT(mbedtls_test_message_queue_pop_info(&queue, 2) == 2); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2519 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2520 | TEST_ASSERT(mbedtls_test_message_queue_pop_info(&queue, 1) |
| 2521 | == MBEDTLS_ERR_SSL_WANT_READ); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2522 | |
| 2523 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2524 | mbedtls_test_message_queue_free(&queue); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2525 | } |
| 2526 | /* END_CASE */ |
| 2527 | |
| 2528 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2529 | void ssl_message_queue_interleaved() |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2530 | { |
| 2531 | mbedtls_test_message_queue queue; |
| 2532 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2533 | TEST_ASSERT(mbedtls_test_message_queue_setup(&queue, 3) == 0); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2534 | |
| 2535 | /* Interleaved test - [2 pushes, 1 pop] twice, and then two pops |
| 2536 | * (to wrap around the buffer) */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2537 | TEST_ASSERT(mbedtls_test_message_queue_push_info(&queue, 1) == 1); |
| 2538 | TEST_ASSERT(mbedtls_test_message_queue_push_info(&queue, 1) == 1); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2539 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2540 | TEST_ASSERT(mbedtls_test_message_queue_pop_info(&queue, 1) == 1); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2541 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2542 | TEST_ASSERT(mbedtls_test_message_queue_push_info(&queue, 2) == 2); |
| 2543 | TEST_ASSERT(mbedtls_test_message_queue_push_info(&queue, 3) == 3); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2544 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2545 | TEST_ASSERT(mbedtls_test_message_queue_pop_info(&queue, 1) == 1); |
| 2546 | TEST_ASSERT(mbedtls_test_message_queue_pop_info(&queue, 2) == 2); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2547 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2548 | TEST_ASSERT(mbedtls_test_message_queue_push_info(&queue, 5) == 5); |
| 2549 | TEST_ASSERT(mbedtls_test_message_queue_push_info(&queue, 8) == 8); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2550 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2551 | TEST_ASSERT(mbedtls_test_message_queue_pop_info(&queue, 3) == 3); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2552 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2553 | TEST_ASSERT(mbedtls_test_message_queue_pop_info(&queue, 5) == 5); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2554 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2555 | TEST_ASSERT(mbedtls_test_message_queue_pop_info(&queue, 8) == 8); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2556 | |
| 2557 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2558 | mbedtls_test_message_queue_free(&queue); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2559 | } |
| 2560 | /* END_CASE */ |
| 2561 | |
| 2562 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2563 | void ssl_message_queue_insufficient_buffer() |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2564 | { |
| 2565 | mbedtls_test_message_queue queue; |
| 2566 | size_t message_len = 10; |
| 2567 | size_t buffer_len = 5; |
| 2568 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2569 | TEST_ASSERT(mbedtls_test_message_queue_setup(&queue, 1) == 0); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2570 | |
| 2571 | /* Popping without a sufficient buffer */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2572 | TEST_ASSERT(mbedtls_test_message_queue_push_info(&queue, message_len) |
| 2573 | == (int) message_len); |
| 2574 | TEST_ASSERT(mbedtls_test_message_queue_pop_info(&queue, buffer_len) |
| 2575 | == (int) buffer_len); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2576 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2577 | mbedtls_test_message_queue_free(&queue); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 2578 | } |
| 2579 | /* END_CASE */ |
| 2580 | |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2581 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2582 | void ssl_message_mock_uninitialized() |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2583 | { |
| 2584 | enum { MSGLEN = 10 }; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2585 | unsigned char message[MSGLEN] = { 0 }, received[MSGLEN]; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2586 | mbedtls_mock_socket client, server; |
| 2587 | mbedtls_test_message_queue server_queue, client_queue; |
| 2588 | mbedtls_test_message_socket_context server_context, client_context; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2589 | mbedtls_message_socket_init(&server_context); |
| 2590 | mbedtls_message_socket_init(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2591 | |
| 2592 | /* Send with a NULL context */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2593 | TEST_ASSERT(mbedtls_mock_tcp_send_msg(NULL, message, MSGLEN) |
| 2594 | == MBEDTLS_TEST_ERROR_CONTEXT_ERROR); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2595 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2596 | TEST_ASSERT(mbedtls_mock_tcp_recv_msg(NULL, message, MSGLEN) |
| 2597 | == MBEDTLS_TEST_ERROR_CONTEXT_ERROR); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2598 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2599 | TEST_ASSERT(mbedtls_message_socket_setup(&server_queue, &client_queue, 1, |
| 2600 | &server, |
| 2601 | &server_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2602 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2603 | TEST_ASSERT(mbedtls_message_socket_setup(&client_queue, &server_queue, 1, |
| 2604 | &client, |
| 2605 | &client_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2606 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2607 | TEST_ASSERT(mbedtls_mock_tcp_send_msg(&client_context, message, MSGLEN) |
| 2608 | == MBEDTLS_TEST_ERROR_SEND_FAILED); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2609 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2610 | TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&server_context, received, MSGLEN) |
| 2611 | == MBEDTLS_ERR_SSL_WANT_READ); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2612 | |
| 2613 | /* Push directly to a queue to later simulate a disconnected behavior */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2614 | TEST_ASSERT(mbedtls_test_message_queue_push_info(&server_queue, MSGLEN) |
| 2615 | == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2616 | |
| 2617 | /* Test if there's an error when trying to read from a disconnected |
| 2618 | * socket */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2619 | TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&server_context, received, MSGLEN) |
| 2620 | == MBEDTLS_TEST_ERROR_RECV_FAILED); |
| 2621 | exit: |
| 2622 | mbedtls_message_socket_close(&server_context); |
| 2623 | mbedtls_message_socket_close(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2624 | } |
| 2625 | /* END_CASE */ |
| 2626 | |
| 2627 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2628 | void ssl_message_mock_basic() |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2629 | { |
| 2630 | enum { MSGLEN = 10 }; |
| 2631 | unsigned char message[MSGLEN], received[MSGLEN]; |
| 2632 | mbedtls_mock_socket client, server; |
| 2633 | unsigned i; |
| 2634 | mbedtls_test_message_queue server_queue, client_queue; |
| 2635 | mbedtls_test_message_socket_context server_context, client_context; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2636 | mbedtls_message_socket_init(&server_context); |
| 2637 | mbedtls_message_socket_init(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2638 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2639 | TEST_ASSERT(mbedtls_message_socket_setup(&server_queue, &client_queue, 1, |
| 2640 | &server, |
| 2641 | &server_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2642 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2643 | TEST_ASSERT(mbedtls_message_socket_setup(&client_queue, &server_queue, 1, |
| 2644 | &client, |
| 2645 | &client_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2646 | |
| 2647 | /* Fill up the buffer with structured data so that unwanted changes |
| 2648 | * can be detected */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2649 | for (i = 0; i < MSGLEN; i++) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2650 | message[i] = i & 0xFF; |
| 2651 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2652 | TEST_ASSERT(0 == mbedtls_mock_socket_connect(&client, &server, |
| 2653 | MSGLEN)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2654 | |
| 2655 | /* Send the message to the server */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2656 | TEST_ASSERT(mbedtls_mock_tcp_send_msg(&client_context, message, |
| 2657 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2658 | |
| 2659 | /* Read from the server */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2660 | TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&server_context, received, MSGLEN) |
| 2661 | == MSGLEN); |
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 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
| 2664 | memset(received, 0, MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2665 | |
| 2666 | /* Send the message to the client */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2667 | TEST_ASSERT(mbedtls_mock_tcp_send_msg(&server_context, message, |
| 2668 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2669 | |
| 2670 | /* Read from the client */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2671 | TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&client_context, received, MSGLEN) |
| 2672 | == MSGLEN); |
| 2673 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2674 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2675 | exit: |
| 2676 | mbedtls_message_socket_close(&server_context); |
| 2677 | mbedtls_message_socket_close(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2678 | } |
| 2679 | /* END_CASE */ |
| 2680 | |
| 2681 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2682 | void ssl_message_mock_queue_overflow_underflow() |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2683 | { |
| 2684 | enum { MSGLEN = 10 }; |
| 2685 | unsigned char message[MSGLEN], received[MSGLEN]; |
| 2686 | mbedtls_mock_socket client, server; |
| 2687 | unsigned i; |
| 2688 | mbedtls_test_message_queue server_queue, client_queue; |
| 2689 | mbedtls_test_message_socket_context server_context, client_context; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2690 | mbedtls_message_socket_init(&server_context); |
| 2691 | mbedtls_message_socket_init(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2692 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2693 | TEST_ASSERT(mbedtls_message_socket_setup(&server_queue, &client_queue, 2, |
| 2694 | &server, |
| 2695 | &server_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2696 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2697 | TEST_ASSERT(mbedtls_message_socket_setup(&client_queue, &server_queue, 2, |
| 2698 | &client, |
| 2699 | &client_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2700 | |
| 2701 | /* Fill up the buffer with structured data so that unwanted changes |
| 2702 | * can be detected */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2703 | for (i = 0; i < MSGLEN; i++) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2704 | message[i] = i & 0xFF; |
| 2705 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2706 | TEST_ASSERT(0 == mbedtls_mock_socket_connect(&client, &server, |
| 2707 | MSGLEN*2)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2708 | |
| 2709 | /* Send three message to the server, last one with an error */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2710 | TEST_ASSERT(mbedtls_mock_tcp_send_msg(&client_context, message, |
| 2711 | MSGLEN - 1) == MSGLEN - 1); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2712 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2713 | TEST_ASSERT(mbedtls_mock_tcp_send_msg(&client_context, message, |
| 2714 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2715 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2716 | TEST_ASSERT(mbedtls_mock_tcp_send_msg(&client_context, message, |
| 2717 | MSGLEN) |
| 2718 | == MBEDTLS_ERR_SSL_WANT_WRITE); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2719 | |
| 2720 | /* Read three messages from the server, last one with an error */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2721 | TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&server_context, received, |
| 2722 | MSGLEN - 1) == MSGLEN - 1); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2723 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2724 | TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&server_context, received, MSGLEN) |
| 2725 | == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2726 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2727 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2728 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2729 | TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&server_context, received, MSGLEN) |
| 2730 | == MBEDTLS_ERR_SSL_WANT_READ); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2731 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2732 | exit: |
| 2733 | mbedtls_message_socket_close(&server_context); |
| 2734 | mbedtls_message_socket_close(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2735 | } |
| 2736 | /* END_CASE */ |
| 2737 | |
| 2738 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2739 | void ssl_message_mock_socket_overflow() |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2740 | { |
| 2741 | enum { MSGLEN = 10 }; |
| 2742 | unsigned char message[MSGLEN], received[MSGLEN]; |
| 2743 | mbedtls_mock_socket client, server; |
| 2744 | unsigned i; |
| 2745 | mbedtls_test_message_queue server_queue, client_queue; |
| 2746 | mbedtls_test_message_socket_context server_context, client_context; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2747 | mbedtls_message_socket_init(&server_context); |
| 2748 | mbedtls_message_socket_init(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2749 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2750 | TEST_ASSERT(mbedtls_message_socket_setup(&server_queue, &client_queue, 2, |
| 2751 | &server, |
| 2752 | &server_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2753 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2754 | TEST_ASSERT(mbedtls_message_socket_setup(&client_queue, &server_queue, 2, |
| 2755 | &client, |
| 2756 | &client_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2757 | |
| 2758 | /* Fill up the buffer with structured data so that unwanted changes |
| 2759 | * can be detected */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2760 | for (i = 0; i < MSGLEN; i++) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2761 | message[i] = i & 0xFF; |
| 2762 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2763 | TEST_ASSERT(0 == mbedtls_mock_socket_connect(&client, &server, |
| 2764 | MSGLEN)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2765 | |
| 2766 | /* Send two message to the server, second one with an error */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2767 | TEST_ASSERT(mbedtls_mock_tcp_send_msg(&client_context, message, |
| 2768 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2769 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2770 | TEST_ASSERT(mbedtls_mock_tcp_send_msg(&client_context, message, |
| 2771 | MSGLEN) |
| 2772 | == MBEDTLS_TEST_ERROR_SEND_FAILED); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2773 | |
| 2774 | /* Read the only message from the server */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2775 | TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&server_context, received, MSGLEN) |
| 2776 | == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2777 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2778 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2779 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2780 | exit: |
| 2781 | mbedtls_message_socket_close(&server_context); |
| 2782 | mbedtls_message_socket_close(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2783 | } |
| 2784 | /* END_CASE */ |
| 2785 | |
| 2786 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2787 | void ssl_message_mock_truncated() |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2788 | { |
| 2789 | enum { MSGLEN = 10 }; |
| 2790 | unsigned char message[MSGLEN], received[MSGLEN]; |
| 2791 | mbedtls_mock_socket client, server; |
| 2792 | unsigned i; |
| 2793 | mbedtls_test_message_queue server_queue, client_queue; |
| 2794 | mbedtls_test_message_socket_context server_context, client_context; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2795 | mbedtls_message_socket_init(&server_context); |
| 2796 | mbedtls_message_socket_init(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2797 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2798 | TEST_ASSERT(mbedtls_message_socket_setup(&server_queue, &client_queue, 2, |
| 2799 | &server, |
| 2800 | &server_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2801 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2802 | TEST_ASSERT(mbedtls_message_socket_setup(&client_queue, &server_queue, 2, |
| 2803 | &client, |
| 2804 | &client_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2805 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2806 | memset(received, 0, MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2807 | /* Fill up the buffer with structured data so that unwanted changes |
| 2808 | * can be detected */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2809 | for (i = 0; i < MSGLEN; i++) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2810 | message[i] = i & 0xFF; |
| 2811 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2812 | TEST_ASSERT(0 == mbedtls_mock_socket_connect(&client, &server, |
| 2813 | 2 * MSGLEN)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2814 | |
| 2815 | /* Send two messages to the server, the second one small enough to fit in the |
| 2816 | * receiver's buffer. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2817 | TEST_ASSERT(mbedtls_mock_tcp_send_msg(&client_context, message, |
| 2818 | MSGLEN) == MSGLEN); |
| 2819 | TEST_ASSERT(mbedtls_mock_tcp_send_msg(&client_context, message, |
| 2820 | MSGLEN / 2) == MSGLEN / 2); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2821 | /* Read a truncated message from the server */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2822 | TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&server_context, received, MSGLEN/2) |
| 2823 | == MSGLEN/2); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2824 | |
| 2825 | /* 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] | 2826 | TEST_ASSERT(memcmp(message, received, MSGLEN/2) == 0); |
| 2827 | TEST_ASSERT(memcmp(message + MSGLEN/2, received + MSGLEN/2, MSGLEN/2) |
| 2828 | != 0); |
| 2829 | memset(received, 0, MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2830 | |
| 2831 | /* Read a full message from the server */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2832 | TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&server_context, received, MSGLEN/2) |
| 2833 | == MSGLEN / 2); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2834 | |
| 2835 | /* Test that the first half of the message is valid */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2836 | TEST_ASSERT(memcmp(message, received, MSGLEN/2) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2837 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2838 | exit: |
| 2839 | mbedtls_message_socket_close(&server_context); |
| 2840 | mbedtls_message_socket_close(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2841 | } |
| 2842 | /* END_CASE */ |
| 2843 | |
| 2844 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2845 | void ssl_message_mock_socket_read_error() |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2846 | { |
| 2847 | enum { MSGLEN = 10 }; |
| 2848 | unsigned char message[MSGLEN], received[MSGLEN]; |
| 2849 | mbedtls_mock_socket client, server; |
| 2850 | unsigned i; |
| 2851 | mbedtls_test_message_queue server_queue, client_queue; |
| 2852 | mbedtls_test_message_socket_context server_context, client_context; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2853 | mbedtls_message_socket_init(&server_context); |
| 2854 | mbedtls_message_socket_init(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2855 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2856 | TEST_ASSERT(mbedtls_message_socket_setup(&server_queue, &client_queue, 1, |
| 2857 | &server, |
| 2858 | &server_context) == 0); |
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_message_socket_setup(&client_queue, &server_queue, 1, |
| 2861 | &client, |
| 2862 | &client_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2863 | |
| 2864 | /* Fill up the buffer with structured data so that unwanted changes |
| 2865 | * can be detected */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2866 | for (i = 0; i < MSGLEN; i++) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2867 | message[i] = i & 0xFF; |
| 2868 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2869 | TEST_ASSERT(0 == mbedtls_mock_socket_connect(&client, &server, |
| 2870 | MSGLEN)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2871 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2872 | TEST_ASSERT(mbedtls_mock_tcp_send_msg(&client_context, message, |
| 2873 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2874 | |
| 2875 | /* Force a read error by disconnecting the socket by hand */ |
| 2876 | server.status = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2877 | TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&server_context, received, MSGLEN) |
| 2878 | == MBEDTLS_TEST_ERROR_RECV_FAILED); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2879 | /* Return to a valid state */ |
| 2880 | server.status = MBEDTLS_MOCK_SOCKET_CONNECTED; |
| 2881 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2882 | memset(received, 0, sizeof(received)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2883 | |
| 2884 | /* Test that even though the server tried to read once disconnected, the |
| 2885 | * continuity is preserved */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2886 | TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&server_context, received, MSGLEN) |
| 2887 | == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2888 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2889 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2890 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2891 | exit: |
| 2892 | mbedtls_message_socket_close(&server_context); |
| 2893 | mbedtls_message_socket_close(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2894 | } |
| 2895 | /* END_CASE */ |
| 2896 | |
| 2897 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2898 | void ssl_message_mock_interleaved_one_way() |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2899 | { |
| 2900 | enum { MSGLEN = 10 }; |
| 2901 | unsigned char message[MSGLEN], received[MSGLEN]; |
| 2902 | mbedtls_mock_socket client, server; |
| 2903 | unsigned i; |
| 2904 | mbedtls_test_message_queue server_queue, client_queue; |
| 2905 | mbedtls_test_message_socket_context server_context, client_context; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2906 | mbedtls_message_socket_init(&server_context); |
| 2907 | mbedtls_message_socket_init(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2908 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2909 | TEST_ASSERT(mbedtls_message_socket_setup(&server_queue, &client_queue, 3, |
| 2910 | &server, |
| 2911 | &server_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2912 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2913 | TEST_ASSERT(mbedtls_message_socket_setup(&client_queue, &server_queue, 3, |
| 2914 | &client, |
| 2915 | &client_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2916 | |
| 2917 | /* Fill up the buffer with structured data so that unwanted changes |
| 2918 | * can be detected */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2919 | for (i = 0; i < MSGLEN; i++) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2920 | message[i] = i & 0xFF; |
| 2921 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2922 | TEST_ASSERT(0 == mbedtls_mock_socket_connect(&client, &server, |
| 2923 | MSGLEN*3)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2924 | |
| 2925 | /* Interleaved test - [2 sends, 1 read] twice, and then two reads |
| 2926 | * (to wrap around the buffer) */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2927 | for (i = 0; i < 2; i++) { |
| 2928 | TEST_ASSERT(mbedtls_mock_tcp_send_msg(&client_context, message, |
| 2929 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2930 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2931 | TEST_ASSERT(mbedtls_mock_tcp_send_msg(&client_context, message, |
| 2932 | MSGLEN) == MSGLEN); |
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, |
| 2935 | MSGLEN) == MSGLEN); |
| 2936 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
| 2937 | memset(received, 0, sizeof(received)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2938 | } |
| 2939 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2940 | for (i = 0; i < 2; i++) { |
| 2941 | TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&server_context, received, |
| 2942 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2943 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2944 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2945 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2946 | TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&server_context, received, MSGLEN) |
| 2947 | == MBEDTLS_ERR_SSL_WANT_READ); |
| 2948 | exit: |
| 2949 | mbedtls_message_socket_close(&server_context); |
| 2950 | mbedtls_message_socket_close(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2951 | } |
| 2952 | /* END_CASE */ |
| 2953 | |
| 2954 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2955 | void ssl_message_mock_interleaved_two_ways() |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2956 | { |
| 2957 | enum { MSGLEN = 10 }; |
| 2958 | unsigned char message[MSGLEN], received[MSGLEN]; |
| 2959 | mbedtls_mock_socket client, server; |
| 2960 | unsigned i; |
| 2961 | mbedtls_test_message_queue server_queue, client_queue; |
| 2962 | mbedtls_test_message_socket_context server_context, client_context; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2963 | mbedtls_message_socket_init(&server_context); |
| 2964 | mbedtls_message_socket_init(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2965 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2966 | TEST_ASSERT(mbedtls_message_socket_setup(&server_queue, &client_queue, 3, |
| 2967 | &server, |
| 2968 | &server_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2969 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2970 | TEST_ASSERT(mbedtls_message_socket_setup(&client_queue, &server_queue, 3, |
| 2971 | &client, |
| 2972 | &client_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2973 | |
| 2974 | /* Fill up the buffer with structured data so that unwanted changes |
| 2975 | * can be detected */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2976 | for (i = 0; i < MSGLEN; i++) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2977 | message[i] = i & 0xFF; |
| 2978 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2979 | TEST_ASSERT(0 == mbedtls_mock_socket_connect(&client, &server, |
| 2980 | MSGLEN*3)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2981 | |
| 2982 | /* Interleaved test - [2 sends, 1 read] twice, both ways, and then two reads |
| 2983 | * (to wrap around the buffer) both ways. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2984 | for (i = 0; i < 2; i++) { |
| 2985 | TEST_ASSERT(mbedtls_mock_tcp_send_msg(&client_context, message, |
| 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(mbedtls_mock_tcp_send_msg(&client_context, message, |
| 2989 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2990 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2991 | TEST_ASSERT(mbedtls_mock_tcp_send_msg(&server_context, message, |
| 2992 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2993 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2994 | TEST_ASSERT(mbedtls_mock_tcp_send_msg(&server_context, message, |
| 2995 | MSGLEN) == MSGLEN); |
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 | TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&server_context, received, |
| 2998 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 2999 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3000 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3001 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3002 | memset(received, 0, sizeof(received)); |
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(mbedtls_mock_tcp_recv_msg(&client_context, received, |
| 3005 | MSGLEN) == MSGLEN); |
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(memcmp(message, received, MSGLEN) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3008 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3009 | memset(received, 0, sizeof(received)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3010 | } |
| 3011 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3012 | for (i = 0; i < 2; i++) { |
| 3013 | TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&server_context, received, |
| 3014 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3015 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3016 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
| 3017 | memset(received, 0, sizeof(received)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3018 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3019 | TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&client_context, received, |
| 3020 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3021 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3022 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
| 3023 | memset(received, 0, sizeof(received)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3024 | } |
| 3025 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3026 | TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&server_context, received, MSGLEN) |
| 3027 | == MBEDTLS_ERR_SSL_WANT_READ); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3028 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3029 | TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&client_context, received, MSGLEN) |
| 3030 | == MBEDTLS_ERR_SSL_WANT_READ); |
| 3031 | exit: |
| 3032 | mbedtls_message_socket_close(&server_context); |
| 3033 | mbedtls_message_socket_close(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 3034 | } |
| 3035 | /* END_CASE */ |
| 3036 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3037 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3038 | 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] | 3039 | { |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 3040 | uint32_t len = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 3041 | mbedtls_ssl_context ssl; |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 3042 | mbedtls_ssl_config conf; |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3043 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3044 | mbedtls_ssl_init(&ssl); |
| 3045 | mbedtls_ssl_config_init(&conf); |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 3046 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3047 | TEST_ASSERT(mbedtls_ssl_config_defaults(&conf, |
| 3048 | MBEDTLS_SSL_IS_CLIENT, |
| 3049 | MBEDTLS_SSL_TRANSPORT_DATAGRAM, |
| 3050 | MBEDTLS_SSL_PRESET_DEFAULT) == 0); |
| 3051 | TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0); |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3052 | |
| 3053 | /* Read previous record numbers */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3054 | for (len = 0; len < prevs->len; len += 6) { |
| 3055 | memcpy(ssl.in_ctr + 2, prevs->x + len, 6); |
| 3056 | mbedtls_ssl_dtls_replay_update(&ssl); |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3057 | } |
| 3058 | |
| 3059 | /* Check new number */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3060 | memcpy(ssl.in_ctr + 2, new->x, 6); |
| 3061 | TEST_ASSERT(mbedtls_ssl_dtls_replay_check(&ssl) == ret); |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3062 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3063 | mbedtls_ssl_free(&ssl); |
| 3064 | mbedtls_ssl_config_free(&conf); |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 3065 | } |
| 3066 | /* END_CASE */ |
Hanno Becker | b25c0c7 | 2017-05-05 11:24:30 +0100 | [diff] [blame] | 3067 | |
Gilles Peskine | 33d03fe | 2023-02-01 18:37:49 +0100 | [diff] [blame] | 3068 | /* 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] | 3069 | void ssl_set_hostname_twice(char *hostname0, char *hostname1) |
Hanno Becker | b25c0c7 | 2017-05-05 11:24:30 +0100 | [diff] [blame] | 3070 | { |
| 3071 | mbedtls_ssl_context ssl; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3072 | mbedtls_ssl_init(&ssl); |
Hanno Becker | b25c0c7 | 2017-05-05 11:24:30 +0100 | [diff] [blame] | 3073 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3074 | TEST_ASSERT(mbedtls_ssl_set_hostname(&ssl, hostname0) == 0); |
| 3075 | TEST_ASSERT(mbedtls_ssl_set_hostname(&ssl, hostname1) == 0); |
Hanno Becker | b25c0c7 | 2017-05-05 11:24:30 +0100 | [diff] [blame] | 3076 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3077 | mbedtls_ssl_free(&ssl); |
Hanno Becker | b25c0c7 | 2017-05-05 11:24:30 +0100 | [diff] [blame] | 3078 | } |
Darryl Green | 11999bb | 2018-03-13 15:22:58 +0000 | [diff] [blame] | 3079 | /* END_CASE */ |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3080 | |
| 3081 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3082 | void ssl_crypt_record(int cipher_type, int hash_id, |
| 3083 | int etm, int tag_mode, int ver, |
| 3084 | int cid0_len, int cid1_len) |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3085 | { |
| 3086 | /* |
| 3087 | * Test several record encryptions and decryptions |
| 3088 | * with plenty of space before and after the data |
| 3089 | * within the record buffer. |
| 3090 | */ |
| 3091 | |
| 3092 | int ret; |
| 3093 | int num_records = 16; |
| 3094 | mbedtls_ssl_context ssl; /* ONLY for debugging */ |
| 3095 | |
| 3096 | mbedtls_ssl_transform t0, t1; |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 3097 | unsigned char *buf = NULL; |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3098 | size_t const buflen = 512; |
| 3099 | mbedtls_record rec, rec_backup; |
| 3100 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3101 | mbedtls_ssl_init(&ssl); |
| 3102 | mbedtls_ssl_transform_init(&t0); |
| 3103 | mbedtls_ssl_transform_init(&t1); |
| 3104 | TEST_ASSERT(build_transforms(&t0, &t1, cipher_type, hash_id, |
| 3105 | etm, tag_mode, ver, |
| 3106 | (size_t) cid0_len, |
| 3107 | (size_t) cid1_len) == 0); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3108 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3109 | TEST_ASSERT((buf = mbedtls_calloc(1, buflen)) != NULL); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3110 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3111 | while (num_records-- > 0) { |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3112 | mbedtls_ssl_transform *t_dec, *t_enc; |
| 3113 | /* Take turns in who's sending and who's receiving. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3114 | if (num_records % 3 == 0) { |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3115 | t_dec = &t0; |
| 3116 | t_enc = &t1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3117 | } else { |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3118 | t_dec = &t1; |
| 3119 | t_enc = &t0; |
| 3120 | } |
| 3121 | |
| 3122 | /* |
| 3123 | * The record header affects the transformation in two ways: |
| 3124 | * 1) It determines the AEAD additional data |
| 3125 | * 2) The record counter sometimes determines the IV. |
| 3126 | * |
| 3127 | * Apart from that, the fields don't have influence. |
| 3128 | * In particular, it is currently not the responsibility |
| 3129 | * of ssl_encrypt/decrypt_buf to check if the transform |
| 3130 | * version matches the record version, or that the |
| 3131 | * type is sensible. |
| 3132 | */ |
| 3133 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3134 | memset(rec.ctr, num_records, sizeof(rec.ctr)); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3135 | rec.type = 42; |
| 3136 | rec.ver[0] = num_records; |
| 3137 | rec.ver[1] = num_records; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3138 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 3139 | rec.cid_len = 0; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3140 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3141 | |
| 3142 | rec.buf = buf; |
| 3143 | rec.buf_len = buflen; |
| 3144 | rec.data_offset = 16; |
| 3145 | /* Make sure to vary the length to exercise different |
| 3146 | * paddings. */ |
| 3147 | rec.data_len = 1 + num_records; |
| 3148 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3149 | memset(rec.buf + rec.data_offset, 42, rec.data_len); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3150 | |
| 3151 | /* Make a copy for later comparison */ |
| 3152 | rec_backup = rec; |
| 3153 | |
| 3154 | /* Encrypt record */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3155 | ret = mbedtls_ssl_encrypt_buf(&ssl, t_enc, &rec, |
| 3156 | mbedtls_test_rnd_std_rand, NULL); |
| 3157 | TEST_ASSERT(ret == 0 || ret == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL); |
| 3158 | if (ret != 0) { |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3159 | continue; |
| 3160 | } |
| 3161 | |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 3162 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3163 | if (rec.cid_len != 0) { |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 3164 | /* DTLS 1.2 + CID hides the real content type and |
| 3165 | * uses a special CID content type in the protected |
| 3166 | * record. Double-check this. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3167 | TEST_ASSERT(rec.type == MBEDTLS_SSL_MSG_CID); |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 3168 | } |
| 3169 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 3170 | |
| 3171 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3172 | if (t_enc->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4) { |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 3173 | /* TLS 1.3 hides the real content type and |
| 3174 | * always uses Application Data as the content type |
| 3175 | * for protected records. Double-check this. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3176 | TEST_ASSERT(rec.type == MBEDTLS_SSL_MSG_APPLICATION_DATA); |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 3177 | } |
| 3178 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ |
| 3179 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3180 | /* Decrypt record with t_dec */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3181 | ret = mbedtls_ssl_decrypt_buf(&ssl, t_dec, &rec); |
| 3182 | TEST_ASSERT(ret == 0); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3183 | |
| 3184 | /* Compare results */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3185 | TEST_ASSERT(rec.type == rec_backup.type); |
| 3186 | TEST_ASSERT(memcmp(rec.ctr, rec_backup.ctr, 8) == 0); |
| 3187 | TEST_ASSERT(rec.ver[0] == rec_backup.ver[0]); |
| 3188 | TEST_ASSERT(rec.ver[1] == rec_backup.ver[1]); |
| 3189 | TEST_ASSERT(rec.data_len == rec_backup.data_len); |
| 3190 | TEST_ASSERT(rec.data_offset == rec_backup.data_offset); |
| 3191 | TEST_ASSERT(memcmp(rec.buf + rec.data_offset, |
| 3192 | rec_backup.buf + rec_backup.data_offset, |
| 3193 | rec.data_len) == 0); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3194 | } |
| 3195 | |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 3196 | exit: |
| 3197 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3198 | /* Cleanup */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3199 | mbedtls_ssl_free(&ssl); |
| 3200 | mbedtls_ssl_transform_free(&t0); |
| 3201 | mbedtls_ssl_transform_free(&t1); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3202 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3203 | mbedtls_free(buf); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 3204 | } |
| 3205 | /* END_CASE */ |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3206 | |
| 3207 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3208 | void ssl_crypt_record_small(int cipher_type, int hash_id, |
| 3209 | int etm, int tag_mode, int ver, |
| 3210 | int cid0_len, int cid1_len) |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3211 | { |
| 3212 | /* |
| 3213 | * Test pairs of encryption and decryption with an increasing |
| 3214 | * amount of space in the record buffer - in more detail: |
| 3215 | * 1) Try to encrypt with 0, 1, 2, ... bytes available |
| 3216 | * in front of the plaintext, and expect the encryption |
| 3217 | * to succeed starting from some offset. Always keep |
| 3218 | * enough space in the end of the buffer. |
| 3219 | * 2) Try to encrypt with 0, 1, 2, ... bytes available |
| 3220 | * at the end of the plaintext, and expect the encryption |
| 3221 | * to succeed starting from some offset. Always keep |
| 3222 | * enough space at the beginning of the buffer. |
| 3223 | * 3) Try to encrypt with 0, 1, 2, ... bytes available |
| 3224 | * both at the front and end of the plaintext, |
| 3225 | * and expect the encryption to succeed starting from |
| 3226 | * some offset. |
| 3227 | * |
| 3228 | * If encryption succeeds, check that decryption succeeds |
| 3229 | * and yields the original record. |
| 3230 | */ |
| 3231 | |
| 3232 | mbedtls_ssl_context ssl; /* ONLY for debugging */ |
| 3233 | |
| 3234 | mbedtls_ssl_transform t0, t1; |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 3235 | unsigned char *buf = NULL; |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 3236 | size_t const buflen = 256; |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3237 | mbedtls_record rec, rec_backup; |
| 3238 | |
| 3239 | int ret; |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 3240 | int mode; /* Mode 1, 2 or 3 as explained above */ |
| 3241 | size_t offset; /* Available space at beginning/end/both */ |
| 3242 | size_t threshold = 96; /* Maximum offset to test against */ |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3243 | |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 3244 | size_t default_pre_padding = 64; /* Pre-padding to use in mode 2 */ |
| 3245 | 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] | 3246 | |
| 3247 | int seen_success; /* Indicates if in the current mode we've |
| 3248 | * already seen a successful test. */ |
| 3249 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3250 | mbedtls_ssl_init(&ssl); |
| 3251 | mbedtls_ssl_transform_init(&t0); |
| 3252 | mbedtls_ssl_transform_init(&t1); |
| 3253 | TEST_ASSERT(build_transforms(&t0, &t1, cipher_type, hash_id, |
| 3254 | etm, tag_mode, ver, |
| 3255 | (size_t) cid0_len, |
| 3256 | (size_t) cid1_len) == 0); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3257 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3258 | TEST_ASSERT((buf = mbedtls_calloc(1, buflen)) != NULL); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3259 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3260 | for (mode = 1; mode <= 3; mode++) { |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3261 | seen_success = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3262 | for (offset = 0; offset <= threshold; offset++) { |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3263 | mbedtls_ssl_transform *t_dec, *t_enc; |
Hanno Becker | 6c87b3f | 2019-04-29 17:24:44 +0100 | [diff] [blame] | 3264 | t_dec = &t0; |
| 3265 | t_enc = &t1; |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3266 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3267 | memset(rec.ctr, offset, sizeof(rec.ctr)); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3268 | rec.type = 42; |
| 3269 | rec.ver[0] = offset; |
| 3270 | rec.ver[1] = offset; |
| 3271 | rec.buf = buf; |
| 3272 | rec.buf_len = buflen; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3273 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 3274 | rec.cid_len = 0; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 3275 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3276 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3277 | switch (mode) { |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3278 | case 1: /* Space in the beginning */ |
| 3279 | rec.data_offset = offset; |
| 3280 | rec.data_len = buflen - offset - default_post_padding; |
| 3281 | break; |
| 3282 | |
| 3283 | case 2: /* Space in the end */ |
| 3284 | rec.data_offset = default_pre_padding; |
| 3285 | rec.data_len = buflen - default_pre_padding - offset; |
| 3286 | break; |
| 3287 | |
| 3288 | case 3: /* Space in the beginning and end */ |
| 3289 | rec.data_offset = offset; |
| 3290 | rec.data_len = buflen - 2 * offset; |
| 3291 | break; |
| 3292 | |
| 3293 | default: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3294 | TEST_ASSERT(0); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3295 | break; |
| 3296 | } |
| 3297 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3298 | memset(rec.buf + rec.data_offset, 42, rec.data_len); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3299 | |
| 3300 | /* Make a copy for later comparison */ |
| 3301 | rec_backup = rec; |
| 3302 | |
| 3303 | /* Encrypt record */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3304 | ret = mbedtls_ssl_encrypt_buf(&ssl, t_enc, &rec, |
| 3305 | mbedtls_test_rnd_std_rand, NULL); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3306 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3307 | if ((mode == 1 || mode == 2) && seen_success) { |
| 3308 | TEST_ASSERT(ret == 0); |
| 3309 | } else { |
| 3310 | TEST_ASSERT(ret == 0 || ret == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL); |
| 3311 | if (ret == 0) { |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3312 | seen_success = 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3313 | } |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3314 | } |
| 3315 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3316 | if (ret != 0) { |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3317 | continue; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3318 | } |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3319 | |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 3320 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3321 | if (rec.cid_len != 0) { |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 3322 | /* DTLS 1.2 + CID hides the real content type and |
| 3323 | * uses a special CID content type in the protected |
| 3324 | * record. Double-check this. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3325 | TEST_ASSERT(rec.type == MBEDTLS_SSL_MSG_CID); |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 3326 | } |
| 3327 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 3328 | |
| 3329 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3330 | if (t_enc->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4) { |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 3331 | /* TLS 1.3 hides the real content type and |
| 3332 | * always uses Application Data as the content type |
| 3333 | * for protected records. Double-check this. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3334 | TEST_ASSERT(rec.type == MBEDTLS_SSL_MSG_APPLICATION_DATA); |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 3335 | } |
| 3336 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ |
| 3337 | |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3338 | /* Decrypt record with t_dec */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3339 | TEST_ASSERT(mbedtls_ssl_decrypt_buf(&ssl, t_dec, &rec) == 0); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3340 | |
| 3341 | /* Compare results */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3342 | TEST_ASSERT(rec.type == rec_backup.type); |
| 3343 | TEST_ASSERT(memcmp(rec.ctr, rec_backup.ctr, 8) == 0); |
| 3344 | TEST_ASSERT(rec.ver[0] == rec_backup.ver[0]); |
| 3345 | TEST_ASSERT(rec.ver[1] == rec_backup.ver[1]); |
| 3346 | TEST_ASSERT(rec.data_len == rec_backup.data_len); |
| 3347 | TEST_ASSERT(rec.data_offset == rec_backup.data_offset); |
| 3348 | TEST_ASSERT(memcmp(rec.buf + rec.data_offset, |
| 3349 | rec_backup.buf + rec_backup.data_offset, |
| 3350 | rec.data_len) == 0); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3351 | } |
| 3352 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3353 | TEST_ASSERT(seen_success == 1); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3354 | } |
| 3355 | |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 3356 | exit: |
| 3357 | |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3358 | /* Cleanup */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3359 | mbedtls_ssl_free(&ssl); |
| 3360 | mbedtls_ssl_transform_free(&t0); |
| 3361 | mbedtls_ssl_transform_free(&t1); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3362 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3363 | mbedtls_free(buf); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 3364 | } |
| 3365 | /* END_CASE */ |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 3366 | |
Manuel Pégourié-Gonnard | 913a204 | 2020-07-09 10:02:41 +0200 | [diff] [blame] | 3367 | /* 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] | 3368 | void ssl_decrypt_non_etm_cbc(int cipher_type, int hash_id, int trunc_hmac, |
| 3369 | int length_selector) |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3370 | { |
| 3371 | /* |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3372 | * Test record decryption for CBC without EtM, focused on the verification |
| 3373 | * of padding and MAC. |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3374 | * |
| 3375 | * 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] | 3376 | * and either AES, ARIA, Camellia or DES, but since the test framework |
| 3377 | * doesn't support alternation in dependency statements, just depend on |
| 3378 | * TLS 1.2 and AES. |
Manuel Pégourié-Gonnard | 864abbf | 2020-07-21 10:37:14 +0200 | [diff] [blame] | 3379 | * |
| 3380 | * The length_selector argument is interpreted as follows: |
| 3381 | * - if it's -1, the plaintext length is 0 and minimal padding is applied |
| 3382 | * - if it's -2, the plaintext length is 0 and maximal padding is applied |
| 3383 | * - otherwise it must be in [0, 255] and is padding_length from RFC 5246: |
| 3384 | * it's the length of the rest of the padding, that is, excluding the |
| 3385 | * byte that encodes the length. The minimal non-zero plaintext length |
| 3386 | * that gives this padding_length is automatically selected. |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3387 | */ |
| 3388 | mbedtls_ssl_context ssl; /* ONLY for debugging */ |
| 3389 | mbedtls_ssl_transform t0, t1; |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3390 | mbedtls_record rec, rec_save; |
| 3391 | unsigned char *buf = NULL, *buf_save = NULL; |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3392 | size_t buflen, olen = 0; |
Manuel Pégourié-Gonnard | 864abbf | 2020-07-21 10:37:14 +0200 | [diff] [blame] | 3393 | size_t plaintext_len, block_size, i; |
Manuel Pégourié-Gonnard | e55653f | 2020-07-22 11:42:57 +0200 | [diff] [blame] | 3394 | unsigned char padlen; /* excluding the padding_length byte */ |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3395 | unsigned char add_data[13]; |
| 3396 | unsigned char mac[MBEDTLS_MD_MAX_SIZE]; |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3397 | int exp_ret; |
Manuel Pégourié-Gonnard | 4adc04a | 2020-07-16 10:00:48 +0200 | [diff] [blame] | 3398 | const unsigned char pad_max_len = 255; /* Per the standard */ |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3399 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3400 | mbedtls_ssl_init(&ssl); |
| 3401 | mbedtls_ssl_transform_init(&t0); |
| 3402 | mbedtls_ssl_transform_init(&t1); |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3403 | |
| 3404 | /* Set up transforms with dummy keys */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3405 | TEST_ASSERT(build_transforms(&t0, &t1, cipher_type, hash_id, |
| 3406 | 0, trunc_hmac, |
| 3407 | MBEDTLS_SSL_MINOR_VERSION_3, |
| 3408 | 0, 0) == 0); |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3409 | |
Manuel Pégourié-Gonnard | 864abbf | 2020-07-21 10:37:14 +0200 | [diff] [blame] | 3410 | /* Determine padding/plaintext length */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3411 | TEST_ASSERT(length_selector >= -2 && length_selector <= 255); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3412 | block_size = t0.ivlen; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3413 | if (length_selector < 0) { |
Manuel Pégourié-Gonnard | 864abbf | 2020-07-21 10:37:14 +0200 | [diff] [blame] | 3414 | plaintext_len = 0; |
| 3415 | |
Manuel Pégourié-Gonnard | e55653f | 2020-07-22 11:42:57 +0200 | [diff] [blame] | 3416 | /* Minimal padding |
| 3417 | * The +1 is for the padding_length byte, not counted in padlen. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3418 | padlen = block_size - (t0.maclen + 1) % block_size; |
Manuel Pégourié-Gonnard | 864abbf | 2020-07-21 10:37:14 +0200 | [diff] [blame] | 3419 | |
| 3420 | /* Maximal padding? */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3421 | if (length_selector == -2) { |
| 3422 | padlen += block_size * ((pad_max_len - padlen) / block_size); |
| 3423 | } |
| 3424 | } else { |
Manuel Pégourié-Gonnard | 864abbf | 2020-07-21 10:37:14 +0200 | [diff] [blame] | 3425 | padlen = length_selector; |
| 3426 | |
Manuel Pégourié-Gonnard | e55653f | 2020-07-22 11:42:57 +0200 | [diff] [blame] | 3427 | /* Minimal non-zero plaintext_length giving desired padding. |
| 3428 | * The +1 is for the padding_length byte, not counted in padlen. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3429 | plaintext_len = block_size - (padlen + t0.maclen + 1) % block_size; |
Manuel Pégourié-Gonnard | 864abbf | 2020-07-21 10:37:14 +0200 | [diff] [blame] | 3430 | } |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3431 | |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3432 | /* Prepare a buffer for record data */ |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3433 | buflen = block_size |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3434 | + plaintext_len |
| 3435 | + t0.maclen |
| 3436 | + padlen + 1; |
| 3437 | ASSERT_ALLOC(buf, buflen); |
| 3438 | ASSERT_ALLOC(buf_save, buflen); |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3439 | |
| 3440 | /* Prepare a dummy record header */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3441 | memset(rec.ctr, 0, sizeof(rec.ctr)); |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3442 | rec.type = MBEDTLS_SSL_MSG_APPLICATION_DATA; |
| 3443 | rec.ver[0] = MBEDTLS_SSL_MAJOR_VERSION_3; |
| 3444 | rec.ver[1] = MBEDTLS_SSL_MINOR_VERSION_3; |
| 3445 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 3446 | rec.cid_len = 0; |
| 3447 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 3448 | |
| 3449 | /* Prepare dummy record content */ |
| 3450 | rec.buf = buf; |
| 3451 | rec.buf_len = buflen; |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3452 | rec.data_offset = block_size; |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3453 | rec.data_len = plaintext_len; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3454 | memset(rec.buf + rec.data_offset, 42, rec.data_len); |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3455 | |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3456 | /* Serialized version of record header for MAC purposes */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3457 | memcpy(add_data, rec.ctr, 8); |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3458 | add_data[8] = rec.type; |
| 3459 | add_data[9] = rec.ver[0]; |
| 3460 | add_data[10] = rec.ver[1]; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3461 | add_data[11] = (rec.data_len >> 8) & 0xff; |
| 3462 | add_data[12] = (rec.data_len >> 0) & 0xff; |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3463 | |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3464 | /* Set dummy IV */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3465 | memset(t0.iv_enc, 0x55, t0.ivlen); |
| 3466 | memcpy(rec.buf, t0.iv_enc, t0.ivlen); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3467 | |
| 3468 | /* |
| 3469 | * Prepare a pre-encryption record (with MAC and padding), and save it. |
| 3470 | */ |
| 3471 | |
| 3472 | /* MAC with additional data */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3473 | TEST_EQUAL(0, mbedtls_md_hmac_update(&t0.md_ctx_enc, add_data, 13)); |
| 3474 | TEST_EQUAL(0, mbedtls_md_hmac_update(&t0.md_ctx_enc, |
| 3475 | rec.buf + rec.data_offset, |
| 3476 | rec.data_len)); |
| 3477 | 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] | 3478 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3479 | 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] | 3480 | rec.data_len += t0.maclen; |
| 3481 | |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3482 | /* Pad */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3483 | 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] | 3484 | rec.data_len += padlen + 1; |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3485 | |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3486 | /* Save correct pre-encryption record */ |
| 3487 | rec_save = rec; |
| 3488 | rec_save.buf = buf_save; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3489 | memcpy(buf_save, buf, buflen); |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3490 | |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3491 | /* |
| 3492 | * Encrypt and decrypt the correct record, expecting success |
| 3493 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3494 | TEST_EQUAL(0, mbedtls_cipher_crypt(&t0.cipher_ctx_enc, |
| 3495 | t0.iv_enc, t0.ivlen, |
| 3496 | rec.buf + rec.data_offset, rec.data_len, |
| 3497 | rec.buf + rec.data_offset, &olen)); |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3498 | rec.data_offset -= t0.ivlen; |
| 3499 | rec.data_len += t0.ivlen; |
| 3500 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3501 | TEST_EQUAL(0, mbedtls_ssl_decrypt_buf(&ssl, &t1, &rec)); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3502 | |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3503 | /* |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3504 | * Modify each byte of the pre-encryption record before encrypting and |
| 3505 | * decrypting it, expecting failure every time. |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3506 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3507 | for (i = block_size; i < buflen; i++) { |
| 3508 | mbedtls_test_set_step(i); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3509 | |
| 3510 | /* Restore correct pre-encryption record */ |
| 3511 | rec = rec_save; |
| 3512 | rec.buf = buf; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3513 | memcpy(buf, buf_save, buflen); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3514 | |
Manuel Pégourié-Gonnard | b51f044 | 2020-07-21 10:40:25 +0200 | [diff] [blame] | 3515 | /* 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] | 3516 | rec.buf[i] ^= 0x01; |
| 3517 | |
| 3518 | /* Encrypt */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3519 | TEST_EQUAL(0, mbedtls_cipher_crypt(&t0.cipher_ctx_enc, |
| 3520 | t0.iv_enc, t0.ivlen, |
| 3521 | rec.buf + rec.data_offset, rec.data_len, |
| 3522 | rec.buf + rec.data_offset, &olen)); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3523 | rec.data_offset -= t0.ivlen; |
| 3524 | rec.data_len += t0.ivlen; |
| 3525 | |
| 3526 | /* Decrypt and expect failure */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3527 | TEST_EQUAL(MBEDTLS_ERR_SSL_INVALID_MAC, |
| 3528 | mbedtls_ssl_decrypt_buf(&ssl, &t1, &rec)); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3529 | } |
| 3530 | |
| 3531 | /* |
| 3532 | * Use larger values of the padding bytes - with small buffers, this tests |
| 3533 | * the case where the announced padlen would be larger than the buffer |
| 3534 | * (and before that, than the buffer minus the size of the MAC), to make |
| 3535 | * sure our padding checking code does not perform any out-of-bounds reads |
| 3536 | * in this case. (With larger buffers, ie when the plaintext is long or |
| 3537 | * maximal length padding is used, this is less relevant but still doesn't |
| 3538 | * hurt to test.) |
| 3539 | * |
| 3540 | * (Start the loop with correct padding, just to double-check that record |
| 3541 | * saving did work, and that we're overwriting the correct bytes.) |
| 3542 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3543 | for (i = padlen; i <= pad_max_len; i++) { |
| 3544 | mbedtls_test_set_step(i); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3545 | |
| 3546 | /* Restore correct pre-encryption record */ |
| 3547 | rec = rec_save; |
| 3548 | rec.buf = buf; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3549 | memcpy(buf, buf_save, buflen); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3550 | |
| 3551 | /* Set padding bytes to new value */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3552 | memset(buf + buflen - padlen - 1, i, padlen + 1); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3553 | |
| 3554 | /* Encrypt */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3555 | TEST_EQUAL(0, mbedtls_cipher_crypt(&t0.cipher_ctx_enc, |
| 3556 | t0.iv_enc, t0.ivlen, |
| 3557 | rec.buf + rec.data_offset, rec.data_len, |
| 3558 | rec.buf + rec.data_offset, &olen)); |
Manuel Pégourié-Gonnard | 527c1ff | 2020-07-07 10:43:37 +0200 | [diff] [blame] | 3559 | rec.data_offset -= t0.ivlen; |
| 3560 | rec.data_len += t0.ivlen; |
| 3561 | |
| 3562 | /* Decrypt and expect failure except the first time */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3563 | exp_ret = (i == padlen) ? 0 : MBEDTLS_ERR_SSL_INVALID_MAC; |
| 3564 | 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] | 3565 | } |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3566 | |
| 3567 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3568 | mbedtls_ssl_free(&ssl); |
| 3569 | mbedtls_ssl_transform_free(&t0); |
| 3570 | mbedtls_ssl_transform_free(&t1); |
| 3571 | mbedtls_free(buf); |
| 3572 | mbedtls_free(buf_save); |
Manuel Pégourié-Gonnard | 0ac01a1 | 2020-07-03 12:49:10 +0200 | [diff] [blame] | 3573 | } |
| 3574 | /* END_CASE */ |
| 3575 | |
Hanno Becker | 39ff492 | 2020-08-21 13:36:56 +0100 | [diff] [blame] | 3576 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3577 | void ssl_tls1_3_hkdf_expand_label(int hash_alg, |
| 3578 | data_t *secret, |
| 3579 | int label_idx, |
| 3580 | data_t *ctx, |
| 3581 | int desired_length, |
| 3582 | data_t *expected) |
Hanno Becker | 39ff492 | 2020-08-21 13:36:56 +0100 | [diff] [blame] | 3583 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3584 | unsigned char dst[100]; |
Hanno Becker | 39ff492 | 2020-08-21 13:36:56 +0100 | [diff] [blame] | 3585 | |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 3586 | unsigned char const *lbl = NULL; |
| 3587 | size_t lbl_len; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3588 | #define MBEDTLS_SSL_TLS1_3_LABEL(name, string) \ |
| 3589 | if (label_idx == (int) tls1_3_label_ ## name) \ |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 3590 | { \ |
| 3591 | lbl = mbedtls_ssl_tls1_3_labels.name; \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3592 | lbl_len = sizeof(mbedtls_ssl_tls1_3_labels.name); \ |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 3593 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3594 | MBEDTLS_SSL_TLS1_3_LABEL_LIST |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 3595 | #undef MBEDTLS_SSL_TLS1_3_LABEL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3596 | TEST_ASSERT(lbl != NULL); |
Hanno Becker | 39ff492 | 2020-08-21 13:36:56 +0100 | [diff] [blame] | 3597 | |
| 3598 | /* Check sanity of test parameters. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3599 | TEST_ASSERT((size_t) desired_length <= sizeof(dst)); |
| 3600 | TEST_ASSERT((size_t) desired_length == expected->len); |
Hanno Becker | 39ff492 | 2020-08-21 13:36:56 +0100 | [diff] [blame] | 3601 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3602 | TEST_ASSERT(mbedtls_ssl_tls1_3_hkdf_expand_label( |
| 3603 | (mbedtls_md_type_t) hash_alg, |
| 3604 | secret->x, secret->len, |
| 3605 | lbl, lbl_len, |
| 3606 | ctx->x, ctx->len, |
| 3607 | dst, desired_length) == 0); |
Hanno Becker | 39ff492 | 2020-08-21 13:36:56 +0100 | [diff] [blame] | 3608 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3609 | ASSERT_COMPARE(dst, (size_t) desired_length, |
| 3610 | expected->x, (size_t) expected->len); |
Hanno Becker | 39ff492 | 2020-08-21 13:36:56 +0100 | [diff] [blame] | 3611 | } |
| 3612 | /* END_CASE */ |
| 3613 | |
Hanno Becker | 19498f8 | 2020-08-21 13:37:08 +0100 | [diff] [blame] | 3614 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3615 | void ssl_tls1_3_traffic_key_generation(int hash_alg, |
| 3616 | data_t *server_secret, |
| 3617 | data_t *client_secret, |
| 3618 | int desired_iv_len, |
| 3619 | int desired_key_len, |
| 3620 | data_t *expected_server_write_key, |
| 3621 | data_t *expected_server_write_iv, |
| 3622 | data_t *expected_client_write_key, |
| 3623 | data_t *expected_client_write_iv) |
Hanno Becker | 19498f8 | 2020-08-21 13:37:08 +0100 | [diff] [blame] | 3624 | { |
| 3625 | mbedtls_ssl_key_set keys; |
| 3626 | |
| 3627 | /* Check sanity of test parameters. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3628 | TEST_ASSERT(client_secret->len == server_secret->len); |
| 3629 | TEST_ASSERT(expected_client_write_iv->len == expected_server_write_iv->len && |
| 3630 | expected_client_write_iv->len == (size_t) desired_iv_len); |
| 3631 | TEST_ASSERT(expected_client_write_key->len == expected_server_write_key->len && |
| 3632 | expected_client_write_key->len == (size_t) desired_key_len); |
Hanno Becker | 19498f8 | 2020-08-21 13:37:08 +0100 | [diff] [blame] | 3633 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3634 | TEST_ASSERT(mbedtls_ssl_tls1_3_make_traffic_keys( |
| 3635 | (mbedtls_md_type_t) hash_alg, |
| 3636 | client_secret->x, |
| 3637 | server_secret->x, |
| 3638 | client_secret->len /* == server_secret->len */, |
| 3639 | desired_key_len, desired_iv_len, |
| 3640 | &keys) == 0); |
Hanno Becker | 19498f8 | 2020-08-21 13:37:08 +0100 | [diff] [blame] | 3641 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3642 | ASSERT_COMPARE(keys.client_write_key, |
| 3643 | keys.key_len, |
| 3644 | expected_client_write_key->x, |
| 3645 | (size_t) desired_key_len); |
| 3646 | ASSERT_COMPARE(keys.server_write_key, |
| 3647 | keys.key_len, |
| 3648 | expected_server_write_key->x, |
| 3649 | (size_t) desired_key_len); |
| 3650 | ASSERT_COMPARE(keys.client_write_iv, |
| 3651 | keys.iv_len, |
| 3652 | expected_client_write_iv->x, |
| 3653 | (size_t) desired_iv_len); |
| 3654 | ASSERT_COMPARE(keys.server_write_iv, |
| 3655 | keys.iv_len, |
| 3656 | expected_server_write_iv->x, |
| 3657 | (size_t) desired_iv_len); |
Hanno Becker | 19498f8 | 2020-08-21 13:37:08 +0100 | [diff] [blame] | 3658 | } |
| 3659 | /* END_CASE */ |
| 3660 | |
Hanno Becker | e4849d1 | 2020-08-21 14:14:14 +0100 | [diff] [blame] | 3661 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3662 | void ssl_tls1_3_derive_secret(int hash_alg, |
| 3663 | data_t *secret, |
| 3664 | int label_idx, |
| 3665 | data_t *ctx, |
| 3666 | int desired_length, |
| 3667 | int already_hashed, |
| 3668 | data_t *expected) |
Hanno Becker | e4849d1 | 2020-08-21 14:14:14 +0100 | [diff] [blame] | 3669 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3670 | unsigned char dst[100]; |
Hanno Becker | e4849d1 | 2020-08-21 14:14:14 +0100 | [diff] [blame] | 3671 | |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 3672 | unsigned char const *lbl = NULL; |
| 3673 | size_t lbl_len; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3674 | #define MBEDTLS_SSL_TLS1_3_LABEL(name, string) \ |
| 3675 | if (label_idx == (int) tls1_3_label_ ## name) \ |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 3676 | { \ |
| 3677 | lbl = mbedtls_ssl_tls1_3_labels.name; \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3678 | lbl_len = sizeof(mbedtls_ssl_tls1_3_labels.name); \ |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 3679 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3680 | MBEDTLS_SSL_TLS1_3_LABEL_LIST |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 3681 | #undef MBEDTLS_SSL_TLS1_3_LABEL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3682 | TEST_ASSERT(lbl != NULL); |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 3683 | |
Hanno Becker | e4849d1 | 2020-08-21 14:14:14 +0100 | [diff] [blame] | 3684 | /* Check sanity of test parameters. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3685 | TEST_ASSERT((size_t) desired_length <= sizeof(dst)); |
| 3686 | TEST_ASSERT((size_t) desired_length == expected->len); |
Hanno Becker | e4849d1 | 2020-08-21 14:14:14 +0100 | [diff] [blame] | 3687 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3688 | TEST_ASSERT(mbedtls_ssl_tls1_3_derive_secret( |
| 3689 | (mbedtls_md_type_t) hash_alg, |
| 3690 | secret->x, secret->len, |
| 3691 | lbl, lbl_len, |
| 3692 | ctx->x, ctx->len, |
| 3693 | already_hashed, |
| 3694 | dst, desired_length) == 0); |
Hanno Becker | e4849d1 | 2020-08-21 14:14:14 +0100 | [diff] [blame] | 3695 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3696 | ASSERT_COMPARE(dst, desired_length, |
| 3697 | expected->x, desired_length); |
Hanno Becker | e4849d1 | 2020-08-21 14:14:14 +0100 | [diff] [blame] | 3698 | } |
| 3699 | /* END_CASE */ |
| 3700 | |
Hanno Becker | 2d2c3eb | 2020-08-20 14:54:24 +0100 | [diff] [blame] | 3701 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3702 | void ssl_tls1_3_key_evolution(int hash_alg, |
| 3703 | data_t *secret, |
| 3704 | data_t *input, |
| 3705 | data_t *expected) |
Hanno Becker | 2d2c3eb | 2020-08-20 14:54:24 +0100 | [diff] [blame] | 3706 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3707 | unsigned char secret_new[MBEDTLS_MD_MAX_SIZE]; |
Hanno Becker | 2d2c3eb | 2020-08-20 14:54:24 +0100 | [diff] [blame] | 3708 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3709 | TEST_ASSERT(mbedtls_ssl_tls1_3_evolve_secret( |
| 3710 | (mbedtls_md_type_t) hash_alg, |
| 3711 | secret->len ? secret->x : NULL, |
| 3712 | input->len ? input->x : NULL, input->len, |
| 3713 | secret_new) == 0); |
Hanno Becker | 2d2c3eb | 2020-08-20 14:54:24 +0100 | [diff] [blame] | 3714 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3715 | ASSERT_COMPARE(secret_new, (size_t) expected->len, |
| 3716 | expected->x, (size_t) expected->len); |
Hanno Becker | 2d2c3eb | 2020-08-20 14:54:24 +0100 | [diff] [blame] | 3717 | } |
| 3718 | /* END_CASE */ |
| 3719 | |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 3720 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3721 | void ssl_tls_prf(int type, data_t *secret, data_t *random, |
| 3722 | char *label, data_t *result_str, int exp_ret) |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 3723 | { |
| 3724 | unsigned char *output; |
| 3725 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3726 | output = mbedtls_calloc(1, result_str->len); |
| 3727 | if (output == NULL) { |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 3728 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3729 | } |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 3730 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3731 | USE_PSA_INIT(); |
Ron Eldor | 6b9b1b8 | 2019-05-15 17:04:33 +0300 | [diff] [blame] | 3732 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3733 | TEST_ASSERT(mbedtls_ssl_tls_prf(type, secret->x, secret->len, |
| 3734 | label, random->x, random->len, |
| 3735 | output, result_str->len) == exp_ret); |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 3736 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3737 | if (exp_ret == 0) { |
| 3738 | TEST_ASSERT(mbedtls_test_hexcmp(output, result_str->x, |
| 3739 | result_str->len, result_str->len) == 0); |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 3740 | } |
| 3741 | exit: |
| 3742 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3743 | mbedtls_free(output); |
| 3744 | USE_PSA_DONE(); |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 3745 | } |
| 3746 | /* END_CASE */ |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 3747 | |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 3748 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3749 | 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] | 3750 | { |
| 3751 | mbedtls_ssl_session original, restored; |
| 3752 | unsigned char *buf = NULL; |
| 3753 | size_t len; |
| 3754 | |
| 3755 | /* |
| 3756 | * Test that a save-load pair is the identity |
| 3757 | */ |
| 3758 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3759 | mbedtls_ssl_session_init(&original); |
| 3760 | mbedtls_ssl_session_init(&restored); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 3761 | |
| 3762 | /* Prepare a dummy session to work on */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3763 | 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] | 3764 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 3765 | /* Serialize it */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3766 | TEST_ASSERT(mbedtls_ssl_session_save(&original, NULL, 0, &len) |
| 3767 | == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL); |
| 3768 | TEST_ASSERT((buf = mbedtls_calloc(1, len)) != NULL); |
| 3769 | TEST_ASSERT(mbedtls_ssl_session_save(&original, buf, len, &len) |
| 3770 | == 0); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 3771 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 3772 | /* Restore session from serialized data */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3773 | TEST_ASSERT(mbedtls_ssl_session_load(&restored, buf, len) == 0); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 3774 | |
| 3775 | /* |
| 3776 | * Make sure both session structures are identical |
| 3777 | */ |
| 3778 | #if defined(MBEDTLS_HAVE_TIME) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3779 | TEST_ASSERT(original.start == restored.start); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 3780 | #endif |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3781 | TEST_ASSERT(original.ciphersuite == restored.ciphersuite); |
| 3782 | TEST_ASSERT(original.compression == restored.compression); |
| 3783 | TEST_ASSERT(original.id_len == restored.id_len); |
| 3784 | TEST_ASSERT(memcmp(original.id, |
| 3785 | restored.id, sizeof(original.id)) == 0); |
| 3786 | TEST_ASSERT(memcmp(original.master, |
| 3787 | restored.master, sizeof(original.master)) == 0); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 3788 | |
Gilles Peskine | 33d03fe | 2023-02-01 18:37:49 +0100 | [diff] [blame] | 3789 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) && \ |
| 3790 | defined(MBEDTLS_CERTS_C) |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 3791 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3792 | TEST_ASSERT((original.peer_cert == NULL) == |
| 3793 | (restored.peer_cert == NULL)); |
| 3794 | if (original.peer_cert != NULL) { |
| 3795 | TEST_ASSERT(original.peer_cert->raw.len == |
| 3796 | restored.peer_cert->raw.len); |
| 3797 | TEST_ASSERT(memcmp(original.peer_cert->raw.p, |
| 3798 | restored.peer_cert->raw.p, |
| 3799 | original.peer_cert->raw.len) == 0); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 3800 | } |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 3801 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3802 | TEST_ASSERT(original.peer_cert_digest_type == |
| 3803 | restored.peer_cert_digest_type); |
| 3804 | TEST_ASSERT(original.peer_cert_digest_len == |
| 3805 | restored.peer_cert_digest_len); |
| 3806 | TEST_ASSERT((original.peer_cert_digest == NULL) == |
| 3807 | (restored.peer_cert_digest == NULL)); |
| 3808 | if (original.peer_cert_digest != NULL) { |
| 3809 | TEST_ASSERT(memcmp(original.peer_cert_digest, |
| 3810 | restored.peer_cert_digest, |
| 3811 | original.peer_cert_digest_len) == 0); |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 3812 | } |
| 3813 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Gilles Peskine | 33d03fe | 2023-02-01 18:37:49 +0100 | [diff] [blame] | 3814 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED && MBEDTLS_CERTS_C */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3815 | TEST_ASSERT(original.verify_result == restored.verify_result); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 3816 | |
| 3817 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3818 | TEST_ASSERT(original.ticket_len == restored.ticket_len); |
| 3819 | if (original.ticket_len != 0) { |
| 3820 | TEST_ASSERT(original.ticket != NULL); |
| 3821 | TEST_ASSERT(restored.ticket != NULL); |
| 3822 | TEST_ASSERT(memcmp(original.ticket, |
| 3823 | restored.ticket, original.ticket_len) == 0); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 3824 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3825 | TEST_ASSERT(original.ticket_lifetime == restored.ticket_lifetime); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 3826 | #endif |
| 3827 | |
| 3828 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3829 | TEST_ASSERT(original.mfl_code == restored.mfl_code); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 3830 | #endif |
| 3831 | |
| 3832 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3833 | TEST_ASSERT(original.trunc_hmac == restored.trunc_hmac); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 3834 | #endif |
| 3835 | |
| 3836 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3837 | TEST_ASSERT(original.encrypt_then_mac == restored.encrypt_then_mac); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 3838 | #endif |
| 3839 | |
| 3840 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3841 | mbedtls_ssl_session_free(&original); |
| 3842 | mbedtls_ssl_session_free(&restored); |
| 3843 | mbedtls_free(buf); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 3844 | } |
| 3845 | /* END_CASE */ |
| 3846 | |
Manuel Pégourié-Gonnard | aa75583 | 2019-06-03 10:53:47 +0200 | [diff] [blame] | 3847 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3848 | 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] | 3849 | { |
| 3850 | mbedtls_ssl_session session; |
| 3851 | unsigned char *buf1 = NULL, *buf2 = NULL; |
| 3852 | size_t len0, len1, len2; |
| 3853 | |
| 3854 | /* |
| 3855 | * Test that a load-save pair is the identity |
| 3856 | */ |
| 3857 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3858 | mbedtls_ssl_session_init(&session); |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 3859 | |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 3860 | /* Prepare a dummy session to work on */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3861 | 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] | 3862 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 3863 | /* Get desired buffer size for serializing */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3864 | TEST_ASSERT(mbedtls_ssl_session_save(&session, NULL, 0, &len0) |
| 3865 | == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL); |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 3866 | |
| 3867 | /* Allocate first buffer */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3868 | buf1 = mbedtls_calloc(1, len0); |
| 3869 | TEST_ASSERT(buf1 != NULL); |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 3870 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 3871 | /* Serialize to buffer and free live session */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3872 | TEST_ASSERT(mbedtls_ssl_session_save(&session, buf1, len0, &len1) |
| 3873 | == 0); |
| 3874 | TEST_ASSERT(len0 == len1); |
| 3875 | mbedtls_ssl_session_free(&session); |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 3876 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 3877 | /* Restore session from serialized data */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3878 | TEST_ASSERT(mbedtls_ssl_session_load(&session, buf1, len1) == 0); |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 3879 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 3880 | /* Allocate second buffer and serialize to it */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3881 | buf2 = mbedtls_calloc(1, len0); |
| 3882 | TEST_ASSERT(buf2 != NULL); |
| 3883 | TEST_ASSERT(mbedtls_ssl_session_save(&session, buf2, len0, &len2) |
| 3884 | == 0); |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 3885 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 3886 | /* Make sure both serialized versions are identical */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3887 | TEST_ASSERT(len1 == len2); |
| 3888 | TEST_ASSERT(memcmp(buf1, buf2, len1) == 0); |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 3889 | |
| 3890 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3891 | mbedtls_ssl_session_free(&session); |
| 3892 | mbedtls_free(buf1); |
| 3893 | mbedtls_free(buf2); |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 3894 | } |
| 3895 | /* END_CASE */ |
Manuel Pégourié-Gonnard | f5fa0aa | 2019-05-23 10:38:11 +0200 | [diff] [blame] | 3896 | |
| 3897 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3898 | 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] | 3899 | { |
| 3900 | mbedtls_ssl_session session; |
| 3901 | unsigned char *buf = NULL; |
| 3902 | size_t good_len, bad_len, test_len; |
| 3903 | |
| 3904 | /* |
| 3905 | * Test that session_save() fails cleanly on small buffers |
| 3906 | */ |
| 3907 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3908 | mbedtls_ssl_session_init(&session); |
Manuel Pégourié-Gonnard | f5fa0aa | 2019-05-23 10:38:11 +0200 | [diff] [blame] | 3909 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 3910 | /* Prepare dummy session and get serialized size */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3911 | TEST_ASSERT(ssl_populate_session(&session, ticket_len, crt_file) == 0); |
| 3912 | TEST_ASSERT(mbedtls_ssl_session_save(&session, NULL, 0, &good_len) |
| 3913 | == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL); |
Manuel Pégourié-Gonnard | f5fa0aa | 2019-05-23 10:38:11 +0200 | [diff] [blame] | 3914 | |
| 3915 | /* Try all possible bad lengths */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3916 | for (bad_len = 1; bad_len < good_len; bad_len++) { |
Manuel Pégourié-Gonnard | f5fa0aa | 2019-05-23 10:38:11 +0200 | [diff] [blame] | 3917 | /* Allocate exact size so that asan/valgrind can detect any overwrite */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3918 | mbedtls_free(buf); |
| 3919 | TEST_ASSERT((buf = mbedtls_calloc(1, bad_len)) != NULL); |
| 3920 | TEST_ASSERT(mbedtls_ssl_session_save(&session, buf, bad_len, |
| 3921 | &test_len) |
| 3922 | == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL); |
| 3923 | TEST_ASSERT(test_len == good_len); |
Manuel Pégourié-Gonnard | f5fa0aa | 2019-05-23 10:38:11 +0200 | [diff] [blame] | 3924 | } |
| 3925 | |
| 3926 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3927 | mbedtls_ssl_session_free(&session); |
| 3928 | mbedtls_free(buf); |
Manuel Pégourié-Gonnard | f5fa0aa | 2019-05-23 10:38:11 +0200 | [diff] [blame] | 3929 | } |
| 3930 | /* END_CASE */ |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 3931 | |
| 3932 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3933 | 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] | 3934 | { |
| 3935 | mbedtls_ssl_session session; |
| 3936 | unsigned char *good_buf = NULL, *bad_buf = NULL; |
| 3937 | size_t good_len, bad_len; |
| 3938 | |
| 3939 | /* |
| 3940 | * Test that session_load() fails cleanly on small buffers |
| 3941 | */ |
| 3942 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3943 | mbedtls_ssl_session_init(&session); |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 3944 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 3945 | /* Prepare serialized session data */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3946 | TEST_ASSERT(ssl_populate_session(&session, ticket_len, crt_file) == 0); |
| 3947 | TEST_ASSERT(mbedtls_ssl_session_save(&session, NULL, 0, &good_len) |
| 3948 | == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL); |
| 3949 | TEST_ASSERT((good_buf = mbedtls_calloc(1, good_len)) != NULL); |
| 3950 | TEST_ASSERT(mbedtls_ssl_session_save(&session, good_buf, good_len, |
| 3951 | &good_len) == 0); |
| 3952 | mbedtls_ssl_session_free(&session); |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 3953 | |
| 3954 | /* Try all possible bad lengths */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3955 | for (bad_len = 0; bad_len < good_len; bad_len++) { |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 3956 | /* Allocate exact size so that asan/valgrind can detect any overread */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3957 | mbedtls_free(bad_buf); |
| 3958 | bad_buf = mbedtls_calloc(1, bad_len ? bad_len : 1); |
| 3959 | TEST_ASSERT(bad_buf != NULL); |
| 3960 | memcpy(bad_buf, good_buf, bad_len); |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 3961 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3962 | TEST_ASSERT(mbedtls_ssl_session_load(&session, bad_buf, bad_len) |
| 3963 | == MBEDTLS_ERR_SSL_BAD_INPUT_DATA); |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 3964 | } |
| 3965 | |
| 3966 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3967 | mbedtls_ssl_session_free(&session); |
| 3968 | mbedtls_free(good_buf); |
| 3969 | mbedtls_free(bad_buf); |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 3970 | } |
| 3971 | /* END_CASE */ |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 3972 | |
Hanno Becker | 363b646 | 2019-05-29 12:44:28 +0100 | [diff] [blame] | 3973 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3974 | void ssl_session_serialize_version_check(int corrupt_major, |
| 3975 | int corrupt_minor, |
| 3976 | int corrupt_patch, |
| 3977 | int corrupt_config) |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 3978 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3979 | unsigned char serialized_session[2048]; |
Hanno Becker | 363b646 | 2019-05-29 12:44:28 +0100 | [diff] [blame] | 3980 | size_t serialized_session_len; |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 3981 | unsigned cur_byte; |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 3982 | mbedtls_ssl_session session; |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 3983 | uint8_t should_corrupt_byte[] = { corrupt_major == 1, |
| 3984 | corrupt_minor == 1, |
| 3985 | corrupt_patch == 1, |
| 3986 | corrupt_config == 1, |
| 3987 | corrupt_config == 1 }; |
| 3988 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3989 | mbedtls_ssl_session_init(&session); |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 3990 | |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 3991 | /* Infer length of serialized session. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3992 | TEST_ASSERT(mbedtls_ssl_session_save(&session, |
| 3993 | serialized_session, |
| 3994 | sizeof(serialized_session), |
| 3995 | &serialized_session_len) == 0); |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 3996 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 3997 | mbedtls_ssl_session_free(&session); |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 3998 | |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 3999 | /* Without any modification, we should be able to successfully |
Hanno Becker | 363b646 | 2019-05-29 12:44:28 +0100 | [diff] [blame] | 4000 | * de-serialize the session - double-check that. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4001 | TEST_ASSERT(mbedtls_ssl_session_load(&session, |
| 4002 | serialized_session, |
| 4003 | serialized_session_len) == 0); |
| 4004 | mbedtls_ssl_session_free(&session); |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 4005 | |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 4006 | /* Go through the bytes in the serialized session header and |
| 4007 | * corrupt them bit-by-bit. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4008 | for (cur_byte = 0; cur_byte < sizeof(should_corrupt_byte); cur_byte++) { |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 4009 | int cur_bit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4010 | unsigned char * const byte = &serialized_session[cur_byte]; |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 4011 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4012 | if (should_corrupt_byte[cur_byte] == 0) { |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 4013 | continue; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4014 | } |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 4015 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4016 | for (cur_bit = 0; cur_bit < CHAR_BIT; cur_bit++) { |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 4017 | unsigned char const corrupted_bit = 0x1u << cur_bit; |
| 4018 | /* Modify a single bit in the serialized session. */ |
| 4019 | *byte ^= corrupted_bit; |
| 4020 | |
| 4021 | /* Attempt to deserialize */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4022 | TEST_ASSERT(mbedtls_ssl_session_load(&session, |
| 4023 | serialized_session, |
| 4024 | serialized_session_len) == |
| 4025 | MBEDTLS_ERR_SSL_VERSION_MISMATCH); |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 4026 | |
| 4027 | /* Undo the change */ |
| 4028 | *byte ^= corrupted_bit; |
| 4029 | } |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 4030 | } |
| 4031 | |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 4032 | } |
| 4033 | /* END_CASE */ |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4034 | |
Gilles Peskine | 33d03fe | 2023-02-01 18:37:49 +0100 | [diff] [blame] | 4035 | /* 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] | 4036 | void mbedtls_endpoint_sanity(int endpoint_type) |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4037 | { |
| 4038 | enum { BUFFSIZE = 1024 }; |
| 4039 | mbedtls_endpoint ep; |
| 4040 | int ret = -1; |
| 4041 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4042 | ret = mbedtls_endpoint_init(NULL, endpoint_type, MBEDTLS_PK_RSA, |
| 4043 | NULL, NULL, NULL, NULL); |
| 4044 | TEST_ASSERT(MBEDTLS_ERR_SSL_BAD_INPUT_DATA == ret); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4045 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4046 | ret = mbedtls_endpoint_certificate_init(NULL, MBEDTLS_PK_RSA); |
| 4047 | TEST_ASSERT(MBEDTLS_ERR_SSL_BAD_INPUT_DATA == ret); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4048 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4049 | ret = mbedtls_endpoint_init(&ep, endpoint_type, MBEDTLS_PK_RSA, |
| 4050 | NULL, NULL, NULL, NULL); |
| 4051 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4052 | |
| 4053 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4054 | mbedtls_endpoint_free(&ep, NULL); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4055 | } |
| 4056 | /* END_CASE */ |
| 4057 | |
Gilles Peskine | 33d03fe | 2023-02-01 18:37:49 +0100 | [diff] [blame] | 4058 | /* 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] | 4059 | void move_handshake_to_state(int endpoint_type, int state, int need_pass) |
| 4060 | { |
| 4061 | enum { BUFFSIZE = 1024 }; |
| 4062 | mbedtls_endpoint base_ep, second_ep; |
| 4063 | int ret = -1; |
| 4064 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4065 | mbedtls_platform_zeroize(&base_ep, sizeof(base_ep)); |
| 4066 | mbedtls_platform_zeroize(&second_ep, sizeof(second_ep)); |
Andrzej Kurek | 0d2982b | 2022-10-18 07:55:46 -0400 | [diff] [blame] | 4067 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4068 | ret = mbedtls_endpoint_init(&base_ep, endpoint_type, MBEDTLS_PK_RSA, |
| 4069 | NULL, NULL, NULL, NULL); |
| 4070 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4071 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4072 | ret = mbedtls_endpoint_init(&second_ep, |
| 4073 | (endpoint_type == MBEDTLS_SSL_IS_SERVER) ? |
| 4074 | MBEDTLS_SSL_IS_CLIENT : MBEDTLS_SSL_IS_SERVER, |
| 4075 | MBEDTLS_PK_RSA, NULL, NULL, NULL, NULL); |
| 4076 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4077 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4078 | ret = mbedtls_mock_socket_connect(&(base_ep.socket), |
| 4079 | &(second_ep.socket), |
| 4080 | BUFFSIZE); |
| 4081 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4082 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4083 | ret = mbedtls_move_handshake_to_state(&(base_ep.ssl), |
| 4084 | &(second_ep.ssl), |
| 4085 | state); |
| 4086 | if (need_pass) { |
| 4087 | TEST_ASSERT(ret == 0); |
| 4088 | TEST_ASSERT(base_ep.ssl.state == state); |
| 4089 | } else { |
| 4090 | TEST_ASSERT(ret != 0); |
| 4091 | TEST_ASSERT(base_ep.ssl.state != state); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4092 | } |
| 4093 | |
| 4094 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4095 | mbedtls_endpoint_free(&base_ep, NULL); |
| 4096 | mbedtls_endpoint_free(&second_ep, NULL); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 4097 | } |
| 4098 | /* END_CASE */ |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 4099 | |
Gilles Peskine | 33d03fe | 2023-02-01 18:37:49 +0100 | [diff] [blame] | 4100 | /* 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] | 4101 | void handshake_version(int dtls, int client_min_version, int client_max_version, |
| 4102 | int server_min_version, int server_max_version, |
| 4103 | int expected_negotiated_version) |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 4104 | { |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 4105 | handshake_test_options options; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4106 | init_handshake_options(&options); |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 4107 | |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 4108 | options.client_min_version = client_min_version; |
| 4109 | options.client_max_version = client_max_version; |
| 4110 | options.server_min_version = server_min_version; |
| 4111 | options.server_max_version = server_max_version; |
| 4112 | |
| 4113 | options.expected_negotiated_version = expected_negotiated_version; |
| 4114 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 4115 | options.dtls = dtls; |
Piotr Nowicki | 438bf3b | 2020-03-10 12:59:10 +0100 | [diff] [blame] | 4116 | /* By default, SSLv3.0 and TLSv1.0 use 1/n-1 splitting when sending data, so |
| 4117 | * the number of fragments will be twice as big. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4118 | if (expected_negotiated_version == MBEDTLS_SSL_MINOR_VERSION_0 || |
| 4119 | expected_negotiated_version == MBEDTLS_SSL_MINOR_VERSION_1) { |
Piotr Nowicki | 438bf3b | 2020-03-10 12:59:10 +0100 | [diff] [blame] | 4120 | options.expected_cli_fragments = 2; |
| 4121 | options.expected_srv_fragments = 2; |
Andrzej Kurek | 941962e | 2020-02-07 09:20:32 -0500 | [diff] [blame] | 4122 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4123 | perform_handshake(&options); |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 4124 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 4125 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 4126 | goto exit; |
| 4127 | } |
| 4128 | /* END_CASE */ |
Andrzej Kurek | 9e9efdc | 2020-02-26 05:25:23 -0500 | [diff] [blame] | 4129 | |
Gilles Peskine | 33d03fe | 2023-02-01 18:37:49 +0100 | [diff] [blame] | 4130 | /* 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] | 4131 | 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] | 4132 | { |
| 4133 | handshake_test_options options; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4134 | init_handshake_options(&options); |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 4135 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 4136 | options.cipher = cipher; |
| 4137 | options.dtls = dtls; |
| 4138 | options.psk_str = psk_str; |
| 4139 | options.pk_alg = pk_alg; |
Andrzej Kurek | cc5169c | 2020-02-04 09:04:56 -0500 | [diff] [blame] | 4140 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4141 | perform_handshake(&options); |
Andrzej Kurek | 316da1f | 2020-02-26 09:03:47 -0500 | [diff] [blame] | 4142 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 4143 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 4144 | goto exit; |
| 4145 | } |
| 4146 | /* END_CASE */ |
Andrzej Kurek | 316da1f | 2020-02-26 09:03:47 -0500 | [diff] [blame] | 4147 | |
Gilles Peskine | 33d03fe | 2023-02-01 18:37:49 +0100 | [diff] [blame] | 4148 | /* 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] | 4149 | void handshake_cipher(char *cipher, int pk_alg, int dtls) |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 4150 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4151 | test_handshake_psk_cipher(cipher, pk_alg, NULL, dtls); |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 4152 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 4153 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 4154 | goto exit; |
| 4155 | } |
| 4156 | /* END_CASE */ |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 4157 | |
Gilles Peskine | 33d03fe | 2023-02-01 18:37:49 +0100 | [diff] [blame] | 4158 | /* 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] | 4159 | void app_data(int mfl, int cli_msg_len, int srv_msg_len, |
| 4160 | int expected_cli_fragments, |
| 4161 | int expected_srv_fragments, int dtls) |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 4162 | { |
| 4163 | handshake_test_options options; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4164 | init_handshake_options(&options); |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 4165 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 4166 | options.mfl = mfl; |
| 4167 | options.cli_msg_len = cli_msg_len; |
| 4168 | options.srv_msg_len = srv_msg_len; |
| 4169 | options.expected_cli_fragments = expected_cli_fragments; |
| 4170 | options.expected_srv_fragments = expected_srv_fragments; |
| 4171 | options.dtls = dtls; |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 4172 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4173 | perform_handshake(&options); |
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_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] | 4180 | void app_data_tls(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, 0); |
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_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 app_data_dtls(int mfl, int cli_msg_len, int srv_msg_len, |
| 4193 | int expected_cli_fragments, |
| 4194 | int expected_srv_fragments) |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 4195 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4196 | test_app_data(mfl, cli_msg_len, srv_msg_len, expected_cli_fragments, |
| 4197 | expected_srv_fragments, 1); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 4198 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 4199 | goto exit; |
| 4200 | } |
| 4201 | /* END_CASE */ |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 4202 | |
Gilles Peskine | 33d03fe | 2023-02-01 18:37:49 +0100 | [diff] [blame] | 4203 | /* 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] | 4204 | void handshake_serialization() |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 4205 | { |
| 4206 | handshake_test_options options; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4207 | init_handshake_options(&options); |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 4208 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 4209 | options.serialize = 1; |
| 4210 | options.dtls = 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4211 | perform_handshake(&options); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 4212 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 4213 | goto exit; |
| 4214 | } |
| 4215 | /* END_CASE */ |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 4216 | |
Gilles Peskine | 33d03fe | 2023-02-01 18:37:49 +0100 | [diff] [blame] | 4217 | /* 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] | 4218 | void handshake_fragmentation(int mfl, |
| 4219 | int expected_srv_hs_fragmentation, |
| 4220 | int expected_cli_hs_fragmentation) |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 4221 | { |
| 4222 | handshake_test_options options; |
| 4223 | log_pattern srv_pattern, cli_pattern; |
| 4224 | |
| 4225 | srv_pattern.pattern = cli_pattern.pattern = "found fragmented DTLS handshake"; |
| 4226 | srv_pattern.counter = 0; |
| 4227 | cli_pattern.counter = 0; |
| 4228 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4229 | init_handshake_options(&options); |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 4230 | options.dtls = 1; |
| 4231 | options.mfl = mfl; |
Darryl Green | aad82f9 | 2019-12-02 10:53:11 +0000 | [diff] [blame] | 4232 | /* Set cipher to one using CBC so that record splitting can be tested */ |
| 4233 | options.cipher = "TLS-DHE-RSA-WITH-AES-256-CBC-SHA256"; |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 4234 | options.srv_auth_mode = MBEDTLS_SSL_VERIFY_REQUIRED; |
| 4235 | options.srv_log_obj = &srv_pattern; |
| 4236 | options.cli_log_obj = &cli_pattern; |
| 4237 | options.srv_log_fun = log_analyzer; |
| 4238 | options.cli_log_fun = log_analyzer; |
| 4239 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4240 | perform_handshake(&options); |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 4241 | |
| 4242 | /* Test if the server received a fragmented handshake */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4243 | if (expected_srv_hs_fragmentation) { |
| 4244 | TEST_ASSERT(srv_pattern.counter >= 1); |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 4245 | } |
| 4246 | /* Test if the client received a fragmented handshake */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4247 | if (expected_cli_hs_fragmentation) { |
| 4248 | TEST_ASSERT(cli_pattern.counter >= 1); |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 4249 | } |
| 4250 | } |
| 4251 | /* END_CASE */ |
| 4252 | |
Gilles Peskine | 33d03fe | 2023-02-01 18:37:49 +0100 | [diff] [blame] | 4253 | /* 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] | 4254 | void renegotiation(int legacy_renegotiation) |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 4255 | { |
| 4256 | handshake_test_options options; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4257 | init_handshake_options(&options); |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 4258 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 4259 | options.renegotiate = 1; |
| 4260 | options.legacy_renegotiation = legacy_renegotiation; |
| 4261 | options.dtls = 1; |
Andrzej Kurek | 316da1f | 2020-02-26 09:03:47 -0500 | [diff] [blame] | 4262 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4263 | perform_handshake(&options); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 4264 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 4265 | goto exit; |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 4266 | } |
| 4267 | /* END_CASE */ |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 4268 | |
Gilles Peskine | 33d03fe | 2023-02-01 18:37:49 +0100 | [diff] [blame] | 4269 | /* 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] | 4270 | void resize_buffers(int mfl, int renegotiation, int legacy_renegotiation, |
| 4271 | int serialize, int dtls, char *cipher) |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 4272 | { |
| 4273 | handshake_test_options options; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4274 | init_handshake_options(&options); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 4275 | |
| 4276 | options.mfl = mfl; |
Andrzej Kurek | 8ea6872 | 2020-04-03 06:40:47 -0400 | [diff] [blame] | 4277 | options.cipher = cipher; |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 4278 | options.renegotiate = renegotiation; |
| 4279 | options.legacy_renegotiation = legacy_renegotiation; |
| 4280 | options.serialize = serialize; |
| 4281 | options.dtls = dtls; |
| 4282 | options.resize_buffers = 1; |
| 4283 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4284 | perform_handshake(&options); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 4285 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 4286 | goto exit; |
| 4287 | } |
| 4288 | /* END_CASE */ |
| 4289 | |
Gilles Peskine | 33d03fe | 2023-02-01 18:37:49 +0100 | [diff] [blame] | 4290 | /* 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] | 4291 | void resize_buffers_serialize_mfl(int mfl) |
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, 0, MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION, 1, 1, |
| 4294 | (char *) ""); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 4295 | |
| 4296 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 4297 | goto exit; |
| 4298 | } |
| 4299 | /* END_CASE */ |
| 4300 | |
Gilles Peskine | 33d03fe | 2023-02-01 18:37:49 +0100 | [diff] [blame] | 4301 | /* 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] | 4302 | void resize_buffers_renegotiate_mfl(int mfl, int legacy_renegotiation, |
| 4303 | char *cipher) |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 4304 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4305 | test_resize_buffers(mfl, 1, legacy_renegotiation, 0, 1, cipher); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 4306 | |
| 4307 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 4308 | goto exit; |
| 4309 | } |
| 4310 | /* END_CASE */ |
Manuel Pégourié-Gonnard | 045f094 | 2020-07-02 11:34:02 +0200 | [diff] [blame] | 4311 | |
Gilles Peskine | 33d03fe | 2023-02-01 18:37:49 +0100 | [diff] [blame] | 4312 | /* 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] | 4313 | void raw_key_agreement_fail(int bad_server_ecdhe_key) |
Andrzej Kurek | b4eedf7 | 2022-04-15 05:41:14 -0400 | [diff] [blame] | 4314 | { |
| 4315 | enum { BUFFSIZE = 17000 }; |
| 4316 | mbedtls_endpoint client, server; |
| 4317 | mbedtls_psa_stats_t stats; |
Andrzej Kurek | 2582ba3 | 2022-03-31 06:30:54 -0400 | [diff] [blame] | 4318 | size_t free_slots_before = -1; |
Andrzej Kurek | b4eedf7 | 2022-04-15 05:41:14 -0400 | [diff] [blame] | 4319 | |
Andrzej Kurek | b4eedf7 | 2022-04-15 05:41:14 -0400 | [diff] [blame] | 4320 | mbedtls_ecp_group_id curve_list[] = { MBEDTLS_ECP_DP_SECP256R1, |
| 4321 | MBEDTLS_ECP_DP_NONE }; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4322 | USE_PSA_INIT(); |
| 4323 | mbedtls_platform_zeroize(&client, sizeof(client)); |
| 4324 | mbedtls_platform_zeroize(&server, sizeof(server)); |
Andrzej Kurek | b4eedf7 | 2022-04-15 05:41:14 -0400 | [diff] [blame] | 4325 | |
| 4326 | /* Client side, force SECP256R1 to make one key bitflip fail |
Andrzej Kurek | 9cb14d4 | 2022-04-14 08:51:41 -0400 | [diff] [blame] | 4327 | * the raw key agreement. Flipping the first byte makes the |
| 4328 | * required 0x04 identifier invalid. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4329 | TEST_EQUAL(mbedtls_endpoint_init(&client, MBEDTLS_SSL_IS_CLIENT, |
| 4330 | MBEDTLS_PK_ECDSA, NULL, NULL, |
| 4331 | NULL, curve_list), 0); |
Andrzej Kurek | b4eedf7 | 2022-04-15 05:41:14 -0400 | [diff] [blame] | 4332 | |
| 4333 | /* Server side */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4334 | TEST_EQUAL(mbedtls_endpoint_init(&server, MBEDTLS_SSL_IS_SERVER, |
| 4335 | MBEDTLS_PK_ECDSA, NULL, NULL, |
| 4336 | NULL, NULL), 0); |
Andrzej Kurek | b4eedf7 | 2022-04-15 05:41:14 -0400 | [diff] [blame] | 4337 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4338 | TEST_EQUAL(mbedtls_mock_socket_connect(&(client.socket), |
| 4339 | &(server.socket), |
| 4340 | BUFFSIZE), 0); |
Andrzej Kurek | b4eedf7 | 2022-04-15 05:41:14 -0400 | [diff] [blame] | 4341 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4342 | TEST_EQUAL(mbedtls_move_handshake_to_state(&(client.ssl), |
| 4343 | &(server.ssl), |
| 4344 | MBEDTLS_SSL_CLIENT_KEY_EXCHANGE) |
| 4345 | , 0); |
Andrzej Kurek | b4eedf7 | 2022-04-15 05:41:14 -0400 | [diff] [blame] | 4346 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4347 | mbedtls_psa_get_stats(&stats); |
Andrzej Kurek | 2582ba3 | 2022-03-31 06:30:54 -0400 | [diff] [blame] | 4348 | /* Save the number of slots in use up to this point. |
| 4349 | * With PSA, one can be used for the ECDH private key. */ |
| 4350 | free_slots_before = stats.empty_slots; |
Andrzej Kurek | 99f6778 | 2022-03-31 07:17:18 -0400 | [diff] [blame] | 4351 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4352 | if (bad_server_ecdhe_key) { |
Gilles Peskine | 6dd489c | 2022-04-15 05:54:40 -0400 | [diff] [blame] | 4353 | /* Force a simulated bitflip in the server key. to make the |
| 4354 | * raw key agreement in ssl_write_client_key_exchange fail. */ |
| 4355 | (client.ssl).handshake->ecdh_psa_peerkey[0] ^= 0x02; |
| 4356 | } |
Andrzej Kurek | b4eedf7 | 2022-04-15 05:41:14 -0400 | [diff] [blame] | 4357 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4358 | TEST_EQUAL(mbedtls_move_handshake_to_state(&(client.ssl), |
| 4359 | &(server.ssl), |
| 4360 | MBEDTLS_SSL_HANDSHAKE_OVER), |
| 4361 | bad_server_ecdhe_key ? MBEDTLS_ERR_SSL_HW_ACCEL_FAILED : 0); |
Andrzej Kurek | b4eedf7 | 2022-04-15 05:41:14 -0400 | [diff] [blame] | 4362 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4363 | mbedtls_psa_get_stats(&stats); |
Andrzej Kurek | b4eedf7 | 2022-04-15 05:41:14 -0400 | [diff] [blame] | 4364 | |
Gilles Peskine | 6dd489c | 2022-04-15 05:54:40 -0400 | [diff] [blame] | 4365 | /* Make sure that the key slot is already destroyed in case of failure, |
| 4366 | * without waiting to close the connection. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4367 | if (bad_server_ecdhe_key) { |
| 4368 | TEST_EQUAL(free_slots_before, stats.empty_slots); |
| 4369 | } |
Andrzej Kurek | b4eedf7 | 2022-04-15 05:41:14 -0400 | [diff] [blame] | 4370 | |
| 4371 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4372 | mbedtls_endpoint_free(&client, NULL); |
| 4373 | mbedtls_endpoint_free(&server, NULL); |
Andrzej Kurek | 99f6778 | 2022-03-31 07:17:18 -0400 | [diff] [blame] | 4374 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4375 | USE_PSA_DONE(); |
Andrzej Kurek | b4eedf7 | 2022-04-15 05:41:14 -0400 | [diff] [blame] | 4376 | } |
| 4377 | /* END_CASE */ |
Andrzej Kurek | 862acb8 | 2022-06-06 13:08:23 -0400 | [diff] [blame] | 4378 | |
Andrzej Kurek | 3c036f5 | 2022-06-08 11:57:57 -0400 | [diff] [blame] | 4379 | /* 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] | 4380 | void cookie_parsing(data_t *cookie, int exp_ret) |
Andrzej Kurek | 862acb8 | 2022-06-06 13:08:23 -0400 | [diff] [blame] | 4381 | { |
| 4382 | mbedtls_ssl_context ssl; |
| 4383 | mbedtls_ssl_config conf; |
| 4384 | size_t len; |
| 4385 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4386 | mbedtls_ssl_init(&ssl); |
| 4387 | mbedtls_ssl_config_init(&conf); |
| 4388 | TEST_EQUAL(mbedtls_ssl_config_defaults(&conf, MBEDTLS_SSL_IS_SERVER, |
| 4389 | MBEDTLS_SSL_TRANSPORT_DATAGRAM, |
| 4390 | MBEDTLS_SSL_PRESET_DEFAULT), |
| 4391 | 0); |
Andrzej Kurek | 862acb8 | 2022-06-06 13:08:23 -0400 | [diff] [blame] | 4392 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4393 | TEST_EQUAL(mbedtls_ssl_setup(&ssl, &conf), 0); |
| 4394 | TEST_EQUAL(mbedtls_ssl_check_dtls_clihlo_cookie(&ssl, ssl.cli_id, |
| 4395 | ssl.cli_id_len, |
| 4396 | cookie->x, cookie->len, |
| 4397 | ssl.out_buf, |
| 4398 | MBEDTLS_SSL_OUT_CONTENT_LEN, |
| 4399 | &len), |
| 4400 | exp_ret); |
Andrzej Kurek | 862acb8 | 2022-06-06 13:08:23 -0400 | [diff] [blame] | 4401 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 4402 | mbedtls_ssl_free(&ssl); |
| 4403 | mbedtls_ssl_config_free(&conf); |
Andrzej Kurek | 862acb8 | 2022-06-06 13:08:23 -0400 | [diff] [blame] | 4404 | } |
| 4405 | /* END_CASE */ |