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__ C/opt/certbot/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   attr_oid_r$   r$   r%   _reject_duplicate_attribute8   s
   r5   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)r7   	utcoffsetdatetime	timedeltareplace)r6   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   )rB   r!   r$   r$   r%   r   ]      zAttribute.oidc                 C   rE   r   )rC   rF   r$   r$   r%   r?   a   rG   zAttribute.valuec                 C   s   d | j| jS )Nz<Attribute(oid={}, value={!r})>)formatr   r?   rF   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!   rJ   r$   r$   r%   __eq__h   s   


zAttribute.__eq__c                 C   s   t | j| j| jfS r   )hashr   r?   r@   rF   r$   r$   r%   __hash__r   s   zAttribute.__hash__)r'   r(   r)   r   
UTF8Stringr?   r   bytesintr    propertyr   r*   rI   objectboolrN   rP   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__rY   c                 C   s   d | jS )Nz<Attributes({})>)rH   rY   rF   r$   r$   r%   rI      s   zAttributes.__repr__r   c                 C   s,   | D ]}|j |kr|  S qtd||)NzNo {} attribute was found)r   r   rH   )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*   rI   r   r[   r$   r$   r$   r%   rW   v   s    
rW   c                   @   s   e Zd ZdZdZdS )Versionr      N)r'   r(   r)   v1v3r$   r$   r$   r%   ra      s    ra   c                       r   )InvalidVersionr   parsed_versionr   Nc                    r   r   )r   re   r    rf   )r!   r   rf   r"   r$   r%   r       r&   zInvalidVersion.__init__)r'   r(   r)   r*   rS   r    r+   r$   r$   r"   r%   re      r,   re   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!   rh   r$   r$   r%   fingerprint       zCertificate.fingerprintc                 C   ri   )z3
        Returns certificate serial number
        Nr$   rF   r$   r$   r%   serial_number   rm   zCertificate.serial_numberc                 C   ri   )z1
        Returns the certificate version
        Nr$   rF   r$   r$   r%   version   rm   zCertificate.versionc                 C   ri   z(
        Returns the public key
        Nr$   rF   r$   r$   r%   
public_key   rm   zCertificate.public_keyc                 C   ri   )z?
        Not before time (represented as UTC datetime)
        Nr$   rF   r$   r$   r%   not_valid_before   rm   zCertificate.not_valid_beforec                 C   ri   )z>
        Not after time (represented as UTC datetime)
        Nr$   rF   r$   r$   r%   not_valid_after   rm   zCertificate.not_valid_afterc                 C   ri   )z1
        Returns the issuer name object.
        Nr$   rF   r$   r$   r%   issuer   rm   zCertificate.issuerc                 C   ri   z2
        Returns the subject name object.
        Nr$   rF   r$   r$   r%   subject   rm   zCertificate.subjectc                 C   ri   zt
        Returns a HashAlgorithm corresponding to the type of the digest signed
        in the certificate.
        Nr$   rF   r$   r$   r%   signature_hash_algorithm   rm   z$Certificate.signature_hash_algorithmc                 C   ri   zJ
        Returns the ObjectIdentifier of the signature algorithm.
        Nr$   rF   r$   r$   r%   signature_algorithm_oid   rm   z#Certificate.signature_algorithm_oidc                 C   ri   )z/
        Returns an Extensions object.
        Nr$   rF   r$   r$   r%   r.      rm   zCertificate.extensionsc                 C   ri   z.
        Returns the signature bytes.
        Nr$   rF   r$   r$   r%   	signature   rm   zCertificate.signaturec                 C   ri   )zR
        Returns the tbsCertificate payload bytes as defined in RFC 5280.
        Nr$   rF   r$   r$   r%   tbs_certificate_bytes   rm   z!Certificate.tbs_certificate_bytesc                 C   ri   )zh
        Returns the tbsCertificate payload bytes with the SCT list extension
        stripped.
        Nr$   rF   r$   r$   r%   tbs_precertificate_bytes   rm   z$Certificate.tbs_precertificate_bytesrJ   c                 C   ri   z"
        Checks equality.
        Nr$   rM   r$   r$   r%   rN      rm   zCertificate.__eq__c                 C   ri   z"
        Computes a hash.
        Nr$   rF   r$   r$   r%   rP      rm   zCertificate.__hash__encodingc                 C   ri   )zB
        Serializes the certificate to PEM or DER format.
        Nr$   r!   r   r$   r$   r%   public_bytes  rm   zCertificate.public_bytesN)'r'   r(   r)   abcabstractmethodr   HashAlgorithmrR   rl   rT   rS   rn   ra   ro   r   rq   r9   rr   rs   r   rt   rv   r\   Optionalrx   r   rz   r   r.   r|   r}   r~   rU   rV   rN   rP   r   Encodingr   r$   r$   r$   r%   rg      sb    
