U
    /je                  	   @  s  d dl mZ d dlZd dlZd dlZd dl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mZmZ d dlmZmZ d d	lmZmZmZ d d
lmZm Z  d dl!m"Z" edddZ#ej$ej%ej&ej'ej(ej)ej*ej+ej,f Z-G dd de.Z/ddddddZ0ddddddZ1dddddZ2G dd  d Z3G d!d" d"Z4G d#d$ d$ej5Z6G d%d& d&e.Z7ej8Z8ej9Z9ej:Z:ej;Z;ej<Z<ej=Z=ej>Z>ej?Z?ej@Z@ejAZAejBZBG d'd( d(ZCG d)d* d*ZDG d+d, d,ZEG d-d. d.ZFd/d0d1d2ZGdS )3    )annotationsN)Iterable)utils)x509)hashes)dsaeced448ed25519paddingrsax448x25519) CertificateIssuerPrivateKeyTypesCertificatePublicKeyTypes)	ExtensionExtensionType_make_sequence_methods)Name	_ASN1Type)ObjectIdentifieri     c                      s&   e Zd Zdddd fddZ  ZS )AttributeNotFoundstrr   None)msgoidreturnc                   s   t  | || _d S N)super__init__r   )selfr   r   	__class__ :/tmp/pip-unpacked-wheel-qzbw3lpx/cryptography/x509/base.pyr    6   s    zAttributeNotFound.__init____name__
__module____qualname__r    __classcell__r$   r$   r"   r%   r   5   s   r   zExtension[ExtensionType]list[Extension[ExtensionType]]r   )	extension
extensionsr   c                 C  s"   |D ]}|j | j krtdqd S )Nz$This extension has already been set.)r   
ValueError)r,   r-   er$   r$   r%   _reject_duplicate_extension;   s    r0   r   0list[tuple[ObjectIdentifier, bytes, int | None]])r   
attributesr   c                 C  s$   |D ]\}}}|| krt dqd S )Nz$This attribute has already been set.)r.   )r   r2   Zattr_oid_r$   r$   r%   _reject_duplicate_attributeE   s    r4   datetime.datetimetimer   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)r8   	utcoffsetdatetime	timedeltareplace)r7   offsetr$   r$   r%   _convert_to_naive_utc_timeO   s
    
r>   c                   @  sx   e Zd ZejjfdddddddZeddd	d
ZeddddZddddZ	dddddZ
ddddZdS )	Attributer   bytesintr   )r   value_typer   c                 C  s   || _ || _|| _d S r   )_oid_valuerC   )r!   r   rB   rC   r$   r$   r%   r    ^   s    zAttribute.__init__r   c                 C  s   | j S r   )rD   r!   r$   r$   r%   r   h   s    zAttribute.oidc                 C  s   | j S r   )rE   rG   r$   r$   r%   rB   l   s    zAttribute.valuer   c                 C  s   d| j  d| jdS )Nz<Attribute(oid=z, value=)>)r   rB   rG   r$   r$   r%   __repr__p   s    zAttribute.__repr__objectbool)otherr   c                 C  s2   t |tstS | j|jko0| j|jko0| j|jkS r   )
isinstancer?   NotImplementedr   rB   rC   )r!   rL   r$   r$   r%   __eq__s   s    


zAttribute.__eq__c                 C  s   t | j| j| jfS r   )hashr   rB   rC   rG   r$   r$   r%   __hash__}   s    zAttribute.__hash__N)r'   r(   r)   r   Z
UTF8StringrB   r    propertyr   rI   rO   rQ   r$   r$   r$   r%   r?   ]   s   

r?   c                   @  sH   e Zd ZdddddZed\ZZZddd	d
ZdddddZ	dS )
AttributeszIterable[Attribute]r   )r2   r   c                 C  s   t || _d S r   )list_attributes)r!   r2   r$   r$   r%   r       s    zAttributes.__init__rU   r   rF   c                 C  s   d| j  dS )Nz<Attributes(rH   )rU   rG   r$   r$   r%   rI      s    zAttributes.__repr__r   r?   )r   r   c                 C  s2   | D ]}|j |kr|  S qtd| d|d S )NzNo z attribute was found)r   r   )r!   r   attrr$   r$   r%   get_attribute_for_oid   s    

