U
    cI                     @   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
 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 d dlmZ dgZeeZeedddZdeeeee ee edddZdS )    N)datetimetimezone)Optional)CertValidationPolicySpecValidationDataHandlers)ValidationError)PastValidatePrecheckFailureTimeSlideFailure)
time_slide)ValidationTimingInfo)ValidationPath)NO_REVOCATIONCertRevTrustPolicy)async_validate_pathpast_validate)pathvalidation_policy_specc              
      s   t | jdd}tdd |D }tdd |D }||krDtdt||dd}tj|tt	d	d
j
|d d}zt|| |jI d H  W n, tk
r } ztd|W 5 d }~X Y nX d S )NF)Zinclude_rootc                 s   s   | ]}|j V  qd S N)Znot_valid_before.0c r   G/tmp/pip-unpacked-wheel-rwcmptg8/pyhanko_certvalidator/ltv/ades_past.py	<genexpr>$   s     z*_past_validate_precheck.<locals>.<genexpr>c                 s   s   | ]}|j V  qd S r   )Znot_valid_afterr   r   r   r   r   %   s     z`The intersection of the validity periods of the certificates in the path is empty or degenerate.TZvalidation_timebest_signature_timeZpoint_in_time_validation)Zrevocation_checking_policy)revinfo_policyZtiming_infohandlersz\Elementary path validation routine failed during pre-check for past point-in-time validation)listZ
iter_certsmaxminr   r   dataclassesreplacer   r   build_validation_contextr   pkix_validation_paramsr   )r   r   certsZlower_boundZupper_boundref_timevalidation_contexter   r   r   _past_validate_precheck   sB    	 r*   )r   r   validation_data_handlersinit_control_timer   returnc           	   
      s   t | |I dH  zN|p"tjtjd}t| ||j|j|j|j	dI dH }t
d|   | W n6 tk
r } ztd|   |W 5 d}~X Y nX t||p|dd}|j||d}t|| |jd	I dH  |S )
u  
    Execute the ETSI EN 319 102-1 past certificate validation algorithm
    against the given path (ETSI EN 319 102-1, § 5.6.2.1).

    Instead of merely evaluating X.509 validation constraints, the algorithm
    will perform a full point-in-time reevaluation of the path at the
    control time mandated by the specification. This implies that a caller
    implementing the past signature validation algorithm no longer needs to
    explicitly reevaluate CA certificate revocation times and/or algorithm
    constraints based on POEs.

    .. warning::
        This is incubating internal API.

    :param path:
        The prospective validation path against which to execute the algorithm.
    :param validation_policy_spec:
        The validation policy specification.
    :param validation_data_handlers:
        The handlers used to manage collected certificates,revocation
        information and proof-of-existence records.
    :param init_control_time:
        Initial control time; defaults to the current time.
    :param best_signature_time:
        Usage time to use in freshness computations.
    :return:
        The control time returned by the time sliding algorithm.
        Informally, the last time at which the certificate was known to be
        valid.
    N)tz)r,   Zrev_trust_policyZalgo_usage_policytime_tolerancerevinfo_managerzAAdES time slide yields %s as the control time for path with leaf zKFailed to get control time for point-in-time validation for path with leaf Tr   r   )
parameters)r*   r   nowr   utcr
   r   Zalgorithm_usage_policyr/   r0   loggerinfoZdescribe_leafr   r	   r   r$   r   r%   )	r   r   r+   r,   r   Zcontrol_timer)   r'   r(   r   r   r   r   G   sN    &
 )NN)r"   loggingr   r   typingr   Zpyhanko_certvalidator.contextr   r   Zpyhanko_certvalidator.errorsr   Z pyhanko_certvalidator.ltv.errorsr   r	   Z$pyhanko_certvalidator.ltv.time_slider
   Zpyhanko_certvalidator.ltv.typesr   Zpyhanko_certvalidator.pathr   Z!pyhanko_certvalidator.policy_declr   r   Zpyhanko_certvalidator.validater   __all__	getLogger__name__r4   r*   r   r   r   r   r   <module>   s4   
1  