U
    \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   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.

    :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``
    Zcert_patternsZobligatory_idsZoptional_idsN)r   extract_idsr   )certificateZhostname r   ?/usr/lib/python3/dist-packages/service_identity/cryptography.pyr   &   s
    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.

    :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   Z
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k
r*   Y nX |dd |jtD  |dd |jt	D  |dd |jt
D  |jtD ]B}|jtkrt|j\}}t|tr|t|  qtdq|sd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   s   g | ]}t |d qS zutf-8r   encode).0namer   r   r   
<listcomp>u   s   zextract_ids.<locals>.<listcomp>c                 S   s   g | ]}t |d qS r    )r   r"   )r#   Zurir   r   r   r%   {   s   c                 S   s   g | ]}t |qS r   )r   )r#   Z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   s   g | ]}t |d qS r    r!   r'   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.)
extensionsZget_extension_for_oidr   ZSUBJECT_ALTERNATIVE_NAMEr   extendr&   Zget_values_for_typer   r   r   r
   Ztype_idID_ON_DNS_SRVr   
isinstancer   appendr   ZasOctetsr   ZsubjectZget_attributes_for_oidr   ZCOMMON_NAMEnextiterwarningswarnformatr   )ZcertZidsZextotherZsrv_ZcnsZcnr   r   r   r   a   sV    




r   )$__doc__Z
__future__r   r   r   r0   Zcryptography.x509r   r   r   r   r	   r
   r   Zcryptography.x509.extensionsr   Zpyasn1.codec.der.decoderr   Zpyasn1.type.charr   Z_commonr   r   r   r   r   r   r   r   
exceptionsr   __all__r   r   r+   r   r   r   r   r   <module>   s   $	(
