o
    Vhi                  	   @  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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 Z.G dd de/Z0d9ddZ1d:ddZ2d;dd Z3G d!d" d"Z4G d#d$ d$Z5G d%d& d&ej6Z7G d'd( d(e/Z8ej9Z9G d)d* d*ej:d+Z;e;<ej; G d,d- d-e;Z=ej>Z>ej?Z?ej@Z@ejAZAejBZBejCZCejDZDejEZEejFZFG d.d/ d/ZGG d0d1 d1ZHG d2d3 d3ZIG d4d5 d5ZJd<d7d8ZKdS )=    )annotationsN)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__ j/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/cryptography/x509/base.pyr"   8      
zAttributeNotFound.__init__)r   r   r   r   r   r   __name__
__module____qualname__r"   __classcell__r&   r&   r$   r'   r   7       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)r/   r1   er&   r&   r'   _reject_duplicate_extension=   s
   r5   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.)r3   )r   r6   attr_oid_r&   r&   r'   _reject_duplicate_attributeG   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_timeQ   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"   `      
zAttribute.__init__c                 C     | j S r    )rK   r#   r&   r&   r'   r   j      zAttribute.oidc                 C  rN   r    )rL   rO   r&   r&   r'   rF   n   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__r      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__u   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   
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 )
Attributesr6   typing.Iterable[Attribute]r   r   c                 C  s   t || _d S r    )list_attributes)r#   r6   r&   r&   r'   r"      s   zAttributes.__init__rc   r   c                 C  s   d| j  dS )Nz<Attributes(rQ   )rc   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)r6   ra   r   r   r\   )r   r   r   rE   )
r*   r+   r,   r"   r   __len____iter____getitem__rR   re   r&   r&   r&   r'   r`      s
    

