2023-05-04 19:38:53 +00:00
|
|
|
---
|
|
|
|
requests_toolbelt/adapters/x509.py | 27 ++++-----------------------
|
|
|
|
1 file changed, 4 insertions(+), 23 deletions(-)
|
|
|
|
|
|
|
|
--- a/requests_toolbelt/adapters/x509.py
|
|
|
|
+++ b/requests_toolbelt/adapters/x509.py
|
2022-09-30 04:31:29 +00:00
|
|
|
@@ -8,6 +8,7 @@ X.509 certificate without needing to con
|
|
|
|
"""
|
|
|
|
|
|
|
|
from OpenSSL.crypto import PKey, X509
|
|
|
|
+from OpenSSL.SSL import Context, TLS_CLIENT_METHOD
|
|
|
|
from cryptography import x509
|
|
|
|
from cryptography.hazmat.primitives.serialization import (load_pem_private_key,
|
|
|
|
load_der_private_key)
|
2023-05-04 19:38:53 +00:00
|
|
|
@@ -20,16 +21,6 @@ import requests
|
2022-09-30 04:31:29 +00:00
|
|
|
|
|
|
|
from .. import exceptions as exc
|
|
|
|
|
|
|
|
-"""
|
|
|
|
-importing the protocol constants from _ssl instead of ssl because only the
|
|
|
|
-constants are needed and to handle issues caused by importing from ssl on
|
|
|
|
-the 2.7.x line.
|
|
|
|
-"""
|
|
|
|
-try:
|
|
|
|
- from _ssl import PROTOCOL_TLS as PROTOCOL
|
|
|
|
-except ImportError:
|
|
|
|
- from _ssl import PROTOCOL_SSLv23 as PROTOCOL
|
|
|
|
-
|
|
|
|
|
2023-05-04 19:38:53 +00:00
|
|
|
PyOpenSSLContext = None
|
|
|
|
|
|
|
|
@@ -84,7 +75,6 @@ class X509Adapter(HTTPAdapter):
|
2022-09-30 04:31:29 +00:00
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
2023-05-04 19:38:53 +00:00
|
|
|
self._import_pyopensslcontext()
|
2022-09-30 04:31:29 +00:00
|
|
|
- self._check_version()
|
|
|
|
cert_bytes = kwargs.pop('cert_bytes', None)
|
|
|
|
pk_bytes = kwargs.pop('pk_bytes', None)
|
|
|
|
password = kwargs.pop('password', None)
|
2023-05-04 19:38:53 +00:00
|
|
|
@@ -136,15 +126,6 @@ class X509Adapter(HTTPAdapter):
|
|
|
|
except ImportError:
|
|
|
|
PyOpenSSLContext = None
|
2022-09-30 04:31:29 +00:00
|
|
|
|
|
|
|
- def _check_version(self):
|
|
|
|
- if PyOpenSSLContext is None:
|
|
|
|
- raise exc.VersionMismatchError(
|
|
|
|
- "The X509Adapter requires at least Requests 2.12.0 to be "
|
2023-05-04 19:38:53 +00:00
|
|
|
- "installed. Version {} was found instead.".format(
|
2022-09-30 04:31:29 +00:00
|
|
|
- requests.__version__
|
|
|
|
- )
|
|
|
|
- )
|
|
|
|
-
|
|
|
|
|
|
|
|
def check_cert_dates(cert):
|
|
|
|
"""Verify that the supplied client cert is not invalid."""
|
2023-05-04 19:38:53 +00:00
|
|
|
@@ -190,7 +171,7 @@ def create_ssl_context(cert_byes, pk_byt
|
2022-09-30 04:31:29 +00:00
|
|
|
raise ValueError('Cert and key could not be parsed from '
|
|
|
|
'provided data')
|
|
|
|
check_cert_dates(cert)
|
|
|
|
- ssl_context = PyOpenSSLContext(PROTOCOL)
|
|
|
|
- ssl_context._ctx.use_certificate(X509.from_cryptography(cert))
|
|
|
|
- ssl_context._ctx.use_privatekey(PKey.from_cryptography_key(key))
|
|
|
|
+ ssl_context = Context(TLS_CLIENT_METHOD)
|
|
|
|
+ ssl_context.use_certificate(X509.from_cryptography(cert))
|
|
|
|
+ ssl_context.use_privatekey(PKey.from_cryptography_key(key))
|
|
|
|
return ssl_context
|