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