r`   c                   @  s   e Zd ZdZdZdS )Versionr      N)r*   r+   r,   v1v3r&   r&   r&   r'   ri      s    ri   c                      r   )
InvalidVersionr   r   parsed_versionrI   r   r   c                   r   r    )r!   r"   rn   )r#   r   rn   r$   r&   r'   r"      r(   zInvalidVersion.__init__)r   r   rn   rI   r   r   r)   r&   r&   r$   r'   rm      r.   rm   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  rp   )zH
        Returns the date of when this certificate was revoked.
        Nr&   rO   r&   r&   r'   revocation_date   rr   z"RevokedCertificate.revocation_datec                 C  rp   )zl
        Returns the date of when this certificate was revoked as a non-naive
        UTC datetime.
        Nr&   rO   r&   r&   r'   revocation_date_utc   rr   z&RevokedCertificate.revocation_date_utcr   c                 C  rp   )zW
        Returns an Extensions object containing a list of Revoked extensions.
        Nr&   rO   r&   r&   r'   r1      rr   zRevokedCertificate.extensionsNr]   r   r<   r   r   )
r*   r+   r,   r_   abcabstractmethodrq   rs   rt   r1   r&   r&   r&   r'   ro      s    ro   )	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 )_RawRevokedCertificaterq   rI   rs   r<   r1   r   c                 C  rJ   r    _serial_number_revocation_date_extensionsr#   rq   rs   r1   r&   r&   r'   r"      rM   z_RawRevokedCertificate.__init__r   c                 C  rN   r    )r|   rO   r&   r&   r'   rq      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.rj   )
stacklevel)warningswarnr   DeprecatedIn42r}   rO   r&   r&   r'   rs      s   z&_RawRevokedCertificate.revocation_datec                 C  s   | j jtjjdS )Nr=   )r}   rB   r@   timezoneutcrO   r&   r&   r'   rt      s   z*_RawRevokedCertificate.revocation_date_utcc                 C  rN   r    )r~   rO   r&   r&   r'   r1      rP   z!_RawRevokedCertificate.extensionsN)rq   rI   rs   r<   r1   r   r]   ru   rv   )	r*   r+   r,   r"   r_   rq   rs   rt   r1   r&   r&   r&   r'   rz      s    

	rz   c                   @  sV   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	d.ddd/d(d)ZdS )0 CertificateSigningRequestBuilderNsubject_nameName | Noner1   r2   r6   r7   c                 C  s   || _ || _|| _dS )zB
        Creates an empty X.509 certificate request (v1).
        N)_subject_namer~   rc   )r#   r   r1   r6   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   r3   r   r~   rc   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   r5   r~   r   r   rc   r#   r   r   r/   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:   rc   rF   r   r   r~   )r#   r   rF   r   tagr&   r&   r'   add_attribute&  s   


z.CertificateSigningRequestBuilder.add_attributersa_paddingprivate_keyr   	algorithm_AllowedHashTypes | Nonebackend
typing.Anyr   %padding.PSS | padding.PKCS1v15 | NoneCertificateSigningRequestc                C  sX   | j du r	td|dur$t|tjtjfstdt|tjs$t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 keys)r   r3   rW   r
   PSSPKCS1v15r   r   RSAPrivateKey	rust_x509create_x509_csrr#   r   r   r   r   r&   r&   r'   signF  s   
z%CertificateSigningRequestBuilder.sign)r   r   r1   r2   r6   r7   )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      s    

$r   c                   @  s   e Zd ZU ded< ddddddg fd9d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	d?dd-d@d7d8ZdS )ACertificateBuilderr2   r~   Nissuer_namer   r   
public_key CertificatePublicKeyTypes | Nonerq   
int | Nonenot_valid_beforedatetime.datetime | Nonenot_valid_afterr1   r   r   c                 C  s6   t j| _|| _|| _|| _|| _|| _|| _|| _	d S r    )
ri   rl   _version_issuer_namer   _public_keyr|   _not_valid_before_not_valid_afterr~   )r#   r   r   r   rq   r   r   r1   r&   r&   r'   r"   b  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   r3   r   r   r   r|   r   r   r~   r   r&   r&   r'   r   u  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   r3   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.)rW   r   DSAPublicKeyr   RSAPublicKeyr   EllipticCurvePublicKeyr	   Ed25519PublicKeyr   Ed448PublicKeyr   X25519PublicKeyr   X448PublicKeyr   r   r3   r   r   r   r|   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   r|   r3   
bit_lengthr   r   r   r   r   r   r~   r#   r   r&   r&   r'   rq     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   r3   rD   _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.)rW   r@   r   r   r3   rD   r   r   r   r   r   r   r|   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   r5   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   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	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   r3   r   r|   r   r   r   rW   r
   r   r   r   r   r   r   create_x509_certificater   r&   r&   r'   r   ,  s(   





zCertificateBuilder.sign)r   r   r   r   r   r   rq   r   r   r   r   r   r1   r2   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,   __annotations__r"   r   r   r   rq   r   r   r   r   r&   r&   r&   r'   r   _  s*   
 



%


 r   c                   @  sz   e Zd ZU ded< ded< dddg g fd.ddZd/ddZd0ddZd1ddZd2ddZd3d d!Z		d4dd"d5d,d-Z
dS )6 CertificateRevocationListBuilderr2   r~   list[RevokedCertificate]_revoked_certificatesNr   r   last_updater   next_updater1   revoked_certificatesc                 C  s"   || _ || _|| _|| _|| _d S r    )r   _last_update_next_updater~   r   )r#   r   r   r   r1   r   r&   r&   r'   r"   X  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   r3   r   r   r   r~   r   )r#   r   r&   r&   r'   r   f  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   r3   rD   r   r   r   r   r~   r   )r#   r   r&   r&   r'   r   u  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   r3   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   r5   r~   r   r   r   r   r   r   r&   r&   r'   r     s   
z.CertificateRevocationListBuilder.add_extensionrevoked_certificatero   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   ro   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   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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   r3   r   r   rW   r
   r   r   r   r   r   r   create_x509_crlr   r&   r&   r'   r     s   


z%CertificateRevocationListBuilder.sign)
r   r   r   r   r   r   r1   r2   r   r   )r   r   r   r   )r   r<   r   r   )r   r<   r   r   )r   r   r   rV   r   r   )r   ro   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   T  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 )$RevokedCertificateBuilderNrq   r   rs   r   r1   r2   c                 C  rJ   r    r{   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   r|   r3   r   r   r}   r~   r   r&   r&   r'   rq     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}   r3   rD   r   r   r|   r~   r   r&   r&   r'   rs     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   r5   r~   r   r|   r}   r   r&   r&   r'   r     s   
z'RevokedCertificateBuilder.add_extensionr   r   ro   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|   r3   r}   rz   r   r~   )r#   r   r&   r&   r'   build   s   

zRevokedCertificateBuilder.build)rq   r   rs   r   r1   r2   )r   rI   r   r   )r;   r<   r   r   )r   r   r   rV   r   r   r    )r   r   r   ro   )r*   r+   r,   r"   rq   rs   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_number.  rS   r   )r/   r0   r1   r2   r   r   )r   r   r6   r7   r   r   )r;   r<   r   r<   r]   )L
__future__r   rw   r@   r   typingr   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   r5   r:   rD   rE   r`   Enumri   rm   r   ABCMetaro   registerrz   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>   sl   (





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