Debian Codegraph @

crt/s2n/tests/integrationv2/test_version_negotiation.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 )
12 from common import ProviderOptions, Protocols, data_bytes
13 from fixtures import managed_process # noqa: F401
14 from providers import Provider, S2N, OpenSSL, GnuTLS
15 from utils import (
16 invalid_test_parameters,
17 get_parameter_name,
18 get_expected_s2n_version,
19 get_expected_openssl_version,
20 to_bytes,
21 get_expected_gnutls_version,
22 )
23
24
25 def test_nothing():
26 """
27 Sometimes the version negotiation test parameters in combination with the s2n
28 libcrypto results in no test cases existing. In this case, pass a nothing test to
29 avoid marking the entire codebuild run as failed.
30 """
31 assert True
32
33
34 def invalid_version_negotiation_test_parameters(*args, **kwargs):
35 # Since s2nd/s2nc will always be using TLS 1.3, make sure the libcrypto is compatible
36 if invalid_test_parameters(**{"provider": S2N, "protocol": Protocols.TLS13}):
37 return True
38
39 return invalid_test_parameters(*args, **kwargs)
40
41
42 @pytest.mark.flaky(reruns=5, reruns_delay=2)
43 @pytest.mark.uncollect_if(func=invalid_version_negotiation_test_parameters)
44 @pytest.mark.parametrize("cipher", ALL_TEST_CIPHERS, ids=get_parameter_name)
45 @pytest.mark.parametrize("curve", ALL_TEST_CURVES, ids=get_parameter_name)
46 @pytest.mark.parametrize("certificate", MINIMAL_TEST_CERTS, ids=get_parameter_name)
47 @pytest.mark.parametrize(
48 "protocol",
49 [Protocols.TLS12, Protocols.TLS11, Protocols.TLS10],
50 ids=get_parameter_name,
51 )
52 @pytest.mark.parametrize("provider", [S2N, OpenSSL, GnuTLS], ids=get_parameter_name)
53 @pytest.mark.parametrize("other_provider", [S2N], ids=get_parameter_name)
54 def test_s2nc_tls13_negotiates_tls12(
55 managed_process, # noqa: F811
56 cipher,
57 curve,
58 certificate,
59 protocol,
60 provider,
61 other_provider,
62 ):
63 port = next(available_ports)
64
65 random_bytes = data_bytes(24)
66 client_options = ProviderOptions(
67 mode=Provider.ClientMode,
68 port=port,
69 cipher=cipher,
70 curve=curve,
71 data_to_send=random_bytes,
72 insecure=True,
73 protocol=Protocols.TLS13,
74 )
75
76 server_options = copy.copy(client_options)
77 server_options.data_to_send = None
78 server_options.mode = Provider.ServerMode
79 server_options.key = certificate.key
80 server_options.cert = certificate.cert
81 server_options.protocol = protocol
82
83 kill_marker = None
84 if provider == GnuTLS:
85 kill_marker = random_bytes
86
87 server = managed_process(
88 provider, server_options, timeout=5, kill_marker=kill_marker
89 )
90 client = managed_process(S2N, client_options, timeout=5)
91
92 client_version = get_expected_s2n_version(Protocols.TLS13, provider)
93 actual_version = get_expected_s2n_version(protocol, provider)
94
95 for results in client.get_results():
96 results.assert_success()
97 assert (
98 to_bytes("Client protocol version: {}".format(client_version))
99 in results.stdout
100 )
101 assert (
102 to_bytes("Actual protocol version: {}".format(actual_version))
103 in results.stdout
104 )
105
106 for results in server.get_results():
107 results.assert_success()
108 # This check only cares about S2N. Trying to maintain expected output of other providers doesn't add benefit to
109 # whether the S2N client was able to negotiate a lower TLS version.
110 if provider is S2N:
111 # The client sends a TLS 1.3 client hello so a client protocol version of TLS 1.3 should always be expected.
112 assert (
113 to_bytes("Client protocol version: {}".format(Protocols.TLS13.value))
114 in results.stdout
115 )
116 assert (
117 to_bytes("Actual protocol version: {}".format(actual_version))
118 in results.stdout
119 )
120
121 assert any([random_bytes[1:] in stream for stream in results.output_streams()])
122
123
124 @pytest.mark.uncollect_if(func=invalid_version_negotiation_test_parameters)
125 @pytest.mark.parametrize("cipher", ALL_TEST_CIPHERS, ids=get_parameter_name)
126 @pytest.mark.parametrize("curve", ALL_TEST_CURVES, ids=get_parameter_name)
127 @pytest.mark.parametrize("certificate", MINIMAL_TEST_CERTS, ids=get_parameter_name)
128 @pytest.mark.parametrize(
129 "protocol",
130 [Protocols.TLS12, Protocols.TLS11, Protocols.TLS10],
131 ids=get_parameter_name,
132 )
133 @pytest.mark.parametrize("provider", [S2N, OpenSSL, GnuTLS], ids=get_parameter_name)
134 @pytest.mark.parametrize("other_provider", [S2N], ids=get_parameter_name)
135 def test_s2nd_tls13_negotiates_tls12(
136 managed_process, # noqa: F811
137 cipher,
138 curve,
139 certificate,
140 protocol,
141 provider,
142 other_provider,
143 ):
144 port = next(available_ports)
145
146 random_bytes = data_bytes(24)
147 client_options = ProviderOptions(
148 mode=Provider.ClientMode,
149 port=port,
150 cipher=cipher,
151 curve=curve,
152 data_to_send=random_bytes,
153 insecure=True,
154 protocol=protocol,
155 )
156
157 server_options = copy.copy(client_options)
158 server_options.data_to_send = None
159 server_options.mode = Provider.ServerMode
160 server_options.key = certificate.key
161 server_options.cert = certificate.cert
162 # When the protocol is set to TLS13, the s2n server provider will default to using
163 # all ciphers, not just the TLS13 ciphers. This is the desired behavior for this test.
164 server_options.protocol = Protocols.TLS13
165
166 server = managed_process(S2N, server_options, timeout=5)
167 client = managed_process(provider, client_options, timeout=5)
168
169 server_version = get_expected_s2n_version(Protocols.TLS13, provider)
170 actual_version = get_expected_s2n_version(protocol, provider)
171
172 for results in client.get_results():
173 results.assert_success()
174 if provider is S2N:
175 # The client will get the server version from the SERVER HELLO, which will be the negotiated version
176 assert (
177 to_bytes("Server protocol version: {}".format(actual_version))
178 in results.stdout
179 )
180 assert (
181 to_bytes("Actual protocol version: {}".format(actual_version))
182 in results.stdout
183 )
184 elif provider is OpenSSL:
185 # This check cares about other providers because we want to know that they did negotiate the version
186 # that our S2N server intended to negotiate.
187 openssl_version = get_expected_openssl_version(protocol)
188 assert to_bytes("Protocol : {}".format(openssl_version)) in results.stdout
189 elif provider is GnuTLS:
190 gnutls_version = get_expected_gnutls_version(protocol)
191 assert to_bytes(f"Version: {gnutls_version}") in results.stdout
192
193 for results in server.get_results():
194 results.assert_success()
195 assert (
196 to_bytes("Server protocol version: {}".format(server_version))
197 in results.stdout
198 )
199 assert (
200 to_bytes("Actual protocol version: {}".format(actual_version))
201 in results.stdout
202 )
203 assert random_bytes[1:] in results.stdout