570 Commits
Author SHA1 Message Date
Christian HeimesandClaude Opus 4.8 7dd8b4736a gh-160040: Distrust compromised eMudhra intermediate CA keys (CVE-2026-115557) (GH-160042)
The ssl module now rejects TLS certificate chains that contain any of a set of
compromised eMudhra intermediate CA keys.  This is an extraordinary measure and
a last line of defense: removing trust is primarily the job of the platform root
store, and the built-in denylist only blocks connections while that update has
not yet reached the OS, a container image, or a bundled trust store in various
Python packages.  Entries are added to the denylist only under a very high
threshold.

The denylist matches on the public key (the SHA-256 digest of the RFC-5280
SubjectPublicKeyInfo), so it also covers the cross-signed certificates that
reuse one of the distrusted keys.

---------

Signed-off-by: Christian Heimes <christian@python.org>
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-10-24 05:40:49 +00:00
46edd6fa41 gh-89730: Implement support for Encrypted Client Hello (ECH) in ssl (GH-160318)
Co-authored-by: irl <iain@learmonth.me>
Co-authored-by: Gregory P. Smith <68491+gpshead@users.noreply.github.com>
2026-10-23 20:02:23 -04:00
Gregory P. SmithandBénédikt Tran a4ac2445c5 gh-160171: Fix ssl get_unverified_chain() on resumed server sessions (GH-160172)
Include the client's certificate when the session was resumed from a
ticket, and return [] instead of raising AttributeError when the client
sent no certificate.

ANT-2026-2RMA0IPV

Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
2026-10-23 06:26:33 +00:00
Jelle ZijlstraandJelle Zijlstra b2b0a33709 gh-159739: Release SSL resources on error paths (GH-159745)
Free decoded subject alternative names when certificate conversion
fails, and close the key log file if the BIO allocation fails.

