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