Your IP : 3.17.174.204


Current Path : /opt/alt/python34/lib64/python3.4/__pycache__/
Upload File :
Current File : //opt/alt/python34/lib64/python3.4/__pycache__/ssl.cpython-34.pyc

�
h f���+@s�dZddlZddlZddlZddlZddlmZddlmZ	ddl
Z
ddl
mZmZm
Z
ddl
mZddl
mZmZmZmZmZmZddl
mZmZmZdd	l
mZmZdd
l
mZmZmZmZyddl
m Z Wne!k
r8YnXdd
�Z"e"d�e"d�e"d�e"d�e"d�ddl
m#Z#m$Z$m%Z%ddl
m&Z&e'e
d�e(�d<dd�e(�j)�D�Z*yddl
m+Z+e+Z,Wne!k
r	dZ,YnXde*e+<yddl
m-Z-m.Z.Wne!k
rBYnXde*e-<de*e.<ej/dkrddl
m0Z0m1Z1ndd l2m2Z2m3Z3m4Z4m5Z5dd!l2m6Z6m7Z7ddl8Z8ddl9Z9e:Z;e
j<r�d"gZ=ngZ=d#Z>d$Z?Gd%d&�d&e@�ZAd'd(d)�ZBd*d+�ZCed,d-�ZDd.d/�ZEGd0d1�d1ed1d2��ZFGd3d4�d4eFe	�ZGGd5d6�d6e�ZHeGjId7dd8dd9dd:d;�ZJeKd<dd=d>d?eGjId@ddAdd7dd8dd9ddBdC�ZLeJZMeLZNGdDdE�dEe2�ZOddd>eeKddFdFddGdH�	ZPdIdJ�ZQdKZRdLZSdMdN�ZTdOdP�ZUeKddQdR�ZVdSdT�ZWdS)Ua�
This module provides some more Pythonic support for SSL.

Object types:

  SSLSocket -- subtype of socket.socket which does SSL over the socket

Exceptions:

  SSLError -- exception raised for I/O errors

Functions:

  cert_time_to_seconds -- convert time string used for certificate
                          notBefore and notAfter functions to integer
                          seconds past the Epoch (the time values
                          returned from time.time())

  fetch_server_certificate (HOST, PORT) -- fetch the certificate provided
                          by the server running on HOST at port PORT.  No
                          validation of the certificate is performed.

Integer constants:

SSL_ERROR_ZERO_RETURN
SSL_ERROR_WANT_READ
SSL_ERROR_WANT_WRITE
SSL_ERROR_WANT_X509_LOOKUP
SSL_ERROR_SYSCALL
SSL_ERROR_SSL
SSL_ERROR_WANT_CONNECT

SSL_ERROR_EOF
SSL_ERROR_INVALID_ERROR_CODE

The following group define certificate requirements that one side is
allowing/requiring from the other side:

CERT_NONE - no certificates from the other side are required (or will
            be looked at if provided)
CERT_OPTIONAL - certificates are not required, but if provided will be
                validated, and if validation fails, the connection will
                also fail
CERT_REQUIRED - certificates are required, and will be validated, and
                if validation fails, the connection will also fail

The following constants identify various SSL protocol variants:

PROTOCOL_SSLv2
PROTOCOL_SSLv3
PROTOCOL_SSLv23
PROTOCOL_TLS
PROTOCOL_TLSv1
PROTOCOL_TLSv1_1
PROTOCOL_TLSv1_2

The following constants identify various SSL alert message descriptions as per
http://www.iana.org/assignments/tls-parameters/tls-parameters.xml#tls-parameters-6

ALERT_DESCRIPTION_CLOSE_NOTIFY
ALERT_DESCRIPTION_UNEXPECTED_MESSAGE
ALERT_DESCRIPTION_BAD_RECORD_MAC
ALERT_DESCRIPTION_RECORD_OVERFLOW
ALERT_DESCRIPTION_DECOMPRESSION_FAILURE
ALERT_DESCRIPTION_HANDSHAKE_FAILURE
ALERT_DESCRIPTION_BAD_CERTIFICATE
ALERT_DESCRIPTION_UNSUPPORTED_CERTIFICATE
ALERT_DESCRIPTION_CERTIFICATE_REVOKED
ALERT_DESCRIPTION_CERTIFICATE_EXPIRED
ALERT_DESCRIPTION_CERTIFICATE_UNKNOWN
ALERT_DESCRIPTION_ILLEGAL_PARAMETER
ALERT_DESCRIPTION_UNKNOWN_CA
ALERT_DESCRIPTION_ACCESS_DENIED
ALERT_DESCRIPTION_DECODE_ERROR
ALERT_DESCRIPTION_DECRYPT_ERROR
ALERT_DESCRIPTION_PROTOCOL_VERSION
ALERT_DESCRIPTION_INSUFFICIENT_SECURITY
ALERT_DESCRIPTION_INTERNAL_ERROR
ALERT_DESCRIPTION_USER_CANCELLED
ALERT_DESCRIPTION_NO_RENEGOTIATION
ALERT_DESCRIPTION_UNSUPPORTED_EXTENSION
ALERT_DESCRIPTION_CERTIFICATE_UNOBTAINABLE
ALERT_DESCRIPTION_UNRECOGNIZED_NAME
ALERT_DESCRIPTION_BAD_CERTIFICATE_STATUS_RESPONSE
ALERT_DESCRIPTION_BAD_CERTIFICATE_HASH_VALUE
ALERT_DESCRIPTION_UNKNOWN_PSK_IDENTITY
�N)�
namedtuple)�Enum)�OPENSSL_VERSION_NUMBER�OPENSSL_VERSION_INFO�OPENSSL_VERSION)�_SSLContext)�SSLError�SSLZeroReturnError�SSLWantReadError�SSLWantWriteError�SSLSyscallError�SSLEOFError)�	CERT_NONE�
CERT_OPTIONAL�
CERT_REQUIRED)�txt2obj�nid2obj)�RAND_status�RAND_add�
RAND_bytes�RAND_pseudo_bytes)�RAND_egdcCsCx<tt�D].}|j|�r
tt|�t�|<q
q
WdS)N)�dir�_ssl�
startswith�getattr�globals)�prefix�n�r�(/opt/alt/python34/lib64/python3.4/ssl.py�_import_symbolsssr!ZOP_ZALERT_DESCRIPTION_Z
SSL_ERROR_�	PROTOCOL_ZVERIFY_)�HAS_SNI�HAS_ECDH�HAS_NPN)�_OPENSSL_API_VERSION�PROTOCOL_TLSZPROTOCOL_SSLv23cCs.i|]$\}}|jd�r||�qS)r")r)�.0�name�valuerrr �
<dictcomp>�s	r+)�PROTOCOL_SSLv2ZSSLv2)�PROTOCOL_TLSv1_1�PROTOCOL_TLSv1_2zTLSv1.1zTLSv1.2�win32)�enum_certificates�	enum_crls)�socket�AF_INET�SOCK_STREAM�create_connection)�
SOL_SOCKET�SO_TYPEz
tls-uniquez�ECDH+AESGCM:ECDH+CHACHA20:DH+AESGCM:DH+CHACHA20:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+HIGH:DH+HIGH:RSA+AESGCM:RSA+AES:RSA+HIGH:!aNULL:!eNULL:!MD5:!3DESz�ECDH+AESGCM:ECDH+CHACHA20:DH+AESGCM:DH+CHACHA20:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+HIGH:DH+HIGH:RSA+AESGCM:RSA+AES:RSA+HIGH:!aNULL:!eNULL:!MD5:!DSS:!RC4:!3DESc@seZdZdS)�CertificateErrorN)�__name__�
__module__�__qualname__rrrr r8�sr8�c	CsDg}|sdS|jd�^}}|jd�}||krYtdt|���n|su|j�|j�kS|dkr�|jd�nY|jd�s�|jd�r�|jtj|��n"|jtj|�j	dd��x$|D]}|jtj|��q�Wtj
d	d
j|�dtj�}|j
|�S)zhMatching according to RFC 6125, section 6.4.3

    http://tools.ietf.org/html/rfc6125#section-6.4.3
    F�.�*z,too many wildcards in certificate DNS name: z[^.]+zxn--z\*z[^.]*z\Az\.z\Z)�split�countr8�repr�lower�appendr�re�escape�replace�compile�join�
