blob: 5bfdedaaf07a0c9638c18414683e35126c8813b7 [file] [log] [blame]
Yanray Wang47907a42022-10-24 14:42:01 +08001/** \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
Dave Rodgman16799db2023-11-02 19:47:20 +00008 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Yanray Wang47907a42022-10-24 14:42:01 +08009 */
10
11#ifndef SSL_HELPERS_H
12#define SSL_HELPERS_H
13
Yanray Wang4d07d1c2022-10-27 15:28:16 +080014#include "mbedtls/build_info.h"
15
16#include <string.h>
17
Yanray Wang47907a42022-10-24 14:42:01 +080018#include <test/helpers.h>
Yanray Wang4d07d1c2022-10-27 15:28:16 +080019#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>
Yanray Wang4d07d1c2022-10-27 15:28:16 +080027
28#include "test/certs.h"
Yanray Wang55a66192022-10-26 09:57:53 +080029
30#if defined(MBEDTLS_SSL_CACHE_C)
31#include "mbedtls/ssl_cache.h"
32#endif
33
Yanray Wang5ba709c2023-02-03 11:07:56 +080034#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
Ronald Cron43263c02023-03-09 16:48:10 +010040#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Elena Uziunaite6121a342024-07-05 11:16:53 +010041#if defined(PSA_WANT_KEY_TYPE_AES)
Elena Uziunaite83a0d9d2024-07-05 11:41:22 +010042#if defined(PSA_WANT_ALG_GCM)
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +010043#if defined(PSA_WANT_ALG_SHA_384)
Ronald Cron43263c02023-03-09 16:48:10 +010044#define MBEDTLS_TEST_HAS_TLS1_3_AES_256_GCM_SHA384
45#endif
Elena Uziunaite0916cd72024-05-23 17:01:07 +010046#if defined(PSA_WANT_ALG_SHA_256)
Ronald Cron43263c02023-03-09 16:48:10 +010047#define MBEDTLS_TEST_HAS_TLS1_3_AES_128_GCM_SHA256
48#endif
Elena Uziunaite83a0d9d2024-07-05 11:41:22 +010049#endif /* PSA_WANT_ALG_GCM */
Elena Uziunaitec2561722024-07-05 11:37:33 +010050#if defined(PSA_WANT_ALG_CCM) && defined(PSA_WANT_ALG_SHA_256)
Ronald Cron43263c02023-03-09 16:48:10 +010051#define MBEDTLS_TEST_HAS_TLS1_3_AES_128_CCM_SHA256
52#define MBEDTLS_TEST_HAS_TLS1_3_AES_128_CCM_8_SHA256
53#endif
Elena Uziunaite6121a342024-07-05 11:16:53 +010054#endif /* PSA_WANT_KEY_TYPE_AES */
Elena Uziunaite5c70c302024-07-05 11:44:44 +010055#if defined(PSA_WANT_ALG_CHACHA20_POLY1305) && defined(PSA_WANT_ALG_SHA_256)
Ronald Cron43263c02023-03-09 16:48:10 +010056#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
Gabor Mezeie1e27302025-02-26 18:06:05 +010069#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED)
Yanray Wang5ba709c2023-02-03 11:07:56 +080070#define MBEDTLS_CAN_HANDLE_RSA_TEST_KEY
71#endif
Gabor Mezei14950932025-03-06 16:06:42 +010072
Gabor Mezei8829aa32025-03-07 13:21:37 +010073#if defined(PSA_WANT_ALG_GCM) || \
74 defined(PSA_WANT_ALG_CCM) || \
Gabor Mezei14950932025-03-06 16:06:42 +010075 defined(PSA_WANT_ALG_CHACHA20_POLY1305)
76#define MBEDTLS_TEST_HAS_AEAD_ALG
77#endif
78
Yanray Wang5ba709c2023-02-03 11:07:56 +080079enum {
80#define MBEDTLS_SSL_TLS1_3_LABEL(name, string) \
81 tls13_label_ ## name,
82 MBEDTLS_SSL_TLS1_3_LABEL_LIST
83#undef MBEDTLS_SSL_TLS1_3_LABEL
84};
85
Waleed Elmelegy4dfb0e72024-03-14 01:48:40 +000086#if defined(MBEDTLS_SSL_ALPN)
87#define MBEDTLS_TEST_MAX_ALPN_LIST_SIZE 10
88#endif
89
Yanray Wang55a66192022-10-26 09:57:53 +080090typedef struct mbedtls_test_ssl_log_pattern {
91 const char *pattern;
92 size_t counter;
93} mbedtls_test_ssl_log_pattern;
94
95typedef struct mbedtls_test_handshake_test_options {
96 const char *cipher;
Ronald Cronfb536472024-01-26 14:55:25 +010097 uint16_t *group_list;
Yanray Wang55a66192022-10-26 09:57:53 +080098 mbedtls_ssl_protocol_version client_min_version;
99 mbedtls_ssl_protocol_version client_max_version;
100 mbedtls_ssl_protocol_version server_min_version;
101 mbedtls_ssl_protocol_version server_max_version;
102 mbedtls_ssl_protocol_version expected_negotiated_version;
103 int expected_handshake_result;
104 int expected_ciphersuite;
105 int pk_alg;
106 int opaque_alg;
107 int opaque_alg2;
108 int opaque_usage;
109 data_t *psk_str;
110 int dtls;
111 int srv_auth_mode;
112 int serialize;
113 int mfl;
114 int cli_msg_len;
115 int srv_msg_len;
116 int expected_cli_fragments;
117 int expected_srv_fragments;
118 int renegotiate;
119 int legacy_renegotiation;
120 void *srv_log_obj;
121 void *cli_log_obj;
122 void (*srv_log_fun)(void *, int, const char *, int, const char *);
123 void (*cli_log_fun)(void *, int, const char *, int, const char *);
124 int resize_buffers;
Ronald Cronced99be2024-01-26 15:49:12 +0100125 int early_data;
Ronald Cron5d3036e2024-02-23 07:43:45 +0100126 int max_early_data_size;
Yanray Wang55a66192022-10-26 09:57:53 +0800127#if defined(MBEDTLS_SSL_CACHE_C)
128 mbedtls_ssl_cache_context *cache;
129#endif
Waleed Elmelegy4dfb0e72024-03-14 01:48:40 +0000130#if defined(MBEDTLS_SSL_ALPN)
131 const char *alpn_list[MBEDTLS_TEST_MAX_ALPN_LIST_SIZE];
132#endif
Yanray Wang55a66192022-10-26 09:57:53 +0800133} mbedtls_test_handshake_test_options;
134
Yanray Wang25b766f2023-03-15 16:39:05 +0800135/*
136 * Buffer structure for custom I/O callbacks.
137 */
Yanray Wang55a66192022-10-26 09:57:53 +0800138typedef struct mbedtls_test_ssl_buffer {
139 size_t start;
140 size_t content_length;
141 size_t capacity;
142 unsigned char *buffer;
143} mbedtls_test_ssl_buffer;
144
145/*
146 * Context for a message metadata queue (fifo) that is on top of the ring buffer.
147 */
148typedef struct mbedtls_test_ssl_message_queue {
149 size_t *messages;
150 int pos;
151 int num;
152 int capacity;
153} mbedtls_test_ssl_message_queue;
154
155/*
156 * Context for the I/O callbacks simulating network connection.
157 */
158
159#define MBEDTLS_MOCK_SOCKET_CONNECTED 1
160
161typedef struct mbedtls_test_mock_socket {
162 int status;
163 mbedtls_test_ssl_buffer *input;
164 mbedtls_test_ssl_buffer *output;
165 struct mbedtls_test_mock_socket *peer;
166} mbedtls_test_mock_socket;
167
168/* Errors used in the message socket mocks */
169
170#define MBEDTLS_TEST_ERROR_CONTEXT_ERROR -55
171#define MBEDTLS_TEST_ERROR_SEND_FAILED -66
172#define MBEDTLS_TEST_ERROR_RECV_FAILED -77
173
174/*
175 * Structure used as an addon, or a wrapper, around the mocked sockets.
176 * Contains an input queue, to which the other socket pushes metadata,
177 * and an output queue, to which this one pushes metadata. This context is
178 * considered as an owner of the input queue only, which is initialized and
179 * freed in the respective setup and free calls.
180 */
181typedef struct mbedtls_test_message_socket_context {
182 mbedtls_test_ssl_message_queue *queue_input;
183 mbedtls_test_ssl_message_queue *queue_output;
184 mbedtls_test_mock_socket *socket;
185} mbedtls_test_message_socket_context;
186
187#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
188
189/*
Yanray Wang55a66192022-10-26 09:57:53 +0800190 * Endpoint structure for SSL communication tests.
191 */
192typedef struct mbedtls_test_ssl_endpoint {
193 const char *name;
194 mbedtls_ssl_context ssl;
195 mbedtls_ssl_config conf;
196 mbedtls_test_mock_socket socket;
Gilles Peskine00eb0722025-06-01 21:50:05 +0200197 uintptr_t user_data_cookie; /* A unique value associated with this endpoint */
Gilles Peskine29969592025-05-27 19:24:28 +0200198
199 /* Objects only used by DTLS.
200 * They should be guarded by MBEDTLS_SSL_PROTO_DTLS, but
201 * currently aren't because some code accesses them without guards. */
202 mbedtls_test_message_socket_context dtls_context;
Gilles Peskine0677e022025-05-27 18:05:20 +0200203#if defined(MBEDTLS_TIMING_C)
204 mbedtls_timing_delay_context timer;
205#endif
Gilles Peskineb6bb3fb2025-05-26 21:57:52 +0200206
207 /* Objects owned by the endpoint */
Gilles Peskine2744a432025-05-27 13:27:22 +0200208 int *ciphersuites;
Gilles Peskine6c154e72025-05-27 20:23:52 +0200209 mbedtls_test_ssl_message_queue queue_input;
Gilles Peskineb6bb3fb2025-05-26 21:57:52 +0200210 mbedtls_x509_crt *ca_chain;
211 mbedtls_x509_crt *cert;
212 mbedtls_pk_context *pkey;
Yanray Wang55a66192022-10-26 09:57:53 +0800213} mbedtls_test_ssl_endpoint;
214
215#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Yanray Wang47907a42022-10-24 14:42:01 +0800216
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800217/*
Ronald Cron10b040f2024-02-05 09:38:09 +0100218 * Random number generator aimed for TLS unitary tests. Its main purpose is to
219 * simplify the set-up of a random number generator for TLS
220 * unitary tests: no need to set up a good entropy source for example.
221 */
222int mbedtls_test_random(void *p_rng, unsigned char *output, size_t output_len);
Yanray Wangf88e5292023-12-01 16:39:34 +0800223
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800224/*
225 * This function can be passed to mbedtls to receive output logs from it. In
226 * this case, it will count the instances of a mbedtls_test_ssl_log_pattern
227 * in the received logged messages.
228 */
229void mbedtls_test_ssl_log_analyzer(void *ctx, int level,
230 const char *file, int line,
231 const char *str);
232
233void mbedtls_test_init_handshake_options(
234 mbedtls_test_handshake_test_options *opts);
235
236void mbedtls_test_free_handshake_options(
237 mbedtls_test_handshake_test_options *opts);
238
239/*
240 * Initialises \p buf. After calling this function it is safe to call
241 * `mbedtls_test_ssl_buffer_free()` on \p buf.
242 */
243void mbedtls_test_ssl_buffer_init(mbedtls_test_ssl_buffer *buf);
244
245/*
246 * Sets up \p buf. After calling this function it is safe to call
247 * `mbedtls_test_ssl_buffer_put()` and `mbedtls_test_ssl_buffer_get()`
248 * on \p buf.
249 */
250int mbedtls_test_ssl_buffer_setup(mbedtls_test_ssl_buffer *buf,
251 size_t capacity);
252
253void mbedtls_test_ssl_buffer_free(mbedtls_test_ssl_buffer *buf);
254
255/*
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800256 * Puts \p input_len bytes from the \p input buffer into the ring buffer \p buf.
257 *
258 * \p buf must have been initialized and set up by calling
259 * `mbedtls_test_ssl_buffer_init()` and `mbedtls_test_ssl_buffer_setup()`.
260 *
261 * \retval \p input_len, if the data fits.
262 * \retval 0 <= value < \p input_len, if the data does not fit.
263 * \retval -1, if \p buf is NULL, it hasn't been set up or \p input_len is not
264 * zero and \p input is NULL.
265 */
266int mbedtls_test_ssl_buffer_put(mbedtls_test_ssl_buffer *buf,
267 const unsigned char *input, size_t input_len);
268
269/*
270 * Gets \p output_len bytes from the ring buffer \p buf into the
271 * \p output buffer. The output buffer can be NULL, in this case a part of the
272 * ring buffer will be dropped, if the requested length is available.
273 *
274 * \p buf must have been initialized and set up by calling
275 * `mbedtls_test_ssl_buffer_init()` and `mbedtls_test_ssl_buffer_setup()`.
276 *
277 * \retval \p output_len, if the data is available.
278 * \retval 0 <= value < \p output_len, if the data is not available.
279 * \retval -1, if \buf is NULL or it hasn't been set up.
280 */
281int mbedtls_test_ssl_buffer_get(mbedtls_test_ssl_buffer *buf,
282 unsigned char *output, size_t output_len);
283
284/*
285 * Errors used in the message transport mock tests
286 */
287 #define MBEDTLS_TEST_ERROR_ARG_NULL -11
288 #define MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED -44
289
290/*
291 * Setup and free functions for the message metadata queue.
292 *
293 * \p capacity describes the number of message metadata chunks that can be held
294 * within the queue.
295 *
296 * \retval 0, if a metadata queue of a given length can be allocated.
297 * \retval MBEDTLS_ERR_SSL_ALLOC_FAILED, if allocation failed.
298 */
299int mbedtls_test_ssl_message_queue_setup(
300 mbedtls_test_ssl_message_queue *queue, size_t capacity);
301
302void mbedtls_test_ssl_message_queue_free(
303 mbedtls_test_ssl_message_queue *queue);
304
305/*
306 * Push message length information onto the message metadata queue.
307 * This will become the last element to leave it (fifo).
308 *
309 * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null.
310 * \retval MBEDTLS_ERR_SSL_WANT_WRITE, if the queue is full.
311 * \retval \p len, if the push was successful.
312 */
313int mbedtls_test_ssl_message_queue_push_info(
314 mbedtls_test_ssl_message_queue *queue, size_t len);
315
316/*
317 * Pop information about the next message length from the queue. This will be
318 * the oldest inserted message length(fifo). \p msg_len can be null, in which
319 * case the data will be popped from the queue but not copied anywhere.
320 *
321 * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null.
322 * \retval MBEDTLS_ERR_SSL_WANT_READ, if the queue is empty.
323 * \retval message length, if the pop was successful, up to the given
324 \p buf_len.
325 */
326int mbedtls_test_ssl_message_queue_pop_info(
327 mbedtls_test_ssl_message_queue *queue, size_t buf_len);
328
329/*
330 * Setup and teardown functions for mock sockets.
331 */
Yanray Wang5f86a422023-03-15 16:02:29 +0800332void mbedtls_test_mock_socket_init(mbedtls_test_mock_socket *socket);
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800333
334/*
335 * Closes the socket \p socket.
336 *
337 * \p socket must have been previously initialized by calling
Yanray Wang5f86a422023-03-15 16:02:29 +0800338 * mbedtls_test_mock_socket_init().
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800339 *
340 * This function frees all allocated resources and both sockets are aware of the
341 * new connection state.
342 *
343 * That is, this function does not simulate half-open TCP connections and the
344 * phenomenon that when closing a UDP connection the peer is not aware of the
345 * connection having been closed.
346 */
347void mbedtls_test_mock_socket_close(mbedtls_test_mock_socket *socket);
348
349/*
350 * Establishes a connection between \p peer1 and \p peer2.
351 *
352 * \p peer1 and \p peer2 must have been previously initialized by calling
Yanray Wang5f86a422023-03-15 16:02:29 +0800353 * mbedtls_test_mock_socket_init().
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800354 *
355 * The capacities of the internal buffers are set to \p bufsize. Setting this to
356 * the correct value allows for simulation of MTU, sanity testing the mock
357 * implementation and mocking TCP connections with lower memory cost.
358 */
359int mbedtls_test_mock_socket_connect(mbedtls_test_mock_socket *peer1,
360 mbedtls_test_mock_socket *peer2,
361 size_t bufsize);
362
363
364/*
365 * Callbacks for simulating blocking I/O over connection-oriented transport.
366 */
Yanray Wangaf727a22023-03-13 19:22:36 +0800367int mbedtls_test_mock_tcp_send_b(void *ctx,
368 const unsigned char *buf, size_t len);
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800369
370int mbedtls_test_mock_tcp_recv_b(void *ctx, unsigned char *buf, size_t len);
371
372/*
373 * Callbacks for simulating non-blocking I/O over connection-oriented transport.
374 */
Yanray Wangaf727a22023-03-13 19:22:36 +0800375int mbedtls_test_mock_tcp_send_nb(void *ctx,
376 const unsigned char *buf, size_t len);
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800377
378int mbedtls_test_mock_tcp_recv_nb(void *ctx, unsigned char *buf, size_t len);
379
380void mbedtls_test_message_socket_init(
381 mbedtls_test_message_socket_context *ctx);
382
383/*
384 * Setup a given message socket context including initialization of
385 * input/output queues to a chosen capacity of messages. Also set the
386 * corresponding mock socket.
387 *
388 * \retval 0, if everything succeeds.
389 * \retval MBEDTLS_ERR_SSL_ALLOC_FAILED, if allocation of a message
390 * queue failed.
391 */
392int mbedtls_test_message_socket_setup(
393 mbedtls_test_ssl_message_queue *queue_input,
394 mbedtls_test_ssl_message_queue *queue_output,
Yanray Wangd19894f2023-03-16 11:47:39 +0800395 size_t queue_capacity,
396 mbedtls_test_mock_socket *socket,
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800397 mbedtls_test_message_socket_context *ctx);
398
399/*
400 * Close a given message socket context, along with the socket itself. Free the
401 * memory allocated by the input queue.
402 */
403void mbedtls_test_message_socket_close(
404 mbedtls_test_message_socket_context *ctx);
405
406/*
407 * Send one message through a given message socket context.
408 *
409 * \retval \p len, if everything succeeds.
410 * \retval MBEDTLS_TEST_ERROR_CONTEXT_ERROR, if any of the needed context
411 * elements or the context itself is null.
412 * \retval MBEDTLS_TEST_ERROR_SEND_FAILED if
413 * mbedtls_test_mock_tcp_send_b failed.
414 * \retval MBEDTLS_ERR_SSL_WANT_WRITE, if the output queue is full.
415 *
416 * This function will also return any error from
417 * mbedtls_test_ssl_message_queue_push_info.
418 */
Yanray Wangaf727a22023-03-13 19:22:36 +0800419int mbedtls_test_mock_tcp_send_msg(void *ctx,
420 const unsigned char *buf, size_t len);
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800421
422/*
423 * Receive one message from a given message socket context and return message
424 * length or an error.
425 *
426 * \retval message length, if everything succeeds.
427 * \retval MBEDTLS_TEST_ERROR_CONTEXT_ERROR, if any of the needed context
428 * elements or the context itself is null.
429 * \retval MBEDTLS_TEST_ERROR_RECV_FAILED if
430 * mbedtls_test_mock_tcp_recv_b failed.
431 *
432 * This function will also return any error other than
Yanray Wang5e22a922023-03-16 14:57:54 +0800433 * MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED from test_ssl_message_queue_peek_info.
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800434 */
Yanray Wangaf727a22023-03-13 19:22:36 +0800435int mbedtls_test_mock_tcp_recv_msg(void *ctx,
436 unsigned char *buf, size_t buf_len);
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800437
438#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
439
440/*
Gilles Peskineb6bb3fb2025-05-26 21:57:52 +0200441 * Load default CA certificates and endpoint keys into \p ep.
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800442 *
443 * \retval 0 on success, otherwise error code.
444 */
445int mbedtls_test_ssl_endpoint_certificate_init(mbedtls_test_ssl_endpoint *ep,
446 int pk_alg,
447 int opaque_alg, int opaque_alg2,
448 int opaque_usage);
449
Gilles Peskine6edb76c2025-06-01 21:53:52 +0200450/** Initialize the configuration in an SSL endpoint structure.
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800451 *
Gilles Peskine6edb76c2025-06-01 21:53:52 +0200452 * \note You must call `mbedtls_test_ssl_endpoint_free()` after
453 * calling this function, even if it fails. This is necessary to
454 * free data that may have been stored in the endpoint structure.
Gilles Peskineb092e782025-05-27 20:15:03 +0200455 *
Gilles Peskine6edb76c2025-06-01 21:53:52 +0200456 * \param[out] ep The endpoint structure to configure.
457 * \param endpoint_type #MBEDTLS_SSL_IS_SERVER or #MBEDTLS_SSL_IS_CLIENT.
458 * \param[in] options The options to use for configuring the endpoint
459 * structure.
460 *
461 * \retval 0 on success, otherwise error code.
462 */
463int mbedtls_test_ssl_endpoint_init_conf(
464 mbedtls_test_ssl_endpoint *ep, int endpoint_type,
465 const mbedtls_test_handshake_test_options *options);
466
467/** Initialize the session context in an endpoint structure.
468 *
469 * \note The endpoint structure must have been set up with
470 * mbedtls_test_ssl_endpoint_init_conf() with the same \p options.
471 * Between calling mbedtls_test_ssl_endpoint_init_conf() and
472 * mbedtls_test_ssl_endpoint_init_ssl(), you may configure `ep->ssl`
473 * further if you know what you're doing.
474 *
475 * \note You must call `mbedtls_test_ssl_endpoint_free()` after
476 * calling this function, even if it fails. This is necessary to
477 * free data that may have been stored in the endpoint structure.
478 *
479 * \param[out] ep The endpoint structure to set up.
480 * \param[in] options The options used for configuring the endpoint
481 * structure.
482 *
483 * \retval 0 on success, otherwise error code.
484 */
485int mbedtls_test_ssl_endpoint_init_ssl(
486 mbedtls_test_ssl_endpoint *ep,
487 const mbedtls_test_handshake_test_options *options);
488
489/** Initialize the configuration and a context in an SSL endpoint structure.
490 *
491 * This function is equivalent to calling
492 * mbedtls_test_ssl_endpoint_init_conf() followed by
493 * mbedtls_test_ssl_endpoint_init_ssl().
494 *
495 * \note You must call `mbedtls_test_ssl_endpoint_free()` after
496 * calling this function, even if it fails. This is necessary to
497 * free data that may have been stored in the endpoint structure.
498 *
499 * \param[out] ep The endpoint structure to configure.
500 * \param endpoint_type #MBEDTLS_SSL_IS_SERVER or #MBEDTLS_SSL_IS_CLIENT.
501 * \param[in] options The options to use for configuring the endpoint
502 * structure.
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800503 *
504 * \retval 0 on success, otherwise error code.
505 */
506int mbedtls_test_ssl_endpoint_init(
507 mbedtls_test_ssl_endpoint *ep, int endpoint_type,
Gilles Peskineca8a9ac2025-05-27 20:52:24 +0200508 const mbedtls_test_handshake_test_options *options);
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800509
510/*
511 * Deinitializes endpoint represented by \p ep.
512 */
Gilles Peskineca8a9ac2025-05-27 20:52:24 +0200513void mbedtls_test_ssl_endpoint_free(mbedtls_test_ssl_endpoint *ep);
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800514
Gilles Peskineb092e782025-05-27 20:15:03 +0200515/* Join a DTLS client with a DTLS server.
516 *
517 * You must call this function after setting up the endpoint objects
518 * and before starting a DTLS handshake.
519 *
520 * \param client The client. It must have been set up with
521 * mbedtls_test_ssl_endpoint_init().
522 * \param server The server. It must have been set up with
523 * mbedtls_test_ssl_endpoint_init().
524 *
525 * \retval 0 on success, otherwise error code.
526 */
527int mbedtls_test_ssl_dtls_join_endpoints(mbedtls_test_ssl_endpoint *client,
528 mbedtls_test_ssl_endpoint *server);
529
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800530/*
531 * This function moves ssl handshake from \p ssl to prescribed \p state.
532 * /p second_ssl is used as second endpoint and their sockets have to be
533 * connected before calling this function.
534 *
Gilles Peskinea4bf0022025-03-07 20:37:49 +0100535 * For example, to perform a full handshake:
536 * ```
537 * mbedtls_test_move_handshake_to_state(
538 * &server.ssl, &client.ssl,
539 * MBEDTLS_SSL_HANDSHAKE_OVER);
540 * mbedtls_test_move_handshake_to_state(
Gilles Peskine161cadd2025-03-10 14:24:22 +0100541 * &client.ssl, &server.ssl,
Gilles Peskinea4bf0022025-03-07 20:37:49 +0100542 * MBEDTLS_SSL_HANDSHAKE_OVER);
543 * ```
544 * Note that you need both calls to reach the handshake-over state on
545 * both sides.
546 *
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800547 * \retval 0 on success, otherwise error code.
548 */
549int mbedtls_test_move_handshake_to_state(mbedtls_ssl_context *ssl,
550 mbedtls_ssl_context *second_ssl,
551 int state);
552
553#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
554
Yanray Wang1fca4de2023-02-06 12:10:48 +0800555/*
556 * Helper function setting up inverse record transformations
557 * using given cipher, hash, EtM mode, authentication tag length,
558 * and version.
559 */
560#define CHK(x) \
561 do \
562 { \
563 if (!(x)) \
564 { \
565 ret = -1; \
566 goto cleanup; \
567 } \
568 } while (0)
569
Yanray Wang25b766f2023-03-15 16:39:05 +0800570#if MBEDTLS_SSL_CID_OUT_LEN_MAX > MBEDTLS_SSL_CID_IN_LEN_MAX
571#define SSL_CID_LEN_MIN MBEDTLS_SSL_CID_IN_LEN_MAX
572#else
573#define SSL_CID_LEN_MIN MBEDTLS_SSL_CID_OUT_LEN_MAX
574#endif
575
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800576#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Elena Uziunaite74342c72024-07-05 11:31:29 +0100577 defined(PSA_WANT_ALG_CBC_NO_PADDING) && defined(PSA_WANT_KEY_TYPE_AES)
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800578int mbedtls_test_psa_cipher_encrypt_helper(mbedtls_ssl_transform *transform,
579 const unsigned char *iv,
580 size_t iv_len,
581 const unsigned char *input,
582 size_t ilen,
583 unsigned char *output,
584 size_t *olen);
Elena Uziunaite74342c72024-07-05 11:31:29 +0100585#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && PSA_WANT_ALG_CBC_NO_PADDING &&
Elena Uziunaite6121a342024-07-05 11:16:53 +0100586 PSA_WANT_KEY_TYPE_AES */
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800587
588int mbedtls_test_ssl_build_transforms(mbedtls_ssl_transform *t_in,
589 mbedtls_ssl_transform *t_out,
590 int cipher_type, int hash_id,
591 int etm, int tag_mode,
592 mbedtls_ssl_protocol_version tls_version,
593 size_t cid0_len,
594 size_t cid1_len);
595
Gilles Peskine9099d3f2023-09-18 13:11:50 +0200596#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
597/**
598 * \param[in,out] record The record to prepare.
599 * It must contain the data to MAC at offset
600 * `record->data_offset`, of length
601 * `record->data_length`.
602 * On success, write the MAC immediately
603 * after the data and increment
604 * `record->data_length` accordingly.
605 * \param[in,out] transform_out The out transform, typically prepared by
606 * mbedtls_test_ssl_build_transforms().
607 * Its HMAC context may be used. Other than that
608 * it is treated as an input parameter.
609 *
610 * \return 0 on success, an `MBEDTLS_ERR_xxx` error code
611 * or -1 on error.
612 */
613int mbedtls_test_ssl_prepare_record_mac(mbedtls_record *record,
614 mbedtls_ssl_transform *transform_out);
615#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
616
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800617/*
618 * Populate a session structure for serialization tests.
619 * Choose dummy values, mostly non-0 to distinguish from the init default.
620 */
621int mbedtls_test_ssl_tls12_populate_session(mbedtls_ssl_session *session,
622 int ticket_len,
Ronald Cron7b1921a2023-11-23 12:31:56 +0100623 int endpoint_type,
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800624 const char *crt_file);
625
626#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
627int mbedtls_test_ssl_tls13_populate_session(mbedtls_ssl_session *session,
628 int ticket_len,
629 int endpoint_type);
630#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
631
632/*
633 * Perform data exchanging between \p ssl_1 and \p ssl_2 and check if the
634 * message was sent in the correct number of fragments.
635 *
636 * /p ssl_1 and /p ssl_2 Endpoints represented by mbedtls_ssl_context. Both
637 * of them must be initialized and connected
638 * beforehand.
639 * /p msg_len_1 and /p msg_len_2 specify the size of the message to send.
640 * /p expected_fragments_1 and /p expected_fragments_2 determine in how many
641 * fragments the message should be sent.
642 * expected_fragments is 0: can be used for DTLS testing while the message
643 * size is larger than MFL. In that case the message
644 * cannot be fragmented and sent to the second
645 * endpoint.
646 * This value can be used for negative tests.
647 * expected_fragments is 1: can be used for TLS/DTLS testing while the
648 * message size is below MFL
649 * expected_fragments > 1: can be used for TLS testing while the message
650 * size is larger than MFL
651 *
652 * \retval 0 on success, otherwise error code.
653 */
Yanray Wangb088bfc2023-03-16 12:15:49 +0800654int mbedtls_test_ssl_exchange_data(
655 mbedtls_ssl_context *ssl_1,
656 int msg_len_1, const int expected_fragments_1,
657 mbedtls_ssl_context *ssl_2,
658 int msg_len_2, const int expected_fragments_2);
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800659
660#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Max Fillingercf007ca2024-10-29 16:57:09 +0100661int mbedtls_test_ssl_do_handshake_with_endpoints(
662 mbedtls_test_ssl_endpoint *server_ep,
663 mbedtls_test_ssl_endpoint *client_ep,
Max Fillinger8f12e312024-10-30 00:29:37 +0100664 mbedtls_test_handshake_test_options *options,
Max Fillingercf007ca2024-10-29 16:57:09 +0100665 mbedtls_ssl_protocol_version proto);
666#endif /* defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) */
667
668#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gilles Peskine7a8fd462025-05-28 15:41:54 +0200669/** Perform an SSL handshake and exchange data over the connection.
670 *
671 * This function also handles cases where the handshake is expected to fail.
672 *
673 * If the handshake succeeds as expected, this function validates that
674 * connection parameters are as expected, exchanges data over the
675 * connection, and exercises some optional protocol features if they
676 * are enabled. See the code to see what features are validated and exercised.
677 *
678 * The handshake is expected to fail in the following cases:
679 * - If `options->expected_handshake_result != 0`.
680 * - If `options->expected_negotiated_version == MBEDTLS_SSL_VERSION_UNKNOWN`.
681 *
682 * \param[in] options Options for the connection.
683 * \param client The client endpoint. It must have been set up with
684 * mbedtls_test_ssl_endpoint_init() with \p options
685 * and #MBEDTLS_SSL_IS_CLIENT.
686 * \param server The server endpoint. It must have been set up with
687 * mbedtls_test_ssl_endpoint_init() with \p options
688 * and #MBEDTLS_SSL_IS_CLIENT.
689 *
690 * \return 1 on success, 0 on failure. On failure, this function
691 * calls mbedtls_test_fail(), indicating the failure
692 * reason and location. The causes of failure are:
693 * - Inconsistent options or bad endpoint state.
694 * - Operational problem during the handshake.
695 * - The handshake was expected to pass, but failed.
696 * - The handshake was expected to fail, but passed or
697 * failed with a different result.
698 * - The handshake passed as expected, but some connection
699 * parameter (e.g. protocol version, cipher suite, ...)
700 * is not as expected.
701 * - The handshake passed as expected, but something
702 * went wrong when attempting to exchange data.
703 * - The handshake passed as expected, but something
704 * went wrong when exercising other features
705 * (e.g. renegotiation, serialization, ...).
706 */
707int mbedtls_test_ssl_perform_connection(
708 const mbedtls_test_handshake_test_options *options,
709 mbedtls_test_ssl_endpoint *client,
710 mbedtls_test_ssl_endpoint *server);
711
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800712void mbedtls_test_ssl_perform_handshake(
Gilles Peskine9b993682025-05-27 18:44:12 +0200713 const mbedtls_test_handshake_test_options *options);
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800714#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
715
716#if defined(MBEDTLS_TEST_HOOKS)
717/*
718 * Tweak vector lengths in a TLS 1.3 Certificate message
719 *
720 * \param[in] buf Buffer containing the Certificate message to tweak
721 * \param[in]]out] end End of the buffer to parse
722 * \param tweak Tweak identifier (from 1 to the number of tweaks).
723 * \param[out] expected_result Error code expected from the parsing function
724 * \param[out] args Arguments of the MBEDTLS_SSL_CHK_BUF_READ_PTR call that
725 * is expected to fail. All zeroes if no
726 * MBEDTLS_SSL_CHK_BUF_READ_PTR failure is expected.
727 */
Yanray Wangf56181a2023-03-16 12:21:33 +0800728int mbedtls_test_tweak_tls13_certificate_msg_vector_len(
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800729 unsigned char *buf, unsigned char **end, int tweak,
730 int *expected_result, mbedtls_ssl_chk_buf_ptr_args *args);
731#endif /* MBEDTLS_TEST_HOOKS */
Yanray Wang1db628f2023-02-03 11:01:29 +0800732
Ronald Cron77abfe62024-01-15 11:17:31 +0100733#if defined(MBEDTLS_SSL_SESSION_TICKETS)
734int mbedtls_test_ticket_write(
735 void *p_ticket, const mbedtls_ssl_session *session,
736 unsigned char *start, const unsigned char *end,
737 size_t *tlen, uint32_t *ticket_lifetime);
738
739int mbedtls_test_ticket_parse(void *p_ticket, mbedtls_ssl_session *session,
740 unsigned char *buf, size_t len);
741#endif /* MBEDTLS_SSL_SESSION_TICKETS */
742
Ronald Cron1f6e4e42024-01-26 16:31:33 +0100743#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SRV_C) && \
744 defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS) && \
745 defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
746int mbedtls_test_get_tls13_ticket(
747 mbedtls_test_handshake_test_options *client_options,
748 mbedtls_test_handshake_test_options *server_options,
749 mbedtls_ssl_session *session);
Ronald Cronb9a9b1f2024-02-14 11:28:05 +0100750#endif
Ronald Cron1f6e4e42024-01-26 16:31:33 +0100751
Yanray Wang1db628f2023-02-03 11:01:29 +0800752#define ECJPAKE_TEST_PWD "bla"
753
754#if defined(MBEDTLS_USE_PSA_CRYPTO)
755#define ECJPAKE_TEST_SET_PASSWORD(exp_ret_val) \
756 ret = (use_opaque_arg) ? \
757 mbedtls_ssl_set_hs_ecjpake_password_opaque(&ssl, pwd_slot) : \
758 mbedtls_ssl_set_hs_ecjpake_password(&ssl, pwd_string, pwd_len); \
759 TEST_EQUAL(ret, exp_ret_val)
760#else
761#define ECJPAKE_TEST_SET_PASSWORD(exp_ret_val) \
762 ret = mbedtls_ssl_set_hs_ecjpake_password(&ssl, \
763 pwd_string, pwd_len); \
764 TEST_EQUAL(ret, exp_ret_val)
765#endif /* MBEDTLS_USE_PSA_CRYPTO */
766
Yanray Wang09a6f7e2023-02-03 11:04:38 +0800767#define TEST_AVAILABLE_ECC(tls_id_, group_id_, psa_family_, psa_bits_) \
768 TEST_EQUAL(mbedtls_ssl_get_ecp_group_id_from_tls_id(tls_id_), \
769 group_id_); \
770 TEST_EQUAL(mbedtls_ssl_get_tls_id_from_ecp_group_id(group_id_), \
771 tls_id_); \
772 TEST_EQUAL(mbedtls_ssl_get_psa_curve_info_from_tls_id(tls_id_, \
Przemek Stekielda4fba62023-06-02 14:52:28 +0200773 &psa_type, &psa_bits), PSA_SUCCESS); \
774 TEST_EQUAL(psa_family_, PSA_KEY_TYPE_ECC_GET_FAMILY(psa_type)); \
Yanray Wang09a6f7e2023-02-03 11:04:38 +0800775 TEST_EQUAL(psa_bits_, psa_bits);
776
777#define TEST_UNAVAILABLE_ECC(tls_id_, group_id_, psa_family_, psa_bits_) \
778 TEST_EQUAL(mbedtls_ssl_get_ecp_group_id_from_tls_id(tls_id_), \
779 MBEDTLS_ECP_DP_NONE); \
780 TEST_EQUAL(mbedtls_ssl_get_tls_id_from_ecp_group_id(group_id_), \
781 0); \
782 TEST_EQUAL(mbedtls_ssl_get_psa_curve_info_from_tls_id(tls_id_, \
Przemek Stekielda4fba62023-06-02 14:52:28 +0200783 &psa_type, &psa_bits), \
Yanray Wang09a6f7e2023-02-03 11:04:38 +0800784 PSA_ERROR_NOT_SUPPORTED);
785
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800786#endif /* MBEDTLS_SSL_TLS_C */
787
Yanray Wang47907a42022-10-24 14:42:01 +0800788#endif /* SSL_HELPERS_H */