U
    *}f)g                     @   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 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 d d	lmZ ed
ddZG dd deZdd Zdd Zdd Z G dd deZ!d6ddZ"d7ddZ#d8ddZ$d9ddZ%d:ddZ&d;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+Z0G d.d/ d/e+Z1G d0d1 d1e+Z2G d2d3 d3e+Z3d4d5 Z4dS )<    )absolute_importdivisionprint_functionN)Enum)utils)_get_backend)dsaeced25519ed448rsa)	ExtensionExtensionType)Name)ObjectIdentifieri     c                       s   e Zd Z fddZ  ZS )AttributeNotFoundc                    s   t t| | || _d S N)superr   __init__oid)selfmsgr   	__class__ :/tmp/pip-unpacked-wheel-x36vw73o/cryptography/x509/base.pyr       s    zAttributeNotFound.__init____name__
__module____qualname__r   __classcell__r   r   r   r   r      s   r   c                 C   s"   |D ]}|j | j krtdqd S )Nz$This extension has already been set.)r   
ValueError)	extension
extensionser   r   r   _reject_duplicate_extension%   s    r&   c                 C   s"   |D ]\}}|| krt dqd S )Nz$This attribute has already been set.)r"   )r   
attributesZattr_oid_r   r   r   _reject_duplicate_attribute,   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*   	utcoffsetdatetime	timedeltareplace)timeoffsetr   r   r   _convert_to_naive_utc_time3   s
    
r1   c                   @   s   e Zd ZdZdZdS )Versionr      N)r   r   r    Zv1v3r   r   r   r   r2   A   s   r2   c                 C   s   t |}|| S r   )r   load_pem_x509_certificatedatabackendr   r   r   r5   F   s    r5   c                 C   s   t |}|| S r   )r   load_der_x509_certificater6   r   r   r   r9   K   s    r9   c                 C   s   t |}|| S r   )r   load_pem_x509_csrr6   r   r   r   r:   P   s    r:   c                 C   s   t |}|| S r   )r   load_der_x509_csrr6   r   r   r   r;   U   s    r;   c                 C   s   t |}|| S r   )r   load_pem_x509_crlr6   r   r   r   r<   Z   s    r<   c                 C   s   t |}|| S r   )r   load_der_x509_crlr6   r   r   r   r=   _   s    r=   c                       s   e Zd Z fddZ  ZS )InvalidVersionc                    s   t t| | || _d S r   )r   r>   r   parsed_version)r   r   r?   r   r   r   r   e   s    zInvalidVersion.__init__r   r   r   r   r   r>   d   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   r   	algorithmr   r   r   fingerprintl   s    zCertificate.fingerprintc                 C   s   dS )z3
        Returns certificate serial number
        Nr   r   r   r   r   serial_numberr   s    zCertificate.serial_numberc                 C   s   dS )z1
        Returns the certificate version
        Nr   rE   r   r   r   versionx   s    zCertificate.versionc                 C   s   dS z(
        Returns the public key
        Nr   rE   r   r   r   
public_key~   s    zCertificate.public_keyc                 C   s   dS )z?
        Not before time (represented as UTC datetime)
        Nr   rE   r   r   r   not_valid_before   s    zCertificate.not_valid_beforec                 C   s   dS )z>
        Not after time (represented as UTC datetime)
        Nr   rE   r   r   r   not_valid_after   s    zCertificate.not_valid_afterc                 C   s   dS )z1
        Returns the issuer name object.
        Nr   rE   r   r   r   issuer   s    zCertificate.issuerc                 C   s   dS z2
        Returns the subject name object.
        Nr   rE   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   rE   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   rE   r   r   r   signature_algorithm_oid   s    z#Certificate.signature_algorithm_oidc                 C   s   dS )z/
        Returns an Extensions object.
        Nr   rE   r   r   r   r$      s    zCertificate.extensionsc                 C   s   dS z.
        Returns the signature bytes.
        Nr   rE   r   r   r   	signature   s    zCertificate.signaturec                 C   s   dS )zR
        Returns the tbsCertificate payload bytes as defined in RFC 5280.
        Nr   rE   r   r   r   tbs_certificate_bytes   s    z!Certificate.tbs_certificate_bytesc                 C   s   dS z"
        Checks equality.
        Nr   r   otherr   r   r   __eq__   s    zCertificate.__eq__c                 C   s   dS z#
        Checks not equal.
        Nr   rW   r   r   r   __ne__   s    zCertificate.__ne__c                 C   s   dS z"
        Computes a hash.
        Nr   rE   r   r   r   __hash__   s    zCertificate.__hash__c                 C   s   dS )zB
        Serializes the certificate to PEM or DER format.
        Nr   r   encodingr   r   r   public_bytes   s    zCertificate.public_bytesN)r   r   r    abcabstractmethodrD   abstractpropertyrF   rG   rI   rJ   rK   rL   rN   rP   rR   r$   rT   rU   rY   r[   r]   r`   r   r   r   r   r@   j   sD   















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 )$CertificateRevocationListc                 C   s   dS )z:
        Serializes the CRL to PEM or DER format.
        Nr   r^   r   r   r   r`      s    z&CertificateRevocationList.public_bytesc                 C   s   dS rA   r   rB   r   r   r   rD      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   )r   rF   r   r   r   (get_revoked_certificate_by_serial_number   s    zBCertificateRevocationList.get_revoked_certificate_by_serial_numberc                 C   s   dS rO   r   rE   r   r   r   rP      s    z2CertificateRevocationList.signature_hash_algorithmc                 C   s   dS rQ   r   rE   r   r   r   rR      s    z1CertificateRevocationList.signature_algorithm_oidc                 C   s   dS )zC
        Returns the X509Name with the issuer of this CRL.
        Nr   rE   r   r   r   rL      s    z CertificateRevocationList.issuerc                 C   s   dS )z?
        Returns the date of next update for this CRL.
        Nr   rE   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   rE   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   rE   r   r   r   r$     s    z$CertificateRevocationList.extensionsc                 C   s   dS rS   r   rE   r   r   r   rT     s    z#CertificateRevocationList.signaturec                 C   s   dS )zO
        Returns the tbsCertList payload bytes as defined in RFC 5280.
        Nr   rE   r   r   r   tbs_certlist_bytes  s    z,CertificateRevocationList.tbs_certlist_bytesc                 C   s   dS rV   r   rW   r   r   r   rY     s    z CertificateRevocationList.__eq__c                 C   s   dS rZ   r   rW   r   r   r   r[      s    z CertificateRevocationList.__ne__c                 C   s   dS )z<
        Number of revoked certificates in the CRL.
        Nr   rE   r   r   r   __len__&  s    z!CertificateRevocationList.__len__c                 C   s   dS )zS
        Returns a revoked certificate (or slice of revoked certificates).
        Nr   )r   idxr   r   r   __getitem__,  s    z%CertificateRevocationList.__getitem__c                 C   s   dS )z8
        Iterator over the revoked certificates
        Nr   rE   r   r   r   __iter__2  s    z"CertificateRevocationList.__iter__c                 C   s   dS )zQ
        Verifies signature of revocation list against given public key.
        Nr   )r   rI   r   r   r   is_signature_valid8  s    z,CertificateRevocationList.is_signature_validN)r   r   r    ra   rb   r`   rD   re   rc   rP   rR   rL   rf   rg   r$   rT   rh   rY   r[   ri   rk   rl   rm   r   r   r   r   rd      sD   















rd   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dS )CertificateSigningRequestc                 C   s   dS rV   r   rW   r   r   r   rY   A  s    z CertificateSigningRequest.__eq__c                 C   s   dS rZ   r   rW   r   r   r   r[   G  s    z CertificateSigningRequest.__ne__c                 C   s   dS r\   r   rE   r   r   r   r]   M  s    z"CertificateSigningRequest.__hash__c                 C   s   dS rH   r   rE   r   r   r   rI   S  s    z$CertificateSigningRequest.public_keyc                 C   s   dS rM   r   rE   r   r   r   rN   Y  s    z!CertificateSigningRequest.subjectc                 C   s   dS rO   r   rE   r   r   r   rP   _  s    z2CertificateSigningRequest.signature_hash_algorithmc                 C   s   dS rQ   r   rE   r   r   r   rR   f  s    z1CertificateSigningRequest.signature_algorithm_oidc                 C   s   dS )z@
        Returns the extensions in the signing request.
        Nr   rE   r   r   r   r$   l  s    z$CertificateSigningRequest.extensionsc                 C   s   dS )z;
        Encodes the request to PEM or DER format.
        Nr   r^   r   r   r   r`   r  s    z&CertificateSigningRequest.public_bytesc                 C   s   dS rS   r   rE   r   r   r   rT   x  s    z#CertificateSigningRequest.signaturec                 C   s   dS )zd
        Returns the PKCS#10 CertificationRequestInfo bytes as defined in RFC
        2986.
        Nr   rE   r   r   r   tbs_certrequest_bytes~  s    z/CertificateSigningRequest.tbs_certrequest_bytesc                 C   s   dS )z8
        Verifies signature of signing request.
        Nr   rE   r   r   r   rm     s    z,CertificateSigningRequest.is_signature_validc                 C   s   dS )z:
        Get the attribute value for a given OID.
        Nr   rE   r   r   r   get_attribute_for_oid  s    z/CertificateSigningRequest.get_attribute_for_oidN)r   r   r    ra   rb   rY   r[   r]   rI   rc   rN   rP   rR   r$   r`   rT   ro   rm   rp   r   r   r   r   rn   ?  s4   











rn   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   rE   r   r   r   rF     s    z RevokedCertificate.serial_numberc                 C   s   dS )zH
        Returns the date of when this certificate was revoked.
        Nr   rE   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   rE   r   r   r   r$     s    zRevokedCertificate.extensionsN)r   r   r    ra   rc   rF   rr   r$   r   r   r   r   rq     s   

rq   c                   @   s>   e Zd Zdg g fddZdd Zdd Zdd	 Zdd
dZdS ) CertificateSigningRequestBuilderNc                 C   s   || _ || _|| _dS )zB
        Creates an empty X.509 certificate request (v1).
        N)_subject_name_extensions_attributes)r   subject_namer$   r'   r   r   r   r     s    z)CertificateSigningRequestBuilder.__init__c                 C   s4   t |tstd| jdk	r$tdt|| j| jS )zF
        Sets the certificate requestor's distinguished name.
        Expecting x509.Name object.N&The subject name may only be set once.)
isinstancer   	TypeErrorrt   r"   rs   ru   rv   r   namer   r   r   rw     s    

  z-CertificateSigningRequestBuilder.subject_namec                 C   sD   t |tstdt|j||}t|| j t| j| j|g | j	S )zE
        Adds an X.509 extension to the certificate request.
        "extension must be an ExtensionType)
rz   r   r{   r   r   r&   ru   rs   rt   rv   r   r#   criticalr   r   r   add_extension  s    

z.CertificateSigningRequestBuilder.add_extensionc                 C   sL   t |tstdt |ts$tdt|| j t| j| j| j||fg S )zK
        Adds an X.509 attribute with an OID and associated value.
        zoid must be an ObjectIdentifierzvalue must be bytes)	rz   r   r{   bytesr)   rv   rs   rt   ru   )r   r   valuer   r   r   add_attribute  s    

z.CertificateSigningRequestBuilder.add_attributec                 C   s(   t |}| jdkrtd|| ||S )zF
        Signs the request using the requestor's private key.
        Nz/A CertificateSigningRequest must have a subject)r   rt   r"   Zcreate_x509_csrr   Zprivate_keyrC   r8   r   r   r   sign  s    
z%CertificateSigningRequestBuilder.sign)N)r   r   r    r   rw   r   r   r   r   r   r   r   rs     s
   rs   c                   @   sf   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dZdS )CertificateBuilderNc                 C   s6   t j| _|| _|| _|| _|| _|| _|| _|| _	d S r   )
r2   r4   _version_issuer_namert   _public_key_serial_number_not_valid_before_not_valid_afterru   )r   issuer_namerw   rI   rF   rJ   rK   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.
        rx   N%The issuer name may only be set once.)rz   r   r{   r   r"   r   rt   r   r   r   r   ru   r|   r   r   r   r     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.
        rx   Nry   )rz   r   r{   rt   r"   r   r   r   r   r   r   ru   r|   r   r   r   rw     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.)rz   r   ZDSAPublicKeyr   ZRSAPublicKeyr	   ZEllipticCurvePublicKeyr
   ZEd25519PublicKeyr   ZEd448PublicKeyr{   r   r"   r   r   rt   r   r   r   ru   )r   keyr   r   r   rI      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.
        'Serial number must be of integral type.N'The serial number may only be set once.r   z%The serial number should be positive.   3The serial number should not be more than 159 bits.)rz   sixinteger_typesr{   r   r"   
bit_lengthr   r   rt   r   r   r   ru   r   numberr   r   r   rF   ?  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.
        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.)rz   r,   r{   r   r"   r1   _EARLIEST_UTC_TIMEr   r   r   rt   r   r   ru   r   r/   r   r   r   rJ   Z  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.
        r   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.)rz   r,   r{   r   r"   r1   r   r   r   r   rt   r   r   ru   r   r   r   r   rK   w  s2    
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.
        r~   )rz   r   r{   r   r   r&   ru   r   r   rt   r   r   r   r   r   r   r   r   r     s    

z CertificateBuilder.add_extensionc                 C   s   t |}| jdkrtd| jdkr,td| jdkr>td| jdkrPtd| jdkrbtd| jdkrtt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)	r   rt   r"   r   r   r   r   r   Zcreate_x509_certificater   r   r   r   r     s    





zCertificateBuilder.sign)N)r   r   r    r   r   rw   rI   rF   rJ   rK   r   r   r   r   r   r   r     s    
 r   c                   @   sR   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dZ	dS ) CertificateRevocationListBuilderNc                 C   s"   || _ || _|| _|| _|| _d S r   )r   _last_update_next_updateru   _revoked_certificates)r   r   rg   rf   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 )Nrx   r   )
rz   r   r{   r   r"   r   r   r   ru   r   )r   r   r   r   r   r     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 )Nr   !Last update may only be set once.8The last update date must be on or after 1950 January 1.z9The last update date must be before the next update date.)rz   r,   r{   r   r"   r1   r   r   r   r   ru   r   )r   rg   r   r   r   rg     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 )Nr   r   r   z8The next update date must be after the last update date.)rz   r,   r{   r   r"   r1   r   r   r   r   ru   r   )r   rf   r   r   r   rf     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.
        r~   )rz   r   r{   r   r   r&   ru   r   r   r   r   r   r   r   r   r   r     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)	rz   rq   r{   r   r   r   r   ru   r   )r   Zrevoked_certificater   r   r   add_revoked_certificate  s    

z8CertificateRevocationListBuilder.add_revoked_certificatec                 C   sL   t |}| jd krtd| jd kr,td| jd kr>t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)r   r   r"   r   r   Zcreate_x509_crlr   r   r   r   r   -  s    


z%CertificateRevocationListBuilder.sign)N)
r   r   r    r   r   rg   rf   r   r   r   r   r   r   r   r     s   
r   c                   @   s>   e Zd Zddg fddZdd Zdd Zdd	 Zdd
dZdS )RevokedCertificateBuilderNc                 C   s   || _ || _|| _d S r   )r   _revocation_dateru   )r   rF   rr   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 )Nr   r   r   z$The serial number should be positiver   r   )
rz   r   r   r{   r   r"   r   r   r   ru   r   r   r   r   rF   C  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 )Nr   z)The revocation date may only be set once.z7The revocation date must be on or after 1950 January 1.)
rz   r,   r{   r   r"   r1   r   r   r   ru   r   r   r   r   rr   U  s    
  z)RevokedCertificateBuilder.revocation_datec                 C   sD   t |tstdt|j||}t|| j t| j| j	| j|g S )Nr~   )
rz   r   r{   r   r   r&   ru   r   r   r   r   r   r   r   r   c  s    

z'RevokedCertificateBuilder.add_extensionc                 C   s6   t |}| 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)r   r   r"   r   Zcreate_x509_revoked_certificate)r   r8   r   r   r   buildo  s    

zRevokedCertificateBuilder.build)N)r   r   r    r   rF   rr   r   r   r   r   r   r   r   ;  s     
r   c                   C   s   t tddd? S )N   bigr   )r   Zint_from_bytesosurandomr   r   r   r   random_serial_number{  s    r   )N)N)N)N)N)N)5
__future__r   r   r   ra   r,   r   enumr   r   Zcryptographyr   Zcryptography.hazmat.backendsr   Z)cryptography.hazmat.primitives.asymmetricr   r	   r
   r   r   Zcryptography.x509.extensionsr   r   Zcryptography.x509.namer   Zcryptography.x509.oidr   r   	Exceptionr   r&   r)   r1   r2   r5   r9   r:   r;   r<   r=   r>   add_metaclassABCMetaobjectr@   rd   rn   rq   rs   r   r   r   r   r   r   r   r   <module>   sL   






i
j
R
A ^v@