blob: 572b6cb71e929d13604d325fa18c80a0ce322d33 [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
8 * SPDX-License-Identifier: Apache-2.0
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License"); you may
11 * not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 */
22
23#ifndef SSL_HELPERS_H
24#define SSL_HELPERS_H
25
Yanray Wang4d07d1c2022-10-27 15:28:16 +080026#include "mbedtls/build_info.h"
27
28#include <string.h>
29
Yanray Wang47907a42022-10-24 14:42:01 +080030#include <test/helpers.h>
Yanray Wang4d07d1c2022-10-27 15:28:16 +080031#include <test/macros.h>
32#include <test/random.h>
33#include <test/psa_crypto_helpers.h>
34
35#if defined(MBEDTLS_SSL_TLS_C)
36#include <ssl_misc.h>
37#include <mbedtls/timing.h>
38#include <mbedtls/debug.h>
39#include "hash_info.h"
40
41#include "test/certs.h"
Yanray Wang55a66192022-10-26 09:57:53 +080042
43#if defined(MBEDTLS_SSL_CACHE_C)
44#include "mbedtls/ssl_cache.h"
45#endif
46
Yanray Wang5ba709c2023-02-03 11:07:56 +080047#if defined(MBEDTLS_USE_PSA_CRYPTO)
48#define PSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \
49 psa_to_ssl_errors, \
50 psa_generic_status_to_mbedtls)
51#endif
52
Ronald Cron43263c02023-03-09 16:48:10 +010053#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
54#if defined(MBEDTLS_AES_C)
55#if defined(MBEDTLS_GCM_C)
56#if defined(MBEDTLS_MD_CAN_SHA384)
57#define MBEDTLS_TEST_HAS_TLS1_3_AES_256_GCM_SHA384
58#endif
59#if defined(MBEDTLS_MD_CAN_SHA256)
60#define MBEDTLS_TEST_HAS_TLS1_3_AES_128_GCM_SHA256
61#endif
62#endif /* MBEDTLS_GCM_C */
63#if defined(MBEDTLS_CCM_C) && defined(MBEDTLS_MD_CAN_SHA256)
64#define MBEDTLS_TEST_HAS_TLS1_3_AES_128_CCM_SHA256
65#define MBEDTLS_TEST_HAS_TLS1_3_AES_128_CCM_8_SHA256
66#endif
67#endif /* MBEDTLS_AES_C */
68#if defined(MBEDTLS_CHACHAPOLY_C) && defined(MBEDTLS_MD_CAN_SHA256)
69#define MBEDTLS_TEST_HAS_TLS1_3_CHACHA20_POLY1305_SHA256
70#endif
71
72#if defined(MBEDTLS_TEST_HAS_TLS1_3_AES_256_GCM_SHA384) || \
73 defined(MBEDTLS_TEST_HAS_TLS1_3_AES_128_GCM_SHA256) || \
74 defined(MBEDTLS_TEST_HAS_TLS1_3_AES_128_CCM_SHA256) || \
75 defined(MBEDTLS_TEST_HAS_TLS1_3_AES_128_CCM_8_SHA256) || \
76 defined(MBEDTLS_TEST_HAS_TLS1_3_CHACHA20_POLY1305_SHA256)
77#define MBEDTLS_TEST_AT_LEAST_ONE_TLS1_3_CIPHERSUITE
78#endif
79
80#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
81
Yanray Wang5ba709c2023-02-03 11:07:56 +080082#if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED) || \
83 defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
84 defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
85#define MBEDTLS_CAN_HANDLE_RSA_TEST_KEY
86#endif
87enum {
88#define MBEDTLS_SSL_TLS1_3_LABEL(name, string) \
89 tls13_label_ ## name,
90 MBEDTLS_SSL_TLS1_3_LABEL_LIST
91#undef MBEDTLS_SSL_TLS1_3_LABEL
92};
93
Yanray Wang55a66192022-10-26 09:57:53 +080094typedef struct mbedtls_test_ssl_log_pattern {
95 const char *pattern;
96 size_t counter;
97} mbedtls_test_ssl_log_pattern;
98
99typedef struct mbedtls_test_handshake_test_options {
100 const char *cipher;
101 mbedtls_ssl_protocol_version client_min_version;
102 mbedtls_ssl_protocol_version client_max_version;
103 mbedtls_ssl_protocol_version server_min_version;
104 mbedtls_ssl_protocol_version server_max_version;
105 mbedtls_ssl_protocol_version expected_negotiated_version;
106 int expected_handshake_result;
107 int expected_ciphersuite;
108 int pk_alg;
109 int opaque_alg;
110 int opaque_alg2;
111 int opaque_usage;
112 data_t *psk_str;
113 int dtls;
114 int srv_auth_mode;
115 int serialize;
116 int mfl;
117 int cli_msg_len;
118 int srv_msg_len;
119 int expected_cli_fragments;
120 int expected_srv_fragments;
121 int renegotiate;
122 int legacy_renegotiation;
123 void *srv_log_obj;
124 void *cli_log_obj;
125 void (*srv_log_fun)(void *, int, const char *, int, const char *);
126 void (*cli_log_fun)(void *, int, const char *, int, const char *);
127 int resize_buffers;
128#if defined(MBEDTLS_SSL_CACHE_C)
129 mbedtls_ssl_cache_context *cache;
130#endif
131} mbedtls_test_handshake_test_options;
132
Yanray Wang25b766f2023-03-15 16:39:05 +0800133/*
134 * Buffer structure for custom I/O callbacks.
135 */
Yanray Wang55a66192022-10-26 09:57:53 +0800136typedef struct mbedtls_test_ssl_buffer {
137 size_t start;
138 size_t content_length;
139 size_t capacity;
140 unsigned char *buffer;
141} mbedtls_test_ssl_buffer;
142
143/*
144 * Context for a message metadata queue (fifo) that is on top of the ring buffer.
145 */
146typedef struct mbedtls_test_ssl_message_queue {
147 size_t *messages;
148 int pos;
149 int num;
150 int capacity;
151} mbedtls_test_ssl_message_queue;
152
153/*
154 * Context for the I/O callbacks simulating network connection.
155 */
156
157#define MBEDTLS_MOCK_SOCKET_CONNECTED 1
158
159typedef struct mbedtls_test_mock_socket {
160 int status;
161 mbedtls_test_ssl_buffer *input;
162 mbedtls_test_ssl_buffer *output;
163 struct mbedtls_test_mock_socket *peer;
164} mbedtls_test_mock_socket;
165
166/* Errors used in the message socket mocks */
167
168#define MBEDTLS_TEST_ERROR_CONTEXT_ERROR -55
169#define MBEDTLS_TEST_ERROR_SEND_FAILED -66
170#define MBEDTLS_TEST_ERROR_RECV_FAILED -77
171
172/*
173 * Structure used as an addon, or a wrapper, around the mocked sockets.
174 * Contains an input queue, to which the other socket pushes metadata,
175 * and an output queue, to which this one pushes metadata. This context is
176 * considered as an owner of the input queue only, which is initialized and
177 * freed in the respective setup and free calls.
178 */
179typedef struct mbedtls_test_message_socket_context {
180 mbedtls_test_ssl_message_queue *queue_input;
181 mbedtls_test_ssl_message_queue *queue_output;
182 mbedtls_test_mock_socket *socket;
183} mbedtls_test_message_socket_context;
184
185#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
186
187/*
188 * Structure with endpoint's certificates for SSL communication tests.
189 */
190typedef struct mbedtls_test_ssl_endpoint_certificate {
191 mbedtls_x509_crt *ca_cert;
192 mbedtls_x509_crt *cert;
193 mbedtls_pk_context *pkey;
194} mbedtls_test_ssl_endpoint_certificate;
195
196/*
197 * Endpoint structure for SSL communication tests.
198 */
199typedef struct mbedtls_test_ssl_endpoint {
200 const char *name;
201 mbedtls_ssl_context ssl;
202 mbedtls_ssl_config conf;
203 mbedtls_test_mock_socket socket;
204 mbedtls_test_ssl_endpoint_certificate cert;
205} mbedtls_test_ssl_endpoint;
206
207#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Yanray Wang47907a42022-10-24 14:42:01 +0800208
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800209/*
210 * This function can be passed to mbedtls to receive output logs from it. In
211 * this case, it will count the instances of a mbedtls_test_ssl_log_pattern
212 * in the received logged messages.
213 */
214void mbedtls_test_ssl_log_analyzer(void *ctx, int level,
215 const char *file, int line,
216 const char *str);
217
218void mbedtls_test_init_handshake_options(
219 mbedtls_test_handshake_test_options *opts);
220
221void mbedtls_test_free_handshake_options(
222 mbedtls_test_handshake_test_options *opts);
223
224/*
225 * Initialises \p buf. After calling this function it is safe to call
226 * `mbedtls_test_ssl_buffer_free()` on \p buf.
227 */
228void mbedtls_test_ssl_buffer_init(mbedtls_test_ssl_buffer *buf);
229
230/*
231 * Sets up \p buf. After calling this function it is safe to call
232 * `mbedtls_test_ssl_buffer_put()` and `mbedtls_test_ssl_buffer_get()`
233 * on \p buf.
234 */
235int mbedtls_test_ssl_buffer_setup(mbedtls_test_ssl_buffer *buf,
236 size_t capacity);
237
238void mbedtls_test_ssl_buffer_free(mbedtls_test_ssl_buffer *buf);
239
240/*
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800241 * Puts \p input_len bytes from the \p input buffer into the ring buffer \p buf.
242 *
243 * \p buf must have been initialized and set up by calling
244 * `mbedtls_test_ssl_buffer_init()` and `mbedtls_test_ssl_buffer_setup()`.
245 *
246 * \retval \p input_len, if the data fits.
247 * \retval 0 <= value < \p input_len, if the data does not fit.
248 * \retval -1, if \p buf is NULL, it hasn't been set up or \p input_len is not
249 * zero and \p input is NULL.
250 */
251int mbedtls_test_ssl_buffer_put(mbedtls_test_ssl_buffer *buf,
252 const unsigned char *input, size_t input_len);
253
254/*
255 * Gets \p output_len bytes from the ring buffer \p buf into the
256 * \p output buffer. The output buffer can be NULL, in this case a part of the
257 * ring buffer will be dropped, if the requested length is available.
258 *
259 * \p buf must have been initialized and set up by calling
260 * `mbedtls_test_ssl_buffer_init()` and `mbedtls_test_ssl_buffer_setup()`.
261 *
262 * \retval \p output_len, if the data is available.
263 * \retval 0 <= value < \p output_len, if the data is not available.
264 * \retval -1, if \buf is NULL or it hasn't been set up.
265 */
266int mbedtls_test_ssl_buffer_get(mbedtls_test_ssl_buffer *buf,
267 unsigned char *output, size_t output_len);
268
269/*
270 * Errors used in the message transport mock tests
271 */
272 #define MBEDTLS_TEST_ERROR_ARG_NULL -11
273 #define MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED -44
274
275/*
276 * Setup and free functions for the message metadata queue.
277 *
278 * \p capacity describes the number of message metadata chunks that can be held
279 * within the queue.
280 *
281 * \retval 0, if a metadata queue of a given length can be allocated.
282 * \retval MBEDTLS_ERR_SSL_ALLOC_FAILED, if allocation failed.
283 */
284int mbedtls_test_ssl_message_queue_setup(
285 mbedtls_test_ssl_message_queue *queue, size_t capacity);
286
287void mbedtls_test_ssl_message_queue_free(
288 mbedtls_test_ssl_message_queue *queue);
289
290/*
291 * Push message length information onto the message metadata queue.
292 * This will become the last element to leave it (fifo).
293 *
294 * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null.
295 * \retval MBEDTLS_ERR_SSL_WANT_WRITE, if the queue is full.
296 * \retval \p len, if the push was successful.
297 */
298int mbedtls_test_ssl_message_queue_push_info(
299 mbedtls_test_ssl_message_queue *queue, size_t len);
300
301/*
302 * Pop information about the next message length from the queue. This will be
303 * the oldest inserted message length(fifo). \p msg_len can be null, in which
304 * case the data will be popped from the queue but not copied anywhere.
305 *
306 * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null.
307 * \retval MBEDTLS_ERR_SSL_WANT_READ, if the queue is empty.
308 * \retval message length, if the pop was successful, up to the given
309 \p buf_len.
310 */
311int mbedtls_test_ssl_message_queue_pop_info(
312 mbedtls_test_ssl_message_queue *queue, size_t buf_len);
313
314/*
315 * Setup and teardown functions for mock sockets.
316 */
Yanray Wang5f86a422023-03-15 16:02:29 +0800317void mbedtls_test_mock_socket_init(mbedtls_test_mock_socket *socket);
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800318
319/*
320 * Closes the socket \p socket.
321 *
322 * \p socket must have been previously initialized by calling
Yanray Wang5f86a422023-03-15 16:02:29 +0800323 * mbedtls_test_mock_socket_init().
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800324 *
325 * This function frees all allocated resources and both sockets are aware of the
326 * new connection state.
327 *
328 * That is, this function does not simulate half-open TCP connections and the
329 * phenomenon that when closing a UDP connection the peer is not aware of the
330 * connection having been closed.
331 */
332void mbedtls_test_mock_socket_close(mbedtls_test_mock_socket *socket);
333
334/*
335 * Establishes a connection between \p peer1 and \p peer2.
336 *
337 * \p peer1 and \p peer2 must have been previously initialized by calling
Yanray Wang5f86a422023-03-15 16:02:29 +0800338 * mbedtls_test_mock_socket_init().
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800339 *
340 * The capacities of the internal buffers are set to \p bufsize. Setting this to
341 * the correct value allows for simulation of MTU, sanity testing the mock
342 * implementation and mocking TCP connections with lower memory cost.
343 */
344int mbedtls_test_mock_socket_connect(mbedtls_test_mock_socket *peer1,
345 mbedtls_test_mock_socket *peer2,
346 size_t bufsize);
347
348
349/*
350 * Callbacks for simulating blocking I/O over connection-oriented transport.
351 */
Yanray Wangaf727a22023-03-13 19:22:36 +0800352int mbedtls_test_mock_tcp_send_b(void *ctx,
353 const unsigned char *buf, size_t len);
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800354
355int mbedtls_test_mock_tcp_recv_b(void *ctx, unsigned char *buf, size_t len);
356
357/*
358 * Callbacks for simulating non-blocking I/O over connection-oriented transport.
359 */
Yanray Wangaf727a22023-03-13 19:22:36 +0800360int mbedtls_test_mock_tcp_send_nb(void *ctx,
361 const unsigned char *buf, size_t len);
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800362
363int mbedtls_test_mock_tcp_recv_nb(void *ctx, unsigned char *buf, size_t len);
364
365void mbedtls_test_message_socket_init(
366 mbedtls_test_message_socket_context *ctx);
367
368/*
369 * Setup a given message socket context including initialization of
370 * input/output queues to a chosen capacity of messages. Also set the
371 * corresponding mock socket.
372 *
373 * \retval 0, if everything succeeds.
374 * \retval MBEDTLS_ERR_SSL_ALLOC_FAILED, if allocation of a message
375 * queue failed.
376 */
377int mbedtls_test_message_socket_setup(
378 mbedtls_test_ssl_message_queue *queue_input,
379 mbedtls_test_ssl_message_queue *queue_output,
Yanray Wangd19894f2023-03-16 11:47:39 +0800380 size_t queue_capacity,
381 mbedtls_test_mock_socket *socket,
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800382 mbedtls_test_message_socket_context *ctx);
383
384/*
385 * Close a given message socket context, along with the socket itself. Free the
386 * memory allocated by the input queue.
387 */
388void mbedtls_test_message_socket_close(
389 mbedtls_test_message_socket_context *ctx);
390
391/*
392 * Send one message through a given message socket context.
393 *
394 * \retval \p len, if everything succeeds.
395 * \retval MBEDTLS_TEST_ERROR_CONTEXT_ERROR, if any of the needed context
396 * elements or the context itself is null.
397 * \retval MBEDTLS_TEST_ERROR_SEND_FAILED if
398 * mbedtls_test_mock_tcp_send_b failed.
399 * \retval MBEDTLS_ERR_SSL_WANT_WRITE, if the output queue is full.
400 *
401 * This function will also return any error from
402 * mbedtls_test_ssl_message_queue_push_info.
403 */
Yanray Wangaf727a22023-03-13 19:22:36 +0800404int mbedtls_test_mock_tcp_send_msg(void *ctx,
405 const unsigned char *buf, size_t len);
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800406
407/*
408 * Receive one message from a given message socket context and return message
409 * length or an error.
410 *
411 * \retval message length, if everything succeeds.
412 * \retval MBEDTLS_TEST_ERROR_CONTEXT_ERROR, if any of the needed context
413 * elements or the context itself is null.
414 * \retval MBEDTLS_TEST_ERROR_RECV_FAILED if
415 * mbedtls_test_mock_tcp_recv_b failed.
416 *
417 * This function will also return any error other than
Yanray Wang5e22a922023-03-16 14:57:54 +0800418 * MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED from test_ssl_message_queue_peek_info.
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800419 */
Yanray Wangaf727a22023-03-13 19:22:36 +0800420int mbedtls_test_mock_tcp_recv_msg(void *ctx,
421 unsigned char *buf, size_t buf_len);
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800422
423#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
424
425/*
426 * Initializes \p ep_cert structure and assigns it to endpoint
427 * represented by \p ep.
428 *
429 * \retval 0 on success, otherwise error code.
430 */
431int mbedtls_test_ssl_endpoint_certificate_init(mbedtls_test_ssl_endpoint *ep,
432 int pk_alg,
433 int opaque_alg, int opaque_alg2,
434 int opaque_usage);
435
436/*
437 * Initializes \p ep structure. It is important to call
438 * `mbedtls_test_ssl_endpoint_free()` after calling this function
439 * even if it fails.
440 *
441 * \p endpoint_type must be set as MBEDTLS_SSL_IS_SERVER or
442 * MBEDTLS_SSL_IS_CLIENT.
443 * \p pk_alg the algorithm to use, currently only MBEDTLS_PK_RSA and
444 * MBEDTLS_PK_ECDSA are supported.
445 * \p dtls_context - in case of DTLS - this is the context handling metadata.
446 * \p input_queue - used only in case of DTLS.
447 * \p output_queue - used only in case of DTLS.
448 *
449 * \retval 0 on success, otherwise error code.
450 */
451int mbedtls_test_ssl_endpoint_init(
452 mbedtls_test_ssl_endpoint *ep, int endpoint_type,
453 mbedtls_test_handshake_test_options *options,
454 mbedtls_test_message_socket_context *dtls_context,
455 mbedtls_test_ssl_message_queue *input_queue,
456 mbedtls_test_ssl_message_queue *output_queue,
457 uint16_t *group_list);
458
459/*
460 * Deinitializes endpoint represented by \p ep.
461 */
462void mbedtls_test_ssl_endpoint_free(
463 mbedtls_test_ssl_endpoint *ep,
464 mbedtls_test_message_socket_context *context);
465
466/*
467 * This function moves ssl handshake from \p ssl to prescribed \p state.
468 * /p second_ssl is used as second endpoint and their sockets have to be
469 * connected before calling this function.
470 *
471 * \retval 0 on success, otherwise error code.
472 */
473int mbedtls_test_move_handshake_to_state(mbedtls_ssl_context *ssl,
474 mbedtls_ssl_context *second_ssl,
475 int state);
476
477#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
478
Yanray Wang1fca4de2023-02-06 12:10:48 +0800479/*
480 * Helper function setting up inverse record transformations
481 * using given cipher, hash, EtM mode, authentication tag length,
482 * and version.
483 */
484#define CHK(x) \
485 do \
486 { \
487 if (!(x)) \
488 { \
489 ret = -1; \
490 goto cleanup; \
491 } \
492 } while (0)
493
Yanray Wang25b766f2023-03-15 16:39:05 +0800494#if MBEDTLS_SSL_CID_OUT_LEN_MAX > MBEDTLS_SSL_CID_IN_LEN_MAX
495#define SSL_CID_LEN_MIN MBEDTLS_SSL_CID_IN_LEN_MAX
496#else
497#define SSL_CID_LEN_MIN MBEDTLS_SSL_CID_OUT_LEN_MAX
498#endif
499
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800500#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
501 defined(MBEDTLS_CIPHER_MODE_CBC) && defined(MBEDTLS_AES_C)
502int mbedtls_test_psa_cipher_encrypt_helper(mbedtls_ssl_transform *transform,
503 const unsigned char *iv,
504 size_t iv_len,
505 const unsigned char *input,
506 size_t ilen,
507 unsigned char *output,
508 size_t *olen);
509#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_CIPHER_MODE_CBC &&
510 MBEDTLS_AES_C */
511
512int mbedtls_test_ssl_build_transforms(mbedtls_ssl_transform *t_in,
513 mbedtls_ssl_transform *t_out,
514 int cipher_type, int hash_id,
515 int etm, int tag_mode,
516 mbedtls_ssl_protocol_version tls_version,
517 size_t cid0_len,
518 size_t cid1_len);
519
520/*
521 * Populate a session structure for serialization tests.
522 * Choose dummy values, mostly non-0 to distinguish from the init default.
523 */
524int mbedtls_test_ssl_tls12_populate_session(mbedtls_ssl_session *session,
525 int ticket_len,
526 const char *crt_file);
527
528#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
529int mbedtls_test_ssl_tls13_populate_session(mbedtls_ssl_session *session,
530 int ticket_len,
531 int endpoint_type);
532#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
533
534/*
535 * Perform data exchanging between \p ssl_1 and \p ssl_2 and check if the
536 * message was sent in the correct number of fragments.
537 *
538 * /p ssl_1 and /p ssl_2 Endpoints represented by mbedtls_ssl_context. Both
539 * of them must be initialized and connected
540 * beforehand.
541 * /p msg_len_1 and /p msg_len_2 specify the size of the message to send.
542 * /p expected_fragments_1 and /p expected_fragments_2 determine in how many
543 * fragments the message should be sent.
544 * expected_fragments is 0: can be used for DTLS testing while the message
545 * size is larger than MFL. In that case the message
546 * cannot be fragmented and sent to the second
547 * endpoint.
548 * This value can be used for negative tests.
549 * expected_fragments is 1: can be used for TLS/DTLS testing while the
550 * message size is below MFL
551 * expected_fragments > 1: can be used for TLS testing while the message
552 * size is larger than MFL
553 *
554 * \retval 0 on success, otherwise error code.
555 */
Yanray Wangb088bfc2023-03-16 12:15:49 +0800556int mbedtls_test_ssl_exchange_data(
557 mbedtls_ssl_context *ssl_1,
558 int msg_len_1, const int expected_fragments_1,
559 mbedtls_ssl_context *ssl_2,
560 int msg_len_2, const int expected_fragments_2);
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800561
562#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
563void mbedtls_test_ssl_perform_handshake(
564 mbedtls_test_handshake_test_options *options);
565#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
566
567#if defined(MBEDTLS_TEST_HOOKS)
568/*
569 * Tweak vector lengths in a TLS 1.3 Certificate message
570 *
571 * \param[in] buf Buffer containing the Certificate message to tweak
572 * \param[in]]out] end End of the buffer to parse
573 * \param tweak Tweak identifier (from 1 to the number of tweaks).
574 * \param[out] expected_result Error code expected from the parsing function
575 * \param[out] args Arguments of the MBEDTLS_SSL_CHK_BUF_READ_PTR call that
576 * is expected to fail. All zeroes if no
577 * MBEDTLS_SSL_CHK_BUF_READ_PTR failure is expected.
578 */
Yanray Wangf56181a2023-03-16 12:21:33 +0800579int mbedtls_test_tweak_tls13_certificate_msg_vector_len(
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800580 unsigned char *buf, unsigned char **end, int tweak,
581 int *expected_result, mbedtls_ssl_chk_buf_ptr_args *args);
582#endif /* MBEDTLS_TEST_HOOKS */
Yanray Wang1db628f2023-02-03 11:01:29 +0800583
584#define ECJPAKE_TEST_PWD "bla"
585
586#if defined(MBEDTLS_USE_PSA_CRYPTO)
587#define ECJPAKE_TEST_SET_PASSWORD(exp_ret_val) \
588 ret = (use_opaque_arg) ? \
589 mbedtls_ssl_set_hs_ecjpake_password_opaque(&ssl, pwd_slot) : \
590 mbedtls_ssl_set_hs_ecjpake_password(&ssl, pwd_string, pwd_len); \
591 TEST_EQUAL(ret, exp_ret_val)
592#else
593#define ECJPAKE_TEST_SET_PASSWORD(exp_ret_val) \
594 ret = mbedtls_ssl_set_hs_ecjpake_password(&ssl, \
595 pwd_string, pwd_len); \
596 TEST_EQUAL(ret, exp_ret_val)
597#endif /* MBEDTLS_USE_PSA_CRYPTO */
598
Yanray Wang09a6f7e2023-02-03 11:04:38 +0800599#define TEST_AVAILABLE_ECC(tls_id_, group_id_, psa_family_, psa_bits_) \
600 TEST_EQUAL(mbedtls_ssl_get_ecp_group_id_from_tls_id(tls_id_), \
601 group_id_); \
602 TEST_EQUAL(mbedtls_ssl_get_tls_id_from_ecp_group_id(group_id_), \
603 tls_id_); \
604 TEST_EQUAL(mbedtls_ssl_get_psa_curve_info_from_tls_id(tls_id_, \
605 &psa_family, &psa_bits), PSA_SUCCESS); \
606 TEST_EQUAL(psa_family_, psa_family); \
607 TEST_EQUAL(psa_bits_, psa_bits);
608
609#define TEST_UNAVAILABLE_ECC(tls_id_, group_id_, psa_family_, psa_bits_) \
610 TEST_EQUAL(mbedtls_ssl_get_ecp_group_id_from_tls_id(tls_id_), \
611 MBEDTLS_ECP_DP_NONE); \
612 TEST_EQUAL(mbedtls_ssl_get_tls_id_from_ecp_group_id(group_id_), \
613 0); \
614 TEST_EQUAL(mbedtls_ssl_get_psa_curve_info_from_tls_id(tls_id_, \
615 &psa_family, &psa_bits), \
616 PSA_ERROR_NOT_SUPPORTED);
617
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800618#endif /* MBEDTLS_SSL_TLS_C */
619
Yanray Wang47907a42022-10-24 14:42:01 +0800620#endif /* SSL_HELPERS_H */