o
    c                  	   @   s  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 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"G dd de#Z$dee dej%ee  ddfddZ&de!dej%ej'e!e(ej)e* f  ddfddZ+dejdejfddZ,G dd dZ-G dd dZ.G dd dej/Z0G d d! d!e#Z1G d"d# d#e j2d$Z3e34ej3 G d%d& d&e j2d$Z5e54ej5 G d'd( d(e5Z6G d)d* d*e j2d$Z7e74ej7 G d+d, d,e j2d$Z8e84ej8 	dGd-e(d.ej9de3fd/d0Z:d-e(dej%e3 fd1d2Z;	dGd-e(d.ej9de3fd3d4Z<	dGd-e(d.ej9de8fd5d6Z=	dGd-e(d.ej9de8fd7d8Z>	dGd-e(d.ej9de7fd9d:Z?	dGd-e(d.ej9de7fd;d<Z@G d=d> d>ZAG d?d@ d@ZBG dAdB dBZCG dCdD dDZDde*fdEdFZEdS )H    N)utils)x509)hashesserialization)dsaeced448ed25519rsax448x25519)#CERTIFICATE_ISSUER_PUBLIC_KEY_TYPESCERTIFICATE_PRIVATE_KEY_TYPESCERTIFICATE_PUBLIC_KEY_TYPES)	Extension
ExtensionsExtensionType_make_sequence_methods)Name	_ASN1Type)ObjectIdentifieri     c                       *   e Zd Zdededdf fddZ  ZS )AttributeNotFoundmsgoidreturnNc                       t t| | || _d S N)superr   __init__r   )selfr   r   	__class__ 6lib/python3.10/site-packages/cryptography/x509/base.pyr    )      
zAttributeNotFound.__init__)__name__
__module____qualname__strr   r    __classcell__r$   r$   r"   r%   r   (       "r   	extension
extensionsr   c                 C   s"   |D ]}|j | j krtdqd S )Nz$This extension has already been set.)r   
ValueError)r-   r.   er$   r$   r%   _reject_duplicate_extension.   s
   r1   r   
attributesc                 C   s$   |D ]\}}}|| krt dqd S )Nz$This attribute has already been set.)r/   )r   r2   Zattr_oid_r$   r$   r%   _reject_duplicate_attribute8   s
   r4   timec                 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)r6   Z	utcoffsetdatetimeZ	timedeltareplace)r5   offsetr$   r$   r%   _convert_to_naive_utc_timeD   s
   
r:   c                	   @   s   e Zd ZejjfdedededdfddZ	e
defdd	Ze
defd
dZdefddZdedefddZdefddZdS )	Attributer   value_typer   Nc                 C      || _ || _|| _d S r   )_oid_valuer=   )r!   r   r<   r=   r$   r$   r%   r    S      
zAttribute.__init__c                 C      | j S r   )r?   r!   r$   r$   r%   r   ]      zAttribute.oidc                 C   rB   r   )r@   rC   r$   r$   r%   r<   a   rD   zAttribute.valuec                 C   s   d | j| jS )Nz<Attribute(oid={}, value={!r})>)formatr   r<   rC   r$   r$   r%   __repr__e   s   zAttribute.__repr__otherc                 C   s2   t |tstS | j|jko| j|jko| j|jkS r   )
isinstancer;   NotImplementedr   r<   r=   r!   rG   r$   r$   r%   __eq__h   s   


zAttribute.__eq__c                 C   s   t | j| j| jfS r   )hashr   r<   r=   rC   r$   r$   r%   __hash__r   s   zAttribute.__hash__)r'   r(   r)   r   Z
UTF8Stringr<   r   bytesintr    propertyr   r*   rF   objectboolrK   rM   r$   r$   r$   r%   r;   R   s$    


r;   c                   @   sR   e Zd Zdeje ddfddZed\ZZ	Z
defddZd	edefd
dZdS )
Attributesr2   r   Nc                 C   s   t || _d S r   )list_attributes)r!   r2   r$   r$   r%   r    w   s   zAttributes.__init__rU   c                 C   s   d | jS )Nz<Attributes({})>)rE   rU   rC   r$   r$   r%   rF      s   zAttributes.__repr__r   c                 C   s,   | D ]}|j |kr|  S qtd||)NzNo {} attribute was found)r   r   rE   )r!   r   attrr$   r$   r%   get_attribute_for_oid   s
   
