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