Debian Codegraph @

crt/s2n/tests/unit/s2n_extended_master_secret_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 "utils/s2n_bitmap.h"
19
20 int main(int argc, char **argv)
21 {
22 BEGIN_TEST();
23
24 /* Test s2n_conn_set_handshake_type is processing EMS data correctly */
25 {
26 struct s2n_config *config = NULL;
27 EXPECT_NOT_NULL(config = s2n_config_new());
28 EXPECT_OK(s2n_resumption_test_ticket_key_setup(config));
29
30 /**
31 *= https://www.rfc-editor.org/rfc/rfc7627#section-5.3
32 *= type=test
33 *# If the original session used the "extended_master_secret"
34 *# extension but the new ClientHello does not contain it, the server
35 *# MUST abort the abbreviated handshake.
36 **/
37 {
38 struct s2n_connection *conn = s2n_connection_new(S2N_SERVER);
39 EXPECT_NOT_NULL(conn);
40 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
41 conn->actual_protocol_version = S2N_TLS12;
42 conn->secure->cipher_suite = &s2n_ecdhe_ecdsa_with_aes_128_gcm_sha256;
43 /* Original connection negotiated an EMS */
44 conn->ems_negotiated = true;
45
46 struct s2n_stuffer ticket = { 0 };
47 struct s2n_blob ticket_blob = { 0 };
48 uint8_t ticket_data[S2N_TLS12_TICKET_SIZE_IN_BYTES] = { 0 };
49 EXPECT_SUCCESS(s2n_blob_init(&ticket_blob, ticket_data, S2N_TLS12_TICKET_SIZE_IN_BYTES));
50 EXPECT_SUCCESS(s2n_stuffer_init(&ticket, &ticket_blob));
51
52 struct s2n_ticket_key *key = s2n_get_ticket_encrypt_decrypt_key(conn->config);
53 EXPECT_NOT_NULL(key);
54
55 /* Encrypt the ticket with EMS data */
56 EXPECT_OK(s2n_resume_encrypt_session_ticket(conn, key, &ticket));
57
58 EXPECT_SUCCESS(s2n_connection_wipe(conn));
59 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
60 conn->actual_protocol_version = S2N_TLS12;
61 conn->secure->cipher_suite = &s2n_ecdhe_ecdsa_with_aes_128_gcm_sha256;
62 conn->session_ticket_status = S2N_DECRYPT_TICKET;
63 EXPECT_SUCCESS(s2n_stuffer_copy(&ticket, &conn->client_ticket_to_decrypt, S2N_TLS12_TICKET_SIZE_IN_BYTES));
64
65 /* Resumed session did not receive the EMS extension */
66 EXPECT_FAILURE_WITH_ERRNO(s2n_conn_set_handshake_type(conn), S2N_ERR_MISSING_EXTENSION);
67 EXPECT_SUCCESS(s2n_connection_free(conn));
68 };
69
70 /**
71 *= https://www.rfc-editor.org/rfc/rfc7627#section-5.3
72 *= type=test
73 *# If the original session did not use the "extended_master_secret"
74 *# extension but the new ClientHello contains the extension, then the
75 *# server MUST NOT perform the abbreviated handshake. Instead, it
76 *# SHOULD continue with a full handshake (as described in
77 *# Section 5.2) to negotiate a new session.
78 **/
79 {
80 struct s2n_connection *conn = s2n_connection_new(S2N_SERVER);
81 EXPECT_NOT_NULL(conn);
82 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
83 conn->actual_protocol_version = S2N_TLS12;
84 conn->secure->cipher_suite = &s2n_ecdhe_ecdsa_with_aes_128_gcm_sha256;
85 /* Original connection did not negotiate an EMS */
86 conn->ems_negotiated = false;
87
88 struct s2n_stuffer ticket = { 0 };
89 struct s2n_blob ticket_blob = { 0 };
90 uint8_t ticket_data[S2N_TLS12_TICKET_SIZE_IN_BYTES] = { 0 };
91 EXPECT_SUCCESS(s2n_blob_init(&ticket_blob, ticket_data, S2N_TLS12_TICKET_SIZE_IN_BYTES));
92 EXPECT_SUCCESS(s2n_stuffer_init(&ticket, &ticket_blob));
93
94 struct s2n_ticket_key *key = s2n_get_ticket_encrypt_decrypt_key(conn->config);
95 EXPECT_NOT_NULL(key);
96
97 /* Encrypt the ticket without EMS data */
98 EXPECT_OK(s2n_resume_encrypt_session_ticket(conn, key, &ticket));
99
100 EXPECT_SUCCESS(s2n_connection_wipe(conn));
101 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
102 conn->actual_protocol_version = S2N_TLS12;
103 conn->secure->cipher_suite = &s2n_ecdhe_ecdsa_with_aes_128_gcm_sha256;
104 conn->session_ticket_status = S2N_DECRYPT_TICKET;
105 EXPECT_SUCCESS(s2n_stuffer_copy(&ticket, &conn->client_ticket_to_decrypt, S2N_TLS12_TICKET_SIZE_IN_BYTES));
106
107 /* Resumed connection received the EMS extension */
108 conn->ems_negotiated = true;
109
110 EXPECT_SUCCESS(s2n_conn_set_handshake_type(conn));
111
112 /* Fallback to full handshake */
113 EXPECT_TRUE(s2n_handshake_type_check_flag(conn, FULL_HANDSHAKE));
114
115 EXPECT_SUCCESS(s2n_connection_free(conn));
116 };
117
118 /* Session ticket is processed correctly if the previous session and current session both negotiated EMS */
119 {
120 struct s2n_connection *conn = s2n_connection_new(S2N_SERVER);
121 EXPECT_NOT_NULL(conn);
122 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
123 conn->actual_protocol_version = S2N_TLS12;
124 conn->secure->cipher_suite = &s2n_ecdhe_ecdsa_with_aes_128_gcm_sha256;
125 /* Original connection negotiated an EMS */
126 conn->ems_negotiated = true;
127
128 struct s2n_stuffer ticket = { 0 };
129 struct s2n_blob ticket_blob = { 0 };
130 uint8_t ticket_data[S2N_TLS12_TICKET_SIZE_IN_BYTES] = { 0 };
131 EXPECT_SUCCESS(s2n_blob_init(&ticket_blob, ticket_data, S2N_TLS12_TICKET_SIZE_IN_BYTES));
132 EXPECT_SUCCESS(s2n_stuffer_init(&ticket, &ticket_blob));
133
134 struct s2n_ticket_key *key = s2n_get_ticket_encrypt_decrypt_key(conn->config);
135 EXPECT_NOT_NULL(key);
136
137 /* Encrypt the ticket with EMS data */
138 EXPECT_OK(s2n_resume_encrypt_session_ticket(conn, key, &ticket));
139
140 EXPECT_SUCCESS(s2n_connection_wipe(conn));
141 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
142 conn->actual_protocol_version = S2N_TLS12;
143 conn->secure->cipher_suite = &s2n_ecdhe_ecdsa_with_aes_128_gcm_sha256;
144 conn->session_ticket_status = S2N_DECRYPT_TICKET;
145 EXPECT_SUCCESS(s2n_stuffer_copy(&ticket, &conn->client_ticket_to_decrypt, S2N_TLS12_TICKET_SIZE_IN_BYTES));
146
147 /* Resumed connection received the EMS extension */
148 conn->ems_negotiated = true;
149 s2n_extension_type_id ems_ext_id = 0;
150 EXPECT_SUCCESS(s2n_extension_supported_iana_value_to_id(TLS_EXTENSION_EMS, &ems_ext_id));
151 S2N_CBIT_SET(conn->extension_requests_received, ems_ext_id);
152
153 EXPECT_SUCCESS(s2n_conn_set_handshake_type(conn));
154
155 EXPECT_FALSE(s2n_handshake_type_check_flag(conn, FULL_HANDSHAKE));
156
157 EXPECT_SUCCESS(s2n_connection_free(conn));
158 };
159
160 EXPECT_SUCCESS(s2n_config_free(config));
161 };
162
163 /* Connection where the client supports EMS but the server does not support EMS */
164 {
165 struct s2n_config *config = s2n_config_new();
166 EXPECT_NOT_NULL(config);
167
168 /* TLS1.2 cipher preferences */
169 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "20240501"));
170 EXPECT_SUCCESS(s2n_config_set_unsafe_for_testing(config));
171 struct s2n_cert_chain_and_key *chain_and_key = NULL;
172 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_and_key,
173 S2N_DEFAULT_TEST_CERT_CHAIN, S2N_DEFAULT_TEST_PRIVATE_KEY));
174 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key));
175
176 struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT);
177 EXPECT_NOT_NULL(client_conn);
178 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
179
180 struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER);
181 EXPECT_NOT_NULL(server_conn);
182 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
183
184 /* Create nonblocking pipes */
185 struct s2n_test_io_pair io_pair = { 0 };
186 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
187 EXPECT_SUCCESS(s2n_connections_set_io_pair(client_conn, server_conn, &io_pair));
188
189 /* Negotiate until server has read the Client Hello message */
190 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
191 EXPECT_OK(s2n_negotiate_until_message(client_conn, &blocked, SERVER_HELLO));
192 EXPECT_OK(s2n_negotiate_until_message(server_conn, &blocked, SERVER_HELLO));
193
194 /* s2n servers by default support EMS. We turn it off by manually setting ems_negotiated to false
195 * and removing the EMS extension from our received extensions. */
196 server_conn->ems_negotiated = false;
197 s2n_extension_type_id ems_ext_id = 0;
198 EXPECT_SUCCESS(s2n_extension_supported_iana_value_to_id(TLS_EXTENSION_EMS, &ems_ext_id));
199 S2N_CBIT_CLR(server_conn->extension_requests_received, ems_ext_id);
200
201 /* Connection is successful and EMS is not negotiated */
202 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
203 EXPECT_EQUAL(server_conn->actual_protocol_version, S2N_TLS12);
204 EXPECT_EQUAL(client_conn->actual_protocol_version, S2N_TLS12);
205 EXPECT_FALSE(server_conn->ems_negotiated);
206 EXPECT_FALSE(client_conn->ems_negotiated);
207
208 EXPECT_SUCCESS(s2n_connection_free(client_conn));
209 EXPECT_SUCCESS(s2n_connection_free(server_conn));
210 EXPECT_SUCCESS(s2n_io_pair_close(&io_pair));
211 EXPECT_SUCCESS(s2n_cert_chain_and_key_free(chain_and_key));
212 EXPECT_SUCCESS(s2n_config_free(config));
213 };
214
215 /* Connection where the server supports EMS but the client does not support EMS */
216 {
217 struct s2n_config *config = s2n_config_new();
218 EXPECT_NOT_NULL(config);
219
220 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "20240501"));
221 EXPECT_SUCCESS(s2n_config_set_unsafe_for_testing(config));
222 struct s2n_cert_chain_and_key *chain_and_key = NULL;
223 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_and_key,
224 S2N_DEFAULT_TEST_CERT_CHAIN, S2N_DEFAULT_TEST_PRIVATE_KEY));
225 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key));
226
227 struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT);
228 EXPECT_NOT_NULL(client_conn);
229 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
230
231 struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER);
232 EXPECT_NOT_NULL(server_conn);
233 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
234
235 /* Create nonblocking pipes */
236 struct s2n_test_io_pair io_pair = { 0 };
237 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
238 EXPECT_SUCCESS(s2n_connections_set_io_pair(client_conn, server_conn, &io_pair));
239
240 /* s2n clients support EMS by default. To manually prevent them from sending the EMS extension, add a
241 * resumption ticket to the connection, which indicates the previous session did not negotiate
242 * EMS and therefore this session shouldn't either. The resumption ticket does not have to be valid
243 * as this test is only interested in EMS. */
244 client_conn->set_session = true;
245
246 /* Connection is successful and EMS is not negotiated */
247 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
248 EXPECT_EQUAL(server_conn->actual_protocol_version, S2N_TLS12);
249 EXPECT_EQUAL(client_conn->actual_protocol_version, S2N_TLS12);
250 EXPECT_FALSE(server_conn->ems_negotiated);
251 EXPECT_FALSE(client_conn->ems_negotiated);
252
253 EXPECT_SUCCESS(s2n_connection_free(client_conn));
254 EXPECT_SUCCESS(s2n_connection_free(server_conn));
255 EXPECT_SUCCESS(s2n_cert_chain_and_key_free(chain_and_key));
256 EXPECT_SUCCESS(s2n_io_pair_close(&io_pair));
257 EXPECT_SUCCESS(s2n_config_free(config));
258 };
259
260 /* Connection where both client and server support EMS */
261 {
262 struct s2n_config *config = s2n_config_new();
263 EXPECT_NOT_NULL(config);
264
265 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "20240501"));
266 EXPECT_SUCCESS(s2n_config_set_unsafe_for_testing(config));
267 struct s2n_cert_chain_and_key *chain_and_key = NULL;
268 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_and_key,
269 S2N_DEFAULT_TEST_CERT_CHAIN, S2N_DEFAULT_TEST_PRIVATE_KEY));
270 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key));
271
272 struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT);
273 EXPECT_NOT_NULL(client_conn);
274 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
275
276 struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER);
277 EXPECT_NOT_NULL(server_conn);
278 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
279
280 /* Create nonblocking pipes */
281 struct s2n_test_io_pair io_pair = { 0 };
282 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
283 EXPECT_SUCCESS(s2n_connections_set_io_pair(client_conn, server_conn, &io_pair));
284
285 /* Connection is successful and EMS is negotiated */
286 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
287 EXPECT_EQUAL(server_conn->actual_protocol_version, S2N_TLS12);
288 EXPECT_EQUAL(client_conn->actual_protocol_version, S2N_TLS12);
289 EXPECT_TRUE(server_conn->ems_negotiated);
290 EXPECT_TRUE(client_conn->ems_negotiated);
291
292 EXPECT_SUCCESS(s2n_connection_free(client_conn));
293 EXPECT_SUCCESS(s2n_connection_free(server_conn));
294 EXPECT_SUCCESS(s2n_cert_chain_and_key_free(chain_and_key));
295 EXPECT_SUCCESS(s2n_io_pair_close(&io_pair));
296 EXPECT_SUCCESS(s2n_config_free(config));
297 };
298
299 END_TEST();
300 }