
    f                    0   d Z ddl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 d	d
lmZ d	dlmZmZmZmZmZmZmZmZ  ej4                  e      5  ddlmZ ddlmZ ddd       dgZ ddZ!ddZ" ed      Z#ddZ$ddZ%y# 1 sw Y   %xY w)zA
`pyOpenSSL <https://github.com/pyca/pyopenssl>`_-specific code.
    )annotationsN)Sequence)decode)	IA5String)ObjectIdentifier)GeneralNames   )CertificateError)DNS_IDCertificatePattern
DNSPatternIPAddress_IDIPAddressPattern
SRVPattern
URIPatternverify_service_identity)X509)
Connectionverify_hostnamec                b    t        t        | j                               t        |      gg        y)a;  
    Verify whether the certificate of *connection* is valid for *hostname*.

    Args:
        connection: A pyOpenSSL connection object.

        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*.

        service_identity.CertificateError:
            If certificate provided by *connection* contains invalid /
            unexpected data. This includes the case where the certificate
            contains no ``subjectAltName``\ s.

    .. versionchanged:: 24.1.0
        :exc:`~service_identity.CertificateError` is raised if the certificate
        contains no ``subjectAltName``\ s instead of
        :exc:`~service_identity.VerificationError`.
    cert_patternsobligatory_idsoptional_idsN)r   extract_patternsget_peer_certificater   )
connectionhostnames     </usr/lib/python3/dist-packages/service_identity/pyopenssl.pyr   r   '   s0    0 &++-
 x()    c                b    t        t        | j                               t        |      gg        y)aX  
    Verify whether the certificate of *connection* is valid for *ip_address*.

    Args:
        connection: A pyOpenSSL connection object.

        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*.

        service_identity.CertificateError:
            If the certificate chain of *connection* contains a certificate
            that contains invalid/unexpected data.

    .. versionadded:: 18.1.0

    .. versionchanged:: 24.1.0
        :exc:`~service_identity.CertificateError` is raised if the certificate
        contains no ``subjectAltName``\ s instead of
        :exc:`~service_identity.VerificationError`.
    r   N)r   r   r   r   )r   
ip_addresss     r   verify_ip_addressr#   H   s0    6 &++-
 %Z01r    z1.3.6.1.5.5.7.8.7c                   g }t        | j                               D ]  }| j                  |      }|j                         dk(  s)t	        |j                         t                     \  }}|D ]  }|j                         }|dk(  rA|j                  t        j                  |j                         j                                      Z|dk(  rA|j                  t        j                  |j                         j                                      |dk(  rA|j                  t        j                  |j                         j                                      |dk(  r|j                         }|j                  d      }	|	t         k(  rlt	        |j                  d            \  }
}t#        |
t$              r4|j                  t'        j                  |
j                                      vt)        d	        |S )
a
  
    Extract all valid ID patterns from a certificate for service verification.

    Args:
        cert: The certificate to be dissected.

    Returns:
        List of IDs.

    .. versionchanged:: 23.1.0
       ``commonName`` is not used as a fallback anymore.
    s   subjectAltName)asn1SpecdNSName	iPAddressuniformResourceIdentifier	otherNamer   r	   zUnexpected certificate content.)rangeget_extension_countget_extensionget_short_namer   get_datar   getNameappendr   
from_bytesgetComponentasOctetsr   r   getComponentByPositionID_ON_DNS_SRV
isinstancer   r   r
   )certidsiextnames_nname_stringcompoidsrvs              r   r   r   o   s    %'C4++-. "  ##44clln|~FHE1 iik)+JJ"--ann.>.G.G.IJ !K/JJ(33NN,557
 !$??JJ"--ann.>.G.G.IJ !K/>>+D55a8Cm+!'(C(CA(F!GQ%c95JJz'<'<S\\^'LM"2 A#  =	"H Jr    c                P    t        j                  t        dd       t        |       S )zm
    Deprecated and never public API.  Use :func:`extract_patterns` instead.

    .. deprecated:: 23.1.0
    z?`extract_ids()` is deprecated, please use `extract_patterns()`.   )categorymessage
stacklevel)warningswarnDeprecationWarningr   )r7   s    r   extract_idsrJ      s&     MM#Q
 D!!r    )r   r   r   strreturnNone)r   r   r"   rK   rL   rM   )r7   r   rL   zSequence[CertificatePattern])&__doc__
__future__r   
contextlibrG   typingr   pyasn1.codec.der.decoderr   pyasn1.type.charr   pyasn1.type.univr   pyasn1_modules.rfc2459r   
exceptionsr
   hazmatr   r   r   r   r   r   r   r   suppressImportErrorOpenSSL.cryptor   OpenSSL.SSLr   __all__r   r#   r5   r   rJ    r    r   <module>r^      s    #    + & - / (	 	 	 Z% '#&' 
B!H !!452j"M' 's   BB