Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for server hostname verification #796

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/OpenSSL/SSL.py
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,12 @@ def explode(*args, **kwargs):
)


_requires_x509_verify = _make_requires(
_lib.Cryptography_HAS_102_VERIFICATION_PARAMS,
"X509 verification not available"
)


class Session(object):
"""
A class representing an SSL session. A session defines certain connection
Expand Down Expand Up @@ -1688,6 +1694,17 @@ def set_tlsext_host_name(self, name):
# XXX I guess this can fail sometimes?
_lib.SSL_set_tlsext_host_name(self._ssl, name)

@_requires_x509_verify
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about integrating this into set_tlsext_host_name, so there's only a single method call required for correct/secure usage?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Certainly doable, but the main difference with set_tlsext_host_name is that it should not be called on IP addresses, per RFC6066, Section 3

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent point, you have to use X509_VERIFY_PARAM_set1_ip() for IP address validation any way. Feel free to copy my implementation from Python's ssl module. The function _ssl_configure_hostname() takes care of SNI, hostname verification, and special cases IP addresses.

https://github.com/python/cpython/blob/ba251c2ae6654bfc8abd9d886b219698ad34ac3c/Modules/_ssl.c#L862-L926

def set_verify_host_name(self, hostname):
param = _lib.SSL_get0_param(self._ssl)
flags = _lib.X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS
if _lib.Cryptography_HAS_110_VERIFICATION_PARAMS:
flags |= _lib.X509_CHECK_FLAG_NEVER_CHECK_SUBJECT
_lib.X509_VERIFY_PARAM_set_hostflags(param, flags)
if not _lib.X509_VERIFY_PARAM_set1_host(param, hostname,
len(hostname)):
raise Error("X509_VERIFY_PARAM_set1_host call failed")

def pending(self):
"""
Get the number of bytes that can be safely read from the SSL buffer
Expand Down