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