o
    CZhE                  	   @  sp  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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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Z4dAdd Z5G d!d" d"Z6G d#d$ d$Z7G d%d& d&ej8Z9G d'd( d(e1Z:G d)d* d*ej;d+Z<e<=ej< G d,d- d-ej;d+Z>e>=ej> G d.d/ d/e>Z?G d0d1 d1ej;d+Z@e@=ej@ G d2d3 d3ej;d+ZAeA=ejA ejBZBejCZCejDZDejEZEejFZFejGZGejHZHG d4d5 d5ZIG d6d7 d7ZJG d8d9 d9ZKG d:d; d;ZLdBd=d>ZMdS )C    )annotationsN)utils)x509)hashesserialization)dsaeced448ed25519paddingrsax448x25519) CertificateIssuerPrivateKeyTypesCertificateIssuerPublicKeyTypes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__ M/var/www/html/lang_env/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)r1   r3   er(   r(   r)   _reject_duplicate_extension>   s
   r7   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.)r5   )r   r8   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
   
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   Z
UTF8StringrG   r$   propertyr   rS   r[   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 )
Attributesr8   typing.Iterable[Attribute]r   r    c                 C  s   t || _d S r"   )list_attributes)r%   r8   r(   r(   r)   r$      s   zAttributes.__init__rg   r   c                 C  s   d| j  dS )Nz<Attributes(rR   )rg   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)r8   re   r   r    r`   )r   r   r   rF   )
r,   r-   r.   r$   r   __len____iter____getitem__rS   ri   r(   r(   r(   r)   rd      s
    

rd   c                   @  s   e Zd ZdZdZdS )Versionr      N)r,   r-   r.   v1v3r(   r(   r(   r)   rm      s    rm   c                      r   )
InvalidVersionr   r   parsed_versionrJ   r   r    c                   r!   r"   )r#   r$   rr   )r%   r   rr   r&   r(   r)   r$      r*   zInvalidVersion.__init__)r   r   rr   rJ   r   r    r+   r(   r(   r&   r)   rq      r0   rq   c                   @  s  e Zd Zejd@ddZeejdAdd	ZeejdBddZejdCddZ	eejdDddZ
eejdDddZeejdDddZeejdDddZeejdEddZeejdEddZeejdFdd ZeejdGd"d#ZeejdHd%d&ZeejdId(d)ZeejdJd*d+ZeejdJd,d-ZeejdJd.d/ZejdKd3d4ZejdAd5d6ZejdLd9d:ZejdMd=d>Zd?S )NCertificate	algorithmhashes.HashAlgorithmr   rH   c                 C     dS z4
        Returns bytes using digest passed.
        Nr(   r%   rt   r(   r(   r)   fingerprint       zCertificate.fingerprintrJ   c                 C  rv   )z3
        Returns certificate serial number
        Nr(   rP   r(   r(   r)   serial_number   rz   zCertificate.serial_numberrm   c                 C  rv   )z1
        Returns the certificate version
        Nr(   rP   r(   r(   r)   version   rz   zCertificate.versionr   c                 C  rv   z(
        Returns the public key
        Nr(   rP   r(   r(   r)   
public_key   rz   zCertificate.public_keyr=   c                 C  rv   )z?
        Not before time (represented as UTC datetime)
        Nr(   rP   r(   r(   r)   not_valid_before   rz   zCertificate.not_valid_beforec                 C  rv   )zK
        Not before time (represented as a non-naive UTC datetime)
        Nr(   rP   r(   r(   r)   not_valid_before_utc   rz   z Certificate.not_valid_before_utcc                 C  rv   )z>
        Not after time (represented as UTC datetime)
        Nr(   rP   r(   r(   r)   not_valid_after   rz   zCertificate.not_valid_afterc                 C  rv   )zJ
        Not after time (represented as a non-naive UTC datetime)
        Nr(   rP   r(   r(   r)   not_valid_after_utc   rz   zCertificate.not_valid_after_utcr   c                 C  rv   )z1
        Returns the issuer name object.
        Nr(   rP   r(   r(   r)   issuer   rz   zCertificate.issuerc                 C  rv   z2
        Returns the subject name object.
        Nr(   rP   r(   r(   r)   subject   rz   zCertificate.subjecthashes.HashAlgorithm | Nonec                 C  rv   zt
        Returns a HashAlgorithm corresponding to the type of the digest signed
        in the certificate.
        Nr(   rP   r(   r(   r)   signature_hash_algorithm   rz   z$Certificate.signature_hash_algorithmr   c                 C  rv   zJ
        Returns the ObjectIdentifier of the signature algorithm.
        Nr(   rP   r(   r(   r)   signature_algorithm_oid   rz   z#Certificate.signature_algorithm_oid0None | padding.PSS | padding.PKCS1v15 | ec.ECDSAc                 C  rv   z=
        Returns the signature algorithm parameters.
        Nr(   rP   r(   r(   r)   signature_algorithm_parameters   rz   z*Certificate.signature_algorithm_parametersr   c                 C  rv   )z/
        Returns an Extensions object.
        Nr(   rP   r(   r(   r)   r3     rz   zCertificate.extensionsc                 C  rv   z.
        Returns the signature bytes.
        Nr(   rP   r(   r(   r)   	signature	  rz   zCertificate.signaturec                 C  rv   )zR
        Returns the tbsCertificate payload bytes as defined in RFC 5280.
        Nr(   rP   r(   r(   r)   tbs_certificate_bytes  rz   z!Certificate.tbs_certificate_bytesc                 C  rv   )zh
        Returns the tbsCertificate payload bytes with the SCT list extension
        stripped.
        Nr(   rP   r(   r(   r)   tbs_precertificate_bytes  rz   z$Certificate.tbs_precertificate_bytesrU   rV   rW   c                 C  rv   z"
        Checks equality.
        Nr(   rZ   r(   r(   r)   r[     rz   zCertificate.__eq__c                 C  rv   z"
        Computes a hash.
        Nr(   rP   r(   r(   r)   r]   %  rz   zCertificate.__hash__encodingserialization.Encodingc                 C  rv   )zB
        Serializes the certificate to PEM or DER format.
        Nr(   r%   r   r(   r(   r)   public_bytes+  rz   zCertificate.public_bytesr   r    c                 C  rv   )z
        This method verifies that certificate issuer name matches the
        issuer subject name and that the certificate is signed by the
        issuer's private key. No other validation is performed.
        Nr(   )r%   r   r(   r(   r)   verify_directly_issued_by1  rz   z%Certificate.verify_directly_issued_byNrt   ru   r   rH   rb   )r   rm   r   r   r   r=   r   r   r   r   r^   r   r   r   r   r_   ra   r   r   r   rH   )r   rs   r   r    )r,   r-   r.   abcabstractmethodry   rc   r{   r|   r~   r   r   r   r   r   r   r   r   r   r3   r   r   r   r[   r]   r   r   r(   r(   r(   r)   rs      st    rs   )	metaclassc                   @  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  rv   )zG
        Returns the serial number of the revoked certificate.
        Nr(   rP   r(   r(   r)   r{   ?  rz   z RevokedCertificate.serial_numberr=   c                 C  rv   )zH
        Returns the date of when this certificate was revoked.
        Nr(   rP   r(   r(   r)   revocation_dateF  rz   z"RevokedCertificate.revocation_datec                 C  rv   )zl
        Returns the date of when this certificate was revoked as a non-naive
        UTC datetime.
        Nr(   rP   r(   r(   r)   revocation_date_utcM  rz   z&RevokedCertificate.revocation_date_utcr   c                 C  rv   )zW
        Returns an Extensions object containing a list of Revoked extensions.
        Nr(   rP   r(   r(   r)   r3   U  rz   zRevokedCertificate.extensionsNrb   r   r   )
r,   r-   r.   rc   r   r   r{   r   r   r3   r(   r(   r(   r)   r   >  s    r   c                   @  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 )_RawRevokedCertificater{   rJ   r   r=   r3   r   c                 C  rK   r"   _serial_number_revocation_date_extensionsr%   r{   r   r3   r(   r(   r)   r$   b  rN   z_RawRevokedCertificate.__init__r   c                 C  rO   r"   )r   rP   r(   r(   r)   r{   l  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.rn   )
stacklevel)warningswarnr   ZDeprecatedIn42r   rP   r(   r(   r)   r   p  s   z&_RawRevokedCertificate.revocation_datec                 C  s   | j jtjjdS )Nr>   )r   rC   rA   timezoneutcrP   r(   r(   r)   r   z  s   z*_RawRevokedCertificate.revocation_date_utcc                 C  rO   r"   )r   rP   r(   r(   r)   r3   ~  rQ   z!_RawRevokedCertificate.extensionsN)r{   rJ   r   r=   r3   r   rb   r   r   )	r,   r-   r.   r$   rc   r{   r   r   r3   r(   r(   r(   r)   r   a  s    

	r   c                   @  s  e Zd ZejdFddZejdGd	d
ZejdHddZeejdIddZ	eejdJddZ
eejdKddZeejdLddZeejdMddZeejdMdd ZeejdNd"d#ZeejdNd$d%ZeejdOd'd(ZeejdPd)d*ZeejdPd+d,ZejdQd0d1ZejdRd2d3ZejdSd6d7ZejdTd:d7ZejdUd=d7ZejdVd?d@ZejdWdCdDZdES )XCertificateRevocationListr   r   r   rH   c                 C  rv   )z:
        Serializes the CRL to PEM or DER format.
        Nr(   r   r(   r(   r)   r     rz   z&CertificateRevocationList.public_bytesrt   ru   c                 C  rv   rw   r(   rx   r(   r(   r)   ry     rz   z%CertificateRevocationList.fingerprintr{   rJ   RevokedCertificate | Nonec                 C  rv   )zs
        Returns an instance of RevokedCertificate or None if the serial_number
        is not in the CRL.
        Nr(   )r%   r{   r(   r(   r)   (get_revoked_certificate_by_serial_number  rz   zBCertificateRevocationList.get_revoked_certificate_by_serial_numberr   c                 C  rv   r   r(   rP   r(   r(   r)   r     rz   z2CertificateRevocationList.signature_hash_algorithmr   c                 C  rv   r   r(   rP   r(   r(   r)   r     rz   z1CertificateRevocationList.signature_algorithm_oidr   c                 C  rv   r   r(   rP   r(   r(   r)   r     rz   z8CertificateRevocationList.signature_algorithm_parametersr   c                 C  rv   )zC
        Returns the X509Name with the issuer of this CRL.
        Nr(   rP   r(   r(   r)   r     rz   z CertificateRevocationList.issuerdatetime.datetime | Nonec                 C  rv   )z?
        Returns the date of next update for this CRL.
        Nr(   rP   r(   r(   r)   next_update  rz   z%CertificateRevocationList.next_updatec                 C  rv   )zc
        Returns the date of next update for this CRL as a non-naive UTC
        datetime.
        Nr(   rP   r(   r(   r)   next_update_utc  rz   z)CertificateRevocationList.next_update_utcr=   c                 C  rv   )z?
        Returns the date of last update for this CRL.
        Nr(   rP   r(   r(   r)   last_update  rz   z%CertificateRevocationList.last_updatec                 C  rv   )zc
        Returns the date of last update for this CRL as a non-naive UTC
        datetime.
        Nr(   rP   r(   r(   r)   last_update_utc  rz   z)CertificateRevocationList.last_update_utcr   c                 C  rv   )zS
        Returns an Extensions object containing a list of CRL extensions.
        Nr(   rP   r(   r(   r)   r3     rz   z$CertificateRevocationList.extensionsc                 C  rv   r   r(   rP   r(   r(   r)   r     rz   z#CertificateRevocationList.signaturec                 C  rv   )zO
        Returns the tbsCertList payload bytes as defined in RFC 5280.
        Nr(   rP   r(   r(   r)   tbs_certlist_bytes  rz   z,CertificateRevocationList.tbs_certlist_bytesrU   rV   rW   c                 C  rv   r   r(   rZ   r(   r(   r)   r[     rz   z CertificateRevocationList.__eq__c                 C  rv   )z<
        Number of revoked certificates in the CRL.
        Nr(   rP   r(   r(   r)   rj     rz   z!CertificateRevocationList.__len__idxr   c                 C     d S r"   r(   r%   r   r(   r(   r)   rl        z%CertificateRevocationList.__getitem__slicelist[RevokedCertificate]c                 C  r   r"   r(   r   r(   r(   r)   rl     r   int | slice-RevokedCertificate | list[RevokedCertificate]c                 C  rv   )zS
        Returns a revoked certificate (or slice of revoked certificates).
        Nr(   r   r(   r(   r)   rl     rz   #typing.Iterator[RevokedCertificate]c                 C  rv   )z8
        Iterator over the revoked certificates
        Nr(   rP   r(   r(   r)   rk   	  rz   z"CertificateRevocationList.__iter__r~   r   c                 C  rv   )zQ
        Verifies signature of revocation list against given public key.
        Nr(   )r%   r~   r(   r(   r)   is_signature_valid  rz   z,CertificateRevocationList.is_signature_validNr   r   )r{   rJ   r   r   r   r^   r   r   )r   r   r   r   r_   ra   rb   )r   rJ   r   r   )r   r   r   r   )r   r   r   r   )r   r   )r~   r   r   rW   )r,   r-   r.   r   r   r   ry   r   rc   r   r   r   r   r   r   r   r   r3   r   r   r[   rj   typingoverloadrl   rk   r   r(   r(   r(   r)   r     sl    r   c                   @  s  e Zd Zejd.ddZejd/dd	Zejd0ddZeejd1ddZ	eejd2ddZ
eejd3ddZeejd4ddZeejd5ddZeejd6ddZejd7d"d#Zeejd8d$d%Zeejd8d&d'Zeejd9d(d)Zejd:d+d,Zd-S );CertificateSigningRequestrU   rV   r   rW   c                 C  rv   r   r(   rZ   r(   r(   r)   r[     rz   z CertificateSigningRequest.__eq__rJ   c                 C  rv   r   r(   rP   r(   r(   r)   r]   "  rz   z"CertificateSigningRequest.__hash__r   c                 C  rv   r}   r(   rP   r(   r(   r)   r~   (  rz   z$CertificateSigningRequest.public_keyr   c                 C  rv   r   r(   rP   r(   r(   r)   r   .  rz   z!CertificateSigningRequest.subjectr   c                 C  rv   r   r(   rP   r(   r(   r)   r   5  rz   z2CertificateSigningRequest.signature_hash_algorithmr   c                 C  rv   r   r(   rP   r(   r(   r)   r   ?  rz   z1CertificateSigningRequest.signature_algorithm_oidr   c                 C  rv   r   r(   rP   r(   r(   r)   r   F  rz   z8CertificateSigningRequest.signature_algorithm_parametersr   c                 C  rv   )z@
        Returns the extensions in the signing request.
        Nr(   rP   r(   r(   r)   r3   O  rz   z$CertificateSigningRequest.extensionsrd   c                 C  rv   )z/
        Returns an Attributes object.
        Nr(   rP   r(   r(   r)   r8   V  rz   z$CertificateSigningRequest.attributesr   r   rH   c                 C  rv   )z;
        Encodes the request to PEM or DER format.
        Nr(   r   r(   r(   r)   r   ]  rz   z&CertificateSigningRequest.public_bytesc                 C  rv   r   r(   rP   r(   r(   r)   r   c  rz   z#CertificateSigningRequest.signaturec                 C  rv   )zd
        Returns the PKCS#10 CertificationRequestInfo bytes as defined in RFC
        2986.
        Nr(   rP   r(   r(   r)   tbs_certrequest_bytesj  rz   z/CertificateSigningRequest.tbs_certrequest_bytesc                 C  rv   )z8
        Verifies signature of signing request.
        Nr(   rP   r(   r(   r)   r   r  rz   z,CertificateSigningRequest.is_signature_validr   c                 C  rv   )z:
        Get the attribute value for a given OID.
        Nr(   )r%   r   r(   r(   r)   ri   y  rz   z/CertificateSigningRequest.get_attribute_for_oidNra   rb   r   r   r   r^   r   r   )r   rd   r   r_   )r   rW   )r   r   r   rH   )r,   r-   r.   r   r   r[   r]   r~   rc   r   r   r   r   r3   r8   r   r   r   r   ri   r(   r(   r(   r)   r     sL    r   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 | Noner3   r4   r8   r9   c                 C  s   || _ || _|| _dS )zB
        Creates an empty X.509 certificate request (v1).
        N)_subject_namer   rg   )r%   r   r3   r8   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   r5   r   r   rg   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   r7   r   r   r   rg   r%   r   r   r1   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;   rg   rG   r   r   r   )r%   r   rG   r   tagr(   r(   r)   add_attribute  s   


z.CertificateSigningRequestBuilder.add_attributersa_paddingprivate_keyr   rt   _AllowedHashTypes | Nonebackend
typing.Anyr   %padding.PSS | padding.PKCS1v15 | Noner   c                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   r5   rX   r   PSSPKCS1v15r   r   RSAPrivateKey	rust_x509Zcreate_x509_csrr%   r   rt   r   r   r(   r(   r)   sign  s   
z%CertificateSigningRequestBuilder.sign)r   r   r3   r4   r8   r9   )r   r   r   r   )r   r   r   rW   r   r   )r   r   rG   rH   r   r   r   r   r"   )
r   r   rt   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CertificateBuilderr4   r   Nissuer_namer   r   r~    CertificatePublicKeyTypes | Noner{   
int | Noner   r   r   r3   r   r    c                 C  s6   t j| _|| _|| _|| _|| _|| _|| _|| _	d S r"   )
rm   rp   _version_issuer_namer   _public_keyr   _not_valid_before_not_valid_afterr   )r%   r   r   r~   r{   r   r   r3   r(   r(   r)   r$     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   r5   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   r5   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   ZDSAPublicKeyr   ZRSAPublicKeyr   ZEllipticCurvePublicKeyr
   ZEd25519PublicKeyr	   ZEd448PublicKeyr   ZX25519PublicKeyr   ZX448PublicKeyr   r   r5   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   r5   
bit_lengthr   r   r   r   r   r   r   r%   r   r(   r(   r)   r{   T  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   r5   rE   _EARLIEST_UTC_TIMEr   r   r   r   r   r   r   r%   r<   r(   r(   r)   r   o  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   r5   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   r7   r   r   r   r   r   r   r   r   r   r(   r(   r)   r     s   
z CertificateBuilder.add_extensionr   r   r   rt   r   r   r   r   r   rs   c                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   r5   r   r   r   r   r   rX   r   r   r   r   r   r   r   Zcreate_x509_certificater   r(   r(   r)   r     s(   





zCertificateBuilder.sign)r   r   r   r   r~   r   r{   r   r   r   r   r   r3   r4   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   rt   r   r   r   r   r   r   rs   )r,   r-   r.   __annotations__r$   r   r   r~   r{   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 CertificateRevocationListBuilderr4   r   r   _revoked_certificatesNr   r   r   r   r   r3   revoked_certificatesc                 C  s"   || _ || _|| _|| _|| _d S r"   )r   _last_update_next_updater   r  )r%   r   r   r   r3   r  r(   r(   r)   r$     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   r5   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  r5   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  r5   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   r7   r   r   r   r  r  r  r   r(   r(   r)   r   ;  s   
z.CertificateRevocationListBuilder.add_extensionrevoked_certificater   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   r   r   r   r   r  r  r   r  )r%   r  r(   r(   r)   add_revoked_certificateN  s   
z8CertificateRevocationListBuilder.add_revoked_certificater   r   r   rt   r   r   r   r   r   r   c                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   r5   r  r  rX   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   r3   r4   r  r   )r   r   r   r   )r   r=   r   r   )r   r=   r   r   )r   r   r   rW   r   r   )r  r   r   r   r"   )
r   r   rt   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                   @  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 )$RevokedCertificateBuilderNr{   r   r   r   r3   r4   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   r5   r   r	  r   r   r   r(   r(   r)   r{     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   r5   rE   r   r	  r   r   r   r(   r(   r)   r     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   r7   r   r	  r   r   r   r(   r(   r)   r     s   
z'RevokedCertificateBuilder.add_extensionr   r   r   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   r5   r   r   r   r   )r%   r   r(   r(   r)   build  s   

zRevokedCertificateBuilder.build)r{   r   r   r   r3   r4   )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(   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_number  rT   r  )r1   r2   r3   r4   r   r    )r   r   r8   r9   r   r    )r<   r=   r   r=   rb   )N
__future__r   r   rA   r  r   r   Zcryptographyr   Z"cryptography.hazmat.bindings._rustr   r   Zcryptography.hazmat.primitivesr   r   Z)cryptography.hazmat.primitives.asymmetricr   r   r	   r
   r   r   r   r   Z/cryptography.hazmat.primitives.asymmetric.typesr   r   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   r7   r;   rE   rF   rd   Enumrm   rq   ABCMetars   registerr   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>   sv   (





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