Debian Codegraph @

crt/s2n/tests/integrationv2/test_npn.py

raw symbols SCIP
1 # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 # SPDX-License-Identifier: Apache-2.0
3 import copy
4 import pytest
5
6 from configuration import (
7 available_ports,
8 ALL_TEST_CIPHERS,
9 ALL_TEST_CURVES,
10 MINIMAL_TEST_CERTS,
11 PROTOCOLS,
12 )
13 from common import ProviderOptions, Protocols
14 from fixtures import managed_process # noqa: F401
15 from providers import OpenSSL, S2N, Provider
16 from utils import invalid_test_parameters, get_parameter_name, to_bytes
17
18
19 # NPN not supported in TLS1.3
20 TLS_PROTOCOLS = [x for x in PROTOCOLS if x.value < Protocols.TLS13.value]
21
22 # Output indicating NPN status
23 S2N_NPN_MARKER = "WITH_NPN"
24 S2N_APPLICATION_MARKER = "Application protocol: "
25 OPENSSL_SERVER_NPN_MARKER = "NEXTPROTO is "
26 # The OpenSSL client uses "(1)" to indicate that a protocol was selected from
27 # the server's advertised list. "(2)" indicates the client couldn't find any overlap
28 # with the server's list and had to select from its own list.
29 OPENSSL_CLIENT_NPN_MARKER = "Next protocol: (1) "
30 OPENSSL_CLIENT_NPN_NO_OVERLAP_MARKER = "Next protocol: (2) "
31
32 # Test lists
33 PROTOCOL_LIST = "http/1.1,h2,h3"
34 PROTOCOL_LIST_ALT_ORDER = "h2,h3,http/1.1"
35 PROTOCOL_LIST_NO_OVERLAP = "spdy"
36
37
38 def s2n_client_npn_handshake(
39 managed_process, # noqa: F811
40 cipher,
41 curve,
42 certificate,
43 protocol,
44 provider,
45 server_list,
46 ):
47 options = ProviderOptions(
48 port=next(available_ports),
49 cipher=cipher,
50 curve=curve,
51 key=certificate.key,
52 cert=certificate.cert,
53 protocol=protocol,
54 insecure=True,
55 )
56
57 client_options = copy.copy(options)
58 client_options.mode = Provider.ClientMode
59 # Flags to turn on NPN for s2nc
60 client_options.extra_flags = ["--alpn", PROTOCOL_LIST, "--npn"]
61
62 server_options = copy.copy(options)
63 server_options.mode = Provider.ServerMode
64 # Flags to turn on NPN for OpenSSL server
65 server_options.extra_flags = ["-nextprotoneg", server_list]
66
67 server = managed_process(provider, server_options, timeout=5)
68 s2n_client = managed_process(S2N, client_options, timeout=5)
69
70 return (s2n_client, server)
71
72
73 """
74 The s2n-tls client successfully negotiates an application protocol using NPN.
75 """
76
77
78 @pytest.mark.uncollect_if(func=invalid_test_parameters)
79 @pytest.mark.parametrize("cipher", ALL_TEST_CIPHERS, ids=get_parameter_name)
80 @pytest.mark.parametrize("curve", ALL_TEST_CURVES, ids=get_parameter_name)
81 @pytest.mark.parametrize("certificate", MINIMAL_TEST_CERTS, ids=get_parameter_name)
82 @pytest.mark.parametrize("protocol", TLS_PROTOCOLS, ids=get_parameter_name)
83 @pytest.mark.parametrize("provider", [OpenSSL], ids=get_parameter_name)
84 def test_s2n_client_npn(
85 managed_process, # noqa: F811
86 cipher,
87 curve,
88 certificate,
89 protocol,
90 provider,
91 ):
92 s2n_client, server = s2n_client_npn_handshake(
93 managed_process,
94 cipher,
95 curve,
96 certificate,
97 protocol,
98 provider,
99 server_list=PROTOCOL_LIST,
100 )
101
102 expected_protocol = "http/1.1"
103
104 for results in server.get_results():
105 results.assert_success()
106 assert to_bytes(OPENSSL_SERVER_NPN_MARKER + expected_protocol) in results.stdout
107
108 for results in s2n_client.get_results():
109 results.assert_success()
110 assert to_bytes(S2N_NPN_MARKER) in results.stdout
111 assert to_bytes(S2N_APPLICATION_MARKER + expected_protocol) in results.stdout
112
113
114 """
115 The s2n-tls client chooses a server-preferred protocol.
116 """
117
118
119 @pytest.mark.uncollect_if(func=invalid_test_parameters)
120 @pytest.mark.parametrize("cipher", ALL_TEST_CIPHERS, ids=get_parameter_name)
121 @pytest.mark.parametrize("curve", ALL_TEST_CURVES, ids=get_parameter_name)
122 @pytest.mark.parametrize("certificate", MINIMAL_TEST_CERTS, ids=get_parameter_name)
123 @pytest.mark.parametrize("protocol", TLS_PROTOCOLS, ids=get_parameter_name)
124 @pytest.mark.parametrize("provider", [OpenSSL], ids=get_parameter_name)
125 def test_s2n_client_npn_server_preference(
126 managed_process, # noqa: F811
127 cipher,
128 curve,
129 certificate,
130 protocol,
131 provider,
132 ):
133 s2n_client, server = s2n_client_npn_handshake(
134 managed_process,
135 cipher,
136 curve,
137 certificate,
138 protocol,
139 provider,
140 server_list=PROTOCOL_LIST_ALT_ORDER,
141 )
142
143 expected_protocol = "h2"
144
145 for results in server.get_results():
146 results.assert_success()
147 assert to_bytes(OPENSSL_SERVER_NPN_MARKER + expected_protocol) in results.stdout
148
149 for results in s2n_client.get_results():
150 results.assert_success()
151 assert to_bytes(S2N_NPN_MARKER) in results.stdout
152 assert to_bytes(S2N_APPLICATION_MARKER + expected_protocol) in results.stdout
153
154
155 """
156 The s2n-tls client chooses its preferred protocol since there is no overlap.
157 """
158
159
160 @pytest.mark.uncollect_if(func=invalid_test_parameters)
161 @pytest.mark.parametrize("cipher", ALL_TEST_CIPHERS, ids=get_parameter_name)
162 @pytest.mark.parametrize("curve", ALL_TEST_CURVES, ids=get_parameter_name)
163 @pytest.mark.parametrize("certificate", MINIMAL_TEST_CERTS, ids=get_parameter_name)
164 @pytest.mark.parametrize("protocol", TLS_PROTOCOLS, ids=get_parameter_name)
165 @pytest.mark.parametrize("provider", [OpenSSL], ids=get_parameter_name)
166 def test_s2n_client_npn_no_overlap(
167 managed_process, # noqa: F811
168 cipher,
169 curve,
170 certificate,
171 protocol,
172 provider,
173 ):
174 s2n_client, server = s2n_client_npn_handshake(
175 managed_process,
176 cipher,
177 curve,
178 certificate,
179 protocol,
180 provider,
181 server_list=PROTOCOL_LIST_NO_OVERLAP,
182 )
183
184 expected_protocol = "http/1.1"
185
186 for results in server.get_results():
187 results.assert_success()
188 assert to_bytes(OPENSSL_SERVER_NPN_MARKER + expected_protocol) in results.stdout
189
190 for results in s2n_client.get_results():
191 results.assert_success()
192 assert to_bytes(S2N_NPN_MARKER) in results.stdout
193 assert to_bytes(S2N_APPLICATION_MARKER + expected_protocol) in results.stdout
194
195
196 def s2n_server_npn_handshake(
197 managed_process, # noqa: F811
198 cipher,
199 curve,
200 certificate,
201 protocol,
202 provider,
203 server_list,
204 ):
205 options = ProviderOptions(
206 port=next(available_ports),
207 cipher=cipher,
208 curve=curve,
209 key=certificate.key,
210 cert=certificate.cert,
211 protocol=protocol,
212 insecure=True,
213 )
214
215 client_options = copy.copy(options)
216 client_options.mode = Provider.ClientMode
217 # Flags to turn on NPN for OpenSSL client
218 client_options.extra_flags = ["-nextprotoneg", PROTOCOL_LIST]
219
220 server_options = copy.copy(options)
221 server_options.mode = Provider.ServerMode
222 # Flags to turn on NPN for s2nd.
223 server_options.extra_flags = ["--alpn", server_list, "--npn"]
224
225 s2n_server = managed_process(S2N, server_options, timeout=5)
226 client = managed_process(provider, client_options, timeout=5)
227
228 return (client, s2n_server)
229
230
231 """
232 The s2n-tls server successfully negotiates an application protocol using NPN.
233 """
234
235
236 @pytest.mark.uncollect_if(func=invalid_test_parameters)
237 @pytest.mark.parametrize("cipher", ALL_TEST_CIPHERS, ids=get_parameter_name)
238 @pytest.mark.parametrize("curve", ALL_TEST_CURVES, ids=get_parameter_name)
239 @pytest.mark.parametrize("certificate", MINIMAL_TEST_CERTS, ids=get_parameter_name)
240 @pytest.mark.parametrize("protocol", TLS_PROTOCOLS, ids=get_parameter_name)
241 @pytest.mark.parametrize("provider", [OpenSSL], ids=get_parameter_name)
242 def test_s2n_server_npn(
243 managed_process, # noqa: F811
244 cipher,
245 curve,
246 certificate,
247 protocol,
248 provider,
249 ):
250 # We only send one protocol on the s2n server
251 # due to the fact that it re-purposes the alpn list(which only sends one protocol)
252 # to work for the NPN list.
253 client, s2n_server = s2n_server_npn_handshake(
254 managed_process,
255 cipher,
256 curve,
257 certificate,
258 protocol,
259 provider,
260 server_list="http/1.1",
261 )
262
263 expected_protocol = "http/1.1"
264
265 for results in s2n_server.get_results():
266 results.assert_success()
267 assert to_bytes(S2N_NPN_MARKER) in results.stdout
268 assert to_bytes(S2N_APPLICATION_MARKER + expected_protocol) in results.stdout
269
270 for results in client.get_results():
271 results.assert_success()
272 assert to_bytes(OPENSSL_CLIENT_NPN_MARKER + expected_protocol) in results.stdout
273
274
275 """
276 The s2n-tls server can handle the case where there is no mutually supported protocol and
277 the client chooses its own protocol.
278 """
279
280
281 @pytest.mark.uncollect_if(func=invalid_test_parameters)
282 @pytest.mark.parametrize("cipher", ALL_TEST_CIPHERS, ids=get_parameter_name)
283 @pytest.mark.parametrize("curve", ALL_TEST_CURVES, ids=get_parameter_name)
284 @pytest.mark.parametrize("certificate", MINIMAL_TEST_CERTS, ids=get_parameter_name)
285 @pytest.mark.parametrize("protocol", TLS_PROTOCOLS, ids=get_parameter_name)
286 @pytest.mark.parametrize("provider", [OpenSSL], ids=get_parameter_name)
287 def test_s2n_server_npn_no_overlap(
288 managed_process, # noqa: F811
289 cipher,
290 curve,
291 certificate,
292 protocol,
293 provider,
294 ):
295 client, s2n_server = s2n_server_npn_handshake(
296 managed_process,
297 cipher,
298 curve,
299 certificate,
300 protocol,
301 provider,
302 server_list=PROTOCOL_LIST_NO_OVERLAP,
303 )
304
305 expected_protocol = "http/1.1"
306
307 for results in s2n_server.get_results():
308 results.assert_success()
309 assert to_bytes(S2N_NPN_MARKER) in results.stdout
310 assert to_bytes(S2N_APPLICATION_MARKER + expected_protocol) in results.stdout
311
312 for results in client.get_results():
313 results.assert_success()
314 assert (
315 to_bytes(OPENSSL_CLIENT_NPN_NO_OVERLAP_MARKER + expected_protocol)
316 in results.stdout
317 )