o
    bi]m                  	   @  s&  d dl mZ d dlZd dl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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 Z0G dd de1Z2d:ddZ3d;ddZ4d<d d!Z5G d"d# d#Z6G d$d% d%Z7G d&d' d'e
j8Z9G d(d) d)e1Z:ej;Z;G d*d+ d+ej<d,Z=e=>ej= G d-d. d.e=Z?ej@Z@ejAZAejBZBejCZCejDZDejEZEejFZFejGZGejHZHG d/d0 d0ZIG d1d2 d2ZJG d3d4 d4ZKG d5d6 d6ZLd=d8d9ZMdS )>    )annotationsN)Iterable)utils)x509)hashes)dsaeced448ed25519paddingrsax448x25519) CertificateIssuerPrivateKeyTypesCertificatePublicKeyTypes)	Extension
ExtensionsExtensionType_make_sequence_methods)Name	_ASN1Type)ObjectIdentifieri     c                         e Zd Zd	 fddZ  ZS )
AttributeNotFoundmsgstroidr   returnNonec                      t  | || _d S N)super__init__r   )selfr   r   	__class__ P/home/kim/smarthome/.venv/lib/python3.10/site-packages/cryptography/x509/base.pyr#   9      
zAttributeNotFound.__init__)r   r   r   r   r   r   __name__
__module____qualname__r#   __classcell__r'   r'   r%   r(   r   8       r   	extensionExtension[ExtensionType]
extensionslist[Extension[ExtensionType]]r   r   c                 C  s"   |D ]}|j | j krtdqd S )Nz$This extension has already been set.)r   
ValueError)r0   r2   er'   r'   r(   _reject_duplicate_extension>   s
   r6   r   r   
attributes0list[tuple[ObjectIdentifier, bytes, int | None]]c                 C  s$   |D ]\}}}|| krt dqd S )Nz$This attribute has already been set.)r4   )r   r7   Zattr_oid_r'   r'   r(   _reject_duplicate_attributeH   s
   r:   timedatetime.datetimec                 C  s6   | j dur|  }|r|nt }| jdd| S | 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)r;   offsetr'   r'   r(   _convert_to_naive_utc_timeR   s
   
rD   c                   @  sX   e Zd Zejjfdd	d
ZedddZedddZdddZ	dddZ
dddZdS ) 	Attributer   r   valuebytes_typeintr   r   c                 C     || _ || _|| _d S r!   )_oid_valuerH   )r$   r   rF   rH   r'   r'   r(   r#   a      
zAttribute.__init__c                 C     | j S r!   )rK   r$   r'   r'   r(   r   k      zAttribute.oidc                 C  rN   r!   )rL   rO   r'   r'   r(   rF   o   rP   zAttribute.valuer   c                 C  s   d| j  d| jdS )Nz<Attribute(oid=z, value=)>)r   rF   rO   r'   r'   r(   __repr__s      zAttribute.__repr__otherobjectboolc                 C  s2   t |tstS | j|jko| j|jko| j|jkS r!   )
isinstancerE   NotImplementedr   rF   rH   )r$   rT   r'   r'   r(   __eq__v   s   


zAttribute.__eq__c                 C  s   t | j| j| jfS r!   )hashr   rF   rH   rO   r'   r'   r(   __hash__   s   zAttribute.__hash__N)r   r   rF   rG   rH   rI   r   r   )r   r   )r   rG   r   r   )rT   rU   r   rV   r   rI   )r+   r,   r-   r   Z
UTF8StringrF   r#   propertyr   rR   rY   r[   r'   r'   r'   r(   rE   `   s    