rg   )	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   ri   )zG
        Returns the serial number of the revoked certificate.
        Nr$   rF   r$   r$   r%   rn     rm   z RevokedCertificate.serial_numberc                 C   ri   )zH
        Returns the date of when this certificate was revoked.
        Nr$   rF   r$   r$   r%   revocation_date  rm   z"RevokedCertificate.revocation_datec                 C   ri   )zW
        Returns an Extensions object containing a list of Revoked extensions.
        Nr$   rF   r$   r$   r%   r.      rm   zRevokedCertificate.extensionsN)r'   r(   r)   rT   r   r   rS   rn   r9   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 )_RawRevokedCertificatern   r   r.   c                 C   rA   r   _serial_number_revocation_date_extensionsr!   rn   r   r.   r$   r$   r%   r    -  rD   z_RawRevokedCertificate.__init__r   c                 C   rE   r   )r   rF   r$   r$   r%   rn   7  rG   z$_RawRevokedCertificate.serial_numberc                 C   rE   r   )r   rF   r$   r$   r%   r   ;  rG   z&_RawRevokedCertificate.revocation_datec                 C   rE   r   )r   rF   r$   r$   r%   r.   ?  rG   z!_RawRevokedCertificate.extensionsN)r'   r(   r)   rS   r9   r   r    rT   rn   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   ri   )z:
        Serializes the CRL to PEM or DER format.
        Nr$   r   r$   r$   r%   r   E  rm   z&CertificateRevocationList.public_bytesrh   c                 C   ri   rj   r$   rk   r$   r$   r%   rl   K  rm   z%CertificateRevocationList.fingerprintrn   c                 C   ri   )zs
        Returns an instance of RevokedCertificate or None if the serial_number
        is not in the CRL.
        Nr$   )r!   rn   r$   r$   r%   (get_revoked_certificate_by_serial_numberQ  rm   zBCertificateRevocationList.get_revoked_certificate_by_serial_numberc                 C   ri   rw   r$   rF   r$   r$   r%   rx   Z  rm   z2CertificateRevocationList.signature_hash_algorithmc                 C   ri   ry   r$   rF   r$   r$   r%   rz   d  rm   z1CertificateRevocationList.signature_algorithm_oidc                 C   ri   )zC
        Returns the X509Name with the issuer of this CRL.
        Nr$   rF   r$   r$   r%   rt   k  rm   z CertificateRevocationList.issuerc                 C   ri   )z?
        Returns the date of next update for this CRL.
        Nr$   rF   r$   r$   r%   next_updater  rm   z%CertificateRevocationList.next_updatec                 C   ri   )z?
        Returns the date of last update for this CRL.
        Nr$   rF   r$   r$   r%   last_updatey  rm   z%CertificateRevocationList.last_updatec                 C   ri   )zS
        Returns an Extensions object containing a list of CRL extensions.
        Nr$   rF   r$   r$   r%   r.     rm   z$CertificateRevocationList.extensionsc                 C   ri   r{   r$   rF   r$   r$   r%   r|     rm   z#CertificateRevocationList.signaturec                 C   ri   )zO
        Returns the tbsCertList payload bytes as defined in RFC 5280.
        Nr$   rF   r$   r$   r%   tbs_certlist_bytes  rm   z,CertificateRevocationList.tbs_certlist_bytesrJ   c                 C   ri   r   r$   rM   r$   r$   r%   rN     rm   z CertificateRevocationList.__eq__c                 C   ri   )z<
        Number of revoked certificates in the CRL.
        Nr$   rF   r$   r$   r%   r^     rm   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   ri   )zS
        Returns a revoked certificate (or slice of revoked certificates).
        Nr$   r   r$   r$   r%   r`     rm   c                 C   ri   )z8
        Iterator over the revoked certificates
        Nr$   rF   r$   r$   r%   r_     rm   z"CertificateRevocationList.__iter__rq   c                 C   ri   )zQ
        Verifies signature of revocation list against given public key.
        Nr$   )r!   rq   r$   r$   r%   is_signature_valid  rm   z,CertificateRevocationList.is_signature_validN)+r'   r(   r)   r   r   r   r   rR   r   r   r   rl   rS   r\   r   r   r   rT   rx   r   rz   r   rt   r9   r   r   r   r.   r|   r   rU   rV   rN   r^   overloadr`   sliceList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 ) CertificateSigningRequestrJ   r   c                 C   ri   r   r$   rM   r$   r$   r%   rN     rm   z CertificateSigningRequest.__eq__c                 C   ri   r   r$   rF   r$   r$   r%   rP     rm   z"CertificateSigningRequest.__hash__c                 C   ri   rp   r$   rF   r$   r$   r%   rq     rm   z$CertificateSigningRequest.public_keyc                 C   ri   ru   r$   rF   r$   r$   r%   rv     rm   z!CertificateSigningRequest.subjectc                 C   ri   rw   r$   rF   r$   r$   r%   rx     rm   z2CertificateSigningRequest.signature_hash_algorithmc                 C   ri   ry   r$   rF   r$   r$   r%   rz     rm   z1CertificateSigningRequest.signature_algorithm_oidc                 C   ri   )z@
        Returns the extensions in the signing request.
        Nr$   rF   r$   r$   r%   r.     rm   z$CertificateSigningRequest.extensionsc                 C   ri   )z/
        Returns an Attributes object.
        Nr$   rF   r$   r$   r%   r2     rm   z$CertificateSigningRequest.attributesr   c                 C   ri   )z;
        Encodes the request to PEM or DER format.
        Nr$   r   r$   r$   r%   r     rm   z&CertificateSigningRequest.public_bytesc                 C   ri   r{   r$   rF   r$   r$   r%   r|     rm   z#CertificateSigningRequest.signaturec                 C   ri   )zd
        Returns the PKCS#10 CertificationRequestInfo bytes as defined in RFC
        2986.
        Nr$   rF   r$   r$   r%   tbs_certrequest_bytes	  rm   z/CertificateSigningRequest.tbs_certrequest_bytesc                 C   ri   )z8
        Verifies signature of signing request.
        Nr$   rF   r$   r$   r%   r     rm   z,CertificateSigningRequest.is_signature_validr   c                 C   ri   )z:
        Get the attribute value for a given OID.
        Nr$   )r!   r   r$   r$   r%   r[     rm   z/CertificateSigningRequest.get_attribute_for_oidN)"r'   r(   r)   r   r   rU   rV   rN   rS   rP   r   rq   rT   r   rv   r\   r   r   r   rx   r   rz   r   r.   rW   r2   r   r   rR   r   r|   r   r   r[   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   rY   )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.)rK   r   	TypeErrorr   r/   r   r   rY   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)
