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

No SSL match_hostname() in ftp, imap, nntp, pop, smtp modules #63708

Closed
tiran opened this issue Nov 5, 2013 · 30 comments
Closed

No SSL match_hostname() in ftp, imap, nntp, pop, smtp modules #63708

tiran opened this issue Nov 5, 2013 · 30 comments
Labels
type-security A security issue

Comments

@tiran
Copy link
Member

tiran commented Nov 5, 2013

BPO 19509
Nosy @gvanrossum, @birkenfeld, @pitrou, @larryhastings, @giampaolo, @tiran, @dstufft, @vajrasky
Dependencies
  • bpo-19781: No SSL match_hostname() in ftplib
  • bpo-19782: No SSL match_hostname() in imaplib
  • bpo-19783: No SSL match_hostname() in nntplib
  • bpo-19784: No SSL match_hostname() in poplib
  • bpo-19785: No SSL match_hostname() in smtplib
  • Files
  • sslctx_check_hostname.patch
  • check_hostname_adjustments.patch
  • asyncio_ssl_verify.patch
  • asyncio_ssl_verify2.patch
  • asyncio_ssl_verify3.patch
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2013-12-06.13:16:29.681>
    created_at = <Date 2013-11-05.22:57:12.554>
    labels = ['type-security']
    title = 'No SSL match_hostname() in ftp, imap, nntp, pop, smtp modules'
    updated_at = <Date 2013-12-06.13:16:29.680>
    user = 'https://github.com/tiran'

    bugs.python.org fields:

    activity = <Date 2013-12-06.13:16:29.680>
    actor = 'christian.heimes'
    assignee = 'none'
    closed = True
    closed_date = <Date 2013-12-06.13:16:29.681>
    closer = 'christian.heimes'
    components = []
    creation = <Date 2013-11-05.22:57:12.554>
    creator = 'christian.heimes'
    dependencies = ['19781', '19782', '19783', '19784', '19785']
    files = ['32881', '32940', '32967', '32991', '32996']
    hgrepos = []
    issue_num = 19509
    keywords = ['patch']
    message_count = 30.0
    messages = ['202246', '204318', '204518', '204519', '204520', '204521', '204522', '204523', '204524', '204526', '204532', '204672', '204673', '204681', '204683', '204989', '205052', '205064', '205198', '205199', '205203', '205231', '205241', '205279', '205309', '205319', '205336', '205338', '205351', '205368']
    nosy_count = 11.0
    nosy_names = ['gvanrossum', 'georg.brandl', 'janssen', 'pitrou', 'larry', 'giampaolo.rodola', 'christian.heimes', 'Arfrever', 'python-dev', 'dstufft', 'vajrasky']
    pr_nums = []
    priority = 'high'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'security'
    url = 'https://bugs.python.org/issue19509'
    versions = ['Python 3.4']

    @tiran
    Copy link
    Member Author

    tiran commented Nov 5, 2013

    None of the TLS/SSL classes for ftp, imap, nntp, pop and smtp have support for match_hostname() in order to verify that the SSL cert matches the host name. I'm not sure how we can handle the problem without creating backwards incompatibilities.

    @tiran tiran added the type-security A security issue label Nov 5, 2013
    @tiran
    Copy link
    Member Author

    tiran commented Nov 25, 2013

    The patch implements check_hostname in order to match SSL certs with the peer's hostname in ftp, imap, nntp, pop and smtp library. So far the patch needs more tests and doc updates.

    I consider the new feature a security fix. Right now everybody with any valid TLS/SSL certificate can claim that its certificate is valid for 'smtp.google.com'.

    @larryhastings
    Copy link
    Contributor

    I don't know enough about SSL to make a "feature vs bug/security fix" ruling on this. Can a second person who does--maybe Bill Janssen?--chime in?

    @dstufft
    Copy link
    Member

    dstufft commented Nov 26, 2013

    I agree with Christian, mail.stufft.io should not be able to masquerade as smtp.google.com and being able to do so is a pretty big security hole.

    @pitrou
    Copy link
    Member

    pitrou commented Nov 26, 2013

    I think adding an API in bugfix releases must be an exceptional decision and I'm honestly not convinced this issue justifies it.
    It'd be more convincing if there was actually demand for this feature (e.g. from higher-level library authors).

    @dstufft
    Copy link
    Member

    dstufft commented Nov 26, 2013

    Probably the higher level libraries don't even realize it's not happening, it's similar to the issue of SSL validation for HTTPS connections where a vast swathe of people didn't even realize that their certificates weren't being validated.

    @tiran
    Copy link
    Member Author

    tiran commented Nov 26, 2013

    I agree with Antoine. The five attached bug reports only refer to Python 3.4.

    @pitrou
    Copy link
    Member

    pitrou commented Nov 26, 2013

    Probably the higher level libraries don't even realize it's not
    happening, it's similar to the issue of SSL validation for HTTPS
    connections where a vast swathe of people didn't even realize that
    their certificates weren't being validated.

    Exactly. And we didn't add certificate checking in bugfix releases in
    the name of security.

    Now I appreciate that it would be a bit of a pity to wait for 3.5 to add
    this, so perhaps Larry wants to make an exception to the 3.4 feature
    freeze so that this feature can come in (assuming Christian's patch is
    ready).

    @birkenfeld
    Copy link
    Member

    For a true security fix, the default for check_hostname would have to be True. However, that will create a lot of backward compatibility problems for questionable gain.

    I think Larry should make an exception for 3.4 and allow this new feature.

    @larryhastings
    Copy link
    Contributor

    Okay, you have my permission to add match_hostname() and fix the security hole you cite. Can you have it done soon, so it can bake for a while in trunk before we cut beta 2?

    @dstufft
    Copy link
    Member

    dstufft commented Nov 26, 2013

    I assumed we were talking about 3.4 and didn't even notice that the issues had 3.3 and 3.2 attached to it.

    @tiran
    Copy link
    Member Author

    tiran commented Nov 28, 2013

    My patch could be much simpler and easier if we could just drop support for ancient versions of OpenSSL. My idea requires at least OpenSSL 0.9.8f (release 2007) with SNI support. Six years are a lot for crypto software. All relevant platforms with vendor support have a more recent version of OpenSSL, too.

    >> context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
    >> context.verify_mode = ssl.CERT_REQUIRED
    >> context.check_hostname = True
    >> context.wrap_socket(sock, server_hostname="www.example.org")

    server_hostname is used to for server name indicator (SNI) as well as the hostname for match_hostname(). It would remove lots and lots of code duplication, too.

    The check_hostname takes care about invalid combinations, too:

    >>> context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
    >>> context.verify_mode == ssl.CERT_NONE
    True
    >>> context.check_hostname = True
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ValueError: check_hostname needs a SSL context with either CERT_OPTIONAL or CERT_REQUIRED

    @pitrou
    Copy link
    Member

    pitrou commented Nov 28, 2013

    My patch could be much simpler and easier if we could just drop
    support for ancient versions of OpenSSL. My idea requires at least
    OpenSSL 0.9.8f (release 2007) with SNI support.

    What are you talking about? Your patch doesn't use HAS_SNI.

    @tiran
    Copy link
    Member Author

    tiran commented Nov 28, 2013

    The patches in the dependency tickets are using SNI. The problem is, a non-None server_hostname argument raises an error when OpenSSL doesn't support the feature.

    Here is a demo patch for my idea. It makes it very easy to add hostname matching to existing code. All it takes is the "server_hostname" argument to wrap_socket() and a new property "check_hostname" for the SSLContext object. The rest is done in do_handshake().

    @pitrou
    Copy link
    Member

    pitrou commented Nov 28, 2013

    The patches in the dependency tickets are using SNI.

    They all check for HAS_SNI, which is simple enough.
    If you want to discuss a minimum supported OpenSSL version, that's fine with me, but it should be a separate discussion (on python-dev?).

    If you think your patches are not ready, then please say so, so that I don't review them.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Dec 2, 2013

    New changeset aa531135bc6b by Christian Heimes in branch 'default':
    Issue bpo-19509: Add SSLContext.check_hostname to match the peer's certificate
    http://hg.python.org/cpython/rev/aa531135bc6b

    @tiran
    Copy link
    Member Author

    tiran commented Dec 2, 2013

    I have addressed the five libraries in five sub-issues. http.client and asyncio.selector_events need small adjustments to use the new feature. Please review the patch.

    @gvanrossum
    Copy link
    Member

    The asyncio patch looks fine, but I'd like to see a unittest that actually fails (or mocks failing) the hostname check where it is now performed (in wrap_socket() IIUC?), to make sure that the exception is propagated out properly.

    @tiran
    Copy link
    Member Author

    tiran commented Dec 4, 2013

    I'd rather have integration test with a real SSL connection in order to check the entire stack. asyncio doesn't have a test for CERT_REQUIRED yet. The attached patch tests three common cases: missing CA, missing server_hostname and a successful connection with full verification.

    @tiran
    Copy link
    Member Author

    tiran commented Dec 4, 2013

    PS: The test uses keycert3.pem and pycacert.pem from the 3.4 test directory. keycert3.pem contains privat + public key and is signed by the CA in pycacert.pem.

    @vajrasky
    Copy link
    Mannequin

    vajrasky mannequin commented Dec 4, 2013

    I left my comment on the review. I forgot to mail the review.

    @gvanrossum
    Copy link
    Member

    See my messages on the review. Is it absolutely necessary to close the socket when the hostname verification fails?

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Dec 4, 2013

    New changeset b6fce698e467 by Christian Heimes in branch 'default':
    Issue bpo-19509: Don't close the socket in do_handshake() when hostname verification fails.
    http://hg.python.org/cpython/rev/b6fce698e467

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Dec 5, 2013

    New changeset 1a945fb875bf by Christian Heimes in branch 'default':
    bpo-19509: Don't call match_hostname() twice in http.client.
    http://hg.python.org/cpython/rev/1a945fb875bf

    @tiran
    Copy link
    Member Author

    tiran commented Dec 5, 2013

    The new patch splits the test into three separate test cases.

    Guido, Python 3.3 doesn't have the necessary CA and server cert files. The files were added to 3.4 for some other tests. The patch tries to load the files from 3.4's test directory and falls back to local copies. You have to copy/delete these files to test the patch:

    $ hg rm Lib/test/test_asyncio/sample.*
    $ hg cp Lib/test/ssl_key.pem Lib/test/ssl_cert.pem Lib/test/pycacert.pem Lib/test/keycert3.pem  Lib/test/test_asyncio/

    @gvanrossum
    Copy link
    Member

    Thanks Christian, this is almost right. I'm uploading a patch that covers all bases:

    • Works on Python 3.3 (TEST_HOME_DIR isn't defined there)
    • Works on older Python 3.4 versions (check_hostname may not exist)
    • Works on latest Python 3.4 version

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Dec 5, 2013

    New changeset 1605eda93392 by Christian Heimes in branch 'default':
    Issue bpo-19509: Finish implementation of check_hostname
    http://hg.python.org/cpython/rev/1605eda93392

    @gvanrossum
    Copy link
    Member

    With the latest (revision 1605eda93392) I get four failures on OS X. Three are like this (in all three selector types -- kqueue, select, poll):

    ======================================================================
    ERROR: test_create_ssl_connection (test_events.SelectEventLoopTests)
    ----------------------------------------------------------------------

    Traceback (most recent call last):
      File "tests/test_events.py", line 532, in test_create_ssl_connection
        tr, pr = self.loop.run_until_complete(f)
      File "/Users/guido/tulip/asyncio/base_events.py", line 177, in run_until_complete
        return future.result()
      File "/Users/guido/tulip/asyncio/futures.py", line 221, in result
        raise self._exception
      File "/Users/guido/tulip/asyncio/tasks.py", line 276, in _step
        result = coro.throw(exc)
      File "/Users/guido/tulip/asyncio/base_events.py", line 388, in create_connection
        yield from waiter
      File "/Users/guido/tulip/asyncio/futures.py", line 320, in __iter__
        yield self  # This tells Task to wait for completion.
      File "/Users/guido/tulip/asyncio/tasks.py", line 329, in _wakeup
        value = future.result()
      File "/Users/guido/tulip/asyncio/futures.py", line 221, in result
        raise self._exception
      File "/Users/guido/tulip/asyncio/selector_events.py", line 618, in _on_handshake
        self._sock.do_handshake()
      File "/usr/local/lib/python3.4/ssl.py", line 748, in do_handshake
        self._sslobj.do_handshake()
    ssl.SSLEOFError: EOF occurred in violation of protocol (_ssl.c:599)

    The last is similar in test_streams.py:

    ======================================================================
    ERROR: test_open_connection_no_loop_ssl (test_streams.StreamReaderTests)
    ----------------------------------------------------------------------

    Traceback (most recent call last):
      File "tests/test_streams.py", line 58, in test_open_connection_no_loop_ssl
        reader, writer = self.loop.run_until_complete(f)
      File "/Users/guido/tulip/asyncio/base_events.py", line 177, in run_until_complete
        return future.result()
      File "/Users/guido/tulip/asyncio/futures.py", line 221, in result
        raise self._exception
      File "/Users/guido/tulip/asyncio/tasks.py", line 276, in _step
        result = coro.throw(exc)
      File "/Users/guido/tulip/asyncio/streams.py", line 43, in open_connection
        lambda: protocol, host, port, **kwds)
      File "/Users/guido/tulip/asyncio/base_events.py", line 388, in create_connection
        yield from waiter
      File "/Users/guido/tulip/asyncio/futures.py", line 320, in __iter__
        yield self  # This tells Task to wait for completion.
      File "/Users/guido/tulip/asyncio/tasks.py", line 329, in _wakeup
        value = future.result()
      File "/Users/guido/tulip/asyncio/futures.py", line 221, in result
        raise self._exception
      File "/Users/guido/tulip/asyncio/selector_events.py", line 618, in _on_handshake
        self._sock.do_handshake()
      File "/usr/local/lib/python3.4/ssl.py", line 748, in do_handshake
        self._sslobj.do_handshake()
    ssl.SSLEOFError: EOF occurred in violation of protocol (_ssl.c:599)

    I get the same failures when I copy the changes to the Tulip repo.

    @gvanrossum
    Copy link
    Member

    That's fixed by revision ec1e7fc9b5a4.

    Christian, can this bug be closed now, or is there more?

    @tiran
    Copy link
    Member Author

    tiran commented Dec 6, 2013

    I'm closing this ticket. All features have been implemented and tests are passing, too.

    I have created bpo-19909 as a reminder for doc improvements.

    @tiran tiran closed this as completed Dec 6, 2013
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    type-security A security issue
    Projects
    None yet
    Development

    No branches or pull requests

    6 participants