1# This file is dual licensed under the terms of the Apache License, Version
2# 2.0, and the BSD License. See the LICENSE file in the root of this repository
3# for complete details.
4
5from __future__ import annotations
6
7from cryptography.hazmat.bindings._rust import openssl as rust_openssl
8from cryptography.hazmat.bindings.openssl import binding
9from cryptography.hazmat.primitives import hashes
10from cryptography.hazmat.primitives._asymmetric import AsymmetricPadding
11from cryptography.hazmat.primitives.asymmetric import ec
12from cryptography.hazmat.primitives.asymmetric import utils as asym_utils
13from cryptography.hazmat.primitives.asymmetric.padding import (
14 MGF1,
15 OAEP,
16 PSS,
17 PKCS1v15,
18)
19from cryptography.hazmat.primitives.ciphers import (
20 CipherAlgorithm,
21)
22from cryptography.hazmat.primitives.ciphers.algorithms import (
23 AES,
24)
25from cryptography.hazmat.primitives.ciphers.modes import (
26 CBC,
27 Mode,
28)
29
30
31class Backend:
32 """
33 OpenSSL API binding interfaces.
34 """
35
36 name = "openssl"
37
38 # TripleDES encryption is disallowed/deprecated throughout 2023 in
39 # FIPS 140-3. To keep it simple we denylist any use of TripleDES (TDEA).
40 _fips_ciphers = (AES,)
41 # Sometimes SHA1 is still permissible. That logic is contained
42 # within the various *_supported methods.
43 _fips_hashes = (
44 hashes.SHA224,
45 hashes.SHA256,
46 hashes.SHA384,
47 hashes.SHA512,
48 hashes.SHA512_224,
49 hashes.SHA512_256,
50 hashes.SHA3_224,
51 hashes.SHA3_256,
52 hashes.SHA3_384,
53 hashes.SHA3_512,
54 hashes.SHAKE128,
55 hashes.SHAKE256,
56 )
57 _fips_ecdh_curves = (
58 ec.SECP224R1,
59 ec.SECP256R1,
60 ec.SECP384R1,
61 ec.SECP521R1,
62 )
63 _fips_rsa_min_key_size = 2048
64 _fips_rsa_min_public_exponent = 65537
65 _fips_dsa_min_modulus = 1 << 2048
66 _fips_dh_min_key_size = 2048
67 _fips_dh_min_modulus = 1 << _fips_dh_min_key_size
68
69 def __init__(self) -> None:
70 self._binding = binding.Binding()
71 self._ffi = self._binding.ffi
72 self._lib = self._binding.lib
73 self._fips_enabled = rust_openssl.is_fips_enabled()
74
75 def __repr__(self) -> str:
76 return (
77 f"<OpenSSLBackend(version: {self.openssl_version_text()}, "
78 f"FIPS: {self._fips_enabled}, "
79 f"Legacy: {rust_openssl._legacy_provider_loaded})>"
80 )
81
82 def openssl_assert(self, ok: bool) -> None:
83 return binding._openssl_assert(ok)
84
85 def _enable_fips(self) -> None:
86 # This function enables FIPS mode for OpenSSL 3.0.0 on installs that
87 # have the FIPS provider installed properly.
88 rust_openssl.enable_fips(rust_openssl._providers)
89 assert rust_openssl.is_fips_enabled()
90 self._fips_enabled = rust_openssl.is_fips_enabled()
91
92 def openssl_version_text(self) -> str:
93 """
94 Friendly string name of the loaded OpenSSL library. This is not
95 necessarily the same version as it was compiled against.
96
97 Example: OpenSSL 3.2.1 30 Jan 2024
98 """
99 return rust_openssl.openssl_version_text()
100
101 def openssl_version_number(self) -> int:
102 return rust_openssl.openssl_version()
103
104 def hash_supported(self, algorithm: hashes.HashAlgorithm) -> bool:
105 if self._fips_enabled and not isinstance(algorithm, self._fips_hashes):
106 return False
107
108 return rust_openssl.hashes.hash_supported(algorithm)
109
110 def signature_hash_supported(
111 self, algorithm: hashes.HashAlgorithm
112 ) -> bool:
113 # Dedicated check for hashing algorithm use in message digest for
114 # signatures, e.g. RSA PKCS#1 v1.5 SHA1 (sha1WithRSAEncryption).
115 if self._fips_enabled and isinstance(algorithm, hashes.SHA1):
116 return False
117 return self.hash_supported(algorithm)
118
119 def scrypt_supported(self) -> bool:
120 if self._fips_enabled:
121 return False
122 else:
123 return hasattr(rust_openssl.kdf.Scrypt, "derive")
124
125 def argon2_supported(self) -> bool:
126 if self._fips_enabled:
127 return False
128 else:
129 return hasattr(rust_openssl.kdf.Argon2id, "derive")
130
131 def hmac_supported(self, algorithm: hashes.HashAlgorithm) -> bool:
132 # FIPS mode still allows SHA1 for HMAC
133 if self._fips_enabled and isinstance(algorithm, hashes.SHA1):
134 return True
135 if rust_openssl.CRYPTOGRAPHY_IS_AWSLC:
136 return isinstance(
137 algorithm,
138 (
139 hashes.MD5,
140 hashes.SHA1,
141 hashes.SHA224,
142 hashes.SHA256,
143 hashes.SHA384,
144 hashes.SHA512,
145 hashes.SHA512_224,
146 hashes.SHA512_256,
147 hashes.SHA3_224,
148 hashes.SHA3_256,
149 hashes.SHA3_384,
150 hashes.SHA3_512,
151 ),
152 )
153 return self.hash_supported(algorithm)
154
155 def cipher_supported(self, cipher: CipherAlgorithm, mode: Mode) -> bool:
156 if self._fips_enabled:
157 # FIPS mode requires AES. TripleDES is disallowed/deprecated in
158 # FIPS 140-3.
159 if not isinstance(cipher, self._fips_ciphers):
160 return False
161
162 return rust_openssl.ciphers.cipher_supported(cipher, mode)
163
164 def _consume_errors(self) -> list[rust_openssl.OpenSSLError]:
165 return rust_openssl.capture_error_stack()
166
167 def _oaep_hash_supported(self, algorithm: hashes.HashAlgorithm) -> bool:
168 if self._fips_enabled and isinstance(algorithm, hashes.SHA1):
169 return False
170
171 return isinstance(
172 algorithm,
173 (
174 hashes.SHA1,
175 hashes.SHA224,
176 hashes.SHA256,
177 hashes.SHA384,
178 hashes.SHA512,
179 ),
180 )
181
182 def rsa_padding_supported(self, padding: AsymmetricPadding) -> bool:
183 if isinstance(padding, PKCS1v15):
184 return True
185 elif isinstance(padding, PSS) and isinstance(padding._mgf, MGF1):
186 # FIPS 186-4 only allows salt length == digest length for PSS
187 # It is technically acceptable to set an explicit salt length
188 # equal to the digest length and this will incorrectly fail, but
189 # since we don't do that in the tests and this method is
190 # private, we'll ignore that until we need to do otherwise.
191 if (
192 self._fips_enabled
193 and padding._salt_length != PSS.DIGEST_LENGTH
194 ):
195 return False
196 return self.hash_supported(padding._mgf._algorithm)
197 elif isinstance(padding, OAEP) and isinstance(padding._mgf, MGF1):
198 return self._oaep_hash_supported(
199 padding._mgf._algorithm
200 ) and self._oaep_hash_supported(padding._algorithm)
201 else:
202 return False
203
204 def rsa_encryption_supported(self, padding: AsymmetricPadding) -> bool:
205 if self._fips_enabled and isinstance(padding, PKCS1v15):
206 return False
207 else:
208 return self.rsa_padding_supported(padding)
209
210 def dsa_supported(self) -> bool:
211 return (
212 not rust_openssl.CRYPTOGRAPHY_IS_BORINGSSL
213 and not self._fips_enabled
214 )
215
216 def dsa_hash_supported(self, algorithm: hashes.HashAlgorithm) -> bool:
217 if not self.dsa_supported():
218 return False
219 return self.signature_hash_supported(algorithm)
220
221 def cmac_algorithm_supported(self, algorithm) -> bool:
222 return self.cipher_supported(
223 algorithm, CBC(b"\x00" * algorithm.block_size)
224 )
225
226 def elliptic_curve_supported(self, curve: ec.EllipticCurve) -> bool:
227 if self._fips_enabled and not isinstance(
228 curve, self._fips_ecdh_curves
229 ):
230 return False
231
232 return rust_openssl.ec.curve_supported(curve)
233
234 def elliptic_curve_signature_algorithm_supported(
235 self,
236 signature_algorithm: ec.EllipticCurveSignatureAlgorithm,
237 curve: ec.EllipticCurve,
238 ) -> bool:
239 # We only support ECDSA right now.
240 if not isinstance(signature_algorithm, ec.ECDSA):
241 return False
242
243 return self.elliptic_curve_supported(curve) and (
244 isinstance(signature_algorithm.algorithm, asym_utils.Prehashed)
245 or self.hash_supported(signature_algorithm.algorithm)
246 )
247
248 def elliptic_curve_exchange_algorithm_supported(
249 self, algorithm: ec.ECDH, curve: ec.EllipticCurve
250 ) -> bool:
251 return self.elliptic_curve_supported(curve) and isinstance(
252 algorithm, ec.ECDH
253 )
254
255 def dh_supported(self) -> bool:
256 return not rust_openssl.CRYPTOGRAPHY_IS_BORINGSSL
257
258 def dh_x942_serialization_supported(self) -> bool:
259 return self._lib.Cryptography_HAS_EVP_PKEY_DHX == 1
260
261 def x25519_supported(self) -> bool:
262 return not self._fips_enabled
263
264 def x448_supported(self) -> bool:
265 if self._fips_enabled:
266 return False
267 return (
268 not rust_openssl.CRYPTOGRAPHY_IS_LIBRESSL
269 and not rust_openssl.CRYPTOGRAPHY_IS_BORINGSSL
270 and not rust_openssl.CRYPTOGRAPHY_IS_AWSLC
271 )
272
273 def mlkem_supported(self) -> bool:
274 return (
275 rust_openssl.CRYPTOGRAPHY_IS_AWSLC
276 or rust_openssl.CRYPTOGRAPHY_IS_BORINGSSL
277 or rust_openssl.CRYPTOGRAPHY_OPENSSL_350_OR_GREATER
278 )
279
280 def mldsa_supported(self) -> bool:
281 return (
282 rust_openssl.CRYPTOGRAPHY_IS_AWSLC
283 or rust_openssl.CRYPTOGRAPHY_IS_BORINGSSL
284 or rust_openssl.CRYPTOGRAPHY_OPENSSL_350_OR_GREATER
285 )
286
287 def ed25519_supported(self) -> bool:
288 return True
289
290 def ed448_supported(self) -> bool:
291 return (
292 not rust_openssl.CRYPTOGRAPHY_IS_LIBRESSL
293 and not rust_openssl.CRYPTOGRAPHY_IS_BORINGSSL
294 and not rust_openssl.CRYPTOGRAPHY_IS_AWSLC
295 )
296
297 def ecdsa_deterministic_supported(self) -> bool:
298 return (
299 rust_openssl.CRYPTOGRAPHY_OPENSSL_320_OR_GREATER
300 and not self._fips_enabled
301 )
302
303 def poly1305_supported(self) -> bool:
304 if rust_openssl.CRYPTOGRAPHY_IS_AWSLC:
305 return True
306 return not self._fips_enabled
307
308
309backend = Backend()