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