Debian Codegraph @

crt/s2n/tests/unit/s2n_self_talk_custom_io_test.c

raw symbols SCIP
1 /*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License").
5 * You may not use this file except in compliance with the License.
6 * A copy of the License is located at
7 *
8 * http://aws.amazon.com/apache2.0
9 *
10 * or in the "license" file accompanying this file. This file is distributed
11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12 * express or implied. See the License for the specific language governing
13 * permissions and limitations under the License.
14 */
15
16 #include <stdint.h>
17
18 #include "api/s2n.h"
19 #include "s2n_test.h"
20 #include "testlib/s2n_testlib.h"
21 #include "tls/s2n_connection.h"
22
23 int main(int argc, char **argv)
24 {
25 BEGIN_TEST();
26
27 /**
28 * This test verifies that a server using custom I/O callbacks
29 * can successfully negotiate and shutdown a TLS connection, and that
30 * s2n_connection_use_corked_io() correctly fails when custom I/O is set.
31 */
32 {
33 DEFER_CLEANUP(struct s2n_config *server_config = s2n_config_new(),
34 s2n_config_ptr_free);
35 EXPECT_OK(s2n_config_set_tls12_security_policy(server_config));
36 DEFER_CLEANUP(struct s2n_cert_chain_and_key *chain_and_key = s2n_cert_chain_and_key_new(),
37 s2n_cert_chain_and_key_ptr_free);
38 char *cert_chain_pem = NULL;
39 char *private_key_pem = NULL;
40 char *dhparams_pem = NULL;
41 EXPECT_NOT_NULL(cert_chain_pem = malloc(S2N_MAX_TEST_PEM_SIZE));
42 EXPECT_NOT_NULL(private_key_pem = malloc(S2N_MAX_TEST_PEM_SIZE));
43 EXPECT_NOT_NULL(dhparams_pem = malloc(S2N_MAX_TEST_PEM_SIZE));
44 EXPECT_SUCCESS(s2n_read_test_pem(S2N_DEFAULT_TEST_CERT_CHAIN, cert_chain_pem, S2N_MAX_TEST_PEM_SIZE));
45 EXPECT_SUCCESS(s2n_read_test_pem(S2N_DEFAULT_TEST_PRIVATE_KEY, private_key_pem, S2N_MAX_TEST_PEM_SIZE));
46 EXPECT_SUCCESS(s2n_read_test_pem(S2N_DEFAULT_TEST_DHPARAMS, dhparams_pem, S2N_MAX_TEST_PEM_SIZE));
47 EXPECT_SUCCESS(s2n_cert_chain_and_key_load_pem(chain_and_key, cert_chain_pem, private_key_pem));
48 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(server_config, chain_and_key));
49 EXPECT_SUCCESS(s2n_config_add_dhparams(server_config, dhparams_pem));
50
51 DEFER_CLEANUP(struct s2n_config *client_config = s2n_config_new(),
52 s2n_config_ptr_free);
53 EXPECT_OK(s2n_config_set_tls12_security_policy(client_config));
54 EXPECT_SUCCESS(s2n_config_disable_x509_verification(client_config));
55
56 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER),
57 s2n_connection_ptr_free);
58 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, server_config));
59
60 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT),
61 s2n_connection_ptr_free);
62 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, client_config));
63
64 /* Set up the server with custom I/O callbacks (stuffers) */
65 DEFER_CLEANUP(struct s2n_test_io_stuffer_pair io_pair = { 0 }, s2n_io_stuffer_pair_free);
66 EXPECT_OK(s2n_io_stuffer_pair_init(&io_pair));
67 EXPECT_OK(s2n_connections_set_io_stuffer_pair(client_conn, server_conn, &io_pair));
68
69 /* Corked IO should fail because the server is using custom I/O callbacks.
70 * s2n_connection_use_corked_io is not available on Windows.
71 */
72 #ifndef _WIN32
73 EXPECT_FAILURE(s2n_connection_use_corked_io(server_conn));
74 #endif
75
76 /* Negotiate the handshake */
77 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
78
79 /* Shutdown after negotiating */
80 EXPECT_SUCCESS(s2n_shutdown_test_server_and_client(server_conn, client_conn));
81
82 free(cert_chain_pem);
83 free(private_key_pem);
84 free(dhparams_pem);
85 };
86
87 /**
88 * Clients and servers can utilize both custom IO and default IO for their sending and receiving.
89 * This test uses s2n_connection_set_write_fd/s2n_connection_set_read_fd which are not
90 * available on Windows.
91 */
92 #ifndef _WIN32
93 {
94 /* Setup connections */
95 struct s2n_connection *client_conn = NULL;
96 struct s2n_connection *server_conn = NULL;
97 EXPECT_NOT_NULL(client_conn = s2n_connection_new(S2N_CLIENT));
98 EXPECT_NOT_NULL(server_conn = s2n_connection_new(S2N_SERVER));
99
100 /* Setup config */
101 struct s2n_config *config_with_certs = NULL;
102 EXPECT_NOT_NULL(config_with_certs = s2n_config_new());
103 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config_with_certs, "default_tls13"));
104 EXPECT_SUCCESS(s2n_config_set_unsafe_for_testing(config_with_certs));
105 struct s2n_cert_chain_and_key *chain_and_key = NULL;
106 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_and_key, S2N_DEFAULT_ECDSA_TEST_CERT_CHAIN,
107 S2N_DEFAULT_ECDSA_TEST_PRIVATE_KEY));
108 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config_with_certs, chain_and_key));
109
110 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config_with_certs));
111 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config_with_certs));
112
113 /* Create nonblocking pipes */
114 struct s2n_test_io_pair io_pair = { 0 };
115 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
116
117 /* Server writes to fd and client reads from fd */
118 EXPECT_SUCCESS(s2n_connection_set_write_fd(server_conn, io_pair.server));
119 EXPECT_SUCCESS(s2n_connection_set_read_fd(client_conn, io_pair.client));
120
121 DEFER_CLEANUP(struct s2n_stuffer stuffer = { 0 }, s2n_stuffer_free);
122 EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&stuffer, S2N_DEFAULT_RECORD_LENGTH));
123
124 /* Client writes to stuffer and server reads from stuffer */
125 EXPECT_SUCCESS(s2n_connection_set_send_io_stuffer(&stuffer, client_conn));
126 EXPECT_SUCCESS(s2n_connection_set_recv_io_stuffer(&stuffer, server_conn));
127
128 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
129
130 /* Clean-up */
131 EXPECT_SUCCESS(s2n_connection_free(server_conn));
132 EXPECT_SUCCESS(s2n_connection_free(client_conn));
133 EXPECT_SUCCESS(s2n_config_free(config_with_certs));
134 EXPECT_SUCCESS(s2n_io_pair_close(&io_pair));
135 EXPECT_SUCCESS(s2n_cert_chain_and_key_free(chain_and_key));
136 };
137 #endif
138
139 END_TEST();
140 }