Debian Codegraph @

crt/s2n/tests/testlib/s2n_test_server_client.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 "testlib/s2n_testlib.h"
17
18 static S2N_RESULT s2n_validate_negotiate_result(bool success, bool peer_is_done, bool *is_done)
19 {
20 /* If we succeeded, we're done. */
21 if (success) {
22 *is_done = true;
23 return S2N_RESULT_OK;
24 }
25
26 /* If we failed for any error other than 'blocked', propagate the error. */
27 if (s2n_error_get_type(s2n_errno) != S2N_ERR_T_BLOCKED) {
28 return S2N_RESULT_ERROR;
29 }
30
31 if (s2n_errno == S2N_ERR_ASYNC_BLOCKED) {
32 return S2N_RESULT_ERROR;
33 }
34
35 /* If we're blocked but our peer is done writing, propagate the error. */
36 if (peer_is_done) {
37 return S2N_RESULT_ERROR;
38 }
39
40 *is_done = false;
41 return S2N_RESULT_OK;
42 }
43
44 int s2n_negotiate_test_server_and_client(struct s2n_connection *server_conn, struct s2n_connection *client_conn)
45 {
46 bool server_done = false, client_done = false;
47 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
48
49 do {
50 bool rc = (s2n_negotiate(client_conn, &blocked) >= S2N_SUCCESS);
51 POSIX_GUARD_RESULT(s2n_validate_negotiate_result(rc, server_done, &client_done));
52
53 rc = (s2n_negotiate(server_conn, &blocked) >= S2N_SUCCESS);
54 POSIX_GUARD_RESULT(s2n_validate_negotiate_result(rc, client_done, &server_done));
55 } while (!client_done || !server_done);
56
57 return S2N_SUCCESS;
58 }
59
60 S2N_RESULT s2n_negotiate_test_server_and_client_with_early_data(struct s2n_connection *server_conn,
61 struct s2n_connection *client_conn, struct s2n_blob *early_data_to_send, struct s2n_blob *early_data_received)
62 {
63 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
64 ssize_t total_data_sent = 0, total_data_recv = 0;
65 ssize_t data_sent = 0, data_recv = 0;
66
67 /* We call s2n_send_early_data and s2n_recv_early_data to handle the early data before
68 * calling s2n_negotiate to complete the handshake.
69 *
70 * s2n_recv_early_data does not indicate success until it receives the EndOfEarlyData message,
71 * indicating that the client is done sending early data. However, the client does not send the
72 * EndOfEarlyData message until s2n_negotiate is called. Instead, we should consider the
73 * server done when it has either read all early data (which may be zero bytes) or has
74 * rejected the early data.
75 */
76 bool server_done = false, client_done = false;
77 bool server_early_done = false, client_early_done = false;
78 do {
79 if (!client_early_done) {
80 /* Adding 0 to a NULL value is undefined behavior */
81 uint8_t *next_early_data_to_send = early_data_to_send->data;
82 if (total_data_sent != 0) {
83 next_early_data_to_send += total_data_sent;
84 }
85 bool success = s2n_send_early_data(client_conn,
86 next_early_data_to_send,
87 early_data_to_send->size - total_data_sent,
88 &data_sent, &blocked)
89 == S2N_SUCCESS;
90 total_data_sent += data_sent;
91 RESULT_GUARD(s2n_validate_negotiate_result(success, false, &client_early_done));
92 } else {
93 bool success = (s2n_negotiate(client_conn, &blocked) == S2N_SUCCESS);
94 RESULT_GUARD(s2n_validate_negotiate_result(success, server_done, &client_done));
95 }
96
97 if (!server_early_done) {
98 bool success = s2n_recv_early_data(server_conn,
99 early_data_received->data + total_data_recv,
100 early_data_received->size - total_data_recv,
101 &data_recv, &blocked)
102 == S2N_SUCCESS;
103 total_data_recv += data_recv;
104 RESULT_GUARD(s2n_validate_negotiate_result(success, false, &server_early_done));
105 } else {
106 bool success = (s2n_negotiate(server_conn, &blocked) == S2N_SUCCESS);
107 RESULT_GUARD(s2n_validate_negotiate_result(success, client_done, &server_done));
108 }
109 } while (!client_done || !server_done);
110
111 early_data_received->size = total_data_recv;
112 return S2N_RESULT_OK;
113 }
114
115 S2N_RESULT s2n_negotiate_test_server_and_client_until_message(struct s2n_connection *server_conn,
116 struct s2n_connection *client_conn, message_type_t message_type)
117 {
118 bool server_done = false, client_done = false;
119 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
120
121 do {
122 bool rc = s2n_result_is_ok(s2n_negotiate_until_message(client_conn, &blocked, message_type));
123 RESULT_GUARD(s2n_validate_negotiate_result(rc, server_done, &client_done));
124
125 rc = s2n_result_is_ok(s2n_negotiate_until_message(server_conn, &blocked, message_type));
126 RESULT_GUARD(s2n_validate_negotiate_result(rc, client_done, &server_done));
127 } while (!client_done || !server_done);
128
129 return S2N_RESULT_OK;
130 }
131
132 int s2n_shutdown_test_server_and_client(struct s2n_connection *server_conn, struct s2n_connection *client_conn)
133 {
134 int server_rc = -1;
135 int client_rc = -1;
136 s2n_blocked_status server_blocked;
137 s2n_blocked_status client_blocked;
138 int server_done = 0;
139 int client_done = 0;
140
141 do {
142 if (!server_done) {
143 s2n_errno = S2N_ERR_T_OK;
144 server_rc = s2n_shutdown(server_conn, &server_blocked);
145
146 if (s2n_error_get_type(s2n_errno) != S2N_ERR_T_BLOCKED || client_done) {
147 /* Success, fatal error, or the peer is done and we're still blocked. */
148 server_done = 1;
149 }
150 }
151 if (!client_done) {
152 s2n_errno = S2N_ERR_T_OK;
153 client_rc = s2n_shutdown(client_conn, &client_blocked);
154
155 if (s2n_error_get_type(s2n_errno) != S2N_ERR_T_BLOCKED || server_done) {
156 /* Success, fatal error, or the peer is done and we're still blocked. */
157 client_done = 1;
158 }
159 }
160 } while (!client_done || !server_done);
161
162 int rc = (server_rc == 0 && client_rc == 0) ? 0 : -1;
163 return rc;
164 }
165
166 S2N_RESULT s2n_send_and_recv_test(struct s2n_connection *send_conn, struct s2n_connection *recv_conn)
167 {
168 RESULT_ENSURE_REF(send_conn);
169 RESULT_ENSURE_REF(recv_conn);
170
171 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
172
173 const uint8_t send_data[] = "hello world";
174 ssize_t send_size = s2n_send(send_conn, send_data, sizeof(send_data), &blocked);
175 RESULT_GUARD_POSIX(send_size);
176 RESULT_ENSURE_EQ(send_size, sizeof(send_data));
177
178 uint8_t recv_data[sizeof(send_data)] = { 0 };
179 ssize_t recv_size = s2n_recv(recv_conn, recv_data, send_size, &blocked);
180 RESULT_GUARD_POSIX(recv_size);
181 RESULT_ENSURE_EQ(recv_size, send_size);
182 RESULT_ENSURE_EQ(memcmp(recv_data, send_data, send_size), 0);
183
184 return S2N_RESULT_OK;
185 }