blob: 15ec5bea119905e942bd55d94de985b9f32bbb33 [file] [log] [blame]
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02001/* BEGIN_HEADER */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002#include <mbedtls/ssl.h>
Chris Jones84a773f2021-03-05 18:38:47 +00003#include <ssl_misc.h>
Andrzej Kurek941962e2020-02-07 09:20:32 -05004#include <mbedtls/timing.h>
Piotr Nowickibde7ee82020-02-21 10:59:50 +01005#include <mbedtls/debug.h>
Hanno Becker73c825a2020-09-08 10:52:58 +01006#include <ssl_tls13_keys.h>
Gabor Mezeib35759d2022-02-09 16:59:11 +01007#include <ssl_tls13_invasive.h>
Mateusz Starzyk1aec6462021-02-08 15:34:42 +01008#include "test/certs.h"
Piotr Nowickibde7ee82020-02-21 10:59:50 +01009
Andrzej Kurek780dc182022-06-10 08:57:19 -040010#if defined(MBEDTLS_SSL_CACHE_C)
11#include "mbedtls/ssl_cache.h"
12#endif
13
Manuel Pégourié-Gonnard07018f92022-09-15 11:29:35 +020014#include <mbedtls/legacy_or_psa.h>
Andrzej Kurek21b68702022-08-17 16:26:12 -040015#include "hash_info.h"
Hanno Becker6667ffd2021-04-19 21:59:22 +010016
Gabor Mezei22c9a6f2021-10-20 12:09:35 +020017#include <constant_time_internal.h>
Manuel Pégourié-Gonnard9670a592020-07-10 10:21:46 +020018#include <test/constant_flow.h>
19
Gilles Peskine449bd832023-01-11 14:50:10 +010020enum {
21#define MBEDTLS_SSL_TLS1_3_LABEL(name, string) \
22 tls13_label_ ## name,
23 MBEDTLS_SSL_TLS1_3_LABEL_LIST
Hanno Becker70d7fb02020-09-09 10:11:21 +010024#undef MBEDTLS_SSL_TLS1_3_LABEL
Hanno Becker1413bd82020-09-09 12:46:09 +010025};
Hanno Becker70d7fb02020-09-09 10:11:21 +010026
Gilles Peskine449bd832023-01-11 14:50:10 +010027typedef struct log_pattern {
Piotr Nowickibde7ee82020-02-21 10:59:50 +010028 const char *pattern;
29 size_t counter;
30} log_pattern;
31
Ronald Crone68ab4f2022-10-05 12:46:29 +020032#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Andrzej Kurekcccb0442022-08-19 03:42:11 -040033static int rng_seed = 0xBEEF;
Gilles Peskine449bd832023-01-11 14:50:10 +010034static int rng_get(void *p_rng, unsigned char *output, size_t output_len)
Andrzej Kurek635c2c22022-08-17 15:20:40 -040035{
36 (void) p_rng;
Gilles Peskine449bd832023-01-11 14:50:10 +010037 for (size_t i = 0; i < output_len; i++) {
Andrzej Kurekcccb0442022-08-19 03:42:11 -040038 output[i] = rand();
Gilles Peskine449bd832023-01-11 14:50:10 +010039 }
Andrzej Kurekcccb0442022-08-19 03:42:11 -040040
Andrzej Kurek635c2c22022-08-17 15:20:40 -040041 return 0;
42}
Andrzej Kurek32bd0632022-08-20 14:10:36 -040043#endif
Andrzej Kurek635c2c22022-08-17 15:20:40 -040044
Piotr Nowicki438bf3b2020-03-10 12:59:10 +010045/*
46 * This function can be passed to mbedtls to receive output logs from it. In
Piotr Nowickibde7ee82020-02-21 10:59:50 +010047 * this case, it will count the instances of a log_pattern in the received
48 * logged messages.
49 */
Gilles Peskine449bd832023-01-11 14:50:10 +010050void log_analyzer(void *ctx, int level,
51 const char *file, int line,
52 const char *str)
Piotr Nowickibde7ee82020-02-21 10:59:50 +010053{
54 log_pattern *p = (log_pattern *) ctx;
55
56 (void) level;
57 (void) line;
58 (void) file;
59
Gilles Peskine449bd832023-01-11 14:50:10 +010060 if (NULL != p &&
Piotr Nowickibde7ee82020-02-21 10:59:50 +010061 NULL != p->pattern &&
Gilles Peskine449bd832023-01-11 14:50:10 +010062 NULL != strstr(str, p->pattern)) {
Piotr Nowickibde7ee82020-02-21 10:59:50 +010063 p->counter++;
64 }
65}
Janos Follath6264e662019-11-26 11:11:15 +000066
Gilles Peskine449bd832023-01-11 14:50:10 +010067typedef struct handshake_test_options {
Andrzej Kurek8a6ff152020-02-26 09:10:14 -050068 const char *cipher;
Glenn Strauss39e624c2022-04-11 13:33:16 -040069 mbedtls_ssl_protocol_version client_min_version;
70 mbedtls_ssl_protocol_version client_max_version;
71 mbedtls_ssl_protocol_version server_min_version;
72 mbedtls_ssl_protocol_version server_max_version;
73 mbedtls_ssl_protocol_version expected_negotiated_version;
Neil Armstrong8c52ed82022-05-27 13:14:55 +020074 int expected_handshake_result;
75 int expected_ciphersuite;
Andrzej Kurek8a6ff152020-02-26 09:10:14 -050076 int pk_alg;
Neil Armstrong8c52ed82022-05-27 13:14:55 +020077 int opaque_alg;
78 int opaque_alg2;
79 int opaque_usage;
Andrzej Kurek8a6ff152020-02-26 09:10:14 -050080 data_t *psk_str;
81 int dtls;
Piotr Nowickibde7ee82020-02-21 10:59:50 +010082 int srv_auth_mode;
Andrzej Kurek8a6ff152020-02-26 09:10:14 -050083 int serialize;
84 int mfl;
85 int cli_msg_len;
86 int srv_msg_len;
87 int expected_cli_fragments;
88 int expected_srv_fragments;
89 int renegotiate;
90 int legacy_renegotiation;
Piotr Nowickibde7ee82020-02-21 10:59:50 +010091 void *srv_log_obj;
92 void *cli_log_obj;
93 void (*srv_log_fun)(void *, int, const char *, int, const char *);
94 void (*cli_log_fun)(void *, int, const char *, int, const char *);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -050095 int resize_buffers;
Andrzej Kurek780dc182022-06-10 08:57:19 -040096#if defined(MBEDTLS_SSL_CACHE_C)
Andrzej Kurek92d74172022-06-28 10:29:42 -040097 mbedtls_ssl_cache_context *cache;
Andrzej Kurek780dc182022-06-10 08:57:19 -040098#endif
Andrzej Kurek8a6ff152020-02-26 09:10:14 -050099} handshake_test_options;
100
Gilles Peskine449bd832023-01-11 14:50:10 +0100101void init_handshake_options(handshake_test_options *opts)
Andrzej Kurek8a6ff152020-02-26 09:10:14 -0500102{
Ronald Crone68ab4f2022-10-05 12:46:29 +0200103#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 srand(rng_seed);
Andrzej Kurek635c2c22022-08-17 15:20:40 -0400105 rng_seed += 0xD0;
Andrzej Kurek32bd0632022-08-20 14:10:36 -0400106#endif
Andrzej Kurek3d0d5012022-07-04 05:20:55 -0400107 opts->cipher = "";
108 opts->client_min_version = MBEDTLS_SSL_VERSION_UNKNOWN;
109 opts->client_max_version = MBEDTLS_SSL_VERSION_UNKNOWN;
110 opts->server_min_version = MBEDTLS_SSL_VERSION_UNKNOWN;
111 opts->server_max_version = MBEDTLS_SSL_VERSION_UNKNOWN;
112 opts->expected_negotiated_version = MBEDTLS_SSL_VERSION_TLS1_2;
113 opts->expected_handshake_result = 0;
114 opts->expected_ciphersuite = 0;
115 opts->pk_alg = MBEDTLS_PK_RSA;
116 opts->opaque_alg = 0;
117 opts->opaque_alg2 = 0;
118 opts->opaque_usage = 0;
119 opts->psk_str = NULL;
120 opts->dtls = 0;
121 opts->srv_auth_mode = MBEDTLS_SSL_VERIFY_NONE;
122 opts->serialize = 0;
123 opts->mfl = MBEDTLS_SSL_MAX_FRAG_LEN_NONE;
124 opts->cli_msg_len = 100;
125 opts->srv_msg_len = 100;
126 opts->expected_cli_fragments = 1;
127 opts->expected_srv_fragments = 1;
128 opts->renegotiate = 0;
129 opts->legacy_renegotiation = MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION;
130 opts->srv_log_obj = NULL;
131 opts->srv_log_obj = NULL;
132 opts->srv_log_fun = NULL;
133 opts->cli_log_fun = NULL;
134 opts->resize_buffers = 1;
Andrzej Kurek780dc182022-06-10 08:57:19 -0400135#if defined(MBEDTLS_SSL_CACHE_C)
Andrzej Kurek9dc44022022-07-04 05:46:15 -0400136 opts->cache = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100137 ASSERT_ALLOC(opts->cache, 1);
138 mbedtls_ssl_cache_init(opts->cache);
Andrzej Kurek3d0d5012022-07-04 05:20:55 -0400139exit:
140 return;
Andrzej Kurek780dc182022-06-10 08:57:19 -0400141#endif
142}
143
Gilles Peskine449bd832023-01-11 14:50:10 +0100144void free_handshake_options(handshake_test_options *opts)
Andrzej Kurek780dc182022-06-10 08:57:19 -0400145{
146#if defined(MBEDTLS_SSL_CACHE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 mbedtls_ssl_cache_free(opts->cache);
148 mbedtls_free(opts->cache);
Andrzej Kurek780dc182022-06-10 08:57:19 -0400149#else
150 (void) opts;
151#endif
Andrzej Kurek8a6ff152020-02-26 09:10:14 -0500152}
Ronald Crone3dac4a2022-06-10 17:21:51 +0200153
Ronald Crone7b9b6b2022-06-10 17:24:31 +0200154#if defined(MBEDTLS_TEST_HOOKS)
155static void set_chk_buf_ptr_args(
156 mbedtls_ssl_chk_buf_ptr_args *args,
Gilles Peskine449bd832023-01-11 14:50:10 +0100157 unsigned char *cur, unsigned char *end, size_t need)
Ronald Crone7b9b6b2022-06-10 17:24:31 +0200158{
159 args->cur = cur;
160 args->end = end;
161 args->need = need;
162}
163
Gilles Peskine449bd832023-01-11 14:50:10 +0100164static void reset_chk_buf_ptr_args(mbedtls_ssl_chk_buf_ptr_args *args)
Ronald Crone7b9b6b2022-06-10 17:24:31 +0200165{
Gilles Peskine449bd832023-01-11 14:50:10 +0100166 memset(args, 0, sizeof(*args));
Ronald Crone7b9b6b2022-06-10 17:24:31 +0200167}
168#endif /* MBEDTLS_TEST_HOOKS */
169
Janos Follath6264e662019-11-26 11:11:15 +0000170/*
171 * Buffer structure for custom I/O callbacks.
172 */
173
Gilles Peskine449bd832023-01-11 14:50:10 +0100174typedef struct mbedtls_test_buffer {
Janos Follath6264e662019-11-26 11:11:15 +0000175 size_t start;
176 size_t content_length;
177 size_t capacity;
178 unsigned char *buffer;
179} mbedtls_test_buffer;
180
181/*
182 * Initialises \p buf. After calling this function it is safe to call
183 * `mbedtls_test_buffer_free()` on \p buf.
184 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100185void mbedtls_test_buffer_init(mbedtls_test_buffer *buf)
Janos Follath6264e662019-11-26 11:11:15 +0000186{
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 memset(buf, 0, sizeof(*buf));
Janos Follath6264e662019-11-26 11:11:15 +0000188}
189
190/*
191 * Sets up \p buf. After calling this function it is safe to call
192 * `mbedtls_test_buffer_put()` and `mbedtls_test_buffer_get()` on \p buf.
193 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100194int mbedtls_test_buffer_setup(mbedtls_test_buffer *buf, size_t capacity)
Janos Follath6264e662019-11-26 11:11:15 +0000195{
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 buf->buffer = (unsigned char *) mbedtls_calloc(capacity,
197 sizeof(unsigned char));
198 if (NULL == buf->buffer) {
Janos Follath6264e662019-11-26 11:11:15 +0000199 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 }
Janos Follath6264e662019-11-26 11:11:15 +0000201 buf->capacity = capacity;
202
203 return 0;
204}
205
Gilles Peskine449bd832023-01-11 14:50:10 +0100206void mbedtls_test_buffer_free(mbedtls_test_buffer *buf)
Janos Follath6264e662019-11-26 11:11:15 +0000207{
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 if (buf->buffer != NULL) {
209 mbedtls_free(buf->buffer);
210 }
Janos Follath6264e662019-11-26 11:11:15 +0000211
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 memset(buf, 0, sizeof(*buf));
Janos Follath6264e662019-11-26 11:11:15 +0000213}
214
215/*
216 * Puts \p input_len bytes from the \p input buffer into the ring buffer \p buf.
217 *
218 * \p buf must have been initialized and set up by calling
219 * `mbedtls_test_buffer_init()` and `mbedtls_test_buffer_setup()`.
220 *
221 * \retval \p input_len, if the data fits.
222 * \retval 0 <= value < \p input_len, if the data does not fit.
223 * \retval -1, if \p buf is NULL, it hasn't been set up or \p input_len is not
224 * zero and \p input is NULL.
225 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100226int mbedtls_test_buffer_put(mbedtls_test_buffer *buf,
227 const unsigned char *input, size_t input_len)
Janos Follath6264e662019-11-26 11:11:15 +0000228{
229 size_t overflow = 0;
230
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 if ((buf == NULL) || (buf->buffer == NULL)) {
Janos Follath6264e662019-11-26 11:11:15 +0000232 return -1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100233 }
Janos Follath6264e662019-11-26 11:11:15 +0000234
235 /* Reduce input_len to a number that fits in the buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 if ((buf->content_length + input_len) > buf->capacity) {
Janos Follath6264e662019-11-26 11:11:15 +0000237 input_len = buf->capacity - buf->content_length;
238 }
239
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 if (input == NULL) {
241 return (input_len == 0) ? 0 : -1;
Janos Follath6264e662019-11-26 11:11:15 +0000242 }
243
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 /* Check if the buffer has not come full circle and free space is not in
245 * the middle */
246 if (buf->start + buf->content_length < buf->capacity) {
Piotr Nowickifb437d72020-01-13 16:59:12 +0100247
248 /* Calculate the number of bytes that need to be placed at lower memory
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 * address */
250 if (buf->start + buf->content_length + input_len
251 > buf->capacity) {
252 overflow = (buf->start + buf->content_length + input_len)
253 % buf->capacity;
Piotr Nowickifb437d72020-01-13 16:59:12 +0100254 }
255
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 memcpy(buf->buffer + buf->start + buf->content_length, input,
257 input_len - overflow);
258 memcpy(buf->buffer, input + input_len - overflow, overflow);
Piotr Nowickifb437d72020-01-13 16:59:12 +0100259
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 } else {
Piotr Nowickifb437d72020-01-13 16:59:12 +0100261 /* The buffer has come full circle and free space is in the middle */
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 memcpy(buf->buffer + buf->start + buf->content_length - buf->capacity,
263 input, input_len);
Janos Follath6264e662019-11-26 11:11:15 +0000264 }
265
Janos Follath6264e662019-11-26 11:11:15 +0000266 buf->content_length += input_len;
Janos Follath6264e662019-11-26 11:11:15 +0000267 return input_len;
268}
269
270/*
Andrzej Kurekf7774142020-01-22 06:34:59 -0500271 * Gets \p output_len bytes from the ring buffer \p buf into the
272 * \p output buffer. The output buffer can be NULL, in this case a part of the
273 * ring buffer will be dropped, if the requested length is available.
Janos Follath6264e662019-11-26 11:11:15 +0000274 *
275 * \p buf must have been initialized and set up by calling
276 * `mbedtls_test_buffer_init()` and `mbedtls_test_buffer_setup()`.
277 *
278 * \retval \p output_len, if the data is available.
279 * \retval 0 <= value < \p output_len, if the data is not available.
Andrzej Kurekf7774142020-01-22 06:34:59 -0500280 * \retval -1, if \buf is NULL or it hasn't been set up.
Janos Follath6264e662019-11-26 11:11:15 +0000281 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100282int mbedtls_test_buffer_get(mbedtls_test_buffer *buf,
283 unsigned char *output, size_t output_len)
Janos Follath6264e662019-11-26 11:11:15 +0000284{
285 size_t overflow = 0;
286
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 if ((buf == NULL) || (buf->buffer == NULL)) {
Janos Follath6264e662019-11-26 11:11:15 +0000288 return -1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 }
Janos Follath6264e662019-11-26 11:11:15 +0000290
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 if (output == NULL && output_len == 0) {
Andrzej Kurekf7774142020-01-22 06:34:59 -0500292 return 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 }
Janos Follath6264e662019-11-26 11:11:15 +0000294
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 if (buf->content_length < output_len) {
Janos Follath6264e662019-11-26 11:11:15 +0000296 output_len = buf->content_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 }
Janos Follath6264e662019-11-26 11:11:15 +0000298
299 /* Calculate the number of bytes that need to be drawn from lower memory
300 * address */
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 if (buf->start + output_len > buf->capacity) {
302 overflow = (buf->start + output_len) % buf->capacity;
Janos Follath6264e662019-11-26 11:11:15 +0000303 }
304
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 if (output != NULL) {
306 memcpy(output, buf->buffer + buf->start, output_len - overflow);
307 memcpy(output + output_len - overflow, buf->buffer, overflow);
Andrzej Kurekf7774142020-01-22 06:34:59 -0500308 }
309
Janos Follath6264e662019-11-26 11:11:15 +0000310 buf->content_length -= output_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 buf->start = (buf->start + output_len) % buf->capacity;
Janos Follath6264e662019-11-26 11:11:15 +0000312
313 return output_len;
314}
315
Hanno Beckera18d1322018-01-03 14:27:32 +0000316/*
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500317 * Errors used in the message transport mock tests
318 */
319 #define MBEDTLS_TEST_ERROR_ARG_NULL -11
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500320 #define MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED -44
321
322/*
323 * Context for a message metadata queue (fifo) that is on top of the ring buffer.
324 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100325typedef struct mbedtls_test_message_queue {
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500326 size_t *messages;
327 int pos;
328 int num;
329 int capacity;
330} mbedtls_test_message_queue;
331
332/*
333 * Setup and free functions for the message metadata queue.
334 *
335 * \p capacity describes the number of message metadata chunks that can be held
336 * within the queue.
337 *
338 * \retval 0, if a metadata queue of a given length can be allocated.
339 * \retval MBEDTLS_ERR_SSL_ALLOC_FAILED, if allocation failed.
340 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100341int mbedtls_test_message_queue_setup(mbedtls_test_message_queue *queue,
342 size_t capacity)
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500343{
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 queue->messages = (size_t *) mbedtls_calloc(capacity, sizeof(size_t));
345 if (NULL == queue->messages) {
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500346 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 }
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500348
349 queue->capacity = capacity;
350 queue->pos = 0;
351 queue->num = 0;
352
353 return 0;
354}
355
Gilles Peskine449bd832023-01-11 14:50:10 +0100356void mbedtls_test_message_queue_free(mbedtls_test_message_queue *queue)
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500357{
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 if (queue == NULL) {
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500359 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 }
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500361
Gilles Peskine449bd832023-01-11 14:50:10 +0100362 if (queue->messages != NULL) {
363 mbedtls_free(queue->messages);
364 }
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500365
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 memset(queue, 0, sizeof(*queue));
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500367}
368
369/*
370 * Push message length information onto the message metadata queue.
371 * This will become the last element to leave it (fifo).
372 *
373 * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null.
Andrzej Kurekf46b9122020-02-07 08:19:00 -0500374 * \retval MBEDTLS_ERR_SSL_WANT_WRITE, if the queue is full.
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500375 * \retval \p len, if the push was successful.
376 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100377int mbedtls_test_message_queue_push_info(mbedtls_test_message_queue *queue,
378 size_t len)
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500379{
380 int place;
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 if (queue == NULL) {
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500382 return MBEDTLS_TEST_ERROR_ARG_NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100383 }
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500384
Gilles Peskine449bd832023-01-11 14:50:10 +0100385 if (queue->num >= queue->capacity) {
Andrzej Kurekf46b9122020-02-07 08:19:00 -0500386 return MBEDTLS_ERR_SSL_WANT_WRITE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 }
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500388
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 place = (queue->pos + queue->num) % queue->capacity;
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500390 queue->messages[place] = len;
391 queue->num++;
392 return len;
393}
394
395/*
396 * Pop information about the next message length from the queue. This will be
397 * the oldest inserted message length(fifo). \p msg_len can be null, in which
398 * case the data will be popped from the queue but not copied anywhere.
399 *
400 * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null.
Andrzej Kurekf46b9122020-02-07 08:19:00 -0500401 * \retval MBEDTLS_ERR_SSL_WANT_READ, if the queue is empty.
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500402 * \retval message length, if the pop was successful, up to the given
403 \p buf_len.
404 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100405int mbedtls_test_message_queue_pop_info(mbedtls_test_message_queue *queue,
406 size_t buf_len)
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500407{
408 size_t message_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100409 if (queue == NULL) {
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500410 return MBEDTLS_TEST_ERROR_ARG_NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100411 }
412 if (queue->num == 0) {
Andrzej Kurekf46b9122020-02-07 08:19:00 -0500413 return MBEDTLS_ERR_SSL_WANT_READ;
Gilles Peskine449bd832023-01-11 14:50:10 +0100414 }
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500415
416 message_length = queue->messages[queue->pos];
417 queue->messages[queue->pos] = 0;
418 queue->num--;
419 queue->pos++;
420 queue->pos %= queue->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 if (queue->pos < 0) {
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500422 queue->pos += queue->capacity;
Gilles Peskine449bd832023-01-11 14:50:10 +0100423 }
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500424
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 return (message_length > buf_len) ? buf_len : message_length;
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500426}
427
428/*
429 * Take a peek on the info about the next message length from the queue.
430 * This will be the oldest inserted message length(fifo).
431 *
432 * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null.
Andrzej Kurekf46b9122020-02-07 08:19:00 -0500433 * \retval MBEDTLS_ERR_SSL_WANT_READ, if the queue is empty.
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500434 * \retval 0, if the peek was successful.
435 * \retval MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED, if the given buffer length is
436 * too small to fit the message. In this case the \p msg_len will be
437 * set to the full message length so that the
438 * caller knows what portion of the message can be dropped.
439 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100440int mbedtls_test_message_queue_peek_info(mbedtls_test_message_queue *queue,
441 size_t buf_len, size_t *msg_len)
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500442{
Gilles Peskine449bd832023-01-11 14:50:10 +0100443 if (queue == NULL || msg_len == NULL) {
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500444 return MBEDTLS_TEST_ERROR_ARG_NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 }
446 if (queue->num == 0) {
Andrzej Kurekf46b9122020-02-07 08:19:00 -0500447 return MBEDTLS_ERR_SSL_WANT_READ;
Gilles Peskine449bd832023-01-11 14:50:10 +0100448 }
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500449
450 *msg_len = queue->messages[queue->pos];
Gilles Peskine449bd832023-01-11 14:50:10 +0100451 return (*msg_len > buf_len) ? MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED : 0;
Andrzej Kurek13719cd2020-01-22 06:36:39 -0500452}
453/*
Janos Follath031827f2019-11-27 11:12:14 +0000454 * Context for the I/O callbacks simulating network connection.
455 */
456
457#define MBEDTLS_MOCK_SOCKET_CONNECTED 1
458
Gilles Peskine449bd832023-01-11 14:50:10 +0100459typedef struct mbedtls_mock_socket {
Janos Follath031827f2019-11-27 11:12:14 +0000460 int status;
461 mbedtls_test_buffer *input;
462 mbedtls_test_buffer *output;
463 struct mbedtls_mock_socket *peer;
464} mbedtls_mock_socket;
465
466/*
467 * Setup and teardown functions for mock sockets.
468 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100469void mbedtls_mock_socket_init(mbedtls_mock_socket *socket)
Janos Follath031827f2019-11-27 11:12:14 +0000470{
Gilles Peskine449bd832023-01-11 14:50:10 +0100471 memset(socket, 0, sizeof(*socket));
Janos Follath031827f2019-11-27 11:12:14 +0000472}
473
474/*
475 * Closes the socket \p socket.
476 *
477 * \p socket must have been previously initialized by calling
478 * mbedtls_mock_socket_init().
479 *
480 * This function frees all allocated resources and both sockets are aware of the
481 * new connection state.
482 *
483 * That is, this function does not simulate half-open TCP connections and the
484 * phenomenon that when closing a UDP connection the peer is not aware of the
485 * connection having been closed.
486 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100487void mbedtls_mock_socket_close(mbedtls_mock_socket *socket)
Janos Follath031827f2019-11-27 11:12:14 +0000488{
Gilles Peskine449bd832023-01-11 14:50:10 +0100489 if (socket == NULL) {
Janos Follath031827f2019-11-27 11:12:14 +0000490 return;
Janos Follath031827f2019-11-27 11:12:14 +0000491 }
492
Gilles Peskine449bd832023-01-11 14:50:10 +0100493 if (socket->input != NULL) {
494 mbedtls_test_buffer_free(socket->input);
495 mbedtls_free(socket->input);
Janos Follath031827f2019-11-27 11:12:14 +0000496 }
497
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 if (socket->output != NULL) {
499 mbedtls_test_buffer_free(socket->output);
500 mbedtls_free(socket->output);
501 }
Janos Follath031827f2019-11-27 11:12:14 +0000502
Gilles Peskine449bd832023-01-11 14:50:10 +0100503 if (socket->peer != NULL) {
504 memset(socket->peer, 0, sizeof(*socket->peer));
505 }
506
507 memset(socket, 0, sizeof(*socket));
Janos Follath031827f2019-11-27 11:12:14 +0000508}
509
510/*
511 * Establishes a connection between \p peer1 and \p peer2.
512 *
513 * \p peer1 and \p peer2 must have been previously initialized by calling
514 * mbedtls_mock_socket_init().
515 *
Tom Cosgrove1797b052022-12-04 17:19:59 +0000516 * The capacities of the internal buffers are set to \p bufsize. Setting this to
Janos Follath031827f2019-11-27 11:12:14 +0000517 * the correct value allows for simulation of MTU, sanity testing the mock
518 * implementation and mocking TCP connections with lower memory cost.
519 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100520int mbedtls_mock_socket_connect(mbedtls_mock_socket *peer1,
521 mbedtls_mock_socket *peer2,
522 size_t bufsize)
Janos Follath031827f2019-11-27 11:12:14 +0000523{
524 int ret = -1;
525
Piotr Nowickid796e192020-01-28 12:09:47 +0100526 peer1->output =
Gilles Peskine449bd832023-01-11 14:50:10 +0100527 (mbedtls_test_buffer *) mbedtls_calloc(1, sizeof(mbedtls_test_buffer));
528 if (peer1->output == NULL) {
Janos Follath031827f2019-11-27 11:12:14 +0000529 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
530 goto exit;
531 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100532 mbedtls_test_buffer_init(peer1->output);
533 if (0 != (ret = mbedtls_test_buffer_setup(peer1->output, bufsize))) {
Janos Follath031827f2019-11-27 11:12:14 +0000534 goto exit;
535 }
536
Piotr Nowickid796e192020-01-28 12:09:47 +0100537 peer2->output =
Gilles Peskine449bd832023-01-11 14:50:10 +0100538 (mbedtls_test_buffer *) mbedtls_calloc(1, sizeof(mbedtls_test_buffer));
539 if (peer2->output == NULL) {
Piotr Nowickid796e192020-01-28 12:09:47 +0100540 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
541 goto exit;
542 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 mbedtls_test_buffer_init(peer2->output);
544 if (0 != (ret = mbedtls_test_buffer_setup(peer2->output, bufsize))) {
Piotr Nowickid796e192020-01-28 12:09:47 +0100545 goto exit;
546 }
547
Janos Follath031827f2019-11-27 11:12:14 +0000548 peer1->peer = peer2;
549 peer2->peer = peer1;
Piotr Nowickid796e192020-01-28 12:09:47 +0100550 peer1->input = peer2->output;
551 peer2->input = peer1->output;
Janos Follath031827f2019-11-27 11:12:14 +0000552
553 peer1->status = peer2->status = MBEDTLS_MOCK_SOCKET_CONNECTED;
554 ret = 0;
555
556exit:
557
Gilles Peskine449bd832023-01-11 14:50:10 +0100558 if (ret != 0) {
559 mbedtls_mock_socket_close(peer1);
560 mbedtls_mock_socket_close(peer2);
Janos Follath031827f2019-11-27 11:12:14 +0000561 }
562
563 return ret;
564}
565
566/*
567 * Callbacks for simulating blocking I/O over connection-oriented transport.
568 */
569
Gilles Peskine449bd832023-01-11 14:50:10 +0100570int mbedtls_mock_tcp_send_b(void *ctx, const unsigned char *buf, size_t len)
Janos Follath031827f2019-11-27 11:12:14 +0000571{
Gilles Peskine449bd832023-01-11 14:50:10 +0100572 mbedtls_mock_socket *socket = (mbedtls_mock_socket *) ctx;
Janos Follath031827f2019-11-27 11:12:14 +0000573
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 if (socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED) {
Janos Follath031827f2019-11-27 11:12:14 +0000575 return -1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100576 }
Janos Follath031827f2019-11-27 11:12:14 +0000577
Gilles Peskine449bd832023-01-11 14:50:10 +0100578 return mbedtls_test_buffer_put(socket->output, buf, len);
Janos Follath031827f2019-11-27 11:12:14 +0000579}
580
Gilles Peskine449bd832023-01-11 14:50:10 +0100581int mbedtls_mock_tcp_recv_b(void *ctx, unsigned char *buf, size_t len)
Janos Follath031827f2019-11-27 11:12:14 +0000582{
Gilles Peskine449bd832023-01-11 14:50:10 +0100583 mbedtls_mock_socket *socket = (mbedtls_mock_socket *) ctx;
Janos Follath031827f2019-11-27 11:12:14 +0000584
Gilles Peskine449bd832023-01-11 14:50:10 +0100585 if (socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED) {
Janos Follath031827f2019-11-27 11:12:14 +0000586 return -1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100587 }
Janos Follath031827f2019-11-27 11:12:14 +0000588
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 return mbedtls_test_buffer_get(socket->input, buf, len);
Janos Follath031827f2019-11-27 11:12:14 +0000590}
591
592/*
Janos Follath3766ba52019-11-27 13:31:42 +0000593 * Callbacks for simulating non-blocking I/O over connection-oriented transport.
594 */
595
Gilles Peskine449bd832023-01-11 14:50:10 +0100596int mbedtls_mock_tcp_send_nb(void *ctx, const unsigned char *buf, size_t len)
Janos Follath3766ba52019-11-27 13:31:42 +0000597{
Gilles Peskine449bd832023-01-11 14:50:10 +0100598 mbedtls_mock_socket *socket = (mbedtls_mock_socket *) ctx;
Janos Follath3766ba52019-11-27 13:31:42 +0000599
Gilles Peskine449bd832023-01-11 14:50:10 +0100600 if (socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED) {
Janos Follath3766ba52019-11-27 13:31:42 +0000601 return -1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100602 }
Janos Follath3766ba52019-11-27 13:31:42 +0000603
Gilles Peskine449bd832023-01-11 14:50:10 +0100604 if (socket->output->capacity == socket->output->content_length) {
Janos Follath3766ba52019-11-27 13:31:42 +0000605 return MBEDTLS_ERR_SSL_WANT_WRITE;
606 }
607
Gilles Peskine449bd832023-01-11 14:50:10 +0100608 return mbedtls_test_buffer_put(socket->output, buf, len);
Janos Follath3766ba52019-11-27 13:31:42 +0000609}
610
Gilles Peskine449bd832023-01-11 14:50:10 +0100611int mbedtls_mock_tcp_recv_nb(void *ctx, unsigned char *buf, size_t len)
Janos Follath3766ba52019-11-27 13:31:42 +0000612{
Gilles Peskine449bd832023-01-11 14:50:10 +0100613 mbedtls_mock_socket *socket = (mbedtls_mock_socket *) ctx;
Janos Follath3766ba52019-11-27 13:31:42 +0000614
Gilles Peskine449bd832023-01-11 14:50:10 +0100615 if (socket == NULL || socket->status != MBEDTLS_MOCK_SOCKET_CONNECTED) {
Janos Follath3766ba52019-11-27 13:31:42 +0000616 return -1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100617 }
Janos Follath3766ba52019-11-27 13:31:42 +0000618
Gilles Peskine449bd832023-01-11 14:50:10 +0100619 if (socket->input->content_length == 0) {
Janos Follath3766ba52019-11-27 13:31:42 +0000620 return MBEDTLS_ERR_SSL_WANT_READ;
621 }
622
Gilles Peskine449bd832023-01-11 14:50:10 +0100623 return mbedtls_test_buffer_get(socket->input, buf, len);
Janos Follath3766ba52019-11-27 13:31:42 +0000624}
625
Andrzej Kurekbc483de2020-01-22 03:40:00 -0500626/* Errors used in the message socket mocks */
627
628#define MBEDTLS_TEST_ERROR_CONTEXT_ERROR -55
629#define MBEDTLS_TEST_ERROR_SEND_FAILED -66
630#define MBEDTLS_TEST_ERROR_RECV_FAILED -77
631
632/*
633 * Structure used as an addon, or a wrapper, around the mocked sockets.
634 * Contains an input queue, to which the other socket pushes metadata,
635 * and an output queue, to which this one pushes metadata. This context is
636 * considered as an owner of the input queue only, which is initialized and
637 * freed in the respective setup and free calls.
638 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100639typedef struct mbedtls_test_message_socket_context {
640 mbedtls_test_message_queue *queue_input;
641 mbedtls_test_message_queue *queue_output;
642 mbedtls_mock_socket *socket;
Andrzej Kurekbc483de2020-01-22 03:40:00 -0500643} mbedtls_test_message_socket_context;
644
Gilles Peskine449bd832023-01-11 14:50:10 +0100645void mbedtls_message_socket_init(mbedtls_test_message_socket_context *ctx)
Andrzej Kurek45916ba2020-03-05 14:46:22 -0500646{
647 ctx->queue_input = NULL;
648 ctx->queue_output = NULL;
649 ctx->socket = NULL;
650}
651
Andrzej Kurekbc483de2020-01-22 03:40:00 -0500652/*
Tom Cosgrove1797b052022-12-04 17:19:59 +0000653 * Setup a given message socket context including initialization of
Andrzej Kurekbc483de2020-01-22 03:40:00 -0500654 * input/output queues to a chosen capacity of messages. Also set the
655 * corresponding mock socket.
656 *
657 * \retval 0, if everything succeeds.
658 * \retval MBEDTLS_ERR_SSL_ALLOC_FAILED, if allocation of a message
659 * queue failed.
660 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100661int mbedtls_message_socket_setup(mbedtls_test_message_queue *queue_input,
662 mbedtls_test_message_queue *queue_output,
663 size_t queue_capacity,
664 mbedtls_mock_socket *socket,
665 mbedtls_test_message_socket_context *ctx)
Andrzej Kurekbc483de2020-01-22 03:40:00 -0500666{
Gilles Peskine449bd832023-01-11 14:50:10 +0100667 int ret = mbedtls_test_message_queue_setup(queue_input, queue_capacity);
668 if (ret != 0) {
Andrzej Kurekbc483de2020-01-22 03:40:00 -0500669 return ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100670 }
Andrzej Kurekbc483de2020-01-22 03:40:00 -0500671 ctx->queue_input = queue_input;
672 ctx->queue_output = queue_output;
673 ctx->socket = socket;
Gilles Peskine449bd832023-01-11 14:50:10 +0100674 mbedtls_mock_socket_init(socket);
Andrzej Kurekbc483de2020-01-22 03:40:00 -0500675
676 return 0;
677}
678
679/*
680 * Close a given message socket context, along with the socket itself. Free the
681 * memory allocated by the input queue.
682 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100683void mbedtls_message_socket_close(mbedtls_test_message_socket_context *ctx)
Andrzej Kurekbc483de2020-01-22 03:40:00 -0500684{
Gilles Peskine449bd832023-01-11 14:50:10 +0100685 if (ctx == NULL) {
Andrzej Kurekbc483de2020-01-22 03:40:00 -0500686 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100687 }
Andrzej Kurekbc483de2020-01-22 03:40:00 -0500688
Gilles Peskine449bd832023-01-11 14:50:10 +0100689 mbedtls_test_message_queue_free(ctx->queue_input);
690 mbedtls_mock_socket_close(ctx->socket);
691 memset(ctx, 0, sizeof(*ctx));
Andrzej Kurekbc483de2020-01-22 03:40:00 -0500692}
693
694/*
695 * Send one message through a given message socket context.
696 *
697 * \retval \p len, if everything succeeds.
698 * \retval MBEDTLS_TEST_ERROR_CONTEXT_ERROR, if any of the needed context
699 * elements or the context itself is null.
700 * \retval MBEDTLS_TEST_ERROR_SEND_FAILED if mbedtls_mock_tcp_send_b failed.
Andrzej Kurekf46b9122020-02-07 08:19:00 -0500701 * \retval MBEDTLS_ERR_SSL_WANT_WRITE, if the output queue is full.
Andrzej Kurekbc483de2020-01-22 03:40:00 -0500702 *
703 * This function will also return any error from
704 * mbedtls_test_message_queue_push_info.
705 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100706int mbedtls_mock_tcp_send_msg(void *ctx, const unsigned char *buf, size_t len)
Andrzej Kurekbc483de2020-01-22 03:40:00 -0500707{
Gilles Peskine449bd832023-01-11 14:50:10 +0100708 mbedtls_test_message_queue *queue;
709 mbedtls_mock_socket *socket;
710 mbedtls_test_message_socket_context *context = (mbedtls_test_message_socket_context *) ctx;
Andrzej Kurekbc483de2020-01-22 03:40:00 -0500711
Gilles Peskine449bd832023-01-11 14:50:10 +0100712 if (context == NULL || context->socket == NULL
713 || context->queue_output == NULL) {
Andrzej Kurekbc483de2020-01-22 03:40:00 -0500714 return MBEDTLS_TEST_ERROR_CONTEXT_ERROR;
715 }
716
717 queue = context->queue_output;
718 socket = context->socket;
719
Gilles Peskine449bd832023-01-11 14:50:10 +0100720 if (queue->num >= queue->capacity) {
Andrzej Kurekf46b9122020-02-07 08:19:00 -0500721 return MBEDTLS_ERR_SSL_WANT_WRITE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100722 }
Andrzej Kurekbc483de2020-01-22 03:40:00 -0500723
Gilles Peskine449bd832023-01-11 14:50:10 +0100724 if (mbedtls_mock_tcp_send_b(socket, buf, len) != (int) len) {
Andrzej Kurekbc483de2020-01-22 03:40:00 -0500725 return MBEDTLS_TEST_ERROR_SEND_FAILED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100726 }
Andrzej Kurekbc483de2020-01-22 03:40:00 -0500727
Gilles Peskine449bd832023-01-11 14:50:10 +0100728 return mbedtls_test_message_queue_push_info(queue, len);
Andrzej Kurekbc483de2020-01-22 03:40:00 -0500729}
730
731/*
732 * Receive one message from a given message socket context and return message
733 * length or an error.
734 *
735 * \retval message length, if everything succeeds.
736 * \retval MBEDTLS_TEST_ERROR_CONTEXT_ERROR, if any of the needed context
737 * elements or the context itself is null.
738 * \retval MBEDTLS_TEST_ERROR_RECV_FAILED if mbedtls_mock_tcp_recv_b failed.
739 *
740 * This function will also return any error other than
741 * MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED from mbedtls_test_message_queue_peek_info.
742 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100743int mbedtls_mock_tcp_recv_msg(void *ctx, unsigned char *buf, size_t buf_len)
Andrzej Kurekbc483de2020-01-22 03:40:00 -0500744{
Gilles Peskine449bd832023-01-11 14:50:10 +0100745 mbedtls_test_message_queue *queue;
746 mbedtls_mock_socket *socket;
747 mbedtls_test_message_socket_context *context = (mbedtls_test_message_socket_context *) ctx;
Gilles Peskine19e841e2020-03-09 20:43:51 +0100748 size_t drop_len = 0;
Andrzej Kurekbc483de2020-01-22 03:40:00 -0500749 size_t msg_len;
750 int ret;
751
Gilles Peskine449bd832023-01-11 14:50:10 +0100752 if (context == NULL || context->socket == NULL
753 || context->queue_input == NULL) {
Andrzej Kurekbc483de2020-01-22 03:40:00 -0500754 return MBEDTLS_TEST_ERROR_CONTEXT_ERROR;
755 }
756
757 queue = context->queue_input;
758 socket = context->socket;
759
760 /* Peek first, so that in case of a socket error the data remains in
761 * the queue. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100762 ret = mbedtls_test_message_queue_peek_info(queue, buf_len, &msg_len);
763 if (ret == MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED) {
Andrzej Kurekbc483de2020-01-22 03:40:00 -0500764 /* Calculate how much to drop */
765 drop_len = msg_len - buf_len;
766
767 /* Set the requested message len to be buffer length */
768 msg_len = buf_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100769 } else if (ret != 0) {
Andrzej Kurekbc483de2020-01-22 03:40:00 -0500770 return ret;
771 }
772
Gilles Peskine449bd832023-01-11 14:50:10 +0100773 if (mbedtls_mock_tcp_recv_b(socket, buf, msg_len) != (int) msg_len) {
Andrzej Kurekbc483de2020-01-22 03:40:00 -0500774 return MBEDTLS_TEST_ERROR_RECV_FAILED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100775 }
Andrzej Kurekbc483de2020-01-22 03:40:00 -0500776
Gilles Peskine449bd832023-01-11 14:50:10 +0100777 if (ret == MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED) {
Andrzej Kurekbc483de2020-01-22 03:40:00 -0500778 /* Drop the remaining part of the message */
Gilles Peskine449bd832023-01-11 14:50:10 +0100779 if (mbedtls_mock_tcp_recv_b(socket, NULL, drop_len) != (int) drop_len) {
780 /* Inconsistent state - part of the message was read,
781 * and a part couldn't. Not much we can do here, but it should not
782 * happen in test environment, unless forced manually. */
Andrzej Kurekbc483de2020-01-22 03:40:00 -0500783 }
784 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100785 mbedtls_test_message_queue_pop_info(queue, buf_len);
Andrzej Kurekbc483de2020-01-22 03:40:00 -0500786
787 return msg_len;
788}
789
Ronald Crone68ab4f2022-10-05 12:46:29 +0200790#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100791
792/*
793 * Structure with endpoint's certificates for SSL communication tests.
794 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100795typedef struct mbedtls_endpoint_certificate {
796 mbedtls_x509_crt *ca_cert;
797 mbedtls_x509_crt *cert;
798 mbedtls_pk_context *pkey;
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100799} mbedtls_endpoint_certificate;
800
801/*
802 * Endpoint structure for SSL communication tests.
803 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100804typedef struct mbedtls_endpoint {
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100805 const char *name;
806 mbedtls_ssl_context ssl;
807 mbedtls_ssl_config conf;
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100808 mbedtls_mock_socket socket;
809 mbedtls_endpoint_certificate cert;
810} mbedtls_endpoint;
811
812/*
Andrzej Kurek0abebeb2022-09-30 12:54:41 -0400813 * Deinitializes certificates from endpoint represented by \p ep.
814 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100815void mbedtls_endpoint_certificate_free(mbedtls_endpoint *ep)
Andrzej Kurek0abebeb2022-09-30 12:54:41 -0400816{
Gilles Peskine449bd832023-01-11 14:50:10 +0100817 mbedtls_endpoint_certificate *cert = &(ep->cert);
818 if (cert != NULL) {
819 if (cert->ca_cert != NULL) {
820 mbedtls_x509_crt_free(cert->ca_cert);
821 mbedtls_free(cert->ca_cert);
Andrzej Kurek0abebeb2022-09-30 12:54:41 -0400822 cert->ca_cert = NULL;
823 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100824 if (cert->cert != NULL) {
825 mbedtls_x509_crt_free(cert->cert);
826 mbedtls_free(cert->cert);
Andrzej Kurek0abebeb2022-09-30 12:54:41 -0400827 cert->cert = NULL;
828 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100829 if (cert->pkey != NULL) {
Andrzej Kurek0abebeb2022-09-30 12:54:41 -0400830#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100831 if (mbedtls_pk_get_type(cert->pkey) == MBEDTLS_PK_OPAQUE) {
Andrzej Kurek0abebeb2022-09-30 12:54:41 -0400832 mbedtls_svc_key_id_t *key_slot = cert->pkey->pk_ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100833 psa_destroy_key(*key_slot);
Andrzej Kurek0abebeb2022-09-30 12:54:41 -0400834 }
835#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100836 mbedtls_pk_free(cert->pkey);
837 mbedtls_free(cert->pkey);
Andrzej Kurek0abebeb2022-09-30 12:54:41 -0400838 cert->pkey = NULL;
839 }
840 }
841}
842
843/*
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100844 * Initializes \p ep_cert structure and assigns it to endpoint
845 * represented by \p ep.
846 *
847 * \retval 0 on success, otherwise error code.
848 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100849int mbedtls_endpoint_certificate_init(mbedtls_endpoint *ep, int pk_alg,
850 int opaque_alg, int opaque_alg2,
851 int opaque_usage)
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100852{
853 int i = 0;
854 int ret = -1;
Andrzej Kurek0abebeb2022-09-30 12:54:41 -0400855 mbedtls_endpoint_certificate *cert = NULL;
Neil Armstrong8c52ed82022-05-27 13:14:55 +0200856#if defined(MBEDTLS_USE_PSA_CRYPTO)
857 mbedtls_svc_key_id_t key_slot = MBEDTLS_SVC_KEY_ID_INIT;
858#endif
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100859
Gilles Peskine449bd832023-01-11 14:50:10 +0100860 if (ep == NULL) {
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100861 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
862 }
863
Gilles Peskine449bd832023-01-11 14:50:10 +0100864 cert = &(ep->cert);
865 ASSERT_ALLOC(cert->ca_cert, 1);
866 ASSERT_ALLOC(cert->cert, 1);
867 ASSERT_ALLOC(cert->pkey, 1);
Andrzej Kurek0abebeb2022-09-30 12:54:41 -0400868
Gilles Peskine449bd832023-01-11 14:50:10 +0100869 mbedtls_x509_crt_init(cert->ca_cert);
870 mbedtls_x509_crt_init(cert->cert);
871 mbedtls_pk_init(cert->pkey);
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100872
873 /* Load the trusted CA */
874
Gilles Peskine449bd832023-01-11 14:50:10 +0100875 for (i = 0; mbedtls_test_cas_der[i] != NULL; i++) {
876 ret = mbedtls_x509_crt_parse_der(cert->ca_cert,
877 (const unsigned char *) mbedtls_test_cas_der[i],
878 mbedtls_test_cas_der_len[i]);
879 TEST_ASSERT(ret == 0);
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100880 }
881
882 /* Load own certificate and private key */
883
Gilles Peskine449bd832023-01-11 14:50:10 +0100884 if (ep->conf.endpoint == MBEDTLS_SSL_IS_SERVER) {
885 if (pk_alg == MBEDTLS_PK_RSA) {
886 ret = mbedtls_x509_crt_parse(cert->cert,
887 (const unsigned char *) mbedtls_test_srv_crt_rsa_sha256_der,
888 mbedtls_test_srv_crt_rsa_sha256_der_len);
889 TEST_ASSERT(ret == 0);
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100890
Gilles Peskine449bd832023-01-11 14:50:10 +0100891 ret = mbedtls_pk_parse_key(cert->pkey,
892 (const unsigned char *) mbedtls_test_srv_key_rsa_der,
893 mbedtls_test_srv_key_rsa_der_len, NULL, 0,
894 mbedtls_test_rnd_std_rand, NULL);
895 TEST_ASSERT(ret == 0);
896 } else {
897 ret = mbedtls_x509_crt_parse(cert->cert,
898 (const unsigned char *) mbedtls_test_srv_crt_ec_der,
899 mbedtls_test_srv_crt_ec_der_len);
900 TEST_ASSERT(ret == 0);
901
902 ret = mbedtls_pk_parse_key(cert->pkey,
903 (const unsigned char *) mbedtls_test_srv_key_ec_der,
904 mbedtls_test_srv_key_ec_der_len, NULL, 0,
905 mbedtls_test_rnd_std_rand, NULL);
906 TEST_ASSERT(ret == 0);
Andrzej Kurekb2980742020-02-02 19:25:26 -0500907 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100908 } else {
909 if (pk_alg == MBEDTLS_PK_RSA) {
910 ret = mbedtls_x509_crt_parse(cert->cert,
911 (const unsigned char *) mbedtls_test_cli_crt_rsa_der,
912 mbedtls_test_cli_crt_rsa_der_len);
913 TEST_ASSERT(ret == 0);
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100914
Gilles Peskine449bd832023-01-11 14:50:10 +0100915 ret = mbedtls_pk_parse_key(cert->pkey,
916 (const unsigned char *) mbedtls_test_cli_key_rsa_der,
917 mbedtls_test_cli_key_rsa_der_len, NULL, 0,
918 mbedtls_test_rnd_std_rand, NULL);
919 TEST_ASSERT(ret == 0);
920 } else {
921 ret = mbedtls_x509_crt_parse(cert->cert,
922 (const unsigned char *) mbedtls_test_cli_crt_ec_der,
923 mbedtls_test_cli_crt_ec_len);
924 TEST_ASSERT(ret == 0);
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100925
Gilles Peskine449bd832023-01-11 14:50:10 +0100926 ret = mbedtls_pk_parse_key(cert->pkey,
927 (const unsigned char *) mbedtls_test_cli_key_ec_der,
928 mbedtls_test_cli_key_ec_der_len, NULL, 0,
929 mbedtls_test_rnd_std_rand, NULL);
930 TEST_ASSERT(ret == 0);
Andrzej Kurekb2980742020-02-02 19:25:26 -0500931 }
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100932 }
933
Neil Armstrong8c52ed82022-05-27 13:14:55 +0200934#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100935 if (opaque_alg != 0) {
936 TEST_EQUAL(mbedtls_pk_wrap_as_opaque(cert->pkey, &key_slot,
937 opaque_alg, opaque_usage,
938 opaque_alg2), 0);
Neil Armstrong8c52ed82022-05-27 13:14:55 +0200939 }
940#else
941 (void) opaque_alg;
942 (void) opaque_alg2;
943 (void) opaque_usage;
944#endif
945
Gilles Peskine449bd832023-01-11 14:50:10 +0100946 mbedtls_ssl_conf_ca_chain(&(ep->conf), cert->ca_cert, NULL);
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100947
Gilles Peskine449bd832023-01-11 14:50:10 +0100948 ret = mbedtls_ssl_conf_own_cert(&(ep->conf), cert->cert,
949 cert->pkey);
950 TEST_ASSERT(ret == 0);
951 TEST_ASSERT(ep->conf.key_cert != NULL);
Glenn Strauss36872db2022-01-22 05:06:31 -0500952
Gilles Peskine449bd832023-01-11 14:50:10 +0100953 ret = mbedtls_ssl_conf_own_cert(&(ep->conf), NULL, NULL);
954 TEST_ASSERT(ret == 0);
955 TEST_ASSERT(ep->conf.key_cert == NULL);
Glenn Strauss36872db2022-01-22 05:06:31 -0500956
Gilles Peskine449bd832023-01-11 14:50:10 +0100957 ret = mbedtls_ssl_conf_own_cert(&(ep->conf), cert->cert,
958 cert->pkey);
959 TEST_ASSERT(ret == 0);
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100960
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100961exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100962 if (ret != 0) {
963 mbedtls_endpoint_certificate_free(ep);
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100964 }
965
966 return ret;
967}
968
969/*
970 * Initializes \p ep structure. It is important to call `mbedtls_endpoint_free()`
971 * after calling this function even if it fails.
972 *
973 * \p endpoint_type must be set as MBEDTLS_SSL_IS_SERVER or
974 * MBEDTLS_SSL_IS_CLIENT.
Andrzej Kurek15daf502020-02-12 09:17:52 -0500975 * \p pk_alg the algorithm to use, currently only MBEDTLS_PK_RSA and
976 * MBEDTLS_PK_ECDSA are supported.
977 * \p dtls_context - in case of DTLS - this is the context handling metadata.
978 * \p input_queue - used only in case of DTLS.
979 * \p output_queue - used only in case of DTLS.
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100980 *
981 * \retval 0 on success, otherwise error code.
982 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100983int mbedtls_endpoint_init(mbedtls_endpoint *ep, int endpoint_type,
984 handshake_test_options *options,
985 mbedtls_test_message_socket_context *dtls_context,
986 mbedtls_test_message_queue *input_queue,
987 mbedtls_test_message_queue *output_queue,
988 uint16_t *group_list)
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100989{
990 int ret = -1;
Gilles Peskine80dae042022-01-21 23:50:39 +0100991 uintptr_t user_data_n;
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100992
Gilles Peskine449bd832023-01-11 14:50:10 +0100993 if (dtls_context != NULL && (input_queue == NULL || output_queue == NULL)) {
Piotr Nowicki2a1f1782020-01-13 09:42:10 +0100994 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Andrzej Kurek15daf502020-02-12 09:17:52 -0500995 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100996
997 if (ep == NULL) {
998 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
999 }
1000
1001 memset(ep, 0, sizeof(*ep));
1002
1003 ep->name = (endpoint_type == MBEDTLS_SSL_IS_SERVER) ? "Server" : "Client";
1004
1005 mbedtls_ssl_init(&(ep->ssl));
1006 mbedtls_ssl_config_init(&(ep->conf));
1007 mbedtls_ssl_conf_rng(&(ep->conf), rng_get, NULL);
1008
1009 TEST_ASSERT(mbedtls_ssl_conf_get_user_data_p(&ep->conf) == NULL);
1010 TEST_EQUAL(mbedtls_ssl_conf_get_user_data_n(&ep->conf), 0);
1011 TEST_ASSERT(mbedtls_ssl_get_user_data_p(&ep->ssl) == NULL);
1012 TEST_EQUAL(mbedtls_ssl_get_user_data_n(&ep->ssl), 0);
1013
1014 (void) mbedtls_test_rnd_std_rand(NULL,
1015 (void *) &user_data_n,
1016 sizeof(user_data_n));
1017 mbedtls_ssl_conf_set_user_data_n(&ep->conf, user_data_n);
1018 mbedtls_ssl_set_user_data_n(&ep->ssl, user_data_n);
1019
1020 if (dtls_context != NULL) {
1021 TEST_ASSERT(mbedtls_message_socket_setup(input_queue, output_queue,
1022 100, &(ep->socket),
1023 dtls_context) == 0);
1024 } else {
1025 mbedtls_mock_socket_init(&(ep->socket));
Andrzej Kurek15daf502020-02-12 09:17:52 -05001026 }
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01001027
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01001028 /* Non-blocking callbacks without timeout */
Gilles Peskine449bd832023-01-11 14:50:10 +01001029 if (dtls_context != NULL) {
1030 mbedtls_ssl_set_bio(&(ep->ssl), dtls_context,
1031 mbedtls_mock_tcp_send_msg,
1032 mbedtls_mock_tcp_recv_msg,
1033 NULL);
1034 } else {
1035 mbedtls_ssl_set_bio(&(ep->ssl), &(ep->socket),
1036 mbedtls_mock_tcp_send_nb,
1037 mbedtls_mock_tcp_recv_nb,
1038 NULL);
Andrzej Kurek15daf502020-02-12 09:17:52 -05001039 }
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01001040
Gilles Peskine449bd832023-01-11 14:50:10 +01001041 ret = mbedtls_ssl_config_defaults(&(ep->conf), endpoint_type,
1042 (dtls_context != NULL) ?
1043 MBEDTLS_SSL_TRANSPORT_DATAGRAM :
1044 MBEDTLS_SSL_TRANSPORT_STREAM,
1045 MBEDTLS_SSL_PRESET_DEFAULT);
1046 TEST_ASSERT(ret == 0);
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01001047
Gilles Peskine449bd832023-01-11 14:50:10 +01001048 if (group_list != NULL) {
1049 mbedtls_ssl_conf_groups(&(ep->conf), group_list);
1050 }
Andrzej Kurek74394a52022-03-08 06:50:12 -05001051
Gilles Peskine449bd832023-01-11 14:50:10 +01001052 mbedtls_ssl_conf_authmode(&(ep->conf), MBEDTLS_SSL_VERIFY_REQUIRED);
Ronald Cronbdddaef2022-06-07 10:34:59 +02001053
Andrzej Kureked58b502022-06-10 19:24:05 -04001054#if defined(MBEDTLS_SSL_CACHE_C) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001055 if (endpoint_type == MBEDTLS_SSL_IS_SERVER && options->cache != NULL) {
1056 mbedtls_ssl_conf_session_cache(&(ep->conf), options->cache,
1057 mbedtls_ssl_cache_get,
1058 mbedtls_ssl_cache_set);
Andrzej Kurek780dc182022-06-10 08:57:19 -04001059 }
1060#endif
1061
Gilles Peskine449bd832023-01-11 14:50:10 +01001062 ret = mbedtls_ssl_setup(&(ep->ssl), &(ep->conf));
1063 TEST_ASSERT(ret == 0);
Andrzej Kurek15daf502020-02-12 09:17:52 -05001064
1065#if defined(MBEDTLS_SSL_PROTO_DTLS) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001066 if (endpoint_type == MBEDTLS_SSL_IS_SERVER && dtls_context != NULL) {
1067 mbedtls_ssl_conf_dtls_cookies(&(ep->conf), NULL, NULL, NULL);
1068 }
Andrzej Kurek15daf502020-02-12 09:17:52 -05001069#endif
1070
Gilles Peskine449bd832023-01-11 14:50:10 +01001071 ret = mbedtls_endpoint_certificate_init(ep, options->pk_alg,
1072 options->opaque_alg,
1073 options->opaque_alg2,
1074 options->opaque_usage);
1075 TEST_ASSERT(ret == 0);
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01001076
Gilles Peskine449bd832023-01-11 14:50:10 +01001077 TEST_EQUAL(mbedtls_ssl_conf_get_user_data_n(&ep->conf), user_data_n);
1078 mbedtls_ssl_conf_set_user_data_p(&ep->conf, ep);
1079 TEST_EQUAL(mbedtls_ssl_get_user_data_n(&ep->ssl), user_data_n);
1080 mbedtls_ssl_set_user_data_p(&ep->ssl, ep);
Gilles Peskine80dae042022-01-21 23:50:39 +01001081
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01001082exit:
1083 return ret;
1084}
1085
1086/*
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01001087 * Deinitializes endpoint represented by \p ep.
1088 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001089void mbedtls_endpoint_free(mbedtls_endpoint *ep,
1090 mbedtls_test_message_socket_context *context)
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01001091{
Gilles Peskine449bd832023-01-11 14:50:10 +01001092 mbedtls_endpoint_certificate_free(ep);
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01001093
Gilles Peskine449bd832023-01-11 14:50:10 +01001094 mbedtls_ssl_free(&(ep->ssl));
1095 mbedtls_ssl_config_free(&(ep->conf));
Andrzej Kurek15daf502020-02-12 09:17:52 -05001096
Gilles Peskine449bd832023-01-11 14:50:10 +01001097 if (context != NULL) {
1098 mbedtls_message_socket_close(context);
1099 } else {
1100 mbedtls_mock_socket_close(&(ep->socket));
Andrzej Kurek15daf502020-02-12 09:17:52 -05001101 }
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01001102}
1103
1104/*
1105 * This function moves ssl handshake from \p ssl to prescribed \p state.
1106 * /p second_ssl is used as second endpoint and their sockets have to be
1107 * connected before calling this function.
1108 *
1109 * \retval 0 on success, otherwise error code.
1110 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001111int mbedtls_move_handshake_to_state(mbedtls_ssl_context *ssl,
1112 mbedtls_ssl_context *second_ssl,
1113 int state)
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01001114{
1115 enum { BUFFSIZE = 1024 };
1116 int max_steps = 1000;
1117 int ret = 0;
1118
Gilles Peskine449bd832023-01-11 14:50:10 +01001119 if (ssl == NULL || second_ssl == NULL) {
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01001120 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
1121 }
1122
1123 /* Perform communication via connected sockets */
Gilles Peskine449bd832023-01-11 14:50:10 +01001124 while ((ssl->state != state) && (--max_steps >= 0)) {
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01001125 /* If /p second_ssl ends the handshake procedure before /p ssl then
1126 * there is no need to call the next step */
Gilles Peskine449bd832023-01-11 14:50:10 +01001127 if (!mbedtls_ssl_is_handshake_over(second_ssl)) {
1128 ret = mbedtls_ssl_handshake_step(second_ssl);
1129 if (ret != 0 && ret != MBEDTLS_ERR_SSL_WANT_READ &&
1130 ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01001131 return ret;
1132 }
1133 }
1134
1135 /* We only care about the \p ssl state and returns, so we call it last,
1136 * to leave the iteration as soon as the state is as expected. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 ret = mbedtls_ssl_handshake_step(ssl);
1138 if (ret != 0 && ret != MBEDTLS_ERR_SSL_WANT_READ &&
1139 ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01001140 return ret;
1141 }
1142 }
1143
Gilles Peskine449bd832023-01-11 14:50:10 +01001144 return (max_steps >= 0) ? ret : -1;
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01001145}
1146
Ronald Crone68ab4f2022-10-05 12:46:29 +02001147#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01001148
Janos Follath3766ba52019-11-27 13:31:42 +00001149/*
Piotr Nowicki438bf3b2020-03-10 12:59:10 +01001150 * Write application data. Increase write counter if necessary.
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001151 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001152int mbedtls_ssl_write_fragment(mbedtls_ssl_context *ssl, unsigned char *buf,
1153 int buf_len, int *written,
1154 const int expected_fragments)
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001155{
Gilles Peskine449bd832023-01-11 14:50:10 +01001156 int ret = mbedtls_ssl_write(ssl, buf + *written, buf_len - *written);
1157 if (ret > 0) {
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001158 *written += ret;
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001159 }
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001160
Gilles Peskine449bd832023-01-11 14:50:10 +01001161 if (expected_fragments == 0) {
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001162 /* Used for DTLS and the message size larger than MFL. In that case
1163 * the message can not be fragmented and the library should return
1164 * MBEDTLS_ERR_SSL_BAD_INPUT_DATA error. This error must be returned
1165 * to prevent a dead loop inside mbedtls_exchange_data(). */
1166 return ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001167 } else if (expected_fragments == 1) {
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001168 /* Used for TLS/DTLS and the message size lower than MFL */
Gilles Peskine449bd832023-01-11 14:50:10 +01001169 TEST_ASSERT(ret == buf_len ||
1170 ret == MBEDTLS_ERR_SSL_WANT_READ ||
1171 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
1172 } else {
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001173 /* Used for TLS and the message size larger than MFL */
Gilles Peskine449bd832023-01-11 14:50:10 +01001174 TEST_ASSERT(expected_fragments > 1);
1175 TEST_ASSERT((ret >= 0 && ret <= buf_len) ||
1176 ret == MBEDTLS_ERR_SSL_WANT_READ ||
1177 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001178 }
1179
1180 return 0;
1181
1182exit:
1183 /* Some of the tests failed */
1184 return -1;
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001185}
1186
1187/*
Piotr Nowicki438bf3b2020-03-10 12:59:10 +01001188 * Read application data and increase read counter and fragments counter if necessary.
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001189 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001190int mbedtls_ssl_read_fragment(mbedtls_ssl_context *ssl, unsigned char *buf,
1191 int buf_len, int *read,
1192 int *fragments, const int expected_fragments)
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001193{
Gilles Peskine449bd832023-01-11 14:50:10 +01001194 int ret = mbedtls_ssl_read(ssl, buf + *read, buf_len - *read);
1195 if (ret > 0) {
1196 (*fragments)++;
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001197 *read += ret;
1198 }
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001199
Gilles Peskine449bd832023-01-11 14:50:10 +01001200 if (expected_fragments == 0) {
1201 TEST_ASSERT(ret == 0);
1202 } else if (expected_fragments == 1) {
1203 TEST_ASSERT(ret == buf_len ||
1204 ret == MBEDTLS_ERR_SSL_WANT_READ ||
1205 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
1206 } else {
1207 TEST_ASSERT(expected_fragments > 1);
1208 TEST_ASSERT((ret >= 0 && ret <= buf_len) ||
1209 ret == MBEDTLS_ERR_SSL_WANT_READ ||
1210 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001211 }
1212
1213 return 0;
1214
1215exit:
1216 /* Some of the tests failed */
1217 return -1;
Piotr Nowickic3fca5e2020-01-30 15:33:42 +01001218}
1219
1220/*
Hanno Beckera18d1322018-01-03 14:27:32 +00001221 * Helper function setting up inverse record transformations
1222 * using given cipher, hash, EtM mode, authentication tag length,
1223 * and version.
1224 */
1225
Gilles Peskine449bd832023-01-11 14:50:10 +01001226#define CHK(x) \
Hanno Beckera18d1322018-01-03 14:27:32 +00001227 do \
1228 { \
Gilles Peskine449bd832023-01-11 14:50:10 +01001229 if (!(x)) \
Hanno Becker81e16a32019-03-01 11:21:44 +00001230 { \
Hanno Beckera5780f12019-04-05 09:55:37 +01001231 ret = -1; \
Hanno Becker81e16a32019-03-01 11:21:44 +00001232 goto cleanup; \
1233 } \
Gilles Peskine449bd832023-01-11 14:50:10 +01001234 } while (0)
Hanno Beckera18d1322018-01-03 14:27:32 +00001235
Gilles Peskine449bd832023-01-11 14:50:10 +01001236void set_ciphersuite(mbedtls_ssl_config *conf, const char *cipher,
1237 int *forced_ciphersuite)
Andrzej Kurekf40daa32020-02-04 09:00:01 -05001238{
1239 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Gilles Peskine449bd832023-01-11 14:50:10 +01001240 forced_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id(cipher);
Andrzej Kurekf40daa32020-02-04 09:00:01 -05001241 forced_ciphersuite[1] = 0;
1242
1243 ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01001244 mbedtls_ssl_ciphersuite_from_id(forced_ciphersuite[0]);
Andrzej Kurekf40daa32020-02-04 09:00:01 -05001245
Gilles Peskine449bd832023-01-11 14:50:10 +01001246 TEST_ASSERT(ciphersuite_info != NULL);
1247 TEST_ASSERT(ciphersuite_info->min_tls_version <= conf->max_tls_version);
1248 TEST_ASSERT(ciphersuite_info->max_tls_version >= conf->min_tls_version);
Andrzej Kurekf40daa32020-02-04 09:00:01 -05001249
Gilles Peskine449bd832023-01-11 14:50:10 +01001250 if (conf->max_tls_version > ciphersuite_info->max_tls_version) {
Glenn Strauss60bfe602022-03-14 19:04:24 -04001251 conf->max_tls_version = ciphersuite_info->max_tls_version;
Andrzej Kurekf40daa32020-02-04 09:00:01 -05001252 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001253 if (conf->min_tls_version < ciphersuite_info->min_tls_version) {
Glenn Strauss60bfe602022-03-14 19:04:24 -04001254 conf->min_tls_version = ciphersuite_info->min_tls_version;
Andrzej Kurekf40daa32020-02-04 09:00:01 -05001255 }
1256
Gilles Peskine449bd832023-01-11 14:50:10 +01001257 mbedtls_ssl_conf_ciphersuites(conf, forced_ciphersuite);
Andrzej Kurekf40daa32020-02-04 09:00:01 -05001258
1259exit:
1260 return;
1261}
1262
Gilles Peskine449bd832023-01-11 14:50:10 +01001263int psk_dummy_callback(void *p_info, mbedtls_ssl_context *ssl,
1264 const unsigned char *name, size_t name_len)
Andrzej Kurekcc5169c2020-02-04 09:04:56 -05001265{
1266 (void) p_info;
1267 (void) ssl;
1268 (void) name;
1269 (void) name_len;
1270
Gilles Peskine449bd832023-01-11 14:50:10 +01001271 return 0;
Andrzej Kurekcc5169c2020-02-04 09:04:56 -05001272}
1273
Hanno Beckerd856c822019-04-29 17:30:59 +01001274#if MBEDTLS_SSL_CID_OUT_LEN_MAX > MBEDTLS_SSL_CID_IN_LEN_MAX
1275#define SSL_CID_LEN_MIN MBEDTLS_SSL_CID_IN_LEN_MAX
1276#else
1277#define SSL_CID_LEN_MIN MBEDTLS_SSL_CID_OUT_LEN_MAX
1278#endif
Hanno Beckera18d1322018-01-03 14:27:32 +00001279
Gilles Peskine9c656ec2022-02-26 19:55:58 +01001280#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
1281 defined(MBEDTLS_CIPHER_MODE_CBC) && defined(MBEDTLS_AES_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001282static int psa_cipher_encrypt_helper(mbedtls_ssl_transform *transform,
1283 const unsigned char *iv, size_t iv_len,
1284 const unsigned char *input, size_t ilen,
1285 unsigned char *output, size_t *olen)
Przemyslaw Stekielf4facef2022-02-02 21:31:04 +01001286{
Przemyslaw Stekiel5648d572022-02-03 14:09:02 +01001287#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemyslaw Stekield66387f2022-02-03 08:55:33 +01001288 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekielf4facef2022-02-02 21:31:04 +01001289 psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT;
1290 size_t part_len;
1291
Gilles Peskine449bd832023-01-11 14:50:10 +01001292 status = psa_cipher_encrypt_setup(&cipher_op,
1293 transform->psa_key_enc, transform->psa_alg);
Przemyslaw Stekielf4facef2022-02-02 21:31:04 +01001294
Gilles Peskine449bd832023-01-11 14:50:10 +01001295 if (status != PSA_SUCCESS) {
1296 return psa_ssl_status_to_mbedtls(status);
1297 }
Przemyslaw Stekielf4facef2022-02-02 21:31:04 +01001298
Gilles Peskine449bd832023-01-11 14:50:10 +01001299 status = psa_cipher_set_iv(&cipher_op, iv, iv_len);
Przemyslaw Stekielf4facef2022-02-02 21:31:04 +01001300
Gilles Peskine449bd832023-01-11 14:50:10 +01001301 if (status != PSA_SUCCESS) {
1302 return psa_ssl_status_to_mbedtls(status);
1303 }
Przemyslaw Stekielf4facef2022-02-02 21:31:04 +01001304
Gilles Peskine449bd832023-01-11 14:50:10 +01001305 status = psa_cipher_update(&cipher_op,
1306 input, ilen, output, ilen, olen);
Przemyslaw Stekielf4facef2022-02-02 21:31:04 +01001307
Gilles Peskine449bd832023-01-11 14:50:10 +01001308 if (status != PSA_SUCCESS) {
1309 return psa_ssl_status_to_mbedtls(status);
1310 }
Przemyslaw Stekielf4facef2022-02-02 21:31:04 +01001311
Gilles Peskine449bd832023-01-11 14:50:10 +01001312 status = psa_cipher_finish(&cipher_op,
1313 output + *olen, ilen - *olen, &part_len);
Przemyslaw Stekielf4facef2022-02-02 21:31:04 +01001314
Gilles Peskine449bd832023-01-11 14:50:10 +01001315 if (status != PSA_SUCCESS) {
1316 return psa_ssl_status_to_mbedtls(status);
1317 }
Przemyslaw Stekielf4facef2022-02-02 21:31:04 +01001318
1319 *olen += part_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001320 return 0;
Przemyslaw Stekiel5648d572022-02-03 14:09:02 +01001321#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001322 return mbedtls_cipher_crypt(&transform->cipher_ctx_enc,
1323 iv, iv_len, input, ilen, output, olen);
Przemyslaw Stekielf4facef2022-02-02 21:31:04 +01001324#endif /* MBEDTLS_USE_PSA_CRYPTO */
Przemyslaw Stekiel5648d572022-02-03 14:09:02 +01001325}
Gilles Peskine9c656ec2022-02-26 19:55:58 +01001326#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_CIPHER_MODE_CBC && MBEDTLS_AES_C */
Przemyslaw Stekielf4facef2022-02-02 21:31:04 +01001327
Gilles Peskine449bd832023-01-11 14:50:10 +01001328static int build_transforms(mbedtls_ssl_transform *t_in,
1329 mbedtls_ssl_transform *t_out,
1330 int cipher_type, int hash_id,
1331 int etm, int tag_mode,
1332 mbedtls_ssl_protocol_version tls_version,
1333 size_t cid0_len,
1334 size_t cid1_len)
Hanno Beckera18d1322018-01-03 14:27:32 +00001335{
1336 mbedtls_cipher_info_t const *cipher_info;
Hanno Beckera5780f12019-04-05 09:55:37 +01001337 int ret = 0;
Hanno Beckera18d1322018-01-03 14:27:32 +00001338
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01001339#if defined(MBEDTLS_USE_PSA_CRYPTO)
1340 psa_key_type_t key_type;
1341 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1342 psa_algorithm_t alg;
1343 size_t key_bits;
Przemyslaw Stekield66387f2022-02-03 08:55:33 +01001344 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01001345#endif
1346
Hanno Beckera18d1322018-01-03 14:27:32 +00001347 size_t keylen, maclen, ivlen;
Hanno Becker81e16a32019-03-01 11:21:44 +00001348 unsigned char *key0 = NULL, *key1 = NULL;
Paul Elliott6f1eda72020-06-11 20:22:00 +01001349 unsigned char *md0 = NULL, *md1 = NULL;
Hanno Beckera18d1322018-01-03 14:27:32 +00001350 unsigned char iv_enc[16], iv_dec[16];
1351
Hanno Beckera0e20d02019-05-15 14:03:01 +01001352#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine449bd832023-01-11 14:50:10 +01001353 unsigned char cid0[SSL_CID_LEN_MIN];
1354 unsigned char cid1[SSL_CID_LEN_MIN];
Hanno Beckerd856c822019-04-29 17:30:59 +01001355
Gilles Peskine449bd832023-01-11 14:50:10 +01001356 mbedtls_test_rnd_std_rand(NULL, cid0, sizeof(cid0));
1357 mbedtls_test_rnd_std_rand(NULL, cid1, sizeof(cid1));
Hanno Becker43c24b82019-05-01 09:45:57 +01001358#else
1359 ((void) cid0_len);
1360 ((void) cid1_len);
Hanno Beckera0e20d02019-05-15 14:03:01 +01001361#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerd856c822019-04-29 17:30:59 +01001362
Hanno Beckera18d1322018-01-03 14:27:32 +00001363 maclen = 0;
1364
1365 /* Pick cipher */
Gilles Peskine449bd832023-01-11 14:50:10 +01001366 cipher_info = mbedtls_cipher_info_from_type(cipher_type);
1367 CHK(cipher_info != NULL);
1368 CHK(cipher_info->iv_size <= 16);
1369 CHK(cipher_info->key_bitlen % 8 == 0);
Hanno Beckera18d1322018-01-03 14:27:32 +00001370
1371 /* Pick keys */
1372 keylen = cipher_info->key_bitlen / 8;
Hanno Becker78d1f702019-04-05 09:56:10 +01001373 /* Allocate `keylen + 1` bytes to ensure that we get
1374 * a non-NULL pointers from `mbedtls_calloc` even if
1375 * `keylen == 0` in the case of the NULL cipher. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001376 CHK((key0 = mbedtls_calloc(1, keylen + 1)) != NULL);
1377 CHK((key1 = mbedtls_calloc(1, keylen + 1)) != NULL);
1378 memset(key0, 0x1, keylen);
1379 memset(key1, 0x2, keylen);
Hanno Beckera18d1322018-01-03 14:27:32 +00001380
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01001381#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckera18d1322018-01-03 14:27:32 +00001382 /* Setup cipher contexts */
Gilles Peskine449bd832023-01-11 14:50:10 +01001383 CHK(mbedtls_cipher_setup(&t_in->cipher_ctx_enc, cipher_info) == 0);
1384 CHK(mbedtls_cipher_setup(&t_in->cipher_ctx_dec, cipher_info) == 0);
1385 CHK(mbedtls_cipher_setup(&t_out->cipher_ctx_enc, cipher_info) == 0);
1386 CHK(mbedtls_cipher_setup(&t_out->cipher_ctx_dec, cipher_info) == 0);
Hanno Beckera18d1322018-01-03 14:27:32 +00001387
1388#if defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine449bd832023-01-11 14:50:10 +01001389 if (cipher_info->mode == MBEDTLS_MODE_CBC) {
1390 CHK(mbedtls_cipher_set_padding_mode(&t_in->cipher_ctx_enc,
1391 MBEDTLS_PADDING_NONE) == 0);
1392 CHK(mbedtls_cipher_set_padding_mode(&t_in->cipher_ctx_dec,
1393 MBEDTLS_PADDING_NONE) == 0);
1394 CHK(mbedtls_cipher_set_padding_mode(&t_out->cipher_ctx_enc,
1395 MBEDTLS_PADDING_NONE) == 0);
1396 CHK(mbedtls_cipher_set_padding_mode(&t_out->cipher_ctx_dec,
1397 MBEDTLS_PADDING_NONE) == 0);
Hanno Beckera18d1322018-01-03 14:27:32 +00001398 }
1399#endif /* MBEDTLS_CIPHER_MODE_CBC */
1400
Gilles Peskine449bd832023-01-11 14:50:10 +01001401 CHK(mbedtls_cipher_setkey(&t_in->cipher_ctx_enc, key0,
1402 keylen << 3, MBEDTLS_ENCRYPT) == 0);
1403 CHK(mbedtls_cipher_setkey(&t_in->cipher_ctx_dec, key1,
1404 keylen << 3, MBEDTLS_DECRYPT) == 0);
1405 CHK(mbedtls_cipher_setkey(&t_out->cipher_ctx_enc, key1,
1406 keylen << 3, MBEDTLS_ENCRYPT) == 0);
1407 CHK(mbedtls_cipher_setkey(&t_out->cipher_ctx_dec, key0,
1408 keylen << 3, MBEDTLS_DECRYPT) == 0);
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01001409#endif
Hanno Beckera18d1322018-01-03 14:27:32 +00001410
1411 /* Setup MAC contexts */
Hanno Beckerfd86ca82020-11-30 08:54:23 +00001412#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01001413 if (cipher_info->mode == MBEDTLS_MODE_CBC ||
1414 cipher_info->mode == MBEDTLS_MODE_STREAM) {
Andrzej Kurek21b68702022-08-17 16:26:12 -04001415#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001416 mbedtls_md_info_t const *md_info = mbedtls_md_info_from_type(hash_id);
1417 CHK(md_info != NULL);
Andrzej Kurek21b68702022-08-17 16:26:12 -04001418#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001419 maclen = mbedtls_hash_info_get_size(hash_id);
1420 CHK(maclen != 0);
Przemek Stekielcc59c992022-08-01 11:51:22 +02001421 /* Pick hash keys */
Gilles Peskine449bd832023-01-11 14:50:10 +01001422 CHK((md0 = mbedtls_calloc(1, maclen)) != NULL);
1423 CHK((md1 = mbedtls_calloc(1, maclen)) != NULL);
1424 memset(md0, 0x5, maclen);
1425 memset(md1, 0x6, maclen);
Hanno Beckera18d1322018-01-03 14:27:32 +00001426
Neil Armstrongf4cc0622022-02-23 16:21:01 +01001427#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001428 alg = mbedtls_hash_info_psa_from_md(hash_id);
Neil Armstrongf4cc0622022-02-23 16:21:01 +01001429
Gilles Peskine449bd832023-01-11 14:50:10 +01001430 CHK(alg != 0);
Neil Armstrongf4cc0622022-02-23 16:21:01 +01001431
Gilles Peskine449bd832023-01-11 14:50:10 +01001432 t_out->psa_mac_alg = PSA_ALG_HMAC(alg);
1433 t_in->psa_mac_alg = PSA_ALG_HMAC(alg);
Neil Armstrongf4cc0622022-02-23 16:21:01 +01001434 t_in->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
1435 t_out->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
1436 t_in->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
1437 t_out->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
1438
Gilles Peskine449bd832023-01-11 14:50:10 +01001439 psa_reset_key_attributes(&attributes);
1440 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
1441 psa_set_key_algorithm(&attributes, PSA_ALG_HMAC(alg));
1442 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Neil Armstrongf4cc0622022-02-23 16:21:01 +01001443
Gilles Peskine449bd832023-01-11 14:50:10 +01001444 CHK(psa_import_key(&attributes,
1445 md0, maclen,
1446 &t_in->psa_mac_enc) == PSA_SUCCESS);
Neil Armstrongf4cc0622022-02-23 16:21:01 +01001447
Gilles Peskine449bd832023-01-11 14:50:10 +01001448 CHK(psa_import_key(&attributes,
1449 md1, maclen,
1450 &t_out->psa_mac_enc) == PSA_SUCCESS);
Neil Armstrongf4cc0622022-02-23 16:21:01 +01001451
Gilles Peskine449bd832023-01-11 14:50:10 +01001452 if (cipher_info->mode == MBEDTLS_MODE_STREAM ||
1453 etm == MBEDTLS_SSL_ETM_DISABLED) {
Neil Armstrong8f92bf32022-03-18 09:56:57 +01001454 /* mbedtls_ct_hmac() requires the key to be exportable */
Gilles Peskine449bd832023-01-11 14:50:10 +01001455 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT |
1456 PSA_KEY_USAGE_VERIFY_HASH);
1457 } else {
1458 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
1459 }
Neil Armstrongf4cc0622022-02-23 16:21:01 +01001460
Gilles Peskine449bd832023-01-11 14:50:10 +01001461 CHK(psa_import_key(&attributes,
1462 md1, maclen,
1463 &t_in->psa_mac_dec) == PSA_SUCCESS);
Neil Armstrongf4cc0622022-02-23 16:21:01 +01001464
Gilles Peskine449bd832023-01-11 14:50:10 +01001465 CHK(psa_import_key(&attributes,
1466 md0, maclen,
1467 &t_out->psa_mac_dec) == PSA_SUCCESS);
Neil Armstrongcf8841a2022-02-24 11:17:45 +01001468#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001469 CHK(mbedtls_md_setup(&t_out->md_ctx_enc, md_info, 1) == 0);
1470 CHK(mbedtls_md_setup(&t_out->md_ctx_dec, md_info, 1) == 0);
1471 CHK(mbedtls_md_setup(&t_in->md_ctx_enc, md_info, 1) == 0);
1472 CHK(mbedtls_md_setup(&t_in->md_ctx_dec, md_info, 1) == 0);
Hanno Beckera18d1322018-01-03 14:27:32 +00001473
Gilles Peskine449bd832023-01-11 14:50:10 +01001474 CHK(mbedtls_md_hmac_starts(&t_in->md_ctx_enc,
1475 md0, maclen) == 0);
1476 CHK(mbedtls_md_hmac_starts(&t_in->md_ctx_dec,
1477 md1, maclen) == 0);
1478 CHK(mbedtls_md_hmac_starts(&t_out->md_ctx_enc,
1479 md1, maclen) == 0);
1480 CHK(mbedtls_md_hmac_starts(&t_out->md_ctx_dec,
1481 md0, maclen) == 0);
Neil Armstrongcf8841a2022-02-24 11:17:45 +01001482#endif
Hanno Beckera18d1322018-01-03 14:27:32 +00001483 }
1484#else
1485 ((void) hash_id);
Hanno Beckerfd86ca82020-11-30 08:54:23 +00001486#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Hanno Beckera18d1322018-01-03 14:27:32 +00001487
1488
1489 /* Pick IV's (regardless of whether they
1490 * are being used by the transform). */
1491 ivlen = cipher_info->iv_size;
Gilles Peskine449bd832023-01-11 14:50:10 +01001492 memset(iv_enc, 0x3, sizeof(iv_enc));
1493 memset(iv_dec, 0x4, sizeof(iv_dec));
Hanno Beckera18d1322018-01-03 14:27:32 +00001494
1495 /*
1496 * Setup transforms
1497 */
1498
Jaeden Amero2de07f12019-06-05 13:32:08 +01001499#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
Hanno Beckerfd86ca82020-11-30 08:54:23 +00001500 defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Hanno Beckera18d1322018-01-03 14:27:32 +00001501 t_out->encrypt_then_mac = etm;
1502 t_in->encrypt_then_mac = etm;
1503#else
1504 ((void) etm);
1505#endif
1506
Glenn Strauss07c64162022-03-14 12:34:51 -04001507 t_out->tls_version = tls_version;
1508 t_in->tls_version = tls_version;
Hanno Beckera18d1322018-01-03 14:27:32 +00001509 t_out->ivlen = ivlen;
1510 t_in->ivlen = ivlen;
1511
Gilles Peskine449bd832023-01-11 14:50:10 +01001512 switch (cipher_info->mode) {
Hanno Beckera18d1322018-01-03 14:27:32 +00001513 case MBEDTLS_MODE_GCM:
1514 case MBEDTLS_MODE_CCM:
Ronald Cron6f135e12021-12-08 16:57:54 +01001515#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001516 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Hanno Beckere6832872020-05-28 08:29:58 +01001517 t_out->fixed_ivlen = 12;
1518 t_in->fixed_ivlen = 12;
Gilles Peskine449bd832023-01-11 14:50:10 +01001519 } else
Ronald Cron6f135e12021-12-08 16:57:54 +01001520#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckere6832872020-05-28 08:29:58 +01001521 {
1522 t_out->fixed_ivlen = 4;
1523 t_in->fixed_ivlen = 4;
1524 }
Hanno Beckera18d1322018-01-03 14:27:32 +00001525 t_out->maclen = 0;
1526 t_in->maclen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001527 switch (tag_mode) {
Hanno Beckera18d1322018-01-03 14:27:32 +00001528 case 0: /* Full tag */
1529 t_out->taglen = 16;
1530 t_in->taglen = 16;
1531 break;
1532 case 1: /* Partial tag */
1533 t_out->taglen = 8;
1534 t_in->taglen = 8;
1535 break;
1536 default:
Paul Elliottc7b53742021-02-03 13:18:33 +00001537 ret = 1;
1538 goto cleanup;
Hanno Beckera18d1322018-01-03 14:27:32 +00001539 }
1540 break;
1541
1542 case MBEDTLS_MODE_CHACHAPOLY:
1543 t_out->fixed_ivlen = 12;
1544 t_in->fixed_ivlen = 12;
1545 t_out->maclen = 0;
1546 t_in->maclen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001547 switch (tag_mode) {
Hanno Beckera18d1322018-01-03 14:27:32 +00001548 case 0: /* Full tag */
1549 t_out->taglen = 16;
1550 t_in->taglen = 16;
1551 break;
1552 case 1: /* Partial tag */
1553 t_out->taglen = 8;
1554 t_in->taglen = 8;
1555 break;
1556 default:
Paul Elliottc7b53742021-02-03 13:18:33 +00001557 ret = 1;
1558 goto cleanup;
Hanno Beckera18d1322018-01-03 14:27:32 +00001559 }
1560 break;
1561
1562 case MBEDTLS_MODE_STREAM:
1563 case MBEDTLS_MODE_CBC:
1564 t_out->fixed_ivlen = 0; /* redundant, must be 0 */
1565 t_in->fixed_ivlen = 0; /* redundant, must be 0 */
1566 t_out->taglen = 0;
1567 t_in->taglen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001568 switch (tag_mode) {
Hanno Beckera18d1322018-01-03 14:27:32 +00001569 case 0: /* Full tag */
1570 t_out->maclen = maclen;
1571 t_in->maclen = maclen;
1572 break;
Hanno Beckera18d1322018-01-03 14:27:32 +00001573 default:
Paul Elliottc7b53742021-02-03 13:18:33 +00001574 ret = 1;
1575 goto cleanup;
Hanno Beckera18d1322018-01-03 14:27:32 +00001576 }
1577 break;
1578 default:
Paul Elliottc7b53742021-02-03 13:18:33 +00001579 ret = 1;
1580 goto cleanup;
Hanno Beckera18d1322018-01-03 14:27:32 +00001581 break;
1582 }
1583
1584 /* Setup IV's */
1585
Gilles Peskine449bd832023-01-11 14:50:10 +01001586 memcpy(&t_in->iv_dec, iv_dec, sizeof(iv_dec));
1587 memcpy(&t_in->iv_enc, iv_enc, sizeof(iv_enc));
1588 memcpy(&t_out->iv_dec, iv_enc, sizeof(iv_enc));
1589 memcpy(&t_out->iv_enc, iv_dec, sizeof(iv_dec));
Hanno Beckera18d1322018-01-03 14:27:32 +00001590
Hanno Beckera0e20d02019-05-15 14:03:01 +01001591#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd856c822019-04-29 17:30:59 +01001592 /* Add CID */
Gilles Peskine449bd832023-01-11 14:50:10 +01001593 memcpy(&t_in->in_cid, cid0, cid0_len);
1594 memcpy(&t_in->out_cid, cid1, cid1_len);
Hanno Beckerd856c822019-04-29 17:30:59 +01001595 t_in->in_cid_len = cid0_len;
1596 t_in->out_cid_len = cid1_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001597 memcpy(&t_out->in_cid, cid1, cid1_len);
1598 memcpy(&t_out->out_cid, cid0, cid0_len);
Hanno Beckerd856c822019-04-29 17:30:59 +01001599 t_out->in_cid_len = cid1_len;
1600 t_out->out_cid_len = cid0_len;
Hanno Beckera0e20d02019-05-15 14:03:01 +01001601#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerd856c822019-04-29 17:30:59 +01001602
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01001603#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001604 status = mbedtls_ssl_cipher_to_psa(cipher_type,
1605 t_in->taglen,
1606 &alg,
1607 &key_type,
1608 &key_bits);
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01001609
Gilles Peskine449bd832023-01-11 14:50:10 +01001610 if (status != PSA_SUCCESS) {
1611 ret = psa_ssl_status_to_mbedtls(status);
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01001612 goto cleanup;
1613 }
1614
1615 t_in->psa_alg = alg;
1616 t_out->psa_alg = alg;
1617
Gilles Peskine449bd832023-01-11 14:50:10 +01001618 if (alg != MBEDTLS_SSL_NULL_CIPHER) {
1619 psa_reset_key_attributes(&attributes);
1620 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
1621 psa_set_key_algorithm(&attributes, alg);
1622 psa_set_key_type(&attributes, key_type);
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01001623
Gilles Peskine449bd832023-01-11 14:50:10 +01001624 status = psa_import_key(&attributes,
1625 key0,
1626 PSA_BITS_TO_BYTES(key_bits),
1627 &t_in->psa_key_enc);
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01001628
Gilles Peskine449bd832023-01-11 14:50:10 +01001629 if (status != PSA_SUCCESS) {
1630 ret = psa_ssl_status_to_mbedtls(status);
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01001631 goto cleanup;
1632 }
1633
Gilles Peskine449bd832023-01-11 14:50:10 +01001634 status = psa_import_key(&attributes,
1635 key1,
1636 PSA_BITS_TO_BYTES(key_bits),
1637 &t_out->psa_key_enc);
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01001638
Gilles Peskine449bd832023-01-11 14:50:10 +01001639 if (status != PSA_SUCCESS) {
1640 ret = psa_ssl_status_to_mbedtls(status);
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01001641 goto cleanup;
1642 }
1643
Gilles Peskine449bd832023-01-11 14:50:10 +01001644 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
Przemyslaw Stekielf4ca3f02022-01-25 00:25:59 +01001645
Gilles Peskine449bd832023-01-11 14:50:10 +01001646 status = psa_import_key(&attributes,
1647 key1,
1648 PSA_BITS_TO_BYTES(key_bits),
1649 &t_in->psa_key_dec);
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01001650
Gilles Peskine449bd832023-01-11 14:50:10 +01001651 if (status != PSA_SUCCESS) {
1652 ret = psa_ssl_status_to_mbedtls(status);
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01001653 goto cleanup;
1654 }
1655
Gilles Peskine449bd832023-01-11 14:50:10 +01001656 status = psa_import_key(&attributes,
1657 key0,
1658 PSA_BITS_TO_BYTES(key_bits),
1659 &t_out->psa_key_dec);
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01001660
Gilles Peskine449bd832023-01-11 14:50:10 +01001661 if (status != PSA_SUCCESS) {
1662 ret = psa_ssl_status_to_mbedtls(status);
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01001663 goto cleanup;
1664 }
1665 }
1666#endif /* MBEDTLS_USE_PSA_CRYPTO */
1667
Hanno Becker81e16a32019-03-01 11:21:44 +00001668cleanup:
1669
Gilles Peskine449bd832023-01-11 14:50:10 +01001670 mbedtls_free(key0);
1671 mbedtls_free(key1);
Hanno Becker81e16a32019-03-01 11:21:44 +00001672
Gilles Peskine449bd832023-01-11 14:50:10 +01001673 mbedtls_free(md0);
1674 mbedtls_free(md1);
Paul Elliott6f1eda72020-06-11 20:22:00 +01001675
Gilles Peskine449bd832023-01-11 14:50:10 +01001676 return ret;
Hanno Beckera18d1322018-01-03 14:27:32 +00001677}
1678
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001679/*
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02001680 * Populate a session structure for serialization tests.
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001681 * Choose dummy values, mostly non-0 to distinguish from the init default.
1682 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001683static int ssl_tls12_populate_session(mbedtls_ssl_session *session,
1684 int ticket_len,
1685 const char *crt_file)
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001686{
1687#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01001688 session->start = mbedtls_time(NULL) - 42;
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001689#endif
Glenn Straussda7851c2022-03-14 13:29:48 -04001690 session->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001691 session->ciphersuite = 0xabcd;
Gilles Peskine449bd832023-01-11 14:50:10 +01001692 session->id_len = sizeof(session->id);
1693 memset(session->id, 66, session->id_len);
1694 memset(session->master, 17, sizeof(session->master));
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001695
Ronald Crone68ab4f2022-10-05 12:46:29 +02001696#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) && defined(MBEDTLS_FS_IO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001697 if (crt_file != NULL && strlen(crt_file) != 0) {
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02001698 mbedtls_x509_crt tmp_crt;
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001699 int ret;
Manuel Pégourié-Gonnard6b840702019-05-24 09:40:17 +02001700
Gilles Peskine449bd832023-01-11 14:50:10 +01001701 mbedtls_x509_crt_init(&tmp_crt);
1702 ret = mbedtls_x509_crt_parse_file(&tmp_crt, crt_file);
1703 if (ret != 0) {
1704 return ret;
1705 }
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02001706
1707#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
1708 /* Move temporary CRT. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001709 session->peer_cert = mbedtls_calloc(1, sizeof(*session->peer_cert));
1710 if (session->peer_cert == NULL) {
1711 return -1;
1712 }
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02001713 *session->peer_cert = tmp_crt;
Gilles Peskine449bd832023-01-11 14:50:10 +01001714 memset(&tmp_crt, 0, sizeof(tmp_crt));
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02001715#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
1716 /* Calculate digest of temporary CRT. */
1717 session->peer_cert_digest =
Gilles Peskine449bd832023-01-11 14:50:10 +01001718 mbedtls_calloc(1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN);
1719 if (session->peer_cert_digest == NULL) {
1720 return -1;
1721 }
Przemek Stekielcc59c992022-08-01 11:51:22 +02001722
1723#if defined(MBEDTLS_USE_PSA_CRYPTO)
1724 psa_algorithm_t psa_alg = mbedtls_hash_info_psa_from_md(
Gilles Peskine449bd832023-01-11 14:50:10 +01001725 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE);
Przemek Stekielcc59c992022-08-01 11:51:22 +02001726 size_t hash_size = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001727 psa_status_t status = psa_hash_compute(psa_alg, tmp_crt.raw.p,
1728 tmp_crt.raw.len,
1729 session->peer_cert_digest,
1730 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN,
1731 &hash_size);
1732 ret = psa_ssl_status_to_mbedtls(status);
Przemek Stekielcc59c992022-08-01 11:51:22 +02001733#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001734 ret = mbedtls_md(mbedtls_md_info_from_type(
1735 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE),
1736 tmp_crt.raw.p, tmp_crt.raw.len,
1737 session->peer_cert_digest);
Przemek Stekielcc59c992022-08-01 11:51:22 +02001738#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001739 if (ret != 0) {
1740 return ret;
1741 }
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02001742 session->peer_cert_digest_type =
1743 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
1744 session->peer_cert_digest_len =
1745 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
1746#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
1747
Gilles Peskine449bd832023-01-11 14:50:10 +01001748 mbedtls_x509_crt_free(&tmp_crt);
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001749 }
Ronald Crone68ab4f2022-10-05 12:46:29 +02001750#else /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED && MBEDTLS_FS_IO */
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001751 (void) crt_file;
Ronald Crone68ab4f2022-10-05 12:46:29 +02001752#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED && MBEDTLS_FS_IO */
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001753 session->verify_result = 0xdeadbeef;
1754
1755#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001756 if (ticket_len != 0) {
1757 session->ticket = mbedtls_calloc(1, ticket_len);
1758 if (session->ticket == NULL) {
1759 return -1;
1760 }
1761 memset(session->ticket, 33, ticket_len);
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001762 }
1763 session->ticket_len = ticket_len;
1764 session->ticket_lifetime = 86401;
1765#else
1766 (void) ticket_len;
1767#endif
1768
1769#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
1770 session->mfl_code = 1;
1771#endif
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001772#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1773 session->encrypt_then_mac = 1;
1774#endif
1775
Gilles Peskine449bd832023-01-11 14:50:10 +01001776 return 0;
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02001777}
1778
Jerry Yu534ff402022-07-14 16:43:43 +08001779#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001780static int ssl_tls13_populate_session(mbedtls_ssl_session *session,
1781 int ticket_len,
1782 int endpoint_type)
Jerry Yu534ff402022-07-14 16:43:43 +08001783{
1784 ((void) ticket_len);
1785 session->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
1786 session->endpoint = endpoint_type == MBEDTLS_SSL_IS_CLIENT ?
Gilles Peskine449bd832023-01-11 14:50:10 +01001787 MBEDTLS_SSL_IS_CLIENT : MBEDTLS_SSL_IS_SERVER;
Jerry Yu534ff402022-07-14 16:43:43 +08001788 session->ciphersuite = 0xabcd;
1789 session->ticket_age_add = 0x87654321;
1790 session->ticket_flags = 0x7;
1791
Jerry Yubc7c1a42022-07-21 22:57:37 +08001792 session->resumption_key_len = 32;
Gilles Peskine449bd832023-01-11 14:50:10 +01001793 memset(session->resumption_key, 0x99, sizeof(session->resumption_key));
Jerry Yu534ff402022-07-14 16:43:43 +08001794
1795#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01001796 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
1797 session->start = mbedtls_time(NULL) - 42;
Jerry Yu534ff402022-07-14 16:43:43 +08001798 }
1799#endif
1800
1801#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001802 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu534ff402022-07-14 16:43:43 +08001803#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01001804 session->ticket_received = mbedtls_time(NULL) - 40;
Jerry Yu534ff402022-07-14 16:43:43 +08001805#endif
1806 session->ticket_lifetime = 0xfedcba98;
1807
1808 session->ticket_len = ticket_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001809 if (ticket_len != 0) {
1810 session->ticket = mbedtls_calloc(1, ticket_len);
1811 if (session->ticket == NULL) {
1812 return -1;
1813 }
1814 memset(session->ticket, 33, ticket_len);
Jerry Yu534ff402022-07-14 16:43:43 +08001815 }
1816 }
1817#endif /* MBEDTLS_SSL_CLI_C */
1818
Gilles Peskine449bd832023-01-11 14:50:10 +01001819 return 0;
Jerry Yu534ff402022-07-14 16:43:43 +08001820}
1821#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1822
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001823/*
1824 * Perform data exchanging between \p ssl_1 and \p ssl_2 and check if the
1825 * message was sent in the correct number of fragments.
1826 *
1827 * /p ssl_1 and /p ssl_2 Endpoints represented by mbedtls_ssl_context. Both
1828 * of them must be initialized and connected beforehand.
1829 * /p msg_len_1 and /p msg_len_2 specify the size of the message to send.
1830 * /p expected_fragments_1 and /p expected_fragments_2 determine in how many
1831 * fragments the message should be sent.
1832 * expected_fragments is 0: can be used for DTLS testing while the message
1833 * size is larger than MFL. In that case the message
1834 * cannot be fragmented and sent to the second endpoint.
1835 * This value can be used for negative tests.
1836 * expected_fragments is 1: can be used for TLS/DTLS testing while the
1837 * message size is below MFL
1838 * expected_fragments > 1: can be used for TLS testing while the message
1839 * size is larger than MFL
1840 *
1841 * \retval 0 on success, otherwise error code.
1842 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001843int mbedtls_exchange_data(mbedtls_ssl_context *ssl_1,
1844 int msg_len_1, const int expected_fragments_1,
1845 mbedtls_ssl_context *ssl_2,
1846 int msg_len_2, const int expected_fragments_2)
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001847{
Gilles Peskine449bd832023-01-11 14:50:10 +01001848 unsigned char *msg_buf_1 = malloc(msg_len_1);
1849 unsigned char *msg_buf_2 = malloc(msg_len_2);
1850 unsigned char *in_buf_1 = malloc(msg_len_2);
1851 unsigned char *in_buf_2 = malloc(msg_len_1);
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001852 int msg_type, ret = -1;
1853
1854 /* Perform this test with two message types. At first use a message
1855 * consisting of only 0x00 for the client and only 0xFF for the server.
1856 * At the second time use message with generated data */
Gilles Peskine449bd832023-01-11 14:50:10 +01001857 for (msg_type = 0; msg_type < 2; msg_type++) {
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001858 int written_1 = 0;
1859 int written_2 = 0;
1860 int read_1 = 0;
1861 int read_2 = 0;
1862 int fragments_1 = 0;
1863 int fragments_2 = 0;
1864
Gilles Peskine449bd832023-01-11 14:50:10 +01001865 if (msg_type == 0) {
1866 memset(msg_buf_1, 0x00, msg_len_1);
1867 memset(msg_buf_2, 0xff, msg_len_2);
1868 } else {
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001869 int i, j = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001870 for (i = 0; i < msg_len_1; i++) {
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001871 msg_buf_1[i] = j++ & 0xFF;
1872 }
Gilles Peskine449bd832023-01-11 14:50:10 +01001873 for (i = 0; i < msg_len_2; i++) {
1874 msg_buf_2[i] = (j -= 5) & 0xFF;
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001875 }
1876 }
1877
Gilles Peskine449bd832023-01-11 14:50:10 +01001878 while (read_1 < msg_len_2 || read_2 < msg_len_1) {
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001879 /* ssl_1 sending */
Gilles Peskine449bd832023-01-11 14:50:10 +01001880 if (msg_len_1 > written_1) {
1881 ret = mbedtls_ssl_write_fragment(ssl_1, msg_buf_1,
1882 msg_len_1, &written_1,
1883 expected_fragments_1);
1884 if (expected_fragments_1 == 0) {
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001885 /* This error is expected when the message is too large and
1886 * cannot be fragmented */
Gilles Peskine449bd832023-01-11 14:50:10 +01001887 TEST_ASSERT(ret == MBEDTLS_ERR_SSL_BAD_INPUT_DATA);
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001888 msg_len_1 = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001889 } else {
1890 TEST_ASSERT(ret == 0);
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001891 }
1892 }
1893
1894 /* ssl_2 sending */
Gilles Peskine449bd832023-01-11 14:50:10 +01001895 if (msg_len_2 > written_2) {
1896 ret = mbedtls_ssl_write_fragment(ssl_2, msg_buf_2,
1897 msg_len_2, &written_2,
1898 expected_fragments_2);
1899 if (expected_fragments_2 == 0) {
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001900 /* This error is expected when the message is too large and
1901 * cannot be fragmented */
Gilles Peskine449bd832023-01-11 14:50:10 +01001902 TEST_ASSERT(ret == MBEDTLS_ERR_SSL_BAD_INPUT_DATA);
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001903 msg_len_2 = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001904 } else {
1905 TEST_ASSERT(ret == 0);
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001906 }
1907 }
1908
1909 /* ssl_1 reading */
Gilles Peskine449bd832023-01-11 14:50:10 +01001910 if (read_1 < msg_len_2) {
1911 ret = mbedtls_ssl_read_fragment(ssl_1, in_buf_1,
1912 msg_len_2, &read_1,
1913 &fragments_2,
1914 expected_fragments_2);
1915 TEST_ASSERT(ret == 0);
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001916 }
1917
1918 /* ssl_2 reading */
Gilles Peskine449bd832023-01-11 14:50:10 +01001919 if (read_2 < msg_len_1) {
1920 ret = mbedtls_ssl_read_fragment(ssl_2, in_buf_2,
1921 msg_len_1, &read_2,
1922 &fragments_1,
1923 expected_fragments_1);
1924 TEST_ASSERT(ret == 0);
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001925 }
1926 }
1927
1928 ret = -1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001929 TEST_ASSERT(0 == memcmp(msg_buf_1, in_buf_2, msg_len_1));
1930 TEST_ASSERT(0 == memcmp(msg_buf_2, in_buf_1, msg_len_2));
1931 TEST_ASSERT(fragments_1 == expected_fragments_1);
1932 TEST_ASSERT(fragments_2 == expected_fragments_2);
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001933 }
1934
1935 ret = 0;
1936
1937exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001938 free(msg_buf_1);
1939 free(in_buf_1);
1940 free(msg_buf_2);
1941 free(in_buf_2);
Piotr Nowicki6a7f01c2020-02-12 13:53:36 +01001942
1943 return ret;
1944}
1945
Piotr Nowicki95e9eb82020-02-14 11:33:34 +01001946/*
1947 * Perform data exchanging between \p ssl_1 and \p ssl_2. Both of endpoints
1948 * must be initialized and connected beforehand.
1949 *
1950 * \retval 0 on success, otherwise error code.
1951 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001952int exchange_data(mbedtls_ssl_context *ssl_1,
1953 mbedtls_ssl_context *ssl_2)
Piotr Nowicki95e9eb82020-02-14 11:33:34 +01001954{
Gilles Peskine449bd832023-01-11 14:50:10 +01001955 return mbedtls_exchange_data(ssl_1, 256, 1,
1956 ssl_2, 256, 1);
Piotr Nowicki95e9eb82020-02-14 11:33:34 +01001957}
1958
Ronald Crone68ab4f2022-10-05 12:46:29 +02001959#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001960static int check_ssl_version(mbedtls_ssl_protocol_version expected_negotiated_version,
1961 const mbedtls_ssl_context *ssl)
Gilles Peskine1255b0d2022-01-13 01:08:48 +01001962{
Gilles Peskine449bd832023-01-11 14:50:10 +01001963 const char *version_string = mbedtls_ssl_get_version(ssl);
Gilles Peskine1255b0d2022-01-13 01:08:48 +01001964 mbedtls_ssl_protocol_version version_number =
Gilles Peskine449bd832023-01-11 14:50:10 +01001965 mbedtls_ssl_get_version_number(ssl);
Gilles Peskine1255b0d2022-01-13 01:08:48 +01001966
Gilles Peskine449bd832023-01-11 14:50:10 +01001967 TEST_EQUAL(ssl->tls_version, expected_negotiated_version);
Gilles Peskine1255b0d2022-01-13 01:08:48 +01001968
Gilles Peskine449bd832023-01-11 14:50:10 +01001969 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1970 TEST_EQUAL(version_string[0], 'D');
Gilles Peskine1255b0d2022-01-13 01:08:48 +01001971 ++version_string;
1972 }
1973
Gilles Peskine449bd832023-01-11 14:50:10 +01001974 switch (expected_negotiated_version) {
Glenn Strausse3af4cb2022-03-15 03:23:42 -04001975 case MBEDTLS_SSL_VERSION_TLS1_2:
Gilles Peskine449bd832023-01-11 14:50:10 +01001976 TEST_EQUAL(version_number, MBEDTLS_SSL_VERSION_TLS1_2);
1977 TEST_ASSERT(strcmp(version_string, "TLSv1.2") == 0);
Gilles Peskine1255b0d2022-01-13 01:08:48 +01001978 break;
1979
Glenn Strausse3af4cb2022-03-15 03:23:42 -04001980 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskine449bd832023-01-11 14:50:10 +01001981 TEST_EQUAL(version_number, MBEDTLS_SSL_VERSION_TLS1_3);
1982 TEST_ASSERT(strcmp(version_string, "TLSv1.3") == 0);
Gilles Peskine1255b0d2022-01-13 01:08:48 +01001983 break;
1984
1985 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01001986 TEST_ASSERT(!"Version check not implemented for this protocol version");
Gilles Peskine1255b0d2022-01-13 01:08:48 +01001987 }
1988
Gilles Peskine449bd832023-01-11 14:50:10 +01001989 return 1;
Gilles Peskine1255b0d2022-01-13 01:08:48 +01001990
1991exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001992 return 0;
Gilles Peskine1255b0d2022-01-13 01:08:48 +01001993}
Ronald Crone68ab4f2022-10-05 12:46:29 +02001994#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gilles Peskine1255b0d2022-01-13 01:08:48 +01001995
1996
Ronald Crone68ab4f2022-10-05 12:46:29 +02001997#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001998void perform_handshake(handshake_test_options *options)
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05001999{
2000 /* forced_ciphersuite needs to last until the end of the handshake */
2001 int forced_ciphersuite[2];
2002 enum { BUFFSIZE = 17000 };
2003 mbedtls_endpoint client, server;
Ronald Cron73fe8df2022-10-05 14:31:43 +02002004#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002005 const char *psk_identity = "foo";
2006#endif
2007#if defined(MBEDTLS_TIMING_C)
2008 mbedtls_timing_delay_context timer_client, timer_server;
2009#endif
2010#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
2011 unsigned char *context_buf = NULL;
2012 size_t context_buf_len;
2013#endif
2014#if defined(MBEDTLS_SSL_RENEGOTIATION)
2015 int ret = -1;
2016#endif
Neil Armstrong8c52ed82022-05-27 13:14:55 +02002017 int expected_handshake_result = options->expected_handshake_result;
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002018
Gilles Peskine449bd832023-01-11 14:50:10 +01002019 USE_PSA_INIT();
2020 mbedtls_platform_zeroize(&client, sizeof(client));
2021 mbedtls_platform_zeroize(&server, sizeof(server));
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002022 mbedtls_test_message_queue server_queue, client_queue;
2023 mbedtls_test_message_socket_context server_context, client_context;
Gilles Peskine449bd832023-01-11 14:50:10 +01002024 mbedtls_message_socket_init(&server_context);
2025 mbedtls_message_socket_init(&client_context);
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002026
2027 /* Client side */
Gilles Peskine449bd832023-01-11 14:50:10 +01002028 if (options->dtls != 0) {
2029 TEST_ASSERT(mbedtls_endpoint_init(&client, MBEDTLS_SSL_IS_CLIENT,
2030 options, &client_context,
2031 &client_queue,
2032 &server_queue, NULL) == 0);
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002033#if defined(MBEDTLS_TIMING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002034 mbedtls_ssl_set_timer_cb(&client.ssl, &timer_client,
2035 mbedtls_timing_set_delay,
2036 mbedtls_timing_get_delay);
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002037#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002038 } else {
2039 TEST_ASSERT(mbedtls_endpoint_init(&client, MBEDTLS_SSL_IS_CLIENT,
2040 options, NULL, NULL,
2041 NULL, NULL) == 0);
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002042 }
Paul Elliottc8570442020-04-15 17:00:50 +01002043
Gilles Peskine449bd832023-01-11 14:50:10 +01002044 if (options->client_min_version != MBEDTLS_SSL_VERSION_UNKNOWN) {
2045 mbedtls_ssl_conf_min_tls_version(&client.conf,
2046 options->client_min_version);
Paul Elliottc8570442020-04-15 17:00:50 +01002047 }
2048
Gilles Peskine449bd832023-01-11 14:50:10 +01002049 if (options->client_max_version != MBEDTLS_SSL_VERSION_UNKNOWN) {
2050 mbedtls_ssl_conf_max_tls_version(&client.conf,
2051 options->client_max_version);
Paul Elliottc8570442020-04-15 17:00:50 +01002052 }
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002053
Gilles Peskine449bd832023-01-11 14:50:10 +01002054 if (strlen(options->cipher) > 0) {
2055 set_ciphersuite(&client.conf, options->cipher, forced_ciphersuite);
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002056 }
Piotr Nowickibde7ee82020-02-21 10:59:50 +01002057
Gilles Peskine449bd832023-01-11 14:50:10 +01002058#if defined(MBEDTLS_DEBUG_C)
2059 if (options->cli_log_fun) {
2060 mbedtls_debug_set_threshold(4);
2061 mbedtls_ssl_conf_dbg(&client.conf, options->cli_log_fun,
2062 options->cli_log_obj);
Piotr Nowickibde7ee82020-02-21 10:59:50 +01002063 }
2064#endif
2065
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002066 /* Server side */
Gilles Peskine449bd832023-01-11 14:50:10 +01002067 if (options->dtls != 0) {
2068 TEST_ASSERT(mbedtls_endpoint_init(&server, MBEDTLS_SSL_IS_SERVER,
2069 options, &server_context,
2070 &server_queue,
2071 &client_queue, NULL) == 0);
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002072#if defined(MBEDTLS_TIMING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002073 mbedtls_ssl_set_timer_cb(&server.ssl, &timer_server,
2074 mbedtls_timing_set_delay,
2075 mbedtls_timing_get_delay);
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002076#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002077 } else {
2078 TEST_ASSERT(mbedtls_endpoint_init(&server, MBEDTLS_SSL_IS_SERVER,
2079 options, NULL, NULL, NULL,
2080 NULL) == 0);
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002081 }
Piotr Nowickibde7ee82020-02-21 10:59:50 +01002082
Gilles Peskine449bd832023-01-11 14:50:10 +01002083 mbedtls_ssl_conf_authmode(&server.conf, options->srv_auth_mode);
Piotr Nowickibde7ee82020-02-21 10:59:50 +01002084
Gilles Peskine449bd832023-01-11 14:50:10 +01002085 if (options->server_min_version != MBEDTLS_SSL_VERSION_UNKNOWN) {
2086 mbedtls_ssl_conf_min_tls_version(&server.conf,
2087 options->server_min_version);
Paul Elliottc8570442020-04-15 17:00:50 +01002088 }
2089
Gilles Peskine449bd832023-01-11 14:50:10 +01002090 if (options->server_max_version != MBEDTLS_SSL_VERSION_UNKNOWN) {
2091 mbedtls_ssl_conf_max_tls_version(&server.conf,
2092 options->server_max_version);
Paul Elliottc8570442020-04-15 17:00:50 +01002093 }
2094
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002095#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01002096 TEST_ASSERT(mbedtls_ssl_conf_max_frag_len(&(server.conf),
2097 (unsigned char) options->mfl) == 0);
2098 TEST_ASSERT(mbedtls_ssl_conf_max_frag_len(&(client.conf),
2099 (unsigned char) options->mfl) == 0);
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002100#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002101 TEST_ASSERT(MBEDTLS_SSL_MAX_FRAG_LEN_NONE == options->mfl);
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002102#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
2103
Ronald Cron73fe8df2022-10-05 14:31:43 +02002104#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002105 if (options->psk_str != NULL && options->psk_str->len > 0) {
2106 TEST_ASSERT(mbedtls_ssl_conf_psk(&client.conf, options->psk_str->x,
2107 options->psk_str->len,
2108 (const unsigned char *) psk_identity,
2109 strlen(psk_identity)) == 0);
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002110
Gilles Peskine449bd832023-01-11 14:50:10 +01002111 TEST_ASSERT(mbedtls_ssl_conf_psk(&server.conf, options->psk_str->x,
2112 options->psk_str->len,
2113 (const unsigned char *) psk_identity,
2114 strlen(psk_identity)) == 0);
Jerry Yu8897c072022-08-12 13:56:53 +08002115#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002116 mbedtls_ssl_conf_psk_cb(&server.conf, psk_dummy_callback, NULL);
Jerry Yu8897c072022-08-12 13:56:53 +08002117#endif
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002118 }
2119#endif
2120#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002121 if (options->renegotiate) {
2122 mbedtls_ssl_conf_renegotiation(&(server.conf),
2123 MBEDTLS_SSL_RENEGOTIATION_ENABLED);
2124 mbedtls_ssl_conf_renegotiation(&(client.conf),
2125 MBEDTLS_SSL_RENEGOTIATION_ENABLED);
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002126
Gilles Peskine449bd832023-01-11 14:50:10 +01002127 mbedtls_ssl_conf_legacy_renegotiation(&(server.conf),
2128 options->legacy_renegotiation);
2129 mbedtls_ssl_conf_legacy_renegotiation(&(client.conf),
2130 options->legacy_renegotiation);
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002131 }
2132#endif /* MBEDTLS_SSL_RENEGOTIATION */
2133
Gilles Peskine449bd832023-01-11 14:50:10 +01002134#if defined(MBEDTLS_DEBUG_C)
2135 if (options->srv_log_fun) {
2136 mbedtls_debug_set_threshold(4);
2137 mbedtls_ssl_conf_dbg(&server.conf, options->srv_log_fun,
2138 options->srv_log_obj);
Piotr Nowickibde7ee82020-02-21 10:59:50 +01002139 }
2140#endif
2141
Gilles Peskine449bd832023-01-11 14:50:10 +01002142 TEST_ASSERT(mbedtls_mock_socket_connect(&(client.socket),
2143 &(server.socket),
2144 BUFFSIZE) == 0);
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002145
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05002146#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01002147 if (options->resize_buffers != 0) {
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05002148 /* Ensure that the buffer sizes are appropriate before resizes */
Gilles Peskine449bd832023-01-11 14:50:10 +01002149 TEST_ASSERT(client.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN);
2150 TEST_ASSERT(client.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN);
2151 TEST_ASSERT(server.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN);
2152 TEST_ASSERT(server.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05002153 }
2154#endif
2155
Gilles Peskine449bd832023-01-11 14:50:10 +01002156 if (options->expected_negotiated_version == MBEDTLS_SSL_VERSION_UNKNOWN) {
Hanno Beckerbc000442021-06-24 09:18:19 +01002157 expected_handshake_result = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
Paul Elliottc8570442020-04-15 17:00:50 +01002158 }
2159
Gilles Peskine449bd832023-01-11 14:50:10 +01002160 TEST_ASSERT(mbedtls_move_handshake_to_state(&(client.ssl),
2161 &(server.ssl),
2162 MBEDTLS_SSL_HANDSHAKE_OVER)
2163 == expected_handshake_result);
Paul Elliottc8570442020-04-15 17:00:50 +01002164
Gilles Peskine449bd832023-01-11 14:50:10 +01002165 if (expected_handshake_result != 0) {
Paul Elliottc8570442020-04-15 17:00:50 +01002166 /* Connection will have failed by this point, skip to cleanup */
2167 goto exit;
2168 }
2169
Gilles Peskine449bd832023-01-11 14:50:10 +01002170 TEST_ASSERT(mbedtls_ssl_is_handshake_over(&client.ssl) == 1);
Jerry Yudf0a71a2022-05-26 10:43:30 +08002171
Jerry Yu66adf312022-05-31 15:23:29 +08002172 /* Make sure server state is moved to HANDSHAKE_OVER also. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002173 TEST_EQUAL(mbedtls_move_handshake_to_state(&(server.ssl),
2174 &(client.ssl),
2175 MBEDTLS_SSL_HANDSHAKE_OVER), 0);
Jerry Yudf0a71a2022-05-26 10:43:30 +08002176
Gilles Peskine449bd832023-01-11 14:50:10 +01002177 TEST_ASSERT(mbedtls_ssl_is_handshake_over(&server.ssl) == 1);
Gilles Peskine1255b0d2022-01-13 01:08:48 +01002178 /* Check that both sides have negotiated the expected version. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002179 mbedtls_test_set_step(0);
2180 if (!check_ssl_version(options->expected_negotiated_version,
2181 &client.ssl)) {
Gilles Peskine1255b0d2022-01-13 01:08:48 +01002182 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002183 }
Paul Elliottc8570442020-04-15 17:00:50 +01002184
Gilles Peskine449bd832023-01-11 14:50:10 +01002185 mbedtls_test_set_step(1);
2186 if (!check_ssl_version(options->expected_negotiated_version,
2187 &server.ssl)) {
Gilles Peskine1255b0d2022-01-13 01:08:48 +01002188 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01002189 }
Paul Elliottc8570442020-04-15 17:00:50 +01002190
Gilles Peskine449bd832023-01-11 14:50:10 +01002191 if (options->expected_ciphersuite != 0) {
2192 TEST_EQUAL(server.ssl.session->ciphersuite,
2193 options->expected_ciphersuite);
Neil Armstrong8c52ed82022-05-27 13:14:55 +02002194 }
2195
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05002196#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01002197 if (options->resize_buffers != 0) {
TRodziewicz2abf03c2021-06-25 14:40:09 +02002198 /* A server, when using DTLS, might delay a buffer resize to happen
2199 * after it receives a message, so we force it. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002200 TEST_ASSERT(exchange_data(&(client.ssl), &(server.ssl)) == 0);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05002201
Gilles Peskine449bd832023-01-11 14:50:10 +01002202 TEST_ASSERT(client.ssl.out_buf_len ==
2203 mbedtls_ssl_get_output_buflen(&client.ssl));
2204 TEST_ASSERT(client.ssl.in_buf_len ==
2205 mbedtls_ssl_get_input_buflen(&client.ssl));
2206 TEST_ASSERT(server.ssl.out_buf_len ==
2207 mbedtls_ssl_get_output_buflen(&server.ssl));
2208 TEST_ASSERT(server.ssl.in_buf_len ==
2209 mbedtls_ssl_get_input_buflen(&server.ssl));
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05002210 }
2211#endif
2212
Gilles Peskine449bd832023-01-11 14:50:10 +01002213 if (options->cli_msg_len != 0 || options->srv_msg_len != 0) {
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002214 /* Start data exchanging test */
Gilles Peskine449bd832023-01-11 14:50:10 +01002215 TEST_ASSERT(mbedtls_exchange_data(&(client.ssl), options->cli_msg_len,
2216 options->expected_cli_fragments,
2217 &(server.ssl), options->srv_msg_len,
2218 options->expected_srv_fragments)
2219 == 0);
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002220 }
2221#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002222 if (options->serialize == 1) {
2223 TEST_ASSERT(options->dtls == 1);
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002224
Gilles Peskine449bd832023-01-11 14:50:10 +01002225 TEST_ASSERT(mbedtls_ssl_context_save(&(server.ssl), NULL,
2226 0, &context_buf_len)
2227 == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL);
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002228
Gilles Peskine449bd832023-01-11 14:50:10 +01002229 context_buf = mbedtls_calloc(1, context_buf_len);
2230 TEST_ASSERT(context_buf != NULL);
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002231
Gilles Peskine449bd832023-01-11 14:50:10 +01002232 TEST_ASSERT(mbedtls_ssl_context_save(&(server.ssl), context_buf,
2233 context_buf_len,
2234 &context_buf_len) == 0);
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002235
Gilles Peskine449bd832023-01-11 14:50:10 +01002236 mbedtls_ssl_free(&(server.ssl));
2237 mbedtls_ssl_init(&(server.ssl));
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002238
Gilles Peskine449bd832023-01-11 14:50:10 +01002239 TEST_ASSERT(mbedtls_ssl_setup(&(server.ssl), &(server.conf)) == 0);
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002240
Gilles Peskine449bd832023-01-11 14:50:10 +01002241 mbedtls_ssl_set_bio(&(server.ssl), &server_context,
2242 mbedtls_mock_tcp_send_msg,
2243 mbedtls_mock_tcp_recv_msg,
2244 NULL);
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002245
Gilles Peskine449bd832023-01-11 14:50:10 +01002246 mbedtls_ssl_set_user_data_p(&server.ssl, &server);
Gilles Peskine49d7ddf2022-01-27 23:25:51 +01002247
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002248#if defined(MBEDTLS_TIMING_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002249 mbedtls_ssl_set_timer_cb(&server.ssl, &timer_server,
2250 mbedtls_timing_set_delay,
2251 mbedtls_timing_get_delay);
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002252#endif
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05002253#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01002254 if (options->resize_buffers != 0) {
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05002255 /* Ensure that the buffer sizes are appropriate before resizes */
Gilles Peskine449bd832023-01-11 14:50:10 +01002256 TEST_ASSERT(server.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN);
2257 TEST_ASSERT(server.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05002258 }
2259#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002260 TEST_ASSERT(mbedtls_ssl_context_load(&(server.ssl), context_buf,
2261 context_buf_len) == 0);
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002262
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05002263#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2264 /* Validate buffer sizes after context deserialization */
Gilles Peskine449bd832023-01-11 14:50:10 +01002265 if (options->resize_buffers != 0) {
2266 TEST_ASSERT(server.ssl.out_buf_len ==
2267 mbedtls_ssl_get_output_buflen(&server.ssl));
2268 TEST_ASSERT(server.ssl.in_buf_len ==
2269 mbedtls_ssl_get_input_buflen(&server.ssl));
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05002270 }
2271#endif
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002272 /* Retest writing/reading */
Gilles Peskine449bd832023-01-11 14:50:10 +01002273 if (options->cli_msg_len != 0 || options->srv_msg_len != 0) {
2274 TEST_ASSERT(mbedtls_exchange_data(&(client.ssl),
2275 options->cli_msg_len,
2276 options->expected_cli_fragments,
2277 &(server.ssl),
2278 options->srv_msg_len,
2279 options->expected_srv_fragments)
2280 == 0);
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002281 }
2282 }
2283#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05002284
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002285#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002286 if (options->renegotiate) {
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002287 /* Start test with renegotiation */
Gilles Peskine449bd832023-01-11 14:50:10 +01002288 TEST_ASSERT(server.ssl.renego_status ==
2289 MBEDTLS_SSL_INITIAL_HANDSHAKE);
2290 TEST_ASSERT(client.ssl.renego_status ==
2291 MBEDTLS_SSL_INITIAL_HANDSHAKE);
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002292
2293 /* After calling this function for the server, it only sends a handshake
2294 * request. All renegotiation should happen during data exchanging */
Gilles Peskine449bd832023-01-11 14:50:10 +01002295 TEST_ASSERT(mbedtls_ssl_renegotiate(&(server.ssl)) == 0);
2296 TEST_ASSERT(server.ssl.renego_status ==
2297 MBEDTLS_SSL_RENEGOTIATION_PENDING);
2298 TEST_ASSERT(client.ssl.renego_status ==
2299 MBEDTLS_SSL_INITIAL_HANDSHAKE);
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002300
Gilles Peskine449bd832023-01-11 14:50:10 +01002301 TEST_ASSERT(exchange_data(&(client.ssl), &(server.ssl)) == 0);
2302 TEST_ASSERT(server.ssl.renego_status ==
2303 MBEDTLS_SSL_RENEGOTIATION_DONE);
2304 TEST_ASSERT(client.ssl.renego_status ==
2305 MBEDTLS_SSL_RENEGOTIATION_DONE);
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002306
2307 /* After calling mbedtls_ssl_renegotiate for the client all renegotiation
2308 * should happen inside this function. However in this test, we cannot
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002309 * perform simultaneous communication between client and server so this
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002310 * function will return waiting error on the socket. All rest of
2311 * renegotiation should happen during data exchanging */
Gilles Peskine449bd832023-01-11 14:50:10 +01002312 ret = mbedtls_ssl_renegotiate(&(client.ssl));
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05002313#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01002314 if (options->resize_buffers != 0) {
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05002315 /* Ensure that the buffer sizes are appropriate before resizes */
Gilles Peskine449bd832023-01-11 14:50:10 +01002316 TEST_ASSERT(client.ssl.out_buf_len == MBEDTLS_SSL_OUT_BUFFER_LEN);
2317 TEST_ASSERT(client.ssl.in_buf_len == MBEDTLS_SSL_IN_BUFFER_LEN);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05002318 }
2319#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002320 TEST_ASSERT(ret == 0 ||
2321 ret == MBEDTLS_ERR_SSL_WANT_READ ||
2322 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
2323 TEST_ASSERT(server.ssl.renego_status ==
2324 MBEDTLS_SSL_RENEGOTIATION_DONE);
2325 TEST_ASSERT(client.ssl.renego_status ==
2326 MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS);
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002327
Gilles Peskine449bd832023-01-11 14:50:10 +01002328 TEST_ASSERT(exchange_data(&(client.ssl), &(server.ssl)) == 0);
2329 TEST_ASSERT(server.ssl.renego_status ==
2330 MBEDTLS_SSL_RENEGOTIATION_DONE);
2331 TEST_ASSERT(client.ssl.renego_status ==
2332 MBEDTLS_SSL_RENEGOTIATION_DONE);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05002333#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
2334 /* Validate buffer sizes after renegotiation */
Gilles Peskine449bd832023-01-11 14:50:10 +01002335 if (options->resize_buffers != 0) {
2336 TEST_ASSERT(client.ssl.out_buf_len ==
2337 mbedtls_ssl_get_output_buflen(&client.ssl));
2338 TEST_ASSERT(client.ssl.in_buf_len ==
2339 mbedtls_ssl_get_input_buflen(&client.ssl));
2340 TEST_ASSERT(server.ssl.out_buf_len ==
2341 mbedtls_ssl_get_output_buflen(&server.ssl));
2342 TEST_ASSERT(server.ssl.in_buf_len ==
2343 mbedtls_ssl_get_input_buflen(&server.ssl));
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05002344 }
2345#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002346 }
2347#endif /* MBEDTLS_SSL_RENEGOTIATION */
2348
Gilles Peskine449bd832023-01-11 14:50:10 +01002349 TEST_ASSERT(mbedtls_ssl_conf_get_user_data_p(&client.conf) == &client);
2350 TEST_ASSERT(mbedtls_ssl_get_user_data_p(&client.ssl) == &client);
2351 TEST_ASSERT(mbedtls_ssl_conf_get_user_data_p(&server.conf) == &server);
2352 TEST_ASSERT(mbedtls_ssl_get_user_data_p(&server.ssl) == &server);
Gilles Peskine80dae042022-01-21 23:50:39 +01002353
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002354exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002355 mbedtls_endpoint_free(&client, options->dtls != 0 ? &client_context : NULL);
2356 mbedtls_endpoint_free(&server, options->dtls != 0 ? &server_context : NULL);
2357#if defined(MBEDTLS_DEBUG_C)
2358 if (options->cli_log_fun || options->srv_log_fun) {
2359 mbedtls_debug_set_threshold(0);
Piotr Nowickibde7ee82020-02-21 10:59:50 +01002360 }
2361#endif
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002362#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002363 if (context_buf != NULL) {
2364 mbedtls_free(context_buf);
2365 }
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002366#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002367 USE_PSA_DONE();
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002368}
Ronald Crone68ab4f2022-10-05 12:46:29 +02002369#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05002370
Ronald Crone7b9b6b2022-06-10 17:24:31 +02002371#if defined(MBEDTLS_TEST_HOOKS)
Ronald Crone3dac4a2022-06-10 17:21:51 +02002372/*
2373 * Tweak vector lengths in a TLS 1.3 Certificate message
2374 *
Ronald Croncf600bc2022-06-17 15:54:16 +02002375 * \param[in] buf Buffer containing the Certificate message to tweak
2376 * \param[in]]out] end End of the buffer to parse
2377 * \param tweak Tweak identifier (from 1 to the number of tweaks).
2378 * \param[out] expected_result Error code expected from the parsing function
2379 * \param[out] args Arguments of the MBEDTLS_SSL_CHK_BUF_READ_PTR call that
Ronald Crone3dac4a2022-06-10 17:21:51 +02002380 * is expected to fail. All zeroes if no
2381 * MBEDTLS_SSL_CHK_BUF_READ_PTR failure is expected.
2382 */
2383int tweak_tls13_certificate_msg_vector_len(
Ronald Crone7b9b6b2022-06-10 17:24:31 +02002384 unsigned char *buf, unsigned char **end, int tweak,
Gilles Peskine449bd832023-01-11 14:50:10 +01002385 int *expected_result, mbedtls_ssl_chk_buf_ptr_args *args)
Ronald Crone3dac4a2022-06-10 17:21:51 +02002386{
2387/*
2388 * The definition of the tweaks assume that the certificate list contains only
2389 * one certificate.
2390 */
2391
2392/*
2393 * struct {
2394 * opaque cert_data<1..2^24-1>;
2395 * Extension extensions<0..2^16-1>;
2396 * } CertificateEntry;
2397 *
2398 * struct {
2399 * opaque certificate_request_context<0..2^8-1>;
2400 * CertificateEntry certificate_list<0..2^24-1>;
2401 * } Certificate;
2402 */
2403 unsigned char *p_certificate_request_context_len = buf;
2404 size_t certificate_request_context_len = buf[0];
2405
2406 unsigned char *p_certificate_list_len = buf + 1 + certificate_request_context_len;
2407 unsigned char *certificate_list = p_certificate_list_len + 3;
Gilles Peskine449bd832023-01-11 14:50:10 +01002408 size_t certificate_list_len = MBEDTLS_GET_UINT24_BE(p_certificate_list_len, 0);
Ronald Crone3dac4a2022-06-10 17:21:51 +02002409
2410 unsigned char *p_cert_data_len = certificate_list;
2411 unsigned char *cert_data = p_cert_data_len + 3;
Gilles Peskine449bd832023-01-11 14:50:10 +01002412 size_t cert_data_len = MBEDTLS_GET_UINT24_BE(p_cert_data_len, 0);
Ronald Crone3dac4a2022-06-10 17:21:51 +02002413
2414 unsigned char *p_extensions_len = cert_data + cert_data_len;
2415 unsigned char *extensions = p_extensions_len + 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002416 size_t extensions_len = MBEDTLS_GET_UINT16_BE(p_extensions_len, 0);
Ronald Crone3dac4a2022-06-10 17:21:51 +02002417
2418 *expected_result = MBEDTLS_ERR_SSL_DECODE_ERROR;
2419
Gilles Peskine449bd832023-01-11 14:50:10 +01002420 switch (tweak) {
Ronald Crone3dac4a2022-06-10 17:21:51 +02002421 case 1:
Gilles Peskine449bd832023-01-11 14:50:10 +01002422 /* Failure when checking if the certificate request context length and
2423 * certificate list length can be read
2424 */
2425 *end = buf + 3;
2426 set_chk_buf_ptr_args(args, buf, *end, 4);
2427 break;
Ronald Crone3dac4a2022-06-10 17:21:51 +02002428
2429 case 2:
Gilles Peskine449bd832023-01-11 14:50:10 +01002430 /* Invalid certificate request context length.
2431 */
2432 *p_certificate_request_context_len =
2433 certificate_request_context_len + 1;
2434 reset_chk_buf_ptr_args(args);
2435 break;
Ronald Crone3dac4a2022-06-10 17:21:51 +02002436
2437 case 3:
Gilles Peskine449bd832023-01-11 14:50:10 +01002438 /* Failure when checking if certificate_list data can be read. */
2439 MBEDTLS_PUT_UINT24_BE(certificate_list_len + 1,
2440 p_certificate_list_len, 0);
2441 set_chk_buf_ptr_args(args, certificate_list, *end,
2442 certificate_list_len + 1);
2443 break;
Ronald Crone3dac4a2022-06-10 17:21:51 +02002444
2445 case 4:
Gilles Peskine449bd832023-01-11 14:50:10 +01002446 /* Failure when checking if the cert_data length can be read. */
2447 MBEDTLS_PUT_UINT24_BE(2, p_certificate_list_len, 0);
2448 set_chk_buf_ptr_args(args, p_cert_data_len, certificate_list + 2, 3);
2449 break;
Ronald Crone3dac4a2022-06-10 17:21:51 +02002450
2451 case 5:
Gilles Peskine449bd832023-01-11 14:50:10 +01002452 /* Failure when checking if cert_data data can be read. */
2453 MBEDTLS_PUT_UINT24_BE(certificate_list_len - 3 + 1,
2454 p_cert_data_len, 0);
2455 set_chk_buf_ptr_args(args, cert_data,
2456 certificate_list + certificate_list_len,
2457 certificate_list_len - 3 + 1);
2458 break;
Ronald Crone3dac4a2022-06-10 17:21:51 +02002459
2460 case 6:
Gilles Peskine449bd832023-01-11 14:50:10 +01002461 /* Failure when checking if the extensions length can be read. */
2462 MBEDTLS_PUT_UINT24_BE(certificate_list_len - extensions_len - 1,
2463 p_certificate_list_len, 0);
2464 set_chk_buf_ptr_args(args, p_extensions_len,
2465 certificate_list + certificate_list_len - extensions_len - 1, 2);
2466 break;
Ronald Crone3dac4a2022-06-10 17:21:51 +02002467
2468 case 7:
Gilles Peskine449bd832023-01-11 14:50:10 +01002469 /* Failure when checking if extensions data can be read. */
2470 MBEDTLS_PUT_UINT16_BE(extensions_len + 1, p_extensions_len, 0);
Ronald Crone7b9b6b2022-06-10 17:24:31 +02002471
Gilles Peskine449bd832023-01-11 14:50:10 +01002472 set_chk_buf_ptr_args(args, extensions,
2473 certificate_list + certificate_list_len, extensions_len + 1);
2474 break;
Ronald Crone3dac4a2022-06-10 17:21:51 +02002475
2476 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01002477 return -1;
Ronald Crone3dac4a2022-06-10 17:21:51 +02002478 }
2479
Gilles Peskine449bd832023-01-11 14:50:10 +01002480 return 0;
Ronald Crone3dac4a2022-06-10 17:21:51 +02002481}
Ronald Crone7b9b6b2022-06-10 17:24:31 +02002482#endif /* MBEDTLS_TEST_HOOKS */
Valerio Setti4452e982022-12-01 15:08:35 +01002483
Valerio Setti4452e982022-12-01 15:08:35 +01002484#define ECJPAKE_TEST_PWD "bla"
Valerio Settie7518ba2022-12-02 12:09:43 +01002485
Gilles Peskine449bd832023-01-11 14:50:10 +01002486#if defined(MBEDTLS_USE_PSA_CRYPTO)
2487#define ECJPAKE_TEST_SET_PASSWORD(exp_ret_val) \
2488 ret = (use_opaque_arg) ? \
2489 mbedtls_ssl_set_hs_ecjpake_password_opaque(&ssl, pwd_slot) : \
2490 mbedtls_ssl_set_hs_ecjpake_password(&ssl, pwd_string, pwd_len); \
2491 TEST_EQUAL(ret, exp_ret_val)
Valerio Settie7518ba2022-12-02 12:09:43 +01002492#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002493#define ECJPAKE_TEST_SET_PASSWORD(exp_ret_val) \
2494 ret = mbedtls_ssl_set_hs_ecjpake_password(&ssl, \
2495 pwd_string, pwd_len); \
2496 TEST_EQUAL(ret, exp_ret_val)
Valerio Settie7518ba2022-12-02 12:09:43 +01002497#endif
Valerio Setti73260b62023-01-03 12:53:28 +01002498
Gilles Peskine449bd832023-01-11 14:50:10 +01002499#define TEST_AVAILABLE_ECC(tls_id_, group_id_, psa_family_, psa_bits_) \
2500 TEST_EQUAL(mbedtls_ssl_get_ecp_group_id_from_tls_id(tls_id_), \
2501 group_id_); \
2502 TEST_EQUAL(mbedtls_ssl_get_tls_id_from_ecp_group_id(group_id_), \
2503 tls_id_); \
2504 TEST_EQUAL(mbedtls_ssl_get_psa_curve_info_from_tls_id(tls_id_, \
2505 &psa_family, &psa_bits), PSA_SUCCESS); \
2506 TEST_EQUAL(psa_family_, psa_family); \
2507 TEST_EQUAL(psa_bits_, psa_bits);
Valerio Setti73260b62023-01-03 12:53:28 +01002508
Gilles Peskine449bd832023-01-11 14:50:10 +01002509#define TEST_UNAVAILABLE_ECC(tls_id_, group_id_, psa_family_, psa_bits_) \
2510 TEST_EQUAL(mbedtls_ssl_get_ecp_group_id_from_tls_id(tls_id_), \
2511 MBEDTLS_ECP_DP_NONE); \
2512 TEST_EQUAL(mbedtls_ssl_get_tls_id_from_ecp_group_id(group_id_), \
2513 0); \
2514 TEST_EQUAL(mbedtls_ssl_get_psa_curve_info_from_tls_id(tls_id_, \
2515 &psa_family, &psa_bits), \
2516 PSA_ERROR_NOT_SUPPORTED);
Valerio Setti73260b62023-01-03 12:53:28 +01002517
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002518/* END_HEADER */
2519
2520/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002521 * depends_on:MBEDTLS_SSL_TLS_C
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02002522 * END_DEPENDENCIES
2523 */
2524
Janos Follath6264e662019-11-26 11:11:15 +00002525/* BEGIN_CASE */
2526void test_callback_buffer_sanity()
2527{
2528 enum { MSGLEN = 10 };
2529 mbedtls_test_buffer buf;
2530 unsigned char input[MSGLEN];
2531 unsigned char output[MSGLEN];
2532
Gilles Peskine449bd832023-01-11 14:50:10 +01002533 memset(input, 0, sizeof(input));
Janos Follath6264e662019-11-26 11:11:15 +00002534
2535 /* Make sure calling put and get on NULL buffer results in error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002536 TEST_ASSERT(mbedtls_test_buffer_put(NULL, input, sizeof(input))
2537 == -1);
2538 TEST_ASSERT(mbedtls_test_buffer_get(NULL, output, sizeof(output))
2539 == -1);
2540 TEST_ASSERT(mbedtls_test_buffer_put(NULL, NULL, sizeof(input)) == -1);
Andrzej Kurekf7774142020-01-22 06:34:59 -05002541
Gilles Peskine449bd832023-01-11 14:50:10 +01002542 TEST_ASSERT(mbedtls_test_buffer_put(NULL, NULL, 0) == -1);
2543 TEST_ASSERT(mbedtls_test_buffer_get(NULL, NULL, 0) == -1);
Janos Follath6264e662019-11-26 11:11:15 +00002544
2545 /* Make sure calling put and get on a buffer that hasn't been set up results
Shaun Case8b0ecbc2021-12-20 21:14:10 -08002546 * in error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002547 mbedtls_test_buffer_init(&buf);
Janos Follath6264e662019-11-26 11:11:15 +00002548
Gilles Peskine449bd832023-01-11 14:50:10 +01002549 TEST_ASSERT(mbedtls_test_buffer_put(&buf, input, sizeof(input)) == -1);
2550 TEST_ASSERT(mbedtls_test_buffer_get(&buf, output, sizeof(output))
2551 == -1);
2552 TEST_ASSERT(mbedtls_test_buffer_put(&buf, NULL, sizeof(input)) == -1);
Andrzej Kurekf7774142020-01-22 06:34:59 -05002553
Gilles Peskine449bd832023-01-11 14:50:10 +01002554 TEST_ASSERT(mbedtls_test_buffer_put(&buf, NULL, 0) == -1);
2555 TEST_ASSERT(mbedtls_test_buffer_get(&buf, NULL, 0) == -1);
Janos Follath6264e662019-11-26 11:11:15 +00002556
Andrzej Kurekf7774142020-01-22 06:34:59 -05002557 /* Make sure calling put and get on NULL input only results in
2558 * error if the length is not zero, and that a NULL output is valid for data
2559 * dropping.
2560 */
Janos Follath6264e662019-11-26 11:11:15 +00002561
Gilles Peskine449bd832023-01-11 14:50:10 +01002562 TEST_ASSERT(mbedtls_test_buffer_setup(&buf, sizeof(input)) == 0);
Janos Follath6264e662019-11-26 11:11:15 +00002563
Gilles Peskine449bd832023-01-11 14:50:10 +01002564 TEST_ASSERT(mbedtls_test_buffer_put(&buf, NULL, sizeof(input)) == -1);
2565 TEST_ASSERT(mbedtls_test_buffer_get(&buf, NULL, sizeof(output))
2566 == 0);
2567 TEST_ASSERT(mbedtls_test_buffer_put(&buf, NULL, 0) == 0);
2568 TEST_ASSERT(mbedtls_test_buffer_get(&buf, NULL, 0) == 0);
Janos Follath6264e662019-11-26 11:11:15 +00002569
Piotr Nowickifb437d72020-01-13 16:59:12 +01002570 /* Make sure calling put several times in the row is safe */
2571
Gilles Peskine449bd832023-01-11 14:50:10 +01002572 TEST_ASSERT(mbedtls_test_buffer_put(&buf, input, sizeof(input))
2573 == sizeof(input));
2574 TEST_ASSERT(mbedtls_test_buffer_get(&buf, output, 2) == 2);
2575 TEST_ASSERT(mbedtls_test_buffer_put(&buf, input, 1) == 1);
2576 TEST_ASSERT(mbedtls_test_buffer_put(&buf, input, 2) == 1);
2577 TEST_ASSERT(mbedtls_test_buffer_put(&buf, input, 2) == 0);
Piotr Nowickifb437d72020-01-13 16:59:12 +01002578
2579
Janos Follath6264e662019-11-26 11:11:15 +00002580exit:
2581
Gilles Peskine449bd832023-01-11 14:50:10 +01002582 mbedtls_test_buffer_free(&buf);
Janos Follath6264e662019-11-26 11:11:15 +00002583}
2584/* END_CASE */
2585
2586/*
2587 * Test if the implementation of `mbedtls_test_buffer` related functions is
2588 * correct and works as expected.
2589 *
2590 * That is
2591 * - If we try to put in \p put1 bytes then we can put in \p put1_ret bytes.
2592 * - Afterwards if we try to get \p get1 bytes then we can get \get1_ret bytes.
2593 * - Next, if we try to put in \p put1 bytes then we can put in \p put1_ret
2594 * bytes.
2595 * - Afterwards if we try to get \p get1 bytes then we can get \get1_ret bytes.
2596 * - All of the bytes we got match the bytes we put in in a FIFO manner.
2597 */
2598
2599/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002600void test_callback_buffer(int size, int put1, int put1_ret,
2601 int get1, int get1_ret, int put2, int put2_ret,
2602 int get2, int get2_ret)
Janos Follath6264e662019-11-26 11:11:15 +00002603{
2604 enum { ROUNDS = 2 };
2605 size_t put[ROUNDS];
2606 int put_ret[ROUNDS];
2607 size_t get[ROUNDS];
2608 int get_ret[ROUNDS];
2609 mbedtls_test_buffer buf;
Gilles Peskine449bd832023-01-11 14:50:10 +01002610 unsigned char *input = NULL;
Janos Follath6264e662019-11-26 11:11:15 +00002611 size_t input_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002612 unsigned char *output = NULL;
Janos Follath6264e662019-11-26 11:11:15 +00002613 size_t output_len;
Janos Follath031827f2019-11-27 11:12:14 +00002614 size_t i, j, written, read;
Janos Follath6264e662019-11-26 11:11:15 +00002615
Gilles Peskine449bd832023-01-11 14:50:10 +01002616 mbedtls_test_buffer_init(&buf);
2617 TEST_ASSERT(mbedtls_test_buffer_setup(&buf, size) == 0);
Janos Follath6264e662019-11-26 11:11:15 +00002618
2619 /* Check the sanity of input parameters and initialise local variables. That
2620 * is, ensure that the amount of data is not negative and that we are not
2621 * expecting more to put or get than we actually asked for. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002622 TEST_ASSERT(put1 >= 0);
Janos Follath6264e662019-11-26 11:11:15 +00002623 put[0] = put1;
2624 put_ret[0] = put1_ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01002625 TEST_ASSERT(put1_ret <= put1);
2626 TEST_ASSERT(put2 >= 0);
Janos Follath6264e662019-11-26 11:11:15 +00002627 put[1] = put2;
2628 put_ret[1] = put2_ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01002629 TEST_ASSERT(put2_ret <= put2);
Janos Follath6264e662019-11-26 11:11:15 +00002630
Gilles Peskine449bd832023-01-11 14:50:10 +01002631 TEST_ASSERT(get1 >= 0);
Janos Follath6264e662019-11-26 11:11:15 +00002632 get[0] = get1;
2633 get_ret[0] = get1_ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01002634 TEST_ASSERT(get1_ret <= get1);
2635 TEST_ASSERT(get2 >= 0);
Janos Follath6264e662019-11-26 11:11:15 +00002636 get[1] = get2;
2637 get_ret[1] = get2_ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01002638 TEST_ASSERT(get2_ret <= get2);
Janos Follath6264e662019-11-26 11:11:15 +00002639
2640 input_len = 0;
2641 /* Calculate actual input and output lengths */
Gilles Peskine449bd832023-01-11 14:50:10 +01002642 for (j = 0; j < ROUNDS; j++) {
2643 if (put_ret[j] > 0) {
Janos Follath6264e662019-11-26 11:11:15 +00002644 input_len += put_ret[j];
2645 }
2646 }
2647 /* In order to always have a valid pointer we always allocate at least 1
2648 * byte. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002649 if (input_len == 0) {
Janos Follath6264e662019-11-26 11:11:15 +00002650 input_len = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01002651 }
2652 ASSERT_ALLOC(input, input_len);
Janos Follath6264e662019-11-26 11:11:15 +00002653
2654 output_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002655 for (j = 0; j < ROUNDS; j++) {
2656 if (get_ret[j] > 0) {
Janos Follath6264e662019-11-26 11:11:15 +00002657 output_len += get_ret[j];
2658 }
2659 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002660 TEST_ASSERT(output_len <= input_len);
Janos Follath6264e662019-11-26 11:11:15 +00002661 /* In order to always have a valid pointer we always allocate at least 1
2662 * byte. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002663 if (output_len == 0) {
Janos Follath6264e662019-11-26 11:11:15 +00002664 output_len = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01002665 }
2666 ASSERT_ALLOC(output, output_len);
Janos Follath6264e662019-11-26 11:11:15 +00002667
2668 /* Fill up the buffer with structured data so that unwanted changes
2669 * can be detected */
Gilles Peskine449bd832023-01-11 14:50:10 +01002670 for (i = 0; i < input_len; i++) {
Janos Follath6264e662019-11-26 11:11:15 +00002671 input[i] = i & 0xFF;
2672 }
2673
2674 written = read = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002675 for (j = 0; j < ROUNDS; j++) {
2676 TEST_ASSERT(put_ret[j] == mbedtls_test_buffer_put(&buf,
2677 input + written, put[j]));
Janos Follath6264e662019-11-26 11:11:15 +00002678 written += put_ret[j];
Gilles Peskine449bd832023-01-11 14:50:10 +01002679 TEST_ASSERT(get_ret[j] == mbedtls_test_buffer_get(&buf,
2680 output + read, get[j]));
Janos Follath6264e662019-11-26 11:11:15 +00002681 read += get_ret[j];
Gilles Peskine449bd832023-01-11 14:50:10 +01002682 TEST_ASSERT(read <= written);
2683 if (get_ret[j] > 0) {
2684 TEST_ASSERT(memcmp(output + read - get_ret[j],
2685 input + read - get_ret[j], get_ret[j])
2686 == 0);
Janos Follath6264e662019-11-26 11:11:15 +00002687 }
2688 }
2689
2690exit:
2691
Gilles Peskine449bd832023-01-11 14:50:10 +01002692 mbedtls_free(input);
2693 mbedtls_free(output);
2694 mbedtls_test_buffer_free(&buf);
Janos Follath6264e662019-11-26 11:11:15 +00002695}
2696/* END_CASE */
2697
Janos Follath031827f2019-11-27 11:12:14 +00002698/*
Janos Follathc673c2c2019-12-02 15:47:26 +00002699 * Test if the implementation of `mbedtls_mock_socket` related I/O functions is
2700 * correct and works as expected on unconnected sockets.
2701 */
2702
2703/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002704void ssl_mock_sanity()
Janos Follathc673c2c2019-12-02 15:47:26 +00002705{
2706 enum { MSGLEN = 105 };
Paul Elliott21c8fe52021-11-24 16:54:26 +00002707 unsigned char message[MSGLEN] = { 0 };
2708 unsigned char received[MSGLEN] = { 0 };
Janos Follathc673c2c2019-12-02 15:47:26 +00002709 mbedtls_mock_socket socket;
2710
Gilles Peskine449bd832023-01-11 14:50:10 +01002711 mbedtls_mock_socket_init(&socket);
2712 TEST_ASSERT(mbedtls_mock_tcp_send_b(&socket, message, MSGLEN) < 0);
2713 mbedtls_mock_socket_close(&socket);
2714 mbedtls_mock_socket_init(&socket);
2715 TEST_ASSERT(mbedtls_mock_tcp_recv_b(&socket, received, MSGLEN) < 0);
2716 mbedtls_mock_socket_close(&socket);
Janos Follathc673c2c2019-12-02 15:47:26 +00002717
Gilles Peskine449bd832023-01-11 14:50:10 +01002718 mbedtls_mock_socket_init(&socket);
2719 TEST_ASSERT(mbedtls_mock_tcp_send_nb(&socket, message, MSGLEN) < 0);
2720 mbedtls_mock_socket_close(&socket);
2721 mbedtls_mock_socket_init(&socket);
2722 TEST_ASSERT(mbedtls_mock_tcp_recv_nb(&socket, received, MSGLEN) < 0);
2723 mbedtls_mock_socket_close(&socket);
Janos Follathc673c2c2019-12-02 15:47:26 +00002724
2725exit:
2726
Gilles Peskine449bd832023-01-11 14:50:10 +01002727 mbedtls_mock_socket_close(&socket);
Janos Follathc673c2c2019-12-02 15:47:26 +00002728}
2729/* END_CASE */
2730
2731/*
2732 * Test if the implementation of `mbedtls_mock_socket` related functions can
2733 * send a single message from the client to the server.
Janos Follath031827f2019-11-27 11:12:14 +00002734 */
2735
2736/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002737void ssl_mock_tcp(int blocking)
Janos Follath031827f2019-11-27 11:12:14 +00002738{
Janos Follathc673c2c2019-12-02 15:47:26 +00002739 enum { MSGLEN = 105 };
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002740 enum { BUFLEN = MSGLEN / 5 };
Janos Follathc673c2c2019-12-02 15:47:26 +00002741 unsigned char message[MSGLEN];
2742 unsigned char received[MSGLEN];
2743 mbedtls_mock_socket client;
2744 mbedtls_mock_socket server;
2745 size_t written, read;
2746 int send_ret, recv_ret;
2747 mbedtls_ssl_send_t *send;
2748 mbedtls_ssl_recv_t *recv;
Janos Follathc673c2c2019-12-02 15:47:26 +00002749 unsigned i;
2750
Gilles Peskine449bd832023-01-11 14:50:10 +01002751 if (blocking == 0) {
Janos Follathc673c2c2019-12-02 15:47:26 +00002752 send = mbedtls_mock_tcp_send_nb;
2753 recv = mbedtls_mock_tcp_recv_nb;
Gilles Peskine449bd832023-01-11 14:50:10 +01002754 } else {
Janos Follathc673c2c2019-12-02 15:47:26 +00002755 send = mbedtls_mock_tcp_send_b;
2756 recv = mbedtls_mock_tcp_recv_b;
2757 }
2758
Gilles Peskine449bd832023-01-11 14:50:10 +01002759 mbedtls_mock_socket_init(&client);
2760 mbedtls_mock_socket_init(&server);
Janos Follathc673c2c2019-12-02 15:47:26 +00002761
2762 /* Fill up the buffer with structured data so that unwanted changes
2763 * can be detected */
Gilles Peskine449bd832023-01-11 14:50:10 +01002764 for (i = 0; i < MSGLEN; i++) {
Janos Follathc673c2c2019-12-02 15:47:26 +00002765 message[i] = i & 0xFF;
2766 }
2767
2768 /* Make sure that sending a message takes a few iterations. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002769 TEST_ASSERT(0 == mbedtls_mock_socket_connect(&client, &server, BUFLEN));
Janos Follathc673c2c2019-12-02 15:47:26 +00002770
2771 /* Send the message to the server */
2772 send_ret = recv_ret = 1;
2773 written = read = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002774 while (send_ret != 0 || recv_ret != 0) {
2775 send_ret = send(&client, message + written, MSGLEN - written);
Janos Follathc673c2c2019-12-02 15:47:26 +00002776
Gilles Peskine449bd832023-01-11 14:50:10 +01002777 TEST_ASSERT(send_ret >= 0);
2778 TEST_ASSERT(send_ret <= BUFLEN);
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002779 written += send_ret;
2780
2781 /* If the buffer is full we can test blocking and non-blocking send */
Gilles Peskine449bd832023-01-11 14:50:10 +01002782 if (send_ret == BUFLEN) {
2783 int blocking_ret = send(&client, message, 1);
2784 if (blocking) {
2785 TEST_ASSERT(blocking_ret == 0);
2786 } else {
2787 TEST_ASSERT(blocking_ret == MBEDTLS_ERR_SSL_WANT_WRITE);
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002788 }
Janos Follathc673c2c2019-12-02 15:47:26 +00002789 }
Janos Follathc673c2c2019-12-02 15:47:26 +00002790
Gilles Peskine449bd832023-01-11 14:50:10 +01002791 recv_ret = recv(&server, received + read, MSGLEN - read);
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002792
2793 /* The result depends on whether any data was sent */
Gilles Peskine449bd832023-01-11 14:50:10 +01002794 if (send_ret > 0) {
2795 TEST_ASSERT(recv_ret > 0);
2796 TEST_ASSERT(recv_ret <= BUFLEN);
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002797 read += recv_ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01002798 } else if (blocking) {
2799 TEST_ASSERT(recv_ret == 0);
2800 } else {
2801 TEST_ASSERT(recv_ret == MBEDTLS_ERR_SSL_WANT_READ);
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002802 recv_ret = 0;
Janos Follathc673c2c2019-12-02 15:47:26 +00002803 }
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002804
2805 /* If the buffer is empty we can test blocking and non-blocking read */
Gilles Peskine449bd832023-01-11 14:50:10 +01002806 if (recv_ret == BUFLEN) {
2807 int blocking_ret = recv(&server, received, 1);
2808 if (blocking) {
2809 TEST_ASSERT(blocking_ret == 0);
2810 } else {
2811 TEST_ASSERT(blocking_ret == MBEDTLS_ERR_SSL_WANT_READ);
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002812 }
2813 }
Janos Follathc673c2c2019-12-02 15:47:26 +00002814 }
Gilles Peskine449bd832023-01-11 14:50:10 +01002815 TEST_ASSERT(memcmp(message, received, MSGLEN) == 0);
Janos Follathc673c2c2019-12-02 15:47:26 +00002816
2817exit:
2818
Gilles Peskine449bd832023-01-11 14:50:10 +01002819 mbedtls_mock_socket_close(&client);
2820 mbedtls_mock_socket_close(&server);
Janos Follathc673c2c2019-12-02 15:47:26 +00002821}
2822/* END_CASE */
2823
2824/*
2825 * Test if the implementation of `mbedtls_mock_socket` related functions can
2826 * send messages in both direction at the same time (with the I/O calls
2827 * interleaving).
2828 */
2829
2830/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002831void ssl_mock_tcp_interleaving(int blocking)
Janos Follathc673c2c2019-12-02 15:47:26 +00002832{
Janos Follath031827f2019-11-27 11:12:14 +00002833 enum { ROUNDS = 2 };
2834 enum { MSGLEN = 105 };
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002835 enum { BUFLEN = MSGLEN / 5 };
Janos Follath031827f2019-11-27 11:12:14 +00002836 unsigned char message[ROUNDS][MSGLEN];
2837 unsigned char received[ROUNDS][MSGLEN];
2838 mbedtls_mock_socket client;
2839 mbedtls_mock_socket server;
2840 size_t written[ROUNDS];
2841 size_t read[ROUNDS];
2842 int send_ret[ROUNDS];
2843 int recv_ret[ROUNDS];
2844 unsigned i, j, progress;
Janos Follath3766ba52019-11-27 13:31:42 +00002845 mbedtls_ssl_send_t *send;
2846 mbedtls_ssl_recv_t *recv;
Janos Follath3766ba52019-11-27 13:31:42 +00002847
Gilles Peskine449bd832023-01-11 14:50:10 +01002848 if (blocking == 0) {
Janos Follath3766ba52019-11-27 13:31:42 +00002849 send = mbedtls_mock_tcp_send_nb;
2850 recv = mbedtls_mock_tcp_recv_nb;
Gilles Peskine449bd832023-01-11 14:50:10 +01002851 } else {
Janos Follath3766ba52019-11-27 13:31:42 +00002852 send = mbedtls_mock_tcp_send_b;
2853 recv = mbedtls_mock_tcp_recv_b;
2854 }
Janos Follath031827f2019-11-27 11:12:14 +00002855
Gilles Peskine449bd832023-01-11 14:50:10 +01002856 mbedtls_mock_socket_init(&client);
2857 mbedtls_mock_socket_init(&server);
Janos Follath031827f2019-11-27 11:12:14 +00002858
2859 /* Fill up the buffers with structured data so that unwanted changes
2860 * can be detected */
Gilles Peskine449bd832023-01-11 14:50:10 +01002861 for (i = 0; i < ROUNDS; i++) {
2862 for (j = 0; j < MSGLEN; j++) {
2863 message[i][j] = (i * MSGLEN + j) & 0xFF;
Janos Follath031827f2019-11-27 11:12:14 +00002864 }
2865 }
2866
Janos Follath031827f2019-11-27 11:12:14 +00002867 /* Make sure that sending a message takes a few iterations. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002868 TEST_ASSERT(0 == mbedtls_mock_socket_connect(&client, &server, BUFLEN));
Janos Follath031827f2019-11-27 11:12:14 +00002869
Janos Follath031827f2019-11-27 11:12:14 +00002870 /* Send the message from both sides, interleaving. */
2871 progress = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01002872 for (i = 0; i < ROUNDS; i++) {
Janos Follath031827f2019-11-27 11:12:14 +00002873 written[i] = 0;
2874 read[i] = 0;
2875 }
2876 /* This loop does not stop as long as there was a successful write or read
2877 * of at least one byte on either side. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002878 while (progress != 0) {
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002879 mbedtls_mock_socket *socket;
Janos Follath031827f2019-11-27 11:12:14 +00002880
Gilles Peskine449bd832023-01-11 14:50:10 +01002881 for (i = 0; i < ROUNDS; i++) {
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002882 /* First sending is from the client */
Gilles Peskine449bd832023-01-11 14:50:10 +01002883 socket = (i % 2 == 0) ? (&client) : (&server);
Janos Follath031827f2019-11-27 11:12:14 +00002884
Gilles Peskine449bd832023-01-11 14:50:10 +01002885 send_ret[i] = send(socket, message[i] + written[i],
2886 MSGLEN - written[i]);
2887 TEST_ASSERT(send_ret[i] >= 0);
2888 TEST_ASSERT(send_ret[i] <= BUFLEN);
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002889 written[i] += send_ret[i];
Janos Follath031827f2019-11-27 11:12:14 +00002890
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002891 /* If the buffer is full we can test blocking and non-blocking
2892 * send */
Gilles Peskine449bd832023-01-11 14:50:10 +01002893 if (send_ret[i] == BUFLEN) {
2894 int blocking_ret = send(socket, message[i], 1);
2895 if (blocking) {
2896 TEST_ASSERT(blocking_ret == 0);
2897 } else {
2898 TEST_ASSERT(blocking_ret == MBEDTLS_ERR_SSL_WANT_WRITE);
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002899 }
2900 }
Janos Follath3766ba52019-11-27 13:31:42 +00002901 }
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002902
Gilles Peskine449bd832023-01-11 14:50:10 +01002903 for (i = 0; i < ROUNDS; i++) {
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002904 /* First receiving is from the server */
Gilles Peskine449bd832023-01-11 14:50:10 +01002905 socket = (i % 2 == 0) ? (&server) : (&client);
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002906
Gilles Peskine449bd832023-01-11 14:50:10 +01002907 recv_ret[i] = recv(socket, received[i] + read[i],
2908 MSGLEN - read[i]);
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002909
2910 /* The result depends on whether any data was sent */
Gilles Peskine449bd832023-01-11 14:50:10 +01002911 if (send_ret[i] > 0) {
2912 TEST_ASSERT(recv_ret[i] > 0);
2913 TEST_ASSERT(recv_ret[i] <= BUFLEN);
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002914 read[i] += recv_ret[i];
Gilles Peskine449bd832023-01-11 14:50:10 +01002915 } else if (blocking) {
2916 TEST_ASSERT(recv_ret[i] == 0);
2917 } else {
2918 TEST_ASSERT(recv_ret[i] == MBEDTLS_ERR_SSL_WANT_READ);
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002919 recv_ret[i] = 0;
2920 }
2921
2922 /* If the buffer is empty we can test blocking and non-blocking
2923 * read */
Gilles Peskine449bd832023-01-11 14:50:10 +01002924 if (recv_ret[i] == BUFLEN) {
2925 int blocking_ret = recv(socket, received[i], 1);
2926 if (blocking) {
2927 TEST_ASSERT(blocking_ret == 0);
2928 } else {
2929 TEST_ASSERT(blocking_ret == MBEDTLS_ERR_SSL_WANT_READ);
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002930 }
2931 }
Janos Follath3766ba52019-11-27 13:31:42 +00002932 }
Janos Follath031827f2019-11-27 11:12:14 +00002933
2934 progress = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002935 for (i = 0; i < ROUNDS; i++) {
Piotr Nowicki890b5ca2020-01-15 16:19:07 +01002936 progress += send_ret[i] + recv_ret[i];
Janos Follath031827f2019-11-27 11:12:14 +00002937 }
2938 }
2939
Gilles Peskine449bd832023-01-11 14:50:10 +01002940 for (i = 0; i < ROUNDS; i++) {
2941 TEST_ASSERT(memcmp(message[i], received[i], MSGLEN) == 0);
2942 }
Janos Follath031827f2019-11-27 11:12:14 +00002943
2944exit:
2945
Gilles Peskine449bd832023-01-11 14:50:10 +01002946 mbedtls_mock_socket_close(&client);
2947 mbedtls_mock_socket_close(&server);
Janos Follath031827f2019-11-27 11:12:14 +00002948}
2949/* END_CASE */
2950
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002951/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002952void ssl_message_queue_sanity()
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002953{
2954 mbedtls_test_message_queue queue;
2955
2956 /* Trying to push/pull to an empty queue */
Gilles Peskine449bd832023-01-11 14:50:10 +01002957 TEST_ASSERT(mbedtls_test_message_queue_push_info(NULL, 1)
2958 == MBEDTLS_TEST_ERROR_ARG_NULL);
2959 TEST_ASSERT(mbedtls_test_message_queue_pop_info(NULL, 1)
2960 == MBEDTLS_TEST_ERROR_ARG_NULL);
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002961
Gilles Peskine449bd832023-01-11 14:50:10 +01002962 TEST_ASSERT(mbedtls_test_message_queue_setup(&queue, 3) == 0);
2963 TEST_ASSERT(queue.capacity == 3);
2964 TEST_ASSERT(queue.num == 0);
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002965
2966exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002967 mbedtls_test_message_queue_free(&queue);
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002968}
2969/* END_CASE */
2970
2971/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002972void ssl_message_queue_basic()
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002973{
2974 mbedtls_test_message_queue queue;
2975
Gilles Peskine449bd832023-01-11 14:50:10 +01002976 TEST_ASSERT(mbedtls_test_message_queue_setup(&queue, 3) == 0);
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002977
2978 /* Sanity test - 3 pushes and 3 pops with sufficient space */
Gilles Peskine449bd832023-01-11 14:50:10 +01002979 TEST_ASSERT(mbedtls_test_message_queue_push_info(&queue, 1) == 1);
2980 TEST_ASSERT(queue.capacity == 3);
2981 TEST_ASSERT(queue.num == 1);
2982 TEST_ASSERT(mbedtls_test_message_queue_push_info(&queue, 1) == 1);
2983 TEST_ASSERT(queue.capacity == 3);
2984 TEST_ASSERT(queue.num == 2);
2985 TEST_ASSERT(mbedtls_test_message_queue_push_info(&queue, 2) == 2);
2986 TEST_ASSERT(queue.capacity == 3);
2987 TEST_ASSERT(queue.num == 3);
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002988
Gilles Peskine449bd832023-01-11 14:50:10 +01002989 TEST_ASSERT(mbedtls_test_message_queue_pop_info(&queue, 1) == 1);
2990 TEST_ASSERT(mbedtls_test_message_queue_pop_info(&queue, 1) == 1);
2991 TEST_ASSERT(mbedtls_test_message_queue_pop_info(&queue, 2) == 2);
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002992
2993exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01002994 mbedtls_test_message_queue_free(&queue);
Andrzej Kurek13719cd2020-01-22 06:36:39 -05002995}
2996/* END_CASE */
2997
2998/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01002999void ssl_message_queue_overflow_underflow()
Andrzej Kurek13719cd2020-01-22 06:36:39 -05003000{
3001 mbedtls_test_message_queue queue;
3002
Gilles Peskine449bd832023-01-11 14:50:10 +01003003 TEST_ASSERT(mbedtls_test_message_queue_setup(&queue, 3) == 0);
Andrzej Kurek13719cd2020-01-22 06:36:39 -05003004
3005 /* 4 pushes (last one with an error), 4 pops (last one with an error) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003006 TEST_ASSERT(mbedtls_test_message_queue_push_info(&queue, 1) == 1);
3007 TEST_ASSERT(mbedtls_test_message_queue_push_info(&queue, 1) == 1);
3008 TEST_ASSERT(mbedtls_test_message_queue_push_info(&queue, 2) == 2);
3009 TEST_ASSERT(mbedtls_test_message_queue_push_info(&queue, 3)
3010 == MBEDTLS_ERR_SSL_WANT_WRITE);
Andrzej Kurek13719cd2020-01-22 06:36:39 -05003011
Gilles Peskine449bd832023-01-11 14:50:10 +01003012 TEST_ASSERT(mbedtls_test_message_queue_pop_info(&queue, 1) == 1);
3013 TEST_ASSERT(mbedtls_test_message_queue_pop_info(&queue, 1) == 1);
3014 TEST_ASSERT(mbedtls_test_message_queue_pop_info(&queue, 2) == 2);
Andrzej Kurek13719cd2020-01-22 06:36:39 -05003015
Gilles Peskine449bd832023-01-11 14:50:10 +01003016 TEST_ASSERT(mbedtls_test_message_queue_pop_info(&queue, 1)
3017 == MBEDTLS_ERR_SSL_WANT_READ);
Andrzej Kurek13719cd2020-01-22 06:36:39 -05003018
3019exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003020 mbedtls_test_message_queue_free(&queue);
Andrzej Kurek13719cd2020-01-22 06:36:39 -05003021}
3022/* END_CASE */
3023
3024/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003025void ssl_message_queue_interleaved()
Andrzej Kurek13719cd2020-01-22 06:36:39 -05003026{
3027 mbedtls_test_message_queue queue;
3028
Gilles Peskine449bd832023-01-11 14:50:10 +01003029 TEST_ASSERT(mbedtls_test_message_queue_setup(&queue, 3) == 0);
Andrzej Kurek13719cd2020-01-22 06:36:39 -05003030
3031 /* Interleaved test - [2 pushes, 1 pop] twice, and then two pops
3032 * (to wrap around the buffer) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003033 TEST_ASSERT(mbedtls_test_message_queue_push_info(&queue, 1) == 1);
3034 TEST_ASSERT(mbedtls_test_message_queue_push_info(&queue, 1) == 1);
Andrzej Kurek13719cd2020-01-22 06:36:39 -05003035
Gilles Peskine449bd832023-01-11 14:50:10 +01003036 TEST_ASSERT(mbedtls_test_message_queue_pop_info(&queue, 1) == 1);
Andrzej Kurek13719cd2020-01-22 06:36:39 -05003037
Gilles Peskine449bd832023-01-11 14:50:10 +01003038 TEST_ASSERT(mbedtls_test_message_queue_push_info(&queue, 2) == 2);
3039 TEST_ASSERT(mbedtls_test_message_queue_push_info(&queue, 3) == 3);
Andrzej Kurek13719cd2020-01-22 06:36:39 -05003040
Gilles Peskine449bd832023-01-11 14:50:10 +01003041 TEST_ASSERT(mbedtls_test_message_queue_pop_info(&queue, 1) == 1);
3042 TEST_ASSERT(mbedtls_test_message_queue_pop_info(&queue, 2) == 2);
Andrzej Kurek13719cd2020-01-22 06:36:39 -05003043
Gilles Peskine449bd832023-01-11 14:50:10 +01003044 TEST_ASSERT(mbedtls_test_message_queue_push_info(&queue, 5) == 5);
3045 TEST_ASSERT(mbedtls_test_message_queue_push_info(&queue, 8) == 8);
Andrzej Kurek13719cd2020-01-22 06:36:39 -05003046
Gilles Peskine449bd832023-01-11 14:50:10 +01003047 TEST_ASSERT(mbedtls_test_message_queue_pop_info(&queue, 3) == 3);
Andrzej Kurek13719cd2020-01-22 06:36:39 -05003048
Gilles Peskine449bd832023-01-11 14:50:10 +01003049 TEST_ASSERT(mbedtls_test_message_queue_pop_info(&queue, 5) == 5);
Andrzej Kurek13719cd2020-01-22 06:36:39 -05003050
Gilles Peskine449bd832023-01-11 14:50:10 +01003051 TEST_ASSERT(mbedtls_test_message_queue_pop_info(&queue, 8) == 8);
Andrzej Kurek13719cd2020-01-22 06:36:39 -05003052
3053exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003054 mbedtls_test_message_queue_free(&queue);
Andrzej Kurek13719cd2020-01-22 06:36:39 -05003055}
3056/* END_CASE */
3057
3058/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003059void ssl_message_queue_insufficient_buffer()
Andrzej Kurek13719cd2020-01-22 06:36:39 -05003060{
3061 mbedtls_test_message_queue queue;
3062 size_t message_len = 10;
3063 size_t buffer_len = 5;
3064
Gilles Peskine449bd832023-01-11 14:50:10 +01003065 TEST_ASSERT(mbedtls_test_message_queue_setup(&queue, 1) == 0);
Andrzej Kurek13719cd2020-01-22 06:36:39 -05003066
3067 /* Popping without a sufficient buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01003068 TEST_ASSERT(mbedtls_test_message_queue_push_info(&queue, message_len)
3069 == (int) message_len);
3070 TEST_ASSERT(mbedtls_test_message_queue_pop_info(&queue, buffer_len)
3071 == (int) buffer_len);
Andrzej Kurek13719cd2020-01-22 06:36:39 -05003072exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01003073 mbedtls_test_message_queue_free(&queue);
Andrzej Kurek13719cd2020-01-22 06:36:39 -05003074}
3075/* END_CASE */
3076
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003077/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003078void ssl_message_mock_uninitialized()
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003079{
3080 enum { MSGLEN = 10 };
Gilles Peskine449bd832023-01-11 14:50:10 +01003081 unsigned char message[MSGLEN] = { 0 }, received[MSGLEN];
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003082 mbedtls_mock_socket client, server;
3083 mbedtls_test_message_queue server_queue, client_queue;
3084 mbedtls_test_message_socket_context server_context, client_context;
Gilles Peskine449bd832023-01-11 14:50:10 +01003085 mbedtls_message_socket_init(&server_context);
3086 mbedtls_message_socket_init(&client_context);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003087
3088 /* Send with a NULL context */
Gilles Peskine449bd832023-01-11 14:50:10 +01003089 TEST_ASSERT(mbedtls_mock_tcp_send_msg(NULL, message, MSGLEN)
3090 == MBEDTLS_TEST_ERROR_CONTEXT_ERROR);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003091
Gilles Peskine449bd832023-01-11 14:50:10 +01003092 TEST_ASSERT(mbedtls_mock_tcp_recv_msg(NULL, message, MSGLEN)
3093 == MBEDTLS_TEST_ERROR_CONTEXT_ERROR);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003094
Gilles Peskine449bd832023-01-11 14:50:10 +01003095 TEST_ASSERT(mbedtls_message_socket_setup(&server_queue, &client_queue, 1,
3096 &server,
3097 &server_context) == 0);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003098
Gilles Peskine449bd832023-01-11 14:50:10 +01003099 TEST_ASSERT(mbedtls_message_socket_setup(&client_queue, &server_queue, 1,
3100 &client,
3101 &client_context) == 0);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003102
Gilles Peskine449bd832023-01-11 14:50:10 +01003103 TEST_ASSERT(mbedtls_mock_tcp_send_msg(&client_context, message, MSGLEN)
3104 == MBEDTLS_TEST_ERROR_SEND_FAILED);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003105
Gilles Peskine449bd832023-01-11 14:50:10 +01003106 TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&server_context, received, MSGLEN)
3107 == MBEDTLS_ERR_SSL_WANT_READ);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003108
3109 /* Push directly to a queue to later simulate a disconnected behavior */
Gilles Peskine449bd832023-01-11 14:50:10 +01003110 TEST_ASSERT(mbedtls_test_message_queue_push_info(&server_queue, MSGLEN)
3111 == MSGLEN);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003112
3113 /* Test if there's an error when trying to read from a disconnected
3114 * socket */
Gilles Peskine449bd832023-01-11 14:50:10 +01003115 TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&server_context, received, MSGLEN)
3116 == MBEDTLS_TEST_ERROR_RECV_FAILED);
3117exit:
3118 mbedtls_message_socket_close(&server_context);
3119 mbedtls_message_socket_close(&client_context);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003120}
3121/* END_CASE */
3122
3123/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003124void ssl_message_mock_basic()
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003125{
3126 enum { MSGLEN = 10 };
3127 unsigned char message[MSGLEN], received[MSGLEN];
3128 mbedtls_mock_socket client, server;
3129 unsigned i;
3130 mbedtls_test_message_queue server_queue, client_queue;
3131 mbedtls_test_message_socket_context server_context, client_context;
Gilles Peskine449bd832023-01-11 14:50:10 +01003132 mbedtls_message_socket_init(&server_context);
3133 mbedtls_message_socket_init(&client_context);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003134
Gilles Peskine449bd832023-01-11 14:50:10 +01003135 TEST_ASSERT(mbedtls_message_socket_setup(&server_queue, &client_queue, 1,
3136 &server,
3137 &server_context) == 0);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003138
Gilles Peskine449bd832023-01-11 14:50:10 +01003139 TEST_ASSERT(mbedtls_message_socket_setup(&client_queue, &server_queue, 1,
3140 &client,
3141 &client_context) == 0);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003142
3143 /* Fill up the buffer with structured data so that unwanted changes
3144 * can be detected */
Gilles Peskine449bd832023-01-11 14:50:10 +01003145 for (i = 0; i < MSGLEN; i++) {
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003146 message[i] = i & 0xFF;
3147 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003148 TEST_ASSERT(0 == mbedtls_mock_socket_connect(&client, &server,
3149 MSGLEN));
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003150
3151 /* Send the message to the server */
Gilles Peskine449bd832023-01-11 14:50:10 +01003152 TEST_ASSERT(mbedtls_mock_tcp_send_msg(&client_context, message,
3153 MSGLEN) == MSGLEN);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003154
3155 /* Read from the server */
Gilles Peskine449bd832023-01-11 14:50:10 +01003156 TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&server_context, received, MSGLEN)
3157 == MSGLEN);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003158
Gilles Peskine449bd832023-01-11 14:50:10 +01003159 TEST_ASSERT(memcmp(message, received, MSGLEN) == 0);
3160 memset(received, 0, MSGLEN);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003161
3162 /* Send the message to the client */
Gilles Peskine449bd832023-01-11 14:50:10 +01003163 TEST_ASSERT(mbedtls_mock_tcp_send_msg(&server_context, message,
3164 MSGLEN) == MSGLEN);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003165
3166 /* Read from the client */
Gilles Peskine449bd832023-01-11 14:50:10 +01003167 TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&client_context, received, MSGLEN)
3168 == MSGLEN);
3169 TEST_ASSERT(memcmp(message, received, MSGLEN) == 0);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003170
Gilles Peskine449bd832023-01-11 14:50:10 +01003171exit:
3172 mbedtls_message_socket_close(&server_context);
3173 mbedtls_message_socket_close(&client_context);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003174}
3175/* END_CASE */
3176
3177/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003178void ssl_message_mock_queue_overflow_underflow()
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003179{
3180 enum { MSGLEN = 10 };
3181 unsigned char message[MSGLEN], received[MSGLEN];
3182 mbedtls_mock_socket client, server;
3183 unsigned i;
3184 mbedtls_test_message_queue server_queue, client_queue;
3185 mbedtls_test_message_socket_context server_context, client_context;
Gilles Peskine449bd832023-01-11 14:50:10 +01003186 mbedtls_message_socket_init(&server_context);
3187 mbedtls_message_socket_init(&client_context);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003188
Gilles Peskine449bd832023-01-11 14:50:10 +01003189 TEST_ASSERT(mbedtls_message_socket_setup(&server_queue, &client_queue, 2,
3190 &server,
3191 &server_context) == 0);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003192
Gilles Peskine449bd832023-01-11 14:50:10 +01003193 TEST_ASSERT(mbedtls_message_socket_setup(&client_queue, &server_queue, 2,
3194 &client,
3195 &client_context) == 0);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003196
3197 /* Fill up the buffer with structured data so that unwanted changes
3198 * can be detected */
Gilles Peskine449bd832023-01-11 14:50:10 +01003199 for (i = 0; i < MSGLEN; i++) {
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003200 message[i] = i & 0xFF;
3201 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003202 TEST_ASSERT(0 == mbedtls_mock_socket_connect(&client, &server,
3203 MSGLEN*2));
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003204
3205 /* Send three message to the server, last one with an error */
Gilles Peskine449bd832023-01-11 14:50:10 +01003206 TEST_ASSERT(mbedtls_mock_tcp_send_msg(&client_context, message,
3207 MSGLEN - 1) == MSGLEN - 1);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003208
Gilles Peskine449bd832023-01-11 14:50:10 +01003209 TEST_ASSERT(mbedtls_mock_tcp_send_msg(&client_context, message,
3210 MSGLEN) == MSGLEN);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003211
Gilles Peskine449bd832023-01-11 14:50:10 +01003212 TEST_ASSERT(mbedtls_mock_tcp_send_msg(&client_context, message,
3213 MSGLEN)
3214 == MBEDTLS_ERR_SSL_WANT_WRITE);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003215
3216 /* Read three messages from the server, last one with an error */
Gilles Peskine449bd832023-01-11 14:50:10 +01003217 TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&server_context, received,
3218 MSGLEN - 1) == MSGLEN - 1);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003219
Gilles Peskine449bd832023-01-11 14:50:10 +01003220 TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&server_context, received, MSGLEN)
3221 == MSGLEN);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003222
Gilles Peskine449bd832023-01-11 14:50:10 +01003223 TEST_ASSERT(memcmp(message, received, MSGLEN) == 0);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003224
Gilles Peskine449bd832023-01-11 14:50:10 +01003225 TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&server_context, received, MSGLEN)
3226 == MBEDTLS_ERR_SSL_WANT_READ);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003227
Gilles Peskine449bd832023-01-11 14:50:10 +01003228exit:
3229 mbedtls_message_socket_close(&server_context);
3230 mbedtls_message_socket_close(&client_context);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003231}
3232/* END_CASE */
3233
3234/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003235void ssl_message_mock_socket_overflow()
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003236{
3237 enum { MSGLEN = 10 };
3238 unsigned char message[MSGLEN], received[MSGLEN];
3239 mbedtls_mock_socket client, server;
3240 unsigned i;
3241 mbedtls_test_message_queue server_queue, client_queue;
3242 mbedtls_test_message_socket_context server_context, client_context;
Gilles Peskine449bd832023-01-11 14:50:10 +01003243 mbedtls_message_socket_init(&server_context);
3244 mbedtls_message_socket_init(&client_context);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003245
Gilles Peskine449bd832023-01-11 14:50:10 +01003246 TEST_ASSERT(mbedtls_message_socket_setup(&server_queue, &client_queue, 2,
3247 &server,
3248 &server_context) == 0);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003249
Gilles Peskine449bd832023-01-11 14:50:10 +01003250 TEST_ASSERT(mbedtls_message_socket_setup(&client_queue, &server_queue, 2,
3251 &client,
3252 &client_context) == 0);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003253
3254 /* Fill up the buffer with structured data so that unwanted changes
3255 * can be detected */
Gilles Peskine449bd832023-01-11 14:50:10 +01003256 for (i = 0; i < MSGLEN; i++) {
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003257 message[i] = i & 0xFF;
3258 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003259 TEST_ASSERT(0 == mbedtls_mock_socket_connect(&client, &server,
3260 MSGLEN));
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003261
3262 /* Send two message to the server, second one with an error */
Gilles Peskine449bd832023-01-11 14:50:10 +01003263 TEST_ASSERT(mbedtls_mock_tcp_send_msg(&client_context, message,
3264 MSGLEN) == MSGLEN);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003265
Gilles Peskine449bd832023-01-11 14:50:10 +01003266 TEST_ASSERT(mbedtls_mock_tcp_send_msg(&client_context, message,
3267 MSGLEN)
3268 == MBEDTLS_TEST_ERROR_SEND_FAILED);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003269
3270 /* Read the only message from the server */
Gilles Peskine449bd832023-01-11 14:50:10 +01003271 TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&server_context, received, MSGLEN)
3272 == MSGLEN);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003273
Gilles Peskine449bd832023-01-11 14:50:10 +01003274 TEST_ASSERT(memcmp(message, received, MSGLEN) == 0);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003275
Gilles Peskine449bd832023-01-11 14:50:10 +01003276exit:
3277 mbedtls_message_socket_close(&server_context);
3278 mbedtls_message_socket_close(&client_context);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003279}
3280/* END_CASE */
3281
3282/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003283void ssl_message_mock_truncated()
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003284{
3285 enum { MSGLEN = 10 };
3286 unsigned char message[MSGLEN], received[MSGLEN];
3287 mbedtls_mock_socket client, server;
3288 unsigned i;
3289 mbedtls_test_message_queue server_queue, client_queue;
3290 mbedtls_test_message_socket_context server_context, client_context;
Gilles Peskine449bd832023-01-11 14:50:10 +01003291 mbedtls_message_socket_init(&server_context);
3292 mbedtls_message_socket_init(&client_context);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003293
Gilles Peskine449bd832023-01-11 14:50:10 +01003294 TEST_ASSERT(mbedtls_message_socket_setup(&server_queue, &client_queue, 2,
3295 &server,
3296 &server_context) == 0);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003297
Gilles Peskine449bd832023-01-11 14:50:10 +01003298 TEST_ASSERT(mbedtls_message_socket_setup(&client_queue, &server_queue, 2,
3299 &client,
3300 &client_context) == 0);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003301
Gilles Peskine449bd832023-01-11 14:50:10 +01003302 memset(received, 0, MSGLEN);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003303 /* Fill up the buffer with structured data so that unwanted changes
3304 * can be detected */
Gilles Peskine449bd832023-01-11 14:50:10 +01003305 for (i = 0; i < MSGLEN; i++) {
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003306 message[i] = i & 0xFF;
3307 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003308 TEST_ASSERT(0 == mbedtls_mock_socket_connect(&client, &server,
3309 2 * MSGLEN));
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003310
3311 /* Send two messages to the server, the second one small enough to fit in the
3312 * receiver's buffer. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003313 TEST_ASSERT(mbedtls_mock_tcp_send_msg(&client_context, message,
3314 MSGLEN) == MSGLEN);
3315 TEST_ASSERT(mbedtls_mock_tcp_send_msg(&client_context, message,
3316 MSGLEN / 2) == MSGLEN / 2);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003317 /* Read a truncated message from the server */
Gilles Peskine449bd832023-01-11 14:50:10 +01003318 TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&server_context, received, MSGLEN/2)
3319 == MSGLEN/2);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003320
3321 /* Test that the first half of the message is valid, and second one isn't */
Gilles Peskine449bd832023-01-11 14:50:10 +01003322 TEST_ASSERT(memcmp(message, received, MSGLEN/2) == 0);
3323 TEST_ASSERT(memcmp(message + MSGLEN/2, received + MSGLEN/2, MSGLEN/2)
3324 != 0);
3325 memset(received, 0, MSGLEN);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003326
3327 /* Read a full message from the server */
Gilles Peskine449bd832023-01-11 14:50:10 +01003328 TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&server_context, received, MSGLEN/2)
3329 == MSGLEN / 2);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003330
3331 /* Test that the first half of the message is valid */
Gilles Peskine449bd832023-01-11 14:50:10 +01003332 TEST_ASSERT(memcmp(message, received, MSGLEN/2) == 0);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003333
Gilles Peskine449bd832023-01-11 14:50:10 +01003334exit:
3335 mbedtls_message_socket_close(&server_context);
3336 mbedtls_message_socket_close(&client_context);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003337}
3338/* END_CASE */
3339
3340/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003341void ssl_message_mock_socket_read_error()
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003342{
3343 enum { MSGLEN = 10 };
3344 unsigned char message[MSGLEN], received[MSGLEN];
3345 mbedtls_mock_socket client, server;
3346 unsigned i;
3347 mbedtls_test_message_queue server_queue, client_queue;
3348 mbedtls_test_message_socket_context server_context, client_context;
Gilles Peskine449bd832023-01-11 14:50:10 +01003349 mbedtls_message_socket_init(&server_context);
3350 mbedtls_message_socket_init(&client_context);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003351
Gilles Peskine449bd832023-01-11 14:50:10 +01003352 TEST_ASSERT(mbedtls_message_socket_setup(&server_queue, &client_queue, 1,
3353 &server,
3354 &server_context) == 0);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003355
Gilles Peskine449bd832023-01-11 14:50:10 +01003356 TEST_ASSERT(mbedtls_message_socket_setup(&client_queue, &server_queue, 1,
3357 &client,
3358 &client_context) == 0);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003359
3360 /* Fill up the buffer with structured data so that unwanted changes
3361 * can be detected */
Gilles Peskine449bd832023-01-11 14:50:10 +01003362 for (i = 0; i < MSGLEN; i++) {
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003363 message[i] = i & 0xFF;
3364 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003365 TEST_ASSERT(0 == mbedtls_mock_socket_connect(&client, &server,
3366 MSGLEN));
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003367
Gilles Peskine449bd832023-01-11 14:50:10 +01003368 TEST_ASSERT(mbedtls_mock_tcp_send_msg(&client_context, message,
3369 MSGLEN) == MSGLEN);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003370
3371 /* Force a read error by disconnecting the socket by hand */
3372 server.status = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003373 TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&server_context, received, MSGLEN)
3374 == MBEDTLS_TEST_ERROR_RECV_FAILED);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003375 /* Return to a valid state */
3376 server.status = MBEDTLS_MOCK_SOCKET_CONNECTED;
3377
Gilles Peskine449bd832023-01-11 14:50:10 +01003378 memset(received, 0, sizeof(received));
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003379
3380 /* Test that even though the server tried to read once disconnected, the
3381 * continuity is preserved */
Gilles Peskine449bd832023-01-11 14:50:10 +01003382 TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&server_context, received, MSGLEN)
3383 == MSGLEN);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003384
Gilles Peskine449bd832023-01-11 14:50:10 +01003385 TEST_ASSERT(memcmp(message, received, MSGLEN) == 0);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003386
Gilles Peskine449bd832023-01-11 14:50:10 +01003387exit:
3388 mbedtls_message_socket_close(&server_context);
3389 mbedtls_message_socket_close(&client_context);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003390}
3391/* END_CASE */
3392
3393/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003394void ssl_message_mock_interleaved_one_way()
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003395{
3396 enum { MSGLEN = 10 };
3397 unsigned char message[MSGLEN], received[MSGLEN];
3398 mbedtls_mock_socket client, server;
3399 unsigned i;
3400 mbedtls_test_message_queue server_queue, client_queue;
3401 mbedtls_test_message_socket_context server_context, client_context;
Gilles Peskine449bd832023-01-11 14:50:10 +01003402 mbedtls_message_socket_init(&server_context);
3403 mbedtls_message_socket_init(&client_context);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003404
Gilles Peskine449bd832023-01-11 14:50:10 +01003405 TEST_ASSERT(mbedtls_message_socket_setup(&server_queue, &client_queue, 3,
3406 &server,
3407 &server_context) == 0);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003408
Gilles Peskine449bd832023-01-11 14:50:10 +01003409 TEST_ASSERT(mbedtls_message_socket_setup(&client_queue, &server_queue, 3,
3410 &client,
3411 &client_context) == 0);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003412
3413 /* Fill up the buffer with structured data so that unwanted changes
3414 * can be detected */
Gilles Peskine449bd832023-01-11 14:50:10 +01003415 for (i = 0; i < MSGLEN; i++) {
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003416 message[i] = i & 0xFF;
3417 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003418 TEST_ASSERT(0 == mbedtls_mock_socket_connect(&client, &server,
3419 MSGLEN*3));
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003420
3421 /* Interleaved test - [2 sends, 1 read] twice, and then two reads
3422 * (to wrap around the buffer) */
Gilles Peskine449bd832023-01-11 14:50:10 +01003423 for (i = 0; i < 2; i++) {
3424 TEST_ASSERT(mbedtls_mock_tcp_send_msg(&client_context, message,
3425 MSGLEN) == MSGLEN);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003426
Gilles Peskine449bd832023-01-11 14:50:10 +01003427 TEST_ASSERT(mbedtls_mock_tcp_send_msg(&client_context, message,
3428 MSGLEN) == MSGLEN);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003429
Gilles Peskine449bd832023-01-11 14:50:10 +01003430 TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&server_context, received,
3431 MSGLEN) == MSGLEN);
3432 TEST_ASSERT(memcmp(message, received, MSGLEN) == 0);
3433 memset(received, 0, sizeof(received));
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003434 }
3435
Gilles Peskine449bd832023-01-11 14:50:10 +01003436 for (i = 0; i < 2; i++) {
3437 TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&server_context, received,
3438 MSGLEN) == MSGLEN);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003439
Gilles Peskine449bd832023-01-11 14:50:10 +01003440 TEST_ASSERT(memcmp(message, received, MSGLEN) == 0);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003441 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003442 TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&server_context, received, MSGLEN)
3443 == MBEDTLS_ERR_SSL_WANT_READ);
3444exit:
3445 mbedtls_message_socket_close(&server_context);
3446 mbedtls_message_socket_close(&client_context);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003447}
3448/* END_CASE */
3449
3450/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003451void ssl_message_mock_interleaved_two_ways()
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003452{
3453 enum { MSGLEN = 10 };
3454 unsigned char message[MSGLEN], received[MSGLEN];
3455 mbedtls_mock_socket client, server;
3456 unsigned i;
3457 mbedtls_test_message_queue server_queue, client_queue;
3458 mbedtls_test_message_socket_context server_context, client_context;
Gilles Peskine449bd832023-01-11 14:50:10 +01003459 mbedtls_message_socket_init(&server_context);
3460 mbedtls_message_socket_init(&client_context);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003461
Gilles Peskine449bd832023-01-11 14:50:10 +01003462 TEST_ASSERT(mbedtls_message_socket_setup(&server_queue, &client_queue, 3,
3463 &server,
3464 &server_context) == 0);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003465
Gilles Peskine449bd832023-01-11 14:50:10 +01003466 TEST_ASSERT(mbedtls_message_socket_setup(&client_queue, &server_queue, 3,
3467 &client,
3468 &client_context) == 0);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003469
3470 /* Fill up the buffer with structured data so that unwanted changes
3471 * can be detected */
Gilles Peskine449bd832023-01-11 14:50:10 +01003472 for (i = 0; i < MSGLEN; i++) {
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003473 message[i] = i & 0xFF;
3474 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003475 TEST_ASSERT(0 == mbedtls_mock_socket_connect(&client, &server,
3476 MSGLEN*3));
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003477
3478 /* Interleaved test - [2 sends, 1 read] twice, both ways, and then two reads
3479 * (to wrap around the buffer) both ways. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003480 for (i = 0; i < 2; i++) {
3481 TEST_ASSERT(mbedtls_mock_tcp_send_msg(&client_context, message,
3482 MSGLEN) == MSGLEN);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003483
Gilles Peskine449bd832023-01-11 14:50:10 +01003484 TEST_ASSERT(mbedtls_mock_tcp_send_msg(&client_context, message,
3485 MSGLEN) == MSGLEN);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003486
Gilles Peskine449bd832023-01-11 14:50:10 +01003487 TEST_ASSERT(mbedtls_mock_tcp_send_msg(&server_context, message,
3488 MSGLEN) == MSGLEN);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003489
Gilles Peskine449bd832023-01-11 14:50:10 +01003490 TEST_ASSERT(mbedtls_mock_tcp_send_msg(&server_context, message,
3491 MSGLEN) == MSGLEN);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003492
Gilles Peskine449bd832023-01-11 14:50:10 +01003493 TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&server_context, received,
3494 MSGLEN) == MSGLEN);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003495
Gilles Peskine449bd832023-01-11 14:50:10 +01003496 TEST_ASSERT(memcmp(message, received, MSGLEN) == 0);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003497
Gilles Peskine449bd832023-01-11 14:50:10 +01003498 memset(received, 0, sizeof(received));
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003499
Gilles Peskine449bd832023-01-11 14:50:10 +01003500 TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&client_context, received,
3501 MSGLEN) == MSGLEN);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003502
Gilles Peskine449bd832023-01-11 14:50:10 +01003503 TEST_ASSERT(memcmp(message, received, MSGLEN) == 0);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003504
Gilles Peskine449bd832023-01-11 14:50:10 +01003505 memset(received, 0, sizeof(received));
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003506 }
3507
Gilles Peskine449bd832023-01-11 14:50:10 +01003508 for (i = 0; i < 2; i++) {
3509 TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&server_context, received,
3510 MSGLEN) == MSGLEN);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003511
Gilles Peskine449bd832023-01-11 14:50:10 +01003512 TEST_ASSERT(memcmp(message, received, MSGLEN) == 0);
3513 memset(received, 0, sizeof(received));
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003514
Gilles Peskine449bd832023-01-11 14:50:10 +01003515 TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&client_context, received,
3516 MSGLEN) == MSGLEN);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003517
Gilles Peskine449bd832023-01-11 14:50:10 +01003518 TEST_ASSERT(memcmp(message, received, MSGLEN) == 0);
3519 memset(received, 0, sizeof(received));
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003520 }
3521
Gilles Peskine449bd832023-01-11 14:50:10 +01003522 TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&server_context, received, MSGLEN)
3523 == MBEDTLS_ERR_SSL_WANT_READ);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003524
Gilles Peskine449bd832023-01-11 14:50:10 +01003525 TEST_ASSERT(mbedtls_mock_tcp_recv_msg(&client_context, received, MSGLEN)
3526 == MBEDTLS_ERR_SSL_WANT_READ);
3527exit:
3528 mbedtls_message_socket_close(&server_context);
3529 mbedtls_message_socket_close(&client_context);
Andrzej Kurekbc483de2020-01-22 03:40:00 -05003530}
3531/* END_CASE */
3532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003533/* BEGIN_CASE depends_on:MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Gilles Peskine449bd832023-01-11 14:50:10 +01003534void ssl_dtls_replay(data_t *prevs, data_t *new, int ret)
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003535{
Azim Khand30ca132017-06-09 04:32:58 +01003536 uint32_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003537 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02003538 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003539
Gilles Peskine449bd832023-01-11 14:50:10 +01003540 mbedtls_ssl_init(&ssl);
3541 mbedtls_ssl_config_init(&conf);
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02003542
Gilles Peskine449bd832023-01-11 14:50:10 +01003543 TEST_ASSERT(mbedtls_ssl_config_defaults(&conf,
3544 MBEDTLS_SSL_IS_CLIENT,
3545 MBEDTLS_SSL_TRANSPORT_DATAGRAM,
3546 MBEDTLS_SSL_PRESET_DEFAULT) == 0);
3547 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003548
3549 /* Read previous record numbers */
Gilles Peskine449bd832023-01-11 14:50:10 +01003550 for (len = 0; len < prevs->len; len += 6) {
3551 memcpy(ssl.in_ctr + 2, prevs->x + len, 6);
3552 mbedtls_ssl_dtls_replay_update(&ssl);
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003553 }
3554
3555 /* Check new number */
Gilles Peskine449bd832023-01-11 14:50:10 +01003556 memcpy(ssl.in_ctr + 2, new->x, 6);
3557 TEST_ASSERT(mbedtls_ssl_dtls_replay_check(&ssl) == ret);
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003558
Gilles Peskine449bd832023-01-11 14:50:10 +01003559 mbedtls_ssl_free(&ssl);
3560 mbedtls_ssl_config_free(&conf);
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02003561}
3562/* END_CASE */
Hanno Beckerb25c0c72017-05-05 11:24:30 +01003563
Ronald Crone68ab4f2022-10-05 12:46:29 +02003564/* BEGIN_CASE depends_on:MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +01003565void ssl_set_hostname_twice(char *hostname0, char *hostname1)
Hanno Beckerb25c0c72017-05-05 11:24:30 +01003566{
3567 mbedtls_ssl_context ssl;
Gilles Peskine449bd832023-01-11 14:50:10 +01003568 mbedtls_ssl_init(&ssl);
Hanno Beckerb25c0c72017-05-05 11:24:30 +01003569
Gilles Peskine449bd832023-01-11 14:50:10 +01003570 TEST_ASSERT(mbedtls_ssl_set_hostname(&ssl, hostname0) == 0);
3571 TEST_ASSERT(mbedtls_ssl_set_hostname(&ssl, hostname1) == 0);
Hanno Beckerb25c0c72017-05-05 11:24:30 +01003572
Gilles Peskine449bd832023-01-11 14:50:10 +01003573 mbedtls_ssl_free(&ssl);
Hanno Beckerb25c0c72017-05-05 11:24:30 +01003574}
Darryl Green11999bb2018-03-13 15:22:58 +00003575/* END_CASE */
Hanno Beckera18d1322018-01-03 14:27:32 +00003576
3577/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003578void ssl_crypt_record(int cipher_type, int hash_id,
3579 int etm, int tag_mode, int ver,
3580 int cid0_len, int cid1_len)
Hanno Beckera18d1322018-01-03 14:27:32 +00003581{
3582 /*
3583 * Test several record encryptions and decryptions
3584 * with plenty of space before and after the data
3585 * within the record buffer.
3586 */
3587
3588 int ret;
3589 int num_records = 16;
3590 mbedtls_ssl_context ssl; /* ONLY for debugging */
3591
3592 mbedtls_ssl_transform t0, t1;
Hanno Becker81e16a32019-03-01 11:21:44 +00003593 unsigned char *buf = NULL;
Hanno Beckera18d1322018-01-03 14:27:32 +00003594 size_t const buflen = 512;
3595 mbedtls_record rec, rec_backup;
3596
Gilles Peskine449bd832023-01-11 14:50:10 +01003597 USE_PSA_INIT();
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01003598
Gilles Peskine449bd832023-01-11 14:50:10 +01003599 mbedtls_ssl_init(&ssl);
3600 mbedtls_ssl_transform_init(&t0);
3601 mbedtls_ssl_transform_init(&t1);
3602 ret = build_transforms(&t0, &t1, cipher_type, hash_id,
3603 etm, tag_mode, ver,
3604 (size_t) cid0_len,
3605 (size_t) cid1_len);
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01003606
Gilles Peskine449bd832023-01-11 14:50:10 +01003607 TEST_ASSERT(ret == 0);
Hanno Beckera18d1322018-01-03 14:27:32 +00003608
Gilles Peskine449bd832023-01-11 14:50:10 +01003609 TEST_ASSERT((buf = mbedtls_calloc(1, buflen)) != NULL);
Hanno Beckera18d1322018-01-03 14:27:32 +00003610
Gilles Peskine449bd832023-01-11 14:50:10 +01003611 while (num_records-- > 0) {
Hanno Beckera18d1322018-01-03 14:27:32 +00003612 mbedtls_ssl_transform *t_dec, *t_enc;
3613 /* Take turns in who's sending and who's receiving. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003614 if (num_records % 3 == 0) {
Hanno Beckera18d1322018-01-03 14:27:32 +00003615 t_dec = &t0;
3616 t_enc = &t1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003617 } else {
Hanno Beckera18d1322018-01-03 14:27:32 +00003618 t_dec = &t1;
3619 t_enc = &t0;
3620 }
3621
3622 /*
3623 * The record header affects the transformation in two ways:
3624 * 1) It determines the AEAD additional data
3625 * 2) The record counter sometimes determines the IV.
3626 *
3627 * Apart from that, the fields don't have influence.
3628 * In particular, it is currently not the responsibility
3629 * of ssl_encrypt/decrypt_buf to check if the transform
3630 * version matches the record version, or that the
3631 * type is sensible.
3632 */
3633
Gilles Peskine449bd832023-01-11 14:50:10 +01003634 memset(rec.ctr, num_records, sizeof(rec.ctr));
Hanno Beckera18d1322018-01-03 14:27:32 +00003635 rec.type = 42;
3636 rec.ver[0] = num_records;
3637 rec.ver[1] = num_records;
Hanno Beckera0e20d02019-05-15 14:03:01 +01003638#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd856c822019-04-29 17:30:59 +01003639 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01003640#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera18d1322018-01-03 14:27:32 +00003641
3642 rec.buf = buf;
3643 rec.buf_len = buflen;
3644 rec.data_offset = 16;
3645 /* Make sure to vary the length to exercise different
3646 * paddings. */
3647 rec.data_len = 1 + num_records;
3648
Gilles Peskine449bd832023-01-11 14:50:10 +01003649 memset(rec.buf + rec.data_offset, 42, rec.data_len);
Hanno Beckera18d1322018-01-03 14:27:32 +00003650
3651 /* Make a copy for later comparison */
3652 rec_backup = rec;
3653
3654 /* Encrypt record */
Gilles Peskine449bd832023-01-11 14:50:10 +01003655 ret = mbedtls_ssl_encrypt_buf(&ssl, t_enc, &rec,
3656 mbedtls_test_rnd_std_rand, NULL);
3657 TEST_ASSERT(ret == 0 || ret == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL);
3658 if (ret != 0) {
Hanno Beckera18d1322018-01-03 14:27:32 +00003659 continue;
3660 }
3661
Hanno Beckerb2713ab2020-05-07 14:54:22 +01003662#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine449bd832023-01-11 14:50:10 +01003663 if (rec.cid_len != 0) {
Hanno Beckerb2713ab2020-05-07 14:54:22 +01003664 /* DTLS 1.2 + CID hides the real content type and
3665 * uses a special CID content type in the protected
3666 * record. Double-check this. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003667 TEST_ASSERT(rec.type == MBEDTLS_SSL_MSG_CID);
Hanno Beckerb2713ab2020-05-07 14:54:22 +01003668 }
3669#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3670
Ronald Cron6f135e12021-12-08 16:57:54 +01003671#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003672 if (t_enc->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Hanno Beckerb2713ab2020-05-07 14:54:22 +01003673 /* TLS 1.3 hides the real content type and
3674 * always uses Application Data as the content type
3675 * for protected records. Double-check this. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003676 TEST_ASSERT(rec.type == MBEDTLS_SSL_MSG_APPLICATION_DATA);
Hanno Beckerb2713ab2020-05-07 14:54:22 +01003677 }
Ronald Cron6f135e12021-12-08 16:57:54 +01003678#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb2713ab2020-05-07 14:54:22 +01003679
Hanno Beckera18d1322018-01-03 14:27:32 +00003680 /* Decrypt record with t_dec */
Gilles Peskine449bd832023-01-11 14:50:10 +01003681 ret = mbedtls_ssl_decrypt_buf(&ssl, t_dec, &rec);
3682 TEST_ASSERT(ret == 0);
Hanno Beckera18d1322018-01-03 14:27:32 +00003683
3684 /* Compare results */
Gilles Peskine449bd832023-01-11 14:50:10 +01003685 TEST_ASSERT(rec.type == rec_backup.type);
3686 TEST_ASSERT(memcmp(rec.ctr, rec_backup.ctr, 8) == 0);
3687 TEST_ASSERT(rec.ver[0] == rec_backup.ver[0]);
3688 TEST_ASSERT(rec.ver[1] == rec_backup.ver[1]);
3689 TEST_ASSERT(rec.data_len == rec_backup.data_len);
3690 TEST_ASSERT(rec.data_offset == rec_backup.data_offset);
3691 TEST_ASSERT(memcmp(rec.buf + rec.data_offset,
3692 rec_backup.buf + rec_backup.data_offset,
3693 rec.data_len) == 0);
Hanno Beckera18d1322018-01-03 14:27:32 +00003694 }
3695
Hanno Becker81e16a32019-03-01 11:21:44 +00003696exit:
3697
Hanno Beckera18d1322018-01-03 14:27:32 +00003698 /* Cleanup */
Gilles Peskine449bd832023-01-11 14:50:10 +01003699 mbedtls_ssl_free(&ssl);
3700 mbedtls_ssl_transform_free(&t0);
3701 mbedtls_ssl_transform_free(&t1);
Hanno Beckera18d1322018-01-03 14:27:32 +00003702
Gilles Peskine449bd832023-01-11 14:50:10 +01003703 mbedtls_free(buf);
3704 USE_PSA_DONE();
Hanno Beckera18d1322018-01-03 14:27:32 +00003705}
3706/* END_CASE */
Hanno Beckerb3268da2018-01-05 15:20:24 +00003707
3708/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003709void ssl_crypt_record_small(int cipher_type, int hash_id,
3710 int etm, int tag_mode, int ver,
3711 int cid0_len, int cid1_len)
Hanno Beckerb3268da2018-01-05 15:20:24 +00003712{
3713 /*
3714 * Test pairs of encryption and decryption with an increasing
3715 * amount of space in the record buffer - in more detail:
3716 * 1) Try to encrypt with 0, 1, 2, ... bytes available
3717 * in front of the plaintext, and expect the encryption
3718 * to succeed starting from some offset. Always keep
3719 * enough space in the end of the buffer.
3720 * 2) Try to encrypt with 0, 1, 2, ... bytes available
3721 * at the end of the plaintext, and expect the encryption
3722 * to succeed starting from some offset. Always keep
3723 * enough space at the beginning of the buffer.
3724 * 3) Try to encrypt with 0, 1, 2, ... bytes available
3725 * both at the front and end of the plaintext,
3726 * and expect the encryption to succeed starting from
3727 * some offset.
3728 *
3729 * If encryption succeeds, check that decryption succeeds
3730 * and yields the original record.
3731 */
3732
3733 mbedtls_ssl_context ssl; /* ONLY for debugging */
3734
3735 mbedtls_ssl_transform t0, t1;
Hanno Becker81e16a32019-03-01 11:21:44 +00003736 unsigned char *buf = NULL;
Hanno Beckerd856c822019-04-29 17:30:59 +01003737 size_t const buflen = 256;
Hanno Beckerb3268da2018-01-05 15:20:24 +00003738 mbedtls_record rec, rec_backup;
3739
3740 int ret;
Hanno Beckerd856c822019-04-29 17:30:59 +01003741 int mode; /* Mode 1, 2 or 3 as explained above */
3742 size_t offset; /* Available space at beginning/end/both */
3743 size_t threshold = 96; /* Maximum offset to test against */
Hanno Beckerb3268da2018-01-05 15:20:24 +00003744
Hanno Beckerd856c822019-04-29 17:30:59 +01003745 size_t default_pre_padding = 64; /* Pre-padding to use in mode 2 */
3746 size_t default_post_padding = 128; /* Post-padding to use in mode 1 */
Hanno Beckerb3268da2018-01-05 15:20:24 +00003747
3748 int seen_success; /* Indicates if in the current mode we've
3749 * already seen a successful test. */
3750
Gilles Peskine449bd832023-01-11 14:50:10 +01003751 USE_PSA_INIT();
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01003752
Gilles Peskine449bd832023-01-11 14:50:10 +01003753 mbedtls_ssl_init(&ssl);
3754 mbedtls_ssl_transform_init(&t0);
3755 mbedtls_ssl_transform_init(&t1);
3756 ret = build_transforms(&t0, &t1, cipher_type, hash_id,
3757 etm, tag_mode, ver,
3758 (size_t) cid0_len,
3759 (size_t) cid1_len);
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01003760
Gilles Peskine449bd832023-01-11 14:50:10 +01003761 TEST_ASSERT(ret == 0);
Hanno Beckerb3268da2018-01-05 15:20:24 +00003762
Gilles Peskine449bd832023-01-11 14:50:10 +01003763 TEST_ASSERT((buf = mbedtls_calloc(1, buflen)) != NULL);
Hanno Beckerb3268da2018-01-05 15:20:24 +00003764
Gilles Peskine449bd832023-01-11 14:50:10 +01003765 for (mode = 1; mode <= 3; mode++) {
Hanno Beckerb3268da2018-01-05 15:20:24 +00003766 seen_success = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003767 for (offset = 0; offset <= threshold; offset++) {
Hanno Beckerb3268da2018-01-05 15:20:24 +00003768 mbedtls_ssl_transform *t_dec, *t_enc;
Hanno Becker6c87b3f2019-04-29 17:24:44 +01003769 t_dec = &t0;
3770 t_enc = &t1;
Hanno Beckerb3268da2018-01-05 15:20:24 +00003771
Gilles Peskine449bd832023-01-11 14:50:10 +01003772 memset(rec.ctr, offset, sizeof(rec.ctr));
Hanno Beckerb3268da2018-01-05 15:20:24 +00003773 rec.type = 42;
3774 rec.ver[0] = offset;
3775 rec.ver[1] = offset;
3776 rec.buf = buf;
3777 rec.buf_len = buflen;
Hanno Beckera0e20d02019-05-15 14:03:01 +01003778#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd856c822019-04-29 17:30:59 +01003779 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01003780#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerb3268da2018-01-05 15:20:24 +00003781
Gilles Peskine449bd832023-01-11 14:50:10 +01003782 switch (mode) {
Hanno Beckerb3268da2018-01-05 15:20:24 +00003783 case 1: /* Space in the beginning */
3784 rec.data_offset = offset;
3785 rec.data_len = buflen - offset - default_post_padding;
3786 break;
3787
3788 case 2: /* Space in the end */
3789 rec.data_offset = default_pre_padding;
3790 rec.data_len = buflen - default_pre_padding - offset;
3791 break;
3792
3793 case 3: /* Space in the beginning and end */
3794 rec.data_offset = offset;
3795 rec.data_len = buflen - 2 * offset;
3796 break;
3797
3798 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003799 TEST_ASSERT(0);
Hanno Beckerb3268da2018-01-05 15:20:24 +00003800 break;
3801 }
3802
Gilles Peskine449bd832023-01-11 14:50:10 +01003803 memset(rec.buf + rec.data_offset, 42, rec.data_len);
Hanno Beckerb3268da2018-01-05 15:20:24 +00003804
3805 /* Make a copy for later comparison */
3806 rec_backup = rec;
3807
3808 /* Encrypt record */
Gilles Peskine449bd832023-01-11 14:50:10 +01003809 ret = mbedtls_ssl_encrypt_buf(&ssl, t_enc, &rec,
3810 mbedtls_test_rnd_std_rand, NULL);
Hanno Beckerb3268da2018-01-05 15:20:24 +00003811
Gilles Peskine449bd832023-01-11 14:50:10 +01003812 if ((mode == 1 || mode == 2) && seen_success) {
3813 TEST_ASSERT(ret == 0);
3814 } else {
3815 TEST_ASSERT(ret == 0 || ret == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL);
3816 if (ret == 0) {
Hanno Beckerb3268da2018-01-05 15:20:24 +00003817 seen_success = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003818 }
Hanno Beckerb3268da2018-01-05 15:20:24 +00003819 }
3820
Gilles Peskine449bd832023-01-11 14:50:10 +01003821 if (ret != 0) {
Hanno Beckerb3268da2018-01-05 15:20:24 +00003822 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01003823 }
Hanno Beckerb3268da2018-01-05 15:20:24 +00003824
Hanno Beckerb2713ab2020-05-07 14:54:22 +01003825#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine449bd832023-01-11 14:50:10 +01003826 if (rec.cid_len != 0) {
Hanno Beckerb2713ab2020-05-07 14:54:22 +01003827 /* DTLS 1.2 + CID hides the real content type and
3828 * uses a special CID content type in the protected
3829 * record. Double-check this. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003830 TEST_ASSERT(rec.type == MBEDTLS_SSL_MSG_CID);
Hanno Beckerb2713ab2020-05-07 14:54:22 +01003831 }
3832#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3833
Ronald Cron6f135e12021-12-08 16:57:54 +01003834#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003835 if (t_enc->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Hanno Beckerb2713ab2020-05-07 14:54:22 +01003836 /* TLS 1.3 hides the real content type and
3837 * always uses Application Data as the content type
3838 * for protected records. Double-check this. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003839 TEST_ASSERT(rec.type == MBEDTLS_SSL_MSG_APPLICATION_DATA);
Hanno Beckerb2713ab2020-05-07 14:54:22 +01003840 }
Ronald Cron6f135e12021-12-08 16:57:54 +01003841#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb2713ab2020-05-07 14:54:22 +01003842
Hanno Beckerb3268da2018-01-05 15:20:24 +00003843 /* Decrypt record with t_dec */
Gilles Peskine449bd832023-01-11 14:50:10 +01003844 TEST_ASSERT(mbedtls_ssl_decrypt_buf(&ssl, t_dec, &rec) == 0);
Hanno Beckerb3268da2018-01-05 15:20:24 +00003845
3846 /* Compare results */
Gilles Peskine449bd832023-01-11 14:50:10 +01003847 TEST_ASSERT(rec.type == rec_backup.type);
3848 TEST_ASSERT(memcmp(rec.ctr, rec_backup.ctr, 8) == 0);
3849 TEST_ASSERT(rec.ver[0] == rec_backup.ver[0]);
3850 TEST_ASSERT(rec.ver[1] == rec_backup.ver[1]);
3851 TEST_ASSERT(rec.data_len == rec_backup.data_len);
3852 TEST_ASSERT(rec.data_offset == rec_backup.data_offset);
3853 TEST_ASSERT(memcmp(rec.buf + rec.data_offset,
3854 rec_backup.buf + rec_backup.data_offset,
3855 rec.data_len) == 0);
Hanno Beckerb3268da2018-01-05 15:20:24 +00003856 }
3857
Gilles Peskine449bd832023-01-11 14:50:10 +01003858 TEST_ASSERT(seen_success == 1);
Hanno Beckerb3268da2018-01-05 15:20:24 +00003859 }
3860
Hanno Becker81e16a32019-03-01 11:21:44 +00003861exit:
3862
Hanno Beckerb3268da2018-01-05 15:20:24 +00003863 /* Cleanup */
Gilles Peskine449bd832023-01-11 14:50:10 +01003864 mbedtls_ssl_free(&ssl);
3865 mbedtls_ssl_transform_free(&t0);
3866 mbedtls_ssl_transform_free(&t1);
Hanno Beckerb3268da2018-01-05 15:20:24 +00003867
Gilles Peskine449bd832023-01-11 14:50:10 +01003868 mbedtls_free(buf);
3869 USE_PSA_DONE();
Hanno Beckerb3268da2018-01-05 15:20:24 +00003870}
3871/* END_CASE */
Ron Eldor824ad7b2019-05-13 14:09:00 +03003872
Przemyslaw Stekielf4facef2022-02-02 21:31:04 +01003873/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC:MBEDTLS_AES_C:MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003874void ssl_decrypt_non_etm_cbc(int cipher_type, int hash_id, int trunc_hmac,
3875 int length_selector)
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003876{
3877 /*
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003878 * Test record decryption for CBC without EtM, focused on the verification
3879 * of padding and MAC.
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003880 *
TRodziewicz299510e2021-07-09 16:55:11 +02003881 * Actually depends on TLS 1.2 and either AES, ARIA or Camellia, but since
3882 * the test framework doesn't support alternation in dependency statements,
3883 * just depend on AES.
Manuel Pégourié-Gonnard864abbf2020-07-21 10:37:14 +02003884 *
3885 * The length_selector argument is interpreted as follows:
3886 * - if it's -1, the plaintext length is 0 and minimal padding is applied
3887 * - if it's -2, the plaintext length is 0 and maximal padding is applied
3888 * - otherwise it must be in [0, 255] and is padding_length from RFC 5246:
3889 * it's the length of the rest of the padding, that is, excluding the
3890 * byte that encodes the length. The minimal non-zero plaintext length
3891 * that gives this padding_length is automatically selected.
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003892 */
3893 mbedtls_ssl_context ssl; /* ONLY for debugging */
3894 mbedtls_ssl_transform t0, t1;
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003895 mbedtls_record rec, rec_save;
3896 unsigned char *buf = NULL, *buf_save = NULL;
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003897 size_t buflen, olen = 0;
Manuel Pégourié-Gonnard864abbf2020-07-21 10:37:14 +02003898 size_t plaintext_len, block_size, i;
Manuel Pégourié-Gonnarde55653f2020-07-22 11:42:57 +02003899 unsigned char padlen; /* excluding the padding_length byte */
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003900 unsigned char add_data[13];
Neil Armstrongcf8841a2022-02-24 11:17:45 +01003901#if defined(MBEDTLS_USE_PSA_CRYPTO)
3902 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
3903 size_t sign_mac_length = 0;
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04003904 unsigned char mac[PSA_HASH_MAX_SIZE];
3905#else
3906 unsigned char mac[MBEDTLS_MD_MAX_SIZE];
Neil Armstrongcf8841a2022-02-24 11:17:45 +01003907#endif
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003908 int exp_ret;
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01003909 int ret;
Manuel Pégourié-Gonnard4adc04a2020-07-16 10:00:48 +02003910 const unsigned char pad_max_len = 255; /* Per the standard */
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003911
Gilles Peskine449bd832023-01-11 14:50:10 +01003912 USE_PSA_INIT();
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01003913
Gilles Peskine449bd832023-01-11 14:50:10 +01003914 mbedtls_ssl_init(&ssl);
3915 mbedtls_ssl_transform_init(&t0);
3916 mbedtls_ssl_transform_init(&t1);
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003917
3918 /* Set up transforms with dummy keys */
Gilles Peskine449bd832023-01-11 14:50:10 +01003919 ret = build_transforms(&t0, &t1, cipher_type, hash_id,
3920 0, trunc_hmac,
3921 MBEDTLS_SSL_VERSION_TLS1_2,
3922 0, 0);
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003923
Gilles Peskine449bd832023-01-11 14:50:10 +01003924 TEST_ASSERT(ret == 0);
Przemyslaw Stekiel4a36dd32022-01-25 00:43:58 +01003925
Manuel Pégourié-Gonnard864abbf2020-07-21 10:37:14 +02003926 /* Determine padding/plaintext length */
Gilles Peskine449bd832023-01-11 14:50:10 +01003927 TEST_ASSERT(length_selector >= -2 && length_selector <= 255);
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003928 block_size = t0.ivlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01003929 if (length_selector < 0) {
Manuel Pégourié-Gonnard864abbf2020-07-21 10:37:14 +02003930 plaintext_len = 0;
3931
Manuel Pégourié-Gonnarde55653f2020-07-22 11:42:57 +02003932 /* Minimal padding
3933 * The +1 is for the padding_length byte, not counted in padlen. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003934 padlen = block_size - (t0.maclen + 1) % block_size;
Manuel Pégourié-Gonnard864abbf2020-07-21 10:37:14 +02003935
3936 /* Maximal padding? */
Gilles Peskine449bd832023-01-11 14:50:10 +01003937 if (length_selector == -2) {
3938 padlen += block_size * ((pad_max_len - padlen) / block_size);
3939 }
3940 } else {
Manuel Pégourié-Gonnard864abbf2020-07-21 10:37:14 +02003941 padlen = length_selector;
3942
Manuel Pégourié-Gonnarde55653f2020-07-22 11:42:57 +02003943 /* Minimal non-zero plaintext_length giving desired padding.
3944 * The +1 is for the padding_length byte, not counted in padlen. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003945 plaintext_len = block_size - (padlen + t0.maclen + 1) % block_size;
Manuel Pégourié-Gonnard864abbf2020-07-21 10:37:14 +02003946 }
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003947
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003948 /* Prepare a buffer for record data */
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003949 buflen = block_size
Gilles Peskine449bd832023-01-11 14:50:10 +01003950 + plaintext_len
3951 + t0.maclen
3952 + padlen + 1;
3953 ASSERT_ALLOC(buf, buflen);
3954 ASSERT_ALLOC(buf_save, buflen);
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003955
3956 /* Prepare a dummy record header */
Gilles Peskine449bd832023-01-11 14:50:10 +01003957 memset(rec.ctr, 0, sizeof(rec.ctr));
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003958 rec.type = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01003959 mbedtls_ssl_write_version(rec.ver, MBEDTLS_SSL_TRANSPORT_STREAM,
3960 MBEDTLS_SSL_VERSION_TLS1_2);
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003961#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
3962 rec.cid_len = 0;
3963#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
3964
3965 /* Prepare dummy record content */
3966 rec.buf = buf;
3967 rec.buf_len = buflen;
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003968 rec.data_offset = block_size;
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003969 rec.data_len = plaintext_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01003970 memset(rec.buf + rec.data_offset, 42, rec.data_len);
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003971
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003972 /* Serialized version of record header for MAC purposes */
Gilles Peskine449bd832023-01-11 14:50:10 +01003973 memcpy(add_data, rec.ctr, 8);
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003974 add_data[8] = rec.type;
3975 add_data[9] = rec.ver[0];
3976 add_data[10] = rec.ver[1];
Gilles Peskine449bd832023-01-11 14:50:10 +01003977 add_data[11] = (rec.data_len >> 8) & 0xff;
3978 add_data[12] = (rec.data_len >> 0) & 0xff;
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003979
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003980 /* Set dummy IV */
Gilles Peskine449bd832023-01-11 14:50:10 +01003981 memset(t0.iv_enc, 0x55, t0.ivlen);
3982 memcpy(rec.buf, t0.iv_enc, t0.ivlen);
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02003983
3984 /*
3985 * Prepare a pre-encryption record (with MAC and padding), and save it.
3986 */
3987
3988 /* MAC with additional data */
Neil Armstrongcf8841a2022-02-24 11:17:45 +01003989#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01003990 TEST_EQUAL(PSA_SUCCESS, psa_mac_sign_setup(&operation,
3991 t0.psa_mac_enc,
3992 t0.psa_mac_alg));
3993 TEST_EQUAL(PSA_SUCCESS, psa_mac_update(&operation, add_data, 13));
3994 TEST_EQUAL(PSA_SUCCESS, psa_mac_update(&operation,
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02003995 rec.buf + rec.data_offset,
Gilles Peskine449bd832023-01-11 14:50:10 +01003996 rec.data_len));
3997 TEST_EQUAL(PSA_SUCCESS, psa_mac_sign_finish(&operation,
3998 mac, sizeof(mac),
3999 &sign_mac_length));
4000#else
4001 TEST_EQUAL(0, mbedtls_md_hmac_update(&t0.md_ctx_enc, add_data, 13));
4002 TEST_EQUAL(0, mbedtls_md_hmac_update(&t0.md_ctx_enc,
4003 rec.buf + rec.data_offset,
4004 rec.data_len));
4005 TEST_EQUAL(0, mbedtls_md_hmac_finish(&t0.md_ctx_enc, mac));
Neil Armstrongcf8841a2022-02-24 11:17:45 +01004006#endif
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02004007
Gilles Peskine449bd832023-01-11 14:50:10 +01004008 memcpy(rec.buf + rec.data_offset + rec.data_len, mac, t0.maclen);
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02004009 rec.data_len += t0.maclen;
4010
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02004011 /* Pad */
Gilles Peskine449bd832023-01-11 14:50:10 +01004012 memset(rec.buf + rec.data_offset + rec.data_len, padlen, padlen + 1);
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02004013 rec.data_len += padlen + 1;
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02004014
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02004015 /* Save correct pre-encryption record */
4016 rec_save = rec;
4017 rec_save.buf = buf_save;
Gilles Peskine449bd832023-01-11 14:50:10 +01004018 memcpy(buf_save, buf, buflen);
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02004019
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02004020 /*
4021 * Encrypt and decrypt the correct record, expecting success
4022 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004023 TEST_EQUAL(0, psa_cipher_encrypt_helper(&t0, t0.iv_enc, t0.ivlen,
4024 rec.buf + rec.data_offset, rec.data_len,
4025 rec.buf + rec.data_offset, &olen));
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02004026 rec.data_offset -= t0.ivlen;
4027 rec.data_len += t0.ivlen;
4028
Gilles Peskine449bd832023-01-11 14:50:10 +01004029 TEST_EQUAL(0, mbedtls_ssl_decrypt_buf(&ssl, &t1, &rec));
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02004030
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02004031 /*
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02004032 * Modify each byte of the pre-encryption record before encrypting and
4033 * decrypting it, expecting failure every time.
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02004034 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004035 for (i = block_size; i < buflen; i++) {
4036 mbedtls_test_set_step(i);
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02004037
4038 /* Restore correct pre-encryption record */
4039 rec = rec_save;
4040 rec.buf = buf;
Gilles Peskine449bd832023-01-11 14:50:10 +01004041 memcpy(buf, buf_save, buflen);
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02004042
Manuel Pégourié-Gonnardb51f0442020-07-21 10:40:25 +02004043 /* Corrupt one byte of the data (could be plaintext, MAC or padding) */
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02004044 rec.buf[i] ^= 0x01;
4045
4046 /* Encrypt */
Gilles Peskine449bd832023-01-11 14:50:10 +01004047 TEST_EQUAL(0, psa_cipher_encrypt_helper(&t0, t0.iv_enc, t0.ivlen,
4048 rec.buf + rec.data_offset, rec.data_len,
4049 rec.buf + rec.data_offset, &olen));
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02004050 rec.data_offset -= t0.ivlen;
4051 rec.data_len += t0.ivlen;
4052
4053 /* Decrypt and expect failure */
Gilles Peskine449bd832023-01-11 14:50:10 +01004054 TEST_EQUAL(MBEDTLS_ERR_SSL_INVALID_MAC,
4055 mbedtls_ssl_decrypt_buf(&ssl, &t1, &rec));
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02004056 }
4057
4058 /*
4059 * Use larger values of the padding bytes - with small buffers, this tests
4060 * the case where the announced padlen would be larger than the buffer
4061 * (and before that, than the buffer minus the size of the MAC), to make
4062 * sure our padding checking code does not perform any out-of-bounds reads
4063 * in this case. (With larger buffers, ie when the plaintext is long or
4064 * maximal length padding is used, this is less relevant but still doesn't
4065 * hurt to test.)
4066 *
4067 * (Start the loop with correct padding, just to double-check that record
4068 * saving did work, and that we're overwriting the correct bytes.)
4069 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004070 for (i = padlen; i <= pad_max_len; i++) {
4071 mbedtls_test_set_step(i);
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02004072
4073 /* Restore correct pre-encryption record */
4074 rec = rec_save;
4075 rec.buf = buf;
Gilles Peskine449bd832023-01-11 14:50:10 +01004076 memcpy(buf, buf_save, buflen);
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02004077
4078 /* Set padding bytes to new value */
Gilles Peskine449bd832023-01-11 14:50:10 +01004079 memset(buf + buflen - padlen - 1, i, padlen + 1);
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02004080
4081 /* Encrypt */
Gilles Peskine449bd832023-01-11 14:50:10 +01004082 TEST_EQUAL(0, psa_cipher_encrypt_helper(&t0, t0.iv_enc, t0.ivlen,
4083 rec.buf + rec.data_offset, rec.data_len,
4084 rec.buf + rec.data_offset, &olen));
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02004085 rec.data_offset -= t0.ivlen;
4086 rec.data_len += t0.ivlen;
4087
4088 /* Decrypt and expect failure except the first time */
Gilles Peskine449bd832023-01-11 14:50:10 +01004089 exp_ret = (i == padlen) ? 0 : MBEDTLS_ERR_SSL_INVALID_MAC;
4090 TEST_EQUAL(exp_ret, mbedtls_ssl_decrypt_buf(&ssl, &t1, &rec));
Manuel Pégourié-Gonnard527c1ff2020-07-07 10:43:37 +02004091 }
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02004092
4093exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004094 mbedtls_ssl_free(&ssl);
4095 mbedtls_ssl_transform_free(&t0);
4096 mbedtls_ssl_transform_free(&t1);
4097 mbedtls_free(buf);
4098 mbedtls_free(buf_save);
4099 USE_PSA_DONE();
Manuel Pégourié-Gonnard0ac01a12020-07-03 12:49:10 +02004100}
4101/* END_CASE */
4102
Ronald Cron6f135e12021-12-08 16:57:54 +01004103/* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004104void ssl_tls13_hkdf_expand_label(int hash_alg,
4105 data_t *secret,
4106 int label_idx,
4107 data_t *ctx,
4108 int desired_length,
4109 data_t *expected)
Hanno Becker39ff4922020-08-21 13:36:56 +01004110{
Gilles Peskine449bd832023-01-11 14:50:10 +01004111 unsigned char dst[100];
Hanno Becker39ff4922020-08-21 13:36:56 +01004112
Hanno Becker70d7fb02020-09-09 10:11:21 +01004113 unsigned char const *lbl = NULL;
4114 size_t lbl_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004115#define MBEDTLS_SSL_TLS1_3_LABEL(name, string) \
4116 if (label_idx == (int) tls13_label_ ## name) \
Xiaofei Baid25fab62021-12-02 06:36:27 +00004117 { \
Xiaofei Bai746f9482021-11-12 08:53:56 +00004118 lbl = mbedtls_ssl_tls13_labels.name; \
Gilles Peskine449bd832023-01-11 14:50:10 +01004119 lbl_len = sizeof(mbedtls_ssl_tls13_labels.name); \
Hanno Becker70d7fb02020-09-09 10:11:21 +01004120 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004121 MBEDTLS_SSL_TLS1_3_LABEL_LIST
Hanno Becker70d7fb02020-09-09 10:11:21 +01004122#undef MBEDTLS_SSL_TLS1_3_LABEL
Gilles Peskine449bd832023-01-11 14:50:10 +01004123 TEST_ASSERT(lbl != NULL);
Hanno Becker39ff4922020-08-21 13:36:56 +01004124
4125 /* Check sanity of test parameters. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004126 TEST_ASSERT((size_t) desired_length <= sizeof(dst));
4127 TEST_ASSERT((size_t) desired_length == expected->len);
Hanno Becker39ff4922020-08-21 13:36:56 +01004128
Gilles Peskine449bd832023-01-11 14:50:10 +01004129 PSA_INIT();
Gabor Mezei892c4aa2022-03-21 12:21:43 +01004130
Gilles Peskine449bd832023-01-11 14:50:10 +01004131 TEST_ASSERT(mbedtls_ssl_tls13_hkdf_expand_label(
4132 (psa_algorithm_t) hash_alg,
4133 secret->x, secret->len,
4134 lbl, lbl_len,
4135 ctx->x, ctx->len,
4136 dst, desired_length) == 0);
Hanno Becker39ff4922020-08-21 13:36:56 +01004137
Gilles Peskine449bd832023-01-11 14:50:10 +01004138 ASSERT_COMPARE(dst, (size_t) desired_length,
4139 expected->x, (size_t) expected->len);
Gabor Mezei892c4aa2022-03-21 12:21:43 +01004140
Gilles Peskine449bd832023-01-11 14:50:10 +01004141 PSA_DONE();
Hanno Becker39ff4922020-08-21 13:36:56 +01004142}
4143/* END_CASE */
4144
Ronald Cron6f135e12021-12-08 16:57:54 +01004145/* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004146void ssl_tls13_traffic_key_generation(int hash_alg,
4147 data_t *server_secret,
4148 data_t *client_secret,
4149 int desired_iv_len,
4150 int desired_key_len,
4151 data_t *expected_server_write_key,
4152 data_t *expected_server_write_iv,
4153 data_t *expected_client_write_key,
4154 data_t *expected_client_write_iv)
Hanno Becker19498f82020-08-21 13:37:08 +01004155{
4156 mbedtls_ssl_key_set keys;
4157
4158 /* Check sanity of test parameters. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004159 TEST_ASSERT(client_secret->len == server_secret->len);
4160 TEST_ASSERT(expected_client_write_iv->len == expected_server_write_iv->len &&
4161 expected_client_write_iv->len == (size_t) desired_iv_len);
4162 TEST_ASSERT(expected_client_write_key->len == expected_server_write_key->len &&
4163 expected_client_write_key->len == (size_t) desired_key_len);
Hanno Becker19498f82020-08-21 13:37:08 +01004164
Gilles Peskine449bd832023-01-11 14:50:10 +01004165 PSA_INIT();
Gabor Mezei892c4aa2022-03-21 12:21:43 +01004166
Gilles Peskine449bd832023-01-11 14:50:10 +01004167 TEST_ASSERT(mbedtls_ssl_tls13_make_traffic_keys(
4168 (psa_algorithm_t) hash_alg,
4169 client_secret->x,
4170 server_secret->x,
4171 client_secret->len /* == server_secret->len */,
4172 desired_key_len, desired_iv_len,
4173 &keys) == 0);
Hanno Becker19498f82020-08-21 13:37:08 +01004174
Gilles Peskine449bd832023-01-11 14:50:10 +01004175 ASSERT_COMPARE(keys.client_write_key,
4176 keys.key_len,
4177 expected_client_write_key->x,
4178 (size_t) desired_key_len);
4179 ASSERT_COMPARE(keys.server_write_key,
4180 keys.key_len,
4181 expected_server_write_key->x,
4182 (size_t) desired_key_len);
4183 ASSERT_COMPARE(keys.client_write_iv,
4184 keys.iv_len,
4185 expected_client_write_iv->x,
4186 (size_t) desired_iv_len);
4187 ASSERT_COMPARE(keys.server_write_iv,
4188 keys.iv_len,
4189 expected_server_write_iv->x,
4190 (size_t) desired_iv_len);
Gabor Mezei892c4aa2022-03-21 12:21:43 +01004191
Gilles Peskine449bd832023-01-11 14:50:10 +01004192 PSA_DONE();
Hanno Becker19498f82020-08-21 13:37:08 +01004193}
4194/* END_CASE */
4195
Ronald Cron6f135e12021-12-08 16:57:54 +01004196/* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004197void ssl_tls13_derive_secret(int hash_alg,
4198 data_t *secret,
4199 int label_idx,
4200 data_t *ctx,
4201 int desired_length,
4202 int already_hashed,
4203 data_t *expected)
Hanno Beckere4849d12020-08-21 14:14:14 +01004204{
Gilles Peskine449bd832023-01-11 14:50:10 +01004205 unsigned char dst[100];
Hanno Beckere4849d12020-08-21 14:14:14 +01004206
Hanno Becker70d7fb02020-09-09 10:11:21 +01004207 unsigned char const *lbl = NULL;
4208 size_t lbl_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004209#define MBEDTLS_SSL_TLS1_3_LABEL(name, string) \
4210 if (label_idx == (int) tls13_label_ ## name) \
Xiaofei Baid25fab62021-12-02 06:36:27 +00004211 { \
Xiaofei Bai746f9482021-11-12 08:53:56 +00004212 lbl = mbedtls_ssl_tls13_labels.name; \
Gilles Peskine449bd832023-01-11 14:50:10 +01004213 lbl_len = sizeof(mbedtls_ssl_tls13_labels.name); \
Hanno Becker70d7fb02020-09-09 10:11:21 +01004214 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004215 MBEDTLS_SSL_TLS1_3_LABEL_LIST
Hanno Becker70d7fb02020-09-09 10:11:21 +01004216#undef MBEDTLS_SSL_TLS1_3_LABEL
Gilles Peskine449bd832023-01-11 14:50:10 +01004217 TEST_ASSERT(lbl != NULL);
Hanno Becker70d7fb02020-09-09 10:11:21 +01004218
Hanno Beckere4849d12020-08-21 14:14:14 +01004219 /* Check sanity of test parameters. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004220 TEST_ASSERT((size_t) desired_length <= sizeof(dst));
4221 TEST_ASSERT((size_t) desired_length == expected->len);
Hanno Beckere4849d12020-08-21 14:14:14 +01004222
Gilles Peskine449bd832023-01-11 14:50:10 +01004223 PSA_INIT();
Gabor Mezei892c4aa2022-03-21 12:21:43 +01004224
Gilles Peskine449bd832023-01-11 14:50:10 +01004225 TEST_ASSERT(mbedtls_ssl_tls13_derive_secret(
4226 (psa_algorithm_t) hash_alg,
4227 secret->x, secret->len,
4228 lbl, lbl_len,
4229 ctx->x, ctx->len,
4230 already_hashed,
4231 dst, desired_length) == 0);
Hanno Beckere4849d12020-08-21 14:14:14 +01004232
Gilles Peskine449bd832023-01-11 14:50:10 +01004233 ASSERT_COMPARE(dst, desired_length,
4234 expected->x, desired_length);
Gabor Mezei892c4aa2022-03-21 12:21:43 +01004235
Gilles Peskine449bd832023-01-11 14:50:10 +01004236 PSA_DONE();
Hanno Beckere4849d12020-08-21 14:14:14 +01004237}
4238/* END_CASE */
4239
Ronald Cron6f135e12021-12-08 16:57:54 +01004240/* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004241void ssl_tls13_derive_early_secrets(int hash_alg,
4242 data_t *secret,
4243 data_t *transcript,
4244 data_t *traffic_expected,
4245 data_t *exporter_expected)
Hanno Beckera4f40a02021-05-24 06:42:11 +01004246{
Xiaofei Bai746f9482021-11-12 08:53:56 +00004247 mbedtls_ssl_tls13_early_secrets secrets;
Hanno Beckera4f40a02021-05-24 06:42:11 +01004248
4249 /* Double-check that we've passed sane parameters. */
Gabor Mezei07732f72022-03-26 17:04:19 +01004250 psa_algorithm_t alg = (psa_algorithm_t) hash_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01004251 size_t const hash_len = PSA_HASH_LENGTH(alg);
4252 TEST_ASSERT(PSA_ALG_IS_HASH(alg) &&
4253 secret->len == hash_len &&
4254 transcript->len == hash_len &&
4255 traffic_expected->len == hash_len &&
4256 exporter_expected->len == hash_len);
Hanno Beckera4f40a02021-05-24 06:42:11 +01004257
Gilles Peskine449bd832023-01-11 14:50:10 +01004258 PSA_INIT();
Hanno Beckera4f40a02021-05-24 06:42:11 +01004259
Gilles Peskine449bd832023-01-11 14:50:10 +01004260 TEST_ASSERT(mbedtls_ssl_tls13_derive_early_secrets(
4261 alg, secret->x, transcript->x, transcript->len,
4262 &secrets) == 0);
Hanno Beckera4f40a02021-05-24 06:42:11 +01004263
Gilles Peskine449bd832023-01-11 14:50:10 +01004264 ASSERT_COMPARE(secrets.client_early_traffic_secret, hash_len,
4265 traffic_expected->x, traffic_expected->len);
4266 ASSERT_COMPARE(secrets.early_exporter_master_secret, hash_len,
4267 exporter_expected->x, exporter_expected->len);
Gabor Mezei892c4aa2022-03-21 12:21:43 +01004268
Gilles Peskine449bd832023-01-11 14:50:10 +01004269 PSA_DONE();
Hanno Beckera4f40a02021-05-24 06:42:11 +01004270}
4271/* END_CASE */
4272
Ronald Cron6f135e12021-12-08 16:57:54 +01004273/* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004274void ssl_tls13_derive_handshake_secrets(int hash_alg,
4275 data_t *secret,
4276 data_t *transcript,
4277 data_t *client_expected,
4278 data_t *server_expected)
Hanno Beckera4f40a02021-05-24 06:42:11 +01004279{
Xiaofei Bai746f9482021-11-12 08:53:56 +00004280 mbedtls_ssl_tls13_handshake_secrets secrets;
Hanno Beckera4f40a02021-05-24 06:42:11 +01004281
4282 /* Double-check that we've passed sane parameters. */
Gabor Mezei07732f72022-03-26 17:04:19 +01004283 psa_algorithm_t alg = (psa_algorithm_t) hash_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01004284 size_t const hash_len = PSA_HASH_LENGTH(alg);
4285 TEST_ASSERT(PSA_ALG_IS_HASH(alg) &&
4286 secret->len == hash_len &&
4287 transcript->len == hash_len &&
4288 client_expected->len == hash_len &&
4289 server_expected->len == hash_len);
Hanno Beckera4f40a02021-05-24 06:42:11 +01004290
Gilles Peskine449bd832023-01-11 14:50:10 +01004291 PSA_INIT();
Hanno Beckera4f40a02021-05-24 06:42:11 +01004292
Gilles Peskine449bd832023-01-11 14:50:10 +01004293 TEST_ASSERT(mbedtls_ssl_tls13_derive_handshake_secrets(
4294 alg, secret->x, transcript->x, transcript->len,
4295 &secrets) == 0);
Hanno Beckera4f40a02021-05-24 06:42:11 +01004296
Gilles Peskine449bd832023-01-11 14:50:10 +01004297 ASSERT_COMPARE(secrets.client_handshake_traffic_secret, hash_len,
4298 client_expected->x, client_expected->len);
4299 ASSERT_COMPARE(secrets.server_handshake_traffic_secret, hash_len,
4300 server_expected->x, server_expected->len);
Gabor Mezei892c4aa2022-03-21 12:21:43 +01004301
Gilles Peskine449bd832023-01-11 14:50:10 +01004302 PSA_DONE();
Hanno Beckera4f40a02021-05-24 06:42:11 +01004303}
4304/* END_CASE */
4305
Ronald Cron6f135e12021-12-08 16:57:54 +01004306/* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004307void ssl_tls13_derive_application_secrets(int hash_alg,
Xiaofei Bai746f9482021-11-12 08:53:56 +00004308 data_t *secret,
4309 data_t *transcript,
Gilles Peskine449bd832023-01-11 14:50:10 +01004310 data_t *client_expected,
4311 data_t *server_expected,
4312 data_t *exporter_expected)
Hanno Beckera4f40a02021-05-24 06:42:11 +01004313{
Xiaofei Bai746f9482021-11-12 08:53:56 +00004314 mbedtls_ssl_tls13_application_secrets secrets;
Hanno Beckera4f40a02021-05-24 06:42:11 +01004315
4316 /* Double-check that we've passed sane parameters. */
Gabor Mezei07732f72022-03-26 17:04:19 +01004317 psa_algorithm_t alg = (psa_algorithm_t) hash_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01004318 size_t const hash_len = PSA_HASH_LENGTH(alg);
4319 TEST_ASSERT(PSA_ALG_IS_HASH(alg) &&
4320 secret->len == hash_len &&
4321 transcript->len == hash_len &&
4322 client_expected->len == hash_len &&
4323 server_expected->len == hash_len &&
4324 exporter_expected->len == hash_len);
Hanno Beckera4f40a02021-05-24 06:42:11 +01004325
Gilles Peskine449bd832023-01-11 14:50:10 +01004326 PSA_INIT();
Hanno Beckera4f40a02021-05-24 06:42:11 +01004327
Gilles Peskine449bd832023-01-11 14:50:10 +01004328 TEST_ASSERT(mbedtls_ssl_tls13_derive_application_secrets(
4329 alg, secret->x, transcript->x, transcript->len,
4330 &secrets) == 0);
Hanno Beckera4f40a02021-05-24 06:42:11 +01004331
Gilles Peskine449bd832023-01-11 14:50:10 +01004332 ASSERT_COMPARE(secrets.client_application_traffic_secret_N, hash_len,
4333 client_expected->x, client_expected->len);
4334 ASSERT_COMPARE(secrets.server_application_traffic_secret_N, hash_len,
4335 server_expected->x, server_expected->len);
4336 ASSERT_COMPARE(secrets.exporter_master_secret, hash_len,
4337 exporter_expected->x, exporter_expected->len);
Gabor Mezei892c4aa2022-03-21 12:21:43 +01004338
Gilles Peskine449bd832023-01-11 14:50:10 +01004339 PSA_DONE();
Hanno Beckera4f40a02021-05-24 06:42:11 +01004340}
4341/* END_CASE */
4342
Ronald Cron6f135e12021-12-08 16:57:54 +01004343/* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004344void ssl_tls13_derive_resumption_secrets(int hash_alg,
4345 data_t *secret,
4346 data_t *transcript,
4347 data_t *resumption_expected)
Hanno Becker55bc2c52021-05-24 06:53:52 +01004348{
Gilles Peskine449bd832023-01-11 14:50:10 +01004349 mbedtls_ssl_tls13_application_secrets secrets;
Hanno Becker55bc2c52021-05-24 06:53:52 +01004350
4351 /* Double-check that we've passed sane parameters. */
Gabor Mezei07732f72022-03-26 17:04:19 +01004352 psa_algorithm_t alg = (psa_algorithm_t) hash_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01004353 size_t const hash_len = PSA_HASH_LENGTH(alg);
4354 TEST_ASSERT(PSA_ALG_IS_HASH(alg) &&
4355 secret->len == hash_len &&
4356 transcript->len == hash_len &&
4357 resumption_expected->len == hash_len);
Hanno Becker55bc2c52021-05-24 06:53:52 +01004358
Gilles Peskine449bd832023-01-11 14:50:10 +01004359 PSA_INIT();
Hanno Becker55bc2c52021-05-24 06:53:52 +01004360
Gilles Peskine449bd832023-01-11 14:50:10 +01004361 TEST_ASSERT(mbedtls_ssl_tls13_derive_resumption_master_secret(
4362 alg, secret->x, transcript->x, transcript->len,
4363 &secrets) == 0);
Hanno Becker55bc2c52021-05-24 06:53:52 +01004364
Gilles Peskine449bd832023-01-11 14:50:10 +01004365 ASSERT_COMPARE(secrets.resumption_master_secret, hash_len,
4366 resumption_expected->x, resumption_expected->len);
Gabor Mezei892c4aa2022-03-21 12:21:43 +01004367
Gilles Peskine449bd832023-01-11 14:50:10 +01004368 PSA_DONE();
4369}
4370/* END_CASE */
4371
4372/* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */
4373void ssl_tls13_create_psk_binder(int hash_alg,
4374 data_t *psk,
4375 int psk_type,
4376 data_t *transcript,
4377 data_t *binder_expected)
4378{
4379 unsigned char binder[MBEDTLS_HASH_MAX_SIZE];
4380
4381 /* Double-check that we've passed sane parameters. */
4382 psa_algorithm_t alg = (psa_algorithm_t) hash_alg;
4383 size_t const hash_len = PSA_HASH_LENGTH(alg);
4384 TEST_ASSERT(PSA_ALG_IS_HASH(alg) &&
4385 transcript->len == hash_len &&
4386 binder_expected->len == hash_len);
4387
4388 PSA_INIT();
4389
4390 TEST_ASSERT(mbedtls_ssl_tls13_create_psk_binder(
4391 NULL, /* SSL context for debugging only */
4392 alg,
4393 psk->x, psk->len,
4394 psk_type,
4395 transcript->x,
4396 binder) == 0);
4397
4398 ASSERT_COMPARE(binder, hash_len,
4399 binder_expected->x, binder_expected->len);
4400
4401 PSA_DONE();
Hanno Becker55bc2c52021-05-24 06:53:52 +01004402}
4403/* END_CASE */
4404
Andrzej Kurekdaf43fb2022-10-12 10:46:42 -04004405/* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3:MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Gilles Peskine449bd832023-01-11 14:50:10 +01004406void ssl_tls13_record_protection(int ciphersuite,
4407 int endpoint,
4408 int ctr,
4409 int padding_used,
4410 data_t *server_write_key,
4411 data_t *server_write_iv,
4412 data_t *client_write_key,
4413 data_t *client_write_iv,
4414 data_t *plaintext,
4415 data_t *ciphertext)
Hanno Beckera77d0052021-03-22 15:16:33 +00004416{
4417 mbedtls_ssl_key_set keys;
4418 mbedtls_ssl_transform transform_send;
4419 mbedtls_ssl_transform transform_recv;
4420 mbedtls_record rec;
4421 unsigned char *buf = NULL;
Hanno Becker1f918782021-08-01 19:18:28 +01004422 size_t buf_len;
Hanno Beckera77d0052021-03-22 15:16:33 +00004423 int other_endpoint;
4424
Gilles Peskine449bd832023-01-11 14:50:10 +01004425 USE_PSA_INIT();
Przemyslaw Stekiel93cf4ee2022-01-19 16:18:53 +01004426
Gilles Peskine449bd832023-01-11 14:50:10 +01004427 TEST_ASSERT(endpoint == MBEDTLS_SSL_IS_CLIENT ||
4428 endpoint == MBEDTLS_SSL_IS_SERVER);
Hanno Beckera77d0052021-03-22 15:16:33 +00004429
Gilles Peskine449bd832023-01-11 14:50:10 +01004430 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Hanno Beckera77d0052021-03-22 15:16:33 +00004431 other_endpoint = MBEDTLS_SSL_IS_CLIENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01004432 }
4433 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Hanno Beckera77d0052021-03-22 15:16:33 +00004434 other_endpoint = MBEDTLS_SSL_IS_SERVER;
Gilles Peskine449bd832023-01-11 14:50:10 +01004435 }
Hanno Beckera77d0052021-03-22 15:16:33 +00004436
Gilles Peskine449bd832023-01-11 14:50:10 +01004437 TEST_ASSERT(server_write_key->len == client_write_key->len);
4438 TEST_ASSERT(server_write_iv->len == client_write_iv->len);
Hanno Beckera77d0052021-03-22 15:16:33 +00004439
Gilles Peskine449bd832023-01-11 14:50:10 +01004440 memcpy(keys.client_write_key,
4441 client_write_key->x, client_write_key->len);
4442 memcpy(keys.client_write_iv,
4443 client_write_iv->x, client_write_iv->len);
4444 memcpy(keys.server_write_key,
4445 server_write_key->x, server_write_key->len);
4446 memcpy(keys.server_write_iv,
4447 server_write_iv->x, server_write_iv->len);
Hanno Beckera77d0052021-03-22 15:16:33 +00004448
4449 keys.key_len = server_write_key->len;
4450 keys.iv_len = server_write_iv->len;
4451
Gilles Peskine449bd832023-01-11 14:50:10 +01004452 mbedtls_ssl_transform_init(&transform_recv);
4453 mbedtls_ssl_transform_init(&transform_send);
Hanno Beckera77d0052021-03-22 15:16:33 +00004454
Gilles Peskine449bd832023-01-11 14:50:10 +01004455 TEST_ASSERT(mbedtls_ssl_tls13_populate_transform(
4456 &transform_send, endpoint,
4457 ciphersuite, &keys, NULL) == 0);
4458 TEST_ASSERT(mbedtls_ssl_tls13_populate_transform(
4459 &transform_recv, other_endpoint,
4460 ciphersuite, &keys, NULL) == 0);
Hanno Beckera77d0052021-03-22 15:16:33 +00004461
Hanno Becker1f918782021-08-01 19:18:28 +01004462 /* Make sure we have enough space in the buffer even if
4463 * we use more padding than the KAT. */
4464 buf_len = ciphertext->len + MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY;
Gilles Peskine449bd832023-01-11 14:50:10 +01004465 ASSERT_ALLOC(buf, buf_len);
Hanno Beckera77d0052021-03-22 15:16:33 +00004466 rec.type = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Hanno Becker41537452021-04-20 05:35:28 +01004467
4468 /* TLS 1.3 uses the version identifier from TLS 1.2 on the wire. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004469 mbedtls_ssl_write_version(rec.ver,
4470 MBEDTLS_SSL_TRANSPORT_STREAM,
4471 MBEDTLS_SSL_VERSION_TLS1_2);
Hanno Beckera77d0052021-03-22 15:16:33 +00004472
4473 /* Copy plaintext into record structure */
4474 rec.buf = buf;
Hanno Becker1f918782021-08-01 19:18:28 +01004475 rec.buf_len = buf_len;
Hanno Beckera77d0052021-03-22 15:16:33 +00004476 rec.data_offset = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01004477 TEST_ASSERT(plaintext->len <= ciphertext->len);
4478 memcpy(rec.buf + rec.data_offset, plaintext->x, plaintext->len);
Hanno Beckera77d0052021-03-22 15:16:33 +00004479 rec.data_len = plaintext->len;
4480#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4481 rec.cid_len = 0;
4482#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4483
Gilles Peskine449bd832023-01-11 14:50:10 +01004484 memset(&rec.ctr[0], 0, 8);
Hanno Beckera77d0052021-03-22 15:16:33 +00004485 rec.ctr[7] = ctr;
4486
Gilles Peskine449bd832023-01-11 14:50:10 +01004487 TEST_ASSERT(mbedtls_ssl_encrypt_buf(NULL, &transform_send, &rec,
4488 NULL, NULL) == 0);
Hanno Becker1f918782021-08-01 19:18:28 +01004489
Gilles Peskine449bd832023-01-11 14:50:10 +01004490 if (padding_used == MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY) {
4491 ASSERT_COMPARE(rec.buf + rec.data_offset, rec.data_len,
4492 ciphertext->x, ciphertext->len);
Hanno Becker1f918782021-08-01 19:18:28 +01004493 }
Hanno Beckera77d0052021-03-22 15:16:33 +00004494
Gilles Peskine449bd832023-01-11 14:50:10 +01004495 TEST_ASSERT(mbedtls_ssl_decrypt_buf(NULL, &transform_recv, &rec) == 0);
4496 ASSERT_COMPARE(rec.buf + rec.data_offset, rec.data_len,
4497 plaintext->x, plaintext->len);
Hanno Beckera77d0052021-03-22 15:16:33 +00004498
Gilles Peskine449bd832023-01-11 14:50:10 +01004499 mbedtls_free(buf);
4500 mbedtls_ssl_transform_free(&transform_send);
4501 mbedtls_ssl_transform_free(&transform_recv);
4502 USE_PSA_DONE();
Hanno Beckera77d0052021-03-22 15:16:33 +00004503}
4504/* END_CASE */
4505
Andrzej Kurek658442f2022-10-12 11:28:41 -04004506/* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_3 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004507void ssl_tls13_key_evolution(int hash_alg,
4508 data_t *secret,
4509 data_t *input,
4510 data_t *expected)
Hanno Becker2d2c3eb2020-08-20 14:54:24 +01004511{
Gilles Peskine449bd832023-01-11 14:50:10 +01004512 unsigned char secret_new[MBEDTLS_HASH_MAX_SIZE];
Hanno Becker2d2c3eb2020-08-20 14:54:24 +01004513
Gabor Mezei5d9a1fe2022-03-24 17:49:14 +01004514 PSA_INIT();
Gabor Mezei892c4aa2022-03-21 12:21:43 +01004515
Gilles Peskine449bd832023-01-11 14:50:10 +01004516 TEST_ASSERT(mbedtls_ssl_tls13_evolve_secret(
4517 (psa_algorithm_t) hash_alg,
4518 secret->len ? secret->x : NULL,
4519 input->len ? input->x : NULL, input->len,
4520 secret_new) == 0);
Hanno Becker2d2c3eb2020-08-20 14:54:24 +01004521
Gilles Peskine449bd832023-01-11 14:50:10 +01004522 ASSERT_COMPARE(secret_new, (size_t) expected->len,
4523 expected->x, (size_t) expected->len);
Gabor Mezei892c4aa2022-03-21 12:21:43 +01004524
Gabor Mezei5d9a1fe2022-03-24 17:49:14 +01004525 PSA_DONE();
Hanno Becker2d2c3eb2020-08-20 14:54:24 +01004526}
4527/* END_CASE */
4528
Jerry Yu53d23e22022-02-09 16:25:09 +08004529/* BEGIN_CASE depends_on:MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004530void ssl_tls_prf(int type, data_t *secret, data_t *random,
4531 char *label, data_t *result_str, int exp_ret)
Ron Eldor824ad7b2019-05-13 14:09:00 +03004532{
4533 unsigned char *output;
4534
Gilles Peskine449bd832023-01-11 14:50:10 +01004535 output = mbedtls_calloc(1, result_str->len);
4536 if (output == NULL) {
Ron Eldor824ad7b2019-05-13 14:09:00 +03004537 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004538 }
Ron Eldor824ad7b2019-05-13 14:09:00 +03004539
Gilles Peskine449bd832023-01-11 14:50:10 +01004540 USE_PSA_INIT();
Ron Eldor6b9b1b82019-05-15 17:04:33 +03004541
Gilles Peskine449bd832023-01-11 14:50:10 +01004542 TEST_ASSERT(mbedtls_ssl_tls_prf(type, secret->x, secret->len,
4543 label, random->x, random->len,
4544 output, result_str->len) == exp_ret);
Ron Eldor824ad7b2019-05-13 14:09:00 +03004545
Gilles Peskine449bd832023-01-11 14:50:10 +01004546 if (exp_ret == 0) {
4547 TEST_ASSERT(mbedtls_test_hexcmp(output, result_str->x,
4548 result_str->len, result_str->len) == 0);
Ron Eldor824ad7b2019-05-13 14:09:00 +03004549 }
4550exit:
4551
Gilles Peskine449bd832023-01-11 14:50:10 +01004552 mbedtls_free(output);
4553 USE_PSA_DONE();
Ron Eldor824ad7b2019-05-13 14:09:00 +03004554}
4555/* END_CASE */
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02004556
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02004557/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004558void ssl_serialize_session_save_load(int ticket_len, char *crt_file,
4559 int endpoint_type, int tls_version)
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02004560{
4561 mbedtls_ssl_session original, restored;
4562 unsigned char *buf = NULL;
4563 size_t len;
4564
4565 /*
4566 * Test that a save-load pair is the identity
4567 */
4568
Gilles Peskine449bd832023-01-11 14:50:10 +01004569 mbedtls_ssl_session_init(&original);
4570 mbedtls_ssl_session_init(&restored);
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02004571
4572 /* Prepare a dummy session to work on */
Jerry Yuf0926292022-07-15 13:05:57 +08004573 ((void) endpoint_type);
4574 ((void) tls_version);
4575#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004576 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
4577 TEST_ASSERT(ssl_tls13_populate_session(
4578 &original, 0, endpoint_type) == 0);
4579 } else
Jerry Yuf0926292022-07-15 13:05:57 +08004580#endif
4581 {
Gilles Peskine449bd832023-01-11 14:50:10 +01004582 TEST_ASSERT(ssl_tls12_populate_session(
4583 &original, ticket_len, crt_file) == 0);
Jerry Yuf0926292022-07-15 13:05:57 +08004584 }
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02004585
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02004586 /* Serialize it */
Gilles Peskine449bd832023-01-11 14:50:10 +01004587 TEST_ASSERT(mbedtls_ssl_session_save(&original, NULL, 0, &len)
4588 == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL);
4589 TEST_ASSERT((buf = mbedtls_calloc(1, len)) != NULL);
4590 TEST_ASSERT(mbedtls_ssl_session_save(&original, buf, len, &len)
4591 == 0);
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02004592
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02004593 /* Restore session from serialized data */
Gilles Peskine449bd832023-01-11 14:50:10 +01004594 TEST_ASSERT(mbedtls_ssl_session_load(&restored, buf, len) == 0);
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02004595
4596 /*
4597 * Make sure both session structures are identical
4598 */
4599#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01004600 TEST_ASSERT(original.start == restored.start);
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02004601#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01004602 TEST_ASSERT(original.tls_version == restored.tls_version);
4603 TEST_ASSERT(original.ciphersuite == restored.ciphersuite);
Jerry Yuf0926292022-07-15 13:05:57 +08004604#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01004605 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
4606 TEST_ASSERT(original.id_len == restored.id_len);
4607 TEST_ASSERT(memcmp(original.id,
4608 restored.id, sizeof(original.id)) == 0);
4609 TEST_ASSERT(memcmp(original.master,
4610 restored.master, sizeof(original.master)) == 0);
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02004611
Ronald Crone68ab4f2022-10-05 12:46:29 +02004612#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02004613#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01004614 TEST_ASSERT((original.peer_cert == NULL) ==
4615 (restored.peer_cert == NULL));
4616 if (original.peer_cert != NULL) {
4617 TEST_ASSERT(original.peer_cert->raw.len ==
4618 restored.peer_cert->raw.len);
4619 TEST_ASSERT(memcmp(original.peer_cert->raw.p,
4620 restored.peer_cert->raw.p,
4621 original.peer_cert->raw.len) == 0);
Jerry Yuf0926292022-07-15 13:05:57 +08004622 }
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02004623#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004624 TEST_ASSERT(original.peer_cert_digest_type ==
4625 restored.peer_cert_digest_type);
4626 TEST_ASSERT(original.peer_cert_digest_len ==
4627 restored.peer_cert_digest_len);
4628 TEST_ASSERT((original.peer_cert_digest == NULL) ==
4629 (restored.peer_cert_digest == NULL));
4630 if (original.peer_cert_digest != NULL) {
4631 TEST_ASSERT(memcmp(original.peer_cert_digest,
4632 restored.peer_cert_digest,
4633 original.peer_cert_digest_len) == 0);
Jerry Yuf0926292022-07-15 13:05:57 +08004634 }
Manuel Pégourié-Gonnardee13a732019-07-29 13:00:39 +02004635#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Ronald Crone68ab4f2022-10-05 12:46:29 +02004636#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +01004637 TEST_ASSERT(original.verify_result == restored.verify_result);
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02004638
Jerry Yuf0926292022-07-15 13:05:57 +08004639#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01004640 TEST_ASSERT(original.mfl_code == restored.mfl_code);
Jerry Yuf0926292022-07-15 13:05:57 +08004641#endif
4642
4643#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01004644 TEST_ASSERT(original.encrypt_then_mac == restored.encrypt_then_mac);
Jerry Yuf0926292022-07-15 13:05:57 +08004645#endif
Jerry Yu6ac7c032022-07-21 23:11:55 +08004646#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004647 TEST_ASSERT(original.ticket_len == restored.ticket_len);
4648 if (original.ticket_len != 0) {
4649 TEST_ASSERT(original.ticket != NULL);
4650 TEST_ASSERT(restored.ticket != NULL);
4651 TEST_ASSERT(memcmp(original.ticket,
4652 restored.ticket, original.ticket_len) == 0);
Jerry Yu6ac7c032022-07-21 23:11:55 +08004653 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004654 TEST_ASSERT(original.ticket_lifetime == restored.ticket_lifetime);
Jerry Yu6ac7c032022-07-21 23:11:55 +08004655#endif
Jerry Yuf0926292022-07-15 13:05:57 +08004656 }
Jerry Yu6ac7c032022-07-21 23:11:55 +08004657#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4658
4659#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004660 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
4661 TEST_ASSERT(original.endpoint == restored.endpoint);
4662 TEST_ASSERT(original.ciphersuite == restored.ciphersuite);
4663 TEST_ASSERT(original.ticket_age_add == restored.ticket_age_add);
4664 TEST_ASSERT(original.ticket_flags == restored.ticket_flags);
4665 TEST_ASSERT(original.resumption_key_len == restored.resumption_key_len);
4666 if (original.resumption_key_len != 0) {
4667 TEST_ASSERT(original.resumption_key != NULL);
4668 TEST_ASSERT(restored.resumption_key != NULL);
4669 TEST_ASSERT(memcmp(original.resumption_key,
4670 restored.resumption_key,
4671 original.resumption_key_len) == 0);
Jerry Yu6ac7c032022-07-21 23:11:55 +08004672 }
4673#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004674 if (endpoint_type == MBEDTLS_SSL_IS_SERVER) {
4675 TEST_ASSERT(original.start == restored.start);
Jerry Yu6ac7c032022-07-21 23:11:55 +08004676 }
Jerry Yuf0926292022-07-15 13:05:57 +08004677#endif
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02004678#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004679 if (endpoint_type == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu6ac7c032022-07-21 23:11:55 +08004680#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01004681 TEST_ASSERT(original.ticket_received == restored.ticket_received);
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02004682#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01004683 TEST_ASSERT(original.ticket_lifetime == restored.ticket_lifetime);
4684 TEST_ASSERT(original.ticket_len == restored.ticket_len);
4685 if (original.ticket_len != 0) {
4686 TEST_ASSERT(original.ticket != NULL);
4687 TEST_ASSERT(restored.ticket != NULL);
4688 TEST_ASSERT(memcmp(original.ticket,
4689 restored.ticket,
4690 original.ticket_len) == 0);
Jerry Yu6ac7c032022-07-21 23:11:55 +08004691 }
4692
4693 }
4694#endif
4695 }
4696#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02004697
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02004698exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004699 mbedtls_ssl_session_free(&original);
4700 mbedtls_ssl_session_free(&restored);
4701 mbedtls_free(buf);
Manuel Pégourié-Gonnardf9deaec2019-05-24 09:41:39 +02004702}
4703/* END_CASE */
4704
Manuel Pégourié-Gonnardaa755832019-06-03 10:53:47 +02004705/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004706void ssl_serialize_session_load_save(int ticket_len, char *crt_file,
4707 int endpoint_type, int tls_version)
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02004708{
4709 mbedtls_ssl_session session;
4710 unsigned char *buf1 = NULL, *buf2 = NULL;
4711 size_t len0, len1, len2;
4712
4713 /*
4714 * Test that a load-save pair is the identity
4715 */
4716
Gilles Peskine449bd832023-01-11 14:50:10 +01004717 mbedtls_ssl_session_init(&session);
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02004718
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02004719 /* Prepare a dummy session to work on */
Jerry Yua180f992022-07-15 12:52:54 +08004720 ((void) endpoint_type);
4721 ((void) tls_version);
4722#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004723 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
4724 TEST_ASSERT(ssl_tls13_populate_session(
4725 &session, 0, endpoint_type) == 0);
4726 } else
Jerry Yua180f992022-07-15 12:52:54 +08004727#endif
4728 {
Gilles Peskine449bd832023-01-11 14:50:10 +01004729 TEST_ASSERT(ssl_tls12_populate_session(
4730 &session, ticket_len, crt_file) == 0);
Jerry Yua180f992022-07-15 12:52:54 +08004731 }
Manuel Pégourié-Gonnard3caa6ca2019-05-23 10:06:14 +02004732
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02004733 /* Get desired buffer size for serializing */
Gilles Peskine449bd832023-01-11 14:50:10 +01004734 TEST_ASSERT(mbedtls_ssl_session_save(&session, NULL, 0, &len0)
4735 == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL);
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02004736
4737 /* Allocate first buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01004738 buf1 = mbedtls_calloc(1, len0);
4739 TEST_ASSERT(buf1 != NULL);
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02004740
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02004741 /* Serialize to buffer and free live session */
Gilles Peskine449bd832023-01-11 14:50:10 +01004742 TEST_ASSERT(mbedtls_ssl_session_save(&session, buf1, len0, &len1)
4743 == 0);
4744 TEST_ASSERT(len0 == len1);
4745 mbedtls_ssl_session_free(&session);
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02004746
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02004747 /* Restore session from serialized data */
Gilles Peskine449bd832023-01-11 14:50:10 +01004748 TEST_ASSERT(mbedtls_ssl_session_load(&session, buf1, len1) == 0);
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02004749
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02004750 /* Allocate second buffer and serialize to it */
Gilles Peskine449bd832023-01-11 14:50:10 +01004751 buf2 = mbedtls_calloc(1, len0);
4752 TEST_ASSERT(buf2 != NULL);
4753 TEST_ASSERT(mbedtls_ssl_session_save(&session, buf2, len0, &len2)
4754 == 0);
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02004755
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02004756 /* Make sure both serialized versions are identical */
Gilles Peskine449bd832023-01-11 14:50:10 +01004757 TEST_ASSERT(len1 == len2);
4758 TEST_ASSERT(memcmp(buf1, buf2, len1) == 0);
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02004759
4760exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004761 mbedtls_ssl_session_free(&session);
4762 mbedtls_free(buf1);
4763 mbedtls_free(buf2);
Manuel Pégourié-Gonnard6eac11b2019-05-23 09:30:55 +02004764}
4765/* END_CASE */
Manuel Pégourié-Gonnardf5fa0aa2019-05-23 10:38:11 +02004766
4767/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004768void ssl_serialize_session_save_buf_size(int ticket_len, char *crt_file,
4769 int endpoint_type, int tls_version)
Manuel Pégourié-Gonnardf5fa0aa2019-05-23 10:38:11 +02004770{
4771 mbedtls_ssl_session session;
4772 unsigned char *buf = NULL;
4773 size_t good_len, bad_len, test_len;
4774
4775 /*
4776 * Test that session_save() fails cleanly on small buffers
4777 */
4778
Gilles Peskine449bd832023-01-11 14:50:10 +01004779 mbedtls_ssl_session_init(&session);
Manuel Pégourié-Gonnardf5fa0aa2019-05-23 10:38:11 +02004780
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02004781 /* Prepare dummy session and get serialized size */
Jerry Yu1ac476c2022-07-15 11:22:40 +08004782 ((void) endpoint_type);
4783 ((void) tls_version);
4784#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004785 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
4786 TEST_ASSERT(ssl_tls13_populate_session(
4787 &session, 0, endpoint_type) == 0);
4788 } else
Jerry Yu1ac476c2022-07-15 11:22:40 +08004789#endif
4790 {
Gilles Peskine449bd832023-01-11 14:50:10 +01004791 TEST_ASSERT(ssl_tls12_populate_session(
4792 &session, ticket_len, crt_file) == 0);
Jerry Yu1ac476c2022-07-15 11:22:40 +08004793 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004794 TEST_ASSERT(mbedtls_ssl_session_save(&session, NULL, 0, &good_len)
4795 == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL);
Manuel Pégourié-Gonnardf5fa0aa2019-05-23 10:38:11 +02004796
4797 /* Try all possible bad lengths */
Gilles Peskine449bd832023-01-11 14:50:10 +01004798 for (bad_len = 1; bad_len < good_len; bad_len++) {
Manuel Pégourié-Gonnardf5fa0aa2019-05-23 10:38:11 +02004799 /* Allocate exact size so that asan/valgrind can detect any overwrite */
Gilles Peskine449bd832023-01-11 14:50:10 +01004800 mbedtls_free(buf);
4801 TEST_ASSERT((buf = mbedtls_calloc(1, bad_len)) != NULL);
4802 TEST_ASSERT(mbedtls_ssl_session_save(&session, buf, bad_len,
4803 &test_len)
4804 == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL);
4805 TEST_ASSERT(test_len == good_len);
Manuel Pégourié-Gonnardf5fa0aa2019-05-23 10:38:11 +02004806 }
4807
4808exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004809 mbedtls_ssl_session_free(&session);
4810 mbedtls_free(buf);
Manuel Pégourié-Gonnardf5fa0aa2019-05-23 10:38:11 +02004811}
4812/* END_CASE */
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02004813
4814/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004815void ssl_serialize_session_load_buf_size(int ticket_len, char *crt_file,
4816 int endpoint_type, int tls_version)
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02004817{
4818 mbedtls_ssl_session session;
4819 unsigned char *good_buf = NULL, *bad_buf = NULL;
4820 size_t good_len, bad_len;
4821
4822 /*
4823 * Test that session_load() fails cleanly on small buffers
4824 */
4825
Gilles Peskine449bd832023-01-11 14:50:10 +01004826 mbedtls_ssl_session_init(&session);
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02004827
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +02004828 /* Prepare serialized session data */
Jerry Yu6e8fec22022-07-15 10:37:02 +08004829 ((void) endpoint_type);
4830 ((void) tls_version);
4831#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004832 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
4833 TEST_ASSERT(ssl_tls13_populate_session(
4834 &session, 0, endpoint_type) == 0);
4835 } else
Jerry Yu6e8fec22022-07-15 10:37:02 +08004836#endif
4837 {
Gilles Peskine449bd832023-01-11 14:50:10 +01004838 TEST_ASSERT(ssl_tls12_populate_session(
4839 &session, ticket_len, crt_file) == 0);
Jerry Yu6e8fec22022-07-15 10:37:02 +08004840 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004841 TEST_ASSERT(mbedtls_ssl_session_save(&session, NULL, 0, &good_len)
4842 == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL);
4843 TEST_ASSERT((good_buf = mbedtls_calloc(1, good_len)) != NULL);
4844 TEST_ASSERT(mbedtls_ssl_session_save(&session, good_buf, good_len,
4845 &good_len) == 0);
4846 mbedtls_ssl_session_free(&session);
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02004847
4848 /* Try all possible bad lengths */
Gilles Peskine449bd832023-01-11 14:50:10 +01004849 for (bad_len = 0; bad_len < good_len; bad_len++) {
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02004850 /* Allocate exact size so that asan/valgrind can detect any overread */
Gilles Peskine449bd832023-01-11 14:50:10 +01004851 mbedtls_free(bad_buf);
4852 bad_buf = mbedtls_calloc(1, bad_len ? bad_len : 1);
4853 TEST_ASSERT(bad_buf != NULL);
4854 memcpy(bad_buf, good_buf, bad_len);
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02004855
Gilles Peskine449bd832023-01-11 14:50:10 +01004856 TEST_ASSERT(mbedtls_ssl_session_load(&session, bad_buf, bad_len)
4857 == MBEDTLS_ERR_SSL_BAD_INPUT_DATA);
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02004858 }
4859
4860exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004861 mbedtls_ssl_session_free(&session);
4862 mbedtls_free(good_buf);
4863 mbedtls_free(bad_buf);
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02004864}
4865/* END_CASE */
Hanno Becker861d0bb2019-05-21 16:39:30 +01004866
Hanno Becker363b6462019-05-29 12:44:28 +01004867/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01004868void ssl_session_serialize_version_check(int corrupt_major,
4869 int corrupt_minor,
4870 int corrupt_patch,
4871 int corrupt_config,
4872 int endpoint_type,
4873 int tls_version)
Hanno Becker861d0bb2019-05-21 16:39:30 +01004874{
Gilles Peskine449bd832023-01-11 14:50:10 +01004875 unsigned char serialized_session[2048];
Hanno Becker363b6462019-05-29 12:44:28 +01004876 size_t serialized_session_len;
Hanno Beckerfe1275e2019-05-29 12:45:21 +01004877 unsigned cur_byte;
Hanno Becker861d0bb2019-05-21 16:39:30 +01004878 mbedtls_ssl_session session;
Hanno Beckerfe1275e2019-05-29 12:45:21 +01004879 uint8_t should_corrupt_byte[] = { corrupt_major == 1,
4880 corrupt_minor == 1,
4881 corrupt_patch == 1,
4882 corrupt_config == 1,
4883 corrupt_config == 1 };
4884
Gilles Peskine449bd832023-01-11 14:50:10 +01004885 mbedtls_ssl_session_init(&session);
Jerry Yu534ff402022-07-14 16:43:43 +08004886 ((void) endpoint_type);
4887 ((void) tls_version);
4888#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004889 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
4890 TEST_ASSERT(ssl_tls13_populate_session(
4891 &session, 0, endpoint_type) == 0);
4892 } else
Jerry Yu534ff402022-07-14 16:43:43 +08004893#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01004894 TEST_ASSERT(ssl_tls12_populate_session(&session, 0, NULL) == 0);
Jerry Yu534ff402022-07-14 16:43:43 +08004895
Hanno Becker861d0bb2019-05-21 16:39:30 +01004896
Hanno Beckerfe1275e2019-05-29 12:45:21 +01004897 /* Infer length of serialized session. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004898 TEST_ASSERT(mbedtls_ssl_session_save(&session,
4899 serialized_session,
4900 sizeof(serialized_session),
4901 &serialized_session_len) == 0);
Hanno Becker861d0bb2019-05-21 16:39:30 +01004902
Gilles Peskine449bd832023-01-11 14:50:10 +01004903 mbedtls_ssl_session_free(&session);
Hanno Becker861d0bb2019-05-21 16:39:30 +01004904
Hanno Beckerfe1275e2019-05-29 12:45:21 +01004905 /* Without any modification, we should be able to successfully
Hanno Becker363b6462019-05-29 12:44:28 +01004906 * de-serialize the session - double-check that. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004907 TEST_ASSERT(mbedtls_ssl_session_load(&session,
4908 serialized_session,
4909 serialized_session_len) == 0);
4910 mbedtls_ssl_session_free(&session);
Hanno Becker861d0bb2019-05-21 16:39:30 +01004911
Hanno Beckerfe1275e2019-05-29 12:45:21 +01004912 /* Go through the bytes in the serialized session header and
4913 * corrupt them bit-by-bit. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004914 for (cur_byte = 0; cur_byte < sizeof(should_corrupt_byte); cur_byte++) {
Hanno Beckerfe1275e2019-05-29 12:45:21 +01004915 int cur_bit;
Gilles Peskine449bd832023-01-11 14:50:10 +01004916 unsigned char * const byte = &serialized_session[cur_byte];
Hanno Beckerfe1275e2019-05-29 12:45:21 +01004917
Gilles Peskine449bd832023-01-11 14:50:10 +01004918 if (should_corrupt_byte[cur_byte] == 0) {
Hanno Beckerfe1275e2019-05-29 12:45:21 +01004919 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01004920 }
Hanno Beckerfe1275e2019-05-29 12:45:21 +01004921
Gilles Peskine449bd832023-01-11 14:50:10 +01004922 for (cur_bit = 0; cur_bit < CHAR_BIT; cur_bit++) {
Hanno Beckerfe1275e2019-05-29 12:45:21 +01004923 unsigned char const corrupted_bit = 0x1u << cur_bit;
4924 /* Modify a single bit in the serialized session. */
4925 *byte ^= corrupted_bit;
4926
4927 /* Attempt to deserialize */
Gilles Peskine449bd832023-01-11 14:50:10 +01004928 TEST_ASSERT(mbedtls_ssl_session_load(&session,
4929 serialized_session,
4930 serialized_session_len) ==
4931 MBEDTLS_ERR_SSL_VERSION_MISMATCH);
Hanno Beckerfe1275e2019-05-29 12:45:21 +01004932
4933 /* Undo the change */
4934 *byte ^= corrupted_bit;
4935 }
Hanno Becker861d0bb2019-05-21 16:39:30 +01004936 }
4937
Hanno Becker861d0bb2019-05-21 16:39:30 +01004938}
4939/* END_CASE */
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01004940
Ronald Crone68ab4f2022-10-05 12:46:29 +02004941/* BEGIN_CASE depends_on:MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:!MBEDTLS_SSL_PROTO_TLS1_3:MBEDTLS_PKCS1_V15:MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Gilles Peskine449bd832023-01-11 14:50:10 +01004942void mbedtls_endpoint_sanity(int endpoint_type)
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01004943{
4944 enum { BUFFSIZE = 1024 };
4945 mbedtls_endpoint ep;
4946 int ret = -1;
Andrzej Kurek780dc182022-06-10 08:57:19 -04004947 handshake_test_options options;
Gilles Peskine449bd832023-01-11 14:50:10 +01004948 init_handshake_options(&options);
Andrzej Kurek780dc182022-06-10 08:57:19 -04004949 options.pk_alg = MBEDTLS_PK_RSA;
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01004950
Gilles Peskine449bd832023-01-11 14:50:10 +01004951 ret = mbedtls_endpoint_init(NULL, endpoint_type, &options,
4952 NULL, NULL, NULL, NULL);
4953 TEST_ASSERT(MBEDTLS_ERR_SSL_BAD_INPUT_DATA == ret);
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01004954
Gilles Peskine449bd832023-01-11 14:50:10 +01004955 ret = mbedtls_endpoint_certificate_init(NULL, options.pk_alg, 0, 0, 0);
4956 TEST_ASSERT(MBEDTLS_ERR_SSL_BAD_INPUT_DATA == ret);
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01004957
Gilles Peskine449bd832023-01-11 14:50:10 +01004958 ret = mbedtls_endpoint_init(&ep, endpoint_type, &options,
4959 NULL, NULL, NULL, NULL);
4960 TEST_ASSERT(ret == 0);
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01004961
4962exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01004963 mbedtls_endpoint_free(&ep, NULL);
4964 free_handshake_options(&options);
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01004965}
4966/* END_CASE */
4967
Ronald Crone68ab4f2022-10-05 12:46:29 +02004968/* BEGIN_CASE depends_on:MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_PKCS1_V15:MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA:MBEDTLS_ECP_C */
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01004969void move_handshake_to_state(int endpoint_type, int state, int need_pass)
4970{
4971 enum { BUFFSIZE = 1024 };
4972 mbedtls_endpoint base_ep, second_ep;
4973 int ret = -1;
Andrzej Kurek780dc182022-06-10 08:57:19 -04004974 handshake_test_options options;
Gilles Peskine449bd832023-01-11 14:50:10 +01004975 init_handshake_options(&options);
Andrzej Kurek780dc182022-06-10 08:57:19 -04004976 options.pk_alg = MBEDTLS_PK_RSA;
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01004977
Gilles Peskine449bd832023-01-11 14:50:10 +01004978 USE_PSA_INIT();
4979 mbedtls_platform_zeroize(&base_ep, sizeof(base_ep));
4980 mbedtls_platform_zeroize(&second_ep, sizeof(second_ep));
Neil Armstrong06baf042022-04-14 16:21:15 +02004981
Gilles Peskine449bd832023-01-11 14:50:10 +01004982 ret = mbedtls_endpoint_init(&base_ep, endpoint_type, &options,
4983 NULL, NULL, NULL, NULL);
4984 TEST_ASSERT(ret == 0);
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01004985
Gilles Peskine449bd832023-01-11 14:50:10 +01004986 ret = mbedtls_endpoint_init(&second_ep,
4987 (endpoint_type == MBEDTLS_SSL_IS_SERVER) ?
4988 MBEDTLS_SSL_IS_CLIENT : MBEDTLS_SSL_IS_SERVER,
4989 &options, NULL, NULL, NULL, NULL);
Andrzej Kurek780dc182022-06-10 08:57:19 -04004990
Gilles Peskine449bd832023-01-11 14:50:10 +01004991 TEST_ASSERT(ret == 0);
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01004992
Gilles Peskine449bd832023-01-11 14:50:10 +01004993 ret = mbedtls_mock_socket_connect(&(base_ep.socket),
4994 &(second_ep.socket),
4995 BUFFSIZE);
4996 TEST_ASSERT(ret == 0);
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01004997
Gilles Peskine449bd832023-01-11 14:50:10 +01004998 ret = mbedtls_move_handshake_to_state(&(base_ep.ssl),
4999 &(second_ep.ssl),
5000 state);
5001 if (need_pass) {
5002 TEST_ASSERT(ret == 0 ||
5003 ret == MBEDTLS_ERR_SSL_WANT_READ ||
5004 ret == MBEDTLS_ERR_SSL_WANT_WRITE);
5005 TEST_ASSERT(base_ep.ssl.state == state);
5006 } else {
5007 TEST_ASSERT(ret != 0 &&
5008 ret != MBEDTLS_ERR_SSL_WANT_READ &&
5009 ret != MBEDTLS_ERR_SSL_WANT_WRITE);
5010 TEST_ASSERT(base_ep.ssl.state != state);
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01005011 }
5012
5013exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005014 free_handshake_options(&options);
5015 mbedtls_endpoint_free(&base_ep, NULL);
5016 mbedtls_endpoint_free(&second_ep, NULL);
5017 USE_PSA_DONE();
Piotr Nowicki2a1f1782020-01-13 09:42:10 +01005018}
5019/* END_CASE */
Andrzej Kurekf40daa32020-02-04 09:00:01 -05005020
Ronald Crone68ab4f2022-10-05 12:46:29 +02005021/* BEGIN_CASE depends_on:MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED:MBEDTLS_PKCS1_V15:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA:MBEDTLS_ECP_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01005022void handshake_version(int dtls, int client_min_version, int client_max_version,
5023 int server_min_version, int server_max_version,
5024 int expected_negotiated_version)
Andrzej Kurekf40daa32020-02-04 09:00:01 -05005025{
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05005026 handshake_test_options options;
Gilles Peskine449bd832023-01-11 14:50:10 +01005027 init_handshake_options(&options);
Andrzej Kurekda2b6782020-02-12 07:56:36 -05005028
Paul Elliottc8570442020-04-15 17:00:50 +01005029 options.client_min_version = client_min_version;
5030 options.client_max_version = client_max_version;
5031 options.server_min_version = server_min_version;
5032 options.server_max_version = server_max_version;
Paul Elliottc8570442020-04-15 17:00:50 +01005033 options.expected_negotiated_version = expected_negotiated_version;
5034
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05005035 options.dtls = dtls;
Gilles Peskine449bd832023-01-11 14:50:10 +01005036 perform_handshake(&options);
Andrzej Kurekf40daa32020-02-04 09:00:01 -05005037
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05005038 /* The goto below is used to avoid an "unused label" warning.*/
5039 goto exit;
Andrzej Kureke11acb22022-06-27 06:11:34 -04005040
5041exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005042 free_handshake_options(&options);
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05005043}
5044/* END_CASE */
Andrzej Kurek9e9efdc2020-02-26 05:25:23 -05005045
Ronald Crone68ab4f2022-10-05 12:46:29 +02005046/* BEGIN_CASE depends_on:MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Gilles Peskine449bd832023-01-11 14:50:10 +01005047void handshake_psk_cipher(char *cipher, int pk_alg, data_t *psk_str, int dtls)
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05005048{
5049 handshake_test_options options;
Gilles Peskine449bd832023-01-11 14:50:10 +01005050 init_handshake_options(&options);
Andrzej Kurekf40daa32020-02-04 09:00:01 -05005051
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05005052 options.cipher = cipher;
5053 options.dtls = dtls;
5054 options.psk_str = psk_str;
5055 options.pk_alg = pk_alg;
Andrzej Kurekcc5169c2020-02-04 09:04:56 -05005056
Gilles Peskine449bd832023-01-11 14:50:10 +01005057 perform_handshake(&options);
Andrzej Kurek316da1f2020-02-26 09:03:47 -05005058
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05005059 /* The goto below is used to avoid an "unused label" warning.*/
5060 goto exit;
Andrzej Kureke11acb22022-06-27 06:11:34 -04005061
Andrzej Kurekddb8cd62022-07-04 16:07:28 -04005062exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005063 free_handshake_options(&options);
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05005064}
5065/* END_CASE */
Andrzej Kurek316da1f2020-02-26 09:03:47 -05005066
Ronald Crone68ab4f2022-10-05 12:46:29 +02005067/* BEGIN_CASE depends_on:MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Gilles Peskine449bd832023-01-11 14:50:10 +01005068void handshake_cipher(char *cipher, int pk_alg, int dtls)
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05005069{
Gilles Peskine449bd832023-01-11 14:50:10 +01005070 test_handshake_psk_cipher(cipher, pk_alg, NULL, dtls);
Andrzej Kurekf40daa32020-02-04 09:00:01 -05005071
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05005072 /* The goto below is used to avoid an "unused label" warning.*/
5073 goto exit;
5074}
5075/* END_CASE */
Andrzej Kurekf40daa32020-02-04 09:00:01 -05005076
Ronald Crone68ab4f2022-10-05 12:46:29 +02005077/* BEGIN_CASE depends_on:MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Gilles Peskine449bd832023-01-11 14:50:10 +01005078void handshake_ciphersuite_select(char *cipher, int pk_alg, data_t *psk_str,
5079 int psa_alg, int psa_alg2, int psa_usage,
5080 int expected_handshake_result,
5081 int expected_ciphersuite)
Neil Armstrong8c52ed82022-05-27 13:14:55 +02005082{
5083 handshake_test_options options;
Gilles Peskine449bd832023-01-11 14:50:10 +01005084 init_handshake_options(&options);
Neil Armstrong8c52ed82022-05-27 13:14:55 +02005085
5086 options.cipher = cipher;
Neil Armstrongdb134972022-06-30 09:06:28 +02005087 options.psk_str = psk_str;
Neil Armstrong8c52ed82022-05-27 13:14:55 +02005088 options.pk_alg = pk_alg;
5089 options.opaque_alg = psa_alg;
5090 options.opaque_alg2 = psa_alg2;
5091 options.opaque_usage = psa_usage;
5092 options.expected_handshake_result = expected_handshake_result;
5093 options.expected_ciphersuite = expected_ciphersuite;
Gilles Peskine449bd832023-01-11 14:50:10 +01005094 perform_handshake(&options);
Neil Armstrong8c52ed82022-05-27 13:14:55 +02005095
5096 /* The goto below is used to avoid an "unused label" warning.*/
5097 goto exit;
Andrzej Kurekddb8cd62022-07-04 16:07:28 -04005098
5099exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005100 free_handshake_options(&options);
Neil Armstrong8c52ed82022-05-27 13:14:55 +02005101}
5102/* END_CASE */
5103
Ronald Crone68ab4f2022-10-05 12:46:29 +02005104/* BEGIN_CASE depends_on:MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED:MBEDTLS_PKCS1_V15:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Gilles Peskine449bd832023-01-11 14:50:10 +01005105void app_data(int mfl, int cli_msg_len, int srv_msg_len,
5106 int expected_cli_fragments,
5107 int expected_srv_fragments, int dtls)
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05005108{
5109 handshake_test_options options;
Gilles Peskine449bd832023-01-11 14:50:10 +01005110 init_handshake_options(&options);
Andrzej Kurekda2b6782020-02-12 07:56:36 -05005111
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05005112 options.mfl = mfl;
5113 options.cli_msg_len = cli_msg_len;
5114 options.srv_msg_len = srv_msg_len;
5115 options.expected_cli_fragments = expected_cli_fragments;
5116 options.expected_srv_fragments = expected_srv_fragments;
5117 options.dtls = dtls;
Gilles Peskine449bd832023-01-11 14:50:10 +01005118#if !defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ronald Cron21a1b2d2022-06-15 17:11:35 +02005119 options.expected_negotiated_version = MBEDTLS_SSL_VERSION_TLS1_3;
5120#endif
Andrzej Kurekda2b6782020-02-12 07:56:36 -05005121
Gilles Peskine449bd832023-01-11 14:50:10 +01005122 perform_handshake(&options);
Andrzej Kurek780dc182022-06-10 08:57:19 -04005123
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05005124 /* The goto below is used to avoid an "unused label" warning.*/
5125 goto exit;
Andrzej Kurek780dc182022-06-10 08:57:19 -04005126
5127exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005128 free_handshake_options(&options);
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05005129}
5130/* END_CASE */
Andrzej Kurekda2b6782020-02-12 07:56:36 -05005131
Ronald Crone68ab4f2022-10-05 12:46:29 +02005132/* BEGIN_CASE depends_on:MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED:MBEDTLS_PKCS1_V15:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA:MBEDTLS_ECP_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01005133void app_data_tls(int mfl, int cli_msg_len, int srv_msg_len,
5134 int expected_cli_fragments,
5135 int expected_srv_fragments)
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05005136{
Gilles Peskine449bd832023-01-11 14:50:10 +01005137 test_app_data(mfl, cli_msg_len, srv_msg_len, expected_cli_fragments,
5138 expected_srv_fragments, 0);
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05005139 /* The goto below is used to avoid an "unused label" warning.*/
5140 goto exit;
5141}
5142/* END_CASE */
Andrzej Kurekda2b6782020-02-12 07:56:36 -05005143
Ronald Crone68ab4f2022-10-05 12:46:29 +02005144/* BEGIN_CASE depends_on:MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED:!MBEDTLS_SSL_PROTO_TLS1_3:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_SSL_PROTO_DTLS:MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Gilles Peskine449bd832023-01-11 14:50:10 +01005145void app_data_dtls(int mfl, int cli_msg_len, int srv_msg_len,
5146 int expected_cli_fragments,
5147 int expected_srv_fragments)
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05005148{
Gilles Peskine449bd832023-01-11 14:50:10 +01005149 test_app_data(mfl, cli_msg_len, srv_msg_len, expected_cli_fragments,
5150 expected_srv_fragments, 1);
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05005151 /* The goto below is used to avoid an "unused label" warning.*/
5152 goto exit;
5153}
5154/* END_CASE */
Andrzej Kurekda2b6782020-02-12 07:56:36 -05005155
Ronald Crone68ab4f2022-10-05 12:46:29 +02005156/* BEGIN_CASE depends_on:MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED:!MBEDTLS_SSL_PROTO_TLS1_3:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_SSL_PROTO_DTLS:MBEDTLS_SSL_RENEGOTIATION:MBEDTLS_SSL_CONTEXT_SERIALIZATION:MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Gilles Peskine449bd832023-01-11 14:50:10 +01005157void handshake_serialization()
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05005158{
5159 handshake_test_options options;
Gilles Peskine449bd832023-01-11 14:50:10 +01005160 init_handshake_options(&options);
Andrzej Kurekda2b6782020-02-12 07:56:36 -05005161
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05005162 options.serialize = 1;
5163 options.dtls = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005164 perform_handshake(&options);
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05005165 /* The goto below is used to avoid an "unused label" warning.*/
5166 goto exit;
Andrzej Kurek6e518ab2022-06-11 05:08:38 -04005167exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005168 free_handshake_options(&options);
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05005169}
5170/* END_CASE */
Andrzej Kurekda2b6782020-02-12 07:56:36 -05005171
Ronald Crone68ab4f2022-10-05 12:46:29 +02005172/* BEGIN_CASE depends_on:MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED:!MBEDTLS_SSL_PROTO_TLS1_3:MBEDTLS_PKCS1_V15:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_DEBUG_C:MBEDTLS_SSL_MAX_FRAGMENT_LENGTH:MBEDTLS_CIPHER_MODE_CBC:MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Gilles Peskine449bd832023-01-11 14:50:10 +01005173void handshake_fragmentation(int mfl,
5174 int expected_srv_hs_fragmentation,
5175 int expected_cli_hs_fragmentation)
Piotr Nowickibde7ee82020-02-21 10:59:50 +01005176{
5177 handshake_test_options options;
5178 log_pattern srv_pattern, cli_pattern;
5179
5180 srv_pattern.pattern = cli_pattern.pattern = "found fragmented DTLS handshake";
5181 srv_pattern.counter = 0;
5182 cli_pattern.counter = 0;
5183
Gilles Peskine449bd832023-01-11 14:50:10 +01005184 init_handshake_options(&options);
Piotr Nowickibde7ee82020-02-21 10:59:50 +01005185 options.dtls = 1;
5186 options.mfl = mfl;
Darryl Greenaad82f92019-12-02 10:53:11 +00005187 /* Set cipher to one using CBC so that record splitting can be tested */
5188 options.cipher = "TLS-DHE-RSA-WITH-AES-256-CBC-SHA256";
Piotr Nowickibde7ee82020-02-21 10:59:50 +01005189 options.srv_auth_mode = MBEDTLS_SSL_VERIFY_REQUIRED;
5190 options.srv_log_obj = &srv_pattern;
5191 options.cli_log_obj = &cli_pattern;
5192 options.srv_log_fun = log_analyzer;
5193 options.cli_log_fun = log_analyzer;
5194
Gilles Peskine449bd832023-01-11 14:50:10 +01005195 perform_handshake(&options);
Piotr Nowickibde7ee82020-02-21 10:59:50 +01005196
5197 /* Test if the server received a fragmented handshake */
Gilles Peskine449bd832023-01-11 14:50:10 +01005198 if (expected_srv_hs_fragmentation) {
5199 TEST_ASSERT(srv_pattern.counter >= 1);
Piotr Nowickibde7ee82020-02-21 10:59:50 +01005200 }
5201 /* Test if the client received a fragmented handshake */
Gilles Peskine449bd832023-01-11 14:50:10 +01005202 if (expected_cli_hs_fragmentation) {
5203 TEST_ASSERT(cli_pattern.counter >= 1);
Piotr Nowickibde7ee82020-02-21 10:59:50 +01005204 }
Andrzej Kurek780dc182022-06-10 08:57:19 -04005205
5206exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005207 free_handshake_options(&options);
Piotr Nowickibde7ee82020-02-21 10:59:50 +01005208}
5209/* END_CASE */
5210
Ronald Crone68ab4f2022-10-05 12:46:29 +02005211/* BEGIN_CASE depends_on:MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED:!MBEDTLS_SSL_PROTO_TLS1_3:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_SSL_PROTO_DTLS:MBEDTLS_SSL_RENEGOTIATION:MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Gilles Peskine449bd832023-01-11 14:50:10 +01005212void renegotiation(int legacy_renegotiation)
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05005213{
5214 handshake_test_options options;
Gilles Peskine449bd832023-01-11 14:50:10 +01005215 init_handshake_options(&options);
Andrzej Kurekda2b6782020-02-12 07:56:36 -05005216
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05005217 options.renegotiate = 1;
5218 options.legacy_renegotiation = legacy_renegotiation;
5219 options.dtls = 1;
Andrzej Kurek316da1f2020-02-26 09:03:47 -05005220
Gilles Peskine449bd832023-01-11 14:50:10 +01005221 perform_handshake(&options);
Andrzej Kurek780dc182022-06-10 08:57:19 -04005222
Andrzej Kurek8a6ff152020-02-26 09:10:14 -05005223 /* The goto below is used to avoid an "unused label" warning.*/
5224 goto exit;
Andrzej Kurekddb8cd62022-07-04 16:07:28 -04005225exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005226 free_handshake_options(&options);
Andrzej Kurekf40daa32020-02-04 09:00:01 -05005227}
5228/* END_CASE */
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05005229
Ronald Crone68ab4f2022-10-05 12:46:29 +02005230/* BEGIN_CASE depends_on:MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED:!MBEDTLS_SSL_PROTO_TLS1_3:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Gilles Peskine449bd832023-01-11 14:50:10 +01005231void resize_buffers(int mfl, int renegotiation, int legacy_renegotiation,
5232 int serialize, int dtls, char *cipher)
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05005233{
5234 handshake_test_options options;
Gilles Peskine449bd832023-01-11 14:50:10 +01005235 init_handshake_options(&options);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05005236
5237 options.mfl = mfl;
Andrzej Kurek8ea68722020-04-03 06:40:47 -04005238 options.cipher = cipher;
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05005239 options.renegotiate = renegotiation;
5240 options.legacy_renegotiation = legacy_renegotiation;
5241 options.serialize = serialize;
5242 options.dtls = dtls;
5243 options.resize_buffers = 1;
5244
Gilles Peskine449bd832023-01-11 14:50:10 +01005245 perform_handshake(&options);
Andrzej Kurek780dc182022-06-10 08:57:19 -04005246
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05005247 /* The goto below is used to avoid an "unused label" warning.*/
5248 goto exit;
Andrzej Kurek780dc182022-06-10 08:57:19 -04005249exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005250 free_handshake_options(&options);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05005251}
5252/* END_CASE */
5253
Ronald Crone68ab4f2022-10-05 12:46:29 +02005254/* BEGIN_CASE depends_on:MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED:!MBEDTLS_SSL_PROTO_TLS1_3:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH:MBEDTLS_SSL_CONTEXT_SERIALIZATION:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_SSL_PROTO_DTLS:MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Gilles Peskine449bd832023-01-11 14:50:10 +01005255void resize_buffers_serialize_mfl(int mfl)
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05005256{
Gilles Peskine449bd832023-01-11 14:50:10 +01005257 test_resize_buffers(mfl, 0, MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION, 1, 1,
5258 (char *) "");
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05005259
5260 /* The goto below is used to avoid an "unused label" warning.*/
5261 goto exit;
5262}
5263/* END_CASE */
5264
Ronald Crone68ab4f2022-10-05 12:46:29 +02005265/* BEGIN_CASE depends_on:MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED:!MBEDTLS_SSL_PROTO_TLS1_3:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH:MBEDTLS_SSL_RENEGOTIATION:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Gilles Peskine449bd832023-01-11 14:50:10 +01005266void resize_buffers_renegotiate_mfl(int mfl, int legacy_renegotiation,
5267 char *cipher)
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05005268{
Gilles Peskine449bd832023-01-11 14:50:10 +01005269 test_resize_buffers(mfl, 1, legacy_renegotiation, 0, 1, cipher);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05005270
5271 /* The goto below is used to avoid an "unused label" warning.*/
5272 goto exit;
5273}
5274/* END_CASE */
Manuel Pégourié-Gonnard045f0942020-07-02 11:34:02 +02005275
Ronald Cron73fe8df2022-10-05 14:31:43 +02005276/* BEGIN_CASE depends_on:MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Hanno Becker6667ffd2021-04-19 21:59:22 +01005277void test_multiple_psks()
5278{
5279 unsigned char psk0[10] = { 0 };
5280 unsigned char psk0_identity[] = { 'f', 'o', 'o' };
5281
5282 unsigned char psk1[10] = { 0 };
5283 unsigned char psk1_identity[] = { 'b', 'a', 'r' };
5284
5285 mbedtls_ssl_config conf;
5286
Gilles Peskine449bd832023-01-11 14:50:10 +01005287 USE_PSA_INIT();
5288 mbedtls_ssl_config_init(&conf);
Hanno Becker6667ffd2021-04-19 21:59:22 +01005289
Gilles Peskine449bd832023-01-11 14:50:10 +01005290 TEST_ASSERT(mbedtls_ssl_conf_psk(&conf,
5291 psk0, sizeof(psk0),
5292 psk0_identity, sizeof(psk0_identity)) == 0);
5293 TEST_ASSERT(mbedtls_ssl_conf_psk(&conf,
5294 psk1, sizeof(psk1),
5295 psk1_identity, sizeof(psk1_identity)) ==
5296 MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE);
Hanno Becker6667ffd2021-04-19 21:59:22 +01005297
5298exit:
5299
Gilles Peskine449bd832023-01-11 14:50:10 +01005300 mbedtls_ssl_config_free(&conf);
Neil Armstrong4c3b4e02022-05-03 09:24:26 +02005301
Gilles Peskine449bd832023-01-11 14:50:10 +01005302 USE_PSA_DONE();
Hanno Becker6667ffd2021-04-19 21:59:22 +01005303}
5304/* END_CASE */
5305
Ronald Cron73fe8df2022-10-05 14:31:43 +02005306/* BEGIN_CASE depends_on:MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED:MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01005307void test_multiple_psks_opaque(int mode)
Hanno Becker6667ffd2021-04-19 21:59:22 +01005308{
5309 /*
5310 * Mode 0: Raw PSK, then opaque PSK
5311 * Mode 1: Opaque PSK, then raw PSK
5312 * Mode 2: 2x opaque PSK
5313 */
5314
5315 unsigned char psk0_raw[10] = { 0 };
5316 unsigned char psk0_raw_identity[] = { 'f', 'o', 'o' };
5317
Gilles Peskine449bd832023-01-11 14:50:10 +01005318 mbedtls_svc_key_id_t psk0_opaque = mbedtls_svc_key_id_make(0x1, (psa_key_id_t) 1);
Andrzej Kurek03e01462022-01-03 12:53:24 +01005319
Hanno Becker6667ffd2021-04-19 21:59:22 +01005320 unsigned char psk0_opaque_identity[] = { 'f', 'o', 'o' };
5321
5322 unsigned char psk1_raw[10] = { 0 };
5323 unsigned char psk1_raw_identity[] = { 'b', 'a', 'r' };
5324
Gilles Peskine449bd832023-01-11 14:50:10 +01005325 mbedtls_svc_key_id_t psk1_opaque = mbedtls_svc_key_id_make(0x1, (psa_key_id_t) 2);
Andrzej Kurek03e01462022-01-03 12:53:24 +01005326
Hanno Becker6667ffd2021-04-19 21:59:22 +01005327 unsigned char psk1_opaque_identity[] = { 'b', 'a', 'r' };
5328
5329 mbedtls_ssl_config conf;
5330
Gilles Peskine449bd832023-01-11 14:50:10 +01005331 USE_PSA_INIT();
5332 mbedtls_ssl_config_init(&conf);
Hanno Becker6667ffd2021-04-19 21:59:22 +01005333
Gilles Peskine449bd832023-01-11 14:50:10 +01005334 switch (mode) {
Hanno Becker6667ffd2021-04-19 21:59:22 +01005335 case 0:
5336
Gilles Peskine449bd832023-01-11 14:50:10 +01005337 TEST_ASSERT(mbedtls_ssl_conf_psk(&conf,
5338 psk0_raw, sizeof(psk0_raw),
5339 psk0_raw_identity, sizeof(psk0_raw_identity))
5340 == 0);
5341 TEST_ASSERT(mbedtls_ssl_conf_psk_opaque(&conf,
5342 psk1_opaque,
5343 psk1_opaque_identity,
5344 sizeof(psk1_opaque_identity))
5345 == MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE);
Hanno Becker6667ffd2021-04-19 21:59:22 +01005346 break;
5347
5348 case 1:
5349
Gilles Peskine449bd832023-01-11 14:50:10 +01005350 TEST_ASSERT(mbedtls_ssl_conf_psk_opaque(&conf,
5351 psk0_opaque,
5352 psk0_opaque_identity,
5353 sizeof(psk0_opaque_identity))
5354 == 0);
5355 TEST_ASSERT(mbedtls_ssl_conf_psk(&conf,
5356 psk1_raw, sizeof(psk1_raw),
5357 psk1_raw_identity, sizeof(psk1_raw_identity))
5358 == MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE);
Hanno Becker6667ffd2021-04-19 21:59:22 +01005359
5360 break;
5361
5362 case 2:
5363
Gilles Peskine449bd832023-01-11 14:50:10 +01005364 TEST_ASSERT(mbedtls_ssl_conf_psk_opaque(&conf,
5365 psk0_opaque,
5366 psk0_opaque_identity,
5367 sizeof(psk0_opaque_identity))
5368 == 0);
5369 TEST_ASSERT(mbedtls_ssl_conf_psk_opaque(&conf,
5370 psk1_opaque,
5371 psk1_opaque_identity,
5372 sizeof(psk1_opaque_identity))
5373 == MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE);
Hanno Becker6667ffd2021-04-19 21:59:22 +01005374
5375 break;
5376
5377 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005378 TEST_ASSERT(0);
Hanno Becker6667ffd2021-04-19 21:59:22 +01005379 break;
5380 }
5381
5382exit:
5383
Gilles Peskine449bd832023-01-11 14:50:10 +01005384 mbedtls_ssl_config_free(&conf);
5385 USE_PSA_DONE();
Hanno Becker6667ffd2021-04-19 21:59:22 +01005386
5387}
5388/* END_CASE */
Brett Warren7f813d52021-10-20 23:08:38 +01005389
Ronald Cron37bdaab2022-03-30 16:45:51 +02005390/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005391void conf_version(int endpoint, int transport,
5392 int min_tls_version, int max_tls_version,
5393 int expected_ssl_setup_result)
Ronald Cron37bdaab2022-03-30 16:45:51 +02005394{
5395 mbedtls_ssl_config conf;
5396 mbedtls_ssl_context ssl;
5397
Gilles Peskine449bd832023-01-11 14:50:10 +01005398 mbedtls_ssl_config_init(&conf);
5399 mbedtls_ssl_init(&ssl);
Ronald Cron37bdaab2022-03-30 16:45:51 +02005400
Gilles Peskine449bd832023-01-11 14:50:10 +01005401 mbedtls_ssl_conf_endpoint(&conf, endpoint);
5402 mbedtls_ssl_conf_transport(&conf, transport);
5403 mbedtls_ssl_conf_min_tls_version(&conf, min_tls_version);
5404 mbedtls_ssl_conf_max_tls_version(&conf, max_tls_version);
Ronald Cron37bdaab2022-03-30 16:45:51 +02005405
Gilles Peskine449bd832023-01-11 14:50:10 +01005406 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == expected_ssl_setup_result);
Ronald Cron37bdaab2022-03-30 16:45:51 +02005407
Gilles Peskine449bd832023-01-11 14:50:10 +01005408 mbedtls_ssl_free(&ssl);
5409 mbedtls_ssl_config_free(&conf);
Ronald Cron37bdaab2022-03-30 16:45:51 +02005410}
5411/* END_CASE */
5412
Brett Warren7f813d52021-10-20 23:08:38 +01005413/* BEGIN_CASE depends_on:MBEDTLS_ECP_C:!MBEDTLS_DEPRECATED_REMOVED:!MBEDTLS_DEPRECATED_WARNING:MBEDTLS_ECP_DP_SECP192R1_ENABLED:MBEDTLS_ECP_DP_SECP224R1_ENABLED:MBEDTLS_ECP_DP_SECP256R1_ENABLED */
5414void conf_curve()
5415{
5416
5417 mbedtls_ecp_group_id curve_list[] = { MBEDTLS_ECP_DP_SECP192R1,
5418 MBEDTLS_ECP_DP_SECP224R1,
5419 MBEDTLS_ECP_DP_SECP256R1,
5420 MBEDTLS_ECP_DP_NONE };
Tom Cosgrovea327b522022-08-03 08:33:06 +01005421 uint16_t iana_tls_group_list[] = { MBEDTLS_SSL_IANA_TLS_GROUP_SECP192R1,
5422 MBEDTLS_SSL_IANA_TLS_GROUP_SECP224R1,
5423 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
5424 MBEDTLS_SSL_IANA_TLS_GROUP_NONE };
Brett Warren7f813d52021-10-20 23:08:38 +01005425
5426 mbedtls_ssl_config conf;
Gilles Peskine449bd832023-01-11 14:50:10 +01005427 mbedtls_ssl_config_init(&conf);
Jerry Yubaa49342022-02-15 10:26:40 +08005428#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005429 mbedtls_ssl_conf_max_tls_version(&conf, MBEDTLS_SSL_VERSION_TLS1_2);
5430 mbedtls_ssl_conf_min_tls_version(&conf, MBEDTLS_SSL_VERSION_TLS1_2);
Jerry Yubaa49342022-02-15 10:26:40 +08005431#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005432 mbedtls_ssl_conf_max_tls_version(&conf, MBEDTLS_SSL_VERSION_TLS1_3);
5433 mbedtls_ssl_conf_min_tls_version(&conf, MBEDTLS_SSL_VERSION_TLS1_3);
Jerry Yubaa49342022-02-15 10:26:40 +08005434#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005435 mbedtls_ssl_conf_curves(&conf, curve_list);
Brett Warren7f813d52021-10-20 23:08:38 +01005436
5437 mbedtls_ssl_context ssl;
Gilles Peskine449bd832023-01-11 14:50:10 +01005438 mbedtls_ssl_init(&ssl);
5439 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Brett Warren7f813d52021-10-20 23:08:38 +01005440
Gilles Peskine449bd832023-01-11 14:50:10 +01005441 TEST_ASSERT(ssl.handshake != NULL && ssl.handshake->group_list != NULL);
5442 TEST_ASSERT(ssl.conf != NULL && ssl.conf->group_list == NULL);
Brett Warren7f813d52021-10-20 23:08:38 +01005443
Gilles Peskine449bd832023-01-11 14:50:10 +01005444 TEST_EQUAL(ssl.handshake->group_list[ARRAY_LENGTH(iana_tls_group_list) - 1],
5445 MBEDTLS_SSL_IANA_TLS_GROUP_NONE);
Brett Warren7f813d52021-10-20 23:08:38 +01005446
Gilles Peskine449bd832023-01-11 14:50:10 +01005447 for (size_t i = 0; i < ARRAY_LENGTH(iana_tls_group_list); i++) {
5448 TEST_EQUAL(iana_tls_group_list[i], ssl.handshake->group_list[i]);
5449 }
Brett Warren7f813d52021-10-20 23:08:38 +01005450
Gilles Peskine449bd832023-01-11 14:50:10 +01005451 mbedtls_ssl_free(&ssl);
5452 mbedtls_ssl_config_free(&conf);
Brett Warren7f813d52021-10-20 23:08:38 +01005453}
5454/* END_CASE */
5455
5456/* BEGIN_CASE depends_on:MBEDTLS_DEPRECATED_REMOVED */
5457void conf_group()
5458{
5459 uint16_t iana_tls_group_list[] = { MBEDTLS_SSL_IANA_TLS_GROUP_SECP192R1,
Gilles Peskine449bd832023-01-11 14:50:10 +01005460 MBEDTLS_SSL_IANA_TLS_GROUP_SECP224R1,
5461 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
5462 MBEDTLS_SSL_IANA_TLS_GROUP_NONE };
Brett Warren7f813d52021-10-20 23:08:38 +01005463
5464 mbedtls_ssl_config conf;
Gilles Peskine449bd832023-01-11 14:50:10 +01005465 mbedtls_ssl_config_init(&conf);
Brett Warren7f813d52021-10-20 23:08:38 +01005466
Gilles Peskine449bd832023-01-11 14:50:10 +01005467 mbedtls_ssl_conf_max_tls_version(&conf, MBEDTLS_SSL_VERSION_TLS1_2);
5468 mbedtls_ssl_conf_min_tls_version(&conf, MBEDTLS_SSL_VERSION_TLS1_2);
Brett Warren7f813d52021-10-20 23:08:38 +01005469
Gilles Peskine449bd832023-01-11 14:50:10 +01005470 mbedtls_ssl_conf_groups(&conf, iana_tls_group_list);
Brett Warren7f813d52021-10-20 23:08:38 +01005471
5472 mbedtls_ssl_context ssl;
Gilles Peskine449bd832023-01-11 14:50:10 +01005473 mbedtls_ssl_init(&ssl);
5474 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Brett Warren7f813d52021-10-20 23:08:38 +01005475
Gilles Peskine449bd832023-01-11 14:50:10 +01005476 TEST_ASSERT(ssl.conf != NULL && ssl.conf->group_list != NULL);
Brett Warren7f813d52021-10-20 23:08:38 +01005477
Gilles Peskine449bd832023-01-11 14:50:10 +01005478 TEST_EQUAL(ssl.conf->group_list[ARRAY_LENGTH(iana_tls_group_list) - 1],
5479 MBEDTLS_SSL_IANA_TLS_GROUP_NONE);
Brett Warren7f813d52021-10-20 23:08:38 +01005480
Gilles Peskine449bd832023-01-11 14:50:10 +01005481 for (size_t i = 0; i < ARRAY_LENGTH(iana_tls_group_list); i++) {
5482 TEST_EQUAL(iana_tls_group_list[i], ssl.conf->group_list[i]);
5483 }
Brett Warren7f813d52021-10-20 23:08:38 +01005484
Gilles Peskine449bd832023-01-11 14:50:10 +01005485 mbedtls_ssl_free(&ssl);
5486 mbedtls_ssl_config_free(&conf);
Brett Warren7f813d52021-10-20 23:08:38 +01005487}
5488/* END_CASE */
Paul Elliottb9af2db2022-03-09 15:34:37 +00005489
Ronald Crone68ab4f2022-10-05 12:46:29 +02005490/* BEGIN_CASE depends_on:MBEDTLS_SSL_SRV_C:MBEDTLS_SSL_CACHE_C:!MBEDTLS_SSL_PROTO_TLS1_3:MBEDTLS_DEBUG_C:MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_PKCS1_V15:MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Gilles Peskine449bd832023-01-11 14:50:10 +01005491void force_bad_session_id_len()
Andrzej Kurek514683a2022-06-10 10:33:05 -04005492{
5493 enum { BUFFSIZE = 1024 };
5494 handshake_test_options options;
5495 mbedtls_endpoint client, server;
5496 log_pattern srv_pattern, cli_pattern;
5497 mbedtls_test_message_socket_context server_context, client_context;
5498
5499 srv_pattern.pattern = cli_pattern.pattern = "cache did not store session";
5500 srv_pattern.counter = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01005501 init_handshake_options(&options);
Andrzej Kurek514683a2022-06-10 10:33:05 -04005502
5503 options.srv_log_obj = &srv_pattern;
5504 options.srv_log_fun = log_analyzer;
5505
Gilles Peskine449bd832023-01-11 14:50:10 +01005506 USE_PSA_INIT();
5507 mbedtls_platform_zeroize(&client, sizeof(client));
5508 mbedtls_platform_zeroize(&server, sizeof(server));
Andrzej Kurek514683a2022-06-10 10:33:05 -04005509
Gilles Peskine449bd832023-01-11 14:50:10 +01005510 mbedtls_message_socket_init(&server_context);
5511 mbedtls_message_socket_init(&client_context);
Andrzej Kurek514683a2022-06-10 10:33:05 -04005512
Gilles Peskine449bd832023-01-11 14:50:10 +01005513 TEST_ASSERT(mbedtls_endpoint_init(&client, MBEDTLS_SSL_IS_CLIENT,
5514 &options, NULL, NULL,
5515 NULL, NULL) == 0);
Andrzej Kurek514683a2022-06-10 10:33:05 -04005516
Gilles Peskine449bd832023-01-11 14:50:10 +01005517 TEST_ASSERT(mbedtls_endpoint_init(&server, MBEDTLS_SSL_IS_SERVER,
5518 &options, NULL, NULL, NULL,
5519 NULL) == 0);
Andrzej Kurek514683a2022-06-10 10:33:05 -04005520
Gilles Peskine449bd832023-01-11 14:50:10 +01005521 mbedtls_debug_set_threshold(1);
5522 mbedtls_ssl_conf_dbg(&server.conf, options.srv_log_fun,
5523 options.srv_log_obj);
Andrzej Kurek514683a2022-06-10 10:33:05 -04005524
Gilles Peskine449bd832023-01-11 14:50:10 +01005525 TEST_ASSERT(mbedtls_mock_socket_connect(&(client.socket),
5526 &(server.socket),
5527 BUFFSIZE) == 0);
Andrzej Kurek514683a2022-06-10 10:33:05 -04005528
Gilles Peskine449bd832023-01-11 14:50:10 +01005529 TEST_ASSERT(mbedtls_move_handshake_to_state(&(client.ssl),
5530 &(server.ssl),
5531 MBEDTLS_SSL_HANDSHAKE_WRAPUP)
5532 == 0);
Andrzej Kurek514683a2022-06-10 10:33:05 -04005533 /* Force a bad session_id_len that will be read by the server in
5534 * mbedtls_ssl_cache_set. */
5535 server.ssl.session_negotiate->id_len = 33;
Gilles Peskine449bd832023-01-11 14:50:10 +01005536 if (options.cli_msg_len != 0 || options.srv_msg_len != 0) {
Andrzej Kurek514683a2022-06-10 10:33:05 -04005537 /* Start data exchanging test */
Gilles Peskine449bd832023-01-11 14:50:10 +01005538 TEST_ASSERT(mbedtls_exchange_data(&(client.ssl), options.cli_msg_len,
5539 options.expected_cli_fragments,
5540 &(server.ssl), options.srv_msg_len,
5541 options.expected_srv_fragments)
5542 == 0);
Andrzej Kurek514683a2022-06-10 10:33:05 -04005543 }
Andrzej Kurek514683a2022-06-10 10:33:05 -04005544
5545 /* Make sure that the cache did not store the session */
Gilles Peskine449bd832023-01-11 14:50:10 +01005546 TEST_EQUAL(srv_pattern.counter, 1);
Andrzej Kurek514683a2022-06-10 10:33:05 -04005547exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005548 mbedtls_endpoint_free(&client, NULL);
5549 mbedtls_endpoint_free(&server, NULL);
5550 free_handshake_options(&options);
5551 mbedtls_debug_set_threshold(0);
5552 USE_PSA_DONE();
Andrzej Kurek514683a2022-06-10 10:33:05 -04005553}
5554/* END_CASE */
5555
Andrzej Kureked4d2172022-06-08 11:57:57 -04005556/* BEGIN_CASE depends_on:MBEDTLS_SSL_SRV_C:MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE:MBEDTLS_TEST_HOOKS */
Gilles Peskine449bd832023-01-11 14:50:10 +01005557void cookie_parsing(data_t *cookie, int exp_ret)
Andrzej Kurekcfb01942022-06-06 13:08:23 -04005558{
5559 mbedtls_ssl_context ssl;
5560 mbedtls_ssl_config conf;
5561 size_t len;
5562
Gilles Peskine449bd832023-01-11 14:50:10 +01005563 mbedtls_ssl_init(&ssl);
5564 mbedtls_ssl_config_init(&conf);
5565 TEST_EQUAL(mbedtls_ssl_config_defaults(&conf, MBEDTLS_SSL_IS_SERVER,
5566 MBEDTLS_SSL_TRANSPORT_DATAGRAM,
5567 MBEDTLS_SSL_PRESET_DEFAULT),
5568 0);
Andrzej Kurekcfb01942022-06-06 13:08:23 -04005569
Gilles Peskine449bd832023-01-11 14:50:10 +01005570 TEST_EQUAL(mbedtls_ssl_setup(&ssl, &conf), 0);
5571 TEST_EQUAL(mbedtls_ssl_check_dtls_clihlo_cookie(&ssl, ssl.cli_id,
5572 ssl.cli_id_len,
5573 cookie->x, cookie->len,
5574 ssl.out_buf,
5575 MBEDTLS_SSL_OUT_CONTENT_LEN,
5576 &len),
5577 exp_ret);
Andrzej Kurekcfb01942022-06-06 13:08:23 -04005578
Gilles Peskine449bd832023-01-11 14:50:10 +01005579 mbedtls_ssl_free(&ssl);
5580 mbedtls_ssl_config_free(&conf);
Andrzej Kurekcfb01942022-06-06 13:08:23 -04005581}
5582/* END_CASE */
5583
Paul Elliottb9af2db2022-03-09 15:34:37 +00005584/* BEGIN_CASE depends_on:MBEDTLS_TIMING_C:MBEDTLS_HAVE_TIME */
Gilles Peskine449bd832023-01-11 14:50:10 +01005585void timing_final_delay_accessor()
Paul Elliottb9af2db2022-03-09 15:34:37 +00005586{
5587 mbedtls_timing_delay_context delay_context;
5588
Gilles Peskine449bd832023-01-11 14:50:10 +01005589 mbedtls_timing_set_delay(&delay_context, 50, 100);
Paul Elliottb9af2db2022-03-09 15:34:37 +00005590
Gilles Peskine449bd832023-01-11 14:50:10 +01005591 TEST_ASSERT(mbedtls_timing_get_final_delay(&delay_context) == 100);
Paul Elliottb9af2db2022-03-09 15:34:37 +00005592}
5593/* END_CASE */
Paul Elliott02758a52022-03-16 14:32:33 +00005594
5595/* BEGIN_CASE depends_on:MBEDTLS_SSL_DTLS_CONNECTION_ID */
Gilles Peskine449bd832023-01-11 14:50:10 +01005596void cid_sanity()
Paul Elliott02758a52022-03-16 14:32:33 +00005597{
5598 mbedtls_ssl_context ssl;
5599 mbedtls_ssl_config conf;
5600
5601 unsigned char own_cid[MBEDTLS_SSL_CID_IN_LEN_MAX];
5602 unsigned char test_cid[MBEDTLS_SSL_CID_IN_LEN_MAX];
5603 int cid_enabled;
5604 size_t own_cid_len;
5605
Gilles Peskine449bd832023-01-11 14:50:10 +01005606 mbedtls_test_rnd_std_rand(NULL, own_cid, sizeof(own_cid));
Paul Elliott02758a52022-03-16 14:32:33 +00005607
Gilles Peskine449bd832023-01-11 14:50:10 +01005608 mbedtls_ssl_init(&ssl);
5609 mbedtls_ssl_config_init(&conf);
Paul Elliott02758a52022-03-16 14:32:33 +00005610
Gilles Peskine449bd832023-01-11 14:50:10 +01005611 TEST_ASSERT(mbedtls_ssl_config_defaults(&conf,
5612 MBEDTLS_SSL_IS_CLIENT,
5613 MBEDTLS_SSL_TRANSPORT_STREAM,
5614 MBEDTLS_SSL_PRESET_DEFAULT)
5615 == 0);
Paul Elliott02758a52022-03-16 14:32:33 +00005616
Gilles Peskine449bd832023-01-11 14:50:10 +01005617 TEST_ASSERT(mbedtls_ssl_setup(&ssl, &conf) == 0);
Paul Elliott02758a52022-03-16 14:32:33 +00005618
5619 /* Can't use CID functions with stream transport. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005620 TEST_ASSERT(mbedtls_ssl_set_cid(&ssl, MBEDTLS_SSL_CID_ENABLED, own_cid,
5621 sizeof(own_cid))
5622 == MBEDTLS_ERR_SSL_BAD_INPUT_DATA);
Paul Elliott02758a52022-03-16 14:32:33 +00005623
Gilles Peskine449bd832023-01-11 14:50:10 +01005624 TEST_ASSERT(mbedtls_ssl_get_own_cid(&ssl, &cid_enabled, test_cid,
5625 &own_cid_len)
5626 == MBEDTLS_ERR_SSL_BAD_INPUT_DATA);
Paul Elliott02758a52022-03-16 14:32:33 +00005627
Gilles Peskine449bd832023-01-11 14:50:10 +01005628 TEST_ASSERT(mbedtls_ssl_config_defaults(&conf,
5629 MBEDTLS_SSL_IS_CLIENT,
5630 MBEDTLS_SSL_TRANSPORT_DATAGRAM,
5631 MBEDTLS_SSL_PRESET_DEFAULT)
5632 == 0);
Paul Elliott02758a52022-03-16 14:32:33 +00005633
5634 /* Attempt to set config cid size too big. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005635 TEST_ASSERT(mbedtls_ssl_conf_cid(&conf, MBEDTLS_SSL_CID_IN_LEN_MAX + 1,
5636 MBEDTLS_SSL_UNEXPECTED_CID_IGNORE)
5637 == MBEDTLS_ERR_SSL_BAD_INPUT_DATA);
Paul Elliott02758a52022-03-16 14:32:33 +00005638
Gilles Peskine449bd832023-01-11 14:50:10 +01005639 TEST_ASSERT(mbedtls_ssl_conf_cid(&conf, sizeof(own_cid),
5640 MBEDTLS_SSL_UNEXPECTED_CID_IGNORE)
5641 == 0);
Paul Elliott02758a52022-03-16 14:32:33 +00005642
5643 /* Attempt to set CID length not matching config. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005644 TEST_ASSERT(mbedtls_ssl_set_cid(&ssl, MBEDTLS_SSL_CID_ENABLED, own_cid,
5645 MBEDTLS_SSL_CID_IN_LEN_MAX - 1)
5646 == MBEDTLS_ERR_SSL_BAD_INPUT_DATA);
Paul Elliott02758a52022-03-16 14:32:33 +00005647
Gilles Peskine449bd832023-01-11 14:50:10 +01005648 TEST_ASSERT(mbedtls_ssl_set_cid(&ssl, MBEDTLS_SSL_CID_ENABLED, own_cid,
5649 sizeof(own_cid))
5650 == 0);
Paul Elliott02758a52022-03-16 14:32:33 +00005651
5652 /* Test we get back what we put in. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005653 TEST_ASSERT(mbedtls_ssl_get_own_cid(&ssl, &cid_enabled, test_cid,
5654 &own_cid_len)
5655 == 0);
Paul Elliott02758a52022-03-16 14:32:33 +00005656
Gilles Peskine449bd832023-01-11 14:50:10 +01005657 TEST_EQUAL(cid_enabled, MBEDTLS_SSL_CID_ENABLED);
5658 ASSERT_COMPARE(own_cid, own_cid_len, test_cid, own_cid_len);
Paul Elliott02758a52022-03-16 14:32:33 +00005659
5660 /* Test disabling works. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005661 TEST_ASSERT(mbedtls_ssl_set_cid(&ssl, MBEDTLS_SSL_CID_DISABLED, NULL,
5662 0)
5663 == 0);
Paul Elliott02758a52022-03-16 14:32:33 +00005664
Gilles Peskine449bd832023-01-11 14:50:10 +01005665 TEST_ASSERT(mbedtls_ssl_get_own_cid(&ssl, &cid_enabled, test_cid,
5666 &own_cid_len)
5667 == 0);
Paul Elliott02758a52022-03-16 14:32:33 +00005668
Gilles Peskine449bd832023-01-11 14:50:10 +01005669 TEST_EQUAL(cid_enabled, MBEDTLS_SSL_CID_DISABLED);
Paul Elliott02758a52022-03-16 14:32:33 +00005670
Gilles Peskine449bd832023-01-11 14:50:10 +01005671 mbedtls_ssl_free(&ssl);
5672 mbedtls_ssl_config_free(&conf);
Paul Elliott02758a52022-03-16 14:32:33 +00005673}
5674/* END_CASE */
5675
Ronald Crone68ab4f2022-10-05 12:46:29 +02005676/* BEGIN_CASE depends_on:MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED:MBEDTLS_USE_PSA_CRYPTO:MBEDTLS_PKCS1_V15:MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_ECP_DP_SECP256R1_ENABLED:MBEDTLS_RSA_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:MBEDTLS_ECDSA_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01005677void raw_key_agreement_fail(int bad_server_ecdhe_key)
Andrzej Kurekb3427822022-03-08 06:55:42 -05005678{
5679 enum { BUFFSIZE = 17000 };
5680 mbedtls_endpoint client, server;
5681 mbedtls_psa_stats_t stats;
Andrzej Kurek39d88d42022-03-31 06:30:54 -04005682 size_t free_slots_before = -1;
Andrzej Kurek626a9312022-06-10 11:07:39 -04005683 handshake_test_options options;
Andrzej Kurekb3427822022-03-08 06:55:42 -05005684
Andrzej Kurekcc28e9a2022-03-08 18:36:35 -05005685 uint16_t iana_tls_group_list[] = { MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
5686 MBEDTLS_SSL_IANA_TLS_GROUP_NONE };
Gilles Peskine449bd832023-01-11 14:50:10 +01005687 USE_PSA_INIT();
5688 mbedtls_platform_zeroize(&client, sizeof(client));
5689 mbedtls_platform_zeroize(&server, sizeof(server));
Andrzej Kurekb3427822022-03-08 06:55:42 -05005690
Gilles Peskine449bd832023-01-11 14:50:10 +01005691 init_handshake_options(&options);
Andrzej Kurek626a9312022-06-10 11:07:39 -04005692 options.pk_alg = MBEDTLS_PK_ECDSA;
5693
Andrzej Kurekb3427822022-03-08 06:55:42 -05005694 /* Client side, force SECP256R1 to make one key bitflip fail
Andrzej Kurek83e60ee2022-04-14 08:51:41 -04005695 * the raw key agreement. Flipping the first byte makes the
5696 * required 0x04 identifier invalid. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005697 TEST_EQUAL(mbedtls_endpoint_init(&client, MBEDTLS_SSL_IS_CLIENT,
5698 &options, NULL, NULL,
5699 NULL, iana_tls_group_list), 0);
Andrzej Kurekb3427822022-03-08 06:55:42 -05005700
5701 /* Server side */
Gilles Peskine449bd832023-01-11 14:50:10 +01005702 TEST_EQUAL(mbedtls_endpoint_init(&server, MBEDTLS_SSL_IS_SERVER,
5703 &options, NULL, NULL,
5704 NULL, NULL), 0);
Andrzej Kurekb3427822022-03-08 06:55:42 -05005705
Gilles Peskine449bd832023-01-11 14:50:10 +01005706 TEST_EQUAL(mbedtls_mock_socket_connect(&(client.socket),
5707 &(server.socket),
5708 BUFFSIZE), 0);
Andrzej Kurekb3427822022-03-08 06:55:42 -05005709
Gilles Peskine449bd832023-01-11 14:50:10 +01005710 TEST_EQUAL(mbedtls_move_handshake_to_state(&(client.ssl),
5711 &(server.ssl),
5712 MBEDTLS_SSL_CLIENT_KEY_EXCHANGE)
5713 , 0);
Andrzej Kurekb3427822022-03-08 06:55:42 -05005714
Gilles Peskine449bd832023-01-11 14:50:10 +01005715 mbedtls_psa_get_stats(&stats);
Andrzej Kurek39d88d42022-03-31 06:30:54 -04005716 /* Save the number of slots in use up to this point.
5717 * With PSA, one can be used for the ECDH private key. */
5718 free_slots_before = stats.empty_slots;
Andrzej Kurekcb33bc52022-03-31 07:17:18 -04005719
Gilles Peskine449bd832023-01-11 14:50:10 +01005720 if (bad_server_ecdhe_key) {
Gilles Peskineb4f874d2022-04-08 16:48:09 -04005721 /* Force a simulated bitflip in the server key. to make the
5722 * raw key agreement in ssl_write_client_key_exchange fail. */
5723 (client.ssl).handshake->ecdh_psa_peerkey[0] ^= 0x02;
5724 }
Andrzej Kurekb3427822022-03-08 06:55:42 -05005725
Gilles Peskine449bd832023-01-11 14:50:10 +01005726 TEST_EQUAL(mbedtls_move_handshake_to_state(&(client.ssl),
5727 &(server.ssl),
5728 MBEDTLS_SSL_HANDSHAKE_OVER),
5729 bad_server_ecdhe_key ? MBEDTLS_ERR_SSL_HW_ACCEL_FAILED : 0);
Andrzej Kurekb3427822022-03-08 06:55:42 -05005730
Gilles Peskine449bd832023-01-11 14:50:10 +01005731 mbedtls_psa_get_stats(&stats);
Andrzej Kurekb3427822022-03-08 06:55:42 -05005732
Gilles Peskineb4f874d2022-04-08 16:48:09 -04005733 /* Make sure that the key slot is already destroyed in case of failure,
5734 * without waiting to close the connection. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005735 if (bad_server_ecdhe_key) {
5736 TEST_EQUAL(free_slots_before, stats.empty_slots);
5737 }
Andrzej Kurekb3427822022-03-08 06:55:42 -05005738
5739exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005740 mbedtls_endpoint_free(&client, NULL);
5741 mbedtls_endpoint_free(&server, NULL);
5742 free_handshake_options(&options);
Andrzej Kurekcb33bc52022-03-31 07:17:18 -04005743
Gilles Peskine449bd832023-01-11 14:50:10 +01005744 USE_PSA_DONE();
Andrzej Kurekb3427822022-03-08 06:55:42 -05005745}
5746/* END_CASE */
Ronald Crone68ab4f2022-10-05 12:46:29 +02005747/* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS:MBEDTLS_SSL_PROTO_TLS1_3:!MBEDTLS_SSL_PROTO_TLS1_2:MBEDTLS_SSL_CLI_C:MBEDTLS_SSL_SRV_C:MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED:MBEDTLS_ECP_DP_SECP384R1_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +01005748void tls13_server_certificate_msg_invalid_vector_len()
Ronald Crone3dac4a2022-06-10 17:21:51 +02005749{
5750 int ret = -1;
5751 mbedtls_endpoint client_ep, server_ep;
5752 unsigned char *buf, *end;
5753 size_t buf_len;
5754 int step = 0;
5755 int expected_result;
Ronald Crone7b9b6b2022-06-10 17:24:31 +02005756 mbedtls_ssl_chk_buf_ptr_args expected_chk_buf_ptr_args;
Paul Elliott9a8d7842022-07-10 12:48:57 +01005757 handshake_test_options client_options;
5758 handshake_test_options server_options;
Ronald Crone3dac4a2022-06-10 17:21:51 +02005759
5760 /*
5761 * Test set-up
5762 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005763 USE_PSA_INIT();
5764 mbedtls_platform_zeroize(&client_ep, sizeof(client_ep));
5765 mbedtls_platform_zeroize(&server_ep, sizeof(server_ep));
Ronald Crone3dac4a2022-06-10 17:21:51 +02005766
Gilles Peskine449bd832023-01-11 14:50:10 +01005767 init_handshake_options(&client_options);
Paul Elliott9a8d7842022-07-10 12:48:57 +01005768 client_options.pk_alg = MBEDTLS_PK_ECDSA;
Gilles Peskine449bd832023-01-11 14:50:10 +01005769 ret = mbedtls_endpoint_init(&client_ep, MBEDTLS_SSL_IS_CLIENT,
5770 &client_options, NULL, NULL, NULL, NULL);
5771 TEST_EQUAL(ret, 0);
Ronald Crone3dac4a2022-06-10 17:21:51 +02005772
Gilles Peskine449bd832023-01-11 14:50:10 +01005773 init_handshake_options(&server_options);
Paul Elliott9a8d7842022-07-10 12:48:57 +01005774 server_options.pk_alg = MBEDTLS_PK_ECDSA;
Gilles Peskine449bd832023-01-11 14:50:10 +01005775 ret = mbedtls_endpoint_init(&server_ep, MBEDTLS_SSL_IS_SERVER,
5776 &server_options, NULL, NULL, NULL, NULL);
5777 TEST_EQUAL(ret, 0);
Ronald Crone3dac4a2022-06-10 17:21:51 +02005778
Gilles Peskine449bd832023-01-11 14:50:10 +01005779 ret = mbedtls_mock_socket_connect(&(client_ep.socket),
5780 &(server_ep.socket), 1024);
5781 TEST_EQUAL(ret, 0);
Ronald Crone3dac4a2022-06-10 17:21:51 +02005782
Gilles Peskine449bd832023-01-11 14:50:10 +01005783 while (1) {
5784 mbedtls_test_set_step(++step);
Ronald Crone3dac4a2022-06-10 17:21:51 +02005785
Gilles Peskine449bd832023-01-11 14:50:10 +01005786 ret = mbedtls_move_handshake_to_state(&(server_ep.ssl),
5787 &(client_ep.ssl),
5788 MBEDTLS_SSL_CERTIFICATE_VERIFY);
5789 TEST_EQUAL(ret, 0);
Ronald Crone3dac4a2022-06-10 17:21:51 +02005790
Gilles Peskine449bd832023-01-11 14:50:10 +01005791 ret = mbedtls_ssl_flush_output(&(server_ep.ssl));
5792 TEST_EQUAL(ret, 0);
Ronald Crone3dac4a2022-06-10 17:21:51 +02005793
Gilles Peskine449bd832023-01-11 14:50:10 +01005794 ret = mbedtls_move_handshake_to_state(&(client_ep.ssl),
5795 &(server_ep.ssl),
5796 MBEDTLS_SSL_SERVER_CERTIFICATE);
5797 TEST_EQUAL(ret, 0);
Ronald Crone3dac4a2022-06-10 17:21:51 +02005798
Gilles Peskine449bd832023-01-11 14:50:10 +01005799 ret = mbedtls_ssl_tls13_fetch_handshake_msg(&(client_ep.ssl),
5800 MBEDTLS_SSL_HS_CERTIFICATE,
5801 &buf, &buf_len);
5802 TEST_EQUAL(ret, 0);
Ronald Crone3dac4a2022-06-10 17:21:51 +02005803
5804 end = buf + buf_len;
5805
5806 /*
5807 * Tweak server Certificate message and parse it.
5808 */
5809
5810 ret = tweak_tls13_certificate_msg_vector_len(
Gilles Peskine449bd832023-01-11 14:50:10 +01005811 buf, &end, step, &expected_result, &expected_chk_buf_ptr_args);
Ronald Crone3dac4a2022-06-10 17:21:51 +02005812
Gilles Peskine449bd832023-01-11 14:50:10 +01005813 if (ret != 0) {
Ronald Crone3dac4a2022-06-10 17:21:51 +02005814 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01005815 }
Ronald Crone3dac4a2022-06-10 17:21:51 +02005816
Gilles Peskine449bd832023-01-11 14:50:10 +01005817 ret = mbedtls_ssl_tls13_parse_certificate(&(client_ep.ssl), buf, end);
5818 TEST_EQUAL(ret, expected_result);
Ronald Crone3dac4a2022-06-10 17:21:51 +02005819
Gilles Peskine449bd832023-01-11 14:50:10 +01005820 TEST_ASSERT(mbedtls_ssl_cmp_chk_buf_ptr_fail_args(
5821 &expected_chk_buf_ptr_args) == 0);
Ronald Crone7b9b6b2022-06-10 17:24:31 +02005822
Gilles Peskine449bd832023-01-11 14:50:10 +01005823 mbedtls_ssl_reset_chk_buf_ptr_fail_args();
Ronald Crone7b9b6b2022-06-10 17:24:31 +02005824
Gilles Peskine449bd832023-01-11 14:50:10 +01005825 ret = mbedtls_ssl_session_reset(&(client_ep.ssl));
5826 TEST_EQUAL(ret, 0);
Ronald Crone3dac4a2022-06-10 17:21:51 +02005827
Gilles Peskine449bd832023-01-11 14:50:10 +01005828 ret = mbedtls_ssl_session_reset(&(server_ep.ssl));
5829 TEST_EQUAL(ret, 0);
Ronald Crone3dac4a2022-06-10 17:21:51 +02005830 }
5831
5832exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01005833 mbedtls_ssl_reset_chk_buf_ptr_fail_args();
5834 mbedtls_endpoint_free(&client_ep, NULL);
5835 mbedtls_endpoint_free(&server_ep, NULL);
5836 free_handshake_options(&client_options);
5837 free_handshake_options(&server_options);
5838 USE_PSA_DONE();
Ronald Crone3dac4a2022-06-10 17:21:51 +02005839}
5840/* END_CASE */
Valerio Setti4452e982022-12-01 15:08:35 +01005841
5842/* BEGIN_CASE depends_on:MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +01005843void ssl_ecjpake_set_password(int use_opaque_arg)
Valerio Setti4452e982022-12-01 15:08:35 +01005844{
5845 mbedtls_ssl_context ssl;
5846 mbedtls_ssl_config conf;
Gilles Peskine449bd832023-01-11 14:50:10 +01005847#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti4452e982022-12-01 15:08:35 +01005848 mbedtls_svc_key_id_t pwd_slot = MBEDTLS_SVC_KEY_ID_INIT;
5849#else /* MBEDTLS_USE_PSA_CRYPTO */
5850 (void) use_opaque_arg;
5851#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01005852 unsigned char pwd_string[sizeof(ECJPAKE_TEST_PWD)] = "";
Valerio Setti4452e982022-12-01 15:08:35 +01005853 size_t pwd_len = 0;
Valerio Setti4452e982022-12-01 15:08:35 +01005854 int ret;
5855
Gilles Peskine449bd832023-01-11 14:50:10 +01005856 USE_PSA_INIT();
Valerio Setti4452e982022-12-01 15:08:35 +01005857
Gilles Peskine449bd832023-01-11 14:50:10 +01005858 mbedtls_ssl_init(&ssl);
Valerio Setti4452e982022-12-01 15:08:35 +01005859
Valerio Settie7518ba2022-12-02 12:09:43 +01005860 /* test with uninitalized SSL context */
Gilles Peskine449bd832023-01-11 14:50:10 +01005861 ECJPAKE_TEST_SET_PASSWORD(MBEDTLS_ERR_SSL_BAD_INPUT_DATA);
Valerio Setti4452e982022-12-01 15:08:35 +01005862
Gilles Peskine449bd832023-01-11 14:50:10 +01005863 mbedtls_ssl_config_init(&conf);
Valerio Setti4452e982022-12-01 15:08:35 +01005864
Gilles Peskine449bd832023-01-11 14:50:10 +01005865 TEST_EQUAL(mbedtls_ssl_config_defaults(&conf,
5866 MBEDTLS_SSL_IS_CLIENT,
5867 MBEDTLS_SSL_TRANSPORT_STREAM,
5868 MBEDTLS_SSL_PRESET_DEFAULT), 0);
Valerio Setti4452e982022-12-01 15:08:35 +01005869
Gilles Peskine449bd832023-01-11 14:50:10 +01005870 TEST_EQUAL(mbedtls_ssl_setup(&ssl, &conf), 0);
Valerio Setti4452e982022-12-01 15:08:35 +01005871
Valerio Settiba22c9c2022-12-06 11:42:33 +01005872 /* test with empty password or unitialized password key (depending on use_opaque_arg) */
Gilles Peskine449bd832023-01-11 14:50:10 +01005873 ECJPAKE_TEST_SET_PASSWORD(MBEDTLS_ERR_SSL_BAD_INPUT_DATA);
Valerio Setti4452e982022-12-01 15:08:35 +01005874
Gilles Peskine449bd832023-01-11 14:50:10 +01005875 pwd_len = strlen(ECJPAKE_TEST_PWD);
5876 memcpy(pwd_string, ECJPAKE_TEST_PWD, pwd_len);
Valerio Setti4452e982022-12-01 15:08:35 +01005877
Gilles Peskine449bd832023-01-11 14:50:10 +01005878#if defined(MBEDTLS_USE_PSA_CRYPTO)
5879 if (use_opaque_arg) {
Valerio Setti4452e982022-12-01 15:08:35 +01005880 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Valerio Setti785116a2022-12-12 11:59:25 +01005881 psa_key_attributes_t check_attributes = PSA_KEY_ATTRIBUTES_INIT;
Valerio Setti4452e982022-12-01 15:08:35 +01005882
Valerio Setti2a3ffb42022-12-08 16:27:46 +01005883 /* First try with an invalid usage */
Gilles Peskine449bd832023-01-11 14:50:10 +01005884 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
5885 psa_set_key_algorithm(&attributes, PSA_ALG_JPAKE);
5886 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
Valerio Setti4452e982022-12-01 15:08:35 +01005887
Gilles Peskine449bd832023-01-11 14:50:10 +01005888 PSA_ASSERT(psa_import_key(&attributes, pwd_string,
5889 pwd_len, &pwd_slot));
Valerio Setti2a3ffb42022-12-08 16:27:46 +01005890
Gilles Peskine449bd832023-01-11 14:50:10 +01005891 ECJPAKE_TEST_SET_PASSWORD(MBEDTLS_ERR_SSL_HW_ACCEL_FAILED);
Valerio Setti2a3ffb42022-12-08 16:27:46 +01005892
Valerio Setti9d313df2022-12-09 11:38:59 +01005893 /* check that the opaque key is still valid after failure */
Gilles Peskine449bd832023-01-11 14:50:10 +01005894 TEST_EQUAL(psa_get_key_attributes(pwd_slot, &check_attributes),
5895 PSA_SUCCESS);
Valerio Setti9d313df2022-12-09 11:38:59 +01005896
Gilles Peskine449bd832023-01-11 14:50:10 +01005897 psa_destroy_key(pwd_slot);
Valerio Setti2a3ffb42022-12-08 16:27:46 +01005898
5899 /* Then set the correct usage */
Gilles Peskine449bd832023-01-11 14:50:10 +01005900 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
Valerio Setti2a3ffb42022-12-08 16:27:46 +01005901
Gilles Peskine449bd832023-01-11 14:50:10 +01005902 PSA_ASSERT(psa_import_key(&attributes, pwd_string,
5903 pwd_len, &pwd_slot));
Valerio Setti4452e982022-12-01 15:08:35 +01005904 }
5905#endif /* MBEDTLS_USE_PSA_CRYPTO */
5906
Valerio Settie7518ba2022-12-02 12:09:43 +01005907 /* final check which should work without errors */
Gilles Peskine449bd832023-01-11 14:50:10 +01005908 ECJPAKE_TEST_SET_PASSWORD(0);
Valerio Setti4452e982022-12-01 15:08:35 +01005909
Gilles Peskine449bd832023-01-11 14:50:10 +01005910#if defined(MBEDTLS_USE_PSA_CRYPTO)
5911 if (use_opaque_arg) {
5912 psa_destroy_key(pwd_slot);
Valerio Setti31e99bb2022-12-09 14:35:10 +01005913 }
Valerio Settie7518ba2022-12-02 12:09:43 +01005914#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01005915 mbedtls_ssl_free(&ssl);
5916 mbedtls_ssl_config_free(&conf);
Valerio Setti4452e982022-12-01 15:08:35 +01005917
Gilles Peskine449bd832023-01-11 14:50:10 +01005918 USE_PSA_DONE();
Valerio Setti4452e982022-12-01 15:08:35 +01005919}
5920/* END_CASE */
Valerio Setti73260b62023-01-03 12:53:28 +01005921
Valerio Setti2c121852023-01-09 18:00:39 +01005922/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01005923void elliptic_curve_get_properties()
Valerio Setti73260b62023-01-03 12:53:28 +01005924{
5925 psa_ecc_family_t psa_family;
5926 size_t psa_bits;
5927
Gilles Peskine449bd832023-01-11 14:50:10 +01005928 USE_PSA_INIT();
Valerio Setti73260b62023-01-03 12:53:28 +01005929
Gilles Peskine449bd832023-01-11 14:50:10 +01005930#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_521)
5931 TEST_AVAILABLE_ECC(25, MBEDTLS_ECP_DP_SECP521R1, PSA_ECC_FAMILY_SECP_R1, 521);
Valerio Setti73260b62023-01-03 12:53:28 +01005932#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005933 TEST_UNAVAILABLE_ECC(25, MBEDTLS_ECP_DP_SECP521R1, PSA_ECC_FAMILY_SECP_R1, 521);
Valerio Setti73260b62023-01-03 12:53:28 +01005934#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005935#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
5936 TEST_AVAILABLE_ECC(28, MBEDTLS_ECP_DP_BP512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 512);
Valerio Setti73260b62023-01-03 12:53:28 +01005937#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005938 TEST_UNAVAILABLE_ECC(28, MBEDTLS_ECP_DP_BP512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 512);
Valerio Setti73260b62023-01-03 12:53:28 +01005939#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005940#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_384)
5941 TEST_AVAILABLE_ECC(24, MBEDTLS_ECP_DP_SECP384R1, PSA_ECC_FAMILY_SECP_R1, 384);
Valerio Setti73260b62023-01-03 12:53:28 +01005942#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005943 TEST_UNAVAILABLE_ECC(24, MBEDTLS_ECP_DP_SECP384R1, PSA_ECC_FAMILY_SECP_R1, 384);
Valerio Setti73260b62023-01-03 12:53:28 +01005944#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005945#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
5946 TEST_AVAILABLE_ECC(27, MBEDTLS_ECP_DP_BP384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 384);
Valerio Setti73260b62023-01-03 12:53:28 +01005947#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005948 TEST_UNAVAILABLE_ECC(27, MBEDTLS_ECP_DP_BP384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 384);
Valerio Setti73260b62023-01-03 12:53:28 +01005949#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005950#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_256)
5951 TEST_AVAILABLE_ECC(23, MBEDTLS_ECP_DP_SECP256R1, PSA_ECC_FAMILY_SECP_R1, 256);
Valerio Setti73260b62023-01-03 12:53:28 +01005952#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005953 TEST_UNAVAILABLE_ECC(23, MBEDTLS_ECP_DP_SECP256R1, PSA_ECC_FAMILY_SECP_R1, 256);
Valerio Setti73260b62023-01-03 12:53:28 +01005954#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005955#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_256)
5956 TEST_AVAILABLE_ECC(22, MBEDTLS_ECP_DP_SECP256K1, PSA_ECC_FAMILY_SECP_K1, 256);
Valerio Setti73260b62023-01-03 12:53:28 +01005957#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005958 TEST_UNAVAILABLE_ECC(22, MBEDTLS_ECP_DP_SECP256K1, PSA_ECC_FAMILY_SECP_K1, 256);
Valerio Setti73260b62023-01-03 12:53:28 +01005959#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005960#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
5961 TEST_AVAILABLE_ECC(26, MBEDTLS_ECP_DP_BP256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 256);
Valerio Setti73260b62023-01-03 12:53:28 +01005962#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005963 TEST_UNAVAILABLE_ECC(26, MBEDTLS_ECP_DP_BP256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 256);
Valerio Setti73260b62023-01-03 12:53:28 +01005964#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005965#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_224)
5966 TEST_AVAILABLE_ECC(21, MBEDTLS_ECP_DP_SECP224R1, PSA_ECC_FAMILY_SECP_R1, 224);
Valerio Setti73260b62023-01-03 12:53:28 +01005967#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005968 TEST_UNAVAILABLE_ECC(21, MBEDTLS_ECP_DP_SECP224R1, PSA_ECC_FAMILY_SECP_R1, 224);
Valerio Setti73260b62023-01-03 12:53:28 +01005969#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005970#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_224)
5971 TEST_AVAILABLE_ECC(20, MBEDTLS_ECP_DP_SECP224K1, PSA_ECC_FAMILY_SECP_K1, 224);
Valerio Setti73260b62023-01-03 12:53:28 +01005972#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005973 TEST_UNAVAILABLE_ECC(20, MBEDTLS_ECP_DP_SECP224K1, PSA_ECC_FAMILY_SECP_K1, 224);
Valerio Setti73260b62023-01-03 12:53:28 +01005974#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005975#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_192)
5976 TEST_AVAILABLE_ECC(19, MBEDTLS_ECP_DP_SECP192R1, PSA_ECC_FAMILY_SECP_R1, 192);
Valerio Setti73260b62023-01-03 12:53:28 +01005977#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005978 TEST_UNAVAILABLE_ECC(19, MBEDTLS_ECP_DP_SECP192R1, PSA_ECC_FAMILY_SECP_R1, 192);
Valerio Setti73260b62023-01-03 12:53:28 +01005979#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005980#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_192)
5981 TEST_AVAILABLE_ECC(18, MBEDTLS_ECP_DP_SECP192K1, PSA_ECC_FAMILY_SECP_K1, 192);
Valerio Setti73260b62023-01-03 12:53:28 +01005982#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005983 TEST_UNAVAILABLE_ECC(18, MBEDTLS_ECP_DP_SECP192K1, PSA_ECC_FAMILY_SECP_K1, 192);
Valerio Setti73260b62023-01-03 12:53:28 +01005984#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005985#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) || defined(PSA_WANT_ECC_MONTGOMERY_255)
5986 TEST_AVAILABLE_ECC(29, MBEDTLS_ECP_DP_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY, 255);
Valerio Setti73260b62023-01-03 12:53:28 +01005987#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005988 TEST_UNAVAILABLE_ECC(29, MBEDTLS_ECP_DP_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY, 255);
Valerio Setti73260b62023-01-03 12:53:28 +01005989#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005990#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED) || defined(PSA_WANT_ECC_MONTGOMERY_448)
5991 TEST_AVAILABLE_ECC(30, MBEDTLS_ECP_DP_CURVE448, PSA_ECC_FAMILY_MONTGOMERY, 448);
Valerio Setti73260b62023-01-03 12:53:28 +01005992#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005993 TEST_UNAVAILABLE_ECC(30, MBEDTLS_ECP_DP_CURVE448, PSA_ECC_FAMILY_MONTGOMERY, 448);
Valerio Setti73260b62023-01-03 12:53:28 +01005994#endif
5995
Gilles Peskine449bd832023-01-11 14:50:10 +01005996 USE_PSA_DONE();
Valerio Setti73260b62023-01-03 12:53:28 +01005997}
5998/* END_CASE */