B
    f]]                 @   s  d dl mZmZmZ d dlZd dlZd dlZd dlmZ d dl	Z	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 d dlmZ edd	d	Zd
d Zdd ZG dd deZdd Zdd Zdd Zdd Zdd Zdd Z G dd de!Z"e	#ej$G dd de%Z&e	#ej$G d d! d!e%Z'e	#ej$G d"d# d#e%Z(e	#ej$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.d/ Z.dS )0    )absolute_importdivisionprint_functionN)Enum)utils)dsaeced25519ed448rsa)	ExtensionExtensionType)Namei     c             C   s&   x |D ]}|j | j krtdqW d S )Nz$This extension has already been set.)oid
ValueError)	extension
extensionse r   5lib/python3.7/site-packages/cryptography/x509/base.py_reject_duplicate_extension   s    
r   c             C   s:   | j dk	r2|  }|r|nt }| jdd| S | S dS )zNormalizes a datetime to a naive datetime in UTC.

    time -- datetime to normalize. Assumed to be in UTC if not timezone
            aware.
    N)tzinfo)r   Z	utcoffsetdatetimeZ	timedeltareplace)timeoffsetr   r   r   _convert_to_naive_utc_time    s
    
r   c               @   s   e Zd ZdZdZdS )Versionr      N)__name__
__module____qualname__Zv1v3r   r   r   r   r   .   s   r   c             C   s
   | | S )N)load_pem_x509_certificate)databackendr   r   r   r$   3   s    r$   c             C   s
   | | S )N)load_der_x509_certificate)r%   r&   r   r   r   r'   7   s    r'   c             C   s
   | | S )N)load_pem_x509_csr)r%   r&   r   r   r   r(   ;   s    r(   c             C   s
   | | S )N)load_der_x509_csr)r%   r&   r   r   r   r)   ?   s    r)   c             C   s
   | | S )N)load_pem_x509_crl)r%   r&   r   r   r   r*   C   s    r*   c             C   s
   | | S )N)load_der_x509_crl)r%   r&   r   r   r   r+   G   s    r+   c                   s   e Zd Z fddZ  ZS )InvalidVersionc                s   t t| | || _d S )N)superr,   __init__parsed_version)selfmsgr/   )	__class__r   r   r.   L   s    zInvalidVersion.__init__)r    r!   r"   r.   __classcell__r   r   )r2   r   r,   K   s   r,   c               @   s   e Zd Zejdd Zejdd Zejdd Zejdd Z	ejd	d
 Z
ejdd Zejdd Zejdd Zejdd Zejdd Zejdd Zejdd Zejdd Zejdd Zejdd Zejdd  Zejd!d" Zd#S )$Certificatec             C   s   dS )z4
        Returns bytes using digest passed.
        Nr   )r0   	algorithmr   r   r   fingerprintS   s    zCertificate.fingerprintc             C   s   dS )z3
        Returns certificate serial number
        Nr   )r0   r   r   r   serial_numberY   s    zCertificate.serial_numberc             C   s   dS )z1
        Returns the certificate version
        Nr   )r0   r   r   r   version_   s    zCertificate.versionc             C   s   dS )z(
        Returns the public key
        Nr   )r0   r   r   r   