IGNORECASE�match)	Zdn�hostnameZ
max_wildcardsZpatsZleftmostZ	remainderZ	wildcardsZfragZpatrrr �_dnsname_match�s&"
&rLcCs[|std��ng}|jdf�}xC|D];\}}|dkr4t||�r_dS|j|�q4q4W|s�xc|jdf�D]L}xC|D];\}}|dkr�t||�r�dS|j|�q�q�Wq�Wnt|�dkrtd|d	jtt|��f��n;t|�dkrKtd
||df��ntd��dS)
a)Verify that *cert* (in decoded format as returned by
    SSLSocket.getpeercert()) matches the *hostname*.  RFC 2818 and RFC 6125
    rules are followed, but IP addresses are not accepted for *hostname*.

    CertificateError is raised on failure. On success, the function
    returns nothing.
    ztempty or no certificate, match_hostname needs a SSL socket or SSL context with either CERT_OPTIONAL or CERT_REQUIREDZsubjectAltNameZDNSNZsubjectZ
commonNamer<z&hostname %r doesn't match either of %sz, zhostname %r doesn't match %rrz=no appropriate commonName or subjectAltName fields were found)	�
ValueError�getrLrC�lenr8rH�maprA)�certrKZdnsnamesZsan�keyr*�subrrr �match_hostnames.%rT�DefaultVerifyPathszQcafile capath openssl_cafile_env openssl_cafile openssl_capath_env openssl_capathcCs�tj�}tjj|d|d�}tjj|d|d�}ttjj|�ra|ndtjj|�r||nd|�S)z/Return paths to default cafile and capath.
    rr<��N)	r�get_default_verify_paths�os�environrNrU�path�isfile�isdir)�parts�cafile�capathrrr rX0srXcs^eZdZdZfZ�fdd�Ze�fdd��Ze�fdd��Z�S)�_ASN1Objectz#ASN.1 object identifier lookup
    cst�j|t|dd��S)Nr)F)�super�__new__�_txt2obj)�cls�oid)�	__class__rr rcCsz_ASN1Object.__new__cst�j|t|��S)z3Create _ASN1Object from OpenSSL numeric ID
        )rbrc�_nid2obj)reZnid)rgrr �fromnidFsz_ASN1Object.fromnidcst�j|t|dd��S)z=Create _ASN1Object from short name, long name or OID
        r)T)rbrcrd)rer))rgrr �fromnameLsz_ASN1Object.fromname)	r9r:r;�__doc__�	__slots__rc�classmethodrirjrr)rgr ra>s