z Attributes.get_attribute_for_oid)r'   r(   r)   typingIterabler;   r    r   __len____iter____getitem__r*   rF   r   rW   r$   r$   r$   r%   rS   v   s    
rS   c                   @   s   e Zd ZdZdZdS )Versionr      N)r'   r(   r)   Zv1v3r$   r$   r$   r%   r]      s    r]   c                       r   )InvalidVersionr   parsed_versionr   Nc                    r   r   )r   r`   r    ra   )r!   r   ra   r"   r$   r%   r       r&   zInvalidVersion.__init__)r'   r(   r)   r*   rO   r    r+   r$   r$   r"   r%   r`      r,   r`   c                   @   s  e Zd ZejdejdefddZe	ejde
fddZe	ejdefddZejdefd	d
Ze	ejdejfddZe	ejdejfddZe	ejdefddZe	ejdefddZe	ejdejej fddZe	ejdefddZe	ejdefddZe	ejdefddZe	ejdefddZe	ejdefddZejde de!fd d!Z"ejde
fd"d#Z#ejd$e$j%defd%d&Z&d'S )(Certificate	algorithmr   c                 C      dS z4
        Returns bytes using digest passed.
        Nr$   r!   rc   r$   r$   r%   fingerprint       zCertificate.fingerprintc                 C   rd   )z3
        Returns certificate serial number
        Nr$   rC   r$   r$   r%   serial_number   rh   zCertificate.serial_numberc                 C   rd   )z1
        Returns the certificate version
        Nr$   rC   r$   r$   r%   version   rh   zCertificate.versionc                 C   rd   z(
        Returns the public key
        Nr$   rC   r$   r$   r%   
public_key   rh   zCertificate.public_keyc                 C   rd   )z?
        Not before time (represented as UTC datetime)
        Nr$   rC   r$   r$   r%   not_valid_before   rh   zCertificate.not_valid_beforec                 C   rd   )z>
        Not after time (represented as UTC datetime)
        Nr$   rC   r$   r$   r%   not_valid_after   rh   zCertificate.not_valid_afterc                 C   rd   )z1
        Returns the issuer name object.
        Nr$   rC   r$   r$   r%   issuer   rh   zCertificate.issuerc                 C   rd   z2
        Returns the subject name object.
        Nr$   rC   r$   r$   r%   subject   rh   zCertificate.subjectc                 C   rd   zt
        Returns a HashAlgorithm corresponding to the type of the digest signed
        in the certificate.
        Nr$   rC   r$   r$   r%   signature_hash_algorithm   rh   z$Certificate.signature_hash_algorithmc                 C   rd   zJ
        Returns the ObjectIdentifier of the signature algorithm.
        Nr$   rC   r$   r$   r%   signature_algorithm_oid   rh   z#Certificate.signature_algorithm_oidc                 C   rd   )z/
        Returns an Extensions object.
        Nr$   rC   r$   r$   r%   r.      rh   zCertificate.extensionsc                 C   rd   z.
        Returns the signature bytes.
        Nr$   rC   r$   r$   r%   	signature   rh   zCertificate.signaturec                 C   rd   )zR
        Returns the tbsCertificate payload bytes as defined in RFC 5280.
        Nr$   rC   r$   r$   r%   tbs_certificate_bytes   rh   z!Certificate.tbs_certificate_bytesc                 C   rd   )zh
        Returns the tbsCertificate payload bytes with the SCT list extension
        stripped.
        Nr$   rC   r$   r$   r%   tbs_precertificate_bytes   rh   z$Certificate.tbs_precertificate_bytesrG   c                 C   rd   z"
        Checks equality.
        Nr$   rJ   r$   r$   r%   rK      rh   zCertificate.__eq__c                 C   rd   z"
        Computes a hash.
        Nr$   rC   r$   r$   r%   rM      rh   zCertificate.__hash__encodingc                 C   rd   )zB
        Serializes the certificate to PEM or DER format.
        Nr$   r!   r|   r$   r$   r%   public_bytes  rh   zCertificate.public_bytesN)'r'   r(   r)   abcabstractmethodr   HashAlgorithmrN   rg   rP   rO   ri   r]   rj   r   rl   r7   rm   rn   r   ro   rq   rX   Optionalrs   r   ru   r   r.   rw   rx   ry   rQ   rR   rK   rM   r   Encodingr~   r$   r$   r$   r%   rb      sb    
rb   )	metaclassc                   @   sV   e Zd ZeejdefddZeejdejfddZ	eejde
fddZdS )	RevokedCertificater   c                 C   rd   )zG
        Returns the serial number of the revoked certificate.
        Nr$   rC   r$   r$   r%   ri     rh   z RevokedCertificate.serial_numberc                 C   rd   )zH
        Returns the date of when this certificate was revoked.
        Nr$   rC   r$   r$   r%   revocation_date  rh   z"RevokedCertificate.revocation_datec                 C   rd   )zW
        Returns an Extensions object containing a list of Revoked extensions.
        Nr$   rC   r$   r$   r%   r.      rh   zRevokedCertificate.extensionsN)r'   r(   r)   rP   r   r   rO   ri   r7   r   r   r.   r$   r$   r$   r%   r     s    r   c                   @   s\   e Zd ZdedejdefddZedefddZedejfd	d
Z	edefddZ
dS )_RawRevokedCertificateri   r   r.   c                 C   r>   r   _serial_number_revocation_date_extensionsr!   ri   r   r.   r$   r$   r%   r    -  rA   z_RawRevokedCertificate.__init__r   c                 C   rB   r   )r   rC   r$   r$   r%   ri   7  rD   z$_RawRevokedCertificate.serial_numberc                 C   rB   r   )r   rC   r$   r$   r%   r   ;  rD   z&_RawRevokedCertificate.revocation_datec                 C   rB   r   )r   rC   r$   r$   r%   r.   ?  rD   z!_RawRevokedCertificate.extensionsN)r'   r(   r)   rO   r7   r   r    rP   ri   r   r.   r$   r$   r$   r%   r   ,  s    

r   c                   @   s  e Zd ZejdejdefddZejde	j
defddZejdedeje fd	d
Zeejdeje	j
 fddZeejdefddZeejdefddZeejdejej fddZeejdejfddZeejdefddZeejdefddZeejdefddZejdedefddZ ejdefddZ!ej"d edefd!d"Z#ej"d e$dej%e fd#d"Z#ejd ej&ee$f dej&eej%e f fd$d"Z#ejdej'e fd%d&Z(ejd'e)defd(d)Z*d*S )+CertificateRevocationListr|   r   c                 C   rd   )z:
        Serializes the CRL to PEM or DER format.
        Nr$   r}   r$   r$   r%   r~   E  rh   z&CertificateRevocationList.public_bytesrc   c                 C   rd   re   r$   rf   r$   r$   r%   rg   K  rh   z%CertificateRevocationList.fingerprintri   c                 C   rd   )zs
        Returns an instance of RevokedCertificate or None if the serial_number
        is not in the CRL.
        Nr$   )r!   ri   r$   r$   r%   (get_revoked_certificate_by_serial_numberQ  rh   zBCertificateRevocationList.get_revoked_certificate_by_serial_numberc                 C   rd   rr   r$   rC   r$   r$   r%   rs   Z  rh   z2CertificateRevocationList.signature_hash_algorithmc                 C   rd   rt   r$   rC   r$   r$   r%   ru   d  rh   z1CertificateRevocationList.signature_algorithm_oidc                 C   rd   )zC
        Returns the X509Name with the issuer of this CRL.
        Nr$   rC   r$   r$   r%   ro   k  rh   z CertificateRevocationList.issuerc                 C   rd   )z?
        Returns the date of next update for this CRL.
        Nr$   rC   r$   r$   r%   next_updater  rh   z%CertificateRevocationList.next_updatec                 C   rd   )z?
        Returns the date of last update for this CRL.
        Nr$   rC   r$   r$   r%   last_updatey  rh   z%CertificateRevocationList.last_updatec                 C   rd   )zS
        Returns an Extensions object containing a list of CRL extensions.
        Nr$   rC   r$   r$   r%   r.     rh   z$CertificateRevocationList.extensionsc                 C   rd   rv   r$   rC   r$   r$   r%   rw     rh   z#CertificateRevocationList.signaturec                 C   rd   )zO
        Returns the tbsCertList payload bytes as defined in RFC 5280.
        Nr$   rC   r$   r$   r%   tbs_certlist_bytes  rh   z,CertificateRevocationList.tbs_certlist_bytesrG   c                 C   rd   rz   r$   rJ   r$   r$   r%   rK     rh   z CertificateRevocationList.__eq__c                 C   rd   )z<
        Number of revoked certificates in the CRL.
        Nr$   rC   r$   r$   r%   rZ     rh   z!CertificateRevocationList.__len__idxc                 C      d S r   r$   r!   r   r$   r$   r%   r\        z%CertificateRevocationList.__getitem__c                 C   r   r   r$   r   r$   r$   r%   r\     r   c                 C   rd   )zS
        Returns a revoked certificate (or slice of revoked certificates).
        Nr$   r   r$   r$   r%   r\     rh   c                 C   rd   )z8
        Iterator over the revoked certificates
        Nr$   rC   r$   r$   r%   r[     rh   z"CertificateRevocationList.__iter__rl   c                 C   rd   )zQ
        Verifies signature of revocation list against given public key.
        Nr$   )r!   rl   r$   r$   r%   is_signature_valid  rh   z,CertificateRevocationList.is_signature_validN)+r'   r(   r)   r   r   r   r   rN   r~   r   r   rg   rO   rX   r   r   r   rP   rs   r   ru   r   ro   r7   r   r   r   r.   rw   r   rQ   rR   rK   rZ   Zoverloadr\   sliceListZUnionIteratorr[   r   r   r$   r$   r$   r%   r   D  sv    
