o
    Th]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__ V/home/air/segue/gpt/backup/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   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
   
rE   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_valuerI   )r$   r   rG   rI   r'   r'   r(   r#   a      
zAttribute.__init__c                 C     | j S r!   )rL   r$   r'   r'   r(   r   k      zAttribute.oidc                 C  rO   r!   )rM   rP   r'   r'   r(   rG   o   rQ   zAttribute.valuer   c                 C  s   d| j  d| jdS )Nz<Attribute(oid=z, value=)>)r   rG   rP   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!   )
isinstancerF   NotImplementedr   rG   rI   )r$   rU   r'   r'   r(   __eq__v   s   


zAttribute.__eq__c                 C  s   t | j| j| jfS r!   )hashr   rG   rI   rP   r'   r'   r(   __hash__   s   zAttribute.__hash__N)r   r   rG   rH   rI   rJ   r   r   )r   r   )r   rH   r   r   )rU   rV   r   rW   r   rJ   )r+   r,   r-   r   
UTF8StringrG   r#   propertyr   rS   rZ   r\   r'   r'   r'   r(   rF   `   s    



rF   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__rd   r   c                 C  s   d| j  dS )Nz<Attributes(rR   )rd   rP   r'   r'   r(   rS      s   zAttributes.__repr__r   r   rF   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   rb   r   r   r]   )r   r   r   rF   )
r+   r,   r-   r#   r   __len____iter____getitem__rS   rf   r'   r'   r'   r(   ra      s
    

ra   c                   @  s   e Zd ZdZdZdS )Versionr      N)r+   r,   r-   v1v3r'   r'   r'   r(   rj      s    rj   c                      r   )
InvalidVersionr   r   parsed_versionrJ   r   r   c                   r    r!   )r"   r#   ro   )r$   r   ro   r%   r'   r(   r#      r)   zInvalidVersion.__init__)r   r   ro   rJ   r   r   r*   r'   r'   r%   r(   rn      r/   rn   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   rJ   c                 C     dS )zG
        Returns the serial number of the revoked certificate.
        Nr'   rP   r'   r'   r(   serial_number       z RevokedCertificate.serial_numberr=   c                 C  rq   )zH
        Returns the date of when this certificate was revoked.
        Nr'   rP   r'   r'   r(   revocation_date   rs   z"RevokedCertificate.revocation_datec                 C  rq   )zl
        Returns the date of when this certificate was revoked as a non-naive
        UTC datetime.
        Nr'   rP   r'   r'   r(   revocation_date_utc   rs   z&RevokedCertificate.revocation_date_utcr   c                 C  rq   )zW
        Returns an Extensions object containing a list of Revoked extensions.
        Nr'   rP   r'   r'   r(   r2      rs   zRevokedCertificate.extensionsNr^   r   r=   r   r   )