z Attributes.get_attribute_for_oidN)
r'   r(   r)   r    r   __len____iter____getitem__rI   rW   r$   r$   r$   r%   rS      s   rS   c                   @  s   e Zd ZdZdZdS )Versionr      N)r'   r(   r)   Zv1v3r$   r$   r$   r%   r[      s   r[   c                      s&   e Zd Zdddd fddZ  ZS )InvalidVersionr   rA   r   )r   parsed_versionr   c                   s   t  | || _d S r   )r   r    r_   )r!   r   r_   r"   r$   r%   r       s    zInvalidVersion.__init__r&   r$   r$   r"   r%   r^      s   r^   c                	   @  s   e Zd Zdg g fddddddZdd d	d
dZddd dddZdddddd dddZd"ddddddddddd d!ZdS )# CertificateSigningRequestBuilderNName | Noner+   r1   )subject_namer-   r2   c                 C  s   || _ || _|| _dS )zB
        Creates an empty X.509 certificate request (v1).
        N)_subject_name_extensionsrU   )r!   rb   r-   r2   r$   r$   r%   r       s    	z)CertificateSigningRequestBuilder.__init__r   namer   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.)rM   r   	TypeErrorrc   r.   r`   rd   rU   r!   rf   r$   r$   r%   rb      s    

  z-CertificateSigningRequestBuilder.subject_namer   rK   extvalcriticalr   c                 C  sD   t |tstdt|j||}t|| j t| j| j|f| j	S )zE
        Adds an X.509 extension to the certificate request.
        "extension must be an ExtensionType)
rM   r   ri   r   r   r0   rd   r`   rc   rU   r!   rl   rm   r,   r$   r$   r%   add_extension   s    

z.CertificateSigningRequestBuilder.add_extension)_tagr   r@   z_ASN1Type | None)r   rB   rq   r   c                C  s|   t |tstdt |ts$td|dk	r>t |ts>tdt|| j |dk	rZ|j}nd}t| j	| j
| j|||ffS )zK
        Adds an X.509 attribute with an OID and associated value.
        zoid must be an ObjectIdentifierzvalue must be bytesNztag must be _ASN1Type)rM   r   ri   r@   r   r4   rU   rB   r`   rc   rd   )r!   r   rB   rq   tagr$   r$   r%   add_attribute   s    


z.CertificateSigningRequestBuilder.add_attributersa_paddingecdsa_deterministicr   _AllowedHashTypes | None
typing.Any%padding.PSS | padding.PKCS1v15 | Nonebool | NoneCertificateSigningRequestprivate_key	algorithmbackendru   rv   r   c                C  sv   | j dkrtd|dk	rHt|tjtjfs4tdt|tjsHtd|dk	rdt|t	j
sdtdt| ||||S )zF
        Signs the request using the requestor's private key.
        Nz/A CertificateSigningRequest must have a subjectPadding must be PSS or PKCS1v15&Padding is only supported for RSA keys1Deterministic ECDSA is only supported for EC keys)rc   r.   rM   r   PSSPKCS1v15ri   r   RSAPrivateKeyr   EllipticCurvePrivateKey	rust_x509Zcreate_x509_csrr!   r}   r~   r   ru   rv   r$   r$   r%   sign   s&    
z%CertificateSigningRequestBuilder.sign)N)r'   r(   r)   r    rb   rp   rs   r   r$   r$   r$   r%   r`      s   $ r`   c                
   @  s   e Zd ZU ded< ddddddg fddddddddd	d
dZdd dddZdd dddZdd dddZdd dddZdd dddZ	dd dddZ
d d!d d"d#d$Zd/ddd%d&d'd(d)d*d+d,d-d.ZdS )0CertificateBuilderr+   rd   Nra   z CertificatePublicKeyTypes | None
int | Nonedatetime.datetime | Noner   )issuer_namerb   
public_keyserial_numbernot_valid_beforenot_valid_afterr-   r   c                 C  s6   t j| _|| _|| _|| _|| _|| _|| _|| _	d S r   )
r[   r]   _version_issuer_namerc   _public_key_serial_number_not_valid_before_not_valid_afterrd   )r!   r   rb   r   r   r   r   r-   r$   r$   r%   r    '  s    
zCertificateBuilder.__init__r   re   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.
        rg   N%The issuer name may only be set once.)rM   r   ri   r   r.   r   rc   r   r   r   r   rd   rj   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.
        rg   Nrh   )rM   r   ri   rc   r.   r   r   r   r   r   r   rd   rj   r$   r$   r%   rb   L  s    

zCertificateBuilder.subject_namer   )keyr   c              	   C  s`   t |tjtjtjtjt	j
tjtjfs.td| jdk	r@tdt| j| j|| j| j| j| jS )zT
        Sets the requestor's public key (as found in the signing request).
        zExpecting one of DSAPublicKey, RSAPublicKey, EllipticCurvePublicKey, Ed25519PublicKey, Ed448PublicKey, X25519PublicKey, or X448PublicKey.Nz$The public key may only be set once.)rM   r   ZDSAPublicKeyr   ZRSAPublicKeyr   ZEllipticCurvePublicKeyr
   ZEd25519PublicKeyr	   ZEd448PublicKeyr   ZX25519PublicKeyr   ZX448PublicKeyri   r   r.   r   r   rc   r   r   r   rd   )r!   r   r$   r$   r%   r   ^  s2    
