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