blob: 9880b4e2324963267b2d830540ebbdda116e5e1f [file] [log] [blame]
Yanray Wang5fce1452022-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 Wang4323e452023-03-14 16:52:06 +080026#include <string.h>
Yanray Wang5fce1452022-10-24 14:42:01 +080027
Yanray Wang4323e452023-03-14 16:52:06 +080028#include <test/helpers.h>
29#include <test/macros.h>
30#include <test/random.h>
31#include <test/psa_crypto_helpers.h>
32
33#if defined(MBEDTLS_SSL_TLS_C)
34#include <mbedtls/ssl_internal.h>
Yanray Wang59ab2762022-10-26 09:57:53 +080035#include <mbedtls/ctr_drbg.h>
36#include <mbedtls/entropy.h>
Yanray Wang4323e452023-03-14 16:52:06 +080037#include <mbedtls/certs.h>
38#include <mbedtls/timing.h>
39#include <mbedtls/debug.h>
40#include <ssl_tls13_keys.h>
41
42#if defined(MBEDTLS_SSL_CACHE_C)
43#include "mbedtls/ssl_cache.h"
44#endif
Yanray Wang59ab2762022-10-26 09:57:53 +080045
46typedef struct mbedtls_test_ssl_log_pattern {
47 const char *pattern;
48 size_t counter;
49} mbedtls_test_ssl_log_pattern;
50
51/* Invalid minor version used when not specifying a min/max version or expecting a test to fail */
52#define TEST_SSL_MINOR_VERSION_NONE -1
53
54typedef struct mbedtls_test_handshake_test_options {
55 const char *cipher;
56 int client_min_version;
57 int client_max_version;
58 int server_min_version;
59 int server_max_version;
60 int expected_negotiated_version;
61 int pk_alg;
62 data_t *psk_str;
63 int dtls;
64 int srv_auth_mode;
65 int serialize;
66 int mfl;
67 int cli_msg_len;
68 int srv_msg_len;
69 int expected_cli_fragments;
70 int expected_srv_fragments;
71 int renegotiate;
72 int legacy_renegotiation;
73 void *srv_log_obj;
74 void *cli_log_obj;
75 void (*srv_log_fun)(void *, int, const char *, int, const char *);
76 void (*cli_log_fun)(void *, int, const char *, int, const char *);
77 int resize_buffers;
78} mbedtls_test_handshake_test_options;
79
80/*
81 * Buffer structure for custom I/O callbacks.
82 */
83typedef struct mbedtls_test_ssl_buffer {
84 size_t start;
85 size_t content_length;
86 size_t capacity;
87 unsigned char *buffer;
88} mbedtls_test_ssl_buffer;
89
90/*
91 * Context for a message metadata queue (fifo) that is on top of the ring buffer.
92 */
93typedef struct mbedtls_test_ssl_message_queue {
94 size_t *messages;
95 int pos;
96 int num;
97 int capacity;
98} mbedtls_test_ssl_message_queue;
99
100/*
101 * Context for the I/O callbacks simulating network connection.
102 */
103
104#define MBEDTLS_MOCK_SOCKET_CONNECTED 1
105
106typedef struct mbedtls_test_mock_socket {
107 int status;
108 mbedtls_test_ssl_buffer *input;
109 mbedtls_test_ssl_buffer *output;
110 struct mbedtls_test_mock_socket *peer;
111} mbedtls_test_mock_socket;
112
113/* Errors used in the message socket mocks */
114
115#define MBEDTLS_TEST_ERROR_CONTEXT_ERROR -55
116#define MBEDTLS_TEST_ERROR_SEND_FAILED -66
117#define MBEDTLS_TEST_ERROR_RECV_FAILED -77
118
119/*
120 * Structure used as an addon, or a wrapper, around the mocked sockets.
121 * Contains an input queue, to which the other socket pushes metadata,
122 * and an output queue, to which this one pushes metadata. This context is
123 * considered as an owner of the input queue only, which is initialized and
124 * freed in the respective setup and free calls.
125 */
126typedef struct mbedtls_test_message_socket_context {
127 mbedtls_test_ssl_message_queue *queue_input;
128 mbedtls_test_ssl_message_queue *queue_output;
129 mbedtls_test_mock_socket *socket;
130} mbedtls_test_message_socket_context;
131
132#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) && \
133 defined(MBEDTLS_CERTS_C) && \
134 defined(MBEDTLS_ENTROPY_C) && \
135 defined(MBEDTLS_CTR_DRBG_C)
136
137/*
138 * Structure with endpoint's certificates for SSL communication tests.
139 */
140typedef struct mbedtls_test_ssl_endpoint_certificate {
141 mbedtls_x509_crt *ca_cert;
142 mbedtls_x509_crt *cert;
143 mbedtls_pk_context *pkey;
144} mbedtls_test_ssl_endpoint_certificate;
145
146/*
147 * Endpoint structure for SSL communication tests.
148 */
149typedef struct mbedtls_test_ssl_endpoint {
150 const char *name;
151 mbedtls_ssl_context ssl;
152 mbedtls_ssl_config conf;
153 mbedtls_ctr_drbg_context ctr_drbg;
154 mbedtls_entropy_context entropy;
155 mbedtls_test_mock_socket socket;
156 mbedtls_test_ssl_endpoint_certificate cert;
157} mbedtls_test_ssl_endpoint;
158
159#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED && MBEDTLS_CERTS_C &&
160 MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */
161
Yanray Wang4323e452023-03-14 16:52:06 +0800162/*
163 * This function can be passed to mbedtls to receive output logs from it. In
164 * this case, it will count the instances of a mbedtls_test_ssl_log_pattern
165 * in the received logged messages.
166 */
167void mbedtls_test_ssl_log_analyzer(void *ctx, int level,
168 const char *file, int line,
169 const char *str);
170
171void mbedtls_test_init_handshake_options(
172 mbedtls_test_handshake_test_options *opts);
173
174/*
175 * Initialises \p buf. After calling this function it is safe to call
176 * `mbedtls_test_ssl_buffer_free()` on \p buf.
177 */
178void mbedtls_test_ssl_buffer_init(mbedtls_test_ssl_buffer *buf);
179
180/*
181 * Sets up \p buf. After calling this function it is safe to call
182 * `mbedtls_test_ssl_buffer_put()` and `mbedtls_test_ssl_buffer_get()`
183 * on \p buf.
184 */
185int mbedtls_test_ssl_buffer_setup(mbedtls_test_ssl_buffer *buf,
186 size_t capacity);
187
188void mbedtls_test_ssl_buffer_free(mbedtls_test_ssl_buffer *buf);
189
190/*
191 * Puts \p input_len bytes from the \p input buffer into the ring buffer \p buf.
192 *
193 * \p buf must have been initialized and set up by calling
194 * `mbedtls_test_ssl_buffer_init()` and `mbedtls_test_ssl_buffer_setup()`.
195 *
196 * \retval \p input_len, if the data fits.
197 * \retval 0 <= value < \p input_len, if the data does not fit.
198 * \retval -1, if \p buf is NULL, it hasn't been set up or \p input_len is not
199 * zero and \p input is NULL.
200 */
201int mbedtls_test_ssl_buffer_put(mbedtls_test_ssl_buffer *buf,
202 const unsigned char *input, size_t input_len);
203
204/*
205 * Gets \p output_len bytes from the ring buffer \p buf into the
206 * \p output buffer. The output buffer can be NULL, in this case a part of the
207 * ring buffer will be dropped, if the requested length is available.
208 *
209 * \p buf must have been initialized and set up by calling
210 * `mbedtls_test_ssl_buffer_init()` and `mbedtls_test_ssl_buffer_setup()`.
211 *
212 * \retval \p output_len, if the data is available.
213 * \retval 0 <= value < \p output_len, if the data is not available.
214 * \retval -1, if \buf is NULL or it hasn't been set up.
215 */
216int mbedtls_test_ssl_buffer_get(mbedtls_test_ssl_buffer *buf,
217 unsigned char *output, size_t output_len);
218
219/*
220 * Errors used in the message transport mock tests
221 */
222 #define MBEDTLS_TEST_ERROR_ARG_NULL -11
223 #define MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED -44
224
225/*
226 * Setup and free functions for the message metadata queue.
227 *
228 * \p capacity describes the number of message metadata chunks that can be held
229 * within the queue.
230 *
231 * \retval 0, if a metadata queue of a given length can be allocated.
232 * \retval MBEDTLS_ERR_SSL_ALLOC_FAILED, if allocation failed.
233 */
234int mbedtls_test_ssl_message_queue_setup(
235 mbedtls_test_ssl_message_queue *queue, size_t capacity);
236
237void mbedtls_test_ssl_message_queue_free(
238 mbedtls_test_ssl_message_queue *queue);
239
240/*
241 * Push message length information onto the message metadata queue.
242 * This will become the last element to leave it (fifo).
243 *
244 * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null.
245 * \retval MBEDTLS_ERR_SSL_WANT_WRITE, if the queue is full.
246 * \retval \p len, if the push was successful.
247 */
248int mbedtls_test_ssl_message_queue_push_info(
249 mbedtls_test_ssl_message_queue *queue, size_t len);
250
251/*
252 * Pop information about the next message length from the queue. This will be
253 * the oldest inserted message length(fifo). \p msg_len can be null, in which
254 * case the data will be popped from the queue but not copied anywhere.
255 *
256 * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null.
257 * \retval MBEDTLS_ERR_SSL_WANT_READ, if the queue is empty.
258 * \retval message length, if the pop was successful, up to the given
259 \p buf_len.
260 */
261int mbedtls_test_ssl_message_queue_pop_info(
262 mbedtls_test_ssl_message_queue *queue, size_t buf_len);
263
264/*
265 * Setup and teardown functions for mock sockets.
266 */
267void mbedtls_mock_socket_init(mbedtls_test_mock_socket *socket);
268
269/*
270 * Closes the socket \p socket.
271 *
272 * \p socket must have been previously initialized by calling
273 * mbedtls_mock_socket_init().
274 *
275 * This function frees all allocated resources and both sockets are aware of the
276 * new connection state.
277 *
278 * That is, this function does not simulate half-open TCP connections and the
279 * phenomenon that when closing a UDP connection the peer is not aware of the
280 * connection having been closed.
281 */
282void mbedtls_test_mock_socket_close(mbedtls_test_mock_socket *socket);
283
284/*
285 * Establishes a connection between \p peer1 and \p peer2.
286 *
287 * \p peer1 and \p peer2 must have been previously initialized by calling
288 * mbedtls_mock_socket_init().
289 *
290 * The capacities of the internal buffers are set to \p bufsize. Setting this to
291 * the correct value allows for simulation of MTU, sanity testing the mock
292 * implementation and mocking TCP connections with lower memory cost.
293 */
294int mbedtls_test_mock_socket_connect(mbedtls_test_mock_socket *peer1,
295 mbedtls_test_mock_socket *peer2,
296 size_t bufsize);
297
298/*
299 * Callbacks for simulating blocking I/O over connection-oriented transport.
300 */
301int mbedtls_test_mock_tcp_send_b(void *ctx,
302 const unsigned char *buf, size_t len);
303
304int mbedtls_test_mock_tcp_recv_b(void *ctx, unsigned char *buf, size_t len);
305
306/*
307 * Callbacks for simulating non-blocking I/O over connection-oriented transport.
308 */
309int mbedtls_test_mock_tcp_send_nb(void *ctx,
310 const unsigned char *buf, size_t len);
311
312int mbedtls_test_mock_tcp_recv_nb(void *ctx, unsigned char *buf, size_t len);
313
314void mbedtls_test_message_socket_init(
315 mbedtls_test_message_socket_context *ctx);
316
317/*
318 * Setup a given message socket context including initialization of
319 * input/output queues to a chosen capacity of messages. Also set the
320 * corresponding mock socket.
321 *
322 * \retval 0, if everything succeeds.
323 * \retval MBEDTLS_ERR_SSL_ALLOC_FAILED, if allocation of a message
324 * queue failed.
325 */
326int mbedtls_test_message_socket_setup(
327 mbedtls_test_ssl_message_queue *queue_input,
328 mbedtls_test_ssl_message_queue *queue_output,
329 size_t queue_capacity,
330 mbedtls_test_mock_socket *socket,
331 mbedtls_test_message_socket_context *ctx);
332
333/*
334 * Close a given message socket context, along with the socket itself. Free the
335 * memory allocated by the input queue.
336 */
337void mbedtls_test_message_socket_close(
338 mbedtls_test_message_socket_context *ctx);
339
340/*
341 * Send one message through a given message socket context.
342 *
343 * \retval \p len, if everything succeeds.
344 * \retval MBEDTLS_TEST_ERROR_CONTEXT_ERROR, if any of the needed context
345 * elements or the context itself is null.
346 * \retval MBEDTLS_TEST_ERROR_SEND_FAILED if
347 * mbedtls_test_mock_tcp_send_b failed.
348 * \retval MBEDTLS_ERR_SSL_WANT_WRITE, if the output queue is full.
349 *
350 * This function will also return any error from
351 * mbedtls_test_ssl_message_queue_push_info.
352 */
353int mbedtls_test_mock_tcp_send_msg(void *ctx,
354 const unsigned char *buf, size_t len);
355
356/*
357 * Receive one message from a given message socket context and return message
358 * length or an error.
359 *
360 * \retval message length, if everything succeeds.
361 * \retval MBEDTLS_TEST_ERROR_CONTEXT_ERROR, if any of the needed context
362 * elements or the context itself is null.
363 * \retval MBEDTLS_TEST_ERROR_RECV_FAILED if
364 * mbedtls_test_mock_tcp_recv_b failed.
365 *
366 * This function will also return any error other than
367 * MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED from
368 * mbedtls_test_message_queue_peek_info.
369 */
370int mbedtls_test_mock_tcp_recv_msg(void *ctx,
371 unsigned char *buf, size_t buf_len);
372
373#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) && \
374 defined(MBEDTLS_CERTS_C) && \
375 defined(MBEDTLS_ENTROPY_C) && \
376 defined(MBEDTLS_CTR_DRBG_C)
377
378/*
379 * Initializes \p ep_cert structure and assigns it to endpoint
380 * represented by \p ep.
381 *
382 * \retval 0 on success, otherwise error code.
383 */
384int mbedtls_test_ssl_endpoint_certificate_init(mbedtls_test_ssl_endpoint *ep,
385 int pk_alg);
386
387/*
388 * Initializes \p ep structure. It is important to call
389 * `mbedtls_test_ssl_endpoint_free()` after calling this function
390 * even if it fails.
391 *
392 * \p endpoint_type must be set as MBEDTLS_SSL_IS_SERVER or
393 * MBEDTLS_SSL_IS_CLIENT.
394 * \p pk_alg the algorithm to use, currently only MBEDTLS_PK_RSA and
395 * MBEDTLS_PK_ECDSA are supported.
396 * \p dtls_context - in case of DTLS - this is the context handling metadata.
397 * \p input_queue - used only in case of DTLS.
398 * \p output_queue - used only in case of DTLS.
399 *
400 * \retval 0 on success, otherwise error code.
401 */
402int mbedtls_test_ssl_endpoint_init(
403 mbedtls_test_ssl_endpoint *ep, int endpoint_type, int pk_alg,
404 mbedtls_test_message_socket_context *dtls_context,
405 mbedtls_test_ssl_message_queue *input_queue,
406 mbedtls_test_ssl_message_queue *output_queue,
407 const mbedtls_ecp_group_id *curves);
408
409/*
410 * Deinitializes endpoint represented by \p ep.
411 */
412void mbedtls_test_ssl_endpoint_free(
413 mbedtls_test_ssl_endpoint *ep,
414 mbedtls_test_message_socket_context *context);
415
416/*
417 * This function moves ssl handshake from \p ssl to prescribed \p state.
418 * /p second_ssl is used as second endpoint and their sockets have to be
419 * connected before calling this function.
420 *
421 * \retval 0 on success, otherwise error code.
422 */
423int mbedtls_test_move_handshake_to_state(mbedtls_ssl_context *ssl,
424 mbedtls_ssl_context *second_ssl,
425 int state);
426
Yanray Wang1ef77c02023-03-14 16:59:00 +0800427#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED && MBEDTLS_CERTS_C &&
428 MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */
429
430/*
431 * Helper function setting up inverse record transformations
432 * using given cipher, hash, EtM mode, authentication tag length,
433 * and version.
434 */
435
436#define CHK(x) \
437 do \
438 { \
439 if (!(x)) \
440 { \
441 ret = -1; \
442 goto cleanup; \
443 } \
444 } while (0)
Yanray Wang4323e452023-03-14 16:52:06 +0800445
446int mbedtls_test_ssl_build_transforms(mbedtls_ssl_transform *t_in,
447 mbedtls_ssl_transform *t_out,
448 int cipher_type, int hash_id,
449 int etm, int tag_mode, int ver,
450 size_t cid0_len,
451 size_t cid1_len);
452
453/*
454 * Populate a session structure for serialization tests.
455 * Choose dummy values, mostly non-0 to distinguish from the init default.
456 */
457int mbedtls_test_ssl_populate_session(mbedtls_ssl_session *session,
458 int ticket_len,
459 const char *crt_file);
460
461/*
462 * Perform data exchanging between \p ssl_1 and \p ssl_2 and check if the
463 * message was sent in the correct number of fragments.
464 *
465 * /p ssl_1 and /p ssl_2 Endpoints represented by mbedtls_ssl_context. Both
466 * of them must be initialized and connected
467 * beforehand.
468 * /p msg_len_1 and /p msg_len_2 specify the size of the message to send.
469 * /p expected_fragments_1 and /p expected_fragments_2 determine in how many
470 * fragments the message should be sent.
471 * expected_fragments is 0: can be used for DTLS testing while the message
472 * size is larger than MFL. In that case the message
473 * cannot be fragmented and sent to the second
474 * endpoint.
475 * This value can be used for negative tests.
476 * expected_fragments is 1: can be used for TLS/DTLS testing while the
477 * message size is below MFL
478 * expected_fragments > 1: can be used for TLS testing while the message
479 * size is larger than MFL
480 *
481 * \retval 0 on success, otherwise error code.
482 */
483int mbedtls_exchange_data(mbedtls_ssl_context *ssl_1,
484 int msg_len_1, const int expected_fragments_1,
485 mbedtls_ssl_context *ssl_2,
486 int msg_len_2, const int expected_fragments_2);
487
488#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED) && \
489 defined(MBEDTLS_CERTS_C) && \
490 defined(MBEDTLS_ENTROPY_C) && \
491 defined(MBEDTLS_CTR_DRBG_C)
492void mbedtls_test_ssl_perform_handshake(
493 mbedtls_test_handshake_test_options *options);
Yanray Wang1ef77c02023-03-14 16:59:00 +0800494#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED && MBEDTLS_CERTS_C &&
495 MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */
Yanray Wang4323e452023-03-14 16:52:06 +0800496#endif /* MBEDTLS_SSL_TLS_C */
497
Yanray Wang5fce1452022-10-24 14:42:01 +0800498#endif /* SSL_HELPERS_H */