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