mirror of
https://github.com/python/cpython.git
synced 2025-07-24 19:54:21 +00:00
[3.11] GH-95494: Fix transport EOF handling in OpenSSL 3.0 (GH-95495) (#103006)
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 420bbb783b
)
Co-authored-by: David Benjamin <davidben@google.com>
This commit is contained in:
parent
b28f919007
commit
13df5d3497
3 changed files with 32 additions and 7 deletions
|
@ -665,6 +665,16 @@ PySSL_SetError(PySSLSocket *sslsock, int ret, const char *filename, int lineno)
|
|||
ERR_GET_REASON(e) == SSL_R_CERTIFICATE_VERIFY_FAILED) {
|
||||
type = state->PySSLCertVerificationErrorObject;
|
||||
}
|
||||
#if defined(SSL_R_UNEXPECTED_EOF_WHILE_READING)
|
||||
/* OpenSSL 3.0 changed transport EOF from SSL_ERROR_SYSCALL with
|
||||
* zero return value to SSL_ERROR_SSL with a special error code. */
|
||||
if (ERR_GET_LIB(e) == ERR_LIB_SSL &&
|
||||
ERR_GET_REASON(e) == SSL_R_UNEXPECTED_EOF_WHILE_READING) {
|
||||
p = PY_SSL_ERROR_EOF;
|
||||
type = state->PySSLEOFErrorObject;
|
||||
errstr = "EOF occurred in violation of protocol";
|
||||
}
|
||||
#endif
|
||||
break;
|
||||
}
|
||||
default:
|
||||
|
@ -3133,10 +3143,6 @@ _ssl__SSLContext_impl(PyTypeObject *type, int proto_version)
|
|||
#endif
|
||||
#ifdef SSL_OP_SINGLE_ECDH_USE
|
||||
options |= SSL_OP_SINGLE_ECDH_USE;
|
||||
#endif
|
||||
#ifdef SSL_OP_IGNORE_UNEXPECTED_EOF
|
||||
/* Make OpenSSL 3.0.0 behave like 1.1.1 */
|
||||
options |= SSL_OP_IGNORE_UNEXPECTED_EOF;
|
||||
#endif
|
||||
SSL_CTX_set_options(self->ctx, options);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue