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 	dEd-e(d.ej9de3fd/d0Z:	dEd-e(d.ej9de3fd1d2Z;	dEd-e(d.ej9de8fd3d4Z<	dEd-e(d.ej9de8fd5d6Z=	dEd-e(d.ej9de7fd7d8Z>	dEd-e(d.ej9de7fd9d:Z?G d;d< d<Z@G d=d> d>ZAG d?d@ d@ZBG dAdB dBZCde*fdCdDZDdS )F    N)utils)x509)hashesserialization)dsaeced25519ed448rsax25519x448)#CERTIFICATE_ISSUER_PUBLIC_KEY_TYPESCERTIFICATE_PRIVATE_KEY_TYPESCERTIFICATE_PUBLIC_KEY_TYPES)	ExtensionExtensionType
Extensions_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__ :usr/lib/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_attribute9   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   	utcoffsetdatetime	timedeltareplace)r5   offsetr$   r$   r%   _convert_to_naive_utc_timeE   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    T      
zAttribute.__init__c                 C      | j S r   )rA   r!   r$   r$   r%   r   ^      zAttribute.oidc                 C   rD   r   )rB   rE   r$   r$   r%   r>   b   rF   zAttribute.valuec                 C   s   d | j| jS )Nz<Attribute(oid={}, value={!r})>)formatr   r>   rE   r$   r$   r%   __repr__f   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!   rI   r$   r$   r%   __eq__i   s   


zAttribute.__eq__c                 C   s   t | j| j| jfS r   )hashr   r>   r?   rE   r$   r$   r%   __hash__s   s   zAttribute.__hash__)r'   r(   r)   r   Z
UTF8Stringr>   r   bytesintr    propertyr   r*   rH   objectboolrM   rO   r$   r$   r$   r%   r=   S   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    x   s   zAttributes.__init__rW   c                 C   s   d | jS )Nz<Attributes({})>)rG   rW   rE   r$   r$   r%   rH      s   zAttributes.__repr__r   c                 C   s,   | D ]}|j |kr|  S qtd||)NzNo {} attribute was found)r   r   rG   )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*   rH   r   rY   r$   r$   r$   r%   rU   w   s    
rU   c                   @   s   e Zd ZdZdZdS )Versionr      N)r'   r(   r)   v1v3r$   r$   r$   r%   r_      s    r_   c                       r   )InvalidVersionr   parsed_versionr   Nc                    r   r   )r   rc   r    rd   )r!   r   rd   r"   r$   r%   r       r&   zInvalidVersion.__init__)r'   r(   r)   r*   rQ   r    r+   r$   r$   r"   r%   rc      r,   rc   c                   @   s|  e Zd ZejdejdefddZej	de
fddZej	defddZejdefd	d
Zej	dejfddZej	dejfddZej	defddZej	defddZej	dejej fddZej	defddZej	defddZej	defddZej	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
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!   rf   r$   r$   r%   fingerprint       zCertificate.fingerprintc                 C   rg   )z3
        Returns certificate serial number
        Nr$   rE   r$   r$   r%   serial_number   rk   zCertificate.serial_numberc                 C   rg   )z1
        Returns the certificate version
        Nr$   rE   r$   r$   r%   version   rk   zCertificate.versionc                 C   rg   z(
        Returns the public key
        Nr$   rE   r$   r$   r%   
public_key   rk   zCertificate.public_keyc                 C   rg   )z?
        Not before time (represented as UTC datetime)
        Nr$   rE   r$   r$   r%   not_valid_before   rk   zCertificate.not_valid_beforec                 C   rg   )z>
        Not after time (represented as UTC datetime)
        Nr$   rE   r$   r$   r%   not_valid_after   rk   zCertificate.not_valid_afterc                 C   rg   )z1
        Returns the issuer name object.
        Nr$   rE   r$   r$   r%   issuer   rk   zCertificate.issuerc                 C   rg   z2
        Returns the subject name object.
        Nr$   rE   r$   r$   r%   subject   rk   zCertificate.subjectc                 C   rg   zt
        Returns a HashAlgorithm corresponding to the type of the digest signed
        in the certificate.
        Nr$   rE   r$   r$   r%   signature_hash_algorithm   rk   z$Certificate.signature_hash_algorithmc                 C   rg   zJ
        Returns the ObjectIdentifier of the signature algorithm.
        Nr$   rE   r$   r$   r%   signature_algorithm_oid   rk   z#Certificate.signature_algorithm_oidc                 C   rg   )z/
        Returns an Extensions object.
        Nr$   rE   r$   r$   r%   r.      rk   zCertificate.extensionsc                 C   rg   z.
        Returns the signature bytes.
        Nr$   rE   r$   r$   r%   	signature   rk   zCertificate.signaturec                 C   rg   )zR
        Returns the tbsCertificate payload bytes as defined in RFC 5280.
        Nr$   rE   r$   r$   r%   tbs_certificate_bytes   rk   z!Certificate.tbs_certificate_bytesc                 C   rg   )zh
        Returns the tbsCertificate payload bytes with the SCT list extension
        stripped.
        Nr$   rE   r$   r$   r%   tbs_precertificate_bytes   rk   z$Certificate.tbs_precertificate_bytesrI   c                 C   rg   z"
        Checks equality.
        Nr$   rL   r$   r$   r%   rM      rk   zCertificate.__eq__c                 C   rg   z"
        Computes a hash.
        Nr$   rE   r$   r$   r%   rO      rk   zCertificate.__hash__encodingc                 C   rg   )zB
        Serializes the certificate to PEM or DER format.
        Nr$   r!   r   r$   r$   r%   public_bytes   rk   zCertificate.public_bytesN)'r'   r(   r)   abcabstractmethodr   HashAlgorithmrP   rj   abstractpropertyrQ   rl   r_   rm   r   ro   r8   rp   rq   r   rr   rt   rZ   Optionalrv   r   rx   r   r.   rz   r{   r|   rS   rT   rM   rO   r   Encodingr   r$   r$   r$   r%   re      sJ    
re   )	metaclassc                   @   sJ   e Zd ZejdefddZejdejfddZejde	fddZ
dS )	RevokedCertificater   c                 C   rg   )zG
        Returns the serial number of the revoked certificate.
        Nr$   rE   r$   r$   r%   rl     rk   z RevokedCertificate.serial_numberc                 C   rg   )zH
        Returns the date of when this certificate was revoked.
        Nr$   rE   r$   r$   r%   revocation_date  rk   z"RevokedCertificate.revocation_datec                 C   rg   )zW
        Returns an Extensions object containing a list of Revoked extensions.
        Nr$   rE   r$   r$   r%   r.     rk   zRevokedCertificate.extensionsN)r'   r(   r)   r   r   rQ   rl   r8   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 )_RawRevokedCertificaterl   r   r.   c                 C   r@   r   _serial_number_revocation_date_extensionsr!   rl   r   r.   r$   r$   r%   r      rC   z_RawRevokedCertificate.__init__r   c                 C   rD   r   )r   rE   r$   r$   r%   rl   )  rF   z$_RawRevokedCertificate.serial_numberc                 C   rD   r   )r   rE   r$   r$   r%   r   -  rF   z&_RawRevokedCertificate.revocation_datec                 C   rD   r   )r   rE   r$   r$   r%   r.   1  rF   z!_RawRevokedCertificate.extensionsN)r'   r(   r)   rQ   r8   r   r    rR   rl   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jdeje	j
 fddZejdefddZejdefddZejdejej fddZejdejfddZejdefddZej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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   rg   )z:
        Serializes the CRL to PEM or DER format.
        Nr$   r   r$   r$   r%   r   7  rk   z&CertificateRevocationList.public_bytesrf   c                 C   rg   rh   r$   ri   r$   r$   r%   rj   =  rk   z%CertificateRevocationList.fingerprintrl   c                 C   rg   )zs
        Returns an instance of RevokedCertificate or None if the serial_number
        is not in the CRL.
        Nr$   )r!   rl   r$   r$   r%   (get_revoked_certificate_by_serial_numberC  rk   zBCertificateRevocationList.get_revoked_certificate_by_serial_numberc                 C   rg   ru   r$   rE   r$   r$   r%   rv   L  rk   z2CertificateRevocationList.signature_hash_algorithmc                 C   rg   rw   r$   rE   r$   r$   r%   rx   U  rk   z1CertificateRevocationList.signature_algorithm_oidc                 C   rg   )zC
        Returns the X509Name with the issuer of this CRL.
        Nr$   rE   r$   r$   r%   rr   [  rk   z CertificateRevocationList.issuerc                 C   rg   )z?
        Returns the date of next update for this CRL.
        Nr$   rE   r$   r$   r%   next_updatea  rk   z%CertificateRevocationList.next_updatec                 C   rg   )z?
        Returns the date of last update for this CRL.
        Nr$   rE   r$   r$   r%   last_updateg  rk   z%CertificateRevocationList.last_updatec                 C   rg   )zS
        Returns an Extensions object containing a list of CRL extensions.
        Nr$   rE   r$   r$   r%   r.   m  rk   z$CertificateRevocationList.extensionsc                 C   rg   ry   r$   rE   r$   r$   r%   rz   s  rk   z#CertificateRevocationList.signaturec                 C   rg   )zO
        Returns the tbsCertList payload bytes as defined in RFC 5280.
        Nr$   rE   r$   r$   r%   tbs_certlist_bytesy  rk   z,CertificateRevocationList.tbs_certlist_bytesrI   c                 C   rg   r}   r$   rL   r$   r$   r%   rM     rk   z CertificateRevocationList.__eq__c                 C   rg   )z<
        Number of revoked certificates in the CRL.
        Nr$   rE   r$   r$   r%   r\     rk   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   rg   )zS
        Returns a revoked certificate (or slice of revoked certificates).
        Nr$   r   r$   r$   r%   r^     rk   c                 C   rg   )z8
        Iterator over the revoked certificates
        Nr$   rE   r$   r$   r%   r]     rk   z"CertificateRevocationList.__iter__ro   c                 C   rg   )zQ
        Verifies signature of revocation list against given public key.
        Nr$   )r!   ro   r$   r$   r%   is_signature_valid  rk   z,CertificateRevocationList.is_signature_validN)+r'   r(   r)   r   r   r   r   rP   r   r   r   rj   rQ   rZ   r   r   r   r   rv   r   rx   r   rr   r8   r   r   r   r.   rz   r   rS   rT   rM   r\   overloadr^   sliceListUnionIteratorr]   r   r   r$   r$   r$   r%   r   6  sf    
