o
    &zhV(                     @   s   d dl m Z  d dlmZmZmZ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 d dlmZ d dlmZmZmZ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# G dd dZ$dS )    )datetime)AsyncGeneratorAsyncIterableAsyncIteratorDictIterableListOptionalSet)crlocspx509)	Authority)CRLFetchErrorOCSPFetchError)Fetchers)KnownPOE
POEManagerPOETypeValidationObjectValidationObjectTypedigest_for_poe)NonRevokedStatusAssertion)CertificateRegistry)CRLContainerOCSPContainersort_freshest_firstc                   @   sx  e Zd ZdZ		d2dededee dee dee	 d	e
e fd
dZedefddZedefddZedefddZedeej fddZedeej fddZedeej fddZdefddZdd Zde
ej fddZdee fd d!Zdee fd"d#Z dee fd$d%Z!d&e"dee fd'd(Z#d)e$e% fd*d+Z&d)e$e% fd,d-Z'd.ejd/e(defd0d1Z)dS )3RevinfoManagera  
    .. versionadded:: 0.20.0

    Class to manage and potentially fetch revocation information.

    :param certificate_registry:
        The associated certificate registry.
    :param poe_manager:
        The proof-of-existence (POE) data manager.
    :param crls:
        CRL data.
    :param ocsps:
        OCSP response data.
    :param fetchers:
        Fetchers for collecting revocation information.
        If ``None``, no fetching will be performed.
     Ncertificate_registrypoe_managercrlsocsps
assertionsfetchersc                 C   sr   || _ || _i | _i | _g | _|rt|| _g | _|r,t| | _}|D ]}| | q$|| _dd |D | _	d S )Nc                 S   s   i | ]}|j |qS r   )cert_sha256).0	assertionr   r   `/var/www/html/kangema/venv/lib/python3.10/site-packages/pyhanko_certvalidator/revinfo/manager.py
<dictcomp>O   s    z+RevinfoManager.__init__.<locals>.<dictcomp>)
_certificate_registry_poe_manager_revocation_certs_crl_issuer_map_crlsr   _ocsps_extract_ocsp_certs	_fetchers_assertions)selfr   r    r!   r"   r#   r$   ocsp_responser   r   r(   __init__5   s    	
zRevinfoManager.__init__returnc                 C      | j S )z<
        The proof-of-existence (POE) data manager.
        )r+   r3   r   r   r(   r    S      zRevinfoManager.poe_managerc                 C   r7   )z6
        The associated certificate registry.
        )r*   r8   r   r   r(   r   Z   r9   z#RevinfoManager.certificate_registryc                 C   s
   | j duS )zA
        Boolean indicating whether fetching is allowed.
        N)r1   r8   r   r   r(   fetching_alloweda   s   
zRevinfoManager.fetching_allowedc                 C   .   dd | j D }| js|S t| jj | S )zK
        A list of all cached :class:`crl.CertificateList` objects
        c                 S      g | ]}|j qS r   )crl_datar&   contr   r   r(   
<listcomp>n       z'RevinfoManager.crls.<locals>.<listcomp>)r.   r1   listcrl_fetcherfetched_crls)r3   raw_crlsr   r   r(   r!   h   s   zRevinfoManager.crlsc                 C   r;   )zI
        A list of all cached :class:`ocsp.OCSPResponse` objects
        c                 S   r<   r   )ocsp_response_datar>   r   r   r(   r@   y   rA   z(RevinfoManager.ocsps.<locals>.<listcomp>)r/   r1   rB   ocsp_fetcherfetched_responses)r3   	raw_ocspsr   r   r(   r"   s   s   zRevinfoManager.ocspsc                 C   s   t | j S )z
        A list of newly-fetched :class:`x509.Certificate` objects that were
        obtained from OCSP responses and CRLs
        )rB   r,   valuesr8   r   r   r(   new_revocation_certs   s   z#RevinfoManager.new_revocation_certsr4   c                 C   s   | j }|| }| j}| j}| }|dur?|d rA|d D ]%}||r>|||j< |ttj	t
| |ttj|dd qdS dS dS )z
        Extracts any certificates included with an OCSP response and adds them
        to the certificate registry

        :param ocsp_response:
            An asn1crypto.ocsp.OCSPResponse object to look for certs inside of
        Ncerts)object_typevalue)poe_typedigestpoe_timevalidation_object)r+   r*   r,   extract_basic_ocsp_responseregisterissuer_serialregister_known_poer   r   
VALIDATIONr   dumpr   r   CERTIFICATE)r3   r4   poe_manocsp_poe_timeregistry
revo_certsbasic
other_certr   r   r(   r0      s.   	


z"RevinfoManager._extract_ocsp_certsc                 C   s   || j |j< dS )aU  
        Records the certificate that issued a certificate list. Used to reduce
        processing code when dealing with self-issued certificates and multiple
        CRLs.

        :param certificate_list:
            An ans1crypto.crl.CertificateList object

        :param cert:
            An ans1crypto.x509.Certificate object
        N)r-   	signature)r3   certificate_listcertr   r   r(   record_crl_issuer   s   z RevinfoManager.record_crl_issuerc                 C   s   | j |jS )a3  
        Checks to see if the certificate that signed a certificate list has
        been found

        :param certificate_list:
            An ans1crypto.crl.CertificateList object

        :return:
            None if not found, or an asn1crypto.x509.Certificate object of the
            issuer
        )r-   getr`   )r3   ra   r   r   r(   check_crl_issuer   s   zRevinfoManager.check_crl_issuerc                 C   s4   t | j}| jr| jj }|dd |D  |S )z
        .. versionadded:: 0.27.0

        Return all currently available CRLs.

        :return:
            A list of :class:`CRLContainer` objects
        c                 s   s    | ]}t |V  qd S Nr   r&   r=   r   r   r(   	<genexpr>   s    z:RevinfoManager.currently_available_crls.<locals>.<genexpr>)rB   r.   r1   rC   rD   extend)r3   resultr!   r   r   r(   currently_available_crls   s
   
	z'RevinfoManager.currently_available_crlsc                    s^   | j std| j }z|j|}W n ty%   |j|I dH }Y nw dd |D }|S )z
        .. versionadded:: 0.27.0

        Download all relevant CRLs for a given certificate.

        :param cert:
            An asn1crypto.x509.Certificate object

        :return:
            A list of :class:`CRLContainer` objects
        zNo CRL fetcher availableNc                 S      g | ]}t |qS r   rg   rh   r   r   r(   r@      s    z-RevinfoManager.fetch_crls.<locals>.<listcomp>)r1   r   rC   fetched_crls_for_certKeyErrorfetch)r3   rb   r$   r!   contsr   r   r(   
fetch_crls   s   zRevinfoManager.fetch_crlsc                    s*   |   }| jr|| |I dH  |S )z
        .. versionadded:: 0.20.0

        :param cert:
            An asn1crypto.x509.Certificate object

        :return:
            A list of :class:`CRLContainer` objects
        N)rl   r1   rj   rr   )r3   rb   r!   r   r   r(   async_retrieve_crls   s
   
z"RevinfoManager.async_retrieve_crls	authorityc              	      s   | j s| jS | j }dd |j|D }|s=|j||I dH }t|}|D ]}z| | W q( ty<   t	dw || j S )a
  
        .. versionadded:: 0.20.0

        :param cert:
            An asn1crypto.x509.Certificate object

        :param authority:
            The issuing authority for the certificate

        :return:
            A list of :class:`OCSPContainer` objects
        c                 S   rm   r   )r   )r&   respr   r   r(   r@     s    z7RevinfoManager.async_retrieve_ocsps.<locals>.<listcomp>Nz9Failed to extract certificates from fetched OCSP response)