raznid shortname longname oidc@s"eZdZdZdZdZdS)�PurposezDSSLContext purpose flags with X509v3 Extended Key Usage objects
    z1.3.6.1.5.5.7.3.1z1.3.6.1.5.5.7.3.2N)r9r:r;rk�SERVER_AUTH�CLIENT_AUTHrrrr rnSsrnc@s�eZdZdZdZdZedd�Zedd	�Zd
dddd
d�Z	dd�Z
dd�Zej
dd�ZdS)�
SSLContextz|An SSLContext holds various SSL-related configuration options and
    data, such as certificates and possibly a private key.�protocol�__weakref__�CA�ROOTcOs2tj||�}|tkr.|jt�n|S)N)rrc�_SSLv2_IF_EXISTS�set_ciphers�_DEFAULT_CIPHERS)rerr�args�kwargs�selfrrr rcaszSSLContext.__new__cCs
||_dS)N)rr)r{rrrrr �__init__gszSSLContext.__init__FTNc
Cs+td|d|d|d|d|d|�S)N�sock�server_side�do_handshake_on_connect�suppress_ragged_eofs�server_hostname�_context)�	SSLSocket)r{r}r~rr�r�rrr �wrap_socketjs
zSSLContext.wrap_socketcCs�t�}xp|D]h}t|d�}t|�dksIt|�dkrXtd��n|jt|��|j|�qW|j|�dS)N�asciir�z(NPN protocols must be 1 to 255 in length)�	bytearray�bytesrOrrC�extendZ_set_npn_protocols)r{�
npn_protocolsZprotosrr�brrr �set_npn_protocolsts	
$zSSLContext.set_npn_protocolscCswt�}xWt|�D]I\}}}|dkr|dksL|j|kr_|j|�q_qqW|jd|�|S)NZx509_asnT�cadata)r�r0rfr��load_verify_locations)r{�	storename�purposeZcertsrQ�encodingZtrustrrr �_load_windows_store_certss	z$SSLContext._load_windows_store_certscCsbt|t�st|��ntjdkrTx$|jD]}|j||�q7Wn|j�dS)Nr/)�
isinstancera�	TypeError�sys�platform�_windows_cert_storesr�Zset_default_verify_paths)r{r�r�rrr �load_default_certs�szSSLContext.load_default_certs)zprotocolz__weakref__)rtru)r9r:r;rkrlr�r'rcr|r�r�r�rnror�rrrr rqZs
rqr_r`r�cCsBt|t�st|��ntt�}|jtO_|jtO_|jtt	dd�O_|t
jkr�t|_
d|_np|t
jkr�|jtt	dd�O_|jtt	dd�O_|jtt	dd�O_|jt�n|s	|s	|r|j|||�n|j
tkr>|j|�n|S)z�Create a SSLContext object with default settings.

    NOTE: The protocol and settings may change anytime without prior
          deprecation. The values represent a fair balance between maximum
          compatibility and security.
    ZOP_NO_COMPRESSIONrTZOP_CIPHER_SERVER_PREFERENCEZOP_SINGLE_DH_USEZOP_SINGLE_ECDH_USE)r�rar�rqr'�options�OP_NO_SSLv2�OP_NO_SSLv3rrrnror�verify_mode�check_hostnamerprw�_RESTRICTED_SERVER_CIPHERSr�rr�)r�r_r`r��contextrrr �create_default_context�s&	r��	cert_reqsr�Fr��certfile�keyfilec
