blob: 335386be1c5637e288c2a7c93b83284202535e83 [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/*
Ronald Cron10b040f2024-02-05 09:38:09 +0100200 * Random number generator aimed for TLS unitary tests. Its main purpose is to
201 * simplify the set-up of a random number generator for TLS
202 * unitary tests: no need to set up a good entropy source for example.
203 */
204int mbedtls_test_random(void *p_rng, unsigned char *output, size_t output_len);
Yanray Wangf88e5292023-12-01 16:39:34 +0800205
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800206/*
207 * This function can be passed to mbedtls to receive output logs from it. In
208 * this case, it will count the instances of a mbedtls_test_ssl_log_pattern
209 * in the received logged messages.
210 */
211void mbedtls_test_ssl_log_analyzer(void *ctx, int level,
212 const char *file, int line,
213 const char *str);
214
215void mbedtls_test_init_handshake_options(
216 mbedtls_test_handshake_test_options *opts);
217
218void mbedtls_test_free_handshake_options(
219 mbedtls_test_handshake_test_options *opts);
220
221/*
222 * Initialises \p buf. After calling this function it is safe to call
223 * `mbedtls_test_ssl_buffer_free()` on \p buf.
224 */
225void mbedtls_test_ssl_buffer_init(mbedtls_test_ssl_buffer *buf);
226
227/*
228 * Sets up \p buf. After calling this function it is safe to call
229 * `mbedtls_test_ssl_buffer_put()` and `mbedtls_test_ssl_buffer_get()`
230 * on \p buf.
231 */
232int mbedtls_test_ssl_buffer_setup(mbedtls_test_ssl_buffer *buf,
233 size_t capacity);
234
235void mbedtls_test_ssl_buffer_free(mbedtls_test_ssl_buffer *buf);
236
237/*
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800238 * Puts \p input_len bytes from the \p input buffer into the ring buffer \p buf.
239 *
240 * \p buf must have been initialized and set up by calling
241 * `mbedtls_test_ssl_buffer_init()` and `mbedtls_test_ssl_buffer_setup()`.
242 *
243 * \retval \p input_len, if the data fits.
244 * \retval 0 <= value < \p input_len, if the data does not fit.
245 * \retval -1, if \p buf is NULL, it hasn't been set up or \p input_len is not
246 * zero and \p input is NULL.
247 */
248int mbedtls_test_ssl_buffer_put(mbedtls_test_ssl_buffer *buf,
249 const unsigned char *input, size_t input_len);
250
251/*
252 * Gets \p output_len bytes from the ring buffer \p buf into the
253 * \p output buffer. The output buffer can be NULL, in this case a part of the
254 * ring buffer will be dropped, if the requested length is available.
255 *
256 * \p buf must have been initialized and set up by calling
257 * `mbedtls_test_ssl_buffer_init()` and `mbedtls_test_ssl_buffer_setup()`.
258 *
259 * \retval \p output_len, if the data is available.
260 * \retval 0 <= value < \p output_len, if the data is not available.
261 * \retval -1, if \buf is NULL or it hasn't been set up.
262 */
263int mbedtls_test_ssl_buffer_get(mbedtls_test_ssl_buffer *buf,
264 unsigned char *output, size_t output_len);
265
266/*
267 * Errors used in the message transport mock tests
268 */
269 #define MBEDTLS_TEST_ERROR_ARG_NULL -11
270 #define MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED -44
271
272/*
273 * Setup and free functions for the message metadata queue.
274 *
275 * \p capacity describes the number of message metadata chunks that can be held
276 * within the queue.
277 *
278 * \retval 0, if a metadata queue of a given length can be allocated.
279 * \retval MBEDTLS_ERR_SSL_ALLOC_FAILED, if allocation failed.
280 */
281int mbedtls_test_ssl_message_queue_setup(
282 mbedtls_test_ssl_message_queue *queue, size_t capacity);
283
284void mbedtls_test_ssl_message_queue_free(
285 mbedtls_test_ssl_message_queue *queue);
286
287/*
288 * Push message length information onto the message metadata queue.
289 * This will become the last element to leave it (fifo).
290 *
291 * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null.
292 * \retval MBEDTLS_ERR_SSL_WANT_WRITE, if the queue is full.
293 * \retval \p len, if the push was successful.
294 */
295int mbedtls_test_ssl_message_queue_push_info(
296 mbedtls_test_ssl_message_queue *queue, size_t len);
297
298/*
299 * Pop information about the next message length from the queue. This will be
300 * the oldest inserted message length(fifo). \p msg_len can be null, in which
301 * case the data will be popped from the queue but not copied anywhere.
302 *
303 * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null.
304 * \retval MBEDTLS_ERR_SSL_WANT_READ, if the queue is empty.
305 * \retval message length, if the pop was successful, up to the given
306 \p buf_len.
307 */
308int mbedtls_test_ssl_message_queue_pop_info(
309 mbedtls_test_ssl_message_queue *queue, size_t buf_len);
310
311/*
312 * Setup and teardown functions for mock sockets.
313 */
Yanray Wang5f86a422023-03-15 16:02:29 +0800314void mbedtls_test_mock_socket_init(mbedtls_test_mock_socket *socket);
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800315
316/*
317 * Closes the socket \p socket.
318 *
319 * \p socket must have been previously initialized by calling
Yanray Wang5f86a422023-03-15 16:02:29 +0800320 * mbedtls_test_mock_socket_init().
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800321 *
322 * This function frees all allocated resources and both sockets are aware of the
323 * new connection state.
324 *
325 * That is, this function does not simulate half-open TCP connections and the
326 * phenomenon that when closing a UDP connection the peer is not aware of the
327 * connection having been closed.
328 */
329void mbedtls_test_mock_socket_close(mbedtls_test_mock_socket *socket);
330
331/*
332 * Establishes a connection between \p peer1 and \p peer2.
333 *
334 * \p peer1 and \p peer2 must have been previously initialized by calling
Yanray Wang5f86a422023-03-15 16:02:29 +0800335 * mbedtls_test_mock_socket_init().
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800336 *
337 * The capacities of the internal buffers are set to \p bufsize. Setting this to
338 * the correct value allows for simulation of MTU, sanity testing the mock
339 * implementation and mocking TCP connections with lower memory cost.
340 */
341int mbedtls_test_mock_socket_connect(mbedtls_test_mock_socket *peer1,
342 mbedtls_test_mock_socket *peer2,
343 size_t bufsize);
344
345
346/*
347 * Callbacks for simulating blocking I/O over connection-oriented transport.
348 */
Yanray Wangaf727a22023-03-13 19:22:36 +0800349int mbedtls_test_mock_tcp_send_b(void *ctx,
350 const unsigned char *buf, size_t len);
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800351
352int mbedtls_test_mock_tcp_recv_b(void *ctx, unsigned char *buf, size_t len);
353
354/*
355 * Callbacks for simulating non-blocking I/O over connection-oriented transport.
356 */
Yanray Wangaf727a22023-03-13 19:22:36 +0800357int mbedtls_test_mock_tcp_send_nb(void *ctx,
358 const unsigned char *buf, size_t len);
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800359
360int mbedtls_test_mock_tcp_recv_nb(void *ctx, unsigned char *buf, size_t len);
361
362void mbedtls_test_message_socket_init(
363 mbedtls_test_message_socket_context *ctx);
364
365/*
366 * Setup a given message socket context including initialization of
367 * input/output queues to a chosen capacity of messages. Also set the
368 * corresponding mock socket.
369 *
370 * \retval 0, if everything succeeds.
371 * \retval MBEDTLS_ERR_SSL_ALLOC_FAILED, if allocation of a message
372 * queue failed.
373 */
374int mbedtls_test_message_socket_setup(
375 mbedtls_test_ssl_message_queue *queue_input,
376 mbedtls_test_ssl_message_queue *queue_output,
Yanray Wangd19894f2023-03-16 11:47:39 +0800377 size_t queue_capacity,
378 mbedtls_test_mock_socket *socket,
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800379 mbedtls_test_message_socket_context *ctx);
380
381/*
382 * Close a given message socket context, along with the socket itself. Free the
383 * memory allocated by the input queue.
384 */
385void mbedtls_test_message_socket_close(
386 mbedtls_test_message_socket_context *ctx);
387
388/*
389 * Send one message through a given message socket context.
390 *
391 * \retval \p len, if everything succeeds.
392 * \retval MBEDTLS_TEST_ERROR_CONTEXT_ERROR, if any of the needed context
393 * elements or the context itself is null.
394 * \retval MBEDTLS_TEST_ERROR_SEND_FAILED if
395 * mbedtls_test_mock_tcp_send_b failed.
396 * \retval MBEDTLS_ERR_SSL_WANT_WRITE, if the output queue is full.
397 *
398 * This function will also return any error from
399 * mbedtls_test_ssl_message_queue_push_info.
400 */
Yanray Wangaf727a22023-03-13 19:22:36 +0800401int mbedtls_test_mock_tcp_send_msg(void *ctx,
402 const unsigned char *buf, size_t len);
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800403
404/*
405 * Receive one message from a given message socket context and return message
406 * length or an error.
407 *
408 * \retval message length, if everything succeeds.
409 * \retval MBEDTLS_TEST_ERROR_CONTEXT_ERROR, if any of the needed context
410 * elements or the context itself is null.
411 * \retval MBEDTLS_TEST_ERROR_RECV_FAILED if
412 * mbedtls_test_mock_tcp_recv_b failed.
413 *
414 * This function will also return any error other than
Yanray Wang5e22a922023-03-16 14:57:54 +0800415 * MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED from test_ssl_message_queue_peek_info.
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800416 */
Yanray Wangaf727a22023-03-13 19:22:36 +0800417int mbedtls_test_mock_tcp_recv_msg(void *ctx,
418 unsigned char *buf, size_t buf_len);
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800419
420#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
421
422/*
423 * Initializes \p ep_cert structure and assigns it to endpoint
424 * represented by \p ep.
425 *
426 * \retval 0 on success, otherwise error code.
427 */
428int mbedtls_test_ssl_endpoint_certificate_init(mbedtls_test_ssl_endpoint *ep,
429 int pk_alg,
430 int opaque_alg, int opaque_alg2,
431 int opaque_usage);
432
433/*
434 * Initializes \p ep structure. It is important to call
435 * `mbedtls_test_ssl_endpoint_free()` after calling this function
436 * even if it fails.
437 *
438 * \p endpoint_type must be set as MBEDTLS_SSL_IS_SERVER or
439 * MBEDTLS_SSL_IS_CLIENT.
440 * \p pk_alg the algorithm to use, currently only MBEDTLS_PK_RSA and
441 * MBEDTLS_PK_ECDSA are supported.
442 * \p dtls_context - in case of DTLS - this is the context handling metadata.
443 * \p input_queue - used only in case of DTLS.
444 * \p output_queue - used only in case of DTLS.
445 *
446 * \retval 0 on success, otherwise error code.
447 */
448int mbedtls_test_ssl_endpoint_init(
449 mbedtls_test_ssl_endpoint *ep, int endpoint_type,
450 mbedtls_test_handshake_test_options *options,
451 mbedtls_test_message_socket_context *dtls_context,
452 mbedtls_test_ssl_message_queue *input_queue,
Ronald Cronfb536472024-01-26 14:55:25 +0100453 mbedtls_test_ssl_message_queue *output_queue);
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800454
455/*
456 * Deinitializes endpoint represented by \p ep.
457 */
458void mbedtls_test_ssl_endpoint_free(
459 mbedtls_test_ssl_endpoint *ep,
460 mbedtls_test_message_socket_context *context);
461
462/*
463 * This function moves ssl handshake from \p ssl to prescribed \p state.
464 * /p second_ssl is used as second endpoint and their sockets have to be
465 * connected before calling this function.
466 *
467 * \retval 0 on success, otherwise error code.
468 */
469int mbedtls_test_move_handshake_to_state(mbedtls_ssl_context *ssl,
470 mbedtls_ssl_context *second_ssl,
471 int state);
472
473#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
474
Yanray Wang1fca4de2023-02-06 12:10:48 +0800475/*
476 * Helper function setting up inverse record transformations
477 * using given cipher, hash, EtM mode, authentication tag length,
478 * and version.
479 */
480#define CHK(x) \
481 do \
482 { \
483 if (!(x)) \
484 { \
485 ret = -1; \
486 goto cleanup; \
487 } \
488 } while (0)
489
Yanray Wang25b766f2023-03-15 16:39:05 +0800490#if MBEDTLS_SSL_CID_OUT_LEN_MAX > MBEDTLS_SSL_CID_IN_LEN_MAX
491#define SSL_CID_LEN_MIN MBEDTLS_SSL_CID_IN_LEN_MAX
492#else
493#define SSL_CID_LEN_MIN MBEDTLS_SSL_CID_OUT_LEN_MAX
494#endif
495
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800496#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Pengyu Lvba6825e2023-11-08 12:16:29 +0800497 defined(MBEDTLS_SSL_HAVE_CBC) && defined(MBEDTLS_SSL_HAVE_AES)
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800498int mbedtls_test_psa_cipher_encrypt_helper(mbedtls_ssl_transform *transform,
499 const unsigned char *iv,
500 size_t iv_len,
501 const unsigned char *input,
502 size_t ilen,
503 unsigned char *output,
504 size_t *olen);
Pengyu Lvba6825e2023-11-08 12:16:29 +0800505#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_HAVE_CBC &&
506 MBEDTLS_SSL_HAVE_AES */
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800507
508int mbedtls_test_ssl_build_transforms(mbedtls_ssl_transform *t_in,
509 mbedtls_ssl_transform *t_out,
510 int cipher_type, int hash_id,
511 int etm, int tag_mode,
512 mbedtls_ssl_protocol_version tls_version,
513 size_t cid0_len,
514 size_t cid1_len);
515
Gilles Peskine9099d3f2023-09-18 13:11:50 +0200516#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
517/**
518 * \param[in,out] record The record to prepare.
519 * It must contain the data to MAC at offset
520 * `record->data_offset`, of length
521 * `record->data_length`.
522 * On success, write the MAC immediately
523 * after the data and increment
524 * `record->data_length` accordingly.
525 * \param[in,out] transform_out The out transform, typically prepared by
526 * mbedtls_test_ssl_build_transforms().
527 * Its HMAC context may be used. Other than that
528 * it is treated as an input parameter.
529 *
530 * \return 0 on success, an `MBEDTLS_ERR_xxx` error code
531 * or -1 on error.
532 */
533int mbedtls_test_ssl_prepare_record_mac(mbedtls_record *record,
534 mbedtls_ssl_transform *transform_out);
535#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
536
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800537/*
538 * Populate a session structure for serialization tests.
539 * Choose dummy values, mostly non-0 to distinguish from the init default.
540 */
541int mbedtls_test_ssl_tls12_populate_session(mbedtls_ssl_session *session,
542 int ticket_len,
Ronald Cron7b1921a2023-11-23 12:31:56 +0100543 int endpoint_type,
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800544 const char *crt_file);
545
546#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
547int mbedtls_test_ssl_tls13_populate_session(mbedtls_ssl_session *session,
548 int ticket_len,
549 int endpoint_type);
550#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
551
552/*
553 * Perform data exchanging between \p ssl_1 and \p ssl_2 and check if the
554 * message was sent in the correct number of fragments.
555 *
556 * /p ssl_1 and /p ssl_2 Endpoints represented by mbedtls_ssl_context. Both
557 * of them must be initialized and connected
558 * beforehand.
559 * /p msg_len_1 and /p msg_len_2 specify the size of the message to send.
560 * /p expected_fragments_1 and /p expected_fragments_2 determine in how many
561 * fragments the message should be sent.
562 * expected_fragments is 0: can be used for DTLS testing while the message
563 * size is larger than MFL. In that case the message
564 * cannot be fragmented and sent to the second
565 * endpoint.
566 * This value can be used for negative tests.
567 * expected_fragments is 1: can be used for TLS/DTLS testing while the
568 * message size is below MFL
569 * expected_fragments > 1: can be used for TLS testing while the message
570 * size is larger than MFL
571 *
572 * \retval 0 on success, otherwise error code.
573 */
Yanray Wangb088bfc2023-03-16 12:15:49 +0800574int mbedtls_test_ssl_exchange_data(
575 mbedtls_ssl_context *ssl_1,
576 int msg_len_1, const int expected_fragments_1,
577 mbedtls_ssl_context *ssl_2,
578 int msg_len_2, const int expected_fragments_2);
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800579
580#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
581void mbedtls_test_ssl_perform_handshake(
582 mbedtls_test_handshake_test_options *options);
583#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
584
585#if defined(MBEDTLS_TEST_HOOKS)
586/*
587 * Tweak vector lengths in a TLS 1.3 Certificate message
588 *
589 * \param[in] buf Buffer containing the Certificate message to tweak
590 * \param[in]]out] end End of the buffer to parse
591 * \param tweak Tweak identifier (from 1 to the number of tweaks).
592 * \param[out] expected_result Error code expected from the parsing function
593 * \param[out] args Arguments of the MBEDTLS_SSL_CHK_BUF_READ_PTR call that
594 * is expected to fail. All zeroes if no
595 * MBEDTLS_SSL_CHK_BUF_READ_PTR failure is expected.
596 */
Yanray Wangf56181a2023-03-16 12:21:33 +0800597int mbedtls_test_tweak_tls13_certificate_msg_vector_len(
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800598 unsigned char *buf, unsigned char **end, int tweak,
599 int *expected_result, mbedtls_ssl_chk_buf_ptr_args *args);
600#endif /* MBEDTLS_TEST_HOOKS */
Yanray Wang1db628f2023-02-03 11:01:29 +0800601
Ronald Cron77abfe62024-01-15 11:17:31 +0100602#if defined(MBEDTLS_SSL_SESSION_TICKETS)
603int mbedtls_test_ticket_write(
604 void *p_ticket, const mbedtls_ssl_session *session,
605 unsigned char *start, const unsigned char *end,
606 size_t *tlen, uint32_t *ticket_lifetime);
607
608int mbedtls_test_ticket_parse(void *p_ticket, mbedtls_ssl_session *session,
609 unsigned char *buf, size_t len);
610#endif /* MBEDTLS_SSL_SESSION_TICKETS */
611
Ronald Cron1f6e4e42024-01-26 16:31:33 +0100612#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SRV_C) && \
613 defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS) && \
614 defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
615int mbedtls_test_get_tls13_ticket(
616 mbedtls_test_handshake_test_options *client_options,
617 mbedtls_test_handshake_test_options *server_options,
618 mbedtls_ssl_session *session);
Ronald Cronb9a9b1f2024-02-14 11:28:05 +0100619#endif
Ronald Cron1f6e4e42024-01-26 16:31:33 +0100620
Yanray Wang1db628f2023-02-03 11:01:29 +0800621#define ECJPAKE_TEST_PWD "bla"
622
623#if defined(MBEDTLS_USE_PSA_CRYPTO)
624#define ECJPAKE_TEST_SET_PASSWORD(exp_ret_val) \
625 ret = (use_opaque_arg) ? \
626 mbedtls_ssl_set_hs_ecjpake_password_opaque(&ssl, pwd_slot) : \
627 mbedtls_ssl_set_hs_ecjpake_password(&ssl, pwd_string, pwd_len); \
628 TEST_EQUAL(ret, exp_ret_val)
629#else
630#define ECJPAKE_TEST_SET_PASSWORD(exp_ret_val) \
631 ret = mbedtls_ssl_set_hs_ecjpake_password(&ssl, \
632 pwd_string, pwd_len); \
633 TEST_EQUAL(ret, exp_ret_val)
634#endif /* MBEDTLS_USE_PSA_CRYPTO */
635
Yanray Wang09a6f7e2023-02-03 11:04:38 +0800636#define TEST_AVAILABLE_ECC(tls_id_, group_id_, psa_family_, psa_bits_) \
637 TEST_EQUAL(mbedtls_ssl_get_ecp_group_id_from_tls_id(tls_id_), \
638 group_id_); \
639 TEST_EQUAL(mbedtls_ssl_get_tls_id_from_ecp_group_id(group_id_), \
640 tls_id_); \
641 TEST_EQUAL(mbedtls_ssl_get_psa_curve_info_from_tls_id(tls_id_, \
Przemek Stekielda4fba62023-06-02 14:52:28 +0200642 &psa_type, &psa_bits), PSA_SUCCESS); \
643 TEST_EQUAL(psa_family_, PSA_KEY_TYPE_ECC_GET_FAMILY(psa_type)); \
Yanray Wang09a6f7e2023-02-03 11:04:38 +0800644 TEST_EQUAL(psa_bits_, psa_bits);
645
646#define TEST_UNAVAILABLE_ECC(tls_id_, group_id_, psa_family_, psa_bits_) \
647 TEST_EQUAL(mbedtls_ssl_get_ecp_group_id_from_tls_id(tls_id_), \
648 MBEDTLS_ECP_DP_NONE); \
649 TEST_EQUAL(mbedtls_ssl_get_tls_id_from_ecp_group_id(group_id_), \
650 0); \
651 TEST_EQUAL(mbedtls_ssl_get_psa_curve_info_from_tls_id(tls_id_, \
Przemek Stekielda4fba62023-06-02 14:52:28 +0200652 &psa_type, &psa_bits), \
Yanray Wang09a6f7e2023-02-03 11:04:38 +0800653 PSA_ERROR_NOT_SUPPORTED);
654
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800655#endif /* MBEDTLS_SSL_TLS_C */
656
Yanray Wang47907a42022-10-24 14:42:01 +0800657#endif /* SSL_HELPERS_H */