r   c                   @   sF  e Zd ZejdedefddZejdefddZ	ejde
fddZeejdefd	d
Zeejdejej fddZeejdefddZeejdefddZeejdefddZejdejdefddZeejdefddZeejdefddZeejdefddZ ejdedefddZ!dS ) CertificateSigningRequestrG   r   c                 C   rd   rz   r$   rJ   r$   r$   r%   rK     rh   z CertificateSigningRequest.__eq__c                 C   rd   r{   r$   rC   r$   r$   r%   rM     rh   z"CertificateSigningRequest.__hash__c                 C   rd   rk   r$   rC   r$   r$   r%   rl     rh   z$CertificateSigningRequest.public_keyc                 C   rd   rp   r$   rC   r$   r$   r%   rq     rh   z!CertificateSigningRequest.subjectc                 C   rd   rr   r$   rC   r$   r$   r%   rs     rh   z2CertificateSigningRequest.signature_hash_algorithmc                 C   rd   rt   r$   rC   r$   r$   r%   ru     rh   z1CertificateSigningRequest.signature_algorithm_oidc                 C   rd   )z@
        Returns the extensions in the signing request.
        Nr$   rC   r$   r$   r%   r.     rh   z$CertificateSigningRequest.extensionsc                 C   rd   )z/
        Returns an Attributes object.
        Nr$   rC   r$   r$   r%   r2     rh   z$CertificateSigningRequest.attributesr|   c                 C   rd   )z;
        Encodes the request to PEM or DER format.
        Nr$   r}   r$   r$   r%   r~     rh   z&CertificateSigningRequest.public_bytesc                 C   rd   rv   r$   rC   r$   r$   r%   rw     rh   z#CertificateSigningRequest.signaturec                 C   rd   )zd
        Returns the PKCS#10 CertificationRequestInfo bytes as defined in RFC
        2986.
        Nr$   rC   r$   r$   r%   tbs_certrequest_bytes	  rh   z/CertificateSigningRequest.tbs_certrequest_bytesc                 C   rd   )z8
        Verifies signature of signing request.
        Nr$   rC   r$   r$   r%   r     rh   z,CertificateSigningRequest.is_signature_validr   c                 C   rd   )z:
        Get the attribute value for a given OID.
        Nr$   )r!   r   r$   r$   r%   rW     rh   z/CertificateSigningRequest.get_attribute_for_oidN)"r'   r(   r)   r   r   rQ   rR   rK   rO   rM   r   rl   rP   r   rq   rX   r   r   r   rs   r   ru   r   r.   rS   r2   r   r   rN   r~   rw   r   r   rW   r$   r$   r$   r%   r     sJ    
r   databackendc                 C   
   t | S r   )	rust_x509load_pem_x509_certificater   r   r$   r$   r%   r   $     
r   c                 C   r   r   )r   load_pem_x509_certificates)r   r$   r$   r%   r   *  s   
r   c                 C   r   r   )r   load_der_x509_certificater   r$   r$   r%   r   /  r   r   c                 C   r   r   )r   load_pem_x509_csrr   r$   r$   r%   r   6  r   r   c                 C   r   r   )r   load_der_x509_csrr   r$   r$   r%   r   =  r   r   c                 C   r   r   )r   load_pem_x509_crlr   r$   r$   r%   r   D  r   r   c                 C   r   r   )r   load_der_x509_crlr   r$   r$   r%   r   K  r   r   c                   @   s   e Zd Zdg g fdeje dejee  dejej	e
eeje f  fddZdedd fd	d
Zdededd fddZddde
dedeje dd fddZ	ddedejej dejdefddZdS ) CertificateSigningRequestBuilderNsubject_namer.   r2   c                 C   s   || _ || _|| _dS )zB
        Creates an empty X.509 certificate request (v1).
        N)_subject_namer   rU   )r!   r   r.   r2   r$   r$   r%   r    R  s   
