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