U
    c6{i�  ã                   @  sö   d Z ddlmZ ddlZddlm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 d	dlmZmZmZmZmZmZmZmZ dgZddddœdd„Z ddddœdd„Z!edƒZ"dddœdd„Z#dddœdd„Z$dS )zL
`cryptography.x509 <https://github.com/pyca/cryptography>`_-specific code.
é    )ÚannotationsN)ÚSequence)ÚCertificateÚDNSNameÚExtensionOIDÚ	IPAddressÚObjectIdentifierÚ	OtherNameÚUniformResourceIdentifier)ÚExtensionNotFound)Údecode)Ú	IA5Stringé   )ÚCertificateError)ÚDNS_IDÚCertificatePatternÚ
DNSPatternÚIPAddress_IDÚIPAddressPatternÚ
SRVPatternÚ
URIPatternÚverify_service_identityÚverify_certificate_hostnamer   ÚstrÚNone)ÚcertificateÚhostnameÚreturnc                 C  s   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.

    Args:
        certificate: A *cryptography* X509 certificate object.

        hostname: The hostname that *certificate* should be valid for.

    Raises:
        service_identity.VerificationError:
            If *certificate* is not valid for *hostname*.

        service_identity.CertificateError:
            If *certificate* 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`.
    ©Zcert_patternsZobligatory_idsZoptional_idsN)r   Úextract_patternsr   )r   r   © r    úA/tmp/pip-unpacked-wheel-knj3pizf/service_identity/cryptography.pyr   (   s
    ý)r   Ú
ip_addressr   c                 C  s   t t| ƒt|ƒgg d� dS )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.

    Args:
        certificate: A *cryptography* X509 certificate object.

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

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

    .. 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"   r    r    r!   Úverify_certificate_ip_addressL   s
    !ýr#   z1.3.6.1.5.5.7.8.7zSequence[CertificatePattern])Úcertr   c                 C  sÚ   g }z| j  tj¡}W n tk
r*   Y n¬X | dd„ |j t¡D ƒ¡ | dd„ |j t	¡D ƒ¡ | dd„ |j t
¡D ƒ¡ |j t¡D ]H}|jtkrŒt|jƒ\}}t|tƒrÈ| t | ¡ ¡¡ qŒd}t|ƒ‚qŒ|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.
    c                 S  s   g | ]}t  | d ¡¡‘qS ©zutf-8)r   Ú
from_bytesÚencode)Ú.0Únamer    r    r!   Ú
<listcomp>�   s   ÿz$extract_patterns.<locals>.<listcomp>c                 S  s   g | ]}t  | d ¡¡‘qS r%   )r   r&   r'   )r(   Úurir    r    r!   r*   “   s   ÿc                 S  s   g | ]}t |ƒ‘qS r    )r   )r(   Úipr    r    r!   r*   ›   s   ÿzUnexpected certificate content.)Ú
extensionsZget_extension_for_oidr   ZSUBJECT_ALTERNATIVE_NAMEr   ÚextendÚvalueZget_values_for_typer   r
   r   r	   Ztype_idÚID_ON_DNS_SRVr   Ú
isinstancer   Úappendr   r&   ZasOctetsr   )r$   ÚidsÚextÚotherZsrvÚ_Úmsgr    r    r!   r   w   s@    ÿ
þÿÿþÿ
þÿ


r   c                 C  s   t jtd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   )r$   r    r    r!   Úextract_ids¬   s    ýr?   )%Ú__doc__Ú
__future__r   r<   Útypingr   Zcryptography.x509r   r   r   r   r   r	   r
   Zcryptography.x509.extensionsr   Zpyasn1.codec.der.decoderr   Zpyasn1.type.charr   Ú
exceptionsr   Zhazmatr   r   r   r   r   r   r   r   Ú__all__r   r#   r0   r   r?   r    r    r    r!   Ú<module>   s   $	($(5