public_keye   s    zCertificate.public_keyc             C   s   dS )z?
        Not before time (represented as UTC datetime)
        Nr   )r0   r   r   r   not_valid_beforek   s    zCertificate.not_valid_beforec             C   s   dS )z>
        Not after time (represented as UTC datetime)
        Nr   )r0   r   r   r   not_valid_afterq   s    zCertificate.not_valid_afterc             C   s   dS )z1
        Returns the issuer name object.
        Nr   )r0   r   r   r   issuerw   s    zCertificate.issuerc             C   s   dS )z2
        Returns the subject name object.
        Nr   )r0   r   r   r   subject}   s    zCertificate.subjectc             C   s   dS )zt
        Returns a HashAlgorithm corresponding to the type of the digest signed
        in the certificate.
        Nr   )r0   r   r   r   signature_hash_algorithm   s    z$Certificate.signature_hash_algorithmc             C   s   dS )zJ
        Returns the ObjectIdentifier of the signature algorithm.
        Nr   )r0   r   r   r   signature_algorithm_oid   s    z#Certificate.signature_algorithm_oidc             C   s   dS )z/
        Returns an Extensions object.
        Nr   )r0   r   r   r   r      s    zCertificate.extensionsc             C   s   dS )z.
        Returns the signature bytes.
        Nr   )r0   r   r   r   	signature   s    zCertificate.signaturec             C   s   dS )zR
        Returns the tbsCertificate payload bytes as defined in RFC 5280.
        Nr   )r0   r   r   r   tbs_certificate_bytes   s    z!Certificate.tbs_certificate_bytesc             C   s   dS )z"
        Checks equality.
        Nr   )r0   otherr   r   r   __eq__   s    zCertificate.__eq__c             C   s   dS )z#
        Checks not equal.
        Nr   )r0   rB   r   r   r   __ne__   s    zCertificate.__ne__c             C   s   dS )z"
        Computes a hash.
        Nr   )r0   r   r   r   __hash__   s    zCertificate.__hash__c             C   s   dS )zB
        Serializes the certificate to PEM or DER format.
        Nr   )r0   encodingr   r   r   public_bytes   s    zCertificate.public_bytesN)r    r!   r"   abcabstractmethodr6   abstractpropertyr7   r8   r9   r:   r;   r<   r=   r>   r?   r   r@   rA   rC   rD   rE   rG   r   r   r   r   r4   Q   s"   r4   c               @   s   e Zd Zejdd Zejdd Zejdd Zejdd Z	ejd	d
 Z
ejdd Zejdd Zejdd Zejdd Zejdd Zejdd Zejdd Zejdd Zejdd Zejdd Zejdd  Zejd!d" Zd#S )$CertificateRevocationListc             C   s   dS )z:
        Serializes the CRL to PEM or DER format.
        Nr   )r0   rF   r   r   r   rG      s    z&CertificateRevocationList.public_bytesc             C   s   dS )z4
        Returns bytes using digest passed.
        Nr   )r0   r5   r   r   r   r6      s    z%CertificateRevocationList.fingerprintc             C   s   dS )zs
        Returns an instance of RevokedCertificate or None if the serial_number
        is not in the CRL.
        Nr   )r0   r7   r   r   r   (get_revoked_certificate_by_serial_number   s    zBCertificateRevocationList.get_revoked_certificate_by_serial_numberc             C   s   dS )zt
        Returns a HashAlgorithm corresponding to the type of the digest signed
        in the certificate.
        Nr   )r0   r   r   r   r>      s    z2CertificateRevocationList.signature_hash_algorithmc             C   s   dS )zJ
        Returns the ObjectIdentifier of the signature algorithm.
        Nr   )r0   r   r   r   r?      s    z1CertificateRevocationList.signature_algorithm_oidc             C   s   dS )zC
        Returns the X509Name with the issuer of this CRL.
        Nr   )r0   r   r   r   r<      s    z CertificateRevocationList.issuerc             C   s   dS )z?
        Returns the date of next update for this CRL.
        Nr   )r0   r   r   r   next_update   s    z%CertificateRevocationList.next_updatec             C   s   dS )z?
        Returns the date of last update for this CRL.
        Nr   )r0   r   r   r   last_update   s    z%CertificateRevocationList.last_updatec             C   s   dS )zS
        Returns an Extensions object containing a list of CRL extensions.
        Nr   )r0   r   r   r   r      s    z$CertificateRevocationList.extensionsc             C   s   dS )z.
        Returns the signature bytes.
        Nr   )r0   r   r   r   r@      s    z#CertificateRevocationList.signaturec             C   s   dS )zO
        Returns the tbsCertList payload bytes as defined in RFC 5280.
        Nr   )r0   r   r   r   tbs_certlist_bytes   s    z,CertificateRevocationList.tbs_certlist_bytesc             C   s   dS )z"
        Checks equality.
        Nr   )r0   rB   r   r   r   rC     s    z CertificateRevocationList.__eq__c             C   s   dS )z#
        Checks not equal.
        Nr   )r0   rB   r   r   r   rD     s    z CertificateRevocationList.__ne__c             C   s   dS )z<
        Number of revoked certificates in the CRL.
        Nr   )r0   r   r   r   __len__  s    z!CertificateRevocationList.__len__c             C   s   dS )zS
        Returns a revoked certificate (or slice of revoked certificates).
        Nr   )r0   idxr   r   r   __getitem__  s    z%CertificateRevocationList.__getitem__c             C   s   dS )z8
        Iterator over the revoked certificates
        Nr   )r0   r   r   r   __iter__  s    z"CertificateRevocationList.__iter__c             C   s   dS )zQ
        Verifies signature of revocation list against given public key.
        Nr   )r0   r9   r   r   r   is_signature_valid  s    z,CertificateRevocationList.is_signature_validN)r    r!   r"   rH   rI   rG   r6   rL   rJ   r>   r?   r<   rM   rN   r   r@   rO   rC   rD   rP   rR   rS   rT   r   r   r   r   rK      s"   rK   c               @   s   e Zd Zejdd Zejdd Zejdd Zejdd Zej	d	d
 Z
ej	dd Zej	dd Zej	dd Zejdd Zej	dd Zej	dd Zej	dd ZdS )CertificateSigningRequestc             C   s   dS )z"
        Checks equality.
        Nr   )r0   rB   r   r   r   rC   (  s    z CertificateSigningRequest.__eq__c             C   s   dS )z#
        Checks not equal.
        Nr   )r0   rB   r   r   r   rD   .  s    z CertificateSigningRequest.__ne__c             C   s   dS )z"
        Computes a hash.
        Nr   )r0   r   r   r   rE   4  s    z"CertificateSigningRequest.__hash__c             C   s   dS )z(
        Returns the public key
        Nr   )r0   r   r   r   r9   :  s    z$CertificateSigningRequest.public_keyc             C   s   dS )z2
        Returns the subject name object.
        Nr   )r0   r   r   r   r=   @  s    z!CertificateSigningRequest.subjectc             C   s   dS )zt
        Returns a HashAlgorithm corresponding to the type of the digest signed
        in the certificate.
        Nr   )r0   r   r   r   r>   F  s    z2CertificateSigningRequest.signature_hash_algorithmc             C   s   dS )zJ
        Returns the ObjectIdentifier of the signature algorithm.
        Nr   )r0   r   r   r   r?   M  s    z1CertificateSigningRequest.signature_algorithm_oidc             C   s   dS )z@
        Returns the extensions in the signing request.
        Nr   )r0   r   r   r   r   S  s    z$CertificateSigningRequest.extensionsc             C   s   dS )z;
        Encodes the request to PEM or DER format.
        Nr   )r0   rF   r   r   r   rG   Y  s    z&CertificateSigningRequest.public_bytesc             C   s   dS )z.
        Returns the signature bytes.
        Nr   )r0   r   r   r   r@   _  s    z#CertificateSigningRequest.signaturec             C   s   dS )zd
        Returns the PKCS#10 CertificationRequestInfo bytes as defined in RFC
        2986.
        Nr   )r0   r   r   r   tbs_certrequest_bytese  s    z/CertificateSigningRequest.tbs_certrequest_bytesc             C   s   dS )z8
        Verifies signature of signing request.
        Nr   )r0   r   r   r   rT   l  s    z,CertificateSigningRequest.is_signature_validN)r    r!   r"   rH   rI   rC   rD   rE   r9   rJ   r=   r>   r?   r   rG   r@   rV   rT   r   r   r   r   rU   &  s   rU   c               @   s6   e Zd Zejdd Zejdd Zejdd ZdS )RevokedCertificatec             C   s   dS )zG
        Returns the serial number of the revoked certificate.
        Nr   )r0   r   r   r   r7   u  s    z RevokedCertificate.serial_numberc             C   s   dS )zH
        Returns the date of when this certificate was revoked.
        Nr   )r0   r   r   r   revocation_date{  s    z"RevokedCertificate.revocation_datec             C   s   dS )zW
        Returns an Extensions object containing a list of Revoked extensions.
        Nr   )r0   r   r   r   r     s    zRevokedCertificate.extensionsN)r    r!   r"   rH   rJ   r7   rX   r   r   r   r   r   rW   s  s   rW   c               @   s2   e Zd Zdg fddZdd Zdd Zdd	 ZdS )
 CertificateSigningRequestBuilderNc             C   s   || _ || _dS )zB
        Creates an empty X.509 certificate request (v1).
        N)_subject_name_extensions)r0   subject_namer   r   r   r   r.     s    z)CertificateSigningRequestBuilder.__init__c             C   s0   t |tstd| jdk	r$tdt|| jS )zF
        Sets the certificate requestor's distinguished name.
        zExpecting x509.Name object.Nz&The subject name may only be set once.)
isinstancer   	TypeErrorrZ   r   rY   r[   )r0   namer   r   r   r\     s
    

z-CertificateSigningRequestBuilder.subject_namec             C   s@   t |tstdt|j||}t|| j t| j| j|g S )zE
        Adds an X.509 extension to the certificate request.
        z"extension must be an ExtensionType)	r]   r   r^   r   r   r   r[   rY   rZ   )r0   r   criticalr   r   r   add_extension  s    
z.CertificateSigningRequestBuilder.add_extensionc             C   s    | j dkrtd|| ||S )zF
        Signs the request using the requestor's private key.
        Nz/A CertificateSigningRequest must have a subject)rZ   r   Zcreate_x509_csr)r0   private_keyr5   r&   r   r   r   sign  s    
z%CertificateSigningRequestBuilder.sign)r    r!   r"   r.   r\   ra   rc   r   r   r   r   rY     s   
rY   c               @   sd   e Zd Zddddddg fddZdd Zdd Zdd	 Zd
d Zdd Zdd Z	dd Z
dd ZdS )CertificateBuilderNc             C   s6   t j| _|| _|| _|| _|| _|| _|| _|| _	d S )N)
r   r#   Z_version_issuer_namerZ   _public_key_serial_number_not_valid_before_not_valid_afterr[   )r0   issuer_namer\   r9   r7   r:   r;   r   r   r   r   r.     s    zCertificateBuilder.__init__c             C   sD   t |tstd| jdk	r$tdt|| j| j| j| j	| j
| jS )z3
        Sets the CA's distinguished name.
        zExpecting x509.Name object.Nz%The issuer name may only be set once.)r]   r   r^   re   r   rd   rZ   rf   rg   rh   ri   r[   )r0   r_   r   r   r   rj     s    


zCertificateBuilder.issuer_namec             C   sD   t |tstd| jdk	r$tdt| j|| j| j| j	| j
| jS )z:
        Sets the requestor's distinguished name.
        zExpecting x509.Name object.Nz&The subject name may only be set once.)r]   r   r^   rZ   r   rd   re   rf   rg   rh   ri   r[   )r0   r_   r   r   r   r\     s    


zCertificateBuilder.subject_namec             C   sX   t |tjtjtjtjt	j
fs&td| jdk	r8tdt| j| j|| j| j| j| jS )zT
        Sets the requestor's public key (as found in the signing request).
        zhExpecting one of DSAPublicKey, RSAPublicKey, EllipticCurvePublicKey, Ed25519PublicKey or Ed448PublicKey.Nz$The public key may only be set once.)r]   r   ZDSAPublicKeyr   ZRSAPublicKeyr   ZEllipticCurvePublicKeyr	   ZEd25519PublicKeyr
   ZEd448PublicKeyr^   rf   r   rd   re   rZ   rg   rh   ri   r[   )r0   keyr   r   r   r9     s    


