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 _evp_md_from_algorithm(self, algorithm: hashes.HashAlgorithm):
105 if algorithm.name in ("blake2b", "blake2s"):
106 alg = f"{algorithm.name}{algorithm.digest_size * 8}".encode(
107 "ascii"
108 )
109 else:
110 alg = algorithm.name.encode("ascii")
111
112 evp_md = self._lib.EVP_get_digestbyname(alg)
113 return evp_md
114
115 def hash_supported(self, algorithm: hashes.HashAlgorithm) -> bool:
116 if self._fips_enabled and not isinstance(algorithm, self._fips_hashes):
117 return False
118
119 evp_md = self._evp_md_from_algorithm(algorithm)
120 return evp_md != self._ffi.NULL
121
122 def signature_hash_supported(
123 self, algorithm: hashes.HashAlgorithm
124 ) -> bool:
125 # Dedicated check for hashing algorithm use in message digest for
126 # signatures, e.g. RSA PKCS#1 v1.5 SHA1 (sha1WithRSAEncryption).
127 if self._fips_enabled and isinstance(algorithm, hashes.SHA1):
128 return False
129 return self.hash_supported(algorithm)
130
131 def scrypt_supported(self) -> bool:
132 if self._fips_enabled:
133 return False
134 else:
135 return hasattr(rust_openssl.kdf, "derive_scrypt")
136
137 def hmac_supported(self, algorithm: hashes.HashAlgorithm) -> bool:
138 # FIPS mode still allows SHA1 for HMAC
139 if self._fips_enabled and isinstance(algorithm, hashes.SHA1):
140 return True
141
142 return self.hash_supported(algorithm)
143
144 def cipher_supported(self, cipher: CipherAlgorithm, mode: Mode) -> bool:
145 if self._fips_enabled:
146 # FIPS mode requires AES. TripleDES is disallowed/deprecated in
147 # FIPS 140-3.
148 if not isinstance(cipher, self._fips_ciphers):
149 return False
150
151 return rust_openssl.ciphers.cipher_supported(cipher, mode)
152
153 def pbkdf2_hmac_supported(self, algorithm: hashes.HashAlgorithm) -> bool:
154 return self.hmac_supported(algorithm)
155
156 def _consume_errors(self) -> list[rust_openssl.OpenSSLError]:
157 return rust_openssl.capture_error_stack()
158
159 def _oaep_hash_supported(self, algorithm: hashes.HashAlgorithm) -> bool:
160 if self._fips_enabled and isinstance(algorithm, hashes.SHA1):
161 return False
162
163 return isinstance(
164 algorithm,
165 (
166 hashes.SHA1,
167 hashes.SHA224,
168 hashes.SHA256,
169 hashes.SHA384,
170 hashes.SHA512,
171 ),
172 )
173
174 def rsa_padding_supported(self, padding: AsymmetricPadding) -> bool:
175 if isinstance(padding, PKCS1v15):
176 return True
177 elif isinstance(padding, PSS) and isinstance(padding._mgf, MGF1):
178 # SHA1 is permissible in MGF1 in FIPS even when SHA1 is blocked
179 # as signature algorithm.
180 if self._fips_enabled and isinstance(
181 padding._mgf._algorithm, hashes.SHA1
182 ):
183 return True
184 else:
185 return self.hash_supported(padding._mgf._algorithm)
186 elif isinstance(padding, OAEP) and isinstance(padding._mgf, MGF1):
187 return self._oaep_hash_supported(
188 padding._mgf._algorithm
189 ) and self._oaep_hash_supported(padding._algorithm)
190 else:
191 return False
192
193 def rsa_encryption_supported(self, padding: AsymmetricPadding) -> bool:
194 if self._fips_enabled and isinstance(padding, PKCS1v15):
195 return False
196 else:
197 return self.rsa_padding_supported(padding)
198
199 def dsa_supported(self) -> bool:
200 return (
201 not rust_openssl.CRYPTOGRAPHY_IS_BORINGSSL
202 and not self._fips_enabled
203 )
204
205 def dsa_hash_supported(self, algorithm: hashes.HashAlgorithm) -> bool:
206 if not self.dsa_supported():
207 return False
208 return self.signature_hash_supported(algorithm)
209
210 def cmac_algorithm_supported(self, algorithm) -> bool:
211 return self.cipher_supported(
212 algorithm, CBC(b"\x00" * algorithm.block_size)
213 )
214
215 def elliptic_curve_supported(self, curve: ec.EllipticCurve) -> bool:
216 if self._fips_enabled and not isinstance(
217 curve, self._fips_ecdh_curves
218 ):
219 return False
220
221 return rust_openssl.ec.curve_supported(curve)
222
223 def elliptic_curve_signature_algorithm_supported(
224 self,
225 signature_algorithm: ec.EllipticCurveSignatureAlgorithm,
226 curve: ec.EllipticCurve,
227 ) -> bool:
228 # We only support ECDSA right now.
229 if not isinstance(signature_algorithm, ec.ECDSA):
230 return False
231
232 return self.elliptic_curve_supported(curve) and (
233 isinstance(signature_algorithm.algorithm, asym_utils.Prehashed)
234 or self.hash_supported(signature_algorithm.algorithm)
235 )
236
237 def elliptic_curve_exchange_algorithm_supported(
238 self, algorithm: ec.ECDH, curve: ec.EllipticCurve
239 ) -> bool:
240 return self.elliptic_curve_supported(curve) and isinstance(
241 algorithm, ec.ECDH
242 )
243
244 def dh_supported(self) -> bool:
245 return not rust_openssl.CRYPTOGRAPHY_IS_BORINGSSL
246
247 def dh_x942_serialization_supported(self) -> bool:
248 return self._lib.Cryptography_HAS_EVP_PKEY_DHX == 1
249
250 def x25519_supported(self) -> bool:
251 if self._fips_enabled:
252 return False
253 return True
254
255 def x448_supported(self) -> bool:
256 if self._fips_enabled:
257 return False
258 return (
259 not rust_openssl.CRYPTOGRAPHY_IS_LIBRESSL
260 and not rust_openssl.CRYPTOGRAPHY_IS_BORINGSSL
261 )
262
263 def ed25519_supported(self) -> bool:
264 if self._fips_enabled:
265 return False
266 return True
267
268 def ed448_supported(self) -> bool:
269 if self._fips_enabled:
270 return False
271 return (
272 not rust_openssl.CRYPTOGRAPHY_IS_LIBRESSL
273 and not rust_openssl.CRYPTOGRAPHY_IS_BORINGSSL
274 )
275
276 def ecdsa_deterministic_supported(self) -> bool:
277 return (
278 rust_openssl.CRYPTOGRAPHY_OPENSSL_320_OR_GREATER
279 and not self._fips_enabled
280 )
281
282 def poly1305_supported(self) -> bool:
283 if self._fips_enabled:
284 return False
285 return True
286
287 def pkcs7_supported(self) -> bool:
288 return not rust_openssl.CRYPTOGRAPHY_IS_BORINGSSL
289
290
291backend = Backend()