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