rK   r   r   r   r   r1   r   r   r   rY   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)rK   r   r   rR   r   r5   rY   r?   r   r   r   )r!   r   r?   r   tagr$   r$   r%   add_attribute  s   


z.CertificateSigningRequestBuilder.add_attributeprivate_keyrh   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   create_x509_csrr!   r   rh   r   r$   r$   r%   sign  s   
	z%CertificateSigningRequestBuilder.signr   )r'   r(   r)   r\   r   r   r   r   r   Tupler   rR   rS   r    r   rV   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   rq   rn   rr   rs   r.   r   c                 C   s6   t j| _|| _|| _|| _|| _|| _|| _|| _	d S r   )
ra   rd   _version_issuer_namer   _public_keyr   _not_valid_before_not_valid_afterr   )r!   r   r   rq   rn   rr   rs   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.)rK   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   )rK   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.)rK   r   DSAPublicKeyr
   RSAPublicKeyr   EllipticCurvePublicKeyr	   Ed25519PublicKeyr   Ed448PublicKeyr   X25519PublicKeyr   X448PublicKeyr   r   r/   r   r   r   r   r   r   r   )r!   r   r$   r$   r%   rq     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.)rK   rS   r   r   r/   
bit_lengthr   r   r   r   r   r   r   r!   r   r$   r$   r%   rn     s&   

z CertificateBuilder.serial_numberr6   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.)rK   r9   r   r   r/   r=   _EARLIEST_UTC_TIMEr   r   r   r   r   r   r   r!   r6   r$   r$   r%   rr   '  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.)rK   r9   r   r   r/   r=   r   r   r   r   r   r   r   r   r   r$   r$   r%   rs   F  s2   
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   )rK   r   r   r   r   r1   r   r   r   r   r   r   r   r   r   r$   r$   r%   r   f  s   

z CertificateBuilder.add_extensionr   rh   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   create_x509_certificater   r$   r$   r%   r   |  s   
	




zCertificateBuilder.signr   )r'   r(   r)   r\   r   r   r   __annotations__r   r   r   rS   r9   r    r   r   rq   rn   rr   rs   rV   r   r   r   r   r   rg   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   )
rK   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.)rK   r9   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.)rK   r9   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   )rK   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)	rK   r   r   r   r   r   r   r   r   )r!   r   r$   r$   r%   add_revoked_certificate  s   

z8CertificateRevocationListBuilder.add_revoked_certificater   rh   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   create_x509_crlr   r$   r$   r%   r     s   


z%CertificateRevocationListBuilder.signr   )r'   r(   r)   r\   r   r   r   r   r   r   r   r9   r    r   r   r   rV   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 )RevokedCertificateBuilderNrn   r   r.   c                 C   rA   r   r   r   r$   r$   r%   r    "  rD   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   )	rK   rS   r   r   r/   r   r   r   r   r   r$   r$   r%   rn   ,  s   


z'RevokedCertificateBuilder.serial_numberr6   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.)
rK   r9   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   )
rK   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)   r\   r   rS   r9   r   r   r   r    rn   r   rV   r   r   r   r   r$   r$   r$   r%   r   !  s2    




r   c                   C   s   t tddd? S )N   bigr   )rS   
from_bytesosurandomr$   r$   r$   r%   random_serial_numberj  s   r   r   )Fr   r9   r   r\   cryptographyr   "cryptography.hazmat.bindings._rustr   r   cryptography.hazmat.primitivesr   r   )cryptography.hazmat.primitives.asymmetricr   r   r   r	   r
   r   r   /cryptography.hazmat.primitives.asymmetric.typesr   r   r   cryptography.x509.extensionsr   r   r   r   cryptography.x509.namer   r   cryptography.x509.oidr   r   	Exceptionr   r   r1   r   rR   r   rS   r5   r=   r>   rW   Enumra   re   ABCMetarg   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