Debian Codegraph @

crt/s2n/tests/integrationv2/test_sslyze.py

raw symbols SCIP
1 # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 # SPDX-License-Identifier: Apache-2.0
3 import pytest
4 import sslyze
5 import abc
6 from enum import Enum, auto
7
8 from configuration import available_ports, MINIMAL_TEST_CERTS
9 from common import ProviderOptions, Protocols, Cipher, Ciphers, Curves
10 from fixtures import managed_process # noqa: F401
11 from providers import S2N
12 from utils import get_parameter_name, invalid_test_parameters
13 from global_flags import get_flag, S2N_PROVIDER_VERSION
14
15 HOST = "127.0.0.1"
16
17 PROTOCOLS_TO_TEST = [
18 Protocols.SSLv3,
19 Protocols.TLS10,
20 Protocols.TLS11,
21 Protocols.TLS12,
22 Protocols.TLS13,
23 ]
24
25 SSLYZE_SCANS_TO_TEST = [
26 sslyze.ScanCommand.ROBOT,
27 sslyze.ScanCommand.SESSION_RESUMPTION,
28 sslyze.ScanCommand.TLS_COMPRESSION,
29 sslyze.ScanCommand.TLS_1_3_EARLY_DATA,
30 sslyze.ScanCommand.TLS_FALLBACK_SCSV,
31 sslyze.ScanCommand.HEARTBLEED,
32 sslyze.ScanCommand.OPENSSL_CCS_INJECTION,
33 sslyze.ScanCommand.SESSION_RENEGOTIATION,
34 ]
35
36 CERTS_TO_TEST = [
37 cert
38 for cert in MINIMAL_TEST_CERTS
39 if cert.name
40 not in {
41 "RSA_PSS_2048_SHA256" # SSLyze errors when given an RSA PSS cert
42 }
43 ]
44
45 CIPHER_SUITE_SCANS = {
46 Protocols.SSLv3.value: sslyze.ScanCommand.SSL_3_0_CIPHER_SUITES,
47 Protocols.TLS10.value: sslyze.ScanCommand.TLS_1_0_CIPHER_SUITES,
48 Protocols.TLS11.value: sslyze.ScanCommand.TLS_1_1_CIPHER_SUITES,
49 Protocols.TLS12.value: sslyze.ScanCommand.TLS_1_2_CIPHER_SUITES,
50 Protocols.TLS13.value: sslyze.ScanCommand.TLS_1_3_CIPHER_SUITES,
51 }
52
53
54 class ScanVerifier:
55 __metaclass__ = abc.ABCMeta
56
57 def __init__(self, scan_result, protocol, certificate=None):
58 self.scan_result = scan_result
59 self.protocol = protocol
60 self.certificate = certificate
61
62 @abc.abstractmethod
63 def assert_scan_success(self):
64 pass
65
66
67 class CipherSuitesVerifier(ScanVerifier):
68 def assert_scan_success(self):
69 assert self.scan_result.is_tls_version_supported is True
70
71 rejected_ciphers = [
72 cipher
73 for rejected_cipher in self.scan_result.rejected_cipher_suites
74 if (cipher := Ciphers.from_iana(rejected_cipher.cipher_suite.name))
75 ]
76
77 for cipher in rejected_ciphers:
78 # If a cipher is rejected, it should be an invalid test parameter in combination with the
79 # protocol/provider/cert, otherwise it should have been accepted
80 assert invalid_test_parameters(
81 protocol=self.protocol,
82 provider=S2N,
83 certificate=self.certificate,
84 cipher=cipher,
85 )
86
87
88 class EllipticCurveVerifier(ScanVerifier):
89 def assert_scan_success(self):
90 assert self.scan_result.supports_ecdh_key_exchange is True
91
92 rejected_curves = [
93 curve
94 for rejected_curve in self.scan_result.rejected_curves
95 if (
96 curve := {
97 "X25519": Curves.X25519,
98 "prime256v1": Curves.P256,
99 "prime384v1": Curves.P384,
100 "prime521v1": Curves.P521,
101 }.get(rejected_curve.name)
102 )
103 ]
104
105 for curve in rejected_curves:
106 # If a curve is rejected, it should be an invalid test parameter in combination with the
107 # protocol/provider/cert, otherwise it should have been accepted
108 assert invalid_test_parameters(
109 protocol=self.protocol,
110 provider=S2N,
111 certificate=self.certificate,
112 curve=curve,
113 )
114
115
116 class RobotVerifier(ScanVerifier):
117 def assert_scan_success(self):
118 if self.protocol == Protocols.TLS13:
119 assert (
120 self.scan_result.robot_result
121 == sslyze.RobotScanResultEnum.NOT_VULNERABLE_RSA_NOT_SUPPORTED
122 )
123 else:
124 assert (
125 self.scan_result.robot_result
126 == sslyze.RobotScanResultEnum.NOT_VULNERABLE_NO_ORACLE
127 )
128
129
130 class SessionResumptionVerifier(ScanVerifier):
131 def assert_scan_success(self):
132 if self.protocol == Protocols.TLS13:
133 pass # SSLyze does not support session resumption scans for tls 1.3
134 else:
135 assert (
136 self.scan_result.tls_ticket_resumption_result
137 == sslyze.TlsResumptionSupportEnum.FULLY_SUPPORTED
138 )
139
140
141 class CrimeVerifier(ScanVerifier):
142 def assert_scan_success(self):
143 assert self.scan_result.supports_compression is False
144
145
146 class EarlyDataVerifier(ScanVerifier):
147 def assert_scan_success(self):
148 if self.protocol == Protocols.TLS13:
149 assert self.scan_result.supports_early_data is True
150 else:
151 assert self.scan_result.supports_early_data is False
152
153
154 class DowngradePreventionVerifier(ScanVerifier):
155 def assert_scan_success(self):
156 assert self.scan_result.supports_fallback_scsv is True
157
158
159 class HeartbleedVerifier(ScanVerifier):
160 def assert_scan_success(self):
161 assert self.scan_result.is_vulnerable_to_heartbleed is False
162
163
164 class CCSInjectionVerifier(ScanVerifier):
165 def assert_scan_success(self):
166 assert self.scan_result.is_vulnerable_to_ccs_injection is False
167
168
169 class InsecureRenegotiationVerifier(ScanVerifier):
170 def assert_scan_success(self):
171 assert self.scan_result.is_vulnerable_to_client_renegotiation_dos is False
172
173
174 def validate_scan_result(scan_attempt, protocol, certificate=None):
175 assert_scan_attempt_completed(scan_attempt)
176 scan_result = scan_attempt.result
177
178 verifier_cls = {
179 sslyze.CipherSuitesScanResult: CipherSuitesVerifier,
180 sslyze.SupportedEllipticCurvesScanResult: EllipticCurveVerifier,
181 sslyze.RobotScanResult: RobotVerifier,
182 sslyze.SessionResumptionSupportScanResult: SessionResumptionVerifier,
183 sslyze.CompressionScanResult: CrimeVerifier,
184 sslyze.EarlyDataScanResult: EarlyDataVerifier,
185 sslyze.FallbackScsvScanResult: DowngradePreventionVerifier,
186 sslyze.HeartbleedScanResult: HeartbleedVerifier,
187 sslyze.OpenSslCcsInjectionScanResult: CCSInjectionVerifier,
188 sslyze.SessionRenegotiationScanResult: InsecureRenegotiationVerifier,
189 }.get(type(scan_result))
190
191 assert verifier_cls is not None, f"unexpected scan: {scan_attempt}"
192
193 verifier = verifier_cls(scan_result, protocol, certificate)
194 verifier.assert_scan_success()
195
196
197 def get_scan_attempts(scan_results):
198 # scan_results (sslyze.AllScanCommandsAttempts) is an object containing parameters mapped to scan attempts. convert
199 # this to a list containing just scan attempts, and then filter out tests that were not scheduled.
200 scan_attribute_names = [
201 attr_name for attr_name in dir(scan_results) if not attr_name.startswith("__")
202 ]
203 scan_attempts = [
204 getattr(scan_results, attr_name) for attr_name in scan_attribute_names
205 ]
206 scan_attempts = [
207 scan_attempt
208 for scan_attempt in scan_attempts
209 if scan_attempt.status != sslyze.ScanCommandAttemptStatusEnum.NOT_SCHEDULED
210 ]
211 return scan_attempts
212
213
214 def assert_scan_result_completed(scan_result):
215 def get_connectivity_error_str(tb):
216 return "\n".join(tb.stack.format())
217
218 assert (
219 scan_result.connectivity_status == sslyze.ServerConnectivityStatusEnum.COMPLETED
220 ), (
221 f"sslyze could not connect to server: {get_connectivity_error_str(scan_result.connectivity_error_trace)}"
222 )
223
224
225 def assert_scan_attempt_completed(scan_attempt):
226 assert scan_attempt.status == sslyze.ScanCommandAttemptStatusEnum.COMPLETED, (
227 f"scan attempt ({scan_attempt}) failed: {scan_attempt.status}"
228 )
229
230
231 def run_sslyze_scan(host, port, scans):
232 scan_request = sslyze.ServerScanRequest(
233 server_location=sslyze.ServerNetworkLocation(hostname=host, port=port),
234 scan_commands=scans,
235 )
236 scanner = sslyze.Scanner(per_server_concurrent_connections_limit=1)
237 scanner.queue_scans([scan_request])
238 return scanner.get_results()
239
240
241 def invalid_sslyze_scan_parameters(*args, **kwargs):
242 scan_command = kwargs["scan_command"]
243 protocol = kwargs["protocol"]
244
245 # BUG_IN_SSLYZE error in TLS compression and session renegotiation scans
246 # in fips libcryptos when TLS version < 1.3
247 if "fips" in get_flag(S2N_PROVIDER_VERSION) and protocol != Protocols.TLS13:
248 if scan_command in [
249 sslyze.ScanCommand.TLS_COMPRESSION,
250 sslyze.ScanCommand.SESSION_RENEGOTIATION,
251 ]:
252 return True
253 # BUG_IN_SSLYZE error for session resumption scan with openssl 1.0.2 fips
254 if "openssl-1.0.2-fips" in get_flag(S2N_PROVIDER_VERSION):
255 if scan_command == sslyze.ScanCommand.SESSION_RESUMPTION:
256 return True
257
258 return invalid_test_parameters(*args, **kwargs)
259
260
261 @pytest.mark.uncollect_if(func=invalid_sslyze_scan_parameters)
262 @pytest.mark.parametrize("protocol", PROTOCOLS_TO_TEST, ids=get_parameter_name)
263 @pytest.mark.parametrize("scan_command", SSLYZE_SCANS_TO_TEST, ids=get_parameter_name)
264 @pytest.mark.parametrize("provider", [S2N], ids=get_parameter_name)
265 def test_sslyze_scans(managed_process, protocol, scan_command, provider): # noqa: F811
266 port = next(available_ports)
267
268 server_options = ProviderOptions(
269 mode=S2N.ServerMode,
270 host=HOST,
271 port=port,
272 protocol=protocol,
273 extra_flags=["--parallelize"],
274 )
275
276 # Test 1.3 exclusively
277 if protocol == Protocols.TLS13:
278 server_options.cipher = Cipher(
279 "test_all_tls13", Protocols.TLS13, False, False, s2n=True
280 )
281
282 if scan_command == sslyze.ScanCommand.SESSION_RESUMPTION:
283 server_options.reconnect = (True,)
284 server_options.use_session_ticket = (True,)
285
286 if scan_command == sslyze.ScanCommand.TLS_1_3_EARLY_DATA:
287 server_options.insecure = True
288 server_options.use_session_ticket = True
289 server_options.extra_flags.extend(
290 [
291 "--max-early-data",
292 "65535",
293 "--https-server", # Early data scan sends http requests
294 ]
295 )
296
297 server = managed_process(S2N, server_options, timeout=30)
298
299 scan_results = run_sslyze_scan(HOST, port, [scan_command])
300
301 for result in scan_results:
302 assert_scan_result_completed(result)
303
304 scan_results = result.scan_result
305 scan_attempts = get_scan_attempts(scan_results)
306 for scan_attempt in scan_attempts:
307 validate_scan_result(scan_attempt, protocol)
308
309 server.kill()
310
311
312 class CertificateScan(Enum):
313 CIPHER_SUITE_SCAN = auto()
314 ELLIPTIC_CURVE_SCAN = auto()
315
316
317 def invalid_certificate_scans_parameters(*args, **kwargs):
318 certificate = kwargs["certificate"]
319 certificate_scan = kwargs["certificate_scan"]
320 protocol = kwargs["protocol"]
321
322 if certificate_scan == CertificateScan.CIPHER_SUITE_SCAN:
323 if "openssl-1.0.2" in get_flag(S2N_PROVIDER_VERSION):
324 # SSLyze scan results in rejected ciphers that should have been accepted
325 # for TLS 1.2
326 if protocol == Protocols.TLS12:
327 return True
328 if "fips" in get_flag(S2N_PROVIDER_VERSION):
329 # BUG_IN_SSLYZE / TLS version supported assertion failures for ECDSA scans
330 # in SSLv3 and RSA with TLS version < 1.2 with fips libcryptos
331 if "ECDSA" in certificate.name and protocol == Protocols.SSLv3:
332 return True
333 if "RSA" in certificate.name and protocol in [
334 Protocols.SSLv3,
335 Protocols.TLS10,
336 Protocols.TLS11,
337 ]:
338 return True
339 elif certificate_scan == CertificateScan.ELLIPTIC_CURVE_SCAN:
340 # SSLyze curves scan errors when given ECDSA certs
341 if "ECDSA" in certificate.name:
342 return True
343
344 # SSLyze curves scan fails to validate with openssl 1.0.2 fips
345 if "openssl-1.0.2-fips" in get_flag(S2N_PROVIDER_VERSION):
346 return True
347
348 return invalid_test_parameters(*args, **kwargs)
349
350
351 @pytest.mark.uncollect_if(func=invalid_certificate_scans_parameters)
352 @pytest.mark.parametrize("protocol", PROTOCOLS_TO_TEST, ids=get_parameter_name)
353 @pytest.mark.parametrize("certificate", CERTS_TO_TEST, ids=get_parameter_name)
354 @pytest.mark.parametrize("provider", [S2N], ids=get_parameter_name)
355 @pytest.mark.parametrize(
356 "certificate_scan",
357 [CertificateScan.CIPHER_SUITE_SCAN, CertificateScan.ELLIPTIC_CURVE_SCAN],
358 ids=lambda certificate_scan: certificate_scan.name,
359 )
360 def test_sslyze_certificate_scans(
361 managed_process, # noqa: F811
362 protocol,
363 certificate,
364 provider,
365 certificate_scan,
366 ):
367 port = next(available_ports)
368
369 server_options = ProviderOptions(
370 mode=S2N.ServerMode,
371 host=HOST,
372 port=port,
373 protocol=protocol,
374 key=certificate.key,
375 cert=certificate.cert,
376 insecure=True,
377 extra_flags=["--parallelize"],
378 )
379 server = managed_process(S2N, server_options, timeout=30)
380
381 scan = {
382 CertificateScan.CIPHER_SUITE_SCAN: CIPHER_SUITE_SCANS.get(protocol.value),
383 CertificateScan.ELLIPTIC_CURVE_SCAN: sslyze.ScanCommand.ELLIPTIC_CURVES,
384 }.get(certificate_scan)
385
386 scan_attempt_results = run_sslyze_scan(HOST, port, [scan])
387
388 for scan_attempt_result in scan_attempt_results:
389 assert_scan_result_completed(scan_attempt_result)
390
391 scan_result = scan_attempt_result.scan_result
392 scan_attempts = get_scan_attempts(scan_result)
393 for scan_attempt in scan_attempts:
394 validate_scan_result(scan_attempt, protocol, certificate)
395
396 server.kill()