blob: 71259d66f55050e45a4f0100668bf9fbb9406fb2 [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;
Ronald Cron5d3036e2024-02-23 07:43:45 +0100117 int max_early_data_size;
Yanray Wang55a66192022-10-26 09:57:53 +0800118#if defined(MBEDTLS_SSL_CACHE_C)
119 mbedtls_ssl_cache_context *cache;
120#endif
121} mbedtls_test_handshake_test_options;
122
Yanray Wang25b766f2023-03-15 16:39:05 +0800123/*
124 * Buffer structure for custom I/O callbacks.
125 */
Yanray Wang55a66192022-10-26 09:57:53 +0800126typedef struct mbedtls_test_ssl_buffer {
127 size_t start;
128 size_t content_length;
129 size_t capacity;
130 unsigned char *buffer;
131} mbedtls_test_ssl_buffer;
132
133/*
134 * Context for a message metadata queue (fifo) that is on top of the ring buffer.
135 */
136typedef struct mbedtls_test_ssl_message_queue {
137 size_t *messages;
138 int pos;
139 int num;
140 int capacity;
141} mbedtls_test_ssl_message_queue;
142
143/*
144 * Context for the I/O callbacks simulating network connection.
145 */
146
147#define MBEDTLS_MOCK_SOCKET_CONNECTED 1
148
149typedef struct mbedtls_test_mock_socket {
150 int status;
151 mbedtls_test_ssl_buffer *input;
152 mbedtls_test_ssl_buffer *output;
153 struct mbedtls_test_mock_socket *peer;
154} mbedtls_test_mock_socket;
155
156/* Errors used in the message socket mocks */
157
158#define MBEDTLS_TEST_ERROR_CONTEXT_ERROR -55
159#define MBEDTLS_TEST_ERROR_SEND_FAILED -66
160#define MBEDTLS_TEST_ERROR_RECV_FAILED -77
161
162/*
163 * Structure used as an addon, or a wrapper, around the mocked sockets.
164 * Contains an input queue, to which the other socket pushes metadata,
165 * and an output queue, to which this one pushes metadata. This context is
166 * considered as an owner of the input queue only, which is initialized and
167 * freed in the respective setup and free calls.
168 */
169typedef struct mbedtls_test_message_socket_context {
170 mbedtls_test_ssl_message_queue *queue_input;
171 mbedtls_test_ssl_message_queue *queue_output;
172 mbedtls_test_mock_socket *socket;
173} mbedtls_test_message_socket_context;
174
175#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
176
177/*
178 * Structure with endpoint's certificates for SSL communication tests.
179 */
180typedef struct mbedtls_test_ssl_endpoint_certificate {
181 mbedtls_x509_crt *ca_cert;
182 mbedtls_x509_crt *cert;
183 mbedtls_pk_context *pkey;
184} mbedtls_test_ssl_endpoint_certificate;
185
186/*
187 * Endpoint structure for SSL communication tests.
188 */
189typedef struct mbedtls_test_ssl_endpoint {
190 const char *name;
191 mbedtls_ssl_context ssl;
192 mbedtls_ssl_config conf;
193 mbedtls_test_mock_socket socket;
194 mbedtls_test_ssl_endpoint_certificate cert;
195} mbedtls_test_ssl_endpoint;
196
197#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Yanray Wang47907a42022-10-24 14:42:01 +0800198
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800199/*
200 * This function can be passed to mbedtls to receive output logs from it. In
201 * this case, it will count the instances of a mbedtls_test_ssl_log_pattern
202 * in the received logged messages.
203 */
204void mbedtls_test_ssl_log_analyzer(void *ctx, int level,
205 const char *file, int line,
206 const char *str);
207
208void mbedtls_test_init_handshake_options(
209 mbedtls_test_handshake_test_options *opts);
210
211void mbedtls_test_free_handshake_options(
212 mbedtls_test_handshake_test_options *opts);
213
214/*
215 * Initialises \p buf. After calling this function it is safe to call
216 * `mbedtls_test_ssl_buffer_free()` on \p buf.
217 */
218void mbedtls_test_ssl_buffer_init(mbedtls_test_ssl_buffer *buf);
219
220/*
221 * Sets up \p buf. After calling this function it is safe to call
222 * `mbedtls_test_ssl_buffer_put()` and `mbedtls_test_ssl_buffer_get()`
223 * on \p buf.
224 */
225int mbedtls_test_ssl_buffer_setup(mbedtls_test_ssl_buffer *buf,
226 size_t capacity);
227
228void mbedtls_test_ssl_buffer_free(mbedtls_test_ssl_buffer *buf);
229
230/*
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800231 * Puts \p input_len bytes from the \p input buffer into the ring buffer \p buf.
232 *
233 * \p buf must have been initialized and set up by calling
234 * `mbedtls_test_ssl_buffer_init()` and `mbedtls_test_ssl_buffer_setup()`.
235 *
236 * \retval \p input_len, if the data fits.
237 * \retval 0 <= value < \p input_len, if the data does not fit.
238 * \retval -1, if \p buf is NULL, it hasn't been set up or \p input_len is not
239 * zero and \p input is NULL.
240 */
241int mbedtls_test_ssl_buffer_put(mbedtls_test_ssl_buffer *buf,
242 const unsigned char *input, size_t input_len);
243
244/*
245 * Gets \p output_len bytes from the ring buffer \p buf into the
246 * \p output buffer. The output buffer can be NULL, in this case a part of the
247 * ring buffer will be dropped, if the requested length is available.
248 *
249 * \p buf must have been initialized and set up by calling
250 * `mbedtls_test_ssl_buffer_init()` and `mbedtls_test_ssl_buffer_setup()`.
251 *
252 * \retval \p output_len, if the data is available.
253 * \retval 0 <= value < \p output_len, if the data is not available.
254 * \retval -1, if \buf is NULL or it hasn't been set up.
255 */
256int mbedtls_test_ssl_buffer_get(mbedtls_test_ssl_buffer *buf,
257 unsigned char *output, size_t output_len);
258
259/*
260 * Errors used in the message transport mock tests
261 */
262 #define MBEDTLS_TEST_ERROR_ARG_NULL -11
263 #define MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED -44
264
265/*
266 * Setup and free functions for the message metadata queue.
267 *
268 * \p capacity describes the number of message metadata chunks that can be held
269 * within the queue.
270 *
271 * \retval 0, if a metadata queue of a given length can be allocated.
272 * \retval MBEDTLS_ERR_SSL_ALLOC_FAILED, if allocation failed.
273 */
274int mbedtls_test_ssl_message_queue_setup(
275 mbedtls_test_ssl_message_queue *queue, size_t capacity);
276
277void mbedtls_test_ssl_message_queue_free(
278 mbedtls_test_ssl_message_queue *queue);
279
280/*
281 * Push message length information onto the message metadata queue.
282 * This will become the last element to leave it (fifo).
283 *
284 * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null.
285 * \retval MBEDTLS_ERR_SSL_WANT_WRITE, if the queue is full.
286 * \retval \p len, if the push was successful.
287 */
288int mbedtls_test_ssl_message_queue_push_info(
289 mbedtls_test_ssl_message_queue *queue, size_t len);
290
291/*
292 * Pop information about the next message length from the queue. This will be
293 * the oldest inserted message length(fifo). \p msg_len can be null, in which
294 * case the data will be popped from the queue but not copied anywhere.
295 *
296 * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null.
297 * \retval MBEDTLS_ERR_SSL_WANT_READ, if the queue is empty.
298 * \retval message length, if the pop was successful, up to the given
299 \p buf_len.
300 */
301int mbedtls_test_ssl_message_queue_pop_info(
302 mbedtls_test_ssl_message_queue *queue, size_t buf_len);
303
304/*
305 * Setup and teardown functions for mock sockets.
306 */
Yanray Wang5f86a422023-03-15 16:02:29 +0800307void mbedtls_test_mock_socket_init(mbedtls_test_mock_socket *socket);
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800308
309/*
310 * Closes the socket \p socket.
311 *
312 * \p socket must have been previously initialized by calling
Yanray Wang5f86a422023-03-15 16:02:29 +0800313 * mbedtls_test_mock_socket_init().
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800314 *
315 * This function frees all allocated resources and both sockets are aware of the
316 * new connection state.
317 *
318 * That is, this function does not simulate half-open TCP connections and the
319 * phenomenon that when closing a UDP connection the peer is not aware of the
320 * connection having been closed.
321 */
322void mbedtls_test_mock_socket_close(mbedtls_test_mock_socket *socket);
323
324/*
325 * Establishes a connection between \p peer1 and \p peer2.
326 *
327 * \p peer1 and \p peer2 must have been previously initialized by calling
Yanray Wang5f86a422023-03-15 16:02:29 +0800328 * mbedtls_test_mock_socket_init().
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800329 *
330 * The capacities of the internal buffers are set to \p bufsize. Setting this to
331 * the correct value allows for simulation of MTU, sanity testing the mock
332 * implementation and mocking TCP connections with lower memory cost.
333 */
334int mbedtls_test_mock_socket_connect(mbedtls_test_mock_socket *peer1,
335 mbedtls_test_mock_socket *peer2,
336 size_t bufsize);
337
338
339/*
340 * Callbacks for simulating blocking I/O over connection-oriented transport.
341 */
Yanray Wangaf727a22023-03-13 19:22:36 +0800342int mbedtls_test_mock_tcp_send_b(void *ctx,
343 const unsigned char *buf, size_t len);
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800344
345int mbedtls_test_mock_tcp_recv_b(void *ctx, unsigned char *buf, size_t len);
346
347/*
348 * Callbacks for simulating non-blocking I/O over connection-oriented transport.
349 */
Yanray Wangaf727a22023-03-13 19:22:36 +0800350int mbedtls_test_mock_tcp_send_nb(void *ctx,
351 const unsigned char *buf, size_t len);
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800352
353int mbedtls_test_mock_tcp_recv_nb(void *ctx, unsigned char *buf, size_t len);
354
355void mbedtls_test_message_socket_init(
356 mbedtls_test_message_socket_context *ctx);
357
358/*
359 * Setup a given message socket context including initialization of
360 * input/output queues to a chosen capacity of messages. Also set the
361 * corresponding mock socket.
362 *
363 * \retval 0, if everything succeeds.
364 * \retval MBEDTLS_ERR_SSL_ALLOC_FAILED, if allocation of a message
365 * queue failed.
366 */
367int mbedtls_test_message_socket_setup(
368 mbedtls_test_ssl_message_queue *queue_input,
369 mbedtls_test_ssl_message_queue *queue_output,
Yanray Wangd19894f2023-03-16 11:47:39 +0800370 size_t queue_capacity,
371 mbedtls_test_mock_socket *socket,
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800372 mbedtls_test_message_socket_context *ctx);
373
374/*
375 * Close a given message socket context, along with the socket itself. Free the
376 * memory allocated by the input queue.
377 */
378void mbedtls_test_message_socket_close(
379 mbedtls_test_message_socket_context *ctx);
380
381/*
382 * Send one message through a given message socket context.
383 *
384 * \retval \p len, if everything succeeds.
385 * \retval MBEDTLS_TEST_ERROR_CONTEXT_ERROR, if any of the needed context
386 * elements or the context itself is null.
387 * \retval MBEDTLS_TEST_ERROR_SEND_FAILED if
388 * mbedtls_test_mock_tcp_send_b failed.
389 * \retval MBEDTLS_ERR_SSL_WANT_WRITE, if the output queue is full.
390 *
391 * This function will also return any error from
392 * mbedtls_test_ssl_message_queue_push_info.
393 */
Yanray Wangaf727a22023-03-13 19:22:36 +0800394int mbedtls_test_mock_tcp_send_msg(void *ctx,
395 const unsigned char *buf, size_t len);
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800396
397/*
398 * Receive one message from a given message socket context and return message
399 * length or an error.
400 *
401 * \retval message length, 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_RECV_FAILED if
405 * mbedtls_test_mock_tcp_recv_b failed.
406 *
407 * This function will also return any error other than
Yanray Wang5e22a922023-03-16 14:57:54 +0800408 * MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED from test_ssl_message_queue_peek_info.
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800409 */
Yanray Wangaf727a22023-03-13 19:22:36 +0800410int mbedtls_test_mock_tcp_recv_msg(void *ctx,
411 unsigned char *buf, size_t buf_len);
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800412
413#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
414
415/*
416 * Initializes \p ep_cert structure and assigns it to endpoint
417 * represented by \p ep.
418 *
419 * \retval 0 on success, otherwise error code.
420 */
421int mbedtls_test_ssl_endpoint_certificate_init(mbedtls_test_ssl_endpoint *ep,
422 int pk_alg,
423 int opaque_alg, int opaque_alg2,
424 int opaque_usage);
425
426/*
427 * Initializes \p ep structure. It is important to call
428 * `mbedtls_test_ssl_endpoint_free()` after calling this function
429 * even if it fails.
430 *
431 * \p endpoint_type must be set as MBEDTLS_SSL_IS_SERVER or
432 * MBEDTLS_SSL_IS_CLIENT.
433 * \p pk_alg the algorithm to use, currently only MBEDTLS_PK_RSA and
434 * MBEDTLS_PK_ECDSA are supported.
435 * \p dtls_context - in case of DTLS - this is the context handling metadata.
436 * \p input_queue - used only in case of DTLS.
437 * \p output_queue - used only in case of DTLS.
438 *
439 * \retval 0 on success, otherwise error code.
440 */
441int mbedtls_test_ssl_endpoint_init(
442 mbedtls_test_ssl_endpoint *ep, int endpoint_type,
443 mbedtls_test_handshake_test_options *options,
444 mbedtls_test_message_socket_context *dtls_context,
445 mbedtls_test_ssl_message_queue *input_queue,
Ronald Cronfb536472024-01-26 14:55:25 +0100446 mbedtls_test_ssl_message_queue *output_queue);
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800447
448/*
449 * Deinitializes endpoint represented by \p ep.
450 */
451void mbedtls_test_ssl_endpoint_free(
452 mbedtls_test_ssl_endpoint *ep,
453 mbedtls_test_message_socket_context *context);
454
455/*
456 * This function moves ssl handshake from \p ssl to prescribed \p state.
457 * /p second_ssl is used as second endpoint and their sockets have to be
458 * connected before calling this function.
459 *
460 * \retval 0 on success, otherwise error code.
461 */
462int mbedtls_test_move_handshake_to_state(mbedtls_ssl_context *ssl,
463 mbedtls_ssl_context *second_ssl,
464 int state);
465
466#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
467
Yanray Wang1fca4de2023-02-06 12:10:48 +0800468/*
469 * Helper function setting up inverse record transformations
470 * using given cipher, hash, EtM mode, authentication tag length,
471 * and version.
472 */
473#define CHK(x) \
474 do \
475 { \
476 if (!(x)) \
477 { \
478 ret = -1; \
479 goto cleanup; \
480 } \
481 } while (0)
482
Yanray Wang25b766f2023-03-15 16:39:05 +0800483#if MBEDTLS_SSL_CID_OUT_LEN_MAX > MBEDTLS_SSL_CID_IN_LEN_MAX
484#define SSL_CID_LEN_MIN MBEDTLS_SSL_CID_IN_LEN_MAX
485#else
486#define SSL_CID_LEN_MIN MBEDTLS_SSL_CID_OUT_LEN_MAX
487#endif
488
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800489#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Pengyu Lvba6825e2023-11-08 12:16:29 +0800490 defined(MBEDTLS_SSL_HAVE_CBC) && defined(MBEDTLS_SSL_HAVE_AES)
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800491int mbedtls_test_psa_cipher_encrypt_helper(mbedtls_ssl_transform *transform,
492 const unsigned char *iv,
493 size_t iv_len,
494 const unsigned char *input,
495 size_t ilen,
496 unsigned char *output,
497 size_t *olen);
Pengyu Lvba6825e2023-11-08 12:16:29 +0800498#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_HAVE_CBC &&
499 MBEDTLS_SSL_HAVE_AES */
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800500
501int mbedtls_test_ssl_build_transforms(mbedtls_ssl_transform *t_in,
502 mbedtls_ssl_transform *t_out,
503 int cipher_type, int hash_id,
504 int etm, int tag_mode,
505 mbedtls_ssl_protocol_version tls_version,
506 size_t cid0_len,
507 size_t cid1_len);
508
Gilles Peskine9099d3f2023-09-18 13:11:50 +0200509#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
510/**
511 * \param[in,out] record The record to prepare.
512 * It must contain the data to MAC at offset
513 * `record->data_offset`, of length
514 * `record->data_length`.
515 * On success, write the MAC immediately
516 * after the data and increment
517 * `record->data_length` accordingly.
518 * \param[in,out] transform_out The out transform, typically prepared by
519 * mbedtls_test_ssl_build_transforms().
520 * Its HMAC context may be used. Other than that
521 * it is treated as an input parameter.
522 *
523 * \return 0 on success, an `MBEDTLS_ERR_xxx` error code
524 * or -1 on error.
525 */
526int mbedtls_test_ssl_prepare_record_mac(mbedtls_record *record,
527 mbedtls_ssl_transform *transform_out);
528#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
529
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800530/*
531 * Populate a session structure for serialization tests.
532 * Choose dummy values, mostly non-0 to distinguish from the init default.
533 */
534int mbedtls_test_ssl_tls12_populate_session(mbedtls_ssl_session *session,
535 int ticket_len,
Ronald Cron7b1921a2023-11-23 12:31:56 +0100536 int endpoint_type,
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800537 const char *crt_file);
538
539#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
540int mbedtls_test_ssl_tls13_populate_session(mbedtls_ssl_session *session,
541 int ticket_len,
542 int endpoint_type);
543#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
544
545/*
546 * Perform data exchanging between \p ssl_1 and \p ssl_2 and check if the
547 * message was sent in the correct number of fragments.
548 *
549 * /p ssl_1 and /p ssl_2 Endpoints represented by mbedtls_ssl_context. Both
550 * of them must be initialized and connected
551 * beforehand.
552 * /p msg_len_1 and /p msg_len_2 specify the size of the message to send.
553 * /p expected_fragments_1 and /p expected_fragments_2 determine in how many
554 * fragments the message should be sent.
555 * expected_fragments is 0: can be used for DTLS testing while the message
556 * size is larger than MFL. In that case the message
557 * cannot be fragmented and sent to the second
558 * endpoint.
559 * This value can be used for negative tests.
560 * expected_fragments is 1: can be used for TLS/DTLS testing while the
561 * message size is below MFL
562 * expected_fragments > 1: can be used for TLS testing while the message
563 * size is larger than MFL
564 *
565 * \retval 0 on success, otherwise error code.
566 */
Yanray Wangb088bfc2023-03-16 12:15:49 +0800567int mbedtls_test_ssl_exchange_data(
568 mbedtls_ssl_context *ssl_1,
569 int msg_len_1, const int expected_fragments_1,
570 mbedtls_ssl_context *ssl_2,
571 int msg_len_2, const int expected_fragments_2);
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800572
573#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
574void mbedtls_test_ssl_perform_handshake(
575 mbedtls_test_handshake_test_options *options);
576#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
577
578#if defined(MBEDTLS_TEST_HOOKS)
579/*
580 * Tweak vector lengths in a TLS 1.3 Certificate message
581 *
582 * \param[in] buf Buffer containing the Certificate message to tweak
583 * \param[in]]out] end End of the buffer to parse
584 * \param tweak Tweak identifier (from 1 to the number of tweaks).
585 * \param[out] expected_result Error code expected from the parsing function
586 * \param[out] args Arguments of the MBEDTLS_SSL_CHK_BUF_READ_PTR call that
587 * is expected to fail. All zeroes if no
588 * MBEDTLS_SSL_CHK_BUF_READ_PTR failure is expected.
589 */
Yanray Wangf56181a2023-03-16 12:21:33 +0800590int mbedtls_test_tweak_tls13_certificate_msg_vector_len(
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800591 unsigned char *buf, unsigned char **end, int tweak,
592 int *expected_result, mbedtls_ssl_chk_buf_ptr_args *args);
593#endif /* MBEDTLS_TEST_HOOKS */
Yanray Wang1db628f2023-02-03 11:01:29 +0800594
Ronald Cron77abfe62024-01-15 11:17:31 +0100595#if defined(MBEDTLS_SSL_SESSION_TICKETS)
596int mbedtls_test_ticket_write(
597 void *p_ticket, const mbedtls_ssl_session *session,
598 unsigned char *start, const unsigned char *end,
599 size_t *tlen, uint32_t *ticket_lifetime);
600
601int mbedtls_test_ticket_parse(void *p_ticket, mbedtls_ssl_session *session,
602 unsigned char *buf, size_t len);
603#endif /* MBEDTLS_SSL_SESSION_TICKETS */
604
Ronald Cron1f6e4e42024-01-26 16:31:33 +0100605#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SRV_C) && \
606 defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS) && \
607 defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
608int mbedtls_test_get_tls13_ticket(
609 mbedtls_test_handshake_test_options *client_options,
610 mbedtls_test_handshake_test_options *server_options,
611 mbedtls_ssl_session *session);
Ronald Cronb9a9b1f2024-02-14 11:28:05 +0100612#endif
Ronald Cron1f6e4e42024-01-26 16:31:33 +0100613
Yanray Wang1db628f2023-02-03 11:01:29 +0800614#define ECJPAKE_TEST_PWD "bla"
615
616#if defined(MBEDTLS_USE_PSA_CRYPTO)
617#define ECJPAKE_TEST_SET_PASSWORD(exp_ret_val) \
618 ret = (use_opaque_arg) ? \
619 mbedtls_ssl_set_hs_ecjpake_password_opaque(&ssl, pwd_slot) : \
620 mbedtls_ssl_set_hs_ecjpake_password(&ssl, pwd_string, pwd_len); \
621 TEST_EQUAL(ret, exp_ret_val)
622#else
623#define ECJPAKE_TEST_SET_PASSWORD(exp_ret_val) \
624 ret = mbedtls_ssl_set_hs_ecjpake_password(&ssl, \
625 pwd_string, pwd_len); \
626 TEST_EQUAL(ret, exp_ret_val)
627#endif /* MBEDTLS_USE_PSA_CRYPTO */
628
Yanray Wang09a6f7e2023-02-03 11:04:38 +0800629#define TEST_AVAILABLE_ECC(tls_id_, group_id_, psa_family_, psa_bits_) \
630 TEST_EQUAL(mbedtls_ssl_get_ecp_group_id_from_tls_id(tls_id_), \
631 group_id_); \
632 TEST_EQUAL(mbedtls_ssl_get_tls_id_from_ecp_group_id(group_id_), \
633 tls_id_); \
634 TEST_EQUAL(mbedtls_ssl_get_psa_curve_info_from_tls_id(tls_id_, \
Przemek Stekielda4fba62023-06-02 14:52:28 +0200635 &psa_type, &psa_bits), PSA_SUCCESS); \
636 TEST_EQUAL(psa_family_, PSA_KEY_TYPE_ECC_GET_FAMILY(psa_type)); \
Yanray Wang09a6f7e2023-02-03 11:04:38 +0800637 TEST_EQUAL(psa_bits_, psa_bits);
638
639#define TEST_UNAVAILABLE_ECC(tls_id_, group_id_, psa_family_, psa_bits_) \
640 TEST_EQUAL(mbedtls_ssl_get_ecp_group_id_from_tls_id(tls_id_), \
641 MBEDTLS_ECP_DP_NONE); \
642 TEST_EQUAL(mbedtls_ssl_get_tls_id_from_ecp_group_id(group_id_), \
643 0); \
644 TEST_EQUAL(mbedtls_ssl_get_psa_curve_info_from_tls_id(tls_id_, \
Przemek Stekielda4fba62023-06-02 14:52:28 +0200645 &psa_type, &psa_bits), \
Yanray Wang09a6f7e2023-02-03 11:04:38 +0800646 PSA_ERROR_NOT_SUPPORTED);
647
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800648#endif /* MBEDTLS_SSL_TLS_C */
649
Yanray Wang47907a42022-10-24 14:42:01 +0800650#endif /* SSL_HELPERS_H */