Cs�t|t�st|��nt|�}	|	jtO_|	jtO_|dk	r`||	_n||	_|r�|r�t	d��n|s�|r�|	j
||�n|s�|s�|r�|	j|||�n|	jtkr�|	j
|�n|	S)a/Create a SSLContext object for Python stdlib modules

    All Python stdlib modules shall use this function to create SSLContext
    objects in order to keep common settings in one place. The configuration
    is less restrict than create_default_context()'s to increase backward
    compatibility.
    Nzcertfile must be specified)r�rar�rqr�r�r�r�r�rM�load_cert_chainr�rr�)
rrr�r�r�r�r�r_r`r�r�rrr �_create_unverified_context�s"	
r�c@seZdZdZddddeeddeeddddddddd�Ze	dd	��Z
e
jd
d	��Z
dd�Zdd
d�Z
dd�Zdddd�Zdd�Zddd�Zdd�Zdd�Zdd�Zddd�Zddd �Zd!d"�Zdd#d$�Zd%dd&d'�Zddd(d)�Zd%dd*d+�Zddd,d-�Zd.d/�Zd0d1�Zd2d3�Zd4d5�Z d6d7�Z!d8d9�Z"dd:d;�Z#d<d=�Z$d>d?�Z%d@dA�Z&dBdC�Z'dDdEdF�Z(dS)Gr�z�This class implements a subtype of socket.socket that wraps
    the underlying OS socket in an SSL context when necessary, and
    provides read and write methods over that channel.NFTrcCsD|r||_n|r.|r.td��n|rJ|rJtd��n|r`|r`|}nt|�|_||j_|r�|jj|�n|r�|jj||�n|r�|jj|�n|r�|jj|�n||_||_	||_
