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