rE   c                   @  s8   e Zd ZdddZed\ZZZdd	d
ZdddZ	dS )
Attributesr7   Iterable[Attribute]r   r   c                 C  s   t || _d S r!   )list_attributes)r$   r7   r'   r'   r(   r#      s   zAttributes.__init__rb   r   c                 C  s   d| j  dS )Nz<Attributes(rQ   )rb   rO   r'   r'   r(   rR      s   zAttributes.__repr__r   r   rE   c                 C  s.   | D ]}|j |kr|  S qtd| d|)NzNo z attribute was found)r   r   )r$   r   attrr'   r'   r(   get_attribute_for_oid   s
   
z Attributes.get_attribute_for_oidN)r7   r`   r   r   r\   )r   r   r   rE   )
r+   r,   r-   r#   r   __len____iter____getitem__rR   rd   r'   r'   r'   r(   r_      s
    

r_   c                   @  s   e Zd ZdZdZdS )Versionr      N)r+   r,   r-   Zv1v3r'   r'   r'   r(   rh      s    rh   c                      r   )
InvalidVersionr   r   parsed_versionrI   r   r   c                   r    r!   )r"   r#   rl   )r$   r   rl   r%   r'   r(   r#      r)   zInvalidVersion.__init__)r   r   rl   rI   r   r   r*   r'   r'   r%   r(   rk      r/   rk   c                   @  s\   e Zd ZeejdddZeejdddZeejddd	ZeejdddZ	dS )RevokedCertificater   rI   c                 C     dS )zG
        Returns the serial number of the revoked certificate.
        Nr'   rO   r'   r'   r(   serial_number       z RevokedCertificate.serial_numberr<   c                 C  rn   )zH
        Returns the date of when this certificate was revoked.
        Nr'   rO   r'   r'   r(   revocation_date   rp   z"RevokedCertificate.revocation_datec                 C  rn   )zl
        Returns the date of when this certificate was revoked as a non-naive
        UTC datetime.
        Nr'   rO   r'   r'   r(   revocation_date_utc   rp   z&RevokedCertificate.revocation_date_utcr   c                 C  rn   )zW
        Returns an Extensions object containing a list of Revoked extensions.
        Nr'   rO   r'   r'   r(   r2      rp   zRevokedCertificate.extensionsNr]   r   r<   r   r   )
r+   r,   r-   r^   abcabstractmethodro   rq   rr   r2   r'   r'   r'   r(   rm      s    rm   )	metaclassc                   @  sN   e Zd ZdddZedd
dZedddZedddZedddZdS )_RawRevokedCertificatero   rI   rq   r<   r2   r   c                 C  rJ   r!   _serial_number_revocation_date_extensionsr$   ro   rq   r2   r'   r'   r(   r#      rM   z_RawRevokedCertificate.__init__r   c                 C  rN   r!   )rz   rO   r'   r'   r(   ro      rP   z$_RawRevokedCertificate.serial_numberc                 C  s   t jdtjdd | jS )Nuk   Properties that return a naïve datetime object have been deprecated. Please switch to revocation_date_utc.ri   )
stacklevel)warningswarnr   ZDeprecatedIn42r{   rO   r'   r'   r(   rq      s   z&_RawRevokedCertificate.revocation_datec                 C  s   | j jtjjdS )Nr=   )r{   rB   r@   timezoneutcrO   r'   r'   r(   rr      s   z*_RawRevokedCertificate.revocation_date_utcc                 C  rN   r!   )r|   rO   r'   r'   r(   r2      rP   z!_RawRevokedCertificate.extensionsN)ro   rI   rq   r<   r2   r   r]   rs   rt   )	r+   r,   r-   r#   r^   ro   rq   rr   r2   r'   r'   r'   r(   rx      s    

	rx   c                   @  sX   e Zd Zdg g fd,dd	Zd-ddZd.ddZddd/ddZ	d0dddd1d*d+ZdS )2 CertificateSigningRequestBuilderNsubject_nameName | Noner2   r3   r7   r8   c                 C  s   || _ || _|| _dS )zB
        Creates an empty X.509 certificate request (v1).
        N)_subject_namer|   rb   )r$   r   r2   r7   r'   r'   r(   r#      s   	
z)CertificateSigningRequestBuilder.__init__namer   r   c                 C  s4   t |ts	td| jdu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.)rW   r   	TypeErrorr   r4   r   r|   rb   r$   r   r'   r'   r(   r   	  s   


z-CertificateSigningRequestBuilder.subject_nameextvalr   criticalrV   c                 C  sF   t |ts	tdt|j||}t|| j t| jg | j|| j	S )zE
        Adds an X.509 extension to the certificate request.
        "extension must be an ExtensionType)
rW   r   r   r   r   r6   r|   r   r   rb   r$   r   r   r0   r'   r'   r(   add_extension  s   
z.CertificateSigningRequestBuilder.add_extension)_tagr   r   rF   rG   r   _ASN1Type | Nonec                C  s~   t |ts	tdt |tstd|durt |tstdt|| j |dur-|j}nd}t| j	| j
g | j|||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)rW   r   r   rG   r   r:   rb   rF   r   r   r|   )r$   r   rF   r   tagr'   r'   r(   add_attribute'  s   


z.CertificateSigningRequestBuilder.add_attributersa_paddingecdsa_deterministicprivate_keyr   	algorithm_AllowedHashTypes | Nonebackend
typing.Anyr   %padding.PSS | padding.PKCS1v15 | Noner   bool | NoneCertificateSigningRequestc                C  sv   | j du r	td|dur$t|tjtjfstdt|tjs$td|dur2t|t	j
s2t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)r   r4   rW   r   PSSPKCS1v15r   r   RSAPrivateKeyr   EllipticCurvePrivateKey	rust_x509Zcreate_x509_csrr$   r   r   r   r   r   r'   r'   r(   signG  s&   
z%CertificateSigningRequestBuilder.sign)r   r   r2   r3   r7   r8   )r   r   r   r   )r   r   r   rV   r   r   )r   r   rF   rG   r   r   r   r   r!   )r   r   r   r   r   r   r   r   r   r   r   r   )r+   r,   r-   r#   r   r   r   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Zd<ddZd<ddZd=ddZd>dd Zd?d#d$Z	d?d%d&Z
d@d+d,Z	dAddd-dBd9d:ZdS )CCertificateBuilderr3   r|   Nissuer_namer   r   
public_key CertificatePublicKeyTypes | Nonero   
int | Nonenot_valid_beforedatetime.datetime | Nonenot_valid_afterr2   r   r   c                 C  s6   t j| _|| _|| _|| _|| _|| _|| _|| _	d S r!   )
rh   rj   _version_issuer_namer   _public_keyrz   _not_valid_before_not_valid_afterr|   )r$   r   r   r   ro   r   r   r2   r'   r'   r(   r#   n  s   

zCertificateBuilder.__init__r   r   c                 C  sD   t |ts	td| jdurtdt|| j| j| j| j	| j
| jS )z3
        Sets the CA's distinguished name.
        r   N%The issuer name may only be set once.)rW   r   r   r   r4   r   r   r   rz   r   r   r|   r   r'   r'   r(   r     s   

zCertificateBuilder.issuer_namec                 C  sD   t |ts	td| jdurtdt| j|| j| j| j	| j
| jS )z:
        Sets the requestor's distinguished name.
        r   Nr   )rW   r   r   r   r4   r   r   r   rz   r   r   r|   r   r'   r'   r(   r     s   

zCertificateBuilder.subject_name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u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.)rW   r   ZDSAPublicKeyr   ZRSAPublicKeyr   ZEllipticCurvePublicKeyr
   ZEd25519PublicKeyr	   ZEd448PublicKeyr   ZX25519PublicKeyr   ZX448PublicKeyr   r   r4   r   r   r   rz   r   r   r|   )r$   r   r'   r'   r(   r     s2   
zCertificateBuilder.public_keynumberrI   c                 C  sh   t |ts	td| jdurtd|dkrtd| dkr$t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.)rW   rI   r   rz   r4   
bit_lengthr   r   r   r   r   r   r|   r$   r   r'   r'   r(   ro     s&   

z CertificateBuilder.serial_numberr;   r<   c                 C  sz   t |tjs
td| jdurtdt|}|tk rtd| jdur-|| jkr-t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.)rW   r@   r   r   r4   rD   _EARLIEST_UTC_TIMEr   r   r   r   r   rz   r|   r$   r;   r'   r'   r(   r     s,   
z#CertificateBuilder.not_valid_beforec                 C  sz   t |tjs
td| jdurtdt|}|tk rtd| jdur-|| jk r-t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.)rW   r@   r   r   r4   rD   r   r   r   r   r   r   rz   r|   r   r'   r'   r(   r     s.   


z"CertificateBuilder.not_valid_afterr   r   r   rV   c              	   C  sV   t |ts	tdt|j||}t|| j t| j| j	| j
| j| j| jg | j|S )z=
        Adds an X.509 extension to the certificate.
        r   )rW   r   r   r   r   r6   r|   r   r   r   r   rz   r   r   r   r'   r'   r(   r   !  s   
z CertificateBuilder.add_extensionr   r   r   r   r   r   r   r   r   r   r   Certificatec                C  s   | j du r	td| jdu rtd| jdu rtd| jdu r$td| jdu r-td| jdu r6td|durQt|tj	tj
fsGtdt|tjsQtd	|dur_t|tjs_t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   )r   r4   r   rz   r   r   r   rW   r   r   r   r   r   r   r   r   r   Zcreate_x509_certificater   r'   r'   r(   r   7  s:   





zCertificateBuilder.sign)r   r   r   r   r   r   ro   r   r   r   r   r   r2   r3   r   r   )r   r   r   r   )r   r   r   r   )r   rI   r   r   )r;   r<   r   r   )r   r   r   rV   r   r   r!   )r   r   r   r   r   r   r   r   r   r   r   r   )r+   r,   r-   __annotations__r#   r   r   r   ro   r   r   r   r   r'   r'   r'   r(   r   k  s,   
 



%


r   c                   @  s|   e Zd ZU ded< ded< dddg g fd0ddZd1ddZd2ddZd3ddZd4ddZd5d d!Z		d6ddd"d7d.d/Z
dS )8 CertificateRevocationListBuilderr3   r|   list[RevokedCertificate]_revoked_certificatesNr   r   last_updater   next_updater2   revoked_certificatesc                 C  s"   || _ || _|| _|| _|| _d S r!   )r   _last_update_next_updater|   r   )r$   r   r   r   r2   r   r'   r'   r(   r#   n  s
   
z)CertificateRevocationListBuilder.__init__r   r   c                 C  s<   t |ts	td| jd urtdt|| j| j| j| j	S )Nr   r   )
rW   r   r   r   r4   r   r   r   r|   r   )r$   r   r'   r'   r(   r   |  s   

z,CertificateRevocationListBuilder.issuer_namer<   c                 C  sr   t |tjs
td| jd urtdt|}|tk rtd| jd ur-|| jkr-t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.)rW   r@   r   r   r4   rD   r   r   r   r   r|   r   )r$   r   r'   r'   r(   r     s(   
z,CertificateRevocationListBuilder.last_updatec                 C  sr   t |tjs
td| jd urtdt|}|tk rtd| jd ur-|| jk r-tdt| j	| j|| j
| jS )Nr   r   r   z8The next update date must be after the last update date.)rW   r@   r   r   r4   rD   r   r   r   r   r|   r   )r$   r   r'   r'   r(   r     s(   
z,CertificateRevocationListBuilder.next_updater   r   r   rV   c                 C  sN   t |ts	tdt|j||}t|| j t| j| j	| j
g | j|| jS )zM
        Adds an X.509 extension to the certificate revocation list.
        r   )rW   r   r   r   r   r6   r|   r   r   r   r   r   r   r'   r'   r(   r     s   
z.CertificateRevocationListBuilder.add_extensionrevoked_certificaterm   c                 C  s4   t |ts	tdt| j| j| j| jg | j|S )z8
        Adds a revoked certificate to the CRL.
        z)Must be an instance of RevokedCertificate)	rW   rm   r   r   r   r   r   r|   r   )r$   r   r'   r'   r(   add_revoked_certificate  s   
z8CertificateRevocationListBuilder.add_revoked_certificater   r   r   r   r   r   r   r   r   r   r   CertificateRevocationListc                C  s   | j d u r	td| jd u rtd| jd u rtd|d ur6t|tjtjfs,tdt|t	j
s6td|d urDt|tjsDt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   r4   r   r   rW   r   r   r   r   r   r   r   r   r   Zcreate_x509_crlr   r'   r'   r(   r     s.   
	

z%CertificateRevocationListBuilder.sign)
r   r   r   r   r   r   r2   r3   r   r   )r   r   r   r   )r   r<   r   r   )r   r<   r   r   )r   r   r   rV   r   r   )r   rm   r   r   r!   )r   r   r   r   r   r   r   r   r   r   r   r   )r+   r,   r-   r   r#   r   r   r   r   r   r   r'   r'   r'   r(   r   j  s&   
 




r   c                   @  sH   e Zd Zddg fddd	ZdddZd ddZd!ddZd"d#ddZdS )$RevokedCertificateBuilderNro   r   rq   r   r2   r3   c                 C  rJ   r!   ry   r}   r'   r'   r(   r#     rM   z"RevokedCertificateBuilder.__init__r   rI   r   c                 C  sX   t |ts	td| jd urtd|dkrtd| dkr$tdt|| j| jS )Nr   r   r   z$The serial number should be positiver   r   )	rW   rI   r   rz   r4   r   r   r{   r|   r   r'   r'   r(   ro     s   


z'RevokedCertificateBuilder.serial_numberr;   r<   c                 C  sN   t |tjs
td| jd u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.)
rW   r@   r   r{   r4   rD   r   r   rz   r|   r   r'   r'   r(   rq   #  s   

z)RevokedCertificateBuilder.revocation_dater   r   r   rV   c                 C  sF   t |ts	tdt|j||}t|| j t| j| j	g | j|S )Nr   )
rW   r   r   r   r   r6   r|   r   rz   r{   r   r'   r'   r(   r   3  s   
z'RevokedCertificateBuilder.add_extensionr   r   rm   c                 C  s:   | j d u r	td| jd u rtdt| j | jt| jS )Nz/A revoked certificate must have a serial numberz1A revoked certificate must have a revocation date)rz   r4   r{   rx   r   r|   )r$   r   r'   r'   r(   buildA  s   

zRevokedCertificateBuilder.build)ro   r   rq   r   r2   r3   )r   rI   r   r   )r;   r<   r   r   )r   r   r   rV   r   r   r!   )r   r   r   rm   )r+   r,   r-   r#   ro   rq   r   r   r'   r'   r'   r(   r     s    



r   rI   c                   C  s   t tddd? S )N   bigr   )rI   
from_bytesosurandomr'   r'   r'   r(   random_serial_numberO  rS   r   )r0   r1   r2   r3   r   r   )r   r   r7   r8   r   r   )r;   r<   r   r<   r]   )N
__future__r   ru   r@   r   typingr   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   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   r6   r:   rD   rE   r_   Enumrh   rk   r   ABCMetarm   registerrx   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>   sp   (





$ "p   I