r   c                   @   s&  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jdefd	d
Zejdejej fddZejdefddZej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ejdefddZejdefddZejdefddZ ejdedefddZ!dS ) CertificateSigningRequestrI   r   c                 C   rg   r}   r$   rL   r$   r$   r%   rM     rk   z CertificateSigningRequest.__eq__c                 C   rg   r~   r$   rE   r$   r$   r%   rO     rk   z"CertificateSigningRequest.__hash__c                 C   rg   rn   r$   rE   r$   r$   r%   ro     rk   z$CertificateSigningRequest.public_keyc                 C   rg   rs   r$   rE   r$   r$   r%   rt     rk   z!CertificateSigningRequest.subjectc                 C   rg   ru   r$   rE   r$   r$   r%   rv     rk   z2CertificateSigningRequest.signature_hash_algorithmc                 C   rg   rw   r$   rE   r$   r$   r%   rx     rk   z1CertificateSigningRequest.signature_algorithm_oidc                 C   rg   )z@
        Returns the extensions in the signing request.
        Nr$   rE   r$   r$   r%   r.     rk   z$CertificateSigningRequest.extensionsc                 C   rg   )z/
        Returns an Attributes object.
        Nr$   rE   r$   r$   r%   r2     rk   z$CertificateSigningRequest.attributesr   c                 C   rg   )z;
        Encodes the request to PEM or DER format.
        Nr$   r   r$   r$   r%   r     rk   z&CertificateSigningRequest.public_bytesc                 C   rg   ry   r$   rE   r$   r$   r%   rz     rk   z#CertificateSigningRequest.signaturec                 C   rg   )zd
        Returns the PKCS#10 CertificationRequestInfo bytes as defined in RFC
        2986.
        Nr$   rE   r$   r$   r%   tbs_certrequest_bytes  rk   z/CertificateSigningRequest.tbs_certrequest_bytesc                 C   rg   )z8
        Verifies signature of signing request.
        Nr$   rE   r$   r$   r%   r     rk   z,CertificateSigningRequest.is_signature_validr   c                 C   rg   )z:
        Get the attribute value for a given OID.
        Nr$   )r!   r   r$   r$   r%   rY     rk   z/CertificateSigningRequest.get_attribute_for_oidN)"r'   r(   r)   r   r   rS   rT   rM   rQ   rO   r   ro   r   r   rt   rZ   r   r   r   rv   r   rx   r   r.   rU   r2   r   r   rP   r   rz   r   r   rY   r$   r$   r$   r%   r     s:    
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_der_x509_certificater   r$   r$   r%   r     r   r   c                 C   r   r   )r   load_pem_x509_csrr   r$   r$   r%   r     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   "  r   r   c                 C   r   r   )r   load_der_x509_crlr   r$   r$   r%   r   )  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   rW   )r!   r   r.   r2   r$   r$   r%   r    0  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.)rJ   r   	TypeErrorr   r/   r   r   rW   r!   r   r$   r$   r%   r   ?  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)
rJ   r   r   r   r   r1   r   r   r   rW   r!   r   r   r-   r$   r$   r%   add_extensionK  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)rJ   r   r   rP   r   r4   rW   r>   r   r   r   )r!   r   r>   r   tagr$   r$   r%   add_attribute]  s   


