Skip to content

Commit c00f703

Browse files
bpo-34759: Fix error handling in ssl 'unwrap()' (pythonGH-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 (cherry picked from commit c0da582) Co-authored-by: Nathaniel J. Smith <njs@pobox.com>
1 parent 5c3d8b2 commit c00f703

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
@@ -1735,6 +1735,47 @@ def test_private_init(self):
17351735
with self.assertRaisesRegex(TypeError, "public constructor"):
17361736
ssl.SSLObject(bio, bio)
17371737

1738+
def test_unwrap(self):
1739+
client_ctx, server_ctx, hostname = testing_context()
1740+
c_in = ssl.MemoryBIO()
1741+
c_out = ssl.MemoryBIO()
1742+
s_in = ssl.MemoryBIO()
1743+
s_out = ssl.MemoryBIO()
1744+
client = client_ctx.wrap_bio(c_in, c_out, server_hostname=hostname)
1745+
server = server_ctx.wrap_bio(s_in, s_out, server_side=True)
1746+
1747+
# Loop on the handshake for a bit to get it settled
1748+
for _ in range(5):
1749+
try:
1750+
client.do_handshake()
1751+
except ssl.SSLWantReadError:
1752+
pass
1753+
if c_out.pending:
1754+
s_in.write(c_out.read())
1755+
try:
1756+
server.do_handshake()
1757+
except ssl.SSLWantReadError:
1758+
pass
1759+
if s_out.pending:
1760+
c_in.write(s_out.read())
1761+
# Now the handshakes should be complete (don't raise WantReadError)
1762+
client.do_handshake()
1763+
server.do_handshake()
1764+
1765+
# Now if we unwrap one side unilaterally, it should send close-notify
1766+
# and raise WantReadError:
1767+
with self.assertRaises(ssl.SSLWantReadError):
1768+
client.unwrap()
1769+
1770+
# But server.unwrap() does not raise, because it reads the client's
1771+
# close-notify:
1772+
s_in.write(c_out.read())
1773+
server.unwrap()
1774+
1775+
# And now that the client gets the server's close-notify, it doesn't
1776+
# raise either.
1777+
c_in.write(s_out.read())
1778+
client.unwrap()
17381779

17391780
class SimpleBackgroundTests(unittest.TestCase):
17401781
"""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
@@ -2584,9 +2584,9 @@ _ssl__SSLSocket_shutdown_impl(PySSLSocket *self)
25842584
break;
25852585
}
25862586

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

0 commit comments

Comments
 (0)