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