
+VY?                 @   s  d  Z  d d l m Z m Z m Z m Z d d l Z d d l m Z d d l	 m
 Z
 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 m Z m Z m Z m Z m Z Gd d	   d	 e  Z Gd
 d   d e  Z Gd d   d e  Z Gd d   d e  Z Gd d   d e  Z  Gd d   d e  Z! Gd d   d e  Z" Gd d   d e  Z# Gd d   d e  Z$ Gd d   d e  Z% Gd d   d e  Z& Gd d   d e  Z' Gd  d!   d! e  Z( d S)"z
ASN.1 type classes for certificate revocation lists (CRL). Exports the
following items:

 - CertificateList()

Other type classes are defined that help compose the types listed above.
    )unicode_literalsdivisionabsolute_importprint_functionN   )SignedDigestAlgorithm)	Boolean
EnumeratedGeneralizedTimeIntegerObjectIdentifierOctetBitStringParsableOctetStringSequence
SequenceOf)AuthorityInfoAccessSyntaxAuthorityKeyIdentifierCRLDistributionPointsDistributionPointNameGeneralNamesNameReasonFlagsTimec               @   s(   e  Z d  Z d d d d d d i Z d S)Versionr   Zv1r   Zv2   Zv3N)__name__
__module____qualname___map r   r   0/usr/lib/python3/dist-packages/asn1crypto/crl.pyr   +   s   r   c               @   s   e  Z d  Z d e d d d d d d i f d e d d	 d d
 d d i f d e d d	 d d d d i f d e d d	 d d d d i f d e d d	 d d d d i f d e d d	 d d d d i f g Z d S)IssuingDistributionPointdistribution_pointtag_typeexplicittagr   optionalTZonly_contains_user_certsZimplicitr   defaultFZonly_contains_ca_certsr   Zonly_some_reasons   Zindirect_crl   Zonly_contains_attribute_certs   N)r   r   r   r   r   r   _fieldsr   r   r   r    r!   3   s   r!   c               @   s@   e  Z d  Z d d d d d d d d d	 d
 d d d d i Z d S)TBSCertListExtensionIdz	2.5.29.18issuer_alt_namez	2.5.29.20
crl_numberz	2.5.29.27delta_crl_indicatorz	2.5.29.28issuing_distribution_pointz	2.5.29.35authority_key_identifierz	2.5.29.46freshest_crlz1.3.6.1.5.5.7.1.1authority_information_accessN)r   r   r   r   r   r   r   r    r,   >   s   r,   c               @   sp   e  Z d  Z d e f d e d d i f d e f g Z d Z d e d e	 d e	 d	 e
 d
 e d e d e i Z d S)TBSCertListExtensionextn_idcriticalr'   F
extn_valuer-   r.   r/   r0   r1   r2   r3   N)r5   r7   )r   r   r   r,   r   r   r+   	_oid_pairr   r   r!   r   r   r   
_oid_specsr   r   r   r    r4   J   s   	r4   c               @   s   e  Z d  Z e Z d S)TBSCertListExtensionsN)r   r   r   r4   _child_specr   r   r   r    r:   ]   s   r:   c               @   sd   e  Z d  Z d d d d d d d d d	 d
 d d d d d d d d d d i
 Z e d d    Z d S)	CRLReasonr   unspecifiedr   key_compromiser   ca_compromiser(   affiliation_changedr)   
supersededr*   cessation_of_operation   certificate_hold   remove_from_crl	   privilege_withdrawn
   aa_compromisec             C   sG   d d d d d d d d d	 d
 d d d d d d d d d d i
 |  j  S)a  
        :return:
            A unicode string with revocation description that is suitable to
            show to end-users. Starts with a lower case letter and phrased in
            such a way that it makes sense after the phrase "because of" or
            "due to".
        r=   zan unspecified reasonr>   za compromised keyr?   zthe CA being compromisedr@   zan affiliation changerA   zcertificate supersessionrB   za cessation of operationrD   za certificate holdrF   zremoval from the CRLrH   zprivilege withdrawlrJ   zthe AA being compromised)native)selfr   r   r    human_friendlyo   s    	zCRLReason.human_friendlyN)r   r   r   r   propertyrM   r   r   r   r    r<   a   s   r<   c               @   s.   e  Z d  Z d d d d d d d d i Z d	 S)
CRLEntryExtensionIdz	2.5.29.21
crl_reasonz	2.5.29.23hold_instruction_codez	2.5.29.24invalidity_datez	2.5.29.29certificate_issuerN)r   r   r   r   r   r   r   r    rO      s   rO   c               @   s^   e  Z d  Z d e f d e d d i f d e f g Z d Z d e d e	 d e
 d	 e i Z d
 S)CRLEntryExtensionr5   r6   r'   Fr7   rP   rQ   rR   rS   N)zextn_idz
