Debian Codegraph @

crt/s2n/tests/unit/s2n_server_early_data_indication_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 "s2n_test.h"
17 #include "testlib/s2n_testlib.h"
18 #include "tls/extensions/s2n_early_data_indication.h"
19 #include "tls/s2n_tls.h"
20 #include "tls/s2n_tls13.h"
21 #include "utils/s2n_array.h"
22
23 static S2N_RESULT s2n_exchange_hellos(struct s2n_connection *client_conn, struct s2n_connection *server_conn)
24 {
25 RESULT_GUARD_POSIX(s2n_client_hello_send(client_conn));
26 RESULT_GUARD_POSIX(s2n_stuffer_copy(&client_conn->handshake.io, &server_conn->handshake.io,
27 s2n_stuffer_data_available(&client_conn->handshake.io)));
28 RESULT_GUARD_POSIX(s2n_establish_session(server_conn));
29
30 RESULT_GUARD_POSIX(s2n_server_hello_send(server_conn));
31 RESULT_GUARD_POSIX(s2n_stuffer_copy(&server_conn->handshake.io, &client_conn->handshake.io,
32 s2n_stuffer_data_available(&server_conn->handshake.io)));
33 RESULT_GUARD_POSIX(s2n_server_hello_recv(client_conn));
34
35 return S2N_RESULT_OK;
36 }
37
38 int main(int argc, char **argv)
39 {
40 BEGIN_TEST();
41
42 if (!s2n_is_tls13_fully_supported()) {
43 END_TEST();
44 }
45
46 const uint32_t nonzero_max_early_data = 10;
47
48 /* Test s2n_server_early_data_indication_should_send */
49 {
50 /* Safety check */
51 EXPECT_FALSE(s2n_server_early_data_indication_extension.should_send(NULL));
52
53 struct s2n_connection *conn = s2n_connection_new(S2N_SERVER);
54 EXPECT_NOT_NULL(conn);
55
56 conn->early_data_state = S2N_EARLY_DATA_NOT_REQUESTED;
57 EXPECT_FALSE(s2n_server_early_data_indication_extension.should_send(conn));
58
59 conn->early_data_state = S2N_EARLY_DATA_REJECTED;
60 EXPECT_FALSE(s2n_server_early_data_indication_extension.should_send(conn));
61
62 conn->early_data_state = S2N_EARLY_DATA_ACCEPTED;
63 EXPECT_TRUE(s2n_server_early_data_indication_extension.should_send(conn));
64
65 EXPECT_SUCCESS(s2n_connection_free(conn));
66 };
67
68 /* Test s2n_server_early_data_indication_is_missing */
69 {
70 /* No-op if early data not requested */
71 {
72 struct s2n_connection *conn = s2n_connection_new(S2N_SERVER);
73 EXPECT_NOT_NULL(conn);
74
75 conn->early_data_state = S2N_EARLY_DATA_NOT_REQUESTED;
76 EXPECT_SUCCESS(s2n_server_early_data_indication_extension.if_missing(conn));
77 EXPECT_EQUAL(conn->early_data_state, S2N_EARLY_DATA_NOT_REQUESTED);
78
79 conn->early_data_state = S2N_EARLY_DATA_REQUESTED;
80 EXPECT_SUCCESS(s2n_server_early_data_indication_extension.if_missing(conn));
81 EXPECT_EQUAL(conn->early_data_state, S2N_EARLY_DATA_REJECTED);
82
83 EXPECT_SUCCESS(s2n_connection_free(conn));
84 };
85 };
86
87 /* Test s2n_server_early_data_indication_recv */
88 {
89 struct s2n_stuffer stuffer = { 0 };
90
91 /* Fails if early data config does not match the connection */
92 {
93 struct s2n_connection *conn = s2n_connection_new(S2N_SERVER);
94 EXPECT_NOT_NULL(conn);
95
96 conn->secure->cipher_suite = &s2n_tls13_aes_256_gcm_sha384;
97 conn->actual_protocol_version = S2N_TLS13;
98 conn->early_data_state = S2N_EARLY_DATA_REQUESTED;
99
100 /* No early data configured */
101 EXPECT_FAILURE_WITH_ERRNO(s2n_server_early_data_indication_extension.recv(conn, &stuffer),
102 S2N_ERR_EARLY_DATA_NOT_ALLOWED);
103 EXPECT_NOT_EQUAL(conn->early_data_state, S2N_EARLY_DATA_ACCEPTED);
104
105 /* Early data correctly configured */
106 EXPECT_OK(s2n_append_test_chosen_psk_with_early_data(conn, nonzero_max_early_data, &s2n_tls13_aes_256_gcm_sha384));
107 EXPECT_SUCCESS(s2n_server_early_data_indication_extension.recv(conn, &stuffer));
108 EXPECT_EQUAL(conn->early_data_state, S2N_EARLY_DATA_ACCEPTED);
109
110 EXPECT_SUCCESS(s2n_connection_free(conn));
111 };
112
113 /* Fails if early data not requested */
114 {
115 struct s2n_connection *conn = s2n_connection_new(S2N_SERVER);
116 EXPECT_NOT_NULL(conn);
117
118 conn->secure->cipher_suite = &s2n_tls13_aes_256_gcm_sha384;
119 conn->actual_protocol_version = S2N_TLS13;
120 EXPECT_OK(s2n_append_test_chosen_psk_with_early_data(conn, nonzero_max_early_data, &s2n_tls13_aes_256_gcm_sha384));
121
122 /* Early data not requested */
123 conn->early_data_state = S2N_EARLY_DATA_NOT_REQUESTED;
124 EXPECT_FAILURE_WITH_ERRNO(s2n_server_early_data_indication_extension.recv(conn, &stuffer),
125 S2N_ERR_INVALID_EARLY_DATA_STATE);
126
127 /* Early data requested */
128 conn->early_data_state = S2N_EARLY_DATA_REQUESTED;
129 EXPECT_SUCCESS(s2n_server_early_data_indication_extension.recv(conn, &stuffer));
130 EXPECT_EQUAL(conn->early_data_state, S2N_EARLY_DATA_ACCEPTED);
131
132 EXPECT_SUCCESS(s2n_connection_free(conn));
133 };
134 };
135
136 /* Test state transitions */
137 {
138 const char *security_policy = "20190801";
139 struct s2n_cipher_suite *expected_cipher_suite = &s2n_tls13_aes_256_gcm_sha384;
140
141 /* When early data not requested */
142 {
143 struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT);
144 EXPECT_NOT_NULL(client_conn);
145 EXPECT_OK(s2n_append_test_chosen_psk_with_early_data(client_conn, 0, expected_cipher_suite));
146 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(client_conn, security_policy));
147 EXPECT_SUCCESS(s2n_connection_set_early_data_expected(client_conn));
148
149 struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER);
150 EXPECT_NOT_NULL(server_conn);
151 EXPECT_OK(s2n_append_test_chosen_psk_with_early_data(server_conn, 0, expected_cipher_suite));
152 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(server_conn, security_policy));
153 EXPECT_SUCCESS(s2n_connection_set_early_data_expected(server_conn));
154
155 EXPECT_OK(s2n_exchange_hellos(client_conn, server_conn));
156 EXPECT_EQUAL(client_conn->early_data_state, S2N_EARLY_DATA_NOT_REQUESTED);
157 EXPECT_EQUAL(server_conn->early_data_state, S2N_EARLY_DATA_NOT_REQUESTED);
158
159 EXPECT_SUCCESS(s2n_encrypted_extensions_send(server_conn));
160 EXPECT_SUCCESS(s2n_stuffer_copy(&server_conn->handshake.io, &client_conn->handshake.io,
161 s2n_stuffer_data_available(&server_conn->handshake.io)));
162 EXPECT_SUCCESS(s2n_encrypted_extensions_recv(client_conn));
163 EXPECT_EQUAL(client_conn->early_data_state, S2N_EARLY_DATA_NOT_REQUESTED);
164 EXPECT_EQUAL(server_conn->early_data_state, S2N_EARLY_DATA_NOT_REQUESTED);
165
166 EXPECT_SUCCESS(s2n_connection_free(client_conn));
167 EXPECT_SUCCESS(s2n_connection_free(server_conn));
168 };
169
170 /** When early data accepted.
171 *
172 *= https://www.rfc-editor.org/rfc/rfc8446#section-4.2.10
173 *= type=test
174 *# A server which receives an "early_data" extension MUST behave in one
175 *# of three ways:
176 *
177 *= https://www.rfc-editor.org/rfc/rfc8446#section-4.2.10
178 *= type=test
179 *# - Return its own "early_data" extension in EncryptedExtensions,
180 *# indicating that it intends to process the early data.
181 **/
182 {
183 struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT);
184 EXPECT_NOT_NULL(client_conn);
185 EXPECT_OK(s2n_append_test_chosen_psk_with_early_data(client_conn, nonzero_max_early_data, expected_cipher_suite));
186 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(client_conn, security_policy));
187 EXPECT_SUCCESS(s2n_connection_set_early_data_expected(client_conn));
188
189 struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER);
190 EXPECT_NOT_NULL(server_conn);
191 EXPECT_OK(s2n_append_test_chosen_psk_with_early_data(server_conn, nonzero_max_early_data, expected_cipher_suite));
192 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(server_conn, security_policy));
193 EXPECT_SUCCESS(s2n_connection_set_early_data_expected(server_conn));
194
195 EXPECT_OK(s2n_exchange_hellos(client_conn, server_conn));
196 EXPECT_EQUAL(client_conn->early_data_state, S2N_EARLY_DATA_REQUESTED);
197 EXPECT_EQUAL(server_conn->early_data_state, S2N_EARLY_DATA_ACCEPTED);
198
199 EXPECT_SUCCESS(s2n_encrypted_extensions_send(server_conn));
200 EXPECT_SUCCESS(s2n_stuffer_copy(&server_conn->handshake.io, &client_conn->handshake.io,
201 s2n_stuffer_data_available(&server_conn->handshake.io)));
202 EXPECT_SUCCESS(s2n_encrypted_extensions_recv(client_conn));
203 EXPECT_EQUAL(client_conn->early_data_state, S2N_EARLY_DATA_ACCEPTED);
204 EXPECT_EQUAL(server_conn->early_data_state, S2N_EARLY_DATA_ACCEPTED);
205
206 EXPECT_SUCCESS(s2n_connection_free(client_conn));
207 EXPECT_SUCCESS(s2n_connection_free(server_conn));
208 };
209
210 /** When early data rejected.
211 *
212 *= https://www.rfc-editor.org/rfc/rfc8446#section-4.2.10
213 *# A server which receives an "early_data" extension MUST behave in one
214 *# of three ways:
215 *#
216 *# - Ignore the extension and return a regular 1-RTT response.
217 **/
218 {
219 struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT);
220 EXPECT_NOT_NULL(client_conn);
221 EXPECT_OK(s2n_append_test_chosen_psk_with_early_data(client_conn, nonzero_max_early_data, expected_cipher_suite));
222 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(client_conn, security_policy));
223 EXPECT_SUCCESS(s2n_connection_set_early_data_expected(client_conn));
224
225 struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER);
226 EXPECT_NOT_NULL(server_conn);
227 EXPECT_OK(s2n_append_test_chosen_psk_with_early_data(server_conn, 0, expected_cipher_suite));
228 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(server_conn, security_policy));
229 EXPECT_SUCCESS(s2n_connection_set_early_data_expected(server_conn));
230
231 EXPECT_OK(s2n_exchange_hellos(client_conn, server_conn));
232 EXPECT_EQUAL(client_conn->early_data_state, S2N_EARLY_DATA_REQUESTED);
233 EXPECT_EQUAL(server_conn->early_data_state, S2N_EARLY_DATA_REJECTED);
234
235 EXPECT_SUCCESS(s2n_encrypted_extensions_send(server_conn));
236 EXPECT_SUCCESS(s2n_stuffer_copy(&server_conn->handshake.io, &client_conn->handshake.io,
237 s2n_stuffer_data_available(&server_conn->handshake.io)));
238 EXPECT_SUCCESS(s2n_encrypted_extensions_recv(client_conn));
239 EXPECT_EQUAL(client_conn->early_data_state, S2N_EARLY_DATA_REJECTED);
240 EXPECT_EQUAL(server_conn->early_data_state, S2N_EARLY_DATA_REJECTED);
241
242 EXPECT_SUCCESS(s2n_connection_free(client_conn));
243 EXPECT_SUCCESS(s2n_connection_free(server_conn));
244 };
245
246 /*
247 *= https://www.rfc-editor.org/rfc/rfc8446#section-4.2.10
248 *= type=test
249 *# A server which receives an "early_data" extension MUST behave in one
250 *# of three ways:
251 *
252 *= https://www.rfc-editor.org/rfc/rfc8446#section-4.2.10
253 *= type=test
254 *# - Request that the client send another ClientHello by responding
255 *# with a HelloRetryRequest.
256 **/
257 {
258 struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT);
259 EXPECT_NOT_NULL(client_conn);
260 EXPECT_OK(s2n_append_test_chosen_psk_with_early_data(client_conn, nonzero_max_early_data, expected_cipher_suite));
261 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(client_conn, security_policy));
262 EXPECT_SUCCESS(s2n_connection_set_early_data_expected(client_conn));
263
264 struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER);
265 EXPECT_NOT_NULL(server_conn);
266 EXPECT_OK(s2n_append_test_chosen_psk_with_early_data(server_conn, nonzero_max_early_data, expected_cipher_suite));
267 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(server_conn, security_policy));
268 EXPECT_SUCCESS(s2n_connection_set_early_data_expected(server_conn));
269
270 EXPECT_SUCCESS(s2n_client_hello_send(client_conn));
271 EXPECT_SUCCESS(s2n_stuffer_copy(&client_conn->handshake.io, &server_conn->handshake.io,
272 s2n_stuffer_data_available(&client_conn->handshake.io)));
273 EXPECT_SUCCESS(s2n_client_hello_recv(server_conn));
274 EXPECT_EQUAL(client_conn->early_data_state, S2N_EARLY_DATA_REQUESTED);
275
276 /* Force a retry. The S2N server does not reject early data via HRR, so we have
277 * to manually trigger the retry. */
278 server_conn->early_data_state = S2N_EARLY_DATA_REJECTED;
279 EXPECT_SUCCESS(s2n_set_hello_retry_required(server_conn));
280 server_conn->handshake.message_number = 2;
281 client_conn->handshake.message_number = 2;
282
283 /* Update the selected_group to ensure the HRR is valid */
284 client_conn->kex_params.client_ecc_evp_params.negotiated_curve = &s2n_ecc_curve_secp521r1;
285
286 EXPECT_SUCCESS(s2n_server_hello_retry_send(server_conn));
287 EXPECT_SUCCESS(s2n_stuffer_copy(&server_conn->handshake.io, &client_conn->handshake.io,
288 s2n_stuffer_data_available(&server_conn->handshake.io)));
289 EXPECT_SUCCESS(s2n_server_hello_recv(client_conn));
290 EXPECT_EQUAL(client_conn->early_data_state, S2N_EARLY_DATA_REJECTED);
291 EXPECT_EQUAL(server_conn->early_data_state, S2N_EARLY_DATA_REJECTED);
292
293 EXPECT_SUCCESS(s2n_encrypted_extensions_send(server_conn));
294 EXPECT_SUCCESS(s2n_stuffer_copy(&server_conn->handshake.io, &client_conn->handshake.io,
295 s2n_stuffer_data_available(&server_conn->handshake.io)));
296 EXPECT_SUCCESS(s2n_encrypted_extensions_recv(client_conn));
297 EXPECT_EQUAL(client_conn->early_data_state, S2N_EARLY_DATA_REJECTED);
298 EXPECT_EQUAL(server_conn->early_data_state, S2N_EARLY_DATA_REJECTED);
299
300 EXPECT_SUCCESS(s2n_connection_free(client_conn));
301 EXPECT_SUCCESS(s2n_connection_free(server_conn));
302 };
303 };
304
305 END_TEST();
306 }