o
    \q                     @   s   d Z ddlmZmZmZ ddlZddlmZmZm	Z	m
Z
mZmZ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 )zL
`cryptography.x509 <https://github.com/pyca/cryptography>`_-specific code.
    )absolute_importdivisionprint_functionN)DNSNameExtensionOID	IPAddressNameOIDObjectIdentifier	OtherNameUniformResourceIdentifier)ExtensionNotFound)decode)	IA5String   )DNS_IDCertificateError
DNSPatternIPAddress_IDIPAddressPattern
SRVPattern
URIPatternverify_service_identity)SubjectAltNameWarningverify_certificate_hostnamec                 C      t t| t|gg d dS )a  
    Verify whether *certificate* is valid for *hostname*.

    .. note:: Nothing is verified about the *authority* of the certificate;
       the caller must verify that the certificate chains to an appropriate
       trust root themselves.

    :param cryptography.x509.Certificate certificate: A cryptography X509
        certificate object.
    :param unicode hostname: The hostname that *certificate* should be valid
        for.

    :raises service_identity.VerificationError: If *certificate* is not valid
        for *hostname*.
    :raises service_identity.CertificateError: If *certificate* contains
        invalid/unexpected data.

    :returns: ``None``
    cert_patternsobligatory_idsoptional_idsN)r   extract_idsr   )certificatehostname r"   ?/usr/lib/python3/dist-packages/service_identity/cryptography.pyr   &   s
   
c                 C   r   )a  
    Verify whether *certificate* is valid for *ip_address*.

    .. note:: Nothing is verified about the *authority* of the certificate;
       the caller must verify that the certificate chains to an appropriate
       trust root themselves.

    :param cryptography.x509.Certificate certificate: A cryptography X509
        certificate object.
    :param unicode ip_address: The IP address that *connection* should be valid
        for.  Can be an IPv4 or IPv6 address.

    :raises service_identity.VerificationError: If *certificate* is not valid
        for *ip_address*.
    :raises service_identity.CertificateError: If *certificate* contains
        invalid/unexpected data.

    :returns: ``None``

    .. versionadded:: 18.1.0
    r   N)r   r   r   )r    
ip_addressr"   r"   r#   verify_certificate_ip_addressA   s
   
r%   z1.3.6.1.5.5.7.8.7c                 C   s  g }z	| j tj}W n	 ty   Y nSw |dd |jtD  |dd |jt	D  |dd |jt
D  |jtD ]!}|jtkrft|j\}}t|trb|t|  qEtdqE|sdd | jtjD }tt|d}dd |D }td	|t |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 cryptography.x509.Certificate cert: The certificate to be dissected.

    :return: List of IDs.
    c                 S      g | ]	}t |d qS zutf-8r   encode).0namer"   r"   r#   
<listcomp>u       zextract_ids.<locals>.<listcomp>c                 S   r&   r'   )r   r)   )r*   urir"   r"   r#   r,   {   r-   c                 S   s   g | ]}t |qS r"   )r   )r*   ipr"   r"   r#   r,      s    zUnexpected certificate content.c                 S   s   g | ]}|j qS r"   )valuer*   nr"   r"   r#   r,      s    s   <not given>c                 S   r&   r'   r(   r1   r"   r"   r#   r,      s    zCertificate with CN {!r} 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.)
extensionsget_extension_for_oidr   SUBJECT_ALTERNATIVE_NAMEr   extendr0   get_values_for_typer   r   r   r
   type_idID_ON_DNS_SRVr   
isinstancer   appendr   asOctetsr   subjectget_attributes_for_oidr   COMMON_NAMEnextiterwarningswarnformatr   )certidsextothersrv_cnscnr"   r"   r#   r   a   sX   



r   )$__doc__
__future__r   r   r   rB   cryptography.x509r   r   r   r   r	   r
   r   cryptography.x509.extensionsr   pyasn1.codec.der.decoderr   pyasn1.type.charr   _commonr   r   r   r   r   r   r   r   
exceptionsr   __all__r   r%   r9   r   r"   r"   r"   r#   <module>   s    $	(
