U
    ;gM$                     @   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                   @   s@  e Zd ZdZd(eeee ee ee	 e
e dddZeeddd	Zeedd
dZeedddZeeej dddZeeej dddZeeej dddZedddZdd Ze
ej dddZee dddZe ee dddZ!e"e# d d!d"Z$e"e# d d#d$Z%eje&ed%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 | _|rXt| | _}|D ]}| | qH|| _dd |D | _	d S )Nc                 S   s   i | ]}|j |qS r   )Zcert_sha256).0Z	assertionr   r   I/tmp/pip-unpacked-wheel-hgp_x7fx/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   s   | j S )z<
        The proof-of-existence (POE) data manager.
        )r%   r-   r   r   r"   r   K   s    zRevinfoManager.poe_managerc                 C   s   | j S )z6
        The associated certificate registry.
        )r$   r1   r   r   r"   r   R   s    z#RevinfoManager.certificate_registryc                 C   s
   | j dk	S )zA
        Boolean indicating whether fetching is allowed.
        N)r+   r1   r   r   r"   fetching_allowedY   s    zRevinfoManager.fetching_allowedc                 C   s.   dd | j D }| js|S t| jj | S )zK
        A list of all cached :class:`crl.CertificateList` objects
        c                 S   s   g | ]
}|j qS r   )crl_datar!   Zcontr   r   r"   
<listcomp>f   s     z'RevinfoManager.crls.<locals>.<listcomp>)r(   r+   listcrl_fetcherZfetched_crls)r-   Zraw_crlsr   r   r"   r   `   s    zRevinfoManager.crlsc                 C   s.   dd | j D }| js|S t| jj | S )zI
        A list of all cached :class:`ocsp.OCSPResponse` objects
        c                 S   s   g | ]
}|j qS r   )ocsp_response_datar4   r   r   r"   r5   q   s     z(RevinfoManager.ocsps.<locals>.<listcomp>)r)   r+   r6   ocsp_fetcherZfetched_responses)r-   Z	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
        )r6   r&   valuesr1   r   r   r"   new_revocation_certsw   s    z#RevinfoManager.new_revocation_certs)r.   c                 C   s   | j }|| }| j}| j}| }|dk	r~|d r~|d D ]B}||r:|||j< |ttj	t
| |ttj|dd q: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)Zobject_typevalue)Zpoe_typedigestZpoe_timeZvalidation_object)r%   r$   r&   Zextract_basic_ocsp_responseregisterZissuer_serialZregister_known_poer   r   Z
VALIDATIONr   dumpr   r   CERTIFICATE)r-   r.   Zpoe_manZocsp_poe_timeregistryZ
revo_certsbasicZ
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'   getrD   )r-   rE   r   r   r"   check_crl_issuer   s    zRevinfoManager.check_crl_issuerc                    sb   | j s| jS | j }z|j|}W n& tk
rH   |j|I dH }Y nX 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   s   g | ]}t |qS r   )r   )r!   r3   r   r   r"   r5      s     z6RevinfoManager.async_retrieve_crls.<locals>.<listcomp>)r+   r(   r7   Zfetched_crls_for_certKeyErrorfetch)r-   rF   r    r   Zcontsr   r   r"   async_retrieve_crls   s    
z"RevinfoManager.async_retrieve_crls)	authorityr0   c              	      s   | j s| jS | j }dd |j|D }|s|j||I dH }t|}|D ]0}z| | W qN tk
r|   t	dY qNX qN|| 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   s   g | ]}t |qS r   )r   )r!   respr   r   r"   r5      s   z7RevinfoManager.async_retrieve_ocsps.<locals>.<listcomp>Nz9Failed to extract certificates from fetched OCSP response)
r+   r)   r9   Zfetched_responses_for_certrK   r   Z
load_multir*   
ValueErrorr   )r-   rF   rM   r    r   r8   rN   r   r   r"   async_retrieve_ocsps   s(    
 

z#RevinfoManager.async_retrieve_ocspshashes_to_evictc                    s(   t d 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                    s   t | j }| kS N)r   r8   r@   rT   r>   rQ   r   r"   p	  s    z%RevinfoManager.evict_ocsps.<locals>.pN)r   r6   filterr)   r-   rR   rW   r   rQ   r"   evict_ocsps  s    zRevinfoManager.evict_ocspsc                    s(   t d fdd}tt|| j| _dS )z
        Internal API to eliminate local CRLs from consideration.

        :param hashes_to_evict:
            A collection of CRL hashes; see :func:`.digest_for_poe`.
        rS   c                    s   t | j }| kS rU   )r   r3   r@   rV   rQ   r   r"   rW     s    z$RevinfoManager.evict_crls.<locals>.pN)r   r6   rX   r(   rY   r   rQ   r"   
evict_crls  s    zRevinfoManager.evict_crls)rF   atr0   c                 C   s0   z|| j |j jkW S  tk
r*   Y dS X d S )NF)r,   sha256r\   rJ   )r-   rF   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   r/   propertyr   r   boolr2   r   r   ZCertificateListr   r	   OCSPResponser   r
   Certificater;   r*   rG   rI   rL   r   rP   r   bytesrZ   r[   r   r^   r   r   r   r"   r      sH     
", r   N)!r   typingr   r   r   r   r   Z
asn1cryptor   r	   r
   Zpyhanko_certvalidator.authorityr   Zpyhanko_certvalidator.errorsr   Zpyhanko_certvalidator.fetchersr   Zpyhanko_certvalidator.ltv.poer   r   r   r   r   r   Z!pyhanko_certvalidator.policy_declr   Zpyhanko_certvalidator.registryr   Z&pyhanko_certvalidator.revinfo.archivalr   r   r   r   r   r   r   r"   <module>   s    