z)CertificateSigningRequestBuilder.__init__namer   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.)rH   r   	TypeErrorr   r/   r   r   rU   r!   r   r$   r$   r%   r   a  s   


z-CertificateSigningRequestBuilder.subject_nameextvalcriticalc                 C   sD   t |ts	tdt|j||}t|| j t| j| j|g | j	S )zE
        Adds an X.509 extension to the certificate request.
        "extension must be an ExtensionType)
rH   r   r   r   r   r1   r   r   r   rU   r!   r   r   r-   r$   r$   r%   add_extensionm  s   

z.CertificateSigningRequestBuilder.add_extension)_tagr   r<   r   c                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
| j|||fg 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)rH   r   r   rN   r   r4   rU   r<   r   r   r   )r!   r   r<   r   tagr$   r$   r%   add_attribute  s   


z.CertificateSigningRequestBuilder.add_attributeprivate_keyrc   r   c                 C   s    | j du r	tdt| ||S )zF
        Signs the request using the requestor's private key.
        Nz/A CertificateSigningRequest must have a subject)r   r/   r   Zcreate_x509_csrr!   r   rc   r   r$   r$   r%   sign  s   
	z%CertificateSigningRequestBuilder.signr   )r'   r(   r)   rX   r   r   r   r   r   Tupler   rN   rO   r    r   rR   r   r   r   r   r   r   Anyr   r   r$   r$   r$   r%   r   Q  sR    


$
r   c                   @   s:  e Zd ZU ejee  ed< ddddddg fdeje	 deje	 deje
 deje dejej dejej d	ejee  d
dfddZde	d
d fddZde	d
d fddZde
d
d fddZded
d fddZdejd
d fddZdejd
d fddZdeded
d fdd Z	d&d!ed"ejej d#ejd
efd$d%ZdS )'CertificateBuilderr   Nissuer_namer   rl   ri   rm   rn   r.   r   c                 C   s6   t j| _|| _|| _|| _|| _|| _|| _|| _	d S r   )
r]   r_   Z_version_issuer_namer   _public_keyr   _not_valid_before_not_valid_afterr   )r!   r   r   rl   ri   rm   rn   r.   r$   r$   r%   r      s   

zCertificateBuilder.__init__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.)rH   r   r   r   r/   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   )rH   r   r   r   r/   r   r   r   r   r   r   r   r   r$   r$   r%   r     s   

zCertificateBuilder.subject_namekeyc              	   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.)rH   r   ZDSAPublicKeyr
   ZRSAPublicKeyr   ZEllipticCurvePublicKeyr	   ZEd25519PublicKeyr   ZEd448PublicKeyr   ZX25519PublicKeyr   ZX448PublicKeyr   r   r/   r   r   r   r   r   r   r   )r!   r   r$   r$   r%   rl     s2   
zCertificateBuilder.public_keynumberc                 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.)rH   rO   r   r   r/   
bit_lengthr   r   r   r   r   r   r   r!   r   r$   r$   r%   ri     s&   

z CertificateBuilder.serial_numberr5   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.)rH   r7   r   r   r/   r:   _EARLIEST_UTC_TIMEr   r   r   r   r   r   r   r!   r5   r$   r$   r%   rm   '  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.)rH   r7   r   r   r/   r:   r   r   r   r   r   r   r   r   r   r$   r$   r%   rn   F  s.   


z"CertificateBuilder.not_valid_afterr   r   c              	   C   sT   t |ts	tdt|j||}t|| j t| j| j	| j
| j| j| j| j|g S )z=
        Adds an X.509 extension to the certificate.
        r   )rH   r   r   r   r   r1   r   r   r   r   r   r   r   r   r   r$   r$   r%   r   f  s   

z CertificateBuilder.add_extensionr   rc   r   c                 C   sz   | 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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 key)	r   r/   r   r   r   r   r   r   Zcreate_x509_certificater   r$   r$   r%   r   |  s   
	