r1   r/   rG   fetched_responses_for_certrp   r   
load_multir0   
ValueErrorr   )r3   rb   rt   r$   r"   rF   ru   r   r   r(   async_retrieve_ocsps   s*   



z#RevinfoManager.async_retrieve_ocspshashes_to_evictc                    (   dt f fdd}tt|| j| _dS )z
        Internal API to eliminate local OCSP records from consideration.

        :param hashes_to_evict:
            A collection of OCSP response hashes; see :func:`.digest_for_poe`.
        	containerc                       t | j }| vS rf   )r   rF   rX   r|   rP   rz   r   r(   p1     z%RevinfoManager.evict_ocsps.<locals>.pN)r   rB   filterr/   r3   rz   r   r   r   r(   evict_ocsps)     zRevinfoManager.evict_ocspsc                    r{   )z
        Internal API to eliminate local CRLs from consideration.

        :param hashes_to_evict:
            A collection of CRL hashes; see :func:`.digest_for_poe`.
        r|   c                    r}   rf   )r   r=   rX   r~   r   r   r(   r   ?  r   z$RevinfoManager.evict_crls.<locals>.pN)r   rB   r   r.   r   r   r   r(   
evict_crls7  r   zRevinfoManager.evict_crlsrb   atc                 C   s*   z
|| j |j jkW S  ty   Y dS w )NF)r2   sha256r   ro   )r3   rb   r   r   r   r(   check_asserted_unrevokedE  s
   z'RevinfoManager.check_asserted_unrevoked)r   N)*__name__
__module____qualname____doc__r   r   r   r   r   r   r	   r   r5   propertyr    r   boolr:   r   r   CertificateListr!   r   OCSPResponser"   r   CertificaterK   r0   rc   re   rl   rr   rs   r   ry   r
   bytesr   r   r   r   r   r   r   r(   r   "   sb    

"
,r   N)%r   typingr   r   r   r   r   r   r	   r
   
asn1cryptor   r   r   pyhanko_certvalidator.authorityr   pyhanko_certvalidator.errorsr   r   pyhanko_certvalidator.fetchersr   pyhanko_certvalidator.ltv.poer   r   r   r   r   r   !pyhanko_certvalidator.policy_declr   pyhanko_certvalidator.registryr   &pyhanko_certvalidator.revinfo.archivalr   r   r   r   r   r   r   r(   <module>   s    ( 