U
    c>                  
   @   s  d dl Z d dlmZmZ d dlmZmZmZmZmZ d dl	m
Z
mZmZ d dlmZ d dlmZmZmZmZ d dlmZ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"m#Z#m$Z$m%Z% d dl&m'Z'm(Z(m)Z) d dl*m+Z+ ddgZ,ee eeeee" ee' f dddZ-edddZ.ee
j/eej0dddZ1eeeedddZ2ee eeee ej0dddZ3eee eee ee+e4 e+e ed	d d!Z5eee eee eed"d#dZ6dS )$    N)datetime	timedelta)IterableListOptionalSetTuple)algoskeysx509)ValProcState)DisallowedAlgorithmErrorInsufficientPOEErrorInsufficientRevinfoErrorRevokedError)ValidationTimingInfoValidationTimingParams)ValidationPath)AlgorithmUsagePolicyCertRevTrustPolicyRevocationCheckingRule)RevinfoContainer)RevinfoManager)CRLOfInterest_check_cert_on_crl_and_delta_CRLErrs collect_relevant_crls_with_paths)OCSPResponseOfInterest_check_ocsp_status%collect_relevant_responses_with_paths)ConsList
time_slideades_gather_prima_facie_revinfo)pathrevinfo_managercontrol_timerevocation_checking_rulereturnc           	         sZ   | j }|jr(t|| ||I dH }|j}ng }|jrNt|| ||I dH }|j}ng }||fS )a  
    Gather potentially relevant revocation information for the leaf
    certificate of a candidate validation path.
    Only the scope of the revocation information will be checked, no
    detailed validation will occur.

    :param path:
        The candidate validation path.
    :param revinfo_manager:
        The revocation info manager.
    :param control_time:
        The time horizon that serves as a relevance cutoff.
    :param revocation_checking_rule:
        Revocation info rule controlling which kind(s) of revocation
        information will be fetched.
    :return:
        A 2-element tuple containing a list of the fetched CRLs and
        OCSP responses, respectively.
    N)leafZocsp_relevantr   	responsesZcrl_relevantr   crls)	r#   r$   r%   r&   certZocsp_resultocspsZ
crl_resultr*    r-   H/tmp/pip-unpacked-wheel-rwcmptg8/pyhanko_certvalidator/ltv/time_slide.pyr"   *   s(       
   
)r#   c                 c   s0   | }|dfV  |j dkr,| }|dfV  qd S )NT   F)pkix_lenZcopy_and_drop_leaf)r#   Zcur_pathr-   r-   r.   _tailsW   s
    

r1   )algo_policy	algo_usedr%   
public_keyc                 C   sl   |  |||}|d j}|jsh|jr2t||j}n6d| d}|jd k	rX|d|j 7 }t|ddd d|S )N	algorithmz
Algorithm z- is banned outright without time constraints.z	 Reason: FT)Z
is_ee_certZis_side_validationZbanned_since)Zsignature_algorithm_allowedZnativeallowedZnot_allowed_afterminZfailure_reasonr   )r2   r3   r%   r4   Zsig_constraintZ	algo_namemsgr-   r-   r.   _apply_algo_policy_   s(      


r9   r%   revinfo_containerrev_trust_policytime_tolerancec              	   C   sL   | |tt| | dd|d}|j}|jjsH|jp4|}|d k	rHt|| } | S )NT)Zvalidation_timeZbest_signature_timeZpoint_in_time_validation)Ztiming_infor=   )Z	usable_atr   r   issuance_dateZratingZusable_adesZlast_usable_atr7   )r%   r;   r<   r=   Z	usabilityr>   Zcutoff_dater-   r-   r.   "_update_control_time_for_unrevoked~   s"    

r?   )revoked_dater%   r;   r2   issuer_public_keyc                 C   s6   | rt | |}|j}|d k	r2|d k	r2t||||}|S N)r7   Zrevinfo_sig_mechanism_usedr9   )r@   r%   r;   r2   rA   r3   r-   r-   r.   _update_control_time   s    
   rC   )	r#   init_control_timer$   r<   algo_usage_policyr=   