zCertificateBuilder.public_keyc             C   sj   t |tjstd| jdk	r&td|dkr6td| dkrJtdt| j| j	| j
|| j| j| jS )z5
        Sets the certificate serial number.
        z'Serial number must be of integral type.Nz'The serial number may only be set once.r   z%The serial number should be positive.   z3The serial number should not be more than 159 bits.)r]   sixinteger_typesr^   rg   r   
bit_lengthrd   re   rZ   rf   rh   ri   r[   )r0   numberr   r   r   r7     s    

z CertificateBuilder.serial_numberc             C   sz   t |tjstd| jdk	r&tdt|}|tk r>td| jdk	rZ|| jkrZtdt| j	| j
| j| j|| j| jS )z7
        Sets the certificate activation time.
        zExpecting datetime object.Nz*The not valid before may only be set once.z>The not valid before date must be on or after 1950 January 1).zBThe not valid before date must be before the not valid after date.)r]   r   r^   rh   r   r   _EARLIEST_UTC_TIMEri   rd   re   rZ   rf   rg   r[   )r0   r   r   r   r   r:     s    

z#CertificateBuilder.not_valid_beforec             C   sz   t |tjstd| jdk	r&tdt|}|tk r>td| jdk	rZ|| jk rZtdt| j	| j
| j| j| j|| jS )z7
        Sets the certificate expiration time.
        zExpecting datetime object.Nz)The not valid after may only be set once.z<The not valid after date must be on or after 1950 January 1.zAThe not valid after date must be after the not valid before date.)r]   r   r^   ri   r   r   rq   rh   rd   re   rZ   rf   rg   r[   )r0   r   r   r   r   r;     s    


z"CertificateBuilder.not_valid_afterc          	   C   sT   t |tstdt|j||}t|| j t| j| j	| j
| j| j| j| j|g S )z=
        Adds an X.509 extension to the certificate.
        z"extension must be an ExtensionType)r]   r   r^   r   r   r   r[   rd   re   rZ   rf   rg   rh   ri   )r0   r   r`   r   r   r   ra   2  s    
z CertificateBuilder.add_extensionc             C   sz   | j dkrtd| jdkr$td| jdkr6td| jdkrHtd| jdkrZtd| jdkrltd|| ||S )zC
        Signs the certificate using the CA's private key.
        Nz&A certificate must have a subject namez&A certificate must have an issuer namez'A certificate must have a serial numberz/A certificate must have a not valid before timez.A certificate must have a not valid after timez$A certificate must have a public key)rZ   r   re   rg   rh   ri   rf   Zcreate_x509_certificate)r0   rb   r5   r&   r   r   r   rc   B  s    





zCertificateBuilder.sign)r    r!   r"   r.   rj   r\   r9   r7   r:   r;   ra   rc   r   r   r   r   rd     s   
rd   c               @   sP   e Zd Zdddg g fddZdd Zdd Zdd	 Zd
d Zdd Zdd Z	dS ) CertificateRevocationListBuilderNc             C   s"   || _ || _|| _|| _|| _d S )N)re   _last_update_next_updater[   _revoked_certificates)r0   rj   rN   rM   r   Zrevoked_certificatesr   r   r   r.   \  s
    z)CertificateRevocationListBuilder.__init__c             C   s<   t |tstd| jd k	r$tdt|| j| j| j| j	S )NzExpecting x509.Name object.z%The issuer name may only be set once.)
r]   r   r^   re   r   rr   rs   rt   r[   ru   )r0   rj   r   r   r   rj   d  s    


z,CertificateRevocationListBuilder.issuer_namec             C   sr   t |tjstd| jd k	r&tdt|}|tk r>td| jd k	rZ|| jkrZtdt| j	|| j| j
| jS )NzExpecting datetime object.z!Last update may only be set once.z8The last update date must be on or after 1950 January 1.z9The last update date must be before the next update date.)r]   r   r^   rs   r   r   rq   rt   rr   re   r[   ru   )r0   rN   r   r   r   rN   n  s    

