o
    >b0n                     @   sJ  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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 d dlmZmZmZ d dlmZ d d	lmZ ed
ddZG dd deZdedeje fddZ dedejej!ee"f  fddZ#dejdejfddZ$G dd dej%Z&G dd deZ'G dd de j(d Z)G d!d" d"e j(d Z*G d#d$ d$e j(d Z+G d%d& d&e j(d Z,d>d'e"de)fd(d)Z-d>d'e"de)fd*d+Z.d>d'e"de,fd,d-Z/d>d'e"de,fd.d/Z0d>d'e"de+fd0d1Z1d>d'e"de+fd2d3Z2G d4d5 d5e3Z4G d6d7 d7e3Z5G d8d9 d9e3Z6G d:d; d;e3Z7de8fd<d=Z9dS )?    N)utils)_PRIVATE_KEY_TYPES_PUBLIC_KEY_TYPES)_get_backend)hashesserialization)dsaeced25519ed448rsa)	ExtensionExtensionType
Extensions)Name)ObjectIdentifieri     c                          e Zd Z fddZ  ZS )AttributeNotFoundc                       t t| | || _d S N)superr   __init__oid)selfmsgr   	__class__ 8/usr/lib/python3/dist-packages/cryptography/x509/base.pyr         
zAttributeNotFound.__init____name__
__module____qualname__r   __classcell__r   r   r   r   r          r   	extension
extensionsc                 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
   r+   r   
attributesc                 C   s"   |D ]\}}|| krt dqd S )Nz$This attribute has already been set.)r)   )r   r,   Zattr_oid_r   r   r   _reject_duplicate_attribute-   s
   r.   timereturnc                 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)r1   Z	utcoffsetdatetimeZ	timedeltareplace)r/   offsetr   r   r   _convert_to_naive_utc_time7   s
   
r5   c                   @   s   e Zd ZdZdZdS )Versionr      N)r"   r#   r$   Zv1v3r   r   r   r   r6   E   s    r6   c                       r   )InvalidVersionc                    r   r   )r   r9   r   parsed_version)r   r   r:   r   r   r   r   K   r    zInvalidVersion.__init__r!   r   r   r   r   r9   J   r&   r9   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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	algorithmr0   c                 C      dS z4
        Returns bytes using digest passed.
        Nr   r   r<   r   r   r   fingerprintQ       zCertificate.fingerprintc                 C   r=   )z3
        Returns certificate serial number
        Nr   r   r   r   r   serial_numberW   rA   zCertificate.serial_numberc                 C   r=   )z1
        Returns the certificate version
        Nr   rB   r   r   r   version]   rA   zCertificate.versionc                 C   r=   z(
        Returns the public key
        Nr   rB   r   r   r   
public_keyc   rA   zCertificate.public_keyc                 C   r=   )z?
        Not before time (represented as UTC datetime)
        Nr   rB   r   r   r   not_valid_beforei   rA   zCertificate.not_valid_beforec                 C   r=   )z>
        Not after time (represented as UTC datetime)
        Nr   rB   r   r   r   not_valid_aftero   rA   zCertificate.not_valid_afterc                 C   r=   )z1
        Returns the issuer name object.
        Nr   rB   r   r   r   issueru   rA   zCertificate.issuerc                 C   r=   z2
        Returns the subject name object.
        Nr   rB   r   r   r   subject{   rA   zCertificate.subjectc                 C   r=   zt
        Returns a HashAlgorithm corresponding to the type of the digest signed
        in the certificate.
        Nr   rB   r   r   r   signature_hash_algorithm   rA   z$Certificate.signature_hash_algorithmc                 C   r=   zJ
        Returns the ObjectIdentifier of the signature algorithm.
        Nr   rB   r   r   r   signature_algorithm_oid   rA   z#Certificate.signature_algorithm_oidc                 C   r=   )z/
        Returns an Extensions object.
        Nr   rB   r   r   r   r(      rA   zCertificate.extensionsc                 C   r=   z.
        Returns the signature bytes.
        Nr   rB   r   r   r   	signature   rA   zCertificate.signaturec                 C   r=   )zR
        Returns the tbsCertificate payload bytes as defined in RFC 5280.
        Nr   rB   r   r   r   tbs_certificate_bytes   rA   z!Certificate.tbs_certificate_bytesotherc                 C   r=   z"
        Checks equality.
        Nr   r   rS   r   r   r   __eq__   rA   zCertificate.__eq__c                 C   r=   z#
        Checks not equal.
        Nr   rU   r   r   r   __ne__   rA   zCertificate.__ne__c                 C   r=   z"
        Computes a hash.
        Nr   rB   r   r   r   __hash__   rA   zCertificate.__hash__encodingc                 C   r=   )zB
        Serializes the certificate to PEM or DER format.
        Nr   r   r[   r   r   r   public_bytes   rA   zCertificate.public_bytesN)'r"   r#   r$   abcabstractmethodr   HashAlgorithmbytesr@   abstractpropertyintrC   r6   rD   r   rF   r2   rG   rH   r   rI   rK   typingOptionalrM   r   rO   r   r(   rQ   rR   objectboolrV   rX   rZ   r   Encodingr]   r   r   r   r   r;   P   sJ    
