U
    c-                     @   s   d dl Z d dlZd dl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 ddlmZ dd	lmZ dd
lmZ ddlmZmZmZ ddlmZmZ ddddddgZdejee e
ee dddZG dd dZdS )    N)IterableOptional)x509   )	type_name)ValidationContext)InvalidCertificateErrorPathBuildingErrorValidationError)ValidationPath)PKIXValidationParams)CancelableAsyncIterator)async_validate_pathvalidate_tls_hostnamevalidate_usage)__version____version_info__r   r   CertificateValidatorr   r   find_valid_path)certificatepathsvalidation_contextpkix_validation_paramsc           	         s   g }zzh|2 z\3 d H W }z"t|||I d H  |W   W W nS  tk
rf } z|| W 5 d }~X Y qX q6 W n4 tk
r   | jdkrtd| jj d Y nX W 5 |  I d H  X t	|dkr|d d }|D ]}dt
|kr|}q|r||d d S )N>   maybeyesz1The X.509 certificate provided is self-signed - ""r   r   	signature)cancelr   r
   appendr	   Zself_signedr   subjectZhuman_friendlylenstr)	r   r   r   r   
exceptionscandidate_patheZnon_signature_exception	exception r&   B/tmp/pip-unpacked-wheel-rwcmptg8/pyhanko_certvalidator/__init__.pyr      s8      
$
c                   @   sx   e Zd ZdZdejeeej  ee ee	 dddZ
edd Zeddd	ZdddZdddZdd Zdd ZdS )r   N)end_entity_certintermediate_certsr   pkix_paramsc                 C   sF   |dkrt  }|dk	r0|j}|D ]}|| q || _|| _|| _dS )a
  
        :param end_entity_cert:
            An asn1crypto.x509.Certificate object X.509 end-entity
            certificate to validate

        :param intermediate_certs:
            None or a list of asn1crypto.x509.Certificate
            Used in constructing certificate paths for validation.

        :param validation_context:
            A pyhanko_certvalidator.context.ValidationContext() object that
            controls generic validation options and tracks revocation data.

            The same validation context will also be used in the validation
            of relevant certificates found in OCSP responses and/or CRLs.

        :param pkix_params:
            A pyhanko_certvalidator.context.PKIXValidationParams() object that
            controls advanced PKIX validation parameters used to validate
            the end-entity certificate. These can be used to constrain
            policy processing and names.

            Ancillary validation of CRLs and OCSP responses ignore these
            settings.
        N)r   certificate_registryregister_context_certificate_params)selfr(   r)   r   r*   r+   Zintermediate_certr&   r&   r'   __init__F   s    !zCertificateValidator.__init__c                 C   s   | j S )N)r.   )r0   r&   r&   r'   r   s   s    z CertificateValidator.certificate)returnc                    sH   | j dk	r| j S | j}| jj|}t||| j| jdI dH  | _ }|S )a  
        Builds possible certificate paths and validates them until a valid one
        is found, or all fail.

        :raises:
            pyhanko_certvalidator.errors.PathBuildingError - when an error occurs building the path
            pyhanko_certvalidator.errors.PathValidationError - when an error occurs validating the path
            pyhanko_certvalidator.errors.RevokedError - when the certificate or another certificate in its path has been revoked
        N)r   r   )_pathr.   r-   Zpath_builderZasync_build_paths_lazyr   r/   )r0   r   r   r#   r&   r&   r'   r   w   s    
