blob: e5b8d74416ab48f81ce54f9eb098ae46a15f12a7 [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)
Elena Uziunaite6121a342024-07-05 11:16:53 +010041#if defined(PSA_WANT_KEY_TYPE_AES)
Elena Uziunaite83a0d9d2024-07-05 11:41:22 +010042#if defined(PSA_WANT_ALG_GCM)
Elena Uziunaiteb476d4b2024-05-23 15:33:41 +010043#if defined(PSA_WANT_ALG_SHA_384)
Ronald Cron43263c02023-03-09 16:48:10 +010044#define MBEDTLS_TEST_HAS_TLS1_3_AES_256_GCM_SHA384
45#endif
Elena Uziunaite0916cd72024-05-23 17:01:07 +010046#if defined(PSA_WANT_ALG_SHA_256)
Ronald Cron43263c02023-03-09 16:48:10 +010047#define MBEDTLS_TEST_HAS_TLS1_3_AES_128_GCM_SHA256
48#endif
Elena Uziunaite83a0d9d2024-07-05 11:41:22 +010049#endif /* PSA_WANT_ALG_GCM */
Elena Uziunaitec2561722024-07-05 11:37:33 +010050#if defined(PSA_WANT_ALG_CCM) && defined(PSA_WANT_ALG_SHA_256)
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
Elena Uziunaite6121a342024-07-05 11:16:53 +010054#endif /* PSA_WANT_KEY_TYPE_AES */
Elena Uziunaite5c70c302024-07-05 11:44:44 +010055#if defined(PSA_WANT_ALG_CHACHA20_POLY1305) && defined(PSA_WANT_ALG_SHA_256)
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
Valerio Setti8438c632025-01-22 11:56:40 +010069#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
Yanray Wang5ba709c2023-02-03 11:07:56 +080070 defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
71#define MBEDTLS_CAN_HANDLE_RSA_TEST_KEY
72#endif
73enum {
74#define MBEDTLS_SSL_TLS1_3_LABEL(name, string) \
75 tls13_label_ ## name,
76 MBEDTLS_SSL_TLS1_3_LABEL_LIST
77#undef MBEDTLS_SSL_TLS1_3_LABEL
78};
79
Waleed Elmelegy4dfb0e72024-03-14 01:48:40 +000080#if defined(MBEDTLS_SSL_ALPN)
81#define MBEDTLS_TEST_MAX_ALPN_LIST_SIZE 10
82#endif
83
Yanray Wang55a66192022-10-26 09:57:53 +080084typedef struct mbedtls_test_ssl_log_pattern {
85 const char *pattern;
86 size_t counter;
87} mbedtls_test_ssl_log_pattern;
88
89typedef struct mbedtls_test_handshake_test_options {
90 const char *cipher;
Ronald Cronfb536472024-01-26 14:55:25 +010091 uint16_t *group_list;
Yanray Wang55a66192022-10-26 09:57:53 +080092 mbedtls_ssl_protocol_version client_min_version;
93 mbedtls_ssl_protocol_version client_max_version;
94 mbedtls_ssl_protocol_version server_min_version;
95 mbedtls_ssl_protocol_version server_max_version;
96 mbedtls_ssl_protocol_version expected_negotiated_version;
97 int expected_handshake_result;
98 int expected_ciphersuite;
99 int pk_alg;
100 int opaque_alg;
101 int opaque_alg2;
102 int opaque_usage;
103 data_t *psk_str;
104 int dtls;
105 int srv_auth_mode;
106 int serialize;
107 int mfl;
108 int cli_msg_len;
109 int srv_msg_len;
110 int expected_cli_fragments;
111 int expected_srv_fragments;
112 int renegotiate;
113 int legacy_renegotiation;
114 void *srv_log_obj;
115 void *cli_log_obj;
116 void (*srv_log_fun)(void *, int, const char *, int, const char *);
117 void (*cli_log_fun)(void *, int, const char *, int, const char *);
118 int resize_buffers;
Ronald Cronced99be2024-01-26 15:49:12 +0100119 int early_data;
Ronald Cron5d3036e2024-02-23 07:43:45 +0100120 int max_early_data_size;
Yanray Wang55a66192022-10-26 09:57:53 +0800121#if defined(MBEDTLS_SSL_CACHE_C)
122 mbedtls_ssl_cache_context *cache;
123#endif
Waleed Elmelegy4dfb0e72024-03-14 01:48:40 +0000124#if defined(MBEDTLS_SSL_ALPN)
125 const char *alpn_list[MBEDTLS_TEST_MAX_ALPN_LIST_SIZE];
126#endif
Yanray Wang55a66192022-10-26 09:57:53 +0800127} mbedtls_test_handshake_test_options;
128
Yanray Wang25b766f2023-03-15 16:39:05 +0800129/*
130 * Buffer structure for custom I/O callbacks.
131 */
Yanray Wang55a66192022-10-26 09:57:53 +0800132typedef struct mbedtls_test_ssl_buffer {
133 size_t start;
134 size_t content_length;
135 size_t capacity;
136 unsigned char *buffer;
137} mbedtls_test_ssl_buffer;
138
139/*
140 * Context for a message metadata queue (fifo) that is on top of the ring buffer.
141 */
142typedef struct mbedtls_test_ssl_message_queue {
143 size_t *messages;
144 int pos;
145 int num;
146 int capacity;
147} mbedtls_test_ssl_message_queue;
148
149/*
150 * Context for the I/O callbacks simulating network connection.
151 */
152
153#define MBEDTLS_MOCK_SOCKET_CONNECTED 1
154
155typedef struct mbedtls_test_mock_socket {
156 int status;
157 mbedtls_test_ssl_buffer *input;
158 mbedtls_test_ssl_buffer *output;
159 struct mbedtls_test_mock_socket *peer;
160} mbedtls_test_mock_socket;
161
162/* Errors used in the message socket mocks */
163
164#define MBEDTLS_TEST_ERROR_CONTEXT_ERROR -55
165#define MBEDTLS_TEST_ERROR_SEND_FAILED -66
166#define MBEDTLS_TEST_ERROR_RECV_FAILED -77
167
168/*
169 * Structure used as an addon, or a wrapper, around the mocked sockets.
170 * Contains an input queue, to which the other socket pushes metadata,
171 * and an output queue, to which this one pushes metadata. This context is
172 * considered as an owner of the input queue only, which is initialized and
173 * freed in the respective setup and free calls.
174 */
175typedef struct mbedtls_test_message_socket_context {
176 mbedtls_test_ssl_message_queue *queue_input;
177 mbedtls_test_ssl_message_queue *queue_output;
178 mbedtls_test_mock_socket *socket;
179} mbedtls_test_message_socket_context;
180
181#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
182
183/*
184 * Structure with endpoint's certificates for SSL communication tests.
185 */
186typedef struct mbedtls_test_ssl_endpoint_certificate {
187 mbedtls_x509_crt *ca_cert;
188 mbedtls_x509_crt *cert;
189 mbedtls_pk_context *pkey;
190} mbedtls_test_ssl_endpoint_certificate;
191
192/*
193 * Endpoint structure for SSL communication tests.
194 */
195typedef struct mbedtls_test_ssl_endpoint {
196 const char *name;
197 mbedtls_ssl_context ssl;
198 mbedtls_ssl_config conf;
199 mbedtls_test_mock_socket socket;
200 mbedtls_test_ssl_endpoint_certificate cert;
201} mbedtls_test_ssl_endpoint;
202
203#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Yanray Wang47907a42022-10-24 14:42:01 +0800204
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800205/*
Ronald Cron10b040f2024-02-05 09:38:09 +0100206 * Random number generator aimed for TLS unitary tests. Its main purpose is to
207 * simplify the set-up of a random number generator for TLS
208 * unitary tests: no need to set up a good entropy source for example.
209 */
210int mbedtls_test_random(void *p_rng, unsigned char *output, size_t output_len);
Yanray Wangf88e5292023-12-01 16:39:34 +0800211
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800212/*
213 * This function can be passed to mbedtls to receive output logs from it. In
214 * this case, it will count the instances of a mbedtls_test_ssl_log_pattern
215 * in the received logged messages.
216 */
217void mbedtls_test_ssl_log_analyzer(void *ctx, int level,
218 const char *file, int line,
219 const char *str);
220
221void mbedtls_test_init_handshake_options(
222 mbedtls_test_handshake_test_options *opts);
223
224void mbedtls_test_free_handshake_options(
225 mbedtls_test_handshake_test_options *opts);
226
227/*
228 * Initialises \p buf. After calling this function it is safe to call
229 * `mbedtls_test_ssl_buffer_free()` on \p buf.
230 */
231void mbedtls_test_ssl_buffer_init(mbedtls_test_ssl_buffer *buf);
232
233/*
234 * Sets up \p buf. After calling this function it is safe to call
235 * `mbedtls_test_ssl_buffer_put()` and `mbedtls_test_ssl_buffer_get()`
236 * on \p buf.
237 */
238int mbedtls_test_ssl_buffer_setup(mbedtls_test_ssl_buffer *buf,
239 size_t capacity);
240
241void mbedtls_test_ssl_buffer_free(mbedtls_test_ssl_buffer *buf);
242
243/*
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800244 * Puts \p input_len bytes from the \p input buffer into the ring buffer \p buf.
245 *
246 * \p buf must have been initialized and set up by calling
247 * `mbedtls_test_ssl_buffer_init()` and `mbedtls_test_ssl_buffer_setup()`.
248 *
249 * \retval \p input_len, if the data fits.
250 * \retval 0 <= value < \p input_len, if the data does not fit.
251 * \retval -1, if \p buf is NULL, it hasn't been set up or \p input_len is not
252 * zero and \p input is NULL.
253 */
254int mbedtls_test_ssl_buffer_put(mbedtls_test_ssl_buffer *buf,
255 const unsigned char *input, size_t input_len);
256
257/*
258 * Gets \p output_len bytes from the ring buffer \p buf into the
259 * \p output buffer. The output buffer can be NULL, in this case a part of the
260 * ring buffer will be dropped, if the requested length is available.
261 *
262 * \p buf must have been initialized and set up by calling
263 * `mbedtls_test_ssl_buffer_init()` and `mbedtls_test_ssl_buffer_setup()`.
264 *
265 * \retval \p output_len, if the data is available.
266 * \retval 0 <= value < \p output_len, if the data is not available.
267 * \retval -1, if \buf is NULL or it hasn't been set up.
268 */
269int mbedtls_test_ssl_buffer_get(mbedtls_test_ssl_buffer *buf,
270 unsigned char *output, size_t output_len);
271
272/*
273 * Errors used in the message transport mock tests
274 */
275 #define MBEDTLS_TEST_ERROR_ARG_NULL -11
276 #define MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED -44
277
278/*
279 * Setup and free functions for the message metadata queue.
280 *
281 * \p capacity describes the number of message metadata chunks that can be held
282 * within the queue.
283 *
284 * \retval 0, if a metadata queue of a given length can be allocated.
285 * \retval MBEDTLS_ERR_SSL_ALLOC_FAILED, if allocation failed.
286 */
287int mbedtls_test_ssl_message_queue_setup(
288 mbedtls_test_ssl_message_queue *queue, size_t capacity);
289
290void mbedtls_test_ssl_message_queue_free(
291 mbedtls_test_ssl_message_queue *queue);
292
293/*
294 * Push message length information onto the message metadata queue.
295 * This will become the last element to leave it (fifo).
296 *
297 * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null.
298 * \retval MBEDTLS_ERR_SSL_WANT_WRITE, if the queue is full.
299 * \retval \p len, if the push was successful.
300 */
301int mbedtls_test_ssl_message_queue_push_info(
302 mbedtls_test_ssl_message_queue *queue, size_t len);
303
304/*
305 * Pop information about the next message length from the queue. This will be
306 * the oldest inserted message length(fifo). \p msg_len can be null, in which
307 * case the data will be popped from the queue but not copied anywhere.
308 *
309 * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null.
310 * \retval MBEDTLS_ERR_SSL_WANT_READ, if the queue is empty.
311 * \retval message length, if the pop was successful, up to the given
312 \p buf_len.
313 */
314int mbedtls_test_ssl_message_queue_pop_info(
315 mbedtls_test_ssl_message_queue *queue, size_t buf_len);
316
317/*
318 * Setup and teardown functions for mock sockets.
319 */
Yanray Wang5f86a422023-03-15 16:02:29 +0800320void mbedtls_test_mock_socket_init(mbedtls_test_mock_socket *socket);
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800321
322/*
323 * Closes the socket \p socket.
324 *
325 * \p socket 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 * This function frees all allocated resources and both sockets are aware of the
329 * new connection state.
330 *
331 * That is, this function does not simulate half-open TCP connections and the
332 * phenomenon that when closing a UDP connection the peer is not aware of the
333 * connection having been closed.
334 */
335void mbedtls_test_mock_socket_close(mbedtls_test_mock_socket *socket);
336
337/*
338 * Establishes a connection between \p peer1 and \p peer2.
339 *
340 * \p peer1 and \p peer2 must have been previously initialized by calling
Yanray Wang5f86a422023-03-15 16:02:29 +0800341 * mbedtls_test_mock_socket_init().
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800342 *
343 * The capacities of the internal buffers are set to \p bufsize. Setting this to
344 * the correct value allows for simulation of MTU, sanity testing the mock
345 * implementation and mocking TCP connections with lower memory cost.
346 */
347int mbedtls_test_mock_socket_connect(mbedtls_test_mock_socket *peer1,
348 mbedtls_test_mock_socket *peer2,
349 size_t bufsize);
350
351
352/*
353 * Callbacks for simulating blocking I/O over connection-oriented transport.
354 */
Yanray Wangaf727a22023-03-13 19:22:36 +0800355int mbedtls_test_mock_tcp_send_b(void *ctx,
356 const unsigned char *buf, size_t len);
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800357
358int mbedtls_test_mock_tcp_recv_b(void *ctx, unsigned char *buf, size_t len);
359
360/*
361 * Callbacks for simulating non-blocking I/O over connection-oriented transport.
362 */
Yanray Wangaf727a22023-03-13 19:22:36 +0800363int mbedtls_test_mock_tcp_send_nb(void *ctx,
364 const unsigned char *buf, size_t len);
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800365
366int mbedtls_test_mock_tcp_recv_nb(void *ctx, unsigned char *buf, size_t len);
367
368void mbedtls_test_message_socket_init(
369 mbedtls_test_message_socket_context *ctx);
370
371/*
372 * Setup a given message socket context including initialization of
373 * input/output queues to a chosen capacity of messages. Also set the
374 * corresponding mock socket.
375 *
376 * \retval 0, if everything succeeds.
377 * \retval MBEDTLS_ERR_SSL_ALLOC_FAILED, if allocation of a message
378 * queue failed.
379 */
380int mbedtls_test_message_socket_setup(
381 mbedtls_test_ssl_message_queue *queue_input,
382 mbedtls_test_ssl_message_queue *queue_output,
Yanray Wangd19894f2023-03-16 11:47:39 +0800383 size_t queue_capacity,
384 mbedtls_test_mock_socket *socket,
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800385 mbedtls_test_message_socket_context *ctx);
386
387/*
388 * Close a given message socket context, along with the socket itself. Free the
389 * memory allocated by the input queue.
390 */
391void mbedtls_test_message_socket_close(
392 mbedtls_test_message_socket_context *ctx);
393
394/*
395 * Send one message through a given message socket context.
396 *
397 * \retval \p len, if everything succeeds.
398 * \retval MBEDTLS_TEST_ERROR_CONTEXT_ERROR, if any of the needed context
399 * elements or the context itself is null.
400 * \retval MBEDTLS_TEST_ERROR_SEND_FAILED if
401 * mbedtls_test_mock_tcp_send_b failed.
402 * \retval MBEDTLS_ERR_SSL_WANT_WRITE, if the output queue is full.
403 *
404 * This function will also return any error from
405 * mbedtls_test_ssl_message_queue_push_info.
406 */
Yanray Wangaf727a22023-03-13 19:22:36 +0800407int mbedtls_test_mock_tcp_send_msg(void *ctx,
408 const unsigned char *buf, size_t len);
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800409
410/*
411 * Receive one message from a given message socket context and return message
412 * length or an error.
413 *
414 * \retval message length, if everything succeeds.
415 * \retval MBEDTLS_TEST_ERROR_CONTEXT_ERROR, if any of the needed context
416 * elements or the context itself is null.
417 * \retval MBEDTLS_TEST_ERROR_RECV_FAILED if
418 * mbedtls_test_mock_tcp_recv_b failed.
419 *
420 * This function will also return any error other than
Yanray Wang5e22a922023-03-16 14:57:54 +0800421 * MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED from test_ssl_message_queue_peek_info.
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800422 */
Yanray Wangaf727a22023-03-13 19:22:36 +0800423int mbedtls_test_mock_tcp_recv_msg(void *ctx,
424 unsigned char *buf, size_t buf_len);
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800425
426#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
427
428/*
429 * Initializes \p ep_cert structure and assigns it to endpoint
430 * represented by \p ep.
431 *
432 * \retval 0 on success, otherwise error code.
433 */
434int mbedtls_test_ssl_endpoint_certificate_init(mbedtls_test_ssl_endpoint *ep,
435 int pk_alg,
436 int opaque_alg, int opaque_alg2,
437 int opaque_usage);
438
439/*
440 * Initializes \p ep structure. It is important to call
441 * `mbedtls_test_ssl_endpoint_free()` after calling this function
442 * even if it fails.
443 *
444 * \p endpoint_type must be set as MBEDTLS_SSL_IS_SERVER or
445 * MBEDTLS_SSL_IS_CLIENT.
446 * \p pk_alg the algorithm to use, currently only MBEDTLS_PK_RSA and
447 * MBEDTLS_PK_ECDSA are supported.
448 * \p dtls_context - in case of DTLS - this is the context handling metadata.
449 * \p input_queue - used only in case of DTLS.
450 * \p output_queue - used only in case of DTLS.
451 *
452 * \retval 0 on success, otherwise error code.
453 */
454int mbedtls_test_ssl_endpoint_init(
455 mbedtls_test_ssl_endpoint *ep, int endpoint_type,
456 mbedtls_test_handshake_test_options *options,
457 mbedtls_test_message_socket_context *dtls_context,
458 mbedtls_test_ssl_message_queue *input_queue,
Ronald Cronfb536472024-01-26 14:55:25 +0100459 mbedtls_test_ssl_message_queue *output_queue);
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800460
461/*
462 * Deinitializes endpoint represented by \p ep.
463 */
464void mbedtls_test_ssl_endpoint_free(
465 mbedtls_test_ssl_endpoint *ep,
466 mbedtls_test_message_socket_context *context);
467
468/*
469 * This function moves ssl handshake from \p ssl to prescribed \p state.
470 * /p second_ssl is used as second endpoint and their sockets have to be
471 * connected before calling this function.
472 *
473 * \retval 0 on success, otherwise error code.
474 */
475int mbedtls_test_move_handshake_to_state(mbedtls_ssl_context *ssl,
476 mbedtls_ssl_context *second_ssl,
477 int state);
478
479#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
480
Yanray Wang1fca4de2023-02-06 12:10:48 +0800481/*
482 * Helper function setting up inverse record transformations
483 * using given cipher, hash, EtM mode, authentication tag length,
484 * and version.
485 */
486#define CHK(x) \
487 do \
488 { \
489 if (!(x)) \
490 { \
491 ret = -1; \
492 goto cleanup; \
493 } \
494 } while (0)
495
Yanray Wang25b766f2023-03-15 16:39:05 +0800496#if MBEDTLS_SSL_CID_OUT_LEN_MAX > MBEDTLS_SSL_CID_IN_LEN_MAX
497#define SSL_CID_LEN_MIN MBEDTLS_SSL_CID_IN_LEN_MAX
498#else
499#define SSL_CID_LEN_MIN MBEDTLS_SSL_CID_OUT_LEN_MAX
500#endif
501
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800502#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
Elena Uziunaite74342c72024-07-05 11:31:29 +0100503 defined(PSA_WANT_ALG_CBC_NO_PADDING) && defined(PSA_WANT_KEY_TYPE_AES)
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800504int mbedtls_test_psa_cipher_encrypt_helper(mbedtls_ssl_transform *transform,
505 const unsigned char *iv,
506 size_t iv_len,
507 const unsigned char *input,
508 size_t ilen,
509 unsigned char *output,
510 size_t *olen);
Elena Uziunaite74342c72024-07-05 11:31:29 +0100511#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && PSA_WANT_ALG_CBC_NO_PADDING &&
Elena Uziunaite6121a342024-07-05 11:16:53 +0100512 PSA_WANT_KEY_TYPE_AES */
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800513
514int mbedtls_test_ssl_build_transforms(mbedtls_ssl_transform *t_in,
515 mbedtls_ssl_transform *t_out,
516 int cipher_type, int hash_id,
517 int etm, int tag_mode,
518 mbedtls_ssl_protocol_version tls_version,
519 size_t cid0_len,
520 size_t cid1_len);
521
Gilles Peskine9099d3f2023-09-18 13:11:50 +0200522#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
523/**
524 * \param[in,out] record The record to prepare.
525 * It must contain the data to MAC at offset
526 * `record->data_offset`, of length
527 * `record->data_length`.
528 * On success, write the MAC immediately
529 * after the data and increment
530 * `record->data_length` accordingly.
531 * \param[in,out] transform_out The out transform, typically prepared by
532 * mbedtls_test_ssl_build_transforms().
533 * Its HMAC context may be used. Other than that
534 * it is treated as an input parameter.
535 *
536 * \return 0 on success, an `MBEDTLS_ERR_xxx` error code
537 * or -1 on error.
538 */
539int mbedtls_test_ssl_prepare_record_mac(mbedtls_record *record,
540 mbedtls_ssl_transform *transform_out);
541#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
542
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800543/*
544 * Populate a session structure for serialization tests.
545 * Choose dummy values, mostly non-0 to distinguish from the init default.
546 */
547int mbedtls_test_ssl_tls12_populate_session(mbedtls_ssl_session *session,
548 int ticket_len,
Ronald Cron7b1921a2023-11-23 12:31:56 +0100549 int endpoint_type,
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800550 const char *crt_file);
551
552#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
553int mbedtls_test_ssl_tls13_populate_session(mbedtls_ssl_session *session,
554 int ticket_len,
555 int endpoint_type);
556#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
557
558/*
559 * Perform data exchanging between \p ssl_1 and \p ssl_2 and check if the
560 * message was sent in the correct number of fragments.
561 *
562 * /p ssl_1 and /p ssl_2 Endpoints represented by mbedtls_ssl_context. Both
563 * of them must be initialized and connected
564 * beforehand.
565 * /p msg_len_1 and /p msg_len_2 specify the size of the message to send.
566 * /p expected_fragments_1 and /p expected_fragments_2 determine in how many
567 * fragments the message should be sent.
568 * expected_fragments is 0: can be used for DTLS testing while the message
569 * size is larger than MFL. In that case the message
570 * cannot be fragmented and sent to the second
571 * endpoint.
572 * This value can be used for negative tests.
573 * expected_fragments is 1: can be used for TLS/DTLS testing while the
574 * message size is below MFL
575 * expected_fragments > 1: can be used for TLS testing while the message
576 * size is larger than MFL
577 *
578 * \retval 0 on success, otherwise error code.
579 */
Yanray Wangb088bfc2023-03-16 12:15:49 +0800580int mbedtls_test_ssl_exchange_data(
581 mbedtls_ssl_context *ssl_1,
582 int msg_len_1, const int expected_fragments_1,
583 mbedtls_ssl_context *ssl_2,
584 int msg_len_2, const int expected_fragments_2);
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800585
586#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
587void mbedtls_test_ssl_perform_handshake(
588 mbedtls_test_handshake_test_options *options);
589#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
590
591#if defined(MBEDTLS_TEST_HOOKS)
592/*
593 * Tweak vector lengths in a TLS 1.3 Certificate message
594 *
595 * \param[in] buf Buffer containing the Certificate message to tweak
596 * \param[in]]out] end End of the buffer to parse
597 * \param tweak Tweak identifier (from 1 to the number of tweaks).
598 * \param[out] expected_result Error code expected from the parsing function
599 * \param[out] args Arguments of the MBEDTLS_SSL_CHK_BUF_READ_PTR call that
600 * is expected to fail. All zeroes if no
601 * MBEDTLS_SSL_CHK_BUF_READ_PTR failure is expected.
602 */
Yanray Wangf56181a2023-03-16 12:21:33 +0800603int mbedtls_test_tweak_tls13_certificate_msg_vector_len(
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800604 unsigned char *buf, unsigned char **end, int tweak,
605 int *expected_result, mbedtls_ssl_chk_buf_ptr_args *args);
606#endif /* MBEDTLS_TEST_HOOKS */
Yanray Wang1db628f2023-02-03 11:01:29 +0800607
Ronald Cron77abfe62024-01-15 11:17:31 +0100608#if defined(MBEDTLS_SSL_SESSION_TICKETS)
609int mbedtls_test_ticket_write(
610 void *p_ticket, const mbedtls_ssl_session *session,
611 unsigned char *start, const unsigned char *end,
612 size_t *tlen, uint32_t *ticket_lifetime);
613
614int mbedtls_test_ticket_parse(void *p_ticket, mbedtls_ssl_session *session,
615 unsigned char *buf, size_t len);
616#endif /* MBEDTLS_SSL_SESSION_TICKETS */
617
Ronald Cron1f6e4e42024-01-26 16:31:33 +0100618#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SRV_C) && \
619 defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS) && \
620 defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
621int mbedtls_test_get_tls13_ticket(
622 mbedtls_test_handshake_test_options *client_options,
623 mbedtls_test_handshake_test_options *server_options,
624 mbedtls_ssl_session *session);
Ronald Cronb9a9b1f2024-02-14 11:28:05 +0100625#endif
Ronald Cron1f6e4e42024-01-26 16:31:33 +0100626
Yanray Wang1db628f2023-02-03 11:01:29 +0800627#define ECJPAKE_TEST_PWD "bla"
628
629#if defined(MBEDTLS_USE_PSA_CRYPTO)
630#define ECJPAKE_TEST_SET_PASSWORD(exp_ret_val) \
631 ret = (use_opaque_arg) ? \
632 mbedtls_ssl_set_hs_ecjpake_password_opaque(&ssl, pwd_slot) : \
633 mbedtls_ssl_set_hs_ecjpake_password(&ssl, pwd_string, pwd_len); \
634 TEST_EQUAL(ret, exp_ret_val)
635#else
636#define ECJPAKE_TEST_SET_PASSWORD(exp_ret_val) \
637 ret = mbedtls_ssl_set_hs_ecjpake_password(&ssl, \
638 pwd_string, pwd_len); \
639 TEST_EQUAL(ret, exp_ret_val)
640#endif /* MBEDTLS_USE_PSA_CRYPTO */
641
Yanray Wang09a6f7e2023-02-03 11:04:38 +0800642#define TEST_AVAILABLE_ECC(tls_id_, group_id_, psa_family_, psa_bits_) \
643 TEST_EQUAL(mbedtls_ssl_get_ecp_group_id_from_tls_id(tls_id_), \
644 group_id_); \
645 TEST_EQUAL(mbedtls_ssl_get_tls_id_from_ecp_group_id(group_id_), \
646 tls_id_); \
647 TEST_EQUAL(mbedtls_ssl_get_psa_curve_info_from_tls_id(tls_id_, \
Przemek Stekielda4fba62023-06-02 14:52:28 +0200648 &psa_type, &psa_bits), PSA_SUCCESS); \
649 TEST_EQUAL(psa_family_, PSA_KEY_TYPE_ECC_GET_FAMILY(psa_type)); \
Yanray Wang09a6f7e2023-02-03 11:04:38 +0800650 TEST_EQUAL(psa_bits_, psa_bits);
651
652#define TEST_UNAVAILABLE_ECC(tls_id_, group_id_, psa_family_, psa_bits_) \
653 TEST_EQUAL(mbedtls_ssl_get_ecp_group_id_from_tls_id(tls_id_), \
654 MBEDTLS_ECP_DP_NONE); \
655 TEST_EQUAL(mbedtls_ssl_get_tls_id_from_ecp_group_id(group_id_), \
656 0); \
657 TEST_EQUAL(mbedtls_ssl_get_psa_curve_info_from_tls_id(tls_id_, \
Przemek Stekielda4fba62023-06-02 14:52:28 +0200658 &psa_type, &psa_bits), \
Yanray Wang09a6f7e2023-02-03 11:04:38 +0800659 PSA_ERROR_NOT_SUPPORTED);
660
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800661#endif /* MBEDTLS_SSL_TLS_C */
662
Yanray Wang47907a42022-10-24 14:42:01 +0800663#endif /* SSL_HELPERS_H */