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