Debian Codegraph @

crt/s2n/tests/unit/s2n_self_talk_npn_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_client_hello.c"
19
20 /* The server will always prefer to negotiate an application protocol with
21 * the ALPN extension. This callback wipes the ALPN extension from the client
22 * hello and forces the server to negotiate a protocol using the NPN extension
23 * instead. */
24 static int s2n_wipe_alpn_ext(struct s2n_connection *conn, void *ctx)
25 {
26 struct s2n_client_hello *client_hello = s2n_connection_get_client_hello(conn);
27 POSIX_ENSURE_REF(client_hello);
28 s2n_parsed_extension *parsed_extension = NULL;
29 POSIX_GUARD(s2n_client_hello_get_parsed_extension(S2N_EXTENSION_ALPN, &client_hello->extensions, &parsed_extension));
30 POSIX_ENSURE_REF(parsed_extension);
31 POSIX_GUARD(s2n_blob_zero(&parsed_extension->extension));
32
33 return S2N_SUCCESS;
34 }
35
36 int main(int argc, char **argv)
37 {
38 BEGIN_TEST();
39
40 const char *protocols[] = { "http/1.1", "spdy/1", "spdy/2" };
41 const uint8_t protocols_count = s2n_array_len(protocols);
42
43 /* Set up connections */
44 DEFER_CLEANUP(struct s2n_connection *server_conn = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free);
45 EXPECT_NOT_NULL(server_conn);
46 DEFER_CLEANUP(struct s2n_connection *client_conn = s2n_connection_new(S2N_CLIENT), s2n_connection_ptr_free);
47 EXPECT_NOT_NULL(client_conn);
48
49 /* Set up config */
50 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
51 EXPECT_NOT_NULL(config);
52 EXPECT_SUCCESS(s2n_config_set_unsafe_for_testing(config));
53 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "20240501"));
54 struct s2n_cert_chain_and_key *chain_and_key = NULL;
55 EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&chain_and_key, S2N_DEFAULT_TEST_CERT_CHAIN, S2N_DEFAULT_TEST_PRIVATE_KEY));
56 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key));
57 EXPECT_SUCCESS(s2n_config_set_protocol_preferences(config, protocols, protocols_count));
58 config->npn_supported = true;
59
60 /* Set up config that wipes ALPN extension */
61 DEFER_CLEANUP(struct s2n_config *npn_config = s2n_config_new(), s2n_config_ptr_free);
62 EXPECT_NOT_NULL(npn_config);
63 EXPECT_SUCCESS(s2n_config_set_unsafe_for_testing(npn_config));
64 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(npn_config, "20240501"));
65 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(npn_config, chain_and_key));
66 EXPECT_SUCCESS(s2n_config_set_protocol_preferences(npn_config, protocols, protocols_count));
67 EXPECT_SUCCESS(s2n_config_set_client_hello_cb(npn_config, s2n_wipe_alpn_ext, NULL));
68 npn_config->npn_supported = true;
69
70 EXPECT_SUCCESS(s2n_connection_set_blinding(client_conn, S2N_SELF_SERVICE_BLINDING));
71 EXPECT_SUCCESS(s2n_connection_set_blinding(server_conn, S2N_SELF_SERVICE_BLINDING));
72
73 /* Client and server both support NPN. ALPN is negotiated since it is also
74 * supported and the server prefers ALPN. */
75 {
76 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, config));
77 EXPECT_SUCCESS(s2n_connection_set_config(client_conn, config));
78
79 /* Create nonblocking pipes */
80 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
81 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
82 EXPECT_SUCCESS(s2n_connection_set_io_pair(server_conn, &io_pair));
83 EXPECT_SUCCESS(s2n_connection_set_io_pair(client_conn, &io_pair));
84
85 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
86
87 /* Server prefers ALPN over NPN */
88 EXPECT_FALSE(IS_NPN_HANDSHAKE(server_conn));
89 EXPECT_FALSE(IS_NPN_HANDSHAKE(client_conn));
90
91 /* ALPN has negotiated a protocol */
92 EXPECT_NOT_NULL(s2n_get_application_protocol(client_conn));
93 EXPECT_BYTEARRAY_EQUAL(s2n_get_application_protocol(client_conn), protocols[0], strlen(protocols[0]));
94 EXPECT_NOT_NULL(s2n_get_application_protocol(server_conn));
95 EXPECT_BYTEARRAY_EQUAL(s2n_get_application_protocol(server_conn), protocols[0], strlen(protocols[0]));
96
97 EXPECT_SUCCESS(s2n_shutdown_test_server_and_client(server_conn, client_conn));
98 EXPECT_SUCCESS(s2n_connection_wipe(client_conn));
99 EXPECT_SUCCESS(s2n_connection_wipe(server_conn));
100 };
101
102 /* Client and server both support NPN. Wipe ALPN with the Client Hello callback so only NPN is received.
103 * NPN is negotiated and not ALPN. */
104 {
105 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, npn_config));
106
107 /* Create nonblocking pipes */
108 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
109 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
110 EXPECT_SUCCESS(s2n_connection_set_io_pair(server_conn, &io_pair));
111 EXPECT_SUCCESS(s2n_connection_set_io_pair(client_conn, &io_pair));
112
113 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
114
115 EXPECT_TRUE(IS_NPN_HANDSHAKE(server_conn));
116 EXPECT_TRUE(IS_NPN_HANDSHAKE(client_conn));
117
118 /* NPN has negotiated a protocol */
119 EXPECT_NOT_NULL(s2n_get_application_protocol(client_conn));
120 EXPECT_BYTEARRAY_EQUAL(s2n_get_application_protocol(client_conn), protocols[0], strlen(protocols[0]));
121 EXPECT_NOT_NULL(s2n_get_application_protocol(server_conn));
122 EXPECT_BYTEARRAY_EQUAL(s2n_get_application_protocol(server_conn), protocols[0], strlen(protocols[0]));
123
124 EXPECT_SUCCESS(s2n_shutdown_test_server_and_client(server_conn, client_conn));
125 EXPECT_SUCCESS(s2n_connection_wipe(client_conn));
126 EXPECT_SUCCESS(s2n_connection_wipe(server_conn));
127 };
128
129 /* Client and server both support NPN, however, they have no protocols in common.
130 * Connection negotiates client's top protocol. */
131 {
132 /* Config with different protocols */
133 const char *server_protocols[] = { "h2", "h3" };
134 const uint8_t server_protocols_count = s2n_array_len(server_protocols);
135 DEFER_CLEANUP(struct s2n_config *different_config = s2n_config_new(), s2n_config_ptr_free);
136 EXPECT_NOT_NULL(different_config);
137 EXPECT_SUCCESS(s2n_config_set_unsafe_for_testing(different_config));
138 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(different_config, "20240501"));
139 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(different_config, chain_and_key));
140 EXPECT_SUCCESS(s2n_config_set_protocol_preferences(different_config, server_protocols, server_protocols_count));
141 EXPECT_SUCCESS(s2n_config_set_client_hello_cb(different_config, s2n_wipe_alpn_ext, NULL));
142 different_config->npn_supported = true;
143 EXPECT_SUCCESS(s2n_connection_set_config(server_conn, different_config));
144
145 /* Create nonblocking pipes */
146 DEFER_CLEANUP(struct s2n_test_io_pair io_pair = { 0 }, s2n_io_pair_close);
147 EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair));
148 EXPECT_SUCCESS(s2n_connection_set_io_pair(server_conn, &io_pair));
149 EXPECT_SUCCESS(s2n_connection_set_io_pair(client_conn, &io_pair));
150
151 EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server_conn, client_conn));
152
153 EXPECT_TRUE(IS_NPN_HANDSHAKE(server_conn));
154 EXPECT_TRUE(IS_NPN_HANDSHAKE(client_conn));
155
156 /* Client-preference protocol is chosen since server and client have no mutually supported protocols. */
157 EXPECT_NOT_NULL(s2n_get_application_protocol(client_conn));
158 EXPECT_BYTEARRAY_EQUAL(s2n_get_application_protocol(client_conn), protocols[0], strlen(protocols[0]));
159 EXPECT_NOT_NULL(s2n_get_application_protocol(server_conn));
160 EXPECT_BYTEARRAY_EQUAL(s2n_get_application_protocol(server_conn), protocols[0], strlen(protocols[0]));
161
162 EXPECT_SUCCESS(s2n_shutdown_test_server_and_client(server_conn, client_conn));
163 EXPECT_SUCCESS(s2n_connection_wipe(client_conn));
164 EXPECT_SUCCESS(s2n_connection_wipe(server_conn));
165 };
166
167 EXPECT_SUCCESS(s2n_cert_chain_and_key_free(chain_and_key));
168
169 END_TEST();
170 }