r;   )	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 )	RevokedCertificater0   c                 C   r=   )zG
        Returns the serial number of the revoked certificate.
        Nr   rB   r   r   r   rC      rA   z RevokedCertificate.serial_numberc                 C   r=   )zH
        Returns the date of when this certificate was revoked.
        Nr   rB   r   r   r   revocation_date   rA   z"RevokedCertificate.revocation_datec                 C   r=   )zW
        Returns an Extensions object containing a list of Revoked extensions.
        Nr   rB   r   r   r   r(      rA   zRevokedCertificate.extensionsN)r"   r#   r$   r^   rb   rc   rC   r2   rk   r   r(   r   r   r   r   rj      s    rj   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
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fddZejde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"d# Z#ejd$d% Z$ejd&e%defd'd(Z&d)S )*CertificateRevocationListr[   r0   c                 C   r=   )z:
        Serializes the CRL to PEM or DER format.
        Nr   r\   r   r   r   r]      rA   z&CertificateRevocationList.public_bytesr<   c                 C   r=   r>   r   r?   r   r   r   r@      rA   z%CertificateRevocationList.fingerprintrC   c                 C   r=   )zs
        Returns an instance of RevokedCertificate or None if the serial_number
        is not in the CRL.
        Nr   )r   rC   r   r   r   (get_revoked_certificate_by_serial_number   rA   zBCertificateRevocationList.get_revoked_certificate_by_serial_numberc                 C   r=   rL   r   rB   r   r   r   rM      rA   z2CertificateRevocationList.signature_hash_algorithmc                 C   r=   rN   r   rB   r   r   r   rO      rA   z1CertificateRevocationList.signature_algorithm_oidc                 C   r=   )zC
        Returns the X509Name with the issuer of this CRL.
        Nr   rB   r   r   r   rI      rA   z CertificateRevocationList.issuerc                 C   r=   )z?
        Returns the date of next update for this CRL.
        Nr   rB   r   r   r   next_update   rA   z%CertificateRevocationList.next_updatec                 C   r=   )z?
        Returns the date of last update for this CRL.
        Nr   rB   r   r   r   last_update   rA   z%CertificateRevocationList.last_updatec                 C   r=   )zS
        Returns an Extensions object containing a list of CRL extensions.
        Nr   rB   r   r   r   r(     rA   z$CertificateRevocationList.extensionsc                 C   r=   rP   r   rB   r   r   r   rQ   
  rA   z#CertificateRevocationList.signaturec                 C   r=   )zO
        Returns the tbsCertList payload bytes as defined in RFC 5280.
        Nr   rB   r   r   r   tbs_certlist_bytes  rA   z,CertificateRevocationList.tbs_certlist_bytesrS   c                 C   r=   rT   r   rU   r   r   r   rV     rA   z CertificateRevocationList.__eq__c                 C   r=   rW   r   rU   r   r   r   rX     rA   z CertificateRevocationList.__ne__c                 C   r=   )z<
        Number of revoked certificates in the CRL.
        Nr   rB   r   r   r   __len__"  rA   z!CertificateRevocationList.__len__c                 C   r=   )zS
        Returns a revoked certificate (or slice of revoked certificates).
        Nr   )r   idxr   r   r   __getitem__(  rA   z%CertificateRevocationList.__getitem__c                 C   r=   )z8
        Iterator over the revoked certificates
        Nr   rB   r   r   r   __iter__.  rA   z"CertificateRevocationList.__iter__rF   c                 C   r=   )zQ
        Verifies signature of revocation list against given public key.
        Nr   )r   rF   r   r   r   is_signature_valid4  rA   z,CertificateRevocationList.is_signature_validN)'r"   r#   r$   r^   r_   r   rh   ra   r]   r   r`   r@   rc   rd   re   rj   rm   rb   rM   r   rO   r   rI   r2   rn   ro   r   r(   rQ   rp   rf   rg   rV   rX   rq   rs   rt   r   ru   r   r   r   r   rl      sN    

rl   c                   @   s$  e Zd Zejde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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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 ) CertificateSigningRequestrS   r0   c                 C   r=   rT   r   rU   r   r   r   rV   <  rA   z CertificateSigningRequest.__eq__c                 C   r=   rW   r   rU   r   r   r   rX   B  rA   z CertificateSigningRequest.__ne__c                 C   r=   rY   r   rB   r   r   r   rZ   H  rA   z"CertificateSigningRequest.__hash__c                 C   r=   rE   r   rB   r   r   r   rF   N  rA   z$CertificateSigningRequest.public_keyc                 C   r=   rJ   r   rB   r   r   r   rK   T  rA   z!CertificateSigningRequest.subjectc                 C   r=   rL   r   rB   r   r   r   rM   Z  rA   z2CertificateSigningRequest.signature_hash_algorithmc                 C   r=   rN   r   rB   r   r   r   rO   a  rA   z1CertificateSigningRequest.signature_algorithm_oidc                 C   r=   )z@
        Returns the extensions in the signing request.
        Nr   rB   r   r   r   r(   g  rA   z$CertificateSigningRequest.extensionsr[   c                 C   r=   )z;
        Encodes the request to PEM or DER format.
        Nr   r\   r   r   r   r]   m  rA   z&CertificateSigningRequest.public_bytesc                 C   r=   rP   r   rB   r   r   r   rQ   s  rA   z#CertificateSigningRequest.signaturec                 C   r=   )zd
        Returns the PKCS#10 CertificationRequestInfo bytes as defined in RFC
        2986.
        Nr   rB   r   r   r   tbs_certrequest_bytesy  rA   z/CertificateSigningRequest.tbs_certrequest_bytesc                 C   r=   )z8
        Verifies signature of signing request.
        Nr   rB   r   r   r   ru     rA   z,CertificateSigningRequest.is_signature_validr   c                 C   r=   )z:
        Get the attribute value for a given OID.
        Nr   )r   r   r   r   r   get_attribute_for_oid  rA   z/CertificateSigningRequest.get_attribute_for_oidN)r"   r#   r$   r^   r_   rf   rg   rV   rX   rc   rZ   r   rF   rb   r   rK   r   r`   rM   r   rO   r   r(   r   rh   ra   r]   rQ   rw   ru   rx   r   r   r   r   rv   ;  s6    rv   datac                 C      t |}|| S r   )r   load_pem_x509_certificatery   backendr   r   r   r{        
r{   c                 C   rz   r   )r   load_der_x509_certificater|   r   r   r   r     r~   r   c                 C   rz   r   )r   load_pem_x509_csrr|   r   r   r   r     r~   r   c                 C   rz   r   )r   load_der_x509_csrr|   r   r   r   r     r~   r   c                 C   rz   r   )r   load_pem_x509_crlr|   r   r   r   r     r~   r   c                 C   rz   r   )r   load_der_x509_crlr|   r   r   r   r     r~   r   c                   @   sj   e Zd Zdg g fddZdefddZdedefd	d
Zde	de
fddZ	ddedejdefddZdS ) CertificateSigningRequestBuilderNc                 C   s   || _ || _|| _dS )zB
        Creates an empty X.509 certificate request (v1).
        N)_subject_name_extensions_attributes)r   subject_namer(   r,   r   r   r   r     s   
z)CertificateSigningRequestBuilder.__init__namec                 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.)
isinstancer   	TypeErrorr   r)   r   r   r   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)
r   r   r   r   r   r+   r   r   r   r   r   r   r   r'   r   r   r   add_extension  s   

z.CertificateSigningRequestBuilder.add_extensionr   valuec                 C   sL   t |ts	tdt |tstdt|| j 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 bytes)	r   r   r   ra   r.   r   r   r   r   )r   r   r   r   r   r   add_attribute  s   

z.CertificateSigningRequestBuilder.add_attributeprivate_keyr<   r0   c                 C   s(   t |}| jdu rtd|| ||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   r<   r}   r   r   r   sign  s   	
z%CertificateSigningRequestBuilder.signr   )r"   r#   r$   r   r   r   r   rg   r   r   ra   r   r   r   r`   rv   r   r   r   r   r   r     s    r   c                   @   s   e Zd Zddddddg fddZdefddZdefddZd	efd
dZde	fddZ
dejfddZdejfddZdedefddZ	ddedejdefddZdS )CertificateBuilderNc                 C   s6   t j| _|| _|| _|| _|| _|| _|| _|| _	d S r   )
r6   r8   Z_version_issuer_namer   _public_key_serial_number_not_valid_before_not_valid_afterr   )r   issuer_namer   rF   rC   rG   rH   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.)r   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   )r   r   r   r   r)   r   r   r   r   r   r   r   r   r   r   r   r     s   

zCertificateBuilder.subject_namekeyc                 C   sX   t |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).
        zhExpecting one of DSAPublicKey, RSAPublicKey, EllipticCurvePublicKey, Ed25519PublicKey or Ed448PublicKey.Nz$The public key may only be set once.)r   r   ZDSAPublicKeyr   ZRSAPublicKeyr	   ZEllipticCurvePublicKeyr
   ZEd25519PublicKeyr   ZEd448PublicKeyr   r   r)   r   r   r   r   r   r   r   )r   r   r   r   r   rF   )  s.   

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.)r   rc   r   r   r)   
bit_lengthr   r   r   r   r   r   r   r   r   r   r   r   rC   K  s&   

z CertificateBuilder.serial_numberr/   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.)r   r2   r   r   r)   r5   _EARLIEST_UTC_TIMEr   r   r   r   r   r   r   r   r/   r   r   r   rG   f  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.)r   r2   r   r   r)   r5   r   r   r   r   r   r   r   r   r   r   r   r   rH     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   )r   r   r   r   r   r+   r   r   r   r   r   r   r   r   r   r   r   r   r     s   

z CertificateBuilder.add_extensionr   r<   r0   c                 C   s   t |}| 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 r1td| jdu r:td|| ||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$   r   r   r   r   r   rF   rc   rC   r2   rG   rH   r   rg   r   r   r   r`   r;   r   r   r   r   r   r     s4    

" r   c                   @   s   e Zd Zdddg g fddZdefddZdejfdd	Zd
ejfddZde	de
fddZdefddZ	ddedejdefddZdS ) CertificateRevocationListBuilderNc                 C   s"   || _ || _|| _|| _|| _d S r   )r   _last_update_next_updater   _revoked_certificates)r   r   ro   rn   r(   Zrevoked_certificatesr   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   )
r   r   r   r   r)   r   r   r   r   r   )r   r   r   r   r   r     s   

z,CertificateRevocationListBuilder.issuer_namero   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.)r   r2   r   r   r)   r5   r   r   r   r   r   r   )r   ro   r   r   r   ro     s(   
z,CertificateRevocationListBuilder.last_updatern   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   r   r   z8The next update date must be after the last update date.)r   r2   r   r   r)   r5   r   r   r   r   r   r   )r   rn   r   r   r   rn     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   )r   r   r   r   r   r+   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)	r   rj   r   r   r   r   r   r   r   )r   r   r   r   r   add_revoked_certificate/  s   

z8CertificateRevocationListBuilder.add_revoked_certificater   r<   r0   c                 C   sL   t |}| jd u rtd| jd u rtd| jd u rtd|| ||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$   r   r   r   r2   ro   rn   r   rg   r   rj   r   r   r   r`   rl   r   r   r   r   r   r     s(    
r   c                   @   s\   e Zd Zddg fddZdefddZdejfdd	Zd
ede	fddZ
ddefddZdS )RevokedCertificateBuilderNc                 C   s   || _ || _|| _d S r   )r   _revocation_dater   )r   rC   rk   r(   r   r   r   r   R  s   
z"RevokedCertificateBuilder.__init__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   )	r   rc   r   r   r)   r   r   r   r   r   r   r   r   rC   Y  s   


z'RevokedCertificateBuilder.serial_numberr/   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.)
r   r2   r   r   r)   r5   r   r   r   r   r   r   r   r   rk   k  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   )
r   r   r   r   r   r+   r   r   r   r   r   r   r   r   r   y  s   

z'RevokedCertificateBuilder.add_extensionr0   c                 C   s6   t |}| jd u rtd| jd u rtd|| S )Nz/A revoked certificate must have a serial numberz1A revoked certificate must have a revocation date)r   r   r)   r   Zcreate_x509_revoked_certificate)r   r}   r   r   r   build  s   


zRevokedCertificateBuilder.buildr   )r"   r#   r$   r   rc   rC   r2   rk   r   rg   r   rj   r   r   r   r   r   r   Q  s    
r   c                   C   s   t tddd? S )N   Zbigr   )rc   
from_bytesosurandomr   r   r   r   random_serial_number  s   r   r   ):r^   r2   r   rd   Zcryptographyr   Zcryptography.hazmat._typesr   r   Zcryptography.hazmat.backendsr   Zcryptography.hazmat.primitivesr   r   Z)cryptography.hazmat.primitives.asymmetricr   r	   r
   r   r   Zcryptography.x509.extensionsr   r   r   Zcryptography.x509.namer   Zcryptography.x509.oidr   r   	Exceptionr   ZListr+   ZTuplera   r.   r5   Enumr6   r9   ABCMetar;   rj   rl   rv   r{   r   r   r   r   r   rf   r   r   r   r   rc   r   r   r   r   r   <module>   sV   
	

klRF f{@