blob: a7bc065bf3e69ec54dbf5164cb3b124d2133bb19 [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 Peskine0677e022025-05-27 18:05:20 +0200197#if defined(MBEDTLS_TIMING_C)
198 mbedtls_timing_delay_context timer;
199#endif
Gilles Peskineb6bb3fb2025-05-26 21:57:52 +0200200
201 /* Objects owned by the endpoint */
202 mbedtls_x509_crt *ca_chain;
203 mbedtls_x509_crt *cert;
204 mbedtls_pk_context *pkey;
Yanray Wang55a66192022-10-26 09:57:53 +0800205} mbedtls_test_ssl_endpoint;
206
207#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Yanray Wang47907a42022-10-24 14:42:01 +0800208
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800209/*
Ronald Cron10b040f2024-02-05 09:38:09 +0100210 * Random number generator aimed for TLS unitary tests. Its main purpose is to
211 * simplify the set-up of a random number generator for TLS
212 * unitary tests: no need to set up a good entropy source for example.
213 */
214int mbedtls_test_random(void *p_rng, unsigned char *output, size_t output_len);
Yanray Wangf88e5292023-12-01 16:39:34 +0800215
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800216/*
217 * This function can be passed to mbedtls to receive output logs from it. In
218 * this case, it will count the instances of a mbedtls_test_ssl_log_pattern
219 * in the received logged messages.
220 */
221void mbedtls_test_ssl_log_analyzer(void *ctx, int level,
222 const char *file, int line,
223 const char *str);
224
225void mbedtls_test_init_handshake_options(
226 mbedtls_test_handshake_test_options *opts);
227
228void mbedtls_test_free_handshake_options(
229 mbedtls_test_handshake_test_options *opts);
230
231/*
232 * Initialises \p buf. After calling this function it is safe to call
233 * `mbedtls_test_ssl_buffer_free()` on \p buf.
234 */
235void mbedtls_test_ssl_buffer_init(mbedtls_test_ssl_buffer *buf);
236
237/*
238 * Sets up \p buf. After calling this function it is safe to call
239 * `mbedtls_test_ssl_buffer_put()` and `mbedtls_test_ssl_buffer_get()`
240 * on \p buf.
241 */
242int mbedtls_test_ssl_buffer_setup(mbedtls_test_ssl_buffer *buf,
243 size_t capacity);
244
245void mbedtls_test_ssl_buffer_free(mbedtls_test_ssl_buffer *buf);
246
247/*
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800248 * Puts \p input_len bytes from the \p input buffer into the ring buffer \p buf.
249 *
250 * \p buf must have been initialized and set up by calling
251 * `mbedtls_test_ssl_buffer_init()` and `mbedtls_test_ssl_buffer_setup()`.
252 *
253 * \retval \p input_len, if the data fits.
254 * \retval 0 <= value < \p input_len, if the data does not fit.
255 * \retval -1, if \p buf is NULL, it hasn't been set up or \p input_len is not
256 * zero and \p input is NULL.
257 */
258int mbedtls_test_ssl_buffer_put(mbedtls_test_ssl_buffer *buf,
259 const unsigned char *input, size_t input_len);
260
261/*
262 * Gets \p output_len bytes from the ring buffer \p buf into the
263 * \p output buffer. The output buffer can be NULL, in this case a part of the
264 * ring buffer will be dropped, if the requested length is available.
265 *
266 * \p buf must have been initialized and set up by calling
267 * `mbedtls_test_ssl_buffer_init()` and `mbedtls_test_ssl_buffer_setup()`.
268 *
269 * \retval \p output_len, if the data is available.
270 * \retval 0 <= value < \p output_len, if the data is not available.
271 * \retval -1, if \buf is NULL or it hasn't been set up.
272 */
273int mbedtls_test_ssl_buffer_get(mbedtls_test_ssl_buffer *buf,
274 unsigned char *output, size_t output_len);
275
276/*
277 * Errors used in the message transport mock tests
278 */
279 #define MBEDTLS_TEST_ERROR_ARG_NULL -11
280 #define MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED -44
281
282/*
283 * Setup and free functions for the message metadata queue.
284 *
285 * \p capacity describes the number of message metadata chunks that can be held
286 * within the queue.
287 *
288 * \retval 0, if a metadata queue of a given length can be allocated.
289 * \retval MBEDTLS_ERR_SSL_ALLOC_FAILED, if allocation failed.
290 */
291int mbedtls_test_ssl_message_queue_setup(
292 mbedtls_test_ssl_message_queue *queue, size_t capacity);
293
294void mbedtls_test_ssl_message_queue_free(
295 mbedtls_test_ssl_message_queue *queue);
296
297/*
298 * Push message length information onto the message metadata queue.
299 * This will become the last element to leave it (fifo).
300 *
301 * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null.
302 * \retval MBEDTLS_ERR_SSL_WANT_WRITE, if the queue is full.
303 * \retval \p len, if the push was successful.
304 */
305int mbedtls_test_ssl_message_queue_push_info(
306 mbedtls_test_ssl_message_queue *queue, size_t len);
307
308/*
309 * Pop information about the next message length from the queue. This will be
310 * the oldest inserted message length(fifo). \p msg_len can be null, in which
311 * case the data will be popped from the queue but not copied anywhere.
312 *
313 * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null.
314 * \retval MBEDTLS_ERR_SSL_WANT_READ, if the queue is empty.
315 * \retval message length, if the pop was successful, up to the given
316 \p buf_len.
317 */
318int mbedtls_test_ssl_message_queue_pop_info(
319 mbedtls_test_ssl_message_queue *queue, size_t buf_len);
320
321/*
322 * Setup and teardown functions for mock sockets.
323 */
Yanray Wang5f86a422023-03-15 16:02:29 +0800324void mbedtls_test_mock_socket_init(mbedtls_test_mock_socket *socket);
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800325
326/*
327 * Closes the socket \p socket.
328 *
329 * \p socket must have been previously initialized by calling
Yanray Wang5f86a422023-03-15 16:02:29 +0800330 * mbedtls_test_mock_socket_init().
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800331 *
332 * This function frees all allocated resources and both sockets are aware of the
333 * new connection state.
334 *
335 * That is, this function does not simulate half-open TCP connections and the
336 * phenomenon that when closing a UDP connection the peer is not aware of the
337 * connection having been closed.
338 */
339void mbedtls_test_mock_socket_close(mbedtls_test_mock_socket *socket);
340
341/*
342 * Establishes a connection between \p peer1 and \p peer2.
343 *
344 * \p peer1 and \p peer2 must have been previously initialized by calling
Yanray Wang5f86a422023-03-15 16:02:29 +0800345 * mbedtls_test_mock_socket_init().
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800346 *
347 * The capacities of the internal buffers are set to \p bufsize. Setting this to
348 * the correct value allows for simulation of MTU, sanity testing the mock
349 * implementation and mocking TCP connections with lower memory cost.
350 */
351int mbedtls_test_mock_socket_connect(mbedtls_test_mock_socket *peer1,
352 mbedtls_test_mock_socket *peer2,
353 size_t bufsize);
354
355
356/*
357 * Callbacks for simulating blocking I/O over connection-oriented transport.
358 */
Yanray Wangaf727a22023-03-13 19:22:36 +0800359int mbedtls_test_mock_tcp_send_b(void *ctx,
360 const unsigned char *buf, size_t len);
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800361
362int mbedtls_test_mock_tcp_recv_b(void *ctx, unsigned char *buf, size_t len);
363
364/*
365 * Callbacks for simulating non-blocking I/O over connection-oriented transport.
366 */
Yanray Wangaf727a22023-03-13 19:22:36 +0800367int mbedtls_test_mock_tcp_send_nb(void *ctx,
368 const unsigned char *buf, size_t len);
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800369
370int mbedtls_test_mock_tcp_recv_nb(void *ctx, unsigned char *buf, size_t len);
371
372void mbedtls_test_message_socket_init(
373 mbedtls_test_message_socket_context *ctx);
374
375/*
376 * Setup a given message socket context including initialization of
377 * input/output queues to a chosen capacity of messages. Also set the
378 * corresponding mock socket.
379 *
380 * \retval 0, if everything succeeds.
381 * \retval MBEDTLS_ERR_SSL_ALLOC_FAILED, if allocation of a message
382 * queue failed.
383 */
384int mbedtls_test_message_socket_setup(
385 mbedtls_test_ssl_message_queue *queue_input,
386 mbedtls_test_ssl_message_queue *queue_output,
Yanray Wangd19894f2023-03-16 11:47:39 +0800387 size_t queue_capacity,
388 mbedtls_test_mock_socket *socket,
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800389 mbedtls_test_message_socket_context *ctx);
390
391/*
392 * Close a given message socket context, along with the socket itself. Free the
393 * memory allocated by the input queue.
394 */
395void mbedtls_test_message_socket_close(
396 mbedtls_test_message_socket_context *ctx);
397
398/*
399 * Send one message through a given message socket context.
400 *
401 * \retval \p len, if everything succeeds.
402 * \retval MBEDTLS_TEST_ERROR_CONTEXT_ERROR, if any of the needed context
403 * elements or the context itself is null.
404 * \retval MBEDTLS_TEST_ERROR_SEND_FAILED if
405 * mbedtls_test_mock_tcp_send_b failed.
406 * \retval MBEDTLS_ERR_SSL_WANT_WRITE, if the output queue is full.
407 *
408 * This function will also return any error from
409 * mbedtls_test_ssl_message_queue_push_info.
410 */
Yanray Wangaf727a22023-03-13 19:22:36 +0800411int mbedtls_test_mock_tcp_send_msg(void *ctx,
412 const unsigned char *buf, size_t len);
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800413
414/*
415 * Receive one message from a given message socket context and return message
416 * length or an error.
417 *
418 * \retval message length, if everything succeeds.
419 * \retval MBEDTLS_TEST_ERROR_CONTEXT_ERROR, if any of the needed context
420 * elements or the context itself is null.
421 * \retval MBEDTLS_TEST_ERROR_RECV_FAILED if
422 * mbedtls_test_mock_tcp_recv_b failed.
423 *
424 * This function will also return any error other than
Yanray Wang5e22a922023-03-16 14:57:54 +0800425 * MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED from test_ssl_message_queue_peek_info.
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800426 */
Yanray Wangaf727a22023-03-13 19:22:36 +0800427int mbedtls_test_mock_tcp_recv_msg(void *ctx,
428 unsigned char *buf, size_t buf_len);
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800429
430#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
431
432/*
Gilles Peskineb6bb3fb2025-05-26 21:57:52 +0200433 * Load default CA certificates and endpoint keys into \p ep.
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800434 *
435 * \retval 0 on success, otherwise error code.
436 */
437int mbedtls_test_ssl_endpoint_certificate_init(mbedtls_test_ssl_endpoint *ep,
438 int pk_alg,
439 int opaque_alg, int opaque_alg2,
440 int opaque_usage);
441
442/*
443 * Initializes \p ep structure. It is important to call
444 * `mbedtls_test_ssl_endpoint_free()` after calling this function
445 * even if it fails.
446 *
447 * \p endpoint_type must be set as MBEDTLS_SSL_IS_SERVER or
448 * MBEDTLS_SSL_IS_CLIENT.
449 * \p pk_alg the algorithm to use, currently only MBEDTLS_PK_RSA and
450 * MBEDTLS_PK_ECDSA are supported.
451 * \p dtls_context - in case of DTLS - this is the context handling metadata.
452 * \p input_queue - used only in case of DTLS.
453 * \p output_queue - used only in case of DTLS.
454 *
455 * \retval 0 on success, otherwise error code.
456 */
457int mbedtls_test_ssl_endpoint_init(
458 mbedtls_test_ssl_endpoint *ep, int endpoint_type,
459 mbedtls_test_handshake_test_options *options,
460 mbedtls_test_message_socket_context *dtls_context,
461 mbedtls_test_ssl_message_queue *input_queue,
Ronald Cronfb536472024-01-26 14:55:25 +0100462 mbedtls_test_ssl_message_queue *output_queue);
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800463
464/*
465 * Deinitializes endpoint represented by \p ep.
466 */
467void mbedtls_test_ssl_endpoint_free(
468 mbedtls_test_ssl_endpoint *ep,
469 mbedtls_test_message_socket_context *context);
470
471/*
472 * This function moves ssl handshake from \p ssl to prescribed \p state.
473 * /p second_ssl is used as second endpoint and their sockets have to be
474 * connected before calling this function.
475 *
Gilles Peskinea4bf0022025-03-07 20:37:49 +0100476 * For example, to perform a full handshake:
477 * ```
478 * mbedtls_test_move_handshake_to_state(
479 * &server.ssl, &client.ssl,
480 * MBEDTLS_SSL_HANDSHAKE_OVER);
481 * mbedtls_test_move_handshake_to_state(
Gilles Peskine161cadd2025-03-10 14:24:22 +0100482 * &client.ssl, &server.ssl,
Gilles Peskinea4bf0022025-03-07 20:37:49 +0100483 * MBEDTLS_SSL_HANDSHAKE_OVER);
484 * ```
485 * Note that you need both calls to reach the handshake-over state on
486 * both sides.
487 *
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800488 * \retval 0 on success, otherwise error code.
489 */
490int mbedtls_test_move_handshake_to_state(mbedtls_ssl_context *ssl,
491 mbedtls_ssl_context *second_ssl,
492 int state);
493
494#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
495
Yanray Wang1fca4de2023-02-06 12:10:48 +0800496/*
497 * Helper function setting up inverse record transformations
498 * using given cipher, hash, EtM mode, authentication tag length,
499 * and version.
500 */
501#define CHK(x) \
502 do \
503 { \
504 if (!(x)) \
505 { \
506 ret = -1; \
507 goto cleanup; \
508 } \
509 } while (0)
510
Yanray Wang25b766f2023-03-15 16:39:05 +0800511#if MBEDTLS_SSL_CID_OUT_LEN_MAX > MBEDTLS_SSL_CID_IN_LEN_MAX
512#define SSL_CID_LEN_MIN MBEDTLS_SSL_CID_IN_LEN_MAX
513#else
514#define SSL_CID_LEN_MIN MBEDTLS_SSL_CID_OUT_LEN_MAX
515#endif
516
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800517#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Elena Uziunaite74342c72024-07-05 11:31:29 +0100518 defined(PSA_WANT_ALG_CBC_NO_PADDING) && defined(PSA_WANT_KEY_TYPE_AES)
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800519int mbedtls_test_psa_cipher_encrypt_helper(mbedtls_ssl_transform *transform,
520 const unsigned char *iv,
521 size_t iv_len,
522 const unsigned char *input,
523 size_t ilen,
524 unsigned char *output,
525 size_t *olen);
Elena Uziunaite74342c72024-07-05 11:31:29 +0100526#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && PSA_WANT_ALG_CBC_NO_PADDING &&
Elena Uziunaite6121a342024-07-05 11:16:53 +0100527 PSA_WANT_KEY_TYPE_AES */
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800528
529int mbedtls_test_ssl_build_transforms(mbedtls_ssl_transform *t_in,
530 mbedtls_ssl_transform *t_out,
531 int cipher_type, int hash_id,
532 int etm, int tag_mode,
533 mbedtls_ssl_protocol_version tls_version,
534 size_t cid0_len,
535 size_t cid1_len);
536
Gilles Peskine9099d3f2023-09-18 13:11:50 +0200537#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
538/**
539 * \param[in,out] record The record to prepare.
540 * It must contain the data to MAC at offset
541 * `record->data_offset`, of length
542 * `record->data_length`.
543 * On success, write the MAC immediately
544 * after the data and increment
545 * `record->data_length` accordingly.
546 * \param[in,out] transform_out The out transform, typically prepared by
547 * mbedtls_test_ssl_build_transforms().
548 * Its HMAC context may be used. Other than that
549 * it is treated as an input parameter.
550 *
551 * \return 0 on success, an `MBEDTLS_ERR_xxx` error code
552 * or -1 on error.
553 */
554int mbedtls_test_ssl_prepare_record_mac(mbedtls_record *record,
555 mbedtls_ssl_transform *transform_out);
556#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
557
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800558/*
559 * Populate a session structure for serialization tests.
560 * Choose dummy values, mostly non-0 to distinguish from the init default.
561 */
562int mbedtls_test_ssl_tls12_populate_session(mbedtls_ssl_session *session,
563 int ticket_len,
Ronald Cron7b1921a2023-11-23 12:31:56 +0100564 int endpoint_type,
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800565 const char *crt_file);
566
567#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
568int mbedtls_test_ssl_tls13_populate_session(mbedtls_ssl_session *session,
569 int ticket_len,
570 int endpoint_type);
571#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
572
573/*
574 * Perform data exchanging between \p ssl_1 and \p ssl_2 and check if the
575 * message was sent in the correct number of fragments.
576 *
577 * /p ssl_1 and /p ssl_2 Endpoints represented by mbedtls_ssl_context. Both
578 * of them must be initialized and connected
579 * beforehand.
580 * /p msg_len_1 and /p msg_len_2 specify the size of the message to send.
581 * /p expected_fragments_1 and /p expected_fragments_2 determine in how many
582 * fragments the message should be sent.
583 * expected_fragments is 0: can be used for DTLS testing while the message
584 * size is larger than MFL. In that case the message
585 * cannot be fragmented and sent to the second
586 * endpoint.
587 * This value can be used for negative tests.
588 * expected_fragments is 1: can be used for TLS/DTLS testing while the
589 * message size is below MFL
590 * expected_fragments > 1: can be used for TLS testing while the message
591 * size is larger than MFL
592 *
593 * \retval 0 on success, otherwise error code.
594 */
Yanray Wangb088bfc2023-03-16 12:15:49 +0800595int mbedtls_test_ssl_exchange_data(
596 mbedtls_ssl_context *ssl_1,
597 int msg_len_1, const int expected_fragments_1,
598 mbedtls_ssl_context *ssl_2,
599 int msg_len_2, const int expected_fragments_2);
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800600
601#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Max Fillingercf007ca2024-10-29 16:57:09 +0100602int mbedtls_test_ssl_do_handshake_with_endpoints(
603 mbedtls_test_ssl_endpoint *server_ep,
604 mbedtls_test_ssl_endpoint *client_ep,
Max Fillinger8f12e312024-10-30 00:29:37 +0100605 mbedtls_test_handshake_test_options *options,
Max Fillingercf007ca2024-10-29 16:57:09 +0100606 mbedtls_ssl_protocol_version proto);
607#endif /* defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED) */
608
609#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800610void mbedtls_test_ssl_perform_handshake(
611 mbedtls_test_handshake_test_options *options);
612#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
613
614#if defined(MBEDTLS_TEST_HOOKS)
615/*
616 * Tweak vector lengths in a TLS 1.3 Certificate message
617 *
618 * \param[in] buf Buffer containing the Certificate message to tweak
619 * \param[in]]out] end End of the buffer to parse
620 * \param tweak Tweak identifier (from 1 to the number of tweaks).
621 * \param[out] expected_result Error code expected from the parsing function
622 * \param[out] args Arguments of the MBEDTLS_SSL_CHK_BUF_READ_PTR call that
623 * is expected to fail. All zeroes if no
624 * MBEDTLS_SSL_CHK_BUF_READ_PTR failure is expected.
625 */
Yanray Wangf56181a2023-03-16 12:21:33 +0800626int mbedtls_test_tweak_tls13_certificate_msg_vector_len(
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800627 unsigned char *buf, unsigned char **end, int tweak,
628 int *expected_result, mbedtls_ssl_chk_buf_ptr_args *args);
629#endif /* MBEDTLS_TEST_HOOKS */
Yanray Wang1db628f2023-02-03 11:01:29 +0800630
Ronald Cron77abfe62024-01-15 11:17:31 +0100631#if defined(MBEDTLS_SSL_SESSION_TICKETS)
632int mbedtls_test_ticket_write(
633 void *p_ticket, const mbedtls_ssl_session *session,
634 unsigned char *start, const unsigned char *end,
635 size_t *tlen, uint32_t *ticket_lifetime);
636
637int mbedtls_test_ticket_parse(void *p_ticket, mbedtls_ssl_session *session,
638 unsigned char *buf, size_t len);
639#endif /* MBEDTLS_SSL_SESSION_TICKETS */
640
Ronald Cron1f6e4e42024-01-26 16:31:33 +0100641#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SRV_C) && \
642 defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS) && \
643 defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
644int mbedtls_test_get_tls13_ticket(
645 mbedtls_test_handshake_test_options *client_options,
646 mbedtls_test_handshake_test_options *server_options,
647 mbedtls_ssl_session *session);
Ronald Cronb9a9b1f2024-02-14 11:28:05 +0100648#endif
Ronald Cron1f6e4e42024-01-26 16:31:33 +0100649
Yanray Wang1db628f2023-02-03 11:01:29 +0800650#define ECJPAKE_TEST_PWD "bla"
651
652#if defined(MBEDTLS_USE_PSA_CRYPTO)
653#define ECJPAKE_TEST_SET_PASSWORD(exp_ret_val) \
654 ret = (use_opaque_arg) ? \
655 mbedtls_ssl_set_hs_ecjpake_password_opaque(&ssl, pwd_slot) : \
656 mbedtls_ssl_set_hs_ecjpake_password(&ssl, pwd_string, pwd_len); \
657 TEST_EQUAL(ret, exp_ret_val)
658#else
659#define ECJPAKE_TEST_SET_PASSWORD(exp_ret_val) \
660 ret = mbedtls_ssl_set_hs_ecjpake_password(&ssl, \
661 pwd_string, pwd_len); \
662 TEST_EQUAL(ret, exp_ret_val)
663#endif /* MBEDTLS_USE_PSA_CRYPTO */
664
Yanray Wang09a6f7e2023-02-03 11:04:38 +0800665#define TEST_AVAILABLE_ECC(tls_id_, group_id_, psa_family_, psa_bits_) \
666 TEST_EQUAL(mbedtls_ssl_get_ecp_group_id_from_tls_id(tls_id_), \
667 group_id_); \
668 TEST_EQUAL(mbedtls_ssl_get_tls_id_from_ecp_group_id(group_id_), \
669 tls_id_); \
670 TEST_EQUAL(mbedtls_ssl_get_psa_curve_info_from_tls_id(tls_id_, \
Przemek Stekielda4fba62023-06-02 14:52:28 +0200671 &psa_type, &psa_bits), PSA_SUCCESS); \
672 TEST_EQUAL(psa_family_, PSA_KEY_TYPE_ECC_GET_FAMILY(psa_type)); \
Yanray Wang09a6f7e2023-02-03 11:04:38 +0800673 TEST_EQUAL(psa_bits_, psa_bits);
674
675#define TEST_UNAVAILABLE_ECC(tls_id_, group_id_, psa_family_, psa_bits_) \
676 TEST_EQUAL(mbedtls_ssl_get_ecp_group_id_from_tls_id(tls_id_), \
677 MBEDTLS_ECP_DP_NONE); \
678 TEST_EQUAL(mbedtls_ssl_get_tls_id_from_ecp_group_id(group_id_), \
679 0); \
680 TEST_EQUAL(mbedtls_ssl_get_psa_curve_info_from_tls_id(tls_id_, \
Przemek Stekielda4fba62023-06-02 14:52:28 +0200681 &psa_type, &psa_bits), \
Yanray Wang09a6f7e2023-02-03 11:04:38 +0800682 PSA_ERROR_NOT_SUPPORTED);
683
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800684#endif /* MBEDTLS_SSL_TLS_C */
685
Yanray Wang47907a42022-10-24 14:42:01 +0800686#endif /* SSL_HELPERS_H */