Skip to content

Commit 00464bb

Browse files
authored
gh-94199: Remove the ssl.wrap_socket() function (#94203)
Remove the ssl.wrap_socket() function, deprecated in Python 3.7: instead, create a ssl.SSLContext object and call its sl.SSLContext.wrap_socket() method. Any package that still uses ssl.wrap_socket() is broken and insecure. The function neither sends a SNI TLS extension nor validates server hostname. Code is subject to CWE-295 : Improper Certificate Validation.
1 parent 23ee4a8 commit 00464bb

File tree

4 files changed

+16
-60
lines changed

4 files changed

+16
-60
lines changed

Doc/whatsnew/3.12.rst

+9
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,15 @@ Removed
304304
:pep:`451` for the rationale.
305305
(Contributed by Victor Stinner in :gh:`94379`.)
306306

307+
* Remove the :func:`ssl.wrap_socket` function, deprecated in Python 3.7:
308+
instead, create a :class:`ssl.SSLContext` object and call its
309+
:class:`ssl.SSLContext.wrap_socket` method. Any package that still uses
310+
:func:`ssl.wrap_socket` is broken and insecure. The function neither sends a
311+
SNI TLS extension nor validates server hostname. Code is subject to `CWE-295
312+
<https://cwe.mitre.org/data/definitions/295.html>`_: Improper Certificate
313+
Validation.
314+
(Contributed by Victor Stinner in :gh:`94199`.)
315+
307316

308317
Porting to Python 3.12
309318
======================

Lib/ssl.py

-30
Original file line numberDiff line numberDiff line change
@@ -1357,36 +1357,6 @@ def version(self):
13571357
SSLContext.sslobject_class = SSLObject
13581358

13591359

1360-
def wrap_socket(sock, keyfile=None, certfile=None,
1361-
server_side=False, cert_reqs=CERT_NONE,
1362-
ssl_version=PROTOCOL_TLS, ca_certs=None,
1363-
do_handshake_on_connect=True,
1364-
suppress_ragged_eofs=True,
1365-
ciphers=None):
1366-
warnings.warn(
1367-
"ssl.wrap_socket() is deprecated, use SSLContext.wrap_socket()",
1368-
category=DeprecationWarning,
1369-
stacklevel=2
1370-
)
1371-
if server_side and not certfile:
1372-
raise ValueError("certfile must be specified for server-side "
1373-
"operations")
1374-
if keyfile and not certfile:
1375-
raise ValueError("certfile must be specified")
1376-
context = SSLContext(ssl_version)
1377-
context.verify_mode = cert_reqs
1378-
if ca_certs:
1379-
context.load_verify_locations(ca_certs)
1380-
if certfile:
1381-
context.load_cert_chain(certfile, keyfile)
1382-
if ciphers:
1383-
context.set_ciphers(ciphers)
1384-
return context.wrap_socket(
1385-
sock=sock, server_side=server_side,
1386-
do_handshake_on_connect=do_handshake_on_connect,
1387-
suppress_ragged_eofs=suppress_ragged_eofs
1388-
)
1389-
13901360
# some utility functions
13911361

13921362
def cert_time_to_seconds(cert_time):

Lib/test/test_ssl.py

-30
Original file line numberDiff line numberDiff line change
@@ -629,36 +629,6 @@ def test_openssl111_deprecations(self):
629629
str(cm.warning)
630630
)
631631

632-
@ignore_deprecation
633-
def test_errors_sslwrap(self):
634-
sock = socket.socket()
635-
self.assertRaisesRegex(ValueError,
636-
"certfile must be specified",
637-
ssl.wrap_socket, sock, keyfile=CERTFILE)
638-
self.assertRaisesRegex(ValueError,
639-
"certfile must be specified for server-side operations",
640-
ssl.wrap_socket, sock, server_side=True)
641-
self.assertRaisesRegex(ValueError,
642-
"certfile must be specified for server-side operations",
643-
ssl.wrap_socket, sock, server_side=True, certfile="")
644-
with ssl.wrap_socket(sock, server_side=True, certfile=CERTFILE) as s:
645-
self.assertRaisesRegex(ValueError, "can't connect in server-side mode",
646-
s.connect, (HOST, 8080))
647-
with self.assertRaises(OSError) as cm:
648-
with socket.socket() as sock:
649-
ssl.wrap_socket(sock, certfile=NONEXISTINGCERT)
650-
self.assertEqual(cm.exception.errno, errno.ENOENT)
651-
with self.assertRaises(OSError) as cm:
652-
with socket.socket() as sock:
653-
ssl.wrap_socket(sock,
654-
certfile=CERTFILE, keyfile=NONEXISTINGCERT)
655-
self.assertEqual(cm.exception.errno, errno.ENOENT)
656-
with self.assertRaises(OSError) as cm:
657-
with socket.socket() as sock:
658-
ssl.wrap_socket(sock,
659-
certfile=NONEXISTINGCERT, keyfile=NONEXISTINGCERT)
660-
self.assertEqual(cm.exception.errno, errno.ENOENT)
661-
662632
def bad_cert_test(self, certfile):
663633
"""Check that trying to use the given client certificate fails"""
664634
certfile = os.path.join(os.path.dirname(__file__) or os.curdir,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Remove the :func:`ssl.wrap_socket` function, deprecated in Python 3.7: instead,
2+
create a :class:`ssl.SSLContext` object and call its
3+
:class:`ssl.SSLContext.wrap_socket` method. Any package that still uses
4+
:func:`ssl.wrap_socket` is broken and insecure. The function neither sends a
5+
SNI TLS extension nor validates server hostname. Code is subject to `CWE-295
6+
<https://cwe.mitre.org/data/definitions/295.html>`_: Improper Certificate
7+
Validation. Patch by Victor Stinner.

0 commit comments

Comments
 (0)