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