Debian Codegraph @

crt/s2n/tests/unit/s2n_client_hello_request_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/s2n_tls.h"
19
20 static const uint8_t hello_request_msg[] = {
21 /* message header */
22 TLS_HELLO_REQUEST, /* msg_type = hello_request */
23 0, 0, 0, /* length = 0 */
24 /* empty message body */
25 };
26
27 static S2N_RESULT s2n_send_client_hello_request(struct s2n_connection *server_conn)
28 {
29 RESULT_ENSURE_REF(server_conn);
30
31 DEFER_CLEANUP(struct s2n_blob message_blob = { 0 }, s2n_free);
32 RESULT_GUARD_POSIX(s2n_realloc(&message_blob, sizeof(hello_request_msg)));
33 RESULT_CHECKED_MEMCPY(message_blob.data, hello_request_msg, message_blob.size);
34
35 /* Send */
36 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
37 RESULT_GUARD(s2n_record_write(server_conn, TLS_HANDSHAKE, &message_blob));
38 RESULT_GUARD_POSIX(s2n_flush(server_conn, &blocked));
39
40 /* Cleanup */
41 RESULT_GUARD_POSIX(s2n_stuffer_wipe(&server_conn->out));
42 RESULT_GUARD_POSIX(s2n_stuffer_wipe(&server_conn->handshake.io));
43
44 return S2N_RESULT_OK;
45 }
46
47 struct s2n_test_reneg_req_ctx {
48 s2n_renegotiate_response app_decision;
49 uint8_t call_count;
50 };
51
52 static int s2n_test_reneg_req_cb(struct s2n_connection *conn, void *context, s2n_renegotiate_response *response)
53 {
54 POSIX_ENSURE_REF(context);
55 POSIX_ENSURE_REF(response);
56
57 struct s2n_test_reneg_req_ctx *test_context = (struct s2n_test_reneg_req_ctx *) context;
58 *response = test_context->app_decision;
59 POSIX_ENSURE_LT(test_context->call_count, UINT8_MAX);
60 test_context->call_count++;
61
62 return S2N_SUCCESS;
63 }
64
65 int main(int argc, char **argv)
66 {
67 BEGIN_TEST();
68
69 static struct s2n_cert_chain_and_key *chain_and_key = NULL;
70 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_and_key,
71 S2N_DEFAULT_TEST_CERT_CHAIN, S2N_DEFAULT_TEST_PRIVATE_KEY));
72
73 DEFER_CLEANUP(struct s2n_cert_chain_and_key *ecdsa_chain_and_key = NULL, s2n_cert_chain_and_key_ptr_free);
74 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&ecdsa_chain_and_key,
75 S2N_DEFAULT_ECDSA_TEST_CERT_CHAIN, S2N_DEFAULT_ECDSA_TEST_PRIVATE_KEY));
76
77 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
78 EXPECT_NOT_NULL(config);
79 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "20240501"));
80 EXPECT_SUCCESS(s2n_config_set_unsafe_for_testing(config));
81 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key));
82
83 DEFER_CLEANUP(struct s2n_config *config_with_reneg_cb = s2n_config_new(), s2n_config_ptr_free);
84 EXPECT_NOT_NULL(config_with_reneg_cb);
85 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config_with_reneg_cb, "20240501"));
86 EXPECT_SUCCESS(s2n_config_set_unsafe_for_testing(config_with_reneg_cb));
87 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config_with_reneg_cb, chain_and_key));
88 EXPECT_SUCCESS(s2n_config_set_renegotiate_request_cb(config_with_reneg_cb, s2n_test_reneg_req_cb, NULL));
89
90 /* Test: Hello requests received during the handshake are a no-op */
91 {
92 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT),
93 s2n_connection_ptr_free);
94 EXPECT_NOT_NULL(client_conn);
95 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
96
97 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER),
98 s2n_connection_ptr_free);
99 EXPECT_NOT_NULL(server_conn);
100 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
101
102 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
103 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
104 EXPECT_SUCCESS(s2n_connection_set_io_pair(client_conn, &io_pair));
105 EXPECT_SUCCESS(s2n_connection_set_io_pair(server_conn, &io_pair));
106
107 /* Start the handshake.
108 * We should be able to receive the hello request in the middle of the handshake. */
109 EXPECT_OK(s2n_negotiate_test_server_and_client_until_message(server_conn, client_conn,
110 SERVER_HELLO_DONE));
111 EXPECT_EQUAL(server_conn->server, server_conn->initial);
112
113 /* Send a hello request */
114 EXPECT_OK(s2n_send_client_hello_request(server_conn));
115
116 /* Successfully complete the handshake */
117 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
118 };
119
120 /* Test: Hello requests received during the handshake are an error for TLS1.3 */
121 if (s2n_is_tls13_fully_supported()) {
122 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT),
123 s2n_connection_ptr_free);
124 EXPECT_NOT_NULL(client_conn);
125 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(client_conn, "default_tls13"));
126 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
127
128 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER),
129 s2n_connection_ptr_free);
130 EXPECT_NOT_NULL(server_conn);
131 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(server_conn, "default_tls13"));
132 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
133
134 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
135 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
136 EXPECT_SUCCESS(s2n_connection_set_io_pair(client_conn, &io_pair));
137 EXPECT_SUCCESS(s2n_connection_set_io_pair(server_conn, &io_pair));
138
139 /* Start the handshake */
140 EXPECT_OK(s2n_negotiate_test_server_and_client_until_message(server_conn, client_conn,
141 SERVER_HELLO));
142
143 /* Send a hello request.
144 * We should be able to receive the hello request before the version is negotiated. */
145 EXPECT_OK(s2n_send_client_hello_request(server_conn));
146
147 /* Continue the handshake */
148 EXPECT_OK(s2n_negotiate_test_server_and_client_until_message(server_conn, client_conn,
149 ENCRYPTED_EXTENSIONS));
150
151 /* Send a hello request.
152 * We should NOT be able to receive the hello request after TLS1.3 is negotiated */
153 EXPECT_EQUAL(server_conn->actual_protocol_version, S2N_TLS13);
154 EXPECT_EQUAL(client_conn->actual_protocol_version, S2N_TLS13);
155 EXPECT_OK(s2n_send_client_hello_request(server_conn));
156
157 /* Handshake should fail because the HelloRequest is unexpected in TLS1.3 */
158 EXPECT_FAILURE_WITH_ERRNO(s2n_negotiate_test_server_and_client(server_conn, client_conn),
159 S2N_ERR_BAD_MESSAGE);
160 }
161
162 /* Test: Hello requests received after the handshake can be ignored.
163 *
164 * We can continue sending and receiving data after the request.
165 * s2n-tls treats warnings as fatals by default though, so we must disable that behavior.
166 */
167 {
168 DEFER_CLEANUP(struct s2n_config *config_with_warns = s2n_config_new(), s2n_config_ptr_free);
169 EXPECT_NOT_NULL(config_with_warns);
170 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config_with_warns, "20240501"));
171 EXPECT_SUCCESS(s2n_config_set_unsafe_for_testing(config_with_warns));
172 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config_with_warns, chain_and_key));
173 EXPECT_SUCCESS(s2n_config_set_alert_behavior(config_with_warns, S2N_ALERT_IGNORE_WARNINGS));
174
175 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT),
176 s2n_connection_ptr_free);
177 EXPECT_NOT_NULL(client_conn);
178 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config_with_warns));
179
180 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER),
181 s2n_connection_ptr_free);
182 EXPECT_NOT_NULL(server_conn);
183 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config_with_warns));
184
185 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
186 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
187 EXPECT_SUCCESS(s2n_connection_set_io_pair(client_conn, &io_pair));
188 EXPECT_SUCCESS(s2n_connection_set_io_pair(server_conn, &io_pair));
189
190 /* Complete the handshake */
191 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
192 EXPECT_TRUE(client_conn->secure_renegotiation);
193
194 /* Send some data */
195 EXPECT_OK(s2n_send_and_recv_test(server_conn, client_conn));
196 EXPECT_OK(s2n_send_and_recv_test(client_conn, server_conn));
197
198 /* Send the hello request message. */
199 EXPECT_OK(s2n_send_client_hello_request(server_conn));
200
201 /* Send some more data */
202 for (size_t i = 0; i < 10; i++) {
203 EXPECT_OK(s2n_send_and_recv_test(server_conn, client_conn));
204 EXPECT_OK(s2n_send_and_recv_test(client_conn, server_conn));
205 EXPECT_TRUE(s2n_connection_check_io_status(client_conn, S2N_IO_FULL_DUPLEX));
206 }
207 };
208
209 /* Test: Hello requests received after the handshake do NOT trigger a no_renegotiation alert
210 * if renegotiation callbacks not set.
211 *
212 *= https://www.rfc-editor.org/rfc/rfc5246#section-7.4.1.1
213 *= type=test
214 *# This message MAY be ignored by
215 *# the client if it does not wish to renegotiate a session, or the
216 *# client may, if it wishes, respond with a no_renegotiation alert.
217 **/
218 {
219 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT),
220 s2n_connection_ptr_free);
221 EXPECT_NOT_NULL(client_conn);
222 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
223 EXPECT_NULL(client_conn->config->renegotiate_request_cb);
224
225 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER),
226 s2n_connection_ptr_free);
227 EXPECT_NOT_NULL(server_conn);
228 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
229
230 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
231 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
232 EXPECT_SUCCESS(s2n_connection_set_io_pair(client_conn, &io_pair));
233 EXPECT_SUCCESS(s2n_connection_set_io_pair(server_conn, &io_pair));
234
235 /* Complete the handshake */
236 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
237 EXPECT_TRUE(client_conn->secure_renegotiation);
238
239 /* Send the hello request message. */
240 EXPECT_OK(s2n_send_client_hello_request(server_conn));
241
242 /* no_renegotation alert NOT sent and received */
243 EXPECT_OK(s2n_send_and_recv_test(server_conn, client_conn));
244 EXPECT_OK(s2n_send_and_recv_test(client_conn, server_conn));
245
246 /* Callback was not set */
247 EXPECT_NULL(client_conn->config->renegotiate_request_cb);
248 }
249
250 /* Test: Hello requests received after the handshake trigger a no_renegotiation alert
251 * if the application rejects the renegotiation request
252 *
253 *= https://www.rfc-editor.org/rfc/rfc5746#5
254 *= type=test
255 *# TLS implementations SHOULD provide a mechanism to disable and enable
256 *# renegotiation.
257 *
258 *= https://www.rfc-editor.org/rfc/rfc5246#section-7.4.1.1
259 *= type=test
260 *# This message MAY be ignored by
261 *# the client if it does not wish to renegotiate a session, or the
262 *# client may, if it wishes, respond with a no_renegotiation alert.
263 **/
264 {
265 struct s2n_test_reneg_req_ctx ctx = { .app_decision = S2N_RENEGOTIATE_REJECT };
266
267 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT),
268 s2n_connection_ptr_free);
269 EXPECT_NOT_NULL(client_conn);
270 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config_with_reneg_cb));
271 EXPECT_SUCCESS(s2n_config_set_renegotiate_request_cb(config_with_reneg_cb, s2n_test_reneg_req_cb, &ctx));
272
273 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER),
274 s2n_connection_ptr_free);
275 EXPECT_NOT_NULL(server_conn);
276 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
277
278 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
279 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
280 EXPECT_SUCCESS(s2n_connection_set_io_pair(client_conn, &io_pair));
281 EXPECT_SUCCESS(s2n_connection_set_io_pair(server_conn, &io_pair));
282
283 /* Complete the handshake */
284 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
285 EXPECT_TRUE(client_conn->secure_renegotiation);
286
287 /* Send the hello request message. */
288 EXPECT_OK(s2n_send_client_hello_request(server_conn));
289
290 /* no_renegotation alert sent and received */
291 EXPECT_OK(s2n_send_and_recv_test(server_conn, client_conn));
292 EXPECT_ERROR_WITH_ERRNO(s2n_send_and_recv_test(client_conn, server_conn), S2N_ERR_ALERT);
293 EXPECT_EQUAL(s2n_connection_get_alert(server_conn), S2N_TLS_ALERT_NO_RENEGOTIATION);
294
295 /* Callback triggered */
296 EXPECT_NOT_NULL(client_conn->config->renegotiate_request_cb);
297 EXPECT_EQUAL(ctx.call_count, 1);
298 };
299
300 /* Test: Hello requests received after the handshake do not trigger a no_renegotiation alert
301 * if the application accepts the renegotiation request
302 *
303 *= https://www.rfc-editor.org/rfc/rfc5746#5
304 *= type=test
305 *# TLS implementations SHOULD provide a mechanism to disable and enable
306 *# renegotiation.
307 */
308 {
309 struct s2n_test_reneg_req_ctx ctx = { .app_decision = S2N_RENEGOTIATE_ACCEPT };
310
311 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT),
312 s2n_connection_ptr_free);
313 EXPECT_NOT_NULL(client_conn);
314 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config_with_reneg_cb));
315 EXPECT_SUCCESS(s2n_config_set_renegotiate_request_cb(config_with_reneg_cb, s2n_test_reneg_req_cb, &ctx));
316
317 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER),
318 s2n_connection_ptr_free);
319 EXPECT_NOT_NULL(server_conn);
320 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
321
322 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
323 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
324 EXPECT_SUCCESS(s2n_connection_set_io_pair(client_conn, &io_pair));
325 EXPECT_SUCCESS(s2n_connection_set_io_pair(server_conn, &io_pair));
326
327 /* Complete the handshake */
328 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
329 EXPECT_TRUE(client_conn->secure_renegotiation);
330
331 /* Send the hello request message. */
332 EXPECT_OK(s2n_send_client_hello_request(server_conn));
333
334 /* no_renegotation alert NOT sent and received */
335 EXPECT_OK(s2n_send_and_recv_test(server_conn, client_conn));
336 EXPECT_OK(s2n_send_and_recv_test(client_conn, server_conn));
337
338 /* Callback triggered */
339 EXPECT_NOT_NULL(client_conn->config->renegotiate_request_cb);
340 EXPECT_EQUAL(ctx.call_count, 1);
341 }
342
343 /* Test: Hello requests received after the handshake do not trigger a no_renegotiation alert
344 * if the application ignores the renegotiation request
345 */
346 {
347 struct s2n_test_reneg_req_ctx ctx = { .app_decision = S2N_RENEGOTIATE_IGNORE };
348
349 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT),
350 s2n_connection_ptr_free);
351 EXPECT_NOT_NULL(client_conn);
352 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config_with_reneg_cb));
353 EXPECT_SUCCESS(s2n_config_set_renegotiate_request_cb(config_with_reneg_cb, s2n_test_reneg_req_cb, &ctx));
354
355 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER),
356 s2n_connection_ptr_free);
357 EXPECT_NOT_NULL(server_conn);
358 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
359
360 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
361 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
362 EXPECT_SUCCESS(s2n_connection_set_io_pair(client_conn, &io_pair));
363 EXPECT_SUCCESS(s2n_connection_set_io_pair(server_conn, &io_pair));
364
365 /* Complete the handshake */
366 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
367 EXPECT_TRUE(client_conn->secure_renegotiation);
368
369 /* Send the hello request message. */
370 EXPECT_OK(s2n_send_client_hello_request(server_conn));
371
372 /* no_renegotation alert NOT sent and received */
373 EXPECT_OK(s2n_send_and_recv_test(server_conn, client_conn));
374 EXPECT_OK(s2n_send_and_recv_test(client_conn, server_conn));
375
376 /* Callback triggered */
377 EXPECT_NOT_NULL(client_conn->config->renegotiate_request_cb);
378 EXPECT_EQUAL(ctx.call_count, 1);
379 }
380
381 /* Test: Hello requests received after the handshake trigger a no_renegotiation alert
382 * if secure renegotiation is not supported, even if the application would have accepted the request.
383 *
384 *= https://www.rfc-editor.org/rfc/rfc5746#section-4.2
385 *= type=test
386 *# This text applies if the connection's "secure_renegotiation" flag is
387 *# set to FALSE.
388 *#
389 *# It is possible that un-upgraded servers will request that the client
390 *# renegotiate. It is RECOMMENDED that clients refuse this
391 *# renegotiation request. Clients that do so MUST respond to such
392 *# requests with a "no_renegotiation" alert (RFC 5246 requires this
393 *# alert to be at the "warning" level). It is possible that the
394 *# apparently un-upgraded server is in fact an attacker who is then
395 *# allowing the client to renegotiate with a different, legitimate,
396 *# upgraded server.
397 **/
398 {
399 struct s2n_test_reneg_req_ctx ctx = { .app_decision = S2N_RENEGOTIATE_ACCEPT };
400
401 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT),
402 s2n_connection_ptr_free);
403 EXPECT_NOT_NULL(client_conn);
404 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config_with_reneg_cb));
405 EXPECT_SUCCESS(s2n_config_set_renegotiate_request_cb(config_with_reneg_cb, s2n_test_reneg_req_cb, &ctx));
406
407 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER),
408 s2n_connection_ptr_free);
409 EXPECT_NOT_NULL(server_conn);
410 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
411
412 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
413 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
414 EXPECT_SUCCESS(s2n_connection_set_io_pair(client_conn, &io_pair));
415 EXPECT_SUCCESS(s2n_connection_set_io_pair(server_conn, &io_pair));
416
417 /* Complete the handshake */
418 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
419
420 /* Force secure_renegotiation to be false */
421 client_conn->secure_renegotiation = false;
422
423 /* Send the hello request message. */
424 EXPECT_OK(s2n_send_client_hello_request(server_conn));
425
426 /* no_renegotation alert sent and received */
427 EXPECT_OK(s2n_send_and_recv_test(server_conn, client_conn));
428 EXPECT_ERROR_WITH_ERRNO(s2n_send_and_recv_test(client_conn, server_conn), S2N_ERR_ALERT);
429 EXPECT_EQUAL(s2n_connection_get_alert(server_conn), S2N_TLS_ALERT_NO_RENEGOTIATION);
430
431 /* Callback was not triggered */
432 EXPECT_NOT_NULL(client_conn->config->renegotiate_request_cb);
433 EXPECT_EQUAL(ctx.call_count, 0);
434 }
435
436 /* Test: Application callback method fails */
437 {
438 struct s2n_test_reneg_req_ctx ctx = { .app_decision = S2N_RENEGOTIATE_ACCEPT };
439
440 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT),
441 s2n_connection_ptr_free);
442 EXPECT_NOT_NULL(client_conn);
443 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config_with_reneg_cb));
444 EXPECT_SUCCESS(s2n_config_set_renegotiate_request_cb(config_with_reneg_cb, s2n_test_reneg_req_cb, &ctx));
445
446 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER),
447 s2n_connection_ptr_free);
448 EXPECT_NOT_NULL(server_conn);
449 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
450
451 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
452 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
453 EXPECT_SUCCESS(s2n_connection_set_io_pair(client_conn, &io_pair));
454 EXPECT_SUCCESS(s2n_connection_set_io_pair(server_conn, &io_pair));
455
456 /* Complete the handshake */
457 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
458
459 /* Send the hello request message. */
460 EXPECT_OK(s2n_send_client_hello_request(server_conn));
461
462 /* Force the callback to fail.
463 * We're only allowed to call it UINT8_MAX times, so calling
464 * one more time will cause it to fail with S2N_ERR_SAFETY.
465 */
466 EXPECT_NOT_NULL(client_conn->config->renegotiate_request_cb);
467 ctx.call_count = UINT8_MAX;
468
469 /* s2n_recv should not surface the callback error as S2N_ERR_SAFETY.
470 * Applications won't be able to set s2n_errno to a meaningful value,
471 * so we need to set it to S2N_ERR_CANCELED for them.
472 */
473 EXPECT_ERROR_WITH_ERRNO(s2n_send_and_recv_test(server_conn, client_conn), S2N_ERR_CANCELLED);
474 };
475
476 /* Test: SSLv3 sends a fatal handshake_failure alert instead of no_renegotiate
477 *
478 *= https://www.rfc-editor.org/rfc/rfc5746#4.5
479 *= type=test
480 *# SSLv3 does not define the "no_renegotiation" alert (and does
481 *# not offer a way to indicate a refusal to renegotiate at a "warning"
482 *# level). SSLv3 clients that refuse renegotiation SHOULD use a fatal
483 *# handshake_failure alert.
484 **/
485 if (s2n_hash_is_available(S2N_HASH_MD5)) {
486 struct s2n_test_reneg_req_ctx ctx = { .app_decision = S2N_RENEGOTIATE_REJECT };
487
488 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT),
489 s2n_connection_ptr_free);
490 EXPECT_NOT_NULL(client_conn);
491 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config_with_reneg_cb));
492 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(client_conn, "test_all"));
493 EXPECT_SUCCESS(s2n_config_set_renegotiate_request_cb(config_with_reneg_cb, s2n_test_reneg_req_cb, &ctx));
494 EXPECT_SUCCESS(s2n_connection_set_blinding(client_conn, S2N_SELF_SERVICE_BLINDING));
495
496 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER),
497 s2n_connection_ptr_free);
498 EXPECT_NOT_NULL(server_conn);
499 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
500 EXPECT_SUCCESS(s2n_connection_set_cipher_preferences(server_conn, "test_all"));
501
502 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
503 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
504 EXPECT_SUCCESS(s2n_connections_set_io_pair(client_conn, server_conn, &io_pair));
505
506 uint8_t buffer[1] = { 0 };
507 s2n_blocked_status blocked = S2N_NOT_BLOCKED;
508
509 /* Force an SSLv3 handshake */
510 client_conn->client_protocol_version = S2N_SSLv3;
511 client_conn->actual_protocol_version = S2N_SSLv3;
512 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
513 EXPECT_EQUAL(client_conn->actual_protocol_version, S2N_SSLv3);
514 EXPECT_EQUAL(server_conn->actual_protocol_version, S2N_SSLv3);
515
516 /* Send the hello request message. */
517 EXPECT_OK(s2n_send_client_hello_request(server_conn));
518
519 /* handshake_failure alert queued */
520 EXPECT_FAILURE_WITH_ERRNO(s2n_recv(client_conn, buffer, sizeof(buffer), &blocked), S2N_ERR_BAD_MESSAGE);
521 EXPECT_TRUE(s2n_connection_check_io_status(client_conn, S2N_IO_CLOSED));
522
523 /* handshake_failure alert send.
524 * Skip blinding. */
525 EXPECT_TRUE(client_conn->delay > 0);
526 client_conn->delay = 0;
527 EXPECT_SUCCESS(s2n_shutdown_send(client_conn, &blocked));
528
529 /* handshake_failure alert received */
530 EXPECT_FAILURE_WITH_ERRNO(s2n_recv(server_conn, buffer, sizeof(buffer), &blocked), S2N_ERR_ALERT);
531 EXPECT_TRUE(s2n_connection_check_io_status(server_conn, S2N_IO_CLOSED));
532 EXPECT_EQUAL(s2n_connection_get_alert(server_conn), S2N_TLS_ALERT_HANDSHAKE_FAILURE);
533
534 /* Callback triggered */
535 EXPECT_NOT_NULL(client_conn->config->renegotiate_request_cb);
536 EXPECT_EQUAL(ctx.call_count, 1);
537 }
538
539 EXPECT_SUCCESS(s2n_cert_chain_and_key_free(chain_and_key));
540 END_TEST();
541 }