||_||_||_
|jtt�tkr?td��n|rZ|rZtd��n|jjr||r|td��n||_||_||_|
|_|dk	rtj|d|jd|jd|jd	|j��|j|j��|j �nA|dk	r"tj|d	|�ntj|d|	d|
d|�y|j!�WnCt"k
r�}z#|j#t#j$kr|�nd
}WYdd}~XnXd}d
|_%d|_&||_'|r@yY|jj(|||�|_&|r|j�}|dkrtd
��n|j)�nWq@t"tfk
r<|j*��Yq@XndS)Nz5certfile must be specified for server-side operationszcertfile must be specifiedz!only stream sockets are supportedz4server_hostname can only be specified in client modez'check_hostname requires server_hostname�family�type�proto�filenoFTgzHdo_handshake_on_connect should not be specified for non-blocking sockets)+r�rMrqr�r�r�r�rwr�r�r��ssl_version�ca_certs�ciphersZ
getsockoptr6r7r4�NotImplementedErrorr�r~r�rr�r2r|r�r�r�r��
settimeout�
gettimeout�detach�getpeername�OSError�errnoZENOTCONNZ_closed�_sslobj�
_connected�_wrap_socket�do_handshake�close)r{r}r�r�r~r�r�r�rr�r�r�r�r�r�r�r�r��eZ	connected�timeoutrrr r|�s�	


														

			
zSSLSocket.__init__cCs|jS)N)r�)r{rrr r�RszSSLSocket.contextcCs||_||j_dS)N)r�r�r�)r{Zctxrrr r�Vs	cCstd|jj��dS)NzCan't dup() %s instances)�NotImplementedrgr9)r{rrr �dup[sz
SSLSocket.dupcCsdS)Nr)r{�msgrrr �_checkClosed_szSSLSocket._checkClosedcCs|js|j�ndS)N)r�r�)r{rrr �_check_connectedcs	zSSLSocket._check_connectedcCs�|j�|js"td��nyD|dk	rI|jj||�}n|jj|p[d�}|SWn[tk
r�}z;|jdtkr�|jr�|dk	r�dSdSn�WYdd}~XnXdS)zORead up to LEN bytes and return them.
        Return zero-length string on EOF.z'Read on closed or unwrapped SSL socket.Nir�)r�r�rM�readrryZ
SSL_ERROR_EOFr�)r{rO�buffer�v�xrrr r�ks
	zSSLSocket.readcCs2|j�|js"td��n|jj|�S)zhWrite DATA to the underlying SSL channel.  Returns
        number of bytes of DATA actually transmitted.z(Write on closed or unwrapped SSL socket.)r�r�rM�write)r{�datarrr r��s
	zSSLSocket.writecCs$|j�|j�|jj|�S)z�Returns a formatted version of the data in the
        certificate provided by the other end of the SSL channel.
        Return None if no certificate was provided, {} if a
        certificate was provided, but not validated.)r�r�r�Zpeer_certificate)r{Zbinary_formrrr �getpeercert�s

zSSLSocket.getpeercertcCs3|j�|jstjr"dS|jj�SdS)N)r�r�rr%�selected_npn_protocol)r{rrr r��s
zSSLSocket.selected_npn_protocolcCs(|j�|jsdS|jj�SdS)N)r�r��cipher)r{rrr r��s
	zSSLSocket.ciphercCs(|j�|jsdS|jj�SdS)N)r�r��compression)r{rrr r��s
	zSSLSocket.compressioncCs�|j�|jr�|dkr5td|j��ny|jj|�}WnVtk
