Debian Codegraph @

debian/patches/pyopenssl-26.3.0.patch

raw
1 From: Illia Volochii <[email protected]>
2 Date: Mon, 6 Jul 2026 23:18:09 +0300
3 Subject: Replace deprecated pyOpenSSL X509.get_subject and
4 Context.set_passwd_cb methods
5
6 Origin: backport, https://github.com/urllib3/urllib3/pull/5103
7 Bug: https://github.com/urllib3/urllib3/issues/5099
8 Bug-Debian: https://bugs.debian.org/1142214
9 Last-Update: 2026-07-21
10 ---
11 src/urllib3/contrib/pyopenssl.py | 35 +++++++++++++++++++++++++++++------
12 1 file changed, 29 insertions(+), 6 deletions(-)
13
14 diff --git a/src/urllib3/contrib/pyopenssl.py b/src/urllib3/contrib/pyopenssl.py
15 index f06b859..f22bb48 100644
16 --- a/src/urllib3/contrib/pyopenssl.py
17 +++ b/src/urllib3/contrib/pyopenssl.py
18 @@ -42,6 +42,8 @@ from __future__ import annotations
19
20 import OpenSSL.SSL # type: ignore[import-not-found]
21 from cryptography import x509
22 +from cryptography.hazmat.primitives.serialization import load_pem_private_key
23 +from cryptography.x509.oid import NameOID
24
25 try:
26 from cryptography.x509 import UnsupportedExtension # type: ignore[attr-defined]
27 @@ -270,6 +272,15 @@ def get_subj_alt_name(peer_cert: X509) -> list[tuple[str, str]]:
28 return names
29
30
31 +def _get_common_name(peer_cert: X509) -> str | None:
32 + """
33 + Given a pyOpenSSL certificate, return the subject's common name.
34 + """
35 + cert = peer_cert.to_cryptography()
36 + names = cert.subject.get_attributes_for_oid(NameOID.COMMON_NAME)
37 + return typing.cast(str, names[0].value) if names else None
38 +
39 +
40 class WrappedSocket:
41 """API-compatibility wrapper for Python OpenSSL's Connection-class."""
42
43 @@ -394,7 +405,7 @@ class WrappedSocket:
44 return OpenSSL.crypto.dump_certificate(OpenSSL.crypto.FILETYPE_ASN1, x509) # type: ignore[no-any-return]
45
46 return {
47 - "subject": ((("commonName", x509.get_subject().CN),),), # type: ignore[dict-item]
48 + "subject": ((("commonName", _get_common_name(x509)),),), # type: ignore[dict-item]
49 "subjectAltName": get_subj_alt_name(x509),
50 }
51
52 @@ -480,16 +491,28 @@ class PyOpenSSLContext:
53 self,
54 certfile: str,
55 keyfile: str | None = None,
56 - password: str | None = None,
57 + password: str | bytes | None = None,
58 ) -> None:
59 try:
60 self._ctx.use_certificate_chain_file(certfile)
61 if password is not None:
62 if not isinstance(password, bytes):
63 - password = password.encode("utf-8") # type: ignore[assignment]
64 - self._ctx.set_passwd_cb(lambda *_: password)
65 - self._ctx.use_privatekey_file(keyfile or certfile)
66 - except OpenSSL.SSL.Error as e:
67 + password = password.encode("utf-8")
68 + # pyOpenSSL added cryptography-key support in 24.3.0.
69 + # Keep using the older password-callback path until 2026's
70 + # versions because set_passwd_cb() became deprecated in 26.3.0.
71 + if int(OpenSSL.__version__.split(".")[0]) >= 26:
72 + with open(keyfile or certfile, "rb") as key_file:
73 + private_key = load_pem_private_key(key_file.read(), password)
74 + # cryptography's loader returns a wider private-key union
75 + # than pyOpenSSL accepts, so we add `type: ignore` here.
76 + self._ctx.use_privatekey(private_key) # type: ignore[arg-type]
77 + else:
78 + self._ctx.set_passwd_cb(lambda *_: password)
79 + self._ctx.use_privatekey_file(keyfile or certfile)
80 + else:
81 + self._ctx.use_privatekey_file(keyfile or certfile)
82 + except (OpenSSL.SSL.Error, TypeError, ValueError) as e:
83 raise ssl.SSLError(f"Unable to load certificate chain: {e!r}") from e
84
85 def set_alpn_protocols(self, protocols: list[bytes | str]) -> None: