B
    M]>                 @   sX  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 G dd	 d	eZG d
d deZG dd deZG dd deZG dd deZ G dd deZ!G dd deZ"G dd deZ#G dd deZ$G dd deZ%G dd deZ&G dd deZ'G d 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ZdS )VersionZv1Zv2Zv3)r   r      N)__name__
__module____qualname___map r   r   -lib/python3.7/site-packages/asn1crypto/crl.pyr   +   s   r   c            
   @   sd   e Zd Zdedddfdedddfd	ed
ddfdedddfdedddfdedddfgZdS )IssuingDistributionPointdistribution_pointr   T)explicitoptionalZonly_contains_user_certsr   F)implicitdefaultZonly_contains_ca_certsr   Zonly_some_reasons   )r%   r$   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Zd	S )
TBSCertListExtensionIdissuer_alt_name
crl_numberdelta_crl_indicatorissuing_distribution_pointauthority_key_identifierfreshest_crlauthority_information_access)z	2.5.29.18z	2.5.29.20z	2.5.29.27z	2.5.29.28z	2.5.29.35z	2.5.29.46z1.3.6.1.5.5.7.1.1N)r   r   r   r   r   r   r   r    r+   >   s   r+   c               @   s@   e Zd ZdefdeddifdefgZdZee	e	e
eeedZdS )	TBSCertListExtensionextn_idcriticalr&   F
extn_value)r4   r6   )r,   r-   r.   r/   r0   r1   r2   N)r   r   r   r+   r   r   r*   	_oid_pairr   r   r!   r   r   r   
_oid_specsr   r   r   r    r3   J   s   
r3   c               @   s   e Zd ZeZdS )TBSCertListExtensionsN)r   r   r   r3   _child_specr   r   r   r    r9   ]   s   r9   c               @   s2   e Zd Zddddddddd	d
d
Zedd ZdS )	CRLReasonunspecifiedkey_compromiseca_compromiseaffiliation_changed
supersededcessation_of_operationcertificate_holdremove_from_crlprivilege_withdrawnaa_compromise)
r   r   r   r'   r(   r)         	   
   c             C   s    ddddddddd	d
d
| 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".
        zan unspecified reasonza compromised keyzthe CA being compromisedzan affiliation changezcertificate supersessionza cessation of operationza certificate holdzremoval from the CRLzprivilege withdrawlzthe AA being compromised)
r<   r=   r>   r?   r@   rA   rB   rC   rD   rE   )native)selfr   r   r    human_friendlyo   s    zCRLReason.human_friendlyN)r   r   r   r   propertyrL   r   r   r   r    r;   a   s   r;   c               @   s   e Zd ZdddddZdS )CRLEntryExtensionId
crl_reasonhold_instruction_codeinvalidity_datecertificate_issuer)z	2.5.29.21z	2.5.29.23z	2.5.29.24z	2.5.29.29N)r   r   r   r   r   r   r   r    rN      s   rN   c               @   s:   e Zd ZdefdeddifdefgZdZee	e
edZdS )	CRLEntryExtensionr4   r5   r&   Fr6   )r4   r6   )rO   rP   rQ   rR   N)r   r   r   rN   r   r   r*   r7   r;   r   r
   r   r8   r   r   r   r    rS      s   
rS   c               @   s   e Zd ZeZdS )CRLEntryExtensionsN)r   r   r   rS   r:   r   r   r   r    rT      s   rT   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   sh   t  | _xT| d D ]H}|d j}d| }t| |rDt| ||d j |d jr| j| qW d| _dS )zv
        Sets common named extensions to private attributes and creates a list
        of critical extensions
        rV   r4   z	_%s_valuer6   r5   TN)set_critical_extensionsrJ   hasattrsetattrparsedadd_processed_extensions)rK   	extensionnameattribute_namer   r   r    _set_extensions   s    


z"RevokedCertificate._set_extensionsc             C   s   | j s|   | 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]   ra   rX   )rK   r   r   r    critical_extensions   s    
z&RevokedCertificate.critical_extensionsc             C   s   | j dkr|   | jS )z
        This extension indicates the reason that a certificate was revoked.

        :return:
            None or a CRLReason object
        F)r]   ra   _crl_reason_value)rK   r   r   r    crl_reason_value   s    	
z#RevokedCertificate.crl_reason_valuec             C   s   | j dkr|   | 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]   ra   _invalidity_date_value)rK   r   r   r    invalidity_date_value   s    
z(RevokedCertificate.invalidity_date_valuec             C   s   | j dkr|   | 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]   ra   _certificate_issuer_value)rK   r   r   r    certificate_issuer_value   s    
z+RevokedCertificate.certificate_issuer_valuec             C   s@   | j dkr:d| _ | jr:x"| jD ]}|jdkr|j| _ P qW | j S )zi
        :return:
            None, or an asn1crypto.x509.Name object for the issuer of the cert
        FNZdirectory_name)_issuer_namerh   r_   chosen)rK   general_namer   r   r    issuer_name   s    

zRevokedCertificate.issuer_name)r   r   r   r   r   rT   r*   r]   rX   rc   re   rg   ri   ra   rM   rb   rd   rf   rh   rl   r   r   r   r    rU      s   rU   c               @   s   e Zd ZeZdS )RevokedCertificatesN)r   r   r   rU   r:   r   r   r   r    rm     s   rm   c               @   sT   e Zd Zdeddifdefdefdefdeddifdeddifd	ed
ddfgZ	dS )TbsCertListversionr$   T	signatureissuerZthis_updateZnext_updateZrevoked_certificatescrl_extensionsr   )r#   r$   N)
r   r   r   r   r   r   r   rm   r9   r*   r   r   r   r    rn     s   rn   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_algorithmrp   FNc             C   sl   t  | _xX| d d D ]H}|d j}d| }t| |rHt| ||d j |d jr| j| qW d| _dS )	zv
        Sets common named extensions to private attributes and creates a list
        of critical extensions
        rt   rr   r4   z	_%s_valuer6   r5   TN)rW   rX   rJ   rY   rZ   r[   r\   r]   )rK   r^   r_   r`   r   r   r    ra   4  s    


zCertificateList._set_extensionsc             C   s   | j s|   | 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]   ra   rX   )rK   r   r   r    rb   F  s    
z#CertificateList.critical_extensionsc             C   s   | j dkr|   | 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]   ra   _issuer_alt_name_value)rK   r   r   r    issuer_alt_name_valueT  s    

z%CertificateList.issuer_alt_name_valuec             C   s   | j dkr|   | 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]   ra   _crl_number_value)rK   r   r   r    crl_number_valueb  s    

z CertificateList.crl_number_valuec             C   s   | j dkr|   | 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]   ra   _delta_crl_indicator_value)rK   r   r   r    delta_crl_indicator_valuep  s    

z)CertificateList.delta_crl_indicator_valuec             C   s   | j dkr|   | 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]   ra   !_issuing_distribution_point_value)rK   r   r   r     issuing_distribution_point_value~  s    

z0CertificateList.issuing_distribution_point_valuec             C   s   | j dkr|   | 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]   ra   _authority_key_identifier_value)rK   r   r   r    authority_key_identifier_value  s    

z.CertificateList.authority_key_identifier_valuec             C   s   | j dkr|   | 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]   ra   _freshest_crl_value)rK   r   r   r    freshest_crl_value  s    

z"CertificateList.freshest_crl_valuec             C   s   | j dkr|   | 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]   ra   #_authority_information_access_value)rK   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
        rt   rq   r   )rK   r   r   r    rq     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~   rJ   )rK   r   r   r    r0     s    z(CertificateList.authority_key_identifierc             C   st   | j dkrng | _ | jrnxV| jD ]L}|d jdkr|d }|jdkrDq|j}| dd dkr| 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   rJ   r_   lowerappend)rK   entrylocationZurlr   r   r    issuer_cert_urls  s    	

z CertificateList.issuer_cert_urlsc             C   sj   | j dkrdg | _ | jdk	rdxH| jD ]>}|d }|jdkr:q"x$|jD ]}|jdkrB| j | qBW q"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_   rj   r   )rK   r"   Zdistribution_point_namerk   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
        rp   )rJ   )rK   r   r   r    rp     s    zCertificateList.signaturec             C   s$   | j dkrt|   | _ | j S )zf
        :return:
            The SHA1 hash of the DER-encoded bytes of this certificate list
        N)_sha1hashlibsha1dumpdigest)rK   r   r   r    r     s    
zCertificateList.sha1c             C   s$   | j dkrt|   | _ | j S )zi
        :return:
            The SHA-256 hash of the DER-encoded bytes of this certificate list
        N)_sha256r   sha256r   r   )rK   r   r   r    r     s    
zCertificateList.sha256)%r   r   r   rn   r   r   r*   r]   rX   ru   rw   ry   r{   r}   r   r   r   r   r   r   ra   rM   rb   rv   rx   rz   r|   r~   r   r   rq   r0   r   r   rp   r   r   r   r   r   r    rs     s@   
		rs   ))__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+   r3   r9   r;   rN   rS   rT   rU   rm   rn   rs   r   r   r   r    <module>
   s$   ,(&	k