Skip to content

use correct ssl version with urllib3 and pyopenssl #816

@stefanfoulis

Description

@stefanfoulis

There are situations where get_max_tls_protocol() will get a max openssl version from python ssl that is higher than what urllib3 supports.

The error will be:

KeyError: 5

in requests/packages/urllib3/contrib/pyopenssl.py:271 on the line with ctx = OpenSSL.SSL.Context(_openssl_versions[ssl_version])

Using the following replacement for docker.ssladapter.ssladapter.get_max_tls_protocol() worked for me, but there might be other cases I am missing:

from requests.packages.urllib3.contrib import pyopenssl
def get_max_tls_protocol():
    protocols = ('PROTOCOL_TLSv1_2',
                 'PROTOCOL_TLSv1_1',
                 'PROTOCOL_TLSv1',
    for proto in protocols:
        if hasattr(ssl, proto):
            proto_id = getattr(ssl, proto)
            if proto_id in pyopenssl._openssl_versions:
                return proto_id

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions