o
    >eM$                     @   s   d dl 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 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)DictIterableListOptionalSet)crlocspx509)	Authority)OCSPFetchError)Fetchers)KnownPOE
POEManagerPOETypeValidationObjectValidationObjectTypedigest_for_poe)NonRevokedStatusAssertion)CertificateRegistry)CRLContainerOCSPContainersort_freshest_firstc                   @   sT  e Zd ZdZ		d.d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 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d,d-Z'dS )/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   f/var/www/html/humari/django-venv/lib/python3.10/site-packages/pyhanko_certvalidator/revinfo/manager.py
<dictcomp>G   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__-   s    	
zRevinfoManager.__init__returnc                 C      | j S )z<
        The proof-of-existence (POE) data manager.
        )r'   r/   r   r   r$   r   K      zRevinfoManager.poe_managerc                 C   r3   )z6
        The associated certificate registry.
        )r&   r4   r   r   r$   r   R   r5   z#RevinfoManager.certificate_registryc                 C   s
   | j duS )zA
        Boolean indicating whether fetching is allowed.
        N)r-   r4   r   r   r$   fetching_allowedY   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>f       z'RevinfoManager.crls.<locals>.<listcomp>)r*   r-   listcrl_fetcherfetched_crls)r/   raw_crlsr   r   r$   r   `   s   zRevinfoManager.crlsc                 C   r7   )zI
        A list of all cached :class:`ocsp.OCSPResponse` objects
        c                 S   r8   r   )ocsp_response_datar:   r   r   r$   r<   q   r=   z(RevinfoManager.ocsps.<locals>.<listcomp>)r+   r-   r>   ocsp_fetcherfetched_responses)r/   	raw_ocspsr   r   r$   r   k   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
        )r>   r(   valuesr4   r   r   r$   new_revocation_certsw   s   z#RevinfoManager.new_revocation_certsr0   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)r/   r0   poe_manocsp_poe_timeregistry
revo_certsbasic
other_certr   r   r$   r,      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)r/   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\   )r/   r]   r   r   r$   check_crl_issuer   s   zRevinfoManager.check_crl_issuerc                    sb   | j s| jS | j }z|j|}W n ty$   |j|I dH }Y nw dd |D }|| j S )z
        .. versionadded:: 0.20.0

        :param cert:
            An asn1crypto.x509.Certificate object

        :return:
            A list of :class:`CRLContainer` objects
        Nc                 S      g | ]}t |qS r   )r   )r"   r9   r   r   r$   r<      s    z6RevinfoManager.async_retrieve_crls.<locals>.<listcomp>)r-   r*   r?   fetched_crls_for_certKeyErrorfetch)r/   r^   r    r   contsr   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   rb   r   )r   )r"   respr   r   r$   r<      s    z7RevinfoManager.async_retrieve_ocsps.<locals>.<listcomp>Nz9Failed to extract certificates from fetched OCSP response)
r-   r+   rC   fetched_responses_for_certre   r   
load_multir,   
ValueErrorr   )r/   r^   rh   r    r   rB   ri   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 N)r   rB   rT   rp   rL   rn   r   r$   p	     z%RevinfoManager.evict_ocsps.<locals>.pN)r   r>   filterr+   r/   rn   ru   r   rt   r$   evict_ocsps     zRevinfoManager.evict_ocspsc                    ro   )z
        Internal API to eliminate local CRLs from consideration.

        :param hashes_to_evict:
            A collection of CRL hashes; see :func:`.digest_for_poe`.
        rp   c                    rq   rr   )r   r9   rT   rs   rt   r   r$   ru     rv   z$RevinfoManager.evict_crls.<locals>.pN)r   r>   rw   r*   rx   r   rt   r$   
evict_crls  rz   zRevinfoManager.evict_crlsr^   atc                 C   s*   z
|| j |j jkW S  ty   Y dS w )NF)r.   sha256r|   rd   )r/   r^   r|   r   r   r$   check_asserted_unrevoked  s
   z'RevinfoManager.check_asserted_unrevoked)r   N)(__name__
__module____qualname____doc__r   r   r   r   r   r   r   r   r1   propertyr   r   boolr6   r   r   CertificateListr   r	   OCSPResponser   r
   CertificaterG   r,   r_   ra   rg   r   rm   r   bytesry   r{   r   r~   r   r   r   r$   r      s^    

"
,r   N)!r   typingr   r   r   r   r   
asn1cryptor   r	   r
   pyhanko_certvalidator.authorityr   pyhanko_certvalidator.errorsr   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     