blob: e7503c7d59ebc75bb6ca2b81514fefdefe41c9a8 [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
133typedef struct mbedtls_test_ssl_buffer {
134 size_t start;
135 size_t content_length;
136 size_t capacity;
137 unsigned char *buffer;
138} mbedtls_test_ssl_buffer;
139
140/*
141 * Context for a message metadata queue (fifo) that is on top of the ring buffer.
142 */
143typedef struct mbedtls_test_ssl_message_queue {
144 size_t *messages;
145 int pos;
146 int num;
147 int capacity;
148} mbedtls_test_ssl_message_queue;
149
150/*
151 * Context for the I/O callbacks simulating network connection.
152 */
153
154#define MBEDTLS_MOCK_SOCKET_CONNECTED 1
155
156typedef struct mbedtls_test_mock_socket {
157 int status;
158 mbedtls_test_ssl_buffer *input;
159 mbedtls_test_ssl_buffer *output;
160 struct mbedtls_test_mock_socket *peer;
161} mbedtls_test_mock_socket;
162
163/* Errors used in the message socket mocks */
164
165#define MBEDTLS_TEST_ERROR_CONTEXT_ERROR -55
166#define MBEDTLS_TEST_ERROR_SEND_FAILED -66
167#define MBEDTLS_TEST_ERROR_RECV_FAILED -77
168
169/*
170 * Structure used as an addon, or a wrapper, around the mocked sockets.
171 * Contains an input queue, to which the other socket pushes metadata,
172 * and an output queue, to which this one pushes metadata. This context is
173 * considered as an owner of the input queue only, which is initialized and
174 * freed in the respective setup and free calls.
175 */
176typedef struct mbedtls_test_message_socket_context {
177 mbedtls_test_ssl_message_queue *queue_input;
178 mbedtls_test_ssl_message_queue *queue_output;
179 mbedtls_test_mock_socket *socket;
180} mbedtls_test_message_socket_context;
181
182#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
183
184/*
185 * Structure with endpoint's certificates for SSL communication tests.
186 */
187typedef struct mbedtls_test_ssl_endpoint_certificate {
188 mbedtls_x509_crt *ca_cert;
189 mbedtls_x509_crt *cert;
190 mbedtls_pk_context *pkey;
191} mbedtls_test_ssl_endpoint_certificate;
192
193/*
194 * Endpoint structure for SSL communication tests.
195 */
196typedef struct mbedtls_test_ssl_endpoint {
197 const char *name;
198 mbedtls_ssl_context ssl;
199 mbedtls_ssl_config conf;
200 mbedtls_test_mock_socket socket;
201 mbedtls_test_ssl_endpoint_certificate cert;
202} mbedtls_test_ssl_endpoint;
203
204#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Yanray Wang47907a42022-10-24 14:42:01 +0800205
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800206/*
207 * This function can be passed to mbedtls to receive output logs from it. In
208 * this case, it will count the instances of a mbedtls_test_ssl_log_pattern
209 * in the received logged messages.
210 */
211void mbedtls_test_ssl_log_analyzer(void *ctx, int level,
212 const char *file, int line,
213 const char *str);
214
215void mbedtls_test_init_handshake_options(
216 mbedtls_test_handshake_test_options *opts);
217
218void mbedtls_test_free_handshake_options(
219 mbedtls_test_handshake_test_options *opts);
220
221/*
222 * Initialises \p buf. After calling this function it is safe to call
223 * `mbedtls_test_ssl_buffer_free()` on \p buf.
224 */
225void mbedtls_test_ssl_buffer_init(mbedtls_test_ssl_buffer *buf);
226
227/*
228 * Sets up \p buf. After calling this function it is safe to call
229 * `mbedtls_test_ssl_buffer_put()` and `mbedtls_test_ssl_buffer_get()`
230 * on \p buf.
231 */
232int mbedtls_test_ssl_buffer_setup(mbedtls_test_ssl_buffer *buf,
233 size_t capacity);
234
235void mbedtls_test_ssl_buffer_free(mbedtls_test_ssl_buffer *buf);
236
237/*
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800238 * Puts \p input_len bytes from the \p input buffer into the ring buffer \p buf.
239 *
240 * \p buf must have been initialized and set up by calling
241 * `mbedtls_test_ssl_buffer_init()` and `mbedtls_test_ssl_buffer_setup()`.
242 *
243 * \retval \p input_len, if the data fits.
244 * \retval 0 <= value < \p input_len, if the data does not fit.
245 * \retval -1, if \p buf is NULL, it hasn't been set up or \p input_len is not
246 * zero and \p input is NULL.
247 */
248int mbedtls_test_ssl_buffer_put(mbedtls_test_ssl_buffer *buf,
249 const unsigned char *input, size_t input_len);
250
251/*
252 * Gets \p output_len bytes from the ring buffer \p buf into the
253 * \p output buffer. The output buffer can be NULL, in this case a part of the
254 * ring buffer will be dropped, if the requested length is available.
255 *
256 * \p buf must have been initialized and set up by calling
257 * `mbedtls_test_ssl_buffer_init()` and `mbedtls_test_ssl_buffer_setup()`.
258 *
259 * \retval \p output_len, if the data is available.
260 * \retval 0 <= value < \p output_len, if the data is not available.
261 * \retval -1, if \buf is NULL or it hasn't been set up.
262 */
263int mbedtls_test_ssl_buffer_get(mbedtls_test_ssl_buffer *buf,
264 unsigned char *output, size_t output_len);
265
266/*
267 * Errors used in the message transport mock tests
268 */
269 #define MBEDTLS_TEST_ERROR_ARG_NULL -11
270 #define MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED -44
271
272/*
273 * Setup and free functions for the message metadata queue.
274 *
275 * \p capacity describes the number of message metadata chunks that can be held
276 * within the queue.
277 *
278 * \retval 0, if a metadata queue of a given length can be allocated.
279 * \retval MBEDTLS_ERR_SSL_ALLOC_FAILED, if allocation failed.
280 */
281int mbedtls_test_ssl_message_queue_setup(
282 mbedtls_test_ssl_message_queue *queue, size_t capacity);
283
284void mbedtls_test_ssl_message_queue_free(
285 mbedtls_test_ssl_message_queue *queue);
286
287/*
288 * Push message length information onto the message metadata queue.
289 * This will become the last element to leave it (fifo).
290 *
291 * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null.
292 * \retval MBEDTLS_ERR_SSL_WANT_WRITE, if the queue is full.
293 * \retval \p len, if the push was successful.
294 */
295int mbedtls_test_ssl_message_queue_push_info(
296 mbedtls_test_ssl_message_queue *queue, size_t len);
297
298/*
299 * Pop information about the next message length from the queue. This will be
300 * the oldest inserted message length(fifo). \p msg_len can be null, in which
301 * case the data will be popped from the queue but not copied anywhere.
302 *
303 * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null.
304 * \retval MBEDTLS_ERR_SSL_WANT_READ, if the queue is empty.
305 * \retval message length, if the pop was successful, up to the given
306 \p buf_len.
307 */
308int mbedtls_test_ssl_message_queue_pop_info(
309 mbedtls_test_ssl_message_queue *queue, size_t buf_len);
310
311/*
312 * Setup and teardown functions for mock sockets.
313 */
314void mbedtls_mock_socket_init(mbedtls_test_mock_socket *socket);
315
316/*
317 * Closes the socket \p socket.
318 *
319 * \p socket must have been previously initialized by calling
320 * mbedtls_mock_socket_init().
321 *
322 * This function frees all allocated resources and both sockets are aware of the
323 * new connection state.
324 *
325 * That is, this function does not simulate half-open TCP connections and the
326 * phenomenon that when closing a UDP connection the peer is not aware of the
327 * connection having been closed.
328 */
329void mbedtls_test_mock_socket_close(mbedtls_test_mock_socket *socket);
330
331/*
332 * Establishes a connection between \p peer1 and \p peer2.
333 *
334 * \p peer1 and \p peer2 must have been previously initialized by calling
335 * mbedtls_mock_socket_init().
336 *
337 * The capacities of the internal buffers are set to \p bufsize. Setting this to
338 * the correct value allows for simulation of MTU, sanity testing the mock
339 * implementation and mocking TCP connections with lower memory cost.
340 */
341int mbedtls_test_mock_socket_connect(mbedtls_test_mock_socket *peer1,
342 mbedtls_test_mock_socket *peer2,
343 size_t bufsize);
344
345
346/*
347 * Callbacks for simulating blocking I/O over connection-oriented transport.
348 */
Yanray Wangaf727a22023-03-13 19:22:36 +0800349int mbedtls_test_mock_tcp_send_b(void *ctx,
350 const unsigned char *buf, size_t len);
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800351
352int mbedtls_test_mock_tcp_recv_b(void *ctx, unsigned char *buf, size_t len);
353
354/*
355 * Callbacks for simulating non-blocking I/O over connection-oriented transport.
356 */
Yanray Wangaf727a22023-03-13 19:22:36 +0800357int mbedtls_test_mock_tcp_send_nb(void *ctx,
358 const unsigned char *buf, size_t len);
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800359
360int mbedtls_test_mock_tcp_recv_nb(void *ctx, unsigned char *buf, size_t len);
361
362void mbedtls_test_message_socket_init(
363 mbedtls_test_message_socket_context *ctx);
364
365/*
366 * Setup a given message socket context including initialization of
367 * input/output queues to a chosen capacity of messages. Also set the
368 * corresponding mock socket.
369 *
370 * \retval 0, if everything succeeds.
371 * \retval MBEDTLS_ERR_SSL_ALLOC_FAILED, if allocation of a message
372 * queue failed.
373 */
374int mbedtls_test_message_socket_setup(
375 mbedtls_test_ssl_message_queue *queue_input,
376 mbedtls_test_ssl_message_queue *queue_output,
377 size_t queue_capacity, mbedtls_test_mock_socket *socket,
378 mbedtls_test_message_socket_context *ctx);
379
380/*
381 * Close a given message socket context, along with the socket itself. Free the
382 * memory allocated by the input queue.
383 */
384void mbedtls_test_message_socket_close(
385 mbedtls_test_message_socket_context *ctx);
386
387/*
388 * Send one message through a given message socket context.
389 *
390 * \retval \p len, if everything succeeds.
391 * \retval MBEDTLS_TEST_ERROR_CONTEXT_ERROR, if any of the needed context
392 * elements or the context itself is null.
393 * \retval MBEDTLS_TEST_ERROR_SEND_FAILED if
394 * mbedtls_test_mock_tcp_send_b failed.
395 * \retval MBEDTLS_ERR_SSL_WANT_WRITE, if the output queue is full.
396 *
397 * This function will also return any error from
398 * mbedtls_test_ssl_message_queue_push_info.
399 */
Yanray Wangaf727a22023-03-13 19:22:36 +0800400int mbedtls_test_mock_tcp_send_msg(void *ctx,
401 const unsigned char *buf, size_t len);
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800402
403/*
404 * Receive one message from a given message socket context and return message
405 * length or an error.
406 *
407 * \retval message length, if everything succeeds.
408 * \retval MBEDTLS_TEST_ERROR_CONTEXT_ERROR, if any of the needed context
409 * elements or the context itself is null.
410 * \retval MBEDTLS_TEST_ERROR_RECV_FAILED if
411 * mbedtls_test_mock_tcp_recv_b failed.
412 *
413 * This function will also return any error other than
414 * MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED from
415 * mbedtls_test_message_queue_peek_info.
416 */
Yanray Wangaf727a22023-03-13 19:22:36 +0800417int mbedtls_test_mock_tcp_recv_msg(void *ctx,
418 unsigned char *buf, size_t buf_len);
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800419
420#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
421
422/*
423 * Initializes \p ep_cert structure and assigns it to endpoint
424 * represented by \p ep.
425 *
426 * \retval 0 on success, otherwise error code.
427 */
428int mbedtls_test_ssl_endpoint_certificate_init(mbedtls_test_ssl_endpoint *ep,
429 int pk_alg,
430 int opaque_alg, int opaque_alg2,
431 int opaque_usage);
432
433/*
434 * Initializes \p ep structure. It is important to call
435 * `mbedtls_test_ssl_endpoint_free()` after calling this function
436 * even if it fails.
437 *
438 * \p endpoint_type must be set as MBEDTLS_SSL_IS_SERVER or
439 * MBEDTLS_SSL_IS_CLIENT.
440 * \p pk_alg the algorithm to use, currently only MBEDTLS_PK_RSA and
441 * MBEDTLS_PK_ECDSA are supported.
442 * \p dtls_context - in case of DTLS - this is the context handling metadata.
443 * \p input_queue - used only in case of DTLS.
444 * \p output_queue - used only in case of DTLS.
445 *
446 * \retval 0 on success, otherwise error code.
447 */
448int mbedtls_test_ssl_endpoint_init(
449 mbedtls_test_ssl_endpoint *ep, int endpoint_type,
450 mbedtls_test_handshake_test_options *options,
451 mbedtls_test_message_socket_context *dtls_context,
452 mbedtls_test_ssl_message_queue *input_queue,
453 mbedtls_test_ssl_message_queue *output_queue,
454 uint16_t *group_list);
455
456/*
457 * Deinitializes endpoint represented by \p ep.
458 */
459void mbedtls_test_ssl_endpoint_free(
460 mbedtls_test_ssl_endpoint *ep,
461 mbedtls_test_message_socket_context *context);
462
463/*
464 * This function moves ssl handshake from \p ssl to prescribed \p state.
465 * /p second_ssl is used as second endpoint and their sockets have to be
466 * connected before calling this function.
467 *
468 * \retval 0 on success, otherwise error code.
469 */
470int mbedtls_test_move_handshake_to_state(mbedtls_ssl_context *ssl,
471 mbedtls_ssl_context *second_ssl,
472 int state);
473
474#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
475
Yanray Wang1fca4de2023-02-06 12:10:48 +0800476/*
477 * Helper function setting up inverse record transformations
478 * using given cipher, hash, EtM mode, authentication tag length,
479 * and version.
480 */
481#define CHK(x) \
482 do \
483 { \
484 if (!(x)) \
485 { \
486 ret = -1; \
487 goto cleanup; \
488 } \
489 } while (0)
490
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800491#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
492 defined(MBEDTLS_CIPHER_MODE_CBC) && defined(MBEDTLS_AES_C)
493int 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);
500#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_CIPHER_MODE_CBC &&
501 MBEDTLS_AES_C */
502
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
511/*
512 * Populate a session structure for serialization tests.
513 * Choose dummy values, mostly non-0 to distinguish from the init default.
514 */
515int mbedtls_test_ssl_tls12_populate_session(mbedtls_ssl_session *session,
516 int ticket_len,
517 const char *crt_file);
518
519#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
520int mbedtls_test_ssl_tls13_populate_session(mbedtls_ssl_session *session,
521 int ticket_len,
522 int endpoint_type);
523#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
524
525/*
526 * Perform data exchanging between \p ssl_1 and \p ssl_2 and check if the
527 * message was sent in the correct number of fragments.
528 *
529 * /p ssl_1 and /p ssl_2 Endpoints represented by mbedtls_ssl_context. Both
530 * of them must be initialized and connected
531 * beforehand.
532 * /p msg_len_1 and /p msg_len_2 specify the size of the message to send.
533 * /p expected_fragments_1 and /p expected_fragments_2 determine in how many
534 * fragments the message should be sent.
535 * expected_fragments is 0: can be used for DTLS testing while the message
536 * size is larger than MFL. In that case the message
537 * cannot be fragmented and sent to the second
538 * endpoint.
539 * This value can be used for negative tests.
540 * expected_fragments is 1: can be used for TLS/DTLS testing while the
541 * message size is below MFL
542 * expected_fragments > 1: can be used for TLS testing while the message
543 * size is larger than MFL
544 *
545 * \retval 0 on success, otherwise error code.
546 */
547int mbedtls_exchange_data(mbedtls_ssl_context *ssl_1,
548 int msg_len_1, const int expected_fragments_1,
549 mbedtls_ssl_context *ssl_2,
550 int msg_len_2, const int expected_fragments_2);
551
552#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
553void mbedtls_test_ssl_perform_handshake(
554 mbedtls_test_handshake_test_options *options);
555#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
556
557#if defined(MBEDTLS_TEST_HOOKS)
558/*
559 * Tweak vector lengths in a TLS 1.3 Certificate message
560 *
561 * \param[in] buf Buffer containing the Certificate message to tweak
562 * \param[in]]out] end End of the buffer to parse
563 * \param tweak Tweak identifier (from 1 to the number of tweaks).
564 * \param[out] expected_result Error code expected from the parsing function
565 * \param[out] args Arguments of the MBEDTLS_SSL_CHK_BUF_READ_PTR call that
566 * is expected to fail. All zeroes if no
567 * MBEDTLS_SSL_CHK_BUF_READ_PTR failure is expected.
568 */
569int tweak_tls13_certificate_msg_vector_len(
570 unsigned char *buf, unsigned char **end, int tweak,
571 int *expected_result, mbedtls_ssl_chk_buf_ptr_args *args);
572#endif /* MBEDTLS_TEST_HOOKS */
Yanray Wang1db628f2023-02-03 11:01:29 +0800573
574#define ECJPAKE_TEST_PWD "bla"
575
576#if defined(MBEDTLS_USE_PSA_CRYPTO)
577#define ECJPAKE_TEST_SET_PASSWORD(exp_ret_val) \
578 ret = (use_opaque_arg) ? \
579 mbedtls_ssl_set_hs_ecjpake_password_opaque(&ssl, pwd_slot) : \
580 mbedtls_ssl_set_hs_ecjpake_password(&ssl, pwd_string, pwd_len); \
581 TEST_EQUAL(ret, exp_ret_val)
582#else
583#define ECJPAKE_TEST_SET_PASSWORD(exp_ret_val) \
584 ret = mbedtls_ssl_set_hs_ecjpake_password(&ssl, \
585 pwd_string, pwd_len); \
586 TEST_EQUAL(ret, exp_ret_val)
587#endif /* MBEDTLS_USE_PSA_CRYPTO */
588
Yanray Wang09a6f7e2023-02-03 11:04:38 +0800589#define TEST_AVAILABLE_ECC(tls_id_, group_id_, psa_family_, psa_bits_) \
590 TEST_EQUAL(mbedtls_ssl_get_ecp_group_id_from_tls_id(tls_id_), \
591 group_id_); \
592 TEST_EQUAL(mbedtls_ssl_get_tls_id_from_ecp_group_id(group_id_), \
593 tls_id_); \
594 TEST_EQUAL(mbedtls_ssl_get_psa_curve_info_from_tls_id(tls_id_, \
595 &psa_family, &psa_bits), PSA_SUCCESS); \
596 TEST_EQUAL(psa_family_, psa_family); \
597 TEST_EQUAL(psa_bits_, psa_bits);
598
599#define TEST_UNAVAILABLE_ECC(tls_id_, group_id_, psa_family_, psa_bits_) \
600 TEST_EQUAL(mbedtls_ssl_get_ecp_group_id_from_tls_id(tls_id_), \
601 MBEDTLS_ECP_DP_NONE); \
602 TEST_EQUAL(mbedtls_ssl_get_tls_id_from_ecp_group_id(group_id_), \
603 0); \
604 TEST_EQUAL(mbedtls_ssl_get_psa_curve_info_from_tls_id(tls_id_, \
605 &psa_family, &psa_bits), \
606 PSA_ERROR_NOT_SUPPORTED);
607
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800608#endif /* MBEDTLS_SSL_TLS_C */
609
Yanray Wang47907a42022-10-24 14:42:01 +0800610#endif /* SSL_HELPERS_H */