Commit graph

538 commits

Author SHA1 Message Date
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 Frederick
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 Frederick
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 Bierma
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
Illia Volochii
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
Victor Stinner
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
Serhiy Storchaka
0fb4c38c27
gh-132987: Support __index__() in the ssl.SSLContext.options setter (GH-133098) 2025-04-29 16:28:47 +03:00
Bénédikt Tran
6a9bfee4fa
gh-131423: Update OpenSSL data to 3.4.1 on Linux (#131618)
OpenSSL 3.4.1 mnemonics are not compatible with OpenSSL 3.4.0 ones since
they were renumbered [1, 2]. Consequently, `_ssl_data_34.h` is renamed to
`_ssl_data_340.h` and `_ssl_data_34.h` now contains OpenSSL 3.4.1 mnemonics.

We also refine the mnemonics that are selected, discarding those that are
mnemonic-like but should not be used as such. More precisely, we remove
the ERR_LIB_MASK and ERR_LIB_OFFSET entries from OpenSSL 1.1.1 data.

[1]: https://github.com/openssl/openssl/issues/26316
[2]: https://github.com/openssl/openssl/issues/26388
2025-04-25 10:26:58 +02:00
Rami
9752c84022
gh-89562: Remove hostflags from PySSLContext (GH-28602)
Remove hostflags from PySSLContext as that was only needed for OpenSSL versions prior to 1.1.1
2025-04-18 15:27:32 -07:00
Collin Funk
1b49c8c71b
gh-131127: Minimal build support on systems using LibreSSL (GH-131128) 2025-04-11 15:35:12 +03:00
Chris Eibl
7cbcb6efd9
GH-131296: Fix parenthesis warnings on Windows-specific code (GH-131905) 2025-03-31 15:54:30 +01:00
Steve Dower
d260631be0
gh-131423: Update to OpenSSL 3.0.16. (GH-131839)
The bin tag is 3.0.16.1 because we rebuilt without uplink support to fix gh-131804.
This PR also prevents making calls that are now unsafe without uplink, and updates
the tests to property interpret these failures as unsupported.
2025-03-28 15:07:57 +00:00
Victor Stinner
34c1ea3109
gh-111178: Fix function signatures for multiple tests (#131496) 2025-03-20 12:27:03 +01:00
Bénédikt Tran
e708ac8dd4
gh-111178: fix UBSan failures in Modules/_ssl.c (GH-130719)
* fix UBSan failures for `PySSLContext`, `PySSLSocket`, `PySSLMemoryBIO`, `PySSLSession`
2025-03-17 16:25:28 +01:00
Victor Stinner
79f85a0bc1
gh-129354: Use PyErr_FormatUnraisable() function (#129518)
Replace PyErr_WriteUnraisable() with PyErr_FormatUnraisable().
2025-01-31 15:06:14 +01:00
Erlend E. Aasland
537296cdcd
gh-111178: Generate correct signature for most self converters (#128447) 2025-01-20 12:40:18 +01:00
Petr Viktorin
802556abfa
gh-127257: Add hex code to ssl "unknown error" message (GH-127360)
* ssl: Add hex error code to "unknown error" messages

To make it easier to vary the individual parts of the message,
replace the if-ladder with constant format strings by building
the string piece-wise with PyUnicodeWriter.

Use "unknown error (0x%x)" rather than just "unknown error" if we
can't get a better error message. (Hex makes sense as the error
includes two packed parts.)
2025-01-10 13:59:51 +01:00
Victor Stinner
f89e5e20cb
gh-127350: Add Py_fopen() and Py_fclose() functions (#127821) 2025-01-06 12:43:09 +00:00
Will Childs-Klein
418114c139
gh-128035: Add ssl.HAS_PHA to detect libssl PHA support (GH-128036)
* Add ssl.HAS_PHA to detect libssl Post-Handshake-Auth support

Co-authored-by: Tomas R. <tomas.roun8@gmail.com>
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
2024-12-24 18:29:27 +00:00
Petr Viktorin
f4b31edf2d
gh-127257: ssl: Raise OSError for ERR_LIB_SYS (GH-127361)
From the ERR_raise manpage:

    ERR_LIB_SYS

        This "library code" indicates that a system error is
        being reported.  In this case, the reason code given
        to `ERR_raise()` and `ERR_raise_data()` *must* be
        `errno(3)`.


This PR only handles ERR_LIB_SYS for the high-lever error types
SSL_ERROR_SYSCALL and SSL_ERROR_SSL, i.e., not the ones where
OpenSSL indicates it has some more information about the issue.
2024-12-10 11:56:24 +01:00
Peter Bierma
c112de1da2
gh-126890: Restore stripped ssl docstrings (GH-127281) 2024-12-02 13:50:34 +01:00
Petr Viktorin
db5c5763f3
gh-127330: Update for OpenSSL 3.4 & document+improve the update process (GH-127331)
- Add `git describe` output to headers generated by `make_ssl_data.py`

  This info is more important than the date when the file was generated.
  It does mean that the tool now requires a Git checkout of OpenSSL,
  not for example a release tarball.

- Regenerate the older file to add the info.
  To the other older file, add a note about manual edits.

- Add notes on how to add a new OpenSSL version

- Add 3.4 error messages and multissl tests
2024-11-28 13:29:27 +01:00
Victor Stinner
b1c4ffc205
gh-126455: Disallow _ssl.SSLSocket instantiation (#126481)
Prevent creation of incomplete/invalid _ssl.SSLSocket objects when
created directly.
2024-11-06 11:59:39 +01:00
Victor Stinner
0b67ce930a
gh-126433: Fix compiler warnings on 32-bit Windows (#126444) 2024-11-05 16:05:13 +01:00
sobolevn
a64a1c9206
gh-126106: Fix NULL possible derefrence in Modules/_ssl.c (#126111)
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
2024-10-29 12:03:18 +03:00
Peter Bierma
4c53b25775
gh-124984: Enhance ssl thread safety (#124993)
Make SSL objects thread safe in Free Theaded build by
using critical sections.

Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
2024-10-19 23:16:36 +02:00
Jeffrey R. Van Voorst
7e7223e18f
gh-116810: fix memory leak in ssl module (GH-123249)
Resolve a memory leak introduced in CPython 3.10's :mod:`ssl` when the :attr:`ssl.SSLSocket.session` property was accessed. Speeds up read and write access to said property by no longer unnecessarily cloning session objects via serialization.

Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
Co-authored-by: Gregory P. Smith <greg@krypto.org>
Co-authored-by: Antoine Pitrou <antoine@python.org>
2024-09-30 12:43:50 -07:00
Will Childs-Klein
56a3ce2715
gh-117784: Only reference PHA functions ifndef SSL_VERIFY_POST_HANDSHAKE (GH-117785)
With this change, builds with OpenSSL forks that don't have this functionalty
(like AWS-LC or BoringSSL) will require less patching.
2024-07-01 15:28:35 +02:00
Victor Stinner
12af8ec864
gh-121040: Use __attribute__((fallthrough)) (#121044)
Fix warnings when using -Wimplicit-fallthrough compiler flag.

Annotate explicitly "fall through" switch cases with a new
_Py_FALLTHROUGH macro which uses __attribute__((fallthrough)) if
available. Replace "fall through" comments with _Py_FALLTHROUGH.

Add _Py__has_attribute() macro. No longer define __has_attribute()
macro if it's not defined. Move also _Py__has_builtin() at the top
of pyport.h.

Co-Authored-By: Nikita Sobolev <mail@sobolevn.me>
2024-06-27 09:58:44 +00:00
Brett Simmers
c2627d6eea
gh-116322: Add Py_mod_gil module slot (#116882)
This PR adds the ability to enable the GIL if it was disabled at
interpreter startup, and modifies the multi-phase module initialization
path to enable the GIL when loading a module, unless that module's spec
includes a slot indicating it can run safely without the GIL.

PEP 703 called the constant for the slot `Py_mod_gil_not_used`; I went
with `Py_MOD_GIL_NOT_USED` for consistency with gh-104148.

A warning will be issued up to once per interpreter for the first
GIL-using module that is loaded. If `-v` is given, a shorter message
will be printed to stderr every time a GIL-using module is loaded
(including the first one that issues a warning).
2024-05-03 11:30:55 -04:00
Alexander Kanavin
37ccf16786
gh-101732: Modules/_ssl.c: use Y2038 compatible openssl function when available (GH-118425) 2024-05-03 15:34:05 +02:00
mpage
df73179048
gh-111926: Make weakrefs thread-safe in free-threaded builds (#117168)
Most mutable data is protected by a striped lock that is keyed on the
referenced object's address. The weakref's hash is protected using the
weakref's per-object lock.
 
Note that this only affects free-threaded builds. Apart from some minor
refactoring, the added code is all either gated by `ifdef`s or is a no-op
(e.g. `Py_BEGIN_CRITICAL_SECTION`).
2024-04-08 10:58:38 -04:00
Gregory P. Smith
8cb7d7ff86
gh-117310: Remove extra DECREF on "no ciphers" error path in _ssl._SSLContext constructor (#117309)
Remove extra self DECREF on ssl "no ciphers" error path.

This doesn't come up in practice because nobody links against a broken
OpenSSL library that provides nothing.
2024-03-28 11:11:58 +01:00
yevgeny hong
ea9a296fce
gh-115627: Fix PySSL_SetError handling SSL_ERROR_SYSCALL (GH-115628)
Python 3.10 changed from using SSL_write() and SSL_read() to SSL_write_ex() and
SSL_read_ex(), but did not update handling of the return value.

Change error handling so that the return value is not examined.
OSError (not EOF) is now returned when retval is 0.

According to *recent* man pages of all functions for which we call
PySSL_SetError, (in OpenSSL 3.0 and 1.1.1), their return value should
be used to determine whether an error happened (i.e. if PySSL_SetError
should be called), but not what kind of error happened (so,
PySSL_SetError shouldn't need retval). To get the error,
we need to use SSL_get_error.

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Co-authored-by: Petr Viktorin <encukou@gmail.com>
2024-03-26 08:45:43 +01:00
Victor Stinner
d207c7cd5a
gh-110850: Cleanup pycore_time.h includes (#115724)
<pycore_time.h> include is no longer needed to get the PyTime_t type
in internal header files. This type is now provided by <Python.h>
include. Add <pycore_time.h> includes to C files instead.
2024-02-20 16:50:43 +00:00
Victor Stinner
9af80ec83d
gh-110850: Replace _PyTime_t with PyTime_t (#115719)
Run command:

sed -i -e 's!\<_PyTime_t\>!PyTime_t!g' $(find -name "*.c" -o -name "*.h")
2024-02-20 15:02:27 +00:00
David Benjamin
bce693111b
gh-114572: Fix locking in cert_store_stats and get_ca_certs (#114573)
* gh-114572: Fix locking in cert_store_stats and get_ca_certs

cert_store_stats and get_ca_certs query the SSLContext's X509_STORE with
X509_STORE_get0_objects, but reading the result requires a lock. See
https://github.com/openssl/openssl/pull/23224 for details.

Instead, use X509_STORE_get1_objects, newly added in that PR.
X509_STORE_get1_objects does not exist in current OpenSSLs, but we can
polyfill it with X509_STORE_lock and X509_STORE_unlock.

* Work around const-correctness problem

* Add missing X509_STORE_get1_objects failure check

* Add blurb
2024-02-15 19:24:51 -05:00
Rami
84d1f76092
gh-89532: Remove LibreSSL workarounds (#28728)
Remove LibreSSL specific workaround ifdefs from `_ssl.c` and delete the non-version-specific `_ssl_data.h` file (relevant for OpenSSL < 1.1.1, which we no longer support per PEP 644).

Co-authored-by: Christian Heimes <christian@python.org>
Co-authored-by: Gregory P. Smith <greg@krypto.org>
2024-01-06 23:25:58 +00:00