Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 1 | /* BEGIN_HEADER */ |
Chris Jones | 84a773f | 2021-03-05 18:38:47 +0000 | [diff] [blame] | 2 | #include <ssl_misc.h> |
Andrzej Kurek | 941962e | 2020-02-07 09:20:32 -0500 | [diff] [blame] | 3 | #include <mbedtls/timing.h> |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 4 | #include <mbedtls/debug.h> |
Valerio Setti | 1b08d42 | 2023-02-13 11:33:26 +0100 | [diff] [blame] | 5 | #include <mbedtls/pk.h> |
Hanno Becker | 73c825a | 2020-09-08 10:52:58 +0100 | [diff] [blame] | 6 | #include <ssl_tls13_keys.h> |
Gabor Mezei | b35759d | 2022-02-09 16:59:11 +0100 | [diff] [blame] | 7 | #include <ssl_tls13_invasive.h> |
Yanray Wang | 47907a4 | 2022-10-24 14:42:01 +0800 | [diff] [blame] | 8 | #include <test/ssl_helpers.h> |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 9 | |
Gabor Mezei | 22c9a6f | 2021-10-20 12:09:35 +0200 | [diff] [blame] | 10 | #include <constant_time_internal.h> |
Manuel Pégourié-Gonnard | 9670a59 | 2020-07-10 10:21:46 +0200 | [diff] [blame] | 11 | #include <test/constant_flow.h> |
| 12 | |
Valerio Setti | 7c0f91b | 2023-04-28 12:20:34 +0200 | [diff] [blame] | 13 | #define SSL_MESSAGE_QUEUE_INIT { NULL, 0, 0, 0 } |
| 14 | |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 15 | /* END_HEADER */ |
| 16 | |
| 17 | /* BEGIN_DEPENDENCIES |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 18 | * depends_on:MBEDTLS_SSL_TLS_C |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 19 | * END_DEPENDENCIES |
| 20 | */ |
| 21 | |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 22 | /* BEGIN_CASE */ |
| 23 | void test_callback_buffer_sanity() |
| 24 | { |
| 25 | enum { MSGLEN = 10 }; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 26 | mbedtls_test_ssl_buffer buf; |
Gilles Peskine | 21e46b3 | 2023-10-17 16:35:20 +0200 | [diff] [blame] | 27 | mbedtls_test_ssl_buffer_init(&buf); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 28 | unsigned char input[MSGLEN]; |
| 29 | unsigned char output[MSGLEN]; |
| 30 | |
Valerio Setti | 285dae8 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 31 | USE_PSA_INIT(); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 32 | memset(input, 0, sizeof(input)); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 33 | |
| 34 | /* Make sure calling put and get on NULL buffer results in error. */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 35 | TEST_ASSERT(mbedtls_test_ssl_buffer_put(NULL, input, sizeof(input)) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 36 | == -1); |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 37 | TEST_ASSERT(mbedtls_test_ssl_buffer_get(NULL, output, sizeof(output)) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 38 | == -1); |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 39 | TEST_ASSERT(mbedtls_test_ssl_buffer_put(NULL, NULL, sizeof(input)) |
| 40 | == -1); |
Andrzej Kurek | f777414 | 2020-01-22 06:34:59 -0500 | [diff] [blame] | 41 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 42 | TEST_ASSERT(mbedtls_test_ssl_buffer_put(NULL, NULL, 0) == -1); |
| 43 | TEST_ASSERT(mbedtls_test_ssl_buffer_get(NULL, NULL, 0) == -1); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 44 | |
| 45 | /* Make sure calling put and get on a buffer that hasn't been set up results |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 46 | * in error. */ |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 47 | TEST_ASSERT(mbedtls_test_ssl_buffer_put(&buf, input, sizeof(input)) |
| 48 | == -1); |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 49 | TEST_ASSERT(mbedtls_test_ssl_buffer_get(&buf, output, sizeof(output)) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 50 | == -1); |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 51 | TEST_ASSERT(mbedtls_test_ssl_buffer_put(&buf, NULL, sizeof(input)) |
| 52 | == -1); |
Andrzej Kurek | f777414 | 2020-01-22 06:34:59 -0500 | [diff] [blame] | 53 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 54 | TEST_ASSERT(mbedtls_test_ssl_buffer_put(&buf, NULL, 0) == -1); |
| 55 | TEST_ASSERT(mbedtls_test_ssl_buffer_get(&buf, NULL, 0) == -1); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 56 | |
Andrzej Kurek | f777414 | 2020-01-22 06:34:59 -0500 | [diff] [blame] | 57 | /* Make sure calling put and get on NULL input only results in |
| 58 | * error if the length is not zero, and that a NULL output is valid for data |
| 59 | * dropping. |
| 60 | */ |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 61 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 62 | TEST_ASSERT(mbedtls_test_ssl_buffer_setup(&buf, sizeof(input)) == 0); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 63 | |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 64 | TEST_ASSERT(mbedtls_test_ssl_buffer_put(&buf, NULL, sizeof(input)) |
| 65 | == -1); |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 66 | TEST_ASSERT(mbedtls_test_ssl_buffer_get(&buf, NULL, sizeof(output)) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 67 | == 0); |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 68 | TEST_ASSERT(mbedtls_test_ssl_buffer_put(&buf, NULL, 0) == 0); |
| 69 | TEST_ASSERT(mbedtls_test_ssl_buffer_get(&buf, NULL, 0) == 0); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 70 | |
Piotr Nowicki | fb437d7 | 2020-01-13 16:59:12 +0100 | [diff] [blame] | 71 | /* Make sure calling put several times in the row is safe */ |
| 72 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 73 | TEST_ASSERT(mbedtls_test_ssl_buffer_put(&buf, input, sizeof(input)) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 74 | == sizeof(input)); |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 75 | TEST_ASSERT(mbedtls_test_ssl_buffer_get(&buf, output, 2) == 2); |
| 76 | TEST_ASSERT(mbedtls_test_ssl_buffer_put(&buf, input, 1) == 1); |
| 77 | TEST_ASSERT(mbedtls_test_ssl_buffer_put(&buf, input, 2) == 1); |
| 78 | TEST_ASSERT(mbedtls_test_ssl_buffer_put(&buf, input, 2) == 0); |
Piotr Nowicki | fb437d7 | 2020-01-13 16:59:12 +0100 | [diff] [blame] | 79 | |
| 80 | |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 81 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 82 | mbedtls_test_ssl_buffer_free(&buf); |
Valerio Setti | 285dae8 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 83 | USE_PSA_DONE(); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 84 | } |
| 85 | /* END_CASE */ |
| 86 | |
| 87 | /* |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 88 | * Test if the implementation of `mbedtls_test_ssl_buffer` related functions is |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 89 | * correct and works as expected. |
| 90 | * |
| 91 | * That is |
| 92 | * - If we try to put in \p put1 bytes then we can put in \p put1_ret bytes. |
| 93 | * - Afterwards if we try to get \p get1 bytes then we can get \get1_ret bytes. |
| 94 | * - Next, if we try to put in \p put1 bytes then we can put in \p put1_ret |
| 95 | * bytes. |
| 96 | * - Afterwards if we try to get \p get1 bytes then we can get \get1_ret bytes. |
| 97 | * - All of the bytes we got match the bytes we put in in a FIFO manner. |
| 98 | */ |
| 99 | |
| 100 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 101 | void test_callback_buffer(int size, int put1, int put1_ret, |
| 102 | int get1, int get1_ret, int put2, int put2_ret, |
| 103 | int get2, int get2_ret) |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 104 | { |
| 105 | enum { ROUNDS = 2 }; |
| 106 | size_t put[ROUNDS]; |
| 107 | int put_ret[ROUNDS]; |
| 108 | size_t get[ROUNDS]; |
| 109 | int get_ret[ROUNDS]; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 110 | mbedtls_test_ssl_buffer buf; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 111 | unsigned char *input = NULL; |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 112 | size_t input_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 113 | unsigned char *output = NULL; |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 114 | size_t output_len; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 115 | size_t i, j, written, read; |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 116 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 117 | mbedtls_test_ssl_buffer_init(&buf); |
Valerio Setti | 00a256f | 2023-04-27 17:22:27 +0200 | [diff] [blame] | 118 | USE_PSA_INIT(); |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 119 | TEST_ASSERT(mbedtls_test_ssl_buffer_setup(&buf, size) == 0); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 120 | |
| 121 | /* Check the sanity of input parameters and initialise local variables. That |
| 122 | * is, ensure that the amount of data is not negative and that we are not |
| 123 | * expecting more to put or get than we actually asked for. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 124 | TEST_ASSERT(put1 >= 0); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 125 | put[0] = put1; |
| 126 | put_ret[0] = put1_ret; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 127 | TEST_ASSERT(put1_ret <= put1); |
| 128 | TEST_ASSERT(put2 >= 0); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 129 | put[1] = put2; |
| 130 | put_ret[1] = put2_ret; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 131 | TEST_ASSERT(put2_ret <= put2); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 132 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 133 | TEST_ASSERT(get1 >= 0); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 134 | get[0] = get1; |
| 135 | get_ret[0] = get1_ret; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 136 | TEST_ASSERT(get1_ret <= get1); |
| 137 | TEST_ASSERT(get2 >= 0); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 138 | get[1] = get2; |
| 139 | get_ret[1] = get2_ret; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 140 | TEST_ASSERT(get2_ret <= get2); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 141 | |
| 142 | input_len = 0; |
| 143 | /* Calculate actual input and output lengths */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 144 | for (j = 0; j < ROUNDS; j++) { |
| 145 | if (put_ret[j] > 0) { |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 146 | input_len += put_ret[j]; |
| 147 | } |
| 148 | } |
| 149 | /* In order to always have a valid pointer we always allocate at least 1 |
| 150 | * byte. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 151 | if (input_len == 0) { |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 152 | input_len = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 153 | } |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 154 | TEST_CALLOC(input, input_len); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 155 | |
| 156 | output_len = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 157 | for (j = 0; j < ROUNDS; j++) { |
| 158 | if (get_ret[j] > 0) { |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 159 | output_len += get_ret[j]; |
| 160 | } |
| 161 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 162 | TEST_ASSERT(output_len <= input_len); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 163 | /* In order to always have a valid pointer we always allocate at least 1 |
| 164 | * byte. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 165 | if (output_len == 0) { |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 166 | output_len = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 167 | } |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 168 | TEST_CALLOC(output, output_len); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 169 | |
| 170 | /* Fill up the buffer with structured data so that unwanted changes |
| 171 | * can be detected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 172 | for (i = 0; i < input_len; i++) { |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 173 | input[i] = i & 0xFF; |
| 174 | } |
| 175 | |
| 176 | written = read = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 177 | for (j = 0; j < ROUNDS; j++) { |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 178 | TEST_ASSERT(put_ret[j] == mbedtls_test_ssl_buffer_put(&buf, |
| 179 | input + written, put[j])); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 180 | written += put_ret[j]; |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 181 | TEST_ASSERT(get_ret[j] == mbedtls_test_ssl_buffer_get(&buf, |
| 182 | output + read, get[j])); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 183 | read += get_ret[j]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 184 | TEST_ASSERT(read <= written); |
| 185 | if (get_ret[j] > 0) { |
| 186 | TEST_ASSERT(memcmp(output + read - get_ret[j], |
| 187 | input + read - get_ret[j], get_ret[j]) |
| 188 | == 0); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | |
| 192 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 193 | mbedtls_free(input); |
| 194 | mbedtls_free(output); |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 195 | mbedtls_test_ssl_buffer_free(&buf); |
Valerio Setti | 285dae8 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 196 | USE_PSA_DONE(); |
Janos Follath | 6264e66 | 2019-11-26 11:11:15 +0000 | [diff] [blame] | 197 | } |
| 198 | /* END_CASE */ |
| 199 | |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 200 | /* |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 201 | * Test if the implementation of `mbedtls_test_mock_socket` related |
| 202 | * I/O functions is correct and works as expected on unconnected sockets. |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 203 | */ |
| 204 | |
| 205 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 206 | void ssl_mock_sanity() |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 207 | { |
| 208 | enum { MSGLEN = 105 }; |
Paul Elliott | 21c8fe5 | 2021-11-24 16:54:26 +0000 | [diff] [blame] | 209 | unsigned char message[MSGLEN] = { 0 }; |
| 210 | unsigned char received[MSGLEN] = { 0 }; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 211 | mbedtls_test_mock_socket socket; |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 212 | |
Yanray Wang | 5f86a42 | 2023-03-15 16:02:29 +0800 | [diff] [blame] | 213 | mbedtls_test_mock_socket_init(&socket); |
Valerio Setti | 00a256f | 2023-04-27 17:22:27 +0200 | [diff] [blame] | 214 | USE_PSA_INIT(); |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 215 | TEST_ASSERT(mbedtls_test_mock_tcp_send_b(&socket, message, MSGLEN) < 0); |
| 216 | mbedtls_test_mock_socket_close(&socket); |
Yanray Wang | 5f86a42 | 2023-03-15 16:02:29 +0800 | [diff] [blame] | 217 | mbedtls_test_mock_socket_init(&socket); |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 218 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_b(&socket, received, MSGLEN) < 0); |
| 219 | mbedtls_test_mock_socket_close(&socket); |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 220 | |
Yanray Wang | 5f86a42 | 2023-03-15 16:02:29 +0800 | [diff] [blame] | 221 | mbedtls_test_mock_socket_init(&socket); |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 222 | TEST_ASSERT(mbedtls_test_mock_tcp_send_nb(&socket, message, MSGLEN) < 0); |
| 223 | mbedtls_test_mock_socket_close(&socket); |
Yanray Wang | 5f86a42 | 2023-03-15 16:02:29 +0800 | [diff] [blame] | 224 | mbedtls_test_mock_socket_init(&socket); |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 225 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_nb(&socket, received, MSGLEN) < 0); |
| 226 | mbedtls_test_mock_socket_close(&socket); |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 227 | |
| 228 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 229 | mbedtls_test_mock_socket_close(&socket); |
Valerio Setti | 285dae8 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 230 | USE_PSA_DONE(); |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 231 | } |
| 232 | /* END_CASE */ |
| 233 | |
| 234 | /* |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 235 | * Test if the implementation of `mbedtls_test_mock_socket` related functions |
| 236 | * can send a single message from the client to the server. |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 237 | */ |
| 238 | |
| 239 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 240 | void ssl_mock_tcp(int blocking) |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 241 | { |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 242 | enum { MSGLEN = 105 }; |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 243 | enum { BUFLEN = MSGLEN / 5 }; |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 244 | unsigned char message[MSGLEN]; |
| 245 | unsigned char received[MSGLEN]; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 246 | mbedtls_test_mock_socket client; |
| 247 | mbedtls_test_mock_socket server; |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 248 | size_t written, read; |
| 249 | int send_ret, recv_ret; |
| 250 | mbedtls_ssl_send_t *send; |
| 251 | mbedtls_ssl_recv_t *recv; |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 252 | unsigned i; |
| 253 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 254 | if (blocking == 0) { |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 255 | send = mbedtls_test_mock_tcp_send_nb; |
| 256 | recv = mbedtls_test_mock_tcp_recv_nb; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 257 | } else { |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 258 | send = mbedtls_test_mock_tcp_send_b; |
| 259 | recv = mbedtls_test_mock_tcp_recv_b; |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Yanray Wang | 5f86a42 | 2023-03-15 16:02:29 +0800 | [diff] [blame] | 262 | mbedtls_test_mock_socket_init(&client); |
| 263 | mbedtls_test_mock_socket_init(&server); |
Valerio Setti | 00a256f | 2023-04-27 17:22:27 +0200 | [diff] [blame] | 264 | USE_PSA_INIT(); |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 265 | |
| 266 | /* Fill up the buffer with structured data so that unwanted changes |
| 267 | * can be detected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 268 | for (i = 0; i < MSGLEN; i++) { |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 269 | message[i] = i & 0xFF; |
| 270 | } |
| 271 | |
| 272 | /* Make sure that sending a message takes a few iterations. */ |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 273 | TEST_ASSERT(0 == mbedtls_test_mock_socket_connect(&client, &server, |
| 274 | BUFLEN)); |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 275 | |
| 276 | /* Send the message to the server */ |
| 277 | send_ret = recv_ret = 1; |
| 278 | written = read = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 279 | while (send_ret != 0 || recv_ret != 0) { |
| 280 | send_ret = send(&client, message + written, MSGLEN - written); |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 281 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 282 | TEST_ASSERT(send_ret >= 0); |
| 283 | TEST_ASSERT(send_ret <= BUFLEN); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 284 | written += send_ret; |
| 285 | |
| 286 | /* If the buffer is full we can test blocking and non-blocking send */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 287 | if (send_ret == BUFLEN) { |
| 288 | int blocking_ret = send(&client, message, 1); |
| 289 | if (blocking) { |
| 290 | TEST_ASSERT(blocking_ret == 0); |
| 291 | } else { |
| 292 | TEST_ASSERT(blocking_ret == MBEDTLS_ERR_SSL_WANT_WRITE); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 293 | } |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 294 | } |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 295 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 296 | recv_ret = recv(&server, received + read, MSGLEN - read); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 297 | |
| 298 | /* The result depends on whether any data was sent */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 299 | if (send_ret > 0) { |
| 300 | TEST_ASSERT(recv_ret > 0); |
| 301 | TEST_ASSERT(recv_ret <= BUFLEN); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 302 | read += recv_ret; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 303 | } else if (blocking) { |
| 304 | TEST_ASSERT(recv_ret == 0); |
| 305 | } else { |
| 306 | TEST_ASSERT(recv_ret == MBEDTLS_ERR_SSL_WANT_READ); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 307 | recv_ret = 0; |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 308 | } |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 309 | |
| 310 | /* If the buffer is empty we can test blocking and non-blocking read */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 311 | if (recv_ret == BUFLEN) { |
| 312 | int blocking_ret = recv(&server, received, 1); |
| 313 | if (blocking) { |
| 314 | TEST_ASSERT(blocking_ret == 0); |
| 315 | } else { |
| 316 | TEST_ASSERT(blocking_ret == MBEDTLS_ERR_SSL_WANT_READ); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 317 | } |
| 318 | } |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 319 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 320 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 321 | |
| 322 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 323 | mbedtls_test_mock_socket_close(&client); |
| 324 | mbedtls_test_mock_socket_close(&server); |
Valerio Setti | 285dae8 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 325 | USE_PSA_DONE(); |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 326 | } |
| 327 | /* END_CASE */ |
| 328 | |
| 329 | /* |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 330 | * Test if the implementation of `mbedtls_test_mock_socket` related functions |
| 331 | * 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] | 332 | * interleaving). |
| 333 | */ |
| 334 | |
| 335 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 336 | void ssl_mock_tcp_interleaving(int blocking) |
Janos Follath | c673c2c | 2019-12-02 15:47:26 +0000 | [diff] [blame] | 337 | { |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 338 | enum { ROUNDS = 2 }; |
| 339 | enum { MSGLEN = 105 }; |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 340 | enum { BUFLEN = MSGLEN / 5 }; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 341 | unsigned char message[ROUNDS][MSGLEN]; |
| 342 | unsigned char received[ROUNDS][MSGLEN]; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 343 | mbedtls_test_mock_socket client; |
| 344 | mbedtls_test_mock_socket server; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 345 | size_t written[ROUNDS]; |
| 346 | size_t read[ROUNDS]; |
| 347 | int send_ret[ROUNDS]; |
| 348 | int recv_ret[ROUNDS]; |
| 349 | unsigned i, j, progress; |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 350 | mbedtls_ssl_send_t *send; |
| 351 | mbedtls_ssl_recv_t *recv; |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 352 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 353 | if (blocking == 0) { |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 354 | send = mbedtls_test_mock_tcp_send_nb; |
| 355 | recv = mbedtls_test_mock_tcp_recv_nb; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 356 | } else { |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 357 | send = mbedtls_test_mock_tcp_send_b; |
| 358 | recv = mbedtls_test_mock_tcp_recv_b; |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 359 | } |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 360 | |
Yanray Wang | 5f86a42 | 2023-03-15 16:02:29 +0800 | [diff] [blame] | 361 | mbedtls_test_mock_socket_init(&client); |
| 362 | mbedtls_test_mock_socket_init(&server); |
Valerio Setti | 00a256f | 2023-04-27 17:22:27 +0200 | [diff] [blame] | 363 | USE_PSA_INIT(); |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 364 | |
| 365 | /* Fill up the buffers with structured data so that unwanted changes |
| 366 | * can be detected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 367 | for (i = 0; i < ROUNDS; i++) { |
| 368 | for (j = 0; j < MSGLEN; j++) { |
| 369 | message[i][j] = (i * MSGLEN + j) & 0xFF; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 370 | } |
| 371 | } |
| 372 | |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 373 | /* Make sure that sending a message takes a few iterations. */ |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 374 | TEST_ASSERT(0 == mbedtls_test_mock_socket_connect(&client, &server, |
| 375 | BUFLEN)); |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 376 | |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 377 | /* Send the message from both sides, interleaving. */ |
| 378 | progress = 1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 379 | for (i = 0; i < ROUNDS; i++) { |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 380 | written[i] = 0; |
| 381 | read[i] = 0; |
| 382 | } |
| 383 | /* This loop does not stop as long as there was a successful write or read |
| 384 | * of at least one byte on either side. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 385 | while (progress != 0) { |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 386 | mbedtls_test_mock_socket *socket; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 387 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 388 | for (i = 0; i < ROUNDS; i++) { |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 389 | /* First sending is from the client */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 390 | socket = (i % 2 == 0) ? (&client) : (&server); |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 391 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 392 | send_ret[i] = send(socket, message[i] + written[i], |
| 393 | MSGLEN - written[i]); |
| 394 | TEST_ASSERT(send_ret[i] >= 0); |
| 395 | TEST_ASSERT(send_ret[i] <= BUFLEN); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 396 | written[i] += send_ret[i]; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 397 | |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 398 | /* If the buffer is full we can test blocking and non-blocking |
| 399 | * send */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 400 | if (send_ret[i] == BUFLEN) { |
| 401 | int blocking_ret = send(socket, message[i], 1); |
| 402 | if (blocking) { |
| 403 | TEST_ASSERT(blocking_ret == 0); |
| 404 | } else { |
| 405 | TEST_ASSERT(blocking_ret == MBEDTLS_ERR_SSL_WANT_WRITE); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 406 | } |
| 407 | } |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 408 | } |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 409 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 410 | for (i = 0; i < ROUNDS; i++) { |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 411 | /* First receiving is from the server */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 412 | socket = (i % 2 == 0) ? (&server) : (&client); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 413 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 414 | recv_ret[i] = recv(socket, received[i] + read[i], |
| 415 | MSGLEN - read[i]); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 416 | |
| 417 | /* The result depends on whether any data was sent */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 418 | if (send_ret[i] > 0) { |
| 419 | TEST_ASSERT(recv_ret[i] > 0); |
| 420 | TEST_ASSERT(recv_ret[i] <= BUFLEN); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 421 | read[i] += recv_ret[i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 422 | } else if (blocking) { |
| 423 | TEST_ASSERT(recv_ret[i] == 0); |
| 424 | } else { |
| 425 | TEST_ASSERT(recv_ret[i] == MBEDTLS_ERR_SSL_WANT_READ); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 426 | recv_ret[i] = 0; |
| 427 | } |
| 428 | |
| 429 | /* If the buffer is empty we can test blocking and non-blocking |
| 430 | * read */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 431 | if (recv_ret[i] == BUFLEN) { |
| 432 | int blocking_ret = recv(socket, received[i], 1); |
| 433 | if (blocking) { |
| 434 | TEST_ASSERT(blocking_ret == 0); |
| 435 | } else { |
| 436 | TEST_ASSERT(blocking_ret == MBEDTLS_ERR_SSL_WANT_READ); |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 437 | } |
| 438 | } |
Janos Follath | 3766ba5 | 2019-11-27 13:31:42 +0000 | [diff] [blame] | 439 | } |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 440 | |
| 441 | progress = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 442 | for (i = 0; i < ROUNDS; i++) { |
Piotr Nowicki | 890b5ca | 2020-01-15 16:19:07 +0100 | [diff] [blame] | 443 | progress += send_ret[i] + recv_ret[i]; |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 444 | } |
| 445 | } |
| 446 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 447 | for (i = 0; i < ROUNDS; i++) { |
| 448 | TEST_ASSERT(memcmp(message[i], received[i], MSGLEN) == 0); |
| 449 | } |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 450 | |
| 451 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 452 | mbedtls_test_mock_socket_close(&client); |
| 453 | mbedtls_test_mock_socket_close(&server); |
Valerio Setti | 285dae8 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 454 | USE_PSA_DONE(); |
Janos Follath | 031827f | 2019-11-27 11:12:14 +0000 | [diff] [blame] | 455 | } |
| 456 | /* END_CASE */ |
| 457 | |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 458 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 459 | void ssl_message_queue_sanity() |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 460 | { |
Valerio Setti | 7c0f91b | 2023-04-28 12:20:34 +0200 | [diff] [blame] | 461 | mbedtls_test_ssl_message_queue queue = SSL_MESSAGE_QUEUE_INIT; |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 462 | |
Valerio Setti | 285dae8 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 463 | USE_PSA_INIT(); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 464 | /* Trying to push/pull to an empty queue */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 465 | TEST_ASSERT(mbedtls_test_ssl_message_queue_push_info(NULL, 1) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 466 | == MBEDTLS_TEST_ERROR_ARG_NULL); |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 467 | TEST_ASSERT(mbedtls_test_ssl_message_queue_pop_info(NULL, 1) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 468 | == MBEDTLS_TEST_ERROR_ARG_NULL); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 469 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 470 | TEST_ASSERT(mbedtls_test_ssl_message_queue_setup(&queue, 3) == 0); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 471 | TEST_ASSERT(queue.capacity == 3); |
| 472 | TEST_ASSERT(queue.num == 0); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 473 | |
| 474 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 475 | mbedtls_test_ssl_message_queue_free(&queue); |
Valerio Setti | 285dae8 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 476 | USE_PSA_DONE(); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 477 | } |
| 478 | /* END_CASE */ |
| 479 | |
| 480 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 481 | void ssl_message_queue_basic() |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 482 | { |
Valerio Setti | 7c0f91b | 2023-04-28 12:20:34 +0200 | [diff] [blame] | 483 | mbedtls_test_ssl_message_queue queue = SSL_MESSAGE_QUEUE_INIT; |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 484 | |
Valerio Setti | 285dae8 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 485 | USE_PSA_INIT(); |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 486 | TEST_ASSERT(mbedtls_test_ssl_message_queue_setup(&queue, 3) == 0); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 487 | |
| 488 | /* Sanity test - 3 pushes and 3 pops with sufficient space */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 489 | TEST_ASSERT(mbedtls_test_ssl_message_queue_push_info(&queue, 1) == 1); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 490 | TEST_ASSERT(queue.capacity == 3); |
| 491 | TEST_ASSERT(queue.num == 1); |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 492 | TEST_ASSERT(mbedtls_test_ssl_message_queue_push_info(&queue, 1) == 1); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 493 | TEST_ASSERT(queue.capacity == 3); |
| 494 | TEST_ASSERT(queue.num == 2); |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 495 | TEST_ASSERT(mbedtls_test_ssl_message_queue_push_info(&queue, 2) == 2); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 496 | TEST_ASSERT(queue.capacity == 3); |
| 497 | TEST_ASSERT(queue.num == 3); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 498 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 499 | TEST_ASSERT(mbedtls_test_ssl_message_queue_pop_info(&queue, 1) == 1); |
| 500 | TEST_ASSERT(mbedtls_test_ssl_message_queue_pop_info(&queue, 1) == 1); |
| 501 | TEST_ASSERT(mbedtls_test_ssl_message_queue_pop_info(&queue, 2) == 2); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 502 | |
| 503 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 504 | mbedtls_test_ssl_message_queue_free(&queue); |
Valerio Setti | 285dae8 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 505 | USE_PSA_DONE(); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 506 | } |
| 507 | /* END_CASE */ |
| 508 | |
| 509 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 510 | void ssl_message_queue_overflow_underflow() |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 511 | { |
Valerio Setti | 7c0f91b | 2023-04-28 12:20:34 +0200 | [diff] [blame] | 512 | mbedtls_test_ssl_message_queue queue = SSL_MESSAGE_QUEUE_INIT; |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 513 | |
Valerio Setti | 285dae8 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 514 | USE_PSA_INIT(); |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 515 | TEST_ASSERT(mbedtls_test_ssl_message_queue_setup(&queue, 3) == 0); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 516 | |
| 517 | /* 4 pushes (last one with an error), 4 pops (last one with an error) */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 518 | TEST_ASSERT(mbedtls_test_ssl_message_queue_push_info(&queue, 1) == 1); |
| 519 | TEST_ASSERT(mbedtls_test_ssl_message_queue_push_info(&queue, 1) == 1); |
| 520 | TEST_ASSERT(mbedtls_test_ssl_message_queue_push_info(&queue, 2) == 2); |
| 521 | TEST_ASSERT(mbedtls_test_ssl_message_queue_push_info(&queue, 3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 522 | == MBEDTLS_ERR_SSL_WANT_WRITE); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 523 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 524 | TEST_ASSERT(mbedtls_test_ssl_message_queue_pop_info(&queue, 1) == 1); |
| 525 | TEST_ASSERT(mbedtls_test_ssl_message_queue_pop_info(&queue, 1) == 1); |
| 526 | TEST_ASSERT(mbedtls_test_ssl_message_queue_pop_info(&queue, 2) == 2); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 527 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 528 | TEST_ASSERT(mbedtls_test_ssl_message_queue_pop_info(&queue, 1) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 529 | == MBEDTLS_ERR_SSL_WANT_READ); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 530 | |
| 531 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 532 | mbedtls_test_ssl_message_queue_free(&queue); |
Valerio Setti | 285dae8 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 533 | USE_PSA_DONE(); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 534 | } |
| 535 | /* END_CASE */ |
| 536 | |
| 537 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 538 | void ssl_message_queue_interleaved() |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 539 | { |
Valerio Setti | 7c0f91b | 2023-04-28 12:20:34 +0200 | [diff] [blame] | 540 | mbedtls_test_ssl_message_queue queue = SSL_MESSAGE_QUEUE_INIT; |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 541 | |
Valerio Setti | 285dae8 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 542 | USE_PSA_INIT(); |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 543 | TEST_ASSERT(mbedtls_test_ssl_message_queue_setup(&queue, 3) == 0); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 544 | |
| 545 | /* Interleaved test - [2 pushes, 1 pop] twice, and then two pops |
| 546 | * (to wrap around the buffer) */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 547 | TEST_ASSERT(mbedtls_test_ssl_message_queue_push_info(&queue, 1) == 1); |
| 548 | TEST_ASSERT(mbedtls_test_ssl_message_queue_push_info(&queue, 1) == 1); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 549 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 550 | TEST_ASSERT(mbedtls_test_ssl_message_queue_pop_info(&queue, 1) == 1); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 551 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 552 | TEST_ASSERT(mbedtls_test_ssl_message_queue_push_info(&queue, 2) == 2); |
| 553 | TEST_ASSERT(mbedtls_test_ssl_message_queue_push_info(&queue, 3) == 3); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 554 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 555 | TEST_ASSERT(mbedtls_test_ssl_message_queue_pop_info(&queue, 1) == 1); |
| 556 | TEST_ASSERT(mbedtls_test_ssl_message_queue_pop_info(&queue, 2) == 2); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 557 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 558 | TEST_ASSERT(mbedtls_test_ssl_message_queue_push_info(&queue, 5) == 5); |
| 559 | TEST_ASSERT(mbedtls_test_ssl_message_queue_push_info(&queue, 8) == 8); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 560 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 561 | TEST_ASSERT(mbedtls_test_ssl_message_queue_pop_info(&queue, 3) == 3); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 562 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 563 | TEST_ASSERT(mbedtls_test_ssl_message_queue_pop_info(&queue, 5) == 5); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 564 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 565 | TEST_ASSERT(mbedtls_test_ssl_message_queue_pop_info(&queue, 8) == 8); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 566 | |
| 567 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 568 | mbedtls_test_ssl_message_queue_free(&queue); |
Valerio Setti | 285dae8 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 569 | USE_PSA_DONE(); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 570 | } |
| 571 | /* END_CASE */ |
| 572 | |
| 573 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 574 | void ssl_message_queue_insufficient_buffer() |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 575 | { |
Valerio Setti | 7c0f91b | 2023-04-28 12:20:34 +0200 | [diff] [blame] | 576 | mbedtls_test_ssl_message_queue queue = SSL_MESSAGE_QUEUE_INIT; |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 577 | size_t message_len = 10; |
| 578 | size_t buffer_len = 5; |
| 579 | |
Valerio Setti | 285dae8 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 580 | USE_PSA_INIT(); |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 581 | TEST_ASSERT(mbedtls_test_ssl_message_queue_setup(&queue, 1) == 0); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 582 | |
| 583 | /* Popping without a sufficient buffer */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 584 | TEST_ASSERT(mbedtls_test_ssl_message_queue_push_info(&queue, message_len) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 585 | == (int) message_len); |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 586 | TEST_ASSERT(mbedtls_test_ssl_message_queue_pop_info(&queue, buffer_len) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 587 | == (int) buffer_len); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 588 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 589 | mbedtls_test_ssl_message_queue_free(&queue); |
Valerio Setti | 285dae8 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 590 | USE_PSA_DONE(); |
Andrzej Kurek | 13719cd | 2020-01-22 06:36:39 -0500 | [diff] [blame] | 591 | } |
| 592 | /* END_CASE */ |
| 593 | |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 594 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 595 | void ssl_message_mock_uninitialized() |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 596 | { |
| 597 | enum { MSGLEN = 10 }; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 598 | unsigned char message[MSGLEN] = { 0 }, received[MSGLEN]; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 599 | mbedtls_test_mock_socket client, server; |
| 600 | mbedtls_test_ssl_message_queue server_queue, client_queue; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 601 | mbedtls_test_message_socket_context server_context, client_context; |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 602 | mbedtls_test_message_socket_init(&server_context); |
| 603 | mbedtls_test_message_socket_init(&client_context); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 604 | |
Valerio Setti | 285dae8 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 605 | USE_PSA_INIT(); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 606 | /* Send with a NULL context */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 607 | TEST_ASSERT(mbedtls_test_mock_tcp_send_msg(NULL, message, MSGLEN) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 608 | == MBEDTLS_TEST_ERROR_CONTEXT_ERROR); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 609 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 610 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(NULL, message, MSGLEN) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 611 | == MBEDTLS_TEST_ERROR_CONTEXT_ERROR); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 612 | |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 613 | TEST_ASSERT(mbedtls_test_message_socket_setup(&server_queue, |
| 614 | &client_queue, 1, |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 615 | &server, |
| 616 | &server_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 617 | |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 618 | TEST_ASSERT(mbedtls_test_message_socket_setup(&client_queue, |
| 619 | &server_queue, 1, |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 620 | &client, |
| 621 | &client_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 622 | |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 623 | TEST_ASSERT(mbedtls_test_mock_tcp_send_msg(&client_context, message, |
| 624 | MSGLEN) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 625 | == MBEDTLS_TEST_ERROR_SEND_FAILED); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 626 | |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 627 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&server_context, received, |
| 628 | MSGLEN) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 629 | == MBEDTLS_ERR_SSL_WANT_READ); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 630 | |
| 631 | /* Push directly to a queue to later simulate a disconnected behavior */ |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 632 | TEST_ASSERT(mbedtls_test_ssl_message_queue_push_info(&server_queue, |
| 633 | MSGLEN) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 634 | == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 635 | |
| 636 | /* Test if there's an error when trying to read from a disconnected |
| 637 | * socket */ |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 638 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&server_context, received, |
| 639 | MSGLEN) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 640 | == MBEDTLS_TEST_ERROR_RECV_FAILED); |
| 641 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 642 | mbedtls_test_message_socket_close(&server_context); |
| 643 | mbedtls_test_message_socket_close(&client_context); |
Valerio Setti | 285dae8 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 644 | USE_PSA_DONE(); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 645 | } |
| 646 | /* END_CASE */ |
| 647 | |
| 648 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 649 | void ssl_message_mock_basic() |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 650 | { |
| 651 | enum { MSGLEN = 10 }; |
| 652 | unsigned char message[MSGLEN], received[MSGLEN]; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 653 | mbedtls_test_mock_socket client, server; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 654 | unsigned i; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 655 | mbedtls_test_ssl_message_queue server_queue, client_queue; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 656 | mbedtls_test_message_socket_context server_context, client_context; |
Valerio Setti | 285dae8 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 657 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 658 | mbedtls_test_message_socket_init(&server_context); |
| 659 | mbedtls_test_message_socket_init(&client_context); |
Valerio Setti | 00a256f | 2023-04-27 17:22:27 +0200 | [diff] [blame] | 660 | USE_PSA_INIT(); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 661 | |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 662 | TEST_ASSERT(mbedtls_test_message_socket_setup(&server_queue, |
| 663 | &client_queue, 1, |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 664 | &server, |
| 665 | &server_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 666 | |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 667 | TEST_ASSERT(mbedtls_test_message_socket_setup(&client_queue, |
| 668 | &server_queue, 1, |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 669 | &client, |
| 670 | &client_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 671 | |
| 672 | /* Fill up the buffer with structured data so that unwanted changes |
| 673 | * can be detected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 674 | for (i = 0; i < MSGLEN; i++) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 675 | message[i] = i & 0xFF; |
| 676 | } |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 677 | TEST_ASSERT(0 == mbedtls_test_mock_socket_connect(&client, &server, |
| 678 | MSGLEN)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 679 | |
| 680 | /* Send the message to the server */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 681 | TEST_ASSERT(mbedtls_test_mock_tcp_send_msg(&client_context, message, |
| 682 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 683 | |
| 684 | /* Read from the server */ |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 685 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&server_context, received, |
| 686 | MSGLEN) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 687 | == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 688 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 689 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
| 690 | memset(received, 0, MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 691 | |
| 692 | /* Send the message to the client */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 693 | TEST_ASSERT(mbedtls_test_mock_tcp_send_msg(&server_context, message, |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 694 | MSGLEN) |
| 695 | == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 696 | |
| 697 | /* Read from the client */ |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 698 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&client_context, received, |
| 699 | MSGLEN) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 700 | == MSGLEN); |
| 701 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 702 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 703 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 704 | mbedtls_test_message_socket_close(&server_context); |
| 705 | mbedtls_test_message_socket_close(&client_context); |
Valerio Setti | 285dae8 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 706 | USE_PSA_DONE(); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 707 | } |
| 708 | /* END_CASE */ |
| 709 | |
| 710 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 711 | void ssl_message_mock_queue_overflow_underflow() |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 712 | { |
| 713 | enum { MSGLEN = 10 }; |
| 714 | unsigned char message[MSGLEN], received[MSGLEN]; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 715 | mbedtls_test_mock_socket client, server; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 716 | unsigned i; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 717 | mbedtls_test_ssl_message_queue server_queue, client_queue; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 718 | mbedtls_test_message_socket_context server_context, client_context; |
Valerio Setti | 285dae8 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 719 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 720 | mbedtls_test_message_socket_init(&server_context); |
| 721 | mbedtls_test_message_socket_init(&client_context); |
Valerio Setti | 00a256f | 2023-04-27 17:22:27 +0200 | [diff] [blame] | 722 | USE_PSA_INIT(); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 723 | |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 724 | TEST_ASSERT(mbedtls_test_message_socket_setup(&server_queue, |
| 725 | &client_queue, 2, |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 726 | &server, |
| 727 | &server_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 728 | |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 729 | TEST_ASSERT(mbedtls_test_message_socket_setup(&client_queue, |
| 730 | &server_queue, 2, |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 731 | &client, |
| 732 | &client_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 733 | |
| 734 | /* Fill up the buffer with structured data so that unwanted changes |
| 735 | * can be detected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 736 | for (i = 0; i < MSGLEN; i++) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 737 | message[i] = i & 0xFF; |
| 738 | } |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 739 | TEST_ASSERT(0 == mbedtls_test_mock_socket_connect(&client, &server, |
| 740 | MSGLEN*2)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 741 | |
| 742 | /* Send three message to the server, last one with an error */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 743 | TEST_ASSERT(mbedtls_test_mock_tcp_send_msg(&client_context, message, |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 744 | MSGLEN - 1) |
| 745 | == MSGLEN - 1); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 746 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 747 | TEST_ASSERT(mbedtls_test_mock_tcp_send_msg(&client_context, message, |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 748 | MSGLEN) |
| 749 | == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 750 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 751 | TEST_ASSERT(mbedtls_test_mock_tcp_send_msg(&client_context, message, |
| 752 | MSGLEN) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 753 | == MBEDTLS_ERR_SSL_WANT_WRITE); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 754 | |
| 755 | /* Read three messages from the server, last one with an error */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 756 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&server_context, received, |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 757 | MSGLEN - 1) |
| 758 | == MSGLEN - 1); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 759 | |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 760 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&server_context, received, |
| 761 | MSGLEN) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 762 | == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 763 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 764 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 765 | |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 766 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&server_context, received, |
| 767 | MSGLEN) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 768 | == MBEDTLS_ERR_SSL_WANT_READ); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 769 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 770 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 771 | mbedtls_test_message_socket_close(&server_context); |
| 772 | mbedtls_test_message_socket_close(&client_context); |
Valerio Setti | 285dae8 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 773 | USE_PSA_DONE(); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 774 | } |
| 775 | /* END_CASE */ |
| 776 | |
| 777 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 778 | void ssl_message_mock_socket_overflow() |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 779 | { |
| 780 | enum { MSGLEN = 10 }; |
| 781 | unsigned char message[MSGLEN], received[MSGLEN]; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 782 | mbedtls_test_mock_socket client, server; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 783 | unsigned i; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 784 | mbedtls_test_ssl_message_queue server_queue, client_queue; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 785 | mbedtls_test_message_socket_context server_context, client_context; |
Valerio Setti | 285dae8 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 786 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 787 | mbedtls_test_message_socket_init(&server_context); |
| 788 | mbedtls_test_message_socket_init(&client_context); |
Valerio Setti | 00a256f | 2023-04-27 17:22:27 +0200 | [diff] [blame] | 789 | USE_PSA_INIT(); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 790 | |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 791 | TEST_ASSERT(mbedtls_test_message_socket_setup(&server_queue, |
| 792 | &client_queue, 2, |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 793 | &server, |
| 794 | &server_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 795 | |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 796 | TEST_ASSERT(mbedtls_test_message_socket_setup(&client_queue, |
| 797 | &server_queue, 2, |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 798 | &client, |
| 799 | &client_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 800 | |
| 801 | /* Fill up the buffer with structured data so that unwanted changes |
| 802 | * can be detected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 803 | for (i = 0; i < MSGLEN; i++) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 804 | message[i] = i & 0xFF; |
| 805 | } |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 806 | TEST_ASSERT(0 == mbedtls_test_mock_socket_connect(&client, &server, |
| 807 | MSGLEN)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 808 | |
| 809 | /* Send two message to the server, second one with an error */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 810 | TEST_ASSERT(mbedtls_test_mock_tcp_send_msg(&client_context, message, |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 811 | MSGLEN) |
| 812 | == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 813 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 814 | TEST_ASSERT(mbedtls_test_mock_tcp_send_msg(&client_context, message, |
| 815 | MSGLEN) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 816 | == MBEDTLS_TEST_ERROR_SEND_FAILED); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 817 | |
| 818 | /* Read the only message from the server */ |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 819 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&server_context, received, |
| 820 | MSGLEN) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 821 | == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 822 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 823 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 824 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 825 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 826 | mbedtls_test_message_socket_close(&server_context); |
| 827 | mbedtls_test_message_socket_close(&client_context); |
Valerio Setti | 285dae8 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 828 | USE_PSA_DONE(); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 829 | } |
| 830 | /* END_CASE */ |
| 831 | |
| 832 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 833 | void ssl_message_mock_truncated() |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 834 | { |
| 835 | enum { MSGLEN = 10 }; |
| 836 | unsigned char message[MSGLEN], received[MSGLEN]; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 837 | mbedtls_test_mock_socket client, server; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 838 | unsigned i; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 839 | mbedtls_test_ssl_message_queue server_queue, client_queue; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 840 | mbedtls_test_message_socket_context server_context, client_context; |
Valerio Setti | 285dae8 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 841 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 842 | mbedtls_test_message_socket_init(&server_context); |
| 843 | mbedtls_test_message_socket_init(&client_context); |
Valerio Setti | 00a256f | 2023-04-27 17:22:27 +0200 | [diff] [blame] | 844 | USE_PSA_INIT(); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 845 | |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 846 | TEST_ASSERT(mbedtls_test_message_socket_setup(&server_queue, |
| 847 | &client_queue, 2, |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 848 | &server, |
| 849 | &server_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 850 | |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 851 | TEST_ASSERT(mbedtls_test_message_socket_setup(&client_queue, |
| 852 | &server_queue, 2, |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 853 | &client, |
| 854 | &client_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 855 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 856 | memset(received, 0, MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 857 | /* Fill up the buffer with structured data so that unwanted changes |
| 858 | * can be detected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 859 | for (i = 0; i < MSGLEN; i++) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 860 | message[i] = i & 0xFF; |
| 861 | } |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 862 | TEST_ASSERT(0 == mbedtls_test_mock_socket_connect(&client, &server, |
| 863 | 2 * MSGLEN)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 864 | |
| 865 | /* Send two messages to the server, the second one small enough to fit in the |
| 866 | * receiver's buffer. */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 867 | TEST_ASSERT(mbedtls_test_mock_tcp_send_msg(&client_context, message, |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 868 | MSGLEN) |
| 869 | == MSGLEN); |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 870 | TEST_ASSERT(mbedtls_test_mock_tcp_send_msg(&client_context, message, |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 871 | MSGLEN / 2) |
| 872 | == MSGLEN / 2); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 873 | /* Read a truncated message from the server */ |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 874 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&server_context, received, |
| 875 | MSGLEN/2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 876 | == MSGLEN/2); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 877 | |
| 878 | /* Test that the first half of the message is valid, and second one isn't */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 879 | TEST_ASSERT(memcmp(message, received, MSGLEN/2) == 0); |
| 880 | TEST_ASSERT(memcmp(message + MSGLEN/2, received + MSGLEN/2, MSGLEN/2) |
| 881 | != 0); |
| 882 | memset(received, 0, MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 883 | |
| 884 | /* Read a full message from the server */ |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 885 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&server_context, received, |
| 886 | MSGLEN/2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 887 | == MSGLEN / 2); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 888 | |
| 889 | /* Test that the first half of the message is valid */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 890 | TEST_ASSERT(memcmp(message, received, MSGLEN/2) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 891 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 892 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 893 | mbedtls_test_message_socket_close(&server_context); |
| 894 | mbedtls_test_message_socket_close(&client_context); |
Valerio Setti | 285dae8 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 895 | USE_PSA_DONE(); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 896 | } |
| 897 | /* END_CASE */ |
| 898 | |
| 899 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 900 | void ssl_message_mock_socket_read_error() |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 901 | { |
| 902 | enum { MSGLEN = 10 }; |
| 903 | unsigned char message[MSGLEN], received[MSGLEN]; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 904 | mbedtls_test_mock_socket client, server; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 905 | unsigned i; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 906 | mbedtls_test_ssl_message_queue server_queue, client_queue; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 907 | mbedtls_test_message_socket_context server_context, client_context; |
Valerio Setti | 285dae8 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 908 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 909 | mbedtls_test_message_socket_init(&server_context); |
| 910 | mbedtls_test_message_socket_init(&client_context); |
Valerio Setti | 00a256f | 2023-04-27 17:22:27 +0200 | [diff] [blame] | 911 | USE_PSA_INIT(); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 912 | |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 913 | TEST_ASSERT(mbedtls_test_message_socket_setup(&server_queue, |
| 914 | &client_queue, 1, |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 915 | &server, |
| 916 | &server_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 917 | |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 918 | TEST_ASSERT(mbedtls_test_message_socket_setup(&client_queue, |
| 919 | &server_queue, 1, |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 920 | &client, |
| 921 | &client_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 922 | |
| 923 | /* Fill up the buffer with structured data so that unwanted changes |
| 924 | * can be detected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 925 | for (i = 0; i < MSGLEN; i++) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 926 | message[i] = i & 0xFF; |
| 927 | } |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 928 | TEST_ASSERT(0 == mbedtls_test_mock_socket_connect(&client, &server, |
| 929 | MSGLEN)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 930 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 931 | TEST_ASSERT(mbedtls_test_mock_tcp_send_msg(&client_context, message, |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 932 | MSGLEN) |
| 933 | == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 934 | |
| 935 | /* Force a read error by disconnecting the socket by hand */ |
| 936 | server.status = 0; |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 937 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&server_context, received, |
| 938 | MSGLEN) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 939 | == MBEDTLS_TEST_ERROR_RECV_FAILED); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 940 | /* Return to a valid state */ |
| 941 | server.status = MBEDTLS_MOCK_SOCKET_CONNECTED; |
| 942 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 943 | memset(received, 0, sizeof(received)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 944 | |
| 945 | /* Test that even though the server tried to read once disconnected, the |
| 946 | * continuity is preserved */ |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 947 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&server_context, received, |
| 948 | MSGLEN) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 949 | == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 950 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 951 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 952 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 953 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 954 | mbedtls_test_message_socket_close(&server_context); |
| 955 | mbedtls_test_message_socket_close(&client_context); |
Valerio Setti | 285dae8 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 956 | USE_PSA_DONE(); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 957 | } |
| 958 | /* END_CASE */ |
| 959 | |
| 960 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 961 | void ssl_message_mock_interleaved_one_way() |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 962 | { |
| 963 | enum { MSGLEN = 10 }; |
| 964 | unsigned char message[MSGLEN], received[MSGLEN]; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 965 | mbedtls_test_mock_socket client, server; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 966 | unsigned i; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 967 | mbedtls_test_ssl_message_queue server_queue, client_queue; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 968 | mbedtls_test_message_socket_context server_context, client_context; |
Valerio Setti | 285dae8 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 969 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 970 | mbedtls_test_message_socket_init(&server_context); |
| 971 | mbedtls_test_message_socket_init(&client_context); |
Valerio Setti | 00a256f | 2023-04-27 17:22:27 +0200 | [diff] [blame] | 972 | USE_PSA_INIT(); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 973 | |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 974 | TEST_ASSERT(mbedtls_test_message_socket_setup(&server_queue, |
| 975 | &client_queue, 3, |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 976 | &server, |
| 977 | &server_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 978 | |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 979 | TEST_ASSERT(mbedtls_test_message_socket_setup(&client_queue, |
| 980 | &server_queue, 3, |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 981 | &client, |
| 982 | &client_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 983 | |
| 984 | /* Fill up the buffer with structured data so that unwanted changes |
| 985 | * can be detected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 986 | for (i = 0; i < MSGLEN; i++) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 987 | message[i] = i & 0xFF; |
| 988 | } |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 989 | TEST_ASSERT(0 == mbedtls_test_mock_socket_connect(&client, &server, |
| 990 | MSGLEN*3)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 991 | |
| 992 | /* Interleaved test - [2 sends, 1 read] twice, and then two reads |
| 993 | * (to wrap around the buffer) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 994 | for (i = 0; i < 2; i++) { |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 995 | TEST_ASSERT(mbedtls_test_mock_tcp_send_msg(&client_context, message, |
| 996 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 997 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 998 | TEST_ASSERT(mbedtls_test_mock_tcp_send_msg(&client_context, message, |
| 999 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 1000 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 1001 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&server_context, received, |
| 1002 | MSGLEN) == MSGLEN); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1003 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
| 1004 | memset(received, 0, sizeof(received)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 1005 | } |
| 1006 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1007 | for (i = 0; i < 2; i++) { |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 1008 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&server_context, received, |
| 1009 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 1010 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1011 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 1012 | } |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 1013 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&server_context, received, |
| 1014 | MSGLEN) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1015 | == MBEDTLS_ERR_SSL_WANT_READ); |
| 1016 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 1017 | mbedtls_test_message_socket_close(&server_context); |
| 1018 | mbedtls_test_message_socket_close(&client_context); |
Valerio Setti | 285dae8 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 1019 | USE_PSA_DONE(); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 1020 | } |
| 1021 | /* END_CASE */ |
| 1022 | |
| 1023 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1024 | void ssl_message_mock_interleaved_two_ways() |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 1025 | { |
| 1026 | enum { MSGLEN = 10 }; |
| 1027 | unsigned char message[MSGLEN], received[MSGLEN]; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 1028 | mbedtls_test_mock_socket client, server; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 1029 | unsigned i; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 1030 | mbedtls_test_ssl_message_queue server_queue, client_queue; |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 1031 | mbedtls_test_message_socket_context server_context, client_context; |
Valerio Setti | 285dae8 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 1032 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 1033 | mbedtls_test_message_socket_init(&server_context); |
| 1034 | mbedtls_test_message_socket_init(&client_context); |
Valerio Setti | 00a256f | 2023-04-27 17:22:27 +0200 | [diff] [blame] | 1035 | USE_PSA_INIT(); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 1036 | |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 1037 | TEST_ASSERT(mbedtls_test_message_socket_setup(&server_queue, |
| 1038 | &client_queue, 3, |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 1039 | &server, |
| 1040 | &server_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 1041 | |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 1042 | TEST_ASSERT(mbedtls_test_message_socket_setup(&client_queue, |
| 1043 | &server_queue, 3, |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 1044 | &client, |
| 1045 | &client_context) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 1046 | |
| 1047 | /* Fill up the buffer with structured data so that unwanted changes |
| 1048 | * can be detected */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1049 | for (i = 0; i < MSGLEN; i++) { |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 1050 | message[i] = i & 0xFF; |
| 1051 | } |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 1052 | TEST_ASSERT(0 == mbedtls_test_mock_socket_connect(&client, &server, |
| 1053 | MSGLEN*3)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 1054 | |
| 1055 | /* Interleaved test - [2 sends, 1 read] twice, both ways, and then two reads |
| 1056 | * (to wrap around the buffer) both ways. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1057 | for (i = 0; i < 2; i++) { |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 1058 | TEST_ASSERT(mbedtls_test_mock_tcp_send_msg(&client_context, message, |
| 1059 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 1060 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 1061 | TEST_ASSERT(mbedtls_test_mock_tcp_send_msg(&client_context, message, |
| 1062 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 1063 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 1064 | TEST_ASSERT(mbedtls_test_mock_tcp_send_msg(&server_context, message, |
| 1065 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 1066 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 1067 | TEST_ASSERT(mbedtls_test_mock_tcp_send_msg(&server_context, message, |
| 1068 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 1069 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 1070 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&server_context, received, |
| 1071 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 1072 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1073 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 1074 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1075 | memset(received, 0, sizeof(received)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 1076 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 1077 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&client_context, received, |
| 1078 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 1079 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1080 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 1081 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1082 | memset(received, 0, sizeof(received)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 1083 | } |
| 1084 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1085 | for (i = 0; i < 2; i++) { |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 1086 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&server_context, received, |
| 1087 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 1088 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1089 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
| 1090 | memset(received, 0, sizeof(received)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 1091 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 1092 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&client_context, received, |
| 1093 | MSGLEN) == MSGLEN); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 1094 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1095 | TEST_ASSERT(memcmp(message, received, MSGLEN) == 0); |
| 1096 | memset(received, 0, sizeof(received)); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 1097 | } |
| 1098 | |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 1099 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&server_context, received, |
| 1100 | MSGLEN) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1101 | == MBEDTLS_ERR_SSL_WANT_READ); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 1102 | |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 1103 | TEST_ASSERT(mbedtls_test_mock_tcp_recv_msg(&client_context, received, |
| 1104 | MSGLEN) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1105 | == MBEDTLS_ERR_SSL_WANT_READ); |
| 1106 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 1107 | mbedtls_test_message_socket_close(&server_context); |
| 1108 | mbedtls_test_message_socket_close(&client_context); |
Valerio Setti | 285dae8 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 1109 | USE_PSA_DONE(); |
Andrzej Kurek | bc483de | 2020-01-22 03:40:00 -0500 | [diff] [blame] | 1110 | } |
| 1111 | /* END_CASE */ |
| 1112 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1113 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_DTLS_ANTI_REPLAY */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1114 | 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] | 1115 | { |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 1116 | uint32_t len = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1117 | mbedtls_ssl_context ssl; |
Manuel Pégourié-Gonnard | def0bbe | 2015-05-04 14:56:36 +0200 | [diff] [blame] | 1118 | mbedtls_ssl_config conf; |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 1119 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1120 | mbedtls_ssl_init(&ssl); |
| 1121 | mbedtls_ssl_config_init(&conf); |
valerio | 32f2ac9 | 2023-04-20 11:59:52 +0200 | [diff] [blame] | 1122 | MD_OR_USE_PSA_INIT(); |
Manuel Pégourié-Gonnard | 41d479e | 2015-04-29 00:48:22 +0200 | [diff] [blame] | 1123 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1124 | TEST_ASSERT(mbedtls_ssl_config_defaults(&conf, |
| 1125 | MBEDTLS_SSL_IS_CLIENT, |
| 1126 | MBEDTLS_SSL_TRANSPORT_DATAGRAM, |
| 1127 | MBEDTLS_SSL_PRESET_DEFAULT) == 0); |
| 1128 | TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0); |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 1129 | |
| 1130 | /* Read previous record numbers */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1131 | for (len = 0; len < prevs->len; len += 6) { |
| 1132 | memcpy(ssl.in_ctr + 2, prevs->x + len, 6); |
| 1133 | mbedtls_ssl_dtls_replay_update(&ssl); |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 1134 | } |
| 1135 | |
| 1136 | /* Check new number */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1137 | memcpy(ssl.in_ctr + 2, new->x, 6); |
| 1138 | TEST_ASSERT(mbedtls_ssl_dtls_replay_check(&ssl) == ret); |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 1139 | |
Manuel Pégourié-Gonnard | 23fc437 | 2023-03-17 13:34:11 +0100 | [diff] [blame] | 1140 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1141 | mbedtls_ssl_free(&ssl); |
| 1142 | mbedtls_ssl_config_free(&conf); |
Manuel Pégourié-Gonnard | 23fc437 | 2023-03-17 13:34:11 +0100 | [diff] [blame] | 1143 | MD_OR_USE_PSA_DONE(); |
Manuel Pégourié-Gonnard | 4956fd7 | 2014-09-24 11:13:44 +0200 | [diff] [blame] | 1144 | } |
| 1145 | /* END_CASE */ |
Hanno Becker | b25c0c7 | 2017-05-05 11:24:30 +0100 | [diff] [blame] | 1146 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 1147 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1148 | void ssl_set_hostname_twice(char *hostname0, char *hostname1) |
Hanno Becker | b25c0c7 | 2017-05-05 11:24:30 +0100 | [diff] [blame] | 1149 | { |
Pengyu Lv | 30e0870 | 2023-07-10 10:53:11 +0800 | [diff] [blame] | 1150 | const char *hostname; |
Hanno Becker | b25c0c7 | 2017-05-05 11:24:30 +0100 | [diff] [blame] | 1151 | mbedtls_ssl_context ssl; |
Valerio Setti | 285dae8 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 1152 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1153 | mbedtls_ssl_init(&ssl); |
valerio | 32f2ac9 | 2023-04-20 11:59:52 +0200 | [diff] [blame] | 1154 | USE_PSA_INIT(); |
Hanno Becker | b25c0c7 | 2017-05-05 11:24:30 +0100 | [diff] [blame] | 1155 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1156 | TEST_ASSERT(mbedtls_ssl_set_hostname(&ssl, hostname0) == 0); |
Pengyu Lv | 30e0870 | 2023-07-10 10:53:11 +0800 | [diff] [blame] | 1157 | hostname = mbedtls_ssl_get_hostname(&ssl); |
| 1158 | TEST_ASSERT(strcmp(hostname0, hostname) == 0); |
| 1159 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1160 | TEST_ASSERT(mbedtls_ssl_set_hostname(&ssl, hostname1) == 0); |
Pengyu Lv | 30e0870 | 2023-07-10 10:53:11 +0800 | [diff] [blame] | 1161 | hostname = mbedtls_ssl_get_hostname(&ssl); |
| 1162 | TEST_ASSERT(strcmp(hostname1, hostname) == 0); |
Hanno Becker | b25c0c7 | 2017-05-05 11:24:30 +0100 | [diff] [blame] | 1163 | |
valerio | 32f2ac9 | 2023-04-20 11:59:52 +0200 | [diff] [blame] | 1164 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1165 | mbedtls_ssl_free(&ssl); |
Valerio Setti | 285dae8 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 1166 | USE_PSA_DONE(); |
Hanno Becker | b25c0c7 | 2017-05-05 11:24:30 +0100 | [diff] [blame] | 1167 | } |
Darryl Green | 11999bb | 2018-03-13 15:22:58 +0000 | [diff] [blame] | 1168 | /* END_CASE */ |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1169 | |
| 1170 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1171 | void ssl_crypt_record(int cipher_type, int hash_id, |
| 1172 | int etm, int tag_mode, int ver, |
| 1173 | int cid0_len, int cid1_len) |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1174 | { |
| 1175 | /* |
| 1176 | * Test several record encryptions and decryptions |
| 1177 | * with plenty of space before and after the data |
| 1178 | * within the record buffer. |
| 1179 | */ |
| 1180 | |
| 1181 | int ret; |
| 1182 | int num_records = 16; |
| 1183 | mbedtls_ssl_context ssl; /* ONLY for debugging */ |
| 1184 | |
| 1185 | mbedtls_ssl_transform t0, t1; |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 1186 | unsigned char *buf = NULL; |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1187 | size_t const buflen = 512; |
| 1188 | mbedtls_record rec, rec_backup; |
| 1189 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1190 | mbedtls_ssl_init(&ssl); |
| 1191 | mbedtls_ssl_transform_init(&t0); |
| 1192 | mbedtls_ssl_transform_init(&t1); |
valerio | 32f2ac9 | 2023-04-20 11:59:52 +0200 | [diff] [blame] | 1193 | MD_OR_USE_PSA_INIT(); |
| 1194 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 1195 | ret = mbedtls_test_ssl_build_transforms(&t0, &t1, cipher_type, hash_id, |
| 1196 | etm, tag_mode, ver, |
| 1197 | (size_t) cid0_len, |
| 1198 | (size_t) cid1_len); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1199 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1200 | TEST_ASSERT(ret == 0); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1201 | |
Gilles Peskine | d681ffd | 2023-10-17 17:31:50 +0200 | [diff] [blame] | 1202 | TEST_CALLOC(buf, buflen); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1203 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1204 | while (num_records-- > 0) { |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1205 | mbedtls_ssl_transform *t_dec, *t_enc; |
| 1206 | /* Take turns in who's sending and who's receiving. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1207 | if (num_records % 3 == 0) { |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1208 | t_dec = &t0; |
| 1209 | t_enc = &t1; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1210 | } else { |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1211 | t_dec = &t1; |
| 1212 | t_enc = &t0; |
| 1213 | } |
| 1214 | |
| 1215 | /* |
| 1216 | * The record header affects the transformation in two ways: |
| 1217 | * 1) It determines the AEAD additional data |
| 1218 | * 2) The record counter sometimes determines the IV. |
| 1219 | * |
| 1220 | * Apart from that, the fields don't have influence. |
| 1221 | * In particular, it is currently not the responsibility |
| 1222 | * of ssl_encrypt/decrypt_buf to check if the transform |
| 1223 | * version matches the record version, or that the |
| 1224 | * type is sensible. |
| 1225 | */ |
| 1226 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1227 | memset(rec.ctr, num_records, sizeof(rec.ctr)); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1228 | rec.type = 42; |
| 1229 | rec.ver[0] = num_records; |
| 1230 | rec.ver[1] = num_records; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1231 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 1232 | rec.cid_len = 0; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1233 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1234 | |
| 1235 | rec.buf = buf; |
| 1236 | rec.buf_len = buflen; |
| 1237 | rec.data_offset = 16; |
| 1238 | /* Make sure to vary the length to exercise different |
| 1239 | * paddings. */ |
| 1240 | rec.data_len = 1 + num_records; |
| 1241 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1242 | memset(rec.buf + rec.data_offset, 42, rec.data_len); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1243 | |
| 1244 | /* Make a copy for later comparison */ |
| 1245 | rec_backup = rec; |
| 1246 | |
| 1247 | /* Encrypt record */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1248 | ret = mbedtls_ssl_encrypt_buf(&ssl, t_enc, &rec, |
| 1249 | mbedtls_test_rnd_std_rand, NULL); |
| 1250 | TEST_ASSERT(ret == 0 || ret == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL); |
| 1251 | if (ret != 0) { |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1252 | continue; |
| 1253 | } |
| 1254 | |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 1255 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1256 | if (rec.cid_len != 0) { |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 1257 | /* DTLS 1.2 + CID hides the real content type and |
| 1258 | * uses a special CID content type in the protected |
| 1259 | * record. Double-check this. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1260 | TEST_ASSERT(rec.type == MBEDTLS_SSL_MSG_CID); |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 1261 | } |
| 1262 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 1263 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1264 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1265 | if (t_enc->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 1266 | /* TLS 1.3 hides the real content type and |
| 1267 | * always uses Application Data as the content type |
| 1268 | * for protected records. Double-check this. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1269 | TEST_ASSERT(rec.type == MBEDTLS_SSL_MSG_APPLICATION_DATA); |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 1270 | } |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1271 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 1272 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1273 | /* Decrypt record with t_dec */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1274 | ret = mbedtls_ssl_decrypt_buf(&ssl, t_dec, &rec); |
| 1275 | TEST_ASSERT(ret == 0); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1276 | |
| 1277 | /* Compare results */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1278 | TEST_ASSERT(rec.type == rec_backup.type); |
| 1279 | TEST_ASSERT(memcmp(rec.ctr, rec_backup.ctr, 8) == 0); |
| 1280 | TEST_ASSERT(rec.ver[0] == rec_backup.ver[0]); |
| 1281 | TEST_ASSERT(rec.ver[1] == rec_backup.ver[1]); |
| 1282 | TEST_ASSERT(rec.data_len == rec_backup.data_len); |
| 1283 | TEST_ASSERT(rec.data_offset == rec_backup.data_offset); |
| 1284 | TEST_ASSERT(memcmp(rec.buf + rec.data_offset, |
| 1285 | rec_backup.buf + rec_backup.data_offset, |
| 1286 | rec.data_len) == 0); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1287 | } |
| 1288 | |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 1289 | exit: |
| 1290 | |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1291 | /* Cleanup */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1292 | mbedtls_ssl_free(&ssl); |
| 1293 | mbedtls_ssl_transform_free(&t0); |
| 1294 | mbedtls_ssl_transform_free(&t1); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1295 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1296 | mbedtls_free(buf); |
Manuel Pégourié-Gonnard | 23fc437 | 2023-03-17 13:34:11 +0100 | [diff] [blame] | 1297 | MD_OR_USE_PSA_DONE(); |
Hanno Becker | a18d132 | 2018-01-03 14:27:32 +0000 | [diff] [blame] | 1298 | } |
| 1299 | /* END_CASE */ |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 1300 | |
| 1301 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1302 | void ssl_crypt_record_small(int cipher_type, int hash_id, |
| 1303 | int etm, int tag_mode, int ver, |
| 1304 | int cid0_len, int cid1_len) |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 1305 | { |
| 1306 | /* |
| 1307 | * Test pairs of encryption and decryption with an increasing |
| 1308 | * amount of space in the record buffer - in more detail: |
| 1309 | * 1) Try to encrypt with 0, 1, 2, ... bytes available |
| 1310 | * in front of the plaintext, and expect the encryption |
| 1311 | * to succeed starting from some offset. Always keep |
| 1312 | * enough space in the end of the buffer. |
| 1313 | * 2) Try to encrypt with 0, 1, 2, ... bytes available |
| 1314 | * at the end of the plaintext, and expect the encryption |
| 1315 | * to succeed starting from some offset. Always keep |
| 1316 | * enough space at the beginning of the buffer. |
| 1317 | * 3) Try to encrypt with 0, 1, 2, ... bytes available |
| 1318 | * both at the front and end of the plaintext, |
| 1319 | * and expect the encryption to succeed starting from |
| 1320 | * some offset. |
| 1321 | * |
| 1322 | * If encryption succeeds, check that decryption succeeds |
| 1323 | * and yields the original record. |
| 1324 | */ |
| 1325 | |
| 1326 | mbedtls_ssl_context ssl; /* ONLY for debugging */ |
| 1327 | |
| 1328 | mbedtls_ssl_transform t0, t1; |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 1329 | unsigned char *buf = NULL; |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 1330 | size_t const buflen = 256; |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 1331 | mbedtls_record rec, rec_backup; |
| 1332 | |
| 1333 | int ret; |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 1334 | int mode; /* Mode 1, 2 or 3 as explained above */ |
| 1335 | size_t offset; /* Available space at beginning/end/both */ |
| 1336 | size_t threshold = 96; /* Maximum offset to test against */ |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 1337 | |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 1338 | size_t default_pre_padding = 64; /* Pre-padding to use in mode 2 */ |
| 1339 | 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] | 1340 | |
| 1341 | int seen_success; /* Indicates if in the current mode we've |
| 1342 | * already seen a successful test. */ |
| 1343 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1344 | mbedtls_ssl_init(&ssl); |
| 1345 | mbedtls_ssl_transform_init(&t0); |
| 1346 | mbedtls_ssl_transform_init(&t1); |
valerio | 32f2ac9 | 2023-04-20 11:59:52 +0200 | [diff] [blame] | 1347 | MD_OR_USE_PSA_INIT(); |
| 1348 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 1349 | ret = mbedtls_test_ssl_build_transforms(&t0, &t1, cipher_type, hash_id, |
| 1350 | etm, tag_mode, ver, |
| 1351 | (size_t) cid0_len, |
| 1352 | (size_t) cid1_len); |
Przemyslaw Stekiel | 93cf4ee | 2022-01-19 16:18:53 +0100 | [diff] [blame] | 1353 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1354 | TEST_ASSERT(ret == 0); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 1355 | |
Gilles Peskine | d681ffd | 2023-10-17 17:31:50 +0200 | [diff] [blame] | 1356 | TEST_CALLOC(buf, buflen); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 1357 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1358 | for (mode = 1; mode <= 3; mode++) { |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 1359 | seen_success = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1360 | for (offset = 0; offset <= threshold; offset++) { |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 1361 | mbedtls_ssl_transform *t_dec, *t_enc; |
Hanno Becker | 6c87b3f | 2019-04-29 17:24:44 +0100 | [diff] [blame] | 1362 | t_dec = &t0; |
| 1363 | t_enc = &t1; |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 1364 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1365 | memset(rec.ctr, offset, sizeof(rec.ctr)); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 1366 | rec.type = 42; |
| 1367 | rec.ver[0] = offset; |
| 1368 | rec.ver[1] = offset; |
| 1369 | rec.buf = buf; |
| 1370 | rec.buf_len = buflen; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1371 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Hanno Becker | d856c82 | 2019-04-29 17:30:59 +0100 | [diff] [blame] | 1372 | rec.cid_len = 0; |
Hanno Becker | a0e20d0 | 2019-05-15 14:03:01 +0100 | [diff] [blame] | 1373 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 1374 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1375 | switch (mode) { |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 1376 | case 1: /* Space in the beginning */ |
| 1377 | rec.data_offset = offset; |
| 1378 | rec.data_len = buflen - offset - default_post_padding; |
| 1379 | break; |
| 1380 | |
| 1381 | case 2: /* Space in the end */ |
| 1382 | rec.data_offset = default_pre_padding; |
| 1383 | rec.data_len = buflen - default_pre_padding - offset; |
| 1384 | break; |
| 1385 | |
| 1386 | case 3: /* Space in the beginning and end */ |
| 1387 | rec.data_offset = offset; |
| 1388 | rec.data_len = buflen - 2 * offset; |
| 1389 | break; |
| 1390 | |
| 1391 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1392 | TEST_ASSERT(0); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 1393 | break; |
| 1394 | } |
| 1395 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1396 | memset(rec.buf + rec.data_offset, 42, rec.data_len); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 1397 | |
| 1398 | /* Make a copy for later comparison */ |
| 1399 | rec_backup = rec; |
| 1400 | |
| 1401 | /* Encrypt record */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1402 | ret = mbedtls_ssl_encrypt_buf(&ssl, t_enc, &rec, |
| 1403 | mbedtls_test_rnd_std_rand, NULL); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 1404 | |
Gilles Peskine | 5fd88b7 | 2023-07-20 22:18:23 +0200 | [diff] [blame] | 1405 | if (ret == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) { |
| 1406 | /* It's ok if the output buffer is too small. We do insist |
| 1407 | * on at least one mode succeeding; this is tracked by |
| 1408 | * seen_success. */ |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 1409 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1410 | } |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 1411 | |
Gilles Peskine | 5fd88b7 | 2023-07-20 22:18:23 +0200 | [diff] [blame] | 1412 | TEST_EQUAL(ret, 0); |
| 1413 | seen_success = 1; |
| 1414 | |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 1415 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1416 | if (rec.cid_len != 0) { |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 1417 | /* DTLS 1.2 + CID hides the real content type and |
| 1418 | * uses a special CID content type in the protected |
| 1419 | * record. Double-check this. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1420 | TEST_ASSERT(rec.type == MBEDTLS_SSL_MSG_CID); |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 1421 | } |
| 1422 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 1423 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1424 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1425 | if (t_enc->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 1426 | /* TLS 1.3 hides the real content type and |
| 1427 | * always uses Application Data as the content type |
| 1428 | * for protected records. Double-check this. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1429 | TEST_ASSERT(rec.type == MBEDTLS_SSL_MSG_APPLICATION_DATA); |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 1430 | } |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1431 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Hanno Becker | b2713ab | 2020-05-07 14:54:22 +0100 | [diff] [blame] | 1432 | |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 1433 | /* Decrypt record with t_dec */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1434 | TEST_ASSERT(mbedtls_ssl_decrypt_buf(&ssl, t_dec, &rec) == 0); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 1435 | |
| 1436 | /* Compare results */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1437 | TEST_ASSERT(rec.type == rec_backup.type); |
| 1438 | TEST_ASSERT(memcmp(rec.ctr, rec_backup.ctr, 8) == 0); |
| 1439 | TEST_ASSERT(rec.ver[0] == rec_backup.ver[0]); |
| 1440 | TEST_ASSERT(rec.ver[1] == rec_backup.ver[1]); |
| 1441 | TEST_ASSERT(rec.data_len == rec_backup.data_len); |
| 1442 | TEST_ASSERT(rec.data_offset == rec_backup.data_offset); |
| 1443 | TEST_ASSERT(memcmp(rec.buf + rec.data_offset, |
| 1444 | rec_backup.buf + rec_backup.data_offset, |
| 1445 | rec.data_len) == 0); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 1446 | } |
| 1447 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1448 | TEST_ASSERT(seen_success == 1); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 1449 | } |
| 1450 | |
Hanno Becker | 81e16a3 | 2019-03-01 11:21:44 +0000 | [diff] [blame] | 1451 | exit: |
| 1452 | |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 1453 | /* Cleanup */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1454 | mbedtls_ssl_free(&ssl); |
| 1455 | mbedtls_ssl_transform_free(&t0); |
| 1456 | mbedtls_ssl_transform_free(&t1); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 1457 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1458 | mbedtls_free(buf); |
Manuel Pégourié-Gonnard | 23fc437 | 2023-03-17 13:34:11 +0100 | [diff] [blame] | 1459 | MD_OR_USE_PSA_DONE(); |
Hanno Becker | b3268da | 2018-01-05 15:20:24 +0000 | [diff] [blame] | 1460 | } |
| 1461 | /* END_CASE */ |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 1462 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1463 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1464 | void ssl_tls13_hkdf_expand_label(int hash_alg, |
| 1465 | data_t *secret, |
| 1466 | int label_idx, |
| 1467 | data_t *ctx, |
| 1468 | int desired_length, |
| 1469 | data_t *expected) |
Hanno Becker | 39ff492 | 2020-08-21 13:36:56 +0100 | [diff] [blame] | 1470 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1471 | unsigned char dst[100]; |
Hanno Becker | 39ff492 | 2020-08-21 13:36:56 +0100 | [diff] [blame] | 1472 | |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 1473 | unsigned char const *lbl = NULL; |
| 1474 | size_t lbl_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1475 | #define MBEDTLS_SSL_TLS1_3_LABEL(name, string) \ |
| 1476 | if (label_idx == (int) tls13_label_ ## name) \ |
Xiaofei Bai | d25fab6 | 2021-12-02 06:36:27 +0000 | [diff] [blame] | 1477 | { \ |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 1478 | lbl = mbedtls_ssl_tls13_labels.name; \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1479 | lbl_len = sizeof(mbedtls_ssl_tls13_labels.name); \ |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 1480 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1481 | MBEDTLS_SSL_TLS1_3_LABEL_LIST |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 1482 | #undef MBEDTLS_SSL_TLS1_3_LABEL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1483 | TEST_ASSERT(lbl != NULL); |
Hanno Becker | 39ff492 | 2020-08-21 13:36:56 +0100 | [diff] [blame] | 1484 | |
| 1485 | /* Check sanity of test parameters. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1486 | TEST_ASSERT((size_t) desired_length <= sizeof(dst)); |
| 1487 | TEST_ASSERT((size_t) desired_length == expected->len); |
Hanno Becker | 39ff492 | 2020-08-21 13:36:56 +0100 | [diff] [blame] | 1488 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1489 | PSA_INIT(); |
Gabor Mezei | 892c4aa | 2022-03-21 12:21:43 +0100 | [diff] [blame] | 1490 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1491 | TEST_ASSERT(mbedtls_ssl_tls13_hkdf_expand_label( |
| 1492 | (psa_algorithm_t) hash_alg, |
| 1493 | secret->x, secret->len, |
| 1494 | lbl, lbl_len, |
| 1495 | ctx->x, ctx->len, |
| 1496 | dst, desired_length) == 0); |
Hanno Becker | 39ff492 | 2020-08-21 13:36:56 +0100 | [diff] [blame] | 1497 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 1498 | TEST_MEMORY_COMPARE(dst, (size_t) desired_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 1499 | expected->x, (size_t) expected->len); |
Gabor Mezei | 892c4aa | 2022-03-21 12:21:43 +0100 | [diff] [blame] | 1500 | |
valerio | 32f2ac9 | 2023-04-20 11:59:52 +0200 | [diff] [blame] | 1501 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1502 | PSA_DONE(); |
Hanno Becker | 39ff492 | 2020-08-21 13:36:56 +0100 | [diff] [blame] | 1503 | } |
| 1504 | /* END_CASE */ |
| 1505 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1506 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1507 | void ssl_tls13_traffic_key_generation(int hash_alg, |
| 1508 | data_t *server_secret, |
| 1509 | data_t *client_secret, |
| 1510 | int desired_iv_len, |
| 1511 | int desired_key_len, |
| 1512 | data_t *expected_server_write_key, |
| 1513 | data_t *expected_server_write_iv, |
| 1514 | data_t *expected_client_write_key, |
| 1515 | data_t *expected_client_write_iv) |
Hanno Becker | 19498f8 | 2020-08-21 13:37:08 +0100 | [diff] [blame] | 1516 | { |
| 1517 | mbedtls_ssl_key_set keys; |
| 1518 | |
| 1519 | /* Check sanity of test parameters. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1520 | TEST_ASSERT(client_secret->len == server_secret->len); |
Yanray Wang | d577a68 | 2022-10-27 11:47:54 +0800 | [diff] [blame] | 1521 | TEST_ASSERT( |
| 1522 | expected_client_write_iv->len == expected_server_write_iv->len && |
| 1523 | expected_client_write_iv->len == (size_t) desired_iv_len); |
| 1524 | TEST_ASSERT( |
| 1525 | expected_client_write_key->len == expected_server_write_key->len && |
| 1526 | expected_client_write_key->len == (size_t) desired_key_len); |
Hanno Becker | 19498f8 | 2020-08-21 13:37:08 +0100 | [diff] [blame] | 1527 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1528 | PSA_INIT(); |
Gabor Mezei | 892c4aa | 2022-03-21 12:21:43 +0100 | [diff] [blame] | 1529 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1530 | TEST_ASSERT(mbedtls_ssl_tls13_make_traffic_keys( |
| 1531 | (psa_algorithm_t) hash_alg, |
| 1532 | client_secret->x, |
| 1533 | server_secret->x, |
| 1534 | client_secret->len /* == server_secret->len */, |
| 1535 | desired_key_len, desired_iv_len, |
| 1536 | &keys) == 0); |
Hanno Becker | 19498f8 | 2020-08-21 13:37:08 +0100 | [diff] [blame] | 1537 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 1538 | TEST_MEMORY_COMPARE(keys.client_write_key, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 1539 | keys.key_len, |
| 1540 | expected_client_write_key->x, |
| 1541 | (size_t) desired_key_len); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 1542 | TEST_MEMORY_COMPARE(keys.server_write_key, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 1543 | keys.key_len, |
| 1544 | expected_server_write_key->x, |
| 1545 | (size_t) desired_key_len); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 1546 | TEST_MEMORY_COMPARE(keys.client_write_iv, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 1547 | keys.iv_len, |
| 1548 | expected_client_write_iv->x, |
| 1549 | (size_t) desired_iv_len); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 1550 | TEST_MEMORY_COMPARE(keys.server_write_iv, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 1551 | keys.iv_len, |
| 1552 | expected_server_write_iv->x, |
| 1553 | (size_t) desired_iv_len); |
Gabor Mezei | 892c4aa | 2022-03-21 12:21:43 +0100 | [diff] [blame] | 1554 | |
valerio | 32f2ac9 | 2023-04-20 11:59:52 +0200 | [diff] [blame] | 1555 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1556 | PSA_DONE(); |
Hanno Becker | 19498f8 | 2020-08-21 13:37:08 +0100 | [diff] [blame] | 1557 | } |
| 1558 | /* END_CASE */ |
| 1559 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1560 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1561 | void ssl_tls13_derive_secret(int hash_alg, |
| 1562 | data_t *secret, |
| 1563 | int label_idx, |
| 1564 | data_t *ctx, |
| 1565 | int desired_length, |
| 1566 | int already_hashed, |
| 1567 | data_t *expected) |
Hanno Becker | e4849d1 | 2020-08-21 14:14:14 +0100 | [diff] [blame] | 1568 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1569 | unsigned char dst[100]; |
Hanno Becker | e4849d1 | 2020-08-21 14:14:14 +0100 | [diff] [blame] | 1570 | |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 1571 | unsigned char const *lbl = NULL; |
| 1572 | size_t lbl_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1573 | #define MBEDTLS_SSL_TLS1_3_LABEL(name, string) \ |
| 1574 | if (label_idx == (int) tls13_label_ ## name) \ |
Xiaofei Bai | d25fab6 | 2021-12-02 06:36:27 +0000 | [diff] [blame] | 1575 | { \ |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 1576 | lbl = mbedtls_ssl_tls13_labels.name; \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1577 | lbl_len = sizeof(mbedtls_ssl_tls13_labels.name); \ |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 1578 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1579 | MBEDTLS_SSL_TLS1_3_LABEL_LIST |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 1580 | #undef MBEDTLS_SSL_TLS1_3_LABEL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1581 | TEST_ASSERT(lbl != NULL); |
Hanno Becker | 70d7fb0 | 2020-09-09 10:11:21 +0100 | [diff] [blame] | 1582 | |
Hanno Becker | e4849d1 | 2020-08-21 14:14:14 +0100 | [diff] [blame] | 1583 | /* Check sanity of test parameters. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1584 | TEST_ASSERT((size_t) desired_length <= sizeof(dst)); |
| 1585 | TEST_ASSERT((size_t) desired_length == expected->len); |
Hanno Becker | e4849d1 | 2020-08-21 14:14:14 +0100 | [diff] [blame] | 1586 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1587 | PSA_INIT(); |
Gabor Mezei | 892c4aa | 2022-03-21 12:21:43 +0100 | [diff] [blame] | 1588 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1589 | TEST_ASSERT(mbedtls_ssl_tls13_derive_secret( |
| 1590 | (psa_algorithm_t) hash_alg, |
| 1591 | secret->x, secret->len, |
| 1592 | lbl, lbl_len, |
| 1593 | ctx->x, ctx->len, |
| 1594 | already_hashed, |
| 1595 | dst, desired_length) == 0); |
Hanno Becker | e4849d1 | 2020-08-21 14:14:14 +0100 | [diff] [blame] | 1596 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 1597 | TEST_MEMORY_COMPARE(dst, desired_length, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 1598 | expected->x, desired_length); |
Gabor Mezei | 892c4aa | 2022-03-21 12:21:43 +0100 | [diff] [blame] | 1599 | |
valerio | 32f2ac9 | 2023-04-20 11:59:52 +0200 | [diff] [blame] | 1600 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1601 | PSA_DONE(); |
Hanno Becker | e4849d1 | 2020-08-21 14:14:14 +0100 | [diff] [blame] | 1602 | } |
| 1603 | /* END_CASE */ |
| 1604 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1605 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1606 | void ssl_tls13_derive_early_secrets(int hash_alg, |
| 1607 | data_t *secret, |
| 1608 | data_t *transcript, |
| 1609 | data_t *traffic_expected, |
| 1610 | data_t *exporter_expected) |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 1611 | { |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 1612 | mbedtls_ssl_tls13_early_secrets secrets; |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 1613 | |
| 1614 | /* Double-check that we've passed sane parameters. */ |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 1615 | psa_algorithm_t alg = (psa_algorithm_t) hash_alg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1616 | size_t const hash_len = PSA_HASH_LENGTH(alg); |
| 1617 | TEST_ASSERT(PSA_ALG_IS_HASH(alg) && |
| 1618 | secret->len == hash_len && |
| 1619 | transcript->len == hash_len && |
| 1620 | traffic_expected->len == hash_len && |
| 1621 | exporter_expected->len == hash_len); |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 1622 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1623 | PSA_INIT(); |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 1624 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1625 | TEST_ASSERT(mbedtls_ssl_tls13_derive_early_secrets( |
| 1626 | alg, secret->x, transcript->x, transcript->len, |
| 1627 | &secrets) == 0); |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 1628 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 1629 | TEST_MEMORY_COMPARE(secrets.client_early_traffic_secret, hash_len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 1630 | traffic_expected->x, traffic_expected->len); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 1631 | TEST_MEMORY_COMPARE(secrets.early_exporter_master_secret, hash_len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 1632 | exporter_expected->x, exporter_expected->len); |
Gabor Mezei | 892c4aa | 2022-03-21 12:21:43 +0100 | [diff] [blame] | 1633 | |
valerio | 32f2ac9 | 2023-04-20 11:59:52 +0200 | [diff] [blame] | 1634 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1635 | PSA_DONE(); |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 1636 | } |
| 1637 | /* END_CASE */ |
| 1638 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1639 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1640 | void ssl_tls13_derive_handshake_secrets(int hash_alg, |
| 1641 | data_t *secret, |
| 1642 | data_t *transcript, |
| 1643 | data_t *client_expected, |
| 1644 | data_t *server_expected) |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 1645 | { |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 1646 | mbedtls_ssl_tls13_handshake_secrets secrets; |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 1647 | |
| 1648 | /* Double-check that we've passed sane parameters. */ |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 1649 | psa_algorithm_t alg = (psa_algorithm_t) hash_alg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1650 | size_t const hash_len = PSA_HASH_LENGTH(alg); |
| 1651 | TEST_ASSERT(PSA_ALG_IS_HASH(alg) && |
| 1652 | secret->len == hash_len && |
| 1653 | transcript->len == hash_len && |
| 1654 | client_expected->len == hash_len && |
| 1655 | server_expected->len == hash_len); |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 1656 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1657 | PSA_INIT(); |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 1658 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1659 | TEST_ASSERT(mbedtls_ssl_tls13_derive_handshake_secrets( |
| 1660 | alg, secret->x, transcript->x, transcript->len, |
| 1661 | &secrets) == 0); |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 1662 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 1663 | TEST_MEMORY_COMPARE(secrets.client_handshake_traffic_secret, hash_len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 1664 | client_expected->x, client_expected->len); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 1665 | TEST_MEMORY_COMPARE(secrets.server_handshake_traffic_secret, hash_len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 1666 | server_expected->x, server_expected->len); |
Gabor Mezei | 892c4aa | 2022-03-21 12:21:43 +0100 | [diff] [blame] | 1667 | |
valerio | 32f2ac9 | 2023-04-20 11:59:52 +0200 | [diff] [blame] | 1668 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1669 | PSA_DONE(); |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 1670 | } |
| 1671 | /* END_CASE */ |
| 1672 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1673 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1674 | void ssl_tls13_derive_application_secrets(int hash_alg, |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 1675 | data_t *secret, |
| 1676 | data_t *transcript, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1677 | data_t *client_expected, |
| 1678 | data_t *server_expected, |
| 1679 | data_t *exporter_expected) |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 1680 | { |
Xiaofei Bai | 746f948 | 2021-11-12 08:53:56 +0000 | [diff] [blame] | 1681 | mbedtls_ssl_tls13_application_secrets secrets; |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 1682 | |
| 1683 | /* Double-check that we've passed sane parameters. */ |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 1684 | psa_algorithm_t alg = (psa_algorithm_t) hash_alg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1685 | size_t const hash_len = PSA_HASH_LENGTH(alg); |
| 1686 | TEST_ASSERT(PSA_ALG_IS_HASH(alg) && |
| 1687 | secret->len == hash_len && |
| 1688 | transcript->len == hash_len && |
| 1689 | client_expected->len == hash_len && |
| 1690 | server_expected->len == hash_len && |
| 1691 | exporter_expected->len == hash_len); |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 1692 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1693 | PSA_INIT(); |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 1694 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1695 | TEST_ASSERT(mbedtls_ssl_tls13_derive_application_secrets( |
| 1696 | alg, secret->x, transcript->x, transcript->len, |
| 1697 | &secrets) == 0); |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 1698 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 1699 | TEST_MEMORY_COMPARE(secrets.client_application_traffic_secret_N, hash_len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 1700 | client_expected->x, client_expected->len); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 1701 | TEST_MEMORY_COMPARE(secrets.server_application_traffic_secret_N, hash_len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 1702 | server_expected->x, server_expected->len); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 1703 | TEST_MEMORY_COMPARE(secrets.exporter_master_secret, hash_len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 1704 | exporter_expected->x, exporter_expected->len); |
Gabor Mezei | 892c4aa | 2022-03-21 12:21:43 +0100 | [diff] [blame] | 1705 | |
valerio | 32f2ac9 | 2023-04-20 11:59:52 +0200 | [diff] [blame] | 1706 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1707 | PSA_DONE(); |
Hanno Becker | a4f40a0 | 2021-05-24 06:42:11 +0100 | [diff] [blame] | 1708 | } |
| 1709 | /* END_CASE */ |
| 1710 | |
Ronald Cron | 6f135e1 | 2021-12-08 16:57:54 +0100 | [diff] [blame] | 1711 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1712 | void ssl_tls13_derive_resumption_secrets(int hash_alg, |
| 1713 | data_t *secret, |
| 1714 | data_t *transcript, |
| 1715 | data_t *resumption_expected) |
Hanno Becker | 55bc2c5 | 2021-05-24 06:53:52 +0100 | [diff] [blame] | 1716 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1717 | mbedtls_ssl_tls13_application_secrets secrets; |
Hanno Becker | 55bc2c5 | 2021-05-24 06:53:52 +0100 | [diff] [blame] | 1718 | |
| 1719 | /* Double-check that we've passed sane parameters. */ |
Gabor Mezei | 07732f7 | 2022-03-26 17:04:19 +0100 | [diff] [blame] | 1720 | psa_algorithm_t alg = (psa_algorithm_t) hash_alg; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1721 | size_t const hash_len = PSA_HASH_LENGTH(alg); |
| 1722 | TEST_ASSERT(PSA_ALG_IS_HASH(alg) && |
| 1723 | secret->len == hash_len && |
| 1724 | transcript->len == hash_len && |
| 1725 | resumption_expected->len == hash_len); |
Hanno Becker | 55bc2c5 | 2021-05-24 06:53:52 +0100 | [diff] [blame] | 1726 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1727 | PSA_INIT(); |
Hanno Becker | 55bc2c5 | 2021-05-24 06:53:52 +0100 | [diff] [blame] | 1728 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1729 | TEST_ASSERT(mbedtls_ssl_tls13_derive_resumption_master_secret( |
| 1730 | alg, secret->x, transcript->x, transcript->len, |
| 1731 | &secrets) == 0); |
Hanno Becker | 55bc2c5 | 2021-05-24 06:53:52 +0100 | [diff] [blame] | 1732 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 1733 | TEST_MEMORY_COMPARE(secrets.resumption_master_secret, hash_len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 1734 | resumption_expected->x, resumption_expected->len); |
Gabor Mezei | 892c4aa | 2022-03-21 12:21:43 +0100 | [diff] [blame] | 1735 | |
valerio | 32f2ac9 | 2023-04-20 11:59:52 +0200 | [diff] [blame] | 1736 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1737 | PSA_DONE(); |
| 1738 | } |
| 1739 | /* END_CASE */ |
| 1740 | |
| 1741 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */ |
| 1742 | void ssl_tls13_create_psk_binder(int hash_alg, |
| 1743 | data_t *psk, |
| 1744 | int psk_type, |
| 1745 | data_t *transcript, |
| 1746 | data_t *binder_expected) |
| 1747 | { |
Manuel Pégourié-Gonnard | 8857984 | 2023-03-28 11:20:23 +0200 | [diff] [blame] | 1748 | unsigned char binder[MBEDTLS_MD_MAX_SIZE]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1749 | |
| 1750 | /* Double-check that we've passed sane parameters. */ |
| 1751 | psa_algorithm_t alg = (psa_algorithm_t) hash_alg; |
| 1752 | size_t const hash_len = PSA_HASH_LENGTH(alg); |
| 1753 | TEST_ASSERT(PSA_ALG_IS_HASH(alg) && |
| 1754 | transcript->len == hash_len && |
| 1755 | binder_expected->len == hash_len); |
| 1756 | |
| 1757 | PSA_INIT(); |
| 1758 | |
| 1759 | TEST_ASSERT(mbedtls_ssl_tls13_create_psk_binder( |
| 1760 | NULL, /* SSL context for debugging only */ |
| 1761 | alg, |
| 1762 | psk->x, psk->len, |
| 1763 | psk_type, |
| 1764 | transcript->x, |
| 1765 | binder) == 0); |
| 1766 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 1767 | TEST_MEMORY_COMPARE(binder, hash_len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 1768 | binder_expected->x, binder_expected->len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1769 | |
valerio | 32f2ac9 | 2023-04-20 11:59:52 +0200 | [diff] [blame] | 1770 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1771 | PSA_DONE(); |
Hanno Becker | 55bc2c5 | 2021-05-24 06:53:52 +0100 | [diff] [blame] | 1772 | } |
| 1773 | /* END_CASE */ |
| 1774 | |
Gilles Peskine | 9d5952d | 2023-07-20 20:11:21 +0200 | [diff] [blame] | 1775 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1776 | void ssl_tls13_record_protection(int ciphersuite, |
| 1777 | int endpoint, |
| 1778 | int ctr, |
| 1779 | int padding_used, |
| 1780 | data_t *server_write_key, |
| 1781 | data_t *server_write_iv, |
| 1782 | data_t *client_write_key, |
| 1783 | data_t *client_write_iv, |
| 1784 | data_t *plaintext, |
| 1785 | data_t *ciphertext) |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 1786 | { |
| 1787 | mbedtls_ssl_key_set keys; |
| 1788 | mbedtls_ssl_transform transform_send; |
Gilles Peskine | 21e46b3 | 2023-10-17 16:35:20 +0200 | [diff] [blame] | 1789 | mbedtls_ssl_transform_init(&transform_send); |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 1790 | mbedtls_ssl_transform transform_recv; |
Gilles Peskine | 21e46b3 | 2023-10-17 16:35:20 +0200 | [diff] [blame] | 1791 | mbedtls_ssl_transform_init(&transform_recv); |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 1792 | mbedtls_record rec; |
| 1793 | unsigned char *buf = NULL; |
Hanno Becker | 1f91878 | 2021-08-01 19:18:28 +0100 | [diff] [blame] | 1794 | size_t buf_len; |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 1795 | int other_endpoint; |
| 1796 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1797 | TEST_ASSERT(endpoint == MBEDTLS_SSL_IS_CLIENT || |
| 1798 | endpoint == MBEDTLS_SSL_IS_SERVER); |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 1799 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1800 | if (endpoint == MBEDTLS_SSL_IS_SERVER) { |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 1801 | other_endpoint = MBEDTLS_SSL_IS_CLIENT; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1802 | } |
| 1803 | if (endpoint == MBEDTLS_SSL_IS_CLIENT) { |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 1804 | other_endpoint = MBEDTLS_SSL_IS_SERVER; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1805 | } |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 1806 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1807 | TEST_ASSERT(server_write_key->len == client_write_key->len); |
| 1808 | TEST_ASSERT(server_write_iv->len == client_write_iv->len); |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 1809 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1810 | memcpy(keys.client_write_key, |
| 1811 | client_write_key->x, client_write_key->len); |
| 1812 | memcpy(keys.client_write_iv, |
| 1813 | client_write_iv->x, client_write_iv->len); |
| 1814 | memcpy(keys.server_write_key, |
| 1815 | server_write_key->x, server_write_key->len); |
| 1816 | memcpy(keys.server_write_iv, |
| 1817 | server_write_iv->x, server_write_iv->len); |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 1818 | |
| 1819 | keys.key_len = server_write_key->len; |
| 1820 | keys.iv_len = server_write_iv->len; |
| 1821 | |
Valerio Setti | 00a256f | 2023-04-27 17:22:27 +0200 | [diff] [blame] | 1822 | MD_OR_USE_PSA_INIT(); |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 1823 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1824 | TEST_ASSERT(mbedtls_ssl_tls13_populate_transform( |
| 1825 | &transform_send, endpoint, |
| 1826 | ciphersuite, &keys, NULL) == 0); |
| 1827 | TEST_ASSERT(mbedtls_ssl_tls13_populate_transform( |
| 1828 | &transform_recv, other_endpoint, |
| 1829 | ciphersuite, &keys, NULL) == 0); |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 1830 | |
Hanno Becker | 1f91878 | 2021-08-01 19:18:28 +0100 | [diff] [blame] | 1831 | /* Make sure we have enough space in the buffer even if |
| 1832 | * we use more padding than the KAT. */ |
| 1833 | buf_len = ciphertext->len + MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY; |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 1834 | TEST_CALLOC(buf, buf_len); |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 1835 | rec.type = MBEDTLS_SSL_MSG_APPLICATION_DATA; |
Hanno Becker | 4153745 | 2021-04-20 05:35:28 +0100 | [diff] [blame] | 1836 | |
| 1837 | /* TLS 1.3 uses the version identifier from TLS 1.2 on the wire. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1838 | mbedtls_ssl_write_version(rec.ver, |
| 1839 | MBEDTLS_SSL_TRANSPORT_STREAM, |
| 1840 | MBEDTLS_SSL_VERSION_TLS1_2); |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 1841 | |
| 1842 | /* Copy plaintext into record structure */ |
| 1843 | rec.buf = buf; |
Hanno Becker | 1f91878 | 2021-08-01 19:18:28 +0100 | [diff] [blame] | 1844 | rec.buf_len = buf_len; |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 1845 | rec.data_offset = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1846 | TEST_ASSERT(plaintext->len <= ciphertext->len); |
| 1847 | memcpy(rec.buf + rec.data_offset, plaintext->x, plaintext->len); |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 1848 | rec.data_len = plaintext->len; |
| 1849 | #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID) |
| 1850 | rec.cid_len = 0; |
| 1851 | #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
| 1852 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1853 | memset(&rec.ctr[0], 0, 8); |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 1854 | rec.ctr[7] = ctr; |
| 1855 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1856 | TEST_ASSERT(mbedtls_ssl_encrypt_buf(NULL, &transform_send, &rec, |
| 1857 | NULL, NULL) == 0); |
Hanno Becker | 1f91878 | 2021-08-01 19:18:28 +0100 | [diff] [blame] | 1858 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1859 | if (padding_used == MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY) { |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 1860 | TEST_MEMORY_COMPARE(rec.buf + rec.data_offset, rec.data_len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 1861 | ciphertext->x, ciphertext->len); |
Hanno Becker | 1f91878 | 2021-08-01 19:18:28 +0100 | [diff] [blame] | 1862 | } |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 1863 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1864 | TEST_ASSERT(mbedtls_ssl_decrypt_buf(NULL, &transform_recv, &rec) == 0); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 1865 | TEST_MEMORY_COMPARE(rec.buf + rec.data_offset, rec.data_len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 1866 | plaintext->x, plaintext->len); |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 1867 | |
valerio | 32f2ac9 | 2023-04-20 11:59:52 +0200 | [diff] [blame] | 1868 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1869 | mbedtls_free(buf); |
| 1870 | mbedtls_ssl_transform_free(&transform_send); |
| 1871 | mbedtls_ssl_transform_free(&transform_recv); |
Manuel Pégourié-Gonnard | 23fc437 | 2023-03-17 13:34:11 +0100 | [diff] [blame] | 1872 | MD_OR_USE_PSA_DONE(); |
Hanno Becker | a77d005 | 2021-03-22 15:16:33 +0000 | [diff] [blame] | 1873 | } |
| 1874 | /* END_CASE */ |
| 1875 | |
Andrzej Kurek | 658442f | 2022-10-12 11:28:41 -0400 | [diff] [blame] | 1876 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1877 | void ssl_tls13_key_evolution(int hash_alg, |
| 1878 | data_t *secret, |
| 1879 | data_t *input, |
| 1880 | data_t *expected) |
Hanno Becker | 2d2c3eb | 2020-08-20 14:54:24 +0100 | [diff] [blame] | 1881 | { |
Manuel Pégourié-Gonnard | 8857984 | 2023-03-28 11:20:23 +0200 | [diff] [blame] | 1882 | unsigned char secret_new[MBEDTLS_MD_MAX_SIZE]; |
Hanno Becker | 2d2c3eb | 2020-08-20 14:54:24 +0100 | [diff] [blame] | 1883 | |
Gabor Mezei | 5d9a1fe | 2022-03-24 17:49:14 +0100 | [diff] [blame] | 1884 | PSA_INIT(); |
Gabor Mezei | 892c4aa | 2022-03-21 12:21:43 +0100 | [diff] [blame] | 1885 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1886 | TEST_ASSERT(mbedtls_ssl_tls13_evolve_secret( |
| 1887 | (psa_algorithm_t) hash_alg, |
| 1888 | secret->len ? secret->x : NULL, |
| 1889 | input->len ? input->x : NULL, input->len, |
| 1890 | secret_new) == 0); |
Hanno Becker | 2d2c3eb | 2020-08-20 14:54:24 +0100 | [diff] [blame] | 1891 | |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 1892 | TEST_MEMORY_COMPARE(secret_new, (size_t) expected->len, |
Tom Cosgrove | 0540fe7 | 2023-07-27 14:17:27 +0100 | [diff] [blame] | 1893 | expected->x, (size_t) expected->len); |
Gabor Mezei | 892c4aa | 2022-03-21 12:21:43 +0100 | [diff] [blame] | 1894 | |
valerio | 32f2ac9 | 2023-04-20 11:59:52 +0200 | [diff] [blame] | 1895 | exit: |
Gabor Mezei | 5d9a1fe | 2022-03-24 17:49:14 +0100 | [diff] [blame] | 1896 | PSA_DONE(); |
Hanno Becker | 2d2c3eb | 2020-08-20 14:54:24 +0100 | [diff] [blame] | 1897 | } |
| 1898 | /* END_CASE */ |
| 1899 | |
Jerry Yu | 53d23e2 | 2022-02-09 16:25:09 +0800 | [diff] [blame] | 1900 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_2 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1901 | void ssl_tls_prf(int type, data_t *secret, data_t *random, |
| 1902 | char *label, data_t *result_str, int exp_ret) |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 1903 | { |
| 1904 | unsigned char *output; |
| 1905 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1906 | output = mbedtls_calloc(1, result_str->len); |
| 1907 | if (output == NULL) { |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 1908 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1909 | } |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 1910 | |
Manuel Pégourié-Gonnard | 23fc437 | 2023-03-17 13:34:11 +0100 | [diff] [blame] | 1911 | MD_OR_USE_PSA_INIT(); |
Ron Eldor | 6b9b1b8 | 2019-05-15 17:04:33 +0300 | [diff] [blame] | 1912 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1913 | TEST_ASSERT(mbedtls_ssl_tls_prf(type, secret->x, secret->len, |
| 1914 | label, random->x, random->len, |
| 1915 | output, result_str->len) == exp_ret); |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 1916 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1917 | if (exp_ret == 0) { |
| 1918 | TEST_ASSERT(mbedtls_test_hexcmp(output, result_str->x, |
| 1919 | result_str->len, result_str->len) == 0); |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 1920 | } |
| 1921 | exit: |
| 1922 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1923 | mbedtls_free(output); |
Manuel Pégourié-Gonnard | 23fc437 | 2023-03-17 13:34:11 +0100 | [diff] [blame] | 1924 | MD_OR_USE_PSA_DONE(); |
Ron Eldor | 824ad7b | 2019-05-13 14:09:00 +0300 | [diff] [blame] | 1925 | } |
| 1926 | /* END_CASE */ |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 1927 | |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 1928 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1929 | void ssl_serialize_session_save_load(int ticket_len, char *crt_file, |
| 1930 | int endpoint_type, int tls_version) |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 1931 | { |
| 1932 | mbedtls_ssl_session original, restored; |
| 1933 | unsigned char *buf = NULL; |
| 1934 | size_t len; |
| 1935 | |
| 1936 | /* |
| 1937 | * Test that a save-load pair is the identity |
| 1938 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1939 | mbedtls_ssl_session_init(&original); |
| 1940 | mbedtls_ssl_session_init(&restored); |
valerio | 32f2ac9 | 2023-04-20 11:59:52 +0200 | [diff] [blame] | 1941 | USE_PSA_INIT(); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 1942 | |
| 1943 | /* Prepare a dummy session to work on */ |
Jerry Yu | f092629 | 2022-07-15 13:05:57 +0800 | [diff] [blame] | 1944 | ((void) endpoint_type); |
| 1945 | ((void) tls_version); |
Jerry Yu | 28547c4 | 2023-10-31 14:42:50 +0800 | [diff] [blame] | 1946 | ((void) ticket_len); |
| 1947 | ((void) crt_file); |
Jerry Yu | f092629 | 2022-07-15 13:05:57 +0800 | [diff] [blame] | 1948 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1949 | if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 1950 | TEST_ASSERT(mbedtls_test_ssl_tls13_populate_session( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1951 | &original, 0, endpoint_type) == 0); |
Jerry Yu | 28547c4 | 2023-10-31 14:42:50 +0800 | [diff] [blame] | 1952 | } |
Jerry Yu | f092629 | 2022-07-15 13:05:57 +0800 | [diff] [blame] | 1953 | #endif |
Jerry Yu | 28547c4 | 2023-10-31 14:42:50 +0800 | [diff] [blame] | 1954 | |
| 1955 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 1956 | if (tls_version == MBEDTLS_SSL_VERSION_TLS1_2) { |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 1957 | TEST_ASSERT(mbedtls_test_ssl_tls12_populate_session( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1958 | &original, ticket_len, crt_file) == 0); |
Jerry Yu | f092629 | 2022-07-15 13:05:57 +0800 | [diff] [blame] | 1959 | } |
Jerry Yu | 28547c4 | 2023-10-31 14:42:50 +0800 | [diff] [blame] | 1960 | #endif |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 1961 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 1962 | /* Serialize it */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1963 | TEST_ASSERT(mbedtls_ssl_session_save(&original, NULL, 0, &len) |
| 1964 | == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL); |
Gilles Peskine | d681ffd | 2023-10-17 17:31:50 +0200 | [diff] [blame] | 1965 | TEST_CALLOC(buf, len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1966 | TEST_ASSERT(mbedtls_ssl_session_save(&original, buf, len, &len) |
| 1967 | == 0); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 1968 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 1969 | /* Restore session from serialized data */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1970 | TEST_ASSERT(mbedtls_ssl_session_load(&restored, buf, len) == 0); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 1971 | |
| 1972 | /* |
| 1973 | * Make sure both session structures are identical |
| 1974 | */ |
| 1975 | #if defined(MBEDTLS_HAVE_TIME) |
Jerry Yu | b2455d2 | 2023-11-15 09:57:32 +0800 | [diff] [blame^] | 1976 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SRV_C) |
Jerry Yu | 28547c4 | 2023-10-31 14:42:50 +0800 | [diff] [blame] | 1977 | if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
Jerry Yu | 25ba4d4 | 2023-11-10 14:12:20 +0800 | [diff] [blame] | 1978 | TEST_ASSERT(original.ticket_creation_time == restored.ticket_creation_time); |
Jerry Yu | 28547c4 | 2023-10-31 14:42:50 +0800 | [diff] [blame] | 1979 | } |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 1980 | #endif |
Jerry Yu | 28547c4 | 2023-10-31 14:42:50 +0800 | [diff] [blame] | 1981 | |
| 1982 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 1983 | if (tls_version == MBEDTLS_SSL_VERSION_TLS1_2) { |
| 1984 | TEST_ASSERT(original.start == restored.start); |
| 1985 | } |
| 1986 | #endif |
| 1987 | |
| 1988 | #endif |
| 1989 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1990 | TEST_ASSERT(original.tls_version == restored.tls_version); |
| 1991 | TEST_ASSERT(original.ciphersuite == restored.ciphersuite); |
Jerry Yu | f092629 | 2022-07-15 13:05:57 +0800 | [diff] [blame] | 1992 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1993 | if (tls_version == MBEDTLS_SSL_VERSION_TLS1_2) { |
| 1994 | TEST_ASSERT(original.id_len == restored.id_len); |
| 1995 | TEST_ASSERT(memcmp(original.id, |
| 1996 | restored.id, sizeof(original.id)) == 0); |
| 1997 | TEST_ASSERT(memcmp(original.master, |
| 1998 | restored.master, sizeof(original.master)) == 0); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 1999 | |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 2000 | #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 2001 | #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2002 | TEST_ASSERT((original.peer_cert == NULL) == |
| 2003 | (restored.peer_cert == NULL)); |
| 2004 | if (original.peer_cert != NULL) { |
| 2005 | TEST_ASSERT(original.peer_cert->raw.len == |
| 2006 | restored.peer_cert->raw.len); |
| 2007 | TEST_ASSERT(memcmp(original.peer_cert->raw.p, |
| 2008 | restored.peer_cert->raw.p, |
| 2009 | original.peer_cert->raw.len) == 0); |
Jerry Yu | f092629 | 2022-07-15 13:05:57 +0800 | [diff] [blame] | 2010 | } |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 2011 | #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2012 | TEST_ASSERT(original.peer_cert_digest_type == |
| 2013 | restored.peer_cert_digest_type); |
| 2014 | TEST_ASSERT(original.peer_cert_digest_len == |
| 2015 | restored.peer_cert_digest_len); |
| 2016 | TEST_ASSERT((original.peer_cert_digest == NULL) == |
| 2017 | (restored.peer_cert_digest == NULL)); |
| 2018 | if (original.peer_cert_digest != NULL) { |
| 2019 | TEST_ASSERT(memcmp(original.peer_cert_digest, |
| 2020 | restored.peer_cert_digest, |
| 2021 | original.peer_cert_digest_len) == 0); |
Jerry Yu | f092629 | 2022-07-15 13:05:57 +0800 | [diff] [blame] | 2022 | } |
Manuel Pégourié-Gonnard | ee13a73 | 2019-07-29 13:00:39 +0200 | [diff] [blame] | 2023 | #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */ |
Ronald Cron | e68ab4f | 2022-10-05 12:46:29 +0200 | [diff] [blame] | 2024 | #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2025 | TEST_ASSERT(original.verify_result == restored.verify_result); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 2026 | |
Jerry Yu | f092629 | 2022-07-15 13:05:57 +0800 | [diff] [blame] | 2027 | #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2028 | TEST_ASSERT(original.mfl_code == restored.mfl_code); |
Jerry Yu | f092629 | 2022-07-15 13:05:57 +0800 | [diff] [blame] | 2029 | #endif |
| 2030 | |
| 2031 | #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2032 | TEST_ASSERT(original.encrypt_then_mac == restored.encrypt_then_mac); |
Jerry Yu | f092629 | 2022-07-15 13:05:57 +0800 | [diff] [blame] | 2033 | #endif |
Jerry Yu | 6ac7c03 | 2022-07-21 23:11:55 +0800 | [diff] [blame] | 2034 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2035 | TEST_ASSERT(original.ticket_len == restored.ticket_len); |
| 2036 | if (original.ticket_len != 0) { |
| 2037 | TEST_ASSERT(original.ticket != NULL); |
| 2038 | TEST_ASSERT(restored.ticket != NULL); |
| 2039 | TEST_ASSERT(memcmp(original.ticket, |
| 2040 | restored.ticket, original.ticket_len) == 0); |
Jerry Yu | 6ac7c03 | 2022-07-21 23:11:55 +0800 | [diff] [blame] | 2041 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2042 | TEST_ASSERT(original.ticket_lifetime == restored.ticket_lifetime); |
Jerry Yu | 6ac7c03 | 2022-07-21 23:11:55 +0800 | [diff] [blame] | 2043 | #endif |
Jerry Yu | f092629 | 2022-07-15 13:05:57 +0800 | [diff] [blame] | 2044 | } |
Jerry Yu | 6ac7c03 | 2022-07-21 23:11:55 +0800 | [diff] [blame] | 2045 | #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */ |
| 2046 | |
| 2047 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2048 | if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
| 2049 | TEST_ASSERT(original.endpoint == restored.endpoint); |
| 2050 | TEST_ASSERT(original.ciphersuite == restored.ciphersuite); |
| 2051 | TEST_ASSERT(original.ticket_age_add == restored.ticket_age_add); |
| 2052 | TEST_ASSERT(original.ticket_flags == restored.ticket_flags); |
| 2053 | TEST_ASSERT(original.resumption_key_len == restored.resumption_key_len); |
| 2054 | if (original.resumption_key_len != 0) { |
| 2055 | TEST_ASSERT(original.resumption_key != NULL); |
| 2056 | TEST_ASSERT(restored.resumption_key != NULL); |
| 2057 | TEST_ASSERT(memcmp(original.resumption_key, |
| 2058 | restored.resumption_key, |
| 2059 | original.resumption_key_len) == 0); |
Jerry Yu | 6ac7c03 | 2022-07-21 23:11:55 +0800 | [diff] [blame] | 2060 | } |
Jerry Yu | 34e9516 | 2022-12-12 15:14:56 +0800 | [diff] [blame] | 2061 | |
| 2062 | #if defined(MBEDTLS_SSL_EARLY_DATA) |
| 2063 | TEST_ASSERT( |
| 2064 | original.max_early_data_size == restored.max_early_data_size); |
| 2065 | #endif |
| 2066 | |
Jerry Yu | 6ac7c03 | 2022-07-21 23:11:55 +0800 | [diff] [blame] | 2067 | #if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2068 | if (endpoint_type == MBEDTLS_SSL_IS_SERVER) { |
Jerry Yu | 25ba4d4 | 2023-11-10 14:12:20 +0800 | [diff] [blame] | 2069 | TEST_ASSERT(original.ticket_creation_time == restored.ticket_creation_time); |
Jerry Yu | 6ac7c03 | 2022-07-21 23:11:55 +0800 | [diff] [blame] | 2070 | } |
Jerry Yu | f092629 | 2022-07-15 13:05:57 +0800 | [diff] [blame] | 2071 | #endif |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 2072 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2073 | if (endpoint_type == MBEDTLS_SSL_IS_CLIENT) { |
Jerry Yu | 6ac7c03 | 2022-07-21 23:11:55 +0800 | [diff] [blame] | 2074 | #if defined(MBEDTLS_HAVE_TIME) |
Jerry Yu | 342a555 | 2023-11-10 14:23:39 +0800 | [diff] [blame] | 2075 | TEST_ASSERT(original.ticket_reception_time == restored.ticket_reception_time); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 2076 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2077 | TEST_ASSERT(original.ticket_lifetime == restored.ticket_lifetime); |
| 2078 | TEST_ASSERT(original.ticket_len == restored.ticket_len); |
| 2079 | if (original.ticket_len != 0) { |
| 2080 | TEST_ASSERT(original.ticket != NULL); |
| 2081 | TEST_ASSERT(restored.ticket != NULL); |
| 2082 | TEST_ASSERT(memcmp(original.ticket, |
| 2083 | restored.ticket, |
| 2084 | original.ticket_len) == 0); |
Jerry Yu | 6ac7c03 | 2022-07-21 23:11:55 +0800 | [diff] [blame] | 2085 | } |
| 2086 | |
| 2087 | } |
| 2088 | #endif |
| 2089 | } |
| 2090 | #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */ |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 2091 | |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 2092 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2093 | mbedtls_ssl_session_free(&original); |
| 2094 | mbedtls_ssl_session_free(&restored); |
| 2095 | mbedtls_free(buf); |
Valerio Setti | 285dae8 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 2096 | USE_PSA_DONE(); |
Manuel Pégourié-Gonnard | f9deaec | 2019-05-24 09:41:39 +0200 | [diff] [blame] | 2097 | } |
| 2098 | /* END_CASE */ |
| 2099 | |
Manuel Pégourié-Gonnard | aa75583 | 2019-06-03 10:53:47 +0200 | [diff] [blame] | 2100 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2101 | void ssl_serialize_session_load_save(int ticket_len, char *crt_file, |
| 2102 | int endpoint_type, int tls_version) |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 2103 | { |
| 2104 | mbedtls_ssl_session session; |
| 2105 | unsigned char *buf1 = NULL, *buf2 = NULL; |
| 2106 | size_t len0, len1, len2; |
| 2107 | |
| 2108 | /* |
| 2109 | * Test that a load-save pair is the identity |
| 2110 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2111 | mbedtls_ssl_session_init(&session); |
valerio | 32f2ac9 | 2023-04-20 11:59:52 +0200 | [diff] [blame] | 2112 | USE_PSA_INIT(); |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 2113 | |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 2114 | /* Prepare a dummy session to work on */ |
Jerry Yu | a180f99 | 2022-07-15 12:52:54 +0800 | [diff] [blame] | 2115 | ((void) endpoint_type); |
| 2116 | ((void) tls_version); |
Jerry Yu | 28547c4 | 2023-10-31 14:42:50 +0800 | [diff] [blame] | 2117 | ((void) ticket_len); |
| 2118 | ((void) crt_file); |
Jerry Yu | a180f99 | 2022-07-15 12:52:54 +0800 | [diff] [blame] | 2119 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2120 | if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2121 | TEST_ASSERT(mbedtls_test_ssl_tls13_populate_session( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2122 | &session, 0, endpoint_type) == 0); |
Jerry Yu | 28547c4 | 2023-10-31 14:42:50 +0800 | [diff] [blame] | 2123 | } |
Jerry Yu | a180f99 | 2022-07-15 12:52:54 +0800 | [diff] [blame] | 2124 | #endif |
Jerry Yu | 28547c4 | 2023-10-31 14:42:50 +0800 | [diff] [blame] | 2125 | |
| 2126 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 2127 | if (tls_version == MBEDTLS_SSL_VERSION_TLS1_2) { |
| 2128 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2129 | TEST_ASSERT(mbedtls_test_ssl_tls12_populate_session( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2130 | &session, ticket_len, crt_file) == 0); |
Jerry Yu | a180f99 | 2022-07-15 12:52:54 +0800 | [diff] [blame] | 2131 | } |
Jerry Yu | 28547c4 | 2023-10-31 14:42:50 +0800 | [diff] [blame] | 2132 | #endif |
Manuel Pégourié-Gonnard | 3caa6ca | 2019-05-23 10:06:14 +0200 | [diff] [blame] | 2133 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 2134 | /* Get desired buffer size for serializing */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2135 | TEST_ASSERT(mbedtls_ssl_session_save(&session, NULL, 0, &len0) |
| 2136 | == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL); |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 2137 | |
| 2138 | /* Allocate first buffer */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2139 | buf1 = mbedtls_calloc(1, len0); |
| 2140 | TEST_ASSERT(buf1 != NULL); |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 2141 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 2142 | /* Serialize to buffer and free live session */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2143 | TEST_ASSERT(mbedtls_ssl_session_save(&session, buf1, len0, &len1) |
| 2144 | == 0); |
| 2145 | TEST_ASSERT(len0 == len1); |
| 2146 | mbedtls_ssl_session_free(&session); |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 2147 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 2148 | /* Restore session from serialized data */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2149 | TEST_ASSERT(mbedtls_ssl_session_load(&session, buf1, len1) == 0); |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 2150 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 2151 | /* Allocate second buffer and serialize to it */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2152 | buf2 = mbedtls_calloc(1, len0); |
| 2153 | TEST_ASSERT(buf2 != NULL); |
| 2154 | TEST_ASSERT(mbedtls_ssl_session_save(&session, buf2, len0, &len2) |
| 2155 | == 0); |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 2156 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 2157 | /* Make sure both serialized versions are identical */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2158 | TEST_ASSERT(len1 == len2); |
| 2159 | TEST_ASSERT(memcmp(buf1, buf2, len1) == 0); |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 2160 | |
| 2161 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2162 | mbedtls_ssl_session_free(&session); |
| 2163 | mbedtls_free(buf1); |
| 2164 | mbedtls_free(buf2); |
Valerio Setti | 285dae8 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 2165 | USE_PSA_DONE(); |
Manuel Pégourié-Gonnard | 6eac11b | 2019-05-23 09:30:55 +0200 | [diff] [blame] | 2166 | } |
| 2167 | /* END_CASE */ |
Manuel Pégourié-Gonnard | f5fa0aa | 2019-05-23 10:38:11 +0200 | [diff] [blame] | 2168 | |
| 2169 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2170 | void ssl_serialize_session_save_buf_size(int ticket_len, char *crt_file, |
| 2171 | int endpoint_type, int tls_version) |
Manuel Pégourié-Gonnard | f5fa0aa | 2019-05-23 10:38:11 +0200 | [diff] [blame] | 2172 | { |
| 2173 | mbedtls_ssl_session session; |
| 2174 | unsigned char *buf = NULL; |
| 2175 | size_t good_len, bad_len, test_len; |
| 2176 | |
| 2177 | /* |
| 2178 | * Test that session_save() fails cleanly on small buffers |
| 2179 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2180 | mbedtls_ssl_session_init(&session); |
valerio | 32f2ac9 | 2023-04-20 11:59:52 +0200 | [diff] [blame] | 2181 | USE_PSA_INIT(); |
Manuel Pégourié-Gonnard | f5fa0aa | 2019-05-23 10:38:11 +0200 | [diff] [blame] | 2182 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 2183 | /* Prepare dummy session and get serialized size */ |
Jerry Yu | 1ac476c | 2022-07-15 11:22:40 +0800 | [diff] [blame] | 2184 | ((void) endpoint_type); |
| 2185 | ((void) tls_version); |
Jerry Yu | 28547c4 | 2023-10-31 14:42:50 +0800 | [diff] [blame] | 2186 | ((void) ticket_len); |
| 2187 | ((void) crt_file); |
Jerry Yu | 1ac476c | 2022-07-15 11:22:40 +0800 | [diff] [blame] | 2188 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2189 | if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2190 | TEST_ASSERT(mbedtls_test_ssl_tls13_populate_session( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2191 | &session, 0, endpoint_type) == 0); |
Jerry Yu | 28547c4 | 2023-10-31 14:42:50 +0800 | [diff] [blame] | 2192 | } |
Jerry Yu | 1ac476c | 2022-07-15 11:22:40 +0800 | [diff] [blame] | 2193 | #endif |
Jerry Yu | 28547c4 | 2023-10-31 14:42:50 +0800 | [diff] [blame] | 2194 | |
| 2195 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 2196 | if (tls_version == MBEDTLS_SSL_VERSION_TLS1_2) { |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2197 | TEST_ASSERT(mbedtls_test_ssl_tls12_populate_session( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2198 | &session, ticket_len, crt_file) == 0); |
Jerry Yu | 1ac476c | 2022-07-15 11:22:40 +0800 | [diff] [blame] | 2199 | } |
Jerry Yu | 28547c4 | 2023-10-31 14:42:50 +0800 | [diff] [blame] | 2200 | #endif |
| 2201 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2202 | TEST_ASSERT(mbedtls_ssl_session_save(&session, NULL, 0, &good_len) |
| 2203 | == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL); |
Manuel Pégourié-Gonnard | f5fa0aa | 2019-05-23 10:38:11 +0200 | [diff] [blame] | 2204 | |
| 2205 | /* Try all possible bad lengths */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2206 | for (bad_len = 1; bad_len < good_len; bad_len++) { |
Manuel Pégourié-Gonnard | f5fa0aa | 2019-05-23 10:38:11 +0200 | [diff] [blame] | 2207 | /* Allocate exact size so that asan/valgrind can detect any overwrite */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2208 | mbedtls_free(buf); |
Gilles Peskine | d681ffd | 2023-10-17 17:31:50 +0200 | [diff] [blame] | 2209 | buf = NULL; |
| 2210 | TEST_CALLOC(buf, bad_len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2211 | TEST_ASSERT(mbedtls_ssl_session_save(&session, buf, bad_len, |
| 2212 | &test_len) |
| 2213 | == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL); |
| 2214 | TEST_ASSERT(test_len == good_len); |
Manuel Pégourié-Gonnard | f5fa0aa | 2019-05-23 10:38:11 +0200 | [diff] [blame] | 2215 | } |
| 2216 | |
| 2217 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2218 | mbedtls_ssl_session_free(&session); |
| 2219 | mbedtls_free(buf); |
Valerio Setti | 285dae8 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 2220 | USE_PSA_DONE(); |
Manuel Pégourié-Gonnard | f5fa0aa | 2019-05-23 10:38:11 +0200 | [diff] [blame] | 2221 | } |
| 2222 | /* END_CASE */ |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 2223 | |
| 2224 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2225 | void ssl_serialize_session_load_buf_size(int ticket_len, char *crt_file, |
| 2226 | int endpoint_type, int tls_version) |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 2227 | { |
| 2228 | mbedtls_ssl_session session; |
| 2229 | unsigned char *good_buf = NULL, *bad_buf = NULL; |
| 2230 | size_t good_len, bad_len; |
| 2231 | |
| 2232 | /* |
| 2233 | * Test that session_load() fails cleanly on small buffers |
| 2234 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2235 | mbedtls_ssl_session_init(&session); |
valerio | 32f2ac9 | 2023-04-20 11:59:52 +0200 | [diff] [blame] | 2236 | USE_PSA_INIT(); |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 2237 | |
Manuel Pégourié-Gonnard | 686adb4 | 2019-06-03 09:55:16 +0200 | [diff] [blame] | 2238 | /* Prepare serialized session data */ |
Jerry Yu | 6e8fec2 | 2022-07-15 10:37:02 +0800 | [diff] [blame] | 2239 | ((void) endpoint_type); |
| 2240 | ((void) tls_version); |
Jerry Yu | 28547c4 | 2023-10-31 14:42:50 +0800 | [diff] [blame] | 2241 | ((void) ticket_len); |
| 2242 | ((void) crt_file); |
Jerry Yu | 6e8fec2 | 2022-07-15 10:37:02 +0800 | [diff] [blame] | 2243 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2244 | if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2245 | TEST_ASSERT(mbedtls_test_ssl_tls13_populate_session( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2246 | &session, 0, endpoint_type) == 0); |
Jerry Yu | 28547c4 | 2023-10-31 14:42:50 +0800 | [diff] [blame] | 2247 | } |
Jerry Yu | 6e8fec2 | 2022-07-15 10:37:02 +0800 | [diff] [blame] | 2248 | #endif |
Jerry Yu | 28547c4 | 2023-10-31 14:42:50 +0800 | [diff] [blame] | 2249 | |
| 2250 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 2251 | if (tls_version == MBEDTLS_SSL_VERSION_TLS1_2) { |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2252 | TEST_ASSERT(mbedtls_test_ssl_tls12_populate_session( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2253 | &session, ticket_len, crt_file) == 0); |
Jerry Yu | 6e8fec2 | 2022-07-15 10:37:02 +0800 | [diff] [blame] | 2254 | } |
Jerry Yu | 28547c4 | 2023-10-31 14:42:50 +0800 | [diff] [blame] | 2255 | #endif |
| 2256 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2257 | TEST_ASSERT(mbedtls_ssl_session_save(&session, NULL, 0, &good_len) |
| 2258 | == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL); |
Gilles Peskine | d681ffd | 2023-10-17 17:31:50 +0200 | [diff] [blame] | 2259 | TEST_CALLOC(good_buf, good_len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2260 | TEST_ASSERT(mbedtls_ssl_session_save(&session, good_buf, good_len, |
| 2261 | &good_len) == 0); |
| 2262 | mbedtls_ssl_session_free(&session); |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 2263 | |
| 2264 | /* Try all possible bad lengths */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2265 | for (bad_len = 0; bad_len < good_len; bad_len++) { |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 2266 | /* Allocate exact size so that asan/valgrind can detect any overread */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2267 | mbedtls_free(bad_buf); |
Gilles Peskine | d681ffd | 2023-10-17 17:31:50 +0200 | [diff] [blame] | 2268 | bad_buf = NULL; |
| 2269 | TEST_CALLOC_NONNULL(bad_buf, bad_len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2270 | memcpy(bad_buf, good_buf, bad_len); |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 2271 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2272 | TEST_ASSERT(mbedtls_ssl_session_load(&session, bad_buf, bad_len) |
| 2273 | == MBEDTLS_ERR_SSL_BAD_INPUT_DATA); |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 2274 | } |
| 2275 | |
| 2276 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2277 | mbedtls_ssl_session_free(&session); |
| 2278 | mbedtls_free(good_buf); |
| 2279 | mbedtls_free(bad_buf); |
Valerio Setti | 285dae8 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 2280 | USE_PSA_DONE(); |
Manuel Pégourié-Gonnard | a3d831b | 2019-05-23 12:28:45 +0200 | [diff] [blame] | 2281 | } |
| 2282 | /* END_CASE */ |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 2283 | |
Hanno Becker | 363b646 | 2019-05-29 12:44:28 +0100 | [diff] [blame] | 2284 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2285 | void ssl_session_serialize_version_check(int corrupt_major, |
| 2286 | int corrupt_minor, |
| 2287 | int corrupt_patch, |
| 2288 | int corrupt_config, |
| 2289 | int endpoint_type, |
| 2290 | int tls_version) |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 2291 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2292 | unsigned char serialized_session[2048]; |
Hanno Becker | 363b646 | 2019-05-29 12:44:28 +0100 | [diff] [blame] | 2293 | size_t serialized_session_len; |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 2294 | unsigned cur_byte; |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 2295 | mbedtls_ssl_session session; |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 2296 | uint8_t should_corrupt_byte[] = { corrupt_major == 1, |
| 2297 | corrupt_minor == 1, |
| 2298 | corrupt_patch == 1, |
| 2299 | corrupt_config == 1, |
| 2300 | corrupt_config == 1 }; |
| 2301 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2302 | mbedtls_ssl_session_init(&session); |
valerio | 32f2ac9 | 2023-04-20 11:59:52 +0200 | [diff] [blame] | 2303 | USE_PSA_INIT(); |
Jerry Yu | 534ff40 | 2022-07-14 16:43:43 +0800 | [diff] [blame] | 2304 | ((void) endpoint_type); |
| 2305 | ((void) tls_version); |
| 2306 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2307 | if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) { |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2308 | TEST_ASSERT(mbedtls_test_ssl_tls13_populate_session( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2309 | &session, 0, endpoint_type) == 0); |
Jerry Yu | 28547c4 | 2023-10-31 14:42:50 +0800 | [diff] [blame] | 2310 | } |
Jerry Yu | 534ff40 | 2022-07-14 16:43:43 +0800 | [diff] [blame] | 2311 | #endif |
Jerry Yu | 534ff40 | 2022-07-14 16:43:43 +0800 | [diff] [blame] | 2312 | |
Jerry Yu | 28547c4 | 2023-10-31 14:42:50 +0800 | [diff] [blame] | 2313 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
| 2314 | if (tls_version == MBEDTLS_SSL_VERSION_TLS1_2) { |
| 2315 | TEST_ASSERT(mbedtls_test_ssl_tls12_populate_session( |
| 2316 | &session, 0, NULL) == 0); |
| 2317 | } |
| 2318 | #endif |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 2319 | |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 2320 | /* Infer length of serialized session. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2321 | TEST_ASSERT(mbedtls_ssl_session_save(&session, |
| 2322 | serialized_session, |
| 2323 | sizeof(serialized_session), |
| 2324 | &serialized_session_len) == 0); |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 2325 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2326 | mbedtls_ssl_session_free(&session); |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 2327 | |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 2328 | /* Without any modification, we should be able to successfully |
Hanno Becker | 363b646 | 2019-05-29 12:44:28 +0100 | [diff] [blame] | 2329 | * de-serialize the session - double-check that. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2330 | TEST_ASSERT(mbedtls_ssl_session_load(&session, |
| 2331 | serialized_session, |
| 2332 | serialized_session_len) == 0); |
| 2333 | mbedtls_ssl_session_free(&session); |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 2334 | |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 2335 | /* Go through the bytes in the serialized session header and |
| 2336 | * corrupt them bit-by-bit. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2337 | for (cur_byte = 0; cur_byte < sizeof(should_corrupt_byte); cur_byte++) { |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 2338 | int cur_bit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2339 | unsigned char * const byte = &serialized_session[cur_byte]; |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 2340 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2341 | if (should_corrupt_byte[cur_byte] == 0) { |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 2342 | continue; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2343 | } |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 2344 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2345 | for (cur_bit = 0; cur_bit < CHAR_BIT; cur_bit++) { |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 2346 | unsigned char const corrupted_bit = 0x1u << cur_bit; |
| 2347 | /* Modify a single bit in the serialized session. */ |
| 2348 | *byte ^= corrupted_bit; |
| 2349 | |
| 2350 | /* Attempt to deserialize */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2351 | TEST_ASSERT(mbedtls_ssl_session_load(&session, |
| 2352 | serialized_session, |
| 2353 | serialized_session_len) == |
| 2354 | MBEDTLS_ERR_SSL_VERSION_MISMATCH); |
Hanno Becker | fe1275e | 2019-05-29 12:45:21 +0100 | [diff] [blame] | 2355 | |
| 2356 | /* Undo the change */ |
| 2357 | *byte ^= corrupted_bit; |
| 2358 | } |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 2359 | } |
valerio | 32f2ac9 | 2023-04-20 11:59:52 +0200 | [diff] [blame] | 2360 | exit: |
Valerio Setti | 285dae8 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 2361 | USE_PSA_DONE(); |
Hanno Becker | 861d0bb | 2019-05-21 16:39:30 +0100 | [diff] [blame] | 2362 | } |
| 2363 | /* END_CASE */ |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 2364 | |
Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 2365 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED:MBEDTLS_RSA_C:MBEDTLS_ECP_HAVE_SECP384R1:!MBEDTLS_SSL_PROTO_TLS1_3:MBEDTLS_PKCS1_V15:MBEDTLS_MD_CAN_SHA256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2366 | void mbedtls_endpoint_sanity(int endpoint_type) |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 2367 | { |
| 2368 | enum { BUFFSIZE = 1024 }; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 2369 | mbedtls_test_ssl_endpoint ep; |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 2370 | int ret = -1; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 2371 | mbedtls_test_handshake_test_options options; |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2372 | mbedtls_test_init_handshake_options(&options); |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 2373 | options.pk_alg = MBEDTLS_PK_RSA; |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 2374 | |
Manuel Pégourié-Gonnard | 23fc437 | 2023-03-17 13:34:11 +0100 | [diff] [blame] | 2375 | MD_OR_USE_PSA_INIT(); |
| 2376 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2377 | ret = mbedtls_test_ssl_endpoint_init(NULL, endpoint_type, &options, |
| 2378 | NULL, NULL, NULL, NULL); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2379 | TEST_ASSERT(MBEDTLS_ERR_SSL_BAD_INPUT_DATA == ret); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 2380 | |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 2381 | ret = mbedtls_test_ssl_endpoint_certificate_init(NULL, options.pk_alg, |
| 2382 | 0, 0, 0); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2383 | TEST_ASSERT(MBEDTLS_ERR_SSL_BAD_INPUT_DATA == ret); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 2384 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2385 | ret = mbedtls_test_ssl_endpoint_init(&ep, endpoint_type, &options, |
| 2386 | NULL, NULL, NULL, NULL); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2387 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 2388 | |
| 2389 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2390 | mbedtls_test_ssl_endpoint_free(&ep, NULL); |
| 2391 | mbedtls_test_free_handshake_options(&options); |
Manuel Pégourié-Gonnard | 23fc437 | 2023-03-17 13:34:11 +0100 | [diff] [blame] | 2392 | MD_OR_USE_PSA_DONE(); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 2393 | } |
| 2394 | /* END_CASE */ |
| 2395 | |
Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 2396 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED:MBEDTLS_RSA_C:MBEDTLS_ECP_HAVE_SECP384R1:MBEDTLS_PKCS1_V15:MBEDTLS_MD_CAN_SHA256:MBEDTLS_PK_HAVE_ECC_KEYS */ |
Ronald Cron | 43263c0 | 2023-03-09 16:48:10 +0100 | [diff] [blame] | 2397 | void move_handshake_to_state(int endpoint_type, int tls_version, int state, int need_pass) |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 2398 | { |
| 2399 | enum { BUFFSIZE = 1024 }; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 2400 | mbedtls_test_ssl_endpoint base_ep, second_ep; |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 2401 | int ret = -1; |
Ronald Cron | 43263c0 | 2023-03-09 16:48:10 +0100 | [diff] [blame] | 2402 | (void) tls_version; |
| 2403 | |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 2404 | mbedtls_test_handshake_test_options options; |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2405 | mbedtls_test_init_handshake_options(&options); |
Ronald Cron | 43263c0 | 2023-03-09 16:48:10 +0100 | [diff] [blame] | 2406 | |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 2407 | options.pk_alg = MBEDTLS_PK_RSA; |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 2408 | |
Ronald Cron | 43263c0 | 2023-03-09 16:48:10 +0100 | [diff] [blame] | 2409 | /* |
Ronald Cron | 097ba14 | 2023-03-08 16:18:00 +0100 | [diff] [blame] | 2410 | * If both TLS 1.2 and 1.3 are enabled and we want to do a TLS 1.2 |
| 2411 | * handshake, force the TLS 1.2 version on endpoint under test. |
Ronald Cron | 43263c0 | 2023-03-09 16:48:10 +0100 | [diff] [blame] | 2412 | */ |
| 2413 | #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Ronald Cron | 097ba14 | 2023-03-08 16:18:00 +0100 | [diff] [blame] | 2414 | if (MBEDTLS_SSL_VERSION_TLS1_2 == tls_version) { |
| 2415 | if (MBEDTLS_SSL_IS_CLIENT == endpoint_type) { |
| 2416 | options.client_min_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 2417 | options.client_max_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 2418 | } else { |
| 2419 | options.server_min_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 2420 | options.server_max_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 2421 | } |
Ronald Cron | 43263c0 | 2023-03-09 16:48:10 +0100 | [diff] [blame] | 2422 | } |
| 2423 | #endif |
| 2424 | |
Manuel Pégourié-Gonnard | 23fc437 | 2023-03-17 13:34:11 +0100 | [diff] [blame] | 2425 | MD_OR_USE_PSA_INIT(); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2426 | mbedtls_platform_zeroize(&base_ep, sizeof(base_ep)); |
| 2427 | mbedtls_platform_zeroize(&second_ep, sizeof(second_ep)); |
Neil Armstrong | 06baf04 | 2022-04-14 16:21:15 +0200 | [diff] [blame] | 2428 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2429 | ret = mbedtls_test_ssl_endpoint_init(&base_ep, endpoint_type, &options, |
| 2430 | NULL, NULL, NULL, NULL); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2431 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 2432 | |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 2433 | ret = mbedtls_test_ssl_endpoint_init( |
| 2434 | &second_ep, |
| 2435 | (endpoint_type == MBEDTLS_SSL_IS_SERVER) ? |
| 2436 | MBEDTLS_SSL_IS_CLIENT : MBEDTLS_SSL_IS_SERVER, |
| 2437 | &options, NULL, NULL, NULL, NULL); |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 2438 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2439 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 2440 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2441 | ret = mbedtls_test_mock_socket_connect(&(base_ep.socket), |
| 2442 | &(second_ep.socket), |
| 2443 | BUFFSIZE); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2444 | TEST_ASSERT(ret == 0); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 2445 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2446 | ret = mbedtls_test_move_handshake_to_state(&(base_ep.ssl), |
| 2447 | &(second_ep.ssl), |
| 2448 | state); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2449 | if (need_pass) { |
| 2450 | TEST_ASSERT(ret == 0 || |
| 2451 | ret == MBEDTLS_ERR_SSL_WANT_READ || |
| 2452 | ret == MBEDTLS_ERR_SSL_WANT_WRITE); |
| 2453 | TEST_ASSERT(base_ep.ssl.state == state); |
| 2454 | } else { |
| 2455 | TEST_ASSERT(ret != 0 && |
| 2456 | ret != MBEDTLS_ERR_SSL_WANT_READ && |
| 2457 | ret != MBEDTLS_ERR_SSL_WANT_WRITE); |
| 2458 | TEST_ASSERT(base_ep.ssl.state != state); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 2459 | } |
| 2460 | |
| 2461 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2462 | mbedtls_test_free_handshake_options(&options); |
| 2463 | mbedtls_test_ssl_endpoint_free(&base_ep, NULL); |
| 2464 | mbedtls_test_ssl_endpoint_free(&second_ep, NULL); |
Manuel Pégourié-Gonnard | 23fc437 | 2023-03-17 13:34:11 +0100 | [diff] [blame] | 2465 | MD_OR_USE_PSA_DONE(); |
Piotr Nowicki | 2a1f178 | 2020-01-13 09:42:10 +0100 | [diff] [blame] | 2466 | } |
| 2467 | /* END_CASE */ |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 2468 | |
Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 2469 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED:MBEDTLS_PKCS1_V15:MBEDTLS_RSA_C:MBEDTLS_ECP_HAVE_SECP384R1:MBEDTLS_MD_CAN_SHA256:MBEDTLS_PK_HAVE_ECC_KEYS:MBEDTLS_CAN_HANDLE_RSA_TEST_KEY */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2470 | void handshake_version(int dtls, int client_min_version, int client_max_version, |
| 2471 | int server_min_version, int server_max_version, |
| 2472 | int expected_negotiated_version) |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 2473 | { |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 2474 | mbedtls_test_handshake_test_options options; |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2475 | mbedtls_test_init_handshake_options(&options); |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 2476 | |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 2477 | options.client_min_version = client_min_version; |
| 2478 | options.client_max_version = client_max_version; |
| 2479 | options.server_min_version = server_min_version; |
| 2480 | options.server_max_version = server_max_version; |
Paul Elliott | c857044 | 2020-04-15 17:00:50 +0100 | [diff] [blame] | 2481 | options.expected_negotiated_version = expected_negotiated_version; |
| 2482 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2483 | options.dtls = dtls; |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2484 | mbedtls_test_ssl_perform_handshake(&options); |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 2485 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2486 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 2487 | goto exit; |
Andrzej Kurek | e11acb2 | 2022-06-27 06:11:34 -0400 | [diff] [blame] | 2488 | |
| 2489 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2490 | mbedtls_test_free_handshake_options(&options); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2491 | } |
| 2492 | /* END_CASE */ |
Andrzej Kurek | 9e9efdc | 2020-02-26 05:25:23 -0500 | [diff] [blame] | 2493 | |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 2494 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_MD_CAN_SHA256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2495 | 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] | 2496 | { |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 2497 | mbedtls_test_handshake_test_options options; |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2498 | mbedtls_test_init_handshake_options(&options); |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 2499 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2500 | options.cipher = cipher; |
| 2501 | options.dtls = dtls; |
| 2502 | options.psk_str = psk_str; |
| 2503 | options.pk_alg = pk_alg; |
Andrzej Kurek | cc5169c | 2020-02-04 09:04:56 -0500 | [diff] [blame] | 2504 | |
Ronald Cron | 097ba14 | 2023-03-08 16:18:00 +0100 | [diff] [blame] | 2505 | options.client_min_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 2506 | options.client_max_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 2507 | options.expected_negotiated_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 2508 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2509 | mbedtls_test_ssl_perform_handshake(&options); |
Andrzej Kurek | 316da1f | 2020-02-26 09:03:47 -0500 | [diff] [blame] | 2510 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2511 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 2512 | goto exit; |
Andrzej Kurek | e11acb2 | 2022-06-27 06:11:34 -0400 | [diff] [blame] | 2513 | |
Andrzej Kurek | ddb8cd6 | 2022-07-04 16:07:28 -0400 | [diff] [blame] | 2514 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2515 | mbedtls_test_free_handshake_options(&options); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2516 | } |
| 2517 | /* END_CASE */ |
Andrzej Kurek | 316da1f | 2020-02-26 09:03:47 -0500 | [diff] [blame] | 2518 | |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 2519 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_MD_CAN_SHA256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2520 | void handshake_cipher(char *cipher, int pk_alg, int dtls) |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2521 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2522 | test_handshake_psk_cipher(cipher, pk_alg, NULL, dtls); |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 2523 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2524 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 2525 | goto exit; |
| 2526 | } |
| 2527 | /* END_CASE */ |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 2528 | |
Manuel Pégourié-Gonnard | bef824d | 2023-03-17 12:50:01 +0100 | [diff] [blame] | 2529 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_MD_CAN_SHA256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2530 | void handshake_ciphersuite_select(char *cipher, int pk_alg, data_t *psk_str, |
| 2531 | int psa_alg, int psa_alg2, int psa_usage, |
| 2532 | int expected_handshake_result, |
| 2533 | int expected_ciphersuite) |
Neil Armstrong | 8c52ed8 | 2022-05-27 13:14:55 +0200 | [diff] [blame] | 2534 | { |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 2535 | mbedtls_test_handshake_test_options options; |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2536 | mbedtls_test_init_handshake_options(&options); |
Neil Armstrong | 8c52ed8 | 2022-05-27 13:14:55 +0200 | [diff] [blame] | 2537 | |
| 2538 | options.cipher = cipher; |
Neil Armstrong | db13497 | 2022-06-30 09:06:28 +0200 | [diff] [blame] | 2539 | options.psk_str = psk_str; |
Neil Armstrong | 8c52ed8 | 2022-05-27 13:14:55 +0200 | [diff] [blame] | 2540 | options.pk_alg = pk_alg; |
| 2541 | options.opaque_alg = psa_alg; |
| 2542 | options.opaque_alg2 = psa_alg2; |
| 2543 | options.opaque_usage = psa_usage; |
| 2544 | options.expected_handshake_result = expected_handshake_result; |
| 2545 | options.expected_ciphersuite = expected_ciphersuite; |
Ronald Cron | 097ba14 | 2023-03-08 16:18:00 +0100 | [diff] [blame] | 2546 | |
| 2547 | options.server_min_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 2548 | options.server_max_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 2549 | options.expected_negotiated_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 2550 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2551 | mbedtls_test_ssl_perform_handshake(&options); |
Neil Armstrong | 8c52ed8 | 2022-05-27 13:14:55 +0200 | [diff] [blame] | 2552 | |
| 2553 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 2554 | goto exit; |
Andrzej Kurek | ddb8cd6 | 2022-07-04 16:07:28 -0400 | [diff] [blame] | 2555 | |
| 2556 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2557 | mbedtls_test_free_handshake_options(&options); |
Neil Armstrong | 8c52ed8 | 2022-05-27 13:14:55 +0200 | [diff] [blame] | 2558 | } |
| 2559 | /* END_CASE */ |
| 2560 | |
Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 2561 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED:MBEDTLS_PKCS1_V15:MBEDTLS_RSA_C:MBEDTLS_ECP_HAVE_SECP384R1:MBEDTLS_MD_CAN_SHA256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2562 | void app_data(int mfl, int cli_msg_len, int srv_msg_len, |
| 2563 | int expected_cli_fragments, |
| 2564 | int expected_srv_fragments, int dtls) |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2565 | { |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 2566 | mbedtls_test_handshake_test_options options; |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2567 | mbedtls_test_init_handshake_options(&options); |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 2568 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2569 | options.mfl = mfl; |
| 2570 | options.cli_msg_len = cli_msg_len; |
| 2571 | options.srv_msg_len = srv_msg_len; |
| 2572 | options.expected_cli_fragments = expected_cli_fragments; |
| 2573 | options.expected_srv_fragments = expected_srv_fragments; |
| 2574 | options.dtls = dtls; |
Ronald Cron | 097ba14 | 2023-03-08 16:18:00 +0100 | [diff] [blame] | 2575 | |
| 2576 | options.client_min_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 2577 | options.client_max_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 2578 | options.expected_negotiated_version = MBEDTLS_SSL_VERSION_TLS1_2; |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 2579 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2580 | mbedtls_test_ssl_perform_handshake(&options); |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 2581 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2582 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 2583 | goto exit; |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 2584 | |
| 2585 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2586 | mbedtls_test_free_handshake_options(&options); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2587 | } |
| 2588 | /* END_CASE */ |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 2589 | |
Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 2590 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED:MBEDTLS_PKCS1_V15:MBEDTLS_RSA_C:MBEDTLS_ECP_HAVE_SECP384R1:MBEDTLS_MD_CAN_SHA256:MBEDTLS_PK_HAVE_ECC_KEYS:MBEDTLS_CAN_HANDLE_RSA_TEST_KEY */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2591 | void app_data_tls(int mfl, int cli_msg_len, int srv_msg_len, |
| 2592 | int expected_cli_fragments, |
| 2593 | int expected_srv_fragments) |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2594 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2595 | test_app_data(mfl, cli_msg_len, srv_msg_len, expected_cli_fragments, |
| 2596 | expected_srv_fragments, 0); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2597 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 2598 | goto exit; |
| 2599 | } |
| 2600 | /* END_CASE */ |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 2601 | |
Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 2602 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED:!MBEDTLS_SSL_PROTO_TLS1_3:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_RSA_C:MBEDTLS_ECP_HAVE_SECP384R1:MBEDTLS_SSL_PROTO_DTLS:MBEDTLS_MD_CAN_SHA256:MBEDTLS_CAN_HANDLE_RSA_TEST_KEY */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2603 | void app_data_dtls(int mfl, int cli_msg_len, int srv_msg_len, |
| 2604 | int expected_cli_fragments, |
| 2605 | int expected_srv_fragments) |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2606 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2607 | test_app_data(mfl, cli_msg_len, srv_msg_len, expected_cli_fragments, |
| 2608 | expected_srv_fragments, 1); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2609 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 2610 | goto exit; |
| 2611 | } |
| 2612 | /* END_CASE */ |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 2613 | |
Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 2614 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_RSA_C:MBEDTLS_ECP_HAVE_SECP384R1:MBEDTLS_SSL_PROTO_DTLS:MBEDTLS_SSL_RENEGOTIATION:MBEDTLS_SSL_CONTEXT_SERIALIZATION:MBEDTLS_MD_CAN_SHA256:MBEDTLS_CAN_HANDLE_RSA_TEST_KEY */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2615 | void handshake_serialization() |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2616 | { |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 2617 | mbedtls_test_handshake_test_options options; |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2618 | mbedtls_test_init_handshake_options(&options); |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 2619 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2620 | options.serialize = 1; |
| 2621 | options.dtls = 1; |
Ronald Cron | 097ba14 | 2023-03-08 16:18:00 +0100 | [diff] [blame] | 2622 | options.expected_negotiated_version = MBEDTLS_SSL_VERSION_TLS1_2; |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2623 | mbedtls_test_ssl_perform_handshake(&options); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2624 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 2625 | goto exit; |
Andrzej Kurek | 6e518ab | 2022-06-11 05:08:38 -0400 | [diff] [blame] | 2626 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2627 | mbedtls_test_free_handshake_options(&options); |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2628 | } |
| 2629 | /* END_CASE */ |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 2630 | |
Pengyu Lv | ba6825e | 2023-11-08 12:16:29 +0800 | [diff] [blame] | 2631 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED:!MBEDTLS_SSL_PROTO_TLS1_3:MBEDTLS_PKCS1_V15:MBEDTLS_RSA_C:MBEDTLS_SSL_HAVE_AES:MBEDTLS_ECP_HAVE_SECP384R1:MBEDTLS_DEBUG_C:MBEDTLS_SSL_MAX_FRAGMENT_LENGTH:MBEDTLS_SSL_HAVE_CBC:MBEDTLS_MD_CAN_SHA256:MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2632 | void handshake_fragmentation(int mfl, |
| 2633 | int expected_srv_hs_fragmentation, |
| 2634 | int expected_cli_hs_fragmentation) |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 2635 | { |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 2636 | mbedtls_test_handshake_test_options options; |
| 2637 | mbedtls_test_ssl_log_pattern srv_pattern, cli_pattern; |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 2638 | |
| 2639 | srv_pattern.pattern = cli_pattern.pattern = "found fragmented DTLS handshake"; |
| 2640 | srv_pattern.counter = 0; |
| 2641 | cli_pattern.counter = 0; |
| 2642 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2643 | mbedtls_test_init_handshake_options(&options); |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 2644 | options.dtls = 1; |
Ronald Cron | 097ba14 | 2023-03-08 16:18:00 +0100 | [diff] [blame] | 2645 | options.expected_negotiated_version = MBEDTLS_SSL_VERSION_TLS1_2; |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 2646 | options.mfl = mfl; |
Darryl Green | aad82f9 | 2019-12-02 10:53:11 +0000 | [diff] [blame] | 2647 | /* Set cipher to one using CBC so that record splitting can be tested */ |
| 2648 | options.cipher = "TLS-DHE-RSA-WITH-AES-256-CBC-SHA256"; |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 2649 | options.srv_auth_mode = MBEDTLS_SSL_VERIFY_REQUIRED; |
| 2650 | options.srv_log_obj = &srv_pattern; |
| 2651 | options.cli_log_obj = &cli_pattern; |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2652 | options.srv_log_fun = mbedtls_test_ssl_log_analyzer; |
| 2653 | options.cli_log_fun = mbedtls_test_ssl_log_analyzer; |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 2654 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2655 | mbedtls_test_ssl_perform_handshake(&options); |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 2656 | |
| 2657 | /* Test if the server received a fragmented handshake */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2658 | if (expected_srv_hs_fragmentation) { |
| 2659 | TEST_ASSERT(srv_pattern.counter >= 1); |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 2660 | } |
| 2661 | /* Test if the client received a fragmented handshake */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2662 | if (expected_cli_hs_fragmentation) { |
| 2663 | TEST_ASSERT(cli_pattern.counter >= 1); |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 2664 | } |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 2665 | |
| 2666 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2667 | mbedtls_test_free_handshake_options(&options); |
Piotr Nowicki | bde7ee8 | 2020-02-21 10:59:50 +0100 | [diff] [blame] | 2668 | } |
| 2669 | /* END_CASE */ |
| 2670 | |
Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 2671 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED:!MBEDTLS_SSL_PROTO_TLS1_3:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_RSA_C:MBEDTLS_ECP_HAVE_SECP384R1:MBEDTLS_SSL_PROTO_DTLS:MBEDTLS_SSL_RENEGOTIATION:MBEDTLS_MD_CAN_SHA256:MBEDTLS_CAN_HANDLE_RSA_TEST_KEY */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2672 | void renegotiation(int legacy_renegotiation) |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2673 | { |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 2674 | mbedtls_test_handshake_test_options options; |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2675 | mbedtls_test_init_handshake_options(&options); |
Andrzej Kurek | da2b678 | 2020-02-12 07:56:36 -0500 | [diff] [blame] | 2676 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2677 | options.renegotiate = 1; |
| 2678 | options.legacy_renegotiation = legacy_renegotiation; |
| 2679 | options.dtls = 1; |
Ronald Cron | 097ba14 | 2023-03-08 16:18:00 +0100 | [diff] [blame] | 2680 | options.expected_negotiated_version = MBEDTLS_SSL_VERSION_TLS1_2; |
Andrzej Kurek | 316da1f | 2020-02-26 09:03:47 -0500 | [diff] [blame] | 2681 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2682 | mbedtls_test_ssl_perform_handshake(&options); |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 2683 | |
Andrzej Kurek | 8a6ff15 | 2020-02-26 09:10:14 -0500 | [diff] [blame] | 2684 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 2685 | goto exit; |
Andrzej Kurek | ddb8cd6 | 2022-07-04 16:07:28 -0400 | [diff] [blame] | 2686 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2687 | mbedtls_test_free_handshake_options(&options); |
Andrzej Kurek | f40daa3 | 2020-02-04 09:00:01 -0500 | [diff] [blame] | 2688 | } |
| 2689 | /* END_CASE */ |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2690 | |
Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 2691 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_RSA_C:MBEDTLS_ECP_HAVE_SECP384R1:MBEDTLS_MD_CAN_SHA256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2692 | void resize_buffers(int mfl, int renegotiation, int legacy_renegotiation, |
| 2693 | int serialize, int dtls, char *cipher) |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2694 | { |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 2695 | mbedtls_test_handshake_test_options options; |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2696 | mbedtls_test_init_handshake_options(&options); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2697 | |
| 2698 | options.mfl = mfl; |
Andrzej Kurek | 8ea6872 | 2020-04-03 06:40:47 -0400 | [diff] [blame] | 2699 | options.cipher = cipher; |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2700 | options.renegotiate = renegotiation; |
| 2701 | options.legacy_renegotiation = legacy_renegotiation; |
| 2702 | options.serialize = serialize; |
| 2703 | options.dtls = dtls; |
Ronald Cron | 097ba14 | 2023-03-08 16:18:00 +0100 | [diff] [blame] | 2704 | if (dtls) { |
| 2705 | options.expected_negotiated_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 2706 | } |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2707 | options.resize_buffers = 1; |
| 2708 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2709 | mbedtls_test_ssl_perform_handshake(&options); |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 2710 | |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2711 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 2712 | goto exit; |
Andrzej Kurek | 780dc18 | 2022-06-10 08:57:19 -0400 | [diff] [blame] | 2713 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2714 | mbedtls_test_free_handshake_options(&options); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2715 | } |
| 2716 | /* END_CASE */ |
| 2717 | |
Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 2718 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED:!MBEDTLS_SSL_PROTO_TLS1_3:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH:MBEDTLS_SSL_CONTEXT_SERIALIZATION:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_RSA_C:MBEDTLS_ECP_HAVE_SECP384R1:MBEDTLS_SSL_PROTO_DTLS:MBEDTLS_MD_CAN_SHA256:MBEDTLS_CAN_HANDLE_RSA_TEST_KEY */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2719 | void resize_buffers_serialize_mfl(int mfl) |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2720 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2721 | test_resize_buffers(mfl, 0, MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION, 1, 1, |
| 2722 | (char *) ""); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2723 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 2724 | goto exit; |
| 2725 | } |
| 2726 | /* END_CASE */ |
| 2727 | |
Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 2728 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED:!MBEDTLS_SSL_PROTO_TLS1_3:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH:MBEDTLS_SSL_RENEGOTIATION:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_RSA_C:MBEDTLS_ECP_HAVE_SECP384R1:MBEDTLS_MD_CAN_SHA256:MBEDTLS_CAN_HANDLE_RSA_TEST_KEY */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2729 | void resize_buffers_renegotiate_mfl(int mfl, int legacy_renegotiation, |
| 2730 | char *cipher) |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2731 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2732 | test_resize_buffers(mfl, 1, legacy_renegotiation, 0, 1, cipher); |
Andrzej Kurek | 0afa2a1 | 2020-03-03 10:39:58 -0500 | [diff] [blame] | 2733 | /* The goto below is used to avoid an "unused label" warning.*/ |
| 2734 | goto exit; |
| 2735 | } |
| 2736 | /* END_CASE */ |
Manuel Pégourié-Gonnard | 045f094 | 2020-07-02 11:34:02 +0200 | [diff] [blame] | 2737 | |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 2738 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */ |
Hanno Becker | 6667ffd | 2021-04-19 21:59:22 +0100 | [diff] [blame] | 2739 | void test_multiple_psks() |
| 2740 | { |
| 2741 | unsigned char psk0[10] = { 0 }; |
| 2742 | unsigned char psk0_identity[] = { 'f', 'o', 'o' }; |
| 2743 | |
| 2744 | unsigned char psk1[10] = { 0 }; |
| 2745 | unsigned char psk1_identity[] = { 'b', 'a', 'r' }; |
| 2746 | |
| 2747 | mbedtls_ssl_config conf; |
| 2748 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2749 | mbedtls_ssl_config_init(&conf); |
valerio | 32f2ac9 | 2023-04-20 11:59:52 +0200 | [diff] [blame] | 2750 | MD_OR_USE_PSA_INIT(); |
Hanno Becker | 6667ffd | 2021-04-19 21:59:22 +0100 | [diff] [blame] | 2751 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2752 | TEST_ASSERT(mbedtls_ssl_conf_psk(&conf, |
| 2753 | psk0, sizeof(psk0), |
| 2754 | psk0_identity, sizeof(psk0_identity)) == 0); |
| 2755 | TEST_ASSERT(mbedtls_ssl_conf_psk(&conf, |
| 2756 | psk1, sizeof(psk1), |
| 2757 | psk1_identity, sizeof(psk1_identity)) == |
| 2758 | MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE); |
Hanno Becker | 6667ffd | 2021-04-19 21:59:22 +0100 | [diff] [blame] | 2759 | |
| 2760 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2761 | mbedtls_ssl_config_free(&conf); |
Manuel Pégourié-Gonnard | 23fc437 | 2023-03-17 13:34:11 +0100 | [diff] [blame] | 2762 | MD_OR_USE_PSA_DONE(); |
Hanno Becker | 6667ffd | 2021-04-19 21:59:22 +0100 | [diff] [blame] | 2763 | } |
| 2764 | /* END_CASE */ |
| 2765 | |
Ronald Cron | 73fe8df | 2022-10-05 14:31:43 +0200 | [diff] [blame] | 2766 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED:MBEDTLS_USE_PSA_CRYPTO */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2767 | void test_multiple_psks_opaque(int mode) |
Hanno Becker | 6667ffd | 2021-04-19 21:59:22 +0100 | [diff] [blame] | 2768 | { |
| 2769 | /* |
| 2770 | * Mode 0: Raw PSK, then opaque PSK |
| 2771 | * Mode 1: Opaque PSK, then raw PSK |
| 2772 | * Mode 2: 2x opaque PSK |
| 2773 | */ |
| 2774 | |
| 2775 | unsigned char psk0_raw[10] = { 0 }; |
| 2776 | unsigned char psk0_raw_identity[] = { 'f', 'o', 'o' }; |
| 2777 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2778 | mbedtls_svc_key_id_t psk0_opaque = mbedtls_svc_key_id_make(0x1, (psa_key_id_t) 1); |
Andrzej Kurek | 03e0146 | 2022-01-03 12:53:24 +0100 | [diff] [blame] | 2779 | |
Hanno Becker | 6667ffd | 2021-04-19 21:59:22 +0100 | [diff] [blame] | 2780 | unsigned char psk0_opaque_identity[] = { 'f', 'o', 'o' }; |
| 2781 | |
| 2782 | unsigned char psk1_raw[10] = { 0 }; |
| 2783 | unsigned char psk1_raw_identity[] = { 'b', 'a', 'r' }; |
| 2784 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2785 | mbedtls_svc_key_id_t psk1_opaque = mbedtls_svc_key_id_make(0x1, (psa_key_id_t) 2); |
Andrzej Kurek | 03e0146 | 2022-01-03 12:53:24 +0100 | [diff] [blame] | 2786 | |
Hanno Becker | 6667ffd | 2021-04-19 21:59:22 +0100 | [diff] [blame] | 2787 | unsigned char psk1_opaque_identity[] = { 'b', 'a', 'r' }; |
| 2788 | |
| 2789 | mbedtls_ssl_config conf; |
| 2790 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2791 | mbedtls_ssl_config_init(&conf); |
valerio | 32f2ac9 | 2023-04-20 11:59:52 +0200 | [diff] [blame] | 2792 | MD_OR_USE_PSA_INIT(); |
Hanno Becker | 6667ffd | 2021-04-19 21:59:22 +0100 | [diff] [blame] | 2793 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2794 | switch (mode) { |
Hanno Becker | 6667ffd | 2021-04-19 21:59:22 +0100 | [diff] [blame] | 2795 | case 0: |
| 2796 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2797 | TEST_ASSERT(mbedtls_ssl_conf_psk(&conf, |
| 2798 | psk0_raw, sizeof(psk0_raw), |
| 2799 | psk0_raw_identity, sizeof(psk0_raw_identity)) |
| 2800 | == 0); |
| 2801 | TEST_ASSERT(mbedtls_ssl_conf_psk_opaque(&conf, |
| 2802 | psk1_opaque, |
| 2803 | psk1_opaque_identity, |
| 2804 | sizeof(psk1_opaque_identity)) |
| 2805 | == MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE); |
Hanno Becker | 6667ffd | 2021-04-19 21:59:22 +0100 | [diff] [blame] | 2806 | break; |
| 2807 | |
| 2808 | case 1: |
| 2809 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2810 | TEST_ASSERT(mbedtls_ssl_conf_psk_opaque(&conf, |
| 2811 | psk0_opaque, |
| 2812 | psk0_opaque_identity, |
| 2813 | sizeof(psk0_opaque_identity)) |
| 2814 | == 0); |
| 2815 | TEST_ASSERT(mbedtls_ssl_conf_psk(&conf, |
| 2816 | psk1_raw, sizeof(psk1_raw), |
| 2817 | psk1_raw_identity, sizeof(psk1_raw_identity)) |
| 2818 | == MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE); |
Hanno Becker | 6667ffd | 2021-04-19 21:59:22 +0100 | [diff] [blame] | 2819 | |
| 2820 | break; |
| 2821 | |
| 2822 | case 2: |
| 2823 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2824 | TEST_ASSERT(mbedtls_ssl_conf_psk_opaque(&conf, |
| 2825 | psk0_opaque, |
| 2826 | psk0_opaque_identity, |
| 2827 | sizeof(psk0_opaque_identity)) |
| 2828 | == 0); |
| 2829 | TEST_ASSERT(mbedtls_ssl_conf_psk_opaque(&conf, |
| 2830 | psk1_opaque, |
| 2831 | psk1_opaque_identity, |
| 2832 | sizeof(psk1_opaque_identity)) |
| 2833 | == MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE); |
Hanno Becker | 6667ffd | 2021-04-19 21:59:22 +0100 | [diff] [blame] | 2834 | |
| 2835 | break; |
| 2836 | |
| 2837 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2838 | TEST_ASSERT(0); |
Hanno Becker | 6667ffd | 2021-04-19 21:59:22 +0100 | [diff] [blame] | 2839 | break; |
| 2840 | } |
| 2841 | |
| 2842 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2843 | mbedtls_ssl_config_free(&conf); |
Manuel Pégourié-Gonnard | 23fc437 | 2023-03-17 13:34:11 +0100 | [diff] [blame] | 2844 | MD_OR_USE_PSA_DONE(); |
Hanno Becker | 6667ffd | 2021-04-19 21:59:22 +0100 | [diff] [blame] | 2845 | |
| 2846 | } |
| 2847 | /* END_CASE */ |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 2848 | |
Ronald Cron | 37bdaab | 2022-03-30 16:45:51 +0200 | [diff] [blame] | 2849 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2850 | void conf_version(int endpoint, int transport, |
| 2851 | int min_tls_version, int max_tls_version, |
| 2852 | int expected_ssl_setup_result) |
Ronald Cron | 37bdaab | 2022-03-30 16:45:51 +0200 | [diff] [blame] | 2853 | { |
| 2854 | mbedtls_ssl_config conf; |
| 2855 | mbedtls_ssl_context ssl; |
| 2856 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2857 | mbedtls_ssl_config_init(&conf); |
| 2858 | mbedtls_ssl_init(&ssl); |
valerio | 32f2ac9 | 2023-04-20 11:59:52 +0200 | [diff] [blame] | 2859 | MD_OR_USE_PSA_INIT(); |
Ronald Cron | 37bdaab | 2022-03-30 16:45:51 +0200 | [diff] [blame] | 2860 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2861 | mbedtls_ssl_conf_endpoint(&conf, endpoint); |
| 2862 | mbedtls_ssl_conf_transport(&conf, transport); |
| 2863 | mbedtls_ssl_conf_min_tls_version(&conf, min_tls_version); |
| 2864 | mbedtls_ssl_conf_max_tls_version(&conf, max_tls_version); |
Ronald Cron | 37bdaab | 2022-03-30 16:45:51 +0200 | [diff] [blame] | 2865 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2866 | TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == expected_ssl_setup_result); |
Pengyu Lv | db61433 | 2023-07-10 10:56:54 +0800 | [diff] [blame] | 2867 | TEST_EQUAL(mbedtls_ssl_conf_get_endpoint( |
| 2868 | mbedtls_ssl_context_get_config(&ssl)), endpoint); |
Ronald Cron | 37bdaab | 2022-03-30 16:45:51 +0200 | [diff] [blame] | 2869 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2870 | mbedtls_ssl_free(&ssl); |
| 2871 | mbedtls_ssl_config_free(&conf); |
Manuel Pégourié-Gonnard | 23fc437 | 2023-03-17 13:34:11 +0100 | [diff] [blame] | 2872 | |
| 2873 | exit: |
| 2874 | MD_OR_USE_PSA_DONE(); |
Ronald Cron | 37bdaab | 2022-03-30 16:45:51 +0200 | [diff] [blame] | 2875 | } |
| 2876 | /* END_CASE */ |
| 2877 | |
Manuel Pégourié-Gonnard | f07ce3b | 2023-09-22 11:53:41 +0200 | [diff] [blame] | 2878 | /* BEGIN_CASE depends_on:MBEDTLS_ECP_C:!MBEDTLS_DEPRECATED_REMOVED:!MBEDTLS_DEPRECATED_WARNING:MBEDTLS_ECP_HAVE_SECP192R1:MBEDTLS_ECP_HAVE_SECP224R1:MBEDTLS_ECP_HAVE_SECP256R1 */ |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 2879 | void conf_curve() |
| 2880 | { |
| 2881 | |
| 2882 | mbedtls_ecp_group_id curve_list[] = { MBEDTLS_ECP_DP_SECP192R1, |
| 2883 | MBEDTLS_ECP_DP_SECP224R1, |
| 2884 | MBEDTLS_ECP_DP_SECP256R1, |
| 2885 | MBEDTLS_ECP_DP_NONE }; |
Tom Cosgrove | a327b52 | 2022-08-03 08:33:06 +0100 | [diff] [blame] | 2886 | uint16_t iana_tls_group_list[] = { MBEDTLS_SSL_IANA_TLS_GROUP_SECP192R1, |
| 2887 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP224R1, |
| 2888 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, |
| 2889 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE }; |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 2890 | |
| 2891 | mbedtls_ssl_config conf; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2892 | mbedtls_ssl_config_init(&conf); |
Jerry Yu | baa4934 | 2022-02-15 10:26:40 +0800 | [diff] [blame] | 2893 | #if defined(MBEDTLS_SSL_PROTO_TLS1_2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2894 | mbedtls_ssl_conf_max_tls_version(&conf, MBEDTLS_SSL_VERSION_TLS1_2); |
| 2895 | mbedtls_ssl_conf_min_tls_version(&conf, MBEDTLS_SSL_VERSION_TLS1_2); |
Jerry Yu | baa4934 | 2022-02-15 10:26:40 +0800 | [diff] [blame] | 2896 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2897 | mbedtls_ssl_conf_max_tls_version(&conf, MBEDTLS_SSL_VERSION_TLS1_3); |
| 2898 | mbedtls_ssl_conf_min_tls_version(&conf, MBEDTLS_SSL_VERSION_TLS1_3); |
Jerry Yu | baa4934 | 2022-02-15 10:26:40 +0800 | [diff] [blame] | 2899 | #endif |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2900 | mbedtls_ssl_conf_curves(&conf, curve_list); |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 2901 | |
| 2902 | mbedtls_ssl_context ssl; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2903 | mbedtls_ssl_init(&ssl); |
valerio | 32f2ac9 | 2023-04-20 11:59:52 +0200 | [diff] [blame] | 2904 | MD_OR_USE_PSA_INIT(); |
| 2905 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2906 | TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0); |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 2907 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2908 | TEST_ASSERT(ssl.handshake != NULL && ssl.handshake->group_list != NULL); |
| 2909 | TEST_ASSERT(ssl.conf != NULL && ssl.conf->group_list == NULL); |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 2910 | |
Yanray Wang | d577a68 | 2022-10-27 11:47:54 +0800 | [diff] [blame] | 2911 | TEST_EQUAL(ssl.handshake-> |
| 2912 | group_list[ARRAY_LENGTH(iana_tls_group_list) - 1], |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2913 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE); |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 2914 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2915 | for (size_t i = 0; i < ARRAY_LENGTH(iana_tls_group_list); i++) { |
| 2916 | TEST_EQUAL(iana_tls_group_list[i], ssl.handshake->group_list[i]); |
| 2917 | } |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 2918 | |
Manuel Pégourié-Gonnard | 23fc437 | 2023-03-17 13:34:11 +0100 | [diff] [blame] | 2919 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2920 | mbedtls_ssl_free(&ssl); |
| 2921 | mbedtls_ssl_config_free(&conf); |
Manuel Pégourié-Gonnard | 23fc437 | 2023-03-17 13:34:11 +0100 | [diff] [blame] | 2922 | MD_OR_USE_PSA_DONE(); |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 2923 | } |
| 2924 | /* END_CASE */ |
| 2925 | |
| 2926 | /* BEGIN_CASE depends_on:MBEDTLS_DEPRECATED_REMOVED */ |
| 2927 | void conf_group() |
| 2928 | { |
| 2929 | uint16_t iana_tls_group_list[] = { MBEDTLS_SSL_IANA_TLS_GROUP_SECP192R1, |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2930 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP224R1, |
| 2931 | MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, |
| 2932 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE }; |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 2933 | |
| 2934 | mbedtls_ssl_config conf; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2935 | mbedtls_ssl_config_init(&conf); |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 2936 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2937 | mbedtls_ssl_conf_max_tls_version(&conf, MBEDTLS_SSL_VERSION_TLS1_2); |
| 2938 | mbedtls_ssl_conf_min_tls_version(&conf, MBEDTLS_SSL_VERSION_TLS1_2); |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 2939 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2940 | mbedtls_ssl_conf_groups(&conf, iana_tls_group_list); |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 2941 | |
| 2942 | mbedtls_ssl_context ssl; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2943 | mbedtls_ssl_init(&ssl); |
valerio | 32f2ac9 | 2023-04-20 11:59:52 +0200 | [diff] [blame] | 2944 | MD_OR_USE_PSA_INIT(); |
| 2945 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2946 | TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0); |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 2947 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2948 | TEST_ASSERT(ssl.conf != NULL && ssl.conf->group_list != NULL); |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 2949 | |
Yanray Wang | d577a68 | 2022-10-27 11:47:54 +0800 | [diff] [blame] | 2950 | TEST_EQUAL(ssl.conf-> |
| 2951 | group_list[ARRAY_LENGTH(iana_tls_group_list) - 1], |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2952 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE); |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 2953 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2954 | for (size_t i = 0; i < ARRAY_LENGTH(iana_tls_group_list); i++) { |
| 2955 | TEST_EQUAL(iana_tls_group_list[i], ssl.conf->group_list[i]); |
| 2956 | } |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 2957 | |
Manuel Pégourié-Gonnard | 23fc437 | 2023-03-17 13:34:11 +0100 | [diff] [blame] | 2958 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2959 | mbedtls_ssl_free(&ssl); |
| 2960 | mbedtls_ssl_config_free(&conf); |
Manuel Pégourié-Gonnard | 23fc437 | 2023-03-17 13:34:11 +0100 | [diff] [blame] | 2961 | MD_OR_USE_PSA_DONE(); |
Brett Warren | 7f813d5 | 2021-10-20 23:08:38 +0100 | [diff] [blame] | 2962 | } |
| 2963 | /* END_CASE */ |
Paul Elliott | b9af2db | 2022-03-09 15:34:37 +0000 | [diff] [blame] | 2964 | |
Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 2965 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_SRV_C:MBEDTLS_SSL_CACHE_C:!MBEDTLS_SSL_PROTO_TLS1_3:MBEDTLS_DEBUG_C:MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED:MBEDTLS_RSA_C:MBEDTLS_ECP_HAVE_SECP384R1:MBEDTLS_PKCS1_V15:MBEDTLS_MD_CAN_SHA256 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2966 | void force_bad_session_id_len() |
Andrzej Kurek | 514683a | 2022-06-10 10:33:05 -0400 | [diff] [blame] | 2967 | { |
| 2968 | enum { BUFFSIZE = 1024 }; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 2969 | mbedtls_test_handshake_test_options options; |
| 2970 | mbedtls_test_ssl_endpoint client, server; |
| 2971 | mbedtls_test_ssl_log_pattern srv_pattern, cli_pattern; |
Andrzej Kurek | 514683a | 2022-06-10 10:33:05 -0400 | [diff] [blame] | 2972 | mbedtls_test_message_socket_context server_context, client_context; |
| 2973 | |
| 2974 | srv_pattern.pattern = cli_pattern.pattern = "cache did not store session"; |
| 2975 | srv_pattern.counter = 0; |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2976 | mbedtls_test_init_handshake_options(&options); |
Andrzej Kurek | 514683a | 2022-06-10 10:33:05 -0400 | [diff] [blame] | 2977 | |
| 2978 | options.srv_log_obj = &srv_pattern; |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2979 | options.srv_log_fun = mbedtls_test_ssl_log_analyzer; |
Andrzej Kurek | 514683a | 2022-06-10 10:33:05 -0400 | [diff] [blame] | 2980 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2981 | mbedtls_platform_zeroize(&client, sizeof(client)); |
| 2982 | mbedtls_platform_zeroize(&server, sizeof(server)); |
Andrzej Kurek | 514683a | 2022-06-10 10:33:05 -0400 | [diff] [blame] | 2983 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2984 | mbedtls_test_message_socket_init(&server_context); |
| 2985 | mbedtls_test_message_socket_init(&client_context); |
Valerio Setti | 00a256f | 2023-04-27 17:22:27 +0200 | [diff] [blame] | 2986 | MD_OR_USE_PSA_INIT(); |
Andrzej Kurek | 514683a | 2022-06-10 10:33:05 -0400 | [diff] [blame] | 2987 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2988 | TEST_ASSERT(mbedtls_test_ssl_endpoint_init(&client, MBEDTLS_SSL_IS_CLIENT, |
| 2989 | &options, NULL, NULL, |
| 2990 | NULL, NULL) == 0); |
Andrzej Kurek | 514683a | 2022-06-10 10:33:05 -0400 | [diff] [blame] | 2991 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 2992 | TEST_ASSERT(mbedtls_test_ssl_endpoint_init(&server, MBEDTLS_SSL_IS_SERVER, |
| 2993 | &options, NULL, NULL, NULL, |
| 2994 | NULL) == 0); |
Andrzej Kurek | 514683a | 2022-06-10 10:33:05 -0400 | [diff] [blame] | 2995 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 2996 | mbedtls_debug_set_threshold(1); |
| 2997 | mbedtls_ssl_conf_dbg(&server.conf, options.srv_log_fun, |
| 2998 | options.srv_log_obj); |
Andrzej Kurek | 514683a | 2022-06-10 10:33:05 -0400 | [diff] [blame] | 2999 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3000 | TEST_ASSERT(mbedtls_test_mock_socket_connect(&(client.socket), |
| 3001 | &(server.socket), |
| 3002 | BUFFSIZE) == 0); |
Andrzej Kurek | 514683a | 2022-06-10 10:33:05 -0400 | [diff] [blame] | 3003 | |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 3004 | TEST_ASSERT(mbedtls_test_move_handshake_to_state( |
| 3005 | &(client.ssl), &(server.ssl), MBEDTLS_SSL_HANDSHAKE_WRAPUP) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3006 | == 0); |
Andrzej Kurek | 514683a | 2022-06-10 10:33:05 -0400 | [diff] [blame] | 3007 | /* Force a bad session_id_len that will be read by the server in |
| 3008 | * mbedtls_ssl_cache_set. */ |
| 3009 | server.ssl.session_negotiate->id_len = 33; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3010 | if (options.cli_msg_len != 0 || options.srv_msg_len != 0) { |
Andrzej Kurek | 514683a | 2022-06-10 10:33:05 -0400 | [diff] [blame] | 3011 | /* Start data exchanging test */ |
Yanray Wang | b088bfc | 2023-03-16 12:15:49 +0800 | [diff] [blame] | 3012 | TEST_ASSERT(mbedtls_test_ssl_exchange_data( |
| 3013 | &(client.ssl), options.cli_msg_len, |
| 3014 | options.expected_cli_fragments, |
| 3015 | &(server.ssl), options.srv_msg_len, |
| 3016 | options.expected_srv_fragments) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3017 | == 0); |
Andrzej Kurek | 514683a | 2022-06-10 10:33:05 -0400 | [diff] [blame] | 3018 | } |
Andrzej Kurek | 514683a | 2022-06-10 10:33:05 -0400 | [diff] [blame] | 3019 | |
| 3020 | /* Make sure that the cache did not store the session */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3021 | TEST_EQUAL(srv_pattern.counter, 1); |
Andrzej Kurek | 514683a | 2022-06-10 10:33:05 -0400 | [diff] [blame] | 3022 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3023 | mbedtls_test_ssl_endpoint_free(&client, NULL); |
| 3024 | mbedtls_test_ssl_endpoint_free(&server, NULL); |
| 3025 | mbedtls_test_free_handshake_options(&options); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3026 | mbedtls_debug_set_threshold(0); |
Manuel Pégourié-Gonnard | 23fc437 | 2023-03-17 13:34:11 +0100 | [diff] [blame] | 3027 | MD_OR_USE_PSA_DONE(); |
Andrzej Kurek | 514683a | 2022-06-10 10:33:05 -0400 | [diff] [blame] | 3028 | } |
| 3029 | /* END_CASE */ |
| 3030 | |
Andrzej Kurek | ed4d217 | 2022-06-08 11:57:57 -0400 | [diff] [blame] | 3031 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_SRV_C:MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE:MBEDTLS_TEST_HOOKS */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3032 | void cookie_parsing(data_t *cookie, int exp_ret) |
Andrzej Kurek | cfb0194 | 2022-06-06 13:08:23 -0400 | [diff] [blame] | 3033 | { |
| 3034 | mbedtls_ssl_context ssl; |
| 3035 | mbedtls_ssl_config conf; |
| 3036 | size_t len; |
| 3037 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3038 | mbedtls_ssl_init(&ssl); |
| 3039 | mbedtls_ssl_config_init(&conf); |
valerio | 32f2ac9 | 2023-04-20 11:59:52 +0200 | [diff] [blame] | 3040 | USE_PSA_INIT(); |
| 3041 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3042 | TEST_EQUAL(mbedtls_ssl_config_defaults(&conf, MBEDTLS_SSL_IS_SERVER, |
| 3043 | MBEDTLS_SSL_TRANSPORT_DATAGRAM, |
| 3044 | MBEDTLS_SSL_PRESET_DEFAULT), |
| 3045 | 0); |
Andrzej Kurek | cfb0194 | 2022-06-06 13:08:23 -0400 | [diff] [blame] | 3046 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3047 | TEST_EQUAL(mbedtls_ssl_setup(&ssl, &conf), 0); |
| 3048 | TEST_EQUAL(mbedtls_ssl_check_dtls_clihlo_cookie(&ssl, ssl.cli_id, |
| 3049 | ssl.cli_id_len, |
| 3050 | cookie->x, cookie->len, |
| 3051 | ssl.out_buf, |
| 3052 | MBEDTLS_SSL_OUT_CONTENT_LEN, |
| 3053 | &len), |
| 3054 | exp_ret); |
Andrzej Kurek | cfb0194 | 2022-06-06 13:08:23 -0400 | [diff] [blame] | 3055 | |
valerio | 32f2ac9 | 2023-04-20 11:59:52 +0200 | [diff] [blame] | 3056 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3057 | mbedtls_ssl_free(&ssl); |
| 3058 | mbedtls_ssl_config_free(&conf); |
Valerio Setti | 285dae8 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 3059 | USE_PSA_DONE(); |
Andrzej Kurek | cfb0194 | 2022-06-06 13:08:23 -0400 | [diff] [blame] | 3060 | } |
| 3061 | /* END_CASE */ |
| 3062 | |
Paul Elliott | b9af2db | 2022-03-09 15:34:37 +0000 | [diff] [blame] | 3063 | /* BEGIN_CASE depends_on:MBEDTLS_TIMING_C:MBEDTLS_HAVE_TIME */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3064 | void timing_final_delay_accessor() |
Paul Elliott | b9af2db | 2022-03-09 15:34:37 +0000 | [diff] [blame] | 3065 | { |
| 3066 | mbedtls_timing_delay_context delay_context; |
| 3067 | |
Valerio Setti | 285dae8 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 3068 | USE_PSA_INIT(); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3069 | mbedtls_timing_set_delay(&delay_context, 50, 100); |
Paul Elliott | b9af2db | 2022-03-09 15:34:37 +0000 | [diff] [blame] | 3070 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3071 | TEST_ASSERT(mbedtls_timing_get_final_delay(&delay_context) == 100); |
valerio | 32f2ac9 | 2023-04-20 11:59:52 +0200 | [diff] [blame] | 3072 | |
| 3073 | exit: |
Valerio Setti | 285dae8 | 2023-04-19 15:10:45 +0200 | [diff] [blame] | 3074 | USE_PSA_DONE(); |
Paul Elliott | b9af2db | 2022-03-09 15:34:37 +0000 | [diff] [blame] | 3075 | } |
| 3076 | /* END_CASE */ |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 3077 | |
| 3078 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_DTLS_CONNECTION_ID */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3079 | void cid_sanity() |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 3080 | { |
| 3081 | mbedtls_ssl_context ssl; |
| 3082 | mbedtls_ssl_config conf; |
| 3083 | |
| 3084 | unsigned char own_cid[MBEDTLS_SSL_CID_IN_LEN_MAX]; |
| 3085 | unsigned char test_cid[MBEDTLS_SSL_CID_IN_LEN_MAX]; |
| 3086 | int cid_enabled; |
| 3087 | size_t own_cid_len; |
| 3088 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3089 | mbedtls_test_rnd_std_rand(NULL, own_cid, sizeof(own_cid)); |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 3090 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3091 | mbedtls_ssl_init(&ssl); |
| 3092 | mbedtls_ssl_config_init(&conf); |
valerio | 32f2ac9 | 2023-04-20 11:59:52 +0200 | [diff] [blame] | 3093 | MD_OR_USE_PSA_INIT(); |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 3094 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3095 | TEST_ASSERT(mbedtls_ssl_config_defaults(&conf, |
| 3096 | MBEDTLS_SSL_IS_CLIENT, |
| 3097 | MBEDTLS_SSL_TRANSPORT_STREAM, |
| 3098 | MBEDTLS_SSL_PRESET_DEFAULT) |
| 3099 | == 0); |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 3100 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3101 | TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0); |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 3102 | |
| 3103 | /* Can't use CID functions with stream transport. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3104 | TEST_ASSERT(mbedtls_ssl_set_cid(&ssl, MBEDTLS_SSL_CID_ENABLED, own_cid, |
| 3105 | sizeof(own_cid)) |
| 3106 | == MBEDTLS_ERR_SSL_BAD_INPUT_DATA); |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 3107 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3108 | TEST_ASSERT(mbedtls_ssl_get_own_cid(&ssl, &cid_enabled, test_cid, |
| 3109 | &own_cid_len) |
| 3110 | == MBEDTLS_ERR_SSL_BAD_INPUT_DATA); |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 3111 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3112 | TEST_ASSERT(mbedtls_ssl_config_defaults(&conf, |
| 3113 | MBEDTLS_SSL_IS_CLIENT, |
| 3114 | MBEDTLS_SSL_TRANSPORT_DATAGRAM, |
| 3115 | MBEDTLS_SSL_PRESET_DEFAULT) |
| 3116 | == 0); |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 3117 | |
| 3118 | /* Attempt to set config cid size too big. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3119 | TEST_ASSERT(mbedtls_ssl_conf_cid(&conf, MBEDTLS_SSL_CID_IN_LEN_MAX + 1, |
| 3120 | MBEDTLS_SSL_UNEXPECTED_CID_IGNORE) |
| 3121 | == MBEDTLS_ERR_SSL_BAD_INPUT_DATA); |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 3122 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3123 | TEST_ASSERT(mbedtls_ssl_conf_cid(&conf, sizeof(own_cid), |
| 3124 | MBEDTLS_SSL_UNEXPECTED_CID_IGNORE) |
| 3125 | == 0); |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 3126 | |
| 3127 | /* Attempt to set CID length not matching config. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3128 | TEST_ASSERT(mbedtls_ssl_set_cid(&ssl, MBEDTLS_SSL_CID_ENABLED, own_cid, |
| 3129 | MBEDTLS_SSL_CID_IN_LEN_MAX - 1) |
| 3130 | == MBEDTLS_ERR_SSL_BAD_INPUT_DATA); |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 3131 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3132 | TEST_ASSERT(mbedtls_ssl_set_cid(&ssl, MBEDTLS_SSL_CID_ENABLED, own_cid, |
| 3133 | sizeof(own_cid)) |
| 3134 | == 0); |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 3135 | |
| 3136 | /* Test we get back what we put in. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3137 | TEST_ASSERT(mbedtls_ssl_get_own_cid(&ssl, &cid_enabled, test_cid, |
| 3138 | &own_cid_len) |
| 3139 | == 0); |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 3140 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3141 | TEST_EQUAL(cid_enabled, MBEDTLS_SSL_CID_ENABLED); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 3142 | TEST_MEMORY_COMPARE(own_cid, own_cid_len, test_cid, own_cid_len); |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 3143 | |
| 3144 | /* Test disabling works. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3145 | TEST_ASSERT(mbedtls_ssl_set_cid(&ssl, MBEDTLS_SSL_CID_DISABLED, NULL, |
| 3146 | 0) |
| 3147 | == 0); |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 3148 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3149 | TEST_ASSERT(mbedtls_ssl_get_own_cid(&ssl, &cid_enabled, test_cid, |
| 3150 | &own_cid_len) |
| 3151 | == 0); |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 3152 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3153 | TEST_EQUAL(cid_enabled, MBEDTLS_SSL_CID_DISABLED); |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 3154 | |
Manuel Pégourié-Gonnard | 23fc437 | 2023-03-17 13:34:11 +0100 | [diff] [blame] | 3155 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3156 | mbedtls_ssl_free(&ssl); |
| 3157 | mbedtls_ssl_config_free(&conf); |
Manuel Pégourié-Gonnard | 23fc437 | 2023-03-17 13:34:11 +0100 | [diff] [blame] | 3158 | MD_OR_USE_PSA_DONE(); |
Paul Elliott | 02758a5 | 2022-03-16 14:32:33 +0000 | [diff] [blame] | 3159 | } |
| 3160 | /* END_CASE */ |
| 3161 | |
Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 3162 | /* BEGIN_CASE depends_on:MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED:MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_ECP_HAVE_SECP256R1:MBEDTLS_RSA_C:MBEDTLS_ECP_HAVE_SECP384R1:MBEDTLS_PK_CAN_ECDSA_SOME */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3163 | void raw_key_agreement_fail(int bad_server_ecdhe_key) |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 3164 | { |
| 3165 | enum { BUFFSIZE = 17000 }; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 3166 | mbedtls_test_ssl_endpoint client, server; |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 3167 | mbedtls_psa_stats_t stats; |
Andrzej Kurek | 39d88d4 | 2022-03-31 06:30:54 -0400 | [diff] [blame] | 3168 | size_t free_slots_before = -1; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 3169 | mbedtls_test_handshake_test_options options; |
Gilles Peskine | 21e46b3 | 2023-10-17 16:35:20 +0200 | [diff] [blame] | 3170 | mbedtls_test_init_handshake_options(&options); |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 3171 | |
Andrzej Kurek | cc28e9a | 2022-03-08 18:36:35 -0500 | [diff] [blame] | 3172 | uint16_t iana_tls_group_list[] = { MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, |
| 3173 | MBEDTLS_SSL_IANA_TLS_GROUP_NONE }; |
Manuel Pégourié-Gonnard | 23fc437 | 2023-03-17 13:34:11 +0100 | [diff] [blame] | 3174 | MD_OR_USE_PSA_INIT(); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3175 | mbedtls_platform_zeroize(&client, sizeof(client)); |
| 3176 | mbedtls_platform_zeroize(&server, sizeof(server)); |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 3177 | |
Andrzej Kurek | 626a931 | 2022-06-10 11:07:39 -0400 | [diff] [blame] | 3178 | options.pk_alg = MBEDTLS_PK_ECDSA; |
Ronald Cron | 097ba14 | 2023-03-08 16:18:00 +0100 | [diff] [blame] | 3179 | options.server_min_version = MBEDTLS_SSL_VERSION_TLS1_2; |
| 3180 | options.server_max_version = MBEDTLS_SSL_VERSION_TLS1_2; |
Andrzej Kurek | 626a931 | 2022-06-10 11:07:39 -0400 | [diff] [blame] | 3181 | |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 3182 | /* Client side, force SECP256R1 to make one key bitflip fail |
Andrzej Kurek | 83e60ee | 2022-04-14 08:51:41 -0400 | [diff] [blame] | 3183 | * the raw key agreement. Flipping the first byte makes the |
| 3184 | * required 0x04 identifier invalid. */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3185 | TEST_EQUAL(mbedtls_test_ssl_endpoint_init(&client, MBEDTLS_SSL_IS_CLIENT, |
| 3186 | &options, NULL, NULL, |
| 3187 | NULL, iana_tls_group_list), 0); |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 3188 | |
| 3189 | /* Server side */ |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3190 | TEST_EQUAL(mbedtls_test_ssl_endpoint_init(&server, MBEDTLS_SSL_IS_SERVER, |
| 3191 | &options, NULL, NULL, |
| 3192 | NULL, NULL), 0); |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 3193 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3194 | TEST_EQUAL(mbedtls_test_mock_socket_connect(&(client.socket), |
| 3195 | &(server.socket), |
| 3196 | BUFFSIZE), 0); |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 3197 | |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 3198 | TEST_EQUAL(mbedtls_test_move_handshake_to_state( |
| 3199 | &(client.ssl), &(server.ssl), |
| 3200 | MBEDTLS_SSL_CLIENT_KEY_EXCHANGE), 0); |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 3201 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3202 | mbedtls_psa_get_stats(&stats); |
Andrzej Kurek | 39d88d4 | 2022-03-31 06:30:54 -0400 | [diff] [blame] | 3203 | /* Save the number of slots in use up to this point. |
| 3204 | * With PSA, one can be used for the ECDH private key. */ |
| 3205 | free_slots_before = stats.empty_slots; |
Andrzej Kurek | cb33bc5 | 2022-03-31 07:17:18 -0400 | [diff] [blame] | 3206 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3207 | if (bad_server_ecdhe_key) { |
Gilles Peskine | b4f874d | 2022-04-08 16:48:09 -0400 | [diff] [blame] | 3208 | /* Force a simulated bitflip in the server key. to make the |
| 3209 | * raw key agreement in ssl_write_client_key_exchange fail. */ |
Przemek Stekiel | 7ac93be | 2023-07-04 10:02:38 +0200 | [diff] [blame] | 3210 | (client.ssl).handshake->xxdh_psa_peerkey[0] ^= 0x02; |
Gilles Peskine | b4f874d | 2022-04-08 16:48:09 -0400 | [diff] [blame] | 3211 | } |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 3212 | |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 3213 | TEST_EQUAL(mbedtls_test_move_handshake_to_state( |
| 3214 | &(client.ssl), &(server.ssl), MBEDTLS_SSL_HANDSHAKE_OVER), |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3215 | bad_server_ecdhe_key ? MBEDTLS_ERR_SSL_HW_ACCEL_FAILED : 0); |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 3216 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3217 | mbedtls_psa_get_stats(&stats); |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 3218 | |
Gilles Peskine | b4f874d | 2022-04-08 16:48:09 -0400 | [diff] [blame] | 3219 | /* Make sure that the key slot is already destroyed in case of failure, |
| 3220 | * without waiting to close the connection. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3221 | if (bad_server_ecdhe_key) { |
| 3222 | TEST_EQUAL(free_slots_before, stats.empty_slots); |
| 3223 | } |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 3224 | |
| 3225 | exit: |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3226 | mbedtls_test_ssl_endpoint_free(&client, NULL); |
| 3227 | mbedtls_test_ssl_endpoint_free(&server, NULL); |
| 3228 | mbedtls_test_free_handshake_options(&options); |
Andrzej Kurek | cb33bc5 | 2022-03-31 07:17:18 -0400 | [diff] [blame] | 3229 | |
Manuel Pégourié-Gonnard | 23fc437 | 2023-03-17 13:34:11 +0100 | [diff] [blame] | 3230 | MD_OR_USE_PSA_DONE(); |
Andrzej Kurek | b342782 | 2022-03-08 06:55:42 -0500 | [diff] [blame] | 3231 | } |
| 3232 | /* END_CASE */ |
Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 3233 | /* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS:MBEDTLS_SSL_PROTO_TLS1_3:!MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_SSL_CLI_C:MBEDTLS_SSL_SRV_C:MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED:MBEDTLS_ECP_HAVE_SECP384R1 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3234 | void tls13_server_certificate_msg_invalid_vector_len() |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 3235 | { |
| 3236 | int ret = -1; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 3237 | mbedtls_test_ssl_endpoint client_ep, server_ep; |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 3238 | unsigned char *buf, *end; |
| 3239 | size_t buf_len; |
| 3240 | int step = 0; |
| 3241 | int expected_result; |
Ronald Cron | e7b9b6b | 2022-06-10 17:24:31 +0200 | [diff] [blame] | 3242 | mbedtls_ssl_chk_buf_ptr_args expected_chk_buf_ptr_args; |
Yanray Wang | 9ef0dce | 2022-10-25 16:44:13 +0800 | [diff] [blame] | 3243 | mbedtls_test_handshake_test_options client_options; |
| 3244 | mbedtls_test_handshake_test_options server_options; |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 3245 | |
| 3246 | /* |
| 3247 | * Test set-up |
| 3248 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3249 | mbedtls_platform_zeroize(&client_ep, sizeof(client_ep)); |
| 3250 | mbedtls_platform_zeroize(&server_ep, sizeof(server_ep)); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 3251 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3252 | mbedtls_test_init_handshake_options(&client_options); |
valerio | 32f2ac9 | 2023-04-20 11:59:52 +0200 | [diff] [blame] | 3253 | MD_OR_USE_PSA_INIT(); |
| 3254 | |
Paul Elliott | 9a8d784 | 2022-07-10 12:48:57 +0100 | [diff] [blame] | 3255 | client_options.pk_alg = MBEDTLS_PK_ECDSA; |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3256 | ret = mbedtls_test_ssl_endpoint_init(&client_ep, MBEDTLS_SSL_IS_CLIENT, |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 3257 | &client_options, NULL, NULL, NULL, |
| 3258 | NULL); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3259 | TEST_EQUAL(ret, 0); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 3260 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3261 | mbedtls_test_init_handshake_options(&server_options); |
Paul Elliott | 9a8d784 | 2022-07-10 12:48:57 +0100 | [diff] [blame] | 3262 | server_options.pk_alg = MBEDTLS_PK_ECDSA; |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3263 | ret = mbedtls_test_ssl_endpoint_init(&server_ep, MBEDTLS_SSL_IS_SERVER, |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 3264 | &server_options, NULL, NULL, NULL, |
| 3265 | NULL); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3266 | TEST_EQUAL(ret, 0); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 3267 | |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3268 | ret = mbedtls_test_mock_socket_connect(&(client_ep.socket), |
| 3269 | &(server_ep.socket), 1024); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3270 | TEST_EQUAL(ret, 0); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 3271 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3272 | while (1) { |
| 3273 | mbedtls_test_set_step(++step); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 3274 | |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 3275 | ret = mbedtls_test_move_handshake_to_state( |
| 3276 | &(server_ep.ssl), &(client_ep.ssl), |
| 3277 | MBEDTLS_SSL_CERTIFICATE_VERIFY); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3278 | TEST_EQUAL(ret, 0); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 3279 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3280 | ret = mbedtls_ssl_flush_output(&(server_ep.ssl)); |
| 3281 | TEST_EQUAL(ret, 0); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 3282 | |
Yanray Wang | bd29683 | 2022-10-26 18:28:11 +0800 | [diff] [blame] | 3283 | ret = mbedtls_test_move_handshake_to_state( |
| 3284 | &(client_ep.ssl), &(server_ep.ssl), |
| 3285 | MBEDTLS_SSL_SERVER_CERTIFICATE); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3286 | TEST_EQUAL(ret, 0); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 3287 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3288 | ret = mbedtls_ssl_tls13_fetch_handshake_msg(&(client_ep.ssl), |
| 3289 | MBEDTLS_SSL_HS_CERTIFICATE, |
| 3290 | &buf, &buf_len); |
| 3291 | TEST_EQUAL(ret, 0); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 3292 | |
| 3293 | end = buf + buf_len; |
| 3294 | |
| 3295 | /* |
| 3296 | * Tweak server Certificate message and parse it. |
| 3297 | */ |
| 3298 | |
Yanray Wang | f56181a | 2023-03-16 12:21:33 +0800 | [diff] [blame] | 3299 | ret = mbedtls_test_tweak_tls13_certificate_msg_vector_len( |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3300 | buf, &end, step, &expected_result, &expected_chk_buf_ptr_args); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 3301 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3302 | if (ret != 0) { |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 3303 | break; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3304 | } |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 3305 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3306 | ret = mbedtls_ssl_tls13_parse_certificate(&(client_ep.ssl), buf, end); |
| 3307 | TEST_EQUAL(ret, expected_result); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 3308 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3309 | TEST_ASSERT(mbedtls_ssl_cmp_chk_buf_ptr_fail_args( |
| 3310 | &expected_chk_buf_ptr_args) == 0); |
Ronald Cron | e7b9b6b | 2022-06-10 17:24:31 +0200 | [diff] [blame] | 3311 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3312 | mbedtls_ssl_reset_chk_buf_ptr_fail_args(); |
Ronald Cron | e7b9b6b | 2022-06-10 17:24:31 +0200 | [diff] [blame] | 3313 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3314 | ret = mbedtls_ssl_session_reset(&(client_ep.ssl)); |
| 3315 | TEST_EQUAL(ret, 0); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 3316 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3317 | ret = mbedtls_ssl_session_reset(&(server_ep.ssl)); |
| 3318 | TEST_EQUAL(ret, 0); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 3319 | } |
| 3320 | |
| 3321 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3322 | mbedtls_ssl_reset_chk_buf_ptr_fail_args(); |
Yanray Wang | f7b6235 | 2022-10-26 11:51:53 +0800 | [diff] [blame] | 3323 | mbedtls_test_ssl_endpoint_free(&client_ep, NULL); |
| 3324 | mbedtls_test_ssl_endpoint_free(&server_ep, NULL); |
| 3325 | mbedtls_test_free_handshake_options(&client_options); |
| 3326 | mbedtls_test_free_handshake_options(&server_options); |
Manuel Pégourié-Gonnard | 23fc437 | 2023-03-17 13:34:11 +0100 | [diff] [blame] | 3327 | MD_OR_USE_PSA_DONE(); |
Ronald Cron | e3dac4a | 2022-06-10 17:21:51 +0200 | [diff] [blame] | 3328 | } |
| 3329 | /* END_CASE */ |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 3330 | |
| 3331 | /* BEGIN_CASE depends_on:MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3332 | void ssl_ecjpake_set_password(int use_opaque_arg) |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 3333 | { |
| 3334 | mbedtls_ssl_context ssl; |
| 3335 | mbedtls_ssl_config conf; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3336 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 3337 | mbedtls_svc_key_id_t pwd_slot = MBEDTLS_SVC_KEY_ID_INIT; |
| 3338 | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
| 3339 | (void) use_opaque_arg; |
| 3340 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3341 | unsigned char pwd_string[sizeof(ECJPAKE_TEST_PWD)] = ""; |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 3342 | size_t pwd_len = 0; |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 3343 | int ret; |
| 3344 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3345 | mbedtls_ssl_init(&ssl); |
valerio | 32f2ac9 | 2023-04-20 11:59:52 +0200 | [diff] [blame] | 3346 | MD_OR_USE_PSA_INIT(); |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 3347 | |
Valerio Setti | e7518ba | 2022-12-02 12:09:43 +0100 | [diff] [blame] | 3348 | /* test with uninitalized SSL context */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3349 | ECJPAKE_TEST_SET_PASSWORD(MBEDTLS_ERR_SSL_BAD_INPUT_DATA); |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 3350 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3351 | mbedtls_ssl_config_init(&conf); |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 3352 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3353 | TEST_EQUAL(mbedtls_ssl_config_defaults(&conf, |
| 3354 | MBEDTLS_SSL_IS_CLIENT, |
| 3355 | MBEDTLS_SSL_TRANSPORT_STREAM, |
| 3356 | MBEDTLS_SSL_PRESET_DEFAULT), 0); |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 3357 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3358 | TEST_EQUAL(mbedtls_ssl_setup(&ssl, &conf), 0); |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 3359 | |
Valerio Setti | ba22c9c | 2022-12-06 11:42:33 +0100 | [diff] [blame] | 3360 | /* test with empty password or unitialized password key (depending on use_opaque_arg) */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3361 | ECJPAKE_TEST_SET_PASSWORD(MBEDTLS_ERR_SSL_BAD_INPUT_DATA); |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 3362 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3363 | pwd_len = strlen(ECJPAKE_TEST_PWD); |
| 3364 | memcpy(pwd_string, ECJPAKE_TEST_PWD, pwd_len); |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 3365 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3366 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 3367 | if (use_opaque_arg) { |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 3368 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Valerio Setti | 785116a | 2022-12-12 11:59:25 +0100 | [diff] [blame] | 3369 | psa_key_attributes_t check_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 3370 | |
Valerio Setti | 2a3ffb4 | 2022-12-08 16:27:46 +0100 | [diff] [blame] | 3371 | /* First try with an invalid usage */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3372 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
| 3373 | psa_set_key_algorithm(&attributes, PSA_ALG_JPAKE); |
| 3374 | psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD); |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 3375 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3376 | PSA_ASSERT(psa_import_key(&attributes, pwd_string, |
| 3377 | pwd_len, &pwd_slot)); |
Valerio Setti | 2a3ffb4 | 2022-12-08 16:27:46 +0100 | [diff] [blame] | 3378 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3379 | ECJPAKE_TEST_SET_PASSWORD(MBEDTLS_ERR_SSL_HW_ACCEL_FAILED); |
Valerio Setti | 2a3ffb4 | 2022-12-08 16:27:46 +0100 | [diff] [blame] | 3380 | |
Valerio Setti | 9d313df | 2022-12-09 11:38:59 +0100 | [diff] [blame] | 3381 | /* check that the opaque key is still valid after failure */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3382 | TEST_EQUAL(psa_get_key_attributes(pwd_slot, &check_attributes), |
| 3383 | PSA_SUCCESS); |
Valerio Setti | 9d313df | 2022-12-09 11:38:59 +0100 | [diff] [blame] | 3384 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3385 | psa_destroy_key(pwd_slot); |
Valerio Setti | 2a3ffb4 | 2022-12-08 16:27:46 +0100 | [diff] [blame] | 3386 | |
| 3387 | /* Then set the correct usage */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3388 | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE); |
Valerio Setti | 2a3ffb4 | 2022-12-08 16:27:46 +0100 | [diff] [blame] | 3389 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3390 | PSA_ASSERT(psa_import_key(&attributes, pwd_string, |
| 3391 | pwd_len, &pwd_slot)); |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 3392 | } |
| 3393 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 3394 | |
Valerio Setti | e7518ba | 2022-12-02 12:09:43 +0100 | [diff] [blame] | 3395 | /* final check which should work without errors */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3396 | ECJPAKE_TEST_SET_PASSWORD(0); |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 3397 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3398 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 3399 | if (use_opaque_arg) { |
| 3400 | psa_destroy_key(pwd_slot); |
Valerio Setti | 31e99bb | 2022-12-09 14:35:10 +0100 | [diff] [blame] | 3401 | } |
Valerio Setti | e7518ba | 2022-12-02 12:09:43 +0100 | [diff] [blame] | 3402 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3403 | mbedtls_ssl_free(&ssl); |
| 3404 | mbedtls_ssl_config_free(&conf); |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 3405 | |
Manuel Pégourié-Gonnard | 23fc437 | 2023-03-17 13:34:11 +0100 | [diff] [blame] | 3406 | MD_OR_USE_PSA_DONE(); |
Valerio Setti | 4452e98 | 2022-12-01 15:08:35 +0100 | [diff] [blame] | 3407 | } |
| 3408 | /* END_CASE */ |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 3409 | |
Valerio Setti | 2c12185 | 2023-01-09 18:00:39 +0100 | [diff] [blame] | 3410 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3411 | void elliptic_curve_get_properties() |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 3412 | { |
Przemek Stekiel | 75a5a9c | 2023-06-12 11:21:18 +0200 | [diff] [blame] | 3413 | psa_key_type_t psa_type = PSA_KEY_TYPE_NONE; |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 3414 | size_t psa_bits; |
| 3415 | |
Manuel Pégourié-Gonnard | 23fc437 | 2023-03-17 13:34:11 +0100 | [diff] [blame] | 3416 | MD_OR_USE_PSA_INIT(); |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 3417 | |
Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 3418 | #if defined(MBEDTLS_ECP_HAVE_SECP521R1) || defined(PSA_WANT_ECC_SECP_R1_521) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3419 | TEST_AVAILABLE_ECC(25, MBEDTLS_ECP_DP_SECP521R1, PSA_ECC_FAMILY_SECP_R1, 521); |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 3420 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3421 | TEST_UNAVAILABLE_ECC(25, MBEDTLS_ECP_DP_SECP521R1, PSA_ECC_FAMILY_SECP_R1, 521); |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 3422 | #endif |
Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 3423 | #if defined(MBEDTLS_ECP_HAVE_BP512R1) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3424 | TEST_AVAILABLE_ECC(28, MBEDTLS_ECP_DP_BP512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 512); |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 3425 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3426 | TEST_UNAVAILABLE_ECC(28, MBEDTLS_ECP_DP_BP512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 512); |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 3427 | #endif |
Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 3428 | #if defined(MBEDTLS_ECP_HAVE_SECP384R1) || defined(PSA_WANT_ECC_SECP_R1_384) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3429 | TEST_AVAILABLE_ECC(24, MBEDTLS_ECP_DP_SECP384R1, PSA_ECC_FAMILY_SECP_R1, 384); |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 3430 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3431 | TEST_UNAVAILABLE_ECC(24, MBEDTLS_ECP_DP_SECP384R1, PSA_ECC_FAMILY_SECP_R1, 384); |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 3432 | #endif |
Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 3433 | #if defined(MBEDTLS_ECP_HAVE_BP384R1) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3434 | TEST_AVAILABLE_ECC(27, MBEDTLS_ECP_DP_BP384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 384); |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 3435 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3436 | TEST_UNAVAILABLE_ECC(27, MBEDTLS_ECP_DP_BP384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 384); |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 3437 | #endif |
Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 3438 | #if defined(MBEDTLS_ECP_HAVE_SECP256R1) || defined(PSA_WANT_ECC_SECP_R1_256) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3439 | TEST_AVAILABLE_ECC(23, MBEDTLS_ECP_DP_SECP256R1, PSA_ECC_FAMILY_SECP_R1, 256); |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 3440 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3441 | TEST_UNAVAILABLE_ECC(23, MBEDTLS_ECP_DP_SECP256R1, PSA_ECC_FAMILY_SECP_R1, 256); |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 3442 | #endif |
Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 3443 | #if defined(MBEDTLS_ECP_HAVE_SECP256K1) || defined(PSA_WANT_ECC_SECP_K1_256) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3444 | TEST_AVAILABLE_ECC(22, MBEDTLS_ECP_DP_SECP256K1, PSA_ECC_FAMILY_SECP_K1, 256); |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 3445 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3446 | TEST_UNAVAILABLE_ECC(22, MBEDTLS_ECP_DP_SECP256K1, PSA_ECC_FAMILY_SECP_K1, 256); |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 3447 | #endif |
Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 3448 | #if defined(MBEDTLS_ECP_HAVE_BP256R1) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3449 | TEST_AVAILABLE_ECC(26, MBEDTLS_ECP_DP_BP256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 256); |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 3450 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3451 | TEST_UNAVAILABLE_ECC(26, MBEDTLS_ECP_DP_BP256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 256); |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 3452 | #endif |
Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 3453 | #if defined(MBEDTLS_ECP_HAVE_SECP224R1) || defined(PSA_WANT_ECC_SECP_R1_224) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3454 | TEST_AVAILABLE_ECC(21, MBEDTLS_ECP_DP_SECP224R1, PSA_ECC_FAMILY_SECP_R1, 224); |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 3455 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3456 | TEST_UNAVAILABLE_ECC(21, MBEDTLS_ECP_DP_SECP224R1, PSA_ECC_FAMILY_SECP_R1, 224); |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 3457 | #endif |
Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 3458 | #if defined(MBEDTLS_ECP_HAVE_SECP224K1) || defined(PSA_WANT_ECC_SECP_K1_224) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3459 | TEST_AVAILABLE_ECC(20, MBEDTLS_ECP_DP_SECP224K1, PSA_ECC_FAMILY_SECP_K1, 224); |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 3460 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3461 | TEST_UNAVAILABLE_ECC(20, MBEDTLS_ECP_DP_SECP224K1, PSA_ECC_FAMILY_SECP_K1, 224); |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 3462 | #endif |
Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 3463 | #if defined(MBEDTLS_ECP_HAVE_SECP192R1) || defined(PSA_WANT_ECC_SECP_R1_192) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3464 | TEST_AVAILABLE_ECC(19, MBEDTLS_ECP_DP_SECP192R1, PSA_ECC_FAMILY_SECP_R1, 192); |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 3465 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3466 | TEST_UNAVAILABLE_ECC(19, MBEDTLS_ECP_DP_SECP192R1, PSA_ECC_FAMILY_SECP_R1, 192); |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 3467 | #endif |
Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 3468 | #if defined(MBEDTLS_ECP_HAVE_SECP192K1) || defined(PSA_WANT_ECC_SECP_K1_192) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3469 | TEST_AVAILABLE_ECC(18, MBEDTLS_ECP_DP_SECP192K1, PSA_ECC_FAMILY_SECP_K1, 192); |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 3470 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3471 | TEST_UNAVAILABLE_ECC(18, MBEDTLS_ECP_DP_SECP192K1, PSA_ECC_FAMILY_SECP_K1, 192); |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 3472 | #endif |
Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 3473 | #if defined(MBEDTLS_ECP_HAVE_CURVE25519) || defined(PSA_WANT_ECC_MONTGOMERY_255) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3474 | TEST_AVAILABLE_ECC(29, MBEDTLS_ECP_DP_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY, 255); |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 3475 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3476 | TEST_UNAVAILABLE_ECC(29, MBEDTLS_ECP_DP_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY, 255); |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 3477 | #endif |
Valerio Setti | db6b4db | 2023-09-01 09:20:51 +0200 | [diff] [blame] | 3478 | #if defined(MBEDTLS_ECP_HAVE_CURVE448) || defined(PSA_WANT_ECC_MONTGOMERY_448) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3479 | TEST_AVAILABLE_ECC(30, MBEDTLS_ECP_DP_CURVE448, PSA_ECC_FAMILY_MONTGOMERY, 448); |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 3480 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 3481 | TEST_UNAVAILABLE_ECC(30, MBEDTLS_ECP_DP_CURVE448, PSA_ECC_FAMILY_MONTGOMERY, 448); |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 3482 | #endif |
valerio | 32f2ac9 | 2023-04-20 11:59:52 +0200 | [diff] [blame] | 3483 | goto exit; |
| 3484 | exit: |
Manuel Pégourié-Gonnard | 23fc437 | 2023-03-17 13:34:11 +0100 | [diff] [blame] | 3485 | MD_OR_USE_PSA_DONE(); |
Valerio Setti | 73260b6 | 2023-01-03 12:53:28 +0100 | [diff] [blame] | 3486 | } |
| 3487 | /* END_CASE */ |