z.CertificateSigningRequestBuilder.add_attributeprivate_keyrf   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   rf   r   r$   r$   r%   sign}  s   
	z%CertificateSigningRequestBuilder.signr   )r'   r(   r)   rZ   r   r   r   r   r   Tupler   rP   rQ   r    r   rT   r   r   r   r   r   r   Anyr   r   r$   r$   r$   r%   r   /  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   ro   rl   rp   rq   r.   r   c                 C   s6   t j| _|| _|| _|| _|| _|| _|| _|| _	d S r   )
r_   rb   _version_issuer_namer   _public_keyr   _not_valid_before_not_valid_afterr   )r!   r   r   ro   rl   rp   rq   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.)rJ   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   )rJ   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.)rJ   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%   ro     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.)rJ   rQ   r   r   r/   
bit_lengthr   r   r   r   r   r   r   r!   r   r$   r$   r%   rl     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.)rJ   r8   r   r   r/   r<   _EARLIEST_UTC_TIMEr   r   r   r   r   r   r   r!   r5   r$   r$   r%   rp     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.)rJ   r8   r   r   r/   r<   r   r   r   r   r   r   r   r   r   r$   r$   r%   rq   $  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   )rJ   r   r   r   r   r1   r   r   r   r   r   r   r   r   r   r$   r$   r%   r   D  s   

z CertificateBuilder.add_extensionr   rf   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   Z  s   
	




zCertificateBuilder.signr   )r'   r(   r)   rZ   r   r   r   __annotations__r   r   r   rQ   r8   r    r   r   ro   rl   rp   rq   rT   r   r   r   r   r   re   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   )
rJ   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.)rJ   r8   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.)rJ   r8   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   )rJ   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)	rJ   r   r   r   r   r   r   r   r   )r!   r   r$   r$   r%   add_revoked_certificate  s   

z8CertificateRevocationListBuilder.add_revoked_certificater   rf   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)   rZ   r   r   r   r   r   r   r   r8   r    r   r   r   rT   r   r   r   r   r   r   r   r   r$   r$   r$   r%   r   x  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 )RevokedCertificateBuilderNrl   r   r.   c                 C   r@   r   r   r   r$   r$   r%   r       rC   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   )	rJ   rQ   r   r   r/   r   r   r   r   r   r$   r$   r%   rl   
  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.)
rJ   r8   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   )
rJ   r   r   r   r   r1   r   r   r   r   r   r$   r$   r%   r   ,  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)   rZ   r   rQ   r8   r   r   r   r    rl   r   rT   r   r   r   r   r$   r$   r$   r%   r     s2    




r   c                   C   s   t tddd? S )N   bigr   )rQ   
from_bytesosurandomr$   r$   r$   r%   random_serial_numberH  s   r   r   )Er   r8   r   rZ   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   rP   r   rQ   r4   r<   r=   rU   Enumr_   rc   ABCMetare   registerr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r$   r$   r$   r%   <module>   s   $	


$mtU





\ n I