Skip to content

ssl.SSLObject method getpeercert() is buggy, do_handshake() is strange #73520

Closed

Description

@GregStark
mannequin
BPO 29334
Nosy @tiran, @benjaminp, @njsmith, @iritkatriel
PRs
  • bpo-29334: Fix ssl.getpeercert for auto-handshake #1769
  • [3.6] bpo-29334: Fix ssl.getpeercert for auto-handshake (GH-1769) #1778
  • [3.5] bpo-29334: Fix ssl.getpeercert for auto-handshake (GH-1769) #1779
  • Files
  • sslbugs.py: A short python program that illustrates the issues reported
  • 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 2020-09-19.14:09:52.162>
    created_at = <Date 2017-01-20.12:13:47.650>
    labels = ['expert-SSL', 'type-bug']
    title = 'ssl.SSLObject method getpeercert() is buggy, do_handshake() is strange'
    updated_at = <Date 2020-09-19.14:09:52.162>
    user = 'https://bugs.python.org/GregStark'

    bugs.python.org fields:

    activity = <Date 2020-09-19.14:09:52.162>
    actor = 'benjamin.peterson'
    assignee = 'none'
    closed = True
    closed_date = <Date 2020-09-19.14:09:52.162>
    closer = 'benjamin.peterson'
    components = ['SSL']
    creation = <Date 2017-01-20.12:13:47.650>
    creator = 'Greg Stark'
    dependencies = []
    files = ['46364']
    hgrepos = []
    issue_num = 29334
    keywords = []
    message_count = 13.0
    messages = ['285903', '285905', '285906', '285922', '294235', '294236', '294291', '295798', '297461', '301370', '312886', '312887', '377160']
    nosy_count = 5.0
    nosy_names = ['christian.heimes', 'benjamin.peterson', 'njs', 'Greg Stark', 'iritkatriel']
    pr_nums = ['1769', '1778', '1779']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue29334'
    versions = ['Python 2.7']

    Activity

    1. GregStark commented on Jan 20, 2017

      GregStarkmannequin
      MannequinAuthor

      In my experiments with the relatively new class SSLObject from the ssl module I've noticed the following behavior(s) which I think can be described as bugs.

      The getpeercert() method raises a ValueError exception "handshake not done" even after the handshake has successfully completed. If, however, I call the do_handshake() method *after* the handshake completes, then getpeercert() correctly runs and returns the peer's certificate. So now let's focus on do_handshake(). This method is basically undocumented, which I thought was ok because what it does should be obvious. It does seem to initiate a handshake if it's the first method call after the SSLObject is created. If called afterward, it doesn't outwardly appear to do anything, but as mentioned previously it does magically make the getpeercert() method start working.

    2. tiran commented on Jan 20, 2017

      @tiran
      Member

      Hi Grek,

      can you provide a script to reproduce the problem, please?

    3. GregStark commented on Jan 20, 2017

      GregStarkmannequin
      MannequinAuthor

      Christian,

      I will gladly do so a little later today. Thanks for your quick response.

      --greg

      On Fri, Jan 20, 2017 at 7:29 AM, Christian Heimes <report@bugs.python.org>
      wrote:

      Christian Heimes added the comment:

      Hi Grek,

      can you provide a script to reproduce the problem, please?

      ----------


      Python tracker <report@bugs.python.org>
      <http://bugs.python.org/issue29334\>


    4. GregStark commented on Jan 20, 2017

      GregStarkmannequin
      MannequinAuthor

      adding script the illustrates the bug.

    5. njsmith commented on May 23, 2017

      @njsmith
      Contributor

      Oddly, I expected to run into this with my code using SSLObject in trio [1], but if I connect to python.org:443 and then 'await trio_ssl_stream.do_handshake(); trio_ssl_stream.getpeercert()' it works just fine ... even though when I run the sslbugs.py script I get the same weird results Greg reports. As far as I can tell the logic is identical. So I guess this might potentially be useful to narrow this down :-).

      Test code that works:

      @trio.run
      async def main():
      import trio
      sock = trio.socket.socket()
      addr = await sock.resolve_remote_address(("python.org", 443))
      await sock.connect(addr)
      s = trio.SocketStream(sock)
      client = trio.ssl.SSLStream(
      s, trio.ssl.create_default_context(), server_hostname="python.org")
      await client.do_handshake()
      print(client.getpeercert())

      [1] Currently in python-trio/trio#107, eventually will be at https://github.com/python-trio/trio/blob/master/trio/ssl.py

    6. tiran commented on May 23, 2017

      @tiran
      Member

      The issue with getpeercert() is a side-effect of your issue bpo-30141. The peer certificate is cached in do_handshake.

    7. tiran commented on May 23, 2017

      @tiran
      Member

      New changeset 66dc33b by Christian Heimes in branch 'master':
      bpo-29334: Fix ssl.getpeercert for auto-handshake (bpo-1769)
      66dc33b

    8. tiran commented on Jun 12, 2017

      @tiran
      Member

      Also needs backport to 2.7 for bpo-22559

    9. ned-deily commented on Jul 1, 2017

      @ned-deily
      Member

      Is anything holding this up for merging into 3.6 and/or 3.5?

    10. tiran commented on Sep 5, 2017

      @tiran
      Member

      New changeset 63b3f2b by Christian Heimes in branch '3.6':
      [3.6] bpo-29334: Fix ssl.getpeercert for auto-handshake (GH-1769) (bpo-1778)
      63b3f2b

    11. tiran commented on Feb 26, 2018

      @tiran
      Member

      The fix hasn't been ported to 2.7 yet.

    12. tiran commented on Feb 26, 2018

      @tiran
      Member

      The fix hasn't been ported to 2.7 yet.

    13. removed their assignment
      on Feb 26, 2018
    14. iritkatriel commented on Sep 19, 2020

      @iritkatriel
      Member

      Backport to 2.7 is no longer relevant, so I think this issue can be closed.

    15. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      topic-SSLtype-bugAn unexpected behavior, bug, or error

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions