Debian Codegraph @

crt/s2n/tests/unit/s2n_self_talk_broken_pipe_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 #include <sys/socket.h>
18 #include <sys/wait.h>
19 #include <time.h>
20 #include <unistd.h>
21
22 #include "api/s2n.h"
23 #include "s2n_test.h"
24 #include "testlib/s2n_testlib.h"
25 #include "tls/s2n_connection.h"
26 #include "tls/s2n_handshake.h"
27
28 #define SUPPORTED_CERTIFICATE_FORMATS (2)
29
30 static const char *certificate_paths[SUPPORTED_CERTIFICATE_FORMATS] = { S2N_RSA_2048_PKCS1_CERT_CHAIN, S2N_RSA_2048_PKCS8_CERT_CHAIN };
31 static const char *private_key_paths[SUPPORTED_CERTIFICATE_FORMATS] = { S2N_RSA_2048_PKCS1_KEY, S2N_RSA_2048_PKCS8_KEY };
32
33 void mock_client(struct s2n_test_io_pair *io_pair)
34 {
35 struct s2n_connection *conn = NULL;
36 struct s2n_config *config = NULL;
37 s2n_blocked_status blocked;
38
39 /* Give the server a chance to listen */
40 sleep(1);
41
42 conn = s2n_connection_new(S2N_CLIENT);
43 config = s2n_config_new();
44 s2n_config_disable_x509_verification(config);
45 EXPECT_OK(s2n_config_set_tls12_security_policy(config));
46 s2n_connection_set_config(conn, config);
47
48 s2n_connection_set_io_pair(conn, io_pair);
49
50 int result = s2n_negotiate(conn, &blocked);
51 if (result < 0) {
52 exit(1);
53 }
54
55 result = s2n_connection_free_handshake(conn);
56 if (result < 0) {
57 exit(1);
58 }
59
60 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__OpenBSD__)
61 /* On FreeBSD shutdown from one end of the socket pair does not give EPIPE. Must use close. */
62 s2n_io_pair_close_one_end(io_pair, S2N_CLIENT);
63 #else
64 /* Close client read fd to mock half closed pipe at server side */
65 s2n_io_pair_shutdown_one_end(io_pair, S2N_CLIENT, SHUT_RD);
66 #endif
67 /* Give server a chance to send data on broken pipe */
68 sleep(2);
69
70 s2n_shutdown(conn, &blocked);
71
72 result = s2n_connection_free(conn);
73 if (result < 0) {
74 exit(1);
75 }
76
77 result = s2n_config_free(config);
78 if (result < 0) {
79 exit(1);
80 }
81
82 /* Give the server a chance to avoid a sigpipe */
83 sleep(1);
84
85 s2n_io_pair_shutdown_one_end(io_pair, S2N_CLIENT, SHUT_WR);
86 s2n_io_pair_close_one_end(io_pair, S2N_CLIENT);
87
88 exit(0);
89 }
90
91 int main(int argc, char **argv)
92 {
93 struct s2n_connection *conn = NULL;
94 struct s2n_config *config = NULL;
95 s2n_blocked_status blocked;
96 int status = 0;
97 char cert_chain_pem[S2N_MAX_TEST_PEM_SIZE];
98 char private_key_pem[S2N_MAX_TEST_PEM_SIZE];
99 char dhparams_pem[S2N_MAX_TEST_PEM_SIZE];
100
101 BEGIN_TEST();
102
103 for (int is_dh_key_exchange = 0; is_dh_key_exchange <= 1; is_dh_key_exchange++) {
104 struct s2n_cert_chain_and_key *chain_and_keys[SUPPORTED_CERTIFICATE_FORMATS];
105
106 /* Create a pipe */
107 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
108 EXPECT_SUCCESS(s2n_io_pair_init(&io_pair));
109
110 /* Create a child process */
111 pid_t pid = fork();
112 if (pid == 0) {
113 /* This is the client process, close the server end of the pipe */
114 EXPECT_SUCCESS(s2n_io_pair_close_one_end(&io_pair, S2N_SERVER));
115
116 /* Write the fragmented hello message */
117 mock_client(&io_pair);
118 }
119
120 /* This is the server process, close the client end of the pipe */
121 EXPECT_SUCCESS(s2n_io_pair_close_one_end(&io_pair, S2N_CLIENT));
122
123 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
124
125 EXPECT_NOT_NULL(config = s2n_config_new());
126 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "20240501"));
127 for (int cert = 0; cert < SUPPORTED_CERTIFICATE_FORMATS; cert++) {
128 EXPECT_SUCCESS(s2n_read_test_pem(certificate_paths[cert], cert_chain_pem, S2N_MAX_TEST_PEM_SIZE));
129 EXPECT_SUCCESS(s2n_read_test_pem(private_key_paths[cert], private_key_pem, S2N_MAX_TEST_PEM_SIZE));
130 EXPECT_NOT_NULL(chain_and_keys[cert] = s2n_cert_chain_and_key_new());
131 EXPECT_SUCCESS(s2n_cert_chain_and_key_load_pem(chain_and_keys[cert], cert_chain_pem, private_key_pem));
132 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_keys[cert]));
133 }
134
135 if (is_dh_key_exchange) {
136 EXPECT_SUCCESS(s2n_read_test_pem(S2N_DEFAULT_TEST_DHPARAMS, dhparams_pem, S2N_MAX_TEST_PEM_SIZE));
137 EXPECT_SUCCESS(s2n_config_add_dhparams(config, dhparams_pem));
138 }
139
140 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
141
142 /* Set up the connection to read from the fd */
143 EXPECT_SUCCESS(s2n_connection_set_io_pair(conn, &io_pair));
144
145 /* Negotiate the handshake. */
146 EXPECT_SUCCESS(s2n_negotiate(conn, &blocked));
147 EXPECT_EQUAL(conn->actual_protocol_version, S2N_TLS12);
148
149 /* Give client a chance to close pipe at the receiving end */
150 sleep(1);
151 char buffer[1];
152 /* Fist flush on half closed pipe should get EPIPE */
153 ssize_t w = s2n_send(conn, buffer, 1, &blocked);
154 EXPECT_EQUAL(w, -1);
155 EXPECT_EQUAL(s2n_errno, S2N_ERR_IO);
156 EXPECT_EQUAL(errno, EPIPE);
157
158 /* Second flush on half closed pipe should not get EPIPE as we write is skipped */
159 w = s2n_shutdown(conn, &blocked);
160 EXPECT_EQUAL(w, -1);
161 EXPECT_EQUAL(s2n_errno, S2N_ERR_IO);
162 EXPECT_EQUAL(errno, 0);
163
164 EXPECT_SUCCESS(s2n_connection_free(conn));
165 for (int cert = 0; cert < SUPPORTED_CERTIFICATE_FORMATS; cert++) {
166 EXPECT_SUCCESS(s2n_cert_chain_and_key_free(chain_and_keys[cert]));
167 }
168 EXPECT_SUCCESS(s2n_config_free(config));
169
170 /* Clean up */
171 EXPECT_EQUAL(waitpid(-1, &status, 0), pid);
172 if (getenv("S2N_VALGRIND") == NULL) {
173 EXPECT_EQUAL(status, 0);
174 }
175 EXPECT_SUCCESS(s2n_io_pair_close_one_end(&io_pair, S2N_SERVER));
176 }
177
178 END_TEST();
179 }