blob: ef4927f72ecf5c8c7e53841319f6e27c9da2973c [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
Valerio Setti8438c632025-01-22 11:56:40 +010069#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Yanray Wang5ba709c2023-02-03 11:07:56 +080070 defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
71#define MBEDTLS_CAN_HANDLE_RSA_TEST_KEY
72#endif
Gabor Mezei14950932025-03-06 16:06:42 +010073
Gabor Mezei8829aa32025-03-07 13:21:37 +010074#if defined(PSA_WANT_ALG_GCM) || \
75 defined(PSA_WANT_ALG_CCM) || \
Gabor Mezei14950932025-03-06 16:06:42 +010076 defined(PSA_WANT_ALG_CHACHA20_POLY1305)
77#define MBEDTLS_TEST_HAS_AEAD_ALG
78#endif
79
Yanray Wang5ba709c2023-02-03 11:07:56 +080080enum {
81#define MBEDTLS_SSL_TLS1_3_LABEL(name, string) \
82 tls13_label_ ## name,
83 MBEDTLS_SSL_TLS1_3_LABEL_LIST
84#undef MBEDTLS_SSL_TLS1_3_LABEL
85};
86
Waleed Elmelegy4dfb0e72024-03-14 01:48:40 +000087#if defined(MBEDTLS_SSL_ALPN)
88#define MBEDTLS_TEST_MAX_ALPN_LIST_SIZE 10
89#endif
90
Yanray Wang55a66192022-10-26 09:57:53 +080091typedef struct mbedtls_test_ssl_log_pattern {
92 const char *pattern;
93 size_t counter;
94} mbedtls_test_ssl_log_pattern;
95
96typedef struct mbedtls_test_handshake_test_options {
97 const char *cipher;
Ronald Cronfb536472024-01-26 14:55:25 +010098 uint16_t *group_list;
Yanray Wang55a66192022-10-26 09:57:53 +080099 mbedtls_ssl_protocol_version client_min_version;
100 mbedtls_ssl_protocol_version client_max_version;
101 mbedtls_ssl_protocol_version server_min_version;
102 mbedtls_ssl_protocol_version server_max_version;
103 mbedtls_ssl_protocol_version expected_negotiated_version;
104 int expected_handshake_result;
105 int expected_ciphersuite;
106 int pk_alg;
107 int opaque_alg;
108 int opaque_alg2;
109 int opaque_usage;
110 data_t *psk_str;
111 int dtls;
112 int srv_auth_mode;
113 int serialize;
114 int mfl;
115 int cli_msg_len;
116 int srv_msg_len;
117 int expected_cli_fragments;
118 int expected_srv_fragments;
119 int renegotiate;
120 int legacy_renegotiation;
121 void *srv_log_obj;
122 void *cli_log_obj;
123 void (*srv_log_fun)(void *, int, const char *, int, const char *);
124 void (*cli_log_fun)(void *, int, const char *, int, const char *);
125 int resize_buffers;
Ronald Cronced99be2024-01-26 15:49:12 +0100126 int early_data;
Ronald Cron5d3036e2024-02-23 07:43:45 +0100127 int max_early_data_size;
Yanray Wang55a66192022-10-26 09:57:53 +0800128#if defined(MBEDTLS_SSL_CACHE_C)
129 mbedtls_ssl_cache_context *cache;
130#endif
Waleed Elmelegy4dfb0e72024-03-14 01:48:40 +0000131#if defined(MBEDTLS_SSL_ALPN)
132 const char *alpn_list[MBEDTLS_TEST_MAX_ALPN_LIST_SIZE];
133#endif
Yanray Wang55a66192022-10-26 09:57:53 +0800134} mbedtls_test_handshake_test_options;
135
Yanray Wang25b766f2023-03-15 16:39:05 +0800136/*
137 * Buffer structure for custom I/O callbacks.
138 */
Yanray Wang55a66192022-10-26 09:57:53 +0800139typedef struct mbedtls_test_ssl_buffer {
140 size_t start;
141 size_t content_length;
142 size_t capacity;
143 unsigned char *buffer;
144} mbedtls_test_ssl_buffer;
145
146/*
147 * Context for a message metadata queue (fifo) that is on top of the ring buffer.
148 */
149typedef struct mbedtls_test_ssl_message_queue {
150 size_t *messages;
151 int pos;
152 int num;
153 int capacity;
154} mbedtls_test_ssl_message_queue;
155
156/*
157 * Context for the I/O callbacks simulating network connection.
158 */
159
160#define MBEDTLS_MOCK_SOCKET_CONNECTED 1
161
162typedef struct mbedtls_test_mock_socket {
163 int status;
164 mbedtls_test_ssl_buffer *input;
165 mbedtls_test_ssl_buffer *output;
166 struct mbedtls_test_mock_socket *peer;
167} mbedtls_test_mock_socket;
168
169/* Errors used in the message socket mocks */
170
171#define MBEDTLS_TEST_ERROR_CONTEXT_ERROR -55
172#define MBEDTLS_TEST_ERROR_SEND_FAILED -66
173#define MBEDTLS_TEST_ERROR_RECV_FAILED -77
174
175/*
176 * Structure used as an addon, or a wrapper, around the mocked sockets.
177 * Contains an input queue, to which the other socket pushes metadata,
178 * and an output queue, to which this one pushes metadata. This context is
179 * considered as an owner of the input queue only, which is initialized and
180 * freed in the respective setup and free calls.
181 */
182typedef struct mbedtls_test_message_socket_context {
183 mbedtls_test_ssl_message_queue *queue_input;
184 mbedtls_test_ssl_message_queue *queue_output;
185 mbedtls_test_mock_socket *socket;
186} mbedtls_test_message_socket_context;
187
188#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
189
190/*
191 * Structure with endpoint's certificates for SSL communication tests.
192 */
193typedef struct mbedtls_test_ssl_endpoint_certificate {
194 mbedtls_x509_crt *ca_cert;
195 mbedtls_x509_crt *cert;
196 mbedtls_pk_context *pkey;
197} mbedtls_test_ssl_endpoint_certificate;
198
199/*
200 * Endpoint structure for SSL communication tests.
201 */
202typedef struct mbedtls_test_ssl_endpoint {
203 const char *name;
204 mbedtls_ssl_context ssl;
205 mbedtls_ssl_config conf;
206 mbedtls_test_mock_socket socket;
207 mbedtls_test_ssl_endpoint_certificate cert;
208} mbedtls_test_ssl_endpoint;
209
210#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Yanray Wang47907a42022-10-24 14:42:01 +0800211
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800212/*
Ronald Cron10b040f2024-02-05 09:38:09 +0100213 * Random number generator aimed for TLS unitary tests. Its main purpose is to
214 * simplify the set-up of a random number generator for TLS
215 * unitary tests: no need to set up a good entropy source for example.
216 */
217int mbedtls_test_random(void *p_rng, unsigned char *output, size_t output_len);
Yanray Wangf88e5292023-12-01 16:39:34 +0800218
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800219/*
220 * This function can be passed to mbedtls to receive output logs from it. In
221 * this case, it will count the instances of a mbedtls_test_ssl_log_pattern
222 * in the received logged messages.
223 */
224void mbedtls_test_ssl_log_analyzer(void *ctx, int level,
225 const char *file, int line,
226 const char *str);
227
228void mbedtls_test_init_handshake_options(
229 mbedtls_test_handshake_test_options *opts);
230
231void mbedtls_test_free_handshake_options(
232 mbedtls_test_handshake_test_options *opts);
233
234/*
235 * Initialises \p buf. After calling this function it is safe to call
236 * `mbedtls_test_ssl_buffer_free()` on \p buf.
237 */
238void mbedtls_test_ssl_buffer_init(mbedtls_test_ssl_buffer *buf);
239
240/*
241 * Sets up \p buf. After calling this function it is safe to call
242 * `mbedtls_test_ssl_buffer_put()` and `mbedtls_test_ssl_buffer_get()`
243 * on \p buf.
244 */
245int mbedtls_test_ssl_buffer_setup(mbedtls_test_ssl_buffer *buf,
246 size_t capacity);
247
248void mbedtls_test_ssl_buffer_free(mbedtls_test_ssl_buffer *buf);
249
250/*
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800251 * Puts \p input_len bytes from the \p input buffer into the ring buffer \p buf.
252 *
253 * \p buf must have been initialized and set up by calling
254 * `mbedtls_test_ssl_buffer_init()` and `mbedtls_test_ssl_buffer_setup()`.
255 *
256 * \retval \p input_len, if the data fits.
257 * \retval 0 <= value < \p input_len, if the data does not fit.
258 * \retval -1, if \p buf is NULL, it hasn't been set up or \p input_len is not
259 * zero and \p input is NULL.
260 */
261int mbedtls_test_ssl_buffer_put(mbedtls_test_ssl_buffer *buf,
262 const unsigned char *input, size_t input_len);
263
264/*
265 * Gets \p output_len bytes from the ring buffer \p buf into the
266 * \p output buffer. The output buffer can be NULL, in this case a part of the
267 * ring buffer will be dropped, if the requested length is available.
268 *
269 * \p buf must have been initialized and set up by calling
270 * `mbedtls_test_ssl_buffer_init()` and `mbedtls_test_ssl_buffer_setup()`.
271 *
272 * \retval \p output_len, if the data is available.
273 * \retval 0 <= value < \p output_len, if the data is not available.
274 * \retval -1, if \buf is NULL or it hasn't been set up.
275 */
276int mbedtls_test_ssl_buffer_get(mbedtls_test_ssl_buffer *buf,
277 unsigned char *output, size_t output_len);
278
279/*
280 * Errors used in the message transport mock tests
281 */
282 #define MBEDTLS_TEST_ERROR_ARG_NULL -11
283 #define MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED -44
284
285/*
286 * Setup and free functions for the message metadata queue.
287 *
288 * \p capacity describes the number of message metadata chunks that can be held
289 * within the queue.
290 *
291 * \retval 0, if a metadata queue of a given length can be allocated.
292 * \retval MBEDTLS_ERR_SSL_ALLOC_FAILED, if allocation failed.
293 */
294int mbedtls_test_ssl_message_queue_setup(
295 mbedtls_test_ssl_message_queue *queue, size_t capacity);
296
297void mbedtls_test_ssl_message_queue_free(
298 mbedtls_test_ssl_message_queue *queue);
299
300/*
301 * Push message length information onto the message metadata queue.
302 * This will become the last element to leave it (fifo).
303 *
304 * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null.
305 * \retval MBEDTLS_ERR_SSL_WANT_WRITE, if the queue is full.
306 * \retval \p len, if the push was successful.
307 */
308int mbedtls_test_ssl_message_queue_push_info(
309 mbedtls_test_ssl_message_queue *queue, size_t len);
310
311/*
312 * Pop information about the next message length from the queue. This will be
313 * the oldest inserted message length(fifo). \p msg_len can be null, in which
314 * case the data will be popped from the queue but not copied anywhere.
315 *
316 * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null.
317 * \retval MBEDTLS_ERR_SSL_WANT_READ, if the queue is empty.
318 * \retval message length, if the pop was successful, up to the given
319 \p buf_len.
320 */
321int mbedtls_test_ssl_message_queue_pop_info(
322 mbedtls_test_ssl_message_queue *queue, size_t buf_len);
323
324/*
325 * Setup and teardown functions for mock sockets.
326 */
Yanray Wang5f86a422023-03-15 16:02:29 +0800327void mbedtls_test_mock_socket_init(mbedtls_test_mock_socket *socket);
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800328
329/*
330 * Closes the socket \p socket.
331 *
332 * \p socket must have been previously initialized by calling
Yanray Wang5f86a422023-03-15 16:02:29 +0800333 * mbedtls_test_mock_socket_init().
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800334 *
335 * This function frees all allocated resources and both sockets are aware of the
336 * new connection state.
337 *
338 * That is, this function does not simulate half-open TCP connections and the
339 * phenomenon that when closing a UDP connection the peer is not aware of the
340 * connection having been closed.
341 */
342void mbedtls_test_mock_socket_close(mbedtls_test_mock_socket *socket);
343
344/*
345 * Establishes a connection between \p peer1 and \p peer2.
346 *
347 * \p peer1 and \p peer2 must have been previously initialized by calling
Yanray Wang5f86a422023-03-15 16:02:29 +0800348 * mbedtls_test_mock_socket_init().
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800349 *
350 * The capacities of the internal buffers are set to \p bufsize. Setting this to
351 * the correct value allows for simulation of MTU, sanity testing the mock
352 * implementation and mocking TCP connections with lower memory cost.
353 */
354int mbedtls_test_mock_socket_connect(mbedtls_test_mock_socket *peer1,
355 mbedtls_test_mock_socket *peer2,
356 size_t bufsize);
357
358
359/*
360 * Callbacks for simulating blocking I/O over connection-oriented transport.
361 */
Yanray Wangaf727a22023-03-13 19:22:36 +0800362int mbedtls_test_mock_tcp_send_b(void *ctx,
363 const unsigned char *buf, size_t len);
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800364
365int mbedtls_test_mock_tcp_recv_b(void *ctx, unsigned char *buf, size_t len);
366
367/*
368 * Callbacks for simulating non-blocking I/O over connection-oriented transport.
369 */
Yanray Wangaf727a22023-03-13 19:22:36 +0800370int mbedtls_test_mock_tcp_send_nb(void *ctx,
371 const unsigned char *buf, size_t len);
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800372
373int mbedtls_test_mock_tcp_recv_nb(void *ctx, unsigned char *buf, size_t len);
374
375void mbedtls_test_message_socket_init(
376 mbedtls_test_message_socket_context *ctx);
377
378/*
379 * Setup a given message socket context including initialization of
380 * input/output queues to a chosen capacity of messages. Also set the
381 * corresponding mock socket.
382 *
383 * \retval 0, if everything succeeds.
384 * \retval MBEDTLS_ERR_SSL_ALLOC_FAILED, if allocation of a message
385 * queue failed.
386 */
387int mbedtls_test_message_socket_setup(
388 mbedtls_test_ssl_message_queue *queue_input,
389 mbedtls_test_ssl_message_queue *queue_output,
Yanray Wangd19894f2023-03-16 11:47:39 +0800390 size_t queue_capacity,
391 mbedtls_test_mock_socket *socket,
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800392 mbedtls_test_message_socket_context *ctx);
393
394/*
395 * Close a given message socket context, along with the socket itself. Free the
396 * memory allocated by the input queue.
397 */
398void mbedtls_test_message_socket_close(
399 mbedtls_test_message_socket_context *ctx);
400
401/*
402 * Send one message through a given message socket context.
403 *
404 * \retval \p len, if everything succeeds.
405 * \retval MBEDTLS_TEST_ERROR_CONTEXT_ERROR, if any of the needed context
406 * elements or the context itself is null.
407 * \retval MBEDTLS_TEST_ERROR_SEND_FAILED if
408 * mbedtls_test_mock_tcp_send_b failed.
409 * \retval MBEDTLS_ERR_SSL_WANT_WRITE, if the output queue is full.
410 *
411 * This function will also return any error from
412 * mbedtls_test_ssl_message_queue_push_info.
413 */
Yanray Wangaf727a22023-03-13 19:22:36 +0800414int mbedtls_test_mock_tcp_send_msg(void *ctx,
415 const unsigned char *buf, size_t len);
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800416
417/*
418 * Receive one message from a given message socket context and return message
419 * length or an error.
420 *
421 * \retval message length, if everything succeeds.
422 * \retval MBEDTLS_TEST_ERROR_CONTEXT_ERROR, if any of the needed context
423 * elements or the context itself is null.
424 * \retval MBEDTLS_TEST_ERROR_RECV_FAILED if
425 * mbedtls_test_mock_tcp_recv_b failed.
426 *
427 * This function will also return any error other than
Yanray Wang5e22a922023-03-16 14:57:54 +0800428 * MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED from test_ssl_message_queue_peek_info.
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800429 */
Yanray Wangaf727a22023-03-13 19:22:36 +0800430int mbedtls_test_mock_tcp_recv_msg(void *ctx,
431 unsigned char *buf, size_t buf_len);
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800432
433#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
434
435/*
436 * Initializes \p ep_cert structure and assigns it to endpoint
437 * represented by \p ep.
438 *
439 * \retval 0 on success, otherwise error code.
440 */
441int mbedtls_test_ssl_endpoint_certificate_init(mbedtls_test_ssl_endpoint *ep,
442 int pk_alg,
443 int opaque_alg, int opaque_alg2,
444 int opaque_usage);
445
446/*
447 * Initializes \p ep structure. It is important to call
448 * `mbedtls_test_ssl_endpoint_free()` after calling this function
449 * even if it fails.
450 *
451 * \p endpoint_type must be set as MBEDTLS_SSL_IS_SERVER or
452 * MBEDTLS_SSL_IS_CLIENT.
453 * \p pk_alg the algorithm to use, currently only MBEDTLS_PK_RSA and
454 * MBEDTLS_PK_ECDSA are supported.
455 * \p dtls_context - in case of DTLS - this is the context handling metadata.
456 * \p input_queue - used only in case of DTLS.
457 * \p output_queue - used only in case of DTLS.
458 *
459 * \retval 0 on success, otherwise error code.
460 */
461int mbedtls_test_ssl_endpoint_init(
462 mbedtls_test_ssl_endpoint *ep, int endpoint_type,
463 mbedtls_test_handshake_test_options *options,
464 mbedtls_test_message_socket_context *dtls_context,
465 mbedtls_test_ssl_message_queue *input_queue,
Ronald Cronfb536472024-01-26 14:55:25 +0100466 mbedtls_test_ssl_message_queue *output_queue);
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800467
468/*
469 * Deinitializes endpoint represented by \p ep.
470 */
471void mbedtls_test_ssl_endpoint_free(
472 mbedtls_test_ssl_endpoint *ep,
473 mbedtls_test_message_socket_context *context);
474
475/*
476 * This function moves ssl handshake from \p ssl to prescribed \p state.
477 * /p second_ssl is used as second endpoint and their sockets have to be
478 * connected before calling this function.
479 *
480 * \retval 0 on success, otherwise error code.
481 */
482int mbedtls_test_move_handshake_to_state(mbedtls_ssl_context *ssl,
483 mbedtls_ssl_context *second_ssl,
484 int state);
485
486#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
487
Yanray Wang1fca4de2023-02-06 12:10:48 +0800488/*
489 * Helper function setting up inverse record transformations
490 * using given cipher, hash, EtM mode, authentication tag length,
491 * and version.
492 */
493#define CHK(x) \
494 do \
495 { \
496 if (!(x)) \
497 { \
498 ret = -1; \
499 goto cleanup; \
500 } \
501 } while (0)
502
Yanray Wang25b766f2023-03-15 16:39:05 +0800503#if MBEDTLS_SSL_CID_OUT_LEN_MAX > MBEDTLS_SSL_CID_IN_LEN_MAX
504#define SSL_CID_LEN_MIN MBEDTLS_SSL_CID_IN_LEN_MAX
505#else
506#define SSL_CID_LEN_MIN MBEDTLS_SSL_CID_OUT_LEN_MAX
507#endif
508
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800509#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Elena Uziunaite74342c72024-07-05 11:31:29 +0100510 defined(PSA_WANT_ALG_CBC_NO_PADDING) && defined(PSA_WANT_KEY_TYPE_AES)
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800511int mbedtls_test_psa_cipher_encrypt_helper(mbedtls_ssl_transform *transform,
512 const unsigned char *iv,
513 size_t iv_len,
514 const unsigned char *input,
515 size_t ilen,
516 unsigned char *output,
517 size_t *olen);
Elena Uziunaite74342c72024-07-05 11:31:29 +0100518#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && PSA_WANT_ALG_CBC_NO_PADDING &&
Elena Uziunaite6121a342024-07-05 11:16:53 +0100519 PSA_WANT_KEY_TYPE_AES */
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800520
521int mbedtls_test_ssl_build_transforms(mbedtls_ssl_transform *t_in,
522 mbedtls_ssl_transform *t_out,
523 int cipher_type, int hash_id,
524 int etm, int tag_mode,
525 mbedtls_ssl_protocol_version tls_version,
526 size_t cid0_len,
527 size_t cid1_len);
528
Gilles Peskine9099d3f2023-09-18 13:11:50 +0200529#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
530/**
531 * \param[in,out] record The record to prepare.
532 * It must contain the data to MAC at offset
533 * `record->data_offset`, of length
534 * `record->data_length`.
535 * On success, write the MAC immediately
536 * after the data and increment
537 * `record->data_length` accordingly.
538 * \param[in,out] transform_out The out transform, typically prepared by
539 * mbedtls_test_ssl_build_transforms().
540 * Its HMAC context may be used. Other than that
541 * it is treated as an input parameter.
542 *
543 * \return 0 on success, an `MBEDTLS_ERR_xxx` error code
544 * or -1 on error.
545 */
546int mbedtls_test_ssl_prepare_record_mac(mbedtls_record *record,
547 mbedtls_ssl_transform *transform_out);
548#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
549
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800550/*
551 * Populate a session structure for serialization tests.
552 * Choose dummy values, mostly non-0 to distinguish from the init default.
553 */
554int mbedtls_test_ssl_tls12_populate_session(mbedtls_ssl_session *session,
555 int ticket_len,
Ronald Cron7b1921a2023-11-23 12:31:56 +0100556 int endpoint_type,
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800557 const char *crt_file);
558
559#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
560int mbedtls_test_ssl_tls13_populate_session(mbedtls_ssl_session *session,
561 int ticket_len,
562 int endpoint_type);
563#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
564
565/*
566 * Perform data exchanging between \p ssl_1 and \p ssl_2 and check if the
567 * message was sent in the correct number of fragments.
568 *
569 * /p ssl_1 and /p ssl_2 Endpoints represented by mbedtls_ssl_context. Both
570 * of them must be initialized and connected
571 * beforehand.
572 * /p msg_len_1 and /p msg_len_2 specify the size of the message to send.
573 * /p expected_fragments_1 and /p expected_fragments_2 determine in how many
574 * fragments the message should be sent.
575 * expected_fragments is 0: can be used for DTLS testing while the message
576 * size is larger than MFL. In that case the message
577 * cannot be fragmented and sent to the second
578 * endpoint.
579 * This value can be used for negative tests.
580 * expected_fragments is 1: can be used for TLS/DTLS testing while the
581 * message size is below MFL
582 * expected_fragments > 1: can be used for TLS testing while the message
583 * size is larger than MFL
584 *
585 * \retval 0 on success, otherwise error code.
586 */
Yanray Wangb088bfc2023-03-16 12:15:49 +0800587int mbedtls_test_ssl_exchange_data(
588 mbedtls_ssl_context *ssl_1,
589 int msg_len_1, const int expected_fragments_1,
590 mbedtls_ssl_context *ssl_2,
591 int msg_len_2, const int expected_fragments_2);
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800592
593#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
594void mbedtls_test_ssl_perform_handshake(
595 mbedtls_test_handshake_test_options *options);
596#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
597
598#if defined(MBEDTLS_TEST_HOOKS)
599/*
600 * Tweak vector lengths in a TLS 1.3 Certificate message
601 *
602 * \param[in] buf Buffer containing the Certificate message to tweak
603 * \param[in]]out] end End of the buffer to parse
604 * \param tweak Tweak identifier (from 1 to the number of tweaks).
605 * \param[out] expected_result Error code expected from the parsing function
606 * \param[out] args Arguments of the MBEDTLS_SSL_CHK_BUF_READ_PTR call that
607 * is expected to fail. All zeroes if no
608 * MBEDTLS_SSL_CHK_BUF_READ_PTR failure is expected.
609 */
Yanray Wangf56181a2023-03-16 12:21:33 +0800610int mbedtls_test_tweak_tls13_certificate_msg_vector_len(
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800611 unsigned char *buf, unsigned char **end, int tweak,
612 int *expected_result, mbedtls_ssl_chk_buf_ptr_args *args);
613#endif /* MBEDTLS_TEST_HOOKS */
Yanray Wang1db628f2023-02-03 11:01:29 +0800614
Ronald Cron77abfe62024-01-15 11:17:31 +0100615#if defined(MBEDTLS_SSL_SESSION_TICKETS)
616int mbedtls_test_ticket_write(
617 void *p_ticket, const mbedtls_ssl_session *session,
618 unsigned char *start, const unsigned char *end,
619 size_t *tlen, uint32_t *ticket_lifetime);
620
621int mbedtls_test_ticket_parse(void *p_ticket, mbedtls_ssl_session *session,
622 unsigned char *buf, size_t len);
623#endif /* MBEDTLS_SSL_SESSION_TICKETS */
624
Ronald Cron1f6e4e42024-01-26 16:31:33 +0100625#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SRV_C) && \
626 defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS) && \
627 defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
628int mbedtls_test_get_tls13_ticket(
629 mbedtls_test_handshake_test_options *client_options,
630 mbedtls_test_handshake_test_options *server_options,
631 mbedtls_ssl_session *session);
Ronald Cronb9a9b1f2024-02-14 11:28:05 +0100632#endif
Ronald Cron1f6e4e42024-01-26 16:31:33 +0100633
Yanray Wang1db628f2023-02-03 11:01:29 +0800634#define ECJPAKE_TEST_PWD "bla"
635
636#if defined(MBEDTLS_USE_PSA_CRYPTO)
637#define ECJPAKE_TEST_SET_PASSWORD(exp_ret_val) \
638 ret = (use_opaque_arg) ? \
639 mbedtls_ssl_set_hs_ecjpake_password_opaque(&ssl, pwd_slot) : \
640 mbedtls_ssl_set_hs_ecjpake_password(&ssl, pwd_string, pwd_len); \
641 TEST_EQUAL(ret, exp_ret_val)
642#else
643#define ECJPAKE_TEST_SET_PASSWORD(exp_ret_val) \
644 ret = mbedtls_ssl_set_hs_ecjpake_password(&ssl, \
645 pwd_string, pwd_len); \
646 TEST_EQUAL(ret, exp_ret_val)
647#endif /* MBEDTLS_USE_PSA_CRYPTO */
648
Yanray Wang09a6f7e2023-02-03 11:04:38 +0800649#define TEST_AVAILABLE_ECC(tls_id_, group_id_, psa_family_, psa_bits_) \
650 TEST_EQUAL(mbedtls_ssl_get_ecp_group_id_from_tls_id(tls_id_), \
651 group_id_); \
652 TEST_EQUAL(mbedtls_ssl_get_tls_id_from_ecp_group_id(group_id_), \
653 tls_id_); \
654 TEST_EQUAL(mbedtls_ssl_get_psa_curve_info_from_tls_id(tls_id_, \
Przemek Stekielda4fba62023-06-02 14:52:28 +0200655 &psa_type, &psa_bits), PSA_SUCCESS); \
656 TEST_EQUAL(psa_family_, PSA_KEY_TYPE_ECC_GET_FAMILY(psa_type)); \
Yanray Wang09a6f7e2023-02-03 11:04:38 +0800657 TEST_EQUAL(psa_bits_, psa_bits);
658
659#define TEST_UNAVAILABLE_ECC(tls_id_, group_id_, psa_family_, psa_bits_) \
660 TEST_EQUAL(mbedtls_ssl_get_ecp_group_id_from_tls_id(tls_id_), \
661 MBEDTLS_ECP_DP_NONE); \
662 TEST_EQUAL(mbedtls_ssl_get_tls_id_from_ecp_group_id(group_id_), \
663 0); \
664 TEST_EQUAL(mbedtls_ssl_get_psa_curve_info_from_tls_id(tls_id_, \
Przemek Stekielda4fba62023-06-02 14:52:28 +0200665 &psa_type, &psa_bits), \
Yanray Wang09a6f7e2023-02-03 11:04:38 +0800666 PSA_ERROR_NOT_SUPPORTED);
667
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800668#endif /* MBEDTLS_SSL_TLS_C */
669
Yanray Wang47907a42022-10-24 14:42:01 +0800670#endif /* SSL_HELPERS_H */