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