summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2023-03-27 14:14:24 (GMT)
committerGitHub <noreply@github.com>2023-03-27 14:14:24 (GMT)
commitb5bf6c1b2275bfa93e77989bec0711c3e97932a6 (patch)
treec53b879781e31cc86c8768938cf804b12fec2be7 /Lib
parentae8a721c2ba2a48de0cb69fa868fa8fc207b129d (diff)
downloadcpython-b5bf6c1b2275bfa93e77989bec0711c3e97932a6.zip
cpython-b5bf6c1b2275bfa93e77989bec0711c3e97932a6.tar.gz
cpython-b5bf6c1b2275bfa93e77989bec0711c3e97932a6.tar.bz2
[3.10] GH-95494: Fix transport EOF handling in OpenSSL 3.0 (GH-95495) (#103007)
GH-25309 enabled SSL_OP_IGNORE_UNEXPECTED_EOF by default, with a comment that it restores OpenSSL 1.1.1 behavior, but this wasn't quite right. That option causes OpenSSL to treat transport EOF as the same as close_notify (i.e. SSL_ERROR_ZERO_RETURN), whereas Python actually has distinct SSLEOFError and SSLZeroReturnError exceptions. (The latter is usually mapped to a zero return from read.) In OpenSSL 1.1.1, the ssl module would raise them for transport EOF and close_notify, respectively. In OpenSSL 3.0, both act like close_notify. Fix this by, instead, just detecting SSL_R_UNEXPECTED_EOF_WHILE_READING and mapping that to the other exception type. There doesn't seem to have been any unit test of this error, so fill in the missing one. This had to be done with the BIO path because it's actually slightly tricky to simulate a transport EOF with Python's fd based APIs. (If you instruct the server to close the socket, it gets confused, probably because the server's SSL object is still referencing the now dead fd?) (cherry picked from commit 420bbb783b43216cc897dc8914851899db37a31d) Co-authored-by: David Benjamin <davidben@google.com>
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_ssl.py18
1 files changed, 15 insertions, 3 deletions
diff --git a/Lib/test/test_ssl.py b/Lib/test/test_ssl.py
index addfb6c..c28a574 100644
--- a/Lib/test/test_ssl.py
+++ b/Lib/test/test_ssl.py
@@ -155,7 +155,6 @@ OP_SINGLE_DH_USE = getattr(ssl, "OP_SINGLE_DH_USE", 0)
OP_SINGLE_ECDH_USE = getattr(ssl, "OP_SINGLE_ECDH_USE", 0)
OP_CIPHER_SERVER_PREFERENCE = getattr(ssl, "OP_CIPHER_SERVER_PREFERENCE", 0)
OP_ENABLE_MIDDLEBOX_COMPAT = getattr(ssl, "OP_ENABLE_MIDDLEBOX_COMPAT", 0)
-OP_IGNORE_UNEXPECTED_EOF = getattr(ssl, "OP_IGNORE_UNEXPECTED_EOF", 0)
# Ubuntu has patched OpenSSL and changed behavior of security level 2
# see https://bugs.python.org/issue41561#msg389003
@@ -1199,8 +1198,7 @@ class ContextTests(unittest.TestCase):
# SSLContext also enables these by default
default |= (OP_NO_COMPRESSION | OP_CIPHER_SERVER_PREFERENCE |
OP_SINGLE_DH_USE | OP_SINGLE_ECDH_USE |
- OP_ENABLE_MIDDLEBOX_COMPAT |
- OP_IGNORE_UNEXPECTED_EOF)
+ OP_ENABLE_MIDDLEBOX_COMPAT)
self.assertEqual(default, ctx.options)
with warnings_helper.check_warnings():
ctx.options |= ssl.OP_NO_TLSv1
@@ -2362,6 +2360,20 @@ class SimpleBackgroundTests(unittest.TestCase):
self.assertEqual(buf, b'foo\n')
self.ssl_io_loop(sock, incoming, outgoing, sslobj.unwrap)
+ def test_transport_eof(self):
+ client_context, server_context, hostname = testing_context()
+ with socket.socket(socket.AF_INET) as sock:
+ sock.connect(self.server_addr)
+ incoming = ssl.MemoryBIO()
+ outgoing = ssl.MemoryBIO()
+ sslobj = client_context.wrap_bio(incoming, outgoing,
+ server_hostname=hostname)
+ self.ssl_io_loop(sock, incoming, outgoing, sslobj.do_handshake)
+
+ # Simulate EOF from the transport.
+ incoming.write_eof()
+ self.assertRaises(ssl.SSLEOFError, sslobj.read)
+
@support.requires_resource('network')
class NetworkedTests(unittest.TestCase):