r�}z6|jdtkrwdS|jdtkr�dS�WYdd}~Xq�X|Snt	j
|||�SdS)Nrz3non-zero flags not allowed in calls to send() on %s)r�r�rMrgr�rryZSSL_ERROR_WANT_READZSSL_ERROR_WANT_WRITEr2�send)r{r��flagsr�r�rrr r��s 
	zSSLSocket.sendcCsb|j�|jr)td|j��n5|dkrHtj|||�Stj||||�SdS)Nz%sendto not allowed on instances of %s)r�r�rMrgr2�sendto)r{r�Z
flags_or_addr�addrrrr r��s
	zSSLSocket.sendtocOstd|j��dS)Nz&sendmsg not allowed on instances of %s)r�rg)r{ryrzrrr �sendmsg�szSSLSocket.sendmsgcCs�|j�|jr�|dkr5td|j��nt|�}d}x3||kr||j||d��}||7}qJW|Stj|||�SdS)Nrz6non-zero flags not allowed in calls to sendall() on %s)r�r�rMrgrOr�r2�sendall)r{r�r�Zamountr@r�rrr r��s
	zSSLSocket.sendallicCsY|j�|jrB|dkr5td|j��n|j|�Stj|||�SdS)Nrz3non-zero flags not allowed in calls to recv() on %s)r�r�rMrgr�r2�recv)r{�buflenr�rrr r��s
	
zSSLSocket.recvcCs�|j�|r+|dkr+t|�}n|dkr@d}n|jr{|dkrktd|j��n|j||�Stj||||�SdS)Nirz8non-zero flags not allowed in calls to recv_into() on %s)r�rOr�rMrgr�r2�	recv_into)r{r��nbytesr�rrr r��s
		zSSLSocket.recv_intocCs@|j�|jr)td|j��ntj|||�SdS)Nz'recvfrom not allowed on instances of %s)r�r�rMrgr2�recvfrom)r{r�r�rrr r��s

	zSSLSocket.recvfromcCsC|j�|jr)td|j��ntj||||�SdS)Nz,recvfrom_into not allowed on instances of %s)r�r�rMrgr2�
recvfrom_into)r{r�r�r�rrr r�s

	zSSLSocket.recvfrom_intocOstd|j��dS)Nz&recvmsg not allowed on instances of %s)r�rg)r{ryrzrrr �recvmsgszSSLSocket.recvmsgcOstd|j��dS)Nz+recvmsg_into not allowed on instances of %s)r�rg)r{ryrzrrr �recvmsg_intoszSSLSocket.recvmsg_intocCs(|j�|jr |jj�SdSdS)Nr)r�r��pending)r{rrr r�s
	
zSSLSocket.pendingcCs'|j�d|_tj||�dS)N)r�r�r2�shutdown)r{Zhowrrr r�s
	zSSLSocket.shutdowncCs?|jr%|jj�}d|_|Stdt|���dS)NzNo SSL wrapper around )r�r�rM�str)r{�srrr �unwraps
		zSSLSocket.unwrapcCsd|_tj|�dS)N)r�r2�_real_close)r{rrr r�$s	zSSLSocket._real_closecCs�|j�|j�}z3|dkr;|r;|jd�n|jj�Wd|j|�X|jjr�|js~td��nt	|j
�|j�ndS)zPerform a TLS/SSL handshake.gNz-check_hostname needs server_hostname argument)r�r�r�r�r�r�r�r�rMrTr�)r{�blockr�rrr r�(s
	zSSLSocket.do_handshakecCs�|jrtd��n|jr0td��n|jj|d|j�|_ya|rltj||�}nd}tj	||�|s�d|_|j
