1 /** \file ssl_helpers.h
2  *
3  * \brief This file contains helper functions to set up a TLS connection.
4  */
5 
6 /*
7  *  Copyright The Mbed TLS Contributors
8  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
9  */
10 
11 #ifndef SSL_HELPERS_H
12 #define SSL_HELPERS_H
13 
14 #include "mbedtls/build_info.h"
15 
16 #include <string.h>
17 
18 #include <test/helpers.h>
19 #include <test/macros.h>
20 #include <test/random.h>
21 #include <test/psa_crypto_helpers.h>
22 
23 #if defined(MBEDTLS_SSL_TLS_C)
24 #include <ssl_misc.h>
25 #include <mbedtls/timing.h>
26 #include <mbedtls/debug.h>
27 
28 #include "test/certs.h"
29 
30 #if defined(MBEDTLS_SSL_CACHE_C)
31 #include "mbedtls/ssl_cache.h"
32 #endif
33 
34 #if defined(MBEDTLS_USE_PSA_CRYPTO)
35 #define PSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \
36                                                            psa_to_ssl_errors, \
37                                                            psa_generic_status_to_mbedtls)
38 #endif
39 
40 #if defined(MBEDTLS_SSL_PROTO_TLS1_3)
41 #if defined(MBEDTLS_SSL_HAVE_AES)
42 #if defined(MBEDTLS_SSL_HAVE_GCM)
43 #if defined(MBEDTLS_MD_CAN_SHA384)
44 #define MBEDTLS_TEST_HAS_TLS1_3_AES_256_GCM_SHA384
45 #endif
46 #if defined(MBEDTLS_MD_CAN_SHA256)
47 #define MBEDTLS_TEST_HAS_TLS1_3_AES_128_GCM_SHA256
48 #endif
49 #endif /* MBEDTLS_SSL_HAVE_GCM */
50 #if defined(MBEDTLS_SSL_HAVE_CCM) && defined(MBEDTLS_MD_CAN_SHA256)
51 #define MBEDTLS_TEST_HAS_TLS1_3_AES_128_CCM_SHA256
52 #define MBEDTLS_TEST_HAS_TLS1_3_AES_128_CCM_8_SHA256
53 #endif
54 #endif /* MBEDTLS_SSL_HAVE_AES */
55 #if defined(MBEDTLS_SSL_HAVE_CHACHAPOLY) && defined(MBEDTLS_MD_CAN_SHA256)
56 #define MBEDTLS_TEST_HAS_TLS1_3_CHACHA20_POLY1305_SHA256
57 #endif
58 
59 #if defined(MBEDTLS_TEST_HAS_TLS1_3_AES_256_GCM_SHA384) || \
60     defined(MBEDTLS_TEST_HAS_TLS1_3_AES_128_GCM_SHA256) || \
61     defined(MBEDTLS_TEST_HAS_TLS1_3_AES_128_CCM_SHA256) || \
62     defined(MBEDTLS_TEST_HAS_TLS1_3_AES_128_CCM_8_SHA256) || \
63     defined(MBEDTLS_TEST_HAS_TLS1_3_CHACHA20_POLY1305_SHA256)
64 #define MBEDTLS_TEST_AT_LEAST_ONE_TLS1_3_CIPHERSUITE
65 #endif
66 
67 #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
68 
69 #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) ||    \
70     defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) ||  \
71     defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
72 #define MBEDTLS_CAN_HANDLE_RSA_TEST_KEY
73 #endif
74 enum {
75 #define MBEDTLS_SSL_TLS1_3_LABEL(name, string)          \
76     tls13_label_ ## name,
77     MBEDTLS_SSL_TLS1_3_LABEL_LIST
78 #undef MBEDTLS_SSL_TLS1_3_LABEL
79 };
80 
81 #if defined(MBEDTLS_SSL_ALPN)
82 #define MBEDTLS_TEST_MAX_ALPN_LIST_SIZE 10
83 #endif
84 
85 typedef struct mbedtls_test_ssl_log_pattern {
86     const char *pattern;
87     size_t counter;
88 } mbedtls_test_ssl_log_pattern;
89 
90 typedef struct mbedtls_test_handshake_test_options {
91     const char *cipher;
92     uint16_t *group_list;
93     mbedtls_ssl_protocol_version client_min_version;
94     mbedtls_ssl_protocol_version client_max_version;
95     mbedtls_ssl_protocol_version server_min_version;
96     mbedtls_ssl_protocol_version server_max_version;
97     mbedtls_ssl_protocol_version expected_negotiated_version;
98     int expected_handshake_result;
99     int expected_ciphersuite;
100     int pk_alg;
101     int opaque_alg;
102     int opaque_alg2;
103     int opaque_usage;
104     data_t *psk_str;
105     int dtls;
106     int srv_auth_mode;
107     int serialize;
108     int mfl;
109     int cli_msg_len;
110     int srv_msg_len;
111     int expected_cli_fragments;
112     int expected_srv_fragments;
113     int renegotiate;
114     int legacy_renegotiation;
115     void *srv_log_obj;
116     void *cli_log_obj;
117     void (*srv_log_fun)(void *, int, const char *, int, const char *);
118     void (*cli_log_fun)(void *, int, const char *, int, const char *);
119     int resize_buffers;
120     int early_data;
121     int max_early_data_size;
122 #if defined(MBEDTLS_SSL_CACHE_C)
123     mbedtls_ssl_cache_context *cache;
124 #endif
125 #if defined(MBEDTLS_SSL_ALPN)
126     const char *alpn_list[MBEDTLS_TEST_MAX_ALPN_LIST_SIZE];
127 #endif
128 } mbedtls_test_handshake_test_options;
129 
130 /*
131  * Buffer structure for custom I/O callbacks.
132  */
133 typedef struct mbedtls_test_ssl_buffer {
134     size_t start;
135     size_t content_length;
136     size_t capacity;
137     unsigned char *buffer;
138 } mbedtls_test_ssl_buffer;
139 
140 /*
141  * Context for a message metadata queue (fifo) that is on top of the ring buffer.
142  */
143 typedef struct mbedtls_test_ssl_message_queue {
144     size_t *messages;
145     int pos;
146     int num;
147     int capacity;
148 } mbedtls_test_ssl_message_queue;
149 
150 /*
151  * Context for the I/O callbacks simulating network connection.
152  */
153 
154 #define MBEDTLS_MOCK_SOCKET_CONNECTED 1
155 
156 typedef struct mbedtls_test_mock_socket {
157     int status;
158     mbedtls_test_ssl_buffer *input;
159     mbedtls_test_ssl_buffer *output;
160     struct mbedtls_test_mock_socket *peer;
161 } mbedtls_test_mock_socket;
162 
163 /* Errors used in the message socket mocks */
164 
165 #define MBEDTLS_TEST_ERROR_CONTEXT_ERROR -55
166 #define MBEDTLS_TEST_ERROR_SEND_FAILED -66
167 #define MBEDTLS_TEST_ERROR_RECV_FAILED -77
168 
169 /*
170  * Structure used as an addon, or a wrapper, around the mocked sockets.
171  * Contains an input queue, to which the other socket pushes metadata,
172  * and an output queue, to which this one pushes metadata. This context is
173  * considered as an owner of the input queue only, which is initialized and
174  * freed in the respective setup and free calls.
175  */
176 typedef struct mbedtls_test_message_socket_context {
177     mbedtls_test_ssl_message_queue *queue_input;
178     mbedtls_test_ssl_message_queue *queue_output;
179     mbedtls_test_mock_socket *socket;
180 } mbedtls_test_message_socket_context;
181 
182 #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
183 
184 /*
185  * Structure with endpoint's certificates for SSL communication tests.
186  */
187 typedef struct mbedtls_test_ssl_endpoint_certificate {
188     mbedtls_x509_crt *ca_cert;
189     mbedtls_x509_crt *cert;
190     mbedtls_pk_context *pkey;
191 } mbedtls_test_ssl_endpoint_certificate;
192 
193 /*
194  * Endpoint structure for SSL communication tests.
195  */
196 typedef struct mbedtls_test_ssl_endpoint {
197     const char *name;
198     mbedtls_ssl_context ssl;
199     mbedtls_ssl_config conf;
200     mbedtls_test_mock_socket socket;
201     mbedtls_test_ssl_endpoint_certificate cert;
202 } mbedtls_test_ssl_endpoint;
203 
204 #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
205 
206 /*
207  * Random number generator aimed for TLS unitary tests. Its main purpose is to
208  * simplify the set-up of a random number generator for TLS
209  * unitary tests: no need to set up a good entropy source for example.
210  */
211 int mbedtls_test_random(void *p_rng, unsigned char *output, size_t output_len);
212 
213 /*
214  * This function can be passed to mbedtls to receive output logs from it. In
215  * this case, it will count the instances of a mbedtls_test_ssl_log_pattern
216  * in the received logged messages.
217  */
218 void mbedtls_test_ssl_log_analyzer(void *ctx, int level,
219                                    const char *file, int line,
220                                    const char *str);
221 
222 void mbedtls_test_init_handshake_options(
223     mbedtls_test_handshake_test_options *opts);
224 
225 void mbedtls_test_free_handshake_options(
226     mbedtls_test_handshake_test_options *opts);
227 
228 /*
229  * Initialises \p buf. After calling this function it is safe to call
230  * `mbedtls_test_ssl_buffer_free()` on \p buf.
231  */
232 void mbedtls_test_ssl_buffer_init(mbedtls_test_ssl_buffer *buf);
233 
234 /*
235  * Sets up \p buf. After calling this function it is safe to call
236  * `mbedtls_test_ssl_buffer_put()` and `mbedtls_test_ssl_buffer_get()`
237  * on \p buf.
238  */
239 int mbedtls_test_ssl_buffer_setup(mbedtls_test_ssl_buffer *buf,
240                                   size_t capacity);
241 
242 void mbedtls_test_ssl_buffer_free(mbedtls_test_ssl_buffer *buf);
243 
244 /*
245  * Puts \p input_len bytes from the \p input buffer into the ring buffer \p buf.
246  *
247  * \p buf must have been initialized and set up by calling
248  * `mbedtls_test_ssl_buffer_init()` and `mbedtls_test_ssl_buffer_setup()`.
249  *
250  * \retval  \p input_len, if the data fits.
251  * \retval  0 <= value < \p input_len, if the data does not fit.
252  * \retval  -1, if \p buf is NULL, it hasn't been set up or \p input_len is not
253  *          zero and \p input is NULL.
254  */
255 int mbedtls_test_ssl_buffer_put(mbedtls_test_ssl_buffer *buf,
256                                 const unsigned char *input, size_t input_len);
257 
258 /*
259  * Gets \p output_len bytes from the ring buffer \p buf into the
260  * \p output buffer. The output buffer can be NULL, in this case a part of the
261  * ring buffer will be dropped, if the requested length is available.
262  *
263  * \p buf must have been initialized and set up by calling
264  * `mbedtls_test_ssl_buffer_init()` and `mbedtls_test_ssl_buffer_setup()`.
265  *
266  * \retval  \p output_len, if the data is available.
267  * \retval  0 <= value < \p output_len, if the data is not available.
268  * \retval  -1, if \buf is NULL or it hasn't been set up.
269  */
270 int mbedtls_test_ssl_buffer_get(mbedtls_test_ssl_buffer *buf,
271                                 unsigned char *output, size_t output_len);
272 
273 /*
274  * Errors used in the message transport mock tests
275  */
276  #define MBEDTLS_TEST_ERROR_ARG_NULL -11
277  #define MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED -44
278 
279 /*
280  * Setup and free functions for the message metadata queue.
281  *
282  * \p capacity describes the number of message metadata chunks that can be held
283  *    within the queue.
284  *
285  * \retval  0, if a metadata queue of a given length can be allocated.
286  * \retval  MBEDTLS_ERR_SSL_ALLOC_FAILED, if allocation failed.
287  */
288 int mbedtls_test_ssl_message_queue_setup(
289     mbedtls_test_ssl_message_queue *queue, size_t capacity);
290 
291 void mbedtls_test_ssl_message_queue_free(
292     mbedtls_test_ssl_message_queue *queue);
293 
294 /*
295  * Push message length information onto the message metadata queue.
296  * This will become the last element to leave it (fifo).
297  *
298  * \retval  MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null.
299  * \retval  MBEDTLS_ERR_SSL_WANT_WRITE, if the queue is full.
300  * \retval  \p len, if the push was successful.
301  */
302 int mbedtls_test_ssl_message_queue_push_info(
303     mbedtls_test_ssl_message_queue *queue, size_t len);
304 
305 /*
306  * Pop information about the next message length from the queue. This will be
307  * the oldest inserted message length(fifo). \p msg_len can be null, in which
308  * case the data will be popped from the queue but not copied anywhere.
309  *
310  * \retval  MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null.
311  * \retval  MBEDTLS_ERR_SSL_WANT_READ, if the queue is empty.
312  * \retval  message length, if the pop was successful, up to the given
313             \p buf_len.
314  */
315 int mbedtls_test_ssl_message_queue_pop_info(
316     mbedtls_test_ssl_message_queue *queue, size_t buf_len);
317 
318 /*
319  * Setup and teardown functions for mock sockets.
320  */
321 void mbedtls_test_mock_socket_init(mbedtls_test_mock_socket *socket);
322 
323 /*
324  * Closes the socket \p socket.
325  *
326  * \p socket must have been previously initialized by calling
327  * mbedtls_test_mock_socket_init().
328  *
329  * This function frees all allocated resources and both sockets are aware of the
330  * new connection state.
331  *
332  * That is, this function does not simulate half-open TCP connections and the
333  * phenomenon that when closing a UDP connection the peer is not aware of the
334  * connection having been closed.
335  */
336 void mbedtls_test_mock_socket_close(mbedtls_test_mock_socket *socket);
337 
338 /*
339  * Establishes a connection between \p peer1 and \p peer2.
340  *
341  * \p peer1 and \p peer2 must have been previously initialized by calling
342  * mbedtls_test_mock_socket_init().
343  *
344  * The capacities of the internal buffers are set to \p bufsize. Setting this to
345  * the correct value allows for simulation of MTU, sanity testing the mock
346  * implementation and mocking TCP connections with lower memory cost.
347  */
348 int mbedtls_test_mock_socket_connect(mbedtls_test_mock_socket *peer1,
349                                      mbedtls_test_mock_socket *peer2,
350                                      size_t bufsize);
351 
352 
353 /*
354  * Callbacks for simulating blocking I/O over connection-oriented transport.
355  */
356 int mbedtls_test_mock_tcp_send_b(void *ctx,
357                                  const unsigned char *buf, size_t len);
358 
359 int mbedtls_test_mock_tcp_recv_b(void *ctx, unsigned char *buf, size_t len);
360 
361 /*
362  * Callbacks for simulating non-blocking I/O over connection-oriented transport.
363  */
364 int mbedtls_test_mock_tcp_send_nb(void *ctx,
365                                   const unsigned char *buf, size_t len);
366 
367 int mbedtls_test_mock_tcp_recv_nb(void *ctx, unsigned char *buf, size_t len);
368 
369 void mbedtls_test_message_socket_init(
370     mbedtls_test_message_socket_context *ctx);
371 
372 /*
373  * Setup a given message socket context including initialization of
374  * input/output queues to a chosen capacity of messages. Also set the
375  * corresponding mock socket.
376  *
377  * \retval  0, if everything succeeds.
378  * \retval  MBEDTLS_ERR_SSL_ALLOC_FAILED, if allocation of a message
379  *          queue failed.
380  */
381 int mbedtls_test_message_socket_setup(
382     mbedtls_test_ssl_message_queue *queue_input,
383     mbedtls_test_ssl_message_queue *queue_output,
384     size_t queue_capacity,
385     mbedtls_test_mock_socket *socket,
386     mbedtls_test_message_socket_context *ctx);
387 
388 /*
389  * Close a given message socket context, along with the socket itself. Free the
390  * memory allocated by the input queue.
391  */
392 void mbedtls_test_message_socket_close(
393     mbedtls_test_message_socket_context *ctx);
394 
395 /*
396  * Send one message through a given message socket context.
397  *
398  * \retval  \p len, if everything succeeds.
399  * \retval  MBEDTLS_TEST_ERROR_CONTEXT_ERROR, if any of the needed context
400  *          elements or the context itself is null.
401  * \retval  MBEDTLS_TEST_ERROR_SEND_FAILED if
402  *          mbedtls_test_mock_tcp_send_b failed.
403  * \retval  MBEDTLS_ERR_SSL_WANT_WRITE, if the output queue is full.
404  *
405  * This function will also return any error from
406  * mbedtls_test_ssl_message_queue_push_info.
407  */
408 int mbedtls_test_mock_tcp_send_msg(void *ctx,
409                                    const unsigned char *buf, size_t len);
410 
411 /*
412  * Receive one message from a given message socket context and return message
413  * length or an error.
414  *
415  * \retval  message length, if everything succeeds.
416  * \retval  MBEDTLS_TEST_ERROR_CONTEXT_ERROR, if any of the needed context
417  *          elements or the context itself is null.
418  * \retval  MBEDTLS_TEST_ERROR_RECV_FAILED if
419  *          mbedtls_test_mock_tcp_recv_b failed.
420  *
421  * This function will also return any error other than
422  * MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED from test_ssl_message_queue_peek_info.
423  */
424 int mbedtls_test_mock_tcp_recv_msg(void *ctx,
425                                    unsigned char *buf, size_t buf_len);
426 
427 #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
428 
429 /*
430  * Initializes \p ep_cert structure and assigns it to endpoint
431  * represented by \p ep.
432  *
433  * \retval  0 on success, otherwise error code.
434  */
435 int mbedtls_test_ssl_endpoint_certificate_init(mbedtls_test_ssl_endpoint *ep,
436                                                int pk_alg,
437                                                int opaque_alg, int opaque_alg2,
438                                                int opaque_usage);
439 
440 /*
441  * Initializes \p ep structure. It is important to call
442  * `mbedtls_test_ssl_endpoint_free()` after calling this function
443  * even if it fails.
444  *
445  * \p endpoint_type must be set as MBEDTLS_SSL_IS_SERVER or
446  * MBEDTLS_SSL_IS_CLIENT.
447  * \p pk_alg the algorithm to use, currently only MBEDTLS_PK_RSA and
448  * MBEDTLS_PK_ECDSA are supported.
449  * \p dtls_context - in case of DTLS - this is the context handling metadata.
450  * \p input_queue - used only in case of DTLS.
451  * \p output_queue - used only in case of DTLS.
452  *
453  * \retval  0 on success, otherwise error code.
454  */
455 int mbedtls_test_ssl_endpoint_init(
456     mbedtls_test_ssl_endpoint *ep, int endpoint_type,
457     mbedtls_test_handshake_test_options *options,
458     mbedtls_test_message_socket_context *dtls_context,
459     mbedtls_test_ssl_message_queue *input_queue,
460     mbedtls_test_ssl_message_queue *output_queue);
461 
462 /*
463  * Deinitializes endpoint represented by \p ep.
464  */
465 void mbedtls_test_ssl_endpoint_free(
466     mbedtls_test_ssl_endpoint *ep,
467     mbedtls_test_message_socket_context *context);
468 
469 /*
470  * This function moves ssl handshake from \p ssl to prescribed \p state.
471  * /p second_ssl is used as second endpoint and their sockets have to be
472  * connected before calling this function.
473  *
474  * \retval  0 on success, otherwise error code.
475  */
476 int mbedtls_test_move_handshake_to_state(mbedtls_ssl_context *ssl,
477                                          mbedtls_ssl_context *second_ssl,
478                                          int state);
479 
480 #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
481 
482 /*
483  * Helper function setting up inverse record transformations
484  * using given cipher, hash, EtM mode, authentication tag length,
485  * and version.
486  */
487 #define CHK(x)                                  \
488     do                                          \
489     {                                           \
490         if (!(x))                               \
491         {                                       \
492             ret = -1;                           \
493             goto cleanup;                       \
494         }                                       \
495     } while (0)
496 
497 #if MBEDTLS_SSL_CID_OUT_LEN_MAX > MBEDTLS_SSL_CID_IN_LEN_MAX
498 #define SSL_CID_LEN_MIN MBEDTLS_SSL_CID_IN_LEN_MAX
499 #else
500 #define SSL_CID_LEN_MIN MBEDTLS_SSL_CID_OUT_LEN_MAX
501 #endif
502 
503 #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
504     defined(MBEDTLS_SSL_HAVE_CBC) && defined(MBEDTLS_SSL_HAVE_AES)
505 int mbedtls_test_psa_cipher_encrypt_helper(mbedtls_ssl_transform *transform,
506                                            const unsigned char *iv,
507                                            size_t iv_len,
508                                            const unsigned char *input,
509                                            size_t ilen,
510                                            unsigned char *output,
511                                            size_t *olen);
512 #endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_HAVE_CBC &&
513           MBEDTLS_SSL_HAVE_AES */
514 
515 int mbedtls_test_ssl_build_transforms(mbedtls_ssl_transform *t_in,
516                                       mbedtls_ssl_transform *t_out,
517                                       int cipher_type, int hash_id,
518                                       int etm, int tag_mode,
519                                       mbedtls_ssl_protocol_version tls_version,
520                                       size_t cid0_len,
521                                       size_t cid1_len);
522 
523 #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
524 /**
525  * \param[in,out] record        The record to prepare.
526  *                              It must contain the data to MAC at offset
527  *                              `record->data_offset`, of length
528  *                              `record->data_length`.
529  *                              On success, write the MAC immediately
530  *                              after the data and increment
531  *                              `record->data_length` accordingly.
532  * \param[in,out] transform_out The out transform, typically prepared by
533  *                              mbedtls_test_ssl_build_transforms().
534  *                              Its HMAC context may be used. Other than that
535  *                              it is treated as an input parameter.
536  *
537  * \return                      0 on success, an `MBEDTLS_ERR_xxx` error code
538  *                              or -1 on error.
539  */
540 int mbedtls_test_ssl_prepare_record_mac(mbedtls_record *record,
541                                         mbedtls_ssl_transform *transform_out);
542 #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
543 
544 /*
545  * Populate a session structure for serialization tests.
546  * Choose dummy values, mostly non-0 to distinguish from the init default.
547  */
548 int mbedtls_test_ssl_tls12_populate_session(mbedtls_ssl_session *session,
549                                             int ticket_len,
550                                             int endpoint_type,
551                                             const char *crt_file);
552 
553 #if defined(MBEDTLS_SSL_PROTO_TLS1_3)
554 int mbedtls_test_ssl_tls13_populate_session(mbedtls_ssl_session *session,
555                                             int ticket_len,
556                                             int endpoint_type);
557 #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
558 
559 /*
560  * Perform data exchanging between \p ssl_1 and \p ssl_2 and check if the
561  * message was sent in the correct number of fragments.
562  *
563  * /p ssl_1 and /p ssl_2    Endpoints represented by mbedtls_ssl_context. Both
564  *                          of them must be initialized and connected
565  *                          beforehand.
566  * /p msg_len_1 and /p msg_len_2 specify the size of the message to send.
567  * /p expected_fragments_1 and /p expected_fragments_2 determine in how many
568  *                          fragments the message should be sent.
569  *      expected_fragments is 0: can be used for DTLS testing while the message
570  *                          size is larger than MFL. In that case the message
571  *                          cannot be fragmented and sent to the second
572  *                          endpoint.
573  *                          This value can be used for negative tests.
574  *      expected_fragments is 1: can be used for TLS/DTLS testing while the
575  *                          message size is below MFL
576  *      expected_fragments > 1: can be used for TLS testing while the message
577  *                          size is larger than MFL
578  *
579  * \retval  0 on success, otherwise error code.
580  */
581 int mbedtls_test_ssl_exchange_data(
582     mbedtls_ssl_context *ssl_1,
583     int msg_len_1, const int expected_fragments_1,
584     mbedtls_ssl_context *ssl_2,
585     int msg_len_2, const int expected_fragments_2);
586 
587 #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
588 void mbedtls_test_ssl_perform_handshake(
589     mbedtls_test_handshake_test_options *options);
590 #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
591 
592 #if defined(MBEDTLS_TEST_HOOKS)
593 /*
594  * Tweak vector lengths in a TLS 1.3 Certificate message
595  *
596  * \param[in]       buf    Buffer containing the Certificate message to tweak
597  * \param[in]]out]  end    End of the buffer to parse
598  * \param           tweak  Tweak identifier (from 1 to the number of tweaks).
599  * \param[out]  expected_result  Error code expected from the parsing function
600  * \param[out]  args  Arguments of the MBEDTLS_SSL_CHK_BUF_READ_PTR call that
601  *                    is expected to fail. All zeroes if no
602  *                    MBEDTLS_SSL_CHK_BUF_READ_PTR failure is expected.
603  */
604 int mbedtls_test_tweak_tls13_certificate_msg_vector_len(
605     unsigned char *buf, unsigned char **end, int tweak,
606     int *expected_result, mbedtls_ssl_chk_buf_ptr_args *args);
607 #endif /* MBEDTLS_TEST_HOOKS */
608 
609 #if defined(MBEDTLS_SSL_SESSION_TICKETS)
610 int mbedtls_test_ticket_write(
611     void *p_ticket, const mbedtls_ssl_session *session,
612     unsigned char *start, const unsigned char *end,
613     size_t *tlen, uint32_t *ticket_lifetime);
614 
615 int mbedtls_test_ticket_parse(void *p_ticket, mbedtls_ssl_session *session,
616                               unsigned char *buf, size_t len);
617 #endif /* MBEDTLS_SSL_SESSION_TICKETS */
618 
619 #if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SRV_C) && \
620     defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS) && \
621     defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
622 int mbedtls_test_get_tls13_ticket(
623     mbedtls_test_handshake_test_options *client_options,
624     mbedtls_test_handshake_test_options *server_options,
625     mbedtls_ssl_session *session);
626 #endif
627 
628 #define ECJPAKE_TEST_PWD        "bla"
629 
630 #if defined(MBEDTLS_USE_PSA_CRYPTO)
631 #define ECJPAKE_TEST_SET_PASSWORD(exp_ret_val)                            \
632     ret = (use_opaque_arg) ?                                              \
633           mbedtls_ssl_set_hs_ecjpake_password_opaque(&ssl, pwd_slot) :    \
634           mbedtls_ssl_set_hs_ecjpake_password(&ssl, pwd_string, pwd_len); \
635     TEST_EQUAL(ret, exp_ret_val)
636 #else
637 #define ECJPAKE_TEST_SET_PASSWORD(exp_ret_val)                            \
638     ret = mbedtls_ssl_set_hs_ecjpake_password(&ssl,                       \
639                                               pwd_string, pwd_len);       \
640     TEST_EQUAL(ret, exp_ret_val)
641 #endif /* MBEDTLS_USE_PSA_CRYPTO */
642 
643 #define TEST_AVAILABLE_ECC(tls_id_, group_id_, psa_family_, psa_bits_)   \
644     TEST_EQUAL(mbedtls_ssl_get_ecp_group_id_from_tls_id(tls_id_),        \
645                group_id_);                                               \
646     TEST_EQUAL(mbedtls_ssl_get_tls_id_from_ecp_group_id(group_id_),      \
647                tls_id_);                                                 \
648     TEST_EQUAL(mbedtls_ssl_get_psa_curve_info_from_tls_id(tls_id_,       \
649                                                           &psa_type, &psa_bits), PSA_SUCCESS);                \
650     TEST_EQUAL(psa_family_, PSA_KEY_TYPE_ECC_GET_FAMILY(psa_type));    \
651     TEST_EQUAL(psa_bits_, psa_bits);
652 
653 #define TEST_UNAVAILABLE_ECC(tls_id_, group_id_, psa_family_, psa_bits_) \
654     TEST_EQUAL(mbedtls_ssl_get_ecp_group_id_from_tls_id(tls_id_),        \
655                MBEDTLS_ECP_DP_NONE);                                     \
656     TEST_EQUAL(mbedtls_ssl_get_tls_id_from_ecp_group_id(group_id_),      \
657                0);                                                       \
658     TEST_EQUAL(mbedtls_ssl_get_psa_curve_info_from_tls_id(tls_id_,       \
659                                                           &psa_type, &psa_bits), \
660                PSA_ERROR_NOT_SUPPORTED);
661 
662 #endif /* MBEDTLS_SSL_TLS_C */
663 
664 #endif /* SSL_HELPERS_H */
665