Debian Codegraph @

crt/s2n/tests/integrationv2/test_hello_retry_requests.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 import re
6
7 from configuration import (
8 available_ports,
9 TLS13_CIPHERS,
10 ALL_TEST_CURVES,
11 MINIMAL_TEST_CERTS,
12 )
13 from common import ProviderOptions, Protocols, data_bytes, Curves
14 from fixtures import managed_process # noqa: F401
15 from providers import Provider, S2N, OpenSSL
16 from utils import invalid_test_parameters, get_parameter_name, to_bytes
17
18 S2N_DEFAULT_CURVE = Curves.X25519
19 S2N_HRR_MARKER = to_bytes("HELLO_RETRY_REQUEST")
20
21 # Mapping list of curve_names for hello retry requests server side test.
22 CURVE_NAMES = {
23 "X25519": "x25519",
24 "P-256": "secp256r1",
25 "P-384": "secp384r1",
26 "P-521": "secp521r1",
27 }
28
29
30 def test_nothing():
31 """
32 Sometimes the hello retry test parameters in combination with the s2n libcrypto
33 results in no test cases existing. In this case, pass a nothing test to avoid
34 marking the entire codebuild run as failed.
35 """
36 assert True
37
38
39 @pytest.mark.uncollect_if(func=invalid_test_parameters)
40 @pytest.mark.parametrize("cipher", TLS13_CIPHERS, ids=get_parameter_name)
41 @pytest.mark.parametrize("provider", [OpenSSL])
42 @pytest.mark.parametrize("other_provider", [S2N], ids=get_parameter_name)
43 @pytest.mark.parametrize("curve", ALL_TEST_CURVES, ids=get_parameter_name)
44 @pytest.mark.parametrize("protocol", [Protocols.TLS13], ids=get_parameter_name)
45 @pytest.mark.parametrize("certificate", MINIMAL_TEST_CERTS, ids=get_parameter_name)
46 def test_hrr_with_s2n_as_client(
47 managed_process, # noqa: F811
48 cipher,
49 provider,
50 other_provider,
51 curve,
52 protocol,
53 certificate,
54 ):
55 if curve == S2N_DEFAULT_CURVE:
56 pytest.skip("No retry if server curve matches client curve")
57
58 port = next(available_ports)
59
60 random_bytes = data_bytes(64)
61 client_options = ProviderOptions(
62 mode=Provider.ClientMode,
63 port=port,
64 cipher=cipher,
65 data_to_send=random_bytes,
66 insecure=True,
67 protocol=protocol,
68 )
69
70 server_options = copy.copy(client_options)
71 server_options.data_to_send = None
72 server_options.mode = Provider.ServerMode
73 server_options.key = certificate.key
74 server_options.cert = certificate.cert
75 server_options.extra_flags = None
76 server_options.curve = curve
77 # We need the full bytes of the messages to find the specific
78 # ServerRandom bytes that indicate a HelloRetryRequest
79 server_options.verbose = True
80
81 # Passing the type of client and server as a parameter will
82 # allow us to use a fixture to enumerate all possibilities.
83 server = managed_process(provider, server_options, timeout=5)
84 client = managed_process(S2N, client_options, timeout=5)
85
86 # The client should connect and return without error
87 for results in client.get_results():
88 results.assert_success()
89 assert to_bytes("Curve: {}".format(CURVE_NAMES[curve.name])) in results.stdout
90 assert S2N_HRR_MARKER in results.stdout
91
92 # These are the special HelloRetryRequest bytes from the Server Random field
93 marker_part1 = b"cf 21 ad 74 e5"
94 marker_part2 = b"9a 61 11 be 1d"
95
96 for results in server.get_results():
97 results.assert_success()
98 assert marker_part1 in results.stdout and marker_part2 in results.stdout
99 # The "test_all" s2n security policy includes draft Hybrid PQ groups that Openssl server prints as hex values
100 assert (
101 re.search(
102 b"Supported Elliptic Groups: [x0-9A-F:]*X25519:P-256:P-384",
103 results.stdout,
104 )
105 is not None
106 )
107 assert (
108 to_bytes("Shared Elliptic groups: {}".format(server_options.curve))
109 in results.stdout
110 )
111 assert random_bytes in results.stdout
112
113
114 @pytest.mark.uncollect_if(func=invalid_test_parameters)
115 @pytest.mark.parametrize("cipher", TLS13_CIPHERS, ids=get_parameter_name)
116 @pytest.mark.parametrize("provider", [OpenSSL])
117 @pytest.mark.parametrize("other_provider", [S2N], ids=get_parameter_name)
118 @pytest.mark.parametrize("curve", ALL_TEST_CURVES, ids=get_parameter_name)
119 @pytest.mark.parametrize("protocol", [Protocols.TLS13], ids=get_parameter_name)
120 @pytest.mark.parametrize("certificate", MINIMAL_TEST_CERTS, ids=get_parameter_name)
121 def test_hrr_with_s2n_as_server(
122 managed_process, # noqa: F811
123 cipher,
124 provider,
125 other_provider,
126 curve,
127 protocol,
128 certificate,
129 ):
130 port = next(available_ports)
131
132 random_bytes = data_bytes(64)
133 client_options = ProviderOptions(
134 mode=Provider.ClientMode,
135 port=port,
136 cipher=cipher,
137 data_to_send=random_bytes,
138 insecure=True,
139 curve=curve,
140 extra_flags=["-msg", "-curves", "X448:" + str(curve)],
141 protocol=protocol,
142 # We need the full bytes of the messages to find the specific
143 # ServerRandom bytes that indicate a HelloRetryRequest
144 verbose=True,
145 )
146
147 server_options = copy.copy(client_options)
148 server_options.data_to_send = None
149 server_options.mode = Provider.ServerMode
150 server_options.key = certificate.key
151 server_options.cert = certificate.cert
152 server_options.extra_flags = None
153
154 # Passing the type of client and server as a parameter will
155 # allow us to use a fixture to enumerate all possibilities.
156 server = managed_process(S2N, server_options, timeout=5)
157 client = managed_process(provider, client_options, timeout=5)
158
159 # The client should connect and return without error
160 for results in server.get_results():
161 results.assert_success()
162 assert random_bytes in results.stdout
163 assert to_bytes("Curve: {}".format(CURVE_NAMES[curve.name])) in results.stdout
164 assert random_bytes in results.stdout
165 assert S2N_HRR_MARKER in results.stdout
166
167 client_hello_count = 0
168 server_hello_count = 0
169 finished_count = 0
170 # HRR random data Refer: https://tools.ietf.org/html/rfc8446#section-4.1.3
171 marker = b"cf 21 ad 74 e5 9a 61 11 be 1d"
172
173 for results in client.get_results():
174 results.assert_success()
175 assert marker in results.stdout
176 client_hello_count = results.stdout.count(b"ClientHello")
177 server_hello_count = results.stdout.count(b"ServerHello")
178 finished_count = results.stdout.count(b"Finished")
179
180 assert client_hello_count == 2
181 assert server_hello_count == 2
182 assert finished_count == 2
183
184
185 # Default Keyshare for TLS v1.3 is x25519
186 TEST_CURVES = ALL_TEST_CURVES[1:]
187
188
189 @pytest.mark.uncollect_if(func=invalid_test_parameters)
190 @pytest.mark.parametrize("cipher", TLS13_CIPHERS, ids=get_parameter_name)
191 @pytest.mark.parametrize("provider", [OpenSSL])
192 @pytest.mark.parametrize("other_provider", [S2N], ids=get_parameter_name)
193 @pytest.mark.parametrize("curve", TEST_CURVES, ids=get_parameter_name)
194 @pytest.mark.parametrize("protocol", [Protocols.TLS13], ids=get_parameter_name)
195 @pytest.mark.parametrize("certificate", MINIMAL_TEST_CERTS, ids=get_parameter_name)
196 def test_hrr_with_default_keyshare(
197 managed_process, # noqa: F811
198 cipher,
199 provider,
200 other_provider,
201 curve,
202 protocol,
203 certificate,
204 ):
205 port = next(available_ports)
206
207 random_bytes = data_bytes(64)
208 client_options = ProviderOptions(
209 mode=Provider.ClientMode,
210 port=port,
211 cipher=cipher,
212 data_to_send=random_bytes,
213 insecure=True,
214 protocol=protocol,
215 )
216
217 server_options = copy.copy(client_options)
218 server_options.data_to_send = None
219 server_options.mode = Provider.ServerMode
220 server_options.key = certificate.key
221 server_options.cert = certificate.cert
222 server_options.extra_flags = None
223 server_options.curve = curve
224 # We need the full bytes of the messages to find the specific
225 # ServerRandom bytes that indicate a HelloRetryRequest
226 server_options.verbose = True
227
228 # Passing the type of client and server as a parameter will
229 # allow us to use a fixture to enumerate all possibilities.
230 server = managed_process(provider, server_options, timeout=5)
231 client = managed_process(S2N, client_options, timeout=5)
232
233 # The client should connect and return without error
234 for results in client.get_results():
235 results.assert_success()
236 assert to_bytes("Curve: {}".format(CURVE_NAMES[curve.name])) in results.stdout
237 assert S2N_HRR_MARKER in results.stdout
238
239 # These are the special HelloRetryRequest bytes from the Server Random field
240 marker_part1 = b"cf 21 ad 74 e5"
241 marker_part2 = b"9a 61 11 be 1d"
242
243 for results in server.get_results():
244 results.assert_success()
245 assert marker_part1 in results.stdout and marker_part2 in results.stdout
246 # The "test_all" s2n security policy includes draft Hybrid PQ groups that Openssl server prints as hex values
247 assert (
248 re.search(
249 b"Supported Elliptic Groups: [x0-9A-F:]*X25519:P-256:P-384",
250 results.stdout,
251 )
252 is not None
253 )
254 assert (
255 to_bytes("Shared Elliptic groups: {}".format(server_options.curve))
256 in results.stdout
257 )
258 assert random_bytes in results.stdout