|
44 | 44 | from scapy.layers.tls.crypto.pkcs1 import pkcs_os2ip, _get_hash, \ |
45 | 45 | _EncryptAndVerifyRSA, _DecryptAndSignRSA |
46 | 46 | from scapy.compat import raw, bytes_encode |
| 47 | + |
47 | 48 | if conf.crypto_valid: |
48 | 49 | from cryptography.exceptions import InvalidSignature |
49 | 50 | from cryptography.hazmat.backends import default_backend |
50 | 51 | from cryptography.hazmat.primitives import serialization |
51 | 52 | from cryptography.hazmat.primitives.asymmetric import rsa, ec |
52 | 53 |
|
| 54 | + # cryptography raised the minimum RSA key length to 1024 in 43.0+ |
| 55 | + # https://github.com/pyca/cryptography/pull/10278 |
| 56 | + # but we need still 512 for EXPORT40 ciphers (yes EXPORT is terrible) |
| 57 | + # https://datatracker.ietf.org/doc/html/rfc2246#autoid-66 |
| 58 | + # The following detects the change and hacks around it using the backend |
| 59 | + |
| 60 | + try: |
| 61 | + rsa.generate_private_key(public_exponent=65537, key_size=512) |
| 62 | + _RSA_512_SUPPORTED = True |
| 63 | + except ValueError: |
| 64 | + # cryptography > 43.0 |
| 65 | + _RSA_512_SUPPORTED = False |
| 66 | + from cryptography.hazmat.primitives.asymmetric.rsa import rust_openssl |
| 67 | + |
53 | 68 |
|
54 | 69 | # Maximum allowed size in bytes for a certificate file, to avoid |
55 | 70 | # loading huge file when importing a cert |
@@ -263,9 +278,18 @@ def fill_and_store(self, modulus=None, modulusLen=None, pubExp=None): |
263 | 278 | pubExp = pubExp or 65537 |
264 | 279 | if not modulus: |
265 | 280 | real_modulusLen = modulusLen or 2048 |
266 | | - private_key = rsa.generate_private_key(public_exponent=pubExp, |
267 | | - key_size=real_modulusLen, |
268 | | - backend=default_backend()) |
| 281 | + if real_modulusLen < 1024 and not _RSA_512_SUPPORTED: |
| 282 | + # cryptography > 43.0 compatibility |
| 283 | + private_key = rust_openssl.rsa.generate_private_key( |
| 284 | + public_exponent=pubExp, |
| 285 | + key_size=real_modulusLen, |
| 286 | + ) |
| 287 | + else: |
| 288 | + private_key = rsa.generate_private_key( |
| 289 | + public_exponent=pubExp, |
| 290 | + key_size=real_modulusLen, |
| 291 | + backend=default_backend(), |
| 292 | + ) |
269 | 293 | self.pubkey = private_key.public_key() |
270 | 294 | else: |
271 | 295 | real_modulusLen = len(binrepr(modulus)) |
@@ -470,9 +494,18 @@ def fill_and_store(self, modulus=None, modulusLen=None, pubExp=None, |
470 | 494 | # in order to call RSAPrivateNumbers(...) |
471 | 495 | # if one of these is missing, we generate a whole new key |
472 | 496 | real_modulusLen = modulusLen or 2048 |
473 | | - self.key = rsa.generate_private_key(public_exponent=pubExp, |
474 | | - key_size=real_modulusLen, |
475 | | - backend=default_backend()) |
| 497 | + if real_modulusLen < 1024 and not _RSA_512_SUPPORTED: |
| 498 | + # cryptography > 43.0 compatibility |
| 499 | + self.key = rust_openssl.rsa.generate_private_key( |
| 500 | + public_exponent=pubExp, |
| 501 | + key_size=real_modulusLen, |
| 502 | + ) |
| 503 | + else: |
| 504 | + self.key = rsa.generate_private_key( |
| 505 | + public_exponent=pubExp, |
| 506 | + key_size=real_modulusLen, |
| 507 | + backend=default_backend(), |
| 508 | + ) |
476 | 509 | self.pubkey = self.key.public_key() |
477 | 510 | else: |
478 | 511 | real_modulusLen = len(binrepr(modulus)) |
|
0 commit comments