cert_stack
path_stackr'   c           %         s  |j }| jdkr|S tttt| }	j}
|	D ]j\}}t||rR|jn|jdI d H \}}|j	}|
| |
| td}|
| krtd|jj d  d||s|st|tjr|jj}nd}|jd krtd  d	| d|d
}d }|D ]}|jj}|r|ks|
|jj krLq|j}ttdd |D B tj fdd|D  I d H }tf||D ]}t|j j	||j|j!t" d\}}|j j	}t|tjst#||d k	O }|j}|d ks|j|jk r|}t$|| |j%dqqd }|D ]}|j&}|j}|r:|ks:|
|j&j' krpq:t(|j) dI d H zt*|tdd d }W n* t+k
r }  z
| j,}W 5 d } ~ X Y nX ||d k	O }|j)j	}!t|!tjst#|d ks|j|k r|}t$|| |!j%dq: d k	r^t|- d }"t. |d |"j%|s6dd ||fD }#t/|#fddd d}$|$d k	r6t0|$dq6S )Nr   )r$   r%   r&   )Zcert_path_stackz0No proof of existence available for certificate z at control time .zattribute certificatezNo revocation info from before z found for certificate Fc                 s   s   | ]}|  V  qd S rB   )dump).0r+   r-   r-   r.   	<genexpr>  s    z_time_slide.<locals>.<genexpr>c                 3   s@   | ]8}|j jr|j j krt|j  d V  qdS )rF   rG   N)r#   r(   rI   _time_slide)rJ   Zcrl_pathrE   r%   Znew_cert_stackZnew_path_stackr<   r$   Zsub_path_skip_listr=   r-   r.   rK   
  s   )Z
crl_issuerr+   Zcertificate_list_contZdelta_certificate_list_contZerrs)r;   r2   rA   rL   )ocsp_response
proc_stater%   Zsignature_algorithmc                 S   s   g | ]}|d k	r|qS rB   r-   )rJ   xr-   r-   r.   
<listcomp>y  s     z_time_slide.<locals>.<listcomp>c                    s
   | j p S rB   )r>   )rR   )r%   r-   r.   <lambda>~      z_time_slide.<locals>.<lambda>)keydefaultr:   )1Zrevocation_checking_policyr0   listreversedr1   poe_managerr"   Zee_certificate_ruleZintermediate_ca_cert_ruler(   ZconsrI   r   r   Z
from_statesubjectZhuman_friendly	isoformat
isinstancer   ZCertificateZocsp_no_check_valuer   Zcrlr>   Zcrl_dataZ
prov_pathssetasyncioZgatherr7   r   r#   deltar   AssertionErrorrC   r4   rO   Zocsp_response_datarM   Z	prov_pathr   r   Zrevocation_dtZiter_authoritiesr9   maxr?   )%r#   rD   r$   r<   rE   r=   rF   rG   Zchecking_policyZpartial_pathsrZ   current_pathZis_eer*   r,   r+   rP   identZonce_revokedZmost_recent_crlZcrl_of_interestZissuedZ	sub_pathsZsub_path_control_timesZcandidate_crl_pathr@   Zrevoked_reasonZcrl_iss_certZcrl_containerZmost_recent_ocspZocsp_of_interestZocsp_containereZocsp_iss_certZleaf_caZrevinfo_itemsZmost_recent_revinfor-   rN   r.   rM      s4   











	
rM   )r#   rD   r$   r<   rE   r=   r'   c              
      s&   t | |||||t t dI dH S )a  
    Execute the ETSI EN 319 102-1 time slide algorithm against the given path.

    .. warning::
        This is incubating internal API.

    .. note::
        This implementation will also attempt to take into account chains of
        trust of indirect CRLs. This is not a requirement of the specification,
        but also somewhat unlikely to arise in practice in cases where AdES
        compliance actually matters.

    :param path:
        The prospective validation path against which to execute the time slide
        algorithm.
    :param init_control_time:
        The initial control time, typically the current time.
    :param revinfo_manager:
        The revocation info manager.
    :param rev_trust_policy:
        The trust policy for revocation information.
    :param algo_usage_policy:
        The algorithm usage policy.
    :param time_tolerance:
        The tolerance to apply when evaluating time-related constraints.
    :return:
        The resulting control time.
    rL   N)rM   r    empty)r#   rD   r$   r<   rE   r=   r-   r-   r.   r!     s    $)7r_   r   r   typingr   r   r   r   r   Z
asn1cryptor	   r
   r   Zpyhanko_certvalidator._stater   Zpyhanko_certvalidator.errorsr   r   r   r   Zpyhanko_certvalidator.ltv.typesr   r   Zpyhanko_certvalidator.pathr   Z!pyhanko_certvalidator.policy_declr   r   r   Z&pyhanko_certvalidator.revinfo.archivalr   Z%pyhanko_certvalidator.revinfo.managerr   Z*pyhanko_certvalidator.revinfo.validate_crlr   r   r   r   Z+pyhanko_certvalidator.revinfo.validate_ocspr   r   r   Zpyhanko_certvalidator.utilr    __all__r"   r1   ZSignedDigestAlgorithmZPublicKeyInfor9   r?   rC   bytesrM   r!   r-   r-   r-   r.   <module>   sp   -	 & Y