Backport to 3.15 and 3.14.

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
2026-10-19 20:42:04 +00:00
Bénédikt Tran a071788e3f gh-155782: avoid a crash in ssl.SSLObject.group for session-less objects (#155829) 2026-10-19 12:26:38 -07:00
Seth LarsonandGregory P. Smith 34a53dce81 gh-156293: Use-after-free for server-side SSLContext with sni_callback (#158504)
Co-authored-by: Gregory P. Smith <68491+gpshead@users.noreply.github.com>
2026-09-30 15:48:00 +00:00
Serhiy StorchakaandClaude Opus 5 3b0c39cb8b gh-157513: Add structured version info for ssl (GH-157515)
ssl.OPENSSL_VERSION_INFO is now a named tuple.

Add ssl.OPENSSL_API_VERSION_INFO which provides information about the
version of the OpenSSL library that was used for building the module.
The private name _OPENSSL_API_VERSION is kept as an alias.

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-29 15:53:11 +03:00
Serhiy Storchaka 6b5d4143f3 gh-113318: Better implementation of @getter and @setter in Argument Clinic (GH-156066)
The value of a @setter is now a parameter, which can use a converter,
and a @getter can define a return converter.  Declaring the value is
optional.

The accessors of an attribute are now collected in a Property, so the
entry of PyGetSetDef is identified by the Python name instead of the C
basename, and is composed without the help of the preprocessor unless
some accessor is compiled conditionally.
2026-09-19 18:48:39 +03:00
Victor Stinner c7dbdb5f6b gh-155742: Use PyBytesWriter in PySSL_RAND() (#157731)
Replace soft deprecated PyBytes_FromStringAndSize() with
PyBytesWriter.

Remove 'pseudo' parameter of PySSL_RAND(): it's no longer needed since
ssl.RAND_pseudo_bytes() has been removed (in 2022,
commit d435a18c53).

Add a test on ssl.RAND_bytes(0).
2026-09-18 15:10:42 +02:00
Victor Stinner e2ca4dc94c gh-157495: Implement Py_MIN/MAX/ABS using typeof() (#157496)
On GCC and clang, implement Py_MIN(), Py_MAX() and Py_ABS()
using typeof() and statement expression to only evaluate each
argument once and detect signed/unsigned comparison.

Test also these macros in test_cext and test_cppext to test different
compiler flags and test the limited C API.
2026-09-15 23:16:43 +02:00
Kumar Aditya 39a9a47516 gh-156228: Use PyMem_Raw allocators for OpenSSL in _ssl and _hashlib (#156229) 2026-09-02 18:26:17 +05:30
Serhiy StorchakaandClaude Opus 5 2c47c65f2e gh-156261: Move converters used in several files to Argument Clinic (GH-156262)
The pid_t, Py_off_t, HANDLE, DWORD and BOOL converters were defined in
9 files.  The Py_off_t type and its converter function are now shared too.
DWORD in _winapi is used with bitwise=True to keep accepting negative
values.

The pid_t converter is now used for the pid parameters in the
_remote_debugging module, which were declared as int.

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-23 19:47:55 +03:00
Serhiy Storchaka 39b058f8ad gh-154053: Fix compilation of the ssl module against LibreSSL (GH-154054)
LibreSSL does not provide SSL_CTX_set1_sigalgs_list() or
SSL_CTX_set1_client_sigalgs_list(), added in gh-138252, so _ssl failed to
compile against LibreSSL on 3.15+. Guard the set_server_sigalgs() and
set_client_sigalgs() methods so they raise NotImplementedError on LibreSSL,
and skip the corresponding tests.
2026-07-19 12:38:47 +03:00
thexai 4221ce7e30 gh-152433: Allow _ssl module to build for Windows UWP API set (GH-152791) 2026-07-06 17:32:36 +01:00
Serhiy Storchaka 287c98f4cb gh-150285: Fix too long docstrings in Argument Clinic code (GH-150338) 2026-05-24 16:16:12 +03:00
Kirill Ignatev 8b31d08e62 gh-149816: Fix SNI callback callable race (GH-150018) 2026-05-19 20:36:46 +02:00
Victor StinnerandGregory P. Smith 7b7fa3f9bf gh-148292: Update _ssl._SSLSocket for OpenSSL 4 (#149102)
The _SSLSocket object now remembers if it gets an EOF error. In this
case, read(), sendfile(), write() and do_handshake method calls fail
with SSLEOFError without calling the underlying OpenSSL function.

Co-authored-by: Gregory P. Smith <greg@krypto.org>
2026-05-04 13:52:57 +02:00
Victor Stinner 70eb56be42 gh-148600: Add Modules/_ssl_data_40.h data (#148601)
The Modules/_ssl_data_40.h file was created with the commands:

python Tools/ssl/multissltests.py --steps=library --base-directory "$PWD/multissl" --openssl '4.0.0' --system Linux
python Tools/ssl/make_ssl_data.py multissl/src/openssl-4.0.0 Modules/_ssl_data_40.h

Update Modules/_ssl.c to include it on OpenSSL 4.0.0 and newer.

Update test_ssl for the new error message.
2026-04-15 15:59:02 +02:00
Sergey MiryanovandVictor Stinner c1b20a6d96 GH-145247: Use _PyTuple_FromPair in Modules (part 2) (#148003)
Co-authored-by: Victor Stinner <vstinner@python.org>
2026-04-02 23:24:03 +02:00
sunmy2019 1c396e1821 gh-146615: Fix format specifiers in extension modules (GH-146617) 2026-03-31 10:57:37 +03:00
Bénédikt Tran 24db78c532 gh-146080: fix a crash in SNI callbacks when the SSL object is gone (#146573) 2026-03-29 14:27:22 +02:00
Victor StinnerandBénédikt Tran 3364e7e62f gh-146207: Add support for OpenSSL 4.0.0 alpha1 (#146217)
OpenSSL 4.0.0 alpha1 removed these functions:

* SSLv3_method()
* TLSv1_method()
* TLSv1_1_method()
* TLSv1_2_method()

Other changes:

* Update test_openssl_version().
* Update multissltests.py for OpenSSL 4.
* Add const qualifier to fix compiler warnings.

Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
2026-03-25 07:44:47 +01:00
Karolina SurmaandVictor Stinner 1887a95f51 gh-128341: Use _Py_ABI_SLOT in stdlib modules (#145770)
Rename from _Py_INTERNAL_ABI_SLOT to _Py_ABI_SLOT
and define the macro using _PyABIInfo_DEFAULT.

Use the ABI slot in stdlib extension modules to enable running
a check of ABI version compatibility.

_tkinter, _tracemalloc and readline don't use the slots, hence they need
explicit handling.

Co-authored-by: Victor Stinner <vstinner@python.org>
2026-03-24 17:47:55 +00:00
Bénédikt Tran 3a2a686cc4 gh-142516: fix reference leaks in ssl.SSLContext objects (#143685) 2026-02-21 12:31:16 +01:00
Ramin Farajpour Cami c91638ca06 gh-144833: Fix use-after-free in SSL module when SSL_new() fails (GH-144843)
In newPySSLSocket(), when SSL_new() returns NULL, Py_DECREF(self)
was called before _setSSLError(get_state_ctx(self), ...), causing
a use-after-free. Additionally, get_state_ctx() was called with
self (PySSLSocket*) instead of sslctx (PySSLContext*), which is
a type confusion bug.

Fix by calling _setSSLError() before Py_DECREF() and using
sslctx instead of self for get_state_ctx().
2026-02-15 18:43:07 -08:00
1a637b29aa gh-144249: Report filename in SSLContext.load_cert_chain errors (#144250)
When user tries to load a certificate chain, attach the related
filename to the exception being raised. Improving user experience.

Co-authored-by: Victor Stinner <vstinner@python.org>
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
2026-01-28 11:20:51 +01:00
Sam Gross ee4e14aa4c gh-143756: Avoid borrowed reference in SSL code (gh-143816)
GET_SOCKET() returned a borrowed reference, which was potentially
unsafe. Also, refactor out some common code.
2026-01-22 14:02:48 -05:00
Sam Gross bcf9cb0217 gh-143756: Fix potential data race in SSLContext.load_cert_chain (gh-143818)
Concurrent calls to `load_cert_chain` caused data races in OpenSSL code.
2026-01-22 14:02:30 -05:00
Hugo van KemenadeandGregory P. Smith a126893fa8 gh-143960: Add support for OpenSSL 3.6, drop EOL 3.2 (#143961)
Co-authored-by: Gregory P. Smith <68491+gpshead@users.noreply.github.com>
2026-01-20 17:25:31 +02:00
Sam Gross 7d155d7915 gh-140795: Remove 'exc' field in SSLObject (gh-143491)
The 'exc' field was used by our debug SSL callbacks. Keep the exception
in the normal per-thread state to avoid shared mutable state between
threads.

This also avoids a reference count leak if the Python callback raised an
exception because it can be called multiple times per SSL operation.
2026-01-12 17:14:13 +00:00
Sam Gross 96ab379dca gh-140795: Keep 'err' in local variable in _ssl.c (gh-143275)
The error return code doesn't need to be mutable state on the SSLSocket.
This simplifes thread-safety and avoids potential reentrancy issues.
2025-12-30 19:45:44 -05:00
David BenjaminandBénédikt Tran c41fce08a5 gh-141801: Use accessors for ASN1_STRING fields in libssl (GH-141802)
* gh-141801: Use accessors for ASN1_STRING fields

While ASN1_STRING is currently exposed, it is better to use the
accessors. See https://github.com/openssl/openssl/issues/29117 where, if
the type were opaque, OpenSSL's X509 objects could be much more
memory-efficient.

* Update Modules/_ssl.c

Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>

* Update Modules/_ssl.c

Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>

---------

Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
2025-11-22 09:17:45 -08:00
Bénédikt Tran cdd3eee7fc gh-139929: fix incorrect OpenSSL version-based guard in _ssl.c (GH-139945)
fix OpenSSL version-based guards
2025-10-11 10:34:08 -07:00
Bénédikt Tran b04a57deef gh-139748: fix leaks in AC error paths when using unicode FS-based converters (#139765) 2025-10-08 20:52:44 +05:30
Skip Montanaro d1ca001d35 gh-127330: Comment correction in _ssl.c (#139603) 2025-10-05 19:15:46 +01:00
Sergey Miryanov 1588413ca7 gh-116946: remove unnecessary gc from immutable types (#139073) 2025-10-01 13:15:58 +05:30
Victor Stinner 446587c58e gh-129813, PEP 782: Use PyBytesWriter in _ssl (#138929)
Replace PyBytes_FromStringAndSize(NULL, size) and _PyBytes_Resize()
with the new public PyBytesWriter API.
2025-09-18 16:54:18 +02:00
Victor Stinner d6a6fe2a5b gh-129813, PEP 782: Use PyBytesWriter in ssl.MemoryBIO (#139113)
Replace PyBytes_FromStringAndSize(NULL, size) and _PyBytes_Resize()
with the new public PyBytesWriter API.
2025-09-18 14:25:31 +02:00
Ron FrederickandBénédikt Tran 6401823db3 gh-138252: Add support in SSL module for getting and setting TLS signature algorithms (#138269)
The signature algorithms allowed for certificate-based client authentication or
for the server to complete the TLS handshake can be defined on a SSL context via
`ctx.set_client_sigalgs()` and `ctx.set_server_sigalgs()`.

With OpenSSL 3.4 or later, the list of available TLS algorithms can be retrieved
by `ssl.get_sigalgs()`.

With OpenSSL 3.5 or later, the selected signature algorithms can be retrieved from
SSL sockets via `socket.client_sigalg()` and `socket.server_sigalg()`.

This commit also partially amends 377b787618
by using `PyUnicode_DecodeFSDefault` instead of `PyUnicode_DecodeASCII` in
`_ssl._SSLContext.get_groups`, so that functions consistently decode strings
obtained from OpenSSL.

---------

Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
2025-09-08 08:36:12 +00:00
Peter Bierma 4f6ecd10c2 gh-138342: Use a common utility for visiting an object's type (GH-138343)
Add `_PyObject_VisitType` in place of `tp_traverse` functions that only visit the object's type.
2025-09-01 16:20:33 +00:00
Ron FrederickandBénédikt Tran bacb7771fb gh-137197: Add SSLContext.set_ciphersuites to set TLSv1.3 ciphers (#137198)
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
2025-08-30 09:32:11 +00:00
Adam Turner 918e3ba6c0 GH-137623: Use an AC decorator for docstring line length enforcement (#137690) 2025-08-18 18:29:00 +01:00
Zachary Ware 7a703c8f19 gh-132339: Add support for OpenSSL 3.5 (GH-137720)
* Add OpenSSL 3.5.2 definitions to Modules/_ssl_data_35.h (moved from Modules/_ssl_data_34.h)

* Demote OpenSSL 3.1 to "old", remove it from CI

* Update all OpenSSL versions to latest patchlevel in CI config and multissltests defaults

* Add OpenSSL 3.5.2 to CI configuration and multissltests default list

* Fix a typo in the argument parser description of multissltests.py
2025-08-13 23:18:03 -05:00
Peter Bierma 55788a9096 gh-137583: Only lock the SSL context, not the SSL socket (GH-137588)
Fixes a deadlock in 3.13.6.
2025-08-10 14:47:11 +00:00
Ron Frederick 377b787618 gh-136306: Add support for getting and setting SSL groups (#136307)
Add support for getting and setting groups used for key agreement.

* `ssl.SSLSocket.group()` returns the name of the group used
  for the key agreement of the current session establishment.
  This feature requires Python to be built with OpenSSL 3.2 or later.

* `ssl.SSLContext.get_groups()` returns the list of names of groups
  that are compatible with the TLS version of the current context.
  This feature requires Python to be built with OpenSSL 3.5 or later.

* `ssl.SSLContext.set_groups()` sets the groups allowed for key agreement
  for sockets created with this context. This feature is always supported.
2025-07-28 19:33:31 +02:00
Peter BiermaandGregory P. Smith e047a35b23 gh-134698: Hold a lock when the thread state is detached in ssl (GH-134724)
Lock when the thread state is detached.

Co-authored-by: Gregory P. Smith <greg@krypto.org>
2025-07-25 08:16:05 -07:00
5a20e79725 gh-99813: Start using SSL_sendfile when available (#99907)
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
2025-07-12 12:42:35 +00:00
f49a07b531 gh-133968: Add PyUnicodeWriter_WriteASCII() function (#133973)
Replace most PyUnicodeWriter_WriteUTF8() calls with
PyUnicodeWriter_WriteASCII().

Unrelated change to please the linter: remove an unused
import in test_ctypes.

Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
2025-05-29 14:54:30 +00:00
Will Childs-Klein 6801bd32cb gh-133623: Add ssl.HAS_PSK_TLS13 to detect external TLS 1.3 PSK support (#133624) 2025-05-09 09:09:09 +02:00
Xuehai Pan f0f93ba5fa gh-131942: Use the Python-specific Py_DEBUG macro rather than _DEBUG in Windows-related C code (GH-131944) 2025-05-08 15:01:25 +00:00