o
    \"                     @   s   d Z ddlmZmZmZ ddlZddlZddlmZ ddl	m
Z
 ddlmZ ddlmZ dd	lmZmZmZmZmZmZmZmZ dd
lmZ dgZdd Zdd ZedZdd ZdS )zA
`pyOpenSSL <https://github.com/pyca/pyopenssl>`_-specific code.
    )absolute_importdivisionprint_functionN)decode)	IA5String)ObjectIdentifier)GeneralNames   )DNS_IDCertificateError
DNSPatternIPAddress_IDIPAddressPattern
SRVPattern
URIPatternverify_service_identity)SubjectAltNameWarningverify_hostnamec                 C       t t|  t|gg d dS )a?  
    Verify whether the certificate of *connection* is valid for *hostname*.

    :param OpenSSL.SSL.Connection connection: A pyOpenSSL connection object.
    :param unicode hostname: The hostname that *connection* should be connected
        to.

    :raises service_identity.VerificationError: If *connection* does not
        provide a certificate that is valid for *hostname*.
    :raises service_identity.CertificateError: If the certificate chain of
        *connection* contains a certificate that contains invalid/unexpected
        data.

    :returns: ``None``
    cert_patternsobligatory_idsoptional_idsN)r   extract_idsget_peer_certificater
   )
connectionhostname r   </usr/lib/python3/dist-packages/service_identity/pyopenssl.pyr       s
   

c                 C   r   )a  
    Verify whether the certificate of *connection* is valid for *ip_address*.

    :param OpenSSL.SSL.Connection connection: A pyOpenSSL connection object.
    :param unicode ip_address: The IP address that *connection* should be
        connected to.  Can be an IPv4 or IPv6 address.

    :raises service_identity.VerificationError: If *connection* does not
        provide a certificate that is valid for *ip_address*.
    :raises service_identity.CertificateError: If the certificate chain of
        *connection* contains a certificate that contains invalid/unexpected
        data.

    :returns: ``None``

    .. versionadded:: 18.1.0
    r   N)r   r   r   r   )r   
ip_addressr   r   r   verify_ip_address7   s
   

r    z1.3.6.1.5.5.7.8.7c                 C   sp  g }t j|  D ]}| |}| dkrt| t d\}}|D ]g}|	 }|dkr:|
t|   q$|dkrK|
t|   q$|dkr[|
t|   q$|dkr| }|d}	|	tkrt|d\}
}t|
tr|
t|
  q$td		 q$	 q$q
|sd
d |   D }tt|d}dd |D }tjd|df tdd |S )a  
    Extract all valid IDs from a certificate for service verification.

    If *cert* doesn't contain any identifiers, the ``CN``s are used as DNS-IDs
    as fallback.

    :param OpenSSL.SSL.X509 cert: The certificate to be dissected.

    :return: List of IDs.
    s   subjectAltName)asn1SpecdNSName	iPAddressuniformResourceIdentifier	otherNamer   r	   zUnexpected certificate content.c                 S   s    g | ]}|d  dkr|d qS )r   s   CNr	   r   .0cr   r   r   
<listcomp>   s
    zextract_ids.<locals>.<listcomp>s   <not given>c                 S   s   g | ]}t |qS r   )r   r&   r   r   r   r)      s    zCertificate with CN '%s' has no `subjectAltName`, falling back to check for a `commonName` for now.  This feature is being removed by major browsers and deprecated by RFC 2818.  service_identity will remove the support for it in mid-2018.zutf-8   )
stacklevel)sixmovesrangeget_extension_countget_extensionget_short_namer   get_datar   getNameappendr   getComponentasOctetsr   
from_bytesr   getComponentByPositionID_ON_DNS_SRV
isinstancer   r   r   get_subjectget_componentsnextiterwarningswarnr   )certidsiextnames_nname_stringcompoidsrv
componentscnr   r   r   r   S   sX   





	r   )__doc__
__future__r   r   r   r?   r,   pyasn1.codec.der.decoderr   pyasn1.type.charr   pyasn1.type.univr   pyasn1_modules.rfc2459r   _commonr
   r   r   r   r   r   r   r   
exceptionsr   __all__r   r    r9   r   r   r   r   r   <module>   s    (
