quickjs-tart

quickjs-based runtime for wallet-core logic
Log | Files | Refs | README | LICENSE

ssl_helpers.h (25926B)


      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  * For example, to perform a full handshake:
    475  * ```
    476  * mbedtls_test_move_handshake_to_state(
    477  *                       &server.ssl, &client.ssl,
    478  *                       MBEDTLS_SSL_HANDSHAKE_OVER);
    479  * mbedtls_test_move_handshake_to_state(
    480  *                       &client.ssl, &server.ssl,
    481  *                       MBEDTLS_SSL_HANDSHAKE_OVER);
    482  * ```
    483  * Note that you need both calls to reach the handshake-over state on
    484  * both sides.
    485  *
    486  * \retval  0 on success, otherwise error code.
    487  */
    488 int mbedtls_test_move_handshake_to_state(mbedtls_ssl_context *ssl,
    489                                          mbedtls_ssl_context *second_ssl,
    490                                          int state);
    491 
    492 #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
    493 
    494 /*
    495  * Helper function setting up inverse record transformations
    496  * using given cipher, hash, EtM mode, authentication tag length,
    497  * and version.
    498  */
    499 #define CHK(x)                                  \
    500     do                                          \
    501     {                                           \
    502         if (!(x))                               \
    503         {                                       \
    504             ret = -1;                           \
    505             goto cleanup;                       \
    506         }                                       \
    507     } while (0)
    508 
    509 #if MBEDTLS_SSL_CID_OUT_LEN_MAX > MBEDTLS_SSL_CID_IN_LEN_MAX
    510 #define SSL_CID_LEN_MIN MBEDTLS_SSL_CID_IN_LEN_MAX
    511 #else
    512 #define SSL_CID_LEN_MIN MBEDTLS_SSL_CID_OUT_LEN_MAX
    513 #endif
    514 
    515 #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
    516     defined(MBEDTLS_SSL_HAVE_CBC) && defined(MBEDTLS_SSL_HAVE_AES)
    517 int mbedtls_test_psa_cipher_encrypt_helper(mbedtls_ssl_transform *transform,
    518                                            const unsigned char *iv,
    519                                            size_t iv_len,
    520                                            const unsigned char *input,
    521                                            size_t ilen,
    522                                            unsigned char *output,
    523                                            size_t *olen);
    524 #endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_HAVE_CBC &&
    525           MBEDTLS_SSL_HAVE_AES */
    526 
    527 int mbedtls_test_ssl_build_transforms(mbedtls_ssl_transform *t_in,
    528                                       mbedtls_ssl_transform *t_out,
    529                                       int cipher_type, int hash_id,
    530                                       int etm, int tag_mode,
    531                                       mbedtls_ssl_protocol_version tls_version,
    532                                       size_t cid0_len,
    533                                       size_t cid1_len);
    534 
    535 #if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
    536 /**
    537  * \param[in,out] record        The record to prepare.
    538  *                              It must contain the data to MAC at offset
    539  *                              `record->data_offset`, of length
    540  *                              `record->data_length`.
    541  *                              On success, write the MAC immediately
    542  *                              after the data and increment
    543  *                              `record->data_length` accordingly.
    544  * \param[in,out] transform_out The out transform, typically prepared by
    545  *                              mbedtls_test_ssl_build_transforms().
    546  *                              Its HMAC context may be used. Other than that
    547  *                              it is treated as an input parameter.
    548  *
    549  * \return                      0 on success, an `MBEDTLS_ERR_xxx` error code
    550  *                              or -1 on error.
    551  */
    552 int mbedtls_test_ssl_prepare_record_mac(mbedtls_record *record,
    553                                         mbedtls_ssl_transform *transform_out);
    554 #endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
    555 
    556 /*
    557  * Populate a session structure for serialization tests.
    558  * Choose dummy values, mostly non-0 to distinguish from the init default.
    559  */
    560 int mbedtls_test_ssl_tls12_populate_session(mbedtls_ssl_session *session,
    561                                             int ticket_len,
    562                                             int endpoint_type,
    563                                             const char *crt_file);
    564 
    565 #if defined(MBEDTLS_SSL_PROTO_TLS1_3)
    566 int mbedtls_test_ssl_tls13_populate_session(mbedtls_ssl_session *session,
    567                                             int ticket_len,
    568                                             int endpoint_type);
    569 #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
    570 
    571 /*
    572  * Perform data exchanging between \p ssl_1 and \p ssl_2 and check if the
    573  * message was sent in the correct number of fragments.
    574  *
    575  * /p ssl_1 and /p ssl_2    Endpoints represented by mbedtls_ssl_context. Both
    576  *                          of them must be initialized and connected
    577  *                          beforehand.
    578  * /p msg_len_1 and /p msg_len_2 specify the size of the message to send.
    579  * /p expected_fragments_1 and /p expected_fragments_2 determine in how many
    580  *                          fragments the message should be sent.
    581  *      expected_fragments is 0: can be used for DTLS testing while the message
    582  *                          size is larger than MFL. In that case the message
    583  *                          cannot be fragmented and sent to the second
    584  *                          endpoint.
    585  *                          This value can be used for negative tests.
    586  *      expected_fragments is 1: can be used for TLS/DTLS testing while the
    587  *                          message size is below MFL
    588  *      expected_fragments > 1: can be used for TLS testing while the message
    589  *                          size is larger than MFL
    590  *
    591  * \retval  0 on success, otherwise error code.
    592  */
    593 int mbedtls_test_ssl_exchange_data(
    594     mbedtls_ssl_context *ssl_1,
    595     int msg_len_1, const int expected_fragments_1,
    596     mbedtls_ssl_context *ssl_2,
    597     int msg_len_2, const int expected_fragments_2);
    598 
    599 #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
    600 int mbedtls_test_ssl_do_handshake_with_endpoints(
    601     mbedtls_test_ssl_endpoint *server_ep,
    602     mbedtls_test_ssl_endpoint *client_ep,
    603     mbedtls_test_handshake_test_options *options,
    604     mbedtls_ssl_protocol_version proto);
    605 #endif /* defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) */
    606 
    607 #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
    608 void mbedtls_test_ssl_perform_handshake(
    609     mbedtls_test_handshake_test_options *options);
    610 #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
    611 
    612 #if defined(MBEDTLS_TEST_HOOKS)
    613 /*
    614  * Tweak vector lengths in a TLS 1.3 Certificate message
    615  *
    616  * \param[in]       buf    Buffer containing the Certificate message to tweak
    617  * \param[in]]out]  end    End of the buffer to parse
    618  * \param           tweak  Tweak identifier (from 1 to the number of tweaks).
    619  * \param[out]  expected_result  Error code expected from the parsing function
    620  * \param[out]  args  Arguments of the MBEDTLS_SSL_CHK_BUF_READ_PTR call that
    621  *                    is expected to fail. All zeroes if no
    622  *                    MBEDTLS_SSL_CHK_BUF_READ_PTR failure is expected.
    623  */
    624 int mbedtls_test_tweak_tls13_certificate_msg_vector_len(
    625     unsigned char *buf, unsigned char **end, int tweak,
    626     int *expected_result, mbedtls_ssl_chk_buf_ptr_args *args);
    627 #endif /* MBEDTLS_TEST_HOOKS */
    628 
    629 #if defined(MBEDTLS_SSL_SESSION_TICKETS)
    630 int mbedtls_test_ticket_write(
    631     void *p_ticket, const mbedtls_ssl_session *session,
    632     unsigned char *start, const unsigned char *end,
    633     size_t *tlen, uint32_t *ticket_lifetime);
    634 
    635 int mbedtls_test_ticket_parse(void *p_ticket, mbedtls_ssl_session *session,
    636                               unsigned char *buf, size_t len);
    637 #endif /* MBEDTLS_SSL_SESSION_TICKETS */
    638 
    639 #if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SRV_C) && \
    640     defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS) && \
    641     defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
    642 int mbedtls_test_get_tls13_ticket(
    643     mbedtls_test_handshake_test_options *client_options,
    644     mbedtls_test_handshake_test_options *server_options,
    645     mbedtls_ssl_session *session);
    646 #endif
    647 
    648 #define ECJPAKE_TEST_PWD        "bla"
    649 
    650 #if defined(MBEDTLS_USE_PSA_CRYPTO)
    651 #define ECJPAKE_TEST_SET_PASSWORD(exp_ret_val)                            \
    652     ret = (use_opaque_arg) ?                                              \
    653           mbedtls_ssl_set_hs_ecjpake_password_opaque(&ssl, pwd_slot) :    \
    654           mbedtls_ssl_set_hs_ecjpake_password(&ssl, pwd_string, pwd_len); \
    655     TEST_EQUAL(ret, exp_ret_val)
    656 #else
    657 #define ECJPAKE_TEST_SET_PASSWORD(exp_ret_val)                            \
    658     ret = mbedtls_ssl_set_hs_ecjpake_password(&ssl,                       \
    659                                               pwd_string, pwd_len);       \
    660     TEST_EQUAL(ret, exp_ret_val)
    661 #endif /* MBEDTLS_USE_PSA_CRYPTO */
    662 
    663 #define TEST_AVAILABLE_ECC(tls_id_, group_id_, psa_family_, psa_bits_)   \
    664     TEST_EQUAL(mbedtls_ssl_get_ecp_group_id_from_tls_id(tls_id_),        \
    665                group_id_);                                               \
    666     TEST_EQUAL(mbedtls_ssl_get_tls_id_from_ecp_group_id(group_id_),      \
    667                tls_id_);                                                 \
    668     TEST_EQUAL(mbedtls_ssl_get_psa_curve_info_from_tls_id(tls_id_,       \
    669                                                           &psa_type, &psa_bits), PSA_SUCCESS);                \
    670     TEST_EQUAL(psa_family_, PSA_KEY_TYPE_ECC_GET_FAMILY(psa_type));    \
    671     TEST_EQUAL(psa_bits_, psa_bits);
    672 
    673 #define TEST_UNAVAILABLE_ECC(tls_id_, group_id_, psa_family_, psa_bits_) \
    674     TEST_EQUAL(mbedtls_ssl_get_ecp_group_id_from_tls_id(tls_id_),        \
    675                MBEDTLS_ECP_DP_NONE);                                     \
    676     TEST_EQUAL(mbedtls_ssl_get_tls_id_from_ecp_group_id(group_id_),      \
    677                0);                                                       \
    678     TEST_EQUAL(mbedtls_ssl_get_psa_curve_info_from_tls_id(tls_id_,       \
    679                                                           &psa_type, &psa_bits), \
    680                PSA_ERROR_NOT_SUPPORTED);
    681 
    682 #endif /* MBEDTLS_SSL_TLS_C */
    683 
    684 #endif /* SSL_HELPERS_H */