Debian Codegraph @

crt/s2n/tests/unit/s2n_self_talk_client_hello_cb_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 <fcntl.h>
17 #include <stdint.h>
18 #include <sys/wait.h>
19 #include <unistd.h>
20
21 #include "api/s2n.h"
22 #include "s2n_test.h"
23 #include "testlib/s2n_testlib.h"
24 #include "tls/s2n_connection.h"
25 #include "tls/s2n_internal.h"
26
27 struct client_hello_context {
28 int invoked;
29 int swap_config_during_callback;
30 int swap_config_nonblocking_mode;
31 int mark_done_during_callback;
32 struct s2n_config *config;
33 /* the right way to mark server name extenstion was used
34 * after parsing ClientHello is to call
35 * s2n_connection_server_name_extension_used
36 *
37 * this flag tests the previous behavior from blocking callbacks
38 */
39 int legacy_rc_for_server_name_used;
40 };
41
42 int mock_client(struct s2n_test_io_pair *io_pair, int expect_failure, int expect_server_name_used)
43 {
44 struct s2n_connection *conn = NULL;
45 struct s2n_config *config = NULL;
46 s2n_blocked_status blocked;
47 int result = 0;
48 int rc = 0;
49 const char *protocols[] = { "h2", "http/1.1" };
50
51 /* Give the server a chance to listen */
52 sleep(1);
53
54 conn = s2n_connection_new(S2N_CLIENT);
55 config = s2n_config_new();
56 EXPECT_OK(s2n_connection_set_tls12_security_policy(conn));
57 s2n_config_set_protocol_preferences(config, protocols, 2);
58 s2n_config_disable_x509_verification(config);
59 s2n_connection_set_config(conn, config);
60
61 s2n_connection_set_io_pair(conn, io_pair);
62
63 s2n_set_server_name(conn, "example.com");
64
65 rc = s2n_negotiate(conn, &blocked);
66 if (expect_failure) {
67 if (!rc) {
68 result = 1;
69 }
70
71 if (s2n_connection_get_alert(conn) != 40) {
72 result = 2;
73 }
74 } else {
75 char buffer[0xffff];
76
77 if (conn->server_name_used != expect_server_name_used) {
78 result = 1;
79 }
80
81 if (rc < 0) {
82 result = 2;
83 }
84
85 for (int i = 1; i < 0xffff; i += 100) {
86 memset(buffer, 33, sizeof(char) * i);
87 s2n_send(conn, buffer, i, &blocked);
88 }
89
90 int shutdown_rc = -1;
91 do {
92 shutdown_rc = s2n_shutdown(conn, &blocked);
93 } while (shutdown_rc != 0);
94 }
95
96 s2n_connection_free(conn);
97 s2n_config_free(config);
98
99 /* Give the server a chance to a void a sigpipe */
100 sleep(1);
101
102 s2n_cleanup();
103 s2n_io_pair_close_one_end(io_pair, S2N_CLIENT);
104
105 exit(result);
106 }
107
108 int client_hello_swap_config(struct s2n_connection *conn, void *ctx)
109 {
110 struct client_hello_context *client_hello_ctx = NULL;
111 struct s2n_client_hello *client_hello = s2n_connection_get_client_hello(conn);
112 const char *sent_server_name = "example.com";
113 const char *received_server_name = NULL;
114 if (ctx == NULL) {
115 return -1;
116 }
117 client_hello_ctx = ctx;
118 /* Increment counter to ensure that callback was invoked */
119 client_hello_ctx->invoked++;
120
121 /* Validate SNI extension */
122 uint8_t expected_server_name[] = {
123 /* Server names len */
124 0x00, 0x0E,
125 /* Server name type - host name */
126 0x00,
127 /* First server name len */
128 0x00, 0x0B,
129 /* First server name, matches sent_server_name */
130 'e', 'x', 'a', 'm', 'p', 'l', 'e', '.', 'c', 'o', 'm'
131 };
132
133 /* Get SNI extension from client hello */
134 uint32_t len = s2n_client_hello_get_extension_length(client_hello, S2N_EXTENSION_SERVER_NAME);
135 if (len != 16) {
136 return -1;
137 }
138
139 uint8_t ser_name[16] = { 0 };
140 if (s2n_client_hello_get_extension_by_id(client_hello, S2N_EXTENSION_SERVER_NAME, ser_name, len) <= 0) {
141 return -1;
142 }
143
144 /* Verify correct server name is returned. */
145 received_server_name = s2n_get_server_name(conn);
146 if (received_server_name == NULL || strcmp(received_server_name, sent_server_name)) {
147 return -1;
148 }
149
150 if (memcmp(ser_name, expected_server_name, len) != 0) {
151 return -1;
152 }
153
154 if (client_hello_ctx->mark_done_during_callback) {
155 EXPECT_SUCCESS(s2n_client_hello_cb_done(conn));
156 }
157
158 if (client_hello_ctx->swap_config_during_callback) {
159 EXPECT_SUCCESS(s2n_connection_set_config(conn, client_hello_ctx->config));
160 if (client_hello_ctx->legacy_rc_for_server_name_used) {
161 return 1;
162 }
163 EXPECT_SUCCESS(s2n_connection_server_name_extension_used(conn));
164 return 0;
165 }
166
167 return 0;
168 }
169
170 int client_hello_fail_handshake(struct s2n_connection *conn, void *ctx)
171 {
172 struct client_hello_context *client_hello_ctx = NULL;
173
174 if (ctx == NULL) {
175 return -1;
176 }
177 client_hello_ctx = ctx;
178
179 /* Incremet counter to ensure that callback was invoked */
180 client_hello_ctx->invoked++;
181
182 /* Return negative value to terminate the handshake */
183 return -1;
184 }
185
186 int s2n_negotiate_nonblocking_ch_cb(struct s2n_connection *conn,
187 struct client_hello_context *ch_ctx, bool server_name_used)
188 {
189 s2n_blocked_status blocked;
190 EXPECT_NOT_NULL(conn);
191 /* negotiate handshake, we should pause after the nonblocking callback is invoked */
192 EXPECT_FAILURE_WITH_ERRNO(s2n_negotiate(conn, &blocked), S2N_ERR_ASYNC_BLOCKED);
193 EXPECT_EQUAL(blocked, S2N_BLOCKED_ON_APPLICATION_INPUT);
194
195 /* verify client hello cb has been invoked */
196 EXPECT_EQUAL(ch_ctx->invoked, 1);
197
198 /* while handshake is paused, swap the config if asked */
199 if (ch_ctx->swap_config_nonblocking_mode) {
200 EXPECT_SUCCESS(s2n_connection_set_config(conn, ch_ctx->config));
201 }
202 /* unless explicitly unblocked we should stay paused */
203 EXPECT_FAILURE_WITH_ERRNO(s2n_negotiate(conn, &blocked), S2N_ERR_ASYNC_BLOCKED);
204 EXPECT_EQUAL(blocked, S2N_BLOCKED_ON_APPLICATION_INPUT);
205
206 /* mark the client hello cb complete */
207 EXPECT_SUCCESS(s2n_client_hello_cb_done(conn));
208 if (server_name_used) {
209 EXPECT_SUCCESS(s2n_connection_server_name_extension_used(conn));
210 }
211 return s2n_negotiate(conn, &blocked);
212 }
213
214 int s2n_negotiate_blocking_ch_cb(struct s2n_connection *conn, struct client_hello_context *ch_ctx)
215 {
216 s2n_blocked_status blocked;
217 EXPECT_NOT_NULL(conn);
218
219 int rc = s2n_negotiate(conn, &blocked);
220 /* verify client hello cb has been invoked */
221 EXPECT_EQUAL(ch_ctx->invoked, 1);
222 return rc;
223 }
224
225 int server_recv(struct s2n_connection *conn)
226 {
227 static char buffer[0xffff];
228 s2n_blocked_status blocked;
229
230 for (int i = 1; i < 0xffff; i += 100) {
231 char *ptr = buffer;
232 int size = i;
233
234 do {
235 int bytes_read = 0;
236 EXPECT_SUCCESS(bytes_read = s2n_recv(conn, ptr, size, &blocked));
237
238 size -= bytes_read;
239 ptr += bytes_read;
240 } while (size);
241
242 for (int j = 0; j < i; j++) {
243 EXPECT_EQUAL((buffer)[j], 33);
244 }
245 }
246 return S2N_SUCCESS;
247 }
248
249 int init_server_conn(struct s2n_connection **conn, struct s2n_test_io_pair *io_pair,
250 struct s2n_config *config)
251 {
252 /* This is the server process, close the client end of the pipe */
253 EXPECT_SUCCESS(s2n_io_pair_close_one_end(io_pair, S2N_CLIENT));
254
255 EXPECT_NOT_NULL(*conn = s2n_connection_new(S2N_SERVER));
256
257 /* Set up the connection to read from the fd */
258 EXPECT_SUCCESS(s2n_connection_set_io_pair(*conn, io_pair));
259 EXPECT_SUCCESS(s2n_connection_set_config(*conn, config));
260 return S2N_SUCCESS;
261 }
262
263 int start_client_conn(struct s2n_test_io_pair *io_pair, pid_t *pid,
264 int expect_failure, int expect_server_name_used)
265 {
266 /* Create a pipe */
267 EXPECT_SUCCESS(s2n_io_pair_init(io_pair));
268
269 /* Create a child process */
270 *pid = fork();
271 if (*pid == 0) {
272 /* This is the client process, close the server end of the pipe */
273 EXPECT_SUCCESS(s2n_io_pair_close_one_end(io_pair, S2N_SERVER));
274
275 mock_client(io_pair, expect_failure, expect_server_name_used);
276 }
277 return S2N_SUCCESS;
278 }
279
280 static int test_case_clean(struct s2n_connection *conn, pid_t client_pid,
281 struct s2n_config *config, struct s2n_test_io_pair *io_pair,
282 struct client_hello_context *ch_ctx, struct s2n_cert_chain_and_key *chain_and_key)
283 {
284 s2n_blocked_status blocked;
285 int status = 0;
286
287 EXPECT_SUCCESS(s2n_shutdown(conn, &blocked));
288 EXPECT_EQUAL(waitpid(-1, &status, 0), client_pid);
289 EXPECT_EQUAL(status, 0);
290 /* client process cleans their end, we just need to close server side */
291 EXPECT_SUCCESS(s2n_io_pair_close_one_end(io_pair, S2N_SERVER));
292
293 EXPECT_SUCCESS(s2n_connection_free(conn));
294 EXPECT_SUCCESS(s2n_config_free(config));
295 memset(ch_ctx, 0, sizeof(struct client_hello_context));
296 EXPECT_SUCCESS(s2n_cert_chain_and_key_free(chain_and_key));
297
298 return S2N_SUCCESS;
299 }
300
301 int run_test_config_swap_ch_cb(s2n_client_hello_cb_mode cb_mode,
302 struct client_hello_context *ch_ctx)
303 {
304 struct s2n_test_io_pair io_pair;
305 struct s2n_config *config = NULL;
306 struct s2n_connection *conn = NULL;
307 struct s2n_config *swap_config = NULL;
308 pid_t pid = 0;
309 struct s2n_cert_chain_and_key *chain_and_key = NULL;
310
311 EXPECT_SUCCESS(start_client_conn(&io_pair, &pid, 0, 1));
312
313 /* Add application protocols to swapped config */
314 static const char *protocols[] = { "h2" };
315 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_and_key, S2N_DEFAULT_TEST_CERT_CHAIN, S2N_DEFAULT_TEST_PRIVATE_KEY));
316
317 /* prepare swap_config */
318 EXPECT_NOT_NULL(swap_config = s2n_config_new());
319 EXPECT_OK(s2n_config_set_tls12_security_policy(swap_config));
320 EXPECT_SUCCESS(s2n_config_set_protocol_preferences(swap_config, protocols, 1));
321 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(swap_config, chain_and_key));
322 ch_ctx->config = swap_config;
323 /* in the swap config make sure blocking more is SET correctly */
324 EXPECT_SUCCESS(s2n_config_set_client_hello_cb_mode(swap_config, cb_mode));
325
326 /* Don't set up certificate and private key for the main config, so if
327 * handshake succeeds we know that config was swapped */
328 EXPECT_NOT_NULL(config = s2n_config_new());
329
330 /* Set up the callback */
331 EXPECT_SUCCESS(s2n_config_set_client_hello_cb_mode(config, cb_mode));
332 EXPECT_SUCCESS(s2n_config_set_client_hello_cb(config, client_hello_swap_config, ch_ctx));
333
334 EXPECT_SUCCESS(init_server_conn(&conn, &io_pair, config));
335
336 /* do the handshake */
337 if (cb_mode == S2N_CLIENT_HELLO_CB_NONBLOCKING && !ch_ctx->mark_done_during_callback) {
338 /* swap the config and mark server_name_used in the async context */
339 EXPECT_SUCCESS(s2n_negotiate_nonblocking_ch_cb(conn, ch_ctx, true));
340 } else {
341 /* cb_mode == S2N_CLIENT_HELLO_CB_BLOCKING or NONBLOCKING mode where
342 * a non blocking callback marks cb_done during the callback itself
343 */
344 EXPECT_SUCCESS(s2n_negotiate_blocking_ch_cb(conn, ch_ctx));
345 }
346
347 /* Server name and error are as expected with null connection */
348 EXPECT_NULL(s2n_get_server_name(NULL));
349 EXPECT_EQUAL(s2n_errno, S2N_ERR_NULL);
350
351 /* Expect most preferred negotiated protocol which only swap_config had */
352 EXPECT_STRING_EQUAL(s2n_get_application_protocol(conn), protocols[0]);
353
354 EXPECT_SUCCESS(server_recv(conn));
355
356 EXPECT_SUCCESS(test_case_clean(conn, pid, config, &io_pair, ch_ctx, chain_and_key));
357 EXPECT_SUCCESS(s2n_config_free(swap_config));
358 return S2N_SUCCESS;
359 }
360
361 int run_test_no_config_swap_ch_cb(s2n_client_hello_cb_mode cb_mode, struct client_hello_context *ch_ctx)
362 {
363 struct s2n_test_io_pair io_pair;
364 struct s2n_config *config = NULL;
365 struct s2n_connection *conn = NULL;
366 pid_t pid = 0;
367 struct s2n_cert_chain_and_key *chain_and_key = NULL;
368
369 EXPECT_SUCCESS(start_client_conn(&io_pair, &pid, 0, 0));
370
371 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_and_key, S2N_DEFAULT_TEST_CERT_CHAIN, S2N_DEFAULT_TEST_PRIVATE_KEY));
372 EXPECT_NOT_NULL(config = s2n_config_new());
373 EXPECT_OK(s2n_config_set_tls12_security_policy(config));
374 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key));
375
376 /* Setup ClientHello callback */
377 EXPECT_SUCCESS(s2n_config_set_client_hello_cb(config, client_hello_swap_config, ch_ctx));
378 EXPECT_SUCCESS(s2n_config_set_client_hello_cb_mode(config, cb_mode));
379 EXPECT_SUCCESS(init_server_conn(&conn, &io_pair, config));
380
381 /* do the handshake */
382 if (cb_mode == S2N_CLIENT_HELLO_CB_NONBLOCKING) {
383 /* swap the config and mark server_name_used in the async context */
384 EXPECT_SUCCESS(s2n_negotiate_nonblocking_ch_cb(conn, ch_ctx, false));
385 } else { /* cb_mode == S2N_CLIENT_HELLO_CB_BLOCKING */
386 EXPECT_SUCCESS(s2n_negotiate_blocking_ch_cb(conn, ch_ctx));
387 }
388
389 /* Server name and error are as expected with null connection */
390 EXPECT_NULL(s2n_get_server_name(NULL));
391 EXPECT_EQUAL(s2n_errno, S2N_ERR_NULL);
392
393 EXPECT_SUCCESS(server_recv(conn));
394
395 EXPECT_SUCCESS(test_case_clean(conn, pid, config, &io_pair, ch_ctx, chain_and_key));
396 return S2N_SUCCESS;
397 }
398
399 int run_test_reject_handshake_ch_cb(s2n_client_hello_cb_mode cb_mode, struct client_hello_context *ch_ctx)
400 {
401 struct s2n_test_io_pair io_pair;
402 struct s2n_config *config = NULL;
403 struct s2n_connection *conn = NULL;
404 pid_t pid = 0;
405 s2n_blocked_status blocked;
406 struct s2n_cert_chain_and_key *chain_and_key = NULL;
407
408 EXPECT_SUCCESS(start_client_conn(&io_pair, &pid, 1, 0));
409
410 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_and_key, S2N_DEFAULT_TEST_CERT_CHAIN, S2N_DEFAULT_TEST_PRIVATE_KEY));
411 EXPECT_NOT_NULL(config = s2n_config_new());
412 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key));
413
414 /* Setup ClientHello callback */
415 EXPECT_SUCCESS(s2n_config_set_client_hello_cb(config, client_hello_fail_handshake, ch_ctx));
416 EXPECT_SUCCESS(s2n_config_set_client_hello_cb_mode(config, cb_mode));
417
418 EXPECT_SUCCESS(init_server_conn(&conn, &io_pair, config));
419 /* If s2n_negotiate fails, it usually would delay with a sleep. In order to
420 * test that we don't blind when CLientHello callback fails the handshake,
421 * disable blinding here */
422 EXPECT_SUCCESS(s2n_connection_set_blinding(conn, S2N_SELF_SERVICE_BLINDING));
423
424 /* Negotiate the handshake. */
425 EXPECT_FAILURE_WITH_ERRNO(s2n_negotiate(conn, &blocked), S2N_ERR_CANCELLED);
426
427 /* Check that blinding was not invoked */
428 EXPECT_EQUAL(s2n_connection_get_delay(conn), 0);
429
430 /* Ensure that callback was invoked */
431 EXPECT_EQUAL(ch_ctx->invoked, 1);
432
433 /* shutdown to flush alert */
434 EXPECT_SUCCESS(test_case_clean(conn, pid, config, &io_pair, ch_ctx, chain_and_key));
435 return S2N_SUCCESS;
436 }
437
438 int main(int argc, char **argv)
439 {
440 struct client_hello_context client_hello_ctx = { 0 };
441 BEGIN_TEST();
442 /* Test config swapping in client hello callback */
443
444 /* we want to update the config outside of callback so don't swap in callback */
445 client_hello_ctx.swap_config_nonblocking_mode = 1;
446 EXPECT_SUCCESS(run_test_config_swap_ch_cb(S2N_CLIENT_HELLO_CB_NONBLOCKING, &client_hello_ctx));
447
448 /* non blocking callback when callback marks cb_done during the callback */
449 client_hello_ctx.swap_config_during_callback = 1;
450 client_hello_ctx.mark_done_during_callback = 1;
451 EXPECT_SUCCESS(run_test_config_swap_ch_cb(S2N_CLIENT_HELLO_CB_NONBLOCKING, &client_hello_ctx));
452
453 /* we want to update the config in the callback */
454 client_hello_ctx.swap_config_during_callback = 1;
455 EXPECT_SUCCESS(run_test_config_swap_ch_cb(S2N_CLIENT_HELLO_CB_BLOCKING, &client_hello_ctx));
456
457 /* validate legacy behavior for server_name_used */
458 /* we want to update the config in the callback */
459 client_hello_ctx.swap_config_during_callback = 1;
460 client_hello_ctx.legacy_rc_for_server_name_used = 1;
461 EXPECT_SUCCESS(run_test_config_swap_ch_cb(S2N_CLIENT_HELLO_CB_BLOCKING, &client_hello_ctx));
462
463 /* Tests for test when server_name_used is not set */
464 EXPECT_SUCCESS(run_test_no_config_swap_ch_cb(S2N_CLIENT_HELLO_CB_BLOCKING, &client_hello_ctx));
465
466 EXPECT_SUCCESS(run_test_no_config_swap_ch_cb(S2N_CLIENT_HELLO_CB_NONBLOCKING, &client_hello_ctx));
467
468 /* Test rejecting connection in client hello callback */
469 EXPECT_SUCCESS(run_test_reject_handshake_ch_cb(S2N_CLIENT_HELLO_CB_BLOCKING, &client_hello_ctx));
470
471 EXPECT_SUCCESS(run_test_reject_handshake_ch_cb(S2N_CLIENT_HELLO_CB_NONBLOCKING, &client_hello_ctx));
472
473 END_TEST();
474
475 return 0;
476 }