Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 1 | /* BEGIN_HEADER */ |
Yanray Wang | 5fce145 | 2022-10-24 14:42:01 +0800 | [diff] [blame] | 2 | #include <test/ssl_helpers.h> |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 3 | |
Gabor Mezei | c0ae1cf | 2021-10-20 12:09:35 +0200 | [diff] [blame] | 4 | #include <constant_time_internal.h> |
Manuel Pégourié-Gonnard | 045f094 | 2020-07-02 11:34:02 +0200 | [diff] [blame] | 5 | |
Manuel Pégourié-Gonnard | 9670a59 | 2020-07-10 10:21:46 +0200 | [diff] [blame] | 6 | #include <test/constant_flow.h> |
| 7 | |
Valerio Setti | 8388fdd | 2023-04-28 12:27:14 +0200 | [diff] [blame] | 8 | #define SSL_MESSAGE_QUEUE_INIT { NULL, 0, 0, 0 } |
| 9 | |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 10 | /* END_HEADER */ |
| 11 | |
| 12 | /* BEGIN_DEPENDENCIES |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 13 | * depends_on:MBEDTLS_SSL_TLS_C |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 14 | * END_DEPENDENCIES |
| 15 | */ |
| 16 | |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 17 | /* BEGIN_CASE */ |
| 18 | void test_callback_buffer_sanity() |
| 19 | { |
| 20 | enum { MSGLEN = 10 }; |
Yanray Wang | abfdcd8 | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 21 | mbedtls_test_ssl_buffer buf; |
Gilles Peskine | 11f4179 | 2023-10-17 16:35:20 +0200 | [diff] [blame] | 22 | mbedtls_test_ssl_buffer_init(&buf); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 23 | unsigned char input[MSGLEN]; |
| 24 | unsigned char output[MSGLEN]; |
| 25 | |
Valerio Setti | 44570a5 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 26 | USE_PSA_INIT(); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 27 | memset(input, 0, sizeof(input)); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 28 | |
| 29 | /* Make sure calling put and get on NULL buffer results in error. */ |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 30 | TEST_ASSERT(mbedtls_test_ssl_buffer_put(NULL, input, sizeof(input)) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 31 | == -1); |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 32 | TEST_ASSERT(mbedtls_test_ssl_buffer_get(NULL, output, sizeof(output)) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 33 | == -1); |
Yanray Wang | 862ef86 | 2023-03-14 11:05:04 +0800 | [diff] [blame] | 34 | TEST_ASSERT(mbedtls_test_ssl_buffer_put(NULL, NULL, sizeof(input)) |
| 35 | == -1); |
Andrzej Kurek | f777414 | 2020-01-22 06:34:59 -0500 | [diff] [blame] | 36 | |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 37 | TEST_ASSERT(mbedtls_test_ssl_buffer_put(NULL, NULL, 0) == -1); |
| 38 | TEST_ASSERT(mbedtls_test_ssl_buffer_get(NULL, NULL, 0) == -1); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 39 | |
| 40 | /* 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] | 41 | * in error. */ |
Yanray Wang | 862ef86 | 2023-03-14 11:05:04 +0800 | [diff] [blame] | 42 | TEST_ASSERT(mbedtls_test_ssl_buffer_put(&buf, input, sizeof(input)) |
| 43 | == -1); |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 44 | TEST_ASSERT(mbedtls_test_ssl_buffer_get(&buf, output, sizeof(output)) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 45 | == -1); |
Yanray Wang | 862ef86 | 2023-03-14 11:05:04 +0800 | [diff] [blame] | 46 | TEST_ASSERT(mbedtls_test_ssl_buffer_put(&buf, NULL, sizeof(input)) |
| 47 | == -1); |
Andrzej Kurek | f777414 | 2020-01-22 06:34:59 -0500 | [diff] [blame] | 48 | |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 49 | TEST_ASSERT(mbedtls_test_ssl_buffer_put(&buf, NULL, 0) == -1); |
| 50 | TEST_ASSERT(mbedtls_test_ssl_buffer_get(&buf, NULL, 0) == -1); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 51 | |
Andrzej Kurek | f777414 | 2020-01-22 06:34:59 -0500 | [diff] [blame] | 52 | /* Make sure calling put and get on NULL input only results in |
| 53 | * error if the length is not zero, and that a NULL output is valid for data |
| 54 | * dropping. |
| 55 | */ |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 56 | |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 57 | TEST_ASSERT(mbedtls_test_ssl_buffer_setup(&buf, sizeof(input)) == 0); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 58 | |
Yanray Wang | 862ef86 | 2023-03-14 11:05:04 +0800 | [diff] [blame] | 59 | TEST_ASSERT(mbedtls_test_ssl_buffer_put(&buf, NULL, sizeof(input)) |
| 60 | == -1); |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 61 | TEST_ASSERT(mbedtls_test_ssl_buffer_get(&buf, NULL, sizeof(output)) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 62 | == 0); |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 63 | TEST_ASSERT(mbedtls_test_ssl_buffer_put(&buf, NULL, 0) == 0); |
| 64 | TEST_ASSERT(mbedtls_test_ssl_buffer_get(&buf, NULL, 0) == 0); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 65 | |
Piotr Nowicki | fb437d7 | 2020-01-13 16:59:12 +0100 | [diff] [blame] | 66 | /* Make sure calling put several times in the row is safe */ |
| 67 | |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 68 | TEST_ASSERT(mbedtls_test_ssl_buffer_put(&buf, input, sizeof(input)) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 69 | == sizeof(input)); |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 70 | TEST_ASSERT(mbedtls_test_ssl_buffer_get(&buf, output, 2) == 2); |
| 71 | TEST_ASSERT(mbedtls_test_ssl_buffer_put(&buf, input, 1) == 1); |
| 72 | TEST_ASSERT(mbedtls_test_ssl_buffer_put(&buf, input, 2) == 1); |
| 73 | TEST_ASSERT(mbedtls_test_ssl_buffer_put(&buf, input, 2) == 0); |
Piotr Nowicki | fb437d7 | 2020-01-13 16:59:12 +0100 | [diff] [blame] | 74 | |
| 75 | |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 76 | exit: |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 77 | mbedtls_test_ssl_buffer_free(&buf); |
Valerio Setti | 44570a5 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 78 | USE_PSA_DONE(); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 79 | } |
| 80 | /* END_CASE */ |
| 81 | |
| 82 | /* |
Yanray Wang | abfdcd8 | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 83 | * Test if the implementation of `mbedtls_test_ssl_buffer` related functions is |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 84 | * correct and works as expected. |
| 85 | * |
| 86 | * That is |
| 87 | * - If we try to put in \p put1 bytes then we can put in \p put1_ret bytes. |
| 88 | * - Afterwards if we try to get \p get1 bytes then we can get \get1_ret bytes. |
| 89 | * - Next, if we try to put in \p put1 bytes then we can put in \p put1_ret |
| 90 | * bytes. |
| 91 | * - Afterwards if we try to get \p get1 bytes then we can get \get1_ret bytes. |
| 92 | * - All of the bytes we got match the bytes we put in in a FIFO manner. |
| 93 | */ |
| 94 | |
| 95 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 96 | void test_callback_buffer(int size, int put1, int put1_ret, |
| 97 | int get1, int get1_ret, int put2, int put2_ret, |
| 98 | int get2, int get2_ret) |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 99 | { |
| 100 | enum { ROUNDS = 2 }; |
| 101 | size_t put[ROUNDS]; |
| 102 | int put_ret[ROUNDS]; |
| 103 | size_t get[ROUNDS]; |
| 104 | int get_ret[ROUNDS]; |
Yanray Wang | abfdcd8 | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 105 | mbedtls_test_ssl_buffer buf; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 106 | unsigned char *input = NULL; |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 107 | size_t input_len; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 108 | unsigned char *output = NULL; |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 109 | size_t output_len; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 110 | size_t i, j, written, read; |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 111 | |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 112 | mbedtls_test_ssl_buffer_init(&buf); |
Valerio Setti | 89ae9b6 | 2023-04-27 17:22:54 +0200 | [diff] [blame] | 113 | USE_PSA_INIT(); |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 114 | TEST_ASSERT(mbedtls_test_ssl_buffer_setup(&buf, size) == 0); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 115 | |
| 116 | /* Check the sanity of input parameters and initialise local variables. That |
| 117 | * is, ensure that the amount of data is not negative and that we are not |
| 118 | * expecting more to put or get than we actually asked for. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 119 | TEST_ASSERT(put1 >= 0); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 120 | put[0] = put1; |
| 121 | put_ret[0] = put1_ret; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 122 | TEST_ASSERT(put1_ret <= put1); |
| 123 | TEST_ASSERT(put2 >= 0); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 124 | put[1] = put2; |
| 125 | put_ret[1] = put2_ret; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 126 | TEST_ASSERT(put2_ret <= put2); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 127 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 128 | TEST_ASSERT(get1 >= 0); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 129 | get[0] = get1; |
| 130 | get_ret[0] = get1_ret; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 131 | TEST_ASSERT(get1_ret <= get1); |
| 132 | TEST_ASSERT(get2 >= 0); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 133 | get[1] = get2; |
| 134 | get_ret[1] = get2_ret; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 135 | TEST_ASSERT(get2_ret <= get2); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 136 | |
| 137 | input_len = 0; |
| 138 | /* Calculate actual input and output lengths */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 139 | for (j = 0; j < ROUNDS; j++) { |
| 140 | if (put_ret[j] > 0) { |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 141 | input_len += put_ret[j]; |
| 142 | } |
| 143 | } |
| 144 | /* In order to always have a valid pointer we always allocate at least 1 |
| 145 | * byte. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 146 | if (input_len == 0) { |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 147 | input_len = 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 148 | } |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 149 | TEST_CALLOC(input, input_len); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 150 | |
| 151 | output_len = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 152 | for (j = 0; j < ROUNDS; j++) { |
| 153 | if (get_ret[j] > 0) { |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 154 | output_len += get_ret[j]; |
| 155 | } |
| 156 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 157 | TEST_ASSERT(output_len <= input_len); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 158 | /* In order to always have a valid pointer we always allocate at least 1 |
| 159 | * byte. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 160 | if (output_len == 0) { |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 161 | output_len = 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 162 | } |
Tom Cosgrove | 30ceb23 | 2023-09-04 11:20:19 +0100 | [diff] [blame] | 163 | TEST_CALLOC(output, output_len); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 164 | |
| 165 | /* Fill up the buffer with structured data so that unwanted changes |
| 166 | * can be detected */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 167 | for (i = 0; i < input_len; i++) { |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 168 | input[i] = i & 0xFF; |
| 169 | } |
| 170 | |
| 171 | written = read = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 172 | for (j = 0; j < ROUNDS; j++) { |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 173 | TEST_ASSERT(put_ret[j] == mbedtls_test_ssl_buffer_put(&buf, |
| 174 | input + written, put[j])); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 175 | written += put_ret[j]; |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 176 | TEST_ASSERT(get_ret[j] == mbedtls_test_ssl_buffer_get(&buf, |
| 177 | output + read, get[j])); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 178 | read += get_ret[j]; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 179 | TEST_ASSERT(read <= written); |
| 180 | if (get_ret[j] > 0) { |
| 181 | TEST_ASSERT(memcmp(output + read - get_ret[j], |
| 182 | input + read - get_ret[j], get_ret[j]) |
| 183 | == 0); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | |
| 187 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 188 | mbedtls_free(input); |
| 189 | mbedtls_free(output); |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 190 | mbedtls_test_ssl_buffer_free(&buf); |
Valerio Setti | 44570a5 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 191 | USE_PSA_DONE(); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 192 | } |
| 193 | /* END_CASE */ |
| 194 | |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 195 | /* |
Yanray Wang | 862ef86 | 2023-03-14 11:05:04 +0800 | [diff] [blame] | 196 | * Test if the implementation of `mbedtls_test_mock_socket` related |
| 197 | * I/O functions is correct and works as expected on unconnected sockets. |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 198 | */ |
| 199 | |
| 200 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 201 | void ssl_mock_sanity() |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 202 | { |
| 203 | enum { MSGLEN = 105 }; |
Paul Elliott | 9545786 | 2021-11-24 16:54:26 +0000 | [diff] [blame] | 204 | unsigned char message[MSGLEN] = { 0 }; |
| 205 | unsigned char received[MSGLEN] = { 0 }; |
Yanray Wang | abfdcd8 | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 206 | mbedtls_test_mock_socket socket; |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 207 | |
Yanray Wang | d02c317 | 2023-03-15 16:02:29 +0800 | [diff] [blame] | 208 | mbedtls_test_mock_socket_init(&socket); |
Valerio Setti | 89ae9b6 | 2023-04-27 17:22:54 +0200 | [diff] [blame] | 209 | USE_PSA_INIT(); |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 210 | TEST_ASSERT(mbedtls_test_mock_tcp_send_b(&socket, message, MSGLEN) < 0); |
| 211 | mbedtls_test_mock_socket_close(&socket); |
Yanray Wang | d02c317 | 2023-03-15 16:02:29 +0800 | [diff] [blame] | 212 | mbedtls_test_mock_socket_init(&socket); |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 213 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_b(&socket, received, MSGLEN) < 0); |
| 214 | mbedtls_test_mock_socket_close(&socket); |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 215 | |
Yanray Wang | d02c317 | 2023-03-15 16:02:29 +0800 | [diff] [blame] | 216 | mbedtls_test_mock_socket_init(&socket); |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 217 | TEST_ASSERT(mbedtls_test_mock_tcp_send_nb(&socket, message, MSGLEN) < 0); |
| 218 | mbedtls_test_mock_socket_close(&socket); |
Yanray Wang | d02c317 | 2023-03-15 16:02:29 +0800 | [diff] [blame] | 219 | mbedtls_test_mock_socket_init(&socket); |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 220 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_nb(&socket, received, MSGLEN) < 0); |
| 221 | mbedtls_test_mock_socket_close(&socket); |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 222 | |
| 223 | exit: |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 224 | mbedtls_test_mock_socket_close(&socket); |
Valerio Setti | 44570a5 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 225 | USE_PSA_DONE(); |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 226 | } |
| 227 | /* END_CASE */ |
| 228 | |
| 229 | /* |
Yanray Wang | 862ef86 | 2023-03-14 11:05:04 +0800 | [diff] [blame] | 230 | * Test if the implementation of `mbedtls_test_mock_socket` related functions |
| 231 | * can send a single message from the client to the server. |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 232 | */ |
| 233 | |
| 234 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 235 | void ssl_mock_tcp(int blocking) |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 236 | { |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 237 | enum { MSGLEN = 105 }; |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 238 | enum { BUFLEN = MSGLEN / 5 }; |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 239 | unsigned char message[MSGLEN]; |
| 240 | unsigned char received[MSGLEN]; |
Yanray Wang | abfdcd8 | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 241 | mbedtls_test_mock_socket client; |
| 242 | mbedtls_test_mock_socket server; |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 243 | size_t written, read; |
| 244 | int send_ret, recv_ret; |
| 245 | mbedtls_ssl_send_t *send; |
| 246 | mbedtls_ssl_recv_t *recv; |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 247 | unsigned i; |
| 248 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 249 | if (blocking == 0) { |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 250 | send = mbedtls_test_mock_tcp_send_nb; |
| 251 | recv = mbedtls_test_mock_tcp_recv_nb; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 252 | } else { |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 253 | send = mbedtls_test_mock_tcp_send_b; |
| 254 | recv = mbedtls_test_mock_tcp_recv_b; |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 255 | } |
| 256 | |
Yanray Wang | d02c317 | 2023-03-15 16:02:29 +0800 | [diff] [blame] | 257 | mbedtls_test_mock_socket_init(&client); |
| 258 | mbedtls_test_mock_socket_init(&server); |
Valerio Setti | 89ae9b6 | 2023-04-27 17:22:54 +0200 | [diff] [blame] | 259 | USE_PSA_INIT(); |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 260 | |
| 261 | /* Fill up the buffer with structured data so that unwanted changes |
| 262 | * can be detected */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 263 | for (i = 0; i < MSGLEN; i++) { |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 264 | message[i] = i & 0xFF; |
| 265 | } |
| 266 | |
| 267 | /* Make sure that sending a message takes a few iterations. */ |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 268 | TEST_ASSERT(0 == mbedtls_test_mock_socket_connect(&client, &server, BUFLEN)); |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 269 | |
| 270 | /* Send the message to the server */ |
| 271 | send_ret = recv_ret = 1; |
| 272 | written = read = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 273 | while (send_ret != 0 || recv_ret != 0) { |
| 274 | send_ret = send(&client, message + written, MSGLEN - written); |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 275 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 276 | TEST_ASSERT(send_ret >= 0); |
| 277 | TEST_ASSERT(send_ret <= BUFLEN); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 278 | written += send_ret; |
| 279 | |
| 280 | /* 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] | 281 | if (send_ret == BUFLEN) { |
| 282 | int blocking_ret = send(&client, message, 1); |
| 283 | if (blocking) { |
| 284 | TEST_ASSERT(blocking_ret == 0); |
| 285 | } else { |
| 286 | TEST_ASSERT(blocking_ret == MBEDTLS_ERR_SSL_WANT_WRITE); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 287 | } |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 288 | } |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 289 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 290 | recv_ret = recv(&server, received + read, MSGLEN - read); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 291 | |
| 292 | /* The result depends on whether any data was sent */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 293 | if (send_ret > 0) { |
| 294 | TEST_ASSERT(recv_ret > 0); |
| 295 | TEST_ASSERT(recv_ret <= BUFLEN); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 296 | read += recv_ret; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 297 | } else if (blocking) { |
| 298 | TEST_ASSERT(recv_ret == 0); |
| 299 | } else { |
| 300 | TEST_ASSERT(recv_ret == MBEDTLS_ERR_SSL_WANT_READ); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 301 | recv_ret = 0; |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 302 | } |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 303 | |
| 304 | /* 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] | 305 | if (recv_ret == BUFLEN) { |
| 306 | int blocking_ret = recv(&server, received, 1); |
| 307 | if (blocking) { |
| 308 | TEST_ASSERT(blocking_ret == 0); |
| 309 | } else { |
| 310 | TEST_ASSERT(blocking_ret == MBEDTLS_ERR_SSL_WANT_READ); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 311 | } |
| 312 | } |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 313 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 314 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 315 | |
| 316 | exit: |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 317 | mbedtls_test_mock_socket_close(&client); |
| 318 | mbedtls_test_mock_socket_close(&server); |
Valerio Setti | 44570a5 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 319 | USE_PSA_DONE(); |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 320 | } |
| 321 | /* END_CASE */ |
| 322 | |
| 323 | /* |
Yanray Wang | 862ef86 | 2023-03-14 11:05:04 +0800 | [diff] [blame] | 324 | * Test if the implementation of `mbedtls_test_mock_socket` related functions |
| 325 | * 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] | 326 | * interleaving). |
| 327 | */ |
| 328 | |
| 329 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 330 | void ssl_mock_tcp_interleaving(int blocking) |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 331 | { |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 332 | enum { ROUNDS = 2 }; |
| 333 | enum { MSGLEN = 105 }; |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 334 | enum { BUFLEN = MSGLEN / 5 }; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 335 | unsigned char message[ROUNDS][MSGLEN]; |
| 336 | unsigned char received[ROUNDS][MSGLEN]; |
Yanray Wang | abfdcd8 | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 337 | mbedtls_test_mock_socket client; |
| 338 | mbedtls_test_mock_socket server; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 339 | size_t written[ROUNDS]; |
| 340 | size_t read[ROUNDS]; |
| 341 | int send_ret[ROUNDS]; |
| 342 | int recv_ret[ROUNDS]; |
| 343 | unsigned i, j, progress; |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 344 | mbedtls_ssl_send_t *send; |
| 345 | mbedtls_ssl_recv_t *recv; |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 346 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 347 | if (blocking == 0) { |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 348 | send = mbedtls_test_mock_tcp_send_nb; |
| 349 | recv = mbedtls_test_mock_tcp_recv_nb; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 350 | } else { |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 351 | send = mbedtls_test_mock_tcp_send_b; |
| 352 | recv = mbedtls_test_mock_tcp_recv_b; |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 353 | } |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 354 | |
Yanray Wang | d02c317 | 2023-03-15 16:02:29 +0800 | [diff] [blame] | 355 | mbedtls_test_mock_socket_init(&client); |
| 356 | mbedtls_test_mock_socket_init(&server); |
Valerio Setti | 89ae9b6 | 2023-04-27 17:22:54 +0200 | [diff] [blame] | 357 | USE_PSA_INIT(); |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 358 | |
| 359 | /* Fill up the buffers with structured data so that unwanted changes |
| 360 | * can be detected */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 361 | for (i = 0; i < ROUNDS; i++) { |
| 362 | for (j = 0; j < MSGLEN; j++) { |
| 363 | message[i][j] = (i * MSGLEN + j) & 0xFF; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 364 | } |
| 365 | } |
| 366 | |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 367 | /* Make sure that sending a message takes a few iterations. */ |
Yanray Wang | 862ef86 | 2023-03-14 11:05:04 +0800 | [diff] [blame] | 368 | TEST_ASSERT(0 == mbedtls_test_mock_socket_connect(&client, &server, |
| 369 | BUFLEN)); |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 370 | |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 371 | /* Send the message from both sides, interleaving. */ |
| 372 | progress = 1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 373 | for (i = 0; i < ROUNDS; i++) { |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 374 | written[i] = 0; |
| 375 | read[i] = 0; |
| 376 | } |
| 377 | /* This loop does not stop as long as there was a successful write or read |
| 378 | * of at least one byte on either side. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 379 | while (progress != 0) { |
Yanray Wang | abfdcd8 | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 380 | mbedtls_test_mock_socket *socket; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 381 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 382 | for (i = 0; i < ROUNDS; i++) { |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 383 | /* First sending is from the client */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 384 | socket = (i % 2 == 0) ? (&client) : (&server); |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 385 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 386 | send_ret[i] = send(socket, message[i] + written[i], |
| 387 | MSGLEN - written[i]); |
| 388 | TEST_ASSERT(send_ret[i] >= 0); |
| 389 | TEST_ASSERT(send_ret[i] <= BUFLEN); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 390 | written[i] += send_ret[i]; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 391 | |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 392 | /* If the buffer is full we can test blocking and non-blocking |
| 393 | * send */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 394 | if (send_ret[i] == BUFLEN) { |
| 395 | int blocking_ret = send(socket, message[i], 1); |
| 396 | if (blocking) { |
| 397 | TEST_ASSERT(blocking_ret == 0); |
| 398 | } else { |
| 399 | TEST_ASSERT(blocking_ret == MBEDTLS_ERR_SSL_WANT_WRITE); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 400 | } |
| 401 | } |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 402 | } |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 403 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 404 | for (i = 0; i < ROUNDS; i++) { |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 405 | /* First receiving is from the server */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 406 | socket = (i % 2 == 0) ? (&server) : (&client); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 407 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 408 | recv_ret[i] = recv(socket, received[i] + read[i], |
| 409 | MSGLEN - read[i]); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 410 | |
| 411 | /* The result depends on whether any data was sent */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 412 | if (send_ret[i] > 0) { |
| 413 | TEST_ASSERT(recv_ret[i] > 0); |
| 414 | TEST_ASSERT(recv_ret[i] <= BUFLEN); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 415 | read[i] += recv_ret[i]; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 416 | } else if (blocking) { |
| 417 | TEST_ASSERT(recv_ret[i] == 0); |
| 418 | } else { |
| 419 | TEST_ASSERT(recv_ret[i] == MBEDTLS_ERR_SSL_WANT_READ); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 420 | recv_ret[i] = 0; |
| 421 | } |
| 422 | |
| 423 | /* If the buffer is empty we can test blocking and non-blocking |
| 424 | * read */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 425 | if (recv_ret[i] == BUFLEN) { |
| 426 | int blocking_ret = recv(socket, received[i], 1); |
| 427 | if (blocking) { |
| 428 | TEST_ASSERT(blocking_ret == 0); |
| 429 | } else { |
| 430 | TEST_ASSERT(blocking_ret == MBEDTLS_ERR_SSL_WANT_READ); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 431 | } |
| 432 | } |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 433 | } |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 434 | |
| 435 | progress = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 436 | for (i = 0; i < ROUNDS; i++) { |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 437 | progress += send_ret[i] + recv_ret[i]; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 438 | } |
| 439 | } |
| 440 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 441 | for (i = 0; i < ROUNDS; i++) { |
| 442 | TEST_ASSERT(memcmp(message[i], received[i], MSGLEN) == 0); |
| 443 | } |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 444 | |
| 445 | exit: |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 446 | mbedtls_test_mock_socket_close(&client); |
| 447 | mbedtls_test_mock_socket_close(&server); |
Valerio Setti | 44570a5 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 448 | USE_PSA_DONE(); |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 449 | } |
| 450 | /* END_CASE */ |
| 451 | |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 452 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 453 | void ssl_message_queue_sanity() |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 454 | { |
Valerio Setti | 8388fdd | 2023-04-28 12:27:14 +0200 | [diff] [blame] | 455 | mbedtls_test_ssl_message_queue queue = SSL_MESSAGE_QUEUE_INIT; |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 456 | |
Valerio Setti | 44570a5 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 457 | USE_PSA_INIT(); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 458 | /* Trying to push/pull to an empty queue */ |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 459 | TEST_ASSERT(mbedtls_test_ssl_message_queue_push_info(NULL, 1) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 460 | == MBEDTLS_TEST_ERROR_ARG_NULL); |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 461 | TEST_ASSERT(mbedtls_test_ssl_message_queue_pop_info(NULL, 1) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 462 | == MBEDTLS_TEST_ERROR_ARG_NULL); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 463 | |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 464 | TEST_ASSERT(mbedtls_test_ssl_message_queue_setup(&queue, 3) == 0); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 465 | TEST_ASSERT(queue.capacity == 3); |
| 466 | TEST_ASSERT(queue.num == 0); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 467 | |
| 468 | exit: |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 469 | mbedtls_test_ssl_message_queue_free(&queue); |
Valerio Setti | 44570a5 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 470 | USE_PSA_DONE(); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 471 | } |
| 472 | /* END_CASE */ |
| 473 | |
| 474 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 475 | void ssl_message_queue_basic() |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 476 | { |
Valerio Setti | 8388fdd | 2023-04-28 12:27:14 +0200 | [diff] [blame] | 477 | mbedtls_test_ssl_message_queue queue = SSL_MESSAGE_QUEUE_INIT; |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 478 | |
Valerio Setti | 44570a5 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 479 | USE_PSA_INIT(); |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 480 | TEST_ASSERT(mbedtls_test_ssl_message_queue_setup(&queue, 3) == 0); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 481 | |
| 482 | /* Sanity test - 3 pushes and 3 pops with sufficient space */ |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 483 | TEST_ASSERT(mbedtls_test_ssl_message_queue_push_info(&queue, 1) == 1); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 484 | TEST_ASSERT(queue.capacity == 3); |
| 485 | TEST_ASSERT(queue.num == 1); |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 486 | TEST_ASSERT(mbedtls_test_ssl_message_queue_push_info(&queue, 1) == 1); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 487 | TEST_ASSERT(queue.capacity == 3); |
| 488 | TEST_ASSERT(queue.num == 2); |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 489 | TEST_ASSERT(mbedtls_test_ssl_message_queue_push_info(&queue, 2) == 2); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 490 | TEST_ASSERT(queue.capacity == 3); |
| 491 | TEST_ASSERT(queue.num == 3); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 492 | |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 493 | TEST_ASSERT(mbedtls_test_ssl_message_queue_pop_info(&queue, 1) == 1); |
| 494 | TEST_ASSERT(mbedtls_test_ssl_message_queue_pop_info(&queue, 1) == 1); |
| 495 | TEST_ASSERT(mbedtls_test_ssl_message_queue_pop_info(&queue, 2) == 2); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 496 | |
| 497 | exit: |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 498 | mbedtls_test_ssl_message_queue_free(&queue); |
Valerio Setti | 44570a5 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 499 | USE_PSA_DONE(); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 500 | } |
| 501 | /* END_CASE */ |
| 502 | |
| 503 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 504 | void ssl_message_queue_overflow_underflow() |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 505 | { |
Valerio Setti | 8388fdd | 2023-04-28 12:27:14 +0200 | [diff] [blame] | 506 | mbedtls_test_ssl_message_queue queue = SSL_MESSAGE_QUEUE_INIT; |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 507 | |
Valerio Setti | 44570a5 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 508 | USE_PSA_INIT(); |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 509 | TEST_ASSERT(mbedtls_test_ssl_message_queue_setup(&queue, 3) == 0); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 510 | |
| 511 | /* 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] | 512 | TEST_ASSERT(mbedtls_test_ssl_message_queue_push_info(&queue, 1) == 1); |
| 513 | TEST_ASSERT(mbedtls_test_ssl_message_queue_push_info(&queue, 1) == 1); |
| 514 | TEST_ASSERT(mbedtls_test_ssl_message_queue_push_info(&queue, 2) == 2); |
| 515 | TEST_ASSERT(mbedtls_test_ssl_message_queue_push_info(&queue, 3) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 516 | == MBEDTLS_ERR_SSL_WANT_WRITE); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 517 | |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 518 | TEST_ASSERT(mbedtls_test_ssl_message_queue_pop_info(&queue, 1) == 1); |
| 519 | TEST_ASSERT(mbedtls_test_ssl_message_queue_pop_info(&queue, 1) == 1); |
| 520 | TEST_ASSERT(mbedtls_test_ssl_message_queue_pop_info(&queue, 2) == 2); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 521 | |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 522 | TEST_ASSERT(mbedtls_test_ssl_message_queue_pop_info(&queue, 1) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 523 | == MBEDTLS_ERR_SSL_WANT_READ); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 524 | |
| 525 | exit: |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 526 | mbedtls_test_ssl_message_queue_free(&queue); |
Valerio Setti | 44570a5 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 527 | USE_PSA_DONE(); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 528 | } |
| 529 | /* END_CASE */ |
| 530 | |
| 531 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 532 | void ssl_message_queue_interleaved() |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 533 | { |
Valerio Setti | 8388fdd | 2023-04-28 12:27:14 +0200 | [diff] [blame] | 534 | mbedtls_test_ssl_message_queue queue = SSL_MESSAGE_QUEUE_INIT; |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 535 | |
Valerio Setti | 44570a5 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 536 | USE_PSA_INIT(); |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 537 | TEST_ASSERT(mbedtls_test_ssl_message_queue_setup(&queue, 3) == 0); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 538 | |
| 539 | /* Interleaved test - [2 pushes, 1 pop] twice, and then two pops |
| 540 | * (to wrap around the buffer) */ |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 541 | TEST_ASSERT(mbedtls_test_ssl_message_queue_push_info(&queue, 1) == 1); |
| 542 | TEST_ASSERT(mbedtls_test_ssl_message_queue_push_info(&queue, 1) == 1); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 543 | |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 544 | TEST_ASSERT(mbedtls_test_ssl_message_queue_pop_info(&queue, 1) == 1); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 545 | |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 546 | TEST_ASSERT(mbedtls_test_ssl_message_queue_push_info(&queue, 2) == 2); |
| 547 | TEST_ASSERT(mbedtls_test_ssl_message_queue_push_info(&queue, 3) == 3); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 548 | |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 549 | TEST_ASSERT(mbedtls_test_ssl_message_queue_pop_info(&queue, 1) == 1); |
| 550 | TEST_ASSERT(mbedtls_test_ssl_message_queue_pop_info(&queue, 2) == 2); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 551 | |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 552 | TEST_ASSERT(mbedtls_test_ssl_message_queue_push_info(&queue, 5) == 5); |
| 553 | TEST_ASSERT(mbedtls_test_ssl_message_queue_push_info(&queue, 8) == 8); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 554 | |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 555 | TEST_ASSERT(mbedtls_test_ssl_message_queue_pop_info(&queue, 3) == 3); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 556 | |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 557 | TEST_ASSERT(mbedtls_test_ssl_message_queue_pop_info(&queue, 5) == 5); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 558 | |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 559 | TEST_ASSERT(mbedtls_test_ssl_message_queue_pop_info(&queue, 8) == 8); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 560 | |
| 561 | exit: |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 562 | mbedtls_test_ssl_message_queue_free(&queue); |
Valerio Setti | 44570a5 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 563 | USE_PSA_DONE(); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 564 | } |
| 565 | /* END_CASE */ |
| 566 | |
| 567 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 568 | void ssl_message_queue_insufficient_buffer() |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 569 | { |
Valerio Setti | 8388fdd | 2023-04-28 12:27:14 +0200 | [diff] [blame] | 570 | mbedtls_test_ssl_message_queue queue = SSL_MESSAGE_QUEUE_INIT; |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 571 | size_t message_len = 10; |
| 572 | size_t buffer_len = 5; |
| 573 | |
Valerio Setti | 44570a5 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 574 | USE_PSA_INIT(); |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 575 | TEST_ASSERT(mbedtls_test_ssl_message_queue_setup(&queue, 1) == 0); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 576 | |
| 577 | /* Popping without a sufficient buffer */ |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 578 | TEST_ASSERT(mbedtls_test_ssl_message_queue_push_info(&queue, message_len) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 579 | == (int) message_len); |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 580 | TEST_ASSERT(mbedtls_test_ssl_message_queue_pop_info(&queue, buffer_len) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 581 | == (int) buffer_len); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 582 | exit: |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 583 | mbedtls_test_ssl_message_queue_free(&queue); |
Valerio Setti | 44570a5 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 584 | USE_PSA_DONE(); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 585 | } |
| 586 | /* END_CASE */ |
| 587 | |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 588 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 589 | void ssl_message_mock_uninitialized() |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 590 | { |
| 591 | enum { MSGLEN = 10 }; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 592 | unsigned char message[MSGLEN] = { 0 }, received[MSGLEN]; |
Yanray Wang | abfdcd8 | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 593 | mbedtls_test_mock_socket client, server; |
| 594 | mbedtls_test_ssl_message_queue server_queue, client_queue; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 595 | mbedtls_test_message_socket_context server_context, client_context; |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 596 | mbedtls_test_message_socket_init(&server_context); |
| 597 | mbedtls_test_message_socket_init(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 598 | |
Valerio Setti | 44570a5 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 599 | USE_PSA_INIT(); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 600 | /* Send with a NULL context */ |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 601 | TEST_ASSERT(mbedtls_test_mock_tcp_send_msg(NULL, message, MSGLEN) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 602 | == MBEDTLS_TEST_ERROR_CONTEXT_ERROR); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 603 | |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 604 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(NULL, message, MSGLEN) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 605 | == MBEDTLS_TEST_ERROR_CONTEXT_ERROR); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 606 | |
Yanray Wang | 862ef86 | 2023-03-14 11:05:04 +0800 | [diff] [blame] | 607 | TEST_ASSERT(mbedtls_test_message_socket_setup(&server_queue, |
| 608 | &client_queue, 1, |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 609 | &server, |
| 610 | &server_context) == 0); |
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 | TEST_ASSERT(mbedtls_test_message_socket_setup(&client_queue, |
| 613 | &server_queue, 1, |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 614 | &client, |
| 615 | &client_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 616 | |
Yanray Wang | 862ef86 | 2023-03-14 11:05:04 +0800 | [diff] [blame] | 617 | TEST_ASSERT(mbedtls_test_mock_tcp_send_msg(&client_context, message, |
| 618 | MSGLEN) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 619 | == MBEDTLS_TEST_ERROR_SEND_FAILED); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 620 | |
Yanray Wang | 862ef86 | 2023-03-14 11:05:04 +0800 | [diff] [blame] | 621 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&server_context, received, |
| 622 | MSGLEN) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 623 | == MBEDTLS_ERR_SSL_WANT_READ); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 624 | |
| 625 | /* Push directly to a queue to later simulate a disconnected behavior */ |
Yanray Wang | 862ef86 | 2023-03-14 11:05:04 +0800 | [diff] [blame] | 626 | TEST_ASSERT(mbedtls_test_ssl_message_queue_push_info(&server_queue, |
| 627 | MSGLEN) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 628 | == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 629 | |
| 630 | /* Test if there's an error when trying to read from a disconnected |
| 631 | * socket */ |
Yanray Wang | 862ef86 | 2023-03-14 11:05:04 +0800 | [diff] [blame] | 632 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&server_context, received, |
| 633 | MSGLEN) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 634 | == MBEDTLS_TEST_ERROR_RECV_FAILED); |
| 635 | exit: |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 636 | mbedtls_test_message_socket_close(&server_context); |
| 637 | mbedtls_test_message_socket_close(&client_context); |
Valerio Setti | 44570a5 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 638 | USE_PSA_DONE(); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 639 | } |
| 640 | /* END_CASE */ |
| 641 | |
| 642 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 643 | void ssl_message_mock_basic() |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 644 | { |
| 645 | enum { MSGLEN = 10 }; |
| 646 | unsigned char message[MSGLEN], received[MSGLEN]; |
Yanray Wang | abfdcd8 | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 647 | mbedtls_test_mock_socket client, server; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 648 | unsigned i; |
Yanray Wang | abfdcd8 | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 649 | mbedtls_test_ssl_message_queue server_queue, client_queue; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 650 | mbedtls_test_message_socket_context server_context, client_context; |
Valerio Setti | 44570a5 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 651 | |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 652 | mbedtls_test_message_socket_init(&server_context); |
| 653 | mbedtls_test_message_socket_init(&client_context); |
Valerio Setti | 89ae9b6 | 2023-04-27 17:22:54 +0200 | [diff] [blame] | 654 | USE_PSA_INIT(); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 655 | |
Yanray Wang | 862ef86 | 2023-03-14 11:05:04 +0800 | [diff] [blame] | 656 | TEST_ASSERT(mbedtls_test_message_socket_setup(&server_queue, |
| 657 | &client_queue, 1, |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 658 | &server, |
| 659 | &server_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 660 | |
Yanray Wang | 862ef86 | 2023-03-14 11:05:04 +0800 | [diff] [blame] | 661 | TEST_ASSERT(mbedtls_test_message_socket_setup(&client_queue, |
| 662 | &server_queue, 1, |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 663 | &client, |
| 664 | &client_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 665 | |
| 666 | /* Fill up the buffer with structured data so that unwanted changes |
| 667 | * can be detected */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 668 | for (i = 0; i < MSGLEN; i++) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 669 | message[i] = i & 0xFF; |
| 670 | } |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 671 | TEST_ASSERT(0 == mbedtls_test_mock_socket_connect(&client, &server, |
| 672 | MSGLEN)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 673 | |
| 674 | /* Send the message to the server */ |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 675 | TEST_ASSERT(mbedtls_test_mock_tcp_send_msg(&client_context, message, |
Yanray Wang | 862ef86 | 2023-03-14 11:05:04 +0800 | [diff] [blame] | 676 | MSGLEN) |
| 677 | == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 678 | |
| 679 | /* Read from the server */ |
Yanray Wang | 862ef86 | 2023-03-14 11:05:04 +0800 | [diff] [blame] | 680 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&server_context, received, |
| 681 | MSGLEN) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 682 | == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 683 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 684 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
| 685 | memset(received, 0, MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 686 | |
| 687 | /* Send the message to the client */ |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 688 | TEST_ASSERT(mbedtls_test_mock_tcp_send_msg(&server_context, message, |
| 689 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 690 | |
| 691 | /* Read from the client */ |
Yanray Wang | 862ef86 | 2023-03-14 11:05:04 +0800 | [diff] [blame] | 692 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&client_context, received, |
| 693 | MSGLEN) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 694 | == MSGLEN); |
| 695 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 696 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 697 | exit: |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 698 | mbedtls_test_message_socket_close(&server_context); |
| 699 | mbedtls_test_message_socket_close(&client_context); |
Valerio Setti | 44570a5 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 700 | USE_PSA_DONE(); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 701 | } |
| 702 | /* END_CASE */ |
| 703 | |
| 704 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 705 | void ssl_message_mock_queue_overflow_underflow() |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 706 | { |
| 707 | enum { MSGLEN = 10 }; |
| 708 | unsigned char message[MSGLEN], received[MSGLEN]; |
Yanray Wang | abfdcd8 | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 709 | mbedtls_test_mock_socket client, server; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 710 | unsigned i; |
Yanray Wang | abfdcd8 | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 711 | mbedtls_test_ssl_message_queue server_queue, client_queue; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 712 | mbedtls_test_message_socket_context server_context, client_context; |
Valerio Setti | 44570a5 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 713 | |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 714 | mbedtls_test_message_socket_init(&server_context); |
| 715 | mbedtls_test_message_socket_init(&client_context); |
Valerio Setti | 89ae9b6 | 2023-04-27 17:22:54 +0200 | [diff] [blame] | 716 | USE_PSA_INIT(); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 717 | |
Yanray Wang | 862ef86 | 2023-03-14 11:05:04 +0800 | [diff] [blame] | 718 | TEST_ASSERT(mbedtls_test_message_socket_setup(&server_queue, |
| 719 | &client_queue, 2, |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 720 | &server, |
| 721 | &server_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 722 | |
Yanray Wang | 862ef86 | 2023-03-14 11:05:04 +0800 | [diff] [blame] | 723 | TEST_ASSERT(mbedtls_test_message_socket_setup(&client_queue, |
| 724 | &server_queue, 2, |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 725 | &client, |
| 726 | &client_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 727 | |
| 728 | /* Fill up the buffer with structured data so that unwanted changes |
| 729 | * can be detected */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 730 | for (i = 0; i < MSGLEN; i++) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 731 | message[i] = i & 0xFF; |
| 732 | } |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 733 | TEST_ASSERT(0 == mbedtls_test_mock_socket_connect(&client, &server, |
| 734 | MSGLEN*2)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 735 | |
| 736 | /* Send three message to the server, last one with an error */ |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 737 | TEST_ASSERT(mbedtls_test_mock_tcp_send_msg(&client_context, message, |
Yanray Wang | 862ef86 | 2023-03-14 11:05:04 +0800 | [diff] [blame] | 738 | MSGLEN - 1) |
| 739 | == MSGLEN - 1); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 740 | |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 741 | TEST_ASSERT(mbedtls_test_mock_tcp_send_msg(&client_context, message, |
Yanray Wang | 862ef86 | 2023-03-14 11:05:04 +0800 | [diff] [blame] | 742 | MSGLEN) |
| 743 | == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 744 | |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 745 | TEST_ASSERT(mbedtls_test_mock_tcp_send_msg(&client_context, message, |
| 746 | MSGLEN) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 747 | == MBEDTLS_ERR_SSL_WANT_WRITE); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 748 | |
| 749 | /* Read three messages from the server, last one with an error */ |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 750 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&server_context, received, |
Yanray Wang | 862ef86 | 2023-03-14 11:05:04 +0800 | [diff] [blame] | 751 | MSGLEN - 1) |
| 752 | == MSGLEN - 1); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 753 | |
Yanray Wang | 862ef86 | 2023-03-14 11:05:04 +0800 | [diff] [blame] | 754 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&server_context, received, |
| 755 | MSGLEN) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 756 | == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 757 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 758 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 759 | |
Yanray Wang | 862ef86 | 2023-03-14 11:05:04 +0800 | [diff] [blame] | 760 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&server_context, received, |
| 761 | MSGLEN) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 762 | == MBEDTLS_ERR_SSL_WANT_READ); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 763 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 764 | exit: |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 765 | mbedtls_test_message_socket_close(&server_context); |
| 766 | mbedtls_test_message_socket_close(&client_context); |
Valerio Setti | 44570a5 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 767 | USE_PSA_DONE(); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 768 | } |
| 769 | /* END_CASE */ |
| 770 | |
| 771 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 772 | void ssl_message_mock_socket_overflow() |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 773 | { |
| 774 | enum { MSGLEN = 10 }; |
| 775 | unsigned char message[MSGLEN], received[MSGLEN]; |
Yanray Wang | abfdcd8 | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 776 | mbedtls_test_mock_socket client, server; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 777 | unsigned i; |
Yanray Wang | abfdcd8 | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 778 | mbedtls_test_ssl_message_queue server_queue, client_queue; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 779 | mbedtls_test_message_socket_context server_context, client_context; |
Valerio Setti | 44570a5 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 780 | |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 781 | mbedtls_test_message_socket_init(&server_context); |
| 782 | mbedtls_test_message_socket_init(&client_context); |
Valerio Setti | 89ae9b6 | 2023-04-27 17:22:54 +0200 | [diff] [blame] | 783 | USE_PSA_INIT(); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 784 | |
Yanray Wang | 862ef86 | 2023-03-14 11:05:04 +0800 | [diff] [blame] | 785 | TEST_ASSERT(mbedtls_test_message_socket_setup(&server_queue, |
| 786 | &client_queue, 2, |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 787 | &server, |
| 788 | &server_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 789 | |
Yanray Wang | 862ef86 | 2023-03-14 11:05:04 +0800 | [diff] [blame] | 790 | TEST_ASSERT(mbedtls_test_message_socket_setup(&client_queue, |
| 791 | &server_queue, 2, |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 792 | &client, |
| 793 | &client_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 794 | |
| 795 | /* Fill up the buffer with structured data so that unwanted changes |
| 796 | * can be detected */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 797 | for (i = 0; i < MSGLEN; i++) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 798 | message[i] = i & 0xFF; |
| 799 | } |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 800 | TEST_ASSERT(0 == mbedtls_test_mock_socket_connect(&client, &server, |
| 801 | MSGLEN)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 802 | |
| 803 | /* Send two message to the server, second one with an error */ |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 804 | TEST_ASSERT(mbedtls_test_mock_tcp_send_msg(&client_context, message, |
Yanray Wang | 862ef86 | 2023-03-14 11:05:04 +0800 | [diff] [blame] | 805 | MSGLEN) |
| 806 | == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 807 | |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 808 | TEST_ASSERT(mbedtls_test_mock_tcp_send_msg(&client_context, message, |
| 809 | MSGLEN) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 810 | == MBEDTLS_TEST_ERROR_SEND_FAILED); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 811 | |
| 812 | /* Read the only message from the server */ |
Yanray Wang | 862ef86 | 2023-03-14 11:05:04 +0800 | [diff] [blame] | 813 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&server_context, received, |
| 814 | MSGLEN) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 815 | == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 816 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 817 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 818 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 819 | exit: |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 820 | mbedtls_test_message_socket_close(&server_context); |
| 821 | mbedtls_test_message_socket_close(&client_context); |
Valerio Setti | 44570a5 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 822 | USE_PSA_DONE(); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 823 | } |
| 824 | /* END_CASE */ |
| 825 | |
| 826 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 827 | void ssl_message_mock_truncated() |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 828 | { |
| 829 | enum { MSGLEN = 10 }; |
| 830 | unsigned char message[MSGLEN], received[MSGLEN]; |
Yanray Wang | abfdcd8 | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 831 | mbedtls_test_mock_socket client, server; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 832 | unsigned i; |
Yanray Wang | abfdcd8 | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 833 | mbedtls_test_ssl_message_queue server_queue, client_queue; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 834 | mbedtls_test_message_socket_context server_context, client_context; |
Valerio Setti | 44570a5 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 835 | |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 836 | mbedtls_test_message_socket_init(&server_context); |
| 837 | mbedtls_test_message_socket_init(&client_context); |
Valerio Setti | 89ae9b6 | 2023-04-27 17:22:54 +0200 | [diff] [blame] | 838 | USE_PSA_INIT(); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 839 | |
Yanray Wang | 862ef86 | 2023-03-14 11:05:04 +0800 | [diff] [blame] | 840 | TEST_ASSERT(mbedtls_test_message_socket_setup(&server_queue, |
| 841 | &client_queue, 2, |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 842 | &server, |
| 843 | &server_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 844 | |
Yanray Wang | 862ef86 | 2023-03-14 11:05:04 +0800 | [diff] [blame] | 845 | TEST_ASSERT(mbedtls_test_message_socket_setup(&client_queue, |
| 846 | &server_queue, 2, |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 847 | &client, |
| 848 | &client_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 849 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 850 | memset(received, 0, MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 851 | /* Fill up the buffer with structured data so that unwanted changes |
| 852 | * can be detected */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 853 | for (i = 0; i < MSGLEN; i++) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 854 | message[i] = i & 0xFF; |
| 855 | } |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 856 | TEST_ASSERT(0 == mbedtls_test_mock_socket_connect(&client, &server, |
| 857 | 2 * MSGLEN)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 858 | |
| 859 | /* Send two messages to the server, the second one small enough to fit in the |
| 860 | * receiver's buffer. */ |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 861 | TEST_ASSERT(mbedtls_test_mock_tcp_send_msg(&client_context, message, |
Yanray Wang | 862ef86 | 2023-03-14 11:05:04 +0800 | [diff] [blame] | 862 | MSGLEN) |
| 863 | == MSGLEN); |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 864 | TEST_ASSERT(mbedtls_test_mock_tcp_send_msg(&client_context, message, |
Yanray Wang | 862ef86 | 2023-03-14 11:05:04 +0800 | [diff] [blame] | 865 | MSGLEN / 2) |
| 866 | == MSGLEN / 2); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 867 | /* Read a truncated message from the server */ |
Yanray Wang | 862ef86 | 2023-03-14 11:05:04 +0800 | [diff] [blame] | 868 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&server_context, received, |
| 869 | MSGLEN/2) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 870 | == MSGLEN/2); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 871 | |
| 872 | /* 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] | 873 | TEST_ASSERT(memcmp(message, received, MSGLEN/2) == 0); |
| 874 | TEST_ASSERT(memcmp(message + MSGLEN/2, received + MSGLEN/2, MSGLEN/2) |
| 875 | != 0); |
| 876 | memset(received, 0, MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 877 | |
| 878 | /* Read a full message from the server */ |
Yanray Wang | 862ef86 | 2023-03-14 11:05:04 +0800 | [diff] [blame] | 879 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&server_context, received, |
| 880 | MSGLEN/2) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 881 | == MSGLEN / 2); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 882 | |
| 883 | /* Test that the first half of the message is valid */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 884 | TEST_ASSERT(memcmp(message, received, MSGLEN/2) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 885 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 886 | exit: |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 887 | mbedtls_test_message_socket_close(&server_context); |
| 888 | mbedtls_test_message_socket_close(&client_context); |
Valerio Setti | 44570a5 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 889 | USE_PSA_DONE(); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 890 | } |
| 891 | /* END_CASE */ |
| 892 | |
| 893 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 894 | void ssl_message_mock_socket_read_error() |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 895 | { |
| 896 | enum { MSGLEN = 10 }; |
| 897 | unsigned char message[MSGLEN], received[MSGLEN]; |
Yanray Wang | abfdcd8 | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 898 | mbedtls_test_mock_socket client, server; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 899 | unsigned i; |
Yanray Wang | abfdcd8 | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 900 | mbedtls_test_ssl_message_queue server_queue, client_queue; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 901 | mbedtls_test_message_socket_context server_context, client_context; |
Valerio Setti | 44570a5 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 902 | |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 903 | mbedtls_test_message_socket_init(&server_context); |
| 904 | mbedtls_test_message_socket_init(&client_context); |
Valerio Setti | 89ae9b6 | 2023-04-27 17:22:54 +0200 | [diff] [blame] | 905 | USE_PSA_INIT(); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 906 | |
Yanray Wang | 862ef86 | 2023-03-14 11:05:04 +0800 | [diff] [blame] | 907 | TEST_ASSERT(mbedtls_test_message_socket_setup(&server_queue, |
| 908 | &client_queue, 1, |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 909 | &server, |
| 910 | &server_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 911 | |
Yanray Wang | 862ef86 | 2023-03-14 11:05:04 +0800 | [diff] [blame] | 912 | TEST_ASSERT(mbedtls_test_message_socket_setup(&client_queue, |
| 913 | &server_queue, 1, |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 914 | &client, |
| 915 | &client_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 916 | |
| 917 | /* Fill up the buffer with structured data so that unwanted changes |
| 918 | * can be detected */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 919 | for (i = 0; i < MSGLEN; i++) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 920 | message[i] = i & 0xFF; |
| 921 | } |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 922 | TEST_ASSERT(0 == mbedtls_test_mock_socket_connect(&client, &server, |
| 923 | MSGLEN)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 924 | |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 925 | TEST_ASSERT(mbedtls_test_mock_tcp_send_msg(&client_context, message, |
Yanray Wang | 862ef86 | 2023-03-14 11:05:04 +0800 | [diff] [blame] | 926 | MSGLEN) |
| 927 | == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 928 | |
| 929 | /* Force a read error by disconnecting the socket by hand */ |
| 930 | server.status = 0; |
Yanray Wang | 862ef86 | 2023-03-14 11:05:04 +0800 | [diff] [blame] | 931 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&server_context, received, |
| 932 | MSGLEN) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 933 | == MBEDTLS_TEST_ERROR_RECV_FAILED); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 934 | /* Return to a valid state */ |
| 935 | server.status = MBEDTLS_MOCK_SOCKET_CONNECTED; |
| 936 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 937 | memset(received, 0, sizeof(received)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 938 | |
| 939 | /* Test that even though the server tried to read once disconnected, the |
| 940 | * continuity is preserved */ |
Yanray Wang | 862ef86 | 2023-03-14 11:05:04 +0800 | [diff] [blame] | 941 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&server_context, received, |
| 942 | MSGLEN) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 943 | == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 944 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 945 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 946 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 947 | exit: |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 948 | mbedtls_test_message_socket_close(&server_context); |
| 949 | mbedtls_test_message_socket_close(&client_context); |
Valerio Setti | 44570a5 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 950 | USE_PSA_DONE(); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 951 | } |
| 952 | /* END_CASE */ |
| 953 | |
| 954 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 955 | void ssl_message_mock_interleaved_one_way() |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 956 | { |
| 957 | enum { MSGLEN = 10 }; |
| 958 | unsigned char message[MSGLEN], received[MSGLEN]; |
Yanray Wang | abfdcd8 | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 959 | mbedtls_test_mock_socket client, server; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 960 | unsigned i; |
Yanray Wang | abfdcd8 | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 961 | mbedtls_test_ssl_message_queue server_queue, client_queue; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 962 | mbedtls_test_message_socket_context server_context, client_context; |
Valerio Setti | 44570a5 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 963 | |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 964 | mbedtls_test_message_socket_init(&server_context); |
| 965 | mbedtls_test_message_socket_init(&client_context); |
Valerio Setti | 89ae9b6 | 2023-04-27 17:22:54 +0200 | [diff] [blame] | 966 | USE_PSA_INIT(); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 967 | |
Yanray Wang | 862ef86 | 2023-03-14 11:05:04 +0800 | [diff] [blame] | 968 | TEST_ASSERT(mbedtls_test_message_socket_setup(&server_queue, |
| 969 | &client_queue, 3, |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 970 | &server, |
| 971 | &server_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 972 | |
Yanray Wang | 862ef86 | 2023-03-14 11:05:04 +0800 | [diff] [blame] | 973 | TEST_ASSERT(mbedtls_test_message_socket_setup(&client_queue, |
| 974 | &server_queue, 3, |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 975 | &client, |
| 976 | &client_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 977 | |
| 978 | /* Fill up the buffer with structured data so that unwanted changes |
| 979 | * can be detected */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 980 | for (i = 0; i < MSGLEN; i++) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 981 | message[i] = i & 0xFF; |
| 982 | } |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 983 | TEST_ASSERT(0 == mbedtls_test_mock_socket_connect(&client, &server, |
| 984 | MSGLEN*3)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 985 | |
| 986 | /* Interleaved test - [2 sends, 1 read] twice, and then two reads |
| 987 | * (to wrap around the buffer) */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 988 | for (i = 0; i < 2; i++) { |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 989 | TEST_ASSERT(mbedtls_test_mock_tcp_send_msg(&client_context, message, |
| 990 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 991 | |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 992 | TEST_ASSERT(mbedtls_test_mock_tcp_send_msg(&client_context, message, |
| 993 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 994 | |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 995 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&server_context, received, |
| 996 | MSGLEN) == MSGLEN); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 997 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
| 998 | memset(received, 0, sizeof(received)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 999 | } |
| 1000 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1001 | for (i = 0; i < 2; i++) { |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 1002 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&server_context, received, |
| 1003 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 1004 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1005 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 1006 | } |
Yanray Wang | 862ef86 | 2023-03-14 11:05:04 +0800 | [diff] [blame] | 1007 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&server_context, received, |
| 1008 | MSGLEN) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1009 | == MBEDTLS_ERR_SSL_WANT_READ); |
| 1010 | exit: |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 1011 | mbedtls_test_message_socket_close(&server_context); |
| 1012 | mbedtls_test_message_socket_close(&client_context); |
Valerio Setti | 44570a5 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 1013 | USE_PSA_DONE(); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 1014 | } |
| 1015 | /* END_CASE */ |
| 1016 | |
| 1017 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1018 | void ssl_message_mock_interleaved_two_ways() |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 1019 | { |
| 1020 | enum { MSGLEN = 10 }; |
| 1021 | unsigned char message[MSGLEN], received[MSGLEN]; |
Yanray Wang | abfdcd8 | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 1022 | mbedtls_test_mock_socket client, server; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 1023 | unsigned i; |
Yanray Wang | abfdcd8 | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 1024 | mbedtls_test_ssl_message_queue server_queue, client_queue; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 1025 | mbedtls_test_message_socket_context server_context, client_context; |
Valerio Setti | 44570a5 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 1026 | |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 1027 | mbedtls_test_message_socket_init(&server_context); |
| 1028 | mbedtls_test_message_socket_init(&client_context); |
Valerio Setti | 89ae9b6 | 2023-04-27 17:22:54 +0200 | [diff] [blame] | 1029 | USE_PSA_INIT(); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 1030 | |
Yanray Wang | 862ef86 | 2023-03-14 11:05:04 +0800 | [diff] [blame] | 1031 | TEST_ASSERT(mbedtls_test_message_socket_setup(&server_queue, |
| 1032 | &client_queue, 3, |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 1033 | &server, |
| 1034 | &server_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 1035 | |
Yanray Wang | 862ef86 | 2023-03-14 11:05:04 +0800 | [diff] [blame] | 1036 | TEST_ASSERT(mbedtls_test_message_socket_setup(&client_queue, |
| 1037 | &server_queue, 3, |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 1038 | &client, |
| 1039 | &client_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 1040 | |
| 1041 | /* Fill up the buffer with structured data so that unwanted changes |
| 1042 | * can be detected */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1043 | for (i = 0; i < MSGLEN; i++) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 1044 | message[i] = i & 0xFF; |
| 1045 | } |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 1046 | TEST_ASSERT(0 == mbedtls_test_mock_socket_connect(&client, &server, |
| 1047 | MSGLEN*3)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 1048 | |
| 1049 | /* Interleaved test - [2 sends, 1 read] twice, both ways, and then two reads |
| 1050 | * (to wrap around the buffer) both ways. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1051 | for (i = 0; i < 2; i++) { |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 1052 | TEST_ASSERT(mbedtls_test_mock_tcp_send_msg(&client_context, message, |
| 1053 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 1054 | |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 1055 | TEST_ASSERT(mbedtls_test_mock_tcp_send_msg(&client_context, message, |
| 1056 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 1057 | |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 1058 | TEST_ASSERT(mbedtls_test_mock_tcp_send_msg(&server_context, message, |
| 1059 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 1060 | |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 1061 | TEST_ASSERT(mbedtls_test_mock_tcp_send_msg(&server_context, message, |
| 1062 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 1063 | |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 1064 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&server_context, received, |
| 1065 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 1066 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1067 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 1068 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1069 | memset(received, 0, sizeof(received)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 1070 | |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 1071 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&client_context, received, |
| 1072 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 1073 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1074 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 1075 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1076 | memset(received, 0, sizeof(received)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 1077 | } |
| 1078 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1079 | for (i = 0; i < 2; i++) { |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 1080 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&server_context, received, |
| 1081 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 1082 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1083 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
| 1084 | memset(received, 0, sizeof(received)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 1085 | |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 1086 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&client_context, received, |
| 1087 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 1088 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1089 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
| 1090 | memset(received, 0, sizeof(received)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 1091 | } |
| 1092 | |
Yanray Wang | 862ef86 | 2023-03-14 11:05:04 +0800 | [diff] [blame] | 1093 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&server_context, received, |
| 1094 | MSGLEN) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1095 | == MBEDTLS_ERR_SSL_WANT_READ); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 1096 | |
Yanray Wang | 862ef86 | 2023-03-14 11:05:04 +0800 | [diff] [blame] | 1097 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&client_context, received, |
| 1098 | MSGLEN) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1099 | == MBEDTLS_ERR_SSL_WANT_READ); |
| 1100 | exit: |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 1101 | mbedtls_test_message_socket_close(&server_context); |
| 1102 | mbedtls_test_message_socket_close(&client_context); |
Valerio Setti | 44570a5 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 1103 | USE_PSA_DONE(); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 1104 | } |
| 1105 | /* END_CASE */ |
| 1106 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1107 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1108 | 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] | 1109 | { |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 1110 | uint32_t len = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1111 | mbedtls_ssl_context ssl; |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 1112 | mbedtls_ssl_config conf; |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 1113 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1114 | mbedtls_ssl_init(&ssl); |
| 1115 | mbedtls_ssl_config_init(&conf); |
Valerio Setti | 44570a5 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 1116 | USE_PSA_INIT(); |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 1117 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1118 | TEST_ASSERT(mbedtls_ssl_config_defaults(&conf, |
| 1119 | MBEDTLS_SSL_IS_CLIENT, |
| 1120 | MBEDTLS_SSL_TRANSPORT_DATAGRAM, |
| 1121 | MBEDTLS_SSL_PRESET_DEFAULT) == 0); |
| 1122 | TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0); |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 1123 | |
| 1124 | /* Read previous record numbers */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1125 | for (len = 0; len < prevs->len; len += 6) { |
| 1126 | memcpy(ssl.in_ctr + 2, prevs->x + len, 6); |
| 1127 | mbedtls_ssl_dtls_replay_update(&ssl); |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 1128 | } |
| 1129 | |
| 1130 | /* Check new number */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1131 | memcpy(ssl.in_ctr + 2, new->x, 6); |
| 1132 | TEST_ASSERT(mbedtls_ssl_dtls_replay_check(&ssl) == ret); |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 1133 | |
Valerio Setti | 44570a5 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 1134 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1135 | mbedtls_ssl_free(&ssl); |
| 1136 | mbedtls_ssl_config_free(&conf); |
Valerio Setti | 44570a5 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 1137 | USE_PSA_DONE(); |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 1138 | } |
| 1139 | /* END_CASE */ |
Hanno Becker | b25c0c7 | 2017-05-05 11:24:30 +0100 | [diff] [blame] | 1140 | |
Gilles Peskine | 33d03fe | 2023-02-01 18:37:49 +0100 | [diff] [blame] | 1141 | /* 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] | 1142 | void ssl_set_hostname_twice(char *hostname0, char *hostname1) |
Hanno Becker | b25c0c7 | 2017-05-05 11:24:30 +0100 | [diff] [blame] | 1143 | { |
| 1144 | mbedtls_ssl_context ssl; |
Valerio Setti | 44570a5 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 1145 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1146 | mbedtls_ssl_init(&ssl); |
Valerio Setti | 89ae9b6 | 2023-04-27 17:22:54 +0200 | [diff] [blame] | 1147 | USE_PSA_INIT(); |
Hanno Becker | b25c0c7 | 2017-05-05 11:24:30 +0100 | [diff] [blame] | 1148 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1149 | TEST_ASSERT(mbedtls_ssl_set_hostname(&ssl, hostname0) == 0); |
| 1150 | TEST_ASSERT(mbedtls_ssl_set_hostname(&ssl, hostname1) == 0); |
Hanno Becker | b25c0c7 | 2017-05-05 11:24:30 +0100 | [diff] [blame] | 1151 | |
Valerio Setti | 44570a5 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 1152 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1153 | mbedtls_ssl_free(&ssl); |
Valerio Setti | 44570a5 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 1154 | USE_PSA_DONE(); |
Hanno Becker | b25c0c7 | 2017-05-05 11:24:30 +0100 | [diff] [blame] | 1155 | } |
Darryl Green | 11999bb | 2018-03-13 15:22:58 +0000 | [diff] [blame] | 1156 | /* END_CASE */ |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1157 | |
| 1158 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1159 | void ssl_crypt_record(int cipher_type, int hash_id, |
| 1160 | int etm, int tag_mode, int ver, |
| 1161 | int cid0_len, int cid1_len) |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1162 | { |
| 1163 | /* |
| 1164 | * Test several record encryptions and decryptions |
| 1165 | * with plenty of space before and after the data |
| 1166 | * within the record buffer. |
| 1167 | */ |
| 1168 | |
| 1169 | int ret; |
| 1170 | int num_records = 16; |
| 1171 | mbedtls_ssl_context ssl; /* ONLY for debugging */ |
| 1172 | |
| 1173 | mbedtls_ssl_transform t0, t1; |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 1174 | unsigned char *buf = NULL; |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1175 | size_t const buflen = 512; |
| 1176 | mbedtls_record rec, rec_backup; |
| 1177 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1178 | mbedtls_ssl_init(&ssl); |
Valerio Setti | 44570a5 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 1179 | USE_PSA_INIT(); |
| 1180 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1181 | mbedtls_ssl_transform_init(&t0); |
| 1182 | mbedtls_ssl_transform_init(&t1); |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 1183 | TEST_ASSERT(mbedtls_test_ssl_build_transforms(&t0, &t1, cipher_type, hash_id, |
| 1184 | etm, tag_mode, ver, |
| 1185 | (size_t) cid0_len, |
| 1186 | (size_t) cid1_len) == 0); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1187 | |
Gilles Peskine | bf85200 | 2023-10-17 17:31:50 +0200 | [diff] [blame] | 1188 | TEST_CALLOC(buf, buflen); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1189 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1190 | while (num_records-- > 0) { |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1191 | mbedtls_ssl_transform *t_dec, *t_enc; |
| 1192 | /* Take turns in who's sending and who's receiving. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1193 | if (num_records % 3 == 0) { |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1194 | t_dec = &t0; |
| 1195 | t_enc = &t1; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1196 | } else { |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1197 | t_dec = &t1; |
| 1198 | t_enc = &t0; |
| 1199 | } |
| 1200 | |
| 1201 | /* |
| 1202 | * The record header affects the transformation in two ways: |
| 1203 | * 1) It determines the AEAD additional data |
| 1204 | * 2) The record counter sometimes determines the IV. |
| 1205 | * |
| 1206 | * Apart from that, the fields don't have influence. |
| 1207 | * In particular, it is currently not the responsibility |
| 1208 | * of ssl_encrypt/decrypt_buf to check if the transform |
| 1209 | * version matches the record version, or that the |
| 1210 | * type is sensible. |
| 1211 | */ |
| 1212 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1213 | memset(rec.ctr, num_records, sizeof(rec.ctr)); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1214 | rec.type = 42; |
| 1215 | rec.ver[0] = num_records; |
| 1216 | rec.ver[1] = num_records; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1217 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 1218 | rec.cid_len = 0; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1219 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1220 | |
| 1221 | rec.buf = buf; |
| 1222 | rec.buf_len = buflen; |
| 1223 | rec.data_offset = 16; |
| 1224 | /* Make sure to vary the length to exercise different |
| 1225 | * paddings. */ |
| 1226 | rec.data_len = 1 + num_records; |
| 1227 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1228 | memset(rec.buf + rec.data_offset, 42, rec.data_len); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1229 | |
| 1230 | /* Make a copy for later comparison */ |
| 1231 | rec_backup = rec; |
| 1232 | |
| 1233 | /* Encrypt record */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1234 | ret = mbedtls_ssl_encrypt_buf(&ssl, t_enc, &rec, |
| 1235 | mbedtls_test_rnd_std_rand, NULL); |
| 1236 | TEST_ASSERT(ret == 0 || ret == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL); |
| 1237 | if (ret != 0) { |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1238 | continue; |
| 1239 | } |
| 1240 | |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 1241 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1242 | if (rec.cid_len != 0) { |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 1243 | /* DTLS 1.2 + CID hides the real content type and |
| 1244 | * uses a special CID content type in the protected |
| 1245 | * record. Double-check this. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1246 | TEST_ASSERT(rec.type == MBEDTLS_SSL_MSG_CID); |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 1247 | } |
| 1248 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 1249 | |
| 1250 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1251 | if (t_enc->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4) { |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 1252 | /* TLS 1.3 hides the real content type and |
| 1253 | * always uses Application Data as the content type |
| 1254 | * for protected records. Double-check this. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1255 | TEST_ASSERT(rec.type == MBEDTLS_SSL_MSG_APPLICATION_DATA); |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 1256 | } |
| 1257 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ |
| 1258 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1259 | /* Decrypt record with t_dec */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1260 | ret = mbedtls_ssl_decrypt_buf(&ssl, t_dec, &rec); |
| 1261 | TEST_ASSERT(ret == 0); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1262 | |
| 1263 | /* Compare results */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1264 | TEST_ASSERT(rec.type == rec_backup.type); |
| 1265 | TEST_ASSERT(memcmp(rec.ctr, rec_backup.ctr, 8) == 0); |
| 1266 | TEST_ASSERT(rec.ver[0] == rec_backup.ver[0]); |
| 1267 | TEST_ASSERT(rec.ver[1] == rec_backup.ver[1]); |
| 1268 | TEST_ASSERT(rec.data_len == rec_backup.data_len); |
| 1269 | TEST_ASSERT(rec.data_offset == rec_backup.data_offset); |
| 1270 | TEST_ASSERT(memcmp(rec.buf + rec.data_offset, |
| 1271 | rec_backup.buf + rec_backup.data_offset, |
| 1272 | rec.data_len) == 0); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1273 | } |
| 1274 | |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 1275 | exit: |
| 1276 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1277 | /* Cleanup */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1278 | mbedtls_ssl_free(&ssl); |
| 1279 | mbedtls_ssl_transform_free(&t0); |
| 1280 | mbedtls_ssl_transform_free(&t1); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1281 | mbedtls_free(buf); |
Valerio Setti | 44570a5 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 1282 | USE_PSA_DONE(); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1283 | } |
| 1284 | /* END_CASE */ |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 1285 | |
| 1286 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1287 | void ssl_crypt_record_small(int cipher_type, int hash_id, |
| 1288 | int etm, int tag_mode, int ver, |
| 1289 | int cid0_len, int cid1_len) |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 1290 | { |
| 1291 | /* |
| 1292 | * Test pairs of encryption and decryption with an increasing |
| 1293 | * amount of space in the record buffer - in more detail: |
| 1294 | * 1) Try to encrypt with 0, 1, 2, ... bytes available |
| 1295 | * in front of the plaintext, and expect the encryption |
| 1296 | * to succeed starting from some offset. Always keep |
| 1297 | * enough space in the end of the buffer. |
| 1298 | * 2) Try to encrypt with 0, 1, 2, ... bytes available |
| 1299 | * at the end of the plaintext, and expect the encryption |
| 1300 | * to succeed starting from some offset. Always keep |
| 1301 | * enough space at the beginning of the buffer. |
| 1302 | * 3) Try to encrypt with 0, 1, 2, ... bytes available |
| 1303 | * both at the front and end of the plaintext, |
| 1304 | * and expect the encryption to succeed starting from |
| 1305 | * some offset. |
| 1306 | * |
| 1307 | * If encryption succeeds, check that decryption succeeds |
| 1308 | * and yields the original record. |
| 1309 | */ |
| 1310 | |
| 1311 | mbedtls_ssl_context ssl; /* ONLY for debugging */ |
| 1312 | |
| 1313 | mbedtls_ssl_transform t0, t1; |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 1314 | unsigned char *buf = NULL; |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 1315 | size_t const buflen = 256; |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 1316 | mbedtls_record rec, rec_backup; |
| 1317 | |
| 1318 | int ret; |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 1319 | int mode; /* Mode 1, 2 or 3 as explained above */ |
| 1320 | size_t offset; /* Available space at beginning/end/both */ |
| 1321 | size_t threshold = 96; /* Maximum offset to test against */ |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 1322 | |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 1323 | size_t default_pre_padding = 64; /* Pre-padding to use in mode 2 */ |
| 1324 | 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] | 1325 | |
| 1326 | int seen_success; /* Indicates if in the current mode we've |
| 1327 | * already seen a successful test. */ |
| 1328 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1329 | mbedtls_ssl_init(&ssl); |
| 1330 | mbedtls_ssl_transform_init(&t0); |
| 1331 | mbedtls_ssl_transform_init(&t1); |
Valerio Setti | 89ae9b6 | 2023-04-27 17:22:54 +0200 | [diff] [blame] | 1332 | USE_PSA_INIT(); |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 1333 | TEST_ASSERT(mbedtls_test_ssl_build_transforms(&t0, &t1, cipher_type, hash_id, |
| 1334 | etm, tag_mode, ver, |
| 1335 | (size_t) cid0_len, |
| 1336 | (size_t) cid1_len) == 0); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 1337 | |
Gilles Peskine | bf85200 | 2023-10-17 17:31:50 +0200 | [diff] [blame] | 1338 | TEST_CALLOC(buf, buflen); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 1339 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1340 | for (mode = 1; mode <= 3; mode++) { |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 1341 | seen_success = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1342 | for (offset = 0; offset <= threshold; offset++) { |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 1343 | mbedtls_ssl_transform *t_dec, *t_enc; |
Hanno Becker | 6c87b3f | 2019-04-29 17:24:44 +0100 | [diff] [blame] | 1344 | t_dec = &t0; |
| 1345 | t_enc = &t1; |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 1346 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1347 | memset(rec.ctr, offset, sizeof(rec.ctr)); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 1348 | rec.type = 42; |
| 1349 | rec.ver[0] = offset; |
| 1350 | rec.ver[1] = offset; |
| 1351 | rec.buf = buf; |
| 1352 | rec.buf_len = buflen; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1353 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 1354 | rec.cid_len = 0; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1355 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 1356 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1357 | switch (mode) { |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 1358 | case 1: /* Space in the beginning */ |
| 1359 | rec.data_offset = offset; |
| 1360 | rec.data_len = buflen - offset - default_post_padding; |
| 1361 | break; |
| 1362 | |
| 1363 | case 2: /* Space in the end */ |
| 1364 | rec.data_offset = default_pre_padding; |
| 1365 | rec.data_len = buflen - default_pre_padding - offset; |
| 1366 | break; |
| 1367 | |
| 1368 | case 3: /* Space in the beginning and end */ |
| 1369 | rec.data_offset = offset; |
| 1370 | rec.data_len = buflen - 2 * offset; |
| 1371 | break; |
| 1372 | |
| 1373 | default: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1374 | TEST_ASSERT(0); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 1375 | break; |
| 1376 | } |
| 1377 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1378 | memset(rec.buf + rec.data_offset, 42, rec.data_len); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 1379 | |
| 1380 | /* Make a copy for later comparison */ |
| 1381 | rec_backup = rec; |
| 1382 | |
| 1383 | /* Encrypt record */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1384 | ret = mbedtls_ssl_encrypt_buf(&ssl, t_enc, &rec, |
| 1385 | mbedtls_test_rnd_std_rand, NULL); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 1386 | |
Gilles Peskine | 56081de | 2023-07-20 22:18:23 +0200 | [diff] [blame] | 1387 | if (ret == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) { |
| 1388 | /* It's ok if the output buffer is too small. We do insist |
| 1389 | * on at least one mode succeeding; this is tracked by |
| 1390 | * seen_success. */ |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 1391 | continue; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1392 | } |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 1393 | |
Gilles Peskine | 56081de | 2023-07-20 22:18:23 +0200 | [diff] [blame] | 1394 | TEST_EQUAL(ret, 0); |
| 1395 | seen_success = 1; |
| 1396 | |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 1397 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1398 | if (rec.cid_len != 0) { |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 1399 | /* DTLS 1.2 + CID hides the real content type and |
| 1400 | * uses a special CID content type in the protected |
| 1401 | * record. Double-check this. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1402 | TEST_ASSERT(rec.type == MBEDTLS_SSL_MSG_CID); |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 1403 | } |
| 1404 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 1405 | |
| 1406 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1407 | if (t_enc->minor_ver == MBEDTLS_SSL_MINOR_VERSION_4) { |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 1408 | /* TLS 1.3 hides the real content type and |
| 1409 | * always uses Application Data as the content type |
| 1410 | * for protected records. Double-check this. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1411 | TEST_ASSERT(rec.type == MBEDTLS_SSL_MSG_APPLICATION_DATA); |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 1412 | } |
| 1413 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ |
| 1414 | |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 1415 | /* Decrypt record with t_dec */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1416 | TEST_ASSERT(mbedtls_ssl_decrypt_buf(&ssl, t_dec, &rec) == 0); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 1417 | |
| 1418 | /* Compare results */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1419 | TEST_ASSERT(rec.type == rec_backup.type); |
| 1420 | TEST_ASSERT(memcmp(rec.ctr, rec_backup.ctr, 8) == 0); |
| 1421 | TEST_ASSERT(rec.ver[0] == rec_backup.ver[0]); |
| 1422 | TEST_ASSERT(rec.ver[1] == rec_backup.ver[1]); |
| 1423 | TEST_ASSERT(rec.data_len == rec_backup.data_len); |
| 1424 | TEST_ASSERT(rec.data_offset == rec_backup.data_offset); |
| 1425 | TEST_ASSERT(memcmp(rec.buf + rec.data_offset, |
| 1426 | rec_backup.buf + rec_backup.data_offset, |
| 1427 | rec.data_len) == 0); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 1428 | } |
| 1429 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1430 | TEST_ASSERT(seen_success == 1); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 1431 | } |
| 1432 | |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 1433 | exit: |
| 1434 | |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 1435 | /* Cleanup */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1436 | mbedtls_ssl_free(&ssl); |
| 1437 | mbedtls_ssl_transform_free(&t0); |
| 1438 | mbedtls_ssl_transform_free(&t1); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1439 | mbedtls_free(buf); |
Valerio Setti | 44570a5 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 1440 | USE_PSA_DONE(); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 1441 | } |
| 1442 | /* END_CASE */ |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 1443 | |
Hanno Becker | 39ff492 | 2020-08-21 13:36:56 +0100 | [diff] [blame] | 1444 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1445 | void ssl_tls1_3_hkdf_expand_label(int hash_alg, |
| 1446 | data_t *secret, |
| 1447 | int label_idx, |
| 1448 | data_t *ctx, |
| 1449 | int desired_length, |
| 1450 | data_t *expected) |
Hanno Becker | 39ff492 | 2020-08-21 13:36:56 +0100 | [diff] [blame] | 1451 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1452 | unsigned char dst[100]; |
Hanno Becker | 39ff492 | 2020-08-21 13:36:56 +0100 | [diff] [blame] | 1453 | |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 1454 | unsigned char const *lbl = NULL; |
| 1455 | size_t lbl_len; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1456 | #define MBEDTLS_SSL_TLS1_3_LABEL(name, string) \ |
| 1457 | if (label_idx == (int) tls1_3_label_ ## name) \ |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 1458 | { \ |
| 1459 | lbl = mbedtls_ssl_tls1_3_labels.name; \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1460 | lbl_len = sizeof(mbedtls_ssl_tls1_3_labels.name); \ |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 1461 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1462 | MBEDTLS_SSL_TLS1_3_LABEL_LIST |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 1463 | #undef MBEDTLS_SSL_TLS1_3_LABEL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1464 | TEST_ASSERT(lbl != NULL); |
Hanno Becker | 39ff492 | 2020-08-21 13:36:56 +0100 | [diff] [blame] | 1465 | |
| 1466 | /* Check sanity of test parameters. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1467 | TEST_ASSERT((size_t) desired_length <= sizeof(dst)); |
| 1468 | TEST_ASSERT((size_t) desired_length == expected->len); |
Hanno Becker | 39ff492 | 2020-08-21 13:36:56 +0100 | [diff] [blame] | 1469 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1470 | TEST_ASSERT(mbedtls_ssl_tls1_3_hkdf_expand_label( |
| 1471 | (mbedtls_md_type_t) hash_alg, |
| 1472 | secret->x, secret->len, |
| 1473 | lbl, lbl_len, |
| 1474 | ctx->x, ctx->len, |
| 1475 | dst, desired_length) == 0); |
Hanno Becker | 39ff492 | 2020-08-21 13:36:56 +0100 | [diff] [blame] | 1476 | |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 1477 | TEST_MEMORY_COMPARE(dst, (size_t) desired_length, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 1478 | expected->x, (size_t) expected->len); |
Hanno Becker | 39ff492 | 2020-08-21 13:36:56 +0100 | [diff] [blame] | 1479 | } |
| 1480 | /* END_CASE */ |
| 1481 | |
Hanno Becker | 19498f8 | 2020-08-21 13:37:08 +0100 | [diff] [blame] | 1482 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1483 | void ssl_tls1_3_traffic_key_generation(int hash_alg, |
| 1484 | data_t *server_secret, |
| 1485 | data_t *client_secret, |
| 1486 | int desired_iv_len, |
| 1487 | int desired_key_len, |
| 1488 | data_t *expected_server_write_key, |
| 1489 | data_t *expected_server_write_iv, |
| 1490 | data_t *expected_client_write_key, |
| 1491 | data_t *expected_client_write_iv) |
Hanno Becker | 19498f8 | 2020-08-21 13:37:08 +0100 | [diff] [blame] | 1492 | { |
| 1493 | mbedtls_ssl_key_set keys; |
| 1494 | |
| 1495 | /* Check sanity of test parameters. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1496 | TEST_ASSERT(client_secret->len == server_secret->len); |
Yanray Wang | 82b4149 | 2023-03-14 12:26:44 +0800 | [diff] [blame] | 1497 | TEST_ASSERT( |
| 1498 | expected_client_write_iv->len == expected_server_write_iv->len && |
| 1499 | expected_client_write_iv->len == (size_t) desired_iv_len); |
| 1500 | TEST_ASSERT( |
| 1501 | expected_client_write_key->len == expected_server_write_key->len && |
| 1502 | expected_client_write_key->len == (size_t) desired_key_len); |
Hanno Becker | 19498f8 | 2020-08-21 13:37:08 +0100 | [diff] [blame] | 1503 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1504 | TEST_ASSERT(mbedtls_ssl_tls1_3_make_traffic_keys( |
| 1505 | (mbedtls_md_type_t) hash_alg, |
| 1506 | client_secret->x, |
| 1507 | server_secret->x, |
| 1508 | client_secret->len /* == server_secret->len */, |
| 1509 | desired_key_len, desired_iv_len, |
| 1510 | &keys) == 0); |
Hanno Becker | 19498f8 | 2020-08-21 13:37:08 +0100 | [diff] [blame] | 1511 | |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 1512 | TEST_MEMORY_COMPARE(keys.client_write_key, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 1513 | keys.key_len, |
| 1514 | expected_client_write_key->x, |
| 1515 | (size_t) desired_key_len); |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 1516 | TEST_MEMORY_COMPARE(keys.server_write_key, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 1517 | keys.key_len, |
| 1518 | expected_server_write_key->x, |
| 1519 | (size_t) desired_key_len); |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 1520 | TEST_MEMORY_COMPARE(keys.client_write_iv, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 1521 | keys.iv_len, |
| 1522 | expected_client_write_iv->x, |
| 1523 | (size_t) desired_iv_len); |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 1524 | TEST_MEMORY_COMPARE(keys.server_write_iv, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 1525 | keys.iv_len, |
| 1526 | expected_server_write_iv->x, |
| 1527 | (size_t) desired_iv_len); |
Hanno Becker | 19498f8 | 2020-08-21 13:37:08 +0100 | [diff] [blame] | 1528 | } |
| 1529 | /* END_CASE */ |
| 1530 | |
Hanno Becker | e4849d1 | 2020-08-21 14:14:14 +0100 | [diff] [blame] | 1531 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1532 | void ssl_tls1_3_derive_secret(int hash_alg, |
| 1533 | data_t *secret, |
| 1534 | int label_idx, |
| 1535 | data_t *ctx, |
| 1536 | int desired_length, |
| 1537 | int already_hashed, |
| 1538 | data_t *expected) |
Hanno Becker | e4849d1 | 2020-08-21 14:14:14 +0100 | [diff] [blame] | 1539 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1540 | unsigned char dst[100]; |
Hanno Becker | e4849d1 | 2020-08-21 14:14:14 +0100 | [diff] [blame] | 1541 | |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 1542 | unsigned char const *lbl = NULL; |
| 1543 | size_t lbl_len; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1544 | #define MBEDTLS_SSL_TLS1_3_LABEL(name, string) \ |
| 1545 | if (label_idx == (int) tls1_3_label_ ## name) \ |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 1546 | { \ |
| 1547 | lbl = mbedtls_ssl_tls1_3_labels.name; \ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1548 | lbl_len = sizeof(mbedtls_ssl_tls1_3_labels.name); \ |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 1549 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1550 | MBEDTLS_SSL_TLS1_3_LABEL_LIST |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 1551 | #undef MBEDTLS_SSL_TLS1_3_LABEL |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1552 | TEST_ASSERT(lbl != NULL); |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 1553 | |
Hanno Becker | e4849d1 | 2020-08-21 14:14:14 +0100 | [diff] [blame] | 1554 | /* Check sanity of test parameters. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1555 | TEST_ASSERT((size_t) desired_length <= sizeof(dst)); |
| 1556 | TEST_ASSERT((size_t) desired_length == expected->len); |
Hanno Becker | e4849d1 | 2020-08-21 14:14:14 +0100 | [diff] [blame] | 1557 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1558 | TEST_ASSERT(mbedtls_ssl_tls1_3_derive_secret( |
| 1559 | (mbedtls_md_type_t) hash_alg, |
| 1560 | secret->x, secret->len, |
| 1561 | lbl, lbl_len, |
| 1562 | ctx->x, ctx->len, |
| 1563 | already_hashed, |
| 1564 | dst, desired_length) == 0); |
Hanno Becker | e4849d1 | 2020-08-21 14:14:14 +0100 | [diff] [blame] | 1565 | |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 1566 | TEST_MEMORY_COMPARE(dst, desired_length, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 1567 | expected->x, desired_length); |
Hanno Becker | e4849d1 | 2020-08-21 14:14:14 +0100 | [diff] [blame] | 1568 | } |
| 1569 | /* END_CASE */ |
| 1570 | |
Hanno Becker | 2d2c3eb | 2020-08-20 14:54:24 +0100 | [diff] [blame] | 1571 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1572 | void ssl_tls1_3_key_evolution(int hash_alg, |
| 1573 | data_t *secret, |
| 1574 | data_t *input, |
| 1575 | data_t *expected) |
Hanno Becker | 2d2c3eb | 2020-08-20 14:54:24 +0100 | [diff] [blame] | 1576 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1577 | unsigned char secret_new[MBEDTLS_MD_MAX_SIZE]; |
Hanno Becker | 2d2c3eb | 2020-08-20 14:54:24 +0100 | [diff] [blame] | 1578 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1579 | TEST_ASSERT(mbedtls_ssl_tls1_3_evolve_secret( |
| 1580 | (mbedtls_md_type_t) hash_alg, |
| 1581 | secret->len ? secret->x : NULL, |
| 1582 | input->len ? input->x : NULL, input->len, |
| 1583 | secret_new) == 0); |
Hanno Becker | 2d2c3eb | 2020-08-20 14:54:24 +0100 | [diff] [blame] | 1584 | |
Tom Cosgrove | ba3b14d | 2023-09-04 11:23:02 +0100 | [diff] [blame] | 1585 | TEST_MEMORY_COMPARE(secret_new, (size_t) expected->len, |
Tom Cosgrove | a240fe3 | 2023-09-04 11:29:39 +0100 | [diff] [blame] | 1586 | expected->x, (size_t) expected->len); |
Hanno Becker | 2d2c3eb | 2020-08-20 14:54:24 +0100 | [diff] [blame] | 1587 | } |
| 1588 | /* END_CASE */ |
| 1589 | |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 1590 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1591 | void ssl_tls_prf(int type, data_t *secret, data_t *random, |
| 1592 | char *label, data_t *result_str, int exp_ret) |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 1593 | { |
| 1594 | unsigned char *output; |
| 1595 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1596 | output = mbedtls_calloc(1, result_str->len); |
| 1597 | if (output == NULL) { |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 1598 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1599 | } |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 1600 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1601 | USE_PSA_INIT(); |
Ron Eldor | 6b9b1b8 | 2019-05-15 17:04:33 +0300 | [diff] [blame] | 1602 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1603 | TEST_ASSERT(mbedtls_ssl_tls_prf(type, secret->x, secret->len, |
| 1604 | label, random->x, random->len, |
| 1605 | output, result_str->len) == exp_ret); |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 1606 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1607 | if (exp_ret == 0) { |
| 1608 | TEST_ASSERT(mbedtls_test_hexcmp(output, result_str->x, |
| 1609 | result_str->len, result_str->len) == 0); |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 1610 | } |
| 1611 | exit: |
| 1612 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1613 | mbedtls_free(output); |
| 1614 | USE_PSA_DONE(); |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 1615 | } |
| 1616 | /* END_CASE */ |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 1617 | |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 1618 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1619 | 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] | 1620 | { |
| 1621 | mbedtls_ssl_session original, restored; |
| 1622 | unsigned char *buf = NULL; |
| 1623 | size_t len; |
| 1624 | |
| 1625 | /* |
| 1626 | * Test that a save-load pair is the identity |
| 1627 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1628 | mbedtls_ssl_session_init(&original); |
| 1629 | mbedtls_ssl_session_init(&restored); |
Valerio Setti | 89ae9b6 | 2023-04-27 17:22:54 +0200 | [diff] [blame] | 1630 | USE_PSA_INIT(); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 1631 | |
| 1632 | /* Prepare a dummy session to work on */ |
Yanray Wang | 862ef86 | 2023-03-14 11:05:04 +0800 | [diff] [blame] | 1633 | TEST_ASSERT(mbedtls_test_ssl_populate_session( |
| 1634 | &original, ticket_len, crt_file) == 0); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 1635 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 1636 | /* Serialize it */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1637 | TEST_ASSERT(mbedtls_ssl_session_save(&original, NULL, 0, &len) |
| 1638 | == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL); |
Gilles Peskine | bf85200 | 2023-10-17 17:31:50 +0200 | [diff] [blame] | 1639 | TEST_CALLOC(buf, len); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1640 | TEST_ASSERT(mbedtls_ssl_session_save(&original, buf, len, &len) |
| 1641 | == 0); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 1642 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 1643 | /* Restore session from serialized data */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1644 | TEST_ASSERT(mbedtls_ssl_session_load(&restored, buf, len) == 0); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 1645 | |
| 1646 | /* |
| 1647 | * Make sure both session structures are identical |
| 1648 | */ |
| 1649 | #if defined(MBEDTLS_HAVE_TIME) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1650 | TEST_ASSERT(original.start == restored.start); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 1651 | #endif |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1652 | TEST_ASSERT(original.ciphersuite == restored.ciphersuite); |
| 1653 | TEST_ASSERT(original.compression == restored.compression); |
| 1654 | TEST_ASSERT(original.id_len == restored.id_len); |
| 1655 | TEST_ASSERT(memcmp(original.id, |
| 1656 | restored.id, sizeof(original.id)) == 0); |
| 1657 | TEST_ASSERT(memcmp(original.master, |
| 1658 | restored.master, sizeof(original.master)) == 0); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 1659 | |
Gilles Peskine | 33d03fe | 2023-02-01 18:37:49 +0100 | [diff] [blame] | 1660 | #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) && \ |
| 1661 | defined(MBEDTLS_CERTS_C) |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 1662 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1663 | TEST_ASSERT((original.peer_cert == NULL) == |
| 1664 | (restored.peer_cert == NULL)); |
| 1665 | if (original.peer_cert != NULL) { |
| 1666 | TEST_ASSERT(original.peer_cert->raw.len == |
| 1667 | restored.peer_cert->raw.len); |
| 1668 | TEST_ASSERT(memcmp(original.peer_cert->raw.p, |
| 1669 | restored.peer_cert->raw.p, |
| 1670 | original.peer_cert->raw.len) == 0); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 1671 | } |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 1672 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1673 | TEST_ASSERT(original.peer_cert_digest_type == |
| 1674 | restored.peer_cert_digest_type); |
| 1675 | TEST_ASSERT(original.peer_cert_digest_len == |
| 1676 | restored.peer_cert_digest_len); |
| 1677 | TEST_ASSERT((original.peer_cert_digest == NULL) == |
| 1678 | (restored.peer_cert_digest == NULL)); |
| 1679 | if (original.peer_cert_digest != NULL) { |
| 1680 | TEST_ASSERT(memcmp(original.peer_cert_digest, |
| 1681 | restored.peer_cert_digest, |
| 1682 | original.peer_cert_digest_len) == 0); |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 1683 | } |
| 1684 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Gilles Peskine | 33d03fe | 2023-02-01 18:37:49 +0100 | [diff] [blame] | 1685 | #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED && MBEDTLS_CERTS_C */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1686 | TEST_ASSERT(original.verify_result == restored.verify_result); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 1687 | |
| 1688 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1689 | TEST_ASSERT(original.ticket_len == restored.ticket_len); |
| 1690 | if (original.ticket_len != 0) { |
| 1691 | TEST_ASSERT(original.ticket != NULL); |
| 1692 | TEST_ASSERT(restored.ticket != NULL); |
| 1693 | TEST_ASSERT(memcmp(original.ticket, |
| 1694 | restored.ticket, original.ticket_len) == 0); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 1695 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1696 | TEST_ASSERT(original.ticket_lifetime == restored.ticket_lifetime); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 1697 | #endif |
| 1698 | |
| 1699 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1700 | TEST_ASSERT(original.mfl_code == restored.mfl_code); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 1701 | #endif |
| 1702 | |
| 1703 | #if defined(MBEDTLS_SSL_TRUNCATED_HMAC) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1704 | TEST_ASSERT(original.trunc_hmac == restored.trunc_hmac); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 1705 | #endif |
| 1706 | |
| 1707 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1708 | TEST_ASSERT(original.encrypt_then_mac == restored.encrypt_then_mac); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 1709 | #endif |
| 1710 | |
| 1711 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1712 | mbedtls_ssl_session_free(&original); |
| 1713 | mbedtls_ssl_session_free(&restored); |
| 1714 | mbedtls_free(buf); |
Valerio Setti | 44570a5 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 1715 | USE_PSA_DONE(); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 1716 | } |
| 1717 | /* END_CASE */ |
| 1718 | |
Manuel Pégourié-Gonnard | aa75583 | 2019-06-03 10:53:47 +0200 | [diff] [blame] | 1719 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1720 | 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] | 1721 | { |
| 1722 | mbedtls_ssl_session session; |
| 1723 | unsigned char *buf1 = NULL, *buf2 = NULL; |
| 1724 | size_t len0, len1, len2; |
| 1725 | |
| 1726 | /* |
| 1727 | * Test that a load-save pair is the identity |
| 1728 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1729 | mbedtls_ssl_session_init(&session); |
Valerio Setti | 89ae9b6 | 2023-04-27 17:22:54 +0200 | [diff] [blame] | 1730 | USE_PSA_INIT(); |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 1731 | |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1732 | /* Prepare a dummy session to work on */ |
Yanray Wang | 862ef86 | 2023-03-14 11:05:04 +0800 | [diff] [blame] | 1733 | TEST_ASSERT(mbedtls_test_ssl_populate_session( |
| 1734 | &session, ticket_len, crt_file) == 0); |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 1735 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 1736 | /* Get desired buffer size for serializing */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1737 | TEST_ASSERT(mbedtls_ssl_session_save(&session, NULL, 0, &len0) |
| 1738 | == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL); |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 1739 | |
| 1740 | /* Allocate first buffer */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1741 | buf1 = mbedtls_calloc(1, len0); |
| 1742 | TEST_ASSERT(buf1 != NULL); |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 1743 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 1744 | /* Serialize to buffer and free live session */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1745 | TEST_ASSERT(mbedtls_ssl_session_save(&session, buf1, len0, &len1) |
| 1746 | == 0); |
| 1747 | TEST_ASSERT(len0 == len1); |
| 1748 | mbedtls_ssl_session_free(&session); |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 1749 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 1750 | /* Restore session from serialized data */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1751 | TEST_ASSERT(mbedtls_ssl_session_load(&session, buf1, len1) == 0); |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 1752 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 1753 | /* Allocate second buffer and serialize to it */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1754 | buf2 = mbedtls_calloc(1, len0); |
| 1755 | TEST_ASSERT(buf2 != NULL); |
| 1756 | TEST_ASSERT(mbedtls_ssl_session_save(&session, buf2, len0, &len2) |
| 1757 | == 0); |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 1758 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 1759 | /* Make sure both serialized versions are identical */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1760 | TEST_ASSERT(len1 == len2); |
| 1761 | TEST_ASSERT(memcmp(buf1, buf2, len1) == 0); |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 1762 | |
| 1763 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1764 | mbedtls_ssl_session_free(&session); |
| 1765 | mbedtls_free(buf1); |
| 1766 | mbedtls_free(buf2); |
Valerio Setti | 44570a5 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 1767 | USE_PSA_DONE(); |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 1768 | } |
| 1769 | /* END_CASE */ |
Manuel Pégourié-Gonnard | f5fa0aa | 2019-05-23 10:38:11 +0200 | [diff] [blame] | 1770 | |
| 1771 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1772 | 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] | 1773 | { |
| 1774 | mbedtls_ssl_session session; |
| 1775 | unsigned char *buf = NULL; |
| 1776 | size_t good_len, bad_len, test_len; |
| 1777 | |
| 1778 | /* |
| 1779 | * Test that session_save() fails cleanly on small buffers |
| 1780 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1781 | mbedtls_ssl_session_init(&session); |
Valerio Setti | 89ae9b6 | 2023-04-27 17:22:54 +0200 | [diff] [blame] | 1782 | USE_PSA_INIT(); |
Manuel Pégourié-Gonnard | f5fa0aa | 2019-05-23 10:38:11 +0200 | [diff] [blame] | 1783 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 1784 | /* Prepare dummy session and get serialized size */ |
Yanray Wang | 862ef86 | 2023-03-14 11:05:04 +0800 | [diff] [blame] | 1785 | TEST_ASSERT(mbedtls_test_ssl_populate_session( |
| 1786 | &session, ticket_len, crt_file) == 0); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1787 | TEST_ASSERT(mbedtls_ssl_session_save(&session, NULL, 0, &good_len) |
| 1788 | == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL); |
Manuel Pégourié-Gonnard | f5fa0aa | 2019-05-23 10:38:11 +0200 | [diff] [blame] | 1789 | |
| 1790 | /* Try all possible bad lengths */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1791 | for (bad_len = 1; bad_len < good_len; bad_len++) { |
Manuel Pégourié-Gonnard | f5fa0aa | 2019-05-23 10:38:11 +0200 | [diff] [blame] | 1792 | /* Allocate exact size so that asan/valgrind can detect any overwrite */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1793 | mbedtls_free(buf); |
Gilles Peskine | bf85200 | 2023-10-17 17:31:50 +0200 | [diff] [blame] | 1794 | buf = NULL; |
| 1795 | TEST_CALLOC(buf, bad_len); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1796 | TEST_ASSERT(mbedtls_ssl_session_save(&session, buf, bad_len, |
| 1797 | &test_len) |
| 1798 | == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL); |
| 1799 | TEST_ASSERT(test_len == good_len); |
Manuel Pégourié-Gonnard | f5fa0aa | 2019-05-23 10:38:11 +0200 | [diff] [blame] | 1800 | } |
| 1801 | |
| 1802 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1803 | mbedtls_ssl_session_free(&session); |
| 1804 | mbedtls_free(buf); |
Valerio Setti | 44570a5 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 1805 | USE_PSA_DONE(); |
Manuel Pégourié-Gonnard | f5fa0aa | 2019-05-23 10:38:11 +0200 | [diff] [blame] | 1806 | } |
| 1807 | /* END_CASE */ |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 1808 | |
| 1809 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1810 | 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] | 1811 | { |
| 1812 | mbedtls_ssl_session session; |
| 1813 | unsigned char *good_buf = NULL, *bad_buf = NULL; |
| 1814 | size_t good_len, bad_len; |
| 1815 | |
| 1816 | /* |
| 1817 | * Test that session_load() fails cleanly on small buffers |
| 1818 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1819 | mbedtls_ssl_session_init(&session); |
Valerio Setti | 89ae9b6 | 2023-04-27 17:22:54 +0200 | [diff] [blame] | 1820 | USE_PSA_INIT(); |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 1821 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 1822 | /* Prepare serialized session data */ |
Yanray Wang | 862ef86 | 2023-03-14 11:05:04 +0800 | [diff] [blame] | 1823 | TEST_ASSERT(mbedtls_test_ssl_populate_session( |
| 1824 | &session, ticket_len, crt_file) == 0); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1825 | TEST_ASSERT(mbedtls_ssl_session_save(&session, NULL, 0, &good_len) |
| 1826 | == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL); |
Gilles Peskine | bf85200 | 2023-10-17 17:31:50 +0200 | [diff] [blame] | 1827 | TEST_CALLOC(good_buf, good_len); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1828 | TEST_ASSERT(mbedtls_ssl_session_save(&session, good_buf, good_len, |
| 1829 | &good_len) == 0); |
| 1830 | mbedtls_ssl_session_free(&session); |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 1831 | |
| 1832 | /* Try all possible bad lengths */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1833 | for (bad_len = 0; bad_len < good_len; bad_len++) { |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 1834 | /* Allocate exact size so that asan/valgrind can detect any overread */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1835 | mbedtls_free(bad_buf); |
Gilles Peskine | bf85200 | 2023-10-17 17:31:50 +0200 | [diff] [blame] | 1836 | bad_buf = NULL; |
| 1837 | TEST_CALLOC_NONNULL(bad_buf, bad_len); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1838 | memcpy(bad_buf, good_buf, bad_len); |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 1839 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1840 | TEST_ASSERT(mbedtls_ssl_session_load(&session, bad_buf, bad_len) |
| 1841 | == MBEDTLS_ERR_SSL_BAD_INPUT_DATA); |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 1842 | } |
| 1843 | |
| 1844 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1845 | mbedtls_ssl_session_free(&session); |
| 1846 | mbedtls_free(good_buf); |
| 1847 | mbedtls_free(bad_buf); |
Valerio Setti | 44570a5 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 1848 | USE_PSA_DONE(); |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 1849 | } |
| 1850 | /* END_CASE */ |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 1851 | |
Hanno Becker | 363b646 | 2019-05-29 12:44:28 +0100 | [diff] [blame] | 1852 | /* BEGIN_CASE */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1853 | void ssl_session_serialize_version_check(int corrupt_major, |
| 1854 | int corrupt_minor, |
| 1855 | int corrupt_patch, |
| 1856 | int corrupt_config) |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 1857 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1858 | unsigned char serialized_session[2048]; |
Hanno Becker | 363b646 | 2019-05-29 12:44:28 +0100 | [diff] [blame] | 1859 | size_t serialized_session_len; |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 1860 | unsigned cur_byte; |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 1861 | mbedtls_ssl_session session; |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 1862 | uint8_t should_corrupt_byte[] = { corrupt_major == 1, |
| 1863 | corrupt_minor == 1, |
| 1864 | corrupt_patch == 1, |
| 1865 | corrupt_config == 1, |
| 1866 | corrupt_config == 1 }; |
| 1867 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1868 | mbedtls_ssl_session_init(&session); |
Valerio Setti | 89ae9b6 | 2023-04-27 17:22:54 +0200 | [diff] [blame] | 1869 | USE_PSA_INIT(); |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 1870 | |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 1871 | /* Infer length of serialized session. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1872 | TEST_ASSERT(mbedtls_ssl_session_save(&session, |
| 1873 | serialized_session, |
| 1874 | sizeof(serialized_session), |
| 1875 | &serialized_session_len) == 0); |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 1876 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1877 | mbedtls_ssl_session_free(&session); |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 1878 | |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 1879 | /* Without any modification, we should be able to successfully |
Hanno Becker | 363b646 | 2019-05-29 12:44:28 +0100 | [diff] [blame] | 1880 | * de-serialize the session - double-check that. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1881 | TEST_ASSERT(mbedtls_ssl_session_load(&session, |
| 1882 | serialized_session, |
| 1883 | serialized_session_len) == 0); |
| 1884 | mbedtls_ssl_session_free(&session); |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 1885 | |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 1886 | /* Go through the bytes in the serialized session header and |
| 1887 | * corrupt them bit-by-bit. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1888 | for (cur_byte = 0; cur_byte < sizeof(should_corrupt_byte); cur_byte++) { |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 1889 | int cur_bit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1890 | unsigned char * const byte = &serialized_session[cur_byte]; |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 1891 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1892 | if (should_corrupt_byte[cur_byte] == 0) { |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 1893 | continue; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1894 | } |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 1895 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1896 | for (cur_bit = 0; cur_bit < CHAR_BIT; cur_bit++) { |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 1897 | unsigned char const corrupted_bit = 0x1u << cur_bit; |
| 1898 | /* Modify a single bit in the serialized session. */ |
| 1899 | *byte ^= corrupted_bit; |
| 1900 | |
| 1901 | /* Attempt to deserialize */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1902 | TEST_ASSERT(mbedtls_ssl_session_load(&session, |
| 1903 | serialized_session, |
| 1904 | serialized_session_len) == |
| 1905 | MBEDTLS_ERR_SSL_VERSION_MISMATCH); |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 1906 | |
| 1907 | /* Undo the change */ |
| 1908 | *byte ^= corrupted_bit; |
| 1909 | } |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 1910 | } |
valerio | 5b5da94 | 2023-04-20 11:59:52 +0200 | [diff] [blame] | 1911 | |
| 1912 | exit: |
Valerio Setti | 44570a5 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 1913 | USE_PSA_DONE(); |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 1914 | } |
| 1915 | /* END_CASE */ |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 1916 | |
Gilles Peskine | 1fc7116 | 2023-07-20 20:04:00 +0200 | [diff] [blame] | 1917 | /* 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_TEST_NULL_ENTROPY:!MBEDTLS_PSA_INJECT_ENTROPY:MBEDTLS_CTR_DRBG_C:MBEDTLS_SHA256_C */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1918 | void mbedtls_endpoint_sanity(int endpoint_type) |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 1919 | { |
| 1920 | enum { BUFFSIZE = 1024 }; |
Yanray Wang | abfdcd8 | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 1921 | mbedtls_test_ssl_endpoint ep; |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 1922 | int ret = -1; |
| 1923 | |
Valerio Setti | 44570a5 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 1924 | USE_PSA_INIT(); |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 1925 | ret = mbedtls_test_ssl_endpoint_init(NULL, endpoint_type, MBEDTLS_PK_RSA, |
| 1926 | NULL, NULL, NULL, NULL); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1927 | TEST_ASSERT(MBEDTLS_ERR_SSL_BAD_INPUT_DATA == ret); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 1928 | |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 1929 | ret = mbedtls_test_ssl_endpoint_certificate_init(NULL, MBEDTLS_PK_RSA); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1930 | TEST_ASSERT(MBEDTLS_ERR_SSL_BAD_INPUT_DATA == ret); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 1931 | |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 1932 | ret = mbedtls_test_ssl_endpoint_init(&ep, endpoint_type, MBEDTLS_PK_RSA, |
| 1933 | NULL, NULL, NULL, NULL); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1934 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 1935 | |
| 1936 | exit: |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 1937 | mbedtls_test_ssl_endpoint_free(&ep, NULL); |
Valerio Setti | 44570a5 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 1938 | USE_PSA_DONE(); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 1939 | } |
| 1940 | /* END_CASE */ |
| 1941 | |
Gilles Peskine | 1fc7116 | 2023-07-20 20:04:00 +0200 | [diff] [blame] | 1942 | /* 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_TEST_NULL_ENTROPY:!MBEDTLS_PSA_INJECT_ENTROPY:MBEDTLS_CTR_DRBG_C:MBEDTLS_ECP_C:MBEDTLS_SHA256_C */ |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 1943 | void move_handshake_to_state(int endpoint_type, int state, int need_pass) |
| 1944 | { |
| 1945 | enum { BUFFSIZE = 1024 }; |
Yanray Wang | abfdcd8 | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 1946 | mbedtls_test_ssl_endpoint base_ep, second_ep; |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 1947 | int ret = -1; |
| 1948 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1949 | mbedtls_platform_zeroize(&base_ep, sizeof(base_ep)); |
| 1950 | mbedtls_platform_zeroize(&second_ep, sizeof(second_ep)); |
Andrzej Kurek | 0d2982b | 2022-10-18 07:55:46 -0400 | [diff] [blame] | 1951 | |
Yanray Wang | 862ef86 | 2023-03-14 11:05:04 +0800 | [diff] [blame] | 1952 | ret = mbedtls_test_ssl_endpoint_init(&base_ep, endpoint_type, |
| 1953 | MBEDTLS_PK_RSA, |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 1954 | NULL, NULL, NULL, NULL); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1955 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 1956 | |
Yanray Wang | 862ef86 | 2023-03-14 11:05:04 +0800 | [diff] [blame] | 1957 | ret = mbedtls_test_ssl_endpoint_init( |
| 1958 | &second_ep, |
| 1959 | (endpoint_type == MBEDTLS_SSL_IS_SERVER) ? |
| 1960 | MBEDTLS_SSL_IS_CLIENT : MBEDTLS_SSL_IS_SERVER, |
| 1961 | MBEDTLS_PK_RSA, NULL, NULL, NULL, NULL); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1962 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 1963 | |
Valerio Setti | 89ae9b6 | 2023-04-27 17:22:54 +0200 | [diff] [blame] | 1964 | USE_PSA_INIT(); |
| 1965 | |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 1966 | ret = mbedtls_test_mock_socket_connect(&(base_ep.socket), |
| 1967 | &(second_ep.socket), |
| 1968 | BUFFSIZE); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1969 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 1970 | |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 1971 | ret = mbedtls_test_move_handshake_to_state(&(base_ep.ssl), |
| 1972 | &(second_ep.ssl), |
| 1973 | state); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 1974 | if (need_pass) { |
| 1975 | TEST_ASSERT(ret == 0); |
| 1976 | TEST_ASSERT(base_ep.ssl.state == state); |
| 1977 | } else { |
| 1978 | TEST_ASSERT(ret != 0); |
| 1979 | TEST_ASSERT(base_ep.ssl.state != state); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 1980 | } |
| 1981 | |
| 1982 | exit: |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 1983 | mbedtls_test_ssl_endpoint_free(&base_ep, NULL); |
| 1984 | mbedtls_test_ssl_endpoint_free(&second_ep, NULL); |
Valerio Setti | 44570a5 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 1985 | USE_PSA_DONE(); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 1986 | } |
| 1987 | /* END_CASE */ |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 1988 | |
Gilles Peskine | 33d03fe | 2023-02-01 18:37:49 +0100 | [diff] [blame] | 1989 | /* 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] | 1990 | void handshake_version(int dtls, int client_min_version, int client_max_version, |
| 1991 | int server_min_version, int server_max_version, |
| 1992 | int expected_negotiated_version) |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 1993 | { |
Yanray Wang | abfdcd8 | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 1994 | mbedtls_test_handshake_test_options options; |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 1995 | mbedtls_test_init_handshake_options(&options); |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 1996 | |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 1997 | options.client_min_version = client_min_version; |
| 1998 | options.client_max_version = client_max_version; |
| 1999 | options.server_min_version = server_min_version; |
| 2000 | options.server_max_version = server_max_version; |
| 2001 | |
| 2002 | options.expected_negotiated_version = expected_negotiated_version; |
| 2003 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2004 | options.dtls = dtls; |
Piotr Nowicki | 438bf3b | 2020-03-10 12:59:10 +0100 | [diff] [blame] | 2005 | /* By default, SSLv3.0 and TLSv1.0 use 1/n-1 splitting when sending data, so |
| 2006 | * the number of fragments will be twice as big. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2007 | if (expected_negotiated_version == MBEDTLS_SSL_MINOR_VERSION_0 || |
| 2008 | expected_negotiated_version == MBEDTLS_SSL_MINOR_VERSION_1) { |
Piotr Nowicki | 438bf3b | 2020-03-10 12:59:10 +0100 | [diff] [blame] | 2009 | options.expected_cli_fragments = 2; |
| 2010 | options.expected_srv_fragments = 2; |
Andrzej Kurek | 941962e | 2020-02-07 09:20:32 -0500 | [diff] [blame] | 2011 | } |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2012 | mbedtls_test_ssl_perform_handshake(&options); |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 2013 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2014 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 2015 | goto exit; |
| 2016 | } |
| 2017 | /* END_CASE */ |
Andrzej Kurek | 9e9efdc | 2020-02-26 05:25:23 -0500 | [diff] [blame] | 2018 | |
Gilles Peskine | 33d03fe | 2023-02-01 18:37:49 +0100 | [diff] [blame] | 2019 | /* 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] | 2020 | 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] | 2021 | { |
Yanray Wang | abfdcd8 | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 2022 | mbedtls_test_handshake_test_options options; |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2023 | mbedtls_test_init_handshake_options(&options); |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 2024 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2025 | options.cipher = cipher; |
| 2026 | options.dtls = dtls; |
| 2027 | options.psk_str = psk_str; |
| 2028 | options.pk_alg = pk_alg; |
Andrzej Kurek | cc5169c | 2020-02-04 09:04:56 -0500 | [diff] [blame] | 2029 | |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2030 | mbedtls_test_ssl_perform_handshake(&options); |
Andrzej Kurek | 316da1f | 2020-02-26 09:03:47 -0500 | [diff] [blame] | 2031 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2032 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 2033 | goto exit; |
| 2034 | } |
| 2035 | /* END_CASE */ |
Andrzej Kurek | 316da1f | 2020-02-26 09:03:47 -0500 | [diff] [blame] | 2036 | |
Gilles Peskine | 33d03fe | 2023-02-01 18:37:49 +0100 | [diff] [blame] | 2037 | /* 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] | 2038 | void handshake_cipher(char *cipher, int pk_alg, int dtls) |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2039 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2040 | test_handshake_psk_cipher(cipher, pk_alg, NULL, dtls); |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 2041 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2042 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 2043 | goto exit; |
| 2044 | } |
| 2045 | /* END_CASE */ |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 2046 | |
Gilles Peskine | 33d03fe | 2023-02-01 18:37:49 +0100 | [diff] [blame] | 2047 | /* 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] | 2048 | void app_data(int mfl, int cli_msg_len, int srv_msg_len, |
| 2049 | int expected_cli_fragments, |
| 2050 | int expected_srv_fragments, int dtls) |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2051 | { |
Yanray Wang | abfdcd8 | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 2052 | mbedtls_test_handshake_test_options options; |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2053 | mbedtls_test_init_handshake_options(&options); |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 2054 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2055 | options.mfl = mfl; |
| 2056 | options.cli_msg_len = cli_msg_len; |
| 2057 | options.srv_msg_len = srv_msg_len; |
| 2058 | options.expected_cli_fragments = expected_cli_fragments; |
| 2059 | options.expected_srv_fragments = expected_srv_fragments; |
| 2060 | options.dtls = dtls; |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 2061 | |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2062 | mbedtls_test_ssl_perform_handshake(&options); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2063 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 2064 | goto exit; |
| 2065 | } |
| 2066 | /* END_CASE */ |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 2067 | |
Gilles Peskine | 33d03fe | 2023-02-01 18:37:49 +0100 | [diff] [blame] | 2068 | /* 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] | 2069 | void app_data_tls(int mfl, int cli_msg_len, int srv_msg_len, |
| 2070 | int expected_cli_fragments, |
| 2071 | int expected_srv_fragments) |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2072 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2073 | test_app_data(mfl, cli_msg_len, srv_msg_len, expected_cli_fragments, |
| 2074 | expected_srv_fragments, 0); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2075 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 2076 | goto exit; |
| 2077 | } |
| 2078 | /* END_CASE */ |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 2079 | |
Gilles Peskine | 33d03fe | 2023-02-01 18:37:49 +0100 | [diff] [blame] | 2080 | /* 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] | 2081 | void app_data_dtls(int mfl, int cli_msg_len, int srv_msg_len, |
| 2082 | int expected_cli_fragments, |
| 2083 | int expected_srv_fragments) |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2084 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2085 | test_app_data(mfl, cli_msg_len, srv_msg_len, expected_cli_fragments, |
| 2086 | expected_srv_fragments, 1); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2087 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 2088 | goto exit; |
| 2089 | } |
| 2090 | /* END_CASE */ |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 2091 | |
Gilles Peskine | 33d03fe | 2023-02-01 18:37:49 +0100 | [diff] [blame] | 2092 | /* 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] | 2093 | void handshake_serialization() |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2094 | { |
Yanray Wang | abfdcd8 | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 2095 | mbedtls_test_handshake_test_options options; |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2096 | mbedtls_test_init_handshake_options(&options); |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 2097 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2098 | options.serialize = 1; |
| 2099 | options.dtls = 1; |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2100 | mbedtls_test_ssl_perform_handshake(&options); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2101 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 2102 | goto exit; |
| 2103 | } |
| 2104 | /* END_CASE */ |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 2105 | |
Gilles Peskine | 33d03fe | 2023-02-01 18:37:49 +0100 | [diff] [blame] | 2106 | /* 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] | 2107 | void handshake_fragmentation(int mfl, |
| 2108 | int expected_srv_hs_fragmentation, |
| 2109 | int expected_cli_hs_fragmentation) |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 2110 | { |
Yanray Wang | abfdcd8 | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 2111 | mbedtls_test_handshake_test_options options; |
| 2112 | mbedtls_test_ssl_log_pattern srv_pattern, cli_pattern; |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 2113 | |
| 2114 | srv_pattern.pattern = cli_pattern.pattern = "found fragmented DTLS handshake"; |
| 2115 | srv_pattern.counter = 0; |
| 2116 | cli_pattern.counter = 0; |
| 2117 | |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2118 | mbedtls_test_init_handshake_options(&options); |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 2119 | options.dtls = 1; |
| 2120 | options.mfl = mfl; |
Darryl Green | aad82f9 | 2019-12-02 10:53:11 +0000 | [diff] [blame] | 2121 | /* Set cipher to one using CBC so that record splitting can be tested */ |
| 2122 | options.cipher = "TLS-DHE-RSA-WITH-AES-256-CBC-SHA256"; |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 2123 | options.srv_auth_mode = MBEDTLS_SSL_VERIFY_REQUIRED; |
| 2124 | options.srv_log_obj = &srv_pattern; |
| 2125 | options.cli_log_obj = &cli_pattern; |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2126 | options.srv_log_fun = mbedtls_test_ssl_log_analyzer; |
| 2127 | options.cli_log_fun = mbedtls_test_ssl_log_analyzer; |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 2128 | |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2129 | mbedtls_test_ssl_perform_handshake(&options); |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 2130 | |
| 2131 | /* Test if the server received a fragmented handshake */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2132 | if (expected_srv_hs_fragmentation) { |
| 2133 | TEST_ASSERT(srv_pattern.counter >= 1); |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 2134 | } |
| 2135 | /* Test if the client received a fragmented handshake */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2136 | if (expected_cli_hs_fragmentation) { |
| 2137 | TEST_ASSERT(cli_pattern.counter >= 1); |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 2138 | } |
| 2139 | } |
| 2140 | /* END_CASE */ |
| 2141 | |
Gilles Peskine | 33d03fe | 2023-02-01 18:37:49 +0100 | [diff] [blame] | 2142 | /* 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] | 2143 | void renegotiation(int legacy_renegotiation) |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2144 | { |
Yanray Wang | abfdcd8 | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 2145 | mbedtls_test_handshake_test_options options; |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2146 | mbedtls_test_init_handshake_options(&options); |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 2147 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2148 | options.renegotiate = 1; |
| 2149 | options.legacy_renegotiation = legacy_renegotiation; |
| 2150 | options.dtls = 1; |
Andrzej Kurek | 316da1f | 2020-02-26 09:03:47 -0500 | [diff] [blame] | 2151 | |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2152 | mbedtls_test_ssl_perform_handshake(&options); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2153 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 2154 | goto exit; |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 2155 | } |
| 2156 | /* END_CASE */ |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2157 | |
Gilles Peskine | 33d03fe | 2023-02-01 18:37:49 +0100 | [diff] [blame] | 2158 | /* 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] | 2159 | void resize_buffers(int mfl, int renegotiation, int legacy_renegotiation, |
| 2160 | int serialize, int dtls, char *cipher) |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2161 | { |
Yanray Wang | abfdcd8 | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 2162 | mbedtls_test_handshake_test_options options; |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2163 | mbedtls_test_init_handshake_options(&options); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2164 | |
| 2165 | options.mfl = mfl; |
Andrzej Kurek | 8ea6872 | 2020-04-03 06:40:47 -0400 | [diff] [blame] | 2166 | options.cipher = cipher; |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2167 | options.renegotiate = renegotiation; |
| 2168 | options.legacy_renegotiation = legacy_renegotiation; |
| 2169 | options.serialize = serialize; |
| 2170 | options.dtls = dtls; |
| 2171 | options.resize_buffers = 1; |
| 2172 | |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2173 | mbedtls_test_ssl_perform_handshake(&options); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2174 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 2175 | goto exit; |
| 2176 | } |
| 2177 | /* END_CASE */ |
| 2178 | |
Gilles Peskine | 33d03fe | 2023-02-01 18:37:49 +0100 | [diff] [blame] | 2179 | /* 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] | 2180 | void resize_buffers_serialize_mfl(int mfl) |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2181 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2182 | test_resize_buffers(mfl, 0, MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION, 1, 1, |
| 2183 | (char *) ""); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2184 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 2185 | goto exit; |
| 2186 | } |
| 2187 | /* END_CASE */ |
| 2188 | |
Gilles Peskine | 33d03fe | 2023-02-01 18:37:49 +0100 | [diff] [blame] | 2189 | /* 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] | 2190 | void resize_buffers_renegotiate_mfl(int mfl, int legacy_renegotiation, |
| 2191 | char *cipher) |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2192 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2193 | test_resize_buffers(mfl, 1, legacy_renegotiation, 0, 1, cipher); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2194 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 2195 | goto exit; |
| 2196 | } |
| 2197 | /* END_CASE */ |
Manuel Pégourié-Gonnard | 045f094 | 2020-07-02 11:34:02 +0200 | [diff] [blame] | 2198 | |
Gilles Peskine | 1fc7116 | 2023-07-20 20:04:00 +0200 | [diff] [blame] | 2199 | /* 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_TEST_NULL_ENTROPY:!MBEDTLS_PSA_INJECT_ENTROPY: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] | 2200 | void raw_key_agreement_fail(int bad_server_ecdhe_key) |
Andrzej Kurek | b4eedf7 | 2022-04-15 05:41:14 -0400 | [diff] [blame] | 2201 | { |
| 2202 | enum { BUFFSIZE = 17000 }; |
Yanray Wang | abfdcd8 | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 2203 | mbedtls_test_ssl_endpoint client, server; |
Andrzej Kurek | b4eedf7 | 2022-04-15 05:41:14 -0400 | [diff] [blame] | 2204 | mbedtls_psa_stats_t stats; |
Andrzej Kurek | 2582ba3 | 2022-03-31 06:30:54 -0400 | [diff] [blame] | 2205 | size_t free_slots_before = -1; |
Andrzej Kurek | b4eedf7 | 2022-04-15 05:41:14 -0400 | [diff] [blame] | 2206 | |
Andrzej Kurek | b4eedf7 | 2022-04-15 05:41:14 -0400 | [diff] [blame] | 2207 | mbedtls_ecp_group_id curve_list[] = { MBEDTLS_ECP_DP_SECP256R1, |
| 2208 | MBEDTLS_ECP_DP_NONE }; |
Valerio Setti | 89ae9b6 | 2023-04-27 17:22:54 +0200 | [diff] [blame] | 2209 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2210 | mbedtls_platform_zeroize(&client, sizeof(client)); |
| 2211 | mbedtls_platform_zeroize(&server, sizeof(server)); |
Andrzej Kurek | b4eedf7 | 2022-04-15 05:41:14 -0400 | [diff] [blame] | 2212 | |
| 2213 | /* Client side, force SECP256R1 to make one key bitflip fail |
Andrzej Kurek | 9cb14d4 | 2022-04-14 08:51:41 -0400 | [diff] [blame] | 2214 | * the raw key agreement. Flipping the first byte makes the |
| 2215 | * required 0x04 identifier invalid. */ |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2216 | TEST_EQUAL(mbedtls_test_ssl_endpoint_init(&client, MBEDTLS_SSL_IS_CLIENT, |
| 2217 | MBEDTLS_PK_ECDSA, NULL, NULL, |
| 2218 | NULL, curve_list), 0); |
Andrzej Kurek | b4eedf7 | 2022-04-15 05:41:14 -0400 | [diff] [blame] | 2219 | |
| 2220 | /* Server side */ |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2221 | TEST_EQUAL(mbedtls_test_ssl_endpoint_init(&server, MBEDTLS_SSL_IS_SERVER, |
| 2222 | MBEDTLS_PK_ECDSA, NULL, NULL, |
| 2223 | NULL, NULL), 0); |
Andrzej Kurek | b4eedf7 | 2022-04-15 05:41:14 -0400 | [diff] [blame] | 2224 | |
Valerio Setti | 89ae9b6 | 2023-04-27 17:22:54 +0200 | [diff] [blame] | 2225 | USE_PSA_INIT(); |
| 2226 | |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2227 | TEST_EQUAL(mbedtls_test_mock_socket_connect(&(client.socket), |
| 2228 | &(server.socket), |
| 2229 | BUFFSIZE), 0); |
Andrzej Kurek | b4eedf7 | 2022-04-15 05:41:14 -0400 | [diff] [blame] | 2230 | |
Yanray Wang | 862ef86 | 2023-03-14 11:05:04 +0800 | [diff] [blame] | 2231 | TEST_EQUAL(mbedtls_test_move_handshake_to_state( |
| 2232 | &(client.ssl), &(server.ssl), |
| 2233 | MBEDTLS_SSL_CLIENT_KEY_EXCHANGE) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2234 | , 0); |
Andrzej Kurek | b4eedf7 | 2022-04-15 05:41:14 -0400 | [diff] [blame] | 2235 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2236 | mbedtls_psa_get_stats(&stats); |
Andrzej Kurek | 2582ba3 | 2022-03-31 06:30:54 -0400 | [diff] [blame] | 2237 | /* Save the number of slots in use up to this point. |
| 2238 | * With PSA, one can be used for the ECDH private key. */ |
| 2239 | free_slots_before = stats.empty_slots; |
Andrzej Kurek | 99f6778 | 2022-03-31 07:17:18 -0400 | [diff] [blame] | 2240 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2241 | if (bad_server_ecdhe_key) { |
Gilles Peskine | 6dd489c | 2022-04-15 05:54:40 -0400 | [diff] [blame] | 2242 | /* Force a simulated bitflip in the server key. to make the |
| 2243 | * raw key agreement in ssl_write_client_key_exchange fail. */ |
| 2244 | (client.ssl).handshake->ecdh_psa_peerkey[0] ^= 0x02; |
| 2245 | } |
Andrzej Kurek | b4eedf7 | 2022-04-15 05:41:14 -0400 | [diff] [blame] | 2246 | |
Yanray Wang | 862ef86 | 2023-03-14 11:05:04 +0800 | [diff] [blame] | 2247 | TEST_EQUAL(mbedtls_test_move_handshake_to_state( |
| 2248 | &(client.ssl), &(server.ssl), MBEDTLS_SSL_HANDSHAKE_OVER), |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2249 | bad_server_ecdhe_key ? MBEDTLS_ERR_SSL_HW_ACCEL_FAILED : 0); |
Andrzej Kurek | b4eedf7 | 2022-04-15 05:41:14 -0400 | [diff] [blame] | 2250 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2251 | mbedtls_psa_get_stats(&stats); |
Andrzej Kurek | b4eedf7 | 2022-04-15 05:41:14 -0400 | [diff] [blame] | 2252 | |
Gilles Peskine | 6dd489c | 2022-04-15 05:54:40 -0400 | [diff] [blame] | 2253 | /* Make sure that the key slot is already destroyed in case of failure, |
| 2254 | * without waiting to close the connection. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2255 | if (bad_server_ecdhe_key) { |
| 2256 | TEST_EQUAL(free_slots_before, stats.empty_slots); |
| 2257 | } |
Andrzej Kurek | b4eedf7 | 2022-04-15 05:41:14 -0400 | [diff] [blame] | 2258 | |
| 2259 | exit: |
Yanray Wang | c463849 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2260 | mbedtls_test_ssl_endpoint_free(&client, NULL); |
| 2261 | mbedtls_test_ssl_endpoint_free(&server, NULL); |
Andrzej Kurek | 99f6778 | 2022-03-31 07:17:18 -0400 | [diff] [blame] | 2262 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2263 | USE_PSA_DONE(); |
Andrzej Kurek | b4eedf7 | 2022-04-15 05:41:14 -0400 | [diff] [blame] | 2264 | } |
| 2265 | /* END_CASE */ |
Andrzej Kurek | 862acb8 | 2022-06-06 13:08:23 -0400 | [diff] [blame] | 2266 | |
Andrzej Kurek | 3c036f5 | 2022-06-08 11:57:57 -0400 | [diff] [blame] | 2267 | /* 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] | 2268 | void cookie_parsing(data_t *cookie, int exp_ret) |
Andrzej Kurek | 862acb8 | 2022-06-06 13:08:23 -0400 | [diff] [blame] | 2269 | { |
| 2270 | mbedtls_ssl_context ssl; |
| 2271 | mbedtls_ssl_config conf; |
| 2272 | size_t len; |
| 2273 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2274 | mbedtls_ssl_init(&ssl); |
| 2275 | mbedtls_ssl_config_init(&conf); |
valerio | 5b5da94 | 2023-04-20 11:59:52 +0200 | [diff] [blame] | 2276 | |
| 2277 | USE_PSA_INIT(); |
| 2278 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2279 | TEST_EQUAL(mbedtls_ssl_config_defaults(&conf, MBEDTLS_SSL_IS_SERVER, |
| 2280 | MBEDTLS_SSL_TRANSPORT_DATAGRAM, |
| 2281 | MBEDTLS_SSL_PRESET_DEFAULT), |
| 2282 | 0); |
Andrzej Kurek | 862acb8 | 2022-06-06 13:08:23 -0400 | [diff] [blame] | 2283 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2284 | TEST_EQUAL(mbedtls_ssl_setup(&ssl, &conf), 0); |
| 2285 | TEST_EQUAL(mbedtls_ssl_check_dtls_clihlo_cookie(&ssl, ssl.cli_id, |
| 2286 | ssl.cli_id_len, |
| 2287 | cookie->x, cookie->len, |
| 2288 | ssl.out_buf, |
| 2289 | MBEDTLS_SSL_OUT_CONTENT_LEN, |
| 2290 | &len), |
| 2291 | exp_ret); |
Andrzej Kurek | 862acb8 | 2022-06-06 13:08:23 -0400 | [diff] [blame] | 2292 | |
Valerio Setti | 44570a5 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 2293 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 2294 | mbedtls_ssl_free(&ssl); |
| 2295 | mbedtls_ssl_config_free(&conf); |
Valerio Setti | 44570a5 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 2296 | USE_PSA_DONE(); |
Andrzej Kurek | 862acb8 | 2022-06-06 13:08:23 -0400 | [diff] [blame] | 2297 | } |
| 2298 | /* END_CASE */ |