8000 bpo-34759: Fix error handling in ssl 'unwrap()' (GH-9468) · python/cpython@c0da582 · GitHub
[go: up one dir, main page]

Skip to content

Commit c0da582

Browse files
njsmithmiss-islington
authored andcommitted
bpo-34759: Fix error handling in ssl 'unwrap()' (GH-9468)
OpenSSL follows the convention that whenever you call a function, it returns an error indicator value; and if this value is negative, then you need to go look at the actual error code to see what happened. Commit c6fd1c1 introduced a small mistake in _ssl__SSLSocket_shutdown_impl: instead of checking whether the error indicator was negative, it started checking whether the actual error code was negative, and it turns out that the error codes are never negative. So the effect was that 'unwrap()' lost the ability to raise SSL errors. https://bugs.python.org/issue34759
1 parent 026337a commit c0da582

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

Lib/test/test_ssl.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1715,6 +1715,47 @@ def test_private_init(self):
17151715
with self.assertRaisesRegex(TypeError, "public constructor"):
17161716
ssl.SSLObject(bio, bio)
17171717

1718+
def test_unwrap(self):
1719+
client_ctx, server_ctx, hostname = testing_context()
1720+
c_in = ssl.MemoryBIO()
1721+
c_out = ssl.MemoryBIO()
1722+
s_in = ssl.MemoryBIO()
1723+
s_out = ssl.MemoryBIO()
1724+
client = client_ctx.wrap_bio(c_in, c_out, server_hostname=hostname)
1725+
server = server_ctx.wrap_bio(s_in, s_out, server_side=True)
1726+
1727+
# Loop on the handshake for a bit to get it settled
1728+
for _ in range(5):
1729+
try:
1730+
client.do_handshake()
1731+
except ssl.SSLWantReadError:
1732+
pass
1733+
if c_out.pending:
1734+
s_in.write(c_out.read())
1735+
try:
1736+
server.do_handshake()
1737+
except ssl.SSLWantReadError:
1738+
pass
1739+
if s_out.pending:
1740+
c_in.write(s_out.read())
1741+
# Now the handshakes should be complete (don't raise WantReadError)
1742+
client.do_handshake()
1743+
server.do_handshake()
1744+
1745+
# Now if we unwrap one side unilaterally, it should send close-notify
1746+
# and raise WantReadError:
1747+
with self.assertRaises(ssl.SSLWantReadError):
1748+
client.unwrap()
1749+
1750+
# But server.unwrap() does not raise, because it reads the client's
1751+
# close-notify:
1752+
s_in.write(c_out.read())
1753+
server.unwrap()
1754+
1755+
# And now that the client gets the server's close-notify, it doesn't
1756+
# raise either.
1757+
c_in.write(s_out.read())
1758+
client.unwrap()
17181759

17191760
class SimpleBackgroundTests(unittest.TestCase):
17201761
"""Tests that connect to a simple server running in the background"""

Modules/_ssl.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2583,9 +2583,9 @@ _ssl__SSLSocket_shutdown_impl(PySSLSocket *self)
25832583
break;
25842584
}
25852585

2586-
if (err.ssl < 0) {
2586+
if (ret < 0) {
25872587
Py_XDECREF(sock);
2588-
return PySSL_SetError(self, err.ssl, __FILE__, __LINE__);
2588+
return PySSL_SetError(self, ret, __FILE__, __LINE__);
25892589
}
25902590
if (sock)
25912591
/* It's already INCREF'ed */

0 commit comments

Comments
 (0)
0