3 �P\� � @ s� d Z ddlmZmZ ddlmZ ddlmZmZ ddlm Z ddl mZ ddlmZ ddl mZ dd lZd dlmZ d dlmZ ydd lmZ W n ek r� ddlmZ Y nX G dd� de�Zdd� Zd e jfdd�Zd S )z�A X509Adapter for use with the requests library. This file contains an implementation of the X509Adapter that will allow users to authenticate a request using an arbitrary X.509 certificate without needing to convert it to a .pem file � )�PKey�X509)�x509)�load_pem_private_key�load_der_private_key)�Encoding)�default_backend)�datetime)�HTTPAdapterN� )�PyOpenSSLContext)� exceptions)�PROTOCOL_TLS)�PROTOCOL_SSLv23c s@ e Zd ZdZ� fdd�Z� fdd�Z� fdd�Zdd � Z� ZS ) �X509Adaptera_ Adapter for use with X.509 certificates. Provides an interface for Requests sessions to contact HTTPS urls and authenticate with an X.509 cert by implementing the Transport Adapter interface. This class will need to be manually instantiated and mounted to the session :param pool_connections: The number of urllib3 connection pools to cache. :param pool_maxsize: The maximum number of connections to save in the pool. :param max_retries: The maximum number of retries each connection should attempt. Note, this applies only to failed DNS lookups, socket connections and connection timeouts, never to requests where data has made it to the server. By default, Requests does not retry failed connections. If you need granular control over the conditions under which we retry a request, import urllib3's ``Retry`` class and pass that instead. :param pool_block: Whether the connection pool should block for connections. :param bytes cert_bytes: bytes object containing contents of a cryptography.x509Certificate object using the encoding specified by the ``encoding`` parameter. :param bytes pk_bytes: bytes object containing contents of a object that implements ``cryptography.hazmat.primitives.serialization.PrivateFormat`` using the encoding specified by the ``encoding`` parameter. :param password: string or utf8 encoded bytes containing the passphrase used for the private key. None if unencrypted. Defaults to None. :param encoding: Enumeration detailing the encoding method used on the ``cert_bytes`` parameter. Can be either PEM or DER. Defaults to PEM. :type encoding: :class: `cryptography.hazmat.primitives.serialization.Encoding` Usage:: >>> import requests >>> from requests_toolbelt.adapters.x509 import X509Adapter >>> s = requests.Session() >>> a = X509Adapter(max_retries=3, cert_bytes=b'...', pk_bytes=b'...', encoding='...' >>> s.mount('https://', a) c s� | j � |jdd �}|jdd �}|jdd �}|jdtj�}d }|d ksRt|t� rZtd��|d ksnt|t� rvtd��t|t�r�|}n|r�|jd�}t||||�| _ t t| �j||� d S )N� cert_bytes�pk_bytes�password�encodingzXInvalid cert content provided. You must provide an X.509 cert formatted as a byte array.z_Invalid private key content provided. You must provide a private key formatted as a byte array.�utf8) �_check_version�popr �PEM� isinstance�bytes� ValueError�encode�create_ssl_context�ssl_context�superr �__init__)�self�args�kwargsr r r r Zpassword_bytes)� __class__� �/usr/lib/python3.6/x509.pyr S s"