z,CertificateRevocationListBuilder.last_updatec             C   sr   t |tjstd| jd k	r&tdt|}|tk r>td| jd k	rZ|| jk rZtdt| j	| j|| j
| jS )NzExpecting datetime object.z!Last update may only be set once.z8The last update date must be on or after 1950 January 1.z8The next update date must be after the last update date.)r]   r   r^   rt   r   r   rq   rs   rr   re   r[   ru   )r0   rM   r   r   r   rM     s    

z,CertificateRevocationListBuilder.next_updatec             C   sL   t |tstdt|j||}t|| j t| j| j	| j
| j|g | jS )zM
        Adds an X.509 extension to the certificate revocation list.
        z"extension must be an ExtensionType)r]   r   r^   r   r   r   r[   rr   re   rs   rt   ru   )r0   r   r`   r   r   r   ra     s    
z.CertificateRevocationListBuilder.add_extensionc             C   s2   t |tstdt| j| j| j| j| j|g S )z8
        Adds a revoked certificate to the CRL.
        z)Must be an instance of RevokedCertificate)	r]   rW   r^   rr   re   rs   rt   r[   ru   )r0   Zrevoked_certificater   r   r   add_revoked_certificate  s    
z8CertificateRevocationListBuilder.add_revoked_certificatec             C   sD   | j d krtd| jd kr$td| jd kr6td|| ||S )NzA CRL must have an issuer namez"A CRL must have a last update timez"A CRL must have a next update time)re   r   rs   rt   Zcreate_x509_crl)r0   rb   r5   r&   r   r   r   rc     s    


z%CertificateRevocationListBuilder.sign)
r    r!   r"   r.   rj   rN   rM   ra   rv   rc   r   r   r   r   rr   [  s   
rr   c               @   s<   e Zd Zddg fddZdd Zdd Zdd	 Zd
d ZdS )RevokedCertificateBuilderNc             C   s   || _ || _|| _d S )N)rg   _revocation_dater[   )r0   r7   rX   r   r   r   r   r.     s    z"RevokedCertificateBuilder.__init__c             C   sZ   t |tjstd| jd k	r&td|dkr6td| dkrJtdt|| j| j	S )Nz'Serial number must be of integral type.z'The serial number may only be set once.r   z$The serial number should be positiverl   z3The serial number should not be more than 159 bits.)
r]   rm   rn   r^   rg   r   ro   rw   rx   r[   )r0   rp   r   r   r   r7     s    
z'RevokedCertificateBuilder.serial_numberc             C   sN   t |tjstd| jd k	r&tdt|}|tk r>tdt| j|| j	S )NzExpecting datetime object.z)The revocation date may only be set once.z7The revocation date must be on or after 1950 January 1.)
r]   r   r^   rx   r   r   rq   rw   rg   r[   )r0   r   r   r   r   rX     s    
z)RevokedCertificateBuilder.revocation_datec             C   sD   t |tstdt|j||}t|| j t| j| j	| j|g S )Nz"extension must be an ExtensionType)
r]   r   r^   r   r   r   r[   rw   rg   rx   )r0   r   r`   r   r   r   ra     s    
z'RevokedCertificateBuilder.add_extensionc             C   s.   | j d krtd| jd kr$td|| S )Nz/A revoked certificate must have a serial numberz1A revoked certificate must have a revocation date)rg   r   rx   Zcreate_x509_revoked_certificate)r0   r&   r   r   r   build  s    

zRevokedCertificateBuilder.build)r    r!   r"   r.   r7   rX   ra   ry   r   r   r   r   rw     s   rw   c               C   s   t tddd? S )N   Zbigr   )r   Zint_from_bytesosurandomr   r   r   r   random_serial_number  s    r}   )/Z
__future__r   r   r   rH   r   r{   enumr   rm   Zcryptographyr   Z)cryptography.hazmat.primitives.asymmetricr   r   r	   r
   r   Zcryptography.x509.extensionsr   r   Zcryptography.x509.namer   rq   r   r   r   r$   r'   r(   r)   r*   r+   	Exceptionr,   Zadd_metaclassABCMetaobjectr4   rK   rU   rW   rY   rd   rr   rw   r}   r   r   r   r   <module>   sD   ijL) +_;