zCertificateBuilder.public_keyrA   numberr   c                 C  sh   t |tstd| jdk	r$td|dkr4td| dkrHt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.)rM   rA   ri   r   r.   
bit_lengthr   r   rc   r   r   r   rd   r!   r   r$   r$   r%   r     s&    

z CertificateBuilder.serial_numberr5   r6   c                 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.)rM   r:   ri   r   r.   r>   _EARLIEST_UTC_TIMEr   r   r   rc   r   r   rd   r!   r7   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.
        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.)rM   r:   ri   r   r.   r>   r   r   r   r   rc   r   r   rd   r   r$   r$   r%   r     s2    
z"CertificateBuilder.not_valid_afterr   rK   rk   c              	   C  sT   t |tstdt|j||}t|| j t| j| j	| j
| j| j| j| j|fS )z=
        Adds an X.509 extension to the certificate.
        rn   )rM   r   ri   r   r   r0   rd   r   r   rc   r   r   r   r   ro   r$   r$   r%   rp     s    

z CertificateBuilder.add_extensionrt   r   rw   rx   ry   rz   Certificater|   c                C  s   | 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|dk	rt|tj	tj
fstdt|tjstd	|dk	rt|tjstd
t| ||||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 keyr   r   r   )rc   r.   r   r   r   r   r   rM   r   r   r   ri   r   r   r   r   r   Zcreate_x509_certificater   r$   r$   r%   r     s:    





zCertificateBuilder.sign)N)r'   r(   r)   __annotations__r    r   rb   r   r   r   r   rp   r   r$   r$   r$   r%   r   $  s*   
% r   c                	   @  s   e Zd ZU ded< ded< dddg g fddddddd	d
Zdd dddZdd dddZdd dddZddd dddZdd dddZ	d)dddd d!d"d#d$d%d&d'd(Z
dS )* CertificateRevocationListBuilderr+   rd   zlist[RevokedCertificate]_revoked_certificatesNra   r   )r   last_updatenext_updater-   revoked_certificatesc                 C  s"   || _ || _|| _|| _|| _d S r   )r   _last_update_next_updaterd   r   )r!   r   r   r   r-   r   r$   r$   r%   r    '  s
    z)CertificateRevocationListBuilder.__init__r   )r   r   c                 C  s<   t |tstd| jd k	r$tdt|| j| j| j| j	S )Nrg   r   )
rM   r   ri   r   r.   r   r   r   rd   r   )r!   r   r$   r$   r%   r   5  s    

z,CertificateRevocationListBuilder.issuer_namer5   )r   r   c                 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.)rM   r:   ri   r   r.   r>   r   r   r   r   rd   r   )r!   r   r$   r$   r%   r   D  s(    
z,CertificateRevocationListBuilder.last_update)r   r   c                 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.)rM   r:   ri   r   r.   r>   r   r   r   r   rd   r   )r!   r   r$   r$   r%   r   \  s(    
z,CertificateRevocationListBuilder.next_updater   rK   rk   c                 C  sL   t |tstdt|j||}t|| j t| j| j	| j
| j|f| jS )zM
        Adds an X.509 extension to the certificate revocation list.
        rn   )rM   r   ri   r   r   r0   rd   r   r   r   r   r   ro   r$   r$   r%   rp   t  s    

z.CertificateRevocationListBuilder.add_extensionRevokedCertificate)revoked_certificater   c                 C  s2   t |tstdt| j| j| j| j| j|fS )z8
        Adds a revoked certificate to the CRL.
        z)Must be an instance of RevokedCertificate)	rM   r   ri   r   r   r   r   rd   r   )r!   r   r$   r$   r%   add_revoked_certificate  s    

z8CertificateRevocationListBuilder.add_revoked_certificatert   r   rw   rx   ry   rz   CertificateRevocationListr|   c                C  s   | j d krtd| jd kr$td| jd kr6td|d k	rlt|tjtjfsXtdt|t	j
sltd|d k	rt|tjstdt| ||||S )NzA CRL must have an issuer namez"A CRL must have a last update timez"A CRL must have a next update timer   r   r   )r   r.   r   r   rM   r   r   r   ri   r   r   r   r   r   Zcreate_x509_crlr   r$   r$   r%   r     s.    	


z%CertificateRevocationListBuilder.sign)N)r'   r(   r)   r   r    r   r   r   rp   r   r   r$   r$   r$   r%   r   #  s$   
 r   c                   @  sj   e Zd Zddg fddddddZdd d	d
dZdd dddZddd dddZddddddZdS )RevokedCertificateBuilderNr   r   r+   )r   revocation_dater-   c                 C  s   || _ || _|| _d S r   )r   _revocation_daterd   )r!   r   r   r-   r$   r$   r%   r      s    z"RevokedCertificateBuilder.__init__rA   r   c                 C  sX   t |tstd| jd k	r$td|dkr4td| dkrHtdt|| j| jS )Nr   r   r   z$The serial number should be positiver   r   )	rM   rA   ri   r   r.   r   r   r   rd   r   r$   r$   r%   r     s    

  z'RevokedCertificateBuilder.serial_numberr5   r6   c                 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.)
rM   r:   ri   r   r.   r>   r   r   r   rd   r   r$   r$   r%   r     s    
  z)RevokedCertificateBuilder.revocation_dater   rK   rk   c                 C  sD   t |tstdt|j||}t|| j t| j| j	| j|fS )Nrn   )
rM   r   ri   r   r   r0   rd   r   r   r   ro   r$   r$   r%   rp     s    

z'RevokedCertificateBuilder.add_extensionrx   r   )r   r   c                 C  s.   | j d krtd| jd kr$tdt| S )Nz/A revoked certificate must have a serial numberz1A revoked certificate must have a revocation date)r   r.   r   r   Zcreate_revoked_certificate)r!   r   r$   r$   r%   build  s    

zRevokedCertificateBuilder.build)N)r'   r(   r)   r    r   r   rp   r   r$   r$   r$   r%   r     s   
r   rA   rF   c                   C  s   t tddd? S )N   bigr   )rA   
from_bytesosurandomr$   r$   r$   r%   random_serial_number  s    r   )H
__future__r   r:   r   typingcollections.abcr   Zcryptographyr   Z"cryptography.hazmat.bindings._rustr   r   Zcryptography.hazmat.primitivesr   Z)cryptography.hazmat.primitives.asymmetricr   r   r	   r
   r   r   r   r   Z/cryptography.hazmat.primitives.asymmetric.typesr   r   Zcryptography.x509.extensionsr   r   r   Zcryptography.x509.namer   r   Zcryptography.x509.oidr   r   UnionSHA224SHA256SHA384SHA512ZSHA3_224ZSHA3_256ZSHA3_384ZSHA3_512Z_AllowedHashTypes	Exceptionr   r0   r4   r>   r?   rS   Enumr[   r^   r   r   r   r{   Zload_pem_x509_certificateZload_der_x509_certificateZload_pem_x509_certificatesZload_pem_x509_csrZload_der_x509_csrZload_pem_x509_crlZload_der_x509_crlr`   r   r   r   r   r$   r$   r$   r%   <module>   sf   (


$p   E