o
    &zhs@                     @   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,dede dededeee" ee' f f
ddZ-defddZ.dede
j/dedej0def
ddZ1deded ed!efd"d#Z2d$ee dededee d%ej0defd&d'Z3ded(ede d ed)ee d!ed*e+e4 d+e+e defd,d-Z5ded(ede d ed)ee d!edef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           	         sb   | j }|jrt|| ||I dH }|j}ng }|jr+t|| ||I dH }|j}||fS 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)leafocsp_relevantr   	responsescrl_relevantr   crls)	r#   r$   r%   r&   certocsp_resultocsps
crl_resultr,    r1   _/var/www/html/kangema/venv/lib/python3.10/site-packages/pyhanko_certvalidator/ltv/time_slide.pyr"   )   s    

c                 c   s>    | }|dfV  |j dkr| }|dfV  |j dksd S d S )NT   F)pkix_lencopy_and_drop_leaf)r#   cur_pathr1   r1   r2   _tailsV   s   


r7   algo_policy	algo_used
public_keyval_proc_statec                 C   sn   |  |||}|d j}|js5|jrt||j}|S d| d}|jd ur-|d|j 7 }tj||d d|S )N	algorithmz
Algorithm z- is banned outright without time constraints.z	 Reason: )banned_since)signature_algorithm_allowednativeallowednot_allowed_afterminfailure_reasonr   
from_state)r8   r9   r%   r:   r;   sig_constraint	algo_namemsgr1   r1   r2   _apply_algo_policy^   s$   


rH   revinfo_containerrev_trust_policytime_tolerancec              	   C   sL   | |tt| | dd|d}|j}|jjs$|jp|}|d ur$t|| } | S )NT)validation_timebest_signature_timepoint_in_time_validation)timing_inforK   )	usable_atr   r   issuance_dateratingusable_adeslast_usable_atrB   )r%   rI   rJ   rK   	usabilityrQ   cutoff_dater1   r1   r2   "_update_control_time_for_unrevoked}   s"   

rW   revoked_dateissuer_public_keyc                 C   s8   | rt | |}|j}|d ur|d urt|||||}|S N)rB   revinfo_sig_mechanism_usedrH   )rX   r%   rI   r8   rY   r;   r9   r1   r1   r2   _update_control_time   s   
r\   init_control_timealgo_usage_policy
cert_stack
path_stackc           %         s  |j }| jdkr|S tttt| }	j}
|	D ]\}}t||r*|jn|jdI d H \}}|j	}|
| |
| td}|
| kr_td|jj d  d||s|st|tjrn|jj}nd}|jd u rtd  d	| d|d
}d }|D ]}|jj}|r|ks|
|j krq|j}ttdd |D B tj fdd|D  I d H }tg||D ]J}t|jj	||j|j t! d\}}|jj	}t|tjsJ ||d uO }|j}|d u r|}n|jr|jr|j|jk r|}t"|| |j#|dqqd }|D ]}|j$}|j}|r5|ks5|
|j$ kr7qt%|j& dI d H zt'|tdd d }W n t(yk }  z	| j)}W Y d } ~ nd } ~ ww ||d uO }|j&j	}!t|!tjsJ |d u s|jr|j|k r|}t"|| |!j#|dq d urt|* d }"t+ |d |"j#|d|sdd ||fD }#t,|#fddd d}$|$d urt-|$dqS )Nr   )r$   r%   r&   )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 rZ   )dump).0r-   r1   r1   r2   	<genexpr>	  s    
z_time_slide.<locals>.<genexpr>c                 3   sB    | ]}|j jr|j j vrt|j  d V  qdS )r_   r`   N)r#   r(   rc   _time_slide)rd   crl_pathr^   r%   new_cert_stacknew_path_stackrJ   r$   sub_path_skip_listrK   r1   r2   re     s$    )
crl_issuerr-   certificate_list_contdelta_certificate_list_conterrs)rI   r8   rY   r;   rf   )ocsp_response
proc_stater%   signature_algorithm)r;   c                 S   s   g | ]}|d ur|qS rZ   r1   )rd   xr1   r1   r2   
<listcomp>  s    z_time_slide.<locals>.<listcomp>c                    s
   | j p S rZ   )rQ   )ru   )r%   r1   r2   <lambda>  s   
 z_time_slide.<locals>.<lambda>)keydefault)r%   rI   rJ   rK   ).revocation_checking_policyr4   listreversedr7   poe_managerr"   ee_certificate_ruleintermediate_ca_cert_ruler(   consrc   r   r   rD   subjecthuman_friendly	isoformat
isinstancer   Certificateocsp_no_check_valuer   crlrQ   
prov_pathssetasynciogatherrB   r   r#   deltar   r\   r:   rq   rg   	prov_pathr   r   revocation_dtiter_authoritiesrH   maxrW   )%r#   r]   r$   rJ   r^   rK   r_   r`   checking_policypartial_pathsr}   current_pathis_eer,   r/   r-   rr   identonce_revokedmost_recent_crlcrl_of_interestissued	sub_pathssub_path_control_timescandidate_crl_pathrX   revoked_reasoncrl_iss_certcrl_containermost_recent_ocspocsp_of_interestocsp_containereocsp_iss_certleaf_carevinfo_itemsmost_recent_revinfor1   ri   r2   rg      sF  








!




	


rg   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.
    rf   N)rg   r    empty)r#   r]   r$   rJ   r^   rK   r1   r1   r2   r!     s   $)7r   r   r   typingr   r   r   r   r   
asn1cryptor	   r
   r   pyhanko_certvalidator._stater   pyhanko_certvalidator.errorsr   r   r   r   pyhanko_certvalidator.ltv.typesr   r   pyhanko_certvalidator.pathr   !pyhanko_certvalidator.policy_declr   r   r   &pyhanko_certvalidator.revinfo.archivalr   %pyhanko_certvalidator.revinfo.managerr   *pyhanko_certvalidator.revinfo.validate_crlr   r   r   r   +pyhanko_certvalidator.revinfo.validate_ocspr   r   r   pyhanko_certvalidator.utilr    __all__r"   r7   SignedDigestAlgorithmPublicKeyInforH   rW   r\   bytesrg   r!   r1   r1   r1   r2   <module>   s    
-

$
	

 [