blob: 72095ea610d8185126f81a663b3a1088dec4336a [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
47typedef struct mbedtls_test_ssl_log_pattern {
48 const char *pattern;
49 size_t counter;
50} mbedtls_test_ssl_log_pattern;
51
52typedef struct mbedtls_test_handshake_test_options {
53 const char *cipher;
54 mbedtls_ssl_protocol_version client_min_version;
55 mbedtls_ssl_protocol_version client_max_version;
56 mbedtls_ssl_protocol_version server_min_version;
57 mbedtls_ssl_protocol_version server_max_version;
58 mbedtls_ssl_protocol_version expected_negotiated_version;
59 int expected_handshake_result;
60 int expected_ciphersuite;
61 int pk_alg;
62 int opaque_alg;
63 int opaque_alg2;
64 int opaque_usage;
65 data_t *psk_str;
66 int dtls;
67 int srv_auth_mode;
68 int serialize;
69 int mfl;
70 int cli_msg_len;
71 int srv_msg_len;
72 int expected_cli_fragments;
73 int expected_srv_fragments;
74 int renegotiate;
75 int legacy_renegotiation;
76 void *srv_log_obj;
77 void *cli_log_obj;
78 void (*srv_log_fun)(void *, int, const char *, int, const char *);
79 void (*cli_log_fun)(void *, int, const char *, int, const char *);
80 int resize_buffers;
81#if defined(MBEDTLS_SSL_CACHE_C)
82 mbedtls_ssl_cache_context *cache;
83#endif
84} mbedtls_test_handshake_test_options;
85
86typedef struct mbedtls_test_ssl_buffer {
87 size_t start;
88 size_t content_length;
89 size_t capacity;
90 unsigned char *buffer;
91} mbedtls_test_ssl_buffer;
92
93/*
94 * Context for a message metadata queue (fifo) that is on top of the ring buffer.
95 */
96typedef struct mbedtls_test_ssl_message_queue {
97 size_t *messages;
98 int pos;
99 int num;
100 int capacity;
101} mbedtls_test_ssl_message_queue;
102
103/*
104 * Context for the I/O callbacks simulating network connection.
105 */
106
107#define MBEDTLS_MOCK_SOCKET_CONNECTED 1
108
109typedef struct mbedtls_test_mock_socket {
110 int status;
111 mbedtls_test_ssl_buffer *input;
112 mbedtls_test_ssl_buffer *output;
113 struct mbedtls_test_mock_socket *peer;
114} mbedtls_test_mock_socket;
115
116/* Errors used in the message socket mocks */
117
118#define MBEDTLS_TEST_ERROR_CONTEXT_ERROR -55
119#define MBEDTLS_TEST_ERROR_SEND_FAILED -66
120#define MBEDTLS_TEST_ERROR_RECV_FAILED -77
121
122/*
123 * Structure used as an addon, or a wrapper, around the mocked sockets.
124 * Contains an input queue, to which the other socket pushes metadata,
125 * and an output queue, to which this one pushes metadata. This context is
126 * considered as an owner of the input queue only, which is initialized and
127 * freed in the respective setup and free calls.
128 */
129typedef struct mbedtls_test_message_socket_context {
130 mbedtls_test_ssl_message_queue *queue_input;
131 mbedtls_test_ssl_message_queue *queue_output;
132 mbedtls_test_mock_socket *socket;
133} mbedtls_test_message_socket_context;
134
135#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
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_test_mock_socket socket;
154 mbedtls_test_ssl_endpoint_certificate cert;
155} mbedtls_test_ssl_endpoint;
156
157#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Yanray Wang47907a42022-10-24 14:42:01 +0800158
Yanray Wang4d07d1c2022-10-27 15:28:16 +0800159/*
160 * This function can be passed to mbedtls to receive output logs from it. In
161 * this case, it will count the instances of a mbedtls_test_ssl_log_pattern
162 * in the received logged messages.
163 */
164void mbedtls_test_ssl_log_analyzer(void *ctx, int level,
165 const char *file, int line,
166 const char *str);
167
168void mbedtls_test_init_handshake_options(
169 mbedtls_test_handshake_test_options *opts);
170
171void mbedtls_test_free_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 *
192 * Puts \p input_len bytes from the \p input buffer into the ring buffer \p buf.
193 *
194 * \p buf must have been initialized and set up by calling
195 * `mbedtls_test_ssl_buffer_init()` and `mbedtls_test_ssl_buffer_setup()`.
196 *
197 * \retval \p input_len, if the data fits.
198 * \retval 0 <= value < \p input_len, if the data does not fit.
199 * \retval -1, if \p buf is NULL, it hasn't been set up or \p input_len is not
200 * zero and \p input is NULL.
201 */
202int mbedtls_test_ssl_buffer_put(mbedtls_test_ssl_buffer *buf,
203 const unsigned char *input, size_t input_len);
204
205/*
206 * Gets \p output_len bytes from the ring buffer \p buf into the
207 * \p output buffer. The output buffer can be NULL, in this case a part of the
208 * ring buffer will be dropped, if the requested length is available.
209 *
210 * \p buf must have been initialized and set up by calling
211 * `mbedtls_test_ssl_buffer_init()` and `mbedtls_test_ssl_buffer_setup()`.
212 *
213 * \retval \p output_len, if the data is available.
214 * \retval 0 <= value < \p output_len, if the data is not available.
215 * \retval -1, if \buf is NULL or it hasn't been set up.
216 */
217int mbedtls_test_ssl_buffer_get(mbedtls_test_ssl_buffer *buf,
218 unsigned char *output, size_t output_len);
219
220/*
221 * Errors used in the message transport mock tests
222 */
223 #define MBEDTLS_TEST_ERROR_ARG_NULL -11
224 #define MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED -44
225
226/*
227 * Setup and free functions for the message metadata queue.
228 *
229 * \p capacity describes the number of message metadata chunks that can be held
230 * within the queue.
231 *
232 * \retval 0, if a metadata queue of a given length can be allocated.
233 * \retval MBEDTLS_ERR_SSL_ALLOC_FAILED, if allocation failed.
234 */
235int mbedtls_test_ssl_message_queue_setup(
236 mbedtls_test_ssl_message_queue *queue, size_t capacity);
237
238void mbedtls_test_ssl_message_queue_free(
239 mbedtls_test_ssl_message_queue *queue);
240
241/*
242 * Push message length information onto the message metadata queue.
243 * This will become the last element to leave it (fifo).
244 *
245 * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null.
246 * \retval MBEDTLS_ERR_SSL_WANT_WRITE, if the queue is full.
247 * \retval \p len, if the push was successful.
248 */
249int mbedtls_test_ssl_message_queue_push_info(
250 mbedtls_test_ssl_message_queue *queue, size_t len);
251
252/*
253 * Pop information about the next message length from the queue. This will be
254 * the oldest inserted message length(fifo). \p msg_len can be null, in which
255 * case the data will be popped from the queue but not copied anywhere.
256 *
257 * \retval MBEDTLS_TEST_ERROR_ARG_NULL, if the queue is null.
258 * \retval MBEDTLS_ERR_SSL_WANT_READ, if the queue is empty.
259 * \retval message length, if the pop was successful, up to the given
260 \p buf_len.
261 */
262int mbedtls_test_ssl_message_queue_pop_info(
263 mbedtls_test_ssl_message_queue *queue, size_t buf_len);
264
265/*
266 * Setup and teardown functions for mock sockets.
267 */
268void mbedtls_mock_socket_init(mbedtls_test_mock_socket *socket);
269
270/*
271 * Closes the socket \p socket.
272 *
273 * \p socket must have been previously initialized by calling
274 * mbedtls_mock_socket_init().
275 *
276 * This function frees all allocated resources and both sockets are aware of the
277 * new connection state.
278 *
279 * That is, this function does not simulate half-open TCP connections and the
280 * phenomenon that when closing a UDP connection the peer is not aware of the
281 * connection having been closed.
282 */
283void mbedtls_test_mock_socket_close(mbedtls_test_mock_socket *socket);
284
285/*
286 * Establishes a connection between \p peer1 and \p peer2.
287 *
288 * \p peer1 and \p peer2 must have been previously initialized by calling
289 * mbedtls_mock_socket_init().
290 *
291 * The capacities of the internal buffers are set to \p bufsize. Setting this to
292 * the correct value allows for simulation of MTU, sanity testing the mock
293 * implementation and mocking TCP connections with lower memory cost.
294 */
295int mbedtls_test_mock_socket_connect(mbedtls_test_mock_socket *peer1,
296 mbedtls_test_mock_socket *peer2,
297 size_t bufsize);
298
299
300/*
301 * Callbacks for simulating blocking I/O over connection-oriented transport.
302 */
303
304int mbedtls_test_mock_tcp_send_b(void *ctx, const unsigned char *buf,
305 size_t len);
306
307int mbedtls_test_mock_tcp_recv_b(void *ctx, unsigned char *buf, size_t len);
308
309/*
310 * Callbacks for simulating non-blocking I/O over connection-oriented transport.
311 */
312
313int mbedtls_test_mock_tcp_send_nb(void *ctx, const unsigned char *buf,
314 size_t len);
315
316int mbedtls_test_mock_tcp_recv_nb(void *ctx, unsigned char *buf, size_t len);
317
318void mbedtls_test_message_socket_init(
319 mbedtls_test_message_socket_context *ctx);
320
321/*
322 * Setup a given message socket context including initialization of
323 * input/output queues to a chosen capacity of messages. Also set the
324 * corresponding mock socket.
325 *
326 * \retval 0, if everything succeeds.
327 * \retval MBEDTLS_ERR_SSL_ALLOC_FAILED, if allocation of a message
328 * queue failed.
329 */
330int mbedtls_test_message_socket_setup(
331 mbedtls_test_ssl_message_queue *queue_input,
332 mbedtls_test_ssl_message_queue *queue_output,
333 size_t queue_capacity, mbedtls_test_mock_socket *socket,
334 mbedtls_test_message_socket_context *ctx);
335
336/*
337 * Close a given message socket context, along with the socket itself. Free the
338 * memory allocated by the input queue.
339 */
340void mbedtls_test_message_socket_close(
341 mbedtls_test_message_socket_context *ctx);
342
343/*
344 * Send one message through a given message socket context.
345 *
346 * \retval \p len, if everything succeeds.
347 * \retval MBEDTLS_TEST_ERROR_CONTEXT_ERROR, if any of the needed context
348 * elements or the context itself is null.
349 * \retval MBEDTLS_TEST_ERROR_SEND_FAILED if
350 * mbedtls_test_mock_tcp_send_b failed.
351 * \retval MBEDTLS_ERR_SSL_WANT_WRITE, if the output queue is full.
352 *
353 * This function will also return any error from
354 * mbedtls_test_ssl_message_queue_push_info.
355 */
356int mbedtls_test_mock_tcp_send_msg(void *ctx, const unsigned char *buf,
357 size_t len);
358
359/*
360 * Receive one message from a given message socket context and return message
361 * length or an error.
362 *
363 * \retval message length, if everything succeeds.
364 * \retval MBEDTLS_TEST_ERROR_CONTEXT_ERROR, if any of the needed context
365 * elements or the context itself is null.
366 * \retval MBEDTLS_TEST_ERROR_RECV_FAILED if
367 * mbedtls_test_mock_tcp_recv_b failed.
368 *
369 * This function will also return any error other than
370 * MBEDTLS_TEST_ERROR_MESSAGE_TRUNCATED from
371 * mbedtls_test_message_queue_peek_info.
372 */
373int mbedtls_test_mock_tcp_recv_msg(void *ctx, unsigned char *buf,
374 size_t buf_len);
375
376#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
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 int opaque_alg, int opaque_alg2,
387 int opaque_usage);
388
389/*
390 * Initializes \p ep structure. It is important to call
391 * `mbedtls_test_ssl_endpoint_free()` after calling this function
392 * even if it fails.
393 *
394 * \p endpoint_type must be set as MBEDTLS_SSL_IS_SERVER or
395 * MBEDTLS_SSL_IS_CLIENT.
396 * \p pk_alg the algorithm to use, currently only MBEDTLS_PK_RSA and
397 * MBEDTLS_PK_ECDSA are supported.
398 * \p dtls_context - in case of DTLS - this is the context handling metadata.
399 * \p input_queue - used only in case of DTLS.
400 * \p output_queue - used only in case of DTLS.
401 *
402 * \retval 0 on success, otherwise error code.
403 */
404int mbedtls_test_ssl_endpoint_init(
405 mbedtls_test_ssl_endpoint *ep, int endpoint_type,
406 mbedtls_test_handshake_test_options *options,
407 mbedtls_test_message_socket_context *dtls_context,
408 mbedtls_test_ssl_message_queue *input_queue,
409 mbedtls_test_ssl_message_queue *output_queue,
410 uint16_t *group_list);
411
412/*
413 * Deinitializes endpoint represented by \p ep.
414 */
415void mbedtls_test_ssl_endpoint_free(
416 mbedtls_test_ssl_endpoint *ep,
417 mbedtls_test_message_socket_context *context);
418
419/*
420 * This function moves ssl handshake from \p ssl to prescribed \p state.
421 * /p second_ssl is used as second endpoint and their sockets have to be
422 * connected before calling this function.
423 *
424 * \retval 0 on success, otherwise error code.
425 */
426int mbedtls_test_move_handshake_to_state(mbedtls_ssl_context *ssl,
427 mbedtls_ssl_context *second_ssl,
428 int state);
429
430#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
431
432#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
433 defined(MBEDTLS_CIPHER_MODE_CBC) && defined(MBEDTLS_AES_C)
434int mbedtls_test_psa_cipher_encrypt_helper(mbedtls_ssl_transform *transform,
435 const unsigned char *iv,
436 size_t iv_len,
437 const unsigned char *input,
438 size_t ilen,
439 unsigned char *output,
440 size_t *olen);
441#endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_CIPHER_MODE_CBC &&
442 MBEDTLS_AES_C */
443
444int mbedtls_test_ssl_build_transforms(mbedtls_ssl_transform *t_in,
445 mbedtls_ssl_transform *t_out,
446 int cipher_type, int hash_id,
447 int etm, int tag_mode,
448 mbedtls_ssl_protocol_version tls_version,
449 size_t cid0_len,
450 size_t cid1_len);
451
452/*
453 * Populate a session structure for serialization tests.
454 * Choose dummy values, mostly non-0 to distinguish from the init default.
455 */
456int mbedtls_test_ssl_tls12_populate_session(mbedtls_ssl_session *session,
457 int ticket_len,
458 const char *crt_file);
459
460#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
461int mbedtls_test_ssl_tls13_populate_session(mbedtls_ssl_session *session,
462 int ticket_len,
463 int endpoint_type);
464#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
465
466/*
467 * Perform data exchanging between \p ssl_1 and \p ssl_2 and check if the
468 * message was sent in the correct number of fragments.
469 *
470 * /p ssl_1 and /p ssl_2 Endpoints represented by mbedtls_ssl_context. Both
471 * of them must be initialized and connected
472 * beforehand.
473 * /p msg_len_1 and /p msg_len_2 specify the size of the message to send.
474 * /p expected_fragments_1 and /p expected_fragments_2 determine in how many
475 * fragments the message should be sent.
476 * expected_fragments is 0: can be used for DTLS testing while the message
477 * size is larger than MFL. In that case the message
478 * cannot be fragmented and sent to the second
479 * endpoint.
480 * This value can be used for negative tests.
481 * expected_fragments is 1: can be used for TLS/DTLS testing while the
482 * message size is below MFL
483 * expected_fragments > 1: can be used for TLS testing while the message
484 * size is larger than MFL
485 *
486 * \retval 0 on success, otherwise error code.
487 */
488int mbedtls_exchange_data(mbedtls_ssl_context *ssl_1,
489 int msg_len_1, const int expected_fragments_1,
490 mbedtls_ssl_context *ssl_2,
491 int msg_len_2, const int expected_fragments_2);
492
493#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
494void mbedtls_test_ssl_perform_handshake(
495 mbedtls_test_handshake_test_options *options);
496#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
497
498#if defined(MBEDTLS_TEST_HOOKS)
499/*
500 * Tweak vector lengths in a TLS 1.3 Certificate message
501 *
502 * \param[in] buf Buffer containing the Certificate message to tweak
503 * \param[in]]out] end End of the buffer to parse
504 * \param tweak Tweak identifier (from 1 to the number of tweaks).
505 * \param[out] expected_result Error code expected from the parsing function
506 * \param[out] args Arguments of the MBEDTLS_SSL_CHK_BUF_READ_PTR call that
507 * is expected to fail. All zeroes if no
508 * MBEDTLS_SSL_CHK_BUF_READ_PTR failure is expected.
509 */
510int tweak_tls13_certificate_msg_vector_len(
511 unsigned char *buf, unsigned char **end, int tweak,
512 int *expected_result, mbedtls_ssl_chk_buf_ptr_args *args);
513#endif /* MBEDTLS_TEST_HOOKS */
514#endif /* MBEDTLS_SSL_TLS_C */
515
Yanray Wang47907a42022-10-24 14:42:01 +0800516#endif /* SSL_HELPERS_H */