Debian Codegraph @

crt/s2n/tests/integrationv2/utils.py

raw symbols SCIP
1 # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 # SPDX-License-Identifier: Apache-2.0
3 from common import Protocols
4 from providers import S2N
5 from global_flags import get_flag, S2N_FIPS_MODE
6
7
8 def to_bytes(val):
9 return bytes(str(val).encode("utf-8"))
10
11
12 def to_string(val: bytes):
13 return val.decode(encoding="ascii", errors="backslashreplace")
14
15
16 def get_expected_s2n_version(protocol, provider):
17 """
18 s2nd and s2nc print a number for the negotiated TLS version.
19
20 provider is s2n's peer. If s2n tries to speak to s2n < tls13,
21 tls12 is always chosen. This is true even when the requested
22 protocol is less than tls12.
23 """
24 if provider == S2N and protocol != Protocols.TLS13:
25 version = "33"
26 else:
27 version = protocol.value
28
29 return version
30
31
32 def get_expected_openssl_version(protocol):
33 return {
34 Protocols.TLS10.value: "TLSv1",
35 Protocols.TLS11.value: "TLSv1.1",
36 Protocols.TLS12.value: "TLSv1.2",
37 Protocols.TLS13.value: "TLSv1.3",
38 }.get(protocol.value)
39
40
41 def get_expected_gnutls_version(protocol):
42 return {
43 Protocols.TLS10.value: "TLS1.0",
44 Protocols.TLS11.value: "TLS1.1",
45 Protocols.TLS12.value: "TLS1.2",
46 Protocols.TLS13.value: "TLS1.3",
47 }.get(protocol.value)
48
49
50 def get_parameter_name(item):
51 if isinstance(item, type):
52 return item.__name__
53 return str(item)
54
55
56 def invalid_test_parameters(*args, **kwargs):
57 """
58 Determine if the parameters chosen for a test makes sense.
59 This function returns True or False, indicating whether a
60 test should be "deselected" based on the arguments.
61 """
62 protocol = kwargs.get("protocol")
63 provider = kwargs.get("provider")
64 other_provider = kwargs.get("other_provider")
65 certificate = kwargs.get("certificate")
66 client_certificate = kwargs.get("client_certificate")
67 cipher = kwargs.get("cipher")
68 curve = kwargs.get("curve")
69 signature = kwargs.get("signature")
70
71 providers = [provider_ for provider_ in [provider, other_provider] if provider_]
72 # Always consider S2N
73 providers.append(S2N)
74
75 certificates = [cert for cert in [certificate, client_certificate] if cert]
76
77 # Older versions do not support RSA-PSS-PSS certificates
78 if protocol and protocol < Protocols.TLS12:
79 if client_certificate and client_certificate.algorithm == "RSAPSS":
80 return True
81 if certificate and certificate.algorithm == "RSAPSS":
82 return True
83
84 for provider_ in providers:
85 if not provider_.supports_protocol(protocol):
86 return True
87
88 for certificate_ in certificates:
89 if not provider_.supports_certificate(certificate_):
90 return True
91
92 if cipher is not None:
93 # If the selected protocol doesn't allow the cipher, don't test
94 if protocol is not None:
95 if cipher.min_version > protocol:
96 return True
97
98 # Ciphersuites prior to TLS13 can not be used with TLS13
99 # https://wiki.openssl.org/index.php/TLS1.3#Differences_with_TLS1.2_and_below
100 if protocol is Protocols.TLS13 and cipher.min_version < protocol:
101 return True
102
103 for provider_ in providers:
104 if not provider_.supports_cipher(cipher, with_curve=curve):
105 return True
106
107 if get_flag(S2N_FIPS_MODE):
108 if not cipher.fips:
109 return True
110
111 # If we are using a cipher that depends on a specific certificate algorithm
112 # deselect the test if the wrong certificate is used.
113 if certificate is not None:
114 if cipher is not None and certificate.compatible_with_cipher(cipher) is False:
115 return True
116
117 # If the curve is specified, then all signatures must use that curve
118 if curve:
119 if certificate and not certificate.compatible_with_curve(curve):
120 return True
121 if client_certificate and not client_certificate.compatible_with_curve(curve):
122 return True
123
124 # Prevent situations like using X25519 with TLS1.2
125 if curve is not None:
126 if protocol is not None and curve.min_protocol > protocol:
127 return True
128
129 if signature is not None:
130 for provider_ in providers:
131 if provider_.supports_signature(signature) is False:
132 return True
133
134 return False