Debian Codegraph @

crt/s2n/tests/integrationv2/test_signature_algorithms.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 available_ports, ALL_TEST_CIPHERS, MINIMAL_TEST_CERTS
7 from common import ProviderOptions, Protocols, Signatures, data_bytes
8 from fixtures import managed_process # noqa: F401
9 from providers import Provider, S2N, OpenSSL, GnuTLS
10 from utils import (
11 invalid_test_parameters,
12 get_parameter_name,
13 get_expected_s2n_version,
14 to_bytes,
15 )
16
17 all_sigs = [
18 Signatures.RSA_SHA1,
19 Signatures.RSA_SHA224,
20 Signatures.RSA_SHA256,
21 Signatures.RSA_SHA384,
22 Signatures.RSA_SHA512,
23 Signatures.ECDSA_SECP256r1_SHA256,
24 Signatures.ECDSA_SECP384r1_SHA384,
25 Signatures.ECDSA_SECP521r1_SHA512,
26 Signatures.RSA_PSS_RSAE_SHA256,
27 Signatures.RSA_PSS_PSS_SHA256,
28 Signatures.ECDSA_SHA1,
29 Signatures.ECDSA_SHA224,
30 Signatures.ECDSA_SHA256,
31 Signatures.ECDSA_SHA384,
32 Signatures.ECDSA_SHA512,
33 ]
34
35
36 def signature_marker(mode, protocol, cipher, signature):
37 # In versions before TLS1.3, the server uses the negotiated signature scheme for the
38 # ServerKeyExchange message. The server only sends the ServerKeyExchange message when using
39 # a key exchange method that provides forward secrecy, ie, NOT static RSA.
40 # So if using RSA key exchange, there is no actual "negotiated" signature scheme, because
41 # the server never sends the client a signature scheme.
42 #
43 # This mostly has to be inferred from the RFCs, but this blog post is a pretty good summary
44 # of the situation: https://timtaubert.de/blog/2016/07/the-evolution-of-signatures-in-tls/
45 if mode == Provider.ServerMode and cipher.iana_standard_name.startswith(
46 "TLS_RSA_WITH_"
47 ):
48 signature = Signatures.NONE
49 elif protocol < Protocols.TLS12:
50 # ECDSA by default hashes with SHA-1.
51 #
52 # This is inferred from extended version of TLS1.1 rfc- https://www.rfc-editor.org/rfc/rfc4492#section-5.10
53 if signature.sig_type == "ECDSA":
54 signature = Signatures.ECDSA_SHA1
55 else:
56 signature = Signatures.RSA_MD5_SHA1
57
58 return to_bytes(
59 "{mode} signature negotiated: {type}+{digest}".format(
60 mode=mode.title(), type=signature.sig_type, digest=signature.sig_digest
61 )
62 )
63
64
65 def skip_ciphers(*args, **kwargs):
66 provider = kwargs.get("provider")
67 cert = kwargs.get("certificate")
68 cipher = kwargs.get("cipher")
69 protocol = kwargs.get("protocol")
70 sigalg = kwargs.get("signature")
71
72 if not provider.supports_signature(sigalg):
73 return True
74
75 if not cert.compatible_with_cipher(cipher):
76 return True
77
78 if not cert.compatible_with_sigalg(sigalg):
79 return True
80
81 if protocol > sigalg.max_protocol:
82 return True
83
84 if protocol < sigalg.min_protocol:
85 return True
86
87 return invalid_test_parameters(*args, **kwargs)
88
89
90 @pytest.mark.uncollect_if(func=skip_ciphers)
91 @pytest.mark.parametrize("cipher", ALL_TEST_CIPHERS, ids=get_parameter_name)
92 @pytest.mark.parametrize("provider", [OpenSSL, GnuTLS])
93 @pytest.mark.parametrize("other_provider", [S2N])
94 @pytest.mark.parametrize(
95 "protocol",
96 [Protocols.TLS13, Protocols.TLS12, Protocols.TLS11],
97 ids=get_parameter_name,
98 )
99 @pytest.mark.parametrize("certificate", MINIMAL_TEST_CERTS, ids=get_parameter_name)
100 @pytest.mark.parametrize("signature", all_sigs, ids=get_parameter_name)
101 @pytest.mark.parametrize(
102 "client_auth",
103 [True, False],
104 ids=lambda val: "client-auth" if val else "no-client-auth",
105 )
106 def test_s2n_server_signature_algorithms(
107 managed_process, # noqa: F811
108 cipher,
109 provider,
110 other_provider,
111 protocol,
112 certificate,
113 signature,
114 client_auth,
115 ):
116 port = next(available_ports)
117
118 random_bytes = data_bytes(64)
119 client_options = ProviderOptions(
120 mode=Provider.ClientMode,
121 port=port,
122 cipher=cipher,
123 data_to_send=random_bytes,
124 insecure=False,
125 use_client_auth=client_auth,
126 key=certificate.key,
127 cert=certificate.cert,
128 signature_algorithm=signature,
129 protocol=protocol,
130 )
131
132 if provider == GnuTLS:
133 # GnuTLS fails the CA verification. It must be run with this check disabled.
134 client_options.extra_flags = ["--no-ca-verification"]
135
136 server_options = copy.copy(client_options)
137 server_options.extra_flags = None
138 server_options.data_to_send = None
139 server_options.mode = Provider.ServerMode
140 server_options.key = certificate.key
141 server_options.cert = certificate.cert
142
143 server = managed_process(S2N, server_options, timeout=5)
144 client = managed_process(provider, client_options, timeout=5)
145
146 for results in client.get_results():
147 results.assert_success()
148
149 expected_version = get_expected_s2n_version(protocol, provider)
150
151 for results in server.get_results():
152 results.assert_success()
153 assert (
154 to_bytes("Actual protocol version: {}".format(expected_version))
155 in results.stdout
156 )
157 assert (
158 signature_marker(Provider.ServerMode, protocol, cipher, signature)
159 in results.stdout
160 )
161 assert (
162 signature_marker(Provider.ClientMode, protocol, cipher, signature)
163 in results.stdout
164 ) == client_auth
165 assert random_bytes in results.stdout
166
167
168 @pytest.mark.flaky(reruns=5, reruns_delay=2)
169 @pytest.mark.uncollect_if(func=skip_ciphers)
170 @pytest.mark.parametrize("cipher", ALL_TEST_CIPHERS, ids=get_parameter_name)
171 @pytest.mark.parametrize("provider", [OpenSSL, GnuTLS])
172 @pytest.mark.parametrize("other_provider", [S2N])
173 @pytest.mark.parametrize(
174 "protocol",
175 [Protocols.TLS13, Protocols.TLS12, Protocols.TLS11],
176 ids=get_parameter_name,
177 )
178 @pytest.mark.parametrize("certificate", MINIMAL_TEST_CERTS, ids=get_parameter_name)
179 @pytest.mark.parametrize("signature", all_sigs, ids=get_parameter_name)
180 @pytest.mark.parametrize(
181 "client_auth",
182 [True, False],
183 ids=lambda val: "client-auth" if val else "no-client-auth",
184 )
185 def test_s2n_client_signature_algorithms(
186 managed_process, # noqa: F811
187 cipher,
188 provider,
189 other_provider,
190 protocol,
191 certificate,
192 signature,
193 client_auth,
194 ):
195 port = next(available_ports)
196
197 random_bytes = data_bytes(64)
198 client_options = ProviderOptions(
199 mode=Provider.ClientMode,
200 port=port,
201 cipher=cipher,
202 data_to_send=random_bytes,
203 insecure=True,
204 use_client_auth=client_auth,
205 key=certificate.key,
206 cert=certificate.cert,
207 protocol=protocol,
208 )
209
210 server_options = copy.copy(client_options)
211 server_options.data_to_send = None
212 server_options.mode = Provider.ServerMode
213 server_options.key = certificate.key
214 server_options.cert = certificate.cert
215 server_options.trust_store = certificate.cert
216 server_options.signature_algorithm = signature
217
218 kill_marker = None
219 if provider == GnuTLS:
220 kill_marker = random_bytes
221
222 server = managed_process(
223 provider, server_options, timeout=5, kill_marker=kill_marker
224 )
225 client = managed_process(S2N, client_options, timeout=5)
226
227 for results in server.get_results():
228 results.assert_success()
229 assert any([random_bytes in stream for stream in results.output_streams()])
230
231 expected_version = get_expected_s2n_version(protocol, provider)
232
233 for results in client.get_results():
234 results.assert_success()
235 assert (
236 to_bytes("Actual protocol version: {}".format(expected_version))
237 in results.stdout
238 )
239 assert (
240 signature_marker(Provider.ServerMode, protocol, cipher, signature)
241 in results.stdout
242 )
243 assert (
244 signature_marker(Provider.ClientMode, protocol, cipher, signature)
245 in results.stdout
246 ) == client_auth