extn_value)r   r   r   rO   r   r   r+   r8   r<   r   r
   r   r9   r   r   r   r    rT      s   	rT   c               @   s   e  Z d  Z e Z d S)CRLEntryExtensionsN)r   r   r   rT   r;   r   r   r   r    rU      s   rU   c               @   s   e  Z d  Z d e f d e f d e d d i f g Z d Z d Z d Z	 d Z
 d Z d Z d d	   Z e d
 d    Z e d d    Z e d d    Z e d d    Z e d d    Z d S)RevokedCertificateZuser_certificateZrevocation_datecrl_entry_extensionsr&   TFNc             C   s   t    |  _ xl |  d D]` } | d j } d | } t |  |  rZ t |  | | d j  | d j r |  j j |  q Wd |  _ d S)zv
        Sets common named extensions to private attributes and creates a list
        of critical extensions
        rW   r5   z	_%s_valuer7   r6   TN)set_critical_extensionsrK   hasattrsetattrparsedadd_processed_extensions)rL   	extensionnameattribute_namer   r   r    _set_extensions   s    
z"RevokedCertificate._set_extensionsc             C   s   |  j  s |  j   |  j S)z
        Returns a set of the names (or OID if not a known extension) of the
        extensions marked as critical

        :return:
            A set of unicode strings
        )r^   rb   rY   )rL   r   r   r    critical_extensions   s    
	
z&RevokedCertificate.critical_extensionsc             C   s    |  j  d k r |  j   |  j S)z
        This extension indicates the reason that a certificate was revoked.

        :return:
            None or a CRLReason object
        F)r^   rb   _crl_reason_value)rL   r   r   r    crl_reason_value   s    	
z#RevokedCertificate.crl_reason_valuec             C   s    |  j  d k r |  j   |  j S)a=  
        This extension indicates the suspected date/time the private key was
        compromised or the certificate became invalid. This would usually be
        before the revocation date, which is when the CA processed the
        revocation.

        :return:
            None or a GeneralizedTime object
        F)r^   rb   _invalidity_date_value)rL   r   r   r    invalidity_date_value   s    
z(RevokedCertificate.invalidity_date_valuec             C   s    |  j  d k r |  j   |  j S)a  
        This extension indicates the issuer of the certificate in question,
        and is used in indirect CRLs. CRL entries without this extension are
        for certificates issued from the last seen issuer.

        :return:
            None or an x509.GeneralNames object
        F)r^   rb   _certificate_issuer_value)rL   r   r   r    certificate_issuer_value   s    
z+RevokedCertificate.certificate_issuer_valuec             C   sX   |  j  d k rQ d |  _  |  j rQ x- |  j D]" } | j d k r+ | j |  _  Pq+ W|  j  S)zi
        :return:
            None, or an asn1crypto.x509.Name object for the issuer of the cert
        FNZdirectory_name)_issuer_nameri   r`   chosen)rL   general_namer   r   r    issuer_name   s    		zRevokedCertificate.issuer_name)r   r   r   r   r   rU   r+   r^   rY   rd   rf   rh   rj   rb   rN   rc   re   rg   ri   rm   r   r   r   r    rV      s   		rV   c               @   s   e  Z d  Z e Z d S)RevokedCertificatesN)r   r   r   rV   r;   r   r   r   r    rn     s   rn   c               @   s|   e  Z d  Z d e d d i f d e f d e f d e f d e f d e d d i f d	 e d
 d d d d d i f g Z	 d S)TbsCertListversionr&   T	signatureissuerZthis_updateZnext_updateZrevoked_certificatescrl_extensionsr#   r$   r%   r   N)
