o
    CZc-                     @   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m	Z	 ddl
mZmZmZ ddlmZmZmZ ddlmZmZ dd	lmZ dd
lmZ g dZG dd dZdS )    N)OptionalIterable)x509   )ValidationContextPKIXValidationParams)ValidationErrorPathBuildingErrorInvalidCertificateError)async_validate_pathvalidate_tls_hostnamevalidate_usage)__version____version_info__)pretty_message)	type_name)r   r   CertificateValidatorr   r   c                	   @   s~   e Zd ZdZdZdZdZ			ddejde	e
ej  de	e defddZ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_certsvalidation_contextpkix_paramsc                 C   sF   |du rt  }|dur|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add_other_cert_context_certificate_params)selfr   r   r   r   r   intermediate_cert r   T/var/www/chikooza/env/lib/python3.10/site-packages/pyhanko_certvalidator/__init__.py__init__&   s    
zCertificateValidator.__init__c                    s   | j durdS g }| jj| jjv rttd| jjz| jj| jI dH }W n t	y@   | jj
dv r?ttd| jjj w |D ]+}zt| j|| jI dH  || _ W  dS  tyn } z|| W Y d}~qCd}~ww t|dkry|d d}|D ]
}dt|vr|}q}|r||d )ar  
        Builds possible certificate paths and validates them until a valid one
        is found, or all fail.

        :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
        Nz
                The X.509 certificate provided has a signature using the weak
                hash algorithm %s
                >   yesmaybez^
                    The X.509 certificate provided is self-signed - "%s"
                    r   r   	signature)_pathr   	hash_algor   weak_hash_algosr
   r   r   async_build_pathsr	   self_signedsubjecthuman_friendlyr   r   r   appendlenstr)r   
exceptionspathscandidate_pathenon_signature_exception	exceptionr   r   r   _validate_pathR   sP   


z#CertificateValidator._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r   	key_usageextended_key_usageextended_optionalr   r   r   r      s   8z#CertificateValidator.validate_usagec                    s*   |   I dH  t| j| 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)r4   r   r   r   r$   r;   r   r   r   r:      s   6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)r5   r6   r7   r8   r9   async_validate_tlsr   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)r4   r   r   r   r$   r@   r   r   r   r?   .  s   z'CertificateValidator.async_validate_tls)NNN)NF)__name__
__module____qualname__r   r   r$   r   r   Certificater   r   r   r   r    r4   r   r:   rB   r?   r   r   r   r   r      s0    
,
;D
@r   )r8   r5   typingr   r   
asn1cryptor   contextr   r   errorsr   r	   r
   validater   r   r   versionr   r   _errorsr   _typesr   __all__r   r   r   r   r   <module>   s    	