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