| 491 | |
| 492 | @staticmethod |
| 493 | def load_dhparam(path: Path) -> DHParams: |
| 494 | # mitmproxy<=0.10 doesn't generate a dhparam file. |
| 495 | # Create it now if necessary. |
| 496 | if not path.exists(): |
| 497 | path.write_bytes(DEFAULT_DHPARAM) |
| 498 | |
| 499 | # we could use cryptography for this, but it's unclear how to convert cryptography's object to pyOpenSSL's |
| 500 | # expected format. |
| 501 | bio = OpenSSL.SSL._lib.BIO_new_file( # type: ignore |
| 502 | str(path).encode(sys.getfilesystemencoding()), b"r" |
| 503 | ) |
| 504 | if bio != OpenSSL.SSL._ffi.NULL: # type: ignore |
| 505 | bio = OpenSSL.SSL._ffi.gc(bio, OpenSSL.SSL._lib.BIO_free) # type: ignore |
| 506 | dh = OpenSSL.SSL._lib.PEM_read_bio_DHparams( # type: ignore |
| 507 | bio, |
| 508 | OpenSSL.SSL._ffi.NULL, # type: ignore |
| 509 | OpenSSL.SSL._ffi.NULL, # type: ignore |
| 510 | OpenSSL.SSL._ffi.NULL, # type: ignore |
| 511 | ) |
| 512 | dh = OpenSSL.SSL._ffi.gc(dh, OpenSSL.SSL._lib.DH_free) # type: ignore |
| 513 | return dh |
| 514 | raise RuntimeError("Error loading DH Params.") # pragma: no cover |
| 515 | |
| 516 | @classmethod |
| 517 | def from_store( |