r+   r,   r-   r`   abcabstractmethodrr   rt   ru   r2   r'   r'   r'   r(   rp      s    rp   )	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 )_RawRevokedCertificaterr   rJ   rt   r=   r2   r   c                 C  rK   r!   _serial_number_revocation_date_extensionsr$   rr   rt   r2   r'   r'   r(   r#      rN   z_RawRevokedCertificate.__init__r   c                 C  rO   r!   )r}   rP   r'   r'   r(   rr      rQ   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.rk   )
stacklevel)warningswarnr   DeprecatedIn42r~   rP   r'   r'   r(   rt      s   z&_RawRevokedCertificate.revocation_datec                 C  s   | j jtjjdS )Nr>   )r~   rC   rA   timezoneutcrP   r'   r'   r(   ru      s   z*_RawRevokedCertificate.revocation_date_utcc                 C  rO   r!   )r   rP   r'   r'   r(   r2      rQ   z!_RawRevokedCertificate.extensionsN)rr   rJ   rt   r=   r2   r   r^   rv   rw   )	r+   r,   r-   r#   r`   rr   rt   ru   r2   r'   r'   r'   r(   r{      s    

	r{   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   rd   )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.)rX   r   	TypeErrorr   r4   r   r   rd   r$   r   r'   r'   r(   r   	  s   


z-CertificateSigningRequestBuilder.subject_nameextvalr   criticalrW   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)
rX   r   r   r   r   r6   r   r   r   rd   r$   r   r   r0   r'   r'   r(   add_extension  s   
z.CertificateSigningRequestBuilder.add_extension)_tagr   r   rG   rH   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)rX   r   r   rH   r   r;   rd   rG   r   r   r   )r$   r   rG   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   rX   r   PSSPKCS1v15r   r   RSAPrivateKeyr   EllipticCurvePrivateKey	rust_x509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   rW   r   r   )r   r   rG   rH   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 | Nonerr   
int | Nonenot_valid_beforedatetime.datetime | Nonenot_valid_afterr2   r   r   c                 C  s6   t j| _|| _|| _|| _|| _|| _|| _|| _	d S r!   )
rj   rm   _version_issuer_namer   _public_keyr}   _not_valid_before_not_valid_afterr   )r$   r   r   r   rr   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.)rX   r   r   r   r4   r   r   r   r}   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   )rX   r   r   r   r4   r   r   r   r}   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.)rX   r   DSAPublicKeyr   RSAPublicKeyr   EllipticCurvePublicKeyr
   Ed25519PublicKeyr	   Ed448PublicKeyr   X25519PublicKeyr   X448PublicKeyr   r   r4   r   r   r   r}   r   r   r   )r$   r   r'   r'   r(   r     s2   
zCertificateBuilder.public_keynumberrJ   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.)rX   rJ   r   r}   r4   
bit_lengthr   r   r   r   r   r   r   r$   r   r'   r'   r(   rr     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.)rX   rA   r   r   r4   rE   _EARLIEST_UTC_TIMEr   r   r   r   r   r}   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.)rX   rA   r   r   r4   rE   r   r   r   r   r   r   r}   r   r   r'   r'   r(   r     s.   


z"CertificateBuilder.not_valid_afterr   r   r   rW   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   )rX   r   r   r   r   r6   r   r   r   r   r   r}   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   r}   r   r   r   rX   r   r   r   r   r   r   r   r   r   create_x509_certificater   r'   r'   r(   r   7  s:   





zCertificateBuilder.sign)r   r   r   r   r   r   rr   r   r   r   r   r   r2   r3   r   r   )r   r   r   r   )r   r   r   r   )r   rJ   r   r   )r<   r=   r   r   )r   r   r   rW   r   r   r!   )r   r   r   r   r   r   r   r   r   r   r   r   )r+   r,   r-   __annotations__r#   r   r   r   rr   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   )
rX   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.)rX   rA   r   r   r4   rE   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.)rX   rA   r   r   r4   rE   r   r   r   r   r   r   )r$   r   r'   r'   r(   r     s(   
z,CertificateRevocationListBuilder.next_updater   r   r   rW   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   )rX   r   r   r   r   r6   r   r   r   r   r   r   r   r'   r'   r(   r     s   
z.CertificateRevocationListBuilder.add_extensionrevoked_certificaterp   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)	rX   rp   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   rX   r   r   r   r   r   r   r   r   r   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   rW   r   r   )r   rp   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 )$RevokedCertificateBuilderNrr   r   rt   r   r2   r3   c                 C  rK   r!   r|   r   r'   r'   r(   r#     rN   z"RevokedCertificateBuilder.__init__r   rJ   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   )	rX   rJ   r   r}   r4   r   r   r~   r   r   r'   r'   r(   rr     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.)
rX   rA   r   r~   r4   rE   r   r   r}   r   r   r'   r'   r(   rt   #  s   

z)RevokedCertificateBuilder.revocation_dater   r   r   rW   c                 C  sF   t |ts	tdt|j||}t|| j t| j| j	g | j|S )Nr   )
rX   r   r   r   r   r6   r   r   r}   r~   r   r'   r'   r(   r   3  s   
z'RevokedCertificateBuilder.add_extensionr   r   rp   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)r}   r4   r~   r{   r   r   )r$   r   r'   r'   r(   buildA  s   

zRevokedCertificateBuilder.build)rr   r   rt   r   r2   r3   )r   rJ   r   r   )r<   r=   r   r   )r   r   r   rW   r   r   r!   )r   r   r   rp   )r+   r,   r-   r#   rr   rt   r   r   r'   r'   r'   r(   r     s    



r   rJ   c                   C  s   t tddd? S )N   bigr   )rJ   
from_bytesosurandomr'   r'   r'   r(   random_serial_numberO  rT   r   )r0   r1   r2   r3   r   r   )r   r   r7   r8   r   r   )r<   r=   r   r=   r^   )N
__future__r   rx   rA   r   typingr   collections.abcr   cryptographyr   "cryptography.hazmat.bindings._rustr   r   cryptography.hazmat.primitivesr   )cryptography.hazmat.primitives.asymmetricr   r   r	   r
   r   r   r   r   /cryptography.hazmat.primitives.asymmetric.typesr   r   cryptography.x509.extensionsr   r   r   r   cryptography.x509.namer   r   cryptography.x509.oidr   r   UnionSHA224SHA256SHA384SHA512SHA3_224SHA3_256SHA3_384SHA3_512_AllowedHashTypes	Exceptionr   r6   r;   rE   rF   ra   Enumrj   rn   r   ABCMetarp   registerr{   r   r   load_pem_x509_certificateload_der_x509_certificateload_pem_x509_certificatesload_pem_x509_csrload_der_x509_csrload_pem_x509_crlload_der_x509_crlr   r   r   r   r   r'   r'   r'   r(   <module>   sp   (





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