r�|j�q�n|SWn$ttfk
r�d|_�YnXdS)Nz!can't connect in server-side modez/attempt to connect already-connected SSLSocket!FT)
r~rMr�r�r�r�r�r2�
connect_ex�connectrr�r�)r{r�r��rcrrr �
_real_connect9s$					zSSLSocket._real_connectcCs|j|d�dS)zQConnects to remote ADDR, and then wraps the connection in
        an SSL channel.FN)r�)r{r�rrr r�PszSSLSocket.connectcCs|j|d�S)zQConnects to remote ADDR, and then wraps the connection in
        an SSL channel.T)r�)r{r�rrr r�UszSSLSocket.connect_excCsItj|�\}}|jj|d|jd|jdd�}||fS)z�Accepts a new connection from a remote client, and returns
        a tuple containing that new connection wrapped with a server-side
        SSL channel, and the address of the remote client.rr�r~T)r2�acceptr�r�rr�)r{Znewsockr�rrr r�Zs			zSSLSocket.acceptz
tls-uniquecCs_|tkrtd��n|dkr?tdj|���n|jdkrRdS|jj�S)z�Get channel binding data for current connection.  Raise ValueError
        if the requested `cb_type` is not supported.  Return bytes of the data
        or None if the data is not available (e.g. before the handshake).
        z Unsupported channel binding typez
tls-uniquez({0} channel binding type not implementedN)�CHANNEL_BINDING_TYPESrMr��formatr�Z
tls_unique_cb)r{Zcb_typerrr �get_channel_bindingfszSSLSocket.get_channel_binding))r9r:r;rkrr'r3r4r|�propertyr��setterr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rrrr r��sP		S	

r�Tc

CsCtd|d|d|d|d|d|d|d|d	|d
|	�
S)Nr}r�r�r~r�r�r�rr�r�)r�)
r}r�r�r~r�r�r�rr�r�rrr r�vsr�cCs%ddl}|j|j|d��S)z�Takes a date-time string in standard ASN1_print form
    ("MON DAY 24HOUR:MINUTE:SEC YEAR TIMEZONE") and return
    a Python time value in seconds past the epoch.rNz%b %d %H:%M:%S %Y GMT)�timeZmktimeZstrptime)Z	cert_timer�rrr �cert_time_to_seconds�sr�z-----BEGIN CERTIFICATE-----z-----END CERTIFICATE-----cCs?ttj|�dd�}tdtj|d�dtdS)z[Takes a certificate in binary DER format and returns the
    PEM version of it as a string.�ASCII�strict�
�@)r��base64Zstandard_b64encode�
PEM_HEADER�textwrapZfill�
PEM_FOOTER)Zder_cert_bytes�frrr �DER_cert_to_PEM_cert�sr�cCs�|jt�s"tdt��n|j�jt�sJtdt��n|j�tt�tt��}tj|j	dd��S)zhTakes a certificate in ASCII PEM format and returns the
    DER-encoded version of it as a byte sequencez(Invalid PEM encoding; must start with %sz&Invalid PEM encoding; must end with %sr�r�)
rr�rM�strip�endswithr�rOr�Zdecodebytes�encode)Zpem_cert_string�drrr �PEM_cert_to_DER_cert�s

#r�c
Cs�|\}}|dk	r!t}nt}t|d|d|�}t|��.}|j|��}|jd�}	WdQXWdQXt|	�S)z�Retrieve the certificate from the server at the specified address,
    and return it as a PEM-encoded string.
    If 'ca_certs' is specified, validate the server cert against it.
    If 'ssl_version' is specified, use it in the connection attempt.Nr�r_T)rr�_create_stdlib_contextr5r�r�r�)
r�r�r��hostZportr�r�r}ZsslsockZdercertrrr �get_server_certificate�s			r�cCstj|d�S)Nz	<unknown>)�_PROTOCOL_NAMESrN)Z
protocol_coderrr �get_protocol_name�sr�)Xrkr�rDr�rY�collectionsrZenumrZ_Enumrrrrrrr	r
rrr
rrrrrdrrhrrrrr�ImportErrorr!r#r$r%r&rr�itemsr�r,rvr-r.r�r0r1r2r3r4r5r6r7r�r�r�Zsocket_errorZHAS_TLS_UNIQUEr�rxr�rMr8rLrTrUrXrarnrqror�r'r�Z_create_default_https_contextr�r�r�r�r�r�r�r�r�r�rrrr �<module>Ys�."











"	1+	8/'��	


?>