zCertificateBuilder.signr   )r'   r(   r)   rX   r   r   r   __annotations__r   r   r   rO   r7   r    r   r   rl   ri   rm   rn   rR   r   r   r   r   r   rb   r   r$   r$   r$   r%   r     sr   
 

	

%
 

r   c                   @   s  e Zd ZU ejee  ed< eje ed< dddg g fdej	e
 dej	ej dej	ej dejee  deje f
d	d
Zde
dd fddZdejdd fddZdejdd fddZdededd fddZdedd fddZ	ddedej	ej dejdefddZdS ) CertificateRevocationListBuilderr   _revoked_certificatesNr   r   r   r.   revoked_certificatesc                 C   s"   || _ || _|| _|| _|| _d S r   )r   _last_update_next_updater   r   )r!   r   r   r   r.   r   r$   r$   r%   r      s
   
z)CertificateRevocationListBuilder.__init__r   c                 C   s<   t |ts	td| jd urtdt|| j| j| j| j	S )Nr   r   )
rH   r   r   r   r/   r   r   r   r   r   )r!   r   r$   r$   r%   r     s   

z,CertificateRevocationListBuilder.issuer_namec                 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.)rH   r7   r   r   r/   r:   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.)rH   r7   r   r   r/   r:   r   r   r   r   r   r   )r!   r   r$   r$   r%   r     s(   
z,CertificateRevocationListBuilder.next_updater   r   c                 C   sL   t |ts	tdt|j||}t|| j t| j| j	| j
| j|g | jS )zM
        Adds an X.509 extension to the certificate revocation list.
        r   )rH   r   r   r   r   r1   r   r   r   r   r   r   r   r$   r$   r%   r     s   

z.CertificateRevocationListBuilder.add_extensionrevoked_certificatec                 C   s2   t |ts	tdt| j| j| j| j| j|g S )z8
        Adds a revoked certificate to the CRL.
        z)Must be an instance of RevokedCertificate)	rH   r   r   r   r   r   r   r   r   )r!   r   r$   r$   r%   add_revoked_certificate  s   

z8CertificateRevocationListBuilder.add_revoked_certificater   rc   r   c                 C   sD   | j d u r	td| jd u rtd| jd u rt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 time)r   r/   r   r   r   Zcreate_x509_crlr   r$   r$   r%   r     s   


z%CertificateRevocationListBuilder.signr   )r'   r(   r)   rX   r   r   r   r   r   r   r   r7   r    r   r   r   rR   r   r   r   r   r   r   r   r   r$   r$   r$   r%   r     sp   
 








r   c                	   @   s   e Zd Zddg fdeje dejej dejee	  fddZ
dedd fd	d
Zdejdd fddZde	dedd fddZddejdefddZdS )RevokedCertificateBuilderNri   r   r.   c                 C   r>   r   r   r   r$   r$   r%   r    "  rA   z"RevokedCertificateBuilder.__init__r   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   )	rH   rO   r   r   r/   r   r   r   r   r   r$   r$   r%   ri   ,  s   


z'RevokedCertificateBuilder.serial_numberr5   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.)
rH   r7   r   r   r/   r:   r   r   r   r   r   r$   r$   r%   r   >  s   

z)RevokedCertificateBuilder.revocation_dater   r   c                 C   sD   t |ts	tdt|j||}t|| j t| j| j	| j|g S )Nr   )
rH   r   r   r   r   r1   r   r   r   r   r   r$   r$   r%   r   N  s   

z'RevokedCertificateBuilder.add_extensionr   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   r/   r   r   r   r   )r!   r   r$   r$   r%   build\  s   

zRevokedCertificateBuilder.buildr   )r'   r(   r)   rX   r   rO   r7   r   r   r   r    ri   r   rR   r   r   r   r   r$   r$   r$   r%   r   !  s2    




r   c                   C   s   t tddd? S )N   Zbigr   )rO   
from_bytesosurandomr$   r$   r$   r%   random_serial_numberj  s   r   r   )Fr   r7   r   rX   Zcryptographyr   Z"cryptography.hazmat.bindings._rustr   r   Zcryptography.hazmat.primitivesr   r   Z)cryptography.hazmat.primitives.asymmetricr   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   	Exceptionr   r   r1   r   rN   r   rO   r4   r:   r;   rS   Enumr]   r`   ABCMetarb   registerr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r$   r$   r$   r%   <module>   s   $	


$y|]





\ n I