blob: dc2ab78691d8a8828b8bf70f73e66436614f8508 [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 Peskine29969592025-05-27 19:24:28 +0200197
198 /* Objects only used by DTLS.
199 * They should be guarded by MBEDTLS_SSL_PROTO_DTLS, but
200 * currently aren't because some code accesses them without guards. */
201 mbedtls_test_message_socket_context dtls_context;
Gilles Peskine0677e022025-05-27 18:05:20 +0200202#if defined(MBEDTLS_TIMING_C)
203 mbedtls_timing_delay_context timer;
204#endif
Gilles Peskineb6bb3fb2025-05-26 21:57:52 +0200205
206 /* Objects owned by the endpoint */
Gilles Peskine2744a432025-05-27 13:27:22 +0200207 int *ciphersuites;
Gilles Peskine6c154e72025-05-27 20:23:52 +0200208 mbedtls_test_ssl_message_queue queue_input;
Gilles Peskineb6bb3fb2025-05-26 21:57:52 +0200209 mbedtls_x509_crt *ca_chain;
210 mbedtls_x509_crt *cert;
211 mbedtls_pk_context *pkey;
Yanray Wang55a66192022-10-26 09:57:53 +0800212} mbedtls_test_ssl_endpoint;
213
214#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Yanray Wang47907a42022-10-24 14:42:01 +0800215
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800216/*
Ronald Cron10b040f2024-02-05 09:38:09 +0100217 * Random number generator aimed for TLS unitary tests. Its main purpose is to
218 * simplify the set-up of a random number generator for TLS
219 * unitary tests: no need to set up a good entropy source for example.
220 */
221int mbedtls_test_random(void *p_rng, unsigned char *output, size_t output_len);
Yanray Wangf88e5292023-12-01 16:39:34 +0800222
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800223/*
224 * This function can be passed to mbedtls to receive output logs from it. In
225 * this case, it will count the instances of a mbedtls_test_ssl_log_pattern
226 * in the received logged messages.
227 */
228void mbedtls_test_ssl_log_analyzer(void *ctx, int level,
229 const char *file, int line,
230 const char *str);
231
232void mbedtls_test_init_handshake_options(
233 mbedtls_test_handshake_test_options *opts);
234
235void mbedtls_test_free_handshake_options(
236 mbedtls_test_handshake_test_options *opts);
237
238/*
239 * Initialises \p buf. After calling this function it is safe to call
240 * `mbedtls_test_ssl_buffer_free()` on \p buf.
241 */
242void mbedtls_test_ssl_buffer_init(mbedtls_test_ssl_buffer *buf);
243
244/*
245 * Sets up \p buf. After calling this function it is safe to call
246 * `mbedtls_test_ssl_buffer_put()` and `mbedtls_test_ssl_buffer_get()`
247 * on \p buf.
248 */
249int mbedtls_test_ssl_buffer_setup(mbedtls_test_ssl_buffer *buf,
250 size_t capacity);
251
252void mbedtls_test_ssl_buffer_free(mbedtls_test_ssl_buffer *buf);
253
254/*
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800255 * Puts \p input_len bytes from the \p input buffer into the ring buffer \p buf.
256 *
257 * \p buf must have been initialized and set up by calling
258 * `mbedtls_test_ssl_buffer_init()` and `mbedtls_test_ssl_buffer_setup()`.
259 *
260 * \retval \p input_len, if the data fits.
261 * \retval 0 <= value < \p input_len, if the data does not fit.
262 * \retval -1, if \p buf is NULL, it hasn't been set up or \p input_len is not
263 * zero and \p input is NULL.
264 */
265int mbedtls_test_ssl_buffer_put(mbedtls_test_ssl_buffer *buf,
266 const unsigned char *input, size_t input_len);
267
268/*
269 * Gets \p output_len bytes from the ring buffer \p buf into the
270 * \p output buffer. The output buffer can be NULL, in this case a part of the
271 * ring buffer will be dropped, if the requested length is available.
272 *
273 * \p buf must have been initialized and set up by calling
274 * `mbedtls_test_ssl_buffer_init()` and `mbedtls_test_ssl_buffer_setup()`.
275 *
276 * \retval \p output_len, if the data is available.
277 * \retval 0 <= value < \p output_len, if the data is not available.
278 * \retval -1, if \buf is NULL or it hasn't been set up.
279 */
280int mbedtls_test_ssl_buffer_get(mbedtls_test_ssl_buffer *buf,
281 unsigned char *output, size_t output_len);
282
283/*
284 * Errors used in the message transport mock tests
285 */
286 #define MBEDTLS_TEST_ERROR_ARG_NULL -11
287 #define MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED -44
288
289/*
290 * Setup and free functions for the message metadata queue.
291 *
292 * \p capacity describes the number of message metadata chunks that can be held
293 * within the queue.
294 *
295 * \retval 0, if a metadata queue of a given length can be allocated.
296 * \retval MBEDTLS_ERR_SSL_ALLOC_FAILED, if allocation failed.
297 */
298int mbedtls_test_ssl_message_queue_setup(
299 mbedtls_test_ssl_message_queue *queue, size_t capacity);
300
301void mbedtls_test_ssl_message_queue_free(
302 mbedtls_test_ssl_message_queue *queue);
303
304/*
305 * Push message length information onto the message metadata queue.
306 * This will become the last element to leave it (fifo).
307 *
308 * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null.
309 * \retval MBEDTLS_ERR_SSL_WANT_WRITE, if the queue is full.
310 * \retval \p len, if the push was successful.
311 */
312int mbedtls_test_ssl_message_queue_push_info(
313 mbedtls_test_ssl_message_queue *queue, size_t len);
314
315/*
316 * Pop information about the next message length from the queue. This will be
317 * the oldest inserted message length(fifo). \p msg_len can be null, in which
318 * case the data will be popped from the queue but not copied anywhere.
319 *
320 * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null.
321 * \retval MBEDTLS_ERR_SSL_WANT_READ, if the queue is empty.
322 * \retval message length, if the pop was successful, up to the given
323 \p buf_len.
324 */
325int mbedtls_test_ssl_message_queue_pop_info(
326 mbedtls_test_ssl_message_queue *queue, size_t buf_len);
327
328/*
329 * Setup and teardown functions for mock sockets.
330 */
Yanray Wang5f86a422023-03-15 16:02:29 +0800331void mbedtls_test_mock_socket_init(mbedtls_test_mock_socket *socket);
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800332
333/*
334 * Closes the socket \p socket.
335 *
336 * \p socket must have been previously initialized by calling
Yanray Wang5f86a422023-03-15 16:02:29 +0800337 * mbedtls_test_mock_socket_init().
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800338 *
339 * This function frees all allocated resources and both sockets are aware of the
340 * new connection state.
341 *
342 * That is, this function does not simulate half-open TCP connections and the
343 * phenomenon that when closing a UDP connection the peer is not aware of the
344 * connection having been closed.
345 */
346void mbedtls_test_mock_socket_close(mbedtls_test_mock_socket *socket);
347
348/*
349 * Establishes a connection between \p peer1 and \p peer2.
350 *
351 * \p peer1 and \p peer2 must have been previously initialized by calling
Yanray Wang5f86a422023-03-15 16:02:29 +0800352 * mbedtls_test_mock_socket_init().
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800353 *
354 * The capacities of the internal buffers are set to \p bufsize. Setting this to
355 * the correct value allows for simulation of MTU, sanity testing the mock
356 * implementation and mocking TCP connections with lower memory cost.
357 */
358int mbedtls_test_mock_socket_connect(mbedtls_test_mock_socket *peer1,
359 mbedtls_test_mock_socket *peer2,
360 size_t bufsize);
361
362
363/*
364 * Callbacks for simulating blocking I/O over connection-oriented transport.
365 */
Yanray Wangaf727a22023-03-13 19:22:36 +0800366int mbedtls_test_mock_tcp_send_b(void *ctx,
367 const unsigned char *buf, size_t len);
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800368
369int mbedtls_test_mock_tcp_recv_b(void *ctx, unsigned char *buf, size_t len);
370
371/*
372 * Callbacks for simulating non-blocking I/O over connection-oriented transport.
373 */
Yanray Wangaf727a22023-03-13 19:22:36 +0800374int mbedtls_test_mock_tcp_send_nb(void *ctx,
375 const unsigned char *buf, size_t len);
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800376
377int mbedtls_test_mock_tcp_recv_nb(void *ctx, unsigned char *buf, size_t len);
378
379void mbedtls_test_message_socket_init(
380 mbedtls_test_message_socket_context *ctx);
381
382/*
383 * Setup a given message socket context including initialization of
384 * input/output queues to a chosen capacity of messages. Also set the
385 * corresponding mock socket.
386 *
387 * \retval 0, if everything succeeds.
388 * \retval MBEDTLS_ERR_SSL_ALLOC_FAILED, if allocation of a message
389 * queue failed.
390 */
391int mbedtls_test_message_socket_setup(
392 mbedtls_test_ssl_message_queue *queue_input,
393 mbedtls_test_ssl_message_queue *queue_output,
Yanray Wangd19894f2023-03-16 11:47:39 +0800394 size_t queue_capacity,
395 mbedtls_test_mock_socket *socket,
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800396 mbedtls_test_message_socket_context *ctx);
397
398/*
399 * Close a given message socket context, along with the socket itself. Free the
400 * memory allocated by the input queue.
401 */
402void mbedtls_test_message_socket_close(
403 mbedtls_test_message_socket_context *ctx);
404
405/*
406 * Send one message through a given message socket context.
407 *
408 * \retval \p len, if everything succeeds.
409 * \retval MBEDTLS_TEST_ERROR_CONTEXT_ERROR, if any of the needed context
410 * elements or the context itself is null.
411 * \retval MBEDTLS_TEST_ERROR_SEND_FAILED if
412 * mbedtls_test_mock_tcp_send_b failed.
413 * \retval MBEDTLS_ERR_SSL_WANT_WRITE, if the output queue is full.
414 *
415 * This function will also return any error from
416 * mbedtls_test_ssl_message_queue_push_info.
417 */
Yanray Wangaf727a22023-03-13 19:22:36 +0800418int mbedtls_test_mock_tcp_send_msg(void *ctx,
419 const unsigned char *buf, size_t len);
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800420
421/*
422 * Receive one message from a given message socket context and return message
423 * length or an error.
424 *
425 * \retval message length, if everything succeeds.
426 * \retval MBEDTLS_TEST_ERROR_CONTEXT_ERROR, if any of the needed context
427 * elements or the context itself is null.
428 * \retval MBEDTLS_TEST_ERROR_RECV_FAILED if
429 * mbedtls_test_mock_tcp_recv_b failed.
430 *
431 * This function will also return any error other than
Yanray Wang5e22a922023-03-16 14:57:54 +0800432 * MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED from test_ssl_message_queue_peek_info.
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800433 */
Yanray Wangaf727a22023-03-13 19:22:36 +0800434int mbedtls_test_mock_tcp_recv_msg(void *ctx,
435 unsigned char *buf, size_t buf_len);
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800436
437#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
438
439/*
Gilles Peskineb6bb3fb2025-05-26 21:57:52 +0200440 * Load default CA certificates and endpoint keys into \p ep.
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800441 *
442 * \retval 0 on success, otherwise error code.
443 */
444int mbedtls_test_ssl_endpoint_certificate_init(mbedtls_test_ssl_endpoint *ep,
445 int pk_alg,
446 int opaque_alg, int opaque_alg2,
447 int opaque_usage);
448
449/*
450 * Initializes \p ep structure. It is important to call
451 * `mbedtls_test_ssl_endpoint_free()` after calling this function
452 * even if it fails.
453 *
Gilles Peskineb092e782025-05-27 20:15:03 +0200454 * \note For DTLS, after calling this function on both endpoints,
455 * call mbedtls_test_ssl_dtls_join_endpoints().
456 *
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800457 * \p endpoint_type must be set as MBEDTLS_SSL_IS_SERVER or
458 * MBEDTLS_SSL_IS_CLIENT.
459 * \p pk_alg the algorithm to use, currently only MBEDTLS_PK_RSA and
460 * MBEDTLS_PK_ECDSA are supported.
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800461 *
462 * \retval 0 on success, otherwise error code.
463 */
464int mbedtls_test_ssl_endpoint_init(
465 mbedtls_test_ssl_endpoint *ep, int endpoint_type,
Gilles Peskineca8a9ac2025-05-27 20:52:24 +0200466 const mbedtls_test_handshake_test_options *options);
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800467
468/*
469 * Deinitializes endpoint represented by \p ep.
470 */
Gilles Peskineca8a9ac2025-05-27 20:52:24 +0200471void mbedtls_test_ssl_endpoint_free(mbedtls_test_ssl_endpoint *ep);
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800472
Gilles Peskineb092e782025-05-27 20:15:03 +0200473/* Join a DTLS client with a DTLS server.
474 *
475 * You must call this function after setting up the endpoint objects
476 * and before starting a DTLS handshake.
477 *
478 * \param client The client. It must have been set up with
479 * mbedtls_test_ssl_endpoint_init().
480 * \param server The server. It must have been set up with
481 * mbedtls_test_ssl_endpoint_init().
482 *
483 * \retval 0 on success, otherwise error code.
484 */
485int mbedtls_test_ssl_dtls_join_endpoints(mbedtls_test_ssl_endpoint *client,
486 mbedtls_test_ssl_endpoint *server);
487
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800488/*
489 * This function moves ssl handshake from \p ssl to prescribed \p state.
490 * /p second_ssl is used as second endpoint and their sockets have to be
491 * connected before calling this function.
492 *
Gilles Peskinea4bf0022025-03-07 20:37:49 +0100493 * For example, to perform a full handshake:
494 * ```
495 * mbedtls_test_move_handshake_to_state(
496 * &server.ssl, &client.ssl,
497 * MBEDTLS_SSL_HANDSHAKE_OVER);
498 * mbedtls_test_move_handshake_to_state(
Gilles Peskine161cadd2025-03-10 14:24:22 +0100499 * &client.ssl, &server.ssl,
Gilles Peskinea4bf0022025-03-07 20:37:49 +0100500 * MBEDTLS_SSL_HANDSHAKE_OVER);
501 * ```
502 * Note that you need both calls to reach the handshake-over state on
503 * both sides.
504 *
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800505 * \retval 0 on success, otherwise error code.
506 */
507int mbedtls_test_move_handshake_to_state(mbedtls_ssl_context *ssl,
508 mbedtls_ssl_context *second_ssl,
509 int state);
510
511#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
512
Yanray Wang1fca4de2023-02-06 12:10:48 +0800513/*
514 * Helper function setting up inverse record transformations
515 * using given cipher, hash, EtM mode, authentication tag length,
516 * and version.
517 */
518#define CHK(x) \
519 do \
520 { \
521 if (!(x)) \
522 { \
523 ret = -1; \
524 goto cleanup; \
525 } \
526 } while (0)
527
Yanray Wang25b766f2023-03-15 16:39:05 +0800528#if MBEDTLS_SSL_CID_OUT_LEN_MAX > MBEDTLS_SSL_CID_IN_LEN_MAX
529#define SSL_CID_LEN_MIN MBEDTLS_SSL_CID_IN_LEN_MAX
530#else
531#define SSL_CID_LEN_MIN MBEDTLS_SSL_CID_OUT_LEN_MAX
532#endif
533
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800534#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Elena Uziunaite74342c72024-07-05 11:31:29 +0100535 defined(PSA_WANT_ALG_CBC_NO_PADDING) && defined(PSA_WANT_KEY_TYPE_AES)
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800536int mbedtls_test_psa_cipher_encrypt_helper(mbedtls_ssl_transform *transform,
537 const unsigned char *iv,
538 size_t iv_len,
539 const unsigned char *input,
540 size_t ilen,
541 unsigned char *output,
542 size_t *olen);
Elena Uziunaite74342c72024-07-05 11:31:29 +0100543#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && PSA_WANT_ALG_CBC_NO_PADDING &&
Elena Uziunaite6121a342024-07-05 11:16:53 +0100544 PSA_WANT_KEY_TYPE_AES */
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800545
546int mbedtls_test_ssl_build_transforms(mbedtls_ssl_transform *t_in,
547 mbedtls_ssl_transform *t_out,
548 int cipher_type, int hash_id,
549 int etm, int tag_mode,
550 mbedtls_ssl_protocol_version tls_version,
551 size_t cid0_len,
552 size_t cid1_len);
553
Gilles Peskine9099d3f2023-09-18 13:11:50 +0200554#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
555/**
556 * \param[in,out] record The record to prepare.
557 * It must contain the data to MAC at offset
558 * `record->data_offset`, of length
559 * `record->data_length`.
560 * On success, write the MAC immediately
561 * after the data and increment
562 * `record->data_length` accordingly.
563 * \param[in,out] transform_out The out transform, typically prepared by
564 * mbedtls_test_ssl_build_transforms().
565 * Its HMAC context may be used. Other than that
566 * it is treated as an input parameter.
567 *
568 * \return 0 on success, an `MBEDTLS_ERR_xxx` error code
569 * or -1 on error.
570 */
571int mbedtls_test_ssl_prepare_record_mac(mbedtls_record *record,
572 mbedtls_ssl_transform *transform_out);
573#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
574
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800575/*
576 * Populate a session structure for serialization tests.
577 * Choose dummy values, mostly non-0 to distinguish from the init default.
578 */
579int mbedtls_test_ssl_tls12_populate_session(mbedtls_ssl_session *session,
580 int ticket_len,
Ronald Cron7b1921a2023-11-23 12:31:56 +0100581 int endpoint_type,
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800582 const char *crt_file);
583
584#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
585int mbedtls_test_ssl_tls13_populate_session(mbedtls_ssl_session *session,
586 int ticket_len,
587 int endpoint_type);
588#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
589
590/*
591 * Perform data exchanging between \p ssl_1 and \p ssl_2 and check if the
592 * message was sent in the correct number of fragments.
593 *
594 * /p ssl_1 and /p ssl_2 Endpoints represented by mbedtls_ssl_context. Both
595 * of them must be initialized and connected
596 * beforehand.
597 * /p msg_len_1 and /p msg_len_2 specify the size of the message to send.
598 * /p expected_fragments_1 and /p expected_fragments_2 determine in how many
599 * fragments the message should be sent.
600 * expected_fragments is 0: can be used for DTLS testing while the message
601 * size is larger than MFL. In that case the message
602 * cannot be fragmented and sent to the second
603 * endpoint.
604 * This value can be used for negative tests.
605 * expected_fragments is 1: can be used for TLS/DTLS testing while the
606 * message size is below MFL
607 * expected_fragments > 1: can be used for TLS testing while the message
608 * size is larger than MFL
609 *
610 * \retval 0 on success, otherwise error code.
611 */
Yanray Wangb088bfc2023-03-16 12:15:49 +0800612int mbedtls_test_ssl_exchange_data(
613 mbedtls_ssl_context *ssl_1,
614 int msg_len_1, const int expected_fragments_1,
615 mbedtls_ssl_context *ssl_2,
616 int msg_len_2, const int expected_fragments_2);
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800617
618#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Max Fillingercf007ca2024-10-29 16:57:09 +0100619int mbedtls_test_ssl_do_handshake_with_endpoints(
620 mbedtls_test_ssl_endpoint *server_ep,
621 mbedtls_test_ssl_endpoint *client_ep,
Max Fillinger8f12e312024-10-30 00:29:37 +0100622 mbedtls_test_handshake_test_options *options,
Max Fillingercf007ca2024-10-29 16:57:09 +0100623 mbedtls_ssl_protocol_version proto);
624#endif /* defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) */
625
626#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gilles Peskine7a8fd462025-05-28 15:41:54 +0200627/** Perform an SSL handshake and exchange data over the connection.
628 *
629 * This function also handles cases where the handshake is expected to fail.
630 *
631 * If the handshake succeeds as expected, this function validates that
632 * connection parameters are as expected, exchanges data over the
633 * connection, and exercises some optional protocol features if they
634 * are enabled. See the code to see what features are validated and exercised.
635 *
636 * The handshake is expected to fail in the following cases:
637 * - If `options->expected_handshake_result != 0`.
638 * - If `options->expected_negotiated_version == MBEDTLS_SSL_VERSION_UNKNOWN`.
639 *
640 * \param[in] options Options for the connection.
641 * \param client The client endpoint. It must have been set up with
642 * mbedtls_test_ssl_endpoint_init() with \p options
643 * and #MBEDTLS_SSL_IS_CLIENT.
644 * \param server The server endpoint. It must have been set up with
645 * mbedtls_test_ssl_endpoint_init() with \p options
646 * and #MBEDTLS_SSL_IS_CLIENT.
647 *
648 * \return 1 on success, 0 on failure. On failure, this function
649 * calls mbedtls_test_fail(), indicating the failure
650 * reason and location. The causes of failure are:
651 * - Inconsistent options or bad endpoint state.
652 * - Operational problem during the handshake.
653 * - The handshake was expected to pass, but failed.
654 * - The handshake was expected to fail, but passed or
655 * failed with a different result.
656 * - The handshake passed as expected, but some connection
657 * parameter (e.g. protocol version, cipher suite, ...)
658 * is not as expected.
659 * - The handshake passed as expected, but something
660 * went wrong when attempting to exchange data.
661 * - The handshake passed as expected, but something
662 * went wrong when exercising other features
663 * (e.g. renegotiation, serialization, ...).
664 */
665int mbedtls_test_ssl_perform_connection(
666 const mbedtls_test_handshake_test_options *options,
667 mbedtls_test_ssl_endpoint *client,
668 mbedtls_test_ssl_endpoint *server);
669
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800670void mbedtls_test_ssl_perform_handshake(
Gilles Peskine9b993682025-05-27 18:44:12 +0200671 const mbedtls_test_handshake_test_options *options);
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800672#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
673
674#if defined(MBEDTLS_TEST_HOOKS)
675/*
676 * Tweak vector lengths in a TLS 1.3 Certificate message
677 *
678 * \param[in] buf Buffer containing the Certificate message to tweak
679 * \param[in]]out] end End of the buffer to parse
680 * \param tweak Tweak identifier (from 1 to the number of tweaks).
681 * \param[out] expected_result Error code expected from the parsing function
682 * \param[out] args Arguments of the MBEDTLS_SSL_CHK_BUF_READ_PTR call that
683 * is expected to fail. All zeroes if no
684 * MBEDTLS_SSL_CHK_BUF_READ_PTR failure is expected.
685 */
Yanray Wangf56181a2023-03-16 12:21:33 +0800686int mbedtls_test_tweak_tls13_certificate_msg_vector_len(
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800687 unsigned char *buf, unsigned char **end, int tweak,
688 int *expected_result, mbedtls_ssl_chk_buf_ptr_args *args);
689#endif /* MBEDTLS_TEST_HOOKS */
Yanray Wang1db628f2023-02-03 11:01:29 +0800690
Ronald Cron77abfe62024-01-15 11:17:31 +0100691#if defined(MBEDTLS_SSL_SESSION_TICKETS)
692int mbedtls_test_ticket_write(
693 void *p_ticket, const mbedtls_ssl_session *session,
694 unsigned char *start, const unsigned char *end,
695 size_t *tlen, uint32_t *ticket_lifetime);
696
697int mbedtls_test_ticket_parse(void *p_ticket, mbedtls_ssl_session *session,
698 unsigned char *buf, size_t len);
699#endif /* MBEDTLS_SSL_SESSION_TICKETS */
700
Ronald Cron1f6e4e42024-01-26 16:31:33 +0100701#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SRV_C) && \
702 defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS) && \
703 defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
704int mbedtls_test_get_tls13_ticket(
705 mbedtls_test_handshake_test_options *client_options,
706 mbedtls_test_handshake_test_options *server_options,
707 mbedtls_ssl_session *session);
Ronald Cronb9a9b1f2024-02-14 11:28:05 +0100708#endif
Ronald Cron1f6e4e42024-01-26 16:31:33 +0100709
Yanray Wang1db628f2023-02-03 11:01:29 +0800710#define ECJPAKE_TEST_PWD "bla"
711
712#if defined(MBEDTLS_USE_PSA_CRYPTO)
713#define ECJPAKE_TEST_SET_PASSWORD(exp_ret_val) \
714 ret = (use_opaque_arg) ? \
715 mbedtls_ssl_set_hs_ecjpake_password_opaque(&ssl, pwd_slot) : \
716 mbedtls_ssl_set_hs_ecjpake_password(&ssl, pwd_string, pwd_len); \
717 TEST_EQUAL(ret, exp_ret_val)
718#else
719#define ECJPAKE_TEST_SET_PASSWORD(exp_ret_val) \
720 ret = mbedtls_ssl_set_hs_ecjpake_password(&ssl, \
721 pwd_string, pwd_len); \
722 TEST_EQUAL(ret, exp_ret_val)
723#endif /* MBEDTLS_USE_PSA_CRYPTO */
724
Yanray Wang09a6f7e2023-02-03 11:04:38 +0800725#define TEST_AVAILABLE_ECC(tls_id_, group_id_, psa_family_, psa_bits_) \
726 TEST_EQUAL(mbedtls_ssl_get_ecp_group_id_from_tls_id(tls_id_), \
727 group_id_); \
728 TEST_EQUAL(mbedtls_ssl_get_tls_id_from_ecp_group_id(group_id_), \
729 tls_id_); \
730 TEST_EQUAL(mbedtls_ssl_get_psa_curve_info_from_tls_id(tls_id_, \
Przemek Stekielda4fba62023-06-02 14:52:28 +0200731 &psa_type, &psa_bits), PSA_SUCCESS); \
732 TEST_EQUAL(psa_family_, PSA_KEY_TYPE_ECC_GET_FAMILY(psa_type)); \
Yanray Wang09a6f7e2023-02-03 11:04:38 +0800733 TEST_EQUAL(psa_bits_, psa_bits);
734
735#define TEST_UNAVAILABLE_ECC(tls_id_, group_id_, psa_family_, psa_bits_) \
736 TEST_EQUAL(mbedtls_ssl_get_ecp_group_id_from_tls_id(tls_id_), \
737 MBEDTLS_ECP_DP_NONE); \
738 TEST_EQUAL(mbedtls_ssl_get_tls_id_from_ecp_group_id(group_id_), \
739 0); \
740 TEST_EQUAL(mbedtls_ssl_get_psa_curve_info_from_tls_id(tls_id_, \
Przemek Stekielda4fba62023-06-02 14:52:28 +0200741 &psa_type, &psa_bits), \
Yanray Wang09a6f7e2023-02-03 11:04:38 +0800742 PSA_ERROR_NOT_SUPPORTED);
743
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800744#endif /* MBEDTLS_SSL_TLS_C */
745
Yanray Wang47907a42022-10-24 14:42:01 +0800746#endif /* SSL_HELPERS_H */