z(CertificateValidator.async_validate_pathFc                 C   s    t dt t| |||S )a  
        Validates the certificate path and that the certificate is valid for
        the key usage and extended key usage purposes specified.

        .. deprecated:: 0.17.0
            Use :meth:`async_validate_usage` instead.

        :param key_usage:
            A set of unicode strings of the required key usage purposes. Valid
            values include:

             - "digital_signature"
             - "non_repudiation"
             - "key_encipherment"
             - "data_encipherment"
             - "key_agreement"
             - "key_cert_sign"
             - "crl_sign"
             - "encipher_only"
             - "decipher_only"

        :param extended_key_usage:
            A set of unicode strings of the required extended key usage
            purposes. These must be either dotted number OIDs, or one of the
            following extended key usage purposes:

             - "server_auth"
             - "client_auth"
             - "code_signing"
             - "email_protection"
             - "ipsec_end_system"
             - "ipsec_tunnel"
             - "ipsec_user"
             - "time_stamping"
             - "ocsp_signing"
             - "wireless_access_points"

            An example of a dotted number OID:

             - "1.3.6.1.5.5.7.3.1"

        :param extended_optional:
            A bool - if the extended_key_usage extension may be ommited and still
            considered valid

        :raises:
            pyhanko_certvalidator.errors.PathValidationError - when an error occurs validating the path
            pyhanko_certvalidator.errors.RevokedError - when the certificate or another certificate in its path has been revoked
            pyhanko_certvalidator.errors.InvalidCertificateError - when the certificate is not valid for the usages specified

        :return:
            A pyhanko_certvalidator.path.ValidationPath object of the validated
            certificate validation path
        zB'validate_usage' is deprecated, use 'async_validate_usage' instead)warningswarnDeprecationWarningasynciorunasync_validate_usage)r0   	key_usageextended_key_usageextended_optionalr&   r&   r'   r      s    :  z#CertificateValidator.validate_usagec                    s&   |   I dH }t| j| j||| |S )aN  
        Validates the certificate path and that the certificate is valid for
        the key usage and extended key usage purposes specified.

        :param key_usage:
            A set of unicode strings of the required key usage purposes. Valid
            values include:

             - "digital_signature"
             - "non_repudiation"
             - "key_encipherment"
             - "data_encipherment"
             - "key_agreement"
             - "key_cert_sign"
             - "crl_sign"
             - "encipher_only"
             - "decipher_only"

        :param extended_key_usage:
            A set of unicode strings of the required extended key usage
            purposes. These must be either dotted number OIDs, or one of the
            following extended key usage purposes:

             - "server_auth"
             - "client_auth"
             - "code_signing"
             - "email_protection"
             - "ipsec_end_system"
             - "ipsec_tunnel"
             - "ipsec_user"
             - "time_stamping"
             - "ocsp_signing"
             - "wireless_access_points"

            An example of a dotted number OID:

             - "1.3.6.1.5.5.7.3.1"

        :param extended_optional:
            A bool - if the extended_key_usage extension may be ommited and still
            considered valid

        :raises:
            pyhanko_certvalidator.errors.PathValidationError - when an error occurs validating the path
            pyhanko_certvalidator.errors.RevokedError - when the certificate or another certificate in its path has been revoked
            pyhanko_certvalidator.errors.InvalidCertificateError - when the certificate is not valid for the usages specified

        :return:
            A pyhanko_certvalidator.path.ValidationPath object of the validated
            certificate validation path
        N)r   r   r-   r.   )r0   r:   r;   r<   Zvalidated_pathr&   r&   r'   r9      s    7z)CertificateValidator.async_validate_usagec                 C   s   t dt t| |S )ah  
        Validates the certificate path, that the certificate is valid for
        the hostname provided and that the certificate is valid for the purpose
        of a TLS connection.

        .. deprecated:: 0.17.0
            Use :meth:`async_validate_tls` instead.

        :param hostname:
            A unicode string of the TLS server hostname

        :raises:
            pyhanko_certvalidator.errors.PathValidationError - when an error occurs validating the path
            pyhanko_certvalidator.errors.RevokedError - when the certificate or another certificate in its path has been revoked
            pyhanko_certvalidator.errors.InvalidCertificateError - when the certificate is not valid for TLS or the hostname

        :return:
            A pyhanko_certvalidator.path.ValidationPath object of the validated
            certificate validation path
        z>'validate_tls' is deprecated, use 'async_validate_tls' instead)r4   r5   r6   r7   r8   async_validate_tlsr0   hostnamer&   r&   r'   validate_tls  s
    z!CertificateValidator.validate_tlsc                    s$   |   I dH  t| j| j| | jS )a  
        Validates the certificate path, that the certificate is valid for
        the hostname provided and that the certificate is valid for the purpose
        of a TLS connection.

        :param hostname:
            A unicode string of the TLS server hostname

        :raises:
            pyhanko_certvalidator.errors.PathValidationError - when an error occurs validating the path
            pyhanko_certvalidator.errors.RevokedError - when the certificate or another certificate in its path has been revoked
            pyhanko_certvalidator.errors.InvalidCertificateError - when the certificate is not valid for TLS or the hostname

        :return:
            A pyhanko_certvalidator.path.ValidationPath object of the validated
            certificate validation path
        N)r   r   r-   r.   r3   r>   r&   r&   r'   r=   4  s    z'CertificateValidator.async_validate_tls)NNN)NF)NF)__name__
__module____qualname__r3   r   Certificater   r   r   r   r1   propertyr   r   r   r   r9   r@   r=   r&   r&   r&   r'   r   B   s*      -
   
G   
A)N) r7   r4   typingr   r   Z
asn1cryptor   _typesr   contextr   errorsr   r	   r
   pathr   Zpolicy_declr   utilr   validater   r   r   versionr   r   __all__rD   r   r   r&   r&   r&   r'   <module>   s4    (