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
136 return self.hash_supported(algorithm)
137
138 def cipher_supported(self, cipher: CipherAlgorithm, mode: Mode) -> bool:
139 if self._fips_enabled:
140 # FIPS mode requires AES. TripleDES is disallowed/deprecated in
141 # FIPS 140-3.
142 if not isinstance(cipher, self._fips_ciphers):
143 return False
144
145 return rust_openssl.ciphers.cipher_supported(cipher, mode)
146
147 def pbkdf2_hmac_supported(self, algorithm: hashes.HashAlgorithm) -> bool:
148 return self.hmac_supported(algorithm)
149
150 def _consume_errors(self) -> list[rust_openssl.OpenSSLError]:
151 return rust_openssl.capture_error_stack()
152
153 def _oaep_hash_supported(self, algorithm: hashes.HashAlgorithm) -> bool:
154 if self._fips_enabled and isinstance(algorithm, hashes.SHA1):
155 return False
156
157 return isinstance(
158 algorithm,
159 (
160 hashes.SHA1,
161 hashes.SHA224,
162 hashes.SHA256,
163 hashes.SHA384,
164 hashes.SHA512,
165 ),
166 )
167
168 def rsa_padding_supported(self, padding: AsymmetricPadding) -> bool:
169 if isinstance(padding, PKCS1v15):
170 return True
171 elif isinstance(padding, PSS) and isinstance(padding._mgf, MGF1):
172 # SHA1 is permissible in MGF1 in FIPS even when SHA1 is blocked
173 # as signature algorithm.
174 if self._fips_enabled and isinstance(
175 padding._mgf._algorithm, hashes.SHA1
176 ):
177 return True
178 else:
179 return self.hash_supported(padding._mgf._algorithm)
180 elif isinstance(padding, OAEP) and isinstance(padding._mgf, MGF1):
181 return self._oaep_hash_supported(
182 padding._mgf._algorithm
183 ) and self._oaep_hash_supported(padding._algorithm)
184 else:
185 return False
186
187 def rsa_encryption_supported(self, padding: AsymmetricPadding) -> bool:
188 if self._fips_enabled and isinstance(padding, PKCS1v15):
189 return False
190 else:
191 return self.rsa_padding_supported(padding)
192
193 def dsa_supported(self) -> bool:
194 return (
195 not rust_openssl.CRYPTOGRAPHY_IS_BORINGSSL
196 and not self._fips_enabled
197 )
198
199 def dsa_hash_supported(self, algorithm: hashes.HashAlgorithm) -> bool:
200 if not self.dsa_supported():
201 return False
202 return self.signature_hash_supported(algorithm)
203
204 def cmac_algorithm_supported(self, algorithm) -> bool:
205 return self.cipher_supported(
206 algorithm, CBC(b"\x00" * algorithm.block_size)
207 )
208
209 def elliptic_curve_supported(self, curve: ec.EllipticCurve) -> bool:
210 if self._fips_enabled and not isinstance(
211 curve, self._fips_ecdh_curves
212 ):
213 return False
214
215 return rust_openssl.ec.curve_supported(curve)
216
217 def elliptic_curve_signature_algorithm_supported(
218 self,
219 signature_algorithm: ec.EllipticCurveSignatureAlgorithm,
220 curve: ec.EllipticCurve,
221 ) -> bool:
222 # We only support ECDSA right now.
223 if not isinstance(signature_algorithm, ec.ECDSA):
224 return False
225
226 return self.elliptic_curve_supported(curve) and (
227 isinstance(signature_algorithm.algorithm, asym_utils.Prehashed)
228 or self.hash_supported(signature_algorithm.algorithm)
229 )
230
231 def elliptic_curve_exchange_algorithm_supported(
232 self, algorithm: ec.ECDH, curve: ec.EllipticCurve
233 ) -> bool:
234 return self.elliptic_curve_supported(curve) and isinstance(
235 algorithm, ec.ECDH
236 )
237
238 def dh_supported(self) -> bool:
239 return not rust_openssl.CRYPTOGRAPHY_IS_BORINGSSL
240
241 def dh_x942_serialization_supported(self) -> bool:
242 return self._lib.Cryptography_HAS_EVP_PKEY_DHX == 1
243
244 def x25519_supported(self) -> bool:
245 if self._fips_enabled:
246 return False
247 return True
248
249 def x448_supported(self) -> bool:
250 if self._fips_enabled:
251 return False
252 return (
253 not rust_openssl.CRYPTOGRAPHY_IS_LIBRESSL
254 and not rust_openssl.CRYPTOGRAPHY_IS_BORINGSSL
255 )
256
257 def ed25519_supported(self) -> bool:
258 if self._fips_enabled:
259 return False
260 return True
261
262 def ed448_supported(self) -> bool:
263 if self._fips_enabled:
264 return False
265 return (
266 not rust_openssl.CRYPTOGRAPHY_IS_LIBRESSL
267 and not rust_openssl.CRYPTOGRAPHY_IS_BORINGSSL
268 )
269
270 def ecdsa_deterministic_supported(self) -> bool:
271 return (
272 rust_openssl.CRYPTOGRAPHY_OPENSSL_320_OR_GREATER
273 and not self._fips_enabled
274 )
275
276 def poly1305_supported(self) -> bool:
277 if self._fips_enabled:
278 return False
279 return True
280
281 def pkcs7_supported(self) -> bool:
282 return not rust_openssl.CRYPTOGRAPHY_IS_BORINGSSL
283
284
285backend = Backend()