3
O6bB                 @   s   d dl Z d dlZyd dlmZ W n ek
r<   dd ZY nX ejd  dk rReZndd Zd dlm	Z	 dd	d
Z
dd Zdd ZdS )    N)
ip_addressc             C   s   d S )N )addressr   r   Y/var/www/html/sandeepIITI/myenv/lib/python3.6/site-packages/pymongo/ssl_match_hostname.py<lambda>   s    r      c             C   s   | S )Nr   )valuer   r   r   r      s    )CertificateError   c       
      C   s   g }| sdS | j d}|d }|dd }|jd}||krLtdt|  |s`| j |j kS |dkrt|jd n>|jd	s|jd	r|jtj| n|jtj|j	d
d x|D ]}|jtj| qW tj
ddj| d tj}	|	j|S )zhMatching according to RFC 6125, section 6.4.3

    http://tools.ietf.org/html/rfc6125#section-6.4.3
    F.r   r
   N*z,too many wildcards in certificate DNS name: z[^.]+zxn--z\*z[^.]*z\Az\.z\Z)splitcountr	   reprlowerappend
startswithreescapereplacecompilejoin
IGNORECASEmatch)
dnhostnamemax_wildcardspatspartsleftmost	remainder	wildcardsfragpatr   r   r   _dnsname_match   s*    


r$   c             C   s   t t| j }||kS )zExact matching of IP addresses.

    RFC 6125 explicitly doesn't define an algorithm for this
    (section 1.7.2 - "Out of Scope").
    )r   _unicoderstrip)ipnamehost_ipipr   r   r   _ipaddress_matchH   s    r*   c             C   sV  | st dytt|}W n t tfk
r8   d}Y nX g }| jdf }xb|D ]Z\}}|dkr|dkrvt||rvdS |j| qP|dkrP|dk	rt||rdS |j| qPW |sxF| jdf D ]6}x0|D ](\}}|dkrt||rdS |j| qW qW t|dkr&t	d	|d
j
tt|f n,t|dkrJt	d||d f nt	ddS )zVerify that *cert* (in decoded format as returned by
    SSLSocket.getpeercert()) matches the *hostname*.  RFC 2818 and RFC 6125
    rules are followed.

    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_REQUIREDNsubjectAltNameDNSz
IP Addresssubject
commonNamer
   z&hostname %r doesn't match either of %sz, zhostname %r doesn't match %rr   z=no appropriate commonName or subjectAltName fields were found)
ValueErrorr   r%   UnicodeErrorgetr$   r   r*   lenr	   r   mapr   )certr   r(   dnsnamessankeyr   subr   r   r   match_hostnameS   s>    

r9   )r
   )r   sys	ipaddressr   ImportErrorversion_infounicoder%   Zpymongo.errorsr	   r$   r*   r9   r   r   r   r   <module>   s   
3