r   r   r   r   r   r   r   rn   r:   r+   r   r   r   r    ro     s   				ro   c               @   s  e  Z d  Z d e f d e f d e f g Z d Z d Z d Z	 d Z
 d Z d Z d Z d Z d Z d Z d Z d Z d Z d d   Z e d d	    Z e d
 d    Z e d d    Z e d d    Z e d d    Z e d d    Z e d d    Z e d d    Z e d d    Z e d d    Z e d d    Z  e d d    Z! e d  d!    Z" e d" d#    Z# e d$ d%    Z$ d S)&CertificateListtbs_cert_listZsignature_algorithmrq   FNc             C   s   t    |  _ xp |  d d D]` } | d j } d | } t |  |  r^ t |  | | d j  | d j r |  j j |  q Wd |  _ d S)	zv
        Sets common named extensions to private attributes and creates a list
        of critical extensions
        ru   rs   r5   z	_%s_valuer7   r6   TN)rX   rY   rK   rZ   r[   r\   r]   r^   )rL   r_   r`   ra   r   r   r    rb   4  s    
zCertificateList._set_extensionsc             C   s   |  j  s |  j   |  j S)z
        Returns a set of the names (or OID if not a known extension) of the
        extensions marked as critical

        :return:
            A set of unicode strings
        )r^   rb   rY   )rL   r   r   r    rc   F  s    
	
z#CertificateList.critical_extensionsc             C   s    |  j  d k r |  j   |  j S)z
        This extension allows associating one or more alternative names with
        the issuer of the CRL.

        :return:
            None or an x509.GeneralNames object
        F)r^   rb   _issuer_alt_name_value)rL   r   r   r    issuer_alt_name_valueT  s    

z%CertificateList.issuer_alt_name_valuec             C   s    |  j  d k r |  j   |  j S)z
        This extension adds a monotonically increasing number to the CRL and is
        used to distinguish different versions of the CRL.

        :return:
            None or an Integer object
        F)r^   rb   _crl_number_value)rL   r   r   r    crl_number_valueb  s    

z CertificateList.crl_number_valuec             C   s    |  j  d k r |  j   |  j S)z
        This extension indicates a CRL is a delta CRL, and contains the CRL
        number of the base CRL that it is a delta from.

        :return:
            None or an Integer object
        F)r^   rb   _delta_crl_indicator_value)rL   r   r   r    delta_crl_indicator_valuep  s    

z)CertificateList.delta_crl_indicator_valuec             C   s    |  j  d k r |  j   |  j S)z
        This extension includes information about what types of revocations
        and certificates are part of the CRL.

        :return:
            None or an IssuingDistributionPoint object
        F)r^   rb   !_issuing_distribution_point_value)rL   r   r   r     issuing_distribution_point_value~  s    

z0CertificateList.issuing_distribution_point_valuec             C   s    |  j  d k r |  j   |  j S)z
        This extension helps in identifying the public key with which to
        validate the authenticity of the CRL.

        :return:
            None or an AuthorityKeyIdentifier object
        F)r^   rb   _authority_key_identifier_value)rL   r   r   r    authority_key_identifier_value  s    

z.CertificateList.authority_key_identifier_valuec             C   s    |  j  d k r |  j   |  j S)z
        This extension is used in complete CRLs to indicate where a delta CRL
        may be located.

        :return:
            None or a CRLDistributionPoints object
        F)r^   rb   _freshest_crl_value)rL   r   r   r    freshest_crl_value  s    

z"CertificateList.freshest_crl_valuec             C   s    |  j  d k r |  j   |  j S)z
        This extension is used to provide a URL with which to download the
        certificate used to sign this CRL.

        :return:
            None or an AuthorityInfoAccessSyntax object
        F)r^   rb   #_authority_information_access_value)rL   r   r   r    "authority_information_access_value  s    

z2CertificateList.authority_information_access_valuec             C   s   |  d d S)z_
        :return:
            An asn1crypto.x509.Name object for the issuer of the CRL
        ru   rr   r   )rL   r   r   r    rr     s    zCertificateList.issuerc             C   s   |  j  s d S|  j  d j S)z
        :return:
            None or a byte string of the key_identifier from the authority key
            identifier extension
        NZkey_identifier)r   rK   )rL   r   r   r    r1     s    	z(CertificateList.authority_key_identifierc             C   s   |  j  d k r g  |  _  |  j r xu |  j D]j } | d j d k r+ | d } | j d k r` q+ | j } | j   d d  d k r+ |  j  j |  q+ W|  j  S)	z
        :return:
            A list of unicode strings that are URLs that should contain either
            an individual DER-encoded X.509 certificate, or a DER-encoded CMS
            message containing multiple certificates
        NZaccess_methodZ
ca_issuersZaccess_locationuniform_resource_identifierr      zhttp://)_issuer_cert_urlsr   rK   r`   lowerappend)rL   entrylocationZurlr   r   r    issuer_cert_urls  s    			
	z CertificateList.issuer_cert_urlsc             C   s   |  j  d k r g  |  _  |  j d k	 r x` |  j D]U } | d } | j d k rS q1 x0 | j D]% } | j d k r] |  j  j |  q] Wq1 W|  j  S)z
        Returns delta CRL URLs - only applies to complete CRLs

        :return:
            A list of zero or more DistributionPoint objects
        Nr"   Zname_relative_to_crl_issuerr   )_delta_crl_distribution_pointsr   r`   rk   r   )rL   r"   Zdistribution_point_namerl   r   r   r    delta_crl_distribution_points  s    		
z-CertificateList.delta_crl_distribution_pointsc             C   s   |  d j  S)zE
        :return:
            A byte string of the signature
        rq   )rK   )rL   r   r   r    rq     s    zCertificateList.signaturec             C   s4   |  j  d k r- t j |  j    j   |  _  |  j  S)zf
        :return:
            The SHA1 hash of the DER-encoded bytes of this certificate list
        N)_sha1hashlibsha1dumpdigest)rL   r   r   r    r     s    zCertificateList.sha1c             C   s4   |  j  d k r- t j |  j    j   |  _  |  j  S)zi
        :return:
            The SHA-256 hash of the DER-encoded bytes of this certificate list
        N)_sha256r   sha256r   r   )rL   r   r   r    r     s    zCertificateList.sha256)%r   r   r   ro   r   r   r+   r^   rY   rv   rx   rz   r|   r~   r   r   r   r   r   r   rb   rN   rc   rw   ry   r{   r}   r   r   r   rr   r1   r   r   rq   r   r   r   r   r   r    rt     s@   				rt   ))__doc__Z
__future__r   r   r   r   r   Zalgosr   Zcorer   r	   r
   r   r   r   r   r   r   Zx509r   r   r   r   r   r   r   r   r   r!   r,   r4   r:   r<   rO   rT   rU   rV   rn   ro   rt   r   r   r   r    <module>
   s$   "@:&	k